selftests/x86/xstate: Introduce common code for testing extended states
cover selftests/x86/xstate: Introduce common code for testing extended states
Hi all,
This series proposes a rework of xstate-related tests to improve maintainability and expand test coverage.
== Motivation: Addressing Missing and New XSTATE Tests ==
With the introduction of AMX, a new test suite [1] was created to verify dynamic state handling by the kernel as observed from userspace. However, previous tests for non-dynamic states like AVX lacked ABI validation, leaving gaps in coverage. While these states currently function without major issues (following the alternate sigstack fix [2]), xstate testing in the x86 selftest suite has been largely overlooked.
Now, with Intel introducing another extended state, Advanced Performance Extensions (APX) [3], a correspondent test case is need. The APX enabling series will follow shortly and will leverage this refactored selftest framework.
== Selftest Code Rework ==
To ensure ABI validation and core functionality across various xstates, refactoring the test code is necessary. Without this, existing code from amx.c would need to be duplicated, compromising the structural quality of xstate tests.
This series introduces a shared test framework for extended state validation, applicable to both existing and new xstates. The test cases cover:
* Context switching
* ABI compatibility for signal handling
* ABI compatibility for ptrace interactions
== Patch Organization ==
The patchset is structured as follows:
* PATCH1: Preparatory cleanup — removing redundant signal handler registration code.
* PATCH2/3: Introduce low-level XSAVE helpers and xstate component enumeration.
* PATCH4/5: Refactor existing test code.
* PATCH6: Introduce a new signal test case.
* PATCH7/8: Consolidate test invocations and clarify the list of supported features.
* PATCH9: Add test coverage for AVX.
== Coverage and Future Work Considerations ==
Currently, these tests are aligned with 64-bit mode only. Support for 32-bit cases will be considered when necessary, but only after this phase of rework is finalized.
FWIW, the AMX TILECFG state is trivial, requiring almost constant values. Additionally, various PKRU tests are already established in tools/selftests/mm.
This series is based on the tip/master branch. You can also find it in the following repository:
git://github.com/intel/apx.git selftest-xstate_v1
Thanks,
Chang
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://www.intel.com/content/www/us/en/developer/articles/technical/advanced-performance-extensions-apx.html
Chang S. Bae (9):
selftests/x86: Consolidate redundant signal helper functions selftests/x86/xstate: Refactor XSAVE helpers for general use selftests/x86/xstate: Enumerate and name xstate components selftests/x86/xstate: Refactor context switching test selftests/x86/xstate: Refactor ptrace ABI test selftests/x86/xstate: Introduce signal ABI test selftests/x86/xstate: Consolidate test invocations into a single entry selftests/x86/xstate: Clarify supported xstates selftests/x86/avx: Add AVX test
tools/testing/selftests/x86/Makefile | 6 +- tools/testing/selftests/x86/amx.c | 442 +--------------- tools/testing/selftests/x86/avx.c | 12 + .../selftests/x86/corrupt_xstate_header.c | 14 +- tools/testing/selftests/x86/entry_from_vm86.c | 24 +- tools/testing/selftests/x86/fsgsbase.c | 24 +- tools/testing/selftests/x86/helpers.h | 28 + tools/testing/selftests/x86/ioperm.c | 25 +- tools/testing/selftests/x86/iopl.c | 25 +- tools/testing/selftests/x86/ldt_gdt.c | 18 +- tools/testing/selftests/x86/mov_ss_trap.c | 14 +- tools/testing/selftests/x86/ptrace_syscall.c | 24 +- tools/testing/selftests/x86/sigaltstack.c | 26 +- tools/testing/selftests/x86/sigreturn.c | 24 +- .../selftests/x86/single_step_syscall.c | 22 - .../testing/selftests/x86/syscall_arg_fault.c | 12 - tools/testing/selftests/x86/syscall_nt.c | 12 - tools/testing/selftests/x86/sysret_rip.c | 24 +- tools/testing/selftests/x86/test_vsyscall.c | 13 - tools/testing/selftests/x86/unwind_vdso.c | 12 - tools/testing/selftests/x86/xstate.c | 477 ++++++++++++++++++ tools/testing/selftests/x86/xstate.h | 195 +++++++ 22 files changed, 753 insertions(+), 720 deletions(-) create mode 100644 tools/testing/selftests/x86/avx.c create mode 100644 tools/testing/selftests/x86/xstate.c create mode 100644 tools/testing/selftests/x86/xstate.h
base-commit: 5bff053d066ba892464995ae4a7246f7a78fce2d
Signature · 1 line
2.45.2
PATCH 1/9 selftests/x86: Consolidate redundant signal helper functions
The x86 selftests frequently register and clean up signal handlers, but the sethandler() and clearhandler() functions have been redundantly copied across multiple .c files.
Move these functions to helpers.h to enable reuse across tests, eliminating around 250 lines of duplicate code.
Converge the error handling by using ksft_exit_fail_msg(), which is functionally equivalent with err() within the selftest framework.
---
This change is a prerequisite for the upcoming xstate selftest, which requires signal handling for registering and cleaning up handlers.
References:
* ksft_exit_fail_msg()
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/testing/selftests/kselftest.h#n383
* err()
https://sourceware.org/git/?p=glibc.git;a=blob;f=misc/err.c;h=2528d36e2da0f9c020c9a0bea3e18ae444f17ea8;hb=HEAD#l103
--- tools/testing/selftests/x86/amx.c | 25 +---------------- .../selftests/x86/corrupt_xstate_header.c | 14 +--------- tools/testing/selftests/x86/entry_from_vm86.c | 24 ++-------------- tools/testing/selftests/x86/fsgsbase.c | 24 ++-------------- tools/testing/selftests/x86/helpers.h | 28 +++++++++++++++++++ tools/testing/selftests/x86/ioperm.c | 25 ++--------------- tools/testing/selftests/x86/iopl.c | 25 ++--------------- tools/testing/selftests/x86/ldt_gdt.c | 18 +++--------- tools/testing/selftests/x86/mov_ss_trap.c | 14 +--------- tools/testing/selftests/x86/ptrace_syscall.c | 24 ++-------------- tools/testing/selftests/x86/sigaltstack.c | 26 ++--------------- tools/testing/selftests/x86/sigreturn.c | 24 ++-------------- .../selftests/x86/single_step_syscall.c | 22 --------------- .../testing/selftests/x86/syscall_arg_fault.c | 12 -------- tools/testing/selftests/x86/syscall_nt.c | 12 -------- tools/testing/selftests/x86/sysret_rip.c | 24 ++-------------- tools/testing/selftests/x86/test_vsyscall.c | 13 --------- tools/testing/selftests/x86/unwind_vdso.c | 12 -------- 18 files changed, 51 insertions(+), 315 deletions(-)
tools/testing/selftests/x86/amx.c +1 -24
@@ -20,6 +20,7 @@#include <sys/uio.h> #include "../kselftest.h" /* For __cpuid_count() */ +#include "helpers.h" #ifndef __x86_64__# error This test is 64-bit only
@@ -61,30 +62,6 @@ static inline void xrstor(struct xsave_buffer *xbuf, uint64_t rfbm)/* err() exits and will not return */ #define fatal_error (msg, ...) err(1, "[FAIL]\t" msg, ##__VA_ARGS__) -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; - -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -fatal_error ("sigaction" ); -} - -static void clearhandler (int sig) -{ -struct sigaction sa; - -memset (&sa,0 ,sizeof (sa)); - sa.sa_handler =SIG_DFL ; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -fatal_error ("sigaction" ); -} -#define XFEATURE_XTILECFG 17#define XFEATURE_XTILEDATA 18#define XFEATURE_MASK_XTILECFG (1 << XFEATURE_XTILECFG)
tools/testing/selftests/x86/corrupt_xstate_header.c +1 -13
@@ -18,6 +18,7 @@#include <sys/wait.h> #include "../kselftest.h" /* For __cpuid_count() */ +#include "helpers.h" static inline int xsave_enabled (void ) {
@@ -29,19 +30,6 @@ static inline int xsave_enabled(void)return ecx & (1U <<27 ); } -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; - -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static void sigusr1 (int sig,siginfo_t *info,void *uc_void) {ucontext_t *uc = uc_void;
tools/testing/selftests/x86/entry_from_vm86.c +2 -22
@@ -24,31 +24,11 @@#include <errno.h> #include <sys/vm86.h> +#include "helpers.h" +static unsigned long load_addr =0x10000 ;static int nerrs =0 ; -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} - -static void clearhandler (int sig) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_handler =SIG_DFL ; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static sig_atomic_t got_signal;static void sighandler (int sig,siginfo_t *info,void *ctx_void)
tools/testing/selftests/x86/fsgsbase.c +2 -22
@@ -28,6 +28,8 @@#include <sys/wait.h> #include <setjmp.h> +#include "helpers.h" +#ifndef __x86_64__# error This test is 64-bit only#endif
@@ -39,28 +41,6 @@ static unsigned short *shared_scratch;static int nerrs; -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} - -static void clearhandler (int sig) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_handler =SIG_DFL ; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static void sigsegv (int sig,siginfo_t *si,void *ctx_void) {ucontext_t *ctx = (ucontext_t *)ctx_void;
tools/testing/selftests/x86/helpers.h +28 -0
@@ -2,8 +2,13 @@#ifndef __SELFTESTS_X86_HELPERS_H#define __SELFTESTS_X86_HELPERS_H +#include <signal.h> +#include <string.h> +#include <asm/processor-flags.h> +#include "../kselftest.h" +static inline unsigned long get_eflags (void ) {#ifdef __x86_64__
@@ -22,4 +27,27 @@ static inline void set_eflags(unsigned long eflags)#endif } +static inline void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *),int flags) +{ +struct sigaction sa; + +memset (&sa,0 ,sizeof (sa)); + sa.sa_sigaction = handler; + sa.sa_flags =SA_SIGINFO | flags; +sigemptyset (&sa.sa_mask); +if (sigaction (sig, &sa,0 )) +ksft_exit_fail_msg ("sigaction failed" ); +} + +static inline void clearhandler (int sig) +{ +struct sigaction sa; + +memset (&sa,0 ,sizeof (sa)); + sa.sa_handler =SIG_DFL ; +sigemptyset (&sa.sa_mask); +if (sigaction (sig, &sa,0 )) +ksft_exit_fail_msg ("sigaction failed" ); +} +#endif /* __SELFTESTS_X86_HELPERS_H */
tools/testing/selftests/x86/ioperm.c +2 -23
@@ -20,31 +20,10 @@#include <sched.h> #include <sys/io.h> +#include "helpers.h" +static int nerrs =0 ; -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); - -} - -static void clearhandler (int sig) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_handler =SIG_DFL ; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static jmp_buf jmpbuf;static void sigsegv (int sig,siginfo_t *si,void *ctx_void)
tools/testing/selftests/x86/iopl.c +2 -23
@@ -20,31 +20,10 @@#include <sched.h> #include <sys/io.h> +#include "helpers.h" +static int nerrs =0 ; -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); - -} - -static void clearhandler (int sig) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_handler =SIG_DFL ; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static jmp_buf jmpbuf;static void sigsegv (int sig,siginfo_t *si,void *ctx_void)
tools/testing/selftests/x86/ldt_gdt.c +4 -14
@@ -26,6 +26,8 @@#include <asm/prctl.h> #include <sys/prctl.h> +#include "helpers.h" +#define AR_ACCESSED (1<<8)#define AR_TYPE_RODATA (0 * (1<<9))
@@ -506,20 +508,6 @@ static void fix_sa_restorer(int sig) }#endif -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); - -fix_sa_restorer (sig); -} -static jmp_buf jmpbuf;static void sigsegv (int sig,siginfo_t *info,void *ctx_void)
@@ -549,9 +537,11 @@ static void do_multicpu_tests(void) }sethandler (SIGSEGV , sigsegv,0 ); +fix_sa_restorer (SIGSEGV );#ifdef __i386__/* True 32-bit kernels send SIGILL instead of SIGSEGV on IRET faults. */ sethandler (SIGILL , sigsegv,0 ); +fix_sa_restorer (SIGILL );#endif printf ("[RUN]\tCross-CPU LDT invalidation\n" );
tools/testing/selftests/x86/mov_ss_trap.c +1 -13
@@ -36,7 +36,7 @@#include <setjmp.h> #include <sys/prctl.h> -#define X86_EFLAGS_RF (1UL << 16) +#include "helpers.h" #if __x86_64__# define REG_IP REG_RIP
@@ -94,18 +94,6 @@ static void enable_watchpoint(void) } } -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static char const *const signames[] = { [SIGSEGV ] ="SIGSEGV" , [SIGBUS ] ="SIBGUS" ,
tools/testing/selftests/x86/ptrace_syscall.c +2 -22
@@ -15,6 +15,8 @@#include <asm/ptrace-abi.h> #include <sys/auxv.h> +#include "helpers.h" +/* Bitness-agnostic defines for user_regs_struct fields. */ #ifdef __x86_64__# define user_syscall_nr orig_rax
@@ -93,18 +95,6 @@ static siginfo_t wait_trap(pid_t chld)return si; } -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static void setsigign (int sig,int flags) {struct sigaction sa;
@@ -116,16 +106,6 @@ static void setsigign(int sig, int flags)err (1 ,"sigaction" ); } -static void clearhandler (int sig) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_handler =SIG_DFL ; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -#ifdef __x86_64__# define REG_BP REG_RBP#else
tools/testing/selftests/x86/sigaltstack.c +2 -24
@@ -14,6 +14,8 @@#include <sys/resource.h> #include <setjmp.h> +#include "helpers.h" +/* sigaltstack()-enforced minimum stack */ #define ENFORCED_MINSIGSTKSZ 2048
@@ -27,30 +29,6 @@ static bool sigalrm_expected;static unsigned long at_minstack_size; -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; - -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} - -static void clearhandler (int sig) -{ -struct sigaction sa; - -memset (&sa,0 ,sizeof (sa)); - sa.sa_handler =SIG_DFL ; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static int setup_altstack (void *start,unsigned long size) {stack_t ss;
tools/testing/selftests/x86/sigreturn.c +2 -22
@@ -46,6 +46,8 @@#include <sys/ptrace.h> #include <sys/user.h> +#include "helpers.h" +/* Pull in AR_xyz defines. */ typedef unsigned int u32 ;typedef unsigned short u16 ;
@@ -138,28 +140,6 @@ static unsigned short LDT3(int idx)return (idx <<3 ) |7 ; } -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} - -static void clearhandler (int sig) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_handler =SIG_DFL ; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static void add_ldt (const struct user_desc *desc,unsigned short *var,const char *name) {
tools/testing/selftests/x86/single_step_syscall.c +0 -22
@@ -33,28 +33,6 @@#include "helpers.h" -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} - -static void clearhandler (int sig) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_handler =SIG_DFL ; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static volatile sig_atomic_t sig_traps, sig_eflags;sigjmp_buf jmpbuf;
tools/testing/selftests/x86/syscall_arg_fault.c +0 -12
@@ -17,18 +17,6 @@#include "helpers.h" -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static sigjmp_buf jmpbuf;static volatile sig_atomic_t n_errs;
tools/testing/selftests/x86/syscall_nt.c +0 -12
@@ -18,18 +18,6 @@static unsigned int nerrs; -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static void sigtrap (int sig,siginfo_t *si,void *ctx_void) { }
tools/testing/selftests/x86/sysret_rip.c +2 -22
@@ -22,6 +22,8 @@#include <sys/mman.h> #include <assert.h> +#include "helpers.h" +/* * These items are in clang_helpers_64.S, in order to avoid clang inline asm * limitations:
@@ -31,28 +33,6 @@ extern const char test_page[];static void const *current_test_page_addr = test_page; -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} - -static void clearhandler (int sig) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_handler =SIG_DFL ; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -/* State used by our signal handlers. */ static gregset_t initial_regs;
tools/testing/selftests/x86/test_vsyscall.c +0 -13
@@ -310,19 +310,6 @@ static void test_getcpu(int cpu)static jmp_buf jmpbuf;static volatile unsigned long segv_err; -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; - -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -ksft_exit_fail_msg ("sigaction failed\n" ); -} -static void sigsegv (int sig,siginfo_t *info,void *ctx_void) {ucontext_t *ctx = (ucontext_t *)ctx_void;
tools/testing/selftests/x86/unwind_vdso.c +0 -12
@@ -43,18 +43,6 @@ int main()#include <dlfcn.h> #include <unwind.h> -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static volatile sig_atomic_t nerrs;static unsigned long sysinfo;static bool got_sysinfo = false;
Signature · 1 line
2.45.2
[tip: x86/fpu] selftests/x86: Consolidate redundant signal helper functions
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: dbd6b649e7d5b66c7fa95a65d67b59cf5b45f0ac Gitweb: https://git.kernel.org/tip/dbd6b649e7d5b66c7fa95a65d67b59cf5b45f0ac Author: Chang S. Bae <[email protected]> AuthorDate: Tue, 25 Feb 2025 17:07:21 -08:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 13:05:28 +01:00
selftests/x86: Consolidate redundant signal helper functions
The x86 selftests frequently register and clean up signal handlers, but the sethandler() and clearhandler() functions have been redundantly copied across multiple .c files.
Move these functions to helpers.h to enable reuse across tests, eliminating around 250 lines of duplicate code.
Converge the error handling by using ksft_exit_fail_msg(), which is functionally equivalent with err() within the selftest framework.
This change is a prerequisite for the upcoming xstate selftest, which requires signal handling for registering and cleaning up handlers.
--- tools/testing/selftests/x86/amx.c | 25 +----------- tools/testing/selftests/x86/corrupt_xstate_header.c | 14 +------ tools/testing/selftests/x86/entry_from_vm86.c | 24 +---------- tools/testing/selftests/x86/fsgsbase.c | 24 +---------- tools/testing/selftests/x86/helpers.h | 28 ++++++++++++- tools/testing/selftests/x86/ioperm.c | 25 +----------- tools/testing/selftests/x86/iopl.c | 25 +----------- tools/testing/selftests/x86/ldt_gdt.c | 18 +------- tools/testing/selftests/x86/mov_ss_trap.c | 14 +------ tools/testing/selftests/x86/ptrace_syscall.c | 24 +---------- tools/testing/selftests/x86/sigaltstack.c | 26 +----------- tools/testing/selftests/x86/sigreturn.c | 24 +---------- tools/testing/selftests/x86/single_step_syscall.c | 22 +--------- tools/testing/selftests/x86/syscall_arg_fault.c | 12 +----- tools/testing/selftests/x86/syscall_nt.c | 12 +----- tools/testing/selftests/x86/sysret_rip.c | 24 +---------- tools/testing/selftests/x86/test_vsyscall.c | 13 +------ tools/testing/selftests/x86/unwind_vdso.c | 12 +----- 18 files changed, 51 insertions(+), 315 deletions(-)
tools/testing/selftests/x86/amx.c +1 -24
@@ -20,6 +20,7 @@#include <sys/uio.h> #include "../kselftest.h" /* For __cpuid_count() */ +#include "helpers.h" #ifndef __x86_64__# error This test is 64-bit only
@@ -61,30 +62,6 @@ static inline void xrstor(struct xsave_buffer *xbuf, uint64_t rfbm)/* err() exits and will not return */ #define fatal_error (msg, ...) err(1, "[FAIL]\t" msg, ##__VA_ARGS__) -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; - -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -fatal_error ("sigaction" ); -} - -static void clearhandler (int sig) -{ -struct sigaction sa; - -memset (&sa,0 ,sizeof (sa)); - sa.sa_handler =SIG_DFL ; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -fatal_error ("sigaction" ); -} -#define XFEATURE_XTILECFG 17#define XFEATURE_XTILEDATA 18#define XFEATURE_MASK_XTILECFG (1 << XFEATURE_XTILECFG)
tools/testing/selftests/x86/corrupt_xstate_header.c +1 -13
@@ -18,6 +18,7 @@#include <sys/wait.h> #include "../kselftest.h" /* For __cpuid_count() */ +#include "helpers.h" static inline int xsave_enabled (void ) {
@@ -29,19 +30,6 @@ static inline int xsave_enabled(void)return ecx & (1U <<27 ); } -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; - -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static void sigusr1 (int sig,siginfo_t *info,void *uc_void) {ucontext_t *uc = uc_void;
tools/testing/selftests/x86/entry_from_vm86.c +2 -22
@@ -24,31 +24,11 @@#include <errno.h> #include <sys/vm86.h> +#include "helpers.h" +static unsigned long load_addr =0x10000 ;static int nerrs =0 ; -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} - -static void clearhandler (int sig) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_handler =SIG_DFL ; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static sig_atomic_t got_signal;static void sighandler (int sig,siginfo_t *info,void *ctx_void)
tools/testing/selftests/x86/fsgsbase.c +2 -22
@@ -28,6 +28,8 @@#include <sys/wait.h> #include <setjmp.h> +#include "helpers.h" +#ifndef __x86_64__# error This test is 64-bit only#endif
@@ -39,28 +41,6 @@ static unsigned short *shared_scratch;static int nerrs; -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} - -static void clearhandler (int sig) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_handler =SIG_DFL ; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static void sigsegv (int sig,siginfo_t *si,void *ctx_void) {ucontext_t *ctx = (ucontext_t *)ctx_void;
tools/testing/selftests/x86/helpers.h +28 -0
@@ -2,8 +2,13 @@#ifndef __SELFTESTS_X86_HELPERS_H#define __SELFTESTS_X86_HELPERS_H +#include <signal.h> +#include <string.h> +#include <asm/processor-flags.h> +#include "../kselftest.h" +static inline unsigned long get_eflags (void ) {#ifdef __x86_64__
@@ -22,4 +27,27 @@ static inline void set_eflags(unsigned long eflags)#endif } +static inline void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *),int flags) +{ +struct sigaction sa; + +memset (&sa,0 ,sizeof (sa)); + sa.sa_sigaction = handler; + sa.sa_flags =SA_SIGINFO | flags; +sigemptyset (&sa.sa_mask); +if (sigaction (sig, &sa,0 )) +ksft_exit_fail_msg ("sigaction failed" ); +} + +static inline void clearhandler (int sig) +{ +struct sigaction sa; + +memset (&sa,0 ,sizeof (sa)); + sa.sa_handler =SIG_DFL ; +sigemptyset (&sa.sa_mask); +if (sigaction (sig, &sa,0 )) +ksft_exit_fail_msg ("sigaction failed" ); +} +#endif /* __SELFTESTS_X86_HELPERS_H */
tools/testing/selftests/x86/ioperm.c +2 -23
@@ -20,30 +20,9 @@#include <sched.h> #include <sys/io.h> -static int nerrs =0 ; - -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); - -} +#include "helpers.h" -static void clearhandler (int sig) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_handler =SIG_DFL ; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} +static int nerrs =0 ;static jmp_buf jmpbuf;
tools/testing/selftests/x86/iopl.c +2 -23
@@ -20,30 +20,9 @@#include <sched.h> #include <sys/io.h> -static int nerrs =0 ; - -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); - -} +#include "helpers.h" -static void clearhandler (int sig) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_handler =SIG_DFL ; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} +static int nerrs =0 ;static jmp_buf jmpbuf;
tools/testing/selftests/x86/ldt_gdt.c +4 -14
@@ -26,6 +26,8 @@#include <asm/prctl.h> #include <sys/prctl.h> +#include "helpers.h" +#define AR_ACCESSED (1<<8)#define AR_TYPE_RODATA (0 * (1<<9))
@@ -506,20 +508,6 @@ static void fix_sa_restorer(int sig) }#endif -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); - -fix_sa_restorer (sig); -} -static jmp_buf jmpbuf;static void sigsegv (int sig,siginfo_t *info,void *ctx_void)
@@ -549,9 +537,11 @@ static void do_multicpu_tests(void) }sethandler (SIGSEGV , sigsegv,0 ); +fix_sa_restorer (SIGSEGV );#ifdef __i386__/* True 32-bit kernels send SIGILL instead of SIGSEGV on IRET faults. */ sethandler (SIGILL , sigsegv,0 ); +fix_sa_restorer (SIGILL );#endif printf ("[RUN]\tCross-CPU LDT invalidation\n" );
tools/testing/selftests/x86/mov_ss_trap.c +1 -13
@@ -36,7 +36,7 @@#include <setjmp.h> #include <sys/prctl.h> -#define X86_EFLAGS_RF (1UL << 16) +#include "helpers.h" #if __x86_64__# define REG_IP REG_RIP
@@ -94,18 +94,6 @@ static void enable_watchpoint(void) } } -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static char const *const signames[] = { [SIGSEGV ] ="SIGSEGV" , [SIGBUS ] ="SIBGUS" ,
tools/testing/selftests/x86/ptrace_syscall.c +2 -22
@@ -15,6 +15,8 @@#include <asm/ptrace-abi.h> #include <sys/auxv.h> +#include "helpers.h" +/* Bitness-agnostic defines for user_regs_struct fields. */ #ifdef __x86_64__# define user_syscall_nr orig_rax
@@ -93,18 +95,6 @@ static siginfo_t wait_trap(pid_t chld)return si; } -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static void setsigign (int sig,int flags) {struct sigaction sa;
@@ -116,16 +106,6 @@ static void setsigign(int sig, int flags)err (1 ,"sigaction" ); } -static void clearhandler (int sig) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_handler =SIG_DFL ; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -#ifdef __x86_64__# define REG_BP REG_RBP#else
tools/testing/selftests/x86/sigaltstack.c +2 -24
@@ -14,6 +14,8 @@#include <sys/resource.h> #include <setjmp.h> +#include "helpers.h" +/* sigaltstack()-enforced minimum stack */ #define ENFORCED_MINSIGSTKSZ 2048
@@ -27,30 +29,6 @@ static bool sigalrm_expected;static unsigned long at_minstack_size; -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; - -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} - -static void clearhandler (int sig) -{ -struct sigaction sa; - -memset (&sa,0 ,sizeof (sa)); - sa.sa_handler =SIG_DFL ; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static int setup_altstack (void *start,unsigned long size) {stack_t ss;
tools/testing/selftests/x86/sigreturn.c +2 -22
@@ -46,6 +46,8 @@#include <sys/ptrace.h> #include <sys/user.h> +#include "helpers.h" +/* Pull in AR_xyz defines. */ typedef unsigned int u32 ;typedef unsigned short u16 ;
@@ -138,28 +140,6 @@ static unsigned short LDT3(int idx)return (idx <<3 ) |7 ; } -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} - -static void clearhandler (int sig) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_handler =SIG_DFL ; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static void add_ldt (const struct user_desc *desc,unsigned short *var,const char *name) {
tools/testing/selftests/x86/single_step_syscall.c +0 -22
@@ -33,28 +33,6 @@#include "helpers.h" -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} - -static void clearhandler (int sig) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_handler =SIG_DFL ; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static volatile sig_atomic_t sig_traps, sig_eflags;sigjmp_buf jmpbuf;
tools/testing/selftests/x86/syscall_arg_fault.c +0 -12
@@ -17,18 +17,6 @@#include "helpers.h" -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static sigjmp_buf jmpbuf;static volatile sig_atomic_t n_errs;
tools/testing/selftests/x86/syscall_nt.c +0 -12
@@ -18,18 +18,6 @@static unsigned int nerrs; -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static void sigtrap (int sig,siginfo_t *si,void *ctx_void) { }
tools/testing/selftests/x86/sysret_rip.c +2 -22
@@ -22,6 +22,8 @@#include <sys/mman.h> #include <assert.h> +#include "helpers.h" +/* * These items are in clang_helpers_64.S, in order to avoid clang inline asm * limitations:
@@ -31,28 +33,6 @@ extern const char test_page[];static void const *current_test_page_addr = test_page; -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} - -static void clearhandler (int sig) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_handler =SIG_DFL ; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -/* State used by our signal handlers. */ static gregset_t initial_regs;
tools/testing/selftests/x86/test_vsyscall.c +0 -13
@@ -310,19 +310,6 @@ static void test_getcpu(int cpu)static jmp_buf jmpbuf;static volatile unsigned long segv_err; -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; - -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -ksft_exit_fail_msg ("sigaction failed\n" ); -} -static void sigsegv (int sig,siginfo_t *info,void *ctx_void) {ucontext_t *ctx = (ucontext_t *)ctx_void;
tools/testing/selftests/x86/unwind_vdso.c +0 -12
@@ -43,18 +43,6 @@ int main()#include <dlfcn.h> #include <unwind.h> -static void sethandler (int sig,void (*handler)(int ,siginfo_t *,void *), -int flags) -{ -struct sigaction sa; -memset (&sa,0 ,sizeof (sa)); - sa.sa_sigaction = handler; - sa.sa_flags =SA_SIGINFO | flags; -sigemptyset (&sa.sa_mask); -if (sigaction (sig, &sa,0 )) -err (1 ,"sigaction" ); -} -static volatile sig_atomic_t nerrs;static unsigned long sysinfo;static bool got_sysinfo = false;
PATCH 2/9 selftests/x86/xstate: Refactor XSAVE helpers for general use
In reply to Chang S. BaeThe AMX test introduced several XSAVE-related helper functions, but so far, it has been the only user of them. These helpers can be generalized for broader test of multiple xstate features.
Move most XSAVE-related code into xsave.h, making it shareable. The restructuring includes:
* Establishing low-level XSAVE helpers for saving and restoring register states, as well as handling XSAVE buffers.
* Generalizing state data manipuldations: set_rand_data()
* Introducing a generic feature query helper: get_xstate_info()
While doing so, remove unused defines in amx.c.
--- tools/testing/selftests/x86/amx.c | 142 ++------------------------- tools/testing/selftests/x86/xstate.h | 132 +++++++++++++++++++++++++ 2 files changed, 142 insertions(+), 132 deletions(-) create mode 100644 tools/testing/selftests/x86/xstate.h
tools/testing/selftests/x86/amx.c +10 -132
@@ -19,46 +19,13 @@#include <sys/wait.h> #include <sys/uio.h> -#include "../kselftest.h" /* For __cpuid_count() */ #include "helpers.h" +#include "xstate.h" #ifndef __x86_64__# error This test is 64-bit only#endif -#define XSAVE_HDR_OFFSET 512 -#define XSAVE_HDR_SIZE 64 - -struct xsave_buffer { -union { -struct { -char legacy[XSAVE_HDR_OFFSET ]; -char header[XSAVE_HDR_SIZE ]; -char extended[0 ]; - }; -char bytes[0 ]; - }; -}; - -static inline void xsave (struct xsave_buffer *xbuf,uint64_t rfbm) -{ -uint32_t rfbm_lo = rfbm; -uint32_t rfbm_hi = rfbm >>32 ; - - asmvolatile ("xsave (%%rdi)" - : :"D" (xbuf),"a" (rfbm_lo),"d" (rfbm_hi) - :"memory" ); -} - -static inline void xrstor (struct xsave_buffer *xbuf,uint64_t rfbm) -{ -uint32_t rfbm_lo = rfbm; -uint32_t rfbm_hi = rfbm >>32 ; - - asmvolatile ("xrstor (%%rdi)" - : :"D" (xbuf),"a" (rfbm_lo),"d" (rfbm_hi)); -} -/* err() exits and will not return */ #define fatal_error (msg, ...) err(1, "[FAIL]\t" msg, ##__VA_ARGS__)
@@ -68,92 +35,12 @@ static inline void xrstor(struct xsave_buffer *xbuf, uint64_t rfbm)#define XFEATURE_MASK_XTILEDATA (1 << XFEATURE_XTILEDATA)#define XFEATURE_MASK_XTILE (XFEATURE_MASK_XTILECFG | XFEATURE_MASK_XTILEDATA) -#define CPUID_LEAF1_ECX_XSAVE_MASK (1 << 26) -#define CPUID_LEAF1_ECX_OSXSAVE_MASK (1 << 27) -static uint32_t xbuf_size; -static struct { -uint32_t xbuf_offset; -uint32_t size; -} xtiledata; - -#define CPUID_LEAF_XSTATE 0xd -#define CPUID_SUBLEAF_XSTATE_USER 0x0 -#define TILE_CPUID 0x1d -#define TILE_PALETTE_ID 0x1 - -static void check_cpuid_xtiledata (void ) -{ -uint32_t eax, ebx, ecx, edx; - -__cpuid_count (CPUID_LEAF_XSTATE ,CPUID_SUBLEAF_XSTATE_USER , - eax, ebx, ecx, edx); - -/* -* EBX enumerates the size (in bytes) required by the XSAVE -* instruction for an XSAVE area containing all the user state -* components corresponding to bits currently set in XCR0. -* -* Stash that off so it can be used to allocate buffers later. -*/ - xbuf_size = ebx; - -__cpuid_count (CPUID_LEAF_XSTATE ,XFEATURE_XTILEDATA , - eax, ebx, ecx, edx); -/* -* eax: XTILEDATA state component size -* ebx: XTILEDATA state component offset in user buffer -*/ -if (!eax || !ebx) -fatal_error ("xstate cpuid: invalid tile data size/offset: %d/%d" , - eax, ebx); - - xtiledata.size = eax; - xtiledata.xbuf_offset = ebx; -} +struct xstate_info xtiledata;/* The helpers for managing XSAVE buffer and tile states: */ -struct xsave_buffer *alloc_xbuf (void ) -{ -struct xsave_buffer *xbuf; - -/* XSAVE buffer should be 64B-aligned. */ - xbuf =aligned_alloc (64 , xbuf_size); -if (!xbuf) -fatal_error ("aligned_alloc()" ); -return xbuf; -} - -static inline void clear_xstate_header (struct xsave_buffer *buffer) -{ -memset (&buffer->header,0 ,sizeof (buffer->header)); -} - -static inline void set_xstatebv (struct xsave_buffer *buffer,uint64_t bv) -{ -/* XSTATE_BV is at the beginning of the header: */ - *(uint64_t *)(&buffer->header) = bv; -} - -static void set_rand_tiledata (struct xsave_buffer *xbuf) -{ -int *ptr = (int *)&xbuf->bytes[xtiledata.xbuf_offset]; -int data; -int i; - -/* -* Ensure that 'data' is never 0. This ensures that -* the registers are never in their initial configuration -* and thus never tracked as being in the init state. -*/ - data =rand () |1 ; - -for (i =0 ; i < xtiledata.size /sizeof (int ); i++, ptr++) - *ptr = data; -} -struct xsave_buffer *stashed_xsave;static void init_stashed_xsave (void )
@@ -169,21 +56,6 @@ static void free_stashed_xsave(void)free (stashed_xsave); } -/* See 'struct _fpx_sw_bytes' at sigcontext.h */ -#define SW_BYTES_OFFSET 464 -/* N.B. The struct's field name varies so read from the offset. */ -#define SW_BYTES_BV_OFFSET (SW_BYTES_OFFSET + 8) - -static inline struct _fpx_sw_bytes *get_fpx_sw_bytes (void *buffer) -{ -return (struct _fpx_sw_bytes *)(buffer +SW_BYTES_OFFSET ); -} - -static inline uint64_t get_fpx_sw_bytes_features (void *buffer) -{ -return *(uint64_t *)(buffer +SW_BYTES_BV_OFFSET ); -} -/* Work around printf() being unsafe in signals: */ #define SIGNAL_BUF_LEN 1000char signal_message_buffer[SIGNAL_BUF_LEN ];
@@ -281,7 +153,7 @@ static inline bool load_rand_tiledata(struct xsave_buffer *xbuf) {clear_xstate_header (xbuf);set_xstatebv (xbuf,XFEATURE_MASK_XTILEDATA ); -set_rand_tiledata (xbuf); +set_rand_data (&xtiledata, xbuf);return xrstor_safe (xbuf,XFEATURE_MASK_XTILEDATA ); }
@@ -884,7 +756,13 @@ int main(void)return KSFT_SKIP ; } -check_cpuid_xtiledata (); + xbuf_size =get_xbuf_size (); + + xtiledata =get_xstate_info (XFEATURE_XTILEDATA ); +if (!xtiledata.size || !xtiledata.xbuf_offset) { +fatal_error ("xstate cpuid: invalid tile data size/offset: %d/%d" , + xtiledata.size, xtiledata.xbuf_offset); + }init_stashed_xsave ();sethandler (SIGILL , handle_noperm,0 );
tools/testing/selftests/x86/xstate.h new file +132 -0
132 lines not on this page · raw message
Signature · 1 line
2.45.2
[tip: x86/fpu] selftests/x86/xstate: Refactor XSAVE helpers for general use
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 0f6d91a327db4a36d6febb74d833cdc668c8661b Gitweb: https://git.kernel.org/tip/0f6d91a327db4a36d6febb74d833cdc668c8661b Author: Chang S. Bae <[email protected]> AuthorDate: Tue, 25 Feb 2025 17:07:22 -08:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 13:05:28 +01:00
selftests/x86/xstate: Refactor XSAVE helpers for general use
The AMX test introduced several XSAVE-related helper functions, but so far, it has been the only user of them. These helpers can be generalized for broader test of multiple xstate features.
Move most XSAVE-related code into xsave.h, making it shareable. The restructuring includes:
* Establishing low-level XSAVE helpers for saving and restoring register states, as well as handling XSAVE buffers.
* Generalizing state data manipuldations: set_rand_data()
* Introducing a generic feature query helper: get_xstate_info()
While doing so, remove unused defines in amx.c.
--- tools/testing/selftests/x86/amx.c | 142 +------------------------- tools/testing/selftests/x86/xstate.h | 132 ++++++++++++++++++++++++- 2 files changed, 142 insertions(+), 132 deletions(-) create mode 100644 tools/testing/selftests/x86/xstate.h
tools/testing/selftests/x86/amx.c +10 -132
@@ -19,46 +19,13 @@#include <sys/wait.h> #include <sys/uio.h> -#include "../kselftest.h" /* For __cpuid_count() */ #include "helpers.h" +#include "xstate.h" #ifndef __x86_64__# error This test is 64-bit only#endif -#define XSAVE_HDR_OFFSET 512 -#define XSAVE_HDR_SIZE 64 - -struct xsave_buffer { -union { -struct { -char legacy[XSAVE_HDR_OFFSET ]; -char header[XSAVE_HDR_SIZE ]; -char extended[0 ]; - }; -char bytes[0 ]; - }; -}; - -static inline void xsave (struct xsave_buffer *xbuf,uint64_t rfbm) -{ -uint32_t rfbm_lo = rfbm; -uint32_t rfbm_hi = rfbm >>32 ; - - asmvolatile ("xsave (%%rdi)" - : :"D" (xbuf),"a" (rfbm_lo),"d" (rfbm_hi) - :"memory" ); -} - -static inline void xrstor (struct xsave_buffer *xbuf,uint64_t rfbm) -{ -uint32_t rfbm_lo = rfbm; -uint32_t rfbm_hi = rfbm >>32 ; - - asmvolatile ("xrstor (%%rdi)" - : :"D" (xbuf),"a" (rfbm_lo),"d" (rfbm_hi)); -} -/* err() exits and will not return */ #define fatal_error (msg, ...) err(1, "[FAIL]\t" msg, ##__VA_ARGS__)
@@ -68,92 +35,12 @@ static inline void xrstor(struct xsave_buffer *xbuf, uint64_t rfbm)#define XFEATURE_MASK_XTILEDATA (1 << XFEATURE_XTILEDATA)#define XFEATURE_MASK_XTILE (XFEATURE_MASK_XTILECFG | XFEATURE_MASK_XTILEDATA) -#define CPUID_LEAF1_ECX_XSAVE_MASK (1 << 26) -#define CPUID_LEAF1_ECX_OSXSAVE_MASK (1 << 27) -static uint32_t xbuf_size; -static struct { -uint32_t xbuf_offset; -uint32_t size; -} xtiledata; - -#define CPUID_LEAF_XSTATE 0xd -#define CPUID_SUBLEAF_XSTATE_USER 0x0 -#define TILE_CPUID 0x1d -#define TILE_PALETTE_ID 0x1 - -static void check_cpuid_xtiledata (void ) -{ -uint32_t eax, ebx, ecx, edx; - -__cpuid_count (CPUID_LEAF_XSTATE ,CPUID_SUBLEAF_XSTATE_USER , - eax, ebx, ecx, edx); - -/* -* EBX enumerates the size (in bytes) required by the XSAVE -* instruction for an XSAVE area containing all the user state -* components corresponding to bits currently set in XCR0. -* -* Stash that off so it can be used to allocate buffers later. -*/ - xbuf_size = ebx; - -__cpuid_count (CPUID_LEAF_XSTATE ,XFEATURE_XTILEDATA , - eax, ebx, ecx, edx); -/* -* eax: XTILEDATA state component size -* ebx: XTILEDATA state component offset in user buffer -*/ -if (!eax || !ebx) -fatal_error ("xstate cpuid: invalid tile data size/offset: %d/%d" , - eax, ebx); - - xtiledata.size = eax; - xtiledata.xbuf_offset = ebx; -} +struct xstate_info xtiledata;/* The helpers for managing XSAVE buffer and tile states: */ -struct xsave_buffer *alloc_xbuf (void ) -{ -struct xsave_buffer *xbuf; - -/* XSAVE buffer should be 64B-aligned. */ - xbuf =aligned_alloc (64 , xbuf_size); -if (!xbuf) -fatal_error ("aligned_alloc()" ); -return xbuf; -} - -static inline void clear_xstate_header (struct xsave_buffer *buffer) -{ -memset (&buffer->header,0 ,sizeof (buffer->header)); -} - -static inline void set_xstatebv (struct xsave_buffer *buffer,uint64_t bv) -{ -/* XSTATE_BV is at the beginning of the header: */ - *(uint64_t *)(&buffer->header) = bv; -} - -static void set_rand_tiledata (struct xsave_buffer *xbuf) -{ -int *ptr = (int *)&xbuf->bytes[xtiledata.xbuf_offset]; -int data; -int i; - -/* -* Ensure that 'data' is never 0. This ensures that -* the registers are never in their initial configuration -* and thus never tracked as being in the init state. -*/ - data =rand () |1 ; - -for (i =0 ; i < xtiledata.size /sizeof (int ); i++, ptr++) - *ptr = data; -} -struct xsave_buffer *stashed_xsave;static void init_stashed_xsave (void )
@@ -169,21 +56,6 @@ static void free_stashed_xsave(void)free (stashed_xsave); } -/* See 'struct _fpx_sw_bytes' at sigcontext.h */ -#define SW_BYTES_OFFSET 464 -/* N.B. The struct's field name varies so read from the offset. */ -#define SW_BYTES_BV_OFFSET (SW_BYTES_OFFSET + 8) - -static inline struct _fpx_sw_bytes *get_fpx_sw_bytes (void *buffer) -{ -return (struct _fpx_sw_bytes *)(buffer +SW_BYTES_OFFSET ); -} - -static inline uint64_t get_fpx_sw_bytes_features (void *buffer) -{ -return *(uint64_t *)(buffer +SW_BYTES_BV_OFFSET ); -} -/* Work around printf() being unsafe in signals: */ #define SIGNAL_BUF_LEN 1000char signal_message_buffer[SIGNAL_BUF_LEN ];
@@ -281,7 +153,7 @@ static inline bool load_rand_tiledata(struct xsave_buffer *xbuf) {clear_xstate_header (xbuf);set_xstatebv (xbuf,XFEATURE_MASK_XTILEDATA ); -set_rand_tiledata (xbuf); +set_rand_data (&xtiledata, xbuf);return xrstor_safe (xbuf,XFEATURE_MASK_XTILEDATA ); }
@@ -884,7 +756,13 @@ int main(void)return KSFT_SKIP ; } -check_cpuid_xtiledata (); + xbuf_size =get_xbuf_size (); + + xtiledata =get_xstate_info (XFEATURE_XTILEDATA ); +if (!xtiledata.size || !xtiledata.xbuf_offset) { +fatal_error ("xstate cpuid: invalid tile data size/offset: %d/%d" , + xtiledata.size, xtiledata.xbuf_offset); + }init_stashed_xsave ();sethandler (SIGILL , handle_noperm,0 );
tools/testing/selftests/x86/xstate.h new file +132 -0
132 lines not on this page · raw message
PATCH 3/9 selftests/x86/xstate: Enumerate and name xstate components
In reply to Chang S. BaeAfter moving essential helpers from amx.c, the code remains neutral regarding which xstate components it handles. However, explicitly listing known components helps users identify which features are ready for testing.
Enumerate xstate components to facilitate identification. Extend struct xstate_info to include a name field, providing a human-readable identifier.
--- tools/testing/selftests/x86/amx.c | 2 - tools/testing/selftests/x86/xstate.h | 60 ++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-)
tools/testing/selftests/x86/amx.c +0 -2
@@ -29,8 +29,6 @@/* err() exits and will not return */ #define fatal_error (msg, ...) err(1, "[FAIL]\t" msg, ##__VA_ARGS__) -#define XFEATURE_XTILECFG 17 -#define XFEATURE_XTILEDATA 18#define XFEATURE_MASK_XTILECFG (1 << XFEATURE_XTILECFG)#define XFEATURE_MASK_XTILEDATA (1 << XFEATURE_XTILEDATA)#define XFEATURE_MASK_XTILE (XFEATURE_MASK_XTILECFG | XFEATURE_MASK_XTILEDATA)
tools/testing/selftests/x86/xstate.h +60 -0
78 lines not on this page · raw message
Signature · 1 line
2.45.2
[tip: x86/fpu] selftests/x86/xstate: Enumerate and name xstate components
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 3fcb4d61465613aee76ebd5d86b488778657dda7 Gitweb: https://git.kernel.org/tip/3fcb4d61465613aee76ebd5d86b488778657dda7 Author: Chang S. Bae <[email protected]> AuthorDate: Tue, 25 Feb 2025 17:07:23 -08:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 13:05:28 +01:00
selftests/x86/xstate: Enumerate and name xstate components
After moving essential helpers from amx.c, the code remains neutral regarding which xstate components it handles. However, explicitly listing known components helps users identify which features are ready for testing.
Enumerate xstate components to facilitate identification. Extend struct xstate_info to include a name field, providing a human-readable identifier.
--- tools/testing/selftests/x86/amx.c | 2 +- tools/testing/selftests/x86/xstate.h | 60 +++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-)
tools/testing/selftests/x86/amx.c +0 -2
@@ -29,8 +29,6 @@/* err() exits and will not return */ #define fatal_error (msg, ...) err(1, "[FAIL]\t" msg, ##__VA_ARGS__) -#define XFEATURE_XTILECFG 17 -#define XFEATURE_XTILEDATA 18#define XFEATURE_MASK_XTILECFG (1 << XFEATURE_XTILECFG)#define XFEATURE_MASK_XTILEDATA (1 << XFEATURE_XTILEDATA)#define XFEATURE_MASK_XTILE (XFEATURE_MASK_XTILECFG | XFEATURE_MASK_XTILEDATA)
tools/testing/selftests/x86/xstate.h +60 -0
78 lines not on this page · raw message
PATCH 4/9 selftests/x86/xstate: Refactor context switching test
In reply to Chang S. BaeThe existing context switching and ptrace tests in amx.c are not specific to dynamic states, making them reusable for general xstate testing.
As a first step, move the context switching test to xstate.c. Refactor the test code to allow specifying which xstate component being tested.
To decouple the test from dynamic states, remove the permission request code. In fact, The permission request inside the test wrapper was redundant.
Additionally, replace fatal_error() with ksft_exit_fail_msg() for consistency in error handling.
---
Expected output:
$ amx_64 ... [RUN] AMX Tile data: check context switches, 10 iterations, 5 threads. [OK] No incorrect case was found. --- tools/testing/selftests/x86/Makefile | 2 + tools/testing/selftests/x86/amx.c | 166 +--------------------- tools/testing/selftests/x86/xstate.c | 198 +++++++++++++++++++++++++++ tools/testing/selftests/x86/xstate.h | 2 + 4 files changed, 204 insertions(+), 164 deletions(-) create mode 100644 tools/testing/selftests/x86/xstate.c
tools/testing/selftests/x86/Makefile +2 -0
@@ -132,3 +132,5 @@ $(OUTPUT)/check_initial_reg_state_64: CFLAGS += -Wl,-ereal_start -static $(OUTPUT)/nx_stack_32: CFLAGS += -Wl,-z,noexecstack $(OUTPUT)/nx_stack_64: CFLAGS += -Wl,-z,noexecstack + +$(OUTPUT)/amx_64: EXTRA_FILES += xstate.c
tools/testing/selftests/x86/amx.c +2 -164
196 lines not on this page · raw message
tools/testing/selftests/x86/xstate.c new file +198 -0
198 lines not on this page · raw message
tools/testing/selftests/x86/xstate.h +2 -0
@@ -189,4 +189,6 @@ static inline void set_rand_data(struct xstate_info *xstate, struct xsave_buffer *ptr = data; } +void test_context_switch (uint32_t feature_num,uint32_t num_threads,uint32_t iterations); +#endif /* __SELFTESTS_X86_XSTATE_H */
Signature · 1 line
2.45.2
[tip: x86/fpu] selftests/x86/xstate: Refactor context switching test
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 40f6852ef2bffcd6fb3634cf71e6fbc2d1d6b768 Gitweb: https://git.kernel.org/tip/40f6852ef2bffcd6fb3634cf71e6fbc2d1d6b768 Author: Chang S. Bae <[email protected]> AuthorDate: Tue, 25 Feb 2025 17:07:24 -08:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 13:05:29 +01:00
selftests/x86/xstate: Refactor context switching test
The existing context switching and ptrace tests in amx.c are not specific to dynamic states, making them reusable for general xstate testing.
As a first step, move the context switching test to xstate.c. Refactor the test code to allow specifying which xstate component being tested.
To decouple the test from dynamic states, remove the permission request code. In fact, The permission request inside the test wrapper was redundant.
Additionally, replace fatal_error() with ksft_exit_fail_msg() for consistency in error handling.
Expected output:
$ amx_64 ... [RUN] AMX Tile data: check context switches, 10 iterations, 5 threads. [OK] No incorrect case was found.
--- tools/testing/selftests/x86/Makefile | 2 +- tools/testing/selftests/x86/amx.c | 166 +---------------------- tools/testing/selftests/x86/xstate.c | 198 ++++++++++++++++++++++++++- tools/testing/selftests/x86/xstate.h | 2 +- 4 files changed, 204 insertions(+), 164 deletions(-) create mode 100644 tools/testing/selftests/x86/xstate.c
tools/testing/selftests/x86/Makefile +2 -0
@@ -132,3 +132,5 @@ $(OUTPUT)/check_initial_reg_state_64: CFLAGS += -Wl,-ereal_start -static $(OUTPUT)/nx_stack_32: CFLAGS += -Wl,-z,noexecstack $(OUTPUT)/nx_stack_64: CFLAGS += -Wl,-z,noexecstack + +$(OUTPUT)/amx_64: EXTRA_FILES += xstate.c
tools/testing/selftests/x86/amx.c +2 -164
196 lines not on this page · raw message
tools/testing/selftests/x86/xstate.c new file +198 -0
198 lines not on this page · raw message
tools/testing/selftests/x86/xstate.h +2 -0
@@ -189,4 +189,6 @@ static inline void set_rand_data(struct xstate_info *xstate, struct xsave_buffer *ptr = data; } +void test_context_switch (uint32_t feature_num,uint32_t num_threads,uint32_t iterations); +#endif /* __SELFTESTS_X86_XSTATE_H */
PATCH 5/9 selftests/x86/xstate: Refactor ptrace ABI test
In reply to Chang S. BaeFollowing the refactor of the context switching test, the ptrace test is another component reusable for other xstate features. As part of this restructuring, add a missing check to validate the user_xstateregs->xstate_fx_sw field in the ABI.
Also, replace err() and fatal_error() with ksft_exit_fail_msg() for consistency in error handling.
---
Expected out:
$ amx_64 ... [RUN] AMX Tile data: inject xstate via ptrace(). [OK] 'xfeatures' in SW reserved area was correctly written [OK] xstate was correctly updated. --- tools/testing/selftests/x86/amx.c | 108 +--------------------- tools/testing/selftests/x86/xstate.c | 129 +++++++++++++++++++++++++++ tools/testing/selftests/x86/xstate.h | 1 + 3 files changed, 131 insertions(+), 107 deletions(-)
tools/testing/selftests/x86/amx.c +1 -107
@@ -13,10 +13,8 @@#include <sys/auxv.h> #include <sys/mman.h> #include <sys/shm.h> -#include <sys/ptrace.h> #include <sys/syscall.h> #include <sys/wait.h> -#include <sys/uio.h> #include "helpers.h" #include "xstate.h"
@@ -32,8 +30,6 @@#define XFEATURE_MASK_XTILEDATA (1 << XFEATURE_XTILEDATA)#define XFEATURE_MASK_XTILE (XFEATURE_MASK_XTILECFG | XFEATURE_MASK_XTILEDATA) -static uint32_t xbuf_size; -struct xstate_info xtiledata;/* The helpers for managing XSAVE buffer and tile states: */
@@ -154,13 +150,6 @@ static inline bool load_rand_tiledata(struct xsave_buffer *xbuf)return xrstor_safe (xbuf,XFEATURE_MASK_XTILEDATA ); } -/* Return XTILEDATA to its initial configuration. */ -static inline void init_xtiledata (void ) -{ -clear_xstate_header (stashed_xsave); -xrstor_safe (stashed_xsave,XFEATURE_MASK_XTILEDATA ); -} -enum expected_result {FAIL_EXPECTED ,SUCCESS_EXPECTED };/* arch_prctl() and sigaltstack() test */
@@ -489,99 +478,6 @@ static void test_fork(void)_exit (0 ); } -/* Ptrace test */ - -/* -* Make sure the ptracee has the expanded kernel buffer on the first -* use. Then, initialize the state before performing the state -* injection from the ptracer. -*/ -static inline void ptracee_firstuse_tiledata (void ) -{ -load_rand_tiledata (stashed_xsave); -init_xtiledata (); -} - -/* -* Ptracer injects the randomized tile data state. It also reads -* before and after that, which will execute the kernel's state copy -* functions. So, the tester is advised to double-check any emitted -* kernel messages. -*/ -static void ptracer_inject_tiledata (pid_t target) -{ -struct xsave_buffer *xbuf; -struct iovec iov; - - xbuf =alloc_xbuf (); -if (!xbuf) -fatal_error ("unable to allocate XSAVE buffer" ); - -printf ("\tRead the init'ed tiledata via ptrace().\n" ); - - iov.iov_base = xbuf; - iov.iov_len = xbuf_size; - -memset (stashed_xsave,0 , xbuf_size); - -if (ptrace (PTRACE_GETREGSET , target, (uint32_t )NT_X86_XSTATE , &iov)) -fatal_error ("PTRACE_GETREGSET" ); - -if (!__compare_tiledata_state (stashed_xsave, xbuf)) -printf ("[OK]\tThe init'ed tiledata was read from ptracee.\n" ); -else -printf ("[FAIL]\tThe init'ed tiledata was not read from ptracee.\n" ); - -printf ("\tInject tiledata via ptrace().\n" ); - -load_rand_tiledata (xbuf); - -memcpy (&stashed_xsave->bytes[xtiledata.xbuf_offset], - &xbuf->bytes[xtiledata.xbuf_offset], - xtiledata.size); - -if (ptrace (PTRACE_SETREGSET , target, (uint32_t )NT_X86_XSTATE , &iov)) -fatal_error ("PTRACE_SETREGSET" ); - -if (ptrace (PTRACE_GETREGSET , target, (uint32_t )NT_X86_XSTATE , &iov)) -fatal_error ("PTRACE_GETREGSET" ); - -if (!__compare_tiledata_state (stashed_xsave, xbuf)) -printf ("[OK]\tTiledata was correctly written to ptracee.\n" ); -else -printf ("[FAIL]\tTiledata was not correctly written to ptracee.\n" ); -} - -static void test_ptrace (void ) -{ -pid_t child; -int status; - - child =fork (); -if (child <0 ) { -err (1 ,"fork" ); - }else if (!child) { -if (ptrace (PTRACE_TRACEME ,0 ,NULL ,NULL )) -err (1 ,"PTRACE_TRACEME" ); - -ptracee_firstuse_tiledata (); - -raise (SIGTRAP ); -_exit (0 ); - } - -do { -wait (&status); - }while (WSTOPSIG (status) !=SIGTRAP ); - -ptracer_inject_tiledata (child); - -ptrace (PTRACE_DETACH , child,NULL ,NULL ); -wait (&status); -if (!WIFEXITED (status) ||WEXITSTATUS (status)) -err (1 ,"ptrace test" ); -} -int main (void ) {const unsigned int ctxtsw_num_threads =5 , ctxtsw_iterations =10 ;
@@ -594,8 +490,6 @@ int main(void)return KSFT_SKIP ; } - xbuf_size =get_xbuf_size (); - xtiledata =get_xstate_info (XFEATURE_XTILEDATA );if (!xtiledata.size || !xtiledata.xbuf_offset) { fatal_error("xstate cpuid: invalid tile data size/offset: %d/%d",
@@ -614,7 +508,7 @@ int main(void)test_context_switch (XFEATURE_XTILEDATA , ctxtsw_num_threads, ctxtsw_iterations); -test_ptrace (); +test_ptrace (XFEATURE_XTILEDATA );clearhandler (SIGILL );free_stashed_xsave ();
tools/testing/selftests/x86/xstate.c +129 -0
150 lines not on this page · raw message
tools/testing/selftests/x86/xstate.h +1 -0
@@ -190,5 +190,6 @@ static inline void set_rand_data(struct xstate_info *xstate, struct xsave_buffer }void test_context_switch (uint32_t feature_num,uint32_t num_threads,uint32_t iterations); +void test_ptrace (uint32_t feature_num);#endif /* __SELFTESTS_X86_XSTATE_H */
Signature · 1 line
2.45.2
[tip: x86/fpu] selftests/x86/xstate: Refactor ptrace ABI test
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 7cb2fbe41949caa35ef1805323b8fc011fac2537 Gitweb: https://git.kernel.org/tip/7cb2fbe41949caa35ef1805323b8fc011fac2537 Author: Chang S. Bae <[email protected]> AuthorDate: Tue, 25 Feb 2025 17:07:25 -08:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 13:05:29 +01:00
selftests/x86/xstate: Refactor ptrace ABI test
Following the refactoring of the context switching test, the ptrace test is another component reusable for other xstate features. As part of this restructuring, add a missing check to validate the user_xstateregs->xstate_fx_sw field in the ABI.
Also, replace err() and fatal_error() with ksft_exit_fail_msg() for consistency in error handling.
Expected output:
$ amx_64 ... [RUN] AMX Tile data: inject xstate via ptrace(). [OK] 'xfeatures' in SW reserved area was correctly written [OK] xstate was correctly updated.
--- tools/testing/selftests/x86/amx.c | 108 +---------------------- tools/testing/selftests/x86/xstate.c | 129 ++++++++++++++++++++++++++- tools/testing/selftests/x86/xstate.h | 1 +- 3 files changed, 131 insertions(+), 107 deletions(-)
tools/testing/selftests/x86/amx.c +1 -107
@@ -13,10 +13,8 @@#include <sys/auxv.h> #include <sys/mman.h> #include <sys/shm.h> -#include <sys/ptrace.h> #include <sys/syscall.h> #include <sys/wait.h> -#include <sys/uio.h> #include "helpers.h" #include "xstate.h"
@@ -32,8 +30,6 @@#define XFEATURE_MASK_XTILEDATA (1 << XFEATURE_XTILEDATA)#define XFEATURE_MASK_XTILE (XFEATURE_MASK_XTILECFG | XFEATURE_MASK_XTILEDATA) -static uint32_t xbuf_size; -struct xstate_info xtiledata;/* The helpers for managing XSAVE buffer and tile states: */
@@ -154,13 +150,6 @@ static inline bool load_rand_tiledata(struct xsave_buffer *xbuf)return xrstor_safe (xbuf,XFEATURE_MASK_XTILEDATA ); } -/* Return XTILEDATA to its initial configuration. */ -static inline void init_xtiledata (void ) -{ -clear_xstate_header (stashed_xsave); -xrstor_safe (stashed_xsave,XFEATURE_MASK_XTILEDATA ); -} -enum expected_result {FAIL_EXPECTED ,SUCCESS_EXPECTED };/* arch_prctl() and sigaltstack() test */
@@ -489,99 +478,6 @@ static void test_fork(void)_exit (0 ); } -/* Ptrace test */ - -/* -* Make sure the ptracee has the expanded kernel buffer on the first -* use. Then, initialize the state before performing the state -* injection from the ptracer. -*/ -static inline void ptracee_firstuse_tiledata (void ) -{ -load_rand_tiledata (stashed_xsave); -init_xtiledata (); -} - -/* -* Ptracer injects the randomized tile data state. It also reads -* before and after that, which will execute the kernel's state copy -* functions. So, the tester is advised to double-check any emitted -* kernel messages. -*/ -static void ptracer_inject_tiledata (pid_t target) -{ -struct xsave_buffer *xbuf; -struct iovec iov; - - xbuf =alloc_xbuf (); -if (!xbuf) -fatal_error ("unable to allocate XSAVE buffer" ); - -printf ("\tRead the init'ed tiledata via ptrace().\n" ); - - iov.iov_base = xbuf; - iov.iov_len = xbuf_size; - -memset (stashed_xsave,0 , xbuf_size); - -if (ptrace (PTRACE_GETREGSET , target, (uint32_t )NT_X86_XSTATE , &iov)) -fatal_error ("PTRACE_GETREGSET" ); - -if (!__compare_tiledata_state (stashed_xsave, xbuf)) -printf ("[OK]\tThe init'ed tiledata was read from ptracee.\n" ); -else -printf ("[FAIL]\tThe init'ed tiledata was not read from ptracee.\n" ); - -printf ("\tInject tiledata via ptrace().\n" ); - -load_rand_tiledata (xbuf); - -memcpy (&stashed_xsave->bytes[xtiledata.xbuf_offset], - &xbuf->bytes[xtiledata.xbuf_offset], - xtiledata.size); - -if (ptrace (PTRACE_SETREGSET , target, (uint32_t )NT_X86_XSTATE , &iov)) -fatal_error ("PTRACE_SETREGSET" ); - -if (ptrace (PTRACE_GETREGSET , target, (uint32_t )NT_X86_XSTATE , &iov)) -fatal_error ("PTRACE_GETREGSET" ); - -if (!__compare_tiledata_state (stashed_xsave, xbuf)) -printf ("[OK]\tTiledata was correctly written to ptracee.\n" ); -else -printf ("[FAIL]\tTiledata was not correctly written to ptracee.\n" ); -} - -static void test_ptrace (void ) -{ -pid_t child; -int status; - - child =fork (); -if (child <0 ) { -err (1 ,"fork" ); - }else if (!child) { -if (ptrace (PTRACE_TRACEME ,0 ,NULL ,NULL )) -err (1 ,"PTRACE_TRACEME" ); - -ptracee_firstuse_tiledata (); - -raise (SIGTRAP ); -_exit (0 ); - } - -do { -wait (&status); - }while (WSTOPSIG (status) !=SIGTRAP ); - -ptracer_inject_tiledata (child); - -ptrace (PTRACE_DETACH , child,NULL ,NULL ); -wait (&status); -if (!WIFEXITED (status) ||WEXITSTATUS (status)) -err (1 ,"ptrace test" ); -} -int main (void ) {const unsigned int ctxtsw_num_threads =5 , ctxtsw_iterations =10 ;
@@ -594,8 +490,6 @@ int main(void)return KSFT_SKIP ; } - xbuf_size =get_xbuf_size (); - xtiledata =get_xstate_info (XFEATURE_XTILEDATA );if (!xtiledata.size || !xtiledata.xbuf_offset) { fatal_error("xstate cpuid: invalid tile data size/offset: %d/%d",
@@ -614,7 +508,7 @@ int main(void)test_context_switch (XFEATURE_XTILEDATA , ctxtsw_num_threads, ctxtsw_iterations); -test_ptrace (); +test_ptrace (XFEATURE_XTILEDATA );clearhandler (SIGILL );free_stashed_xsave ();
tools/testing/selftests/x86/xstate.c +129 -0
150 lines not on this page · raw message
tools/testing/selftests/x86/xstate.h +1 -0
@@ -190,5 +190,6 @@ static inline void set_rand_data(struct xstate_info *xstate, struct xsave_buffer }void test_context_switch (uint32_t feature_num,uint32_t num_threads,uint32_t iterations); +void test_ptrace (uint32_t feature_num);#endif /* __SELFTESTS_X86_XSTATE_H */
PATCH 6/9 selftests/x86/xstate: Introduce signal ABI test
In reply to Chang S. BaeWith the refactored test cases, another xstate exposure to userspace is through signal delivery. While amx.c includes signal-related scenarios, its primary focus is on xstate permission management, which is largely specific to dynamic states.
The remaining gap is testing xstate preservation and restoration across signal delivery. The kernel defines an ABI for presenting xstate in the signal frame, closely resembling the hardware XSAVE format, where xstate modification is also possible.
Introduce a new test case to verify xstate preservation across signal delivery and return, that is ensuring ABI compatibility by:
- Loading xstate before raising a signal.
- Verifying correct exposure in the signal frame
- Modifying xstate in the signal frame before returning.
- Checking the state restoration upon signal return.
Integrate this test into the AMX test suite as an initial usage site.
---
Expected output:
$ amx_64 ... [RUN] AMX Tile data: load xstate and raise SIGUSR1 [OK] 'magic1' is valid [OK] 'xfeatures' in SW reserved area is valid [OK] 'xfeatures' in XSAVE header is valid [OK] xstate delivery was successful [OK] 'magic2' is valid [RUN] AMX Tile data: load new xstate from sighandler and check it after sigreturn [OK] xstate was restored correctly --- tools/testing/selftests/x86/amx.c | 2 + tools/testing/selftests/x86/xstate.c | 108 +++++++++++++++++++++++++++ tools/testing/selftests/x86/xstate.h | 1 + 3 files changed, 111 insertions(+)
tools/testing/selftests/x86/amx.c +2 -0
@@ -510,6 +510,8 @@ int main(void)test_ptrace (XFEATURE_XTILEDATA ); +test_signal (XFEATURE_XTILEDATA ); +clearhandler (SIGILL );free_stashed_xsave ();
tools/testing/selftests/x86/xstate.c +108 -0
117 lines not on this page · raw message
tools/testing/selftests/x86/xstate.h +1 -0
@@ -191,5 +191,6 @@ static inline void set_rand_data(struct xstate_info *xstate, struct xsave_buffervoid test_context_switch (uint32_t feature_num,uint32_t num_threads,uint32_t iterations);void test_ptrace (uint32_t feature_num); +void test_signal (uint32_t feature_num);#endif /* __SELFTESTS_X86_XSTATE_H */
Signature · 1 line
2.45.2
[tip: x86/fpu] selftests/x86/xstate: Introduce signal ABI test
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: e075d9fa16b3681b8a375cb47f4b5a1901e93530 Gitweb: https://git.kernel.org/tip/e075d9fa16b3681b8a375cb47f4b5a1901e93530 Author: Chang S. Bae <[email protected]> AuthorDate: Tue, 25 Feb 2025 17:07:26 -08:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 13:05:29 +01:00
selftests/x86/xstate: Introduce signal ABI test
With the refactored test cases, another xstate exposure to userspace is through signal delivery. While amx.c includes signal-related scenarios, its primary focus is on xstate permission management, which is largely specific to dynamic states.
The remaining gap is testing xstate preservation and restoration across signal delivery. The kernel defines an ABI for presenting xstate in the signal frame, closely resembling the hardware XSAVE format, where xstate modification is also possible.
Introduce a new test case to verify xstate preservation across signal delivery and return, that is ensuring ABI compatibility by:
- Loading xstate before raising a signal.
- Verifying correct exposure in the signal frame
- Modifying xstate in the signal frame before returning.
- Checking the state restoration upon signal return.
Integrate this test into the AMX test suite as an initial usage site.
Expected output:
$ amx_64 ... [RUN] AMX Tile data: load xstate and raise SIGUSR1 [OK] 'magic1' is valid [OK] 'xfeatures' in SW reserved area is valid [OK] 'xfeatures' in XSAVE header is valid [OK] xstate delivery was successful [OK] 'magic2' is valid [RUN] AMX Tile data: load new xstate from sighandler and check it after sigreturn [OK] xstate was restored correctly
--- tools/testing/selftests/x86/amx.c | 2 +- tools/testing/selftests/x86/xstate.c | 108 ++++++++++++++++++++++++++- tools/testing/selftests/x86/xstate.h | 1 +- 3 files changed, 111 insertions(+)
tools/testing/selftests/x86/amx.c +2 -0
@@ -510,6 +510,8 @@ int main(void)test_ptrace (XFEATURE_XTILEDATA ); +test_signal (XFEATURE_XTILEDATA ); +clearhandler (SIGILL );free_stashed_xsave ();
tools/testing/selftests/x86/xstate.c +108 -0
117 lines not on this page · raw message
tools/testing/selftests/x86/xstate.h +1 -0
@@ -191,5 +191,6 @@ static inline void set_rand_data(struct xstate_info *xstate, struct xsave_buffervoid test_context_switch (uint32_t feature_num,uint32_t num_threads,uint32_t iterations);void test_ptrace (uint32_t feature_num); +void test_signal (uint32_t feature_num);#endif /* __SELFTESTS_X86_XSTATE_H */
PATCH 7/9 selftests/x86/xstate: Consolidate test invocations into a single entry
In reply to Chang S. BaeCurrently, each of the three xstate tests runs as a separate invocation, requiring the xstate number to be passed and state information to be reconstructed repeatedly. This approach arose from their individual and isolated development, but now it makes sense to unify them.
Introduce a wrapper function that first verifies feature availability from the kernel and constructs the necessary state information once. The wrapper then sequentially invokes all tests to ensure consistent execution.
Update the AMX test to use this unified invocation.
--- tools/testing/selftests/x86/amx.c | 11 ++++---- tools/testing/selftests/x86/xstate.c | 38 ++++++++++++++++++++-------- tools/testing/selftests/x86/xstate.h | 5 ++-- 3 files changed, 35 insertions(+), 19 deletions(-)
tools/testing/selftests/x86/amx.c +5 -6
@@ -480,7 +480,6 @@ static void test_fork(void)int main (void ) { -const unsigned int ctxtsw_num_threads =5 , ctxtsw_iterations =10 ;unsigned long features;long rc;
@@ -506,11 +505,11 @@ int main(void)test_fork (); -test_context_switch (XFEATURE_XTILEDATA , ctxtsw_num_threads, ctxtsw_iterations); - -test_ptrace (XFEATURE_XTILEDATA ); - -test_signal (XFEATURE_XTILEDATA ); +/* +* Perform generic xstate tests for context switching, ptrace, +* and signal. +*/ +test_xstate (XFEATURE_XTILEDATA );clearhandler (SIGILL );free_stashed_xsave ();
tools/testing/selftests/x86/xstate.c +28 -10
@@ -6,7 +6,9 @@#include <pthread.h> #include <stdbool.h> +#include <asm/prctl.h> #include <sys/ptrace.h> +#include <sys/syscall.h> #include <sys/uio.h> #include <sys/wait.h>
@@ -189,15 +191,13 @@ static void affinitize_cpu0(void)ksft_exit_fail_msg ("sched_setaffinity to CPU 0 failed\n" ); } -void test_context_switch (uint32_t feature_num,uint32_t num_threads,uint32_t iterations) +static void test_context_switch (uint32_t num_threads,uint32_t iterations) {struct futex_info *finfo;/* Affinitize to one CPU to force context switches */ affinitize_cpu0 (); - xstate =get_xstate_info (feature_num); -printf ("[RUN]\t%s: check context switches, %d iterations, %d threads.\n" , xstate.name, iterations, num_threads);
@@ -299,13 +299,11 @@ static void ptracer_inject_xstate(pid_t target)free (xbuf2); } -void test_ptrace (uint32_t feature_num) +static void test_ptrace (void ) {pid_t child;int status; - xstate =get_xstate_info (feature_num); - child =fork ();if (child <0 ) {ksft_exit_fail_msg ("fork() failed\n" );
@@ -392,17 +390,14 @@ static void validate_sigfpstate(int sig, siginfo_t *si, void *ctx_void)copy_xstate (stashed_xbuf, xbuf); } -void test_signal (uint32_t feature_num) +static void test_signal (void ) {bool valid_xstate; - xstate =get_xstate_info (feature_num); -/* * The signal handler will access this to verify xstate context * preservation. */ - stashed_xbuf =alloc_xbuf ();if (!stashed_xbuf)ksft_exit_fail_msg ("unable to allocate XSAVE buffer\n" );
@@ -433,3 +428,26 @@ void test_signal(uint32_t feature_num)clearhandler (SIGUSR1 );free (stashed_xbuf); } + +void test_xstate (uint32_t feature_num) +{ +const unsigned int ctxtsw_num_threads =5 , ctxtsw_iterations =10 ; +unsigned long features; +long rc; + + rc =syscall (SYS_arch_prctl,ARCH_GET_XCOMP_SUPP , &features); +if (rc || !(features & (1 << feature_num))) { +ksft_print_msg ("The kernel does not support feature number: %u\n" , feature_num); +return ; + } + + xstate =get_xstate_info (feature_num); +if (!xstate.size || !xstate.xbuf_offset) { +ksft_exit_fail_msg ("invalid state size/offset (%d/%d)\n" , + xstate.size, xstate.xbuf_offset); + } + +test_context_switch (ctxtsw_num_threads, ctxtsw_iterations); +test_ptrace (); +test_signal (); +}
tools/testing/selftests/x86/xstate.h +2 -3
@@ -189,8 +189,7 @@ static inline void set_rand_data(struct xstate_info *xstate, struct xsave_buffer *ptr = data; } -void test_context_switch (uint32_t feature_num,uint32_t num_threads,uint32_t iterations); -void test_ptrace (uint32_t feature_num); -void test_signal (uint32_t feature_num); +/* Testing kernel's context switching and ABI support for the xstate. */ +void test_xstate (uint32_t feature_num);#endif /* __SELFTESTS_X86_XSTATE_H */
Signature · 1 line
2.45.2
[tip: x86/fpu] selftests/x86/xstate: Consolidate test invocations into a single entry
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 10d8a204c5009fb8a6cb2790d17b5611b795c349 Gitweb: https://git.kernel.org/tip/10d8a204c5009fb8a6cb2790d17b5611b795c349 Author: Chang S. Bae <[email protected]> AuthorDate: Tue, 25 Feb 2025 17:07:27 -08:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 13:05:29 +01:00
selftests/x86/xstate: Consolidate test invocations into a single entry
Currently, each of the three xstate tests runs as a separate invocation, requiring the xstate number to be passed and state information to be reconstructed repeatedly. This approach arose from their individual and isolated development, but now it makes sense to unify them.
Introduce a wrapper function that first verifies feature availability from the kernel and constructs the necessary state information once. The wrapper then sequentially invokes all tests to ensure consistent execution.
Update the AMX test to use this unified invocation.
--- tools/testing/selftests/x86/amx.c | 11 +++----- tools/testing/selftests/x86/xstate.c | 38 +++++++++++++++++++-------- tools/testing/selftests/x86/xstate.h | 5 +--- 3 files changed, 35 insertions(+), 19 deletions(-)
tools/testing/selftests/x86/amx.c +5 -6
@@ -480,7 +480,6 @@ static void test_fork(void)int main (void ) { -const unsigned int ctxtsw_num_threads =5 , ctxtsw_iterations =10 ;unsigned long features;long rc;
@@ -506,11 +505,11 @@ int main(void)test_fork (); -test_context_switch (XFEATURE_XTILEDATA , ctxtsw_num_threads, ctxtsw_iterations); - -test_ptrace (XFEATURE_XTILEDATA ); - -test_signal (XFEATURE_XTILEDATA ); +/* +* Perform generic xstate tests for context switching, ptrace, +* and signal. +*/ +test_xstate (XFEATURE_XTILEDATA );clearhandler (SIGILL );free_stashed_xsave ();
tools/testing/selftests/x86/xstate.c +28 -10
@@ -6,7 +6,9 @@#include <pthread.h> #include <stdbool.h> +#include <asm/prctl.h> #include <sys/ptrace.h> +#include <sys/syscall.h> #include <sys/uio.h> #include <sys/wait.h>
@@ -189,15 +191,13 @@ static void affinitize_cpu0(void)ksft_exit_fail_msg ("sched_setaffinity to CPU 0 failed\n" ); } -void test_context_switch (uint32_t feature_num,uint32_t num_threads,uint32_t iterations) +static void test_context_switch (uint32_t num_threads,uint32_t iterations) {struct futex_info *finfo;/* Affinitize to one CPU to force context switches */ affinitize_cpu0 (); - xstate =get_xstate_info (feature_num); -printf ("[RUN]\t%s: check context switches, %d iterations, %d threads.\n" , xstate.name, iterations, num_threads);
@@ -299,13 +299,11 @@ static void ptracer_inject_xstate(pid_t target)free (xbuf2); } -void test_ptrace (uint32_t feature_num) +static void test_ptrace (void ) {pid_t child;int status; - xstate =get_xstate_info (feature_num); - child =fork ();if (child <0 ) {ksft_exit_fail_msg ("fork() failed\n" );
@@ -392,17 +390,14 @@ static void validate_sigfpstate(int sig, siginfo_t *si, void *ctx_void)copy_xstate (stashed_xbuf, xbuf); } -void test_signal (uint32_t feature_num) +static void test_signal (void ) {bool valid_xstate; - xstate =get_xstate_info (feature_num); -/* * The signal handler will access this to verify xstate context * preservation. */ - stashed_xbuf =alloc_xbuf ();if (!stashed_xbuf)ksft_exit_fail_msg ("unable to allocate XSAVE buffer\n" );
@@ -433,3 +428,26 @@ void test_signal(uint32_t feature_num)clearhandler (SIGUSR1 );free (stashed_xbuf); } + +void test_xstate (uint32_t feature_num) +{ +const unsigned int ctxtsw_num_threads =5 , ctxtsw_iterations =10 ; +unsigned long features; +long rc; + + rc =syscall (SYS_arch_prctl,ARCH_GET_XCOMP_SUPP , &features); +if (rc || !(features & (1 << feature_num))) { +ksft_print_msg ("The kernel does not support feature number: %u\n" , feature_num); +return ; + } + + xstate =get_xstate_info (feature_num); +if (!xstate.size || !xstate.xbuf_offset) { +ksft_exit_fail_msg ("invalid state size/offset (%d/%d)\n" , + xstate.size, xstate.xbuf_offset); + } + +test_context_switch (ctxtsw_num_threads, ctxtsw_iterations); +test_ptrace (); +test_signal (); +}
tools/testing/selftests/x86/xstate.h +2 -3
@@ -189,8 +189,7 @@ static inline void set_rand_data(struct xstate_info *xstate, struct xsave_buffer *ptr = data; } -void test_context_switch (uint32_t feature_num,uint32_t num_threads,uint32_t iterations); -void test_ptrace (uint32_t feature_num); -void test_signal (uint32_t feature_num); +/* Testing kernel's context switching and ABI support for the xstate. */ +void test_xstate (uint32_t feature_num);#endif /* __SELFTESTS_X86_XSTATE_H */
PATCH 8/9 selftests/x86/xstate: Clarify supported xstates
In reply to Chang S. BaeThe established xstate test code is designed to be generic, but certain xstates require special handling and cannot be tested without additional adjustments.
Clarify which xstates are currently supported, and enforce testing only for them.
--- tools/testing/selftests/x86/xstate.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
tools/testing/selftests/x86/xstate.c +24 -0
@@ -15,6 +15,24 @@#include "helpers.h" #include "xstate.h" +/* +* The userspace xstate test suite is designed to be generic and operates +* with randomized xstate data. However, some states require special handling: +* +* - PKRU and XTILECFG need specific adjustments, such as modifying +* randomization behavior or using fixed values. +* - But, PKRU already has a dedicated test suite in /tools/selftests/mm. +* - Legacy states (FP and SSE) are excluded, as they are not considered +* part of extended states (xstates) and their usage is already deeply +* integrated into user-space libraries. +*/ +#define XFEATURE_MASK_TEST_SUPPORTED \ + ((1 << XFEATURE_YMM) | \ + (1 << XFEATURE_OPMASK) | \ + (1 << XFEATURE_ZMM_Hi256) | \ + (1 << XFEATURE_Hi16_ZMM) | \ + (1 << XFEATURE_XTILEDATA)) +static inline uint64_t xgetbv (uint32_t index) {uint32_t eax, edx;
@@ -435,6 +453,12 @@ void test_xstate(uint32_t feature_num)unsigned long features;long rc; +if (!(XFEATURE_MASK_TEST_SUPPORTED & (1 << feature_num))) { +ksft_print_msg ("The xstate test does not fully support the component %u, yet.\n" , + feature_num); +return ; + } + rc =syscall (SYS_arch_prctl,ARCH_GET_XCOMP_SUPP , &features);if (rc || !(features & (1 << feature_num))) {ksft_print_msg ("The kernel does not support feature number: %u\n" , feature_num);
Signature · 1 line
2.45.2
[tip: x86/fpu] selftests/x86/xstate: Clarify supported xstates
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: fa826c1f2cc92db44c207d323d2568e3f04c1257 Gitweb: https://git.kernel.org/tip/fa826c1f2cc92db44c207d323d2568e3f04c1257 Author: Chang S. Bae <[email protected]> AuthorDate: Tue, 25 Feb 2025 17:07:28 -08:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 13:05:30 +01:00
selftests/x86/xstate: Clarify supported xstates
The established xstate test code is designed to be generic, but certain xstates require special handling and cannot be tested without additional adjustments.
Clarify which xstates are currently supported, and enforce testing only for them.
--- tools/testing/selftests/x86/xstate.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
tools/testing/selftests/x86/xstate.c +24 -0
@@ -15,6 +15,24 @@#include "helpers.h" #include "xstate.h" +/* +* The userspace xstate test suite is designed to be generic and operates +* with randomized xstate data. However, some states require special handling: +* +* - PKRU and XTILECFG need specific adjustments, such as modifying +* randomization behavior or using fixed values. +* - But, PKRU already has a dedicated test suite in /tools/selftests/mm. +* - Legacy states (FP and SSE) are excluded, as they are not considered +* part of extended states (xstates) and their usage is already deeply +* integrated into user-space libraries. +*/ +#define XFEATURE_MASK_TEST_SUPPORTED \ + ((1 << XFEATURE_YMM) | \ + (1 << XFEATURE_OPMASK) | \ + (1 << XFEATURE_ZMM_Hi256) | \ + (1 << XFEATURE_Hi16_ZMM) | \ + (1 << XFEATURE_XTILEDATA)) +static inline uint64_t xgetbv (uint32_t index) {uint32_t eax, edx;
@@ -435,6 +453,12 @@ void test_xstate(uint32_t feature_num)unsigned long features;long rc; +if (!(XFEATURE_MASK_TEST_SUPPORTED & (1 << feature_num))) { +ksft_print_msg ("The xstate test does not fully support the component %u, yet.\n" , + feature_num); +return ; + } + rc =syscall (SYS_arch_prctl,ARCH_GET_XCOMP_SUPP , &features);if (rc || !(features & (1 << feature_num))) {ksft_print_msg ("The kernel does not support feature number: %u\n" , feature_num);
PATCH 9/9 selftests/x86/avx: Add AVX test
In reply to Chang S. BaeAdd xstate testing specifically for those vector register states, validating kernel's context switching and ensuring ABI compliance. Use the established xstate testing framework.
---
Alternatively, this invocation could be placed directly in xstate.c::main(). However, the current test file naming convention, which clearly specifies the tested area, seems reasonable. Adding avx.c considerably aligns with that convention.
The test output should be like this for ZMM_Hi256 as an example:
$ avx_64 ... [RUN] AVX-512 ZMM_Hi256: check context switches, 10 iterations, 5 threads. [OK] No incorrect case was found. [RUN] AVX-512 ZMM_Hi256: inject xstate via ptrace(). [OK] 'xfeatures' in SW reserved area was correctly written [OK] xstate was correctly updated. [RUN] AVX-512 ZMM_Hi256: load xstate and raise SIGUSR1 [OK] 'magic1' is valid [OK] 'xfeatures' in SW reserved area is valid [OK] 'xfeatures' in XSAVE header is valid [OK] xstate delivery was successful [OK] 'magic2' is valid [RUN] AVX-512 ZMM_Hi256: load new xstate from sighandler and check it after sigreturn [OK] xstate was restored correctly
But systems without AVX-512 will look like:
...
The kernel does not support feature number: 5
The kernel does not support feature number: 6
The kernel does not support feature number: 7
--- tools/testing/selftests/x86/Makefile | 4 +++- tools/testing/selftests/x86/avx.c | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/x86/avx.c
tools/testing/selftests/x86/Makefile +3 -1
@@ -19,7 +19,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 lam test_shadow_stack + corrupt_xstate_header amx lam test_shadow_stack avx # Some selftests require 32bit support enabled also on 64bit systems TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
@@ -133,4 +133,6 @@ $(OUTPUT)/check_initial_reg_state_64: CFLAGS += -Wl,-ereal_start -static $(OUTPUT)/nx_stack_32: CFLAGS += -Wl,-z,noexecstack $(OUTPUT)/nx_stack_64: CFLAGS += -Wl,-z,noexecstack +$(OUTPUT)/avx_64: CFLAGS += -mno-avx -mno-avx512f $(OUTPUT)/amx_64: EXTRA_FILES += xstate.c +$(OUTPUT)/avx_64: EXTRA_FILES += xstate.c
tools/testing/selftests/x86/avx.c new file +12 -0
@@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE/* Required for inline xstate helpers */ +#include "xstate.h" + +int main (void ) +{ +test_xstate (XFEATURE_YMM ); +test_xstate (XFEATURE_OPMASK ); +test_xstate (XFEATURE_ZMM_Hi256); +test_xstate (XFEATURE_Hi16_ZMM); +}
Signature · 1 line
2.45.2
[tip: x86/fpu] selftests/x86/avx: Add AVX tests
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: bfc98dbcb3c75c6e2eb1dcf389d02a8c2419c41c Gitweb: https://git.kernel.org/tip/bfc98dbcb3c75c6e2eb1dcf389d02a8c2419c41c Author: Chang S. Bae <[email protected]> AuthorDate: Tue, 25 Feb 2025 17:07:29 -08:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 13:05:30 +01:00
selftests/x86/avx: Add AVX tests
Add xstate testing specifically for those vector register states, validating kernel's context switching and ensuring ABI compliance. Use the established xstate testing framework.
Alternatively, this invocation could be placed directly in xstate.c::main(). However, the current test file naming convention, which clearly specifies the tested area, seems reasonable. Adding avx.c considerably aligns with that convention.
The test output should be like this for ZMM_Hi256 as an example:
$ avx_64 ... [RUN] AVX-512 ZMM_Hi256: check context switches, 10 iterations, 5 threads. [OK] No incorrect case was found. [RUN] AVX-512 ZMM_Hi256: inject xstate via ptrace(). [OK] 'xfeatures' in SW reserved area was correctly written [OK] xstate was correctly updated. [RUN] AVX-512 ZMM_Hi256: load xstate and raise SIGUSR1 [OK] 'magic1' is valid [OK] 'xfeatures' in SW reserved area is valid [OK] 'xfeatures' in XSAVE header is valid [OK] xstate delivery was successful [OK] 'magic2' is valid [RUN] AVX-512 ZMM_Hi256: load new xstate from sighandler and check it after sigreturn [OK] xstate was restored correctly
But systems without AVX-512 will look like:
...
The kernel does not support feature number: 5
The kernel does not support feature number: 6
The kernel does not support feature number: 7
--- tools/testing/selftests/x86/Makefile | 4 +++- tools/testing/selftests/x86/avx.c | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/x86/avx.c
tools/testing/selftests/x86/Makefile +3 -1
@@ -19,7 +19,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 lam test_shadow_stack + corrupt_xstate_header amx lam test_shadow_stack avx # Some selftests require 32bit support enabled also on 64bit systems TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
@@ -133,4 +133,6 @@ $(OUTPUT)/check_initial_reg_state_64: CFLAGS += -Wl,-ereal_start -static $(OUTPUT)/nx_stack_32: CFLAGS += -Wl,-z,noexecstack $(OUTPUT)/nx_stack_64: CFLAGS += -Wl,-z,noexecstack +$(OUTPUT)/avx_64: CFLAGS += -mno-avx -mno-avx512f $(OUTPUT)/amx_64: EXTRA_FILES += xstate.c +$(OUTPUT)/avx_64: EXTRA_FILES += xstate.c
tools/testing/selftests/x86/avx.c new file +12 -0
@@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE/* Required for inline xstate helpers */ +#include "xstate.h" + +int main (void ) +{ +test_xstate (XFEATURE_YMM ); +test_xstate (XFEATURE_OPMASK ); +test_xstate (XFEATURE_ZMM_Hi256); +test_xstate (XFEATURE_Hi16_ZMM); +}
Quoted 79 lines · Chang S. Bae
Hi all,
This series proposes a rework of xstate-related tests to improve maintainability and expand test coverage.
== Motivation: Addressing Missing and New XSTATE Tests ==
With the introduction of AMX, a new test suite [1] was created to verify dynamic state handling by the kernel as observed from userspace. However, previous tests for non-dynamic states like AVX lacked ABI validation, leaving gaps in coverage. While these states currently function without major issues (following the alternate sigstack fix [2]), xstate testing in the x86 selftest suite has been largely overlooked.
Now, with Intel introducing another extended state, Advanced Performance Extensions (APX) [3], a correspondent test case is need. The APX enabling series will follow shortly and will leverage this refactored selftest framework.
== Selftest Code Rework ==
To ensure ABI validation and core functionality across various xstates, refactoring the test code is necessary. Without this, existing code from
amx.cwould need to be duplicated, compromising the structural quality of xstate tests.This series introduces a shared test framework for extended state validation, applicable to both existing and new xstates. The test cases cover:
* Context switching
* ABI compatibility for signal handling
* ABI compatibility for ptrace interactions== Patch Organization ==
The patchset is structured as follows:
* PATCH1: Preparatory cleanup — removing redundant signal handler registration code.
* PATCH2/3: Introduce low-level XSAVE helpers and xstate component enumeration.
* PATCH4/5: Refactor existing test code.
* PATCH6: Introduce a new signal test case.
* PATCH7/8: Consolidate test invocations and clarify the list of supported features.
* PATCH9: Add test coverage for AVX.== Coverage and Future Work Considerations ==
Currently, these tests are aligned with 64-bit mode only. Support for 32-bit cases will be considered when necessary, but only after this phase of rework is finalized.
FWIW, the AMX TILECFG state is trivial, requiring almost constant values. Additionally, various PKRU tests are already established in
tools/selftests/mm.This series is based on the tip/master branch. You can also find it in the following repository:
git://github.com/intel/apx.gitselftest-xstate_v1Thanks,
Chang[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://www.intel.com/content/www/us/en/developer/articles/technical/advanced-performance-extensions-apx.htmlChang S. Bae (9):
selftests/x86: Consolidate redundant signal helper functions selftests/x86/xstate: Refactor XSAVE helpers for general use selftests/x86/xstate: Enumerate and name xstate components selftests/x86/xstate: Refactor context switching test selftests/x86/xstate: Refactor ptrace ABI test selftests/x86/xstate: Introduce signal ABI test selftests/x86/xstate: Consolidate test invocations into a single entry selftests/x86/xstate: Clarify supported xstates selftests/x86/avx: Add AVX test
Quoted 1 line
22 files changed, 753 insertions(+), 720 deletions(-)
Thank you very much for doing this - this series is a very big step forward in this area.
I've applied your series to tip:x86/fpu with minor edits to the changelogs, and if all goes fine it should all go upstream in the v6.15 merge window in a couple of weeks.
Thanks,
Ingo