diff mbox series

[kvm-unit-tests,3/8] nVMX: Consolidate non-canonical code in test_canonical()

Message ID 20200312232745.884-4-sean.j.christopherson@intel.com (mailing list archive)
State New, archived
Headers show
Series nVMX: Clean up __enter_guest() and co. | expand

Commit Message

Sean Christopherson March 12, 2020, 11:27 p.m. UTC
Refactor 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.

Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
---
 x86/vmx_tests.c | 40 +++++++++++++++++-----------------------
 1 file changed, 17 insertions(+), 23 deletions(-)
diff mbox series

Patch

diff --git a/x86/vmx_tests.c b/x86/vmx_tests.c
index c4077b1..ac02b9d 100644
--- a/x86/vmx_tests.c
+++ b/x86/vmx_tests.c
@@ -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)				\