Skip to contentSkip to conversation

Introduce XSAVE feature self-test

PATCH Superseded Pengfei Xu 1 participant 3 messages Jun 22, 2022

Subsystems (2)

v10 of 13 Superseded Latest is v13 →

lore
Share & tools
lore

Pengfei Xu

author
#0 lore raw

v10 cover Introduce XSAVE feature self-test

The XSAVE feature set supports the saving and restoring of xstate components. XSAVE feature has been used for process context switching. XSAVE components include x87 state for FP execution environment, SSE state, AVX state and so on.

In order to ensure that XSAVE works correctly, add XSAVE most basic test for XSAVE architecture functionality.

This patch tests "FP, SSE(XMM), AVX2(YMM), AVX512_OPMASK/AVX512_ZMM_Hi256/ AVX512_Hi16_ZMM and PKRU parts" xstates with following cases:
1. The contents of these xstates in the process should not change after the signal handling.
2. The contents of these xstates in the child process should be the same as the contents of the xstate in the parent process after the fork syscall.
3. The contents of xstates in the parent process should not change after the context switch.

Because xstate like XMM will not be preserved across function calls, fork() and raise() are implemented and inlined.
To prevent GCC from generating any FP/SSE(XMM)/AVX/PKRU code, add "-mno-sse -mno-mmx -mno-sse2 -mno-avx -mno-pku" compiler arguments. stdlib.h can not be used because of the "-mno-sse" option.
Thanks Dave, Hansen for the above suggestion!
Thanks Chen Yu; Shuah Khan; Chatre Reinette and Tony Luck's comments! Thanks to Bae, Chang Seok for a bunch of comments!

========
- Change from v9 to v10
- Remove the small function if the function will be called once and there is no good reason. (Shuah Khan)

- Change from v8 to v9
- Use function pointers to make it more structured. (Hansen, Dave)
- Improve the function name: xstate_tested -> xstate_in_test. (Chang S. Bae)
- Break this test up into two pieces: keep the xstate key test steps with "-mno-sse" and no stdlib.h, keep others in xstate.c file. (Hansen, Dave)
- Use kselftest infrastructure for xstate.c file. (Hansen, Dave)
- Use instruction back to populate fp xstate buffer. (Hansen, Dave)
- Will skip the test if cpu could not support xsave. (Chang S. Bae)
- Use __cpuid_count() helper in kselftest.h. (Reinette, Chatre)

- Change from v7 to v8
Many thanks to Bae, Chang Seok for a bunch of comments as follow:
- Use the filling buffer way to prepare the xstate buffer, and use xrstor instruction way to load the tested xstates.
- Remove useless dump_buffer, compare_buffer functions.
- Improve the struct of xstate_info.
- Added AVX512_ZMM_Hi256 and AVX512_Hi16_ZMM components in xstate test.
- Remove redundant xstate_info.xstate_mask, xstate_flag[], and xfeature_test_mask, use xstate_info.mask instead.
- Check if xfeature is supported outside of fill_xstate_buf() , this change is easier to read and understand.
- Remove useless wrpkru, only use filling all tested xstate buffer in

    fill_xstates_buf().

- Improve a bunch of function names and variable names.
- Improve test steps flow for readability.

- Change from v6 to v7:
- Added the error number and error description of the reason for the failure, thanks Shuah Khan's suggestion.
- Added a description of what these tests are doing in the head comments.
- Added changes update in the head comments.
- Added description of the purpose of the function. thanks Shuah Khan.

- Change from v5 to v6:
- In order to prevent GCC from generating any FP code by mistake, "-mno-sse -mno-mmx -mno-sse2 -mno-avx -mno-pku" compiler parameter was added, it's referred to the parameters for compiling the x86 kernel. Thanks Dave Hansen's suggestion.
- Removed the use of "kselftest.h", because kselftest.h included <stdlib.h>, and "stdlib.h" would use sse instructions in it's libc, and this *XSAVE* test needed to be compiled without libc sse instructions(-mno-sse).
- Improved the description in commit header, thanks Chen Yu's suggestion.
- Becasue test code could not use buildin xsave64 in libc without sse, added xsave function by instruction way.
- Every key test action would not use libc(like printf) except syscall until it's failed or done. If it's failed, then it would print the failed reason.
- Used __cpuid_count() instead of native_cpuid(), becasue __cpuid_count() was a macro definition function with one instruction in libc and did not change xstate. Thanks Chatre Reinette, Shuah Khan.
https://lore.kernel.org/linux-sgx/[email protected]/

- Change from v4 to v5:
- Moved code files into tools/testing/selftests/x86.
- Delete xsave instruction test, becaue it's not related to kernel.
- Improved case description.
- Added AVX512 opmask change and related XSAVE content verification.
- Added PKRU part xstate test into instruction and signal handling test.
- Added XSAVE process swich test for FPU, AVX2, AVX512 opmask and PKRU part.

