Enable FRED with KVM VMX
Subsystems (8)
cover Enable FRED with KVM VMX
This patch set enables the Intel flexible return and event delivery (FRED) architecture with KVM VMX to allow guests to utilize FRED.
The FRED architecture defines simple new transitions that change privilege level (ring transitions). The FRED architecture was designed with the following goals:
1) Improve overall performance and response time by replacing event delivery through the interrupt descriptor table (IDT event delivery) and event return by the IRET instruction with lower latency transitions.
2) Improve software robustness by ensuring that event delivery establishes the full supervisor context and that event return establishes the full user context.
The new transitions defined by the FRED architecture are FRED event delivery and, for returning from events, two FRED return instructions. FRED event delivery can effect a transition from ring 3 to ring 0, but it is used also to deliver events incident to ring 0. One FRED instruction (ERETU) effects a return from ring 0 to ring 3, while the other (ERETS) returns while remaining in ring 0. Collectively, FRED event delivery and the FRED return instructions are FRED transitions.
Intel VMX architecture is extended to run FRED guests, and the changes are majorly:
1) New VMCS fields for FRED context management, which includes two new event data VMCS fields, eight new guest FRED context VMCS fields and eight new host FRED context VMCS fields.
2) VMX nested-Exception support for proper virtualization of stack levels introduced with FRED architecture.
Search for the latest FRED spec in most search engines with this search pattern:
site:intel.com FRED (flexible return and event delivery) specification
We want to send out the FRED VMX patch set for review while the FRED native patch set v12 is being reviewed @
https://lkml.kernel.org/kvm/[email protected]/.
For easier review, I have set up a base tree with the latest FRED native patch set on top of tip tree in the 'fred_v12' branch of repo
https://github.com/xinli-intel/linux-fred-public.git.
Patch 1-2 are cleanups to VMX basic and misc MSRs, which were sent out earlier as a preparation for FRED changes:
https://lore.kernel.org/kvm/[email protected]/.
Patch 3-14 add FRED support to VMX.
Patch 15-18 add FRED support to nested VMX.
Patch 19 exposes FRED to KVM guests to complete the enabling. Patch 20-23 adds FRED selftests.
Shan Kang (1):
KVM: selftests: Add fred exception tests
Xin Li (22):
KVM: VMX: Cleanup VMX basic information defines and usages
KVM: VMX: Cleanup VMX misc information defines and usages
KVM: VMX: Add support for the secondary VM exit controls
KVM: x86: Mark CR4.FRED as not reserved
KVM: VMX: Initialize FRED VM entry/exit controls in vmcs_config
KVM: VMX: Defer enabling FRED MSRs save/load until after set CPUID
KVM: VMX: Disable intercepting FRED MSRs
KVM: VMX: Initialize VMCS FRED fields
KVM: VMX: Switch FRED RSP0 between host and guest
KVM: VMX: Add support for FRED context save/restore
KVM: x86: Add kvm_is_fred_enabled()
KVM: VMX: Handle FRED event data
KVM: VMX: Handle VMX nested exception for FRED
KVM: VMX: Dump FRED context in dump_vmcs()
KVM: nVMX: Add support for the secondary VM exit controls
KVM: nVMX: Add FRED VMCS fields
KVM: nVMX: Add support for VMX FRED controls
KVM: nVMX: Add VMCS FRED states checking
KVM: x86: Allow FRED/LKGS/WRMSRNS to be exposed to guests
KVM: selftests: Add FRED VMCS fields to evmcs
KVM: selftests: Run debug_regs test with FRED enabled
KVM: selftests: Add a new VM guest mode to run user level code
Documentation/virt/kvm/x86/nested-vmx.rst | 19 + arch/x86/include/asm/hyperv-tlfs.h | 19 + arch/x86/include/asm/kvm_host.h | 9 +- arch/x86/include/asm/msr-index.h | 15 +- arch/x86/include/asm/vmx.h | 57 ++- arch/x86/kvm/cpuid.c | 4 +- arch/x86/kvm/kvm_cache_regs.h | 10 + arch/x86/kvm/svm/svm.c | 4 +- arch/x86/kvm/vmx/capabilities.h | 20 +- arch/x86/kvm/vmx/hyperv.c | 61 ++- arch/x86/kvm/vmx/nested.c | 315 ++++++++++++-- arch/x86/kvm/vmx/nested.h | 2 +- arch/x86/kvm/vmx/vmcs.h | 1 + arch/x86/kvm/vmx/vmcs12.c | 19 + arch/x86/kvm/vmx/vmcs12.h | 38 ++ arch/x86/kvm/vmx/vmcs_shadow_fields.h | 6 +- arch/x86/kvm/vmx/vmx.c | 404 ++++++++++++++++-- arch/x86/kvm/vmx/vmx.h | 14 +- arch/x86/kvm/x86.c | 55 ++- arch/x86/kvm/x86.h | 5 +- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/include/kvm_util_base.h | 1 + .../selftests/kvm/include/x86_64/evmcs.h | 146 +++++++ .../selftests/kvm/include/x86_64/processor.h | 33 ++ .../selftests/kvm/include/x86_64/vmx.h | 20 + tools/testing/selftests/kvm/lib/kvm_util.c | 5 +- .../selftests/kvm/lib/x86_64/processor.c | 15 +- tools/testing/selftests/kvm/lib/x86_64/vmx.c | 4 +- .../testing/selftests/kvm/x86_64/debug_regs.c | 50 ++- .../testing/selftests/kvm/x86_64/fred_test.c | 262 ++++++++++++ 30 files changed, 1464 insertions(+), 150 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86_64/fred_test.c
base-commit: d49b86c24e836941c85c4906e9519fca9426a6e0
Signature · 1 line
2.42.0
PATCH 1/23 KVM: VMX: Cleanup VMX basic information defines and usages
Define VMX basic information fields with BIT_ULL()/GENMASK_ULL(), and replace hardcoded VMX basic numbers with these field macros.
Per Sean's ask, save the full/raw value of MSR_IA32_VMX_BASIC in the global vmcs_config as type u64 to get rid of the hi/lo crud, and then use VMX_BASIC helpers to extract info as needed.
---
Changes since v2:
* Simply save the full/raw value of MSR_IA32_VMX_BASIC in the global vmcs_config, and then use the helpers to extract info from it as needed (Sean Christopherson).
* Move all VMX_MISC related changes to the second patch (Kai Huang).
* Commonize memory type definitions used in the VMX files, as memory types are architectural.
Changes since v1:
* Don't add field shift macros unless it's really needed, extra layer of indirect makes it harder to read (Sean Christopherson).
* Add a static_assert() to ensure that VMX_BASIC_FEATURES_MASK doesn't overlap with VMX_BASIC_RESERVED_BITS (Sean Christopherson).
* read MSR_IA32_VMX_BASIC into an u64 rather than 2 u32 (Sean Christopherson).
* Add 2 new functions for extracting fields from VMX basic (Sean Christopherson).
* Drop the tools header update (Sean Christopherson).
* Move VMX basic field macros to arch/x86/include/asm/vmx.h.
--- arch/x86/include/asm/msr-index.h | 9 --------- arch/x86/include/asm/vmx.h | 15 +++++++++++++-- arch/x86/kvm/vmx/capabilities.h | 6 ++---- arch/x86/kvm/vmx/nested.c | 31 +++++++++++++++++++++---------- arch/x86/kvm/vmx/vmx.c | 24 ++++++++++-------------- 5 files changed, 46 insertions(+), 39 deletions(-)
arch/x86/include/asm/msr-index.h +0 -9
@@ -1110,15 +1110,6 @@#define MSR_IA32_VMX_VMFUNC 0x00000491#define MSR_IA32_VMX_PROCBASED_CTLS3 0x00000492 -/* VMX_BASIC bits and bitmasks */ -#define VMX_BASIC_VMCS_SIZE_SHIFT 32 -#define VMX_BASIC_TRUE_CTLS (1ULL << 55) -#define VMX_BASIC_64 0x0001000000000000LLU -#define VMX_BASIC_MEM_TYPE_SHIFT 50 -#define VMX_BASIC_MEM_TYPE_MASK 0x003c000000000000LLU -#define VMX_BASIC_MEM_TYPE_WB 6LLU -#define VMX_BASIC_INOUT 0x0040000000000000LLU -/* Resctrl MSRs: */ /* - Intel: */ #define MSR_IA32_L3_QOS_CFG 0xc81
arch/x86/include/asm/vmx.h +13 -2
@@ -121,6 +121,14 @@#define VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR 0x000011ff +/* x86 memory types, explicitly used in VMX only */ +#define MEM_TYPE_WB 0x6ULL +#define MEM_TYPE_UC 0x0ULL + +/* VMX_BASIC bits and bitmasks */ +#define VMX_BASIC_32BIT_PHYS_ADDR_ONLY BIT_ULL(48) +#define VMX_BASIC_INOUT BIT_ULL(54) +#define VMX_MISC_PREEMPTION_TIMER_RATE_MASK 0x0000001f#define VMX_MISC_SAVE_EFER_LMA 0x00000020#define VMX_MISC_ACTIVITY_HLT 0x00000040
@@ -144,6 +152,11 @@ static inline u32 vmx_basic_vmcs_size(u64 vmx_basic)return (vmx_basic &GENMASK_ULL (44 ,32 )) >>32 ; } +static inline u32 vmx_basic_vmcs_mem_type (u64 vmx_basic) +{ +return (vmx_basic &GENMASK_ULL (53 ,50 )) >>50 ; +} +static inline int vmx_misc_preemption_timer_rate (u64 vmx_misc) {return vmx_misc &VMX_MISC_PREEMPTION_TIMER_RATE_MASK ;
@@ -506,8 +519,6 @@ enum vmcs_field {#define VMX_EPTP_PWL_5 0x20ull#define VMX_EPTP_AD_ENABLE_BIT (1ull << 6)#define VMX_EPTP_MT_MASK 0x7ull -#define VMX_EPTP_MT_WB 0x6ull -#define VMX_EPTP_MT_UC 0x0ull#define VMX_EPT_READABLE_MASK 0x1ull#define VMX_EPT_WRITABLE_MASK 0x2ull#define VMX_EPT_EXECUTABLE_MASK 0x4ull
arch/x86/kvm/vmx/capabilities.h +2 -4
@@ -54,9 +54,7 @@ struct nested_vmx_msrs { };struct vmcs_config { -int size; -u32 basic_cap; -u32 revision_id; +u64 basic;u32 pin_based_exec_ctrl;u32 cpu_based_exec_ctrl;u32 cpu_based_2nd_exec_ctrl;
@@ -76,7 +74,7 @@ extern struct vmx_capability vmx_capability __ro_after_init;static inline bool cpu_has_vmx_basic_inout (void ) { -return (((u64 )vmcs_config.basic_cap <<32 ) &VMX_BASIC_INOUT ); +return vmcs_config.basic &VMX_BASIC_INOUT ; }static inline bool cpu_has_virtual_nmis (void )
arch/x86/kvm/vmx/nested.c +21 -10
@@ -1201,23 +1201,34 @@ static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)return (superset | subset) == superset; } +#define VMX_BASIC_VMCS_SIZE_SHIFT 32 +#define VMX_BASIC_DUAL_MONITOR_TREATMENT BIT_ULL(49) +#define VMX_BASIC_MEM_TYPE_SHIFT 50 +#define VMX_BASIC_TRUE_CTLS BIT_ULL(55) + +#define VMX_BASIC_FEATURES_MASK \ + (VMX_BASIC_DUAL_MONITOR_TREATMENT | \ + VMX_BASIC_INOUT | \ + VMX_BASIC_TRUE_CTLS) + +#define VMX_BASIC_RESERVED_BITS \ + (GENMASK_ULL(63, 56) | GENMASK_ULL(47, 45) | BIT_ULL(31)) +static int vmx_restore_vmx_basic (struct vcpu_vmx *vmx,u64 data) { -const u64 feature_and_reserved = -/* feature (except bit 48; see below) */ -BIT_ULL (49 ) |BIT_ULL (54 ) |BIT_ULL (55 ) | -/* reserved */ -BIT_ULL (31 ) |GENMASK_ULL (47 ,45 ) |GENMASK_ULL (63 ,56 );u64 vmx_basic = vmcs_config.nested.basic; -if (!is_bitwise_subset (vmx_basic, data, feature_and_reserved)) +static_assert (!(VMX_BASIC_FEATURES_MASK &VMX_BASIC_RESERVED_BITS )); + +if (!is_bitwise_subset (vmx_basic, data, +VMX_BASIC_FEATURES_MASK |VMX_BASIC_RESERVED_BITS ))return -EINVAL ;/* * KVM does not emulate a version of VMX that constrains physical * addresses of VMX structures (e.g. VMCS) to 32-bits. */ -if (data &BIT_ULL (48 )) +if (data &VMX_BASIC_32BIT_PHYS_ADDR_ONLY )return -EINVAL ; if (vmx_basic_vmcs_revision_id(vmx_basic) !=
@@ -2690,11 +2701,11 @@ static bool nested_vmx_check_eptp(struct kvm_vcpu *vcpu, u64 new_eptp)/* Check for memory type validity */ switch (new_eptp &VMX_EPTP_MT_MASK ) { -case VMX_EPTP_MT_UC : +case MEM_TYPE_UC :if (CC (!(vmx->nested.msrs.ept_caps &VMX_EPTP_UC_BIT )))return false;break ; -case VMX_EPTP_MT_WB : +case MEM_TYPE_WB :if (CC (!(vmx->nested.msrs.ept_caps &VMX_EPTP_WB_BIT )))return false;break ;
@@ -6964,7 +6975,7 @@ static void nested_vmx_setup_basic(struct nested_vmx_msrs *msrs)VMCS12_REVISION |VMX_BASIC_TRUE_CTLS | ((u64 )VMCS12_SIZE <<VMX_BASIC_VMCS_SIZE_SHIFT ) | - (VMX_BASIC_MEM_TYPE_WB <<VMX_BASIC_MEM_TYPE_SHIFT ); + (MEM_TYPE_WB <<VMX_BASIC_MEM_TYPE_SHIFT );if (cpu_has_vmx_basic_inout ()) msrs->basic |=VMX_BASIC_INOUT ;
arch/x86/kvm/vmx/vmx.c +10 -14
@@ -2569,13 +2569,13 @@ static u64 adjust_vmx_controls64(u64 ctl_opt, u32 msr)static int setup_vmcs_config (struct vmcs_config *vmcs_conf,struct vmx_capability *vmx_cap) { -u32 vmx_msr_low, vmx_msr_high;u32 _pin_based_exec_control =0 ;u32 _cpu_based_exec_control =0 ;u32 _cpu_based_2nd_exec_control =0 ;u64 _cpu_based_3rd_exec_control =0 ;u32 _vmexit_control =0 ;u32 _vmentry_control =0 ; +u64 basic_msr;u64 misc_msr;int i;
@@ -2694,29 +2694,25 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf, _vmexit_control &= ~x_ctrl; } -rdmsr (MSR_IA32_VMX_BASIC , vmx_msr_low, vmx_msr_high); +rdmsrl (MSR_IA32_VMX_BASIC , basic_msr);/* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ -if ((vmx_msr_high &0x1fff ) >PAGE_SIZE ) +if ((vmx_basic_vmcs_size (basic_msr) >PAGE_SIZE ))return -EIO ;#ifdef CONFIG_X86_64 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ -if (vmx_msr_high & (1u <<16 )) +if (basic_msr &VMX_BASIC_32BIT_PHYS_ADDR_ONLY )return -EIO ;#endif /* Require Write-Back (WB) memory type for VMCS accesses. */ -if (((vmx_msr_high >>18 ) &15 ) !=6 ) +if (vmx_basic_vmcs_mem_type (basic_msr) !=MEM_TYPE_WB )return -EIO ;rdmsrl (MSR_IA32_VMX_MISC , misc_msr); - vmcs_conf->size = vmx_msr_high &0x1fff ; - vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff ; - - vmcs_conf->revision_id = vmx_msr_low; - + vmcs_conf->basic = basic_msr; vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
@@ -2866,13 +2862,13 @@ struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags)if (!pages)return NULL ; vmcs =page_address (pages); -memset (vmcs,0 , vmcs_config.size); +memset (vmcs,0 ,vmx_basic_vmcs_size (vmcs_config.basic));/* KVM supports Enlightened VMCS v1 only */ if (kvm_is_using_evmcs ()) vmcs->hdr.revision_id =KVM_EVMCS_VERSION ;else - vmcs->hdr.revision_id = vmcs_config.revision_id; + vmcs->hdr.revision_id =vmx_basic_vmcs_revision_id (vmcs_config.basic);if (shadow) vmcs->hdr.shadow_vmcs =1 ;
@@ -2965,7 +2961,7 @@ static __init int alloc_kvm_area(void)* physical CPU. */ if (kvm_is_using_evmcs ()) - vmcs->hdr.revision_id = vmcs_config.revision_id; + vmcs->hdr.revision_id =vmx_basic_vmcs_revision_id (vmcs_config.basic);per_cpu (vmxarea, cpu) = vmcs; }
@@ -3367,7 +3363,7 @@ static int vmx_get_max_ept_level(void)u64 construct_eptp (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) { -u64 eptp =VMX_EPTP_MT_WB ; +u64 eptp =MEM_TYPE_WB ; eptp |= (root_level ==5 ) ?VMX_EPTP_PWL_5 :VMX_EPTP_PWL_4 ;
Signature · 1 line
2.42.0
PATCH 2/23 KVM: VMX: Cleanup VMX misc information defines and usages
In reply to Xin LiDefine VMX misc information fields with BIT_ULL()/GENMASK_ULL(), and move VMX misc field macros to vmx.h if used in multiple files or where they are used only once.
--- arch/x86/include/asm/msr-index.h | 5 ----- arch/x86/include/asm/vmx.h | 12 +++++------ arch/x86/kvm/vmx/capabilities.h | 4 ++-- arch/x86/kvm/vmx/nested.c | 34 ++++++++++++++++++++++++-------- arch/x86/kvm/vmx/nested.h | 2 +- arch/x86/kvm/vmx/vmx.c | 8 +++----- 6 files changed, 37 insertions(+), 28 deletions(-)
arch/x86/include/asm/msr-index.h +0 -5
@@ -1126,11 +1126,6 @@#define MSR_IA32_SMBA_BW_BASE 0xc0000280#define MSR_IA32_EVT_CFG_BASE 0xc0000400 -/* MSR_IA32_VMX_MISC bits */ -#define MSR_IA32_VMX_MISC_INTEL_PT (1ULL << 14) -#define MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS (1ULL << 29) -#define MSR_IA32_VMX_MISC_PREEMPTION_TIMER_SCALE 0x1F -/* AMD-V MSRs */ #define MSR_VM_CR 0xc0010114#define MSR_VM_IGNNE 0xc0010115
arch/x86/include/asm/vmx.h +5 -7
@@ -129,12 +129,10 @@#define VMX_BASIC_32BIT_PHYS_ADDR_ONLY BIT_ULL(48)#define VMX_BASIC_INOUT BIT_ULL(54) -#define VMX_MISC_PREEMPTION_TIMER_RATE_MASK 0x0000001f -#define VMX_MISC_SAVE_EFER_LMA 0x00000020 -#define VMX_MISC_ACTIVITY_HLT 0x00000040 -#define VMX_MISC_ACTIVITY_WAIT_SIPI 0x00000100 -#define VMX_MISC_ZERO_LEN_INS 0x40000000 -#define VMX_MISC_MSR_LIST_MULTIPLIER 512 +/* VMX_MISC bits and bitmasks */ +#define VMX_MISC_INTEL_PT BIT_ULL(14) +#define VMX_MISC_VMWRITE_SHADOW_RO_FIELDS BIT_ULL(29) +#define VMX_MISC_ZERO_LEN_INS BIT_ULL(30)/* VMFUNC functions */ #define VMFUNC_CONTROL_BIT (x) BIT((VMX_FEATURE_##x & 0x1f) - 28)
@@ -159,7 +157,7 @@ static inline u32 vmx_basic_vmcs_mem_type(u64 vmx_basic)static inline int vmx_misc_preemption_timer_rate (u64 vmx_misc) { -return vmx_misc &VMX_MISC_PREEMPTION_TIMER_RATE_MASK ; +return vmx_misc &GENMASK_ULL (4 ,0 ); }static inline int vmx_misc_cr3_count (u64 vmx_misc)
arch/x86/kvm/vmx/capabilities.h +2 -2
@@ -223,7 +223,7 @@ static inline bool cpu_has_vmx_vmfunc(void)static inline bool cpu_has_vmx_shadow_vmcs (void ) {/* check if the cpu supports writing r/o exit information fields */ -if (!(vmcs_config.misc &MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS )) +if (!(vmcs_config.misc &VMX_MISC_VMWRITE_SHADOW_RO_FIELDS ))return false; return vmcs_config.cpu_based_2nd_exec_ctrl &
@@ -365,7 +365,7 @@ static inline bool cpu_has_vmx_invvpid_global(void)static inline bool cpu_has_vmx_intel_pt (void ) { -return (vmcs_config.misc &MSR_IA32_VMX_MISC_INTEL_PT ) && +return (vmcs_config.misc &VMX_MISC_INTEL_PT ) && (vmcs_config.cpu_based_2nd_exec_ctrl &SECONDARY_EXEC_PT_USE_GPA ) && (vmcs_config.vmentry_ctrl &VM_ENTRY_LOAD_IA32_RTIT_CTL ); }
arch/x86/kvm/vmx/nested.c +26 -8
@@ -886,6 +886,8 @@ static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,return 0 ; } +#define VMX_MISC_MSR_LIST_MULTIPLIER 512 +static u32 nested_vmx_max_atomic_switch_msrs (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu);
@@ -1295,18 +1297,34 @@ vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)return 0 ; } +#define VMX_MISC_SAVE_EFER_LMA BIT_ULL(5) +#define VMX_MISC_ACTIVITY_STATE_BITMAP GENMASK_ULL(8, 6) +#define VMX_MISC_ACTIVITY_HLT BIT_ULL(6) +#define VMX_MISC_ACTIVITY_WAIT_SIPI BIT_ULL(8) +#define VMX_MISC_RDMSR_IN_SMM BIT_ULL(15) +#define VMX_MISC_VMXOFF_BLOCK_SMI BIT_ULL(28) + +#define VMX_MISC_FEATURES_MASK \ + (VMX_MISC_SAVE_EFER_LMA | \ + VMX_MISC_ACTIVITY_STATE_BITMAP | \ + VMX_MISC_INTEL_PT | \ + VMX_MISC_RDMSR_IN_SMM | \ + VMX_MISC_VMXOFF_BLOCK_SMI | \ + VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | \ + VMX_MISC_ZERO_LEN_INS) + +#define VMX_MISC_RESERVED_BITS \ + (BIT_ULL(31) | GENMASK_ULL(13, 9)) +static int vmx_restore_vmx_misc (struct vcpu_vmx *vmx,u64 data) { -const u64 feature_and_reserved_bits = -/* feature */ -BIT_ULL (5 ) |GENMASK_ULL (8 ,6 ) |BIT_ULL (14 ) |BIT_ULL (15 ) | -BIT_ULL (28 ) |BIT_ULL (29 ) |BIT_ULL (30 ) | -/* reserved */ -GENMASK_ULL (13 ,9 ) |BIT_ULL (31 );u64 vmx_misc =vmx_control_msr (vmcs_config.nested.misc_low, vmcs_config.nested.misc_high); -if (!is_bitwise_subset (vmx_misc, data, feature_and_reserved_bits)) +static_assert (!(VMX_MISC_FEATURES_MASK &VMX_MISC_RESERVED_BITS )); + +if (!is_bitwise_subset (vmx_misc, data, +VMX_MISC_FEATURES_MASK |VMX_MISC_RESERVED_BITS ))return -EINVAL ; if ((vmx->nested.msrs.pinbased_ctls_high &
@@ -6956,7 +6974,7 @@ static void nested_vmx_setup_misc_data(struct vmcs_config *vmcs_conf, { msrs->misc_low = (u32 )vmcs_conf->misc &VMX_MISC_SAVE_EFER_LMA ; msrs->misc_low |= -MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | +VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |VMX_MISC_ACTIVITY_HLT |VMX_MISC_ACTIVITY_WAIT_SIPI ;
arch/x86/kvm/vmx/nested.h +1 -1
@@ -108,7 +108,7 @@ static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)static inline bool nested_cpu_has_vmwrite_any_field (struct kvm_vcpu *vcpu) {return to_vmx (vcpu)->nested.msrs.misc_low & -MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS ; +VMX_MISC_VMWRITE_SHADOW_RO_FIELDS ; }static inline bool nested_cpu_has_zero_length_injection (struct kvm_vcpu *vcpu)
arch/x86/kvm/vmx/vmx.c +3 -5
@@ -2576,7 +2576,6 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf,u32 _vmexit_control =0 ;u32 _vmentry_control =0 ;u64 basic_msr; -u64 misc_msr;int i;/*
@@ -2710,8 +2709,6 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf,if (vmx_basic_vmcs_mem_type (basic_msr) !=MEM_TYPE_WB )return -EIO ; -rdmsrl (MSR_IA32_VMX_MISC , misc_msr); - vmcs_conf->basic = basic_msr; vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
@@ -2719,7 +2716,8 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf, vmcs_conf->cpu_based_3rd_exec_ctrl = _cpu_based_3rd_exec_control; vmcs_conf->vmexit_ctrl = _vmexit_control; vmcs_conf->vmentry_ctrl = _vmentry_control; - vmcs_conf->misc = misc_msr; + +rdmsrl (MSR_IA32_VMX_MISC , vmcs_conf->misc);#if IS_ENABLED (CONFIG_HYPERV )if (enlightened_vmcs)
@@ -8555,7 +8553,7 @@ static __init int hardware_setup(void)u64 use_timer_freq =5000ULL *1000 *1000 ; cpu_preemption_timer_multi = - vmcs_config.misc &VMX_MISC_PREEMPTION_TIMER_RATE_MASK ; +vmx_misc_preemption_timer_rate (vmcs_config.misc);if (tsc_khz) use_timer_freq = (u64 )tsc_khz *1000 ;
Signature · 1 line
2.42.0
PATCH 4/23 KVM: x86: Mark CR4.FRED as not reserved
In reply to Xin LiThe CR4.FRED bit, i.e., CR4[32], is no longer a reserved bit when a guest enumerates FRED, otherwise it is still a reserved bit.
--- arch/x86/include/asm/kvm_host.h | 3 ++- arch/x86/kvm/x86.h | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +2 -1
@@ -133,7 +133,8 @@ | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR | X86_CR4_PCIDE \ | X86_CR4_OSXSAVE | X86_CR4_SMEP | X86_CR4_FSGSBASE \ | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_VMXE \ - | X86_CR4_SMAP | X86_CR4_PKE | X86_CR4_UMIP)) + | X86_CR4_SMAP | X86_CR4_PKE | X86_CR4_UMIP \ + | X86_CR4_FRED))#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
arch/x86/kvm/x86.h +2 -0
@@ -530,6 +530,8 @@ bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type); __reserved_bits |=X86_CR4_VMXE ; \if (!__cpu_has (__c,X86_FEATURE_PCID )) \ __reserved_bits |=X86_CR4_PCIDE ; \ +if (!__cpu_has (__c,X86_FEATURE_FRED )) \ + __reserved_bits |=X86_CR4_FRED ; \ __reserved_bits; \ })
Signature · 1 line
2.42.0
PATCH 3/23 KVM: VMX: Add support for the secondary VM exit controls
In reply to Xin LiEnable the secondary VM exit controls to prepare for FRED enabling.
The activation of the secondary VM exit controls is off now, and it will be switched on when a VMX feature needing it is enabled.
--- arch/x86/include/asm/msr-index.h | 1 + arch/x86/include/asm/vmx.h | 3 +++ arch/x86/kvm/vmx/capabilities.h | 9 ++++++++- arch/x86/kvm/vmx/vmcs.h | 1 + arch/x86/kvm/vmx/vmx.c | 28 +++++++++++++++++++++++++--- arch/x86/kvm/vmx/vmx.h | 7 ++++++- 6 files changed, 44 insertions(+), 5 deletions(-)
arch/x86/include/asm/msr-index.h +1 -0
@@ -1109,6 +1109,7 @@#define MSR_IA32_VMX_TRUE_ENTRY_CTLS 0x00000490#define MSR_IA32_VMX_VMFUNC 0x00000491#define MSR_IA32_VMX_PROCBASED_CTLS3 0x00000492 +#define MSR_IA32_VMX_EXIT_CTLS2 0x00000493/* Resctrl MSRs: */ /* - Intel: */
arch/x86/include/asm/vmx.h +3 -0
@@ -105,6 +105,7 @@#define VM_EXIT_CLEAR_BNDCFGS 0x00800000#define VM_EXIT_PT_CONCEAL_PIP 0x01000000#define VM_EXIT_CLEAR_IA32_RTIT_CTL 0x02000000 +#define VM_EXIT_ACTIVATE_SECONDARY_CONTROLS 0x80000000#define VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR 0x00036dff
@@ -247,6 +248,8 @@ enum vmcs_field {TERTIARY_VM_EXEC_CONTROL_HIGH =0x00002035 ,PID_POINTER_TABLE =0x00002042 ,PID_POINTER_TABLE_HIGH =0x00002043 , +SECONDARY_VM_EXIT_CONTROLS =0x00002044 , +SECONDARY_VM_EXIT_CONTROLS_HIGH =0x00002045 ,GUEST_PHYSICAL_ADDRESS =0x00002400 ,GUEST_PHYSICAL_ADDRESS_HIGH =0x00002401 , VMCS_LINK_POINTER = 0x00002800,
arch/x86/kvm/vmx/capabilities.h +8 -1
@@ -59,8 +59,9 @@ struct vmcs_config {u32 cpu_based_exec_ctrl;u32 cpu_based_2nd_exec_ctrl;u64 cpu_based_3rd_exec_ctrl; -u32 vmexit_ctrl;u32 vmentry_ctrl; +u32 vmexit_ctrl; +u64 secondary_vmexit_ctrl;u64 misc;struct nested_vmx_msrs nested; };
@@ -136,6 +137,12 @@ static inline bool cpu_has_tertiary_exec_ctrls(void)CPU_BASED_ACTIVATE_TERTIARY_CONTROLS ; } +static inline bool cpu_has_secondary_vmexit_ctrls (void ) +{ +return vmcs_config.vmexit_ctrl & +VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ; +} +static inline bool cpu_has_vmx_virtualize_apic_accesses (void ) { return vmcs_config.cpu_based_2nd_exec_ctrl &
arch/x86/kvm/vmx/vmcs.h +1 -0
@@ -47,6 +47,7 @@ struct vmcs_host_state {struct vmcs_controls_shadow {u32 vm_entry;u32 vm_exit; +u64 secondary_vm_exit;u32 pin;u32 exec;u32 secondary_exec;
arch/x86/kvm/vmx/vmx.c +25 -3
@@ -2574,6 +2574,7 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf,u32 _cpu_based_2nd_exec_control =0 ;u64 _cpu_based_3rd_exec_control =0 ;u32 _vmexit_control =0 ; +u64 _secondary_vmexit_control =0 ;u32 _vmentry_control =0 ;u64 basic_msr;int i;
@@ -2693,6 +2694,11 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf, _vmexit_control &= ~x_ctrl; } +if (_vmexit_control &VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ) + _secondary_vmexit_control = +adjust_vmx_controls64 (KVM_OPTIONAL_VMX_SECONDARY_VM_EXIT_CONTROLS , +MSR_IA32_VMX_EXIT_CTLS2 ); +rdmsrl (MSR_IA32_VMX_BASIC , basic_msr);/* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
@@ -2714,8 +2720,9 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf, vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; vmcs_conf->cpu_based_3rd_exec_ctrl = _cpu_based_3rd_exec_control; - vmcs_conf->vmexit_ctrl = _vmexit_control; vmcs_conf->vmentry_ctrl = _vmentry_control; + vmcs_conf->vmexit_ctrl = _vmexit_control; + vmcs_conf->secondary_vmexit_ctrl = _secondary_vmexit_control;rdmsrl (MSR_IA32_VMX_MISC , vmcs_conf->misc);
@@ -4420,9 +4427,21 @@ static u32 vmx_vmexit_ctrl(void)if (cpu_has_perf_global_ctrl_bug ()) vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL ; -/* Loading of EFER and PERF_GLOBAL_CTRL are toggled dynamically */ +/* +* The following features are toggled dynamically: +* - Loading of PERF_GLOBAL_CTRL +* - Loading of EFER +* - Activating of SECONDARY_CONTROLS +*/ return vmexit_ctrl & - ~(VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL |VM_EXIT_LOAD_IA32_EFER ); + ~(VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | +VM_EXIT_LOAD_IA32_EFER | +VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ); +} + +static u64 vmx_secondary_vmexit_ctrl (void ) +{ +return vmcs_config.secondary_vmexit_ctrl; }static void vmx_refresh_apicv_exec_ctrl (struct kvm_vcpu *vcpu)
@@ -4770,6 +4789,9 @@ static void init_vmcs(struct vcpu_vmx *vmx)vm_exit_controls_set (vmx,vmx_vmexit_ctrl ()); +if (cpu_has_secondary_vmexit_ctrls ()) +secondary_vm_exit_controls_set (vmx,vmx_secondary_vmexit_ctrl ()); +/* 22.2.1, 20.8.1 */ vm_entry_controls_set (vmx,vmx_vmentry_ctrl ());
arch/x86/kvm/vmx/vmx.h +6 -1
@@ -502,7 +502,11 @@ static inline u8 vmx_get_rvi(void) VM_EXIT_LOAD_IA32_EFER | \ VM_EXIT_CLEAR_BNDCFGS | \ VM_EXIT_PT_CONCEAL_PIP | \ - VM_EXIT_CLEAR_IA32_RTIT_CTL) + VM_EXIT_CLEAR_IA32_RTIT_CTL | \ + VM_EXIT_ACTIVATE_SECONDARY_CONTROLS) + +#define KVM_REQUIRED_VMX_SECONDARY_VM_EXIT_CONTROLS (0) +#define KVM_OPTIONAL_VMX_SECONDARY_VM_EXIT_CONTROLS (0)#define KVM_REQUIRED_VMX_PIN_BASED_VM_EXEC_CONTROL \ (PIN_BASED_EXT_INTR_MASK | \
@@ -606,6 +610,7 @@ static __always_inline void lname##_controls_clearbit(struct vcpu_vmx *vmx, u##b } BUILD_CONTROLS_SHADOW(vm_entry, VM_ENTRY_CONTROLS, 32) BUILD_CONTROLS_SHADOW(vm_exit, VM_EXIT_CONTROLS, 32) +BUILD_CONTROLS_SHADOW(secondary_vm_exit, SECONDARY_VM_EXIT_CONTROLS, 64) BUILD_CONTROLS_SHADOW(pin, PIN_BASED_VM_EXEC_CONTROL, 32) BUILD_CONTROLS_SHADOW(exec, CPU_BASED_VM_EXEC_CONTROL, 32)BUILD_CONTROLS_SHADOW (secondary_exec,SECONDARY_VM_EXEC_CONTROL ,32 )
Signature · 1 line
2.42.0
PATCH 5/23 KVM: VMX: Initialize FRED VM entry/exit controls in vmcs_config
In reply to Xin LiSetup the global vmcs_config for FRED:
1) Add VM_ENTRY_LOAD_IA32_FRED to KVM_OPTIONAL_VMX_VM_ENTRY_CONTROLS to have a FRED CPU load guest FRED MSRs from VMCS upon VM entry.
2) Add SECONDARY_VM_EXIT_SAVE_IA32_FRED to KVM_OPTIONAL_VMX_SECONDARY_VM_EXIT_CONTROLS to have a FRED CPU save guest FRED MSRs to VMCS during VM exit.
3) add SECONDARY_VM_EXIT_LOAD_IA32_FRED to KVM_OPTIONAL_VMX_SECONDARY_VM_EXIT_CONTROLS to have a FRED CPU load host FRED MSRs from VMCS during VM exit.
Also add sanity checks to make sure FRED VM entry/exit controls can be set on a FRED CPU.
--- arch/x86/include/asm/vmx.h | 3 +++ arch/x86/kvm/vmx/vmx.c | 19 ++++++++++++++++++- arch/x86/kvm/vmx/vmx.h | 7 +++++-- 3 files changed, 26 insertions(+), 3 deletions(-)
arch/x86/include/asm/vmx.h +3 -0
@@ -106,6 +106,8 @@#define VM_EXIT_PT_CONCEAL_PIP 0x01000000#define VM_EXIT_CLEAR_IA32_RTIT_CTL 0x02000000#define VM_EXIT_ACTIVATE_SECONDARY_CONTROLS 0x80000000 +#define SECONDARY_VM_EXIT_SAVE_IA32_FRED 0x00000001 +#define SECONDARY_VM_EXIT_LOAD_IA32_FRED 0x00000002#define VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR 0x00036dff
@@ -119,6 +121,7 @@#define VM_ENTRY_LOAD_BNDCFGS 0x00010000#define VM_ENTRY_PT_CONCEAL_PIP 0x00020000#define VM_ENTRY_LOAD_IA32_RTIT_CTL 0x00040000 +#define VM_ENTRY_LOAD_IA32_FRED 0x00800000#define VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR 0x000011ff
arch/x86/kvm/vmx/vmx.c +18 -1
@@ -2694,10 +2694,27 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf, _vmexit_control &= ~x_ctrl; } -if (_vmexit_control &VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ) +if (_vmexit_control &VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ) { _secondary_vmexit_control =adjust_vmx_controls64 (KVM_OPTIONAL_VMX_SECONDARY_VM_EXIT_CONTROLS ,MSR_IA32_VMX_EXIT_CTLS2 ); +if (cpu_feature_enabled (X86_FEATURE_FRED ) && + !(_secondary_vmexit_control &SECONDARY_VM_EXIT_SAVE_IA32_FRED && + _secondary_vmexit_control &SECONDARY_VM_EXIT_LOAD_IA32_FRED )) { +pr_warn_once ("FRED enabled but no VMX VM-Exit {SAVE,LOAD}_IA32_FRED controls: %llx\n" , + _secondary_vmexit_control); +if (error_on_inconsistent_vmcs_config) +return -EIO ; + } + } + +if (cpu_feature_enabled (X86_FEATURE_FRED ) && + !(_vmentry_control &VM_ENTRY_LOAD_IA32_FRED )) { +pr_warn_once ("FRED enabled but no VMX VM-Entry LOAD_IA32_FRED control: %x\n" , + _vmentry_control); +if (error_on_inconsistent_vmcs_config) +return -EIO ; + }rdmsrl (MSR_IA32_VMX_BASIC , basic_msr);
arch/x86/kvm/vmx/vmx.h +5 -2
@@ -480,7 +480,8 @@ static inline u8 vmx_get_rvi(void) VM_ENTRY_LOAD_IA32_EFER | \ VM_ENTRY_LOAD_BNDCFGS | \ VM_ENTRY_PT_CONCEAL_PIP | \ - VM_ENTRY_LOAD_IA32_RTIT_CTL) + VM_ENTRY_LOAD_IA32_RTIT_CTL | \ + VM_ENTRY_LOAD_IA32_FRED)#define __KVM_REQUIRED_VMX_VM_EXIT_CONTROLS \ (VM_EXIT_SAVE_DEBUG_CONTROLS | \
@@ -506,7 +507,9 @@ static inline u8 vmx_get_rvi(void) VM_EXIT_ACTIVATE_SECONDARY_CONTROLS)#define KVM_REQUIRED_VMX_SECONDARY_VM_EXIT_CONTROLS (0) -#define KVM_OPTIONAL_VMX_SECONDARY_VM_EXIT_CONTROLS (0) +#define KVM_OPTIONAL_VMX_SECONDARY_VM_EXIT_CONTROLS \ + (SECONDARY_VM_EXIT_SAVE_IA32_FRED | \ + SECONDARY_VM_EXIT_LOAD_IA32_FRED)#define KVM_REQUIRED_VMX_PIN_BASED_VM_EXEC_CONTROL \ (PIN_BASED_EXT_INTR_MASK | \
Signature · 1 line
2.42.0
PATCH 7/23 KVM: VMX: Disable intercepting FRED MSRs
In reply to Xin LiAdd FRED MSRs to the valid passthrough MSR list and disable intercepting FRED MSRs only if FRED is enumerated after set CPUID.
--- arch/x86/kvm/vmx/vmx.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
arch/x86/kvm/vmx/vmx.c +13 -0
@@ -700,6 +700,9 @@ static bool is_valid_passthrough_msr(u32 msr) case MSR_LBR_CORE_TO ... MSR_LBR_CORE_TO + 8:/* LBR MSRs. These are handled in vmx_update_intercept_for_lbr_msrs() */ return true; + case MSR_IA32_FRED_RSP0 ... MSR_IA32_FRED_CONFIG: +/* FRED MSRs should be passthrough to FRED guests only */ +return true; } r =possible_passthrough_msr_slot (msr) != -ENOENT ;
@@ -7813,6 +7816,16 @@ static void vmx_vcpu_config_fred_after_set_cpuid(struct kvm_vcpu *vcpu)secondary_vm_exit_controls_setbit (vmx,SECONDARY_VM_EXIT_SAVE_IA32_FRED |SECONDARY_VM_EXIT_LOAD_IA32_FRED ); + +vmx_disable_intercept_for_msr (vcpu,MSR_IA32_FRED_RSP0 ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (vcpu,MSR_IA32_FRED_RSP1 ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (vcpu,MSR_IA32_FRED_RSP2 ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (vcpu,MSR_IA32_FRED_RSP3 ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (vcpu,MSR_IA32_FRED_STKLVLS ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (vcpu,MSR_IA32_FRED_SSP1 ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (vcpu,MSR_IA32_FRED_SSP2 ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (vcpu,MSR_IA32_FRED_SSP3 ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (vcpu,MSR_IA32_FRED_CONFIG ,MSR_TYPE_RW ); }static void vmx_vcpu_after_set_cpuid (struct kvm_vcpu *vcpu)
Signature · 1 line
2.42.0
PATCH 6/23 KVM: VMX: Defer enabling FRED MSRs save/load until after set CPUID
In reply to Xin LiClear FRED VM entry/exit controls when initializing a vCPU, and set these controls only if FRED is enumerated after set CPUID.
FRED VM entry/exit controls need to be set to establish context sufficient to support FRED event delivery immediately after VM entry and exit. However it is not required to save/load FRED MSRs for a non-FRED guest, which aren't supposed to access FRED MSRs.
A non-FRED guest should get #GP upon accessing FRED MSRs, otherwise it corrupts host FRED MSRs.
--- arch/x86/kvm/vmx/vmx.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/vmx.c +33 -1
@@ -4423,6 +4423,9 @@ static u32 vmx_vmentry_ctrl(void)if (cpu_has_perf_global_ctrl_bug ()) vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL ; +/* Whether to load guest FRED MSRs is deferred until after set CPUID */ + vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_FRED ; +return vmentry_ctrl; }
@@ -4458,7 +4461,13 @@ static u32 vmx_vmexit_ctrl(void)static u64 vmx_secondary_vmexit_ctrl (void ) { -return vmcs_config.secondary_vmexit_ctrl; +u64 secondary_vmexit_ctrl = vmcs_config.secondary_vmexit_ctrl; + +/* Whether to save/load FRED MSRs is deferred until after set CPUID */ + secondary_vmexit_ctrl &= ~(SECONDARY_VM_EXIT_SAVE_IA32_FRED | +SECONDARY_VM_EXIT_LOAD_IA32_FRED ); + +return secondary_vmexit_ctrl; }static void vmx_refresh_apicv_exec_ctrl (struct kvm_vcpu *vcpu)
@@ -7785,10 +7794,33 @@ static void update_intel_pt_cfg(struct kvm_vcpu *vcpu) vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i *4 )); } +static void vmx_vcpu_config_fred_after_set_cpuid (struct kvm_vcpu *vcpu) +{ +struct vcpu_vmx *vmx =to_vmx (vcpu); + +if (!cpu_feature_enabled (X86_FEATURE_FRED ) || + !guest_cpuid_has (vcpu,X86_FEATURE_FRED )) +return ; + +/* Enable loading guest FRED MSRs from VMCS */ +vm_entry_controls_setbit (vmx,VM_ENTRY_LOAD_IA32_FRED ); + +/* +* Enable saving guest FRED MSRs into VMCS and loading host FRED MSRs +* from VMCS. +*/ +vm_exit_controls_setbit (vmx,VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ); +secondary_vm_exit_controls_setbit (vmx, +SECONDARY_VM_EXIT_SAVE_IA32_FRED | +SECONDARY_VM_EXIT_LOAD_IA32_FRED ); +} +static void vmx_vcpu_after_set_cpuid (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu); +vmx_vcpu_config_fred_after_set_cpuid (vcpu); +/* * XSAVES is effectively enabled if and only if XSAVE is also exposed * to the guest. XSAVES depends on CR4.OSXSAVE, and CR4.OSXSAVE can be
Signature · 1 line
2.42.0
PATCH 8/23 KVM: VMX: Initialize VMCS FRED fields
In reply to Xin LiInitialize host VMCS FRED fields with host FRED MSRs' value and guest VMCS FRED fields to 0.
FRED CPU states are managed in 9 new FRED MSRs, as well as a few existing CPU registers and MSRs, e.g., CR4.FRED. To support FRED context management, new VMCS fields corresponding to most of FRED CPU state MSRs are added to both the host-state and guest-state areas of VMCS.
Specifically no VMCS fields are added for FRED RSP0 and SSP0 MSRs, because the 2 FRED MSRs are used during ring 3 event delivery only, thus KVM, running on ring 0, can run safely even with guest FRED RSP0 and SSP0. It can be deferred to load host FRED RSP0 and SSP0 until before returning to user level.
--- arch/x86/include/asm/vmx.h | 16 ++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+)
arch/x86/include/asm/vmx.h +16 -0
@@ -277,12 +277,28 @@ enum vmcs_field {GUEST_BNDCFGS_HIGH =0x00002813 ,GUEST_IA32_RTIT_CTL =0x00002814 ,GUEST_IA32_RTIT_CTL_HIGH =0x00002815 , +GUEST_IA32_FRED_CONFIG =0x0000281a , +GUEST_IA32_FRED_RSP1 =0x0000281c , +GUEST_IA32_FRED_RSP2 =0x0000281e , +GUEST_IA32_FRED_RSP3 =0x00002820 , +GUEST_IA32_FRED_STKLVLS =0x00002822 , +GUEST_IA32_FRED_SSP1 =0x00002824 , +GUEST_IA32_FRED_SSP2 =0x00002826 , +GUEST_IA32_FRED_SSP3 =0x00002828 ,HOST_IA32_PAT =0x00002c00 ,HOST_IA32_PAT_HIGH =0x00002c01 ,HOST_IA32_EFER =0x00002c02 ,HOST_IA32_EFER_HIGH =0x00002c03 ,HOST_IA32_PERF_GLOBAL_CTRL =0x00002c04 ,HOST_IA32_PERF_GLOBAL_CTRL_HIGH =0x00002c05 , +HOST_IA32_FRED_CONFIG =0x00002c08 , +HOST_IA32_FRED_RSP1 =0x00002c0a , +HOST_IA32_FRED_RSP2 =0x00002c0c , +HOST_IA32_FRED_RSP3 =0x00002c0e , +HOST_IA32_FRED_STKLVLS =0x00002c10 , +HOST_IA32_FRED_SSP1 =0x00002c12 , +HOST_IA32_FRED_SSP2 =0x00002c14 , +HOST_IA32_FRED_SSP3 =0x00002c16 ,PIN_BASED_VM_EXEC_CONTROL =0x00004000 ,CPU_BASED_VM_EXEC_CONTROL =0x00004002 , EXCEPTION_BITMAP = 0x00004004,
arch/x86/kvm/vmx/vmx.c +32 -0
@@ -1477,6 +1477,18 @@ void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu, (unsigned long)(cpu_entry_stack(cpu) + 1)); } +#ifdef CONFIG_X86_64 +/* Per-CPU FRED MSRs */ +if (cpu_feature_enabled (X86_FEATURE_FRED )) { +vmcs_write64 (HOST_IA32_FRED_RSP1 ,read_msr (MSR_IA32_FRED_RSP1 )); +vmcs_write64 (HOST_IA32_FRED_RSP2 ,read_msr (MSR_IA32_FRED_RSP2 )); +vmcs_write64 (HOST_IA32_FRED_RSP3 ,read_msr (MSR_IA32_FRED_RSP3 )); +vmcs_write64 (HOST_IA32_FRED_SSP1 ,read_msr (MSR_IA32_FRED_SSP1 )); +vmcs_write64 (HOST_IA32_FRED_SSP2 ,read_msr (MSR_IA32_FRED_SSP2 )); +vmcs_write64 (HOST_IA32_FRED_SSP3 ,read_msr (MSR_IA32_FRED_SSP3 )); + } +#endif + vmx->loaded_vmcs->cpu = cpu; } }
@@ -4375,6 +4387,15 @@ void vmx_set_constant_host_state(struct vcpu_vmx *vmx)if (cpu_has_load_ia32_efer ())vmcs_write64 (HOST_IA32_EFER , host_efer); + +/* +* FRED MSRs are per-cpu, however FRED CONFIG and STKLVLS MSRs +* are the same on all CPUs, thus they are initialized here. +*/ +if (cpu_feature_enabled (X86_FEATURE_FRED )) { +vmcs_write64 (HOST_IA32_FRED_CONFIG ,read_msr (MSR_IA32_FRED_CONFIG )); +vmcs_write64 (HOST_IA32_FRED_STKLVLS ,read_msr (MSR_IA32_FRED_STKLVLS )); + } }void set_cr4_guest_host_mask (struct vcpu_vmx *vmx)
@@ -4936,6 +4957,17 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)vmcs_writel (GUEST_IDTR_BASE ,0 );vmcs_write32 (GUEST_IDTR_LIMIT ,0xffff ); +if (cpu_feature_enabled (X86_FEATURE_FRED )) { +vmcs_write64 (GUEST_IA32_FRED_CONFIG ,0 ); +vmcs_write64 (GUEST_IA32_FRED_RSP1 ,0 ); +vmcs_write64 (GUEST_IA32_FRED_RSP2 ,0 ); +vmcs_write64 (GUEST_IA32_FRED_RSP3 ,0 ); +vmcs_write64 (GUEST_IA32_FRED_STKLVLS ,0 ); +vmcs_write64 (GUEST_IA32_FRED_SSP1 ,0 ); +vmcs_write64 (GUEST_IA32_FRED_SSP2 ,0 ); +vmcs_write64 (GUEST_IA32_FRED_SSP3 ,0 ); + } +vmcs_write32 (GUEST_ACTIVITY_STATE ,GUEST_ACTIVITY_ACTIVE );vmcs_write32 (GUEST_INTERRUPTIBILITY_INFO ,0 );vmcs_writel (GUEST_PENDING_DBG_EXCEPTIONS ,0 );
Signature · 1 line
2.42.0
PATCH 9/23 KVM: VMX: Switch FRED RSP0 between host and guest
In reply to Xin LiSwitch MSR_IA32_FRED_RSP0 between host and guest in vmx_prepare_switch_to_{host,guest}().
MSR_IA32_FRED_RSP0 is used during ring 3 event delivery only, thus KVM, running on ring 0, can run safely with guest FRED RSP0, i.e., no need to switch between host/guest FRED RSP0 during VM entry and exit.
KVM should switch to host FRED RSP0 before returning to user level, and switch to guest FRED RSP0 before entering guest mode.
--- arch/x86/kvm/vmx/vmx.c | 17 +++++++++++++++++ arch/x86/kvm/vmx/vmx.h | 2 ++ 2 files changed, 19 insertions(+)
arch/x86/kvm/vmx/vmx.c +17 -0
@@ -1344,6 +1344,17 @@ void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu) }wrmsrl (MSR_KERNEL_GS_BASE , vmx->msr_guest_kernel_gs_base); + +if (cpu_feature_enabled (X86_FEATURE_FRED ) && +guest_cpuid_has (vcpu,X86_FEATURE_FRED )) { +/* +* MSR_IA32_FRED_RSP0 is top of task stack, which never changes. +* Thus it should be initialized only once. +*/ +if (unlikely (vmx->msr_host_fred_rsp0 ==0 )) + vmx->msr_host_fred_rsp0 =read_msr (MSR_IA32_FRED_RSP0 ); +wrmsrl (MSR_IA32_FRED_RSP0 , vmx->msr_guest_fred_rsp0); + }#else savesegment (fs, fs_sel);savesegment (gs, gs_sel);
@@ -1388,6 +1399,12 @@ static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)invalidate_tss_limit ();#ifdef CONFIG_X86_64 wrmsrl (MSR_KERNEL_GS_BASE , vmx->msr_host_kernel_gs_base); + +if (cpu_feature_enabled (X86_FEATURE_FRED ) && +guest_cpuid_has (&vmx->vcpu,X86_FEATURE_FRED )) { + vmx->msr_guest_fred_rsp0 =read_msr (MSR_IA32_FRED_RSP0 ); +wrmsrl (MSR_IA32_FRED_RSP0 , vmx->msr_host_fred_rsp0); + }#endif load_fixmap_gdt (raw_smp_processor_id ()); vmx->guest_state_loaded = false;
arch/x86/kvm/vmx/vmx.h +2 -0
@@ -276,6 +276,8 @@ struct vcpu_vmx {#ifdef CONFIG_X86_64 u64 msr_host_kernel_gs_base;u64 msr_guest_kernel_gs_base; +u64 msr_host_fred_rsp0; +u64 msr_guest_fred_rsp0;#endif u64 spec_ctrl;
Signature · 1 line
2.42.0
PATCH 10/23 KVM: VMX: Add support for FRED context save/restore
In reply to Xin LiHandle host initiated FRED MSR access requests to allow FRED context to be set/get from user level.
During VM save/restore and live migration, FRED context needs to be saved/restored, which requires FRED MSRs to be accessed from a user level application, e.g., Qemu.
Note, handling of MSR_IA32_FRED_SSP0, i.e., MSR_IA32_PL0_SSP, is not added yet, which needs to be aligned with KVM CET patch set.
--- arch/x86/kvm/vmx/vmx.c | 72 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 23 ++++++++++++++ 2 files changed, 95 insertions(+)
arch/x86/kvm/vmx/vmx.c +72 -0
@@ -1429,6 +1429,24 @@ static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)preempt_enable (); vmx->msr_guest_kernel_gs_base = data; } + +static u64 vmx_read_guest_fred_rsp0 (struct vcpu_vmx *vmx) +{ +preempt_disable (); +if (vmx->guest_state_loaded) + vmx->msr_guest_fred_rsp0 =read_msr (MSR_IA32_FRED_RSP0 ); +preempt_enable (); +return vmx->msr_guest_fred_rsp0; +} + +static void vmx_write_guest_fred_rsp0 (struct vcpu_vmx *vmx,u64 data) +{ +preempt_disable (); +if (vmx->guest_state_loaded) +wrmsrl (MSR_IA32_FRED_RSP0 , data); +preempt_enable (); + vmx->msr_guest_fred_rsp0 = data; +}#endif void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu,
@@ -2028,6 +2046,33 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_KERNEL_GS_BASE : msr_info->data =vmx_read_guest_kernel_gs_base (vmx);break ; +case MSR_IA32_FRED_RSP0 : + msr_info->data =vmx_read_guest_fred_rsp0 (vmx); +break ; +case MSR_IA32_FRED_RSP1 : + msr_info->data =vmcs_read64 (GUEST_IA32_FRED_RSP1 ); +break ; +case MSR_IA32_FRED_RSP2 : + msr_info->data =vmcs_read64 (GUEST_IA32_FRED_RSP2 ); +break ; +case MSR_IA32_FRED_RSP3 : + msr_info->data =vmcs_read64 (GUEST_IA32_FRED_RSP3 ); +break ; +case MSR_IA32_FRED_STKLVLS : + msr_info->data =vmcs_read64 (GUEST_IA32_FRED_STKLVLS ); +break ; +case MSR_IA32_FRED_SSP1 : + msr_info->data =vmcs_read64 (GUEST_IA32_FRED_SSP1 ); +break ; +case MSR_IA32_FRED_SSP2 : + msr_info->data =vmcs_read64 (GUEST_IA32_FRED_SSP2 ); +break ; +case MSR_IA32_FRED_SSP3 : + msr_info->data =vmcs_read64 (GUEST_IA32_FRED_SSP3 ); +break ; +case MSR_IA32_FRED_CONFIG : + msr_info->data =vmcs_read64 (GUEST_IA32_FRED_CONFIG ); +break ;#endif case MSR_EFER :return kvm_get_msr_common (vcpu, msr_info);
@@ -2233,6 +2278,33 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) vmx_update_exception_bitmap(vcpu); }break ; +case MSR_IA32_FRED_RSP0 : +vmx_write_guest_fred_rsp0 (vmx, data); +break ; +case MSR_IA32_FRED_RSP1 : +vmcs_write64 (GUEST_IA32_FRED_RSP1 , data); +break ; +case MSR_IA32_FRED_RSP2 : +vmcs_write64 (GUEST_IA32_FRED_RSP2 , data); +break ; +case MSR_IA32_FRED_RSP3 : +vmcs_write64 (GUEST_IA32_FRED_RSP3 , data); +break ; +case MSR_IA32_FRED_STKLVLS : +vmcs_write64 (GUEST_IA32_FRED_STKLVLS , data); +break ; +case MSR_IA32_FRED_SSP1 : +vmcs_write64 (GUEST_IA32_FRED_SSP1 , data); +break ; +case MSR_IA32_FRED_SSP2 : +vmcs_write64 (GUEST_IA32_FRED_SSP2 , data); +break ; +case MSR_IA32_FRED_SSP3 : +vmcs_write64 (GUEST_IA32_FRED_SSP3 , data); +break ; +case MSR_IA32_FRED_CONFIG : +vmcs_write64 (GUEST_IA32_FRED_CONFIG , data); +break ;#endif case MSR_IA32_SYSENTER_CS :if (is_guest_mode (vcpu))
arch/x86/kvm/x86.c +23 -0
@@ -1451,6 +1451,9 @@ static const u32 msrs_to_save_base[] = { MSR_STAR,#ifdef CONFIG_X86_64 MSR_CSTAR ,MSR_KERNEL_GS_BASE ,MSR_SYSCALL_MASK ,MSR_LSTAR , +MSR_IA32_FRED_RSP0 ,MSR_IA32_FRED_RSP1 ,MSR_IA32_FRED_RSP2 , +MSR_IA32_FRED_RSP3 ,MSR_IA32_FRED_STKLVLS ,MSR_IA32_FRED_SSP1 , +MSR_IA32_FRED_SSP2 ,MSR_IA32_FRED_SSP3 ,MSR_IA32_FRED_CONFIG , #endifMSR_IA32_TSC ,MSR_IA32_CR_PAT ,MSR_VM_HSAVE_PA , MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
@@ -1890,6 +1893,16 @@ static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data, data = (u32 )data;break ; + case MSR_IA32_FRED_RSP0 ... MSR_IA32_FRED_CONFIG: +if (host_initiated ||guest_cpuid_has (vcpu,X86_FEATURE_FRED )) +break ; + +/* +* Inject #GP upon FRED MSRs accesses from a non-FRED guest to +* make sure no malicious guest can write to FRED MSRs thus to +* corrupt host FRED MSRs. +*/ +return 1 ; } msr.data = data;
@@ -1933,6 +1946,16 @@ int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data, !guest_cpuid_has(vcpu, X86_FEATURE_RDPID))return 1 ;break ; + case MSR_IA32_FRED_RSP0 ... MSR_IA32_FRED_CONFIG: +if (host_initiated ||guest_cpuid_has (vcpu,X86_FEATURE_FRED )) +break ; + +/* +* Inject #GP upon FRED MSRs accesses from a non-FRED guest to +* make sure no malicious guest can write to FRED MSRs thus to +* corrupt host FRED MSRs. +*/ +return 1 ; } msr.index = index;
Signature · 1 line
2.42.0
PATCH 11/23 KVM: x86: Add kvm_is_fred_enabled()
In reply to Xin LiAdd kvm_is_fred_enabled() to get if FRED is enabled on a vCPU.
--- arch/x86/kvm/kvm_cache_regs.h | 10 ++++++++++ 1 file changed, 10 insertions(+)
arch/x86/kvm/kvm_cache_regs.h +10 -0
@@ -187,6 +187,16 @@ static __always_inline bool kvm_is_cr4_bit_set(struct kvm_vcpu *vcpu,return !!kvm_read_cr4_bits (vcpu, cr4_bit); } +static __always_inline bool kvm_is_fred_enabled(struct kvm_vcpu *vcpu) +{ +#ifdef CONFIG_X86_64 +return cpu_feature_enabled (X86_FEATURE_FRED ) && +kvm_is_cr4_bit_set (vcpu,X86_CR4_FRED ); +#else +return false; +#endif +} +static inline ulong kvm_read_cr3 (struct kvm_vcpu *vcpu) {if (!kvm_register_is_available (vcpu,VCPU_EXREG_CR3 ))
Signature · 1 line
2.42.0
PATCH 12/23 KVM: VMX: Handle FRED event data
In reply to Xin LiSet injected-event data when injecting a #PF, #DB, or #NM caused by extended feature disable using FRED event delivery, and save original-event data for being used as injected-event data.
Unlike IDT using some extra CPU register as part of an event context, e.g., %cr2 for #PF, FRED saves a complete event context in its stack frame, e.g., FRED saves the faulting linear address of a #PF into the event data field defined in its stack frame.
Thus a new VMX control field called injected-event data is added to provide the event data that will be pushed into a FRED stack frame for VM entries that inject an event using FRED event delivery. In addition, a new VM exit information field called original-event data is added to store the event data that would have saved into a FRED stack frame for VM exits that occur during FRED event delivery. After such a VM exit is handled to allow the original-event to be delivered, the data in the original-event data VMCS field needs to be set into the injected-event data VMCS field for the injection of the original event.
--- arch/x86/include/asm/vmx.h | 4 ++ arch/x86/kvm/vmx/vmx.c | 84 +++++++++++++++++++++++++++++++++++--- arch/x86/kvm/x86.c | 10 ++++- 3 files changed, 91 insertions(+), 7 deletions(-)
arch/x86/include/asm/vmx.h +4 -0
@@ -253,8 +253,12 @@ enum vmcs_field {PID_POINTER_TABLE_HIGH =0x00002043 ,SECONDARY_VM_EXIT_CONTROLS =0x00002044 ,SECONDARY_VM_EXIT_CONTROLS_HIGH =0x00002045 , +INJECTED_EVENT_DATA =0x00002052 , +INJECTED_EVENT_DATA_HIGH =0x00002053 ,GUEST_PHYSICAL_ADDRESS =0x00002400 ,GUEST_PHYSICAL_ADDRESS_HIGH =0x00002401 , +ORIGINAL_EVENT_DATA =0x00002404 , +ORIGINAL_EVENT_DATA_HIGH =0x00002405 ,VMCS_LINK_POINTER =0x00002800 ,VMCS_LINK_POINTER_HIGH =0x00002801 , GUEST_IA32_DEBUGCTL = 0x00002802,
arch/x86/kvm/vmx/vmx.c +79 -5
123 lines not on this page · raw message
arch/x86/kvm/x86.c +8 -2
@@ -680,8 +680,14 @@ static void kvm_multiple_exception(struct kvm_vcpu *vcpu, vcpu->arch.exception.injected = true;if (WARN_ON_ONCE (has_payload)) {/* -* A reinjected event has already -* delivered its payload. +* For a reinjected event, KVM delivers its +* payload through: +* #PF: save %cr2 into arch.cr2 immediately +* after VM exits. +* #DB: save %dr6 into arch.dr6 later in +* sync_dirty_debug_regs(). +* +* For FRED guest, see __vmx_complete_interrupts(). */ has_payload = false; payload =0 ;
Signature · 1 line
2.42.0
PATCH 13/23 KVM: VMX: Handle VMX nested exception for FRED
In reply to Xin LiSet VMX nested exception bit in the VM-entry interruption information VMCS field when injecting a nested exception using FRED event delivery to ensure:
1) The nested exception is injected on a correct stack level.
2) The nested bit defined in FRED stack frame is set.
The event stack level used by FRED event delivery depends on whether the event was a nested exception encountered during delivery of another event, because a nested exception is "regarded" as happening on ring 0. E.g., when #PF is configured to use stack level 1 in IA32_FRED_STKLVLS MSR:
- nested #PF will be delivered on stack level 1 when triggered from user level.
- normal #PF will be delivered on stack level 0 when triggered from user level.
The VMX nested-exception support ensures the correct event stack level is chosen when a VM entry injects a nested exception.
--- arch/x86/include/asm/kvm_host.h | 6 ++++-- arch/x86/include/asm/vmx.h | 4 +++- arch/x86/kvm/svm/svm.c | 4 ++-- arch/x86/kvm/vmx/vmx.c | 26 +++++++++++++++++++++----- arch/x86/kvm/x86.c | 22 +++++++++++++--------- arch/x86/kvm/x86.h | 1 + 6 files changed, 44 insertions(+), 19 deletions(-)
arch/x86/include/asm/kvm_host.h +4 -2
@@ -721,6 +721,7 @@ struct kvm_queued_exception {u32 error_code;unsigned long payload;bool has_payload; +bool nested; };struct kvm_vcpu_arch {
@@ -2015,8 +2016,9 @@ int kvm_emulate_rdpmc(struct kvm_vcpu *vcpu);void kvm_queue_exception (struct kvm_vcpu *vcpu,unsigned nr);void kvm_queue_exception_e (struct kvm_vcpu *vcpu,unsigned nr,u32 error_code);void kvm_queue_exception_p (struct kvm_vcpu *vcpu,unsigned nr,unsigned long payload); -void kvm_requeue_exception (struct kvm_vcpu *vcpu,unsigned nr); -void kvm_requeue_exception_e (struct kvm_vcpu *vcpu,unsigned nr,u32 error_code); +void kvm_requeue_exception (struct kvm_vcpu *vcpu,unsigned nr,bool nested); +void kvm_requeue_exception_e (struct kvm_vcpu *vcpu,unsigned nr, +u32 error_code,bool nested);void kvm_inject_page_fault (struct kvm_vcpu *vcpu,struct x86_exception *fault);void kvm_inject_emulated_page_fault (struct kvm_vcpu *vcpu,struct x86_exception *fault);
arch/x86/include/asm/vmx.h +3 -1
@@ -132,6 +132,7 @@/* VMX_BASIC bits and bitmasks */ #define VMX_BASIC_32BIT_PHYS_ADDR_ONLY BIT_ULL(48)#define VMX_BASIC_INOUT BIT_ULL(54) +#define VMX_BASIC_NESTED_EXCEPTION BIT_ULL(58)/* VMX_MISC bits and bitmasks */ #define VMX_MISC_INTEL_PT BIT_ULL(14)
@@ -404,8 +405,9 @@ enum vmcs_field {#define INTR_INFO_INTR_TYPE_MASK 0x700/* 10:8 */ #define INTR_INFO_DELIVER_CODE_MASK 0x800/* 11 */ #define INTR_INFO_UNBLOCK_NMI 0x1000/* 12 */ +#define INTR_INFO_NESTED_EXCEPTION_MASK 0x2000/* 13 */ #define INTR_INFO_VALID_MASK 0x80000000/* 31 */ -#define INTR_INFO_RESVD_BITS_MASK 0x7ffff000 +#define INTR_INFO_RESVD_BITS_MASK 0x7fffd000#define VECTORING_INFO_VECTOR_MASK INTR_INFO_VECTOR_MASK#define VECTORING_INFO_TYPE_MASK INTR_INFO_INTR_TYPE_MASK
arch/x86/kvm/svm/svm.c +2 -2
@@ -4047,10 +4047,10 @@ static void svm_complete_interrupts(struct kvm_vcpu *vcpu)if (exitintinfo &SVM_EXITINTINFO_VALID_ERR ) {u32 err = svm->vmcb->control.exit_int_info_err; -kvm_requeue_exception_e (vcpu, vector, err); +kvm_requeue_exception_e (vcpu, vector, err, false); }else -kvm_requeue_exception (vcpu, vector); +kvm_requeue_exception (vcpu, vector, false);break ;case SVM_EXITINTINFO_TYPE_INTR :kvm_queue_interrupt (vcpu, vector, false);
arch/x86/kvm/vmx/vmx.c +21 -5
@@ -1901,6 +1901,8 @@ static void vmx_inject_exception(struct kvm_vcpu *vcpu) event_data = vcpu->arch.guest_fpu.xfd_err;vmcs_write64 (INJECTED_EVENT_DATA , event_data); + + intr_info |= ex->nested ?INTR_INFO_NESTED_EXCEPTION_MASK :0 ; } }
@@ -2851,6 +2853,19 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf,/* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ if (basic_msr &VMX_BASIC_32BIT_PHYS_ADDR_ONLY )return -EIO ; + +/* +* FRED draft Spec 5.0 Section 9.2: +* +* Any processor that enumerates support for FRED transitions +* will also enumerate VMX nested-exception support. +*/ +if (cpu_feature_enabled (X86_FEATURE_FRED ) && + !(basic_msr &VMX_BASIC_NESTED_EXCEPTION )) { +pr_warn_once ("FRED enabled but no VMX nested-exception support\n" ); +if (error_on_inconsistent_vmcs_config) +return -EIO ; + }#endif /* Require Write-Back (WB) memory type for VMCS accesses. */
@@ -7313,11 +7328,12 @@ static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, } } -if (idt_vectoring_info &VECTORING_INFO_DELIVER_CODE_MASK ) { -u32 err =vmcs_read32 (error_code_field); -kvm_requeue_exception_e (vcpu, vector, err); - }else -kvm_requeue_exception (vcpu, vector); +if (idt_vectoring_info &VECTORING_INFO_DELIVER_CODE_MASK ) +kvm_requeue_exception_e (vcpu, vector,vmcs_read32 (error_code_field), + idt_vectoring_info &INTR_INFO_NESTED_EXCEPTION_MASK ); +else +kvm_requeue_exception (vcpu, vector, + idt_vectoring_info &INTR_INFO_NESTED_EXCEPTION_MASK );break ;case INTR_TYPE_SOFT_INTR : vcpu->arch.event_exit_inst_len =vmcs_read32 (instr_len_field);
arch/x86/kvm/x86.c +13 -9
@@ -645,7 +645,8 @@ static void kvm_leave_nested(struct kvm_vcpu *vcpu)static void kvm_multiple_exception (struct kvm_vcpu *vcpu,unsigned nr,bool has_error,u32 error_code, -bool has_payload,unsigned long payload,bool reinject) +bool has_payload,unsigned long payload, +bool reinject,bool nested) {u32 prev_nr;int class1, class2;
@@ -678,6 +679,7 @@ static void kvm_multiple_exception(struct kvm_vcpu *vcpu,*/ WARN_ON_ONCE (kvm_is_exception_pending (vcpu)); vcpu->arch.exception.injected = true; + vcpu->arch.exception.nested = nested;if (WARN_ON_ONCE (has_payload)) {/* * For a reinjected event, KVM delivers its
@@ -727,6 +729,8 @@ static void kvm_multiple_exception(struct kvm_vcpu *vcpu,kvm_queue_exception_e (vcpu,DF_VECTOR ,0 ); }else { + vcpu->arch.exception.nested = true; +/* replace previous exception with a new one in a hope that instruction re-execution will regenerate lost exception */
@@ -736,20 +740,20 @@ static void kvm_multiple_exception(struct kvm_vcpu *vcpu,void kvm_queue_exception (struct kvm_vcpu *vcpu,unsigned nr) { -kvm_multiple_exception (vcpu, nr, false,0 , false,0 , false); +kvm_multiple_exception (vcpu, nr, false,0 , false,0 , false, false); }EXPORT_SYMBOL_GPL (kvm_queue_exception); -void kvm_requeue_exception (struct kvm_vcpu *vcpu,unsigned nr) +void kvm_requeue_exception (struct kvm_vcpu *vcpu,unsigned nr,bool nested) { -kvm_multiple_exception (vcpu, nr, false,0 , false,0 , true); +kvm_multiple_exception (vcpu, nr, false,0 , false,0 , true, nested); }EXPORT_SYMBOL_GPL (kvm_requeue_exception);void kvm_queue_exception_p (struct kvm_vcpu *vcpu,unsigned nr,unsigned long payload) { -kvm_multiple_exception (vcpu, nr, false,0 , true, payload, false); +kvm_multiple_exception (vcpu, nr, false,0 , true, payload, false, false); }EXPORT_SYMBOL_GPL (kvm_queue_exception_p);
@@ -757,7 +761,7 @@ static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code, unsigned long payload) {kvm_multiple_exception (vcpu ,nr ,true ,error_code , -true ,payload ,false ); +true ,payload ,false ,false ); }int kvm_complete_insn_gp (struct kvm_vcpu *vcpu,int err)
@@ -829,13 +833,13 @@ void kvm_inject_nmi(struct kvm_vcpu *vcpu)void kvm_queue_exception_e (struct kvm_vcpu *vcpu,unsigned nr,u32 error_code) { -kvm_multiple_exception (vcpu, nr, true, error_code, false,0 , false); +kvm_multiple_exception (vcpu, nr, true, error_code, false,0 , false, false); }EXPORT_SYMBOL_GPL (kvm_queue_exception_e); -void kvm_requeue_exception_e (struct kvm_vcpu *vcpu,unsigned nr,u32 error_code) +void kvm_requeue_exception_e (struct kvm_vcpu *vcpu,unsigned nr,u32 error_code,bool nested) { -kvm_multiple_exception (vcpu, nr, true, error_code, false,0 , true); +kvm_multiple_exception (vcpu, nr, true, error_code, false,0 , true, nested); }EXPORT_SYMBOL_GPL (kvm_requeue_exception_e);
arch/x86/kvm/x86.h +1 -0
@@ -108,6 +108,7 @@ static inline void kvm_clear_exception_queue(struct kvm_vcpu *vcpu) { vcpu->arch.exception.pending = false; vcpu->arch.exception.injected = false; + vcpu->arch.exception.nested = false; vcpu->arch.exception_vmexit.pending = false; }
Signature · 1 line
2.42.0
PATCH 14/23 KVM: VMX: Dump FRED context in dump_vmcs()
In reply to Xin LiAdd FRED related VMCS fields to dump_vmcs() to have it dump FRED context.
--- arch/x86/kvm/vmx/vmx.c | 48 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 7 deletions(-)
arch/x86/kvm/vmx/vmx.c +41 -7
@@ -6429,7 +6429,7 @@ void dump_vmcs(struct kvm_vcpu *vcpu)struct vcpu_vmx *vmx =to_vmx (vcpu);u32 vmentry_ctl, vmexit_ctl;u32 cpu_based_exec_ctrl, pin_based_exec_ctrl, secondary_exec_control; -u64 tertiary_exec_control; +u64 tertiary_exec_control, secondary_vmexit_ctl;unsigned long cr4;int efer_slot;
@@ -6440,6 +6440,8 @@ void dump_vmcs(struct kvm_vcpu *vcpu) vmentry_ctl =vmcs_read32 (VM_ENTRY_CONTROLS ); vmexit_ctl =vmcs_read32 (VM_EXIT_CONTROLS ); + secondary_vmexit_ctl =cpu_has_secondary_vmexit_ctrls () ? +vmcs_read64 (SECONDARY_VM_EXIT_CONTROLS ) :0 ; cpu_based_exec_ctrl =vmcs_read32 (CPU_BASED_VM_EXEC_CONTROL ); pin_based_exec_ctrl =vmcs_read32 (PIN_BASED_VM_EXEC_CONTROL ); cr4 =vmcs_readl (GUEST_CR4 );
@@ -6486,6 +6488,19 @@ void dump_vmcs(struct kvm_vcpu *vcpu)vmx_dump_sel ("LDTR:" ,GUEST_LDTR_SELECTOR );vmx_dump_dtsel ("IDTR:" ,GUEST_IDTR_LIMIT );vmx_dump_sel ("TR: " ,GUEST_TR_SELECTOR ); +#ifdef CONFIG_X86_64 +if (cpu_feature_enabled (X86_FEATURE_FRED )) { +pr_err ("FRED guest: config=0x%016llx, stack levels=0x%016llx\n" +"RSP0=0x%016lx, RSP1=0x%016llx\n" +"RSP2=0x%016llx, RSP3=0x%016llx\n" , +vmcs_read64 (GUEST_IA32_FRED_CONFIG ), +vmcs_read64 (GUEST_IA32_FRED_STKLVLS ), +read_msr (MSR_IA32_FRED_RSP0 ), +vmcs_read64 (GUEST_IA32_FRED_RSP1 ), +vmcs_read64 (GUEST_IA32_FRED_RSP2 ), +vmcs_read64 (GUEST_IA32_FRED_RSP3 )); + } +#endif efer_slot =vmx_find_loadstore_msr_slot (&vmx->msr_autoload.guest,MSR_EFER );if (vmentry_ctl &VM_ENTRY_LOAD_IA32_EFER )pr_err ("EFER= 0x%016llx\n" ,vmcs_read64 (GUEST_IA32_EFER ));
@@ -6533,6 +6548,19 @@ void dump_vmcs(struct kvm_vcpu *vcpu) vmcs_readl(HOST_TR_BASE));pr_err ("GDTBase=%016lx IDTBase=%016lx\n" ,vmcs_readl (HOST_GDTR_BASE ),vmcs_readl (HOST_IDTR_BASE )); +#ifdef CONFIG_X86_64 +if (cpu_feature_enabled (X86_FEATURE_FRED )) { +pr_err ("FRED host: config=0x%016llx, stack levels=0x%016llx\n" +"RSP0=0x%016llx, RSP1=0x%016llx\n" +"RSP2=0x%016llx, RSP3=0x%016llx\n" , +vmcs_read64 (HOST_IA32_FRED_CONFIG ), +vmcs_read64 (HOST_IA32_FRED_STKLVLS ), + vmx->msr_host_fred_rsp0, +vmcs_read64 (HOST_IA32_FRED_RSP1 ), +vmcs_read64 (HOST_IA32_FRED_RSP2 ), +vmcs_read64 (HOST_IA32_FRED_RSP3 )); + } +#endif pr_err ("CR0=%016lx CR3=%016lx CR4=%016lx\n" ,vmcs_readl (HOST_CR0 ),vmcs_readl (HOST_CR3 ),vmcs_readl (HOST_CR4 ));
@@ -6554,25 +6582,31 @@ void dump_vmcs(struct kvm_vcpu *vcpu)pr_err ("*** Control State ***\n" );pr_err ("CPUBased=0x%08x SecondaryExec=0x%08x TertiaryExec=0x%016llx\n" , cpu_based_exec_ctrl, secondary_exec_control, tertiary_exec_control); -pr_err ("PinBased=0x%08x EntryControls=%08x ExitControls=%08x\n" , - pin_based_exec_ctrl, vmentry_ctl, vmexit_ctl); +pr_err ("PinBased=0x%08x EntryControls=0x%08x\n" , + pin_based_exec_ctrl, vmentry_ctl); +pr_err ("ExitControls=0x%08x SecondaryExitControls=0x%016llx\n" , + vmexit_ctl, secondary_vmexit_ctl);pr_err ("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n" ,vmcs_read32 (EXCEPTION_BITMAP ),vmcs_read32 (PAGE_FAULT_ERROR_CODE_MASK ),vmcs_read32 (PAGE_FAULT_ERROR_CODE_MATCH )); -pr_err ("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n" , +pr_err ("VMEntry: intr_info=%08x errcode=%08x ilen=%08x event data=%016llx\n" ,vmcs_read32 (VM_ENTRY_INTR_INFO_FIELD ),vmcs_read32 (VM_ENTRY_EXCEPTION_ERROR_CODE ), -vmcs_read32 (VM_ENTRY_INSTRUCTION_LEN )); +vmcs_read32 (VM_ENTRY_INSTRUCTION_LEN ), +cpu_feature_enabled (X86_FEATURE_FRED ) ? +vmcs_read64 (INJECTED_EVENT_DATA ) :0 );pr_err ("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n" ,vmcs_read32 (VM_EXIT_INTR_INFO ),vmcs_read32 (VM_EXIT_INTR_ERROR_CODE ),vmcs_read32 (VM_EXIT_INSTRUCTION_LEN ));pr_err (" reason=%08x qualification=%016lx\n" ,vmcs_read32 (VM_EXIT_REASON ),vmcs_readl (EXIT_QUALIFICATION )); -pr_err ("IDTVectoring: info=%08x errcode=%08x\n" , +pr_err ("IDTVectoring: info=%08x errcode=%08x event data=%016llx\n" ,vmcs_read32 (IDT_VECTORING_INFO_FIELD ), -vmcs_read32 (IDT_VECTORING_ERROR_CODE )); +vmcs_read32 (IDT_VECTORING_ERROR_CODE ), +cpu_feature_enabled (X86_FEATURE_FRED ) ? +vmcs_read64 (ORIGINAL_EVENT_DATA ) :0 );pr_err ("TSC Offset = 0x%016llx\n" ,vmcs_read64 (TSC_OFFSET ));if (secondary_exec_control &SECONDARY_EXEC_TSC_SCALING ) pr_err("TSC Multiplier = 0x%016llx\n",
Signature · 1 line
2.42.0
PATCH 15/23 KVM: nVMX: Add support for the secondary VM exit controls
In reply to Xin LiEnable the secondary VM exit controls to prepare for nested FRED.
--- Documentation/virt/kvm/x86/nested-vmx.rst | 1 + arch/x86/include/asm/hyperv-tlfs.h | 1 + arch/x86/kvm/vmx/capabilities.h | 1 + arch/x86/kvm/vmx/hyperv.c | 18 +++++++++++++++++- arch/x86/kvm/vmx/nested.c | 18 +++++++++++++++++- arch/x86/kvm/vmx/vmcs12.c | 1 + arch/x86/kvm/vmx/vmcs12.h | 2 ++ arch/x86/kvm/x86.h | 2 +- 8 files changed, 41 insertions(+), 3 deletions(-)
Documentation/virt/kvm/x86/nested-vmx.rst +1 -0
@@ -217,6 +217,7 @@ struct shadow_vmcs is ever changed. u16 host_fs_selector; u16 host_gs_selector; u16 host_tr_selector; + u64 secondary_vm_exit_controls; };
arch/x86/include/asm/hyperv-tlfs.h +1 -0
@@ -616,6 +616,7 @@ struct hv_enlightened_vmcs {u64 host_ssp;u64 host_ia32_int_ssp_table_addr;u64 padding64_6; +u64 secondary_vm_exit_controls; } __packed;#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE 0
arch/x86/kvm/vmx/capabilities.h +1 -0
@@ -38,6 +38,7 @@ struct nested_vmx_msrs {u32 pinbased_ctls_high;u32 exit_ctls_low;u32 exit_ctls_high; +u64 secondary_exit_ctls;u32 entry_ctls_low;u32 entry_ctls_high;u32 misc_low;
arch/x86/kvm/vmx/hyperv.c +17 -1
@@ -103,7 +103,10 @@ VM_EXIT_LOAD_IA32_EFER | \ VM_EXIT_CLEAR_BNDCFGS | \ VM_EXIT_PT_CONCEAL_PIP | \ - VM_EXIT_CLEAR_IA32_RTIT_CTL) + VM_EXIT_CLEAR_IA32_RTIT_CTL | \ + VM_EXIT_ACTIVATE_SECONDARY_CONTROLS) + +#define EVMCS1_SUPPORTED_VMEXIT_CTRL2 (0ULL)#define EVMCS1_SUPPORTED_VMENTRY_CTRL \ (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | \
@@ -315,6 +318,8 @@ const struct evmcs_field vmcs_field_to_evmcs_1[] = { HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1),EVMCS1_FIELD (VM_EXIT_CONTROLS , vm_exit_controls,HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1 ), +EVMCS1_FIELD (SECONDARY_VM_EXIT_CONTROLS , secondary_vm_exit_controls, +HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1 ),EVMCS1_FIELD (SECONDARY_VM_EXEC_CONTROL , secondary_vm_exec_control,HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1 ), EVMCS1_FIELD(GUEST_ES_LIMIT, guest_es_limit,
@@ -464,6 +469,7 @@ enum evmcs_revision {enum evmcs_ctrl_type {EVMCS_EXIT_CTRLS , +EVMCS_2NDEXIT ,EVMCS_ENTRY_CTRLS ,EVMCS_EXEC_CTRL ,EVMCS_2NDEXEC ,
@@ -477,6 +483,9 @@ static const u32 evmcs_supported_ctrls[NR_EVMCS_CTRLS][NR_EVMCS_REVISIONS] = { [EVMCS_EXIT_CTRLS] = { [EVMCSv1_LEGACY] = EVMCS1_SUPPORTED_VMEXIT_CTRL, }, + [EVMCS_2NDEXIT] = { + [EVMCSv1_LEGACY] = EVMCS1_SUPPORTED_VMEXIT_CTRL2, + }, [EVMCS_ENTRY_CTRLS] = { [EVMCSv1_LEGACY] = EVMCS1_SUPPORTED_VMENTRY_CTRL, },
@@ -539,6 +548,9 @@ void nested_evmcs_filter_control_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 * supported_ctrls &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL ; ctl_high &= supported_ctrls;break ; +case MSR_IA32_VMX_EXIT_CTLS2 : + ctl_low &=evmcs_get_supported_ctls (EVMCS_2NDEXIT ); +break ;case MSR_IA32_VMX_ENTRY_CTLS :case MSR_IA32_VMX_TRUE_ENTRY_CTLS : supported_ctrls =evmcs_get_supported_ctls (EVMCS_ENTRY_CTRLS );
@@ -589,6 +601,10 @@ int nested_evmcs_check_controls(struct vmcs12 *vmcs12) vmcs12->vm_exit_controls)))return -EINVAL ; +if (CC (!nested_evmcs_is_valid_controls (EVMCS_2NDEXIT , + vmcs12->secondary_vm_exit_controls))) +return -EINVAL ; +if (CC (!nested_evmcs_is_valid_controls (EVMCS_ENTRY_CTRLS , vmcs12->vm_entry_controls)))return -EINVAL ;
arch/x86/kvm/vmx/nested.c +17 -1
@@ -1411,6 +1411,7 @@ int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)case MSR_IA32_VMX_PINBASED_CTLS :case MSR_IA32_VMX_PROCBASED_CTLS :case MSR_IA32_VMX_EXIT_CTLS : +case MSR_IA32_VMX_EXIT_CTLS2 :case MSR_IA32_VMX_ENTRY_CTLS :/* * The "non-true" VMX capability MSRs are generated from the
@@ -1489,6 +1490,9 @@ int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)if (msr_index ==MSR_IA32_VMX_EXIT_CTLS ) *pdata |=VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR ;break ; +case MSR_IA32_VMX_EXIT_CTLS2 : + *pdata = msrs->secondary_exit_ctls; +break ;case MSR_IA32_VMX_TRUE_ENTRY_CTLS :case MSR_IA32_VMX_ENTRY_CTLS : *pdata = vmx_control_msr(
@@ -1692,6 +1696,8 @@ static void copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx, u32 hv_clean_fields vmcs12->pin_based_vm_exec_control = evmcs->pin_based_vm_exec_control; vmcs12->vm_exit_controls = evmcs->vm_exit_controls; + vmcs12->secondary_vm_exit_controls = + evmcs->secondary_vm_exit_controls; vmcs12->secondary_vm_exec_control = evmcs->secondary_vm_exec_control; }
@@ -1894,6 +1900,7 @@ static void copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)* evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer; * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control; * evmcs->vm_exit_controls = vmcs12->vm_exit_controls; +* evmcs->secondary_vm_exit_controls = vmcs12->secondary_vm_exit_controls; * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control; * evmcs->page_fault_error_code_mask = * vmcs12->page_fault_error_code_mask;
@@ -2411,6 +2418,11 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0 exec_control &= ~VM_EXIT_LOAD_IA32_EFER ;vm_exit_controls_set (vmx, exec_control); +if (exec_control &VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ) { + exec_control =__secondary_vm_exit_controls_get (vmcs01); +secondary_vm_exit_controls_set (vmx, exec_control); + } +/* * Interrupt/Exception Fields */
@@ -6819,13 +6831,17 @@ static void nested_vmx_setup_exit_ctls(struct vmcs_config *vmcs_conf,VM_EXIT_HOST_ADDR_SPACE_SIZE | #endifVM_EXIT_LOAD_IA32_PAT |VM_EXIT_SAVE_IA32_PAT | -VM_EXIT_CLEAR_BNDCFGS ; +VM_EXIT_CLEAR_BNDCFGS |VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ; msrs->exit_ctls_high |=VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |VM_EXIT_LOAD_IA32_EFER |VM_EXIT_SAVE_IA32_EFER |VM_EXIT_SAVE_VMX_PREEMPTION_TIMER |VM_EXIT_ACK_INTR_ON_EXIT |VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL ; +/* secondary exit controls */ +if (msrs->exit_ctls_high &VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ) +rdmsrl (MSR_IA32_VMX_EXIT_CTLS2 , msrs->secondary_exit_ctls); +/* We support free control of debug control saving. */ msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; }
arch/x86/kvm/vmx/vmcs12.c +1 -0
@@ -73,6 +73,7 @@ const unsigned short vmcs12_field_offsets[] = {FIELD (PAGE_FAULT_ERROR_CODE_MATCH , page_fault_error_code_match),FIELD (CR3_TARGET_COUNT , cr3_target_count),FIELD (VM_EXIT_CONTROLS , vm_exit_controls), +FIELD (SECONDARY_VM_EXIT_CONTROLS , secondary_vm_exit_controls),FIELD (VM_EXIT_MSR_STORE_COUNT , vm_exit_msr_store_count),FIELD (VM_EXIT_MSR_LOAD_COUNT , vm_exit_msr_load_count), FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
arch/x86/kvm/vmx/vmcs12.h +2 -0
@@ -185,6 +185,7 @@ struct __packed vmcs12 {u16 host_gs_selector;u16 host_tr_selector;u16 guest_pml_index; +u64 secondary_vm_exit_controls; };/*
@@ -358,6 +359,7 @@ static inline void vmx_check_vmcs12_offsets(void)CHECK_OFFSET (host_gs_selector,992 );CHECK_OFFSET (host_tr_selector,994 );CHECK_OFFSET (guest_pml_index,996 ); +CHECK_OFFSET (secondary_vm_exit_controls,998 ); }extern const unsigned short vmcs12_field_offsets[];
arch/x86/kvm/x86.h +1 -1
@@ -47,7 +47,7 @@ void kvm_spurious_fault(void);* associated feature that KVM supports for nested virtualization. */ #define KVM_FIRST_EMULATED_VMX_MSR MSR_IA32_VMX_BASIC -#define KVM_LAST_EMULATED_VMX_MSR MSR_IA32_VMX_VMFUNC +#define KVM_LAST_EMULATED_VMX_MSR MSR_IA32_VMX_EXIT_CTLS2#define KVM_DEFAULT_PLE_GAP 128#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
Signature · 1 line
2.42.0
PATCH 16/23 KVM: nVMX: Add FRED VMCS fields
In reply to Xin LiAdd FRED VMCS fields to nested VMX context management.
--- Documentation/virt/kvm/x86/nested-vmx.rst | 18 +++ arch/x86/include/asm/hyperv-tlfs.h | 18 +++ arch/x86/kvm/vmx/hyperv.c | 38 ++++++ arch/x86/kvm/vmx/nested.c | 154 ++++++++++++++++++++-- arch/x86/kvm/vmx/vmcs12.c | 18 +++ arch/x86/kvm/vmx/vmcs12.h | 36 +++++ arch/x86/kvm/vmx/vmcs_shadow_fields.h | 6 +- 7 files changed, 274 insertions(+), 14 deletions(-)
Documentation/virt/kvm/x86/nested-vmx.rst +18 -0
@@ -218,6 +218,24 @@ struct shadow_vmcs is ever changed. u16 host_gs_selector; u16 host_tr_selector; u64 secondary_vm_exit_controls; + u64 guest_ia32_fred_config; + u64 guest_ia32_fred_rsp1; + u64 guest_ia32_fred_rsp2; + u64 guest_ia32_fred_rsp3; + u64 guest_ia32_fred_stklvls; + u64 guest_ia32_fred_ssp1; + u64 guest_ia32_fred_ssp2; + u64 guest_ia32_fred_ssp3; + u64 host_ia32_fred_config; + u64 host_ia32_fred_rsp1; + u64 host_ia32_fred_rsp2; + u64 host_ia32_fred_rsp3; + u64 host_ia32_fred_stklvls; + u64 host_ia32_fred_ssp1; + u64 host_ia32_fred_ssp2; + u64 host_ia32_fred_ssp3; + u64 injected_event_data; + u64 original_event_data; };
arch/x86/include/asm/hyperv-tlfs.h +18 -0
@@ -617,6 +617,24 @@ struct hv_enlightened_vmcs {u64 host_ia32_int_ssp_table_addr;u64 padding64_6;u64 secondary_vm_exit_controls; +u64 guest_ia32_fred_config; +u64 guest_ia32_fred_rsp1; +u64 guest_ia32_fred_rsp2; +u64 guest_ia32_fred_rsp3; +u64 guest_ia32_fred_stklvls; +u64 guest_ia32_fred_ssp1; +u64 guest_ia32_fred_ssp2; +u64 guest_ia32_fred_ssp3; +u64 host_ia32_fred_config; +u64 host_ia32_fred_rsp1; +u64 host_ia32_fred_rsp2; +u64 host_ia32_fred_rsp3; +u64 host_ia32_fred_stklvls; +u64 host_ia32_fred_ssp1; +u64 host_ia32_fred_ssp2; +u64 host_ia32_fred_ssp3; +u64 injected_event_data; +u64 original_event_data; } __packed;#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE 0
arch/x86/kvm/vmx/hyperv.c +38 -0
@@ -243,6 +243,8 @@ const struct evmcs_field vmcs_field_to_evmcs_1[] = { HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2),EVMCS1_FIELD (TSC_MULTIPLIER ,tsc_multiplier ,HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2 ), +EVMCS1_FIELD (INJECTED_EVENT_DATA ,injected_event_data , +HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT ),/* * Not used by KVM: *
@@ -262,11 +264,47 @@ const struct evmcs_field vmcs_field_to_evmcs_1[] = {* HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1), */ +/* FRED guest and host context */ +EVMCS1_FIELD (GUEST_IA32_FRED_CONFIG , guest_ia32_fred_config, +HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2 ), +EVMCS1_FIELD (GUEST_IA32_FRED_RSP1 , guest_ia32_fred_rsp1, +HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2 ), +EVMCS1_FIELD (GUEST_IA32_FRED_RSP2 , guest_ia32_fred_rsp2, +HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2 ), +EVMCS1_FIELD (GUEST_IA32_FRED_RSP3 , guest_ia32_fred_rsp3, +HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2 ), +EVMCS1_FIELD (GUEST_IA32_FRED_STKLVLS , guest_ia32_fred_stklvls, +HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2 ), +EVMCS1_FIELD (GUEST_IA32_FRED_SSP1 , guest_ia32_fred_ssp1, +HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2 ), +EVMCS1_FIELD (GUEST_IA32_FRED_SSP2 , guest_ia32_fred_ssp2, +HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2 ), +EVMCS1_FIELD (GUEST_IA32_FRED_SSP3 , guest_ia32_fred_ssp3, +HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2 ), +EVMCS1_FIELD (HOST_IA32_FRED_CONFIG , host_ia32_fred_config, +HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER ), +EVMCS1_FIELD (HOST_IA32_FRED_RSP1 , host_ia32_fred_rsp1, +HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER ), +EVMCS1_FIELD (HOST_IA32_FRED_RSP2 , host_ia32_fred_rsp2, +HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER ), +EVMCS1_FIELD (HOST_IA32_FRED_RSP3 , host_ia32_fred_rsp3, +HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER ), +EVMCS1_FIELD (HOST_IA32_FRED_STKLVLS , host_ia32_fred_stklvls, +HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER ), +EVMCS1_FIELD (HOST_IA32_FRED_SSP1 , host_ia32_fred_ssp1, +HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER ), +EVMCS1_FIELD (HOST_IA32_FRED_SSP2 , host_ia32_fred_ssp2, +HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER ), +EVMCS1_FIELD (HOST_IA32_FRED_SSP3 , host_ia32_fred_ssp3, +HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER ), +/* 64 bit read only */ EVMCS1_FIELD (GUEST_PHYSICAL_ADDRESS , guest_physical_address,HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE ),EVMCS1_FIELD (EXIT_QUALIFICATION , exit_qualification,HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE ), +EVMCS1_FIELD (ORIGINAL_EVENT_DATA , original_event_data, +HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE ),/* * Not defined in KVM: *
arch/x86/kvm/vmx/nested.c +141 -13
281 lines not on this page · raw message
arch/x86/kvm/vmx/vmcs12.c +18 -0
@@ -80,6 +80,7 @@ const unsigned short vmcs12_field_offsets[] = {FIELD (VM_ENTRY_MSR_LOAD_COUNT , vm_entry_msr_load_count),FIELD (VM_ENTRY_INTR_INFO_FIELD , vm_entry_intr_info_field),FIELD (VM_ENTRY_EXCEPTION_ERROR_CODE , vm_entry_exception_error_code), +FIELD (INJECTED_EVENT_DATA , injected_event_data),FIELD (VM_ENTRY_INSTRUCTION_LEN , vm_entry_instruction_len),FIELD (TPR_THRESHOLD , tpr_threshold), FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
@@ -89,6 +90,7 @@ const unsigned short vmcs12_field_offsets[] = {FIELD (VM_EXIT_INTR_ERROR_CODE , vm_exit_intr_error_code),FIELD (IDT_VECTORING_INFO_FIELD , idt_vectoring_info_field),FIELD (IDT_VECTORING_ERROR_CODE , idt_vectoring_error_code), +FIELD (ORIGINAL_EVENT_DATA , original_event_data),FIELD (VM_EXIT_INSTRUCTION_LEN , vm_exit_instruction_len),FIELD (VMX_INSTRUCTION_INFO , vmx_instruction_info), FIELD(GUEST_ES_LIMIT, guest_es_limit),
@@ -152,5 +154,21 @@ const unsigned short vmcs12_field_offsets[] = {FIELD (HOST_IA32_SYSENTER_EIP , host_ia32_sysenter_eip),FIELD (HOST_RSP , host_rsp),FIELD (HOST_RIP , host_rip), +FIELD (GUEST_IA32_FRED_CONFIG , guest_ia32_fred_config), +FIELD (GUEST_IA32_FRED_RSP1 , guest_ia32_fred_rsp1), +FIELD (GUEST_IA32_FRED_RSP2 , guest_ia32_fred_rsp2), +FIELD (GUEST_IA32_FRED_RSP3 , guest_ia32_fred_rsp3), +FIELD (GUEST_IA32_FRED_STKLVLS , guest_ia32_fred_stklvls), +FIELD (GUEST_IA32_FRED_SSP1 , guest_ia32_fred_ssp1), +FIELD (GUEST_IA32_FRED_SSP2 , guest_ia32_fred_ssp2), +FIELD (GUEST_IA32_FRED_SSP3 , guest_ia32_fred_ssp3), +FIELD (HOST_IA32_FRED_CONFIG , host_ia32_fred_config), +FIELD (HOST_IA32_FRED_RSP1 , host_ia32_fred_rsp1), +FIELD (HOST_IA32_FRED_RSP2 , host_ia32_fred_rsp2), +FIELD (HOST_IA32_FRED_RSP3 , host_ia32_fred_rsp3), +FIELD (HOST_IA32_FRED_STKLVLS , host_ia32_fred_stklvls), +FIELD (HOST_IA32_FRED_SSP1 , host_ia32_fred_ssp1), +FIELD (HOST_IA32_FRED_SSP2 , host_ia32_fred_ssp2), + FIELD(HOST_IA32_FRED_SSP3, host_ia32_fred_ssp3), };const unsigned int nr_vmcs12_fields =ARRAY_SIZE (vmcs12_field_offsets);
arch/x86/kvm/vmx/vmcs12.h +36 -0
@@ -186,6 +186,24 @@ struct __packed vmcs12 {u16 host_tr_selector;u16 guest_pml_index;u64 secondary_vm_exit_controls; +u64 guest_ia32_fred_config; +u64 guest_ia32_fred_rsp1; +u64 guest_ia32_fred_rsp2; +u64 guest_ia32_fred_rsp3; +u64 guest_ia32_fred_stklvls; +u64 guest_ia32_fred_ssp1; +u64 guest_ia32_fred_ssp2; +u64 guest_ia32_fred_ssp3; +u64 host_ia32_fred_config; +u64 host_ia32_fred_rsp1; +u64 host_ia32_fred_rsp2; +u64 host_ia32_fred_rsp3; +u64 host_ia32_fred_stklvls; +u64 host_ia32_fred_ssp1; +u64 host_ia32_fred_ssp2; +u64 host_ia32_fred_ssp3; +u64 injected_event_data; +u64 original_event_data; };/*
@@ -360,6 +378,24 @@ static inline void vmx_check_vmcs12_offsets(void)CHECK_OFFSET (host_tr_selector,994 );CHECK_OFFSET (guest_pml_index,996 );CHECK_OFFSET (secondary_vm_exit_controls,998 ); +CHECK_OFFSET (guest_ia32_fred_config,1006 ); +CHECK_OFFSET (guest_ia32_fred_rsp1,1014 ); +CHECK_OFFSET (guest_ia32_fred_rsp2,1022 ); +CHECK_OFFSET (guest_ia32_fred_rsp3,1030 ); +CHECK_OFFSET (guest_ia32_fred_stklvls,1038 ); +CHECK_OFFSET (guest_ia32_fred_ssp1,1046 ); +CHECK_OFFSET (guest_ia32_fred_ssp2,1054 ); +CHECK_OFFSET (guest_ia32_fred_ssp3,1062 ); +CHECK_OFFSET (host_ia32_fred_config,1070 ); +CHECK_OFFSET (host_ia32_fred_rsp1,1078 ); +CHECK_OFFSET (host_ia32_fred_rsp2,1086 ); +CHECK_OFFSET (host_ia32_fred_rsp3,1094 ); +CHECK_OFFSET (host_ia32_fred_stklvls,1102 ); +CHECK_OFFSET (host_ia32_fred_ssp1,1110 ); +CHECK_OFFSET (host_ia32_fred_ssp2,1118 ); +CHECK_OFFSET (host_ia32_fred_ssp3,1126 ); +CHECK_OFFSET (injected_event_data,1134 ); +CHECK_OFFSET (original_event_data,1142 ); }extern const unsigned short vmcs12_field_offsets[];
arch/x86/kvm/vmx/vmcs_shadow_fields.h +5 -1
@@ -41,9 +41,9 @@ SHADOW_FIELD_RW(HOST_GS_SELECTOR, host_gs_selector) SHADOW_FIELD_RO(VM_EXIT_REASON, vm_exit_reason) SHADOW_FIELD_RO(VM_EXIT_INTR_INFO, vm_exit_intr_info) SHADOW_FIELD_RO(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len) +SHADOW_FIELD_RO(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code) SHADOW_FIELD_RO(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field) SHADOW_FIELD_RO(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code) -SHADOW_FIELD_RO(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code) SHADOW_FIELD_RO(GUEST_CS_AR_BYTES, guest_cs_ar_bytes) SHADOW_FIELD_RO(GUEST_SS_AR_BYTES, guest_ss_ar_bytes)SHADOW_FIELD_RW (CPU_BASED_VM_EXEC_CONTROL , cpu_based_vm_exec_control)
@@ -74,6 +74,10 @@ SHADOW_FIELD_RW(HOST_GS_BASE, host_gs_base)/* 64-bit */ SHADOW_FIELD_RO(GUEST_PHYSICAL_ADDRESS, guest_physical_address) SHADOW_FIELD_RO(GUEST_PHYSICAL_ADDRESS_HIGH, guest_physical_address) +SHADOW_FIELD_RO(ORIGINAL_EVENT_DATA, original_event_data) +SHADOW_FIELD_RO(ORIGINAL_EVENT_DATA_HIGH, original_event_data) +SHADOW_FIELD_RW(INJECTED_EVENT_DATA, injected_event_data) +SHADOW_FIELD_RW(INJECTED_EVENT_DATA_HIGH, injected_event_data)#undef SHADOW_FIELD_RO#undef SHADOW_FIELD_RW
Signature · 1 line
2.42.0
PATCH 17/23 KVM: nVMX: Add support for VMX FRED controls
In reply to Xin LiAdd VMX FRED controls to nested VMX controls and set the VMX nested-exception support bit (bit 58) in the nested IA32_VMX_BASIC MSR when FRED is enabled.
--- arch/x86/kvm/vmx/hyperv.c | 7 +++++-- arch/x86/kvm/vmx/nested.c | 14 ++++++++++---- arch/x86/kvm/vmx/vmx.c | 3 +++ 3 files changed, 18 insertions(+), 6 deletions(-)
arch/x86/kvm/vmx/hyperv.c +5 -2
@@ -106,7 +106,9 @@ VM_EXIT_CLEAR_IA32_RTIT_CTL | \ VM_EXIT_ACTIVATE_SECONDARY_CONTROLS) -#define EVMCS1_SUPPORTED_VMEXIT_CTRL2 (0ULL) +#define EVMCS1_SUPPORTED_VMEXIT_CTRL2 \ + (SECONDARY_VM_EXIT_SAVE_IA32_FRED | \ + SECONDARY_VM_EXIT_LOAD_IA32_FRED)#define EVMCS1_SUPPORTED_VMENTRY_CTRL \ (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | \
@@ -117,7 +119,8 @@VM_ENTRY_LOAD_IA32_EFER | \VM_ENTRY_LOAD_BNDCFGS | \VM_ENTRY_PT_CONCEAL_PIP | \ - VM_ENTRY_LOAD_IA32_RTIT_CTL) +VM_ENTRY_LOAD_IA32_RTIT_CTL | \ + VM_ENTRY_LOAD_IA32_FRED) #define EVMCS1_SUPPORTED_VMFUNC (0)
arch/x86/kvm/vmx/nested.c +10 -4
@@ -1230,10 +1230,12 @@ static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)#define VMX_BASIC_FEATURES_MASK \ (VMX_BASIC_DUAL_MONITOR_TREATMENT | \ VMX_BASIC_INOUT | \ - VMX_BASIC_TRUE_CTLS) + VMX_BASIC_TRUE_CTLS | \ + VMX_BASIC_NESTED_EXCEPTION) -#define VMX_BASIC_RESERVED_BITS \ - (GENMASK_ULL(63, 56) | GENMASK_ULL(47, 45) | BIT_ULL(31)) +#define VMX_BASIC_RESERVED_BITS \ + (GENMASK_ULL(63, 59) | GENMASK_ULL(57, 56) | \ + GENMASK_ULL(47, 45) | BIT_ULL(31))static int vmx_restore_vmx_basic (struct vcpu_vmx *vmx,u64 data) {
@@ -6985,7 +6987,8 @@ static void nested_vmx_setup_entry_ctls(struct vmcs_config *vmcs_conf,#ifdef CONFIG_X86_64 VM_ENTRY_IA32E_MODE | #endif -VM_ENTRY_LOAD_IA32_PAT |VM_ENTRY_LOAD_BNDCFGS ; +VM_ENTRY_LOAD_IA32_PAT |VM_ENTRY_LOAD_BNDCFGS | +VM_ENTRY_LOAD_IA32_FRED ; msrs->entry_ctls_high |= (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR |VM_ENTRY_LOAD_IA32_EFER |VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL );
@@ -7141,6 +7144,9 @@ static void nested_vmx_setup_basic(struct nested_vmx_msrs *msrs)if (cpu_has_vmx_basic_inout ()) msrs->basic |=VMX_BASIC_INOUT ; + +if (cpu_feature_enabled (X86_FEATURE_FRED )) + msrs->basic |=VMX_BASIC_NESTED_EXCEPTION ; }static void nested_vmx_setup_cr_fixed (struct nested_vmx_msrs *msrs)
arch/x86/kvm/vmx/vmx.c +3 -0
@@ -7970,6 +7970,9 @@ static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)cr4_fixed1_update (X86_CR4_UMIP , ecx,feature_bit (UMIP ));cr4_fixed1_update (X86_CR4_LA57 , ecx,feature_bit (LA57 )); + entry =kvm_find_cpuid_entry_index (vcpu,0x7 ,1 ); +cr4_fixed1_update (X86_CR4_FRED , eax,feature_bit (FRED )); +#undef cr4_fixed1_update }
Signature · 1 line
2.42.0
PATCH 18/23 KVM: nVMX: Add VMCS FRED states checking
In reply to Xin LiAdd FRED related VMCS fields checkings.
As real hardware, nested VMX performs checks on various VMCS fields, including both controls and guest/host states. With the introduction of VMX FRED, add FRED related VMCS fields checkings.
--- arch/x86/kvm/vmx/nested.c | 70 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/nested.c +69 -1
@@ -2940,6 +2940,7 @@ static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) { struct vcpu_vmx *vmx = to_vmx(vcpu); + bool fred_enabled = !!(vmcs12->guest_cr4 & X86_CR4_FRED); if (CC(!vmx_control_verify(vmcs12->vm_entry_controls, vmx->nested.msrs.entry_ctls_low,
@@ -2958,6 +2959,7 @@ static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu,u32 intr_type = intr_info &INTR_INFO_INTR_TYPE_MASK ;bool has_error_code = intr_info &INTR_INFO_DELIVER_CODE_MASK ;bool should_have_error_code; +bool has_nested_exception = vmx->nested.msrs.basic &VMX_BASIC_NESTED_EXCEPTION ;bool urg =nested_cpu_has2 (vmcs12,SECONDARY_EXEC_UNRESTRICTED_GUEST );bool prot_mode = !urg || vmcs12->guest_cr0 &X86_CR0_PE ;
@@ -2971,7 +2973,9 @@ static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu,/* VM-entry interruption-info field: vector */ if (CC (intr_type ==INTR_TYPE_NMI_INTR && vector !=NMI_VECTOR ) ||CC (intr_type ==INTR_TYPE_HARD_EXCEPTION && vector >31 ) || -CC (intr_type ==INTR_TYPE_OTHER_EVENT && vector !=0 )) +CC (intr_type ==INTR_TYPE_OTHER_EVENT && + ((!fred_enabled && vector >0 ) || + (fred_enabled && vector >2 ))))return -EINVAL ;/* VM-entry interruption-info field: deliver error code */
@@ -2990,6 +2994,15 @@ static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu,if (CC (intr_info &INTR_INFO_RESVD_BITS_MASK ))return -EINVAL ; +/* +* When the CPU enumerates VMX nested-exception support, bit 13 +* (set to indicate a nested exception) of the intr info field +* may have value 1. Otherwise the bit 13 is reserved. +*/ +if (CC (!has_nested_exception && + (intr_info &INTR_INFO_NESTED_EXCEPTION_MASK ))) +return -EINVAL ; +/* VM-entry instruction length */ switch (intr_type) {case INTR_TYPE_SOFT_EXCEPTION :
@@ -2999,6 +3012,12 @@ static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu,CC (vmcs12->vm_entry_instruction_len ==0 && CC(!nested_cpu_has_zero_length_injection(vcpu)))) return -EINVAL; +break ; +case INTR_TYPE_OTHER_EVENT : +if (fred_enabled && (vector ==1 || vector ==2 )) +if (CC (vmcs12->vm_entry_instruction_len >15 )) +return -EINVAL ; +break ; } }
@@ -3056,14 +3075,31 @@ static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, vmcs12->host_ia32_perf_global_ctrl)))return -EINVAL ; +/* Host FRED state checking */ if (ia32e) {if (CC (!(vmcs12->host_cr4 &X86_CR4_PAE )))return -EINVAL ; +if (vmcs12->vm_exit_controls &VM_EXIT_ACTIVATE_SECONDARY_CONTROLS && + vmcs12->secondary_vm_exit_controls &SECONDARY_VM_EXIT_LOAD_IA32_FRED ) { +/* Bit 2, bits 5:4, and bit 11 of the IA32_FRED_CONFIG must be zero */ +if (CC (vmcs12->host_ia32_fred_config &0x834 ) || +CC (vmcs12->host_ia32_fred_rsp1 &0x3F ) || +CC (vmcs12->host_ia32_fred_rsp2 &0x3F ) || +CC (vmcs12->host_ia32_fred_rsp3 &0x3F )) +return -EINVAL ; +if (CC (is_noncanonical_address (vmcs12->host_ia32_fred_config & ~0xFFFULL , vcpu)) || +CC (is_noncanonical_address (vmcs12->host_ia32_fred_rsp1, vcpu)) || +CC (is_noncanonical_address (vmcs12->host_ia32_fred_rsp2, vcpu)) || +CC (is_noncanonical_address (vmcs12->host_ia32_fred_rsp3, vcpu))) +return -EINVAL ; + } }else {if (CC (vmcs12->vm_entry_controls &VM_ENTRY_IA32E_MODE ) ||CC (vmcs12->host_cr4 &X86_CR4_PCIDE ) ||CC ((vmcs12->host_rip) >>32 ))return -EINVAL ; +if (CC (vmcs12->host_cr4 &X86_CR4_FRED )) +return -EINVAL ; } if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
@@ -3205,6 +3241,38 @@ static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu, CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))))return -EINVAL ; +/* Guest FRED state checking */ +if (ia32e) { +if (vmcs12->vm_entry_controls &VM_ENTRY_LOAD_IA32_FRED ) { +/* Bit 2, bits 5:4, and bit 11 of the IA32_FRED_CONFIG must be zero */ +if (CC (vmcs12->guest_ia32_fred_config &0x834 ) || +CC (vmcs12->guest_ia32_fred_rsp1 &0x3F ) || +CC (vmcs12->guest_ia32_fred_rsp2 &0x3F ) || +CC (vmcs12->guest_ia32_fred_rsp3 &0x3F )) +return -EINVAL ; +if (CC (is_noncanonical_address (vmcs12->guest_ia32_fred_config & ~0xFFFULL , vcpu)) || +CC (is_noncanonical_address (vmcs12->guest_ia32_fred_rsp1, vcpu)) || +CC (is_noncanonical_address (vmcs12->guest_ia32_fred_rsp2, vcpu)) || +CC (is_noncanonical_address (vmcs12->guest_ia32_fred_rsp3, vcpu))) +return -EINVAL ; + } +if (vmcs12->guest_cr4 &X86_CR4_FRED ) { +unsigned int ss_dpl =VMX_AR_DPL (vmcs12->guest_ss_ar_bytes); +if (CC (ss_dpl ==1 || ss_dpl ==2 )) +return -EINVAL ; +if (ss_dpl ==0 && +CC (!(vmcs12->guest_cs_ar_bytes &VMX_AR_L_MASK ))) +return -EINVAL ; +if (ss_dpl ==3 && + (CC (vmcs12->guest_rflags &X86_EFLAGS_IOPL ) || +CC (vmcs12->guest_interruptibility_info &GUEST_INTR_STATE_STI ))) +return -EINVAL ; + } + }else { +if (CC (vmcs12->guest_cr4 &X86_CR4_FRED )) +return -EINVAL ; + } +if (nested_check_guest_non_reg_state (vmcs12))return -EINVAL ;
Signature · 1 line
2.42.0
PATCH 19/23 KVM: x86: Allow FRED/LKGS/WRMSRNS to be exposed to guests
In reply to Xin LiAllow FRED/LKGS/WRMSRNS to be exposed to guests, thus a guest OS could see these features when the guest is configured with FRED/LKGS/WRMSRNS in Qemu.
A qemu patch is required to expose FRED/LKGS/WRMSRNS to KVM guests.
--- arch/x86/kvm/cpuid.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/cpuid.c +2 -2
@@ -670,8 +670,8 @@ void kvm_set_cpu_caps(void)kvm_cpu_cap_mask (CPUID_7_1_EAX ,F (AVX_VNNI ) |F (AVX512_BF16 ) |F (CMPCCXADD ) | -F (FZRM ) |F (FSRS ) |F (FSRC ) | -F (AMX_FP16 ) |F (AVX_IFMA ) +F (FZRM ) |F (FSRS ) |F (FSRC ) |F (FRED ) |F (LKGS ) | +F (WRMSRNS ) |F (AMX_FP16 ) |F (AVX_IFMA ) ); kvm_cpu_cap_init_kvm_defined(CPUID_7_1_EDX,
Signature · 1 line
2.42.0
PATCH 20/23 KVM: selftests: Add FRED VMCS fields to evmcs
In reply to Xin LiAdd FRED VMCS fields to evmcs.
--- .../selftests/kvm/include/x86_64/evmcs.h | 146 ++++++++++++++++++ .../selftests/kvm/include/x86_64/vmx.h | 20 +++ 2 files changed, 166 insertions(+)
tools/testing/selftests/kvm/include/x86_64/evmcs.h +146 -0
@@ -216,6 +216,27 @@ struct hv_enlightened_vmcs {u64 host_ssp;u64 host_ia32_int_ssp_table_addr;u64 padding64_6; + +u64 host_ia32_fred_config; +u64 host_ia32_fred_rsp1; +u64 host_ia32_fred_rsp2; +u64 host_ia32_fred_rsp3; +u64 host_ia32_fred_stklvls; +u64 host_ia32_fred_ssp1; +u64 host_ia32_fred_ssp2; +u64 host_ia32_fred_ssp3; + +u64 guest_ia32_fred_config; +u64 guest_ia32_fred_rsp1; +u64 guest_ia32_fred_rsp2; +u64 guest_ia32_fred_rsp3; +u64 guest_ia32_fred_stklvls; +u64 guest_ia32_fred_ssp1; +u64 guest_ia32_fred_ssp2; +u64 guest_ia32_fred_ssp3; + +u64 injected_event_data; +u64 original_event_data; } __packed;#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE 0
@@ -450,6 +471,9 @@ static inline int evmcs_vmread(uint64_t encoding, uint64_t *value)case GUEST_LINEAR_ADDRESS : *value = current_evmcs->guest_linear_address;break ; +case ORIGINAL_EVENT_DATA : + *value = current_evmcs->original_event_data; +break ;case VM_EXIT_MSR_STORE_ADDR : *value = current_evmcs->vm_exit_msr_store_addr;break ;
@@ -492,6 +516,9 @@ static inline int evmcs_vmread(uint64_t encoding, uint64_t *value)case VM_ENTRY_EXCEPTION_ERROR_CODE : *value = current_evmcs->vm_entry_exception_error_code;break ; +case INJECTED_EVENT_DATA : + *value = current_evmcs->injected_event_data; +break ;case VM_ENTRY_INSTRUCTION_LEN : *value = current_evmcs->vm_entry_instruction_len;break ;
@@ -669,6 +696,54 @@ static inline int evmcs_vmread(uint64_t encoding, uint64_t *value)case TSC_MULTIPLIER : *value = current_evmcs->tsc_multiplier;break ; +case HOST_IA32_FRED_CONFIG : + *value = current_evmcs->host_ia32_fred_config; +break ; +case HOST_IA32_FRED_RSP1 : + *value = current_evmcs->host_ia32_fred_rsp1; +break ; +case HOST_IA32_FRED_RSP2 : + *value = current_evmcs->host_ia32_fred_rsp2; +break ; +case HOST_IA32_FRED_RSP3 : + *value = current_evmcs->host_ia32_fred_rsp3; +break ; +case HOST_IA32_FRED_STKLVLS : + *value = current_evmcs->host_ia32_fred_stklvls; +break ; +case HOST_IA32_FRED_SSP1 : + *value = current_evmcs->host_ia32_fred_ssp1; +break ; +case HOST_IA32_FRED_SSP2 : + *value = current_evmcs->host_ia32_fred_ssp2; +break ; +case HOST_IA32_FRED_SSP3 : + *value = current_evmcs->host_ia32_fred_ssp3; +break ; +case GUEST_IA32_FRED_CONFIG : + *value = current_evmcs->guest_ia32_fred_config; +break ; +case GUEST_IA32_FRED_RSP1 : + *value = current_evmcs->guest_ia32_fred_rsp1; +break ; +case GUEST_IA32_FRED_RSP2 : + *value = current_evmcs->guest_ia32_fred_rsp2; +break ; +case GUEST_IA32_FRED_RSP3 : + *value = current_evmcs->guest_ia32_fred_rsp3; +break ; +case GUEST_IA32_FRED_STKLVLS : + *value = current_evmcs->guest_ia32_fred_stklvls; +break ; +case GUEST_IA32_FRED_SSP1 : + *value = current_evmcs->guest_ia32_fred_ssp1; +break ; +case GUEST_IA32_FRED_SSP2 : + *value = current_evmcs->guest_ia32_fred_ssp2; +break ; +case GUEST_IA32_FRED_SSP3 : + *value = current_evmcs->guest_ia32_fred_ssp3; +break ;default :return 1 ; }
@@ -906,6 +981,10 @@ static inline int evmcs_vmwrite(uint64_t encoding, uint64_t value) current_evmcs->guest_linear_address = value; current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE ;break ; +case ORIGINAL_EVENT_DATA : + current_evmcs->original_event_data = value; + current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE ; +break ;case VM_EXIT_MSR_STORE_ADDR : current_evmcs->vm_exit_msr_store_addr = value; current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL ;
@@ -962,6 +1041,10 @@ static inline int evmcs_vmwrite(uint64_t encoding, uint64_t value) current_evmcs->vm_entry_exception_error_code = value; current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT ;break ; +case INJECTED_EVENT_DATA : + current_evmcs->injected_event_data = value; + current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT ; +break ;case VM_ENTRY_INSTRUCTION_LEN : current_evmcs->vm_entry_instruction_len = value; current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT ;
@@ -1198,6 +1281,69 @@ static inline int evmcs_vmwrite(uint64_t encoding, uint64_t value) current_evmcs->tsc_multiplier = value; current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2 ;break ; +case HOST_IA32_FRED_CONFIG : + current_evmcs->host_ia32_fred_config = value; + current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER ; +break ; +case HOST_IA32_FRED_RSP1 : + current_evmcs->host_ia32_fred_rsp1 = value; + current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER ; +break ; +case HOST_IA32_FRED_RSP2 : + current_evmcs->host_ia32_fred_rsp2 = value; + current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER ; +break ; +case HOST_IA32_FRED_RSP3 : + current_evmcs->host_ia32_fred_rsp3 = value; + current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER ; +break ; +case HOST_IA32_FRED_STKLVLS : + current_evmcs->host_ia32_fred_stklvls = value; + current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER ; +break ; +case HOST_IA32_FRED_SSP1 : + current_evmcs->host_ia32_fred_ssp1 = value; + current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER ; +break ; +case HOST_IA32_FRED_SSP2 : + current_evmcs->host_ia32_fred_ssp2 = value; + current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER ; +break ; +case HOST_IA32_FRED_SSP3 : + current_evmcs->host_ia32_fred_ssp3 = value; + current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER ; +break ; +case GUEST_IA32_FRED_CONFIG : + current_evmcs->guest_ia32_fred_config = value; + current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2 ; +case GUEST_IA32_FRED_RSP1 : + current_evmcs->guest_ia32_fred_rsp1 = value; + current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2 ; +break ; +case GUEST_IA32_FRED_RSP2 : + current_evmcs->guest_ia32_fred_rsp2 = value; + current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2 ; +break ; +case GUEST_IA32_FRED_RSP3 : + current_evmcs->guest_ia32_fred_rsp3 = value; + current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2 ; +break ; +case GUEST_IA32_FRED_STKLVLS : + current_evmcs->guest_ia32_fred_stklvls = value; + current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2 ; +break ; +case GUEST_IA32_FRED_SSP1 : + current_evmcs->guest_ia32_fred_ssp1 = value; + current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2 ; +break ; +case GUEST_IA32_FRED_SSP2 : + current_evmcs->guest_ia32_fred_ssp2 = value; + current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2 ; +break ; +case GUEST_IA32_FRED_SSP3 : + current_evmcs->guest_ia32_fred_ssp3 = value; + current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2 ; +break ;default :return 1 ; }
tools/testing/selftests/kvm/include/x86_64/vmx.h +20 -0
@@ -165,8 +165,12 @@ enum vmcs_field {ENCLS_EXITING_BITMAP_HIGH =0x0000202F ,TSC_MULTIPLIER =0x00002032 ,TSC_MULTIPLIER_HIGH =0x00002033 , +INJECTED_EVENT_DATA =0x00002052 , +INJECTED_EVENT_DATA_HIGH =0x00002053 ,GUEST_PHYSICAL_ADDRESS =0x00002400 ,GUEST_PHYSICAL_ADDRESS_HIGH =0x00002401 , +ORIGINAL_EVENT_DATA =0x00002404 , +ORIGINAL_EVENT_DATA_HIGH =0x00002405 ,VMCS_LINK_POINTER =0x00002800 ,VMCS_LINK_POINTER_HIGH =0x00002801 , GUEST_IA32_DEBUGCTL = 0x00002802,
@@ -187,12 +191,28 @@ enum vmcs_field {GUEST_PDPTR3_HIGH =0x00002811 ,GUEST_BNDCFGS =0x00002812 ,GUEST_BNDCFGS_HIGH =0x00002813 , +GUEST_IA32_FRED_CONFIG =0x0000281a , +GUEST_IA32_FRED_RSP1 =0x0000281c , +GUEST_IA32_FRED_RSP2 =0x0000281e , +GUEST_IA32_FRED_RSP3 =0x00002820 , +GUEST_IA32_FRED_STKLVLS =0x00002822 , +GUEST_IA32_FRED_SSP1 =0x00002824 , +GUEST_IA32_FRED_SSP2 =0x00002826 , +GUEST_IA32_FRED_SSP3 =0x00002828 ,HOST_IA32_PAT =0x00002c00 ,HOST_IA32_PAT_HIGH =0x00002c01 ,HOST_IA32_EFER =0x00002c02 ,HOST_IA32_EFER_HIGH =0x00002c03 ,HOST_IA32_PERF_GLOBAL_CTRL =0x00002c04 ,HOST_IA32_PERF_GLOBAL_CTRL_HIGH =0x00002c05 , +HOST_IA32_FRED_CONFIG =0x00002c08 , +HOST_IA32_FRED_RSP1 =0x00002c0a , +HOST_IA32_FRED_RSP2 =0x00002c0c , +HOST_IA32_FRED_RSP3 =0x00002c0e , +HOST_IA32_FRED_STKLVLS =0x00002c10 , +HOST_IA32_FRED_SSP1 =0x00002c12 , +HOST_IA32_FRED_SSP2 =0x00002c14 , +HOST_IA32_FRED_SSP3 =0x00002c16 ,PIN_BASED_VM_EXEC_CONTROL =0x00004000 ,CPU_BASED_VM_EXEC_CONTROL =0x00004002 , EXCEPTION_BITMAP = 0x00004004,
Signature · 1 line
2.42.0
PATCH 21/23 KVM: selftests: Run debug_regs test with FRED enabled
In reply to Xin LiRun another round of debug_regs test with FRED enabled if FRED is available.
--- .../selftests/kvm/include/x86_64/processor.h | 4 ++ .../testing/selftests/kvm/x86_64/debug_regs.c | 50 ++++++++++++++----- 2 files changed, 41 insertions(+), 13 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +4 -0
@@ -47,6 +47,7 @@ extern bool host_cpu_is_amd;#define X86_CR4_SMEP (1ul << 20)#define X86_CR4_SMAP (1ul << 21)#define X86_CR4_PKE (1ul << 22) +#define X86_CR4_FRED (1ul << 32)struct xstate_header {u64 xstate_bv;
@@ -163,6 +164,9 @@ struct kvm_x86_cpu_feature {#define X86_FEATURE_SPEC_CTRL KVM_X86_CPU_FEATURE(0x7, 0, EDX, 26)#define X86_FEATURE_ARCH_CAPABILITIES KVM_X86_CPU_FEATURE(0x7, 0, EDX, 29)#define X86_FEATURE_PKS KVM_X86_CPU_FEATURE(0x7, 0, ECX, 31) +#define X86_FEATURE_FRED KVM_X86_CPU_FEATURE(0x7, 1, EAX, 17) +#define X86_FEATURE_LKGS KVM_X86_CPU_FEATURE(0x7, 1, EAX, 18) +#define X86_FEATURE_WRMSRNS KVM_X86_CPU_FEATURE(0x7, 1, EAX, 19)#define X86_FEATURE_XTILECFG KVM_X86_CPU_FEATURE(0xD, 0, EAX, 17)#define X86_FEATURE_XTILEDATA KVM_X86_CPU_FEATURE(0xD, 0, EAX, 18)#define X86_FEATURE_XSAVES KVM_X86_CPU_FEATURE(0xD, 1, EAX, 3)
tools/testing/selftests/kvm/x86_64/debug_regs.c +37 -13
86 lines not on this page · raw message
Signature · 1 line
2.42.0
PATCH 22/23 KVM: selftests: Add a new VM guest mode to run user level code
In reply to Xin LiAdd a new VM guest mode VM_MODE_PXXV48_4K_USER to set the user bit of guest page table entries, thus allow user level code to run in guests.
--- .../testing/selftests/kvm/include/kvm_util_base.h | 1 + tools/testing/selftests/kvm/lib/kvm_util.c | 5 ++++- .../testing/selftests/kvm/lib/x86_64/processor.c | 15 ++++++++++----- tools/testing/selftests/kvm/lib/x86_64/vmx.c | 4 ++-- 4 files changed, 17 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +1 -0
@@ -185,6 +185,7 @@ enum vm_guest_mode {VM_MODE_P36V48_16K ,VM_MODE_P36V48_64K ,VM_MODE_P36V47_16K , +VM_MODE_PXXV48_4K_USER ,/* For 48bits VA but ANY bits PA with USER bit set */ NUM_VM_MODES, };
tools/testing/selftests/kvm/lib/kvm_util.c +4 -1
@@ -162,6 +162,7 @@ const char *vm_guest_mode_string(uint32_t i) [VM_MODE_P36V48_16K] = "PA-bits:36, VA-bits:48, 16K pages", [VM_MODE_P36V48_64K] = "PA-bits:36, VA-bits:48, 64K pages", [VM_MODE_P36V47_16K] = "PA-bits:36, VA-bits:47, 16K pages", + [VM_MODE_PXXV48_4K_USER ] ="PA-bits:ANY, VA-bits:48, 4K user pages" , };_Static_assert (sizeof (strings)/sizeof (char *) ==NUM_VM_MODES ,"Missing new mode strings?" );
@@ -187,6 +188,7 @@ const struct vm_guest_mode_params vm_guest_mode_params[] = { [VM_MODE_P36V48_16K] = { 36, 48, 0x4000, 14 }, [VM_MODE_P36V48_64K] = { 36, 48, 0x10000, 16 }, [VM_MODE_P36V47_16K] = { 36, 47, 0x4000, 14 }, + [VM_MODE_PXXV48_4K_USER] = { 0, 0, 0x1000, 12 }, };_Static_assert (sizeof (vm_guest_mode_params)/sizeof (struct vm_guest_mode_params ) ==NUM_VM_MODES ,"Missing new mode params?" );
@@ -260,6 +262,7 @@ struct kvm_vm *____vm_create(enum vm_guest_mode mode) vm->pgtable_levels =3 ;break ;case VM_MODE_PXXV48_4K : +case VM_MODE_PXXV48_4K_USER :#ifdef __x86_64__kvm_get_cpu_address_width (&vm->pa_bits, &vm->va_bits);/*
@@ -275,7 +278,7 @@ struct kvm_vm *____vm_create(enum vm_guest_mode mode) vm->pgtable_levels =4 ; vm->va_bits =48 ;#else -TEST_FAIL ("VM_MODE_PXXV48_4K not supported on non-x86 platforms" ); +TEST_FAIL ("VM_MODE_PXXV48_4K(_USER) not supported on non-x86 platforms" );#endif break ;case VM_MODE_P47V64_4K :
tools/testing/selftests/kvm/lib/x86_64/processor.c +10 -5
@@ -124,8 +124,8 @@ bool kvm_is_tdp_enabled(void)void virt_arch_pgd_alloc (struct kvm_vm *vm) { -TEST_ASSERT (vm->mode ==VM_MODE_PXXV48_4K ,"Attempt to use " -"unknown or unsupported guest mode, mode: 0x%x" , vm->mode); +TEST_ASSERT ((vm->mode ==VM_MODE_PXXV48_4K ) || (vm->mode ==VM_MODE_PXXV48_4K_USER ), +"Attempt to use unknown or unsupported guest mode, mode: 0x%x" , vm->mode);/* If needed, create page map l4 table. */ if (!vm->pgd_created) {
@@ -159,6 +159,8 @@ static uint64_t *virt_create_upper_pte(struct kvm_vm *vm,if (!(*pte &PTE_PRESENT_MASK )) { *pte =PTE_PRESENT_MASK |PTE_WRITABLE_MASK ; +if (vm->mode ==VM_MODE_PXXV48_4K_USER ) + *pte |=PTE_USER_MASK ;if (current_level == target_level) *pte |=PTE_LARGE_MASK | (paddr &PHYSICAL_PAGE_MASK );else
@@ -185,7 +187,7 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level)uint64_t *pml4e, *pdpe, *pde;uint64_t *pte; -TEST_ASSERT (vm->mode ==VM_MODE_PXXV48_4K , +TEST_ASSERT ((vm->mode ==VM_MODE_PXXV48_4K ) || (vm->mode ==VM_MODE_PXXV48_4K_USER ),"Unknown or unsupported guest mode, mode: 0x%x" , vm->mode); TEST_ASSERT((vaddr % pg_size) == 0,
@@ -222,6 +224,8 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level)TEST_ASSERT (!(*pte &PTE_PRESENT_MASK ),"PTE already present for 4k page at vaddr: 0x%lx\n" , vaddr); *pte =PTE_PRESENT_MASK |PTE_WRITABLE_MASK | (paddr &PHYSICAL_PAGE_MASK ); +if (vm->mode ==VM_MODE_PXXV48_4K_USER ) + *pte |=PTE_USER_MASK ; }void virt_arch_pg_map (struct kvm_vm *vm,uint64_t vaddr,uint64_t paddr)
@@ -268,8 +272,8 @@ uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr,TEST_ASSERT (*level >=PG_LEVEL_NONE && *level <PG_LEVEL_NUM ,"Invalid PG_LEVEL_* '%d'" , *level); -TEST_ASSERT (vm->mode ==VM_MODE_PXXV48_4K ,"Attempt to use " -"unknown or unsupported guest mode, mode: 0x%x" , vm->mode); +TEST_ASSERT ((vm->mode ==VM_MODE_PXXV48_4K ) || (vm->mode ==VM_MODE_PXXV48_4K_USER ), +"Attempt to use unknown or unsupported guest mode, mode: 0x%x" , vm->mode); TEST_ASSERT(sparsebit_is_set(vm->vpages_valid, (vaddr >> vm->page_shift)), "Invalid virtual address, vaddr: 0x%lx",
@@ -536,6 +540,7 @@ static void vcpu_setup(struct kvm_vm *vm, struct kvm_vcpu *vcpu)switch (vm->mode) {case VM_MODE_PXXV48_4K : +case VM_MODE_PXXV48_4K_USER : sregs.cr0 =X86_CR0_PE |X86_CR0_NE |X86_CR0_PG ; sregs.cr4 |=X86_CR4_PAE |X86_CR4_OSFXSR ; sregs.efer |= (EFER_LME |EFER_LMA |EFER_NX );
tools/testing/selftests/kvm/lib/x86_64/vmx.c +2 -2
@@ -403,8 +403,8 @@ void __nested_pg_map(struct vmx_pages *vmx, struct kvm_vm *vm,struct eptPageTableEntry *pt = vmx->eptp_hva, *pte;uint16_t index; -TEST_ASSERT (vm->mode ==VM_MODE_PXXV48_4K ,"Attempt to use " -"unknown or unsupported guest mode, mode: 0x%x" , vm->mode); +TEST_ASSERT ((vm->mode ==VM_MODE_PXXV48_4K ) || (vm->mode ==VM_MODE_PXXV48_4K_USER ), +"Attempt to use unknown or unsupported guest mode, mode: 0x%x" , vm->mode); TEST_ASSERT((nested_paddr >> 48) == 0, "Nested physical address 0x%lx requires 5-level paging",
Signature · 1 line
2.42.0
PATCH 23/23 KVM: selftests: Add fred exception tests
In reply to Xin LiFrom: Shan Kang <[email protected]>
Add tests for FRED event data and VMX nested-exception.
FRED is designed to save a complete event context in its stack frame, e.g., FRED saves the faulting linear address of a #PF into a 64-bit event data field defined in FRED stack frame. As such, FRED VMX adds event data handling during VMX transitions.
FRED introduces event stack levels to dispatch an event handler onto a stack baesd on current stack level and stack levels defined in IA32_FRED_STKLVLS MSR for each exception vector. VMX nested-exception support ensures a correct event stack level is chosen when a VM entry injects a nested exception, which is "regarded" as occurred in ring 0.
--- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/include/x86_64/processor.h | 29 ++ .../testing/selftests/kvm/x86_64/fred_test.c | 262 ++++++++++++++++++ 3 files changed, 292 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/fred_test.c
tools/testing/selftests/kvm/Makefile +1 -0
@@ -76,6 +76,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/get_msr_index_features TEST_GEN_PROGS_x86_64 += x86_64/exit_on_emulation_failure_test TEST_GEN_PROGS_x86_64 += x86_64/fix_hypercall_test TEST_GEN_PROGS_x86_64 += x86_64/hwcr_msr_test +TEST_GEN_PROGS_x86_64 += x86_64/fred_test TEST_GEN_PROGS_x86_64 += x86_64/hyperv_clock TEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid TEST_GEN_PROGS_x86_64 += x86_64/hyperv_evmcs
tools/testing/selftests/kvm/include/x86_64/processor.h +29 -0
@@ -1260,4 +1260,33 @@ void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,#define PFERR_GUEST_PAGE_MASK BIT_ULL(PFERR_GUEST_PAGE_BIT)#define PFERR_IMPLICIT_ACCESS BIT_ULL(PFERR_IMPLICIT_ACCESS_BIT) +/* +* FRED related data structures and functions +*/ +struct fred_stack { +u64 r15; +u64 r14; +u64 r13; +u64 r12; +u64 bp; +u64 bx; +u64 r11; +u64 r10; +u64 r9; +u64 r8; +u64 ax; +u64 cx; +u64 dx; +u64 si; +u64 di; +u64 error_code; +u64 ip; +u64 csx; +u64 flags; +u64 sp; +u64 ssx; +u64 event_data; +u64 reserved; +}; +#endif /* SELFTEST_KVM_PROCESSOR_H */
tools/testing/selftests/kvm/x86_64/fred_test.c new file +262 -0
262 lines not on this page · raw message
Signature · 1 line
2.42.0
Re: [PATCH v1 15/23] KVM: nVMX: Add support for the secondary VM exit controls
In reply to Xin LiQuoted 37 lines · Xin Li
Enable the secondary VM exit controls to prepare for nested FRED.
Tested-by Shan Kang <[email protected]>Signed-off-by Xin Li <[email protected]>--- Documentation/virt/kvm/x86/nested-vmx.rst | 1 + arch/x86/include/asm/hyperv-tlfs.h | 1 + arch/x86/kvm/vmx/capabilities.h | 1 + arch/x86/kvm/vmx/hyperv.c | 18 +++++++++++++++++- arch/x86/kvm/vmx/nested.c | 18 +++++++++++++++++- arch/x86/kvm/vmx/vmcs12.c | 1 + arch/x86/kvm/vmx/vmcs12.h | 2 ++ arch/x86/kvm/x86.h | 2 +- 8 files changed, 41 insertions(+), 3 deletions(-)Documentation/virt/kvm/x86/nested-vmx.rst +1
-0@@ -217,6 +217,7 @@ struct shadow_vmcs is ever changed. u16 host_fs_selector; u16 host_gs_selector; u16 host_tr_selector; + u64 secondary_vm_exit_controls; };arch/x86/include/asm/hyperv-tlfs.h +1
-0@@ -616,6 +616,7 @@ struct hv_enlightened_vmcs {u64 host_ssp;u64 host_ia32_int_ssp_table_addr;u64 padding64_6; +u64 secondary_vm_exit_controls; } __packed;
Quoted 1 line
Quoted 1 line
#define
HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE0
Hi Xin Li,
The comment at the top of hyperv-tlfs.h says:
"This file contains definitions from Hyper-V Hypervisor Top-Level Functional Specification (TLFS)"
These struct definitions are shared with the hypervisor, so you can't just add fields to it. Same comment for patch 16.
Would FRED work in nested virtualization if the L0 hypervisor does not support it (or doesn't know about it)?
Thanks,
Jeremi
Re: [PATCH v1 05/23] KVM: VMX: Initialize FRED VM entry/exit controls in vmcs_config
In reply to Xin LiQuoted 50 lines · Xin Li
Setup the global
vmcs_configfor FRED:
1) AddVM_ENTRY_LOAD_IA32_FREDtoKVM_OPTIONAL_VMX_VM_ENTRY_CONTROLSto have a FRED CPU load guest FRED MSRs from VMCS upon VM entry.
2) AddSECONDARY_VM_EXIT_SAVE_IA32_FREDtoKVM_OPTIONAL_VMX_SECONDARY_VM_EXIT_CONTROLSto have a FRED CPU save guest FRED MSRs to VMCS during VM exit.
3) addSECONDARY_VM_EXIT_LOAD_IA32_FREDtoKVM_OPTIONAL_VMX_SECONDARY_VM_EXIT_CONTROLSto have a FRED CPU load host FRED MSRs from VMCS during VM exit.Also add sanity checks to make sure FRED VM entry/exit controls can be set on a FRED CPU.
Tested-by Shan Kang <[email protected]>Signed-off-by Xin Li <[email protected]>---
arch/x86/include/asm/vmx.h | 3 +++ arch/x86/kvm/vmx/vmx.c | 19 ++++++++++++++++++- arch/x86/kvm/vmx/vmx.h | 7 +++++-- 3 files changed, 26 insertions(+), 3 deletions(-)arch/x86/include/asm/vmx.h +3
-0@@ -106,6 +106,8 @@#define VM_EXIT_PT_CONCEAL_PIP 0x01000000#define VM_EXIT_CLEAR_IA32_RTIT_CTL 0x02000000#define VM_EXIT_ACTIVATE_SECONDARY_CONTROLS 0x80000000 +#define SECONDARY_VM_EXIT_SAVE_IA32_FRED 0x00000001 +#define SECONDARY_VM_EXIT_LOAD_IA32_FRED 0x00000002#define VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR 0x00036dff@@ -119,6 +121,7 @@#define VM_ENTRY_LOAD_BNDCFGS 0x00010000#define VM_ENTRY_PT_CONCEAL_PIP 0x00020000#define VM_ENTRY_LOAD_IA32_RTIT_CTL 0x00040000 +#define VM_ENTRY_LOAD_IA32_FRED 0x00800000#define VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR 0x000011ffarch/x86/kvm/vmx/vmx.c +0
-0@@ -2694,10 +2694,27 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf, _vmexit_control &= ~x_ctrl; }
arch/x86/kvm/vmx/vmx.c static int setup_vmcs_config(struct vmcs_config *vmcs_conf, · line 2705
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +6
-1-if (_vmexit_control &VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ) +if (_vmexit_control &VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ) { _secondary_vmexit_control =adjust_vmx_controls64 (KVM_OPTIONAL_VMX_SECONDARY_VM_EXIT_CONTROLS ,MSR_IA32_VMX_EXIT_CTLS2 ); +if (cpu_feature_enabled (X86_FEATURE_FRED ) && + !(_secondary_vmexit_control &SECONDARY_VM_EXIT_SAVE_IA32_FRED && + _secondary_vmexit_control &SECONDARY_VM_EXIT_LOAD_IA32_FRED )) { +pr_warn_once ("FRED enabled but no VMX VM-Exit {SAVE,LOAD}_IA32_FRED controls: %llx\n" , + _secondary_vmexit_control);
if there is no VM_EXIT_ACTIVATE_SECONDARY_CONTROLS, shouldn't we also emit this warning?
arch/x86/kvm/vmx/vmx.c static int setup_vmcs_config(struct vmcs_config *vmcs_conf, · line 2714
Quoted 9 lines
arch/x86/kvm/vmx/vmx.c +9
-0+if (error_on_inconsistent_vmcs_config) +return -EIO ; + } + } + +if (cpu_feature_enabled (X86_FEATURE_FRED ) && + !(_vmentry_control &VM_ENTRY_LOAD_IA32_FRED )) { +pr_warn_once ("FRED enabled but no VMX VM-Entry LOAD_IA32_FRED control: %x\n" , + _vmentry_control);
Can we just hide FRED from guests like what KVM does for other features which have similar dependencies? see vmx_set_cpu_caps().
Re: [PATCH v1 06/23] KVM: VMX: Defer enabling FRED MSRs save/load until after set CPUID
In reply to Xin LiQuoted 59 lines · Xin Li
Clear FRED VM entry/exit controls when initializing a vCPU, and set these controls only if FRED is enumerated after set CPUID.
FRED VM entry/exit controls need to be set to establish context sufficient to support FRED event delivery immediately after VM entry and exit. However it is not required to save/load FRED MSRs for a non-FRED guest, which aren't supposed to access FRED MSRs.
A non-FRED guest should get #GP upon accessing FRED MSRs, otherwise it corrupts host FRED MSRs.
Tested-by Shan Kang <[email protected]>Signed-off-by Xin Li <[email protected]>---
arch/x86/kvm/vmx/vmx.c| 34 +++++++++++++++++++++++++++++++++-1 file changed, 33 insertions(+), 1 deletion(-)arch/x86/kvm/vmx/vmx.c +19
-1@@ -4423,6 +4423,9 @@ static u32 vmx_vmentry_ctrl(void)if (cpu_has_perf_global_ctrl_bug ()) vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL ; +/* Whether to load guest FRED MSRs is deferred until after set CPUID */ + vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_FRED ; +return vmentry_ctrl; }@@ -4458,7 +4461,13 @@ static u32 vmx_vmexit_ctrl(void)static u64 vmx_secondary_vmexit_ctrl (void ) { -return vmcs_config.secondary_vmexit_ctrl; +u64 secondary_vmexit_ctrl = vmcs_config.secondary_vmexit_ctrl; + +/* Whether to save/load FRED MSRs is deferred until after set CPUID */ + secondary_vmexit_ctrl &= ~(SECONDARY_VM_EXIT_SAVE_IA32_FRED | +SECONDARY_VM_EXIT_LOAD_IA32_FRED ); + +return secondary_vmexit_ctrl; }static void vmx_refresh_apicv_exec_ctrl (struct kvm_vcpu *vcpu)@@ -7785,10 +7794,33 @@ static void update_intel_pt_cfg(struct kvm_vcpu *vcpu) vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i *4 )); } +static void vmx_vcpu_config_fred_after_set_cpuid (struct kvm_vcpu *vcpu) +{ +struct vcpu_vmx *vmx =to_vmx (vcpu); + +if (!cpu_feature_enabled (X86_FEATURE_FRED ) || + !guest_cpuid_has (vcpu,X86_FEATURE_FRED )) +return ; + +/* Enable loading guest FRED MSRs from VMCS */
arch/x86/kvm/vmx/vmx.c static void update_intel_pt_cfg(struct kvm_vcpu *vcpu) · line 7815
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10
-0+vm_entry_controls_setbit (vmx,VM_ENTRY_LOAD_IA32_FRED ); + +/* +* Enable saving guest FRED MSRs into VMCS and loading host FRED MSRs +* from VMCS. +*/ +vm_exit_controls_setbit (vmx,VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ); +secondary_vm_exit_controls_setbit (vmx, +SECONDARY_VM_EXIT_SAVE_IA32_FRED | +SECONDARY_VM_EXIT_LOAD_IA32_FRED );
all above vmcs controls need to be cleared if guest doesn't enumerate FRED, see
https://lore.kernel.org/all/[email protected]/
Clearing VM_EXIT_ACTIVATE_SECONDARY_CONTROLS may be problematic when new bits are added to secondary vmcs controls. Why not keep VM_EXIT_ACTIVATE_SECONDARY_CONTROLS always on if it is supported? or you see any perf impact?
Quoted 15 lines
arch/x86/kvm/vmx/vmx.c +4
-0+} +static void vmx_vcpu_after_set_cpuid (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu); +vmx_vcpu_config_fred_after_set_cpuid (vcpu); +/* * XSAVES is effectively enabled if and only if XSAVE is also exposed * to the guest. XSAVES depends on CR4.OSXSAVE, and CR4.OSXSAVE can be Signature · 1 line
2.42.0
Re: [PATCH v1 07/23] KVM: VMX: Disable intercepting FRED MSRs
In reply to Xin LiQuoted 27 lines · Xin Li
Add FRED MSRs to the valid passthrough MSR list and disable intercepting FRED MSRs only if FRED is enumerated after set CPUID.
Tested-by Shan Kang <[email protected]>Signed-off-by Xin Li <[email protected]>---
arch/x86/kvm/vmx/vmx.c| 13 +++++++++++++1 file changed, 13 insertions(+)arch/x86/kvm/vmx/vmx.c +3
-0@@ -700,6 +700,9 @@ static bool is_valid_passthrough_msr(u32 msr) case MSR_LBR_CORE_TO ... MSR_LBR_CORE_TO + 8:/* LBR MSRs. These are handled in vmx_update_intercept_for_lbr_msrs() */ return true; + case MSR_IA32_FRED_RSP0 ... MSR_IA32_FRED_CONFIG: +/* FRED MSRs should be passthrough to FRED guests only */ +return true; } r =possible_passthrough_msr_slot (msr) != -ENOENT ;@@ -7813,6 +7816,16 @@ static void vmx_vcpu_config_fred_after_set_cpuid(struct kvm_vcpu *vcpu)secondary_vm_exit_controls_setbit (vmx,SECONDARY_VM_EXIT_SAVE_IA32_FRED |SECONDARY_VM_EXIT_LOAD_IA32_FRED );
arch/x86/kvm/vmx/vmx.c static void vmx_vcpu_config_fred_after_set_cpuid(struct kvm_vcpu *vcpu) · line 7828
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10
-0+ +vmx_disable_intercept_for_msr (vcpu,MSR_IA32_FRED_RSP0 ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (vcpu,MSR_IA32_FRED_RSP1 ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (vcpu,MSR_IA32_FRED_RSP2 ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (vcpu,MSR_IA32_FRED_RSP3 ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (vcpu,MSR_IA32_FRED_STKLVLS ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (vcpu,MSR_IA32_FRED_SSP1 ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (vcpu,MSR_IA32_FRED_SSP2 ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (vcpu,MSR_IA32_FRED_SSP3 ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (vcpu,MSR_IA32_FRED_CONFIG ,MSR_TYPE_RW );
this has the same issue as patch 6.
Sean Christopherson
maintainerRe: [PATCH v1 05/23] KVM: VMX: Initialize FRED VM entry/exit controls in vmcs_config
In reply to Chao GaoQuoted 65 lines · Chao Gao
Quoted 50 lines · Xin Li
Setup the global
vmcs_configfor FRED:
1) AddVM_ENTRY_LOAD_IA32_FREDtoKVM_OPTIONAL_VMX_VM_ENTRY_CONTROLSto have a FRED CPU load guest FRED MSRs from VMCS upon VM entry.
2) AddSECONDARY_VM_EXIT_SAVE_IA32_FREDtoKVM_OPTIONAL_VMX_SECONDARY_VM_EXIT_CONTROLSto have a FRED CPU save guest FRED MSRs to VMCS during VM exit.
3) addSECONDARY_VM_EXIT_LOAD_IA32_FREDtoKVM_OPTIONAL_VMX_SECONDARY_VM_EXIT_CONTROLSto have a FRED CPU load host FRED MSRs from VMCS during VM exit.Also add sanity checks to make sure FRED VM entry/exit controls can be set on a FRED CPU.
Tested-by Shan Kang <[email protected]>Signed-off-by Xin Li <[email protected]>---
arch/x86/include/asm/vmx.h | 3 +++ arch/x86/kvm/vmx/vmx.c | 19 ++++++++++++++++++- arch/x86/kvm/vmx/vmx.h | 7 +++++-- 3 files changed, 26 insertions(+), 3 deletions(-)arch/x86/include/asm/vmx.h +3
-0@@ -106,6 +106,8 @@#define VM_EXIT_PT_CONCEAL_PIP 0x01000000#define VM_EXIT_CLEAR_IA32_RTIT_CTL 0x02000000#define VM_EXIT_ACTIVATE_SECONDARY_CONTROLS 0x80000000 +#define SECONDARY_VM_EXIT_SAVE_IA32_FRED 0x00000001 +#define SECONDARY_VM_EXIT_LOAD_IA32_FRED 0x00000002#define VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR 0x00036dff@@ -119,6 +121,7 @@#define VM_ENTRY_LOAD_BNDCFGS 0x00010000#define VM_ENTRY_PT_CONCEAL_PIP 0x00020000#define VM_ENTRY_LOAD_IA32_RTIT_CTL 0x00040000 +#define VM_ENTRY_LOAD_IA32_FRED 0x00800000#define VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR 0x000011ffarch/x86/kvm/vmx/vmx.c +0
-0@@ -2694,10 +2694,27 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf, _vmexit_control &= ~x_ctrl; }arch/x86/kvm/vmx/vmx.c static int setup_vmcs_config(struct vmcs_config *vmcs_conf, · line 2705
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +6
-1-if (_vmexit_control &VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ) +if (_vmexit_control &VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ) { _secondary_vmexit_control =adjust_vmx_controls64 (KVM_OPTIONAL_VMX_SECONDARY_VM_EXIT_CONTROLS ,MSR_IA32_VMX_EXIT_CTLS2 ); +if (cpu_feature_enabled (X86_FEATURE_FRED ) && + !(_secondary_vmexit_control &SECONDARY_VM_EXIT_SAVE_IA32_FRED && + _secondary_vmexit_control &SECONDARY_VM_EXIT_LOAD_IA32_FRED )) { +pr_warn_once ("FRED enabled but no VMX VM-Exit {SAVE,LOAD}_IA32_FRED controls: %llx\n" , + _secondary_vmexit_control);if there is no
VM_EXIT_ACTIVATE_SECONDARY_CONTROLS, shouldn't we also emit this warning?
Quoted 12 lines
arch/x86/kvm/vmx/vmx.c static int setup_vmcs_config(struct vmcs_config *vmcs_conf, · line 2714
Quoted 9 lines
arch/x86/kvm/vmx/vmx.c +9
-0+if (error_on_inconsistent_vmcs_config) +return -EIO ; + } + } + +if (cpu_feature_enabled (X86_FEATURE_FRED ) && + !(_vmentry_control &VM_ENTRY_LOAD_IA32_FRED )) { +pr_warn_once ("FRED enabled but no VMX VM-Entry LOAD_IA32_FRED control: %x\n" , + _vmentry_control);Can we just hide FRED from guests like what KVM does for other features which have similar dependencies? see
vmx_set_cpu_caps().
Both of these warnings should simply be dropped. The error_on_inconsistent_vmcs_config stuff is for inconsistencies within the allowed VMCS fields. Having a feature that is supported in bare metal but not virtualized is perfectly legal, if uncommon.
What *is* needed is for KVM to refuse to virtualize FRED if the entry/exit controls aren't consistent. E.g. if at least one control is present, and at least one
control is missing. I.e. KVM needs a version of vmcs_entry_exit_pairs that can
deal with SECONDAY_VM_EXIT controls. I'll circle back to this when I give the series a proper review, which is going to be 3+ weeks.
RE: [PATCH v1 06/23] KVM: VMX: Defer enabling FRED MSRs save/load until after set CPUID
In reply to Chao GaoQuoted 20 lines
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +6
-0+static void vmx_vcpu_config_fred_after_set_cpuid (struct kvm_vcpu *vcpu) +{ +struct vcpu_vmx *vmx =to_vmx (vcpu); + +if (!cpu_feature_enabled (X86_FEATURE_FRED ) || + !guest_cpuid_has (vcpu,X86_FEATURE_FRED ))arch/x86/kvm/vmx/vmx.c static void update_intel_pt_cfg(struct kvm_vcpu *vcpu)
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10
-0+return ; + +/* Enable loading guest FRED MSRs from VMCS */ +vm_entry_controls_setbit (vmx,VM_ENTRY_LOAD_IA32_FRED ); + +/* +* Enable saving guest FRED MSRs into VMCS and loading host FRED MSRs +* from VMCS. +*/ + vm_exit_controls_setbit(vmx,
VM_EXIT_ACTIVATE_SECONDARY_CONTROLS);Review
Quoted 2 lines
(quoted diff) +2
-0+ secondary_vm_exit_controls_setbit(vmx, + SECONDARY_VM_EXIT_SAVE_IA32_FRED|
Quoted 6 lines
Quoted 1 line
+
SECONDARY_VM_EXIT_LOAD_IA32_FRED);all above vmcs controls need to be cleared if guest doesn't enumerate FRED, see
Good point, the user space could set cpuid multiple times...
Quoted 4 lines
Clearing
VM_EXIT_ACTIVATE_SECONDARY_CONTROLSmay be problematic when new bits are added to secondary vmcs controls. Why not keepVM_EXIT_ACTIVATE_SECONDARY_CONTROLSalways on if it is supported? or you see any perf impact?
I think it from the other way, why keeps hw loading it on every vmentry even if it's not used by a guest?
Different CPUs may implement it in different ways, which we can't assume.
Other features needing it should set it separately, say with a refcount.
RE: [PATCH v1 05/23] KVM: VMX: Initialize FRED VM entry/exit controls in vmcs_config
In reply to Sean ChristophersonQuoted 9 lines
Quoted 3 lines
arch/x86/kvm/vmx/vmx.c static int setup_vmcs_config(struct vmcs_config *vmcs_conf,
Quoted 3 lines
arch/x86/kvm/vmx/vmx.c +3
-0+if (cpu_feature_enabled (X86_FEATURE_FRED ) && + !(_vmentry_control &VM_ENTRY_LOAD_IA32_FRED )) { + pr_warn_once("FRED enabled but no VMX VM-Entry
LOAD_IA32_FREDcontrol: %x\n",Quoted 4 lines
arch/x86/kvm/vmx/vmx.c static int setup_vmcs_config(struct vmcs_config *vmcs_conf, · line 2714
Quoted 1 line
arch/x86/kvm/vmx/vmx.c +1
-0+ _vmentry_control);Can we just hide FRED from guests like what KVM does for other features which have similar dependencies? see
vmx_set_cpu_caps().
Quoted 4 lines
Both of these warnings should simply be dropped. The
error_on_inconsistent_vmcs_configstuff is for inconsistencies within the allowed VMCS fields. Having a feature that is supported in bare metal but not virtualized is perfectly legal, if uncommon.
I deliberately keep it, at least for now, because these checks are helpful during the development of nVMX FRED. It will be helpful for other VMMs being developed/tested on KVM.
Quoted 4 lines
What *is* needed is for KVM to refuse to virtualize FRED if the entry/exit controls aren't consistent. E.g. if at least one control is present, and at least one
control is missing. I.e. KVM needs a version ofvmcs_entry_exit_pairsthat can
deal withSECONDAY_VM_EXITcontrols.
I agree there are better ways. But maybe after or before VMX FRED.
Quoted 2 lines
I'll circle back to this when I give the
series a proper review, which is going to be 3+ weeks.
The traffic in KVM mailing list is surprisingly high recently. So that is totally expected.
RE: [PATCH v1 15/23] KVM: nVMX: Add support for the secondary VM exit controls
In reply to Jeremi PiotrowskiQuoted 15 lines
Quoted 11 lines
arch/x86/include/asm/hyperv-tlfs.h +1
-0@@ -616,6 +616,7 @@ struct hv_enlightened_vmcs {u64 host_ssp;u64 host_ia32_int_ssp_table_addr;u64 padding64_6; +u64 secondary_vm_exit_controls; } __packed;Quoted 1 line
Quoted 1 line
#define
HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE0Hi Xin Li,
Quoted 7 lines
The comment at the top of
hyperv-tlfs.hsays:
"This file contains definitions from Hyper-V Hypervisor Top-Level Functional Specification (TLFS)"These struct definitions are shared with the hypervisor, so you can't just add fields to it.
Same comment for patch 16.
I tried not to touch any hyperv stuff but hit some problems.
Quoted 3 lines
Would FRED work in nested virtualization if the L0 hypervisor does not support it (or doesn't know about it)?
I don't think so AFAICT. But I could be wrong, say a VMM implements FRED completely in software. Otherwise L0 needs to add code to have hardware to switch FRED context between host and guest, which can't be delayed.
Thanks a lot for your quick review!
Sean Christopherson
maintainerRe: [PATCH v1 06/23] KVM: VMX: Defer enabling FRED MSRs save/load until after set CPUID
In reply to Li, Xin3Quoted 29 lines · On Thu, Nov 09, 2023, Xin3 Li wrote:
Quoted 20 lines
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +6
-0+static void vmx_vcpu_config_fred_after_set_cpuid (struct kvm_vcpu *vcpu) +{ +struct vcpu_vmx *vmx =to_vmx (vcpu); + +if (!cpu_feature_enabled (X86_FEATURE_FRED ) || + !guest_cpuid_has (vcpu,X86_FEATURE_FRED ))arch/x86/kvm/vmx/vmx.c static void update_intel_pt_cfg(struct kvm_vcpu *vcpu)
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10
-0+return ; + +/* Enable loading guest FRED MSRs from VMCS */ +vm_entry_controls_setbit (vmx,VM_ENTRY_LOAD_IA32_FRED ); + +/* +* Enable saving guest FRED MSRs into VMCS and loading host FRED MSRs +* from VMCS. +*/ + vm_exit_controls_setbit(vmx,
VM_EXIT_ACTIVATE_SECONDARY_CONTROLS);Review
Quoted 2 lines
(quoted diff) +2
-0+ secondary_vm_exit_controls_setbit(vmx, + SECONDARY_VM_EXIT_SAVE_IA32_FRED|
Quoted 6 lines
Quoted 1 line
+
SECONDARY_VM_EXIT_LOAD_IA32_FRED);all above vmcs controls need to be cleared if guest doesn't enumerate FRED, see
Good point, the user space could set cpuid multiple times...
Quoted 7 lines
Quoted 4 lines
Clearing
VM_EXIT_ACTIVATE_SECONDARY_CONTROLSmay be problematic when new bits are added to secondary vmcs controls. Why not keepVM_EXIT_ACTIVATE_SECONDARY_CONTROLSalways on if it is supported? or you see any perf impact?I think it from the other way, why keeps hw loading it on every vmentry even if it's not used by a guest?
Oh, yeesh, this is clearing the activation control. Yeah, NAK to that, just leave it set. If it weren't for the fact that there is apparently a metrict ton of FRED state (I thought the whole point of FRED was to kill off legacy crap like CPL1 and CPL2 stacks?) _and_ that KVM needs to toggle MSR intercepts, I'd probably push back on toggling even the FRED controls.
Quoted 1 line
Different CPUs may implement it in different ways, which we can't assume.
Implement what in a different way? The VMCS fields and FRED are architectural. The internal layout of the VMCS is uarch specific, but the encodings and semantics absolutely cannot change without breaking software. And if Intel does something asinine like make a control active-low then we have far, far bigger problems.
Quoted 1 line
Other features needing it should set it separately, say with a refcount.
Absolutely not. Unless Intel screwed up the implementation, the cost of leaving VM_EXIT_ACTIVATE_SECONDARY_CONTROLS set when it's supported shouldn't even be measurable.
Sean Christopherson
maintainerRe: [PATCH v1 05/23] KVM: VMX: Initialize FRED VM entry/exit controls in vmcs_config
In reply to Li, Xin3Quoted 9 lines · On Fri, Nov 10, 2023, Xin3 Li wrote:
Quoted 9 lines
Quoted 3 lines
arch/x86/kvm/vmx/vmx.c static int setup_vmcs_config(struct vmcs_config *vmcs_conf,
Quoted 3 lines
arch/x86/kvm/vmx/vmx.c +3
-0+if (cpu_feature_enabled (X86_FEATURE_FRED ) && + !(_vmentry_control &VM_ENTRY_LOAD_IA32_FRED )) { + pr_warn_once("FRED enabled but no VMX VM-Entry
LOAD_IA32_FREDcontrol: %x\n",Quoted 4 lines
arch/x86/kvm/vmx/vmx.c static int setup_vmcs_config(struct vmcs_config *vmcs_conf, · line 2714
Quoted 1 line
arch/x86/kvm/vmx/vmx.c +1
-0+ _vmentry_control);Can we just hide FRED from guests like what KVM does for other features which have similar dependencies? see
vmx_set_cpu_caps().
Quoted 8 lines
Quoted 4 lines
Both of these warnings should simply be dropped. The
error_on_inconsistent_vmcs_configstuff is for inconsistencies within the allowed VMCS fields. Having a feature that is supported in bare metal but not virtualized is perfectly legal, if uncommon.I deliberately keep it, at least for now, because these checks are helpful during the development of nVMX FRED. It will be helpful for other VMMs being developed/tested on KVM.
No, remove it. It's architecturally legal for a CPU to support a feature in bare metal but not provide virtualization support.
Quoted 6 lines
Quoted 4 lines
What *is* needed is for KVM to refuse to virtualize FRED if the entry/exit controls aren't consistent. E.g. if at least one control is present, and at least one
control is missing. I.e. KVM needs a version ofvmcs_entry_exit_pairsthat can
deal withSECONDAY_VM_EXITcontrols.I agree there are better ways. But maybe after or before VMX FRED.
Uh, no. This is not optional. FRED is the first feature that uses SECONDAY_VM_EXIT controls, so FRED gets the honor of adding the necessary infrastructure support.
Re: [PATCH v1 08/23] KVM: VMX: Initialize VMCS FRED fields
In reply to Xin LiQuoted 60 lines · Xin Li
Initialize host VMCS FRED fields with host FRED MSRs' value and guest VMCS FRED fields to 0.
FRED CPU states are managed in 9 new FRED MSRs, as well as a few existing CPU registers and MSRs, e.g., CR4.FRED. To support FRED context management, new VMCS fields corresponding to most of FRED CPU state MSRs are added to both the host-state and guest-state areas of VMCS.
Specifically no VMCS fields are added for FRED RSP0 and SSP0 MSRs, because the 2 FRED MSRs are used during ring 3 event delivery only, thus KVM, running on ring 0, can run safely even with guest FRED RSP0 and SSP0. It can be deferred to load host FRED RSP0 and SSP0 until before returning to user level.
Tested-by Shan Kang <[email protected]>Signed-off-by Xin Li <[email protected]>---
arch/x86/include/asm/vmx.h| 16 ++++++++++++++++arch/x86/kvm/vmx/vmx.c| 32 ++++++++++++++++++++++++++++++++2 files changed, 48 insertions(+)arch/x86/include/asm/vmx.h +16
-0@@ -277,12 +277,28 @@ enum vmcs_field {GUEST_BNDCFGS_HIGH =0x00002813 ,GUEST_IA32_RTIT_CTL =0x00002814 ,GUEST_IA32_RTIT_CTL_HIGH =0x00002815 , +GUEST_IA32_FRED_CONFIG =0x0000281a , +GUEST_IA32_FRED_RSP1 =0x0000281c , +GUEST_IA32_FRED_RSP2 =0x0000281e , +GUEST_IA32_FRED_RSP3 =0x00002820 , +GUEST_IA32_FRED_STKLVLS =0x00002822 , +GUEST_IA32_FRED_SSP1 =0x00002824 , +GUEST_IA32_FRED_SSP2 =0x00002826 , +GUEST_IA32_FRED_SSP3 =0x00002828 ,HOST_IA32_PAT =0x00002c00 ,HOST_IA32_PAT_HIGH =0x00002c01 ,HOST_IA32_EFER =0x00002c02 ,HOST_IA32_EFER_HIGH =0x00002c03 ,HOST_IA32_PERF_GLOBAL_CTRL =0x00002c04 ,HOST_IA32_PERF_GLOBAL_CTRL_HIGH =0x00002c05 , +HOST_IA32_FRED_CONFIG =0x00002c08 , +HOST_IA32_FRED_RSP1 =0x00002c0a , +HOST_IA32_FRED_RSP2 =0x00002c0c , +HOST_IA32_FRED_RSP3 =0x00002c0e , +HOST_IA32_FRED_STKLVLS =0x00002c10 , +HOST_IA32_FRED_SSP1 =0x00002c12 , +HOST_IA32_FRED_SSP2 =0x00002c14 , +HOST_IA32_FRED_SSP3 =0x00002c16 ,PIN_BASED_VM_EXEC_CONTROL =0x00004000 ,CPU_BASED_VM_EXEC_CONTROL =0x00004002 , EXCEPTION_BITMAP = 0x00004004,arch/x86/kvm/vmx/vmx.c +0
-0
arch/x86/kvm/vmx/vmx.c void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu, · line 1482
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +3
-0@@ -1477,6 +1477,18 @@ void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu, (unsigned long)(cpu_entry_stack(cpu) + 1)); } +#ifdef CONFIG_X86_64 +/* Per-CPU FRED MSRs */ +if (cpu_feature_enabled (X86_FEATURE_FRED )) {
how about kvm_cpu_cap_has()? to decouple KVM's capability to virtualize a feature and host's enabling a feature.
arch/x86/kvm/vmx/vmx.c void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu, · line 1490
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +8
-0+vmcs_write64 (HOST_IA32_FRED_RSP1 ,read_msr (MSR_IA32_FRED_RSP1 )); +vmcs_write64 (HOST_IA32_FRED_RSP2 ,read_msr (MSR_IA32_FRED_RSP2 )); +vmcs_write64 (HOST_IA32_FRED_RSP3 ,read_msr (MSR_IA32_FRED_RSP3 )); +vmcs_write64 (HOST_IA32_FRED_SSP1 ,read_msr (MSR_IA32_FRED_SSP1 )); +vmcs_write64 (HOST_IA32_FRED_SSP2 ,read_msr (MSR_IA32_FRED_SSP2 )); + vmcs_write64(HOST_IA32_FRED_SSP3, read_msr(MSR_IA32_FRED_SSP3)); + } +#endif
why is this hunk enclosed in #ifdef CONFIG_X86_64 while the one below isn't?
Quoted 25 lines
arch/x86/kvm/vmx/vmx.c +11
-0+ vmx->loaded_vmcs->cpu = cpu; } }@@ -4375,6 +4387,15 @@ void vmx_set_constant_host_state(struct vcpu_vmx *vmx)if (cpu_has_load_ia32_efer ())vmcs_write64 (HOST_IA32_EFER , host_efer); + +/* +* FRED MSRs are per-cpu, however FRED CONFIG and STKLVLS MSRs +* are the same on all CPUs, thus they are initialized here. +*/ +if (cpu_feature_enabled (X86_FEATURE_FRED )) { +vmcs_write64 (HOST_IA32_FRED_CONFIG ,read_msr (MSR_IA32_FRED_CONFIG )); +vmcs_write64 (HOST_IA32_FRED_STKLVLS ,read_msr (MSR_IA32_FRED_STKLVLS )); + } }void set_cr4_guest_host_mask (struct vcpu_vmx *vmx)@@ -4936,6 +4957,17 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)vmcs_writel (GUEST_IDTR_BASE ,0 );vmcs_write32 (GUEST_IDTR_LIMIT ,0xffff ); +if (cpu_feature_enabled (X86_FEATURE_FRED )) {
arch/x86/kvm/vmx/vmx.c static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 4970
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10
-0+vmcs_write64 (GUEST_IA32_FRED_CONFIG ,0 ); +vmcs_write64 (GUEST_IA32_FRED_RSP1 ,0 ); +vmcs_write64 (GUEST_IA32_FRED_RSP2 ,0 ); +vmcs_write64 (GUEST_IA32_FRED_RSP3 ,0 ); +vmcs_write64 (GUEST_IA32_FRED_STKLVLS ,0 ); +vmcs_write64 (GUEST_IA32_FRED_SSP1 ,0 ); +vmcs_write64 (GUEST_IA32_FRED_SSP2 ,0 ); +vmcs_write64 (GUEST_IA32_FRED_SSP3 ,0 ); + } +
move this hunk to __vmx_vcpu_reset() because FRED spec says
"INIT does not change the value of the new MSRs."
Quoted 7 lines
arch/x86/kvm/vmx/vmx.c +0
-0vmcs_write32 (GUEST_ACTIVITY_STATE ,GUEST_ACTIVITY_ACTIVE );vmcs_write32 (GUEST_INTERRUPTIBILITY_INFO ,0 );vmcs_writel (GUEST_PENDING_DBG_EXCEPTIONS ,0 );Signature · 1 line
2.42.0
Re: [PATCH v1 09/23] KVM: VMX: Switch FRED RSP0 between host and guest
In reply to Xin LiQuoted 41 lines · Xin Li
Switch
MSR_IA32_FRED_RSP0between host and guest in vmx_prepare_switch_to_{host,guest}().
MSR_IA32_FRED_RSP0is used during ring 3 event delivery only, thus KVM, running on ring 0, can run safely with guest FRED RSP0, i.e., no need to switch between host/guest FRED RSP0 during VM entry and exit.KVM should switch to host FRED RSP0 before returning to user level, and switch to guest FRED RSP0 before entering guest mode.
Tested-by Shan Kang <[email protected]>Signed-off-by Xin Li <[email protected]>---
arch/x86/kvm/vmx/vmx.c| 17 +++++++++++++++++arch/x86/kvm/vmx/vmx.h| 2 ++2 files changed, 19 insertions(+)arch/x86/kvm/vmx/vmx.c +11
-0@@ -1344,6 +1344,17 @@ void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu) }wrmsrl (MSR_KERNEL_GS_BASE , vmx->msr_guest_kernel_gs_base); + +if (cpu_feature_enabled (X86_FEATURE_FRED ) && +guest_cpuid_has (vcpu,X86_FEATURE_FRED )) { +/* +* MSR_IA32_FRED_RSP0 is top of task stack, which never changes. +* Thus it should be initialized only once. +*/ +if (unlikely (vmx->msr_host_fred_rsp0 ==0 )) + vmx->msr_host_fred_rsp0 =read_msr (MSR_IA32_FRED_RSP0 ); +wrmsrl (MSR_IA32_FRED_RSP0 , vmx->msr_guest_fred_rsp0); + }#else savesegment (fs, fs_sel);savesegment (gs, gs_sel);
arch/x86/kvm/vmx/vmx.c static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx) · line 1404
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +3
-0@@ -1388,6 +1399,12 @@ static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)invalidate_tss_limit ();#ifdef CONFIG_X86_64 wrmsrl (MSR_KERNEL_GS_BASE , vmx->msr_host_kernel_gs_base); + +if (cpu_feature_enabled (X86_FEATURE_FRED ) && +guest_cpuid_has (&vmx->vcpu,X86_FEATURE_FRED )) {
IIUC, vmx_prepare_switch_to_host() is called from IRQ-disabled context. using guest_cpuid_has() in this context is not desired, see lockdep_assert_irqs_enabled() in cpuid_entry2_find().
Quoted 11 lines
arch/x86/kvm/vmx/vmx.c +3
-0+ vmx->msr_guest_fred_rsp0 =read_msr (MSR_IA32_FRED_RSP0 ); + wrmsrl(MSR_IA32_FRED_RSP0, vmx->msr_host_fred_rsp0); + }#endif load_fixmap_gdt (raw_smp_processor_id ()); vmx->guest_state_loaded = false;arch/x86/kvm/vmx/vmx.h +0
-0
arch/x86/kvm/vmx/vmx.h struct vcpu_vmx { · line 280
Quoted 5 lines
arch/x86/kvm/vmx/vmx.h +2
-0@@ -276,6 +276,8 @@ struct vcpu_vmx {#ifdef CONFIG_X86_64 u64 msr_host_kernel_gs_base;u64 msr_guest_kernel_gs_base; +u64 msr_host_fred_rsp0; +u64 msr_guest_fred_rsp0;
resetting guest fred rsp0 to 0 during vcpu reset is missing.
Quoted 7 lines
arch/x86/kvm/vmx/vmx.h +0
-0#endif u64 spec_ctrl;Signature · 1 line
2.42.0
Re: [PATCH v1 10/23] KVM: VMX: Add support for FRED context save/restore
In reply to Xin Liarch/x86/kvm/x86.c static const u32 msrs_to_save_base[] = { · line 1456
Quoted 11 lines
arch/x86/kvm/x86.c +3
-0@@ -1451,6 +1451,9 @@ static const u32 msrs_to_save_base[] = { MSR_STAR,#ifdef CONFIG_X86_64 MSR_CSTAR ,MSR_KERNEL_GS_BASE ,MSR_SYSCALL_MASK ,MSR_LSTAR , +MSR_IA32_FRED_RSP0 ,MSR_IA32_FRED_RSP1 ,MSR_IA32_FRED_RSP2 , +MSR_IA32_FRED_RSP3 ,MSR_IA32_FRED_STKLVLS ,MSR_IA32_FRED_SSP1 , + MSR_IA32_FRED_SSP2, MSR_IA32_FRED_SSP3, MSR_IA32_FRED_CONFIG,
Need to handle the case where FRED MSRs are valid but KVM cannot virtualize FRED, see kvm_probe_msr_to_save().
arch/x86/kvm/x86.c static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data, · line 1898
Quoted 10 lines
arch/x86/kvm/x86.c +3
-0#endif MSR_IA32_TSC ,MSR_IA32_CR_PAT ,MSR_VM_HSAVE_PA , MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,@@ -1890,6 +1893,16 @@ static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data, data = (u32 )data;break ; + case MSR_IA32_FRED_RSP0 ... MSR_IA32_FRED_CONFIG: +if (host_initiated ||guest_cpuid_has (vcpu,X86_FEATURE_FRED )) +break ;
Nothing guarantees FRED MSRs/VMCS fields exist on the hardware here. Probably use guest_cpu_cap_has()*.
arch/x86/kvm/x86.c static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data, · line 1904
Quoted 6 lines
arch/x86/kvm/x86.c +6
-0+ +/* +* Inject #GP upon FRED MSRs accesses from a non-FRED guest to +* make sure no malicious guest can write to FRED MSRs thus to +* corrupt host FRED MSRs. +*/
I think injecting #GP here is simply because KVM should emulate hardware behavior. To me, preventing guest from corrupting FRED MSRs is at most a byproduct. I prefer to drop the comment.
Quoted 25 lines
arch/x86/kvm/x86.c +11
-0+return 1 ; } msr.data = data;@@ -1933,6 +1946,16 @@ int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data, !guest_cpuid_has(vcpu, X86_FEATURE_RDPID))return 1 ;break ; + case MSR_IA32_FRED_RSP0 ... MSR_IA32_FRED_CONFIG: +if (host_initiated ||guest_cpuid_has (vcpu,X86_FEATURE_FRED )) +break ; + +/* +* Inject #GP upon FRED MSRs accesses from a non-FRED guest to +* make sure no malicious guest can write to FRED MSRs thus to +* corrupt host FRED MSRs. +*/ +return 1 ; } msr.index = index;Signature · 1 line
2.42.0
Re: [PATCH v1 11/23] KVM: x86: Add kvm_is_fred_enabled()
In reply to Xin LiQuoted 13 lines · Xin Li
Add
kvm_is_fred_enabled()to get if FRED is enabled on a vCPU.Tested-by Shan Kang <[email protected]>Signed-off-by Xin Li <[email protected]>---
arch/x86/kvm/kvm_cache_regs.h| 10 ++++++++++1 file changed, 10 insertions(+)arch/x86/kvm/kvm_cache_regs.h +0
-0
arch/x86/kvm/kvm_cache_regs.h static __always_inline bool kvm_is_cr4_bit_set(struct kvm_vcpu *vcpu, · line 194
Quoted 8 lines
arch/x86/kvm/kvm_cache_regs.h +5
-0@@ -187,6 +187,16 @@ static __always_inline bool kvm_is_cr4_bit_set(struct kvm_vcpu *vcpu,return !!kvm_read_cr4_bits (vcpu, cr4_bit); } +static __always_inline bool kvm_is_fred_enabled(struct kvm_vcpu *vcpu) +{ +#ifdef CONFIG_X86_64 +return cpu_feature_enabled (X86_FEATURE_FRED ) && +kvm_is_cr4_bit_set (vcpu,X86_CR4_FRED );
FRED is enabled when CR4.FRED = IA32_EFER.LMA = 1. Any reason to omit the check about long mode?
Quoted 12 lines
arch/x86/kvm/kvm_cache_regs.h +5
-0+#else +return false; +#endif +} +static inline ulong kvm_read_cr3 (struct kvm_vcpu *vcpu) {if (!kvm_register_is_available (vcpu,VCPU_EXREG_CR3 ))Signature · 1 line
2.42.0
Re: [PATCH v1 12/23] KVM: VMX: Handle FRED event data
In reply to Xin LiQuoted 62 lines · Xin Li
Set injected-event data when injecting a #PF, #DB, or #NM caused by extended feature disable using FRED event delivery, and save original-event data for being used as injected-event data.
Unlike IDT using some extra CPU register as part of an event context, e.g., %cr2 for #PF, FRED saves a complete event context in its stack frame, e.g., FRED saves the faulting linear address of a #PF into the event data field defined in its stack frame.
Thus a new VMX control field called injected-event data is added to provide the event data that will be pushed into a FRED stack frame for VM entries that inject an event using FRED event delivery. In addition, a new VM exit information field called original-event data is added to store the event data that would have saved into a FRED stack frame for VM exits that occur during FRED event delivery. After such a VM exit is handled to allow the original-event to be delivered, the data in the original-event data VMCS field needs to be set into the injected-event data VMCS field for the injection of the original event.
Tested-by Shan Kang <[email protected]>Signed-off-by Xin Li <[email protected]>---
arch/x86/include/asm/vmx.h | 4 ++ arch/x86/kvm/vmx/vmx.c | 84 +++++++++++++++++++++++++++++++++++--- arch/x86/kvm/x86.c | 10 ++++- 3 files changed, 91 insertions(+), 7 deletions(-)arch/x86/include/asm/vmx.h +4
-0@@ -253,8 +253,12 @@ enum vmcs_field {PID_POINTER_TABLE_HIGH =0x00002043 ,SECONDARY_VM_EXIT_CONTROLS =0x00002044 ,SECONDARY_VM_EXIT_CONTROLS_HIGH =0x00002045 , +INJECTED_EVENT_DATA =0x00002052 , +INJECTED_EVENT_DATA_HIGH =0x00002053 ,GUEST_PHYSICAL_ADDRESS =0x00002400 ,GUEST_PHYSICAL_ADDRESS_HIGH =0x00002401 , +ORIGINAL_EVENT_DATA =0x00002404 , +ORIGINAL_EVENT_DATA_HIGH =0x00002405 ,VMCS_LINK_POINTER =0x00002800 ,VMCS_LINK_POINTER_HIGH =0x00002801 , GUEST_IA32_DEBUGCTL = 0x00002802,arch/x86/kvm/vmx/vmx.c +6
-1@@ -1880,9 +1880,30 @@ static void vmx_inject_exception(struct kvm_vcpu *vcpu)vmcs_write32 (VM_ENTRY_INSTRUCTION_LEN , vmx->vcpu.arch.event_exit_inst_len); intr_info |=INTR_TYPE_SOFT_EXCEPTION ; - }else + }else { intr_info |=INTR_TYPE_HARD_EXCEPTION ; +if (kvm_is_fred_enabled (vcpu)) { +u64 event_data =0 ; + +if (is_debug (intr_info)) +/*
arch/x86/kvm/vmx/vmx.c static void vmx_inject_exception(struct kvm_vcpu *vcpu) · line 1900
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10
-0+* Compared to DR6, FRED #DB event data saved on +* the stack frame have bits 4 ~ 11 and 16 ~ 31 +* inverted, i.e., +* fred_db_event_data = dr6 ^ 0xFFFF0FF0UL +*/ + event_data = vcpu->arch.dr6 ^DR6_RESERVED ; + else if (is_page_fault(intr_info)) + event_data = vcpu->arch.cr2; + else if (is_nm_fault(intr_info) && + vcpu->arch.guest_fpu.fpstate->xfd)
does this necessarily mean the #NM is caused by XFD?
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +6
-0+ event_data = vcpu->arch.guest_fpu.xfd_err; + +vmcs_write64 (INJECTED_EVENT_DATA , event_data); + } + } +vmcs_write32 (VM_ENTRY_INTR_INFO_FIELD , intr_info);vmx_clear_hlt (vcpu);
arch/x86/kvm/vmx/vmx.c static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) · line 7251
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +2
-1@@ -7226,7 +7247,8 @@ static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)static void __vmx_complete_interrupts (struct kvm_vcpu *vcpu,u32 idt_vectoring_info,int instr_len_field, -int error_code_field) +int error_code_field, +int event_data_field)
event_data_field is used to indicate whether this is a "cancel". I may think it is better to simply use a boolean e.g., bool cancel.
arch/x86/kvm/vmx/vmx.c static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, · line 7288
Quoted 11 lines
arch/x86/kvm/vmx/vmx.c +4
-0{u8 vector;int type;@@ -7260,6 +7282,37 @@ static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, vcpu->arch.event_exit_inst_len =vmcs_read32 (instr_len_field); fallthrough;case INTR_TYPE_HARD_EXCEPTION : +if (kvm_is_fred_enabled (vcpu) && event_data_field) { +/* +* Save original-event data for being used as injected-event data. +*/
Looks we also expect CPU will update CR2/DR6/XFD_ERR. this hunk looks to me just a paranoid check to ensure the cpu works as expected. if that's the case, I suggest documenting it a bit in the comment.
Quoted 13 lines
arch/x86/kvm/vmx/vmx.c +13
-0+u64 event_data =vmcs_read64 (event_data_field); + +switch (vector) { +case DB_VECTOR : +get_debugreg (vcpu->arch.dr6,6 ); +WARN_ON (vcpu->arch.dr6 != (event_data ^DR6_RESERVED )); + vcpu->arch.dr6 = event_data ^DR6_RESERVED ; +break ; +case NM_VECTOR : +if (vcpu->arch.guest_fpu.fpstate->xfd) { +rdmsrl (MSR_IA32_XFD_ERR , vcpu->arch.guest_fpu.xfd_err); +WARN_ON (vcpu->arch.guest_fpu.xfd_err != event_data); + vcpu->arch.guest_fpu.xfd_err = event_data;
arch/x86/kvm/vmx/vmx.c static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, · line 7311
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10
-0+ }else { +WARN_ON (event_data !=0 ); + } +break ; +case PF_VECTOR : +WARN_ON (vcpu->arch.cr2 != event_data); + vcpu->arch.cr2 = event_data; +break ; +default : +WARN_ON (event_data !=0 );
I am not sure if this WARN_ON() can be triggeded by nested VMX. It is legitimate for L1 VMM to inject any event w/ an event_data.
FRED spec says:
Section 5.2.1 specifies the event data that FRED event delivery of certain events saves on the stack. When FRED event delivery is used for an event injected by VM entry, the event data saved is the value of the injected-event-data field in the VMCS. This value is used instead of what is specified in Section 5.2.1 and is done for __ALL__ injected events using FRED event delivery
Quoted 31 lines
arch/x86/kvm/vmx/vmx.c +10
-3+break ; + } + } +if (idt_vectoring_info &VECTORING_INFO_DELIVER_CODE_MASK ) {u32 err =vmcs_read32 (error_code_field);kvm_requeue_exception_e (vcpu, vector, err);@@ -7279,9 +7332,11 @@ static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,static void vmx_complete_interrupts (struct vcpu_vmx *vmx) { -__vmx_complete_interrupts (&vmx->vcpu, vmx->idt_vectoring_info, +__vmx_complete_interrupts (&vmx->vcpu, + vmx->idt_vectoring_info,VM_EXIT_INSTRUCTION_LEN , -IDT_VECTORING_ERROR_CODE ); +IDT_VECTORING_ERROR_CODE , +ORIGINAL_EVENT_DATA ); }static void vmx_cancel_injection (struct kvm_vcpu *vcpu)@@ -7289,7 +7344,8 @@ static void vmx_cancel_injection(struct kvm_vcpu *vcpu)__vmx_complete_interrupts (vcpu,vmcs_read32 (VM_ENTRY_INTR_INFO_FIELD ),VM_ENTRY_INSTRUCTION_LEN , -VM_ENTRY_EXCEPTION_ERROR_CODE ); +VM_ENTRY_EXCEPTION_ERROR_CODE , +0 );vmcs_write32 (VM_ENTRY_INTR_INFO_FIELD ,0 ); }
Re: [PATCH v1 05/23] KVM: VMX: Initialize FRED VM entry/exit controls in vmcs_config
In reply to Xin LiQuoted 48 lines · Xin Li
Setup the global
vmcs_configfor FRED:
1) AddVM_ENTRY_LOAD_IA32_FREDtoKVM_OPTIONAL_VMX_VM_ENTRY_CONTROLSto have a FRED CPU load guest FRED MSRs from VMCS upon VM entry.
2) AddSECONDARY_VM_EXIT_SAVE_IA32_FREDtoKVM_OPTIONAL_VMX_SECONDARY_VM_EXIT_CONTROLSto have a FRED CPU save guest FRED MSRs to VMCS during VM exit.
3) addSECONDARY_VM_EXIT_LOAD_IA32_FREDtoKVM_OPTIONAL_VMX_SECONDARY_VM_EXIT_CONTROLSto have a FRED CPU load host FRED MSRs from VMCS during VM exit.Also add sanity checks to make sure FRED VM entry/exit controls can be set on a FRED CPU.
Tested-by Shan Kang <[email protected]>Signed-off-by Xin Li <[email protected]>--- arch/x86/include/asm/vmx.h | 3 +++ arch/x86/kvm/vmx/vmx.c | 19 ++++++++++++++++++- arch/x86/kvm/vmx/vmx.h | 7 +++++-- 3 files changed, 26 insertions(+), 3 deletions(-)arch/x86/include/asm/vmx.h +3
-0@@ -106,6 +106,8 @@#define VM_EXIT_PT_CONCEAL_PIP 0x01000000#define VM_EXIT_CLEAR_IA32_RTIT_CTL 0x02000000#define VM_EXIT_ACTIVATE_SECONDARY_CONTROLS 0x80000000 +#define SECONDARY_VM_EXIT_SAVE_IA32_FRED 0x00000001 +#define SECONDARY_VM_EXIT_LOAD_IA32_FRED 0x00000002#define VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR 0x00036dff@@ -119,6 +121,7 @@#define VM_ENTRY_LOAD_BNDCFGS 0x00010000#define VM_ENTRY_PT_CONCEAL_PIP 0x00020000#define VM_ENTRY_LOAD_IA32_RTIT_CTL 0x00040000 +#define VM_ENTRY_LOAD_IA32_FRED 0x00800000#define VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR 0x000011ffarch/x86/kvm/vmx/vmx.c +0
-0@@ -2694,10 +2694,27 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf, _vmexit_control &= ~x_ctrl;
arch/x86/kvm/vmx/vmx.c static int setup_vmcs_config(struct vmcs_config *vmcs_conf, · line 2703
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +4
-1} -if (_vmexit_control &VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ) +if (_vmexit_control &VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ) { _secondary_vmexit_control =adjust_vmx_controls64 (KVM_OPTIONAL_VMX_SECONDARY_VM_EXIT_CONTROLS ,MSR_IA32_VMX_EXIT_CTLS2 ); +if (cpu_feature_enabled (X86_FEATURE_FRED ) && + !(_secondary_vmexit_control &SECONDARY_VM_EXIT_SAVE_IA32_FRED && + _secondary_vmexit_control &SECONDARY_VM_EXIT_LOAD_IA32_FRED )) {
Can those checks actually trigger? I.e if FEATURE_FRED is set it means the CPU implements the FRED spec. According to the spec it's guaranteed that VMX_EXIT_CTLS2 will contain those bits set to 1. So aren't those checks superfluous?
arch/x86/kvm/vmx/vmx.c static int setup_vmcs_config(struct vmcs_config *vmcs_conf, · line 2712
Quoted 9 lines
arch/x86/kvm/vmx/vmx.c +9
-0+pr_warn_once ("FRED enabled but no VMX VM-Exit {SAVE,LOAD}_IA32_FRED controls: %llx\n" , + _secondary_vmexit_control); +if (error_on_inconsistent_vmcs_config) +return -EIO ; + } + } + +if (cpu_feature_enabled (X86_FEATURE_FRED ) && + !(_vmentry_control &VM_ENTRY_LOAD_IA32_FRED )) {
DITTO
<snip>
RE: [PATCH v1 06/23] KVM: VMX: Defer enabling FRED MSRs save/load until after set CPUID
In reply to Sean ChristophersonQuoted 5 lines
Quoted 5 lines
Quoted 4 lines
Clearing
VM_EXIT_ACTIVATE_SECONDARY_CONTROLSmay be problematic when new bits are added to secondary vmcs controls. Why not keepVM_EXIT_ACTIVATE_SECONDARY_CONTROLSalways on if it is supported? or you see any perf impact?
Quoted 8 lines
Quoted 2 lines
I think it from the other way, why keeps hw loading it on every vmentry even if it's not used by a guest?
Oh, yeesh, this is clearing the activation control. Yeah, NAK to that, just leave it set. If it weren't for the fact that there is apparently a metrict ton of FRED state (I thought the whole point of FRED was to kill off legacy crap like
CPL1 and CPL2 stacks?) _and_ that KVM needs to toggle MSR intercepts, I'd probably push back on toggling even the FRED controls.
To me, FRED is to _architecturally_ do the right thing for x86 event handling.
I don't think FRED's major purpose is to kill legacy craps, but longer term it paves the way for that.
Yeah, I would like to discuss whether to toggle FRED controls.
Quoted 7 lines
Quoted 2 lines
Different CPUs may implement it in different ways, which we can't assume.
Implement what in a different way? The VMCS fields and FRED are architectural. The internal layout of the VMCS is uarch specific, but the encodings and semantics absolutely cannot change without breaking software. And if Intel does something asinine like make a control active-low then we have far, far bigger problems.
I should have made it clear that I wasn't talking at the ISA level. And of course CPU uarch implementations should be transparent to software.
I mean a CPU uarch could choose to check the activation bit in the VM exit controls first and then decide whether to load the 2nd VM exit controls. While if resources allow, a CPU uarch could always load the 2nd VM exit controls.
BTW, I believe the active-low controls are really gone with new features. All new controls are all 0s by default.
Quoted 5 lines
Quoted 1 line
Other features needing it should set it separately, say with a refcount.
Absolutely not. Unless Intel screwed up the implementation, the cost of leaving
VM_EXIT_ACTIVATE_SECONDARY_CONTROLSset when it's supported shouldn't even be measurable.
I do hope so. However, I don't know whether this is guaranteed or not on all uarch implementations.
A decision to leave it set is good enough for now.
Thanks!
Xin
RE: [PATCH v1 05/23] KVM: VMX: Initialize FRED VM entry/exit controls in vmcs_config
In reply to Sean ChristophersonQuoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Can we just hide FRED from guests like what KVM does for other features which have similar dependencies? see
vmx_set_cpu_caps().Both of these warnings should simply be dropped. The
error_on_inconsistent_vmcs_configstuff is for inconsistencies within theallowed
Quoted 1 line
Quoted 1 line
VMCS fields. Having a feature that is supported in bare metal but not
virtualized
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
is perfectly legal, if uncommon.
I deliberately keep it, at least for now, because these checks are helpful during the development of nVMX FRED. It will be helpful for other VMMs being developed/tested on KVM.
No, remove it. It's architecturally legal for a CPU to support a feature in bare metal but not provide virtualization support.
Like the stage when native Linux has FRED support while KVM not yet?
Quoted 11 lines
Quoted 1 line
Quoted 1 line
What *is* needed is for KVM to refuse to virtualize FRED if the entry/exit
controls
Quoted 5 lines
Quoted 3 lines
aren't consistent. E.g. if at least one control is present, and at least one
control is missing. I.e. KVM needs a version ofvmcs_entry_exit_pairsthat can
deal withSECONDAY_VM_EXITcontrols.I agree there are better ways. But maybe after or before VMX FRED.
Uh, no. This is not optional. FRED is the first feature that uses
SECONDAY_VM_EXIT
controls, so FRED gets the honor of adding the necessary infrastructure support.
The 2nd VM exit controls is a must for FRED, so I should do it.
I think you mean the consistency checks can be done in a better way (which is not just for FRED controls consistency checks). No?
Thanks!
Xin
RE: [PATCH v1 12/23] KVM: VMX: Handle FRED event data
In reply to Chao GaoQuoted 4 lines
arch/x86/kvm/vmx/vmx.c static void vmx_inject_exception(struct kvm_vcpu *vcpu) · line 1900
Quoted 2 lines
arch/x86/kvm/vmx/vmx.c +2
-0+else if (is_nm_fault (intr_info) && + vcpu->arch.guest_fpu.fpstate->xfd)does this necessarily mean the #NM is caused by XFD?
Then the event data should be 0. Or I missed something obvious? I.e., it can be easily differentiated and we should just explicitly set it to 0.
Quoted 9 lines
Quoted 9 lines
arch/x86/kvm/vmx/vmx.c +6
-0+ event_data = vcpu->arch.guest_fpu.xfd_err; + +vmcs_write64 (INJECTED_EVENT_DATA , event_data); + } + } +vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Quoted 11 lines
arch/x86/kvm/vmx/vmx.c static void vmx_recover_nmi_blocking(struct · line 7251
Quoted 8 lines
vmx_clear_hlt(vcpu);arch/x86/kvm/vmx/vmx.c +2
-1@@ -7226,7 +7247,8 @@ static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,u32 idt_vectoring_info,int instr_len_field, -int error_code_field) +int error_code_field, +int event_data_field)
event_data_fieldis used to indicate whether this is a "cancel". I may think it is better to simply use a boolean e.g., bool cancel.
I'm fine with the idea if no objections.
Quoted 14 lines
arch/x86/kvm/vmx/vmx.c static void __vmx_complete_interrupts(struct
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +0
-0{u8 vector;int type;@@ -7260,6 +7282,37 @@ static void __vmx_complete_interrupts(struct
kvm_vcpu*vcpu,arch/x86/kvm/vmx/vmx.c static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +3
-0vcpu->arch.event_exit_inst_len =vmcs_read32 (instr_len_field); fallthrough;case INTR_TYPE_HARD_EXCEPTION : +if (kvm_is_fred_enabled (vcpu) && event_data_field) { +/* +* Save original-event data for being used as injected- event data.
Quoted 5 lines
Review static void __vmx_complete_interrupts(struct · line 7288
Quoted 1 line
(quoted diff) +1
-0+ */Looks we also expect CPU will update CR2/DR6/XFD_ERR. this hunk looks to me just a paranoid check to ensure the cpu works as expected. if that's the case, I suggest documenting it a bit in the comment.
These checks are not intended for hw, they make sure nVMX FRED is correctly implemented and catch regressions.
And yes, in the early stage, I prefer to be paranoid.
Quoted 32 lines
arch/x86/kvm/vmx/vmx.c static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Quoted 7 lines
arch/x86/kvm/vmx/vmx.c +6
-0+u64 event_data =vmcs_read64 (event_data_field); + +switch (vector) { +case DB_VECTOR : +get_debugreg (vcpu->arch.dr6,6 ); + WARN_ON(vcpu->arch.dr6 != (event_data ^
DR6_RESERVED));Review static void __vmx_complete_interrupts(struct · line 7303
Quoted 9 lines
(quoted diff) +7
-0+ vcpu->arch.dr6 = event_data ^ DR6_RESERVED; + break; + case NM_VECTOR: + if (vcpu->arch.guest_fpu.fpstate->xfd) { + rdmsrl(MSR_IA32_XFD_ERR, vcpu- arch.guest_fpu.xfd_err); + WARN_ON(vcpu- arch.guest_fpu.xfd_err != event_data); + vcpu->arch.guest_fpu.xfd_err =
event_data;arch/x86/kvm/vmx/vmx.c static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, · line 7311
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10
-0+ }else { +WARN_ON (event_data !=0 ); + } +break ; +case PF_VECTOR : +WARN_ON (vcpu->arch.cr2 != event_data); + vcpu->arch.cr2 = event_data; +break ; +default : +WARN_ON (event_data !=0 );I am not sure if this
WARN_ON()can be triggeded by nested VMX. It is legitimate for L1 VMM to inject any event w/ anevent_data.
Quoted 7 lines
FRED spec says:
Section 5.2.1 specifies the event data that FRED event delivery of certain events saves on the stack. When FRED event delivery is used for an event injected by VM entry, the event data saved is the value of the injected-event-data field in the VMCS. This value is used instead of what is specified in Section 5.2.1 and is done for
__ALL__injected events using FRED event delivery
5.2.1 Saving Information on the Regular Stack also says:
- For any other event, the event data are not currently defined and will be zero until they are.
Or you mean something else?
RE: [PATCH v1 11/23] KVM: x86: Add kvm_is_fred_enabled()
In reply to Chao GaoQuoted 5 lines
arch/x86/kvm/kvm_cache_regs.h static __always_inline bool kvm_is_cr4_bit_set(struct kvm_vcpu *vcpu, · line 194
Quoted 2 lines
arch/x86/kvm/kvm_cache_regs.h +2
-0+return cpu_feature_enabled (X86_FEATURE_FRED ) && +kvm_is_cr4_bit_set (vcpu,X86_CR4_FRED );FRED is enabled when CR4.FRED = IA32_EFER.LMA = 1. Any reason to omit the check about long mode?
It won' t allow CR4.FRED to be set if not in long mode, I don't expect it at runtime. Or you have one?
If you are talking about save/restore a corrupted vCPU state, a following VM entry should fail anyway.
RE: [PATCH v1 10/23] KVM: VMX: Add support for FRED context save/restore
In reply to Chao GaoQuoted 8 lines
arch/x86/kvm/x86.c static const u32 msrs_to_save_base[] = { · line 1456
Quoted 5 lines
#ifdef
CONFIG_X86_64
MSR_CSTAR,MSR_KERNEL_GS_BASE,MSR_SYSCALL_MASK,MSR_LSTAR,arch/x86/kvm/x86.c +3
-0+MSR_IA32_FRED_RSP0 ,MSR_IA32_FRED_RSP1 ,MSR_IA32_FRED_RSP2 , +MSR_IA32_FRED_RSP3 ,MSR_IA32_FRED_STKLVLS ,MSR_IA32_FRED_SSP1 , + MSR_IA32_FRED_SSP2, MSR_IA32_FRED_SSP3, MSR_IA32_FRED_CONFIG,Need to handle the case where FRED MSRs are valid but KVM cannot virtualize FRED, see
kvm_probe_msr_to_save().
Will take care of it, thanks for reminding.
Quoted 6 lines
Quoted 3 lines
#endif MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA, MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX, @@ -1890,6+1893,16
Quoted 2 lines
@@ static int __kvm_set_msr(struct
kvm_vcpu*vcpu, u32 index, u64 data,
Quoted 8 lines
arch/x86/kvm/x86.c static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data, · line 1898
Quoted 5 lines
data = (u32)data; break;arch/x86/kvm/x86.c +3
-0+ case MSR_IA32_FRED_RSP0 ... MSR_IA32_FRED_CONFIG: +if (host_initiated ||guest_cpuid_has (vcpu,X86_FEATURE_FRED )) +break ;Nothing guarantees FRED MSRs/VMCS fields exist on the hardware here. Probably use guest_cpu_cap_has()*.
Ah, my bad!
Quoted 11 lines
*: https://lore.kernel.org/kvm/[email protected]
arch/x86/kvm/x86.c static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data, · line 1904
Quoted 6 lines
arch/x86/kvm/x86.c +6
-0+ +/* +* Inject #GP upon FRED MSRs accesses from a non-FRED guest to +* make sure no malicious guest can write to FRED MSRs thus to +* corrupt host FRED MSRs. +*/ I think injecting #GP here is simply because KVM should emulate hardware behavior. To me, preventing guest from corrupting FRED MSRs is at most a byproduct. I prefer to drop the comment.
From security POV, this is important to mention.
RE: [PATCH v1 09/23] KVM: VMX: Switch FRED RSP0 between host and guest
In reply to Chao GaoQuoted 6 lines
arch/x86/kvm/vmx/vmx.c static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx) · line 1404
Quoted 2 lines
arch/x86/kvm/vmx/vmx.c +2
-0+if (cpu_feature_enabled (X86_FEATURE_FRED ) && +guest_cpuid_has (&vmx->vcpu,X86_FEATURE_FRED )) {IIUC,
vmx_prepare_switch_to_host()is called from IRQ-disabled context. usingguest_cpuid_has()in this context is not desired, seelockdep_assert_irqs_enabled()incpuid_entry2_find().
Nice catch!
Anyway it's a bad idea to do a search call here, let me find a better way for all FRED CPUID checks.
Quoted 4 lines
Quoted 4 lines
arch/x86/kvm/vmx/vmx.h index renamed from arch/x86/kvm/vmx/vmx.h +0
-0
f8c02bd37069..328a3447f064100644arch/x86/kvm/vmx/vmx.h +0
-0
Quoted 8 lines
arch/x86/kvm/vmx/vmx.h struct vcpu_vmx { · line 280
Quoted 6 lines
arch/x86/kvm/vmx/vmx.h +2
-0@@ -276,6 +276,8 @@ struct vcpu_vmx {#ifdef CONFIG_X86_64 u64 msr_host_kernel_gs_base;u64 msr_guest_kernel_gs_base; +u64 msr_host_fred_rsp0; +u64 msr_guest_fred_rsp0;resetting guest fred rsp0 to 0 during vcpu reset is missing.
hmm, I assume it gets the same treatment as guest_kernel_gs_base.
It seems we don't reset guest_kernel_gs_base. No?
RE: [PATCH v1 08/23] KVM: VMX: Initialize VMCS FRED fields
In reply to Chao GaoQuoted 11 lines
Review void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int
Quoted 1 line
(quoted diff) +0
-0@@ -1477,6 +1477,18 @@ void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, intcpu,
arch/x86/kvm/vmx/vmx.c void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu, · line 1482
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +3
-0(unsigned long)(cpu_entry_stack(cpu) + 1)); } +#ifdef CONFIG_X86_64 +/* Per-CPU FRED MSRs */ +if (cpu_feature_enabled (X86_FEATURE_FRED )) {how about
kvm_cpu_cap_has()? to decouple KVM's capability to virtualize a feature and host's enabling a feature.
Very likely I guess.
Quoted 8 lines
Review void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int · line 1483
Quoted 1 line
(quoted diff) +1
-0+ vmcs_write64(HOST_IA32_FRED_RSP1,
read_msr(MSR_IA32_FRED_RSP1));Review void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int · line 1484
Quoted 1 line
(quoted diff) +1
-0+ vmcs_write64(HOST_IA32_FRED_RSP2,
read_msr(MSR_IA32_FRED_RSP2));Review void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int · line 1485
Quoted 1 line
(quoted diff) +1
-0+ vmcs_write64(HOST_IA32_FRED_RSP3,
read_msr(MSR_IA32_FRED_RSP3));Review void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int · line 1486
Quoted 1 line
(quoted diff) +1
-0+ vmcs_write64(HOST_IA32_FRED_SSP1,
read_msr(MSR_IA32_FRED_SSP1));
Quoted 8 lines
Review void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int · line 1487
Quoted 1 line
(quoted diff) +1
-0+ vmcs_write64(HOST_IA32_FRED_SSP2,
read_msr(MSR_IA32_FRED_SSP2));Review void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int · line 1488
Quoted 1 line
(quoted diff) +1
-0+ vmcs_write64(HOST_IA32_FRED_SSP3,
read_msr(MSR_IA32_FRED_SSP3));arch/x86/kvm/vmx/vmx.c void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu, · line 1490
Quoted 2 lines
arch/x86/kvm/vmx/vmx.c +2
-0+ } +#endif why is this hunk enclosed in #ifdef
CONFIG_X86_64while the one below isn't?
As if the compiler doesn't complain, I should NOT add it.
Quoted 15 lines
arch/x86/kvm/vmx/vmx.c static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 4970
Quoted 12 lines
arch/x86/kvm/vmx/vmx.c +11
-0+if (cpu_feature_enabled (X86_FEATURE_FRED )) { +vmcs_write64 (GUEST_IA32_FRED_CONFIG ,0 ); +vmcs_write64 (GUEST_IA32_FRED_RSP1 ,0 ); +vmcs_write64 (GUEST_IA32_FRED_RSP2 ,0 ); +vmcs_write64 (GUEST_IA32_FRED_RSP3 ,0 ); +vmcs_write64 (GUEST_IA32_FRED_STKLVLS ,0 ); +vmcs_write64 (GUEST_IA32_FRED_SSP1 ,0 ); +vmcs_write64 (GUEST_IA32_FRED_SSP2 ,0 ); +vmcs_write64 (GUEST_IA32_FRED_SSP3 ,0 ); + } +move this hunk to
__vmx_vcpu_reset()because FRED spec says
Quoted 2 lines
"INIT does not change the value of the new MSRs."
Yeah, will do.
RE: [PATCH v1 08/23] KVM: VMX: Initialize VMCS FRED fields
Quoted 11 lines
Quoted 11 lines
Review
Quoted 2 lines
(quoted diff) +1
-0+ vmcs_write64(HOST_IA32_FRED_RSP1,
read_msr(MSR_IA32_FRED_RSP1));Review
Quoted 1 line
(quoted diff) +1
-0+ vmcs_write64(HOST_IA32_FRED_RSP2,
read_msr(MSR_IA32_FRED_RSP2));Review
Quoted 1 line
(quoted diff) +1
-0+ vmcs_write64(HOST_IA32_FRED_RSP3,
read_msr(MSR_IA32_FRED_RSP3));Review
Quoted 1 line
(quoted diff) +1
-0+ vmcs_write64(HOST_IA32_FRED_SSP1,
read_msr(MSR_IA32_FRED_SSP1));Review
Quoted 1 line
(quoted diff) +1
-0+ vmcs_write64(HOST_IA32_FRED_SSP2,
read_msr(MSR_IA32_FRED_SSP2));
Quoted 8 lines
Quoted 6 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ vmcs_write64(HOST_IA32_FRED_SSP3,
read_msr(MSR_IA32_FRED_SSP3));arch/x86/kvm/vmx/vmx.c void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu, · line 1490
Quoted 2 lines
arch/x86/kvm/vmx/vmx.c +2
-0+ } +#endif why is this hunk enclosed in #ifdef
CONFIG_X86_64while the one below isn't?As if the compiler doesn't complain, I should NOT add it.
I think I don't need to add CONFIG_X86_64 for the above, but somehow this was left over. Let me double check.
Re: [PATCH v1 13/23] KVM: VMX: Handle VMX nested exception for FRED
In reply to Xin LiQuoted 6 lines
/* Require Write-Back (WB) memory type for VMCS accesses. */arch/x86/kvm/vmx/vmx.c +0
-1@@ -7313,11 +7328,12 @@ static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, } } -if (idt_vectoring_info &VECTORING_INFO_DELIVER_CODE_MASK ) {
arch/x86/kvm/vmx/vmx.c static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, · line 7336
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +6
-4-u32 err =vmcs_read32 (error_code_field); -kvm_requeue_exception_e (vcpu, vector, err); - }else -kvm_requeue_exception (vcpu, vector); +if (idt_vectoring_info &VECTORING_INFO_DELIVER_CODE_MASK ) +kvm_requeue_exception_e (vcpu, vector,vmcs_read32 (error_code_field), + idt_vectoring_info &INTR_INFO_NESTED_EXCEPTION_MASK ); +else +kvm_requeue_exception (vcpu, vector, + idt_vectoring_info &INTR_INFO_NESTED_EXCEPTION_MASK );
Exiting-event identification can also have bit 13 set, indicating a nested exception encountered and caused VM-exit. when reinjecting the exception to guests, kvm needs to set the "nested" bit, right? I suspect some changes to e.g., handle_exception_nmi() are needed.
Re: [PATCH v1 09/23] KVM: VMX: Switch FRED RSP0 between host and guest
In reply to Li, Xin3Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
arch/x86/kvm/vmx/vmx.h index renamed from arch/x86/kvm/vmx/vmx.h +0
-0
f8c02bd37069..328a3447f064100644arch/x86/kvm/vmx/vmx.h +0
-0Quoted 8 lines
arch/x86/kvm/vmx/vmx.h struct vcpu_vmx { · line 280
Quoted 6 lines
arch/x86/kvm/vmx/vmx.h +2
-0@@ -276,6 +276,8 @@ struct vcpu_vmx {#ifdef CONFIG_X86_64 u64 msr_host_kernel_gs_base;u64 msr_guest_kernel_gs_base; +u64 msr_host_fred_rsp0; +u64 msr_guest_fred_rsp0;resetting guest fred rsp0 to 0 during vcpu reset is missing.
Quoted 3 lines
hmm, I assume it gets the same treatment as
guest_kernel_gs_base.It seems we don't reset
guest_kernel_gs_base. No?
Yes. But for fred MSRs, FRED spec clearly says their RESET values are 0s. for kernel_gs_base MSR, looks there is no such description in SDM.
Re: [PATCH v1 11/23] KVM: x86: Add kvm_is_fred_enabled()
In reply to Li, Xin3Quoted 8 lines · On Tue, Nov 14, 2023 at 12:42:13PM +0800, Li, Xin3 wrote:
Quoted 5 lines
arch/x86/kvm/kvm_cache_regs.h static __always_inline bool kvm_is_cr4_bit_set(struct kvm_vcpu *vcpu, · line 194
Quoted 2 lines
arch/x86/kvm/kvm_cache_regs.h +2
-0+return cpu_feature_enabled (X86_FEATURE_FRED ) && +kvm_is_cr4_bit_set (vcpu,X86_CR4_FRED );FRED is enabled when CR4.FRED = IA32_EFER.LMA = 1. Any reason to omit the check about long mode?
It won' t allow CR4.FRED to be set if not in long mode, I don't expect it at runtime. Or you have one?
I was thinking about a very contrived case:
1. the CPU enters 64-bit long mode and sets CR4.FRED
2. the CPU switches out of 64-bit long mode
and SDM vol3 chapter 2.5 CONTROL REGISTERS says:
A 64-bit capable processor will retain the upper 32 bits of each control register when transitioning out of IA-32e mode.
so, to me, it is possible that CR4.FRED is 1 while IA32_EFER.LMA is 0. and in this case, FRED should be considered disabled.
Anyway, I think we should align with FRED SPEC. If we deliberately omit the check about long mode, please add a comment to explain why it is ok to do that.
Quoted 3 lines
If you are talking about save/restore a corrupted vCPU state, a following VM entry should fail anyway.
Re: [PATCH v1 12/23] KVM: VMX: Handle FRED event data
In reply to Li, Xin3Quoted 8 lines · On Tue, Nov 14, 2023 at 12:34:02PM +0800, Li, Xin3 wrote:
Quoted 4 lines
arch/x86/kvm/vmx/vmx.c static void vmx_inject_exception(struct kvm_vcpu *vcpu) · line 1900
Quoted 2 lines
arch/x86/kvm/vmx/vmx.c +2
-0+else if (is_nm_fault (intr_info) && + vcpu->arch.guest_fpu.fpstate->xfd)does this necessarily mean the #NM is caused by XFD?
Then the event data should be 0. Or I missed something obvious? I.e., it can be easily differentiated and we should just explicitly set it to 0.
vcpu->arch.guest_fpu.fpstate->xfd just means the guest is enabling XFD. I don't think we can conclude that this #NM is caused by XFD only from this. i.e., there may be some false positives.
Quoted 39 lines
Quoted 31 lines
arch/x86/kvm/vmx/vmx.c static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +6
-0+u64 event_data =vmcs_read64 (event_data_field); + +switch (vector) { +case DB_VECTOR : +get_debugreg (vcpu->arch.dr6,6 ); + WARN_ON(vcpu->arch.dr6 != (event_data ^
DR6_RESERVED));Review
Quoted 9 lines
arch/x86/kvm/vmx/vmx.c +5
-0+ vcpu->arch.dr6 = event_data ^DR6_RESERVED ; +break ; +case NM_VECTOR : +if (vcpu->arch.guest_fpu.fpstate->xfd) { + rdmsrl(MSR_IA32_XFD_ERR, vcpu-arch.guest_fpu.xfd_err);
(quoted diff) +1
-0+ WARN_ON(vcpu-arch.guest_fpu.xfd_err !=
event_data);(quoted diff) +1
-0+ vcpu->arch.guest_fpu.xfd_err =
event_data;arch/x86/kvm/vmx/vmx.c static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, · line 7311
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10
-0+ }else { +WARN_ON (event_data !=0 ); + } +break ; +case PF_VECTOR : +WARN_ON (vcpu->arch.cr2 != event_data); + vcpu->arch.cr2 = event_data; +break ; +default : +WARN_ON (event_data !=0 );I am not sure if this
WARN_ON()can be triggeded by nested VMX. It is legitimate for L1 VMM to inject any event w/ anevent_data.Quoted 7 lines
FRED spec says:
Section 5.2.1 specifies the event data that FRED event delivery of certain events saves on the stack. When FRED event delivery is used for an event injected by VM entry, the event data saved is the value of the injected-event-data field in the VMCS. This value is used instead of what is specified in Section 5.2.1 and is done for
__ALL__injected events using FRED event delivery
Quoted 5 lines
5.2.1 Saving Information on the Regular Stack also says:
- For any other event, the event data are not currently defined and will be zero until they are.Or you mean something else?
IIUC, L1 KVM can inject a nested exception whose vector isn't #DB, or #NM or #PF with a non-zero event_data to L2. If delivering the nested exception causes a VM-exit to L0 KVM, the assertion that event_data is always 0 for vectors other than #DB/#NM/#PF fails.
Re: [PATCH v1 14/23] KVM: VMX: Dump FRED context in dump_vmcs()
In reply to Xin LiQuoted 31 lines · Xin Li
Add FRED related VMCS fields to
dump_vmcs()to have it dump FRED context.Tested-by Shan Kang <[email protected]>Signed-off-by Xin Li <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 48 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 7 deletions(-)arch/x86/kvm/vmx/vmx.c +3
-1@@ -6429,7 +6429,7 @@ void dump_vmcs(struct kvm_vcpu *vcpu)struct vcpu_vmx *vmx =to_vmx (vcpu);u32 vmentry_ctl, vmexit_ctl;u32 cpu_based_exec_ctrl, pin_based_exec_ctrl, secondary_exec_control; -u64 tertiary_exec_control; +u64 tertiary_exec_control, secondary_vmexit_ctl;unsigned long cr4;int efer_slot;@@ -6440,6 +6440,8 @@ void dump_vmcs(struct kvm_vcpu *vcpu) vmentry_ctl =vmcs_read32 (VM_ENTRY_CONTROLS ); vmexit_ctl =vmcs_read32 (VM_EXIT_CONTROLS ); + secondary_vmexit_ctl =cpu_has_secondary_vmexit_ctrls () ? +vmcs_read64 (SECONDARY_VM_EXIT_CONTROLS ) :0 ; cpu_based_exec_ctrl =vmcs_read32 (CPU_BASED_VM_EXEC_CONTROL ); pin_based_exec_ctrl =vmcs_read32 (PIN_BASED_VM_EXEC_CONTROL ); cr4 =vmcs_readl (GUEST_CR4 );
arch/x86/kvm/vmx/vmx.c void dump_vmcs(struct kvm_vcpu *vcpu) · line 6492
Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +2
-0@@ -6486,6 +6488,19 @@ void dump_vmcs(struct kvm_vcpu *vcpu)vmx_dump_sel ("LDTR:" ,GUEST_LDTR_SELECTOR );vmx_dump_dtsel ("IDTR:" ,GUEST_IDTR_LIMIT );vmx_dump_sel ("TR: " ,GUEST_TR_SELECTOR ); +#ifdef CONFIG_X86_64 +if (cpu_feature_enabled (X86_FEATURE_FRED )) {
Shouldn't this be gated on whether FRED is enabled in kvm aka the CPUID bit is enumerated ?
<snip>
RE: [PATCH v1 11/23] KVM: x86: Add kvm_is_fred_enabled()
In reply to Chao GaoQuoted 12 lines
Quoted 6 lines
Quoted 3 lines
FRED is enabled when CR4.FRED = IA32_EFER.LMA = 1. Any reason to omit the check about long mode?
It won' t allow CR4.FRED to be set if not in long mode, I don't expect it at runtime. Or you have one?
I was thinking about a very contrived case:
1. the CPU enters 64-bit long mode and sets CR4.FRED
2. the CPU switches out of 64-bit long mode
Quoted 7 lines
and SDM vol3 chapter 2.5 CONTROL REGISTERS says:
A 64-bit capable processor will retain the upper 32 bits of each control register when transitioning out of IA-32e mode.
so, to me, it is possible that CR4.FRED is 1 while IA32_EFER.LMA is 0. and in this case, FRED should be considered disabled.
You're correct, this is a solid case.
It's not one-way, but I forgot the other way around.
Quoted 4 lines
Anyway, I think we should align with FRED SPEC. If we deliberately omit the check about long mode, please add a comment to explain why it is ok to do that.
Yeah, I will add it.
RE: [PATCH v1 05/23] KVM: VMX: Initialize FRED VM entry/exit controls in vmcs_config
In reply to Nikolay BorisovQuoted 10 lines
arch/x86/kvm/vmx/vmx.c static int setup_vmcs_config(struct vmcs_config *vmcs_conf, · line 2698
Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +1
-1-if (_vmexit_control &VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ) +if (_vmexit_control &VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ) { _secondary_vmexit_control =adjust_vmx_controls64(KVM_OPTIONAL_VMX_SECONDARY_VM_EXIT_CO
NTROLS,Review
Quoted 3 lines
MSR_IA32_VMX_EXIT_CTLS2);(quoted diff) +2
-0+ if (cpu_feature_enabled(X86_FEATURE_FRED) && + !(_secondary_vmexit_control &
SECONDARY_VM_EXIT_SAVE_IA32_FRED&&
Quoted 7 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ _secondary_vmexit_control &
SECONDARY_VM_EXIT_LOAD_IA32_FRED)) {Can those checks actually trigger? I.e if
FEATURE_FREDis set it means the CPU implements the FRED spec. According to the spec it's guaranteed thatVMX_EXIT_CTLS2will contain those bits set to 1. So aren't those checks superfluous?
Such checks are for nVMX FRED.
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (cpu_feature_enabled(X86_FEATURE_FRED) && + !(_vmentry_control & VM_ENTRY_LOAD_IA32_FRED)) {DITTO
<snip>
RE: [PATCH v1 14/23] KVM: VMX: Dump FRED context in dump_vmcs()
In reply to Nikolay BorisovQuoted 18 lines
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +2
-0@@ -6440,6 +6440,8 @@ void dump_vmcs(struct kvm_vcpu *vcpu) vmentry_ctl =vmcs_read32 (VM_ENTRY_CONTROLS ); vmexit_ctl =vmcs_read32 (VM_EXIT_CONTROLS ); + secondary_vmexit_ctl =cpu_has_secondary_vmexit_ctrls () ? +vmcs_read64 (SECONDARY_VM_EXIT_CONTROLS ) :0 ; cpu_based_exec_ctrl =vmcs_read32 (CPU_BASED_VM_EXEC_CONTROL ); pin_based_exec_ctrl =vmcs_read32 (PIN_BASED_VM_EXEC_CONTROL ); cr4 =vmcs_readl (GUEST_CR4 );arch/x86/kvm/vmx/vmx.c void dump_vmcs(struct kvm_vcpu *vcpu) · line 6492
Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +2
-0@@ -6486,6 +6488,19 @@ void dump_vmcs(struct kvm_vcpu *vcpu)vmx_dump_sel ("LDTR:" ,GUEST_LDTR_SELECTOR );vmx_dump_dtsel ("IDTR:" ,GUEST_IDTR_LIMIT );vmx_dump_sel ("TR: " ,GUEST_TR_SELECTOR ); +#ifdef CONFIG_X86_64 +if (cpu_feature_enabled (X86_FEATURE_FRED )) {Shouldn't this be gated on whether FRED is enabled in kvm aka the CPUID bit is enumerated ?
Yeah, that is more accurate.
RE: [PATCH v1 12/23] KVM: VMX: Handle FRED event data
In reply to Chao GaoQuoted 5 lines
Quoted 5 lines
Quoted 4 lines
arch/x86/kvm/vmx/vmx.c static void vmx_inject_exception(struct kvm_vcpu *vcpu) · line 1900
Quoted 2 lines
arch/x86/kvm/vmx/vmx.c +2
-0+else if (is_nm_fault (intr_info) && + vcpu->arch.guest_fpu.fpstate->xfd)does this necessarily mean the #NM is caused by XFD?
Quoted 7 lines
Quoted 3 lines
Then the event data should be 0. Or I missed something obvious? I.e., it can be easily differentiated and we should just explicitly set it to 0.
vcpu->arch.guest_fpu.fpstate->xfdjust means the guest is enabling XFD. I don't think we can conclude that this #NM is caused by XFD only from this. i.e., there may be some false positives.
Then we should get 0 in event data. Otherwise, it is a bug in how we deal with IA32_XFD_ERR MSR, w/ or w/o FRED.
Quoted 20 lines
Quoted 20 lines
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, · line 7311
Quoted 2 lines
arch/x86/kvm/vmx/vmx.c +2
-0+default : +WARN_ON (event_data !=0 );I am not sure if this
WARN_ON()can be triggeded by nested VMX. It is legitimate for L1 VMM to inject any event w/ anevent_data.Quoted 8 lines
FRED spec says:
Section 5.2.1 specifies the event data that FRED event delivery of certain events saves on the stack. When FRED event delivery is used for an event injected by VM entry, the event data saved is the value of the injected-event-data field in the VMCS. This value is used instead of what is specified in Section 5.2.1 and is done for
__ALL__injected events using FRED event delivery5.2.1 Saving Information on the Regular Stack also says:
- For any other event, the event data are not currently defined and will
be zero until they are.
Quoted 4 lines
Quoted 1 line
Or you mean something else?
IIUC, L1 KVM can inject a nested exception whose vector isn't #DB, or #NM or #PF with a non-zero
event_datato L2.
No, this is not allowed.
Quoted 3 lines
If delivering the nested exception causes a VM-
exit to L0 KVM, the assertion thatevent_datais always 0 for vectors other than #DB/#NM/#PF fails.
RE: [PATCH v1 13/23] KVM: VMX: Handle VMX nested exception for FRED
In reply to Chao GaoQuoted 10 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) + kvm_requeue_exception_e(vcpu, vector,
vmcs_read32(error_code_field),Review
Quoted 1 line
(quoted diff) +1
-0+ idt_vectoring_info &
INTR_INFO_NESTED_EXCEPTION_MASK);arch/x86/kvm/vmx/vmx.c static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Quoted 3 lines
arch/x86/kvm/vmx/vmx.c +3
-0+else + kvm_requeue_exception(vcpu, vector, + idt_vectoring_info &
INTR_INFO_NESTED_EXCEPTION_MASK);
Quoted 4 lines
Exiting-event identification can also have bit 13 set, indicating a nested exception encountered and caused VM-exit. when reinjecting the exception to guests, kvm needs to set the "nested" bit, right? I suspect some changes to e.g.,
handle_exception_nmi()are needed.
The current patch relies on kvm_multiple_exception() to do that. But TBH, I'm not sure it can recognize all nested cases. I probably should revisit it.
RE: [PATCH v1 09/23] KVM: VMX: Switch FRED RSP0 between host and guest
In reply to Chao GaoQuoted 13 lines
Quoted 13 lines
Quoted 12 lines
arch/x86/kvm/vmx/vmx.h struct vcpu_vmx { · line 280
Quoted 10 lines
arch/x86/kvm/vmx/vmx.h index renamed from arch/x86/kvm/vmx/vmx.h +0
-0
f8c02bd37069..328a3447f064100644arch/x86/kvm/vmx/vmx.h +2
-0@@ -276,6 +276,8 @@ struct vcpu_vmx {#ifdef CONFIG_X86_64 u64 msr_host_kernel_gs_base;u64 msr_guest_kernel_gs_base; +u64 msr_host_fred_rsp0; +u64 msr_guest_fred_rsp0;resetting guest fred rsp0 to 0 during vcpu reset is missing.
Quoted 7 lines
Quoted 3 lines
hmm, I assume it gets the same treatment as
guest_kernel_gs_base.It seems we don't reset
guest_kernel_gs_base. No?Yes. But for fred MSRs, FRED spec clearly says their RESET values are 0s. for
kernel_gs_baseMSR, looks there is no such description in SDM.
Right, maybe better to set both to 0s.
Sean Christopherson
maintainerRe: [PATCH v1 06/23] KVM: VMX: Defer enabling FRED MSRs save/load until after set CPUID
In reply to Li, Xin3Quoted 5 lines · On Tue, Nov 14, 2023, Xin3 Li wrote:
Quoted 4 lines
Implement what in a different way? The VMCS fields and FRED are architectural. The internal layout of the VMCS is uarch specific, but the encodings and semantics absolutely cannot change without breaking software. And if Intel does something asinine like make a control active-low then we have far, far bigger problems.
Quoted 7 lines
I should have made it clear that I wasn't talking at the ISA level. And of course CPU uarch implementations should be transparent to software.
I mean a CPU uarch could choose to check the activation bit in the VM exit controls first and then decide whether to load the 2nd VM exit controls. While if resources allow, a CPU uarch could always load the 2nd VM exit controls.
And why does that matter? Loading a field speculatively/out-of-order is fine, consuming it when it architecturally is supposed to be ignored is not.
Re: [PATCH v1 12/23] KVM: VMX: Handle FRED event data
In reply to Li, Xin3Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, · line 7311
Quoted 2 lines
arch/x86/kvm/vmx/vmx.c +2
-0+default : +WARN_ON (event_data !=0 );I am not sure if this
WARN_ON()can be triggeded by nested VMX. It is legitimate for L1 VMM to inject any event w/ anevent_data.Quoted 8 lines
FRED spec says:
Section 5.2.1 specifies the event data that FRED event delivery of certain events saves on the stack. When FRED event delivery is used for an event injected by VM entry, the event data saved is the value of the injected-event-data field in the VMCS. This value is used instead of what is specified in Section 5.2.1 and is done for
__ALL__injected events using FRED event delivery5.2.1 Saving Information on the Regular Stack also says:
- For any other event, the event data are not currently defined and will
be zero until they are.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Or you mean something else?
IIUC, L1 KVM can inject a nested exception whose vector isn't #DB, or #NM or #PF with a non-zero
event_datato L2.No, this is not allowed.
How do you interpret the last sentence:
Section 5.2.1 specifies the event data that FRED event delivery of certain events saves on the stack. When FRED event delivery is used for an event injected by VM entry, the event data saved is the value of the injected-event-data field in the VMCS. This value is used instead of what is specified in Section 5.2.1 and is done for __ALL__ injected events using FRED event delivery
RE: [PATCH v1 12/23] KVM: VMX: Handle FRED event data
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
IIUC, L1 KVM can inject a nested exception whose vector isn't #DB, or #NM or #PF with a non-zero
event_datato L2.No, this is not allowed.
Quoted 8 lines
How do you interpret the last sentence:
Section 5.2.1 specifies the event data that FRED event delivery of certain events saves on the stack. When FRED event delivery is used for an event injected by VM entry, the event data saved is the value of the injected-event-data field in the VMCS. This value is used instead of what is specified in Section 5.2.1 and is done for
__ALL__injected events using FRED event delivery
To me, it means FRED event injection during VM entry simply pushes the value in the injected-event-data field of the VMCS as event data. But the event data definition should comply with Section 5.2.1. It is a forward compatibility issue otherwise.
RE: [PATCH v1 11/23] KVM: x86: Add kvm_is_fred_enabled()
In reply to Li, Xin3Quoted 10 lines
Quoted 10 lines
Quoted 5 lines
Quoted 2 lines
FRED is enabled when CR4.FRED = IA32_EFER.LMA = 1. Any reason to omit the check about long mode?
It won' t allow CR4.FRED to be set if not in long mode, I don't expect it at runtime. Or you have one?
I was thinking about a very contrived case:
1. the CPU enters 64-bit long mode and sets CR4.FRED 2. the CPU switches out of 64-bit long mode
Actually, SDM3 Section 10.8.5 Initializing IA-32e Mode says: 64-bit mode consistency checks fail on attempts to enable or disable IA-32e mode while paging is enabled. In another word, the CPU allows software to modify IA32_EFER.LME only when CR0.PG = 0 (i.e., only in legacy mode). Thus, attempts to do so when CR0.PG = 1 will cause #GP.
Remember FRED only works with 64-bit kernel, which can be entered only if paging is enabled. As such, to clear IA32_EFER.LME, OS/VMM needs to turn off paging first. And to turn off paging, 64-bit mode needs to be turned off (to enter compatibility mode in ring 0 because it's not allowed to disable paging in 64-bit mode). Thus CR4.FRED needs be cleared first.
So CR4.FRED fully indicates whether FRED is enabled or not, which is why FRED spec 5.0 section 9.5 says so. But we can put more explanation there.
Vitaly Kuznetsov
maintainerRe: [PATCH v1 15/23] KVM: nVMX: Add support for the secondary VM exit controls
In reply to Xin LiQuoted 32 lines · Xin Li
Enable the secondary VM exit controls to prepare for nested FRED.
Tested-by Shan Kang <[email protected]>Signed-off-by Xin Li <[email protected]>--- Documentation/virt/kvm/x86/nested-vmx.rst | 1 + arch/x86/include/asm/hyperv-tlfs.h | 1 + arch/x86/kvm/vmx/capabilities.h | 1 + arch/x86/kvm/vmx/hyperv.c | 18 +++++++++++++++++- arch/x86/kvm/vmx/nested.c | 18 +++++++++++++++++- arch/x86/kvm/vmx/vmcs12.c | 1 + arch/x86/kvm/vmx/vmcs12.h | 2 ++ arch/x86/kvm/x86.h | 2 +- 8 files changed, 41 insertions(+), 3 deletions(-)Documentation/virt/kvm/x86/nested-vmx.rst +1
-0@@ -217,6 +217,7 @@ struct shadow_vmcs is ever changed. u16 host_fs_selector; u16 host_gs_selector; u16 host_tr_selector; + u64 secondary_vm_exit_controls; };arch/x86/include/asm/hyperv-tlfs.h +0
-0
arch/x86/include/asm/hyperv-tlfs.h struct hv_enlightened_vmcs { · line 619
Quoted 4 lines
arch/x86/include/asm/hyperv-tlfs.h +1
-0@@ -616,6 +616,7 @@ struct hv_enlightened_vmcs {u64 host_ssp;u64 host_ia32_int_ssp_table_addr;u64 padding64_6; +u64 secondary_vm_exit_controls;
(I think Jeremi has asked a similar question but just to be sure)
This doesn't seem to be present in the currently available TLFS version e.g. here:
https://learn.microsoft.com/en-us/virtualization/hyper-v-on-windows/tlfs/datatypes/hv_vmx_enlightened_vmcs
That wouldn't be the first time when TLFS lags behind but as I don't see anyone from Microsoft signing this off, let me ask: where did you get this information and, in case it came from someone @microsoft.com, can we get their sign-off on the patch?
Quoted 49 lines
arch/x86/include/asm/hyperv-tlfs.h +0
-0} __packed;#define HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE 0arch/x86/kvm/vmx/capabilities.h +1
-0@@ -38,6 +38,7 @@ struct nested_vmx_msrs {u32 pinbased_ctls_high;u32 exit_ctls_low;u32 exit_ctls_high; +u64 secondary_exit_ctls;u32 entry_ctls_low;u32 entry_ctls_high;u32 misc_low;arch/x86/kvm/vmx/hyperv.c +7
-1@@ -103,7 +103,10 @@ VM_EXIT_LOAD_IA32_EFER | \ VM_EXIT_CLEAR_BNDCFGS | \ VM_EXIT_PT_CONCEAL_PIP | \ - VM_EXIT_CLEAR_IA32_RTIT_CTL) + VM_EXIT_CLEAR_IA32_RTIT_CTL | \ + VM_EXIT_ACTIVATE_SECONDARY_CONTROLS) + +#define EVMCS1_SUPPORTED_VMEXIT_CTRL2 (0ULL)#define EVMCS1_SUPPORTED_VMENTRY_CTRL \ (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | \@@ -315,6 +318,8 @@ const struct evmcs_field vmcs_field_to_evmcs_1[] = { HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1),EVMCS1_FIELD (VM_EXIT_CONTROLS , vm_exit_controls,HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1 ), +EVMCS1_FIELD (SECONDARY_VM_EXIT_CONTROLS , secondary_vm_exit_controls, +HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1 ),EVMCS1_FIELD (SECONDARY_VM_EXEC_CONTROL , secondary_vm_exec_control,HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1 ), EVMCS1_FIELD(GUEST_ES_LIMIT, guest_es_limit,@@ -464,6 +469,7 @@ enum evmcs_revision {enum evmcs_ctrl_type {EVMCS_EXIT_CTRLS , +EVMCS_2NDEXIT ,EVMCS_ENTRY_CTRLS ,EVMCS_EXEC_CTRL ,EVMCS_2NDEXEC ,
arch/x86/kvm/vmx/hyperv.c static const u32 evmcs_supported_ctrls[NR_EVMCS_CTRLS][NR_EVMCS_REVISIONS] = { · line 491
Quoted 9 lines
arch/x86/kvm/vmx/hyperv.c +3
-0@@ -477,6 +483,9 @@ static const u32 evmcs_supported_ctrls[NR_EVMCS_CTRLS][NR_EVMCS_REVISIONS] = { [EVMCS_EXIT_CTRLS] = { [EVMCSv1_LEGACY] = EVMCS1_SUPPORTED_VMEXIT_CTRL, }, + [EVMCS_2NDEXIT] = { + [EVMCSv1_LEGACY] = EVMCS1_SUPPORTED_VMEXIT_CTRL2, + }, [EVMCS_ENTRY_CTRLS] = { [EVMCSv1_LEGACY] = EVMCS1_SUPPORTED_VMENTRY_CTRL, },
What's the desired effect here? I.e. why exposing VM_EXIT_ACTIVATE_SECONDARY_CONTROLS when none of the controls are going to be exposed?
Quoted 136 lines
arch/x86/kvm/vmx/hyperv.c +7
-0@@ -539,6 +548,9 @@ void nested_evmcs_filter_control_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 * supported_ctrls &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL ; ctl_high &= supported_ctrls;break ; +case MSR_IA32_VMX_EXIT_CTLS2 : + ctl_low &=evmcs_get_supported_ctls (EVMCS_2NDEXIT ); +break ;case MSR_IA32_VMX_ENTRY_CTLS :case MSR_IA32_VMX_TRUE_ENTRY_CTLS : supported_ctrls =evmcs_get_supported_ctls (EVMCS_ENTRY_CTRLS );@@ -589,6 +601,10 @@ int nested_evmcs_check_controls(struct vmcs12 *vmcs12) vmcs12->vm_exit_controls)))return -EINVAL ; +if (CC (!nested_evmcs_is_valid_controls (EVMCS_2NDEXIT , + vmcs12->secondary_vm_exit_controls))) +return -EINVAL ; +if (CC (!nested_evmcs_is_valid_controls (EVMCS_ENTRY_CTRLS , vmcs12->vm_entry_controls)))return -EINVAL ;arch/x86/kvm/vmx/nested.c +17
-1@@ -1411,6 +1411,7 @@ int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)case MSR_IA32_VMX_PINBASED_CTLS :case MSR_IA32_VMX_PROCBASED_CTLS :case MSR_IA32_VMX_EXIT_CTLS : +case MSR_IA32_VMX_EXIT_CTLS2 :case MSR_IA32_VMX_ENTRY_CTLS :/* * The "non-true" VMX capability MSRs are generated from the @@ -1489,6 +1490,9 @@ int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)if (msr_index ==MSR_IA32_VMX_EXIT_CTLS ) *pdata |=VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR ;break ; +case MSR_IA32_VMX_EXIT_CTLS2 : + *pdata = msrs->secondary_exit_ctls; +break ;case MSR_IA32_VMX_TRUE_ENTRY_CTLS :case MSR_IA32_VMX_ENTRY_CTLS : *pdata = vmx_control_msr(@@ -1692,6 +1696,8 @@ static void copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx, u32 hv_clean_fields vmcs12->pin_based_vm_exec_control = evmcs->pin_based_vm_exec_control; vmcs12->vm_exit_controls = evmcs->vm_exit_controls; + vmcs12->secondary_vm_exit_controls = + evmcs->secondary_vm_exit_controls; vmcs12->secondary_vm_exec_control = evmcs->secondary_vm_exec_control; }@@ -1894,6 +1900,7 @@ static void copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)* evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer; * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control; * evmcs->vm_exit_controls = vmcs12->vm_exit_controls; +* evmcs->secondary_vm_exit_controls = vmcs12->secondary_vm_exit_controls; * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control; * evmcs->page_fault_error_code_mask = * vmcs12->page_fault_error_code_mask; @@ -2411,6 +2418,11 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0 exec_control &= ~VM_EXIT_LOAD_IA32_EFER ;vm_exit_controls_set (vmx, exec_control); +if (exec_control &VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ) { + exec_control =__secondary_vm_exit_controls_get (vmcs01); +secondary_vm_exit_controls_set (vmx, exec_control); + } +/* * Interrupt/Exception Fields */ @@ -6819,13 +6831,17 @@ static void nested_vmx_setup_exit_ctls(struct vmcs_config *vmcs_conf,VM_EXIT_HOST_ADDR_SPACE_SIZE | #endifVM_EXIT_LOAD_IA32_PAT |VM_EXIT_SAVE_IA32_PAT | -VM_EXIT_CLEAR_BNDCFGS ; +VM_EXIT_CLEAR_BNDCFGS |VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ; msrs->exit_ctls_high |=VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |VM_EXIT_LOAD_IA32_EFER |VM_EXIT_SAVE_IA32_EFER |VM_EXIT_SAVE_VMX_PREEMPTION_TIMER |VM_EXIT_ACK_INTR_ON_EXIT |VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL ; +/* secondary exit controls */ +if (msrs->exit_ctls_high &VM_EXIT_ACTIVATE_SECONDARY_CONTROLS ) +rdmsrl (MSR_IA32_VMX_EXIT_CTLS2 , msrs->secondary_exit_ctls); +/* We support free control of debug control saving. */ msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; }arch/x86/kvm/vmx/vmcs12.c +1
-0@@ -73,6 +73,7 @@ const unsigned short vmcs12_field_offsets[] = {FIELD (PAGE_FAULT_ERROR_CODE_MATCH , page_fault_error_code_match),FIELD (CR3_TARGET_COUNT , cr3_target_count),FIELD (VM_EXIT_CONTROLS , vm_exit_controls), +FIELD (SECONDARY_VM_EXIT_CONTROLS , secondary_vm_exit_controls),FIELD (VM_EXIT_MSR_STORE_COUNT , vm_exit_msr_store_count),FIELD (VM_EXIT_MSR_LOAD_COUNT , vm_exit_msr_load_count), FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),arch/x86/kvm/vmx/vmcs12.h +2
-0@@ -185,6 +185,7 @@ struct __packed vmcs12 {u16 host_gs_selector;u16 host_tr_selector;u16 guest_pml_index; +u64 secondary_vm_exit_controls; };/* @@ -358,6 +359,7 @@ static inline void vmx_check_vmcs12_offsets(void)CHECK_OFFSET (host_gs_selector,992 );CHECK_OFFSET (host_tr_selector,994 );CHECK_OFFSET (guest_pml_index,996 ); +CHECK_OFFSET (secondary_vm_exit_controls,998 ); }extern const unsigned short vmcs12_field_offsets[];arch/x86/kvm/x86.h +1
-1@@ -47,7 +47,7 @@ void kvm_spurious_fault(void);* associated feature that KVM supports for nested virtualization. */ #define KVM_FIRST_EMULATED_VMX_MSR MSR_IA32_VMX_BASIC -#define KVM_LAST_EMULATED_VMX_MSR MSR_IA32_VMX_VMFUNC +#define KVM_LAST_EMULATED_VMX_MSR MSR_IA32_VMX_EXIT_CTLS2#define KVM_DEFAULT_PLE_GAP 128#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
Signature · 1 line
Vitaly
RE: [PATCH v1 15/23] KVM: nVMX: Add support for the secondary VM exit controls
Quoted 13 lines
arch/x86/include/asm/hyperv-tlfs.h struct hv_enlightened_vmcs { · line 619
Quoted 10 lines
arch/x86/include/asm/hyperv-tlfs.h +1
-0@@ -616,6 +616,7 @@ struct hv_enlightened_vmcs {u64 host_ssp;u64 host_ia32_int_ssp_table_addr;u64 padding64_6; +u64 secondary_vm_exit_controls;(I think Jeremi has asked a similar question but just to be sure)
Quoted 8 lines
This doesn't seem to be present in the currently available TLFS version e.g. here:
https://learn.microsoft.com/en-us/virtualization/hyper-v-on-
windows/tlfs/datatypes/hv_vmx_enlightened_vmcsThat wouldn't be the first time when TLFS lags behind but as I don't see anyone from Microsoft signing this off, let me ask: where did you get this information and, in case it came from someone @microsoft.com, can we get their sign-off on the patch?
This is being worked on.
Quoted 6 lines
arch/x86/kvm/vmx/hyperv.c static const u32
Quoted 5 lines
arch/x86/kvm/vmx/hyperv.c +0
-0@@ -477,6 +483,9 @@ static const u32evmcs_supported_ctrls[NR_EVMCS_CTRLS][NR_EVMCS_REVISIONS] = {
Quoted 13 lines
arch/x86/kvm/vmx/hyperv.c static const u32 evmcs_supported_ctrls[NR_EVMCS_CTRLS][NR_EVMCS_REVISIONS] = { · line 491
Quoted 9 lines
arch/x86/kvm/vmx/hyperv.c +3
-0[EVMCS_EXIT_CTRLS] = { [EVMCSv1_LEGACY] = EVMCS1_SUPPORTED_VMEXIT_CTRL, }, + [EVMCS_2NDEXIT] = { + [EVMCSv1_LEGACY] = EVMCS1_SUPPORTED_VMEXIT_CTRL2, + }, [EVMCS_ENTRY_CTRLS] = { [EVMCSv1_LEGACY] = EVMCS1_SUPPORTED_VMENTRY_CTRL, },What's the desired effect here? I.e. why exposing
VM_EXIT_ACTIVATE_SECONDARY_CONTROLSwhen none of the controls are going to be exposed?
This is wrong for evmcs v1, I will drop it.
RE: [PATCH v1 13/23] KVM: VMX: Handle VMX nested exception for FRED
In reply to Li, Xin3Quoted 13 lines
Subject: RE: [PATCH v1 13/23] KVM: VMX: Handle VMX nested exception for FRED
Quoted 1 line
Review
Quoted 1 line
(quoted diff) +1
-0+ if (idt_vectoring_info &
VECTORING_INFO_DELIVER_CODE_MASK)Quoted 9 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ kvm_requeue_exception_e(vcpu, vector,
vmcs_read32(error_code_field),Review
Quoted 1 line
(quoted diff) +1
-0+ idt_vectoring_info &
INTR_INFO_NESTED_EXCEPTION_MASK);arch/x86/kvm/vmx/vmx.c static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Quoted 3 lines
arch/x86/kvm/vmx/vmx.c +3
-0+else + kvm_requeue_exception(vcpu, vector, + idt_vectoring_info &
INTR_INFO_NESTED_EXCEPTION_MASK);
Quoted 7 lines
Quoted 4 lines
Exiting-event identification can also have bit 13 set, indicating a nested exception encountered and caused VM-exit. when reinjecting the exception to guests, kvm needs to set the "nested" bit, right? I suspect some changes to e.g.,
handle_exception_nmi()are needed.The current patch relies on
kvm_multiple_exception()to do that. But TBH, I'm not sure it can recognize all nested cases. I probably should revisit it.
So the conclusion is that kvm_multiple_exception() is smart enough, and a VMM doesn't have to check bit 13 of the Exiting-event identification.
In FRED spec 5.0, section 9.2 - New VMX Feature: VMX Nested-Exception Support, there is a statement at the end of Exiting-event identification:
(The value of this bit is always identical to that of the valid bit of the original-event identification field.)
It means that even w/o VMX Nested-Exception support, a VMM already knows if an exception is a nested exception encountered during delivery of another event in an exception caused VM exit (exit reason 0). This is done in KVM through reading IDT_VECTORING_INFO_FIELD and calling vmx_complete_interrupts() immediately after VM exits.
vmx_complete_interrupts() simply queues the original exception if there is one, and later the nested exception causing the VM exit could be cancelled if it is a shadow page fault. However if the shadow page fault is caused by a guest page fault, KVM injects it as a nested exception to have guest fix its page table.
I will add comments about this background in the next iteration.
Re: [PATCH v1 13/23] KVM: VMX: Handle VMX nested exception for FRED
Quoted 36 lines · On Wed, Dec 06, 2023 at 04:37:39PM +0800, Li, Xin3 wrote:
Quoted 13 lines
Subject: RE: [PATCH v1 13/23] KVM: VMX: Handle VMX nested exception for FRED
Quoted 1 line
Review
Quoted 1 line
(quoted diff) +1
-0+ if (idt_vectoring_info &
VECTORING_INFO_DELIVER_CODE_MASK)Quoted 9 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ kvm_requeue_exception_e(vcpu, vector,
vmcs_read32(error_code_field),Review
Quoted 1 line
(quoted diff) +1
-0+ idt_vectoring_info &
INTR_INFO_NESTED_EXCEPTION_MASK);arch/x86/kvm/vmx/vmx.c static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Quoted 3 lines
arch/x86/kvm/vmx/vmx.c +3
-0+else + kvm_requeue_exception(vcpu, vector, + idt_vectoring_info &
INTR_INFO_NESTED_EXCEPTION_MASK);Quoted 7 lines
Quoted 4 lines
Exiting-event identification can also have bit 13 set, indicating a nested exception encountered and caused VM-exit. when reinjecting the exception to guests, kvm needs to set the "nested" bit, right? I suspect some changes to e.g.,
handle_exception_nmi()are needed.The current patch relies on
kvm_multiple_exception()to do that. But TBH, I'm not sure it can recognize all nested cases. I probably should revisit it.So the conclusion is that
kvm_multiple_exception()is smart enough, and a VMM doesn't have to check bit 13 of the Exiting-event identification.In FRED spec 5.0, section 9.2 - New VMX Feature: VMX Nested-Exception Support, there is a statement at the end of Exiting-event identification:
(The value of this bit is always identical to that of the valid bit of the original-event identification field.)
It means that even w/o VMX Nested-Exception support, a VMM already knows if an exception is a nested exception encountered during delivery of another event in an exception caused VM exit (exit reason 0). This is done in KVM through reading
IDT_VECTORING_INFO_FIELDand callingvmx_complete_interrupts()immediately after VM exits.
Quoted 7 lines
vmx_complete_interrupts()simply queues the original exception if there is one, and later the nested exception causing the VM exit could be cancelled if it is a shadow page fault. However if the shadow page fault is caused by a guest page fault, KVM injects it as a nested exception to have guest fix its page table.I will add comments about this background in the next iteration.
is it possible that the CPU encounters an exception and causes VM-exit during injecting an __interrupt__? in this case, no __exception__ will be (re-)queued by vmx_complete_interrupts().
RE: [PATCH v1 13/23] KVM: VMX: Handle VMX nested exception for FRED
Quoted 29 lines
Quoted 29 lines
Quoted 7 lines
Quoted 4 lines
Exiting-event identification can also have bit 13 set, indicating a nested exception encountered and caused VM-exit. when reinjecting the exception to guests, kvm needs to set the "nested" bit, right? I suspect some changes to e.g.,
handle_exception_nmi()are needed.The current patch relies on
kvm_multiple_exception()to do that. But TBH, I'm not sure it can recognize all nested cases. I probably should revisit it.So the conclusion is that
kvm_multiple_exception()is smart enough, and a VMM doesn't have to check bit 13 of the Exiting-event identification.In FRED spec 5.0, section 9.2 - New VMX Feature: VMX Nested-Exception Support, there is a statement at the end of Exiting-event identification:
(The value of this bit is always identical to that of the valid bit of the original-event identification field.)
It means that even w/o VMX Nested-Exception support, a VMM already knows if an exception is a nested exception encountered during delivery of another event in an exception caused VM exit (exit reason 0). This is done in KVM through reading
IDT_VECTORING_INFO_FIELDand callingvmx_complete_interrupts()immediately after VM exits.
vmx_complete_interrupts()simply queues the original exception if there is one, and later the nested exception causing the VM exit could be cancelled if it is a shadow page fault. However if the shadow page fault is caused by a guest page fault, KVM injects it as a nested exception to have guest fix its page table.
Quoted 5 lines
Quoted 1 line
I will add comments about this background in the next iteration.
is it possible that the CPU encounters an exception and causes VM-exit during injecting an
__interrupt__? in this case, no__exception__will be (re-)queued byvmx_complete_interrupts().
I guess the following case is what you're suggesting:
KVM injects an external interrupt after shadow page tables are nuked.
vmx_complete_interrupts() are called after each VM exit to clear both interrupt and exception queues, which means it always pushes the deepest event if there is an original event. In the above case, the original event is the external interrupt KVM just tried to inject.
Re: [PATCH v1 13/23] KVM: VMX: Handle VMX nested exception for FRED
Quoted 35 lines · On Thu, Dec 07, 2023 at 06:09:46PM +0800, Li, Xin3 wrote:
Quoted 29 lines
Quoted 29 lines
Quoted 7 lines
Quoted 4 lines
Exiting-event identification can also have bit 13 set, indicating a nested exception encountered and caused VM-exit. when reinjecting the exception to guests, kvm needs to set the "nested" bit, right? I suspect some changes to e.g.,
handle_exception_nmi()are needed.The current patch relies on
kvm_multiple_exception()to do that. But TBH, I'm not sure it can recognize all nested cases. I probably should revisit it.So the conclusion is that
kvm_multiple_exception()is smart enough, and a VMM doesn't have to check bit 13 of the Exiting-event identification.In FRED spec 5.0, section 9.2 - New VMX Feature: VMX Nested-Exception Support, there is a statement at the end of Exiting-event identification:
(The value of this bit is always identical to that of the valid bit of the original-event identification field.)
It means that even w/o VMX Nested-Exception support, a VMM already knows if an exception is a nested exception encountered during delivery of another event in an exception caused VM exit (exit reason 0). This is done in KVM through reading
IDT_VECTORING_INFO_FIELDand callingvmx_complete_interrupts()immediately after VM exits.
vmx_complete_interrupts()simply queues the original exception if there is one, and later the nested exception causing the VM exit could be cancelled if it is a shadow page fault. However if the shadow page fault is caused by a guest page fault, KVM injects it as a nested exception to have guest fix its page table.Quoted 5 lines
Quoted 1 line
I will add comments about this background in the next iteration.
is it possible that the CPU encounters an exception and causes VM-exit during injecting an
__interrupt__? in this case, no__exception__will be (re-)queued byvmx_complete_interrupts().
Quoted 7 lines
I guess the following case is what you're suggesting:
KVM injects an external interrupt after shadow page tables are nuked.
vmx_complete_interrupts()are called after each VM exit to clear both interrupt and exception queues, which means it always pushes the deepest event if there is an original event. In the above case, the original event is the external interrupt KVM just tried to inject.
in my understanding, your point is:
1. if bit 13 of the Exiting-event identification is set. the original-event identification field should be valid.
2. vmx_complete_interrupts() is done immediately after VM exits and reads original-event identification and reinjects the event there.
3. if KVM injects the exception in exiting-event identification to guest, KVM doesn't need to read the bit 13 because kvm_multiple_exception() is "smart enough" and recognize the exception as nested-exception because if bit 13 is 1, one exception must has been queued in #2.
my question is:
what if the event in original-event identification is an interrupt e.g., external interrupt or NMI, rather than exception. vmx_complete_interrupts() won't queue an exception, then how can KVM or kvm_multiple_exception() know the exception that caused VM-exit is an nested exception w/o reading bit 13 of the Exiting-event identification?
RE: [PATCH v1 13/23] KVM: VMX: Handle VMX nested exception for FRED
Quoted 55 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Exiting-event identification can also have bit 13 set, indicating a nested exception encountered and caused VM-exit. when reinjecting the exception to guests, kvm needs to set the "nested" bit, right? I suspect some changes to e.g.,
handle_exception_nmi()are needed.The current patch relies on
kvm_multiple_exception()to do that. But TBH,I'm
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
not sure it can recognize all nested cases. I probably should revisit it.
So the conclusion is that
kvm_multiple_exception()is smart enough, and a VMM doesn't have to check bit 13 of the Exiting-event identification.Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
In FRED spec 5.0, section 9.2 - New VMX Feature: VMX Nested-Exception Support, there is a statement at the end of Exiting-event identification:
(The value of this bit is always identical to that of the valid bit of the original-event identification field.)
It means that even w/o VMX Nested-Exception support, a VMM already
knows
Quoted 17 lines
Quoted 11 lines
Quoted 11 lines
if an exception is a nested exception encountered during delivery of another event in an exception caused VM exit (exit reason 0). This is done in KVM through reading
IDT_VECTORING_INFO_FIELDand callingvmx_complete_interrupts()immediately after VM exits.
vmx_complete_interrupts()simply queues the original exception if there is one, and later the nested exception causing the VM exit could be cancelled if it is a shadow page fault. However if the shadow page fault is caused by a guest page fault, KVM injects it as a nested exception to have guest fix its page table.Quoted 5 lines
Quoted 1 line
I will add comments about this background in the next iteration.
is it possible that the CPU encounters an exception and causes VM-exit during injecting an
__interrupt__? in this case, no__exception__will be (re-)queued byvmx_complete_interrupts().Quoted 7 lines
I guess the following case is what you're suggesting:
KVM injects an external interrupt after shadow page tables are nuked.
vmx_complete_interrupts()are called after each VM exit to clear both interrupt and exception queues, which means it always pushes the deepest event if there is an original event. In the above case, the original event is the external interrupt KVM just tried to inject.in my understanding, your point is:
1. if bit 13 of the Exiting-event identification is set. the original-event identification field should be valid.
2.vmx_complete_interrupts()is done immediately after VM exits and reads original-event identification and reinjects the event there.
3. if KVM injects the exception in exiting-event identification to guest, KVM doesn't need to read the bit 13 becausekvm_multiple_exception()is "smart enough" and recognize the exception as nested-exception because if bit 13 is 1, one exception must has been queued in #2.
Quoted 7 lines
my question is:
what if the event in original-event identification is an interrupt e.g., external interrupt or NMI, rather than exception.vmx_complete_interrupts()won't queue an exception, then how can KVM orkvm_multiple_exception()know the
exception that caused VM-exit is an nested exception w/o reading bit 13 of the Exiting-event identification?
The good news is that vmx_complete_interrupts() still queues the event even it's not a hardware exception. It's just that kvm_multiple_exception() doesn't check if there is an original interrupt or NMI because IDT event delivery doesn't care such a case.
I think your point is more of that we should check it when FRED is enabled for a guest. Yes, architecturally we should do it.
What I want to emphasize is that bit 13 of the exiting-event identification is set to the valid bit of the original-event identification, they are logically the same thing when FRED is enabled. It doens't matter which one a VMM reads and uses. But a VMM doesn't need to differentiate FRED and IDT if it reads the info from original-event identification.