Skip to contentSkip to conversation

Introduce XSAVE feature self-test

PATCH Superseded Pengfei Xu 2 participants 5 messages Jun 14, 2022 – Jun 17, 2022

Subsystems (2)

v9 of 13 Superseded Latest is v13 →

lore
Share & tools
lore

Pengfei Xu

author
#0 lore raw

v9 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 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         | 235 ++++++++++++++++
 tools/testing/selftests/x86/xstate.h         | 267 +++++++++++++++++++
 tools/testing/selftests/x86/xstate_helpers.c | 211 +++++++++++++++
 tools/testing/selftests/x86/xstate_helpers.h |  10 +
 6 files changed, 733 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 v9 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: bunches of cleanups ]

---
 tools/testing/selftests/x86/.gitignore       |   1 +
 tools/testing/selftests/x86/Makefile         |  11 +-
 tools/testing/selftests/x86/xstate.c         | 215 +++++++++++++++
 tools/testing/selftests/x86/xstate.h         | 266 +++++++++++++++++++
 tools/testing/selftests/x86/xstate_helpers.c | 160 +++++++++++
 tools/testing/selftests/x86/xstate_helpers.h |   8 +
 6 files changed, 659 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 +215 -0

215 lines not on this page · raw message

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

266 lines not on this page · raw message

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

160 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
#4 lore raw

PATCH v9 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: bunches of cleanups ]

---
 tools/testing/selftests/x86/xstate.c         | 22 ++++++++-
 tools/testing/selftests/x86/xstate.h         |  1 +
 tools/testing/selftests/x86/xstate_helpers.c | 51 ++++++++++++++++++++
 tools/testing/selftests/x86/xstate_helpers.h |  2 +
 4 files changed, 75 insertions(+), 1 deletion(-)
tools/testing/selftests/x86/xstate.c +21 -1
index 05dabb4733a0..96e6b5db907b 100644
@@ -45,7 +45,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;	\
@@ -135,6 +135,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,
@@ -153,6 +172,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 a60429591c49..17e0f7b17a6e 100644
@@ -87,6 +87,7 @@ enum supportability {
 
 enum xstate_case {
 	XSTATE_CASE_SIG,
+	XSTATE_CASE_FORK,
 	XSTATE_CASE_MAX,
 };
 
tools/testing/selftests/x86/xstate_helpers.c +51 -0

54 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
maintainer
#2 lore raw

Re: [PATCH v9 1/2] selftests/x86/xstate: Add xstate signal handling test for XSAVE feature

In reply to Pengfei Xu
Quoted 128 lines · Pengfei Xu

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: bunches of cleanups ]

---
  tools/testing/selftests/x86/.gitignore       |   1 +
  tools/testing/selftests/x86/Makefile         |  11 +-
  tools/testing/selftests/x86/xstate.c         | 215 +++++++++++++++
  tools/testing/selftests/x86/xstate.h         | 266 +++++++++++++++++++
  tools/testing/selftests/x86/xstate_helpers.c | 160 +++++++++++
  tools/testing/selftests/x86/xstate_helpers.h |   8 +
  6 files changed, 659 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 +51 -0
