Skip to contentSkip to conversation

VMX framework enhancements and new tests

PATCH In review David Matlack 6 participants 52 messages Apr 21, 2017 – May 12, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover VMX framework enhancements and new tests

Patches 1-6 split out the VMX tests into their own unit tests.

Patches 7-12 add new VMX unit tests.

Patches 13-17 implement a new VMX test framework ("v2"). Instead of registering a main function and an exit handler, entering and exiting guest mode is accomplished by calling a function, enter_guest().

Patches 18-22 add some assert and printf enhancements to lib/.

Patches 23-32 add EPT access tests, using the v2 framework.

4 of the EPT access tests reveal KVM bugs (4.11-rc6, on a Haswell CPU):
vmx_ept_access_test_execute_only vmx_ept_access_test_paddr_not_present_ad_disabled vmx_ept_access_test_paddr_read_only_ad_disabled vmx_ept_access_test_paddr_read_execute_ad_disabled

The rest of the new tests in this series pass.

David Matlack (8):
x86: move basic vmx tests into separate test cases
x86: add missing vmx test cases to unittests.cfg
x86: vmx: filter exit_monitor_from_l2_test from full vmx test
x86: basic vmwrite/vmread test
x86: vmcs lifecycle test
x86: test VMCS in memory after VMCLEAR
x86: test VMPTRLD does not drop VMWRITEs
lib: add report_pass

GanShun (1):
x86: Adding gtest to check correct instruction error is returned

Peter Feiner (23):
x86: add test filter to vmx.flat.
x86: add config for each vmx unit test case
lib: better test name filtering
x86: test exit while vmcs02 is loaded
x86: don't special case vmx null test
x86: factor out vmx_enter_guest
x86: binstr utility function
x86: vmx exit reason descriptions
x86: v2 vmx test framework
lib: provide stdio.h
lib: added assert_msg macro
lib: fix *printf return value
lib: printf-style report prefixes
lib: add newline to assert_msg
lib: x86: store free pages in ascending order
lib: x86: multi-page allocator
x86: ept assertions
x86: setup_ept code motion
x86: 2GiB RAM for vmx tests
x86: ept capability utilities
lib: x86: page table utilities
x86: ept access tests
x86: force ept 2m test

 lib/libcflat.h     |  21 ++
 lib/printf.c       |  31 +-
 lib/report.c       |  45 ++-
 lib/stdio.h        |   4 +
 lib/string.c       |  41 +++
 lib/x86/asm/page.h |   2 +
 lib/x86/vm.c       | 183 ++++++++--
 lib/x86/vm.h       |  28 ++
 x86/unittests.cfg  | 291 +++++++++++++++-
 x86/vmx.c          | 833 +++++++++++++++++++++++++++++++++++++++-----
 x86/vmx.h          | 151 +++++++-
 x86/vmx_tests.c    | 989 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 12 files changed, 2493 insertions(+), 126 deletions(-)
 create mode 100644 lib/stdio.h
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#1 lore raw

PATCH 1/32 x86: add test filter to vmx.flat.

From: Peter Feiner <[email protected]>

The VMX nesting test has lots of test cases. Now a specific test case can be run by appending its name to the multiboot command line (i.e., under Qemu's: -append <name>)

---
 x86/vmx.c | 39 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 37 insertions(+), 2 deletions(-)
x86/vmx.c +37 -2
index b0b69602bdae..202cfaaa822e 100644
@@ -1158,9 +1158,37 @@ out:
 
 extern struct vmx_test vmx_tests[];
 
-int main(void)
+/* Match name with wanted allowing underscores in place of spaces. */
+static bool test_name_wanted(const char *name, const char *wanted)
+{
+	const char *n;
+	const char *w;
+
+	for (n = name, w = wanted; *n != '\0' && *w != '\0'; n++, w++) {
+		if (*n != *w && !(*n == ' ' && *w == '_'))
+			return false;
+	}
+	return *n == '\0' && *w == '\0';
+}
+
+static bool test_wanted(struct vmx_test *test, char *wanted[], int nwanted)
+{
+	int i;
+
+	if (!nwanted)
+		return true;
+
+	for (i = 0; i < nwanted; ++i) {
+		if (test_name_wanted(test->name, wanted[i]))
+			return true;
+	}
+	return false;
+}
+
+int main(int argc, char *argv[])
 {
 	int i = 0;
+	int matched = 0;
 
 	setup_vm();
 	setup_idt();
@@ -1188,9 +1216,16 @@ int main(void)
 	test_vmxoff();
 	test_vmx_caps();
 
-	while (vmx_tests[++i].name != NULL)
+	while (vmx_tests[++i].name != NULL) {
+		if (!test_wanted(&vmx_tests[i], argv + 1, argc - 1))
+			continue;
+		matched++;
 		if (test_run(&vmx_tests[i]))
 			goto exit;
+	}
+
+	if (!matched)
+		report("command line didn't match any tests!", matched);
 
 exit:
 	return report_summary();
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#2 lore raw

PATCH 2/32 x86: add config for each vmx unit test case

In reply to David Matlack

From: Peter Feiner <[email protected]>

---
 x86/unittests.cfg | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 x86/vmx_tests.c   |  4 +--
 2 files changed, 87 insertions(+), 2 deletions(-)
x86/unittests.cfg +85 -0
index 54138388fd5d..a267638733aa 100644
@@ -198,6 +198,91 @@ arch = x86_64
 file = vmx.flat
 extra_params = -cpu host,+vmx
 arch = x86_64
+groups = vmx
+
+[vmx_vmenter]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append vmenter
+arch = x86_64
+groups = vmx
+
+[vmx_preemption_timer]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append preemption_timer
+arch = x86_64
+groups = vmx
+
+[vmx_control_field_PAT]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append control_field_PAT
+arch = x86_64
+groups = vmx
+
+[vmx_control_field_EFER]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append control_field_EFER
+arch = x86_64
+groups = vmx
+
+[vmx_CR_shadowing]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append CR_shadowing
+arch = x86_64
+groups = vmx
+
+[vmx_IO_bitmap]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append I/O_bitmap
+arch = x86_64
+groups = vmx
+
+[vmx_instruction_intercept]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append instruction_intercept
+arch = x86_64
+groups = vmx
+
+[vmx_EPT_AD_enabled]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append EPT_A/D_enabled
+arch = x86_64
+groups = vmx
+
+[vmx_EPT_AD_disabled]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append EPT_A/D_disabled
+arch = x86_64
+groups = vmx
+
+[vmx_VPID]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append VPID
+arch = x86_64
+groups = vmx
+
+[vmx_interrupt]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append interrupt
+arch = x86_64
+groups = vmx
+
+[vmx_debug_controls]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append debug_controls
+arch = x86_64
+groups = vmx
+
+[vmx_MSR_switch]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append MSR_switch
+arch = x86_64
+groups = vmx
+
+[vmx_vmmcall]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append vmmcall
+arch = x86_64
+groups = vmx
 
 [debug]
 file = debug.flat
x86/vmx_tests.c +2 -2
index b9115637915c..cd18b133a3a3 100644
@@ -1900,8 +1900,8 @@ struct vmx_test vmx_tests[] = {
 		NULL, {0} },
 	{ "instruction intercept", insn_intercept_init, insn_intercept_main,
 		insn_intercept_exit_handler, NULL, {0} },
-	{ "EPT, A/D disabled", ept_init, ept_main, ept_exit_handler, NULL, {0} },
-	{ "EPT, A/D enabled", eptad_init, eptad_main, eptad_exit_handler, NULL, {0} },
+	{ "EPT A/D disabled", ept_init, ept_main, ept_exit_handler, NULL, {0} },
+	{ "EPT A/D enabled", eptad_init, eptad_main, eptad_exit_handler, NULL, {0} },
 	{ "VPID", vpid_init, vpid_main, vpid_exit_handler, NULL, {0} },
 	{ "interrupt", interrupt_init, interrupt_main,
 		interrupt_exit_handler, NULL, {0} },
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#3 lore raw

PATCH 3/32 x86: move basic vmx tests into separate test cases

In reply to David Matlack

By default every vmx kvm-unit-test runs a series of tests at startup. This patch moves those startup tests into test cases which are only run when requested.

---
 x86/unittests.cfg | 36 +++++++++++++++++++++++++++++++
 x86/vmx.c         | 64 +++++++++++++++++++++++++++++++++++++++++--------------
 2 files changed, 84 insertions(+), 16 deletions(-)
x86/unittests.cfg +36 -0
index a267638733aa..c942adad6325 100644
@@ -200,6 +200,42 @@ extra_params = -cpu host,+vmx
 arch = x86_64
 groups = vmx
 
+[vmx_test_vmx_feature_control]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append test_vmx_feature_control
+arch = x86_64
+groups = vmx
+
+[vmx_test_vmxon]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append test_vmxon
+arch = x86_64
+groups = vmx
+
+[vmx_test_vmptrld]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append test_vmptrld
+arch = x86_64
+groups = vmx
+
+[vmx_test_vmclear]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append test_vmclear
+arch = x86_64
+groups = vmx
+
+[vmx_test_vmptrst]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append test_vmptrst
+arch = x86_64
+groups = vmx
+
+[vmx_test_vmx_caps]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append test_vmx_caps
+arch = x86_64
+groups = vmx
+
 [vmx_vmenter]
 file = vmx.flat
 extra_params = -cpu host,+vmx -append vmenter
x86/vmx.c +48 -16

109 lines not on this page · raw message

Signature · 1 line
2.12.2.816.g2cccc81164-goog
#8 lore raw

PATCH 7/32 x86: test exit while vmcs02 is loaded

In reply to David Matlack

From: Peter Feiner <[email protected]>

Before applying "KVM: nVMX: fix lifetime issues for vmcs0", exiting while vmcs02 was loaded would trigger a VM_BUG_ON.

---
 x86/vmx_tests.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
x86/vmx_tests.c +14 -0
index cd18b133a3a3..35af67744971 100644
@@ -1884,6 +1884,18 @@ int into_exit_handler()
 	return VMX_TEST_VMEXIT;
 }
 
+static void exit_monitor_from_l2_main(void)
+{
+	printf("Calling exit(0) from l2...\n");
+	exit(0);
+}
+
+static int exit_monitor_from_l2_handler(void)
+{
+	report("The guest should have killed the VMM", false);
+	return VMX_TEST_EXIT;
+}
+
 /* name/init/guest_main/exit_handler/syscall_handler/guest_regs */
 struct vmx_test vmx_tests[] = {
 	{ "null", NULL, basic_guest_main, basic_exit_handler, NULL, {0} },
@@ -1914,5 +1926,7 @@ struct vmx_test vmx_tests[] = {
 		disable_rdtscp_exit_handler, NULL, {0} },
 	{ "int3", int3_init, int3_guest_main, int3_exit_handler, NULL, {0} },
 	{ "into", into_init, into_guest_main, into_exit_handler, NULL, {0} },
+	{ "exit_monitor_from_l2_test", NULL, exit_monitor_from_l2_main,
+		exit_monitor_from_l2_handler, NULL, {0} },
 	{ NULL, NULL, NULL, NULL, NULL, {0} },
 };
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#5 lore raw

PATCH 5/32 lib: better test name filtering

In reply to David Matlack

From: Peter Feiner <[email protected]>

Limited glob matching with leading or trailing '*'.

Tests (and globs of tests) can be excluded by prepending with a '-'.

---
 lib/libcflat.h |  2 ++
 lib/string.c   | 41 +++++++++++++++++++++++++++++++++++++++++
 x86/vmx.c      | 52 +++++++++++++++++++++++++++-------------------------
 3 files changed, 70 insertions(+), 25 deletions(-)
lib/libcflat.h +2 -0
index 96a37926f302..248fd023626e 100644
@@ -105,6 +105,8 @@ extern void report_skip(const char *msg_fmt, ...);
 extern void report_info(const char *msg_fmt, ...);
 extern int report_summary(void);
 
+bool simple_glob(const char *text, const char *pattern);
+
 extern void dump_stack(void);
 extern void dump_frame_stack(const void *instruction, const void *frame);
 
lib/string.c +41 -0

44 lines not on this page · raw message

x86/vmx.c +27 -25

68 lines not on this page · raw message

Signature · 1 line
2.12.2.816.g2cccc81164-goog
#10 lore raw

PATCH 9/32 x86: basic vmwrite/vmread test

In reply to David Matlack

Issues VMWRITE to every VMCS field and the checks that VMREAD returns the expected result. Some tricky cases: read-only fields (skipped), not-yet-implemented fields (skipped, VMREAD fails with VMfailValid), guest segment access rights fields (reserved bits are zeroed by the CPU, so only check non-reserved bits).

---
 x86/unittests.cfg |   6 ++
 x86/vmx.c         | 250 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 x86/vmx.h         |  29 +++++++
 3 files changed, 275 insertions(+), 10 deletions(-)
x86/unittests.cfg +6 -0
index 8011429d2307..7973f2f62d26 100644
@@ -230,6 +230,12 @@ extra_params = -cpu host,+vmx -append test_vmptrst
 arch = x86_64
 groups = vmx
 
+[vmx_test_vmwrite_vmread]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append test_vmwrite_vmread
+arch = x86_64
+groups = vmx
+
 [vmx_test_vmx_caps]
 file = vmx.flat
 extra_params = -cpu host,+vmx -append test_vmx_caps
x86/vmx.c +240 -10

268 lines not on this page · raw message

x86/vmx.h +29 -0
index 52ece1aa53c8..2328f0eee05d 100644
@@ -567,6 +567,16 @@ void vmx_set_test_stage(u32 s);
 u32 vmx_get_test_stage(void);
 void vmx_inc_test_stage(void);
 
+static inline int make_vmcs_current(struct vmcs *vmcs)
+{
+	bool ret;
+	u64 rflags = read_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF;
+
+	asm volatile ("push %1; popf; vmptrld %2; setbe %0"
+		      : "=q" (ret) : "q" (rflags), "m" (vmcs) : "cc");
+	return ret;
+}
+
 static inline int vmcs_clear(struct vmcs *vmcs)
 {
 	bool ret;
@@ -584,6 +594,25 @@ static inline u64 vmcs_read(enum Encoding enc)
 	return val;
 }
 
+static inline int vmcs_read_checking(enum Encoding enc, u64 *value)
+{
+	u64 rflags = read_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF;
+	u64 encoding = enc;
+	u64 val;
+
+	asm volatile ("shl $8, %%rax;"
+		      "sahf;"
+		      "vmread %[encoding], %[val];"
+		      "lahf;"
+		      "shr $8, %%rax"
+		      : /* output */ [val]"=rm"(val), "+a"(rflags)
+		      : /* input */ [encoding]"r"(encoding)
+		      : /* clobber */ "cc");
+
+	*value = val;
+	return rflags & (X86_EFLAGS_CF | X86_EFLAGS_ZF);
+}
+
 static inline int vmcs_write(enum Encoding enc, u64 val)
 {
 	bool ret;
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#6 lore raw

PATCH 6/32 x86: vmx: filter exit_monitor_from_l2_test from full vmx test

In reply to David Matlack

Filter out exit_monitor_from_l2_test when running the full vmx test suite (i.e. the "[vmx]" test suite in unittest.cfg). Since exit_monitor_from_l2_test causes the VM to shutdown, many of the tests in vmx_tests.c end up getting skipped.

---
 x86/unittests.cfg | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/unittests.cfg +1 -1
index 02dd6b235739..8011429d2307 100644
@@ -196,7 +196,7 @@ arch = x86_64
 
 [vmx]
 file = vmx.flat
-extra_params = -cpu host,+vmx
+extra_params = -cpu host,+vmx -append -exit_monitor_from_l2_test
 arch = x86_64
 groups = vmx
 
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#9 lore raw

PATCH 8/32 x86: Adding gtest to check correct instruction error is returned

In reply to David Matlack

From: GanShun <[email protected]>

Added a check in test_vmptrld to verfiy that VMXERR_VMPTRLD_VMXON_POINTER is returned instead of VMXERR_VMCLEAR_VMXON_POINTER.

---
 x86/vmx.c |  3 +++
 x86/vmx.h | 31 +++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)
x86/vmx.c +3 -0
index 651807a76a1e..39a891da4635 100644
@@ -899,9 +899,12 @@ static void test_vmptrld(void)
 	       make_vmcs_current(tmp_root) == 1);
 
 	/* Pass VMXON region */
