blob: 5a2bc4b0dfe5a9b83c4d5be3b820bda90e57a641 [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>
Paul Mackerrasb0a94d42012-11-04 18:15:43 +000025#include <asm/time.h>
Alexander Grafc215c6e2009-10-30 05:47:14 +000026
27#define OP_19_XOP_RFID 18
28#define OP_19_XOP_RFI 50
29
30#define OP_31_XOP_MFMSR 83
31#define OP_31_XOP_MTMSR 146
32#define OP_31_XOP_MTMSRD 178
Alexander Graf71db4082010-02-19 11:00:37 +010033#define OP_31_XOP_MTSR 210
Alexander Grafc215c6e2009-10-30 05:47:14 +000034#define OP_31_XOP_MTSRIN 242
35#define OP_31_XOP_TLBIEL 274
36#define OP_31_XOP_TLBIE 306
Alexander Graf50c7bb82012-12-14 23:42:05 +010037/* Opcode is officially reserved, reuse it as sc 1 when sc 1 doesn't trap */
38#define OP_31_XOP_FAKE_SC1 308
Alexander Grafc215c6e2009-10-30 05:47:14 +000039#define OP_31_XOP_SLBMTE 402
40#define OP_31_XOP_SLBIE 434
41#define OP_31_XOP_SLBIA 498
Alexander Grafc6648762010-03-24 21:48:24 +010042#define OP_31_XOP_MFSR 595
Alexander Grafc215c6e2009-10-30 05:47:14 +000043#define OP_31_XOP_MFSRIN 659
Alexander Grafbd7cdbb2010-03-24 21:48:33 +010044#define OP_31_XOP_DCBA 758
Alexander Grafc215c6e2009-10-30 05:47:14 +000045#define OP_31_XOP_SLBMFEV 851
46#define OP_31_XOP_EIOIO 854
47#define OP_31_XOP_SLBMFEE 915
48
49/* DCBZ is actually 1014, but we patch it to 1010 so we get a trap */
50#define OP_31_XOP_DCBZ 1010
51
Alexander Grafca7f4202010-03-24 21:48:28 +010052#define OP_LFS 48
53#define OP_LFD 50
54#define OP_STFS 52
55#define OP_STFD 54
56
Alexander Grafd6d549b2010-02-19 11:00:33 +010057#define SPRN_GQR0 912
58#define SPRN_GQR1 913
59#define SPRN_GQR2 914
60#define SPRN_GQR3 915
61#define SPRN_GQR4 916
62#define SPRN_GQR5 917
63#define SPRN_GQR6 918
64#define SPRN_GQR7 919
65
Alexander Graf07b09072010-04-16 00:11:53 +020066/* Book3S_32 defines mfsrin(v) - but that messes up our abstract
67 * function pointers, so let's just disable the define. */
68#undef mfsrin
69
Alexander Graf317a8fa2011-08-08 16:07:16 +020070enum priv_level {
71 PRIV_PROBLEM = 0,
72 PRIV_SUPER = 1,
73 PRIV_HYPER = 2,
74};
75
76static bool spr_allowed(struct kvm_vcpu *vcpu, enum priv_level level)
77{
78 /* PAPR VMs only access supervisor SPRs */
79 if (vcpu->arch.papr_enabled && (level > PRIV_SUPER))
80 return false;
81
82 /* Limit user space to its own small SPR set */
Alexander Graf5deb8e72014-04-24 13:46:24 +020083 if ((kvmppc_get_msr(vcpu) & MSR_PR) && level > PRIV_PROBLEM)
Alexander Graf317a8fa2011-08-08 16:07:16 +020084 return false;
85
86 return true;
87}
88
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053089int kvmppc_core_emulate_op_pr(struct kvm_run *run, struct kvm_vcpu *vcpu,
90 unsigned int inst, int *advance)
Alexander Grafc215c6e2009-10-30 05:47:14 +000091{
92 int emulated = EMULATE_DONE;
Alexander Grafc46dc9a2012-05-04 14:01:33 +020093 int rt = get_rt(inst);
94 int rs = get_rs(inst);
95 int ra = get_ra(inst);
96 int rb = get_rb(inst);
Alexander Graf42188362014-05-13 17:05:51 +020097 u32 inst_sc = 0x44000002;
Alexander Grafc215c6e2009-10-30 05:47:14 +000098
99 switch (get_op(inst)) {
Alexander Graf42188362014-05-13 17:05:51 +0200100 case 0:
101 emulated = EMULATE_FAIL;
102 if ((kvmppc_get_msr(vcpu) & MSR_LE) &&
103 (inst == swab32(inst_sc))) {
104 /*
105 * This is the byte reversed syscall instruction of our
106 * hypercall handler. Early versions of LE Linux didn't
107 * swap the instructions correctly and ended up in
108 * illegal instructions.
109 * Just always fail hypercalls on these broken systems.
110 */
111 kvmppc_set_gpr(vcpu, 3, EV_UNIMPLEMENTED);
112 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
113 emulated = EMULATE_DONE;
114 }
115 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000116 case 19:
117 switch (get_xop(inst)) {
118 case OP_19_XOP_RFID:
119 case OP_19_XOP_RFI:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200120 kvmppc_set_pc(vcpu, kvmppc_get_srr0(vcpu));
121 kvmppc_set_msr(vcpu, kvmppc_get_srr1(vcpu));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000122 *advance = 0;
123 break;
124
125 default:
126 emulated = EMULATE_FAIL;
127 break;
128 }
129 break;
130 case 31:
131 switch (get_xop(inst)) {
132 case OP_31_XOP_MFMSR:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200133 kvmppc_set_gpr(vcpu, rt, kvmppc_get_msr(vcpu));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000134 break;
135 case OP_31_XOP_MTMSRD:
136 {
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200137 ulong rs_val = kvmppc_get_gpr(vcpu, rs);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000138 if (inst & 0x10000) {
Alexander Graf5deb8e72014-04-24 13:46:24 +0200139 ulong new_msr = kvmppc_get_msr(vcpu);
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200140 new_msr &= ~(MSR_RI | MSR_EE);
141 new_msr |= rs_val & (MSR_RI | MSR_EE);
Alexander Graf5deb8e72014-04-24 13:46:24 +0200142 kvmppc_set_msr_fast(vcpu, new_msr);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000143 } else
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200144 kvmppc_set_msr(vcpu, rs_val);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000145 break;
146 }
147 case OP_31_XOP_MTMSR:
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200148 kvmppc_set_msr(vcpu, kvmppc_get_gpr(vcpu, rs));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000149 break;
Alexander Grafc6648762010-03-24 21:48:24 +0100150 case OP_31_XOP_MFSR:
151 {
152 int srnum;
153
154 srnum = kvmppc_get_field(inst, 12 + 32, 15 + 32);
155 if (vcpu->arch.mmu.mfsrin) {
156 u32 sr;
157 sr = vcpu->arch.mmu.mfsrin(vcpu, srnum);
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200158 kvmppc_set_gpr(vcpu, rt, sr);
Alexander Grafc6648762010-03-24 21:48:24 +0100159 }
160 break;
161 }
Alexander Grafc215c6e2009-10-30 05:47:14 +0000162 case OP_31_XOP_MFSRIN:
163 {
164 int srnum;
165
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200166 srnum = (kvmppc_get_gpr(vcpu, rb) >> 28) & 0xf;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000167 if (vcpu->arch.mmu.mfsrin) {
168 u32 sr;
169 sr = vcpu->arch.mmu.mfsrin(vcpu, srnum);
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200170 kvmppc_set_gpr(vcpu, rt, sr);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000171 }
172 break;
173 }
Alexander Graf71db4082010-02-19 11:00:37 +0100174 case OP_31_XOP_MTSR:
175 vcpu->arch.mmu.mtsrin(vcpu,
176 (inst >> 16) & 0xf,
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200177 kvmppc_get_gpr(vcpu, rs));
Alexander Graf71db4082010-02-19 11:00:37 +0100178 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000179 case OP_31_XOP_MTSRIN:
180 vcpu->arch.mmu.mtsrin(vcpu,
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200181 (kvmppc_get_gpr(vcpu, rb) >> 28) & 0xf,
182 kvmppc_get_gpr(vcpu, rs));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000183 break;
184 case OP_31_XOP_TLBIE:
185 case OP_31_XOP_TLBIEL:
186 {
187 bool large = (inst & 0x00200000) ? true : false;
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200188 ulong addr = kvmppc_get_gpr(vcpu, rb);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000189 vcpu->arch.mmu.tlbie(vcpu, addr, large);
190 break;
191 }
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +0530192#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf50c7bb82012-12-14 23:42:05 +0100193 case OP_31_XOP_FAKE_SC1:
194 {
195 /* SC 1 papr hypercalls */
196 ulong cmd = kvmppc_get_gpr(vcpu, 3);
197 int i;
198
Alexander Graf5deb8e72014-04-24 13:46:24 +0200199 if ((kvmppc_get_msr(vcpu) & MSR_PR) ||
Alexander Graf50c7bb82012-12-14 23:42:05 +0100200 !vcpu->arch.papr_enabled) {
201 emulated = EMULATE_FAIL;
202 break;
203 }
204
205 if (kvmppc_h_pr(vcpu, cmd) == EMULATE_DONE)
206 break;
207
208 run->papr_hcall.nr = cmd;
209 for (i = 0; i < 9; ++i) {
210 ulong gpr = kvmppc_get_gpr(vcpu, 4 + i);
211 run->papr_hcall.args[i] = gpr;
212 }
213
Bharat Bhushan0f47f9b2013-04-08 00:32:14 +0000214 run->exit_reason = KVM_EXIT_PAPR_HCALL;
215 vcpu->arch.hcall_needed = 1;
Bharat Bhushanc402a3f2013-04-08 00:32:13 +0000216 emulated = EMULATE_EXIT_USER;
Alexander Graf50c7bb82012-12-14 23:42:05 +0100217 break;
218 }
219#endif
Alexander Grafc215c6e2009-10-30 05:47:14 +0000220 case OP_31_XOP_EIOIO:
221 break;
222 case OP_31_XOP_SLBMTE:
223 if (!vcpu->arch.mmu.slbmte)
224 return EMULATE_FAIL;
225
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100226 vcpu->arch.mmu.slbmte(vcpu,
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200227 kvmppc_get_gpr(vcpu, rs),
228 kvmppc_get_gpr(vcpu, rb));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000229 break;
230 case OP_31_XOP_SLBIE:
231 if (!vcpu->arch.mmu.slbie)
232 return EMULATE_FAIL;
233
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100234 vcpu->arch.mmu.slbie(vcpu,
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200235 kvmppc_get_gpr(vcpu, rb));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000236 break;
237 case OP_31_XOP_SLBIA:
238 if (!vcpu->arch.mmu.slbia)
239 return EMULATE_FAIL;
240
241 vcpu->arch.mmu.slbia(vcpu);
242 break;
243 case OP_31_XOP_SLBMFEE:
244 if (!vcpu->arch.mmu.slbmfee) {
245 emulated = EMULATE_FAIL;
246 } else {
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200247 ulong t, rb_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000248
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200249 rb_val = kvmppc_get_gpr(vcpu, rb);
250 t = vcpu->arch.mmu.slbmfee(vcpu, rb_val);
251 kvmppc_set_gpr(vcpu, rt, t);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000252 }
253 break;
254 case OP_31_XOP_SLBMFEV:
255 if (!vcpu->arch.mmu.slbmfev) {
256 emulated = EMULATE_FAIL;
257 } else {
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200258 ulong t, rb_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000259
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200260 rb_val = kvmppc_get_gpr(vcpu, rb);
261 t = vcpu->arch.mmu.slbmfev(vcpu, rb_val);
262 kvmppc_set_gpr(vcpu, rt, t);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000263 }
264 break;
Alexander Grafbd7cdbb2010-03-24 21:48:33 +0100265 case OP_31_XOP_DCBA:
266 /* Gets treated as NOP */
267 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000268 case OP_31_XOP_DCBZ:
269 {
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200270 ulong rb_val = kvmppc_get_gpr(vcpu, rb);
271 ulong ra_val = 0;
Alexander Graf5467a972010-02-19 11:00:38 +0100272 ulong addr, vaddr;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000273 u32 zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Alexander Graf9fb244a2010-03-24 21:48:32 +0100274 u32 dsisr;
275 int r;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000276
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200277 if (ra)
278 ra_val = kvmppc_get_gpr(vcpu, ra);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000279
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200280 addr = (ra_val + rb_val) & ~31ULL;
Alexander Graf5deb8e72014-04-24 13:46:24 +0200281 if (!(kvmppc_get_msr(vcpu) & MSR_SF))
Alexander Grafc215c6e2009-10-30 05:47:14 +0000282 addr &= 0xffffffff;
Alexander Graf5467a972010-02-19 11:00:38 +0100283 vaddr = addr;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000284
Alexander Graf9fb244a2010-03-24 21:48:32 +0100285 r = kvmppc_st(vcpu, &addr, 32, zeros, true);
286 if ((r == -ENOENT) || (r == -EPERM)) {
287 *advance = 0;
Alexander Graf5deb8e72014-04-24 13:46:24 +0200288 kvmppc_set_dar(vcpu, vaddr);
Paul Mackerrasa2d56022013-09-20 14:52:43 +1000289 vcpu->arch.fault_dar = vaddr;
Alexander Graf9fb244a2010-03-24 21:48:32 +0100290
291 dsisr = DSISR_ISSTORE;
292 if (r == -ENOENT)
293 dsisr |= DSISR_NOHPTE;
294 else if (r == -EPERM)
295 dsisr |= DSISR_PROTFAULT;
296
Alexander Graf5deb8e72014-04-24 13:46:24 +0200297 kvmppc_set_dsisr(vcpu, dsisr);
Paul Mackerrasa2d56022013-09-20 14:52:43 +1000298 vcpu->arch.fault_dsisr = dsisr;
Alexander Graf9fb244a2010-03-24 21:48:32 +0100299
Alexander Grafc215c6e2009-10-30 05:47:14 +0000300 kvmppc_book3s_queue_irqprio(vcpu,
301 BOOK3S_INTERRUPT_DATA_STORAGE);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000302 }
303
304 break;
305 }
306 default:
307 emulated = EMULATE_FAIL;
308 }
309 break;
310 default:
311 emulated = EMULATE_FAIL;
312 }
313
Alexander Graf831317b2010-02-19 11:00:44 +0100314 if (emulated == EMULATE_FAIL)
315 emulated = kvmppc_emulate_paired_single(run, vcpu);
316
Alexander Grafc215c6e2009-10-30 05:47:14 +0000317 return emulated;
318}
319
Alexander Grafe15a1132009-11-30 03:02:02 +0000320void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat, bool upper,
321 u32 val)
322{
323 if (upper) {
324 /* Upper BAT */
325 u32 bl = (val >> 2) & 0x7ff;
326 bat->bepi_mask = (~bl << 17);
327 bat->bepi = val & 0xfffe0000;
328 bat->vs = (val & 2) ? 1 : 0;
329 bat->vp = (val & 1) ? 1 : 0;
330 bat->raw = (bat->raw & 0xffffffff00000000ULL) | val;
331 } else {
332 /* Lower BAT */
333 bat->brpn = val & 0xfffe0000;
334 bat->wimg = (val >> 3) & 0xf;
335 bat->pp = val & 3;
336 bat->raw = (bat->raw & 0x00000000ffffffffULL) | ((u64)val << 32);
337 }
338}
339
Alexander Grafc1c88e22010-08-02 23:23:04 +0200340static struct kvmppc_bat *kvmppc_find_bat(struct kvm_vcpu *vcpu, int sprn)
Alexander Grafc04a6952010-03-24 21:48:25 +0100341{
342 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
343 struct kvmppc_bat *bat;
344
345 switch (sprn) {
346 case SPRN_IBAT0U ... SPRN_IBAT3L:
347 bat = &vcpu_book3s->ibat[(sprn - SPRN_IBAT0U) / 2];
348 break;
349 case SPRN_IBAT4U ... SPRN_IBAT7L:
350 bat = &vcpu_book3s->ibat[4 + ((sprn - SPRN_IBAT4U) / 2)];
351 break;
352 case SPRN_DBAT0U ... SPRN_DBAT3L:
353 bat = &vcpu_book3s->dbat[(sprn - SPRN_DBAT0U) / 2];
354 break;
355 case SPRN_DBAT4U ... SPRN_DBAT7L:
356 bat = &vcpu_book3s->dbat[4 + ((sprn - SPRN_DBAT4U) / 2)];
357 break;
358 default:
359 BUG();
360 }
361
Alexander Grafc1c88e22010-08-02 23:23:04 +0200362 return bat;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000363}
364
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530365int kvmppc_core_emulate_mtspr_pr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
Alexander Grafc215c6e2009-10-30 05:47:14 +0000366{
367 int emulated = EMULATE_DONE;
368
369 switch (sprn) {
370 case SPRN_SDR1:
Alexander Graf317a8fa2011-08-08 16:07:16 +0200371 if (!spr_allowed(vcpu, PRIV_HYPER))
372 goto unprivileged;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100373 to_book3s(vcpu)->sdr1 = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000374 break;
375 case SPRN_DSISR:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200376 kvmppc_set_dsisr(vcpu, spr_val);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000377 break;
378 case SPRN_DAR:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200379 kvmppc_set_dar(vcpu, spr_val);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000380 break;
381 case SPRN_HIOR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100382 to_book3s(vcpu)->hior = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000383 break;
384 case SPRN_IBAT0U ... SPRN_IBAT3L:
385 case SPRN_IBAT4U ... SPRN_IBAT7L:
386 case SPRN_DBAT0U ... SPRN_DBAT3L:
387 case SPRN_DBAT4U ... SPRN_DBAT7L:
Alexander Grafc1c88e22010-08-02 23:23:04 +0200388 {
389 struct kvmppc_bat *bat = kvmppc_find_bat(vcpu, sprn);
390
391 kvmppc_set_bat(vcpu, bat, !(sprn % 2), (u32)spr_val);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000392 /* BAT writes happen so rarely that we're ok to flush
393 * everything here */
394 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Alexander Grafc04a6952010-03-24 21:48:25 +0100395 kvmppc_mmu_flush_segments(vcpu);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000396 break;
Alexander Grafc1c88e22010-08-02 23:23:04 +0200397 }
Alexander Grafc215c6e2009-10-30 05:47:14 +0000398 case SPRN_HID0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100399 to_book3s(vcpu)->hid[0] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000400 break;
401 case SPRN_HID1:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100402 to_book3s(vcpu)->hid[1] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000403 break;
404 case SPRN_HID2:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100405 to_book3s(vcpu)->hid[2] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000406 break;
Alexander Grafd6d549b2010-02-19 11:00:33 +0100407 case SPRN_HID2_GEKKO:
408 to_book3s(vcpu)->hid[2] = spr_val;
409 /* HID2.PSE controls paired single on gekko */
410 switch (vcpu->arch.pvr) {
411 case 0x00080200: /* lonestar 2.0 */
412 case 0x00088202: /* lonestar 2.2 */
413 case 0x70000100: /* gekko 1.0 */
414 case 0x00080100: /* gekko 2.0 */
415 case 0x00083203: /* gekko 2.3a */
416 case 0x00083213: /* gekko 2.3b */
417 case 0x00083204: /* gekko 2.4 */
418 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
Alexander Grafb83d4a92010-04-20 02:49:54 +0200419 case 0x00087200: /* broadway */
420 if (vcpu->arch.hflags & BOOK3S_HFLAG_NATIVE_PS) {
421 /* Native paired singles */
422 } else if (spr_val & (1 << 29)) { /* HID2.PSE */
Alexander Grafd6d549b2010-02-19 11:00:33 +0100423 vcpu->arch.hflags |= BOOK3S_HFLAG_PAIRED_SINGLE;
424 kvmppc_giveup_ext(vcpu, MSR_FP);
425 } else {
426 vcpu->arch.hflags &= ~BOOK3S_HFLAG_PAIRED_SINGLE;
427 }
428 break;
429 }
430 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000431 case SPRN_HID4:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100432 case SPRN_HID4_GEKKO:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100433 to_book3s(vcpu)->hid[4] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000434 break;
435 case SPRN_HID5:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100436 to_book3s(vcpu)->hid[5] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000437 /* guest HID5 set can change is_dcbz32 */
438 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
439 (mfmsr() & MSR_HV))
440 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
441 break;
Alexander Grafd6d549b2010-02-19 11:00:33 +0100442 case SPRN_GQR0:
443 case SPRN_GQR1:
444 case SPRN_GQR2:
445 case SPRN_GQR3:
446 case SPRN_GQR4:
447 case SPRN_GQR5:
448 case SPRN_GQR6:
449 case SPRN_GQR7:
450 to_book3s(vcpu)->gqr[sprn - SPRN_GQR0] = spr_val;
451 break;
Alexander Graf2e23f542014-04-29 13:36:21 +0200452#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf8e6afa32014-07-31 10:21:59 +0200453 case SPRN_FSCR:
454 kvmppc_set_fscr(vcpu, spr_val);
455 break;
Alexander Graf2e23f542014-04-29 13:36:21 +0200456 case SPRN_BESCR:
457 vcpu->arch.bescr = spr_val;
458 break;
459 case SPRN_EBBHR:
460 vcpu->arch.ebbhr = spr_val;
461 break;
462 case SPRN_EBBRR:
463 vcpu->arch.ebbrr = spr_val;
464 break;
Alexander Graf9916d572014-04-29 17:54:40 +0200465#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
466 case SPRN_TFHAR:
467 vcpu->arch.tfhar = spr_val;
468 break;
469 case SPRN_TEXASR:
470 vcpu->arch.texasr = spr_val;
471 break;
472 case SPRN_TFIAR:
473 vcpu->arch.tfiar = spr_val;
474 break;
475#endif
Alexander Graf2e23f542014-04-29 13:36:21 +0200476#endif
Alexander Grafc215c6e2009-10-30 05:47:14 +0000477 case SPRN_ICTC:
478 case SPRN_THRM1:
479 case SPRN_THRM2:
480 case SPRN_THRM3:
481 case SPRN_CTRLF:
482 case SPRN_CTRLT:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100483 case SPRN_L2CR:
Paul Mackerrasb0a94d42012-11-04 18:15:43 +0000484 case SPRN_DSCR:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100485 case SPRN_MMCR0_GEKKO:
486 case SPRN_MMCR1_GEKKO:
487 case SPRN_PMC1_GEKKO:
488 case SPRN_PMC2_GEKKO:
489 case SPRN_PMC3_GEKKO:
490 case SPRN_PMC4_GEKKO:
491 case SPRN_WPAR_GEKKO:
Mihai Caramanf2be6552012-12-20 04:52:39 +0000492 case SPRN_MSSSR0:
Alexander Graff3532022013-07-02 16:15:10 +0200493 case SPRN_DABR:
Alexander Graff8f6eb02014-04-22 12:41:06 +0200494#ifdef CONFIG_PPC_BOOK3S_64
495 case SPRN_MMCRS:
496 case SPRN_MMCRA:
497 case SPRN_MMCR0:
498 case SPRN_MMCR1:
499 case SPRN_MMCR2:
500#endif
Alexander Grafc215c6e2009-10-30 05:47:14 +0000501 break;
Alexander Graf317a8fa2011-08-08 16:07:16 +0200502unprivileged:
Alexander Grafc215c6e2009-10-30 05:47:14 +0000503 default:
504 printk(KERN_INFO "KVM: invalid SPR write: %d\n", sprn);
505#ifndef DEBUG_SPR
506 emulated = EMULATE_FAIL;
507#endif
508 break;
509 }
510
511 return emulated;
512}
513
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530514int kvmppc_core_emulate_mfspr_pr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
Alexander Grafc215c6e2009-10-30 05:47:14 +0000515{
516 int emulated = EMULATE_DONE;
517
518 switch (sprn) {
Alexander Grafc04a6952010-03-24 21:48:25 +0100519 case SPRN_IBAT0U ... SPRN_IBAT3L:
520 case SPRN_IBAT4U ... SPRN_IBAT7L:
521 case SPRN_DBAT0U ... SPRN_DBAT3L:
522 case SPRN_DBAT4U ... SPRN_DBAT7L:
Alexander Grafc1c88e22010-08-02 23:23:04 +0200523 {
524 struct kvmppc_bat *bat = kvmppc_find_bat(vcpu, sprn);
525
526 if (sprn % 2)
Alexander Graf54771e62012-05-04 14:55:12 +0200527 *spr_val = bat->raw >> 32;
Alexander Grafc1c88e22010-08-02 23:23:04 +0200528 else
Alexander Graf54771e62012-05-04 14:55:12 +0200529 *spr_val = bat->raw;
Alexander Grafc1c88e22010-08-02 23:23:04 +0200530
Alexander Grafc04a6952010-03-24 21:48:25 +0100531 break;
Alexander Grafc1c88e22010-08-02 23:23:04 +0200532 }
Alexander Grafc215c6e2009-10-30 05:47:14 +0000533 case SPRN_SDR1:
Alexander Graf317a8fa2011-08-08 16:07:16 +0200534 if (!spr_allowed(vcpu, PRIV_HYPER))
535 goto unprivileged;
Alexander Graf54771e62012-05-04 14:55:12 +0200536 *spr_val = to_book3s(vcpu)->sdr1;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000537 break;
538 case SPRN_DSISR:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200539 *spr_val = kvmppc_get_dsisr(vcpu);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000540 break;
541 case SPRN_DAR:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200542 *spr_val = kvmppc_get_dar(vcpu);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000543 break;
544 case SPRN_HIOR:
Alexander Graf54771e62012-05-04 14:55:12 +0200545 *spr_val = to_book3s(vcpu)->hior;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000546 break;
547 case SPRN_HID0:
Alexander Graf54771e62012-05-04 14:55:12 +0200548 *spr_val = to_book3s(vcpu)->hid[0];
Alexander Grafc215c6e2009-10-30 05:47:14 +0000549 break;
550 case SPRN_HID1:
Alexander Graf54771e62012-05-04 14:55:12 +0200551 *spr_val = to_book3s(vcpu)->hid[1];
Alexander Grafc215c6e2009-10-30 05:47:14 +0000552 break;
553 case SPRN_HID2:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100554 case SPRN_HID2_GEKKO:
Alexander Graf54771e62012-05-04 14:55:12 +0200555 *spr_val = to_book3s(vcpu)->hid[2];
Alexander Grafc215c6e2009-10-30 05:47:14 +0000556 break;
557 case SPRN_HID4:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100558 case SPRN_HID4_GEKKO:
Alexander Graf54771e62012-05-04 14:55:12 +0200559 *spr_val = to_book3s(vcpu)->hid[4];
Alexander Grafc215c6e2009-10-30 05:47:14 +0000560 break;
561 case SPRN_HID5:
Alexander Graf54771e62012-05-04 14:55:12 +0200562 *spr_val = to_book3s(vcpu)->hid[5];
Alexander Grafc215c6e2009-10-30 05:47:14 +0000563 break;
Alexander Grafaacf9aa2011-08-08 17:22:59 +0200564 case SPRN_CFAR:
Paul Mackerrasb0a94d42012-11-04 18:15:43 +0000565 case SPRN_DSCR:
Alexander Graf54771e62012-05-04 14:55:12 +0200566 *spr_val = 0;
Alexander Grafaacf9aa2011-08-08 17:22:59 +0200567 break;
Paul Mackerrasb0a94d42012-11-04 18:15:43 +0000568 case SPRN_PURR:
Aneesh Kumar K.V3cd60e32014-06-04 16:47:55 +0530569 /*
570 * On exit we would have updated purr
571 */
572 *spr_val = vcpu->arch.purr;
Paul Mackerrasb0a94d42012-11-04 18:15:43 +0000573 break;
574 case SPRN_SPURR:
Aneesh Kumar K.V3cd60e32014-06-04 16:47:55 +0530575 /*
576 * On exit we would have updated spurr
577 */
578 *spr_val = vcpu->arch.spurr;
Paul Mackerrasb0a94d42012-11-04 18:15:43 +0000579 break;
Aneesh Kumar K.V8f42ab22014-06-05 17:38:02 +0530580 case SPRN_VTB:
581 *spr_val = vcpu->arch.vtb;
582 break;
Aneesh Kumar K.V06da28e2014-06-05 17:38:05 +0530583 case SPRN_IC:
584 *spr_val = vcpu->arch.ic;
585 break;
Alexander Grafd6d549b2010-02-19 11:00:33 +0100586 case SPRN_GQR0:
587 case SPRN_GQR1:
588 case SPRN_GQR2:
589 case SPRN_GQR3:
590 case SPRN_GQR4:
591 case SPRN_GQR5:
592 case SPRN_GQR6:
593 case SPRN_GQR7:
Alexander Graf54771e62012-05-04 14:55:12 +0200594 *spr_val = to_book3s(vcpu)->gqr[sprn - SPRN_GQR0];
Alexander Grafd6d549b2010-02-19 11:00:33 +0100595 break;
Alexander Graf8e6afa32014-07-31 10:21:59 +0200596#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf616dff82014-04-29 16:48:44 +0200597 case SPRN_FSCR:
598 *spr_val = vcpu->arch.fscr;
599 break;
Alexander Graf2e23f542014-04-29 13:36:21 +0200600 case SPRN_BESCR:
601 *spr_val = vcpu->arch.bescr;
602 break;
603 case SPRN_EBBHR:
604 *spr_val = vcpu->arch.ebbhr;
605 break;
606 case SPRN_EBBRR:
607 *spr_val = vcpu->arch.ebbrr;
608 break;
Alexander Graf9916d572014-04-29 17:54:40 +0200609#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
610 case SPRN_TFHAR:
611 *spr_val = vcpu->arch.tfhar;
612 break;
613 case SPRN_TEXASR:
614 *spr_val = vcpu->arch.texasr;
615 break;
616 case SPRN_TFIAR:
617 *spr_val = vcpu->arch.tfiar;
618 break;
619#endif
Alexander Graf2e23f542014-04-29 13:36:21 +0200620#endif
Alexander Grafc215c6e2009-10-30 05:47:14 +0000621 case SPRN_THRM1:
622 case SPRN_THRM2:
623 case SPRN_THRM3:
624 case SPRN_CTRLF:
625 case SPRN_CTRLT:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100626 case SPRN_L2CR:
627 case SPRN_MMCR0_GEKKO:
628 case SPRN_MMCR1_GEKKO:
629 case SPRN_PMC1_GEKKO:
630 case SPRN_PMC2_GEKKO:
631 case SPRN_PMC3_GEKKO:
632 case SPRN_PMC4_GEKKO:
633 case SPRN_WPAR_GEKKO:
Mihai Caramanf2be6552012-12-20 04:52:39 +0000634 case SPRN_MSSSR0:
Alexander Graff3532022013-07-02 16:15:10 +0200635 case SPRN_DABR:
Alexander Graff8f6eb02014-04-22 12:41:06 +0200636#ifdef CONFIG_PPC_BOOK3S_64
637 case SPRN_MMCRS:
638 case SPRN_MMCRA:
639 case SPRN_MMCR0:
640 case SPRN_MMCR1:
641 case SPRN_MMCR2:
Alexander Grafa5948fa2014-04-25 16:07:21 +0200642 case SPRN_TIR:
Alexander Graff8f6eb02014-04-22 12:41:06 +0200643#endif
Alexander Graf54771e62012-05-04 14:55:12 +0200644 *spr_val = 0;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000645 break;
646 default:
Alexander Graf317a8fa2011-08-08 16:07:16 +0200647unprivileged:
Alexander Grafc215c6e2009-10-30 05:47:14 +0000648 printk(KERN_INFO "KVM: invalid SPR read: %d\n", sprn);
649#ifndef DEBUG_SPR
650 emulated = EMULATE_FAIL;
651#endif
652 break;
653 }
654
655 return emulated;
656}
657
Alexander Grafca7f4202010-03-24 21:48:28 +0100658u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst)
659{
Aneesh Kumar K.Vddca1562014-05-12 17:04:06 +0530660 return make_dsisr(inst);
Alexander Grafca7f4202010-03-24 21:48:28 +0100661}
662
663ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst)
664{
Aneesh Kumar K.V7310f3a2014-05-12 17:04:05 +0530665#ifdef CONFIG_PPC_BOOK3S_64
666 /*
667 * Linux's fix_alignment() assumes that DAR is valid, so can we
668 */
669 return vcpu->arch.fault_dar;
670#else
Alexander Grafca7f4202010-03-24 21:48:28 +0100671 ulong dar = 0;
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200672 ulong ra = get_ra(inst);
673 ulong rb = get_rb(inst);
Alexander Grafca7f4202010-03-24 21:48:28 +0100674
675 switch (get_op(inst)) {
676 case OP_LFS:
677 case OP_LFD:
678 case OP_STFD:
679 case OP_STFS:
Alexander Grafca7f4202010-03-24 21:48:28 +0100680 if (ra)
681 dar = kvmppc_get_gpr(vcpu, ra);
682 dar += (s32)((s16)inst);
683 break;
684 case 31:
Alexander Grafca7f4202010-03-24 21:48:28 +0100685 if (ra)
686 dar = kvmppc_get_gpr(vcpu, ra);
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200687 dar += kvmppc_get_gpr(vcpu, rb);
Alexander Grafca7f4202010-03-24 21:48:28 +0100688 break;
689 default:
690 printk(KERN_INFO "KVM: Unaligned instruction 0x%x\n", inst);
691 break;
692 }
693
694 return dar;
Aneesh Kumar K.V7310f3a2014-05-12 17:04:05 +0530695#endif
Alexander Grafca7f4202010-03-24 21:48:28 +0100696}