kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christoffer Dall <c.dall@virtualopensystems.com>
To: android-virt@lists.cs.columbia.edu, kvm@vger.kernel.org
Cc: Marc.Zyngier@arm.com, catalin.marinas@arm.com,
	tech@virtualopensystems.com, avi@redhat.com,
	peter.maydell@linaro.org
Subject: [PATCH v5 08/13] ARM: KVM: Handle guest faults in KVM
Date: Sun, 11 Dec 2011 05:25:09 -0500	[thread overview]
Message-ID: <20111211102509.21693.23709.stgit@localhost> (raw)
In-Reply-To: <20111211102403.21693.6887.stgit@localhost>

From: Christoffer Dall <cdall@cs.columbia.edu>

Handles the guest faults in KVM by mapping in corresponding user pages
in the 2nd stage page tables.

Introduces new ARM-specific kernel memory types, PAGE_KVM_GUEST and
pgprot_guest variables used to map 2nd stage memory for KVM guests.

Signed-off-by: Christoffer Dall <c.dall@virtualopensystems.com>
---
 arch/arm/include/asm/pgtable-3level.h |    8 ++
 arch/arm/include/asm/pgtable.h        |    4 +
 arch/arm/kvm/mmu.c                    |  107 ++++++++++++++++++++++++++++++++-
 arch/arm/mm/mmu.c                     |    3 +
 4 files changed, 120 insertions(+), 2 deletions(-)

diff --git a/arch/arm/include/asm/pgtable-3level.h b/arch/arm/include/asm/pgtable-3level.h
index edc3cb9..6dc5331 100644
--- a/arch/arm/include/asm/pgtable-3level.h
+++ b/arch/arm/include/asm/pgtable-3level.h
@@ -104,6 +104,14 @@
  */
 #define L_PGD_SWAPPER		(_AT(pgdval_t, 1) << 55)	/* swapper_pg_dir entry */
 
+/*
+ * 2-nd stage PTE definitions for LPAE.
+ */
+#define L_PTE2_READ		(_AT(pteval_t, 1) << 6)	/* HAP[0] */
+#define L_PTE2_WRITE		(_AT(pteval_t, 1) << 7)	/* HAP[1] */
+#define L_PTE2_NORM_WB		(_AT(pteval_t, 3) << 4)	/* MemAttr[3:2] */
+#define L_PTE2_INNER_WB		(_AT(pteval_t, 3) << 2)	/* MemAttr[1:0] */
+
 #ifndef __ASSEMBLY__
 
 #define pud_none(pud)		(!pud_val(pud))
diff --git a/arch/arm/include/asm/pgtable.h b/arch/arm/include/asm/pgtable.h
index 20025cc..778856b 100644
--- a/arch/arm/include/asm/pgtable.h
+++ b/arch/arm/include/asm/pgtable.h
@@ -76,6 +76,7 @@ extern void __pgd_error(const char *file, int line, pgd_t);
 
 extern pgprot_t		pgprot_user;
 extern pgprot_t		pgprot_kernel;
+extern pgprot_t		pgprot_guest;
 
 #define _MOD_PROT(p, b)	__pgprot(pgprot_val(p) | (b))
 
@@ -89,6 +90,9 @@ extern pgprot_t		pgprot_kernel;
 #define PAGE_KERNEL		_MOD_PROT(pgprot_kernel, L_PTE_XN)
 #define PAGE_KERNEL_EXEC	pgprot_kernel
 #define PAGE_HYP		_MOD_PROT(pgprot_kernel, L_PTE_USER)
+#define PAGE_KVM_GUEST		_MOD_PROT(pgprot_guest, L_PTE2_READ | \
+					  L_PTE2_WRITE | L_PTE2_NORM_WB | \
+					  L_PTE2_INNER_WB)
 
 #define __PAGE_NONE		__pgprot(_L_PTE_DEFAULT | L_PTE_RDONLY | L_PTE_XN)
 #define __PAGE_SHARED		__pgprot(_L_PTE_DEFAULT | L_PTE_USER | L_PTE_XN)
diff --git a/arch/arm/kvm/mmu.c b/arch/arm/kvm/mmu.c
index f7a7b17..d468238 100644
--- a/arch/arm/kvm/mmu.c
+++ b/arch/arm/kvm/mmu.c
@@ -229,8 +229,111 @@ void kvm_free_stage2_pgd(struct kvm *kvm)
 	kvm->arch.pgd = NULL;
 }
 
