Skip to contentSkip to conversation

Nested Virtualiztion: hap-on-hap

PATCH In review Christoph Egger 3 participants 9 messages Sep 1, 2010 – Sep 13, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 13/13 Nested Virtualiztion: hap-on-hap

Signature · 6 lines
---to satisfy European Law for business letters:
Advanced Micro Devices GmbH
Einsteinring 24, 85609 Dornach b. Muenchen
Geschaeftsfuehrer: Alberto Bozzo, Andrew Bowd
Sitz: Dornach, Gemeinde Aschheim, Landkreis Muenchen
Registergericht Muenchen, HRB Nr. 43632

# HG changeset patch
# User cegger
# Date 1283345895 -7200
Implement Nested-on-Nested.
This allows the guest to run nested guest with hap enabled.

xen/arch/x86/hvm/hvm.c +72 -7
diff -r 50b3e6c73d7c -r 24daea64bdff xen/arch/x86/hvm/hvm.c
@@ -1042,12 +1042,56 @@ void hvm_inject_exception(unsigned int t
     hvm_funcs.inject_exception(trapnr, errcode, cr2);
 }
 
-bool_t hvm_hap_nested_page_fault(unsigned long gfn)
+bool_t hvm_hap_nested_page_fault(paddr_t gpa, struct cpu_user_regs *regs)
 {
     p2m_type_t p2mt;
     mfn_t mfn;
     struct vcpu *v = current;
     struct p2m_domain *p2m = p2m_get_hostp2m(v->domain);
+    unsigned long gfn = gpa >> PAGE_SHIFT;
+    int rv;
+
+    /* On Nested Virtualization, walk the guest page table.
+     * If this succeeds, all is fine.
+     * If this fails, inject a nested page fault into the guest.
+     */
+    if ( nestedhvm_enabled(v->domain)
+        && nestedhvm_vcpu_in_guestmode(v)
+        && nestedhvm_paging_mode_hap(v) )
+    {
+        enum nestedhvm_vmexits nsret;
+        struct nestedhvm *hvm = &vcpu_nestedhvm(v);
+
+        /* nested guest gpa == guest gva */
+        rv = nestedhvm_hap_nested_page_fault(v, gpa);
+        switch (rv) {
+        case NESTEDHVM_PAGEFAULT_DONE:
+            return 1;
+        case NESTEDHVM_PAGEFAULT_ERROR:
+            return 0;
+        case NESTEDHVM_PAGEFAULT_INJECT:
+            break;
+        }
+
+        /* inject #VMEXIT(NPF) into guest. */
+        hvm->nh_forcevmexit.exitcode = NESTEDHVM_INTERCEPT_NPF;
+        hvm->nh_forcevmexit.exitinfo1 = regs->error_code;
+        hvm->nh_forcevmexit.exitinfo2 = gpa;
+        hvm->nh_hostflags.fields.forcevmexit = 1;
+        nsret = nestedhvm_vcpu_vmexit(v, regs, NESTEDHVM_INTERCEPT_NPF);
+        hvm->nh_hostflags.fields.forcevmexit = 0;
+        switch (nsret) {
+        case NESTEDHVM_VMEXIT_DONE:
+        case NESTEDHVM_VMEXIT_ERROR: /* L1 guest will crash L2 guest */
+            return 1;
+        case NESTEDHVM_VMEXIT_HOST:
+        case NESTEDHVM_VMEXIT_CONTINUE:
+        case NESTEDHVM_VMEXIT_FATALERROR:
+        default:
+            gdprintk(XENLOG_ERR, "unexpected nestedhvm error %i\n", nsret);
+            return 0;
+        }
+    }
 
     mfn = gfn_to_mfn_guest(p2m, gfn, &p2mt);
 
@@ -1125,6 +1169,15 @@ int hvm_set_efer(uint64_t value)
         return X86EMUL_EXCEPTION;
     }
 
+    if ( nestedhvm_enabled(v->domain) && cpu_has_svm &&
+       ((value & EFER_SVME) == 0 ) &&
+       ((value ^ v->arch.hvm_vcpu.guest_efer) & EFER_SVME) )
+    {
+        /* Cleared EFER.SVME: Flush all nestedp2m tables */
+        p2m_flush_nestedp2m(v->domain);
+        nestedhvm_vcpu_reset(v);
+    }
+
     value |= v->arch.hvm_vcpu.guest_efer & EFER_LMA;
     v->arch.hvm_vcpu.guest_efer = value;
     hvm_update_guest_efer(v);
@@ -1278,8 +1331,12 @@ int hvm_set_cr0(unsigned long value)
     v->arch.hvm_vcpu.guest_cr[0] = value;
     hvm_update_guest_cr(v, 0);
 
-    if ( (value ^ old_value) & X86_CR0_PG )
-        paging_update_paging_modes(v);
+    if ( (value ^ old_value) & X86_CR0_PG ) {
+        if ( !nestedhvm_vmentry_emulate(v) && nestedhvm_vcpu_in_guestmode(v) )
+            paging_update_nestedmode(v);
+        else
+            paging_update_paging_modes(v);
+    }
 
     return X86EMUL_OKAY;
 
@@ -1346,8 +1403,12 @@ int hvm_set_cr4(unsigned long value)
     hvm_update_guest_cr(v, 4);
 
     /* Modifying CR4.{PSE,PAE,PGE} invalidates all TLB entries, inc. Global. */
-    if ( (old_cr ^ value) & (X86_CR4_PSE | X86_CR4_PGE | X86_CR4_PAE) )
-        paging_update_paging_modes(v);
+    if ( (old_cr ^ value) & (X86_CR4_PSE | X86_CR4_PGE | X86_CR4_PAE) ) {
+        if ( !nestedhvm_vmentry_emulate(v) && nestedhvm_vcpu_in_guestmode(v) )
+            paging_update_nestedmode(v);
+        else
+            paging_update_paging_modes(v);
+    }
 
     return X86EMUL_OKAY;
 
