kvm: vmx: add nested virtualization support for xsaves
PATCH 1/4 kvm: vmx: add nested virtualization support for xsaves
Add nested virtualization support for xsaves.
--- arch/x86/kvm/vmx.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx.c +16 -1
@@ -216,6 +216,7 @@ struct __packed vmcs12 {u64 virtual_apic_page_addr;u64 apic_access_addr;u64 ept_pointer; +u64 xss_exit_bitmap;u64 guest_physical_address;u64 vmcs_link_pointer;u64 guest_ia32_debugctl;
@@ -618,6 +619,7 @@ static const unsigned short vmcs_field_to_offset_table[] = {FIELD64 (VIRTUAL_APIC_PAGE_ADDR , virtual_apic_page_addr),FIELD64 (APIC_ACCESS_ADDR , apic_access_addr),FIELD64 (EPT_POINTER , ept_pointer), +FIELD64 (XSS_EXIT_BITMAP , xss_exit_bitmap),FIELD64 (GUEST_PHYSICAL_ADDRESS , guest_physical_address),FIELD64 (VMCS_LINK_POINTER , vmcs_link_pointer), FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
@@ -1104,6 +1106,12 @@ static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)return nested_cpu_has2 (vmcs12,SECONDARY_EXEC_ENABLE_EPT ); } +static inline bool nested_cpu_has_xsaves (struct vmcs12 *vmcs12) +{ +return nested_cpu_has2 (vmcs12,SECONDARY_EXEC_XSAVES ) && +vmx_xsaves_supported (); +} +static inline bool is_exception (u32 intr_info) {return (intr_info & (INTR_INFO_INTR_TYPE_MASK |INTR_INFO_VALID_MASK ))
@@ -2392,7 +2400,8 @@ static __init void nested_vmx_setup_ctls_msrs(void) nested_vmx_secondary_ctls_high &=SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |SECONDARY_EXEC_UNRESTRICTED_GUEST | -SECONDARY_EXEC_WBINVD_EXITING ; +SECONDARY_EXEC_WBINVD_EXITING | +SECONDARY_EXEC_XSAVES ;if (enable_ept) {/* nested EPT: emulate EPT also to L1 */
@@ -7285,6 +7294,8 @@ static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)return nested_cpu_has2 (vmcs12,SECONDARY_EXEC_WBINVD_EXITING );case EXIT_REASON_XSETBV :return 1 ; +case EXIT_REASON_XSAVES :case EXIT_REASON_XRSTORS : +return nested_cpu_has2 (vmcs12,SECONDARY_EXEC_XSAVES );default :return 1 ; }
@@ -8341,6 +8352,8 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)vmcs_writel (GUEST_SYSENTER_ESP , vmcs12->guest_sysenter_esp);vmcs_writel (GUEST_SYSENTER_EIP , vmcs12->guest_sysenter_eip); +if (nested_cpu_has_xsaves (vmcs12)) +vmcs_write64 (XSS_EXIT_BITMAP , vmcs12->xss_exit_bitmap);vmcs_write64 (VMCS_LINK_POINTER ,-1ull ); exec_control = vmcs12->pin_based_vm_exec_control;
@@ -8981,6 +8994,8 @@ static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, vmcs12->guest_sysenter_eip =vmcs_readl (GUEST_SYSENTER_EIP );if (vmx_mpx_supported ()) vmcs12->guest_bndcfgs =vmcs_read64 (GUEST_BNDCFGS ); +if (nested_cpu_has_xsaves (vmcs12)) + vmcs12->xss_exit_bitmap =vmcs_read64 (XSS_EXIT_BITMAP );/* update exit information fields: */
Signature · 1 line
1.9.1
PATCH 2/4 kvm: cpuid: fix the size of xsaves area
The section of CPUID(EAX=0xd, ECX=1) in the spec which commit f5c2290cd01e (KVM: cpuid: mask more bits in leaf 0xd and subleaves) mentioned is older than SDM.
EBX: Bits 31-00: The size in bytes of the XSAVE area containing all states enabled by XCR0|IA32_XSS.
The the value of EBX should represent the size of XCR0 related XSAVE area since IA32_XSS is not used currently.
--- arch/x86/kvm/cpuid.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +4 -1
@@ -477,7 +477,10 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,do_cpuid_1_ent (&entry[i], function, idx);if (idx ==1 ) { entry[i].eax &= kvm_supported_word10_x86_features; - entry[i].ebx =0 ; +if (entry[i].eax &F (XSAVES )) + entry[i].ebx =xstate_required_size (supported, true); +else + entry[i].ebx =xstate_required_size (supported, false); }else {if (entry[i].eax ==0 || !(supported & mask))continue ;
Signature · 1 line
1.9.1
Paolo Bonzini
maintainerRe: [PATCH 2/4] kvm: cpuid: fix the size of xsaves area
Quoted 20 lines · Wanpeng Li
The section of
CPUID(EAX=0xd, ECX=1)in the spec which commitf5c2290cd01e(KVM: cpuid: mask more bits in leaf0xdand subleaves) mentioned is older than SDM.EBX: Bits 31-00: The size in bytes of the XSAVE area containing all states enabled by XCR0|IA32_XSS.
The the value of EBX should represent the size of XCR0 related XSAVE area since
IA32_XSSis not used currently.Signed-off-by Wanpeng Li <[email protected]>--- arch/x86/kvm/cpuid.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)arch/x86/kvm/cpuid.c +0
-0
arch/x86/kvm/cpuid.c static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, · line 483
Quoted 8 lines
arch/x86/kvm/cpuid.c +4
-1@@ -477,7 +477,10 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,do_cpuid_1_ent (&entry[i], function, idx);if (idx ==1 ) { entry[i].eax &= kvm_supported_word10_x86_features; - entry[i].ebx =0 ; +if (entry[i].eax &F (XSAVES )) + entry[i].ebx =xstate_required_size (supported, true); +else + entry[i].ebx =xstate_required_size (supported, false);
On machines without XSAVES, this value is zero though.
Paolo
Quoted 4 lines
arch/x86/kvm/cpuid.c +0
-0}else {if (entry[i].eax ==0 || !(supported & mask))continue ;
Paolo Bonzini
maintainerRe: [PATCH 2/4] kvm: cpuid: fix the size of xsaves area
In reply to Wanpeng LiQuoted 20 lines · Wanpeng Li
The section of
CPUID(EAX=0xd, ECX=1)in the spec which commitf5c2290cd01e(KVM: cpuid: mask more bits in leaf0xdand subleaves) mentioned is older than SDM.EBX: Bits 31-00: The size in bytes of the XSAVE area containing all states enabled by XCR0|IA32_XSS.
The the value of EBX should represent the size of XCR0 related XSAVE area since
IA32_XSSis not used currently.Signed-off-by Wanpeng Li <[email protected]>--- arch/x86/kvm/cpuid.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)arch/x86/kvm/cpuid.c +0
-0
arch/x86/kvm/cpuid.c static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, · line 480
Quoted 5 lines
arch/x86/kvm/cpuid.c +1
-1@@ -477,7 +477,10 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,do_cpuid_1_ent (&entry[i], function, idx);if (idx ==1 ) { entry[i].eax &= kvm_supported_word10_x86_features; - entry[i].ebx =0 ; +if (entry[i].eax &F (XSAVES ))
Also, either you have F(XSAVES)|F(XSAVEC) both here and in patch 3, or you have F(XSAVES). Making it inconsistent is definitely wrong.
Paolo
Quoted 7 lines
arch/x86/kvm/cpuid.c +3
-0+ entry[i].ebx =xstate_required_size (supported, true); + else + entry[i].ebx = xstate_required_size(supported, false); }else {if (entry[i].eax ==0 || !(supported & mask))continue ;
PATCH 3/4 kvm: cpuid: fix xsave area size of XSAVEC
In reply to Wanpeng LiXSAVEC also use the compacted format for the extended region of the XSAVE area. This patch fix it by caculate the size of XSAVEC extended region of XSAVE area as compact format.
--- arch/x86/kvm/cpuid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +1 -1
@@ -92,7 +92,7 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu) } best =kvm_find_cpuid_entry (vcpu,0xD ,1 ); -if (best && (best->eax &F (XSAVES ))) +if (best && (best->eax & (F (XSAVES ) ||F (XSAVEC )))) best->ebx =xstate_required_size (vcpu->arch.xcr0, true);/*
Signature · 1 line
1.9.1
Re: [PATCH 3/4] kvm: cpuid: fix xsave area size of XSAVEC
Quoted 14 lines · Wanpeng Li
XSAVEC also use the compacted format for the extended region of the XSAVE area. This patch fix it by caculate the size of XSAVEC extended region of XSAVE area as compact format.
Signed-off-by Wanpeng Li <[email protected]>---
arch/x86/kvm/cpuid.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/cpuid.c +0
-0
arch/x86/kvm/cpuid.c int kvm_update_cpuid(struct kvm_vcpu *vcpu) · line 95
Quoted 5 lines
arch/x86/kvm/cpuid.c +1
-1@@ -92,7 +92,7 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu) } best =kvm_find_cpuid_entry (vcpu,0xD ,1 ); -if (best && (best->eax &F (XSAVES ))) +if (best && (best->eax & (F (XSAVES ) ||F (XSAVEC ))))
Did you want to use | (bitwise or) instead of II (logical or) ?
Quoted 10 lines
arch/x86/kvm/cpuid.c +0
-0best->ebx =xstate_required_size (vcpu->arch.xcr0, true);/* Signature · 6 lines
1.9.1 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Nadav
Re: [PATCH 3/4] kvm: cpuid: fix xsave area size of XSAVEC
Hi Nadav,
Quoted 18 lines · Nadav Amit
Quoted 16 lines · Wanpeng Li
XSAVEC also use the compacted format for the extended region of the XSAVE area. This patch fix it by caculate the size of XSAVEC extended region of XSAVE area as compact format.
Signed-off-by Wanpeng Li <[email protected]>---
arch/x86/kvm/cpuid.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/cpuid.c +0
-0@@ -92,7 +92,7 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu) }
Quoted 4 lines
arch/x86/kvm/cpuid.c int kvm_update_cpuid(struct kvm_vcpu *vcpu) · line 95
Quoted 3 lines
arch/x86/kvm/cpuid.c +1
-1best =kvm_find_cpuid_entry (vcpu,0xD ,1 ); -if (best && (best->eax &F (XSAVES ))) +if (best && (best->eax & (F (XSAVES ) ||F (XSAVEC ))))Did you want to use | (bitwise or) instead of II (logical or) ?
I make a mistake here, I will fix it in next version.
Regards,
Wanpeng Li
Quoted 17 lines
Quoted 11 lines
arch/x86/kvm/cpuid.c +0
-0best->ebx =xstate_required_size (vcpu->arch.xcr0, true);/* Signature · 6 lines
1.9.1 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.htmlNadav
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Paolo Bonzini
maintainerRe: [PATCH 3/4] kvm: cpuid: fix xsave area size of XSAVEC
In reply to Wanpeng LiQuoted 14 lines · Wanpeng Li
XSAVEC also use the compacted format for the extended region of the XSAVE area. This patch fix it by caculate the size of XSAVEC extended region of XSAVE area as compact format.
Signed-off-by Wanpeng Li <[email protected]>--- arch/x86/kvm/cpuid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/cpuid.c +0
-0
arch/x86/kvm/cpuid.c int kvm_update_cpuid(struct kvm_vcpu *vcpu) · line 95
Quoted 5 lines
arch/x86/kvm/cpuid.c +1
-1@@ -92,7 +92,7 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu) } best =kvm_find_cpuid_entry (vcpu,0xD ,1 ); -if (best && (best->eax &F (XSAVES ))) +if (best && (best->eax & (F (XSAVES ) ||F (XSAVEC ))))
"|" instead of "||" as mentioned by Nadav.
Is the Intel manual implicitly relying on the fact that there is no processor that has XSAVES but not XSAVEC?
Paolo
Quoted 4 lines
arch/x86/kvm/cpuid.c +0
-0best->ebx =xstate_required_size (vcpu->arch.xcr0, true);/*
PATCH 4/4 kvm: vmx: fix VMfailValid when write vmcs02/vmcs01
In reply to Wanpeng LiSDM 30.3 VMWRITE
ELSIF secondary source operand does not correspond to any VMCS field
THEN VMfailValid(VMREAD/VMWRITE from/to unsupported VMCS component);
We can't suppose L1 VMM expose MPX to L2 just if L0 support MPX. There will be VMfailValid if L0 doesn't support MPX and L1 expose MPX to L2 when L0 writes vmcs02/vmcs01, in addition, there is no need to read GUEST_BNDCFGS if L1 VMM doesn't expose it to L2. This patch fix it by both check L0 support xsaves and L1 expose MPX to L2.
--- arch/x86/kvm/vmx.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx.c +6 -3
@@ -8496,7 +8496,8 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)set_cr4_guest_host_mask (vmx); -if (vmcs12->vm_entry_controls &VM_ENTRY_LOAD_BNDCFGS ) +if (vmx_mpx_supported () && + (vmcs12->vm_entry_controls &VM_ENTRY_LOAD_BNDCFGS ))vmcs_write64 (GUEST_BNDCFGS , vmcs12->guest_bndcfgs);if (vmcs12->cpu_based_vm_exec_control &CPU_BASED_USE_TSC_OFFSETING )
@@ -8992,7 +8993,8 @@ static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, vmcs12->guest_sysenter_cs =vmcs_read32 (GUEST_SYSENTER_CS ); vmcs12->guest_sysenter_esp =vmcs_readl (GUEST_SYSENTER_ESP ); vmcs12->guest_sysenter_eip =vmcs_readl (GUEST_SYSENTER_EIP ); -if (vmx_mpx_supported ()) +if (vmx_mpx_supported () && + (vmcs12->vm_entry_controls &VM_ENTRY_LOAD_BNDCFGS )) vmcs12->guest_bndcfgs =vmcs_read64 (GUEST_BNDCFGS );if (nested_cpu_has_xsaves (vmcs12)) vmcs12->xss_exit_bitmap =vmcs_read64 (XSS_EXIT_BITMAP );
@@ -9106,7 +9108,8 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,vmcs_writel (GUEST_GDTR_BASE , vmcs12->host_gdtr_base);/* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ -if (vmcs12->vm_exit_controls &VM_EXIT_CLEAR_BNDCFGS ) +if (vmx_mpx_supported () && + (vmcs12->vm_exit_controls &VM_EXIT_CLEAR_BNDCFGS ))vmcs_write64 (GUEST_BNDCFGS ,0 );if (vmcs12->vm_exit_controls &VM_EXIT_LOAD_IA32_PAT ) {
Signature · 1 line
1.9.1