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 TLB handling, this file is part of the Linux host kernel so that
7 * TLB handlers run from KSEG0
9 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
10 * Authors: Sanjay Lal <sanjayl@kymasys.com>
13 #include <linux/sched.h>
14 #include <linux/smp.h>
16 #include <linux/delay.h>
17 #include <linux/module.h>
18 #include <linux/kvm_host.h>
19 #include <linux/srcu.h>
22 #include <asm/bootinfo.h>
23 #include <asm/mmu_context.h>
24 #include <asm/pgtable.h>
25 #include <asm/cacheflush.h>
29 #include <asm/r4kcache.h>
30 #define CONFIG_MIPS_MT
32 #define KVM_GUEST_PC_TLB 0
33 #define KVM_GUEST_SP_TLB 1
37 atomic_t kvm_mips_instance
;
38 EXPORT_SYMBOL(kvm_mips_instance
);
40 /* These function pointers are initialized once the KVM module is loaded */
41 pfn_t (*kvm_mips_gfn_to_pfn
)(struct kvm
*kvm
, gfn_t gfn
);
42 EXPORT_SYMBOL(kvm_mips_gfn_to_pfn
);
44 void (*kvm_mips_release_pfn_clean
)(pfn_t pfn
);
45 EXPORT_SYMBOL(kvm_mips_release_pfn_clean
);
47 bool (*kvm_mips_is_error_pfn
)(pfn_t pfn
);
48 EXPORT_SYMBOL(kvm_mips_is_error_pfn
);
50 uint32_t kvm_mips_get_kernel_asid(struct kvm_vcpu
*vcpu
)
52 return vcpu
->arch
.guest_kernel_asid
[smp_processor_id()] & ASID_MASK
;
55 uint32_t kvm_mips_get_user_asid(struct kvm_vcpu
*vcpu
)
57 return vcpu
->arch
.guest_user_asid
[smp_processor_id()] & ASID_MASK
;
60 inline uint32_t kvm_mips_get_commpage_asid(struct kvm_vcpu
*vcpu
)
62 return vcpu
->kvm
->arch
.commpage_tlb
;
65 /* Structure defining an tlb entry data set. */
67 void kvm_mips_dump_host_tlbs(void)
69 unsigned long old_entryhi
;
70 unsigned long old_pagemask
;
71 struct kvm_mips_tlb tlb
;
75 local_irq_save(flags
);
77 old_entryhi
= read_c0_entryhi();
78 old_pagemask
= read_c0_pagemask();
80 kvm_info("HOST TLBs:\n");
81 kvm_info("ASID: %#lx\n", read_c0_entryhi() & ASID_MASK
);
83 for (i
= 0; i
< current_cpu_data
.tlbsize
; i
++) {
90 tlb
.tlb_hi
= read_c0_entryhi();
91 tlb
.tlb_lo0
= read_c0_entrylo0();
92 tlb
.tlb_lo1
= read_c0_entrylo1();
93 tlb
.tlb_mask
= read_c0_pagemask();
95 kvm_info("TLB%c%3d Hi 0x%08lx ",
96 (tlb
.tlb_lo0
| tlb
.tlb_lo1
) & MIPS3_PG_V
? ' ' : '*',
98 kvm_info("Lo0=0x%09" PRIx64
" %c%c attr %lx ",
99 (uint64_t) mips3_tlbpfn_to_paddr(tlb
.tlb_lo0
),
100 (tlb
.tlb_lo0
& MIPS3_PG_D
) ? 'D' : ' ',
101 (tlb
.tlb_lo0
& MIPS3_PG_G
) ? 'G' : ' ',
102 (tlb
.tlb_lo0
>> 3) & 7);
103 kvm_info("Lo1=0x%09" PRIx64
" %c%c attr %lx sz=%lx\n",
104 (uint64_t) mips3_tlbpfn_to_paddr(tlb
.tlb_lo1
),
105 (tlb
.tlb_lo1
& MIPS3_PG_D
) ? 'D' : ' ',
106 (tlb
.tlb_lo1
& MIPS3_PG_G
) ? 'G' : ' ',
107 (tlb
.tlb_lo1
>> 3) & 7, tlb
.tlb_mask
);
109 write_c0_entryhi(old_entryhi
);
110 write_c0_pagemask(old_pagemask
);
112 local_irq_restore(flags
);
114 EXPORT_SYMBOL(kvm_mips_dump_host_tlbs
);
116 void kvm_mips_dump_guest_tlbs(struct kvm_vcpu
*vcpu
)
118 struct mips_coproc
*cop0
= vcpu
->arch
.cop0
;
119 struct kvm_mips_tlb tlb
;
122 kvm_info("Guest TLBs:\n");
123 kvm_info("Guest EntryHi: %#lx\n", kvm_read_c0_guest_entryhi(cop0
));
125 for (i
= 0; i
< KVM_MIPS_GUEST_TLB_SIZE
; i
++) {
126 tlb
= vcpu
->arch
.guest_tlb
[i
];
127 kvm_info("TLB%c%3d Hi 0x%08lx ",
128 (tlb
.tlb_lo0
| tlb
.tlb_lo1
) & MIPS3_PG_V
? ' ' : '*',
130 kvm_info("Lo0=0x%09" PRIx64
" %c%c attr %lx ",
131 (uint64_t) mips3_tlbpfn_to_paddr(tlb
.tlb_lo0
),
132 (tlb
.tlb_lo0
& MIPS3_PG_D
) ? 'D' : ' ',
133 (tlb
.tlb_lo0
& MIPS3_PG_G
) ? 'G' : ' ',
134 (tlb
.tlb_lo0
>> 3) & 7);
135 kvm_info("Lo1=0x%09" PRIx64
" %c%c attr %lx sz=%lx\n",
136 (uint64_t) mips3_tlbpfn_to_paddr(tlb
.tlb_lo1
),
137 (tlb
.tlb_lo1
& MIPS3_PG_D
) ? 'D' : ' ',
138 (tlb
.tlb_lo1
& MIPS3_PG_G
) ? 'G' : ' ',
139 (tlb
.tlb_lo1
>> 3) & 7, tlb
.tlb_mask
);
142 EXPORT_SYMBOL(kvm_mips_dump_guest_tlbs
);
144 static int kvm_mips_map_page(struct kvm
*kvm
, gfn_t gfn
)
146 int srcu_idx
, err
= 0;
149 if (kvm
->arch
.guest_pmap
[gfn
] != KVM_INVALID_PAGE
)
152 srcu_idx
= srcu_read_lock(&kvm
->srcu
);
153 pfn
= kvm_mips_gfn_to_pfn(kvm
, gfn
);
155 if (kvm_mips_is_error_pfn(pfn
)) {
156 kvm_err("Couldn't get pfn for gfn %#" PRIx64
"!\n", gfn
);
161 kvm
->arch
.guest_pmap
[gfn
] = pfn
;
163 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
167 /* Translate guest KSEG0 addresses to Host PA */
168 unsigned long kvm_mips_translate_guest_kseg0_to_hpa(struct kvm_vcpu
*vcpu
,
172 uint32_t offset
= gva
& ~PAGE_MASK
;
173 struct kvm
*kvm
= vcpu
->kvm
;
175 if (KVM_GUEST_KSEGX(gva
) != KVM_GUEST_KSEG0
) {
176 kvm_err("%s/%p: Invalid gva: %#lx\n", __func__
,
177 __builtin_return_address(0), gva
);
178 return KVM_INVALID_PAGE
;
181 gfn
= (KVM_GUEST_CPHYSADDR(gva
) >> PAGE_SHIFT
);
183 if (gfn
>= kvm
->arch
.guest_pmap_npages
) {
184 kvm_err("%s: Invalid gfn: %#llx, GVA: %#lx\n", __func__
, gfn
,
186 return KVM_INVALID_PAGE
;
189 if (kvm_mips_map_page(vcpu
->kvm
, gfn
) < 0)
190 return KVM_INVALID_ADDR
;
192 return (kvm
->arch
.guest_pmap
[gfn
] << PAGE_SHIFT
) + offset
;
194 EXPORT_SYMBOL(kvm_mips_translate_guest_kseg0_to_hpa
);
196 /* XXXKYMA: Must be called with interrupts disabled */
197 /* set flush_dcache_mask == 0 if no dcache flush required */
198 int kvm_mips_host_tlb_write(struct kvm_vcpu
*vcpu
, unsigned long entryhi
,
199 unsigned long entrylo0
, unsigned long entrylo1
,
200 int flush_dcache_mask
)
203 unsigned long old_entryhi
;
206 local_irq_save(flags
);
208 old_entryhi
= read_c0_entryhi();
209 write_c0_entryhi(entryhi
);
214 idx
= read_c0_index();
216 if (idx
> current_cpu_data
.tlbsize
) {
217 kvm_err("%s: Invalid Index: %d\n", __func__
, idx
);
218 kvm_mips_dump_host_tlbs();
219 local_irq_restore(flags
);
223 write_c0_entrylo0(entrylo0
);
224 write_c0_entrylo1(entrylo1
);
233 kvm_debug("@ %#lx idx: %2d [entryhi(R): %#lx] entrylo0(R): 0x%08lx, entrylo1(R): 0x%08lx\n",
234 vcpu
->arch
.pc
, idx
, read_c0_entryhi(),
235 read_c0_entrylo0(), read_c0_entrylo1());
238 if (flush_dcache_mask
) {
239 if (entrylo0
& MIPS3_PG_V
) {
240 ++vcpu
->stat
.flush_dcache_exits
;
241 flush_data_cache_page((entryhi
& VPN2_MASK
) &
244 if (entrylo1
& MIPS3_PG_V
) {
245 ++vcpu
->stat
.flush_dcache_exits
;
246 flush_data_cache_page(((entryhi
& VPN2_MASK
) &
247 ~flush_dcache_mask
) |
248 (0x1 << PAGE_SHIFT
));
252 /* Restore old ASID */
253 write_c0_entryhi(old_entryhi
);
256 local_irq_restore(flags
);
260 /* XXXKYMA: Must be called with interrupts disabled */
261 int kvm_mips_handle_kseg0_tlb_fault(unsigned long badvaddr
,
262 struct kvm_vcpu
*vcpu
)
266 unsigned long vaddr
= 0;
267 unsigned long entryhi
= 0, entrylo0
= 0, entrylo1
= 0;
269 struct kvm
*kvm
= vcpu
->kvm
;
270 const int flush_dcache_mask
= 0;
272 if (KVM_GUEST_KSEGX(badvaddr
) != KVM_GUEST_KSEG0
) {
273 kvm_err("%s: Invalid BadVaddr: %#lx\n", __func__
, badvaddr
);
274 kvm_mips_dump_host_tlbs();
278 gfn
= (KVM_GUEST_CPHYSADDR(badvaddr
) >> PAGE_SHIFT
);
279 if (gfn
>= kvm
->arch
.guest_pmap_npages
) {
280 kvm_err("%s: Invalid gfn: %#llx, BadVaddr: %#lx\n", __func__
,
282 kvm_mips_dump_host_tlbs();
286 vaddr
= badvaddr
& (PAGE_MASK
<< 1);
288 if (kvm_mips_map_page(vcpu
->kvm
, gfn
) < 0)
291 if (kvm_mips_map_page(vcpu
->kvm
, gfn
^ 0x1) < 0)
295 pfn0
= kvm
->arch
.guest_pmap
[gfn
];
296 pfn1
= kvm
->arch
.guest_pmap
[gfn
^ 0x1];
298 pfn0
= kvm
->arch
.guest_pmap
[gfn
^ 0x1];
299 pfn1
= kvm
->arch
.guest_pmap
[gfn
];
302 entryhi
= (vaddr
| kvm_mips_get_kernel_asid(vcpu
));
303 entrylo0
= mips3_paddr_to_tlbpfn(pfn0
<< PAGE_SHIFT
) | (0x3 << 3) |
304 (1 << 2) | (0x1 << 1);
305 entrylo1
= mips3_paddr_to_tlbpfn(pfn1
<< PAGE_SHIFT
) | (0x3 << 3) |
306 (1 << 2) | (0x1 << 1);
308 return kvm_mips_host_tlb_write(vcpu
, entryhi
, entrylo0
, entrylo1
,
311 EXPORT_SYMBOL(kvm_mips_handle_kseg0_tlb_fault
);
313 int kvm_mips_handle_commpage_tlb_fault(unsigned long badvaddr
,
314 struct kvm_vcpu
*vcpu
)
317 unsigned long flags
, old_entryhi
= 0, vaddr
= 0;
318 unsigned long entrylo0
= 0, entrylo1
= 0;
320 pfn0
= CPHYSADDR(vcpu
->arch
.kseg0_commpage
) >> PAGE_SHIFT
;
322 entrylo0
= mips3_paddr_to_tlbpfn(pfn0
<< PAGE_SHIFT
) | (0x3 << 3) |
323 (1 << 2) | (0x1 << 1);
326 local_irq_save(flags
);
328 old_entryhi
= read_c0_entryhi();
329 vaddr
= badvaddr
& (PAGE_MASK
<< 1);
330 write_c0_entryhi(vaddr
| kvm_mips_get_kernel_asid(vcpu
));
332 write_c0_entrylo0(entrylo0
);
334 write_c0_entrylo1(entrylo1
);
336 write_c0_index(kvm_mips_get_commpage_asid(vcpu
));
342 kvm_debug("@ %#lx idx: %2d [entryhi(R): %#lx] entrylo0 (R): 0x%08lx, entrylo1(R): 0x%08lx\n",
343 vcpu
->arch
.pc
, read_c0_index(), read_c0_entryhi(),
344 read_c0_entrylo0(), read_c0_entrylo1());
346 /* Restore old ASID */
347 write_c0_entryhi(old_entryhi
);
350 local_irq_restore(flags
);
354 EXPORT_SYMBOL(kvm_mips_handle_commpage_tlb_fault
);
356 int kvm_mips_handle_mapped_seg_tlb_fault(struct kvm_vcpu
*vcpu
,
357 struct kvm_mips_tlb
*tlb
,
361 unsigned long entryhi
= 0, entrylo0
= 0, entrylo1
= 0;
362 struct kvm
*kvm
= vcpu
->kvm
;
365 if ((tlb
->tlb_hi
& VPN2_MASK
) == 0) {
369 if (kvm_mips_map_page(kvm
, mips3_tlbpfn_to_paddr(tlb
->tlb_lo0
)
373 if (kvm_mips_map_page(kvm
, mips3_tlbpfn_to_paddr(tlb
->tlb_lo1
)
377 pfn0
= kvm
->arch
.guest_pmap
[mips3_tlbpfn_to_paddr(tlb
->tlb_lo0
)
379 pfn1
= kvm
->arch
.guest_pmap
[mips3_tlbpfn_to_paddr(tlb
->tlb_lo1
)
384 *hpa0
= pfn0
<< PAGE_SHIFT
;
387 *hpa1
= pfn1
<< PAGE_SHIFT
;
389 /* Get attributes from the Guest TLB */
390 entryhi
= (tlb
->tlb_hi
& VPN2_MASK
) | (KVM_GUEST_KERNEL_MODE(vcpu
) ?
391 kvm_mips_get_kernel_asid(vcpu
) :
392 kvm_mips_get_user_asid(vcpu
));
393 entrylo0
= mips3_paddr_to_tlbpfn(pfn0
<< PAGE_SHIFT
) | (0x3 << 3) |
394 (tlb
->tlb_lo0
& MIPS3_PG_D
) | (tlb
->tlb_lo0
& MIPS3_PG_V
);
395 entrylo1
= mips3_paddr_to_tlbpfn(pfn1
<< PAGE_SHIFT
) | (0x3 << 3) |
396 (tlb
->tlb_lo1
& MIPS3_PG_D
) | (tlb
->tlb_lo1
& MIPS3_PG_V
);
398 kvm_debug("@ %#lx tlb_lo0: 0x%08lx tlb_lo1: 0x%08lx\n", vcpu
->arch
.pc
,
399 tlb
->tlb_lo0
, tlb
->tlb_lo1
);
401 return kvm_mips_host_tlb_write(vcpu
, entryhi
, entrylo0
, entrylo1
,
404 EXPORT_SYMBOL(kvm_mips_handle_mapped_seg_tlb_fault
);
406 int kvm_mips_guest_tlb_lookup(struct kvm_vcpu
*vcpu
, unsigned long entryhi
)
410 struct kvm_mips_tlb
*tlb
= vcpu
->arch
.guest_tlb
;
412 for (i
= 0; i
< KVM_MIPS_GUEST_TLB_SIZE
; i
++) {
413 if (TLB_HI_VPN2_HIT(tlb
[i
], entryhi
) &&
414 TLB_HI_ASID_HIT(tlb
[i
], entryhi
)) {
420 kvm_debug("%s: entryhi: %#lx, index: %d lo0: %#lx, lo1: %#lx\n",
421 __func__
, entryhi
, index
, tlb
[i
].tlb_lo0
, tlb
[i
].tlb_lo1
);
425 EXPORT_SYMBOL(kvm_mips_guest_tlb_lookup
);
427 int kvm_mips_host_tlb_lookup(struct kvm_vcpu
*vcpu
, unsigned long vaddr
)
429 unsigned long old_entryhi
, flags
;
432 local_irq_save(flags
);
434 old_entryhi
= read_c0_entryhi();
436 if (KVM_GUEST_KERNEL_MODE(vcpu
))
437 write_c0_entryhi((vaddr
& VPN2_MASK
) |
438 kvm_mips_get_kernel_asid(vcpu
));
440 write_c0_entryhi((vaddr
& VPN2_MASK
) |
441 kvm_mips_get_user_asid(vcpu
));
448 idx
= read_c0_index();
450 /* Restore old ASID */
451 write_c0_entryhi(old_entryhi
);
455 local_irq_restore(flags
);
457 kvm_debug("Host TLB lookup, %#lx, idx: %2d\n", vaddr
, idx
);
461 EXPORT_SYMBOL(kvm_mips_host_tlb_lookup
);
463 int kvm_mips_host_tlb_inv(struct kvm_vcpu
*vcpu
, unsigned long va
)
466 unsigned long flags
, old_entryhi
;
468 local_irq_save(flags
);
470 old_entryhi
= read_c0_entryhi();
472 write_c0_entryhi((va
& VPN2_MASK
) | kvm_mips_get_user_asid(vcpu
));
477 idx
= read_c0_index();
479 if (idx
>= current_cpu_data
.tlbsize
)
483 write_c0_entryhi(UNIQUE_ENTRYHI(idx
));
486 write_c0_entrylo0(0);
489 write_c0_entrylo1(0);
496 write_c0_entryhi(old_entryhi
);
500 local_irq_restore(flags
);
503 kvm_debug("%s: Invalidated entryhi %#lx @ idx %d\n", __func__
,
504 (va
& VPN2_MASK
) | kvm_mips_get_user_asid(vcpu
), idx
);
508 EXPORT_SYMBOL(kvm_mips_host_tlb_inv
);
510 /* XXXKYMA: Fix Guest USER/KERNEL no longer share the same ASID */
511 int kvm_mips_host_tlb_inv_index(struct kvm_vcpu
*vcpu
, int index
)
513 unsigned long flags
, old_entryhi
;
515 if (index
>= current_cpu_data
.tlbsize
)
518 local_irq_save(flags
);
520 old_entryhi
= read_c0_entryhi();
522 write_c0_entryhi(UNIQUE_ENTRYHI(index
));
525 write_c0_index(index
);
528 write_c0_entrylo0(0);
531 write_c0_entrylo1(0);
538 write_c0_entryhi(old_entryhi
);
542 local_irq_restore(flags
);
547 void kvm_mips_flush_host_tlb(int skip_kseg0
)
550 unsigned long old_entryhi
, entryhi
;
551 unsigned long old_pagemask
;
553 int maxentry
= current_cpu_data
.tlbsize
;
555 local_irq_save(flags
);
557 old_entryhi
= read_c0_entryhi();
558 old_pagemask
= read_c0_pagemask();
560 /* Blast 'em all away. */
561 for (entry
= 0; entry
< maxentry
; entry
++) {
562 write_c0_index(entry
);
569 entryhi
= read_c0_entryhi();
571 /* Don't blow away guest kernel entries */
572 if (KVM_GUEST_KSEGX(entryhi
) == KVM_GUEST_KSEG0
)
576 /* Make sure all entries differ. */
577 write_c0_entryhi(UNIQUE_ENTRYHI(entry
));
579 write_c0_entrylo0(0);
581 write_c0_entrylo1(0);
590 write_c0_entryhi(old_entryhi
);
591 write_c0_pagemask(old_pagemask
);
595 local_irq_restore(flags
);
597 EXPORT_SYMBOL(kvm_mips_flush_host_tlb
);
599 void kvm_get_new_mmu_context(struct mm_struct
*mm
, unsigned long cpu
,
600 struct kvm_vcpu
*vcpu
)
602 unsigned long asid
= asid_cache(cpu
);
605 if (!(asid
& ASID_MASK
)) {
606 if (cpu_has_vtag_icache
)
609 kvm_local_flush_tlb_all(); /* start new asid cycle */
611 if (!asid
) /* fix version if needed */
612 asid
= ASID_FIRST_VERSION
;
615 cpu_context(cpu
, mm
) = asid_cache(cpu
) = asid
;
618 void kvm_local_flush_tlb_all(void)
621 unsigned long old_ctx
;
624 local_irq_save(flags
);
625 /* Save old context and create impossible VPN2 value */
626 old_ctx
= read_c0_entryhi();
627 write_c0_entrylo0(0);
628 write_c0_entrylo1(0);
630 /* Blast 'em all away. */
631 while (entry
< current_cpu_data
.tlbsize
) {
632 /* Make sure all entries differ. */
633 write_c0_entryhi(UNIQUE_ENTRYHI(entry
));
634 write_c0_index(entry
);
640 write_c0_entryhi(old_ctx
);
643 local_irq_restore(flags
);
645 EXPORT_SYMBOL(kvm_local_flush_tlb_all
);
648 * kvm_mips_migrate_count() - Migrate timer.
649 * @vcpu: Virtual CPU.
651 * Migrate CP0_Count hrtimer to the current CPU by cancelling and restarting it
652 * if it was running prior to being cancelled.
654 * Must be called when the VCPU is migrated to a different CPU to ensure that
655 * timer expiry during guest execution interrupts the guest and causes the
656 * interrupt to be delivered in a timely manner.
658 static void kvm_mips_migrate_count(struct kvm_vcpu
*vcpu
)
660 if (hrtimer_cancel(&vcpu
->arch
.comparecount_timer
))
661 hrtimer_restart(&vcpu
->arch
.comparecount_timer
);
664 /* Restore ASID once we are scheduled back after preemption */
665 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
670 kvm_debug("%s: vcpu %p, cpu: %d\n", __func__
, vcpu
, cpu
);
672 /* Alocate new kernel and user ASIDs if needed */
674 local_irq_save(flags
);
677 guest_kernel_asid
[cpu
] ^ asid_cache(cpu
)) & ASID_VERSION_MASK
)) {
678 kvm_get_new_mmu_context(&vcpu
->arch
.guest_kernel_mm
, cpu
, vcpu
);
679 vcpu
->arch
.guest_kernel_asid
[cpu
] =
680 vcpu
->arch
.guest_kernel_mm
.context
.asid
[cpu
];
681 kvm_get_new_mmu_context(&vcpu
->arch
.guest_user_mm
, cpu
, vcpu
);
682 vcpu
->arch
.guest_user_asid
[cpu
] =
683 vcpu
->arch
.guest_user_mm
.context
.asid
[cpu
];
686 kvm_debug("[%d]: cpu_context: %#lx\n", cpu
,
687 cpu_context(cpu
, current
->mm
));
688 kvm_debug("[%d]: Allocated new ASID for Guest Kernel: %#x\n",
689 cpu
, vcpu
->arch
.guest_kernel_asid
[cpu
]);
690 kvm_debug("[%d]: Allocated new ASID for Guest User: %#x\n", cpu
,
691 vcpu
->arch
.guest_user_asid
[cpu
]);
694 if (vcpu
->arch
.last_sched_cpu
!= cpu
) {
695 kvm_debug("[%d->%d]KVM VCPU[%d] switch\n",
696 vcpu
->arch
.last_sched_cpu
, cpu
, vcpu
->vcpu_id
);
698 * Migrate the timer interrupt to the current CPU so that it
699 * always interrupts the guest and synchronously triggers a
700 * guest timer interrupt.
702 kvm_mips_migrate_count(vcpu
);
707 * If we preempted while the guest was executing, then reload
708 * the pre-empted ASID
710 if (current
->flags
& PF_VCPU
) {
711 write_c0_entryhi(vcpu
->arch
.
712 preempt_entryhi
& ASID_MASK
);
716 /* New ASIDs were allocated for the VM */
719 * Were we in guest context? If so then the pre-empted ASID is
720 * no longer valid, we need to set it to what it should be based
721 * on the mode of the Guest (Kernel/User)
723 if (current
->flags
& PF_VCPU
) {
724 if (KVM_GUEST_KERNEL_MODE(vcpu
))
725 write_c0_entryhi(vcpu
->arch
.
726 guest_kernel_asid
[cpu
] &
729 write_c0_entryhi(vcpu
->arch
.
730 guest_user_asid
[cpu
] &
736 /* restore guest state to registers */
737 kvm_mips_callbacks
->vcpu_set_regs(vcpu
);
739 local_irq_restore(flags
);
742 EXPORT_SYMBOL(kvm_arch_vcpu_load
);
744 /* ASID can change if another task is scheduled during preemption */
745 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
750 local_irq_save(flags
);
752 cpu
= smp_processor_id();
754 vcpu
->arch
.preempt_entryhi
= read_c0_entryhi();
755 vcpu
->arch
.last_sched_cpu
= cpu
;
757 /* save guest state in registers */
758 kvm_mips_callbacks
->vcpu_get_regs(vcpu
);
760 if (((cpu_context(cpu
, current
->mm
) ^ asid_cache(cpu
)) &
761 ASID_VERSION_MASK
)) {
762 kvm_debug("%s: Dropping MMU Context: %#lx\n", __func__
,
763 cpu_context(cpu
, current
->mm
));
764 drop_mmu_context(current
->mm
, cpu
);
766 write_c0_entryhi(cpu_asid(cpu
, current
->mm
));
769 local_irq_restore(flags
);
771 EXPORT_SYMBOL(kvm_arch_vcpu_put
);
773 uint32_t kvm_get_inst(uint32_t *opc
, struct kvm_vcpu
*vcpu
)
775 struct mips_coproc
*cop0
= vcpu
->arch
.cop0
;
776 unsigned long paddr
, flags
, vpn2
, asid
;
780 if (KVM_GUEST_KSEGX((unsigned long) opc
) < KVM_GUEST_KSEG0
||
781 KVM_GUEST_KSEGX((unsigned long) opc
) == KVM_GUEST_KSEG23
) {
782 local_irq_save(flags
);
783 index
= kvm_mips_host_tlb_lookup(vcpu
, (unsigned long) opc
);
787 vpn2
= (unsigned long) opc
& VPN2_MASK
;
788 asid
= kvm_read_c0_guest_entryhi(cop0
) & ASID_MASK
;
789 index
= kvm_mips_guest_tlb_lookup(vcpu
, vpn2
| asid
);
791 kvm_err("%s: get_user_failed for %p, vcpu: %p, ASID: %#lx\n",
792 __func__
, opc
, vcpu
, read_c0_entryhi());
793 kvm_mips_dump_host_tlbs();
794 local_irq_restore(flags
);
795 return KVM_INVALID_INST
;
797 kvm_mips_handle_mapped_seg_tlb_fault(vcpu
,
803 local_irq_restore(flags
);
804 } else if (KVM_GUEST_KSEGX(opc
) == KVM_GUEST_KSEG0
) {
806 kvm_mips_translate_guest_kseg0_to_hpa(vcpu
,
807 (unsigned long) opc
);
808 inst
= *(uint32_t *) CKSEG0ADDR(paddr
);
810 kvm_err("%s: illegal address: %p\n", __func__
, opc
);
811 return KVM_INVALID_INST
;
816 EXPORT_SYMBOL(kvm_get_inst
);