blob: bf0ddcd98bcc7d618cf800e147f13f715921b4ec [file] [log] [blame]
Alexander Grafc215c6e2009-10-30 05:47:14 +00001/*
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.
5 *
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.
10 *
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.
14 *
15 * Copyright SUSE Linux Products GmbH 2009
16 *
17 * Authors: Alexander Graf <agraf@suse.de>
18 */
19
20#include <asm/kvm_ppc.h>
21#include <asm/disassemble.h>
22#include <asm/kvm_book3s.h>
23#include <asm/reg.h>
24
25#define OP_19_XOP_RFID 18
26#define OP_19_XOP_RFI 50
27
28#define OP_31_XOP_MFMSR 83
29#define OP_31_XOP_MTMSR 146
30#define OP_31_XOP_MTMSRD 178
Alexander Graf71db4082010-02-19 11:00:37 +010031#define OP_31_XOP_MTSR 210
Alexander Grafc215c6e2009-10-30 05:47:14 +000032#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
Alexander Grafc6648762010-03-24 21:48:24 +010038#define OP_31_XOP_MFSR 595
Alexander Grafc215c6e2009-10-30 05:47:14 +000039#define OP_31_XOP_MFSRIN 659
Alexander Grafbd7cdbb2010-03-24 21:48:33 +010040#define OP_31_XOP_DCBA 758
Alexander Grafc215c6e2009-10-30 05:47:14 +000041#define OP_31_XOP_SLBMFEV 851
42#define OP_31_XOP_EIOIO 854
43#define OP_31_XOP_SLBMFEE 915
44
45/* DCBZ is actually 1014, but we patch it to 1010 so we get a trap */
46#define OP_31_XOP_DCBZ 1010
47
Alexander Grafca7f4202010-03-24 21:48:28 +010048#define OP_LFS 48
49#define OP_LFD 50
50#define OP_STFS 52
51#define OP_STFD 54
52
Alexander Grafd6d549b2010-02-19 11:00:33 +010053#define SPRN_GQR0 912
54#define SPRN_GQR1 913
55#define SPRN_GQR2 914
56#define SPRN_GQR3 915
57#define SPRN_GQR4 916
58#define SPRN_GQR5 917
59#define SPRN_GQR6 918
60#define SPRN_GQR7 919
61
Alexander Graf07b09072010-04-16 00:11:53 +020062/* Book3S_32 defines mfsrin(v) - but that messes up our abstract
63 * function pointers, so let's just disable the define. */
64#undef mfsrin
65
Alexander Graf317a8fa2011-08-08 16:07:16 +020066enum priv_level {
67 PRIV_PROBLEM = 0,
68 PRIV_SUPER = 1,
69 PRIV_HYPER = 2,
70};
71
72static bool spr_allowed(struct kvm_vcpu *vcpu, enum priv_level level)
73{
74 /* PAPR VMs only access supervisor SPRs */
75 if (vcpu->arch.papr_enabled && (level > PRIV_SUPER))
76 return false;
77
78 /* Limit user space to its own small SPR set */
79 if ((vcpu->arch.shared->msr & MSR_PR) && level > PRIV_PROBLEM)
80 return false;
81
82 return true;
83}
84
Alexander Grafc215c6e2009-10-30 05:47:14 +000085int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
86 unsigned int inst, int *advance)
87{
88 int emulated = EMULATE_DONE;
89
90 switch (get_op(inst)) {
91 case 19:
92 switch (get_xop(inst)) {
93 case OP_19_XOP_RFID:
94 case OP_19_XOP_RFI:
Alexander Grafde7906c2010-07-29 14:47:46 +020095 kvmppc_set_pc(vcpu, vcpu->arch.shared->srr0);
96 kvmppc_set_msr(vcpu, vcpu->arch.shared->srr1);
Alexander Grafc215c6e2009-10-30 05:47:14 +000097 *advance = 0;
98 break;
99
100 default:
101 emulated = EMULATE_FAIL;
102 break;
103 }
104 break;
105 case 31:
106 switch (get_xop(inst)) {
107 case OP_31_XOP_MFMSR:
Alexander Graf666e7252010-07-29 14:47:43 +0200108 kvmppc_set_gpr(vcpu, get_rt(inst),
109 vcpu->arch.shared->msr);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000110 break;
111 case OP_31_XOP_MTMSRD:
112 {
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100113 ulong rs = kvmppc_get_gpr(vcpu, get_rs(inst));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000114 if (inst & 0x10000) {
Alexander Graf666e7252010-07-29 14:47:43 +0200115 vcpu->arch.shared->msr &= ~(MSR_RI | MSR_EE);
116 vcpu->arch.shared->msr |= rs & (MSR_RI | MSR_EE);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000117 } else
118 kvmppc_set_msr(vcpu, rs);
119 break;
120 }
121 case OP_31_XOP_MTMSR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100122 kvmppc_set_msr(vcpu, kvmppc_get_gpr(vcpu, get_rs(inst)));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000123 break;
Alexander Grafc6648762010-03-24 21:48:24 +0100124 case OP_31_XOP_MFSR:
125 {
126 int srnum;
127
128 srnum = kvmppc_get_field(inst, 12 + 32, 15 + 32);
129 if (vcpu->arch.mmu.mfsrin) {
130 u32 sr;
131 sr = vcpu->arch.mmu.mfsrin(vcpu, srnum);
132 kvmppc_set_gpr(vcpu, get_rt(inst), sr);
133 }
134 break;
135 }
Alexander Grafc215c6e2009-10-30 05:47:14 +0000136 case OP_31_XOP_MFSRIN:
137 {
138 int srnum;
139
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100140 srnum = (kvmppc_get_gpr(vcpu, get_rb(inst)) >> 28) & 0xf;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000141 if (vcpu->arch.mmu.mfsrin) {
142 u32 sr;
143 sr = vcpu->arch.mmu.mfsrin(vcpu, srnum);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100144 kvmppc_set_gpr(vcpu, get_rt(inst), sr);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000145 }
146 break;
147 }
Alexander Graf71db4082010-02-19 11:00:37 +0100148 case OP_31_XOP_MTSR:
149 vcpu->arch.mmu.mtsrin(vcpu,
150 (inst >> 16) & 0xf,
151 kvmppc_get_gpr(vcpu, get_rs(inst)));
152 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000153 case OP_31_XOP_MTSRIN:
154 vcpu->arch.mmu.mtsrin(vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100155 (kvmppc_get_gpr(vcpu, get_rb(inst)) >> 28) & 0xf,
156 kvmppc_get_gpr(vcpu, get_rs(inst)));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000157 break;
158 case OP_31_XOP_TLBIE:
159 case OP_31_XOP_TLBIEL:
160 {
161 bool large = (inst & 0x00200000) ? true : false;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100162 ulong addr = kvmppc_get_gpr(vcpu, get_rb(inst));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000163 vcpu->arch.mmu.tlbie(vcpu, addr, large);
164 break;
165 }
166 case OP_31_XOP_EIOIO:
167 break;
168 case OP_31_XOP_SLBMTE:
169 if (!vcpu->arch.mmu.slbmte)
170 return EMULATE_FAIL;
171
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100172 vcpu->arch.mmu.slbmte(vcpu,
173 kvmppc_get_gpr(vcpu, get_rs(inst)),
174 kvmppc_get_gpr(vcpu, get_rb(inst)));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000175 break;
176 case OP_31_XOP_SLBIE:
177 if (!vcpu->arch.mmu.slbie)
178 return EMULATE_FAIL;
179
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100180 vcpu->arch.mmu.slbie(vcpu,
181 kvmppc_get_gpr(vcpu, get_rb(inst)));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000182 break;
183 case OP_31_XOP_SLBIA:
184 if (!vcpu->arch.mmu.slbia)
185 return EMULATE_FAIL;
186
187 vcpu->arch.mmu.slbia(vcpu);
188 break;
189 case OP_31_XOP_SLBMFEE:
190 if (!vcpu->arch.mmu.slbmfee) {
191 emulated = EMULATE_FAIL;
192 } else {
193 ulong t, rb;
194
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100195 rb = kvmppc_get_gpr(vcpu, get_rb(inst));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000196 t = vcpu->arch.mmu.slbmfee(vcpu, rb);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100197 kvmppc_set_gpr(vcpu, get_rt(inst), t);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000198 }
199 break;
200 case OP_31_XOP_SLBMFEV:
201 if (!vcpu->arch.mmu.slbmfev) {
202 emulated = EMULATE_FAIL;
203 } else {
204 ulong t, rb;
205
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100206 rb = kvmppc_get_gpr(vcpu, get_rb(inst));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000207 t = vcpu->arch.mmu.slbmfev(vcpu, rb);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100208 kvmppc_set_gpr(vcpu, get_rt(inst), t);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000209 }
210 break;
Alexander Grafbd7cdbb2010-03-24 21:48:33 +0100211 case OP_31_XOP_DCBA:
212 /* Gets treated as NOP */
213 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000214 case OP_31_XOP_DCBZ:
215 {
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100216 ulong rb = kvmppc_get_gpr(vcpu, get_rb(inst));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000217 ulong ra = 0;
Alexander Graf5467a972010-02-19 11:00:38 +0100218 ulong addr, vaddr;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000219 u32 zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Alexander Graf9fb244a2010-03-24 21:48:32 +0100220 u32 dsisr;
221 int r;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000222
223 if (get_ra(inst))
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100224 ra = kvmppc_get_gpr(vcpu, get_ra(inst));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000225
226 addr = (ra + rb) & ~31ULL;
Alexander Graf666e7252010-07-29 14:47:43 +0200227 if (!(vcpu->arch.shared->msr & MSR_SF))
Alexander Grafc215c6e2009-10-30 05:47:14 +0000228 addr &= 0xffffffff;
Alexander Graf5467a972010-02-19 11:00:38 +0100229 vaddr = addr;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000230
Alexander Graf9fb244a2010-03-24 21:48:32 +0100231 r = kvmppc_st(vcpu, &addr, 32, zeros, true);
232 if ((r == -ENOENT) || (r == -EPERM)) {
233 *advance = 0;
Alexander Graf5e030182010-07-29 14:47:45 +0200234 vcpu->arch.shared->dar = vaddr;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200235 to_svcpu(vcpu)->fault_dar = vaddr;
Alexander Graf9fb244a2010-03-24 21:48:32 +0100236
237 dsisr = DSISR_ISSTORE;
238 if (r == -ENOENT)
239 dsisr |= DSISR_NOHPTE;
240 else if (r == -EPERM)
241 dsisr |= DSISR_PROTFAULT;
242
Alexander Grafd562de42010-07-29 14:47:44 +0200243 vcpu->arch.shared->dsisr = dsisr;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200244 to_svcpu(vcpu)->fault_dsisr = dsisr;
Alexander Graf9fb244a2010-03-24 21:48:32 +0100245
Alexander Grafc215c6e2009-10-30 05:47:14 +0000246 kvmppc_book3s_queue_irqprio(vcpu,
247 BOOK3S_INTERRUPT_DATA_STORAGE);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000248 }
249
250 break;
251 }
252 default:
253 emulated = EMULATE_FAIL;
254 }
255 break;
256 default:
257 emulated = EMULATE_FAIL;
258 }
259
Alexander Graf831317b2010-02-19 11:00:44 +0100260 if (emulated == EMULATE_FAIL)
261 emulated = kvmppc_emulate_paired_single(run, vcpu);
262
Alexander Grafc215c6e2009-10-30 05:47:14 +0000263 return emulated;
264}
265
Alexander Grafe15a1132009-11-30 03:02:02 +0000266void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat, bool upper,
267 u32 val)
268{
269 if (upper) {
270 /* Upper BAT */
271 u32 bl = (val >> 2) & 0x7ff;
272 bat->bepi_mask = (~bl << 17);
273 bat->bepi = val & 0xfffe0000;
274 bat->vs = (val & 2) ? 1 : 0;
275 bat->vp = (val & 1) ? 1 : 0;
276 bat->raw = (bat->raw & 0xffffffff00000000ULL) | val;
277 } else {
278 /* Lower BAT */
279 bat->brpn = val & 0xfffe0000;
280 bat->wimg = (val >> 3) & 0xf;
281 bat->pp = val & 3;
282 bat->raw = (bat->raw & 0x00000000ffffffffULL) | ((u64)val << 32);
283 }
284}
285
Alexander Grafc1c88e22010-08-02 23:23:04 +0200286static struct kvmppc_bat *kvmppc_find_bat(struct kvm_vcpu *vcpu, int sprn)
Alexander Grafc04a6952010-03-24 21:48:25 +0100287{
288 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
289 struct kvmppc_bat *bat;
290
291 switch (sprn) {
292 case SPRN_IBAT0U ... SPRN_IBAT3L:
293 bat = &vcpu_book3s->ibat[(sprn - SPRN_IBAT0U) / 2];
294 break;
295 case SPRN_IBAT4U ... SPRN_IBAT7L:
296 bat = &vcpu_book3s->ibat[4 + ((sprn - SPRN_IBAT4U) / 2)];
297 break;
298 case SPRN_DBAT0U ... SPRN_DBAT3L:
299 bat = &vcpu_book3s->dbat[(sprn - SPRN_DBAT0U) / 2];
300 break;
301 case SPRN_DBAT4U ... SPRN_DBAT7L:
302 bat = &vcpu_book3s->dbat[4 + ((sprn - SPRN_DBAT4U) / 2)];
303 break;
304 default:
305 BUG();
306 }
307
Alexander Grafc1c88e22010-08-02 23:23:04 +0200308 return bat;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000309}
310
311int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
312{
313 int emulated = EMULATE_DONE;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100314 ulong spr_val = kvmppc_get_gpr(vcpu, rs);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000315
316 switch (sprn) {
317 case SPRN_SDR1:
Alexander Graf317a8fa2011-08-08 16:07:16 +0200318 if (!spr_allowed(vcpu, PRIV_HYPER))
319 goto unprivileged;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100320 to_book3s(vcpu)->sdr1 = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000321 break;
322 case SPRN_DSISR:
Alexander Grafd562de42010-07-29 14:47:44 +0200323 vcpu->arch.shared->dsisr = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000324 break;
325 case SPRN_DAR:
Alexander Graf5e030182010-07-29 14:47:45 +0200326 vcpu->arch.shared->dar = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000327 break;
328 case SPRN_HIOR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100329 to_book3s(vcpu)->hior = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000330 break;
331 case SPRN_IBAT0U ... SPRN_IBAT3L:
332 case SPRN_IBAT4U ... SPRN_IBAT7L:
333 case SPRN_DBAT0U ... SPRN_DBAT3L:
334 case SPRN_DBAT4U ... SPRN_DBAT7L:
Alexander Grafc1c88e22010-08-02 23:23:04 +0200335 {
336 struct kvmppc_bat *bat = kvmppc_find_bat(vcpu, sprn);
337
338 kvmppc_set_bat(vcpu, bat, !(sprn % 2), (u32)spr_val);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000339 /* BAT writes happen so rarely that we're ok to flush
340 * everything here */
341 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Alexander Grafc04a6952010-03-24 21:48:25 +0100342 kvmppc_mmu_flush_segments(vcpu);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000343 break;
Alexander Grafc1c88e22010-08-02 23:23:04 +0200344 }
Alexander Grafc215c6e2009-10-30 05:47:14 +0000345 case SPRN_HID0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100346 to_book3s(vcpu)->hid[0] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000347 break;
348 case SPRN_HID1:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100349 to_book3s(vcpu)->hid[1] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000350 break;
351 case SPRN_HID2:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100352 to_book3s(vcpu)->hid[2] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000353 break;
Alexander Grafd6d549b2010-02-19 11:00:33 +0100354 case SPRN_HID2_GEKKO:
355 to_book3s(vcpu)->hid[2] = spr_val;
356 /* HID2.PSE controls paired single on gekko */
357 switch (vcpu->arch.pvr) {
358 case 0x00080200: /* lonestar 2.0 */
359 case 0x00088202: /* lonestar 2.2 */
360 case 0x70000100: /* gekko 1.0 */
361 case 0x00080100: /* gekko 2.0 */
362 case 0x00083203: /* gekko 2.3a */
363 case 0x00083213: /* gekko 2.3b */
364 case 0x00083204: /* gekko 2.4 */
365 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
Alexander Grafb83d4a92010-04-20 02:49:54 +0200366 case 0x00087200: /* broadway */
367 if (vcpu->arch.hflags & BOOK3S_HFLAG_NATIVE_PS) {
368 /* Native paired singles */
369 } else if (spr_val & (1 << 29)) { /* HID2.PSE */
Alexander Grafd6d549b2010-02-19 11:00:33 +0100370 vcpu->arch.hflags |= BOOK3S_HFLAG_PAIRED_SINGLE;
371 kvmppc_giveup_ext(vcpu, MSR_FP);
372 } else {
373 vcpu->arch.hflags &= ~BOOK3S_HFLAG_PAIRED_SINGLE;
374 }
375 break;
376 }
377 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000378 case SPRN_HID4:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100379 case SPRN_HID4_GEKKO:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100380 to_book3s(vcpu)->hid[4] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000381 break;
382 case SPRN_HID5:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100383 to_book3s(vcpu)->hid[5] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000384 /* guest HID5 set can change is_dcbz32 */
385 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
386 (mfmsr() & MSR_HV))
387 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
388 break;
Alexander Grafd6d549b2010-02-19 11:00:33 +0100389 case SPRN_GQR0:
390 case SPRN_GQR1:
391 case SPRN_GQR2:
392 case SPRN_GQR3:
393 case SPRN_GQR4:
394 case SPRN_GQR5:
395 case SPRN_GQR6:
396 case SPRN_GQR7:
397 to_book3s(vcpu)->gqr[sprn - SPRN_GQR0] = spr_val;
398 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000399 case SPRN_ICTC:
400 case SPRN_THRM1:
401 case SPRN_THRM2:
402 case SPRN_THRM3:
403 case SPRN_CTRLF:
404 case SPRN_CTRLT:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100405 case SPRN_L2CR:
406 case SPRN_MMCR0_GEKKO:
407 case SPRN_MMCR1_GEKKO:
408 case SPRN_PMC1_GEKKO:
409 case SPRN_PMC2_GEKKO:
410 case SPRN_PMC3_GEKKO:
411 case SPRN_PMC4_GEKKO:
412 case SPRN_WPAR_GEKKO:
Alexander Grafc215c6e2009-10-30 05:47:14 +0000413 break;
Alexander Graf317a8fa2011-08-08 16:07:16 +0200414unprivileged:
Alexander Grafc215c6e2009-10-30 05:47:14 +0000415 default:
416 printk(KERN_INFO "KVM: invalid SPR write: %d\n", sprn);
417#ifndef DEBUG_SPR
418 emulated = EMULATE_FAIL;
419#endif
420 break;
421 }
422
423 return emulated;
424}
425
426int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
427{
428 int emulated = EMULATE_DONE;
429
430 switch (sprn) {
Alexander Grafc04a6952010-03-24 21:48:25 +0100431 case SPRN_IBAT0U ... SPRN_IBAT3L:
432 case SPRN_IBAT4U ... SPRN_IBAT7L:
433 case SPRN_DBAT0U ... SPRN_DBAT3L:
434 case SPRN_DBAT4U ... SPRN_DBAT7L:
Alexander Grafc1c88e22010-08-02 23:23:04 +0200435 {
436 struct kvmppc_bat *bat = kvmppc_find_bat(vcpu, sprn);
437
438 if (sprn % 2)
439 kvmppc_set_gpr(vcpu, rt, bat->raw >> 32);
440 else
441 kvmppc_set_gpr(vcpu, rt, bat->raw);
442
Alexander Grafc04a6952010-03-24 21:48:25 +0100443 break;
Alexander Grafc1c88e22010-08-02 23:23:04 +0200444 }
Alexander Grafc215c6e2009-10-30 05:47:14 +0000445 case SPRN_SDR1:
Alexander Graf317a8fa2011-08-08 16:07:16 +0200446 if (!spr_allowed(vcpu, PRIV_HYPER))
447 goto unprivileged;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100448 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->sdr1);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000449 break;
450 case SPRN_DSISR:
Alexander Grafd562de42010-07-29 14:47:44 +0200451 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->dsisr);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000452 break;
453 case SPRN_DAR:
Alexander Graf5e030182010-07-29 14:47:45 +0200454 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->dar);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000455 break;
456 case SPRN_HIOR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100457 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hior);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000458 break;
459 case SPRN_HID0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100460 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[0]);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000461 break;
462 case SPRN_HID1:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100463 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[1]);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000464 break;
465 case SPRN_HID2:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100466 case SPRN_HID2_GEKKO:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100467 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[2]);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000468 break;
469 case SPRN_HID4:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100470 case SPRN_HID4_GEKKO:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100471 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[4]);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000472 break;
473 case SPRN_HID5:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100474 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[5]);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000475 break;
Alexander Grafd6d549b2010-02-19 11:00:33 +0100476 case SPRN_GQR0:
477 case SPRN_GQR1:
478 case SPRN_GQR2:
479 case SPRN_GQR3:
480 case SPRN_GQR4:
481 case SPRN_GQR5:
482 case SPRN_GQR6:
483 case SPRN_GQR7:
484 kvmppc_set_gpr(vcpu, rt,
485 to_book3s(vcpu)->gqr[sprn - SPRN_GQR0]);
486 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000487 case SPRN_THRM1:
488 case SPRN_THRM2:
489 case SPRN_THRM3:
490 case SPRN_CTRLF:
491 case SPRN_CTRLT:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100492 case SPRN_L2CR:
493 case SPRN_MMCR0_GEKKO:
494 case SPRN_MMCR1_GEKKO:
495 case SPRN_PMC1_GEKKO:
496 case SPRN_PMC2_GEKKO:
497 case SPRN_PMC3_GEKKO:
498 case SPRN_PMC4_GEKKO:
499 case SPRN_WPAR_GEKKO:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100500 kvmppc_set_gpr(vcpu, rt, 0);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000501 break;
502 default:
Alexander Graf317a8fa2011-08-08 16:07:16 +0200503unprivileged:
Alexander Grafc215c6e2009-10-30 05:47:14 +0000504 printk(KERN_INFO "KVM: invalid SPR read: %d\n", sprn);
505#ifndef DEBUG_SPR
506 emulated = EMULATE_FAIL;
507#endif
508 break;
509 }
510
511 return emulated;
512}
513
Alexander Grafca7f4202010-03-24 21:48:28 +0100514u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst)
515{
516 u32 dsisr = 0;
517
518 /*
519 * This is what the spec says about DSISR bits (not mentioned = 0):
520 *
521 * 12:13 [DS] Set to bits 30:31
522 * 15:16 [X] Set to bits 29:30
523 * 17 [X] Set to bit 25
524 * [D/DS] Set to bit 5
525 * 18:21 [X] Set to bits 21:24
526 * [D/DS] Set to bits 1:4
527 * 22:26 Set to bits 6:10 (RT/RS/FRT/FRS)
528 * 27:31 Set to bits 11:15 (RA)
529 */
530
531 switch (get_op(inst)) {
532 /* D-form */
533 case OP_LFS:
534 case OP_LFD:
535 case OP_STFD:
536 case OP_STFS:
537 dsisr |= (inst >> 12) & 0x4000; /* bit 17 */
538 dsisr |= (inst >> 17) & 0x3c00; /* bits 18:21 */
539 break;
540 /* X-form */
541 case 31:
542 dsisr |= (inst << 14) & 0x18000; /* bits 15:16 */
543 dsisr |= (inst << 8) & 0x04000; /* bit 17 */
544 dsisr |= (inst << 3) & 0x03c00; /* bits 18:21 */
545 break;
546 default:
547 printk(KERN_INFO "KVM: Unaligned instruction 0x%x\n", inst);
548 break;
549 }
550
551 dsisr |= (inst >> 16) & 0x03ff; /* bits 22:31 */
552
553 return dsisr;
554}
555
556ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst)
557{
558 ulong dar = 0;
559 ulong ra;
560
561 switch (get_op(inst)) {
562 case OP_LFS:
563 case OP_LFD:
564 case OP_STFD:
565 case OP_STFS:
566 ra = get_ra(inst);
567 if (ra)
568 dar = kvmppc_get_gpr(vcpu, ra);
569 dar += (s32)((s16)inst);
570 break;
571 case 31:
572 ra = get_ra(inst);
573 if (ra)
574 dar = kvmppc_get_gpr(vcpu, ra);
575 dar += kvmppc_get_gpr(vcpu, get_rb(inst));
576 break;
577 default:
578 printk(KERN_INFO "KVM: Unaligned instruction 0x%x\n", inst);
579 break;
580 }
581
582 return dar;
583}