- Change from v3 to v4:
- Improve the comment in patch 1.

- Change from v2 to v3:
- Improve the description of patch 2 git log.

- Change from v1 to v2:
- Improve the cover-letter. Thanks Dave Hansen's suggestion.

Pengfei Xu (2):
selftests/x86/xstate: Add xstate signal handling test for XSAVE
feature
selftests/x86/xstate: Add xstate fork test for XSAVE feature

 tools/testing/selftests/x86/.gitignore       |   1 +
 tools/testing/selftests/x86/Makefile         |  11 +-
 tools/testing/selftests/x86/xstate.c         | 208 +++++++++++++++
 tools/testing/selftests/x86/xstate.h         | 262 +++++++++++++++++++
 tools/testing/selftests/x86/xstate_helpers.c | 209 +++++++++++++++
 tools/testing/selftests/x86/xstate_helpers.h |  10 +
 6 files changed, 699 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/x86/xstate.c
 create mode 100644 tools/testing/selftests/x86/xstate.h
 create mode 100644 tools/testing/selftests/x86/xstate_helpers.c
 create mode 100644 tools/testing/selftests/x86/xstate_helpers.h
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH v10 1/2 selftests/x86/xstate: Add xstate signal handling test for XSAVE feature

The XSAVE feature set supports the saving and restoring of xstate components.

In order to ensure that XSAVE works correctly, add XSAVE most basic signal handling test for XSAVE architecture functionality, this patch tests "FP, SSE(XMM), AVX2(YMM), AVX512_OPMASK/AVX512_ZMM_Hi256/AVX512_Hi16_ZMM and PKRU" xstates with the following:
The contents of these xstates in the process should not change after the signal handling.

[ Dave Hansen; Chang S. Bae; Shuah Khan: bunches of cleanups ]

---
 tools/testing/selftests/x86/.gitignore       |   1 +
 tools/testing/selftests/x86/Makefile         |  11 +-
 tools/testing/selftests/x86/xstate.c         | 188 +++++++++++++
 tools/testing/selftests/x86/xstate.h         | 261 +++++++++++++++++++
 tools/testing/selftests/x86/xstate_helpers.c | 142 ++++++++++
 tools/testing/selftests/x86/xstate_helpers.h |   8 +
 6 files changed, 609 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/x86/xstate.c
 create mode 100644 tools/testing/selftests/x86/xstate.h
 create mode 100644 tools/testing/selftests/x86/xstate_helpers.c
 create mode 100644 tools/testing/selftests/x86/xstate_helpers.h
tools/testing/selftests/x86/.gitignore +1 -0
index 1aaef5bf119a..68951ceefe30 100644
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 *_32
 *_64
+*.o
 single_step_syscall
 sysret_ss_attrs
 syscall_nt
tools/testing/selftests/x86/Makefile +9 -2
index 0388c4d60af0..49a6d78e0831 100644
@@ -18,7 +18,7 @@ TARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \
 			test_FCMOV test_FCOMI test_FISTTP \
 			vdso_restorer
 TARGETS_C_64BIT_ONLY := fsgsbase sysret_rip syscall_numbering \
-			corrupt_xstate_header amx
+			corrupt_xstate_header amx xstate
 # Some selftests require 32bit support enabled also on 64bit systems
 TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
 
@@ -69,7 +69,7 @@ all_32: $(BINARIES_32)
 
 all_64: $(BINARIES_64)
 
-EXTRA_CLEAN := $(BINARIES_32) $(BINARIES_64)
+EXTRA_CLEAN := $(BINARIES_32) $(BINARIES_64) *.o
 
 $(BINARIES_32): $(OUTPUT)/%_32: %.c helpers.h
 	$(CC) -m32 -o $@ $(CFLAGS) $(EXTRA_CFLAGS) $^ -lrt -ldl -lm
@@ -109,3 +109,10 @@ $(OUTPUT)/test_syscall_vdso_32: thunks_32.S
 # state.
 $(OUTPUT)/check_initial_reg_state_32: CFLAGS += -Wl,-ereal_start -static
 $(OUTPUT)/check_initial_reg_state_64: CFLAGS += -Wl,-ereal_start -static
+
+# xstate_64 is special: it needs xstate_helpers.o to prevent GCC from
+# generating any FP code by mistake and stdlib.h can't be used due to
+# "-mno-sse" parameter, so compile xstate_64 with the code file xstate.c
+# which can use stdlib.h and xstate_helpers.o which cannot use stdlib.h
+xstate_helpers.o: CFLAGS += -mno-sse -mno-mmx -mno-sse2 -mno-avx -mno-pku
+$(OUTPUT)/xstate_64: xstate_helpers.o
tools/testing/selftests/x86/xstate.c new file +188 -0

