booke_emulate.c 8.3 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
 *
 * 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
27
#define OP_19_XOP_RFCI    51
28 29 30 31 32 33 34 35

#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)
{
36 37
	vcpu->arch.pc = vcpu->arch.shared->srr0;
	kvmppc_set_msr(vcpu, vcpu->arch.shared->srr1);
38 39
}

40 41 42 43 44 45
static void kvmppc_emul_rfci(struct kvm_vcpu *vcpu)
{
	vcpu->arch.pc = vcpu->arch.csrr0;
	kvmppc_set_msr(vcpu, vcpu->arch.csrr1);
}

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

	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;

62 63 64 65 66 67
		case OP_19_XOP_RFCI:
			kvmppc_emul_rfci(vcpu);
			kvmppc_set_exit_type(vcpu, EMULATED_RFCI_EXITS);
			*advance = 0;
			break;

68 69 70 71 72 73 74 75 76 77
		default:
			emulated = EMULATE_FAIL;
			break;
		}
		break;

	case 31:
		switch (get_xop(inst)) {

		case OP_31_XOP_MFMSR:
78
			kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->msr);
79 80 81 82 83
			kvmppc_set_exit_type(vcpu, EMULATED_MFMSR_EXITS);
			break;

		case OP_31_XOP_MTMSR:
			kvmppc_set_exit_type(vcpu, EMULATED_MTMSR_EXITS);
84
			kvmppc_set_msr(vcpu, kvmppc_get_gpr(vcpu, rs));
85 86 87
			break;

		case OP_31_XOP_WRTEE:
88
			vcpu->arch.shared->msr = (vcpu->arch.shared->msr & ~MSR_EE)
89
					| (kvmppc_get_gpr(vcpu, rs) & MSR_EE);
90 91 92 93
			kvmppc_set_exit_type(vcpu, EMULATED_WRTEE_EXITS);
			break;

		case OP_31_XOP_WRTEEI:
94
			vcpu->arch.shared->msr = (vcpu->arch.shared->msr & ~MSR_EE)
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
							 | (inst & MSR_EE);
			kvmppc_set_exit_type(vcpu, EMULATED_WRTEE_EXITS);
			break;

		default:
			emulated = EMULATE_FAIL;
		}

		break;

	default:
		emulated = EMULATE_FAIL;
	}

	return emulated;
}

112 113 114 115 116 117
/*
 * 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).
 */
118
int kvmppc_booke_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
119 120 121 122 123
{
	int emulated = EMULATE_DONE;

	switch (sprn) {
	case SPRN_DEAR:
124 125
		vcpu->arch.shared->dar = spr_val;
		break;
126
	case SPRN_ESR:
127 128
		vcpu->arch.shared->esr = spr_val;
		break;
129 130 131 132 133 134
	case SPRN_CSRR0:
		vcpu->arch.csrr0 = spr_val;
		break;
	case SPRN_CSRR1:
		vcpu->arch.csrr1 = spr_val;
		break;
135
	case SPRN_DBCR0:
136
		vcpu->arch.dbg_reg.dbcr0 = spr_val;
137
		break;
138
	case SPRN_DBCR1:
139
		vcpu->arch.dbg_reg.dbcr1 = spr_val;
140
		break;
141
	case SPRN_DBSR:
142 143
		vcpu->arch.dbsr &= ~spr_val;
		break;
144
	case SPRN_TSR:
145 146
		kvmppc_clr_tsr_bits(vcpu, spr_val);
		break;
147
	case SPRN_TCR:
148 149 150 151 152 153 154 155
		/*
		 * WRC is a 2-bit field that is supposed to preserve its
		 * value once written to non-zero.
		 */
		if (vcpu->arch.tcr & TCR_WRC_MASK) {
			spr_val &= ~TCR_WRC_MASK;
			spr_val |= vcpu->arch.tcr & TCR_WRC_MASK;
		}
156
		kvmppc_set_tcr(vcpu, spr_val);
157 158
		break;

159 160 161
	case SPRN_DECAR:
		vcpu->arch.decar = spr_val;
		break;
162 163 164 165 166
	/*
	 * Note: SPRG4-7 are user-readable.
	 * These values are loaded into the real SPRGs when resuming the
	 * guest (PR-mode only).
	 */
167
	case SPRN_SPRG4:
168 169
		vcpu->arch.shared->sprg4 = spr_val;
		break;
170
	case SPRN_SPRG5:
171 172
		vcpu->arch.shared->sprg5 = spr_val;
		break;
173
	case SPRN_SPRG6:
174 175
		vcpu->arch.shared->sprg6 = spr_val;
		break;
176
	case SPRN_SPRG7:
177 178
		vcpu->arch.shared->sprg7 = spr_val;
		break;
179 180

	case SPRN_IVPR:
181
		vcpu->arch.ivpr = spr_val;
182 183 184
#ifdef CONFIG_KVM_BOOKE_HV
		mtspr(SPRN_GIVPR, spr_val);
#endif
185 186
		break;
	case SPRN_IVOR0:
187
		vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = spr_val;
188 189
		break;
	case SPRN_IVOR1:
190
		vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = spr_val;
191 192
		break;
	case SPRN_IVOR2:
193
		vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = spr_val;
194 195 196
#ifdef CONFIG_KVM_BOOKE_HV
		mtspr(SPRN_GIVOR2, spr_val);
#endif
197 198
		break;
	case SPRN_IVOR3:
199
		vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = spr_val;
200 201
		break;
	case SPRN_IVOR4:
202
		vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = spr_val;
203 204
		break;
	case SPRN_IVOR5:
205
		vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = spr_val;
206 207
		break;
	case SPRN_IVOR6:
208
		vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = spr_val;
209 210
		break;
	case SPRN_IVOR7:
211
		vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = spr_val;
212 213
		break;
	case SPRN_IVOR8:
214
		vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = spr_val;
215 216 217
#ifdef CONFIG_KVM_BOOKE_HV
		mtspr(SPRN_GIVOR8, spr_val);
#endif
218 219
		break;
	case SPRN_IVOR9:
220
		vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = spr_val;
221 222
		break;
	case SPRN_IVOR10:
223
		vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = spr_val;
224 225
		break;
	case SPRN_IVOR11:
226
		vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = spr_val;
227 228
		break;
	case SPRN_IVOR12:
229
		vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = spr_val;
230 231
		break;
	case SPRN_IVOR13:
232
		vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = spr_val;
233 234
		break;
	case SPRN_IVOR14:
235
		vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = spr_val;
236 237
		break;
	case SPRN_IVOR15:
238
		vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = spr_val;
239
		break;
240 241 242
	case SPRN_MCSR:
		vcpu->arch.mcsr &= ~spr_val;
		break;
243 244 245 246 247 248 249 250
#if defined(CONFIG_64BIT)
	case SPRN_EPCR:
		kvmppc_set_epcr(vcpu, spr_val);
#ifdef CONFIG_KVM_BOOKE_HV
		mtspr(SPRN_EPCR, vcpu->arch.shadow_epcr);
#endif
		break;
#endif
251 252 253 254 255 256 257
	default:
		emulated = EMULATE_FAIL;
	}

	return emulated;
}