+	make_vmcs_current(vmcs);
 	tmp_root = (struct vmcs *)vmxon_region;
 	report("test vmptrld with vmxon region",
 	       make_vmcs_current(tmp_root) == 1);
+	report("test vmptrld with vmxon region vm-instruction error",
+	       vmcs_read(VMX_INST_ERROR) == VMXERR_VMPTRLD_VMXON_POINTER);
 
 	report("test vmptrld with valid vmcs region", make_vmcs_current(vmcs) == 0);
 }
x86/vmx.h +31 -0
index 290e6bd72b19..52ece1aa53c8 100644
@@ -396,6 +396,37 @@ enum Intr_type {
 
 #define INTR_INFO_INTR_TYPE_SHIFT       8
 
+/*
+ * VM-instruction error numbers
+ */
+enum vm_instruction_error_number {
+	VMXERR_VMCALL_IN_VMX_ROOT_OPERATION = 1,
+	VMXERR_VMCLEAR_INVALID_ADDRESS = 2,
+	VMXERR_VMCLEAR_VMXON_POINTER = 3,
+	VMXERR_VMLAUNCH_NONCLEAR_VMCS = 4,
+	VMXERR_VMRESUME_NONLAUNCHED_VMCS = 5,
+	VMXERR_VMRESUME_AFTER_VMXOFF = 6,
+	VMXERR_ENTRY_INVALID_CONTROL_FIELD = 7,
+	VMXERR_ENTRY_INVALID_HOST_STATE_FIELD = 8,
+	VMXERR_VMPTRLD_INVALID_ADDRESS = 9,
+	VMXERR_VMPTRLD_VMXON_POINTER = 10,
+	VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID = 11,
+	VMXERR_UNSUPPORTED_VMCS_COMPONENT = 12,
+	VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT = 13,
+	VMXERR_VMXON_IN_VMX_ROOT_OPERATION = 15,
+	VMXERR_ENTRY_INVALID_EXECUTIVE_VMCS_POINTER = 16,
+	VMXERR_ENTRY_NONLAUNCHED_EXECUTIVE_VMCS = 17,
+	VMXERR_ENTRY_EXECUTIVE_VMCS_POINTER_NOT_VMXON_POINTER = 18,
+	VMXERR_VMCALL_NONCLEAR_VMCS = 19,
+	VMXERR_VMCALL_INVALID_VM_EXIT_CONTROL_FIELDS = 20,
+	VMXERR_VMCALL_INCORRECT_MSEG_REVISION_ID = 22,
+	VMXERR_VMXOFF_UNDER_DUAL_MONITOR_TREATMENT_OF_SMIS_AND_SMM = 23,
+	VMXERR_VMCALL_INVALID_SMM_MONITOR_FEATURES = 24,
+	VMXERR_ENTRY_INVALID_VM_EXECUTION_CONTROL_FIELDS_IN_EXECUTIVE_VMCS = 25,
+	VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS = 26,
+	VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID = 28,
+};
+
 #define SAVE_GPR				\
 	"xchg %rax, regs\n\t"			\
 	"xchg %rbx, regs+0x8\n\t"		\
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#4 lore raw

PATCH 4/32 x86: add missing vmx test cases to unittests.cfg

In reply to David Matlack
---
 x86/unittests.cfg | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
x86/unittests.cfg +24 -0
index c942adad6325..02dd6b235739 100644
@@ -320,6 +320,30 @@ extra_params = -cpu host,+vmx -append vmmcall
 arch = x86_64
 groups = vmx
 
+[vmx_disable_RDTSCP]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append disable_RDTSCP
+arch = x86_64
+groups = vmx
+
+[vmx_int3]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append int3
+arch = x86_64
+groups = vmx
+
+[vmx_into]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append into
+arch = x86_64
+groups = vmx
+
+[vmx_exit_monitor_from_l2_test]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append exit_monitor_from_l2_test
+arch = x86_64
+groups = vmx
+
 [debug]
 file = debug.flat
 arch = x86_64
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#15 lore raw

PATCH 14/32 x86: factor out vmx_enter_guest

In reply to David Matlack

From: Peter Feiner <[email protected]>

---
 x86/vmx.c | 75 ++++++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 41 insertions(+), 34 deletions(-)
x86/vmx.c +41 -34
index 15ed94af56fd..e6c11b013d94 100644
@@ -1376,43 +1376,54 @@ entry_failure_handler(struct vmentry_failure *failure)
 		return VMX_TEST_EXIT;
 }
 
-static int vmx_run()
+/*
+ * Tries to enter the guest. Returns true iff entry succeeded. Otherwise,
+ * populates @failure.
+ */
+static bool vmx_enter_guest(struct vmentry_failure *failure)
 {
-	unsigned long host_rflags;
+	failure->early = 0;
+
+	asm volatile (
+		"mov %[HOST_RSP], %%rdi\n\t"
+		"vmwrite %%rsp, %%rdi\n\t"
+		LOAD_GPR_C
+		"cmpb $0, %[launched]\n\t"
+		"jne 1f\n\t"
+		"vmlaunch\n\t"
+		"jmp 2f\n\t"
+		"1: "
+		"vmresume\n\t"
+		"2: "
+		SAVE_GPR_C
+		"pushf\n\t"
+		"pop %%rdi\n\t"
+		"mov %%rdi, %[failure_flags]\n\t"
+		"movl $1, %[failure_flags]\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)
+		: [launched]"m"(launched), [HOST_RSP]"i"(HOST_RSP)
+		: "rdi", "memory", "cc"
+	);
+
+	failure->vmlaunch = !launched;
+	failure->instr = launched ? "vmresume" : "vmlaunch";
+
+	return !failure->early && !(vmcs_read(EXI_REASON) & VMX_ENTRY_FAILURE);
+}
 
