blob: a665c71352b84359d01863bfd3e4864ab28ea85f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +02003 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Pentium III FXSR, SSE support
6 * Gareth Hughes <gareth@valinux.com>, May 2000
7 */
8
9/*
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020010 * Handle hardware traps and faults.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kallsyms.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010014#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kprobes.h>
Andrew Morton1e2af922006-09-27 01:51:15 -070016#include <linux/uaccess.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010017#include <linux/kdebug.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/ptrace.h>
21#include <linux/string.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010022#include <linux/delay.h>
23#include <linux/errno.h>
24#include <linux/kexec.h>
25#include <linux/sched.h>
26#include <linux/timer.h>
27#include <linux/init.h>
Jeremy Fitzhardinge91768d62006-12-08 02:36:21 -080028#include <linux/bug.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010029#include <linux/nmi.h>
30#include <linux/mm.h>
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020031#include <linux/smp.h>
32#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#ifdef CONFIG_EISA
35#include <linux/ioport.h>
36#include <linux/eisa.h>
37#endif
38
39#ifdef CONFIG_MCA
40#include <linux/mca.h>
41#endif
42
Dave Jiangc0d12172007-07-19 01:49:46 -070043#if defined(CONFIG_EDAC)
44#include <linux/edac.h>
45#endif
46
Vegard Nossumf8561292008-04-04 00:53:23 +020047#include <asm/kmemcheck.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010048#include <asm/stacktrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/debugreg.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010051#include <asm/atomic.h>
52#include <asm/system.h>
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020053#include <asm/traps.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/desc.h>
55#include <asm/i387.h>
Hidetoshi Seto9e55e442009-06-15 17:22:15 +090056#include <asm/mce.h>
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020057
Ingo Molnar1164dd02009-01-28 19:34:09 +010058#include <asm/mach_traps.h>
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020059
Alexander van Heukelum081f75b2008-10-03 22:00:39 +020060#ifdef CONFIG_X86_64
Thomas Gleixner428cf902009-08-20 10:35:46 +020061#include <asm/x86_init.h>
Alexander van Heukelum081f75b2008-10-03 22:00:39 +020062#include <asm/pgalloc.h>
63#include <asm/proto.h>
Alexander van Heukelum081f75b2008-10-03 22:00:39 +020064#else
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020065#include <asm/processor-flags.h>
Ingo Molnar8e6dafd2009-02-23 00:34:39 +010066#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068asmlinkage int system_call(void);
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/* Do we ignore FPU interrupts ? */
Ingo Molnarb5964402008-02-26 11:15:50 +010071char ignore_fpu_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
74 * The IDT has to be page-aligned to simplify the Pentium
75 * F0 0F bug workaround.. We have a special link segment
76 * for this.
77 */
Cyrill Gorcunov9ff80942009-07-08 22:03:53 +040078gate_desc idt_table[NR_VECTORS]
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +010079 __attribute__((__section__(".data.idt"))) = { { { { 0, 0 } } }, };
Alexander van Heukelum081f75b2008-10-03 22:00:39 +020080#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Yinghai Lub77b8812008-12-19 15:23:44 -080082DECLARE_BITMAP(used_vectors, NR_VECTORS);
83EXPORT_SYMBOL_GPL(used_vectors);
84
Alexander van Heukelumbadc7652008-07-02 01:30:30 +020085static int ignore_nmis;
Alan Sterne041c682006-03-27 01:16:30 -080086
Alexander van Heukelum762db432008-09-09 21:55:55 +020087static inline void conditional_sti(struct pt_regs *regs)
88{
89 if (regs->flags & X86_EFLAGS_IF)
90 local_irq_enable();
91}
92
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +020093static inline void preempt_conditional_sti(struct pt_regs *regs)
94{
95 inc_preempt_count();
96 if (regs->flags & X86_EFLAGS_IF)
97 local_irq_enable();
98}
99
Thomas Gleixnerbe716612009-01-13 23:36:34 +0100100static inline void conditional_cli(struct pt_regs *regs)
101{
102 if (regs->flags & X86_EFLAGS_IF)
103 local_irq_disable();
104}
105
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200106static inline void preempt_conditional_cli(struct pt_regs *regs)
107{
108 if (regs->flags & X86_EFLAGS_IF)
109 local_irq_disable();
110 dec_preempt_count();
111}
112
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200113#ifdef CONFIG_X86_32
Ingo Molnarb5964402008-02-26 11:15:50 +0100114static inline void
115die_if_kernel(const char *str, struct pt_regs *regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Vincent Hanquez717b5942005-06-23 00:08:45 -0700117 if (!user_mode_vm(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 die(str, regs, err);
119}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200120#endif
Alexander van Heukelumae821572008-09-30 18:41:35 +0200121
Ingo Molnarb5964402008-02-26 11:15:50 +0100122static void __kprobes
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200123do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
Ingo Molnarb5964402008-02-26 11:15:50 +0100124 long error_code, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700126 struct task_struct *tsk = current;
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700127
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200128#ifdef CONFIG_X86_32
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300129 if (regs->flags & X86_VM_MASK) {
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200130 /*
131 * traps 0, 1, 3, 4, and 5 should be forwarded to vm86.
132 * On nmi (interrupt 2), do_trap should not be called.
133 */
134 if (trapnr < 6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 goto vm86_trap;
136 goto trap_signal;
137 }
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200138#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Vincent Hanquez717b5942005-06-23 00:08:45 -0700140 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 goto kernel_trap;
142
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200143#ifdef CONFIG_X86_32
Ingo Molnarb5964402008-02-26 11:15:50 +0100144trap_signal:
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200145#endif
Ingo Molnarb5964402008-02-26 11:15:50 +0100146 /*
147 * We want error_code and trap_no set for userspace faults and
148 * kernelspace faults which result in die(), but not
149 * kernelspace faults which are fixed up. die() gives the
150 * process no chance to handle the signal and notice the
151 * kernel fault information, so that won't result in polluting
152 * the information about previously queued, but not yet
153 * delivered, faults. See also do_general_protection below.
154 */
155 tsk->thread.error_code = error_code;
156 tsk->thread.trap_no = trapnr;
157
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200158#ifdef CONFIG_X86_64
159 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
160 printk_ratelimit()) {
161 printk(KERN_INFO
162 "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
163 tsk->comm, tsk->pid, str,
164 regs->ip, regs->sp, error_code);
165 print_vma_addr(" in ", regs->ip);
166 printk("\n");
167 }
168#endif
169
Ingo Molnarb5964402008-02-26 11:15:50 +0100170 if (info)
171 force_sig_info(signr, info, tsk);
172 else
173 force_sig(signr, tsk);
174 return;
175
176kernel_trap:
177 if (!fixup_exception(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200178 tsk->thread.error_code = error_code;
179 tsk->thread.trap_no = trapnr;
Ingo Molnarb5964402008-02-26 11:15:50 +0100180 die(str, regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
Ingo Molnarb5964402008-02-26 11:15:50 +0100182 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200184#ifdef CONFIG_X86_32
Ingo Molnarb5964402008-02-26 11:15:50 +0100185vm86_trap:
186 if (handle_vm86_trap((struct kernel_vm86_regs *) regs,
187 error_code, trapnr))
188 goto trap_signal;
189 return;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200190#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Ingo Molnarb5964402008-02-26 11:15:50 +0100193#define DO_ERROR(trapnr, signr, str, name) \
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200194dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100195{ \
196 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200197 == NOTIFY_STOP) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100198 return; \
Alexander van Heukelum61aef7d2008-09-09 21:55:56 +0200199 conditional_sti(regs); \
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200200 do_trap(trapnr, signr, str, regs, error_code, NULL); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200203#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200204dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100205{ \
206 siginfo_t info; \
207 info.si_signo = signr; \
208 info.si_errno = 0; \
209 info.si_code = sicode; \
210 info.si_addr = (void __user *)siaddr; \
Ingo Molnarb5964402008-02-26 11:15:50 +0100211 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200212 == NOTIFY_STOP) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100213 return; \
Alexander van Heukelum61aef7d2008-09-09 21:55:56 +0200214 conditional_sti(regs); \
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200215 do_trap(trapnr, signr, str, regs, error_code, &info); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200218DO_ERROR_INFO(0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
219DO_ERROR(4, SIGSEGV, "overflow", overflow)
220DO_ERROR(5, SIGSEGV, "bounds", bounds)
221DO_ERROR_INFO(6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
Alexander van Heukelum51bc1ed2008-09-09 21:56:03 +0200222DO_ERROR(9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
Alexander van Heukelum6bf77bf2008-09-09 21:56:04 +0200223DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
Alexander van Heukelum36d936c2008-09-09 21:56:05 +0200224DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200225#ifdef CONFIG_X86_32
Alexander van Heukelumf5ca8182008-09-09 21:56:06 +0200226DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200227#endif
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200228DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200230#ifdef CONFIG_X86_64
231/* Runs on IST stack */
232dotraplinkage void do_stack_segment(struct pt_regs *regs, long error_code)
233{
234 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
235 12, SIGBUS) == NOTIFY_STOP)
236 return;
237 preempt_conditional_sti(regs);
238 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
239 preempt_conditional_cli(regs);
240}
241
242dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code)
243{
244 static const char str[] = "double fault";
245 struct task_struct *tsk = current;
246
247 /* Return not checked because double check cannot be ignored */
248 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
249
250 tsk->thread.error_code = error_code;
251 tsk->thread.trap_no = 8;
252
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100253 /*
254 * This is always a kernel trap and never fixable (and thus must
255 * never return).
256 */
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200257 for (;;)
258 die(str, regs, error_code);
259}
260#endif
261
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200262dotraplinkage void __kprobes
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200263do_general_protection(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200265 struct task_struct *tsk;
Ingo Molnarb5964402008-02-26 11:15:50 +0100266
Alexander van Heukelumc6df0d72008-09-09 21:56:07 +0200267 conditional_sti(regs);
268
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200269#ifdef CONFIG_X86_32
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300270 if (regs->flags & X86_VM_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 goto gp_in_vm86;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200272#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200274 tsk = current;
Vincent Hanquez717b5942005-06-23 00:08:45 -0700275 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 goto gp_in_kernel;
277
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200278 tsk->thread.error_code = error_code;
279 tsk->thread.trap_no = 13;
Ingo Molnarb5964402008-02-26 11:15:50 +0100280
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200281 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
282 printk_ratelimit()) {
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200283 printk(KERN_INFO
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200284 "%s[%d] general protection ip:%lx sp:%lx error:%lx",
285 tsk->comm, task_pid_nr(tsk),
286 regs->ip, regs->sp, error_code);
Andi Kleen03252912008-01-30 13:33:18 +0100287 print_vma_addr(" in ", regs->ip);
288 printk("\n");
289 }
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200290
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200291 force_sig(SIGSEGV, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return;
293
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200294#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295gp_in_vm86:
296 local_irq_enable();
297 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
298 return;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200299#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301gp_in_kernel:
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200302 if (fixup_exception(regs))
303 return;
304
305 tsk->thread.error_code = error_code;
306 tsk->thread.trap_no = 13;
307 if (notify_die(DIE_GPF, "general protection fault", regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 error_code, 13, SIGSEGV) == NOTIFY_STOP)
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200309 return;
310 die("general protection fault", regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200313static notrace __kprobes void
Ingo Molnarb5964402008-02-26 11:15:50 +0100314mem_parity_error(unsigned char reason, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Ingo Molnarb5964402008-02-26 11:15:50 +0100316 printk(KERN_EMERG
317 "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
318 reason, smp_processor_id());
319
320 printk(KERN_EMERG
321 "You have some hardware problem, likely on the PCI bus.\n");
Dave Jiangc0d12172007-07-19 01:49:46 -0700322
323#if defined(CONFIG_EDAC)
Ingo Molnarb5964402008-02-26 11:15:50 +0100324 if (edac_handler_set()) {
Dave Jiangc0d12172007-07-19 01:49:46 -0700325 edac_atomic_assert_error();
326 return;
327 }
328#endif
329
Don Zickus8da5adda2006-09-26 10:52:27 +0200330 if (panic_on_unrecovered_nmi)
Ingo Molnarb5964402008-02-26 11:15:50 +0100331 panic("NMI: Not continuing");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Don Zickusc41c5cd2006-09-26 10:52:27 +0200333 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 /* Clear and disable the memory parity error line. */
Alexander van Heukelum79704792008-10-03 22:00:36 +0200336 reason = (reason & 0xf) | 4;
337 outb(reason, 0x61);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200340static notrace __kprobes void
Ingo Molnarb5964402008-02-26 11:15:50 +0100341io_check_error(unsigned char reason, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 unsigned long i;
344
Dave Jones9c107802006-01-09 20:51:32 -0800345 printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 show_registers(regs);
347
Kurt Garloff5211a242009-06-24 14:32:11 -0700348 if (panic_on_io_nmi)
349 panic("NMI IOCK error: Not continuing");
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /* Re-enable the IOCK line, wait for a few seconds */
352 reason = (reason & 0xf) | 8;
353 outb(reason, 0x61);
Ingo Molnarb5964402008-02-26 11:15:50 +0100354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 i = 2000;
Ingo Molnarb5964402008-02-26 11:15:50 +0100356 while (--i)
357 udelay(1000);
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 reason &= ~8;
360 outb(reason, 0x61);
361}
362
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200363static notrace __kprobes void
Ingo Molnarb5964402008-02-26 11:15:50 +0100364unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200366 if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) ==
367 NOTIFY_STOP)
Jason Wesseld3597522008-02-15 14:55:53 -0600368 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369#ifdef CONFIG_MCA
Ingo Molnarb5964402008-02-26 11:15:50 +0100370 /*
371 * Might actually be able to figure out what the guilty party
372 * is:
373 */
374 if (MCA_bus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 mca_handle_nmi();
376 return;
377 }
378#endif
Ingo Molnarb5964402008-02-26 11:15:50 +0100379 printk(KERN_EMERG
380 "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
381 reason, smp_processor_id());
382
Don Zickusc41c5cd2006-09-26 10:52:27 +0200383 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
Don Zickus8da5adda2006-09-26 10:52:27 +0200384 if (panic_on_unrecovered_nmi)
Ingo Molnarb5964402008-02-26 11:15:50 +0100385 panic("NMI: Not continuing");
Don Zickus8da5adda2006-09-26 10:52:27 +0200386
Don Zickusc41c5cd2006-09-26 10:52:27 +0200387 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200390static notrace __kprobes void default_do_nmi(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 unsigned char reason = 0;
Alexander van Heukelumabd34802008-07-02 18:39:01 +0200393 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Alexander van Heukelumabd34802008-07-02 18:39:01 +0200395 cpu = smp_processor_id();
396
397 /* Only the BSP gets external NMIs from the system. */
398 if (!cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 reason = get_nmi_reason();
Ingo Molnarb5964402008-02-26 11:15:50 +0100400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (!(reason & 0xc0)) {
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800402 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200403 == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return;
405#ifdef CONFIG_X86_LOCAL_APIC
406 /*
407 * Ok, so this is none of the documented NMI sources,
408 * so it must be the NMI watchdog.
409 */
Don Zickus3adbbcc2006-09-26 10:52:26 +0200410 if (nmi_watchdog_tick(regs, reason))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return;
Alexander van Heukelumabd34802008-07-02 18:39:01 +0200412 if (!do_nmi_callback(regs, cpu))
Don Zickus3adbbcc2006-09-26 10:52:26 +0200413 unknown_nmi_error(reason, regs);
Ingo Molnarb5964402008-02-26 11:15:50 +0100414#else
415 unknown_nmi_error(reason, regs);
416#endif
Don Zickus2fbe7b22006-09-26 10:52:27 +0200417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return;
419 }
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800420 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return;
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200422
423 /* AK: following checks seem to be broken on modern chipsets. FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (reason & 0x80)
425 mem_parity_error(reason, regs);
426 if (reason & 0x40)
427 io_check_error(reason, regs);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200428#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /*
430 * Reassert NMI in case it became active meanwhile
Ingo Molnarb5964402008-02-26 11:15:50 +0100431 * as it's edge-triggered:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 */
433 reassert_nmi();
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200434#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200437dotraplinkage notrace __kprobes void
438do_nmi(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 nmi_enter();
441
Hiroshi Shimamoto915b0d02008-12-08 19:19:26 -0800442 inc_irq_stat(__nmi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Andi Kleen8f4e9562007-07-22 11:12:32 +0200444 if (!ignore_nmis)
445 default_do_nmi(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 nmi_exit();
448}
449
Andi Kleen8f4e9562007-07-22 11:12:32 +0200450void stop_nmi(void)
451{
452 acpi_nmi_disable();
453 ignore_nmis++;
454}
455
456void restart_nmi(void)
457{
458 ignore_nmis--;
459 acpi_nmi_enable();
460}
461
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200462/* May run on IST stack. */
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200463dotraplinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Alexander van Heukelumb94da1e2008-09-09 21:55:58 +0200465#ifdef CONFIG_KPROBES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
467 == NOTIFY_STOP)
Stas Sergeev48c882112005-05-01 08:58:49 -0700468 return;
Alexander van Heukelumb94da1e2008-09-09 21:55:58 +0200469#else
470 if (notify_die(DIE_TRAP, "int3", regs, error_code, 3, SIGTRAP)
471 == NOTIFY_STOP)
472 return;
473#endif
Ingo Molnarb5964402008-02-26 11:15:50 +0100474
Alexander van Heukelum4915a352008-10-03 22:00:34 +0200475 preempt_conditional_sti(regs);
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200476 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
Alexander van Heukelum4915a352008-10-03 22:00:34 +0200477 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200480#ifdef CONFIG_X86_64
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100481/*
482 * Help handler running on IST stack to switch back to user stack
483 * for scheduling or signal handling. The actual stack switch is done in
484 * entry.S
485 */
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200486asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
487{
488 struct pt_regs *regs = eregs;
489 /* Did already sync */
490 if (eregs == (struct pt_regs *)eregs->sp)
491 ;
492 /* Exception from user space */
493 else if (user_mode(eregs))
494 regs = task_pt_regs(current);
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100495 /*
496 * Exception from kernel and interrupts are enabled. Move to
497 * kernel process stack.
498 */
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200499 else if (eregs->flags & X86_EFLAGS_IF)
500 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
501 if (eregs != regs)
502 *regs = *eregs;
503 return regs;
504}
505#endif
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/*
508 * Our handling of the processor debug registers is non-trivial.
509 * We do not clear them on entry and exit from the kernel. Therefore
510 * it is possible to get a watchpoint trap here from inside the kernel.
511 * However, the code in ./ptrace.c has ensured that the user can
512 * only set watchpoints on userspace addresses. Therefore the in-kernel
513 * watchpoint trap can only occur in code which is reading/writing
514 * from user space. Such code must not hold kernel locks (since it
515 * can equally take a page fault), therefore it is safe to call
516 * force_sig_info even though that claims and releases locks.
Ingo Molnarb5964402008-02-26 11:15:50 +0100517 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 * Code in ./signal.c ensures that the debug control register
519 * is restored before we deliver any signal, and therefore that
520 * user code runs with the correct debug control register even though
521 * we clear it here.
522 *
523 * Being careful here means that we don't have to be as careful in a
524 * lot of more complicated places (task switching can be a bit lazy
525 * about restoring all the debug state, and ptrace doesn't have to
526 * find every occurrence of the TF bit that could be saved away even
527 * by user code)
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200528 *
529 * May run on IST stack.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 */
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200531dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 struct task_struct *tsk = current;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200534 unsigned long condition;
Srinivasa Dsda654b72008-09-23 15:23:52 +0530535 int si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Vincent Hanquez1cc6f122005-06-23 00:08:43 -0700537 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Vegard Nossumf8561292008-04-04 00:53:23 +0200539 /* Catch kmemcheck conditions first of all! */
540 if (condition & DR_STEP && kmemcheck_trap(regs))
541 return;
542
Roland McGrath10faa812008-01-30 13:30:54 +0100543 /*
544 * The processor cleared BTF, so don't mark that we need it set.
545 */
546 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
547 tsk->thread.debugctlmsr = 0;
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200550 SIGTRAP) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* It's safe to allow irq's after DR6 has been saved */
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200554 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 /* Mask out spurious debug traps due to lazy DR7 setting */
557 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
Roland McGrath0f534092008-01-30 13:30:59 +0100558 if (!tsk->thread.debugreg7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 goto clear_dr7;
560 }
561
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200562#ifdef CONFIG_X86_32
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300563 if (regs->flags & X86_VM_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 goto debug_vm86;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200565#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 /* Save debug status register where ptrace can see it */
Roland McGrath0f534092008-01-30 13:30:59 +0100568 tsk->thread.debugreg6 = condition;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 /*
571 * Single-stepping through TF: make sure we ignore any events in
572 * kernel space (but re-enable TF when returning to user mode).
573 */
574 if (condition & DR_STEP) {
Vincent Hanquez717b5942005-06-23 00:08:45 -0700575 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 goto clear_TF_reenable;
577 }
578
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200579 si_code = get_si_code(condition);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /* Ok, finally something we can handle */
Srinivasa Dsda654b72008-09-23 15:23:52 +0530581 send_sigtrap(tsk, regs, error_code, si_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Ingo Molnarb5964402008-02-26 11:15:50 +0100583 /*
584 * Disable additional traps. They'll be re-enabled when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 * the signal is delivered.
586 */
587clear_dr7:
Vincent Hanquez1cc6f122005-06-23 00:08:43 -0700588 set_debugreg(0, 7);
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200589 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return;
591
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200592#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593debug_vm86:
Thomas Gleixnerbe716612009-01-13 23:36:34 +0100594 /* reenable preemption: handle_vm86_trap() might sleep */
595 dec_preempt_count();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
Thomas Gleixnerbe716612009-01-13 23:36:34 +0100597 conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200599#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601clear_TF_reenable:
602 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
Ingo Molnar60930152008-03-30 11:45:23 +0200603 regs->flags &= ~X86_EFLAGS_TF;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200604 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return;
606}
607
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200608#ifdef CONFIG_X86_64
609static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
610{
611 if (fixup_exception(regs))
612 return 1;
613
614 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
615 /* Illegal floating point operation in the kernel */
616 current->thread.trap_no = trapnr;
617 die(str, regs, 0);
618 return 0;
619}
620#endif
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622/*
623 * Note that we play around with the 'TS' bit in an attempt to get
624 * the correct behaviour even in the presence of the asynchronous
625 * IRQ13 behaviour
626 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100627void math_error(void __user *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Ingo Molnarb5964402008-02-26 11:15:50 +0100629 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 siginfo_t info;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800631 unsigned short cwd, swd, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 /*
634 * Save the info for the exception handler and clear the error.
635 */
636 task = current;
637 save_init_fpu(task);
638 task->thread.trap_no = 16;
639 task->thread.error_code = 0;
640 info.si_signo = SIGFPE;
641 info.si_errno = 0;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100642 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /*
644 * (~cwd & swd) will mask out exceptions that are not set to unmasked
645 * status. 0x3f is the exception bits in these regs, 0x200 is the
646 * C1 reg you need in case of a stack fault, 0x040 is the stack
647 * fault bit. We should only be taking one exception at a time,
648 * so if this combination doesn't produce any single exception,
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200649 * then we have a bad program that isn't synchronizing its FPU usage
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 * and it will suffer the consequences since we won't be able to
651 * fully reproduce the context of the exception
652 */
653 cwd = get_fpu_cwd(task);
654 swd = get_fpu_swd(task);
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800655
H. Peter Anvina73ad332008-12-25 10:39:01 -0800656 err = swd & ~cwd;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800657
658 if (err & 0x001) { /* Invalid op */
Ingo Molnarb5964402008-02-26 11:15:50 +0100659 /*
660 * swd & 0x240 == 0x040: Stack Underflow
661 * swd & 0x240 == 0x240: Stack Overflow
662 * User must clear the SF bit (0x40) if set
663 */
664 info.si_code = FPE_FLTINV;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800665 } else if (err & 0x004) { /* Divide by Zero */
Ingo Molnarb5964402008-02-26 11:15:50 +0100666 info.si_code = FPE_FLTDIV;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800667 } else if (err & 0x008) { /* Overflow */
Ingo Molnarb5964402008-02-26 11:15:50 +0100668 info.si_code = FPE_FLTOVF;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800669 } else if (err & 0x012) { /* Denormal, Underflow */
670 info.si_code = FPE_FLTUND;
671 } else if (err & 0x020) { /* Precision */
Ingo Molnarb5964402008-02-26 11:15:50 +0100672 info.si_code = FPE_FLTRES;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800673 } else {
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100674 /*
675 * If we're using IRQ 13, or supposedly even some trap 16
676 * implementations, it's possible we get a spurious trap...
677 */
H. Peter Anvina73ad332008-12-25 10:39:01 -0800678 return; /* Spurious trap, no error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680 force_sig_info(SIGFPE, &info, task);
681}
682
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200683dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Alexander van Heukelum252d28f2008-09-09 21:56:09 +0200685 conditional_sti(regs);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200686
687#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 ignore_fpu_irq = 1;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200689#else
690 if (!user_mode(regs) &&
691 kernel_math_error(regs, "kernel x87 math error", 16))
692 return;
693#endif
694
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100695 math_error((void __user *)regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100698static void simd_math_error(void __user *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Ingo Molnarb5964402008-02-26 11:15:50 +0100700 struct task_struct *task;
Ingo Molnarb5964402008-02-26 11:15:50 +0100701 siginfo_t info;
Alexander van Heukelum7b4fd4b2008-07-02 01:33:14 +0200702 unsigned short mxcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 /*
705 * Save the info for the exception handler and clear the error.
706 */
707 task = current;
708 save_init_fpu(task);
709 task->thread.trap_no = 19;
710 task->thread.error_code = 0;
711 info.si_signo = SIGFPE;
712 info.si_errno = 0;
713 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100714 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 /*
716 * The SIMD FPU exceptions are handled a little differently, as there
717 * is only a single status/control register. Thus, to determine which
718 * unmasked exception was caught we must mask the exception mask bits
719 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
720 */
721 mxcsr = get_fpu_mxcsr(task);
722 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
Ingo Molnarb5964402008-02-26 11:15:50 +0100723 case 0x000:
724 default:
725 break;
726 case 0x001: /* Invalid Op */
727 info.si_code = FPE_FLTINV;
728 break;
729 case 0x002: /* Denormalize */
730 case 0x010: /* Underflow */
731 info.si_code = FPE_FLTUND;
732 break;
733 case 0x004: /* Zero Divide */
734 info.si_code = FPE_FLTDIV;
735 break;
736 case 0x008: /* Overflow */
737 info.si_code = FPE_FLTOVF;
738 break;
739 case 0x020: /* Precision */
740 info.si_code = FPE_FLTRES;
741 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743 force_sig_info(SIGFPE, &info, task);
744}
745
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200746dotraplinkage void
747do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Alexander van Heukelumb939bde2008-09-09 21:56:12 +0200749 conditional_sti(regs);
750
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200751#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (cpu_has_xmm) {
753 /* Handle SIMD FPU exceptions on PIII+ processors. */
754 ignore_fpu_irq = 1;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100755 simd_math_error((void __user *)regs->ip);
Ingo Molnarb5964402008-02-26 11:15:50 +0100756 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
Ingo Molnarb5964402008-02-26 11:15:50 +0100758 /*
759 * Handle strange cache flush from user space exception
760 * in all other cases. This is undocumented behaviour.
761 */
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300762 if (regs->flags & X86_VM_MASK) {
Ingo Molnarb5964402008-02-26 11:15:50 +0100763 handle_vm86_fault((struct kernel_vm86_regs *)regs, error_code);
764 return;
765 }
766 current->thread.trap_no = 19;
767 current->thread.error_code = error_code;
768 die_if_kernel("cache flush denied", regs, error_code);
769 force_sig(SIGSEGV, current);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200770#else
771 if (!user_mode(regs) &&
772 kernel_math_error(regs, "kernel simd math error", 19))
773 return;
774 simd_math_error((void __user *)regs->ip);
775#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200778dotraplinkage void
779do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Alexander van Heukelumcf819782008-09-09 21:56:08 +0200781 conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782#if 0
783 /* No need to warn about this any longer. */
Ingo Molnarb5964402008-02-26 11:15:50 +0100784 printk(KERN_INFO "Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785#endif
786}
787
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200788asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
789{
790}
791
Andi Kleen7856f6c2009-04-28 23:32:56 +0200792asmlinkage void __attribute__((weak)) smp_threshold_interrupt(void)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200793{
794}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796/*
Jeremy Fitzhardingee6e9cac2009-04-24 00:40:59 -0700797 * __math_state_restore assumes that cr0.TS is already clear and the
798 * fpu state is all ready for use. Used during context switch.
799 */
800void __math_state_restore(void)
801{
802 struct thread_info *thread = current_thread_info();
803 struct task_struct *tsk = thread->task;
804
805 /*
806 * Paranoid restore. send a SIGSEGV if we fail to restore the state.
807 */
808 if (unlikely(restore_fpu_checking(tsk))) {
809 stts();
810 force_sig(SIGSEGV, tsk);
811 return;
812 }
813
814 thread->status |= TS_USEDFPU; /* So we fnsave on switch_to() */
815 tsk->fpu_counter++;
816}
817
818/*
Ingo Molnarb5964402008-02-26 11:15:50 +0100819 * 'math_state_restore()' saves the current math information in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 * old math state array, and gets the new ones from the current task
821 *
822 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
823 * Don't touch unless you *really* know how it works.
824 *
825 * Must be called with kernel preemption disabled (in this case,
826 * local interrupts are disabled at the call-site in entry.S).
827 */
Chuck Ebbertacc20762006-12-07 02:14:01 +0100828asmlinkage void math_state_restore(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 struct thread_info *thread = current_thread_info();
831 struct task_struct *tsk = thread->task;
832
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700833 if (!tsk_used_math(tsk)) {
834 local_irq_enable();
835 /*
836 * does a slab alloc which can sleep
837 */
838 if (init_fpu(tsk)) {
839 /*
840 * ran out of memory!
841 */
842 do_group_exit(SIGKILL);
843 return;
844 }
845 local_irq_disable();
846 }
847
Ingo Molnarb5964402008-02-26 11:15:50 +0100848 clts(); /* Allow maths ops (or we recurse) */
Jiri Slabyfcb2ac52009-04-08 13:31:58 +0200849
Jeremy Fitzhardingee6e9cac2009-04-24 00:40:59 -0700850 __math_state_restore();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
Rusty Russell5992b6d2007-07-19 01:49:21 -0700852EXPORT_SYMBOL_GPL(math_state_restore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854#ifndef CONFIG_MATH_EMULATION
Tejun Heod3157602009-02-09 22:17:39 +0900855void math_emulate(struct math_emu_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Ingo Molnarb5964402008-02-26 11:15:50 +0100857 printk(KERN_EMERG
858 "math-emulation not enabled and no coprocessor found.\n");
859 printk(KERN_EMERG "killing %s.\n", current->comm);
860 force_sig(SIGFPE, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 schedule();
862}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863#endif /* CONFIG_MATH_EMULATION */
864
Brian Gerstaa78bcf2009-02-10 09:51:45 -0500865dotraplinkage void __kprobes
866do_device_not_available(struct pt_regs *regs, long error_code)
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200867{
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200868#ifdef CONFIG_X86_32
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200869 if (read_cr0() & X86_CR0_EM) {
Tejun Heod3157602009-02-09 22:17:39 +0900870 struct math_emu_info info = { };
871
Brian Gerstaa78bcf2009-02-10 09:51:45 -0500872 conditional_sti(regs);
Tejun Heod3157602009-02-09 22:17:39 +0900873
Brian Gerstaa78bcf2009-02-10 09:51:45 -0500874 info.regs = regs;
Tejun Heod3157602009-02-09 22:17:39 +0900875 math_emulate(&info);
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200876 } else {
877 math_state_restore(); /* interrupts still off */
Brian Gerstaa78bcf2009-02-10 09:51:45 -0500878 conditional_sti(regs);
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200879 }
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200880#else
881 math_state_restore();
882#endif
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200883}
884
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200885#ifdef CONFIG_X86_32
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200886dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200887{
888 siginfo_t info;
889 local_irq_enable();
890
891 info.si_signo = SIGILL;
892 info.si_errno = 0;
893 info.si_code = ILL_BADSTK;
Hannes Ederfc6fcdf2009-02-22 01:00:57 +0100894 info.si_addr = NULL;
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200895 if (notify_die(DIE_TRAP, "iret exception",
896 regs, error_code, 32, SIGILL) == NOTIFY_STOP)
897 return;
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200898 do_trap(32, SIGILL, "iret exception", regs, error_code, &info);
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200899}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200900#endif
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902void __init trap_init(void)
903{
Rusty Russelldbeb2be2007-10-19 20:35:03 +0200904 int i;
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906#ifdef CONFIG_EISA
Ingo Molnar927222b2008-01-30 13:33:49 +0100907 void __iomem *p = early_ioremap(0x0FFFD9, 4);
Ingo Molnarb5964402008-02-26 11:15:50 +0100908
909 if (readl(p) == 'E' + ('I'<<8) + ('S'<<16) + ('A'<<24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 EISA_bus = 1;
Ingo Molnar927222b2008-01-30 13:33:49 +0100911 early_iounmap(p, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912#endif
913
Alexander van Heukelum976382d2008-09-09 21:55:57 +0200914 set_intr_gate(0, &divide_error);
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200915 set_intr_gate_ist(1, &debug, DEBUG_STACK);
916 set_intr_gate_ist(2, &nmi, NMI_STACK);
917 /* int3 can be called from all */
918 set_system_intr_gate_ist(3, &int3, DEBUG_STACK);
919 /* int4 can be called from all */
920 set_system_intr_gate(4, &overflow);
Alexander van Heukelum64f644c2008-09-09 21:56:00 +0200921 set_intr_gate(5, &bounds);
Alexander van Heukelum12394cf2008-09-09 21:56:01 +0200922 set_intr_gate(6, &invalid_op);
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200923 set_intr_gate(7, &device_not_available);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200924#ifdef CONFIG_X86_32
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200925 set_task_gate(8, GDT_ENTRY_DOUBLEFAULT_TSS);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200926#else
927 set_intr_gate_ist(8, &double_fault, DOUBLEFAULT_STACK);
928#endif
Alexander van Heukelum51bc1ed2008-09-09 21:56:03 +0200929 set_intr_gate(9, &coprocessor_segment_overrun);
Alexander van Heukelum6bf77bf2008-09-09 21:56:04 +0200930 set_intr_gate(10, &invalid_TSS);
Alexander van Heukelum36d936c2008-09-09 21:56:05 +0200931 set_intr_gate(11, &segment_not_present);
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200932 set_intr_gate_ist(12, &stack_segment, STACKFAULT_STACK);
Alexander van Heukelumc6df0d72008-09-09 21:56:07 +0200933 set_intr_gate(13, &general_protection);
Ingo Molnarb5964402008-02-26 11:15:50 +0100934 set_intr_gate(14, &page_fault);
Alexander van Heukelumcf819782008-09-09 21:56:08 +0200935 set_intr_gate(15, &spurious_interrupt_bug);
Alexander van Heukelum252d28f2008-09-09 21:56:09 +0200936 set_intr_gate(16, &coprocessor_error);
Alexander van Heukelum5feedfd2008-09-09 21:56:10 +0200937 set_intr_gate(17, &alignment_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938#ifdef CONFIG_X86_MCE
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200939 set_intr_gate_ist(18, &machine_check, MCE_STACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940#endif
Alexander van Heukelumb939bde2008-09-09 21:56:12 +0200941 set_intr_gate(19, &simd_coprocessor_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Yinghai Lubb3f0b52009-01-25 02:38:09 -0800943 /* Reserve all the builtin and the syscall vector: */
944 for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
945 set_bit(i, used_vectors);
946
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200947#ifdef CONFIG_IA32_EMULATION
948 set_system_intr_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
Yinghai Lubb3f0b52009-01-25 02:38:09 -0800949 set_bit(IA32_SYSCALL_VECTOR, used_vectors);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200950#endif
951
952#ifdef CONFIG_X86_32
Jan Beulichd43c6e82006-01-06 00:11:49 -0800953 if (cpu_has_fxsr) {
Jan Beulichd43c6e82006-01-06 00:11:49 -0800954 printk(KERN_INFO "Enabling fast FPU save and restore... ");
955 set_in_cr4(X86_CR4_OSFXSR);
956 printk("done.\n");
957 }
958 if (cpu_has_xmm) {
Ingo Molnarb5964402008-02-26 11:15:50 +0100959 printk(KERN_INFO
960 "Enabling unmasked SIMD FPU exception support... ");
Jan Beulichd43c6e82006-01-06 00:11:49 -0800961 set_in_cr4(X86_CR4_OSXMMEXCPT);
962 printk("done.\n");
963 }
964
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200965 set_system_trap_gate(SYSCALL_VECTOR, &system_call);
Rusty Russelldbeb2be2007-10-19 20:35:03 +0200966 set_bit(SYSCALL_VECTOR, used_vectors);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200967#endif
Yinghai Lubb3f0b52009-01-25 02:38:09 -0800968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /*
Ingo Molnarb5964402008-02-26 11:15:50 +0100970 * Should be a barrier for any external CPU state:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 */
972 cpu_init();
973
Thomas Gleixner428cf902009-08-20 10:35:46 +0200974 x86_init.irqs.trap_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}