Skip to contentSkip to conversation

KVM: SEV-ES: Add tests to validate #VC handling

PATCH New Vasant Karasulli 1 participant 5 messages Jun 13, 2022

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

v7 cover KVM: SEV-ES: Add tests to validate #VC handling

Hi All,

This is the version 7 of the patch written to add tests for AMD SEV-ES #VC handling. This version attempts to address review comments to the previous version of the patch.

Changes v6->v7:
1. Added information about how to run the tests.
2. test->priv no longer points to a location on heap.

Thanks,
Vasant

 arch/x86/Kbuild              |   2 +
 arch/x86/Kconfig.debug       |  19 +++++
 arch/x86/kernel/Makefile     |   7 ++
 arch/x86/tests/Makefile      |   3 +
 arch/x86/tests/sev-test-vc.c | 145 +++++++++++++++++++++++++++++++++++
 5 files changed, 176 insertions(+)
 create mode 100644 arch/x86/tests/Makefile
 create mode 100644 arch/x86/tests/sev-test-vc.c

base-commit: b13baccc3850ca8b8cccbf8ed9912dbaa0fdf7f3 --
2.32.0

#1 lore raw

PATCH v7 1/4 KVM: SEV-ES: Add configuration options to test #VC handling

Configuration options CONFIG_X86_TESTS and CONFIG_AMD_SEV_ES_TEST_VC enable the execution of the KUnit tests added under arch/x86/tests that test AMD SEV-ES #VC handling.

---
 arch/x86/Kbuild          |  2 ++
 arch/x86/Kconfig.debug   | 19 +++++++++++++++++++
 arch/x86/kernel/Makefile |  7 +++++++
 3 files changed, 28 insertions(+)
arch/x86/Kbuild +2 -0
index 5a83da703e87..ec01cd024bae 100644
@@ -28,5 +28,7 @@ obj-y += net/
 
 obj-$(CONFIG_KEXEC_FILE) += purgatory/
 
+obj-y += tests/
+
 # for cleaning
 subdir- += boot tools
arch/x86/Kconfig.debug +19 -0
index 340399f69954..b0687173fb97 100644
@@ -278,3 +278,22 @@ endchoice
 config FRAME_POINTER
 	depends on !UNWINDER_ORC && !UNWINDER_GUESS
 	bool
+
+config X86_TESTS
+	bool "x86 unit tests"
+	help
+	    This enables building the tests under arch/x86/tests.
+
+if X86_TESTS
+config AMD_SEV_TEST_VC
+	bool "Test for AMD SEV VC exception handling"
+	depends on AMD_MEM_ENCRYPT
+	select FUNCTION_TRACER
+	select KPROBES
+	select KUNIT
+	help
+	  Enable KUnit-based testing for AMD SEV #VC exception handling.
+	  When this configuration option is enabled, these Kunit tests
+	  get executed at the kernel boot time. Results of the test
+	  execution can be monitored in the kernel log.
+endif # X86_TESTS
arch/x86/kernel/Makefile +7 -0
index 03364dc40d8d..3fa9ce2a700e 100644
@@ -23,6 +23,13 @@ CFLAGS_REMOVE_head64.o = -pg
 CFLAGS_REMOVE_sev.o = -pg
 endif
 
+# AMD_SEV_TEST_VC registers a kprobe by function name. IPA-SRA creates
+# function copies and renames them to have an .isra suffix, which breaks kprobes'
+# lookup. Build with -fno-ipa-sra for the test.
+ifdef CONFIG_AMD_SEV_TEST_VC
+CFLAGS_sev.o	+= -fno-ipa-sra
+endif
+
 KASAN_SANITIZE_head$(BITS).o				:= n
 KASAN_SANITIZE_dumpstack.o				:= n
 KASAN_SANITIZE_dumpstack_$(BITS).o			:= n

--
2.32.0

#2 lore raw

PATCH v7 2/4 KVM: SEV-ES: Add tests to validate VC handling for CPUID and WBINVD

In reply to Vasant Karasulli

These tests:
1. install a kretprobe on the #VC handler (sev_es_ghcb_hv_call, to access GHCB before/after the resulting VMGEXIT).
2. trigger an NAE by executing either CPUID or WBINVD.
3. check that the kretprobe was hit with the right exit_code available in GHCB.

To run these tests, configuration options CONFIG_X86_TESTS and CONFIG_AMD_SEV_ES_TEST_VC have to be enabled. These tests run at the kernel boot time. Result of the test execution can be monitored in the kernel log.

---
 arch/x86/tests/Makefile      |   3 +
 arch/x86/tests/sev-test-vc.c | 104 +++++++++++++++++++++++++++++++++++
 2 files changed, 107 insertions(+)
 create mode 100644 arch/x86/tests/Makefile
 create mode 100644 arch/x86/tests/sev-test-vc.c
arch/x86/tests/Makefile new file +3 -0
new file mode 100644
index 000000000000..4beca64bd2aa
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_AMD_SEV_TEST_VC)	+= sev-test-vc.o
arch/x86/tests/sev-test-vc.c new file +104 -0

