blob: 9924c1d253a7c655ba0e41928ac2c9126ac4cb21 [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
118 entrylo0 = mips3_paddr_to_tlbpfn(pfn0 << PAGE_SHIFT) | (0x3 << 3) |
119 (1 << 2) | (0x1 << 1);
120 entrylo1 = mips3_paddr_to_tlbpfn(pfn1 << PAGE_SHIFT) | (0x3 << 3) |
121 (1 << 2) | (0x1 << 1);
122
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,
133 struct kvm_mips_tlb *tlb,
134 unsigned long *hpa0,
135 unsigned long *hpa1)
136{
137 unsigned long entryhi = 0, entrylo0 = 0, entrylo1 = 0;
138 struct kvm *kvm = vcpu->kvm;
139 kvm_pfn_t pfn0, pfn1;
140 int ret;
141
142 if ((tlb->tlb_hi & VPN2_MASK) == 0) {
143 pfn0 = 0;
144 pfn1 = 0;
145 } else {
146 if (kvm_mips_map_page(kvm, mips3_tlbpfn_to_paddr(tlb->tlb_lo0)
147 >> PAGE_SHIFT) < 0)
148 return -1;
149
150 if (kvm_mips_map_page(kvm, mips3_tlbpfn_to_paddr(tlb->tlb_lo1)
151 >> PAGE_SHIFT) < 0)
152 return -1;
153
154 pfn0 = kvm->arch.guest_pmap[mips3_tlbpfn_to_paddr(tlb->tlb_lo0)
155 >> PAGE_SHIFT];
156 pfn1 = kvm->arch.guest_pmap[mips3_tlbpfn_to_paddr(tlb->tlb_lo1)
157 >> PAGE_SHIFT];
158 }
159
160 if (hpa0)
161 *hpa0 = pfn0 << PAGE_SHIFT;
162
163 if (hpa1)
164 *hpa1 = pfn1 << PAGE_SHIFT;
165
166 /* Get attributes from the Guest TLB */
167 entrylo0 = mips3_paddr_to_tlbpfn(pfn0 << PAGE_SHIFT) | (0x3 << 3) |
168 (tlb->tlb_lo0 & MIPS3_PG_D) | (tlb->tlb_lo0 & MIPS3_PG_V);
169 entrylo1 = mips3_paddr_to_tlbpfn(pfn1 << PAGE_SHIFT) | (0x3 << 3) |
170 (tlb->tlb_lo1 & MIPS3_PG_D) | (tlb->tlb_lo1 & MIPS3_PG_V);
171
172 kvm_debug("@ %#lx tlb_lo0: 0x%08lx tlb_lo1: 0x%08lx\n", vcpu->arch.pc,
173 tlb->tlb_lo0, tlb->tlb_lo1);
174
175 preempt_disable();
176 entryhi = (tlb->tlb_hi & VPN2_MASK) | (KVM_GUEST_KERNEL_MODE(vcpu) ?
177 kvm_mips_get_kernel_asid(vcpu) :
178 kvm_mips_get_user_asid(vcpu));
179 ret = kvm_mips_host_tlb_write(vcpu, entryhi, entrylo0, entrylo1,
180 tlb->tlb_mask);
181 preempt_enable();
182
183 return ret;
184}
185
186void kvm_get_new_mmu_context(struct mm_struct *mm, unsigned long cpu,
187 struct kvm_vcpu *vcpu)
188{
189 unsigned long asid = asid_cache(cpu);
190
191 asid += cpu_asid_inc();
192 if (!(asid & cpu_asid_mask(&cpu_data[cpu]))) {
193 if (cpu_has_vtag_icache)
194 flush_icache_all();
195
196 kvm_local_flush_tlb_all(); /* start new asid cycle */
197
198 if (!asid) /* fix version if needed */
199 asid = asid_first_version(cpu);
200 }
201
202 cpu_context(cpu, mm) = asid_cache(cpu) = asid;
203}
204
205/**
206 * kvm_mips_migrate_count() - Migrate timer.
207 * @vcpu: Virtual CPU.
208 *
209 * Migrate CP0_Count hrtimer to the current CPU by cancelling and restarting it
210 * if it was running prior to being cancelled.
211 *
212 * Must be called when the VCPU is migrated to a different CPU to ensure that
213 * timer expiry during guest execution interrupts the guest and causes the
214 * interrupt to be delivered in a timely manner.
215 */
216static void kvm_mips_migrate_count(struct kvm_vcpu *vcpu)
217{
218 if (hrtimer_cancel(&vcpu->arch.comparecount_timer))
219 hrtimer_restart(&vcpu->arch.comparecount_timer);
220}
221
222/* Restore ASID once we are scheduled back after preemption */
223void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
224{
225 unsigned long asid_mask = cpu_asid_mask(&cpu_data[cpu]);
226 unsigned long flags;
227 int newasid = 0;
228
229 kvm_debug("%s: vcpu %p, cpu: %d\n", __func__, vcpu, cpu);
230
231 /* Allocate new kernel and user ASIDs if needed */
232
233 local_irq_save(flags);
234
235 if ((vcpu->arch.guest_kernel_asid[cpu] ^ asid_cache(cpu)) &
236 asid_version_mask(cpu)) {
237 kvm_get_new_mmu_context(&vcpu->arch.guest_kernel_mm, cpu, vcpu);
238 vcpu->arch.guest_kernel_asid[cpu] =
239 vcpu->arch.guest_kernel_mm.context.asid[cpu];
240 kvm_get_new_mmu_context(&vcpu->arch.guest_user_mm, cpu, vcpu);
241 vcpu->arch.guest_user_asid[cpu] =
242 vcpu->arch.guest_user_mm.context.asid[cpu];
243 newasid++;
244
245 kvm_debug("[%d]: cpu_context: %#lx\n", cpu,
246 cpu_context(cpu, current->mm));
247 kvm_debug("[%d]: Allocated new ASID for Guest Kernel: %#x\n",
248 cpu, vcpu->arch.guest_kernel_asid[cpu]);
249 kvm_debug("[%d]: Allocated new ASID for Guest User: %#x\n", cpu,
250 vcpu->arch.guest_user_asid[cpu]);
251 }
252
253 if (vcpu->arch.last_sched_cpu != cpu) {
254 kvm_debug("[%d->%d]KVM VCPU[%d] switch\n",
255 vcpu->arch.last_sched_cpu, cpu, vcpu->vcpu_id);
256 /*
257 * Migrate the timer interrupt to the current CPU so that it
258 * always interrupts the guest and synchronously triggers a
259 * guest timer interrupt.
260 */
261 kvm_mips_migrate_count(vcpu);
262 }
263
264 if (!newasid) {
265 /*
266 * If we preempted while the guest was executing, then reload
267 * the pre-empted ASID
268 */
269 if (current->flags & PF_VCPU) {
270 write_c0_entryhi(vcpu->arch.
271 preempt_entryhi & asid_mask);
272 ehb();
273 }
274 } else {
275 /* New ASIDs were allocated for the VM */
276
277 /*
278 * Were we in guest context? If so then the pre-empted ASID is
279 * no longer valid, we need to set it to what it should be based
280 * on the mode of the Guest (Kernel/User)
281 */
282 if (current->flags & PF_VCPU) {
283 if (KVM_GUEST_KERNEL_MODE(vcpu))
284 write_c0_entryhi(vcpu->arch.
285 guest_kernel_asid[cpu] &
286 asid_mask);
287 else
288 write_c0_entryhi(vcpu->arch.
289 guest_user_asid[cpu] &
290 asid_mask);
291 ehb();
292 }
293 }
294
295 /* restore guest state to registers */
296 kvm_mips_callbacks->vcpu_set_regs(vcpu);
297
298 local_irq_restore(flags);
299
300}
301
302/* ASID can change if another task is scheduled during preemption */
303void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
304{
305 unsigned long flags;
306 int cpu;
307
308 local_irq_save(flags);
309
310 cpu = smp_processor_id();
311
312 vcpu->arch.preempt_entryhi = read_c0_entryhi();
313 vcpu->arch.last_sched_cpu = cpu;
314
315 /* save guest state in registers */
316 kvm_mips_callbacks->vcpu_get_regs(vcpu);
317
318 if (((cpu_context(cpu, current->mm) ^ asid_cache(cpu)) &
319 asid_version_mask(cpu))) {
320 kvm_debug("%s: Dropping MMU Context: %#lx\n", __func__,
321 cpu_context(cpu, current->mm));
322 drop_mmu_context(current->mm, cpu);
323 }
324 write_c0_entryhi(cpu_asid(cpu, current->mm));
325 ehb();
326
327 local_irq_restore(flags);
328}
329
330u32 kvm_get_inst(u32 *opc, struct kvm_vcpu *vcpu)
331{
332 struct mips_coproc *cop0 = vcpu->arch.cop0;
333 unsigned long paddr, flags, vpn2, asid;
334 u32 inst;
335 int index;
336
337 if (KVM_GUEST_KSEGX((unsigned long) opc) < KVM_GUEST_KSEG0 ||
338 KVM_GUEST_KSEGX((unsigned long) opc) == KVM_GUEST_KSEG23) {
339 local_irq_save(flags);
340 index = kvm_mips_host_tlb_lookup(vcpu, (unsigned long) opc);
341 if (index >= 0) {
342 inst = *(opc);
343 } else {
344 vpn2 = (unsigned long) opc & VPN2_MASK;
345 asid = kvm_read_c0_guest_entryhi(cop0) &
346 KVM_ENTRYHI_ASID;
347 index = kvm_mips_guest_tlb_lookup(vcpu, vpn2 | asid);
348 if (index < 0) {
349 kvm_err("%s: get_user_failed for %p, vcpu: %p, ASID: %#lx\n",
350 __func__, opc, vcpu, read_c0_entryhi());
351 kvm_mips_dump_host_tlbs();
352 local_irq_restore(flags);
353 return KVM_INVALID_INST;
354 }
355 kvm_mips_handle_mapped_seg_tlb_fault(vcpu,
356 &vcpu->arch.
357 guest_tlb[index],
358 NULL, NULL);
359 inst = *(opc);
360 }
361 local_irq_restore(flags);
362 } else if (KVM_GUEST_KSEGX(opc) == KVM_GUEST_KSEG0) {
363 paddr =
364 kvm_mips_translate_guest_kseg0_to_hpa(vcpu,
365 (unsigned long) opc);
366 inst = *(u32 *) CKSEG0ADDR(paddr);
367 } else {
368 kvm_err("%s: illegal address: %p\n", __func__, opc);
369 return KVM_INVALID_INST;
370 }
371
372 return inst;
373}