arch/x86/kernel/shstk.c
Source file repositories/reference/linux-study-clean/arch/x86/kernel/shstk.c
File Facts
- System
- Linux kernel
- Corpus path
arch/x86/kernel/shstk.c- Extension
.c- Size
- 14047 bytes
- Lines
- 633
- Domain
- Architecture Layer
- Bucket
- arch/x86
- Inferred role
- Architecture Layer: syscall or user/kernel boundary
- Status
- core implementation candidate
Why This File Exists
CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.
- CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.
- Defines or participates in a user/kernel boundary; inspect argument validation, copy_from_user/copy_to_user, credentials, and dispatch target.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/sched.hlinux/bitops.hlinux/types.hlinux/mm.hlinux/mman.hlinux/slab.hlinux/uaccess.hlinux/sched/signal.hlinux/compat.hlinux/sizes.hlinux/user.hlinux/syscalls.hasm/msr.hasm/fpu/xstate.hasm/fpu/types.hasm/shstk.hasm/special_insns.hasm/fpu/api.hasm/prctl.h
Detected Declarations
syscall map_shadow_stackfunction Copyrightfunction features_setfunction features_clrfunction create_rstor_tokenfunction alloc_shstkfunction adjust_shstk_sizefunction unmap_shadow_stackfunction shstk_setupfunction reset_thread_featuresfunction shstk_alloc_thread_stackfunction get_user_shstk_addrfunction shstk_popfunction shstk_pushfunction put_shstk_datafunction get_shstk_datafunction shstk_push_sigframefunction shstk_pop_sigframefunction setup_signal_shadow_stackfunction restore_signal_shadow_stackfunction shstk_freefunction wrss_controlfunction shstk_disablefunction shstk_prctlfunction shstk_update_last_framefunction shstk_is_enabled
Annotated Snippet
SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size, unsigned int, flags)
{
bool set_tok = flags & SHADOW_STACK_SET_TOKEN;
unsigned long aligned_size;
if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK))
return -EOPNOTSUPP;
if (flags & ~SHADOW_STACK_SET_TOKEN)
return -EINVAL;
/* If there isn't space for a token */
if (set_tok && size < 8)
return -ENOSPC;
if (addr && addr < SZ_4G)
return -ERANGE;
/*
* An overflow would result in attempting to write the restore token
* to the wrong location. Not catastrophic, but just return the right
* error code and block it.
*/
aligned_size = PAGE_ALIGN(size);
if (aligned_size < size)
return -EOVERFLOW;
return alloc_shstk(addr, aligned_size, size, set_tok);
}
long shstk_prctl(struct task_struct *task, int option, unsigned long arg2)
{
unsigned long features = arg2;
if (option == ARCH_SHSTK_STATUS) {
return put_user(task->thread.features, (unsigned long __user *)arg2);
}
if (option == ARCH_SHSTK_LOCK) {
task->thread.features_locked |= features;
return 0;
}
/* Only allow via ptrace */
if (task != current) {
if (option == ARCH_SHSTK_UNLOCK && IS_ENABLED(CONFIG_CHECKPOINT_RESTORE)) {
task->thread.features_locked &= ~features;
return 0;
}
return -EINVAL;
}
/* Do not allow to change locked features */
if (features & task->thread.features_locked)
return -EPERM;
/* Only support enabling/disabling one feature at a time. */
if (hweight_long(features) > 1)
return -EINVAL;
if (option == ARCH_SHSTK_DISABLE) {
if (features & ARCH_SHSTK_WRSS)
return wrss_control(false);
if (features & ARCH_SHSTK_SHSTK)
return shstk_disable();
return -EINVAL;
}
/* Handle ARCH_SHSTK_ENABLE */
if (features & ARCH_SHSTK_SHSTK)
return shstk_setup();
if (features & ARCH_SHSTK_WRSS)
return wrss_control(true);
return -EINVAL;
}
int shstk_update_last_frame(unsigned long val)
{
unsigned long ssp;
if (!features_enabled(ARCH_SHSTK_SHSTK))
return 0;
ssp = get_user_shstk_addr();
return write_user_shstk_64((u64 __user *)ssp, (u64)val);
}
bool shstk_is_enabled(void)
{
return features_enabled(ARCH_SHSTK_SHSTK);
Annotation
- Immediate include surface: `linux/sched.h`, `linux/bitops.h`, `linux/types.h`, `linux/mm.h`, `linux/mman.h`, `linux/slab.h`, `linux/uaccess.h`, `linux/sched/signal.h`.
- Detected declarations: `syscall map_shadow_stack`, `function Copyright`, `function features_set`, `function features_clr`, `function create_rstor_token`, `function alloc_shstk`, `function adjust_shstk_size`, `function unmap_shadow_stack`, `function shstk_setup`, `function reset_thread_features`.
- Atlas domain: Architecture Layer / arch/x86.
- Implementation status: core implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
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.