vvmx: fixes after CR4 trapping optimizations
PATCH 1/1 vvmx: fixes after CR4 trapping optimizations
Commit 406817 doesn't update nested VMX code in order to take into account L1 CR4 host mask when nested guest (L2) writes to CR4, and thus the mask written to CR4_GUEST_HOST_MASK is likely not as restrictive as it should be.
Also the VVMCS GUEST_CR4 value should be updated to match the underlying value when syncing the VVMCS state.
---
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
---
I've manually tested and AFAICT this fixes the osstest failure detected in 120076 ("test-amd64-amd64-qemuu-nested-intel").
--- xen/arch/x86/hvm/vmx/vmx.c | 4 ++++ xen/arch/x86/hvm/vmx/vvmx.c | 13 ++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/vmx/vmx.c +4 -0
@@ -1617,6 +1617,10 @@ static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr, v->arch.hvm_vmx.cr4_host_mask |= ~v->domain->arch.monitor.write_ctrlreg_mask[VM_EVENT_X86_CR4 ]; +if (nestedhvm_vcpu_in_guestmode (v) ) +/* Add the nested host mask to get the more restrictive one. */ + v->arch.hvm_vmx.cr4_host_mask |=get_vvmcs (v, +CR4_GUEST_HOST_MASK ); }__vmwrite (CR4_GUEST_HOST_MASK , v->arch.hvm_vmx.cr4_host_mask);
xen/arch/x86/hvm/vmx/vvmx.c +12 -1
@@ -1101,7 +1101,8 @@ static void load_shadow_guest_state(struct vcpu *v) (get_vvmcs (v,CR4_READ_SHADOW ) & cr_gh_mask);__vmwrite (CR4_READ_SHADOW , cr_read_shadow);/* Add the nested host mask to the one set by vmx_update_guest_cr. */ -__vmwrite (CR4_GUEST_HOST_MASK , cr_gh_mask | v->arch.hvm_vmx.cr4_host_mask); + v->arch.hvm_vmx.cr4_host_mask |= cr_gh_mask; +__vmwrite (CR4_GUEST_HOST_MASK , v->arch.hvm_vmx.cr4_host_mask);/* TODO: CR3 target control */ }
@@ -1232,6 +1233,16 @@ static void sync_vvmcs_guest_state(struct vcpu *v, struct cpu_user_regs *regs)/* CR3 sync if exec doesn't want cr3 load exiting: i.e. nested EPT */ if ( !(__n2_exec_control (v) &CPU_BASED_CR3_LOAD_EXITING ) )shadow_to_vvmcs (v,GUEST_CR3 ); + +if ( v->arch.hvm_vmx.cr4_host_mask != ~0UL ) + { +/* Only need to update nested GUEST_CR4 if not all bits are trapped. */ +unsigned long nested_cr4_mask =get_vvmcs (v,CR4_GUEST_HOST_MASK ); + +set_vvmcs (v,GUEST_CR4 , + (get_vvmcs (v,GUEST_CR4 ) & nested_cr4_mask) | + (v->arch.hvm_vcpu.guest_cr[4 ] & ~nested_cr4_mask)); + } }static void sync_vvmcs_ro (struct vcpu *v)
Signature · 7 lines
2.16.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 55 lines · Roger Pau Monne
Commit 406817 doesn't update nested VMX code in order to take into account L1 CR4 host mask when nested guest (L2) writes to CR4, and thus the mask written to
CR4_GUEST_HOST_MASKis likely not as restrictive as it should be.Also the VVMCS
GUEST_CR4value should be updated to match the underlying value when syncing the VVMCS state.Fixes 406817 ("vmx/hap: optimize CR4 trapping")Signed-off-by Roger Pau Monné <[email protected]>---
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
---
I've manually tested and AFAICT this fixes the osstest failure detected in 120076 ("test-amd64-amd64-qemuu-nested-intel").--- xen/arch/x86/hvm/vmx/vmx.c | 4 ++++ xen/arch/x86/hvm/vmx/vvmx.c | 13 ++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-)xen/arch/x86/hvm/vmx/vmx.c +4
-0@@ -1617,6 +1617,10 @@ static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr, v->arch.hvm_vmx.cr4_host_mask |= ~v->domain->arch.monitor.write_ctrlreg_mask[VM_EVENT_X86_CR4 ]; +if (nestedhvm_vcpu_in_guestmode (v) ) +/* Add the nested host mask to get the more restrictive one. */ + v->arch.hvm_vmx.cr4_host_mask |=get_vvmcs (v, +CR4_GUEST_HOST_MASK ); }__vmwrite (CR4_GUEST_HOST_MASK , v->arch.hvm_vmx.cr4_host_mask);xen/arch/x86/hvm/vmx/vvmx.c +2
-1@@ -1101,7 +1101,8 @@ static void load_shadow_guest_state(struct vcpu *v) (get_vvmcs (v,CR4_READ_SHADOW ) & cr_gh_mask);__vmwrite (CR4_READ_SHADOW , cr_read_shadow);/* Add the nested host mask to the one set by vmx_update_guest_cr. */ -__vmwrite (CR4_GUEST_HOST_MASK , cr_gh_mask | v->arch.hvm_vmx.cr4_host_mask); + v->arch.hvm_vmx.cr4_host_mask |= cr_gh_mask; +__vmwrite (CR4_GUEST_HOST_MASK , v->arch.hvm_vmx.cr4_host_mask);/* TODO: CR3 target control */ }@@ -1232,6 +1233,16 @@ static void sync_vvmcs_guest_state(struct vcpu *v, struct cpu_user_regs *regs)/* CR3 sync if exec doesn't want cr3 load exiting: i.e. nested EPT */ if ( !(__n2_exec_control (v) &CPU_BASED_CR3_LOAD_EXITING ) )
xen/arch/x86/hvm/vmx/vvmx.c static void sync_vvmcs_guest_state(struct vcpu *v, struct cpu_user_regs *regs) · line 1244
Quoted 10 lines
xen/arch/x86/hvm/vmx/vvmx.c +9
-0shadow_to_vvmcs (v,GUEST_CR3 ); + +if ( v->arch.hvm_vmx.cr4_host_mask != ~0UL ) + { +/* Only need to update nested GUEST_CR4 if not all bits are trapped. */ +unsigned long nested_cr4_mask =get_vvmcs (v,CR4_GUEST_HOST_MASK ); + +set_vvmcs (v,GUEST_CR4 , + (get_vvmcs (v,GUEST_CR4 ) & nested_cr4_mask) | + (v->arch.hvm_vcpu.guest_cr[4 ] & ~nested_cr4_mask));
Why reading the old GUEST_CR4 is needed here? Can the new value be set directly from guest_cr[4]?
Quoted 4 lines
xen/arch/x86/hvm/vmx/vvmx.c +1
-0+ } }static void sync_vvmcs_ro (struct vcpu *v)
Signature · 6 lines
Thanks, Sergey _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 61 lines · Sergey Dyasli
Quoted 60 lines · Roger Pau Monne
Commit 406817 doesn't update nested VMX code in order to take into account L1 CR4 host mask when nested guest (L2) writes to CR4, and thus the mask written to
CR4_GUEST_HOST_MASKis likely not as restrictive as it should be.Also the VVMCS
GUEST_CR4value should be updated to match the underlying value when syncing the VVMCS state.Fixes 406817 ("vmx/hap: optimize CR4 trapping")Signed-off-by Roger Pau Monné <[email protected]>---
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
---
I've manually tested and AFAICT this fixes the osstest failure detected in 120076 ("test-amd64-amd64-qemuu-nested-intel").--- xen/arch/x86/hvm/vmx/vmx.c | 4 ++++ xen/arch/x86/hvm/vmx/vvmx.c | 13 ++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-)xen/arch/x86/hvm/vmx/vmx.c +4
-0@@ -1617,6 +1617,10 @@ static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr, v->arch.hvm_vmx.cr4_host_mask |= ~v->domain->arch.monitor.write_ctrlreg_mask[VM_EVENT_X86_CR4 ]; +if (nestedhvm_vcpu_in_guestmode (v) ) +/* Add the nested host mask to get the more restrictive one. */ + v->arch.hvm_vmx.cr4_host_mask |=get_vvmcs (v, +CR4_GUEST_HOST_MASK ); }__vmwrite (CR4_GUEST_HOST_MASK , v->arch.hvm_vmx.cr4_host_mask);xen/arch/x86/hvm/vmx/vvmx.c +6
-1@@ -1101,7 +1101,8 @@ static void load_shadow_guest_state(struct vcpu *v) (get_vvmcs (v,CR4_READ_SHADOW ) & cr_gh_mask);__vmwrite (CR4_READ_SHADOW , cr_read_shadow);/* Add the nested host mask to the one set by vmx_update_guest_cr. */ -__vmwrite (CR4_GUEST_HOST_MASK , cr_gh_mask | v->arch.hvm_vmx.cr4_host_mask); + v->arch.hvm_vmx.cr4_host_mask |= cr_gh_mask; +__vmwrite (CR4_GUEST_HOST_MASK , v->arch.hvm_vmx.cr4_host_mask);/* TODO: CR3 target control */ }@@ -1232,6 +1233,16 @@ static void sync_vvmcs_guest_state(struct vcpu *v, struct cpu_user_regs *regs)/* CR3 sync if exec doesn't want cr3 load exiting: i.e. nested EPT */ if ( !(__n2_exec_control (v) &CPU_BASED_CR3_LOAD_EXITING ) )shadow_to_vvmcs (v,GUEST_CR3 ); + +if ( v->arch.hvm_vmx.cr4_host_mask != ~0UL ) + { +/* Only need to update nested GUEST_CR4 if not all bits are trapped. */
Quoted 8 lines
xen/arch/x86/hvm/vmx/vvmx.c static void sync_vvmcs_guest_state(struct vcpu *v, struct cpu_user_regs *regs) · line 1244
Quoted 5 lines
xen/arch/x86/hvm/vmx/vvmx.c +5
-0+unsigned long nested_cr4_mask =get_vvmcs (v,CR4_GUEST_HOST_MASK ); + +set_vvmcs (v,GUEST_CR4 , + (get_vvmcs (v,GUEST_CR4 ) & nested_cr4_mask) | + (v->arch.hvm_vcpu.guest_cr[4 ] & ~nested_cr4_mask));Why reading the old
GUEST_CR4is needed here? Can the new value be set directly from guest_cr[4]?
Yes, I think so. The nested GUEST_CR4 value is the value the L1 hypervisor thinks is written to the hardware CR4 (while the L2 guest is running) and guest_cr[4] contains the value of the CR4 register as seen by the L1 hypervisor.
There's no way CR4 L1 tapped bits can leak into guest_cr[4] because cr4_host_mask is always equally or more restrictive than the nested CR4_GUEST_HOST_MASK. Let me send a new version.
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel