blob: 135663a3e4fc945d4eaf9ef7706fa388feccfe07 [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>
Benjamin Herrenschmidt95327d02012-04-01 17:35:53 +000024#include <asm/switch_to.h>
Alexander Grafc215c6e2009-10-30 05:47:14 +000025
26#define OP_19_XOP_RFID 18
27#define OP_19_XOP_RFI 50
28
29#define OP_31_XOP_MFMSR 83
30#define OP_31_XOP_MTMSR 146
31#define OP_31_XOP_MTMSRD 178
Alexander Graf71db4082010-02-19 11:00:37 +010032#define OP_31_XOP_MTSR 210
Alexander Grafc215c6e2009-10-30 05:47:14 +000033#define OP_31_XOP_MTSRIN 242
34#define OP_31_XOP_TLBIEL 274
35#define OP_31_XOP_TLBIE 306
36#define OP_31_XOP_SLBMTE 402
37#define OP_31_XOP_SLBIE 434
38#define OP_31_XOP_SLBIA 498
Alexander Grafc6648762010-03-24 21:48:24 +010039#define OP_31_XOP_MFSR 595
Alexander Grafc215c6e2009-10-30 05:47:14 +000040#define OP_31_XOP_MFSRIN 659
Alexander Grafbd7cdbb2010-03-24 21:48:33 +010041#define OP_31_XOP_DCBA 758
Alexander Grafc215c6e2009-10-30 05:47:14 +000042#define OP_31_XOP_SLBMFEV 851
43#define OP_31_XOP_EIOIO 854
44#define OP_31_XOP_SLBMFEE 915
45
46/* DCBZ is actually 1014, but we patch it to 1010 so we get a trap */
47#define OP_31_XOP_DCBZ 1010
48
Alexander Grafca7f4202010-03-24 21:48:28 +010049#define OP_LFS 48
50#define OP_LFD 50
51#define OP_STFS 52
52#define OP_STFD 54
53
Alexander Grafd6d549b2010-02-19 11:00:33 +010054#define SPRN_GQR0 912
55#define SPRN_GQR1 913
56#define SPRN_GQR2 914
57#define SPRN_GQR3 915
58#define SPRN_GQR4 916
59#define SPRN_GQR5 917
60#define SPRN_GQR6 918
61#define SPRN_GQR7 919
62
Alexander Graf07b09072010-04-16 00:11:53 +020063/* Book3S_32 defines mfsrin(v) - but that messes up our abstract
64 * function pointers, so let's just disable the define. */
65#undef mfsrin
66
Alexander Graf317a8fa2011-08-08 16:07:16 +020067enum priv_level {
68 PRIV_PROBLEM = 0,
69 PRIV_SUPER = 1,
70 PRIV_HYPER = 2,
71};
72
73static bool spr_allowed(struct kvm_vcpu *vcpu, enum priv_level level)
74{
75 /* PAPR VMs only access supervisor SPRs */
76 if (vcpu->arch.papr_enabled && (level > PRIV_SUPER))
77 return false;
78
79 /* Limit user space to its own small SPR set */
80 if ((vcpu->arch.shared->msr & MSR_PR) && level > PRIV_PROBLEM)
81 return false;
82
83 return true;
84}
85
Alexander Grafc215c6e2009-10-30 05:47:14 +000086int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
87 unsigned int inst, int *advance)
88{
89 int emulated = EMULATE_DONE;
90
91 switch (get_op(inst)) {
92 case 19:
93 switch (get_xop(inst)) {
94 case OP_19_XOP_RFID:
95 case OP_19_XOP_RFI:
Alexander Grafde7906c2010-07-29 14:47:46 +020096 kvmppc_set_pc(vcpu, vcpu->arch.shared->srr0);
97 kvmppc_set_msr(vcpu, vcpu->arch.shared->srr1);
Alexander Grafc215c6e2009-10-30 05:47:14 +000098 *advance = 0;
99 break;
100
101 default:
102 emulated = EMULATE_FAIL;
103 break;
104 }
105 break;
106 case 31:
107 switch (get_xop(inst)) {
108 case OP_31_XOP_MFMSR:
Alexander Graf666e7252010-07-29 14:47:43 +0200109 kvmppc_set_gpr(vcpu, get_rt(inst),
110 vcpu->arch.shared->msr);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000111 break;
112 case OP_31_XOP_MTMSRD:
113 {
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100114 ulong rs = kvmppc_get_gpr(vcpu, get_rs(inst));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000115 if (inst & 0x10000) {
Alexander Graf666e7252010-07-29 14:47:43 +0200116 vcpu->arch.shared->msr &= ~(MSR_RI | MSR_EE);
117 vcpu->arch.shared->msr |= rs & (MSR_RI | MSR_EE);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000118 } else
119 kvmppc_set_msr(vcpu, rs);
120 break;
121 }
122 case OP_31_XOP_MTMSR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100123 kvmppc_set_msr(vcpu, kvmppc_get_gpr(vcpu, get_rs(inst)));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000124 break;
Alexander Grafc6648762010-03-24 21:48:24 +0100125 case OP_31_XOP_MFSR:
126 {
127 int srnum;
128
129 srnum = kvmppc_get_field(inst, 12 + 32, 15 + 32);
130 if (vcpu->arch.mmu.mfsrin) {
131 u32 sr;
132 sr = vcpu->arch.mmu.mfsrin(vcpu, srnum);
133 kvmppc_set_gpr(vcpu, get_rt(inst), sr);
134 }
135 break;
136 }
Alexander Grafc215c6e2009-10-30 05:47:14 +0000137 case OP_31_XOP_MFSRIN:
138 {
139 int srnum;
140
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100141 srnum = (kvmppc_get_gpr(vcpu, get_rb(inst)) >> 28) & 0xf;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000142 if (vcpu->arch.mmu.mfsrin) {
143 u32 sr;
144 sr = vcpu->arch.mmu.mfsrin(vcpu, srnum);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100145 kvmppc_set_gpr(vcpu, get_rt(inst), sr);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000146 }
147 break;
148 }
Alexander Graf71db4082010-02-19 11:00:37 +0100149 case OP_31_XOP_MTSR:
150 vcpu->arch.mmu.mtsrin(vcpu,
151 (inst >> 16) & 0xf,
152 kvmppc_get_gpr(vcpu, get_rs(inst)));
153 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000154 case OP_31_XOP_MTSRIN:
155 vcpu->arch.mmu.mtsrin(vcpu,
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100156 (kvmppc_get_gpr(vcpu, get_rb(inst)) >> 28) & 0xf,
157 kvmppc_get_gpr(vcpu, get_rs(inst)));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000158 break;
159 case OP_31_XOP_TLBIE:
160 case OP_31_XOP_TLBIEL:
161 {
162 bool large = (inst & 0x00200000) ? true : false;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100163 ulong addr = kvmppc_get_gpr(vcpu, get_rb(inst));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000164 vcpu->arch.mmu.tlbie(vcpu, addr, large);
165 break;
166 }
167 case OP_31_XOP_EIOIO:
168 break;
169 case OP_31_XOP_SLBMTE:
170 if (!vcpu->arch.mmu.slbmte)
171 return EMULATE_FAIL;
172
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100173 vcpu->arch.mmu.slbmte(vcpu,
174 kvmppc_get_gpr(vcpu, get_rs(inst)),
175 kvmppc_get_gpr(vcpu, get_rb(inst)));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000176 break;
177 case OP_31_XOP_SLBIE:
178 if (!vcpu->arch.mmu.slbie)
179 return EMULATE_FAIL;
180
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100181 vcpu->arch.mmu.slbie(vcpu,
182 kvmppc_get_gpr(vcpu, get_rb(inst)));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000183 break;
184 case OP_31_XOP_SLBIA:
185 if (!vcpu->arch.mmu.slbia)
186 return EMULATE_FAIL;
187
188 vcpu->arch.mmu.slbia(vcpu);
189 break;
190 case OP_31_XOP_SLBMFEE:
191 if (!vcpu->arch.mmu.slbmfee) {
192 emulated = EMULATE_FAIL;
193 } else {
194 ulong t, rb;
195
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100196 rb = kvmppc_get_gpr(vcpu, get_rb(inst));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000197 t = vcpu->arch.mmu.slbmfee(vcpu, rb);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100198 kvmppc_set_gpr(vcpu, get_rt(inst), t);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000199 }
200 break;
201 case OP_31_XOP_SLBMFEV:
202 if (!vcpu->arch.mmu.slbmfev) {
203 emulated = EMULATE_FAIL;
204 } else {
205 ulong t, rb;
206
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100207 rb = kvmppc_get_gpr(vcpu, get_rb(inst));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000208 t = vcpu->arch.mmu.slbmfev(vcpu, rb);
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100209 kvmppc_set_gpr(vcpu, get_rt(inst), t);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000210 }
211 break;
Alexander Grafbd7cdbb2010-03-24 21:48:33 +0100212 case OP_31_XOP_DCBA:
213 /* Gets treated as NOP */
214 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000215 case OP_31_XOP_DCBZ:
216 {
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100217 ulong rb = kvmppc_get_gpr(vcpu, get_rb(inst));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000218 ulong ra = 0;
Alexander Graf5467a972010-02-19 11:00:38 +0100219 ulong addr, vaddr;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000220 u32 zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Alexander Graf9fb244a2010-03-24 21:48:32 +0100221 u32 dsisr;
222 int r;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000223
224 if (get_ra(inst))
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100225 ra = kvmppc_get_gpr(vcpu, get_ra(inst));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000226
227 addr = (ra + rb) & ~31ULL;
Alexander Graf666e7252010-07-29 14:47:43 +0200228 if (!(vcpu->arch.shared->msr & MSR_SF))
Alexander Grafc215c6e2009-10-30 05:47:14 +0000229 addr &= 0xffffffff;
Alexander Graf5467a972010-02-19 11:00:38 +0100230 vaddr = addr;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000231
Alexander Graf9fb244a2010-03-24 21:48:32 +0100232 r = kvmppc_st(vcpu, &addr, 32, zeros, true);
233 if ((r == -ENOENT) || (r == -EPERM)) {
Alexander Graf468a12c2011-12-09 14:44:13 +0100234 struct kvmppc_book3s_shadow_vcpu *svcpu;
235
236 svcpu = svcpu_get(vcpu);
Alexander Graf9fb244a2010-03-24 21:48:32 +0100237 *advance = 0;
Alexander Graf5e030182010-07-29 14:47:45 +0200238 vcpu->arch.shared->dar = vaddr;
Alexander Graf468a12c2011-12-09 14:44:13 +0100239 svcpu->fault_dar = vaddr;
Alexander Graf9fb244a2010-03-24 21:48:32 +0100240
241 dsisr = DSISR_ISSTORE;
242 if (r == -ENOENT)
243 dsisr |= DSISR_NOHPTE;
244 else if (r == -EPERM)
245 dsisr |= DSISR_PROTFAULT;
246
Alexander Grafd562de42010-07-29 14:47:44 +0200247 vcpu->arch.shared->dsisr = dsisr;
Alexander Graf468a12c2011-12-09 14:44:13 +0100248 svcpu->fault_dsisr = dsisr;
249 svcpu_put(svcpu);
Alexander Graf9fb244a2010-03-24 21:48:32 +0100250
Alexander Grafc215c6e2009-10-30 05:47:14 +0000251 kvmppc_book3s_queue_irqprio(vcpu,
252 BOOK3S_INTERRUPT_DATA_STORAGE);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000253 }
254
255 break;
256 }
257 default:
258 emulated = EMULATE_FAIL;
259 }
260 break;
261 default:
262 emulated = EMULATE_FAIL;
263 }
264
Alexander Graf831317b2010-02-19 11:00:44 +0100265 if (emulated == EMULATE_FAIL)
266 emulated = kvmppc_emulate_paired_single(run, vcpu);
267
Alexander Grafc215c6e2009-10-30 05:47:14 +0000268 return emulated;
269}
270
Alexander Grafe15a1132009-11-30 03:02:02 +0000271void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat, bool upper,
272 u32 val)
273{
274 if (upper) {
275 /* Upper BAT */
276 u32 bl = (val >> 2) & 0x7ff;
277 bat->bepi_mask = (~bl << 17);
278 bat->bepi = val & 0xfffe0000;
279 bat->vs = (val & 2) ? 1 : 0;
280 bat->vp = (val & 1) ? 1 : 0;
281 bat->raw = (bat->raw & 0xffffffff00000000ULL) | val;
282 } else {
283 /* Lower BAT */
284 bat->brpn = val & 0xfffe0000;
285 bat->wimg = (val >> 3) & 0xf;
286 bat->pp = val & 3;
287 bat->raw = (bat->raw & 0x00000000ffffffffULL) | ((u64)val << 32);
288 }
289}
290
Alexander Grafc1c88e22010-08-02 23:23:04 +0200291static struct kvmppc_bat *kvmppc_find_bat(struct kvm_vcpu *vcpu, int sprn)
Alexander Grafc04a6952010-03-24 21:48:25 +0100292{
293 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
294 struct kvmppc_bat *bat;
295
296 switch (sprn) {
297 case SPRN_IBAT0U ... SPRN_IBAT3L:
298 bat = &vcpu_book3s->ibat[(sprn - SPRN_IBAT0U) / 2];
299 break;
300 case SPRN_IBAT4U ... SPRN_IBAT7L:
301 bat = &vcpu_book3s->ibat[4 + ((sprn - SPRN_IBAT4U) / 2)];
302 break;
303 case SPRN_DBAT0U ... SPRN_DBAT3L:
304 bat = &vcpu_book3s->dbat[(sprn - SPRN_DBAT0U) / 2];
305 break;
306 case SPRN_DBAT4U ... SPRN_DBAT7L:
307 bat = &vcpu_book3s->dbat[4 + ((sprn - SPRN_DBAT4U) / 2)];
308 break;
309 default:
310 BUG();
311 }
312
Alexander Grafc1c88e22010-08-02 23:23:04 +0200313 return bat;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000314}
315
316int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
317{
318 int emulated = EMULATE_DONE;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100319 ulong spr_val = kvmppc_get_gpr(vcpu, rs);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000320
321 switch (sprn) {
322 case SPRN_SDR1:
Alexander Graf317a8fa2011-08-08 16:07:16 +0200323 if (!spr_allowed(vcpu, PRIV_HYPER))
324 goto unprivileged;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100325 to_book3s(vcpu)->sdr1 = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000326 break;
327 case SPRN_DSISR:
Alexander Grafd562de42010-07-29 14:47:44 +0200328 vcpu->arch.shared->dsisr = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000329 break;
330 case SPRN_DAR:
Alexander Graf5e030182010-07-29 14:47:45 +0200331 vcpu->arch.shared->dar = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000332 break;
333 case SPRN_HIOR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100334 to_book3s(vcpu)->hior = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000335 break;
336 case SPRN_IBAT0U ... SPRN_IBAT3L:
337 case SPRN_IBAT4U ... SPRN_IBAT7L:
338 case SPRN_DBAT0U ... SPRN_DBAT3L:
339 case SPRN_DBAT4U ... SPRN_DBAT7L:
Alexander Grafc1c88e22010-08-02 23:23:04 +0200340 {
341 struct kvmppc_bat *bat = kvmppc_find_bat(vcpu, sprn);
342
343 kvmppc_set_bat(vcpu, bat, !(sprn % 2), (u32)spr_val);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000344 /* BAT writes happen so rarely that we're ok to flush
345 * everything here */
346 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Alexander Grafc04a6952010-03-24 21:48:25 +0100347 kvmppc_mmu_flush_segments(vcpu);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000348 break;
Alexander Grafc1c88e22010-08-02 23:23:04 +0200349 }
Alexander Grafc215c6e2009-10-30 05:47:14 +0000350 case SPRN_HID0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100351 to_book3s(vcpu)->hid[0] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000352 break;
353 case SPRN_HID1:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100354 to_book3s(vcpu)->hid[1] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000355 break;
356 case SPRN_HID2:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100357 to_book3s(vcpu)->hid[2] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000358 break;
Alexander Grafd6d549b2010-02-19 11:00:33 +0100359 case SPRN_HID2_GEKKO:
360 to_book3s(vcpu)->hid[2] = spr_val;
361 /* HID2.PSE controls paired single on gekko */
362 switch (vcpu->arch.pvr) {
363 case 0x00080200: /* lonestar 2.0 */
364 case 0x00088202: /* lonestar 2.2 */
365 case 0x70000100: /* gekko 1.0 */
366 case 0x00080100: /* gekko 2.0 */
367 case 0x00083203: /* gekko 2.3a */
368 case 0x00083213: /* gekko 2.3b */
369 case 0x00083204: /* gekko 2.4 */
370 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
Alexander Grafb83d4a92010-04-20 02:49:54 +0200371 case 0x00087200: /* broadway */
372 if (vcpu->arch.hflags & BOOK3S_HFLAG_NATIVE_PS) {
373 /* Native paired singles */
374 } else if (spr_val & (1 << 29)) { /* HID2.PSE */
Alexander Grafd6d549b2010-02-19 11:00:33 +0100375 vcpu->arch.hflags |= BOOK3S_HFLAG_PAIRED_SINGLE;
376 kvmppc_giveup_ext(vcpu, MSR_FP);
377 } else {
378 vcpu->arch.hflags &= ~BOOK3S_HFLAG_PAIRED_SINGLE;
379 }
380 break;
381 }
382 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000383 case SPRN_HID4:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100384 case SPRN_HID4_GEKKO:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100385 to_book3s(vcpu)->hid[4] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000386 break;
387 case SPRN_HID5:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100388 to_book3s(vcpu)->hid[5] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000389 /* guest HID5 set can change is_dcbz32 */
390 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
391 (mfmsr() & MSR_HV))
392 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
393 break;
Alexander Grafd6d549b2010-02-19 11:00:33 +0100394 case SPRN_GQR0:
395 case SPRN_GQR1:
396 case SPRN_GQR2:
397 case SPRN_GQR3:
398 case SPRN_GQR4:
399 case SPRN_GQR5:
400 case SPRN_GQR6:
401 case SPRN_GQR7:
402 to_book3s(vcpu)->gqr[sprn - SPRN_GQR0] = spr_val;
403 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000404 case SPRN_ICTC:
405 case SPRN_THRM1:
406 case SPRN_THRM2:
407 case SPRN_THRM3:
408 case SPRN_CTRLF:
409 case SPRN_CTRLT:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100410 case SPRN_L2CR:
411 case SPRN_MMCR0_GEKKO:
412 case SPRN_MMCR1_GEKKO:
413 case SPRN_PMC1_GEKKO:
414 case SPRN_PMC2_GEKKO:
415 case SPRN_PMC3_GEKKO:
416 case SPRN_PMC4_GEKKO:
417 case SPRN_WPAR_GEKKO:
Alexander Grafc215c6e2009-10-30 05:47:14 +0000418 break;
Alexander Graf317a8fa2011-08-08 16:07:16 +0200419unprivileged:
Alexander Grafc215c6e2009-10-30 05:47:14 +0000420 default:
421 printk(KERN_INFO "KVM: invalid SPR write: %d\n", sprn);
422#ifndef DEBUG_SPR
423 emulated = EMULATE_FAIL;
424#endif
425 break;
426 }
427
428 return emulated;
429}
430
431int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
432{
433 int emulated = EMULATE_DONE;
434
435 switch (sprn) {
Alexander Grafc04a6952010-03-24 21:48:25 +0100436 case SPRN_IBAT0U ... SPRN_IBAT3L:
437 case SPRN_IBAT4U ... SPRN_IBAT7L:
438 case SPRN_DBAT0U ... SPRN_DBAT3L:
439 case SPRN_DBAT4U ... SPRN_DBAT7L:
Alexander Grafc1c88e22010-08-02 23:23:04 +0200440 {
441 struct kvmppc_bat *bat = kvmppc_find_bat(vcpu, sprn);
442
443 if (sprn % 2)
444 kvmppc_set_gpr(vcpu, rt, bat->raw >> 32);
445 else
446 kvmppc_set_gpr(vcpu, rt, bat->raw);
447
Alexander Grafc04a6952010-03-24 21:48:25 +0100448 break;
Alexander Grafc1c88e22010-08-02 23:23:04 +0200449 }
Alexander Grafc215c6e2009-10-30 05:47:14 +0000450 case SPRN_SDR1:
Alexander Graf317a8fa2011-08-08 16:07:16 +0200451 if (!spr_allowed(vcpu, PRIV_HYPER))
452 goto unprivileged;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100453 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->sdr1);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000454 break;
455 case SPRN_DSISR:
Alexander Grafd562de42010-07-29 14:47:44 +0200456 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->dsisr);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000457 break;
458 case SPRN_DAR:
Alexander Graf5e030182010-07-29 14:47:45 +0200459 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->dar);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000460 break;
461 case SPRN_HIOR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100462 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hior);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000463 break;
464 case SPRN_HID0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100465 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[0]);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000466 break;
467 case SPRN_HID1:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100468 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[1]);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000469 break;
470 case SPRN_HID2:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100471 case SPRN_HID2_GEKKO:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100472 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[2]);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000473 break;
474 case SPRN_HID4:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100475 case SPRN_HID4_GEKKO:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100476 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[4]);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000477 break;
478 case SPRN_HID5:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100479 kvmppc_set_gpr(vcpu, rt, to_book3s(vcpu)->hid[5]);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000480 break;
Alexander Grafaacf9aa2011-08-08 17:22:59 +0200481 case SPRN_CFAR:
482 case SPRN_PURR:
483 kvmppc_set_gpr(vcpu, rt, 0);
484 break;
Alexander Grafd6d549b2010-02-19 11:00:33 +0100485 case SPRN_GQR0:
486 case SPRN_GQR1:
487 case SPRN_GQR2:
488 case SPRN_GQR3:
489 case SPRN_GQR4:
490 case SPRN_GQR5:
491 case SPRN_GQR6:
492 case SPRN_GQR7:
493 kvmppc_set_gpr(vcpu, rt,
494 to_book3s(vcpu)->gqr[sprn - SPRN_GQR0]);
495 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000496 case SPRN_THRM1:
497 case SPRN_THRM2:
498 case SPRN_THRM3:
499 case SPRN_CTRLF:
500 case SPRN_CTRLT:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100501 case SPRN_L2CR:
502 case SPRN_MMCR0_GEKKO:
503 case SPRN_MMCR1_GEKKO:
504 case SPRN_PMC1_GEKKO:
505 case SPRN_PMC2_GEKKO:
506 case SPRN_PMC3_GEKKO:
507 case SPRN_PMC4_GEKKO:
508 case SPRN_WPAR_GEKKO:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100509 kvmppc_set_gpr(vcpu, rt, 0);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000510 break;
511 default:
Alexander Graf317a8fa2011-08-08 16:07:16 +0200512unprivileged:
Alexander Grafc215c6e2009-10-30 05:47:14 +0000513 printk(KERN_INFO "KVM: invalid SPR read: %d\n", sprn);
514#ifndef DEBUG_SPR
515 emulated = EMULATE_FAIL;
516#endif
517 break;
518 }
519
520 return emulated;
521}
522
Alexander Grafca7f4202010-03-24 21:48:28 +0100523u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst)
524{
525 u32 dsisr = 0;
526
527 /*
528 * This is what the spec says about DSISR bits (not mentioned = 0):
529 *
530 * 12:13 [DS] Set to bits 30:31
531 * 15:16 [X] Set to bits 29:30
532 * 17 [X] Set to bit 25
533 * [D/DS] Set to bit 5
534 * 18:21 [X] Set to bits 21:24
535 * [D/DS] Set to bits 1:4
536 * 22:26 Set to bits 6:10 (RT/RS/FRT/FRS)
537 * 27:31 Set to bits 11:15 (RA)
538 */
539
540 switch (get_op(inst)) {
541 /* D-form */
542 case OP_LFS:
543 case OP_LFD:
544 case OP_STFD:
545 case OP_STFS:
546 dsisr |= (inst >> 12) & 0x4000; /* bit 17 */
547 dsisr |= (inst >> 17) & 0x3c00; /* bits 18:21 */
548 break;
549 /* X-form */
550 case 31:
551 dsisr |= (inst << 14) & 0x18000; /* bits 15:16 */
552 dsisr |= (inst << 8) & 0x04000; /* bit 17 */
553 dsisr |= (inst << 3) & 0x03c00; /* bits 18:21 */
554 break;
555 default:
556 printk(KERN_INFO "KVM: Unaligned instruction 0x%x\n", inst);
557 break;
558 }
559
560 dsisr |= (inst >> 16) & 0x03ff; /* bits 22:31 */
561
562 return dsisr;
563}
564
565ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst)
566{
567 ulong dar = 0;
568 ulong ra;
569
570 switch (get_op(inst)) {
571 case OP_LFS:
572 case OP_LFD:
573 case OP_STFD:
574 case OP_STFS:
575 ra = get_ra(inst);
576 if (ra)
577 dar = kvmppc_get_gpr(vcpu, ra);
578 dar += (s32)((s16)inst);
579 break;
580 case 31:
581 ra = get_ra(inst);
582 if (ra)
583 dar = kvmppc_get_gpr(vcpu, ra);
584 dar += kvmppc_get_gpr(vcpu, get_rb(inst));
585 break;
586 default:
587 printk(KERN_INFO "KVM: Unaligned instruction 0x%x\n", inst);
588 break;
589 }
590
591 return dar;
592}