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.
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.
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.
15 * Copyright SUSE Linux Products GmbH 2009
17 * Authors: Alexander Graf <agraf@suse.de>
20 #include <asm/kvm_ppc.h>
21 #include <asm/disassemble.h>
22 #include <asm/kvm_book3s.h>
24 #include <asm/switch_to.h>
27 #define OP_19_XOP_RFID 18
28 #define OP_19_XOP_RFI 50
30 #define OP_31_XOP_MFMSR 83
31 #define OP_31_XOP_MTMSR 146
32 #define OP_31_XOP_MTMSRD 178
33 #define OP_31_XOP_MTSR 210
34 #define OP_31_XOP_MTSRIN 242
35 #define OP_31_XOP_TLBIEL 274
36 #define OP_31_XOP_TLBIE 306
37 /* Opcode is officially reserved, reuse it as sc 1 when sc 1 doesn't trap */
38 #define OP_31_XOP_FAKE_SC1 308
39 #define OP_31_XOP_SLBMTE 402
40 #define OP_31_XOP_SLBIE 434
41 #define OP_31_XOP_SLBIA 498
42 #define OP_31_XOP_MFSR 595
43 #define OP_31_XOP_MFSRIN 659
44 #define OP_31_XOP_DCBA 758
45 #define OP_31_XOP_SLBMFEV 851
46 #define OP_31_XOP_EIOIO 854
47 #define OP_31_XOP_SLBMFEE 915
49 /* DCBZ is actually 1014, but we patch it to 1010 so we get a trap */
50 #define OP_31_XOP_DCBZ 1010
66 /* Book3S_32 defines mfsrin(v) - but that messes up our abstract
67 * function pointers, so let's just disable the define. */
76 static bool spr_allowed(struct kvm_vcpu
*vcpu
, enum priv_level level
)
78 /* PAPR VMs only access supervisor SPRs */
79 if (vcpu
->arch
.papr_enabled
&& (level
> PRIV_SUPER
))
82 /* Limit user space to its own small SPR set */
83 if ((kvmppc_get_msr(vcpu
) & MSR_PR
) && level
> PRIV_PROBLEM
)
89 int kvmppc_core_emulate_op_pr(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
90 unsigned int inst
, int *advance
)
92 int emulated
= EMULATE_DONE
;
93 int rt
= get_rt(inst
);
94 int rs
= get_rs(inst
);
95 int ra
= get_ra(inst
);
96 int rb
= get_rb(inst
);
97 u32 inst_sc
= 0x44000002;
99 switch (get_op(inst
)) {
101 emulated
= EMULATE_FAIL
;
102 if ((kvmppc_get_msr(vcpu
) & MSR_LE
) &&
103 (inst
== swab32(inst_sc
))) {
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.
111 kvmppc_set_gpr(vcpu
, 3, EV_UNIMPLEMENTED
);
112 kvmppc_set_pc(vcpu
, kvmppc_get_pc(vcpu
) + 4);
113 emulated
= EMULATE_DONE
;
117 switch (get_xop(inst
)) {
120 kvmppc_set_pc(vcpu
, kvmppc_get_srr0(vcpu
));
121 kvmppc_set_msr(vcpu
, kvmppc_get_srr1(vcpu
));
126 emulated
= EMULATE_FAIL
;
131 switch (get_xop(inst
)) {
132 case OP_31_XOP_MFMSR
:
133 kvmppc_set_gpr(vcpu
, rt
, kvmppc_get_msr(vcpu
));
135 case OP_31_XOP_MTMSRD
:
137 ulong rs_val
= kvmppc_get_gpr(vcpu
, rs
);
138 if (inst
& 0x10000) {
139 ulong new_msr
= kvmppc_get_msr(vcpu
);
140 new_msr
&= ~(MSR_RI
| MSR_EE
);
141 new_msr
|= rs_val
& (MSR_RI
| MSR_EE
);
142 kvmppc_set_msr_fast(vcpu
, new_msr
);
144 kvmppc_set_msr(vcpu
, rs_val
);
147 case OP_31_XOP_MTMSR
:
148 kvmppc_set_msr(vcpu
, kvmppc_get_gpr(vcpu
, rs
));
154 srnum
= kvmppc_get_field(inst
, 12 + 32, 15 + 32);
155 if (vcpu
->arch
.mmu
.mfsrin
) {
157 sr
= vcpu
->arch
.mmu
.mfsrin(vcpu
, srnum
);
158 kvmppc_set_gpr(vcpu
, rt
, sr
);
162 case OP_31_XOP_MFSRIN
:
166 srnum
= (kvmppc_get_gpr(vcpu
, rb
) >> 28) & 0xf;
167 if (vcpu
->arch
.mmu
.mfsrin
) {
169 sr
= vcpu
->arch
.mmu
.mfsrin(vcpu
, srnum
);
170 kvmppc_set_gpr(vcpu
, rt
, sr
);
175 vcpu
->arch
.mmu
.mtsrin(vcpu
,
177 kvmppc_get_gpr(vcpu
, rs
));
179 case OP_31_XOP_MTSRIN
:
180 vcpu
->arch
.mmu
.mtsrin(vcpu
,
181 (kvmppc_get_gpr(vcpu
, rb
) >> 28) & 0xf,
182 kvmppc_get_gpr(vcpu
, rs
));
184 case OP_31_XOP_TLBIE
:
185 case OP_31_XOP_TLBIEL
:
187 bool large
= (inst
& 0x00200000) ? true : false;
188 ulong addr
= kvmppc_get_gpr(vcpu
, rb
);
189 vcpu
->arch
.mmu
.tlbie(vcpu
, addr
, large
);
192 #ifdef CONFIG_PPC_BOOK3S_64
193 case OP_31_XOP_FAKE_SC1
:
195 /* SC 1 papr hypercalls */
196 ulong cmd
= kvmppc_get_gpr(vcpu
, 3);
199 if ((kvmppc_get_msr(vcpu
) & MSR_PR
) ||
200 !vcpu
->arch
.papr_enabled
) {
201 emulated
= EMULATE_FAIL
;
205 if (kvmppc_h_pr(vcpu
, cmd
) == EMULATE_DONE
)
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
;
214 run
->exit_reason
= KVM_EXIT_PAPR_HCALL
;
215 vcpu
->arch
.hcall_needed
= 1;
216 emulated
= EMULATE_EXIT_USER
;
220 case OP_31_XOP_EIOIO
:
222 case OP_31_XOP_SLBMTE
:
223 if (!vcpu
->arch
.mmu
.slbmte
)
226 vcpu
->arch
.mmu
.slbmte(vcpu
,
227 kvmppc_get_gpr(vcpu
, rs
),
228 kvmppc_get_gpr(vcpu
, rb
));
230 case OP_31_XOP_SLBIE
:
231 if (!vcpu
->arch
.mmu
.slbie
)
234 vcpu
->arch
.mmu
.slbie(vcpu
,
235 kvmppc_get_gpr(vcpu
, rb
));
237 case OP_31_XOP_SLBIA
:
238 if (!vcpu
->arch
.mmu
.slbia
)
241 vcpu
->arch
.mmu
.slbia(vcpu
);
243 case OP_31_XOP_SLBMFEE
:
244 if (!vcpu
->arch
.mmu
.slbmfee
) {
245 emulated
= EMULATE_FAIL
;
249 rb_val
= kvmppc_get_gpr(vcpu
, rb
);
250 t
= vcpu
->arch
.mmu
.slbmfee(vcpu
, rb_val
);
251 kvmppc_set_gpr(vcpu
, rt
, t
);
254 case OP_31_XOP_SLBMFEV
:
255 if (!vcpu
->arch
.mmu
.slbmfev
) {
256 emulated
= EMULATE_FAIL
;
260 rb_val
= kvmppc_get_gpr(vcpu
, rb
);
261 t
= vcpu
->arch
.mmu
.slbmfev(vcpu
, rb_val
);
262 kvmppc_set_gpr(vcpu
, rt
, t
);
266 /* Gets treated as NOP */
270 ulong rb_val
= kvmppc_get_gpr(vcpu
, rb
);
273 u32 zeros
[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
278 ra_val
= kvmppc_get_gpr(vcpu
, ra
);
280 addr
= (ra_val
+ rb_val
) & ~31ULL;
281 if (!(kvmppc_get_msr(vcpu
) & MSR_SF
))
285 r
= kvmppc_st(vcpu
, &addr
, 32, zeros
, true);
286 if ((r
== -ENOENT
) || (r
== -EPERM
)) {
288 kvmppc_set_dar(vcpu
, vaddr
);
289 vcpu
->arch
.fault_dar
= vaddr
;
291 dsisr
= DSISR_ISSTORE
;
293 dsisr
|= DSISR_NOHPTE
;
294 else if (r
== -EPERM
)
295 dsisr
|= DSISR_PROTFAULT
;
297 kvmppc_set_dsisr(vcpu
, dsisr
);
298 vcpu
->arch
.fault_dsisr
= dsisr
;
300 kvmppc_book3s_queue_irqprio(vcpu
,
301 BOOK3S_INTERRUPT_DATA_STORAGE
);
307 emulated
= EMULATE_FAIL
;
311 emulated
= EMULATE_FAIL
;
314 if (emulated
== EMULATE_FAIL
)
315 emulated
= kvmppc_emulate_paired_single(run
, vcpu
);
320 void kvmppc_set_bat(struct kvm_vcpu
*vcpu
, struct kvmppc_bat
*bat
, bool upper
,
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
;
333 bat
->brpn
= val
& 0xfffe0000;
334 bat
->wimg
= (val
>> 3) & 0xf;
336 bat
->raw
= (bat
->raw
& 0x00000000ffffffffULL
) | ((u64
)val
<< 32);
340 static struct kvmppc_bat
*kvmppc_find_bat(struct kvm_vcpu
*vcpu
, int sprn
)
342 struct kvmppc_vcpu_book3s
*vcpu_book3s
= to_book3s(vcpu
);
343 struct kvmppc_bat
*bat
;
346 case SPRN_IBAT0U
... SPRN_IBAT3L
:
347 bat
= &vcpu_book3s
->ibat
[(sprn
- SPRN_IBAT0U
) / 2];
349 case SPRN_IBAT4U
... SPRN_IBAT7L
:
350 bat
= &vcpu_book3s
->ibat
[4 + ((sprn
- SPRN_IBAT4U
) / 2)];
352 case SPRN_DBAT0U
... SPRN_DBAT3L
:
353 bat
= &vcpu_book3s
->dbat
[(sprn
- SPRN_DBAT0U
) / 2];
355 case SPRN_DBAT4U
... SPRN_DBAT7L
:
356 bat
= &vcpu_book3s
->dbat
[4 + ((sprn
- SPRN_DBAT4U
) / 2)];
365 int kvmppc_core_emulate_mtspr_pr(struct kvm_vcpu
*vcpu
, int sprn
, ulong spr_val
)
367 int emulated
= EMULATE_DONE
;
371 if (!spr_allowed(vcpu
, PRIV_HYPER
))
373 to_book3s(vcpu
)->sdr1
= spr_val
;
376 kvmppc_set_dsisr(vcpu
, spr_val
);
379 kvmppc_set_dar(vcpu
, spr_val
);
382 to_book3s(vcpu
)->hior
= spr_val
;
384 case SPRN_IBAT0U
... SPRN_IBAT3L
:
385 case SPRN_IBAT4U
... SPRN_IBAT7L
:
386 case SPRN_DBAT0U
... SPRN_DBAT3L
:
387 case SPRN_DBAT4U
... SPRN_DBAT7L
:
389 struct kvmppc_bat
*bat
= kvmppc_find_bat(vcpu
, sprn
);
391 kvmppc_set_bat(vcpu
, bat
, !(sprn
% 2), (u32
)spr_val
);
392 /* BAT writes happen so rarely that we're ok to flush
394 kvmppc_mmu_pte_flush(vcpu
, 0, 0);
395 kvmppc_mmu_flush_segments(vcpu
);
399 to_book3s(vcpu
)->hid
[0] = spr_val
;
402 to_book3s(vcpu
)->hid
[1] = spr_val
;
405 to_book3s(vcpu
)->hid
[2] = spr_val
;
407 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 */
419 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 */
423 vcpu
->arch
.hflags
|= BOOK3S_HFLAG_PAIRED_SINGLE
;
424 kvmppc_giveup_ext(vcpu
, MSR_FP
);
426 vcpu
->arch
.hflags
&= ~BOOK3S_HFLAG_PAIRED_SINGLE
;
432 case SPRN_HID4_GEKKO
:
433 to_book3s(vcpu
)->hid
[4] = spr_val
;
436 to_book3s(vcpu
)->hid
[5] = spr_val
;
437 /* guest HID5 set can change is_dcbz32 */
438 if (vcpu
->arch
.mmu
.is_dcbz32(vcpu
) &&
440 vcpu
->arch
.hflags
|= BOOK3S_HFLAG_DCBZ32
;
450 to_book3s(vcpu
)->gqr
[sprn
- SPRN_GQR0
] = spr_val
;
452 #ifdef CONFIG_PPC_BOOK3S_64
454 kvmppc_set_fscr(vcpu
, spr_val
);
457 vcpu
->arch
.bescr
= spr_val
;
460 vcpu
->arch
.ebbhr
= spr_val
;
463 vcpu
->arch
.ebbrr
= spr_val
;
465 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
467 vcpu
->arch
.tfhar
= spr_val
;
470 vcpu
->arch
.texasr
= spr_val
;
473 vcpu
->arch
.tfiar
= spr_val
;
485 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
:
494 #ifdef CONFIG_PPC_BOOK3S_64
504 printk(KERN_INFO
"KVM: invalid SPR write: %d\n", sprn
);
506 emulated
= EMULATE_FAIL
;
514 int kvmppc_core_emulate_mfspr_pr(struct kvm_vcpu
*vcpu
, int sprn
, ulong
*spr_val
)
516 int emulated
= EMULATE_DONE
;
519 case SPRN_IBAT0U
... SPRN_IBAT3L
:
520 case SPRN_IBAT4U
... SPRN_IBAT7L
:
521 case SPRN_DBAT0U
... SPRN_DBAT3L
:
522 case SPRN_DBAT4U
... SPRN_DBAT7L
:
524 struct kvmppc_bat
*bat
= kvmppc_find_bat(vcpu
, sprn
);
527 *spr_val
= bat
->raw
>> 32;
534 if (!spr_allowed(vcpu
, PRIV_HYPER
))
536 *spr_val
= to_book3s(vcpu
)->sdr1
;
539 *spr_val
= kvmppc_get_dsisr(vcpu
);
542 *spr_val
= kvmppc_get_dar(vcpu
);
545 *spr_val
= to_book3s(vcpu
)->hior
;
548 *spr_val
= to_book3s(vcpu
)->hid
[0];
551 *spr_val
= to_book3s(vcpu
)->hid
[1];
554 case SPRN_HID2_GEKKO
:
555 *spr_val
= to_book3s(vcpu
)->hid
[2];
558 case SPRN_HID4_GEKKO
:
559 *spr_val
= to_book3s(vcpu
)->hid
[4];
562 *spr_val
= to_book3s(vcpu
)->hid
[5];
570 * On exit we would have updated purr
572 *spr_val
= vcpu
->arch
.purr
;
576 * On exit we would have updated spurr
578 *spr_val
= vcpu
->arch
.spurr
;
581 *spr_val
= vcpu
->arch
.vtb
;
584 *spr_val
= vcpu
->arch
.ic
;
594 *spr_val
= to_book3s(vcpu
)->gqr
[sprn
- SPRN_GQR0
];
596 #ifdef CONFIG_PPC_BOOK3S_64
598 *spr_val
= vcpu
->arch
.fscr
;
601 *spr_val
= vcpu
->arch
.bescr
;
604 *spr_val
= vcpu
->arch
.ebbhr
;
607 *spr_val
= vcpu
->arch
.ebbrr
;
609 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
611 *spr_val
= vcpu
->arch
.tfhar
;
614 *spr_val
= vcpu
->arch
.texasr
;
617 *spr_val
= vcpu
->arch
.tfiar
;
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
:
636 #ifdef CONFIG_PPC_BOOK3S_64
648 printk(KERN_INFO
"KVM: invalid SPR read: %d\n", sprn
);
650 emulated
= EMULATE_FAIL
;
658 u32
kvmppc_alignment_dsisr(struct kvm_vcpu
*vcpu
, unsigned int inst
)
660 return make_dsisr(inst
);
663 ulong
kvmppc_alignment_dar(struct kvm_vcpu
*vcpu
, unsigned int inst
)
665 #ifdef CONFIG_PPC_BOOK3S_64
667 * Linux's fix_alignment() assumes that DAR is valid, so can we
669 return vcpu
->arch
.fault_dar
;
672 ulong ra
= get_ra(inst
);
673 ulong rb
= get_rb(inst
);
675 switch (get_op(inst
)) {
681 dar
= kvmppc_get_gpr(vcpu
, ra
);
682 dar
+= (s32
)((s16
)inst
);
686 dar
= kvmppc_get_gpr(vcpu
, ra
);
687 dar
+= kvmppc_get_gpr(vcpu
, rb
);
690 printk(KERN_INFO
"KVM: Unaligned instruction 0x%x\n", inst
);