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
6 * KVM/MIPS MMU handling in the KVM module.
8 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
9 * Authors: Sanjay Lal <sanjayl@kymasys.com>
12 #include <linux/kvm_host.h>
13 #include <asm/mmu_context.h>
15 static u32 kvm_mips_get_kernel_asid(struct kvm_vcpu *vcpu)
17 int cpu = smp_processor_id();
19 return vcpu->arch.guest_kernel_asid[cpu] &
20 cpu_asid_mask(&cpu_data[cpu]);
23 static u32 kvm_mips_get_user_asid(struct kvm_vcpu *vcpu)
25 int cpu = smp_processor_id();
27 return vcpu->arch.guest_user_asid[cpu] &
28 cpu_asid_mask(&cpu_data[cpu]);
31 static int kvm_mips_map_page(struct kvm *kvm, gfn_t gfn)
33 int srcu_idx, err = 0;
36 if (kvm->arch.guest_pmap[gfn] != KVM_INVALID_PAGE)
39 srcu_idx = srcu_read_lock(&kvm->srcu);
40 pfn = gfn_to_pfn(kvm, gfn);
42 if (is_error_pfn(pfn)) {
43 kvm_err("Couldn't get pfn for gfn %#llx!\n", gfn);
48 kvm->arch.guest_pmap[gfn] = pfn;
50 srcu_read_unlock(&kvm->srcu, srcu_idx);
54 /* Translate guest KSEG0 addresses to Host PA */
55 unsigned long kvm_mips_translate_guest_kseg0_to_hpa(struct kvm_vcpu *vcpu,
59 unsigned long offset = gva & ~PAGE_MASK;
60 struct kvm *kvm = vcpu->kvm;
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;
68 gfn = (KVM_GUEST_CPHYSADDR(gva) >> PAGE_SHIFT);
70 if (gfn >= kvm->arch.guest_pmap_npages) {
71 kvm_err("%s: Invalid gfn: %#llx, GVA: %#lx\n", __func__, gfn,
73 return KVM_INVALID_PAGE;
76 if (kvm_mips_map_page(vcpu->kvm, gfn) < 0)
77 return KVM_INVALID_ADDR;
79 return (kvm->arch.guest_pmap[gfn] << PAGE_SHIFT) + offset;
82 /* XXXKYMA: Must be called with interrupts disabled */
83 int kvm_mips_handle_kseg0_tlb_fault(unsigned long badvaddr,
84 struct kvm_vcpu *vcpu)
88 unsigned long vaddr = 0;
89 unsigned long entryhi = 0, entrylo0 = 0, entrylo1 = 0;
90 struct kvm *kvm = vcpu->kvm;
91 const int flush_dcache_mask = 0;
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();
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__,
104 kvm_mips_dump_host_tlbs();
107 vaddr = badvaddr & (PAGE_MASK << 1);
109 if (kvm_mips_map_page(vcpu->kvm, gfn) < 0)
112 if (kvm_mips_map_page(vcpu->kvm, gfn ^ 0x1) < 0)
115 pfn0 = kvm->arch.guest_pmap[gfn & ~0x1];
116 pfn1 = kvm->arch.guest_pmap[gfn | 0x1];
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;
124 entryhi = (vaddr | kvm_mips_get_kernel_asid(vcpu));
125 ret = kvm_mips_host_tlb_write(vcpu, entryhi, entrylo0, entrylo1,
132 int kvm_mips_handle_mapped_seg_tlb_fault(struct kvm_vcpu *vcpu,
133 struct kvm_mips_tlb *tlb)
135 unsigned long entryhi = 0, entrylo0 = 0, entrylo1 = 0;
136 struct kvm *kvm = vcpu->kvm;
137 kvm_pfn_t pfn0, pfn1;
140 if ((tlb->tlb_hi & VPN2_MASK) == 0) {
144 if (kvm_mips_map_page(kvm, mips3_tlbpfn_to_paddr(tlb->tlb_lo[0])
148 if (kvm_mips_map_page(kvm, mips3_tlbpfn_to_paddr(tlb->tlb_lo[1])
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];
158 /* Get attributes from the Guest TLB */
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);
168 kvm_debug("@ %#lx tlb_lo0: 0x%08lx tlb_lo1: 0x%08lx\n", vcpu->arch.pc,
169 tlb->tlb_lo[0], tlb->tlb_lo[1]);
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,
182 void kvm_get_new_mmu_context(struct mm_struct *mm, unsigned long cpu,
183 struct kvm_vcpu *vcpu)
185 unsigned long asid = asid_cache(cpu);
187 asid += cpu_asid_inc();
188 if (!(asid & cpu_asid_mask(&cpu_data[cpu]))) {
189 if (cpu_has_vtag_icache)
192 kvm_local_flush_tlb_all(); /* start new asid cycle */
194 if (!asid) /* fix version if needed */
195 asid = asid_first_version(cpu);
198 cpu_context(cpu, mm) = asid_cache(cpu) = asid;
202 * kvm_mips_migrate_count() - Migrate timer.
203 * @vcpu: Virtual CPU.
205 * Migrate CP0_Count hrtimer to the current CPU by cancelling and restarting it
206 * if it was running prior to being cancelled.
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.
212 static void kvm_mips_migrate_count(struct kvm_vcpu *vcpu)
214 if (hrtimer_cancel(&vcpu->arch.comparecount_timer))
215 hrtimer_restart(&vcpu->arch.comparecount_timer);
218 /* Restore ASID once we are scheduled back after preemption */
219 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
221 unsigned long asid_mask = cpu_asid_mask(&cpu_data[cpu]);
225 kvm_debug("%s: vcpu %p, cpu: %d\n", __func__, vcpu, cpu);
227 /* Allocate new kernel and user ASIDs if needed */
229 local_irq_save(flags);
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];
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]);
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);
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.
257 kvm_mips_migrate_count(vcpu);
262 * If we preempted while the guest was executing, then reload
263 * the pre-empted ASID
265 if (current->flags & PF_VCPU) {
266 write_c0_entryhi(vcpu->arch.
267 preempt_entryhi & asid_mask);
271 /* New ASIDs were allocated for the VM */
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)
278 if (current->flags & PF_VCPU) {
279 if (KVM_GUEST_KERNEL_MODE(vcpu))
280 write_c0_entryhi(vcpu->arch.
281 guest_kernel_asid[cpu] &
284 write_c0_entryhi(vcpu->arch.
285 guest_user_asid[cpu] &
291 /* restore guest state to registers */
292 kvm_mips_callbacks->vcpu_set_regs(vcpu);
294 local_irq_restore(flags);
298 /* ASID can change if another task is scheduled during preemption */
299 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
304 local_irq_save(flags);
306 cpu = smp_processor_id();
308 vcpu->arch.preempt_entryhi = read_c0_entryhi();
309 vcpu->arch.last_sched_cpu = cpu;
311 /* save guest state in registers */
312 kvm_mips_callbacks->vcpu_get_regs(vcpu);
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);
320 write_c0_entryhi(cpu_asid(cpu, current->mm));
323 local_irq_restore(flags);
326 u32 kvm_get_inst(u32 *opc, struct kvm_vcpu *vcpu)
328 struct mips_coproc *cop0 = vcpu->arch.cop0;
329 unsigned long paddr, flags, vpn2, asid;
333 if (KVM_GUEST_KSEGX((unsigned long) opc) < KVM_GUEST_KSEG0 ||
334 KVM_GUEST_KSEGX((unsigned long) opc) == KVM_GUEST_KSEG23) {
335 local_irq_save(flags);
336 index = kvm_mips_host_tlb_lookup(vcpu, (unsigned long) opc);
340 vpn2 = (unsigned long) opc & VPN2_MASK;
341 asid = kvm_read_c0_guest_entryhi(cop0) &
343 index = kvm_mips_guest_tlb_lookup(vcpu, vpn2 | asid);
345 kvm_err("%s: get_user_failed for %p, vcpu: %p, ASID: %#lx\n",
346 __func__, opc, vcpu, read_c0_entryhi());
347 kvm_mips_dump_host_tlbs();
348 local_irq_restore(flags);
349 return KVM_INVALID_INST;
351 kvm_mips_handle_mapped_seg_tlb_fault(vcpu,
356 local_irq_restore(flags);
357 } else if (KVM_GUEST_KSEGX(opc) == KVM_GUEST_KSEG0) {
359 kvm_mips_translate_guest_kseg0_to_hpa(vcpu,
360 (unsigned long) opc);
361 inst = *(u32 *) CKSEG0ADDR(paddr);
363 kvm_err("%s: illegal address: %p\n", __func__, opc);
364 return KVM_INVALID_INST;