@@ -1831,11 +1892,15 @@ static enum hvm_copy_result __hvm_copy(
     void *buf, paddr_t addr, int size, unsigned int flags, uint32_t pfec)
 {
     struct vcpu *curr = current;
-    struct p2m_domain *p2m = p2m_get_hostp2m(curr->domain);
+    struct p2m_domain *p2m;
     unsigned long gfn, mfn;
     p2m_type_t p2mt;
     char *p;
     int count, todo = size;
+    unsigned long cr3 = curr->arch.hvm_vcpu.guest_cr[3];
+    const struct paging_mode *mode = paging_get_mode(curr);
+
+    p2m = p2m_get_p2m(curr);
 
     while ( todo > 0 )
     {
@@ -1843,7 +1908,7 @@ static enum hvm_copy_result __hvm_copy(
 
         if ( flags & HVMCOPY_virt )
         {
-            gfn = paging_gva_to_gfn(curr, addr, &pfec);
+            gfn = paging_p2m_ga_to_gfn(curr, p2m, mode, cr3, addr, &pfec);
             if ( gfn == INVALID_GFN )
             {
                 if ( pfec == PFEC_page_paged )
xen/arch/x86/hvm/nestedhvm.c +19 -1
diff -r 50b3e6c73d7c -r 24daea64bdff xen/arch/x86/hvm/nestedhvm.c
@@ -20,6 +20,7 @@
 #include <asm/msr.h>
 #include <asm/hvm/support.h>	/* for HVM_DELIVER_NO_ERROR_CODE */
 #include <asm/hvm/hvm.h>
+#include <asm/p2m.h>    /* for struct p2m_domain */
 #include <asm/hvm/nestedhvm.h>
 #include <asm/event.h>  /* for local_event_delivery_(en|dis)able */
 #include <asm/paging.h> /* for paging_mode_hap() */
@@ -477,6 +478,7 @@ nestedhvm_vcpu_vmexit(struct vcpu *v, st
 	enum hvm_copy_result hvm_rc;
 
 	hvm->nh_hostflags.fields.vmentry = 1;
+	paging_update_nestedmode(v);
 	if (nestedhvm_vcpu_in_guestmode(v)) {
 		ret = nestedhvm_vmexit(v, regs, exitcode);
 		switch (ret) {
@@ -529,14 +531,30 @@ nestedhvm_vcpu_vmexit(struct vcpu *v, st
 void
 nestedhvm_vcpu_enter_guestmode(struct vcpu *v)
 {
+	struct p2m_domain *p2m;
 	vcpu_nestedhvm(v).nh_guestmode = 1;
+
+	p2m = vcpu_nestedhvm(v).nh_p2m;
+	if (p2m == NULL)
+		/* p2m has either been invalidated or not yet assigned. */
+		return;
+
+	cpu_set(v->processor, p2m->p2m_dirty_cpumask);
 }
 
 void
 nestedhvm_vcpu_exit_guestmode(struct vcpu *v)
 {
+	struct p2m_domain *p2m;
 	vcpu_nestedhvm(v).nh_guestmode = 0;
-} 
+
+	p2m = vcpu_nestedhvm(v).nh_p2m;
+	if (p2m == NULL)
+		/* p2m has either been invalidated or not yet assigned. */
+		return;
+
+	cpu_clear(v->processor, p2m->p2m_dirty_cpumask);
+}
 
 /* Virtual GIF */
 int
xen/arch/x86/hvm/svm/svm.c +16 -7
diff -r 50b3e6c73d7c -r 24daea64bdff xen/arch/x86/hvm/svm/svm.c
@@ -1597,6 +1597,10 @@ static int nsvm_vmcb_prepare4vmrun(struc
     /* Nested paging mode */
     if (nestedhvm_paging_mode_hap(v)) {
         /* host nested paging + guest nested paging. */
+        host_vmcb->np_enable = 1;
+
+        host_vmcb->h_cr3 =
+            pagetable_get_paddr(p2m_get_pagetable(p2m_get_nestedp2m(v, ns_vmcb->h_cr3)));
 
         /* hvm_set_cr3() below sets v->arch.hvm_vcpu.guest_cr[3] for us. */
         rc = hvm_set_cr3(ns_vmcb->cr3);
@@ -1978,14 +1982,16 @@ struct hvm_function_table * __init start
     return &svm_function_table;
 }
 
-static void svm_do_nested_pgfault(paddr_t gpa)
+static void svm_do_nested_pgfault(struct vcpu *v,
+    struct cpu_user_regs *regs, paddr_t gpa)
 {
+    int ret;
     unsigned long gfn = gpa >> PAGE_SHIFT;
     mfn_t mfn;
     p2m_type_t p2mt;
-    struct p2m_domain *p2m;
-
-    p2m = p2m_get_hostp2m(current->domain);
+    struct p2m_domain *p2m = NULL;
+
+    ret = hvm_hap_nested_page_fault(gpa, regs);
 
     if ( tb_init_done )
     {
@@ -1996,6 +2002,7 @@ static void svm_do_nested_pgfault(paddr_
             uint32_t p2mt;
         } _d;
 
+        p2m = p2m_get_p2m(v);
         _d.gpa = gpa;
         _d.qualification = 0;
         _d.mfn = mfn_x(gfn_to_mfn_query(p2m, gfn, &_d.p2mt));
@@ -2003,14 +2010,16 @@ static void svm_do_nested_pgfault(paddr_
         __trace_var(TRC_HVM_NPF, 0, sizeof(_d), (unsigned char *)&_d);
     }
 
-    if ( hvm_hap_nested_page_fault(gfn) )
+    if ( ret )
         return;
 
+    if ( p2m == NULL )
+        p2m = p2m_get_p2m(v);
     /* Everything else is an error. */
     mfn = gfn_to_mfn_guest(p2m, gfn, &p2mt);
     gdprintk(XENLOG_ERR, "SVM violation gpa %#"PRIpaddr", mfn %#lx, type %i\n",
              gpa, mfn_x(mfn), p2mt);
-    domain_crash(current->domain);
+    domain_crash(v->domain);
 }
 
 static void svm_fpu_dirty_intercept(void)
@@ -2946,7 +2955,7 @@ asmlinkage void svm_vmexit_handler(struc
     case VMEXIT_NPF:
         perfc_incra(svmexits, VMEXIT_NPF_PERFC);
         regs->error_code = vmcb->exitinfo1;
-        svm_do_nested_pgfault(vmcb->exitinfo2);
+        svm_do_nested_pgfault(v, regs, vmcb->exitinfo2);
         break;
 
     case VMEXIT_IRET:
xen/arch/x86/hvm/vmx/vmx.c +1 -1
diff -r 50b3e6c73d7c -r 24daea64bdff xen/arch/x86/hvm/vmx/vmx.c
@@ -2120,7 +2120,7 @@ static void ept_handle_violation(unsigne
     }
 
     if ( (qualification & EPT_GLA_VALID) &&
-         hvm_hap_nested_page_fault(gfn) )
+         hvm_hap_nested_page_fault(gpa, guest_cpu_user_regs()) )
         return;
 
     /* Everything else is an error. */
xen/arch/x86/mm/hap/Makefile +1 -0
diff -r 50b3e6c73d7c -r 24daea64bdff xen/arch/x86/mm/hap/Makefile
@@ -3,6 +3,7 @@ obj-y += guest_walk_2level.o
 obj-y += guest_walk_3level.o
 obj-y += guest_walk_4level.o
 obj-y += p2m-ept.o
+obj-y += nested_hap.o
 
 guest_levels  = $(subst level,,$(filter %level,$(subst ., ,$(subst _, ,$(1)))))
 guest_walk_defns = -DGUEST_PAGING_LEVELS=$(call guest_levels,$(1))
xen/arch/x86/mm/hap/guest_walk.c +23 -3
diff -r 50b3e6c73d7c -r 24daea64bdff xen/arch/x86/mm/hap/guest_walk.c
@@ -29,6 +29,9 @@
 #define _hap_gva_to_gfn(levels) hap_gva_to_gfn_##levels##_levels
 #define hap_gva_to_gfn(levels) _hap_gva_to_gfn(levels)
 
+#define _hap_p2m_ga_to_gfn(levels) hap_p2m_ga_to_gfn_##levels##_levels
+#define hap_p2m_ga_to_gfn(levels) _hap_p2m_ga_to_gfn(levels)
+
 #if GUEST_PAGING_LEVELS <= CONFIG_PAGING_LEVELS
 
 #include <asm/guest_pt.h>
@@ -38,15 +41,23 @@ unsigned long hap_gva_to_gfn(GUEST_PAGIN
     struct vcpu *v, unsigned long gva, uint32_t *pfec)
 {
     unsigned long cr3;
+    struct p2m_domain *p2m = p2m_get_hostp2m(v->domain);
+
+    cr3 = v->arch.hvm_vcpu.guest_cr[3];
+    return hap_p2m_ga_to_gfn(GUEST_PAGING_LEVELS)(v, p2m, cr3, gva, pfec);
+}
+
+unsigned long hap_p2m_ga_to_gfn(GUEST_PAGING_LEVELS)(
+    struct vcpu *v, struct p2m_domain *p2m, unsigned long cr3,
+    paddr_t ga, uint32_t *pfec)
+{
     uint32_t missing;
     mfn_t top_mfn;
     void *top_map;
     p2m_type_t p2mt;
     walk_t gw;
-    struct p2m_domain *p2m = p2m_get_hostp2m(v->domain);
 
     /* Get the top-level table's MFN */
-    cr3 = v->arch.hvm_vcpu.guest_cr[3];
     top_mfn = gfn_to_mfn_unshare(p2m, cr3 >> PAGE_SHIFT, &p2mt, 0);
     if ( p2m_is_paging(p2mt) )
     {
@@ -72,7 +83,7 @@ unsigned long hap_gva_to_gfn(GUEST_PAGIN
 #if GUEST_PAGING_LEVELS == 3
     top_map += (cr3 & ~(PAGE_MASK | 31));
 #endif
-    missing = guest_walk_tables(v, p2m, gva, &gw, pfec[0], top_mfn, top_map);
+    missing = guest_walk_tables(v, p2m, ga, &gw, pfec[0], top_mfn, top_map);
     unmap_domain_page(top_map);
 
     /* Interpret the answer */
@@ -119,6 +130,15 @@ unsigned long hap_gva_to_gfn(GUEST_PAGIN
     return INVALID_GFN;
 }
 
+unsigned long hap_p2m_ga_to_gfn(GUEST_PAGING_LEVELS)(
+    struct vcpu *v, struct p2m_domain *p2m, unsigned long cr3,
+    paddr_t ga, uint32_t *pfec)
+{
+    gdprintk(XENLOG_ERR,
+             "Guest paging level is greater than host paging level!\n");
+    domain_crash(v->domain);
+    return INVALID_GFN;
+}
 #endif
 
 
xen/arch/x86/mm/hap/hap.c +73 -6

168 lines not on this page · raw message

xen/arch/x86/mm/hap/nested_hap.c new file +423 -0

423 lines not on this page · raw message

xen/arch/x86/mm/hap/private.h +10 -0
diff -r 50b3e6c73d7c -r 24daea64bdff xen/arch/x86/mm/hap/private.h
@@ -30,4 +30,14 @@ unsigned long hap_gva_to_gfn_3_levels(st
 unsigned long hap_gva_to_gfn_4_levels(struct vcpu *v, unsigned long gva,
                                      uint32_t *pfec);
 
+unsigned long hap_p2m_ga_to_gfn_2_levels(struct vcpu *v,
+    struct p2m_domain *p2m, unsigned long cr3,
+    paddr_t ga, uint32_t *pfec);
+unsigned long hap_p2m_ga_to_gfn_3_levels(struct vcpu *v,
+    struct p2m_domain *p2m, unsigned long cr3,
+    paddr_t ga, uint32_t *pfec);
+unsigned long hap_p2m_ga_to_gfn_4_levels(struct vcpu *v,
+    struct p2m_domain *p2m, unsigned long cr3,
+    paddr_t ga, uint32_t *pfec);
+
 #endif /* __HAP_PRIVATE_H__ */
xen/arch/x86/mm/p2m.c +222 -4

302 lines not on this page · raw message

xen/arch/x86/mm/paging.c +37 -5

64 lines not on this page · raw message

xen/include/asm-x86/domain.h +7 -0
diff -r 50b3e6c73d7c -r 24daea64bdff xen/include/asm-x86/domain.h
@@ -206,6 +206,8 @@ struct paging_domain {
 struct paging_vcpu {
     /* Pointers to mode-specific entry points. */
     const struct paging_mode *mode;
+    /* Nested Virtualization: paging mode of nested guest */
+    const struct paging_mode *nestedmode;
     /* HVM guest: last emulate was to a pagetable */
     unsigned int last_write_was_pt:1;
     /* HVM guest: last write emulation succeeds */
@@ -221,6 +223,7 @@ struct paging_vcpu {
 #define MAX_CPUID_INPUT 40
 typedef xen_domctl_cpuid_t cpuid_input_t;
 
+#define MAX_NESTEDP2M 10
 struct p2m_domain;
 struct time_scale {
     int shift;
@@ -254,6 +257,10 @@ struct arch_domain
     struct paging_domain paging;
     struct p2m_domain *p2m;
 
+    /* nestedhvm: translate l2 guest physical to host physical */
+    struct p2m_domain *nested_p2m[MAX_NESTEDP2M];
+    spinlock_t nested_p2m_lock;
+
     /* NB. protected by d->event_lock and by irq_desc[irq].lock */
     int *irq_pirq;
     int *pirq_irq;
xen/include/asm-x86/hvm/hvm.h +1 -1
diff -r 50b3e6c73d7c -r 24daea64bdff xen/include/asm-x86/hvm/hvm.h
@@ -371,7 +371,7 @@ static inline void hvm_set_info_guest(st
 
 int hvm_debug_op(struct vcpu *v, int32_t op);
 
-bool_t hvm_hap_nested_page_fault(unsigned long gfn);
+bool_t hvm_hap_nested_page_fault(paddr_t gpa, struct cpu_user_regs *regs);
 
 #define hvm_msr_tsc_aux(v) ({                                               \
     struct domain *__d = (v)->domain;                                       \
xen/include/asm-x86/hvm/nestedhvm.h +2 -2
diff -r 50b3e6c73d7c -r 24daea64bdff xen/include/asm-x86/hvm/nestedhvm.h
@@ -99,8 +99,8 @@ enum hvm_copy_result
 nestedhvm_vm_fromguest(struct nestedhvm *hvm, uint64_t vmaddr);
 enum hvm_copy_result
 nestedhvm_vm_toguest(struct nestedhvm *hvm, uint64_t vmaddr);
-#define nestedhvm_vm_flushtlb(d) \
-    flush_tlb_mask(&(d)->arch.hvm_domain.nh_dirty_cpumask)
+#define nestedhvm_vm_flushtlb(p2m) \
+    flush_tlb_mask(&((p2m)->p2m_dirty_cpumask))
 
 /* Nested VCPU */
 int nestedhvm_vcpu_initialise(struct vcpu *v);
xen/include/asm-x86/p2m.h +35 -0
diff -r 50b3e6c73d7c -r 24daea64bdff xen/include/asm-x86/p2m.h
@@ -175,7 +175,14 @@ struct p2m_domain {
     /* VRAM dirty support. */
     struct sh_dirty_vram *dirty_vram;
 
+    /* Same as domain_dirty_cpumask but limited to
+     * this p2m and those physical cpus whose vcpu's are in
+     * guestmode.
+     */
+    cpumask_t          p2m_dirty_cpumask;
+
     struct domain     *domain;   /* back pointer to domain */
+    uint64_t           cr3;      /* to identify this p2m for re-use */
 
     /* Pages used to construct the p2m */
     struct page_list_head pages;
@@ -232,8 +239,26 @@ struct p2m_domain {
 /* get host p2m table */
 #define p2m_get_hostp2m(d)      ((d)->arch.p2m)
 
+/* Get p2m table (re)usable for specified cr3.
+ * Automatically destroys and re-initializes a p2m if none found.
+ * If cr3 == 0 then v->arch.hvm_vcpu.guest_cr[3] is used.
+ */
+struct p2m_domain *p2m_get_nestedp2m(struct vcpu *v, uint64_t cr3);
+
+/* If vcpu is in host mode then behaviour matches p2m_get_hostp2m().
+ * If vcpu is in guest mode then behaviour matches p2m_get_nestedp2m().
+ */
+struct p2m_domain *p2m_get_p2m(struct vcpu *v);
+
+#define p2m_is_nestedp2m(p2m)   ((p2m) != p2m_get_hostp2m((p2m->domain)))
+
 #define p2m_get_pagetable(p2m)  ((p2m)->phys_table)
 
+/* Flushes specified p2m table */
+void p2m_flush(struct vcpu *v, struct p2m_domain *p2m);
+/* Flushes all nested p2m tables */
+void p2m_flush_nestedp2m(struct domain *d);
+
 /*
  * The P2M lock.  This protects all updates to the p2m table.
  * Updates are expected to be safe against concurrent reads,
@@ -375,6 +400,9 @@ static inline unsigned long mfn_to_gfn(s
 /* Init the datastructures for later use by the p2m code */
 int p2m_init(struct domain *d);
 
+/* PTE flags for various types of p2m entry */
+unsigned long p2m_type_to_flags(p2m_type_t t);
+
 /* Allocate a new p2m table for a domain. 
  *
  * The alloc_page and free_page functions will be used to get memory to
@@ -385,6 +413,13 @@ int p2m_alloc_table(struct p2m_domain *p
                struct page_info * (*alloc_page)(struct p2m_domain *p2m),
                void (*free_page)(struct p2m_domain *p2m, struct page_info *pg));
 
+/* Find the next level's P2M entry, checking for out-of-range gfn's...
+ * Returns NULL on error.
+ */
+l1_pgentry_t *
+p2m_find_entry(void *table, unsigned long *gfn_remainder,
+               unsigned long gfn, uint32_t shift, uint32_t max);
+
 /* Return all the p2m resources to Xen. */
 void p2m_teardown(struct p2m_domain *p2m);
 void p2m_final_teardown(struct domain *d);
xen/include/asm-x86/paging.h +42 -12

127 lines not on this page · raw message

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw

Hi,

This is looking better - I think the TLB flushing is almost right. A few more detailed comments below.

Content-Description: xen_nh13_haphap.diff

Quoted 25 lines

# HG changeset patch
# User cegger
# Date 1283345895 -7200
Implement Nested-on-Nested.
This allows the guest to run nested guest with hap enabled.

diff -r 50b3e6c73d7c -r 24daea64bdff xen/arch/x86/hvm/hvm.c

xen/arch/x86/hvm/hvm.c +6 -1
@@ -1042,12 +1042,56 @@ void hvm_inject_exception(unsigned int t
     hvm_funcs.inject_exception(trapnr, errcode, cr2);
 }
 
-bool_t hvm_hap_nested_page_fault(unsigned long gfn)
+bool_t hvm_hap_nested_page_fault(paddr_t gpa, struct cpu_user_regs *regs)
 {
     p2m_type_t p2mt;
     mfn_t mfn;
     struct vcpu *v = current;
     struct p2m_domain *p2m = p2m_get_hostp2m(v->domain);
+    unsigned long gfn = gpa >> PAGE_SHIFT;
+    int rv;
+
+    /* On Nested Virtualization, walk the guest page table.
+     * If this succeeds, all is fine.

xen/arch/x86/hvm/hvm.c void hvm_inject_exception(unsigned int t · line 1065

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10 -0
+     * If this fails, inject a nested page fault into the guest.
+     */
+    if ( nestedhvm_enabled(v->domain)
+        && nestedhvm_vcpu_in_guestmode(v)
+        && nestedhvm_paging_mode_hap(v) )
+    {
+        enum nestedhvm_vmexits nsret;
+        struct nestedhvm *hvm = &vcpu_nestedhvm(v);
+
+        /* nested guest gpa == guest gva */

This comment is confusing to me. The nested guest gpa isn't a virtual address, and certainly not the same as an L1-guest VA. Can you reword it, maybe just to say what the call to nestedhvm_hap_nested_page_fault() is going to do?

Quoted 34 lines
xen/arch/x86/hvm/hvm.c +29 -0
+        rv = nestedhvm_hap_nested_page_fault(v, gpa);
+        switch (rv) {
+        case NESTEDHVM_PAGEFAULT_DONE:
+            return 1;
+        case NESTEDHVM_PAGEFAULT_ERROR:
+            return 0;
+        case NESTEDHVM_PAGEFAULT_INJECT:
+            break;
+        }
+
+        /* inject #VMEXIT(NPF) into guest. */
+        hvm->nh_forcevmexit.exitcode = NESTEDHVM_INTERCEPT_NPF;
+        hvm->nh_forcevmexit.exitinfo1 = regs->error_code;
+        hvm->nh_forcevmexit.exitinfo2 = gpa;
+        hvm->nh_hostflags.fields.forcevmexit = 1;
+        nsret = nestedhvm_vcpu_vmexit(v, regs, NESTEDHVM_INTERCEPT_NPF);
+        hvm->nh_hostflags.fields.forcevmexit = 0;
+        switch (nsret) {
+        case NESTEDHVM_VMEXIT_DONE:
+        case NESTEDHVM_VMEXIT_ERROR: /* L1 guest will crash L2 guest */
+            return 1;
+        case NESTEDHVM_VMEXIT_HOST:
+        case NESTEDHVM_VMEXIT_CONTINUE:
+        case NESTEDHVM_VMEXIT_FATALERROR:
+        default:
+            gdprintk(XENLOG_ERR, "unexpected nestedhvm error %i\n", nsret);
+            return 0;
+        }
+    }
 
     mfn = gfn_to_mfn_guest(p2m, gfn, &p2mt);
 

xen/arch/x86/hvm/hvm.c static enum hvm_copy_result __hvm_copy( · line 1911

Quoted 5 lines
xen/arch/x86/hvm/hvm.c +1 -1
@@ -1843,7 +1908,7 @@ static enum hvm_copy_result __hvm_copy(
 
         if ( flags & HVMCOPY_virt )
         {
-            gfn = paging_gva_to_gfn(curr, addr, &pfec);
+            gfn = paging_p2m_ga_to_gfn(curr, p2m, mode, cr3, addr, &pfec);

Do other callers of paging_gva_to_gfn need to handle nested-npt mode as well? If so, would it be better to update paging_gva_to_gfn() itself?

Quoted 25 lines
xen/arch/x86/hvm/hvm.c +0 -0
             if ( gfn == INVALID_GFN )
             {
                 if ( pfec == PFEC_page_paged )

diff -r 50b3e6c73d7c -r 24daea64bdff xen/arch/x86/hvm/nestedhvm.c

xen/arch/x86/hvm/nestedhvm.c +2 -0
@@ -20,6 +20,7 @@
 #include <asm/msr.h>
 #include <asm/hvm/support.h>	/* for HVM_DELIVER_NO_ERROR_CODE */
 #include <asm/hvm/hvm.h>
+#include <asm/p2m.h>    /* for struct p2m_domain */
 #include <asm/hvm/nestedhvm.h>
 #include <asm/event.h>  /* for local_event_delivery_(en|dis)able */
 #include <asm/paging.h> /* for paging_mode_hap() */
@@ -477,6 +478,7 @@ nestedhvm_vcpu_vmexit(struct vcpu *v, st
 	enum hvm_copy_result hvm_rc;
 
 	hvm->nh_hostflags.fields.vmentry = 1;
+	paging_update_nestedmode(v);
 	if (nestedhvm_vcpu_in_guestmode(v)) {
 		ret = nestedhvm_vmexit(v, regs, exitcode);
 		switch (ret) {
@@ -529,14 +531,30 @@ nestedhvm_vcpu_vmexit(struct vcpu *v, st
 void
 nestedhvm_vcpu_enter_guestmode(struct vcpu *v)

xen/arch/x86/hvm/nestedhvm.c nestedhvm_vcpu_vmexit(struct vcpu *v, st · line 542

Quoted 10 lines
xen/arch/x86/hvm/nestedhvm.c +8 -0
 {
+	struct p2m_domain *p2m;
 	vcpu_nestedhvm(v).nh_guestmode = 1;
+
+	p2m = vcpu_nestedhvm(v).nh_p2m;
+	if (p2m == NULL)
+		/* p2m has either been invalidated or not yet assigned. */
+		return;
+
+	cpu_set(v->processor, p2m->p2m_dirty_cpumask);

Is this arch-specific code? (and likewise the cpu_clear that follows)

Also, in the case where p2m is NULL, when the p2m is allocated later you need to set a bit in p2m->p2m_dirty_cpumask there too.

Quoted 32 lines
xen/arch/x86/hvm/nestedhvm.c +9 -1
 }
 
 void
 nestedhvm_vcpu_exit_guestmode(struct vcpu *v)
 {
+	struct p2m_domain *p2m;
 	vcpu_nestedhvm(v).nh_guestmode = 0;
-} 
+
+	p2m = vcpu_nestedhvm(v).nh_p2m;
+	if (p2m == NULL)
+		/* p2m has either been invalidated or not yet assigned. */
+		return;
+
+	cpu_clear(v->processor, p2m->p2m_dirty_cpumask);
+}
 
 
 

diff -r 50b3e6c73d7c -r 24daea64bdff xen/arch/x86/mm/hap/hap.c

xen/arch/x86/mm/hap/hap.c +1 -0
@@ -40,6 +40,7 @@
 #include <asm/p2m.h>
 #include <asm/domain.h>
 #include <xen/numa.h>
+#include <asm/hvm/nestedhvm.h>
 
 #include "private.h"
 

xen/arch/x86/mm/hap/hap.c static void hap_free_p2m_page(struct p2m · line 347

Quoted 7 lines
xen/arch/x86/mm/hap/hap.c +4 -0
@@ -340,6 +341,30 @@ static void hap_free_p2m_page(struct p2m
     hap_unlock(d);
 }
 
+#define nestedp2m_alloc_p2m_page hap_alloc_p2m_page
+
+/* We must use hap_free() or flushing the nested p2m tables fails
+ * with "freeing ptp fails due to insufficient pages.

That's quite sensible -- other code that frees p2m pages directly to Xen is basically wrong. It's a relic of the shadow allocator, where freeing individual p2m pages to the shadow pool would have been a bit tricky and I knew at the time that p2m pages were only ever freed on destruction.

I really ought to update the shadow p2m-free path now that the shadow pool is simpler. It just needs a little care around the order things happen in teardown.

Quoted 105 lines
xen/arch/x86/mm/hap/hap.c +100 -0
+ * XXX This triggers a bug in p2m that causes a crash in
+ * xen/common/page_alloc.c:1201 on L1 guest shutdown/destroy.
+ */
+static void
+nestedp2m_free_p2m_page(struct p2m_domain *p2m, struct page_info *pg)
+{
+    struct domain *d = p2m->domain;
+    hap_lock(d);
+    ASSERT(page_get_owner(pg) == d);
+    /* Should have just the one ref we gave it in alloc_p2m_page() */
+    BUG_ON((pg->count_info & PGC_count_mask) != 1);
+    pg->count_info = 0;
+    page_set_owner(pg, NULL);
+    hap_free(d, page_to_mfn(pg));
+    d->arch.paging.hap.total_pages++;
+    d->arch.paging.hap.p2m_pages--;
+    ASSERT(d->arch.paging.hap.p2m_pages >= 0);
+    hap_unlock(d);
+}
+
 /* Return the size of the pool, rounded up to the nearest MB */
 static unsigned int
 hap_get_allocation(struct domain *d)
 
 
+static int 
+nestedp2m_next_level(struct p2m_domain *p2m, struct page_info **table_pg, 
+                     void **table, unsigned long *gfn_remainder, 
+                     unsigned long gfn, uint32_t shift, uint32_t max, 
+                     unsigned long type)
+{
+    l1_pgentry_t *l1_entry;
+    l1_pgentry_t *p2m_entry;
+    l1_pgentry_t new_entry;
+    void *next;
+    int i;
+
+    ASSERT(p2m);
+    ASSERT(p2m->alloc_page);
+
+    if ( !(p2m_entry = p2m_find_entry(*table, gfn_remainder, gfn, shift, max)) )
+        return 0;
+
+    if ( !(l1e_get_flags(*p2m_entry) & _PAGE_PRESENT) )
+    {
+        struct page_info *pg;
+
+        pg = p2m_alloc_ptp(p2m, type);
+        if ( pg == NULL )
+            return 0;
+
+        new_entry = l1e_from_pfn(mfn_x(page_to_mfn(pg)),
+                                 __PAGE_HYPERVISOR | _PAGE_USER);
+
+        switch ( type ) {
+        case PGT_l3_page_table:
+            nested_write_p2m_entry(p2m, p2m_entry, new_entry);
+            break;
+        case PGT_l2_page_table:
+#if CONFIG_PAGING_LEVELS == 3
+            /* for PAE mode, PDPE only has PCD/PWT/P bits available */
+            new_entry = l1e_from_pfn(mfn_x(page_to_mfn(pg)), _PAGE_PRESENT);
+#endif
+            nested_write_p2m_entry(p2m, p2m_entry, new_entry);
+            break;
+        case PGT_l1_page_table:
+            nested_write_p2m_entry(p2m, p2m_entry, new_entry);
+            break;
+        default:
+            BUG();
+            break;
+        }
+    }
+
+    ASSERT(l1e_get_flags(*p2m_entry) & (_PAGE_PRESENT|_PAGE_PSE));
+
+    /* split single large page into 4KB page in P2M table */
+    if ( type == PGT_l1_page_table && (l1e_get_flags(*p2m_entry) & _PAGE_PSE) )
+    {
+        unsigned long flags, pfn;
+        struct page_info *pg;
+
+        pg = p2m_alloc_ptp(p2m, PGT_l1_page_table);
+        if ( pg == NULL )
+            return 0;
+
+        /* New splintered mappings inherit the flags of the old superpage, 
+         * with a little reorganisation for the _PAGE_PSE_PAT bit. */
+        flags = l1e_get_flags(*p2m_entry);
+        pfn = l1e_get_pfn(*p2m_entry);
+        if ( pfn & 1 )           /* ==> _PAGE_PSE_PAT was set */
+            pfn -= 1;            /* Clear it; _PAGE_PSE becomes _PAGE_PAT */
+        else
+            flags &= ~_PAGE_PSE; /* Clear _PAGE_PSE (== _PAGE_PAT) */
+        
+        l1_entry = __map_domain_page(pg);
+        for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ )
+        {
+            new_entry = l1e_from_pfn(pfn + i, flags);
+            nested_write_p2m_entry(p2m, l1_entry+i, new_entry);
+        }
+        unmap_domain_page(l1_entry);
+        
+        new_entry = l1e_from_pfn(mfn_x(page_to_mfn(pg)),
+                                 __PAGE_HYPERVISOR|_PAGE_USER);

xen/arch/x86/mm/hap/hap.c static void hap_free_p2m_page(struct p2m

Quoted 10 lines
xen/arch/x86/mm/hap/hap.c +10 -0
+        nested_write_p2m_entry(p2m, p2m_entry, new_entry);
+    }
+
+    *table_pg = l1e_get_page(*p2m_entry);
+    next = __map_domain_page(*table_pg);
+    unmap_domain_page(*table);
+    *table = next;
+
+    return 1;
+}

This function looks like it duplicates a lot of logic from an old version of the normal p2m next-level function. Would it be easy to merge them?

Quoted 89 lines
xen/arch/x86/mm/hap/hap.c +89 -0
+int 
+nestedp2m_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn,
+                    unsigned int page_order, p2m_type_t p2mt);
+
+int 
+nestedp2m_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn,
+                    unsigned int page_order, p2m_type_t p2mt)
+{
+    struct page_info *table_pg;
+    void *table;
+    unsigned long gfn_remainder = gfn;
+    l1_pgentry_t *p2m_entry;
+    l1_pgentry_t entry_content;
+    l2_pgentry_t l2e_content;
+    int rv = 0;
+    
+    ASSERT(p2m);
+    ASSERT(p2m->alloc_page);
+
+    /* address of nested paging table */
+    table_pg = pagetable_get_page(p2m_get_pagetable(p2m));
+    table = __map_domain_page(table_pg);
+
+#if CONFIG_PAGING_LEVELS >= 4
+    if ( !nestedp2m_next_level(p2m, &table_pg, &table, 
+                               &gfn_remainder, gfn, 
+                               L4_PAGETABLE_SHIFT - PAGE_SHIFT,
+                               L4_PAGETABLE_ENTRIES, PGT_l3_page_table) )
+        goto out;
+#endif
+
+    if ( !nestedp2m_next_level(p2m, &table_pg, &table, &gfn_remainder, 
+                               gfn, L3_PAGETABLE_SHIFT - PAGE_SHIFT,
+                               ((CONFIG_PAGING_LEVELS == 3)
+                                ? (paging_mode_hap(p2m->domain) ? 4 : 8)
+                                : L3_PAGETABLE_ENTRIES),
+                               PGT_l2_page_table) )
+        goto out;
+
+    if ( page_order == 0 )
+    {
+        if ( !nestedp2m_next_level(p2m, &table_pg, &table, 
+                                   &gfn_remainder, gfn,
+                                   L2_PAGETABLE_SHIFT - PAGE_SHIFT,
+                                   L2_PAGETABLE_ENTRIES, PGT_l1_page_table) )
+            goto out;
+
+        p2m_entry = p2m_find_entry(table, &gfn_remainder, gfn,
+                                   0, L1_PAGETABLE_ENTRIES);
+        ASSERT(p2m_entry);
+        
+        if ( mfn_valid(mfn) ) {
+            entry_content = l1e_from_pfn(mfn_x(mfn), 
+                                         p2m_type_to_flags(p2mt));
+        } else {
+            entry_content = l1e_empty();
+        }
+        
+        /* level 1 entry */
+        nested_write_p2m_entry(p2m, p2m_entry, entry_content);
+    }
+    else 
+    {
+        p2m_entry = p2m_find_entry(table, &gfn_remainder, gfn,
+                                   L2_PAGETABLE_SHIFT - PAGE_SHIFT,
+                                   L2_PAGETABLE_ENTRIES);
+        ASSERT(p2m_entry);
+        
+        /* FIXME: Deal with 4k replaced by 2MB pages */
+        if ( (l1e_get_flags(*p2m_entry) & _PAGE_PRESENT) &&
+             !(l1e_get_flags(*p2m_entry) & _PAGE_PSE) )
+        {
+            domain_crash(p2m->domain);
+            goto out;
+        }
+        
+        if ( mfn_valid(mfn) )
+            l2e_content = l2e_from_pfn(mfn_x(mfn),
+                p2m_type_to_flags(p2mt) | _PAGE_PSE);
+        else {
+            l2e_content = l2e_empty();
+	}
+        
+        entry_content.l1 = l2e_content.l2;
+        nested_write_p2m_entry(p2m, p2m_entry, entry_content);
+    }
+
+    /* Track the highest gfn for which we have ever had a valid mapping */
+    if ( mfn_valid(mfn)

xen/arch/x86/mm/hap/hap.c

Quoted 10 lines
xen/arch/x86/mm/hap/hap.c +10 -0
+         && (gfn + (1UL << page_order) - 1 > p2m->max_mapped_pfn) )
+        p2m->max_mapped_pfn = gfn + (1UL << page_order) - 1;
+
+    /* Success */
+    rv = 1;
+
+out:
+    unmap_domain_page(table);
+    return rv;
+}

Same for this: looks like it duplicates existing code.

Quoted 4 lines
xen/arch/x86/mm/hap/hap.c +4 -0
+int
+nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t L2_gpa)
+{
+    int rv;

xen/arch/x86/mm/hap/hap.c

Quoted 10 lines
xen/arch/x86/mm/hap/hap.c +10 -0
+    paddr_t L1_gpa, L0_gpa;
+    struct domain *d = v->domain;
+    struct p2m_domain *p2m, *nested_p2m;
+
+    p2m = p2m_get_hostp2m(d); /* L0 p2m */
+    nested_p2m = p2m_get_nestedp2m(v, vcpu_nestedhvm(v).nh_vm_hostcr3);
+
+    /* walk the L1 P2M table, note we have to pass p2m
+     * and not nested_p2m here or we fail the walk forever,
+     * otherwise. */

Can you explain that more fully? It looks like you're walking the L0 table to translate an L2 gfn into an L1 gfn, which surely can't be right.

Quoted 114 lines
xen/arch/x86/mm/hap/hap.c +50 -0
+    rv = nestedhap_walk_L1_p2m(v, p2m, L2_gpa, &L1_gpa);
+
+    /* let caller to handle these two cases */
+    switch (rv) {
+    case NESTEDHVM_PAGEFAULT_INJECT:
+        return rv;
+    case NESTEDHVM_PAGEFAULT_ERROR:
+        return rv;
+    case NESTEDHVM_PAGEFAULT_DONE:
+        break;
+    default:
+        BUG();
+        break;
+    }
+
+    /* ==> we have to walk L0 P2M */
+    rv = nestedhap_walk_L0_p2m(p2m, L1_gpa, &L0_gpa);
+
+    /* let upper level caller to handle these two cases */
+    switch (rv) {
+    case NESTEDHVM_PAGEFAULT_INJECT:
+        return rv;
+    case NESTEDHVM_PAGEFAULT_ERROR:
+        return rv;
+    case NESTEDHVM_PAGEFAULT_DONE:
+        break;
+    default:
+        BUG();
+        break;
+    }
+
+    /* fix p2m_get_pagetable(nested_p2m) */
+    nestedhap_fix_p2m(nested_p2m, L2_gpa, L0_gpa);
+
+    return NESTEDHVM_PAGEFAULT_DONE;
+}
+
+/********************************************/
+/*     NESTED VIRT INITIALIZATION FUNCS     */
+/********************************************/
+
+/*
+ * Local variables:
+ * mode: C
+ * c-set-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */

diff -r 50b3e6c73d7c -r 24daea64bdff xen/arch/x86/mm/hap/private.h

xen/arch/x86/mm/hap/private.h +10 -0
@@ -30,4 +30,14 @@ unsigned long hap_gva_to_gfn_3_levels(st
 unsigned long hap_gva_to_gfn_4_levels(struct vcpu *v, unsigned long gva,
                                      uint32_t *pfec);
 
+unsigned long hap_p2m_ga_to_gfn_2_levels(struct vcpu *v,
+    struct p2m_domain *p2m, unsigned long cr3,
+    paddr_t ga, uint32_t *pfec);
+unsigned long hap_p2m_ga_to_gfn_3_levels(struct vcpu *v,
+    struct p2m_domain *p2m, unsigned long cr3,
+    paddr_t ga, uint32_t *pfec);
+unsigned long hap_p2m_ga_to_gfn_4_levels(struct vcpu *v,
+    struct p2m_domain *p2m, unsigned long cr3,
+    paddr_t ga, uint32_t *pfec);
+
 #endif /* __HAP_PRIVATE_H__ */

diff -r 50b3e6c73d7c -r 24daea64bdff xen/arch/x86/mm/p2m.c

xen/arch/x86/mm/p2m.c +6 -3
@@ -34,6 +34,7 @@
 #include <public/mem_event.h>
 #include <asm/mem_sharing.h>
 #include <xen/event.h>
+#include <asm/hvm/nestedhvm.h>
 
 /* Debugging and auditing of the P2M code? */
 #define P2M_AUDIT     0
@@ -72,7 +73,7 @@ boolean_param("hap_1gb", opt_hap_1gb);
 #define SUPERPAGE_PAGES (1UL << 9)
 #define superpage_aligned(_x)  (((_x)&(SUPERPAGE_PAGES-1))==0)
 
-static unsigned long p2m_type_to_flags(p2m_type_t t) 
+unsigned long p2m_type_to_flags(p2m_type_t t) 
 {
     unsigned long flags;
 #ifdef __x86_64__
@@ -116,9 +117,9 @@ static void audit_p2m(struct p2m_domain 
 // Find the next level's P2M entry, checking for out-of-range gfn's...
 // Returns NULL on error.
 //
-static l1_pgentry_t *
+l1_pgentry_t *
 p2m_find_entry(void *table, unsigned long *gfn_remainder,
-                   unsigned long gfn, u32 shift, u32 max)
+                   unsigned long gfn, uint32_t shift, uint32_t max)
 {
     u32 index;
 
@@ -1719,10 +1720,12 @@ static void p2m_initialise(struct domain
     INIT_PAGE_LIST_HEAD(&p2m->pod.single);
 
     p2m->domain = d;
+    p2m->cr3 = 0;
     p2m->set_entry = p2m_set_entry;
     p2m->get_entry = p2m_gfn_to_mfn;
     p2m->get_entry_current = p2m_gfn_to_mfn_current;
     p2m->change_entry_type_global = p2m_change_type_global;
+    cpus_clear(p2m->p2m_dirty_cpumask);
 
     if ( hap_enabled(d) && (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) )
         ept_p2m_init(d);

xen/arch/x86/mm/p2m.c static void p2m_initialise(struct domain · line 1738

Quoted 6 lines
xen/arch/x86/mm/p2m.c +3 -0
@@ -1730,6 +1733,28 @@ static void p2m_initialise(struct domain
     return;
 }
 
+extern int
+nestedp2m_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn,
+                    unsigned int page_order, p2m_type_t p2mt);

Please define this in a header file.

Quoted 133 lines
xen/arch/x86/mm/p2m.c +85 -1
+static int
+p2m_init_nestedp2m(struct domain *d)
+{
+    uint8_t i;
+    struct p2m_domain *p2m;
+
+    spin_lock_init(&d->arch.nested_p2m_lock);
+    for (i = 0; i < MAX_NESTEDP2M; i++) {
+        d->arch.nested_p2m[i] = p2m = xmalloc(struct p2m_domain);
+        if (p2m == NULL)
+            return -ENOMEM;
+        p2m_initialise(d, p2m);
+        p2m->set_entry = nestedp2m_set_entry;
+    }
+
+    return 0;
+}
+
 int p2m_init(struct domain *d)
 {
     struct p2m_domain *p2m;
@@ -1739,7 +1764,11 @@ int p2m_init(struct domain *d)
         return -ENOMEM;
     p2m_initialise(d, p2m);
 
-    return 0;
+    /* Must initialise nestedp2m unconditionally
+     * since nestedhvm_enabled(d) returns false here.
+     * (p2m_init runs too early for HVM_PARAM_* options)
+     */
+    return p2m_init_nestedp2m(d);
 }
 
 void p2m_change_entry_type_global(struct p2m_domain *p2m,
@@ -1836,6 +1865,9 @@ int p2m_alloc_table(struct p2m_domain *p
                         p2m_invalid) )
         goto error;
 
+    if (p2m_is_nestedp2m(p2m))
+        goto nesteddone;
+
     /* Copy all existing mappings from the page list and m2p */
     spin_lock(&p2m->domain->page_alloc_lock);
     page_list_for_each(page, &p2m->domain->page_list)
@@ -1857,6 +1889,7 @@ int p2m_alloc_table(struct p2m_domain *p
     }
     spin_unlock(&p2m->domain->page_alloc_lock);
 
+ nesteddone:
     P2M_PRINTK("p2m table initialised (%u pages)\n", page_count);
     p2m_unlock(p2m);
     return 0;
@@ -1881,6 +1914,9 @@ void p2m_teardown(struct p2m_domain *p2m
     mfn_t mfn;
 #endif
 
+    if (p2m == NULL)
+        return;
+
     p2m_lock(p2m);
 
 #ifdef __x86_64__
@@ -1899,11 +1935,26 @@ void p2m_teardown(struct p2m_domain *p2m
     p2m_unlock(p2m);
 }
 
+static void p2m_teardown_nestedp2m(struct domain *d)
+{
+    uint8_t i;
+
+    for (i = 0; i < MAX_NESTEDP2M; i++) {
+        xfree(d->arch.nested_p2m[i]);
+        d->arch.nested_p2m[i] = NULL;
+    }
+}
+
 void p2m_final_teardown(struct domain *d)
 {
     /* Iterate over all p2m tables per domain */
     xfree(d->arch.p2m);
     d->arch.p2m = NULL;
+
+    /* We must teardown unconditionally because
+     * we initialise them unconditionally.
+     */
+    p2m_teardown_nestedp2m(d);
 }
 
 #if P2M_AUDIT
@@ -2823,6 +2874,173 @@ void p2m_mem_paging_resume(struct p2m_do
 }
 #endif /* __x86_64__ */
 
+static struct p2m_domain *
+p2m_getlru_nestedp2m(struct domain *d, struct p2m_domain *p2m)
+{
+    int i, lru_index = -1;
+    struct p2m_domain *lrup2m, *tmp;
+
+    if (p2m == NULL) {
+        lru_index = MAX_NESTEDP2M - 1;
+        lrup2m = d->arch.nested_p2m[lru_index];
+    } else {
+        lrup2m = p2m;
+        for (i = 0; i < MAX_NESTEDP2M; i++) {
+            if (d->arch.nested_p2m[i] == p2m) {
+                lru_index = i;
+                break;
+            }
+        }
+    }
+
+    ASSERT(lru_index >= 0);
+    if (lru_index == 0) {
+        return lrup2m;
+    }
+
+    /* move the other's down the array "list" */
+    for (i = lru_index - 1; i >= 0; i--) {
+        tmp = d->arch.nested_p2m[i];
+        d->arch.nested_p2m[i+1] = tmp;        
+    }
+
+    /* make the entry the first one */
+    d->arch.nested_p2m[0] = lrup2m;
+
+    return lrup2m;
+}
+
+static int 
+p2m_flush_locked(struct p2m_domain *p2m)
+{
+    struct page_info * (*alloc)(struct p2m_domain *);

xen/arch/x86/mm/p2m.c void p2m_mem_paging_resume(struct p2m_do · line 2926

Quoted 10 lines
xen/arch/x86/mm/p2m.c +10 -0
+    void (*free)(struct p2m_domain *, struct page_info *);
+
+    alloc = p2m->alloc_page;
+    free = p2m->free_page;
+
+    if (p2m->cr3 == 0)
+        /* Microoptimisation: p2m is already empty.
+         * => about 0.3% speedup of overall system performance.
+         */
+        return 0;

What happens if a malicious guest uses 0 as its actual CR3 value?

Quoted 75 lines
xen/arch/x86/mm/p2m.c +75 -0
+
+    p2m_teardown(p2m);
+    p2m_initialise(p2m->domain, p2m);
+    p2m->set_entry = nestedp2m_set_entry;
+    BUG_ON(p2m_alloc_table(p2m, alloc, free) != 0);
+
+    ASSERT(p2m);
+    ASSERT(p2m->alloc_page);
+    return 0;
+}
+
+void
+p2m_flush(struct vcpu *v, struct p2m_domain *p2m)
+{
+    struct domain *d = p2m->domain;
+
+    nestedhvm_vm_flushtlb(p2m);
+    ASSERT(v->domain == d);
+    vcpu_nestedhvm(v).nh_p2m = NULL;
+    spin_lock(&d->arch.nested_p2m_lock);
+    BUG_ON(p2m_flush_locked(p2m) != 0);
+    spin_unlock(&d->arch.nested_p2m_lock);
+    hvm_asid_flush_vcpu(v);
+}
+
+void
+p2m_flush_nestedp2m(struct domain *d)
+{
+    int i;
+
+    spin_lock(&d->arch.nested_p2m_lock);
+    for (i = 0; i < MAX_NESTEDP2M; i++)
+        BUG_ON(p2m_flush_locked(d->arch.nested_p2m[i]) != 0);
+    spin_unlock(&d->arch.nested_p2m_lock);
+    flush_tlb_mask(&d->domain_dirty_cpumask);
+}
+
+struct p2m_domain *
+p2m_get_nestedp2m(struct vcpu *v, uint64_t cr3)
+{
+    struct nestedhvm *hvm = &vcpu_nestedhvm(v);
+    struct domain *d;
+    struct p2m_domain *p2m;
+    int i, rv;
+
+    if (cr3 == 0)
+        cr3 = v->arch.hvm_vcpu.guest_cr[3];
+
+    if (hvm->nh_flushp2m && hvm->nh_p2m) {
+        nestedhvm_vm_flushtlb(hvm->nh_p2m);
+        hvm->nh_p2m = NULL;
+    }
+
+    d = v->domain;
+    spin_lock(&d->arch.nested_p2m_lock);
+    for (i = 0; i < MAX_NESTEDP2M; i++) {
+        p2m = d->arch.nested_p2m[i];
+        if (p2m->cr3 == cr3 && p2m == hvm->nh_p2m) {
+            p2m_getlru_nestedp2m(d, p2m);
+            if (hvm->nh_flushp2m) {
+               BUG_ON(p2m_flush_locked(p2m) != 0);
+               hvm->nh_flushp2m = 0;
+               hvm_asid_flush_vcpu(v);
+            }
+            p2m->cr3 = cr3;
+            spin_unlock(&d->arch.nested_p2m_lock);
+            return p2m;
+        }
+        if (p2m->cr3 == 0) { /* found unused p2m table */
+            hvm->nh_flushp2m = 0;
+            p2m_getlru_nestedp2m(d, p2m);
+            hvm->nh_p2m = p2m;
+            p2m->cr3 = cr3;
+            spin_unlock(&d->arch.nested_p2m_lock);
+            hvm_asid_flush_vcpu(v);

xen/arch/x86/mm/p2m.c void p2m_mem_paging_resume(struct p2m_do · line 3011

Quoted 10 lines
xen/arch/x86/mm/p2m.c +10 -0
+            return p2m;
+        }
+    }
+
+    /* All p2m's are or were in use. We know the least recent used one.
+     * Destroy and re-initialize it.
+     */
+    for (i = 0; i < MAX_NESTEDP2M; i++) {
+        p2m = p2m_getlru_nestedp2m(d, NULL);
+        rv = p2m_flush_locked(p2m);

Is this enough? If this p2m might be in host_vmcb->h_cr3 somewhere on another vcpu, then you need to make sure that vcpu gets reset not to use it any more.

Cheers,

Tim.

Signature · 3 lines
Tim Deegan <[email protected]>
Principal Software Engineer, XenServer Engineering
Citrix Systems UK Ltd.  (Company #02937203, SL9 0BG)
#2 lore raw
Quoted 39 lines · On Wednesday 08 September 2010 16:04:51 Tim Deegan wrote:

Hi,

This is looking better - I think the TLB flushing is almost right. A few more detailed comments below.

Content-Description: xen_nh13_haphap.diff

Quoted 32 lines

# HG changeset patch
# User cegger
# Date 1283345895 -7200
Implement Nested-on-Nested.
This allows the guest to run nested guest with hap enabled.

diff -r 50b3e6c73d7c -r 24daea64bdff xen/arch/x86/hvm/hvm.c

xen/arch/x86/hvm/hvm.c +13 -1
@@ -1042,12 +1042,56 @@ void hvm_inject_exception(unsigned int t
     hvm_funcs.inject_exception(trapnr, errcode, cr2);
 }
 
-bool_t hvm_hap_nested_page_fault(unsigned long gfn)
+bool_t hvm_hap_nested_page_fault(paddr_t gpa, struct cpu_user_regs
 *regs) {
     p2m_type_t p2mt;
     mfn_t mfn;
     struct vcpu *v = current;
     struct p2m_domain *p2m = p2m_get_hostp2m(v->domain);
+    unsigned long gfn = gpa >> PAGE_SHIFT;
+    int rv;
+
+    /* On Nested Virtualization, walk the guest page table.
+     * If this succeeds, all is fine.
+     * If this fails, inject a nested page fault into the guest.
+     */
+    if ( nestedhvm_enabled(v->domain)
+        && nestedhvm_vcpu_in_guestmode(v)
+        && nestedhvm_paging_mode_hap(v) )
+    {
+        enum nestedhvm_vmexits nsret;
Quoted 8 lines

xen/arch/x86/hvm/hvm.c void hvm_inject_exception(unsigned int t · line 1065

Quoted 3 lines
xen/arch/x86/hvm/hvm.c +3 -0
+        struct nestedhvm *hvm = &vcpu_nestedhvm(v);
+
+        /* nested guest gpa == guest gva */

This comment is confusing to me. The nested guest gpa isn't a virtual address, and certainly not the same as an L1-guest VA. Can you reword it, maybe just to say what the call to nestedhvm_hap_nested_page_fault() is going to do?

New wording is:

/* The vcpu is in guest mdoe and the l1 guest
* uses hap. That means 'gpa' is in l2 guest
* physical adderss space.
* Fix the nested p2m or inject nested page fault
* into l1 guest if not fixable. The algorithm is
* the same as for shadow paging.
*/

Is this fine with you ?

Quoted 36 lines
Quoted 36 lines
xen/arch/x86/hvm/hvm.c +28 -0
+        rv = nestedhvm_hap_nested_page_fault(v, gpa);
+        switch (rv) {
+        case NESTEDHVM_PAGEFAULT_DONE:
+            return 1;
+        case NESTEDHVM_PAGEFAULT_ERROR:
+            return 0;
+        case NESTEDHVM_PAGEFAULT_INJECT:
+            break;
+        }
+
+        /* inject #VMEXIT(NPF) into guest. */
+        hvm->nh_forcevmexit.exitcode = NESTEDHVM_INTERCEPT_NPF;
+        hvm->nh_forcevmexit.exitinfo1 = regs->error_code;
+        hvm->nh_forcevmexit.exitinfo2 = gpa;
+        hvm->nh_hostflags.fields.forcevmexit = 1;
+        nsret = nestedhvm_vcpu_vmexit(v, regs, NESTEDHVM_INTERCEPT_NPF);
+        hvm->nh_hostflags.fields.forcevmexit = 0;
+        switch (nsret) {
+        case NESTEDHVM_VMEXIT_DONE:
+        case NESTEDHVM_VMEXIT_ERROR: /* L1 guest will crash L2 guest */
+            return 1;
+        case NESTEDHVM_VMEXIT_HOST:
+        case NESTEDHVM_VMEXIT_CONTINUE:
+        case NESTEDHVM_VMEXIT_FATALERROR:
+        default:
+            gdprintk(XENLOG_ERR, "unexpected nestedhvm error %i\n",
 nsret); +            return 0;
+        }
+    }
 
     mfn = gfn_to_mfn_guest(p2m, gfn, &p2mt);
@@ -1843,7 +1908,7 @@ static enum hvm_copy_result __hvm_copy(
 
Quoted 8 lines

xen/arch/x86/hvm/hvm.c static enum hvm_copy_result __hvm_copy(

Quoted 5 lines
xen/arch/x86/hvm/hvm.c +1 -1
         if ( flags & HVMCOPY_virt )
         {
-            gfn = paging_gva_to_gfn(curr, addr, &pfec);
+            gfn = paging_p2m_ga_to_gfn(curr, p2m, mode, cr3, addr,
 &pfec);

Do other callers of paging_gva_to_gfn need to handle nested-npt mode as well?

If the other callers can be reached when the vcpu is in guest mode, then yes.

Quoted 1 line

If so, would it be better to update paging_gva_to_gfn() itself?

This is definitely easier but the call to p2m_get_p2m() might become be very expensive and should be cached by passing through per function argument.

Quoted 29 lines
Quoted 29 lines
xen/arch/x86/hvm/hvm.c +0 -0
             if ( gfn == INVALID_GFN )
             {
                 if ( pfec == PFEC_page_paged )

diff -r 50b3e6c73d7c -r 24daea64bdff xen/arch/x86/hvm/nestedhvm.c

xen/arch/x86/hvm/nestedhvm.c +4 -0
@@ -20,6 +20,7 @@
 #include <asm/msr.h>
 #include <asm/hvm/support.h> /* for HVM_DELIVER_NO_ERROR_CODE */
 #include <asm/hvm/hvm.h>
+#include <asm/p2m.h>    /* for struct p2m_domain */
 #include <asm/hvm/nestedhvm.h>
 #include <asm/event.h>  /* for local_event_delivery_(en|dis)able */
 #include <asm/paging.h> /* for paging_mode_hap() */
@@ -477,6 +478,7 @@ nestedhvm_vcpu_vmexit(struct vcpu *v, st
      enum hvm_copy_result hvm_rc;
 
      hvm->nh_hostflags.fields.vmentry = 1;
+     paging_update_nestedmode(v);
      if (nestedhvm_vcpu_in_guestmode(v)) {
              ret = nestedhvm_vmexit(v, regs, exitcode);
              switch (ret) {
@@ -529,14 +531,30 @@ nestedhvm_vcpu_vmexit(struct vcpu *v, st
 void
 nestedhvm_vcpu_enter_guestmode(struct vcpu *v)
 {
+     struct p2m_domain *p2m;
      vcpu_nestedhvm(v).nh_guestmode = 1;
+
Quoted 8 lines

xen/arch/x86/hvm/nestedhvm.c nestedhvm_vcpu_vmexit(struct vcpu *v, st · line 542

Quoted 6 lines
xen/arch/x86/hvm/nestedhvm.c +6 -0
+     p2m = vcpu_nestedhvm(v).nh_p2m;
+     if (p2m == NULL)
+             /* p2m has either been invalidated or not yet assigned. */
+             return;
+
+     cpu_set(v->processor, p2m->p2m_dirty_cpumask);

Is this arch-specific code? (and likewise the cpu_clear that follows)

No. I don't see how.

Quoted 2 lines

Also, in the case where p2m is NULL, when the p2m is allocated later you need to set a bit in p2m->p2m_dirty_cpumask there too.

The idea is that the cpu bitmap is per p2m and has the bits for all physical cpus set whose vcpus are in guest mode.

Say, a l1 guest has four vcpus pinned on the physical cpus 0, 1, 2 and 3. Three of them share the same p2m and the vcpus 0 and 2 are in guest mode.

When the nested p2m is flushed then only the TLBs of the pcpus 0 and 2 are flushed. The pcpu 1 flushes its TLB on VMRUN emulation. An IPI is saved that way.

When the l1 guest runs multiple l2 smp guests then the TLB shootdown the savings on IPI's increase.

Quoted 45 lines
Quoted 32 lines
xen/arch/x86/hvm/nestedhvm.c +9 -1
 }
 
 void
 nestedhvm_vcpu_exit_guestmode(struct vcpu *v)
 {
+     struct p2m_domain *p2m;
      vcpu_nestedhvm(v).nh_guestmode = 0;
-}
+
+     p2m = vcpu_nestedhvm(v).nh_p2m;
+     if (p2m == NULL)
+             /* p2m has either been invalidated or not yet assigned. */
+             return;
+
+     cpu_clear(v->processor, p2m->p2m_dirty_cpumask);
+}
 
 
 

diff -r 50b3e6c73d7c -r 24daea64bdff xen/arch/x86/mm/hap/hap.c

xen/arch/x86/mm/hap/hap.c +1 -0
@@ -40,6 +40,7 @@
 #include <asm/p2m.h>
 #include <asm/domain.h>
 #include <xen/numa.h>
+#include <asm/hvm/nestedhvm.h>
 
 #include "private.h"
 

xen/arch/x86/mm/hap/hap.c static void hap_free_p2m_page(struct p2m · line 347

Quoted 7 lines
xen/arch/x86/mm/hap/hap.c +4 -0
@@ -340,6 +341,30 @@ static void hap_free_p2m_page(struct p2m
     hap_unlock(d);
 }
 
+#define nestedp2m_alloc_p2m_page hap_alloc_p2m_page
+
+/* We must use hap_free() or flushing the nested p2m tables fails
+ * with "freeing ptp fails due to insufficient pages.

That's quite sensible -- other code that frees p2m pages directly to Xen is basically wrong. It's a relic of the shadow allocator, where freeing individual p2m pages to the shadow pool would have been a bit tricky and I knew at the time that p2m pages were only ever freed on destruction.

Quoted 3 lines

I really ought to update the shadow p2m-free path now that the shadow pool is simpler. It just needs a little care around the order things happen in teardown.

I'm looking forward to see your patch.

Quoted 111 lines
Quoted 111 lines
xen/arch/x86/mm/hap/hap.c +22 -0
+ * XXX This triggers a bug in p2m that causes a crash in
+ * xen/common/page_alloc.c:1201 on L1 guest shutdown/destroy.
+ */
+static void
+nestedp2m_free_p2m_page(struct p2m_domain *p2m, struct page_info *pg)
+{
+    struct domain *d = p2m->domain;
+    hap_lock(d);
+    ASSERT(page_get_owner(pg) == d);
+    /* Should have just the one ref we gave it in alloc_p2m_page() */
+    BUG_ON((pg->count_info & PGC_count_mask) != 1);
+    pg->count_info = 0;
+    page_set_owner(pg, NULL);
+    hap_free(d, page_to_mfn(pg));
+    d->arch.paging.hap.total_pages++;
+    d->arch.paging.hap.p2m_pages--;
+    ASSERT(d->arch.paging.hap.p2m_pages >= 0);
+    hap_unlock(d);
+}
+
 /* Return the size of the pool, rounded up to the nearest MB */
 static unsigned int
 hap_get_allocation(struct domain *d)
 
 
+static int
+nestedp2m_next_level(struct p2m_domain *p2m, struct page_info

**table_pg, + void **table, unsigned long
*gfn_remainder, + unsigned long gfn, uint32_t shift,
uint32_t max, + unsigned long type)

xen/arch/x86/mm/hap/hap.c +11 -0
+{
+    l1_pgentry_t *l1_entry;
+    l1_pgentry_t *p2m_entry;
+    l1_pgentry_t new_entry;
+    void *next;
+    int i;
+
+    ASSERT(p2m);
+    ASSERT(p2m->alloc_page);
+
+    if ( !(p2m_entry = p2m_find_entry(*table, gfn_remainder, gfn, shift,

max)) ) + return 0;

xen/arch/x86/mm/hap/hap.c +20 -0
+
+    if ( !(l1e_get_flags(*p2m_entry) & _PAGE_PRESENT) )
+    {
+        struct page_info *pg;
+
+        pg = p2m_alloc_ptp(p2m, type);
+        if ( pg == NULL )
+            return 0;
+
+        new_entry = l1e_from_pfn(mfn_x(page_to_mfn(pg)),
+                                 __PAGE_HYPERVISOR | _PAGE_USER);
+
+        switch ( type ) {
+        case PGT_l3_page_table:
+            nested_write_p2m_entry(p2m, p2m_entry, new_entry);
+            break;
+        case PGT_l2_page_table:
+#if CONFIG_PAGING_LEVELS == 3
+            /* for PAE mode, PDPE only has PCD/PWT/P bits available */
+            new_entry = l1e_from_pfn(mfn_x(page_to_mfn(pg)),

_PAGE_PRESENT); +#endif

xen/arch/x86/mm/hap/hap.c +15 -0
+            nested_write_p2m_entry(p2m, p2m_entry, new_entry);
+            break;
+        case PGT_l1_page_table:
+            nested_write_p2m_entry(p2m, p2m_entry, new_entry);
+            break;
+        default:
+            BUG();
+            break;
+        }
+    }
+
+    ASSERT(l1e_get_flags(*p2m_entry) & (_PAGE_PRESENT|_PAGE_PSE));
+
+    /* split single large page into 4KB page in P2M table */
+    if ( type == PGT_l1_page_table && (l1e_get_flags(*p2m_entry) &

_PAGE_PSE) ) + {

xen/arch/x86/mm/hap/hap.c +8 -0
+        unsigned long flags, pfn;
+        struct page_info *pg;
+
+        pg = p2m_alloc_ptp(p2m, PGT_l1_page_table);
+        if ( pg == NULL )
+            return 0;
+
+        /* New splintered mappings inherit the flags of the old

superpage, + * with a little reorganisation for the _PAGE_PSE_PAT
bit. */ + flags = l1e_get_flags(*p2m_entry);

xen/arch/x86/mm/hap/hap.c +3 -0
+        pfn = l1e_get_pfn(*p2m_entry);
+        if ( pfn & 1 )           /* ==> _PAGE_PSE_PAT was set */
+            pfn -= 1;            /* Clear it; _PAGE_PSE becomes

_PAGE_PAT */ + else

xen/arch/x86/mm/hap/hap.c +17 -0
+            flags &= ~_PAGE_PSE; /* Clear _PAGE_PSE (== _PAGE_PAT) */
+
+        l1_entry = __map_domain_page(pg);
+        for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ )
+        {
+            new_entry = l1e_from_pfn(pfn + i, flags);
+            nested_write_p2m_entry(p2m, l1_entry+i, new_entry);
+        }
+        unmap_domain_page(l1_entry);
+
+        new_entry = l1e_from_pfn(mfn_x(page_to_mfn(pg)),
+                                 __PAGE_HYPERVISOR|_PAGE_USER);
+        nested_write_p2m_entry(p2m, p2m_entry, new_entry);
+    }
+
+    *table_pg = l1e_get_page(*p2m_entry);
+    next = __map_domain_page(*table_pg);
Quoted 8 lines

xen/arch/x86/mm/hap/hap.c

Quoted 5 lines
xen/arch/x86/mm/hap/hap.c +5 -0
+    unmap_domain_page(*table);
+    *table = next;
+
+    return 1;
+}

This function looks like it duplicates a lot of logic from an old version of the normal p2m next-level function.

Yes, it is redundant. The point is that the *_write_p2m_entry() functions are not nested virtualization aware. I am having troubles with understanding the monitor p2m tables.

Quoted 1 line

Would it be easy to merge them?

Maybe, maybe not. I can answer the question when I understand the use of the monitor table and how they work.

Quoted 94 lines
Quoted 94 lines
xen/arch/x86/mm/hap/hap.c +2 -0
+int
+nestedp2m_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t

mfn, + unsigned int page_order, p2m_type_t p2mt);

xen/arch/x86/mm/hap/hap.c +3 -0
+
+int
+nestedp2m_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t

mfn, + unsigned int page_order, p2m_type_t p2mt)

xen/arch/x86/mm/hap/hap.c +21 -0
+{
+    struct page_info *table_pg;
+    void *table;
+    unsigned long gfn_remainder = gfn;
+    l1_pgentry_t *p2m_entry;
+    l1_pgentry_t entry_content;
+    l2_pgentry_t l2e_content;
+    int rv = 0;
+
+    ASSERT(p2m);
+    ASSERT(p2m->alloc_page);
+
+    /* address of nested paging table */
+    table_pg = pagetable_get_page(p2m_get_pagetable(p2m));
+    table = __map_domain_page(table_pg);
+
+#if CONFIG_PAGING_LEVELS >= 4
+    if ( !nestedp2m_next_level(p2m, &table_pg, &table,
+                               &gfn_remainder, gfn,
+                               L4_PAGETABLE_SHIFT - PAGE_SHIFT,
+                               L4_PAGETABLE_ENTRIES, PGT_l3_page_table)

) + goto out;

xen/arch/x86/mm/hap/hap.c +16 -0
+#endif
+
+    if ( !nestedp2m_next_level(p2m, &table_pg, &table, &gfn_remainder,
+                               gfn, L3_PAGETABLE_SHIFT - PAGE_SHIFT,
+                               ((CONFIG_PAGING_LEVELS == 3)
+                                ? (paging_mode_hap(p2m->domain) ? 4 : 8)
+                                : L3_PAGETABLE_ENTRIES),
+                               PGT_l2_page_table) )
+        goto out;
+
+    if ( page_order == 0 )
+    {
+        if ( !nestedp2m_next_level(p2m, &table_pg, &table,
+                                   &gfn_remainder, gfn,
+                                   L2_PAGETABLE_SHIFT - PAGE_SHIFT,
+                                   L2_PAGETABLE_ENTRIES,

PGT_l1_page_table) ) + goto out;

xen/arch/x86/mm/hap/hap.c +42 -0
+
+        p2m_entry = p2m_find_entry(table, &gfn_remainder, gfn,
+                                   0, L1_PAGETABLE_ENTRIES);
+        ASSERT(p2m_entry);
+
+        if ( mfn_valid(mfn) ) {
+            entry_content = l1e_from_pfn(mfn_x(mfn),
+                                         p2m_type_to_flags(p2mt));
+        } else {
+            entry_content = l1e_empty();
+        }
+
+        /* level 1 entry */
+        nested_write_p2m_entry(p2m, p2m_entry, entry_content);
+    }
+    else
+    {
+        p2m_entry = p2m_find_entry(table, &gfn_remainder, gfn,
+                                   L2_PAGETABLE_SHIFT - PAGE_SHIFT,
+                                   L2_PAGETABLE_ENTRIES);
+        ASSERT(p2m_entry);
+
+        /* FIXME: Deal with 4k replaced by 2MB pages */
+        if ( (l1e_get_flags(*p2m_entry) & _PAGE_PRESENT) &&
+             !(l1e_get_flags(*p2m_entry) & _PAGE_PSE) )
+        {
+            domain_crash(p2m->domain);
+            goto out;
+        }
+
+        if ( mfn_valid(mfn) )
+            l2e_content = l2e_from_pfn(mfn_x(mfn),
+                p2m_type_to_flags(p2mt) | _PAGE_PSE);
+        else {
+            l2e_content = l2e_empty();
+     }
+
+        entry_content.l1 = l2e_content.l2;
+        nested_write_p2m_entry(p2m, p2m_entry, entry_content);
+    }
+
+    /* Track the highest gfn for which we have ever had a valid mapping

*/ + if ( mfn_valid(mfn)

xen/arch/x86/mm/hap/hap.c +4 -0
+         && (gfn + (1UL << page_order) - 1 > p2m->max_mapped_pfn) )
+        p2m->max_mapped_pfn = gfn + (1UL << page_order) - 1;
+
+    /* Success */
Quoted 8 lines

xen/arch/x86/mm/hap/hap.c

Quoted 6 lines
xen/arch/x86/mm/hap/hap.c +6 -0
+    rv = 1;
+
+out:
+    unmap_domain_page(table);
+    return rv;
+}

Same for this: looks like it duplicates existing code.

Correct. nestedp2m_next_level() exists as long as this one exist. And this one exists until *_write_p2m_entry() is made aware of nested virtualization.

Quoted 10 lines
Quoted 10 lines
xen/arch/x86/mm/hap/hap.c +9 -0
+int
+nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t L2_gpa)
+{
+    int rv;
+    paddr_t L1_gpa, L0_gpa;
+    struct domain *d = v->domain;
+    struct p2m_domain *p2m, *nested_p2m;
+
+    p2m = p2m_get_hostp2m(d); /* L0 p2m */
Quoted 8 lines

xen/arch/x86/mm/hap/hap.c

Quoted 5 lines
xen/arch/x86/mm/hap/hap.c +5 -0
+    nested_p2m = p2m_get_nestedp2m(v, vcpu_nestedhvm(v).nh_vm_hostcr3);
+
+    /* walk the L1 P2M table, note we have to pass p2m
+     * and not nested_p2m here or we fail the walk forever,
+     * otherwise. */

Can you explain that more fully? It looks like you're walking the L0 table to translate an L2 gfn into an L1 gfn, which surely can't be right.

The l1 guest hostcr3 is in l1 guest physical address space. To walk the l1 guest's page table I need to translate it into host physical address space by walking the l0 p2m table.

Quoted 1 line
Quoted 1 line

+ rv = nestedhap_walk_L1_p2m(v, p2m, L2_gpa, &L1_gpa);

This translates the l2 guest physical address into l1 guest physical address and check if the l1 guest page table walk was successful:

Quoted 6 lines
Quoted 6 lines
xen/arch/x86/mm/hap/hap.c +6 -0
+
+    /* let caller to handle these two cases */
+    switch (rv) {
+    case NESTEDHVM_PAGEFAULT_INJECT:
+        return rv;
+    case NESTEDHVM_PAGEFAULT_ERROR:
Quoted 8 lines

xen/arch/x86/mm/hap/hap.c

Quoted 8 lines
xen/arch/x86/mm/hap/hap.c +8 -0
+        return rv;
+    case NESTEDHVM_PAGEFAULT_DONE:
+        break;
+    default:
+        BUG();
+        break;
+    }
+

Here we know the l1 guest page table walk was successful so translate the l1 guest physical address into host physical address.

Quoted 7 lines
Quoted 7 lines
xen/arch/x86/mm/hap/hap.c +7 -0
+    /* ==> we have to walk L0 P2M */
+    rv = nestedhap_walk_L0_p2m(p2m, L1_gpa, &L0_gpa);
+
+    /* let upper level caller to handle these two cases */
+    switch (rv) {
+    case NESTEDHVM_PAGEFAULT_INJECT:
+        return rv;
Quoted 8 lines

xen/arch/x86/mm/hap/hap.c

Quoted 8 lines
xen/arch/x86/mm/hap/hap.c +8 -0
+    case NESTEDHVM_PAGEFAULT_ERROR:
+        return rv;
+    case NESTEDHVM_PAGEFAULT_DONE:
+        break;
+    default:
+        BUG();
+        break;
+    }

Here we know the walk was successfull so we can fix the nestedp2m.

Quoted 86 lines
Quoted 86 lines
xen/arch/x86/mm/hap/hap.c +19 -0
+    /* fix p2m_get_pagetable(nested_p2m) */
+    nestedhap_fix_p2m(nested_p2m, L2_gpa, L0_gpa);
+
+    return NESTEDHVM_PAGEFAULT_DONE;
+}
+
+/********************************************/
+/*     NESTED VIRT INITIALIZATION FUNCS     */
+/********************************************/
+
+/*
+ * Local variables:
+ * mode: C
+ * c-set-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */

diff -r 50b3e6c73d7c -r 24daea64bdff xen/arch/x86/mm/hap/private.h

xen/arch/x86/mm/hap/private.h +10 -0
@@ -30,4 +30,14 @@ unsigned long hap_gva_to_gfn_3_levels(st
 unsigned long hap_gva_to_gfn_4_levels(struct vcpu *v, unsigned long gva,
                                      uint32_t *pfec);
 
+unsigned long hap_p2m_ga_to_gfn_2_levels(struct vcpu *v,
+    struct p2m_domain *p2m, unsigned long cr3,
+    paddr_t ga, uint32_t *pfec);
+unsigned long hap_p2m_ga_to_gfn_3_levels(struct vcpu *v,
+    struct p2m_domain *p2m, unsigned long cr3,
+    paddr_t ga, uint32_t *pfec);
+unsigned long hap_p2m_ga_to_gfn_4_levels(struct vcpu *v,
+    struct p2m_domain *p2m, unsigned long cr3,
+    paddr_t ga, uint32_t *pfec);
+
 #endif /* __HAP_PRIVATE_H__ */

diff -r 50b3e6c73d7c -r 24daea64bdff xen/arch/x86/mm/p2m.c

xen/arch/x86/mm/p2m.c +6 -3
@@ -34,6 +34,7 @@
 #include <public/mem_event.h>
 #include <asm/mem_sharing.h>
 #include <xen/event.h>
+#include <asm/hvm/nestedhvm.h>
 
 /* Debugging and auditing of the P2M code? */
 #define P2M_AUDIT     0
@@ -72,7 +73,7 @@ boolean_param("hap_1gb", opt_hap_1gb);
 #define SUPERPAGE_PAGES (1UL << 9)
 #define superpage_aligned(_x)  (((_x)&(SUPERPAGE_PAGES-1))==0)
 
-static unsigned long p2m_type_to_flags(p2m_type_t t)
+unsigned long p2m_type_to_flags(p2m_type_t t)
 {
     unsigned long flags;
 #ifdef __x86_64__
@@ -116,9 +117,9 @@ static void audit_p2m(struct p2m_domain
 // Find the next level's P2M entry, checking for out-of-range gfn's...
 // Returns NULL on error.
 //
-static l1_pgentry_t *
+l1_pgentry_t *
 p2m_find_entry(void *table, unsigned long *gfn_remainder,
-                   unsigned long gfn, u32 shift, u32 max)
+                   unsigned long gfn, uint32_t shift, uint32_t max)
 {
     u32 index;
 
@@ -1719,10 +1720,12 @@ static void p2m_initialise(struct domain
     INIT_PAGE_LIST_HEAD(&p2m->pod.single);
 
     p2m->domain = d;
+    p2m->cr3 = 0;
     p2m->set_entry = p2m_set_entry;
     p2m->get_entry = p2m_gfn_to_mfn;
     p2m->get_entry_current = p2m_gfn_to_mfn_current;
     p2m->change_entry_type_global = p2m_change_type_global;
+    cpus_clear(p2m->p2m_dirty_cpumask);
 
     if ( hap_enabled(d) && (boot_cpu_data.x86_vendor ==
 X86_VENDOR_INTEL) ) ept_p2m_init(d);
@@ -1730,6 +1733,28 @@ static void p2m_initialise(struct domain
     return;
 }
 
Quoted 5 lines

xen/arch/x86/mm/p2m.c static void p2m_initialise(struct domain · line 1738

Quoted 3 lines
xen/arch/x86/mm/p2m.c +2 -0
+extern int
+nestedp2m_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t
 mfn, +                    unsigned int page_order, p2m_type_t p2mt);

Please define this in a header file.

This will go away along with the code duplication we talked above. Is it ok to keep it here until then?

Quoted 137 lines
Quoted 137 lines
xen/arch/x86/mm/p2m.c +89 -1
+static int
+p2m_init_nestedp2m(struct domain *d)
+{
+    uint8_t i;
+    struct p2m_domain *p2m;
+
+    spin_lock_init(&d->arch.nested_p2m_lock);
+    for (i = 0; i < MAX_NESTEDP2M; i++) {
+        d->arch.nested_p2m[i] = p2m = xmalloc(struct p2m_domain);
+        if (p2m == NULL)
+            return -ENOMEM;
+        p2m_initialise(d, p2m);
+        p2m->set_entry = nestedp2m_set_entry;
+    }
+
+    return 0;
+}
+
 int p2m_init(struct domain *d)
 {
     struct p2m_domain *p2m;
@@ -1739,7 +1764,11 @@ int p2m_init(struct domain *d)
         return -ENOMEM;
     p2m_initialise(d, p2m);
 
-    return 0;
+    /* Must initialise nestedp2m unconditionally
+     * since nestedhvm_enabled(d) returns false here.
+     * (p2m_init runs too early for HVM_PARAM_* options)
+     */
+    return p2m_init_nestedp2m(d);
 }
 
 void p2m_change_entry_type_global(struct p2m_domain *p2m,
@@ -1836,6 +1865,9 @@ int p2m_alloc_table(struct p2m_domain *p
                         p2m_invalid) )
         goto error;
 
+    if (p2m_is_nestedp2m(p2m))
+        goto nesteddone;
+
     /* Copy all existing mappings from the page list and m2p */
     spin_lock(&p2m->domain->page_alloc_lock);
     page_list_for_each(page, &p2m->domain->page_list)
@@ -1857,6 +1889,7 @@ int p2m_alloc_table(struct p2m_domain *p
     }
     spin_unlock(&p2m->domain->page_alloc_lock);
 
+ nesteddone:
     P2M_PRINTK("p2m table initialised (%u pages)\n", page_count);
     p2m_unlock(p2m);
     return 0;
@@ -1881,6 +1914,9 @@ void p2m_teardown(struct p2m_domain *p2m
     mfn_t mfn;
 #endif
 
+    if (p2m == NULL)
+        return;
+
     p2m_lock(p2m);
 
 #ifdef __x86_64__
@@ -1899,11 +1935,26 @@ void p2m_teardown(struct p2m_domain *p2m
     p2m_unlock(p2m);
 }
 
+static void p2m_teardown_nestedp2m(struct domain *d)
+{
+    uint8_t i;
+
+    for (i = 0; i < MAX_NESTEDP2M; i++) {
+        xfree(d->arch.nested_p2m[i]);
+        d->arch.nested_p2m[i] = NULL;
+    }
+}
+
 void p2m_final_teardown(struct domain *d)
 {
     /* Iterate over all p2m tables per domain */
     xfree(d->arch.p2m);
     d->arch.p2m = NULL;
+
+    /* We must teardown unconditionally because
+     * we initialise them unconditionally.
+     */
+    p2m_teardown_nestedp2m(d);
 }
 
 #if P2M_AUDIT
@@ -2823,6 +2874,173 @@ void p2m_mem_paging_resume(struct p2m_do
 }
 #endif /* __x86_64__ */
 
+static struct p2m_domain *
+p2m_getlru_nestedp2m(struct domain *d, struct p2m_domain *p2m)
+{
+    int i, lru_index = -1;
+    struct p2m_domain *lrup2m, *tmp;
+
+    if (p2m == NULL) {
+        lru_index = MAX_NESTEDP2M - 1;
+        lrup2m = d->arch.nested_p2m[lru_index];
+    } else {
+        lrup2m = p2m;
+        for (i = 0; i < MAX_NESTEDP2M; i++) {
+            if (d->arch.nested_p2m[i] == p2m) {
+                lru_index = i;
+                break;
+            }
+        }
+    }
+
+    ASSERT(lru_index >= 0);
+    if (lru_index == 0) {
+        return lrup2m;
+    }
+
+    /* move the other's down the array "list" */
+    for (i = lru_index - 1; i >= 0; i--) {
+        tmp = d->arch.nested_p2m[i];
+        d->arch.nested_p2m[i+1] = tmp;
+    }
+
+    /* make the entry the first one */
+    d->arch.nested_p2m[0] = lrup2m;
+
+    return lrup2m;
+}
+
+static int
+p2m_flush_locked(struct p2m_domain *p2m)
+{
+    struct page_info * (*alloc)(struct p2m_domain *);
+    void (*free)(struct p2m_domain *, struct page_info *);
+
+    alloc = p2m->alloc_page;
+    free = p2m->free_page;
Quoted 8 lines

xen/arch/x86/mm/p2m.c void p2m_mem_paging_resume(struct p2m_do · line 2926

Quoted 6 lines
xen/arch/x86/mm/p2m.c +6 -0
+
+    if (p2m->cr3 == 0)
+        /* Microoptimisation: p2m is already empty.
+         * => about 0.3% speedup of overall system performance.
+         */
+        return 0;

What happens if a malicious guest uses 0 as its actual CR3 value?

When l1 guest uses hap and l2 guest uses shadow paging, this code path never runs.
When l1 guest uses hap and l2 guest uses hap, this is can only be the guest's hostcr3.

So your question is what happens if a malicious guest uses 0 to point to its nested page table ?

I think, this guest crashes sooner or later anyway.

Quoted 81 lines
Quoted 81 lines
xen/arch/x86/mm/p2m.c +81 -0
+
+    p2m_teardown(p2m);
+    p2m_initialise(p2m->domain, p2m);
+    p2m->set_entry = nestedp2m_set_entry;
+    BUG_ON(p2m_alloc_table(p2m, alloc, free) != 0);
+
+    ASSERT(p2m);
+    ASSERT(p2m->alloc_page);
+    return 0;
+}
+
+void
+p2m_flush(struct vcpu *v, struct p2m_domain *p2m)
+{
+    struct domain *d = p2m->domain;
+
+    nestedhvm_vm_flushtlb(p2m);
+    ASSERT(v->domain == d);
+    vcpu_nestedhvm(v).nh_p2m = NULL;
+    spin_lock(&d->arch.nested_p2m_lock);
+    BUG_ON(p2m_flush_locked(p2m) != 0);
+    spin_unlock(&d->arch.nested_p2m_lock);
+    hvm_asid_flush_vcpu(v);
+}
+
+void
+p2m_flush_nestedp2m(struct domain *d)
+{
+    int i;
+
+    spin_lock(&d->arch.nested_p2m_lock);
+    for (i = 0; i < MAX_NESTEDP2M; i++)
+        BUG_ON(p2m_flush_locked(d->arch.nested_p2m[i]) != 0);
+    spin_unlock(&d->arch.nested_p2m_lock);
+    flush_tlb_mask(&d->domain_dirty_cpumask);
+}
+
+struct p2m_domain *
+p2m_get_nestedp2m(struct vcpu *v, uint64_t cr3)
+{
+    struct nestedhvm *hvm = &vcpu_nestedhvm(v);
+    struct domain *d;
+    struct p2m_domain *p2m;
+    int i, rv;
+
+    if (cr3 == 0)
+        cr3 = v->arch.hvm_vcpu.guest_cr[3];
+
+    if (hvm->nh_flushp2m && hvm->nh_p2m) {
+        nestedhvm_vm_flushtlb(hvm->nh_p2m);
+        hvm->nh_p2m = NULL;
+    }
+
+    d = v->domain;
+    spin_lock(&d->arch.nested_p2m_lock);
+    for (i = 0; i < MAX_NESTEDP2M; i++) {
+        p2m = d->arch.nested_p2m[i];
+        if (p2m->cr3 == cr3 && p2m == hvm->nh_p2m) {
+            p2m_getlru_nestedp2m(d, p2m);
+            if (hvm->nh_flushp2m) {
+               BUG_ON(p2m_flush_locked(p2m) != 0);
+               hvm->nh_flushp2m = 0;
+               hvm_asid_flush_vcpu(v);
+            }
+            p2m->cr3 = cr3;
+            spin_unlock(&d->arch.nested_p2m_lock);
+            return p2m;
+        }
+        if (p2m->cr3 == 0) { /* found unused p2m table */
+            hvm->nh_flushp2m = 0;
+            p2m_getlru_nestedp2m(d, p2m);
+            hvm->nh_p2m = p2m;
+            p2m->cr3 = cr3;
+            spin_unlock(&d->arch.nested_p2m_lock);
+            hvm_asid_flush_vcpu(v);
+            return p2m;
+        }
+    }
+
+    /* All p2m's are or were in use. We know the least recent used one.
+     * Destroy and re-initialize it.
Quoted 8 lines

xen/arch/x86/mm/p2m.c void p2m_mem_paging_resume(struct p2m_do · line 3011

Quoted 4 lines
xen/arch/x86/mm/p2m.c +4 -0
+     */
+    for (i = 0; i < MAX_NESTEDP2M; i++) {
+        p2m = p2m_getlru_nestedp2m(d, NULL);
+        rv = p2m_flush_locked(p2m);

Is this enough? If this p2m might be in host_vmcb->h_cr3 somewhere on another vcpu, then you need to make sure that vcpu gets reset not to use it any more.

There are three possibilities:
An other vcpu is in VMRUN emulation before a nestedp2m is assigned.
In this case, it will get a new nestedp2m as it won't find its 'old' nestedp2m anymore.

An other vcpu is in VMRUN emulation after a nestedp2m is assigned.
It will VMEXIT with a nested page fault.

An other vcpu already running l2 guest.
It will VMEXIT with a nested page fault immediately.

Christoph

Signature · 6 lines
---to satisfy European Law for business letters:
Advanced Micro Devices GmbH
Einsteinring 24, 85609 Dornach b. Muenchen
Geschaeftsfuehrer: Alberto Bozzo, Andrew Bowd
Sitz: Dornach, Gemeinde Aschheim, Landkreis Muenchen
Registergericht Muenchen, HRB Nr. 43632
#3 lore raw
Quoted 54 lines · Christoph Egger
Quoted 34 lines · On Wednesday 08 September 2010 16:04:51 Tim Deegan wrote:

Hi,

This is looking better - I think the TLB flushing is almost right. A few more detailed comments below.

Content-Description: xen_nh13_haphap.diff

Quoted 27 lines

# HG changeset patch
# User cegger
# Date 1283345895 -7200
Implement Nested-on-Nested.
This allows the guest to run nested guest with hap enabled.

diff -r 50b3e6c73d7c -r 24daea64bdff xen/arch/x86/hvm/hvm.c --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c

xen/arch/x86/hvm/hvm.c +10 -1
@@ -1042,12 +1042,56 @@ void hvm_inject_exception(unsigned int t
     hvm_funcs.inject_exception(trapnr, errcode, cr2);  }
 
-bool_t hvm_hap_nested_page_fault(unsigned long gfn)
+bool_t hvm_hap_nested_page_fault(paddr_t gpa, struct cpu_user_regs
     *regs) { p2m_type_t p2mt;
     mfn_t mfn;
     struct vcpu *v = current;
     struct p2m_domain *p2m = p2m_get_hostp2m(v->domain);
+    unsigned long gfn = gpa >> PAGE_SHIFT;
+    int rv;
+
+    /* On Nested Virtualization, walk the guest page table.
+     * If this succeeds, all is fine.
+     * If this fails, inject a nested page fault into the guest. +
 */ +    if ( nestedhvm_enabled(v->domain)
+        && nestedhvm_vcpu_in_guestmode(v)
+        && nestedhvm_paging_mode_hap(v) )
+    {
Quoted 8 lines

xen/arch/x86/hvm/hvm.c void hvm_inject_exception(unsigned int t · line 1065

Quoted 3 lines
xen/arch/x86/hvm/hvm.c +3 -0
+        enum nestedhvm_vmexits nsret;
+        struct nestedhvm *hvm = &vcpu_nestedhvm(v); +
+        /* nested guest gpa == guest gva */

This comment is confusing to me. The nested guest gpa isn't a virtual address, and certainly not the same as an L1-guest VA. Can you reword it, maybe just to say what the call to nestedhvm_hap_nested_page_fault() is going to do?

New wording is:

/* The vcpu is in guest mdoe and the l1 guest
* uses hap. That means 'gpa' is in l2 guest
* physical adderss space.
* Fix the nested p2m or inject nested page fault
* into l1 guest if not fixable. The algorithm is
* the same as for shadow paging.
*/

Quoted 1 line

Is this fine with you ?

wording is always a challenge in nested virtualization :(

I have similar feeling and thinking. In all the explaination text, we use the term l1 guest, l2 guest which makes everybody easy to understand, but in the code we are avoiding those clear prefix both here and in Qing's patch. How about we use l1/l2 prefix more to explicitly differentiate among them? Just 2 cents, it may be too later.

Thx, Eddie

#8 lore raw
In reply to Christoph Egger
Quoted 15 lines · Christoph Egger
Quoted 4 lines

This comment is confusing to me. The nested guest gpa isn't a virtual address, and certainly not the same as an L1-guest VA. Can you reword it, maybe just to say what the call to nestedhvm_hap_nested_page_fault() is going to do?

New wording is:

/* The vcpu is in guest mdoe and the l1 guest
* uses hap. That means 'gpa' is in l2 guest
* physical adderss space.
* Fix the nested p2m or inject nested page fault
* into l1 guest if not fixable. The algorithm is
* the same as for shadow paging.
*/

Quoted 1 line

Is this fine with you ?

Sure, that's better.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
(quoted diff) +1 -1
-            gfn = paging_gva_to_gfn(curr, addr, &pfec);
+            gfn = paging_p2m_ga_to_gfn(curr, p2m, mode, cr3, addr,

&pfec);

Do other callers of paging_gva_to_gfn need to handle nested-npt mode as well?

If the other callers can be reached when the vcpu is in guest mode, then yes.

It looks like most of them can.

Quoted 4 lines
Quoted 1 line

If so, would it be better to update paging_gva_to_gfn() itself?

This is definitely easier but the call to p2m_get_p2m() might become be very expensive and should be cached by passing through per function argument.

I'd rather it was cached somewhere in the per-vcpu state if p2m_get_p2m() is expensive, and not pass any more arguments. Callers of paging_gva_to_gfn() shouldn't need to know about the internals of the p2m code - they should just be able to pass a VA and get a (L1) gfn.

Quoted 10 lines
Quoted 8 lines

xen/arch/x86/hvm/nestedhvm.c nestedhvm_vcpu_vmexit(struct vcpu *v, st · line 542

Quoted 6 lines
xen/arch/x86/hvm/nestedhvm.c +6 -0
+     p2m = vcpu_nestedhvm(v).nh_p2m;
+     if (p2m == NULL)
+             /* p2m has either been invalidated or not yet assigned. */
+             return;
+
+     cpu_set(v->processor, p2m->p2m_dirty_cpumask);

Is this arch-specific code? (and likewise the cpu_clear that follows)

No. I don't see how.

It's only used by NPT-on-NPT. IIRC the EPT-on-EPT code uses a soft-TLB model so it won't need this cpumask. But it's not a big deal.

Quoted 11 lines
Quoted 2 lines

This function looks like it duplicates a lot of logic from an old version of the normal p2m next-level function.

Yes, it is redundant. The point is that the *_write_p2m_entry() functions are not nested virtualization aware. I am having troubles with understanding the monitor p2m tables.

Quoted 1 line

Would it be easy to merge them?

Maybe, maybe not. I can answer the question when I understand the use of the monitor table and how they work.

OK. I'll wait for the next rev then.

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
xen/arch/x86/mm/hap/nested_hap.c new file +9 -0
+int
+nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t L2_gpa)
+{
+    int rv;
+    paddr_t L1_gpa, L0_gpa;
+    struct domain *d = v->domain;
+    struct p2m_domain *p2m, *nested_p2m;
+
+    p2m = p2m_get_hostp2m(d); /* L0 p2m */
Quoted 8 lines

xen/arch/x86/mm/hap/nested_hap.c line 373

Quoted 5 lines
xen/arch/x86/mm/hap/nested_hap.c new file +5 -0
+    nested_p2m = p2m_get_nestedp2m(v, vcpu_nestedhvm(v).nh_vm_hostcr3);
+
+    /* walk the L1 P2M table, note we have to pass p2m
+     * and not nested_p2m here or we fail the walk forever,
+     * otherwise. */

Can you explain that more fully? It looks like you're walking the L0 table to translate an L2 gfn into an L1 gfn, which surely can't be right.

Quoted 5 lines

The l1 guest hostcr3 is in l1 guest physical address space. To walk the l1 guest's page table I need to translate it into host physical address space by walking the l0 p2m table.

Quoted 1 line
Quoted 1 line

+ rv = nestedhap_walk_L1_p2m(v, p2m, L2_gpa, &L1_gpa);

OK, I understand now, thanks.

Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/x86/mm/p2m.c +8 -0
+static int
+p2m_flush_locked(struct p2m_domain *p2m)
+{
+    struct page_info * (*alloc)(struct p2m_domain *);
+    void (*free)(struct p2m_domain *, struct page_info *);
+
+    alloc = p2m->alloc_page;
+    free = p2m->free_page;
Quoted 8 lines

xen/arch/x86/mm/p2m.c void p2m_mem_paging_resume(struct p2m_do · line 2926

Quoted 6 lines
xen/arch/x86/mm/p2m.c +6 -0
+
+    if (p2m->cr3 == 0)
+        /* Microoptimisation: p2m is already empty.
+         * => about 0.3% speedup of overall system performance.
+         */
+        return 0;

What happens if a malicious guest uses 0 as its actual CR3 value?

Quoted 7 lines

When l1 guest uses hap and l2 guest uses shadow paging, this code path never runs.
When l1 guest uses hap and l2 guest uses hap, this is can only be the guest's hostcr3.

So your question is what happens if a malicious guest uses 0 to point to its nested page table ?

Yes.

Quoted 1 line

I think, this guest crashes sooner or later anyway.

It might, but in the meantime we've missed a flush. If we fail to properly flush a nested P2M when we should, the L2 guest gets to use stale entries to write to memory it shouldn't see.

My point is that you can't use cr3==0 as an indicator that this slot isn't in use because the guest can cause it to be true. Maybe -1 would be a better choice, since that's not a valid cr3 value.

Quoted 11 lines
Quoted 10 lines

xen/arch/x86/mm/p2m.c void p2m_mem_paging_resume(struct p2m_do · line 3011

Quoted 6 lines
xen/arch/x86/mm/p2m.c +6 -0
+    /* All p2m's are or were in use. We know the least recent used one.
+     * Destroy and re-initialize it.
+     */
+    for (i = 0; i < MAX_NESTEDP2M; i++) {
+        p2m = p2m_getlru_nestedp2m(d, NULL);
+        rv = p2m_flush_locked(p2m);

Is this enough? If this p2m might be in host_vmcb->h_cr3 somewhere on another vcpu, then you need to make sure that vcpu gets reset not to use it any more.

Quoted 7 lines

There are three possibilities:
An other vcpu is in VMRUN emulation before a nestedp2m is assigned.
In this case, it will get a new nestedp2m as it won't find its 'old' nestedp2m anymore.

An other vcpu is in VMRUN emulation after a nestedp2m is assigned.
It will VMEXIT with a nested page fault.

Why?

Quoted 2 lines

An other vcpu already running l2 guest.
It will VMEXIT with a nested page fault immediately.

Hmm. It will exit for the TLB shootdown IPI, but I think you need to clear vcpu_nestedhvm(v).nh_p2m on the other vcpu to make sure it doesn't re-enter with the p2m you've just recycled.

Cheers,

Tim.

Signature · 3 lines
Tim Deegan <[email protected]>
Principal Software Engineer, XenServer Engineering
Citrix Systems UK Ltd.  (Company #02937203, SL9 0BG)
#4 lore raw
In reply to Dong, Eddie
Quoted 8 lines · Eddie

wording is always a challenge in nested virtualization :(

I have similar feeling and thinking. In all the explaination text, we use the term l1 guest, l2 guest which makes everybody easy to understand, but in the code we are avoiding those clear prefix both here and in Qing's patch. How about we use l1/l2 prefix more to explicitly differentiate among them? Just 2 cents, it may be too later.

That sounds like a good idea. My only reservation is that it might be confusing since we already use l1 and l2 when naming levels of pagetables, so e.g. the shadow code has variables called l2gfn.

Maybe we could use n0, n1, n2 for nesting levels instead?

Cheers,

Tim.

Signature · 3 lines
Tim Deegan <[email protected]>
Principal Software Engineer, XenServer Engineering
Citrix Systems UK Ltd.  (Company #02937203, SL9 0BG)
#5 lore raw
Quoted 10 lines · Tim Deegan
Quoted 8 lines · Eddie

wording is always a challenge in nested virtualization :(

I have similar feeling and thinking. In all the explaination text, we use the term l1 guest, l2 guest which makes everybody easy to understand, but in the code we are avoiding those clear prefix both here and in Qing's patch. How about we use l1/l2 prefix more to explicitly differentiate among them? Just 2 cents, it may be too later.

Quoted 6 lines

That sounds like a good idea. My only reservation is that it might be confusing since we already use l1 and l2 when naming levels of pagetables, so e.g. the shadow code has variables called l2gfn.

Maybe we could use n0, n1, n2 for nesting levels instead?

Or l1g, l2g? Anyway, either is better to me and up to your decision.

#6 lore raw
Quoted 11 lines · Eddie
Quoted 10 lines · Tim Deegan
Quoted 8 lines · Eddie

wording is always a challenge in nested virtualization :(

I have similar feeling and thinking. In all the explaination text, we use the term l1 guest, l2 guest which makes everybody easy to understand, but in the code we are avoiding those clear prefix both here and in Qing's patch. How about we use l1/l2 prefix more to explicitly differentiate among them? Just 2 cents, it may be too later.

Quoted 7 lines
Quoted 6 lines

That sounds like a good idea. My only reservation is that it might be confusing since we already use l1 and l2 when naming levels of pagetables, so e.g. the shadow code has variables called l2gfn.

Maybe we could use n0, n1, n2 for nesting levels instead?

Or l1g, l2g? Anyway, either is better to me and up to your decision.

I think I prefer n1, n2 - just because it's shorter. :)

Tim.

Signature · 3 lines
Tim Deegan <[email protected]>
Principal Software Engineer, XenServer Engineering
Citrix Systems UK Ltd.  (Company #02937203, SL9 0BG)
#7 lore raw
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

Maybe we could use n0, n1, n2 for nesting levels instead?

Or l1g, l2g? Anyway, either is better to me and up to your decision.

I think I prefer n1, n2 - just because it's shorter. :)

FIne. Will follow in VMX side.

Eddie