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 DEFINE_PER_CPU(unsigned long, fred_rsp0);
25 EXPORT_PER_CPU_SYMBOL(fred_rsp0);
26
cpu_init_fred_exceptions(void)27 void cpu_init_fred_exceptions(void)
28 {
29 /* When FRED is enabled by default, remove this log message */
30 pr_info("Initialize FRED on CPU%d\n", smp_processor_id());
31
32 /*
33 * If a kernel event is delivered before a CPU goes to user level for
34 * the first time, its SS is NULL thus NULL is pushed into the SS field
35 * of the FRED stack frame. But before ERETS is executed, the CPU may
36 * context switch to another task and go to user level. Then when the
37 * CPU comes back to kernel mode, SS is changed to __KERNEL_DS. Later
38 * when ERETS is executed to return from the kernel event handler, a #GP
39 * fault is generated because SS doesn't match the SS saved in the FRED
40 * stack frame.
41 *
42 * Initialize SS to __KERNEL_DS when enabling FRED to avoid such #GPs.
43 */
44 loadsegment(ss, __KERNEL_DS);
45
46 wrmsrl(MSR_IA32_FRED_CONFIG,
47 /* Reserve for CALL emulation */
48 FRED_CONFIG_REDZONE |
49 FRED_CONFIG_INT_STKLVL(0) |
50 FRED_CONFIG_ENTRYPOINT(asm_fred_entrypoint_user));
51
52 wrmsrl(MSR_IA32_FRED_STKLVLS, 0);
53
54 /*
55 * Ater a CPU offline/online cycle, the FRED RSP0 MSR should be
56 * resynchronized with its per-CPU cache.
57 */
58 wrmsrl(MSR_IA32_FRED_RSP0, __this_cpu_read(fred_rsp0));
59
60 wrmsrl(MSR_IA32_FRED_RSP1, 0);
61 wrmsrl(MSR_IA32_FRED_RSP2, 0);
62 wrmsrl(MSR_IA32_FRED_RSP3, 0);
63
64 /* Enable FRED */
65 cr4_set_bits(X86_CR4_FRED);
66 /* Any further IDT use is a bug */
67 idt_invalidate();
68
69 /* Use int $0x80 for 32-bit system calls in FRED mode */
70 setup_clear_cpu_cap(X86_FEATURE_SYSENTER32);
71 setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
72 }
73
74 /* Must be called after setup_cpu_entry_areas() */
cpu_init_fred_rsps(void)75 void cpu_init_fred_rsps(void)
76 {
77 /*
78 * The purpose of separate stacks for NMI, #DB and #MC *in the kernel*
79 * (remember that user space faults are always taken on stack level 0)
80 * is to avoid overflowing the kernel stack.
81 */
82 wrmsrl(MSR_IA32_FRED_STKLVLS,
83 FRED_STKLVL(X86_TRAP_DB, FRED_DB_STACK_LEVEL) |
84 FRED_STKLVL(X86_TRAP_NMI, FRED_NMI_STACK_LEVEL) |
85 FRED_STKLVL(X86_TRAP_MC, FRED_MC_STACK_LEVEL) |
86 FRED_STKLVL(X86_TRAP_DF, FRED_DF_STACK_LEVEL));
87
88 /* The FRED equivalents to IST stacks... */
89 wrmsrl(MSR_IA32_FRED_RSP1, __this_cpu_ist_top_va(DB));
90 wrmsrl(MSR_IA32_FRED_RSP2, __this_cpu_ist_top_va(NMI));
91 wrmsrl(MSR_IA32_FRED_RSP3, __this_cpu_ist_top_va(DF));
92 }
93