Skip to contentSkip to conversation

x86/tests: Add tests for AMD SEV-ES #VC handling

PATCH In review Vasant Karasulli 2 participants 8 messages Mar 18, 2022 – Jun 7, 2022

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

v6 cover x86/tests: Add tests for AMD SEV-ES #VC handling

Hi All,

This is the version 6 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 in
https://lore.kernel.org/kvm/[email protected]/.

Changes in this version:
1. The patch in the previous version is split into 4 parts.
2. Constants in function sev_es_nae_mmio are replaced by macros.

PS: I am resending this version because I made a mistake in the subject. Apologies for the spam.

Thanks,
Vasant

Vasant Karasulli (4):
x86/tests: Add Kconfig options for testing AMD SEV related features. x86/tests: Add KUnit based tests to validate Linux's VC handling for
instructions cpuid and wbinvd. 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.
x86/tests: Add KUnit based tests to validate Linux's VC handling for
instructions accessing registers such as MSR and DR7. 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.
x86/tests: Add KUnit based tests to validate Linux's VC handling for
IO instructions. 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.

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

base-commit: 09688c0166e76ce2fb85e86b9d99be8b0084cdf9
prerequisite-patch-id: b74bc39d7ca69ad86b5f9090047c44ab039f4622
prerequisite-patch-id: a53a291b59b4ceaffa25a9a08dfa08b5a78a01b9 --
2.32.0

#1 lore raw

PATCH v6 1/4 x86/tests: Add tests for AMD SEV-ES #VC handling

Add Kconfig options for testing AMD SEV
related features.

---
 arch/x86/Kbuild          |  2 ++
 arch/x86/Kconfig.debug   | 16 ++++++++++++++++
 arch/x86/kernel/Makefile |  7 +++++++
 arch/x86/tests/Makefile  |  1 +
 4 files changed, 26 insertions(+)
 create mode 100644 arch/x86/tests/Makefile
arch/x86/Kbuild +2 -0
index f384cb1a4f7a..90470c76866a 100644
@@ -26,5 +26,7 @@ obj-y += net/
 
 obj-$(CONFIG_KEXEC_FILE) += purgatory/
 
+obj-y += tests/
+
 # for cleaning
 subdir- += boot tools
arch/x86/Kconfig.debug +16 -0
index d3a6f74a94bd..e4f61af66816 100644
@@ -279,3 +279,19 @@ endchoice
 config FRAME_POINTER
 	depends on !UNWINDER_ORC && !UNWINDER_GUESS
 	bool
+
+config X86_TESTS
+	bool "Tests for x86"
+	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.
+endif # X86_TESTS
arch/x86/kernel/Makefile +7 -0
index 6aef9ee28a39..69472a576909 100644
@@ -24,6 +24,13 @@ CFLAGS_REMOVE_sev.o = -pg
 CFLAGS_REMOVE_cc_platform.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
arch/x86/tests/Makefile new file +1 -0
new file mode 100644
index 000000000000..f66554cd5c45
@@ -0,0 +1 @@
+# SPDX-License-Identifier: GPL-2.0

--
2.32.0

maintainer
#2 lore raw
Quoted 1 line

Subject: Re: [PATCH v6 1/4] x86/tests: Add tests for AMD SEV-ES #VC handling

Your subject need to summarize each patch and not be the same for each patch.

Quoted 29 lines · Vasant Karasulli

Add Kconfig options for testing AMD SEV
related features.

---
 arch/x86/Kbuild          |  2 ++
 arch/x86/Kconfig.debug   | 16 ++++++++++++++++
 arch/x86/kernel/Makefile |  7 +++++++
 arch/x86/tests/Makefile  |  1 +
 4 files changed, 26 insertions(+)
 create mode 100644 arch/x86/tests/Makefile
arch/x86/Kbuild +2 -0
index f384cb1a4f7a..90470c76866a 100644
@@ -26,5 +26,7 @@ obj-y += net/
 
 obj-$(CONFIG_KEXEC_FILE) += purgatory/
 
+obj-y += tests/
+
 # for cleaning
 subdir- += boot tools
arch/x86/Kconfig.debug +0 -0
index d3a6f74a94bd..e4f61af66816 100644

arch/x86/Kconfig.debug endchoice · line 284

Quoted 6 lines
arch/x86/Kconfig.debug +3 -0
@@ -279,3 +279,19 @@ endchoice
 config FRAME_POINTER
 	depends on !UNWINDER_ORC && !UNWINDER_GUESS
 	bool
+
+config X86_TESTS
+	bool "Tests for x86"

"x86 unit tests"

or so.

arch/x86/Kconfig.debug endchoice · line 286

Quoted 2 lines
arch/x86/Kconfig.debug +2 -0
+	help
+	    This enables building the tests under arch/x86/tests.

This needs to explain more what "the tests" are and how people should run them or at least there should be a pointer to a doc how. Running tests should be trivial to mount so that everyone can run them. You want as many people testing stuff as possible so the testing infra needs to be easy to use.

For example, I have no clue how to run those tests.

Also, I have no clue why those tests are in arch/x86/tests/ and not somewhere in tools/testing/selftests/x86/ or so.

All this stuff needs to be explained in the commit message.

