booke_emulate.c 7.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Copyright IBM Corp. 2008
16
 * Copyright 2011 Freescale Semiconductor, Inc.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *
 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
 */

#include <linux/kvm_host.h>
#include <asm/disassemble.h>

#include "booke.h"

#define OP_19_XOP_RFI     50

#define OP_31_XOP_MFMSR   83
#define OP_31_XOP_WRTEE   131
#define OP_31_XOP_MTMSR   146
#define OP_31_XOP_WRTEEI  163

static void kvmppc_emul_rfi(struct kvm_vcpu *vcpu)
{
35 36
	vcpu->arch.pc = vcpu->arch.shared->srr0;
	kvmppc_set_msr(vcpu, vcpu->arch.shared->srr1);
37 38 39 40 41 42
}

int kvmppc_booke_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
                            unsigned int inst, int *advance)
{
	int emulated = EMULATE_DONE;
43 44
	int rs = get_rs(inst);
	int rt = get_rt(inst);
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

	switch (get_op(inst)) {
	case 19:
		switch (get_xop(inst)) {
		case OP_19_XOP_RFI:
			kvmppc_emul_rfi(vcpu);
			kvmppc_set_exit_type(vcpu, EMULATED_RFI_EXITS);
			*advance = 0;
			break;

		default:
			emulated = EMULATE_FAIL;
			break;
		}
		break;

	case 31:
		switch (get_xop(inst)) {

		case OP_31_XOP_MFMSR:
65
			kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->msr);
66 67 68 69 70
			kvmppc_set_exit_type(vcpu, EMULATED_MFMSR_EXITS);
			break;

		case OP_31_XOP_MTMSR:
			kvmppc_set_exit_type(vcpu, EMULATED_MTMSR_EXITS);
71
			kvmppc_set_msr(vcpu, kvmppc_get_gpr(vcpu, rs));
72 73 74
			break;

		case OP_31_XOP_WRTEE:
75
			vcpu->arch.shared->msr = (vcpu->arch.shared->msr & ~MSR_EE)
76
					| (kvmppc_get_gpr(vcpu, rs) & MSR_EE);
77 78 79 80
			kvmppc_set_exit_type(vcpu, EMULATED_WRTEE_EXITS);
			break;

		case OP_31_XOP_WRTEEI:
81
			vcpu->arch.shared->msr = (vcpu->arch.shared->msr & ~MSR_EE)
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
							 | (inst & MSR_EE);
			kvmppc_set_exit_type(vcpu, EMULATED_WRTEE_EXITS);
			break;

		default:
			emulated = EMULATE_FAIL;
		}

		break;

	default:
		emulated = EMULATE_FAIL;
	}

	return emulated;
}

99 100 101 102 103 104
/*
 * NOTE: some of these registers are not emulated on BOOKE_HV (GS-mode).
 * Their backing store is in real registers, and these functions
 * will return the wrong result if called for them in another context
 * (such as debugging).
 */
