KVM: nSVM/nVMX: fix nested virtualization treatment of nested exceptions
cover KVM: nSVM/nVMX: fix nested virtualization treatment of nested exceptions
clone of "kernel-starship-5.12.unstable"
Maxim Levitsky (4):
KVM: x86: pending exceptions must not be blocked by an injected event
KVM: x86: separate pending and injected exception
KVM: x86: correctly merge pending and injected exception
KVM: x86: remove tweaking of inject_page_fault
arch/x86/include/asm/kvm_host.h | 34 +++- arch/x86/kvm/svm/nested.c | 65 +++---- arch/x86/kvm/svm/svm.c | 8 +- arch/x86/kvm/vmx/nested.c | 107 +++++------ arch/x86/kvm/vmx/vmx.c | 14 +- arch/x86/kvm/x86.c | 302 ++++++++++++++++++-------------- arch/x86/kvm/x86.h | 6 +- 7 files changed, 283 insertions(+), 253 deletions(-)
Signature · 1 line
2.26.2
PATCH 1/4 KVM: x86: pending exceptions must not be blocked by an injected event
Injected interrupts/nmi should not block a pending exception, but rather be either lost if nested hypervisor doesn't intercept the pending exception (as in stock x86), or be delivered in exitintinfo/IDT_VECTORING_INFO field, as a part of a VMexit that corresponds to the pending exception.
The only reason for an exception to be blocked is when nested run is pending (and that can't really happen currently
but still worth checking for).
--- arch/x86/kvm/svm/nested.c | 8 +++++++- arch/x86/kvm/vmx/nested.c | 10 ++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/nested.c +7 -1
@@ -1062,7 +1062,13 @@ static int svm_check_nested_events(struct kvm_vcpu *vcpu) }if (vcpu->arch.exception.pending) { -if (block_nested_events) +/* +* Only a pending nested run can block a pending exception. +* Otherwise an injected NMI/interrupt should either be +* lost or delivered to the nested hypervisor in the EXITINTINFO +* vmcb field, while delivering the pending exception. +*/ +if (svm->nested.nested_run_pending)return -EBUSY ;if (!nested_exit_on_exception (svm))return 0 ;
arch/x86/kvm/vmx/nested.c +8 -2
@@ -3806,9 +3806,15 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)/* * Process any exceptions that are not debug traps before MTF. +* +* Note that only a pending nested run can block a pending exception. +* Otherwise an injected NMI/interrupt should either be +* lost or delivered to the nested hypervisor in the IDT_VECTORING_INFO, +* while delivering the pending exception. */ +if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap (vcpu)) { -if (block_nested_events) +if (vmx->nested.nested_run_pending)return -EBUSY ;if (!nested_vmx_check_exception (vcpu, &exit_qual)) goto no_vmexit;
@@ -3825,7 +3831,7 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu) }if (vcpu->arch.exception.pending) { -if (block_nested_events) +if (vmx->nested.nested_run_pending)return -EBUSY ;if (!nested_vmx_check_exception (vcpu, &exit_qual)) goto no_vmexit;
Signature · 1 line
2.26.2
Paolo Bonzini
maintainerRe: [PATCH 1/4] KVM: x86: pending exceptions must not be blocked by an injected event
Quoted 11 lines · Maxim Levitsky
Injected interrupts/nmi should not block a pending exception, but rather be either lost if nested hypervisor doesn't intercept the pending exception (as in stock x86), or be delivered in exitintinfo/IDT_VECTORING_INFO field, as a part of a VMexit that corresponds to the pending exception.
The only reason for an exception to be blocked is when nested run is pending (and that can't really happen currently
but still worth checking for).Signed-off-by Maxim Levitsky <[email protected]>
This patch would be an almost separate bugfix, right? I am going to queue this, but a confirmation would be helpful.
Paolo
Quoted 55 lines
--- arch/x86/kvm/svm/nested.c | 8 +++++++- arch/x86/kvm/vmx/nested.c | 10 ++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-)arch/x86/kvm/svm/nested.c +7
-1@@ -1062,7 +1062,13 @@ static int svm_check_nested_events(struct kvm_vcpu *vcpu) }if (vcpu->arch.exception.pending) { -if (block_nested_events) +/* +* Only a pending nested run can block a pending exception. +* Otherwise an injected NMI/interrupt should either be +* lost or delivered to the nested hypervisor in the EXITINTINFO +* vmcb field, while delivering the pending exception. +*/ +if (svm->nested.nested_run_pending)return -EBUSY ;if (!nested_exit_on_exception (svm))return 0 ;arch/x86/kvm/vmx/nested.c +8
-2@@ -3806,9 +3806,15 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)/* * Process any exceptions that are not debug traps before MTF. +* +* Note that only a pending nested run can block a pending exception. +* Otherwise an injected NMI/interrupt should either be +* lost or delivered to the nested hypervisor in the IDT_VECTORING_INFO, +* while delivering the pending exception. */ +if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap (vcpu)) { -if (block_nested_events) +if (vmx->nested.nested_run_pending)return -EBUSY ;if (!nested_vmx_check_exception (vcpu, &exit_qual)) goto no_vmexit;@@ -3825,7 +3831,7 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu) }if (vcpu->arch.exception.pending) { -if (block_nested_events) +if (vmx->nested.nested_run_pending)return -EBUSY ;if (!nested_vmx_check_exception (vcpu, &exit_qual)) goto no_vmexit;
Re: [PATCH 1/4] KVM: x86: pending exceptions must not be blocked by an injected event
Quoted 7 lines · Paolo Bonzini
Quoted 6 lines · Maxim Levitsky
Injected interrupts/nmi should not block a pending exception, but rather be either lost if nested hypervisor doesn't intercept the pending exception (as in stock x86), or be delivered in exitintinfo/IDT_VECTORING_INFO field, as a part of a VMexit that corresponds to the pending exception.
Quoted 8 lines
Quoted 5 lines
The only reason for an exception to be blocked is when nested run is pending (and that can't really happen currently
but still worth checking for).Signed-off-by Maxim Levitsky <[email protected]>This patch would be an almost separate bugfix, right? I am going to queue this, but a confirmation would be helpful.
Yes, this patch doesn't depend on anything else.
Thanks!
Best regards,
Maxim Levitsky
Quoted 58 lines
Paolo
Quoted 55 lines
--- arch/x86/kvm/svm/nested.c | 8 +++++++- arch/x86/kvm/vmx/nested.c | 10 ++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-)arch/x86/kvm/svm/nested.c +7
-1@@ -1062,7 +1062,13 @@ static int svm_check_nested_events(struct kvm_vcpu *vcpu) }if (vcpu->arch.exception.pending) { -if (block_nested_events) +/* +* Only a pending nested run can block a pending exception. +* Otherwise an injected NMI/interrupt should either be +* lost or delivered to the nested hypervisor in the EXITINTINFO +* vmcb field, while delivering the pending exception. +*/ +if (svm->nested.nested_run_pending)return -EBUSY ;if (!nested_exit_on_exception (svm))return 0 ;arch/x86/kvm/vmx/nested.c +8
-2@@ -3806,9 +3806,15 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)/* * Process any exceptions that are not debug traps before MTF. +* +* Note that only a pending nested run can block a pending exception. +* Otherwise an injected NMI/interrupt should either be +* lost or delivered to the nested hypervisor in the IDT_VECTORING_INFO, +* while delivering the pending exception. */ +if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap (vcpu)) { -if (block_nested_events) +if (vmx->nested.nested_run_pending)return -EBUSY ;if (!nested_vmx_check_exception (vcpu, &exit_qual)) goto no_vmexit;@@ -3825,7 +3831,7 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu) }if (vcpu->arch.exception.pending) { -if (block_nested_events) +if (vmx->nested.nested_run_pending)return -EBUSY ;if (!nested_vmx_check_exception (vcpu, &exit_qual)) goto no_vmexit;
PATCH 2/4 KVM: x86: separate pending and injected exception
In reply to Maxim LevitskyUse 'pending_exception' and 'injected_exception' fields to store the pending and the injected exceptions.
After this patch still only one is active, but in the next patch both could co-exist in some cases.
--- arch/x86/include/asm/kvm_host.h | 25 ++++-- arch/x86/kvm/svm/nested.c | 26 +++--- arch/x86/kvm/svm/svm.c | 6 +- arch/x86/kvm/vmx/nested.c | 36 ++++---- arch/x86/kvm/vmx/vmx.c | 12 +-- arch/x86/kvm/x86.c | 145 ++++++++++++++++++-------------- arch/x86/kvm/x86.h | 6 +- 7 files changed, 143 insertions(+), 113 deletions(-)
arch/x86/include/asm/kvm_host.h +16 -9
@@ -547,6 +547,14 @@ struct kvm_vcpu_xen {u64 runstate_times[4 ]; }; +struct kvm_queued_exception { +bool valid; +u8 nr; +bool has_error_code; +u32 error_code; +}; + +struct kvm_vcpu_arch {/* * rip and regs accesses must go through
@@ -645,16 +653,15 @@ struct kvm_vcpu_arch {u8 event_exit_inst_len; -struct kvm_queued_exception { -bool pending; -bool injected; -bool has_error_code; -u8 nr; -u32 error_code; -unsigned long payload; -bool has_payload; +struct kvm_queued_exception pending_exception; + +struct kvm_exception_payload { +bool valid; +unsigned long value;u8 nested_apf; - } exception; + } exception_payload; + +struct kvm_queued_exception injected_exception;struct kvm_queued_interrupt {bool injected;
arch/x86/kvm/svm/nested.c +13 -13
@@ -349,14 +349,14 @@ static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,u32 exit_int_info =0 ;unsigned int nr; -if (vcpu->arch.exception.injected) { - nr = vcpu->arch.exception.nr; +if (vcpu->arch.injected_exception.valid) { + nr = vcpu->arch.injected_exception.nr; exit_int_info = nr |SVM_EVTINJ_VALID |SVM_EVTINJ_TYPE_EXEPT ; -if (vcpu->arch.exception.has_error_code) { +if (vcpu->arch.injected_exception.has_error_code) { exit_int_info |=SVM_EVTINJ_VALID_ERR ; vmcb12->control.exit_int_info_err = - vcpu->arch.exception.error_code; + vcpu->arch.injected_exception.error_code; } }else if (vcpu->arch.nmi_injected) {
@@ -1000,30 +1000,30 @@ int nested_svm_check_permissions(struct kvm_vcpu *vcpu)static bool nested_exit_on_exception (struct vcpu_svm *svm) { -unsigned int nr = svm->vcpu.arch.exception.nr; +unsigned int nr = svm->vcpu.arch.pending_exception.nr;return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION ] &BIT (nr)); }static void nested_svm_inject_exception_vmexit (struct vcpu_svm *svm) { -unsigned int nr = svm->vcpu.arch.exception.nr; +unsigned int nr = svm->vcpu.arch.pending_exception.nr; svm->vmcb->control.exit_code =SVM_EXIT_EXCP_BASE + nr; svm->vmcb->control.exit_code_hi =0 ; -if (svm->vcpu.arch.exception.has_error_code) - svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code; +if (svm->vcpu.arch.pending_exception.has_error_code) + svm->vmcb->control.exit_info_1 = svm->vcpu.arch.pending_exception.error_code;/* * EXITINFO2 is undefined for all exception intercepts other * than #PF. */ if (nr ==PF_VECTOR ) { -if (svm->vcpu.arch.exception.nested_apf) +if (svm->vcpu.arch.exception_payload.nested_apf) svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token; -else if (svm->vcpu.arch.exception.has_payload) - svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload; +else if (svm->vcpu.arch.exception_payload.valid) + svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception_payload.value;else svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2; }else if (nr ==DB_VECTOR ) {
@@ -1034,7 +1034,7 @@ static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)kvm_update_dr7 (&svm->vcpu); } }else -WARN_ON (svm->vcpu.arch.exception.has_payload); +WARN_ON (svm->vcpu.arch.exception_payload.valid);nested_svm_vmexit (svm); }
@@ -1061,7 +1061,7 @@ static int svm_check_nested_events(struct kvm_vcpu *vcpu)return 0 ; } -if (vcpu->arch.exception.pending) { +if (vcpu->arch.pending_exception.valid) {/* * Only a pending nested run can block a pending exception. * Otherwise an injected NMI/interrupt should either be
arch/x86/kvm/svm/svm.c +3 -3
@@ -359,9 +359,9 @@ static int skip_emulated_instruction(struct kvm_vcpu *vcpu)static void svm_queue_exception (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); -unsigned nr = vcpu->arch.exception.nr; -bool has_error_code = vcpu->arch.exception.has_error_code; -u32 error_code = vcpu->arch.exception.error_code; +unsigned int nr = vcpu->arch.injected_exception.nr; +bool has_error_code = vcpu->arch.injected_exception.has_error_code; +u32 error_code = vcpu->arch.injected_exception.error_code;kvm_deliver_exception_payload (vcpu);
arch/x86/kvm/vmx/nested.c +18 -18
@@ -388,17 +388,17 @@ static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,static int nested_vmx_check_exception (struct kvm_vcpu *vcpu,unsigned long *exit_qual) {struct vmcs12 *vmcs12 =get_vmcs12 (vcpu); -unsigned int nr = vcpu->arch.exception.nr; -bool has_payload = vcpu->arch.exception.has_payload; -unsigned long payload = vcpu->arch.exception.payload; +unsigned int nr = vcpu->arch.pending_exception.nr; +bool has_payload = vcpu->arch.exception_payload.valid; +unsigned long payload = vcpu->arch.exception_payload.value;if (nr ==PF_VECTOR ) { -if (vcpu->arch.exception.nested_apf) { +if (vcpu->arch.exception_payload.nested_apf) { *exit_qual = vcpu->arch.apf.nested_apf_token;return 1 ; }if (nested_vmx_is_page_fault_vmexit (vmcs12, - vcpu->arch.exception.error_code)) { + vcpu->arch.pending_exception.error_code)) { *exit_qual = has_payload ? payload : vcpu->arch.cr2;return 1 ; }
@@ -3617,8 +3617,8 @@ static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,u32 idt_vectoring;unsigned int nr; -if (vcpu->arch.exception.injected) { - nr = vcpu->arch.exception.nr; +if (vcpu->arch.injected_exception.valid) { + nr = vcpu->arch.injected_exception.nr; idt_vectoring = nr |VECTORING_INFO_VALID_MASK ;if (kvm_exception_is_soft (nr)) {
@@ -3628,10 +3628,10 @@ static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, }else idt_vectoring |=INTR_TYPE_HARD_EXCEPTION ; -if (vcpu->arch.exception.has_error_code) { +if (vcpu->arch.injected_exception.has_error_code) { idt_vectoring |=VECTORING_INFO_DELIVER_CODE_MASK ; vmcs12->idt_vectoring_error_code = - vcpu->arch.exception.error_code; + vcpu->arch.injected_exception.error_code; } vmcs12->idt_vectoring_info_field = idt_vectoring;
@@ -3712,11 +3712,11 @@ static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, unsigned long exit_qual) {struct vmcs12 *vmcs12 =get_vmcs12 (vcpu); -unsigned int nr = vcpu->arch.exception.nr; +unsigned int nr = vcpu->arch.pending_exception.nr;u32 intr_info = nr |INTR_INFO_VALID_MASK ; -if (vcpu->arch.exception.has_error_code) { - vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; +if (vcpu->arch.pending_exception.has_error_code) { + vmcs12->vm_exit_intr_error_code = vcpu->arch.pending_exception.error_code; intr_info |=INTR_INFO_DELIVER_CODE_MASK ; }
@@ -3740,9 +3740,9 @@ static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,*/ static inline bool vmx_pending_dbg_trap (struct kvm_vcpu *vcpu) { -return vcpu->arch.exception.pending && - vcpu->arch.exception.nr ==DB_VECTOR && - vcpu->arch.exception.payload; +return vcpu->arch.pending_exception.valid && + vcpu->arch.pending_exception.nr ==DB_VECTOR && + vcpu->arch.exception_payload.value; }/*
@@ -3756,7 +3756,7 @@ static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu) {if (vmx_pending_dbg_trap (vcpu))vmcs_writel (GUEST_PENDING_DBG_EXCEPTIONS , - vcpu->arch.exception.payload); + vcpu->arch.exception_payload.value); }static bool nested_vmx_preemption_timer_pending (struct kvm_vcpu *vcpu)
@@ -3813,7 +3813,7 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)* while delivering the pending exception. */ -if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap (vcpu)) { +if (vcpu->arch.pending_exception.valid && !vmx_pending_dbg_trap (vcpu)) {if (vmx->nested.nested_run_pending)return -EBUSY ;if (!nested_vmx_check_exception (vcpu, &exit_qual))
@@ -3830,7 +3830,7 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)return 0 ; } -if (vcpu->arch.exception.pending) { +if (vcpu->arch.pending_exception.valid) {if (vmx->nested.nested_run_pending)return -EBUSY ;if (!nested_vmx_check_exception (vcpu, &exit_qual))
arch/x86/kvm/vmx/vmx.c +6 -6
@@ -1648,8 +1648,8 @@ static void vmx_update_emulated_instruction(struct kvm_vcpu *vcpu)* vmx_check_nested_events(). */ if (nested_cpu_has_mtf (vmcs12) && - (!vcpu->arch.exception.pending || - vcpu->arch.exception.nr ==DB_VECTOR )) + (!vcpu->arch.pending_exception.valid || + vcpu->arch.pending_exception.nr ==DB_VECTOR )) vmx->nested.mtf_pending = true;else vmx->nested.mtf_pending = false;
@@ -1677,9 +1677,9 @@ static void vmx_clear_hlt(struct kvm_vcpu *vcpu)static void vmx_queue_exception (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu); -unsigned nr = vcpu->arch.exception.nr; -bool has_error_code = vcpu->arch.exception.has_error_code; -u32 error_code = vcpu->arch.exception.error_code; +unsigned int nr = vcpu->arch.injected_exception.nr; +bool has_error_code = vcpu->arch.injected_exception.has_error_code; +u32 error_code = vcpu->arch.injected_exception.error_code;u32 intr_info = nr |INTR_INFO_VALID_MASK ;kvm_deliver_exception_payload (vcpu);
@@ -5397,7 +5397,7 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)return 0 ;if (vmx->emulation_required && !vmx->rmode.vm86_active && - vcpu->arch.exception.pending) { + vcpu->arch.pending_exception.valid) { vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; vcpu->run->internal.suberror =KVM_INTERNAL_ERROR_EMULATION ;
arch/x86/kvm/x86.c +84 -61
309 lines not on this page · raw message
arch/x86/kvm/x86.h +3 -3
@@ -60,8 +60,8 @@ int kvm_check_nested_events(struct kvm_vcpu *vcpu);static inline void kvm_clear_exception_queue (struct kvm_vcpu *vcpu) { - vcpu->arch.exception.pending = false; - vcpu->arch.exception.injected = false; + vcpu->arch.pending_exception.valid = false; + vcpu->arch.injected_exception.valid = false; } static inline void kvm_queue_interrupt(struct kvm_vcpu *vcpu, u8 vector,
@@ -79,7 +79,7 @@ static inline void kvm_clear_interrupt_queue(struct kvm_vcpu *vcpu)static inline bool kvm_event_needs_reinjection (struct kvm_vcpu *vcpu) { -return vcpu->arch.exception.injected || vcpu->arch.interrupt.injected || +return vcpu->arch.injected_exception.valid || vcpu->arch.interrupt.injected || vcpu->arch.nmi_injected; }
Signature · 1 line
2.26.2
Sean Christopherson
maintainerRe: [PATCH 2/4] KVM: x86: separate pending and injected exception
Quoted 5 lines · Maxim Levitsky
Use '
pending_exception' and 'injected_exception' fields to store the pending and the injected exceptions.After this patch still only one is active, but in the next patch both could co-exist in some cases.
Please explain _why_.
Quoted 16 lines
Signed-off-by Maxim Levitsky <[email protected]>--- arch/x86/include/asm/kvm_host.h | 25 ++++-- arch/x86/kvm/svm/nested.c | 26 +++--- arch/x86/kvm/svm/svm.c | 6 +- arch/x86/kvm/vmx/nested.c | 36 ++++---- arch/x86/kvm/vmx/vmx.c | 12 +-- arch/x86/kvm/x86.c | 145 ++++++++++++++++++-------------- arch/x86/kvm/x86.h | 6 +- 7 files changed, 143 insertions(+), 113 deletions(-)arch/x86/include/asm/kvm_host.h +0
-0
arch/x86/include/asm/kvm_host.h struct kvm_vcpu_xen { · line 552
Quoted 6 lines
arch/x86/include/asm/kvm_host.h +3
-0@@ -547,6 +547,14 @@ struct kvm_vcpu_xen {u64 runstate_times[4 ]; }; +struct kvm_queued_exception { +bool valid; +u8 nr;
If we're refactoring all this code anyways, maybe change "nr" to something a bit more descriptive? E.g. vector.
Quoted 18 lines
arch/x86/include/asm/kvm_host.h +5
-6+bool has_error_code; +u32 error_code; +}; + +struct kvm_vcpu_arch {/* * rip and regs accesses must go through @@ -645,16 +653,15 @@ struct kvm_vcpu_arch {u8 event_exit_inst_len; -struct kvm_queued_exception { -bool pending; -bool injected; -bool has_error_code; -u8 nr; -u32 error_code;
arch/x86/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 662
Quoted 10 lines
arch/x86/include/asm/kvm_host.h +6
-3-unsigned long payload; -bool has_payload; +struct kvm_queued_exception pending_exception; + +struct kvm_exception_payload { +bool valid; +unsigned long value;u8 nested_apf; - } exception; + } exception_payload;
Hmm, even if it's dead code at this time, I think the exception payload should be part of 'struct kvm_queued_exception'. The payload is very much tied to a single exception.
arch/x86/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 664
Quoted 2 lines
arch/x86/include/asm/kvm_host.h +2
-0+ +struct kvm_queued_exception injected_exception;
Any objection to keeping the current syntax, arch.exception.{pending,injected}? Maybe it's fear of change, but I like the current style, I think because the relevant info is condensed at the end, e.g. I can ignore "vcpu->arch.exception" and look at "pending.vector" or whatever. E.g.
struct {
struct kvm_queued_exception pending;
struct kvm_queued_exception injected;
} exception;Quoted 3 lines
arch/x86/include/asm/kvm_host.h +0
-0struct kvm_queued_interrupt {bool injected;
Paolo Bonzini
maintainerRe: [PATCH 2/4] KVM: x86: separate pending and injected exception
Quoted 7 lines · Sean Christopherson
arch/x86/include/asm/kvm_host.h struct kvm_vcpu_xen { · line 552
Quoted 4 lines
arch/x86/include/asm/kvm_host.h +3
-0+struct kvm_queued_exception { +bool valid; +u8 nr;If we're refactoring all this code anyways, maybe change "nr" to something a bit more descriptive? E.g. vector.
"nr" is part of the userspace structure, so consistency is an advantage too.
Quoted 10 lines
arch/x86/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 662
Quoted 6 lines
arch/x86/include/asm/kvm_host.h +4
-1+struct kvm_exception_payload { +bool valid; +unsigned long value;u8 nested_apf; - } exception; + } exception_payload;Hmm, even if it's dead code at this time, I think the exception payload should be part of 'struct
kvm_queued_exception'. The payload is very much tied to a single exception.
Agreed, when handling injected exceptions you can WARN that there is no payload.
Paolo
Sean Christopherson
maintainerRe: [PATCH 2/4] KVM: x86: separate pending and injected exception
Quoted 10 lines · Paolo Bonzini
Quoted 7 lines · Sean Christopherson
arch/x86/include/asm/kvm_host.h struct kvm_vcpu_xen { · line 552
Quoted 4 lines
arch/x86/include/asm/kvm_host.h +3
-0+struct kvm_queued_exception { +bool valid; +u8 nr;If we're refactoring all this code anyways, maybe change "nr" to something a bit more descriptive? E.g. vector.
"nr" is part of the userspace structure, so consistency is an advantage too.
Foiled at every turn. Keeping "nr" probably does make sense.
PATCH 3/4 KVM: x86: correctly merge pending and injected exception
In reply to Maxim LevitskyAllow the pending and the injected exceptions to co-exist when they are raised.
Add 'kvm_deliver_pending_exception' function which 'merges' the pending and injected exception or delivers a VM exit with both for a case when the L1 intercepts the pending exception.
The later is done by vendor code using new nested callback 'deliver_exception_as_vmexit'
The kvm_deliver_pending_exception is called after each VM exit, and prior to VM entry which ensures that during userspace VM exits, only injected exception can be in a raised state.
--- arch/x86/include/asm/kvm_host.h | 9 ++ arch/x86/kvm/svm/nested.c | 27 ++-- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/nested.c | 58 ++++---- arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/kvm/x86.c | 233 ++++++++++++++++++-------------- 6 files changed, 181 insertions(+), 150 deletions(-)
arch/x86/include/asm/kvm_host.h +9 -0
@@ -1346,6 +1346,15 @@ struct kvm_x86_ops {struct kvm_x86_nested_ops {int (*check_events)(struct kvm_vcpu *vcpu); + +/* +* Deliver a pending exception as a VM exit if the L1 intercepts it. +* Returns -EBUSY if L1 does intercept the exception but, +* it is not possible to deliver it right now. +* (for example when nested run is pending) +*/ +int (*deliver_exception_as_vmexit)(struct kvm_vcpu *vcpu); +bool (*hv_timer_pending)(struct kvm_vcpu *vcpu);void (*triple_fault)(struct kvm_vcpu *vcpu); int (*get_state)(struct kvm_vcpu *vcpu,
arch/x86/kvm/svm/nested.c +12 -15
@@ -1061,21 +1061,6 @@ static int svm_check_nested_events(struct kvm_vcpu *vcpu)return 0 ; } -if (vcpu->arch.pending_exception.valid) { -/* -* Only a pending nested run can block a pending exception. -* Otherwise an injected NMI/interrupt should either be -* lost or delivered to the nested hypervisor in the EXITINTINFO -* vmcb field, while delivering the pending exception. -*/ -if (svm->nested.nested_run_pending) -return -EBUSY ; -if (!nested_exit_on_exception (svm)) -return 0 ; -nested_svm_inject_exception_vmexit (svm); -return 0 ; - } -if (vcpu->arch.smi_pending && !svm_smi_blocked (vcpu)) {if (block_nested_events)return -EBUSY ;
@@ -1107,6 +1092,17 @@ static int svm_check_nested_events(struct kvm_vcpu *vcpu)return 0 ; } +int svm_deliver_nested_exception_as_vmexit (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); + +if (svm->nested.nested_run_pending) +return -EBUSY ; +if (nested_exit_on_exception (svm)) +nested_svm_inject_exception_vmexit (svm); +return 0 ; +} +int nested_svm_exit_special (struct vcpu_svm *svm) {u32 exit_code = svm->vmcb->control.exit_code;
@@ -1321,6 +1317,7 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,struct kvm_x86_nested_ops svm_nested_ops = { .check_events = svm_check_nested_events, .triple_fault = nested_svm_triple_fault, + .deliver_exception_as_vmexit = svm_deliver_nested_exception_as_vmexit, .get_nested_state_pages = svm_get_nested_state_pages, .get_state = svm_get_nested_state, .set_state = svm_set_nested_state,
arch/x86/kvm/svm/svm.c +1 -1
@@ -363,7 +363,7 @@ static void svm_queue_exception(struct kvm_vcpu *vcpu)bool has_error_code = vcpu->arch.injected_exception.has_error_code;u32 error_code = vcpu->arch.injected_exception.error_code; -kvm_deliver_exception_payload (vcpu); +WARN_ON_ONCE (vcpu->arch.pending_exception.valid);if (nr ==BP_VECTOR && !nrips) {unsigned long rip, old_rip =kvm_rip_read (vcpu);
arch/x86/kvm/vmx/nested.c +30 -28
90 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -1682,7 +1682,7 @@ static void vmx_queue_exception(struct kvm_vcpu *vcpu)u32 error_code = vcpu->arch.injected_exception.error_code;u32 intr_info = nr |INTR_INFO_VALID_MASK ; -kvm_deliver_exception_payload (vcpu); +WARN_ON_ONCE (vcpu->arch.pending_exception.valid);if (has_error_code) {vmcs_write32 (VM_ENTRY_EXCEPTION_ERROR_CODE , error_code);
arch/x86/kvm/x86.c +128 -105
301 lines not on this page · raw message
Signature · 1 line
2.26.2
Paolo Bonzini
maintainerRe: [PATCH 3/4] KVM: x86: correctly merge pending and injected exception
Quoted 7 lines · Maxim Levitsky
arch/x86/kvm/x86.c +7
-0+static int kvm_do_deliver_pending_exception (struct kvm_vcpu *vcpu) +{ +int class1, class2, ret; + +/* try to deliver current pending exception as VM exit */ +if (is_guest_mode (vcpu)) { + ret = kvm_x86_ops.nested_ops->deliver_exception_as_vmexit (vcpu);
arch/x86/kvm/x86.c static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr, · line 604
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+if (ret || !vcpu->arch.pending_exception.valid) +return ret; + } + +/* No injected exception, so just deliver the payload and inject it */ +if (!vcpu->arch.injected_exception.valid) { +trace_kvm_inj_exception (vcpu->arch.pending_exception.nr, + vcpu->arch.pending_exception.has_error_code, + vcpu->arch.pending_exception.error_code); +queue:
If you move the queue label to the top of the function, you can "goto queue" for #DF as well and you don't need to call kvm_do_deliver_pending_exception again. In fact you can merge this function and kvm_deliver_pending_exception completely:
static int kvm_deliver_pending_exception_as_vmexit(struct kvm_vcpu *vcpu)
{
WARN_ON(!vcpu->arch.pending_exception.valid);
if (is_guest_mode(vcpu))
return kvm_x86_ops.nested_ops->deliver_exception_as_vmexit(vcpu);
else
return 0;
}static int kvm_merge_injected_exception(struct kvm_vcpu *vcpu)
{
/*
* First check if the pending exception takes precedence
* over the injected one, which will be reported in the
* vmexit info.
*/
ret = kvm_deliver_pending_exception_as_vmexit(vcpu);
if (ret || !vcpu->arch.pending_exception.valid)
return ret;
if (vcpu->arch.injected_exception.nr == DF_VECTOR) {
...
return 0;
}
...
if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
|| (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
...
}
vcpu->arch.injected_exception.valid = false;
}static int kvm_deliver_pending_exception(struct kvm_vcpu *vcpu)
{
if (!vcpu->arch.pending_exception.valid)
return 0;
if (vcpu->arch.injected_exception.valid)
kvm_merge_injected_exception(vcpu);
ret = kvm_deliver_pending_exception_as_vmexit(vcpu));
if (ret || !vcpu->arch.pending_exception.valid)
return ret;
trace_kvm_inj_exception(vcpu->arch.pending_exception.nr,
vcpu->arch.pending_exception.has_error_code,
vcpu->arch.pending_exception.error_code);
...
}Note that if the pending exception is a page fault, its payload must be delivered to CR2 before converting it to a double fault.
Going forward to vmx.c:
arch/x86/kvm/vmx/nested.c static int vmx_check_nested_events(struct kvm_vcpu *vcpu) · line 3810
Quoted 6 lines
arch/x86/kvm/vmx/nested.c +1
-0if (mtf_pending) {if (block_nested_events)return -EBUSY ; +nested_vmx_update_pending_dbg (vcpu);
Should this instead "WARN_ON(vmx_pending_dbg_trap(vcpu));" since the pending-#DB-plus-MTF combination is handled in vmx_deliver_exception_as_vmexit?...
Quoted 4 lines
arch/x86/kvm/vmx/nested.c +3
-0+ +if (vmx->nested.mtf_pending &&vmx_pending_dbg_trap (vcpu)) { +/*
arch/x86/kvm/vmx/nested.c static int vmx_check_nested_events(struct kvm_vcpu *vcpu) · line 3879
Quoted 10 lines
arch/x86/kvm/vmx/nested.c +10
-0+* A pending monitor trap takes precedence over pending +* debug exception which is 'stashed' into +* 'GUEST_PENDING_DBG_EXCEPTIONS' +*/ + +nested_vmx_update_pending_dbg (vcpu); + vmx->nested.mtf_pending = false; +nested_vmx_vmexit (vcpu,EXIT_REASON_MONITOR_TRAP_FLAG ,0 ,0 ); +return 0 ; + }
... though this is quite ugly, even more so if you add the case of an INIT with a pending #DB. The problem is that INIT and MTF have higher priority than debug exceptions.
The good thing is that an INIT or MTF plus #DB cannot happen with nested_run_pending == 1, so it will always be inject right away.
There is precedent with KVM_GET_* modifying the CPU state; in particular, KVM_GET_MPSTATE can modify CS and RIP and even cause a vmexit via kvm_apic_accept_events. And in fact, because kvm_apic_accept_events calls kvm_check_nested_events, calling it from KVM_GET_VCPU_EVENTS would fix the problem: the injected exception would go into the IDT-vectored exit field, while the pending exception would go into GUEST_PENDING_DBG_EXCEPTION and disappear.
However, you cannot do kvm_apic_accept_events twice because there would be a race with INIT: a #DB exception could be first stored by KVM_GET_VCPU_EVENTS, then disappear when kvm_apic_accept_events KVM_GET_MPSTATE is called.
Fortunately, the correct order for KVM_GET_* events is first KVM_GET_VCPU_EVENTS and then KVM_GET_MPSTATE. So perhaps instead of calling kvm_deliver_pending_exception on vmexit, KVM_GET_VCPU_EVENTS could call kvm_apic_accept_events (and thus kvm_check_nested_events) instead of KVM_GET_MPSTATE. In addition:
nested_ops.check_events would be split in two, with high-priority events (triple fault, now in kvm_check_nested_events; INIT; MTF) remaining in the first and interrupts in the second, tentatively named check_interrupts).
I'll take a look at cleaning up the current mess we have around kvm_apic_accept_events, where most of the calls do not have to handle guest mode at all.
Thanks for this work and for showing that it's possible to fix the underlying mess with exception vmexit. It may be a bit more complicated than this, but it's a great start.
Paolo
Sean Christopherson
maintainerRe: [PATCH 3/4] KVM: x86: correctly merge pending and injected exception
Quoted 55 lines · Paolo Bonzini
Quoted 7 lines · Maxim Levitsky
arch/x86/kvm/x86.c +7
-0+static int kvm_do_deliver_pending_exception (struct kvm_vcpu *vcpu) +{ +int class1, class2, ret; + +/* try to deliver current pending exception as VM exit */ +if (is_guest_mode (vcpu)) { + ret = kvm_x86_ops.nested_ops->deliver_exception_as_vmexit (vcpu);arch/x86/kvm/x86.c static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr, · line 604
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+if (ret || !vcpu->arch.pending_exception.valid) +return ret; + } + +/* No injected exception, so just deliver the payload and inject it */ +if (!vcpu->arch.injected_exception.valid) { +trace_kvm_inj_exception (vcpu->arch.pending_exception.nr, + vcpu->arch.pending_exception.has_error_code, + vcpu->arch.pending_exception.error_code); +queue:If you move the queue label to the top of the function, you can "goto queue" for #DF as well and you don't need to call
kvm_do_deliver_pending_exceptionagain. In fact you can merge this function andkvm_deliver_pending_exceptioncompletely:static int
kvm_deliver_pending_exception_as_vmexit(struct kvm_vcpu *vcpu){ WARN_ON(!vcpu->arch.pending_exception.valid); if (is_guest_mode(vcpu)) return kvm_x86_ops.nested_ops->deliver_exception_as_vmexit(vcpu); else return 0; }static int
kvm_merge_injected_exception(struct kvm_vcpu *vcpu){ /* * First check if the pending exception takes precedence * over the injected one, which will be reported in the * vmexit info. */ ret = kvm_deliver_pending_exception_as_vmexit(vcpu); if (ret || !vcpu->arch.pending_exception.valid) return ret; if (vcpu->arch.injected_exception.nr == DF_VECTOR) { ... return 0; } ... if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY) || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) { ... } vcpu->arch.injected_exception.valid = false; }static int
kvm_deliver_pending_exception(struct kvm_vcpu *vcpu)
Quoted 9 lines
{
if (!vcpu->arch.pending_exception.valid)
return 0;
if (vcpu->arch.injected_exception.valid)
kvm_merge_injected_exception(vcpu);
ret = kvm_deliver_pending_exception_as_vmexit(vcpu));
if (ret || !vcpu->arch.pending_exception.valid)I really don't like querying arch.pending_exception.valid to see if the exception was morphed to a VM-Exit. I also find kvm_deliver_pending_exception_as_vmexit() to be misleading; to me, that reads as being a command, i.e. "deliver this pending exception as a VM-Exit".
It' also be nice to make the helpers closer to pure functions, i.e. pass the exception as a param instead of pulling it from vcpu->arch.
Now that we have static_call, the number of calls into vendor code isn't a huge issue. Moving nested_run_pending to arch code would help, too. What about doing something like:
static bool kvm_l1_wants_exception_vmexit(struct kvm_vcpu *vcpu, u8 vector)
{
return is_guest_mode(vcpu) && kvm_x86_l1_wants_exception(vcpu, vector);
}... if (!kvm_x86_exception_allowed(vcpu)) return -EBUSY; if (kvm_l1_wants_exception_vmexit(vcpu, vcpu->arch...)) return kvm_x86_deliver_exception_as_vmexit(...);
Quoted 8 lines
return ret; trace_kvm_inj_exception(vcpu->arch.pending_exception.nr, vcpu->arch.pending_exception.has_error_code, vcpu->arch.pending_exception.error_code); ... }
PATCH 4/4 KVM: x86: remove tweaking of inject_page_fault
In reply to Maxim LevitskyThis is no longer needed since page faults can now be injected as regular exceptions in all the cases.
--- arch/x86/kvm/svm/nested.c | 20 -------------------- arch/x86/kvm/vmx/nested.c | 23 ----------------------- 2 files changed, 43 deletions(-)
arch/x86/kvm/svm/nested.c +0 -20
@@ -53,23 +53,6 @@ static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,nested_svm_vmexit (svm); } -static void svm_inject_page_fault_nested (struct kvm_vcpu *vcpu,struct x86_exception *fault) -{ -struct vcpu_svm *svm =to_svm (vcpu); -WARN_ON (!is_guest_mode (vcpu)); - -if (vmcb_is_intercept (&svm->nested.ctl,INTERCEPT_EXCEPTION_OFFSET +PF_VECTOR ) && - !svm->nested.nested_run_pending) { - svm->vmcb->control.exit_code =SVM_EXIT_EXCP_BASE +PF_VECTOR ; - svm->vmcb->control.exit_code_hi =0 ; - svm->vmcb->control.exit_info_1 = fault->error_code; - svm->vmcb->control.exit_info_2 = fault->address; -nested_svm_vmexit (svm); - }else { -kvm_inject_page_fault (vcpu, fault); - } -} -static u64 nested_svm_get_tdp_pdptr (struct kvm_vcpu *vcpu,int index) {struct vcpu_svm *svm =to_svm (vcpu);
@@ -575,9 +558,6 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,if (ret)return ret; -if (!npt_enabled) - vcpu->arch.mmu->inject_page_fault = svm_inject_page_fault_nested; -svm_set_gif (svm, true);return 0 ;
arch/x86/kvm/vmx/nested.c +0 -23
@@ -418,26 +418,6 @@ static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exitreturn 0 ; } - -static void vmx_inject_page_fault_nested (struct kvm_vcpu *vcpu, -struct x86_exception *fault) -{ -struct vmcs12 *vmcs12 =get_vmcs12 (vcpu); - -WARN_ON (!is_guest_mode (vcpu)); - -if (nested_vmx_is_page_fault_vmexit (vmcs12, fault->error_code) && - !to_vmx (vcpu)->nested.nested_run_pending) { - vmcs12->vm_exit_intr_error_code = fault->error_code; -nested_vmx_vmexit (vcpu,EXIT_REASON_EXCEPTION_NMI , -PF_VECTOR |INTR_TYPE_HARD_EXCEPTION | -INTR_INFO_DELIVER_CODE_MASK |INTR_INFO_VALID_MASK , - fault->address); - }else { -kvm_inject_page_fault (vcpu, fault); - } -} -static int nested_vmx_check_io_bitmap_controls (struct kvm_vcpu *vcpu,struct vmcs12 *vmcs12) {
@@ -2588,9 +2568,6 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,vmcs_write64 (GUEST_PDPTR3 , vmcs12->guest_pdptr3); } -if (!enable_ept) - vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; -if ((vmcs12->vm_entry_controls &VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL ) &&WARN_ON_ONCE (kvm_set_msr (vcpu,MSR_CORE_PERF_GLOBAL_CTRL , vmcs12->guest_ia32_perf_global_ctrl)))
Signature · 1 line
2.26.2