[RFC PATCH 25/27] x86/entry: Prepare IDT entry for shadow stacks

From: K Prateek Nayak

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


Similar to FRED entry, IDT entry must populate a metadata frame around
the pt_regs. Unlike FRED entry point which clearly distinguishes entry
form kernel vs entry from user, IDT entry has a common entry point.

The POP_KERN_SHSTK_FRAME needs a consistent stack state to locate CS and
pop accordingly which makes the handling complicated. Always push two
slots - two blank ones for user entry and the correct FRED frame for
entry from kernel such that POP_KERN_SHSTK_FRAME can always see a
consistent state.

Once the CET helpers see a non-NULL shadow stack pointer, it can operate
on the shadow stack frames for kernel events.

Signed-off-by: K Prateek Nayak <kprateek.nayak@xxxxxxx>
---
arch/x86/entry/calling.h | 85 ++++++++++++++++++++++++++++++++++
arch/x86/entry/entry_64.S | 24 ++++++++++
arch/x86/entry/entry_64_fred.S | 73 -----------------------------
3 files changed, 109 insertions(+), 73 deletions(-)

diff --git a/arch/x86/entry/calling.h b/arch/x86/entry/calling.h
index 77e2d920a640..402c08bac5b5 100644
--- a/arch/x86/entry/calling.h
+++ b/arch/x86/entry/calling.h
@@ -381,6 +381,91 @@ For 32-bit we have the following conventions - kernel is built with
wrgsbase \scratch_reg
.endm

+#ifdef CONFIG_X86_KERN_SHADOW_STACK
+
+/*
+ * A INT3 breakpoint can emulate a CALL which requires pushing the
+ * return frame below this FRED event. Allow the handlers to communicate
+ * if it emulated a CALL by setting a flag on the stack.
+ *
+ * FRED event pushes { SSP, LIP, CS } onto the shadow stack and SSP
+ * points to record three slots for top here and it should stay that
+ * way.
+ *
+ * CALL emulation will consume the SSP, push a return frame below, and
+ * set a flag on stack. If the flag is set, FRED_EXIT will add an
+ * shadow stack frame on top by doing an INTRA_FUNCTION_CALL and
+ * populate the top with a pointer to three slots below, thus recreating
+ * the correct FRED frame.
+ */
+.macro PUSH_KERN_SHSTK_FRAME
+ xor %eax, %eax
+ rdsspq %rax
+ pushq %rax
+ /*
+ * Frame metadata:
+ *
+ * [15:8] Context (FRED_EVENT)
+ * [ 7:0] fred_exit_work_flags
+ *
+ * Context data is used by CET helpers to interpret
+ * SSP frame. fred_exit_work_flags is used to fixup
+ * shadow stack for CALL emulation.
+ */
+ pushq $0x100
+.endm
+
+/* Clobbers RAX, RCX; Both will be restored later. */
+.macro POP_KERN_SHSTK_FRAME
+ popq %rax /* fred_exit_work_flags */
+
+ /* Only one action is supported currently. */
+ test %al, %al
+ jz .Lpop_shstk_done\@
+
+ /*
+ * CALL Emulation has overwritten the shadow-stack frame and
+ * SSP is lost. Point it back to 3 slots down where the real
+ * return has to go.
+ *
+ * Doing a CALL here will add a RSB which the next RET will
+ * mispredict on but all RETs after that are balanced.
+ */
+ ANNOTATE_INTRA_FUNCTION_CALL
+ call 1f
+ int3
+1:
+ /* Synthesize the corrct SSP to fix the FRED Frame. */
+ addq $8, %rsp
+ rdsspq %rax
+ leaq 24(%rax), %rcx
+ wrssq %rcx, (%rax)
+
+.Lpop_shstk_done\@:
+ popq %rax
+.endm
+
+.macro PUSH_USER_SHSTK_FRAME
+ subq $16, %rsp
+.endm
+
+.macro POP_USER_SHSTK_FRAME
+ addq $16, %rsp
+.endm
+
+#else /* !CONFIG_X86_KERN_SHADOW_STACK */
+
+.macro PUSH_KERN_SHSTK_FRAME
+.endm
+.macro POP_KERN_SHSTK_FRAME
+.endm
+.macro PUSH_USER_SHSTK_FRAME
+.endm
+.macro POP_USER_SHSTK_FRAME
+.endm
+
+#endif /* CONFIG_X86_KERN_SHADOW_STACK */
+
#else /* CONFIG_X86_64 */
# undef UNWIND_HINT_IRET_REGS
# define UNWIND_HINT_IRET_REGS
diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
index c255722cd66d..6ce8c523b2d6 100644
--- a/arch/x86/entry/entry_64.S
+++ b/arch/x86/entry/entry_64.S
@@ -296,6 +296,26 @@ SYM_CODE_START(xen_error_entry)
RET
SYM_CODE_END(xen_error_entry)

