xref: /linux/arch/x86/kernel/fred.c (revision aec3202247b4ab41c5bf3b9f704a2d9a323a051b)
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/msr.h>
7 #include <asm/tlbflush.h>
8 #include <asm/traps.h>
9 
10 /* #DB in the kernel would imply the use of a kernel debugger. */
11 #define FRED_DB_STACK_LEVEL		1UL
12 #define FRED_NMI_STACK_LEVEL		2UL
13 #define FRED_MC_STACK_LEVEL		2UL
14 /*
15  * #DF is the highest level because a #DF means "something went wrong
16  * *while delivering an exception*." The number of cases for which that
17  * can happen with FRED is drastically reduced and basically amounts to
18  * "the stack you pointed me to is broken." Thus, always change stacks
19  * on #DF, which means it should be at the highest level.
20  */
21 #define FRED_DF_STACK_LEVEL		3UL
22 
23 #define FRED_STKLVL(vector, lvl)	((lvl) << (2 * (vector)))
24 
25 DEFINE_PER_CPU(unsigned long, fred_rsp0);
26 EXPORT_PER_CPU_SYMBOL(fred_rsp0);
27 
28 void cpu_init_fred_exceptions(void)
29 {
30 	/*
31 	 * If a kernel event is delivered before a CPU goes to user level for
32 	 * the first time, its SS is NULL thus NULL is pushed into the SS field
33 	 * of the FRED stack frame.  But before ERETS is executed, the CPU may
34 	 * context switch to another task and go to user level.  Then when the
35 	 * CPU comes back to kernel mode, SS is changed to __KERNEL_DS.  Later
36 	 * when ERETS is executed to return from the kernel event handler, a #GP
37 	 * fault is generated because SS doesn't match the SS saved in the FRED
38 	 * stack frame.
39 	 *
40 	 * Initialize SS to __KERNEL_DS when enabling FRED to avoid such #GPs.
41 	 */
42 	loadsegment(ss, __KERNEL_DS);
43 
44 	wrmsrq(MSR_IA32_FRED_CONFIG,
45 	       /* Reserve for CALL emulation */
46 	       FRED_CONFIG_REDZONE |
47 	       FRED_CONFIG_INT_STKLVL(0) |
48 	       FRED_CONFIG_ENTRYPOINT(asm_fred_entrypoint_user));
49 
50 	wrmsrq(MSR_IA32_FRED_STKLVLS, 0);
51 
52 	/*
53 	 * Ater a CPU offline/online cycle, the FRED RSP0 MSR should be
54 	 * resynchronized with its per-CPU cache.
55 	 */
56 	wrmsrq(MSR_IA32_FRED_RSP0, __this_cpu_read(fred_rsp0));
57 
58 	wrmsrq(MSR_IA32_FRED_RSP1, 0);
59 	wrmsrq(MSR_IA32_FRED_RSP2, 0);
60 	wrmsrq(MSR_IA32_FRED_RSP3, 0);
61 
62 	/* Enable FRED */
63 	cr4_set_bits(X86_CR4_FRED);
64 	/* Any further IDT use is a bug */
65 	idt_invalidate();
66 
67 	/* Use int $0x80 for 32-bit system calls in FRED mode */
68 	setup_clear_cpu_cap(X86_FEATURE_SYSFAST32);
69 	setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
70 }
71 
72 /* Must be called after setup_cpu_entry_areas() */
73 void cpu_init_fred_rsps(void)
74 {
75 	/*
76 	 * The purpose of separate stacks for NMI, #DB and #MC *in the kernel*
77 	 * (remember that user space faults are always taken on stack level 0)
78 	 * is to avoid overflowing the kernel stack.
79 	 */
80 	wrmsrq(MSR_IA32_FRED_STKLVLS,
81 	       FRED_STKLVL(X86_TRAP_DB,  FRED_DB_STACK_LEVEL) |
82 	       FRED_STKLVL(X86_TRAP_NMI, FRED_NMI_STACK_LEVEL) |
83 	       FRED_STKLVL(X86_TRAP_MC,  FRED_MC_STACK_LEVEL) |
84 	       FRED_STKLVL(X86_TRAP_DF,  FRED_DF_STACK_LEVEL));
85 
86 	/* The FRED equivalents to IST stacks... */
87 	wrmsrq(MSR_IA32_FRED_RSP1, __this_cpu_ist_top_va(DB));
88 	wrmsrq(MSR_IA32_FRED_RSP2, __this_cpu_ist_top_va(NMI));
89 	wrmsrq(MSR_IA32_FRED_RSP3, __this_cpu_ist_top_va(DF));
90 }
91