xref: /linux/arch/x86/kernel/fred.c (revision a4a755c422242c27cb0f7900ac00cf33ac17b1ce)
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 }
60