new file mode 100644
index 000000000000..05dabb4733a0
@@ -0,0 +1,215 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * xstate.c - tests XSAVE feature with fork and signal handling.
+ *
+ * The XSAVE feature set supports the saving and restoring of state components.
+ * It 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.
+ *
+ * The regions and reserved bytes of the components tested for XSAVE feature
+ * are as follows:
+ * x87(FP)/SSE    (0 - 159 bytes)
+ * SSE(XMM part)  (160-415 bytes)
+ * Reserved       (416-511 bytes)
+ * Header_used    (512-527 bytes; XSTATE BV(bitmap vector) mask:512-519 bytes)
+ * Header_reserved(528-575 bytes must be 00)
+ * YMM            (Offset:CPUID.(EAX=0D,ECX=2).EBX Size:CPUID(EAX=0D,ECX=2).EAX)
+ * AVX512_OPMASK  (Offset:CPUID.(EAX=0D,ECX=5).EBX Size:CPUID(EAX=0D,ECX=5).EAX)
+ * ZMM_Hi256      (Offset:CPUID.(EAX=0D,ECX=6).EBX Size:CPUID(EAX=0D,ECX=6).EAX)
+ * Hi16_ZMM       (Offset:CPUID.(EAX=0D,ECX=7).EBX Size:CPUID(EAX=0D,ECX=7).EAX)
+ * PKRU           (Offset:CPUID.(EAX=0D,ECX=9).EBX Size:CPUID(EAX=0D,ECX=9).EAX)
+ */
+
+#define _GNU_SOURCE
+#include <err.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include <signal.h>
+#include <unistd.h>
+#include <sched.h>
+#include <stdbool.h>
+#include <sys/wait.h>
+#include <sys/syscall.h>
+#include <cpuid.h>
+#include <malloc.h>
+#include <stdlib.h>
+
+#include "xstate.h"
+#include "xstate_helpers.h"
+#include "../kselftest.h"
+
+#define NUM_TESTS 1
+#define xstate_test_array_init(idx, init_opt, fill_opt)	\
+	do {						\
+		xstate_tests[idx].init = init_opt;	\

tools/testing/selftests/x86/xstate.c line 61

Quoted 10 lines
tools/testing/selftests/x86/xstate.c new file +10 -0
+		xstate_tests[idx].fill_xbuf = fill_opt;	\
+	} while (0)
+
+static struct xsave_buffer *valid_xbuf, *compared_xbuf;
+static struct xstate_test xstate_tests[XFEATURE_MAX];
+static uint32_t xstate_size;
+
+static bool xstate_in_test(int xfeature_num)
+{
+	return !!(xstate_info.mask & (1 << xfeature_num));

This is used just one. Why do you need a function for this? Also please don't use !! - it is just very hard to read.

Quoted 10 lines
tools/testing/selftests/x86/xstate.c new file +10 -0
+}
+
+static struct xsave_buffer *alloc_xbuf(uint32_t buf_size)
+{
+	struct xsave_buffer *xbuf;
+
+	/* XSAVE buffer should be 64B-aligned. */
+	xbuf = aligned_alloc(64, buf_size);
+	if (!xbuf)
+		ksft_exit_fail_msg("aligned_alloc() failed.\n");

tools/testing/selftests/x86/xstate.c line 81

Quoted 10 lines
tools/testing/selftests/x86/xstate.c new file +10 -0
+
+	return xbuf;
+}
+
+static void free_xbuf(void)
+{
+	free(valid_xbuf);
+	free(compared_xbuf);
+}
+

Again this is called just one. WHy do you need a speacial function for this. Please don't fragment code without a good reason.

tools/testing/selftests/x86/xstate.c line 87

Quoted 6 lines
tools/testing/selftests/x86/xstate.c new file +6 -0
+static void allocate_xbuf(void)
+{
+	valid_xbuf = alloc_xbuf(xstate_size);
+	compared_xbuf = alloc_xbuf(xstate_size);
+}
+

Probably another case of unnecessary function?

Quoted 12 lines
tools/testing/selftests/x86/xstate.c new file +12 -0
+static void show_test_xfeatures(void)
+{
+	uint32_t i;
+	const char *feature_name;
+
+	ksft_print_msg("[NOTE] Test following xstates with mask:%lx.\n",
+		       xstate_info.mask);
+	for (i = 0; i < XFEATURE_MAX; i++) {
+		if (!xstate_in_test(i))
+			continue;
+		feature_name = xfeature_names[i];
+		ksft_print_msg("[NOTE] XSAVE feature num %02d: '%s'.\n", i,

tools/testing/selftests/x86/xstate.c line 109

Quoted 10 lines
tools/testing/selftests/x86/xstate.c new file +10 -0
+			       feature_name);
+	}
+}
+
+static inline void set_xstatebv(struct xsave_buffer *buffer, uint64_t bv)
+{
+	/* XSTATE_BV is at the beginning of xstate header. */
+	*(uint64_t *)(&buffer->header) = bv;
+}
+

Okay - if you have a function - I want to see it called at least 2 times. Having so many little function breaks up the code for no good reason.

Let's fix these first and in both patches.

thanks,
-- Shuah

author
#3 lore raw

Re: [PATCH v9 1/2] selftests/x86/xstate: Add xstate signal handling test for XSAVE feature

Hi Shuah,

Quoted 130 lines · Shuah Khan
Quoted 129 lines · Pengfei Xu

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: bunches of cleanups ]

---
  tools/testing/selftests/x86/.gitignore       |   1 +
  tools/testing/selftests/x86/Makefile         |  11 +-
  tools/testing/selftests/x86/xstate.c         | 215 +++++++++++++++
  tools/testing/selftests/x86/xstate.h         | 266 +++++++++++++++++++
  tools/testing/selftests/x86/xstate_helpers.c | 160 +++++++++++
  tools/testing/selftests/x86/xstate_helpers.h |   8 +
  6 files changed, 659 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 +56 -0