+static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
+			  gfn_t gfn, struct kvm_memory_slot *memslot)
+{
+	pfn_t pfn;
+	pgd_t *pgd;
+	pud_t *pud;
+	pmd_t *pmd;
+	pte_t *pte, new_pte;
+
+	pfn = gfn_to_pfn(vcpu->kvm, gfn);
+
+	if (is_error_pfn(pfn)) {
+		kvm_err(-EFAULT, "Guest gfn %u (0x%08lx) does not have "
+				"corresponding host mapping",
+				gfn, gfn << PAGE_SHIFT);
+		return -EFAULT;
+	}
+
+	/* Create 2nd stage page table mapping - Level 1 */
+	pgd = vcpu->kvm->arch.pgd + pgd_index(fault_ipa);
+	pud = pud_offset(pgd, fault_ipa);
+	if (pud_none(*pud)) {
+		pmd = pmd_alloc_one(NULL, fault_ipa);
+		if (!pmd) {
+			kvm_err(-ENOMEM, "Cannot allocate 2nd stage pmd");
+			return -ENOMEM;
+		}
+		pud_populate(NULL, pud, pmd);
+		pmd += pmd_index(fault_ipa);
+	} else
+		pmd = pmd_offset(pud, fault_ipa);
+
+	/* Create 2nd stage page table mapping - Level 2 */
+	if (pmd_none(*pmd)) {
+		pte = pte_alloc_one_kernel(NULL, fault_ipa);
+		if (!pte) {
+			kvm_err(-ENOMEM, "Cannot allocate 2nd stage pte");
+			return -ENOMEM;
+		}
+		pmd_populate_kernel(NULL, pmd, pte);
+		pte += pte_index(fault_ipa);
+	} else
+		pte = pte_offset_kernel(pmd, fault_ipa);
+
+	/* Create 2nd stage page table mapping - Level 3 */
+	new_pte = pfn_pte(pfn, PAGE_KVM_GUEST);
+	set_pte_ext(pte, new_pte, 0);
+
+	return 0;
+}
+
+#define HSR_ABT_FS	(0x3f)
+#define HPFAR_MASK	(~0xf)
+
+/**
+ * kvm_handle_guest_abort - handles all 2nd stage aborts
+ * @vcpu:	the VCPU pointer
+ * @run:	the kvm_run structure
+ *
+ * Any abort that gets to the host is almost guaranteed to be caused by a
+ * missing second stage translation table entry, which can mean that either the
+ * guest simply needs more memory and we must allocate an appropriate page or it
+ * can mean that the guest tried to access I/O memory, which is emulated by user
+ * space. The distinction is based on the IPA causing the fault and whether this
+ * memory region has been registered as standard RAM by user space.
+ */
 int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
 {
-	KVMARM_NOT_IMPLEMENTED();
-	return -EINVAL;
+	unsigned long hsr_ec;
+	unsigned long fault_status;
+	phys_addr_t fault_ipa;
+	struct kvm_memory_slot *memslot = NULL;
+	bool is_iabt;
+	gfn_t gfn;
+
+	hsr_ec = vcpu->arch.hsr >> HSR_EC_SHIFT;
+	is_iabt = (hsr_ec == HSR_EC_IABT);
+
+	/* Check that the second stage fault is a translation fault */
+	fault_status = vcpu->arch.hsr & HSR_ABT_FS;
+	if ((fault_status & 0x3c) != 0x4) {
+		kvm_err(-EFAULT, "Unsupported fault status: %x",
+				fault_status & 0x3c);
+		return -EFAULT;
+	}
+
+	fault_ipa = ((phys_addr_t)vcpu->arch.hpfar & HPFAR_MASK) << 8;
+
+	gfn = fault_ipa >> PAGE_SHIFT;
+	if (!kvm_is_visible_gfn(vcpu->kvm, gfn)) {
+		if (is_iabt) {
+			kvm_err(-EFAULT, "Inst. abort on I/O address");
+			return -EFAULT;
+		}
+
+		kvm_msg("I/O address abort...");
+		KVMARM_NOT_IMPLEMENTED();
+		return -EINVAL;
+	}
+
+	memslot = gfn_to_memslot(vcpu->kvm, gfn);
+	if (!memslot->user_alloc) {
+		kvm_err(-EINVAL, "non user-alloc memslots not supported");
+		return -EINVAL;
+	}
+
+	return user_mem_abort(vcpu, fault_ipa, gfn, memslot);
 }
