]> Git Repo - J-linux.git/blob - arch/x86/kernel/fred.c
Merge tag 'vfs-6.13-rc7.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
[J-linux.git] / arch / x86 / kernel / fred.c
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
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         wrmsrl(MSR_IA32_FRED_RSP0, 0);
54         wrmsrl(MSR_IA32_FRED_RSP1, 0);
55         wrmsrl(MSR_IA32_FRED_RSP2, 0);
56         wrmsrl(MSR_IA32_FRED_RSP3, 0);
57
58         /* Enable FRED */
59         cr4_set_bits(X86_CR4_FRED);
60         /* Any further IDT use is a bug */
61         idt_invalidate();
62
63         /* Use int $0x80 for 32-bit system calls in FRED mode */
64         setup_clear_cpu_cap(X86_FEATURE_SYSENTER32);
65         setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
66 }
67
68 /* Must be called after setup_cpu_entry_areas() */
69 void cpu_init_fred_rsps(void)
70 {
71         /*
72          * The purpose of separate stacks for NMI, #DB and #MC *in the kernel*
73          * (remember that user space faults are always taken on stack level 0)
74          * is to avoid overflowing the kernel stack.
75          */
76         wrmsrl(MSR_IA32_FRED_STKLVLS,
77                FRED_STKLVL(X86_TRAP_DB,  FRED_DB_STACK_LEVEL) |
78                FRED_STKLVL(X86_TRAP_NMI, FRED_NMI_STACK_LEVEL) |
79                FRED_STKLVL(X86_TRAP_MC,  FRED_MC_STACK_LEVEL) |
80                FRED_STKLVL(X86_TRAP_DF,  FRED_DF_STACK_LEVEL));
81
82         /* The FRED equivalents to IST stacks... */
83         wrmsrl(MSR_IA32_FRED_RSP1, __this_cpu_ist_top_va(DB));
84         wrmsrl(MSR_IA32_FRED_RSP2, __this_cpu_ist_top_va(NMI));
85         wrmsrl(MSR_IA32_FRED_RSP3, __this_cpu_ist_top_va(DF));
86 }
This page took 0.031713 seconds and 4 git commands to generate.