Skip to content

Commit cdd99dd

Browse files
H. Peter Anvin (Intel)bp3tk0v
authored andcommitted
x86/fred: Add FRED initialization functions
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
1 parent 530dce2 commit cdd99dd

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

arch/x86/entry/entry_fred.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,27 @@ void __init fred_install_sysvec(unsigned int sysvec, idtentry_t handler)
133133
sysvec_table[sysvec - FIRST_SYSTEM_VECTOR] = handler;
134134
}
135135

136+
static noinstr void fred_handle_spurious_interrupt(struct pt_regs *regs)
137+
{
138+
spurious_interrupt(regs, regs->fred_ss.vector);
139+
}
140+
141+
void __init fred_complete_exception_setup(void)
142+
{
143+
unsigned int vector;
144+
145+
for (vector = 0; vector < FIRST_EXTERNAL_VECTOR; vector++)
146+
set_bit(vector, system_vectors);
147+
148+
for (vector = 0; vector < NR_SYSTEM_VECTORS; vector++) {
149+
if (sysvec_table[vector])
150+
set_bit(vector + FIRST_SYSTEM_VECTOR, system_vectors);
151+
else
152+
sysvec_table[vector] = fred_handle_spurious_interrupt;
153+
}
154+
fred_setup_done = true;
155+
}
156+
136157
static noinstr void fred_extint(struct pt_regs *regs)
137158
{
138159
unsigned int vector = regs->fred_ss.vector;

arch/x86/include/asm/fred.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,13 @@ static __always_inline void fred_entry_from_kvm(unsigned int type, unsigned int
8383
asm_fred_entry_from_kvm(ss);
8484
}
8585

86+
void cpu_init_fred_exceptions(void);
87+
void fred_complete_exception_setup(void);
88+
8689
#else /* CONFIG_X86_FRED */
8790
static __always_inline unsigned long fred_event_data(struct pt_regs *regs) { return 0; }
91+
static inline void cpu_init_fred_exceptions(void) { }
92+
static inline void fred_complete_exception_setup(void) { }
8893
static __always_inline void fred_entry_from_kvm(unsigned int type, unsigned int vector) { }
8994
#endif /* CONFIG_X86_FRED */
9095
#endif /* !__ASSEMBLY__ */

arch/x86/kernel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ obj-y += platform-quirks.o
4848
obj-y += process_$(BITS).o signal.o signal_$(BITS).o
4949
obj-y += traps.o idt.o irq.o irq_$(BITS).o dumpstack_$(BITS).o
5050
obj-y += time.o ioport.o dumpstack.o nmi.o
51+
obj-$(CONFIG_X86_FRED) += fred.o
5152
obj-$(CONFIG_MODIFY_LDT_SYSCALL) += ldt.o
5253
obj-$(CONFIG_X86_KERNEL_IBT) += ibt_selftest.o
5354
obj-y += setup.o x86_init.o i8259.o irqinit.o

arch/x86/kernel/fred.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#include <linux/kernel.h>
3+
4+
#include <asm/desc.h>
5+
#include <asm/fred.h>
6+
#include <asm/tlbflush.h>
7+
#include <asm/traps.h>
8+
9+
/* #DB in the kernel would imply the use of a kernel debugger. */
10+
#define FRED_DB_STACK_LEVEL 1UL
11+
#define FRED_NMI_STACK_LEVEL 2UL
12+
#define FRED_MC_STACK_LEVEL 2UL
13+
/*
14+
* #DF is the highest level because a #DF means "something went wrong
15+
* *while delivering an exception*." The number of cases for which that
16+
* can happen with FRED is drastically reduced and basically amounts to
17+
* "the stack you pointed me to is broken." Thus, always change stacks
18+
* on #DF, which means it should be at the highest level.
19+
*/
20+
#define FRED_DF_STACK_LEVEL 3UL
21+
22+
#define FRED_STKLVL(vector, lvl) ((lvl) << (2 * (vector)))
23+
24+
void cpu_init_fred_exceptions(void)
25+
{
26+
/* When FRED is enabled by default, remove this log message */
27+
pr_info("Initialize FRED on CPU%d\n", smp_processor_id());
28+
29+
wrmsrl(MSR_IA32_FRED_CONFIG,
30+
/* Reserve for CALL emulation */
31+
FRED_CONFIG_REDZONE |
32+
FRED_CONFIG_INT_STKLVL(0) |
33+
FRED_CONFIG_ENTRYPOINT(asm_fred_entrypoint_user));
34+
35+
/*
36+
* The purpose of separate stacks for NMI, #DB and #MC *in the kernel*
37+
* (remember that user space faults are always taken on stack level 0)
38+
* is to avoid overflowing the kernel stack.
39+
*/
40+
wrmsrl(MSR_IA32_FRED_STKLVLS,
41+
FRED_STKLVL(X86_TRAP_DB, FRED_DB_STACK_LEVEL) |
42+
FRED_STKLVL(X86_TRAP_NMI, FRED_NMI_STACK_LEVEL) |
43+
FRED_STKLVL(X86_TRAP_MC, FRED_MC_STACK_LEVEL) |
44+
FRED_STKLVL(X86_TRAP_DF, FRED_DF_STACK_LEVEL));
45+
46+
/* The FRED equivalents to IST stacks... */
47+
wrmsrl(MSR_IA32_FRED_RSP1, __this_cpu_ist_top_va(DB));
48+
wrmsrl(MSR_IA32_FRED_RSP2, __this_cpu_ist_top_va(NMI));
49+
wrmsrl(MSR_IA32_FRED_RSP3, __this_cpu_ist_top_va(DF));
50+
51+
/* Enable FRED */
52+
cr4_set_bits(X86_CR4_FRED);
53+
/* Any further IDT use is a bug */
54+
idt_invalidate();
55+
56+
/* Use int $0x80 for 32-bit system calls in FRED mode */
57+
setup_clear_cpu_cap(X86_FEATURE_SYSENTER32);
58+
setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
59+
}

0 commit comments

Comments
 (0)