diff --git a/arch/arm/mm/mmu.c b/arch/arm/mm/mmu.c
index b836d6b..1aa6e2c 100644
--- a/arch/arm/mm/mmu.c
+++ b/arch/arm/mm/mmu.c
@@ -53,9 +53,11 @@ static unsigned int cachepolicy __initdata = CPOLICY_WRITEBACK;
 static unsigned int ecc_mask __initdata = 0;
 pgprot_t pgprot_user;
 pgprot_t pgprot_kernel;
+pgprot_t pgprot_guest;
 
 EXPORT_SYMBOL(pgprot_user);
 EXPORT_SYMBOL(pgprot_kernel);
+EXPORT_SYMBOL(pgprot_guest);
 
 struct cachepolicy {
 	const char	policy[16];
@@ -503,6 +505,7 @@ static void __init build_mem_type_table(void)
 	pgprot_user   = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | user_pgprot);
 	pgprot_kernel = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG |
 				 L_PTE_DIRTY | kern_pgprot);
+	pgprot_guest  = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG);
 
 	mem_types[MT_LOW_VECTORS].prot_l1 |= ecc_mask;
 	mem_types[MT_HIGH_VECTORS].prot_l1 |= ecc_mask;


  parent reply	other threads:[~2011-12-11 10:25 UTC|newest]

Thread overview: 105+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-11 10:24 [PATCH v5 00/13] KVM/ARM Implementation Christoffer Dall
2011-12-11 10:24 ` [PATCH v5 01/13] ARM: KVM: Initial skeleton to compile KVM support Christoffer Dall
2011-12-11 10:24 ` [PATCH v5 02/13] ARM: KVM: Hypervisor identity mapping Christoffer Dall
2011-12-11 10:24 ` [PATCH v5 03/13] ARM: KVM: Add hypervisor inititalization Christoffer Dall
2011-12-11 10:24 ` [PATCH v5 04/13] ARM: KVM: Memory virtualization setup Christoffer Dall
2011-12-12 14:40   ` Avi Kivity
2011-12-12 15:09     ` [Android-virt] " Christoffer Dall
2011-12-12 15:15       ` Avi Kivity
2011-12-12 15:25         ` Peter Maydell
2011-12-12 15:49           ` Avi Kivity
2011-12-12 17:40             ` Christoffer Dall
2011-12-13 17:10             ` Antonios Motakis
2011-12-13 17:13               ` Christoffer Dall
2011-12-11 10:24 ` [PATCH v5 05/13] ARM: KVM: Inject IRQs and FIQs from userspace Christoffer Dall
2011-12-11 15:18   ` Jan Kiszka
2011-12-11 16:03     ` Peter Maydell
2011-12-11 19:30       ` Christoffer Dall
2011-12-11 19:48         ` Peter Maydell
2011-12-11 20:07           ` [Android-virt] " Christoffer Dall
2011-12-11 20:25             ` Peter Maydell
2011-12-11 21:36               ` Christoffer Dall
2011-12-11 22:12                 ` Peter Maydell
2011-12-11 22:35                   ` Peter Maydell
2011-12-11 22:53                     ` Christoffer Dall
2011-12-11 23:01                       ` Jan Kiszka
2011-12-12 16:31                         ` Peter Maydell
2011-12-12 17:40                           ` Avi Kivity
2011-12-29  1:29                             ` Christoffer Dall
2012-02-09  1:15                             ` Peter Maydell
2011-12-12 11:06             ` Marc Zyngier
2011-12-12 12:54               ` Christoffer Dall
2011-12-12  6:35           ` Alexander Graf
2011-12-11 19:16     ` Christoffer Dall
2011-12-12 13:28   ` Avi Kivity
2011-12-12 14:38     ` [Android-virt] " Christoffer Dall
2011-12-12 14:50       ` Avi Kivity
2011-12-12 15:11         ` Christoffer Dall
2011-12-12 15:16           ` Avi Kivity
2011-12-11 10:24 ` [PATCH v5 06/13] ARM: KVM: World-switch implementation Christoffer Dall
2011-12-11 10:25 ` [PATCH v5 07/13] ARM: KVM: Emulation framework and CP15 emulation Christoffer Dall
2011-12-12 13:44   ` Avi Kivity
2011-12-12 16:17     ` Christoffer Dall
2011-12-11 10:25 ` Christoffer Dall [this message]
2011-12-12 15:05   ` [PATCH v5 08/13] ARM: KVM: Handle guest faults in KVM Avi Kivity
2011-12-12 19:53     ` Christoffer Dall
2011-12-13  9:45       ` Avi Kivity
2011-12-13 13:10         ` [Android-virt] " Christoffer Dall
2011-12-13 13:17           ` Marc Zyngier
2011-12-13 13:23           ` Avi Kivity
2011-12-13 13:44             ` Christoffer Dall
2011-12-13 14:27               ` Avi Kivity
2011-12-11 10:25 ` [PATCH v5 09/13] ARM: KVM: Handle I/O aborts Christoffer Dall
2011-12-12 13:54   ` Avi Kivity
2011-12-12 14:56     ` [Android-virt] " Christoffer Dall
2011-12-11 10:25 ` [PATCH v5 10/13] ARM: KVM: Guest wait-for-interrupts (WFI) support Christoffer Dall
2011-12-12 14:12   ` Avi Kivity
2011-12-12 16:20     ` Christoffer Dall
2011-12-12 17:44       ` Avi Kivity
2011-12-12 19:21         ` [Android-virt] " Christoffer Dall
2011-12-13  9:41           ` Avi Kivity
2011-12-11 10:25 ` [PATCH v5 11/13] ARM: KVM: Support SMP hosts Christoffer Dall
2011-12-12 14:30   ` Avi Kivity
2011-12-12 17:37     ` Christoffer Dall
2011-12-12 17:56       ` Avi Kivity
2011-12-12 19:38         ` [Android-virt] " Christoffer Dall
     [not found]         ` <CAEDV+gJ=zeDpfp0kS2uBvmgRMyCpsV1LitjKR66R4W9Y3VGgWw@mail.gmail.com>
     [not found]           ` <4EE71CF1.5080705@redhat.com>
