1 // SPDX-License-Identifier: GPL-2.0-only
3 * Corrupt the XSTATE header in a signal frame
5 * Based on analysis and a test case from Thomas Gleixner.
20 #include "../kselftest.h" /* For __cpuid_count() */
22 static inline int xsave_enabled(void)
24 unsigned int eax
, ebx
, ecx
, edx
;
26 __cpuid_count(0x1, 0x0, eax
, ebx
, ecx
, edx
);
28 /* Is CR4.OSXSAVE enabled ? */
29 return ecx
& (1U << 27);
32 static void sethandler(int sig
, void (*handler
)(int, siginfo_t
*, void *),
37 memset(&sa
, 0, sizeof(sa
));
38 sa
.sa_sigaction
= handler
;
39 sa
.sa_flags
= SA_SIGINFO
| flags
;
40 sigemptyset(&sa
.sa_mask
);
41 if (sigaction(sig
, &sa
, 0))
45 static void sigusr1(int sig
, siginfo_t
*info
, void *uc_void
)
47 ucontext_t
*uc
= uc_void
;
48 uint8_t *fpstate
= (uint8_t *)uc
->uc_mcontext
.fpregs
;
49 uint64_t *xfeatures
= (uint64_t *)(fpstate
+ 512);
51 printf("\tWreck XSTATE header\n");
52 /* Wreck the first reserved bytes in the header */
53 *(xfeatures
+ 2) = 0xfffffff;
56 static void sigsegv(int sig
, siginfo_t
*info
, void *uc_void
)
58 printf("\tGot SIGSEGV\n");
65 sethandler(SIGUSR1
, sigusr1
, 0);
66 sethandler(SIGSEGV
, sigsegv
, 0);
68 if (!xsave_enabled()) {
69 printf("[SKIP] CR4.OSXSAVE disabled.\n");
77 * Enforce that the child runs on the same CPU
78 * which in turn forces a schedule.
80 sched_setaffinity(getpid(), sizeof(set
), &set
);
82 printf("[RUN]\tSend ourselves a signal\n");
85 printf("[OK]\tBack from the signal. Now schedule.\n");
92 waitpid(child
, NULL
, 0);
93 printf("[OK]\tBack in the main thread.\n");
96 * We could try to confirm that extended state is still preserved
97 * when we schedule. For now, the only indication of failure is
98 * a warning in the kernel logs.