Skip to contentSkip to conversation

VMX: Eliminate cr3 store/load vmexit when UG enabled

PATCH In review Yang Zhang 4 participants 9 messages Oct 23, 2013 – Oct 29, 2013

lore
Share & tools
lore

Yang Zhang

author
#0 lore raw

PATCH 1/1 VMX: Eliminate cr3 store/load vmexit when UG enabled

From: Yang Zhang <[email protected]>

With the feature of unrestricted guest, there should no vmexit be triggered when guest accesses the cr3 in non-paging mode.

---
 xen/arch/x86/hvm/vmx/vmx.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +3 -3
index 9ca8632..b9b5e74 100644
@@ -1086,7 +1086,7 @@ static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr)
             uint32_t cr3_ctls = (CPU_BASED_CR3_LOAD_EXITING |
                                  CPU_BASED_CR3_STORE_EXITING);
             v->arch.hvm_vmx.exec_control &= ~cr3_ctls;
-            if ( !hvm_paging_enabled(v) )
+            if ( !hvm_paging_enabled(v) && !vmx_unrestricted_guest(v) )
                 v->arch.hvm_vmx.exec_control |= cr3_ctls;
 
             /* Trap CR3 updates if CR3 memory events are enabled. */
@@ -1156,7 +1156,7 @@ static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr)
     case 3:
         if ( paging_mode_hap(v->domain) )
         {
-            if ( !hvm_paging_enabled(v) )
+            if ( !hvm_paging_enabled(v) && !vmx_unrestricted_guest(v) )
                 v->arch.hvm_vcpu.hw_cr[3] =
                     v->domain->arch.hvm_domain.params[HVM_PARAM_IDENT_PT];
             vmx_load_pdptrs(v);
@@ -2408,7 +2408,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
 
     hvm_invalidate_regs_fields(regs);
 
-    if ( paging_mode_hap(v->domain) && hvm_paging_enabled(v) )
+    if ( paging_mode_hap(v->domain) )
     {
         __vmread(GUEST_CR3, &v->arch.hvm_vcpu.hw_cr[3]);
         v->arch.hvm_vcpu.guest_cr[3] = v->arch.hvm_vcpu.hw_cr[3];
Signature · 1 line
1.7.1
#1 lore raw
Quoted 6 lines · Yang Zhang

From: Yang Zhang <[email protected]>

With the feature of unrestricted guest, there should no vmexit be triggered when guest accesses the cr3 in non-paging mode.

You English here confused me for a bit. I presume you mean "Xen should not cause vmexits for cr3 accesses in unrestricted guests", whereas the current meaning implies that hardware wont generate a vmexit for cr3 accesses for unrestricted guests (which is not correct according to the SDM).

Quoted 9 lines
---
 xen/arch/x86/hvm/vmx/vmx.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +0 -0
index 9ca8632..b9b5e74 100644

xen/arch/x86/hvm/vmx/vmx.c static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr) · line 1091

Quoted 7 lines
xen/arch/x86/hvm/vmx/vmx.c +1 -1
@@ -1086,7 +1086,7 @@ static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr)
             uint32_t cr3_ctls = (CPU_BASED_CR3_LOAD_EXITING |
                                  CPU_BASED_CR3_STORE_EXITING);
             v->arch.hvm_vmx.exec_control &= ~cr3_ctls;
-            if ( !hvm_paging_enabled(v) )
+            if ( !hvm_paging_enabled(v) && !vmx_unrestricted_guest(v) )
 
 From my (brief, so please correct me if I am wrong) reading of the spec,

guest_CR0.PG is enforced if SECONDARY_EXEC_UNRESTRICTED_GUEST is clear. Therefore, !hvm_paging_enable() implies SECONDARY_EXEC_UNRESTRICTED_GUEST is set (or we will incur a vmentry failure)

xen/arch/x86/hvm/vmx/vmx.c static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr) · line 1162

Quoted 12 lines
xen/arch/x86/hvm/vmx/vmx.c +0 -0
                 v->arch.hvm_vmx.exec_control |= cr3_ctls;
 
             /* Trap CR3 updates if CR3 memory events are enabled. */
