[RFC PATCH 27/27] x86/entry: Handle shadow stack on nested NMI
From: K Prateek Nayak
Date: Sun Nov 29 2026 - 17:38:23 EST
IDT sets NMI on IST gate 1 and teh corresponding shadow shadows from
INT_SSP_TAB is asserted and marked busy.
On userspace NMIs that use the task's stack, clear the busy bit from the
NMI shadow stack to allow nesting and switch to PL0_SSP stack. Since it
is an NMI from userspace, the PL0SSP stack is empty and the stack can be
used safely from the top.
NMI from kernel needs special handling. Similar to the NMI from
userspace handling, clear the busy bit on the IST gate token, and switch
back to the same shadow stack at Gate 1 via restorssp.
Once back, the shadow stack establishes the same pattern as the regular
stack by duplicating { CS, LIP, SS } on top as the inner frame. The NMI
entry in "repeat NMI" will replicate the entries a second time to
stablish the outer frame.
The structure of shadow stack is as follows:
+-------------------------------+
| supervisor token |
+-------------------------------+
| Original CS |
| Original LIP | Hardware overwrites this slot
| Original SSP |
+-------------------------------+
| Inner CS |
| Inner LIP | Copied from first NMI
| Inner SSP |
+-------------------------------+
| Outer CS |
| Outer LIP | For the IRET from first NMI
| Outer SSP | (BUILD_NMI_REPEAT_SHSTK_FRAME)
+-------------------------------+
At the beginning, all three copies are same when it is established by
the first NMI. A nested NMI, will point outer SSP to Inner SSP's slot,
sets CS to, __KERNEL_CS and modify the LIP to jump to repeat_nmi just
like it populates the outer IRET frame on the stack.
Since the replication is consistent, the address of the outer frame is
known beforehand. IRET of first NMI is supposed to consume the outer
frame.
If there was nesting, repeat_nmi will rebuild the outer frame it
consumed during IRET in BUILD_NMI_REPEAT_SHSTK_FRAME and continue. Any
nested NMI at this point will simply take the nested_nmi_out path
without modifying anything on the shadow stack.
If there is no nested NMI, the outer frame, which is a replica of the
hardware populated frame, will transfer the control back into the
kernel.
This is super convoluted like the stack handling and severely unbalances
the RSB.
Signed-off-by: K Prateek Nayak <kprateek.nayak@xxxxxxx>
---
arch/x86/entry/entry_64.S | 232 ++++++++++++++++++++++++++++++++++++++
1 file changed, 232 insertions(+)
diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
index 53ea96470f15..5a25c629a4d2 100644
--- a/arch/x86/entry/entry_64.S
+++ b/arch/x86/entry/entry_64.S
@@ -1145,6 +1145,214 @@ SYM_CODE_START_LOCAL(error_return)
jmp swapgs_restore_regs_and_return_to_usermode
SYM_CODE_END(error_return)
+#ifdef CONFIG_X86_KERN_SHADOW_STACK
+
+/*
+ * This allocates three dummy slots on the shadow stack frame
+ * and copies the previous top 3 slots on to the newly
+ * created dummy slots.
+ */
+.macro BUILD_NMI_REPEAT_SHSTK_FRAME needs_scratch=1
+ /* Save the scratch that will be clobbered. */
+ .if \needs_scratch
+ pushq %rax /* Extra scratch. */
+ pushq %rdx
+ .endif
+
+ xor %rdx, %rdx
+ rdsspq %rdx
+ testq %rdx, %rdx
+ jz .Lskip_shstk\@
+
+ /* Replicate top 3 frames below. */
+ .rept 3
+ ANNOTATE_INTRA_FUNCTION_CALL
+ call 7771f
+ int3
+7771:
+ movq 16(%rdx), %rax
+ wrssq %rax, -8(%rdx)
+ leaq -8(%rdx), %rdx
+ .endr
+ addq $24, %rsp
+.Lskip_shstk\@:
+
+ /* Restore scratch. */
+ .if \needs_scratch
+ popq %rdx
+ popq %rax
+ .endif
+.endm
+
+/*
+ * Nested NMI Shadow Stack handling
+ *
+ * On IDT systems, the hardware asserts the IST Gate-1 token and pushes
+ * a { CS, LIP, SS } package onto the shadow stack. Until the token is
+ * occupied, a nested NMI will #GP.
+ *
+ * We mark the shadow stack non-busy and use rstorssp to switch back to
+ * the same shadow stack. Then, like NMI handler does on regular stack,
+ * we construct a similar inner frame, outer frame structure, on the
+ * shadow stack:
+ *
+ * +-------------------------------+
+ * | supervisor token |
+ * +-------------------------------+
+ * | Original CS |
+ * | Original LIP | Hardware overwrites this slot
+ * | Original SSP |
+ * +-------------------------------+
+ * | Inner CS |
+ * | Inner LIP | Copied from first NMI
+ * | Inner SSP |
+ * +-------------------------------+
+ * | Outer CS |
+ * | Outer LIP | For the IRET from first NMI
+ * | Outer SSP | (BUILD_NMI_REPEAT_SHSTK_FRAME)
+ * +-------------------------------+
+ *
+ * At the beginning, all three copies are same when first NMI arrives.
+ * A nested NMI, will point outer SSP to Inner SSP's slot, set CS to,
+ * __KERNEL_CS and modify the LIP to jump to repeat_nmi just like it
+ * populates the stack frame.
+ *
+ * repeat_nmi will rebuild the outer frame it consumed in
+ * BUILD_NMI_REPEAT_SHSTK_FRAME and continue. Any nested NMI at this
+ * point will simply take the nested_nmi_out path to jump out.
+ *
+ * If there is no other NMI in-between, the outer frame is same as
+ * hardware populated one and the transfer jumps back at IRET.
+ *
+ * Note: RSB is severely unbalanced by the time we exit here.
+ */
+.macro INIT_NMI_SHSTK_STATE
+ xor %rdx, %rdx
+ rdsspq %rdx
+ testq %rdx, %rdx
+ jz .Lskip_shstk_init\@
+
+ /* Write a restore token. */
+ pushq %rax /* Extra scratch. */
+ movq %rdx, %rax
+ orq $1, %rax
+ wrssq %rax, -8(%rdx)
+
+ /*
+ * Supervisor token is 3 slots up, at the IST SSP.
+ * Clear the busy bit to allow nesting.
+ * NMIs are still disabled.
+ */
+ clrssbsy 24(%rdx)
+
+ /* Switch back to the same shadow stack */
+ rstorssp -8(%rdx)
+ movq $1, %rdx
+ incsspq %rdx
+
+ /*
+ * Build the original copy. first_nmi falls through into
+ * repeat_nmi, which builds the outer copy from it.
+ */
+ BUILD_NMI_REPEAT_SHSTK_FRAME needs_scratch=0
+
+ popq %rax /* Restore scratch. */
+
+.Lskip_shstk_init\@:
+.endm
+
+/*
+ * For CONFIG_DEBUG_ENTRY build a shadow stack frame that
+ * IRET-to-self can pop out.
+ */
+.macro BUILD_NMI_DEBUG_SHSTK_FRAME
+ pushq %rax /* Extra scratch. */
+ pushq %rdx
+
+ xor %eax, %eax
+ rdsspq %rax
+ testq %rax, %rax
+ jz .Lskip_shstk_debug\@
+
+ .rept 3
+ ANNOTATE_INTRA_FUNCTION_CALL
+ call 7772f
+ int3
+7772:
+ .endr
+ addq $24, %rsp
+
+ rdsspq %rdx
+ wrssq %rax, (%rdx) /* SSP */
+ movq 16(%rsp), %rax
+ wrssq %rax, 8(%rdx) /* LIP */
+ movq 24(%rsp), %rax
+ wrssq %rax, 16(%rdx) /* CS */
+
+.Lskip_shstk_debug\@:
+ popq %rdx
+ popq %rax
+.endm
+
+/*
+ * Fixup the outer frame to jump to "repeat_nmi" and point the
+ * CS, SSP to correct location to jump back to the inner copy
+ * 3 slots down the same shadow stack.
+ */
+.macro FIX_NESTED_NMI_SHSTK_FRAME
+ xor %rdx, %rdx
+ rdsspq %rdx
+ testq %rdx, %rdx
+ jz .Lskip_shstk_fix\@
+
+ pushq %rax
+
+ /*
+ * %rdx is S0-24. Redirect the outer copy (S0-56..S0-72) to
+ * repeat_nmi, resuming on top of the original copy (S0-48).
+ */
+ movq $__KERNEL_CS, %rax
+ wrssq %rax, -32(%rdx) /* CS */
+
+ movabsq $repeat_nmi, %rax
+ wrssq %rax, -40(%rdx) /* LIP */
+
+ leaq -24(%rdx), %rax
+ wrssq %rax, -48(%rdx) /* SSP */
+
+ popq %rax
+.Lskip_shstk_fix\@:
+.endm
+
+/*
+ * NMI from userspace uses the thread's stack.
+ *
+ * Set NMI shadow stack as non-busy to accommodate nested NMIs and
+ * switch to PL0_SSP stack. Simce this comes from userspace, the
+ * SSP0 stack is currently unused.
+ */
+.macro SWITCH_TO_TASK_SHSTK
+ rdsspq %rdx
+ clrssbsy (%rdx)
+ setssbsy
+.endm
+
+#else
+
+.macro BUILD_NMI_REPEAT_SHSTK_FRAME
+.endm
+.macro INIT_NMI_SHSTK_STATE
+.endm
+.macro BUILD_NMI_DEBUG_SHSTK_FRAME
+.endm
+.macro FIX_NESTED_NMI_SHSTK_FRAME
+.endm
+.macro SWITCH_TO_TASK_SHSTK
+.endm
+
+#endif
+
+
/*
* Runs on exception stack. Xen PV does not go through this path at all,
* so we can use real assembly here.
@@ -1231,6 +1439,13 @@ SYM_CODE_START(asm_exc_nmi)
PUSH_AND_CLEAR_REGS rdx=(%rdx)
ENCODE_FRAME_POINTER
+ /*
+ * Like the data stack, leave the NMI IST shadow stack so a nested
+ * NMI can claim its token, and run on the task's shadow stack.
+ * The IRET to user frees the task token again.
+ */
+ ALTERNATIVE "", __stringify(SWITCH_TO_TASK_SHSTK), X86_FEATURE_KERN_SHSTK
+
IBRS_ENTER
UNTRAIN_RET
@@ -1354,6 +1569,14 @@ nested_nmi:
* iteration of NMI handling.
*/
subq $8, %rsp
+
+ /*
+ * Fix needs to be done here since we have
+ * 5 spare stack frame we can use that gets
+ * overwritten by the later push.
+ */
+ FIX_NESTED_NMI_SHSTK_FRAME
+
leaq -10*8(%rsp), %rdx
pushq $__KERNEL_DS
pushq %rdx
@@ -1371,6 +1594,9 @@ nested_nmi_out:
iretq
first_nmi:
+
+ INIT_NMI_SHSTK_STATE
+
/* Restore rdx. */
movq (%rsp), %rdx
@@ -1399,6 +1625,9 @@ first_nmi:
pushfq /* RFLAGS */
pushq $__KERNEL_CS /* CS */
pushq $1f /* RIP */
+
+ BUILD_NMI_DEBUG_SHSTK_FRAME
+
iretq /* continues at repeat_nmi below */
UNWIND_HINT_IRET_REGS
1:
@@ -1433,6 +1662,9 @@ repeat_nmi:
pushq -6*8(%rsp)
.endr
subq $(5*8), %rsp
+
+ BUILD_NMI_REPEAT_SHSTK_FRAME
+
end_repeat_nmi:
ANNOTATE_NOENDBR // this code
--
2.43.0