Skip to contentSkip to conversation

Nested VMX: update nested paging mode on vmexit

PATCH In review Yang Zhang 2 participants 5 messages Feb 12, 2014 – Feb 13, 2014

lore
Share & tools
lore

Yang Zhang

author
#0 lore raw

PATCH 1/2 Nested VMX: update nested paging mode on vmexit

From: Yang Zhang <[email protected]>

Since SVM and VMX use different mechanism to emulate the virtual-vmentry and virtual-vmexit, it's hard to update the nested paging mode correctly in common code. So we need to update the nested paging mode in their respective code path.
SVM already updates the nested paging mode on vmexit. This patch adds the same logic in VMX side.

Previous discussion is here:
http://lists.xen.org/archives/html/xen-devel/2013-12/msg01759.html

---
 xen/arch/x86/hvm/vmx/vmx.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +1 -0
index f6409d6..baf3040 100644
@@ -2541,6 +2541,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
     vcpu_nestedhvm(v).nv_vmswitch_in_progress = 0;
     if ( nestedhvm_vcpu_in_guestmode(v) )
     {
+        paging_update_nestedmode(v);
         if ( nvmx_n2_vmexit_handler(regs, exit_reason) )
             goto out;
     }
Signature · 1 line
1.7.1
author
#1 lore raw

PATCH 2/2 Nested EPT: fixing issue of translate L2 gva to L1 gfn

From: Yang Zhang <[email protected]>

There is no way to translate L2 gva to L1 gfn directly. To do it, we need to get L2's gfn first. Then look up the virtual EPT to get L1's gfn.

---
 xen/arch/x86/mm/p2m.c |   25 ++++++++++++++++++++-----
 1 files changed, 20 insertions(+), 5 deletions(-)
xen/arch/x86/mm/p2m.c +20 -5
index 8f380ed..e92cfbe 100644
@@ -1605,22 +1605,37 @@ unsigned long paging_gva_to_gfn(struct vcpu *v,
         && paging_mode_hap(v->domain) 
         && nestedhvm_is_n2(v) )
     {
-        unsigned long gfn;
+        unsigned long gfn, l1gfn, exit_qual;
         struct p2m_domain *p2m;
         const struct paging_mode *mode;
-        uint32_t pfec_21 = *pfec;
         uint64_t np2m_base = nhvm_vcpu_p2m_base(v);
+        unsigned int page_order, exit_reason;
+        int rc;
+        uint8_t p2m_acc;
+        struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
 
         /* 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);
 
+        if ( gfn == INVALID_GFN )
+            return 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);
-    }
+        rc = nept_translate_l2ga(v, gfn << 12 , &page_order, 4, &l1gfn, &p2m_acc,
+                                &exit_qual, &exit_reason);
+        if ( rc == EPT_TRANSLATE_VIOLATION || rc == EPT_TRANSLATE_MISCONFIG )
+        {
+            nvmx->ept.exit_reason = exit_reason;
+            nvmx->ept.exit_qual = exit_qual;
+            vcpu_nestedhvm(current).nv_vmexit_pending = 1;
+        }
+        if ( rc == EPT_TRANSLATE_RETRY )
+            *pfec = PFEC_page_paged;
 
+        return l1gfn;
+    }
     return hostmode->gva_to_gfn(v, hostp2m, va, pfec);
 }
 
Signature · 1 line
1.7.1
Reviewed-by on v1 1/2
#4 lore raw
In reply to Yang Zhang
Quoted 9 lines · Yang Zhang

From: Yang Zhang <[email protected]>

Since SVM and VMX use different mechanism to emulate the virtual-vmentry and virtual-vmexit, it's hard to update the nested paging mode correctly in common code. So we need to update the nested paging mode in their respective code path.
SVM already updates the nested paging mode on vmexit. This patch adds the same logic in VMX side.

Quoted 4 lines

Previous discussion is here:
http://lists.xen.org/archives/html/xen-devel/2013-12/msg01759.html

Quoted 17 lines
---
 xen/arch/x86/hvm/vmx/vmx.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +1 -0
index f6409d6..baf3040 100644
@@ -2541,6 +2541,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
     vcpu_nestedhvm(v).nv_vmswitch_in_progress = 0;
     if ( nestedhvm_vcpu_in_guestmode(v) )
     {
+        paging_update_nestedmode(v);
         if ( nvmx_n2_vmexit_handler(regs, exit_reason) )
             goto out;
     }
#2 lore raw

Re: [PATCH 2/2] Nested EPT: fixing issue of translate L2 gva to L1 gfn

In reply to Yang Zhang
Quoted 3 lines · Yang Zhang

From: Yang Zhang <[email protected]>

There is no way to translate L2 gva to L1 gfn directly.

Why?

Quoted 28 lines

To do it, we need to get L2's gfn first. Then look up the virtual EPT to get L1's gfn.

---
 xen/arch/x86/mm/p2m.c |   25 ++++++++++++++++++++-----
 1 files changed, 20 insertions(+), 5 deletions(-)
xen/arch/x86/mm/p2m.c +5 -2
index 8f380ed..e92cfbe 100644
@@ -1605,22 +1605,37 @@ unsigned long paging_gva_to_gfn(struct vcpu *v,
         && paging_mode_hap(v->domain) 
         && nestedhvm_is_n2(v) )
     {
-        unsigned long gfn;
+        unsigned long gfn, l1gfn, exit_qual;
         struct p2m_domain *p2m;
         const struct paging_mode *mode;
-        uint32_t pfec_21 = *pfec;
         uint64_t np2m_base = nhvm_vcpu_p2m_base(v);
+        unsigned int page_order, exit_reason;
+        int rc;
+        uint8_t p2m_acc;
+        struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
 
         /* translate l2 guest va into l2 guest gfn */
         p2m = p2m_get_nestedp2m(v, np2m_base);

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

Quoted 10 lines
xen/arch/x86/mm/p2m.c +3 -3
         mode = paging_get_nestedmode(v);
         gfn = mode->gva_to_gfn(v, p2m, va, pfec);
 
+        if ( gfn == INVALID_GFN )
+            return 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);
-    }

