Skip to contentSkip to conversation

Add virtual EPT support Xen.

PATCH Superseded [email protected] 3 participants 27 messages Dec 10, 2012 – Dec 17, 2012

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover Add virtual EPT support Xen.

From: Zhang Xiantao <[email protected]>

With virtual EPT support, L1 hyerpvisor can use EPT hardware for L2 guest's memory virtualization. In this way, L2 guest's performance can be improved sharply. According to our testing, some benchmarks can show > 5x performance gain.

Zhang Xiantao (11):
nestedhap: Change hostcr3 and p2m->cr3 to meaningful words
nestedhap: Change nested p2m's walker to vendor-specific
nested_ept: Implement guest ept's walker
nested_ept: Add permission check for success case
EPT: Make ept data structure or operations neutral
nEPT: Try to enable EPT paging for L2 guest.
nEPT: Sync PDPTR fields if L2 guest in PAE paging mode
nEPT: Use minimal permission for nested p2m.
nEPT: handle invept instruction from L1 VMM
nEPT: expost EPT capablity to L1 VMM
nVMX: Expose VPID capability to nested VMM.

 xen/arch/x86/hvm/hvm.c                  |    7 +-
 xen/arch/x86/hvm/svm/nestedsvm.c        |   31 +++
 xen/arch/x86/hvm/svm/svm.c              |    3 +-
 xen/arch/x86/hvm/vmx/vmcs.c             |    2 +-
 xen/arch/x86/hvm/vmx/vmx.c              |   76 +++++---
 xen/arch/x86/hvm/vmx/vvmx.c             |  208 ++++++++++++++++++-
 xen/arch/x86/mm/guest_walk.c            |   12 +-
 xen/arch/x86/mm/hap/Makefile            |    1 +
 xen/arch/x86/mm/hap/nested_ept.c        |  345 +++++++++++++++++++++++++++++++
 xen/arch/x86/mm/hap/nested_hap.c        |   79 +++----
 xen/arch/x86/mm/mm-locks.h              |    2 +-
 xen/arch/x86/mm/p2m-ept.c               |   96 +++++++--
 xen/arch/x86/mm/p2m.c                   |   44 +++--
 xen/arch/x86/mm/shadow/multi.c          |    2 +-
 xen/include/asm-x86/guest_pt.h          |    8 +
 xen/include/asm-x86/hvm/hvm.h           |    9 +-
 xen/include/asm-x86/hvm/nestedhvm.h     |    1 +
 xen/include/asm-x86/hvm/svm/nestedsvm.h |    3 +
 xen/include/asm-x86/hvm/vmx/vmcs.h      |   31 ++-
 xen/include/asm-x86/hvm/vmx/vmx.h       |    6 +-
 xen/include/asm-x86/hvm/vmx/vvmx.h      |   29 +++-
 xen/include/asm-x86/p2m.h               |   17 +-
 22 files changed, 859 insertions(+), 153 deletions(-)
 create mode 100644 xen/arch/x86/mm/hap/nested_ept.c
#1 lore raw

PATCH 1/11 nestedhap: Change hostcr3 and p2m->cr3 to meaningful words

From: Zhang Xiantao <[email protected]>

VMX doesn't have the concept about host cr3 for nested p2m, and only SVM has, so change it to netural words.

---
 xen/arch/x86/hvm/hvm.c             |    6 +++---
 xen/arch/x86/hvm/svm/svm.c         |    2 +-
 xen/arch/x86/hvm/vmx/vmx.c         |    2 +-
 xen/arch/x86/hvm/vmx/vvmx.c        |    2 +-
 xen/arch/x86/mm/hap/nested_hap.c   |   15 ++++++++-------
 xen/arch/x86/mm/mm-locks.h         |    2 +-
 xen/arch/x86/mm/p2m.c              |   26 +++++++++++++-------------
 xen/include/asm-x86/hvm/hvm.h      |    4 ++--
 xen/include/asm-x86/hvm/vmx/vvmx.h |    2 +-
 xen/include/asm-x86/p2m.h          |   16 ++++++++--------
 10 files changed, 39 insertions(+), 38 deletions(-)
xen/arch/x86/hvm/hvm.c +3 -3
index b6026d7..85bc9be 100644
@@ -4536,10 +4536,10 @@ uint64_t nhvm_vcpu_guestcr3(struct vcpu *v)
     return -EOPNOTSUPP;
 }
 
-uint64_t nhvm_vcpu_hostcr3(struct vcpu *v)
+uint64_t nhvm_vcpu_p2m_base(struct vcpu *v)
 {
-    if (hvm_funcs.nhvm_vcpu_hostcr3)
-        return hvm_funcs.nhvm_vcpu_hostcr3(v);
+    if (hvm_funcs.nhvm_vcpu_p2m_base)
+        return hvm_funcs.nhvm_vcpu_p2m_base(v);
     return -EOPNOTSUPP;
 }
 