+static int vmx_run()
+{
 	while (1) {
 		u32 ret;
-		u32 fail = 0;
 		bool entered;
 		struct vmentry_failure failure;
 
-		asm volatile (
-			"mov %[HOST_RSP], %%rdi\n\t"
-			"vmwrite %%rsp, %%rdi\n\t"
-			LOAD_GPR_C
-			"cmpb $0, %[launched]\n\t"
-			"jne 1f\n\t"
-			"vmlaunch\n\t"
-			"jmp 2f\n\t"
-			"1: "
-			"vmresume\n\t"
-			"2: "
-			SAVE_GPR_C
-			"pushf\n\t"
-			"pop %%rdi\n\t"
-			"mov %%rdi, %[host_rflags]\n\t"
-			"movl $1, %[fail]\n\t"
-			"jmp 3f\n\t"
-			"vmx_return:\n\t"
-			SAVE_GPR_C
-			"3: \n\t"
-			: [fail]"+m"(fail), [host_rflags]"=m"(host_rflags)
-			: [launched]"m"(launched), [HOST_RSP]"i"(HOST_RSP)
-			: "rdi", "memory", "cc"
-
-		);
-
-		entered = !fail && !(vmcs_read(EXI_REASON) & VMX_ENTRY_FAILURE);
+		entered = vmx_enter_guest(&failure);
 
 		if (entered) {
 			/*
@@ -1422,10 +1433,6 @@ static int vmx_run()
 			launched = 1;
 			ret = exit_handler();
 		} else {
-			failure.flags = host_rflags;
-			failure.vmlaunch = !launched;
-			failure.instr = launched ? "vmresume" : "vmlaunch";
-			failure.early = fail;
 			ret = entry_failure_handler(&failure);
 		}
 
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#13 lore raw

PATCH 12/32 x86: test VMPTRLD does not drop VMWRITEs

In reply to David Matlack
---
 x86/vmx.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
x86/vmx.c +6 -1
index fd1aa3bc6c2e..9ca37f63b636 100644
@@ -257,7 +257,7 @@ static bool check_vmcs_field(struct vmcs_field *f, u8 cookie)
 	if (expected == actual)
 		return true;
 
-	printf("FAIL: VMWRITE/VMREAD %lx (expected: %lx, actual: %lx)",
+	printf("FAIL: VMWRITE/VMREAD %lx (expected: %lx, actual: %lx)\n",
 	       f->encoding, (unsigned long) expected, (unsigned long) actual);
 
 	return false;
@@ -349,6 +349,11 @@ void test_vmcs_lifecycle(void)
 	VMCLEAR(0);
 	report("current:VMCS1 active:[VCMS1]", check_all_vmcs_fields(1));
 
+	/* VMPTRLD should not erase VMWRITEs to the current VMCS */
+	set_all_vmcs_fields(2);
+	VMPTRLD(1);
+	report("current:VMCS1 active:[VCMS1]", check_all_vmcs_fields(2));
+
 	for (i = 0; i < ARRAY_SIZE(vmcs); i++) {
 		VMCLEAR(i);
 		free_page(vmcs[i]);
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#19 lore raw

PATCH 18/32 lib: provide stdio.h

In reply to David Matlack

From: Peter Feiner <[email protected]>

kvm-unit-tests builds with the odd combination of system include files but not system libraries. That is, -nostdlib is specified but -nostdinc isn't! Problems arise when the include files don't match the libraries, e.g., printf() in some versions of glibc linking in a __printf_chk symbol that libcflat doesn't have. This patch fixes the problem with printf by pointing '#include "stdio.h"' to lib/libcflat.h, where a printf declaration that libcflat actually implements exists, rather than letting /usr/include/stdio.h get included. This patch does not solve the general problem of potential mismatches between libcflat and the compiler's notion standard include files; doing so would require rewriting stdarg.h, stdint.h, and several others in libcflat.

---
 lib/stdio.h | 4 ++++
 1 file changed, 4 insertions(+)
 create mode 100644 lib/stdio.h
lib/stdio.h new file +4 -0
new file mode 100644
index 000000000000..7b30db93f7f1
@@ -0,0 +1,4 @@
+#ifndef LIBCFLAT_STDIO_H
+#define LIBCFLAT_STDIO_H
+#include "libcflat.h"
+#endif
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#14 lore raw

PATCH 13/32 x86: don't special case vmx null test

In reply to David Matlack

From: Peter Feiner <[email protected]>

---
 x86/unittests.cfg |  6 ++++++
 x86/vmx.c         | 25 ++++---------------------
 x86/vmx_tests.c   |  1 +
 3 files changed, 11 insertions(+), 21 deletions(-)
x86/unittests.cfg +6 -0
index 0f2c94fb548d..8a89eb0ef0ad 100644
@@ -200,6 +200,12 @@ extra_params = -cpu host,+vmx -append -exit_monitor_from_l2_test
 arch = x86_64
 groups = vmx
 
+[vmx_null]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append null
+arch = x86_64
+groups = vmx
+
 [vmx_test_vmx_feature_control]
 file = vmx.flat
 extra_params = -cpu host,+vmx -append test_vmx_feature_control
x86/vmx.c +4 -21
index 9ca37f63b636..15ed94af56fd 100644
@@ -563,14 +563,6 @@ static void test_vmclear(void)
 	test_vmclear_flushing();
 }
 
-static void test_vmxoff(void)
-{
-	int ret;
-
-	ret = vmx_off();
-	report("test vmxoff", !ret);
-}
-
 static void __attribute__((__used__)) guest_main(void)
 {
 	current->guest_main();
@@ -1550,9 +1542,6 @@ int main(int argc, const char *argv[])
 			wrmsr(MSR_IA32_FEATURE_CONTROL, 0x5);
 	}
 
-	/* Set basic test ctxt the same as "null" */
-	current = &vmx_tests[0];
-
 	if (test_wanted("test_vmxon", argv, argc)) {
 		/* Enables VMX */
 		if (test_vmxon() != 0)
@@ -1574,19 +1563,13 @@ int main(int argc, const char *argv[])
 		test_vmwrite_vmread();
 	if (test_wanted("test_vmcs_lifecycle", argv, argc))
 		test_vmcs_lifecycle();
-
-	init_vmcs(&vmcs_root);
-	if (vmx_run()) {
-		report("test vmlaunch", 0);
-		goto exit;
-	}
-
-	test_vmxoff();
-
 	if (test_wanted("test_vmx_caps", argv, argc))
 		test_vmx_caps();
 
-	while (vmx_tests[++i].name != NULL) {
+	/* Balance vmxon from test_vmxon. */
+	vmx_off();
+
+	for (; vmx_tests[i].name != NULL; i++) {
 		if (!test_wanted(vmx_tests[i].name, argv, argc))
 			continue;
 		if (test_run(&vmx_tests[i]))
x86/vmx_tests.c +1 -0
index 35af67744971..918cade2a3e8 100644
@@ -29,6 +29,7 @@ static inline void vmcall()
 
 void basic_guest_main()
 {
+	report("Basic VMX test", 1);
 }
 
 int basic_exit_handler()
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#16 lore raw

PATCH 15/32 x86: binstr utility function

In reply to David Matlack

From: Peter Feiner <[email protected]>

---
 lib/libcflat.h |  7 +++++++
 lib/printf.c   | 30 ++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
lib/libcflat.h +7 -0
index 248fd023626e..1d2eba980e1a 100644
@@ -131,4 +131,11 @@ static inline bool is_power_of_2(unsigned long n)
 	return n && !(n & (n - 1));
 }
 
+/*
+ * One byte per bit, a ' between each group of 4 bits, and a null terminator.
+ */
+#define BINSTR_SZ (sizeof(unsigned long) * 8 + sizeof(unsigned long) * 2)
+void binstr(unsigned long x, char out[BINSTR_SZ]);
+void print_binstr(unsigned long x);
+
 #endif
lib/printf.c +30 -0
index 2aec59aa6d6d..cecbeadc4440 100644
@@ -259,3 +259,33 @@ int printf(const char *fmt, ...)
     puts(buf);
     return r;
 }
+
+void binstr(unsigned long x, char out[BINSTR_SZ])
+{
+	int i;
+	char *c;
+	int n;
+
+	n = sizeof(unsigned long) * 8;
+	i = 0;
+	c = &out[0];
+	for (;;) {
+		*c++ = (x & (1ul << (n - i - 1))) ? '1' : '0';
+		i++;
+
+		if (i == n) {
+			*c = '\0';
+			break;
+		}
+		if (i % 4 == 0)
+			*c++ = '\'';
+	}
+	assert(c + 1 - &out[0] == BINSTR_SZ);
+}
+
+void print_binstr(unsigned long x)
+{
+	char out[BINSTR_SZ];
+	binstr(x, out);
+	printf("%s", out);
+}
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#23 lore raw

PATCH 19/32 lib: added assert_msg macro

In reply to David Matlack

From: Peter Feiner <[email protected]>

---
 lib/libcflat.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
lib/libcflat.h +10 -0
index 1d2eba980e1a..5d356df75f1f 100644
@@ -126,6 +126,16 @@ do {									\
 	}								\
 } while (0)
 
+#define assert_msg(cond, fmt, args...)					\
+do {									\
+	if (!(cond)) {							\
+		printf("%s:%d: assert failed: %s: " fmt,		\
+		       __FILE__, __LINE__, #cond, ## args);		\
+		dump_stack();						\
+		abort();						\
+	}								\
+} while (0)
+
 static inline bool is_power_of_2(unsigned long n)
 {
 	return n && !(n & (n - 1));
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#25 lore raw

PATCH 20/32 lib: fix *printf return value

In reply to David Matlack

From: Peter Feiner <[email protected]>

Was returning the number of characters printed plus, incorrectly, one for the null terminator.

---
 lib/printf.c | 1 -
 1 file changed, 1 deletion(-)
lib/printf.c +0 -1
index cecbeadc4440..aca2920887c1 100644
@@ -221,7 +221,6 @@ int vsnprintf(char *buf, int size, const char *fmt, va_list va)
 	}
     }
     *s.buffer = 0;
-    ++s.added;
     return s.added;
 }
 
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#11 lore raw

PATCH 10/32 x86: vmcs lifecycle test

In reply to David Matlack

This test VMCLEARs and VMPTRLDs two VMCS's to put each VMCS into different states of active/not-active and current/not-current and then verifies that VMREAD/VMWRITE to the current VMCS work correctly.

---
 x86/unittests.cfg |  6 ++++++
 x86/vmx.c         | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)
x86/unittests.cfg +6 -0
index 7973f2f62d26..0f2c94fb548d 100644
@@ -236,6 +236,12 @@ extra_params = -cpu host,+vmx -append test_vmwrite_vmread
 arch = x86_64
 groups = vmx
 
+[vmx_test_vmcs_lifecycle]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append test_vmcs_lifecycle
+arch = x86_64
+groups = vmx
+
 [vmx_test_vmx_caps]
 file = vmx.flat
 extra_params = -cpu host,+vmx -append test_vmx_caps
x86/vmx.c +60 -0

72 lines not on this page · raw message

Signature · 1 line
2.12.2.816.g2cccc81164-goog
#17 lore raw

PATCH 16/32 x86: vmx exit reason descriptions

In reply to David Matlack

From: Peter Feiner <[email protected]>

---
 x86/vmx.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 x86/vmx.h | 11 +++++++++-
 2 files changed, 79 insertions(+), 1 deletion(-)
x86/vmx.c +69 -0
index e6c11b013d94..cd4cb1219040 100644
@@ -425,6 +425,75 @@ static inline int vmx_off()
 	return ret;
 }
 
+static const char * const exit_reason_descriptions[] = {
+	[VMX_EXC_NMI]		= "VMX_EXC_NMI",
+	[VMX_EXTINT]		= "VMX_EXTINT",
+	[VMX_TRIPLE_FAULT]	= "VMX_TRIPLE_FAULT",
+	[VMX_INIT]		= "VMX_INIT",
+	[VMX_SIPI]		= "VMX_SIPI",
+	[VMX_SMI_IO]		= "VMX_SMI_IO",
+	[VMX_SMI_OTHER]		= "VMX_SMI_OTHER",
+	[VMX_INTR_WINDOW]	= "VMX_INTR_WINDOW",
+	[VMX_NMI_WINDOW]	= "VMX_NMI_WINDOW",
+	[VMX_TASK_SWITCH]	= "VMX_TASK_SWITCH",
+	[VMX_CPUID]		= "VMX_CPUID",
+	[VMX_GETSEC]		= "VMX_GETSEC",
+	[VMX_HLT]		= "VMX_HLT",
+	[VMX_INVD]		= "VMX_INVD",
+	[VMX_INVLPG]		= "VMX_INVLPG",
+	[VMX_RDPMC]		= "VMX_RDPMC",
+	[VMX_RDTSC]		= "VMX_RDTSC",
+	[VMX_RSM]		= "VMX_RSM",
+	[VMX_VMCALL]		= "VMX_VMCALL",
+	[VMX_VMCLEAR]		= "VMX_VMCLEAR",
+	[VMX_VMLAUNCH]		= "VMX_VMLAUNCH",
+	[VMX_VMPTRLD]		= "VMX_VMPTRLD",
+	[VMX_VMPTRST]		= "VMX_VMPTRST",
+	[VMX_VMREAD]		= "VMX_VMREAD",
+	[VMX_VMRESUME]		= "VMX_VMRESUME",
+	[VMX_VMWRITE]		= "VMX_VMWRITE",
+	[VMX_VMXOFF]		= "VMX_VMXOFF",
+	[VMX_VMXON]		= "VMX_VMXON",
+	[VMX_CR]		= "VMX_CR",
+	[VMX_DR]		= "VMX_DR",
+	[VMX_IO]		= "VMX_IO",
+	[VMX_RDMSR]		= "VMX_RDMSR",
+	[VMX_WRMSR]		= "VMX_WRMSR",
+	[VMX_FAIL_STATE]	= "VMX_FAIL_STATE",
+	[VMX_FAIL_MSR]		= "VMX_FAIL_MSR",
+	[VMX_MWAIT]		= "VMX_MWAIT",
+	[VMX_MTF]		= "VMX_MTF",
+	[VMX_MONITOR]		= "VMX_MONITOR",
+	[VMX_PAUSE]		= "VMX_PAUSE",
+	[VMX_FAIL_MCHECK]	= "VMX_FAIL_MCHECK",
+	[VMX_TPR_THRESHOLD]	= "VMX_TPR_THRESHOLD",
+	[VMX_APIC_ACCESS]	= "VMX_APIC_ACCESS",
+	[VMX_GDTR_IDTR]		= "VMX_GDTR_IDTR",
+	[VMX_LDTR_TR]		= "VMX_LDTR_TR",
+	[VMX_EPT_VIOLATION]	= "VMX_EPT_VIOLATION",
+	[VMX_EPT_MISCONFIG]	= "VMX_EPT_MISCONFIG",
+	[VMX_INVEPT]		= "VMX_INVEPT",
+	[VMX_PREEMPT]		= "VMX_PREEMPT",
+	[VMX_INVVPID]		= "VMX_INVVPID",
+	[VMX_WBINVD]		= "VMX_WBINVD",
+	[VMX_XSETBV]		= "VMX_XSETBV",
+	[VMX_APIC_WRITE]	= "VMX_APIC_WRITE",
+	[VMX_RDRAND]		= "VMX_RDRAND",
+	[VMX_INVPCID]		= "VMX_INVPCID",
+	[VMX_VMFUNC]		= "VMX_VMFUNC",
+	[VMX_RDSEED]		= "VMX_RDSEED",
+	[VMX_PML_FULL]		= "VMX_PML_FULL",
+	[VMX_XSAVES]		= "VMX_XSAVES",
+	[VMX_XRSTORS]		= "VMX_XRSTORS",
+};
+
+const char *exit_reason_description(u64 reason)
+{
+	if (reason >= ARRAY_SIZE(exit_reason_descriptions))
+		return "(unknown)";
+	return exit_reason_descriptions[reason] ? : "(unused)";
+}
+
 void print_vmexit_info()
 {
 	u64 guest_rip, guest_rsp;
x86/vmx.h +10 -1
index 2328f0eee05d..3e4015660797 100644
@@ -317,7 +317,15 @@ enum Reason {
 	VMX_PREEMPT		= 52,
 	VMX_INVVPID		= 53,
 	VMX_WBINVD		= 54,
-	VMX_XSETBV		= 55
+	VMX_XSETBV		= 55,
+	VMX_APIC_WRITE		= 56,
+	VMX_RDRAND		= 57,
+	VMX_INVPCID		= 58,
+	VMX_VMFUNC		= 59,
+	VMX_RDSEED		= 61,
+	VMX_PML_FULL		= 62,
+	VMX_XSAVES		= 63,
+	VMX_XRSTORS		= 64,
 };
 
 enum Ctrl_exi {
@@ -659,6 +667,7 @@ static inline bool invvpid(unsigned long type, u16 vpid, u64 gva)
 	return ret;
 }
 
+const char *exit_reason_description(u64 reason);
 void print_vmexit_info();
 void print_vmentry_failure_info(struct vmentry_failure *failure);
 void ept_sync(int type, u64 eptp);
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#27 lore raw

PATCH 21/32 lib: printf-style report prefixes

In reply to David Matlack

From: Peter Feiner <[email protected]>

---
 lib/libcflat.h |  1 +
 lib/report.c   | 38 +++++++++++++++++++++++++++++++++-----
 2 files changed, 34 insertions(+), 5 deletions(-)
lib/libcflat.h +1 -0
index 5d356df75f1f..05c18543dd72 100644
@@ -96,6 +96,7 @@ extern int vsnprintf(char *buf, int size, const char *fmt, va_list va)
 extern int vprintf(const char *fmt, va_list va)
 					__attribute__((format(printf, 1, 0)));
 
+void report_prefix_pushf(const char *prefix_fmt, ...);
 extern void report_prefix_push(const char *prefix);
 extern void report_prefix_pop(void);
 extern void report(const char *msg_fmt, bool pass, ...);
lib/report.c +33 -5
index e24e81382f9e..1033f1e44e99 100644
@@ -17,14 +17,42 @@ static unsigned int tests, failures, xfailures, skipped;
 static char prefixes[256];
 static struct spinlock lock;
 
-void report_prefix_push(const char *prefix)
+#define PREFIX_DELIMITER ": "
+
+void report_prefix_pushf(const char *prefix_fmt, ...)
 {
+	va_list va;
+	int len;
+	int start;
+
 	spin_lock(&lock);
-	strcat(prefixes, prefix);
-	strcat(prefixes, ": ");
+
+	len = strlen(prefixes);
+	assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
+	start = len;
+
+	va_start(va, prefix_fmt);
+	len += vsnprintf(&prefixes[len], sizeof(prefixes) - len, prefix_fmt,
+			 va);
+	va_end(va);
+	assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
+
+	assert_msg(!strstr(&prefixes[start], PREFIX_DELIMITER),
+		   "Prefix \"%s\" contains delimiter \"" PREFIX_DELIMITER "\"",
+		   &prefixes[start]);
+
+	len += snprintf(&prefixes[len], sizeof(prefixes) - len,
+			PREFIX_DELIMITER);
+	assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
+
 	spin_unlock(&lock);
 }
 
+void report_prefix_push(const char *prefix)
+{
+	report_prefix_pushf("%s", prefix);
+}
+
 void report_prefix_pop(void)
 {
 	char *p, *q;
@@ -34,9 +62,9 @@ void report_prefix_pop(void)
 	if (!*prefixes)
 		return;
 
-	for (p = prefixes, q = strstr(p, ": ") + 2;
+	for (p = prefixes, q = strstr(p, PREFIX_DELIMITER) + 2;
 			*q;
-			p = q, q = strstr(p, ": ") + 2)
+			p = q, q = strstr(p, PREFIX_DELIMITER) + 2)
 		;
 	*p = '\0';
 
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#12 lore raw

PATCH 11/32 x86: test VMCS in memory after VMCLEAR

In reply to David Matlack

The Intel SDM states that following a VMCLEAR instruction, the VMCS given as an operand to VMCLEAR should be resident in memory. If the CPU caches VMCS data outside of memory, VMCLEAR must flush this cache to memory.

---
 x86/vmx.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
x86/vmx.c +37 -0
index 7daa1d110c82..fd1aa3bc6c2e 100644
@@ -487,6 +487,42 @@ print_vmentry_failure_info(struct vmentry_failure *failure) {
 	}
 }
 
+/*
+ * VMCLEAR should ensures all VMCS state is flushed to the VMCS
+ * region in memory.
+ */
+static void test_vmclear_flushing(void)
+{
+	struct vmcs *vmcs[3] = {};
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(vmcs); i++) {
+		vmcs[i] = alloc_page();
+		memset(vmcs[i], 0, PAGE_SIZE);
+	}
+
+	vmcs[0]->revision_id = basic.revision;
+	assert(!vmcs_clear(vmcs[0]));
+	assert(!make_vmcs_current(vmcs[0]));
+	set_all_vmcs_fields(0x86);
+
+	assert(!vmcs_clear(vmcs[0]));
+	memcpy(vmcs[1], vmcs[0], basic.size);
+	assert(!make_vmcs_current(vmcs[1]));
+	report("test vmclear flush (current VMCS)", check_all_vmcs_fields(0x86));
+
+	set_all_vmcs_fields(0x87);
+	assert(!make_vmcs_current(vmcs[0]));
+	assert(!vmcs_clear(vmcs[1]));
+	memcpy(vmcs[2], vmcs[1], basic.size);
+	assert(!make_vmcs_current(vmcs[2]));
+	report("test vmclear flush (!current VMCS)", check_all_vmcs_fields(0x87));
+
+	for (i = 0; i < ARRAY_SIZE(vmcs); i++) {
+		assert(!vmcs_clear(vmcs[i]));
+		free_page(vmcs[i]);
+	}
+}
 
 static void test_vmclear(void)
 {
@@ -519,6 +555,7 @@ static void test_vmclear(void)
 	/* Valid VMCS */
 	report("test vmclear with valid vmcs region", vmcs_clear(vmcs_root) == 0);
 
+	test_vmclear_flushing();
 }
 
 static void test_vmxoff(void)
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#37 lore raw

