x86/fred: Add FRED initialization functions
authorH. Peter Anvin (Intel) <hpa@zytor.com>
Tue, 5 Dec 2023 10:50:23 +0000 (02:50 -0800)
committerBorislav Petkov (AMD) <bp@alien8.de>
Wed, 31 Jan 2024 21:03:32 +0000 (22:03 +0100)
Add cpu_init_fred_exceptions() to:
  - Set FRED entrypoints for events happening in ring 0 and 3.
  - Specify the stack level for IRQs occurred ring 0.
  - Specify dedicated event stacks for #DB/NMI/#MCE/#DF.
  - Enable FRED and invalidtes IDT.
  - Force 32-bit system calls to use "int $0x80" only.

Add fred_complete_exception_setup() to:
  - Initialize system_vectors as done for IDT systems.
  - Set unused sysvec_table entries to fred_handle_spurious_interrupt().

Co-developed-by: Xin Li <xin3.li@intel.com>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
Signed-off-by: Xin Li <xin3.li@intel.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Tested-by: Shan Kang <shan.kang@intel.com>
Link: https://lore.kernel.org/r/20231205105030.8698-35-xin3.li@intel.com
arch/x86/entry/entry_fred.c
arch/x86/include/asm/fred.h
arch/x86/kernel/Makefile
arch/x86/kernel/fred.c [new file with mode: 0644]

index 6ecc08b6d72a24b2f24dca95279678e67e55868f..ac120cbdaaf2b4c474954c9a9f148222a370a72a 100644 (file)
@@ -133,6 +133,27 @@ void __init fred_install_sysvec(unsigned int sysvec, idtentry_t handler)
                 sysvec_table[sysvec - FIRST_SYSTEM_VECTOR] = handler;
 }
 
+static noinstr void fred_handle_spurious_interrupt(struct pt_regs *regs)
+{
+       spurious_interrupt(regs, regs->fred_ss.vector);
+}
+
+void __init fred_complete_exception_setup(void)
+{
+       unsigned int vector;
+
+       for (vector = 0; vector < FIRST_EXTERNAL_VECTOR; vector++)
+               set_bit(vector, system_vectors);
+
+       for (vector = 0; vector < NR_SYSTEM_VECTORS; vector++) {
+               if (sysvec_table[vector])
+                       set_bit(vector + FIRST_SYSTEM_VECTOR, system_vectors);
+               else
+                       sysvec_table[vector] = fred_handle_spurious_interrupt;
+       }
+       fred_setup_done = true;
+}
+
 static noinstr void fred_extint(struct pt_regs *regs)
 {
        unsigned int vector = regs->fred_ss.vector;
index 2fa9f34e5c95f5c18db8e8181d6521c502c022f6..e86c7ba32435f5a6956ebab09656c639a21ec53f 100644 (file)
@@ -83,8 +83,13 @@ static __always_inline void fred_entry_from_kvm(unsigned int type, unsigned int
        asm_fred_entry_from_kvm(ss);
 }
 
+void cpu_init_fred_exceptions(void);
+void fred_complete_exception_setup(void);
+
 #else /* CONFIG_X86_FRED */
 static __always_inline unsigned long fred_event_data(struct pt_regs *regs) { return 0; }
+static inline void cpu_init_fred_exceptions(void) { }
+static inline void fred_complete_exception_setup(void) { }
 static __always_inline void fred_entry_from_kvm(unsigned int type, unsigned int vector) { }
 #endif /* CONFIG_X86_FRED */
 #endif /* !__ASSEMBLY__ */
index 0000325ab98f4dc7408d0f338aab42bf6850c367..0dcbfc1a4c419fc3fdbf7c62dcbdee6283658eb2 100644 (file)
@@ -48,6 +48,7 @@ obj-y                 += platform-quirks.o
 obj-y                  += process_$(BITS).o signal.o signal_$(BITS).o
 obj-y                  += traps.o idt.o irq.o irq_$(BITS).o dumpstack_$(BITS).o
 obj-y                  += time.o ioport.o dumpstack.o nmi.o
+obj-$(CONFIG_X86_FRED) += fred.o
 obj-$(CONFIG_MODIFY_LDT_SYSCALL)       += ldt.o
 obj-$(CONFIG_X86_KERNEL_IBT)           += ibt_selftest.o
 obj-y                  += setup.o x86_init.o i8259.o irqinit.o
diff --git a/arch/x86/kernel/fred.c b/arch/x86/kernel/fred.c
new file mode 100644 (file)
index 0000000..4bcd879
--- /dev/null
@@ -0,0 +1,59 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#include <linux/kernel.h>
+
+#include <asm/desc.h>
+#include <asm/fred.h>
+#include <asm/tlbflush.h>
+#include <asm/traps.h>
+
+/* #DB in the kernel would imply the use of a kernel debugger. */
+#define FRED_DB_STACK_LEVEL            1UL
+#define FRED_NMI_STACK_LEVEL           2UL
+#define FRED_MC_STACK_LEVEL            2UL
+/*
+ * #DF is the highest level because a #DF means "something went wrong
+ * *while delivering an exception*." The number of cases for which that
+ * can happen with FRED is drastically reduced and basically amounts to
+ * "the stack you pointed me to is broken." Thus, always change stacks
+ * on #DF, which means it should be at the highest level.
+ */
+#define FRED_DF_STACK_LEVEL            3UL
+
+#define FRED_STKLVL(vector, lvl)       ((lvl) << (2 * (vector)))
+
+void cpu_init_fred_exceptions(void)
+{
+       /* When FRED is enabled by default, remove this log message */
+       pr_info("Initialize FRED on CPU%d\n", smp_processor_id());
+
+       wrmsrl(MSR_IA32_FRED_CONFIG,
+              /* Reserve for CALL emulation */
+              FRED_CONFIG_REDZONE |
+              FRED_CONFIG_INT_STKLVL(0) |
+              FRED_CONFIG_ENTRYPOINT(asm_fred_entrypoint_user));
+
+       /*
+        * The purpose of separate stacks for NMI, #DB and #MC *in the kernel*
+        * (remember that user space faults are always taken on stack level 0)
+        * is to avoid overflowing the kernel stack.
+        */
+       wrmsrl(MSR_IA32_FRED_STKLVLS,
+              FRED_STKLVL(X86_TRAP_DB,  FRED_DB_STACK_LEVEL) |
+              FRED_STKLVL(X86_TRAP_NMI, FRED_NMI_STACK_LEVEL) |
+              FRED_STKLVL(X86_TRAP_MC,  FRED_MC_STACK_LEVEL) |
+              FRED_STKLVL(X86_TRAP_DF,  FRED_DF_STACK_LEVEL));
+
+       /* The FRED equivalents to IST stacks... */
+       wrmsrl(MSR_IA32_FRED_RSP1, __this_cpu_ist_top_va(DB));
+       wrmsrl(MSR_IA32_FRED_RSP2, __this_cpu_ist_top_va(NMI));
+       wrmsrl(MSR_IA32_FRED_RSP3, __this_cpu_ist_top_va(DF));
+
+       /* Enable FRED */
+       cr4_set_bits(X86_CR4_FRED);
+       /* Any further IDT use is a bug */
+       idt_invalidate();
+
+       /* Use int $0x80 for 32-bit system calls in FRED mode */
+       setup_clear_cpu_cap(X86_FEATURE_SYSENTER32);
+       setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
+}