2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * KVM/MIPS: Support for hardware virtualization extensions
8 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
9 * Authors: Yann Le Du <ledu@kymasys.com>
12 #include <linux/errno.h>
13 #include <linux/err.h>
14 #include <linux/module.h>
15 #include <linux/preempt.h>
16 #include <linux/vmalloc.h>
17 #include <asm/cacheflush.h>
18 #include <asm/cacheops.h>
19 #include <asm/cmpxchg.h>
21 #include <asm/hazards.h>
23 #include <asm/mmu_context.h>
24 #include <asm/r4kcache.h>
27 #include <asm/tlbex.h>
29 #include <linux/kvm_host.h>
31 #include "interrupt.h"
32 #ifdef CONFIG_CPU_LOONGSON64
33 #include "loongson_regs.h"
38 /* Pointers to last VCPU loaded on each physical CPU */
39 static struct kvm_vcpu
*last_vcpu
[NR_CPUS
];
40 /* Pointers to last VCPU executed on each physical CPU */
41 static struct kvm_vcpu
*last_exec_vcpu
[NR_CPUS
];
44 * Number of guest VTLB entries to use, so we can catch inconsistency between
47 static unsigned int kvm_vz_guest_vtlb_size
;
49 static inline long kvm_vz_read_gc0_ebase(void)
51 if (sizeof(long) == 8 && cpu_has_ebase_wg
)
52 return read_gc0_ebase_64();
54 return read_gc0_ebase();
57 static inline void kvm_vz_write_gc0_ebase(long v
)
60 * First write with WG=1 to write upper bits, then write again in case
61 * WG should be left at 0.
62 * write_gc0_ebase_64() is no longer UNDEFINED since R6.
64 if (sizeof(long) == 8 &&
65 (cpu_has_mips64r6
|| cpu_has_ebase_wg
)) {
66 write_gc0_ebase_64(v
| MIPS_EBASE_WG
);
67 write_gc0_ebase_64(v
);
69 write_gc0_ebase(v
| MIPS_EBASE_WG
);
75 * These Config bits may be writable by the guest:
76 * Config: [K23, KU] (!TLB), K0
78 * Config2: [TU, SU] (impl)
80 * Config4: FTLBPageSize
81 * Config5: K, CV, MSAEn, UFE, FRE, SBRI, UFR
84 static inline unsigned int kvm_vz_config_guest_wrmask(struct kvm_vcpu
*vcpu
)
89 static inline unsigned int kvm_vz_config1_guest_wrmask(struct kvm_vcpu
*vcpu
)
94 static inline unsigned int kvm_vz_config2_guest_wrmask(struct kvm_vcpu
*vcpu
)
99 static inline unsigned int kvm_vz_config3_guest_wrmask(struct kvm_vcpu
*vcpu
)
101 return MIPS_CONF3_ISA_OE
;
104 static inline unsigned int kvm_vz_config4_guest_wrmask(struct kvm_vcpu
*vcpu
)
106 /* no need to be exact */
107 return MIPS_CONF4_VFTLBPAGESIZE
;
110 static inline unsigned int kvm_vz_config5_guest_wrmask(struct kvm_vcpu
*vcpu
)
112 unsigned int mask
= MIPS_CONF5_K
| MIPS_CONF5_CV
| MIPS_CONF5_SBRI
;
114 /* Permit MSAEn changes if MSA supported and enabled */
115 if (kvm_mips_guest_has_msa(&vcpu
->arch
))
116 mask
|= MIPS_CONF5_MSAEN
;
119 * Permit guest FPU mode changes if FPU is enabled and the relevant
120 * feature exists according to FIR register.
122 if (kvm_mips_guest_has_fpu(&vcpu
->arch
)) {
124 mask
|= MIPS_CONF5_UFR
;
126 mask
|= MIPS_CONF5_FRE
| MIPS_CONF5_UFE
;
132 static inline unsigned int kvm_vz_config6_guest_wrmask(struct kvm_vcpu
*vcpu
)
134 return LOONGSON_CONF6_INTIMER
| LOONGSON_CONF6_EXTIMER
;
138 * VZ optionally allows these additional Config bits to be written by root:
140 * Config1: M, [MMUSize-1, C2, MD, PC, WR, CA], FP
142 * Config3: M, MSAP, [BPG], ULRI, [DSP2P, DSPP], CTXTC, [ITL, LPA, VEIC,
143 * VInt, SP, CDMM, MT, SM, TL]
144 * Config4: M, [VTLBSizeExt, MMUSizeExt]
148 static inline unsigned int kvm_vz_config_user_wrmask(struct kvm_vcpu
*vcpu
)
150 return kvm_vz_config_guest_wrmask(vcpu
) | MIPS_CONF_M
;
153 static inline unsigned int kvm_vz_config1_user_wrmask(struct kvm_vcpu
*vcpu
)
155 unsigned int mask
= kvm_vz_config1_guest_wrmask(vcpu
) | MIPS_CONF_M
;
157 /* Permit FPU to be present if FPU is supported */
158 if (kvm_mips_guest_can_have_fpu(&vcpu
->arch
))
159 mask
|= MIPS_CONF1_FP
;
164 static inline unsigned int kvm_vz_config2_user_wrmask(struct kvm_vcpu
*vcpu
)
166 return kvm_vz_config2_guest_wrmask(vcpu
) | MIPS_CONF_M
;
169 static inline unsigned int kvm_vz_config3_user_wrmask(struct kvm_vcpu
*vcpu
)
171 unsigned int mask
= kvm_vz_config3_guest_wrmask(vcpu
) | MIPS_CONF_M
|
172 MIPS_CONF3_ULRI
| MIPS_CONF3_CTXTC
;
174 /* Permit MSA to be present if MSA is supported */
175 if (kvm_mips_guest_can_have_msa(&vcpu
->arch
))
176 mask
|= MIPS_CONF3_MSA
;
181 static inline unsigned int kvm_vz_config4_user_wrmask(struct kvm_vcpu
*vcpu
)
183 return kvm_vz_config4_guest_wrmask(vcpu
) | MIPS_CONF_M
;
186 static inline unsigned int kvm_vz_config5_user_wrmask(struct kvm_vcpu
*vcpu
)
188 return kvm_vz_config5_guest_wrmask(vcpu
) | MIPS_CONF5_MRP
;
191 static inline unsigned int kvm_vz_config6_user_wrmask(struct kvm_vcpu
*vcpu
)
193 return kvm_vz_config6_guest_wrmask(vcpu
) |
194 LOONGSON_CONF6_SFBEN
| LOONGSON_CONF6_FTLBDIS
;
197 static gpa_t
kvm_vz_gva_to_gpa_cb(gva_t gva
)
199 /* VZ guest has already converted gva to gpa */
203 static void kvm_vz_queue_irq(struct kvm_vcpu
*vcpu
, unsigned int priority
)
205 set_bit(priority
, &vcpu
->arch
.pending_exceptions
);
206 clear_bit(priority
, &vcpu
->arch
.pending_exceptions_clr
);
209 static void kvm_vz_dequeue_irq(struct kvm_vcpu
*vcpu
, unsigned int priority
)
211 clear_bit(priority
, &vcpu
->arch
.pending_exceptions
);
212 set_bit(priority
, &vcpu
->arch
.pending_exceptions_clr
);
215 static void kvm_vz_queue_timer_int_cb(struct kvm_vcpu
*vcpu
)
218 * timer expiry is asynchronous to vcpu execution therefore defer guest
221 kvm_vz_queue_irq(vcpu
, MIPS_EXC_INT_TIMER
);
224 static void kvm_vz_dequeue_timer_int_cb(struct kvm_vcpu
*vcpu
)
227 * timer expiry is asynchronous to vcpu execution therefore defer guest
230 kvm_vz_dequeue_irq(vcpu
, MIPS_EXC_INT_TIMER
);
233 static void kvm_vz_queue_io_int_cb(struct kvm_vcpu
*vcpu
,
234 struct kvm_mips_interrupt
*irq
)
236 int intr
= (int)irq
->irq
;
239 * interrupts are asynchronous to vcpu execution therefore defer guest
242 kvm_vz_queue_irq(vcpu
, kvm_irq_to_priority(intr
));
245 static void kvm_vz_dequeue_io_int_cb(struct kvm_vcpu
*vcpu
,
246 struct kvm_mips_interrupt
*irq
)
248 int intr
= (int)irq
->irq
;
251 * interrupts are asynchronous to vcpu execution therefore defer guest
254 kvm_vz_dequeue_irq(vcpu
, kvm_irq_to_priority(-intr
));
257 static int kvm_vz_irq_deliver_cb(struct kvm_vcpu
*vcpu
, unsigned int priority
,
260 u32 irq
= (priority
< MIPS_EXC_MAX
) ?
261 kvm_priority_to_irq
[priority
] : 0;
264 case MIPS_EXC_INT_TIMER
:
268 case MIPS_EXC_INT_IO_1
:
269 case MIPS_EXC_INT_IO_2
:
270 case MIPS_EXC_INT_IPI_1
:
271 case MIPS_EXC_INT_IPI_2
:
272 if (cpu_has_guestctl2
)
273 set_c0_guestctl2(irq
);
282 clear_bit(priority
, &vcpu
->arch
.pending_exceptions
);
286 static int kvm_vz_irq_clear_cb(struct kvm_vcpu
*vcpu
, unsigned int priority
,
289 u32 irq
= (priority
< MIPS_EXC_MAX
) ?
290 kvm_priority_to_irq
[priority
] : 0;
293 case MIPS_EXC_INT_TIMER
:
295 * Explicitly clear irq associated with Cause.IP[IPTI]
296 * if GuestCtl2 virtual interrupt register not
297 * supported or if not using GuestCtl2 Hardware Clear.
299 if (cpu_has_guestctl2
) {
300 if (!(read_c0_guestctl2() & (irq
<< 14)))
301 clear_c0_guestctl2(irq
);
303 clear_gc0_cause(irq
);
307 case MIPS_EXC_INT_IO_1
:
308 case MIPS_EXC_INT_IO_2
:
309 case MIPS_EXC_INT_IPI_1
:
310 case MIPS_EXC_INT_IPI_2
:
311 /* Clear GuestCtl2.VIP irq if not using Hardware Clear */
312 if (cpu_has_guestctl2
) {
313 if (!(read_c0_guestctl2() & (irq
<< 14)))
314 clear_c0_guestctl2(irq
);
316 clear_gc0_cause(irq
);
324 clear_bit(priority
, &vcpu
->arch
.pending_exceptions_clr
);
329 * VZ guest timer handling.
333 * kvm_vz_should_use_htimer() - Find whether to use the VZ hard guest timer.
334 * @vcpu: Virtual CPU.
336 * Returns: true if the VZ GTOffset & real guest CP0_Count should be used
337 * instead of software emulation of guest timer.
340 static bool kvm_vz_should_use_htimer(struct kvm_vcpu
*vcpu
)
342 if (kvm_mips_count_disabled(vcpu
))
345 /* Chosen frequency must match real frequency */
346 if (mips_hpt_frequency
!= vcpu
->arch
.count_hz
)
349 /* We don't support a CP0_GTOffset with fewer bits than CP0_Count */
350 if (current_cpu_data
.gtoffset_mask
!= 0xffffffff)
357 * _kvm_vz_restore_stimer() - Restore soft timer state.
358 * @vcpu: Virtual CPU.
359 * @compare: CP0_Compare register value, restored by caller.
360 * @cause: CP0_Cause register to restore.
362 * Restore VZ state relating to the soft timer. The hard timer can be enabled
365 static void _kvm_vz_restore_stimer(struct kvm_vcpu
*vcpu
, u32 compare
,
369 * Avoid spurious counter interrupts by setting Guest CP0_Count to just
370 * after Guest CP0_Compare.
372 write_c0_gtoffset(compare
- read_c0_count());
374 back_to_back_c0_hazard();
375 write_gc0_cause(cause
);
379 * _kvm_vz_restore_htimer() - Restore hard timer state.
380 * @vcpu: Virtual CPU.
381 * @compare: CP0_Compare register value, restored by caller.
382 * @cause: CP0_Cause register to restore.
384 * Restore hard timer Guest.Count & Guest.Cause taking care to preserve the
385 * value of Guest.CP0_Cause.TI while restoring Guest.CP0_Cause.
387 static void _kvm_vz_restore_htimer(struct kvm_vcpu
*vcpu
,
388 u32 compare
, u32 cause
)
390 u32 start_count
, after_count
;
394 * Freeze the soft-timer and sync the guest CP0_Count with it. We do
395 * this with interrupts disabled to avoid latency.
397 local_irq_save(flags
);
398 kvm_mips_freeze_hrtimer(vcpu
, &start_count
);
399 write_c0_gtoffset(start_count
- read_c0_count());
400 local_irq_restore(flags
);
402 /* restore guest CP0_Cause, as TI may already be set */
403 back_to_back_c0_hazard();
404 write_gc0_cause(cause
);
407 * The above sequence isn't atomic and would result in lost timer
408 * interrupts if we're not careful. Detect if a timer interrupt is due
411 back_to_back_c0_hazard();
412 after_count
= read_gc0_count();
413 if (after_count
- start_count
> compare
- start_count
- 1)
414 kvm_vz_queue_irq(vcpu
, MIPS_EXC_INT_TIMER
);
418 * kvm_vz_restore_timer() - Restore timer state.
419 * @vcpu: Virtual CPU.
421 * Restore soft timer state from saved context.
423 static void kvm_vz_restore_timer(struct kvm_vcpu
*vcpu
)
425 struct mips_coproc
*cop0
= &vcpu
->arch
.cop0
;
428 compare
= kvm_read_sw_gc0_compare(cop0
);
429 cause
= kvm_read_sw_gc0_cause(cop0
);
431 write_gc0_compare(compare
);
432 _kvm_vz_restore_stimer(vcpu
, compare
, cause
);
436 * kvm_vz_acquire_htimer() - Switch to hard timer state.
437 * @vcpu: Virtual CPU.
439 * Restore hard timer state on top of existing soft timer state if possible.
441 * Since hard timer won't remain active over preemption, preemption should be
442 * disabled by the caller.
444 void kvm_vz_acquire_htimer(struct kvm_vcpu
*vcpu
)
448 gctl0
= read_c0_guestctl0();
449 if (!(gctl0
& MIPS_GCTL0_GT
) && kvm_vz_should_use_htimer(vcpu
)) {
450 /* enable guest access to hard timer */
451 write_c0_guestctl0(gctl0
| MIPS_GCTL0_GT
);
453 _kvm_vz_restore_htimer(vcpu
, read_gc0_compare(),
459 * _kvm_vz_save_htimer() - Switch to software emulation of guest timer.
460 * @vcpu: Virtual CPU.
461 * @out_compare: Pointer to write compare value to.
462 * @out_cause: Pointer to write cause value to.
464 * Save VZ guest timer state and switch to software emulation of guest CP0
465 * timer. The hard timer must already be in use, so preemption should be
468 static void _kvm_vz_save_htimer(struct kvm_vcpu
*vcpu
,
469 u32
*out_compare
, u32
*out_cause
)
471 u32 cause
, compare
, before_count
, end_count
;
474 compare
= read_gc0_compare();
475 *out_compare
= compare
;
477 before_time
= ktime_get();
480 * Record the CP0_Count *prior* to saving CP0_Cause, so we have a time
481 * at which no pending timer interrupt is missing.
483 before_count
= read_gc0_count();
484 back_to_back_c0_hazard();
485 cause
= read_gc0_cause();
489 * Record a final CP0_Count which we will transfer to the soft-timer.
490 * This is recorded *after* saving CP0_Cause, so we don't get any timer
491 * interrupts from just after the final CP0_Count point.
493 back_to_back_c0_hazard();
494 end_count
= read_gc0_count();
497 * The above sequence isn't atomic, so we could miss a timer interrupt
498 * between reading CP0_Cause and end_count. Detect and record any timer
499 * interrupt due between before_count and end_count.
501 if (end_count
- before_count
> compare
- before_count
- 1)
502 kvm_vz_queue_irq(vcpu
, MIPS_EXC_INT_TIMER
);
505 * Restore soft-timer, ignoring a small amount of negative drift due to
506 * delay between freeze_hrtimer and setting CP0_GTOffset.
508 kvm_mips_restore_hrtimer(vcpu
, before_time
, end_count
, -0x10000);
512 * kvm_vz_save_timer() - Save guest timer state.
513 * @vcpu: Virtual CPU.
515 * Save VZ guest timer state and switch to soft guest timer if hard timer was in
518 static void kvm_vz_save_timer(struct kvm_vcpu
*vcpu
)
520 struct mips_coproc
*cop0
= &vcpu
->arch
.cop0
;
521 u32 gctl0
, compare
, cause
;
523 gctl0
= read_c0_guestctl0();
524 if (gctl0
& MIPS_GCTL0_GT
) {
525 /* disable guest use of hard timer */
526 write_c0_guestctl0(gctl0
& ~MIPS_GCTL0_GT
);
528 /* save hard timer state */
529 _kvm_vz_save_htimer(vcpu
, &compare
, &cause
);
531 compare
= read_gc0_compare();
532 cause
= read_gc0_cause();
535 /* save timer-related state to VCPU context */
536 kvm_write_sw_gc0_cause(cop0
, cause
);
537 kvm_write_sw_gc0_compare(cop0
, compare
);
541 * kvm_vz_lose_htimer() - Ensure hard guest timer is not in use.
542 * @vcpu: Virtual CPU.
544 * Transfers the state of the hard guest timer to the soft guest timer, leaving
545 * guest state intact so it can continue to be used with the soft timer.
547 void kvm_vz_lose_htimer(struct kvm_vcpu
*vcpu
)
549 u32 gctl0
, compare
, cause
;
552 gctl0
= read_c0_guestctl0();
553 if (gctl0
& MIPS_GCTL0_GT
) {
554 /* disable guest use of timer */
555 write_c0_guestctl0(gctl0
& ~MIPS_GCTL0_GT
);
557 /* switch to soft timer */
558 _kvm_vz_save_htimer(vcpu
, &compare
, &cause
);
560 /* leave soft timer in usable state */
561 _kvm_vz_restore_stimer(vcpu
, compare
, cause
);
567 * is_eva_access() - Find whether an instruction is an EVA memory accessor.
568 * @inst: 32-bit instruction encoding.
570 * Finds whether @inst encodes an EVA memory access instruction, which would
571 * indicate that emulation of it should access the user mode address space
572 * instead of the kernel mode address space. This matters for MUSUK segments
573 * which are TLB mapped for user mode but unmapped for kernel mode.
575 * Returns: Whether @inst encodes an EVA accessor instruction.
577 static bool is_eva_access(union mips_instruction inst
)
579 if (inst
.spec3_format
.opcode
!= spec3_op
)
582 switch (inst
.spec3_format
.func
) {
606 * is_eva_am_mapped() - Find whether an access mode is mapped.
607 * @vcpu: KVM VCPU state.
608 * @am: 3-bit encoded access mode.
609 * @eu: Segment becomes unmapped and uncached when Status.ERL=1.
611 * Decode @am to find whether it encodes a mapped segment for the current VCPU
612 * state. Where necessary @eu and the actual instruction causing the fault are
613 * taken into account to make the decision.
615 * Returns: Whether the VCPU faulted on a TLB mapped address.
617 static bool is_eva_am_mapped(struct kvm_vcpu
*vcpu
, unsigned int am
, bool eu
)
623 * Interpret access control mode. We assume address errors will already
624 * have been caught by the guest, leaving us with:
625 * AM UM SM KM 31..24 23..16
628 * MSK 2 010 TLB TLB 1
629 * MUSK 3 011 TLB TLB TLB 1
630 * MUSUK 4 100 TLB TLB Unm 0 1
631 * USK 5 101 Unm Unm 0 0
633 * UUSK 7 111 Unm Unm Unm 0 0
635 * We shift a magic value by AM across the sign bit to find if always
636 * TLB mapped, and if not shift by 8 again to find if it depends on KM.
638 am_lookup
= 0x70080000 << am
;
639 if ((s32
)am_lookup
< 0) {
642 * Always TLB mapped, unless SegCtl.EU && ERL
644 if (!eu
|| !(read_gc0_status() & ST0_ERL
))
648 if ((s32
)am_lookup
< 0) {
649 union mips_instruction inst
;
655 * TLB mapped if not in kernel mode
657 status
= read_gc0_status();
658 if (!(status
& (ST0_EXL
| ST0_ERL
)) &&
662 * EVA access instructions in kernel
663 * mode access user address space.
665 opc
= (u32
*)vcpu
->arch
.pc
;
666 if (vcpu
->arch
.host_cp0_cause
& CAUSEF_BD
)
668 err
= kvm_get_badinstr(opc
, vcpu
, &inst
.word
);
669 if (!err
&& is_eva_access(inst
))
678 * kvm_vz_gva_to_gpa() - Convert valid GVA to GPA.
679 * @vcpu: KVM VCPU state.
680 * @gva: Guest virtual address to convert.
681 * @gpa: Output guest physical address.
683 * Convert a guest virtual address (GVA) which is valid according to the guest
684 * context, to a guest physical address (GPA).
686 * Returns: 0 on success.
689 static int kvm_vz_gva_to_gpa(struct kvm_vcpu
*vcpu
, unsigned long gva
,
693 unsigned long segctl
;
695 if ((long)gva
== (s32
)gva32
) {
696 /* Handle canonical 32-bit virtual address */
697 if (cpu_guest_has_segments
) {
698 unsigned long mask
, pa
;
700 switch (gva32
>> 29) {
702 case 1: /* CFG5 (1GB) */
703 segctl
= read_gc0_segctl2() >> 16;
704 mask
= (unsigned long)0xfc0000000ull
;
707 case 3: /* CFG4 (1GB) */
708 segctl
= read_gc0_segctl2();
709 mask
= (unsigned long)0xfc0000000ull
;
711 case 4: /* CFG3 (512MB) */
712 segctl
= read_gc0_segctl1() >> 16;
713 mask
= (unsigned long)0xfe0000000ull
;
715 case 5: /* CFG2 (512MB) */
716 segctl
= read_gc0_segctl1();
717 mask
= (unsigned long)0xfe0000000ull
;
719 case 6: /* CFG1 (512MB) */
720 segctl
= read_gc0_segctl0() >> 16;
721 mask
= (unsigned long)0xfe0000000ull
;
723 case 7: /* CFG0 (512MB) */
724 segctl
= read_gc0_segctl0();
725 mask
= (unsigned long)0xfe0000000ull
;
729 * GCC 4.9 isn't smart enough to figure out that
730 * segctl and mask are always initialised.
735 if (is_eva_am_mapped(vcpu
, (segctl
>> 4) & 0x7,
739 /* Unmapped, find guest physical address */
740 pa
= (segctl
<< 20) & mask
;
744 } else if ((s32
)gva32
< (s32
)0xc0000000) {
745 /* legacy unmapped KSeg0 or KSeg1 */
746 *gpa
= gva32
& 0x1fffffff;
750 } else if ((gva
& 0xc000000000000000) == 0x8000000000000000) {
752 if (cpu_guest_has_segments
) {
754 * Each of the 8 regions can be overridden by SegCtl2.XR
755 * to use SegCtl1.XAM.
757 segctl
= read_gc0_segctl2();
758 if (segctl
& (1ull << (56 + ((gva
>> 59) & 0x7)))) {
759 segctl
= read_gc0_segctl1();
760 if (is_eva_am_mapped(vcpu
, (segctl
>> 59) & 0x7,
767 * Traditionally fully unmapped.
768 * Bits 61:59 specify the CCA, which we can just mask off here.
769 * Bits 58:PABITS should be zero, but we shouldn't have got here
772 *gpa
= gva
& 0x07ffffffffffffff;
778 return kvm_vz_guest_tlb_lookup(vcpu
, gva
, gpa
);
782 * kvm_vz_badvaddr_to_gpa() - Convert GVA BadVAddr from root exception to GPA.
783 * @vcpu: KVM VCPU state.
784 * @badvaddr: Root BadVAddr.
785 * @gpa: Output guest physical address.
787 * VZ implementations are permitted to report guest virtual addresses (GVA) in
788 * BadVAddr on a root exception during guest execution, instead of the more
789 * convenient guest physical addresses (GPA). When we get a GVA, this function
790 * converts it to a GPA, taking into account guest segmentation and guest TLB
793 * Returns: 0 on success.
796 static int kvm_vz_badvaddr_to_gpa(struct kvm_vcpu
*vcpu
, unsigned long badvaddr
,
799 unsigned int gexccode
= (vcpu
->arch
.host_cp0_guestctl0
&
800 MIPS_GCTL0_GEXC
) >> MIPS_GCTL0_GEXC_SHIFT
;
802 /* If BadVAddr is GPA, then all is well in the world */
803 if (likely(gexccode
== MIPS_GCTL0_GEXC_GPA
)) {
808 /* Otherwise we'd expect it to be GVA ... */
809 if (WARN(gexccode
!= MIPS_GCTL0_GEXC_GVA
,
810 "Unexpected gexccode %#x\n", gexccode
))
813 /* ... and we need to perform the GVA->GPA translation in software */
814 return kvm_vz_gva_to_gpa(vcpu
, badvaddr
, gpa
);
817 static int kvm_trap_vz_no_handler(struct kvm_vcpu
*vcpu
)
819 u32
*opc
= (u32
*) vcpu
->arch
.pc
;
820 u32 cause
= vcpu
->arch
.host_cp0_cause
;
821 u32 exccode
= (cause
& CAUSEF_EXCCODE
) >> CAUSEB_EXCCODE
;
822 unsigned long badvaddr
= vcpu
->arch
.host_cp0_badvaddr
;
826 * Fetch the instruction.
828 if (cause
& CAUSEF_BD
)
830 kvm_get_badinstr(opc
, vcpu
, &inst
);
832 kvm_err("Exception Code: %d not handled @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#x\n",
833 exccode
, opc
, inst
, badvaddr
,
835 kvm_arch_vcpu_dump_regs(vcpu
);
836 vcpu
->run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
840 static unsigned long mips_process_maar(unsigned int op
, unsigned long val
)
842 /* Mask off unused bits */
843 unsigned long mask
= 0xfffff000 | MIPS_MAAR_S
| MIPS_MAAR_VL
;
845 if (read_gc0_pagegrain() & PG_ELPA
)
846 mask
|= 0x00ffffff00000000ull
;
847 if (cpu_guest_has_mvh
)
848 mask
|= MIPS_MAAR_VH
;
850 /* Set or clear VH */
853 val
&= ~MIPS_MAAR_VH
;
854 } else if (op
== dmtc_op
) {
855 /* set VH to match VL */
856 val
&= ~MIPS_MAAR_VH
;
857 if (val
& MIPS_MAAR_VL
)
864 static void kvm_write_maari(struct kvm_vcpu
*vcpu
, unsigned long val
)
866 struct mips_coproc
*cop0
= &vcpu
->arch
.cop0
;
868 val
&= MIPS_MAARI_INDEX
;
869 if (val
== MIPS_MAARI_INDEX
)
870 kvm_write_sw_gc0_maari(cop0
, ARRAY_SIZE(vcpu
->arch
.maar
) - 1);
871 else if (val
< ARRAY_SIZE(vcpu
->arch
.maar
))
872 kvm_write_sw_gc0_maari(cop0
, val
);
875 static enum emulation_result
kvm_vz_gpsi_cop0(union mips_instruction inst
,
877 struct kvm_vcpu
*vcpu
)
879 struct mips_coproc
*cop0
= &vcpu
->arch
.cop0
;
880 enum emulation_result er
= EMULATE_DONE
;
882 unsigned long curr_pc
;
886 * Update PC and hold onto current PC in case there is
887 * an error and we want to rollback the PC
889 curr_pc
= vcpu
->arch
.pc
;
890 er
= update_pc(vcpu
, cause
);
891 if (er
== EMULATE_FAIL
)
894 if (inst
.co_format
.co
) {
895 switch (inst
.co_format
.func
) {
897 er
= kvm_mips_emul_wait(vcpu
);
903 rt
= inst
.c0r_format
.rt
;
904 rd
= inst
.c0r_format
.rd
;
905 sel
= inst
.c0r_format
.sel
;
907 switch (inst
.c0r_format
.rs
) {
910 #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
911 cop0
->stat
[rd
][sel
]++;
913 if (rd
== MIPS_CP0_COUNT
&&
914 sel
== 0) { /* Count */
915 val
= kvm_mips_read_count(vcpu
);
916 } else if (rd
== MIPS_CP0_COMPARE
&&
917 sel
== 0) { /* Compare */
918 val
= read_gc0_compare();
919 } else if (rd
== MIPS_CP0_LLADDR
&&
920 sel
== 0) { /* LLAddr */
921 if (cpu_guest_has_rw_llb
)
922 val
= read_gc0_lladdr() &
926 } else if (rd
== MIPS_CP0_LLADDR
&&
927 sel
== 1 && /* MAAR */
928 cpu_guest_has_maar
&&
929 !cpu_guest_has_dyn_maar
) {
930 /* MAARI must be in range */
931 BUG_ON(kvm_read_sw_gc0_maari(cop0
) >=
932 ARRAY_SIZE(vcpu
->arch
.maar
));
933 val
= vcpu
->arch
.maar
[
934 kvm_read_sw_gc0_maari(cop0
)];
935 } else if ((rd
== MIPS_CP0_PRID
&&
936 (sel
== 0 || /* PRid */
937 sel
== 2 || /* CDMMBase */
938 sel
== 3)) || /* CMGCRBase */
939 (rd
== MIPS_CP0_STATUS
&&
940 (sel
== 2 || /* SRSCtl */
941 sel
== 3)) || /* SRSMap */
942 (rd
== MIPS_CP0_CONFIG
&&
943 (sel
== 6 || /* Config6 */
944 sel
== 7)) || /* Config7 */
945 (rd
== MIPS_CP0_LLADDR
&&
946 (sel
== 2) && /* MAARI */
947 cpu_guest_has_maar
&&
948 !cpu_guest_has_dyn_maar
) ||
949 (rd
== MIPS_CP0_ERRCTL
&&
950 (sel
== 0))) { /* ErrCtl */
951 val
= cop0
->reg
[rd
][sel
];
952 #ifdef CONFIG_CPU_LOONGSON64
953 } else if (rd
== MIPS_CP0_DIAG
&&
954 (sel
== 0)) { /* Diag */
955 val
= cop0
->reg
[rd
][sel
];
962 if (er
!= EMULATE_FAIL
) {
964 if (inst
.c0r_format
.rs
== mfc_op
)
966 vcpu
->arch
.gprs
[rt
] = val
;
969 trace_kvm_hwr(vcpu
, (inst
.c0r_format
.rs
== mfc_op
) ?
970 KVM_TRACE_MFC0
: KVM_TRACE_DMFC0
,
971 KVM_TRACE_COP0(rd
, sel
), val
);
976 #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
977 cop0
->stat
[rd
][sel
]++;
979 val
= vcpu
->arch
.gprs
[rt
];
980 trace_kvm_hwr(vcpu
, (inst
.c0r_format
.rs
== mtc_op
) ?
981 KVM_TRACE_MTC0
: KVM_TRACE_DMTC0
,
982 KVM_TRACE_COP0(rd
, sel
), val
);
984 if (rd
== MIPS_CP0_COUNT
&&
985 sel
== 0) { /* Count */
986 kvm_vz_lose_htimer(vcpu
);
987 kvm_mips_write_count(vcpu
, vcpu
->arch
.gprs
[rt
]);
988 } else if (rd
== MIPS_CP0_COMPARE
&&
989 sel
== 0) { /* Compare */
990 kvm_mips_write_compare(vcpu
,
993 } else if (rd
== MIPS_CP0_LLADDR
&&
994 sel
== 0) { /* LLAddr */
996 * P5600 generates GPSI on guest MTC0 LLAddr.
997 * Only allow the guest to clear LLB.
999 if (cpu_guest_has_rw_llb
&&
1000 !(val
& MIPS_LLADDR_LLB
))
1001 write_gc0_lladdr(0);
1002 } else if (rd
== MIPS_CP0_LLADDR
&&
1003 sel
== 1 && /* MAAR */
1004 cpu_guest_has_maar
&&
1005 !cpu_guest_has_dyn_maar
) {
1006 val
= mips_process_maar(inst
.c0r_format
.rs
,
1009 /* MAARI must be in range */
1010 BUG_ON(kvm_read_sw_gc0_maari(cop0
) >=
1011 ARRAY_SIZE(vcpu
->arch
.maar
));
1012 vcpu
->arch
.maar
[kvm_read_sw_gc0_maari(cop0
)] =
1014 } else if (rd
== MIPS_CP0_LLADDR
&&
1015 (sel
== 2) && /* MAARI */
1016 cpu_guest_has_maar
&&
1017 !cpu_guest_has_dyn_maar
) {
1018 kvm_write_maari(vcpu
, val
);
1019 } else if (rd
== MIPS_CP0_CONFIG
&&
1021 cop0
->reg
[rd
][sel
] = (int)val
;
1022 } else if (rd
== MIPS_CP0_ERRCTL
&&
1023 (sel
== 0)) { /* ErrCtl */
1024 /* ignore the written value */
1025 #ifdef CONFIG_CPU_LOONGSON64
1026 } else if (rd
== MIPS_CP0_DIAG
&&
1027 (sel
== 0)) { /* Diag */
1028 unsigned long flags
;
1030 local_irq_save(flags
);
1031 if (val
& LOONGSON_DIAG_BTB
) {
1033 set_c0_diag(LOONGSON_DIAG_BTB
);
1035 if (val
& LOONGSON_DIAG_ITLB
) {
1037 set_c0_diag(LOONGSON_DIAG_ITLB
);
1039 if (val
& LOONGSON_DIAG_DTLB
) {
1041 set_c0_diag(LOONGSON_DIAG_DTLB
);
1043 if (val
& LOONGSON_DIAG_VTLB
) {
1045 kvm_loongson_clear_guest_vtlb();
1047 if (val
& LOONGSON_DIAG_FTLB
) {
1049 kvm_loongson_clear_guest_ftlb();
1051 local_irq_restore(flags
);
1063 /* Rollback PC only if emulation was unsuccessful */
1064 if (er
== EMULATE_FAIL
) {
1065 kvm_err("[%#lx]%s: unsupported cop0 instruction 0x%08x\n",
1066 curr_pc
, __func__
, inst
.word
);
1068 vcpu
->arch
.pc
= curr_pc
;
1074 static enum emulation_result
kvm_vz_gpsi_cache(union mips_instruction inst
,
1075 u32
*opc
, u32 cause
,
1076 struct kvm_vcpu
*vcpu
)
1078 enum emulation_result er
= EMULATE_DONE
;
1079 u32 cache
, op_inst
, op
, base
;
1081 struct kvm_vcpu_arch
*arch
= &vcpu
->arch
;
1082 unsigned long va
, curr_pc
;
1085 * Update PC and hold onto current PC in case there is
1086 * an error and we want to rollback the PC
1088 curr_pc
= vcpu
->arch
.pc
;
1089 er
= update_pc(vcpu
, cause
);
1090 if (er
== EMULATE_FAIL
)
1093 base
= inst
.i_format
.rs
;
1094 op_inst
= inst
.i_format
.rt
;
1095 if (cpu_has_mips_r6
)
1096 offset
= inst
.spec3_format
.simmediate
;
1098 offset
= inst
.i_format
.simmediate
;
1099 cache
= op_inst
& CacheOp_Cache
;
1100 op
= op_inst
& CacheOp_Op
;
1102 va
= arch
->gprs
[base
] + offset
;
1104 kvm_debug("CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1105 cache
, op
, base
, arch
->gprs
[base
], offset
);
1107 /* Secondary or tirtiary cache ops ignored */
1108 if (cache
!= Cache_I
&& cache
!= Cache_D
)
1109 return EMULATE_DONE
;
1112 case Index_Invalidate_I
:
1113 flush_icache_line_indexed(va
);
1114 return EMULATE_DONE
;
1115 case Index_Writeback_Inv_D
:
1116 flush_dcache_line_indexed(va
);
1117 return EMULATE_DONE
;
1118 case Hit_Invalidate_I
:
1119 case Hit_Invalidate_D
:
1120 case Hit_Writeback_Inv_D
:
1121 if (boot_cpu_type() == CPU_CAVIUM_OCTEON3
) {
1122 /* We can just flush entire icache */
1123 local_flush_icache_range(0, 0);
1124 return EMULATE_DONE
;
1127 /* So far, other platforms support guest hit cache ops */
1133 kvm_err("@ %#lx/%#lx CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1134 curr_pc
, vcpu
->arch
.gprs
[31], cache
, op
, base
, arch
->gprs
[base
],
1137 vcpu
->arch
.pc
= curr_pc
;
1139 return EMULATE_FAIL
;
1142 #ifdef CONFIG_CPU_LOONGSON64
1143 static enum emulation_result
kvm_vz_gpsi_lwc2(union mips_instruction inst
,
1144 u32
*opc
, u32 cause
,
1145 struct kvm_vcpu
*vcpu
)
1147 unsigned int rs
, rd
;
1148 unsigned int hostcfg
;
1149 unsigned long curr_pc
;
1150 enum emulation_result er
= EMULATE_DONE
;
1153 * Update PC and hold onto current PC in case there is
1154 * an error and we want to rollback the PC
1156 curr_pc
= vcpu
->arch
.pc
;
1157 er
= update_pc(vcpu
, cause
);
1158 if (er
== EMULATE_FAIL
)
1161 rs
= inst
.loongson3_lscsr_format
.rs
;
1162 rd
= inst
.loongson3_lscsr_format
.rd
;
1163 switch (inst
.loongson3_lscsr_format
.fr
) {
1164 case 0x8: /* Read CPUCFG */
1165 ++vcpu
->stat
.vz_cpucfg_exits
;
1166 hostcfg
= read_cpucfg(vcpu
->arch
.gprs
[rs
]);
1168 switch (vcpu
->arch
.gprs
[rs
]) {
1170 vcpu
->arch
.gprs
[rd
] = 0x14c000;
1173 hostcfg
&= (LOONGSON_CFG1_FP
| LOONGSON_CFG1_MMI
|
1174 LOONGSON_CFG1_MSA1
| LOONGSON_CFG1_MSA2
|
1175 LOONGSON_CFG1_SFBP
);
1176 vcpu
->arch
.gprs
[rd
] = hostcfg
;
1179 hostcfg
&= (LOONGSON_CFG2_LEXT1
| LOONGSON_CFG2_LEXT2
|
1180 LOONGSON_CFG2_LEXT3
| LOONGSON_CFG2_LSPW
);
1181 vcpu
->arch
.gprs
[rd
] = hostcfg
;
1184 vcpu
->arch
.gprs
[rd
] = hostcfg
;
1187 /* Don't export any other advanced features to guest */
1188 vcpu
->arch
.gprs
[rd
] = 0;
1194 kvm_err("lwc2 emulate not impl %d rs %lx @%lx\n",
1195 inst
.loongson3_lscsr_format
.fr
, vcpu
->arch
.gprs
[rs
], curr_pc
);
1200 /* Rollback PC only if emulation was unsuccessful */
1201 if (er
== EMULATE_FAIL
) {
1202 kvm_err("[%#lx]%s: unsupported lwc2 instruction 0x%08x 0x%08x\n",
1203 curr_pc
, __func__
, inst
.word
, inst
.loongson3_lscsr_format
.fr
);
1205 vcpu
->arch
.pc
= curr_pc
;
1212 static enum emulation_result
kvm_trap_vz_handle_gpsi(u32 cause
, u32
*opc
,
1213 struct kvm_vcpu
*vcpu
)
1215 enum emulation_result er
= EMULATE_DONE
;
1216 struct kvm_vcpu_arch
*arch
= &vcpu
->arch
;
1217 union mips_instruction inst
;
1222 * Fetch the instruction.
1224 if (cause
& CAUSEF_BD
)
1226 err
= kvm_get_badinstr(opc
, vcpu
, &inst
.word
);
1228 return EMULATE_FAIL
;
1230 switch (inst
.r_format
.opcode
) {
1232 er
= kvm_vz_gpsi_cop0(inst
, opc
, cause
, vcpu
);
1234 #ifndef CONFIG_CPU_MIPSR6
1236 trace_kvm_exit(vcpu
, KVM_TRACE_EXIT_CACHE
);
1237 er
= kvm_vz_gpsi_cache(inst
, opc
, cause
, vcpu
);
1240 #ifdef CONFIG_CPU_LOONGSON64
1242 er
= kvm_vz_gpsi_lwc2(inst
, opc
, cause
, vcpu
);
1246 switch (inst
.spec3_format
.func
) {
1247 #ifdef CONFIG_CPU_MIPSR6
1249 trace_kvm_exit(vcpu
, KVM_TRACE_EXIT_CACHE
);
1250 er
= kvm_vz_gpsi_cache(inst
, opc
, cause
, vcpu
);
1254 if (inst
.r_format
.rs
|| (inst
.r_format
.re
>> 3))
1257 rd
= inst
.r_format
.rd
;
1258 rt
= inst
.r_format
.rt
;
1259 sel
= inst
.r_format
.re
& 0x7;
1262 case MIPS_HWR_CC
: /* Read count register */
1264 (long)(int)kvm_mips_read_count(vcpu
);
1267 trace_kvm_hwr(vcpu
, KVM_TRACE_RDHWR
,
1268 KVM_TRACE_HWR(rd
, sel
), 0);
1272 trace_kvm_hwr(vcpu
, KVM_TRACE_RDHWR
,
1273 KVM_TRACE_HWR(rd
, sel
), arch
->gprs
[rt
]);
1275 er
= update_pc(vcpu
, cause
);
1284 kvm_err("GPSI exception not supported (%p/%#x)\n",
1286 kvm_arch_vcpu_dump_regs(vcpu
);
1294 static enum emulation_result
kvm_trap_vz_handle_gsfc(u32 cause
, u32
*opc
,
1295 struct kvm_vcpu
*vcpu
)
1297 enum emulation_result er
= EMULATE_DONE
;
1298 struct kvm_vcpu_arch
*arch
= &vcpu
->arch
;
1299 union mips_instruction inst
;
1303 * Fetch the instruction.
1305 if (cause
& CAUSEF_BD
)
1307 err
= kvm_get_badinstr(opc
, vcpu
, &inst
.word
);
1309 return EMULATE_FAIL
;
1311 /* complete MTC0 on behalf of guest and advance EPC */
1312 if (inst
.c0r_format
.opcode
== cop0_op
&&
1313 inst
.c0r_format
.rs
== mtc_op
&&
1314 inst
.c0r_format
.z
== 0) {
1315 int rt
= inst
.c0r_format
.rt
;
1316 int rd
= inst
.c0r_format
.rd
;
1317 int sel
= inst
.c0r_format
.sel
;
1318 unsigned int val
= arch
->gprs
[rt
];
1319 unsigned int old_val
, change
;
1321 trace_kvm_hwr(vcpu
, KVM_TRACE_MTC0
, KVM_TRACE_COP0(rd
, sel
),
1324 if ((rd
== MIPS_CP0_STATUS
) && (sel
== 0)) {
1325 /* FR bit should read as zero if no FPU */
1326 if (!kvm_mips_guest_has_fpu(&vcpu
->arch
))
1327 val
&= ~(ST0_CU1
| ST0_FR
);
1330 * Also don't allow FR to be set if host doesn't support
1333 if (!(boot_cpu_data
.fpu_id
& MIPS_FPIR_F64
))
1336 old_val
= read_gc0_status();
1337 change
= val
^ old_val
;
1339 if (change
& ST0_FR
) {
1341 * FPU and Vector register state is made
1342 * UNPREDICTABLE by a change of FR, so don't
1343 * even bother saving it.
1349 * If MSA state is already live, it is undefined how it
1350 * interacts with FR=0 FPU state, and we don't want to
1351 * hit reserved instruction exceptions trying to save
1352 * the MSA state later when CU=1 && FR=1, so play it
1353 * safe and save it first.
1355 if (change
& ST0_CU1
&& !(val
& ST0_FR
) &&
1356 vcpu
->arch
.aux_inuse
& KVM_MIPS_AUX_MSA
)
1359 write_gc0_status(val
);
1360 } else if ((rd
== MIPS_CP0_CAUSE
) && (sel
== 0)) {
1361 u32 old_cause
= read_gc0_cause();
1362 u32 change
= old_cause
^ val
;
1364 /* DC bit enabling/disabling timer? */
1365 if (change
& CAUSEF_DC
) {
1366 if (val
& CAUSEF_DC
) {
1367 kvm_vz_lose_htimer(vcpu
);
1368 kvm_mips_count_disable_cause(vcpu
);
1370 kvm_mips_count_enable_cause(vcpu
);
1374 /* Only certain bits are RW to the guest */
1375 change
&= (CAUSEF_DC
| CAUSEF_IV
| CAUSEF_WP
|
1376 CAUSEF_IP0
| CAUSEF_IP1
);
1378 /* WP can only be cleared */
1379 change
&= ~CAUSEF_WP
| old_cause
;
1381 write_gc0_cause(old_cause
^ change
);
1382 } else if ((rd
== MIPS_CP0_STATUS
) && (sel
== 1)) { /* IntCtl */
1383 write_gc0_intctl(val
);
1384 } else if ((rd
== MIPS_CP0_CONFIG
) && (sel
== 5)) {
1385 old_val
= read_gc0_config5();
1386 change
= val
^ old_val
;
1387 /* Handle changes in FPU/MSA modes */
1391 * Propagate FRE changes immediately if the FPU
1392 * context is already loaded.
1394 if (change
& MIPS_CONF5_FRE
&&
1395 vcpu
->arch
.aux_inuse
& KVM_MIPS_AUX_FPU
)
1396 change_c0_config5(MIPS_CONF5_FRE
, val
);
1401 (change
& kvm_vz_config5_guest_wrmask(vcpu
));
1402 write_gc0_config5(val
);
1404 kvm_err("Handle GSFC, unsupported field change @ %p: %#x\n",
1409 if (er
!= EMULATE_FAIL
)
1410 er
= update_pc(vcpu
, cause
);
1412 kvm_err("Handle GSFC, unrecognized instruction @ %p: %#x\n",
1420 static enum emulation_result
kvm_trap_vz_handle_ghfc(u32 cause
, u32
*opc
,
1421 struct kvm_vcpu
*vcpu
)
1424 * Presumably this is due to MC (guest mode change), so lets trace some
1427 trace_kvm_guest_mode_change(vcpu
);
1429 return EMULATE_DONE
;
1432 static enum emulation_result
kvm_trap_vz_handle_hc(u32 cause
, u32
*opc
,
1433 struct kvm_vcpu
*vcpu
)
1435 enum emulation_result er
;
1436 union mips_instruction inst
;
1437 unsigned long curr_pc
;
1440 if (cause
& CAUSEF_BD
)
1442 err
= kvm_get_badinstr(opc
, vcpu
, &inst
.word
);
1444 return EMULATE_FAIL
;
1447 * Update PC and hold onto current PC in case there is
1448 * an error and we want to rollback the PC
1450 curr_pc
= vcpu
->arch
.pc
;
1451 er
= update_pc(vcpu
, cause
);
1452 if (er
== EMULATE_FAIL
)
1455 er
= kvm_mips_emul_hypcall(vcpu
, inst
);
1456 if (er
== EMULATE_FAIL
)
1457 vcpu
->arch
.pc
= curr_pc
;
1462 static enum emulation_result
kvm_trap_vz_no_handler_guest_exit(u32 gexccode
,
1465 struct kvm_vcpu
*vcpu
)
1470 * Fetch the instruction.
1472 if (cause
& CAUSEF_BD
)
1474 kvm_get_badinstr(opc
, vcpu
, &inst
);
1476 kvm_err("Guest Exception Code: %d not yet handled @ PC: %p, inst: 0x%08x Status: %#x\n",
1477 gexccode
, opc
, inst
, read_gc0_status());
1479 return EMULATE_FAIL
;
1482 static int kvm_trap_vz_handle_guest_exit(struct kvm_vcpu
*vcpu
)
1484 u32
*opc
= (u32
*) vcpu
->arch
.pc
;
1485 u32 cause
= vcpu
->arch
.host_cp0_cause
;
1486 enum emulation_result er
= EMULATE_DONE
;
1487 u32 gexccode
= (vcpu
->arch
.host_cp0_guestctl0
&
1488 MIPS_GCTL0_GEXC
) >> MIPS_GCTL0_GEXC_SHIFT
;
1489 int ret
= RESUME_GUEST
;
1491 trace_kvm_exit(vcpu
, KVM_TRACE_EXIT_GEXCCODE_BASE
+ gexccode
);
1493 case MIPS_GCTL0_GEXC_GPSI
:
1494 ++vcpu
->stat
.vz_gpsi_exits
;
1495 er
= kvm_trap_vz_handle_gpsi(cause
, opc
, vcpu
);
1497 case MIPS_GCTL0_GEXC_GSFC
:
1498 ++vcpu
->stat
.vz_gsfc_exits
;
1499 er
= kvm_trap_vz_handle_gsfc(cause
, opc
, vcpu
);
1501 case MIPS_GCTL0_GEXC_HC
:
1502 ++vcpu
->stat
.vz_hc_exits
;
1503 er
= kvm_trap_vz_handle_hc(cause
, opc
, vcpu
);
1505 case MIPS_GCTL0_GEXC_GRR
:
1506 ++vcpu
->stat
.vz_grr_exits
;
1507 er
= kvm_trap_vz_no_handler_guest_exit(gexccode
, cause
, opc
,
1510 case MIPS_GCTL0_GEXC_GVA
:
1511 ++vcpu
->stat
.vz_gva_exits
;
1512 er
= kvm_trap_vz_no_handler_guest_exit(gexccode
, cause
, opc
,
1515 case MIPS_GCTL0_GEXC_GHFC
:
1516 ++vcpu
->stat
.vz_ghfc_exits
;
1517 er
= kvm_trap_vz_handle_ghfc(cause
, opc
, vcpu
);
1519 case MIPS_GCTL0_GEXC_GPA
:
1520 ++vcpu
->stat
.vz_gpa_exits
;
1521 er
= kvm_trap_vz_no_handler_guest_exit(gexccode
, cause
, opc
,
1525 ++vcpu
->stat
.vz_resvd_exits
;
1526 er
= kvm_trap_vz_no_handler_guest_exit(gexccode
, cause
, opc
,
1532 if (er
== EMULATE_DONE
) {
1534 } else if (er
== EMULATE_HYPERCALL
) {
1535 ret
= kvm_mips_handle_hypcall(vcpu
);
1537 vcpu
->run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1544 * kvm_trap_vz_handle_cop_unusable() - Guest used unusable coprocessor.
1545 * @vcpu: Virtual CPU context.
1547 * Handle when the guest attempts to use a coprocessor which hasn't been allowed
1548 * by the root context.
1550 * Return: value indicating whether to resume the host or the guest
1551 * (RESUME_HOST or RESUME_GUEST)
1553 static int kvm_trap_vz_handle_cop_unusable(struct kvm_vcpu
*vcpu
)
1555 u32 cause
= vcpu
->arch
.host_cp0_cause
;
1556 enum emulation_result er
= EMULATE_FAIL
;
1557 int ret
= RESUME_GUEST
;
1559 if (((cause
& CAUSEF_CE
) >> CAUSEB_CE
) == 1) {
1561 * If guest FPU not present, the FPU operation should have been
1562 * treated as a reserved instruction!
1563 * If FPU already in use, we shouldn't get this at all.
1565 if (WARN_ON(!kvm_mips_guest_has_fpu(&vcpu
->arch
) ||
1566 vcpu
->arch
.aux_inuse
& KVM_MIPS_AUX_FPU
)) {
1568 return EMULATE_FAIL
;
1574 /* other coprocessors not handled */
1582 vcpu
->run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1593 * kvm_trap_vz_handle_msa_disabled() - Guest used MSA while disabled in root.
1594 * @vcpu: Virtual CPU context.
1596 * Handle when the guest attempts to use MSA when it is disabled in the root
1599 * Return: value indicating whether to resume the host or the guest
1600 * (RESUME_HOST or RESUME_GUEST)
1602 static int kvm_trap_vz_handle_msa_disabled(struct kvm_vcpu
*vcpu
)
1605 * If MSA not present or not exposed to guest or FR=0, the MSA operation
1606 * should have been treated as a reserved instruction!
1607 * Same if CU1=1, FR=0.
1608 * If MSA already in use, we shouldn't get this at all.
1610 if (!kvm_mips_guest_has_msa(&vcpu
->arch
) ||
1611 (read_gc0_status() & (ST0_CU1
| ST0_FR
)) == ST0_CU1
||
1612 !(read_gc0_config5() & MIPS_CONF5_MSAEN
) ||
1613 vcpu
->arch
.aux_inuse
& KVM_MIPS_AUX_MSA
) {
1614 vcpu
->run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1620 return RESUME_GUEST
;
1623 static int kvm_trap_vz_handle_tlb_ld_miss(struct kvm_vcpu
*vcpu
)
1625 struct kvm_run
*run
= vcpu
->run
;
1626 u32
*opc
= (u32
*) vcpu
->arch
.pc
;
1627 u32 cause
= vcpu
->arch
.host_cp0_cause
;
1628 ulong badvaddr
= vcpu
->arch
.host_cp0_badvaddr
;
1629 union mips_instruction inst
;
1630 enum emulation_result er
= EMULATE_DONE
;
1631 int err
, ret
= RESUME_GUEST
;
1633 if (kvm_mips_handle_vz_root_tlb_fault(badvaddr
, vcpu
, false)) {
1634 /* A code fetch fault doesn't count as an MMIO */
1635 if (kvm_is_ifetch_fault(&vcpu
->arch
)) {
1636 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1640 /* Fetch the instruction */
1641 if (cause
& CAUSEF_BD
)
1643 err
= kvm_get_badinstr(opc
, vcpu
, &inst
.word
);
1645 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1650 er
= kvm_mips_emulate_load(inst
, cause
, vcpu
);
1651 if (er
== EMULATE_FAIL
) {
1652 kvm_err("Guest Emulate Load from MMIO space failed: PC: %p, BadVaddr: %#lx\n",
1654 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1658 if (er
== EMULATE_DONE
) {
1660 } else if (er
== EMULATE_DO_MMIO
) {
1661 run
->exit_reason
= KVM_EXIT_MMIO
;
1664 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1670 static int kvm_trap_vz_handle_tlb_st_miss(struct kvm_vcpu
*vcpu
)
1672 struct kvm_run
*run
= vcpu
->run
;
1673 u32
*opc
= (u32
*) vcpu
->arch
.pc
;
1674 u32 cause
= vcpu
->arch
.host_cp0_cause
;
1675 ulong badvaddr
= vcpu
->arch
.host_cp0_badvaddr
;
1676 union mips_instruction inst
;
1677 enum emulation_result er
= EMULATE_DONE
;
1679 int ret
= RESUME_GUEST
;
1681 /* Just try the access again if we couldn't do the translation */
1682 if (kvm_vz_badvaddr_to_gpa(vcpu
, badvaddr
, &badvaddr
))
1683 return RESUME_GUEST
;
1684 vcpu
->arch
.host_cp0_badvaddr
= badvaddr
;
1686 if (kvm_mips_handle_vz_root_tlb_fault(badvaddr
, vcpu
, true)) {
1687 /* Fetch the instruction */
1688 if (cause
& CAUSEF_BD
)
1690 err
= kvm_get_badinstr(opc
, vcpu
, &inst
.word
);
1692 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1697 er
= kvm_mips_emulate_store(inst
, cause
, vcpu
);
1698 if (er
== EMULATE_FAIL
) {
1699 kvm_err("Guest Emulate Store to MMIO space failed: PC: %p, BadVaddr: %#lx\n",
1701 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1705 if (er
== EMULATE_DONE
) {
1707 } else if (er
== EMULATE_DO_MMIO
) {
1708 run
->exit_reason
= KVM_EXIT_MMIO
;
1711 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1717 static u64 kvm_vz_get_one_regs
[] = {
1718 KVM_REG_MIPS_CP0_INDEX
,
1719 KVM_REG_MIPS_CP0_ENTRYLO0
,
1720 KVM_REG_MIPS_CP0_ENTRYLO1
,
1721 KVM_REG_MIPS_CP0_CONTEXT
,
1722 KVM_REG_MIPS_CP0_PAGEMASK
,
1723 KVM_REG_MIPS_CP0_PAGEGRAIN
,
1724 KVM_REG_MIPS_CP0_WIRED
,
1725 KVM_REG_MIPS_CP0_HWRENA
,
1726 KVM_REG_MIPS_CP0_BADVADDR
,
1727 KVM_REG_MIPS_CP0_COUNT
,
1728 KVM_REG_MIPS_CP0_ENTRYHI
,
1729 KVM_REG_MIPS_CP0_COMPARE
,
1730 KVM_REG_MIPS_CP0_STATUS
,
1731 KVM_REG_MIPS_CP0_INTCTL
,
1732 KVM_REG_MIPS_CP0_CAUSE
,
1733 KVM_REG_MIPS_CP0_EPC
,
1734 KVM_REG_MIPS_CP0_PRID
,
1735 KVM_REG_MIPS_CP0_EBASE
,
1736 KVM_REG_MIPS_CP0_CONFIG
,
1737 KVM_REG_MIPS_CP0_CONFIG1
,
1738 KVM_REG_MIPS_CP0_CONFIG2
,
1739 KVM_REG_MIPS_CP0_CONFIG3
,
1740 KVM_REG_MIPS_CP0_CONFIG4
,
1741 KVM_REG_MIPS_CP0_CONFIG5
,
1742 KVM_REG_MIPS_CP0_CONFIG6
,
1744 KVM_REG_MIPS_CP0_XCONTEXT
,
1746 KVM_REG_MIPS_CP0_ERROREPC
,
1748 KVM_REG_MIPS_COUNT_CTL
,
1749 KVM_REG_MIPS_COUNT_RESUME
,
1750 KVM_REG_MIPS_COUNT_HZ
,
1753 static u64 kvm_vz_get_one_regs_contextconfig
[] = {
1754 KVM_REG_MIPS_CP0_CONTEXTCONFIG
,
1756 KVM_REG_MIPS_CP0_XCONTEXTCONFIG
,
1760 static u64 kvm_vz_get_one_regs_segments
[] = {
1761 KVM_REG_MIPS_CP0_SEGCTL0
,
1762 KVM_REG_MIPS_CP0_SEGCTL1
,
1763 KVM_REG_MIPS_CP0_SEGCTL2
,
1766 static u64 kvm_vz_get_one_regs_htw
[] = {
1767 KVM_REG_MIPS_CP0_PWBASE
,
1768 KVM_REG_MIPS_CP0_PWFIELD
,
1769 KVM_REG_MIPS_CP0_PWSIZE
,
1770 KVM_REG_MIPS_CP0_PWCTL
,
1773 static u64 kvm_vz_get_one_regs_kscratch
[] = {
1774 KVM_REG_MIPS_CP0_KSCRATCH1
,
1775 KVM_REG_MIPS_CP0_KSCRATCH2
,
1776 KVM_REG_MIPS_CP0_KSCRATCH3
,
1777 KVM_REG_MIPS_CP0_KSCRATCH4
,
1778 KVM_REG_MIPS_CP0_KSCRATCH5
,
1779 KVM_REG_MIPS_CP0_KSCRATCH6
,
1782 static unsigned long kvm_vz_num_regs(struct kvm_vcpu
*vcpu
)
1786 ret
= ARRAY_SIZE(kvm_vz_get_one_regs
);
1787 if (cpu_guest_has_userlocal
)
1789 if (cpu_guest_has_badinstr
)
1791 if (cpu_guest_has_badinstrp
)
1793 if (cpu_guest_has_contextconfig
)
1794 ret
+= ARRAY_SIZE(kvm_vz_get_one_regs_contextconfig
);
1795 if (cpu_guest_has_segments
)
1796 ret
+= ARRAY_SIZE(kvm_vz_get_one_regs_segments
);
1797 if (cpu_guest_has_htw
|| cpu_guest_has_ldpte
)
1798 ret
+= ARRAY_SIZE(kvm_vz_get_one_regs_htw
);
1799 if (cpu_guest_has_maar
&& !cpu_guest_has_dyn_maar
)
1800 ret
+= 1 + ARRAY_SIZE(vcpu
->arch
.maar
);
1801 ret
+= __arch_hweight8(cpu_data
[0].guest
.kscratch_mask
);
1806 static int kvm_vz_copy_reg_indices(struct kvm_vcpu
*vcpu
, u64 __user
*indices
)
1811 if (copy_to_user(indices
, kvm_vz_get_one_regs
,
1812 sizeof(kvm_vz_get_one_regs
)))
1814 indices
+= ARRAY_SIZE(kvm_vz_get_one_regs
);
1816 if (cpu_guest_has_userlocal
) {
1817 index
= KVM_REG_MIPS_CP0_USERLOCAL
;
1818 if (copy_to_user(indices
, &index
, sizeof(index
)))
1822 if (cpu_guest_has_badinstr
) {
1823 index
= KVM_REG_MIPS_CP0_BADINSTR
;
1824 if (copy_to_user(indices
, &index
, sizeof(index
)))
1828 if (cpu_guest_has_badinstrp
) {
1829 index
= KVM_REG_MIPS_CP0_BADINSTRP
;
1830 if (copy_to_user(indices
, &index
, sizeof(index
)))
1834 if (cpu_guest_has_contextconfig
) {
1835 if (copy_to_user(indices
, kvm_vz_get_one_regs_contextconfig
,
1836 sizeof(kvm_vz_get_one_regs_contextconfig
)))
1838 indices
+= ARRAY_SIZE(kvm_vz_get_one_regs_contextconfig
);
1840 if (cpu_guest_has_segments
) {
1841 if (copy_to_user(indices
, kvm_vz_get_one_regs_segments
,
1842 sizeof(kvm_vz_get_one_regs_segments
)))
1844 indices
+= ARRAY_SIZE(kvm_vz_get_one_regs_segments
);
1846 if (cpu_guest_has_htw
|| cpu_guest_has_ldpte
) {
1847 if (copy_to_user(indices
, kvm_vz_get_one_regs_htw
,
1848 sizeof(kvm_vz_get_one_regs_htw
)))
1850 indices
+= ARRAY_SIZE(kvm_vz_get_one_regs_htw
);
1852 if (cpu_guest_has_maar
&& !cpu_guest_has_dyn_maar
) {
1853 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.maar
); ++i
) {
1854 index
= KVM_REG_MIPS_CP0_MAAR(i
);
1855 if (copy_to_user(indices
, &index
, sizeof(index
)))
1860 index
= KVM_REG_MIPS_CP0_MAARI
;
1861 if (copy_to_user(indices
, &index
, sizeof(index
)))
1865 for (i
= 0; i
< 6; ++i
) {
1866 if (!cpu_guest_has_kscr(i
+ 2))
1869 if (copy_to_user(indices
, &kvm_vz_get_one_regs_kscratch
[i
],
1870 sizeof(kvm_vz_get_one_regs_kscratch
[i
])))
1878 static inline s64
entrylo_kvm_to_user(unsigned long v
)
1882 if (BITS_PER_LONG
== 32) {
1884 * KVM API exposes 64-bit version of the register, so move the
1885 * RI/XI bits up into place.
1887 mask
= MIPS_ENTRYLO_RI
| MIPS_ENTRYLO_XI
;
1889 ret
|= ((s64
)v
& mask
) << 32;
1894 static inline unsigned long entrylo_user_to_kvm(s64 v
)
1896 unsigned long mask
, ret
= v
;
1898 if (BITS_PER_LONG
== 32) {
1900 * KVM API exposes 64-bit versiono of the register, so move the
1901 * RI/XI bits down into place.
1903 mask
= MIPS_ENTRYLO_RI
| MIPS_ENTRYLO_XI
;
1905 ret
|= (v
>> 32) & mask
;
1910 static int kvm_vz_get_one_reg(struct kvm_vcpu
*vcpu
,
1911 const struct kvm_one_reg
*reg
,
1914 struct mips_coproc
*cop0
= &vcpu
->arch
.cop0
;
1918 case KVM_REG_MIPS_CP0_INDEX
:
1919 *v
= (long)read_gc0_index();
1921 case KVM_REG_MIPS_CP0_ENTRYLO0
:
1922 *v
= entrylo_kvm_to_user(read_gc0_entrylo0());
1924 case KVM_REG_MIPS_CP0_ENTRYLO1
:
1925 *v
= entrylo_kvm_to_user(read_gc0_entrylo1());
1927 case KVM_REG_MIPS_CP0_CONTEXT
:
1928 *v
= (long)read_gc0_context();
1930 case KVM_REG_MIPS_CP0_CONTEXTCONFIG
:
1931 if (!cpu_guest_has_contextconfig
)
1933 *v
= read_gc0_contextconfig();
1935 case KVM_REG_MIPS_CP0_USERLOCAL
:
1936 if (!cpu_guest_has_userlocal
)
1938 *v
= read_gc0_userlocal();
1941 case KVM_REG_MIPS_CP0_XCONTEXTCONFIG
:
1942 if (!cpu_guest_has_contextconfig
)
1944 *v
= read_gc0_xcontextconfig();
1947 case KVM_REG_MIPS_CP0_PAGEMASK
:
1948 *v
= (long)read_gc0_pagemask();
1950 case KVM_REG_MIPS_CP0_PAGEGRAIN
:
1951 *v
= (long)read_gc0_pagegrain();
1953 case KVM_REG_MIPS_CP0_SEGCTL0
:
1954 if (!cpu_guest_has_segments
)
1956 *v
= read_gc0_segctl0();
1958 case KVM_REG_MIPS_CP0_SEGCTL1
:
1959 if (!cpu_guest_has_segments
)
1961 *v
= read_gc0_segctl1();
1963 case KVM_REG_MIPS_CP0_SEGCTL2
:
1964 if (!cpu_guest_has_segments
)
1966 *v
= read_gc0_segctl2();
1968 case KVM_REG_MIPS_CP0_PWBASE
:
1969 if (!cpu_guest_has_htw
&& !cpu_guest_has_ldpte
)
1971 *v
= read_gc0_pwbase();
1973 case KVM_REG_MIPS_CP0_PWFIELD
:
1974 if (!cpu_guest_has_htw
&& !cpu_guest_has_ldpte
)
1976 *v
= read_gc0_pwfield();
1978 case KVM_REG_MIPS_CP0_PWSIZE
:
1979 if (!cpu_guest_has_htw
&& !cpu_guest_has_ldpte
)
1981 *v
= read_gc0_pwsize();
1983 case KVM_REG_MIPS_CP0_WIRED
:
1984 *v
= (long)read_gc0_wired();
1986 case KVM_REG_MIPS_CP0_PWCTL
:
1987 if (!cpu_guest_has_htw
&& !cpu_guest_has_ldpte
)
1989 *v
= read_gc0_pwctl();
1991 case KVM_REG_MIPS_CP0_HWRENA
:
1992 *v
= (long)read_gc0_hwrena();
1994 case KVM_REG_MIPS_CP0_BADVADDR
:
1995 *v
= (long)read_gc0_badvaddr();
1997 case KVM_REG_MIPS_CP0_BADINSTR
:
1998 if (!cpu_guest_has_badinstr
)
2000 *v
= read_gc0_badinstr();
2002 case KVM_REG_MIPS_CP0_BADINSTRP
:
2003 if (!cpu_guest_has_badinstrp
)
2005 *v
= read_gc0_badinstrp();
2007 case KVM_REG_MIPS_CP0_COUNT
:
2008 *v
= kvm_mips_read_count(vcpu
);
2010 case KVM_REG_MIPS_CP0_ENTRYHI
:
2011 *v
= (long)read_gc0_entryhi();
2013 case KVM_REG_MIPS_CP0_COMPARE
:
2014 *v
= (long)read_gc0_compare();
2016 case KVM_REG_MIPS_CP0_STATUS
:
2017 *v
= (long)read_gc0_status();
2019 case KVM_REG_MIPS_CP0_INTCTL
:
2020 *v
= read_gc0_intctl();
2022 case KVM_REG_MIPS_CP0_CAUSE
:
2023 *v
= (long)read_gc0_cause();
2025 case KVM_REG_MIPS_CP0_EPC
:
2026 *v
= (long)read_gc0_epc();
2028 case KVM_REG_MIPS_CP0_PRID
:
2029 switch (boot_cpu_type()) {
2030 case CPU_CAVIUM_OCTEON3
:
2031 /* Octeon III has a read-only guest.PRid */
2032 *v
= read_gc0_prid();
2035 *v
= (long)kvm_read_c0_guest_prid(cop0
);
2039 case KVM_REG_MIPS_CP0_EBASE
:
2040 *v
= kvm_vz_read_gc0_ebase();
2042 case KVM_REG_MIPS_CP0_CONFIG
:
2043 *v
= read_gc0_config();
2045 case KVM_REG_MIPS_CP0_CONFIG1
:
2046 if (!cpu_guest_has_conf1
)
2048 *v
= read_gc0_config1();
2050 case KVM_REG_MIPS_CP0_CONFIG2
:
2051 if (!cpu_guest_has_conf2
)
2053 *v
= read_gc0_config2();
2055 case KVM_REG_MIPS_CP0_CONFIG3
:
2056 if (!cpu_guest_has_conf3
)
2058 *v
= read_gc0_config3();
2060 case KVM_REG_MIPS_CP0_CONFIG4
:
2061 if (!cpu_guest_has_conf4
)
2063 *v
= read_gc0_config4();
2065 case KVM_REG_MIPS_CP0_CONFIG5
:
2066 if (!cpu_guest_has_conf5
)
2068 *v
= read_gc0_config5();
2070 case KVM_REG_MIPS_CP0_CONFIG6
:
2071 *v
= kvm_read_sw_gc0_config6(cop0
);
2073 case KVM_REG_MIPS_CP0_MAAR(0) ... KVM_REG_MIPS_CP0_MAAR(0x3f):
2074 if (!cpu_guest_has_maar
|| cpu_guest_has_dyn_maar
)
2076 idx
= reg
->id
- KVM_REG_MIPS_CP0_MAAR(0);
2077 if (idx
>= ARRAY_SIZE(vcpu
->arch
.maar
))
2079 *v
= vcpu
->arch
.maar
[idx
];
2081 case KVM_REG_MIPS_CP0_MAARI
:
2082 if (!cpu_guest_has_maar
|| cpu_guest_has_dyn_maar
)
2084 *v
= kvm_read_sw_gc0_maari(&vcpu
->arch
.cop0
);
2087 case KVM_REG_MIPS_CP0_XCONTEXT
:
2088 *v
= read_gc0_xcontext();
2091 case KVM_REG_MIPS_CP0_ERROREPC
:
2092 *v
= (long)read_gc0_errorepc();
2094 case KVM_REG_MIPS_CP0_KSCRATCH1
... KVM_REG_MIPS_CP0_KSCRATCH6
:
2095 idx
= reg
->id
- KVM_REG_MIPS_CP0_KSCRATCH1
+ 2;
2096 if (!cpu_guest_has_kscr(idx
))
2100 *v
= (long)read_gc0_kscratch1();
2103 *v
= (long)read_gc0_kscratch2();
2106 *v
= (long)read_gc0_kscratch3();
2109 *v
= (long)read_gc0_kscratch4();
2112 *v
= (long)read_gc0_kscratch5();
2115 *v
= (long)read_gc0_kscratch6();
2119 case KVM_REG_MIPS_COUNT_CTL
:
2120 *v
= vcpu
->arch
.count_ctl
;
2122 case KVM_REG_MIPS_COUNT_RESUME
:
2123 *v
= ktime_to_ns(vcpu
->arch
.count_resume
);
2125 case KVM_REG_MIPS_COUNT_HZ
:
2126 *v
= vcpu
->arch
.count_hz
;
2134 static int kvm_vz_set_one_reg(struct kvm_vcpu
*vcpu
,
2135 const struct kvm_one_reg
*reg
,
2138 struct mips_coproc
*cop0
= &vcpu
->arch
.cop0
;
2141 unsigned int cur
, change
;
2144 case KVM_REG_MIPS_CP0_INDEX
:
2147 case KVM_REG_MIPS_CP0_ENTRYLO0
:
2148 write_gc0_entrylo0(entrylo_user_to_kvm(v
));
2150 case KVM_REG_MIPS_CP0_ENTRYLO1
:
2151 write_gc0_entrylo1(entrylo_user_to_kvm(v
));
2153 case KVM_REG_MIPS_CP0_CONTEXT
:
2154 write_gc0_context(v
);
2156 case KVM_REG_MIPS_CP0_CONTEXTCONFIG
:
2157 if (!cpu_guest_has_contextconfig
)
2159 write_gc0_contextconfig(v
);
2161 case KVM_REG_MIPS_CP0_USERLOCAL
:
2162 if (!cpu_guest_has_userlocal
)
2164 write_gc0_userlocal(v
);
2167 case KVM_REG_MIPS_CP0_XCONTEXTCONFIG
:
2168 if (!cpu_guest_has_contextconfig
)
2170 write_gc0_xcontextconfig(v
);
2173 case KVM_REG_MIPS_CP0_PAGEMASK
:
2174 write_gc0_pagemask(v
);
2176 case KVM_REG_MIPS_CP0_PAGEGRAIN
:
2177 write_gc0_pagegrain(v
);
2179 case KVM_REG_MIPS_CP0_SEGCTL0
:
2180 if (!cpu_guest_has_segments
)
2182 write_gc0_segctl0(v
);
2184 case KVM_REG_MIPS_CP0_SEGCTL1
:
2185 if (!cpu_guest_has_segments
)
2187 write_gc0_segctl1(v
);
2189 case KVM_REG_MIPS_CP0_SEGCTL2
:
2190 if (!cpu_guest_has_segments
)
2192 write_gc0_segctl2(v
);
2194 case KVM_REG_MIPS_CP0_PWBASE
:
2195 if (!cpu_guest_has_htw
&& !cpu_guest_has_ldpte
)
2197 write_gc0_pwbase(v
);
2199 case KVM_REG_MIPS_CP0_PWFIELD
:
2200 if (!cpu_guest_has_htw
&& !cpu_guest_has_ldpte
)
2202 write_gc0_pwfield(v
);
2204 case KVM_REG_MIPS_CP0_PWSIZE
:
2205 if (!cpu_guest_has_htw
&& !cpu_guest_has_ldpte
)
2207 write_gc0_pwsize(v
);
2209 case KVM_REG_MIPS_CP0_WIRED
:
2210 change_gc0_wired(MIPSR6_WIRED_WIRED
, v
);
2212 case KVM_REG_MIPS_CP0_PWCTL
:
2213 if (!cpu_guest_has_htw
&& !cpu_guest_has_ldpte
)
2217 case KVM_REG_MIPS_CP0_HWRENA
:
2218 write_gc0_hwrena(v
);
2220 case KVM_REG_MIPS_CP0_BADVADDR
:
2221 write_gc0_badvaddr(v
);
2223 case KVM_REG_MIPS_CP0_BADINSTR
:
2224 if (!cpu_guest_has_badinstr
)
2226 write_gc0_badinstr(v
);
2228 case KVM_REG_MIPS_CP0_BADINSTRP
:
2229 if (!cpu_guest_has_badinstrp
)
2231 write_gc0_badinstrp(v
);
2233 case KVM_REG_MIPS_CP0_COUNT
:
2234 kvm_mips_write_count(vcpu
, v
);
2236 case KVM_REG_MIPS_CP0_ENTRYHI
:
2237 write_gc0_entryhi(v
);
2239 case KVM_REG_MIPS_CP0_COMPARE
:
2240 kvm_mips_write_compare(vcpu
, v
, false);
2242 case KVM_REG_MIPS_CP0_STATUS
:
2243 write_gc0_status(v
);
2245 case KVM_REG_MIPS_CP0_INTCTL
:
2246 write_gc0_intctl(v
);
2248 case KVM_REG_MIPS_CP0_CAUSE
:
2250 * If the timer is stopped or started (DC bit) it must look
2251 * atomic with changes to the timer interrupt pending bit (TI).
2252 * A timer interrupt should not happen in between.
2254 if ((read_gc0_cause() ^ v
) & CAUSEF_DC
) {
2255 if (v
& CAUSEF_DC
) {
2256 /* disable timer first */
2257 kvm_mips_count_disable_cause(vcpu
);
2258 change_gc0_cause((u32
)~CAUSEF_DC
, v
);
2260 /* enable timer last */
2261 change_gc0_cause((u32
)~CAUSEF_DC
, v
);
2262 kvm_mips_count_enable_cause(vcpu
);
2268 case KVM_REG_MIPS_CP0_EPC
:
2271 case KVM_REG_MIPS_CP0_PRID
:
2272 switch (boot_cpu_type()) {
2273 case CPU_CAVIUM_OCTEON3
:
2274 /* Octeon III has a guest.PRid, but its read-only */
2277 kvm_write_c0_guest_prid(cop0
, v
);
2281 case KVM_REG_MIPS_CP0_EBASE
:
2282 kvm_vz_write_gc0_ebase(v
);
2284 case KVM_REG_MIPS_CP0_CONFIG
:
2285 cur
= read_gc0_config();
2286 change
= (cur
^ v
) & kvm_vz_config_user_wrmask(vcpu
);
2289 write_gc0_config(v
);
2292 case KVM_REG_MIPS_CP0_CONFIG1
:
2293 if (!cpu_guest_has_conf1
)
2295 cur
= read_gc0_config1();
2296 change
= (cur
^ v
) & kvm_vz_config1_user_wrmask(vcpu
);
2299 write_gc0_config1(v
);
2302 case KVM_REG_MIPS_CP0_CONFIG2
:
2303 if (!cpu_guest_has_conf2
)
2305 cur
= read_gc0_config2();
2306 change
= (cur
^ v
) & kvm_vz_config2_user_wrmask(vcpu
);
2309 write_gc0_config2(v
);
2312 case KVM_REG_MIPS_CP0_CONFIG3
:
2313 if (!cpu_guest_has_conf3
)
2315 cur
= read_gc0_config3();
2316 change
= (cur
^ v
) & kvm_vz_config3_user_wrmask(vcpu
);
2319 write_gc0_config3(v
);
2322 case KVM_REG_MIPS_CP0_CONFIG4
:
2323 if (!cpu_guest_has_conf4
)
2325 cur
= read_gc0_config4();
2326 change
= (cur
^ v
) & kvm_vz_config4_user_wrmask(vcpu
);
2329 write_gc0_config4(v
);
2332 case KVM_REG_MIPS_CP0_CONFIG5
:
2333 if (!cpu_guest_has_conf5
)
2335 cur
= read_gc0_config5();
2336 change
= (cur
^ v
) & kvm_vz_config5_user_wrmask(vcpu
);
2339 write_gc0_config5(v
);
2342 case KVM_REG_MIPS_CP0_CONFIG6
:
2343 cur
= kvm_read_sw_gc0_config6(cop0
);
2344 change
= (cur
^ v
) & kvm_vz_config6_user_wrmask(vcpu
);
2347 kvm_write_sw_gc0_config6(cop0
, (int)v
);
2350 case KVM_REG_MIPS_CP0_MAAR(0) ... KVM_REG_MIPS_CP0_MAAR(0x3f):
2351 if (!cpu_guest_has_maar
|| cpu_guest_has_dyn_maar
)
2353 idx
= reg
->id
- KVM_REG_MIPS_CP0_MAAR(0);
2354 if (idx
>= ARRAY_SIZE(vcpu
->arch
.maar
))
2356 vcpu
->arch
.maar
[idx
] = mips_process_maar(dmtc_op
, v
);
2358 case KVM_REG_MIPS_CP0_MAARI
:
2359 if (!cpu_guest_has_maar
|| cpu_guest_has_dyn_maar
)
2361 kvm_write_maari(vcpu
, v
);
2364 case KVM_REG_MIPS_CP0_XCONTEXT
:
2365 write_gc0_xcontext(v
);
2368 case KVM_REG_MIPS_CP0_ERROREPC
:
2369 write_gc0_errorepc(v
);
2371 case KVM_REG_MIPS_CP0_KSCRATCH1
... KVM_REG_MIPS_CP0_KSCRATCH6
:
2372 idx
= reg
->id
- KVM_REG_MIPS_CP0_KSCRATCH1
+ 2;
2373 if (!cpu_guest_has_kscr(idx
))
2377 write_gc0_kscratch1(v
);
2380 write_gc0_kscratch2(v
);
2383 write_gc0_kscratch3(v
);
2386 write_gc0_kscratch4(v
);
2389 write_gc0_kscratch5(v
);
2392 write_gc0_kscratch6(v
);
2396 case KVM_REG_MIPS_COUNT_CTL
:
2397 ret
= kvm_mips_set_count_ctl(vcpu
, v
);
2399 case KVM_REG_MIPS_COUNT_RESUME
:
2400 ret
= kvm_mips_set_count_resume(vcpu
, v
);
2402 case KVM_REG_MIPS_COUNT_HZ
:
2403 ret
= kvm_mips_set_count_hz(vcpu
, v
);
2411 #define guestid_cache(cpu) (cpu_data[cpu].guestid_cache)
2412 static void kvm_vz_get_new_guestid(unsigned long cpu
, struct kvm_vcpu
*vcpu
)
2414 unsigned long guestid
= guestid_cache(cpu
);
2416 if (!(++guestid
& GUESTID_MASK
)) {
2417 if (cpu_has_vtag_icache
)
2420 if (!guestid
) /* fix version if needed */
2421 guestid
= GUESTID_FIRST_VERSION
;
2423 ++guestid
; /* guestid 0 reserved for root */
2425 /* start new guestid cycle */
2426 kvm_vz_local_flush_roottlb_all_guests();
2427 kvm_vz_local_flush_guesttlb_all();
2430 guestid_cache(cpu
) = guestid
;
2433 /* Returns 1 if the guest TLB may be clobbered */
2434 static int kvm_vz_check_requests(struct kvm_vcpu
*vcpu
, int cpu
)
2439 if (!kvm_request_pending(vcpu
))
2442 if (kvm_check_request(KVM_REQ_TLB_FLUSH
, vcpu
)) {
2443 if (cpu_has_guestid
) {
2444 /* Drop all GuestIDs for this VCPU */
2445 for_each_possible_cpu(i
)
2446 vcpu
->arch
.vzguestid
[i
] = 0;
2447 /* This will clobber guest TLB contents too */
2451 * For Root ASID Dealias (RAD) we don't do anything here, but we
2452 * still need the request to ensure we recheck asid_flush_mask.
2453 * We can still return 0 as only the root TLB will be affected
2454 * by a root ASID flush.
2461 static void kvm_vz_vcpu_save_wired(struct kvm_vcpu
*vcpu
)
2463 unsigned int wired
= read_gc0_wired();
2464 struct kvm_mips_tlb
*tlbs
;
2467 /* Expand the wired TLB array if necessary */
2468 wired
&= MIPSR6_WIRED_WIRED
;
2469 if (wired
> vcpu
->arch
.wired_tlb_limit
) {
2470 tlbs
= krealloc(vcpu
->arch
.wired_tlb
, wired
*
2471 sizeof(*vcpu
->arch
.wired_tlb
), GFP_ATOMIC
);
2472 if (WARN_ON(!tlbs
)) {
2473 /* Save whatever we can */
2474 wired
= vcpu
->arch
.wired_tlb_limit
;
2476 vcpu
->arch
.wired_tlb
= tlbs
;
2477 vcpu
->arch
.wired_tlb_limit
= wired
;
2482 /* Save wired entries from the guest TLB */
2483 kvm_vz_save_guesttlb(vcpu
->arch
.wired_tlb
, 0, wired
);
2484 /* Invalidate any dropped entries since last time */
2485 for (i
= wired
; i
< vcpu
->arch
.wired_tlb_used
; ++i
) {
2486 vcpu
->arch
.wired_tlb
[i
].tlb_hi
= UNIQUE_GUEST_ENTRYHI(i
);
2487 vcpu
->arch
.wired_tlb
[i
].tlb_lo
[0] = 0;
2488 vcpu
->arch
.wired_tlb
[i
].tlb_lo
[1] = 0;
2489 vcpu
->arch
.wired_tlb
[i
].tlb_mask
= 0;
2491 vcpu
->arch
.wired_tlb_used
= wired
;
2494 static void kvm_vz_vcpu_load_wired(struct kvm_vcpu
*vcpu
)
2496 /* Load wired entries into the guest TLB */
2497 if (vcpu
->arch
.wired_tlb
)
2498 kvm_vz_load_guesttlb(vcpu
->arch
.wired_tlb
, 0,
2499 vcpu
->arch
.wired_tlb_used
);
2502 static void kvm_vz_vcpu_load_tlb(struct kvm_vcpu
*vcpu
, int cpu
)
2504 struct kvm
*kvm
= vcpu
->kvm
;
2505 struct mm_struct
*gpa_mm
= &kvm
->arch
.gpa_mm
;
2509 * Are we entering guest context on a different CPU to last time?
2510 * If so, the VCPU's guest TLB state on this CPU may be stale.
2512 migrated
= (vcpu
->arch
.last_exec_cpu
!= cpu
);
2513 vcpu
->arch
.last_exec_cpu
= cpu
;
2516 * A vcpu's GuestID is set in GuestCtl1.ID when the vcpu is loaded and
2517 * remains set until another vcpu is loaded in. As a rule GuestRID
2518 * remains zeroed when in root context unless the kernel is busy
2519 * manipulating guest tlb entries.
2521 if (cpu_has_guestid
) {
2523 * Check if our GuestID is of an older version and thus invalid.
2525 * We also discard the stored GuestID if we've executed on
2526 * another CPU, as the guest mappings may have changed without
2527 * hypervisor knowledge.
2530 (vcpu
->arch
.vzguestid
[cpu
] ^ guestid_cache(cpu
)) &
2531 GUESTID_VERSION_MASK
) {
2532 kvm_vz_get_new_guestid(cpu
, vcpu
);
2533 vcpu
->arch
.vzguestid
[cpu
] = guestid_cache(cpu
);
2534 trace_kvm_guestid_change(vcpu
,
2535 vcpu
->arch
.vzguestid
[cpu
]);
2538 /* Restore GuestID */
2539 change_c0_guestctl1(GUESTID_MASK
, vcpu
->arch
.vzguestid
[cpu
]);
2542 * The Guest TLB only stores a single guest's TLB state, so
2543 * flush it if another VCPU has executed on this CPU.
2545 * We also flush if we've executed on another CPU, as the guest
2546 * mappings may have changed without hypervisor knowledge.
2548 if (migrated
|| last_exec_vcpu
[cpu
] != vcpu
)
2549 kvm_vz_local_flush_guesttlb_all();
2550 last_exec_vcpu
[cpu
] = vcpu
;
2553 * Root ASID dealiases guest GPA mappings in the root TLB.
2554 * Allocate new root ASID if needed.
2556 if (cpumask_test_and_clear_cpu(cpu
, &kvm
->arch
.asid_flush_mask
))
2557 get_new_mmu_context(gpa_mm
);
2559 check_mmu_context(gpa_mm
);
2563 static int kvm_vz_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
2565 struct mips_coproc
*cop0
= &vcpu
->arch
.cop0
;
2569 * Have we migrated to a different CPU?
2570 * If so, any old guest TLB state may be stale.
2572 migrated
= (vcpu
->arch
.last_sched_cpu
!= cpu
);
2575 * Was this the last VCPU to run on this CPU?
2576 * If not, any old guest state from this VCPU will have been clobbered.
2578 all
= migrated
|| (last_vcpu
[cpu
] != vcpu
);
2579 last_vcpu
[cpu
] = vcpu
;
2582 * Restore CP0_Wired unconditionally as we clear it after use, and
2583 * restore wired guest TLB entries (while in guest context).
2585 kvm_restore_gc0_wired(cop0
);
2586 if (current
->flags
& PF_VCPU
) {
2588 kvm_vz_vcpu_load_tlb(vcpu
, cpu
);
2589 kvm_vz_vcpu_load_wired(vcpu
);
2593 * Restore timer state regardless, as e.g. Cause.TI can change over time
2594 * if left unmaintained.
2596 kvm_vz_restore_timer(vcpu
);
2598 /* Set MC bit if we want to trace guest mode changes */
2599 if (kvm_trace_guest_mode_change
)
2600 set_c0_guestctl0(MIPS_GCTL0_MC
);
2602 clear_c0_guestctl0(MIPS_GCTL0_MC
);
2604 /* Don't bother restoring registers multiple times unless necessary */
2609 * Restore config registers first, as some implementations restrict
2610 * writes to other registers when the corresponding feature bits aren't
2611 * set. For example Status.CU1 cannot be set unless Config1.FP is set.
2613 kvm_restore_gc0_config(cop0
);
2614 if (cpu_guest_has_conf1
)
2615 kvm_restore_gc0_config1(cop0
);
2616 if (cpu_guest_has_conf2
)
2617 kvm_restore_gc0_config2(cop0
);
2618 if (cpu_guest_has_conf3
)
2619 kvm_restore_gc0_config3(cop0
);
2620 if (cpu_guest_has_conf4
)
2621 kvm_restore_gc0_config4(cop0
);
2622 if (cpu_guest_has_conf5
)
2623 kvm_restore_gc0_config5(cop0
);
2624 if (cpu_guest_has_conf6
)
2625 kvm_restore_gc0_config6(cop0
);
2626 if (cpu_guest_has_conf7
)
2627 kvm_restore_gc0_config7(cop0
);
2629 kvm_restore_gc0_index(cop0
);
2630 kvm_restore_gc0_entrylo0(cop0
);
2631 kvm_restore_gc0_entrylo1(cop0
);
2632 kvm_restore_gc0_context(cop0
);
2633 if (cpu_guest_has_contextconfig
)
2634 kvm_restore_gc0_contextconfig(cop0
);
2636 kvm_restore_gc0_xcontext(cop0
);
2637 if (cpu_guest_has_contextconfig
)
2638 kvm_restore_gc0_xcontextconfig(cop0
);
2640 kvm_restore_gc0_pagemask(cop0
);
2641 kvm_restore_gc0_pagegrain(cop0
);
2642 kvm_restore_gc0_hwrena(cop0
);
2643 kvm_restore_gc0_badvaddr(cop0
);
2644 kvm_restore_gc0_entryhi(cop0
);
2645 kvm_restore_gc0_status(cop0
);
2646 kvm_restore_gc0_intctl(cop0
);
2647 kvm_restore_gc0_epc(cop0
);
2648 kvm_vz_write_gc0_ebase(kvm_read_sw_gc0_ebase(cop0
));
2649 if (cpu_guest_has_userlocal
)
2650 kvm_restore_gc0_userlocal(cop0
);
2652 kvm_restore_gc0_errorepc(cop0
);
2654 /* restore KScratch registers if enabled in guest */
2655 if (cpu_guest_has_conf4
) {
2656 if (cpu_guest_has_kscr(2))
2657 kvm_restore_gc0_kscratch1(cop0
);
2658 if (cpu_guest_has_kscr(3))
2659 kvm_restore_gc0_kscratch2(cop0
);
2660 if (cpu_guest_has_kscr(4))
2661 kvm_restore_gc0_kscratch3(cop0
);
2662 if (cpu_guest_has_kscr(5))
2663 kvm_restore_gc0_kscratch4(cop0
);
2664 if (cpu_guest_has_kscr(6))
2665 kvm_restore_gc0_kscratch5(cop0
);
2666 if (cpu_guest_has_kscr(7))
2667 kvm_restore_gc0_kscratch6(cop0
);
2670 if (cpu_guest_has_badinstr
)
2671 kvm_restore_gc0_badinstr(cop0
);
2672 if (cpu_guest_has_badinstrp
)
2673 kvm_restore_gc0_badinstrp(cop0
);
2675 if (cpu_guest_has_segments
) {
2676 kvm_restore_gc0_segctl0(cop0
);
2677 kvm_restore_gc0_segctl1(cop0
);
2678 kvm_restore_gc0_segctl2(cop0
);
2681 /* restore HTW registers */
2682 if (cpu_guest_has_htw
|| cpu_guest_has_ldpte
) {
2683 kvm_restore_gc0_pwbase(cop0
);
2684 kvm_restore_gc0_pwfield(cop0
);
2685 kvm_restore_gc0_pwsize(cop0
);
2686 kvm_restore_gc0_pwctl(cop0
);
2689 /* restore Root.GuestCtl2 from unused Guest guestctl2 register */
2690 if (cpu_has_guestctl2
)
2692 cop0
->reg
[MIPS_CP0_GUESTCTL2
][MIPS_CP0_GUESTCTL2_SEL
]);
2695 * We should clear linked load bit to break interrupted atomics. This
2696 * prevents a SC on the next VCPU from succeeding by matching a LL on
2697 * the previous VCPU.
2699 if (vcpu
->kvm
->created_vcpus
> 1)
2700 write_gc0_lladdr(0);
2705 static int kvm_vz_vcpu_put(struct kvm_vcpu
*vcpu
, int cpu
)
2707 struct mips_coproc
*cop0
= &vcpu
->arch
.cop0
;
2709 if (current
->flags
& PF_VCPU
)
2710 kvm_vz_vcpu_save_wired(vcpu
);
2714 kvm_save_gc0_index(cop0
);
2715 kvm_save_gc0_entrylo0(cop0
);
2716 kvm_save_gc0_entrylo1(cop0
);
2717 kvm_save_gc0_context(cop0
);
2718 if (cpu_guest_has_contextconfig
)
2719 kvm_save_gc0_contextconfig(cop0
);
2721 kvm_save_gc0_xcontext(cop0
);
2722 if (cpu_guest_has_contextconfig
)
2723 kvm_save_gc0_xcontextconfig(cop0
);
2725 kvm_save_gc0_pagemask(cop0
);
2726 kvm_save_gc0_pagegrain(cop0
);
2727 kvm_save_gc0_wired(cop0
);
2728 /* allow wired TLB entries to be overwritten */
2729 clear_gc0_wired(MIPSR6_WIRED_WIRED
);
2730 kvm_save_gc0_hwrena(cop0
);
2731 kvm_save_gc0_badvaddr(cop0
);
2732 kvm_save_gc0_entryhi(cop0
);
2733 kvm_save_gc0_status(cop0
);
2734 kvm_save_gc0_intctl(cop0
);
2735 kvm_save_gc0_epc(cop0
);
2736 kvm_write_sw_gc0_ebase(cop0
, kvm_vz_read_gc0_ebase());
2737 if (cpu_guest_has_userlocal
)
2738 kvm_save_gc0_userlocal(cop0
);
2740 /* only save implemented config registers */
2741 kvm_save_gc0_config(cop0
);
2742 if (cpu_guest_has_conf1
)
2743 kvm_save_gc0_config1(cop0
);
2744 if (cpu_guest_has_conf2
)
2745 kvm_save_gc0_config2(cop0
);
2746 if (cpu_guest_has_conf3
)
2747 kvm_save_gc0_config3(cop0
);
2748 if (cpu_guest_has_conf4
)
2749 kvm_save_gc0_config4(cop0
);
2750 if (cpu_guest_has_conf5
)
2751 kvm_save_gc0_config5(cop0
);
2752 if (cpu_guest_has_conf6
)
2753 kvm_save_gc0_config6(cop0
);
2754 if (cpu_guest_has_conf7
)
2755 kvm_save_gc0_config7(cop0
);
2757 kvm_save_gc0_errorepc(cop0
);
2759 /* save KScratch registers if enabled in guest */
2760 if (cpu_guest_has_conf4
) {
2761 if (cpu_guest_has_kscr(2))
2762 kvm_save_gc0_kscratch1(cop0
);
2763 if (cpu_guest_has_kscr(3))
2764 kvm_save_gc0_kscratch2(cop0
);
2765 if (cpu_guest_has_kscr(4))
2766 kvm_save_gc0_kscratch3(cop0
);
2767 if (cpu_guest_has_kscr(5))
2768 kvm_save_gc0_kscratch4(cop0
);
2769 if (cpu_guest_has_kscr(6))
2770 kvm_save_gc0_kscratch5(cop0
);
2771 if (cpu_guest_has_kscr(7))
2772 kvm_save_gc0_kscratch6(cop0
);
2775 if (cpu_guest_has_badinstr
)
2776 kvm_save_gc0_badinstr(cop0
);
2777 if (cpu_guest_has_badinstrp
)
2778 kvm_save_gc0_badinstrp(cop0
);
2780 if (cpu_guest_has_segments
) {
2781 kvm_save_gc0_segctl0(cop0
);
2782 kvm_save_gc0_segctl1(cop0
);
2783 kvm_save_gc0_segctl2(cop0
);
2786 /* save HTW registers if enabled in guest */
2787 if (cpu_guest_has_ldpte
|| (cpu_guest_has_htw
&&
2788 kvm_read_sw_gc0_config3(cop0
) & MIPS_CONF3_PW
)) {
2789 kvm_save_gc0_pwbase(cop0
);
2790 kvm_save_gc0_pwfield(cop0
);
2791 kvm_save_gc0_pwsize(cop0
);
2792 kvm_save_gc0_pwctl(cop0
);
2795 kvm_vz_save_timer(vcpu
);
2797 /* save Root.GuestCtl2 in unused Guest guestctl2 register */
2798 if (cpu_has_guestctl2
)
2799 cop0
->reg
[MIPS_CP0_GUESTCTL2
][MIPS_CP0_GUESTCTL2_SEL
] =
2800 read_c0_guestctl2();
2806 * kvm_vz_resize_guest_vtlb() - Attempt to resize guest VTLB.
2807 * @size: Number of guest VTLB entries (0 < @size <= root VTLB entries).
2809 * Attempt to resize the guest VTLB by writing guest Config registers. This is
2810 * necessary for cores with a shared root/guest TLB to avoid overlap with wired
2811 * entries in the root VTLB.
2813 * Returns: The resulting guest VTLB size.
2815 static unsigned int kvm_vz_resize_guest_vtlb(unsigned int size
)
2817 unsigned int config4
= 0, ret
= 0, limit
;
2819 /* Write MMUSize - 1 into guest Config registers */
2820 if (cpu_guest_has_conf1
)
2821 change_gc0_config1(MIPS_CONF1_TLBS
,
2822 (size
- 1) << MIPS_CONF1_TLBS_SHIFT
);
2823 if (cpu_guest_has_conf4
) {
2824 config4
= read_gc0_config4();
2825 if (cpu_has_mips_r6
|| (config4
& MIPS_CONF4_MMUEXTDEF
) ==
2826 MIPS_CONF4_MMUEXTDEF_VTLBSIZEEXT
) {
2827 config4
&= ~MIPS_CONF4_VTLBSIZEEXT
;
2828 config4
|= ((size
- 1) >> MIPS_CONF1_TLBS_SIZE
) <<
2829 MIPS_CONF4_VTLBSIZEEXT_SHIFT
;
2830 } else if ((config4
& MIPS_CONF4_MMUEXTDEF
) ==
2831 MIPS_CONF4_MMUEXTDEF_MMUSIZEEXT
) {
2832 config4
&= ~MIPS_CONF4_MMUSIZEEXT
;
2833 config4
|= ((size
- 1) >> MIPS_CONF1_TLBS_SIZE
) <<
2834 MIPS_CONF4_MMUSIZEEXT_SHIFT
;
2836 write_gc0_config4(config4
);
2840 * Set Guest.Wired.Limit = 0 (no limit up to Guest.MMUSize-1), unless it
2841 * would exceed Root.Wired.Limit (clearing Guest.Wired.Wired so write
2844 if (cpu_has_mips_r6
) {
2845 limit
= (read_c0_wired() & MIPSR6_WIRED_LIMIT
) >>
2846 MIPSR6_WIRED_LIMIT_SHIFT
;
2847 if (size
- 1 <= limit
)
2849 write_gc0_wired(limit
<< MIPSR6_WIRED_LIMIT_SHIFT
);
2852 /* Read back MMUSize - 1 */
2853 back_to_back_c0_hazard();
2854 if (cpu_guest_has_conf1
)
2855 ret
= (read_gc0_config1() & MIPS_CONF1_TLBS
) >>
2856 MIPS_CONF1_TLBS_SHIFT
;
2858 if (cpu_has_mips_r6
|| (config4
& MIPS_CONF4_MMUEXTDEF
) ==
2859 MIPS_CONF4_MMUEXTDEF_VTLBSIZEEXT
)
2860 ret
|= ((config4
& MIPS_CONF4_VTLBSIZEEXT
) >>
2861 MIPS_CONF4_VTLBSIZEEXT_SHIFT
) <<
2862 MIPS_CONF1_TLBS_SIZE
;
2863 else if ((config4
& MIPS_CONF4_MMUEXTDEF
) ==
2864 MIPS_CONF4_MMUEXTDEF_MMUSIZEEXT
)
2865 ret
|= ((config4
& MIPS_CONF4_MMUSIZEEXT
) >>
2866 MIPS_CONF4_MMUSIZEEXT_SHIFT
) <<
2867 MIPS_CONF1_TLBS_SIZE
;
2872 static int kvm_vz_enable_virtualization_cpu(void)
2874 unsigned int mmu_size
, guest_mmu_size
, ftlb_size
;
2875 u64 guest_cvmctl
, cvmvmconfig
;
2877 switch (current_cpu_type()) {
2878 case CPU_CAVIUM_OCTEON3
:
2879 /* Set up guest timer/perfcount IRQ lines */
2880 guest_cvmctl
= read_gc0_cvmctl();
2881 guest_cvmctl
&= ~CVMCTL_IPTI
;
2882 guest_cvmctl
|= 7ull << CVMCTL_IPTI_SHIFT
;
2883 guest_cvmctl
&= ~CVMCTL_IPPCI
;
2884 guest_cvmctl
|= 6ull << CVMCTL_IPPCI_SHIFT
;
2885 write_gc0_cvmctl(guest_cvmctl
);
2887 cvmvmconfig
= read_c0_cvmvmconfig();
2888 /* No I/O hole translation. */
2889 cvmvmconfig
|= CVMVMCONF_DGHT
;
2890 /* Halve the root MMU size */
2891 mmu_size
= ((cvmvmconfig
& CVMVMCONF_MMUSIZEM1
)
2892 >> CVMVMCONF_MMUSIZEM1_S
) + 1;
2893 guest_mmu_size
= mmu_size
/ 2;
2894 mmu_size
-= guest_mmu_size
;
2895 cvmvmconfig
&= ~CVMVMCONF_RMMUSIZEM1
;
2896 cvmvmconfig
|= mmu_size
- 1;
2897 write_c0_cvmvmconfig(cvmvmconfig
);
2899 /* Update our records */
2900 current_cpu_data
.tlbsize
= mmu_size
;
2901 current_cpu_data
.tlbsizevtlb
= mmu_size
;
2902 current_cpu_data
.guest
.tlbsize
= guest_mmu_size
;
2904 /* Flush moved entries in new (guest) context */
2905 kvm_vz_local_flush_guesttlb_all();
2909 * ImgTec cores tend to use a shared root/guest TLB. To avoid
2910 * overlap of root wired and guest entries, the guest TLB may
2913 mmu_size
= current_cpu_data
.tlbsizevtlb
;
2914 ftlb_size
= current_cpu_data
.tlbsize
- mmu_size
;
2916 /* Try switching to maximum guest VTLB size for flush */
2917 guest_mmu_size
= kvm_vz_resize_guest_vtlb(mmu_size
);
2918 current_cpu_data
.guest
.tlbsize
= guest_mmu_size
+ ftlb_size
;
2919 kvm_vz_local_flush_guesttlb_all();
2922 * Reduce to make space for root wired entries and at least 2
2923 * root non-wired entries. This does assume that long-term wired
2924 * entries won't be added later.
2926 guest_mmu_size
= mmu_size
- num_wired_entries() - 2;
2927 guest_mmu_size
= kvm_vz_resize_guest_vtlb(guest_mmu_size
);
2928 current_cpu_data
.guest
.tlbsize
= guest_mmu_size
+ ftlb_size
;
2931 * Write the VTLB size, but if another CPU has already written,
2932 * check it matches or we won't provide a consistent view to the
2933 * guest. If this ever happens it suggests an asymmetric number
2936 if (cmpxchg(&kvm_vz_guest_vtlb_size
, 0, guest_mmu_size
) &&
2937 WARN(guest_mmu_size
!= kvm_vz_guest_vtlb_size
,
2938 "Available guest VTLB size mismatch"))
2944 * Enable virtualization features granting guest direct control of
2946 * CP0=1: Guest coprocessor 0 context.
2947 * AT=Guest: Guest MMU.
2948 * CG=1: Hit (virtual address) CACHE operations (optional).
2949 * CF=1: Guest Config registers.
2950 * CGI=1: Indexed flush CACHE operations (optional).
2952 write_c0_guestctl0(MIPS_GCTL0_CP0
|
2953 (MIPS_GCTL0_AT_GUEST
<< MIPS_GCTL0_AT_SHIFT
) |
2954 MIPS_GCTL0_CG
| MIPS_GCTL0_CF
);
2955 if (cpu_has_guestctl0ext
) {
2956 if (current_cpu_type() != CPU_LOONGSON64
)
2957 set_c0_guestctl0ext(MIPS_GCTL0EXT_CGI
);
2959 clear_c0_guestctl0ext(MIPS_GCTL0EXT_CGI
);
2962 if (cpu_has_guestid
) {
2963 write_c0_guestctl1(0);
2964 kvm_vz_local_flush_roottlb_all_guests();
2966 GUESTID_MASK
= current_cpu_data
.guestid_mask
;
2967 GUESTID_FIRST_VERSION
= GUESTID_MASK
+ 1;
2968 GUESTID_VERSION_MASK
= ~GUESTID_MASK
;
2970 current_cpu_data
.guestid_cache
= GUESTID_FIRST_VERSION
;
2973 /* clear any pending injected virtual guest interrupts */
2974 if (cpu_has_guestctl2
)
2975 clear_c0_guestctl2(0x3f << 10);
2977 #ifdef CONFIG_CPU_LOONGSON64
2978 /* Control guest CCA attribute */
2980 csr_writel(csr_readl(0xffffffec) | 0x1, 0xffffffec);
2986 static void kvm_vz_disable_virtualization_cpu(void)
2989 unsigned int mmu_size
;
2991 /* Flush any remaining guest TLB entries */
2992 kvm_vz_local_flush_guesttlb_all();
2994 switch (current_cpu_type()) {
2995 case CPU_CAVIUM_OCTEON3
:
2997 * Allocate whole TLB for root. Existing guest TLB entries will
2998 * change ownership to the root TLB. We should be safe though as
2999 * they've already been flushed above while in guest TLB.
3001 cvmvmconfig
= read_c0_cvmvmconfig();
3002 mmu_size
= ((cvmvmconfig
& CVMVMCONF_MMUSIZEM1
)
3003 >> CVMVMCONF_MMUSIZEM1_S
) + 1;
3004 cvmvmconfig
&= ~CVMVMCONF_RMMUSIZEM1
;
3005 cvmvmconfig
|= mmu_size
- 1;
3006 write_c0_cvmvmconfig(cvmvmconfig
);
3008 /* Update our records */
3009 current_cpu_data
.tlbsize
= mmu_size
;
3010 current_cpu_data
.tlbsizevtlb
= mmu_size
;
3011 current_cpu_data
.guest
.tlbsize
= 0;
3013 /* Flush moved entries in new (root) context */
3014 local_flush_tlb_all();
3018 if (cpu_has_guestid
) {
3019 write_c0_guestctl1(0);
3020 kvm_vz_local_flush_roottlb_all_guests();
3024 static int kvm_vz_check_extension(struct kvm
*kvm
, long ext
)
3029 case KVM_CAP_MIPS_VZ
:
3030 /* we wouldn't be here unless cpu_has_vz */
3034 case KVM_CAP_MIPS_64BIT
:
3035 /* We support 64-bit registers/operations and addresses */
3039 case KVM_CAP_IOEVENTFD
:
3050 static int kvm_vz_vcpu_init(struct kvm_vcpu
*vcpu
)
3054 for_each_possible_cpu(i
)
3055 vcpu
->arch
.vzguestid
[i
] = 0;
3060 static void kvm_vz_vcpu_uninit(struct kvm_vcpu
*vcpu
)
3065 * If the VCPU is freed and reused as another VCPU, we don't want the
3066 * matching pointer wrongly hanging around in last_vcpu[] or
3069 for_each_possible_cpu(cpu
) {
3070 if (last_vcpu
[cpu
] == vcpu
)
3071 last_vcpu
[cpu
] = NULL
;
3072 if (last_exec_vcpu
[cpu
] == vcpu
)
3073 last_exec_vcpu
[cpu
] = NULL
;
3077 static int kvm_vz_vcpu_setup(struct kvm_vcpu
*vcpu
)
3079 struct mips_coproc
*cop0
= &vcpu
->arch
.cop0
;
3080 unsigned long count_hz
= 100*1000*1000; /* default to 100 MHz */
3083 * Start off the timer at the same frequency as the host timer, but the
3084 * soft timer doesn't handle frequencies greater than 1GHz yet.
3086 if (mips_hpt_frequency
&& mips_hpt_frequency
<= NSEC_PER_SEC
)
3087 count_hz
= mips_hpt_frequency
;
3088 kvm_mips_init_count(vcpu
, count_hz
);
3091 * Initialize guest register state to valid architectural reset state.
3095 if (cpu_has_mips_r5
|| cpu_has_mips_r6
)
3096 kvm_write_sw_gc0_pagegrain(cop0
, PG_RIE
| PG_XIE
| PG_IEC
);
3098 if (cpu_has_mips_r6
)
3099 kvm_write_sw_gc0_wired(cop0
,
3100 read_gc0_wired() & MIPSR6_WIRED_LIMIT
);
3102 kvm_write_sw_gc0_status(cop0
, ST0_BEV
| ST0_ERL
);
3103 if (cpu_has_mips_r5
|| cpu_has_mips_r6
)
3104 kvm_change_sw_gc0_status(cop0
, ST0_FR
, read_gc0_status());
3106 kvm_write_sw_gc0_intctl(cop0
, read_gc0_intctl() &
3107 (INTCTLF_IPFDC
| INTCTLF_IPPCI
| INTCTLF_IPTI
));
3109 kvm_write_sw_gc0_prid(cop0
, boot_cpu_data
.processor_id
);
3111 kvm_write_sw_gc0_ebase(cop0
, (s32
)0x80000000 | vcpu
->vcpu_id
);
3113 kvm_save_gc0_config(cop0
);
3114 /* architecturally writable (e.g. from guest) */
3115 kvm_change_sw_gc0_config(cop0
, CONF_CM_CMASK
,
3116 _page_cachable_default
>> _CACHE_SHIFT
);
3117 /* architecturally read only, but maybe writable from root */
3118 kvm_change_sw_gc0_config(cop0
, MIPS_CONF_MT
, read_c0_config());
3119 if (cpu_guest_has_conf1
) {
3120 kvm_set_sw_gc0_config(cop0
, MIPS_CONF_M
);
3122 kvm_save_gc0_config1(cop0
);
3123 /* architecturally read only, but maybe writable from root */
3124 kvm_clear_sw_gc0_config1(cop0
, MIPS_CONF1_C2
|
3131 if (cpu_guest_has_conf2
) {
3132 kvm_set_sw_gc0_config1(cop0
, MIPS_CONF_M
);
3134 kvm_save_gc0_config2(cop0
);
3136 if (cpu_guest_has_conf3
) {
3137 kvm_set_sw_gc0_config2(cop0
, MIPS_CONF_M
);
3139 kvm_save_gc0_config3(cop0
);
3140 /* architecturally writable (e.g. from guest) */
3141 kvm_clear_sw_gc0_config3(cop0
, MIPS_CONF3_ISA_OE
);
3142 /* architecturally read only, but maybe writable from root */
3143 kvm_clear_sw_gc0_config3(cop0
, MIPS_CONF3_MSA
|
3158 if (cpu_guest_has_conf4
) {
3159 kvm_set_sw_gc0_config3(cop0
, MIPS_CONF_M
);
3161 kvm_save_gc0_config4(cop0
);
3163 if (cpu_guest_has_conf5
) {
3164 kvm_set_sw_gc0_config4(cop0
, MIPS_CONF_M
);
3166 kvm_save_gc0_config5(cop0
);
3167 /* architecturally writable (e.g. from guest) */
3168 kvm_clear_sw_gc0_config5(cop0
, MIPS_CONF5_K
|
3175 /* architecturally read only, but maybe writable from root */
3176 kvm_clear_sw_gc0_config5(cop0
, MIPS_CONF5_MRP
);
3179 if (cpu_guest_has_contextconfig
) {
3181 kvm_write_sw_gc0_contextconfig(cop0
, 0x007ffff0);
3183 /* XContextConfig */
3184 /* bits SEGBITS-13+3:4 set */
3185 kvm_write_sw_gc0_xcontextconfig(cop0
,
3186 ((1ull << (cpu_vmbits
- 13)) - 1) << 4);
3190 /* Implementation dependent, use the legacy layout */
3191 if (cpu_guest_has_segments
) {
3192 /* SegCtl0, SegCtl1, SegCtl2 */
3193 kvm_write_sw_gc0_segctl0(cop0
, 0x00200010);
3194 kvm_write_sw_gc0_segctl1(cop0
, 0x00000002 |
3195 (_page_cachable_default
>> _CACHE_SHIFT
) <<
3196 (16 + MIPS_SEGCFG_C_SHIFT
));
3197 kvm_write_sw_gc0_segctl2(cop0
, 0x00380438);
3200 /* reset HTW registers */
3201 if (cpu_guest_has_htw
&& (cpu_has_mips_r5
|| cpu_has_mips_r6
)) {
3203 kvm_write_sw_gc0_pwfield(cop0
, 0x0c30c302);
3205 kvm_write_sw_gc0_pwsize(cop0
, 1 << MIPS_PWSIZE_PTW_SHIFT
);
3208 /* start with no pending virtual guest interrupts */
3209 if (cpu_has_guestctl2
)
3210 cop0
->reg
[MIPS_CP0_GUESTCTL2
][MIPS_CP0_GUESTCTL2_SEL
] = 0;
3212 /* Put PC at reset vector */
3213 vcpu
->arch
.pc
= CKSEG1ADDR(0x1fc00000);
3218 static void kvm_vz_prepare_flush_shadow(struct kvm
*kvm
)
3220 if (!cpu_has_guestid
) {
3222 * For each CPU there is a single GPA ASID used by all VCPUs in
3223 * the VM, so it doesn't make sense for the VCPUs to handle
3224 * invalidation of these ASIDs individually.
3226 * Instead mark all CPUs as needing ASID invalidation in
3227 * asid_flush_mask, and kvm_flush_remote_tlbs(kvm) will
3228 * kick any running VCPUs so they check asid_flush_mask.
3230 cpumask_setall(&kvm
->arch
.asid_flush_mask
);
3234 static void kvm_vz_vcpu_reenter(struct kvm_vcpu
*vcpu
)
3236 int cpu
= smp_processor_id();
3237 int preserve_guest_tlb
;
3239 preserve_guest_tlb
= kvm_vz_check_requests(vcpu
, cpu
);
3241 if (preserve_guest_tlb
)
3242 kvm_vz_vcpu_save_wired(vcpu
);
3244 kvm_vz_vcpu_load_tlb(vcpu
, cpu
);
3246 if (preserve_guest_tlb
)
3247 kvm_vz_vcpu_load_wired(vcpu
);
3250 static int kvm_vz_vcpu_run(struct kvm_vcpu
*vcpu
)
3252 int cpu
= smp_processor_id();
3255 kvm_vz_acquire_htimer(vcpu
);
3256 /* Check if we have any exceptions/interrupts pending */
3257 kvm_mips_deliver_interrupts(vcpu
, read_gc0_cause());
3259 kvm_vz_check_requests(vcpu
, cpu
);
3260 kvm_vz_vcpu_load_tlb(vcpu
, cpu
);
3261 kvm_vz_vcpu_load_wired(vcpu
);
3263 r
= vcpu
->arch
.vcpu_run(vcpu
);
3265 kvm_vz_vcpu_save_wired(vcpu
);
3270 static struct kvm_mips_callbacks kvm_vz_callbacks
= {
3271 .handle_cop_unusable
= kvm_trap_vz_handle_cop_unusable
,
3272 .handle_tlb_mod
= kvm_trap_vz_handle_tlb_st_miss
,
3273 .handle_tlb_ld_miss
= kvm_trap_vz_handle_tlb_ld_miss
,
3274 .handle_tlb_st_miss
= kvm_trap_vz_handle_tlb_st_miss
,
3275 .handle_addr_err_st
= kvm_trap_vz_no_handler
,
3276 .handle_addr_err_ld
= kvm_trap_vz_no_handler
,
3277 .handle_syscall
= kvm_trap_vz_no_handler
,
3278 .handle_res_inst
= kvm_trap_vz_no_handler
,
3279 .handle_break
= kvm_trap_vz_no_handler
,
3280 .handle_msa_disabled
= kvm_trap_vz_handle_msa_disabled
,
3281 .handle_guest_exit
= kvm_trap_vz_handle_guest_exit
,
3283 .enable_virtualization_cpu
= kvm_vz_enable_virtualization_cpu
,
3284 .disable_virtualization_cpu
= kvm_vz_disable_virtualization_cpu
,
3285 .check_extension
= kvm_vz_check_extension
,
3286 .vcpu_init
= kvm_vz_vcpu_init
,
3287 .vcpu_uninit
= kvm_vz_vcpu_uninit
,
3288 .vcpu_setup
= kvm_vz_vcpu_setup
,
3289 .prepare_flush_shadow
= kvm_vz_prepare_flush_shadow
,
3290 .gva_to_gpa
= kvm_vz_gva_to_gpa_cb
,
3291 .queue_timer_int
= kvm_vz_queue_timer_int_cb
,
3292 .dequeue_timer_int
= kvm_vz_dequeue_timer_int_cb
,
3293 .queue_io_int
= kvm_vz_queue_io_int_cb
,
3294 .dequeue_io_int
= kvm_vz_dequeue_io_int_cb
,
3295 .irq_deliver
= kvm_vz_irq_deliver_cb
,
3296 .irq_clear
= kvm_vz_irq_clear_cb
,
3297 .num_regs
= kvm_vz_num_regs
,
3298 .copy_reg_indices
= kvm_vz_copy_reg_indices
,
3299 .get_one_reg
= kvm_vz_get_one_reg
,
3300 .set_one_reg
= kvm_vz_set_one_reg
,
3301 .vcpu_load
= kvm_vz_vcpu_load
,
3302 .vcpu_put
= kvm_vz_vcpu_put
,
3303 .vcpu_run
= kvm_vz_vcpu_run
,
3304 .vcpu_reenter
= kvm_vz_vcpu_reenter
,
3307 /* FIXME: Get rid of the callbacks now that trap-and-emulate is gone. */
3308 const struct kvm_mips_callbacks
* const kvm_mips_callbacks
= &kvm_vz_callbacks
;
3310 int kvm_mips_emulation_init(void)
3316 * VZ requires at least 2 KScratch registers, so it should have been
3317 * possible to allocate pgd_reg.
3319 if (WARN(pgd_reg
== -1,
3320 "pgd_reg not allocated even though cpu_has_vz\n"))
3323 pr_info("Starting KVM with MIPS VZ extensions\n");