2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 * Copyright SUSE Linux Products GmbH 2009
17 * Authors: Alexander Graf <agraf@suse.de>
20 #include <asm/kvm_ppc.h>
21 #include <asm/disassemble.h>
22 #include <asm/kvm_book3s.h>
25 #define OP_19_XOP_RFID 18
26 #define OP_19_XOP_RFI 50
28 #define OP_31_XOP_MFMSR 83
29 #define OP_31_XOP_MTMSR 146
30 #define OP_31_XOP_MTMSRD 178
31 #define OP_31_XOP_MTSR 210
32 #define OP_31_XOP_MTSRIN 242
33 #define OP_31_XOP_TLBIEL 274
34 #define OP_31_XOP_TLBIE 306
35 #define OP_31_XOP_SLBMTE 402
36 #define OP_31_XOP_SLBIE 434
37 #define OP_31_XOP_SLBIA 498
38 #define OP_31_XOP_MFSR 595
39 #define OP_31_XOP_MFSRIN 659
40 #define OP_31_XOP_SLBMFEV 851
41 #define OP_31_XOP_EIOIO 854
42 #define OP_31_XOP_SLBMFEE 915
44 /* DCBZ is actually 1014, but we patch it to 1010 so we get a trap */
45 #define OP_31_XOP_DCBZ 1010
61 int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
62 unsigned int inst, int *advance)
64 int emulated = EMULATE_DONE;
66 switch (get_op(inst)) {
68 switch (get_xop(inst)) {
71 vcpu->arch.pc = vcpu->arch.srr0;
72 kvmppc_set_msr(vcpu, vcpu->arch.srr1);
77 emulated = EMULATE_FAIL;
82 switch (get_xop(inst)) {
84 kvmppc_set_gpr(vcpu, get_rt(inst), vcpu->arch.msr);
86 case OP_31_XOP_MTMSRD:
88 ulong rs = kvmppc_get_gpr(vcpu, get_rs(inst));
90 vcpu->arch.msr &= ~(MSR_RI | MSR_EE);
91 vcpu->arch.msr |= rs & (MSR_RI | MSR_EE);
93 kvmppc_set_msr(vcpu, rs);
97 kvmppc_set_msr(vcpu, kvmppc_get_gpr(vcpu, get_rs(inst)));
103 srnum = kvmppc_get_field(inst, 12 + 32, 15 + 32);
104 if (vcpu->arch.mmu.mfsrin) {
106 sr = vcpu->arch.mmu.mfsrin(vcpu, srnum);
107 kvmppc_set_gpr(vcpu, get_rt(inst), sr);
111 case OP_31_XOP_MFSRIN:
115 srnum = (kvmppc_get_gpr(vcpu, get_rb(inst)) >> 28) & 0xf;
116 if (vcpu->arch.mmu.mfsrin) {
118 sr = vcpu->arch.mmu.mfsrin(vcpu, srnum);
119 kvmppc_set_gpr(vcpu, get_rt(inst), sr);
124 vcpu->arch.mmu.mtsrin(vcpu,
126 kvmppc_get_gpr(vcpu, get_rs(inst)));
128 case OP_31_XOP_MTSRIN:
129 vcpu->arch.mmu.mtsrin(vcpu,
130 (kvmppc_get_gpr(vcpu, get_rb(inst)) >> 28) & 0xf,
131 kvmppc_get_gpr(vcpu, get_rs(inst)));
133 case OP_31_XOP_TLBIE:
134 case OP_31_XOP_TLBIEL:
136 bool large = (inst & 0x00200000) ? true : false;
137 ulong addr = kvmppc_get_gpr(vcpu, get_rb(inst));
138 vcpu->arch.mmu.tlbie(vcpu, addr, large);
141 case OP_31_XOP_EIOIO:
143 case OP_31_XOP_SLBMTE:
144 if (!vcpu->arch.mmu.slbmte)
147 vcpu->arch.mmu.slbmte(vcpu,
148 kvmppc_get_gpr(vcpu, get_rs(inst)),
149 kvmppc_get_gpr(vcpu, get_rb(inst)));
151 case OP_31_XOP_SLBIE:
152 if (!vcpu->arch.mmu.slbie)
155 vcpu->arch.mmu.slbie(vcpu,
156 kvmppc_get_gpr(vcpu, get_rb(inst)));
158 case OP_31_XOP_SLBIA:
159 if (!vcpu->arch.mmu.slbia)
162 vcpu->arch.mmu.slbia(vcpu);
164 case OP_31_XOP_SLBMFEE:
165 if (!vcpu->arch.mmu.slbmfee) {
166 emulated = EMULATE_FAIL;
170 rb = kvmppc_get_gpr(vcpu, get_rb(inst));
171 t = vcpu->arch.mmu.slbmfee(vcpu, rb);
172 kvmppc_set_gpr(vcpu, get_rt(inst), t);
175 case OP_31_XOP_SLBMFEV:
176 if (!vcpu->arch.mmu.slbmfev) {
177 emulated = EMULATE_FAIL;
181 rb = kvmppc_get_gpr(vcpu, get_rb(inst));
182 t = vcpu->arch.mmu.slbmfev(vcpu, rb);
183 kvmppc_set_gpr(vcpu, get_rt(inst), t);
188 ulong rb = kvmppc_get_gpr(vcpu, get_rb(inst));
191 u32 zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
196 ra = kvmppc_get_gpr(vcpu, get_ra(inst));
198 addr = (ra + rb) & ~31ULL;
199 if (!(vcpu->arch.msr & MSR_SF))
203 r = kvmppc_st(vcpu, &addr, 32, zeros, true);
204 if ((r == -ENOENT) || (r == -EPERM)) {
206 vcpu->arch.dear = vaddr;
207 vcpu->arch.fault_dear = vaddr;
209 dsisr = DSISR_ISSTORE;
211 dsisr |= DSISR_NOHPTE;
212 else if (r == -EPERM)
213 dsisr |= DSISR_PROTFAULT;
215 to_book3s(vcpu)->dsisr = dsisr;
216 vcpu->arch.fault_dsisr = dsisr;
218 kvmppc_book3s_queue_irqprio(vcpu,
219 BOOK3S_INTERRUPT_DATA_STORAGE);
225 emulated = EMULATE_FAIL;
229 emulated = EMULATE_FAIL;
232 if (emulated == EMULATE_FAIL)
233 emulated = kvmppc_emulate_paired_single(run, vcpu);
238 void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat, bool upper,
243 u32 bl = (val >> 2) & 0x7ff;
244 bat->bepi_mask = (~bl << 17);
245 bat->bepi = val & 0xfffe0000;
246 bat->vs = (val & 2) ? 1 : 0;
247 bat->vp = (val & 1) ? 1 : 0;
248 bat->raw = (bat->raw & 0xffffffff00000000ULL) | val;
251 bat->brpn = val & 0xfffe0000;
252 bat->wimg = (val >> 3) & 0xf;
254 bat->raw = (bat->raw & 0x00000000ffffffffULL) | ((u64)val << 32);
258 static u32 kvmppc_read_bat(struct kvm_vcpu *vcpu, int sprn)
260 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
261 struct kvmppc_bat *bat;
264 case SPRN_IBAT0U ... SPRN_IBAT3L:
265 bat = &vcpu_book3s->ibat[(sprn - SPRN_IBAT0U) / 2];
267 case SPRN_IBAT4U ... SPRN_IBAT7L:
268 bat = &vcpu_book3s->ibat[4 + ((sprn - SPRN_IBAT4U) / 2)];
270 case SPRN_DBAT0U ... SPRN_DBAT3L:
271 bat = &vcpu_book3s->dbat[(sprn - SPRN_DBAT0U) / 2];
273 case SPRN_DBAT4U ... SPRN_DBAT7L:
274 bat = &vcpu_book3s->dbat[4 + ((sprn - SPRN_DBAT4U) / 2)];
281 return bat->raw >> 32;
286 static void kvmppc_write_bat(struct kvm_vcpu *vcpu, int sprn, u32 val)
288 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
289 struct kvmppc_bat *bat;
292 case SPRN_IBAT0U ... SPRN_IBAT3L:
293 bat = &vcpu_book3s->ibat[(sprn - SPRN_IBAT0U) / 2];
295 case SPRN_IBAT4U ... SPRN_IBAT7L:
296 bat = &vcpu_book3s->ibat[4 + ((sprn - SPRN_IBAT4U) / 2)];
298 case SPRN_DBAT0U ... SPRN_DBAT3L:
299 bat = &vcpu_book3s->dbat[(sprn - SPRN_DBAT0U) / 2];
301 case SPRN_DBAT4U ... SPRN_DBAT7L:
302 bat = &vcpu_book3s->dbat[4 + ((sprn - SPRN_DBAT4U) / 2)];
308 kvmppc_set_bat(vcpu, bat, !(sprn % 2), val);
311 int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
313 int emulated = EMULATE_DONE;
314 ulong spr_val = kvmppc_get_gpr(vcpu, rs);
318 to_book3s(vcpu)->sdr1 = spr_val;
321 to_book3s(vcpu)->dsisr = spr_val;
324 vcpu->arch.dear = spr_val;
327 to_book3s(vcpu)->hior = spr_val;
329 case SPRN_IBAT0U ... SPRN_IBAT3L:
330 case SPRN_IBAT4U ... SPRN_IBAT7L:
331 case SPRN_DBAT0U ... SPRN_DBAT3L:
332 case SPRN_DBAT4U ... SPRN_DBAT7L:
333 kvmppc_write_bat(vcpu, sprn, (u32)spr_val);
334 /* BAT writes happen so rarely that we're ok to flush
336 kvmppc_mmu_pte_flush(vcpu, 0, 0);
337 kvmppc_mmu_flush_segments(vcpu);
340 to_book3s(vcpu)->hid[0] = spr_val;
343 to_book3s(vcpu)->hid[1] = spr_val;
346 to_book3s(vcpu)->hid[2] = spr_val;
348 case SPRN_HID2_GEKKO:
349 to_book3s(vcpu)->hid[2] = spr_val;
350 /* HID2.PSE controls paired single on gekko */
351 switch (vcpu->arch.pvr) {
352 case 0x00080200: /* lonestar 2.0 */
353 case 0x00088202: /* lonestar 2.2 */
354 case 0x70000100: /* gekko 1.0 */
355 case 0x00080100: /* gekko 2.0 */
356 case 0x00083203: /* gekko 2.3a */
357 case 0x00083213: /* gekko 2.3b */
358 case 0x00083204: /* gekko 2.4 */
359 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
360 if (spr_val & (1 << 29)) { /* HID2.PSE */
361 vcpu->arch.hflags |= BOOK3S_HFLAG_PAIRED_SINGLE;
362 kvmppc_giveup_ext(vcpu, MSR_FP);
364 vcpu->arch.hflags &= ~BOOK3S_HFLAG_PAIRED_SINGLE;
370 case SPRN_HID4_GEKKO:
371 to_book3s(vcpu)->hid[4] = spr_val;
374 to_book3s(vcpu)->hid[5] = spr_val;
375 /* guest HID5 set can change is_dcbz32 */
376 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
378 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
388 to_book3s(vcpu)->gqr[sprn - SPRN_GQR0] = spr_val;
397 case SPRN_MMCR0_GEKKO:
398 case SPRN_MMCR1_GEKKO:
399 case SPRN_PMC1_GEKKO:
400 case SPRN_PMC2_GEKKO:
401 case SPRN_PMC3_GEKKO:
402 case SPRN_PMC4_GEKKO:
403 case SPRN_WPAR_GEKKO:
406 printk(KERN_INFO "KVM: invalid SPR write: %d\n", sprn);
408 emulated = EMULATE_FAIL;
416 int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
418 int emulated = EMULATE_DONE;
421 case SPRN_IBAT0U ... SPRN_IBAT3L:
422 case SPRN_IBAT4U ... SPRN_IBAT7L:
423 case SPRN_DBAT0U ... SPRN_DBAT3L:
424 case SPRN_DBAT4U ... SPRN_DBAT7L:
425 kvmppc_set_gpr(vcpu, rt, kvmppc_read_bat(vcpu, sprn));
428 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->sdr1);
431 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->dsisr);
434 kvmppc_set_gpr(vcpu, rt, vcpu->arch.dear);
437 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hior);
440 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[0]);
443 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[1]);
446 case SPRN_HID2_GEKKO:
447 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[2]);
450 case SPRN_HID4_GEKKO:
451 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[4]);
454 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[5]);
464 kvmppc_set_gpr(vcpu, rt,
465 to_book3s(vcpu)->gqr[sprn - SPRN_GQR0]);
473 case SPRN_MMCR0_GEKKO:
474 case SPRN_MMCR1_GEKKO:
475 case SPRN_PMC1_GEKKO:
476 case SPRN_PMC2_GEKKO:
477 case SPRN_PMC3_GEKKO:
478 case SPRN_PMC4_GEKKO:
479 case SPRN_WPAR_GEKKO:
480 kvmppc_set_gpr(vcpu, rt, 0);
483 printk(KERN_INFO "KVM: invalid SPR read: %d\n", sprn);
485 emulated = EMULATE_FAIL;
493 u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst)
498 * This is what the spec says about DSISR bits (not mentioned = 0):
500 * 12:13 [DS] Set to bits 30:31
501 * 15:16 [X] Set to bits 29:30
502 * 17 [X] Set to bit 25
503 * [D/DS] Set to bit 5
504 * 18:21 [X] Set to bits 21:24
505 * [D/DS] Set to bits 1:4
506 * 22:26 Set to bits 6:10 (RT/RS/FRT/FRS)
507 * 27:31 Set to bits 11:15 (RA)
510 switch (get_op(inst)) {
516 dsisr |= (inst >> 12) & 0x4000; /* bit 17 */
517 dsisr |= (inst >> 17) & 0x3c00; /* bits 18:21 */
521 dsisr |= (inst << 14) & 0x18000; /* bits 15:16 */
522 dsisr |= (inst << 8) & 0x04000; /* bit 17 */
523 dsisr |= (inst << 3) & 0x03c00; /* bits 18:21 */
526 printk(KERN_INFO "KVM: Unaligned instruction 0x%x\n", inst);
530 dsisr |= (inst >> 16) & 0x03ff; /* bits 22:31 */
535 ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst)
540 switch (get_op(inst)) {
547 dar = kvmppc_get_gpr(vcpu, ra);
548 dar += (s32)((s16)inst);
553 dar = kvmppc_get_gpr(vcpu, ra);
554 dar += kvmppc_get_gpr(vcpu, get_rb(inst));
557 printk(KERN_INFO "KVM: Unaligned instruction 0x%x\n", inst);