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.
13 #include <linux/sched.h>
14 #include <linux/smp.h>
16 #include <linux/delay.h>
17 #include <linux/export.h>
18 #include <linux/kvm_host.h>
19 #include <linux/srcu.h>
22 #include <asm/bootinfo.h>
23 #include <asm/mipsregs.h>
24 #include <asm/mmu_context.h>
25 #include <asm/pgtable.h>
26 #include <asm/cacheflush.h>
28 #include <asm/tlbdebug.h>
31 #include <asm/r4kcache.h>
32 #define CONFIG_MIPS_MT
34 #define KVM_GUEST_PC_TLB 0
35 #define KVM_GUEST_SP_TLB 1
37 #ifdef CONFIG_KVM_MIPS_VZ
38 unsigned long GUESTID_MASK;
39 EXPORT_SYMBOL_GPL(GUESTID_MASK);
40 unsigned long GUESTID_FIRST_VERSION;
41 EXPORT_SYMBOL_GPL(GUESTID_FIRST_VERSION);
42 unsigned long GUESTID_VERSION_MASK;
43 EXPORT_SYMBOL_GPL(GUESTID_VERSION_MASK);
45 static u32 kvm_mips_get_root_asid(struct kvm_vcpu *vcpu)
47 struct mm_struct *gpa_mm = &vcpu->kvm->arch.gpa_mm;
52 return cpu_asid(smp_processor_id(), gpa_mm);
56 static u32 kvm_mips_get_kernel_asid(struct kvm_vcpu *vcpu)
58 struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm;
59 int cpu = smp_processor_id();
61 return cpu_asid(cpu, kern_mm);
64 static u32 kvm_mips_get_user_asid(struct kvm_vcpu *vcpu)
66 struct mm_struct *user_mm = &vcpu->arch.guest_user_mm;
67 int cpu = smp_processor_id();
69 return cpu_asid(cpu, user_mm);
72 /* Structure defining an tlb entry data set. */
74 void kvm_mips_dump_host_tlbs(void)
78 local_irq_save(flags);
80 kvm_info("HOST TLBs:\n");
85 local_irq_restore(flags);
87 EXPORT_SYMBOL_GPL(kvm_mips_dump_host_tlbs);
89 void kvm_mips_dump_guest_tlbs(struct kvm_vcpu *vcpu)
91 struct mips_coproc *cop0 = vcpu->arch.cop0;
92 struct kvm_mips_tlb tlb;
95 kvm_info("Guest TLBs:\n");
96 kvm_info("Guest EntryHi: %#lx\n", kvm_read_c0_guest_entryhi(cop0));
98 for (i = 0; i < KVM_MIPS_GUEST_TLB_SIZE; i++) {
99 tlb = vcpu->arch.guest_tlb[i];
100 kvm_info("TLB%c%3d Hi 0x%08lx ",
101 (tlb.tlb_lo[0] | tlb.tlb_lo[1]) & ENTRYLO_V
104 kvm_info("Lo0=0x%09llx %c%c attr %lx ",
105 (u64) mips3_tlbpfn_to_paddr(tlb.tlb_lo[0]),
106 (tlb.tlb_lo[0] & ENTRYLO_D) ? 'D' : ' ',
107 (tlb.tlb_lo[0] & ENTRYLO_G) ? 'G' : ' ',
108 (tlb.tlb_lo[0] & ENTRYLO_C) >> ENTRYLO_C_SHIFT);
109 kvm_info("Lo1=0x%09llx %c%c attr %lx sz=%lx\n",
110 (u64) mips3_tlbpfn_to_paddr(tlb.tlb_lo[1]),
111 (tlb.tlb_lo[1] & ENTRYLO_D) ? 'D' : ' ',
112 (tlb.tlb_lo[1] & ENTRYLO_G) ? 'G' : ' ',
113 (tlb.tlb_lo[1] & ENTRYLO_C) >> ENTRYLO_C_SHIFT,
117 EXPORT_SYMBOL_GPL(kvm_mips_dump_guest_tlbs);
119 int kvm_mips_guest_tlb_lookup(struct kvm_vcpu *vcpu, unsigned long entryhi)
123 struct kvm_mips_tlb *tlb = vcpu->arch.guest_tlb;
125 for (i = 0; i < KVM_MIPS_GUEST_TLB_SIZE; i++) {
126 if (TLB_HI_VPN2_HIT(tlb[i], entryhi) &&
127 TLB_HI_ASID_HIT(tlb[i], entryhi)) {
133 kvm_debug("%s: entryhi: %#lx, index: %d lo0: %#lx, lo1: %#lx\n",
134 __func__, entryhi, index, tlb[i].tlb_lo[0], tlb[i].tlb_lo[1]);
138 EXPORT_SYMBOL_GPL(kvm_mips_guest_tlb_lookup);
140 static int _kvm_mips_host_tlb_inv(unsigned long entryhi)
144 write_c0_entryhi(entryhi);
149 idx = read_c0_index();
151 if (idx >= current_cpu_data.tlbsize)
155 write_c0_entryhi(UNIQUE_ENTRYHI(idx));
156 write_c0_entrylo0(0);
157 write_c0_entrylo1(0);
167 int kvm_mips_host_tlb_inv(struct kvm_vcpu *vcpu, unsigned long va,
168 bool user, bool kernel)
171 * Initialize idx_user and idx_kernel to workaround bogus
172 * maybe-initialized warning when using GCC 6.
174 int idx_user = 0, idx_kernel = 0;
175 unsigned long flags, old_entryhi;
177 local_irq_save(flags);
179 old_entryhi = read_c0_entryhi();
182 idx_user = _kvm_mips_host_tlb_inv((va & VPN2_MASK) |
183 kvm_mips_get_user_asid(vcpu));
185 idx_kernel = _kvm_mips_host_tlb_inv((va & VPN2_MASK) |
186 kvm_mips_get_kernel_asid(vcpu));
188 write_c0_entryhi(old_entryhi);
191 local_irq_restore(flags);
194 * We don't want to get reserved instruction exceptions for missing tlb
197 if (cpu_has_vtag_icache)
200 if (user && idx_user >= 0)
201 kvm_debug("%s: Invalidated guest user entryhi %#lx @ idx %d\n",
202 __func__, (va & VPN2_MASK) |
203 kvm_mips_get_user_asid(vcpu), idx_user);
204 if (kernel && idx_kernel >= 0)
205 kvm_debug("%s: Invalidated guest kernel entryhi %#lx @ idx %d\n",
206 __func__, (va & VPN2_MASK) |
207 kvm_mips_get_kernel_asid(vcpu), idx_kernel);
211 EXPORT_SYMBOL_GPL(kvm_mips_host_tlb_inv);
213 #ifdef CONFIG_KVM_MIPS_VZ
215 /* GuestID management */
218 * clear_root_gid() - Set GuestCtl1.RID for normal root operation.
220 static inline void clear_root_gid(void)
222 if (cpu_has_guestid) {
223 clear_c0_guestctl1(MIPS_GCTL1_RID);
229 * set_root_gid_to_guest_gid() - Set GuestCtl1.RID to match GuestCtl1.ID.
231 * Sets the root GuestID to match the current guest GuestID, for TLB operation
232 * on the GPA->RPA mappings in the root TLB.
234 * The caller must be sure to disable HTW while the root GID is set, and
235 * possibly longer if TLB registers are modified.
237 static inline void set_root_gid_to_guest_gid(void)
239 unsigned int guestctl1;
241 if (cpu_has_guestid) {
242 back_to_back_c0_hazard();
243 guestctl1 = read_c0_guestctl1();
244 guestctl1 = (guestctl1 & ~MIPS_GCTL1_RID) |
245 ((guestctl1 & MIPS_GCTL1_ID) >> MIPS_GCTL1_ID_SHIFT)
246 << MIPS_GCTL1_RID_SHIFT;
247 write_c0_guestctl1(guestctl1);
252 int kvm_vz_host_tlb_inv(struct kvm_vcpu *vcpu, unsigned long va)
255 unsigned long flags, old_entryhi;
257 local_irq_save(flags);
260 /* Set root GuestID for root probe and write of guest TLB entry */
261 set_root_gid_to_guest_gid();
263 old_entryhi = read_c0_entryhi();
265 idx = _kvm_mips_host_tlb_inv((va & VPN2_MASK) |
266 kvm_mips_get_root_asid(vcpu));
268 write_c0_entryhi(old_entryhi);
273 local_irq_restore(flags);
276 * We don't want to get reserved instruction exceptions for missing tlb
279 if (cpu_has_vtag_icache)
283 kvm_debug("%s: Invalidated root entryhi %#lx @ idx %d\n",
284 __func__, (va & VPN2_MASK) |
285 kvm_mips_get_root_asid(vcpu), idx);
289 EXPORT_SYMBOL_GPL(kvm_vz_host_tlb_inv);
292 * kvm_vz_guest_tlb_lookup() - Lookup a guest VZ TLB mapping.
293 * @vcpu: KVM VCPU pointer.
294 * @gpa: Guest virtual address in a TLB mapped guest segment.
295 * @gpa: Ponter to output guest physical address it maps to.
297 * Converts a guest virtual address in a guest TLB mapped segment to a guest
298 * physical address, by probing the guest TLB.
300 * Returns: 0 if guest TLB mapping exists for @gva. *@gpa will have been
302 * -EFAULT if no guest TLB mapping exists for @gva. *@gpa may not
305 int kvm_vz_guest_tlb_lookup(struct kvm_vcpu *vcpu, unsigned long gva,
308 unsigned long o_entryhi, o_entrylo[2], o_pagemask;
309 unsigned int o_index;
310 unsigned long entrylo[2], pagemask, pagemaskbit, pa;
314 /* Probe the guest TLB for a mapping */
315 local_irq_save(flags);
316 /* Set root GuestID for root probe of guest TLB entry */
318 set_root_gid_to_guest_gid();
320 o_entryhi = read_gc0_entryhi();
321 o_index = read_gc0_index();
323 write_gc0_entryhi((o_entryhi & 0x3ff) | (gva & ~0xfffl));
328 index = read_gc0_index();
331 write_gc0_entryhi(o_entryhi);
332 write_gc0_index(o_index);
336 local_irq_restore(flags);
340 /* Match! read the TLB entry */
341 o_entrylo[0] = read_gc0_entrylo0();
342 o_entrylo[1] = read_gc0_entrylo1();
343 o_pagemask = read_gc0_pagemask();
349 entrylo[0] = read_gc0_entrylo0();
350 entrylo[1] = read_gc0_entrylo1();
351 pagemask = ~read_gc0_pagemask() & ~0x1fffl;
353 write_gc0_entryhi(o_entryhi);
354 write_gc0_index(o_index);
355 write_gc0_entrylo0(o_entrylo[0]);
356 write_gc0_entrylo1(o_entrylo[1]);
357 write_gc0_pagemask(o_pagemask);
361 local_irq_restore(flags);
363 /* Select one of the EntryLo values and interpret the GPA */
364 pagemaskbit = (pagemask ^ (pagemask & (pagemask - 1))) >> 1;
365 pa = entrylo[!!(gva & pagemaskbit)];
368 * TLB entry may have become invalid since TLB probe if physical FTLB
369 * entries are shared between threads (e.g. I6400).
371 if (!(pa & ENTRYLO_V))
375 * Note, this doesn't take guest MIPS32 XPA into account, where PFN is
376 * split with XI/RI in the middle.
378 pa = (pa << 6) & ~0xfffl;
379 pa |= gva & ~(pagemask | pagemaskbit);
384 EXPORT_SYMBOL_GPL(kvm_vz_guest_tlb_lookup);
387 * kvm_vz_local_flush_roottlb_all_guests() - Flush all root TLB entries for
390 * Invalidate all entries in root tlb which are GPA mappings.
392 void kvm_vz_local_flush_roottlb_all_guests(void)
395 unsigned long old_entryhi, old_pagemask, old_guestctl1;
398 if (WARN_ON(!cpu_has_guestid))
401 local_irq_save(flags);
404 /* TLBR may clobber EntryHi.ASID, PageMask, and GuestCtl1.RID */
405 old_entryhi = read_c0_entryhi();
406 old_pagemask = read_c0_pagemask();
407 old_guestctl1 = read_c0_guestctl1();
410 * Invalidate guest entries in root TLB while leaving root entries
411 * intact when possible.
413 for (entry = 0; entry < current_cpu_data.tlbsize; entry++) {
414 write_c0_index(entry);
419 /* Don't invalidate non-guest (RVA) mappings in the root TLB */
420 if (!(read_c0_guestctl1() & MIPS_GCTL1_RID))
423 /* Make sure all entries differ. */
424 write_c0_entryhi(UNIQUE_ENTRYHI(entry));
425 write_c0_entrylo0(0);
426 write_c0_entrylo1(0);
427 write_c0_guestctl1(0);
432 write_c0_entryhi(old_entryhi);
433 write_c0_pagemask(old_pagemask);
434 write_c0_guestctl1(old_guestctl1);
438 local_irq_restore(flags);
440 EXPORT_SYMBOL_GPL(kvm_vz_local_flush_roottlb_all_guests);
443 * kvm_vz_local_flush_guesttlb_all() - Flush all guest TLB entries.
445 * Invalidate all entries in guest tlb irrespective of guestid.
447 void kvm_vz_local_flush_guesttlb_all(void)
450 unsigned long old_index;
451 unsigned long old_entryhi;
452 unsigned long old_entrylo[2];
453 unsigned long old_pagemask;
457 local_irq_save(flags);
459 /* Preserve all clobbered guest registers */
460 old_index = read_gc0_index();
461 old_entryhi = read_gc0_entryhi();
462 old_entrylo[0] = read_gc0_entrylo0();
463 old_entrylo[1] = read_gc0_entrylo1();
464 old_pagemask = read_gc0_pagemask();
466 switch (current_cpu_type()) {
467 case CPU_CAVIUM_OCTEON3:
468 /* Inhibit machine check due to multiple matching TLB entries */
469 cvmmemctl2 = read_c0_cvmmemctl2();
470 cvmmemctl2 |= CVMMEMCTL2_INHIBITTS;
471 write_c0_cvmmemctl2(cvmmemctl2);
475 /* Invalidate guest entries in guest TLB */
476 write_gc0_entrylo0(0);
477 write_gc0_entrylo1(0);
478 write_gc0_pagemask(0);
479 for (entry = 0; entry < current_cpu_data.guest.tlbsize; entry++) {
480 /* Make sure all entries differ. */
481 write_gc0_index(entry);
482 write_gc0_entryhi(UNIQUE_GUEST_ENTRYHI(entry));
484 guest_tlb_write_indexed();
488 cvmmemctl2 &= ~CVMMEMCTL2_INHIBITTS;
489 write_c0_cvmmemctl2(cvmmemctl2);
492 write_gc0_index(old_index);
493 write_gc0_entryhi(old_entryhi);
494 write_gc0_entrylo0(old_entrylo[0]);
495 write_gc0_entrylo1(old_entrylo[1]);
496 write_gc0_pagemask(old_pagemask);
499 local_irq_restore(flags);
501 EXPORT_SYMBOL_GPL(kvm_vz_local_flush_guesttlb_all);
504 * kvm_vz_save_guesttlb() - Save a range of guest TLB entries.
505 * @buf: Buffer to write TLB entries into.
506 * @index: Start index.
507 * @count: Number of entries to save.
509 * Save a range of guest TLB entries. The caller must ensure interrupts are
512 void kvm_vz_save_guesttlb(struct kvm_mips_tlb *buf, unsigned int index,
515 unsigned int end = index + count;
516 unsigned long old_entryhi, old_entrylo0, old_entrylo1, old_pagemask;
517 unsigned int guestctl1 = 0;
520 /* Save registers we're about to clobber */
521 old_index = read_gc0_index();
522 old_entryhi = read_gc0_entryhi();
523 old_entrylo0 = read_gc0_entrylo0();
524 old_entrylo1 = read_gc0_entrylo1();
525 old_pagemask = read_gc0_pagemask();
527 /* Set root GuestID for root probe */
529 set_root_gid_to_guest_gid();
531 guestctl1 = read_c0_guestctl1();
533 /* Read each entry from guest TLB */
534 for (i = index; i < end; ++i, ++buf) {
541 if (cpu_has_guestid &&
542 (read_c0_guestctl1() ^ guestctl1) & MIPS_GCTL1_RID) {
543 /* Entry invalid or belongs to another guest */
544 buf->tlb_hi = UNIQUE_GUEST_ENTRYHI(i);
549 /* Entry belongs to the right guest */
550 buf->tlb_hi = read_gc0_entryhi();
551 buf->tlb_lo[0] = read_gc0_entrylo0();
552 buf->tlb_lo[1] = read_gc0_entrylo1();
553 buf->tlb_mask = read_gc0_pagemask();
557 /* Clear root GuestID again */
561 /* Restore clobbered registers */
562 write_gc0_index(old_index);
563 write_gc0_entryhi(old_entryhi);
564 write_gc0_entrylo0(old_entrylo0);
565 write_gc0_entrylo1(old_entrylo1);
566 write_gc0_pagemask(old_pagemask);
570 EXPORT_SYMBOL_GPL(kvm_vz_save_guesttlb);
573 * kvm_vz_load_guesttlb() - Save a range of guest TLB entries.
574 * @buf: Buffer to read TLB entries from.
575 * @index: Start index.
576 * @count: Number of entries to load.
578 * Load a range of guest TLB entries. The caller must ensure interrupts are
581 void kvm_vz_load_guesttlb(const struct kvm_mips_tlb *buf, unsigned int index,
584 unsigned int end = index + count;
585 unsigned long old_entryhi, old_entrylo0, old_entrylo1, old_pagemask;
588 /* Save registers we're about to clobber */
589 old_index = read_gc0_index();
590 old_entryhi = read_gc0_entryhi();
591 old_entrylo0 = read_gc0_entrylo0();
592 old_entrylo1 = read_gc0_entrylo1();
593 old_pagemask = read_gc0_pagemask();
595 /* Set root GuestID for root probe */
597 set_root_gid_to_guest_gid();
599 /* Write each entry to guest TLB */
600 for (i = index; i < end; ++i, ++buf) {
602 write_gc0_entryhi(buf->tlb_hi);
603 write_gc0_entrylo0(buf->tlb_lo[0]);
604 write_gc0_entrylo1(buf->tlb_lo[1]);
605 write_gc0_pagemask(buf->tlb_mask);
608 guest_tlb_write_indexed();
611 /* Clear root GuestID again */
615 /* Restore clobbered registers */
616 write_gc0_index(old_index);
617 write_gc0_entryhi(old_entryhi);
618 write_gc0_entrylo0(old_entrylo0);
619 write_gc0_entrylo1(old_entrylo1);
620 write_gc0_pagemask(old_pagemask);
624 EXPORT_SYMBOL_GPL(kvm_vz_load_guesttlb);
626 #ifdef CONFIG_CPU_LOONGSON64
627 void kvm_loongson_clear_guest_vtlb(void)
629 int idx = read_gc0_index();
631 /* Set root GuestID for root probe and write of guest TLB entry */
632 set_root_gid_to_guest_gid();
636 write_gc0_index(idx);
639 set_c0_diag(LOONGSON_DIAG_ITLB | LOONGSON_DIAG_DTLB);
641 EXPORT_SYMBOL_GPL(kvm_loongson_clear_guest_vtlb);
643 void kvm_loongson_clear_guest_ftlb(void)
646 int idx = read_gc0_index();
648 /* Set root GuestID for root probe and write of guest TLB entry */
649 set_root_gid_to_guest_gid();
651 for (i = current_cpu_data.tlbsizevtlb;
652 i < (current_cpu_data.tlbsizevtlb +
653 current_cpu_data.tlbsizeftlbsets);
658 write_gc0_index(idx);
661 set_c0_diag(LOONGSON_DIAG_ITLB | LOONGSON_DIAG_DTLB);
663 EXPORT_SYMBOL_GPL(kvm_loongson_clear_guest_ftlb);
669 * kvm_mips_suspend_mm() - Suspend the active mm.
670 * @cpu The CPU we're running on.
672 * Suspend the active_mm, ready for a switch to a KVM guest virtual address
673 * space. This is left active for the duration of guest context, including time
674 * with interrupts enabled, so we need to be careful not to confuse e.g. cache
677 * kvm_mips_resume_mm() should be called before context switching to a different
678 * process so we don't need to worry about reference counting.
680 * This needs to be in static kernel code to avoid exporting init_mm.
682 void kvm_mips_suspend_mm(int cpu)
684 cpumask_clear_cpu(cpu, mm_cpumask(current->active_mm));
685 current->active_mm = &init_mm;
687 EXPORT_SYMBOL_GPL(kvm_mips_suspend_mm);
690 * kvm_mips_resume_mm() - Resume the current process mm.
691 * @cpu The CPU we're running on.
693 * Resume the mm of the current process, after a switch back from a KVM guest
694 * virtual address space (see kvm_mips_suspend_mm()).
696 void kvm_mips_resume_mm(int cpu)
698 cpumask_set_cpu(cpu, mm_cpumask(current->mm));
699 current->active_mm = current->mm;
701 EXPORT_SYMBOL_GPL(kvm_mips_resume_mm);