Skip to contentSkip to conversation

Nested Virtualization: hap-on-hap

PATCH In review Christoph Egger 3 participants 9 messages Mar 9, 2011 – Apr 29, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 12/12 Nested Virtualization: hap-on-hap

Signature · 7 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 1299677057 -3600
Implement Nested-on-Nested.
This allows the guest to run nested guest with hap enabled.

xen/arch/x86/hvm/hvm.c +62 -14
diff -r 3ab405e67be6 -r 98598880e482 xen/arch/x86/hvm/hvm.c
@@ -1170,21 +1170,50 @@ void hvm_inject_exception(unsigned int t
     hvm_funcs.inject_exception(trapnr, errcode, cr2);
 }
 
-bool_t hvm_hap_nested_page_fault(unsigned long gpa,
-                                 bool_t gla_valid,
-                                 unsigned long gla,
-                                 bool_t access_valid,
-                                 bool_t access_r,
-                                 bool_t access_w,
-                                 bool_t access_x)
+int hvm_hap_nested_page_fault(unsigned long gpa,
+                              bool_t gla_valid,
+                              unsigned long gla,
+                              bool_t access_valid,
+                              bool_t access_r,
+                              bool_t access_w,
+                              bool_t access_x)
 {
     unsigned long gfn = gpa >> PAGE_SHIFT;
     p2m_type_t p2mt;
     p2m_access_t p2ma;
     mfn_t mfn;
     struct vcpu *v = current;
-    struct p2m_domain *p2m = p2m_get_hostp2m(v->domain);
-
+    struct p2m_domain *p2m = NULL;
+
+    /* 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) )
+    {
+        int rv;
+
+        /* The vcpu is in guest mode and the l1 guest
+         * uses hap. That means 'gpa' is in l2 guest
+         * physical address 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.
+         */
+        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:
+            return -1;
+        }
+    }
+
+    p2m = p2m_get_hostp2m(v->domain);
     mfn = gfn_to_mfn_type_current(p2m, gfn, &p2mt, &p2ma, p2m_guest);
 
     /* Check access permissions first, then handle faults */
@@ -1328,6 +1357,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);
@@ -1478,8 +1516,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_vmswitch_in_progress(v) && nestedhvm_vcpu_in_guestmode(v) )
+            paging_update_nestedmode(v);
+        else
+            paging_update_paging_modes(v);
+    }
 
     return X86EMUL_OKAY;
 
@@ -1546,8 +1588,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_vmswitch_in_progress(v) && nestedhvm_vcpu_in_guestmode(v) )
+            paging_update_nestedmode(v);
+        else
+            paging_update_paging_modes(v);
+    }
 
     return X86EMUL_OKAY;
 