188 lines not on this page · raw message

tools/testing/selftests/x86/xstate.h new file +261 -0

261 lines not on this page · raw message

tools/testing/selftests/x86/xstate_helpers.c new file +142 -0

142 lines not on this page · raw message

tools/testing/selftests/x86/xstate_helpers.h new file +8 -0
new file mode 100644
index 000000000000..1806c0bf484b
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#include <stdint.h>
+
+extern void fill_fp_mxcsr_xstate_buf(void *buf, int xfeature_num,
+				     uint8_t ui8_fp);
+extern bool xstate_sig_handle(void *valid_xbuf, void *compared_xbuf,
+			      uint64_t mask, uint32_t xstate_size);
Signature · 1 line
2.31.1
author
#2 lore raw

PATCH v10 2/2 selftests/x86/xstate: Add xstate fork test for XSAVE feature

In reply to Pengfei Xu

In order to ensure that XSAVE works correctly, add XSAVE most basic fork test:
1. The contents of these xstates in the child process should be the same as the contents of the xstate in the parent process after the fork syscall.
2. The contents of xstates in the parent process should not change after the context switch.

[ Dave Hansen; Chang S. Bae; Shuah Khan: bunches of cleanups ]

---
 tools/testing/selftests/x86/xstate.c         | 22 ++++++-
 tools/testing/selftests/x86/xstate.h         |  1 +
 tools/testing/selftests/x86/xstate_helpers.c | 67 ++++++++++++++++++++
 tools/testing/selftests/x86/xstate_helpers.h |  2 +
 4 files changed, 91 insertions(+), 1 deletion(-)
tools/testing/selftests/x86/xstate.c +21 -1
index 4eff539e783d..4abb46fe40dc 100644
@@ -39,7 +39,7 @@
 #include "xstate_helpers.h"
 #include "../kselftest.h"
 
-#define NUM_TESTS	1
+#define NUM_TESTS	3
 #define xstate_test_array_init(idx, init_opt, fill_opt)	\
 	do {						\
 		xstate_tests[idx].init = init_opt;	\
@@ -106,6 +106,25 @@ static void test_xstate_sig_handle(void)
 	compare_buf_result(valid_xbuf, compared_xbuf, case_name1);
 }
 
+static void test_xstate_fork(void)
+{
+	const char *case_name2 = "xstate of child process should be same as xstate of parent";
+	const char *case_name3 = "parent xstate should be same after context switch";
+
+	ksft_print_msg("[RUN]\tParent pid:%d check xstate around fork test.\n",
+		       getpid());
+	/* Child process xstate should be same as the parent process xstate. */
+	if (xstate_fork(valid_xbuf, compared_xbuf, xstate_info.mask,
+	    xstate_size)) {
+		ksft_test_result_pass("The case: %s.\n", case_name2);
+	} else {
+		ksft_test_result_fail("The case: %s.\n", case_name2);
+	}
+
+	/* The parent process xstate should not change after context switch. */
+	compare_buf_result(valid_xbuf, compared_xbuf, case_name3);
+}
+
 static void prepare_xstate_test(void)
 {
 	xstate_test_array_init(XFEATURE_FP, init_legacy_info,
@@ -124,6 +143,7 @@ static void prepare_xstate_test(void)
 			       fill_pkru_xstate_buf);
 
 	xstate_tests[XSTATE_CASE_SIG].xstate_case = test_xstate_sig_handle;
+	xstate_tests[XSTATE_CASE_FORK].xstate_case = test_xstate_fork;
 }
 
 static void test_xstate(void)
tools/testing/selftests/x86/xstate.h +1 -0
index 92c853d64e99..c9ec4cbf141e 100644
@@ -82,6 +82,7 @@ enum xfeature {
 
 enum xstate_case {
 	XSTATE_CASE_SIG,
+	XSTATE_CASE_FORK,
 	XSTATE_CASE_MAX,
 };
 
tools/testing/selftests/x86/xstate_helpers.c +67 -0

76 lines not on this page · raw message

tools/testing/selftests/x86/xstate_helpers.h +2 -0
index 1806c0bf484b..307781708ffa 100644
@@ -6,3 +6,5 @@ extern void fill_fp_mxcsr_xstate_buf(void *buf, int xfeature_num,
 				     uint8_t ui8_fp);
 extern bool xstate_sig_handle(void *valid_xbuf, void *compared_xbuf,
 			      uint64_t mask, uint32_t xstate_size);
+extern bool xstate_fork(void *valid_xbuf, void *compared_xbuf,
+			uint64_t mask, uint32_t xstate_size);
Signature · 1 line
2.31.1