2011-12-13 13:36             ` Christoffer Dall
2011-12-13 14:17               ` Avi Kivity
2011-12-13 14:36                 ` Christoffer Dall
2011-12-13 14:17               ` Marc Zyngier
2011-12-19  6:15   ` Antonios Motakis
2011-12-19 14:57     ` [Android-virt] " Christoffer Dall
2011-12-19 15:19       ` Marc Zyngier
2011-12-19 15:30         ` Antonios Motakis
2011-12-19 15:37           ` Marc Zyngier
2011-12-19 15:40             ` Christoffer Dall
2011-12-19 15:42               ` Antonios Motakis
2011-12-19 15:45                 ` Marc Zyngier
     [not found]                   ` <CAEDV+gL929Hpa=PncVWeHRNAa5fBuorNNYFC=iix=PO+5aO2cg@mail.gmail.com>
2011-12-19 17:19                     ` Peter Maydell
2011-12-19 17:24                       ` Christoffer Dall
2011-12-19 17:36                         ` Peter Maydell
2011-12-19 17:40                           ` Christoffer Dall
2011-12-11 10:25 ` [PATCH v5 12/13] ARM: KVM: Fix guest view of MPIDR Christoffer Dall
2011-12-12 14:32   ` Avi Kivity
2011-12-12 17:39     ` Christoffer Dall
2011-12-12 17:44       ` Marc Zyngier
2011-12-12 19:43         ` Christoffer Dall
2011-12-13  9:46           ` Avi Kivity
2011-12-13 13:38             ` Christoffer Dall
2011-12-11 10:25 ` [PATCH v5 13/13] ARM: KVM: Support SMP guests Christoffer Dall
2011-12-11 11:32 ` [PATCH v5 00/13] KVM/ARM Implementation Peter Maydell
2011-12-11 19:23   ` Christoffer Dall
2011-12-11 19:27     ` Peter Maydell
2012-01-11 16:48     ` Peter Maydell
2012-01-12  3:29       ` Christoffer Dall
2012-01-12  8:19         ` Peter Maydell
2012-01-12 16:15           ` [Android-virt] " Christoffer Dall
2012-01-20  2:59             ` Christoffer Dall
2012-01-30 22:46               ` Peter Maydell
2012-01-30 23:02                 ` Alexander Graf
2012-01-31 14:39                 ` Antonios Motakis
2012-02-01 12:11                 ` Marc Zyngier
2012-02-01 12:20                   ` Peter Maydell
2012-02-01 13:40                     ` Marc Zyngier
2012-02-01 13:57                       ` Peter Maydell
2012-02-01 13:59                       ` Christoffer Dall

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=20111211102509.21693.23709.stgit@localhost \
    --to=c.dall@virtualopensystems.com \
    --cc=Marc.Zyngier@arm.com \
    --cc=android-virt@lists.cs.columbia.edu \
    --cc=avi@redhat.com \
    --cc=catalin.marinas@arm.com \
    --cc=kvm@vger.kernel.org \
    --cc=peter.maydell@linaro.org \
    --cc=tech@virtualopensystems.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).