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