Commit | Line | Data |
---|---|---|
403015b3 JH |
1 | /* |
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 | ||
15 | static 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 | ||
23 | static 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 | ||
31 | static 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); | |
9befad23 | 40 | pfn = gfn_to_pfn(kvm, gfn); |
403015b3 | 41 | |
9befad23 | 42 | if (is_error_pfn(pfn)) { |
403015b3 JH |
43 | 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; | |
49 | out: | |
50 | srcu_read_unlock(&kvm->srcu, srcu_idx); | |
51 | return err; | |
52 | } | |
53 | ||
54 | /* Translate guest KSEG0 addresses to Host PA */ | |
55 | unsigned 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 */ | |
83 | int 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; | |
403015b3 JH |
90 | 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 | } | |
403015b3 JH |
107 | 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 | ||
021df206 JH |
115 | pfn0 = kvm->arch.guest_pmap[gfn & ~0x1]; |
116 | pfn1 = kvm->arch.guest_pmap[gfn | 0x1]; | |
403015b3 | 117 | |
e6207bbe JH |
118 | 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; | |
403015b3 JH |
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 | ||
132 | int kvm_mips_handle_mapped_seg_tlb_fault(struct kvm_vcpu *vcpu, | |
26ee17ff | 133 | struct kvm_mips_tlb *tlb) |
403015b3 JH |
134 | { |
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 { | |
9fbfb06a | 144 | if (kvm_mips_map_page(kvm, mips3_tlbpfn_to_paddr(tlb->tlb_lo[0]) |
403015b3 JH |
145 | >> PAGE_SHIFT) < 0) |
146 | return -1; | |
147 | ||
9fbfb06a | 148 | if (kvm_mips_map_page(kvm, mips3_tlbpfn_to_paddr(tlb->tlb_lo[1]) |
403015b3 JH |
149 | >> PAGE_SHIFT) < 0) |
150 | return -1; | |
151 | ||
9fbfb06a JH |
152 | 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]; | |
403015b3 JH |
156 | } |
157 | ||
403015b3 | 158 | /* Get attributes from the Guest TLB */ |
e6207bbe JH |
159 | 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); | |
403015b3 JH |
167 | |
168 | kvm_debug("@ %#lx tlb_lo0: 0x%08lx tlb_lo1: 0x%08lx\n", vcpu->arch.pc, | |
9fbfb06a | 169 | tlb->tlb_lo[0], tlb->tlb_lo[1]); |
403015b3 JH |
170 | |
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 | ||
182 | void 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 | */ | |
212 | static 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 */ | |
219 | void 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 */ | |
299 | void 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 | ||
326 | u32 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; | |
35fec262 | 330 | unsigned long va = (unsigned long)opc; |
403015b3 JH |
331 | u32 inst; |
332 | int index; | |
333 | ||
35fec262 JH |
334 | if (KVM_GUEST_KSEGX(va) < KVM_GUEST_KSEG0 || |
335 | KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG23) { | |
403015b3 | 336 | local_irq_save(flags); |
35fec262 | 337 | index = kvm_mips_host_tlb_lookup(vcpu, va); |
403015b3 JH |
338 | if (index >= 0) { |
339 | inst = *(opc); | |
340 | } else { | |
35fec262 | 341 | vpn2 = va & VPN2_MASK; |
403015b3 JH |
342 | 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(); | |
349 | local_irq_restore(flags); | |
350 | return KVM_INVALID_INST; | |
351 | } | |
352 | kvm_mips_handle_mapped_seg_tlb_fault(vcpu, | |
353 | &vcpu->arch. | |
26ee17ff | 354 | guest_tlb[index]); |
403015b3 JH |
355 | inst = *(opc); |
356 | } | |
357 | local_irq_restore(flags); | |
35fec262 JH |
358 | } else if (KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG0) { |
359 | paddr = kvm_mips_translate_guest_kseg0_to_hpa(vcpu, va); | |
403015b3 JH |
360 | inst = *(u32 *) CKSEG0ADDR(paddr); |
361 | } else { | |
362 | kvm_err("%s: illegal address: %p\n", __func__, opc); | |
363 | return KVM_INVALID_INST; | |
364 | } | |
365 | ||
366 | return inst; | |
367 | } |