Skip to contentSkip to conversation

Test of VMCS pin-based controls

PATCH Applied Jim Mattson 2 participants 5 messages Aug 24, 2017 – Aug 25, 2017 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 Test of VMCS pin-based controls

This test checks that a nested VM-entry fails if reserved bits in the pin-based VM-execution controls are not set properly (according to either the IA32_VMX_PINBASED_CTLS MSR or the IA32_VMX_TRUE_PINBASED_CTLS MSR, as appropriate).

---
 x86/vmx_tests.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
x86/vmx_tests.c +68 -0
index 0ccc7e7..9c4bcc9 100644
@@ -3186,6 +3186,73 @@ static void test_vmx_controls(bool controls_valid)
 	}
 }
 
+/*
+ * Test a particular value of a VM-execution control bit, if the value
+ * is required or if the value is zero.
+ */
+static void test_rsvd_ctl_bit_value(const char *name, union vmx_ctrl_msr msr,
+				    enum Encoding encoding, unsigned bit,
+				    unsigned val)
+{
+	u32 mask = 1u << bit;
+	bool expected;
+	u32 controls;
+
+	if (msr.set & mask)
+		TEST_ASSERT(msr.clr & mask);
+
+	/*
+	 * We can't arbitrarily turn on a control bit, because it may
+	 * introduce dependencies on other VMCS fields. So, we only
+	 * test turning on bits that have a required setting.
+	 */
+	if (val && (msr.clr & mask) && !(msr.set & mask))
+		return;
+
+	report_prefix_pushf("%s %s bit %d",
+			    val ? "Set" : "Clear", name, bit);
+
+	controls = vmcs_read(encoding);
+	if (val) {
+		vmcs_write(encoding, msr.set | mask);
+		expected = (msr.clr & mask);
+	} else {
+		vmcs_write(encoding, msr.set & ~mask);
+		expected = !(msr.set & mask);
+	}
+	test_vmx_controls(expected);
+	vmcs_write(encoding, controls);
+	report_prefix_pop();
+}
+
+/*
+ * Test reserved values of a VM-execution control bit, based on the
+ * allowed bit settings from the corresponding VMX capability MSR.
+ */
+static void test_rsvd_ctl_bit(const char *name, union vmx_ctrl_msr msr,
+			      enum Encoding encoding, unsigned bit)
+{
+	test_rsvd_ctl_bit_value(name, msr, encoding, bit, 0);
+	test_rsvd_ctl_bit_value(name, msr, encoding, bit, 1);
+}
+
+/*
+ * Reserved bits in the pin-based VM-execution controls must be set
+ * properly. Software may consult the VMX capability MSRs to determine
+ * the proper settings.
+ * [Intel SDM]
+ */
+static void test_pin_based_ctls(void)
+{
+	unsigned bit;
+
+	printf("%s: %lx\n", basic.ctrl ? "MSR_IA32_VMX_TRUE_PIN" :
+	       "MSR_IA32_VMX_PINBASED_CTLS", ctrl_pin_rev.val);
+	for (bit = 0; bit < 32; bit++)
+		test_rsvd_ctl_bit("pin-based controls",
+				  ctrl_pin_rev, PIN_CONTROLS, bit);
+}
+
 /*
  * Test a particular address setting for a physical page reference in
  * the VMCS.
@@ -3289,6 +3356,7 @@ static void vmx_controls_test(void)
 	 */
 	vmcs_write(GUEST_RFLAGS, 0);
 
+	test_pin_based_ctls();
 	test_io_bitmaps();
 	test_msr_bitmap();
 }
Signature · 1 line
2.14.1.342.g6490525c54-goog
#1 lore raw

PATCH 2/4 Test of VMCS processor-based controls

This test checks that a nested VM-entry fails if reserved bits in the primary processor-based VM-execution controls are not set properly (according to either the IA32_VMX_PROCBASED_CTLS MSR or the IA32_VMX_TRUE_PROCBASED_CTLS MSR, as appropriate). It also checks that a nested VM-entry fails if the "activate secondary controls" VM-execution control is set and a reserved bit is set in the secondary processor-based VM-execution controls (according to the IA32_VMX_PROCBASED_CTLS2 MSR).

---
 x86/vmx_tests.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
x86/vmx_tests.c +61 -0
index 9c4bcc9..8853875 100644
@@ -3253,6 +3253,65 @@ static void test_pin_based_ctls(void)
 				  ctrl_pin_rev, PIN_CONTROLS, bit);
 }
 
