[RFC KVM 11/27] kvm/isolation: add KVM page table entry offset functions
From: Alexandre Chartre
Date: Mon May 13 2019 - 10:41:27 EST
These functions are wrappers are the p4d/pud/pmd/pte offset functions
which ensure that page table pointers are in the KVM page table.
Signed-off-by: Alexandre Chartre <alexandre.chartre@xxxxxxxxxx>
---
arch/x86/kvm/isolation.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 61 insertions(+), 0 deletions(-)
diff --git a/arch/x86/kvm/isolation.c b/arch/x86/kvm/isolation.c
index 61df750..b29a09b 100644
--- a/arch/x86/kvm/isolation.c
+++ b/arch/x86/kvm/isolation.c
@@ -162,6 +162,67 @@ static bool kvm_valid_pgt_entry(void *ptr)
}
/*
+ * kvm_pXX_offset() functions are equivalent to kernel pXX_offset()
+ * functions but, in addition, they ensure that page table pointers
+ * are in the KVM page table. Otherwise an error is returned.
+ */
+
+static pte_t *kvm_pte_offset(pmd_t *pmd, unsigned long addr)
+{
+ pte_t *pte;
+
+ pte = pte_offset_map(pmd, addr);
+ if (!kvm_valid_pgt_entry(pte)) {
+ pr_err("PTE %px is not in KVM page table\n", pte);
+ return ERR_PTR(-EINVAL);
+ }
+
+ return pte;
+}
+
+static pmd_t *kvm_pmd_offset(pud_t *pud, unsigned long addr)
+{
+ pmd_t *pmd;
+
+ pmd = pmd_offset(pud, addr);
+ if (!kvm_valid_pgt_entry(pmd)) {
+ pr_err("PMD %px is not in KVM page table\n", pmd);
+ return ERR_PTR(-EINVAL);
+ }
+
+ return pmd;
+}
+
+static pud_t *kvm_pud_offset(p4d_t *p4d, unsigned long addr)
+{
+ pud_t *pud;
+
+ pud = pud_offset(p4d, addr);
+ if (!kvm_valid_pgt_entry(pud)) {
+ pr_err("PUD %px is not in KVM page table\n", pud);
+ return ERR_PTR(-EINVAL);
+ }
+
+ return pud;
+}
+
+static p4d_t *kvm_p4d_offset(pgd_t *pgd, unsigned long addr)
+{
+ p4d_t *p4d;
+
+ p4d = p4d_offset(pgd, addr);
+ /*
+ * p4d is the same has pgd if we don't have a 5-level page table.
+ */
+ if ((p4d != (p4d_t *)pgd) && !kvm_valid_pgt_entry(p4d)) {
+ pr_err("P4D %px is not in KVM page table\n", p4d);
+ return ERR_PTR(-EINVAL);
+ }
+
+ return p4d;
+}
+
+/*
* kvm_pXX_free() functions are equivalent to kernel pXX_free()
* functions but they can be used with any PXX pointer in the
* directory.
--
1.7.1