Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * Copyright (C) 1991, 1992 Linus Torvalds |
3 | * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs | |
4 | * | |
5 | * Pentium III FXSR, SSE support | |
6 | * Gareth Hughes <gareth@valinux.com>, May 2000 | |
1da177e4 LT |
7 | */ |
8 | ||
9 | /* | |
10 | * 'Traps.c' handles hardware traps and faults after we have saved some | |
11 | * state in 'entry.S'. | |
12 | */ | |
badc7652 AH |
13 | #include <linux/moduleparam.h> |
14 | #include <linux/interrupt.h> | |
15 | #include <linux/kallsyms.h> | |
16 | #include <linux/spinlock.h> | |
17 | #include <linux/kprobes.h> | |
18 | #include <linux/uaccess.h> | |
19 | #include <linux/utsname.h> | |
20 | #include <linux/kdebug.h> | |
1da177e4 | 21 | #include <linux/kernel.h> |
badc7652 AH |
22 | #include <linux/module.h> |
23 | #include <linux/ptrace.h> | |
1da177e4 | 24 | #include <linux/string.h> |
badc7652 AH |
25 | #include <linux/unwind.h> |
26 | #include <linux/delay.h> | |
1da177e4 | 27 | #include <linux/errno.h> |
badc7652 AH |
28 | #include <linux/kexec.h> |
29 | #include <linux/sched.h> | |
1da177e4 | 30 | #include <linux/timer.h> |
1da177e4 | 31 | #include <linux/init.h> |
c31a0bf3 | 32 | #include <linux/bug.h> |
badc7652 AH |
33 | #include <linux/nmi.h> |
34 | #include <linux/mm.h> | |
e32ede19 | 35 | |
c0d12172 DJ |
36 | #if defined(CONFIG_EDAC) |
37 | #include <linux/edac.h> | |
38 | #endif | |
39 | ||
badc7652 AH |
40 | #include <asm/stacktrace.h> |
41 | #include <asm/processor.h> | |
1da177e4 | 42 | #include <asm/debugreg.h> |
badc7652 AH |
43 | #include <asm/atomic.h> |
44 | #include <asm/system.h> | |
45 | #include <asm/unwind.h> | |
1da177e4 LT |
46 | #include <asm/desc.h> |
47 | #include <asm/i387.h> | |
badc7652 | 48 | #include <asm/nmi.h> |
1da177e4 | 49 | #include <asm/smp.h> |
badc7652 | 50 | #include <asm/io.h> |
1da177e4 | 51 | #include <asm/pgalloc.h> |
1da177e4 | 52 | #include <asm/proto.h> |
badc7652 | 53 | #include <asm/pda.h> |
6ac8d51f | 54 | #include <asm/traps.h> |
badc7652 AH |
55 | |
56 | #include <mach_traps.h> | |
1da177e4 | 57 | |
c6425b9f | 58 | int panic_on_unrecovered_nmi; |
badc7652 | 59 | int kstack_depth_to_print = 12; |
a25bd949 | 60 | static unsigned int code_bytes = 64; |
badc7652 AH |
61 | static int ignore_nmis; |
62 | static int die_counter; | |
a25bd949 | 63 | |
1da177e4 LT |
64 | static inline void conditional_sti(struct pt_regs *regs) |
65 | { | |
65ea5b03 | 66 | if (regs->flags & X86_EFLAGS_IF) |
1da177e4 LT |
67 | local_irq_enable(); |
68 | } | |
69 | ||
a65d17c9 JB |
70 | static inline void preempt_conditional_sti(struct pt_regs *regs) |
71 | { | |
e8bff74a | 72 | inc_preempt_count(); |
65ea5b03 | 73 | if (regs->flags & X86_EFLAGS_IF) |
a65d17c9 JB |
74 | local_irq_enable(); |
75 | } | |
76 | ||
77 | static inline void preempt_conditional_cli(struct pt_regs *regs) | |
78 | { | |
65ea5b03 | 79 | if (regs->flags & X86_EFLAGS_IF) |
a65d17c9 | 80 | local_irq_disable(); |
40e59a61 AK |
81 | /* Make sure to not schedule here because we could be running |
82 | on an exception stack. */ | |
e8bff74a | 83 | dec_preempt_count(); |
a65d17c9 JB |
84 | } |
85 | ||
bc850d6b | 86 | void printk_address(unsigned long address, int reliable) |
3ac94932 | 87 | { |
7daf705f | 88 | printk(" [<%016lx>] %s%pS\n", address, reliable ? "": "? ", (void *) address); |
a5ff677c | 89 | } |
1da177e4 | 90 | |
0a658002 | 91 | static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack, |
c0b766f1 | 92 | unsigned *usedp, char **idp) |
0a658002 | 93 | { |
b556b35e | 94 | static char ids[][8] = { |
0a658002 AK |
95 | [DEBUG_STACK - 1] = "#DB", |
96 | [NMI_STACK - 1] = "NMI", | |
97 | [DOUBLEFAULT_STACK - 1] = "#DF", | |
98 | [STACKFAULT_STACK - 1] = "#SS", | |
99 | [MCE_STACK - 1] = "#MC", | |
b556b35e JB |
100 | #if DEBUG_STKSZ > EXCEPTION_STKSZ |
101 | [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]" | |
102 | #endif | |
0a658002 AK |
103 | }; |
104 | unsigned k; | |
1da177e4 | 105 | |
c9ca1ba5 IM |
106 | /* |
107 | * Iterate over all exception stacks, and figure out whether | |
108 | * 'stack' is in one of them: | |
109 | */ | |
0a658002 | 110 | for (k = 0; k < N_EXCEPTION_STACKS; k++) { |
f5741644 | 111 | unsigned long end = per_cpu(orig_ist, cpu).ist[k]; |
c9ca1ba5 IM |
112 | /* |
113 | * Is 'stack' above this exception frame's end? | |
114 | * If yes then skip to the next frame. | |
115 | */ | |
0a658002 AK |
116 | if (stack >= end) |
117 | continue; | |
c9ca1ba5 IM |
118 | /* |
119 | * Is 'stack' above this exception frame's start address? | |
120 | * If yes then we found the right frame. | |
121 | */ | |
0a658002 | 122 | if (stack >= end - EXCEPTION_STKSZ) { |
c9ca1ba5 IM |
123 | /* |
124 | * Make sure we only iterate through an exception | |
125 | * stack once. If it comes up for the second time | |
126 | * then there's something wrong going on - just | |
127 | * break out and return NULL: | |
128 | */ | |
0a658002 AK |
129 | if (*usedp & (1U << k)) |
130 | break; | |
131 | *usedp |= 1U << k; | |
132 | *idp = ids[k]; | |
133 | return (unsigned long *)end; | |
134 | } | |
c9ca1ba5 IM |
135 | /* |
136 | * If this is a debug stack, and if it has a larger size than | |
137 | * the usual exception stacks, then 'stack' might still | |
138 | * be within the lower portion of the debug stack: | |
139 | */ | |
b556b35e JB |
140 | #if DEBUG_STKSZ > EXCEPTION_STKSZ |
141 | if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) { | |
142 | unsigned j = N_EXCEPTION_STACKS - 1; | |
143 | ||
c9ca1ba5 IM |
144 | /* |
145 | * Black magic. A large debug stack is composed of | |
146 | * multiple exception stack entries, which we | |
147 | * iterate through now. Dont look: | |
148 | */ | |
b556b35e JB |
149 | do { |
150 | ++j; | |
151 | end -= EXCEPTION_STKSZ; | |
152 | ids[j][4] = '1' + (j - N_EXCEPTION_STACKS); | |
153 | } while (stack < end - EXCEPTION_STKSZ); | |
154 | if (*usedp & (1U << j)) | |
155 | break; | |
156 | *usedp |= 1U << j; | |
157 | *idp = ids[j]; | |
158 | return (unsigned long *)end; | |
159 | } | |
160 | #endif | |
1da177e4 LT |
161 | } |
162 | return NULL; | |
0a658002 | 163 | } |
1da177e4 LT |
164 | |
165 | /* | |
676b1855 | 166 | * x86-64 can have up to three kernel stacks: |
1da177e4 LT |
167 | * process stack |
168 | * interrupt stack | |
0a658002 | 169 | * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack |
1da177e4 LT |
170 | */ |
171 | ||
e4a94568 AV |
172 | static inline int valid_stack_ptr(struct thread_info *tinfo, |
173 | void *p, unsigned int size, void *end) | |
c547c77e | 174 | { |
ade1af77 | 175 | void *t = tinfo; |
e4a94568 AV |
176 | if (end) { |
177 | if (p < end && p >= (end-THREAD_SIZE)) | |
178 | return 1; | |
179 | else | |
180 | return 0; | |
181 | } | |
182 | return p > t && p < t + THREAD_SIZE - size; | |
183 | } | |
184 | ||
80b51f31 AV |
185 | /* The form of the top of the frame on the stack */ |
186 | struct stack_frame { | |
187 | struct stack_frame *next_frame; | |
188 | unsigned long return_address; | |
189 | }; | |
190 | ||
a8c1be9d AH |
191 | static inline unsigned long |
192 | print_context_stack(struct thread_info *tinfo, | |
193 | unsigned long *stack, unsigned long bp, | |
194 | const struct stacktrace_ops *ops, void *data, | |
195 | unsigned long *end) | |
e4a94568 | 196 | { |
80b51f31 AV |
197 | struct stack_frame *frame = (struct stack_frame *)bp; |
198 | ||
199 | while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) { | |
200 | unsigned long addr; | |
201 | ||
202 | addr = *stack; | |
e4a94568 | 203 | if (__kernel_text_address(addr)) { |
80b51f31 AV |
204 | if ((unsigned long) stack == bp + 8) { |
205 | ops->address(data, addr, 1); | |
206 | frame = frame->next_frame; | |
207 | bp = (unsigned long) frame; | |
208 | } else { | |
209 | ops->address(data, addr, bp == 0); | |
210 | } | |
e4a94568 | 211 | } |
80b51f31 | 212 | stack++; |
e4a94568 AV |
213 | } |
214 | return bp; | |
c547c77e AK |
215 | } |
216 | ||
a8c1be9d | 217 | void dump_trace(struct task_struct *task, struct pt_regs *regs, |
bc850d6b | 218 | unsigned long *stack, unsigned long bp, |
9689ba8a | 219 | const struct stacktrace_ops *ops, void *data) |
1da177e4 | 220 | { |
da68933e | 221 | const unsigned cpu = get_cpu(); |
b615ebda | 222 | unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr; |
0a658002 | 223 | unsigned used = 0; |
c547c77e | 224 | struct thread_info *tinfo; |
1da177e4 | 225 | |
a8c1be9d AH |
226 | if (!task) |
227 | task = current; | |
b538ed27 | 228 | |
c0b766f1 AK |
229 | if (!stack) { |
230 | unsigned long dummy; | |
231 | stack = &dummy; | |
a8c1be9d AH |
232 | if (task && task != current) |
233 | stack = (unsigned long *)task->thread.sp; | |
b538ed27 JB |
234 | } |
235 | ||
80b51f31 AV |
236 | #ifdef CONFIG_FRAME_POINTER |
237 | if (!bp) { | |
a8c1be9d | 238 | if (task == current) { |
80b51f31 | 239 | /* Grab bp right from our regs */ |
a8c1be9d | 240 | asm("movq %%rbp, %0" : "=r" (bp) :); |
80b51f31 AV |
241 | } else { |
242 | /* bp is the last reg pushed by switch_to */ | |
a8c1be9d | 243 | bp = *(unsigned long *) task->thread.sp; |
80b51f31 AV |
244 | } |
245 | } | |
246 | #endif | |
247 | ||
c9ca1ba5 IM |
248 | /* |
249 | * Print function call entries in all stacks, starting at the | |
250 | * current stack address. If the stacks consist of nested | |
251 | * exceptions | |
252 | */ | |
7b4fd4bb | 253 | tinfo = task_thread_info(task); |
c0b766f1 AK |
254 | for (;;) { |
255 | char *id; | |
0a658002 AK |
256 | unsigned long *estack_end; |
257 | estack_end = in_exception_stack(cpu, (unsigned long)stack, | |
258 | &used, &id); | |
259 | ||
260 | if (estack_end) { | |
c0b766f1 AK |
261 | if (ops->stack(data, id) < 0) |
262 | break; | |
e4a94568 | 263 | |
80b51f31 AV |
264 | bp = print_context_stack(tinfo, stack, bp, ops, |
265 | data, estack_end); | |
c0b766f1 | 266 | ops->stack(data, "<EOE>"); |
c9ca1ba5 IM |
267 | /* |
268 | * We link to the next stack via the | |
269 | * second-to-last pointer (index -2 to end) in the | |
270 | * exception stack: | |
271 | */ | |
0a658002 AK |
272 | stack = (unsigned long *) estack_end[-2]; |
273 | continue; | |
1da177e4 | 274 | } |
0a658002 AK |
275 | if (irqstack_end) { |
276 | unsigned long *irqstack; | |
277 | irqstack = irqstack_end - | |
278 | (IRQSTACKSIZE - 64) / sizeof(*irqstack); | |
279 | ||
280 | if (stack >= irqstack && stack < irqstack_end) { | |
c0b766f1 AK |
281 | if (ops->stack(data, "IRQ") < 0) |
282 | break; | |
80b51f31 AV |
283 | bp = print_context_stack(tinfo, stack, bp, |
284 | ops, data, irqstack_end); | |
c9ca1ba5 IM |
285 | /* |
286 | * We link to the next stack (which would be | |
287 | * the process stack normally) the last | |
288 | * pointer (index -1 to end) in the IRQ stack: | |
289 | */ | |
0a658002 AK |
290 | stack = (unsigned long *) (irqstack_end[-1]); |
291 | irqstack_end = NULL; | |
c0b766f1 | 292 | ops->stack(data, "EOI"); |
0a658002 | 293 | continue; |
1da177e4 | 294 | } |
1da177e4 | 295 | } |
0a658002 | 296 | break; |
1da177e4 | 297 | } |
0a658002 | 298 | |
c9ca1ba5 | 299 | /* |
c0b766f1 | 300 | * This handles the process stack: |
c9ca1ba5 | 301 | */ |
80b51f31 | 302 | bp = print_context_stack(tinfo, stack, bp, ops, data, NULL); |
da68933e | 303 | put_cpu(); |
c0b766f1 AK |
304 | } |
305 | EXPORT_SYMBOL(dump_trace); | |
306 | ||
307 | static void | |
308 | print_trace_warning_symbol(void *data, char *msg, unsigned long symbol) | |
309 | { | |
310 | print_symbol(msg, symbol); | |
311 | printk("\n"); | |
312 | } | |
313 | ||
314 | static void print_trace_warning(void *data, char *msg) | |
315 | { | |
316 | printk("%s\n", msg); | |
317 | } | |
318 | ||
319 | static int print_trace_stack(void *data, char *name) | |
320 | { | |
321 | printk(" <%s> ", name); | |
322 | return 0; | |
323 | } | |
3ac94932 | 324 | |
bc850d6b | 325 | static void print_trace_address(void *data, unsigned long addr, int reliable) |
c0b766f1 | 326 | { |
1c978b93 | 327 | touch_nmi_watchdog(); |
bc850d6b | 328 | printk_address(addr, reliable); |
c0b766f1 AK |
329 | } |
330 | ||
9689ba8a | 331 | static const struct stacktrace_ops print_trace_ops = { |
c0b766f1 AK |
332 | .warning = print_trace_warning, |
333 | .warning_symbol = print_trace_warning_symbol, | |
334 | .stack = print_trace_stack, | |
335 | .address = print_trace_address, | |
336 | }; | |
337 | ||
7dedcee3 AH |
338 | static void |
339 | show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs, | |
340 | unsigned long *stack, unsigned long bp, char *log_lvl) | |
c0b766f1 AK |
341 | { |
342 | printk("\nCall Trace:\n"); | |
7dedcee3 | 343 | dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl); |
1da177e4 LT |
344 | printk("\n"); |
345 | } | |
346 | ||
7dedcee3 AH |
347 | void show_trace(struct task_struct *task, struct pt_regs *regs, |
348 | unsigned long *stack, unsigned long bp) | |
349 | { | |
350 | show_trace_log_lvl(task, regs, stack, bp, ""); | |
351 | } | |
352 | ||
c0b766f1 | 353 | static void |
7dedcee3 AH |
354 | show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs, |
355 | unsigned long *sp, unsigned long bp, char *log_lvl) | |
1da177e4 LT |
356 | { |
357 | unsigned long *stack; | |
358 | int i; | |
151f8cc1 | 359 | const int cpu = smp_processor_id(); |
df79efde RT |
360 | unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr); |
361 | unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE); | |
1da177e4 LT |
362 | |
363 | // debugging aid: "show_stack(NULL, NULL);" prints the | |
364 | // back trace for this cpu. | |
365 | ||
65ea5b03 | 366 | if (sp == NULL) { |
a8c1be9d AH |
367 | if (task) |
368 | sp = (unsigned long *)task->thread.sp; | |
1da177e4 | 369 | else |
65ea5b03 | 370 | sp = (unsigned long *)&sp; |
1da177e4 LT |
371 | } |
372 | ||
65ea5b03 | 373 | stack = sp; |
a8c1be9d | 374 | for (i = 0; i < kstack_depth_to_print; i++) { |
1da177e4 LT |
375 | if (stack >= irqstack && stack <= irqstack_end) { |
376 | if (stack == irqstack_end) { | |
377 | stack = (unsigned long *) (irqstack_end[-1]); | |
378 | printk(" <EOI> "); | |
379 | } | |
380 | } else { | |
381 | if (((long) stack & (THREAD_SIZE-1)) == 0) | |
382 | break; | |
383 | } | |
384 | if (i && ((i % 4) == 0)) | |
3ac94932 IM |
385 | printk("\n"); |
386 | printk(" %016lx", *stack++); | |
35faa714 | 387 | touch_nmi_watchdog(); |
1da177e4 | 388 | } |
7dedcee3 | 389 | show_trace_log_lvl(task, regs, sp, bp, log_lvl); |
b538ed27 JB |
390 | } |
391 | ||
a8c1be9d | 392 | void show_stack(struct task_struct *task, unsigned long *sp) |
b538ed27 | 393 | { |
7dedcee3 | 394 | show_stack_log_lvl(task, NULL, sp, 0, ""); |
1da177e4 LT |
395 | } |
396 | ||
397 | /* | |
398 | * The architecture-independent dump_stack generator | |
399 | */ | |
400 | void dump_stack(void) | |
401 | { | |
bc850d6b | 402 | unsigned long bp = 0; |
7b4fd4bb | 403 | unsigned long stack; |
57c351de | 404 | |
80b51f31 AV |
405 | #ifdef CONFIG_FRAME_POINTER |
406 | if (!bp) | |
407 | asm("movq %%rbp, %0" : "=r" (bp):); | |
408 | #endif | |
409 | ||
57c351de AV |
410 | printk("Pid: %d, comm: %.20s %s %s %.*s\n", |
411 | current->pid, current->comm, print_tainted(), | |
412 | init_utsname()->release, | |
413 | (int)strcspn(init_utsname()->version, " "), | |
414 | init_utsname()->version); | |
a8c1be9d | 415 | show_trace(NULL, NULL, &stack, bp); |
1da177e4 LT |
416 | } |
417 | ||
418 | EXPORT_SYMBOL(dump_stack); | |
419 | ||
420 | void show_registers(struct pt_regs *regs) | |
421 | { | |
422 | int i; | |
65ea5b03 | 423 | unsigned long sp; |
151f8cc1 | 424 | const int cpu = smp_processor_id(); |
df79efde | 425 | struct task_struct *cur = cpu_pda(cpu)->pcurrent; |
1da177e4 | 426 | |
65ea5b03 | 427 | sp = regs->sp; |
1da177e4 LT |
428 | printk("CPU %d ", cpu); |
429 | __show_regs(regs); | |
430 | printk("Process %s (pid: %d, threadinfo %p, task %p)\n", | |
e4f17c43 | 431 | cur->comm, cur->pid, task_thread_info(cur), cur); |
1da177e4 LT |
432 | |
433 | /* | |
434 | * When in-kernel, we also print out the stack and code at the | |
435 | * time of the fault.. | |
436 | */ | |
a25bd949 | 437 | if (!user_mode(regs)) { |
7b4fd4bb AH |
438 | unsigned int code_prologue = code_bytes * 43 / 64; |
439 | unsigned int code_len = code_bytes; | |
a25bd949 | 440 | unsigned char c; |
7b4fd4bb AH |
441 | u8 *ip; |
442 | ||
1da177e4 | 443 | printk("Stack: "); |
7dedcee3 AH |
444 | show_stack_log_lvl(NULL, regs, (unsigned long *)sp, |
445 | regs->bp, ""); | |
a25bd949 | 446 | printk("\n"); |
1da177e4 | 447 | |
a25bd949 | 448 | printk(KERN_EMERG "Code: "); |
7b4fd4bb AH |
449 | |
450 | ip = (u8 *)regs->ip - code_prologue; | |
a25bd949 AV |
451 | if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) { |
452 | /* try starting at RIP */ | |
a8c1be9d | 453 | ip = (u8 *)regs->ip; |
a25bd949 AV |
454 | code_len = code_len - code_prologue + 1; |
455 | } | |
456 | for (i = 0; i < code_len; i++, ip++) { | |
457 | if (ip < (u8 *)PAGE_OFFSET || | |
458 | probe_kernel_address(ip, c)) { | |
1da177e4 LT |
459 | printk(" Bad RIP value."); |
460 | break; | |
461 | } | |
a25bd949 AV |
462 | if (ip == (u8 *)regs->ip) |
463 | printk("<%02x> ", c); | |
464 | else | |
465 | printk("%02x ", c); | |
1da177e4 LT |
466 | } |
467 | } | |
468 | printk("\n"); | |
a8c1be9d | 469 | } |
1da177e4 | 470 | |
65ea5b03 | 471 | int is_valid_bugaddr(unsigned long ip) |
c31a0bf3 JF |
472 | { |
473 | unsigned short ud2; | |
474 | ||
65ea5b03 | 475 | if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2))) |
c31a0bf3 JF |
476 | return 0; |
477 | ||
478 | return ud2 == 0x0b0f; | |
479 | } | |
1da177e4 | 480 | |
39743c9e | 481 | static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED; |
1da177e4 | 482 | static int die_owner = -1; |
cdc60a4c | 483 | static unsigned int die_nest_count; |
1da177e4 | 484 | |
eddb6fb9 | 485 | unsigned __kprobes long oops_begin(void) |
1da177e4 | 486 | { |
b39b7036 | 487 | int cpu; |
1209140c JB |
488 | unsigned long flags; |
489 | ||
abf0f109 AM |
490 | oops_enter(); |
491 | ||
1209140c | 492 | /* racy, but better than risking deadlock. */ |
39743c9e | 493 | raw_local_irq_save(flags); |
b39b7036 | 494 | cpu = smp_processor_id(); |
39743c9e | 495 | if (!__raw_spin_trylock(&die_lock)) { |
1da177e4 LT |
496 | if (cpu == die_owner) |
497 | /* nested oops. should stop eventually */; | |
498 | else | |
39743c9e | 499 | __raw_spin_lock(&die_lock); |
1da177e4 | 500 | } |
cdc60a4c | 501 | die_nest_count++; |
1209140c | 502 | die_owner = cpu; |
1da177e4 | 503 | console_verbose(); |
1209140c JB |
504 | bust_spinlocks(1); |
505 | return flags; | |
1da177e4 LT |
506 | } |
507 | ||
22f5991c | 508 | void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr) |
78cbac65 | 509 | { |
1da177e4 | 510 | die_owner = -1; |
1209140c | 511 | bust_spinlocks(0); |
cdc60a4c | 512 | die_nest_count--; |
39743c9e | 513 | if (!die_nest_count) |
cdc60a4c | 514 | /* Nest count reaches zero, release the lock. */ |
39743c9e AK |
515 | __raw_spin_unlock(&die_lock); |
516 | raw_local_irq_restore(flags); | |
22f5991c JB |
517 | if (!regs) { |
518 | oops_exit(); | |
519 | return; | |
520 | } | |
1da177e4 | 521 | if (panic_on_oops) |
012c437d | 522 | panic("Fatal exception"); |
abf0f109 | 523 | oops_exit(); |
22f5991c | 524 | do_exit(signr); |
1209140c | 525 | } |
1da177e4 | 526 | |
e423f49f | 527 | int __kprobes __die(const char *str, struct pt_regs *regs, long err) |
1da177e4 | 528 | { |
e423f49f | 529 | printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff, ++die_counter); |
1da177e4 LT |
530 | #ifdef CONFIG_PREEMPT |
531 | printk("PREEMPT "); | |
532 | #endif | |
533 | #ifdef CONFIG_SMP | |
534 | printk("SMP "); | |
535 | #endif | |
536 | #ifdef CONFIG_DEBUG_PAGEALLOC | |
537 | printk("DEBUG_PAGEALLOC"); | |
538 | #endif | |
539 | printk("\n"); | |
a8c1be9d AH |
540 | if (notify_die(DIE_OOPS, str, regs, err, |
541 | current->thread.trap_no, SIGSEGV) == NOTIFY_STOP) | |
22f5991c | 542 | return 1; |
a8c1be9d | 543 | |
1da177e4 | 544 | show_registers(regs); |
bcdcd8e7 | 545 | add_taint(TAINT_DIE); |
1da177e4 LT |
546 | /* Executive summary in case the oops scrolled away */ |
547 | printk(KERN_ALERT "RIP "); | |
aafbd7eb | 548 | printk_address(regs->ip, 1); |
65ea5b03 | 549 | printk(" RSP <%016lx>\n", regs->sp); |
8bcc5280 VG |
550 | if (kexec_should_crash(current)) |
551 | crash_kexec(regs); | |
22f5991c | 552 | return 0; |
1da177e4 LT |
553 | } |
554 | ||
7b4fd4bb | 555 | void die(const char *str, struct pt_regs *regs, long err) |
1da177e4 | 556 | { |
1209140c JB |
557 | unsigned long flags = oops_begin(); |
558 | ||
c31a0bf3 | 559 | if (!user_mode(regs)) |
65ea5b03 | 560 | report_bug(regs->ip, regs); |
c31a0bf3 | 561 | |
22f5991c JB |
562 | if (__die(str, regs, err)) |
563 | regs = NULL; | |
564 | oops_end(flags, regs, SIGSEGV); | |
1da177e4 | 565 | } |
1da177e4 | 566 | |
5deb45e3 SR |
567 | notrace __kprobes void |
568 | die_nmi(char *str, struct pt_regs *regs, int do_panic) | |
1da177e4 | 569 | { |
737a460f | 570 | unsigned long flags; |
1209140c | 571 | |
a8c1be9d | 572 | if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP) |
737a460f | 573 | return; |
1209140c | 574 | |
737a460f | 575 | flags = oops_begin(); |
1da177e4 LT |
576 | /* |
577 | * We are in trouble anyway, lets at least try | |
578 | * to get a message out. | |
579 | */ | |
e56b3a12 CG |
580 | printk(KERN_EMERG "%s", str); |
581 | printk(" on CPU%d, ip %08lx, registers:\n", | |
582 | smp_processor_id(), regs->ip); | |
1da177e4 | 583 | show_registers(regs); |
8bcc5280 VG |
584 | if (kexec_should_crash(current)) |
585 | crash_kexec(regs); | |
fac58550 AK |
586 | if (do_panic || panic_on_oops) |
587 | panic("Non maskable interrupt"); | |
22f5991c | 588 | oops_end(flags, NULL, SIGBUS); |
8b1ffe95 CM |
589 | nmi_exit(); |
590 | local_irq_enable(); | |
22f5991c | 591 | do_exit(SIGBUS); |
1da177e4 LT |
592 | } |
593 | ||
a8c1be9d AH |
594 | static void __kprobes |
595 | do_trap(int trapnr, int signr, char *str, struct pt_regs *regs, | |
596 | long error_code, siginfo_t *info) | |
1da177e4 | 597 | { |
6e3f3617 JB |
598 | struct task_struct *tsk = current; |
599 | ||
a7bbb0ce AH |
600 | if (!user_mode(regs)) |
601 | goto kernel_trap; | |
1da177e4 | 602 | |
a7bbb0ce AH |
603 | /* |
604 | * We want error_code and trap_no set for userspace faults and | |
605 | * kernelspace faults which result in die(), but not | |
606 | * kernelspace faults which are fixed up. die() gives the | |
607 | * process no chance to handle the signal and notice the | |
608 | * kernel fault information, so that won't result in polluting | |
609 | * the information about previously queued, but not yet | |
610 | * delivered, faults. See also do_general_protection below. | |
611 | */ | |
612 | tsk->thread.error_code = error_code; | |
613 | tsk->thread.trap_no = trapnr; | |
614 | ||
615 | if (show_unhandled_signals && unhandled_signal(tsk, signr) && | |
616 | printk_ratelimit()) { | |
617 | printk(KERN_INFO | |
618 | "%s[%d] trap %s ip:%lx sp:%lx error:%lx", | |
619 | tsk->comm, tsk->pid, str, | |
620 | regs->ip, regs->sp, error_code); | |
621 | print_vma_addr(" in ", regs->ip); | |
622 | printk("\n"); | |
1da177e4 LT |
623 | } |
624 | ||
a7bbb0ce AH |
625 | if (info) |
626 | force_sig_info(signr, info, tsk); | |
627 | else | |
628 | force_sig(signr, tsk); | |
629 | return; | |
1da177e4 | 630 | |
a7bbb0ce | 631 | kernel_trap: |
b3a5acc1 HH |
632 | if (!fixup_exception(regs)) { |
633 | tsk->thread.error_code = error_code; | |
634 | tsk->thread.trap_no = trapnr; | |
635 | die(str, regs, error_code); | |
1da177e4 | 636 | } |
b3a5acc1 | 637 | return; |
1da177e4 LT |
638 | } |
639 | ||
640 | #define DO_ERROR(trapnr, signr, str, name) \ | |
a8c1be9d AH |
641 | asmlinkage void do_##name(struct pt_regs * regs, long error_code) \ |
642 | { \ | |
643 | if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \ | |
644 | == NOTIFY_STOP) \ | |
645 | return; \ | |
40e59a61 | 646 | conditional_sti(regs); \ |
a8c1be9d | 647 | do_trap(trapnr, signr, str, regs, error_code, NULL); \ |
1da177e4 LT |
648 | } |
649 | ||
a8c1be9d AH |
650 | #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \ |
651 | asmlinkage void do_##name(struct pt_regs * regs, long error_code) \ | |
652 | { \ | |
653 | siginfo_t info; \ | |
654 | info.si_signo = signr; \ | |
655 | info.si_errno = 0; \ | |
656 | info.si_code = sicode; \ | |
657 | info.si_addr = (void __user *)siaddr; \ | |
658 | trace_hardirqs_fixup(); \ | |
659 | if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \ | |
660 | == NOTIFY_STOP) \ | |
661 | return; \ | |
40e59a61 | 662 | conditional_sti(regs); \ |
a8c1be9d | 663 | do_trap(trapnr, signr, str, regs, error_code, &info); \ |
1da177e4 LT |
664 | } |
665 | ||
a8c1be9d AH |
666 | DO_ERROR_INFO(0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip) |
667 | DO_ERROR(4, SIGSEGV, "overflow", overflow) | |
668 | DO_ERROR(5, SIGSEGV, "bounds", bounds) | |
669 | DO_ERROR_INFO(6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip) | |
670 | DO_ERROR(9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun) | |
1da177e4 | 671 | DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS) |
a8c1be9d | 672 | DO_ERROR(11, SIGBUS, "segment not present", segment_not_present) |
1da177e4 | 673 | DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0) |
40e59a61 AK |
674 | |
675 | /* Runs on IST stack */ | |
676 | asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code) | |
677 | { | |
678 | if (notify_die(DIE_TRAP, "stack segment", regs, error_code, | |
679 | 12, SIGBUS) == NOTIFY_STOP) | |
680 | return; | |
681 | preempt_conditional_sti(regs); | |
682 | do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL); | |
683 | preempt_conditional_cli(regs); | |
684 | } | |
eca37c18 JB |
685 | |
686 | asmlinkage void do_double_fault(struct pt_regs * regs, long error_code) | |
687 | { | |
688 | static const char str[] = "double fault"; | |
689 | struct task_struct *tsk = current; | |
690 | ||
691 | /* Return not checked because double check cannot be ignored */ | |
692 | notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV); | |
693 | ||
694 | tsk->thread.error_code = error_code; | |
695 | tsk->thread.trap_no = 8; | |
696 | ||
697 | /* This is always a kernel trap and never fixable (and thus must | |
698 | never return). */ | |
699 | for (;;) | |
700 | die(str, regs, error_code); | |
701 | } | |
1da177e4 | 702 | |
13485ab5 AH |
703 | asmlinkage void __kprobes |
704 | do_general_protection(struct pt_regs *regs, long error_code) | |
1da177e4 | 705 | { |
13485ab5 | 706 | struct task_struct *tsk; |
6e3f3617 | 707 | |
1da177e4 LT |
708 | conditional_sti(regs); |
709 | ||
13485ab5 AH |
710 | tsk = current; |
711 | if (!user_mode(regs)) | |
712 | goto gp_in_kernel; | |
1da177e4 | 713 | |
13485ab5 AH |
714 | tsk->thread.error_code = error_code; |
715 | tsk->thread.trap_no = 13; | |
716 | ||
717 | if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) && | |
718 | printk_ratelimit()) { | |
719 | printk(KERN_INFO | |
720 | "%s[%d] general protection ip:%lx sp:%lx error:%lx", | |
721 | tsk->comm, tsk->pid, | |
722 | regs->ip, regs->sp, error_code); | |
723 | print_vma_addr(" in ", regs->ip); | |
724 | printk("\n"); | |
725 | } | |
726 | ||
727 | force_sig(SIGSEGV, tsk); | |
728 | return; | |
1da177e4 | 729 | |
13485ab5 | 730 | gp_in_kernel: |
b3a5acc1 HH |
731 | if (fixup_exception(regs)) |
732 | return; | |
d1895183 | 733 | |
b3a5acc1 HH |
734 | tsk->thread.error_code = error_code; |
735 | tsk->thread.trap_no = 13; | |
736 | if (notify_die(DIE_GPF, "general protection fault", regs, | |
737 | error_code, 13, SIGSEGV) == NOTIFY_STOP) | |
738 | return; | |
739 | die("general protection fault", regs, error_code); | |
1da177e4 LT |
740 | } |
741 | ||
5deb45e3 | 742 | static notrace __kprobes void |
a8c1be9d | 743 | mem_parity_error(unsigned char reason, struct pt_regs *regs) |
1da177e4 | 744 | { |
c41c5cd3 DZ |
745 | printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n", |
746 | reason); | |
9c5f8be4 | 747 | printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n"); |
c41c5cd3 | 748 | |
c0d12172 | 749 | #if defined(CONFIG_EDAC) |
a8c1be9d | 750 | if (edac_handler_set()) { |
c0d12172 DJ |
751 | edac_atomic_assert_error(); |
752 | return; | |
753 | } | |
754 | #endif | |
755 | ||
8da5adda | 756 | if (panic_on_unrecovered_nmi) |
c41c5cd3 DZ |
757 | panic("NMI: Not continuing"); |
758 | ||
759 | printk(KERN_EMERG "Dazed and confused, but trying to continue\n"); | |
1da177e4 LT |
760 | |
761 | /* Clear and disable the memory parity error line. */ | |
762 | reason = (reason & 0xf) | 4; | |
763 | outb(reason, 0x61); | |
764 | } | |
765 | ||
5deb45e3 | 766 | static notrace __kprobes void |
a8c1be9d | 767 | io_check_error(unsigned char reason, struct pt_regs *regs) |
1da177e4 LT |
768 | { |
769 | printk("NMI: IOCK error (debug interrupt?)\n"); | |
770 | show_registers(regs); | |
771 | ||
772 | /* Re-enable the IOCK line, wait for a few seconds */ | |
773 | reason = (reason & 0xf) | 8; | |
774 | outb(reason, 0x61); | |
775 | mdelay(2000); | |
776 | reason &= ~8; | |
777 | outb(reason, 0x61); | |
778 | } | |
779 | ||
5deb45e3 | 780 | static notrace __kprobes void |
eddb6fb9 | 781 | unknown_nmi_error(unsigned char reason, struct pt_regs * regs) |
c41c5cd3 | 782 | { |
d3597524 JW |
783 | if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP) |
784 | return; | |
c41c5cd3 DZ |
785 | printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n", |
786 | reason); | |
787 | printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n"); | |
8da5adda DZ |
788 | |
789 | if (panic_on_unrecovered_nmi) | |
c41c5cd3 | 790 | panic("NMI: Not continuing"); |
8da5adda | 791 | |
c41c5cd3 | 792 | printk(KERN_EMERG "Dazed and confused, but trying to continue\n"); |
1da177e4 LT |
793 | } |
794 | ||
6fefb0d1 AK |
795 | /* Runs on IST stack. This code must keep interrupts off all the time. |
796 | Nested NMIs are prevented by the CPU. */ | |
abd34807 | 797 | asmlinkage notrace __kprobes void default_do_nmi(struct pt_regs *regs) |
1da177e4 LT |
798 | { |
799 | unsigned char reason = 0; | |
76e4f660 AR |
800 | int cpu; |
801 | ||
802 | cpu = smp_processor_id(); | |
1da177e4 | 803 | |
a8c1be9d | 804 | /* Only the BSP gets external NMIs from the system. */ |
76e4f660 | 805 | if (!cpu) |
1da177e4 LT |
806 | reason = get_nmi_reason(); |
807 | ||
808 | if (!(reason & 0xc0)) { | |
6e3f3617 | 809 | if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT) |
1da177e4 LT |
810 | == NOTIFY_STOP) |
811 | return; | |
1da177e4 LT |
812 | /* |
813 | * Ok, so this is none of the documented NMI sources, | |
814 | * so it must be the NMI watchdog. | |
815 | */ | |
a8c1be9d | 816 | if (nmi_watchdog_tick(regs, reason)) |
1da177e4 | 817 | return; |
a8c1be9d | 818 | if (!do_nmi_callback(regs, cpu)) |
3adbbcce DZ |
819 | unknown_nmi_error(reason, regs); |
820 | ||
1da177e4 LT |
821 | return; |
822 | } | |
6e3f3617 | 823 | if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP) |
a8c1be9d | 824 | return; |
1da177e4 LT |
825 | |
826 | /* AK: following checks seem to be broken on modern chipsets. FIXME */ | |
1da177e4 LT |
827 | if (reason & 0x80) |
828 | mem_parity_error(reason, regs); | |
829 | if (reason & 0x40) | |
830 | io_check_error(reason, regs); | |
831 | } | |
832 | ||
c6425b9f CG |
833 | asmlinkage notrace __kprobes void |
834 | do_nmi(struct pt_regs *regs, long error_code) | |
835 | { | |
836 | nmi_enter(); | |
a8c1be9d | 837 | |
c6425b9f | 838 | add_pda(__nmi_count, 1); |
a8c1be9d | 839 | |
c6425b9f CG |
840 | if (!ignore_nmis) |
841 | default_do_nmi(regs); | |
a8c1be9d | 842 | |
c6425b9f CG |
843 | nmi_exit(); |
844 | } | |
845 | ||
846 | void stop_nmi(void) | |
847 | { | |
848 | acpi_nmi_disable(); | |
849 | ignore_nmis++; | |
850 | } | |
851 | ||
852 | void restart_nmi(void) | |
853 | { | |
854 | ignore_nmis--; | |
855 | acpi_nmi_enable(); | |
856 | } | |
857 | ||
b556b35e | 858 | /* runs on IST stack. */ |
a8c1be9d | 859 | asmlinkage void __kprobes do_int3(struct pt_regs *regs, long error_code) |
1da177e4 | 860 | { |
143a5d32 PZ |
861 | trace_hardirqs_fixup(); |
862 | ||
a8c1be9d AH |
863 | if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) |
864 | == NOTIFY_STOP) | |
1da177e4 | 865 | return; |
a8c1be9d | 866 | |
40e59a61 | 867 | preempt_conditional_sti(regs); |
1da177e4 | 868 | do_trap(3, SIGTRAP, "int3", regs, error_code, NULL); |
40e59a61 | 869 | preempt_conditional_cli(regs); |
1da177e4 LT |
870 | } |
871 | ||
6fefb0d1 AK |
872 | /* Help handler running on IST stack to switch back to user stack |
873 | for scheduling or signal handling. The actual stack switch is done in | |
874 | entry.S */ | |
eddb6fb9 | 875 | asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs) |
6fefb0d1 AK |
876 | { |
877 | struct pt_regs *regs = eregs; | |
878 | /* Did already sync */ | |
65ea5b03 | 879 | if (eregs == (struct pt_regs *)eregs->sp) |
6fefb0d1 AK |
880 | ; |
881 | /* Exception from user space */ | |
76381fee | 882 | else if (user_mode(eregs)) |
bb049232 | 883 | regs = task_pt_regs(current); |
6fefb0d1 AK |
884 | /* Exception from kernel and interrupts are enabled. Move to |
885 | kernel process stack. */ | |
65ea5b03 PA |
886 | else if (eregs->flags & X86_EFLAGS_IF) |
887 | regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs)); | |
6fefb0d1 AK |
888 | if (eregs != regs) |
889 | *regs = *eregs; | |
890 | return regs; | |
891 | } | |
892 | ||
1da177e4 | 893 | /* runs on IST stack. */ |
0f2fbdcb PP |
894 | asmlinkage void __kprobes do_debug(struct pt_regs * regs, |
895 | unsigned long error_code) | |
1da177e4 | 896 | { |
1da177e4 | 897 | struct task_struct *tsk = current; |
7b4fd4bb | 898 | unsigned long condition; |
1da177e4 LT |
899 | siginfo_t info; |
900 | ||
000f4a9e PZ |
901 | trace_hardirqs_fixup(); |
902 | ||
e9129e56 | 903 | get_debugreg(condition, 6); |
1da177e4 | 904 | |
10faa81e RM |
905 | /* |
906 | * The processor cleared BTF, so don't mark that we need it set. | |
907 | */ | |
908 | clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR); | |
909 | tsk->thread.debugctlmsr = 0; | |
910 | ||
1da177e4 | 911 | if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code, |
daeeafec | 912 | SIGTRAP) == NOTIFY_STOP) |
6fefb0d1 | 913 | return; |
daeeafec | 914 | |
a65d17c9 | 915 | preempt_conditional_sti(regs); |
1da177e4 LT |
916 | |
917 | /* Mask out spurious debug traps due to lazy DR7 setting */ | |
918 | if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) { | |
a8c1be9d | 919 | if (!tsk->thread.debugreg7) |
1da177e4 | 920 | goto clear_dr7; |
1da177e4 LT |
921 | } |
922 | ||
923 | tsk->thread.debugreg6 = condition; | |
924 | ||
e1f28773 RM |
925 | /* |
926 | * Single-stepping through TF: make sure we ignore any events in | |
927 | * kernel space (but re-enable TF when returning to user mode). | |
928 | */ | |
daeeafec | 929 | if (condition & DR_STEP) { |
a8c1be9d AH |
930 | if (!user_mode(regs)) |
931 | goto clear_TF_reenable; | |
1da177e4 LT |
932 | } |
933 | ||
934 | /* Ok, finally something we can handle */ | |
935 | tsk->thread.trap_no = 1; | |
936 | tsk->thread.error_code = error_code; | |
937 | info.si_signo = SIGTRAP; | |
938 | info.si_errno = 0; | |
939 | info.si_code = TRAP_BRKPT; | |
65ea5b03 | 940 | info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL; |
01b8faae | 941 | force_sig_info(SIGTRAP, &info, tsk); |
1da177e4 | 942 | |
1da177e4 | 943 | clear_dr7: |
a8c1be9d | 944 | set_debugreg(0, 7); |
a65d17c9 | 945 | preempt_conditional_cli(regs); |
6fefb0d1 | 946 | return; |
1da177e4 LT |
947 | |
948 | clear_TF_reenable: | |
949 | set_tsk_thread_flag(tsk, TIF_SINGLESTEP); | |
053de044 | 950 | regs->flags &= ~X86_EFLAGS_TF; |
a65d17c9 | 951 | preempt_conditional_cli(regs); |
a8c1be9d | 952 | return; |
1da177e4 LT |
953 | } |
954 | ||
6e3f3617 | 955 | static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr) |
1da177e4 | 956 | { |
b3a5acc1 | 957 | if (fixup_exception(regs)) |
1da177e4 | 958 | return 1; |
b3a5acc1 | 959 | |
6e3f3617 | 960 | notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE); |
3a848f63 | 961 | /* Illegal floating point operation in the kernel */ |
6e3f3617 | 962 | current->thread.trap_no = trapnr; |
1da177e4 | 963 | die(str, regs, 0); |
1da177e4 LT |
964 | return 0; |
965 | } | |
966 | ||
967 | /* | |
968 | * Note that we play around with the 'TS' bit in an attempt to get | |
969 | * the correct behaviour even in the presence of the asynchronous | |
970 | * IRQ13 behaviour | |
971 | */ | |
972 | asmlinkage void do_coprocessor_error(struct pt_regs *regs) | |
973 | { | |
65ea5b03 | 974 | void __user *ip = (void __user *)(regs->ip); |
a8c1be9d | 975 | struct task_struct *task; |
1da177e4 LT |
976 | siginfo_t info; |
977 | unsigned short cwd, swd; | |
978 | ||
979 | conditional_sti(regs); | |
76381fee | 980 | if (!user_mode(regs) && |
6e3f3617 | 981 | kernel_math_error(regs, "kernel x87 math error", 16)) |
1da177e4 LT |
982 | return; |
983 | ||
984 | /* | |
985 | * Save the info for the exception handler and clear the error. | |
986 | */ | |
987 | task = current; | |
988 | save_init_fpu(task); | |
989 | task->thread.trap_no = 16; | |
990 | task->thread.error_code = 0; | |
991 | info.si_signo = SIGFPE; | |
992 | info.si_errno = 0; | |
993 | info.si_code = __SI_FAULT; | |
65ea5b03 | 994 | info.si_addr = ip; |
1da177e4 LT |
995 | /* |
996 | * (~cwd & swd) will mask out exceptions that are not set to unmasked | |
997 | * status. 0x3f is the exception bits in these regs, 0x200 is the | |
998 | * C1 reg you need in case of a stack fault, 0x040 is the stack | |
999 | * fault bit. We should only be taking one exception at a time, | |
1000 | * so if this combination doesn't produce any single exception, | |
1001 | * then we have a bad program that isn't synchronizing its FPU usage | |
1002 | * and it will suffer the consequences since we won't be able to | |
1003 | * fully reproduce the context of the exception | |
1004 | */ | |
1005 | cwd = get_fpu_cwd(task); | |
1006 | swd = get_fpu_swd(task); | |
ff347b22 | 1007 | switch (swd & ~cwd & 0x3f) { |
a8c1be9d AH |
1008 | case 0x000: /* No unmasked exception */ |
1009 | default: /* Multiple exceptions */ | |
1010 | break; | |
1011 | case 0x001: /* Invalid Op */ | |
1012 | /* | |
1013 | * swd & 0x240 == 0x040: Stack Underflow | |
1014 | * swd & 0x240 == 0x240: Stack Overflow | |
1015 | * User must clear the SF bit (0x40) if set | |
1016 | */ | |
1017 | info.si_code = FPE_FLTINV; | |
1018 | break; | |
1019 | case 0x002: /* Denormalize */ | |
1020 | case 0x010: /* Underflow */ | |
1021 | info.si_code = FPE_FLTUND; | |
1022 | break; | |
1023 | case 0x004: /* Zero Divide */ | |
1024 | info.si_code = FPE_FLTDIV; | |
1025 | break; | |
1026 | case 0x008: /* Overflow */ | |
1027 | info.si_code = FPE_FLTOVF; | |
1028 | break; | |
1029 | case 0x020: /* Precision */ | |
1030 | info.si_code = FPE_FLTRES; | |
1031 | break; | |
1da177e4 LT |
1032 | } |
1033 | force_sig_info(SIGFPE, &info, task); | |
1034 | } | |
1035 | ||
1036 | asmlinkage void bad_intr(void) | |
1037 | { | |
1038 | printk("bad interrupt"); | |
1039 | } | |
1040 | ||
1041 | asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs) | |
1042 | { | |
65ea5b03 | 1043 | void __user *ip = (void __user *)(regs->ip); |
a8c1be9d | 1044 | struct task_struct *task; |
1da177e4 LT |
1045 | siginfo_t info; |
1046 | unsigned short mxcsr; | |
1047 | ||
1048 | conditional_sti(regs); | |
76381fee | 1049 | if (!user_mode(regs) && |
6e3f3617 | 1050 | kernel_math_error(regs, "kernel simd math error", 19)) |
1da177e4 LT |
1051 | return; |
1052 | ||
1053 | /* | |
1054 | * Save the info for the exception handler and clear the error. | |
1055 | */ | |
1056 | task = current; | |
1057 | save_init_fpu(task); | |
1058 | task->thread.trap_no = 19; | |
1059 | task->thread.error_code = 0; | |
1060 | info.si_signo = SIGFPE; | |
1061 | info.si_errno = 0; | |
1062 | info.si_code = __SI_FAULT; | |
65ea5b03 | 1063 | info.si_addr = ip; |
1da177e4 LT |
1064 | /* |
1065 | * The SIMD FPU exceptions are handled a little differently, as there | |
1066 | * is only a single status/control register. Thus, to determine which | |
1067 | * unmasked exception was caught we must mask the exception mask bits | |
1068 | * at 0x1f80, and then use these to mask the exception bits at 0x3f. | |
1069 | */ | |
1070 | mxcsr = get_fpu_mxcsr(task); | |
1071 | switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) { | |
a8c1be9d AH |
1072 | case 0x000: |
1073 | default: | |
1074 | break; | |
1075 | case 0x001: /* Invalid Op */ | |
1076 | info.si_code = FPE_FLTINV; | |
1077 | break; | |
1078 | case 0x002: /* Denormalize */ | |
1079 | case 0x010: /* Underflow */ | |
1080 | info.si_code = FPE_FLTUND; | |
1081 | break; | |
1082 | case 0x004: /* Zero Divide */ | |
1083 | info.si_code = FPE_FLTDIV; | |
1084 | break; | |
1085 | case 0x008: /* Overflow */ | |
1086 | info.si_code = FPE_FLTOVF; | |
1087 | break; | |
1088 | case 0x020: /* Precision */ | |
1089 | info.si_code = FPE_FLTRES; | |
1090 | break; | |
1da177e4 LT |
1091 | } |
1092 | force_sig_info(SIGFPE, &info, task); | |
1093 | } | |
1094 | ||
1095 | asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs) | |
1096 | { | |
1097 | } | |
1098 | ||
1099 | asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void) | |
89b831ef JS |
1100 | { |
1101 | } | |
1102 | ||
1103 | asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void) | |
1da177e4 LT |
1104 | { |
1105 | } | |
1106 | ||
1107 | /* | |
a8c1be9d | 1108 | * 'math_state_restore()' saves the current math information in the |
1da177e4 LT |
1109 | * old math state array, and gets the new ones from the current task |
1110 | * | |
1111 | * Careful.. There are problems with IBM-designed IRQ13 behaviour. | |
1112 | * Don't touch unless you *really* know how it works. | |
1113 | */ | |
1114 | asmlinkage void math_state_restore(void) | |
1115 | { | |
1116 | struct task_struct *me = current; | |
1da177e4 | 1117 | |
aa283f49 SS |
1118 | if (!used_math()) { |
1119 | local_irq_enable(); | |
1120 | /* | |
1121 | * does a slab alloc which can sleep | |
1122 | */ | |
1123 | if (init_fpu(me)) { | |
1124 | /* | |
1125 | * ran out of memory! | |
1126 | */ | |
1127 | do_group_exit(SIGKILL); | |
1128 | return; | |
1129 | } | |
1130 | local_irq_disable(); | |
1131 | } | |
1132 | ||
a8c1be9d | 1133 | clts(); /* Allow maths ops (or we recurse) */ |
61c4628b | 1134 | restore_fpu_checking(&me->thread.xstate->fxsave); |
e4f17c43 | 1135 | task_thread_info(me)->status |= TS_USEDFPU; |
e07e23e1 | 1136 | me->fpu_counter++; |
1da177e4 | 1137 | } |
21db5584 | 1138 | EXPORT_SYMBOL_GPL(math_state_restore); |
1da177e4 | 1139 | |
1da177e4 LT |
1140 | void __init trap_init(void) |
1141 | { | |
a8c1be9d AH |
1142 | set_intr_gate(0, ÷_error); |
1143 | set_intr_gate_ist(1, &debug, DEBUG_STACK); | |
1144 | set_intr_gate_ist(2, &nmi, NMI_STACK); | |
1145 | set_system_gate_ist(3, &int3, DEBUG_STACK); /* int3 can be called from all */ | |
1146 | set_system_gate(4, &overflow); /* int4 can be called from all */ | |
1147 | set_intr_gate(5, &bounds); | |
1148 | set_intr_gate(6, &invalid_op); | |
1149 | set_intr_gate(7, &device_not_available); | |
1150 | set_intr_gate_ist(8, &double_fault, DOUBLEFAULT_STACK); | |
1151 | set_intr_gate(9, &coprocessor_segment_overrun); | |
1152 | set_intr_gate(10, &invalid_TSS); | |
1153 | set_intr_gate(11, &segment_not_present); | |
1154 | set_intr_gate_ist(12, &stack_segment, STACKFAULT_STACK); | |
1155 | set_intr_gate(13, &general_protection); | |
1156 | set_intr_gate(14, &page_fault); | |
1157 | set_intr_gate(15, &spurious_interrupt_bug); | |
1158 | set_intr_gate(16, &coprocessor_error); | |
1159 | set_intr_gate(17, &alignment_check); | |
1da177e4 | 1160 | #ifdef CONFIG_X86_MCE |
a8c1be9d | 1161 | set_intr_gate_ist(18, &machine_check, MCE_STACK); |
1da177e4 | 1162 | #endif |
a8c1be9d | 1163 | set_intr_gate(19, &simd_coprocessor_error); |
1da177e4 LT |
1164 | |
1165 | #ifdef CONFIG_IA32_EMULATION | |
1166 | set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall); | |
1167 | #endif | |
61c4628b SS |
1168 | /* |
1169 | * initialize the per thread extended state: | |
1170 | */ | |
7b4fd4bb | 1171 | init_thread_xstate(); |
1da177e4 | 1172 | /* |
a8c1be9d | 1173 | * Should be a barrier for any external CPU state: |
1da177e4 LT |
1174 | */ |
1175 | cpu_init(); | |
1176 | } | |
1177 | ||
2c8c0e6b | 1178 | static int __init oops_setup(char *s) |
a8c1be9d | 1179 | { |
2c8c0e6b AK |
1180 | if (!s) |
1181 | return -EINVAL; | |
1182 | if (!strcmp(s, "panic")) | |
1183 | panic_on_oops = 1; | |
1184 | return 0; | |
a8c1be9d | 1185 | } |
2c8c0e6b | 1186 | early_param("oops", oops_setup); |
1da177e4 LT |
1187 | |
1188 | static int __init kstack_setup(char *s) | |
1189 | { | |
2c8c0e6b AK |
1190 | if (!s) |
1191 | return -EINVAL; | |
a8c1be9d | 1192 | kstack_depth_to_print = simple_strtoul(s, NULL, 0); |
2c8c0e6b | 1193 | return 0; |
1da177e4 | 1194 | } |
2c8c0e6b | 1195 | early_param("kstack", kstack_setup); |
a25bd949 | 1196 | |
a25bd949 AV |
1197 | static int __init code_bytes_setup(char *s) |
1198 | { | |
1199 | code_bytes = simple_strtoul(s, NULL, 0); | |
1200 | if (code_bytes > 8192) | |
1201 | code_bytes = 8192; | |
1202 | ||
1203 | return 1; | |
1204 | } | |
1205 | __setup("code_bytes=", code_bytes_setup); |