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_MFSRIN 659
39 #define OP_31_XOP_SLBMFEV 851
40 #define OP_31_XOP_EIOIO 854
41 #define OP_31_XOP_SLBMFEE 915
43 /* DCBZ is actually 1014, but we patch it to 1010 so we get a trap */
44 #define OP_31_XOP_DCBZ 1010
55 int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
56 unsigned int inst, int *advance)
58 int emulated = EMULATE_DONE;
60 switch (get_op(inst)) {
62 switch (get_xop(inst)) {
65 vcpu->arch.pc = vcpu->arch.srr0;
66 kvmppc_set_msr(vcpu, vcpu->arch.srr1);
71 emulated = EMULATE_FAIL;
76 switch (get_xop(inst)) {
78 kvmppc_set_gpr(vcpu, get_rt(inst), vcpu->arch.msr);
80 case OP_31_XOP_MTMSRD:
82 ulong rs = kvmppc_get_gpr(vcpu, get_rs(inst));
84 vcpu->arch.msr &= ~(MSR_RI | MSR_EE);
85 vcpu->arch.msr |= rs & (MSR_RI | MSR_EE);
87 kvmppc_set_msr(vcpu, rs);
91 kvmppc_set_msr(vcpu, kvmppc_get_gpr(vcpu, get_rs(inst)));
93 case OP_31_XOP_MFSRIN:
97 srnum = (kvmppc_get_gpr(vcpu, get_rb(inst)) >> 28) & 0xf;
98 if (vcpu->arch.mmu.mfsrin) {
100 sr = vcpu->arch.mmu.mfsrin(vcpu, srnum);
101 kvmppc_set_gpr(vcpu, get_rt(inst), sr);
106 vcpu->arch.mmu.mtsrin(vcpu,
108 kvmppc_get_gpr(vcpu, get_rs(inst)));
110 case OP_31_XOP_MTSRIN:
111 vcpu->arch.mmu.mtsrin(vcpu,
112 (kvmppc_get_gpr(vcpu, get_rb(inst)) >> 28) & 0xf,
113 kvmppc_get_gpr(vcpu, get_rs(inst)));
115 case OP_31_XOP_TLBIE:
116 case OP_31_XOP_TLBIEL:
118 bool large = (inst & 0x00200000) ? true : false;
119 ulong addr = kvmppc_get_gpr(vcpu, get_rb(inst));
120 vcpu->arch.mmu.tlbie(vcpu, addr, large);
123 case OP_31_XOP_EIOIO:
125 case OP_31_XOP_SLBMTE:
126 if (!vcpu->arch.mmu.slbmte)
129 vcpu->arch.mmu.slbmte(vcpu,
130 kvmppc_get_gpr(vcpu, get_rs(inst)),
131 kvmppc_get_gpr(vcpu, get_rb(inst)));
133 case OP_31_XOP_SLBIE:
134 if (!vcpu->arch.mmu.slbie)
137 vcpu->arch.mmu.slbie(vcpu,
138 kvmppc_get_gpr(vcpu, get_rb(inst)));
140 case OP_31_XOP_SLBIA:
141 if (!vcpu->arch.mmu.slbia)
144 vcpu->arch.mmu.slbia(vcpu);
146 case OP_31_XOP_SLBMFEE:
147 if (!vcpu->arch.mmu.slbmfee) {
148 emulated = EMULATE_FAIL;
152 rb = kvmppc_get_gpr(vcpu, get_rb(inst));
153 t = vcpu->arch.mmu.slbmfee(vcpu, rb);
154 kvmppc_set_gpr(vcpu, get_rt(inst), t);
157 case OP_31_XOP_SLBMFEV:
158 if (!vcpu->arch.mmu.slbmfev) {
159 emulated = EMULATE_FAIL;
163 rb = kvmppc_get_gpr(vcpu, get_rb(inst));
164 t = vcpu->arch.mmu.slbmfev(vcpu, rb);
165 kvmppc_set_gpr(vcpu, get_rt(inst), t);
170 ulong rb = kvmppc_get_gpr(vcpu, get_rb(inst));
173 u32 zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
176 ra = kvmppc_get_gpr(vcpu, get_ra(inst));
178 addr = (ra + rb) & ~31ULL;
179 if (!(vcpu->arch.msr & MSR_SF))
183 if (kvmppc_st(vcpu, &addr, 32, zeros, true)) {
184 vcpu->arch.dear = vaddr;
185 vcpu->arch.fault_dear = vaddr;
186 to_book3s(vcpu)->dsisr = DSISR_PROTFAULT |
188 kvmppc_book3s_queue_irqprio(vcpu,
189 BOOK3S_INTERRUPT_DATA_STORAGE);
190 kvmppc_mmu_pte_flush(vcpu, vaddr, ~0xFFFULL);
196 emulated = EMULATE_FAIL;
200 emulated = EMULATE_FAIL;
203 if (emulated == EMULATE_FAIL)
204 emulated = kvmppc_emulate_paired_single(run, vcpu);
209 void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat, bool upper,
214 u32 bl = (val >> 2) & 0x7ff;
215 bat->bepi_mask = (~bl << 17);
216 bat->bepi = val & 0xfffe0000;
217 bat->vs = (val & 2) ? 1 : 0;
218 bat->vp = (val & 1) ? 1 : 0;
219 bat->raw = (bat->raw & 0xffffffff00000000ULL) | val;
222 bat->brpn = val & 0xfffe0000;
223 bat->wimg = (val >> 3) & 0xf;
225 bat->raw = (bat->raw & 0x00000000ffffffffULL) | ((u64)val << 32);
229 static void kvmppc_write_bat(struct kvm_vcpu *vcpu, int sprn, u32 val)
231 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
232 struct kvmppc_bat *bat;
235 case SPRN_IBAT0U ... SPRN_IBAT3L:
236 bat = &vcpu_book3s->ibat[(sprn - SPRN_IBAT0U) / 2];
238 case SPRN_IBAT4U ... SPRN_IBAT7L:
239 bat = &vcpu_book3s->ibat[4 + ((sprn - SPRN_IBAT4U) / 2)];
241 case SPRN_DBAT0U ... SPRN_DBAT3L:
242 bat = &vcpu_book3s->dbat[(sprn - SPRN_DBAT0U) / 2];
244 case SPRN_DBAT4U ... SPRN_DBAT7L:
245 bat = &vcpu_book3s->dbat[4 + ((sprn - SPRN_DBAT4U) / 2)];
251 kvmppc_set_bat(vcpu, bat, !(sprn % 2), val);
254 int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
256 int emulated = EMULATE_DONE;
257 ulong spr_val = kvmppc_get_gpr(vcpu, rs);
261 to_book3s(vcpu)->sdr1 = spr_val;
264 to_book3s(vcpu)->dsisr = spr_val;
267 vcpu->arch.dear = spr_val;
270 to_book3s(vcpu)->hior = spr_val;
272 case SPRN_IBAT0U ... SPRN_IBAT3L:
273 case SPRN_IBAT4U ... SPRN_IBAT7L:
274 case SPRN_DBAT0U ... SPRN_DBAT3L:
275 case SPRN_DBAT4U ... SPRN_DBAT7L:
276 kvmppc_write_bat(vcpu, sprn, (u32)spr_val);
277 /* BAT writes happen so rarely that we're ok to flush
279 kvmppc_mmu_pte_flush(vcpu, 0, 0);
282 to_book3s(vcpu)->hid[0] = spr_val;
285 to_book3s(vcpu)->hid[1] = spr_val;
288 to_book3s(vcpu)->hid[2] = spr_val;
290 case SPRN_HID2_GEKKO:
291 to_book3s(vcpu)->hid[2] = spr_val;
292 /* HID2.PSE controls paired single on gekko */
293 switch (vcpu->arch.pvr) {
294 case 0x00080200: /* lonestar 2.0 */
295 case 0x00088202: /* lonestar 2.2 */
296 case 0x70000100: /* gekko 1.0 */
297 case 0x00080100: /* gekko 2.0 */
298 case 0x00083203: /* gekko 2.3a */
299 case 0x00083213: /* gekko 2.3b */
300 case 0x00083204: /* gekko 2.4 */
301 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
302 if (spr_val & (1 << 29)) { /* HID2.PSE */
303 vcpu->arch.hflags |= BOOK3S_HFLAG_PAIRED_SINGLE;
304 kvmppc_giveup_ext(vcpu, MSR_FP);
306 vcpu->arch.hflags &= ~BOOK3S_HFLAG_PAIRED_SINGLE;
312 case SPRN_HID4_GEKKO:
313 to_book3s(vcpu)->hid[4] = spr_val;
316 to_book3s(vcpu)->hid[5] = spr_val;
317 /* guest HID5 set can change is_dcbz32 */
318 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
320 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
330 to_book3s(vcpu)->gqr[sprn - SPRN_GQR0] = spr_val;
339 case SPRN_MMCR0_GEKKO:
340 case SPRN_MMCR1_GEKKO:
341 case SPRN_PMC1_GEKKO:
342 case SPRN_PMC2_GEKKO:
343 case SPRN_PMC3_GEKKO:
344 case SPRN_PMC4_GEKKO:
345 case SPRN_WPAR_GEKKO:
348 printk(KERN_INFO "KVM: invalid SPR write: %d\n", sprn);
350 emulated = EMULATE_FAIL;
358 int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
360 int emulated = EMULATE_DONE;
364 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->sdr1);
367 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->dsisr);
370 kvmppc_set_gpr(vcpu, rt, vcpu->arch.dear);
373 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hior);
376 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[0]);
379 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[1]);
382 case SPRN_HID2_GEKKO:
383 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[2]);
386 case SPRN_HID4_GEKKO:
387 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[4]);
390 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[5]);
400 kvmppc_set_gpr(vcpu, rt,
401 to_book3s(vcpu)->gqr[sprn - SPRN_GQR0]);
409 case SPRN_MMCR0_GEKKO:
410 case SPRN_MMCR1_GEKKO:
411 case SPRN_PMC1_GEKKO:
412 case SPRN_PMC2_GEKKO:
413 case SPRN_PMC3_GEKKO:
414 case SPRN_PMC4_GEKKO:
415 case SPRN_WPAR_GEKKO:
416 kvmppc_set_gpr(vcpu, rt, 0);
419 printk(KERN_INFO "KVM: invalid SPR read: %d\n", sprn);
421 emulated = EMULATE_FAIL;