[RFC PATCH 22/27] x86/shstk: Save and restore shadow stack pointer during kexec
From: K Prateek Nayak
Date: Sun Nov 29 2026 - 17:36:53 EST
To support KEXEC and KEXEC_JUMP, save and restore shadow stack token on
either side of relocate_kernel_ptr(). On regual kexec path,
disable_kernel_shstk() from stop_this_cpu() will disable shadow stack on
APs after the APIC is turned off. KEXEC_JUMP would disable the shadow
stack from the offline callback.
restore_processor_state() populates the CR4 and FRED exceptions, RSPs
and SSPs on the way back - restore the shadow stack token on the way
back in machine_kexec() after the necessary FRED bits have been
initialized.
APs re-enable shadow stack when coming online in start_secondary.
Signed-off-by: K Prateek Nayak <kprateek.nayak@xxxxxxx>
---
arch/x86/Kconfig | 4 ----
arch/x86/kernel/machine_kexec_64.c | 4 ++++
arch/x86/kernel/process.c | 1 +
3 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 4f5097fdd365..c101f7b23644 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -1907,10 +1907,6 @@ config X86_KERN_SHADOW_STACK
depends on VMAP_STACK
depends on AS_WRUSS
select X86_CET
- # relocate_kernel() switches CR3 and CR4 with a shadow stack live
- # and uses "pushq $0 ... ret" as the handoff into purgatory. It
- # needs S_CET.SH_STK_EN cleared first, which nothing does yet.
- depends on !KEXEC_CORE
# __efi64_thunk() drops into 32-bit firmware with LRETQ and comes
# back with LRET. Far returns are shadow stack checked and expect
# a far CALL frame that was never pushed.
diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_kexec_64.c
index c3f4a389992d..e18f7d292d62 100644
--- a/arch/x86/kernel/machine_kexec_64.c
+++ b/arch/x86/kernel/machine_kexec_64.c
@@ -386,6 +386,7 @@ void __nocfi machine_kexec(struct kimage *image)
unsigned long reloc_start = (unsigned long)__relocate_kernel_start;
relocate_kernel_fn *relocate_kernel_ptr;
unsigned int relocate_kernel_flags;
+ unsigned long shstk_token;
int save_ftrace_enabled;
void *control_page;
@@ -399,6 +400,8 @@ void __nocfi machine_kexec(struct kimage *image)
/* Interrupts aren't acceptable while we reboot */
local_irq_disable();
hw_breakpoint_disable();
+
+ shstk_token = disable_kernel_shstk();
cet_disable();
if (image->preserve_context) {
@@ -461,6 +464,7 @@ void __nocfi machine_kexec(struct kimage *image)
restore_processor_state();
#endif
+ enable_kernel_shstk(shstk_token);
__ftrace_enabled_restore(save_ftrace_enabled);
}
/*
diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
index 7b3a9f112451..357c815d4522 100644
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -902,6 +902,7 @@ void __noreturn stop_this_cpu(void *dummy)
set_cpu_online(cpu, false);
disable_local_APIC();
mcheck_cpu_clear(c);
+ disable_kernel_shstk();
if (this_cpu_read(cache_state_incoherent))
wbinvd();
--
2.43.0