tools/testing/selftests/x86/xstate.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/x86/xstate.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/x86/xstate.c- Extension
.c- Size
- 11994 bytes
- Lines
- 479
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
elf.hpthread.hstdbool.hasm/prctl.hsys/ptrace.hsys/syscall.hsys/uio.hsys/wait.hhelpers.hxstate.h
Detected Declarations
struct futex_infofunction statesfunction get_xstatebvfunction load_rand_xstatefunction load_init_xstatefunction copy_xstatefunction validate_xstate_samefunction validate_xregs_samefunction create_threadsfunction checkout_threadsfunction affinitize_cpu0function test_context_switchfunction ptracee_touch_xstatefunction ptracer_inject_xstatefunction test_ptracefunction sig_printfunction validate_sigfpstatefunction test_signalfunction test_xstate
Annotated Snippet
struct futex_info {
unsigned int iterations;
struct futex_info *next;
pthread_mutex_t mutex;
pthread_t thread;
bool valid;
int nr;
};
static inline void load_rand_xstate(struct xstate_info *xstate, struct xsave_buffer *xbuf)
{
clear_xstate_header(xbuf);
set_xstatebv(xbuf, xstate->mask);
set_rand_data(xstate, xbuf);
xrstor(xbuf, xstate->mask);
}
static inline void load_init_xstate(struct xstate_info *xstate, struct xsave_buffer *xbuf)
{
clear_xstate_header(xbuf);
xrstor(xbuf, xstate->mask);
}
static inline void copy_xstate(struct xsave_buffer *xbuf_dst, struct xsave_buffer *xbuf_src)
{
memcpy(&xbuf_dst->bytes[xstate.xbuf_offset],
&xbuf_src->bytes[xstate.xbuf_offset],
xstate.size);
}
static inline bool validate_xstate_same(struct xsave_buffer *xbuf1, struct xsave_buffer *xbuf2)
{
int ret;
ret = memcmp(&xbuf1->bytes[xstate.xbuf_offset],
&xbuf2->bytes[xstate.xbuf_offset],
xstate.size);
return ret == 0;
}
static inline bool validate_xregs_same(struct xsave_buffer *xbuf1)
{
struct xsave_buffer *xbuf2;
bool ret;
xbuf2 = alloc_xbuf();
if (!xbuf2)
ksft_exit_fail_msg("failed to allocate XSAVE buffer\n");
xsave(xbuf2, xstate.mask);
ret = validate_xstate_same(xbuf1, xbuf2);
free(xbuf2);
return ret;
}
/* Context switching test */
static void *check_xstate(void *info)
{
struct futex_info *finfo = (struct futex_info *)info;
struct xsave_buffer *xbuf;
int i;
xbuf = alloc_xbuf();
if (!xbuf)
ksft_exit_fail_msg("unable to allocate XSAVE buffer\n");
/*
* Load random data into 'xbuf' and then restore it to the xstate
* registers.
*/
load_rand_xstate(&xstate, xbuf);
finfo->valid = true;
for (i = 0; i < finfo->iterations; i++) {
pthread_mutex_lock(&finfo->mutex);
/*
* Ensure the register values have not diverged from the
* record. Then reload a new random value. If it failed
* ever before, skip it.
*/
if (finfo->valid) {
finfo->valid = validate_xregs_same(xbuf);
load_rand_xstate(&xstate, xbuf);
}
/*
* The last thread's last unlock will be for thread 0's
Annotation
- Immediate include surface: `elf.h`, `pthread.h`, `stdbool.h`, `asm/prctl.h`, `sys/ptrace.h`, `sys/syscall.h`, `sys/uio.h`, `sys/wait.h`.
- Detected declarations: `struct futex_info`, `function states`, `function get_xstatebv`, `function load_rand_xstate`, `function load_init_xstate`, `function copy_xstate`, `function validate_xstate_same`, `function validate_xregs_same`, `function create_threads`, `function checkout_threads`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.