Currently, KVM doesn't inject a privileged software exception itself, but L1 can inject an INT1 into L2. If a VM-exit interrupts the delivery of the INT1, KVM drops it, as it requeues only hardware and software exceptions. For example, a delivery is interrupted when it touches an IDT or stack page that isn't mapped yet. If KVM handles the VM-exit itself, L2 then resumes without the INT1. If L1 handles it, L1 doesn't find the INT1 in the IDT-vectoring information and can't re-inject it. Handle an INT1 the way KVM handles a software exception, which also needs its instruction length. Requeue it with its length, re-inject it with its own event type, and report it to L1 with that type and length. Check the event type before the vector, as L1 can inject a privileged software exception with any vector. When the INT1 goes through a task gate, KVM emulates the task switch. Clear the requeued INT1 there, so that KVM doesn't inject it twice. And leave a queued INT1 out of the vCPU events, as the uAPI doesn't provide a mechanism to carry its length. Signed-off-by: Sohil Mehta --- Patch 3 relies on this patch, as it makes KVM inject an ICEBP's #DB as a privileged software exception. Without this patch, KVM would inject that #DB as a hardware exception with RIP still on the ICEBP. The guest would then execute the ICEBP forever, and patch 4's test would hang. No Fixes tag, as KVM has dropped this event type ever since nested VMX started passing L1's injected events through to L2. --- arch/x86/kvm/vmx/nested.c | 13 ++++++++++--- arch/x86/kvm/vmx/vmx.c | 12 ++++++++++-- arch/x86/kvm/x86.c | 11 ++++++----- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 1a98f28651f7..6cb07873998d 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -3970,12 +3970,17 @@ static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, nr = vcpu->arch.exception.vector; idt_vectoring = nr | VECTORING_INFO_VALID_MASK; - if (kvm_exception_is_soft(nr)) { + if (vcpu->arch.exception.event_type == EVENT_TYPE_PRIV_SWEXC) { + vmcs12->vm_exit_instruction_len = + vcpu->arch.event_exit_inst_len; + idt_vectoring |= INTR_TYPE_PRIV_SW_EXCEPTION; + } else if (kvm_exception_is_soft(nr)) { vmcs12->vm_exit_instruction_len = vcpu->arch.event_exit_inst_len; idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; - } else + } else { idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; + } if (vcpu->arch.exception.has_error_code) { idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; @@ -4085,7 +4090,9 @@ static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu) intr_info |= INTR_INFO_DELIVER_CODE_MASK; } - if (kvm_exception_is_soft(ex->vector)) + if (ex->event_type == EVENT_TYPE_PRIV_SWEXC) + intr_info |= INTR_TYPE_PRIV_SW_EXCEPTION; + else if (kvm_exception_is_soft(ex->vector)) intr_info |= INTR_TYPE_SOFT_EXCEPTION; else intr_info |= INTR_TYPE_HARD_EXCEPTION; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index a6c0e8ccdfad..fdc531a61fd3 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -1973,12 +1973,18 @@ void vmx_inject_exception(struct kvm_vcpu *vcpu) WARN_ON_ONCE(vmx->vt.emulation_required); - if (kvm_exception_is_soft(ex->vector)) { + /* L1 can inject a privileged software exception with any vector. */ + if (ex->event_type == EVENT_TYPE_PRIV_SWEXC) { + vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, + vmx->vcpu.arch.event_exit_inst_len); + intr_info |= INTR_TYPE_PRIV_SW_EXCEPTION; + } else if (kvm_exception_is_soft(ex->vector)) { 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; + } vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); @@ -5946,6 +5952,7 @@ static int handle_task_switch(struct kvm_vcpu *vcpu) vmcs_read32(IDT_VECTORING_ERROR_CODE); } fallthrough; + case INTR_TYPE_PRIV_SW_EXCEPTION: case INTR_TYPE_SOFT_EXCEPTION: kvm_clear_exception_queue(vcpu); break; @@ -7185,6 +7192,7 @@ static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, */ vmx_set_nmi_mask(vcpu, false); break; + case INTR_TYPE_PRIV_SW_EXCEPTION: case INTR_TYPE_SOFT_EXCEPTION: vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); fallthrough; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index c27813bde407..b58518a777de 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3010,12 +3010,13 @@ static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu, memset(events, 0, sizeof(*events)); /* - * The API doesn't provide the instruction length for software - * exceptions, so don't report them. As long as the guest RIP - * isn't advanced, we should expect to encounter the exception - * again. + * The API doesn't provide the instruction length that software + * exceptions and INT1 need, so don't report them. As long as the + * guest RIP isn't advanced, we should expect to encounter the + * exception again. */ - if (!kvm_exception_is_soft(ex->vector)) { + if (!kvm_exception_is_soft(ex->vector) && + ex->event_type != EVENT_TYPE_PRIV_SWEXC) { events->exception.injected = ex->injected; events->exception.pending = ex->pending; /* -- 2.43.0