mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 23/27] x86/idt: Map MCE shadow stack and populate the INT_SPP table
Date: Sun, 29 Nov 2026 22:29:48 +0000	[thread overview]
Message-ID: <20261129222953.9124-24-kprateek.nayak@amd.com> (raw)
In-Reply-To: <20261129222953.9124-1-kprateek.nayak@amd.com>

In preparation for IDT handling of shadow stacks, allocate the MCE
shadow stack too and map it similar to other shadow stack areas on IDT
init path.

IDT systems need a supervisor token to mark shadow stack busy at the top
entry. Use the R/W estacks alias to populate the entry corresponding to
the linear address of the top slot.

IDT systems additionally require populating the INT_SSP table that maps
the shadow stacks to IST gates. Initialize the SSP table in
MSR_IA32_INT_SSP_TAB before enabling shadow stacks.

Signed-off-by: K Prateek Nayak <kprateek.nayak@amd.com>
---
 arch/x86/include/asm/cpu.h            |  2 ++
 arch/x86/include/asm/cpu_entry_area.h |  2 ++
 arch/x86/include/asm/traps.h          |  1 +
 arch/x86/kernel/idt.c                 | 30 +++++++++++++++++++++++++++
 arch/x86/mm/cpu_entry_area.c          | 27 ++++++++++++++++++++++++
 5 files changed, 62 insertions(+)

diff --git a/arch/x86/include/asm/cpu.h b/arch/x86/include/asm/cpu.h
index e9b0907b1a2b..e8227cc1336b 100644
--- a/arch/x86/include/asm/cpu.h
+++ b/arch/x86/include/asm/cpu.h
@@ -9,6 +9,7 @@
 #include <linux/percpu.h>
 
 #include <asm/ibt.h>
+#include <asm/traps.h>
 #include <asm/msr-index.h>
 #include <asm/thread_info.h>
 #include <asm/special_insns.h>
@@ -150,6 +151,7 @@ static __always_inline void cpu_setup_kernel_shstk(bool boot_cpu)
 	BUG_ON(!tsk->thread.kshstk_ssp);
 	shstk_top = (unsigned long)tsk->thread.kshstk_ssp;
 
+	load_ssp_table();
 	wrmsrq(MSR_IA32_FRED_SSP0, shstk_top);
 	enable_kernel_shstk(shstk_top);
 }
diff --git a/arch/x86/include/asm/cpu_entry_area.h b/arch/x86/include/asm/cpu_entry_area.h
index 676feba17d27..014212159fe5 100644
--- a/arch/x86/include/asm/cpu_entry_area.h
+++ b/arch/x86/include/asm/cpu_entry_area.h
@@ -34,6 +34,7 @@
 	char	DB_shadow_stack[EXCEPTION_SHADOW_STKSZ];	\
 	char	DB_stack[EXCEPTION_STKSZ];			\
 	char	MCE_stack_guard[guardsize];			\
+	char	MCE_shadow_stack[EXCEPTION_SHADOW_STKSZ];	\
 	char	MCE_stack[EXCEPTION_STKSZ];			\
 	char	VC_stack_guard[guardsize];			\
 	char	VC_stack[optional_stack_size];			\
@@ -145,6 +146,7 @@ DECLARE_PER_CPU(struct cpu_entry_area *, cpu_entry_area);
 DECLARE_PER_CPU(struct cea_exception_stacks *, cea_exception_stacks);
 
 extern void setup_cpu_entry_areas(void);
+extern void init_idt_shadows_stacks(unsigned int cpu);
 extern void cea_set_pte(void *cea_vaddr, phys_addr_t pa, pgprot_t flags);
 
 extern struct cpu_entry_area *get_cpu_entry_area(int cpu);