258
int kvmppc_booke_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
259 260 261 262 263
{
	int emulated = EMULATE_DONE;

	switch (sprn) {
	case SPRN_IVPR:
264 265
		*spr_val = vcpu->arch.ivpr;
		break;
266
	case SPRN_DEAR:
267 268
		*spr_val = vcpu->arch.shared->dar;
		break;
269
	case SPRN_ESR:
270 271
		*spr_val = vcpu->arch.shared->esr;
		break;
272 273 274
	case SPRN_EPR:
		*spr_val = vcpu->arch.epr;
		break;
275 276 277 278 279 280
	case SPRN_CSRR0:
		*spr_val = vcpu->arch.csrr0;
		break;
	case SPRN_CSRR1:
		*spr_val = vcpu->arch.csrr1;
		break;
281
	case SPRN_DBCR0:
282
		*spr_val = vcpu->arch.dbg_reg.dbcr0;
283
		break;
284
	case SPRN_DBCR1:
285
		*spr_val = vcpu->arch.dbg_reg.dbcr1;
286
		break;
287
	case SPRN_DBSR:
288 289
		*spr_val = vcpu->arch.dbsr;
		break;
290
	case SPRN_TSR:
291 292
		*spr_val = vcpu->arch.tsr;
		break;
293
	case SPRN_TCR:
294 295
		*spr_val = vcpu->arch.tcr;
		break;
296 297

	case SPRN_IVOR0:
298
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
299 300
		break;
	case SPRN_IVOR1:
301
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
302 303
		break;
	case SPRN_IVOR2:
304
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
305 306
		break;
	case SPRN_IVOR3:
307
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
308 309
		break;
	case SPRN_IVOR4:
310
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
311 312
		break;
	case SPRN_IVOR5:
313
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
314 315
		break;
	case SPRN_IVOR6:
316
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
317 318
		break;
	case SPRN_IVOR7:
319
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
320 321
		break;
	case SPRN_IVOR8:
322
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
323 324
		break;
	case SPRN_IVOR9:
325
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
326 327
		break;
	case SPRN_IVOR10:
328
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
329 330
		break;
	case SPRN_IVOR11:
331
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
332 333
		break;
	case SPRN_IVOR12:
334
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
335 336
		break;
	case SPRN_IVOR13:
337
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
338 339
		break;
	case SPRN_IVOR14:
340
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
341 342
		break;
	case SPRN_IVOR15:
343
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
344
		break;
345 346 347
	case SPRN_MCSR:
		*spr_val = vcpu->arch.mcsr;
		break;
348 349 350 351 352
#if defined(CONFIG_64BIT)
	case SPRN_EPCR:
		*spr_val = vcpu->arch.epcr;
		break;
#endif
353 354 355 356 357 358 359

	default:
		emulated = EMULATE_FAIL;
	}

	return emulated;
}