KVM: VMX: Use basic exit reason for cheking and indexing
cover KVM: VMX: Use basic exit reason for cheking and indexing
Current KVM directly uses the whole 32-bit EXIT REASON when
1) checking: if (vmx->exit_reason == EXIT_REASON_*)
2) indexing: kvm_vmx_exit_handlers[exit_reason]
However, only the low 16-bit of EXIT REASON serves as basic Exit Reason. Fix it by using the 16-bit basic exit reason.
BTW, I'm not sure if it's necessary to split nested case into a seperate patch.
Xiaoyao Li (2):
kvm: vmx: Use basic exit reason to check if it's the specific VM EXIT
kvm: nvmx: Use basic(exit_reason) when checking specific EXIT_REASON
arch/x86/kvm/vmx/nested.c | 6 +++--- arch/x86/kvm/vmx/nested.h | 2 +- arch/x86/kvm/vmx/vmx.c | 44 ++++++++++++++++++++------------------- arch/x86/kvm/vmx/vmx.h | 2 ++ 4 files changed, 29 insertions(+), 25 deletions(-)
Signature · 1 line
2.23.0
PATCH 1/2 kvm: vmx: Use basic exit reason to check if it's the specific VM EXIT
Current kvm uses the 32-bit exit reason to check if it's any specific VM EXIT, however only the low 16-bit of VM EXIT REASON acts as the basic exit reason.
Introduce Macro basic(exit_reaso) to help retrieve the basic exit reason from VM EXIT REASON, and use the basic exit reason for checking and indexing the exit hanlder.
--- arch/x86/kvm/vmx/vmx.c | 44 ++++++++++++++++++++++-------------------- arch/x86/kvm/vmx/vmx.h | 2 ++ 2 files changed, 25 insertions(+), 21 deletions(-)
arch/x86/kvm/vmx/vmx.c +23 -21
@@ -1584,7 +1584,7 @@ static int skip_emulated_instruction(struct kvm_vcpu *vcpu)* i.e. we end up advancing IP with some random value. */ if (!static_cpu_has (X86_FEATURE_HYPERVISOR ) || -to_vmx (vcpu)->exit_reason !=EXIT_REASON_EPT_MISCONFIG ) { +basic (to_vmx (vcpu)->exit_reason) !=EXIT_REASON_EPT_MISCONFIG ) { rip =kvm_rip_read (vcpu); rip +=vmcs_read32 (VM_EXIT_INSTRUCTION_LEN );kvm_rip_write (vcpu, rip);
@@ -5797,6 +5797,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu, {struct vcpu_vmx *vmx =to_vmx (vcpu);u32 exit_reason = vmx->exit_reason; +u16 basic_exit_reason =basic (exit_reason);u32 vectoring_info = vmx->idt_vectoring_info;trace_kvm_exit (exit_reason, vcpu,KVM_ISA_VMX );
@@ -5842,17 +5843,17 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu,* will cause infinite loop. */ if ((vectoring_info &VECTORING_INFO_VALID_MASK ) && - (exit_reason !=EXIT_REASON_EXCEPTION_NMI && - exit_reason !=EXIT_REASON_EPT_VIOLATION && - exit_reason !=EXIT_REASON_PML_FULL && - exit_reason !=EXIT_REASON_TASK_SWITCH )) { + (basic_exit_reason !=EXIT_REASON_EXCEPTION_NMI && + basic_exit_reason !=EXIT_REASON_EPT_VIOLATION && + basic_exit_reason !=EXIT_REASON_PML_FULL && + basic_exit_reason !=EXIT_REASON_TASK_SWITCH )) { vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; vcpu->run->internal.suberror =KVM_INTERNAL_ERROR_DELIVERY_EV ; vcpu->run->internal.ndata =3 ; vcpu->run->internal.data[0 ] = vectoring_info; vcpu->run->internal.data[1 ] = exit_reason; vcpu->run->internal.data[2 ] = vcpu->arch.exit_qualification; -if (exit_reason ==EXIT_REASON_EPT_MISCONFIG ) { +if (basic_exit_reason ==EXIT_REASON_EPT_MISCONFIG ) { vcpu->run->internal.ndata++; vcpu->run->internal.data[3 ] =vmcs_read64 (GUEST_PHYSICAL_ADDRESS );
@@ -5884,32 +5885,32 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu,return 1 ; } -if (exit_reason >= kvm_vmx_max_exit_handlers) +if (basic_exit_reason >= kvm_vmx_max_exit_handlers) goto unexpected_vmexit;#ifdef CONFIG_RETPOLINE -if (exit_reason ==EXIT_REASON_MSR_WRITE ) +if (basic_exit_reason ==EXIT_REASON_MSR_WRITE )return kvm_emulate_wrmsr (vcpu); -else if (exit_reason ==EXIT_REASON_PREEMPTION_TIMER ) +else if (basic_exit_reason ==EXIT_REASON_PREEMPTION_TIMER )return handle_preemption_timer (vcpu); -else if (exit_reason ==EXIT_REASON_INTERRUPT_WINDOW ) +else if (basic_exit_reason ==EXIT_REASON_INTERRUPT_WINDOW )return handle_interrupt_window (vcpu); -else if (exit_reason ==EXIT_REASON_EXTERNAL_INTERRUPT ) +else if (basic_exit_reason ==EXIT_REASON_EXTERNAL_INTERRUPT )return handle_external_interrupt (vcpu); -else if (exit_reason ==EXIT_REASON_HLT ) +else if (basic_exit_reason ==EXIT_REASON_HLT )return kvm_emulate_halt (vcpu); -else if (exit_reason ==EXIT_REASON_EPT_MISCONFIG ) +else if (basic_exit_reason ==EXIT_REASON_EPT_MISCONFIG )return handle_ept_misconfig (vcpu);#endif - exit_reason =array_index_nospec (exit_reason, + basic_exit_reason =array_index_nospec (basic_exit_reason, kvm_vmx_max_exit_handlers); -if (!kvm_vmx_exit_handlers[exit_reason]) +if (!kvm_vmx_exit_handlers[basic_exit_reason]) goto unexpected_vmexit; -return kvm_vmx_exit_handlers[exit_reason](vcpu); +return kvm_vmx_exit_handlers[basic_exit_reason](vcpu); unexpected_vmexit: -vcpu_unimpl (vcpu,"vmx: unexpected exit reason 0x%x\n" , exit_reason); +vcpu_unimpl (vcpu,"vmx: unexpected exit reason 0x%x\n" , basic_exit_reason);dump_vmcs (); vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; vcpu->run->internal.suberror =
@@ -6241,13 +6242,14 @@ static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu, enum exit_fastpath_completion *exit_fastpath) {struct vcpu_vmx *vmx =to_vmx (vcpu); +u16 basic_exit_reason =basic (vmx->exit_reason); -if (vmx->exit_reason ==EXIT_REASON_EXTERNAL_INTERRUPT ) +if (basic_exit_reason ==EXIT_REASON_EXTERNAL_INTERRUPT )handle_external_interrupt_irqoff (vcpu); -else if (vmx->exit_reason ==EXIT_REASON_EXCEPTION_NMI ) +else if (basic_exit_reason ==EXIT_REASON_EXCEPTION_NMI )handle_exception_nmi_irqoff (vmx);else if (!is_guest_mode (vcpu) && - vmx->exit_reason ==EXIT_REASON_MSR_WRITE ) + basic_exit_reason ==EXIT_REASON_MSR_WRITE ) *exit_fastpath =handle_fastpath_set_msr_irqoff (vcpu); }
@@ -6621,7 +6623,7 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu) vmx->idt_vectoring_info =0 ; vmx->exit_reason = vmx->fail ?0xdead :vmcs_read32 (VM_EXIT_REASON ); -if ((u16 )vmx->exit_reason ==EXIT_REASON_MCE_DURING_VMENTRY ) +if (basic (vmx->exit_reason) ==EXIT_REASON_MCE_DURING_VMENTRY )kvm_machine_check ();if (vmx->fail || (vmx->exit_reason &VMX_EXIT_REASONS_FAILED_VMENTRY ))
arch/x86/kvm/vmx/vmx.h +2 -0
@@ -22,6 +22,8 @@ extern u32 get_umwait_control_msr(void);#define X2APIC_MSR (r) (APIC_BASE_MSR + ((r) >> 4)) +#define basic (exit_reason) ((u16)(exit_reason)) +#ifdef CONFIG_X86_64 #define NR_SHARED_MSRS 7#else
Signature · 1 line
2.23.0
Re: [PATCH 1/2] kvm: vmx: Use basic exit reason to check if it's the specific VM EXIT
Quoted 5 lines · Xiaoyao Li
Current kvm uses the 32-bit exit reason to check if it's any specific VM EXIT, however only the low 16-bit of VM EXIT REASON acts as the basic exit reason.
Introduce Macro
basic(exit_reaso)
"exit_reason"
Quoted 15 lines
to help retrieve the basic exit reason
from VM EXIT REASON, and use the basic exit reason for checking and indexing the exit hanlder.Signed-off-by Xiaoyao Li <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 44 ++++++++++++++++++++++-------------------- arch/x86/kvm/vmx/vmx.h | 2 ++ 2 files changed, 25 insertions(+), 21 deletions(-)arch/x86/kvm/vmx/vmx.c +0
-0
arch/x86/kvm/vmx/vmx.c static int skip_emulated_instruction(struct kvm_vcpu *vcpu) · line 1587
Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +1
-1@@ -1584,7 +1584,7 @@ static int skip_emulated_instruction(struct kvm_vcpu *vcpu)* i.e. we end up advancing IP with some random value. */ if (!static_cpu_has (X86_FEATURE_HYPERVISOR ) || -to_vmx (vcpu)->exit_reason !=EXIT_REASON_EPT_MISCONFIG ) { +basic (to_vmx (vcpu)->exit_reason) !=EXIT_REASON_EPT_MISCONFIG ) {
"basic" word is probably 'too basic' to be used for this purpose. Even if we need a macro for it (I'm not really convinced it improves the readability), I'd suggest we name it 'basic_exit_reason()' instead.
arch/x86/kvm/vmx/vmx.c static int vmx_handle_exit(struct kvm_vcpu *vcpu, · line 5800
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +1
-0rip =kvm_rip_read (vcpu); rip +=vmcs_read32 (VM_EXIT_INSTRUCTION_LEN );kvm_rip_write (vcpu, rip);@@ -5797,6 +5797,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu, {struct vcpu_vmx *vmx =to_vmx (vcpu);u32 exit_reason = vmx->exit_reason; +u16 basic_exit_reason =basic (exit_reason);
I don't think renaming local variable is needed, let's just do
'u16 exit_reason = basic_exit_reason(vmx->exit_reason)' and keep the rest of the code as-is.
Quoted 71 lines
arch/x86/kvm/vmx/vmx.c +16
-16u32 vectoring_info = vmx->idt_vectoring_info;trace_kvm_exit (exit_reason, vcpu,KVM_ISA_VMX );@@ -5842,17 +5843,17 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu,* will cause infinite loop. */ if ((vectoring_info &VECTORING_INFO_VALID_MASK ) && - (exit_reason !=EXIT_REASON_EXCEPTION_NMI && - exit_reason !=EXIT_REASON_EPT_VIOLATION && - exit_reason !=EXIT_REASON_PML_FULL && - exit_reason !=EXIT_REASON_TASK_SWITCH )) { + (basic_exit_reason !=EXIT_REASON_EXCEPTION_NMI && + basic_exit_reason !=EXIT_REASON_EPT_VIOLATION && + basic_exit_reason !=EXIT_REASON_PML_FULL && + basic_exit_reason !=EXIT_REASON_TASK_SWITCH )) { vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; vcpu->run->internal.suberror =KVM_INTERNAL_ERROR_DELIVERY_EV ; vcpu->run->internal.ndata =3 ; vcpu->run->internal.data[0 ] = vectoring_info; vcpu->run->internal.data[1 ] = exit_reason; vcpu->run->internal.data[2 ] = vcpu->arch.exit_qualification; -if (exit_reason ==EXIT_REASON_EPT_MISCONFIG ) { +if (basic_exit_reason ==EXIT_REASON_EPT_MISCONFIG ) { vcpu->run->internal.ndata++; vcpu->run->internal.data[3 ] =vmcs_read64 (GUEST_PHYSICAL_ADDRESS );@@ -5884,32 +5885,32 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu,return 1 ; } -if (exit_reason >= kvm_vmx_max_exit_handlers) +if (basic_exit_reason >= kvm_vmx_max_exit_handlers) goto unexpected_vmexit;#ifdef CONFIG_RETPOLINE -if (exit_reason ==EXIT_REASON_MSR_WRITE ) +if (basic_exit_reason ==EXIT_REASON_MSR_WRITE )return kvm_emulate_wrmsr (vcpu); -else if (exit_reason ==EXIT_REASON_PREEMPTION_TIMER ) +else if (basic_exit_reason ==EXIT_REASON_PREEMPTION_TIMER )return handle_preemption_timer (vcpu); -else if (exit_reason ==EXIT_REASON_INTERRUPT_WINDOW ) +else if (basic_exit_reason ==EXIT_REASON_INTERRUPT_WINDOW )return handle_interrupt_window (vcpu); -else if (exit_reason ==EXIT_REASON_EXTERNAL_INTERRUPT ) +else if (basic_exit_reason ==EXIT_REASON_EXTERNAL_INTERRUPT )return handle_external_interrupt (vcpu); -else if (exit_reason ==EXIT_REASON_HLT ) +else if (basic_exit_reason ==EXIT_REASON_HLT )return kvm_emulate_halt (vcpu); -else if (exit_reason ==EXIT_REASON_EPT_MISCONFIG ) +else if (basic_exit_reason ==EXIT_REASON_EPT_MISCONFIG )return handle_ept_misconfig (vcpu);#endif - exit_reason =array_index_nospec (exit_reason, + basic_exit_reason =array_index_nospec (basic_exit_reason, kvm_vmx_max_exit_handlers); -if (!kvm_vmx_exit_handlers[exit_reason]) +if (!kvm_vmx_exit_handlers[basic_exit_reason]) goto unexpected_vmexit; -return kvm_vmx_exit_handlers[exit_reason](vcpu); +return kvm_vmx_exit_handlers[basic_exit_reason](vcpu); unexpected_vmexit: -vcpu_unimpl (vcpu,"vmx: unexpected exit reason 0x%x\n" , exit_reason); +vcpu_unimpl (vcpu,"vmx: unexpected exit reason 0x%x\n" , basic_exit_reason);dump_vmcs (); vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; vcpu->run->internal.suberror =
arch/x86/kvm/vmx/vmx.c static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu, · line 6245
Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +1
-0@@ -6241,13 +6242,14 @@ static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu, enum exit_fastpath_completion *exit_fastpath) {struct vcpu_vmx *vmx =to_vmx (vcpu); +u16 basic_exit_reason =basic (vmx->exit_reason);
Here I'd suggest we also use the same
'u16 exit_reason = basic_exit_reason(vmx->exit_reason)'
as above.
Quoted 35 lines
arch/x86/kvm/vmx/vmx.c +4
-4-if (vmx->exit_reason ==EXIT_REASON_EXTERNAL_INTERRUPT ) +if (basic_exit_reason ==EXIT_REASON_EXTERNAL_INTERRUPT )handle_external_interrupt_irqoff (vcpu); -else if (vmx->exit_reason ==EXIT_REASON_EXCEPTION_NMI ) +else if (basic_exit_reason ==EXIT_REASON_EXCEPTION_NMI )handle_exception_nmi_irqoff (vmx);else if (!is_guest_mode (vcpu) && - vmx->exit_reason ==EXIT_REASON_MSR_WRITE ) + basic_exit_reason ==EXIT_REASON_MSR_WRITE ) *exit_fastpath =handle_fastpath_set_msr_irqoff (vcpu); }@@ -6621,7 +6623,7 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu) vmx->idt_vectoring_info =0 ; vmx->exit_reason = vmx->fail ?0xdead :vmcs_read32 (VM_EXIT_REASON ); -if ((u16 )vmx->exit_reason ==EXIT_REASON_MCE_DURING_VMENTRY ) +if (basic (vmx->exit_reason) ==EXIT_REASON_MCE_DURING_VMENTRY )kvm_machine_check ();if (vmx->fail || (vmx->exit_reason &VMX_EXIT_REASONS_FAILED_VMENTRY ))arch/x86/kvm/vmx/vmx.h +2
-0@@ -22,6 +22,8 @@ extern u32 get_umwait_control_msr(void);#define X2APIC_MSR (r) (APIC_BASE_MSR + ((r) >> 4)) +#define basic (exit_reason) ((u16)(exit_reason)) +#ifdef CONFIG_X86_64 #define NR_SHARED_MSRS 7#else
Signature · 1 line
Vitaly
Re: [PATCH 1/2] kvm: vmx: Use basic exit reason to check if it's the specific VM EXIT
Quoted 9 lines · Vitaly Kuznetsov
Quoted 5 lines · Xiaoyao Li
Current kvm uses the 32-bit exit reason to check if it's any specific VM EXIT, however only the low 16-bit of VM EXIT REASON acts as the basic exit reason.
Introduce Macro
basic(exit_reaso)"
exit_reason"
Ah, will correct it in v2.
Quoted 10 lines
Quoted 10 lines
to help retrieve the basic exit reason
from VM EXIT REASON, and use the basic exit reason for checking and indexing the exit hanlder.Signed-off-by Xiaoyao Li <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 44 ++++++++++++++++++++++-------------------- arch/x86/kvm/vmx/vmx.h | 2 ++ 2 files changed, 25 insertions(+), 21 deletions(-)
Quoted 14 lines
arch/x86/kvm/vmx/vmx.c static int skip_emulated_instruction(struct kvm_vcpu *vcpu) · line 1587
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +1
-1@@ -1584,7 +1584,7 @@ static int skip_emulated_instruction(struct kvm_vcpu *vcpu)* i.e. we end up advancing IP with some random value. */ if (!static_cpu_has (X86_FEATURE_HYPERVISOR ) || -to_vmx (vcpu)->exit_reason !=EXIT_REASON_EPT_MISCONFIG ) { +basic (to_vmx (vcpu)->exit_reason) !=EXIT_REASON_EPT_MISCONFIG ) {"basic" word is probably 'too basic' to be used for this purpose. Even if we need a macro for it (I'm not really convinced it improves the readability), I'd suggest we name it '
basic_exit_reason()' instead.
Agreed.
Quoted 11 lines
arch/x86/kvm/vmx/vmx.c static int vmx_handle_exit(struct kvm_vcpu *vcpu, · line 5800
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +1
-0rip =kvm_rip_read (vcpu); rip +=vmcs_read32 (VM_EXIT_INSTRUCTION_LEN );kvm_rip_write (vcpu, rip);@@ -5797,6 +5797,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu, {struct vcpu_vmx *vmx =to_vmx (vcpu);u32 exit_reason = vmx->exit_reason; +u16 basic_exit_reason =basic (exit_reason);I don't think renaming local variable is needed, let's just do
Quoted 2 lines
'u16
exit_reason=basic_exit_reason(vmx->exit_reason)' and keep the rest of the code as-is.
No, we can't do this.
It's not just renaming local variable, the full 32-bit exit reason is used elsewhere in this function that needs the upper 16-bit.
Here variable basic_exit_reason is added for the cases where only basic exit reason number is needed.
Quoted 118 lines
Quoted 71 lines
arch/x86/kvm/vmx/vmx.c +16
-16u32 vectoring_info = vmx->idt_vectoring_info;trace_kvm_exit (exit_reason, vcpu,KVM_ISA_VMX );@@ -5842,17 +5843,17 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu,* will cause infinite loop. */ if ((vectoring_info &VECTORING_INFO_VALID_MASK ) && - (exit_reason !=EXIT_REASON_EXCEPTION_NMI && - exit_reason !=EXIT_REASON_EPT_VIOLATION && - exit_reason !=EXIT_REASON_PML_FULL && - exit_reason !=EXIT_REASON_TASK_SWITCH )) { + (basic_exit_reason !=EXIT_REASON_EXCEPTION_NMI && + basic_exit_reason !=EXIT_REASON_EPT_VIOLATION && + basic_exit_reason !=EXIT_REASON_PML_FULL && + basic_exit_reason !=EXIT_REASON_TASK_SWITCH )) { vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; vcpu->run->internal.suberror =KVM_INTERNAL_ERROR_DELIVERY_EV ; vcpu->run->internal.ndata =3 ; vcpu->run->internal.data[0 ] = vectoring_info; vcpu->run->internal.data[1 ] = exit_reason; vcpu->run->internal.data[2 ] = vcpu->arch.exit_qualification; -if (exit_reason ==EXIT_REASON_EPT_MISCONFIG ) { +if (basic_exit_reason ==EXIT_REASON_EPT_MISCONFIG ) { vcpu->run->internal.ndata++; vcpu->run->internal.data[3 ] =vmcs_read64 (GUEST_PHYSICAL_ADDRESS );@@ -5884,32 +5885,32 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu,return 1 ; } -if (exit_reason >= kvm_vmx_max_exit_handlers) +if (basic_exit_reason >= kvm_vmx_max_exit_handlers) goto unexpected_vmexit;#ifdef CONFIG_RETPOLINE -if (exit_reason ==EXIT_REASON_MSR_WRITE ) +if (basic_exit_reason ==EXIT_REASON_MSR_WRITE )return kvm_emulate_wrmsr (vcpu); -else if (exit_reason ==EXIT_REASON_PREEMPTION_TIMER ) +else if (basic_exit_reason ==EXIT_REASON_PREEMPTION_TIMER )return handle_preemption_timer (vcpu); -else if (exit_reason ==EXIT_REASON_INTERRUPT_WINDOW ) +else if (basic_exit_reason ==EXIT_REASON_INTERRUPT_WINDOW )return handle_interrupt_window (vcpu); -else if (exit_reason ==EXIT_REASON_EXTERNAL_INTERRUPT ) +else if (basic_exit_reason ==EXIT_REASON_EXTERNAL_INTERRUPT )return handle_external_interrupt (vcpu); -else if (exit_reason ==EXIT_REASON_HLT ) +else if (basic_exit_reason ==EXIT_REASON_HLT )return kvm_emulate_halt (vcpu); -else if (exit_reason ==EXIT_REASON_EPT_MISCONFIG ) +else if (basic_exit_reason ==EXIT_REASON_EPT_MISCONFIG )return handle_ept_misconfig (vcpu);#endif - exit_reason =array_index_nospec (exit_reason, + basic_exit_reason =array_index_nospec (basic_exit_reason, kvm_vmx_max_exit_handlers); -if (!kvm_vmx_exit_handlers[exit_reason]) +if (!kvm_vmx_exit_handlers[basic_exit_reason]) goto unexpected_vmexit; -return kvm_vmx_exit_handlers[exit_reason](vcpu); +return kvm_vmx_exit_handlers[basic_exit_reason](vcpu); unexpected_vmexit: -vcpu_unimpl (vcpu,"vmx: unexpected exit reason 0x%x\n" , exit_reason); +vcpu_unimpl (vcpu,"vmx: unexpected exit reason 0x%x\n" , basic_exit_reason);dump_vmcs (); vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; vcpu->run->internal.suberror =arch/x86/kvm/vmx/vmx.c static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu, · line 6245
Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +1
-0@@ -6241,13 +6242,14 @@ static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu, enum exit_fastpath_completion *exit_fastpath) {struct vcpu_vmx *vmx =to_vmx (vcpu); +u16 basic_exit_reason =basic (vmx->exit_reason);Here I'd suggest we also use the same
'u16
exit_reason=basic_exit_reason(vmx->exit_reason)'as above.
Quoted 35 lines
arch/x86/kvm/vmx/vmx.c +4
-4-if (vmx->exit_reason ==EXIT_REASON_EXTERNAL_INTERRUPT ) +if (basic_exit_reason ==EXIT_REASON_EXTERNAL_INTERRUPT )handle_external_interrupt_irqoff (vcpu); -else if (vmx->exit_reason ==EXIT_REASON_EXCEPTION_NMI ) +else if (basic_exit_reason ==EXIT_REASON_EXCEPTION_NMI )handle_exception_nmi_irqoff (vmx);else if (!is_guest_mode (vcpu) && - vmx->exit_reason ==EXIT_REASON_MSR_WRITE ) + basic_exit_reason ==EXIT_REASON_MSR_WRITE ) *exit_fastpath =handle_fastpath_set_msr_irqoff (vcpu); }@@ -6621,7 +6623,7 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu) vmx->idt_vectoring_info =0 ; vmx->exit_reason = vmx->fail ?0xdead :vmcs_read32 (VM_EXIT_REASON ); -if ((u16 )vmx->exit_reason ==EXIT_REASON_MCE_DURING_VMENTRY ) +if (basic (vmx->exit_reason) ==EXIT_REASON_MCE_DURING_VMENTRY )kvm_machine_check ();if (vmx->fail || (vmx->exit_reason &VMX_EXIT_REASONS_FAILED_VMENTRY ))arch/x86/kvm/vmx/vmx.h +2
-0@@ -22,6 +22,8 @@ extern u32 get_umwait_control_msr(void);#define X2APIC_MSR (r) (APIC_BASE_MSR + ((r) >> 4)) +#define basic (exit_reason) ((u16)(exit_reason)) +#ifdef CONFIG_X86_64 #define NR_SHARED_MSRS 7#else
Re: [PATCH 1/2] kvm: vmx: Use basic exit reason to check if it's the specific VM EXIT
Quoted 3 lines · Xiaoyao Li
Quoted 2 lines · Vitaly Kuznetsov
Xiaoyao Li <[email protected]> writes:
...
Quoted 14 lines
Quoted 11 lines
arch/x86/kvm/vmx/vmx.c static int vmx_handle_exit(struct kvm_vcpu *vcpu, · line 5800
Quoted 8 lines
rip = kvm_rip_read(vcpu); rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); kvm_rip_write(vcpu, rip);arch/x86/kvm/vmx/vmx.c +1
-0@@ -5797,6 +5797,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu, {struct vcpu_vmx *vmx =to_vmx (vcpu);u32 exit_reason = vmx->exit_reason; +u16 basic_exit_reason =basic (exit_reason);I don't think renaming local variable is needed, let's just do
Quoted 2 lines
'u16
exit_reason=basic_exit_reason(vmx->exit_reason)' and keep the rest of the code as-is.
Quoted 8 lines
No, we can't do this.
It's not just renaming local variable, the full 32-bit exit reason is used elsewhere in this function that needs the upper 16-bit.
Here variable
basic_exit_reasonis added for the cases where only basic exit reason number is needed.
Can we do the other way around, i.e. introduce 'extended_exit_reason' and use it where all 32 bits are needed? I'm fine with the change, just trying to minimize the (unneeded) code churn.
Signature · 1 line
Vitaly
Re: [PATCH 1/2] kvm: vmx: Use basic exit reason to check if it's the specific VM EXIT
Quoted 27 lines · Vitaly Kuznetsov
Quoted 3 lines · Xiaoyao Li
Quoted 2 lines · Vitaly Kuznetsov
Xiaoyao Li <[email protected]> writes:
...
Quoted 19 lines
Quoted 11 lines
Review static int vmx_handle_exit(struct kvm_vcpu *vcpu, · line 5800
Quoted 8 lines
rip = kvm_rip_read(vcpu); rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); kvm_rip_write(vcpu, rip);(quoted diff) +1
-0@@ -5797,6 +5797,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu, { struct vcpu_vmx *vmx = to_vmx(vcpu); u32 exit_reason = vmx->exit_reason; + u16 basic_exit_reason = basic(exit_reason);I don't think renaming local variable is needed, let's just do
Quoted 2 lines
'u16
exit_reason=basic_exit_reason(vmx->exit_reason)' and keep the rest of the code as-is.No, we can't do this.
It's not just renaming local variable, the full 32-bit exit reason is used elsewhere in this function that needs the upper 16-bit.
Quoted 7 lines
Quoted 3 lines
Here variable
basic_exit_reasonis added for the cases where only basic exit reason number is needed.Can we do the other way around, i.e. introduce '
extended_exit_reason' and use it where all 32 bits are needed? I'm fine with the change, just trying to minimize the (unneeded) code churn.
100% agree. Even better than adding a second field to vcpu_vmx would be to make it a union, though we'd probably want to call it something like full_exit_reason in that case. That should give us compile-time checks on exit_reason, e.g. if we try to query one of the upper bits using a u16, e.g.
arch/x86/kvm/vmx/vmx.c +5 -4
@@ -5818,7 +5818,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu,if (is_guest_mode (vcpu) &&nested_vmx_exit_reflected (vcpu, exit_reason))return nested_vmx_reflect_vmexit (vcpu, exit_reason); -if (exit_reason &VMX_EXIT_REASONS_FAILED_VMENTRY ) { +if (vmx->full_exit_reason &VMX_EXIT_REASONS_FAILED_VMENTRY ) {dump_vmcs (); vcpu->run->exit_reason =KVM_EXIT_FAIL_ENTRY ; vcpu->run->fail_entry.hardware_entry_failure_reason
@@ -6620,11 +6620,12 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu) vmx->nested.nested_run_pending =0 ; vmx->idt_vectoring_info =0 ; - vmx->exit_reason = vmx->fail ?0xdead :vmcs_read32 (VM_EXIT_REASON ); -if ((u16 )vmx->exit_reason ==EXIT_REASON_MCE_DURING_VMENTRY ) + vmx->full_exit_reason = vmx->fail ?0xdead :vmcs_read32 (VM_EXIT_REASON ); +if (vmx->exit_reason ==EXIT_REASON_MCE_DURING_VMENTRY )kvm_machine_check (); -if (vmx->fail || (vmx->exit_reason &VMX_EXIT_REASONS_FAILED_VMENTRY )) +if (vmx->fail || + (vmx->full_exit_reason &VMX_EXIT_REASONS_FAILED_VMENTRY ))return ; vmx->loaded_vmcs->launched =1 ;
arch/x86/kvm/vmx/vmx.h +4 -1
@@ -260,7 +260,10 @@ struct vcpu_vmx {int vpid;bool emulation_required; -u32 exit_reason; +union { +u16 exit_reason; +u32 full_exit_reason; + }/* Posted interrupt descriptor */ struct pi_desc pi_desc;
Quoted 3 lines
Signature · 1 line
Vitaly
Re: [PATCH 1/2] kvm: vmx: Use basic exit reason to check if it's the specific VM EXIT
Quoted 32 lines · Sean Christopherson
Quoted 31 lines · Vitaly Kuznetsov
Quoted 3 lines · Xiaoyao Li
Quoted 2 lines · Vitaly Kuznetsov
Xiaoyao Li <[email protected]> writes:
...
Quoted 22 lines
Quoted 11 lines
Review static int vmx_handle_exit(struct kvm_vcpu *vcpu, · line 5800
Quoted 8 lines
rip = kvm_rip_read(vcpu); rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); kvm_rip_write(vcpu, rip);(quoted diff) +1
-0@@ -5797,6 +5797,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu, { struct vcpu_vmx *vmx = to_vmx(vcpu); u32 exit_reason = vmx->exit_reason; + u16 basic_exit_reason = basic(exit_reason);I don't think renaming local variable is needed, let's just do
Quoted 2 lines
'u16
exit_reason=basic_exit_reason(vmx->exit_reason)' and keep the rest of the code as-is.No, we can't do this.
It's not just renaming local variable, the full 32-bit exit reason is used elsewhere in this function that needs the upper 16-bit.
Here variable
basic_exit_reasonis added for the cases where only basic exit reason number is needed.
Quoted 8 lines
Quoted 3 lines
Can we do the other way around, i.e. introduce '
extended_exit_reason' and use it where all 32 bits are needed? I'm fine with the change, just trying to minimize the (unneeded) code churn.100% agree. Even better than adding a second field to
vcpu_vmxwould be to make it a union, though we'd probably want to call it something likefull_exit_reasonin that case. That should give us compile-time checks onexit_reason, e.g. if we try to query one of the upper bits using a u16, e.g.
I have thought about union, but it seems
union {
u16 exit_reason;
u32 full_exit_reason;
}is not a good name. Since there are many codes in vmx.c and nested.c assume that exit_reason stands for 32-bit EXIT REASON vmcs field as well as evmcs->vm_exit_reason and vmcs12->vm_exit_reason. Do we really want to also rename them to full_exit_reason?
Maybe we name it
union {
u16 basic_exit_reason;
u32 exit_reason;
}as what SDM defines?
Quoted 51 lines
arch/x86/kvm/vmx/vmx.c +5
-4@@ -5818,7 +5818,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu,if (is_guest_mode (vcpu) &&nested_vmx_exit_reflected (vcpu, exit_reason))return nested_vmx_reflect_vmexit (vcpu, exit_reason); -if (exit_reason &VMX_EXIT_REASONS_FAILED_VMENTRY ) { +if (vmx->full_exit_reason &VMX_EXIT_REASONS_FAILED_VMENTRY ) {dump_vmcs (); vcpu->run->exit_reason =KVM_EXIT_FAIL_ENTRY ; vcpu->run->fail_entry.hardware_entry_failure_reason@@ -6620,11 +6620,12 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu) vmx->nested.nested_run_pending =0 ; vmx->idt_vectoring_info =0 ; - vmx->exit_reason = vmx->fail ?0xdead :vmcs_read32 (VM_EXIT_REASON ); -if ((u16 )vmx->exit_reason ==EXIT_REASON_MCE_DURING_VMENTRY ) + vmx->full_exit_reason = vmx->fail ?0xdead :vmcs_read32 (VM_EXIT_REASON ); +if (vmx->exit_reason ==EXIT_REASON_MCE_DURING_VMENTRY )kvm_machine_check (); -if (vmx->fail || (vmx->exit_reason &VMX_EXIT_REASONS_FAILED_VMENTRY )) +if (vmx->fail || + (vmx->full_exit_reason &VMX_EXIT_REASONS_FAILED_VMENTRY ))return ; vmx->loaded_vmcs->launched =1 ;arch/x86/kvm/vmx/vmx.h +4
-1@@ -260,7 +260,10 @@ struct vcpu_vmx {int vpid;bool emulation_required; -u32 exit_reason; +union { +u16 exit_reason; +u32 full_exit_reason; + }/* Posted interrupt descriptor */ struct pi_desc pi_desc;Quoted 8 lines
Signature · 2 lines
-- Vitaly
Re: [PATCH 1/2] kvm: vmx: Use basic exit reason to check if it's the specific VM EXIT
Quoted 27 lines · Xiaoyao Li
Quoted 10 lines · Sean Christopherson
Quoted 9 lines · Vitaly Kuznetsov
Quoted 6 lines · Xiaoyao Li
Quoted 2 lines · Vitaly Kuznetsov
Xiaoyao Li <[email protected]> writes:
Here variable
basic_exit_reasonis added for the cases where only basic exit reason number is needed.Quoted 8 lines
Quoted 3 lines
Can we do the other way around, i.e. introduce '
extended_exit_reason' and use it where all 32 bits are needed? I'm fine with the change, just trying to minimize the (unneeded) code churn.100% agree. Even better than adding a second field to
vcpu_vmxwould be to make it a union, though we'd probably want to call it something likefull_exit_reasonin that case. That should give us compile-time checks onexit_reason, e.g. if we try to query one of the upper bits using a u16, e.g.I have thought about union, but it seems
union { u16 exit_reason; u32 full_exit_reason; }
Quoted 4 lines
is not a good name. Since there are many codes in
vmx.candnested.cassume thatexit_reasonstands for 32-bit EXIT REASON vmcs field as well asevmcs->vm_exit_reasonandvmcs12->vm_exit_reason. Do we really want to also rename them tofull_exit_reason?
It's actually the opposite, almost all of the VMX code assumes exit_reason holds only the basic exit reason, i.e. a 16-bit value. For example, SGX adds a modifier flag to denote a VM-Exit was from enclave mode, and that bit needs to be stripped from exit_reason, otherwise all the checks like "if (exit_reason == blah_blah_blah)" fail.
Making exit_reason a 16-bit alias of the full/extended exit_reason neatly sidesteps that issue. And it is an issue that has caused actual problems in the past, e.g. see commit beb8d93b3e42 ("KVM: VMX: Fix handling of #MC that occurs during VM-Entry"). Coincidentally, that commit also removes a local "u16 basic_exit_reason" :-).
Except for one mistake, the pseudo-patch below is the entirety of required changes. Most (all?) of the functions that take "u32 exit_reason" can (and should) continue to take a u32.
As for the name, I strongly prefer keeping the exit_reason name for the basic exit reason. The vast majority of VM-Exits do not have modifiers set, i.e. "basic exit reason" == vmcs.EXIT_REASON for nearly all normal usage. This holds true in every form of communication, e.g. when discussing VM-Exit reasons, it's never qualified with "basic", it's simply the exit reason. IMO the code is better off following the colloquial usage of "exit reason". A simple comment above the union would suffice to clear up any confusion with respect to the SDM.
Quoted 9 lines
Maybe we name it
union { u16 basic_exit_reason; u32 exit_reason; }as what SDM defines?
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c static int vmx_handle_exit(struct kvm_vcpu *vcpu, · line 5821
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +1
-1@@ -5818,7 +5818,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu,if (is_guest_mode (vcpu) &&nested_vmx_exit_reflected (vcpu, exit_reason))return nested_vmx_reflect_vmexit (vcpu, exit_reason); -if (exit_reason &VMX_EXIT_REASONS_FAILED_VMENTRY ) { +if (vmx->full_exit_reason &VMX_EXIT_REASONS_FAILED_VMENTRY ) {
If we do go the union route, this snippet of code is insufficient, the full/extended exit reason needs to be snapshotted early for use in the tracepoint and in fail_entry.hardware_entry_failure_reason.
Quoted 44 lines
Quoted 43 lines
arch/x86/kvm/vmx/vmx.c +4
-3dump_vmcs (); vcpu->run->exit_reason =KVM_EXIT_FAIL_ENTRY ; vcpu->run->fail_entry.hardware_entry_failure_reason@@ -6620,11 +6620,12 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu) vmx->nested.nested_run_pending =0 ; vmx->idt_vectoring_info =0 ; - vmx->exit_reason = vmx->fail ?0xdead :vmcs_read32 (VM_EXIT_REASON ); -if ((u16 )vmx->exit_reason ==EXIT_REASON_MCE_DURING_VMENTRY ) + vmx->full_exit_reason = vmx->fail ?0xdead :vmcs_read32 (VM_EXIT_REASON ); +if (vmx->exit_reason ==EXIT_REASON_MCE_DURING_VMENTRY )kvm_machine_check (); -if (vmx->fail || (vmx->exit_reason &VMX_EXIT_REASONS_FAILED_VMENTRY )) +if (vmx->fail || + (vmx->full_exit_reason &VMX_EXIT_REASONS_FAILED_VMENTRY ))return ; vmx->loaded_vmcs->launched =1 ;arch/x86/kvm/vmx/vmx.h +4
-1@@ -260,7 +260,10 @@ struct vcpu_vmx {int vpid;bool emulation_required; -u32 exit_reason; +union { +u16 exit_reason; +u32 full_exit_reason; + }/* Posted interrupt descriptor */ struct pi_desc pi_desc;Quoted 8 lines
Signature · 2 lines
-- Vitaly
Re: [PATCH 1/2] kvm: vmx: Use basic exit reason to check if it's the specific VM EXIT
Quoted 49 lines · Sean Christopherson
Quoted 27 lines · Xiaoyao Li
Quoted 10 lines · Sean Christopherson
Quoted 9 lines · Vitaly Kuznetsov
Quoted 6 lines · Xiaoyao Li
Quoted 2 lines · Vitaly Kuznetsov
Xiaoyao Li <[email protected]> writes:
Here variable
basic_exit_reasonis added for the cases where only basic exit reason number is needed.Quoted 8 lines
Quoted 3 lines
Can we do the other way around, i.e. introduce '
extended_exit_reason' and use it where all 32 bits are needed? I'm fine with the change, just trying to minimize the (unneeded) code churn.100% agree. Even better than adding a second field to
vcpu_vmxwould be to make it a union, though we'd probably want to call it something likefull_exit_reasonin that case. That should give us compile-time checks onexit_reason, e.g. if we try to query one of the upper bits using a u16, e.g.I have thought about union, but it seems
union { u16 exit_reason; u32 full_exit_reason; }Quoted 4 lines
is not a good name. Since there are many codes in
vmx.candnested.cassume thatexit_reasonstands for 32-bit EXIT REASON vmcs field as well asevmcs->vm_exit_reasonandvmcs12->vm_exit_reason. Do we really want to also rename them tofull_exit_reason?It's actually the opposite, almost all of the VMX code assumes
exit_reasonholds only the basic exit reason, i.e. a 16-bit value. For example, SGX adds a modifier flag to denote a VM-Exit was from enclave mode, and that bit needs to be stripped fromexit_reason, otherwise all the checks like "if (exit_reason==blah_blah_blah)" fail.Making
exit_reasona 16-bit alias of the full/extendedexit_reasonneatly sidesteps that issue. And it is an issue that has caused actual problems in the past, e.g. see commitbeb8d93b3e42("KVM: VMX: Fix handling of #MC that occurs during VM-Entry"). Coincidentally, that commit also removes a local "u16basic_exit_reason" :-).Except for one mistake, the pseudo-patch below is the entirety of required changes. Most (all?) of the functions that take "u32
exit_reason" can (and should) continue to take a u32.
Quoted 8 lines
As for the name, I strongly prefer keeping the
exit_reasonname for the basic exit reason. The vast majority of VM-Exits do not have modifiers set, i.e. "basic exit reason" == vmcs.EXIT_REASON for nearly all normal usage. This holds true in every form of communication, e.g. when discussing VM-Exit reasons, it's never qualified with "basic", it's simply the exit reason. IMO the code is better off following the colloquial usage of "exit reason". A simple comment above the union would suffice to clear up any confusion with respect to the SDM.
Well, for this reason we can keep exit_reason for 16-bit usage, and define full/extended_exit_reason for 32-bit cases. This makes less code churn.
But after we choose to use exit_reason and full/extended_exit_reason, what if someday new modifier flags are added and we want to enable some modifier flags for nested case? I guess we need to change existing exit_reason to full/extended_exit_reason in nested.c/nested.h to keep the naming rule consistent.
Quoted 66 lines
Quoted 9 lines
Maybe we name it
union { u16 basic_exit_reason; u32 exit_reason; }as what SDM defines?
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c static int vmx_handle_exit(struct kvm_vcpu *vcpu, · line 5821
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +1
-1@@ -5818,7 +5818,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu,if (is_guest_mode (vcpu) &&nested_vmx_exit_reflected (vcpu, exit_reason))return nested_vmx_reflect_vmexit (vcpu, exit_reason); -if (exit_reason &VMX_EXIT_REASONS_FAILED_VMENTRY ) { +if (vmx->full_exit_reason &VMX_EXIT_REASONS_FAILED_VMENTRY ) {If we do go the union route, this snippet of code is insufficient, the full/extended exit reason needs to be snapshotted early for use in the tracepoint and in fail_entry.hardware_entry_failure_reason.
Quoted 44 lines
Quoted 43 lines
arch/x86/kvm/vmx/vmx.c +4
-3dump_vmcs (); vcpu->run->exit_reason =KVM_EXIT_FAIL_ENTRY ; vcpu->run->fail_entry.hardware_entry_failure_reason@@ -6620,11 +6620,12 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu) vmx->nested.nested_run_pending =0 ; vmx->idt_vectoring_info =0 ; - vmx->exit_reason = vmx->fail ?0xdead :vmcs_read32 (VM_EXIT_REASON ); -if ((u16 )vmx->exit_reason ==EXIT_REASON_MCE_DURING_VMENTRY ) + vmx->full_exit_reason = vmx->fail ?0xdead :vmcs_read32 (VM_EXIT_REASON ); +if (vmx->exit_reason ==EXIT_REASON_MCE_DURING_VMENTRY )kvm_machine_check (); -if (vmx->fail || (vmx->exit_reason &VMX_EXIT_REASONS_FAILED_VMENTRY )) +if (vmx->fail || + (vmx->full_exit_reason &VMX_EXIT_REASONS_FAILED_VMENTRY ))return ; vmx->loaded_vmcs->launched =1 ;arch/x86/kvm/vmx/vmx.h +4
-1@@ -260,7 +260,10 @@ struct vcpu_vmx {int vpid;bool emulation_required; -u32 exit_reason; +union { +u16 exit_reason; +u32 full_exit_reason; + }/* Posted interrupt descriptor */ struct pi_desc pi_desc;Quoted 8 lines
Signature · 2 lines
-- Vitaly
Re: [PATCH 1/2] kvm: vmx: Use basic exit reason to check if it's the specific VM EXIT
Quoted 40 lines · Xiaoyao Li
Quoted 30 lines · Sean Christopherson
Quoted 8 lines · Xiaoyao Li
On 2/25/2020 12:17 AM, Sean Christopherson wrote:
I have thought about union, but it seemsunion { u16 exit_reason; u32 full_exit_reason; }Quoted 4 lines
is not a good name. Since there are many codes in
vmx.candnested.cassume thatexit_reasonstands for 32-bit EXIT REASON vmcs field as well asevmcs->vm_exit_reasonandvmcs12->vm_exit_reason. Do we really want to also rename them tofull_exit_reason?It's actually the opposite, almost all of the VMX code assumes
exit_reasonholds only the basic exit reason, i.e. a 16-bit value. For example, SGX adds a modifier flag to denote a VM-Exit was from enclave mode, and that bit needs to be stripped fromexit_reason, otherwise all the checks like "if (exit_reason==blah_blah_blah)" fail.Making
exit_reasona 16-bit alias of the full/extendedexit_reasonneatly sidesteps that issue. And it is an issue that has caused actual problems in the past, e.g. see commitbeb8d93b3e42("KVM: VMX: Fix handling of #MC that occurs during VM-Entry"). Coincidentally, that commit also removes a local "u16basic_exit_reason" :-).Except for one mistake, the pseudo-patch below is the entirety of required changes. Most (all?) of the functions that take "u32
exit_reason" can (and should) continue to take a u32.Quoted 8 lines
As for the name, I strongly prefer keeping the
exit_reasonname for the basic exit reason. The vast majority of VM-Exits do not have modifiers set, i.e. "basic exit reason" == vmcs.EXIT_REASON for nearly all normal usage. This holds true in every form of communication, e.g. when discussing VM-Exit reasons, it's never qualified with "basic", it's simply the exit reason. IMO the code is better off following the colloquial usage of "exit reason". A simple comment above the union would suffice to clear up any confusion with respect to the SDM.
Quoted 8 lines
Well, for this reason we can keep
exit_reasonfor 16-bit usage, and define full/extended_exit_reason for 32-bit cases. This makes less code churn.But after we choose to use
exit_reasonand full/extended_exit_reason, what if someday new modifier flags are added and we want to enable some modifier flags for nested case?
I guess we need to change existingexit_reasonto full/extended_exit_reason innested.c/nested.hto keep the naming rule consistent.
Ah, good point. But, that's just another bug in my psuedo patch :-) It's literally one call site that needs to be updated. E.g.
if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason)) return nested_vmx_reflect_vmexit(vcpu, full_exit_reason);
Everywhere else KVM calls nested_vmx_reflect_vmexit() is (currently) done with a hardcoded value (except handle_vmfunc(), but I actually want to change that one).
Re: [PATCH 1/2] kvm: vmx: Use basic exit reason to check if it's the specific VM EXIT
Quoted 50 lines · Sean Christopherson
Quoted 40 lines · Xiaoyao Li
Quoted 30 lines · Sean Christopherson
Quoted 8 lines · Xiaoyao Li
On 2/25/2020 12:17 AM, Sean Christopherson wrote:
I have thought about union, but it seemsunion { u16 exit_reason; u32 full_exit_reason; }Quoted 4 lines
is not a good name. Since there are many codes in
vmx.candnested.cassume thatexit_reasonstands for 32-bit EXIT REASON vmcs field as well asevmcs->vm_exit_reasonandvmcs12->vm_exit_reason. Do we really want to also rename them tofull_exit_reason?It's actually the opposite, almost all of the VMX code assumes
exit_reasonholds only the basic exit reason, i.e. a 16-bit value. For example, SGX adds a modifier flag to denote a VM-Exit was from enclave mode, and that bit needs to be stripped fromexit_reason, otherwise all the checks like "if (exit_reason==blah_blah_blah)" fail.Making
exit_reasona 16-bit alias of the full/extendedexit_reasonneatly sidesteps that issue. And it is an issue that has caused actual problems in the past, e.g. see commitbeb8d93b3e42("KVM: VMX: Fix handling of #MC that occurs during VM-Entry"). Coincidentally, that commit also removes a local "u16basic_exit_reason" :-).Except for one mistake, the pseudo-patch below is the entirety of required changes. Most (all?) of the functions that take "u32
exit_reason" can (and should) continue to take a u32.Quoted 8 lines
As for the name, I strongly prefer keeping the
exit_reasonname for the basic exit reason. The vast majority of VM-Exits do not have modifiers set, i.e. "basic exit reason" == vmcs.EXIT_REASON for nearly all normal usage. This holds true in every form of communication, e.g. when discussing VM-Exit reasons, it's never qualified with "basic", it's simply the exit reason. IMO the code is better off following the colloquial usage of "exit reason". A simple comment above the union would suffice to clear up any confusion with respect to the SDM.Quoted 8 lines
Well, for this reason we can keep
exit_reasonfor 16-bit usage, and define full/extended_exit_reason for 32-bit cases. This makes less code churn.But after we choose to use
exit_reasonand full/extended_exit_reason, what if someday new modifier flags are added and we want to enable some modifier flags for nested case?
I guess we need to change existingexit_reasonto full/extended_exit_reason innested.c/nested.hto keep the naming rule consistent.
Quoted 6 lines
Ah, good point. But, that's just another bug in my psuedo patch :-) It's literally one call site that needs to be updated. E.g.
if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason)) return nested_vmx_reflect_vmexit(vcpu, full_exit_reason);
shouldn't we also pass full_exit_reason to nested_vmx_exit_reflected()?
Quoted 1 line
Everywhere else KVM calls
nested_vmx_reflect_vmexit()is (currently) done
I guess you wanted to say nested_vmx_vmexit() not nested_vmx_reflect_vmexit() here.
Quoted 3 lines
with a hardcoded value (except
handle_vmfunc(), but I actually want to change that one).
Re: [PATCH 1/2] kvm: vmx: Use basic exit reason to check if it's the specific VM EXIT
Quoted 9 lines · Xiaoyao Li
Quoted 6 lines · Sean Christopherson
Ah, good point. But, that's just another bug in my psuedo patch :-) It's literally one call site that needs to be updated. E.g.
if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason)) return nested_vmx_reflect_vmexit(vcpu, full_exit_reason);shouldn't we also pass
full_exit_reasontonested_vmx_exit_reflected()?
Yep, see the patch I sent. Alternatively, and perhaps a better approach once we have the union, would be to not pass exit_reason at all and instead have nested_vmx_exit_reflected() grab it directly from vmx->...
Quoted 5 lines
Quoted 2 lines
Everywhere else KVM calls
nested_vmx_reflect_vmexit()is (currently) doneI guess you wanted to say
nested_vmx_vmexit()notnested_vmx_reflect_vmexit()here.
Ya.
Quoted 4 lines
Quoted 3 lines
with a hardcoded value (except
handle_vmfunc(), but I actually want to change that one).
Re: [PATCH 1/2] kvm: vmx: Use basic exit reason to check if it's the specific VM EXIT
In reply to Xiaoyao LiQuoted 162 lines · Xiaoyao Li
Quoted 9 lines · Vitaly Kuznetsov
Quoted 5 lines · Xiaoyao Li
Current kvm uses the 32-bit exit reason to check if it's any specific VM EXIT, however only the low 16-bit of VM EXIT REASON acts as the basic exit reason.
Introduce Macro
basic(exit_reaso)"
exit_reason"Ah, will correct it in v2.
Quoted 11 lines
Quoted 11 lines
to help retrieve the basic exit reason
from VM EXIT REASON, and use the basic exit reason for checking and indexing the exit hanlder.Signed-off-by Xiaoyao Li <[email protected]>---
arch/x86/kvm/vmx/vmx.c| 44
++++++++++++++++++++++--------------------
arch/x86/kvm/vmx/vmx.h| 2 ++2 files changed, 25 insertions(+), 21 deletions(-)Quoted 16 lines
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +0
-0@@ -1584,7 +1584,7 @@ static int skip_emulated_instruction(structkvm_vcpu *vcpu) * i.e. we end up advancing IP with some random value. */ Quoted 4 lines
if (!static_cpu_has(X86_FEATURE_HYPERVISOR) ||
- to_vmx(vcpu)->exit_reason !=EXIT_REASON_EPT_MISCONFIG) { + basic(to_vmx(vcpu)->exit_reason) !=EXIT_REASON_EPT_MISCONFIG) {"basic" word is probably 'too basic' to be used for this purpose. Even if we need a macro for it (I'm not really convinced it improves the readability), I'd suggest we name it '
basic_exit_reason()' instead.Agreed.
Quoted 11 lines
arch/x86/kvm/vmx/vmx.c static int vmx_handle_exit(struct kvm_vcpu *vcpu, · line 5800
Quoted 8 lines
rip =
kvm_rip_read(vcpu);
rip +=vmcs_read32(VM_EXIT_INSTRUCTION_LEN);kvm_rip_write(vcpu, rip);arch/x86/kvm/vmx/vmx.c +1
-0@@ -5797,6 +5797,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu, { struct vcpu_vmx *vmx = to_vmx(vcpu); u32 exit_reason = vmx->exit_reason; + u16 basic_exit_reason = basic(exit_reason);I don't think renaming local variable is needed, let's just do
Quoted 2 lines
'u16
exit_reason=basic_exit_reason(vmx->exit_reason)' and keep the rest of the code as-is.No, we can't do this.
It's not just renaming local variable, the full 32-bit exit reason is used elsewhere in this function that needs the upper 16-bit.
Here variable
basic_exit_reasonis added for the cases where only basic exit reason number is needed.Quoted 99 lines
Quoted 70 lines
arch/x86/kvm/vmx/vmx.c +0
-0u32 vectoring_info = vmx->idt_vectoring_info; trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);@@ -5842,17 +5843,17 @@ static int vmx_handle_exit(struct kvm_vcpu*vcpu, * will cause infinite loop. */ if ((
vectoring_info&VECTORING_INFO_VALID_MASK) &&
- (exit_reason!=EXIT_REASON_EXCEPTION_NMI&&
-exit_reason!=EXIT_REASON_EPT_VIOLATION&&
-exit_reason!=EXIT_REASON_PML_FULL&&
-exit_reason!=EXIT_REASON_TASK_SWITCH)) {
+ (basic_exit_reason!=EXIT_REASON_EXCEPTION_NMI&& +basic_exit_reason!=EXIT_REASON_EPT_VIOLATION&& +basic_exit_reason!=EXIT_REASON_PML_FULL&&
+basic_exit_reason!=EXIT_REASON_TASK_SWITCH)) {vcpu->run->exit_reason=KVM_EXIT_INTERNAL_ERROR;vcpu->run->internal.suberror=KVM_INTERNAL_ERROR_DELIVERY_EV;vcpu->run->internal.ndata= 3; vcpu->run->internal.data[0] =vectoring_info; vcpu->run->internal.data[1] =exit_reason; vcpu->run->internal.data[2] =vcpu->arch.exit_qualification; - if (exit_reason==EXIT_REASON_EPT_MISCONFIG) {
+ if (basic_exit_reason==EXIT_REASON_EPT_MISCONFIG) { vcpu->run->internal.ndata++; vcpu->run->internal.data[3] =vmcs_read64(GUEST_PHYSICAL_ADDRESS);arch/x86/kvm/vmx/vmx.c +10
-7@@ -5884,32 +5885,32 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu, return 1; } - if (exit_reason >= kvm_vmx_max_exit_handlers) + if (basic_exit_reason >= kvm_vmx_max_exit_handlers) goto unexpected_vmexit; #ifdef CONFIG_RETPOLINE - if (exit_reason == EXIT_REASON_MSR_WRITE) + if (basic_exit_reason == EXIT_REASON_MSR_WRITE) return kvm_emulate_wrmsr(vcpu); - else if (exit_reason == EXIT_REASON_PREEMPTION_TIMER) + else if (basic_exit_reason == EXIT_REASON_PREEMPTION_TIMER) return handle_preemption_timer(vcpu); - else if (exit_reason == EXIT_REASON_INTERRUPT_WINDOW) + else if (basic_exit_reason == EXIT_REASON_INTERRUPT_WINDOW) return handle_interrupt_window(vcpu); - else if (exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT) + else if (basic_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT) return handle_external_interrupt(vcpu); - else if (exit_reason == EXIT_REASON_HLT) + else if (basic_exit_reason == EXIT_REASON_HLT) return kvm_emulate_halt(vcpu); - else if (exit_reason == EXIT_REASON_EPT_MISCONFIG) + else if (basic_exit_reason == EXIT_REASON_EPT_MISCONFIG) return handle_ept_misconfig(vcpu); #endif - exit_reason = array_index_nospec(exit_reason, + basic_exit_reason = array_index_nospec(basic_exit_reason, kvm_vmx_max_exit_handlers); - if (!kvm_vmx_exit_handlers[exit_reason]) + if (!kvm_vmx_exit_handlers[basic_exit_reason]) goto unexpected_vmexit; - return kvm_vmx_exit_handlers[exit_reason](vcpu); + return kvm_vmx_exit_handlers[basic_exit_reason](vcpu);
unexpected_vmexit:
-vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", exit_reason);
+vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", basic_exit_reason);
dump_vmcs();
vcpu->run->exit_reason=KVM_EXIT_INTERNAL_ERROR;vcpu->run->internal.suberror=arch/x86/kvm/vmx/vmx.c static void vmx_handle_exit_irqoff(struct · line 6246
Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +1
-0@@ -6241,13 +6242,14 @@ static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu, enum exit_fastpath_completion *exit_fastpath) { struct vcpu_vmx *vmx = to_vmx(vcpu); + u16 basic_exit_reason = basic(vmx->exit_reason);Here I'd suggest we also use the same
'u16
exit_reason=basic_exit_reason(vmx->exit_reason)'as above.
Quoted 17 lines
arch/x86/kvm/vmx/vmx.c +2
-1- if (vmx->exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT) + if (basic_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT) handle_external_interrupt_irqoff(vcpu); - else if (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI) + else if (basic_exit_reason == EXIT_REASON_EXCEPTION_NMI) handle_exception_nmi_irqoff(vmx);else if (!is_guest_mode(vcpu) &&
-vmx->exit_reason==EXIT_REASON_MSR_WRITE)
+basic_exit_reason==EXIT_REASON_MSR_WRITE)*exit_fastpath=handle_fastpath_set_msr_irqoff(vcpu); }
@@ -6621,7 +6623,7 @@ static voidvmx_vcpu_run(struct kvm_vcpu *vcpu)vmx->idt_vectoring_info= 0;
vmx->exit_reason=vmx->fail?0xdead:vmcs_read32(VM_EXIT_REASON); - if ((u16)vmx->exit_reason ==EXIT_REASON_MCE_DURING_VMENTRY) + if (basic(vmx->exit_reason)==EXIT_REASON_MCE_DURING_VMENTRY)kvm_machine_check();
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/vmx/vmx.h extern u32 get_umwait_control_msr(void); · line 23
Quoted 8 lines
if (
vmx->fail|| (vmx->exit_reason&VMX_EXIT_REASONS_FAILED_VMENTRY))arch/x86/kvm/vmx/vmx.h +0
-0@@ -22,6 +22,8 @@ extern u32 get_umwait_control_msr(void); #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4)) +#define basic(exit_reason) ((u16)(exit_reason))
We have a macro for bit 31,
VMX_EXIT_REASONS_FAILED_VMENTRY 0x80000000
Does it make sense to define a macro like that instead ? Say,
VMX_BASIC_EXIT_REASON 0x0000ffff
and then we do,
u32 exit_reason = vmx->exit_reason;
u16 basic_exit_reason = exit_reason & VMX_BASIC_EXIT_REASON;
Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
arch/x86/kvm/vmx/vmx.h +1
-0+ #ifdef CONFIG_X86_64 #define NR_SHARED_MSRS 7 #else
Re: [PATCH 1/2] kvm: vmx: Use basic exit reason to check if it's the specific VM EXIT
Quoted 10 lines · Krish Sadhukhan
We have a macro for bit 31,
VMX_EXIT_REASONS_FAILED_VMENTRY
0x80000000Does it make sense to define a macro like that instead ? Say,
VMX_BASIC_EXIT_REASON
0x0000ffff
0xffffU ?
Quoted 5 lines
and then we do,
u32
exit_reason=vmx->exit_reason;
u16basic_exit_reason=exit_reason&VMX_BASIC_EXIT_REASON;
Just a naming suggestion: if we decide to go down this road, let's name it e.g. VMX_BASIC_EXIT_REASON_MASK to make it clear this is *not* an exit reason.
Signature · 1 line
Vitaly
Re: [PATCH 1/2] kvm: vmx: Use basic exit reason to check if it's the specific VM EXIT
Quoted 13 lines · Vitaly Kuznetsov
Quoted 9 lines · Krish Sadhukhan
We have a macro for bit 31,
VMX_EXIT_REASONS_FAILED_VMENTRY
0x80000000Does it make sense to define a macro like that instead ? Say,
VMX_BASIC_EXIT_REASON
0x0000ffff
0xffffU?
Quoted 9 lines
Quoted 5 lines
and then we do,
u32 exit_reason = vmx->exit_reason; u16 basic_exit_reason = exit_reason & VMX_BASIC_EXIT_REASON;Just a naming suggestion: if we decide to go down this road, let's name it e.g.
VMX_BASIC_EXIT_REASON_MASKto make it clear this is *not* an exit reason.
VMX_BASIC_EXIT_REASON_MASK works.
PATCH 2/2 kvm: nvmx: Use basic(exit_reason) when checking specific EXIT_REASON
In reply to Xiaoyao LiUse the basic exit reason when checking if it equals the specific EXIT REASON for nested.
--- arch/x86/kvm/vmx/nested.c | 6 +++--- arch/x86/kvm/vmx/nested.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/nested.c +3 -3
@@ -4281,7 +4281,7 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,* message for details. */ if (nested_exit_intr_ack_set (vcpu) && - exit_reason ==EXIT_REASON_EXTERNAL_INTERRUPT && +basic (exit_reason) ==EXIT_REASON_EXTERNAL_INTERRUPT &&kvm_cpu_has_interrupt (vcpu)) {int irq =kvm_cpu_get_interrupt (vcpu);WARN_ON (irq <0 );
@@ -5321,7 +5321,7 @@ static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,* First we need to figure out which of the four to use: */ bitmap = vmcs12->msr_bitmap; -if (exit_reason ==EXIT_REASON_MSR_WRITE ) +if (basic (exit_reason) ==EXIT_REASON_MSR_WRITE ) bitmap +=2048 ;if (msr_index >=0xc0000000 ) { msr_index -=0xc0000000 ;
@@ -5487,7 +5487,7 @@ bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)vmcs_read32 (VM_EXIT_INTR_ERROR_CODE ), KVM_ISA_VMX); -switch (exit_reason) { +switch (basic (exit_reason)) {case EXIT_REASON_EXCEPTION_NMI :if (is_nmi (intr_info))return false;
arch/x86/kvm/vmx/nested.h +1 -1
@@ -83,7 +83,7 @@ static inline int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu,* is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT * we need to query the in-kernel LAPIC. */ -WARN_ON (exit_reason ==EXIT_REASON_EXTERNAL_INTERRUPT ); +WARN_ON (basic (exit_reason) ==EXIT_REASON_EXTERNAL_INTERRUPT );if ((exit_intr_info & (INTR_INFO_VALID_MASK |INTR_INFO_DELIVER_CODE_MASK )) == (INTR_INFO_VALID_MASK |INTR_INFO_DELIVER_CODE_MASK )) {
Signature · 1 line
2.23.0