linux-mips.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: James Hogan <james.hogan@imgtec.com>
To: linux-mips@linux-mips.org
Cc: "James Hogan" <james.hogan@imgtec.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Radim Krčmář" <rkrcmar@redhat.com>,
	"Ralf Baechle" <ralf@linux-mips.org>,
	kvm@vger.kernel.org
Subject: [PATCH v2 22/30] KVM: MIPS/MMU: Convert KSeg0 faults to page tables
Date: Thu, 2 Feb 2017 12:04:35 +0000	[thread overview]
Message-ID: <a9411989d9230885f1e0a4f21f73c7e7d9036972.1486036366.git-series.james.hogan@imgtec.com> (raw)
Message-ID: <20170202120435.SGMe4kT4UH5wmroxLB2Err6YKgvQPiAb398NxgdlzSU@z> (raw)
In-Reply-To: <cover.e37f86dece46fc3ed00a075d68119cab361cda8e.1486036366.git-series.james.hogan@imgtec.com>

Now that we have GVA page tables and an optimised TLB refill handler in
place, convert the handling of KSeg0 page faults from the guest to fill
the GVA page tables and invalidate the TLB entry, rather than filling a
TLB entry directly.

Signed-off-by: James Hogan <james.hogan@imgtec.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Radim Krčmář" <rkrcmar@redhat.com>
Cc: Ralf Baechle <ralf@linux-mips.org>
Cc: linux-mips@linux-mips.org
Cc: kvm@vger.kernel.org
---
 arch/mips/kvm/mmu.c | 79 +++++++++++++++++++++++++++++++++++++---------
 1 file changed, 64 insertions(+), 15 deletions(-)

diff --git a/arch/mips/kvm/mmu.c b/arch/mips/kvm/mmu.c
index dbf2b55ee874..afb47f21d8bc 100644
--- a/arch/mips/kvm/mmu.c
+++ b/arch/mips/kvm/mmu.c
@@ -14,6 +14,33 @@
 #include <asm/mmu_context.h>
 #include <asm/pgalloc.h>
 