xen/arch/x86/hvm/svm/svm.c +1 -1
index 4c4abfc..6c469ec 100644
@@ -2003,7 +2003,7 @@ static struct hvm_function_table __read_mostly svm_function_table = {
     .nhvm_vcpu_vmexit = nsvm_vcpu_vmexit_inject,
     .nhvm_vcpu_vmexit_trap = nsvm_vcpu_vmexit_trap,
     .nhvm_vcpu_guestcr3 = nsvm_vcpu_guestcr3,
-    .nhvm_vcpu_hostcr3 = nsvm_vcpu_hostcr3,
+    .nhvm_vcpu_p2m_base = nsvm_vcpu_hostcr3,
     .nhvm_vcpu_asid = nsvm_vcpu_asid,
     .nhvm_vmcx_guest_intercepts_trap = nsvm_vmcb_guest_intercepts_trap,
     .nhvm_vmcx_hap_enabled = nsvm_vmcb_hap_enabled,
xen/arch/x86/hvm/vmx/vmx.c +1 -1
index 9fb9562..47d8ca6 100644
@@ -1504,7 +1504,7 @@ static struct hvm_function_table __read_mostly vmx_function_table = {
     .nhvm_vcpu_destroy    = nvmx_vcpu_destroy,
     .nhvm_vcpu_reset      = nvmx_vcpu_reset,
     .nhvm_vcpu_guestcr3   = nvmx_vcpu_guestcr3,
-    .nhvm_vcpu_hostcr3    = nvmx_vcpu_hostcr3,
+    .nhvm_vcpu_p2m_base   = nvmx_vcpu_eptp_base,
     .nhvm_vcpu_asid       = nvmx_vcpu_asid,
     .nhvm_vmcx_guest_intercepts_trap = nvmx_intercepts_exception,
     .nhvm_vcpu_vmexit_trap = nvmx_vmexit_trap,
xen/arch/x86/hvm/vmx/vvmx.c +1 -1
index b005816..6d1a736 100644
@@ -94,7 +94,7 @@ uint64_t nvmx_vcpu_guestcr3(struct vcpu *v)
     return 0;
 }
 
-uint64_t nvmx_vcpu_hostcr3(struct vcpu *v)
+uint64_t nvmx_vcpu_eptp_base(struct vcpu *v)
 {
     /* TODO */
     ASSERT(0);
xen/arch/x86/mm/hap/nested_hap.c +8 -7
index 317875d..f9a5edc 100644
@@ -48,9 +48,10 @@
  *    1. If #NPF is from L1 guest, then we crash the guest VM (same as old 
  *       code)
  *    2. If #NPF is from L2 guest, then we continue from (3)
- *    3. Get h_cr3 from L1 guest. Map h_cr3 into L0 hypervisor address space.
- *    4. Walk the h_cr3 page table
- *    5.    - if not present, then we inject #NPF back to L1 guest and 
+ *    3. Get np2m base from L1 guest. Map np2m base into L0 hypervisor address space.
+ *    4. Walk the np2m's  page table
+ *    5.    - if not present or permission check failure, then we inject #NPF back to 
+ *    L1 guest and 
  *            re-launch L1 guest (L1 guest will either treat this #NPF as MMIO,
  *            or fix its p2m table for L2 guest)
  *    6.    - if present, then we will get the a new translated value L1-GPA 
@@ -89,7 +90,7 @@ nestedp2m_write_p2m_entry(struct p2m_domain *p2m, unsigned long gfn,
 
     if (old_flags & _PAGE_PRESENT)
         flush_tlb_mask(p2m->dirty_cpumask);
-    
+
     paging_unlock(d);
 }
 
@@ -110,7 +111,7 @@ nestedhap_fix_p2m(struct vcpu *v, struct p2m_domain *p2m,
     /* If this p2m table has been flushed or recycled under our feet, 
      * leave it alone.  We'll pick up the right one as we try to 
      * vmenter the guest. */
-    if ( p2m->cr3 == nhvm_vcpu_hostcr3(v) )
+    if ( p2m->np2m_base == nhvm_vcpu_p2m_base(v) )
     {
         unsigned long gfn, mask;
         mfn_t mfn;
@@ -186,7 +187,7 @@ nestedhap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
     uint32_t pfec;
     unsigned long nested_cr3, gfn;
     
-    nested_cr3 = nhvm_vcpu_hostcr3(v);
+    nested_cr3 = nhvm_vcpu_p2m_base(v);
 
     pfec = PFEC_user_mode | PFEC_page_present;
     if (access_w)
@@ -221,7 +222,7 @@ nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa,
     p2m_type_t p2mt_10;
 
     p2m = p2m_get_hostp2m(d); /* L0 p2m */
-    nested_p2m = p2m_get_nestedp2m(v, nhvm_vcpu_hostcr3(v));
+    nested_p2m = p2m_get_nestedp2m(v, nhvm_vcpu_p2m_base(v));
 
     /* walk the L1 P2M table */
     rv = nestedhap_walk_L1_p2m(v, *L2_gpa, &L1_gpa, &page_order_21,
xen/arch/x86/mm/mm-locks.h +1 -1
index 3700e32..1817f81 100644
@@ -249,7 +249,7 @@ declare_mm_order_constraint(per_page_sharing)
  * A per-domain lock that protects the mapping from nested-CR3 to 
  * nested-p2m.  In particular it covers:
  * - the array of nested-p2m tables, and all LRU activity therein; and
- * - setting the "cr3" field of any p2m table to a non-CR3_EADDR value. 
+ * - setting the "cr3" field of any p2m table to a non-P2M_BASE_EAADR value. 
  *   (i.e. assigning a p2m table to be the shadow of that cr3 */
 
 /* PoD lock (per-p2m-table)
xen/arch/x86/mm/p2m.c +13 -13
index e351942..62c2d78 100644
@@ -81,7 +81,7 @@ static void p2m_initialise(struct domain *d, struct p2m_domain *p2m)
     p2m->domain = d;
     p2m->default_access = p2m_access_rwx;
 
-    p2m->cr3 = CR3_EADDR;
+    p2m->np2m_base = P2M_BASE_EADDR;
 
     if ( hap_enabled(d) && cpu_has_vmx )
         ept_p2m_init(p2m);
@@ -1445,7 +1445,7 @@ p2m_flush_table(struct p2m_domain *p2m)
     ASSERT(page_list_empty(&p2m->pod.single));
 
     /* This is no longer a valid nested p2m for any address space */
-    p2m->cr3 = CR3_EADDR;
+    p2m->np2m_base = P2M_BASE_EADDR;
     
     /* Zap the top level of the trie */
     top = mfn_to_page(pagetable_get_mfn(p2m_get_pagetable(p2m)));
@@ -1483,7 +1483,7 @@ p2m_flush_nestedp2m(struct domain *d)
 }
 
 struct p2m_domain *
-p2m_get_nestedp2m(struct vcpu *v, uint64_t cr3)
+p2m_get_nestedp2m(struct vcpu *v, uint64_t np2m_base)
 {
     /* Use volatile to prevent gcc to cache nv->nv_p2m in a cpu register as
      * this may change within the loop by an other (v)cpu.
@@ -1492,8 +1492,8 @@ p2m_get_nestedp2m(struct vcpu *v, uint64_t cr3)
     struct domain *d;
     struct p2m_domain *p2m;
 
-    /* Mask out low bits; this avoids collisions with CR3_EADDR */
-    cr3 &= ~(0xfffull);
+    /* Mask out low bits; this avoids collisions with P2M_BASE_EADDR */
+    np2m_base &= ~(0xfffull);
 
     if (nv->nv_flushp2m && nv->nv_p2m) {
         nv->nv_p2m = NULL;
@@ -1505,14 +1505,14 @@ p2m_get_nestedp2m(struct vcpu *v, uint64_t cr3)
     if ( p2m ) 
     {
         p2m_lock(p2m);
-        if ( p2m->cr3 == cr3 || p2m->cr3 == CR3_EADDR )
+        if ( p2m->np2m_base == np2m_base || p2m->np2m_base == P2M_BASE_EADDR )
         {
             nv->nv_flushp2m = 0;
             p2m_getlru_nestedp2m(d, p2m);
             nv->nv_p2m = p2m;
-            if (p2m->cr3 == CR3_EADDR)
+            if (p2m->np2m_base == P2M_BASE_EADDR)
                 hvm_asid_flush_vcpu(v);
-            p2m->cr3 = cr3;
+            p2m->np2m_base = np2m_base;
             cpumask_set_cpu(v->processor, p2m->dirty_cpumask);
             p2m_unlock(p2m);
             nestedp2m_unlock(d);
@@ -1527,7 +1527,7 @@ p2m_get_nestedp2m(struct vcpu *v, uint64_t cr3)
     p2m_flush_table(p2m);
     p2m_lock(p2m);
     nv->nv_p2m = p2m;
-    p2m->cr3 = cr3;
+    p2m->np2m_base = np2m_base;
     nv->nv_flushp2m = 0;
     hvm_asid_flush_vcpu(v);
     cpumask_set_cpu(v->processor, p2m->dirty_cpumask);
@@ -1543,7 +1543,7 @@ p2m_get_p2m(struct vcpu *v)
     if (!nestedhvm_is_n2(v))
         return p2m_get_hostp2m(v->domain);
 
-    return p2m_get_nestedp2m(v, nhvm_vcpu_hostcr3(v));
+    return p2m_get_nestedp2m(v, nhvm_vcpu_p2m_base(v));
 }
 
 unsigned long paging_gva_to_gfn(struct vcpu *v,
@@ -1561,15 +1561,15 @@ unsigned long paging_gva_to_gfn(struct vcpu *v,
         struct p2m_domain *p2m;
         const struct paging_mode *mode;
         uint32_t pfec_21 = *pfec;
-        uint64_t ncr3 = nhvm_vcpu_hostcr3(v);
+        uint64_t np2m_base = nhvm_vcpu_p2m_base(v);
 
         /* translate l2 guest va into l2 guest gfn */
-        p2m = p2m_get_nestedp2m(v, ncr3);
+        p2m = p2m_get_nestedp2m(v, np2m_base);
         mode = paging_get_nestedmode(v);
         gfn = mode->gva_to_gfn(v, p2m, va, pfec);
 
         /* translate l2 guest gfn into l1 guest gfn */
-        return hostmode->p2m_ga_to_gfn(v, hostp2m, ncr3,
+        return hostmode->p2m_ga_to_gfn(v, hostp2m, np2m_base,
                                        gfn << PAGE_SHIFT, &pfec_21, NULL);
     }
 
xen/include/asm-x86/hvm/hvm.h +2 -2
index fdb0f58..d3535b6 100644
@@ -170,7 +170,7 @@ struct hvm_function_table {
                                 uint64_t exitcode);
     int (*nhvm_vcpu_vmexit_trap)(struct vcpu *v, struct hvm_trap *trap);
     uint64_t (*nhvm_vcpu_guestcr3)(struct vcpu *v);
-    uint64_t (*nhvm_vcpu_hostcr3)(struct vcpu *v);
+    uint64_t (*nhvm_vcpu_p2m_base)(struct vcpu *v);
     uint32_t (*nhvm_vcpu_asid)(struct vcpu *v);
     int (*nhvm_vmcx_guest_intercepts_trap)(struct vcpu *v, 
                                unsigned int trapnr, int errcode);
@@ -475,7 +475,7 @@ uint64_t nhvm_vcpu_guestcr3(struct vcpu *v);
 /* returns l1 guest's cr3 that points to the page table used to
  * translate l2 guest physical address to l1 guest physical address.
  */
-uint64_t nhvm_vcpu_hostcr3(struct vcpu *v);
+uint64_t nhvm_vcpu_p2m_base(struct vcpu *v);
 /* returns the asid number l1 guest wants to use to run the l2 guest */
 uint32_t nhvm_vcpu_asid(struct vcpu *v);
 
xen/include/asm-x86/hvm/vmx/vvmx.h +1 -1
index dce2cd8..d97011d 100644
@@ -99,7 +99,7 @@ int nvmx_vcpu_initialise(struct vcpu *v);
 void nvmx_vcpu_destroy(struct vcpu *v);
 int nvmx_vcpu_reset(struct vcpu *v);
 uint64_t nvmx_vcpu_guestcr3(struct vcpu *v);
-uint64_t nvmx_vcpu_hostcr3(struct vcpu *v);
+uint64_t nvmx_vcpu_eptp_base(struct vcpu *v);
 uint32_t nvmx_vcpu_asid(struct vcpu *v);
 enum hvm_intblk nvmx_intr_blocked(struct vcpu *v);
 int nvmx_intercepts_exception(struct vcpu *v, 
xen/include/asm-x86/p2m.h +8 -8
index 907a817..1807ad6 100644
@@ -197,17 +197,17 @@ struct p2m_domain {
 
     struct domain     *domain;   /* back pointer to domain */
 
-    /* Nested p2ms only: nested-CR3 value that this p2m shadows. 
-     * This can be cleared to CR3_EADDR under the per-p2m lock but
+    /* Nested p2ms only: nested p2m base value that this p2m shadows. 
+     * This can be cleared to P2M_BASE_EADDR under the per-p2m lock but
      * needs both the per-p2m lock and the per-domain nestedp2m lock
      * to set it to any other value. */
-#define CR3_EADDR     (~0ULL)
-    uint64_t           cr3;
+#define P2M_BASE_EADDR     (~0ULL)
+    uint64_t           np2m_base;
 
     /* Nested p2ms: linked list of n2pms allocated to this domain. 
      * The host p2m hasolds the head of the list and the np2ms are 
      * threaded on in LRU order. */
-    struct list_head np2m_list; 
+    struct list_head   np2m_list; 
 
 
     /* Host p2m: when this flag is set, don't flush all the nested-p2m 
@@ -282,11 +282,11 @@ struct p2m_domain {
 /* get host p2m table */
 #define p2m_get_hostp2m(d)      ((d)->arch.p2m)
 
-/* Get p2m table (re)usable for specified cr3.
+/* Get p2m table (re)usable for specified np2m base.
  * Automatically destroys and re-initializes a p2m if none found.
- * If cr3 == 0 then v->arch.hvm_vcpu.guest_cr[3] is used.
+ * If np2m_base == 0 then v->arch.hvm_vcpu.guest_cr[3] is used.
  */
-struct p2m_domain *p2m_get_nestedp2m(struct vcpu *v, uint64_t cr3);
+struct p2m_domain *p2m_get_nestedp2m(struct vcpu *v, uint64_t np2m_base);
 
 /* If vcpu is in host mode then behaviour matches p2m_get_hostp2m().
  * If vcpu is in guest mode then behaviour matches p2m_get_nestedp2m().
Signature · 1 line
1.7.1
#3 lore raw

PATCH 2/11 nestedhap: Change nested p2m's walker to vendor-specific

In reply to [email protected]

From: Zhang Xiantao <[email protected]>

EPT and NPT adopts differnt formats for each-level entry, so change the walker functions to vendor-specific.

---
 xen/arch/x86/hvm/svm/nestedsvm.c        |   31 +++++++++++++++++++++
 xen/arch/x86/hvm/svm/svm.c              |    1 +
 xen/arch/x86/hvm/vmx/vmx.c              |    3 +-
 xen/arch/x86/hvm/vmx/vvmx.c             |   13 +++++++++
 xen/arch/x86/mm/hap/nested_hap.c        |   46 +++++++++++--------------------
 xen/include/asm-x86/hvm/hvm.h           |    5 +++
 xen/include/asm-x86/hvm/svm/nestedsvm.h |    3 ++
 xen/include/asm-x86/hvm/vmx/vvmx.h      |    5 +++
 8 files changed, 76 insertions(+), 31 deletions(-)
xen/arch/x86/hvm/svm/nestedsvm.c +31 -0
index ed0faa6..5dcb354 100644
@@ -1171,6 +1171,37 @@ nsvm_vmcb_hap_enabled(struct vcpu *v)
     return vcpu_nestedsvm(v).ns_hap_enabled;
 }
 
+/* This function uses L2_gpa to walk the P2M page table in L1. If the 
+ * walk is successful, the translated value is returned in
+ * L1_gpa. The result value tells what to do next.
+ */
+int
+nsvm_hap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
+                      unsigned int *page_order,
+                      bool_t access_r, bool_t access_w, bool_t access_x)
+{
+    uint32_t pfec;
+    unsigned long nested_cr3, gfn;
+    
+    nested_cr3 = nhvm_vcpu_p2m_base(v);
+
+    pfec = PFEC_user_mode | PFEC_page_present;
+    if (access_w)
+        pfec |= PFEC_write_access;
+    if (access_x)
+        pfec |= PFEC_insn_fetch;
+
+    /* Walk the guest-supplied NPT table, just as if it were a pagetable */
+    gfn = paging_ga_to_gfn_cr3(v, nested_cr3, L2_gpa, &pfec, page_order);
+
+    if ( gfn == INVALID_GFN ) 
+        return NESTEDHVM_PAGEFAULT_INJECT;
+
+    *L1_gpa = (gfn << PAGE_SHIFT) + (L2_gpa & ~PAGE_MASK);
+    return NESTEDHVM_PAGEFAULT_DONE;
+}
+
+
 enum hvm_intblk nsvm_intr_blocked(struct vcpu *v)
 {
     struct nestedsvm *svm = &vcpu_nestedsvm(v);
xen/arch/x86/hvm/svm/svm.c +1 -0
index 6c469ec..a905764 100644
@@ -2008,6 +2008,7 @@ static struct hvm_function_table __read_mostly svm_function_table = {
     .nhvm_vmcx_guest_intercepts_trap = nsvm_vmcb_guest_intercepts_trap,
     .nhvm_vmcx_hap_enabled = nsvm_vmcb_hap_enabled,
     .nhvm_intr_blocked = nsvm_intr_blocked,
+    .nhvm_hap_walk_L1_p2m = nsvm_hap_walk_L1_p2m,
 };
 
 void svm_vmexit_handler(struct cpu_user_regs *regs)
xen/arch/x86/hvm/vmx/vmx.c +2 -1
index 47d8ca6..c67ac59 100644
@@ -1511,7 +1511,8 @@ static struct hvm_function_table __read_mostly vmx_function_table = {
     .nhvm_intr_blocked    = nvmx_intr_blocked,
     .nhvm_domain_relinquish_resources = nvmx_domain_relinquish_resources,
     .update_eoi_exit_bitmap = vmx_update_eoi_exit_bitmap,
-    .virtual_intr_delivery_enabled = vmx_virtual_intr_delivery_enabled
+    .virtual_intr_delivery_enabled = vmx_virtual_intr_delivery_enabled,
+    .nhvm_hap_walk_L1_p2m = nvmx_hap_walk_L1_p2m,
 };
 
 struct hvm_function_table * __init start_vmx(void)
xen/arch/x86/hvm/vmx/vvmx.c +13 -0
index 6d1a736..4495dd6 100644
@@ -1445,6 +1445,19 @@ int nvmx_msr_write_intercept(unsigned int msr, u64 msr_content)
     return 1;
 }
 
+/* This function uses L2_gpa to walk the P2M page table in L1. If the 
+ * walk is successful, the translated value is returned in
+ * L1_gpa. The result value tells what to do next.
+ */
+int
+nvmx_hap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
+                      unsigned int *page_order,
+                      bool_t access_r, bool_t access_w, bool_t access_x)
+{
+    /*TODO:*/
+    return 0;
+}
+
 void nvmx_idtv_handling(void)
 {
     struct vcpu *v = current;
xen/arch/x86/mm/hap/nested_hap.c +16 -30

58 lines not on this page · raw message

xen/include/asm-x86/hvm/hvm.h +5 -0
index d3535b6..80f07e9 100644
@@ -183,6 +183,11 @@ struct hvm_function_table {
     /* Virtual interrupt delivery */
     void (*update_eoi_exit_bitmap)(struct vcpu *v, u8 vector, u8 trig);
     int (*virtual_intr_delivery_enabled)(void);
+
+    /*Walk nested p2m  */
+    int (*nhvm_hap_walk_L1_p2m)(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
+                      unsigned int *page_order,
+                      bool_t access_r, bool_t access_w, bool_t access_x);
 };
 
 extern struct hvm_function_table hvm_funcs;
xen/include/asm-x86/hvm/svm/nestedsvm.h +3 -0
index fa83023..0c90f30 100644
@@ -133,6 +133,9 @@ int nsvm_wrmsr(struct vcpu *v, unsigned int msr, uint64_t msr_content);
 void svm_vmexit_do_clgi(struct cpu_user_regs *regs, struct vcpu *v);
 void svm_vmexit_do_stgi(struct cpu_user_regs *regs, struct vcpu *v);
 bool_t nestedsvm_gif_isset(struct vcpu *v);
+int nsvm_hap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
+                      unsigned int *page_order,
+                      bool_t access_r, bool_t access_w, bool_t access_x);
 
 #define NSVM_INTR_NOTHANDLED     3
 #define NSVM_INTR_NOTINTERCEPTED 2
xen/include/asm-x86/hvm/vmx/vvmx.h +5 -0
index d97011d..422f006 100644
@@ -108,6 +108,11 @@ void nvmx_domain_relinquish_resources(struct domain *d);
 
 int nvmx_handle_vmxon(struct cpu_user_regs *regs);
 int nvmx_handle_vmxoff(struct cpu_user_regs *regs);
+
+int
+nvmx_hap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
+                      unsigned int *page_order,
+                      bool_t access_r, bool_t access_w, bool_t access_x);
 /*
  * Virtual VMCS layout
  *
Signature · 1 line
1.7.1
#5 lore raw

PATCH 3/11 nEPT: Implement guest ept's walker

In reply to [email protected]

From: Zhang Xiantao <[email protected]>

Implment guest EPT PT walker, some logic is based on shadow's ia32e PT walker. During the PT walking, if the target pages are not in memory, use RETRY mechanism and get a chance to let the target page back.

---
 xen/arch/x86/hvm/hvm.c              |    1 +
 xen/arch/x86/hvm/vmx/vvmx.c         |   42 +++++-
 xen/arch/x86/mm/guest_walk.c        |   12 +-
 xen/arch/x86/mm/hap/Makefile        |    1 +
 xen/arch/x86/mm/hap/nested_ept.c    |  327 +++++++++++++++++++++++++++++++++++
 xen/arch/x86/mm/hap/nested_hap.c    |    2 +-
 xen/arch/x86/mm/shadow/multi.c      |    2 +-
 xen/include/asm-x86/guest_pt.h      |    8 +
 xen/include/asm-x86/hvm/nestedhvm.h |    1 +
 xen/include/asm-x86/hvm/vmx/vmcs.h  |    1 +
 xen/include/asm-x86/hvm/vmx/vvmx.h  |   14 ++
 11 files changed, 403 insertions(+), 8 deletions(-)
 create mode 100644 xen/arch/x86/mm/hap/nested_ept.c
xen/arch/x86/hvm/hvm.c +1 -0
index 85bc9be..3400e6b 100644
@@ -1324,6 +1324,7 @@ int hvm_hap_nested_page_fault(paddr_t gpa,
                                              access_r, access_w, access_x);
         switch (rv) {
         case NESTEDHVM_PAGEFAULT_DONE:
+        case NESTEDHVM_PAGEFAULT_RETRY:
             return 1;
         case NESTEDHVM_PAGEFAULT_L1_ERROR:
             /* An error occured while translating gpa from
xen/arch/x86/hvm/vmx/vvmx.c +40 -2

57 lines not on this page · raw message

xen/arch/x86/mm/guest_walk.c +8 -4
index 13ea0bb..afbe9db 100644
@@ -88,10 +88,11 @@ static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty)
 
 /* If the map is non-NULL, we leave this function having 
  * acquired an extra ref on mfn_to_page(*mfn) */
-static inline void *map_domain_gfn(struct p2m_domain *p2m,
+void *map_domain_gfn(struct p2m_domain *p2m,
                                    gfn_t gfn, 
                                    mfn_t *mfn,
                                    p2m_type_t *p2mt,
+                                   p2m_query_t *q,
                                    uint32_t *rc) 
 {
     struct page_info *page;
@@ -99,7 +100,7 @@ static inline void *map_domain_gfn(struct p2m_domain *p2m,
 
     /* Translate the gfn, unsharing if shared */
     page = get_page_from_gfn_p2m(p2m->domain, p2m, gfn_x(gfn), p2mt, NULL,
-                                  P2M_ALLOC | P2M_UNSHARE);
+                                  *q);
     if ( p2m_is_paging(*p2mt) )
     {
         ASSERT(!p2m_is_nestedp2m(p2m));
@@ -128,7 +129,6 @@ static inline void *map_domain_gfn(struct p2m_domain *p2m,
     return map;
 }
 
-
 /* Walk the guest pagetables, after the manner of a hardware walker. */
 /* Because the walk is essentially random, it can cause a deadlock 
  * warning in the p2m locking code. Highly unlikely this is an actual
@@ -149,6 +149,7 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
     uint32_t gflags, mflags, iflags, rc = 0;
     int smep;
     bool_t pse1G = 0, pse2M = 0;
+    p2m_query_t qt = P2M_ALLOC | P2M_UNSHARE;
 
     perfc_incr(guest_walk);
     memset(gw, 0, sizeof(*gw));
@@ -188,7 +189,8 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
     l3p = map_domain_gfn(p2m, 
                          guest_l4e_get_gfn(gw->l4e), 
                          &gw->l3mfn,
-                         &p2mt, 
+                         &p2mt,
+                         &qt, 
                          &rc); 
     if(l3p == NULL)
         goto out;
@@ -249,6 +251,7 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
                          guest_l3e_get_gfn(gw->l3e), 
                          &gw->l2mfn,
                          &p2mt, 
+                         &qt,
                          &rc); 
     if(l2p == NULL)
         goto out;
@@ -322,6 +325,7 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
                              guest_l2e_get_gfn(gw->l2e), 
                              &gw->l1mfn,
                              &p2mt,
+                             &qt,
                              &rc);
         if(l1p == NULL)
             goto out;
xen/arch/x86/mm/hap/Makefile +1 -0
index 80a6bec..68f2bb5 100644
@@ -3,6 +3,7 @@ obj-y += guest_walk_2level.o
 obj-y += guest_walk_3level.o
 obj-$(x86_64) += guest_walk_4level.o
 obj-y += nested_hap.o
+obj-y += nested_ept.o
 
 guest_walk_%level.o: guest_walk.c Makefile
 	$(CC) $(CFLAGS) -DGUEST_PAGING_LEVELS=$* -c $< -o $@
xen/arch/x86/mm/hap/nested_ept.c new file +327 -0

327 lines not on this page · raw message

xen/arch/x86/mm/hap/nested_hap.c +1 -1
index 8787c91..6d1264b 100644
@@ -217,7 +217,7 @@ nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa,
     /* let caller to handle these two cases */
     switch (rv) {
     case NESTEDHVM_PAGEFAULT_INJECT:
-        return rv;
+    case NESTEDHVM_PAGEFAULT_RETRY:
     case NESTEDHVM_PAGEFAULT_L1_ERROR:
         return rv;
     case NESTEDHVM_PAGEFAULT_DONE:
xen/arch/x86/mm/shadow/multi.c +1 -1
index 4967da1..409198c 100644
@@ -4582,7 +4582,7 @@ static mfn_t emulate_gva_to_mfn(struct vcpu *v,
     /* Translate the GFN to an MFN */
     ASSERT(!paging_locked_by_me(v->domain));
     mfn = get_gfn(v->domain, _gfn(gfn), &p2mt);
-        
+
     if ( p2m_is_readonly(p2mt) )
     {
         put_gfn(v->domain, gfn);
xen/include/asm-x86/guest_pt.h +8 -0
index 4e1dda0..600c52d 100644
@@ -315,6 +315,14 @@ guest_walk_to_page_order(walk_t *gw)
 #define GPT_RENAME2(_n, _l) _n ## _ ## _l ## _levels
 #define GPT_RENAME(_n, _l) GPT_RENAME2(_n, _l)
 #define guest_walk_tables GPT_RENAME(guest_walk_tables, GUEST_PAGING_LEVELS)
+#define map_domain_gfn GPT_RENAME(map_domain_gfn, GUEST_PAGING_LEVELS)
+
+extern void *map_domain_gfn(struct p2m_domain *p2m,
+                                   gfn_t gfn, 
+                                   mfn_t *mfn,
+                                   p2m_type_t *p2mt,
+                                   p2m_query_t *q,
+                                   uint32_t *rc);
 
 extern uint32_t 
 guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, unsigned long va,
xen/include/asm-x86/hvm/nestedhvm.h +1 -0
index 91fde0b..649c511 100644
@@ -52,6 +52,7 @@ bool_t nestedhvm_vcpu_in_guestmode(struct vcpu *v);
 #define NESTEDHVM_PAGEFAULT_L1_ERROR   2
 #define NESTEDHVM_PAGEFAULT_L0_ERROR   3
 #define NESTEDHVM_PAGEFAULT_MMIO       4
+#define NESTEDHVM_PAGEFAULT_RETRY      5
 int nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa,
     bool_t access_r, bool_t access_w, bool_t access_x);
 
xen/include/asm-x86/hvm/vmx/vmcs.h +1 -0
index ef2c9c9..9a728b6 100644
@@ -194,6 +194,7 @@ extern u32 vmx_secondary_exec_control;
 
 extern bool_t cpu_has_vmx_ins_outs_instr_info;
 
+#define VMX_EPT_EXEC_ONLY_SUPPORTED             0x00000001
 #define VMX_EPT_WALK_LENGTH_4_SUPPORTED         0x00000040
 #define VMX_EPT_MEMORY_TYPE_UC                  0x00000100
 #define VMX_EPT_MEMORY_TYPE_WB                  0x00004000
xen/include/asm-x86/hvm/vmx/vvmx.h +14 -0
index 422f006..8eb377b 100644
@@ -32,6 +32,10 @@ struct nestedvmx {
         unsigned long intr_info;
         u32           error_code;
     } intr;
+    struct {
+        uint32_t exit_reason;
+        uint32_t exit_qual;
+    } ept_exit;
 };
 
 #define vcpu_2_nvmx(v)	(vcpu_nestedhvm(v).u.nvmx)
@@ -109,6 +113,12 @@ void nvmx_domain_relinquish_resources(struct domain *d);
 int nvmx_handle_vmxon(struct cpu_user_regs *regs);
 int nvmx_handle_vmxoff(struct cpu_user_regs *regs);
 
+#define EPT_TRANSLATE_SUCCEED   0
+#define EPT_TRANSLATE_VIOLATION 1
+#define EPT_TRANSLATE_ERR_PAGE  2
+#define EPT_TRANSLATE_MISCONFIG 3
+#define EPT_TRANSLATE_RETRY     4
+
 int
 nvmx_hap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
                       unsigned int *page_order,
@@ -192,5 +202,9 @@ u64 nvmx_get_tsc_offset(struct vcpu *v);
 int nvmx_n2_vmexit_handler(struct cpu_user_regs *regs,
                           unsigned int exit_reason);
 
+int nept_translate_l2ga(struct vcpu *v, paddr_t l2ga, 
+                        unsigned int *page_order, uint32_t rwx_acc,
+                        unsigned long *l1gfn, uint64_t *exit_qual,
+                        uint32_t *exit_reason);
 #endif /* __ASM_X86_HVM_VVMX_H__ */
 
Signature · 1 line
1.7.1
#6 lore raw

Re: [PATCH 03/11] nEPT: Implement guest ept's walker

Quoted 8 lines · At 01:57 +0800 on 11 Dec (1355191035), [email protected] wrote:

From: Zhang Xiantao <[email protected]>

Implment guest EPT PT walker, some logic is based on shadow's ia32e PT walker. During the PT walking, if the target pages are not in memory, use RETRY mechanism and get a chance to let the target page back.

The design looks pretty good. A few comments below on code details -- I think the only big one is that the ept walker shouldn't force eptes into 'normal' pte types just so it can reuse the walk_t struct.

xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 95

Quoted 10 lines
xen/arch/x86/mm/guest_walk.c +2 -1
@@ -88,10 +88,11 @@ static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty)
 
 /* If the map is non-NULL, we leave this function having 
  * acquired an extra ref on mfn_to_page(*mfn) */
-static inline void *map_domain_gfn(struct p2m_domain *p2m,
+void *map_domain_gfn(struct p2m_domain *p2m,
                                    gfn_t gfn, 
                                    mfn_t *mfn,
                                    p2m_type_t *p2mt,
+                                   p2m_query_t *q,

I think this should just be a plain p2m_query_t and not a pointer to one; the code below only dereferences the pointer to read it. That will save you having a variable just to hold 'P2M_ALLOC | P2M_UNSHARE' in a few places below.

xen/arch/x86/mm/hap/nested_ept.c line 49

Quoted 9 lines
xen/arch/x86/mm/hap/nested_ept.c new file +7 -0
+/* For EPT's walker reserved bits and EMT check  */
+#define EPT_MUST_RSV_BITS (((1ull << PADDR_BITS) -1) & \
+                     ~((1ull << paddr_bits) - 1))
+
+
+#define EPT_EMT_WB  6
+#define EPT_EMT_UC  0

These two definitions should be in vmx.h along with the other architectural constants for EPTEs.

Quoted 16 lines
xen/arch/x86/mm/hap/nested_ept.c new file +16 -0
+
+#define NEPT_VPID_CAP_BITS 0
+
+#define NEPT_1G_ENTRY_FLAG (1 << 11)
+#define NEPT_2M_ENTRY_FLAG (1 << 10)
+#define NEPT_4K_ENTRY_FLAG (1 << 9)
+
+/* Always expose 1G and 2M capability to guest, 
+ so don't need additional check */
+bool_t nept_sp_entry(uint64_t entry)
+{
+    return !!(entry & EPTE_SUPER_PAGE_MASK);
+}
+
+static bool_t nept_rsv_bits_check(uint64_t entry, uint32_t level)
+{

xen/arch/x86/mm/hap/nested_ept.c line 75

Quoted 10 lines
xen/arch/x86/mm/hap/nested_ept.c new file +10 -0
+    uint64_t rsv_bits = EPT_MUST_RSV_BITS;
+
+    switch ( level ){
+        case 1:
+            break;
+        case 2 ... 3:
+            if (nept_sp_entry(entry))
+                rsv_bits |=  ((1ull << (9 * (level -1 ))) -1) << PAGE_SHIFT;
+            else
+                rsv_bits |= 0xfull << 3;

Please use EPTE_EMT_MASK rather than open-coding it.

xen/arch/x86/mm/hap/nested_ept.c line 78

Quoted 3 lines
xen/arch/x86/mm/hap/nested_ept.c new file +3 -0
+            break;
+        case 4:
+        rsv_bits |= 0xf8;

Again, please use EPTE_EMT_MASK | EPTE_IGMT_MASK | EPTE_SUPER_PAGE_MASK.

xen/arch/x86/mm/hap/nested_ept.c line 84

Quoted 6 lines
xen/arch/x86/mm/hap/nested_ept.c new file +6 -0
+        break;
+        default:
+            printk("Unsupported EPT paging level: %d\n", level);
+    }
+    if ( ((entry & rsv_bits) ^ rsv_bits) == rsv_bits )
+        return 0;

This XOR is useful in the normal walker because we care about _which_ bits are wrong. Here, you can just return !(entry & rsv_bits) for the same result.

xen/arch/x86/mm/hap/nested_ept.c line 95

Quoted 11 lines
xen/arch/x86/mm/hap/nested_ept.c new file +11 -0
+    return 1;
+}
+
+/* EMT checking*/
+static bool_t nept_emt_bits_check(uint64_t entry, uint32_t level)
+{
+    ept_entry_t e;
+    e.epte = entry;
+    if ( e.sp || level == 1 ) {
+        if ( e.emt == 2 || e.emt == 3 || e.emt == 7 )
+            return 1;

Please define more of the EPT_EMT_* constants for these values and use them.

xen/arch/x86/mm/hap/nested_ept.c line 107

Quoted 12 lines
xen/arch/x86/mm/hap/nested_ept.c new file +12 -0
+    }
+    return 0;
+}
+
+static bool_t nept_rwx_bits_check(uint64_t entry) {
+    /*write only or write/execute only*/
+    uint8_t rwx_bits = entry & 0x7;
+
+    if ( rwx_bits == 2 || rwx_bits == 6)
+        return 1;
+    if ( rwx_bits == 4 && !(NEPT_VPID_CAP_BITS &
+                        VMX_EPT_EXEC_ONLY_SUPPORTED))

Please pass the entry as an ept_entry_t and check the named r, w and x fields rather than using magic numbers.

Quoted 5 lines
xen/arch/x86/mm/hap/nested_ept.c new file +5 -0
+        return 1;
+    return 0;
+}
+
+/* nept's misconfiguration check */

xen/arch/x86/mm/hap/nested_ept.c line 122

Quoted 10 lines
xen/arch/x86/mm/hap/nested_ept.c new file +10 -0
+static bool_t nept_misconfiguration_check(uint64_t entry, uint32_t level)
+{
+    return (nept_rsv_bits_check(entry, level) ||
+                nept_emt_bits_check(entry, level) ||
+                nept_rwx_bits_check(entry));
+}
+
+static bool_t nept_present_check(uint64_t entry)
+{
+    if (entry & 0x7)

Again, please pass an ept_entry_t and check the r/w/x fields.

xen/arch/x86/mm/hap/nested_ept.c line 129

Quoted 7 lines
xen/arch/x86/mm/hap/nested_ept.c new file +7 -0
+        return 1;
+    return 0;
+}
+
+uint64_t nept_get_ept_vpid_cap(void)
+{
+    /*TODO: exposed ept and vpid features*/

This TODO comment doesn't get removed later in the series. Is returning 0 here always OK?

Quoted 7 lines
xen/arch/x86/mm/hap/nested_ept.c new file +7 -0
+    return NEPT_VPID_CAP_BITS;
+}
+
+static uint32_t
+nept_walk_tables(struct vcpu *v, unsigned long l2ga, walk_t *gw)
+{
+    p2m_type_t p2mt;

xen/arch/x86/mm/hap/nested_ept.c line 146

Quoted 10 lines
xen/arch/x86/mm/hap/nested_ept.c new file +10 -0
+    uint32_t rc = 0, ret = 0, gflags;
+    struct domain *d = v->domain;
+    struct p2m_domain *p2m = d->arch.p2m;
+    gfn_t base_gfn = _gfn(nhvm_vcpu_p2m_base(v) >> PAGE_SHIFT);
+    p2m_query_t qt = P2M_ALLOC;
+
+    guest_l1e_t *l1p = NULL;
+    guest_l2e_t *l2p = NULL;
+    guest_l3e_t *l3p = NULL;
+    guest_l4e_t *l4p = NULL;

These aren't realy guest_l*es, so I think you should use ept_entry_t * to point to them. While you're at it, why not define an equivalent ept_walk_t struct that uses the ept-specific types instead of putting EPT entries in a normal walk_t?

Also, unlike the normal guest walker, you don't need to hold these maps open for writing A/D bits, so you could just use a single pointer and unmap as you go.

xen/arch/x86/mm/hap/nested_ept.c line 181

Quoted 6 lines
xen/arch/x86/mm/hap/nested_ept.c new file +6 -0
+    sp = nept_sp_entry(gw->l3e.l3);
+    /* Super 1G entry */
+    if ( sp )
+    {
+        /* Generate a fake l1 table entry so callers don't all 
+         * have to understand superpages. */

You only have one caller for this function, and it does understand superpages -- it explicitly check for them. So I think you can avoid this part altogether (likewise for 2M superpages) and just DTRT in the caller.

Cheers,

Tim.

Quoted 11 lines
xen/arch/x86/mm/hap/nested_ept.c new file +11 -0
+        gfn_t start = guest_l3e_get_gfn(gw->l3e);
+
+        /* Increment the pfn by the right number of 4k pages. */
+        start = _gfn((gfn_x(start) & ~GUEST_L3_GFN_MASK) +
+                     ((l2ga >> PAGE_SHIFT) & GUEST_L3_GFN_MASK));
+        gflags = (gw->l3e.l3 & 0x7f) | NEPT_1G_ENTRY_FLAG;
+        gw->l1e = guest_l1e_from_gfn(start, gflags);
+        gw->l2mfn = gw->l1mfn = _mfn(INVALID_MFN);
+        goto done;
+    }
+
#7 lore raw

PATCH 4/11 nEPT: Do further permission check for sucessful translation.

In reply to [email protected]

From: Zhang Xiantao <[email protected]>

If permission check fails, inject EPT violation vmexit to guest.

---
 xen/arch/x86/mm/hap/nested_ept.c |   24 ++++++++++++++++++++----
 1 files changed, 20 insertions(+), 4 deletions(-)
xen/arch/x86/mm/hap/nested_ept.c +20 -4
index da868e7..2d733a8 100644
@@ -272,6 +272,16 @@ unmap:
     return ret;
 }
 
+static
+bool_t nept_permission_check(uint32_t rwx_acc, uint32_t rwx_bits)
+{
+    if ( ((rwx_acc & 0x1) && !(rwx_bits & 0x1)) ||
+        ((rwx_acc & 0x2) && !(rwx_bits & 0x2 )) ||
+        ((rwx_acc & 0x4) && !(rwx_bits & 0x4 )) )
+        return 0;
+    return 1;
+}
+
 /* Translate a L2 guest address to L1 gpa via L1 EPT paging structure */
 
 int nept_translate_l2ga(struct vcpu *v, paddr_t l2ga, 
@@ -301,11 +311,17 @@ int nept_translate_l2ga(struct vcpu *v, paddr_t l2ga,
                 rwx_bits = gw.l4e.l4 & gw.l3e.l3  & 0x7;
                 *page_order = 18;
             }
-            else
+            else {
                 gdprintk(XENLOG_ERR, "Uncorrect l1 entry!\n");
-
-            *l1gfn = guest_l1e_get_paddr(gw.l1e) >> PAGE_SHIFT;
-            break;
+                BUG();
+            }
+            if ( nept_permission_check(rwx_acc, rwx_bits) )
+            {
+                 *l1gfn = guest_l1e_get_paddr(gw.l1e) >> PAGE_SHIFT;
+                 break;
+            }
+            rc = EPT_TRANSLATE_VIOLATION;
+        /* Fall through to EPT violation if permission check fails. */
         case EPT_TRANSLATE_VIOLATION:
             *exit_qual = (*exit_qual & 0xffffffc0) | (rwx_bits << 3) | rwx_acc;
             *exit_reason = EXIT_REASON_EPT_VIOLATION;
Signature · 1 line
1.7.1
#8 lore raw

Re: [PATCH 04/11] nEPT: Do further permission check for sucessful translation.

xen/arch/x86/mm/hap/nested_ept.c unmap: · line 281

Quoted 7 lines · At 01:57 +0800 on 11 Dec (1355191036), [email protected] wrote:
xen/arch/x86/mm/hap/nested_ept.c +7 -0
+static
+bool_t nept_permission_check(uint32_t rwx_acc, uint32_t rwx_bits)
+{
+    if ( ((rwx_acc & 0x1) && !(rwx_bits & 0x1)) ||
+        ((rwx_acc & 0x2) && !(rwx_bits & 0x2 )) ||
+        ((rwx_acc & 0x4) && !(rwx_bits & 0x4 )) )
+        return 0;

Ugh. It would be nice to use human-readable names for these. Or, since you know these are both <= 0x7, just test for !(rwx_acc & ~rwx_bits).

Also, this should really be folded into the previous patch.

Cheers,

Tim.

Quoted 34 lines
xen/arch/x86/mm/hap/nested_ept.c +11 -4
+
 /* Translate a L2 guest address to L1 gpa via L1 EPT paging structure */
 
 int nept_translate_l2ga(struct vcpu *v, paddr_t l2ga, 
@@ -301,11 +311,17 @@ int nept_translate_l2ga(struct vcpu *v, paddr_t l2ga,
                 rwx_bits = gw.l4e.l4 & gw.l3e.l3  & 0x7;
                 *page_order = 18;
             }
-            else
+            else {
                 gdprintk(XENLOG_ERR, "Uncorrect l1 entry!\n");
-
-            *l1gfn = guest_l1e_get_paddr(gw.l1e) >> PAGE_SHIFT;
-            break;
+                BUG();
+            }
+            if ( nept_permission_check(rwx_acc, rwx_bits) )
+            {
+                 *l1gfn = guest_l1e_get_paddr(gw.l1e) >> PAGE_SHIFT;
+                 break;
+            }
+            rc = EPT_TRANSLATE_VIOLATION;
+        /* Fall through to EPT violation if permission check fails. */
         case EPT_TRANSLATE_VIOLATION:
             *exit_qual = (*exit_qual & 0xffffffc0) | (rwx_bits << 3) | rwx_acc;
             *exit_reason = EXIT_REASON_EPT_VIOLATION;
Signature · 7 lines
1.7.1


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#9 lore raw

PATCH 5/11 EPT: Make ept data structure or operations neutral

In reply to [email protected]

From: Zhang Xiantao <[email protected]>

Share the current EPT logic with nested EPT case, so make the related data structure or operations netural to comment EPT and nested EPT.

---
 xen/arch/x86/hvm/vmx/vmcs.c        |    2 +-
 xen/arch/x86/hvm/vmx/vmx.c         |   39 +++++++++------
 xen/arch/x86/mm/p2m-ept.c          |   96 ++++++++++++++++++++++++++++--------
 xen/arch/x86/mm/p2m.c              |   16 +++++-
 xen/include/asm-x86/hvm/vmx/vmcs.h |   30 +++++++----
 xen/include/asm-x86/hvm/vmx/vmx.h  |    6 ++-
 xen/include/asm-x86/p2m.h          |    1 +
 7 files changed, 137 insertions(+), 53 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +1 -1
index 9adc7a4..b9ebdfe 100644
@@ -942,7 +942,7 @@ static int construct_vmcs(struct vcpu *v)
     }
 
     if ( paging_mode_hap(d) )
-        __vmwrite(EPT_POINTER, d->arch.hvm_domain.vmx.ept_control.eptp);
+        __vmwrite(EPT_POINTER, d->arch.hvm_domain.vmx.ept.ept_ctl.eptp);
 
     if ( cpu_has_vmx_pat && paging_mode_hap(d) )
     {
xen/arch/x86/hvm/vmx/vmx.c +23 -16
index c67ac59..06455bf 100644
@@ -79,22 +79,23 @@ static void __ept_sync_domain(void *info);
 static int vmx_domain_initialise(struct domain *d)
 {
     int rc;
+    struct ept_data *ept = &d->arch.hvm_domain.vmx.ept;
 
     /* Set the memory type used when accessing EPT paging structures. */
-    d->arch.hvm_domain.vmx.ept_control.ept_mt = EPT_DEFAULT_MT;
+    ept->ept_ctl.ept_mt = EPT_DEFAULT_MT;
 
     /* set EPT page-walk length, now it's actual walk length - 1, i.e. 3 */
-    d->arch.hvm_domain.vmx.ept_control.ept_wl = 3;
+    ept->ept_ctl.ept_wl = 3;
 
-    d->arch.hvm_domain.vmx.ept_control.asr  =
+    ept->ept_ctl.asr  =
         pagetable_get_pfn(p2m_get_pagetable(p2m_get_hostp2m(d)));
 
-    if ( !zalloc_cpumask_var(&d->arch.hvm_domain.vmx.ept_synced) )
+    if ( !zalloc_cpumask_var(&ept->ept_synced) )
         return -ENOMEM;
 
     if ( (rc = vmx_alloc_vlapic_mapping(d)) != 0 )
     {
-        free_cpumask_var(d->arch.hvm_domain.vmx.ept_synced);
+        free_cpumask_var(ept->ept_synced);
         return rc;
     }
 
@@ -103,9 +104,10 @@ static int vmx_domain_initialise(struct domain *d)
 
 static void vmx_domain_destroy(struct domain *d)
 {
+    struct ept_data *ept = &d->arch.hvm_domain.vmx.ept;
     if ( paging_mode_hap(d) )
-        on_each_cpu(__ept_sync_domain, d, 1);
-    free_cpumask_var(d->arch.hvm_domain.vmx.ept_synced);
+        on_each_cpu(__ept_sync_domain, p2m_get_hostp2m(d), 1);
+    free_cpumask_var(ept->ept_synced);
     vmx_free_vlapic_mapping(d);
 }
 
@@ -641,6 +643,7 @@ static void vmx_ctxt_switch_to(struct vcpu *v)
 {
     struct domain *d = v->domain;
     unsigned long old_cr4 = read_cr4(), new_cr4 = mmu_cr4_features;
+    struct ept_data *ept_data = p2m_get_hostp2m(d)->hap_data;
 
     /* HOST_CR4 in VMCS is always mmu_cr4_features. Sync CR4 now. */
     if ( old_cr4 != new_cr4 )
@@ -650,10 +653,10 @@ static void vmx_ctxt_switch_to(struct vcpu *v)
     {
         unsigned int cpu = smp_processor_id();
         /* Test-and-test-and-set this CPU in the EPT-is-synced mask. */
-        if ( !cpumask_test_cpu(cpu, d->arch.hvm_domain.vmx.ept_synced) &&
+        if ( !cpumask_test_cpu(cpu, ept_get_synced_mask(ept_data)) &&
              !cpumask_test_and_set_cpu(cpu,
-                                       d->arch.hvm_domain.vmx.ept_synced) )
-            __invept(INVEPT_SINGLE_CONTEXT, ept_get_eptp(d), 0);
+                                       ept_get_synced_mask(ept_data)) )
+            __invept(INVEPT_SINGLE_CONTEXT, ept_get_eptp(ept_data), 0);
     }
 
     vmx_restore_guest_msrs(v);
@@ -1218,12 +1221,16 @@ static void vmx_update_guest_efer(struct vcpu *v)
 
 static void __ept_sync_domain(void *info)
 {
-    struct domain *d = info;
-    __invept(INVEPT_SINGLE_CONTEXT, ept_get_eptp(d), 0);
+    struct p2m_domain *p2m = info;
+    struct ept_data *ept_data = p2m->hap_data;
+
+    __invept(INVEPT_SINGLE_CONTEXT, ept_get_eptp(ept_data), 0);
 }
 
-void ept_sync_domain(struct domain *d)
+void ept_sync_domain(struct p2m_domain *p2m)
 {
+    struct domain *d = p2m->domain;
+    struct ept_data *ept_data = p2m->hap_data;
     /* Only if using EPT and this domain has some VCPUs to dirty. */
     if ( !paging_mode_hap(d) || !d->vcpu || !d->vcpu[0] )
         return;
@@ -1236,11 +1243,11 @@ void ept_sync_domain(struct domain *d)
      * the ept_synced mask before on_selected_cpus() reads it, resulting in
      * unnecessary extra flushes, to avoid allocating a cpumask_t on the stack.
      */
-    cpumask_and(d->arch.hvm_domain.vmx.ept_synced,
+    cpumask_and(ept_get_synced_mask(ept_data),
                 d->domain_dirty_cpumask, &cpu_online_map);
 
-    on_selected_cpus(d->arch.hvm_domain.vmx.ept_synced,
-                     __ept_sync_domain, d, 1);
+    on_selected_cpus(ept_get_synced_mask(ept_data),
+                     __ept_sync_domain, p2m, 1);
 }
 
 void nvmx_enqueue_n2_exceptions(struct vcpu *v, 
xen/arch/x86/mm/p2m-ept.c +75 -21

207 lines not on this page · raw message

xen/arch/x86/mm/p2m.c +13 -3
index 62c2d78..799bbfb 100644
@@ -105,6 +105,8 @@ p2m_init_nestedp2m(struct domain *d)
         if ( !zalloc_cpumask_var(&p2m->dirty_cpumask) )
             return -ENOMEM;
         p2m_initialise(d, p2m);
+        if ( cpu_has_vmx && alloc_p2m_hap_data(p2m) )
+            return -ENOMEM;
         p2m->write_p2m_entry = nestedp2m_write_p2m_entry;
         list_add(&p2m->np2m_list, &p2m_get_hostp2m(d)->np2m_list);
     }
@@ -126,12 +128,14 @@ int p2m_init(struct domain *d)
         return -ENOMEM;
     }
     p2m_initialise(d, p2m);
+    if ( hap_enabled(d) && cpu_has_vmx)
+        p2m->hap_data = &d->arch.hvm_domain.vmx.ept;
 
     /* Must initialise nestedp2m unconditionally
      * since nestedhvm_enabled(d) returns false here.
      * (p2m_init runs too early for HVM_PARAM_* options) */
     rc = p2m_init_nestedp2m(d);
-    if ( rc ) 
+    if ( rc )
         p2m_final_teardown(d);
     return rc;
 }
@@ -354,6 +358,8 @@ int p2m_alloc_table(struct p2m_domain *p2m)
 
     if ( hap_enabled(d) )
         iommu_share_p2m_table(d);
+    if ( p2m_is_nestedp2m(p2m) && hap_enabled(d) )
+        p2m_init_hap_data(p2m);
 
     P2M_PRINTK("populating p2m table\n");
 
@@ -436,12 +442,16 @@ void p2m_teardown(struct p2m_domain *p2m)
 static void p2m_teardown_nestedp2m(struct domain *d)
 {
     uint8_t i;
+    struct p2m_domain *p2m;
 
     for (i = 0; i < MAX_NESTEDP2M; i++) {
         if ( !d->arch.nested_p2m[i] )
             continue;
-        free_cpumask_var(d->arch.nested_p2m[i]->dirty_cpumask);
-        xfree(d->arch.nested_p2m[i]);
+        p2m = d->arch.nested_p2m[i];
+        if ( p2m->hap_data )
+            free_p2m_hap_data(p2m);
+        free_cpumask_var(p2m->dirty_cpumask);
+        xfree(p2m);
         d->arch.nested_p2m[i] = NULL;
     }
 }
xen/include/asm-x86/hvm/vmx/vmcs.h +19 -11
index 9a728b6..e6b4e3b 100644
@@ -56,26 +56,34 @@ struct vmx_msr_state {
 
 #define EPT_DEFAULT_MT      MTRR_TYPE_WRBACK
 
-struct vmx_domain {
-    unsigned long apic_access_mfn;
-    union {
-        struct {
+union eptp_control{
+    struct {
             u64 ept_mt :3,
                 ept_wl :3,
                 rsvd   :6,
                 asr    :52;
         };
         u64 eptp;
-    } ept_control;
+};
+
+struct ept_data{
+    union eptp_control ept_ctl;
     cpumask_var_t ept_synced;
 };
 
-#define ept_get_wl(d)   \
-    ((d)->arch.hvm_domain.vmx.ept_control.ept_wl)
-#define ept_get_asr(d)  \
-    ((d)->arch.hvm_domain.vmx.ept_control.asr)
-#define ept_get_eptp(d) \
-    ((d)->arch.hvm_domain.vmx.ept_control.eptp)
+struct vmx_domain {
+    unsigned long apic_access_mfn;
+    struct ept_data ept; 
+};
+
+#define ept_get_wl(ept_data)   \
+    (((struct ept_data*)(ept_data))->ept_ctl.ept_wl)
+#define ept_get_asr(ept_data)  \
+    (((struct ept_data*)(ept_data))->ept_ctl.asr)
+#define ept_get_eptp(ept_data) \
+    (((struct ept_data*)(ept_data))->ept_ctl.eptp)
+#define ept_get_synced_mask(ept_data)\
+    (((struct ept_data*)(ept_data))->ept_synced)
 
 struct arch_vmx_struct {
     /* Virtual address of VMCS. */
xen/include/asm-x86/hvm/vmx/vmx.h +5 -1
index aa5b080..573a12e 100644
@@ -333,7 +333,7 @@ static inline void ept_sync_all(void)
     __invept(INVEPT_ALL_CONTEXT, 0, 0);
 }
 
-void ept_sync_domain(struct domain *d);
+void ept_sync_domain(struct p2m_domain *p2m);
 
 static inline void vpid_sync_vcpu_gva(struct vcpu *v, unsigned long gva)
 {
@@ -401,6 +401,10 @@ void setup_ept_dump(void);
 
 void update_guest_eip(void);
 
+int alloc_p2m_hap_data(struct p2m_domain *p2m);
+void free_p2m_hap_data(struct p2m_domain *p2m);
+void p2m_init_hap_data(struct p2m_domain *p2m);
+
 /* EPT violation qualifications definitions */
 #define _EPT_READ_VIOLATION         0
 #define EPT_READ_VIOLATION          (1UL<<_EPT_READ_VIOLATION)
xen/include/asm-x86/p2m.h +1 -0
index 1807ad6..0fb1b2d 100644
@@ -277,6 +277,7 @@ struct p2m_domain {
         mm_lock_t        lock;         /* Locking of private pod structs,   *
                                         * not relying on the p2m lock.      */
     } pod;
+    void *hap_data;
 };
 
 /* get host p2m table */
Signature · 1 line
1.7.1
#10 lore raw

Re: [PATCH 05/11] EPT: Make ept data structure or operations neutral

Quoted 7 lines · At 01:57 +0800 on 11 Dec (1355191037), [email protected] wrote:

From: Zhang Xiantao <[email protected]>

Share the current EPT logic with nested EPT case, so make the related data structure or operations netural to comment EPT and nested EPT.

Since the struct ept_data is only 16 bytes long, why not just embed it in the struct p2m_domain, as

Review

Quoted 8 lines

mm_lock_t lock; /* Locking of private pod structs, *
* not relying on the p2m lock. */
} pod;

(quoted diff) +4 -0
+    union {
+        struct ept_data ept;
+        /* NPT equivalent could go here if needed */
+    };
 };

That would tidy up the alloc/free stuff a fair bit, though you'd still need it for the cpumask, I guess.

It would be nice to wrap the alloc/free functions up in the usual way so we dont get ept-specific functions with arch-independednt names.

Otherwise taht looks fine.

Cheers,

Tim.

Quoted 500 lines
---
 xen/arch/x86/hvm/vmx/vmcs.c        |    2 +-
 xen/arch/x86/hvm/vmx/vmx.c         |   39 +++++++++------
 xen/arch/x86/mm/p2m-ept.c          |   96 ++++++++++++++++++++++++++++--------
 xen/arch/x86/mm/p2m.c              |   16 +++++-
 xen/include/asm-x86/hvm/vmx/vmcs.h |   30 +++++++----
 xen/include/asm-x86/hvm/vmx/vmx.h  |    6 ++-
 xen/include/asm-x86/p2m.h          |    1 +
 7 files changed, 137 insertions(+), 53 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +1 -1
index 9adc7a4..b9ebdfe 100644
@@ -942,7 +942,7 @@ static int construct_vmcs(struct vcpu *v)
     }
 
     if ( paging_mode_hap(d) )
-        __vmwrite(EPT_POINTER, d->arch.hvm_domain.vmx.ept_control.eptp);
+        __vmwrite(EPT_POINTER, d->arch.hvm_domain.vmx.ept.ept_ctl.eptp);
 
     if ( cpu_has_vmx_pat && paging_mode_hap(d) )
     {
xen/arch/x86/hvm/vmx/vmx.c +23 -16
index c67ac59..06455bf 100644
@@ -79,22 +79,23 @@ static void __ept_sync_domain(void *info);
 static int vmx_domain_initialise(struct domain *d)
 {
     int rc;
+    struct ept_data *ept = &d->arch.hvm_domain.vmx.ept;
 
     /* Set the memory type used when accessing EPT paging structures. */
-    d->arch.hvm_domain.vmx.ept_control.ept_mt = EPT_DEFAULT_MT;
+    ept->ept_ctl.ept_mt = EPT_DEFAULT_MT;
 
     /* set EPT page-walk length, now it's actual walk length - 1, i.e. 3 */
-    d->arch.hvm_domain.vmx.ept_control.ept_wl = 3;
+    ept->ept_ctl.ept_wl = 3;
 
-    d->arch.hvm_domain.vmx.ept_control.asr  =
+    ept->ept_ctl.asr  =
         pagetable_get_pfn(p2m_get_pagetable(p2m_get_hostp2m(d)));
 
-    if ( !zalloc_cpumask_var(&d->arch.hvm_domain.vmx.ept_synced) )
+    if ( !zalloc_cpumask_var(&ept->ept_synced) )
         return -ENOMEM;
 
     if ( (rc = vmx_alloc_vlapic_mapping(d)) != 0 )
     {
-        free_cpumask_var(d->arch.hvm_domain.vmx.ept_synced);
+        free_cpumask_var(ept->ept_synced);
         return rc;
     }
 
@@ -103,9 +104,10 @@ static int vmx_domain_initialise(struct domain *d)
 
 static void vmx_domain_destroy(struct domain *d)
 {
+    struct ept_data *ept = &d->arch.hvm_domain.vmx.ept;
     if ( paging_mode_hap(d) )
-        on_each_cpu(__ept_sync_domain, d, 1);
-    free_cpumask_var(d->arch.hvm_domain.vmx.ept_synced);
+        on_each_cpu(__ept_sync_domain, p2m_get_hostp2m(d), 1);
+    free_cpumask_var(ept->ept_synced);
     vmx_free_vlapic_mapping(d);
 }
 
@@ -641,6 +643,7 @@ static void vmx_ctxt_switch_to(struct vcpu *v)
 {
     struct domain *d = v->domain;
     unsigned long old_cr4 = read_cr4(), new_cr4 = mmu_cr4_features;
+    struct ept_data *ept_data = p2m_get_hostp2m(d)->hap_data;
 
     /* HOST_CR4 in VMCS is always mmu_cr4_features. Sync CR4 now. */
     if ( old_cr4 != new_cr4 )
@@ -650,10 +653,10 @@ static void vmx_ctxt_switch_to(struct vcpu *v)
     {
         unsigned int cpu = smp_processor_id();
         /* Test-and-test-and-set this CPU in the EPT-is-synced mask. */
-        if ( !cpumask_test_cpu(cpu, d->arch.hvm_domain.vmx.ept_synced) &&
+        if ( !cpumask_test_cpu(cpu, ept_get_synced_mask(ept_data)) &&
              !cpumask_test_and_set_cpu(cpu,
-                                       d->arch.hvm_domain.vmx.ept_synced) )
-            __invept(INVEPT_SINGLE_CONTEXT, ept_get_eptp(d), 0);
+                                       ept_get_synced_mask(ept_data)) )
+            __invept(INVEPT_SINGLE_CONTEXT, ept_get_eptp(ept_data), 0);
     }
 
     vmx_restore_guest_msrs(v);
@@ -1218,12 +1221,16 @@ static void vmx_update_guest_efer(struct vcpu *v)
 
 static void __ept_sync_domain(void *info)
 {
-    struct domain *d = info;
-    __invept(INVEPT_SINGLE_CONTEXT, ept_get_eptp(d), 0);
+    struct p2m_domain *p2m = info;
+    struct ept_data *ept_data = p2m->hap_data;
+
+    __invept(INVEPT_SINGLE_CONTEXT, ept_get_eptp(ept_data), 0);
 }
 
-void ept_sync_domain(struct domain *d)
+void ept_sync_domain(struct p2m_domain *p2m)
 {
+    struct domain *d = p2m->domain;
+    struct ept_data *ept_data = p2m->hap_data;
     /* Only if using EPT and this domain has some VCPUs to dirty. */
     if ( !paging_mode_hap(d) || !d->vcpu || !d->vcpu[0] )
         return;
@@ -1236,11 +1243,11 @@ void ept_sync_domain(struct domain *d)
      * the ept_synced mask before on_selected_cpus() reads it, resulting in
      * unnecessary extra flushes, to avoid allocating a cpumask_t on the stack.
      */
-    cpumask_and(d->arch.hvm_domain.vmx.ept_synced,
+    cpumask_and(ept_get_synced_mask(ept_data),
                 d->domain_dirty_cpumask, &cpu_online_map);
 
-    on_selected_cpus(d->arch.hvm_domain.vmx.ept_synced,
-                     __ept_sync_domain, d, 1);
+    on_selected_cpus(ept_get_synced_mask(ept_data),
+                     __ept_sync_domain, p2m, 1);
 }
 
 void nvmx_enqueue_n2_exceptions(struct vcpu *v, 
xen/arch/x86/mm/p2m-ept.c +75 -21
index c964f54..8adf3f9 100644
@@ -291,9 +291,11 @@ ept_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn,
     int need_modify_vtd_table = 1;
     int vtd_pte_present = 0;
     int needs_sync = 1;
-    struct domain *d = p2m->domain;
     ept_entry_t old_entry = { .epte = 0 };
+    struct ept_data *ept_data = p2m->hap_data;
+    struct domain *d = p2m->domain;
 
+    ASSERT(ept_data);
     /*
      * the caller must make sure:
      * 1. passing valid gfn and mfn at order boundary.
@@ -301,17 +303,17 @@ ept_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn,
      * 3. passing a valid order.
      */
     if ( ((gfn | mfn_x(mfn)) & ((1UL << order) - 1)) ||
-         ((u64)gfn >> ((ept_get_wl(d) + 1) * EPT_TABLE_ORDER)) ||
+         ((u64)gfn >> ((ept_get_wl(ept_data) + 1) * EPT_TABLE_ORDER)) ||
          (order % EPT_TABLE_ORDER) )
         return 0;
 
-    ASSERT((target == 2 && hvm_hap_has_1gb(d)) ||
-           (target == 1 && hvm_hap_has_2mb(d)) ||
+    ASSERT((target == 2 && hvm_hap_has_1gb()) ||
+           (target == 1 && hvm_hap_has_2mb()) ||
            (target == 0));
 
-    table = map_domain_page(ept_get_asr(d));
+    table = map_domain_page(pagetable_get_pfn(p2m_get_pagetable(p2m)));
 
-    for ( i = ept_get_wl(d); i > target; i-- )
+    for ( i = ept_get_wl(ept_data); i > target; i-- )
     {
         ret = ept_next_level(p2m, 0, &table, &gfn_remainder, i);
         if ( !ret )
@@ -439,9 +441,11 @@ out:
     unmap_domain_page(table);
 
     if ( needs_sync )
-        ept_sync_domain(p2m->domain);
+        ept_sync_domain(p2m);
 
-    if ( rv && iommu_enabled && need_iommu(p2m->domain) && need_modify_vtd_table )
+    /* For non-nested p2m, may need to change VT-d page table.*/
+    if ( rv && !p2m_is_nestedp2m(p2m) && iommu_enabled && need_iommu(p2m->domain) &&
+                need_modify_vtd_table )
     {
         if ( iommu_hap_pt_share )
             iommu_pte_flush(d, gfn, (u64*)ept_entry, order, vtd_pte_present);
@@ -488,14 +492,14 @@ static mfn_t ept_get_entry(struct p2m_domain *p2m,
                            unsigned long gfn, p2m_type_t *t, p2m_access_t* a,
                            p2m_query_t q, unsigned int *page_order)
 {
-    struct domain *d = p2m->domain;
-    ept_entry_t *table = map_domain_page(ept_get_asr(d));
+    ept_entry_t *table = map_domain_page(pagetable_get_pfn(p2m_get_pagetable(p2m)));
     unsigned long gfn_remainder = gfn;
     ept_entry_t *ept_entry;
     u32 index;
     int i;
     int ret = 0;
     mfn_t mfn = _mfn(INVALID_MFN);
+    struct ept_data *ept_data = p2m->hap_data;
 
     *t = p2m_mmio_dm;
     *a = p2m_access_n;
@@ -506,7 +510,7 @@ static mfn_t ept_get_entry(struct p2m_domain *p2m,
 
     /* Should check if gfn obeys GAW here. */
 
-    for ( i = ept_get_wl(d); i > 0; i-- )
+    for ( i = ept_get_wl(ept_data); i > 0; i-- )
     {
     retry:
         ret = ept_next_level(p2m, 1, &table, &gfn_remainder, i);
@@ -588,19 +592,20 @@ out:
 static ept_entry_t ept_get_entry_content(struct p2m_domain *p2m,
     unsigned long gfn, int *level)
 {
-    ept_entry_t *table = map_domain_page(ept_get_asr(p2m->domain));
+    ept_entry_t *table =  map_domain_page(pagetable_get_pfn(p2m_get_pagetable(p2m)));
     unsigned long gfn_remainder = gfn;
     ept_entry_t *ept_entry;
     ept_entry_t content = { .epte = 0 };
     u32 index;
     int i;
     int ret=0;
+    struct ept_data *ept_data = p2m->hap_data;
 
     /* This pfn is higher than the highest the p2m map currently holds */
     if ( gfn > p2m->max_mapped_pfn )
         goto out;
 
-    for ( i = ept_get_wl(p2m->domain); i > 0; i-- )
+    for ( i = ept_get_wl(ept_data); i > 0; i-- )
     {
         ret = ept_next_level(p2m, 1, &table, &gfn_remainder, i);
         if ( !ret || ret == GUEST_TABLE_POD_PAGE )
@@ -622,7 +627,8 @@ static ept_entry_t ept_get_entry_content(struct p2m_domain *p2m,
 void ept_walk_table(struct domain *d, unsigned long gfn)
 {
     struct p2m_domain *p2m = p2m_get_hostp2m(d);
-    ept_entry_t *table = map_domain_page(ept_get_asr(d));
+    struct ept_data *ept_data = p2m->hap_data;
+    ept_entry_t *table =  map_domain_page(pagetable_get_pfn(p2m_get_pagetable(p2m)));
     unsigned long gfn_remainder = gfn;
 
     int i;
@@ -638,7 +644,7 @@ void ept_walk_table(struct domain *d, unsigned long gfn)
         goto out;
     }
 
-    for ( i = ept_get_wl(d); i >= 0; i-- )
+    for ( i = ept_get_wl(ept_data); i >= 0; i-- )
     {
         ept_entry_t *ept_entry, *next;
         u32 index;
@@ -778,16 +784,16 @@ static void ept_change_entry_type_page(mfn_t ept_page_mfn, int ept_page_level,
 static void ept_change_entry_type_global(struct p2m_domain *p2m,
                                          p2m_type_t ot, p2m_type_t nt)
 {
-    struct domain *d = p2m->domain;
-    if ( ept_get_asr(d) == 0 )
+    struct ept_data *ept_data = p2m->hap_data;
+    if ( ept_get_asr(ept_data) == 0 )
         return;
 
     BUG_ON(p2m_is_grant(ot) || p2m_is_grant(nt));
     BUG_ON(ot != nt && (ot == p2m_mmio_direct || nt == p2m_mmio_direct));
 
-    ept_change_entry_type_page(_mfn(ept_get_asr(d)), ept_get_wl(d), ot, nt);
+    ept_change_entry_type_page(_mfn(ept_get_asr(ept_data)), ept_get_wl(ept_data), ot, nt);
 
-    ept_sync_domain(d);
+    ept_sync_domain(p2m);
 }
 
 void ept_p2m_init(struct p2m_domain *p2m)
@@ -811,6 +817,7 @@ static void ept_dump_p2m_table(unsigned char key)
     unsigned long gfn, gfn_remainder;
     unsigned long record_counter = 0;
     struct p2m_domain *p2m;
+    struct ept_data *ept_data;
 
     for_each_domain(d)
     {
@@ -818,15 +825,16 @@ static void ept_dump_p2m_table(unsigned char key)
             continue;
 
         p2m = p2m_get_hostp2m(d);
+    ept_data = p2m->hap_data;
         printk("\ndomain%d EPT p2m table: \n", d->domain_id);
 
         for ( gfn = 0; gfn <= p2m->max_mapped_pfn; gfn += (1 << order) )
         {
             gfn_remainder = gfn;
             mfn = _mfn(INVALID_MFN);
-            table = map_domain_page(ept_get_asr(d));
+            table = map_domain_page(pagetable_get_pfn(p2m_get_pagetable(p2m)));
 
-            for ( i = ept_get_wl(d); i > 0; i-- )
+            for ( i = ept_get_wl(ept_data); i > 0; i-- )
             {
                 ret = ept_next_level(p2m, 1, &table, &gfn_remainder, i);
                 if ( ret != GUEST_TABLE_NORMAL_PAGE )
@@ -858,6 +866,52 @@ out:
     }
 }
 
+int alloc_p2m_hap_data(struct p2m_domain *p2m)
+{
+    struct domain *d = p2m->domain;
+    struct ept_data *ept;
+
+    ASSERT(d);
+    if (!hap_enabled(d))
+        return 0;
+
+    p2m->hap_data = ept = xzalloc(struct ept_data);
+    if ( !p2m->hap_data )
+        return -ENOMEM;
+    if ( !zalloc_cpumask_var(&ept->ept_synced) )
+    {
+        xfree(ept);
+        p2m->hap_data = NULL;
+        return -ENOMEM;    
+    }
+    return 0;
+}
+
+void free_p2m_hap_data(struct p2m_domain *p2m)
+{
+    struct ept_data *ept;
+
+    if ( !hap_enabled(p2m->domain) )
+        return;
+
+    if ( p2m_is_nestedp2m(p2m)) {
+        ept = p2m->hap_data;
+        if ( ept ) {
+            free_cpumask_var(ept->ept_synced);
+            xfree(ept);
+        }
+    }
+}
+
+void p2m_init_hap_data(struct p2m_domain *p2m)
+{
+    struct ept_data *ept = p2m->hap_data;
+
+    ept->ept_ctl.ept_wl = 3;
+    ept->ept_ctl.ept_mt = EPT_DEFAULT_MT;
+    ept->ept_ctl.asr = pagetable_get_pfn(p2m_get_pagetable(p2m));
+}
+
 static struct keyhandler ept_p2m_table = {
     .diagnostic = 0,
     .u.fn = ept_dump_p2m_table,
xen/arch/x86/mm/p2m.c +13 -3
index 62c2d78..799bbfb 100644
@@ -105,6 +105,8 @@ p2m_init_nestedp2m(struct domain *d)
         if ( !zalloc_cpumask_var(&p2m->dirty_cpumask) )
             return -ENOMEM;
         p2m_initialise(d, p2m);
+        if ( cpu_has_vmx && alloc_p2m_hap_data(p2m) )
+            return -ENOMEM;
         p2m->write_p2m_entry = nestedp2m_write_p2m_entry;
         list_add(&p2m->np2m_list, &p2m_get_hostp2m(d)->np2m_list);
     }
@@ -126,12 +128,14 @@ int p2m_init(struct domain *d)
         return -ENOMEM;
     }
     p2m_initialise(d, p2m);
+    if ( hap_enabled(d) && cpu_has_vmx)
+        p2m->hap_data = &d->arch.hvm_domain.vmx.ept;
 
     /* Must initialise nestedp2m unconditionally
      * since nestedhvm_enabled(d) returns false here.
      * (p2m_init runs too early for HVM_PARAM_* options) */
     rc = p2m_init_nestedp2m(d);
-    if ( rc ) 
+    if ( rc )
         p2m_final_teardown(d);
     return rc;
 }
@@ -354,6 +358,8 @@ int p2m_alloc_table(struct p2m_domain *p2m)
 
     if ( hap_enabled(d) )
         iommu_share_p2m_table(d);
+    if ( p2m_is_nestedp2m(p2m) && hap_enabled(d) )
+        p2m_init_hap_data(p2m);
 
     P2M_PRINTK("populating p2m table\n");
 
@@ -436,12 +442,16 @@ void p2m_teardown(struct p2m_domain *p2m)
 static void p2m_teardown_nestedp2m(struct domain *d)
 {
     uint8_t i;
+    struct p2m_domain *p2m;
 
     for (i = 0; i < MAX_NESTEDP2M; i++) {
         if ( !d->arch.nested_p2m[i] )
             continue;
-        free_cpumask_var(d->arch.nested_p2m[i]->dirty_cpumask);
-        xfree(d->arch.nested_p2m[i]);
+        p2m = d->arch.nested_p2m[i];
+        if ( p2m->hap_data )
+            free_p2m_hap_data(p2m);
+        free_cpumask_var(p2m->dirty_cpumask);
+        xfree(p2m);
         d->arch.nested_p2m[i] = NULL;
     }
 }
xen/include/asm-x86/hvm/vmx/vmcs.h +19 -11
index 9a728b6..e6b4e3b 100644
@@ -56,26 +56,34 @@ struct vmx_msr_state {
 
 #define EPT_DEFAULT_MT      MTRR_TYPE_WRBACK
 
-struct vmx_domain {
-    unsigned long apic_access_mfn;
-    union {
-        struct {
+union eptp_control{
+    struct {
             u64 ept_mt :3,
                 ept_wl :3,
                 rsvd   :6,
                 asr    :52;
         };
         u64 eptp;
-    } ept_control;
+};
+
+struct ept_data{
+    union eptp_control ept_ctl;
     cpumask_var_t ept_synced;
 };
 
-#define ept_get_wl(d)   \
-    ((d)->arch.hvm_domain.vmx.ept_control.ept_wl)
-#define ept_get_asr(d)  \
-    ((d)->arch.hvm_domain.vmx.ept_control.asr)
-#define ept_get_eptp(d) \
-    ((d)->arch.hvm_domain.vmx.ept_control.eptp)
+struct vmx_domain {
+    unsigned long apic_access_mfn;
+    struct ept_data ept; 
+};
+
+#define ept_get_wl(ept_data)   \
+    (((struct ept_data*)(ept_data))->ept_ctl.ept_wl)
+#define ept_get_asr(ept_data)  \
+    (((struct ept_data*)(ept_data))->ept_ctl.asr)
+#define ept_get_eptp(ept_data) \
+    (((struct ept_data*)(ept_data))->ept_ctl.eptp)
+#define ept_get_synced_mask(ept_data)\
+    (((struct ept_data*)(ept_data))->ept_synced)
 
 struct arch_vmx_struct {
     /* Virtual address of VMCS. */
xen/include/asm-x86/hvm/vmx/vmx.h +5 -1
index aa5b080..573a12e 100644
@@ -333,7 +333,7 @@ static inline void ept_sync_all(void)
     __invept(INVEPT_ALL_CONTEXT, 0, 0);
 }
 
-void ept_sync_domain(struct domain *d);
+void ept_sync_domain(struct p2m_domain *p2m);
 
 static inline void vpid_sync_vcpu_gva(struct vcpu *v, unsigned long gva)
 {
@@ -401,6 +401,10 @@ void setup_ept_dump(void);
 
 void update_guest_eip(void);
 
+int alloc_p2m_hap_data(struct p2m_domain *p2m);
+void free_p2m_hap_data(struct p2m_domain *p2m);
+void p2m_init_hap_data(struct p2m_domain *p2m);
+
 /* EPT violation qualifications definitions */
 #define _EPT_READ_VIOLATION         0
 #define EPT_READ_VIOLATION          (1UL<<_EPT_READ_VIOLATION)
xen/include/asm-x86/p2m.h +1 -0
index 1807ad6..0fb1b2d 100644
@@ -277,6 +277,7 @@ struct p2m_domain {
         mm_lock_t        lock;         /* Locking of private pod structs,   *
                                         * not relying on the p2m lock.      */
     } pod;
+    void *hap_data;
 };
 
 /* get host p2m table */
Signature · 7 lines
1.7.1


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#11 lore raw

Re: [PATCH 05/11] EPT: Make ept data structure or operations neutral

Quoted 13 lines

-----Original Message-----
From: Tim Deegan [mailto:[email protected]]
Sent: Friday, December 14, 2012 12:04 AM
To: Zhang, Xiantao
Cc: [email protected]; Dong, Eddie; [email protected]; Nakajima, Jun; [email protected]
Subject: Re: [Xen-devel] [PATCH 05/11] EPT: Make ept data structure or operations neutral

Quoted 2 lines · At 01:57 +0800 on 11 Dec (1355191037), [email protected] wrote:

From: Zhang Xiantao <[email protected]>

Quoted 16 lines
Quoted 4 lines

Share the current EPT logic with nested EPT case, so make the related data structure or operations netural to comment EPT and nested EPT.

Since the struct ept_data is only 16 bytes long, why not just embed it in the struct p2m_domain, as

Quoted 8 lines

mm_lock_t lock; /* Locking of private pod structs, *
* not relying on the p2m lock. */
} pod;

(quoted diff) +4 -0
+    union {
+        struct ept_data ept;
+        /* NPT equivalent could go here if needed */
+    };
 };

Hi, Tim
Thanks for your review! If we change it like this, p2m.h have to include asm/hvm/vmx/vmcs.h, is it acceptable ?
Xiantao

Quoted 531 lines

That would tidy up the alloc/free stuff a fair bit, though you'd still need it for the cpumask, I guess.

It would be nice to wrap the alloc/free functions up in the usual way so we dont get ept-specific functions with arch-independednt names.

Otherwise taht looks fine.

Cheers,

Tim.

Quoted 4 lines
---
 xen/arch/x86/hvm/vmx/vmcs.c        |    2 +-
 xen/arch/x86/hvm/vmx/vmx.c         |   39 +++++++++------
 xen/arch/x86/mm/p2m-ept.c          |   96

++++++++++++++++++++++++++++--------

Review

Quoted 7 lines
 xen/arch/x86/mm/p2m.c              |   16 +++++-
 xen/include/asm-x86/hvm/vmx/vmcs.h |   30 +++++++----
 xen/include/asm-x86/hvm/vmx/vmx.h  |    6 ++-
 xen/include/asm-x86/p2m.h          |    1 +
 7 files changed, 137 insertions(+), 53 deletions(-)
(quoted diff) +0 -0

b/xen/arch/x86/hvm/vmx/vmcs.c

Review

Quoted 15 lines

index 9adc7a4..b9ebdfe 100644

xen/arch/x86/hvm/vmx/vmcs.c +2 -1
@@ -942,7 +942,7 @@ static int construct_vmcs(struct vcpu *v)
     }
 
     if ( paging_mode_hap(d) )
-        __vmwrite(EPT_POINTER, d-
 arch.hvm_domain.vmx.ept_control.eptp);
+        __vmwrite(EPT_POINTER,
+ d->arch.hvm_domain.vmx.ept.ept_ctl.eptp);
 
     if ( cpu_has_vmx_pat && paging_mode_hap(d) )
     {
(quoted diff) +0 -0

b/xen/arch/x86/hvm/vmx/vmx.c

Quoted 52 lines

index c67ac59..06455bf 100644

xen/arch/x86/hvm/vmx/vmx.c +10 -7
@@ -79,22 +79,23 @@ static void __ept_sync_domain(void *info);  static
 int vmx_domain_initialise(struct domain *d)  {
     int rc;
+    struct ept_data *ept = &d->arch.hvm_domain.vmx.ept;
 
     /* Set the memory type used when accessing EPT paging structures. */
-    d->arch.hvm_domain.vmx.ept_control.ept_mt = EPT_DEFAULT_MT;
+    ept->ept_ctl.ept_mt = EPT_DEFAULT_MT;
 
     /* set EPT page-walk length, now it's actual walk length - 1, i.e. 3 */
-    d->arch.hvm_domain.vmx.ept_control.ept_wl = 3;
+    ept->ept_ctl.ept_wl = 3;
 
-    d->arch.hvm_domain.vmx.ept_control.asr  =
+    ept->ept_ctl.asr  =
         pagetable_get_pfn(p2m_get_pagetable(p2m_get_hostp2m(d)));
 
-    if ( !zalloc_cpumask_var(&d->arch.hvm_domain.vmx.ept_synced) )
+    if ( !zalloc_cpumask_var(&ept->ept_synced) )
         return -ENOMEM;
 
     if ( (rc = vmx_alloc_vlapic_mapping(d)) != 0 )
     {
-        free_cpumask_var(d->arch.hvm_domain.vmx.ept_synced);
+        free_cpumask_var(ept->ept_synced);
         return rc;
     }
 
@@ -103,9 +104,10 @@ static int vmx_domain_initialise(struct domain
 *d)
 
 static void vmx_domain_destroy(struct domain *d)  {
+    struct ept_data *ept = &d->arch.hvm_domain.vmx.ept;
     if ( paging_mode_hap(d) )
-        on_each_cpu(__ept_sync_domain, d, 1);
-    free_cpumask_var(d->arch.hvm_domain.vmx.ept_synced);
+        on_each_cpu(__ept_sync_domain, p2m_get_hostp2m(d), 1);
+    free_cpumask_var(ept->ept_synced);
     vmx_free_vlapic_mapping(d);
 }
 
@@ -641,6 +643,7 @@ static void vmx_ctxt_switch_to(struct vcpu *v)  {
     struct domain *d = v->domain;
     unsigned long old_cr4 = read_cr4(), new_cr4 = mmu_cr4_features;
+    struct ept_data *ept_data = p2m_get_hostp2m(d)->hap_data;
 
     /* HOST_CR4 in VMCS is always mmu_cr4_features. Sync CR4 now. */
     if ( old_cr4 != new_cr4 )

xen/arch/x86/hvm/vmx/vmx.c static void vmx_ctxt_switch_to(struct vcpu *v) · line 653

Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +0 -1
@@ -650,10 +653,10 @@ static void vmx_ctxt_switch_to(struct vcpu *v)
     {
         unsigned int cpu = smp_processor_id();
         /* Test-and-test-and-set this CPU in the EPT-is-synced mask. */
-        if ( !cpumask_test_cpu(cpu, d->arch.hvm_domain.vmx.ept_synced)

&&

Quoted 31 lines
xen/arch/x86/hvm/vmx/vmx.c +11 -5
+        if ( !cpumask_test_cpu(cpu, ept_get_synced_mask(ept_data)) &&
              !cpumask_test_and_set_cpu(cpu,
-                                       d->arch.hvm_domain.vmx.ept_synced) )
-            __invept(INVEPT_SINGLE_CONTEXT, ept_get_eptp(d), 0);
+                                       ept_get_synced_mask(ept_data)) )
+            __invept(INVEPT_SINGLE_CONTEXT, ept_get_eptp(ept_data),
+ 0);
     }
 
     vmx_restore_guest_msrs(v);
@@ -1218,12 +1221,16 @@ static void vmx_update_guest_efer(struct vcpu
 *v)
 
 static void __ept_sync_domain(void *info)  {
-    struct domain *d = info;
-    __invept(INVEPT_SINGLE_CONTEXT, ept_get_eptp(d), 0);
+    struct p2m_domain *p2m = info;
+    struct ept_data *ept_data = p2m->hap_data;
+
+    __invept(INVEPT_SINGLE_CONTEXT, ept_get_eptp(ept_data), 0);
 }
 
-void ept_sync_domain(struct domain *d)
+void ept_sync_domain(struct p2m_domain *p2m)
 {
+    struct domain *d = p2m->domain;
+    struct ept_data *ept_data = p2m->hap_data;
     /* Only if using EPT and this domain has some VCPUs to dirty. */
     if ( !paging_mode_hap(d) || !d->vcpu || !d->vcpu[0] )
         return;

xen/arch/x86/hvm/vmx/vmx.c void ept_sync_domain(struct domain *d) · line 1244

Quoted 2 lines
xen/arch/x86/hvm/vmx/vmx.c +0 -0
@@ -1236,11 +1243,11 @@ void ept_sync_domain(struct domain *d)
      * the ept_synced mask before on_selected_cpus() reads it, resulting in
      * unnecessary extra flushes, to avoid allocating a cpumask_t on the

stack.

xen/arch/x86/mm/p2m-ept.c ept_set_entry(struct p2m_domain *p2m,

Quoted 17 lines
xen/arch/x86/hvm/vmx/vmx.c +3 -3
      */
-    cpumask_and(d->arch.hvm_domain.vmx.ept_synced,
+    cpumask_and(ept_get_synced_mask(ept_data),
                 d->domain_dirty_cpumask, &cpu_online_map);
 
-    on_selected_cpus(d->arch.hvm_domain.vmx.ept_synced,
-                     __ept_sync_domain, d, 1);
+    on_selected_cpus(ept_get_synced_mask(ept_data),
+                     __ept_sync_domain, p2m, 1);
 }
 
 void nvmx_enqueue_n2_exceptions(struct vcpu *v, diff --git

a/xen/arch/x86/mm/p2m-ept.c b/xen/arch/x86/mm/p2m-ept.c index c964f54..8adf3f9 100644

xen/arch/x86/mm/p2m-ept.c +0 -0
@@ -291,9 +291,11 @@ ept_set_entry(struct p2m_domain *p2m,

unsigned long gfn, mfn_t mfn,

xen/arch/x86/mm/p2m-ept.c ept_set_entry(struct p2m_domain *p2m,

Quoted 13 lines
xen/arch/x86/mm/p2m-ept.c +3 -1
     int need_modify_vtd_table = 1;
     int vtd_pte_present = 0;
     int needs_sync = 1;
-    struct domain *d = p2m->domain;
     ept_entry_t old_entry = { .epte = 0 };
+    struct ept_data *ept_data = p2m->hap_data;
+    struct domain *d = p2m->domain;
 
+    ASSERT(ept_data);
     /*
      * the caller must make sure:
      * 1. passing valid gfn and mfn at order boundary.
@@ -301,17 +303,17 @@ ept_set_entry(struct p2m_domain *p2m,

unsigned long gfn, mfn_t mfn,

Quoted 25 lines
xen/arch/x86/mm/p2m-ept.c +7 -5
      * 3. passing a valid order.
      */
     if ( ((gfn | mfn_x(mfn)) & ((1UL << order) - 1)) ||
-         ((u64)gfn >> ((ept_get_wl(d) + 1) * EPT_TABLE_ORDER)) ||
+         ((u64)gfn >> ((ept_get_wl(ept_data) + 1) * EPT_TABLE_ORDER))
+ ||
          (order % EPT_TABLE_ORDER) )
         return 0;
 
-    ASSERT((target == 2 && hvm_hap_has_1gb(d)) ||
-           (target == 1 && hvm_hap_has_2mb(d)) ||
+    ASSERT((target == 2 && hvm_hap_has_1gb()) ||
+           (target == 1 && hvm_hap_has_2mb()) ||
            (target == 0));
 
-    table = map_domain_page(ept_get_asr(d));
+    table =
+ map_domain_page(pagetable_get_pfn(p2m_get_pagetable(p2m)));
 
-    for ( i = ept_get_wl(d); i > target; i-- )
+    for ( i = ept_get_wl(ept_data); i > target; i-- )
     {
         ret = ept_next_level(p2m, 0, &table, &gfn_remainder, i);
         if ( !ret )

xen/arch/x86/mm/p2m-ept.c out: · line 444

Quoted 7 lines
xen/arch/x86/mm/p2m-ept.c +1 -2
@@ -439,9 +441,11 @@ out:
     unmap_domain_page(table);
 
     if ( needs_sync )
-        ept_sync_domain(p2m->domain);
+        ept_sync_domain(p2m);
 
-    if ( rv && iommu_enabled && need_iommu(p2m->domain) &&

need_modify_vtd_table )

xen/arch/x86/mm/p2m-ept.c out: · line 447

Quoted 2 lines
xen/arch/x86/mm/p2m-ept.c +2 -0
+    /* For non-nested p2m, may need to change VT-d page table.*/
+    if ( rv && !p2m_is_nestedp2m(p2m) && iommu_enabled &&

need_iommu(p2m->domain) &&

Quoted 5 lines
xen/arch/x86/mm/p2m-ept.c +1 -0
+                need_modify_vtd_table )
     {
         if ( iommu_hap_pt_share )
             iommu_pte_flush(d, gfn, (u64*)ept_entry, order,

vtd_pte_present); @@ -488,14 +492,14 @@ static mfn_t

ept_get_entry(struct p2m_domain *p2m,

xen/arch/x86/mm/p2m-ept.c static mfn_t ept_get_entry(struct p2m_domain

Quoted 18 lines

unsigned long gfn, p2m_type_t *t, p2m_access_t* a, p2m_query_t q, unsigned int *page_order)
{

xen/arch/x86/mm/p2m-ept.c +3 -2
-    struct domain *d = p2m->domain;
-    ept_entry_t *table = map_domain_page(ept_get_asr(d));
+    ept_entry_t *table =
+ map_domain_page(pagetable_get_pfn(p2m_get_pagetable(p2m)));
     unsigned long gfn_remainder = gfn;
     ept_entry_t *ept_entry;
     u32 index;
     int i;
     int ret = 0;
     mfn_t mfn = _mfn(INVALID_MFN);
+    struct ept_data *ept_data = p2m->hap_data;
 
     *t = p2m_mmio_dm;
     *a = p2m_access_n;
@@ -506,7 +510,7 @@ static mfn_t ept_get_entry(struct p2m_domain

*p2m,

xen/arch/x86/mm/p2m-ept.c void ept_walk_table(struct domain *d, unsigned

Quoted 43 lines
xen/arch/x86/mm/p2m-ept.c +5 -4
     /* Should check if gfn obeys GAW here. */
 
-    for ( i = ept_get_wl(d); i > 0; i-- )
+    for ( i = ept_get_wl(ept_data); i > 0; i-- )
     {
     retry:
         ret = ept_next_level(p2m, 1, &table, &gfn_remainder, i); @@
-588,19 +592,20 @@ out:
 static ept_entry_t ept_get_entry_content(struct p2m_domain *p2m,
     unsigned long gfn, int *level)
 {
-    ept_entry_t *table = map_domain_page(ept_get_asr(p2m->domain));
+    ept_entry_t *table =
+ map_domain_page(pagetable_get_pfn(p2m_get_pagetable(p2m)));
     unsigned long gfn_remainder = gfn;
     ept_entry_t *ept_entry;
     ept_entry_t content = { .epte = 0 };
     u32 index;
     int i;
     int ret=0;
+    struct ept_data *ept_data = p2m->hap_data;
 
     /* This pfn is higher than the highest the p2m map currently holds */
     if ( gfn > p2m->max_mapped_pfn )
         goto out;
 
-    for ( i = ept_get_wl(p2m->domain); i > 0; i-- )
+    for ( i = ept_get_wl(ept_data); i > 0; i-- )
     {
         ret = ept_next_level(p2m, 1, &table, &gfn_remainder, i);
         if ( !ret || ret == GUEST_TABLE_POD_PAGE ) @@ -622,7 +627,8

@@ static ept_entry_t ept_get_entry_content(struct p2m_domain *p2m, void ept_walk_table(struct domain *d, unsigned long gfn) {

     struct p2m_domain *p2m = p2m_get_hostp2m(d);
xen/arch/x86/mm/p2m-ept.c +3 -1
-    ept_entry_t *table = map_domain_page(ept_get_asr(d));
+    struct ept_data *ept_data = p2m->hap_data;
+    ept_entry_t *table =
+ map_domain_page(pagetable_get_pfn(p2m_get_pagetable(p2m)));
     unsigned long gfn_remainder = gfn;
 
     int i;
@@ -638,7 +644,7 @@ void ept_walk_table(struct domain *d, unsigned

long gfn)

Quoted 10 lines
xen/arch/x86/mm/p2m-ept.c +1 -1
         goto out;
     }
 
-    for ( i = ept_get_wl(d); i >= 0; i-- )
+    for ( i = ept_get_wl(ept_data); i >= 0; i-- )
     {
         ept_entry_t *ept_entry, *next;
         u32 index;
@@ -778,16 +784,16 @@ static void ept_change_entry_type_page(mfn_t

ept_page_mfn, int ept_page_level, static void

ept_change_entry_type_global(struct p2m_domain *p2m,

Quoted 12 lines
                                          p2m_type_t ot, p2m_type_t

nt) {

xen/arch/x86/mm/p2m-ept.c +2 -2
-    struct domain *d = p2m->domain;
-    if ( ept_get_asr(d) == 0 )
+    struct ept_data *ept_data = p2m->hap_data;
+    if ( ept_get_asr(ept_data) == 0 )
         return;
 
     BUG_ON(p2m_is_grant(ot) || p2m_is_grant(nt));
     BUG_ON(ot != nt && (ot == p2m_mmio_direct || nt ==

p2m_mmio_direct));

Quoted 1 line

- ept_change_entry_type_page(_mfn(ept_get_asr(d)), ept_get_wl(d),

ot, nt);

xen/arch/x86/mm/p2m-ept.c static void ept_dump_p2m_table(unsigned char

Quoted 17 lines
xen/arch/x86/mm/p2m-ept.c +3 -1
+    ept_change_entry_type_page(_mfn(ept_get_asr(ept_data)),
+ ept_get_wl(ept_data), ot, nt);
 
-    ept_sync_domain(d);
+    ept_sync_domain(p2m);
 }
 
 void ept_p2m_init(struct p2m_domain *p2m) @@ -811,6 +817,7 @@ static

void ept_dump_p2m_table(unsigned char key)

     unsigned long gfn, gfn_remainder;
     unsigned long record_counter = 0;
     struct p2m_domain *p2m;
xen/arch/x86/mm/p2m-ept.c +1 -0
+    struct ept_data *ept_data;
 
     for_each_domain(d)
     {
@@ -818,15 +825,16 @@ static void ept_dump_p2m_table(unsigned char

key)

Quoted 219 lines
xen/arch/x86/mm/p2m-ept.c +48 -2
             continue;
 
         p2m = p2m_get_hostp2m(d);
+    ept_data = p2m->hap_data;
         printk("\ndomain%d EPT p2m table: \n", d->domain_id);
 
         for ( gfn = 0; gfn <= p2m->max_mapped_pfn; gfn += (1 << order) )
         {
             gfn_remainder = gfn;
             mfn = _mfn(INVALID_MFN);
-            table = map_domain_page(ept_get_asr(d));
+            table =
+ map_domain_page(pagetable_get_pfn(p2m_get_pagetable(p2m)));
 
-            for ( i = ept_get_wl(d); i > 0; i-- )
+            for ( i = ept_get_wl(ept_data); i > 0; i-- )
             {
                 ret = ept_next_level(p2m, 1, &table, &gfn_remainder, i);
                 if ( ret != GUEST_TABLE_NORMAL_PAGE ) @@ -858,6
+866,52 @@ out:
     }
 }
 
+int alloc_p2m_hap_data(struct p2m_domain *p2m) {
+    struct domain *d = p2m->domain;
+    struct ept_data *ept;
+
+    ASSERT(d);
+    if (!hap_enabled(d))
+        return 0;
+
+    p2m->hap_data = ept = xzalloc(struct ept_data);
+    if ( !p2m->hap_data )
+        return -ENOMEM;
+    if ( !zalloc_cpumask_var(&ept->ept_synced) )
+    {
+        xfree(ept);
+        p2m->hap_data = NULL;
+        return -ENOMEM;
+    }
+    return 0;
+}
+
+void free_p2m_hap_data(struct p2m_domain *p2m) {
+    struct ept_data *ept;
+
+    if ( !hap_enabled(p2m->domain) )
+        return;
+
+    if ( p2m_is_nestedp2m(p2m)) {
+        ept = p2m->hap_data;
+        if ( ept ) {
+            free_cpumask_var(ept->ept_synced);
+            xfree(ept);
+        }
+    }
+}
+
+void p2m_init_hap_data(struct p2m_domain *p2m) {
+    struct ept_data *ept = p2m->hap_data;
+
+    ept->ept_ctl.ept_wl = 3;
+    ept->ept_ctl.ept_mt = EPT_DEFAULT_MT;
+    ept->ept_ctl.asr = pagetable_get_pfn(p2m_get_pagetable(p2m));
+}
+
 static struct keyhandler ept_p2m_table = {
     .diagnostic = 0,
     .u.fn = ept_dump_p2m_table,
xen/arch/x86/mm/p2m.c index renamed from xen/arch/x86/mm/p2m.c +0 -0

62c2d78..799bbfb 100644

xen/arch/x86/mm/p2m.c +13 -3
@@ -105,6 +105,8 @@ p2m_init_nestedp2m(struct domain *d)
         if ( !zalloc_cpumask_var(&p2m->dirty_cpumask) )
             return -ENOMEM;
         p2m_initialise(d, p2m);
+        if ( cpu_has_vmx && alloc_p2m_hap_data(p2m) )
+            return -ENOMEM;
         p2m->write_p2m_entry = nestedp2m_write_p2m_entry;
         list_add(&p2m->np2m_list, &p2m_get_hostp2m(d)->np2m_list);
     }
@@ -126,12 +128,14 @@ int p2m_init(struct domain *d)
         return -ENOMEM;
     }
     p2m_initialise(d, p2m);
+    if ( hap_enabled(d) && cpu_has_vmx)
+        p2m->hap_data = &d->arch.hvm_domain.vmx.ept;
 
     /* Must initialise nestedp2m unconditionally
      * since nestedhvm_enabled(d) returns false here.
      * (p2m_init runs too early for HVM_PARAM_* options) */
     rc = p2m_init_nestedp2m(d);
-    if ( rc )
+    if ( rc )
         p2m_final_teardown(d);
     return rc;
 }
@@ -354,6 +358,8 @@ int p2m_alloc_table(struct p2m_domain *p2m)
 
     if ( hap_enabled(d) )
         iommu_share_p2m_table(d);
+    if ( p2m_is_nestedp2m(p2m) && hap_enabled(d) )
+        p2m_init_hap_data(p2m);
 
     P2M_PRINTK("populating p2m table\n");
 
@@ -436,12 +442,16 @@ void p2m_teardown(struct p2m_domain *p2m)
 static void p2m_teardown_nestedp2m(struct domain *d)  {
     uint8_t i;
+    struct p2m_domain *p2m;
 
     for (i = 0; i < MAX_NESTEDP2M; i++) {
         if ( !d->arch.nested_p2m[i] )
             continue;
-        free_cpumask_var(d->arch.nested_p2m[i]->dirty_cpumask);
-        xfree(d->arch.nested_p2m[i]);
+        p2m = d->arch.nested_p2m[i];
+        if ( p2m->hap_data )
+            free_p2m_hap_data(p2m);
+        free_cpumask_var(p2m->dirty_cpumask);
+        xfree(p2m);
         d->arch.nested_p2m[i] = NULL;
     }
 }
xen/include/asm-x86/hvm/vmx/vmcs.h +19 -11
index 9a728b6..e6b4e3b 100644
@@ -56,26 +56,34 @@ struct vmx_msr_state {
 
 #define EPT_DEFAULT_MT      MTRR_TYPE_WRBACK
 
-struct vmx_domain {
-    unsigned long apic_access_mfn;
-    union {
-        struct {
+union eptp_control{
+    struct {
             u64 ept_mt :3,
                 ept_wl :3,
                 rsvd   :6,
                 asr    :52;
         };
         u64 eptp;
-    } ept_control;
+};
+
+struct ept_data{
+    union eptp_control ept_ctl;
     cpumask_var_t ept_synced;
 };
 
-#define ept_get_wl(d)   \
-    ((d)->arch.hvm_domain.vmx.ept_control.ept_wl)
-#define ept_get_asr(d)  \
-    ((d)->arch.hvm_domain.vmx.ept_control.asr)
-#define ept_get_eptp(d) \
-    ((d)->arch.hvm_domain.vmx.ept_control.eptp)
+struct vmx_domain {
+    unsigned long apic_access_mfn;
+    struct ept_data ept;
+};
+
+#define ept_get_wl(ept_data)   \
+    (((struct ept_data*)(ept_data))->ept_ctl.ept_wl)
+#define ept_get_asr(ept_data)  \
+    (((struct ept_data*)(ept_data))->ept_ctl.asr)
+#define ept_get_eptp(ept_data) \
+    (((struct ept_data*)(ept_data))->ept_ctl.eptp)
+#define ept_get_synced_mask(ept_data)\
+    (((struct ept_data*)(ept_data))->ept_synced)
 
 struct arch_vmx_struct {
     /* Virtual address of VMCS. */
xen/include/asm-x86/hvm/vmx/vmx.h +1 -1
index aa5b080..573a12e 100644
@@ -333,7 +333,7 @@ static inline void ept_sync_all(void)
     __invept(INVEPT_ALL_CONTEXT, 0, 0);  }
 
-void ept_sync_domain(struct domain *d);
+void ept_sync_domain(struct p2m_domain *p2m);
 
 static inline void vpid_sync_vcpu_gva(struct vcpu *v, unsigned long
 gva)  { @@ -401,6 +401,10 @@ void setup_ept_dump(void);
 

void update_guest_eip(void);

xen/include/asm-x86/hvm/vmx/vmx.h +4 -0
+int alloc_p2m_hap_data(struct p2m_domain *p2m); void
+free_p2m_hap_data(struct p2m_domain *p2m); void
+p2m_init_hap_data(struct p2m_domain *p2m);
+
 /* EPT violation qualifications definitions */
 #define _EPT_READ_VIOLATION         0
 #define EPT_READ_VIOLATION          (1UL<<_EPT_READ_VIOLATION)
xen/include/asm-x86/p2m.h +1 -1
index 1807ad6..0fb1b2d 100644
@@ -277,6 +277,7 @@ struct p2m_domain {
         mm_lock_t        lock;         /* Locking of private pod structs,   *
                                         * not relying on the p2m lock.      */
     } pod;
+    void *hap_data;
 };
 
 /* get host p2m table */
--

1.7.1

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

#12 lore raw

Re: [PATCH 05/11] EPT: Make ept data structure or operations neutral

Quoted 34 lines
Quoted 12 lines
Quoted 1 line · Xiantao

-----Original Message-----
From: Tim Deegan [mailto:[email protected]]
Sent: Friday, December 14, 2012 12:04 AM
To: Zhang, Xiantao
Cc: [email protected]; Dong, Eddie; [email protected]; Nakajima, Jun; [email protected]
Subject: Re: [Xen-devel] [PATCH 05/11] EPT: Make ept data structure or operations neutral

At 01:57 +0800 on 11 Dec (1355191037), [email protected] wrote:

Quoted 8 lines
Quoted 6 lines

From: Zhang Xiantao <[email protected]>

Share the current EPT logic with nested EPT case, so make the related data structure or operations netural to comment EPT and nested EPT.

Since the struct ept_data is only 16 bytes long, why not just embed it in

the

Quoted 3 lines

struct p2m_domain, as

Quoted 1 line

mm_lock_t lock; /* Locking of private pod structs,

*

Quoted 1 line
Quoted 1 line

* not relying on the p2m lock.

*/

Quoted 6 lines

Review

Quoted 6 lines
     } pod;
(quoted diff) +4 -0
+    union {
+        struct ept_data ept;
+        /* NPT equivalent could go here if needed */
+    };
 };
Quoted 3 lines

Hi, Tim
Thanks for your review! If we change it like this, p2m.h have to
include asm/hvm/vmx/vmcs.h, is it acceptable ?

I'm sure there are ways to avoid such a dependency.

Jan

#13 lore raw

PATCH 6/11 nEPT: Try to enable EPT paging for L2 guest.

In reply to [email protected]

From: Zhang Xiantao <[email protected]>

Once found EPT is enabled by L1 VMM, enabled nested EPT support for L2 guest.

---
 xen/arch/x86/hvm/vmx/vmx.c         |   16 +++++++++--
 xen/arch/x86/hvm/vmx/vvmx.c        |   50 ++++++++++++++++++++++++++++--------
 xen/include/asm-x86/hvm/vmx/vvmx.h |    5 +++-
 3 files changed, 56 insertions(+), 15 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +13 -3
index 06455bf..1bfb67f 100644
@@ -1513,6 +1513,7 @@ static struct hvm_function_table __read_mostly vmx_function_table = {
     .nhvm_vcpu_guestcr3   = nvmx_vcpu_guestcr3,
     .nhvm_vcpu_p2m_base   = nvmx_vcpu_eptp_base,
     .nhvm_vcpu_asid       = nvmx_vcpu_asid,
+    .nhvm_vmcx_hap_enabled = nvmx_ept_enabled,
     .nhvm_vmcx_guest_intercepts_trap = nvmx_intercepts_exception,
     .nhvm_vcpu_vmexit_trap = nvmx_vmexit_trap,
     .nhvm_intr_blocked    = nvmx_intr_blocked,
@@ -2055,6 +2056,7 @@ static void ept_handle_violation(unsigned long qualification, paddr_t gpa)
     unsigned long gla, gfn = gpa >> PAGE_SHIFT;
     mfn_t mfn;
     p2m_type_t p2mt;
+    int ret;
     struct domain *d = current->domain;
 
     if ( tb_init_done )
@@ -2073,14 +2075,22 @@ static void ept_handle_violation(unsigned long qualification, paddr_t gpa)
         __trace_var(TRC_HVM_NPF, 0, sizeof(_d), &_d);
     }
 
-    if ( hvm_hap_nested_page_fault(gpa,
+    ret = hvm_hap_nested_page_fault(gpa,
                                    qualification & EPT_GLA_VALID       ? 1 : 0,
                                    qualification & EPT_GLA_VALID
                                      ? __vmread(GUEST_LINEAR_ADDRESS) : ~0ull,
                                    qualification & EPT_READ_VIOLATION  ? 1 : 0,
                                    qualification & EPT_WRITE_VIOLATION ? 1 : 0,
-                                   qualification & EPT_EXEC_VIOLATION  ? 1 : 0) )
-        return;
+                                   qualification & EPT_EXEC_VIOLATION  ? 1 : 0);
+    switch (ret) {
+        case 0:
+            break;
+        case 1:
+            return;
+        case -1:
+            vcpu_nestedhvm(current).nv_vmexit_pending = 1;
+            return;
+    }
 
     /* Everything else is an error. */
     mfn = get_gfn_query_unlocked(d, gfn, &p2mt);
xen/arch/x86/hvm/vmx/vvmx.c +39 -11

103 lines not on this page · raw message

xen/include/asm-x86/hvm/vmx/vvmx.h +4 -1
index 8eb377b..661cd8a 100644
@@ -33,9 +33,10 @@ struct nestedvmx {
         u32           error_code;
     } intr;
     struct {
+        char     enabled;
         uint32_t exit_reason;
         uint32_t exit_qual;
-    } ept_exit;
+    } ept;
 };
 
 #define vcpu_2_nvmx(v)	(vcpu_nestedhvm(v).u.nvmx)
@@ -110,6 +111,8 @@ int nvmx_intercepts_exception(struct vcpu *v,
                               unsigned int trap, int error_code);
 void nvmx_domain_relinquish_resources(struct domain *d);
 
+bool_t nvmx_ept_enabled(struct vcpu *v);
+
 int nvmx_handle_vmxon(struct cpu_user_regs *regs);
 int nvmx_handle_vmxoff(struct cpu_user_regs *regs);
 
Signature · 1 line
1.7.1
#15 lore raw

PATCH 7/11 nEPT: Sync PDPTR fields if L2 guest in PAE paging mode

In reply to [email protected]

From: Zhang Xiantao <[email protected]>

For PAE L2 guest, GUEST_DPPTR registers needs to be synced for each virtual vmentry.

---
 xen/arch/x86/hvm/vmx/vvmx.c |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/vmx/vvmx.c +8 -2
index ab68b52..3fc128b 100644
@@ -824,9 +824,15 @@ static void load_shadow_guest_state(struct vcpu *v)
     vvmcs_to_shadow(vvmcs, CR0_READ_SHADOW);
     vvmcs_to_shadow(vvmcs, CR4_READ_SHADOW);
     vvmcs_to_shadow(vvmcs, CR0_GUEST_HOST_MASK);
-    vvmcs_to_shadow(vvmcs, CR4_GUEST_HOST_MASK);
 
-    /* TODO: PDPTRs for nested ept */
+    if ( nvmx_ept_enabled(v) && hvm_pae_enabled(v) &&
+                    (v->arch.hvm_vcpu.guest_efer & EFER_LMA) ) {
+	    vvmcs_to_shadow(vvmcs, GUEST_PDPTR0);
+	    vvmcs_to_shadow(vvmcs, GUEST_PDPTR1);
+	    vvmcs_to_shadow(vvmcs, GUEST_PDPTR2);
+	    vvmcs_to_shadow(vvmcs, GUEST_PDPTR3);
+    }
+
     /* TODO: CR3 target control */
 }
 
Signature · 1 line
1.7.1
#16 lore raw

Re: [PATCH 07/11] nEPT: Sync PDPTR fields if L2 guest in PAE paging mode

Quoted 14 lines · At 01:57 +0800 on 11 Dec (1355191039), [email protected] wrote:

From: Zhang Xiantao <[email protected]>

For PAE L2 guest, GUEST_DPPTR registers needs to be synced for each virtual vmentry.

---
 xen/arch/x86/hvm/vmx/vvmx.c |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/vmx/vvmx.c +0 -0
index ab68b52..3fc128b 100644

xen/arch/x86/hvm/vmx/vvmx.c static void load_shadow_guest_state(struct vcpu *v) · line 827

Quoted 4 lines
xen/arch/x86/hvm/vmx/vvmx.c +0 -1
@@ -824,9 +824,15 @@ static void load_shadow_guest_state(struct vcpu *v)
     vvmcs_to_shadow(vvmcs, CR0_READ_SHADOW);
     vvmcs_to_shadow(vvmcs, CR4_READ_SHADOW);
     vvmcs_to_shadow(vvmcs, CR0_GUEST_HOST_MASK);
-    vvmcs_to_shadow(vvmcs, CR4_GUEST_HOST_MASK);

Did you really mean to remove this line as well? If so, it'll need some explanation in the checkin description.

Tim.

Quoted 21 lines
xen/arch/x86/hvm/vmx/vvmx.c +8 -1
-    /* TODO: PDPTRs for nested ept */
+    if ( nvmx_ept_enabled(v) && hvm_pae_enabled(v) &&
+                    (v->arch.hvm_vcpu.guest_efer & EFER_LMA) ) {
+	    vvmcs_to_shadow(vvmcs, GUEST_PDPTR0);
+	    vvmcs_to_shadow(vvmcs, GUEST_PDPTR1);
+	    vvmcs_to_shadow(vvmcs, GUEST_PDPTR2);
+	    vvmcs_to_shadow(vvmcs, GUEST_PDPTR3);
+    }
+
     /* TODO: CR3 target control */
 }
 
Signature · 7 lines
1.7.1


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#17 lore raw

PATCH 8/11 nEPT: Use minimal permission for nested p2m.

In reply to [email protected]

From: Zhang Xiantao <[email protected]>

Emulate permission check for the nested p2m. Current solution is to use minimal permission, and once meet permission violation in L0, then determin whether it is caused by guest EPT or host EPT

---
 xen/arch/x86/hvm/svm/nestedsvm.c        |    2 +-
 xen/arch/x86/hvm/vmx/vvmx.c             |    4 ++--
 xen/arch/x86/mm/hap/nested_ept.c        |    9 +++++----
 xen/arch/x86/mm/hap/nested_hap.c        |   22 +++++++++++++---------
 xen/include/asm-x86/hvm/hvm.h           |    2 +-
 xen/include/asm-x86/hvm/svm/nestedsvm.h |    2 +-
 xen/include/asm-x86/hvm/vmx/vvmx.h      |    6 +++---
 7 files changed, 26 insertions(+), 21 deletions(-)
xen/arch/x86/hvm/svm/nestedsvm.c +1 -1
index 5dcb354..ab455a9 100644
@@ -1177,7 +1177,7 @@ nsvm_vmcb_hap_enabled(struct vcpu *v)
  */
 int
 nsvm_hap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
-                      unsigned int *page_order,
+                      unsigned int *page_order, uint8_t *p2m_acc,
                       bool_t access_r, bool_t access_w, bool_t access_x)
 {
     uint32_t pfec;
xen/arch/x86/hvm/vmx/vvmx.c +2 -2
index 3fc128b..41779bc 100644
@@ -1494,7 +1494,7 @@ int nvmx_msr_write_intercept(unsigned int msr, u64 msr_content)
  */
 int
 nvmx_hap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
-                      unsigned int *page_order,
+                      unsigned int *page_order, uint8_t *p2m_acc,
                       bool_t access_r, bool_t access_w, bool_t access_x)
 {
     uint64_t exit_qual = __vmread(EXIT_QUALIFICATION);
@@ -1504,7 +1504,7 @@ nvmx_hap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
     uint32_t rwx_rights = (access_x << 2) | (access_w << 1) | access_r;
     struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
 
-    rc = nept_translate_l2ga(v, L2_gpa, page_order, rwx_rights, &gfn,
+    rc = nept_translate_l2ga(v, L2_gpa, page_order, rwx_rights, &gfn, p2m_acc,
                                 &exit_qual, &exit_reason);
     switch ( rc ) {
         case EPT_TRANSLATE_SUCCEED:
xen/arch/x86/mm/hap/nested_ept.c +5 -4
index 2d733a8..637db1a 100644
@@ -286,8 +286,8 @@ bool_t nept_permission_check(uint32_t rwx_acc, uint32_t rwx_bits)
 
 int nept_translate_l2ga(struct vcpu *v, paddr_t l2ga, 
                         unsigned int *page_order, uint32_t rwx_acc,
-                        unsigned long *l1gfn, uint64_t *exit_qual,
-                        uint32_t *exit_reason)
+                        unsigned long *l1gfn, uint8_t *p2m_acc,
+                        uint64_t *exit_qual, uint32_t *exit_reason)
 {
     uint32_t rc, rwx_bits = 0;
     walk_t gw;
@@ -317,8 +317,9 @@ int nept_translate_l2ga(struct vcpu *v, paddr_t l2ga,
             }
             if ( nept_permission_check(rwx_acc, rwx_bits) )
             {
-                 *l1gfn = guest_l1e_get_paddr(gw.l1e) >> PAGE_SHIFT;
-                 break;
+                *l1gfn = guest_l1e_get_paddr(gw.l1e) >> PAGE_SHIFT;
+                *p2m_acc = (uint8_t)rwx_bits;
+                break;
             }
             rc = EPT_TRANSLATE_VIOLATION;
         /* Fall through to EPT violation if permission check fails. */
xen/arch/x86/mm/hap/nested_hap.c +13 -9
index 6d1264b..9c1654d 100644
@@ -142,12 +142,12 @@ nestedhap_fix_p2m(struct vcpu *v, struct p2m_domain *p2m,
  */
 static int
 nestedhap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
-                      unsigned int *page_order,
+                      unsigned int *page_order, uint8_t *p2m_acc,
                       bool_t access_r, bool_t access_w, bool_t access_x)
 {
     ASSERT(hvm_funcs.nhvm_hap_walk_L1_p2m);
 
-    return hvm_funcs.nhvm_hap_walk_L1_p2m(v, L2_gpa, L1_gpa, page_order,
+    return hvm_funcs.nhvm_hap_walk_L1_p2m(v, L2_gpa, L1_gpa, page_order, p2m_acc,
         access_r, access_w, access_x);
 }
 
@@ -158,16 +158,15 @@ nestedhap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
  */
 static int
 nestedhap_walk_L0_p2m(struct p2m_domain *p2m, paddr_t L1_gpa, paddr_t *L0_gpa,
-                      p2m_type_t *p2mt,
+                      p2m_type_t *p2mt, p2m_access_t *p2ma,
                       unsigned int *page_order,
                       bool_t access_r, bool_t access_w, bool_t access_x)
 {
     mfn_t mfn;
-    p2m_access_t p2ma;
     int rc;
 
     /* walk L0 P2M table */
-    mfn = get_gfn_type_access(p2m, L1_gpa >> PAGE_SHIFT, p2mt, &p2ma, 
+    mfn = get_gfn_type_access(p2m, L1_gpa >> PAGE_SHIFT, p2mt, p2ma, 
                               0, page_order);
 
     rc = NESTEDHVM_PAGEFAULT_MMIO;
@@ -206,12 +205,14 @@ nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa,
     struct p2m_domain *p2m, *nested_p2m;
     unsigned int page_order_21, page_order_10, page_order_20;
     p2m_type_t p2mt_10;
+    p2m_access_t p2ma_10;
+    uint8_t p2ma_21;
 
     p2m = p2m_get_hostp2m(d); /* L0 p2m */
     nested_p2m = p2m_get_nestedp2m(v, nhvm_vcpu_p2m_base(v));
 
     /* walk the L1 P2M table */
-    rv = nestedhap_walk_L1_p2m(v, *L2_gpa, &L1_gpa, &page_order_21,
+    rv = nestedhap_walk_L1_p2m(v, *L2_gpa, &L1_gpa, &page_order_21, &p2ma_21,
         access_r, access_w, access_x);
 
     /* let caller to handle these two cases */
@@ -229,7 +230,7 @@ nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa,
 
     /* ==> we have to walk L0 P2M */
     rv = nestedhap_walk_L0_p2m(p2m, L1_gpa, &L0_gpa,
-        &p2mt_10, &page_order_10,
+        &p2mt_10, &p2ma_10, &page_order_10,
         access_r, access_w, access_x);
 
     /* let upper level caller to handle these two cases */
@@ -250,10 +251,13 @@ nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa,
 
     page_order_20 = min(page_order_21, page_order_10);
 
+    if (p2ma_10 > p2m_access_rwx)
+        p2ma_10 = p2m_access_rwx;
+    p2ma_10 &= (p2m_access_t)p2ma_21; /* Use minimal permission for nested p2m. */
+
     /* fix p2m_get_pagetable(nested_p2m) */
     nestedhap_fix_p2m(v, nested_p2m, *L2_gpa, L0_gpa, page_order_20,
-        p2mt_10,
-        p2m_access_rwx /* FIXME: Should use minimum permission. */);
+        p2mt_10, p2ma_10);
 
     return NESTEDHVM_PAGEFAULT_DONE;
 }
xen/include/asm-x86/hvm/hvm.h +1 -1
index 80f07e9..889e3c9 100644
@@ -186,7 +186,7 @@ struct hvm_function_table {
 
     /*Walk nested p2m  */
     int (*nhvm_hap_walk_L1_p2m)(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
-                      unsigned int *page_order,
+                      unsigned int *page_order, uint8_t *p2m_acc,
                       bool_t access_r, bool_t access_w, bool_t access_x);
 };
 
xen/include/asm-x86/hvm/svm/nestedsvm.h +1 -1
index 0c90f30..748cc04 100644
@@ -134,7 +134,7 @@ void svm_vmexit_do_clgi(struct cpu_user_regs *regs, struct vcpu *v);
 void svm_vmexit_do_stgi(struct cpu_user_regs *regs, struct vcpu *v);
 bool_t nestedsvm_gif_isset(struct vcpu *v);
 int nsvm_hap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
-                      unsigned int *page_order,
+                      unsigned int *page_order, uint8_t *p2m_acc,
                       bool_t access_r, bool_t access_w, bool_t access_x);
 
 #define NSVM_INTR_NOTHANDLED     3
xen/include/asm-x86/hvm/vmx/vvmx.h +3 -3
index 661cd8a..55c0ad1 100644
@@ -124,7 +124,7 @@ int nvmx_handle_vmxoff(struct cpu_user_regs *regs);
 
 int
 nvmx_hap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
-                      unsigned int *page_order,
+                      unsigned int *page_order, uint8_t *p2m_acc,
                       bool_t access_r, bool_t access_w, bool_t access_x);
 /*
  * Virtual VMCS layout
@@ -207,7 +207,7 @@ int nvmx_n2_vmexit_handler(struct cpu_user_regs *regs,
 
 int nept_translate_l2ga(struct vcpu *v, paddr_t l2ga, 
                         unsigned int *page_order, uint32_t rwx_acc,
-                        unsigned long *l1gfn, uint64_t *exit_qual,
-                        uint32_t *exit_reason);
+                        unsigned long *l1gfn, uint8_t *p2m_acc,
+                        uint64_t *exit_qual, uint32_t *exit_reason);
 #endif /* __ASM_X86_HVM_VVMX_H__ */
 
Signature · 1 line
1.7.1
#18 lore raw

Re: [PATCH 08/11] nEPT: Use minimal permission for nested p2m.

Quoted 11 lines · At 01:57 +0800 on 11 Dec (1355191040), [email protected] wrote:

From: Zhang Xiantao <[email protected]>

Emulate permission check for the nested p2m. Current solution is to use minimal permission, and once meet permission violation in L0, then determin whether it is caused by guest EPT or host EPT

xen/arch/x86/hvm/svm/nestedsvm.c nsvm_vmcb_hap_enabled(struct vcpu *v) · line 1181

Quoted 6 lines
xen/arch/x86/hvm/svm/nestedsvm.c +1 -1
@@ -1177,7 +1177,7 @@ nsvm_vmcb_hap_enabled(struct vcpu *v)
  */
 int
 nsvm_hap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
-                      unsigned int *page_order,
+                      unsigned int *page_order, uint8_t *p2m_acc,
                       bool_t access_r, bool_t access_w, bool_t access_x)

I don't like these interface changes (see below) but if we do have them, at least make the SVM version use p2m_access_rwx, to match the old behaviour, rather than letting it use an uninitialised stack variable. :)

xen/arch/x86/hvm/svm/nestedsvm.c nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa, · line 255

Quoted 6 lines
xen/arch/x86/hvm/svm/nestedsvm.c +2 -0
@@ -250,10 +251,13 @@ nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa,
 
     page_order_20 = min(page_order_21, page_order_10);
 
+    if (p2ma_10 > p2m_access_rwx)
+        p2ma_10 = p2m_access_rwx;

That's plain wrong. If the access type is p2m_access_rx2rw, this will give the guest write access to what ought to be a read-only page.

I think it would be best to leave the p2m-access stuff to the p2m walkers, and not add all those extra p2ma arguments. Instead, just use
the _actual_ access permissions of this fault as the p2ma. That way
you know you have something that's acceptabel to both p2m tables.

I guess that will mean some extra faults on read-then-write behaviour. If those are measurable, we could look at pulling the p2m-access types out like this, but you'll have to explicitly handle all the special types.

Cheers,

Tim.

#19 lore raw

PATCH 9/11 nEPT: handle invept instruction from L1 VMM

In reply to [email protected]

From: Zhang Xiantao <[email protected]>

Add the INVEPT instruction emulation logic.

---
 xen/arch/x86/hvm/vmx/vmx.c         |    6 +++-
 xen/arch/x86/hvm/vmx/vvmx.c        |   37 ++++++++++++++++++++++++++++++++++++
 xen/arch/x86/mm/p2m.c              |    2 +-
 xen/include/asm-x86/hvm/vmx/vvmx.h |    1 +
 4 files changed, 43 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +4 -2
index 1bfb67f..36f6d82 100644
@@ -2622,11 +2622,13 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
         if ( nvmx_handle_vmresume(regs) == X86EMUL_OKAY )
             update_guest_eip();
         break;
-
+    case EXIT_REASON_INVEPT:
+        if ( nvmx_handle_invept(regs) == X86EMUL_OKAY )
+            update_guest_eip();
+        break;
     case EXIT_REASON_MWAIT_INSTRUCTION:
     case EXIT_REASON_MONITOR_INSTRUCTION:
     case EXIT_REASON_GETSEC:
-    case EXIT_REASON_INVEPT:
     case EXIT_REASON_INVVPID:
         /*
          * We should never exit on GETSEC because CR4.SMXE is always 0 when
xen/arch/x86/hvm/vmx/vvmx.c +37 -0
index 41779bc..07ca90e 100644
@@ -1357,6 +1357,43 @@ int nvmx_handle_vmwrite(struct cpu_user_regs *regs)
     return X86EMUL_OKAY;
 }
 
+int nvmx_handle_invept(struct cpu_user_regs *regs)
+{
+    struct vmx_inst_decoded decode;
+    unsigned long eptp;
+    u64 inv_type;
+
+    if ( decode_vmx_inst(regs, &decode, &eptp, 0)
+             != X86EMUL_OKAY )
+        return X86EMUL_EXCEPTION;
+
+    inv_type = reg_read(regs, decode.reg2);
+    gdprintk(XENLOG_DEBUG,"inv_type:%ld, eptp:%lx\n", inv_type, eptp);
+
+    switch (inv_type){
+    case INVEPT_SINGLE_CONTEXT:
+        {
+            struct p2m_domain *p2m = vcpu_nestedhvm(current).nv_p2m;
+            if ( p2m )
+            {
+	            p2m_flush(current, p2m);
+		        ept_sync_domain(p2m);
+            }
+        }
+		break;
+   case INVEPT_ALL_CONTEXT:
+       p2m_flush_nestedp2m(current->domain);
+       __invept(INVEPT_ALL_CONTEXT, 0, 0);
+       break;
+   default:
+       return X86EMUL_EXCEPTION;
+   }
+   vmreturn(regs, VMSUCCEED);
+
+   return X86EMUL_OKAY;
+}
+
+
 #define __emul_value(enable1, default1) \
     ((enable1 | default1) << 32 | (default1))
 
xen/arch/x86/mm/p2m.c +1 -1
index 799bbfb..657fc03 100644
@@ -1478,7 +1478,7 @@ p2m_flush_table(struct p2m_domain *p2m)
 void
 p2m_flush(struct vcpu *v, struct p2m_domain *p2m)
 {
-    ASSERT(v->domain == p2m->domain);
+    ASSERT(p2m && v->domain == p2m->domain);
     vcpu_nestedhvm(v).nv_p2m = NULL;
     p2m_flush_table(p2m);
     hvm_asid_flush_vcpu(v);
xen/include/asm-x86/hvm/vmx/vvmx.h +1 -0
index 55c0ad1..cf5ed9a 100644
@@ -190,6 +190,7 @@ int nvmx_handle_vmread(struct cpu_user_regs *regs);
 int nvmx_handle_vmwrite(struct cpu_user_regs *regs);
 int nvmx_handle_vmresume(struct cpu_user_regs *regs);
 int nvmx_handle_vmlaunch(struct cpu_user_regs *regs);
+int nvmx_handle_invept(struct cpu_user_regs *regs);
 int nvmx_msr_read_intercept(unsigned int msr,
                                 u64 *msr_content);
 int nvmx_msr_write_intercept(unsigned int msr,
Signature · 1 line
1.7.1
#20 lore raw

Re: [PATCH 09/11] nEPT: handle invept instruction from L1 VMM

Quoted 5 lines · At 01:57 +0800 on 11 Dec (1355191041), [email protected] wrote:

From: Zhang Xiantao <[email protected]>

Add the INVEPT instruction emulation logic.

Looks fine, but you have some whitespace problems...

Quoted 4 lines
xen/arch/x86/hvm/vmx/vvmx.c +4 -0
+int nvmx_handle_invept(struct cpu_user_regs *regs)
+{
+    struct vmx_inst_decoded decode;
+    unsigned long eptp;

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_handle_vmwrite(struct cpu_user_regs *regs) · line 1373

Quoted 10 lines
xen/arch/x86/hvm/vmx/vvmx.c +10 -0
+    u64 inv_type;
+
+    if ( decode_vmx_inst(regs, &decode, &eptp, 0)
+             != X86EMUL_OKAY )
+        return X86EMUL_EXCEPTION;
+
+    inv_type = reg_read(regs, decode.reg2);
+    gdprintk(XENLOG_DEBUG,"inv_type:%ld, eptp:%lx\n", inv_type, eptp);
+
+    switch (inv_type){

here

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_handle_vmwrite(struct cpu_user_regs *regs) · line 1380

Quoted 7 lines
xen/arch/x86/hvm/vmx/vvmx.c +7 -0
+    case INVEPT_SINGLE_CONTEXT:
+        {
+            struct p2m_domain *p2m = vcpu_nestedhvm(current).nv_p2m;
+            if ( p2m )
+            {
+	            p2m_flush(current, p2m);
+		        ept_sync_domain(p2m);

and again here (hard tabs)

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_handle_vmwrite(struct cpu_user_regs *regs) · line 1383

Quoted 3 lines
xen/arch/x86/hvm/vmx/vvmx.c +3 -0
+            }
+        }
+		break;

and again.

With those fixed, Acked-by: Tim Deegan <[email protected]> (again with the caveat that this isn't under x86/mm)

Cheers,

Tim.

#21 lore raw

PATCH 10/11 nEPT: expost EPT capablity to L1 VMM

In reply to [email protected]

From: Zhang Xiantao <[email protected]>

Expose EPT's basic features to L1 VMM.
No EPT A/D bit feature supported.

---
 xen/arch/x86/hvm/vmx/vvmx.c        |    6 +++++-
 xen/arch/x86/mm/hap/nested_ept.c   |    2 +-
 xen/include/asm-x86/hvm/vmx/vvmx.h |    2 ++
 3 files changed, 8 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/vmx/vvmx.c +5 -1
index 07ca90e..ec875d2 100644
@@ -1457,7 +1457,8 @@ int nvmx_msr_read_intercept(unsigned int msr, u64 *msr_content)
     case MSR_IA32_VMX_PROCBASED_CTLS2:
         /* 1-seetings */
         data = SECONDARY_EXEC_DESCRIPTOR_TABLE_EXITING |
-               SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
+               SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
+               SECONDARY_EXEC_ENABLE_EPT;
         data = gen_vmx_msr(data, 0, host_data);
         break;
     case MSR_IA32_VMX_EXIT_CTLS:
@@ -1510,6 +1511,9 @@ int nvmx_msr_read_intercept(unsigned int msr, u64 *msr_content)
     case MSR_IA32_VMX_MISC:
         gdprintk(XENLOG_WARNING, "VMX MSR %x not fully supported yet.\n", msr);
         break;
+    case MSR_IA32_VMX_EPT_VPID_CAP:
+        data = nept_get_ept_vpid_cap();
+        break;
     default:
         r = 0;
         break;
xen/arch/x86/mm/hap/nested_ept.c +1 -1
index 637db1a..8dfb70a 100644
@@ -48,7 +48,7 @@
 #define EPT_EMT_WB  6
 #define EPT_EMT_UC  0
 
-#define NEPT_VPID_CAP_BITS 0
+#define NEPT_VPID_CAP_BITS 0x0000000006134140ul
 
 #define NEPT_1G_ENTRY_FLAG (1 << 11)
 #define NEPT_2M_ENTRY_FLAG (1 << 10)
xen/include/asm-x86/hvm/vmx/vvmx.h +2 -0
index cf5ed9a..fcdce62 100644
@@ -206,6 +206,8 @@ u64 nvmx_get_tsc_offset(struct vcpu *v);
 int nvmx_n2_vmexit_handler(struct cpu_user_regs *regs,
                           unsigned int exit_reason);
 
+uint64_t nept_get_ept_vpid_cap(void);
+
 int nept_translate_l2ga(struct vcpu *v, paddr_t l2ga, 
                         unsigned int *page_order, uint32_t rwx_acc,
                         unsigned long *l1gfn, uint8_t *p2m_acc,
Signature · 1 line
1.7.1
#22 lore raw

Re: [PATCH 10/11] nEPT: expost EPT capablity to L1 VMM

xen/arch/x86/mm/hap/nested_ept.c line 51

Quoted 8 lines · At 01:57 +0800 on 11 Dec (1355191042), [email protected] wrote:
xen/arch/x86/mm/hap/nested_ept.c +1 -1
@@ -48,7 +48,7 @@
 #define EPT_EMT_WB  6
 #define EPT_EMT_UC  0
 
-#define NEPT_VPID_CAP_BITS 0
+#define NEPT_VPID_CAP_BITS 0x0000000006134140ul

Ah, I didn't spot this earlier. I think for clarity the definition of nept_get_ept_vpid_cap() should be moved entirely into this faile (and presuambly the TODO comment can be removed).

Where does the magic number 0x0000000006134140ul come from? Can it be broken out into meaningful constants?

Cheers,

Tim.

#23 lore raw

PATCH 11/11 nVMX: Expose VPID capability to nested VMM.

In reply to [email protected]

From: Zhang Xiantao <[email protected]>

Virtualize VPID for the nested vmm, use host's VPID to emualte guest's VPID. For each virtual vmentry, if guest'v vpid is changed, allocate a new host VPID for L2 guest.

---
 xen/arch/x86/hvm/vmx/vmx.c         |   10 +++++-
 xen/arch/x86/hvm/vmx/vvmx.c        |   60 +++++++++++++++++++++++++++++++++++-
 xen/arch/x86/mm/hap/nested_ept.c   |    7 ++--
 xen/include/asm-x86/hvm/vmx/vvmx.h |    2 +
 4 files changed, 73 insertions(+), 6 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +8 -2
index 36f6d82..fb40392 100644
@@ -2626,10 +2626,13 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
         if ( nvmx_handle_invept(regs) == X86EMUL_OKAY )
             update_guest_eip();
         break;
+    case EXIT_REASON_INVVPID:
+        if ( nvmx_handle_invvpid(regs) == X86EMUL_OKAY )
+            update_guest_eip();
+        break;
     case EXIT_REASON_MWAIT_INSTRUCTION:
     case EXIT_REASON_MONITOR_INSTRUCTION:
     case EXIT_REASON_GETSEC:
-    case EXIT_REASON_INVVPID:
         /*
          * We should never exit on GETSEC because CR4.SMXE is always 0 when
          * running in guest context, and the CPU checks that before getting
@@ -2747,8 +2750,11 @@ void vmx_vmenter_helper(void)
 
     if ( !cpu_has_vmx_vpid )
         goto out;
+    if ( nestedhvm_vcpu_in_guestmode(curr) )
+        p_asid =  &vcpu_nestedhvm(curr).nv_n2asid;
+    else
+        p_asid = &curr->arch.hvm_vcpu.n1asid;
 
-    p_asid = &curr->arch.hvm_vcpu.n1asid;
     old_asid = p_asid->asid;
     need_flush = hvm_asid_handle_vmenter(p_asid);
     new_asid = p_asid->asid;
xen/arch/x86/hvm/vmx/vvmx.c +59 -1

102 lines not on this page · raw message

xen/arch/x86/mm/hap/nested_ept.c +4 -3
index 8dfb70a..d0be5ce 100644
@@ -48,7 +48,7 @@
 #define EPT_EMT_WB  6
 #define EPT_EMT_UC  0
 
-#define NEPT_VPID_CAP_BITS 0x0000000006134140ul
+#define NEPT_VPID_CAP_BITS 0xf0106134140ul
 
 #define NEPT_1G_ENTRY_FLAG (1 << 11)
 #define NEPT_2M_ENTRY_FLAG (1 << 10)
@@ -126,8 +126,9 @@ static bool_t nept_present_check(uint64_t entry)
 
 uint64_t nept_get_ept_vpid_cap(void)
 {
-    /*TODO: exposed ept and vpid features*/
-    return NEPT_VPID_CAP_BITS;
+    if (cpu_has_vmx_ept && cpu_has_vmx_vpid)
+        return NEPT_VPID_CAP_BITS;
+    return 0;
 }
 
 static uint32_t
xen/include/asm-x86/hvm/vmx/vvmx.h +2 -0
index fcdce62..1e7a6d7 100644
@@ -37,6 +37,7 @@ struct nestedvmx {
         uint32_t exit_reason;
         uint32_t exit_qual;
     } ept;
+    uint32_t guest_vpid;
 };
 
 #define vcpu_2_nvmx(v)	(vcpu_nestedhvm(v).u.nvmx)
@@ -191,6 +192,7 @@ int nvmx_handle_vmwrite(struct cpu_user_regs *regs);
 int nvmx_handle_vmresume(struct cpu_user_regs *regs);
 int nvmx_handle_vmlaunch(struct cpu_user_regs *regs);
 int nvmx_handle_invept(struct cpu_user_regs *regs);
+int nvmx_handle_invvpid(struct cpu_user_regs *regs);
 int nvmx_msr_read_intercept(unsigned int msr,
                                 u64 *msr_content);
 int nvmx_msr_write_intercept(unsigned int msr,
Signature · 1 line
1.7.1
Acked-by on v1 11/11
#24 lore raw

Re: [PATCH 11/11] nVMX: Expose VPID capability to nested VMM.

Quoted 6 lines · At 01:57 +0800 on 11 Dec (1355191043), [email protected] wrote:

From: Zhang Xiantao <[email protected]>

Virtualize VPID for the nested vmm, use host's VPID to emualte guest's VPID. For each virtual vmentry, if guest'v vpid is changed, allocate a new host VPID for L2 guest.

Looks fine to me, but there's some whitespace mangling:

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmenter_helper(void) · line 2754

Quoted 6 lines
xen/arch/x86/hvm/vmx/vmx.c +2 -0
@@ -2747,8 +2750,11 @@ void vmx_vmenter_helper(void)
 
     if ( !cpu_has_vmx_vpid )
         goto out;
+    if ( nestedhvm_vcpu_in_guestmode(curr) )
+        p_asid =  &vcpu_nestedhvm(curr).nv_n2asid;

here (after '='),

xen/arch/x86/hvm/vmx/vvmx.c static void virtual_vmentry(struct cpu_user_regs *regs) · line 915

Quoted 9 lines
xen/arch/x86/hvm/vmx/vvmx.c +5 -0
@@ -897,6 +908,18 @@ static void virtual_vmentry(struct cpu_user_regs *regs)
     if ( nestedhvm_paging_mode_hap(v) )
         __vmwrite(EPT_POINTER, get_shadow_eptp(v));
 
+    /* nested VPID support! */
+    if ( cpu_has_vmx_vpid && nvmx_vpid_enabled(nvcpu) )
+    {
+        struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
+        uint32_t new_vpid =  __get_vvmcs(vvmcs, VIRTUAL_PROCESSOR_ID);

here (after '='),

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_handle_invept(struct cpu_user_regs *regs) · line 1389

Quoted 5 lines
xen/arch/x86/hvm/vmx/vvmx.c +1 -0
@@ -1363,6 +1386,9 @@ int nvmx_handle_invept(struct cpu_user_regs *regs)
     unsigned long eptp;
     u64 inv_type;
 
+    if(!cpu_has_vmx_ept)

here,

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_handle_invept(struct cpu_user_regs *regs) · line 1436

Quoted 11 lines
xen/arch/x86/hvm/vmx/vvmx.c +7 -0
@@ -1401,6 +1427,37 @@ int nvmx_handle_invept(struct cpu_user_regs *regs)
     (((__emul_value(enable1, default1) & host_value) & (~0ul << 32)) | \
     ((uint32_t)(__emul_value(enable1, default1) | host_value)))
 
+int nvmx_handle_invvpid(struct cpu_user_regs *regs)
+{
+    struct vmx_inst_decoded decode;
+    unsigned long vpid;
+    u64 inv_type;
+
+    if(!cpu_has_vmx_vpid)

here,

Quoted 9 lines
xen/arch/x86/hvm/vmx/vvmx.c +9 -0
+        return X86EMUL_EXCEPTION;
+
+    if ( decode_vmx_inst(regs, &decode, &vpid, 0)
+             != X86EMUL_OKAY )
+        return X86EMUL_EXCEPTION;
+
+    inv_type = reg_read(regs, decode.reg2);
+    gdprintk(XENLOG_DEBUG,"inv_type:%ld, vpid:%lx\n", inv_type, vpid);
+

xen/arch/x86/hvm/vmx/vvmx.c int nvmx_handle_invept(struct cpu_user_regs *regs) · line 1455

Quoted 10 lines
xen/arch/x86/hvm/vmx/vvmx.c +10 -0
+    switch ( inv_type ){
+        /* Just invalidate all tlb entries for all types! */
+        case INVVPID_INDIVIDUAL_ADDR:
+	    case INVVPID_SINGLE_CONTEXT:
+	    case INVVPID_ALL_CONTEXT:
+            hvm_asid_flush_vcpu_asid(&vcpu_nestedhvm(current).nv_n2asid);
+            break;
+	    default:
+		    return X86EMUL_EXCEPTION;
+	}

here (lots of tabs),

xen/arch/x86/mm/hap/nested_ept.c static bool_t nept_present_check(uint64_t entry) · line 129

Quoted 7 lines
xen/arch/x86/mm/hap/nested_ept.c +1 -2
@@ -126,8 +126,9 @@ static bool_t nept_present_check(uint64_t entry)
 
 uint64_t nept_get_ept_vpid_cap(void)
 {
-    /*TODO: exposed ept and vpid features*/
-    return NEPT_VPID_CAP_BITS;
+    if (cpu_has_vmx_ept && cpu_has_vmx_vpid)

and here.

With those fixed,

#25 lore raw
In reply to [email protected]

Hi, Guys
Do you have comments for this patchset ? Thanks!
Xiantao

Quoted 55 lines

-----Original Message-----
From: Zhang, Xiantao
Sent: Tuesday, December 11, 2012 1:57 AM
To: [email protected]
Cc: [email protected]; [email protected]; Dong, Eddie; Nakajima, Jun; Zhang, Xiantao
Subject: [PATCH 00/11] Add virtual EPT support Xen.

From: Zhang Xiantao <[email protected]>

With virtual EPT support, L1 hyerpvisor can use EPT hardware for L2 guest's memory virtualization. In this way, L2 guest's performance can be improved sharply. According to our testing, some benchmarks can show > 5x performance gain.

Zhang Xiantao (11):
nestedhap: Change hostcr3 and p2m->cr3 to meaningful words
nestedhap: Change nested p2m's walker to vendor-specific
nested_ept: Implement guest ept's walker
nested_ept: Add permission check for success case
EPT: Make ept data structure or operations neutral
nEPT: Try to enable EPT paging for L2 guest.
nEPT: Sync PDPTR fields if L2 guest in PAE paging mode
nEPT: Use minimal permission for nested p2m.
nEPT: handle invept instruction from L1 VMM
nEPT: expost EPT capablity to L1 VMM
nVMX: Expose VPID capability to nested VMM.

 xen/arch/x86/hvm/hvm.c                  |    7 +-
 xen/arch/x86/hvm/svm/nestedsvm.c        |   31 +++
 xen/arch/x86/hvm/svm/svm.c              |    3 +-
 xen/arch/x86/hvm/vmx/vmcs.c             |    2 +-
 xen/arch/x86/hvm/vmx/vmx.c              |   76 +++++---
 xen/arch/x86/hvm/vmx/vvmx.c             |  208 ++++++++++++++++++-
 xen/arch/x86/mm/guest_walk.c            |   12 +-
 xen/arch/x86/mm/hap/Makefile            |    1 +
 xen/arch/x86/mm/hap/nested_ept.c        |  345
+++++++++++++++++++++++++++++++
 xen/arch/x86/mm/hap/nested_hap.c        |   79 +++----
 xen/arch/x86/mm/mm-locks.h              |    2 +-
 xen/arch/x86/mm/p2m-ept.c               |   96 +++++++--
 xen/arch/x86/mm/p2m.c                   |   44 +++--
 xen/arch/x86/mm/shadow/multi.c          |    2 +-
 xen/include/asm-x86/guest_pt.h          |    8 +
 xen/include/asm-x86/hvm/hvm.h           |    9 +-
 xen/include/asm-x86/hvm/nestedhvm.h     |    1 +
 xen/include/asm-x86/hvm/svm/nestedsvm.h |    3 +
 xen/include/asm-x86/hvm/vmx/vmcs.h      |   31 ++-
 xen/include/asm-x86/hvm/vmx/vmx.h       |    6 +-
 xen/include/asm-x86/hvm/vmx/vvmx.h      |   29 +++-
 xen/include/asm-x86/p2m.h               |   17 +-
 22 files changed, 859 insertions(+), 153 deletions(-)  create mode 100644

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

#26 lore raw
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Xiantao

Hi, Guys
Do you have comments for this patchset ? Thanks!
Xiantao

I was actually hoping for Tim to take a look. But you should probably be a little more patient - it's been just two days since this got posted.

Jan

Quoted 55 lines
Quoted 55 lines

-----Original Message-----
From: Zhang, Xiantao
Sent: Tuesday, December 11, 2012 1:57 AM
To: [email protected]
Cc: [email protected]; [email protected]; Dong, Eddie; Nakajima, Jun; Zhang, Xiantao
Subject: [PATCH 00/11] Add virtual EPT support Xen.

From: Zhang Xiantao <[email protected]>

With virtual EPT support, L1 hyerpvisor can use EPT hardware for L2 guest's memory virtualization. In this way, L2 guest's performance can be improved sharply. According to our testing, some benchmarks can show > 5x performance gain.

Zhang Xiantao (11):
nestedhap: Change hostcr3 and p2m->cr3 to meaningful words
nestedhap: Change nested p2m's walker to vendor-specific
nested_ept: Implement guest ept's walker
nested_ept: Add permission check for success case
EPT: Make ept data structure or operations neutral
nEPT: Try to enable EPT paging for L2 guest.
nEPT: Sync PDPTR fields if L2 guest in PAE paging mode
nEPT: Use minimal permission for nested p2m.
nEPT: handle invept instruction from L1 VMM
nEPT: expost EPT capablity to L1 VMM
nVMX: Expose VPID capability to nested VMM.

 xen/arch/x86/hvm/hvm.c                  |    7 +-
 xen/arch/x86/hvm/svm/nestedsvm.c        |   31 +++
 xen/arch/x86/hvm/svm/svm.c              |    3 +-
 xen/arch/x86/hvm/vmx/vmcs.c             |    2 +-
 xen/arch/x86/hvm/vmx/vmx.c              |   76 +++++---
 xen/arch/x86/hvm/vmx/vvmx.c             |  208 ++++++++++++++++++-
 xen/arch/x86/mm/guest_walk.c            |   12 +-
 xen/arch/x86/mm/hap/Makefile            |    1 +
 xen/arch/x86/mm/hap/nested_ept.c        |  345
+++++++++++++++++++++++++++++++
 xen/arch/x86/mm/hap/nested_hap.c        |   79 +++----
 xen/arch/x86/mm/mm-locks.h              |    2 +-
 xen/arch/x86/mm/p2m-ept.c               |   96 +++++++--
 xen/arch/x86/mm/p2m.c                   |   44 +++--
 xen/arch/x86/mm/shadow/multi.c          |    2 +-
 xen/include/asm-x86/guest_pt.h          |    8 +
 xen/include/asm-x86/hvm/hvm.h           |    9 +-
 xen/include/asm-x86/hvm/nestedhvm.h     |    1 +
 xen/include/asm-x86/hvm/svm/nestedsvm.h |    3 +
 xen/include/asm-x86/hvm/vmx/vmcs.h      |   31 ++-
 xen/include/asm-x86/hvm/vmx/vmx.h       |    6 +-
 xen/include/asm-x86/hvm/vmx/vvmx.h      |   29 +++-
 xen/include/asm-x86/p2m.h               |   17 +-
 22 files changed, 859 insertions(+), 153 deletions(-)  create mode 100644

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