blob: b9a989dc76ccff5cd739f22910c4201c0dafb4e2 [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;
Alexander Grafc46dc9a2012-05-04 14:01:33 +020090 int rt = get_rt(inst);
91 int rs = get_rs(inst);
92 int ra = get_ra(inst);
93 int rb = get_rb(inst);
Alexander Grafc215c6e2009-10-30 05:47:14 +000094
95 switch (get_op(inst)) {
96 case 19:
97 switch (get_xop(inst)) {
98 case OP_19_XOP_RFID:
99 case OP_19_XOP_RFI:
Alexander Grafde7906c2010-07-29 14:47:46 +0200100 kvmppc_set_pc(vcpu, vcpu->arch.shared->srr0);
101 kvmppc_set_msr(vcpu, vcpu->arch.shared->srr1);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000102 *advance = 0;
103 break;
104
105 default:
106 emulated = EMULATE_FAIL;
107 break;
108 }
109 break;
110 case 31:
111 switch (get_xop(inst)) {
112 case OP_31_XOP_MFMSR:
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200113 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->msr);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000114 break;
115 case OP_31_XOP_MTMSRD:
116 {
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200117 ulong rs_val = kvmppc_get_gpr(vcpu, rs);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000118 if (inst & 0x10000) {
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200119 ulong new_msr = vcpu->arch.shared->msr;
120 new_msr &= ~(MSR_RI | MSR_EE);
121 new_msr |= rs_val & (MSR_RI | MSR_EE);
122 vcpu->arch.shared->msr = new_msr;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000123 } else
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200124 kvmppc_set_msr(vcpu, rs_val);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000125 break;
126 }
127 case OP_31_XOP_MTMSR:
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200128 kvmppc_set_msr(vcpu, kvmppc_get_gpr(vcpu, rs));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000129 break;
Alexander Grafc6648762010-03-24 21:48:24 +0100130 case OP_31_XOP_MFSR:
131 {
132 int srnum;
133
134 srnum = kvmppc_get_field(inst, 12 + 32, 15 + 32);
135 if (vcpu->arch.mmu.mfsrin) {
136 u32 sr;
137 sr = vcpu->arch.mmu.mfsrin(vcpu, srnum);
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200138 kvmppc_set_gpr(vcpu, rt, sr);
Alexander Grafc6648762010-03-24 21:48:24 +0100139 }
140 break;
141 }
Alexander Grafc215c6e2009-10-30 05:47:14 +0000142 case OP_31_XOP_MFSRIN:
143 {
144 int srnum;
145
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200146 srnum = (kvmppc_get_gpr(vcpu, rb) >> 28) & 0xf;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000147 if (vcpu->arch.mmu.mfsrin) {
148 u32 sr;
149 sr = vcpu->arch.mmu.mfsrin(vcpu, srnum);
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200150 kvmppc_set_gpr(vcpu, rt, sr);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000151 }
152 break;
153 }
Alexander Graf71db4082010-02-19 11:00:37 +0100154 case OP_31_XOP_MTSR:
155 vcpu->arch.mmu.mtsrin(vcpu,
156 (inst >> 16) & 0xf,
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200157 kvmppc_get_gpr(vcpu, rs));
Alexander Graf71db4082010-02-19 11:00:37 +0100158 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000159 case OP_31_XOP_MTSRIN:
160 vcpu->arch.mmu.mtsrin(vcpu,
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200161 (kvmppc_get_gpr(vcpu, rb) >> 28) & 0xf,
162 kvmppc_get_gpr(vcpu, rs));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000163 break;
164 case OP_31_XOP_TLBIE:
165 case OP_31_XOP_TLBIEL:
166 {
167 bool large = (inst & 0x00200000) ? true : false;
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200168 ulong addr = kvmppc_get_gpr(vcpu, rb);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000169 vcpu->arch.mmu.tlbie(vcpu, addr, large);
170 break;
171 }
172 case OP_31_XOP_EIOIO:
173 break;
174 case OP_31_XOP_SLBMTE:
175 if (!vcpu->arch.mmu.slbmte)
176 return EMULATE_FAIL;
177
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100178 vcpu->arch.mmu.slbmte(vcpu,
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200179 kvmppc_get_gpr(vcpu, rs),
180 kvmppc_get_gpr(vcpu, rb));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000181 break;
182 case OP_31_XOP_SLBIE:
183 if (!vcpu->arch.mmu.slbie)
184 return EMULATE_FAIL;
185
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100186 vcpu->arch.mmu.slbie(vcpu,
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200187 kvmppc_get_gpr(vcpu, rb));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000188 break;
189 case OP_31_XOP_SLBIA:
190 if (!vcpu->arch.mmu.slbia)
191 return EMULATE_FAIL;
192
193 vcpu->arch.mmu.slbia(vcpu);
194 break;
195 case OP_31_XOP_SLBMFEE:
196 if (!vcpu->arch.mmu.slbmfee) {
197 emulated = EMULATE_FAIL;
198 } else {
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200199 ulong t, rb_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000200
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200201 rb_val = kvmppc_get_gpr(vcpu, rb);
202 t = vcpu->arch.mmu.slbmfee(vcpu, rb_val);
203 kvmppc_set_gpr(vcpu, rt, t);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000204 }
205 break;
206 case OP_31_XOP_SLBMFEV:
207 if (!vcpu->arch.mmu.slbmfev) {
208 emulated = EMULATE_FAIL;
209 } else {
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200210 ulong t, rb_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000211
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200212 rb_val = kvmppc_get_gpr(vcpu, rb);
213 t = vcpu->arch.mmu.slbmfev(vcpu, rb_val);
214 kvmppc_set_gpr(vcpu, rt, t);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000215 }
216 break;
Alexander Grafbd7cdbb2010-03-24 21:48:33 +0100217 case OP_31_XOP_DCBA:
218 /* Gets treated as NOP */
219 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000220 case OP_31_XOP_DCBZ:
221 {
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200222 ulong rb_val = kvmppc_get_gpr(vcpu, rb);
223 ulong ra_val = 0;
Alexander Graf5467a972010-02-19 11:00:38 +0100224 ulong addr, vaddr;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000225 u32 zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Alexander Graf9fb244a2010-03-24 21:48:32 +0100226 u32 dsisr;
227 int r;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000228
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200229 if (ra)
230 ra_val = kvmppc_get_gpr(vcpu, ra);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000231
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200232 addr = (ra_val + rb_val) & ~31ULL;
Alexander Graf666e7252010-07-29 14:47:43 +0200233 if (!(vcpu->arch.shared->msr & MSR_SF))
Alexander Grafc215c6e2009-10-30 05:47:14 +0000234 addr &= 0xffffffff;
Alexander Graf5467a972010-02-19 11:00:38 +0100235 vaddr = addr;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000236
Alexander Graf9fb244a2010-03-24 21:48:32 +0100237 r = kvmppc_st(vcpu, &addr, 32, zeros, true);
238 if ((r == -ENOENT) || (r == -EPERM)) {
Alexander Graf468a12c2011-12-09 14:44:13 +0100239 struct kvmppc_book3s_shadow_vcpu *svcpu;
240
241 svcpu = svcpu_get(vcpu);
Alexander Graf9fb244a2010-03-24 21:48:32 +0100242 *advance = 0;
Alexander Graf5e030182010-07-29 14:47:45 +0200243 vcpu->arch.shared->dar = vaddr;
Alexander Graf468a12c2011-12-09 14:44:13 +0100244 svcpu->fault_dar = vaddr;
Alexander Graf9fb244a2010-03-24 21:48:32 +0100245
246 dsisr = DSISR_ISSTORE;
247 if (r == -ENOENT)
248 dsisr |= DSISR_NOHPTE;
249 else if (r == -EPERM)
250 dsisr |= DSISR_PROTFAULT;
251
Alexander Grafd562de42010-07-29 14:47:44 +0200252 vcpu->arch.shared->dsisr = dsisr;
Alexander Graf468a12c2011-12-09 14:44:13 +0100253 svcpu->fault_dsisr = dsisr;
254 svcpu_put(svcpu);
Alexander Graf9fb244a2010-03-24 21:48:32 +0100255
Alexander Grafc215c6e2009-10-30 05:47:14 +0000256 kvmppc_book3s_queue_irqprio(vcpu,
257 BOOK3S_INTERRUPT_DATA_STORAGE);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000258 }
259
260 break;
261 }
262 default:
263 emulated = EMULATE_FAIL;
264 }
265 break;
266 default:
267 emulated = EMULATE_FAIL;
268 }
269
Alexander Graf831317b2010-02-19 11:00:44 +0100270 if (emulated == EMULATE_FAIL)
271 emulated = kvmppc_emulate_paired_single(run, vcpu);
272
Alexander Grafc215c6e2009-10-30 05:47:14 +0000273 return emulated;
274}
275
Alexander Grafe15a1132009-11-30 03:02:02 +0000276void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat, bool upper,
277 u32 val)
278{
279 if (upper) {
280 /* Upper BAT */
281 u32 bl = (val >> 2) & 0x7ff;
282 bat->bepi_mask = (~bl << 17);
283 bat->bepi = val & 0xfffe0000;
284 bat->vs = (val & 2) ? 1 : 0;
285 bat->vp = (val & 1) ? 1 : 0;
286 bat->raw = (bat->raw & 0xffffffff00000000ULL) | val;
287 } else {
288 /* Lower BAT */
289 bat->brpn = val & 0xfffe0000;
290 bat->wimg = (val >> 3) & 0xf;
291 bat->pp = val & 3;
292 bat->raw = (bat->raw & 0x00000000ffffffffULL) | ((u64)val << 32);
293 }
294}
295
Alexander Grafc1c88e22010-08-02 23:23:04 +0200296static struct kvmppc_bat *kvmppc_find_bat(struct kvm_vcpu *vcpu, int sprn)
Alexander Grafc04a6952010-03-24 21:48:25 +0100297{
298 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
299 struct kvmppc_bat *bat;
300
301 switch (sprn) {
302 case SPRN_IBAT0U ... SPRN_IBAT3L:
303 bat = &vcpu_book3s->ibat[(sprn - SPRN_IBAT0U) / 2];
304 break;
305 case SPRN_IBAT4U ... SPRN_IBAT7L:
306 bat = &vcpu_book3s->ibat[4 + ((sprn - SPRN_IBAT4U) / 2)];
307 break;
308 case SPRN_DBAT0U ... SPRN_DBAT3L:
309 bat = &vcpu_book3s->dbat[(sprn - SPRN_DBAT0U) / 2];
310 break;
311 case SPRN_DBAT4U ... SPRN_DBAT7L:
312 bat = &vcpu_book3s->dbat[4 + ((sprn - SPRN_DBAT4U) / 2)];
313 break;
314 default:
315 BUG();
316 }
317
Alexander Grafc1c88e22010-08-02 23:23:04 +0200318 return bat;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000319}
320
Alexander Graf54771e62012-05-04 14:55:12 +0200321int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
Alexander Grafc215c6e2009-10-30 05:47:14 +0000322{
323 int emulated = EMULATE_DONE;
324
325 switch (sprn) {
326 case SPRN_SDR1:
Alexander Graf317a8fa2011-08-08 16:07:16 +0200327 if (!spr_allowed(vcpu, PRIV_HYPER))
328 goto unprivileged;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100329 to_book3s(vcpu)->sdr1 = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000330 break;
331 case SPRN_DSISR:
Alexander Grafd562de42010-07-29 14:47:44 +0200332 vcpu->arch.shared->dsisr = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000333 break;
334 case SPRN_DAR:
Alexander Graf5e030182010-07-29 14:47:45 +0200335 vcpu->arch.shared->dar = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000336 break;
337 case SPRN_HIOR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100338 to_book3s(vcpu)->hior = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000339 break;
340 case SPRN_IBAT0U ... SPRN_IBAT3L:
341 case SPRN_IBAT4U ... SPRN_IBAT7L:
342 case SPRN_DBAT0U ... SPRN_DBAT3L:
343 case SPRN_DBAT4U ... SPRN_DBAT7L:
Alexander Grafc1c88e22010-08-02 23:23:04 +0200344 {
345 struct kvmppc_bat *bat = kvmppc_find_bat(vcpu, sprn);
346
347 kvmppc_set_bat(vcpu, bat, !(sprn % 2), (u32)spr_val);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000348 /* BAT writes happen so rarely that we're ok to flush
349 * everything here */
350 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Alexander Grafc04a6952010-03-24 21:48:25 +0100351 kvmppc_mmu_flush_segments(vcpu);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000352 break;
Alexander Grafc1c88e22010-08-02 23:23:04 +0200353 }
Alexander Grafc215c6e2009-10-30 05:47:14 +0000354 case SPRN_HID0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100355 to_book3s(vcpu)->hid[0] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000356 break;
357 case SPRN_HID1:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100358 to_book3s(vcpu)->hid[1] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000359 break;
360 case SPRN_HID2:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100361 to_book3s(vcpu)->hid[2] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000362 break;
Alexander Grafd6d549b2010-02-19 11:00:33 +0100363 case SPRN_HID2_GEKKO:
364 to_book3s(vcpu)->hid[2] = spr_val;
365 /* HID2.PSE controls paired single on gekko */
366 switch (vcpu->arch.pvr) {
367 case 0x00080200: /* lonestar 2.0 */
368 case 0x00088202: /* lonestar 2.2 */
369 case 0x70000100: /* gekko 1.0 */
370 case 0x00080100: /* gekko 2.0 */
371 case 0x00083203: /* gekko 2.3a */
372 case 0x00083213: /* gekko 2.3b */
373 case 0x00083204: /* gekko 2.4 */
374 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
Alexander Grafb83d4a92010-04-20 02:49:54 +0200375 case 0x00087200: /* broadway */
376 if (vcpu->arch.hflags & BOOK3S_HFLAG_NATIVE_PS) {
377 /* Native paired singles */
378 } else if (spr_val & (1 << 29)) { /* HID2.PSE */
Alexander Grafd6d549b2010-02-19 11:00:33 +0100379 vcpu->arch.hflags |= BOOK3S_HFLAG_PAIRED_SINGLE;
380 kvmppc_giveup_ext(vcpu, MSR_FP);
381 } else {
382 vcpu->arch.hflags &= ~BOOK3S_HFLAG_PAIRED_SINGLE;
383 }
384 break;
385 }
386 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000387 case SPRN_HID4:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100388 case SPRN_HID4_GEKKO:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100389 to_book3s(vcpu)->hid[4] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000390 break;
391 case SPRN_HID5:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100392 to_book3s(vcpu)->hid[5] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000393 /* guest HID5 set can change is_dcbz32 */
394 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
395 (mfmsr() & MSR_HV))
396 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
397 break;
Alexander Grafd6d549b2010-02-19 11:00:33 +0100398 case SPRN_GQR0:
399 case SPRN_GQR1:
400 case SPRN_GQR2:
401 case SPRN_GQR3:
402 case SPRN_GQR4:
403 case SPRN_GQR5:
404 case SPRN_GQR6:
405 case SPRN_GQR7:
406 to_book3s(vcpu)->gqr[sprn - SPRN_GQR0] = spr_val;
407 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000408 case SPRN_ICTC:
409 case SPRN_THRM1:
410 case SPRN_THRM2:
411 case SPRN_THRM3:
412 case SPRN_CTRLF:
413 case SPRN_CTRLT:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100414 case SPRN_L2CR:
415 case SPRN_MMCR0_GEKKO:
416 case SPRN_MMCR1_GEKKO:
417 case SPRN_PMC1_GEKKO:
418 case SPRN_PMC2_GEKKO:
419 case SPRN_PMC3_GEKKO:
420 case SPRN_PMC4_GEKKO:
421 case SPRN_WPAR_GEKKO:
Alexander Grafc215c6e2009-10-30 05:47:14 +0000422 break;
Alexander Graf317a8fa2011-08-08 16:07:16 +0200423unprivileged:
Alexander Grafc215c6e2009-10-30 05:47:14 +0000424 default:
425 printk(KERN_INFO "KVM: invalid SPR write: %d\n", sprn);
426#ifndef DEBUG_SPR
427 emulated = EMULATE_FAIL;
428#endif
429 break;
430 }
431
432 return emulated;
433}
434
Alexander Graf54771e62012-05-04 14:55:12 +0200435int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
Alexander Grafc215c6e2009-10-30 05:47:14 +0000436{
437 int emulated = EMULATE_DONE;
438
439 switch (sprn) {
Alexander Grafc04a6952010-03-24 21:48:25 +0100440 case SPRN_IBAT0U ... SPRN_IBAT3L:
441 case SPRN_IBAT4U ... SPRN_IBAT7L:
442 case SPRN_DBAT0U ... SPRN_DBAT3L:
443 case SPRN_DBAT4U ... SPRN_DBAT7L:
Alexander Grafc1c88e22010-08-02 23:23:04 +0200444 {
445 struct kvmppc_bat *bat = kvmppc_find_bat(vcpu, sprn);
446
447 if (sprn % 2)
Alexander Graf54771e62012-05-04 14:55:12 +0200448 *spr_val = bat->raw >> 32;
Alexander Grafc1c88e22010-08-02 23:23:04 +0200449 else
Alexander Graf54771e62012-05-04 14:55:12 +0200450 *spr_val = bat->raw;
Alexander Grafc1c88e22010-08-02 23:23:04 +0200451
Alexander Grafc04a6952010-03-24 21:48:25 +0100452 break;
Alexander Grafc1c88e22010-08-02 23:23:04 +0200453 }
Alexander Grafc215c6e2009-10-30 05:47:14 +0000454 case SPRN_SDR1:
Alexander Graf317a8fa2011-08-08 16:07:16 +0200455 if (!spr_allowed(vcpu, PRIV_HYPER))
456 goto unprivileged;
Alexander Graf54771e62012-05-04 14:55:12 +0200457 *spr_val = to_book3s(vcpu)->sdr1;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000458 break;
459 case SPRN_DSISR:
Alexander Graf54771e62012-05-04 14:55:12 +0200460 *spr_val = vcpu->arch.shared->dsisr;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000461 break;
462 case SPRN_DAR:
Alexander Graf54771e62012-05-04 14:55:12 +0200463 *spr_val = vcpu->arch.shared->dar;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000464 break;
465 case SPRN_HIOR:
Alexander Graf54771e62012-05-04 14:55:12 +0200466 *spr_val = to_book3s(vcpu)->hior;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000467 break;
468 case SPRN_HID0:
Alexander Graf54771e62012-05-04 14:55:12 +0200469 *spr_val = to_book3s(vcpu)->hid[0];
Alexander Grafc215c6e2009-10-30 05:47:14 +0000470 break;
471 case SPRN_HID1:
Alexander Graf54771e62012-05-04 14:55:12 +0200472 *spr_val = to_book3s(vcpu)->hid[1];
Alexander Grafc215c6e2009-10-30 05:47:14 +0000473 break;
474 case SPRN_HID2:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100475 case SPRN_HID2_GEKKO:
Alexander Graf54771e62012-05-04 14:55:12 +0200476 *spr_val = to_book3s(vcpu)->hid[2];
Alexander Grafc215c6e2009-10-30 05:47:14 +0000477 break;
478 case SPRN_HID4:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100479 case SPRN_HID4_GEKKO:
Alexander Graf54771e62012-05-04 14:55:12 +0200480 *spr_val = to_book3s(vcpu)->hid[4];
Alexander Grafc215c6e2009-10-30 05:47:14 +0000481 break;
482 case SPRN_HID5:
Alexander Graf54771e62012-05-04 14:55:12 +0200483 *spr_val = to_book3s(vcpu)->hid[5];
Alexander Grafc215c6e2009-10-30 05:47:14 +0000484 break;
Alexander Grafaacf9aa2011-08-08 17:22:59 +0200485 case SPRN_CFAR:
486 case SPRN_PURR:
Alexander Graf54771e62012-05-04 14:55:12 +0200487 *spr_val = 0;
Alexander Grafaacf9aa2011-08-08 17:22:59 +0200488 break;
Alexander Grafd6d549b2010-02-19 11:00:33 +0100489 case SPRN_GQR0:
490 case SPRN_GQR1:
491 case SPRN_GQR2:
492 case SPRN_GQR3:
493 case SPRN_GQR4:
494 case SPRN_GQR5:
495 case SPRN_GQR6:
496 case SPRN_GQR7:
Alexander Graf54771e62012-05-04 14:55:12 +0200497 *spr_val = to_book3s(vcpu)->gqr[sprn - SPRN_GQR0];
Alexander Grafd6d549b2010-02-19 11:00:33 +0100498 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000499 case SPRN_THRM1:
500 case SPRN_THRM2:
501 case SPRN_THRM3:
502 case SPRN_CTRLF:
503 case SPRN_CTRLT:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100504 case SPRN_L2CR:
505 case SPRN_MMCR0_GEKKO:
506 case SPRN_MMCR1_GEKKO:
507 case SPRN_PMC1_GEKKO:
508 case SPRN_PMC2_GEKKO:
509 case SPRN_PMC3_GEKKO:
510 case SPRN_PMC4_GEKKO:
511 case SPRN_WPAR_GEKKO:
Alexander Graf54771e62012-05-04 14:55:12 +0200512 *spr_val = 0;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000513 break;
514 default:
Alexander Graf317a8fa2011-08-08 16:07:16 +0200515unprivileged:
Alexander Grafc215c6e2009-10-30 05:47:14 +0000516 printk(KERN_INFO "KVM: invalid SPR read: %d\n", sprn);
517#ifndef DEBUG_SPR
518 emulated = EMULATE_FAIL;
519#endif
520 break;
521 }
522
523 return emulated;
524}
525
Alexander Grafca7f4202010-03-24 21:48:28 +0100526u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst)
527{
528 u32 dsisr = 0;
529
530 /*
531 * This is what the spec says about DSISR bits (not mentioned = 0):
532 *
533 * 12:13 [DS] Set to bits 30:31
534 * 15:16 [X] Set to bits 29:30
535 * 17 [X] Set to bit 25
536 * [D/DS] Set to bit 5
537 * 18:21 [X] Set to bits 21:24
538 * [D/DS] Set to bits 1:4
539 * 22:26 Set to bits 6:10 (RT/RS/FRT/FRS)
540 * 27:31 Set to bits 11:15 (RA)
541 */
542
543 switch (get_op(inst)) {
544 /* D-form */
545 case OP_LFS:
546 case OP_LFD:
547 case OP_STFD:
548 case OP_STFS:
549 dsisr |= (inst >> 12) & 0x4000; /* bit 17 */
550 dsisr |= (inst >> 17) & 0x3c00; /* bits 18:21 */
551 break;
552 /* X-form */
553 case 31:
554 dsisr |= (inst << 14) & 0x18000; /* bits 15:16 */
555 dsisr |= (inst << 8) & 0x04000; /* bit 17 */
556 dsisr |= (inst << 3) & 0x03c00; /* bits 18:21 */
557 break;
558 default:
559 printk(KERN_INFO "KVM: Unaligned instruction 0x%x\n", inst);
560 break;
561 }
562
563 dsisr |= (inst >> 16) & 0x03ff; /* bits 22:31 */
564
565 return dsisr;
566}
567
568ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst)
569{
570 ulong dar = 0;
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200571 ulong ra = get_ra(inst);
572 ulong rb = get_rb(inst);
Alexander Grafca7f4202010-03-24 21:48:28 +0100573
574 switch (get_op(inst)) {
575 case OP_LFS:
576 case OP_LFD:
577 case OP_STFD:
578 case OP_STFS:
Alexander Grafca7f4202010-03-24 21:48:28 +0100579 if (ra)
580 dar = kvmppc_get_gpr(vcpu, ra);
581 dar += (s32)((s16)inst);
582 break;
583 case 31:
Alexander Grafca7f4202010-03-24 21:48:28 +0100584 if (ra)
585 dar = kvmppc_get_gpr(vcpu, ra);
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200586 dar += kvmppc_get_gpr(vcpu, rb);
Alexander Grafca7f4202010-03-24 21:48:28 +0100587 break;
588 default:
589 printk(KERN_INFO "KVM: Unaligned instruction 0x%x\n", inst);
590 break;
591 }
592
593 return dar;
594}