blob: 2f494ec5c939c0332487c6ea830908ffa797e846 [file] [log] [blame]
James Hogan403015b2016-06-09 14:19:10 +01001/*
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 MMU handling in the KVM module.
7 *
8 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
9 * Authors: Sanjay Lal <sanjayl@kymasys.com>
10 */
11
12#include <linux/kvm_host.h>
13#include <asm/mmu_context.h>
14
15static u32 kvm_mips_get_kernel_asid(struct kvm_vcpu *vcpu)
16{
17 int cpu = smp_processor_id();
18
19 return vcpu->arch.guest_kernel_asid[cpu] &
20 cpu_asid_mask(&cpu_data[cpu]);
21}
22
23static u32 kvm_mips_get_user_asid(struct kvm_vcpu *vcpu)
24{
25 int cpu = smp_processor_id();
26
27 return vcpu->arch.guest_user_asid[cpu] &
28 cpu_asid_mask(&cpu_data[cpu]);
29}
30
31static int kvm_mips_map_page(struct kvm *kvm, gfn_t gfn)
32{
33 int srcu_idx, err = 0;
34 kvm_pfn_t pfn;
35
36 if (kvm->arch.guest_pmap[gfn] != KVM_INVALID_PAGE)
37 return 0;
38
39 srcu_idx = srcu_read_lock(&kvm->srcu);
James Hogan9befad22016-06-09 14:19:11 +010040 pfn = gfn_to_pfn(kvm, gfn);
James Hogan403015b2016-06-09 14:19:10 +010041
James Hogan9befad22016-06-09 14:19:11 +010042 if (is_error_pfn(pfn)) {
James Hogan403015b2016-06-09 14:19:10 +010043 kvm_err("Couldn't get pfn for gfn %#llx!\n", gfn);
44 err = -EFAULT;
45 goto out;
46 }
47
48 kvm->arch.guest_pmap[gfn] = pfn;
49out:
50 srcu_read_unlock(&kvm->srcu, srcu_idx);
51 return err;
52}
53
54/* Translate guest KSEG0 addresses to Host PA */
55unsigned long kvm_mips_translate_guest_kseg0_to_hpa(struct kvm_vcpu *vcpu,
56 unsigned long gva)
57{
58 gfn_t gfn;
59 unsigned long offset = gva & ~PAGE_MASK;
60 struct kvm *kvm = vcpu->kvm;
61
62 if (KVM_GUEST_KSEGX(gva) != KVM_GUEST_KSEG0) {
63 kvm_err("%s/%p: Invalid gva: %#lx\n", __func__,
64 __builtin_return_address(0), gva);
65 return KVM_INVALID_PAGE;
66 }
67
68 gfn = (KVM_GUEST_CPHYSADDR(gva) >> PAGE_SHIFT);
69
70 if (gfn >= kvm->arch.guest_pmap_npages) {
71 kvm_err("%s: Invalid gfn: %#llx, GVA: %#lx\n", __func__, gfn,
72 gva);
73 return KVM_INVALID_PAGE;
74 }
75
76 if (kvm_mips_map_page(vcpu->kvm, gfn) < 0)
77 return KVM_INVALID_ADDR;
78
79 return (kvm->arch.guest_pmap[gfn] << PAGE_SHIFT) + offset;
80}
81
82/* XXXKYMA: Must be called with interrupts disabled */
83int kvm_mips_handle_kseg0_tlb_fault(unsigned long badvaddr,
84 struct kvm_vcpu *vcpu)
85{
86 gfn_t gfn;
87 kvm_pfn_t pfn0, pfn1;
88 unsigned long vaddr = 0;
89 unsigned long entryhi = 0, entrylo0 = 0, entrylo1 = 0;
James Hogan403015b2016-06-09 14:19:10 +010090 struct kvm *kvm = vcpu->kvm;
91 const int flush_dcache_mask = 0;
92 int ret;
93
94 if (KVM_GUEST_KSEGX(badvaddr) != KVM_GUEST_KSEG0) {
95 kvm_err("%s: Invalid BadVaddr: %#lx\n", __func__, badvaddr);
96 kvm_mips_dump_host_tlbs();
97 return -1;
98 }
99
100 gfn = (KVM_GUEST_CPHYSADDR(badvaddr) >> PAGE_SHIFT);
101 if (gfn >= kvm->arch.guest_pmap_npages) {
102 kvm_err("%s: Invalid gfn: %#llx, BadVaddr: %#lx\n", __func__,
103 gfn, badvaddr);
104 kvm_mips_dump_host_tlbs();
105 return -1;
106 }
James Hogan403015b2016-06-09 14:19:10 +0100107 vaddr = badvaddr & (PAGE_MASK << 1);
108
109 if (kvm_mips_map_page(vcpu->kvm, gfn) < 0)
110 return -1;
111
112 if (kvm_mips_map_page(vcpu->kvm, gfn ^ 0x1) < 0)
113 return -1;
114
James Hogan021df202016-06-09 14:19:12 +0100115 pfn0 = kvm->arch.guest_pmap[gfn & ~0x1];
116 pfn1 = kvm->arch.guest_pmap[gfn | 0x1];
James Hogan403015b2016-06-09 14:19:10 +0100117
James Hogane6207bb2016-06-09 14:19:19 +0100118 entrylo0 = mips3_paddr_to_tlbpfn(pfn0 << PAGE_SHIFT) |
119 (0x3 << ENTRYLO_C_SHIFT) | ENTRYLO_D | ENTRYLO_V;
120 entrylo1 = mips3_paddr_to_tlbpfn(pfn1 << PAGE_SHIFT) |
121 (0x3 << ENTRYLO_C_SHIFT) | ENTRYLO_D | ENTRYLO_V;
James Hogan403015b2016-06-09 14:19:10 +0100122
123 preempt_disable();
124 entryhi = (vaddr | kvm_mips_get_kernel_asid(vcpu));
125 ret = kvm_mips_host_tlb_write(vcpu, entryhi, entrylo0, entrylo1,
126 flush_dcache_mask);
127 preempt_enable();
128
129 return ret;
130}
131
132int kvm_mips_handle_mapped_seg_tlb_fault(struct kvm_vcpu *vcpu,
James Hogan26ee17f2016-06-09 14:19:13 +0100133 struct kvm_mips_tlb *tlb)
James Hogan403015b2016-06-09 14:19:10 +0100134{
135 unsigned long entryhi = 0, entrylo0 = 0, entrylo1 = 0;
136 struct kvm *kvm = vcpu->kvm;
137 kvm_pfn_t pfn0, pfn1;
138 int ret;
139
140 if ((tlb->tlb_hi & VPN2_MASK) == 0) {
141 pfn0 = 0;
142 pfn1 = 0;
143 } else {
James Hogan9fbfb062016-06-09 14:19:17 +0100144 if (kvm_mips_map_page(kvm, mips3_tlbpfn_to_paddr(tlb->tlb_lo[0])
James Hogan403015b2016-06-09 14:19:10 +0100145 >> PAGE_SHIFT) < 0)
146 return -1;
147
James Hogan9fbfb062016-06-09 14:19:17 +0100148 if (kvm_mips_map_page(kvm, mips3_tlbpfn_to_paddr(tlb->tlb_lo[1])
James Hogan403015b2016-06-09 14:19:10 +0100149 >> PAGE_SHIFT) < 0)
150 return -1;
151
James Hogan9fbfb062016-06-09 14:19:17 +0100152 pfn0 = kvm->arch.guest_pmap[
153 mips3_tlbpfn_to_paddr(tlb->tlb_lo[0]) >> PAGE_SHIFT];
154 pfn1 = kvm->arch.guest_pmap[
155 mips3_tlbpfn_to_paddr(tlb->tlb_lo[1]) >> PAGE_SHIFT];
James Hogan403015b2016-06-09 14:19:10 +0100156 }
157
James Hogan403015b2016-06-09 14:19:10 +0100158 /* Get attributes from the Guest TLB */
James Hogane6207bb2016-06-09 14:19:19 +0100159 entrylo0 = mips3_paddr_to_tlbpfn(pfn0 << PAGE_SHIFT) |
160 (0x3 << ENTRYLO_C_SHIFT) |
161 (tlb->tlb_lo[0] & ENTRYLO_D) |
162 (tlb->tlb_lo[0] & ENTRYLO_V);
163 entrylo1 = mips3_paddr_to_tlbpfn(pfn1 << PAGE_SHIFT) |
164 (0x3 << ENTRYLO_C_SHIFT) |
165 (tlb->tlb_lo[1] & ENTRYLO_D) |
166 (tlb->tlb_lo[1] & ENTRYLO_V);
James Hogan403015b2016-06-09 14:19:10 +0100167
168 kvm_debug("@ %#lx tlb_lo0: 0x%08lx tlb_lo1: 0x%08lx\n", vcpu->arch.pc,
James Hogan9fbfb062016-06-09 14:19:17 +0100169 tlb->tlb_lo[0], tlb->tlb_lo[1]);
James Hogan403015b2016-06-09 14:19:10 +0100170
171 preempt_disable();
172 entryhi = (tlb->tlb_hi & VPN2_MASK) | (KVM_GUEST_KERNEL_MODE(vcpu) ?
173 kvm_mips_get_kernel_asid(vcpu) :
174 kvm_mips_get_user_asid(vcpu));
175 ret = kvm_mips_host_tlb_write(vcpu, entryhi, entrylo0, entrylo1,
176 tlb->tlb_mask);
177 preempt_enable();
178
179 return ret;
180}
181
182void kvm_get_new_mmu_context(struct mm_struct *mm, unsigned long cpu,
183 struct kvm_vcpu *vcpu)
184{
185 unsigned long asid = asid_cache(cpu);
186
187 asid += cpu_asid_inc();
188 if (!(asid & cpu_asid_mask(&cpu_data[cpu]))) {
189 if (cpu_has_vtag_icache)
190 flush_icache_all();
191
192 kvm_local_flush_tlb_all(); /* start new asid cycle */
193
194 if (!asid) /* fix version if needed */
195 asid = asid_first_version(cpu);
196 }
197
198 cpu_context(cpu, mm) = asid_cache(cpu) = asid;
199}
200
201/**
202 * kvm_mips_migrate_count() - Migrate timer.
203 * @vcpu: Virtual CPU.
204 *
205 * Migrate CP0_Count hrtimer to the current CPU by cancelling and restarting it
206 * if it was running prior to being cancelled.
207 *
208 * Must be called when the VCPU is migrated to a different CPU to ensure that
209 * timer expiry during guest execution interrupts the guest and causes the
210 * interrupt to be delivered in a timely manner.
211 */
212static void kvm_mips_migrate_count(struct kvm_vcpu *vcpu)
213{
214 if (hrtimer_cancel(&vcpu->arch.comparecount_timer))
215 hrtimer_restart(&vcpu->arch.comparecount_timer);
216}
217
218/* Restore ASID once we are scheduled back after preemption */
219void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
220{
221 unsigned long asid_mask = cpu_asid_mask(&cpu_data[cpu]);
222 unsigned long flags;
223 int newasid = 0;
224
225 kvm_debug("%s: vcpu %p, cpu: %d\n", __func__, vcpu, cpu);
226
227 /* Allocate new kernel and user ASIDs if needed */
228
229 local_irq_save(flags);
230
231 if ((vcpu->arch.guest_kernel_asid[cpu] ^ asid_cache(cpu)) &
232 asid_version_mask(cpu)) {
233 kvm_get_new_mmu_context(&vcpu->arch.guest_kernel_mm, cpu, vcpu);
234 vcpu->arch.guest_kernel_asid[cpu] =
235 vcpu->arch.guest_kernel_mm.context.asid[cpu];
236 kvm_get_new_mmu_context(&vcpu->arch.guest_user_mm, cpu, vcpu);
237 vcpu->arch.guest_user_asid[cpu] =
238 vcpu->arch.guest_user_mm.context.asid[cpu];
239 newasid++;
240
241 kvm_debug("[%d]: cpu_context: %#lx\n", cpu,
242 cpu_context(cpu, current->mm));
243 kvm_debug("[%d]: Allocated new ASID for Guest Kernel: %#x\n",
244 cpu, vcpu->arch.guest_kernel_asid[cpu]);
245 kvm_debug("[%d]: Allocated new ASID for Guest User: %#x\n", cpu,
246 vcpu->arch.guest_user_asid[cpu]);
247 }
248
249 if (vcpu->arch.last_sched_cpu != cpu) {
250 kvm_debug("[%d->%d]KVM VCPU[%d] switch\n",
251 vcpu->arch.last_sched_cpu, cpu, vcpu->vcpu_id);
252 /*
253 * Migrate the timer interrupt to the current CPU so that it
254 * always interrupts the guest and synchronously triggers a
255 * guest timer interrupt.
256 */
257 kvm_mips_migrate_count(vcpu);
258 }
259
260 if (!newasid) {
261 /*
262 * If we preempted while the guest was executing, then reload
263 * the pre-empted ASID
264 */
265 if (current->flags & PF_VCPU) {
266 write_c0_entryhi(vcpu->arch.
267 preempt_entryhi & asid_mask);
268 ehb();
269 }
270 } else {
271 /* New ASIDs were allocated for the VM */
272
273 /*
274 * Were we in guest context? If so then the pre-empted ASID is
275 * no longer valid, we need to set it to what it should be based
276 * on the mode of the Guest (Kernel/User)
277 */
278 if (current->flags & PF_VCPU) {
279 if (KVM_GUEST_KERNEL_MODE(vcpu))
280 write_c0_entryhi(vcpu->arch.
281 guest_kernel_asid[cpu] &
282 asid_mask);
283 else
284 write_c0_entryhi(vcpu->arch.
285 guest_user_asid[cpu] &
286 asid_mask);
287 ehb();
288 }
289 }
290
291 /* restore guest state to registers */
292 kvm_mips_callbacks->vcpu_set_regs(vcpu);
293
294 local_irq_restore(flags);
295
296}
297
298/* ASID can change if another task is scheduled during preemption */
299void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
300{
301 unsigned long flags;
302 int cpu;
303
304 local_irq_save(flags);
305
306 cpu = smp_processor_id();
307
308 vcpu->arch.preempt_entryhi = read_c0_entryhi();
309 vcpu->arch.last_sched_cpu = cpu;
310
311 /* save guest state in registers */
312 kvm_mips_callbacks->vcpu_get_regs(vcpu);
313
314 if (((cpu_context(cpu, current->mm) ^ asid_cache(cpu)) &
315 asid_version_mask(cpu))) {
316 kvm_debug("%s: Dropping MMU Context: %#lx\n", __func__,
317 cpu_context(cpu, current->mm));
318 drop_mmu_context(current->mm, cpu);
319 }
320 write_c0_entryhi(cpu_asid(cpu, current->mm));
321 ehb();
322
323 local_irq_restore(flags);
324}
325
326u32 kvm_get_inst(u32 *opc, struct kvm_vcpu *vcpu)
327{
328 struct mips_coproc *cop0 = vcpu->arch.cop0;
329 unsigned long paddr, flags, vpn2, asid;
James Hogan35fec262016-06-09 14:19:21 +0100330 unsigned long va = (unsigned long)opc;
James Hogan403015b2016-06-09 14:19:10 +0100331 u32 inst;
332 int index;
333
James Hogan35fec262016-06-09 14:19:21 +0100334 if (KVM_GUEST_KSEGX(va) < KVM_GUEST_KSEG0 ||
335 KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG23) {
James Hogan403015b2016-06-09 14:19:10 +0100336 local_irq_save(flags);
James Hogan35fec262016-06-09 14:19:21 +0100337 index = kvm_mips_host_tlb_lookup(vcpu, va);
James Hogan403015b2016-06-09 14:19:10 +0100338 if (index >= 0) {
339 inst = *(opc);
340 } else {
James Hogan35fec262016-06-09 14:19:21 +0100341 vpn2 = va & VPN2_MASK;
James Hogan403015b2016-06-09 14:19:10 +0100342 asid = kvm_read_c0_guest_entryhi(cop0) &
343 KVM_ENTRYHI_ASID;
344 index = kvm_mips_guest_tlb_lookup(vcpu, vpn2 | asid);
345 if (index < 0) {
346 kvm_err("%s: get_user_failed for %p, vcpu: %p, ASID: %#lx\n",
347 __func__, opc, vcpu, read_c0_entryhi());
348 kvm_mips_dump_host_tlbs();
James Hoganeafc4ed2016-06-14 09:40:16 +0100349 kvm_mips_dump_guest_tlbs(vcpu);
James Hogan403015b2016-06-09 14:19:10 +0100350 local_irq_restore(flags);
351 return KVM_INVALID_INST;
352 }
353 kvm_mips_handle_mapped_seg_tlb_fault(vcpu,
354 &vcpu->arch.
James Hogan26ee17f2016-06-09 14:19:13 +0100355 guest_tlb[index]);
James Hogan403015b2016-06-09 14:19:10 +0100356 inst = *(opc);
357 }
358 local_irq_restore(flags);
James Hogan35fec262016-06-09 14:19:21 +0100359 } else if (KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG0) {
360 paddr = kvm_mips_translate_guest_kseg0_to_hpa(vcpu, va);
James Hogan403015b2016-06-09 14:19:10 +0100361 inst = *(u32 *) CKSEG0ADDR(paddr);
362 } else {
363 kvm_err("%s: illegal address: %p\n", __func__, opc);
364 return KVM_INVALID_INST;
365 }
366
367 return inst;
368}