Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 1 | /* |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 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 |
| 4 | * for more details. |
| 5 | * |
| 6 | * KVM/MIPS TLB handling, this file is part of the Linux host kernel so that |
| 7 | * TLB handlers run from KSEG0 |
| 8 | * |
| 9 | * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved. |
| 10 | * Authors: Sanjay Lal <sanjayl@kymasys.com> |
| 11 | */ |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 12 | |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 13 | #include <linux/sched.h> |
| 14 | #include <linux/smp.h> |
| 15 | #include <linux/mm.h> |
| 16 | #include <linux/delay.h> |
James Hogan | 403015b | 2016-06-09 14:19:10 +0100 | [diff] [blame] | 17 | #include <linux/export.h> |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 18 | #include <linux/kvm_host.h> |
Sanjay Lal | 6d17c0d | 2013-05-18 06:54:24 -0700 | [diff] [blame] | 19 | #include <linux/srcu.h> |
| 20 | |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 21 | #include <asm/cpu.h> |
| 22 | #include <asm/bootinfo.h> |
| 23 | #include <asm/mmu_context.h> |
| 24 | #include <asm/pgtable.h> |
| 25 | #include <asm/cacheflush.h> |
James Hogan | e36059e | 2014-01-17 12:01:30 +0000 | [diff] [blame] | 26 | #include <asm/tlb.h> |
James Hogan | e922a4c | 2016-06-09 14:19:16 +0100 | [diff] [blame] | 27 | #include <asm/tlbdebug.h> |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 28 | |
| 29 | #undef CONFIG_MIPS_MT |
| 30 | #include <asm/r4kcache.h> |
| 31 | #define CONFIG_MIPS_MT |
| 32 | |
| 33 | #define KVM_GUEST_PC_TLB 0 |
| 34 | #define KVM_GUEST_SP_TLB 1 |
| 35 | |
James Hogan | 403015b | 2016-06-09 14:19:10 +0100 | [diff] [blame] | 36 | static u32 kvm_mips_get_kernel_asid(struct kvm_vcpu *vcpu) |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 37 | { |
James Hogan | c550d53 | 2016-10-11 23:14:39 +0100 | [diff] [blame] | 38 | struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm; |
Paul Burton | 4edf00a | 2016-05-06 14:36:23 +0100 | [diff] [blame] | 39 | int cpu = smp_processor_id(); |
| 40 | |
James Hogan | c550d53 | 2016-10-11 23:14:39 +0100 | [diff] [blame] | 41 | return cpu_asid(cpu, kern_mm); |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 42 | } |
| 43 | |
James Hogan | 403015b | 2016-06-09 14:19:10 +0100 | [diff] [blame] | 44 | static u32 kvm_mips_get_user_asid(struct kvm_vcpu *vcpu) |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 45 | { |
James Hogan | c550d53 | 2016-10-11 23:14:39 +0100 | [diff] [blame] | 46 | struct mm_struct *user_mm = &vcpu->arch.guest_user_mm; |
Paul Burton | 4edf00a | 2016-05-06 14:36:23 +0100 | [diff] [blame] | 47 | int cpu = smp_processor_id(); |
| 48 | |
James Hogan | c550d53 | 2016-10-11 23:14:39 +0100 | [diff] [blame] | 49 | return cpu_asid(cpu, user_mm); |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 52 | /* Structure defining an tlb entry data set. */ |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 53 | |
| 54 | void kvm_mips_dump_host_tlbs(void) |
| 55 | { |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 56 | unsigned long flags; |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 57 | |
| 58 | local_irq_save(flags); |
| 59 | |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 60 | kvm_info("HOST TLBs:\n"); |
James Hogan | e922a4c | 2016-06-09 14:19:16 +0100 | [diff] [blame] | 61 | dump_tlb_regs(); |
| 62 | pr_info("\n"); |
| 63 | dump_tlb_all(); |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 64 | |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 65 | local_irq_restore(flags); |
| 66 | } |
James Hogan | cb1b447 | 2015-12-16 23:49:30 +0000 | [diff] [blame] | 67 | EXPORT_SYMBOL_GPL(kvm_mips_dump_host_tlbs); |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 68 | |
| 69 | void kvm_mips_dump_guest_tlbs(struct kvm_vcpu *vcpu) |
| 70 | { |
| 71 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 72 | struct kvm_mips_tlb tlb; |
| 73 | int i; |
| 74 | |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 75 | kvm_info("Guest TLBs:\n"); |
| 76 | kvm_info("Guest EntryHi: %#lx\n", kvm_read_c0_guest_entryhi(cop0)); |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 77 | |
| 78 | for (i = 0; i < KVM_MIPS_GUEST_TLB_SIZE; i++) { |
| 79 | tlb = vcpu->arch.guest_tlb[i]; |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 80 | kvm_info("TLB%c%3d Hi 0x%08lx ", |
James Hogan | e6207bb | 2016-06-09 14:19:19 +0100 | [diff] [blame] | 81 | (tlb.tlb_lo[0] | tlb.tlb_lo[1]) & ENTRYLO_V |
James Hogan | 9fbfb06 | 2016-06-09 14:19:17 +0100 | [diff] [blame] | 82 | ? ' ' : '*', |
Deng-Cheng Zhu | 6ad78a5 | 2014-06-26 12:11:35 -0700 | [diff] [blame] | 83 | i, tlb.tlb_hi); |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 84 | kvm_info("Lo0=0x%09llx %c%c attr %lx ", |
James Hogan | 9fbfb06 | 2016-06-09 14:19:17 +0100 | [diff] [blame] | 85 | (u64) mips3_tlbpfn_to_paddr(tlb.tlb_lo[0]), |
James Hogan | e6207bb | 2016-06-09 14:19:19 +0100 | [diff] [blame] | 86 | (tlb.tlb_lo[0] & ENTRYLO_D) ? 'D' : ' ', |
| 87 | (tlb.tlb_lo[0] & ENTRYLO_G) ? 'G' : ' ', |
| 88 | (tlb.tlb_lo[0] & ENTRYLO_C) >> ENTRYLO_C_SHIFT); |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 89 | kvm_info("Lo1=0x%09llx %c%c attr %lx sz=%lx\n", |
James Hogan | 9fbfb06 | 2016-06-09 14:19:17 +0100 | [diff] [blame] | 90 | (u64) mips3_tlbpfn_to_paddr(tlb.tlb_lo[1]), |
James Hogan | e6207bb | 2016-06-09 14:19:19 +0100 | [diff] [blame] | 91 | (tlb.tlb_lo[1] & ENTRYLO_D) ? 'D' : ' ', |
| 92 | (tlb.tlb_lo[1] & ENTRYLO_G) ? 'G' : ' ', |
| 93 | (tlb.tlb_lo[1] & ENTRYLO_C) >> ENTRYLO_C_SHIFT, |
| 94 | tlb.tlb_mask); |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
James Hogan | cb1b447 | 2015-12-16 23:49:30 +0000 | [diff] [blame] | 97 | EXPORT_SYMBOL_GPL(kvm_mips_dump_guest_tlbs); |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 98 | |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 99 | int kvm_mips_guest_tlb_lookup(struct kvm_vcpu *vcpu, unsigned long entryhi) |
| 100 | { |
| 101 | int i; |
| 102 | int index = -1; |
| 103 | struct kvm_mips_tlb *tlb = vcpu->arch.guest_tlb; |
| 104 | |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 105 | for (i = 0; i < KVM_MIPS_GUEST_TLB_SIZE; i++) { |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 106 | if (TLB_HI_VPN2_HIT(tlb[i], entryhi) && |
| 107 | TLB_HI_ASID_HIT(tlb[i], entryhi)) { |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 108 | index = i; |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 113 | kvm_debug("%s: entryhi: %#lx, index: %d lo0: %#lx, lo1: %#lx\n", |
James Hogan | 9fbfb06 | 2016-06-09 14:19:17 +0100 | [diff] [blame] | 114 | __func__, entryhi, index, tlb[i].tlb_lo[0], tlb[i].tlb_lo[1]); |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 115 | |
| 116 | return index; |
| 117 | } |
James Hogan | cb1b447 | 2015-12-16 23:49:30 +0000 | [diff] [blame] | 118 | EXPORT_SYMBOL_GPL(kvm_mips_guest_tlb_lookup); |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 119 | |
James Hogan | 57e3869c | 2016-10-08 00:15:52 +0100 | [diff] [blame] | 120 | static int _kvm_mips_host_tlb_inv(unsigned long entryhi) |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 121 | { |
| 122 | int idx; |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 123 | |
James Hogan | 57e3869c | 2016-10-08 00:15:52 +0100 | [diff] [blame] | 124 | write_c0_entryhi(entryhi); |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 125 | mtc0_tlbw_hazard(); |
| 126 | |
| 127 | tlb_probe(); |
| 128 | tlb_probe_hazard(); |
| 129 | idx = read_c0_index(); |
| 130 | |
| 131 | if (idx >= current_cpu_data.tlbsize) |
| 132 | BUG(); |
| 133 | |
James Hogan | f3a8603 | 2016-10-07 22:01:05 +0100 | [diff] [blame] | 134 | if (idx >= 0) { |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 135 | write_c0_entryhi(UNIQUE_ENTRYHI(idx)); |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 136 | write_c0_entrylo0(0); |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 137 | write_c0_entrylo1(0); |
| 138 | mtc0_tlbw_hazard(); |
| 139 | |
| 140 | tlb_write_indexed(); |
James Hogan | 138f7ad | 2016-06-09 14:19:15 +0100 | [diff] [blame] | 141 | tlbw_use_hazard(); |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 142 | } |
| 143 | |
James Hogan | 57e3869c | 2016-10-08 00:15:52 +0100 | [diff] [blame] | 144 | return idx; |
| 145 | } |
| 146 | |
| 147 | int kvm_mips_host_tlb_inv(struct kvm_vcpu *vcpu, unsigned long va, |
| 148 | bool user, bool kernel) |
| 149 | { |
| 150 | int idx_user, idx_kernel; |
| 151 | unsigned long flags, old_entryhi; |
| 152 | |
| 153 | local_irq_save(flags); |
| 154 | |
| 155 | old_entryhi = read_c0_entryhi(); |
| 156 | |
| 157 | if (user) |
| 158 | idx_user = _kvm_mips_host_tlb_inv((va & VPN2_MASK) | |
| 159 | kvm_mips_get_user_asid(vcpu)); |
| 160 | if (kernel) |
| 161 | idx_kernel = _kvm_mips_host_tlb_inv((va & VPN2_MASK) | |
| 162 | kvm_mips_get_kernel_asid(vcpu)); |
| 163 | |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 164 | write_c0_entryhi(old_entryhi); |
| 165 | mtc0_tlbw_hazard(); |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 166 | |
| 167 | local_irq_restore(flags); |
| 168 | |
James Hogan | 57e3869c | 2016-10-08 00:15:52 +0100 | [diff] [blame] | 169 | if (user && idx_user >= 0) |
| 170 | kvm_debug("%s: Invalidated guest user entryhi %#lx @ idx %d\n", |
| 171 | __func__, (va & VPN2_MASK) | |
| 172 | kvm_mips_get_user_asid(vcpu), idx_user); |
| 173 | if (kernel && idx_kernel >= 0) |
| 174 | kvm_debug("%s: Invalidated guest kernel entryhi %#lx @ idx %d\n", |
| 175 | __func__, (va & VPN2_MASK) | |
| 176 | kvm_mips_get_kernel_asid(vcpu), idx_kernel); |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 177 | |
| 178 | return 0; |
| 179 | } |
James Hogan | cb1b447 | 2015-12-16 23:49:30 +0000 | [diff] [blame] | 180 | EXPORT_SYMBOL_GPL(kvm_mips_host_tlb_inv); |
Sanjay Lal | 858dd5d | 2012-11-21 18:34:05 -0800 | [diff] [blame] | 181 | |
James Hogan | a7ebb2e | 2016-11-15 00:06:05 +0000 | [diff] [blame] | 182 | /** |
| 183 | * kvm_mips_suspend_mm() - Suspend the active mm. |
| 184 | * @cpu The CPU we're running on. |
| 185 | * |
| 186 | * Suspend the active_mm, ready for a switch to a KVM guest virtual address |
| 187 | * space. This is left active for the duration of guest context, including time |
| 188 | * with interrupts enabled, so we need to be careful not to confuse e.g. cache |
| 189 | * management IPIs. |
| 190 | * |
| 191 | * kvm_mips_resume_mm() should be called before context switching to a different |
| 192 | * process so we don't need to worry about reference counting. |
| 193 | * |
| 194 | * This needs to be in static kernel code to avoid exporting init_mm. |
| 195 | */ |
| 196 | void kvm_mips_suspend_mm(int cpu) |
| 197 | { |
| 198 | cpumask_clear_cpu(cpu, mm_cpumask(current->active_mm)); |
| 199 | current->active_mm = &init_mm; |
| 200 | } |
| 201 | EXPORT_SYMBOL_GPL(kvm_mips_suspend_mm); |
| 202 | |
| 203 | /** |
| 204 | * kvm_mips_resume_mm() - Resume the current process mm. |
| 205 | * @cpu The CPU we're running on. |
| 206 | * |
| 207 | * Resume the mm of the current process, after a switch back from a KVM guest |
| 208 | * virtual address space (see kvm_mips_suspend_mm()). |
| 209 | */ |
| 210 | void kvm_mips_resume_mm(int cpu) |
| 211 | { |
| 212 | cpumask_set_cpu(cpu, mm_cpumask(current->mm)); |
| 213 | current->active_mm = current->mm; |
| 214 | } |
| 215 | EXPORT_SYMBOL_GPL(kvm_mips_resume_mm); |