+/*
+ * KVM_MMU_CACHE_MIN_PAGES is the number of GPA page table translation levels
+ * for which pages need to be cached.
+ */
+#if defined(__PAGETABLE_PMD_FOLDED)
+#define KVM_MMU_CACHE_MIN_PAGES 1
+#else
+#define KVM_MMU_CACHE_MIN_PAGES 2
+#endif
+
+static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
+				  int min, int max)
+{
+	void *page;
+
+	BUG_ON(max > KVM_NR_MEM_OBJS);
+	if (cache->nobjs >= min)
+		return 0;
+	while (cache->nobjs < max) {
+		page = (void *)__get_free_page(GFP_KERNEL);
+		if (!page)
+			return -ENOMEM;
+		cache->objects[cache->nobjs++] = page;
+	}
+	return 0;
+}
+
 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
 {
 	while (mc->nobjs)
@@ -151,6 +178,27 @@ unsigned long kvm_mips_translate_guest_kseg0_to_hpa(struct kvm_vcpu *vcpu,
 	return (kvm->arch.guest_pmap[gfn] << PAGE_SHIFT) + offset;
 }
 
+static pte_t *kvm_trap_emul_pte_for_gva(struct kvm_vcpu *vcpu,
+					unsigned long addr)
+{
+	struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
+	pgd_t *pgdp;
+	int ret;
+
+	/* We need a minimum of cached pages ready for page table creation */
+	ret = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES,
+				     KVM_NR_MEM_OBJS);
+	if (ret)
+		return NULL;
+
+	if (KVM_GUEST_KERNEL_MODE(vcpu))
+		pgdp = vcpu->arch.guest_kernel_mm.pgd;
+	else
+		pgdp = vcpu->arch.guest_user_mm.pgd;
+
+	return kvm_mips_walk_pgd(pgdp, memcache, addr);
+}
+
 void kvm_trap_emul_invalidate_gva(struct kvm_vcpu *vcpu, unsigned long addr,
 				  bool user)
 {
@@ -316,10 +364,8 @@ int kvm_mips_handle_kseg0_tlb_fault(unsigned long badvaddr,
 	gfn_t gfn;
 	kvm_pfn_t pfn0, pfn1;
 	unsigned long vaddr = 0;
-	unsigned long entryhi = 0, entrylo0 = 0, entrylo1 = 0;
 	struct kvm *kvm = vcpu->kvm;
-	const int flush_dcache_mask = 0;
-	int ret;
+	pte_t *ptep_gva;
 
 	if (KVM_GUEST_KSEGX(badvaddr) != KVM_GUEST_KSEG0) {
 		kvm_err("%s: Invalid BadVaddr: %#lx\n", __func__, badvaddr);
@@ -327,6 +373,8 @@ int kvm_mips_handle_kseg0_tlb_fault(unsigned long badvaddr,
 		return -1;
 	}
 
+	/* Find host PFNs */
+
 	gfn = (KVM_GUEST_CPHYSADDR(badvaddr) >> PAGE_SHIFT);
 	if ((gfn | 1) >= kvm->arch.guest_pmap_npages) {
 		kvm_err("%s: Invalid gfn: %#llx, BadVaddr: %#lx\n", __func__,
@@ -345,20 +393,21 @@ int kvm_mips_handle_kseg0_tlb_fault(unsigned long badvaddr,
 	pfn0 = kvm->arch.guest_pmap[gfn & ~0x1];
 	pfn1 = kvm->arch.guest_pmap[gfn | 0x1];
 
-	entrylo0 = mips3_paddr_to_tlbpfn(pfn0 << PAGE_SHIFT) |
-		((_page_cachable_default >> _CACHE_SHIFT) << ENTRYLO_C_SHIFT) |
-		ENTRYLO_D | ENTRYLO_V;
-	entrylo1 = mips3_paddr_to_tlbpfn(pfn1 << PAGE_SHIFT) |
-		((_page_cachable_default >> _CACHE_SHIFT) << ENTRYLO_C_SHIFT) |
-		ENTRYLO_D | ENTRYLO_V;
+	/* Find GVA page table entry */
 
-	preempt_disable();
-	entryhi = (vaddr | kvm_mips_get_kernel_asid(vcpu));
-	ret = kvm_mips_host_tlb_write(vcpu, entryhi, entrylo0, entrylo1,
-				      flush_dcache_mask);
-	preempt_enable();
+	ptep_gva = kvm_trap_emul_pte_for_gva(vcpu, vaddr);
+	if (!ptep_gva) {
+		kvm_err("No ptep for gva %lx\n", vaddr);
+		return -1;
+	}
 
-	return ret;
+	/* Write host PFNs into GVA page table */
+	ptep_gva[0] = pte_mkyoung(pte_mkdirty(pfn_pte(pfn0, PAGE_SHARED)));
+	ptep_gva[1] = pte_mkyoung(pte_mkdirty(pfn_pte(pfn1, PAGE_SHARED)));
+
+	/* Invalidate this entry in the TLB, guest kernel ASID only */
+	kvm_mips_host_tlb_inv(vcpu, vaddr, false, true);
+	return 0;
 }
 
 int kvm_mips_handle_mapped_seg_tlb_fault(struct kvm_vcpu *vcpu,
-- 
git-series 0.8.10

  parent reply	other threads:[~2017-02-02 12:05 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-02 12:04 [PATCH v2 0/30] KVM: MIPS: Implement GVA page tables James Hogan
2017-02-02 12:04 ` James Hogan
2017-02-02 12:04 ` [PATCH v2 1/30] MIPS: Move pgd_alloc() out of header James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 2/30] MIPS: Export pgd/pmd symbols for KVM James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 3/30] MIPS: uasm: Add include guards in asm/uasm.h James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 4/30] MIPS: Export some tlbex internals for KVM to use James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 5/30] KVM: MIPS: Drop partial KVM_NMI implementation James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 6/30] KVM: MIPS/MMU: Simplify ASID restoration James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 7/30] KVM: MIPS: Convert get/set_regs -> vcpu_load/put James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 8/30] KVM: MIPS/MMU: Move preempt/ASID handling to implementation James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 9/30] KVM: MIPS: Remove duplicated ASIDs from vcpu James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 10/30] KVM: MIPS: Add vcpu_run() & vcpu_reenter() callbacks James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 11/30] KVM: MIPS/T&E: Restore host asid on return to host James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 12/30] KVM: MIPS/T&E: active_mm = init_mm in guest context James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 13/30] KVM: MIPS: Wire up vcpu uninit James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 14/30] KVM: MIPS/T&E: Allocate GVA -> HPA page tables James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 15/30] KVM: MIPS/T&E: Activate GVA page tables in guest context James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 16/30] KVM: MIPS: Support NetLogic KScratch registers James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 17/30] KVM: MIPS: Add fast path TLB refill handler James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 18/30] KVM: MIPS/TLB: Fix off-by-one in TLB invalidate James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 19/30] KVM: MIPS/TLB: Generalise host TLB invalidate to kernel ASID James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 20/30] KVM: MIPS/MMU: Invalidate GVA PTs on ASID changes James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 21/30] KVM: MIPS/MMU: Invalidate stale GVA PTEs on TLBW James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` James Hogan [this message]
2017-02-02 12:04   ` [PATCH v2 22/30] KVM: MIPS/MMU: Convert KSeg0 faults to page tables James Hogan
2017-02-02 12:04 ` [PATCH v2 23/30] KVM: MIPS/MMU: Convert TLB mapped " James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 24/30] KVM: MIPS/MMU: Convert commpage fault handling " James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 25/30] KVM: MIPS: Drop vm_init() callback James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 26/30] KVM: MIPS: Use uaccess to read/modify guest instructions James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 27/30] KVM: MIPS/Emulate: Fix CACHE emulation for EVA hosts James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 28/30] KVM: MIPS/TLB: Drop kvm_local_flush_tlb_all() James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 29/30] KVM: MIPS/Emulate: Drop redundant TLB flushes on exceptions James Hogan
2017-02-02 12:04   ` James Hogan
2017-02-02 12:04 ` [PATCH v2 30/30] KVM: MIPS/MMU: Drop kvm_get_new_mmu_context() James Hogan
2017-02-02 12:04   ` James Hogan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a9411989d9230885f1e0a4f21f73c7e7d9036972.1486036366.git-series.james.hogan@imgtec.com \
    --to=james.hogan@imgtec.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-mips@linux-mips.org \
    --cc=pbonzini@redhat.com \
    --cc=ralf@linux-mips.org \
    --cc=rkrcmar@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).