xen/arch/x86/hvm/vmx/vmx.c +1 -1
@@ -1156,7 +1156,7 @@ static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr)
     case 3:
         if ( paging_mode_hap(v->domain) )
         {
-            if ( !hvm_paging_enabled(v) )
+            if ( !hvm_paging_enabled(v) && !vmx_unrestricted_guest(v) )
                 v->arch.hvm_vcpu.hw_cr[3] =
                     v->domain->arch.hvm_domain.params[HVM_PARAM_IDENT_PT];
             vmx_load_pdptrs(v);

Why does unrestricted mode affect which pagetables we use when moving back into Xen?

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 2414

Quoted 9 lines
xen/arch/x86/hvm/vmx/vmx.c +1 -1
@@ -2408,7 +2408,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
 
     hvm_invalidate_regs_fields(regs);
 
-    if ( paging_mode_hap(v->domain) && hvm_paging_enabled(v) )
+    if ( paging_mode_hap(v->domain) )
     {
         __vmread(GUEST_CR3, &v->arch.hvm_vcpu.hw_cr[3]);
         v->arch.hvm_vcpu.guest_cr[3] = v->arch.hvm_vcpu.hw_cr[3];

~Andrew

#2 lore raw

Andrew Cooper wrote on 2013-10-23:

Quoted 6 lines · Yang Zhang
Quoted 5 lines

From: Yang Zhang <[email protected]>

With the feature of unrestricted guest, there should no vmexit be triggered when guest accesses the cr3 in non-paging mode.

Quoted 7 lines
Quoted 1 line

You English here confused me for a bit. I presume you mean "Xen
should not cause vmexits for cr3 accesses in unrestricted guests",
whereas the current meaning implies that hardware wont generate a
vmexit for cr3 accesses for unrestricted guests (which is not correct according to the SDM).

Apology for my poor English. Yes, your understanding is right.

Quoted 18 lines

xen/arch/x86/hvm/vmx/vmx.c static void vmx_update_guest_cr(struct vcpu · line 1086

Quoted 9 lines
---
 xen/arch/x86/hvm/vmx/vmx.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +0 -0
index 9ca8632..b9b5e74 100644
@@ -1086,7 +1086,7 @@ static void vmx_update_guest_cr(struct vcpu
 *v,

unsigned int cr)

xen/arch/x86/hvm/vmx/vmx.c static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr) · line 1086

Quoted 1 line
xen/arch/x86/hvm/vmx/vmx.c +0 -0
             uint32_t cr3_ctls = (CPU_BASED_CR3_LOAD_EXITING |

CPU_BASED_CR3_STORE_EXITING);

xen/arch/x86/hvm/vmx/vmx.c static void vmx_update_guest_cr(struct vcpu · line 1091

Quoted 5 lines
xen/arch/x86/hvm/vmx/vmx.c +3 -1
             v->arch.hvm_vmx.exec_control &= ~cr3_ctls;
-            if ( !hvm_paging_enabled(v) )
+            if ( !hvm_paging_enabled(v) &&
+ !vmx_unrestricted_guest(v)
+ )
Quoted 6 lines

From my (brief, so please correct me if I am wrong) reading of the spec, guest_CR0.PG is enforced if SECONDARY_EXEC_UNRESTRICTED_GUEST is clear. Therefore, !hvm_paging_enable() implies SECONDARY_EXEC_UNRESTRICTED_GUEST is set (or we will incur a vmentry failure)

GUEST_CR0 from SPEC means hw_cr in Xen not guest_cr. hvm_paging_enable() checks the paging status that as guest saw them not the real status in hardware.

Quoted 13 lines
Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +0 -0
                 v->arch.hvm_vmx.exec_control |= cr3_ctls;
             /* Trap CR3 updates if CR3 memory events are enabled.

*/ @@ -1156,7 +1156,7 @@ static void vmx_update_guest_cr(struct vcpu *v,

unsigned int cr)

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

Quoted 8 lines
     case 3:
         if ( paging_mode_hap(v->domain) )
         {
xen/arch/x86/hvm/vmx/vmx.c +3 -1
-            if ( !hvm_paging_enabled(v) )
+            if ( !hvm_paging_enabled(v) &&
+ !vmx_unrestricted_guest(v)
+ )
                 v->arch.hvm_vcpu.hw_cr[3] =
Quoted 5 lines

v->domain->arch.hvm_domain.params[HVM_PARAM_IDENT_PT];

Quoted 1 line
             vmx_load_pdptrs(v);

Why does unrestricted mode affect which pagetables we use when moving back into Xen?

With unrestricted guest, the CR3 of hardware is owned by guest itself. Hypervisor should not touch the CR3. I don't why current logic tries to modify the CR3 as long as guest in non-paging mode which totally ignore the UG.
Normally, it will work well since we trap all guests' modifications to CR3 and CR0.PG. But in nested case, we saw an issue that L2 running in non-paging mode with UG enabled, and L1 set CR3 for L2 to run. With currently logic, L0 will rewrite the CR3 with its value and cause the problem.

Quoted 4 lines
Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +0 -0
@@ -2408,7 +2408,7 @@ void vmx_vmexit_handler(struct cpu_user_regs
 *regs)
 
Quoted 8 lines

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 2414

Quoted 6 lines
xen/arch/x86/hvm/vmx/vmx.c +1 -1
     hvm_invalidate_regs_fields(regs);
-    if ( paging_mode_hap(v->domain) && hvm_paging_enabled(v) )
+    if ( paging_mode_hap(v->domain) )
     {
         __vmread(GUEST_CR3, &v->arch.hvm_vcpu.hw_cr[3]);
         v->arch.hvm_vcpu.guest_cr[3] = v->arch.hvm_vcpu.hw_cr[3];

~Andrew

Best regards,
Yang

#3 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line · Yang Z

Andrew Cooper wrote on 2013-10-23:

Quoted 8 lines · Yang Zhang
Quoted 6 lines

From: Yang Zhang <[email protected]>

With the feature of unrestricted guest, there should no vmexit be triggered when guest accesses the cr3 in non-paging mode.

Quoted 8 lines
Quoted 4 lines

You English here confused me for a bit. I presume you mean "Xen should not cause vmexits for cr3 accesses in unrestricted guests", whereas the current meaning implies that hardware wont generate a vmexit for cr3 accesses for unrestricted guests (which is not correct

according to the SDM).

Quoted 1 line

Apology for my poor English. Yes, your understanding is right.

So assuming we'll get an ack from one of the VMX maintainers, should this then be committed with the suggested change to the description? Also, Andrew, any more concerns regarding this change (IOW did Yang address your earlier questions)?

Jan

#4 lore raw
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · Yang Z

Andrew Cooper wrote on 2013-10-23:

Quoted 11 lines · Yang Zhang
Quoted 6 lines

From: Yang Zhang <[email protected]>

With the feature of unrestricted guest, there should no vmexit be triggered when guest accesses the cr3 in non-paging mode.

You English here confused me for a bit. I presume you mean "Xen should not cause vmexits for cr3 accesses in unrestricted guests", whereas the current meaning implies that hardware wont generate a vmexit for cr3 accesses for unrestricted guests (which is not correct

Quoted 9 lines
Quoted 2 lines

according to the SDM).
Apology for my poor English. Yes, your understanding is right.

So assuming we'll get an ack from one of the VMX maintainers, should this then be committed with the suggested change to the description? Also, Andrew, any more concerns regarding this change (IOW did Yang address your earlier questions)?

Jan

I think so, but I really don't think I know the implications of the changes well enough to be happy giving it a Reviewed-by tag.

Given the clarification regarding the commit message, I shall defer to the maintainers for the correctness of the change.

~Andrew

#5 lore raw

Andrew Cooper wrote on 2013-10-28:

Quoted 22 lines · Jan Beulich
Quoted 21 lines
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Yang Z

Andrew Cooper wrote on 2013-10-23:

Quoted 6 lines · Yang Zhang
Quoted 5 lines

From: Yang Zhang <[email protected]>

With the feature of unrestricted guest, there should no vmexit be triggered when guest accesses the cr3 in non-paging mode.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

You English here confused me for a bit. I presume you mean "Xen should not cause vmexits for cr3 accesses in unrestricted guests", whereas the current meaning implies that hardware wont generate a vmexit for cr3 accesses for unrestricted guests (which is not correct

according to the SDM).
Apology for my poor English. Yes, your understanding is right.

So assuming we'll get an ack from one of the VMX maintainers, should this then be committed with the suggested change to the description? Also, Andrew, any more concerns regarding this change (IOW did Yang address your earlier questions)?

Quoted 5 lines
Quoted 2 lines

Jan

I think so, but I really don't think I know the implications of the changes well enough to be happy giving it a Reviewed-by tag.

I will ask our QA to do a full testing against this patch to see whether it introduces any regression.

Quoted 5 lines

Given the clarification regarding the commit message, I shall defer to the maintainers for the correctness of the change.

~Andrew

Best regards,
Yang

Acked-by
#6 lore raw
In reply to Andrew Cooper
Quoted 24 lines · Andrew Cooper
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · Yang Z

Andrew Cooper wrote on 2013-10-23:

Quoted 11 lines · Yang Zhang
Quoted 6 lines

From: Yang Zhang <[email protected]>

With the feature of unrestricted guest, there should no vmexit be triggered when guest accesses the cr3 in non-paging mode.

You English here confused me for a bit. I presume you mean "Xen should not cause vmexits for cr3 accesses in unrestricted guests", whereas the current meaning implies that hardware wont generate a vmexit for cr3 accesses for unrestricted guests (which is not correct

Quoted 9 lines
Quoted 2 lines

according to the SDM).
Apology for my poor English. Yes, your understanding is right.

So assuming we'll get an ack from one of the VMX maintainers, should this then be committed with the suggested change to the description? Also, Andrew, any more concerns regarding this change (IOW did Yang address your earlier questions)?

Jan

Quoted 7 lines

I think so, but I really don't think I know the implications of the changes well enough to be happy giving it a Reviewed-by tag.

Given the clarification regarding the commit message, I shall defer to the maintainers for the correctness of the change.

~Andrew

The patch seems doing the right thing.

Signature · 2 lines
Jun
Intel Open Source Technology Center
#7 lore raw
In reply to Jan Beulich

Jan Beulich wrote on 2013-10-28:

Quoted 17 lines
Quoted 17 lines
Quoted 1 line
Quoted 1 line · Yang Z

Andrew Cooper wrote on 2013-10-23:

Quoted 14 lines · Yang Zhang
Quoted 6 lines

From: Yang Zhang <[email protected]>

With the feature of unrestricted guest, there should no vmexit be triggered when guest accesses the cr3 in non-paging mode.

You English here confused me for a bit. I presume you mean "Xen should not cause vmexits for cr3 accesses in unrestricted guests", whereas the current meaning implies that hardware wont generate a vmexit for cr3 accesses for unrestricted guests (which is not correct according to the SDM).

Quoted 4 lines
Quoted 1 line

Apology for my poor English. Yes, your understanding is right.

So assuming we'll get an ack from one of the VMX maintainers, should this then be committed with the suggested change to the description?

Sure. If no need to revise the current patch, please help to commit it with the comments Paolo suggested.

Quoted 4 lines

Also, Andrew, any more concerns regarding this change (IOW did Yang address your earlier questions)?

Jan

Best regards,
Yang

#8 lore raw
Quoted 19 lines
Quoted 1 line
Quoted 1 line · Yang Z

Jan Beulich wrote on 2013-10-28:

Quoted 17 lines
Quoted 17 lines
Quoted 1 line
Quoted 1 line · Yang Z

Andrew Cooper wrote on 2013-10-23:

Quoted 14 lines · Yang Zhang
Quoted 6 lines

From: Yang Zhang <[email protected]>

With the feature of unrestricted guest, there should no vmexit be triggered when guest accesses the cr3 in non-paging mode.

You English here confused me for a bit. I presume you mean "Xen should not cause vmexits for cr3 accesses in unrestricted guests", whereas the current meaning implies that hardware wont generate a vmexit for cr3 accesses for unrestricted guests (which is not correct according to the SDM).

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Apology for my poor English. Yes, your understanding is right.

So assuming we'll get an ack from one of the VMX maintainers, should this then be committed with the suggested change to the description?

Sure. If no need to revise the current patch, please help to commit it with the comments Paolo suggested.

Andrew you mean?

Jan