Add a helper to read GPR index and move INVPCID validation to a common place
v4 cover Add a helper to read GPR index and move INVPCID validation to a common place
INVPCID, INVVPID, and INVEPT instructions retrieve the GPR index similarly to find the invalidation type. Patch 1 moves the shift and mask magic to a single place.
INVPCID invalidation type check is same for both VMX and SVM. This instruction is not documented to verify the type before reading the operand from memory. So, moving the check to a common place in patch 2.
v4:
- Changed commit message of the patch 1
v3:
- https://lore.kernel.org/lkml/[email protected]/
- Patch 2's commit message is more detailed now.
v2:
- https://lore.kernel.org/lkml/[email protected]/
- Keeping the register read visible in the functions.
- Removed INVPCID type check hardcoding and moved error condition to common function.
v1: https://lore.kernel.org/lkml/[email protected]/
Vipin Sharma (2):
KVM: VMX: Add a helper function to retrieve the GPR index for INVPCID,
INVVPID, and INVEPT
KVM: Move INVPCID type check from vmx and svm to the common
kvm_handle_invpcid()
arch/x86/kvm/svm/svm.c | 5 ----- arch/x86/kvm/vmx/nested.c | 10 ++++++---- arch/x86/kvm/vmx/vmx.c | 9 +++------ arch/x86/kvm/vmx/vmx.h | 5 +++++ arch/x86/kvm/x86.c | 3 ++- 5 files changed, 16 insertions(+), 16 deletions(-)
Signature · 1 line
2.34.0.rc0.344.g81b53c2807-goog
PATCH v4 1/2 KVM: VMX: Add a helper function to retrieve the GPR index for INVPCID, INVVPID, and INVEPT
handle_invept(), handle_invvpid(), handle_invpcid() read the same reg2 field in vmcs.VMX_INSTRUCTION_INFO to get the index of the GPR that holds the invalidation type. Add a helper to retrieve reg2 from VMX instruction info to consolidate and document the shift+mask magic.
--- arch/x86/kvm/vmx/nested.c | 10 ++++++---- arch/x86/kvm/vmx/vmx.c | 4 +++- arch/x86/kvm/vmx/vmx.h | 5 +++++ 3 files changed, 14 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/nested.c +6 -4
@@ -5379,7 +5379,7 @@ static int handle_invept(struct kvm_vcpu *vcpu)struct {u64 eptp, gpa; } operand; -int i, r; +int i, r, gpr_index; if (!(vmx->nested.msrs.secondary_ctls_high & SECONDARY_EXEC_ENABLE_EPT) ||
@@ -5392,7 +5392,8 @@ static int handle_invept(struct kvm_vcpu *vcpu)return 1 ; vmx_instruction_info =vmcs_read32 (VMX_INSTRUCTION_INFO ); - type =kvm_register_read (vcpu, (vmx_instruction_info >>28 ) &0xf ); + gpr_index =vmx_get_instr_info_reg2 (vmx_instruction_info); + type =kvm_register_read (vcpu, gpr_index); types = (vmx->nested.msrs.ept_caps >>VMX_EPT_EXTENT_SHIFT ) &6 ;
@@ -5459,7 +5460,7 @@ static int handle_invvpid(struct kvm_vcpu *vcpu)u64 gla; } operand;u16 vpid02; -int r; +int r, gpr_index; if (!(vmx->nested.msrs.secondary_ctls_high & SECONDARY_EXEC_ENABLE_VPID) ||
@@ -5472,7 +5473,8 @@ static int handle_invvpid(struct kvm_vcpu *vcpu)return 1 ; vmx_instruction_info =vmcs_read32 (VMX_INSTRUCTION_INFO ); - type =kvm_register_read (vcpu, (vmx_instruction_info >>28 ) &0xf ); + gpr_index =vmx_get_instr_info_reg2 (vmx_instruction_info); + type =kvm_register_read (vcpu, gpr_index); types = (vmx->nested.msrs.vpid_caps &VMX_VPID_EXTENT_SUPPORTED_MASK ) >>8 ;
arch/x86/kvm/vmx/vmx.c +3 -1
@@ -5494,6 +5494,7 @@ static int handle_invpcid(struct kvm_vcpu *vcpu)u64 pcid;u64 gla; } operand; +int gpr_index;if (!guest_cpuid_has (vcpu,X86_FEATURE_INVPCID )) {kvm_queue_exception (vcpu,UD_VECTOR );
@@ -5501,7 +5502,8 @@ static int handle_invpcid(struct kvm_vcpu *vcpu) } vmx_instruction_info =vmcs_read32 (VMX_INSTRUCTION_INFO ); - type =kvm_register_read (vcpu, (vmx_instruction_info >>28 ) &0xf ); + gpr_index =vmx_get_instr_info_reg2 (vmx_instruction_info); + type =kvm_register_read (vcpu, gpr_index);if (type >3 ) {kvm_inject_gp (vcpu,0 );
arch/x86/kvm/vmx/vmx.h +5 -0
@@ -522,4 +522,9 @@ 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_reg2 (u32 vmx_instr_info) +{ +return (vmx_instr_info >>28 ) &0xf ; +} +#endif /* __KVM_X86_VMX_H */
Signature · 1 line
2.34.0.rc0.344.g81b53c2807-goog
PATCH v4 2/2 KVM: Move INVPCID type check from vmx and svm to the common kvm_handle_invpcid()
In reply to Vipin SharmaHandle #GP on INVPCID due to an invalid type in the common switch statement instead of relying on the callers (VMX and SVM) to manually validate the type.
Unlike INVVPID and INVEPT, INVPCID is not explicitly documented to check the type before reading the operand from memory, so deferring the type validity check until after that point is architecturally allowed.
--- arch/x86/kvm/svm/svm.c | 5 ----- arch/x86/kvm/vmx/vmx.c | 5 ----- arch/x86/kvm/x86.c | 3 ++- 3 files changed, 2 insertions(+), 11 deletions(-)
arch/x86/kvm/svm/svm.c +0 -5
@@ -3119,11 +3119,6 @@ static int invpcid_interception(struct kvm_vcpu *vcpu) type = svm->vmcb->control.exit_info_2; gva = svm->vmcb->control.exit_info_1; -if (type >3 ) { -kvm_inject_gp (vcpu,0 ); -return 1 ; - } -return kvm_handle_invpcid (vcpu, type, gva); }
arch/x86/kvm/vmx/vmx.c +0 -5
@@ -5505,11 +5505,6 @@ static int handle_invpcid(struct kvm_vcpu *vcpu) gpr_index =vmx_get_instr_info_reg2 (vmx_instruction_info); type =kvm_register_read (vcpu, gpr_index); -if (type >3 ) { -kvm_inject_gp (vcpu,0 ); -return 1 ; - } -/* According to the Intel instruction reference, the memory operand * is read even if it isn't needed (e.g., for type==all) */
arch/x86/kvm/x86.c +2 -1
@@ -12443,7 +12443,8 @@ int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva)return kvm_skip_emulated_instruction (vcpu);default : -BUG ();/* We have already checked above that type <= 3 */ +kvm_inject_gp (vcpu,0 ); +return 1 ; } }EXPORT_SYMBOL_GPL (kvm_handle_invpcid);
Signature · 1 line
2.34.0.rc0.344.g81b53c2807-goog
Quoted 29 lines · Vipin Sharma
INVPCID, INVVPID, and INVEPT instructions retrieve the GPR index similarly to find the invalidation type. Patch 1 moves the shift and mask magic to a single place.
INVPCID invalidation type check is same for both VMX and SVM. This instruction is not documented to verify the type before reading the operand from memory. So, moving the check to a common place in patch 2.
v4:
- Changed commit message of the patch 1v3:
- https://lore.kernel.org/lkml/[email protected]/
- Patch 2's commit message is more detailed now.v2:
- https://lore.kernel.org/lkml/[email protected]/
- Keeping the register read visible in the functions.
- Removed INVPCID type check hardcoding and moved error condition to common function.v1: https://lore.kernel.org/lkml/[email protected]/
Vipin Sharma (2):
KVM: VMX: Add a helper function to retrieve the GPR index for INVPCID,
INVVPID, and INVEPT
KVM: Move INVPCID type check from vmx and svm to the common
kvm_handle_invpcid()
Quoted 7 lines
arch/x86/kvm/svm/svm.c | 5 ----- arch/x86/kvm/vmx/nested.c | 10 ++++++---- arch/x86/kvm/vmx/vmx.c | 9 +++------ arch/x86/kvm/vmx/vmx.h | 5 +++++ arch/x86/kvm/x86.c | 3 ++- 5 files changed, 16 insertions(+), 16 deletions(-)
Queued, thanks.
Paolo