105
int kvmppc_booke_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
106 107 108 109 110
{
	int emulated = EMULATE_DONE;

	switch (sprn) {
	case SPRN_DEAR:
111 112
		vcpu->arch.shared->dar = spr_val;
		break;
113
	case SPRN_ESR:
114 115
		vcpu->arch.shared->esr = spr_val;
		break;
116
	case SPRN_DBCR0:
117 118
		vcpu->arch.dbcr0 = spr_val;
		break;
119
	case SPRN_DBCR1:
120 121
		vcpu->arch.dbcr1 = spr_val;
		break;
122
	case SPRN_DBSR:
123 124
		vcpu->arch.dbsr &= ~spr_val;
		break;
125
	case SPRN_TSR:
126 127
		kvmppc_clr_tsr_bits(vcpu, spr_val);
		break;
128
	case SPRN_TCR:
129
		kvmppc_set_tcr(vcpu, spr_val);
130 131
		break;

132 133 134
	case SPRN_DECAR:
		vcpu->arch.decar = spr_val;
		break;
135 136 137 138 139
	/*
	 * Note: SPRG4-7 are user-readable.
	 * These values are loaded into the real SPRGs when resuming the
	 * guest (PR-mode only).
	 */
140
	case SPRN_SPRG4:
141 142
		vcpu->arch.shared->sprg4 = spr_val;
		break;
143
	case SPRN_SPRG5:
144 145
		vcpu->arch.shared->sprg5 = spr_val;
		break;
146
	case SPRN_SPRG6:
147 148
		vcpu->arch.shared->sprg6 = spr_val;
		break;
149
	case SPRN_SPRG7:
150 151
		vcpu->arch.shared->sprg7 = spr_val;
		break;
152 153

	case SPRN_IVPR:
154
		vcpu->arch.ivpr = spr_val;
155 156 157
#ifdef CONFIG_KVM_BOOKE_HV
		mtspr(SPRN_GIVPR, spr_val);
#endif
158 159
		break;
	case SPRN_IVOR0:
160
		vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = spr_val;
161 162
		break;
	case SPRN_IVOR1:
163
		vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = spr_val;
164 165
		break;
	case SPRN_IVOR2:
166
		vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = spr_val;
167 168 169
#ifdef CONFIG_KVM_BOOKE_HV
		mtspr(SPRN_GIVOR2, spr_val);
#endif
170 171
		break;
	case SPRN_IVOR3:
172
		vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = spr_val;
173 174
		break;
	case SPRN_IVOR4:
175
		vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = spr_val;
176 177
		break;
	case SPRN_IVOR5:
178
		vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = spr_val;
179 180
		break;
	case SPRN_IVOR6:
181
		vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = spr_val;
182 183
		break;
	case SPRN_IVOR7:
184
		vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = spr_val;
185 186
		break;
	case SPRN_IVOR8:
187
		vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = spr_val;
188 189 190
#ifdef CONFIG_KVM_BOOKE_HV
		mtspr(SPRN_GIVOR8, spr_val);
#endif
191 192
		break;
	case SPRN_IVOR9:
193
		vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = spr_val;
194 195
		break;
	case SPRN_IVOR10:
196
		vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = spr_val;
197 198
		break;
	case SPRN_IVOR11:
199
		vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = spr_val;
200 201
		break;
	case SPRN_IVOR12:
202
		vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = spr_val;
203 204
		break;
	case SPRN_IVOR13:
205
		vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = spr_val;
206 207
		break;
	case SPRN_IVOR14:
208
		vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = spr_val;
209 210
		break;
	case SPRN_IVOR15:
211
		vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = spr_val;
212 213 214 215 216 217 218 219 220
		break;

	default:
		emulated = EMULATE_FAIL;
	}

	return emulated;
}

221
int kvmppc_booke_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
222 223 224 225 226
{
	int emulated = EMULATE_DONE;

	switch (sprn) {
	case SPRN_IVPR:
227 228
		*spr_val = vcpu->arch.ivpr;
		break;
229
	case SPRN_DEAR:
230 231
		*spr_val = vcpu->arch.shared->dar;
		break;
232
	case SPRN_ESR:
233 234
		*spr_val = vcpu->arch.shared->esr;
		break;
235
	case SPRN_DBCR0:
236 237
		*spr_val = vcpu->arch.dbcr0;
		break;
238
	case SPRN_DBCR1:
239 240
		*spr_val = vcpu->arch.dbcr1;
		break;
241
	case SPRN_DBSR:
242 243
		*spr_val = vcpu->arch.dbsr;
		break;
244
	case SPRN_TSR:
245 246
		*spr_val = vcpu->arch.tsr;
		break;
247
	case SPRN_TCR:
248 249
		*spr_val = vcpu->arch.tcr;
		break;
250 251

	case SPRN_IVOR0:
252
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
253 254
		break;
	case SPRN_IVOR1:
255
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
256 257
		break;
	case SPRN_IVOR2:
258
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
259 260
		break;
	case SPRN_IVOR3:
261
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
262 263
		break;
	case SPRN_IVOR4:
264
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
265 266
		break;
	case SPRN_IVOR5:
267
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
268 269
		break;
	case SPRN_IVOR6:
270
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
271 272
		break;
	case SPRN_IVOR7:
273
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
274 275
		break;
	case SPRN_IVOR8:
276
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
277 278
		break;
	case SPRN_IVOR9:
279
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
280 281
		break;
	case SPRN_IVOR10:
282
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
283 284
		break;
	case SPRN_IVOR11:
285
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
286 287
		break;
	case SPRN_IVOR12:
288
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
289 290
		break;
	case SPRN_IVOR13:
291
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
292 293
		break;
	case SPRN_IVOR14:
294
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
295 296
		break;
	case SPRN_IVOR15:
297
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
298 299 300 301 302 303 304 305
		break;

	default:
		emulated = EMULATE_FAIL;
	}

	return emulated;
}