xref: /openbmc/linux/arch/powerpc/kvm/e500_emulate.c (revision bc000245)
1 /*
2  * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved.
3  *
4  * Author: Yu Liu, <yu.liu@freescale.com>
5  *
6  * Description:
7  * This file is derived from arch/powerpc/kvm/44x_emulate.c,
8  * by Hollis Blanchard <hollisb@us.ibm.com>.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License, version 2, as
12  * published by the Free Software Foundation.
13  */
14 
15 #include <asm/kvm_ppc.h>
16 #include <asm/disassemble.h>
17 #include <asm/dbell.h>
18 
19 #include "booke.h"
20 #include "e500.h"
21 
22 #define XOP_MSGSND  206
23 #define XOP_MSGCLR  238
24 #define XOP_TLBIVAX 786
25 #define XOP_TLBSX   914
26 #define XOP_TLBRE   946
27 #define XOP_TLBWE   978
28 #define XOP_TLBILX  18
29 #define XOP_EHPRIV  270
30 
31 #ifdef CONFIG_KVM_E500MC
32 static int dbell2prio(ulong param)
33 {
34 	int msg = param & PPC_DBELL_TYPE_MASK;
35 	int prio = -1;
36 
37 	switch (msg) {
38 	case PPC_DBELL_TYPE(PPC_DBELL):
39 		prio = BOOKE_IRQPRIO_DBELL;
40 		break;
41 	case PPC_DBELL_TYPE(PPC_DBELL_CRIT):
42 		prio = BOOKE_IRQPRIO_DBELL_CRIT;
43 		break;
44 	default:
45 		break;
46 	}
47 
48 	return prio;
49 }
50 
51 static int kvmppc_e500_emul_msgclr(struct kvm_vcpu *vcpu, int rb)
52 {
53 	ulong param = vcpu->arch.gpr[rb];
54 	int prio = dbell2prio(param);
55 
56 	if (prio < 0)
57 		return EMULATE_FAIL;
58 
59 	clear_bit(prio, &vcpu->arch.pending_exceptions);
60 	return EMULATE_DONE;
61 }
62 
63 static int kvmppc_e500_emul_msgsnd(struct kvm_vcpu *vcpu, int rb)
64 {
65 	ulong param = vcpu->arch.gpr[rb];
66 	int prio = dbell2prio(rb);
67 	int pir = param & PPC_DBELL_PIR_MASK;
68 	int i;
69 	struct kvm_vcpu *cvcpu;
70 
71 	if (prio < 0)
72 		return EMULATE_FAIL;
73 
74 	kvm_for_each_vcpu(i, cvcpu, vcpu->kvm) {
75 		int cpir = cvcpu->arch.shared->pir;
76 		if ((param & PPC_DBELL_MSG_BRDCAST) || (cpir == pir)) {
77 			set_bit(prio, &cvcpu->arch.pending_exceptions);
78 			kvm_vcpu_kick(cvcpu);
79 		}
80 	}
81 
82 	return EMULATE_DONE;
83 }
84 #endif
85 
86 static int kvmppc_e500_emul_ehpriv(struct kvm_run *run, struct kvm_vcpu *vcpu,
87 				   unsigned int inst, int *advance)
88 {
89 	int emulated = EMULATE_DONE;
90 
91 	switch (get_oc(inst)) {
92 	case EHPRIV_OC_DEBUG:
93 		run->exit_reason = KVM_EXIT_DEBUG;
94 		run->debug.arch.address = vcpu->arch.pc;
95 		run->debug.arch.status = 0;
96 		kvmppc_account_exit(vcpu, DEBUG_EXITS);
97 		emulated = EMULATE_EXIT_USER;
98 		*advance = 0;
99 		break;
100 	default:
101 		emulated = EMULATE_FAIL;
102 	}
103 	return emulated;
104 }
105 
106 int kvmppc_core_emulate_op_e500(struct kvm_run *run, struct kvm_vcpu *vcpu,
107 				unsigned int inst, int *advance)
108 {
109 	int emulated = EMULATE_DONE;
110 	int ra = get_ra(inst);
111 	int rb = get_rb(inst);
112 	int rt = get_rt(inst);
113 	gva_t ea;
114 
115 	switch (get_op(inst)) {
116 	case 31:
117 		switch (get_xop(inst)) {
118 
119 #ifdef CONFIG_KVM_E500MC
120 		case XOP_MSGSND:
121 			emulated = kvmppc_e500_emul_msgsnd(vcpu, rb);
122 			break;
123 
124 		case XOP_MSGCLR:
125 			emulated = kvmppc_e500_emul_msgclr(vcpu, rb);
126 			break;
127 #endif
128 
129 		case XOP_TLBRE:
130 			emulated = kvmppc_e500_emul_tlbre(vcpu);
131 			break;
132 
133 		case XOP_TLBWE:
134 			emulated = kvmppc_e500_emul_tlbwe(vcpu);
135 			break;
136 
137 		case XOP_TLBSX:
138 			ea = kvmppc_get_ea_indexed(vcpu, ra, rb);
139 			emulated = kvmppc_e500_emul_tlbsx(vcpu, ea);
140 			break;
141 
142 		case XOP_TLBILX: {
143 			int type = rt & 0x3;
144 			ea = kvmppc_get_ea_indexed(vcpu, ra, rb);
145 			emulated = kvmppc_e500_emul_tlbilx(vcpu, type, ea);
146 			break;
147 		}
148 
149 		case XOP_TLBIVAX:
150 			ea = kvmppc_get_ea_indexed(vcpu, ra, rb);
151 			emulated = kvmppc_e500_emul_tlbivax(vcpu, ea);
152 			break;
153 
154 		case XOP_EHPRIV:
155 			emulated = kvmppc_e500_emul_ehpriv(run, vcpu, inst,
156 							   advance);
157 			break;
158 
159 		default:
160 			emulated = EMULATE_FAIL;
161 		}
162 
163 		break;
164 
165 	default:
166 		emulated = EMULATE_FAIL;
167 	}
168 
169 	if (emulated == EMULATE_FAIL)
170 		emulated = kvmppc_booke_emulate_op(run, vcpu, inst, advance);
171 
172 	return emulated;
173 }
174 
175 int kvmppc_core_emulate_mtspr_e500(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
176 {
177 	struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
178 	int emulated = EMULATE_DONE;
179 
180 	switch (sprn) {
181 #ifndef CONFIG_KVM_BOOKE_HV
182 	case SPRN_PID:
183 		kvmppc_set_pid(vcpu, spr_val);
184 		break;
185 	case SPRN_PID1:
186 		if (spr_val != 0)
187 			return EMULATE_FAIL;
188 		vcpu_e500->pid[1] = spr_val;
189 		break;
190 	case SPRN_PID2:
191 		if (spr_val != 0)
192 			return EMULATE_FAIL;
193 		vcpu_e500->pid[2] = spr_val;
194 		break;
195 	case SPRN_MAS0:
196 		vcpu->arch.shared->mas0 = spr_val;
197 		break;
198 	case SPRN_MAS1:
199 		vcpu->arch.shared->mas1 = spr_val;
200 		break;
201 	case SPRN_MAS2:
202 		vcpu->arch.shared->mas2 = spr_val;
203 		break;
204 	case SPRN_MAS3:
205 		vcpu->arch.shared->mas7_3 &= ~(u64)0xffffffff;
206 		vcpu->arch.shared->mas7_3 |= spr_val;
207 		break;
208 	case SPRN_MAS4:
209 		vcpu->arch.shared->mas4 = spr_val;
210 		break;
211 	case SPRN_MAS6:
212 		vcpu->arch.shared->mas6 = spr_val;
213 		break;
214 	case SPRN_MAS7:
215 		vcpu->arch.shared->mas7_3 &= (u64)0xffffffff;
216 		vcpu->arch.shared->mas7_3 |= (u64)spr_val << 32;
217 		break;
218 #endif
219 	case SPRN_L1CSR0:
220 		vcpu_e500->l1csr0 = spr_val;
221 		vcpu_e500->l1csr0 &= ~(L1CSR0_DCFI | L1CSR0_CLFC);
222 		break;
223 	case SPRN_L1CSR1:
224 		vcpu_e500->l1csr1 = spr_val;
225 		break;
226 	case SPRN_HID0:
227 		vcpu_e500->hid0 = spr_val;
228 		break;
229 	case SPRN_HID1:
230 		vcpu_e500->hid1 = spr_val;
231 		break;
232 
233 	case SPRN_MMUCSR0:
234 		emulated = kvmppc_e500_emul_mt_mmucsr0(vcpu_e500,
235 				spr_val);
236 		break;
237 
238 	/* extra exceptions */
239 	case SPRN_IVOR32:
240 		vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL] = spr_val;
241 		break;
242 	case SPRN_IVOR33:
243 		vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA] = spr_val;
244 		break;
245 	case SPRN_IVOR34:
246 		vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND] = spr_val;
247 		break;
248 	case SPRN_IVOR35:
249 		vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] = spr_val;
250 		break;
251 #ifdef CONFIG_KVM_BOOKE_HV
252 	case SPRN_IVOR36:
253 		vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL] = spr_val;
254 		break;
255 	case SPRN_IVOR37:
256 		vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT] = spr_val;
257 		break;
258 #endif
259 	default:
260 		emulated = kvmppc_booke_emulate_mtspr(vcpu, sprn, spr_val);
261 	}
262 
263 	return emulated;
264 }
265 
266 int kvmppc_core_emulate_mfspr_e500(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
267 {
268 	struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
269 	int emulated = EMULATE_DONE;
270 
271 	switch (sprn) {
272 #ifndef CONFIG_KVM_BOOKE_HV
273 	case SPRN_PID:
274 		*spr_val = vcpu_e500->pid[0];
275 		break;
276 	case SPRN_PID1:
277 		*spr_val = vcpu_e500->pid[1];
278 		break;
279 	case SPRN_PID2:
280 		*spr_val = vcpu_e500->pid[2];
281 		break;
282 	case SPRN_MAS0:
283 		*spr_val = vcpu->arch.shared->mas0;
284 		break;
285 	case SPRN_MAS1:
286 		*spr_val = vcpu->arch.shared->mas1;
287 		break;
288 	case SPRN_MAS2:
289 		*spr_val = vcpu->arch.shared->mas2;
290 		break;
291 	case SPRN_MAS3:
292 		*spr_val = (u32)vcpu->arch.shared->mas7_3;
293 		break;
294 	case SPRN_MAS4:
295 		*spr_val = vcpu->arch.shared->mas4;
296 		break;
297 	case SPRN_MAS6:
298 		*spr_val = vcpu->arch.shared->mas6;
299 		break;
300 	case SPRN_MAS7:
301 		*spr_val = vcpu->arch.shared->mas7_3 >> 32;
302 		break;
303 #endif
304 	case SPRN_DECAR:
305 		*spr_val = vcpu->arch.decar;
306 		break;
307 	case SPRN_TLB0CFG:
308 		*spr_val = vcpu->arch.tlbcfg[0];
309 		break;
310 	case SPRN_TLB1CFG:
311 		*spr_val = vcpu->arch.tlbcfg[1];
312 		break;
313 	case SPRN_TLB0PS:
314 		if (!has_feature(vcpu, VCPU_FTR_MMU_V2))
315 			return EMULATE_FAIL;
316 		*spr_val = vcpu->arch.tlbps[0];
317 		break;
318 	case SPRN_TLB1PS:
319 		if (!has_feature(vcpu, VCPU_FTR_MMU_V2))
320 			return EMULATE_FAIL;
321 		*spr_val = vcpu->arch.tlbps[1];
322 		break;
323 	case SPRN_L1CSR0:
324 		*spr_val = vcpu_e500->l1csr0;
325 		break;
326 	case SPRN_L1CSR1:
327 		*spr_val = vcpu_e500->l1csr1;
328 		break;
329 	case SPRN_HID0:
330 		*spr_val = vcpu_e500->hid0;
331 		break;
332 	case SPRN_HID1:
333 		*spr_val = vcpu_e500->hid1;
334 		break;
335 	case SPRN_SVR:
336 		*spr_val = vcpu_e500->svr;
337 		break;
338 
339 	case SPRN_MMUCSR0:
340 		*spr_val = 0;
341 		break;
342 
343 	case SPRN_MMUCFG:
344 		*spr_val = vcpu->arch.mmucfg;
345 		break;
346 	case SPRN_EPTCFG:
347 		if (!has_feature(vcpu, VCPU_FTR_MMU_V2))
348 			return EMULATE_FAIL;
349 		/*
350 		 * Legacy Linux guests access EPTCFG register even if the E.PT
351 		 * category is disabled in the VM. Give them a chance to live.
352 		 */
353 		*spr_val = vcpu->arch.eptcfg;
354 		break;
355 
356 	/* extra exceptions */
357 	case SPRN_IVOR32:
358 		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_UNAVAIL];
359 		break;
360 	case SPRN_IVOR33:
361 		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_DATA];
362 		break;
363 	case SPRN_IVOR34:
364 		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_SPE_FP_ROUND];
365 		break;
366 	case SPRN_IVOR35:
367 		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR];
368 		break;
369 #ifdef CONFIG_KVM_BOOKE_HV
370 	case SPRN_IVOR36:
371 		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL];
372 		break;
373 	case SPRN_IVOR37:
374 		*spr_val = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT];
375 		break;
376 #endif
377 	default:
378 		emulated = kvmppc_booke_emulate_mfspr(vcpu, sprn, spr_val);
379 	}
380 
381 	return emulated;
382 }
383 
384