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