Skip to contentSkip to conversation

xen/nested_p2m: Don't walk EPT tables with a regular PT walker

PATCH Superseded Andrew Cooper 5 participants 10 messages May 13, 2016 – May 18, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/nested_p2m: Don't walk EPT tables with a regular PT walker

hostmode->p2m_ga_to_gfn() is a plain PT walker, and is not appropriate for a general L1 p2m walk. It is fine for AMD as NPT share the same format as normal pagetables. For Intel EPT however, it is wrong.

The translation ends up correct (as the formats are sufficiently similar), but the control bits in lower 12 bits differ in meaning. A plain PT walker sets A/D bits (bits 5 and 6) as it walks, but in EPT tables, these are the IPAT and top bit of EMT (caching type). This in turn causes problem when the EPT tables are subsequently used.

Replace hostmode->p2m_ga_to_gfn() with nestedhap_walk_L1_p2m() in paging_gva_to_gfn(), which is the correct function for the task. This involves making nestedhap_walk_L1_p2m() non-static, and adding vmx_vmcs_enter/exit() pairs to nvmx_hap_walk_L1_p2m() as it is now reachable from contexts other than v == current.

---
CC: Jan Beulich <[email protected]>
CC: George Dunlap <[email protected]>
CC: Tim Deegan <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/arch/x86/hvm/vmx/vvmx.c         |  4 ++++
 xen/arch/x86/mm/hap/nested_hap.c    |  2 +-
 xen/arch/x86/mm/p2m.c               | 19 ++++++++++++++-----
 xen/include/asm-x86/hvm/nestedhvm.h |  4 ++++
 4 files changed, 23 insertions(+), 6 deletions(-)
xen/arch/x86/hvm/vmx/vvmx.c +4 -0
index 35f3687..faa8b69 100644
@@ -2036,6 +2036,8 @@ 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);
 
+    vmx_vmcs_enter(v);
+
     __vmread(EXIT_QUALIFICATION, &exit_qual);
     rc = nept_translate_l2ga(v, L2_gpa, page_order, rwx_rights, &gfn, p2m_acc,
                              &exit_qual, &exit_reason);
@@ -2060,6 +2062,8 @@ nvmx_hap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
         break;
     }
 
+    vmx_vmcs_exit(v);
+
     return rc;
 }
 
xen/arch/x86/mm/hap/nested_hap.c +1 -1
index 9cee5a0..d41bb09 100644
@@ -141,7 +141,7 @@ nestedhap_fix_p2m(struct vcpu *v, struct p2m_domain *p2m,
  * walk is successful, the translated value is returned in
  * L1_gpa. The result value tells what to do next.
  */
-static int
+int
 nestedhap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
                       unsigned int *page_order, uint8_t *p2m_acc,
                       bool_t access_r, bool_t access_w, bool_t access_x)
xen/arch/x86/mm/p2m.c +14 -5
index 94eabf6..de5791b 100644
@@ -2081,20 +2081,29 @@ unsigned long paging_gva_to_gfn(struct vcpu *v,
 
     if ( is_hvm_vcpu(v) && paging_mode_hap(v->domain) && nestedhvm_is_n2(v) )
     {
-        unsigned long gfn;
+        unsigned long l2_gfn, l1_gfn;
         struct p2m_domain *p2m;
         const struct paging_mode *mode;
-        uint32_t pfec_21 = *pfec;
         uint64_t np2m_base = nhvm_vcpu_p2m_base(v);
+        uint8_t l1_p2ma;
+        unsigned int l1_page_order;
+        int rv;
 
         /* translate l2 guest va into l2 guest gfn */
         p2m = p2m_get_nestedp2m(v, np2m_base);
         mode = paging_get_nestedmode(v);
-        gfn = mode->gva_to_gfn(v, p2m, va, pfec);
+        l2_gfn = mode->gva_to_gfn(v, p2m, va, pfec);
+
+        if ( l2_gfn == INVALID_GFN )
+            return INVALID_GFN;
 
         /* translate l2 guest gfn into l1 guest gfn */
-        return hostmode->p2m_ga_to_gfn(v, hostp2m, np2m_base,
-                                       gfn << PAGE_SHIFT, &pfec_21, NULL);
+        rv = nestedhap_walk_L1_p2m(v, l2_gfn, &l1_gfn, &l1_page_order, &l1_p2ma,
+                                   1,
+                                   !!(*pfec & PFEC_write_access),
+                                   !!(*pfec & PFEC_insn_fetch));
+
+        return rv == NESTEDHVM_PAGEFAULT_DONE ? l1_gfn : INVALID_GFN;
     }
 
     return hostmode->gva_to_gfn(v, hostp2m, va, pfec);
xen/include/asm-x86/hvm/nestedhvm.h +4 -0
index cf1a8f4..bc82425 100644
@@ -56,6 +56,10 @@ bool_t nestedhvm_vcpu_in_guestmode(struct vcpu *v);
 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);
 
