KVM: nVMX: Don't clobber VM-Entry instruction length on soft INTn exits
v3 cover KVM: nVMX: Don't clobber VM-Entry instruction length on soft INTn exits
KVM writes the length of a soft INTn that L2 was vectoring at the time of a nested VM-Exit into vmcs12's VM-Entry instruction length instead of the VM-Exit instruction length.
Patch 1 is there because Sashiko pointed out that prepare_vmcs12() overwrites the VM-exit instruction length that vmcs12_save_pending_event() saves for soft events.
v3:
- Rebased onto kvm-x86/next.
v2:
- New Patch 1
- Same Patch 2 from v1 (with commit message changes)
- Patch 3 now extends nested_exceptions_test instead of adding a new test (suggested by Sashiko).
Mahmoud Abumandour (3):
KVM: nVMX: Fill VM-Exit instruction length before saving pending event
KVM: nVMX: Don't clobber VM-Entry instruction length when saving soft
INTn
KVM: selftests: Verify nested VM-Exits while vectoring INT3 and INTn
arch/x86/kvm/vmx/nested.c | 6 ++- .../kvm/x86/nested_exceptions_test.c | 39 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-)
base-commit: cbe54fc3462b3949ab27b9ae81fb9ddb509040c7
Signature · 1 line
2.25.1
PATCH v3 1/3 KVM: nVMX: Fill VM-Exit instruction length before saving pending event
Fill vmcs12's VM-Exit instruction length before calling vmcs12_save_pending_event(), so that the length it saves for a software exception isn't overwritten. Commit c3634d25fbee ("KVM: nVMX: Leave most VM-Exit info fields unmodified on failed VM-Entry") moved the assignment after the call.
L1 sees no difference since both lengths are read from vmcs02's VM-Exit instruction length.
--- arch/x86/kvm/vmx/nested.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/nested.c +3 -1
@@ -4741,6 +4741,9 @@ static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,* instead of reading the real value. */ vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK ; +/* vmcs12_save_pending_event() may override this. */ + vmcs12->vm_exit_instruction_len = exit_insn_len; +/* * Transfer the event that L0 or L1 may wanted to inject into * L2 to IDT_VECTORING_INFO_FIELD.
@@ -4749,7 +4752,6 @@ static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, vm_exit_reason, exit_intr_info); 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 );/*
Signature · 1 line
2.25.1
Re: [PATCH v3 1/3] KVM: nVMX: Fill VM-Exit instruction length before saving pending event
Quoted 11 lines · Mahmoud Abumandour
Fill vmcs12's VM-Exit instruction length before calling
vmcs12_save_pending_event(), so that the length it saves for a software exception isn't overwritten. Commitc3634d25fbee("KVM: nVMX: Leave most VM-Exit info fields unmodified on failed VM-Entry") moved the assignment after the call.L1 sees no difference since both lengths are read from vmcs02's VM-Exit instruction length.
Sashiko is wrong. Neither of the scenarios it cited in v1 holds:
1. After vmx_cancel_injection(): if nested_run_pending is set, vmx_check_nested_events() blocks all nested VM-Exits (and __nested_vmx_vmexit() warns if called with nested_run_pending set). Once vmcs02 actually runs and exits, hardware copies VM_ENTRY_INSTRUCTION_LEN into VM_EXIT_INSTRUCTION_LEN if a VM-Exit occurs during delivery of an injected soft event (SDM vol. 3, section 30.2.5). If nested_run_pending is clear, VM_ENTRY_INSTRUCTION_LEN came from event_exit_inst_len, which already came from vmcs02's VM_EXIT_INSTRUCTION_LEN.
2. After KVM_SET_NESTED_STATE + KVM_SET_VCPU_EVENTS:
kvm_vcpu_ioctl_x86_set_vcpu_events() never updates vcpu->arch.event_exit_inst_len (struct kvm_vcpu_events has no instruction length field), so on a reused vCPU event_exit_inst_len is just stale junk.
Moreover, L1 *can* see a difference, and this patch introduces a subtle regression for SGX enclave-mode #BP. Per the SDM, volume 3, sections 30.2.1, 30.2.4, and 30.2.5:
- A #BP that occurs in enclave mode is reported as a hardware exception (INTR_TYPE_HARD_EXCEPTION).
- A VM-Exit due to an exception encountered during delivery of an enclave-mode exception still sets bit 27 (enclave mode) of the exit reason.
- "If the VM exit occurred in enclave mode, this field [VM-exit instruction length] is cleared (none of the previous items apply)."
In __vmx_complete_interrupts(), INTR_TYPE_HARD_EXCEPTION does not update vcpu->arch.event_exit_inst_len, so it retains whatever stale length was saved on an earlier soft event. Because vmcs12_save_pending_event() checks kvm_exception_is_soft(nr) unconditionally, it misclassifies an enclave-mode #BP as a software exception (a pre-existing bug, outside the scope of this series). Before this patch, prepare_vmcs12() at least overwrote vmcs12->vm_exit_instruction_len with exit_insn_len (0). With this patch, vmcs12_save_pending_event() clobbers 0 with stale vcpu->arch.event_exit_inst_len.
AFAICT, there is no valid path to prepare_vmcs12() where vcpu->arch.event_exit_inst_len is better than exit_insn_len. Can't we just drop this patch and delete both vcpu->arch.event_exit_inst_len assignments in vmcs12_save_pending_event() in patch 2, letting prepare_vmcs12() be the sole writer of vmcs12->vm_exit_instruction_len?
PATCH v3 2/3 KVM: nVMX: Don't clobber VM-Entry instruction length when saving soft INTn
In reply to Mahmoud AbumandourWhen a nested VM-Exit occurs while L2 is vectoring a software interrupt, save the INTn's length in vmcs12's VM-Exit instruction length, as is done for software exceptions, instead of VM-Entry instruction length.
--- arch/x86/kvm/vmx/nested.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/nested.c +1 -1
@@ -3993,7 +3993,7 @@ static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,if (vcpu->arch.interrupt.soft) { idt_vectoring |=INTR_TYPE_SOFT_INTR ; - vmcs12->vm_entry_instruction_len = + vmcs12->vm_exit_instruction_len = vcpu->arch.event_exit_inst_len; }else idt_vectoring |=INTR_TYPE_EXT_INTR ;
Signature · 1 line
2.25.1
PATCH v3 3/3 KVM: selftests: Verify nested VM-Exits while vectoring INT3 and INTn
In reply to Mahmoud AbumandourVerify that a VM-Exit while L2 is vectoring INT3 or INTn through the empty IDT reports the event and its instruction length, and leaves the VM-Entry instruction length alone. Only VMX is tested, as both lengths are VMX fields.
--- .../kvm/x86/nested_exceptions_test.c | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+)
tools/testing/selftests/kvm/x86/nested_exceptions_test.c +39 -0
@@ -40,12 +40,25 @@#define GP_ERROR_CODE_INTEL ((SS_VECTOR * 8) | ERROR_CODE_IDT_FLAG | ERROR_CODE_EXT_FLAG)#define GP_ERROR_CODE_ZHAOXIN ((SS_VECTOR * 8) | ERROR_CODE_IDT_FLAG) +/* No "External event" flag, as INT3 and INTn are software events. */ +#define GP_ERROR_CODE_SOFT_INT (vector) (((vector) * 8) | ERROR_CODE_IDT_FLAG) +/* * Intel and AMD both shove '0' into the error code on #DF, regardless of what * led to the double fault. */ #define DF_ERROR_CODE 0 +/* Arbitrary, any vector #GPs with the empty IDT. */ +#define INTN_VECTOR 0x40 + +#define INTR_INFO_VALID_MASK BIT(31) +#define INTR_TYPE_SOFT_INTR (4 << 8) +#define INTR_TYPE_SOFT_EXCEPTION (6 << 8) + +/* Arbitrary, but not the length of INT3 or INTn. */ +#define VM_ENTRY_INSN_LEN 7 +#define INTERCEPT_SS (BIT_ULL(SS_VECTOR))#define INTERCEPT_SS_DF (INTERCEPT_SS | BIT_ULL(DF_VECTOR))#define INTERCEPT_SS_GP_DF (INTERCEPT_SS_DF | BIT_ULL(GP_VECTOR))
@@ -70,6 +83,16 @@ static void l2_ss_injected_tf_test(void)GUEST_SYNC (FAKE_TRIPLE_FAULT_VECTOR ); } +static void l2_int3_test (void ) +{ + asmvolatile ("int3" ); +} + +static void l2_intn_test (void ) +{ + asm volatile("int $" __stringify(INTN_VECTOR)); +} +static void svm_run_l2 (struct svm_test_data *svm,void *l2_code,int vector,u32 error_code) {
@@ -172,6 +195,18 @@ static void vmx_run_l2(void *l2_code, int vector, u32 error_code)GUEST_ASSERT (!vmread (GUEST_INTERRUPTIBILITY_INFO )); } +static void vmx_run_l2_soft_int (void *l2_code,u32 type,int vector, +u32 insn_len) +{ +vmwrite (VM_ENTRY_INSTRUCTION_LEN ,VM_ENTRY_INSN_LEN ); +vmx_run_l2 (l2_code,GP_VECTOR ,GP_ERROR_CODE_SOFT_INT (vector)); + +GUEST_ASSERT_EQ (vmread (IDT_VECTORING_INFO_FIELD ), +INTR_INFO_VALID_MASK | type | vector); +GUEST_ASSERT_EQ (vmread (VM_EXIT_INSTRUCTION_LEN ), insn_len); +GUEST_ASSERT_EQ (vmread (VM_ENTRY_INSTRUCTION_LEN ),VM_ENTRY_INSN_LEN ); +} +static void l1_vmx_code (struct vmx_pages *vmx) {unsigned long cr0;
@@ -193,6 +228,10 @@ static void l1_vmx_code(struct vmx_pages *vmx)vmx_run_l2 (l2_ss_injected_gp_test,GP_VECTOR , host_cpu_is_zhaoxin ?GP_ERROR_CODE_ZHAOXIN :GP_ERROR_CODE_INTEL ); +/* INT3 and INTn also #GP, due to the empty IDT. */ +vmx_run_l2_soft_int (l2_int3_test,INTR_TYPE_SOFT_EXCEPTION ,BP_VECTOR ,1 ); +vmx_run_l2_soft_int (l2_intn_test,INTR_TYPE_SOFT_INTR ,INTN_VECTOR ,2 ); +vmwrite (EXCEPTION_BITMAP ,INTERCEPT_SS_DF );vmx_run_l2 (l2_ss_injected_df_test,DF_VECTOR ,DF_ERROR_CODE );
Signature · 1 line
2.25.1
Re: [PATCH v3 3/3] KVM: selftests: Verify nested VM-Exits while vectoring INT3 and INTn
Quoted 7 lines · Mahmoud Abumandour
Verify that a VM-Exit while L2 is vectoring INT3 or INTn through the empty IDT reports the event and its instruction length, and leaves the VM-Entry instruction length alone. Only VMX is tested, as both lengths are VMX fields.
Signed-off-by Mahmoud Abumandour <[email protected]>
You might consider testing the scenarios that Sashiko flagged in V1.
Sean Christopherson
maintainerQuoted 10 lines · Mahmoud Abumandour
KVM writes the length of a soft INTn that L2 was vectoring at the time of a nested VM-Exit into vmcs12's VM-Entry instruction length instead of the VM-Exit instruction length.
Patch 1 is there because Sashiko pointed out that
prepare_vmcs12()overwrites the VM-exit instruction length thatvmcs12_save_pending_event()saves for soft events.v3:
- Rebased onto kvm-x86/next.
v2 still applies cleanly, there's no need to rebase. I realize it's been several weeks since you posted v2, but the reality is that everyone and their mother is using AI to send patches right now, and so anything that isn't affecting real world use cases is very low priority. Sorry.
TL;DR: please be patient.
Quoted 1 line · Sean Christopherson
v2 still applies cleanly, there's no need to rebase.
[...]
Quoted 1 line
TL;DR: please be patient.
Understood, sorry for the noise. Plain git am failed on patch 3's context here, and I should have tried -3 before rebasing.