blob: 2eb457bc7b6eef655c4227696e81905aa649c93c [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>
Thomas Huth5358a962015-05-22 09:25:02 +020026#include "book3s.h"
Alexander Grafc215c6e2009-10-30 05:47:14 +000027
28#define OP_19_XOP_RFID 18
29#define OP_19_XOP_RFI 50
30
31#define OP_31_XOP_MFMSR 83
32#define OP_31_XOP_MTMSR 146
33#define OP_31_XOP_MTMSRD 178
Alexander Graf71db4082010-02-19 11:00:37 +010034#define OP_31_XOP_MTSR 210
Alexander Grafc215c6e2009-10-30 05:47:14 +000035#define OP_31_XOP_MTSRIN 242
36#define OP_31_XOP_TLBIEL 274
37#define OP_31_XOP_TLBIE 306
Alexander Graf50c7bb82012-12-14 23:42:05 +010038/* Opcode is officially reserved, reuse it as sc 1 when sc 1 doesn't trap */
39#define OP_31_XOP_FAKE_SC1 308
Alexander Grafc215c6e2009-10-30 05:47:14 +000040#define OP_31_XOP_SLBMTE 402
41#define OP_31_XOP_SLBIE 434
42#define OP_31_XOP_SLBIA 498
Alexander Grafc6648762010-03-24 21:48:24 +010043#define OP_31_XOP_MFSR 595
Alexander Grafc215c6e2009-10-30 05:47:14 +000044#define OP_31_XOP_MFSRIN 659
Alexander Grafbd7cdbb2010-03-24 21:48:33 +010045#define OP_31_XOP_DCBA 758
Alexander Grafc215c6e2009-10-30 05:47:14 +000046#define OP_31_XOP_SLBMFEV 851
47#define OP_31_XOP_EIOIO 854
48#define OP_31_XOP_SLBMFEE 915
49
50/* DCBZ is actually 1014, but we patch it to 1010 so we get a trap */
51#define OP_31_XOP_DCBZ 1010
52
Alexander Grafca7f4202010-03-24 21:48:28 +010053#define OP_LFS 48
54#define OP_LFD 50
55#define OP_STFS 52
56#define OP_STFD 54
57
Alexander Grafd6d549b2010-02-19 11:00:33 +010058#define SPRN_GQR0 912
59#define SPRN_GQR1 913
60#define SPRN_GQR2 914
61#define SPRN_GQR3 915
62#define SPRN_GQR4 916
63#define SPRN_GQR5 917
64#define SPRN_GQR6 918
65#define SPRN_GQR7 919
66
Alexander Graf07b09072010-04-16 00:11:53 +020067/* Book3S_32 defines mfsrin(v) - but that messes up our abstract
68 * function pointers, so let's just disable the define. */
69#undef mfsrin
70
Alexander Graf317a8fa2011-08-08 16:07:16 +020071enum priv_level {
72 PRIV_PROBLEM = 0,
73 PRIV_SUPER = 1,
74 PRIV_HYPER = 2,
75};
76
77static bool spr_allowed(struct kvm_vcpu *vcpu, enum priv_level level)
78{
79 /* PAPR VMs only access supervisor SPRs */
80 if (vcpu->arch.papr_enabled && (level > PRIV_SUPER))
81 return false;
82
83 /* Limit user space to its own small SPR set */
Alexander Graf5deb8e72014-04-24 13:46:24 +020084 if ((kvmppc_get_msr(vcpu) & MSR_PR) && level > PRIV_PROBLEM)
Alexander Graf317a8fa2011-08-08 16:07:16 +020085 return false;
86
87 return true;
88}
89
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053090int kvmppc_core_emulate_op_pr(struct kvm_run *run, struct kvm_vcpu *vcpu,
91 unsigned int inst, int *advance)
Alexander Grafc215c6e2009-10-30 05:47:14 +000092{
93 int emulated = EMULATE_DONE;
Alexander Grafc46dc9a2012-05-04 14:01:33 +020094 int rt = get_rt(inst);
95 int rs = get_rs(inst);
96 int ra = get_ra(inst);
97 int rb = get_rb(inst);
Alexander Graf42188362014-05-13 17:05:51 +020098 u32 inst_sc = 0x44000002;
Alexander Grafc215c6e2009-10-30 05:47:14 +000099
100 switch (get_op(inst)) {
Alexander Graf42188362014-05-13 17:05:51 +0200101 case 0:
102 emulated = EMULATE_FAIL;
103 if ((kvmppc_get_msr(vcpu) & MSR_LE) &&
104 (inst == swab32(inst_sc))) {
105 /*
106 * This is the byte reversed syscall instruction of our
107 * hypercall handler. Early versions of LE Linux didn't
108 * swap the instructions correctly and ended up in
109 * illegal instructions.
110 * Just always fail hypercalls on these broken systems.
111 */
112 kvmppc_set_gpr(vcpu, 3, EV_UNIMPLEMENTED);
113 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
114 emulated = EMULATE_DONE;
115 }
116 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000117 case 19:
118 switch (get_xop(inst)) {
119 case OP_19_XOP_RFID:
Simon Guo401a89e2018-05-23 15:01:54 +0800120 case OP_19_XOP_RFI: {
121 unsigned long srr1 = kvmppc_get_srr1(vcpu);
122#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
123 unsigned long cur_msr = kvmppc_get_msr(vcpu);
124
125 /*
126 * add rules to fit in ISA specification regarding TM
127 * state transistion in TM disable/Suspended state,
128 * and target TM state is TM inactive(00) state. (the
129 * change should be suppressed).
130 */
131 if (((cur_msr & MSR_TM) == 0) &&
132 ((srr1 & MSR_TM) == 0) &&
133 MSR_TM_SUSPENDED(cur_msr) &&
134 !MSR_TM_ACTIVE(srr1))
135 srr1 |= MSR_TS_S;
136#endif
Alexander Graf5deb8e72014-04-24 13:46:24 +0200137 kvmppc_set_pc(vcpu, kvmppc_get_srr0(vcpu));
Simon Guo401a89e2018-05-23 15:01:54 +0800138 kvmppc_set_msr(vcpu, srr1);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000139 *advance = 0;
140 break;
Simon Guo401a89e2018-05-23 15:01:54 +0800141 }
Alexander Grafc215c6e2009-10-30 05:47:14 +0000142
143 default:
144 emulated = EMULATE_FAIL;
145 break;
146 }
147 break;
148 case 31:
149 switch (get_xop(inst)) {
150 case OP_31_XOP_MFMSR:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200151 kvmppc_set_gpr(vcpu, rt, kvmppc_get_msr(vcpu));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000152 break;
153 case OP_31_XOP_MTMSRD:
154 {
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200155 ulong rs_val = kvmppc_get_gpr(vcpu, rs);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000156 if (inst & 0x10000) {
Alexander Graf5deb8e72014-04-24 13:46:24 +0200157 ulong new_msr = kvmppc_get_msr(vcpu);
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200158 new_msr &= ~(MSR_RI | MSR_EE);
159 new_msr |= rs_val & (MSR_RI | MSR_EE);
Alexander Graf5deb8e72014-04-24 13:46:24 +0200160 kvmppc_set_msr_fast(vcpu, new_msr);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000161 } else
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200162 kvmppc_set_msr(vcpu, rs_val);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000163 break;
164 }
165 case OP_31_XOP_MTMSR:
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200166 kvmppc_set_msr(vcpu, kvmppc_get_gpr(vcpu, rs));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000167 break;
Alexander Grafc6648762010-03-24 21:48:24 +0100168 case OP_31_XOP_MFSR:
169 {
170 int srnum;
171
172 srnum = kvmppc_get_field(inst, 12 + 32, 15 + 32);
173 if (vcpu->arch.mmu.mfsrin) {
174 u32 sr;
175 sr = vcpu->arch.mmu.mfsrin(vcpu, srnum);
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200176 kvmppc_set_gpr(vcpu, rt, sr);
Alexander Grafc6648762010-03-24 21:48:24 +0100177 }
178 break;
179 }
Alexander Grafc215c6e2009-10-30 05:47:14 +0000180 case OP_31_XOP_MFSRIN:
181 {
182 int srnum;
183
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200184 srnum = (kvmppc_get_gpr(vcpu, rb) >> 28) & 0xf;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000185 if (vcpu->arch.mmu.mfsrin) {
186 u32 sr;
187 sr = vcpu->arch.mmu.mfsrin(vcpu, srnum);
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200188 kvmppc_set_gpr(vcpu, rt, sr);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000189 }
190 break;
191 }
Alexander Graf71db4082010-02-19 11:00:37 +0100192 case OP_31_XOP_MTSR:
193 vcpu->arch.mmu.mtsrin(vcpu,
194 (inst >> 16) & 0xf,
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200195 kvmppc_get_gpr(vcpu, rs));
Alexander Graf71db4082010-02-19 11:00:37 +0100196 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000197 case OP_31_XOP_MTSRIN:
198 vcpu->arch.mmu.mtsrin(vcpu,
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200199 (kvmppc_get_gpr(vcpu, rb) >> 28) & 0xf,
200 kvmppc_get_gpr(vcpu, rs));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000201 break;
202 case OP_31_XOP_TLBIE:
203 case OP_31_XOP_TLBIEL:
204 {
205 bool large = (inst & 0x00200000) ? true : false;
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200206 ulong addr = kvmppc_get_gpr(vcpu, rb);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000207 vcpu->arch.mmu.tlbie(vcpu, addr, large);
208 break;
209 }
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +0530210#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf50c7bb82012-12-14 23:42:05 +0100211 case OP_31_XOP_FAKE_SC1:
212 {
213 /* SC 1 papr hypercalls */
214 ulong cmd = kvmppc_get_gpr(vcpu, 3);
215 int i;
216
Alexander Graf5deb8e72014-04-24 13:46:24 +0200217 if ((kvmppc_get_msr(vcpu) & MSR_PR) ||
Alexander Graf50c7bb82012-12-14 23:42:05 +0100218 !vcpu->arch.papr_enabled) {
219 emulated = EMULATE_FAIL;
220 break;
221 }
222
223 if (kvmppc_h_pr(vcpu, cmd) == EMULATE_DONE)
224 break;
225
226 run->papr_hcall.nr = cmd;
227 for (i = 0; i < 9; ++i) {
228 ulong gpr = kvmppc_get_gpr(vcpu, 4 + i);
229 run->papr_hcall.args[i] = gpr;
230 }
231
Bharat Bhushan0f47f9b2013-04-08 00:32:14 +0000232 run->exit_reason = KVM_EXIT_PAPR_HCALL;
233 vcpu->arch.hcall_needed = 1;
Bharat Bhushanc402a3f2013-04-08 00:32:13 +0000234 emulated = EMULATE_EXIT_USER;
Alexander Graf50c7bb82012-12-14 23:42:05 +0100235 break;
236 }
237#endif
Alexander Grafc215c6e2009-10-30 05:47:14 +0000238 case OP_31_XOP_EIOIO:
239 break;
240 case OP_31_XOP_SLBMTE:
241 if (!vcpu->arch.mmu.slbmte)
242 return EMULATE_FAIL;
243
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100244 vcpu->arch.mmu.slbmte(vcpu,
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200245 kvmppc_get_gpr(vcpu, rs),
246 kvmppc_get_gpr(vcpu, rb));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000247 break;
248 case OP_31_XOP_SLBIE:
249 if (!vcpu->arch.mmu.slbie)
250 return EMULATE_FAIL;
251
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100252 vcpu->arch.mmu.slbie(vcpu,
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200253 kvmppc_get_gpr(vcpu, rb));
Alexander Grafc215c6e2009-10-30 05:47:14 +0000254 break;
255 case OP_31_XOP_SLBIA:
256 if (!vcpu->arch.mmu.slbia)
257 return EMULATE_FAIL;
258
259 vcpu->arch.mmu.slbia(vcpu);
260 break;
261 case OP_31_XOP_SLBMFEE:
262 if (!vcpu->arch.mmu.slbmfee) {
263 emulated = EMULATE_FAIL;
264 } else {
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200265 ulong t, rb_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000266
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200267 rb_val = kvmppc_get_gpr(vcpu, rb);
268 t = vcpu->arch.mmu.slbmfee(vcpu, rb_val);
269 kvmppc_set_gpr(vcpu, rt, t);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000270 }
271 break;
272 case OP_31_XOP_SLBMFEV:
273 if (!vcpu->arch.mmu.slbmfev) {
274 emulated = EMULATE_FAIL;
275 } else {
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200276 ulong t, rb_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000277
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200278 rb_val = kvmppc_get_gpr(vcpu, rb);
279 t = vcpu->arch.mmu.slbmfev(vcpu, rb_val);
280 kvmppc_set_gpr(vcpu, rt, t);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000281 }
282 break;
Alexander Grafbd7cdbb2010-03-24 21:48:33 +0100283 case OP_31_XOP_DCBA:
284 /* Gets treated as NOP */
285 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000286 case OP_31_XOP_DCBZ:
287 {
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200288 ulong rb_val = kvmppc_get_gpr(vcpu, rb);
289 ulong ra_val = 0;
Alexander Graf5467a972010-02-19 11:00:38 +0100290 ulong addr, vaddr;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000291 u32 zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Alexander Graf9fb244a2010-03-24 21:48:32 +0100292 u32 dsisr;
293 int r;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000294
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200295 if (ra)
296 ra_val = kvmppc_get_gpr(vcpu, ra);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000297
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200298 addr = (ra_val + rb_val) & ~31ULL;
Alexander Graf5deb8e72014-04-24 13:46:24 +0200299 if (!(kvmppc_get_msr(vcpu) & MSR_SF))
Alexander Grafc215c6e2009-10-30 05:47:14 +0000300 addr &= 0xffffffff;
Alexander Graf5467a972010-02-19 11:00:38 +0100301 vaddr = addr;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000302
Alexander Graf9fb244a2010-03-24 21:48:32 +0100303 r = kvmppc_st(vcpu, &addr, 32, zeros, true);
304 if ((r == -ENOENT) || (r == -EPERM)) {
305 *advance = 0;
Alexander Graf5deb8e72014-04-24 13:46:24 +0200306 kvmppc_set_dar(vcpu, vaddr);
Paul Mackerrasa2d56022013-09-20 14:52:43 +1000307 vcpu->arch.fault_dar = vaddr;
Alexander Graf9fb244a2010-03-24 21:48:32 +0100308
309 dsisr = DSISR_ISSTORE;
310 if (r == -ENOENT)
311 dsisr |= DSISR_NOHPTE;
312 else if (r == -EPERM)
313 dsisr |= DSISR_PROTFAULT;
314
Alexander Graf5deb8e72014-04-24 13:46:24 +0200315 kvmppc_set_dsisr(vcpu, dsisr);
Paul Mackerrasa2d56022013-09-20 14:52:43 +1000316 vcpu->arch.fault_dsisr = dsisr;
Alexander Graf9fb244a2010-03-24 21:48:32 +0100317
Alexander Grafc215c6e2009-10-30 05:47:14 +0000318 kvmppc_book3s_queue_irqprio(vcpu,
319 BOOK3S_INTERRUPT_DATA_STORAGE);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000320 }
321
322 break;
323 }
324 default:
325 emulated = EMULATE_FAIL;
326 }
327 break;
328 default:
329 emulated = EMULATE_FAIL;
330 }
331
Alexander Graf831317b2010-02-19 11:00:44 +0100332 if (emulated == EMULATE_FAIL)
333 emulated = kvmppc_emulate_paired_single(run, vcpu);
334
Alexander Grafc215c6e2009-10-30 05:47:14 +0000335 return emulated;
336}
337
Alexander Grafe15a1132009-11-30 03:02:02 +0000338void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat, bool upper,
339 u32 val)
340{
341 if (upper) {
342 /* Upper BAT */
343 u32 bl = (val >> 2) & 0x7ff;
344 bat->bepi_mask = (~bl << 17);
345 bat->bepi = val & 0xfffe0000;
346 bat->vs = (val & 2) ? 1 : 0;
347 bat->vp = (val & 1) ? 1 : 0;
348 bat->raw = (bat->raw & 0xffffffff00000000ULL) | val;
349 } else {
350 /* Lower BAT */
351 bat->brpn = val & 0xfffe0000;
352 bat->wimg = (val >> 3) & 0xf;
353 bat->pp = val & 3;
354 bat->raw = (bat->raw & 0x00000000ffffffffULL) | ((u64)val << 32);
355 }
356}
357
Alexander Grafc1c88e22010-08-02 23:23:04 +0200358static struct kvmppc_bat *kvmppc_find_bat(struct kvm_vcpu *vcpu, int sprn)
Alexander Grafc04a6952010-03-24 21:48:25 +0100359{
360 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
361 struct kvmppc_bat *bat;
362
363 switch (sprn) {
364 case SPRN_IBAT0U ... SPRN_IBAT3L:
365 bat = &vcpu_book3s->ibat[(sprn - SPRN_IBAT0U) / 2];
366 break;
367 case SPRN_IBAT4U ... SPRN_IBAT7L:
368 bat = &vcpu_book3s->ibat[4 + ((sprn - SPRN_IBAT4U) / 2)];
369 break;
370 case SPRN_DBAT0U ... SPRN_DBAT3L:
371 bat = &vcpu_book3s->dbat[(sprn - SPRN_DBAT0U) / 2];
372 break;
373 case SPRN_DBAT4U ... SPRN_DBAT7L:
374 bat = &vcpu_book3s->dbat[4 + ((sprn - SPRN_DBAT4U) / 2)];
375 break;
376 default:
377 BUG();
378 }
379
Alexander Grafc1c88e22010-08-02 23:23:04 +0200380 return bat;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000381}
382
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530383int kvmppc_core_emulate_mtspr_pr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
Alexander Grafc215c6e2009-10-30 05:47:14 +0000384{
385 int emulated = EMULATE_DONE;
386
387 switch (sprn) {
388 case SPRN_SDR1:
Alexander Graf317a8fa2011-08-08 16:07:16 +0200389 if (!spr_allowed(vcpu, PRIV_HYPER))
390 goto unprivileged;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100391 to_book3s(vcpu)->sdr1 = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000392 break;
393 case SPRN_DSISR:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200394 kvmppc_set_dsisr(vcpu, spr_val);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000395 break;
396 case SPRN_DAR:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200397 kvmppc_set_dar(vcpu, spr_val);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000398 break;
399 case SPRN_HIOR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100400 to_book3s(vcpu)->hior = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000401 break;
402 case SPRN_IBAT0U ... SPRN_IBAT3L:
403 case SPRN_IBAT4U ... SPRN_IBAT7L:
404 case SPRN_DBAT0U ... SPRN_DBAT3L:
405 case SPRN_DBAT4U ... SPRN_DBAT7L:
Alexander Grafc1c88e22010-08-02 23:23:04 +0200406 {
407 struct kvmppc_bat *bat = kvmppc_find_bat(vcpu, sprn);
408
409 kvmppc_set_bat(vcpu, bat, !(sprn % 2), (u32)spr_val);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000410 /* BAT writes happen so rarely that we're ok to flush
411 * everything here */
412 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Alexander Grafc04a6952010-03-24 21:48:25 +0100413 kvmppc_mmu_flush_segments(vcpu);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000414 break;
Alexander Grafc1c88e22010-08-02 23:23:04 +0200415 }
Alexander Grafc215c6e2009-10-30 05:47:14 +0000416 case SPRN_HID0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100417 to_book3s(vcpu)->hid[0] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000418 break;
419 case SPRN_HID1:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100420 to_book3s(vcpu)->hid[1] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000421 break;
422 case SPRN_HID2:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100423 to_book3s(vcpu)->hid[2] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000424 break;
Alexander Grafd6d549b2010-02-19 11:00:33 +0100425 case SPRN_HID2_GEKKO:
426 to_book3s(vcpu)->hid[2] = spr_val;
427 /* HID2.PSE controls paired single on gekko */
428 switch (vcpu->arch.pvr) {
429 case 0x00080200: /* lonestar 2.0 */
430 case 0x00088202: /* lonestar 2.2 */
431 case 0x70000100: /* gekko 1.0 */
432 case 0x00080100: /* gekko 2.0 */
433 case 0x00083203: /* gekko 2.3a */
434 case 0x00083213: /* gekko 2.3b */
435 case 0x00083204: /* gekko 2.4 */
436 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
Alexander Grafb83d4a92010-04-20 02:49:54 +0200437 case 0x00087200: /* broadway */
438 if (vcpu->arch.hflags & BOOK3S_HFLAG_NATIVE_PS) {
439 /* Native paired singles */
440 } else if (spr_val & (1 << 29)) { /* HID2.PSE */
Alexander Grafd6d549b2010-02-19 11:00:33 +0100441 vcpu->arch.hflags |= BOOK3S_HFLAG_PAIRED_SINGLE;
442 kvmppc_giveup_ext(vcpu, MSR_FP);
443 } else {
444 vcpu->arch.hflags &= ~BOOK3S_HFLAG_PAIRED_SINGLE;
445 }
446 break;
447 }
448 break;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000449 case SPRN_HID4:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100450 case SPRN_HID4_GEKKO:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100451 to_book3s(vcpu)->hid[4] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000452 break;
453 case SPRN_HID5:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100454 to_book3s(vcpu)->hid[5] = spr_val;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000455 /* guest HID5 set can change is_dcbz32 */
456 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
457 (mfmsr() & MSR_HV))
458 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
459 break;
Alexander Grafd6d549b2010-02-19 11:00:33 +0100460 case SPRN_GQR0:
461 case SPRN_GQR1:
462 case SPRN_GQR2:
463 case SPRN_GQR3:
464 case SPRN_GQR4:
465 case SPRN_GQR5:
466 case SPRN_GQR6:
467 case SPRN_GQR7:
468 to_book3s(vcpu)->gqr[sprn - SPRN_GQR0] = spr_val;
469 break;
Alexander Graf2e23f542014-04-29 13:36:21 +0200470#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf8e6afa32014-07-31 10:21:59 +0200471 case SPRN_FSCR:
472 kvmppc_set_fscr(vcpu, spr_val);
473 break;
Alexander Graf2e23f542014-04-29 13:36:21 +0200474 case SPRN_BESCR:
475 vcpu->arch.bescr = spr_val;
476 break;
477 case SPRN_EBBHR:
478 vcpu->arch.ebbhr = spr_val;
479 break;
480 case SPRN_EBBRR:
481 vcpu->arch.ebbrr = spr_val;
482 break;
Alexander Graf9916d572014-04-29 17:54:40 +0200483#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
484 case SPRN_TFHAR:
485 vcpu->arch.tfhar = spr_val;
486 break;
487 case SPRN_TEXASR:
488 vcpu->arch.texasr = spr_val;
489 break;
490 case SPRN_TFIAR:
491 vcpu->arch.tfiar = spr_val;
492 break;
493#endif
Alexander Graf2e23f542014-04-29 13:36:21 +0200494#endif
Alexander Grafc215c6e2009-10-30 05:47:14 +0000495 case SPRN_ICTC:
496 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:
Paul Mackerrasb0a94d42012-11-04 18:15:43 +0000502 case SPRN_DSCR:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100503 case SPRN_MMCR0_GEKKO:
504 case SPRN_MMCR1_GEKKO:
505 case SPRN_PMC1_GEKKO:
506 case SPRN_PMC2_GEKKO:
507 case SPRN_PMC3_GEKKO:
508 case SPRN_PMC4_GEKKO:
509 case SPRN_WPAR_GEKKO:
Mihai Caramanf2be6552012-12-20 04:52:39 +0000510 case SPRN_MSSSR0:
Alexander Graff3532022013-07-02 16:15:10 +0200511 case SPRN_DABR:
Alexander Graff8f6eb02014-04-22 12:41:06 +0200512#ifdef CONFIG_PPC_BOOK3S_64
513 case SPRN_MMCRS:
514 case SPRN_MMCRA:
515 case SPRN_MMCR0:
516 case SPRN_MMCR1:
517 case SPRN_MMCR2:
Thomas Huthfa73c3b2016-09-21 15:06:45 +0200518 case SPRN_UMMCR2:
Alexander Graff8f6eb02014-04-22 12:41:06 +0200519#endif
Alexander Grafc215c6e2009-10-30 05:47:14 +0000520 break;
Alexander Graf317a8fa2011-08-08 16:07:16 +0200521unprivileged:
Alexander Grafc215c6e2009-10-30 05:47:14 +0000522 default:
Thomas Huthfeafd132017-04-05 15:58:51 +0200523 pr_info_ratelimited("KVM: invalid SPR write: %d\n", sprn);
524 if (sprn & 0x10) {
525 if (kvmppc_get_msr(vcpu) & MSR_PR) {
526 kvmppc_core_queue_program(vcpu, SRR1_PROGPRIV);
527 emulated = EMULATE_AGAIN;
528 }
529 } else {
530 if ((kvmppc_get_msr(vcpu) & MSR_PR) || sprn == 0) {
531 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
532 emulated = EMULATE_AGAIN;
533 }
534 }
Alexander Grafc215c6e2009-10-30 05:47:14 +0000535 break;
536 }
537
538 return emulated;
539}
540
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530541int kvmppc_core_emulate_mfspr_pr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
Alexander Grafc215c6e2009-10-30 05:47:14 +0000542{
543 int emulated = EMULATE_DONE;
544
545 switch (sprn) {
Alexander Grafc04a6952010-03-24 21:48:25 +0100546 case SPRN_IBAT0U ... SPRN_IBAT3L:
547 case SPRN_IBAT4U ... SPRN_IBAT7L:
548 case SPRN_DBAT0U ... SPRN_DBAT3L:
549 case SPRN_DBAT4U ... SPRN_DBAT7L:
Alexander Grafc1c88e22010-08-02 23:23:04 +0200550 {
551 struct kvmppc_bat *bat = kvmppc_find_bat(vcpu, sprn);
552
553 if (sprn % 2)
Alexander Graf54771e62012-05-04 14:55:12 +0200554 *spr_val = bat->raw >> 32;
Alexander Grafc1c88e22010-08-02 23:23:04 +0200555 else
Alexander Graf54771e62012-05-04 14:55:12 +0200556 *spr_val = bat->raw;
Alexander Grafc1c88e22010-08-02 23:23:04 +0200557
Alexander Grafc04a6952010-03-24 21:48:25 +0100558 break;
Alexander Grafc1c88e22010-08-02 23:23:04 +0200559 }
Alexander Grafc215c6e2009-10-30 05:47:14 +0000560 case SPRN_SDR1:
Alexander Graf317a8fa2011-08-08 16:07:16 +0200561 if (!spr_allowed(vcpu, PRIV_HYPER))
562 goto unprivileged;
Alexander Graf54771e62012-05-04 14:55:12 +0200563 *spr_val = to_book3s(vcpu)->sdr1;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000564 break;
565 case SPRN_DSISR:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200566 *spr_val = kvmppc_get_dsisr(vcpu);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000567 break;
568 case SPRN_DAR:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200569 *spr_val = kvmppc_get_dar(vcpu);
Alexander Grafc215c6e2009-10-30 05:47:14 +0000570 break;
571 case SPRN_HIOR:
Alexander Graf54771e62012-05-04 14:55:12 +0200572 *spr_val = to_book3s(vcpu)->hior;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000573 break;
574 case SPRN_HID0:
Alexander Graf54771e62012-05-04 14:55:12 +0200575 *spr_val = to_book3s(vcpu)->hid[0];
Alexander Grafc215c6e2009-10-30 05:47:14 +0000576 break;
577 case SPRN_HID1:
Alexander Graf54771e62012-05-04 14:55:12 +0200578 *spr_val = to_book3s(vcpu)->hid[1];
Alexander Grafc215c6e2009-10-30 05:47:14 +0000579 break;
580 case SPRN_HID2:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100581 case SPRN_HID2_GEKKO:
Alexander Graf54771e62012-05-04 14:55:12 +0200582 *spr_val = to_book3s(vcpu)->hid[2];
Alexander Grafc215c6e2009-10-30 05:47:14 +0000583 break;
584 case SPRN_HID4:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100585 case SPRN_HID4_GEKKO:
Alexander Graf54771e62012-05-04 14:55:12 +0200586 *spr_val = to_book3s(vcpu)->hid[4];
Alexander Grafc215c6e2009-10-30 05:47:14 +0000587 break;
588 case SPRN_HID5:
Alexander Graf54771e62012-05-04 14:55:12 +0200589 *spr_val = to_book3s(vcpu)->hid[5];
Alexander Grafc215c6e2009-10-30 05:47:14 +0000590 break;
Alexander Grafaacf9aa2011-08-08 17:22:59 +0200591 case SPRN_CFAR:
Paul Mackerrasb0a94d42012-11-04 18:15:43 +0000592 case SPRN_DSCR:
Alexander Graf54771e62012-05-04 14:55:12 +0200593 *spr_val = 0;
Alexander Grafaacf9aa2011-08-08 17:22:59 +0200594 break;
Paul Mackerrasb0a94d42012-11-04 18:15:43 +0000595 case SPRN_PURR:
Aneesh Kumar K.V3cd60e32014-06-04 16:47:55 +0530596 /*
597 * On exit we would have updated purr
598 */
599 *spr_val = vcpu->arch.purr;
Paul Mackerrasb0a94d42012-11-04 18:15:43 +0000600 break;
601 case SPRN_SPURR:
Aneesh Kumar K.V3cd60e32014-06-04 16:47:55 +0530602 /*
603 * On exit we would have updated spurr
604 */
605 *spr_val = vcpu->arch.spurr;
Paul Mackerrasb0a94d42012-11-04 18:15:43 +0000606 break;
Aneesh Kumar K.V8f42ab22014-06-05 17:38:02 +0530607 case SPRN_VTB:
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000608 *spr_val = to_book3s(vcpu)->vtb;
Aneesh Kumar K.V8f42ab22014-06-05 17:38:02 +0530609 break;
Aneesh Kumar K.V06da28e2014-06-05 17:38:05 +0530610 case SPRN_IC:
611 *spr_val = vcpu->arch.ic;
612 break;
Alexander Grafd6d549b2010-02-19 11:00:33 +0100613 case SPRN_GQR0:
614 case SPRN_GQR1:
615 case SPRN_GQR2:
616 case SPRN_GQR3:
617 case SPRN_GQR4:
618 case SPRN_GQR5:
619 case SPRN_GQR6:
620 case SPRN_GQR7:
Alexander Graf54771e62012-05-04 14:55:12 +0200621 *spr_val = to_book3s(vcpu)->gqr[sprn - SPRN_GQR0];
Alexander Grafd6d549b2010-02-19 11:00:33 +0100622 break;
Alexander Graf8e6afa32014-07-31 10:21:59 +0200623#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf616dff82014-04-29 16:48:44 +0200624 case SPRN_FSCR:
625 *spr_val = vcpu->arch.fscr;
626 break;
Alexander Graf2e23f542014-04-29 13:36:21 +0200627 case SPRN_BESCR:
628 *spr_val = vcpu->arch.bescr;
629 break;
630 case SPRN_EBBHR:
631 *spr_val = vcpu->arch.ebbhr;
632 break;
633 case SPRN_EBBRR:
634 *spr_val = vcpu->arch.ebbrr;
635 break;
Alexander Graf9916d572014-04-29 17:54:40 +0200636#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
637 case SPRN_TFHAR:
638 *spr_val = vcpu->arch.tfhar;
639 break;
640 case SPRN_TEXASR:
641 *spr_val = vcpu->arch.texasr;
642 break;
643 case SPRN_TFIAR:
644 *spr_val = vcpu->arch.tfiar;
645 break;
646#endif
Alexander Graf2e23f542014-04-29 13:36:21 +0200647#endif
Alexander Grafc215c6e2009-10-30 05:47:14 +0000648 case SPRN_THRM1:
649 case SPRN_THRM2:
650 case SPRN_THRM3:
651 case SPRN_CTRLF:
652 case SPRN_CTRLT:
Alexander Grafd6d549b2010-02-19 11:00:33 +0100653 case SPRN_L2CR:
654 case SPRN_MMCR0_GEKKO:
655 case SPRN_MMCR1_GEKKO:
656 case SPRN_PMC1_GEKKO:
657 case SPRN_PMC2_GEKKO:
658 case SPRN_PMC3_GEKKO:
659 case SPRN_PMC4_GEKKO:
660 case SPRN_WPAR_GEKKO:
Mihai Caramanf2be6552012-12-20 04:52:39 +0000661 case SPRN_MSSSR0:
Alexander Graff3532022013-07-02 16:15:10 +0200662 case SPRN_DABR:
Alexander Graff8f6eb02014-04-22 12:41:06 +0200663#ifdef CONFIG_PPC_BOOK3S_64
664 case SPRN_MMCRS:
665 case SPRN_MMCRA:
666 case SPRN_MMCR0:
667 case SPRN_MMCR1:
668 case SPRN_MMCR2:
Thomas Huthfa73c3b2016-09-21 15:06:45 +0200669 case SPRN_UMMCR2:
Alexander Grafa5948fa2014-04-25 16:07:21 +0200670 case SPRN_TIR:
Alexander Graff8f6eb02014-04-22 12:41:06 +0200671#endif
Alexander Graf54771e62012-05-04 14:55:12 +0200672 *spr_val = 0;
Alexander Grafc215c6e2009-10-30 05:47:14 +0000673 break;
674 default:
Alexander Graf317a8fa2011-08-08 16:07:16 +0200675unprivileged:
Thomas Huthfeafd132017-04-05 15:58:51 +0200676 pr_info_ratelimited("KVM: invalid SPR read: %d\n", sprn);
677 if (sprn & 0x10) {
678 if (kvmppc_get_msr(vcpu) & MSR_PR) {
679 kvmppc_core_queue_program(vcpu, SRR1_PROGPRIV);
680 emulated = EMULATE_AGAIN;
681 }
682 } else {
683 if ((kvmppc_get_msr(vcpu) & MSR_PR) || sprn == 0 ||
684 sprn == 4 || sprn == 5 || sprn == 6) {
685 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
686 emulated = EMULATE_AGAIN;
687 }
688 }
689
Alexander Grafc215c6e2009-10-30 05:47:14 +0000690 break;
691 }
692
693 return emulated;
694}
695
Alexander Grafca7f4202010-03-24 21:48:28 +0100696u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst)
697{
Aneesh Kumar K.Vddca1562014-05-12 17:04:06 +0530698 return make_dsisr(inst);
Alexander Grafca7f4202010-03-24 21:48:28 +0100699}
700
701ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst)
702{
Aneesh Kumar K.V7310f3a2014-05-12 17:04:05 +0530703#ifdef CONFIG_PPC_BOOK3S_64
704 /*
705 * Linux's fix_alignment() assumes that DAR is valid, so can we
706 */
707 return vcpu->arch.fault_dar;
708#else
Alexander Grafca7f4202010-03-24 21:48:28 +0100709 ulong dar = 0;
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200710 ulong ra = get_ra(inst);
711 ulong rb = get_rb(inst);
Alexander Grafca7f4202010-03-24 21:48:28 +0100712
713 switch (get_op(inst)) {
714 case OP_LFS:
715 case OP_LFD:
716 case OP_STFD:
717 case OP_STFS:
Alexander Grafca7f4202010-03-24 21:48:28 +0100718 if (ra)
719 dar = kvmppc_get_gpr(vcpu, ra);
720 dar += (s32)((s16)inst);
721 break;
722 case 31:
Alexander Grafca7f4202010-03-24 21:48:28 +0100723 if (ra)
724 dar = kvmppc_get_gpr(vcpu, ra);
Alexander Grafc46dc9a2012-05-04 14:01:33 +0200725 dar += kvmppc_get_gpr(vcpu, rb);
Alexander Grafca7f4202010-03-24 21:48:28 +0100726 break;
727 default:
728 printk(KERN_INFO "KVM: Unaligned instruction 0x%x\n", inst);
729 break;
730 }
731
732 return dar;
Aneesh Kumar K.V7310f3a2014-05-12 17:04:05 +0530733#endif
Alexander Grafca7f4202010-03-24 21:48:28 +0100734}