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