@@ -2060,7 +2106,7 @@ 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;
@@ -2082,6 +2128,8 @@ static enum hvm_copy_result __hvm_copy(
         return HVMCOPY_unhandleable;
 #endif
 
+    p2m = p2m_get_hostp2m(curr->domain);
+
     while ( todo > 0 )
     {
         count = min_t(int, PAGE_SIZE - (addr & ~PAGE_MASK), todo);
xen/arch/x86/hvm/nestedhvm.c +49 -0

61 lines not on this page · raw message

xen/arch/x86/hvm/svm/entry.S +1 -0
diff -r 3ab405e67be6 -r 98598880e482 xen/arch/x86/hvm/svm/entry.S
@@ -65,6 +65,7 @@ ENTRY(svm_asm_do_resume)
         testl $~0,(r(dx),r(ax),1)
         jnz  .Lsvm_process_softirqs
 
+        call nsvm_p2m_handle_vmrun
         call svm_asid_handle_vmrun
 
         cmpb $0,addr_of(tb_init_done)
xen/arch/x86/hvm/svm/nestedsvm.c +37 -0
diff -r 3ab405e67be6 -r 98598880e482 xen/arch/x86/hvm/svm/nestedsvm.c
@@ -26,6 +26,7 @@
 #include <asm/hvm/svm/svmdebug.h>
 #include <asm/paging.h> /* paging_mode_hap */
 #include <asm/event.h> /* for local_event_delivery_(en|dis)able */
+#include <asm/p2m.h> /* p2m_get_pagetable, p2m_get_nestedp2m */
 
 static void
 nestedsvm_vcpu_clgi(struct vcpu *v)
@@ -320,6 +321,18 @@ static int nsvm_vmrun_permissionmap(stru
     return 0;
 }
 
+static void nestedsvm_vmcb_set_nestedp2m(struct vcpu *v,
+    struct vmcb_struct *vvmcb, struct vmcb_struct *n2vmcb)
+{
+    struct p2m_domain *p2m;
+
+    ASSERT(v != NULL);
+    ASSERT(vvmcb != NULL);
+    ASSERT(n2vmcb != NULL);
+    p2m = p2m_get_nestedp2m(v, vvmcb->_h_cr3);
+    n2vmcb->_h_cr3 = pagetable_get_paddr(p2m_get_pagetable(p2m));
+}
+
 static int nsvm_vmcb_prepare4vmrun(struct vcpu *v, struct cpu_user_regs *regs)
 {
     struct nestedvcpu *nv = &vcpu_nestedhvm(v);
@@ -475,6 +488,9 @@ static int nsvm_vmcb_prepare4vmrun(struc
     /* Nested paging mode */
     if (nestedhvm_paging_mode_hap(v)) {
         /* host nested paging + guest nested paging. */
+        n2vmcb->_np_enable = 1;
+
+        nestedsvm_vmcb_set_nestedp2m(v, ns_vmcb, n2vmcb);
 
         /* hvm_set_cr3() below sets v->arch.hvm_vcpu.guest_cr[3] for us. */
         rc = hvm_set_cr3(ns_vmcb->_cr3);
@@ -1323,6 +1339,27 @@ asmlinkage void nsvm_vcpu_switch(struct 
     }
 }
 
+/* This runs after VMRUN emulation. It is possible
+ * that an other (v)cpu flushed the assigned nestedp2m
+ * during the VMRUN emulation. Check for this case
+ * and get a new one.
+ * Caller must ensure the GIF is cleared.
+ */
+asmlinkage void nsvm_p2m_handle_vmrun(void)
+{
+    struct vcpu *v = current;
+    struct nestedvcpu *nv;
+
+    if (!nestedhvm_enabled(v->domain))
+        return;
+
+    nv = &vcpu_nestedhvm(v);
+    if (nv->nv_p2m == NULL
+      && nestedhvm_vcpu_in_guestmode(v)
+      && nestedhvm_paging_mode_hap(v))
+        nestedsvm_vmcb_set_nestedp2m(v, nv->nv_vvmcx, nv->nv_n2vmcx);
+}
+
 /* Interrupts, Virtual GIF */
 int
 nestedsvm_vcpu_interrupt(struct vcpu *v, const struct hvm_intack intack)
xen/arch/x86/hvm/svm/svm.c +25 -8
diff -r 3ab405e67be6 -r 98598880e482 xen/arch/x86/hvm/svm/svm.c
@@ -1014,14 +1014,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;
+    struct p2m_domain *p2m = NULL;
 
-    p2m = p2m_get_hostp2m(current->domain);
+    ret = hvm_hap_nested_page_fault(gpa, 0, ~0ul, 0, 0, 0, 0);
 
     if ( tb_init_done )
     {
@@ -1032,6 +1034,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));
@@ -1039,14 +1042,26 @@ static void svm_do_nested_pgfault(paddr_
         __trace_var(TRC_HVM_NPF, 0, sizeof(_d), &_d);
     }
 
-    if ( hvm_hap_nested_page_fault(gpa, 0, ~0ul, 0, 0, 0, 0) )
+    switch (ret) {
+    case 0:
+        break;
+    case 1:
         return;
+    case -1:
+        ASSERT(nestedhvm_enabled(v->domain) && nestedhvm_vcpu_in_guestmode(v));
+        /* inject #VMEXIT(NPF) into guest. */
+        nestedsvm_vmexit_defer(v, VMEXIT_NPF, regs->error_code, gpa);
+        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);
+    gdprintk(XENLOG_ERR,
+         "SVM violation gpa %#"PRIpaddr", mfn %#lx, type %i\n",
+         gpa, mfn_x(mfn), p2mt);
+    domain_crash(v->domain);
 }
 
 static void svm_fpu_dirty_intercept(void)
@@ -1659,6 +1674,8 @@ asmlinkage void svm_vmexit_handler(struc
         struct vmcb_struct *ns_vmcb = nv->nv_vvmcx;
         uint64_t exitinfo1, exitinfo2;
 
+        paging_update_nestedmode(v);
+
         /* Write real exitinfo1 back into virtual vmcb.
          * nestedsvm_check_intercepts() expects to have the correct
          * exitinfo1 value there.
@@ -1948,7 +1965,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/mm/hap/Makefile +1 -0
diff -r 3ab405e67be6 -r 98598880e482 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 +22 -5
diff -r 3ab405e67be6 -r 98598880e482 xen/arch/x86/mm/hap/guest_walk.c
@@ -29,24 +29,32 @@
 #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>
 #include <asm/p2m.h>
 
 unsigned long hap_gva_to_gfn(GUEST_PAGING_LEVELS)(
-    struct vcpu *v, unsigned long gva, uint32_t *pfec)
+    struct vcpu *v, struct p2m_domain *p2m, unsigned long gva, uint32_t *pfec)
 {
-    unsigned long cr3;
+    unsigned long 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 +80,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 */
@@ -122,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 +89 -13

205 lines not on this page · raw message

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

236 lines not on this page · raw message

xen/arch/x86/mm/hap/private.h +19 -3
diff -r 3ab405e67be6 -r 98598880e482 xen/arch/x86/mm/hap/private.h
@@ -23,11 +23,27 @@
 /********************************************/
 /*          GUEST TRANSLATION FUNCS         */
 /********************************************/
-unsigned long hap_gva_to_gfn_2_levels(struct vcpu *v, unsigned long gva, 
+unsigned long hap_gva_to_gfn_2_levels(struct vcpu *v,
+                                     struct p2m_domain *p2m,
+                                     unsigned long gva, 
                                      uint32_t *pfec);
-unsigned long hap_gva_to_gfn_3_levels(struct vcpu *v, unsigned long gva,
+unsigned long hap_gva_to_gfn_3_levels(struct vcpu *v,
+                                     struct p2m_domain *p2m,
+                                     unsigned long gva, 
                                      uint32_t *pfec);
-unsigned long hap_gva_to_gfn_4_levels(struct vcpu *v, unsigned long gva,
+unsigned long hap_gva_to_gfn_4_levels(struct vcpu *v,
+                                     struct p2m_domain *p2m,
+                                     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 +248 -33

428 lines not on this page · raw message

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

70 lines not on this page · raw message

xen/arch/x86/mm/shadow/multi.c +3 -2
diff -r 3ab405e67be6 -r 98598880e482 xen/arch/x86/mm/shadow/multi.c
@@ -3768,7 +3768,8 @@ sh_invlpg(struct vcpu *v, unsigned long 
 
 
 static unsigned long
-sh_gva_to_gfn(struct vcpu *v, unsigned long va, uint32_t *pfec)
+sh_gva_to_gfn(struct vcpu *v, struct p2m_domain *p2m,
+    unsigned long va, uint32_t *pfec)
 /* Called to translate a guest virtual address to what the *guest*
  * pagetables would map it to. */
 {
@@ -4820,7 +4821,7 @@ static mfn_t emulate_gva_to_mfn(struct v
     struct p2m_domain *p2m = p2m_get_hostp2m(v->domain);
 
     /* Translate the VA to a GFN */
-    gfn = sh_gva_to_gfn(v, vaddr, &pfec);
+    gfn = sh_gva_to_gfn(v, p2m, vaddr, &pfec);
     if ( gfn == INVALID_GFN ) 
     {
         if ( is_hvm_vcpu(v) )
xen/include/asm-x86/domain.h +9 -0
diff -r 3ab405e67be6 -r 98598880e482 xen/include/asm-x86/domain.h
@@ -210,6 +210,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 */
@@ -225,6 +227,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;
@@ -258,6 +261,12 @@ 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;
+    int nested_p2m_locker;
+    const char *nested_p2m_function;
+
     /* 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 +6 -6
diff -r 3ab405e67be6 -r 98598880e482 xen/include/asm-x86/hvm/hvm.h
@@ -374,12 +374,12 @@ 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 gpa,
-                                 bool_t gla_valid, unsigned long gla,
-                                 bool_t access_valid, 
-                                 bool_t access_r,
-                                 bool_t access_w,
-                                 bool_t access_x);
+int hvm_hap_nested_page_fault(unsigned long gpa,
+                              bool_t gla_valid, unsigned long gla,
+                              bool_t access_valid, 
+                              bool_t access_r,
+                              bool_t access_w,
+                              bool_t access_x);
 
 #define hvm_msr_tsc_aux(v) ({                                               \
     struct domain *__d = (v)->domain;                                       \
xen/include/asm-x86/hvm/nestedhvm.h +5 -0
diff -r 3ab405e67be6 -r 98598880e482 xen/include/asm-x86/hvm/nestedhvm.h
@@ -60,4 +60,9 @@ unsigned long *nestedhvm_vcpu_iomap_get(
 #define nestedhvm_vmswitch_in_progress(v)   \
     (!!vcpu_nestedhvm((v)).nv_vmswitch_in_progress)
 
+void nestedhvm_vmcx_flushtlb(struct p2m_domain *p2m);
+void nestedhvm_vmcx_flushtlbdomain(struct domain *d);
+
+bool_t nestedhvm_is_n2(struct vcpu *v);
+
 #endif /* _HVM_NESTEDHVM_H */
xen/include/asm-x86/p2m.h +75 -0

119 lines not on this page · raw message

xen/include/asm-x86/paging.h +47 -28

149 lines not on this page · raw message

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

#1 lore raw

Hi,

Looks like you've sorted out shooting down old users of a p2m table. hap_write_p2m_entry still isn't right, though:

xen/arch/x86/mm/hap/hap.c static void · line 868

Quoted 7 lines
xen/arch/x86/mm/hap/hap.c +2 -1
@@ -834,38 +864,81 @@ static void
 hap_write_p2m_entry(struct vcpu *v, unsigned long gfn, l1_pgentry_t *p,
                     mfn_t table_mfn, l1_pgentry_t new, unsigned int level)
 {
-    uint32_t old_flags;
+    struct domain *d = v->domain;
+    uint32_t old_flags = l1e_get_flags(*p);

You have moved this read outside the hap_lock. Please put it back.

Quoted 16 lines
xen/arch/x86/mm/hap/hap.c +14 -1
+    p2m_type_t op2mt = p2m_flags_to_type(old_flags);
 
-    hap_lock(v->domain);
+    /* We know always use the host p2m here, regardless if the vcpu
+     * is in host or guest mode. The vcpu can be in guest mode by
+     * a hypercall which passes a domain and chooses mostly the first
+     * vcpu.
+     * XXX This is the reason why this function can not be used re-used
+     * for updating the nestedp2m. Otherwise, hypercalls would randomly
+     * operate on host p2m and nested p2m.
+     */
+    if ( nestedhvm_enabled(d)
+      && p2m_is_valid(op2mt) )
+    {
+        if ( l1e_get_intpte(new) != l1e_get_intpte(*p) ) {
+            p2m_type_t np2mt = p2m_flags_to_type(l1e_get_flags(new));

xen/arch/x86/mm/hap/hap.c static void · line 892

Quoted 10 lines
xen/arch/x86/mm/hap/hap.c +8 -1
 
-    old_flags = l1e_get_flags(*p);
+            /* Skip flush on vram tracking or XP mode in Win7 hang
+             * very early in the virtual BIOS (long before the bootloader
+             * runs), otherwise. VRAM tracking happens so often that
+             * flushing and fixing the nestedp2m doesn't let XP mode
+             * proceed to boot.
+             */
+            if ( !((op2mt == p2m_ram_rw && np2mt == p2m_ram_logdirty)
+                || (op2mt == p2m_ram_logdirty && np2mt == p2m_ram_rw)) )

That's not safe. If the MFN has changed, you _have_ to flush, even if
you're replacing a logdirty entry with a r/w one. And if you're
replacing a r/w entry with a logdirty one, you _have_ to flush or logdirty doesn't work correctly. If that case is too slow then you should batch the flushes somehow, not just skip them.

Cheers,

Tim.

Quoted 31 lines
xen/arch/x86/mm/hap/hap.c +16 -3
+            {
+                /* This GFN -> MFN is going to get removed. */
+                /* XXX There is a more efficient way to do that
+                 * but it works for now.
+                 * Note, p2m_flush_nestedp2m calls hap_lock() internally.
+                 */
+                p2m_flush_nestedp2m(d);
+            }
+        }
+    }
+
+    hap_lock(d);
+
     safe_write_pte(p, new);
     if ( (old_flags & _PAGE_PRESENT)
          && (level == 1 || (level == 2 && (old_flags & _PAGE_PSE))) )
-             flush_tlb_mask(&v->domain->domain_dirty_cpumask);
+             flush_tlb_mask(&d->domain_dirty_cpumask);
 
 #if CONFIG_PAGING_LEVELS == 3
     /* install P2M in monitor table for PAE Xen */
     if ( level == 3 )
         /* We have written to the p2m l3: need to sync the per-vcpu
          * copies of it in the monitor tables */
-        p2m_install_entry_in_monitors(v->domain, (l3_pgentry_t *)p);
+        p2m_install_entry_in_monitors(d, (l3_pgentry_t *)p);
 #endif
 
-    hap_unlock(v->domain);
+    hap_unlock(d);
 }
Signature · 3 lines
Tim Deegan <[email protected]>
Principal Software Engineer, Xen Platform Team
Citrix Systems UK Ltd.  (Company #02937203, SL9 0BG)
#2 lore raw
In reply to Christoph Egger

This is the new version. I fixed the open items from Tim's last review.

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 1299677057 -3600
Implement Nested-on-Nested.
This allows the guest to run nested guest with hap enabled.

xen/arch/x86/hvm/hvm.c +62 -14
diff -r 3ab405e67be6 -r 98598880e482 xen/arch/x86/hvm/hvm.c
@@ -1170,21 +1170,50 @@ void hvm_inject_exception(unsigned int t
     hvm_funcs.inject_exception(trapnr, errcode, cr2);
 }
 
-bool_t hvm_hap_nested_page_fault(unsigned long gpa,
-                                 bool_t gla_valid,
-                                 unsigned long gla,
-                                 bool_t access_valid,
-                                 bool_t access_r,
-                                 bool_t access_w,
-                                 bool_t access_x)
+int hvm_hap_nested_page_fault(unsigned long gpa,
+                              bool_t gla_valid,
+                              unsigned long gla,
+                              bool_t access_valid,
+                              bool_t access_r,
+                              bool_t access_w,
+                              bool_t access_x)
 {
     unsigned long gfn = gpa >> PAGE_SHIFT;
     p2m_type_t p2mt;
     p2m_access_t p2ma;
     mfn_t mfn;
     struct vcpu *v = current;
-    struct p2m_domain *p2m = p2m_get_hostp2m(v->domain);
-
+    struct p2m_domain *p2m = NULL;
+
+    /* 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) )
+    {
+        int rv;
+
+        /* The vcpu is in guest mode and the l1 guest
+         * uses hap. That means 'gpa' is in l2 guest
+         * physical address 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.
+         */
+        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:
+            return -1;
+        }
+    }
+
+    p2m = p2m_get_hostp2m(v->domain);
     mfn = gfn_to_mfn_type_current(p2m, gfn, &p2mt, &p2ma, p2m_guest);
 
     /* Check access permissions first, then handle faults */
@@ -1328,6 +1357,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);
@@ -1478,8 +1516,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_vmswitch_in_progress(v) && nestedhvm_vcpu_in_guestmode(v) )
+            paging_update_nestedmode(v);
+        else
+            paging_update_paging_modes(v);
+    }
 
     return X86EMUL_OKAY;
 
@@ -1546,8 +1588,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_vmswitch_in_progress(v) && nestedhvm_vcpu_in_guestmode(v) )
+            paging_update_nestedmode(v);
+        else
+            paging_update_paging_modes(v);
+    }
 
     return X86EMUL_OKAY;
 
@@ -2060,7 +2106,7 @@ 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;
@@ -2082,6 +2128,8 @@ static enum hvm_copy_result __hvm_copy(
         return HVMCOPY_unhandleable;
 #endif
 
+    p2m = p2m_get_hostp2m(curr->domain);
+
     while ( todo > 0 )
     {
         count = min_t(int, PAGE_SIZE - (addr & ~PAGE_MASK), todo);
xen/arch/x86/hvm/nestedhvm.c +49 -0

61 lines not on this page · raw message

xen/arch/x86/hvm/svm/entry.S +1 -0
diff -r 3ab405e67be6 -r 98598880e482 xen/arch/x86/hvm/svm/entry.S
@@ -65,6 +65,7 @@ ENTRY(svm_asm_do_resume)
         testl $~0,(r(dx),r(ax),1)
         jnz  .Lsvm_process_softirqs
 
+        call nsvm_p2m_handle_vmrun
         call svm_asid_handle_vmrun
 
         cmpb $0,addr_of(tb_init_done)
xen/arch/x86/hvm/svm/nestedsvm.c +37 -0
diff -r 3ab405e67be6 -r 98598880e482 xen/arch/x86/hvm/svm/nestedsvm.c
@@ -26,6 +26,7 @@
 #include <asm/hvm/svm/svmdebug.h>
 #include <asm/paging.h> /* paging_mode_hap */
 #include <asm/event.h> /* for local_event_delivery_(en|dis)able */
+#include <asm/p2m.h> /* p2m_get_pagetable, p2m_get_nestedp2m */
 
 static void
 nestedsvm_vcpu_clgi(struct vcpu *v)
@@ -320,6 +321,18 @@ static int nsvm_vmrun_permissionmap(stru
     return 0;
 }
 
+static void nestedsvm_vmcb_set_nestedp2m(struct vcpu *v,
+    struct vmcb_struct *vvmcb, struct vmcb_struct *n2vmcb)
+{
+    struct p2m_domain *p2m;
+
+    ASSERT(v != NULL);
+    ASSERT(vvmcb != NULL);
+    ASSERT(n2vmcb != NULL);
+    p2m = p2m_get_nestedp2m(v, vvmcb->_h_cr3);
+    n2vmcb->_h_cr3 = pagetable_get_paddr(p2m_get_pagetable(p2m));
+}
+
 static int nsvm_vmcb_prepare4vmrun(struct vcpu *v, struct cpu_user_regs *regs)
 {
     struct nestedvcpu *nv = &vcpu_nestedhvm(v);
@@ -475,6 +488,9 @@ static int nsvm_vmcb_prepare4vmrun(struc
     /* Nested paging mode */
     if (nestedhvm_paging_mode_hap(v)) {
         /* host nested paging + guest nested paging. */
+        n2vmcb->_np_enable = 1;
+
+        nestedsvm_vmcb_set_nestedp2m(v, ns_vmcb, n2vmcb);
 
         /* hvm_set_cr3() below sets v->arch.hvm_vcpu.guest_cr[3] for us. */
         rc = hvm_set_cr3(ns_vmcb->_cr3);
@@ -1323,6 +1339,27 @@ asmlinkage void nsvm_vcpu_switch(struct 
     }
 }
 
+/* This runs after VMRUN emulation. It is possible
+ * that an other (v)cpu flushed the assigned nestedp2m
+ * during the VMRUN emulation. Check for this case
+ * and get a new one.
+ * Caller must ensure the GIF is cleared.
+ */
+asmlinkage void nsvm_p2m_handle_vmrun(void)
+{
+    struct vcpu *v = current;
+    struct nestedvcpu *nv;
+
+    if (!nestedhvm_enabled(v->domain))
+        return;
+
+    nv = &vcpu_nestedhvm(v);
+    if (nv->nv_p2m == NULL
+      && nestedhvm_vcpu_in_guestmode(v)
+      && nestedhvm_paging_mode_hap(v))
+        nestedsvm_vmcb_set_nestedp2m(v, nv->nv_vvmcx, nv->nv_n2vmcx);
+}
+
 /* Interrupts, Virtual GIF */
 int
 nestedsvm_vcpu_interrupt(struct vcpu *v, const struct hvm_intack intack)
xen/arch/x86/hvm/svm/svm.c +25 -8
diff -r 3ab405e67be6 -r 98598880e482 xen/arch/x86/hvm/svm/svm.c
@@ -1014,14 +1014,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;
+    struct p2m_domain *p2m = NULL;
 
-    p2m = p2m_get_hostp2m(current->domain);
+    ret = hvm_hap_nested_page_fault(gpa, 0, ~0ul, 0, 0, 0, 0);
 
     if ( tb_init_done )
     {
@@ -1032,6 +1034,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));
@@ -1039,14 +1042,26 @@ static void svm_do_nested_pgfault(paddr_
         __trace_var(TRC_HVM_NPF, 0, sizeof(_d), &_d);
     }
 
-    if ( hvm_hap_nested_page_fault(gpa, 0, ~0ul, 0, 0, 0, 0) )
+    switch (ret) {
+    case 0:
+        break;
+    case 1:
         return;
+    case -1:
+        ASSERT(nestedhvm_enabled(v->domain) && nestedhvm_vcpu_in_guestmode(v));
+        /* inject #VMEXIT(NPF) into guest. */
+        nestedsvm_vmexit_defer(v, VMEXIT_NPF, regs->error_code, gpa);
+        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);
+    gdprintk(XENLOG_ERR,
+         "SVM violation gpa %#"PRIpaddr", mfn %#lx, type %i\n",
+         gpa, mfn_x(mfn), p2mt);
+    domain_crash(v->domain);
 }
 
 static void svm_fpu_dirty_intercept(void)
@@ -1659,6 +1674,8 @@ asmlinkage void svm_vmexit_handler(struc
         struct vmcb_struct *ns_vmcb = nv->nv_vvmcx;
         uint64_t exitinfo1, exitinfo2;
 
+        paging_update_nestedmode(v);
+
         /* Write real exitinfo1 back into virtual vmcb.
          * nestedsvm_check_intercepts() expects to have the correct
          * exitinfo1 value there.
@@ -1948,7 +1965,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/mm/hap/Makefile +1 -0
diff -r 3ab405e67be6 -r 98598880e482 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 +22 -5
diff -r 3ab405e67be6 -r 98598880e482 xen/arch/x86/mm/hap/guest_walk.c
@@ -29,24 +29,32 @@
 #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>
 #include <asm/p2m.h>
 
 unsigned long hap_gva_to_gfn(GUEST_PAGING_LEVELS)(
-    struct vcpu *v, unsigned long gva, uint32_t *pfec)
+    struct vcpu *v, struct p2m_domain *p2m, unsigned long gva, uint32_t *pfec)
 {
-    unsigned long cr3;
+    unsigned long 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 +80,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 */
@@ -122,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 +89 -13

205 lines not on this page · raw message

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

236 lines not on this page · raw message

xen/arch/x86/mm/hap/private.h +19 -3
diff -r 3ab405e67be6 -r 98598880e482 xen/arch/x86/mm/hap/private.h
@@ -23,11 +23,27 @@
 /********************************************/
 /*          GUEST TRANSLATION FUNCS         */
 /********************************************/
-unsigned long hap_gva_to_gfn_2_levels(struct vcpu *v, unsigned long gva, 
+unsigned long hap_gva_to_gfn_2_levels(struct vcpu *v,
+                                     struct p2m_domain *p2m,
+                                     unsigned long gva, 
                                      uint32_t *pfec);
-unsigned long hap_gva_to_gfn_3_levels(struct vcpu *v, unsigned long gva,
+unsigned long hap_gva_to_gfn_3_levels(struct vcpu *v,
+                                     struct p2m_domain *p2m,
+                                     unsigned long gva, 
                                      uint32_t *pfec);
-unsigned long hap_gva_to_gfn_4_levels(struct vcpu *v, unsigned long gva,
+unsigned long hap_gva_to_gfn_4_levels(struct vcpu *v,
+                                     struct p2m_domain *p2m,
+                                     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 +248 -33

428 lines not on this page · raw message

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

70 lines not on this page · raw message

xen/arch/x86/mm/shadow/multi.c +3 -2
diff -r 3ab405e67be6 -r 98598880e482 xen/arch/x86/mm/shadow/multi.c
@@ -3768,7 +3768,8 @@ sh_invlpg(struct vcpu *v, unsigned long 
 
 
 static unsigned long
-sh_gva_to_gfn(struct vcpu *v, unsigned long va, uint32_t *pfec)
+sh_gva_to_gfn(struct vcpu *v, struct p2m_domain *p2m,
+    unsigned long va, uint32_t *pfec)
 /* Called to translate a guest virtual address to what the *guest*
  * pagetables would map it to. */
 {
@@ -4820,7 +4821,7 @@ static mfn_t emulate_gva_to_mfn(struct v
     struct p2m_domain *p2m = p2m_get_hostp2m(v->domain);
 
     /* Translate the VA to a GFN */
-    gfn = sh_gva_to_gfn(v, vaddr, &pfec);
+    gfn = sh_gva_to_gfn(v, p2m, vaddr, &pfec);
     if ( gfn == INVALID_GFN ) 
     {
         if ( is_hvm_vcpu(v) )
xen/include/asm-x86/domain.h +9 -0
diff -r 3ab405e67be6 -r 98598880e482 xen/include/asm-x86/domain.h
@@ -210,6 +210,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 */
@@ -225,6 +227,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;
@@ -258,6 +261,12 @@ 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;
+    int nested_p2m_locker;
+    const char *nested_p2m_function;
+
     /* 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 +6 -6
diff -r 3ab405e67be6 -r 98598880e482 xen/include/asm-x86/hvm/hvm.h
@@ -374,12 +374,12 @@ 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 gpa,
-                                 bool_t gla_valid, unsigned long gla,
-                                 bool_t access_valid, 
-                                 bool_t access_r,
-                                 bool_t access_w,
-                                 bool_t access_x);
+int hvm_hap_nested_page_fault(unsigned long gpa,
+                              bool_t gla_valid, unsigned long gla,
+                              bool_t access_valid, 
+                              bool_t access_r,
+                              bool_t access_w,
+                              bool_t access_x);
 
 #define hvm_msr_tsc_aux(v) ({                                               \
     struct domain *__d = (v)->domain;                                       \
xen/include/asm-x86/hvm/nestedhvm.h +5 -0
diff -r 3ab405e67be6 -r 98598880e482 xen/include/asm-x86/hvm/nestedhvm.h
@@ -60,4 +60,9 @@ unsigned long *nestedhvm_vcpu_iomap_get(
 #define nestedhvm_vmswitch_in_progress(v)   \
     (!!vcpu_nestedhvm((v)).nv_vmswitch_in_progress)
 
+void nestedhvm_vmcx_flushtlb(struct p2m_domain *p2m);
+void nestedhvm_vmcx_flushtlbdomain(struct domain *d);
+
+bool_t nestedhvm_is_n2(struct vcpu *v);
+
 #endif /* _HVM_NESTEDHVM_H */
xen/include/asm-x86/p2m.h +75 -0

119 lines not on this page · raw message

xen/include/asm-x86/paging.h +47 -28

149 lines not on this page · raw message

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

#3 lore raw
Quoted 2 lines · Christoph Egger

This is the new version. I fixed the open items from Tim's last review.

Sorry, I mistakenly resent an older version and noticed it just now. This time this is the latest version.

Christoph

Signature · 6 lines
---to satisfy European Law for business letters:
Advanced Micro Devices GmbH
Einsteinring 24, 85689 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 1302011049 -7200
Implement Nested-on-Nested.
This allows the guest to run nested guest with hap enabled.

xen/arch/x86/hvm/hvm.c +62 -14
diff -r cfde4384be14 -r 28809c365861 xen/arch/x86/hvm/hvm.c
@@ -1170,21 +1170,50 @@ void hvm_inject_exception(unsigned int t
     hvm_funcs.inject_exception(trapnr, errcode, cr2);
 }
 
-bool_t hvm_hap_nested_page_fault(unsigned long gpa,
-                                 bool_t gla_valid,
-                                 unsigned long gla,
-                                 bool_t access_valid,
-                                 bool_t access_r,
-                                 bool_t access_w,
-                                 bool_t access_x)
+int hvm_hap_nested_page_fault(unsigned long gpa,
+                              bool_t gla_valid,
+                              unsigned long gla,
+                              bool_t access_valid,
+                              bool_t access_r,
+                              bool_t access_w,
+                              bool_t access_x)
 {
     unsigned long gfn = gpa >> PAGE_SHIFT;
     p2m_type_t p2mt;
     p2m_access_t p2ma;
     mfn_t mfn;
     struct vcpu *v = current;
-    struct p2m_domain *p2m = p2m_get_hostp2m(v->domain);
-
+    struct p2m_domain *p2m = NULL;
+
+    /* 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) )
+    {
+        int rv;
+
+        /* The vcpu is in guest mode and the l1 guest
+         * uses hap. That means 'gpa' is in l2 guest
+         * physical address 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.
+         */
+        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:
+            return -1;
+        }
+    }
+
+    p2m = p2m_get_hostp2m(v->domain);
     mfn = gfn_to_mfn_type_current(p2m, gfn, &p2mt, &p2ma, p2m_guest);
 
     /* Check access permissions first, then handle faults */
@@ -1328,6 +1357,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);
@@ -1478,8 +1516,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_vmswitch_in_progress(v) && nestedhvm_vcpu_in_guestmode(v) )
+            paging_update_nestedmode(v);
+        else
+            paging_update_paging_modes(v);
+    }
 
     return X86EMUL_OKAY;
 
@@ -1546,8 +1588,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_vmswitch_in_progress(v) && nestedhvm_vcpu_in_guestmode(v) )
+            paging_update_nestedmode(v);
+        else
+            paging_update_paging_modes(v);
+    }
 
     return X86EMUL_OKAY;
 
@@ -2060,7 +2106,7 @@ 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;
@@ -2082,6 +2128,8 @@ static enum hvm_copy_result __hvm_copy(
         return HVMCOPY_unhandleable;
 #endif
 
+    p2m = p2m_get_hostp2m(curr->domain);
+
     while ( todo > 0 )
     {
         count = min_t(int, PAGE_SIZE - (addr & ~PAGE_MASK), todo);
xen/arch/x86/hvm/nestedhvm.c +49 -0

61 lines not on this page · raw message

xen/arch/x86/hvm/svm/nestedsvm.c +29 -1
diff -r cfde4384be14 -r 28809c365861 xen/arch/x86/hvm/svm/nestedsvm.c
@@ -26,6 +26,7 @@
 #include <asm/hvm/svm/svmdebug.h>
 #include <asm/paging.h> /* paging_mode_hap */
 #include <asm/event.h> /* for local_event_delivery_(en|dis)able */
+#include <asm/p2m.h> /* p2m_get_pagetable, p2m_get_nestedp2m */
 
 static void
 nestedsvm_vcpu_clgi(struct vcpu *v)
@@ -320,6 +321,18 @@ static int nsvm_vmrun_permissionmap(stru
     return 0;
 }
 
+static void nestedsvm_vmcb_set_nestedp2m(struct vcpu *v,
+    struct vmcb_struct *vvmcb, struct vmcb_struct *n2vmcb)
+{
+    struct p2m_domain *p2m;
+
+    ASSERT(v != NULL);
+    ASSERT(vvmcb != NULL);
+    ASSERT(n2vmcb != NULL);
+    p2m = p2m_get_nestedp2m(v, vvmcb->_h_cr3);
+    n2vmcb->_h_cr3 = pagetable_get_paddr(p2m_get_pagetable(p2m));
+}
+
 static int nsvm_vmcb_prepare4vmrun(struct vcpu *v, struct cpu_user_regs *regs)
 {
     struct nestedvcpu *nv = &vcpu_nestedhvm(v);
@@ -475,6 +488,9 @@ static int nsvm_vmcb_prepare4vmrun(struc
     /* Nested paging mode */
     if (nestedhvm_paging_mode_hap(v)) {
         /* host nested paging + guest nested paging. */
+        n2vmcb->_np_enable = 1;
+
+        nestedsvm_vmcb_set_nestedp2m(v, ns_vmcb, n2vmcb);
 
         /* hvm_set_cr3() below sets v->arch.hvm_vcpu.guest_cr[3] for us. */
         rc = hvm_set_cr3(ns_vmcb->_cr3);
@@ -1318,8 +1334,20 @@ asmlinkage void nsvm_vcpu_switch(struct 
         ret = nsvm_vcpu_vmrun(v, regs);
         if (ret < 0)
             goto vmexit;
+
+        ASSERT(nestedhvm_vcpu_in_guestmode(v));
         nv->nv_vmentry_pending = 0;
-        return;
+    }
+
+    if (nestedhvm_vcpu_in_guestmode(v)
+       && nestedhvm_paging_mode_hap(v))
+    {
+        /* In case left the l2 guest due to a physical interrupt (e.g. IPI)
+         * that is not for the l1 guest then we continue running the l2 guest
+         * but check if the nestedp2m is still valid.
+         */
+        if (nv->nv_p2m == NULL)
+            nestedsvm_vmcb_set_nestedp2m(v, nv->nv_vvmcx, nv->nv_n2vmcx);
     }
 }
 
xen/arch/x86/hvm/svm/svm.c +25 -8
diff -r cfde4384be14 -r 28809c365861 xen/arch/x86/hvm/svm/svm.c
@@ -1014,14 +1014,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;
+    struct p2m_domain *p2m = NULL;
 
-    p2m = p2m_get_hostp2m(current->domain);
+    ret = hvm_hap_nested_page_fault(gpa, 0, ~0ul, 0, 0, 0, 0);
 
     if ( tb_init_done )
     {
@@ -1032,6 +1034,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));
@@ -1039,14 +1042,26 @@ static void svm_do_nested_pgfault(paddr_
         __trace_var(TRC_HVM_NPF, 0, sizeof(_d), &_d);
     }
 
-    if ( hvm_hap_nested_page_fault(gpa, 0, ~0ul, 0, 0, 0, 0) )
+    switch (ret) {
+    case 0:
+        break;
+    case 1:
         return;
+    case -1:
+        ASSERT(nestedhvm_enabled(v->domain) && nestedhvm_vcpu_in_guestmode(v));
+        /* inject #VMEXIT(NPF) into guest. */
+        nestedsvm_vmexit_defer(v, VMEXIT_NPF, regs->error_code, gpa);
+        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);
+    gdprintk(XENLOG_ERR,
+         "SVM violation gpa %#"PRIpaddr", mfn %#lx, type %i\n",
+         gpa, mfn_x(mfn), p2mt);
+    domain_crash(v->domain);
 }
 
 static void svm_fpu_dirty_intercept(void)
@@ -1659,6 +1674,8 @@ asmlinkage void svm_vmexit_handler(struc
         struct vmcb_struct *ns_vmcb = nv->nv_vvmcx;
         uint64_t exitinfo1, exitinfo2;
 
+        paging_update_nestedmode(v);
+
         /* Write real exitinfo1 back into virtual vmcb.
          * nestedsvm_check_intercepts() expects to have the correct
          * exitinfo1 value there.
@@ -1948,7 +1965,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/mm/hap/Makefile +1 -0
diff -r cfde4384be14 -r 28809c365861 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 +22 -5
diff -r cfde4384be14 -r 28809c365861 xen/arch/x86/mm/hap/guest_walk.c
@@ -29,24 +29,32 @@
 #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>
 #include <asm/p2m.h>
 
 unsigned long hap_gva_to_gfn(GUEST_PAGING_LEVELS)(
-    struct vcpu *v, unsigned long gva, uint32_t *pfec)
+    struct vcpu *v, struct p2m_domain *p2m, unsigned long gva, uint32_t *pfec)
 {
-    unsigned long cr3;
+    unsigned long 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 +80,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 */
@@ -122,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 +76 -11

192 lines not on this page · raw message

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

236 lines not on this page · raw message

xen/arch/x86/mm/hap/private.h +19 -3
diff -r cfde4384be14 -r 28809c365861 xen/arch/x86/mm/hap/private.h
@@ -23,11 +23,27 @@
 /********************************************/
 /*          GUEST TRANSLATION FUNCS         */
 /********************************************/
-unsigned long hap_gva_to_gfn_2_levels(struct vcpu *v, unsigned long gva, 
+unsigned long hap_gva_to_gfn_2_levels(struct vcpu *v,
+                                     struct p2m_domain *p2m,
+                                     unsigned long gva, 
                                      uint32_t *pfec);
-unsigned long hap_gva_to_gfn_3_levels(struct vcpu *v, unsigned long gva,
+unsigned long hap_gva_to_gfn_3_levels(struct vcpu *v,
+                                     struct p2m_domain *p2m,
+                                     unsigned long gva, 
                                      uint32_t *pfec);
-unsigned long hap_gva_to_gfn_4_levels(struct vcpu *v, unsigned long gva,
+unsigned long hap_gva_to_gfn_4_levels(struct vcpu *v,
+                                     struct p2m_domain *p2m,
+                                     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 +251 -33

431 lines not on this page · raw message

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

70 lines not on this page · raw message

xen/arch/x86/mm/shadow/multi.c +3 -18
diff -r cfde4384be14 -r 28809c365861 xen/arch/x86/mm/shadow/multi.c
@@ -837,22 +837,6 @@ shadow_write_entries(void *d, void *s, i
     if ( map != NULL ) sh_unmap_domain_page(map);
 }
 
-static inline int
-perms_strictly_increased(u32 old_flags, u32 new_flags) 
-/* Given the flags of two entries, are the new flags a strict
- * increase in rights over the old ones? */
-{
-    u32 of = old_flags & (_PAGE_PRESENT|_PAGE_RW|_PAGE_USER|_PAGE_NX_BIT);
-    u32 nf = new_flags & (_PAGE_PRESENT|_PAGE_RW|_PAGE_USER|_PAGE_NX_BIT);
-    /* Flip the NX bit, since it's the only one that decreases rights;
-     * we calculate as if it were an "X" bit. */
-    of ^= _PAGE_NX_BIT;
-    nf ^= _PAGE_NX_BIT;
-    /* If the changed bits are all set in the new flags, then rights strictly 
-     * increased between old and new. */
-    return ((of | (of ^ nf)) == nf);
-}
-
 /* type is only used to distinguish grant map pages from ordinary RAM
  * i.e. non-p2m_is_grant() pages are treated as p2m_ram_rw.  */
 static int inline
@@ -3768,7 +3752,8 @@ sh_invlpg(struct vcpu *v, unsigned long 
 
 
 static unsigned long
-sh_gva_to_gfn(struct vcpu *v, unsigned long va, uint32_t *pfec)
+sh_gva_to_gfn(struct vcpu *v, struct p2m_domain *p2m,
+    unsigned long va, uint32_t *pfec)
 /* Called to translate a guest virtual address to what the *guest*
  * pagetables would map it to. */
 {
@@ -4820,7 +4805,7 @@ static mfn_t emulate_gva_to_mfn(struct v
     struct p2m_domain *p2m = p2m_get_hostp2m(v->domain);
 
     /* Translate the VA to a GFN */
-    gfn = sh_gva_to_gfn(v, vaddr, &pfec);
+    gfn = sh_gva_to_gfn(v, p2m, vaddr, &pfec);
     if ( gfn == INVALID_GFN ) 
     {
         if ( is_hvm_vcpu(v) )
xen/include/asm-x86/domain.h +9 -0
diff -r cfde4384be14 -r 28809c365861 xen/include/asm-x86/domain.h
@@ -210,6 +210,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 */
@@ -225,6 +227,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;
@@ -258,6 +261,12 @@ 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;
+    int nested_p2m_locker;
+    const char *nested_p2m_function;
+
     /* 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 +6 -6
diff -r cfde4384be14 -r 28809c365861 xen/include/asm-x86/hvm/hvm.h
@@ -374,12 +374,12 @@ 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 gpa,
-                                 bool_t gla_valid, unsigned long gla,
-                                 bool_t access_valid, 
-                                 bool_t access_r,
-                                 bool_t access_w,
-                                 bool_t access_x);
+int hvm_hap_nested_page_fault(unsigned long gpa,
+                              bool_t gla_valid, unsigned long gla,
+                              bool_t access_valid, 
+                              bool_t access_r,
+                              bool_t access_w,
+                              bool_t access_x);
 
 #define hvm_msr_tsc_aux(v) ({                                               \
     struct domain *__d = (v)->domain;                                       \
xen/include/asm-x86/hvm/nestedhvm.h +5 -0
diff -r cfde4384be14 -r 28809c365861 xen/include/asm-x86/hvm/nestedhvm.h
@@ -60,4 +60,9 @@ unsigned long *nestedhvm_vcpu_iomap_get(
 #define nestedhvm_vmswitch_in_progress(v)   \
     (!!vcpu_nestedhvm((v)).nv_vmswitch_in_progress)
 
+void nestedhvm_vmcx_flushtlb(struct p2m_domain *p2m);
+void nestedhvm_vmcx_flushtlbdomain(struct domain *d);
+
+bool_t nestedhvm_is_n2(struct vcpu *v);
+
 #endif /* _HVM_NESTEDHVM_H */
xen/include/asm-x86/p2m.h +75 -0

119 lines not on this page · raw message

xen/include/asm-x86/page.h +17 -0
diff -r cfde4384be14 -r 28809c365861 xen/include/asm-x86/page.h
@@ -391,6 +391,23 @@ static inline uint32_t cacheattr_to_pte_
     return ((cacheattr & 4) << 5) | ((cacheattr & 3) << 3);
 }
 
+/* return true if permission increased */
+static inline bool_t
+perms_strictly_increased(uint32_t old_flags, uint32_t new_flags)
+/* Given the flags of two entries, are the new flags a strict
+ * increase in rights over the old ones? */
+{
+    uint32_t of = old_flags & (_PAGE_PRESENT|_PAGE_RW|_PAGE_USER|_PAGE_NX_BIT);
+    uint32_t nf = new_flags & (_PAGE_PRESENT|_PAGE_RW|_PAGE_USER|_PAGE_NX_BIT);
+    /* Flip the NX bit, since it's the only one that decreases rights;
+     * we calculate as if it were an "X" bit. */
+    of ^= _PAGE_NX_BIT;
+    nf ^= _PAGE_NX_BIT;
+    /* If the changed bits are all set in the new flags, then rights strictly
+     * increased between old and new. */
+    return ((of | (of ^ nf)) == nf);
+}
+
 #endif /* !__ASSEMBLY__ */
 
 #define PAGE_ALIGN(x) (((x) + PAGE_SIZE - 1) & PAGE_MASK)
xen/include/asm-x86/paging.h +47 -28

149 lines not on this page · raw message

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

#4 lore raw
Quoted 6 lines · Christoph Egger
Quoted 2 lines · Christoph Egger

This is the new version. I fixed the open items from Tim's last review.

Sorry, I mistakenly resent an older version and noticed it just now. This time this is the latest version.

Thank you. I have applied the full series, which should appear as 23157--23168 in the staging tree soon. I had to forward-port a few small things, and I also fixed up one last race condition in the remote shootdowns, as 23170:86f87da1445a, so please check that I havent broken anything in your tests.

Cheers,

Tim.

Signature · 3 lines
Tim Deegan <[email protected]>
Principal Software Engineer, Xen Platform Team
Citrix Systems UK Ltd.  (Company #02937203, SL9 0BG)
#5 lore raw
Quoted 5 lines · Tim Deegan
Quoted 4 lines · Christoph Egger
Quoted 2 lines · Christoph Egger

This is the new version. I fixed the open items from Tim's last review.

Quoted 8 lines
Quoted 2 lines

Sorry, I mistakenly resent an older version and noticed it just now. This time this is the latest version.

Thank you. I have applied the full series, which should appear as 23157--23168 in the staging tree soon. I had to forward-port a few small things, and I also fixed up one last race condition in the remote shootdowns, as 23170:86f87da1445a, so please check that I havent broken anything in your tests.

Thank you for applying the patch series. xentrace was broken again but that wasn't you. I already submitted a fix for this.

Christoph

Signature · 6 lines
---to satisfy European Law for business letters:
Advanced Micro Devices GmbH
Einsteinring 24, 85689 Dornach b. Muenchen
Geschaeftsfuehrer: Alberto Bozzo, Andrew Bowd
Sitz: Dornach, Gemeinde Aschheim, Landkreis Muenchen
Registergericht Muenchen, HRB Nr. 43632
#6 lore raw
In reply to Christoph Egger
Quoted 16 lines
Quoted 1 line
Quoted 1 line · Christoph Egger

diff -r cfde4384be14 -r 28809c365861 xen/include/asm-x86/domain.h

xen/include/asm-x86/domain.h +1 -0
...
@@ -225,6 +227,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;
@@ -258,6 +261,12 @@ struct arch_domain
    struct paging_domain paging;
    struct p2m_domain *p2m;

xen/include/asm-x86/domain.h struct arch_domain · line 272

Quoted 10 lines
xen/include/asm-x86/domain.h +6 -0
 
+    /* nestedhvm: translate l2 guest physical to host physical */
+    struct p2m_domain *nested_p2m[MAX_NESTEDP2M];
+    spinlock_t nested_p2m_lock;
+    int nested_p2m_locker;
+    const char *nested_p2m_function;
+
    /* NB. protected by d->event_lock and by irq_desc[irq].lock */
    int *irq_pirq;
    int *pirq_irq;

Was there a specific reason to add this to struct arch_domain instead of struct hvm_domain? I.e. can any pf these fields be used on pv (or idle) domains?

Thanks, Jan

#7 lore raw
Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · Christoph Egger

diff -r cfde4384be14 -r 28809c365861 xen/include/asm-x86/domain.h

xen/include/asm-x86/domain.h +1 -0
...
@@ -225,6 +227,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;
@@ -258,6 +261,12 @@ struct arch_domain
     struct paging_domain paging;
     struct p2m_domain *p2m;
 
Quoted 13 lines

xen/include/asm-x86/domain.h struct arch_domain · line 272

Quoted 9 lines
xen/include/asm-x86/domain.h +6 -0
+    /* nestedhvm: translate l2 guest physical to host physical */
+    struct p2m_domain *nested_p2m[MAX_NESTEDP2M];
+    spinlock_t nested_p2m_lock;
+    int nested_p2m_locker;
+    const char *nested_p2m_function;
+
     /* NB. protected by d->event_lock and by irq_desc[irq].lock */
     int *irq_pirq;
     int *pirq_irq;

Was there a specific reason to add this to struct arch_domain instead of struct hvm_domain? I.e. can any pf these fields be used on pv (or idle) domains?

The reason is that there is already a 'struct p2m_domain *p2m' field. If that can be moved to struct hvm_domain then nested_p2m can definitely move over to there, too.

Christoph

Signature · 6 lines
---to satisfy European Law for business letters:
Advanced Micro Devices GmbH
Einsteinring 24, 85689 Dornach b. Muenchen
Geschaeftsfuehrer: Alberto Bozzo, Andrew Bowd
Sitz: Dornach, Gemeinde Aschheim, Landkreis Muenchen
Registergericht Muenchen, HRB Nr. 43632
#8 lore raw
Quoted 33 lines
Quoted 1 line
Quoted 1 line · Christoph Egger
Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · Christoph Egger

diff -r cfde4384be14 -r 28809c365861 xen/include/asm-x86/domain.h

xen/include/asm-x86/domain.h +1 -0
...
@@ -225,6 +227,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;
@@ -258,6 +261,12 @@ struct arch_domain
     struct paging_domain paging;
     struct p2m_domain *p2m;
 
Quoted 13 lines

xen/include/asm-x86/domain.h struct arch_domain · line 272

Quoted 9 lines
xen/include/asm-x86/domain.h +6 -0
+    /* nestedhvm: translate l2 guest physical to host physical */
+    struct p2m_domain *nested_p2m[MAX_NESTEDP2M];
+    spinlock_t nested_p2m_lock;
+    int nested_p2m_locker;
+    const char *nested_p2m_function;
+
     /* NB. protected by d->event_lock and by irq_desc[irq].lock */
     int *irq_pirq;
     int *pirq_irq;

Was there a specific reason to add this to struct arch_domain instead of struct hvm_domain? I.e. can any pf these fields be used on pv (or idle) domains?

Quoted 3 lines

The reason is that there is already a 'struct p2m_domain *p2m' field. If that can be moved to struct hvm_domain then nested_p2m can definitely move over to there, too.

No, I don't think these are connected - a pv domain can still require a p2m (e.g. for the iommu), but I would have thought that the nesting stuff doesn't apply there.

Jan