xref: /OK3568_Linux_fs/kernel/arch/x86/mm/extable.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun #include <linux/extable.h>
3*4882a593Smuzhiyun #include <linux/uaccess.h>
4*4882a593Smuzhiyun #include <linux/sched/debug.h>
5*4882a593Smuzhiyun #include <xen/xen.h>
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <asm/fpu/internal.h>
8*4882a593Smuzhiyun #include <asm/sev-es.h>
9*4882a593Smuzhiyun #include <asm/traps.h>
10*4882a593Smuzhiyun #include <asm/kdebug.h>
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun typedef bool (*ex_handler_t)(const struct exception_table_entry *,
13*4882a593Smuzhiyun 			    struct pt_regs *, int, unsigned long,
14*4882a593Smuzhiyun 			    unsigned long);
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun static inline unsigned long
ex_fixup_addr(const struct exception_table_entry * x)17*4882a593Smuzhiyun ex_fixup_addr(const struct exception_table_entry *x)
18*4882a593Smuzhiyun {
19*4882a593Smuzhiyun 	return (unsigned long)&x->fixup + x->fixup;
20*4882a593Smuzhiyun }
21*4882a593Smuzhiyun static inline ex_handler_t
ex_fixup_handler(const struct exception_table_entry * x)22*4882a593Smuzhiyun ex_fixup_handler(const struct exception_table_entry *x)
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun 	return (ex_handler_t)((unsigned long)&x->handler + x->handler);
25*4882a593Smuzhiyun }
26*4882a593Smuzhiyun 
ex_handler_default(const struct exception_table_entry * fixup,struct pt_regs * regs,int trapnr,unsigned long error_code,unsigned long fault_addr)27*4882a593Smuzhiyun __visible bool ex_handler_default(const struct exception_table_entry *fixup,
28*4882a593Smuzhiyun 				  struct pt_regs *regs, int trapnr,
29*4882a593Smuzhiyun 				  unsigned long error_code,
30*4882a593Smuzhiyun 				  unsigned long fault_addr)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun 	regs->ip = ex_fixup_addr(fixup);
33*4882a593Smuzhiyun 	return true;
34*4882a593Smuzhiyun }
35*4882a593Smuzhiyun EXPORT_SYMBOL(ex_handler_default);
36*4882a593Smuzhiyun 
ex_handler_fault(const struct exception_table_entry * fixup,struct pt_regs * regs,int trapnr,unsigned long error_code,unsigned long fault_addr)37*4882a593Smuzhiyun __visible bool ex_handler_fault(const struct exception_table_entry *fixup,
38*4882a593Smuzhiyun 				struct pt_regs *regs, int trapnr,
39*4882a593Smuzhiyun 				unsigned long error_code,
40*4882a593Smuzhiyun 				unsigned long fault_addr)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	regs->ip = ex_fixup_addr(fixup);
43*4882a593Smuzhiyun 	regs->ax = trapnr;
44*4882a593Smuzhiyun 	return true;
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ex_handler_fault);
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun /*
49*4882a593Smuzhiyun  * Handler for when we fail to restore a task's FPU state.  We should never get
50*4882a593Smuzhiyun  * here because the FPU state of a task using the FPU (task->thread.fpu.state)
51*4882a593Smuzhiyun  * should always be valid.  However, past bugs have allowed userspace to set
52*4882a593Smuzhiyun  * reserved bits in the XSAVE area using PTRACE_SETREGSET or sys_rt_sigreturn().
53*4882a593Smuzhiyun  * These caused XRSTOR to fail when switching to the task, leaking the FPU
54*4882a593Smuzhiyun  * registers of the task previously executing on the CPU.  Mitigate this class
55*4882a593Smuzhiyun  * of vulnerability by restoring from the initial state (essentially, zeroing
56*4882a593Smuzhiyun  * out all the FPU registers) if we can't restore from the task's FPU state.
57*4882a593Smuzhiyun  */
ex_handler_fprestore(const struct exception_table_entry * fixup,struct pt_regs * regs,int trapnr,unsigned long error_code,unsigned long fault_addr)58*4882a593Smuzhiyun __visible bool ex_handler_fprestore(const struct exception_table_entry *fixup,
59*4882a593Smuzhiyun 				    struct pt_regs *regs, int trapnr,
60*4882a593Smuzhiyun 				    unsigned long error_code,
61*4882a593Smuzhiyun 				    unsigned long fault_addr)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	regs->ip = ex_fixup_addr(fixup);
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	WARN_ONCE(1, "Bad FPU state detected at %pB, reinitializing FPU registers.",
66*4882a593Smuzhiyun 		  (void *)instruction_pointer(regs));
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	__copy_kernel_to_fpregs(&init_fpstate, -1);
69*4882a593Smuzhiyun 	return true;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ex_handler_fprestore);
72*4882a593Smuzhiyun 
ex_handler_uaccess(const struct exception_table_entry * fixup,struct pt_regs * regs,int trapnr,unsigned long error_code,unsigned long fault_addr)73*4882a593Smuzhiyun __visible bool ex_handler_uaccess(const struct exception_table_entry *fixup,
74*4882a593Smuzhiyun 				  struct pt_regs *regs, int trapnr,
75*4882a593Smuzhiyun 				  unsigned long error_code,
76*4882a593Smuzhiyun 				  unsigned long fault_addr)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	WARN_ONCE(trapnr == X86_TRAP_GP, "General protection fault in user access. Non-canonical address?");
79*4882a593Smuzhiyun 	regs->ip = ex_fixup_addr(fixup);
80*4882a593Smuzhiyun 	return true;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun EXPORT_SYMBOL(ex_handler_uaccess);
83*4882a593Smuzhiyun 
ex_handler_copy(const struct exception_table_entry * fixup,struct pt_regs * regs,int trapnr,unsigned long error_code,unsigned long fault_addr)84*4882a593Smuzhiyun __visible bool ex_handler_copy(const struct exception_table_entry *fixup,
85*4882a593Smuzhiyun 			       struct pt_regs *regs, int trapnr,
86*4882a593Smuzhiyun 			       unsigned long error_code,
87*4882a593Smuzhiyun 			       unsigned long fault_addr)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	WARN_ONCE(trapnr == X86_TRAP_GP, "General protection fault in user access. Non-canonical address?");
90*4882a593Smuzhiyun 	regs->ip = ex_fixup_addr(fixup);
91*4882a593Smuzhiyun 	regs->ax = trapnr;
92*4882a593Smuzhiyun 	return true;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun EXPORT_SYMBOL(ex_handler_copy);
95*4882a593Smuzhiyun 
ex_handler_rdmsr_unsafe(const struct exception_table_entry * fixup,struct pt_regs * regs,int trapnr,unsigned long error_code,unsigned long fault_addr)96*4882a593Smuzhiyun __visible bool ex_handler_rdmsr_unsafe(const struct exception_table_entry *fixup,
97*4882a593Smuzhiyun 				       struct pt_regs *regs, int trapnr,
98*4882a593Smuzhiyun 				       unsigned long error_code,
99*4882a593Smuzhiyun 				       unsigned long fault_addr)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun 	if (pr_warn_once("unchecked MSR access error: RDMSR from 0x%x at rIP: 0x%lx (%pS)\n",
102*4882a593Smuzhiyun 			 (unsigned int)regs->cx, regs->ip, (void *)regs->ip))
103*4882a593Smuzhiyun 		show_stack_regs(regs);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	/* Pretend that the read succeeded and returned 0. */
106*4882a593Smuzhiyun 	regs->ip = ex_fixup_addr(fixup);
107*4882a593Smuzhiyun 	regs->ax = 0;
108*4882a593Smuzhiyun 	regs->dx = 0;
109*4882a593Smuzhiyun 	return true;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun EXPORT_SYMBOL(ex_handler_rdmsr_unsafe);
112*4882a593Smuzhiyun 
ex_handler_wrmsr_unsafe(const struct exception_table_entry * fixup,struct pt_regs * regs,int trapnr,unsigned long error_code,unsigned long fault_addr)113*4882a593Smuzhiyun __visible bool ex_handler_wrmsr_unsafe(const struct exception_table_entry *fixup,
114*4882a593Smuzhiyun 				       struct pt_regs *regs, int trapnr,
115*4882a593Smuzhiyun 				       unsigned long error_code,
116*4882a593Smuzhiyun 				       unsigned long fault_addr)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	if (pr_warn_once("unchecked MSR access error: WRMSR to 0x%x (tried to write 0x%08x%08x) at rIP: 0x%lx (%pS)\n",
119*4882a593Smuzhiyun 			 (unsigned int)regs->cx, (unsigned int)regs->dx,
120*4882a593Smuzhiyun 			 (unsigned int)regs->ax,  regs->ip, (void *)regs->ip))
121*4882a593Smuzhiyun 		show_stack_regs(regs);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	/* Pretend that the write succeeded. */
124*4882a593Smuzhiyun 	regs->ip = ex_fixup_addr(fixup);
125*4882a593Smuzhiyun 	return true;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun EXPORT_SYMBOL(ex_handler_wrmsr_unsafe);
128*4882a593Smuzhiyun 
ex_handler_clear_fs(const struct exception_table_entry * fixup,struct pt_regs * regs,int trapnr,unsigned long error_code,unsigned long fault_addr)129*4882a593Smuzhiyun __visible bool ex_handler_clear_fs(const struct exception_table_entry *fixup,
130*4882a593Smuzhiyun 				   struct pt_regs *regs, int trapnr,
131*4882a593Smuzhiyun 				   unsigned long error_code,
132*4882a593Smuzhiyun 				   unsigned long fault_addr)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	if (static_cpu_has(X86_BUG_NULL_SEG))
135*4882a593Smuzhiyun 		asm volatile ("mov %0, %%fs" : : "rm" (__USER_DS));
136*4882a593Smuzhiyun 	asm volatile ("mov %0, %%fs" : : "rm" (0));
137*4882a593Smuzhiyun 	return ex_handler_default(fixup, regs, trapnr, error_code, fault_addr);
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun EXPORT_SYMBOL(ex_handler_clear_fs);
140*4882a593Smuzhiyun 
ex_get_fault_handler_type(unsigned long ip)141*4882a593Smuzhiyun enum handler_type ex_get_fault_handler_type(unsigned long ip)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	const struct exception_table_entry *e;
144*4882a593Smuzhiyun 	ex_handler_t handler;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	e = search_exception_tables(ip);
147*4882a593Smuzhiyun 	if (!e)
148*4882a593Smuzhiyun 		return EX_HANDLER_NONE;
149*4882a593Smuzhiyun 	handler = ex_fixup_handler(e);
150*4882a593Smuzhiyun 	if (handler == ex_handler_fault)
151*4882a593Smuzhiyun 		return EX_HANDLER_FAULT;
152*4882a593Smuzhiyun 	else if (handler == ex_handler_uaccess || handler == ex_handler_copy)
153*4882a593Smuzhiyun 		return EX_HANDLER_UACCESS;
154*4882a593Smuzhiyun 	else
155*4882a593Smuzhiyun 		return EX_HANDLER_OTHER;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun __nocfi
fixup_exception(struct pt_regs * regs,int trapnr,unsigned long error_code,unsigned long fault_addr)159*4882a593Smuzhiyun int fixup_exception(struct pt_regs *regs, int trapnr, unsigned long error_code,
160*4882a593Smuzhiyun 		    unsigned long fault_addr)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun 	const struct exception_table_entry *e;
163*4882a593Smuzhiyun 	ex_handler_t handler;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun #ifdef CONFIG_PNPBIOS
166*4882a593Smuzhiyun 	if (unlikely(SEGMENT_IS_PNP_CODE(regs->cs))) {
167*4882a593Smuzhiyun 		extern u32 pnp_bios_fault_eip, pnp_bios_fault_esp;
168*4882a593Smuzhiyun 		extern u32 pnp_bios_is_utter_crap;
169*4882a593Smuzhiyun 		pnp_bios_is_utter_crap = 1;
170*4882a593Smuzhiyun 		printk(KERN_CRIT "PNPBIOS fault.. attempting recovery.\n");
171*4882a593Smuzhiyun 		__asm__ volatile(
172*4882a593Smuzhiyun 			"movl %0, %%esp\n\t"
173*4882a593Smuzhiyun 			"jmp *%1\n\t"
174*4882a593Smuzhiyun 			: : "g" (pnp_bios_fault_esp), "g" (pnp_bios_fault_eip));
175*4882a593Smuzhiyun 		panic("do_trap: can't hit this");
176*4882a593Smuzhiyun 	}
177*4882a593Smuzhiyun #endif
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	e = search_exception_tables(regs->ip);
180*4882a593Smuzhiyun 	if (!e)
181*4882a593Smuzhiyun 		return 0;
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	handler = ex_fixup_handler(e);
184*4882a593Smuzhiyun 	return handler(e, regs, trapnr, error_code, fault_addr);
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun extern unsigned int early_recursion_flag;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun /* Restricted version used during very early boot */
early_fixup_exception(struct pt_regs * regs,int trapnr)190*4882a593Smuzhiyun void __init early_fixup_exception(struct pt_regs *regs, int trapnr)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	/* Ignore early NMIs. */
193*4882a593Smuzhiyun 	if (trapnr == X86_TRAP_NMI)
194*4882a593Smuzhiyun 		return;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	if (early_recursion_flag > 2)
197*4882a593Smuzhiyun 		goto halt_loop;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	/*
200*4882a593Smuzhiyun 	 * Old CPUs leave the high bits of CS on the stack
201*4882a593Smuzhiyun 	 * undefined.  I'm not sure which CPUs do this, but at least
202*4882a593Smuzhiyun 	 * the 486 DX works this way.
203*4882a593Smuzhiyun 	 * Xen pv domains are not using the default __KERNEL_CS.
204*4882a593Smuzhiyun 	 */
205*4882a593Smuzhiyun 	if (!xen_pv_domain() && regs->cs != __KERNEL_CS)
206*4882a593Smuzhiyun 		goto fail;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	/*
209*4882a593Smuzhiyun 	 * The full exception fixup machinery is available as soon as
210*4882a593Smuzhiyun 	 * the early IDT is loaded.  This means that it is the
211*4882a593Smuzhiyun 	 * responsibility of extable users to either function correctly
212*4882a593Smuzhiyun 	 * when handlers are invoked early or to simply avoid causing
213*4882a593Smuzhiyun 	 * exceptions before they're ready to handle them.
214*4882a593Smuzhiyun 	 *
215*4882a593Smuzhiyun 	 * This is better than filtering which handlers can be used,
216*4882a593Smuzhiyun 	 * because refusing to call a handler here is guaranteed to
217*4882a593Smuzhiyun 	 * result in a hard-to-debug panic.
218*4882a593Smuzhiyun 	 *
219*4882a593Smuzhiyun 	 * Keep in mind that not all vectors actually get here.  Early
220*4882a593Smuzhiyun 	 * page faults, for example, are special.
221*4882a593Smuzhiyun 	 */
222*4882a593Smuzhiyun 	if (fixup_exception(regs, trapnr, regs->orig_ax, 0))
223*4882a593Smuzhiyun 		return;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	if (trapnr == X86_TRAP_UD) {
226*4882a593Smuzhiyun 		if (report_bug(regs->ip, regs) == BUG_TRAP_TYPE_WARN) {
227*4882a593Smuzhiyun 			/* Skip the ud2. */
228*4882a593Smuzhiyun 			regs->ip += LEN_UD2;
229*4882a593Smuzhiyun 			return;
230*4882a593Smuzhiyun 		}
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 		/*
233*4882a593Smuzhiyun 		 * If this was a BUG and report_bug returns or if this
234*4882a593Smuzhiyun 		 * was just a normal #UD, we want to continue onward and
235*4882a593Smuzhiyun 		 * crash.
236*4882a593Smuzhiyun 		 */
237*4882a593Smuzhiyun 	}
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun fail:
240*4882a593Smuzhiyun 	early_printk("PANIC: early exception 0x%02x IP %lx:%lx error %lx cr2 0x%lx\n",
241*4882a593Smuzhiyun 		     (unsigned)trapnr, (unsigned long)regs->cs, regs->ip,
242*4882a593Smuzhiyun 		     regs->orig_ax, read_cr2());
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	show_regs(regs);
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun halt_loop:
247*4882a593Smuzhiyun 	while (true)
248*4882a593Smuzhiyun 		halt();
249*4882a593Smuzhiyun }
250