blob: b9c52c1d35d6a37f181d843819d835b341f5958b [file] [log] [blame]
Sanjay Lal858dd5d2012-11-21 18:34:05 -08001/*
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002 * 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 Lal858dd5d2012-11-21 18:34:05 -080012
Sanjay Lal858dd5d2012-11-21 18:34:05 -080013#include <linux/sched.h>
14#include <linux/smp.h>
15#include <linux/mm.h>
16#include <linux/delay.h>
17#include <linux/module.h>
18#include <linux/kvm_host.h>
Sanjay Lal6d17c0d2013-05-18 06:54:24 -070019#include <linux/srcu.h>
20
Sanjay Lal858dd5d2012-11-21 18:34:05 -080021#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 Hogane36059e2014-01-17 12:01:30 +000026#include <asm/tlb.h>
Sanjay Lal858dd5d2012-11-21 18:34:05 -080027
28#undef CONFIG_MIPS_MT
29#include <asm/r4kcache.h>
30#define CONFIG_MIPS_MT
31
32#define KVM_GUEST_PC_TLB 0
33#define KVM_GUEST_SP_TLB 1
34
35#define PRIx64 "llx"
36
Sanjay Lal858dd5d2012-11-21 18:34:05 -080037atomic_t kvm_mips_instance;
James Hogancb1b4472015-12-16 23:49:30 +000038EXPORT_SYMBOL_GPL(kvm_mips_instance);
Sanjay Lal858dd5d2012-11-21 18:34:05 -080039
40/* These function pointers are initialized once the KVM module is loaded */
Dan Williamsba049e92016-01-15 16:56:11 -080041kvm_pfn_t (*kvm_mips_gfn_to_pfn)(struct kvm *kvm, gfn_t gfn);
James Hogancb1b4472015-12-16 23:49:30 +000042EXPORT_SYMBOL_GPL(kvm_mips_gfn_to_pfn);
Sanjay Lal858dd5d2012-11-21 18:34:05 -080043
Dan Williamsba049e92016-01-15 16:56:11 -080044void (*kvm_mips_release_pfn_clean)(kvm_pfn_t pfn);
James Hogancb1b4472015-12-16 23:49:30 +000045EXPORT_SYMBOL_GPL(kvm_mips_release_pfn_clean);
Sanjay Lal858dd5d2012-11-21 18:34:05 -080046
Dan Williamsba049e92016-01-15 16:56:11 -080047bool (*kvm_mips_is_error_pfn)(kvm_pfn_t pfn);
James Hogancb1b4472015-12-16 23:49:30 +000048EXPORT_SYMBOL_GPL(kvm_mips_is_error_pfn);
Sanjay Lal858dd5d2012-11-21 18:34:05 -080049
50uint32_t kvm_mips_get_kernel_asid(struct kvm_vcpu *vcpu)
51{
Paul Burton4edf00a2016-05-06 14:36:23 +010052 int cpu = smp_processor_id();
53
54 return vcpu->arch.guest_kernel_asid[cpu] &
55 cpu_asid_mask(&cpu_data[cpu]);
Sanjay Lal858dd5d2012-11-21 18:34:05 -080056}
57
Sanjay Lal858dd5d2012-11-21 18:34:05 -080058uint32_t kvm_mips_get_user_asid(struct kvm_vcpu *vcpu)
59{
Paul Burton4edf00a2016-05-06 14:36:23 +010060 int cpu = smp_processor_id();
61
62 return vcpu->arch.guest_user_asid[cpu] &
63 cpu_asid_mask(&cpu_data[cpu]);
Sanjay Lal858dd5d2012-11-21 18:34:05 -080064}
65
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070066inline uint32_t kvm_mips_get_commpage_asid(struct kvm_vcpu *vcpu)
Sanjay Lal858dd5d2012-11-21 18:34:05 -080067{
68 return vcpu->kvm->arch.commpage_tlb;
69}
70
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070071/* Structure defining an tlb entry data set. */
Sanjay Lal858dd5d2012-11-21 18:34:05 -080072
73void kvm_mips_dump_host_tlbs(void)
74{
75 unsigned long old_entryhi;
76 unsigned long old_pagemask;
77 struct kvm_mips_tlb tlb;
78 unsigned long flags;
79 int i;
80
81 local_irq_save(flags);
82
83 old_entryhi = read_c0_entryhi();
84 old_pagemask = read_c0_pagemask();
85
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -070086 kvm_info("HOST TLBs:\n");
Paul Burton4edf00a2016-05-06 14:36:23 +010087 kvm_info("ASID: %#lx\n", read_c0_entryhi() &
88 cpu_asid_mask(&current_cpu_data));
Sanjay Lal858dd5d2012-11-21 18:34:05 -080089
90 for (i = 0; i < current_cpu_data.tlbsize; i++) {
91 write_c0_index(i);
92 mtc0_tlbw_hazard();
93
94 tlb_read();
95 tlbw_use_hazard();
96
97 tlb.tlb_hi = read_c0_entryhi();
98 tlb.tlb_lo0 = read_c0_entrylo0();
99 tlb.tlb_lo1 = read_c0_entrylo1();
100 tlb.tlb_mask = read_c0_pagemask();
101
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700102 kvm_info("TLB%c%3d Hi 0x%08lx ",
103 (tlb.tlb_lo0 | tlb.tlb_lo1) & MIPS3_PG_V ? ' ' : '*',
104 i, tlb.tlb_hi);
105 kvm_info("Lo0=0x%09" PRIx64 " %c%c attr %lx ",
106 (uint64_t) mips3_tlbpfn_to_paddr(tlb.tlb_lo0),
107 (tlb.tlb_lo0 & MIPS3_PG_D) ? 'D' : ' ',
108 (tlb.tlb_lo0 & MIPS3_PG_G) ? 'G' : ' ',
109 (tlb.tlb_lo0 >> 3) & 7);
110 kvm_info("Lo1=0x%09" PRIx64 " %c%c attr %lx sz=%lx\n",
111 (uint64_t) mips3_tlbpfn_to_paddr(tlb.tlb_lo1),
112 (tlb.tlb_lo1 & MIPS3_PG_D) ? 'D' : ' ',
113 (tlb.tlb_lo1 & MIPS3_PG_G) ? 'G' : ' ',
114 (tlb.tlb_lo1 >> 3) & 7, tlb.tlb_mask);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800115 }
116 write_c0_entryhi(old_entryhi);
117 write_c0_pagemask(old_pagemask);
118 mtc0_tlbw_hazard();
119 local_irq_restore(flags);
120}
James Hogancb1b4472015-12-16 23:49:30 +0000121EXPORT_SYMBOL_GPL(kvm_mips_dump_host_tlbs);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800122
123void kvm_mips_dump_guest_tlbs(struct kvm_vcpu *vcpu)
124{
125 struct mips_coproc *cop0 = vcpu->arch.cop0;
126 struct kvm_mips_tlb tlb;
127 int i;
128
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700129 kvm_info("Guest TLBs:\n");
130 kvm_info("Guest EntryHi: %#lx\n", kvm_read_c0_guest_entryhi(cop0));
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800131
132 for (i = 0; i < KVM_MIPS_GUEST_TLB_SIZE; i++) {
133 tlb = vcpu->arch.guest_tlb[i];
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700134 kvm_info("TLB%c%3d Hi 0x%08lx ",
135 (tlb.tlb_lo0 | tlb.tlb_lo1) & MIPS3_PG_V ? ' ' : '*',
136 i, tlb.tlb_hi);
137 kvm_info("Lo0=0x%09" PRIx64 " %c%c attr %lx ",
138 (uint64_t) mips3_tlbpfn_to_paddr(tlb.tlb_lo0),
139 (tlb.tlb_lo0 & MIPS3_PG_D) ? 'D' : ' ',
140 (tlb.tlb_lo0 & MIPS3_PG_G) ? 'G' : ' ',
141 (tlb.tlb_lo0 >> 3) & 7);
142 kvm_info("Lo1=0x%09" PRIx64 " %c%c attr %lx sz=%lx\n",
143 (uint64_t) mips3_tlbpfn_to_paddr(tlb.tlb_lo1),
144 (tlb.tlb_lo1 & MIPS3_PG_D) ? 'D' : ' ',
145 (tlb.tlb_lo1 & MIPS3_PG_G) ? 'G' : ' ',
146 (tlb.tlb_lo1 >> 3) & 7, tlb.tlb_mask);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800147 }
148}
James Hogancb1b4472015-12-16 23:49:30 +0000149EXPORT_SYMBOL_GPL(kvm_mips_dump_guest_tlbs);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800150
Sanjay Lal6d17c0d2013-05-18 06:54:24 -0700151static int kvm_mips_map_page(struct kvm *kvm, gfn_t gfn)
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800152{
Sanjay Lal6d17c0d2013-05-18 06:54:24 -0700153 int srcu_idx, err = 0;
Dan Williamsba049e92016-01-15 16:56:11 -0800154 kvm_pfn_t pfn;
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800155
156 if (kvm->arch.guest_pmap[gfn] != KVM_INVALID_PAGE)
Sanjay Lal6d17c0d2013-05-18 06:54:24 -0700157 return 0;
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800158
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700159 srcu_idx = srcu_read_lock(&kvm->srcu);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800160 pfn = kvm_mips_gfn_to_pfn(kvm, gfn);
161
162 if (kvm_mips_is_error_pfn(pfn)) {
Sanjay Lal6d17c0d2013-05-18 06:54:24 -0700163 kvm_err("Couldn't get pfn for gfn %#" PRIx64 "!\n", gfn);
164 err = -EFAULT;
165 goto out;
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800166 }
167
168 kvm->arch.guest_pmap[gfn] = pfn;
Sanjay Lal6d17c0d2013-05-18 06:54:24 -0700169out:
170 srcu_read_unlock(&kvm->srcu, srcu_idx);
171 return err;
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800172}
173
174/* Translate guest KSEG0 addresses to Host PA */
175unsigned long kvm_mips_translate_guest_kseg0_to_hpa(struct kvm_vcpu *vcpu,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700176 unsigned long gva)
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800177{
178 gfn_t gfn;
179 uint32_t offset = gva & ~PAGE_MASK;
180 struct kvm *kvm = vcpu->kvm;
181
182 if (KVM_GUEST_KSEGX(gva) != KVM_GUEST_KSEG0) {
183 kvm_err("%s/%p: Invalid gva: %#lx\n", __func__,
184 __builtin_return_address(0), gva);
185 return KVM_INVALID_PAGE;
186 }
187
188 gfn = (KVM_GUEST_CPHYSADDR(gva) >> PAGE_SHIFT);
189
190 if (gfn >= kvm->arch.guest_pmap_npages) {
191 kvm_err("%s: Invalid gfn: %#llx, GVA: %#lx\n", __func__, gfn,
192 gva);
193 return KVM_INVALID_PAGE;
194 }
Sanjay Lal6d17c0d2013-05-18 06:54:24 -0700195
196 if (kvm_mips_map_page(vcpu->kvm, gfn) < 0)
197 return KVM_INVALID_ADDR;
198
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800199 return (kvm->arch.guest_pmap[gfn] << PAGE_SHIFT) + offset;
200}
James Hogancb1b4472015-12-16 23:49:30 +0000201EXPORT_SYMBOL_GPL(kvm_mips_translate_guest_kseg0_to_hpa);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800202
203/* XXXKYMA: Must be called with interrupts disabled */
204/* set flush_dcache_mask == 0 if no dcache flush required */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700205int kvm_mips_host_tlb_write(struct kvm_vcpu *vcpu, unsigned long entryhi,
206 unsigned long entrylo0, unsigned long entrylo1,
207 int flush_dcache_mask)
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800208{
209 unsigned long flags;
210 unsigned long old_entryhi;
Deng-Cheng Zhub045c402014-06-26 12:11:37 -0700211 int idx;
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800212
213 local_irq_save(flags);
214
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800215 old_entryhi = read_c0_entryhi();
216 write_c0_entryhi(entryhi);
217 mtc0_tlbw_hazard();
218
219 tlb_probe();
220 tlb_probe_hazard();
221 idx = read_c0_index();
222
223 if (idx > current_cpu_data.tlbsize) {
224 kvm_err("%s: Invalid Index: %d\n", __func__, idx);
225 kvm_mips_dump_host_tlbs();
Tapasweni Pathakcfec0e72015-02-22 21:48:21 +0530226 local_irq_restore(flags);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800227 return -1;
228 }
229
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800230 write_c0_entrylo0(entrylo0);
231 write_c0_entrylo1(entrylo1);
232 mtc0_tlbw_hazard();
233
James Hoganb5dfc6c2014-05-29 10:16:26 +0100234 if (idx < 0)
235 tlb_write_random();
236 else
237 tlb_write_indexed();
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800238 tlbw_use_hazard();
239
James Hogan3d654832014-05-29 10:16:41 +0100240 kvm_debug("@ %#lx idx: %2d [entryhi(R): %#lx] entrylo0(R): 0x%08lx, entrylo1(R): 0x%08lx\n",
241 vcpu->arch.pc, idx, read_c0_entryhi(),
242 read_c0_entrylo0(), read_c0_entrylo1());
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800243
244 /* Flush D-cache */
245 if (flush_dcache_mask) {
246 if (entrylo0 & MIPS3_PG_V) {
247 ++vcpu->stat.flush_dcache_exits;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700248 flush_data_cache_page((entryhi & VPN2_MASK) &
249 ~flush_dcache_mask);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800250 }
251 if (entrylo1 & MIPS3_PG_V) {
252 ++vcpu->stat.flush_dcache_exits;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700253 flush_data_cache_page(((entryhi & VPN2_MASK) &
254 ~flush_dcache_mask) |
255 (0x1 << PAGE_SHIFT));
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800256 }
257 }
258
259 /* Restore old ASID */
260 write_c0_entryhi(old_entryhi);
261 mtc0_tlbw_hazard();
262 tlbw_use_hazard();
263 local_irq_restore(flags);
264 return 0;
265}
266
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800267/* XXXKYMA: Must be called with interrupts disabled */
268int kvm_mips_handle_kseg0_tlb_fault(unsigned long badvaddr,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700269 struct kvm_vcpu *vcpu)
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800270{
271 gfn_t gfn;
Dan Williamsba049e92016-01-15 16:56:11 -0800272 kvm_pfn_t pfn0, pfn1;
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800273 unsigned long vaddr = 0;
274 unsigned long entryhi = 0, entrylo0 = 0, entrylo1 = 0;
275 int even;
276 struct kvm *kvm = vcpu->kvm;
277 const int flush_dcache_mask = 0;
278
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800279 if (KVM_GUEST_KSEGX(badvaddr) != KVM_GUEST_KSEG0) {
280 kvm_err("%s: Invalid BadVaddr: %#lx\n", __func__, badvaddr);
281 kvm_mips_dump_host_tlbs();
282 return -1;
283 }
284
285 gfn = (KVM_GUEST_CPHYSADDR(badvaddr) >> PAGE_SHIFT);
286 if (gfn >= kvm->arch.guest_pmap_npages) {
287 kvm_err("%s: Invalid gfn: %#llx, BadVaddr: %#lx\n", __func__,
288 gfn, badvaddr);
289 kvm_mips_dump_host_tlbs();
290 return -1;
291 }
292 even = !(gfn & 0x1);
293 vaddr = badvaddr & (PAGE_MASK << 1);
294
Sanjay Lal6d17c0d2013-05-18 06:54:24 -0700295 if (kvm_mips_map_page(vcpu->kvm, gfn) < 0)
296 return -1;
297
298 if (kvm_mips_map_page(vcpu->kvm, gfn ^ 0x1) < 0)
299 return -1;
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800300
301 if (even) {
302 pfn0 = kvm->arch.guest_pmap[gfn];
303 pfn1 = kvm->arch.guest_pmap[gfn ^ 0x1];
304 } else {
305 pfn0 = kvm->arch.guest_pmap[gfn ^ 0x1];
306 pfn1 = kvm->arch.guest_pmap[gfn];
307 }
308
309 entryhi = (vaddr | kvm_mips_get_kernel_asid(vcpu));
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700310 entrylo0 = mips3_paddr_to_tlbpfn(pfn0 << PAGE_SHIFT) | (0x3 << 3) |
311 (1 << 2) | (0x1 << 1);
312 entrylo1 = mips3_paddr_to_tlbpfn(pfn1 << PAGE_SHIFT) | (0x3 << 3) |
313 (1 << 2) | (0x1 << 1);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800314
315 return kvm_mips_host_tlb_write(vcpu, entryhi, entrylo0, entrylo1,
316 flush_dcache_mask);
317}
James Hogancb1b4472015-12-16 23:49:30 +0000318EXPORT_SYMBOL_GPL(kvm_mips_handle_kseg0_tlb_fault);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800319
320int kvm_mips_handle_commpage_tlb_fault(unsigned long badvaddr,
321 struct kvm_vcpu *vcpu)
322{
Dan Williamsba049e92016-01-15 16:56:11 -0800323 kvm_pfn_t pfn0, pfn1;
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800324 unsigned long flags, old_entryhi = 0, vaddr = 0;
325 unsigned long entrylo0 = 0, entrylo1 = 0;
326
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800327 pfn0 = CPHYSADDR(vcpu->arch.kseg0_commpage) >> PAGE_SHIFT;
328 pfn1 = 0;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700329 entrylo0 = mips3_paddr_to_tlbpfn(pfn0 << PAGE_SHIFT) | (0x3 << 3) |
330 (1 << 2) | (0x1 << 1);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800331 entrylo1 = 0;
332
333 local_irq_save(flags);
334
335 old_entryhi = read_c0_entryhi();
336 vaddr = badvaddr & (PAGE_MASK << 1);
337 write_c0_entryhi(vaddr | kvm_mips_get_kernel_asid(vcpu));
338 mtc0_tlbw_hazard();
339 write_c0_entrylo0(entrylo0);
340 mtc0_tlbw_hazard();
341 write_c0_entrylo1(entrylo1);
342 mtc0_tlbw_hazard();
343 write_c0_index(kvm_mips_get_commpage_asid(vcpu));
344 mtc0_tlbw_hazard();
345 tlb_write_indexed();
346 mtc0_tlbw_hazard();
347 tlbw_use_hazard();
348
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700349 kvm_debug("@ %#lx idx: %2d [entryhi(R): %#lx] entrylo0 (R): 0x%08lx, entrylo1(R): 0x%08lx\n",
350 vcpu->arch.pc, read_c0_index(), read_c0_entryhi(),
351 read_c0_entrylo0(), read_c0_entrylo1());
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800352
353 /* Restore old ASID */
354 write_c0_entryhi(old_entryhi);
355 mtc0_tlbw_hazard();
356 tlbw_use_hazard();
357 local_irq_restore(flags);
358
359 return 0;
360}
James Hogancb1b4472015-12-16 23:49:30 +0000361EXPORT_SYMBOL_GPL(kvm_mips_handle_commpage_tlb_fault);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800362
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700363int kvm_mips_handle_mapped_seg_tlb_fault(struct kvm_vcpu *vcpu,
364 struct kvm_mips_tlb *tlb,
365 unsigned long *hpa0,
366 unsigned long *hpa1)
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800367{
368 unsigned long entryhi = 0, entrylo0 = 0, entrylo1 = 0;
369 struct kvm *kvm = vcpu->kvm;
Dan Williamsba049e92016-01-15 16:56:11 -0800370 kvm_pfn_t pfn0, pfn1;
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800371
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800372 if ((tlb->tlb_hi & VPN2_MASK) == 0) {
373 pfn0 = 0;
374 pfn1 = 0;
375 } else {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700376 if (kvm_mips_map_page(kvm, mips3_tlbpfn_to_paddr(tlb->tlb_lo0)
377 >> PAGE_SHIFT) < 0)
Sanjay Lal6d17c0d2013-05-18 06:54:24 -0700378 return -1;
379
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700380 if (kvm_mips_map_page(kvm, mips3_tlbpfn_to_paddr(tlb->tlb_lo1)
381 >> PAGE_SHIFT) < 0)
Sanjay Lal6d17c0d2013-05-18 06:54:24 -0700382 return -1;
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800383
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700384 pfn0 = kvm->arch.guest_pmap[mips3_tlbpfn_to_paddr(tlb->tlb_lo0)
385 >> PAGE_SHIFT];
386 pfn1 = kvm->arch.guest_pmap[mips3_tlbpfn_to_paddr(tlb->tlb_lo1)
387 >> PAGE_SHIFT];
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800388 }
389
390 if (hpa0)
391 *hpa0 = pfn0 << PAGE_SHIFT;
392
393 if (hpa1)
394 *hpa1 = pfn1 << PAGE_SHIFT;
395
396 /* Get attributes from the Guest TLB */
397 entryhi = (tlb->tlb_hi & VPN2_MASK) | (KVM_GUEST_KERNEL_MODE(vcpu) ?
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700398 kvm_mips_get_kernel_asid(vcpu) :
399 kvm_mips_get_user_asid(vcpu));
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800400 entrylo0 = mips3_paddr_to_tlbpfn(pfn0 << PAGE_SHIFT) | (0x3 << 3) |
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700401 (tlb->tlb_lo0 & MIPS3_PG_D) | (tlb->tlb_lo0 & MIPS3_PG_V);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800402 entrylo1 = mips3_paddr_to_tlbpfn(pfn1 << PAGE_SHIFT) | (0x3 << 3) |
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700403 (tlb->tlb_lo1 & MIPS3_PG_D) | (tlb->tlb_lo1 & MIPS3_PG_V);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800404
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800405 kvm_debug("@ %#lx tlb_lo0: 0x%08lx tlb_lo1: 0x%08lx\n", vcpu->arch.pc,
406 tlb->tlb_lo0, tlb->tlb_lo1);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800407
408 return kvm_mips_host_tlb_write(vcpu, entryhi, entrylo0, entrylo1,
409 tlb->tlb_mask);
410}
James Hogancb1b4472015-12-16 23:49:30 +0000411EXPORT_SYMBOL_GPL(kvm_mips_handle_mapped_seg_tlb_fault);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800412
413int kvm_mips_guest_tlb_lookup(struct kvm_vcpu *vcpu, unsigned long entryhi)
414{
415 int i;
416 int index = -1;
417 struct kvm_mips_tlb *tlb = vcpu->arch.guest_tlb;
418
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800419 for (i = 0; i < KVM_MIPS_GUEST_TLB_SIZE; i++) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700420 if (TLB_HI_VPN2_HIT(tlb[i], entryhi) &&
421 TLB_HI_ASID_HIT(tlb[i], entryhi)) {
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800422 index = i;
423 break;
424 }
425 }
426
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800427 kvm_debug("%s: entryhi: %#lx, index: %d lo0: %#lx, lo1: %#lx\n",
428 __func__, entryhi, index, tlb[i].tlb_lo0, tlb[i].tlb_lo1);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800429
430 return index;
431}
James Hogancb1b4472015-12-16 23:49:30 +0000432EXPORT_SYMBOL_GPL(kvm_mips_guest_tlb_lookup);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800433
434int kvm_mips_host_tlb_lookup(struct kvm_vcpu *vcpu, unsigned long vaddr)
435{
436 unsigned long old_entryhi, flags;
Deng-Cheng Zhub045c402014-06-26 12:11:37 -0700437 int idx;
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800438
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800439 local_irq_save(flags);
440
441 old_entryhi = read_c0_entryhi();
442
443 if (KVM_GUEST_KERNEL_MODE(vcpu))
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700444 write_c0_entryhi((vaddr & VPN2_MASK) |
445 kvm_mips_get_kernel_asid(vcpu));
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800446 else {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700447 write_c0_entryhi((vaddr & VPN2_MASK) |
448 kvm_mips_get_user_asid(vcpu));
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800449 }
450
451 mtc0_tlbw_hazard();
452
453 tlb_probe();
454 tlb_probe_hazard();
455 idx = read_c0_index();
456
457 /* Restore old ASID */
458 write_c0_entryhi(old_entryhi);
459 mtc0_tlbw_hazard();
460 tlbw_use_hazard();
461
462 local_irq_restore(flags);
463
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800464 kvm_debug("Host TLB lookup, %#lx, idx: %2d\n", vaddr, idx);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800465
466 return idx;
467}
James Hogancb1b4472015-12-16 23:49:30 +0000468EXPORT_SYMBOL_GPL(kvm_mips_host_tlb_lookup);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800469
470int kvm_mips_host_tlb_inv(struct kvm_vcpu *vcpu, unsigned long va)
471{
472 int idx;
473 unsigned long flags, old_entryhi;
474
475 local_irq_save(flags);
476
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800477 old_entryhi = read_c0_entryhi();
478
479 write_c0_entryhi((va & VPN2_MASK) | kvm_mips_get_user_asid(vcpu));
480 mtc0_tlbw_hazard();
481
482 tlb_probe();
483 tlb_probe_hazard();
484 idx = read_c0_index();
485
486 if (idx >= current_cpu_data.tlbsize)
487 BUG();
488
489 if (idx > 0) {
490 write_c0_entryhi(UNIQUE_ENTRYHI(idx));
491 mtc0_tlbw_hazard();
492
493 write_c0_entrylo0(0);
494 mtc0_tlbw_hazard();
495
496 write_c0_entrylo1(0);
497 mtc0_tlbw_hazard();
498
499 tlb_write_indexed();
500 mtc0_tlbw_hazard();
501 }
502
503 write_c0_entryhi(old_entryhi);
504 mtc0_tlbw_hazard();
505 tlbw_use_hazard();
506
507 local_irq_restore(flags);
508
James Hogan3d654832014-05-29 10:16:41 +0100509 if (idx > 0)
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800510 kvm_debug("%s: Invalidated entryhi %#lx @ idx %d\n", __func__,
James Hogan3d654832014-05-29 10:16:41 +0100511 (va & VPN2_MASK) | kvm_mips_get_user_asid(vcpu), idx);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800512
513 return 0;
514}
James Hogancb1b4472015-12-16 23:49:30 +0000515EXPORT_SYMBOL_GPL(kvm_mips_host_tlb_inv);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800516
517void kvm_mips_flush_host_tlb(int skip_kseg0)
518{
519 unsigned long flags;
520 unsigned long old_entryhi, entryhi;
521 unsigned long old_pagemask;
522 int entry = 0;
523 int maxentry = current_cpu_data.tlbsize;
524
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800525 local_irq_save(flags);
526
527 old_entryhi = read_c0_entryhi();
528 old_pagemask = read_c0_pagemask();
529
530 /* Blast 'em all away. */
531 for (entry = 0; entry < maxentry; entry++) {
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800532 write_c0_index(entry);
533 mtc0_tlbw_hazard();
534
535 if (skip_kseg0) {
536 tlb_read();
537 tlbw_use_hazard();
538
539 entryhi = read_c0_entryhi();
540
541 /* Don't blow away guest kernel entries */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700542 if (KVM_GUEST_KSEGX(entryhi) == KVM_GUEST_KSEG0)
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800543 continue;
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800544 }
545
546 /* Make sure all entries differ. */
547 write_c0_entryhi(UNIQUE_ENTRYHI(entry));
548 mtc0_tlbw_hazard();
549 write_c0_entrylo0(0);
550 mtc0_tlbw_hazard();
551 write_c0_entrylo1(0);
552 mtc0_tlbw_hazard();
553
554 tlb_write_indexed();
555 mtc0_tlbw_hazard();
556 }
557
558 tlbw_use_hazard();
559
560 write_c0_entryhi(old_entryhi);
561 write_c0_pagemask(old_pagemask);
562 mtc0_tlbw_hazard();
563 tlbw_use_hazard();
564
565 local_irq_restore(flags);
566}
James Hogancb1b4472015-12-16 23:49:30 +0000567EXPORT_SYMBOL_GPL(kvm_mips_flush_host_tlb);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800568
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700569void kvm_get_new_mmu_context(struct mm_struct *mm, unsigned long cpu,
570 struct kvm_vcpu *vcpu)
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800571{
572 unsigned long asid = asid_cache(cpu);
573
Paul Burton4edf00a2016-05-06 14:36:23 +0100574 asid += cpu_asid_inc();
575 if (!(asid & cpu_asid_mask(&cpu_data[cpu]))) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700576 if (cpu_has_vtag_icache)
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800577 flush_icache_all();
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800578
579 kvm_local_flush_tlb_all(); /* start new asid cycle */
580
581 if (!asid) /* fix version if needed */
Paul Burton4edf00a2016-05-06 14:36:23 +0100582 asid = asid_first_version(cpu);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800583 }
584
585 cpu_context(cpu, mm) = asid_cache(cpu) = asid;
586}
587
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800588void kvm_local_flush_tlb_all(void)
589{
590 unsigned long flags;
591 unsigned long old_ctx;
592 int entry = 0;
593
594 local_irq_save(flags);
595 /* Save old context and create impossible VPN2 value */
596 old_ctx = read_c0_entryhi();
597 write_c0_entrylo0(0);
598 write_c0_entrylo1(0);
599
600 /* Blast 'em all away. */
601 while (entry < current_cpu_data.tlbsize) {
602 /* Make sure all entries differ. */
603 write_c0_entryhi(UNIQUE_ENTRYHI(entry));
604 write_c0_index(entry);
605 mtc0_tlbw_hazard();
606 tlb_write_indexed();
607 entry++;
608 }
609 tlbw_use_hazard();
610 write_c0_entryhi(old_ctx);
611 mtc0_tlbw_hazard();
612
613 local_irq_restore(flags);
614}
James Hogancb1b4472015-12-16 23:49:30 +0000615EXPORT_SYMBOL_GPL(kvm_local_flush_tlb_all);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800616
James Hogan3a0ba772014-05-29 10:16:34 +0100617/**
618 * kvm_mips_migrate_count() - Migrate timer.
619 * @vcpu: Virtual CPU.
620 *
621 * Migrate CP0_Count hrtimer to the current CPU by cancelling and restarting it
622 * if it was running prior to being cancelled.
623 *
624 * Must be called when the VCPU is migrated to a different CPU to ensure that
625 * timer expiry during guest execution interrupts the guest and causes the
626 * interrupt to be delivered in a timely manner.
627 */
628static void kvm_mips_migrate_count(struct kvm_vcpu *vcpu)
629{
630 if (hrtimer_cancel(&vcpu->arch.comparecount_timer))
631 hrtimer_restart(&vcpu->arch.comparecount_timer);
632}
633
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800634/* Restore ASID once we are scheduled back after preemption */
635void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
636{
Paul Burton4edf00a2016-05-06 14:36:23 +0100637 unsigned long asid_mask = cpu_asid_mask(&cpu_data[cpu]);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800638 unsigned long flags;
639 int newasid = 0;
640
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800641 kvm_debug("%s: vcpu %p, cpu: %d\n", __func__, vcpu, cpu);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800642
Adam Buchbinder92a76f62016-02-25 00:44:58 -0800643 /* Allocate new kernel and user ASIDs if needed */
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800644
645 local_irq_save(flags);
646
James Hogancaa1faa2015-12-16 23:49:26 +0000647 if ((vcpu->arch.guest_kernel_asid[cpu] ^ asid_cache(cpu)) &
Paul Burton4edf00a2016-05-06 14:36:23 +0100648 asid_version_mask(cpu)) {
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800649 kvm_get_new_mmu_context(&vcpu->arch.guest_kernel_mm, cpu, vcpu);
650 vcpu->arch.guest_kernel_asid[cpu] =
651 vcpu->arch.guest_kernel_mm.context.asid[cpu];
652 kvm_get_new_mmu_context(&vcpu->arch.guest_user_mm, cpu, vcpu);
653 vcpu->arch.guest_user_asid[cpu] =
654 vcpu->arch.guest_user_mm.context.asid[cpu];
655 newasid++;
656
James Hogan6e95bfd2014-05-29 10:16:43 +0100657 kvm_debug("[%d]: cpu_context: %#lx\n", cpu,
658 cpu_context(cpu, current->mm));
659 kvm_debug("[%d]: Allocated new ASID for Guest Kernel: %#x\n",
660 cpu, vcpu->arch.guest_kernel_asid[cpu]);
661 kvm_debug("[%d]: Allocated new ASID for Guest User: %#x\n", cpu,
662 vcpu->arch.guest_user_asid[cpu]);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800663 }
664
665 if (vcpu->arch.last_sched_cpu != cpu) {
James Hogan6e95bfd2014-05-29 10:16:43 +0100666 kvm_debug("[%d->%d]KVM VCPU[%d] switch\n",
667 vcpu->arch.last_sched_cpu, cpu, vcpu->vcpu_id);
James Hogan3a0ba772014-05-29 10:16:34 +0100668 /*
669 * Migrate the timer interrupt to the current CPU so that it
670 * always interrupts the guest and synchronously triggers a
671 * guest timer interrupt.
672 */
673 kvm_mips_migrate_count(vcpu);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800674 }
675
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800676 if (!newasid) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700677 /*
678 * If we preempted while the guest was executing, then reload
679 * the pre-empted ASID
680 */
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800681 if (current->flags & PF_VCPU) {
David Daney48c4ac92013-05-13 13:56:44 -0700682 write_c0_entryhi(vcpu->arch.
Paul Burton4edf00a2016-05-06 14:36:23 +0100683 preempt_entryhi & asid_mask);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800684 ehb();
685 }
686 } else {
687 /* New ASIDs were allocated for the VM */
688
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700689 /*
690 * Were we in guest context? If so then the pre-empted ASID is
691 * no longer valid, we need to set it to what it should be based
692 * on the mode of the Guest (Kernel/User)
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800693 */
694 if (current->flags & PF_VCPU) {
695 if (KVM_GUEST_KERNEL_MODE(vcpu))
David Daney48c4ac92013-05-13 13:56:44 -0700696 write_c0_entryhi(vcpu->arch.
697 guest_kernel_asid[cpu] &
Paul Burton4edf00a2016-05-06 14:36:23 +0100698 asid_mask);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800699 else
David Daney48c4ac92013-05-13 13:56:44 -0700700 write_c0_entryhi(vcpu->arch.
701 guest_user_asid[cpu] &
Paul Burton4edf00a2016-05-06 14:36:23 +0100702 asid_mask);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800703 ehb();
704 }
705 }
706
James Hoganb86ecb32015-02-09 16:35:20 +0000707 /* restore guest state to registers */
708 kvm_mips_callbacks->vcpu_set_regs(vcpu);
709
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800710 local_irq_restore(flags);
711
712}
James Hogancb1b4472015-12-16 23:49:30 +0000713EXPORT_SYMBOL_GPL(kvm_arch_vcpu_load);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800714
715/* ASID can change if another task is scheduled during preemption */
716void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
717{
718 unsigned long flags;
719 uint32_t cpu;
720
721 local_irq_save(flags);
722
723 cpu = smp_processor_id();
724
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800725 vcpu->arch.preempt_entryhi = read_c0_entryhi();
726 vcpu->arch.last_sched_cpu = cpu;
727
James Hoganb86ecb32015-02-09 16:35:20 +0000728 /* save guest state in registers */
729 kvm_mips_callbacks->vcpu_get_regs(vcpu);
730
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800731 if (((cpu_context(cpu, current->mm) ^ asid_cache(cpu)) &
Paul Burton4edf00a2016-05-06 14:36:23 +0100732 asid_version_mask(cpu))) {
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800733 kvm_debug("%s: Dropping MMU Context: %#lx\n", __func__,
734 cpu_context(cpu, current->mm));
735 drop_mmu_context(current->mm, cpu);
736 }
737 write_c0_entryhi(cpu_asid(cpu, current->mm));
738 ehb();
739
740 local_irq_restore(flags);
741}
James Hogancb1b4472015-12-16 23:49:30 +0000742EXPORT_SYMBOL_GPL(kvm_arch_vcpu_put);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800743
744uint32_t kvm_get_inst(uint32_t *opc, struct kvm_vcpu *vcpu)
745{
746 struct mips_coproc *cop0 = vcpu->arch.cop0;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700747 unsigned long paddr, flags, vpn2, asid;
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800748 uint32_t inst;
749 int index;
750
751 if (KVM_GUEST_KSEGX((unsigned long) opc) < KVM_GUEST_KSEG0 ||
752 KVM_GUEST_KSEGX((unsigned long) opc) == KVM_GUEST_KSEG23) {
753 local_irq_save(flags);
754 index = kvm_mips_host_tlb_lookup(vcpu, (unsigned long) opc);
755 if (index >= 0) {
756 inst = *(opc);
757 } else {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700758 vpn2 = (unsigned long) opc & VPN2_MASK;
Paul Burtonca64c2b2016-05-06 14:36:20 +0100759 asid = kvm_read_c0_guest_entryhi(cop0) &
760 KVM_ENTRYHI_ASID;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700761 index = kvm_mips_guest_tlb_lookup(vcpu, vpn2 | asid);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800762 if (index < 0) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700763 kvm_err("%s: get_user_failed for %p, vcpu: %p, ASID: %#lx\n",
764 __func__, opc, vcpu, read_c0_entryhi());
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800765 kvm_mips_dump_host_tlbs();
766 local_irq_restore(flags);
767 return KVM_INVALID_INST;
768 }
769 kvm_mips_handle_mapped_seg_tlb_fault(vcpu,
770 &vcpu->arch.
771 guest_tlb[index],
772 NULL, NULL);
773 inst = *(opc);
774 }
775 local_irq_restore(flags);
776 } else if (KVM_GUEST_KSEGX(opc) == KVM_GUEST_KSEG0) {
777 paddr =
778 kvm_mips_translate_guest_kseg0_to_hpa(vcpu,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700779 (unsigned long) opc);
Sanjay Lal858dd5d2012-11-21 18:34:05 -0800780 inst = *(uint32_t *) CKSEG0ADDR(paddr);
781 } else {
782 kvm_err("%s: illegal address: %p\n", __func__, opc);
783 return KVM_INVALID_INST;
784 }
785
786 return inst;
787}
James Hogancb1b4472015-12-16 23:49:30 +0000788EXPORT_SYMBOL_GPL(kvm_get_inst);