nVMX: Fix garbage "host addr size" tests
Sean Christopherson
reviewercover nVMX: Fix garbage "host addr size" tests
This started out as a "simple" fix for vCPUs without PCID, which thanks to a bug on Alderlake/Raptorlake, are suddenly quite prevelant. Then I got the bright idea to tidy up a bit, and lo and behold, test_host_addr_size() is full of garbage.
Patches 2 and 3 are cleanups to reduce indentation and help detect bogus setups.
Patch 4 renames vmlaunch_succeeds(), which has bugged me forever, and would get especially weird when the last patch adds a version that _must_ fail.
Patches 5-7 fix the HOST_RIP testcases. Unsurpisingly, vmlaunch() writes HOST_RIP to ensure the resulting VM-Exit doesn't go into the weeds, which means the tests that write HOST_RIP and expect meaningful test coverage are completely useless. Patch 5 deliberately introduces a test failure by exposing the underlying bugs related to the above.
I could have ordered the patches to not introduce the bug, but I'm more than a bit annoyed at the moment, and I want to highlight the issue, as not restoring/sanitizing state is a massive problem throughout KUT, and the nVMX tests in particular are a horrible mess in this regard.
Patch 6 rips out the HOST_RIP tests that provide no meaningful coverage and have no hope of passing.
Patch 7 fixes the noncanonical HOST_RIP testcase by using a bare VMLAUNCH.
Sean Christopherson (7):
nVMX: Test CR4.PCIDE can be set for 64-bit host iff PCID is supported
nVMX: Assert CR4.PAE is set when testing 64-bit host
nVMX: Assert that the test is configured for 64-bit mode
nVMX: Rename vmlaunch_succeeds() to vmlaunch()
nVMX: Shuffle test_host_addr_size() tests to "restore" CR4 and RIP
nVMX: Drop testcase that falsely claims to verify vmcs.HOST_RIP[63:32]
nVMX: Fix the noncanonical HOST_RIP testcase
x86/vmx_tests.c | 122 ++++++++++++++++++++++++++++-------------------- 1 file changed, 71 insertions(+), 51 deletions(-)
base-commit: e8f8554f810821e37f05112a46ae9775a029b5d1
Signature · 1 line
2.42.0.283.g2d96d420d3-goog
Sean Christopherson
reviewerPATCH 1/7 nVMX: Test CR4.PCIDE can be set for 64-bit host iff PCID is supported
Check that PCID is actually supported before verifying that it can be set in vmcs.HOST_CR4 for 64-bit hosts. PCID is all but ubiquitous on modern CPUs, but PCID may be hidden by the host to workaround a TLB flushing bug on Alderlake and Raptorlake.
--- x86/vmx_tests.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
x86/vmx_tests.c +6 -4
@@ -7625,10 +7625,12 @@ static void test_host_addr_size(void)test_vmx_vmlaunch (0 );report_prefix_pop (); -vmcs_write (HOST_CR4 , cr4_saved |X86_CR4_PCIDE ); -report_prefix_pushf ("\"CR4.PCIDE\" set" ); -test_vmx_vmlaunch (0 ); -report_prefix_pop (); +if (this_cpu_has (X86_FEATURE_PCID )) { +vmcs_write (HOST_CR4 , cr4_saved |X86_CR4_PCIDE ); +report_prefix_pushf ("\"CR4.PCIDE\" set" ); +test_vmx_vmlaunch (0 ); +report_prefix_pop (); + }for (i =32 ; i <=63 ; i = i +4 ) { tmp = rip_saved |1ull << i;
Signature · 1 line
2.42.0.283.g2d96d420d3-goog
Sean Christopherson
reviewerPATCH 2/7 nVMX: Assert CR4.PAE is set when testing 64-bit host
In reply to Sean ChristophersonAssert that PAE is set in the original CR4 value when the host is setup to VM-Exit to 64-bit mode in test_host_addr_size(). If CR4.PAE isn't set then something is wildly broken and all bets are off as VM-Enter can't possibly succeed.
--- x86/vmx_tests.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
x86/vmx_tests.c +6 -8
@@ -7620,6 +7620,8 @@ static void test_host_addr_size(void)u64 tmp;if (vmcs_read (EXI_CONTROLS ) &EXI_HOST_64 ) { +assert (cr4_saved &X86_CR4_PAE ); +vmcs_write (ENT_CONTROLS , entry_ctrl_saved |ENT_GUEST_64 );report_prefix_pushf ("\"IA-32e mode guest\" enabled" );test_vmx_vmlaunch (0 );
@@ -7640,14 +7642,10 @@ static void test_host_addr_size(void)report_prefix_pop (); } -if (cr4_saved &X86_CR4_PAE ) { -vmcs_write (HOST_CR4 , cr4_saved & ~X86_CR4_PAE ); -report_prefix_pushf ("\"CR4.PAE\" unset" ); -test_vmx_vmlaunch (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD ); - }else { -report_prefix_pushf ("\"CR4.PAE\" set" ); -test_vmx_vmlaunch (0 ); - } +vmcs_write (HOST_CR4 , cr4_saved & ~X86_CR4_PAE ); +report_prefix_pushf ("\"CR4.PAE\" unset" ); +test_vmx_vmlaunch (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD ); +vmcs_write (HOST_CR4 , cr4_saved);report_prefix_pop ();vmcs_write (HOST_RIP ,NONCANONICAL );
Signature · 1 line
2.42.0.283.g2d96d420d3-goog
Sean Christopherson
reviewerPATCH 3/7 nVMX: Assert that the test is configured for 64-bit mode
In reply to Sean ChristophersonThe nVMX tests are 64-bit only, assert as much to reduce the probability of the tests not actually running due to some funky bug. It's easy to re-add an if-statement if the nVMX tests ever gain 32-bit support, whereas most developers lack the psychic powers necessary to detect that a test isn't doing anything useful.
--- x86/vmx_tests.c | 69 ++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 35 deletions(-)
x86/vmx_tests.c +34 -35
@@ -7619,49 +7619,48 @@ static void test_host_addr_size(void)int i;u64 tmp; -if (vmcs_read (EXI_CONTROLS ) &EXI_HOST_64 ) { -assert (cr4_saved &X86_CR4_PAE ); +assert (vmcs_read (EXI_CONTROLS ) &EXI_HOST_64 ); +assert (cr4_saved &X86_CR4_PAE ); -vmcs_write (ENT_CONTROLS , entry_ctrl_saved |ENT_GUEST_64 ); -report_prefix_pushf ("\"IA-32e mode guest\" enabled" ); +vmcs_write (ENT_CONTROLS , entry_ctrl_saved |ENT_GUEST_64 ); +report_prefix_pushf ("\"IA-32e mode guest\" enabled" ); +test_vmx_vmlaunch (0 ); +report_prefix_pop (); + +if (this_cpu_has (X86_FEATURE_PCID )) { +vmcs_write (HOST_CR4 , cr4_saved |X86_CR4_PCIDE ); +report_prefix_pushf ("\"CR4.PCIDE\" set" );test_vmx_vmlaunch (0 );report_prefix_pop (); + } -if (this_cpu_has (X86_FEATURE_PCID )) { -vmcs_write (HOST_CR4 , cr4_saved |X86_CR4_PCIDE ); -report_prefix_pushf ("\"CR4.PCIDE\" set" ); -test_vmx_vmlaunch (0 ); -report_prefix_pop (); - } - -for (i =32 ; i <=63 ; i = i +4 ) { - tmp = rip_saved |1ull << i; -vmcs_write (HOST_RIP , tmp); -report_prefix_pushf ("HOST_RIP %lx" , tmp); -test_vmx_vmlaunch (0 ); -report_prefix_pop (); - } - -vmcs_write (HOST_CR4 , cr4_saved & ~X86_CR4_PAE ); -report_prefix_pushf ("\"CR4.PAE\" unset" ); -test_vmx_vmlaunch (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD ); -vmcs_write (HOST_CR4 , cr4_saved); +for (i =32 ; i <=63 ; i = i +4 ) { + tmp = rip_saved |1ull << i; +vmcs_write (HOST_RIP , tmp); +report_prefix_pushf ("HOST_RIP %lx" , tmp); +test_vmx_vmlaunch (0 );report_prefix_pop (); + } -vmcs_write (HOST_RIP ,NONCANONICAL ); -report_prefix_pushf ("HOST_RIP %llx" ,NONCANONICAL ); -test_vmx_vmlaunch (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD ); -report_prefix_pop (); +vmcs_write (HOST_CR4 , cr4_saved & ~X86_CR4_PAE ); +report_prefix_pushf ("\"CR4.PAE\" unset" ); +test_vmx_vmlaunch (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD ); +vmcs_write (HOST_CR4 , cr4_saved); +report_prefix_pop (); -vmcs_write (ENT_CONTROLS , entry_ctrl_saved |ENT_GUEST_64 ); -vmcs_write (HOST_RIP , rip_saved); -vmcs_write (HOST_CR4 , cr4_saved); +vmcs_write (HOST_RIP ,NONCANONICAL ); +report_prefix_pushf ("HOST_RIP %llx" ,NONCANONICAL ); +test_vmx_vmlaunch (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD ); +report_prefix_pop (); -/* Restore host's active RIP and CR4 values. */ -report_prefix_pushf ("restore host state" ); -test_vmx_vmlaunch (0 ); -report_prefix_pop (); - } +vmcs_write (ENT_CONTROLS , entry_ctrl_saved |ENT_GUEST_64 ); +vmcs_write (HOST_RIP , rip_saved); +vmcs_write (HOST_CR4 , cr4_saved); + +/* Restore host's active RIP and CR4 values. */ +report_prefix_pushf ("restore host state" ); +test_vmx_vmlaunch (0 ); +report_prefix_pop (); }/*
Signature · 1 line
2.42.0.283.g2d96d420d3-goog
Sean Christopherson
reviewerPATCH 4/7 nVMX: Rename vmlaunch_succeeds() to vmlaunch()
In reply to Sean ChristophersonRename vmlaunch_succeeds() to just vmlaunch(), the "succeeds" postfix is misleading for any test that expects VMLAUNCH to _fail_ as it gives the false impression that the helper expects VMLAUNCH to succeed.
--- x86/vmx_tests.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
x86/vmx_tests.c +5 -5
@@ -3290,7 +3290,7 @@ static void invvpid_test(void)* VMLAUNCH fails early and execution falls through to the next * instruction. */ -static bool vmlaunch_succeeds (void ) +static bool vmlaunch (void ) {u32 exit_reason;
@@ -3320,7 +3320,7 @@ success:*/ static void test_vmx_vmlaunch (u32 xerror) { -bool success =vmlaunch_succeeds (); +bool success =vmlaunch ();u32 vmx_inst_err; report(success == !xerror, "vmlaunch %s",
@@ -3339,7 +3339,7 @@ static void test_vmx_vmlaunch(u32 xerror)*/ static void test_vmx_vmlaunch2 (u32 xerror1,u32 xerror2) { -bool success =vmlaunch_succeeds (); +bool success =vmlaunch ();u32 vmx_inst_err;if (!xerror1 == !xerror2)
@@ -3487,7 +3487,7 @@ static void test_secondary_processor_based_ctls(void)*/ vmcs_write (CPU_EXEC_CTRL0 , primary & ~CPU_SECONDARY );vmcs_write (CPU_EXEC_CTRL1 , ~0 ); -report (vmlaunch_succeeds (), +report (vmlaunch (),"Secondary processor-based controls ignored" );vmcs_write (CPU_EXEC_CTRL1 , secondary);vmcs_write (CPU_EXEC_CTRL0 , primary);
@@ -7320,7 +7320,7 @@ static void test_pgc_vmlaunch(u32 xerror, u32 xreason, bool xfail, bool host)struct vmx_state_area_test_data *data = &vmx_state_area_test_data;if (host) { - success =vmlaunch_succeeds (); + success =vmlaunch (); obs =rdmsr (data->msr);if (!success) { inst_err =vmcs_read (VMX_INST_ERROR );
Signature · 1 line
2.42.0.283.g2d96d420d3-goog
Sean Christopherson
reviewerPATCH 5/7 nVMX: Shuffle test_host_addr_size() tests to "restore" CR4 and RIP
In reply to Sean ChristophersonRe-order the testcases in test_host_addr_size() to guarantee that the host CR4 and RIP values are either written or restored before each testcase. If a test fails unexpectedly, running with a test value from a previous testcase may cause all subsequent tests to also fail, e.g. if the CR4 PCIDE test fails, all of the RIP tests will fail because of the bad CR4.
This also "fixes" the noncanonical RIP testcase, as running with the bad CR4 setup by the !PAE testcase would mask a missed noncanonical check.
[sean: Surprise! The bad CR4 is indeed masking a bug. I'm leaving it for
now and intentionally creating a failing testcase for a commit or two to highlight the importance of cleaning up after testcases, and isolating what is actually being tested.]
--- x86/vmx_tests.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)
x86/vmx_tests.c +17 -7
@@ -7634,6 +7634,12 @@ static void test_host_addr_size(void)report_prefix_pop (); } +vmcs_write (HOST_CR4 , cr4_saved & ~X86_CR4_PAE ); +report_prefix_pushf ("\"CR4.PAE\" unset" ); +test_vmx_vmlaunch (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD ); +vmcs_write (HOST_CR4 , cr4_saved); +report_prefix_pop (); +for (i =32 ; i <=63 ; i = i +4 ) { tmp = rip_saved |1ull << i;vmcs_write (HOST_RIP , tmp);
@@ -7642,12 +7648,6 @@ static void test_host_addr_size(void)report_prefix_pop (); } -vmcs_write (HOST_CR4 , cr4_saved & ~X86_CR4_PAE ); -report_prefix_pushf ("\"CR4.PAE\" unset" ); -test_vmx_vmlaunch (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD ); -vmcs_write (HOST_CR4 , cr4_saved); -report_prefix_pop (); -vmcs_write (HOST_RIP ,NONCANONICAL );report_prefix_pushf ("HOST_RIP %llx" ,NONCANONICAL );test_vmx_vmlaunch (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD );
@@ -7657,7 +7657,17 @@ static void test_host_addr_size(void)vmcs_write (HOST_RIP , rip_saved);vmcs_write (HOST_CR4 , cr4_saved); -/* Restore host's active RIP and CR4 values. */ +/* +* Restore host's active CR4 and RIP values by triggering a VM-Exit. +* The original CR4 and RIP values in the VMCS are restored between +* testcases as needed, but don't guarantee a VM-Exit and so the active +* CR4 and RIP may still hold a test value. Running with the test CR4 +* and RIP values at some point is unavoidable, and the active values +* are unlikely to affect VM-Enter, so the above doen't force a VM-Exit +* between testcases. Note, if VM-Enter is surrounded by CALL+RET then +* the active RIP will already be restored, but that's also not +* guaranteed, and CR4 needs to be restored regardless. +*/ report_prefix_pushf ("restore host state" );test_vmx_vmlaunch (0 );report_prefix_pop ();
Signature · 1 line
2.42.0.283.g2d96d420d3-goog
Sean Christopherson
reviewerPATCH 6/7 nVMX: Drop testcase that falsely claims to verify vmcs.HOST_RIP[63:32]
In reply to Sean ChristophersonExcise the completely bogus testcase in test_host_addr_size() which purports to verify that setting vmcs.HOST_RIP[63:32] to non-zero values is allowed for 64-bit hosts. The testcase is mindbogglingly broken: setting arbitrary, single bits above bit 46 creates a noncanonical address, and setting arbitrary bits below bit 47 would send the test into the weeds as a "successful" VMLAUNCH generates a VM-Exit, i.e. would load the garbage RIP and immediately encounter a #PF.
The only reason the passes is because it does absolutely nothing useful:vmlaunch() unconditionally writes HOST_RIP before VMLAUNCH, because not jumping to a random RIP on a VM-Exit is mildly important.
Outright drop the testcase, trying to salvage anything from it would be a waste of time as simply running any 64-bit guest will generate a huge variety of RIPs with non-zero values in bits 63:32.
--- x86/vmx_tests.c | 10 ---------- 1 file changed, 10 deletions(-)
x86/vmx_tests.c +0 -10
@@ -7616,8 +7616,6 @@ static void test_host_addr_size(void)u64 cr4_saved =vmcs_read (HOST_CR4 );u64 rip_saved =vmcs_read (HOST_RIP );u64 entry_ctrl_saved =vmcs_read (ENT_CONTROLS ); -int i; -u64 tmp;assert (vmcs_read (EXI_CONTROLS ) &EXI_HOST_64 );assert (cr4_saved &X86_CR4_PAE );
@@ -7640,14 +7638,6 @@ static void test_host_addr_size(void)vmcs_write (HOST_CR4 , cr4_saved);report_prefix_pop (); -for (i =32 ; i <=63 ; i = i +4 ) { - tmp = rip_saved |1ull << i; -vmcs_write (HOST_RIP , tmp); -report_prefix_pushf ("HOST_RIP %lx" , tmp); -test_vmx_vmlaunch (0 ); -report_prefix_pop (); - } -vmcs_write (HOST_RIP ,NONCANONICAL );report_prefix_pushf ("HOST_RIP %llx" ,NONCANONICAL );test_vmx_vmlaunch (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD );
Signature · 1 line
2.42.0.283.g2d96d420d3-goog
Sean Christopherson
reviewerPATCH 7/7 nVMX: Fix the noncanonical HOST_RIP testcase
In reply to Sean ChristophersonDo a bare VMLAUNCH for the noncanonical HOST_RIP testcase to actually test the noncanonical RIP instead of the sane value written by vmlaunch(). Put up a variety of warnings around test_vmx_vmlaunch_must_fail() to discourage improper usage.
--- x86/vmx_tests.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-)
x86/vmx_tests.c +29 -8
@@ -3284,6 +3284,32 @@ static void invvpid_test(void)invvpid_test_not_in_vmx_operation (); } +static void test_assert_vmlaunch_inst_error (u32 expected_error) +{ +u32 vmx_inst_err =vmcs_read (VMX_INST_ERROR ); + +report (vmx_inst_err == expected_error, +"VMX inst error is %d (actual %d)" , expected_error, vmx_inst_err); +} + +/* +* This version is wildly unsafe and should _only_ be used to test VM-Fail +* scenarios involving HOST_RIP. +*/ +static void test_vmx_vmlaunch_must_fail (u32 expected_error) +{ +/* Read the function name. */ +TEST_ASSERT (expected_error); + +/* +* Don't bother with any prep work, if VMLAUNCH passes the VM-Fail +* consistency checks and generates a VM-Exit, then the test is doomed +* no matter what as it will jump to a garbage RIP. +*/ + __asm__ __volatile__ ("vmlaunch" ); +test_assert_vmlaunch_inst_error (expected_error); +} +/* * Test for early VMLAUNCH failure. Returns true if VMLAUNCH makes it * at least as far as the guest-state checks. Returns false if the
@@ -3321,16 +3347,11 @@ success:static void test_vmx_vmlaunch (u32 xerror) {bool success =vmlaunch (); -u32 vmx_inst_err;report (success == !xerror,"vmlaunch %s" , !xerror ?"succeeds" :"fails" ); -if (!success && xerror) { - vmx_inst_err =vmcs_read (VMX_INST_ERROR ); -report (vmx_inst_err == xerror, -"VMX inst error is %d (actual %d)" , xerror, - vmx_inst_err); - } +if (!success && xerror) +test_assert_vmlaunch_inst_error (xerror); }/*
@@ -7640,7 +7661,7 @@ static void test_host_addr_size(void)vmcs_write (HOST_RIP ,NONCANONICAL );report_prefix_pushf ("HOST_RIP %llx" ,NONCANONICAL ); -test_vmx_vmlaunch (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD ); +test_vmx_vmlaunch_must_fail (VMXERR_ENTRY_INVALID_HOST_STATE_FIELD );report_prefix_pop ();vmcs_write (ENT_CONTROLS , entry_ctrl_saved |ENT_GUEST_64 );
Signature · 1 line
2.42.0.283.g2d96d420d3-goog