PATCH 25/32 x86: ept assertions

In reply to David Matlack

From: Peter Feiner <[email protected]>

The upper bounds on level in set_ept_pte and install_ept_entry were just wrong. There's nothing wrong with setting an EPT PTE at the highest (512GiB) level.

Got rid of unused error return values and replaced them with assertions. Nobody was checking the return values, so these functions were silently failing.

Change-Id I4db1e1c2a0c050f5f69cce28df460b4c8ce10d1c
---
 x86/vmx.c | 23 ++++++++++++-----------
 x86/vmx.h |  2 +-
 2 files changed, 13 insertions(+), 12 deletions(-)
x86/vmx.c +12 -11
index cae650ae2e68..c003d5d11e63 100644
@@ -724,6 +724,9 @@ void install_ept_entry(unsigned long *pml4,
 	unsigned long *pt = pml4;
 	unsigned offset;
 
+	/* EPT only uses 48 bits of GPA. */
+	assert(guest_addr < (1ul << 48));
+
 	for (level = EPT_PAGE_LEVEL; level > pte_level; --level) {
 		offset = (guest_addr >> EPT_LEVEL_SHIFT(level))
 				& EPT_PGDIR_MASK;
@@ -813,17 +816,17 @@ unsigned long get_ept_pte(unsigned long *pml4,
 	unsigned long *pt = pml4, pte;
 	unsigned offset;
 
-	if (level < 1 || level > 3)
-		return -1;
+	assert(level >= 1 && level <= 4);
+
 	for (l = EPT_PAGE_LEVEL; ; --l) {
 		offset = (guest_addr >> EPT_LEVEL_SHIFT(l)) & EPT_PGDIR_MASK;
 		pte = pt[offset];
 		if (!(pte & (EPT_PRESENT)))
-			return 0;
+			return -1;
 		if (l == level)
 			break;
 		if (l < 4 && (pte & EPT_LARGE_PAGE))
-			return pte;
+			return -1;
 		pt = (unsigned long *)(pte & EPT_ADDR_MASK);
 	}
 	offset = (guest_addr >> EPT_LEVEL_SHIFT(l)) & EPT_PGDIR_MASK;
@@ -950,26 +953,24 @@ void ept_sync(int type, u64 eptp)
 	}
 }
 
-int set_ept_pte(unsigned long *pml4, unsigned long guest_addr,
-		int level, u64 pte_val)
+void set_ept_pte(unsigned long *pml4, unsigned long guest_addr,
+		 int level, u64 pte_val)
 {
 	int l;
 	unsigned long *pt = pml4;
 	unsigned offset;
 
-	if (level < 1 || level > 3)
-		return -1;
+	assert(level >= 1 && level <= 4);
+
 	for (l = EPT_PAGE_LEVEL; ; --l) {
 		offset = (guest_addr >> EPT_LEVEL_SHIFT(l)) & EPT_PGDIR_MASK;
 		if (l == level)
 			break;
-		if (!(pt[offset] & (EPT_PRESENT)))
-			return -1;
+		assert(pt[offset] & EPT_PRESENT);
 		pt = (unsigned long *)(pt[offset] & EPT_ADDR_MASK);
 	}
 	offset = (guest_addr >> EPT_LEVEL_SHIFT(l)) & EPT_PGDIR_MASK;
 	pt[offset] = pte_val;
-	return 0;
 }
 
 void vpid_sync(int type, u16 vpid)
x86/vmx.h +1 -1
index 966fff653561..4b899aa69145 100644
@@ -691,7 +691,7 @@ void setup_ept_range(unsigned long *pml4, unsigned long start,
 		     unsigned long len, int map_1g, int map_2m, u64 perm);
 unsigned long get_ept_pte(unsigned long *pml4,
 		unsigned long guest_addr, int level);
-int set_ept_pte(unsigned long *pml4, unsigned long guest_addr,
+void set_ept_pte(unsigned long *pml4, unsigned long guest_addr,
 		int level, u64 pte_val);
 void check_ept_ad(unsigned long *pml4, u64 guest_cr3,
 		  unsigned long guest_addr, int expected_gpa_ad,
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#32 lore raw

PATCH 22/32 lib: add newline to assert_msg

In reply to David Matlack

From: Peter Feiner <[email protected]>

---
 lib/libcflat.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/libcflat.h +1 -1
index 05c18543dd72..7e4bce1dd335 100644
@@ -130,7 +130,7 @@ do {									\
 #define assert_msg(cond, fmt, args...)					\
 do {									\
 	if (!(cond)) {							\
-		printf("%s:%d: assert failed: %s: " fmt,		\
+		printf("%s:%d: assert failed: %s: " fmt "\n",		\
 		       __FILE__, __LINE__, #cond, ## args);		\
 		dump_stack();						\
 		abort();						\
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#39 lore raw

PATCH 26/32 x86: setup_ept code motion

In reply to David Matlack

From: Peter Feiner <[email protected]>

---
 x86/vmx_tests.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
x86/vmx_tests.c +8 -6
index 5758b6becfa6..be01709d3057 100644
@@ -940,12 +940,20 @@ static int insn_intercept_exit_handler()
 }
 
 
+/* Enables EPT and sets up the identity map. */
 static int setup_ept(bool enable_ad)
 {
 	int support_2m;
 	unsigned long end_of_memory;
 	u32 ctrl_cpu[2];
 
+	if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) ||
+	    !(ctrl_cpu_rev[1].clr & CPU_EPT)) {
+		printf("\tEPT is not supported");
+		return 1;
+	}
+
+
 	if (!(ept_vpid.val & EPT_CAP_UC) &&
 			!(ept_vpid.val & EPT_CAP_WB)) {
 		printf("\tEPT paging-structure memory type "
@@ -992,12 +1000,6 @@ static int apic_version;
 
 static int ept_init_common(bool have_ad)
 {
-	if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) ||
-	    !(ctrl_cpu_rev[1].clr & CPU_EPT)) {
-		printf("\tEPT is not supported");
-		return VMX_TEST_EXIT;
-	}
-
 	if (setup_ept(have_ad))
 		return VMX_TEST_EXIT;
 	data_page1 = alloc_page();
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#18 lore raw

PATCH 17/32 x86: v2 vmx test framework

In reply to David Matlack

From: Peter Feiner <[email protected]>

---
 x86/unittests.cfg |   6 ++
 x86/vmx.c         | 179 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 x86/vmx.h         |  66 ++++++++++++++++++++
 x86/vmx_tests.c   |  94 ++++++++++++++++++++++++++++
 4 files changed, 343 insertions(+), 2 deletions(-)
x86/unittests.cfg +6 -0
index 8a89eb0ef0ad..f79688af93f8 100644
@@ -362,6 +362,12 @@ extra_params = -cpu host,+vmx -append exit_monitor_from_l2_test
 arch = x86_64
 groups = vmx
 
+[vmx_v2]
+file = vmx.flat
+extra_params = -cpu host,+vmx -append "v2_null_test v2_multiple_entries_test fixture_test_case1 fixture_test_case2"
+arch = x86_64
+groups = vmx
+
 [debug]
 file = debug.flat
 arch = x86_64
x86/vmx.c +177 -2

262 lines not on this page · raw message

x86/vmx.h +66 -0

86 lines not on this page · raw message

x86/vmx_tests.c +94 -0

105 lines not on this page · raw message

Signature · 1 line
2.12.2.816.g2cccc81164-goog
#35 lore raw

PATCH 23/32 lib: x86: store free pages in ascending order

In reply to David Matlack

From: Peter Feiner <[email protected]>

Makes allocation of contiguous physical pages simpler.

---
 lib/x86/vm.c | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)
lib/x86/vm.c +22 -5
index cda4c5f46da3..8bd0c88e141e 100644
@@ -7,12 +7,29 @@ static void *vfree_top = 0;
 
 static void free_memory(void *mem, unsigned long size)
 {
-    while (size >= PAGE_SIZE) {
-	*(void **)mem = free;
+	void *end;
+
+	assert_msg((unsigned long) mem % PAGE_SIZE == 0,
+		   "mem not page aligned: %p", mem);
+
+	assert_msg(size % PAGE_SIZE == 0, "size not page aligned: 0x%lx", size);
+
+	assert_msg(size == 0 || mem + size > mem,
+		   "mem + size overflow: %p + 0x%lx", mem, size);
+
+	if (size == 0) {
+		free = NULL;
+		return;
+	}
+
 	free = mem;
-	mem += PAGE_SIZE;
-	size -= PAGE_SIZE;
-    }
+	end = mem + size;
+	while (mem + PAGE_SIZE != end) {
+		*(void **)mem = (mem + PAGE_SIZE);
+		mem += PAGE_SIZE;
+	}
+
+	*(void **)mem = NULL;
 }
 
 void *alloc_page()
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#40 lore raw

PATCH 27/32 x86: 2GiB RAM for vmx tests

In reply to David Matlack

From: Peter Feiner <[email protected]>

Upcoming test allocates a 1GiB huge page. The VM needs more memory and the default container limit of 2 GiB isn't enough.

---
 x86/unittests.cfg | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/unittests.cfg +1 -1
index f79688af93f8..587bf40e8bc9 100644
@@ -196,7 +196,7 @@ arch = x86_64
 
 [vmx]
 file = vmx.flat
-extra_params = -cpu host,+vmx -append -exit_monitor_from_l2_test
+extra_params = -cpu host,+vmx -m 2048 -append -exit_monitor_from_l2_test
 arch = x86_64
 groups = vmx
 
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#36 lore raw

PATCH 24/32 lib: x86: multi-page allocator

In reply to David Matlack

From: Peter Feiner <[email protected]>

Need this for testing huge pages.

Change-Id Iecf4f916a3d3d24844957dcd4e81c8aa10b1cdda
---
 lib/x86/vm.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/x86/vm.h |  1 +
 2 files changed, 58 insertions(+)
lib/x86/vm.c +57 -0
index 8bd0c88e141e..438ab9c70ffa 100644
@@ -45,6 +45,63 @@ void *alloc_page()
     return p;
 }
 
+/*
+ * Allocates (1 << order) physically contiguous and naturally aligned pages.
+ * Returns NULL if there's no memory left.
+ */
+void *alloc_pages(unsigned long order)
+{
+	/* Generic list traversal. */
+	void *prev;
+	void *curr = NULL;
+	void *next = free;
+
+	/* Looking for a run of length (1 << order). */
+	unsigned long run = 0;
+	const unsigned long n = 1ul << order;
+	const unsigned long align_mask = (n << PAGE_SHIFT) - 1;
+	void *run_start = NULL;
+	void *run_prev = NULL;
+	unsigned long run_next_pa = 0;
+	unsigned long pa;
+
+	assert(order < sizeof(unsigned long) * 8);
+
+	for (;;) {
+		prev = curr;
+		curr = next;
+		next = curr ? *((void **) curr) : NULL;
+
+		if (!curr)
+			return 0;
+
+		pa = virt_to_phys(curr);
+
+		if (run == 0) {
+			if (!(pa & align_mask)) {
+				run_start = curr;
+				run_prev = prev;
+				run_next_pa = pa + PAGE_SIZE;
+				run = 1;
+			}
+		} else if (pa == run_next_pa) {
+			run_next_pa += PAGE_SIZE;
+			run += 1;
+		} else {
+			run = 0;
+		}
+
+		if (run == n) {
+			if (run_prev)
+				*((void **) run_prev) = next;
+			else
+				free = next;
+			return run_start;
+		}
+	}
+}
+
+
 void free_page(void *page)
 {
     *(void **)page = free;
lib/x86/vm.h +1 -0
index 6a4384f5a48d..64e4b566333f 100644
@@ -22,6 +22,7 @@ unsigned long *install_pte(unsigned long *cr3,
                            unsigned long *pt_page);
 
 void *alloc_page();
+void *alloc_pages(unsigned long order);
 void free_page(void *page);
 
 unsigned long *install_large_page(unsigned long *cr3,unsigned long phys,
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#43 lore raw

PATCH 28/32 x86: ept capability utilities

In reply to David Matlack

From: Peter Feiner <[email protected]>

---
 x86/vmx.c       | 30 ++++++++++++++++++++++++++++++
 x86/vmx.h       |  6 ++++++
 x86/vmx_tests.c |  6 ++----
 3 files changed, 38 insertions(+), 4 deletions(-)
x86/vmx.c +30 -0
index c003d5d11e63..f7a34d20ab6a 100644
@@ -973,6 +973,36 @@ void set_ept_pte(unsigned long *pml4, unsigned long guest_addr,
 	pt[offset] = pte_val;
 }
 
+bool ept_2m_supported(void)
+{
+	return ept_vpid.val & EPT_CAP_2M_PAGE;
+}
+
+bool ept_1g_supported(void)
+{
+	return ept_vpid.val & EPT_CAP_1G_PAGE;
+}
+
+bool ept_huge_pages_supported(int level)
+{
+	if (level == 2)
+		return ept_2m_supported();
+	else if (level == 3)
+		return ept_1g_supported();
+	else
+		return false;
+}
+
+bool ept_execute_only_supported(void)
+{
+	return ept_vpid.val & EPT_CAP_WT;
+}
+
+bool ept_ad_bits_supported(void)
+{
+	return ept_vpid.val & EPT_CAP_AD_FLAG;
+}
+
 void vpid_sync(int type, u16 vpid)
 {
 	switch(type) {
x86/vmx.h +6 -0
index 4b899aa69145..890cdcc9e67f 100644
@@ -699,6 +699,12 @@ void check_ept_ad(unsigned long *pml4, u64 guest_cr3,
 void clear_ept_ad(unsigned long *pml4, u64 guest_cr3,
 		  unsigned long guest_addr);
 
+bool ept_2m_supported(void);
+bool ept_1g_supported(void);
+bool ept_huge_pages_supported(int level);
+bool ept_execute_only_supported(void);
+bool ept_ad_bits_supported(void);
+
 void enter_guest(void);
 
 typedef void (*test_guest_func)(void);
x86/vmx_tests.c +2 -4
index be01709d3057..bf45bd2566ca 100644
@@ -943,7 +943,6 @@ static int insn_intercept_exit_handler()
 /* Enables EPT and sets up the identity map. */
 static int setup_ept(bool enable_ad)
 {
-	int support_2m;
 	unsigned long end_of_memory;
 	u32 ctrl_cpu[2];
 
@@ -983,15 +982,14 @@ static int setup_ept(bool enable_ad)
 	if (enable_ad)
 		eptp |= EPTP_AD_FLAG;
 	vmcs_write(EPTP, eptp);
-	support_2m = !!(ept_vpid.val & EPT_CAP_2M_PAGE);
 	end_of_memory = fwcfg_get_u64(FW_CFG_RAM_SIZE);
 	if (end_of_memory < (1ul << 32))
 		end_of_memory = (1ul << 32);
 	/* Cannot use large EPT pages if we need to track EPT
 	 * accessed/dirty bits at 4K granularity.
 	 */
-	setup_ept_range(pml4, 0, end_of_memory,
-			0, !enable_ad && support_2m,
+	setup_ept_range(pml4, 0, end_of_memory, 0,
+			!enable_ad && ept_2m_supported(),
 			EPT_WA | EPT_RA | EPT_EA);
 	return 0;
 }
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#44 lore raw

PATCH 29/32 lib: x86: page table utilities

In reply to David Matlack

From: Peter Feiner <[email protected]>

Convenience functions for manipulating and querying page tables.

---
 lib/x86/vm.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++-----------
 lib/x86/vm.h | 27 +++++++++++++++++
 2 files changed, 108 insertions(+), 18 deletions(-)
lib/x86/vm.c +81 -18
index 438ab9c70ffa..d52bb53fcf5e 100644
@@ -139,23 +139,62 @@ unsigned long *install_pte(unsigned long *cr3,
     return &pt[offset];
 }
 
+/*
+ * Finds last PTE in the mapping of @virt that's at or above @lowest_level. The
+ * returned PTE isn't necessarily present, but its parent is.
+ */
+struct pte_search find_pte_level(unsigned long *cr3, void *virt,
+				 int lowest_level)
+{
+	unsigned long *pt = cr3, pte;
+	unsigned offset;
+	unsigned long shift;
+	struct pte_search r;
+
+	assert(lowest_level >= 1 && lowest_level <= PAGE_LEVEL);
+
+	for (r.level = PAGE_LEVEL;; --r.level) {
+		shift = (r.level - 1) * PGDIR_WIDTH + 12;
+		offset = ((unsigned long)virt >> shift) & PGDIR_MASK;
+		r.pte = &pt[offset];
+		pte = *r.pte;
+
+		if (!(pte & PT_PRESENT_MASK))
+			return r;
+
+		if ((r.level == 2 || r.level == 3) && (pte & PT_PAGE_SIZE_MASK))
+			return r;
+
+		if (r.level == lowest_level)
+			return r;
+
+		pt = phys_to_virt(pte & 0xffffffffff000ull);
+	}
+}
+
+/*
+ * Returns the leaf PTE in the mapping of @virt (i.e., 4K PTE or a present huge
+ * PTE). Returns NULL if no leaf PTE exists.
+ */
 unsigned long *get_pte(unsigned long *cr3, void *virt)
 {
-    int level;
-    unsigned long *pt = cr3, pte;
-    unsigned offset;
+	struct pte_search search;
 
-    for (level = PAGE_LEVEL; level > 1; --level) {
-	offset = ((unsigned long)virt >> (((level-1) * PGDIR_WIDTH) + 12)) & PGDIR_MASK;
-	pte = pt[offset];
-	if (!(pte & PT_PRESENT_MASK))
-	    return NULL;
-	if (level == 2 && (pte & PT_PAGE_SIZE_MASK))
-	    return &pt[offset];
-	pt = phys_to_virt(pte & PT_ADDR_MASK);
-    }
-    offset = ((unsigned long)virt >> (((level-1) * PGDIR_WIDTH) + 12)) & PGDIR_MASK;
-    return &pt[offset];
+	search = find_pte_level(cr3, virt, 1);
+	return found_leaf_pte(search) ? search.pte : NULL;
+}
+
+/*
+ * Returns the PTE in the mapping of @virt at the given level @pte_level.
+ * Returns NULL if the PT at @pte_level isn't present (i.e., the mapping at
+ * @pte_level - 1 isn't present).
+ */
+unsigned long *get_pte_level(unsigned long *cr3, void *virt, int pte_level)
+{
+	struct pte_search search;
+
+	search = find_pte_level(cr3, virt, pte_level);
+	return search.level == pte_level ? search.pte : NULL;
 }
 
 unsigned long *install_large_page(unsigned long *cr3,
@@ -173,6 +212,33 @@ unsigned long *install_page(unsigned long *cr3,
     return install_pte(cr3, 1, virt, phys | PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK, 0);
 }
 
+void install_pages(unsigned long *cr3, unsigned long phys, unsigned long len,
+		   void *virt)
+{
+	unsigned long max = (u64)len + (u64)phys;
+	assert(phys % PAGE_SIZE == 0);
+	assert((unsigned long) virt % PAGE_SIZE == 0);
+	assert(len % PAGE_SIZE == 0);
+
+	while (phys + PAGE_SIZE <= max) {
+		install_page(cr3, phys, virt);
+		phys += PAGE_SIZE;
+		virt = (char *) virt + PAGE_SIZE;
+	}
+}
+
+bool any_present_pages(unsigned long *cr3, void *virt, unsigned long len)
+{
+	unsigned long max = (unsigned long) virt + len;
+	unsigned long curr;
+
+	for (curr = (unsigned long) virt; curr < max; curr += PAGE_SIZE) {
+		unsigned long *ptep = get_pte(cr3, (void *) curr);
+		if (ptep && (*ptep & PT_PRESENT_MASK))
+			return true;
+	}
+	return false;
+}
 
 static void setup_mmu_range(unsigned long *cr3, unsigned long start,
 			    unsigned long len)
@@ -184,10 +250,7 @@ static void setup_mmu_range(unsigned long *cr3, unsigned long start,
 		install_large_page(cr3, phys, (void *)(ulong)phys);
 		phys += LARGE_PAGE_SIZE;
 	}
-	while (phys + PAGE_SIZE <= max) {
-		install_page(cr3, phys, (void *)(ulong)phys);
-		phys += PAGE_SIZE;
-	}
+	install_pages(cr3, phys, max - phys, (void *)(ulong)phys);
 }
 
 static void setup_mmu(unsigned long len)
lib/x86/vm.h +27 -0
index 64e4b566333f..3522ba8b99b0 100644
@@ -14,7 +14,27 @@ void *alloc_vpage(void);
 void *alloc_vpages(ulong nr);
 uint64_t virt_to_phys_cr3(void *mem);
 
+struct pte_search {
+	int level;
+	unsigned long *pte;
+};
+
+static inline bool found_huge_pte(struct pte_search search)
+{
+	return (search.level == 2 || search.level == 3) &&
+	       (*search.pte & PT_PRESENT_MASK) &&
+	       (*search.pte & PT_PAGE_SIZE_MASK);
+}
+
+static inline bool found_leaf_pte(struct pte_search search)
+{
+	return search.level == 1 || found_huge_pte(search);
+}
+
+struct pte_search find_pte_level(unsigned long *cr3, void *virt,
+				 int lowest_level);
 unsigned long *get_pte(unsigned long *cr3, void *virt);
+unsigned long *get_pte_level(unsigned long *cr3, void *virt, int pte_level);
 unsigned long *install_pte(unsigned long *cr3,
                            int pte_level,
                            void *virt,
@@ -28,5 +48,12 @@ void free_page(void *page);
 unsigned long *install_large_page(unsigned long *cr3,unsigned long phys,
                                   void *virt);
 unsigned long *install_page(unsigned long *cr3, unsigned long phys, void *virt);
+void install_pages(unsigned long *cr3, unsigned long phys, unsigned long len,
+		   void *virt);
+bool any_present_pages(unsigned long *cr3, void *virt, unsigned long len);
 
+static inline void *current_page_table(void)
+{
+	return phys_to_virt(read_cr3());
+}
 #endif
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#45 lore raw

PATCH 30/32 x86: ept access tests

In reply to David Matlack

From: Peter Feiner <[email protected]>

---
 lib/x86/asm/page.h |   2 +
 x86/unittests.cfg  | 114 ++++++++
 x86/vmx.h          |   3 +
 x86/vmx_tests.c    | 843 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 962 insertions(+)
lib/x86/asm/page.h +2 -0
index c43bab28ca2e..562594df2450 100644
@@ -31,6 +31,8 @@
 #define PT64_NX_MASK		(1ull << 63)
 #define PT_ADDR_MASK		GENMASK_ULL(51, 12)
 
+#define PT_AD_MASK              (PT_ACCESSED_MASK | PT_DIRTY_MASK)
+
 #ifdef __x86_64__
 #define	PAGE_LEVEL	4
 #define	PGDIR_WIDTH	9
x86/unittests.cfg +114 -0

120 lines not on this page · raw message

x86/vmx.h +3 -0
index 890cdcc9e67f..2390c4a99901 100644
@@ -551,12 +551,15 @@ enum vm_instruction_error_number {
 #define EPT_VLT_PERM_RD		(1 << 3)
 #define EPT_VLT_PERM_WR		(1 << 4)
 #define EPT_VLT_PERM_EX		(1 << 5)
+#define EPT_VLT_PERMS		(EPT_VLT_PERM_RD | EPT_VLT_PERM_WR | \
+				 EPT_VLT_PERM_EX)
 #define EPT_VLT_LADDR_VLD	(1 << 7)
 #define EPT_VLT_PADDR		(1 << 8)
 
 #define MAGIC_VAL_1		0x12345678ul
 #define MAGIC_VAL_2		0x87654321ul
 #define MAGIC_VAL_3		0xfffffffful
+#define MAGIC_VAL_4		0xdeadbeeful
 
 #define INVEPT_SINGLE		1
 #define INVEPT_GLOBAL		2
x86/vmx_tests.c +843 -0

860 lines not on this page · raw message

Signature · 1 line
2.12.2.816.g2cccc81164-goog
#46 lore raw

PATCH 31/32 x86: force ept 2m test

In reply to David Matlack

From: Peter Feiner <[email protected]>

EPT tests don't exercise 2m support unless it's advertised properly. This test asserts that we actually advertise it and that some basic 2m stuff works.

---
 x86/unittests.cfg |  6 ++++++
 x86/vmx_tests.c   | 13 +++++++++++++
 2 files changed, 19 insertions(+)
x86/unittests.cfg +6 -0
index bc245651ce80..09b2252ac2af 100644
@@ -482,6 +482,12 @@ extra_params = -cpu host,+vmx -m 2048 -append ept_access_test_paddr_not_present_
 arch = x86_64
 groups = vmx
 
+[vmx_ept_access_test_force_2m_page]
+file = vmx.flat
+extra_params = -cpu host,+vmx -m 2048 -append ept_access_test_force_2m_page
+arch = x86_64
+groups = vmx
+
 [debug]
 file = debug.flat
 arch = x86_64
x86/vmx_tests.c +13 -0
index c4cd3f96001d..d59ca0baf48a 100644
@@ -2807,6 +2807,18 @@ static void ept_access_test_paddr_not_present_page_fault(void)
 	 */
 }
 
+static void ept_access_test_force_2m_page(void)
+{
+	ept_access_test_setup();
+
+	TEST_ASSERT_EQ(ept_2m_supported(), true);
+	ept_allowed_at_level_mkhuge(true, 2, 0, 0, OP_READ);
+	ept_violation_at_level_mkhuge(true, 2, EPT_PRESENT, EPT_RA, OP_WRITE,
+				      EPT_VLT_WR | EPT_VLT_PERM_RD |
+				      EPT_VLT_LADDR_VLD | EPT_VLT_PADDR);
+	ept_misconfig_at_level_mkhuge(true, 2, EPT_PRESENT, EPT_WA);
+}
+
 #define TEST(name) { #name, .v2 = name }
 
 /* name/init/guest_main/exit_handler/syscall_handler/guest_regs */
@@ -2866,5 +2878,6 @@ struct vmx_test vmx_tests[] = {
 	TEST(ept_access_test_paddr_read_execute_ad_disabled),
 	TEST(ept_access_test_paddr_read_execute_ad_enabled),
 	TEST(ept_access_test_paddr_not_present_page_fault),
+	TEST(ept_access_test_force_2m_page),
 	{ NULL, NULL, NULL, NULL, NULL, {0} },
 };
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#47 lore raw

PATCH 32/32 lib: add report_pass

In reply to David Matlack

The assertion macros in the vmx tests only call report() if the test fails. This avoids a lot of logging (e.g. vmx_ept_access_test_reserved_bits checks 3460 assertions). However the test runner interprets passing runs as "skipped" because it looks like no testcases were run. Add a function called report_pass(), which lets tests report that a test passed without printing anything to the console.

---
 lib/libcflat.h | 1 +
 lib/report.c   | 7 +++++++
 x86/vmx.h      | 3 +++
 3 files changed, 11 insertions(+)
lib/libcflat.h +1 -0
index 7e4bce1dd335..b1ea5e607033 100644
@@ -104,6 +104,7 @@ extern void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...);
 extern void report_abort(const char *msg_fmt, ...);
 extern void report_skip(const char *msg_fmt, ...);
 extern void report_info(const char *msg_fmt, ...);
+extern void report_pass(void);
 extern int report_summary(void);
 
 bool simple_glob(const char *text, const char *pattern);
lib/report.c +7 -0
index 1033f1e44e99..aa23b6582512 100644
@@ -19,6 +19,13 @@ static struct spinlock lock;
 
 #define PREFIX_DELIMITER ": "
 
+void report_pass(void)
+{
+	spin_lock(&lock);
+	tests++;
+	spin_unlock(&lock);
+}
+
 void report_prefix_pushf(const char *prefix_fmt, ...)
 {
 	va_list va;
x86/vmx.h +3 -0
index 2390c4a99901..4194429e07b8 100644
@@ -726,6 +726,7 @@ do { \
 		dump_stack(); \
 		__abort_test(); \
 	} \
+	report_pass(); \
 } while (0)
 
 #define TEST_ASSERT_MSG(cond, fmt, args...) \
@@ -736,6 +737,7 @@ do { \
 		dump_stack(); \
 		__abort_test(); \
 	} \
+	report_pass(); \
 } while (0)
 
 #define __TEST_EQ(a, b, a_str, b_str, assertion, fmt, args...) \
@@ -759,6 +761,7 @@ do { \
 		if (assertion) \
 			__abort_test(); \
 	} \
+	report_pass(); \
 } while (0)
 
 #define TEST_ASSERT_EQ(a, b) __TEST_EQ(a, b, #a, #b, 1, "")
Signature · 1 line
2.12.2.816.g2cccc81164-goog
#48 lore raw
In reply to David Matlack

2017-04-21 8:49 GMT+08:00 David Matlack <[email protected]>:

Quoted 10 lines

Patches 1-6 split out the VMX tests into their own unit tests.

Patches 7-12 add new VMX unit tests.

Patches 13-17 implement a new VMX test framework ("v2"). Instead of registering a main function and an exit handler, entering and exiting guest mode is accomplished by calling a function, enter_guest().

Patches 18-22 add some assert and printf enhancements to lib/.

Quoted 7 lines

Patches 23-32 add EPT access tests, using the v2 framework.

4 of the EPT access tests reveal KVM bugs (4.11-rc6, on a Haswell CPU):
vmx_ept_access_test_execute_only vmx_ept_access_test_paddr_not_present_ad_disabled vmx_ept_access_test_paddr_read_only_ad_disabled vmx_ept_access_test_paddr_read_execute_ad_disabled

If the bugs are still present after apply Paolo's nested EPT A/D bit? I can look into it if they are still present.

Regards,
Wanpeng Li

Quoted 59 lines

The rest of the new tests in this series pass.

David Matlack (8):
x86: move basic vmx tests into separate test cases
x86: add missing vmx test cases to unittests.cfg
x86: vmx: filter exit_monitor_from_l2_test from full vmx test
x86: basic vmwrite/vmread test
x86: vmcs lifecycle test
x86: test VMCS in memory after VMCLEAR
x86: test VMPTRLD does not drop VMWRITEs
lib: add report_pass

GanShun (1):
x86: Adding gtest to check correct instruction error is returned

Peter Feiner (23):
x86: add test filter to vmx.flat.
x86: add config for each vmx unit test case
lib: better test name filtering
x86: test exit while vmcs02 is loaded
x86: don't special case vmx null test
x86: factor out vmx_enter_guest
x86: binstr utility function
x86: vmx exit reason descriptions
x86: v2 vmx test framework
lib: provide stdio.h
lib: added assert_msg macro
lib: fix *printf return value
lib: printf-style report prefixes
lib: add newline to assert_msg
lib: x86: store free pages in ascending order
lib: x86: multi-page allocator
x86: ept assertions
x86: setup_ept code motion
x86: 2GiB RAM for vmx tests
x86: ept capability utilities
lib: x86: page table utilities
x86: ept access tests
x86: force ept 2m test

 lib/libcflat.h     |  21 ++
 lib/printf.c       |  31 +-
 lib/report.c       |  45 ++-
 lib/stdio.h        |   4 +
 lib/string.c       |  41 +++
 lib/x86/asm/page.h |   2 +
 lib/x86/vm.c       | 183 ++++++++--
 lib/x86/vm.h       |  28 ++
 x86/unittests.cfg  | 291 +++++++++++++++-
 x86/vmx.c          | 833 +++++++++++++++++++++++++++++++++++++++-----
 x86/vmx.h          | 151 +++++++-
 x86/vmx_tests.c    | 989 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 12 files changed, 2493 insertions(+), 126 deletions(-)
 create mode 100644 lib/stdio.h

--
2.12.2.816.g2cccc81164-goog

#20 lore raw

Re: [kvm-unit-tests PATCH 18/32] lib: provide stdio.h

In reply to David Matlack
Quoted 14 lines · David Matlack

From: Peter Feiner <[email protected]>

kvm-unit-tests builds with the odd combination of system include files but not system libraries. That is, -nostdlib is specified but -nostdinc isn't! Problems arise when the include files don't match the libraries, e.g., printf() in some versions of glibc linking in a __printf_chk symbol that libcflat doesn't have. This patch fixes the problem with printf by pointing '#include "stdio.h"' to lib/libcflat.h, where a printf declaration that libcflat actually implements exists, rather than letting /usr/include/stdio.h get included. This patch does not solve the general problem of potential mismatches between libcflat and the compiler's notion standard include files; doing so would require rewriting stdarg.h, stdint.h, and several others in libcflat.

Yeah, I'm not sure rewriting those would be worth being able to add -nostdinc. When we cross compile ARM and POWER those few headers are some of the ones provided by the cross compiler.

Quoted 21 lines
---
 lib/stdio.h | 4 ++++
 1 file changed, 4 insertions(+)
 create mode 100644 lib/stdio.h
lib/stdio.h new file +4 -0
new file mode 100644
index 000000000000..7b30db93f7f1
@@ -0,0 +1,4 @@
+#ifndef LIBCFLAT_STDIO_H
+#define LIBCFLAT_STDIO_H
+#include "libcflat.h"
+#endif
Signature · 1 line
2.12.2.816.g2cccc81164-goog

I'd rather we move the stdio functions (just printf?) into stdio.h rather than redirect to libcflat. We can include stdio.h from libcflat.h to avoid updating all the unit tests for now though.

Thanks,
drew

Reviewed-by on v1 20/32
#26 lore raw

Re: [kvm-unit-tests PATCH 20/32] lib: fix *printf return value

In reply to David Matlack
Quoted 26 lines · David Matlack

From: Peter Feiner <[email protected]>

Was returning the number of characters printed plus, incorrectly, one for the null terminator.

---
 lib/printf.c | 1 -
 1 file changed, 1 deletion(-)
lib/printf.c +0 -1
index cecbeadc4440..aca2920887c1 100644
@@ -221,7 +221,6 @@ int vsnprintf(char *buf, int size, const char *fmt, va_list va)
 	}
     }
     *s.buffer = 0;
-    ++s.added;
     return s.added;
 }
 
Signature · 1 line
2.12.2.816.g2cccc81164-goog
Reviewed-by on v1 21/32
#28 lore raw

Re: [kvm-unit-tests PATCH 21/32] lib: printf-style report prefixes

In reply to David Matlack
Quoted 49 lines · David Matlack

From: Peter Feiner <[email protected]>

---
 lib/libcflat.h |  1 +
 lib/report.c   | 38 +++++++++++++++++++++++++++++++++-----
 2 files changed, 34 insertions(+), 5 deletions(-)
lib/libcflat.h +1 -0
index 5d356df75f1f..05c18543dd72 100644
@@ -96,6 +96,7 @@ extern int vsnprintf(char *buf, int size, const char *fmt, va_list va)
 extern int vprintf(const char *fmt, va_list va)
 					__attribute__((format(printf, 1, 0)));
 
+void report_prefix_pushf(const char *prefix_fmt, ...);
 extern void report_prefix_push(const char *prefix);
 extern void report_prefix_pop(void);
 extern void report(const char *msg_fmt, bool pass, ...);
lib/report.c +15 -3
index e24e81382f9e..1033f1e44e99 100644
@@ -17,14 +17,42 @@ static unsigned int tests, failures, xfailures, skipped;
 static char prefixes[256];
 static struct spinlock lock;
 
-void report_prefix_push(const char *prefix)
+#define PREFIX_DELIMITER ": "
+
+void report_prefix_pushf(const char *prefix_fmt, ...)
 {
+	va_list va;
+	int len;
+	int start;
+
 	spin_lock(&lock);
-	strcat(prefixes, prefix);
-	strcat(prefixes, ": ");
+
+	len = strlen(prefixes);
+	assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
+	start = len;
+
+	va_start(va, prefix_fmt);
+	len += vsnprintf(&prefixes[len], sizeof(prefixes) - len, prefix_fmt,
+			 va);

lib/report.c static unsigned int tests, failures, xfailures, skipped; · line 46

Quoted 10 lines
lib/report.c +10 -0
+	va_end(va);
+	assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
+
+	assert_msg(!strstr(&prefixes[start], PREFIX_DELIMITER),
+		   "Prefix \"%s\" contains delimiter \"" PREFIX_DELIMITER "\"",
+		   &prefixes[start]);
+
+	len += snprintf(&prefixes[len], sizeof(prefixes) - len,
+			PREFIX_DELIMITER);
+	assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));

I don't think this last assert is necessary.

Quoted 27 lines
lib/report.c +8 -2
+
 	spin_unlock(&lock);
 }
 
+void report_prefix_push(const char *prefix)
+{
+	report_prefix_pushf("%s", prefix);
+}
+
 void report_prefix_pop(void)
 {
 	char *p, *q;
@@ -34,9 +62,9 @@ void report_prefix_pop(void)
 	if (!*prefixes)
 		return;
 
-	for (p = prefixes, q = strstr(p, ": ") + 2;
+	for (p = prefixes, q = strstr(p, PREFIX_DELIMITER) + 2;
 			*q;
-			p = q, q = strstr(p, ": ") + 2)
+			p = q, q = strstr(p, PREFIX_DELIMITER) + 2)
 		;
 	*p = '\0';
 
Signature · 1 line
2.12.2.816.g2cccc81164-goog

Otherwise

#33 lore raw

Re: [kvm-unit-tests PATCH 22/32] lib: add newline to assert_msg

In reply to David Matlack
Quoted 24 lines · David Matlack

From: Peter Feiner <[email protected]>

---
 lib/libcflat.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/libcflat.h +1 -1
index 05c18543dd72..7e4bce1dd335 100644
@@ -130,7 +130,7 @@ do {									\
 #define assert_msg(cond, fmt, args...)					\
 do {									\
 	if (!(cond)) {							\
-		printf("%s:%d: assert failed: %s: " fmt,		\
+		printf("%s:%d: assert failed: %s: " fmt "\n",		\
 		       __FILE__, __LINE__, #cond, ## args);		\
 		dump_stack();						\
 		abort();						\
Signature · 1 line
2.12.2.816.g2cccc81164-goog

Should be squashed in with the introduction of assert_msg

Thanks,
drew

maintainer
#21 lore raw

Re: [kvm-unit-tests PATCH 18/32] lib: provide stdio.h

In reply to Andrew Jones
Quoted 14 lines · Andrew Jones
Quoted 13 lines

kvm-unit-tests builds with the odd combination of system include files but not system libraries. That is, -nostdlib is specified but -nostdinc isn't! Problems arise when the include files don't match the libraries, e.g., printf() in some versions of glibc linking in a __printf_chk symbol that libcflat doesn't have. This patch fixes the problem with printf by pointing '#include "stdio.h"' to lib/libcflat.h, where a printf declaration that libcflat actually implements exists, rather than letting /usr/include/stdio.h get included. This patch does not solve the general problem of potential mismatches between libcflat and the compiler's notion standard include files; doing so would require rewriting stdarg.h, stdint.h, and several others in libcflat.

Quoted 3 lines

Yeah, I'm not sure rewriting those would be worth being able to add -nostdinc. When we cross compile ARM and POWER those few headers are some of the ones provided by the cross compiler.

I think -sysroot=pwd would keep the usual compiler include path, and remove /usr/include.

Paolo

maintainer
#34 lore raw

Re: [kvm-unit-tests PATCH 22/32] lib: add newline to assert_msg

In reply to Andrew Jones
Quoted 19 lines · Andrew Jones
Quoted 18 lines · David Matlack

From: Peter Feiner <[email protected]>

---
 lib/libcflat.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/libcflat.h +1 -1
index 05c18543dd72..7e4bce1dd335 100644
@@ -130,7 +130,7 @@ do {									\
 #define assert_msg(cond, fmt, args...)					\
 do {									\
 	if (!(cond)) {							\
-		printf("%s:%d: assert failed: %s: " fmt,		\
+		printf("%s:%d: assert failed: %s: " fmt "\n",		\
Quoted 8 lines
Quoted 6 lines
lib/libcflat.h +0 -0
 		       __FILE__, __LINE__, #cond, ## args);		\
 		dump_stack();						\
 		abort();						\
Signature · 1 line
2.12.2.816.g2cccc81164-goog

Should be squashed in with the introduction of assert_msg

Indeed, can do when applying too.

Paolo

maintainer
#41 lore raw

Re: [kvm-unit-tests PATCH 27/32] x86: 2GiB RAM for vmx tests

In reply to David Matlack
Quoted 4 lines · David Matlack

From: Peter Feiner <[email protected]>

Upcoming test allocates a 1GiB huge page. The VM needs more memory and the default container limit of 2 GiB isn't enough.

Google-y commit message? :)

Paolo

Quoted 16 lines
---
 x86/unittests.cfg | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/unittests.cfg +1 -1
index f79688af93f8..587bf40e8bc9 100644
@@ -196,7 +196,7 @@ arch = x86_64
 
 [vmx]
 file = vmx.flat
-extra_params = -cpu host,+vmx -append -exit_monitor_from_l2_test
+extra_params = -cpu host,+vmx -m 2048 -append -exit_monitor_from_l2_test
maintainer
#7 lore raw

Re: [kvm-unit-tests PATCH 06/32] x86: vmx: filter exit_monitor_from_l2_test from full vmx test

In reply to David Matlack
Quoted 6 lines · David Matlack

Filter out exit_monitor_from_l2_test when running the full vmx test suite (i.e. the "[vmx]" test suite in unittest.cfg). Since exit_monitor_from_l2_test causes the VM to shutdown, many of the tests in vmx_tests.c end up getting skipped.

This can be squashed into patch 7, since the test doesn't exist yet.

Paolo

Quoted 18 lines
---
 x86/unittests.cfg | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/unittests.cfg +1 -1
index 02dd6b235739..8011429d2307 100644
@@ -196,7 +196,7 @@ arch = x86_64
 
 [vmx]
 file = vmx.flat
-extra_params = -cpu host,+vmx
+extra_params = -cpu host,+vmx -append -exit_monitor_from_l2_test
 arch = x86_64
 groups = vmx
 
maintainer
#38 lore raw

Re: [kvm-unit-tests PATCH 25/32] x86: ept assertions

In reply to David Matlack
Quoted 5 lines · David Matlack

From: Peter Feiner <[email protected]>

The upper bounds on level in set_ept_pte and install_ept_entry were just wrong. There's nothing wrong with setting an EPT PTE at the highest (512GiB) level.

Right, I think the idea was that the biggest supported page size is 1GB, not 512, but setting up "super-huge" EPT pages can even be desirable for testing purposes.

Paolo

Quoted 6 lines

Got rid of unused error return values and replaced them with assertions. Nobody was checking the return values, so these functions were silently failing.

Change-Id I4db1e1c2a0c050f5f69cce28df460b4c8ce10d1c
#42 lore raw

Re: [kvm-unit-tests PATCH 27/32] x86: 2GiB RAM for vmx tests

In reply to Paolo Bonzini
Quoted 9 lines · Paolo Bonzini
Quoted 4 lines · David Matlack

From: Peter Feiner <[email protected]>

Upcoming test allocates a 1GiB huge page. The VM needs more memory and the default container limit of 2 GiB isn't enough.

Google-y commit message? :)

Whoops, yes it is. Feel free to drop that sentence when applying the patch.

Quoted 19 lines

Paolo

Quoted 16 lines
---
 x86/unittests.cfg | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/unittests.cfg +1 -1
index f79688af93f8..587bf40e8bc9 100644
@@ -196,7 +196,7 @@ arch = x86_64
 
 [vmx]
 file = vmx.flat
-extra_params = -cpu host,+vmx -append -exit_monitor_from_l2_test
+extra_params = -cpu host,+vmx -m 2048 -append -exit_monitor_from_l2_test
#22 lore raw

Re: [kvm-unit-tests PATCH 18/32] lib: provide stdio.h

In reply to Andrew Jones
Quoted 37 lines · Andrew Jones
Quoted 14 lines · David Matlack

From: Peter Feiner <[email protected]>

kvm-unit-tests builds with the odd combination of system include files but not system libraries. That is, -nostdlib is specified but -nostdinc isn't! Problems arise when the include files don't match the libraries, e.g., printf() in some versions of glibc linking in a __printf_chk symbol that libcflat doesn't have. This patch fixes the problem with printf by pointing '#include "stdio.h"' to lib/libcflat.h, where a printf declaration that libcflat actually implements exists, rather than letting /usr/include/stdio.h get included. This patch does not solve the general problem of potential mismatches between libcflat and the compiler's notion standard include files; doing so would require rewriting stdarg.h, stdint.h, and several others in libcflat.

Yeah, I'm not sure rewriting those would be worth being able to add -nostdinc. When we cross compile ARM and POWER those few headers are some of the ones provided by the cross compiler.

Quoted 17 lines
---
 lib/stdio.h | 4 ++++
 1 file changed, 4 insertions(+)
 create mode 100644 lib/stdio.h
lib/stdio.h new file +3 -0
new file mode 100644
index 000000000000..7b30db93f7f1
@@ -0,0 +1,4 @@
+#ifndef LIBCFLAT_STDIO_H
+#define LIBCFLAT_STDIO_H
+#include "libcflat.h"
Quoted 8 lines
Quoted 4 lines
lib/stdio.h new file +1 -1
+#endif
--

2.12.2.816.g2cccc81164-goog

I'd rather we move the stdio functions (just printf?) into stdio.h rather than redirect to libcflat. We can include stdio.h from libcflat.h to avoid updating all the unit tests for now though.

That sounds reasonable to me. Feel free to leave this patch out when applying if you'd like. None of the other patches in this series actually depend on it.

Quoted 3 lines

Thanks,
drew

maintainer
#49 lore raw
In reply to David Matlack
Quoted 6 lines · David Matlack

4 of the EPT access tests reveal KVM bugs (4.11-rc6, on a Haswell CPU):
vmx_ept_access_test_execute_only vmx_ept_access_test_paddr_not_present_ad_disabled vmx_ept_access_test_paddr_read_only_ad_disabled vmx_ept_access_test_paddr_read_execute_ad_disabled

These are the failure I get:

FAIL vmx_ept_access_test_execute_only (164 tests, 6 unexpected failures) FAIL vmx_ept_access_test_paddr_not_present_ad_enabled (65 tests, 3 unexpected failures) FAIL vmx_ept_access_test_paddr_read_only_ad_disabled (timeout; duration=90s) FAIL vmx_ept_access_test_paddr_read_only_ad_enabled (175 tests, 9 unexpected failures) FAIL vmx_ept_access_test_paddr_read_execute_ad_disabled (timeout; duration=90s) FAIL vmx_ept_access_test_paddr_read_execute_ad_enabled (175 tests, 9 unexpected failures)

The problem is that there is the tests have no comment explaining what is being tested. For example in ept_access_test_paddr_read_only_ad_enabled they are all like this:

EPT_VLT_RD unexpected
FAIL: x86/vmx_tests.c:2124: Expectation failed: (expected_qual) == (qual)

	LHS: 0x000000000000008a - 0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'1000'1010 - 138
	RHS: 0x000000000000008b - 0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'1000'1011 - 139
	STACK: 4072ad 407a00 407acc 407be6 401a9c 403653 4002d2

and it's not obvious what is the difference between them without looking at the source. The problem is that, when only one of them is broken, it's not clear which one it is. Please make sure that prefix push/pop is used consistently around each subtest; it's okay to do it on top of these patches, so I've placed them in the next branch of kvm-unit-tests.git.

In this case, in addition, the exit qualification comes straight from the processor, so I think the failures are a testcase bug: "If such an access causes an EPT violation, the processor sets both bit 0 and bit 1 of the exit qualification", footnote 1 of table 27-7 "Exit Qualification for EPT Violations".

Paolo

#50 lore raw
Quoted 10 lines · Paolo Bonzini
Quoted 6 lines · David Matlack

4 of the EPT access tests reveal KVM bugs (4.11-rc6, on a Haswell CPU):
vmx_ept_access_test_execute_only vmx_ept_access_test_paddr_not_present_ad_disabled vmx_ept_access_test_paddr_read_only_ad_disabled vmx_ept_access_test_paddr_read_execute_ad_disabled

Quoted 8 lines

These are the failure I get:

FAIL vmx_ept_access_test_execute_only (164 tests, 6 unexpected failures) FAIL vmx_ept_access_test_paddr_not_present_ad_enabled (65 tests, 3 unexpected failures) FAIL vmx_ept_access_test_paddr_read_only_ad_disabled (timeout; duration=90s) FAIL vmx_ept_access_test_paddr_read_only_ad_enabled (175 tests, 9 unexpected failures) FAIL vmx_ept_access_test_paddr_read_execute_ad_disabled (timeout; duration=90s) FAIL vmx_ept_access_test_paddr_read_execute_ad_enabled (175 tests, 9 unexpected failures)

I get the same set of failures when testing a kernel sync'd to kvm/next (at commit bd17117bb2af).

Quoted 10 lines

The problem is that there is the tests have no comment explaining what is being tested. For example in ept_access_test_paddr_read_only_ad_enabled they are all like this:

EPT_VLT_RD unexpected
FAIL: x86/vmx_tests.c:2124: Expectation failed: (expected_qual) == (qual)

        LHS: 0x000000000000008a - 0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'1000'1010 - 138
        RHS: 0x000000000000008b - 0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'1000'1011 - 139
        STACK: 4072ad 407a00 407acc 407be6 401a9c 403653 4002d2
Quoted 5 lines

and it's not obvious what is the difference between them without looking at the source. The problem is that, when only one of them is broken, it's not clear which one it is. Please make sure that prefix push/pop is used consistently around each subtest; it's okay to do it on top of these patches, so I've placed them in the next branch of kvm-unit-tests.git.

The stack trace identifies which test case failed. For example, in logs/vmx_ept_access_test_paddr_not_present_ad_enabled.log I see:

EPT_VLT_RD unexpected
FAIL: x86/vmx_tests.c:2124: Expectation failed: (expected_qual) == (qual)

        LHS: 0x0000000000000082 -
0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'1000'0010
- 130
        RHS: 0x0000000000000083 -
0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'1000'0011
- 131
        STACK: 4072ad 407a00 407acc 407d09 401a9c 403653 4002d2
0x00000000004072ac: do_ept_violation at x86/vmx_tests.c:2124 (discriminator 1)
                diagnose_ept_violation_qual(expected_qual, qual);
      >         TEST_EXPECT_EQ(expected_qual, qual);

0x00000000004079ff: ept_access_paddr at x86/vmx_tests.c:2252
if (expect_violation)
> do_ept_violation(/*leaf=*/true, op,
expected_qual |
EPT_VLT_LADDR_VLD, gpa);
0x0000000000407acb: ept_access_violation_paddr at x86/vmx_tests.c:2278
{
> ept_access_paddr(ept_access, pte_ad, op,
/*expect_violation=*/true,

                                 expected_qual);
0x0000000000407d08: ept_access_test_paddr_not_present_ad_enabled at
x86/vmx_tests.c:2690
                ept_access_violation_paddr(0, PT_AD_MASK, OP_WRITE, EPT_VLT_WR);
      >         ept_access_violation_paddr(0, PT_AD_MASK, OP_EXEC, EPT_VLT_WR);
        }
0x0000000000401a9b: test_run at x86/vmx.c:1675
                if (test->v2)
      >                 test->v2();
                else
0x0000000000403652: main at x86/vmx.c:1857
                                continue;
      >                 if (test_run(&vmx_tests[i]))
                                goto exit;

0x00000000004002d1: start64 at x86/cstart64.S:210
lea __environ(%rip), %rdx
> call main
mov %eax, %edi

Does this work for you?

Quoted 6 lines

In this case, in addition, the exit qualification comes straight from the processor, so I think the failures are a testcase bug: "If such an access causes an EPT violation, the processor sets both bit 0 and bit 1 of the exit qualification", footnote 1 of table 27-7 "Exit Qualification for EPT Violations".

Agreed, good catch. I will send a patch to fix the tests.

Quoted 2 lines

Paolo

maintainer
#51 lore raw
Quoted 54 lines · David Matlack
Quoted 9 lines

The problem is that there is the tests have no comment explaining what is being tested. For example in ept_access_test_paddr_read_only_ad_enabled they are all like this:

EPT_VLT_RD unexpected
FAIL: x86/vmx_tests.c:2124: Expectation failed: (expected_qual) == (qual)

        LHS: 0x000000000000008a - 0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'1000'1010 - 138
        RHS: 0x000000000000008b - 0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'1000'1011 - 139
        STACK: 4072ad 407a00 407acc 407be6 401a9c 403653 4002d2
Quoted 5 lines

and it's not obvious what is the difference between them without looking at the source. The problem is that, when only one of them is broken, it's not clear which one it is. Please make sure that prefix push/pop is used consistently around each subtest; it's okay to do it on top of these patches, so I've placed them in the next branch of kvm-unit-tests.git.

The stack trace identifies which test case failed. For example, in logs/vmx_ept_access_test_paddr_not_present_ad_enabled.log I see:

EPT_VLT_RD unexpected
FAIL: x86/vmx_tests.c:2124: Expectation failed: (expected_qual) == (qual)

        LHS: 0x0000000000000082 -
0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'1000'0010
- 130
        RHS: 0x0000000000000083 -
0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'1000'0011
- 131
        STACK: 4072ad 407a00 407acc 407d09 401a9c 403653 4002d2
0x00000000004072ac: do_ept_violation at x86/vmx_tests.c:2124 (discriminator 1)
                diagnose_ept_violation_qual(expected_qual, qual);
      >         TEST_EXPECT_EQ(expected_qual, qual);

0x00000000004079ff: ept_access_paddr at x86/vmx_tests.c:2252
if (expect_violation)
> do_ept_violation(/*leaf=*/true, op,
expected_qual |
EPT_VLT_LADDR_VLD, gpa);
0x0000000000407acb: ept_access_violation_paddr at x86/vmx_tests.c:2278
{
> ept_access_paddr(ept_access, pte_ad, op,
/*expect_violation=*/true,

                                 expected_qual);
0x0000000000407d08: ept_access_test_paddr_not_present_ad_enabled at
x86/vmx_tests.c:2690
                ept_access_violation_paddr(0, PT_AD_MASK, OP_WRITE, EPT_VLT_WR);
      >         ept_access_violation_paddr(0, PT_AD_MASK, OP_EXEC, EPT_VLT_WR);
        }
0x0000000000401a9b: test_run at x86/vmx.c:1675
                if (test->v2)
      >                 test->v2();
                else
0x0000000000403652: main at x86/vmx.c:1857
                                continue;
      >                 if (test_run(&vmx_tests[i]))
                                goto exit;
Quoted 6 lines

0x00000000004002d1: start64 at x86/cstart64.S:210
lea __environ(%rip), %rdx
> call main
mov %eax, %edi

Does this work for you?

Certainly much better. :)

Paolo

Quoted 11 lines
Quoted 5 lines

In this case, in addition, the exit qualification comes straight from the processor, so I think the failures are a testcase bug: "If such an access causes an EPT violation, the processor sets both bit 0 and bit 1 of the exit qualification", footnote 1 of table 27-7 "Exit Qualification for EPT Violations".

Agreed, good catch. I will send a patch to fix the tests.

Quoted 2 lines

Paolo

#29 lore raw

Re: [kvm-unit-tests PATCH 21/32] lib: printf-style report prefixes

In reply to David Matlack
Quoted 52 lines · David Matlack

From: Peter Feiner <[email protected]>

---
 lib/libcflat.h |  1 +
 lib/report.c   | 38 +++++++++++++++++++++++++++++++++-----
 2 files changed, 34 insertions(+), 5 deletions(-)
lib/libcflat.h +1 -0
index 5d356df75f1f..05c18543dd72 100644
@@ -96,6 +96,7 @@ extern int vsnprintf(char *buf, int size, const char *fmt, va_list va)
 extern int vprintf(const char *fmt, va_list va)
 					__attribute__((format(printf, 1, 0)));
 
+void report_prefix_pushf(const char *prefix_fmt, ...);
 extern void report_prefix_push(const char *prefix);
 extern void report_prefix_pop(void);
 extern void report(const char *msg_fmt, bool pass, ...);
lib/report.c +18 -3
index e24e81382f9e..1033f1e44e99 100644
@@ -17,14 +17,42 @@ static unsigned int tests, failures, xfailures, skipped;
 static char prefixes[256];
 static struct spinlock lock;
 
-void report_prefix_push(const char *prefix)
+#define PREFIX_DELIMITER ": "
+
+void report_prefix_pushf(const char *prefix_fmt, ...)
 {
+	va_list va;
+	int len;
+	int start;
+
 	spin_lock(&lock);
-	strcat(prefixes, prefix);
-	strcat(prefixes, ": ");
+
+	len = strlen(prefixes);
+	assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
+	start = len;
+
+	va_start(va, prefix_fmt);
+	len += vsnprintf(&prefixes[len], sizeof(prefixes) - len, prefix_fmt,
+			 va);
+	va_end(va);
+	assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
+

lib/report.c static unsigned int tests, failures, xfailures, skipped; · line 49

Quoted 10 lines
lib/report.c +8 -0
+	assert_msg(!strstr(&prefixes[start], PREFIX_DELIMITER),
+		   "Prefix \"%s\" contains delimiter \"" PREFIX_DELIMITER "\"",
+		   &prefixes[start]);
+
+	len += snprintf(&prefixes[len], sizeof(prefixes) - len,
+			PREFIX_DELIMITER);
+	assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
+
 	spin_unlock(&lock);
 }

I can not compile the current master of kvm-unit-tests anymore... I think it is due to the above patch. I get now these error messages:

powerpc64-linux-gnu-gcc -std=gnu99 -ffreestanding -Wextra -O2 -I lib -I lib/libfdt -Wa,-mregnames -g -MMD -MF lib/.report.d -Wall -Werror -fomit-frame-pointer -Wno-frame-address -fno-pic -mbig-endian -c -o lib/report.o lib/report.c
In file included from lib/report.c:13:0:
lib/report.c: In function ‘report_prefix_pushf’:
lib/report.c:38:17: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]

  assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
                 ^
lib/libcflat.h:133:8: note: in definition of macro ‘assert_msg’
  if (!(cond)) {       \
        ^
lib/report.c:45:17: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
  assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
                 ^
lib/libcflat.h:133:8: note: in definition of macro ‘assert_msg’
  if (!(cond)) {       \
        ^
lib/report.c:53:17: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
  assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
                 ^
lib/libcflat.h:133:8: note: in definition of macro ‘assert_msg’
  if (!(cond)) {       \
        ^

Thomas

#30 lore raw

Re: [kvm-unit-tests PATCH 21/32] lib: printf-style report prefixes

Quoted 88 lines · Thomas Huth
Quoted 52 lines · David Matlack

From: Peter Feiner <[email protected]>

---
 lib/libcflat.h |  1 +
 lib/report.c   | 38 +++++++++++++++++++++++++++++++++-----
 2 files changed, 34 insertions(+), 5 deletions(-)
lib/libcflat.h +1 -0
index 5d356df75f1f..05c18543dd72 100644
@@ -96,6 +96,7 @@ extern int vsnprintf(char *buf, int size, const char *fmt, va_list va)
 extern int vprintf(const char *fmt, va_list va)
 					__attribute__((format(printf, 1, 0)));
 
+void report_prefix_pushf(const char *prefix_fmt, ...);
 extern void report_prefix_push(const char *prefix);
 extern void report_prefix_pop(void);
 extern void report(const char *msg_fmt, bool pass, ...);
lib/report.c +18 -3
index e24e81382f9e..1033f1e44e99 100644
@@ -17,14 +17,42 @@ static unsigned int tests, failures, xfailures, skipped;
 static char prefixes[256];
 static struct spinlock lock;
 
-void report_prefix_push(const char *prefix)
+#define PREFIX_DELIMITER ": "
+
+void report_prefix_pushf(const char *prefix_fmt, ...)
 {
+	va_list va;
+	int len;
+	int start;
+
 	spin_lock(&lock);
-	strcat(prefixes, prefix);
-	strcat(prefixes, ": ");
+
+	len = strlen(prefixes);
+	assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
+	start = len;
+
+	va_start(va, prefix_fmt);
+	len += vsnprintf(&prefixes[len], sizeof(prefixes) - len, prefix_fmt,
+			 va);
+	va_end(va);
+	assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
+

lib/report.c static unsigned int tests, failures, xfailures, skipped; · line 49

Quoted 10 lines
lib/report.c +8 -0
+	assert_msg(!strstr(&prefixes[start], PREFIX_DELIMITER),
+		   "Prefix \"%s\" contains delimiter \"" PREFIX_DELIMITER "\"",
+		   &prefixes[start]);
+
+	len += snprintf(&prefixes[len], sizeof(prefixes) - len,
+			PREFIX_DELIMITER);
+	assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
+
 	spin_unlock(&lock);
 }

I can not compile the current master of kvm-unit-tests anymore... I think it is due to the above patch. I get now these error messages:

powerpc64-linux-gnu-gcc -std=gnu99 -ffreestanding -Wextra -O2 -I lib -I lib/libfdt -Wa,-mregnames -g -MMD -MF lib/.report.d -Wall -Werror -fomit-frame-pointer -Wno-frame-address -fno-pic -mbig-endian -c -o lib/report.o lib/report.c
In file included from lib/report.c:13:0:
lib/report.c: In function ‘report_prefix_pushf’:
lib/report.c:38:17: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]

  assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
                 ^
lib/libcflat.h:133:8: note: in definition of macro ‘assert_msg’
  if (!(cond)) {       \
        ^
lib/report.c:45:17: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
  assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
                 ^
lib/libcflat.h:133:8: note: in definition of macro ‘assert_msg’
  if (!(cond)) {       \
        ^
lib/report.c:53:17: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
  assert_msg(len < sizeof(prefixes), "%d >= %lu", len, sizeof(prefixes));
                 ^
lib/libcflat.h:133:8: note: in definition of macro ‘assert_msg’
  if (!(cond)) {       \
        ^
Quoted 2 lines

Thomas

Also found that while preparing the s390x patch series. Already sent

[kvm-unit-tests PATCH v1] lib: fix compilation warning

Signature · 3 lines
Thanks,

David
#31 lore raw

Re: [kvm-unit-tests PATCH 21/32] lib: printf-style report prefixes

Quoted 4 lines · David Hildenbrand

Also found that while preparing the s390x patch series. Already sent

[kvm-unit-tests PATCH v1] lib: fix compilation warning

Sorry about that. Thanks for the fix!

Quoted 6 lines

--

Thanks,

David