Also, you should read

Documentation/process/submitting-patches.rst

first as there it is explained at length how a patch should look like.

Quoted 16 lines
arch/x86/Kconfig.debug +11 -0
+
+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.
+endif # X86_TESTS
arch/x86/kernel/Makefile +0 -0
index 6aef9ee28a39..69472a576909 100644

arch/x86/kernel/Makefile CFLAGS_REMOVE_sev.o = -pg · line 30

Quoted 7 lines
arch/x86/kernel/Makefile +4 -0
@@ -24,6 +24,13 @@ CFLAGS_REMOVE_sev.o = -pg
 CFLAGS_REMOVE_cc_platform.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

Why ifdef?

I think you want this to be enabled unconditionally since the VC tests select KRPOBES.

arch/x86/tests/Makefile line 1

Quoted 13 lines
arch/x86/kernel/Makefile +3 -0
+CFLAGS_sev.o	+= -fno-ipa-sra
+endif
+
 KASAN_SANITIZE_head$(BITS).o				:= n
 KASAN_SANITIZE_dumpstack.o				:= n
 KASAN_SANITIZE_dumpstack_$(BITS).o			:= n
arch/x86/tests/Makefile new file +1 -0
new file mode 100644
index 000000000000..f66554cd5c45
@@ -0,0 +1 @@
+# SPDX-License-Identifier: GPL-2.0

Add that file with the next patch - this hunk is just silly as it is.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#3 lore raw
Quoted 17 lines · Borislav Petkov
Quoted 7 lines

Subject: Re: [PATCH v6 1/4] x86/tests: Add tests for AMD SEV-ES #VC handling

arch/x86/kernel/Makefile +0 -0
index 6aef9ee28a39..69472a576909 100644

arch/x86/kernel/Makefile CFLAGS_REMOVE_sev.o = -pg · line 30

Quoted 7 lines
arch/x86/kernel/Makefile +4 -0
@@ -24,6 +24,13 @@ CFLAGS_REMOVE_sev.o = -pg
 CFLAGS_REMOVE_cc_platform.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

Why ifdef?

Quoted 3 lines

I think you want this to be enabled unconditionally since the VC tests select KRPOBES.

VC tests added in this patch series depend on the configuration option CONFIG_AMD_SEV_TEST_VC which in turn selects KPROBES. I think compiler flag -fno-ipa-sra wouldn't be necessary if configuration option CONFIG_AMD_SEV_TEST_VC is disabled.

Quoted 9 lines

arch/x86/kernel/Makefile CFLAGS_REMOVE_sev.o = -pg · line 33

Quoted 3 lines
arch/x86/kernel/Makefile +3 -0
+CFLAGS_sev.o	+= -fno-ipa-sra
+endif
+

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

--
Vasant Karasulli
Kernel generalist
www.suse.com<http://www.suse.com>
[https://www.suse.com/assets/img/social-platforms-suse-logo.png]<http://www.suse.com/>
SUSE - Open Source Solutions for Enterprise Servers & Cloud<http://www.suse.com/>
Modernize your infrastructure with SUSE Linux Enterprise servers, cloud technology for IaaS, and SUSE's software-defined storage. www.suse.com

#4 lore raw

PATCH v6 2/4 x86/tests: Add tests for AMD SEV-ES #VC handling

In reply to Vasant Karasulli

Add KUnit based tests to validate Linux's
VC handling for instructions cpuid and wbinvd.
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.

---
 arch/x86/tests/Makefile      |   2 +
 arch/x86/tests/sev-test-vc.c | 114 +++++++++++++++++++++++++++++++++++
 2 files changed, 116 insertions(+)
 create mode 100644 arch/x86/tests/sev-test-vc.c
arch/x86/tests/Makefile +2 -0
index f66554cd5c45..4beca64bd2aa 100644
@@ -1 +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 +114 -0

114 lines not on this page · raw message

--
2.32.0

maintainer
#5 lore raw
Quoted 2 lines · Vasant Karasulli

Add KUnit based tests to validate Linux's
VC handling for instructions cpuid and wbinvd.

This looks like a commit message title to me.

Also, please write x86 instructions in all caps: CPUID and WBINVD.

Quoted 6 lines

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.

Yes, I'd appreciate a short step-by-step thing how to run them or a pointer to the docs.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#6 lore raw

PATCH v6 3/4 x86/tests: Add tests for AMD SEV-ES #VC handling

In reply to Vasant Karasulli

Add KUnit based tests to validate Linux's
VC handling for instructions accessing registers such as MSR and DR7. 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.

---
 arch/x86/tests/sev-test-vc.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
arch/x86/tests/sev-test-vc.c +14 -0
index 9d415b9708dc..b27b9f114a12 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>
@@ -99,9 +100,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

#7 lore raw

PATCH v6 4/4 x86/tests: Add tests for AMD SEV-ES #VC handling

In reply to Vasant Karasulli

Add KUnit based tests to validate Linux's VC handling for IO instructions.
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.

---
 arch/x86/tests/sev-test-vc.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
arch/x86/tests/sev-test-vc.c +27 -0
index b27b9f114a12..d0e742e2bf9f 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>
 
@@ -111,11 +113,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