18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci#include <linux/extable.h>
38c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
48c2ecf20Sopenharmony_ci#include <linux/sched/debug.h>
58c2ecf20Sopenharmony_ci#include <xen/xen.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <asm/fpu/internal.h>
88c2ecf20Sopenharmony_ci#include <asm/sev-es.h>
98c2ecf20Sopenharmony_ci#include <asm/traps.h>
108c2ecf20Sopenharmony_ci#include <asm/kdebug.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_citypedef bool (*ex_handler_t)(const struct exception_table_entry *,
138c2ecf20Sopenharmony_ci			    struct pt_regs *, int, unsigned long,
148c2ecf20Sopenharmony_ci			    unsigned long);
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistatic inline unsigned long
178c2ecf20Sopenharmony_ciex_fixup_addr(const struct exception_table_entry *x)
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	return (unsigned long)&x->fixup + x->fixup;
208c2ecf20Sopenharmony_ci}
218c2ecf20Sopenharmony_cistatic inline ex_handler_t
228c2ecf20Sopenharmony_ciex_fixup_handler(const struct exception_table_entry *x)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	return (ex_handler_t)((unsigned long)&x->handler + x->handler);
258c2ecf20Sopenharmony_ci}
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci__visible bool ex_handler_default(const struct exception_table_entry *fixup,
288c2ecf20Sopenharmony_ci				  struct pt_regs *regs, int trapnr,
298c2ecf20Sopenharmony_ci				  unsigned long error_code,
308c2ecf20Sopenharmony_ci				  unsigned long fault_addr)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	regs->ip = ex_fixup_addr(fixup);
338c2ecf20Sopenharmony_ci	return true;
348c2ecf20Sopenharmony_ci}
358c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ex_handler_default);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci__visible bool ex_handler_fault(const struct exception_table_entry *fixup,
388c2ecf20Sopenharmony_ci				struct pt_regs *regs, int trapnr,
398c2ecf20Sopenharmony_ci				unsigned long error_code,
408c2ecf20Sopenharmony_ci				unsigned long fault_addr)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	regs->ip = ex_fixup_addr(fixup);
438c2ecf20Sopenharmony_ci	regs->ax = trapnr;
448c2ecf20Sopenharmony_ci	return true;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ex_handler_fault);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/*
498c2ecf20Sopenharmony_ci * Handler for when we fail to restore a task's FPU state.  We should never get
508c2ecf20Sopenharmony_ci * here because the FPU state of a task using the FPU (task->thread.fpu.state)
518c2ecf20Sopenharmony_ci * should always be valid.  However, past bugs have allowed userspace to set
528c2ecf20Sopenharmony_ci * reserved bits in the XSAVE area using PTRACE_SETREGSET or sys_rt_sigreturn().
538c2ecf20Sopenharmony_ci * These caused XRSTOR to fail when switching to the task, leaking the FPU
548c2ecf20Sopenharmony_ci * registers of the task previously executing on the CPU.  Mitigate this class
558c2ecf20Sopenharmony_ci * of vulnerability by restoring from the initial state (essentially, zeroing
568c2ecf20Sopenharmony_ci * out all the FPU registers) if we can't restore from the task's FPU state.
578c2ecf20Sopenharmony_ci */
588c2ecf20Sopenharmony_ci__visible bool ex_handler_fprestore(const struct exception_table_entry *fixup,
598c2ecf20Sopenharmony_ci				    struct pt_regs *regs, int trapnr,
608c2ecf20Sopenharmony_ci				    unsigned long error_code,
618c2ecf20Sopenharmony_ci				    unsigned long fault_addr)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	regs->ip = ex_fixup_addr(fixup);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	WARN_ONCE(1, "Bad FPU state detected at %pB, reinitializing FPU registers.",
668c2ecf20Sopenharmony_ci		  (void *)instruction_pointer(regs));
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	__copy_kernel_to_fpregs(&init_fpstate, -1);
698c2ecf20Sopenharmony_ci	return true;
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ex_handler_fprestore);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci__visible bool ex_handler_uaccess(const struct exception_table_entry *fixup,
748c2ecf20Sopenharmony_ci				  struct pt_regs *regs, int trapnr,
758c2ecf20Sopenharmony_ci				  unsigned long error_code,
768c2ecf20Sopenharmony_ci				  unsigned long fault_addr)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	WARN_ONCE(trapnr == X86_TRAP_GP, "General protection fault in user access. Non-canonical address?");
798c2ecf20Sopenharmony_ci	regs->ip = ex_fixup_addr(fixup);
808c2ecf20Sopenharmony_ci	return true;
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ex_handler_uaccess);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci__visible bool ex_handler_copy(const struct exception_table_entry *fixup,
858c2ecf20Sopenharmony_ci			       struct pt_regs *regs, int trapnr,
868c2ecf20Sopenharmony_ci			       unsigned long error_code,
878c2ecf20Sopenharmony_ci			       unsigned long fault_addr)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	WARN_ONCE(trapnr == X86_TRAP_GP, "General protection fault in user access. Non-canonical address?");
908c2ecf20Sopenharmony_ci	regs->ip = ex_fixup_addr(fixup);
918c2ecf20Sopenharmony_ci	regs->ax = trapnr;
928c2ecf20Sopenharmony_ci	return true;
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ex_handler_copy);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci__visible bool ex_handler_rdmsr_unsafe(const struct exception_table_entry *fixup,
978c2ecf20Sopenharmony_ci				       struct pt_regs *regs, int trapnr,
988c2ecf20Sopenharmony_ci				       unsigned long error_code,
998c2ecf20Sopenharmony_ci				       unsigned long fault_addr)
1008c2ecf20Sopenharmony_ci{
1018c2ecf20Sopenharmony_ci	if (pr_warn_once("unchecked MSR access error: RDMSR from 0x%x at rIP: 0x%lx (%pS)\n",
1028c2ecf20Sopenharmony_ci			 (unsigned int)regs->cx, regs->ip, (void *)regs->ip))
1038c2ecf20Sopenharmony_ci		show_stack_regs(regs);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	/* Pretend that the read succeeded and returned 0. */
1068c2ecf20Sopenharmony_ci	regs->ip = ex_fixup_addr(fixup);
1078c2ecf20Sopenharmony_ci	regs->ax = 0;
1088c2ecf20Sopenharmony_ci	regs->dx = 0;
1098c2ecf20Sopenharmony_ci	return true;
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ex_handler_rdmsr_unsafe);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci__visible bool ex_handler_wrmsr_unsafe(const struct exception_table_entry *fixup,
1148c2ecf20Sopenharmony_ci				       struct pt_regs *regs, int trapnr,
1158c2ecf20Sopenharmony_ci				       unsigned long error_code,
1168c2ecf20Sopenharmony_ci				       unsigned long fault_addr)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	if (pr_warn_once("unchecked MSR access error: WRMSR to 0x%x (tried to write 0x%08x%08x) at rIP: 0x%lx (%pS)\n",
1198c2ecf20Sopenharmony_ci			 (unsigned int)regs->cx, (unsigned int)regs->dx,
1208c2ecf20Sopenharmony_ci			 (unsigned int)regs->ax,  regs->ip, (void *)regs->ip))
1218c2ecf20Sopenharmony_ci		show_stack_regs(regs);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	/* Pretend that the write succeeded. */
1248c2ecf20Sopenharmony_ci	regs->ip = ex_fixup_addr(fixup);
1258c2ecf20Sopenharmony_ci	return true;
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ex_handler_wrmsr_unsafe);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci__visible bool ex_handler_clear_fs(const struct exception_table_entry *fixup,
1308c2ecf20Sopenharmony_ci				   struct pt_regs *regs, int trapnr,
1318c2ecf20Sopenharmony_ci				   unsigned long error_code,
1328c2ecf20Sopenharmony_ci				   unsigned long fault_addr)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	if (static_cpu_has(X86_BUG_NULL_SEG))
1358c2ecf20Sopenharmony_ci		asm volatile ("mov %0, %%fs" : : "rm" (__USER_DS));
1368c2ecf20Sopenharmony_ci	asm volatile ("mov %0, %%fs" : : "rm" (0));
1378c2ecf20Sopenharmony_ci	return ex_handler_default(fixup, regs, trapnr, error_code, fault_addr);
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ex_handler_clear_fs);
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cienum handler_type ex_get_fault_handler_type(unsigned long ip)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	const struct exception_table_entry *e;
1448c2ecf20Sopenharmony_ci	ex_handler_t handler;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	e = search_exception_tables(ip);
1478c2ecf20Sopenharmony_ci	if (!e)
1488c2ecf20Sopenharmony_ci		return EX_HANDLER_NONE;
1498c2ecf20Sopenharmony_ci	handler = ex_fixup_handler(e);
1508c2ecf20Sopenharmony_ci	if (handler == ex_handler_fault)
1518c2ecf20Sopenharmony_ci		return EX_HANDLER_FAULT;
1528c2ecf20Sopenharmony_ci	else if (handler == ex_handler_uaccess || handler == ex_handler_copy)
1538c2ecf20Sopenharmony_ci		return EX_HANDLER_UACCESS;
1548c2ecf20Sopenharmony_ci	else
1558c2ecf20Sopenharmony_ci		return EX_HANDLER_OTHER;
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ciint fixup_exception(struct pt_regs *regs, int trapnr, unsigned long error_code,
1598c2ecf20Sopenharmony_ci		    unsigned long fault_addr)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	const struct exception_table_entry *e;
1628c2ecf20Sopenharmony_ci	ex_handler_t handler;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci#ifdef CONFIG_PNPBIOS
1658c2ecf20Sopenharmony_ci	if (unlikely(SEGMENT_IS_PNP_CODE(regs->cs))) {
1668c2ecf20Sopenharmony_ci		extern u32 pnp_bios_fault_eip, pnp_bios_fault_esp;
1678c2ecf20Sopenharmony_ci		extern u32 pnp_bios_is_utter_crap;
1688c2ecf20Sopenharmony_ci		pnp_bios_is_utter_crap = 1;
1698c2ecf20Sopenharmony_ci		printk(KERN_CRIT "PNPBIOS fault.. attempting recovery.\n");
1708c2ecf20Sopenharmony_ci		__asm__ volatile(
1718c2ecf20Sopenharmony_ci			"movl %0, %%esp\n\t"
1728c2ecf20Sopenharmony_ci			"jmp *%1\n\t"
1738c2ecf20Sopenharmony_ci			: : "g" (pnp_bios_fault_esp), "g" (pnp_bios_fault_eip));
1748c2ecf20Sopenharmony_ci		panic("do_trap: can't hit this");
1758c2ecf20Sopenharmony_ci	}
1768c2ecf20Sopenharmony_ci#endif
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	e = search_exception_tables(regs->ip);
1798c2ecf20Sopenharmony_ci	if (!e)
1808c2ecf20Sopenharmony_ci		return 0;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	handler = ex_fixup_handler(e);
1838c2ecf20Sopenharmony_ci	return handler(e, regs, trapnr, error_code, fault_addr);
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ciextern unsigned int early_recursion_flag;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci/* Restricted version used during very early boot */
1898c2ecf20Sopenharmony_civoid __init early_fixup_exception(struct pt_regs *regs, int trapnr)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	/* Ignore early NMIs. */
1928c2ecf20Sopenharmony_ci	if (trapnr == X86_TRAP_NMI)
1938c2ecf20Sopenharmony_ci		return;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	if (early_recursion_flag > 2)
1968c2ecf20Sopenharmony_ci		goto halt_loop;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	/*
1998c2ecf20Sopenharmony_ci	 * Old CPUs leave the high bits of CS on the stack
2008c2ecf20Sopenharmony_ci	 * undefined.  I'm not sure which CPUs do this, but at least
2018c2ecf20Sopenharmony_ci	 * the 486 DX works this way.
2028c2ecf20Sopenharmony_ci	 * Xen pv domains are not using the default __KERNEL_CS.
2038c2ecf20Sopenharmony_ci	 */
2048c2ecf20Sopenharmony_ci	if (!xen_pv_domain() && regs->cs != __KERNEL_CS)
2058c2ecf20Sopenharmony_ci		goto fail;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	/*
2088c2ecf20Sopenharmony_ci	 * The full exception fixup machinery is available as soon as
2098c2ecf20Sopenharmony_ci	 * the early IDT is loaded.  This means that it is the
2108c2ecf20Sopenharmony_ci	 * responsibility of extable users to either function correctly
2118c2ecf20Sopenharmony_ci	 * when handlers are invoked early or to simply avoid causing
2128c2ecf20Sopenharmony_ci	 * exceptions before they're ready to handle them.
2138c2ecf20Sopenharmony_ci	 *
2148c2ecf20Sopenharmony_ci	 * This is better than filtering which handlers can be used,
2158c2ecf20Sopenharmony_ci	 * because refusing to call a handler here is guaranteed to
2168c2ecf20Sopenharmony_ci	 * result in a hard-to-debug panic.
2178c2ecf20Sopenharmony_ci	 *
2188c2ecf20Sopenharmony_ci	 * Keep in mind that not all vectors actually get here.  Early
2198c2ecf20Sopenharmony_ci	 * page faults, for example, are special.
2208c2ecf20Sopenharmony_ci	 */
2218c2ecf20Sopenharmony_ci	if (fixup_exception(regs, trapnr, regs->orig_ax, 0))
2228c2ecf20Sopenharmony_ci		return;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	if (trapnr == X86_TRAP_UD) {
2258c2ecf20Sopenharmony_ci		if (report_bug(regs->ip, regs) == BUG_TRAP_TYPE_WARN) {
2268c2ecf20Sopenharmony_ci			/* Skip the ud2. */
2278c2ecf20Sopenharmony_ci			regs->ip += LEN_UD2;
2288c2ecf20Sopenharmony_ci			return;
2298c2ecf20Sopenharmony_ci		}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci		/*
2328c2ecf20Sopenharmony_ci		 * If this was a BUG and report_bug returns or if this
2338c2ecf20Sopenharmony_ci		 * was just a normal #UD, we want to continue onward and
2348c2ecf20Sopenharmony_ci		 * crash.
2358c2ecf20Sopenharmony_ci		 */
2368c2ecf20Sopenharmony_ci	}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_cifail:
2398c2ecf20Sopenharmony_ci	early_printk("PANIC: early exception 0x%02x IP %lx:%lx error %lx cr2 0x%lx\n",
2408c2ecf20Sopenharmony_ci		     (unsigned)trapnr, (unsigned long)regs->cs, regs->ip,
2418c2ecf20Sopenharmony_ci		     regs->orig_ax, read_cr2());
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	show_regs(regs);
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cihalt_loop:
2468c2ecf20Sopenharmony_ci	while (true)
2478c2ecf20Sopenharmony_ci		halt();
2488c2ecf20Sopenharmony_ci}
249