+/*
+ * Reserved bits in the primary processor-based VM-execution controls
+ * must be set properly. Software may consult the VMX capability MSRs
+ * to determine the proper settings.
+ * [Intel SDM]
+ */
+static void test_primary_processor_based_ctls(void)
+{
+	unsigned bit;
+
+	printf("\n%s: %lx\n", basic.ctrl ? "MSR_IA32_VMX_TRUE_PROC" :
+	       "MSR_IA32_VMX_PROCBASED_CTLS", ctrl_cpu_rev[0].val);
+	for (bit = 0; bit < 32; bit++)
+		test_rsvd_ctl_bit("primary processor-based controls",
+				  ctrl_cpu_rev[0], CPU_EXEC_CTRL0, bit);
+}
+
+/*
+ * If the "activate secondary controls" primary processor-based
+ * VM-execution control is 1, reserved bits in the secondary
+ * processor-based VM-execution controls must be cleared. Software may
+ * consult the VMX capability MSRs to determine which bits are
+ * reserved.
+ * If the "activate secondary controls" primary processor-based
+ * VM-execution control is 0 (or if the processor does not support the
+ * 1-setting of that control), no checks are performed on the
+ * secondary processor-based VM-execution controls.
+ * [Intel SDM]
+ */
+static void test_secondary_processor_based_ctls(void)
+{
+	u32 primary;
+	u32 secondary;
+	unsigned bit;
+
+	if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY))
+		return;
+
+	primary = vmcs_read(CPU_EXEC_CTRL0);
+	secondary = vmcs_read(CPU_EXEC_CTRL1);
+
+	vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY);
+	printf("\nMSR_IA32_VMX_PROCBASED_CTLS2: %lx\n", ctrl_cpu_rev[1].val);
+	for (bit = 0; bit < 32; bit++)
+		test_rsvd_ctl_bit("secondary processor-based controls",
+				  ctrl_cpu_rev[1], CPU_EXEC_CTRL1, bit);
+
+	/*
+	 * When the "activate secondary controls" VM-execution control
+	 * is clear, there are no checks on the secondary controls.
+	 */
+	vmcs_write(CPU_EXEC_CTRL0, primary & ~CPU_SECONDARY);
+	vmcs_write(CPU_EXEC_CTRL1, ~0);
+	report("Secondary processor-based controls ignored",
+	       vmlaunch_succeeds());
+	vmcs_write(CPU_EXEC_CTRL1, secondary);
+	vmcs_write(CPU_EXEC_CTRL0, primary);
+}
+
 /*
  * Test a particular address setting for a physical page reference in
  * the VMCS.
@@ -3357,6 +3416,8 @@ static void vmx_controls_test(void)
 	vmcs_write(GUEST_RFLAGS, 0);
 
 	test_pin_based_ctls();
+	test_primary_processor_based_ctls();
+	test_secondary_processor_based_ctls();
 	test_io_bitmaps();
 	test_msr_bitmap();
 }
Signature · 1 line
2.14.1.342.g6490525c54-goog
#2 lore raw

PATCH 3/4 Test CR3 target count validation

In reply to Jim Mattson

If the CR3 target count is too high, VM-entry should fail.

---
 x86/vmx_tests.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
x86/vmx_tests.c +33 -0
index 8853875..22295e8 100644
@@ -3312,6 +3312,38 @@ static void test_secondary_processor_based_ctls(void)
 	vmcs_write(CPU_EXEC_CTRL0, primary);
 }
 
+static void try_cr3_target_count(unsigned i, unsigned max)
+{
+	report_prefix_pushf("CR3 target count 0x%x", i);
+	vmcs_write(CR3_TARGET_COUNT, i);
+	test_vmx_controls(i <= max);
+	report_prefix_pop();
+}
+
+/*
+ * The CR3-target count must not be greater than 4. Future processors
+ * may support a different number of CR3-target values. Software
+ * should read the VMX capability MSR IA32_VMX_MISC to determine the
+ * number of values supported.
+ * [Intel SDM]
+ */
+static void test_cr3_targets(void)
+{
+	unsigned supported_targets = (rdmsr(MSR_IA32_VMX_MISC) >> 16) & 0x1ff;
+	u32 cr3_targets = vmcs_read(CR3_TARGET_COUNT);
+	unsigned i;
+
+	printf("\nSupported CR3 targets: %d\n", supported_targets);
+	TEST_ASSERT(supported_targets <= 256);
+
+	try_cr3_target_count(-1u, supported_targets);
+	try_cr3_target_count(0x80000000, supported_targets);
+	try_cr3_target_count(0x7fffffff, supported_targets);
+	for (i = 0; i <= supported_targets + 1; i++)
+		try_cr3_target_count(i, supported_targets);
+	vmcs_write(CR3_TARGET_COUNT, cr3_targets);
+}
+
 /*
  * Test a particular address setting for a physical page reference in
  * the VMCS.
@@ -3418,6 +3450,7 @@ static void vmx_controls_test(void)
 	test_pin_based_ctls();
 	test_primary_processor_based_ctls();
 	test_secondary_processor_based_ctls();
+	test_cr3_targets();
 	test_io_bitmaps();
 	test_msr_bitmap();
 }
Signature · 1 line
2.14.1.342.g6490525c54-goog
#3 lore raw

PATCH 4/4 Test of virtual-APIC address

In reply to Jim Mattson

This test checks that a nested VM-entry fails if the "use TPR shadow" VM-execution control is set and the virtual-APIC address is illegal. A legal virtual-apic address is 4k-aligned and within the physical address space supported by the processor.

---
 x86/vmx_tests.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
x86/vmx_tests.c +15 -0
index 22295e8..32aa35a 100644
@@ -3438,6 +3438,20 @@ static void test_msr_bitmap(void)
 				 "MSR bitmap", "Use MSR bitmaps");
 }
 
+/*
+ * If the "use TPR shadow" VM-execution control is 1, the virtual-APIC
+ * address must satisfy the following checks:
+ * - Bits 11:0 of the address must be 0.
+ * - The address should not set any bits beyond the processor's
+ *   physical-address width.
+ * [Intel SDM]
+ */
+static void test_apic_virt_addr(void)
+{
+	test_vmcs_page_reference(CPU_TPR_SHADOW, APIC_VIRT_ADDR,
+				 "virtual-APIC address", "Use TPR shadow");
+}
+
 static void vmx_controls_test(void)
 {
 	/*
@@ -3453,6 +3467,7 @@ static void vmx_controls_test(void)
 	test_cr3_targets();
 	test_io_bitmaps();
 	test_msr_bitmap();
+	test_apic_virt_addr();
 }
 
 static bool valid_vmcs_for_vmentry(void)
Signature · 1 line
2.14.1.342.g6490525c54-goog
maintainer Applied
#4 lore raw
In reply to Jim Mattson
Quoted 6 lines · Jim Mattson

This test checks that a nested VM-entry fails if reserved bits in the pin-based VM-execution controls are not set properly (according to either the IA32_VMX_PINBASED_CTLS MSR or the IA32_VMX_TRUE_PINBASED_CTLS MSR, as appropriate).

Applied all four, thanks.

Paolo

Quoted 91 lines
---
 x86/vmx_tests.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
x86/vmx_tests.c +68 -0
index 0ccc7e7..9c4bcc9 100644
@@ -3186,6 +3186,73 @@ static void test_vmx_controls(bool controls_valid)
 	}
 }
 
+/*
+ * Test a particular value of a VM-execution control bit, if the value
+ * is required or if the value is zero.
+ */
+static void test_rsvd_ctl_bit_value(const char *name, union vmx_ctrl_msr msr,
+				    enum Encoding encoding, unsigned bit,
+				    unsigned val)
+{
+	u32 mask = 1u << bit;
+	bool expected;
+	u32 controls;
+
+	if (msr.set & mask)
+		TEST_ASSERT(msr.clr & mask);
+
+	/*
+	 * We can't arbitrarily turn on a control bit, because it may
+	 * introduce dependencies on other VMCS fields. So, we only
+	 * test turning on bits that have a required setting.
+	 */
+	if (val && (msr.clr & mask) && !(msr.set & mask))
+		return;
+
+	report_prefix_pushf("%s %s bit %d",
+			    val ? "Set" : "Clear", name, bit);
+
+	controls = vmcs_read(encoding);
+	if (val) {
+		vmcs_write(encoding, msr.set | mask);
+		expected = (msr.clr & mask);
+	} else {
+		vmcs_write(encoding, msr.set & ~mask);
+		expected = !(msr.set & mask);
+	}
+	test_vmx_controls(expected);
+	vmcs_write(encoding, controls);
+	report_prefix_pop();
+}
+
+/*
+ * Test reserved values of a VM-execution control bit, based on the
+ * allowed bit settings from the corresponding VMX capability MSR.
+ */
+static void test_rsvd_ctl_bit(const char *name, union vmx_ctrl_msr msr,
+			      enum Encoding encoding, unsigned bit)
+{
+	test_rsvd_ctl_bit_value(name, msr, encoding, bit, 0);
+	test_rsvd_ctl_bit_value(name, msr, encoding, bit, 1);
+}
+
+/*
+ * Reserved bits in the pin-based VM-execution controls must be set
+ * properly. Software may consult the VMX capability MSRs to determine
+ * the proper settings.
+ * [Intel SDM]
+ */
+static void test_pin_based_ctls(void)
+{
+	unsigned bit;
+
+	printf("%s: %lx\n", basic.ctrl ? "MSR_IA32_VMX_TRUE_PIN" :
+	       "MSR_IA32_VMX_PINBASED_CTLS", ctrl_pin_rev.val);
+	for (bit = 0; bit < 32; bit++)
+		test_rsvd_ctl_bit("pin-based controls",
+				  ctrl_pin_rev, PIN_CONTROLS, bit);
+}
+
 /*
  * Test a particular address setting for a physical page reference in
  * the VMCS.
@@ -3289,6 +3356,7 @@ static void vmx_controls_test(void)
 	 */
 	vmcs_write(GUEST_RFLAGS, 0);
 
+	test_pin_based_ctls();
 	test_io_bitmaps();
 	test_msr_bitmap();
 }