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>
28 #define OP_19_XOP_RFID 18
29 #define OP_19_XOP_RFI 50
31 #define OP_31_XOP_MFMSR 83
32 #define OP_31_XOP_MTMSR 146
33 #define OP_31_XOP_MTMSRD 178
34 #define OP_31_XOP_MTSR 210
35 #define OP_31_XOP_MTSRIN 242
36 #define OP_31_XOP_TLBIEL 274
37 #define OP_31_XOP_TLBIE 306
38 /* Opcode is officially reserved, reuse it as sc 1 when sc 1 doesn't trap */
39 #define OP_31_XOP_FAKE_SC1 308
40 #define OP_31_XOP_SLBMTE 402
41 #define OP_31_XOP_SLBIE 434
42 #define OP_31_XOP_SLBIA 498
43 #define OP_31_XOP_MFSR 595
44 #define OP_31_XOP_MFSRIN 659
45 #define OP_31_XOP_DCBA 758
46 #define OP_31_XOP_SLBMFEV 851
47 #define OP_31_XOP_EIOIO 854
48 #define OP_31_XOP_SLBMFEE 915
50 /* DCBZ is actually 1014, but we patch it to 1010 so we get a trap */
51 #define OP_31_XOP_DCBZ 1010
67 /* Book3S_32 defines mfsrin(v) - but that messes up our abstract
68 * function pointers, so let's just disable the define. */
77 static bool spr_allowed(struct kvm_vcpu
*vcpu
, enum priv_level level
)
79 /* PAPR VMs only access supervisor SPRs */
80 if (vcpu
->arch
.papr_enabled
&& (level
> PRIV_SUPER
))
83 /* Limit user space to its own small SPR set */
84 if ((kvmppc_get_msr(vcpu
) & MSR_PR
) && level
> PRIV_PROBLEM
)
90 int kvmppc_core_emulate_op_pr(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
91 unsigned int inst
, int *advance
)
93 int emulated
= EMULATE_DONE
;
94 int rt
= get_rt(inst
);
95 int rs
= get_rs(inst
);
96 int ra
= get_ra(inst
);
97 int rb
= get_rb(inst
);
98 u32 inst_sc
= 0x44000002;
100 switch (get_op(inst
)) {
102 emulated
= EMULATE_FAIL
;
103 if ((kvmppc_get_msr(vcpu
) & MSR_LE
) &&
104 (inst
== swab32(inst_sc
))) {
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.
112 kvmppc_set_gpr(vcpu
, 3, EV_UNIMPLEMENTED
);
113 kvmppc_set_pc(vcpu
, kvmppc_get_pc(vcpu
) + 4);
114 emulated
= EMULATE_DONE
;
118 switch (get_xop(inst
)) {
121 kvmppc_set_pc(vcpu
, kvmppc_get_srr0(vcpu
));
122 kvmppc_set_msr(vcpu
, kvmppc_get_srr1(vcpu
));
127 emulated
= EMULATE_FAIL
;
132 switch (get_xop(inst
)) {
133 case OP_31_XOP_MFMSR
:
134 kvmppc_set_gpr(vcpu
, rt
, kvmppc_get_msr(vcpu
));
136 case OP_31_XOP_MTMSRD
:
138 ulong rs_val
= kvmppc_get_gpr(vcpu
, rs
);
139 if (inst
& 0x10000) {
140 ulong new_msr
= kvmppc_get_msr(vcpu
);
141 new_msr
&= ~(MSR_RI
| MSR_EE
);
142 new_msr
|= rs_val
& (MSR_RI
| MSR_EE
);
143 kvmppc_set_msr_fast(vcpu
, new_msr
);
145 kvmppc_set_msr(vcpu
, rs_val
);
148 case OP_31_XOP_MTMSR
:
149 kvmppc_set_msr(vcpu
, kvmppc_get_gpr(vcpu
, rs
));
155 srnum
= kvmppc_get_field(inst
, 12 + 32, 15 + 32);
156 if (vcpu
->arch
.mmu
.mfsrin
) {
158 sr
= vcpu
->arch
.mmu
.mfsrin(vcpu
, srnum
);
159 kvmppc_set_gpr(vcpu
, rt
, sr
);
163 case OP_31_XOP_MFSRIN
:
167 srnum
= (kvmppc_get_gpr(vcpu
, rb
) >> 28) & 0xf;
168 if (vcpu
->arch
.mmu
.mfsrin
) {
170 sr
= vcpu
->arch
.mmu
.mfsrin(vcpu
, srnum
);
171 kvmppc_set_gpr(vcpu
, rt
, sr
);
176 vcpu
->arch
.mmu
.mtsrin(vcpu
,
178 kvmppc_get_gpr(vcpu
, rs
));
180 case OP_31_XOP_MTSRIN
:
181 vcpu
->arch
.mmu
.mtsrin(vcpu
,
182 (kvmppc_get_gpr(vcpu
, rb
) >> 28) & 0xf,
183 kvmppc_get_gpr(vcpu
, rs
));
185 case OP_31_XOP_TLBIE
:
186 case OP_31_XOP_TLBIEL
:
188 bool large
= (inst
& 0x00200000) ? true : false;
189 ulong addr
= kvmppc_get_gpr(vcpu
, rb
);
190 vcpu
->arch
.mmu
.tlbie(vcpu
, addr
, large
);
193 #ifdef CONFIG_PPC_BOOK3S_64
194 case OP_31_XOP_FAKE_SC1
:
196 /* SC 1 papr hypercalls */
197 ulong cmd
= kvmppc_get_gpr(vcpu
, 3);
200 if ((kvmppc_get_msr(vcpu
) & MSR_PR
) ||
201 !vcpu
->arch
.papr_enabled
) {
202 emulated
= EMULATE_FAIL
;
206 if (kvmppc_h_pr(vcpu
, cmd
) == EMULATE_DONE
)
209 run
->papr_hcall
.nr
= cmd
;
210 for (i
= 0; i
< 9; ++i
) {
211 ulong gpr
= kvmppc_get_gpr(vcpu
, 4 + i
);
212 run
->papr_hcall
.args
[i
] = gpr
;
215 run
->exit_reason
= KVM_EXIT_PAPR_HCALL
;
216 vcpu
->arch
.hcall_needed
= 1;
217 emulated
= EMULATE_EXIT_USER
;
221 case OP_31_XOP_EIOIO
:
223 case OP_31_XOP_SLBMTE
:
224 if (!vcpu
->arch
.mmu
.slbmte
)
227 vcpu
->arch
.mmu
.slbmte(vcpu
,
228 kvmppc_get_gpr(vcpu
, rs
),
229 kvmppc_get_gpr(vcpu
, rb
));
231 case OP_31_XOP_SLBIE
:
232 if (!vcpu
->arch
.mmu
.slbie
)
235 vcpu
->arch
.mmu
.slbie(vcpu
,
236 kvmppc_get_gpr(vcpu
, rb
));
238 case OP_31_XOP_SLBIA
:
239 if (!vcpu
->arch
.mmu
.slbia
)
242 vcpu
->arch
.mmu
.slbia(vcpu
);
244 case OP_31_XOP_SLBMFEE
:
245 if (!vcpu
->arch
.mmu
.slbmfee
) {
246 emulated
= EMULATE_FAIL
;
250 rb_val
= kvmppc_get_gpr(vcpu
, rb
);
251 t
= vcpu
->arch
.mmu
.slbmfee(vcpu
, rb_val
);
252 kvmppc_set_gpr(vcpu
, rt
, t
);
255 case OP_31_XOP_SLBMFEV
:
256 if (!vcpu
->arch
.mmu
.slbmfev
) {
257 emulated
= EMULATE_FAIL
;
261 rb_val
= kvmppc_get_gpr(vcpu
, rb
);
262 t
= vcpu
->arch
.mmu
.slbmfev(vcpu
, rb_val
);
263 kvmppc_set_gpr(vcpu
, rt
, t
);
267 /* Gets treated as NOP */
271 ulong rb_val
= kvmppc_get_gpr(vcpu
, rb
);
274 u32 zeros
[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
279 ra_val
= kvmppc_get_gpr(vcpu
, ra
);
281 addr
= (ra_val
+ rb_val
) & ~31ULL;
282 if (!(kvmppc_get_msr(vcpu
) & MSR_SF
))
286 r
= kvmppc_st(vcpu
, &addr
, 32, zeros
, true);
287 if ((r
== -ENOENT
) || (r
== -EPERM
)) {
289 kvmppc_set_dar(vcpu
, vaddr
);
290 vcpu
->arch
.fault_dar
= vaddr
;
292 dsisr
= DSISR_ISSTORE
;
294 dsisr
|= DSISR_NOHPTE
;
295 else if (r
== -EPERM
)
296 dsisr
|= DSISR_PROTFAULT
;
298 kvmppc_set_dsisr(vcpu
, dsisr
);
299 vcpu
->arch
.fault_dsisr
= dsisr
;
301 kvmppc_book3s_queue_irqprio(vcpu
,
302 BOOK3S_INTERRUPT_DATA_STORAGE
);
308 emulated
= EMULATE_FAIL
;
312 emulated
= EMULATE_FAIL
;
315 if (emulated
== EMULATE_FAIL
)
316 emulated
= kvmppc_emulate_paired_single(run
, vcpu
);
321 void kvmppc_set_bat(struct kvm_vcpu
*vcpu
, struct kvmppc_bat
*bat
, bool upper
,
326 u32 bl
= (val
>> 2) & 0x7ff;
327 bat
->bepi_mask
= (~bl
<< 17);
328 bat
->bepi
= val
& 0xfffe0000;
329 bat
->vs
= (val
& 2) ? 1 : 0;
330 bat
->vp
= (val
& 1) ? 1 : 0;
331 bat
->raw
= (bat
->raw
& 0xffffffff00000000ULL
) | val
;
334 bat
->brpn
= val
& 0xfffe0000;
335 bat
->wimg
= (val
>> 3) & 0xf;
337 bat
->raw
= (bat
->raw
& 0x00000000ffffffffULL
) | ((u64
)val
<< 32);
341 static struct kvmppc_bat
*kvmppc_find_bat(struct kvm_vcpu
*vcpu
, int sprn
)
343 struct kvmppc_vcpu_book3s
*vcpu_book3s
= to_book3s(vcpu
);
344 struct kvmppc_bat
*bat
;
347 case SPRN_IBAT0U
... SPRN_IBAT3L
:
348 bat
= &vcpu_book3s
->ibat
[(sprn
- SPRN_IBAT0U
) / 2];
350 case SPRN_IBAT4U
... SPRN_IBAT7L
:
351 bat
= &vcpu_book3s
->ibat
[4 + ((sprn
- SPRN_IBAT4U
) / 2)];
353 case SPRN_DBAT0U
... SPRN_DBAT3L
:
354 bat
= &vcpu_book3s
->dbat
[(sprn
- SPRN_DBAT0U
) / 2];
356 case SPRN_DBAT4U
... SPRN_DBAT7L
:
357 bat
= &vcpu_book3s
->dbat
[4 + ((sprn
- SPRN_DBAT4U
) / 2)];
366 int kvmppc_core_emulate_mtspr_pr(struct kvm_vcpu
*vcpu
, int sprn
, ulong spr_val
)
368 int emulated
= EMULATE_DONE
;
372 if (!spr_allowed(vcpu
, PRIV_HYPER
))
374 to_book3s(vcpu
)->sdr1
= spr_val
;
377 kvmppc_set_dsisr(vcpu
, spr_val
);
380 kvmppc_set_dar(vcpu
, spr_val
);
383 to_book3s(vcpu
)->hior
= spr_val
;
385 case SPRN_IBAT0U
... SPRN_IBAT3L
:
386 case SPRN_IBAT4U
... SPRN_IBAT7L
:
387 case SPRN_DBAT0U
... SPRN_DBAT3L
:
388 case SPRN_DBAT4U
... SPRN_DBAT7L
:
390 struct kvmppc_bat
*bat
= kvmppc_find_bat(vcpu
, sprn
);
392 kvmppc_set_bat(vcpu
, bat
, !(sprn
% 2), (u32
)spr_val
);
393 /* BAT writes happen so rarely that we're ok to flush
395 kvmppc_mmu_pte_flush(vcpu
, 0, 0);
396 kvmppc_mmu_flush_segments(vcpu
);
400 to_book3s(vcpu
)->hid
[0] = spr_val
;
403 to_book3s(vcpu
)->hid
[1] = spr_val
;
406 to_book3s(vcpu
)->hid
[2] = spr_val
;
408 case SPRN_HID2_GEKKO
:
409 to_book3s(vcpu
)->hid
[2] = spr_val
;
410 /* HID2.PSE controls paired single on gekko */
411 switch (vcpu
->arch
.pvr
) {
412 case 0x00080200: /* lonestar 2.0 */
413 case 0x00088202: /* lonestar 2.2 */
414 case 0x70000100: /* gekko 1.0 */
415 case 0x00080100: /* gekko 2.0 */
416 case 0x00083203: /* gekko 2.3a */
417 case 0x00083213: /* gekko 2.3b */
418 case 0x00083204: /* gekko 2.4 */
419 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
420 case 0x00087200: /* broadway */
421 if (vcpu
->arch
.hflags
& BOOK3S_HFLAG_NATIVE_PS
) {
422 /* Native paired singles */
423 } else if (spr_val
& (1 << 29)) { /* HID2.PSE */
424 vcpu
->arch
.hflags
|= BOOK3S_HFLAG_PAIRED_SINGLE
;
425 kvmppc_giveup_ext(vcpu
, MSR_FP
);
427 vcpu
->arch
.hflags
&= ~BOOK3S_HFLAG_PAIRED_SINGLE
;
433 case SPRN_HID4_GEKKO
:
434 to_book3s(vcpu
)->hid
[4] = spr_val
;
437 to_book3s(vcpu
)->hid
[5] = spr_val
;
438 /* guest HID5 set can change is_dcbz32 */
439 if (vcpu
->arch
.mmu
.is_dcbz32(vcpu
) &&
441 vcpu
->arch
.hflags
|= BOOK3S_HFLAG_DCBZ32
;
451 to_book3s(vcpu
)->gqr
[sprn
- SPRN_GQR0
] = spr_val
;
453 #ifdef CONFIG_PPC_BOOK3S_64
455 kvmppc_set_fscr(vcpu
, spr_val
);
458 vcpu
->arch
.bescr
= spr_val
;
461 vcpu
->arch
.ebbhr
= spr_val
;
464 vcpu
->arch
.ebbrr
= spr_val
;
466 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
468 vcpu
->arch
.tfhar
= spr_val
;
471 vcpu
->arch
.texasr
= spr_val
;
474 vcpu
->arch
.tfiar
= spr_val
;
486 case SPRN_MMCR0_GEKKO
:
487 case SPRN_MMCR1_GEKKO
:
488 case SPRN_PMC1_GEKKO
:
489 case SPRN_PMC2_GEKKO
:
490 case SPRN_PMC3_GEKKO
:
491 case SPRN_PMC4_GEKKO
:
492 case SPRN_WPAR_GEKKO
:
495 #ifdef CONFIG_PPC_BOOK3S_64
506 pr_info_ratelimited("KVM: invalid SPR write: %d\n", sprn
);
508 if (kvmppc_get_msr(vcpu
) & MSR_PR
) {
509 kvmppc_core_queue_program(vcpu
, SRR1_PROGPRIV
);
510 emulated
= EMULATE_AGAIN
;
513 if ((kvmppc_get_msr(vcpu
) & MSR_PR
) || sprn
== 0) {
514 kvmppc_core_queue_program(vcpu
, SRR1_PROGILL
);
515 emulated
= EMULATE_AGAIN
;
524 int kvmppc_core_emulate_mfspr_pr(struct kvm_vcpu
*vcpu
, int sprn
, ulong
*spr_val
)
526 int emulated
= EMULATE_DONE
;
529 case SPRN_IBAT0U
... SPRN_IBAT3L
:
530 case SPRN_IBAT4U
... SPRN_IBAT7L
:
531 case SPRN_DBAT0U
... SPRN_DBAT3L
:
532 case SPRN_DBAT4U
... SPRN_DBAT7L
:
534 struct kvmppc_bat
*bat
= kvmppc_find_bat(vcpu
, sprn
);
537 *spr_val
= bat
->raw
>> 32;
544 if (!spr_allowed(vcpu
, PRIV_HYPER
))
546 *spr_val
= to_book3s(vcpu
)->sdr1
;
549 *spr_val
= kvmppc_get_dsisr(vcpu
);
552 *spr_val
= kvmppc_get_dar(vcpu
);
555 *spr_val
= to_book3s(vcpu
)->hior
;
558 *spr_val
= to_book3s(vcpu
)->hid
[0];
561 *spr_val
= to_book3s(vcpu
)->hid
[1];
564 case SPRN_HID2_GEKKO
:
565 *spr_val
= to_book3s(vcpu
)->hid
[2];
568 case SPRN_HID4_GEKKO
:
569 *spr_val
= to_book3s(vcpu
)->hid
[4];
572 *spr_val
= to_book3s(vcpu
)->hid
[5];
580 * On exit we would have updated purr
582 *spr_val
= vcpu
->arch
.purr
;
586 * On exit we would have updated spurr
588 *spr_val
= vcpu
->arch
.spurr
;
591 *spr_val
= to_book3s(vcpu
)->vtb
;
594 *spr_val
= vcpu
->arch
.ic
;
604 *spr_val
= to_book3s(vcpu
)->gqr
[sprn
- SPRN_GQR0
];
606 #ifdef CONFIG_PPC_BOOK3S_64
608 *spr_val
= vcpu
->arch
.fscr
;
611 *spr_val
= vcpu
->arch
.bescr
;
614 *spr_val
= vcpu
->arch
.ebbhr
;
617 *spr_val
= vcpu
->arch
.ebbrr
;
619 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
621 *spr_val
= vcpu
->arch
.tfhar
;
624 *spr_val
= vcpu
->arch
.texasr
;
627 *spr_val
= vcpu
->arch
.tfiar
;
637 case SPRN_MMCR0_GEKKO
:
638 case SPRN_MMCR1_GEKKO
:
639 case SPRN_PMC1_GEKKO
:
640 case SPRN_PMC2_GEKKO
:
641 case SPRN_PMC3_GEKKO
:
642 case SPRN_PMC4_GEKKO
:
643 case SPRN_WPAR_GEKKO
:
646 #ifdef CONFIG_PPC_BOOK3S_64
659 pr_info_ratelimited("KVM: invalid SPR read: %d\n", sprn
);
661 if (kvmppc_get_msr(vcpu
) & MSR_PR
) {
662 kvmppc_core_queue_program(vcpu
, SRR1_PROGPRIV
);
663 emulated
= EMULATE_AGAIN
;
666 if ((kvmppc_get_msr(vcpu
) & MSR_PR
) || sprn
== 0 ||
667 sprn
== 4 || sprn
== 5 || sprn
== 6) {
668 kvmppc_core_queue_program(vcpu
, SRR1_PROGILL
);
669 emulated
= EMULATE_AGAIN
;
679 u32
kvmppc_alignment_dsisr(struct kvm_vcpu
*vcpu
, unsigned int inst
)
681 return make_dsisr(inst
);
684 ulong
kvmppc_alignment_dar(struct kvm_vcpu
*vcpu
, unsigned int inst
)
686 #ifdef CONFIG_PPC_BOOK3S_64
688 * Linux's fix_alignment() assumes that DAR is valid, so can we
690 return vcpu
->arch
.fault_dar
;
693 ulong ra
= get_ra(inst
);
694 ulong rb
= get_rb(inst
);
696 switch (get_op(inst
)) {
702 dar
= kvmppc_get_gpr(vcpu
, ra
);
703 dar
+= (s32
)((s16
)inst
);
707 dar
= kvmppc_get_gpr(vcpu
, ra
);
708 dar
+= kvmppc_get_gpr(vcpu
, rb
);
711 printk(KERN_INFO
"KVM: Unaligned instruction 0x%x\n", inst
);