1#include <linux/extable.h>
2#include <linux/uaccess.h>
3#include <linux/sched/debug.h>
4#include <xen/xen.h>
5
6#include <asm/fpu/internal.h>
7#include <asm/traps.h>
8#include <asm/kdebug.h>
9
10typedef bool (*ex_handler_t)(const struct exception_table_entry *,
11 struct pt_regs *, int, unsigned long,
12 unsigned long);
13
14static inline unsigned long
15ex_fixup_addr(const struct exception_table_entry *x)
16{
17 return (unsigned long)&x->fixup + x->fixup;
18}
19static inline ex_handler_t
20ex_fixup_handler(const struct exception_table_entry *x)
21{
22 return (ex_handler_t)((unsigned long)&x->handler + x->handler);
23}
24
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)
29{
30 regs->ip = ex_fixup_addr(fixup);
31 return true;
32}
33EXPORT_SYMBOL(ex_handler_default);
34
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)
39{
40 regs->ip = ex_fixup_addr(fixup);
41 regs->ax = trapnr;
42 return true;
43}
44EXPORT_SYMBOL_GPL(ex_handler_fault);
45
46
47
48
49
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)
54{
55
56 *(int *)regs->cx = INT_MIN / 2;
57
58
59
60
61
62
63
64
65
66
67 regs->ip = ex_fixup_addr(fixup);
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 if (regs->flags & (X86_EFLAGS_OF | X86_EFLAGS_ZF)) {
83 bool zero = regs->flags & X86_EFLAGS_ZF;
84
85 refcount_error_report(regs, zero ? "hit zero" : "overflow");
86 } else if ((regs->flags & X86_EFLAGS_SF) == 0) {
87
88 refcount_error_report(regs, "unexpected saturation");
89 }
90
91 return true;
92}
93EXPORT_SYMBOL(ex_handler_refcount);
94
95
96
97
98
99
100
101
102
103
104
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)
109{
110 regs->ip = ex_fixup_addr(fixup);
111
112 WARN_ONCE(1, "Bad FPU state detected at %pB, reinitializing FPU registers.",
113 (void *)instruction_pointer(regs));
114
115 __copy_kernel_to_fpregs(&init_fpstate, -1);
116 return true;
117}
118EXPORT_SYMBOL_GPL(ex_handler_fprestore);
119
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)
124{
125 regs->ip = ex_fixup_addr(fixup);
126 return true;
127}
128EXPORT_SYMBOL(ex_handler_uaccess);
129
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)
134{
135
136 current->thread.uaccess_err = 1;
137 regs->ip = ex_fixup_addr(fixup);
138 return true;
139}
140EXPORT_SYMBOL(ex_handler_ext);
141
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)
146{
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);
150
151
152 regs->ip = ex_fixup_addr(fixup);
153 regs->ax = 0;
154 regs->dx = 0;
155 return true;
156}
157EXPORT_SYMBOL(ex_handler_rdmsr_unsafe);
158
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)
163{
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);
168
169
170 regs->ip = ex_fixup_addr(fixup);
171 return true;
172}
173EXPORT_SYMBOL(ex_handler_wrmsr_unsafe);
174
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)
179{
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);
184}
185EXPORT_SYMBOL(ex_handler_clear_fs);
186
187__visible bool ex_has_fault_handler(unsigned long ip)
188{
189 const struct exception_table_entry *e;
190 ex_handler_t handler;
191
192 e = search_exception_tables(ip);
193 if (!e)
194 return false;
195 handler = ex_fixup_handler(e);
196
197 return handler == ex_handler_fault;
198}
199
200int fixup_exception(struct pt_regs *regs, int trapnr, unsigned long error_code,
201 unsigned long fault_addr)
202{
203 const struct exception_table_entry *e;
204 ex_handler_t handler;
205
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");
212 __asm__ volatile(
213 "movl %0, %%esp\n\t"
214 "jmp *%1\n\t"
215 : : "g" (pnp_bios_fault_esp), "g" (pnp_bios_fault_eip));
216 panic("do_trap: can't hit this");
217 }
218#endif
219
220 e = search_exception_tables(regs->ip);
221 if (!e)
222 return 0;
223
224 handler = ex_fixup_handler(e);
225 return handler(e, regs, trapnr, error_code, fault_addr);
226}
227
228extern unsigned int early_recursion_flag;
229
230
231void __init early_fixup_exception(struct pt_regs *regs, int trapnr)
232{
233
234 if (trapnr == X86_TRAP_NMI)
235 return;
236
237 if (early_recursion_flag > 2)
238 goto halt_loop;
239
240
241
242
243
244
245
246 if (!xen_pv_domain() && regs->cs != __KERNEL_CS)
247 goto fail;
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263 if (fixup_exception(regs, trapnr, regs->orig_ax, 0))
264 return;
265
266 if (fixup_bug(regs, trapnr))
267 return;
268
269fail:
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());
273
274 show_regs(regs);
275
276halt_loop:
277 while (true)
278 halt();
279}
280