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>
26 #define OP_19_XOP_RFID 18
27 #define OP_19_XOP_RFI 50
29 #define OP_31_XOP_MFMSR 83
30 #define OP_31_XOP_MTMSR 146
31 #define OP_31_XOP_MTMSRD 178
32 #define OP_31_XOP_MTSR 210
33 #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
39 #define OP_31_XOP_MFSR 595
40 #define OP_31_XOP_MFSRIN 659
41 #define OP_31_XOP_DCBA 758
42 #define OP_31_XOP_SLBMFEV 851
43 #define OP_31_XOP_EIOIO 854
44 #define OP_31_XOP_SLBMFEE 915
46 /* DCBZ is actually 1014, but we patch it to 1010 so we get a trap */
47 #define OP_31_XOP_DCBZ 1010
63 /* Book3S_32 defines mfsrin(v) - but that messes up our abstract
64 * function pointers, so let's just disable the define. */
73 static bool spr_allowed(struct kvm_vcpu
*vcpu
, enum priv_level level
)
75 /* PAPR VMs only access supervisor SPRs */
76 if (vcpu
->arch
.papr_enabled
&& (level
> PRIV_SUPER
))
79 /* Limit user space to its own small SPR set */
80 if ((vcpu
->arch
.shared
->msr
& MSR_PR
) && level
> PRIV_PROBLEM
)
86 int kvmppc_core_emulate_op(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
87 unsigned int inst
, int *advance
)
89 int emulated
= EMULATE_DONE
;
90 int rt
= get_rt(inst
);
91 int rs
= get_rs(inst
);
92 int ra
= get_ra(inst
);
93 int rb
= get_rb(inst
);
95 switch (get_op(inst
)) {
97 switch (get_xop(inst
)) {
100 kvmppc_set_pc(vcpu
, vcpu
->arch
.shared
->srr0
);
101 kvmppc_set_msr(vcpu
, vcpu
->arch
.shared
->srr1
);
106 emulated
= EMULATE_FAIL
;
111 switch (get_xop(inst
)) {
112 case OP_31_XOP_MFMSR
:
113 kvmppc_set_gpr(vcpu
, rt
, vcpu
->arch
.shared
->msr
);
115 case OP_31_XOP_MTMSRD
:
117 ulong rs_val
= kvmppc_get_gpr(vcpu
, rs
);
118 if (inst
& 0x10000) {
119 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
;
124 kvmppc_set_msr(vcpu
, rs_val
);
127 case OP_31_XOP_MTMSR
:
128 kvmppc_set_msr(vcpu
, kvmppc_get_gpr(vcpu
, rs
));
134 srnum
= kvmppc_get_field(inst
, 12 + 32, 15 + 32);
135 if (vcpu
->arch
.mmu
.mfsrin
) {
137 sr
= vcpu
->arch
.mmu
.mfsrin(vcpu
, srnum
);
138 kvmppc_set_gpr(vcpu
, rt
, sr
);
142 case OP_31_XOP_MFSRIN
:
146 srnum
= (kvmppc_get_gpr(vcpu
, rb
) >> 28) & 0xf;
147 if (vcpu
->arch
.mmu
.mfsrin
) {
149 sr
= vcpu
->arch
.mmu
.mfsrin(vcpu
, srnum
);
150 kvmppc_set_gpr(vcpu
, rt
, sr
);
155 vcpu
->arch
.mmu
.mtsrin(vcpu
,
157 kvmppc_get_gpr(vcpu
, rs
));
159 case OP_31_XOP_MTSRIN
:
160 vcpu
->arch
.mmu
.mtsrin(vcpu
,
161 (kvmppc_get_gpr(vcpu
, rb
) >> 28) & 0xf,
162 kvmppc_get_gpr(vcpu
, rs
));
164 case OP_31_XOP_TLBIE
:
165 case OP_31_XOP_TLBIEL
:
167 bool large
= (inst
& 0x00200000) ? true : false;
168 ulong addr
= kvmppc_get_gpr(vcpu
, rb
);
169 vcpu
->arch
.mmu
.tlbie(vcpu
, addr
, large
);
172 case OP_31_XOP_EIOIO
:
174 case OP_31_XOP_SLBMTE
:
175 if (!vcpu
->arch
.mmu
.slbmte
)
178 vcpu
->arch
.mmu
.slbmte(vcpu
,
179 kvmppc_get_gpr(vcpu
, rs
),
180 kvmppc_get_gpr(vcpu
, rb
));
182 case OP_31_XOP_SLBIE
:
183 if (!vcpu
->arch
.mmu
.slbie
)
186 vcpu
->arch
.mmu
.slbie(vcpu
,
187 kvmppc_get_gpr(vcpu
, rb
));
189 case OP_31_XOP_SLBIA
:
190 if (!vcpu
->arch
.mmu
.slbia
)
193 vcpu
->arch
.mmu
.slbia(vcpu
);
195 case OP_31_XOP_SLBMFEE
:
196 if (!vcpu
->arch
.mmu
.slbmfee
) {
197 emulated
= EMULATE_FAIL
;
201 rb_val
= kvmppc_get_gpr(vcpu
, rb
);
202 t
= vcpu
->arch
.mmu
.slbmfee(vcpu
, rb_val
);
203 kvmppc_set_gpr(vcpu
, rt
, t
);
206 case OP_31_XOP_SLBMFEV
:
207 if (!vcpu
->arch
.mmu
.slbmfev
) {
208 emulated
= EMULATE_FAIL
;
212 rb_val
= kvmppc_get_gpr(vcpu
, rb
);
213 t
= vcpu
->arch
.mmu
.slbmfev(vcpu
, rb_val
);
214 kvmppc_set_gpr(vcpu
, rt
, t
);
218 /* Gets treated as NOP */
222 ulong rb_val
= kvmppc_get_gpr(vcpu
, rb
);
225 u32 zeros
[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
230 ra_val
= kvmppc_get_gpr(vcpu
, ra
);
232 addr
= (ra_val
+ rb_val
) & ~31ULL;
233 if (!(vcpu
->arch
.shared
->msr
& MSR_SF
))
237 r
= kvmppc_st(vcpu
, &addr
, 32, zeros
, true);
238 if ((r
== -ENOENT
) || (r
== -EPERM
)) {
239 struct kvmppc_book3s_shadow_vcpu
*svcpu
;
241 svcpu
= svcpu_get(vcpu
);
243 vcpu
->arch
.shared
->dar
= vaddr
;
244 svcpu
->fault_dar
= vaddr
;
246 dsisr
= DSISR_ISSTORE
;
248 dsisr
|= DSISR_NOHPTE
;
249 else if (r
== -EPERM
)
250 dsisr
|= DSISR_PROTFAULT
;
252 vcpu
->arch
.shared
->dsisr
= dsisr
;
253 svcpu
->fault_dsisr
= dsisr
;
256 kvmppc_book3s_queue_irqprio(vcpu
,
257 BOOK3S_INTERRUPT_DATA_STORAGE
);
263 emulated
= EMULATE_FAIL
;
267 emulated
= EMULATE_FAIL
;
270 if (emulated
== EMULATE_FAIL
)
271 emulated
= kvmppc_emulate_paired_single(run
, vcpu
);
276 void kvmppc_set_bat(struct kvm_vcpu
*vcpu
, struct kvmppc_bat
*bat
, bool upper
,
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
;
289 bat
->brpn
= val
& 0xfffe0000;
290 bat
->wimg
= (val
>> 3) & 0xf;
292 bat
->raw
= (bat
->raw
& 0x00000000ffffffffULL
) | ((u64
)val
<< 32);
296 static struct kvmppc_bat
*kvmppc_find_bat(struct kvm_vcpu
*vcpu
, int sprn
)
298 struct kvmppc_vcpu_book3s
*vcpu_book3s
= to_book3s(vcpu
);
299 struct kvmppc_bat
*bat
;
302 case SPRN_IBAT0U
... SPRN_IBAT3L
:
303 bat
= &vcpu_book3s
->ibat
[(sprn
- SPRN_IBAT0U
) / 2];
305 case SPRN_IBAT4U
... SPRN_IBAT7L
:
306 bat
= &vcpu_book3s
->ibat
[4 + ((sprn
- SPRN_IBAT4U
) / 2)];
308 case SPRN_DBAT0U
... SPRN_DBAT3L
:
309 bat
= &vcpu_book3s
->dbat
[(sprn
- SPRN_DBAT0U
) / 2];
311 case SPRN_DBAT4U
... SPRN_DBAT7L
:
312 bat
= &vcpu_book3s
->dbat
[4 + ((sprn
- SPRN_DBAT4U
) / 2)];
321 int kvmppc_core_emulate_mtspr(struct kvm_vcpu
*vcpu
, int sprn
, ulong spr_val
)
323 int emulated
= EMULATE_DONE
;
327 if (!spr_allowed(vcpu
, PRIV_HYPER
))
329 to_book3s(vcpu
)->sdr1
= spr_val
;
332 vcpu
->arch
.shared
->dsisr
= spr_val
;
335 vcpu
->arch
.shared
->dar
= spr_val
;
338 to_book3s(vcpu
)->hior
= spr_val
;
340 case SPRN_IBAT0U
... SPRN_IBAT3L
:
341 case SPRN_IBAT4U
... SPRN_IBAT7L
:
342 case SPRN_DBAT0U
... SPRN_DBAT3L
:
343 case SPRN_DBAT4U
... SPRN_DBAT7L
:
345 struct kvmppc_bat
*bat
= kvmppc_find_bat(vcpu
, sprn
);
347 kvmppc_set_bat(vcpu
, bat
, !(sprn
% 2), (u32
)spr_val
);
348 /* BAT writes happen so rarely that we're ok to flush
350 kvmppc_mmu_pte_flush(vcpu
, 0, 0);
351 kvmppc_mmu_flush_segments(vcpu
);
355 to_book3s(vcpu
)->hid
[0] = spr_val
;
358 to_book3s(vcpu
)->hid
[1] = spr_val
;
361 to_book3s(vcpu
)->hid
[2] = spr_val
;
363 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 */
375 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 */
379 vcpu
->arch
.hflags
|= BOOK3S_HFLAG_PAIRED_SINGLE
;
380 kvmppc_giveup_ext(vcpu
, MSR_FP
);
382 vcpu
->arch
.hflags
&= ~BOOK3S_HFLAG_PAIRED_SINGLE
;
388 case SPRN_HID4_GEKKO
:
389 to_book3s(vcpu
)->hid
[4] = spr_val
;
392 to_book3s(vcpu
)->hid
[5] = spr_val
;
393 /* guest HID5 set can change is_dcbz32 */
394 if (vcpu
->arch
.mmu
.is_dcbz32(vcpu
) &&
396 vcpu
->arch
.hflags
|= BOOK3S_HFLAG_DCBZ32
;
406 to_book3s(vcpu
)->gqr
[sprn
- SPRN_GQR0
] = spr_val
;
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
:
425 printk(KERN_INFO
"KVM: invalid SPR write: %d\n", sprn
);
427 emulated
= EMULATE_FAIL
;
435 int kvmppc_core_emulate_mfspr(struct kvm_vcpu
*vcpu
, int sprn
, ulong
*spr_val
)
437 int emulated
= EMULATE_DONE
;
440 case SPRN_IBAT0U
... SPRN_IBAT3L
:
441 case SPRN_IBAT4U
... SPRN_IBAT7L
:
442 case SPRN_DBAT0U
... SPRN_DBAT3L
:
443 case SPRN_DBAT4U
... SPRN_DBAT7L
:
445 struct kvmppc_bat
*bat
= kvmppc_find_bat(vcpu
, sprn
);
448 *spr_val
= bat
->raw
>> 32;
455 if (!spr_allowed(vcpu
, PRIV_HYPER
))
457 *spr_val
= to_book3s(vcpu
)->sdr1
;
460 *spr_val
= vcpu
->arch
.shared
->dsisr
;
463 *spr_val
= vcpu
->arch
.shared
->dar
;
466 *spr_val
= to_book3s(vcpu
)->hior
;
469 *spr_val
= to_book3s(vcpu
)->hid
[0];
472 *spr_val
= to_book3s(vcpu
)->hid
[1];
475 case SPRN_HID2_GEKKO
:
476 *spr_val
= to_book3s(vcpu
)->hid
[2];
479 case SPRN_HID4_GEKKO
:
480 *spr_val
= to_book3s(vcpu
)->hid
[4];
483 *spr_val
= to_book3s(vcpu
)->hid
[5];
497 *spr_val
= to_book3s(vcpu
)->gqr
[sprn
- SPRN_GQR0
];
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
:
516 printk(KERN_INFO
"KVM: invalid SPR read: %d\n", sprn
);
518 emulated
= EMULATE_FAIL
;
526 u32
kvmppc_alignment_dsisr(struct kvm_vcpu
*vcpu
, unsigned int inst
)
531 * This is what the spec says about DSISR bits (not mentioned = 0):
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)
543 switch (get_op(inst
)) {
549 dsisr
|= (inst
>> 12) & 0x4000; /* bit 17 */
550 dsisr
|= (inst
>> 17) & 0x3c00; /* bits 18:21 */
554 dsisr
|= (inst
<< 14) & 0x18000; /* bits 15:16 */
555 dsisr
|= (inst
<< 8) & 0x04000; /* bit 17 */
556 dsisr
|= (inst
<< 3) & 0x03c00; /* bits 18:21 */
559 printk(KERN_INFO
"KVM: Unaligned instruction 0x%x\n", inst
);
563 dsisr
|= (inst
>> 16) & 0x03ff; /* bits 22:31 */
568 ulong
kvmppc_alignment_dar(struct kvm_vcpu
*vcpu
, unsigned int inst
)
571 ulong ra
= get_ra(inst
);
572 ulong rb
= get_rb(inst
);
574 switch (get_op(inst
)) {
580 dar
= kvmppc_get_gpr(vcpu
, ra
);
581 dar
+= (s32
)((s16
)inst
);
585 dar
= kvmppc_get_gpr(vcpu
, ra
);
586 dar
+= kvmppc_get_gpr(vcpu
, rb
);
589 printk(KERN_INFO
"KVM: Unaligned instruction 0x%x\n", inst
);