booke_emulate.c 7.8 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 240 241 242 243 244 245 246 247
		break;

	default:
		emulated = EMULATE_FAIL;
	}

	return emulated;
}

248
int kvmppc_booke_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
249 250 251 252 253
{
	int emulated = EMULATE_DONE;

	switch (sprn) {
	case SPRN_IVPR:
254 255
		*spr_val = vcpu->arch.ivpr;
		break;
256
	case SPRN_DEAR:
257 258
		*spr_val = vcpu->arch.shared->dar;
		break;
259
	case SPRN_ESR:
260 261
		*spr_val = vcpu->arch.shared->esr;
		break;
262 263 264 265 266 267
	case SPRN_CSRR0:
		*spr_val = vcpu->arch.csrr0;
		break;
	case SPRN_CSRR1:
		*spr_val = vcpu->arch.csrr1;
		break;
268
	case SPRN_DBCR0:
269
		*spr_val = vcpu->arch.dbg_reg.dbcr0;
270
		break;
271
	case SPRN_DBCR1:
272
		*spr_val = vcpu->arch.dbg_reg.dbcr1;
273
		break;
274
	case SPRN_DBSR:
275 276
		*spr_val = vcpu->arch.dbsr;
		break;
277
	case SPRN_TSR:
278 279
		*spr_val = vcpu->arch.tsr;
		break;
280
	case SPRN_TCR:
281 282
		*spr_val = vcpu->arch.tcr;
		break;
283 284

	case SPRN_IVOR0:
285
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
286 287
		break;
	case SPRN_IVOR1:
288
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
289 290
		break;
	case SPRN_IVOR2:
291
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
292 293
		break;
	case SPRN_IVOR3:
294
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
295 296
		break;
	case SPRN_IVOR4:
297
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
298 299
		break;
	case SPRN_IVOR5:
300
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
301 302
		break;
	case SPRN_IVOR6:
303
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
304 305
		break;
	case SPRN_IVOR7:
306
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
307 308
		break;
	case SPRN_IVOR8:
309
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
310 311
		break;
	case SPRN_IVOR9:
312
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
313 314
		break;
	case SPRN_IVOR10:
315
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
316 317
		break;
	case SPRN_IVOR11:
318
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
319 320
		break;
	case SPRN_IVOR12:
321
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
322 323
		break;
	case SPRN_IVOR13:
324
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
325 326
		break;
	case SPRN_IVOR14:
327
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
328 329
		break;
	case SPRN_IVOR15:
330
		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
331 332 333 334 335 336 337 338
		break;

	default:
		emulated = EMULATE_FAIL;
	}

	return emulated;
}