+int nestedhap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
+                          unsigned int *page_order, uint8_t *p2m_acc,
+                          bool_t access_r, bool_t access_w, bool_t access_x);
+
 /* IO permission map */
 unsigned long *nestedhvm_vcpu_iomap_get(bool_t ioport_80, bool_t ioport_ed);
 
Signature · 7 lines
2.1.4


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

xen/arch/x86/mm/hap/nested_hap.c nestedhap_fix_p2m(struct vcpu *v, struct p2m_domain *p2m, · line 146

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 13.05.16 at 15:33, <[email protected]> wrote:
xen/arch/x86/mm/hap/nested_hap.c +1 -1
@@ -141,7 +141,7 @@ nestedhap_fix_p2m(struct vcpu *v, struct p2m_domain *p2m,
  * walk is successful, the translated value is returned in
  * L1_gpa. The result value tells what to do next.
  */
-static int
+int
 nestedhap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
                       unsigned int *page_order, uint8_t *p2m_acc,

The function becoming non-static widens the visibility of the bogus uint8_t here (should be p2m_access_t afaics). Granted this isn't an issue the patch introduces, but I would prefer this to be adjusted prior to dropping the static here...

xen/arch/x86/mm/p2m.c unsigned long paging_gva_to_gfn(struct vcpu *v, · line 2088

Quoted 13 lines
xen/arch/x86/mm/p2m.c +2 -2
@@ -2081,20 +2081,29 @@ unsigned long paging_gva_to_gfn(struct vcpu *v,
 
     if ( is_hvm_vcpu(v) && paging_mode_hap(v->domain) && nestedhvm_is_n2(v) )
     {
-        unsigned long gfn;
+        unsigned long l2_gfn, l1_gfn;
         struct p2m_domain *p2m;
         const struct paging_mode *mode;
-        uint32_t pfec_21 = *pfec;
         uint64_t np2m_base = nhvm_vcpu_p2m_base(v);
+        uint8_t l1_p2ma;

... avoiding proliferation of the quirkiness.

Quoted 12 lines
xen/arch/x86/mm/p2m.c +6 -1
+        unsigned int l1_page_order;
+        int rv;
 
         /* translate l2 guest va into l2 guest gfn */
         p2m = p2m_get_nestedp2m(v, np2m_base);
         mode = paging_get_nestedmode(v);
-        gfn = mode->gva_to_gfn(v, p2m, va, pfec);
+        l2_gfn = mode->gva_to_gfn(v, p2m, va, pfec);
+
+        if ( l2_gfn == INVALID_GFN )
+            return INVALID_GFN;
 

xen/arch/x86/mm/p2m.c unsigned long paging_gva_to_gfn(struct vcpu *v, · line 2107

Quoted 10 lines
xen/arch/x86/mm/p2m.c +6 -2
         /* translate l2 guest gfn into l1 guest gfn */
-        return hostmode->p2m_ga_to_gfn(v, hostp2m, np2m_base,
-                                       gfn << PAGE_SHIFT, &pfec_21, NULL);
+        rv = nestedhap_walk_L1_p2m(v, l2_gfn, &l1_gfn, &l1_page_order, &l1_p2ma,
+                                   1,
+                                   !!(*pfec & PFEC_write_access),
+                                   !!(*pfec & PFEC_insn_fetch));
+
+        return rv == NESTEDHVM_PAGEFAULT_DONE ? l1_gfn : INVALID_GFN;
     }

I'm really happy to see this getting cleaned up - I've stumbled across the apparent brokenness here more than once, without ever finding the time to help the situation. One question though: Is the return value here correct even for l1_page_order > 0?

Jan

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

#2 lore raw
Quoted 20 lines · Jan Beulich

xen/arch/x86/mm/hap/nested_hap.c nestedhap_fix_p2m(struct vcpu *v, struct p2m_domain *p2m, · line 146

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 13.05.16 at 15:33, <[email protected]> wrote:
xen/arch/x86/mm/hap/nested_hap.c +1 -1
@@ -141,7 +141,7 @@ nestedhap_fix_p2m(struct vcpu *v, struct p2m_domain *p2m,
  * walk is successful, the translated value is returned in
  * L1_gpa. The result value tells what to do next.
  */
-static int
+int
 nestedhap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
                       unsigned int *page_order, uint8_t *p2m_acc,

The function becoming non-static widens the visibility of the bogus uint8_t here (should be p2m_access_t afaics). Granted this isn't an issue the patch introduces, but I would prefer this to be adjusted prior to dropping the static here...

Quoted 4 lines
xen/arch/x86/mm/p2m.c +0 -0
@@ -2081,20 +2081,29 @@ unsigned long paging_gva_to_gfn(struct vcpu *v,
 
Quoted 10 lines

xen/arch/x86/mm/p2m.c unsigned long paging_gva_to_gfn(struct vcpu *v, · line 2088

Quoted 9 lines
xen/arch/x86/mm/p2m.c +2 -2
     if ( is_hvm_vcpu(v) && paging_mode_hap(v->domain) && nestedhvm_is_n2(v) )
     {
-        unsigned long gfn;
+        unsigned long l2_gfn, l1_gfn;
         struct p2m_domain *p2m;
         const struct paging_mode *mode;
-        uint32_t pfec_21 = *pfec;
         uint64_t np2m_base = nhvm_vcpu_p2m_base(v);
+        uint8_t l1_p2ma;

... avoiding proliferation of the quirkiness.

Will do.

Quoted 13 lines
Quoted 13 lines
xen/arch/x86/mm/p2m.c +6 -1
+        unsigned int l1_page_order;
+        int rv;
 
         /* translate l2 guest va into l2 guest gfn */
         p2m = p2m_get_nestedp2m(v, np2m_base);
         mode = paging_get_nestedmode(v);
-        gfn = mode->gva_to_gfn(v, p2m, va, pfec);
+        l2_gfn = mode->gva_to_gfn(v, p2m, va, pfec);
+
+        if ( l2_gfn == INVALID_GFN )
+            return INVALID_GFN;
 
Quoted 14 lines

xen/arch/x86/mm/p2m.c unsigned long paging_gva_to_gfn(struct vcpu *v, · line 2107

Quoted 10 lines
xen/arch/x86/mm/p2m.c +6 -2
         /* translate l2 guest gfn into l1 guest gfn */
-        return hostmode->p2m_ga_to_gfn(v, hostp2m, np2m_base,
-                                       gfn << PAGE_SHIFT, &pfec_21, NULL);
+        rv = nestedhap_walk_L1_p2m(v, l2_gfn, &l1_gfn, &l1_page_order, &l1_p2ma,
+                                   1,
+                                   !!(*pfec & PFEC_write_access),
+                                   !!(*pfec & PFEC_insn_fetch));
+
+        return rv == NESTEDHVM_PAGEFAULT_DONE ? l1_gfn : INVALID_GFN;
     }

I'm really happy to see this getting cleaned up - I've stumbled across the apparent brokenness here more than once, without ever finding the time to help the situation. One question though: Is the return value here correct even for l1_page_order > 0?

Not completely sure. This code is messy, and my current testing is "it now doesn't crash where it used to crash before".

The specific path being tripped over was __hvm_copy() performing an instruction fetch for emulation from the L2 guest.

Looking at the code, if your caller only cares about 4K mappings, you will get the correct translation, due to the adjustment nept_walk_tables() makes.

~Andrew

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

#3 lore raw
Quoted 33 lines
Quoted 1 line
Quoted 1 line · On 13.05.16 at 17:00, <[email protected]> wrote:
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 13.05.16 at 15:33, <[email protected]> wrote:
xen/arch/x86/mm/p2m.c +6 -1
+        unsigned int l1_page_order;
+        int rv;
 
         /* translate l2 guest va into l2 guest gfn */
         p2m = p2m_get_nestedp2m(v, np2m_base);
         mode = paging_get_nestedmode(v);
-        gfn = mode->gva_to_gfn(v, p2m, va, pfec);
+        l2_gfn = mode->gva_to_gfn(v, p2m, va, pfec);
+
+        if ( l2_gfn == INVALID_GFN )
+            return INVALID_GFN;
Quoted 14 lines

xen/arch/x86/mm/p2m.c unsigned long paging_gva_to_gfn(struct vcpu *v, · line 2107

Quoted 10 lines
         /* translate l2 guest gfn into l1 guest gfn */
xen/arch/x86/mm/p2m.c +6 -2
-        return hostmode->p2m_ga_to_gfn(v, hostp2m, np2m_base,
-                                       gfn << PAGE_SHIFT, &pfec_21, NULL);
+        rv = nestedhap_walk_L1_p2m(v, l2_gfn, &l1_gfn, &l1_page_order, &l1_p2ma,
+                                   1,
+                                   !!(*pfec & PFEC_write_access),
+                                   !!(*pfec & PFEC_insn_fetch));
+
+        return rv == NESTEDHVM_PAGEFAULT_DONE ? l1_gfn : INVALID_GFN;
     }

I'm really happy to see this getting cleaned up - I've stumbled across the apparent brokenness here more than once, without ever finding the time to help the situation. One question though: Is the return value here correct even for l1_page_order > 0?

Not completely sure. This code is messy, and my current testing is "it now doesn't crash where it used to crash before".

Quoted 6 lines

The specific path being tripped over was __hvm_copy() performing an instruction fetch for emulation from the L2 guest.

Looking at the code, if your caller only cares about 4K mappings, you will get the correct translation, due to the adjustment nept_walk_tables() makes.

Ah, indeed. But very odd for it to be done there, the more that "all callers" is exactly one afaics. In that case may I suggest adding an assertion to check that the respective bits in L1 and L2 GFN match?

Jan

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

#4 lore raw

PATCH v2 1/1 xen/nested_p2m: Don't walk EPT tables with a regular PT walker

hostmode->p2m_ga_to_gfn() is a plain PT walker, and is not appropriate for a general L1 p2m walk. It is fine for AMD as NPT share the same format as normal pagetables. For Intel EPT however, it is wrong.

The translation ends up correct (as the formats are sufficiently similar), but the control bits in lower 12 bits differ in meaning. A plain PT walker sets A/D bits (bits 5 and 6) as it walks, but in EPT tables, these are the IPAT and top bit of EMT (caching type). This in turn causes problem when the EPT tables are subsequently used.

Replace hostmode->p2m_ga_to_gfn() with nestedhap_walk_L1_p2m() in paging_gva_to_gfn(), which is the correct function for the task. This involves making nestedhap_walk_L1_p2m() non-static, and adding vmx_vmcs_enter/exit() pairs to nvmx_hap_walk_L1_p2m() as it is now reachable from contexts other than v == current.

---
CC: Jan Beulich <[email protected]>
CC: George Dunlap <[email protected]>
CC: Tim Deegan <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Wei Liu <[email protected]>

v2:
* ASSERT() that the translation doesn't go wrong with superpages

---
 xen/arch/x86/hvm/vmx/vvmx.c         |  4 ++++
 xen/arch/x86/mm/hap/nested_hap.c    |  2 +-
 xen/arch/x86/mm/p2m.c               | 29 ++++++++++++++++++++++++-----
 xen/include/asm-x86/hvm/nestedhvm.h |  4 ++++
 4 files changed, 33 insertions(+), 6 deletions(-)
xen/arch/x86/hvm/vmx/vvmx.c +4 -0
index 35f3687..faa8b69 100644
@@ -2036,6 +2036,8 @@ 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);
 
+    vmx_vmcs_enter(v);
+
     __vmread(EXIT_QUALIFICATION, &exit_qual);
     rc = nept_translate_l2ga(v, L2_gpa, page_order, rwx_rights, &gfn, p2m_acc,
                              &exit_qual, &exit_reason);
@@ -2060,6 +2062,8 @@ nvmx_hap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
         break;
     }
 
+    vmx_vmcs_exit(v);
+
     return rc;
 }
 
xen/arch/x86/mm/hap/nested_hap.c +1 -1
index 9cee5a0..d41bb09 100644
@@ -141,7 +141,7 @@ nestedhap_fix_p2m(struct vcpu *v, struct p2m_domain *p2m,
  * walk is successful, the translated value is returned in
  * L1_gpa. The result value tells what to do next.
  */
-static int
+int
 nestedhap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
                       unsigned int *page_order, uint8_t *p2m_acc,
                       bool_t access_r, bool_t access_w, bool_t access_x)
xen/arch/x86/mm/p2m.c +24 -5
index 94eabf6..9b19769 100644
@@ -2081,20 +2081,39 @@ unsigned long paging_gva_to_gfn(struct vcpu *v,
 
     if ( is_hvm_vcpu(v) && paging_mode_hap(v->domain) && nestedhvm_is_n2(v) )
     {
-        unsigned long gfn;
+        unsigned long l2_gfn, l1_gfn;
         struct p2m_domain *p2m;
         const struct paging_mode *mode;
-        uint32_t pfec_21 = *pfec;
         uint64_t np2m_base = nhvm_vcpu_p2m_base(v);
+        uint8_t l1_p2ma;
+        unsigned int l1_page_order;
+        int rv;
 
         /* translate l2 guest va into l2 guest gfn */
         p2m = p2m_get_nestedp2m(v, np2m_base);
         mode = paging_get_nestedmode(v);
-        gfn = mode->gva_to_gfn(v, p2m, va, pfec);
+        l2_gfn = mode->gva_to_gfn(v, p2m, va, pfec);
+
+        if ( l2_gfn == INVALID_GFN )
+            return INVALID_GFN;
 
         /* translate l2 guest gfn into l1 guest gfn */
-        return hostmode->p2m_ga_to_gfn(v, hostp2m, np2m_base,
-                                       gfn << PAGE_SHIFT, &pfec_21, NULL);
+        rv = nestedhap_walk_L1_p2m(v, l2_gfn, &l1_gfn, &l1_page_order, &l1_p2ma,
+                                   1,
+                                   !!(*pfec & PFEC_write_access),
+                                   !!(*pfec & PFEC_insn_fetch));
+
+        if ( rv != NESTEDHVM_PAGEFAULT_DONE )
+            return INVALID_GFN;
+
+        /*
+         * Sanity check that l1_gfn can be used properly as a 4K mapping, even
+         * if it mapped by a nested superpage.
+         */
+        ASSERT((l2_gfn & ((1ul << l1_page_order) - 1)) ==
+               (l1_gfn & ((1ul << l1_page_order) - 1)));
+
+        return l1_gfn;
     }
 
     return hostmode->gva_to_gfn(v, hostp2m, va, pfec);
xen/include/asm-x86/hvm/nestedhvm.h +4 -0
index cf1a8f4..bc82425 100644
@@ -56,6 +56,10 @@ bool_t nestedhvm_vcpu_in_guestmode(struct vcpu *v);
 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);
 
+int nestedhap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
+                          unsigned int *page_order, uint8_t *p2m_acc,
+                          bool_t access_r, bool_t access_w, bool_t access_x);
+
 /* IO permission map */
 unsigned long *nestedhvm_vcpu_iomap_get(bool_t ioport_80, bool_t ioport_ed);
 
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Reviewed-by on v2 1/1
#5 lore raw
Quoted 13 lines

From: Andrew Cooper [mailto:[email protected]]
Sent: Saturday, May 14, 2016 1:26 AM

hostmode->p2m_ga_to_gfn() is a plain PT walker, and is not appropriate for a general L1 p2m walk. It is fine for AMD as NPT share the same format as normal pagetables. For Intel EPT however, it is wrong.

The translation ends up correct (as the formats are sufficiently similar), but the control bits in lower 12 bits differ in meaning. A plain PT walker sets A/D bits (bits 5 and 6) as it walks, but in EPT tables, these are the IPAT and top bit of EMT (caching type). This in turn causes problem when the EPT tables are subsequently used.

Quoted 7 lines

Replace hostmode->p2m_ga_to_gfn() with nestedhap_walk_L1_p2m() in paging_gva_to_gfn(), which is the correct function for the task. This involves making nestedhap_walk_L1_p2m() non-static, and adding vmx_vmcs_enter/exit() pairs to nvmx_hap_walk_L1_p2m() as it is now reachable from contexts other than v == current.

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

Acked-by on v2 1/1
#6 lore raw
In reply to Andrew Cooper
Quoted 10 lines · Andrew Cooper

hostmode->p2m_ga_to_gfn() is a plain PT walker, and is not appropriate for a general L1 p2m walk. It is fine for AMD as NPT share the same format as normal pagetables. For Intel EPT however, it is wrong.

The translation ends up correct (as the formats are sufficiently similar), but the control bits in lower 12 bits differ in meaning. A plain PT walker sets A/D bits (bits 5 and 6) as it walks, but in EPT tables, these are the IPAT and top bit of EMT (caching type). This in turn causes problem when the EPT tables are subsequently used.

Quoted 7 lines

Replace hostmode->p2m_ga_to_gfn() with nestedhap_walk_L1_p2m() in paging_gva_to_gfn(), which is the correct function for the task. This involves making nestedhap_walk_L1_p2m() non-static, and adding vmx_vmcs_enter/exit() pairs to nvmx_hap_walk_L1_p2m() as it is now reachable from contexts other than v == current.

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

#7 lore raw
In reply to Andrew Cooper
Quoted 10 lines · Andrew Cooper

hostmode->p2m_ga_to_gfn() is a plain PT walker, and is not appropriate for a general L1 p2m walk. It is fine for AMD as NPT share the same format as normal pagetables. For Intel EPT however, it is wrong.

The translation ends up correct (as the formats are sufficiently similar), but the control bits in lower 12 bits differ in meaning. A plain PT walker sets A/D bits (bits 5 and 6) as it walks, but in EPT tables, these are the IPAT and top bit of EMT (caching type). This in turn causes problem when the EPT tables are subsequently used.

Quoted 7 lines

Replace hostmode->p2m_ga_to_gfn() with nestedhap_walk_L1_p2m() in paging_gva_to_gfn(), which is the correct function for the task. This involves making nestedhap_walk_L1_p2m() non-static, and adding vmx_vmcs_enter/exit() pairs to nvmx_hap_walk_L1_p2m() as it is now reachable from contexts other than v == current.

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

#8 lore raw
In reply to Andrew Cooper
Quoted 31 lines · Andrew Cooper
Quoted 20 lines · Jan Beulich

xen/arch/x86/mm/hap/nested_hap.c nestedhap_fix_p2m(struct vcpu *v, struct p2m_domain *p2m, · line 146

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 13.05.16 at 15:33, <[email protected]> wrote:
xen/arch/x86/mm/hap/nested_hap.c +1 -1
@@ -141,7 +141,7 @@ nestedhap_fix_p2m(struct vcpu *v, struct p2m_domain *p2m,
  * walk is successful, the translated value is returned in
  * L1_gpa. The result value tells what to do next.
  */
-static int
+int
 nestedhap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
                       unsigned int *page_order, uint8_t *p2m_acc,

The function becoming non-static widens the visibility of the bogus uint8_t here (should be p2m_access_t afaics). Granted this isn't an issue the patch introduces, but I would prefer this to be adjusted prior to dropping the static here...

Quoted 4 lines
xen/arch/x86/mm/p2m.c +0 -0
@@ -2081,20 +2081,29 @@ unsigned long paging_gva_to_gfn(struct vcpu *v,
 
Quoted 10 lines

xen/arch/x86/mm/p2m.c unsigned long paging_gva_to_gfn(struct vcpu *v, · line 2088

Quoted 9 lines
xen/arch/x86/mm/p2m.c +2 -2
     if ( is_hvm_vcpu(v) && paging_mode_hap(v->domain) && nestedhvm_is_n2(v) )
     {
-        unsigned long gfn;
+        unsigned long l2_gfn, l1_gfn;
         struct p2m_domain *p2m;
         const struct paging_mode *mode;
-        uint32_t pfec_21 = *pfec;
         uint64_t np2m_base = nhvm_vcpu_p2m_base(v);
+        uint8_t l1_p2ma;

... avoiding proliferation of the quirkiness.

Quoted 1 line

Will do.

It turns out that this is currently hard, due to a tangle of header files.

I will put this on my TODO list, but this point, I have more important things to do for 4.7.

~Andrew

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

#9 lore raw
Quoted 34 lines
Quoted 1 line
Quoted 1 line · On 13.05.16 at 17:24, <[email protected]> wrote:
Quoted 5 lines · Andrew Cooper
Quoted 4 lines · Jan Beulich

xen/arch/x86/mm/hap/nested_hap.c nestedhap_fix_p2m(struct vcpu *v, struct p2m_domain

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 13.05.16 at 15:33, <[email protected]> wrote:
xen/arch/x86/mm/hap/nested_hap.c +0 -0
@@ -141,7 +141,7 @@ nestedhap_fix_p2m(struct vcpu *v, struct p2m_domain 

*p2m,

Quoted 26 lines
Quoted 26 lines

xen/arch/x86/mm/hap/nested_hap.c nestedhap_fix_p2m(struct vcpu *v, struct p2m_domain *p2m, · line 146

Quoted 7 lines
xen/arch/x86/mm/hap/nested_hap.c +1 -1
  * walk is successful, the translated value is returned in
  * L1_gpa. The result value tells what to do next.
  */
-static int
+int
 nestedhap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,
                       unsigned int *page_order, uint8_t *p2m_acc,

The function becoming non-static widens the visibility of the bogus uint8_t here (should be p2m_access_t afaics). Granted this isn't an issue the patch introduces, but I would prefer this to be adjusted prior to dropping the static here...

xen/arch/x86/mm/p2m.c unsigned long paging_gva_to_gfn(struct vcpu *v, · line 2088

Quoted 13 lines
xen/arch/x86/mm/p2m.c +2 -2
@@ -2081,20 +2081,29 @@ unsigned long paging_gva_to_gfn(struct vcpu *v,
 
     if ( is_hvm_vcpu(v) && paging_mode_hap(v->domain) && nestedhvm_is_n2(v) )
     {
-        unsigned long gfn;
+        unsigned long l2_gfn, l1_gfn;
         struct p2m_domain *p2m;
         const struct paging_mode *mode;
-        uint32_t pfec_21 = *pfec;
         uint64_t np2m_base = nhvm_vcpu_p2m_base(v);
+        uint8_t l1_p2ma;

... avoiding proliferation of the quirkiness.

Quoted 3 lines
Quoted 1 line

Will do.

It turns out that this is currently hard, due to a tangle of header files.

And I withdraw the request then.

Quoted 2 lines

I will put this on my TODO list, but this point, I have more important things to do for 4.7.

Thanks, understood.

Jan

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