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
35 atomic_t kvm_mips_instance
;
36 EXPORT_SYMBOL_GPL(kvm_mips_instance
);
38 /* These function pointers are initialized once the KVM module is loaded */
39 kvm_pfn_t (*kvm_mips_gfn_to_pfn
)(struct kvm
*kvm
, gfn_t gfn
);
40 EXPORT_SYMBOL_GPL(kvm_mips_gfn_to_pfn
);
42 void (*kvm_mips_release_pfn_clean
)(kvm_pfn_t pfn
);
43 EXPORT_SYMBOL_GPL(kvm_mips_release_pfn_clean
);
45 bool (*kvm_mips_is_error_pfn
)(kvm_pfn_t pfn
);
46 EXPORT_SYMBOL_GPL(kvm_mips_is_error_pfn
);
48 u32
kvm_mips_get_kernel_asid(struct kvm_vcpu
*vcpu
)
50 int cpu
= smp_processor_id();
52 return vcpu
->arch
.guest_kernel_asid
[cpu
] &
53 cpu_asid_mask(&cpu_data
[cpu
]);
56 u32
kvm_mips_get_user_asid(struct kvm_vcpu
*vcpu
)
58 int cpu
= smp_processor_id();
60 return vcpu
->arch
.guest_user_asid
[cpu
] &
61 cpu_asid_mask(&cpu_data
[cpu
]);
64 inline u32
kvm_mips_get_commpage_asid(struct kvm_vcpu
*vcpu
)
66 return vcpu
->kvm
->arch
.commpage_tlb
;
69 /* Structure defining an tlb entry data set. */
71 void kvm_mips_dump_host_tlbs(void)
73 unsigned long old_entryhi
;
74 unsigned long old_pagemask
;
75 struct kvm_mips_tlb tlb
;
79 local_irq_save(flags
);
81 old_entryhi
= read_c0_entryhi();
82 old_pagemask
= read_c0_pagemask();
84 kvm_info("HOST TLBs:\n");
85 kvm_info("ASID: %#lx\n", read_c0_entryhi() &
86 cpu_asid_mask(¤t_cpu_data
));
88 for (i
= 0; i
< current_cpu_data
.tlbsize
; i
++) {
95 tlb
.tlb_hi
= read_c0_entryhi();
96 tlb
.tlb_lo0
= read_c0_entrylo0();
97 tlb
.tlb_lo1
= read_c0_entrylo1();
98 tlb
.tlb_mask
= read_c0_pagemask();
100 kvm_info("TLB%c%3d Hi 0x%08lx ",
101 (tlb
.tlb_lo0
| tlb
.tlb_lo1
) & MIPS3_PG_V
? ' ' : '*',
103 kvm_info("Lo0=0x%09llx %c%c attr %lx ",
104 (u64
) mips3_tlbpfn_to_paddr(tlb
.tlb_lo0
),
105 (tlb
.tlb_lo0
& MIPS3_PG_D
) ? 'D' : ' ',
106 (tlb
.tlb_lo0
& MIPS3_PG_G
) ? 'G' : ' ',
107 (tlb
.tlb_lo0
>> 3) & 7);
108 kvm_info("Lo1=0x%09llx %c%c attr %lx sz=%lx\n",
109 (u64
) mips3_tlbpfn_to_paddr(tlb
.tlb_lo1
),
110 (tlb
.tlb_lo1
& MIPS3_PG_D
) ? 'D' : ' ',
111 (tlb
.tlb_lo1
& MIPS3_PG_G
) ? 'G' : ' ',
112 (tlb
.tlb_lo1
>> 3) & 7, tlb
.tlb_mask
);
114 write_c0_entryhi(old_entryhi
);
115 write_c0_pagemask(old_pagemask
);
117 local_irq_restore(flags
);
119 EXPORT_SYMBOL_GPL(kvm_mips_dump_host_tlbs
);
121 void kvm_mips_dump_guest_tlbs(struct kvm_vcpu
*vcpu
)
123 struct mips_coproc
*cop0
= vcpu
->arch
.cop0
;
124 struct kvm_mips_tlb tlb
;
127 kvm_info("Guest TLBs:\n");
128 kvm_info("Guest EntryHi: %#lx\n", kvm_read_c0_guest_entryhi(cop0
));
130 for (i
= 0; i
< KVM_MIPS_GUEST_TLB_SIZE
; i
++) {
131 tlb
= vcpu
->arch
.guest_tlb
[i
];
132 kvm_info("TLB%c%3d Hi 0x%08lx ",
133 (tlb
.tlb_lo0
| tlb
.tlb_lo1
) & MIPS3_PG_V
? ' ' : '*',
135 kvm_info("Lo0=0x%09llx %c%c attr %lx ",
136 (u64
) mips3_tlbpfn_to_paddr(tlb
.tlb_lo0
),
137 (tlb
.tlb_lo0
& MIPS3_PG_D
) ? 'D' : ' ',
138 (tlb
.tlb_lo0
& MIPS3_PG_G
) ? 'G' : ' ',
139 (tlb
.tlb_lo0
>> 3) & 7);
140 kvm_info("Lo1=0x%09llx %c%c attr %lx sz=%lx\n",
141 (u64
) mips3_tlbpfn_to_paddr(tlb
.tlb_lo1
),
142 (tlb
.tlb_lo1
& MIPS3_PG_D
) ? 'D' : ' ',
143 (tlb
.tlb_lo1
& MIPS3_PG_G
) ? 'G' : ' ',
144 (tlb
.tlb_lo1
>> 3) & 7, tlb
.tlb_mask
);
147 EXPORT_SYMBOL_GPL(kvm_mips_dump_guest_tlbs
);
149 static int kvm_mips_map_page(struct kvm
*kvm
, gfn_t gfn
)
151 int srcu_idx
, err
= 0;
154 if (kvm
->arch
.guest_pmap
[gfn
] != KVM_INVALID_PAGE
)
157 srcu_idx
= srcu_read_lock(&kvm
->srcu
);
158 pfn
= kvm_mips_gfn_to_pfn(kvm
, gfn
);
160 if (kvm_mips_is_error_pfn(pfn
)) {
161 kvm_err("Couldn't get pfn for gfn %#llx!\n", gfn
);
166 kvm
->arch
.guest_pmap
[gfn
] = pfn
;
168 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
172 /* Translate guest KSEG0 addresses to Host PA */
173 unsigned long kvm_mips_translate_guest_kseg0_to_hpa(struct kvm_vcpu
*vcpu
,
177 unsigned long offset
= gva
& ~PAGE_MASK
;
178 struct kvm
*kvm
= vcpu
->kvm
;
180 if (KVM_GUEST_KSEGX(gva
) != KVM_GUEST_KSEG0
) {
181 kvm_err("%s/%p: Invalid gva: %#lx\n", __func__
,
182 __builtin_return_address(0), gva
);
183 return KVM_INVALID_PAGE
;
186 gfn
= (KVM_GUEST_CPHYSADDR(gva
) >> PAGE_SHIFT
);
188 if (gfn
>= kvm
->arch
.guest_pmap_npages
) {
189 kvm_err("%s: Invalid gfn: %#llx, GVA: %#lx\n", __func__
, gfn
,
191 return KVM_INVALID_PAGE
;
194 if (kvm_mips_map_page(vcpu
->kvm
, gfn
) < 0)
195 return KVM_INVALID_ADDR
;
197 return (kvm
->arch
.guest_pmap
[gfn
] << PAGE_SHIFT
) + offset
;
199 EXPORT_SYMBOL_GPL(kvm_mips_translate_guest_kseg0_to_hpa
);
201 /* XXXKYMA: Must be called with interrupts disabled */
202 /* set flush_dcache_mask == 0 if no dcache flush required */
203 int kvm_mips_host_tlb_write(struct kvm_vcpu
*vcpu
, unsigned long entryhi
,
204 unsigned long entrylo0
, unsigned long entrylo1
,
205 int flush_dcache_mask
)
208 unsigned long old_entryhi
;
211 local_irq_save(flags
);
213 old_entryhi
= read_c0_entryhi();
214 write_c0_entryhi(entryhi
);
219 idx
= read_c0_index();
221 if (idx
> current_cpu_data
.tlbsize
) {
222 kvm_err("%s: Invalid Index: %d\n", __func__
, idx
);
223 kvm_mips_dump_host_tlbs();
224 local_irq_restore(flags
);
228 write_c0_entrylo0(entrylo0
);
229 write_c0_entrylo1(entrylo1
);
238 kvm_debug("@ %#lx idx: %2d [entryhi(R): %#lx] entrylo0(R): 0x%08lx, entrylo1(R): 0x%08lx\n",
239 vcpu
->arch
.pc
, idx
, read_c0_entryhi(),
240 read_c0_entrylo0(), read_c0_entrylo1());
243 if (flush_dcache_mask
) {
244 if (entrylo0
& MIPS3_PG_V
) {
245 ++vcpu
->stat
.flush_dcache_exits
;
246 flush_data_cache_page((entryhi
& VPN2_MASK
) &
249 if (entrylo1
& MIPS3_PG_V
) {
250 ++vcpu
->stat
.flush_dcache_exits
;
251 flush_data_cache_page(((entryhi
& VPN2_MASK
) &
252 ~flush_dcache_mask
) |
253 (0x1 << PAGE_SHIFT
));
257 /* Restore old ASID */
258 write_c0_entryhi(old_entryhi
);
261 local_irq_restore(flags
);
265 /* XXXKYMA: Must be called with interrupts disabled */
266 int kvm_mips_handle_kseg0_tlb_fault(unsigned long badvaddr
,
267 struct kvm_vcpu
*vcpu
)
270 kvm_pfn_t pfn0
, pfn1
;
271 unsigned long vaddr
= 0;
272 unsigned long entryhi
= 0, entrylo0
= 0, entrylo1
= 0;
274 struct kvm
*kvm
= vcpu
->kvm
;
275 const int flush_dcache_mask
= 0;
278 if (KVM_GUEST_KSEGX(badvaddr
) != KVM_GUEST_KSEG0
) {
279 kvm_err("%s: Invalid BadVaddr: %#lx\n", __func__
, badvaddr
);
280 kvm_mips_dump_host_tlbs();
284 gfn
= (KVM_GUEST_CPHYSADDR(badvaddr
) >> PAGE_SHIFT
);
285 if (gfn
>= kvm
->arch
.guest_pmap_npages
) {
286 kvm_err("%s: Invalid gfn: %#llx, BadVaddr: %#lx\n", __func__
,
288 kvm_mips_dump_host_tlbs();
292 vaddr
= badvaddr
& (PAGE_MASK
<< 1);
294 if (kvm_mips_map_page(vcpu
->kvm
, gfn
) < 0)
297 if (kvm_mips_map_page(vcpu
->kvm
, gfn
^ 0x1) < 0)
301 pfn0
= kvm
->arch
.guest_pmap
[gfn
];
302 pfn1
= kvm
->arch
.guest_pmap
[gfn
^ 0x1];
304 pfn0
= kvm
->arch
.guest_pmap
[gfn
^ 0x1];
305 pfn1
= kvm
->arch
.guest_pmap
[gfn
];
308 entrylo0
= mips3_paddr_to_tlbpfn(pfn0
<< PAGE_SHIFT
) | (0x3 << 3) |
309 (1 << 2) | (0x1 << 1);
310 entrylo1
= mips3_paddr_to_tlbpfn(pfn1
<< PAGE_SHIFT
) | (0x3 << 3) |
311 (1 << 2) | (0x1 << 1);
314 entryhi
= (vaddr
| kvm_mips_get_kernel_asid(vcpu
));
315 ret
= kvm_mips_host_tlb_write(vcpu
, entryhi
, entrylo0
, entrylo1
,
321 EXPORT_SYMBOL_GPL(kvm_mips_handle_kseg0_tlb_fault
);
323 int kvm_mips_handle_commpage_tlb_fault(unsigned long badvaddr
,
324 struct kvm_vcpu
*vcpu
)
326 kvm_pfn_t pfn0
, pfn1
;
327 unsigned long flags
, old_entryhi
= 0, vaddr
= 0;
328 unsigned long entrylo0
= 0, entrylo1
= 0;
330 pfn0
= CPHYSADDR(vcpu
->arch
.kseg0_commpage
) >> PAGE_SHIFT
;
332 entrylo0
= mips3_paddr_to_tlbpfn(pfn0
<< PAGE_SHIFT
) | (0x3 << 3) |
333 (1 << 2) | (0x1 << 1);
336 local_irq_save(flags
);
338 old_entryhi
= read_c0_entryhi();
339 vaddr
= badvaddr
& (PAGE_MASK
<< 1);
340 write_c0_entryhi(vaddr
| kvm_mips_get_kernel_asid(vcpu
));
342 write_c0_entrylo0(entrylo0
);
344 write_c0_entrylo1(entrylo1
);
346 write_c0_index(kvm_mips_get_commpage_asid(vcpu
));
352 kvm_debug("@ %#lx idx: %2d [entryhi(R): %#lx] entrylo0 (R): 0x%08lx, entrylo1(R): 0x%08lx\n",
353 vcpu
->arch
.pc
, read_c0_index(), read_c0_entryhi(),
354 read_c0_entrylo0(), read_c0_entrylo1());
356 /* Restore old ASID */
357 write_c0_entryhi(old_entryhi
);
360 local_irq_restore(flags
);
364 EXPORT_SYMBOL_GPL(kvm_mips_handle_commpage_tlb_fault
);
366 int kvm_mips_handle_mapped_seg_tlb_fault(struct kvm_vcpu
*vcpu
,
367 struct kvm_mips_tlb
*tlb
,
371 unsigned long entryhi
= 0, entrylo0
= 0, entrylo1
= 0;
372 struct kvm
*kvm
= vcpu
->kvm
;
373 kvm_pfn_t pfn0
, pfn1
;
376 if ((tlb
->tlb_hi
& VPN2_MASK
) == 0) {
380 if (kvm_mips_map_page(kvm
, mips3_tlbpfn_to_paddr(tlb
->tlb_lo0
)
384 if (kvm_mips_map_page(kvm
, mips3_tlbpfn_to_paddr(tlb
->tlb_lo1
)
388 pfn0
= kvm
->arch
.guest_pmap
[mips3_tlbpfn_to_paddr(tlb
->tlb_lo0
)
390 pfn1
= kvm
->arch
.guest_pmap
[mips3_tlbpfn_to_paddr(tlb
->tlb_lo1
)
395 *hpa0
= pfn0
<< PAGE_SHIFT
;
398 *hpa1
= pfn1
<< PAGE_SHIFT
;
400 /* Get attributes from the Guest TLB */
401 entrylo0
= mips3_paddr_to_tlbpfn(pfn0
<< PAGE_SHIFT
) | (0x3 << 3) |
402 (tlb
->tlb_lo0
& MIPS3_PG_D
) | (tlb
->tlb_lo0
& MIPS3_PG_V
);
403 entrylo1
= mips3_paddr_to_tlbpfn(pfn1
<< PAGE_SHIFT
) | (0x3 << 3) |
404 (tlb
->tlb_lo1
& MIPS3_PG_D
) | (tlb
->tlb_lo1
& MIPS3_PG_V
);
406 kvm_debug("@ %#lx tlb_lo0: 0x%08lx tlb_lo1: 0x%08lx\n", vcpu
->arch
.pc
,
407 tlb
->tlb_lo0
, tlb
->tlb_lo1
);
410 entryhi
= (tlb
->tlb_hi
& VPN2_MASK
) | (KVM_GUEST_KERNEL_MODE(vcpu
) ?
411 kvm_mips_get_kernel_asid(vcpu
) :
412 kvm_mips_get_user_asid(vcpu
));
413 ret
= kvm_mips_host_tlb_write(vcpu
, entryhi
, entrylo0
, entrylo1
,
419 EXPORT_SYMBOL_GPL(kvm_mips_handle_mapped_seg_tlb_fault
);
421 int kvm_mips_guest_tlb_lookup(struct kvm_vcpu
*vcpu
, unsigned long entryhi
)
425 struct kvm_mips_tlb
*tlb
= vcpu
->arch
.guest_tlb
;
427 for (i
= 0; i
< KVM_MIPS_GUEST_TLB_SIZE
; i
++) {
428 if (TLB_HI_VPN2_HIT(tlb
[i
], entryhi
) &&
429 TLB_HI_ASID_HIT(tlb
[i
], entryhi
)) {
435 kvm_debug("%s: entryhi: %#lx, index: %d lo0: %#lx, lo1: %#lx\n",
436 __func__
, entryhi
, index
, tlb
[i
].tlb_lo0
, tlb
[i
].tlb_lo1
);
440 EXPORT_SYMBOL_GPL(kvm_mips_guest_tlb_lookup
);
442 int kvm_mips_host_tlb_lookup(struct kvm_vcpu
*vcpu
, unsigned long vaddr
)
444 unsigned long old_entryhi
, flags
;
447 local_irq_save(flags
);
449 old_entryhi
= read_c0_entryhi();
451 if (KVM_GUEST_KERNEL_MODE(vcpu
))
452 write_c0_entryhi((vaddr
& VPN2_MASK
) |
453 kvm_mips_get_kernel_asid(vcpu
));
455 write_c0_entryhi((vaddr
& VPN2_MASK
) |
456 kvm_mips_get_user_asid(vcpu
));
463 idx
= read_c0_index();
465 /* Restore old ASID */
466 write_c0_entryhi(old_entryhi
);
470 local_irq_restore(flags
);
472 kvm_debug("Host TLB lookup, %#lx, idx: %2d\n", vaddr
, idx
);
476 EXPORT_SYMBOL_GPL(kvm_mips_host_tlb_lookup
);
478 int kvm_mips_host_tlb_inv(struct kvm_vcpu
*vcpu
, unsigned long va
)
481 unsigned long flags
, old_entryhi
;
483 local_irq_save(flags
);
485 old_entryhi
= read_c0_entryhi();
487 write_c0_entryhi((va
& VPN2_MASK
) | kvm_mips_get_user_asid(vcpu
));
492 idx
= read_c0_index();
494 if (idx
>= current_cpu_data
.tlbsize
)
498 write_c0_entryhi(UNIQUE_ENTRYHI(idx
));
501 write_c0_entrylo0(0);
504 write_c0_entrylo1(0);
511 write_c0_entryhi(old_entryhi
);
515 local_irq_restore(flags
);
518 kvm_debug("%s: Invalidated entryhi %#lx @ idx %d\n", __func__
,
519 (va
& VPN2_MASK
) | kvm_mips_get_user_asid(vcpu
), idx
);
523 EXPORT_SYMBOL_GPL(kvm_mips_host_tlb_inv
);
525 void kvm_mips_flush_host_tlb(int skip_kseg0
)
528 unsigned long old_entryhi
, entryhi
;
529 unsigned long old_pagemask
;
531 int maxentry
= current_cpu_data
.tlbsize
;
533 local_irq_save(flags
);
535 old_entryhi
= read_c0_entryhi();
536 old_pagemask
= read_c0_pagemask();
538 /* Blast 'em all away. */
539 for (entry
= 0; entry
< maxentry
; entry
++) {
540 write_c0_index(entry
);
547 entryhi
= read_c0_entryhi();
549 /* Don't blow away guest kernel entries */
550 if (KVM_GUEST_KSEGX(entryhi
) == KVM_GUEST_KSEG0
)
554 /* Make sure all entries differ. */
555 write_c0_entryhi(UNIQUE_ENTRYHI(entry
));
557 write_c0_entrylo0(0);
559 write_c0_entrylo1(0);
568 write_c0_entryhi(old_entryhi
);
569 write_c0_pagemask(old_pagemask
);
573 local_irq_restore(flags
);
575 EXPORT_SYMBOL_GPL(kvm_mips_flush_host_tlb
);
577 void kvm_get_new_mmu_context(struct mm_struct
*mm
, unsigned long cpu
,
578 struct kvm_vcpu
*vcpu
)
580 unsigned long asid
= asid_cache(cpu
);
582 asid
+= cpu_asid_inc();
583 if (!(asid
& cpu_asid_mask(&cpu_data
[cpu
]))) {
584 if (cpu_has_vtag_icache
)
587 kvm_local_flush_tlb_all(); /* start new asid cycle */
589 if (!asid
) /* fix version if needed */
590 asid
= asid_first_version(cpu
);
593 cpu_context(cpu
, mm
) = asid_cache(cpu
) = asid
;
596 void kvm_local_flush_tlb_all(void)
599 unsigned long old_ctx
;
602 local_irq_save(flags
);
603 /* Save old context and create impossible VPN2 value */
604 old_ctx
= read_c0_entryhi();
605 write_c0_entrylo0(0);
606 write_c0_entrylo1(0);
608 /* Blast 'em all away. */
609 while (entry
< current_cpu_data
.tlbsize
) {
610 /* Make sure all entries differ. */
611 write_c0_entryhi(UNIQUE_ENTRYHI(entry
));
612 write_c0_index(entry
);
618 write_c0_entryhi(old_ctx
);
621 local_irq_restore(flags
);
623 EXPORT_SYMBOL_GPL(kvm_local_flush_tlb_all
);
626 * kvm_mips_migrate_count() - Migrate timer.
627 * @vcpu: Virtual CPU.
629 * Migrate CP0_Count hrtimer to the current CPU by cancelling and restarting it
630 * if it was running prior to being cancelled.
632 * Must be called when the VCPU is migrated to a different CPU to ensure that
633 * timer expiry during guest execution interrupts the guest and causes the
634 * interrupt to be delivered in a timely manner.
636 static void kvm_mips_migrate_count(struct kvm_vcpu
*vcpu
)
638 if (hrtimer_cancel(&vcpu
->arch
.comparecount_timer
))
639 hrtimer_restart(&vcpu
->arch
.comparecount_timer
);
642 /* Restore ASID once we are scheduled back after preemption */
643 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
645 unsigned long asid_mask
= cpu_asid_mask(&cpu_data
[cpu
]);
649 kvm_debug("%s: vcpu %p, cpu: %d\n", __func__
, vcpu
, cpu
);
651 /* Allocate new kernel and user ASIDs if needed */
653 local_irq_save(flags
);
655 if ((vcpu
->arch
.guest_kernel_asid
[cpu
] ^ asid_cache(cpu
)) &
656 asid_version_mask(cpu
)) {
657 kvm_get_new_mmu_context(&vcpu
->arch
.guest_kernel_mm
, cpu
, vcpu
);
658 vcpu
->arch
.guest_kernel_asid
[cpu
] =
659 vcpu
->arch
.guest_kernel_mm
.context
.asid
[cpu
];
660 kvm_get_new_mmu_context(&vcpu
->arch
.guest_user_mm
, cpu
, vcpu
);
661 vcpu
->arch
.guest_user_asid
[cpu
] =
662 vcpu
->arch
.guest_user_mm
.context
.asid
[cpu
];
665 kvm_debug("[%d]: cpu_context: %#lx\n", cpu
,
666 cpu_context(cpu
, current
->mm
));
667 kvm_debug("[%d]: Allocated new ASID for Guest Kernel: %#x\n",
668 cpu
, vcpu
->arch
.guest_kernel_asid
[cpu
]);
669 kvm_debug("[%d]: Allocated new ASID for Guest User: %#x\n", cpu
,
670 vcpu
->arch
.guest_user_asid
[cpu
]);
673 if (vcpu
->arch
.last_sched_cpu
!= cpu
) {
674 kvm_debug("[%d->%d]KVM VCPU[%d] switch\n",
675 vcpu
->arch
.last_sched_cpu
, cpu
, vcpu
->vcpu_id
);
677 * Migrate the timer interrupt to the current CPU so that it
678 * always interrupts the guest and synchronously triggers a
679 * guest timer interrupt.
681 kvm_mips_migrate_count(vcpu
);
686 * If we preempted while the guest was executing, then reload
687 * the pre-empted ASID
689 if (current
->flags
& PF_VCPU
) {
690 write_c0_entryhi(vcpu
->arch
.
691 preempt_entryhi
& asid_mask
);
695 /* New ASIDs were allocated for the VM */
698 * Were we in guest context? If so then the pre-empted ASID is
699 * no longer valid, we need to set it to what it should be based
700 * on the mode of the Guest (Kernel/User)
702 if (current
->flags
& PF_VCPU
) {
703 if (KVM_GUEST_KERNEL_MODE(vcpu
))
704 write_c0_entryhi(vcpu
->arch
.
705 guest_kernel_asid
[cpu
] &
708 write_c0_entryhi(vcpu
->arch
.
709 guest_user_asid
[cpu
] &
715 /* restore guest state to registers */
716 kvm_mips_callbacks
->vcpu_set_regs(vcpu
);
718 local_irq_restore(flags
);
721 EXPORT_SYMBOL_GPL(kvm_arch_vcpu_load
);
723 /* ASID can change if another task is scheduled during preemption */
724 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
729 local_irq_save(flags
);
731 cpu
= smp_processor_id();
733 vcpu
->arch
.preempt_entryhi
= read_c0_entryhi();
734 vcpu
->arch
.last_sched_cpu
= cpu
;
736 /* save guest state in registers */
737 kvm_mips_callbacks
->vcpu_get_regs(vcpu
);
739 if (((cpu_context(cpu
, current
->mm
) ^ asid_cache(cpu
)) &
740 asid_version_mask(cpu
))) {
741 kvm_debug("%s: Dropping MMU Context: %#lx\n", __func__
,
742 cpu_context(cpu
, current
->mm
));
743 drop_mmu_context(current
->mm
, cpu
);
745 write_c0_entryhi(cpu_asid(cpu
, current
->mm
));
748 local_irq_restore(flags
);
750 EXPORT_SYMBOL_GPL(kvm_arch_vcpu_put
);
752 u32
kvm_get_inst(u32
*opc
, struct kvm_vcpu
*vcpu
)
754 struct mips_coproc
*cop0
= vcpu
->arch
.cop0
;
755 unsigned long paddr
, flags
, vpn2
, asid
;
759 if (KVM_GUEST_KSEGX((unsigned long) opc
) < KVM_GUEST_KSEG0
||
760 KVM_GUEST_KSEGX((unsigned long) opc
) == KVM_GUEST_KSEG23
) {
761 local_irq_save(flags
);
762 index
= kvm_mips_host_tlb_lookup(vcpu
, (unsigned long) opc
);
766 vpn2
= (unsigned long) opc
& VPN2_MASK
;
767 asid
= kvm_read_c0_guest_entryhi(cop0
) &
769 index
= kvm_mips_guest_tlb_lookup(vcpu
, vpn2
| asid
);
771 kvm_err("%s: get_user_failed for %p, vcpu: %p, ASID: %#lx\n",
772 __func__
, opc
, vcpu
, read_c0_entryhi());
773 kvm_mips_dump_host_tlbs();
774 local_irq_restore(flags
);
775 return KVM_INVALID_INST
;
777 kvm_mips_handle_mapped_seg_tlb_fault(vcpu
,
783 local_irq_restore(flags
);
784 } else if (KVM_GUEST_KSEGX(opc
) == KVM_GUEST_KSEG0
) {
786 kvm_mips_translate_guest_kseg0_to_hpa(vcpu
,
787 (unsigned long) opc
);
788 inst
= *(u32
*) CKSEG0ADDR(paddr
);
790 kvm_err("%s: illegal address: %p\n", __func__
, opc
);
791 return KVM_INVALID_INST
;
796 EXPORT_SYMBOL_GPL(kvm_get_inst
);