booke_emulate.c 7.4 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
}

int kvmppc_booke_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
                            unsigned int inst, int *advance)
{
	int emulated = EMULATE_DONE;
	int rs;
	int rt;

	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:
			rt = get_rt(inst);
66
			kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->msr);
67 68 69 70 71 72
			kvmppc_set_exit_type(vcpu, EMULATED_MFMSR_EXITS);
			break;

		case OP_31_XOP_MTMSR:
			rs = get_rs(inst);
			kvmppc_set_exit_type(vcpu, EMULATED_MTMSR_EXITS);
73
			kvmppc_set_msr(vcpu, kvmppc_get_gpr(vcpu, rs));
74 75 76 77
			break;

		case OP_31_XOP_WRTEE:
			rs = get_rs(inst);
78
			vcpu->arch.shared->msr = (vcpu->arch.shared->msr & ~MSR_EE)
79
					| (kvmppc_get_gpr(vcpu, rs) & MSR_EE);
80 81 82 83
			kvmppc_set_exit_type(vcpu, EMULATED_WRTEE_EXITS);
			break;

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

		default:
			emulated = EMULATE_FAIL;
		}

		break;

	default:
		emulated = EMULATE_FAIL;
	}

	return emulated;
}

102 103 104 105 106 107
/*
 * 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).
 */
108 109 110
int kvmppc_booke_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
{
	int emulated = EMULATE_DONE;
111
	ulong spr_val = kvmppc_get_gpr(vcpu, rs);
112 113 114

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

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

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

	default:
		emulated = EMULATE_FAIL;
	}

	return emulated;
}

int kvmppc_booke_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
{
	int emulated = EMULATE_DONE;

	switch (sprn) {
	case SPRN_IVPR:
219
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivpr); break;
220
	case SPRN_DEAR:
221
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->dar); break;
222
	case SPRN_ESR:
223
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->esr); break;
224
	case SPRN_DBCR0:
225
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.dbcr0); break;
226
	case SPRN_DBCR1:
227
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.dbcr1); break;
228
	case SPRN_DBSR:
229
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.dbsr); break;
230 231 232 233
	case SPRN_TSR:
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.tsr); break;
	case SPRN_TCR:
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.tcr); break;
234 235

	case SPRN_IVOR0:
236
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL]);
237 238
		break;
	case SPRN_IVOR1:
239
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK]);
240 241
		break;
	case SPRN_IVOR2:
242
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE]);
243 244
		break;
	case SPRN_IVOR3:
245
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE]);
246 247
		break;
	case SPRN_IVOR4:
248
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL]);
249 250
		break;
	case SPRN_IVOR5:
251
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT]);
252 253
		break;
	case SPRN_IVOR6:
254
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM]);
255 256
		break;
	case SPRN_IVOR7:
257
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL]);
258 259
		break;
	case SPRN_IVOR8:
260
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL]);
261 262
		break;
	case SPRN_IVOR9:
263
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL]);
264 265
		break;
	case SPRN_IVOR10:
266
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER]);
267 268
		break;
	case SPRN_IVOR11:
269
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_FIT]);
270 271
		break;
	case SPRN_IVOR12:
272
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG]);
273 274
		break;
	case SPRN_IVOR13:
275
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS]);
276 277
		break;
	case SPRN_IVOR14:
278
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS]);
279 280
		break;
	case SPRN_IVOR15:
281
		kvmppc_set_gpr(vcpu, rt, vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG]);
282 283 284 285 286 287 288 289
		break;

	default:
		emulated = EMULATE_FAIL;
	}

	return emulated;
}