[RFC PATCH 24/27] x86/process: Initialize shadow stack for IDT systems

From: K Prateek Nayak

Date: Sun Nov 29 2026 - 17:37:24 EST


On IDT systems, the bottom of the stack should always have a supervisor
token that can be marked busy by the hardware. Populate the supervisor
token in the fist slot and, like FRED path, populate the first return
address and the restore token below.

Since SSP0 should point to the supervisor slot, set SSP0 to one slot
below task_shstk_top() that contains the supervisor token.

The SSP0 reset is no longer limited to FRED enabled systems. Use
MSR_IA32_PL0_SSP to reference the MSR instead of the FRED alias.

Signed-off-by: K Prateek Nayak <kprateek.nayak@xxxxxxx>
---
arch/x86/include/asm/cpu.h | 4 +++-
arch/x86/include/asm/entry-common.h | 19 ++++++++++++++++++-
arch/x86/include/asm/fred.h | 7 -------
arch/x86/kernel/process.c | 17 +++++++++++++++--
4 files changed, 36 insertions(+), 11 deletions(-)

diff --git a/arch/x86/include/asm/cpu.h b/arch/x86/include/asm/cpu.h
index e8227cc1336b..8c91a3ee59d8 100644
--- a/arch/x86/include/asm/cpu.h
+++ b/arch/x86/include/asm/cpu.h
@@ -138,8 +138,10 @@ static __always_inline unsigned long disable_kernel_shstk(void)
static __always_inline void cpu_setup_kernel_shstk(bool boot_cpu)
{
struct task_struct *tsk = current;
+ u64 ssp0 = (u64)task_shstk_top(tsk) - sizeof(unsigned long);
unsigned long shstk_top;

+
if (!cpu_feature_enabled(X86_FEATURE_KERN_SHSTK))
return;

@@ -152,7 +154,7 @@ static __always_inline void cpu_setup_kernel_shstk(bool boot_cpu)
shstk_top = (unsigned long)tsk->thread.kshstk_ssp;

load_ssp_table();
- wrmsrq(MSR_IA32_FRED_SSP0, shstk_top);
+ wrmsrq(MSR_IA32_PL0_SSP, ssp0);
enable_kernel_shstk(shstk_top);
}

diff --git a/arch/x86/include/asm/entry-common.h b/arch/x86/include/asm/entry-common.h
index 470ac946afba..10960d8237c9 100644
--- a/arch/x86/include/asm/entry-common.h
+++ b/arch/x86/include/asm/entry-common.h
@@ -56,6 +56,23 @@ static inline void arch_exit_work(unsigned long ti_work)
switch_fpu_return();
}

+#ifdef CONFIG_X86_KERN_SHADOW_STACK
+static inline void update_ssp0(void)
+{
+ if (current->thread.kshstk_ssp) {
+ u64 top = (u64)task_shstk_top(current);
+
+ /* On IDT systems, restore past the token. */
+ if (!cpu_feature_enabled(X86_FEATURE_FRED))
+ top -= sizeof(unsigned long);
+
+ wrmsrns(MSR_IA32_PL0_SSP, top);
+ }
+}
+#else /* !CONFIG_X86_KERN_SHADOW_STACK */
+static inline void update_ssp0(void) { }
+#endif /* CONFIG_X86_KERN_SHADOW_STACK */
+
static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
unsigned long ti_work)
{
@@ -65,7 +82,7 @@ static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
arch_exit_work(ti_work);

fred_update_rsp0();
- fred_update_ssp0();
+ update_ssp0();
#ifdef CONFIG_COMPAT
/*
* Compat syscalls set TS_COMPAT. Make sure we clear it before
diff --git a/arch/x86/include/asm/fred.h b/arch/x86/include/asm/fred.h
index 57c7e78c787a..477b0b2605a7 100644
--- a/arch/x86/include/asm/fred.h
+++ b/arch/x86/include/asm/fred.h
@@ -105,13 +105,6 @@ static __always_inline void fred_update_rsp0(void)
__this_cpu_write(fred_rsp0, rsp0);
}
}
-static __always_inline void fred_update_ssp0(void)
-{
-#ifdef CONFIG_X86_KERN_SHADOW_STACK
- if (current->thread.kshstk_ssp)
- wrmsrns(MSR_IA32_FRED_SSP0, (u64)task_shstk_top(current));
-#endif
-}
#else /* CONFIG_X86_FRED */
static __always_inline unsigned long fred_event_data(struct pt_regs *regs) { return 0; }
static inline void cpu_init_fred_exceptions(void) { }
diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
index 357c815d4522..661932d894cb 100644
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -206,8 +206,21 @@ void prep_kernel_shadow_stack(struct task_struct *tsk,
* Write the return address for the first RET: and point the task's
* kshstk_ssp to the top slot.
*/
- *ss_top_slot = (unsigned long)ret_addr;
- tsk->thread.kshstk_ssp = task_shstk_top(tsk) - sizeof(unsigned long);
+ if (cpu_feature_enabled(X86_FEATURE_FRED)) {
+ *ss_top_slot = (unsigned long)ret_addr;
+ tsk->thread.kshstk_ssp = task_shstk_top(tsk) - sizeof(unsigned long);
+
+ } else {
+ unsigned long *shstk_top = task_shstk_top(tsk);
+
+ /* Supervisor token for PL0_SSP */
+ *ss_top_slot = (unsigned long)(shstk_top - 1);
+
+ ss_top_slot -= 1;
+ /* Token for first return. */
+ *ss_top_slot = (unsigned long)ret_addr;
+ tsk->thread.kshstk_ssp = shstk_top - 2;
+ }

/*
* Back up one more frame and write the token for rstorssp when this
--
2.43.0