From: K Prateek Nayak <kprateek.nayak@amd.com>
To: Dave Hansen <dave.hansen@linux.intel.com>,
Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
Borislav Petkov <bp@alien8.de>,
the arch/x86 maintainers <x86@kernel.org>,
<linux-kernel@vger.kernel.org>
Cc: Andy Lutomirski <luto@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Rick Edgecombe <rick.p.edgecombe@intel.com>,
Andrew Cooper <andrew.cooper3@citrix.com>,
Kees Cook <kees@kernel.org>,
Pawan Gupta <pawan.kumar.gupta@linux.intel.com>,
K Prateek Nayak <kprateek.nayak@amd.com>
Subject: [RFC PATCH 27/27] x86/entry: Handle shadow stack on nested NMI
Date: Sun, 29 Nov 2026 22:29:52 +0000 [thread overview]
Message-ID: <20261129222953.9124-28-kprateek.nayak@amd.com> (raw)
In-Reply-To: <20261129222953.9124-1-kprateek.nayak@amd.com>
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@amd.com>
---
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
next prev parent reply other threads:[~2026-11-29 22:38 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-29 22:29 [RFC PATCH 00/27] x86/shstk: More shadow stack enablement and path to IDT support K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 01/27] x86/entry: Parameterize kernel shadow stack handling at FRED entry K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 02/27] x86/perf_regs: Rename pt_regs_offset array to prevent build failure K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 03/27] x86/cet: Fix build for KERN_SHADOW_STACK and !USER_SHADOW_STACK K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 04/27] x86/cet: Make kernel shadow stack #CP fatal instead of livelocking K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 05/27] x86/sync_core: Don't use IRET-to-self with kernel shadow stacks K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 06/27] x86/process: Initialize and enable kernel shadow stack at boot K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 07/27] x86/entry: Handle int3 text patching with kernel shadow stack K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 08/27] x86/retpoline: Handle retpolies and thunks " K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 09/27] x86/ftrace: Prepare ftrace trampolines to handle " K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 10/27] x86/kprobes: Prepare optprobe trampoline to handle shadow stacks K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 11/27] x86/rethook: Prepare the rethook trampoline to handle shadow stack K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 12/27] x86/cet: Make regs_set_return_ip() helpers context aware K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 13/27] x86/cet: Introduce regs_set_return_trampoline() for rethook trampoline K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 14/27] x86/ftrace,livepatch: Use CET helpers for ftrace regs->ip and trampoline redirection K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 15/27] x86/rethook: Make the trampoline insert and return shadow stack aware K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 16/27] x86/error_inject: Use CET helpers to insert just_return_func trampoline K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 17/27] x86/kprobes: Use CET helpers in kprobe core to modify regs->ip K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 18/27] x86/bpf: Make BPF JIT aware of CET handling K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 19/27] x86/shstk: Remove profiling related dependencies for KERN_SHADOW_STACK K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 20/27] x86/shstk: Disable kenrel shadow stack when CPU is offlined K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 21/27] x86/shstsk: Allow suspend-resume and hibernation with kernel shadow stack K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 22/27] x86/shstk: Save and restore shadow stack pointer during kexec K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 23/27] x86/idt: Map MCE shadow stack and populate the INT_SPP table K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 24/27] x86/process: Initialize shadow stack for IDT systems K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 25/27] x86/entry: Prepare IDT entry for shadow stacks K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 26/27] x86/syscall: Toggle shadow stack busy on entry and exit from syscall K Prateek Nayak
2026-11-29 22:29 ` K Prateek Nayak [this message]
2026-11-29 23:00 ` [RFC PATCH 00/27] x86/shstk: More shadow stack enablement and path to IDT support Andrew Cooper
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261129222953.9124-28-kprateek.nayak@amd.com \
--to=kprateek.nayak@amd.com \
--cc=andrew.cooper3@citrix.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=kees@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=mingo@redhat.com \
--cc=pawan.kumar.gupta@linux.intel.com \
--cc=peterz@infradead.org \
--cc=rick.p.edgecombe@intel.com \
--cc=tglx@kernel.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®