Introduce XSAVE feature self-test
v7 cover Introduce XSAVE feature self-test
The XSAVE feature set supports the saving and restoring of xstate components, which is used for process context switching. The state components include x87 state for FPU execution environment, SSE state, AVX state and so on. In order to ensure that XSAVE works correctly, add XSAVE basic test for XSAVE architecture functionality.
This patch set tests and verifies the basic functions of XSAVE in user space; it tests "FPU, SSE(XMM), AVX2(YMM), AVX512 opmask and PKRU parts" xstates with following cases:
1. In nested signal processing, the signal handling will use each signal's own xstates, and the xstates of the signal handling under test should not be changed after another nested signal handling is completed; and these xstates content in the process should not change after the nested signal handling is complete.
2. The xstates content of the child process should be the same as that of the parent process. The xstates content of the process should be the same across process switching.
This is the xstates position for FP, XMM, Header, YMM, AVX512 opmask and PKRU:
It could be saved by xsave instruction, and mask could control which part will be saved(Header will be saved as mandatory):
FP (0 - 159 bytes) XMM (160-415 bytes) Reserved (416-511 bytes SDM vol1 13.4.1) Header_used (512-527 bytes) Headed_reserved (528-575 bytes must 00, otherwise xrstor will #GP) 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) PKRU (Offset:CPUID.(EAX=0D,ECX=9).EBX Size:CPUID(EAX=0D,ECX=9).EAX)
It uses syscall function instead of fork() function, becasue syscall libc function will resume xstates after syscall if there is some xstates change in syscall libc function.
And populate the xstates will try not to use libc, and every key test action will try not to use libc except syscall until it's failed or done. In order to prevent GCC from generating any FP code by mistake, "-mno-sse -mno-mmx -mno-sse2 -mno-avx -mno-pku" compiler parameter is added to avoid fake failure. Thanks Dave Hansen's suggestion.
This series introduces only the most basic XSAVE tests. In the future, the intention is to continue expanding the scope of these selftests to include more xstates and kernel XSAVE-related functionality tests.
========
- 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: add xsave test related to nested signal handling selftests/x86: add xsave test related to process switching
tools/testing/selftests/x86/Makefile | 4 +- tools/testing/selftests/x86/xsave_common.h | 397 ++++++++++++++++++ tools/testing/selftests/x86/xsave_fork_test.c | 148 +++++++ .../selftests/x86/xsave_signal_handle.c | 189 +++++++++ 4 files changed, 737 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/x86/xsave_common.h create mode 100644 tools/testing/selftests/x86/xsave_fork_test.c create mode 100644 tools/testing/selftests/x86/xsave_signal_handle.c
Signature · 1 line
2.31.1
RFC PATCH v7 1/2 selftests/x86: add xsave test related to nested signal handling
In order to ensure that content of FPU, SSE(XMM), AVX2(YMM), AVX512 opmask and PKRU xstates in the same process is not affected by signal handling, this case tests that:
1. In nested signal processing, the signal handling uses each signal's own xstates, and the xstates of the signal handling under test should not be changed after previous nested signal handling is completed;
2. The xstates content of the process should not change after the entire nested signal handling.
Process will change above mentioned xstates before signal handling test to ensure that these xstates have been tested.
--- tools/testing/selftests/x86/Makefile | 3 +- tools/testing/selftests/x86/xsave_common.h | 397 ++++++++++++++++++ .../selftests/x86/xsave_signal_handle.c | 189 +++++++++ 3 files changed, 588 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/x86/xsave_common.h create mode 100644 tools/testing/selftests/x86/xsave_signal_handle.c
tools/testing/selftests/x86/Makefile +2 -1
@@ -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 xsave_signal_handle # Some selftests require 32bit support enabled also on 64bit systems TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
@@ -105,3 +105,4 @@ $(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 +$(OUTPUT)/xsave_signal_handle_64: CFLAGS += -mno-sse -mno-mmx -mno-sse2 -mno-avx -mno-pku
tools/testing/selftests/x86/xsave_common.h new file +397 -0
397 lines not on this page · raw message
tools/testing/selftests/x86/xsave_signal_handle.c new file +189 -0
189 lines not on this page · raw message
Signature · 1 line
2.31.1
RFC PATCH v7 2/2 selftests/x86: add xsave test related to process switching
In reply to Pengfei XuIt will change FPU, SSE(XMM), AVX2(YMM), AVX512, PKRU xstates before process switching test to ensure that these xstates have been tested.
In order to ensure that the content of xstates is not affected across process switching, this case tests that:
1. The xstates content of the child process should be the same as that of the parent process.
2. The xstates content of the process should be the same across process switching.
--- tools/testing/selftests/x86/Makefile | 3 +- tools/testing/selftests/x86/xsave_fork_test.c | 148 ++++++++++++++++++ 2 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/x86/xsave_fork_test.c
tools/testing/selftests/x86/Makefile +2 -1
@@ -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 xsave_signal_handle + corrupt_xstate_header amx xsave_signal_handle xsave_fork_test # Some selftests require 32bit support enabled also on 64bit systems TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
@@ -106,3 +106,4 @@ $(OUTPUT)/test_syscall_vdso_32: thunks_32.S $(OUTPUT)/check_initial_reg_state_32: CFLAGS += -Wl,-ereal_start -static $(OUTPUT)/check_initial_reg_state_64: CFLAGS += -Wl,-ereal_start -static $(OUTPUT)/xsave_signal_handle_64: CFLAGS += -mno-sse -mno-mmx -mno-sse2 -mno-avx -mno-pku +$(OUTPUT)/xsave_fork_test_64: CFLAGS += -mno-sse -mno-mmx -mno-sse2 -mno-avx -mno-pku
tools/testing/selftests/x86/xsave_fork_test.c new file +148 -0
148 lines not on this page · raw message
Signature · 1 line
2.31.1