104 lines not on this page · raw message

--
2.32.0

#3 lore raw

PATCH v7 3/4 KVM: SEV-ES: Add tests to validate VC handling for MSR and DR7 register accesses

In reply to Vasant Karasulli

These tests:
1. install a kretprobe on the #VC handler (sev_es_ghcb_hv_call, to access GHCB before/after the resulting VMGEXIT).
2. trigger an NAE by accessing either MSR or DR7.
3. check that the kretprobe was hit with the right exit_code available in GHCB.

To run these tests, configuration options CONFIG_X86_TESTS and CONFIG_AMD_SEV_ES_TEST_VC have to be enabled. These tests run at the kernel boot time. Result of the test execution can be monitored in the kernel log.

---
 arch/x86/tests/sev-test-vc.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
arch/x86/tests/sev-test-vc.c +14 -0
index 900ca357a273..629aa0ca1c86 100644
@@ -7,6 +7,7 @@
 
 #include <asm/cpufeature.h>
 #include <asm/sev-common.h>
+#include <asm/debugreg.h>
 #include <asm/svm.h>
 #include <kunit/test.h>
 #include <linux/kprobes.h>
@@ -89,9 +90,22 @@ static void sev_es_nae_wbinvd(struct kunit *test)
 	check_op(test, SVM_EXIT_WBINVD, wbinvd());
 }
 
+static void sev_es_nae_msr(struct kunit *test)
+{
+	check_op(test, SVM_EXIT_MSR, __rdmsr(MSR_IA32_TSC));
+}
+
+static void sev_es_nae_dr7_rw(struct kunit *test)
+{
+	check_op(test, SVM_EXIT_WRITE_DR7,
+		   native_set_debugreg(7, native_get_debugreg(7)));
+}
+
 static struct kunit_case sev_es_vc_testcases[] = {
 	KUNIT_CASE(sev_es_nae_cpuid),
 	KUNIT_CASE(sev_es_nae_wbinvd),
+	KUNIT_CASE(sev_es_nae_msr),
+	KUNIT_CASE(sev_es_nae_dr7_rw),
 	{}
 };
 

--
2.32.0

#4 lore raw

PATCH v7 4/4 KVM SEV-ES: Add tests to validate VC handling for IO instructions

In reply to Vasant Karasulli

These tests:
1. install a kretprobe on the #VC handler (sev_es_ghcb_hv_call, to access GHCB before/after the resulting VMGEXIT).
2. trigger an NAE by issuing an IO instruction.
3. check that the kretprobe was hit with the right exit_code available in GHCB.

To run these tests, configuration options CONFIG_X86_TESTS and CONFIG_AMD_SEV_ES_TEST_VC have to be enabled. These tests run at the kernel boot time. Result of the test execution can be monitored in the kernel log.

---
 arch/x86/tests/sev-test-vc.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
arch/x86/tests/sev-test-vc.c +27 -0
index 629aa0ca1c86..33ca761bf9cb 100644
@@ -8,7 +8,9 @@
 #include <asm/cpufeature.h>
 #include <asm/sev-common.h>
 #include <asm/debugreg.h>
+#include <asm/io.h>
 #include <asm/svm.h>
+#include <asm/apicdef.h>
 #include <kunit/test.h>
 #include <linux/kprobes.h>
 
@@ -101,11 +103,36 @@ static void sev_es_nae_dr7_rw(struct kunit *test)
 		   native_set_debugreg(7, native_get_debugreg(7)));
 }
 
+static void sev_es_nae_ioio(struct kunit *test)
+{
+	unsigned long port = 0x80;
+	char val = 0;
+
+	check_op(test, SVM_EXIT_IOIO, val = inb(port));
+	check_op(test, SVM_EXIT_IOIO, outb(val, port));
+	check_op(test, SVM_EXIT_IOIO, insb(port, &val, sizeof(val)));
+	check_op(test, SVM_EXIT_IOIO, outsb(port, &val, sizeof(val)));
+}
+
+static void sev_es_nae_mmio(struct kunit *test)
+{
+	unsigned long lapic_ver_pa = APIC_DEFAULT_PHYS_BASE + APIC_LVR;
+	unsigned long __iomem *lapic = ioremap(lapic_ver_pa, 0x4);
+	unsigned long lapic_version = 0;
+
+	check_op(test, SVM_VMGEXIT_MMIO_READ, lapic_version = *lapic);
+	check_op(test, SVM_VMGEXIT_MMIO_WRITE, *lapic = lapic_version);
+
+	iounmap(lapic);
+}
+
 static struct kunit_case sev_es_vc_testcases[] = {
 	KUNIT_CASE(sev_es_nae_cpuid),
 	KUNIT_CASE(sev_es_nae_wbinvd),
 	KUNIT_CASE(sev_es_nae_msr),
 	KUNIT_CASE(sev_es_nae_dr7_rw),
+	KUNIT_CASE(sev_es_nae_ioio),
+	KUNIT_CASE(sev_es_nae_mmio),
 	{}
 };
 

--
2.32.0