+.macro PUSH_IDT_SHSTK_FRAME
+ testb $3, CS(%rsp)
+ jnz .Lpush_from_usermode\@
+ PUSH_KERN_SHSTK_FRAME
+ jmp .Lpush_shstk_done\@
+.Lpush_from_usermode\@:
+ PUSH_USER_SHSTK_FRAME
+.Lpush_shstk_done\@:
+.endm
+
+.macro POP_IDT_SHSTK_FRAME
+ testb $3, CS+16(%rsp)
+ jnz .Lpop_from_usermode\@
+ POP_KERN_SHSTK_FRAME
+ jmp .Lpush_shstk_done\@
+.Lpop_from_usermode\@:
+ POP_USER_SHSTK_FRAME
+.Lpush_shstk_done\@:
+.endm
+
/**
* idtentry_body - Macro to emit code calling the C function
* @cfunc: C function to be called
@@ -324,10 +344,14 @@ SYM_CODE_END(xen_error_entry)
movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
.endif

+ PUSH_IDT_SHSTK_FRAME
+
/* For some configurations \cfunc ends up being a noreturn. */
ANNOTATE_REACHABLE
call \cfunc

+ POP_IDT_SHSTK_FRAME
+
jmp error_return
.endm

diff --git a/arch/x86/entry/entry_64_fred.S b/arch/x86/entry/entry_64_fred.S
index 284976114c70..2119d0da50b4 100644
--- a/arch/x86/entry/entry_64_fred.S
+++ b/arch/x86/entry/entry_64_fred.S
@@ -17,79 +17,6 @@
.code64
.section .noinstr.text, "ax"

-#ifdef CONFIG_X86_KERN_SHADOW_STACK
-
-/*
- * A INT3 breakpoint can emulate a CALL which requires pushing the
- * return frame below this FRED event. Allow the handlers to communicate
- * if it emulated a CALL by setting a flag on the stack.
- *
- * FRED event pushes { SSP, LIP, CS } onto the shadow stack and SSP
- * points to record three slots for top here and it should stay that
- * way.
- *
- * CALL emulation will consume the SSP, push a return frame below, and
- * set a flag on stack. If the flag is set, FRED_EXIT will add an
- * shadow stack frame on top by doing an INTRA_FUNCTION_CALL and
- * populate the top with a pointer to three slots below, thus recreating
- * the correct FRED frame.
- */
-.macro PUSH_KERN_SHSTK_FRAME
- xor %eax, %eax
- rdsspq %rax /* NOP if SHSTK is off. */
- pushq %rax
- /*
- * Frame metadata:
- *
- * [15:8] Context (FRED_EVENT)
- * [ 7:0] fred_exit_work_flags
- *
- * Context data is used by CET helpers to interpret
- * SSP frame. fred_exit_work_flags is used to fixup
- * shadow stack for CALL emulation.
- */
- pushq $0x100
-.endm
-
-/* Clobbers RAX, RCX; Both will be restored later. */
-.macro POP_KERN_SHSTK_FRAME
- popq %rax /* fred_exit_work_flags */
-
- /* Only one action is supported currently. */
- test %al, %al
- jz .Lpop_shstk_done\@
-
- /*
- * CALL Emulation has overwritten the shadow-stack frame and
- * SSP is lost. Point it back to 3 slots down where the real
- * return has to go.
- *
- * Doing a CALL here will add a RSB which the next RET will
- * mispredict on but all RETs after that are balanced.
- */
- ANNOTATE_INTRA_FUNCTION_CALL
- call 1f
- int3
-1:
- /* Synthesize the corrct SSP to fix the FRED Frame. */
- addq $8, %rsp
- rdsspq %rax
- leaq 24(%rax), %rcx
- wrssq %rcx, (%rax)
-
-.Lpop_shstk_done\@:
- popq %rax
-.endm
-
-#else /* !CONFIG_X86_KERN_SHADOW_STACK */
-
-.macro PUSH_KERN_SHSTK_FRAME
-.endm
-.macro POP_KERN_SHSTK_FRAME
-.endm
-
-#endif /* CONFIG_X86_KERN_SHADOW_STACK */
-
.macro FRED_ENTER ssp=0
UNWIND_HINT_END_OF_STACK
ANNOTATE_NOENDBR
--
2.43.0