1 #include <linux/extable.h>
2 #include <linux/uaccess.h>
3 #include <linux/sched/debug.h>
6 #include <asm/fpu/internal.h>
8 #include <asm/kdebug.h>
10 typedef bool (*ex_handler_t)(const struct exception_table_entry *,
11 struct pt_regs *, int, unsigned long,
14 static inline unsigned long
15 ex_fixup_addr(const struct exception_table_entry *x)
17 return (unsigned long)&x->fixup + x->fixup;
19 static inline ex_handler_t
20 ex_fixup_handler(const struct exception_table_entry *x)
22 return (ex_handler_t)((unsigned long)&x->handler + x->handler);
25 __visible bool ex_handler_default(const struct exception_table_entry *fixup,
26 struct pt_regs *regs, int trapnr,
27 unsigned long error_code,
28 unsigned long fault_addr)
30 regs->ip = ex_fixup_addr(fixup);
33 EXPORT_SYMBOL(ex_handler_default);
35 __visible bool ex_handler_fault(const struct exception_table_entry *fixup,
36 struct pt_regs *regs, int trapnr,
37 unsigned long error_code,
38 unsigned long fault_addr)
40 regs->ip = ex_fixup_addr(fixup);
44 EXPORT_SYMBOL_GPL(ex_handler_fault);
47 * Handler for UD0 exception following a failed test against the
48 * result of a refcount inc/dec/add/sub.
50 __visible bool ex_handler_refcount(const struct exception_table_entry *fixup,
51 struct pt_regs *regs, int trapnr,
52 unsigned long error_code,
53 unsigned long fault_addr)
55 /* First unconditionally saturate the refcount. */
56 *(int *)regs->cx = INT_MIN / 2;
59 * Strictly speaking, this reports the fixup destination, not
60 * the fault location, and not the actually overflowing
61 * instruction, which is the instruction before the "js", but
62 * since that instruction could be a variety of lengths, just
63 * report the location after the overflow, which should be close
64 * enough for finding the overflow, as it's at least back in
65 * the function, having returned from .text.unlikely.
67 regs->ip = ex_fixup_addr(fixup);
70 * This function has been called because either a negative refcount
71 * value was seen by any of the refcount functions, or a zero
72 * refcount value was seen by refcount_dec().
74 * If we crossed from INT_MAX to INT_MIN, OF (Overflow Flag: result
75 * wrapped around) will be set. Additionally, seeing the refcount
76 * reach 0 will set ZF (Zero Flag: result was zero). In each of
77 * these cases we want a report, since it's a boundary condition.
78 * The SF case is not reported since it indicates post-boundary
79 * manipulations below zero or above INT_MAX. And if none of the
80 * flags are set, something has gone very wrong, so report it.
82 if (regs->flags & (X86_EFLAGS_OF | X86_EFLAGS_ZF)) {
83 bool zero = regs->flags & X86_EFLAGS_ZF;
85 refcount_error_report(regs, zero ? "hit zero" : "overflow");
86 } else if ((regs->flags & X86_EFLAGS_SF) == 0) {
87 /* Report if none of OF, ZF, nor SF are set. */
88 refcount_error_report(regs, "unexpected saturation");
93 EXPORT_SYMBOL(ex_handler_refcount);
96 * Handler for when we fail to restore a task's FPU state. We should never get
97 * here because the FPU state of a task using the FPU (task->thread.fpu.state)
98 * should always be valid. However, past bugs have allowed userspace to set
99 * reserved bits in the XSAVE area using PTRACE_SETREGSET or sys_rt_sigreturn().
100 * These caused XRSTOR to fail when switching to the task, leaking the FPU
101 * registers of the task previously executing on the CPU. Mitigate this class
102 * of vulnerability by restoring from the initial state (essentially, zeroing
103 * out all the FPU registers) if we can't restore from the task's FPU state.
105 __visible bool ex_handler_fprestore(const struct exception_table_entry *fixup,
106 struct pt_regs *regs, int trapnr,
107 unsigned long error_code,
108 unsigned long fault_addr)
110 regs->ip = ex_fixup_addr(fixup);
112 WARN_ONCE(1, "Bad FPU state detected at %pB, reinitializing FPU registers.",
113 (void *)instruction_pointer(regs));
115 __copy_kernel_to_fpregs(&init_fpstate, -1);
118 EXPORT_SYMBOL_GPL(ex_handler_fprestore);
120 __visible bool ex_handler_uaccess(const struct exception_table_entry *fixup,
121 struct pt_regs *regs, int trapnr,
122 unsigned long error_code,
123 unsigned long fault_addr)
125 regs->ip = ex_fixup_addr(fixup);
128 EXPORT_SYMBOL(ex_handler_uaccess);
130 __visible bool ex_handler_ext(const struct exception_table_entry *fixup,
131 struct pt_regs *regs, int trapnr,
132 unsigned long error_code,
133 unsigned long fault_addr)
135 /* Special hack for uaccess_err */
136 current->thread.uaccess_err = 1;
137 regs->ip = ex_fixup_addr(fixup);
140 EXPORT_SYMBOL(ex_handler_ext);
142 __visible bool ex_handler_rdmsr_unsafe(const struct exception_table_entry *fixup,
143 struct pt_regs *regs, int trapnr,
144 unsigned long error_code,
145 unsigned long fault_addr)
147 if (pr_warn_once("unchecked MSR access error: RDMSR from 0x%x at rIP: 0x%lx (%pF)\n",
148 (unsigned int)regs->cx, regs->ip, (void *)regs->ip))
149 show_stack_regs(regs);
151 /* Pretend that the read succeeded and returned 0. */
152 regs->ip = ex_fixup_addr(fixup);
157 EXPORT_SYMBOL(ex_handler_rdmsr_unsafe);
159 __visible bool ex_handler_wrmsr_unsafe(const struct exception_table_entry *fixup,
160 struct pt_regs *regs, int trapnr,
161 unsigned long error_code,
162 unsigned long fault_addr)
164 if (pr_warn_once("unchecked MSR access error: WRMSR to 0x%x (tried to write 0x%08x%08x) at rIP: 0x%lx (%pF)\n",
165 (unsigned int)regs->cx, (unsigned int)regs->dx,
166 (unsigned int)regs->ax, regs->ip, (void *)regs->ip))
167 show_stack_regs(regs);
169 /* Pretend that the write succeeded. */
170 regs->ip = ex_fixup_addr(fixup);
173 EXPORT_SYMBOL(ex_handler_wrmsr_unsafe);
175 __visible bool ex_handler_clear_fs(const struct exception_table_entry *fixup,
176 struct pt_regs *regs, int trapnr,
177 unsigned long error_code,
178 unsigned long fault_addr)
180 if (static_cpu_has(X86_BUG_NULL_SEG))
181 asm volatile ("mov %0, %%fs" : : "rm" (__USER_DS));
182 asm volatile ("mov %0, %%fs" : : "rm" (0));
183 return ex_handler_default(fixup, regs, trapnr, error_code, fault_addr);
185 EXPORT_SYMBOL(ex_handler_clear_fs);
187 __visible bool ex_has_fault_handler(unsigned long ip)
189 const struct exception_table_entry *e;
190 ex_handler_t handler;
192 e = search_exception_tables(ip);
195 handler = ex_fixup_handler(e);
197 return handler == ex_handler_fault;
200 int fixup_exception(struct pt_regs *regs, int trapnr, unsigned long error_code,
201 unsigned long fault_addr)
203 const struct exception_table_entry *e;
204 ex_handler_t handler;
206 #ifdef CONFIG_PNPBIOS
207 if (unlikely(SEGMENT_IS_PNP_CODE(regs->cs))) {
208 extern u32 pnp_bios_fault_eip, pnp_bios_fault_esp;
209 extern u32 pnp_bios_is_utter_crap;
210 pnp_bios_is_utter_crap = 1;
211 printk(KERN_CRIT "PNPBIOS fault.. attempting recovery.\n");
215 : : "g" (pnp_bios_fault_esp), "g" (pnp_bios_fault_eip));
216 panic("do_trap: can't hit this");
220 e = search_exception_tables(regs->ip);
224 handler = ex_fixup_handler(e);
225 return handler(e, regs, trapnr, error_code, fault_addr);
228 extern unsigned int early_recursion_flag;
230 /* Restricted version used during very early boot */
231 void __init early_fixup_exception(struct pt_regs *regs, int trapnr)
233 /* Ignore early NMIs. */
234 if (trapnr == X86_TRAP_NMI)
237 if (early_recursion_flag > 2)
241 * Old CPUs leave the high bits of CS on the stack
242 * undefined. I'm not sure which CPUs do this, but at least
243 * the 486 DX works this way.
244 * Xen pv domains are not using the default __KERNEL_CS.
246 if (!xen_pv_domain() && regs->cs != __KERNEL_CS)
250 * The full exception fixup machinery is available as soon as
251 * the early IDT is loaded. This means that it is the
252 * responsibility of extable users to either function correctly
253 * when handlers are invoked early or to simply avoid causing
254 * exceptions before they're ready to handle them.
256 * This is better than filtering which handlers can be used,
257 * because refusing to call a handler here is guaranteed to
258 * result in a hard-to-debug panic.
260 * Keep in mind that not all vectors actually get here. Early
261 * page faults, for example, are special.
263 if (fixup_exception(regs, trapnr, regs->orig_ax, 0))
266 if (fixup_bug(regs, trapnr))
270 early_printk("PANIC: early exception 0x%02x IP %lx:%lx error %lx cr2 0x%lx\n",
271 (unsigned)trapnr, (unsigned long)regs->cs, regs->ip,
272 regs->orig_ax, read_cr2());