FRED delivers events on one of four stack levels, and each level has its own stack pointer MSR. The mapping of stacks to levels is only implied by the MSR writes. Nothing in the code explicitly states it, or notes that #MC uses the NMI stack. Add a helper that makes the connection obvious and returns the corresponding stack pointer for each RSP MSR. Use it to program all four FRED RSP MSRs during CPU initialization. Key the helper on the MSR index rather than the stack level to keep it simple. Later, the same interface can easily be exported to KVM or extended for FRED shadow stack MSRs as well. No functional change intended. Signed-off-by: Sohil Mehta --- This is a standalone cleanup that was part of the FRED KVM series. Dave asked me to send things like this up separately for consideration: https://lore.kernel.org/lkml/1712c0cd-747b-4dce-8231-0c8d75225648@intel.com/ v2: - Reframe as an x86 cleanup. Drop the export, the KVM heavy changelog and the unused !CONFIG_X86_FRED stub (Dave). - Program the level 0 stack pointer MSR through the helper too, so that all four of them go through it. - Keep the helper static for now, since all the callers are file-local. v1: https://lore.kernel.org/lkml/20261124015840.2673975-1-sohil.mehta@intel.com/ --- arch/x86/kernel/fred.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/arch/x86/kernel/fred.c b/arch/x86/kernel/fred.c index 1c452a389a70..35f5c27c7198 100644 --- a/arch/x86/kernel/fred.c +++ b/arch/x86/kernel/fred.c @@ -25,6 +25,27 @@ DEFINE_PER_CPU(unsigned long, fred_rsp0); EXPORT_PER_CPU_SYMBOL(fred_rsp0); +static unsigned long this_cpu_fred_sp(u32 msr) +{ + switch (msr) { + case MSR_IA32_FRED_RSP0: + return __this_cpu_read(fred_rsp0); + case MSR_IA32_FRED_RSP1: + return __this_cpu_ist_top_va(ESTACK_DB); + case MSR_IA32_FRED_RSP2: + /* + * NMI and #MC both use the NMI stack so ESTACK_MCE is left + * unused on a FRED system. + */ + return __this_cpu_ist_top_va(ESTACK_NMI); + case MSR_IA32_FRED_RSP3: + return __this_cpu_ist_top_va(ESTACK_DF); + default: + WARN_ONCE(1, "Unexpected FRED stack pointer MSR 0x%x\n", msr); + return 0; + } +} + void cpu_init_fred_exceptions(void) { /* @@ -53,7 +74,7 @@ void cpu_init_fred_exceptions(void) * Ater a CPU offline/online cycle, the FRED RSP0 MSR should be * resynchronized with its per-CPU cache. */ - wrmsrq(MSR_IA32_FRED_RSP0, __this_cpu_read(fred_rsp0)); + wrmsrq(MSR_IA32_FRED_RSP0, this_cpu_fred_sp(MSR_IA32_FRED_RSP0)); wrmsrq(MSR_IA32_FRED_RSP1, 0); wrmsrq(MSR_IA32_FRED_RSP2, 0); @@ -84,7 +105,7 @@ void cpu_init_fred_rsps(void) FRED_STKLVL(X86_TRAP_DF, FRED_DF_STACK_LEVEL)); /* The FRED equivalents to IST stacks... */ - wrmsrq(MSR_IA32_FRED_RSP1, __this_cpu_ist_top_va(ESTACK_DB)); - wrmsrq(MSR_IA32_FRED_RSP2, __this_cpu_ist_top_va(ESTACK_NMI)); - wrmsrq(MSR_IA32_FRED_RSP3, __this_cpu_ist_top_va(ESTACK_DF)); + wrmsrq(MSR_IA32_FRED_RSP1, this_cpu_fred_sp(MSR_IA32_FRED_RSP1)); + wrmsrq(MSR_IA32_FRED_RSP2, this_cpu_fred_sp(MSR_IA32_FRED_RSP2)); + wrmsrq(MSR_IA32_FRED_RSP3, this_cpu_fred_sp(MSR_IA32_FRED_RSP3)); } base-commit: 135dbd0519cf7f72af120801ec7dbb79275a362d -- 2.43.0