diff --git a/arch/x86/include/asm/traps.h b/arch/x86/include/asm/traps.h
index 3f24cc472ce9..0f99535fe31f 100644
--- a/arch/x86/include/asm/traps.h
+++ b/arch/x86/include/asm/traps.h
@@ -19,6 +19,7 @@ asmlinkage __visible noinstr struct pt_regs *vc_switch_off_ist(struct pt_regs *e
 
 extern int ibt_selftest(void);
 extern int ibt_selftest_noendbr(void);
+extern void load_ssp_table(void);
 
 #ifdef CONFIG_X86_F00F_BUG
 /* For handling the FOOF bug */
diff --git a/arch/x86/kernel/idt.c b/arch/x86/kernel/idt.c
index 90a22e24a9eb..85aaf841f813 100644
--- a/arch/x86/kernel/idt.c
+++ b/arch/x86/kernel/idt.c
@@ -180,10 +180,40 @@ static struct desc_ptr idt_descr __ro_after_init = {
 	.address	= (unsigned long) idt_table,
 };
 
+#define INT_SSP_TABLE_SIZE	8
+
+struct int_ssp_table {
+	unsigned long	*int_ssp[INT_SSP_TABLE_SIZE];
+};
+
+static DEFINE_PER_CPU(struct int_ssp_table, ssp_table);
+
+#define load_int_ssp_table(_vec)								\
+	do {											\
+		unsigned long *__shstk = (unsigned long *)__this_cpu_shadow_top_va(_vec);	\
+		table->int_ssp[IST_INDEX_ ##_vec + 1] = __shstk - 1;				\
+	} while(0)
+
+void load_ssp_table(void)
+{
+	struct int_ssp_table *table = this_cpu_ptr(&ssp_table);
+
+	if (cpu_feature_enabled(X86_FEATURE_FRED))
+		return;
+
+	load_int_ssp_table(DB);
+	load_int_ssp_table(NMI);
+	load_int_ssp_table(DF);
+	load_int_ssp_table(MCE);
+
+	wrmsrq(MSR_IA32_INT_SSP_TAB, (u64)table->int_ssp);
+}
+
 void load_current_idt(void)
 {
 	lockdep_assert_irqs_disabled();
 	load_idt(&idt_descr);
+	init_idt_shadows_stacks(smp_processor_id());
 }
 
 #ifdef CONFIG_X86_F00F_BUG
diff --git a/arch/x86/mm/cpu_entry_area.c b/arch/x86/mm/cpu_entry_area.c
index 7c1f04e7651a..f182e2f82f12 100644
--- a/arch/x86/mm/cpu_entry_area.c
+++ b/arch/x86/mm/cpu_entry_area.c
@@ -167,6 +167,29 @@ static void __init percpu_setup_exception_stacks(unsigned int cpu)
 	cea_map_stack(DB,  shadow_stack, PAGE_KERNEL_SHSTK);
 	cea_map_stack(NMI, shadow_stack, PAGE_KERNEL_SHSTK);
 	cea_map_stack(DF,  shadow_stack, PAGE_KERNEL_SHSTK);
+	cea_map_stack(MCE, shadow_stack, PAGE_KERNEL_SHSTK);
+}
+
+#define shstk_get_top_slot(ptr, stack)				\
+	(unsigned long *)((u8*)(ptr)->stack## _shadow_stack +	\
+			  EXCEPTION_SHADOW_STKSZ) - 1
+
+#define cea_init_shadow_stack(name) do {						\
+		unsigned long *estacks_alias = shstk_get_top_slot(estacks, name);	\
+		unsigned long *cae_alias = shstk_get_top_slot(&cea->estacks, name);	\
+		*estacks_alias = (unsigned long)cae_alias;				\
+	} while (0)
+
+void init_idt_shadows_stacks(unsigned int cpu)
+{
+	struct exception_stacks *estacks = per_cpu_ptr(&exception_stacks, cpu);
+	struct cpu_entry_area *cea = get_cpu_entry_area(cpu);
+
+	/* Map the per-CPU exception shadow stacks: */
+	cea_init_shadow_stack(DB);
+	cea_init_shadow_stack(NMI);
+	cea_init_shadow_stack(DF);
+	cea_init_shadow_stack(MCE);
 }
 #else
 static void __init percpu_setup_exception_stacks(unsigned int cpu)
@@ -176,6 +199,10 @@ static void __init percpu_setup_exception_stacks(unsigned int cpu)
 	cea_map_percpu_pages(&cea->doublefault_stack,
 			     &per_cpu(doublefault_stack, cpu), 1, PAGE_KERNEL);
 }
+
+void init_idt_shadows_stacks(unsigned int cpu)
+{
+}
 #endif
 
 /* Setup the fixmap mappings only once per-processor */
-- 
2.43.0


  parent reply	other threads:[~2026-11-29 22:37 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 ` K Prateek Nayak [this message]
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 ` [RFC PATCH 27/27] x86/entry: Handle shadow stack on nested NMI K Prateek Nayak
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-24-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®