I think in p2m-ept.c you should override that function pointer to a EPT specific implementation.

Christoph

Quoted 17 lines
xen/arch/x86/mm/p2m.c +12 -0
+        rc = nept_translate_l2ga(v, gfn << 12 , &page_order, 4, &l1gfn, &p2m_acc,
+                                &exit_qual, &exit_reason);
+        if ( rc == EPT_TRANSLATE_VIOLATION || rc == EPT_TRANSLATE_MISCONFIG )
+        {
+            nvmx->ept.exit_reason = exit_reason;
+            nvmx->ept.exit_qual = exit_qual;
+            vcpu_nestedhvm(current).nv_vmexit_pending = 1;
+        }
+        if ( rc == EPT_TRANSLATE_RETRY )
+            *pfec = PFEC_page_paged;
 
+        return l1gfn;
+    }
     return hostmode->gva_to_gfn(v, hostp2m, va, pfec);
 }
 
#3 lore raw

Re: [PATCH 2/2] Nested EPT: fixing issue of translate L2 gva to L1 gfn

Egger, Christoph wrote on 2014-02-12:

Quoted 6 lines · Yang Zhang
Quoted 3 lines

From: Yang Zhang <[email protected]>

There is no way to translate L2 gva to L1 gfn directly.

Why?

I guess you mean p2m_ga_to_gfn() is able to do it.

Quoted 39 lines

xen/arch/x86/mm/p2m.c unsigned long paging_gva_to_gfn(struct vcpu

Quoted 13 lines

To do it, we need to get L2's gfn first. Then look up the virtual EPT to get L1's gfn.

---
 xen/arch/x86/mm/p2m.c |   25 ++++++++++++++++++++-----
 1 files changed, 20 insertions(+), 5 deletions(-)
xen/arch/x86/mm/p2m.c index renamed from xen/arch/x86/mm/p2m.c +0 -0

8f380ed..e92cfbe 100644

xen/arch/x86/mm/p2m.c +0 -0
@@ -1605,22 +1605,37 @@ unsigned long paging_gva_to_gfn(struct vcpu

*v,

Quoted 14 lines
xen/arch/x86/mm/p2m.c +5 -1
         && paging_mode_hap(v->domain)
         && nestedhvm_is_n2(v) )
     {
-        unsigned long gfn;
+        unsigned long gfn, l1gfn, exit_qual;
         struct p2m_domain *p2m; const struct paging_mode *mode; -     
           uint32_t pfec_21 = *pfec; uint64_t np2m_base =
         nhvm_vcpu_p2m_base(v);
+        unsigned int page_order, exit_reason;
+        int rc;
+        uint8_t p2m_acc;
+        struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
 
         /* translate l2 guest va into l2 guest gfn */

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

Quoted 10 lines
xen/arch/x86/mm/p2m.c +3 -1
         p2m = p2m_get_nestedp2m(v, np2m_base);
         mode = paging_get_nestedmode(v);
         gfn = mode->gva_to_gfn(v, p2m, va, pfec);
+        if ( gfn == INVALID_GFN )
+            return 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); -   
 }
Quoted 3 lines

I think in p2m-ept.c you should override that function pointer to a EPT specific implementation.

Right. I just noticed that p2m_ga_to_gfn() is designed to do this.

Quoted 14 lines

Christoph

Quoted 12 lines
xen/arch/x86/mm/p2m.c +1 -0
+        rc = nept_translate_l2ga(v, gfn << 12 , &page_order, 4, +
 &l1gfn, &p2m_acc, +                                &exit_qual,

&exit_reason); + if ( rc == EPT_TRANSLATE_VIOLATION || rc ==
EPT_TRANSLATE_MISCONFIG ) + { + nvmx->ept.exit_reason
= exit_reason; + nvmx->ept.exit_qual = exit_qual; +
vcpu_nestedhvm(current).nv_vmexit_pending = 1; + } + if
( rc == EPT_TRANSLATE_RETRY ) + *pfec = PFEC_page_paged;

xen/arch/x86/mm/p2m.c +2 -0
+        return l1gfn;
+    }
     return hostmode->gva_to_gfn(v, hostp2m, va, pfec);  }

Best regards,
Yang