/* SPDX-License-Identifier: GPL-2.0 */1#include <linux/kernel.h>23#include <asm/desc.h>4#include <asm/fred.h>5#include <asm/msr.h>6#include <asm/tlbflush.h>7#include <asm/traps.h>89/* #DB in the kernel would imply the use of a kernel debugger. */10#define FRED_DB_STACK_LEVEL 1UL11#define FRED_NMI_STACK_LEVEL 2UL12#define FRED_MC_STACK_LEVEL 2UL13/*14* #DF is the highest level because a #DF means "something went wrong15* *while delivering an exception*." The number of cases for which that16* can happen with FRED is drastically reduced and basically amounts to17* "the stack you pointed me to is broken." Thus, always change stacks18* on #DF, which means it should be at the highest level.19*/20#define FRED_DF_STACK_LEVEL 3UL2122#define FRED_STKLVL(vector, lvl) ((lvl) << (2 * (vector)))2324DEFINE_PER_CPU(unsigned long, fred_rsp0);25EXPORT_PER_CPU_SYMBOL(fred_rsp0);2627void cpu_init_fred_exceptions(void)28{29/* When FRED is enabled by default, remove this log message */30pr_info("Initialize FRED on CPU%d\n", smp_processor_id());3132/*33* If a kernel event is delivered before a CPU goes to user level for34* the first time, its SS is NULL thus NULL is pushed into the SS field35* of the FRED stack frame. But before ERETS is executed, the CPU may36* context switch to another task and go to user level. Then when the37* CPU comes back to kernel mode, SS is changed to __KERNEL_DS. Later38* when ERETS is executed to return from the kernel event handler, a #GP39* fault is generated because SS doesn't match the SS saved in the FRED40* stack frame.41*42* Initialize SS to __KERNEL_DS when enabling FRED to avoid such #GPs.43*/44loadsegment(ss, __KERNEL_DS);4546wrmsrq(MSR_IA32_FRED_CONFIG,47/* Reserve for CALL emulation */48FRED_CONFIG_REDZONE |49FRED_CONFIG_INT_STKLVL(0) |50FRED_CONFIG_ENTRYPOINT(asm_fred_entrypoint_user));5152wrmsrq(MSR_IA32_FRED_STKLVLS, 0);5354/*55* Ater a CPU offline/online cycle, the FRED RSP0 MSR should be56* resynchronized with its per-CPU cache.57*/58wrmsrq(MSR_IA32_FRED_RSP0, __this_cpu_read(fred_rsp0));5960wrmsrq(MSR_IA32_FRED_RSP1, 0);61wrmsrq(MSR_IA32_FRED_RSP2, 0);62wrmsrq(MSR_IA32_FRED_RSP3, 0);6364/* Enable FRED */65cr4_set_bits(X86_CR4_FRED);66/* Any further IDT use is a bug */67idt_invalidate();6869/* Use int $0x80 for 32-bit system calls in FRED mode */70setup_clear_cpu_cap(X86_FEATURE_SYSENTER32);71setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);72}7374/* Must be called after setup_cpu_entry_areas() */75void 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*/82wrmsrq(MSR_IA32_FRED_STKLVLS,83FRED_STKLVL(X86_TRAP_DB, FRED_DB_STACK_LEVEL) |84FRED_STKLVL(X86_TRAP_NMI, FRED_NMI_STACK_LEVEL) |85FRED_STKLVL(X86_TRAP_MC, FRED_MC_STACK_LEVEL) |86FRED_STKLVL(X86_TRAP_DF, FRED_DF_STACK_LEVEL));8788/* The FRED equivalents to IST stacks... */89wrmsrq(MSR_IA32_FRED_RSP1, __this_cpu_ist_top_va(DB));90wrmsrq(MSR_IA32_FRED_RSP2, __this_cpu_ist_top_va(NMI));91wrmsrq(MSR_IA32_FRED_RSP3, __this_cpu_ist_top_va(DF));92}939495