KVM: x86: Enable APX for guest
v10 cover KVM: x86: Enable APX for guest
Here it is v10 but I think the series is winding down. Besides Jim Mattson's review tags - thanks!, this respin includes a few more changes addressing his feedback:
* Patch 03: Simplify EGPR handling.
* Patch 04: Remove a redundant check.
* Patch 11: Mask high-order bits for segment registers.
I also came to other minor touches like VCPU -> vCPU.
As usual, they can be also found in
git://github.com/intel/apx.git apx-kvm_v10 (based on the next branch)
V9: https://lore.kernel.org/[email protected]
Thanks,
Chang
Chang S. Bae (19):
KVM: x86: Extend vCPU registers for EGPRs
KVM: VMX: Save guest EGPRs in vCPU cache
KVM: x86: Support EGPRs on vCPU RESET/INIT events
KVM: x86: Support APX state for XSAVE ABI
KVM: VMX: Refactor VMX instruction information access
KVM: VMX: Refactor instruction information decoding
KVM: VMX: Remove unused control-register access defines
KVM: VMX: Refactor register index retrieval from exit qualification
KVM: VMX: Support instruction information extension
KVM: nVMX: Propagate extended instruction information
KVM: x86: Support EGPR accessing and tracking for emulator
KVM: x86: Handle EGPR index and REX2-incompatible opcodes
KVM: x86: Support REX2-prefixed opcode decode
KVM: x86: Document EVEX-prefix not implemented
KVM: x86: Guard valid XCR0.APX settings
KVM: x86: Add APX to supported XCR0
KVM: x86: Expose APX feature to userspace
KVM: selftests: Add APX state and ABI test
KVM: selftests: Add APX state handling and XCR0 sanity checks
Sean Christopherson (1):
KVM: x86: Move KVM_SUPPORTED_{XCR0,XSS} into kvm_x86_vendor_init()
arch/x86/Kconfig.assembler | 5 + arch/x86/include/asm/cpuid/api.h | 1 + arch/x86/include/asm/kvm_host.h | 25 +++ arch/x86/include/asm/kvm_vcpu_regs.h | 50 +++++ arch/x86/include/asm/vmx.h | 22 +- arch/x86/kvm/Kconfig | 4 + arch/x86/kvm/cpuid.c | 33 ++- arch/x86/kvm/cpuid.h | 2 + arch/x86/kvm/emulate.c | 138 +++++++++--- arch/x86/kvm/kvm_emulate.h | 21 +- arch/x86/kvm/reverse_cpuid.h | 6 + arch/x86/kvm/svm/svm.c | 8 +- arch/x86/kvm/vmenter.h | 1 + arch/x86/kvm/vmx/common.h | 5 + arch/x86/kvm/vmx/nested.c | 72 +++--- arch/x86/kvm/vmx/nested.h | 2 +- arch/x86/kvm/vmx/vmcs12.c | 4 + arch/x86/kvm/vmx/vmcs12.h | 3 +- arch/x86/kvm/vmx/vmenter.S | 31 ++- arch/x86/kvm/vmx/vmx.c | 36 ++- arch/x86/kvm/vmx/vmx.h | 63 +++++- arch/x86/kvm/vmx/vmx_ops.h | 9 + arch/x86/kvm/x86.c | 114 ++++++++-- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/include/x86/processor.h | 120 ++++++++++ tools/testing/selftests/kvm/x86/apx_test.c | 209 ++++++++++++++++++ tools/testing/selftests/kvm/x86/state_test.c | 3 + .../selftests/kvm/x86/xcr0_cpuid_test.c | 19 ++ 28 files changed, 871 insertions(+), 136 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86/apx_test.c
base-commit: cbe54fc3462b3949ab27b9ae81fb9ddb509040c7
Signature · 1 line
2.53.0
PATCH v10 1/20 KVM: x86: Extend vCPU registers for EGPRs
Extend the storage to include extended general-purpose registers (EGPRs:
R16-R31) when CONFIG_KVM_APX=y. This option is for VMX only.
Although EGPR state is XSAVE-managed, and thus could be stored in the guest fpstate, vCPU storage provides a more convenient and efficient access path for KVM.
---
V9 -> V10: Collect a review tag
--- arch/x86/include/asm/kvm_host.h | 18 ++++++++++++++++++ arch/x86/kvm/Kconfig | 4 ++++ 2 files changed, 22 insertions(+)
arch/x86/include/asm/kvm_host.h +18 -0
@@ -168,6 +168,24 @@ enum kvm_reg {VCPU_REGS_R13 ,VCPU_REGS_R14 ,VCPU_REGS_R15 , +#endif +#ifdef CONFIG_KVM_APX +VCPU_REGS_R16 , +VCPU_REGS_R17 , +VCPU_REGS_R18 , +VCPU_REGS_R19 , +VCPU_REGS_R20 , +VCPU_REGS_R21 , +VCPU_REGS_R22 , +VCPU_REGS_R23 , +VCPU_REGS_R24 , +VCPU_REGS_R25 , +VCPU_REGS_R26 , +VCPU_REGS_R27 , +VCPU_REGS_R28 , +VCPU_REGS_R29 , +VCPU_REGS_R30 , +VCPU_REGS_R31 , #endif NR_VCPU_GENERAL_PURPOSE_REGS,
arch/x86/kvm/Kconfig +4 -0
@@ -102,10 +102,14 @@ config KVM_SW_PROTECTED_VM If unsure, say "N". +config KVM_APX + bool + config KVM_INTEL tristate "KVM for Intel (and compatible) processors support" depends on KVM && IA32_FEAT_CTL select X86_FRED if X86_64 + select KVM_APX if X86_64 help Provides support for KVM on processors equipped with Intel's VT extensions, a.k.a. Virtual Machine Extensions (VMX).
Signature · 1 line
2.53.0
PATCH v10 2/20 KVM: VMX: Save guest EGPRs in vCPU cache
In reply to Chang S. BaeSave and restore the guest EGPRs on VM exit/entry if the system supports Advanced Performance Extensions (APX).
There are two possible ways of handling EGPR accesses: reading them from the processor lazily, or switching them in the vmentry/vmexit path and accessing them via vcpu->arch.regs. Prepare for the latter; it does not pollute kvm_register_read()/kvm_register_write() with conditionals to treat GPRs vs. EGPRs differently, and it allows for future usage of APX registers in the kernel.
KVM intercepts all XCR0 writes, therefore XCR0 cannot change during the execution of VMLAUNCH/VMRESUME. Thus, for a guest that does not set XCR0[APX], the EGPR state does not have to be loaded/saved.
The register saving path is placed after SPEC_CTRL is restored, so as to avoid any mis-speculation into EGPR accesses when APX is disabled.
While saving may be skipped on VM-Fail, it is not worth the added complication for a slow path.
--- arch/x86/Kconfig.assembler | 5 +++ arch/x86/include/asm/kvm_vcpu_regs.h | 50 ++++++++++++++++++++++++++++ arch/x86/kvm/Kconfig | 2 +- arch/x86/kvm/vmenter.h | 1 + arch/x86/kvm/vmx/vmenter.S | 31 +++++++++++++++-- arch/x86/kvm/vmx/vmx.c | 13 ++++++++ 6 files changed, 99 insertions(+), 3 deletions(-)
arch/x86/Kconfig.assembler +5 -0
@@ -1,6 +1,11 @@ # SPDX-License-Identifier: GPL-2.0 # Copyright (C) 2020 Jason A. Donenfeld <[email protected]>. All Rights Reserved. +config AS_APX + def_bool $(as-instr64,xor %r16$(comma)%r16) + help + Supported by binutils >= 2.42 and LLVM integrated assembler >= 18 + config AS_WRUSS def_bool $(as-instr64,wrussq %rax$(comma)(%rbx)) help
arch/x86/include/asm/kvm_vcpu_regs.h +50 -0
56 lines not on this page · raw message
arch/x86/kvm/Kconfig +1 -1
@@ -109,7 +109,7 @@ config KVM_INTEL tristate "KVM for Intel (and compatible) processors support" depends on KVM && IA32_FEAT_CTL select X86_FRED if X86_64 - select KVM_APX if X86_64 + select KVM_APX if X86_64 && AS_APX help Provides support for KVM on processors equipped with Intel's VT extensions, a.k.a. Virtual Machine Extensions (VMX).
arch/x86/kvm/vmenter.h +1 -0
@@ -7,6 +7,7 @@#define KVM_ENTER_VMRESUME BIT(0)#define KVM_ENTER_SAVE_SPEC_CTRL BIT(1)#define KVM_ENTER_CLEAR_CPU_BUFFERS_FOR_MMIO BIT(2) +#define KVM_ENTER_EGPR_SWITCH BIT(3)#ifdef __ASSEMBLER__ .macro RESTORE_GUEST_SPEC_CTRL_BODY guest_spec_ctrl:req, label:req
arch/x86/kvm/vmx/vmenter.S +29 -2
@@ -16,6 +16,7 @@ * @flags: KVM_ENTER_VMRESUME: use VMRESUME instead of VMLAUNCH * KVM_ENTER_SAVE_SPEC_CTRL: save guest SPEC_CTRL into vmx->spec_ctrl * KVM_ENTER_CLEAR_CPU_BUFFERS_FOR_MMIO: vCPU can access host MMIO + * KVM_ENTER_EGPR_SWITCH: load/store guest EGPRs * * Returns: * 0 on VM-Exit, 1 on VM-Fail
@@ -46,6 +47,16 @@ SYM_FUNC_START(__vmx_vcpu_run) /* Reload @vmx, _ASM_ARG1 may be modified by vmx_update_host_rsp(). */ mov WORD_SIZE(%_ASM_SP), %_ASM_DI +#ifdef CONFIG_KVM_APX + ALTERNATIVE "jmp .Lload_egprs_done", "", X86_FEATURE_APX + testl $KVM_ENTER_EGPR_SWITCH, (%_ASM_SP) + jz .Lload_egprs_done + LOAD_REGS %_ASM_DI, VMX_vcpu_arch_regs, \ + %r16, %r17, %r18, %r19, %r20, %r21, %r22, %r23, \ + %r24, %r25, %r26, %r27, %r28, %r29, %r30, %r31 +.Lload_egprs_done: +#endif + /* * Unlike AMD there's no V_SPEC_CTRL here, so do not leave the body * out of line. Clobbers RAX, RCX, RDX, RSI.
@@ -193,8 +204,24 @@ SYM_INNER_LABEL_ALIGN(vmx_vmexit, SYM_L_GLOBAL) mov %_ASM_BX, %_ASM_AX /* Pop our saved arguments from the stack */ - pop %_ASM_BX - pop %_ASM_BX + pop %_ASM_BX /* @flags */ + pop %_ASM_DI /* @vmx */ + +#ifdef CONFIG_KVM_APX + ALTERNATIVE "jmp .Lclear_egprs_done", "", X86_FEATURE_APX + test $KVM_ENTER_EGPR_SWITCH, %_ASM_BX + jz .Lclear_egprs_done + /* + * Saving EGPRs could be skipped on VM-Fail, but that is a slow path, so + * always save them. + */ + STORE_REGS %_ASM_DI, VMX_vcpu_arch_regs, \ + %r16, %r17, %r18, %r19, %r20, %r21, %r22, %r23, \ + %r24, %r25, %r26, %r27, %r28, %r29, %r30, %r31 + CLEAR_REGS %r16d, %r17d, %r18d, %r19d, %r20d, %r21d, %r22d, %r23d, \ + %r24d, %r25d, %r26d, %r27d, %r28d, %r29d, %r30d, %r31d +.Lclear_egprs_done: +#endif /* ... and then the callee-save registers */ pop %_ASM_BX
arch/x86/kvm/vmx/vmx.c +13 -0
@@ -994,6 +994,19 @@ unsigned int __vmx_vcpu_enter_flags(struct vcpu_vmx *vmx) kvm_vcpu_can_access_host_mmio(&vmx->vcpu)) flags |=KVM_ENTER_CLEAR_CPU_BUFFERS_FOR_MMIO ; +/* +* KVM intercepts XSETBV and thus always tracks the guest XCR0. EGPR +* save/restore is gated by this flag. The resulting behavior is: +* +* - When the guest enables APX, KVM restores EGPRs (initially zeroed). +* - When the guest disables APX, EGPRs are preserved in the vCPU cache. +* - When APX is re-enabled, the saved state is restored, which matches +* architectural expectations. +*/ +if (IS_ENABLED (CONFIG_KVM_APX ) &&cpu_feature_enabled (X86_FEATURE_APX ) && + vmx->vcpu.arch.xcr0 &XFEATURE_MASK_APX ) + flags |=KVM_ENTER_EGPR_SWITCH ; +return flags; }
Signature · 1 line
2.53.0
PATCH v10 3/20 KVM: x86: Support EGPRs on vCPU RESET/INIT events
In reply to Chang S. BaeZero EGPRs on vCPU RESET and preserve them on INIT, matching the hardware specification.
---
V9 -> V10:
* Use NR_VCPU_LEGACY_GPRS instead of a hard-coded number, then the change looks small, reverting the previous refactoring as well. (Jim)
* Add a review tag, thanks!
--- arch/x86/include/asm/kvm_host.h | 6 ++++++ arch/x86/kvm/x86.c | 9 +++++++-- 2 files changed, 13 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +6 -0
@@ -211,6 +211,12 @@ enum kvm_reg { NR_VCPU_TOTAL_REGS, }; +#ifdef CONFIG_X86_64 +#define NR_VCPU_LEGACY_GPRS (VCPU_REGS_R15 + 1) +#else +#define NR_VCPU_LEGACY_GPRS (VCPU_REGS_RDI + 1) +#endif +enum {VCPU_SREG_ES ,VCPU_SREG_CS ,
arch/x86/kvm/x86.c +7 -2
@@ -9673,8 +9673,13 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)kvm_msr_write (vcpu,MSR_IA32_XSS ,0 ); } -/* All GPRs except RDX (handled below) are zeroed on RESET/INIT. */ -memset (vcpu->arch.regs,0 ,sizeof (vcpu->arch.regs)); +/* +* All legacy GPRs except RDX (handled below) are zeroed on RESET/INIT. +* EGPRs (R16-R31) are zeroed on RESET, but are unchanged on INIT. +*/ +memset (vcpu->arch.regs,0 , + init_event ?NR_VCPU_LEGACY_GPRS *sizeof (vcpu->arch.regs[0 ]) : +sizeof (vcpu->arch.regs));kvm_register_mark_dirty (vcpu,VCPU_REGS_RSP );/*
Signature · 1 line
2.53.0
PATCH v10 4/20 KVM: x86: Support APX state for XSAVE ABI
In reply to Chang S. BaeIntroduce a facility to copy APX state between the vCPU cache and the userspace buffer as the vCPU cache is the single source of truth.
The existing fpstate copy functions historically sync all XSTATEs in between userspace and kernel buffers [1]. In this regard, any additional state handling logic should be consistent with them -- i.e. validation of XSTATE_BV against the supported XCR0 mask.
Now the two copy paths must account for these facts:
* When exporting to userspace, copy fpstate first, since it zeros the area of any component that is absent or inactive. Then copy the APX state from the vCPU cache.
* When importing from userspace, the APX state is copied to both fpstate and the vCPU cache. Avoiding the duplicate copy would need special care in the common code or in XSTATE_BV/XCR0 which isn't worth that much for a slow path, so keep it simple.
[1] Except for PKRU state, as stored in struct thread_struct.
---
V9 -> V10: Skip the redundant sanity check (Jim Mattson)
--- arch/x86/kvm/cpuid.c | 10 ++++++ arch/x86/kvm/cpuid.h | 2 ++ arch/x86/kvm/x86.c | 74 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 82 insertions(+), 4 deletions(-)
arch/x86/kvm/cpuid.c +10 -0
@@ -61,6 +61,16 @@ void __init kvm_init_xstate_sizes(void) } } +u32 xstate_size (unsigned int xfeature) +{ +return xstate_sizes[xfeature].eax; +} + +u32 xstate_offset (unsigned int xfeature) +{ +return xstate_sizes[xfeature].ebx; +} +u32 xstate_required_size (u64 xstate_bv,bool compacted) {u32 ret =XSAVE_HDR_SIZE +XSAVE_HDR_OFFSET ;
arch/x86/kvm/cpuid.h +2 -0
@@ -66,6 +66,8 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,void __init kvm_init_xstate_sizes (void );u32 xstate_required_size (u64 xstate_bv,bool compacted); +u32 xstate_size (unsigned int xfeature); +u32 xstate_offset (unsigned int xfeature);int cpuid_query_maxphyaddr (struct kvm_vcpu *vcpu);u64 kvm_vcpu_reserved_gpa_bits_raw (struct kvm_vcpu *vcpu);
arch/x86/kvm/x86.c +70 -4
100 lines not on this page · raw message
Signature · 1 line
2.53.0
PATCH v10 5/20 KVM: VMX: Refactor VMX instruction information access
In reply to Chang S. BaeIntroduce a helper that returns the instruction information as a 64-bit value and adjust existing sites to prepare for a wider field.
The VMX instruction information field is currently 32 bits. Future extensions may expand this field to support extended register IDs, requiring a wider width. The change provides a single access point for the transition.
No functional change intended.
--- arch/x86/kvm/vmx/nested.c | 23 ++++++++++++----------- arch/x86/kvm/vmx/nested.h | 2 +- arch/x86/kvm/vmx/vmx.c | 6 +++--- arch/x86/kvm/vmx/vmx.h | 10 ++++++++-- 4 files changed, 24 insertions(+), 17 deletions(-)
arch/x86/kvm/vmx/nested.c +12 -11
@@ -5218,7 +5218,7 @@ static void nested_vmx_triple_fault(struct kvm_vcpu *vcpu)* #UD, #GP, or #SS. */ int get_vmx_mem_address (struct kvm_vcpu *vcpu,unsigned long exit_qualification, -u32 vmx_instruction_info,bool wr,int len,gva_t *ret) +u64 vmx_instruction_info,bool wr,int len,gva_t *ret) {gva_t off;bool exn;
@@ -5350,7 +5350,7 @@ static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer,int r;if (get_vmx_mem_address (vcpu,vt_get_exit_qual (vcpu), -vmcs_read32 (VMX_INSTRUCTION_INFO ), false, +vmx_get_instr_info (), false,sizeof (*vmpointer), &gva)) { *ret =1 ;return -EINVAL ;
@@ -5644,7 +5644,7 @@ static int handle_vmread(struct kvm_vcpu *vcpu)struct vmcs12 *vmcs12 =is_guest_mode (vcpu) ?get_shadow_vmcs12 (vcpu) :get_vmcs12 (vcpu);unsigned long exit_qualification =vt_get_exit_qual (vcpu); -u32 instr_info =vmcs_read32 (VMX_INSTRUCTION_INFO ); +u64 instr_info =vmx_get_instr_info ();struct vcpu_vmx *vmx =to_vmx (vcpu);struct x86_exception e;unsigned long field;
@@ -5750,7 +5750,7 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu)struct vmcs12 *vmcs12 =is_guest_mode (vcpu) ?get_shadow_vmcs12 (vcpu) :get_vmcs12 (vcpu);unsigned long exit_qualification =vt_get_exit_qual (vcpu); -u32 instr_info =vmcs_read32 (VMX_INSTRUCTION_INFO ); +u64 instr_info =vmx_get_instr_info ();struct vcpu_vmx *vmx =to_vmx (vcpu);struct x86_exception e;unsigned long field;
@@ -5941,7 +5941,7 @@ static int handle_vmptrld(struct kvm_vcpu *vcpu)static int handle_vmptrst (struct kvm_vcpu *vcpu) {unsigned long exit_qual =vt_get_exit_qual (vcpu); -u32 instr_info =vmcs_read32 (VMX_INSTRUCTION_INFO ); +u64 instr_info =vmx_get_instr_info ();gpa_t current_vmptr;struct x86_exception e;gva_t gva;
@@ -5977,8 +5977,8 @@ static int handle_vmptrst(struct kvm_vcpu *vcpu)static int handle_invept (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu); -u32 vmx_instruction_info, types;unsigned long type, roots_to_free; +u64 vmx_instruction_info;struct kvm_mmu *mmu;gva_t gva;struct x86_exception e;
@@ -5986,6 +5986,7 @@ static int handle_invept(struct kvm_vcpu *vcpu)u64 eptp, gpa; } operand;int i, r, gpr_index; +u32 types; if (!(vmx->nested.msrs.secondary_ctls_high & SECONDARY_EXEC_ENABLE_EPT) ||
@@ -5997,7 +5998,7 @@ static int handle_invept(struct kvm_vcpu *vcpu)if (!nested_vmx_check_permission (vcpu))return 1 ; - vmx_instruction_info =vmcs_read32 (VMX_INSTRUCTION_INFO ); + vmx_instruction_info =vmx_get_instr_info (); gpr_index =vmx_get_instr_info_reg2 (vmx_instruction_info); type =kvm_register_read (vcpu, gpr_index);
@@ -6057,7 +6058,7 @@ static int handle_invept(struct kvm_vcpu *vcpu)static int handle_invvpid (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu); -u32 vmx_instruction_info; +u64 vmx_instruction_info;unsigned long type, types;gva_t gva;struct x86_exception e;
@@ -6078,7 +6079,7 @@ static int handle_invvpid(struct kvm_vcpu *vcpu)if (!nested_vmx_check_permission (vcpu))return 1 ; - vmx_instruction_info =vmcs_read32 (VMX_INSTRUCTION_INFO ); + vmx_instruction_info =vmx_get_instr_info (); gpr_index =vmx_get_instr_info_reg2 (vmx_instruction_info); type =kvm_register_read (vcpu, gpr_index);
@@ -6423,7 +6424,7 @@ static bool nested_vmx_exit_handled_encls(struct kvm_vcpu *vcpu,static bool nested_vmx_exit_handled_vmcs_access (struct kvm_vcpu *vcpu,struct vmcs12 *vmcs12,gpa_t bitmap) { -u32 vmx_instruction_info; +u64 vmx_instruction_info;unsigned long field;u8 b;
@@ -6431,7 +6432,7 @@ static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,return true;/* Decode instruction info and find the field to access */ - vmx_instruction_info =vmcs_read32 (VMX_INSTRUCTION_INFO ); + vmx_instruction_info =vmx_get_instr_info (); field =kvm_register_read (vcpu, (((vmx_instruction_info) >>28 ) &0xf ));/* Out-of-range fields always cause a VM exit from L2 to L1 */
arch/x86/kvm/vmx/nested.h +1 -1
@@ -51,7 +51,7 @@ void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu);int vmx_set_vmx_msr (struct kvm_vcpu *vcpu,u32 msr_index,u64 data);int vmx_get_vmx_msr (struct nested_vmx_msrs *msrs,u32 msr_index,u64 *pdata);int get_vmx_mem_address (struct kvm_vcpu *vcpu,unsigned long exit_qualification, -u32 vmx_instruction_info,bool wr,int len,gva_t *ret); +u64 vmx_instruction_info,bool wr,int len,gva_t *ret);bool nested_vmx_check_io_bitmaps (struct kvm_vcpu *vcpu,unsigned int port,int size);
arch/x86/kvm/vmx/vmx.c +3 -3
@@ -6154,7 +6154,7 @@ static int handle_monitor_trap(struct kvm_vcpu *vcpu)static int handle_invpcid (struct kvm_vcpu *vcpu) { -u32 vmx_instruction_info; +u64 vmx_instruction_info;unsigned long type;gva_t gva;struct {
@@ -6168,7 +6168,7 @@ static int handle_invpcid(struct kvm_vcpu *vcpu)return 1 ; } - vmx_instruction_info =vmcs_read32 (VMX_INSTRUCTION_INFO ); + vmx_instruction_info =vmx_get_instr_info (); gpr_index =vmx_get_instr_info_reg2 (vmx_instruction_info); type =kvm_register_read (vcpu, gpr_index);
@@ -6297,7 +6297,7 @@ static int handle_notify(struct kvm_vcpu *vcpu)static int vmx_get_msr_imm_reg (struct kvm_vcpu *vcpu) { -return vmx_get_instr_info_reg (vmcs_read32 (VMX_INSTRUCTION_INFO )); +return vmx_get_instr_info_reg (vmx_get_instr_info ()); }static int handle_rdmsr_imm (struct kvm_vcpu *vcpu)
arch/x86/kvm/vmx/vmx.h +8 -2
@@ -678,12 +678,18 @@ static inline bool vmx_guest_state_valid(struct kvm_vcpu *vcpu)void dump_vmcs (struct kvm_vcpu *vcpu); -static inline int vmx_get_instr_info_reg (u32 vmx_instr_info) +/* A placeholder to smoothen 64-bit extension */ +static inline u64 vmx_get_instr_info (void ) +{ +return vmcs_read32 (VMX_INSTRUCTION_INFO ); +} + +static inline int vmx_get_instr_info_reg (u64 vmx_instr_info) {return (vmx_instr_info >>3 ) &0xf ; } -static inline int vmx_get_instr_info_reg2 (u32 vmx_instr_info) +static inline int vmx_get_instr_info_reg2 (u64 vmx_instr_info) {return (vmx_instr_info >>28 ) &0xf ; }
Signature · 1 line
2.53.0
PATCH v10 6/20 KVM: VMX: Refactor instruction information decoding
In reply to Chang S. BaeKVM currently decodes the VMX instruction information field using a mix of open-coded bit manipulations and ad hoc helpers. Convert all decoding to use helpers to centralize the decoding logic for the transition to a wider instruction information.
No functional change intended.
--- arch/x86/kvm/vmx/nested.c | 58 +++++++++++++++++++-------------------- arch/x86/kvm/vmx/vmx.c | 9 +++--- arch/x86/kvm/vmx/vmx.h | 48 +++++++++++++++++++++++++++++--- 3 files changed, 77 insertions(+), 38 deletions(-)
arch/x86/kvm/vmx/nested.c +29 -29
@@ -5218,7 +5218,7 @@ static void nested_vmx_triple_fault(struct kvm_vcpu *vcpu)* #UD, #GP, or #SS. */ int get_vmx_mem_address (struct kvm_vcpu *vcpu,unsigned long exit_qualification, -u64 vmx_instruction_info,bool wr,int len,gva_t *ret) +u64 instr_info,bool wr,int len,gva_t *ret) {gva_t off;bool exn;
@@ -5226,20 +5226,20 @@ int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,/* * According to Vol. 3B, "Information for VM Exits Due to Instruction -* Execution", on an exit, vmx_instruction_info holds most of the -* addressing components of the operand. Only the displacement part -* is put in exit_qualification (see 3B, "Basic VM-Exit Information"). +* Execution", on an exit, instr_info holds most of the addressing +* components of the operand. Only the displacement part is put in +* exit_qualification (see 3B, "Basic VM-Exit Information"). * For how an actual address is calculated from all these components, * refer to Vol. 1, "Operand Addressing". */ -int scaling = vmx_instruction_info &3 ; -int addr_size = (vmx_instruction_info >>7 ) &7 ; -bool is_reg = vmx_instruction_info & (1u <<10 ); -int seg_reg = (vmx_instruction_info >>15 ) &7 ; -int index_reg = (vmx_instruction_info >>18 ) &0xf ; -bool index_is_valid = !(vmx_instruction_info & (1u <<22 )); -int base_reg = (vmx_instruction_info >>23 ) &0xf ; -bool base_is_valid = !(vmx_instruction_info & (1u <<27 )); +int scaling =vmx_get_instr_info_scaling (instr_info); +int addr_size =vmx_get_instr_info_addr_size (instr_info); +bool is_reg =vmx_get_instr_info_is_reg (instr_info); +int seg_reg =vmx_get_instr_info_seg_reg (instr_info); +int index_reg =vmx_get_instr_info_index_reg (instr_info); +bool index_is_valid =vmx_get_instr_info_index_is_valid (instr_info); +int base_reg =vmx_get_instr_info_base_reg (instr_info); +bool base_is_valid =vmx_get_instr_info_base_is_valid (instr_info);if (is_reg) {kvm_queue_exception (vcpu,UD_VECTOR );
@@ -5657,7 +5657,7 @@ static int handle_vmread(struct kvm_vcpu *vcpu)return 1 ;/* Decode instruction info and find the field to read */ - field =kvm_register_read (vcpu, (((instr_info) >>28 ) &0xf )); + field =kvm_register_read (vcpu,vmx_get_instr_info_reg2 (instr_info));if (!nested_vmx_is_evmptr12_valid (vmx)) {/*
@@ -5705,8 +5705,8 @@ static int handle_vmread(struct kvm_vcpu *vcpu)* Note that the number of bits actually copied is 32 or 64 depending * on the guest's mode (32 or 64 bit), not on the given field's length. */ -if (instr_info &BIT (10 )) { -kvm_register_write (vcpu, (((instr_info) >>3 ) &0xf ), value); +if (vmx_get_instr_info_is_reg (instr_info)) { +kvm_register_write (vcpu,vmx_get_instr_info_reg (instr_info), value); }else { len =is_64_bit_mode (vcpu) ?8 :4 ; if (get_vmx_mem_address(vcpu, exit_qualification,
@@ -5779,8 +5779,8 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu) get_vmcs12(vcpu)->vmcs_link_pointer == INVALID_GPA))return nested_vmx_failInvalid (vcpu); -if (instr_info &BIT (10 )) - value =kvm_register_read (vcpu, (((instr_info) >>3 ) &0xf )); +if (vmx_get_instr_info_is_reg (instr_info)) + value =kvm_register_read (vcpu,vmx_get_instr_info_reg (instr_info));else { len =is_64_bit_mode (vcpu) ?8 :4 ; if (get_vmx_mem_address(vcpu, exit_qualification,
@@ -5791,7 +5791,7 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu)return kvm_handle_memory_failure (vcpu, r, &e); } - field =kvm_register_read (vcpu, (((instr_info) >>28 ) &0xf )); + field =kvm_register_read (vcpu,vmx_get_instr_info_reg2 (instr_info)); offset =get_vmcs12_field_offset (field);if (offset <0 )
@@ -5978,7 +5978,7 @@ static int handle_invept(struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu);unsigned long type, roots_to_free; -u64 vmx_instruction_info; +u64 instr_info;struct kvm_mmu *mmu;gva_t gva;struct x86_exception e;
@@ -5998,8 +5998,8 @@ static int handle_invept(struct kvm_vcpu *vcpu)if (!nested_vmx_check_permission (vcpu))return 1 ; - vmx_instruction_info =vmx_get_instr_info (); - gpr_index =vmx_get_instr_info_reg2 (vmx_instruction_info); + instr_info =vmx_get_instr_info (); + gpr_index =vmx_get_instr_info_reg2 (instr_info); type =kvm_register_read (vcpu, gpr_index); types = (vmx->nested.msrs.ept_caps >>VMX_EPT_EXTENT_SHIFT ) &6 ;
@@ -6011,7 +6011,7 @@ static int handle_invept(struct kvm_vcpu *vcpu)* operand is read even if it isn't needed (e.g., for type==global) */ if (get_vmx_mem_address (vcpu,vt_get_exit_qual (vcpu), - vmx_instruction_info, false,sizeof (operand), &gva)) + instr_info, false,sizeof (operand), &gva))return 1 ; r =kvm_read_guest_virt (vcpu, gva, &operand,sizeof (operand), &e);if (r !=X86EMUL_CONTINUE )
@@ -6058,8 +6058,8 @@ static int handle_invept(struct kvm_vcpu *vcpu)static int handle_invvpid (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu); -u64 vmx_instruction_info;unsigned long type, types; +u64 instr_info;gva_t gva;struct x86_exception e;struct {
@@ -6079,8 +6079,8 @@ static int handle_invvpid(struct kvm_vcpu *vcpu)if (!nested_vmx_check_permission (vcpu))return 1 ; - vmx_instruction_info =vmx_get_instr_info (); - gpr_index =vmx_get_instr_info_reg2 (vmx_instruction_info); + instr_info =vmx_get_instr_info (); + gpr_index =vmx_get_instr_info_reg2 (instr_info); type =kvm_register_read (vcpu, gpr_index); types = (vmx->nested.msrs.vpid_caps &
@@ -6094,7 +6094,7 @@ static int handle_invvpid(struct kvm_vcpu *vcpu)* operand is read even if it isn't needed (e.g., for type==global) */ if (get_vmx_mem_address (vcpu,vt_get_exit_qual (vcpu), - vmx_instruction_info, false,sizeof (operand), &gva)) + instr_info, false,sizeof (operand), &gva))return 1 ; r =kvm_read_guest_virt (vcpu, gva, &operand,sizeof (operand), &e);if (r !=X86EMUL_CONTINUE )
@@ -6424,16 +6424,16 @@ static bool nested_vmx_exit_handled_encls(struct kvm_vcpu *vcpu,static bool nested_vmx_exit_handled_vmcs_access (struct kvm_vcpu *vcpu,struct vmcs12 *vmcs12,gpa_t bitmap) { -u64 vmx_instruction_info;unsigned long field; +u64 instr_info;u8 b;if (!nested_cpu_has_shadow_vmcs (vmcs12))return true;/* Decode instruction info and find the field to access */ - vmx_instruction_info =vmx_get_instr_info (); - field =kvm_register_read (vcpu, (((vmx_instruction_info) >>28 ) &0xf )); + instr_info =vmx_get_instr_info (); + field =kvm_register_read (vcpu,vmx_get_instr_info_reg2 (instr_info));/* Out-of-range fields always cause a VM exit from L2 to L1 */ if (field >>15 )
arch/x86/kvm/vmx/vmx.c +4 -5
@@ -6154,8 +6154,8 @@ static int handle_monitor_trap(struct kvm_vcpu *vcpu)static int handle_invpcid (struct kvm_vcpu *vcpu) { -u64 vmx_instruction_info;unsigned long type; +u64 instr_info;gva_t gva;struct {u64 pcid;
@@ -6168,16 +6168,15 @@ static int handle_invpcid(struct kvm_vcpu *vcpu)return 1 ; } - vmx_instruction_info =vmx_get_instr_info (); - gpr_index =vmx_get_instr_info_reg2 (vmx_instruction_info); + instr_info =vmx_get_instr_info (); + gpr_index =vmx_get_instr_info_reg2 (instr_info); type =kvm_register_read (vcpu, gpr_index);/* According to the Intel instruction reference, the memory operand * is read even if it isn't needed (e.g., for type==all) */ if (get_vmx_mem_address (vcpu,vt_get_exit_qual (vcpu), - vmx_instruction_info, false, -sizeof (operand), &gva)) + instr_info, false,sizeof (operand), &gva))return 1 ;return kvm_handle_invpcid (vcpu, type, gva);
arch/x86/kvm/vmx/vmx.h +44 -4
58 lines not on this page · raw message
Signature · 1 line
2.53.0
PATCH v10 7/20 KVM: VMX: Remove unused control-register access defines
In reply to Chang S. BaeCONTROL_REG_ACCESS_REG becomes misleading with EGPRs. Rather than leave stale, unused definitions behind, remove them all.
--- arch/x86/include/asm/vmx.h | 19 ------------------- 1 file changed, 19 deletions(-)
arch/x86/include/asm/vmx.h +0 -19
@@ -453,27 +453,8 @@ enum vmcs_field {/* * Exit Qualifications for MOV for Control Register Access */ -#define CONTROL_REG_ACCESS_NUM 0x7/* 2:0, number of control reg.*/ -#define CONTROL_REG_ACCESS_TYPE 0x30/* 5:4, access type */ -#define CONTROL_REG_ACCESS_REG 0xf00/* 10:8, general purpose reg. */ #define LMSW_SOURCE_DATA_SHIFT 16#define LMSW_SOURCE_DATA (0xFFFF << LMSW_SOURCE_DATA_SHIFT)/* 16:31 lmsw source */ -#define REG_EAX (0 << 8) -#define REG_ECX (1 << 8) -#define REG_EDX (2 << 8) -#define REG_EBX (3 << 8) -#define REG_ESP (4 << 8) -#define REG_EBP (5 << 8) -#define REG_ESI (6 << 8) -#define REG_EDI (7 << 8) -#define REG_R8 (8 << 8) -#define REG_R9 (9 << 8) -#define REG_R10 (10 << 8) -#define REG_R11 (11 << 8) -#define REG_R12 (12 << 8) -#define REG_R13 (13 << 8) -#define REG_R14 (14 << 8) -#define REG_R15 (15 << 8)/* * Exit Qualifications for MOV for Debug Register Access
Signature · 1 line
2.53.0
PATCH v10 8/20 KVM: VMX: Refactor register index retrieval from exit qualification
In reply to Chang S. BaeIntroduce a helper to extract the register index from the VMX exit qualification field.
In addition to the VMX instruction information field, exit qualification also encodes a register index. This field will expand into the previously reserved bit for extended register IDs. This refactoring will simplify the extended register handling without code duplication.
No functional change intended.
--- arch/x86/include/asm/vmx.h | 1 - arch/x86/kvm/vmx/common.h | 5 +++++ arch/x86/kvm/vmx/nested.c | 2 +- arch/x86/kvm/vmx/vmx.c | 4 ++-- 4 files changed, 8 insertions(+), 4 deletions(-)
arch/x86/include/asm/vmx.h +0 -1
@@ -463,7 +463,6 @@ enum vmcs_field {#define DEBUG_REG_ACCESS_TYPE 0x10/* 4, direction of access */ #define TYPE_MOV_TO_DR (0 << 4)#define TYPE_MOV_FROM_DR (1 << 4) -#define DEBUG_REG_ACCESS_REG (eq) (((eq) >> 8) & 0xf)/* 11:8, general purpose reg. */ /*
arch/x86/kvm/vmx/common.h +5 -0
@@ -110,6 +110,11 @@ static __always_inline unsigned long vt_get_exit_qual(struct kvm_vcpu *vcpu)return vt->exit_qualification; } +static __always_inline int vt_get_exit_qual_reg(unsigned long exit_qualification) +{ +return (exit_qualification >>8 ) &0xf ; +} + static __always_inline u32 vt_get_intr_info(struct kvm_vcpu *vcpu) {struct vcpu_vt *vt =to_vt (vcpu);
arch/x86/kvm/vmx/nested.c +1 -1
@@ -6346,7 +6346,7 @@ static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,switch ((exit_qualification >>4 ) &3 ) {case 0 :/* mov to cr */ - reg = (exit_qualification >>8 ) &15 ; + reg =vt_get_exit_qual_reg (exit_qualification); val =kvm_register_read (vcpu, reg);switch (cr) {case 0 :
arch/x86/kvm/vmx/vmx.c +2 -2
@@ -5698,7 +5698,7 @@ static int handle_cr(struct kvm_vcpu *vcpu) exit_qualification =vt_get_exit_qual (vcpu); cr = exit_qualification &15 ; - reg = (exit_qualification >>8 ) &15 ; + reg =vt_get_exit_qual_reg (exit_qualification);switch ((exit_qualification >>4 ) &3 ) {case 0 :/* mov to cr */ val =kvm_register_read (vcpu, reg);
@@ -5817,7 +5817,7 @@ static int handle_dr(struct kvm_vcpu *vcpu)return 1 ; } - reg =DEBUG_REG_ACCESS_REG (exit_qualification); + reg =vt_get_exit_qual_reg (exit_qualification);if (exit_qualification &TYPE_MOV_FROM_DR ) {kvm_register_write (vcpu, reg,kvm_get_dr (vcpu, dr)); err =0 ;
Signature · 1 line
2.53.0
PATCH v10 9/20 KVM: VMX: Support instruction information extension
In reply to Chang S. BaeDefine the VMCS field offset for the extended instruction information. Then, retrieve 5-bit register indices from the VMCS fields.
Although the APX CPUID enumeration indicates that the extension is available, hide it when using eVMCS, as eVMCS support is not ready yet.
For safety, do not assume that previously reserved bits were zero on older implementations.
---
V9 -> V10: Collect a review tag
--- arch/x86/include/asm/vmx.h | 2 ++ arch/x86/kvm/vmx/common.h | 2 +- arch/x86/kvm/vmx/vmx.h | 31 ++++++++++++++++++++----------- arch/x86/kvm/vmx/vmx_ops.h | 9 +++++++++ 4 files changed, 32 insertions(+), 12 deletions(-)
arch/x86/include/asm/vmx.h +2 -0
@@ -285,6 +285,8 @@ enum vmcs_field {PID_POINTER_TABLE_HIGH =0x00002043 ,GUEST_PHYSICAL_ADDRESS =0x00002400 ,GUEST_PHYSICAL_ADDRESS_HIGH =0x00002401 , +EXTENDED_INSTRUCTION_INFO =0x00002406 , +EXTENDED_INSTRUCTION_INFO_HIGH =0x00002407 ,VMCS_LINK_POINTER =0x00002800 ,VMCS_LINK_POINTER_HIGH =0x00002801 , GUEST_IA32_DEBUGCTL = 0x00002802,
arch/x86/kvm/vmx/common.h +1 -1
@@ -112,7 +112,7 @@ static __always_inline unsigned long vt_get_exit_qual(struct kvm_vcpu *vcpu) static __always_inline int vt_get_exit_qual_reg(unsigned long exit_qualification) { -return (exit_qualification >>8 ) &0xf ; +return (exit_qualification >>8 ) & (vmx_instr_info_extended () ?0x1f :0xf ); } static __always_inline u32 vt_get_intr_info(struct kvm_vcpu *vcpu)
arch/x86/kvm/vmx/vmx.h +20 -11
@@ -678,20 +678,22 @@ static inline bool vmx_guest_state_valid(struct kvm_vcpu *vcpu)void dump_vmcs (struct kvm_vcpu *vcpu); -/* A placeholder to smoothen 64-bit extension */ static inline u64 vmx_get_instr_info (void ) { -return vmcs_read32 (VMX_INSTRUCTION_INFO ); +return vmx_instr_info_extended () ?vmcs_read64 (EXTENDED_INSTRUCTION_INFO ) : +vmcs_read32 (VMX_INSTRUCTION_INFO ); }static inline int vmx_get_instr_info_reg (u64 instr_info) { -return (instr_info >>3 ) &0xf ; +return vmx_instr_info_extended () ? (instr_info >>16 ) &0x1f : + (instr_info >>3 ) &0xf ; }static inline int vmx_get_instr_info_reg2 (u64 instr_info) { -return (instr_info >>28 ) &0xf ; +return vmx_instr_info_extended () ? (instr_info >>40 ) &0x1f : + (instr_info >>28 ) &0xf ; }static inline int vmx_get_instr_info_scaling (u64 instr_info)
@@ -701,37 +703,44 @@ static inline int vmx_get_instr_info_scaling(u64 instr_info)static inline int vmx_get_instr_info_addr_size (u64 instr_info) { -return (instr_info >>7 ) &7 ; +return vmx_instr_info_extended () ? (instr_info >>2 ) &3 : + (instr_info >>7 ) &7 ; }static inline bool vmx_get_instr_info_is_reg (u64 instr_info) { -return !!(instr_info &BIT (10 )); +return vmx_instr_info_extended () ? !!(instr_info &BIT (4 )) : + !!(instr_info &BIT (10 )); }static inline int vmx_get_instr_info_seg_reg (u64 instr_info) { -return (instr_info >>15 ) &7 ; +return vmx_instr_info_extended () ? (instr_info >>7 ) &7 : + (instr_info >>15 ) &7 ; }static inline int vmx_get_instr_info_index_reg (u64 instr_info) { -return (instr_info >>18 ) &0xf ; +return vmx_instr_info_extended () ? (instr_info >>24 ) &0x1f : + (instr_info >>18 ) &0xf ; }static inline bool vmx_get_instr_info_index_is_valid (u64 instr_info) { -return !(instr_info &BIT (22 )); +return vmx_instr_info_extended () ? !(instr_info &BIT (10 )) : + !(instr_info &BIT (22 )); }static inline int vmx_get_instr_info_base_reg (u64 instr_info) { -return (instr_info >>23 ) &0xf ; +return vmx_instr_info_extended () ? (instr_info >>32 ) &0x1f : + (instr_info >>23 ) &0xf ; }static inline bool vmx_get_instr_info_base_is_valid (u64 instr_info) { -return !(instr_info &BIT (27 )); +return vmx_instr_info_extended () ? !(instr_info &BIT (11 )) : + !(instr_info &BIT (27 )); }static inline bool vmx_can_use_ipiv (struct kvm_vcpu *vcpu)
arch/x86/kvm/vmx/vmx_ops.h +9 -0
@@ -365,4 +365,13 @@ static inline void ept_sync_context(u64 eptp) ept_sync_global(); } +/* +* The APX enumeration guarantees the presence of the extended fields. The host +* CPUID bit alone is sufficient to rely on it. eVMCS does not support them yet. +*/ +static inline bool vmx_instr_info_extended (void ) +{ +return cpu_feature_enabled (X86_FEATURE_APX ) && !kvm_is_using_evmcs (); +} +#endif /* __KVM_X86_VMX_INSN_H */
Signature · 1 line
2.53.0
PATCH v10 10/20 KVM: nVMX: Propagate extended instruction information
In reply to Chang S. BaeDefine the new extended_instruction_info field in struct vmcs12 and propagate it to nested VMX. Gate the propagation on the guest APX enumeration, which aligns with bare metal behavior. Thus, define the CPUID bit here too.
---
V9 -> V10: Collect a review tag
--- arch/x86/kvm/reverse_cpuid.h | 2 ++ arch/x86/kvm/vmx/nested.c | 3 +++ arch/x86/kvm/vmx/vmcs12.c | 4 ++++ arch/x86/kvm/vmx/vmcs12.h | 3 ++- 4 files changed, 11 insertions(+), 1 deletion(-)
arch/x86/kvm/reverse_cpuid.h +2 -0
@@ -37,6 +37,7 @@#define X86_FEATURE_AVX_VNNI_INT16 KVM_X86_FEATURE(CPUID_7_1_EDX, 10)#define X86_FEATURE_PREFETCHITI KVM_X86_FEATURE(CPUID_7_1_EDX, 14)#define X86_FEATURE_AVX10 KVM_X86_FEATURE(CPUID_7_1_EDX, 19) +#define KVM_X86_FEATURE_APX KVM_X86_FEATURE(CPUID_7_1_EDX, 21)/* Intel-defined sub-features, CPUID level 0x00000007:2 (EDX) */ #define X86_FEATURE_INTEL_PSFD KVM_X86_FEATURE(CPUID_7_2_EDX, 0)
@@ -146,6 +147,7 @@ static __always_inline u32 __feature_translate(int x86_feature)KVM_X86_TRANSLATE_FEATURE (SGX1 );KVM_X86_TRANSLATE_FEATURE (SGX2 );KVM_X86_TRANSLATE_FEATURE (SGX_EDECCSSA ); +KVM_X86_TRANSLATE_FEATURE (APX );KVM_X86_TRANSLATE_FEATURE (CONSTANT_TSC );KVM_X86_TRANSLATE_FEATURE (PERFMON_V2 );KVM_X86_TRANSLATE_FEATURE (RRSBA_CTRL );
arch/x86/kvm/vmx/nested.c +3 -0
@@ -4751,6 +4751,9 @@ static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, vmcs12->vm_exit_intr_info = exit_intr_info; vmcs12->vm_exit_instruction_len = exit_insn_len; vmcs12->vmx_instruction_info =vmcs_read32 (VMX_INSTRUCTION_INFO ); +/* The extended field exists whenever APX is enumerated. */ +if (guest_cpu_cap_has (vcpu,X86_FEATURE_APX )) + vmcs12->extended_instruction_info =vmcs_read64 (EXTENDED_INSTRUCTION_INFO );/* * According to spec, there's no need to store the guest's
arch/x86/kvm/vmx/vmcs12.c +4 -0
@@ -2,6 +2,7 @@#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt#include "vmcs12.h" +#include "vmx_ops.h" #define VMCS12_OFFSET (x) offsetof(struct vmcs12, x)#define FIELD (number, name) [ENC_TO_VMCS12_IDX(number)] = VMCS12_OFFSET(name)
@@ -53,6 +54,7 @@ static const u16 kvm_supported_vmcs12_field_offsets[] __initconst = {FIELD64 (XSS_EXIT_BITMAP , xss_exit_bitmap),FIELD64 (ENCLS_EXITING_BITMAP , encls_exiting_bitmap),FIELD64 (GUEST_PHYSICAL_ADDRESS , guest_physical_address), +FIELD64 (EXTENDED_INSTRUCTION_INFO , extended_instruction_info),FIELD64 (VMCS_LINK_POINTER , vmcs_link_pointer),FIELD64 (GUEST_IA32_DEBUGCTL , guest_ia32_debugctl), FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
@@ -203,6 +205,8 @@ static __init bool cpu_has_vmcs12_field(unsigned int idx)case HOST_SSP :case HOST_INTR_SSP_TABLE :return cpu_has_load_cet_ctrl (); + VMCS12_CASE64(EXTENDED_INSTRUCTION_INFO): +return vmx_instr_info_extended ();/* KVM always emulates PML and the VMX preemption timer in software. */ case GUEST_PML_INDEX :
arch/x86/kvm/vmx/vmcs12.h +2 -1
@@ -71,7 +71,7 @@ struct __packed vmcs12 {u64 pml_address;u64 encls_exiting_bitmap;u64 tsc_multiplier; -u64 padding64[1 ];/* room for future expansion */ +u64 extended_instruction_info;/* * To allow migration of L1 (complete with its L2 guests) between * machines of different natural widths (32 or 64 bit), we cannot have
@@ -261,6 +261,7 @@ static inline void vmx_check_vmcs12_offsets(void)CHECK_OFFSET (pml_address,312 );CHECK_OFFSET (encls_exiting_bitmap,320 );CHECK_OFFSET (tsc_multiplier,328 ); +CHECK_OFFSET (extended_instruction_info,336 );CHECK_OFFSET (cr0_guest_host_mask,344 );CHECK_OFFSET (cr4_guest_host_mask,352 );CHECK_OFFSET (cr0_read_shadow,360 );
Signature · 1 line
2.53.0
PATCH v10 11/20 KVM: x86: Support EGPR accessing and tracking for emulator
In reply to Chang S. BaeExtend the emulator context and GPR accessors to handle EGPRs before adding support for REX2-prefixed instructions. Like vCPU cache, the emulator can uniformly cache and track all GPRs.
Also replace 1 << reg with BIT(reg) in reg_read()/reg_write(). The former performs a signed 32-bit shift, which becomes undefined for R31. BIT() instead shifts an unsigned value that is suitable for all indices.
--- arch/x86/kvm/kvm_emulate.h | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)
arch/x86/kvm/kvm_emulate.h +11 -9
@@ -109,13 +109,13 @@ struct x86_instruction_info {struct x86_emulate_ops {void (*vm_bugged)(struct x86_emulate_ctxt *ctxt);/* -* read_gpr: read a general purpose register (rax - r15) +* read_gpr: read a general purpose register (rax - r31) * * @reg: gpr number. */ ulong (*read_gpr)(struct x86_emulate_ctxt *ctxt,unsigned reg);/* -* write_gpr: write a general purpose register (rax - r15) +* write_gpr: write a general purpose register (rax - r31) * * @reg: gpr number. * @val: value to write.
@@ -321,7 +321,9 @@ typedef void (*fastop_t)(struct fastop *);* also uses _eip, RIP cannot be a register operand nor can it be an operand in * a ModRM or SIB byte. */ -#ifdef CONFIG_X86_64 +#if defined(CONFIG_KVM_APX ) +#define NR_EMULATOR_GPRS 32 +#elif defined(CONFIG_X86_64 )#define NR_EMULATOR_GPRS 16#else #define NR_EMULATOR_GPRS 8
@@ -381,9 +383,9 @@ struct x86_emulate_ctxt {u8 lock_prefix;u8 rep_prefix;/* bitmaps of registers in _regs[] that can be read */ -u16 regs_valid; +u32 regs_valid;/* bitmaps of registers in _regs[] that have been written */ -u16 regs_dirty; +u32 regs_dirty;/* modrm */ u8 modrm;u8 modrm_mod;
@@ -547,8 +549,8 @@ static inline ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)if (KVM_EMULATOR_BUG_ON (nr >=NR_EMULATOR_GPRS , ctxt)) nr &=NR_EMULATOR_GPRS -1 ; -if (!(ctxt->regs_valid & (1 << nr))) { - ctxt->regs_valid |=1 << nr; +if (!(ctxt->regs_valid &BIT (nr))) { + ctxt->regs_valid |=BIT (nr); ctxt->_regs[nr] = ctxt->ops->read_gpr (ctxt, nr); }return ctxt->_regs[nr];
@@ -562,8 +564,8 @@ static inline ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)BUILD_BUG_ON (sizeof (ctxt->regs_dirty) *BITS_PER_BYTE <NR_EMULATOR_GPRS );BUILD_BUG_ON (sizeof (ctxt->regs_valid) *BITS_PER_BYTE <NR_EMULATOR_GPRS ); - ctxt->regs_valid |=1 << nr; - ctxt->regs_dirty |=1 << nr; + ctxt->regs_valid |=BIT (nr); + ctxt->regs_dirty |=BIT (nr);return &ctxt->_regs[nr]; }
Signature · 1 line
2.53.0
PATCH v10 12/20 KVM: x86: Handle EGPR index and REX2-incompatible opcodes
In reply to Chang S. BaePrepare the emulator for REX2 handling by introducing the NoRex2 opcode flag and supporting extended register indices. For the latter, factor out common logic for calculating register IDs.
REX2 does not support three-byte opcodes. Instead, the REX2.M bit selects between one- and two-byte opcode tables, which were previously distinguished by the 0x0F escape byte.
Some legacy instructions in those tables never reference extended registers. When prefixed with REX, such instructions are treated as if the prefix were absent. In contrast, a REX2 prefix causes a #UD, which should be handled explicitly.
JMPABS (0xA1) is an exception: it is defined for REX2 but mark it as not implemented, since emulating non-memory operation isn't practically that useful here.
Also, ignore the high-order register index bits when accessing vector registers (XMM and YMM) or segment registers.
---
V9 -> V10: Mask higher bits for segment registers (Jim Mattson)
--- arch/x86/kvm/emulate.c | 97 ++++++++++++++++++++++++++------------ arch/x86/kvm/kvm_emulate.h | 1 + 2 files changed, 67 insertions(+), 31 deletions(-)
arch/x86/kvm/emulate.c +66 -31
@@ -176,6 +176,7 @@#define TwoMemOp ((u64)1 << 55)/* Instruction has two memory operand */ #define IsBranch ((u64)1 << 56)/* Instruction is considered a branch. */ #define ShadowStack ((u64)1 << 57)/* Instruction affects Shadow Stacks. */ +#define NoRex2 ((u64)1 << 58)/* Instruction not present in REX2 maps */ #define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
@@ -245,6 +246,7 @@ enum rex_bits {REX_X =2 ,REX_R =4 ,REX_W =8 , + REX_M = 0x80, };static void writeback_registers (struct x86_emulate_ctxt *ctxt)
@@ -1061,10 +1063,24 @@ static int em_fnstsw(struct x86_emulate_ctxt *ctxt)return X86EMUL_CONTINUE ; } +static __always_inline int rex_get_rxb(u8 rex, u8 fld) +{ +BUILD_BUG_ON (!__builtin_constant_p (fld)); +BUILD_BUG_ON (fld !=REX_B && fld !=REX_X && fld !=REX_R ); + + rex >>=ffs (fld) -1 ; +return (rex &1 ?8 :0 ) + (rex &0x10 ?16 :0 ); +} + +/* +* With EGPR support, @reg may exceed the number of available vector +* registers. Mask off the higher bits accordingly. +*/ static void __decode_register_operand (struct x86_emulate_ctxt *ctxt,struct operand *op,int reg) {if ((ctxt->d & Avx) && ctxt->op_bytes ==32 ) { + reg &=0xf ; op->type =OP_YMM ; op->bytes =32 ; op->addr.xmm = reg;
@@ -1072,6 +1088,7 @@ static void __decode_register_operand(struct x86_emulate_ctxt *ctxt,return ; }if (ctxt->d & (Avx|Sse)) { + reg &=0xf ; op->type =OP_XMM ; op->bytes =16 ; op->addr.xmm = reg;
@@ -1100,7 +1117,7 @@ static void decode_register_operand(struct x86_emulate_ctxt *ctxt,if (ctxt->d & ModRM) reg = ctxt->modrm_reg;else - reg = (ctxt->b &7 ) | (ctxt->rex_bits &REX_B ?8 :0 ); + reg = (ctxt->b &7 ) |rex_get_rxb (ctxt->rex_bits,REX_B );__decode_register_operand (ctxt, op, reg); }
@@ -1119,9 +1136,9 @@ static int decode_modrm(struct x86_emulate_ctxt *ctxt,int rc =X86EMUL_CONTINUE ;ulong modrm_ea =0 ; - ctxt->modrm_reg = (ctxt->rex_bits &REX_R ?8 :0 ); - index_reg = (ctxt->rex_bits &REX_X ?8 :0 ); - base_reg = (ctxt->rex_bits &REX_B ?8 :0 ); + ctxt->modrm_reg =rex_get_rxb (ctxt->rex_bits,REX_R ); + index_reg =rex_get_rxb (ctxt->rex_bits,REX_X ); + base_reg =rex_get_rxb (ctxt->rex_bits,REX_B ); ctxt->modrm_mod = (ctxt->modrm &0xc0 ) >>6 ; ctxt->modrm_reg |= (ctxt->modrm &0x38 ) >>3 ;
@@ -3342,6 +3359,9 @@ static int em_store_sreg(struct x86_emulate_ctxt *ctxt, int segment)static int em_mov_rm_sreg (struct x86_emulate_ctxt *ctxt) { +/* Ignore higher register index bits from REX/REX2: */ + ctxt->modrm_reg &=7 ; +if (ctxt->modrm_reg >VCPU_SREG_GS )return emulate_ud (ctxt);
@@ -3352,6 +3372,9 @@ static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt) {u16 sel = ctxt->src.val; +/* Ignore higher register index bits from REX/REX2: */ + ctxt->modrm_reg &=7 ; +if (ctxt->modrm_reg ==VCPU_SREG_CS || ctxt->modrm_reg >VCPU_SREG_GS )return emulate_ud (ctxt);
@@ -4255,7 +4278,7 @@ static const struct opcode opcode_table[256] = {/* 0x38 - 0x3F */ I6ALU (NoWrite, em_cmp),N ,N ,/* 0x40 - 0x4F */ -X8 (I (DstReg, em_inc)),X8 (I (DstReg, em_dec)), +X8 (I (DstReg | NoRex2, em_inc)),X8 (I (DstReg | NoRex2, em_dec)),/* 0x50 - 0x57 */ X8(I(SrcReg | Stack, em_push)),/* 0x58 - 0x5F */
@@ -4273,7 +4296,7 @@ static const struct opcode opcode_table[256] = {I2bvIP (DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in),/* insb, insw/insd */ I2bvIP (SrcSI | DstDX | String, em_out, outs, check_perm_out),/* outsb, outsw/outsd */ /* 0x70 - 0x7F */ -X16 (D (SrcImmByte | NearBranch | IsBranch)), +X16 (D (SrcImmByte | NearBranch | IsBranch | NoRex2)),/* 0x80 - 0x87 */ G (ByteOp | DstMem | SrcImm, group1), G(DstMem | SrcImm, group1),
@@ -4297,15 +4320,15 @@ static const struct opcode opcode_table[256] = {II (ImplicitOps | Stack, em_popf, popf),I (ImplicitOps, em_sahf),I (ImplicitOps, em_lahf),/* 0xA0 - 0xA7 */ -I2bv (DstAcc | SrcMem | Mov | MemAbs, em_mov), -I2bv (DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov), -I2bv (SrcSI | DstDI | Mov | String | TwoMemOp, em_mov), -I2bv (SrcSI | DstDI | String | NoWrite | TwoMemOp, em_cmp_r), +I2bv (DstAcc | SrcMem | Mov | MemAbs | NoRex2, em_mov), +I2bv (DstMem | SrcAcc | Mov | MemAbs | PageTable | NoRex2, em_mov), +I2bv (SrcSI | DstDI | Mov | String | TwoMemOp | NoRex2, em_mov), +I2bv (SrcSI | DstDI | String | NoWrite | TwoMemOp | NoRex2, em_cmp_r),/* 0xA8 - 0xAF */ -I2bv (DstAcc | SrcImm | NoWrite, em_test), -I2bv (SrcAcc | DstDI | Mov | String, em_mov), -I2bv (SrcSI | DstAcc | Mov | String, em_mov), -I2bv (SrcAcc | DstDI | String | NoWrite, em_cmp_r), +I2bv (DstAcc | SrcImm | NoWrite | NoRex2, em_test), +I2bv (SrcAcc | DstDI | Mov | String | NoRex2, em_mov), +I2bv (SrcSI | DstAcc | Mov | String | NoRex2, em_mov), +I2bv (SrcAcc | DstDI | String | NoWrite | NoRex2, em_cmp_r),/* 0xB0 - 0xB7 */ X8 (I (ByteOp | DstReg | SrcImm | Mov, em_mov)), /* 0xB8 - 0xBF */
@@ -4335,17 +4358,17 @@ static const struct opcode opcode_table[256] = {/* 0xD8 - 0xDF */ N ,E (0 , &escape_d9),N ,E (0 , &escape_db),N ,E (0 , &escape_dd),N ,N ,/* 0xE0 - 0xE7 */ -X3 (I (SrcImmByte | NearBranch | IsBranch, em_loop)), -I (SrcImmByte | NearBranch | IsBranch, em_jcxz), -I2bvIP (SrcImmUByte | DstAcc, em_in, in, check_perm_in), -I2bvIP (SrcAcc | DstImmUByte, em_out, out, check_perm_out), +X3 (I (SrcImmByte | NearBranch | IsBranch | NoRex2, em_loop)), +I (SrcImmByte | NearBranch | IsBranch | NoRex2, em_jcxz), +I2bvIP (SrcImmUByte | DstAcc | NoRex2, em_in, in, check_perm_in), +I2bvIP (SrcAcc | DstImmUByte | NoRex2, em_out, out, check_perm_out),/* 0xE8 - 0xEF */ -I (SrcImm | NearBranch | IsBranch | ShadowStack, em_call), -D (SrcImm | ImplicitOps | NearBranch | IsBranch), -I (SrcImmFAddr | No64 | IsBranch, em_jmp_far), -D (SrcImmByte | ImplicitOps | NearBranch | IsBranch), -I2bvIP (SrcDX | DstAcc, em_in, in, check_perm_in), -I2bvIP (SrcAcc | DstDX, em_out, out, check_perm_out), +I (SrcImm | NearBranch | IsBranch | ShadowStack | NoRex2, em_call), +D (SrcImm | ImplicitOps | NearBranch | IsBranch | NoRex2), +I (SrcImmFAddr | No64 | IsBranch | NoRex2, em_jmp_far), +D (SrcImmByte | ImplicitOps | NearBranch | IsBranch | NoRex2), +I2bvIP (SrcDX | DstAcc | NoRex2, em_in, in, check_perm_in), +I2bvIP (SrcAcc | DstDX | NoRex2, em_out, out, check_perm_out),/* 0xF0 - 0xF7 */ N ,DI (ImplicitOps, icebp),N ,N , DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
@@ -4385,12 +4408,12 @@ static const struct opcode twobyte_table[256] = {N ,GP (ModRM | DstMem | SrcReg | Mov | Sse | Avx, &pfx_0f_2b),N ,N ,N ,N ,/* 0x30 - 0x3F */ -II (ImplicitOps | Priv, em_wrmsr, wrmsr), -IIP (ImplicitOps, em_rdtsc, rdtsc, check_rdtsc), -II (ImplicitOps | Priv, em_rdmsr, rdmsr), -IIP (ImplicitOps, em_rdpmc, rdpmc, check_rdpmc), -I (ImplicitOps | EmulateOnUD | IsBranch | ShadowStack, em_sysenter), -I (ImplicitOps | Priv | EmulateOnUD | IsBranch | ShadowStack, em_sysexit), +II (ImplicitOps | Priv | NoRex2, em_wrmsr, wrmsr), +IIP (ImplicitOps | NoRex2, em_rdtsc, rdtsc, check_rdtsc), +II (ImplicitOps | Priv | NoRex2, em_rdmsr, rdmsr), +IIP (ImplicitOps | NoRex2, em_rdpmc, rdpmc, check_rdpmc), +I (ImplicitOps | EmulateOnUD | IsBranch | ShadowStack | NoRex2, em_sysenter), +I (ImplicitOps | Priv | EmulateOnUD | IsBranch | ShadowStack | NoRex2, em_sysexit),N ,N ,N ,N ,N ,N ,N ,N ,N ,N , /* 0x40 - 0x4F */
@@ -4408,7 +4431,7 @@ static const struct opcode twobyte_table[256] = {N ,N ,N ,N ,N ,N ,N ,GP (SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),/* 0x80 - 0x8F */ -X16 (D (SrcImm | NearBranch | IsBranch)), +X16 (D (SrcImm | NearBranch | IsBranch | NoRex2)),/* 0x90 - 0x9F */ X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),/* 0xA0 - 0xA7 */
@@ -5065,6 +5088,18 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int ctxt->d |= opcode.flags; } +/* +* Instructions marked with NoRex2 ignore a legacy REX prefix, but #UD +* should be raised when prefixed with REX2. +*/ +if (ctxt->d & NoRex2 && ctxt->rex_prefix ==REX2_PREFIX ) { + ctxt->d = Undefined; + +/* JMPABS is not a memory operation so not implemented. */ +if (ctxt->b ==0xa1 ) + ctxt->d = NotImpl; + } + ctxt->is_branch = opcode.flags & IsBranch;/* Unrecognised? */
arch/x86/kvm/kvm_emulate.h +1 -0
@@ -335,6 +335,7 @@ typedef void (*fastop_t)(struct fastop *);enum rex_type {REX_NONE ,REX_PREFIX , +REX2_PREFIX , };struct x86_emulate_ctxt {
Signature · 1 line
2.53.0
PATCH v10 13/20 KVM: x86: Support REX2-prefixed opcode decode
In reply to Chang S. BaeExtend the instruction decoder to recognize and handle the REX2 prefix, including validation of prefix sequences and correct opcode table selection. Mark the UD opcode flag explicitly with Undefined, instead of zeroing.
REX2 is a terminal prefix: once 0xD5 is encountered, the following byte is the opcode. When REX2.M=0, most prefix bytes are invalid after REX2, including REX, VEX, EVEX, and another REX2. Also, REX2-prefixed instructions are only valid in 64-bit mode.
All of the invalid prefix combinations after REX2 coincide with opcodes that are architecturally invalid in 64-bit mode. Thus, marking such opcodes with No64 in opcode_table[] naturally disallows those illegal prefix sequences.
The 0x40 - 0x4F opcode row was missing the No64 flag. While NoRex2 already invalidates REX2 for these opcodes, adding No64 makes opcode attributes explicit and complete.
Note opcodes marked NotImpl alone end up in handle_emulation_failure(), but that will inject #UD eventually.
---
V9 -> V10: Collect a review tag
--- arch/x86/kvm/emulate.c | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-)
arch/x86/kvm/emulate.c +35 -5
@@ -3996,7 +3996,7 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt)I2bv (((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \ I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e) -static const struct opcode ud =I (SrcNone, emulate_ud); +static const struct opcode ud =I (SrcNone | Undefined, emulate_ud);static const struct opcode group7_rm0[] = {N ,
@@ -4278,7 +4278,7 @@ static const struct opcode opcode_table[256] = {/* 0x38 - 0x3F */ I6ALU (NoWrite, em_cmp),N ,N ,/* 0x40 - 0x4F */ -X8 (I (DstReg | NoRex2, em_inc)),X8 (I (DstReg | NoRex2, em_dec)), +X8 (I (DstReg | NoRex2 | No64, em_inc)),X8 (I (DstReg | NoRex2 | No64, em_dec)),/* 0x50 - 0x57 */ X8(I(SrcReg | Stack, em_push)),/* 0x58 - 0x5F */
@@ -4871,6 +4871,17 @@ static int x86_decode_avx(struct x86_emulate_ctxt *ctxt,return rc; } +static inline bool rex2_invalid (struct x86_emulate_ctxt *ctxt) +{ +const struct x86_emulate_ops *ops = ctxt->ops; +u64 xcr =0 ; + +return ctxt->rex_prefix ==REX_PREFIX || + !(ops->get_cr (ctxt,4 ) &X86_CR4_OSXSAVE ) || + ops->get_xcr (ctxt,0 , &xcr) || + !(xcr &XFEATURE_MASK_APX ); +} +int x86_decode_insn (struct x86_emulate_ctxt *ctxt,void *insn,int insn_len,int emulation_type) {int rc =X86EMUL_CONTINUE ;
@@ -4924,7 +4935,7 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int ctxt->op_bytes = def_op_bytes; ctxt->ad_bytes = def_ad_bytes; -/* Legacy prefixes. */ +/* Legacy and REX/REX2 prefixes. */ for (;;) {switch (ctxt->b =insn_fetch (u8, ctxt)) {case 0x66 :/* operand-size override */
@@ -4970,6 +4981,17 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int ctxt->rex_prefix =REX_PREFIX ; ctxt->rex_bits = ctxt->b &0xf ;continue ; +case 0xd5 :/* REX2 */ +if (mode !=X86EMUL_MODE_PROT64 ) + goto done_prefixes; +if (rex2_invalid (ctxt)) { + opcode = ud; + goto done_modrm; + } + ctxt->rex_prefix =REX2_PREFIX ; + ctxt->rex_bits =insn_fetch (u8, ctxt); + ctxt->b =insn_fetch (u8, ctxt); + goto done_prefixes;case 0xf0 :/* LOCK */ ctxt->lock_prefix =1 ;break ;
@@ -4992,6 +5014,12 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, intif (ctxt->rex_bits &REX_W ) ctxt->op_bytes =8 ; +/* REX2 opcode is one byte unless M-bit selects the two-byte map */ +if (ctxt->rex_bits &REX_M ) + goto decode_twobytes; +if (ctxt->rex_prefix ==REX2_PREFIX ) + goto decode_onebyte; +/* Opcode byte(s). */ if (ctxt->b ==0xc4 || ctxt->b ==0xc5 ) {/* VEX or LDS/LES */
@@ -5009,17 +5037,19 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int goto done; }else if (ctxt->b ==0x0f ) {/* Two- or three-byte opcode */ - ctxt->opcode_len =2 ; ctxt->b =insn_fetch (u8, ctxt); +decode_twobytes: + ctxt->opcode_len =2 ; opcode = twobyte_table[ctxt->b];/* 0F_38 opcode map */ -if (ctxt->b ==0x38 ) { +if (ctxt->b ==0x38 && ctxt->rex_prefix !=REX2_PREFIX ) { ctxt->opcode_len =3 ; ctxt->b =insn_fetch (u8, ctxt); opcode = opcode_map_0f_38[ctxt->b]; } }else { +decode_onebyte:/* Opcode byte(s). */ opcode = opcode_table[ctxt->b]; }
Signature · 1 line
2.53.0
PATCH v10 14/20 KVM: x86: Document EVEX-prefix not implemented
In reply to Chang S. BaeThe opcode is already marked as not implemented. Add a comment for clarity.
---
V9 -> V10: Collect a review tag
--- arch/x86/kvm/emulate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/emulate.c +2 -1
@@ -4286,7 +4286,8 @@ static const struct opcode opcode_table[256] = {/* 0x60 - 0x67 */ I (ImplicitOps | Stack | No64, em_pusha),I (ImplicitOps | Stack | No64, em_popa), -N ,MD (ModRM, &mode_dual_63), +N ,/* This effectively represents EVEX-prefixed instructions are not implemented */ +MD (ModRM, &mode_dual_63),N ,N ,N ,N ,/* 0x68 - 0x6F */ I(SrcImm | Mov | Stack, em_push),
Signature · 1 line
2.53.0
PATCH v10 15/20 KVM: x86: Move KVM_SUPPORTED_{XCR0,XSS} into kvm_x86_vendor_init()
In reply to Chang S. BaeFrom: Sean Christopherson <[email protected]>
Move KVM_SUPPORTED_{XCR0,XSS} into kvm_x86_vendor_init() as "const u64" values so that KVM's initialization code can further manipulate the set of supported XCR0/XSS features without ugly #ifdeffery and without risking other code treating KVM_SUPPORTED_{XCR0,XSS} as 100% authoritative.
No functional change intended.
--- arch/x86/kvm/cpuid.c | 2 +- arch/x86/kvm/x86.c | 25 +++++++++++++------------ 2 files changed, 14 insertions(+), 13 deletions(-)
arch/x86/kvm/cpuid.c +1 -1
@@ -265,7 +265,7 @@ static u32 kvm_apply_cpuid_pv_features_quirk(struct kvm_vcpu *vcpu)/* * Calculate guest's supported XCR0 taking into account guest CPUID data and -* KVM's supported XCR0 (comprised of host's XCR0 and KVM_SUPPORTED_XCR0). +* KVM's supported XCR0 (the host's actual XCR0 masked by what KVM supports). */ static u64 cpuid_get_supported_xcr0 (struct kvm_vcpu *vcpu) {
arch/x86/kvm/x86.c +13 -12
@@ -210,19 +210,7 @@ module_param(pi_inject_timer, bint, 0644);static bool __read_mostly mitigate_smt_rsb;module_param (mitigate_smt_rsb, bool,0444 ); -#define KVM_SUPPORTED_XCR0 (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \ - | XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \ - | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \ - | XFEATURE_MASK_PKRU | XFEATURE_MASK_XTILE) -#define XFEATURE_MASK_CET_ALL (XFEATURE_MASK_CET_USER | XFEATURE_MASK_CET_KERNEL) -/* -* Note, KVM supports exposing PT to the guest, but does not support context -* switching PT via XSTATE (KVM's PT virtualization relies on perf; swapping -* PT via guest XSTATE would clobber perf state), i.e. KVM doesn't support -* IA32_XSS[bit 8] (guests can/must use RDMSR/WRMSR to save/restore PT MSRs). -*/ -#define KVM_SUPPORTED_XSS (XFEATURE_MASK_CET_ALL)bool __read_mostly allow_smaller_maxphyaddr =0 ;EXPORT_SYMBOL_FOR_KVM_INTERNAL (allow_smaller_maxphyaddr);
@@ -7121,6 +7109,19 @@ static void kvm_x86_check_cpu_compat(void *ret)int kvm_x86_vendor_init (struct kvm_x86_init_ops *ops) { +const u64 KVM_SUPPORTED_XCR0 =XFEATURE_MASK_FP |XFEATURE_MASK_SSE | +XFEATURE_MASK_YMM |XFEATURE_MASK_BNDREGS | +XFEATURE_MASK_BNDCSR |XFEATURE_MASK_AVX512 | +XFEATURE_MASK_PKRU |XFEATURE_MASK_XTILE ; +/* +* Note, KVM supports exposing PT to the guest, but does not support +* context switching PT via XSTATE (KVM's PT virtualization relies on +* perf; swapping PT via guest XSTATE would clobber perf state), i.e. +* KVM doesn't support IA32_XSS[bit 8] (guests can/must use RDMSR/WRMSR +* to save/restore PT MSRs). +*/ +const u64 KVM_SUPPORTED_XSS =XFEATURE_MASK_CET_ALL ; +u64 host_pat;int r, cpu;
Signature · 1 line
2.53.0
PATCH v10 16/20 KVM: x86: Guard valid XCR0.APX settings
In reply to Chang S. BaePrevent invalid XCR0.APX configurations in two cases: conflict with MPX and lack of SVM support.
In the non-compacted XSAVE format, APX and MPX conflict on the same offset. Although MPX is being deprecated in practice, KVM should explicitly reject such configurations that set both bits.
At this point, only VMX supports EGPRs. SVM will require corresponding extensions to handle EGPR indices.
This is in preparation for APX in KVM's supported XCR0.
--- arch/x86/kvm/svm/svm.c | 7 ++++++- arch/x86/kvm/x86.c | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +6 -1
@@ -5758,8 +5758,13 @@ static __init int svm_hardware_setup(void)return -EOPNOTSUPP ; } +/* +* APX introduces EGPRs, which require additional VMCB support. +* Disable APX until the necessary extensions are handled. +*/ kvm_caps.supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | -XFEATURE_MASK_BNDCSR ); +XFEATURE_MASK_BNDCSR | +XFEATURE_MASK_APX );if (tsc_scaling) { if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR) ||
arch/x86/kvm/x86.c +4 -0
@@ -711,6 +711,10 @@ int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) (!(xcr0 & XFEATURE_MASK_BNDCSR)))return 1 ; +/* MPX and APX conflict in the non-compacted XSAVE format */ +if (xcr0 &XFEATURE_MASK_BNDREGS && xcr0 &XFEATURE_MASK_APX ) +return 1 ; +if (xcr0 &XFEATURE_MASK_AVX512 ) {if (!(xcr0 &XFEATURE_MASK_YMM ))return 1 ;
Signature · 1 line
2.53.0
PATCH v10 17/20 KVM: x86: Add APX to supported XCR0
In reply to Chang S. BaeSet kvm_caps.supported_xcr0[APX] when the host has enabled the APX state along with CONFIG_KVM_APX=y, and eVMCS is not in use as eVMCS does not support APX yet.
--- arch/x86/kvm/vmx/vmx.c | 4 ++++ arch/x86/kvm/x86.c | 2 ++ 2 files changed, 6 insertions(+)
arch/x86/kvm/vmx/vmx.c +4 -0
@@ -8571,6 +8571,10 @@ __init int vmx_hardware_setup(void) kvm_caps.supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS |XFEATURE_MASK_BNDCSR ); +/* eVMCS isn't prepared yet for APX */ +if (kvm_is_using_evmcs ()) + kvm_caps.supported_xcr0 &= ~XFEATURE_MASK_APX ; +if (!cpu_has_vmx_vpid () || !cpu_has_vmx_invvpid () || !(cpu_has_vmx_invvpid_single () ||cpu_has_vmx_invvpid_global ())) enable_vpid =0 ;
arch/x86/kvm/x86.c +2 -0
@@ -7194,6 +7194,8 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)if (boot_cpu_has (X86_FEATURE_XSAVE )) { kvm_host.xcr0 =xgetbv (XCR_XFEATURE_ENABLED_MASK ); kvm_caps.supported_xcr0 = kvm_host.xcr0 &KVM_SUPPORTED_XCR0 ; +if (IS_ENABLED (CONFIG_KVM_APX )) + kvm_caps.supported_xcr0 |= kvm_host.xcr0 &XFEATURE_MASK_APX ; }if (boot_cpu_has (X86_FEATURE_XSAVES )) {
Signature · 1 line
2.53.0
PATCH v10 18/20 KVM: x86: Expose APX feature to userspace
In reply to Chang S. BaeExpose the APX feature and its sub-features to userspace. Add support for CPUID leaf 0x29, sub-leaf 0, to enumerate the APX sub-features.
On SVM or when using eVMCS, do not advertise APX, as EGPR support is not implemented yet.
---
V9 -> V10: Collect a review tag
--- arch/x86/include/asm/cpuid/api.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/cpuid.c | 21 ++++++++++++++++++++- arch/x86/kvm/reverse_cpuid.h | 4 ++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/vmx/vmx.c | 4 ++++ 6 files changed, 31 insertions(+), 1 deletion(-)
arch/x86/include/asm/cpuid/api.h +1 -0
@@ -183,6 +183,7 @@ static __always_inline bool cpuid_function_is_indexed(u32 function)case 0x1f :case 0x23 :case 0x24 : +case 0x29 :case 0x8000001d :return true; }
arch/x86/include/asm/kvm_host.h +1 -0
@@ -729,6 +729,7 @@ enum kvm_only_cpuid_leafs {CPUID_7_1_ECX ,CPUID_1E_1_EAX ,CPUID_24_1_ECX , +CPUID_29_0_EBX ,NR_KVM_CPU_CAPS , NKVMCAPINTS = NR_KVM_CPU_CAPS - NCAPINTS,
arch/x86/kvm/cpuid.c +20 -1
@@ -1049,8 +1049,12 @@ void kvm_initialize_cpu_caps(void)F (AVX_VNNI_INT16 ),F (PREFETCHITI ),F (AVX10 ), + SCATTERED_F(APX), ); +if (!IS_ENABLED (CONFIG_KVM_APX )) +kvm_cpu_cap_clear (X86_FEATURE_APX ); + kvm_cpu_cap_init(CPUID_7_2_EDX, F(INTEL_PSFD), F(IPRED_CTRL),
@@ -1095,6 +1099,10 @@ void kvm_initialize_cpu_caps(void)F (AVX10_V1_AUX ), ); +kvm_cpu_cap_init (CPUID_29_0_EBX , + F(APX_NCI_NDD_NF), + ); + kvm_cpu_cap_init(CPUID_8000_0001_ECX, F(LAHF_LM), F(CMP_LEGACY),
@@ -1463,7 +1471,7 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)switch (function) {case 0 :/* Limited to the highest leaf implemented in KVM. */ - entry->eax =min (entry->eax,0x24U ); + entry->eax =min (entry->eax,0x29U );break ;case 1 :cpuid_entry_override (entry,CPUID_1_EDX );
@@ -1734,6 +1742,17 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) }break ; } +/* APX sub-features */ +case 0x29 : +if (!kvm_cpu_cap_has (X86_FEATURE_APX )) { + entry->eax = entry->ebx = entry->ecx = entry->edx =0 ; +break ; + } +cpuid_entry_override (entry,CPUID_29_0_EBX ); + entry->eax =0 ; + entry->ecx =0 ; + entry->edx =0 ; +break ;case KVM_CPUID_SIGNATURE : {const u32 *sigptr = (const u32 *)KVM_SIGNATURE ; entry->eax =KVM_CPUID_FEATURES ;
arch/x86/kvm/reverse_cpuid.h +4 -0
@@ -69,6 +69,9 @@/* Intel-defined sub-features, CPUID level 0x00000024:1 (ECX) */ #define X86_FEATURE_AVX10_V1_AUX KVM_X86_FEATURE(CPUID_24_1_ECX, 2) +/* Intel-defined sub-features, CPUID level 0x00000029:0 (EBX) */ +#define X86_FEATURE_APX_NCI_NDD_NF KVM_X86_FEATURE(CPUID_29_0_EBX, 0) +/* CPUID level 0x80000007 (EDX). */ #define KVM_X86_FEATURE_CONSTANT_TSC KVM_X86_FEATURE(CPUID_8000_0007_EDX, 8)
@@ -112,6 +115,7 @@ static const struct cpuid_reg reverse_cpuid[] = { [CPUID_7_1_ECX] = { 7, 1, CPUID_ECX}, [CPUID_1E_1_EAX] = { 0x1e, 1, CPUID_EAX}, [CPUID_24_1_ECX] = { 0x24, 1, CPUID_ECX}, + [CPUID_29_0_EBX] = { 0x29, 0, CPUID_EBX}, };/*
arch/x86/kvm/svm/svm.c +1 -0
@@ -5730,6 +5730,7 @@ static __init void svm_set_cpu_caps(void)* but that require explicit SVM support (that isn't yet implemented). */ kvm_cpu_cap_clear (X86_FEATURE_MSR_IMM ); +kvm_cpu_cap_clear (X86_FEATURE_APX );/* * LBR Virtualization must be enabled to support BusLockTrap inside the
arch/x86/kvm/vmx/vmx.c +4 -0
@@ -8023,6 +8023,10 @@ static __init void vmx_set_cpu_caps(void)*/ kvm_cpu_cap_set (X86_FEATURE_EFER_LMSLE_MBZ ); +/* eVMCS isn't prepared yet for APX */ +if (kvm_is_using_evmcs ()) +kvm_cpu_cap_clear (X86_FEATURE_APX ); +kvm_setup_xss_caps ();kvm_finalize_cpu_caps (); }
Signature · 1 line
2.53.0
PATCH v10 19/20 KVM: selftests: Add APX state and ABI test
In reply to Chang S. BaeTest APX-specific behavior and ABI interactions as these are unique compared to other state components:
* EGPR state is switched on VM entry assembly (unlike other components).
* The saved state is retained even if the guest disables APX.
* EGPR state is exposed via the XSAVE ABI while not stored in the kernel XSAVE buffer.
* EGPR state is initialized through the XSAVE ABI by clearing
XSTATE_BV[APX].
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/include/x86/processor.h | 120 ++++++++++ tools/testing/selftests/kvm/x86/apx_test.c | 209 ++++++++++++++++++ 3 files changed, 330 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/apx_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -169,6 +169,7 @@ TEST_GEN_PROGS_x86 += x86/recalc_apic_map_test TEST_GEN_PROGS_x86 += x86/aperfmperf_test TEST_GEN_PROGS_x86 += x86/guest_memfd_conversions_test TEST_GEN_PROGS_x86 += x86/tdx_vm_test +TEST_GEN_PROGS_x86 += x86/apx_test TEST_GEN_PROGS_x86 += access_tracking_perf_test TEST_GEN_PROGS_x86 += coalesced_io_test TEST_GEN_PROGS_x86 += dirty_log_perf_test
tools/testing/selftests/kvm/include/x86/processor.h +120 -0
138 lines not on this page · raw message
tools/testing/selftests/kvm/x86/apx_test.c new file +209 -0
209 lines not on this page · raw message
Signature · 1 line
2.53.0
PATCH v10 20/20 KVM: selftests: Add APX state handling and XCR0 sanity checks
In reply to Chang S. BaeNow that KVM exposes the APX feature to guests, extend some existing selftests to validate XCR0 configuration and state management.
Since APX repurposes the XSAVE area previously used by MPX in the non-compacted format, add a check to ensure that MPX state is not set when APX is enabled.
Also, load non-init APX state for the ABI test.
--- tools/testing/selftests/kvm/x86/state_test.c | 3 +++ .../selftests/kvm/x86/xcr0_cpuid_test.c | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+)
tools/testing/selftests/kvm/x86/state_test.c +3 -0
@@ -164,6 +164,9 @@ static void __attribute__((__flatten__)) guest_code(void *arg) asmvolatile ("vmovupd %0, %%zmm16" ::"m" (buffer)); } +if (supported_xcr0 &XFEATURE_MASK_APX ) +write_egpr (16 ,0xcccccccc ); +if (this_cpu_has (X86_FEATURE_MPX )) {u64 bounds[2 ] = {10 ,0xffffffffull };u64 output[2 ] = { };
tools/testing/selftests/kvm/x86/xcr0_cpuid_test.c +19 -0
@@ -46,6 +46,20 @@ do { \ __supported, (xfeatures)); \ }while (0 ) +/* +* Verify that mutually exclusive architectural features do not overlap. +* For example, APX and MPX must never be reported as supported together. + */ +#define ASSERT_XFEATURE_CONFLICT (supported_xcr0, xfeatures, conflicts) \ +do { \ + u64 __supported = (supported_xcr0) & ((xfeatures) | (conflicts)); \ + \ + __GUEST_ASSERT((__supported & (xfeatures)) != (xfeatures) || \ + !(__supported & (conflicts)), \ + "supported = 0x%lx, xfeatures = 0x%llx, conflicts = 0x%llx", \ + __supported, (xfeatures), (conflicts)); \ +} while (0) +static void guest_code (void ) {u64 initial_xcr0;
@@ -79,6 +93,11 @@ static void guest_code(void)ASSERT_ALL_OR_NONE_XFEATURE (supported_xcr0,XFEATURE_MASK_XTILE ); +/* Check APX by ensuring MPX is not exposed concurrently */ +ASSERT_XFEATURE_CONFLICT (supported_xcr0, +XFEATURE_MASK_APX , +XFEATURE_MASK_BNDREGS |XFEATURE_MASK_BNDCSR ); + vector =xsetbv_safe (0 ,XFEATURE_MASK_FP ); __GUEST_ASSERT(!vector, "Expected success on XSETBV(FP), got %s",
Signature · 1 line
2.53.0