nVMX: Clean up __enter_guest() and co.
cover nVMX: Clean up __enter_guest() and co.
Start chipping away at the crustiness in the nVMX tests by refactoring "struct vmentry_failure" into "struct vmentry_result", with the full VM-Exit stored in vmentry_result. Capturing the exit reason allows for a variety of cleanups and consolidations.
This series really only dives into the v1 tests. I'd like to also clean up the v2 tests, e.g. take the expected exit reason in enter_guest() so that the expected behavior is more obvious, but that's a more invasive cleanup for another day.
Sean Christopherson (8):
nVMX: Eliminate superfluous entry_failure_handler() wrapper
nVMX: Refactor VM-Entry "failure" struct into "result"
nVMX: Consolidate non-canonical code in test_canonical()
nVMX: Drop redundant check for guest termination
nVMX: Expose __enter_guest() and consolidate guest state test code
nVMX: Pass exit reason union to v1 exit handlers
nVMX: Pass exit reason union to is_hypercall()
nVMX: Pass exit reason enum to print_vmexit_info()
x86/vmx.c | 191 +++++++++++-------------- x86/vmx.h | 50 +++++-- x86/vmx_tests.c | 366 +++++++++++++++++++----------------------------- 3 files changed, 263 insertions(+), 344 deletions(-)
Signature · 1 line
2.24.1
PATCH 1/8 nVMX: Eliminate superfluous entry_failure_handler() wrapper
Check and invoke the current entry failure handler directly from vmx_run() to eliminate an unnecessary layer and its stale comment.
--- x86/vmx.c | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-)
x86/vmx.c +3 -16
@@ -1631,21 +1631,6 @@ static int exit_handler(void)return ret; } -/* -* Called if vmlaunch or vmresume fails. -* @early - failure due to "VMX controls and host-state area" (26.2) -* @vmlaunch - was this a vmlaunch or vmresume -* @rflags - host rflags -*/ -static int -entry_failure_handler (struct vmentry_failure *failure) -{ -if (current->entry_failure_handler) -return current->entry_failure_handler (failure); -else -return VMX_TEST_EXIT ; -} -/* * Tries to enter the guest. Returns true if entry succeeded. Otherwise, * populates @failure.
@@ -1704,8 +1689,10 @@ static int vmx_run(void)*/ launched =1 ; ret =exit_handler (); + }else if (current->entry_failure_handler) { + ret = current->entry_failure_handler (failure); }else { - ret =entry_failure_handler (&failure); + ret =VMX_TEST_EXIT ; }switch (ret) {
Signature · 1 line
2.24.1
PATCH 2/8 nVMX: Refactor VM-Entry "failure" struct into "result"
In reply to Sean ChristophersonRename "struct vmentry_failure" to "struct vmentry_result" and add the full VM-Exit reason to the result. Implement exit_reason as a union so that tests can easily pull out the parts of interest, e.g. basic exit reason, whether VM-Entry failed, etc...
--- x86/vmx.c | 128 ++++++++++++++++++++++++++---------------------- x86/vmx.h | 39 ++++++++++++--- x86/vmx_tests.c | 24 ++++----- 3 files changed, 112 insertions(+), 79 deletions(-)
x86/vmx.c +70 -58
@@ -607,11 +607,14 @@ void print_vmexit_info() regs.r12, regs.r13, regs.r14, regs.r15); } -void -print_vmentry_failure_info (struct vmentry_failure *failure) { -if (failure->early) { -printf ("Early %s failure: " , failure->instr); -switch (failure->flags &VMX_ENTRY_FLAGS ) { +void print_vmentry_failure_info (struct vmentry_result *result) +{ +if (result->entered) +return ; + +if (result->vm_fail) { +printf ("VM-Fail on %s: " , result->instr); +switch (result->flags &VMX_ENTRY_FLAGS ) {case X86_EFLAGS_CF :printf ("current-VMCS pointer is not valid.\n" );break ;
@@ -620,16 +623,15 @@ print_vmentry_failure_info(struct vmentry_failure *failure) { vmcs_read(VMX_INST_ERROR));break ;default : -printf ("unexpected flags %lx!\n" , failure->flags); +printf ("unexpected flags %lx!\n" , result->flags); } }else { -u64 reason =vmcs_read (EXI_REASON );u64 qual =vmcs_read (EXI_QUALIFICATION ); -printf ("Non-early %s failure (reason=%#lx, qual=%#lx): " , - failure->instr, reason, qual); +printf ("VM-Exit failure on %s (reason=%#x, qual=%#lx): " , + result->instr, result->exit_reason.full, qual); -switch (reason &0xff ) { +switch (result->exit_reason.basic) {case VMX_FAIL_STATE :printf ("invalid guest state\n" );break ;
@@ -640,14 +642,14 @@ print_vmentry_failure_info(struct vmentry_failure *failure) {printf ("machine-check event\n" );break ;default : -printf ("unexpected basic exit reason %ld\n" , - reason &0xff ); +printf ("unexpected basic exit reason %u\n" , + result->exit_reason.basic); } -if (!(reason &VMX_ENTRY_FAILURE )) +if (!result->exit_reason.failed_vmentry)printf ("\tVMX_ENTRY_FAILURE BIT NOT SET!\n" ); -if (reason &0x7fff0000 ) +if (result->exit_reason.full &0x7fff0000 )printf ("\tRESERVED BITS SET!\n" ); } }
@@ -1632,12 +1634,12 @@ static int exit_handler(void) }/* -* Tries to enter the guest. Returns true if entry succeeded. Otherwise, -* populates @failure. +* Tries to enter the guest, populates @result with VM-Fail, VM-Exit, entered, +* etc... */ -static void vmx_enter_guest (struct vmentry_failure *failure) +static void vmx_enter_guest (struct vmentry_result *result) { - failure->early =0 ; +memset (result,0 ,sizeof (*result)); in_guest =1 ; asm volatile (
@@ -1654,35 +1656,35 @@ static void vmx_enter_guest(struct vmentry_failure *failure)SAVE_GPR_C "pushf\n\t" "pop %%rdi\n\t" -"mov %%rdi, %[failure_flags]\n\t" -"movl $1, %[failure_early]\n\t" +"mov %%rdi, %[vm_fail_flags]\n\t" +"movl $1, %[vm_fail]\n\t" "jmp 3f\n\t" "vmx_return:\n\t" SAVE_GPR_C "3: \n\t" - : [failure_early]"+m"(failure->early), - [failure_flags]"=m"(failure->flags) + : [vm_fail]"+m"(result->vm_fail), + [vm_fail_flags]"=m"(result->flags) : [launched]"m"(launched), [HOST_RSP]"i"(HOST_RSP) : "rdi", "memory", "cc" ); in_guest =0 ; - failure->vmlaunch = !launched; - failure->instr = launched ?"vmresume" :"vmlaunch" ; + result->vmlaunch = !launched; + result->instr = launched ?"vmresume" :"vmlaunch" ; + result->exit_reason.full = result->vm_fail ?0xdead : +vmcs_read (EXI_REASON ); + result->entered = !result->vm_fail && + !result->exit_reason.failed_vmentry; }static int vmx_run (void ) { +struct vmentry_result result; +u32 ret; +while (1 ) { -u32 ret; -bool entered; -struct vmentry_failure failure; - -vmx_enter_guest (&failure); - entered = !failure.early && - !(vmcs_read (EXI_REASON ) &VMX_ENTRY_FAILURE ); - -if (entered) { +vmx_enter_guest (&result); +if (result.entered) {/* * VMCS isn't in "launched" state if there's been any * entry failure (early or otherwise).
@@ -1690,7 +1692,7 @@ static int vmx_run(void) launched =1 ; ret =exit_handler (); }else if (current->entry_failure_handler) { - ret = current->entry_failure_handler (failure); + ret = current->entry_failure_handler (&result); }else { ret =VMX_TEST_EXIT ; }
@@ -1705,15 +1707,15 @@ static int vmx_run(void)break ;default :printf ("ERROR : Invalid %s_handler return val %d.\n" , - entered ?"exit" :"entry_failure" , + result.entered ?"exit" :"entry_failure" , ret);break ; } -if (entered) +if (result.entered)print_vmexit_info ();else -print_vmentry_failure_info (&failure); +print_vmentry_failure_info (&result);abort (); } }
@@ -1845,7 +1847,7 @@ static void check_for_guest_termination(void)* Enters the guest (or launches it for the first time). Error to call once the * guest has returned (i.e., run past the end of its guest() function). */ -static void __enter_guest (u8 abort_flag,struct vmentry_failure *failure) +static void __enter_guest (u8 abort_flag,struct vmentry_result *result) {TEST_ASSERT_MSG (v2_guest_main,"Never called test_set_guest_func!" );
@@ -1853,24 +1855,32 @@ static void __enter_guest(u8 abort_flag, struct vmentry_failure *failure)TEST_ASSERT_MSG (!guest_finished,"Called enter_guest() after guest returned." ); -vmx_enter_guest (failure); -if ((abort_flag &ABORT_ON_EARLY_VMENTRY_FAIL && failure->early) || - (abort_flag &ABORT_ON_INVALID_GUEST_STATE && -vmcs_read (EXI_REASON ) &VMX_ENTRY_FAILURE )) { +vmx_enter_guest (result); -print_vmentry_failure_info (failure); -abort (); +if (result->vm_fail) { +if (abort_flag &ABORT_ON_EARLY_VMENTRY_FAIL ) + goto do_abort; +return ; } - -if (!failure->early && !(vmcs_read (EXI_REASON ) &VMX_ENTRY_FAILURE )) { - launched =1 ; -check_for_guest_termination (); +if (result->exit_reason.failed_vmentry) { +if ((abort_flag &ABORT_ON_INVALID_GUEST_STATE ) || + result->exit_reason.basic !=VMX_FAIL_STATE ) + goto do_abort; +return ; } + + launched =1 ; +check_for_guest_termination (); +return ; + +do_abort: +print_vmentry_failure_info (result); +abort (); }void enter_guest_with_bad_controls (void ) { -struct vmentry_failure failure = {0 }; +struct vmentry_result result;TEST_ASSERT_MSG (v2_guest_main,"Never called test_set_guest_func!" );
@@ -1878,10 +1888,10 @@ void enter_guest_with_bad_controls(void)TEST_ASSERT_MSG (!guest_finished,"Called enter_guest() after guest returned." ); -__enter_guest (ABORT_ON_INVALID_GUEST_STATE , &failure); -report (failure.early,"failure occurred early" ); -report ((failure.flags &VMX_ENTRY_FLAGS ) ==X86_EFLAGS_ZF , -"FLAGS set correctly" ); +__enter_guest (ABORT_ON_INVALID_GUEST_STATE , &result); +report (result.vm_fail,"VM-Fail occurred as expected" ); +report ((result.flags &VMX_ENTRY_FLAGS ) ==X86_EFLAGS_ZF , +"FLAGS set correctly on VM-Fail" );report (vmcs_read (VMX_INST_ERROR ) ==VMXERR_ENTRY_INVALID_CONTROL_FIELD ,"VM-Inst Error # is %d (VM entry with invalid control field(s))" ,VMXERR_ENTRY_INVALID_CONTROL_FIELD );
@@ -1893,23 +1903,23 @@ void enter_guest_with_bad_controls(void)* unexpectedly succeed, it's nice to check whether the guest has * terminated, to reduce the number of error messages. */ -if (!failure.early) +if (!result.vm_fail)check_for_guest_termination (); }void enter_guest (void ) { -struct vmentry_failure failure = {0 }; +struct vmentry_result result;__enter_guest (ABORT_ON_EARLY_VMENTRY_FAIL | -ABORT_ON_INVALID_GUEST_STATE , &failure); +ABORT_ON_INVALID_GUEST_STATE , &result); }void enter_guest_with_invalid_guest_state (void ) { -struct vmentry_failure failure = {0 }; +struct vmentry_result result; -__enter_guest (ABORT_ON_EARLY_VMENTRY_FAIL , &failure); +__enter_guest (ABORT_ON_EARLY_VMENTRY_FAIL , &result); }extern struct vmx_test vmx_tests[];
@@ -1924,6 +1934,8 @@ test_wanted(const char *name, const char *filters[], int filter_count)char *c;const char *n; +printf ("filter = %s, test = %s\n" , filters[0 ], name); +/* Replace spaces with underscores. */ n = name; c = &clean_name[0 ];
x86/vmx.h +32 -7
@@ -44,13 +44,38 @@ struct regs {u64 rflags; }; -struct vmentry_failure { -/* Did a vmlaunch or vmresume fail? */ -bool vmlaunch; +struct vmentry_result {/* Instruction mnemonic (for convenience). */ const char *instr; -/* Did the instruction return right away, or did we jump to HOST_RIP? */ -bool early; +/* Did the test attempt vmlaunch or vmresume? */ +bool vmlaunch; +/* Did the instruction VM-Fail? */ +bool vm_fail; +/* Did the VM-Entry fully enter the guest? */ +bool entered; +/* VM-Exit reason, valid iff !vm_fail */ +union { +struct { +u32 basic :16 ; +u32 reserved16 :1 ; +u32 reserved17 :1 ; +u32 reserved18 :1 ; +u32 reserved19 :1 ; +u32 reserved20 :1 ; +u32 reserved21 :1 ; +u32 reserved22 :1 ; +u32 reserved23 :1 ; +u32 reserved24 :1 ; +u32 reserved25 :1 ; +u32 reserved26 :1 ; +u32 enclave_mode :1 ; +u32 smi_pending_mtf :1 ; +u32 smi_from_vmx_root :1 ; +u32 reserved30 :1 ; +u32 failed_vmentry :1 ; + }; +u32 full; + } exit_reason;/* Contents of [re]flags after failed entry. */ unsigned long flags; };
@@ -62,7 +87,7 @@ struct vmx_test {int (*exit_handler)(void );void (*syscall_handler)(u64 syscall_no);struct regs guest_regs; -int (*entry_failure_handler)(struct vmentry_failure *failure); +int (*entry_failure_handler)(struct vmentry_result *result);struct vmcs *vmcs;int exits;/* Alternative test interface. */
@@ -800,7 +825,7 @@ void init_vmx(u64 *vmxon_region);const char *exit_reason_description (u64 reason);void print_vmexit_info (void ); -void print_vmentry_failure_info (struct vmentry_failure *failure); +void print_vmentry_failure_info (struct vmentry_result *result);void ept_sync (int type,u64 eptp);void vpid_sync (int type,u16 vpid); void install_ept_entry(unsigned long *pml4, int pte_level,
x86/vmx_tests.c +10 -14
@@ -1996,24 +1996,22 @@ static int msr_switch_exit_handler(void)return VMX_TEST_EXIT ; } -static int msr_switch_entry_failure (struct vmentry_failure *failure) +static int msr_switch_entry_failure (struct vmentry_result *result) { -ulong reason; - -if (failure->early) { -printf ("ERROR %s: early exit\n" , __func__); +if (result->vm_fail) { +printf ("ERROR %s: VM-Fail on %s\n" , __func__, result->instr);return VMX_TEST_EXIT ; } - reason =vmcs_read (EXI_REASON ); -if (reason == (VMX_ENTRY_FAILURE |VMX_FAIL_MSR ) && +if (result->exit_reason.failed_vmentry && + result->exit_reason.basic ==VMX_FAIL_MSR &&vmx_get_test_stage () ==3 ) {report (vmcs_read (EXI_QUALIFICATION ) ==1 ,"VM entry MSR load: try to load FS_BASE" );return VMX_TEST_VMEXIT ; } -printf ("ERROR %s: unexpected stage=%u or reason=%lu\n" , - __func__,vmx_get_test_stage (), reason); +printf ("ERROR %s: unexpected stage=%u or reason=%x\n" , + __func__,vmx_get_test_stage (), result->exit_reason.full);return VMX_TEST_EXIT ; }
@@ -9428,12 +9426,10 @@ static int invalid_msr_exit_handler(void)return VMX_TEST_EXIT ; } -static int invalid_msr_entry_failure (struct vmentry_failure *failure) +static int invalid_msr_entry_failure (struct vmentry_result *result) { -ulong reason; - - reason =vmcs_read (EXI_REASON ); -report (reason == (0x80000000u |VMX_FAIL_MSR ),"Invalid MSR load" ); +report (result->exit_reason.failed_vmentry && + result->exit_reason.basic ==VMX_FAIL_MSR ,"Invalid MSR load" );return VMX_TEST_VMEXIT ; }
Signature · 1 line
2.24.1
Re: [kvm-unit-tests PATCH 2/8] nVMX: Refactor VM-Entry "failure" struct into "result"
Quoted 288 lines · Sean Christopherson
Rename "struct
vmentry_failure" to "structvmentry_result" and add the full VM-Exit reason to the result. Implementexit_reasonas a union so that tests can easily pull out the parts of interest, e.g. basic exit reason, whether VM-Entry failed, etc...Signed-off-by Sean Christopherson <[email protected]>--- x86/vmx.c | 128 ++++++++++++++++++++++++++---------------------- x86/vmx.h | 39 ++++++++++++--- x86/vmx_tests.c | 24 ++++----- 3 files changed, 112 insertions(+), 79 deletions(-)x86/vmx.c +70
-58@@ -607,11 +607,14 @@ void print_vmexit_info() regs.r12, regs.r13, regs.r14, regs.r15); } -void -print_vmentry_failure_info (struct vmentry_failure *failure) { -if (failure->early) { -printf ("Early %s failure: " , failure->instr); -switch (failure->flags &VMX_ENTRY_FLAGS ) { +void print_vmentry_failure_info (struct vmentry_result *result) +{ +if (result->entered) +return ; + +if (result->vm_fail) { +printf ("VM-Fail on %s: " , result->instr); +switch (result->flags &VMX_ENTRY_FLAGS ) {case X86_EFLAGS_CF :printf ("current-VMCS pointer is not valid.\n" );break ;@@ -620,16 +623,15 @@ print_vmentry_failure_info(struct vmentry_failure *failure) { vmcs_read(VMX_INST_ERROR));break ;default : -printf ("unexpected flags %lx!\n" , failure->flags); +printf ("unexpected flags %lx!\n" , result->flags); } }else { -u64 reason =vmcs_read (EXI_REASON );u64 qual =vmcs_read (EXI_QUALIFICATION ); -printf ("Non-early %s failure (reason=%#lx, qual=%#lx): " , - failure->instr, reason, qual); +printf ("VM-Exit failure on %s (reason=%#x, qual=%#lx): " , + result->instr, result->exit_reason.full, qual); -switch (reason &0xff ) { +switch (result->exit_reason.basic) {case VMX_FAIL_STATE :printf ("invalid guest state\n" );break ;@@ -640,14 +642,14 @@ print_vmentry_failure_info(struct vmentry_failure *failure) {printf ("machine-check event\n" );break ;default : -printf ("unexpected basic exit reason %ld\n" , - reason &0xff ); +printf ("unexpected basic exit reason %u\n" , + result->exit_reason.basic); } -if (!(reason &VMX_ENTRY_FAILURE )) +if (!result->exit_reason.failed_vmentry)printf ("\tVMX_ENTRY_FAILURE BIT NOT SET!\n" ); -if (reason &0x7fff0000 ) +if (result->exit_reason.full &0x7fff0000 )printf ("\tRESERVED BITS SET!\n" ); } }@@ -1632,12 +1634,12 @@ static int exit_handler(void) }/* -* Tries to enter the guest. Returns true if entry succeeded. Otherwise, -* populates @failure. +* Tries to enter the guest, populates @result with VM-Fail, VM-Exit, entered, +* etc... */ -static void vmx_enter_guest (struct vmentry_failure *failure) +static void vmx_enter_guest (struct vmentry_result *result) { - failure->early =0 ; +memset (result,0 ,sizeof (*result)); in_guest =1 ; asm volatile (@@ -1654,35 +1656,35 @@ static void vmx_enter_guest(struct vmentry_failure *failure)SAVE_GPR_C "pushf\n\t" "pop %%rdi\n\t" -"mov %%rdi, %[failure_flags]\n\t" -"movl $1, %[failure_early]\n\t" +"mov %%rdi, %[vm_fail_flags]\n\t" +"movl $1, %[vm_fail]\n\t" "jmp 3f\n\t" "vmx_return:\n\t" SAVE_GPR_C "3: \n\t" - : [failure_early]"+m"(failure->early), - [failure_flags]"=m"(failure->flags) + : [vm_fail]"+m"(result->vm_fail), + [vm_fail_flags]"=m"(result->flags) : [launched]"m"(launched), [HOST_RSP]"i"(HOST_RSP) : "rdi", "memory", "cc" ); in_guest =0 ; - failure->vmlaunch = !launched; - failure->instr = launched ?"vmresume" :"vmlaunch" ; + result->vmlaunch = !launched; + result->instr = launched ?"vmresume" :"vmlaunch" ; + result->exit_reason.full = result->vm_fail ?0xdead : +vmcs_read (EXI_REASON ); + result->entered = !result->vm_fail && + !result->exit_reason.failed_vmentry; }static int vmx_run (void ) { +struct vmentry_result result; +u32 ret; +while (1 ) { -u32 ret; -bool entered; -struct vmentry_failure failure; - -vmx_enter_guest (&failure); - entered = !failure.early && - !(vmcs_read (EXI_REASON ) &VMX_ENTRY_FAILURE ); - -if (entered) { +vmx_enter_guest (&result); +if (result.entered) {/* * VMCS isn't in "launched" state if there's been any * entry failure (early or otherwise). @@ -1690,7 +1692,7 @@ static int vmx_run(void) launched =1 ; ret =exit_handler (); }else if (current->entry_failure_handler) { - ret = current->entry_failure_handler (failure); + ret = current->entry_failure_handler (&result); }else { ret =VMX_TEST_EXIT ; }@@ -1705,15 +1707,15 @@ static int vmx_run(void)break ;default :printf ("ERROR : Invalid %s_handler return val %d.\n" , - entered ?"exit" :"entry_failure" , + result.entered ?"exit" :"entry_failure" , ret);break ; } -if (entered) +if (result.entered)print_vmexit_info ();else -print_vmentry_failure_info (&failure); +print_vmentry_failure_info (&result);abort (); } }@@ -1845,7 +1847,7 @@ static void check_for_guest_termination(void)* Enters the guest (or launches it for the first time). Error to call once the * guest has returned (i.e., run past the end of its guest() function). */ -static void __enter_guest (u8 abort_flag,struct vmentry_failure *failure) +static void __enter_guest (u8 abort_flag,struct vmentry_result *result) {TEST_ASSERT_MSG (v2_guest_main,"Never called test_set_guest_func!" );@@ -1853,24 +1855,32 @@ static void __enter_guest(u8 abort_flag, struct vmentry_failure *failure)TEST_ASSERT_MSG (!guest_finished,"Called enter_guest() after guest returned." ); -vmx_enter_guest (failure); -if ((abort_flag &ABORT_ON_EARLY_VMENTRY_FAIL && failure->early) || - (abort_flag &ABORT_ON_INVALID_GUEST_STATE && -vmcs_read (EXI_REASON ) &VMX_ENTRY_FAILURE )) { +vmx_enter_guest (result); -print_vmentry_failure_info (failure); -abort (); +if (result->vm_fail) { +if (abort_flag &ABORT_ON_EARLY_VMENTRY_FAIL ) + goto do_abort; +return ; } - -if (!failure->early && !(vmcs_read (EXI_REASON ) &VMX_ENTRY_FAILURE )) { - launched =1 ; -check_for_guest_termination (); +if (result->exit_reason.failed_vmentry) { +if ((abort_flag &ABORT_ON_INVALID_GUEST_STATE ) || + result->exit_reason.basic !=VMX_FAIL_STATE ) + goto do_abort; +return ; } + + launched =1 ; +check_for_guest_termination (); +return ; + +do_abort: +print_vmentry_failure_info (result); +abort (); }void enter_guest_with_bad_controls (void ) { -struct vmentry_failure failure = {0 }; +struct vmentry_result result;TEST_ASSERT_MSG (v2_guest_main,"Never called test_set_guest_func!" );@@ -1878,10 +1888,10 @@ void enter_guest_with_bad_controls(void)TEST_ASSERT_MSG (!guest_finished,"Called enter_guest() after guest returned." ); -__enter_guest (ABORT_ON_INVALID_GUEST_STATE , &failure); -report (failure.early,"failure occurred early" ); -report ((failure.flags &VMX_ENTRY_FLAGS ) ==X86_EFLAGS_ZF , -"FLAGS set correctly" ); +__enter_guest (ABORT_ON_INVALID_GUEST_STATE , &result); +report (result.vm_fail,"VM-Fail occurred as expected" ); +report ((result.flags &VMX_ENTRY_FLAGS ) ==X86_EFLAGS_ZF , +"FLAGS set correctly on VM-Fail" );report (vmcs_read (VMX_INST_ERROR ) ==VMXERR_ENTRY_INVALID_CONTROL_FIELD ,"VM-Inst Error # is %d (VM entry with invalid control field(s))" ,VMXERR_ENTRY_INVALID_CONTROL_FIELD );@@ -1893,23 +1903,23 @@ void enter_guest_with_bad_controls(void)* unexpectedly succeed, it's nice to check whether the guest has * terminated, to reduce the number of error messages. */ -if (!failure.early) +if (!result.vm_fail)check_for_guest_termination (); }void enter_guest (void ) { -struct vmentry_failure failure = {0 }; +struct vmentry_result result;__enter_guest (ABORT_ON_EARLY_VMENTRY_FAIL | -ABORT_ON_INVALID_GUEST_STATE , &failure); +ABORT_ON_INVALID_GUEST_STATE , &result); }void enter_guest_with_invalid_guest_state (void ) { -struct vmentry_failure failure = {0 }; +struct vmentry_result result; -__enter_guest (ABORT_ON_EARLY_VMENTRY_FAIL , &failure); +__enter_guest (ABORT_ON_EARLY_VMENTRY_FAIL , &result); }extern struct vmx_test vmx_tests[];@@ -1924,6 +1934,8 @@ test_wanted(const char *name, const char *filters[], int filter_count)char *c;const char *n; +printf ("filter = %s, test = %s\n" , filters[0 ], name); +/* Replace spaces with underscores. */ n = name; c = &clean_name[0 ];x86/vmx.h +0
-2@@ -44,13 +44,38 @@ struct regs {u64 rflags; }; -struct vmentry_failure { -/* Did a vmlaunch or vmresume fail? */
x86/vmx.h struct regs { · line 53
Quoted 10 lines
x86/vmx.h +5
-3-bool vmlaunch; +struct vmentry_result {/* Instruction mnemonic (for convenience). */ const char *instr; -/* Did the instruction return right away, or did we jump to HOST_RIP? */ -bool early; +/* Did the test attempt vmlaunch or vmresume? */ +bool vmlaunch; +/* Did the instruction VM-Fail? */ +bool vm_fail;
I still like the old name, "failure_early". To me, "vm_fail" and "failed_vmentry" sound similar and confusing.
The SDM calls this type of failure as "early failure" which is denoted by an (instruction) error number, in order to distinguish it from the failure that happens during guest state checking/loading. So, probably a better naming is "vm_early_failure" or "vm_fail_early". Or may be, "vm_instr_error" ?
Quoted 98 lines
x86/vmx.h +27
-2+/* Did the VM-Entry fully enter the guest? */ +bool entered; +/* VM-Exit reason, valid iff !vm_fail */ +union { +struct { +u32 basic :16 ; +u32 reserved16 :1 ; +u32 reserved17 :1 ; +u32 reserved18 :1 ; +u32 reserved19 :1 ; +u32 reserved20 :1 ; +u32 reserved21 :1 ; +u32 reserved22 :1 ; +u32 reserved23 :1 ; +u32 reserved24 :1 ; +u32 reserved25 :1 ; +u32 reserved26 :1 ; +u32 enclave_mode :1 ; +u32 smi_pending_mtf :1 ; +u32 smi_from_vmx_root :1 ; +u32 reserved30 :1 ; +u32 failed_vmentry :1 ; + }; +u32 full; + } exit_reason;/* Contents of [re]flags after failed entry. */ unsigned long flags; };@@ -62,7 +87,7 @@ struct vmx_test {int (*exit_handler)(void );void (*syscall_handler)(u64 syscall_no);struct regs guest_regs; -int (*entry_failure_handler)(struct vmentry_failure *failure); +int (*entry_failure_handler)(struct vmentry_result *result);struct vmcs *vmcs;int exits;/* Alternative test interface. */ @@ -800,7 +825,7 @@ void init_vmx(u64 *vmxon_region);const char *exit_reason_description (u64 reason);void print_vmexit_info (void ); -void print_vmentry_failure_info (struct vmentry_failure *failure); +void print_vmentry_failure_info (struct vmentry_result *result);void ept_sync (int type,u64 eptp);void vpid_sync (int type,u16 vpid); void install_ept_entry(unsigned long *pml4, int pte_level,x86/vmx_tests.c +10
-14@@ -1996,24 +1996,22 @@ static int msr_switch_exit_handler(void)return VMX_TEST_EXIT ; } -static int msr_switch_entry_failure (struct vmentry_failure *failure) +static int msr_switch_entry_failure (struct vmentry_result *result) { -ulong reason; - -if (failure->early) { -printf ("ERROR %s: early exit\n" , __func__); +if (result->vm_fail) { +printf ("ERROR %s: VM-Fail on %s\n" , __func__, result->instr);return VMX_TEST_EXIT ; } - reason =vmcs_read (EXI_REASON ); -if (reason == (VMX_ENTRY_FAILURE |VMX_FAIL_MSR ) && +if (result->exit_reason.failed_vmentry && + result->exit_reason.basic ==VMX_FAIL_MSR &&vmx_get_test_stage () ==3 ) {report (vmcs_read (EXI_QUALIFICATION ) ==1 ,"VM entry MSR load: try to load FS_BASE" );return VMX_TEST_VMEXIT ; } -printf ("ERROR %s: unexpected stage=%u or reason=%lu\n" , - __func__,vmx_get_test_stage (), reason); +printf ("ERROR %s: unexpected stage=%u or reason=%x\n" , + __func__,vmx_get_test_stage (), result->exit_reason.full);return VMX_TEST_EXIT ; }@@ -9428,12 +9426,10 @@ static int invalid_msr_exit_handler(void)return VMX_TEST_EXIT ; } -static int invalid_msr_entry_failure (struct vmentry_failure *failure) +static int invalid_msr_entry_failure (struct vmentry_result *result) { -ulong reason; - - reason =vmcs_read (EXI_REASON ); -report (reason == (0x80000000u |VMX_FAIL_MSR ),"Invalid MSR load" ); +report (result->exit_reason.failed_vmentry && + result->exit_reason.basic ==VMX_FAIL_MSR ,"Invalid MSR load" );return VMX_TEST_VMEXIT ; }
Paolo Bonzini
maintainerRe: [kvm-unit-tests PATCH 2/8] nVMX: Refactor VM-Entry "failure" struct into "result"
Quoted 7 lines · Krish Sadhukhan
Quoted 5 lines
+ /* Did the test attempt vmlaunch or vmresume? */ + bool vmlaunch;
+ /* Did the instruction VM-Fail? */
+ boolvm_fail;
Quoted 8 lines
I still like the old name, "
failure_early". To me, "vm_fail" and "failed_vmentry" sound similar and confusing.The SDM calls this type of failure as "early failure" which is denoted by an (instruction) error number, in order to distinguish it from the failure that happens during guest state checking/loading. So, probably a better naming is "
vm_early_failure" or "vm_fail_early". Or may be, "vm_instr_error" ?
This is actually what the SDM calls VMfailValid (we should never get to VMfailInvalid in these tests), so vm_fail is appropriate.
failed_vmentry is what the SDM calls "VM-entry failure".
I agree that the names are similar and confusing, but there's some value in keeping them close to the SDM.
Paolo
PATCH 3/8 nVMX: Consolidate non-canonical code in test_canonical()
In reply to Sean ChristophersonRefactor test_canonical() to provide a single flow for the non-canonical path. Practically speaking, its extremely unlikely the field being tested already has a non-canonical address, and even less likely that it's anything other than NONCANONICAL. I.e. the added complexity doesn't come with added coverage.
--- x86/vmx_tests.c | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-)
x86/vmx_tests.c +17 -23
@@ -7411,6 +7411,11 @@ static void test_canonical(u64 field, const char * field_name, bool host) {u64 addr_saved =vmcs_read (field); +/* +* Use the existing value if possible. Writing a random canonical +* value is not an option as doing so would corrupt the field being +* tested and likely hose the test. +*/ if (is_canonical (addr_saved)) {if (host) {report_prefix_pushf ("%s %lx" , field_name, addr_saved);
@@ -7422,33 +7427,22 @@ static void test_canonical(u64 field, const char * field_name, bool host)VMX_VMCALL , addr_saved, field_name); } + } -vmcs_write (field,NONCANONICAL ); +vmcs_write (field,NONCANONICAL ); -if (host) { -report_prefix_pushf ("%s %llx" , field_name,NONCANONICAL ); -test_vmx_vmlaunch (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD ); -report_prefix_pop (); - }else { -enter_guest_with_invalid_guest_state (); -report_guest_state_test ("Test canonical address" , -VMX_FAIL_STATE |VMX_ENTRY_FAILURE , -NONCANONICAL , field_name); - } - -vmcs_write (field, addr_saved); +if (host) { +report_prefix_pushf ("%s %llx" , field_name,NONCANONICAL ); +test_vmx_vmlaunch (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD ); +report_prefix_pop (); }else { -if (host) { -report_prefix_pushf ("%s %llx" , field_name,NONCANONICAL ); -test_vmx_vmlaunch (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD ); -report_prefix_pop (); - }else { -enter_guest_with_invalid_guest_state (); -report_guest_state_test ("Test canonical address" , -VMX_FAIL_STATE |VMX_ENTRY_FAILURE , -NONCANONICAL , field_name); - } +enter_guest_with_invalid_guest_state (); +report_guest_state_test ("Test non-canonical address" , +VMX_FAIL_STATE |VMX_ENTRY_FAILURE , +NONCANONICAL , field_name); } + +vmcs_write (field, addr_saved); }#define TEST_RPL_TI_FLAGS (reg, name) \
Signature · 1 line
2.24.1
PATCH 4/8 nVMX: Drop redundant check for guest termination
In reply to Sean ChristophersonRemove the check_for_guest_termination() call in enter_guest_with_bad_controls() as __enter_guest() unconditionally performs the check if VM-Enter is successful (and aborts on failed VM-Entry for the ...bad_controls() variant).
--- x86/vmx.c | 10 ---------- 1 file changed, 10 deletions(-)
x86/vmx.c +0 -10
@@ -1895,16 +1895,6 @@ void enter_guest_with_bad_controls(void)report (vmcs_read (VMX_INST_ERROR ) ==VMXERR_ENTRY_INVALID_CONTROL_FIELD ,"VM-Inst Error # is %d (VM entry with invalid control field(s))" ,VMXERR_ENTRY_INVALID_CONTROL_FIELD ); - -/* -* This if statement shouldn't fire, as the entire premise of this -* function is that VM entry is expected to fail, rather than succeed -* and execute to termination. However, if the VM entry does -* unexpectedly succeed, it's nice to check whether the guest has -* terminated, to reduce the number of error messages. -*/ -if (!result.vm_fail) -check_for_guest_termination (); }void enter_guest (void )
Signature · 1 line
2.24.1
PATCH 5/8 nVMX: Expose __enter_guest() and consolidate guest state test code
In reply to Sean ChristophersonExpose __enter_guest() outside of vmx.c and use it in a new wrapper for testing guest state. Handling both success and failure paths in a common helper eliminates a lot of boilerplate code in the tests themselves.
--- x86/vmx.c | 12 +---- x86/vmx.h | 5 ++- x86/vmx_tests.c | 115 +++++++++++++++--------------------------------- 3 files changed, 40 insertions(+), 92 deletions(-)
x86/vmx.c +1 -11
@@ -1840,14 +1840,11 @@ static void check_for_guest_termination(void) } } -#define ABORT_ON_EARLY_VMENTRY_FAIL 0x1 -#define ABORT_ON_INVALID_GUEST_STATE 0x2 -/* * Enters the guest (or launches it for the first time). Error to call once the * guest has returned (i.e., run past the end of its guest() function). */ -static void __enter_guest (u8 abort_flag,struct vmentry_result *result) +void __enter_guest (u8 abort_flag,struct vmentry_result *result) {TEST_ASSERT_MSG (v2_guest_main,"Never called test_set_guest_func!" );
@@ -1905,13 +1902,6 @@ void enter_guest(void) ABORT_ON_INVALID_GUEST_STATE, &result); } -void enter_guest_with_invalid_guest_state (void ) -{ -struct vmentry_result result; - -__enter_guest (ABORT_ON_EARLY_VMENTRY_FAIL , &result); -} -extern struct vmx_test vmx_tests[]; static bool
x86/vmx.h +4 -1
@@ -855,9 +855,12 @@ bool ept_huge_pages_supported(int level);bool ept_execute_only_supported (void );bool ept_ad_bits_supported (void ); +#define ABORT_ON_EARLY_VMENTRY_FAIL 0x1 +#define ABORT_ON_INVALID_GUEST_STATE 0x2 + +void __enter_guest (u8 abort_flag,struct vmentry_result *result);void enter_guest (void );void enter_guest_with_bad_controls (void ); -void enter_guest_with_invalid_guest_state (void );typedef void (*test_guest_func )(void );typedef void (*test_teardown_func )(void *data);
x86/vmx_tests.c +35 -80
180 lines not on this page · raw message
Signature · 1 line
2.24.1
PATCH 6/8 nVMX: Pass exit reason union to v1 exit handlers
In reply to Sean ChristophersonPass the recently introduce "union exit_reason" to the v1 exit handlers and use it in lieu of a manual VMREAD of the exit reason.
Opportunistically fix a variety of warts in the handlers, e.g. grabbing only bits 7:0 of the exit reason. Modify the "Unknown exit reason" prints to display the exit reason in hex format to make a failed VM-Entry more recognizable.
--- x86/vmx.c | 6 +- x86/vmx.h | 48 ++++++++-------- x86/vmx_tests.c | 149 +++++++++++++++++++----------------------------- 3 files changed, 88 insertions(+), 115 deletions(-)
x86/vmx.c +3 -3
@@ -1618,7 +1618,7 @@ void test_skip(const char *msg)abort (); } -static int exit_handler (void ) +static int exit_handler (union exit_reason exit_reason) {int ret;
@@ -1627,7 +1627,7 @@ static int exit_handler(void)if (is_hypercall ()) ret =handle_hypercall ();else - ret = current->exit_handler (); + ret = current->exit_handler (exit_reason);vmcs_write (GUEST_RFLAGS , regs.rflags);return ret;
@@ -1690,7 +1690,7 @@ static int vmx_run(void)* entry failure (early or otherwise). */ launched =1 ; - ret =exit_handler (); + ret =exit_handler (result.exit_reason); }else if (current->entry_failure_handler) { ret = current->entry_failure_handler (&result); }else {
x86/vmx.h +25 -23
66 lines not on this page · raw message
x86/vmx_tests.c +60 -89
457 lines not on this page · raw message
Signature · 1 line
2.24.1
PATCH 7/8 nVMX: Pass exit reason union to is_hypercall()
In reply to Sean ChristophersonPass the exit reason captured in VM-Entry results into __enter_guest()'s helpers to simplify code and eliminate extra VMREADS.
--- x86/vmx.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-)
x86/vmx.c +7 -12
@@ -1553,15 +1553,10 @@ static void __attribute__((__used__)) hypercall(u32 hypercall_no) asmvolatile ("vmcall\n\t" ); } -static bool is_hypercall (void ) +static bool is_hypercall (union exit_reason exit_reason) { -ulong reason, hyper_bit; - - reason =vmcs_read (EXI_REASON ) &0xff ; - hyper_bit = hypercall_field &HYPERCALL_BIT ; -if (reason ==VMX_VMCALL && hyper_bit) -return true; -return false; +return exit_reason.basic ==VMX_VMCALL && + (hypercall_field &HYPERCALL_BIT ); }static int handle_hypercall (void )
@@ -1624,7 +1619,7 @@ static int exit_handler(union exit_reason exit_reason) current->exits++; regs.rflags =vmcs_read (GUEST_RFLAGS ); -if (is_hypercall ()) +if (is_hypercall (exit_reason)) ret =handle_hypercall ();else ret = current->exit_handler (exit_reason);
@@ -1816,9 +1811,9 @@ void test_set_guest(test_guest_func func) v2_guest_main = func; } -static void check_for_guest_termination (void ) +static void check_for_guest_termination (union exit_reason exit_reason) { -if (is_hypercall ()) { +if (is_hypercall (exit_reason)) {int ret; ret =handle_hypercall ();
@@ -1867,7 +1862,7 @@ void __enter_guest(u8 abort_flag, struct vmentry_result *result) } launched =1 ; -check_for_guest_termination (); +check_for_guest_termination (result->exit_reason);return ; do_abort:
Signature · 1 line
2.24.1
PATCH 8/8 nVMX: Pass exit reason enum to print_vmexit_info()
In reply to Sean ChristophersonTake the exit reason as a parameter when printing VM-Exit info instead of rereading it from the VMCS. Opportunistically clean up the related printing.
--- x86/vmx.c | 9 ++++----- x86/vmx.h | 2 +- x86/vmx_tests.c | 46 +++++++++++++++++++++++----------------------- 3 files changed, 28 insertions(+), 29 deletions(-)
x86/vmx.c +4 -5
@@ -585,17 +585,16 @@ const char *exit_reason_description(u64 reason)return exit_reason_descriptions[reason] ? :"(unused)" ; } -void print_vmexit_info () +void print_vmexit_info (union exit_reason exit_reason) {u64 guest_rip, guest_rsp; -ulong reason =vmcs_read (EXI_REASON ) &0xff ;ulong exit_qual =vmcs_read (EXI_QUALIFICATION ); guest_rip =vmcs_read (GUEST_RIP ); guest_rsp =vmcs_read (GUEST_RSP );printf ("VMEXIT info:\n" ); -printf ("\tvmexit reason = %ld\n" , reason); +printf ("\tvmexit reason = %u\n" , exit_reason.basic); +printf ("\tfailed vmentry = %u\n" , !!exit_reason.failed_vmentry);printf ("\texit qualification = %#lx\n" , exit_qual); -printf ("\tBit 31 of reason = %lx\n" , (vmcs_read (EXI_REASON ) >>31 ) &1 );printf ("\tguest_rip = %#lx\n" , guest_rip);printf ("\tRAX=%#lx RBX=%#lx RCX=%#lx RDX=%#lx\n" , regs.rax, regs.rbx, regs.rcx, regs.rdx);
@@ -1708,7 +1707,7 @@ static int vmx_run(void) }if (result.entered) -print_vmexit_info (); +print_vmexit_info (result.exit_reason);else print_vmentry_failure_info (&result);abort ();
x86/vmx.h +1 -1
@@ -826,7 +826,7 @@ void enable_vmx(void);void init_vmx (u64 *vmxon_region);const char *exit_reason_description (u64 reason); -void print_vmexit_info (void ); +void print_vmexit_info (union exit_reason exit_reason);void print_vmentry_failure_info (struct vmentry_result *result);void ept_sync (int type,u64 eptp);void vpid_sync (int type,u16 vpid);
x86/vmx_tests.c +23 -23
182 lines not on this page · raw message
Signature · 1 line
2.24.1
Quoted 20 lines · Sean Christopherson
Start chipping away at the crustiness in the nVMX tests by refactoring "struct
vmentry_failure" into "structvmentry_result", with the full VM-Exit stored invmentry_result. Capturing the exit reason allows for a variety of cleanups and consolidations.This series really only dives into the v1 tests. I'd like to also clean up the v2 tests, e.g. take the expected exit reason in
enter_guest()so that the expected behavior is more obvious, but that's a more invasive cleanup for another day.Sean Christopherson (8):
nVMX: Eliminate superfluousentry_failure_handler()wrapper
nVMX: Refactor VM-Entry "failure" struct into "result"
nVMX: Consolidate non-canonical code intest_canonical()
nVMX: Drop redundant check for guest termination
nVMX: Expose__enter_guest()and consolidate guest state test code
nVMX: Pass exit reason union to v1 exit handlers
nVMX: Pass exit reason union tois_hypercall()
nVMX: Pass exit reason enum toprint_vmexit_info()
Quoted 5 lines
x86/vmx.c | 191 +++++++++++-------------- x86/vmx.h | 50 +++++-- x86/vmx_tests.c | 366 +++++++++++++++++++----------------------------- 3 files changed, 263 insertions(+), 344 deletions(-)
Queued, thanks.
Paolo