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
56 int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
57 unsigned int inst, int *advance)
59 int emulated = EMULATE_DONE;
61 switch (get_op(inst)) {
63 switch (get_xop(inst)) {
66 vcpu->arch.pc = vcpu->arch.srr0;
67 kvmppc_set_msr(vcpu, vcpu->arch.srr1);
72 emulated = EMULATE_FAIL;
77 switch (get_xop(inst)) {
79 kvmppc_set_gpr(vcpu, get_rt(inst), vcpu->arch.msr);
81 case OP_31_XOP_MTMSRD:
83 ulong rs = kvmppc_get_gpr(vcpu, get_rs(inst));
85 vcpu->arch.msr &= ~(MSR_RI | MSR_EE);
86 vcpu->arch.msr |= rs & (MSR_RI | MSR_EE);
88 kvmppc_set_msr(vcpu, rs);
92 kvmppc_set_msr(vcpu, kvmppc_get_gpr(vcpu, get_rs(inst)));
98 srnum = kvmppc_get_field(inst, 12 + 32, 15 + 32);
99 if (vcpu->arch.mmu.mfsrin) {
101 sr = vcpu->arch.mmu.mfsrin(vcpu, srnum);
102 kvmppc_set_gpr(vcpu, get_rt(inst), sr);
106 case OP_31_XOP_MFSRIN:
110 srnum = (kvmppc_get_gpr(vcpu, get_rb(inst)) >> 28) & 0xf;
111 if (vcpu->arch.mmu.mfsrin) {
113 sr = vcpu->arch.mmu.mfsrin(vcpu, srnum);
114 kvmppc_set_gpr(vcpu, get_rt(inst), sr);
119 vcpu->arch.mmu.mtsrin(vcpu,
121 kvmppc_get_gpr(vcpu, get_rs(inst)));
123 case OP_31_XOP_MTSRIN:
124 vcpu->arch.mmu.mtsrin(vcpu,
125 (kvmppc_get_gpr(vcpu, get_rb(inst)) >> 28) & 0xf,
126 kvmppc_get_gpr(vcpu, get_rs(inst)));
128 case OP_31_XOP_TLBIE:
129 case OP_31_XOP_TLBIEL:
131 bool large = (inst & 0x00200000) ? true : false;
132 ulong addr = kvmppc_get_gpr(vcpu, get_rb(inst));
133 vcpu->arch.mmu.tlbie(vcpu, addr, large);
136 case OP_31_XOP_EIOIO:
138 case OP_31_XOP_SLBMTE:
139 if (!vcpu->arch.mmu.slbmte)
142 vcpu->arch.mmu.slbmte(vcpu,
143 kvmppc_get_gpr(vcpu, get_rs(inst)),
144 kvmppc_get_gpr(vcpu, get_rb(inst)));
146 case OP_31_XOP_SLBIE:
147 if (!vcpu->arch.mmu.slbie)
150 vcpu->arch.mmu.slbie(vcpu,
151 kvmppc_get_gpr(vcpu, get_rb(inst)));
153 case OP_31_XOP_SLBIA:
154 if (!vcpu->arch.mmu.slbia)
157 vcpu->arch.mmu.slbia(vcpu);
159 case OP_31_XOP_SLBMFEE:
160 if (!vcpu->arch.mmu.slbmfee) {
161 emulated = EMULATE_FAIL;
165 rb = kvmppc_get_gpr(vcpu, get_rb(inst));
166 t = vcpu->arch.mmu.slbmfee(vcpu, rb);
167 kvmppc_set_gpr(vcpu, get_rt(inst), t);
170 case OP_31_XOP_SLBMFEV:
171 if (!vcpu->arch.mmu.slbmfev) {
172 emulated = EMULATE_FAIL;
176 rb = kvmppc_get_gpr(vcpu, get_rb(inst));
177 t = vcpu->arch.mmu.slbmfev(vcpu, rb);
178 kvmppc_set_gpr(vcpu, get_rt(inst), t);
183 ulong rb = kvmppc_get_gpr(vcpu, get_rb(inst));
186 u32 zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
189 ra = kvmppc_get_gpr(vcpu, get_ra(inst));
191 addr = (ra + rb) & ~31ULL;
192 if (!(vcpu->arch.msr & MSR_SF))
196 if (kvmppc_st(vcpu, &addr, 32, zeros, true)) {
197 vcpu->arch.dear = vaddr;
198 vcpu->arch.fault_dear = vaddr;
199 to_book3s(vcpu)->dsisr = DSISR_PROTFAULT |
201 kvmppc_book3s_queue_irqprio(vcpu,
202 BOOK3S_INTERRUPT_DATA_STORAGE);
203 kvmppc_mmu_pte_flush(vcpu, vaddr, ~0xFFFULL);
209 emulated = EMULATE_FAIL;
213 emulated = EMULATE_FAIL;
216 if (emulated == EMULATE_FAIL)
217 emulated = kvmppc_emulate_paired_single(run, vcpu);
222 void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat, bool upper,
227 u32 bl = (val >> 2) & 0x7ff;
228 bat->bepi_mask = (~bl << 17);
229 bat->bepi = val & 0xfffe0000;
230 bat->vs = (val & 2) ? 1 : 0;
231 bat->vp = (val & 1) ? 1 : 0;
232 bat->raw = (bat->raw & 0xffffffff00000000ULL) | val;
235 bat->brpn = val & 0xfffe0000;
236 bat->wimg = (val >> 3) & 0xf;
238 bat->raw = (bat->raw & 0x00000000ffffffffULL) | ((u64)val << 32);
242 static u32 kvmppc_read_bat(struct kvm_vcpu *vcpu, int sprn)
244 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
245 struct kvmppc_bat *bat;
248 case SPRN_IBAT0U ... SPRN_IBAT3L:
249 bat = &vcpu_book3s->ibat[(sprn - SPRN_IBAT0U) / 2];
251 case SPRN_IBAT4U ... SPRN_IBAT7L:
252 bat = &vcpu_book3s->ibat[4 + ((sprn - SPRN_IBAT4U) / 2)];
254 case SPRN_DBAT0U ... SPRN_DBAT3L:
255 bat = &vcpu_book3s->dbat[(sprn - SPRN_DBAT0U) / 2];
257 case SPRN_DBAT4U ... SPRN_DBAT7L:
258 bat = &vcpu_book3s->dbat[4 + ((sprn - SPRN_DBAT4U) / 2)];
265 return bat->raw >> 32;
270 static void kvmppc_write_bat(struct kvm_vcpu *vcpu, int sprn, u32 val)
272 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
273 struct kvmppc_bat *bat;
276 case SPRN_IBAT0U ... SPRN_IBAT3L:
277 bat = &vcpu_book3s->ibat[(sprn - SPRN_IBAT0U) / 2];
279 case SPRN_IBAT4U ... SPRN_IBAT7L:
280 bat = &vcpu_book3s->ibat[4 + ((sprn - SPRN_IBAT4U) / 2)];
282 case SPRN_DBAT0U ... SPRN_DBAT3L:
283 bat = &vcpu_book3s->dbat[(sprn - SPRN_DBAT0U) / 2];
285 case SPRN_DBAT4U ... SPRN_DBAT7L:
286 bat = &vcpu_book3s->dbat[4 + ((sprn - SPRN_DBAT4U) / 2)];
292 kvmppc_set_bat(vcpu, bat, !(sprn % 2), val);
295 int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
297 int emulated = EMULATE_DONE;
298 ulong spr_val = kvmppc_get_gpr(vcpu, rs);
302 to_book3s(vcpu)->sdr1 = spr_val;
305 to_book3s(vcpu)->dsisr = spr_val;
308 vcpu->arch.dear = spr_val;
311 to_book3s(vcpu)->hior = spr_val;
313 case SPRN_IBAT0U ... SPRN_IBAT3L:
314 case SPRN_IBAT4U ... SPRN_IBAT7L:
315 case SPRN_DBAT0U ... SPRN_DBAT3L:
316 case SPRN_DBAT4U ... SPRN_DBAT7L:
317 kvmppc_write_bat(vcpu, sprn, (u32)spr_val);
318 /* BAT writes happen so rarely that we're ok to flush
320 kvmppc_mmu_pte_flush(vcpu, 0, 0);
321 kvmppc_mmu_flush_segments(vcpu);
324 to_book3s(vcpu)->hid[0] = spr_val;
327 to_book3s(vcpu)->hid[1] = spr_val;
330 to_book3s(vcpu)->hid[2] = spr_val;
332 case SPRN_HID2_GEKKO:
333 to_book3s(vcpu)->hid[2] = spr_val;
334 /* HID2.PSE controls paired single on gekko */
335 switch (vcpu->arch.pvr) {
336 case 0x00080200: /* lonestar 2.0 */
337 case 0x00088202: /* lonestar 2.2 */
338 case 0x70000100: /* gekko 1.0 */
339 case 0x00080100: /* gekko 2.0 */
340 case 0x00083203: /* gekko 2.3a */
341 case 0x00083213: /* gekko 2.3b */
342 case 0x00083204: /* gekko 2.4 */
343 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
344 if (spr_val & (1 << 29)) { /* HID2.PSE */
345 vcpu->arch.hflags |= BOOK3S_HFLAG_PAIRED_SINGLE;
346 kvmppc_giveup_ext(vcpu, MSR_FP);
348 vcpu->arch.hflags &= ~BOOK3S_HFLAG_PAIRED_SINGLE;
354 case SPRN_HID4_GEKKO:
355 to_book3s(vcpu)->hid[4] = spr_val;
358 to_book3s(vcpu)->hid[5] = spr_val;
359 /* guest HID5 set can change is_dcbz32 */
360 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
362 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
372 to_book3s(vcpu)->gqr[sprn - SPRN_GQR0] = spr_val;
381 case SPRN_MMCR0_GEKKO:
382 case SPRN_MMCR1_GEKKO:
383 case SPRN_PMC1_GEKKO:
384 case SPRN_PMC2_GEKKO:
385 case SPRN_PMC3_GEKKO:
386 case SPRN_PMC4_GEKKO:
387 case SPRN_WPAR_GEKKO:
390 printk(KERN_INFO "KVM: invalid SPR write: %d\n", sprn);
392 emulated = EMULATE_FAIL;
400 int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
402 int emulated = EMULATE_DONE;
405 case SPRN_IBAT0U ... SPRN_IBAT3L:
406 case SPRN_IBAT4U ... SPRN_IBAT7L:
407 case SPRN_DBAT0U ... SPRN_DBAT3L:
408 case SPRN_DBAT4U ... SPRN_DBAT7L:
409 kvmppc_set_gpr(vcpu, rt, kvmppc_read_bat(vcpu, sprn));
412 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->sdr1);
415 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->dsisr);
418 kvmppc_set_gpr(vcpu, rt, vcpu->arch.dear);
421 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hior);
424 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[0]);
427 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[1]);
430 case SPRN_HID2_GEKKO:
431 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[2]);
434 case SPRN_HID4_GEKKO:
435 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[4]);
438 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[5]);
448 kvmppc_set_gpr(vcpu, rt,
449 to_book3s(vcpu)->gqr[sprn - SPRN_GQR0]);
457 case SPRN_MMCR0_GEKKO:
458 case SPRN_MMCR1_GEKKO:
459 case SPRN_PMC1_GEKKO:
460 case SPRN_PMC2_GEKKO:
461 case SPRN_PMC3_GEKKO:
462 case SPRN_PMC4_GEKKO:
463 case SPRN_WPAR_GEKKO:
464 kvmppc_set_gpr(vcpu, rt, 0);
467 printk(KERN_INFO "KVM: invalid SPR read: %d\n", sprn);
469 emulated = EMULATE_FAIL;