new file mode 100644
index 000000000000..05dabb4733a0
@@ -0,0 +1,215 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * xstate.c - tests XSAVE feature with fork and signal handling.
+ *
+ * The XSAVE feature set supports the saving and restoring of state components.
+ * It 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.
+ *
+ * The regions and reserved bytes of the components tested for XSAVE feature
+ * are as follows:
+ * x87(FP)/SSE    (0 - 159 bytes)
+ * SSE(XMM part)  (160-415 bytes)
+ * Reserved       (416-511 bytes)
+ * Header_used    (512-527 bytes; XSTATE BV(bitmap vector) mask:512-519 bytes)
+ * Header_reserved(528-575 bytes must be 00)
+ * YMM            (Offset:CPUID.(EAX=0D,ECX=2).EBX Size:CPUID(EAX=0D,ECX=2).EAX)
+ * AVX512_OPMASK  (Offset:CPUID.(EAX=0D,ECX=5).EBX Size:CPUID(EAX=0D,ECX=5).EAX)
+ * ZMM_Hi256      (Offset:CPUID.(EAX=0D,ECX=6).EBX Size:CPUID(EAX=0D,ECX=6).EAX)
+ * Hi16_ZMM       (Offset:CPUID.(EAX=0D,ECX=7).EBX Size:CPUID(EAX=0D,ECX=7).EAX)
+ * PKRU           (Offset:CPUID.(EAX=0D,ECX=9).EBX Size:CPUID(EAX=0D,ECX=9).EAX)
+ */
+
+#define _GNU_SOURCE
+#include <err.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include <signal.h>
+#include <unistd.h>
+#include <sched.h>
+#include <stdbool.h>
+#include <sys/wait.h>
+#include <sys/syscall.h>
+#include <cpuid.h>
+#include <malloc.h>
+#include <stdlib.h>
+
+#include "xstate.h"
+#include "xstate_helpers.h"
+#include "../kselftest.h"
+
+#define NUM_TESTS 1
+#define xstate_test_array_init(idx, init_opt, fill_opt)	\
+	do {						\
+		xstate_tests[idx].init = init_opt;	\
+		xstate_tests[idx].fill_xbuf = fill_opt;	\
+	} while (0)
+
+static struct xsave_buffer *valid_xbuf, *compared_xbuf;
+static struct xstate_test xstate_tests[XFEATURE_MAX];
Quoted 9 lines

tools/testing/selftests/x86/xstate.c line 61

Quoted 5 lines
tools/testing/selftests/x86/xstate.c new file +5 -0
+static uint32_t xstate_size;
+
+static bool xstate_in_test(int xfeature_num)
+{
+	return !!(xstate_info.mask & (1 << xfeature_num));

This is used just one. Why do you need a function for this? Also please don't use !! - it is just very hard to read.

Ok, this function is only used once, will remove it. Thanks!

Quoted 15 lines
Quoted 15 lines
tools/testing/selftests/x86/xstate.c new file +15 -0
+}
+
+static struct xsave_buffer *alloc_xbuf(uint32_t buf_size)
+{
+	struct xsave_buffer *xbuf;
+
+	/* XSAVE buffer should be 64B-aligned. */
+	xbuf = aligned_alloc(64, buf_size);
+	if (!xbuf)
+		ksft_exit_fail_msg("aligned_alloc() failed.\n");
+
+	return xbuf;
+}
+
+static void free_xbuf(void)
Quoted 9 lines

tools/testing/selftests/x86/xstate.c line 81

Quoted 5 lines
tools/testing/selftests/x86/xstate.c new file +5 -0
+{
+	free(valid_xbuf);
+	free(compared_xbuf);
+}
+

Again this is called just one. WHy do you need a speacial function for this. Please don't fragment code without a good reason.

Will fix it, thanks!

Quoted 9 lines

tools/testing/selftests/x86/xstate.c line 87

Quoted 6 lines
tools/testing/selftests/x86/xstate.c new file +6 -0
+static void allocate_xbuf(void)
+{
+	valid_xbuf = alloc_xbuf(xstate_size);
+	compared_xbuf = alloc_xbuf(xstate_size);
+}
+

Probably another case of unnecessary function?

Will fix it, thanks!

Quoted 26 lines
Quoted 12 lines
tools/testing/selftests/x86/xstate.c new file +12 -0
+static void show_test_xfeatures(void)
+{
+	uint32_t i;
+	const char *feature_name;
+
+	ksft_print_msg("[NOTE] Test following xstates with mask:%lx.\n",
+		       xstate_info.mask);
+	for (i = 0; i < XFEATURE_MAX; i++) {
+		if (!xstate_in_test(i))
+			continue;
+		feature_name = xfeature_names[i];
+		ksft_print_msg("[NOTE] XSAVE feature num %02d: '%s'.\n", i,

tools/testing/selftests/x86/xstate.c line 109

Quoted 10 lines
tools/testing/selftests/x86/xstate.c new file +10 -0
+			       feature_name);
+	}
+}
+
+static inline void set_xstatebv(struct xsave_buffer *buffer, uint64_t bv)
+{
+	/* XSTATE_BV is at the beginning of xstate header. */
+	*(uint64_t *)(&buffer->header) = bv;
+}
+

Okay - if you have a function - I want to see it called at least 2 times. Having so many little function breaks up the code for no good reason.

Quoted 1 line

Let's fix these first and in both patches.

Sure, I will collect all the comments and fix them in next version.

Thanks!
-- Pengfei

Quoted 3 lines

thanks,
-- Shuah