blob: a9e7548e17906f885f28e4763128e749c7a76481 [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/utsname.h>
18#include <linux/kdebug.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/ptrace.h>
22#include <linux/string.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010023#include <linux/delay.h>
24#include <linux/errno.h>
25#include <linux/kexec.h>
26#include <linux/sched.h>
27#include <linux/timer.h>
28#include <linux/init.h>
Jeremy Fitzhardinge91768d62006-12-08 02:36:21 -080029#include <linux/bug.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010030#include <linux/nmi.h>
31#include <linux/mm.h>
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020032#include <linux/smp.h>
33#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#ifdef CONFIG_EISA
36#include <linux/ioport.h>
37#include <linux/eisa.h>
38#endif
39
40#ifdef CONFIG_MCA
41#include <linux/mca.h>
42#endif
43
Dave Jiangc0d12172007-07-19 01:49:46 -070044#if defined(CONFIG_EDAC)
45#include <linux/edac.h>
46#endif
47
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>
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020056
57#include <mach_traps.h>
58
Alexander van Heukelum081f75b2008-10-03 22:00:39 +020059#ifdef CONFIG_X86_64
60#include <asm/pgalloc.h>
61#include <asm/proto.h>
62#include <asm/pda.h>
63#else
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020064#include <asm/processor-flags.h>
65#include <asm/arch_hooks.h>
Jaswinder Singh6ac8d512008-07-15 21:09:13 +053066#include <asm/traps.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Alexander van Heukelumeb642f62008-09-09 21:56:11 +020068#include "cpu/mcheck/mce.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70asmlinkage int system_call(void);
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* Do we ignore FPU interrupts ? */
Ingo Molnarb5964402008-02-26 11:15:50 +010073char ignore_fpu_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75/*
76 * The IDT has to be page-aligned to simplify the Pentium
77 * F0 0F bug workaround.. We have a special link segment
78 * for this.
79 */
Glauber de Oliveira Costa010d4f82008-01-30 13:31:12 +010080gate_desc idt_table[256]
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +010081 __attribute__((__section__(".data.idt"))) = { { { { 0, 0 } } }, };
Alexander van Heukelum081f75b2008-10-03 22:00:39 +020082#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Yinghai Lub77b8812008-12-19 15:23:44 -080084DECLARE_BITMAP(used_vectors, NR_VECTORS);
85EXPORT_SYMBOL_GPL(used_vectors);
86
Alexander van Heukelumbadc7652008-07-02 01:30:30 +020087static int ignore_nmis;
Alan Sterne041c682006-03-27 01:16:30 -080088
Alexander van Heukelum762db432008-09-09 21:55:55 +020089static inline void conditional_sti(struct pt_regs *regs)
90{
91 if (regs->flags & X86_EFLAGS_IF)
92 local_irq_enable();
93}
94
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +020095static inline void preempt_conditional_sti(struct pt_regs *regs)
96{
97 inc_preempt_count();
98 if (regs->flags & X86_EFLAGS_IF)
99 local_irq_enable();
100}
101
Thomas Gleixnerbe716612009-01-13 23:36:34 +0100102static inline void conditional_cli(struct pt_regs *regs)
103{
104 if (regs->flags & X86_EFLAGS_IF)
105 local_irq_disable();
106}
107
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200108static inline void preempt_conditional_cli(struct pt_regs *regs)
109{
110 if (regs->flags & X86_EFLAGS_IF)
111 local_irq_disable();
112 dec_preempt_count();
113}
114
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200115#ifdef CONFIG_X86_32
Ingo Molnarb5964402008-02-26 11:15:50 +0100116static inline void
117die_if_kernel(const char *str, struct pt_regs *regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Vincent Hanquez717b5942005-06-23 00:08:45 -0700119 if (!user_mode_vm(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 die(str, regs, err);
121}
122
Alexander van Heukelumae821572008-09-30 18:41:35 +0200123/*
124 * Perform the lazy TSS's I/O bitmap copy. If the TSS has an
125 * invalid offset set (the LAZY one) and the faulting thread has
126 * a valid I/O bitmap pointer, we copy the I/O bitmap in the TSS,
127 * we set the offset field correctly and return 1.
128 */
129static int lazy_iobitmap_copy(void)
130{
131 struct thread_struct *thread;
132 struct tss_struct *tss;
133 int cpu;
134
135 cpu = get_cpu();
136 tss = &per_cpu(init_tss, cpu);
137 thread = &current->thread;
138
139 if (tss->x86_tss.io_bitmap_base == INVALID_IO_BITMAP_OFFSET_LAZY &&
140 thread->io_bitmap_ptr) {
141 memcpy(tss->io_bitmap, thread->io_bitmap_ptr,
142 thread->io_bitmap_max);
143 /*
144 * If the previously set map was extending to higher ports
145 * than the current one, pad extra space with 0xff (no access).
146 */
147 if (thread->io_bitmap_max < tss->io_bitmap_max) {
148 memset((char *) tss->io_bitmap +
149 thread->io_bitmap_max, 0xff,
150 tss->io_bitmap_max - thread->io_bitmap_max);
151 }
152 tss->io_bitmap_max = thread->io_bitmap_max;
153 tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
154 tss->io_bitmap_owner = thread;
155 put_cpu();
156
157 return 1;
158 }
159 put_cpu();
160
161 return 0;
162}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200163#endif
Alexander van Heukelumae821572008-09-30 18:41:35 +0200164
Ingo Molnarb5964402008-02-26 11:15:50 +0100165static void __kprobes
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200166do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
Ingo Molnarb5964402008-02-26 11:15:50 +0100167 long error_code, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700169 struct task_struct *tsk = current;
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700170
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200171#ifdef CONFIG_X86_32
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300172 if (regs->flags & X86_VM_MASK) {
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200173 /*
174 * traps 0, 1, 3, 4, and 5 should be forwarded to vm86.
175 * On nmi (interrupt 2), do_trap should not be called.
176 */
177 if (trapnr < 6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 goto vm86_trap;
179 goto trap_signal;
180 }
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200181#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Vincent Hanquez717b5942005-06-23 00:08:45 -0700183 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 goto kernel_trap;
185
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200186#ifdef CONFIG_X86_32
Ingo Molnarb5964402008-02-26 11:15:50 +0100187trap_signal:
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200188#endif
Ingo Molnarb5964402008-02-26 11:15:50 +0100189 /*
190 * We want error_code and trap_no set for userspace faults and
191 * kernelspace faults which result in die(), but not
192 * kernelspace faults which are fixed up. die() gives the
193 * process no chance to handle the signal and notice the
194 * kernel fault information, so that won't result in polluting
195 * the information about previously queued, but not yet
196 * delivered, faults. See also do_general_protection below.
197 */
198 tsk->thread.error_code = error_code;
199 tsk->thread.trap_no = trapnr;
200
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200201#ifdef CONFIG_X86_64
202 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
203 printk_ratelimit()) {
204 printk(KERN_INFO
205 "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
206 tsk->comm, tsk->pid, str,
207 regs->ip, regs->sp, error_code);
208 print_vma_addr(" in ", regs->ip);
209 printk("\n");
210 }
211#endif
212
Ingo Molnarb5964402008-02-26 11:15:50 +0100213 if (info)
214 force_sig_info(signr, info, tsk);
215 else
216 force_sig(signr, tsk);
217 return;
218
219kernel_trap:
220 if (!fixup_exception(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200221 tsk->thread.error_code = error_code;
222 tsk->thread.trap_no = trapnr;
Ingo Molnarb5964402008-02-26 11:15:50 +0100223 die(str, regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Ingo Molnarb5964402008-02-26 11:15:50 +0100225 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200227#ifdef CONFIG_X86_32
Ingo Molnarb5964402008-02-26 11:15:50 +0100228vm86_trap:
229 if (handle_vm86_trap((struct kernel_vm86_regs *) regs,
230 error_code, trapnr))
231 goto trap_signal;
232 return;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200233#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Ingo Molnarb5964402008-02-26 11:15:50 +0100236#define DO_ERROR(trapnr, signr, str, name) \
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200237dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100238{ \
239 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200240 == NOTIFY_STOP) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100241 return; \
Alexander van Heukelum61aef7d2008-09-09 21:55:56 +0200242 conditional_sti(regs); \
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200243 do_trap(trapnr, signr, str, regs, error_code, NULL); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200246#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200247dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100248{ \
249 siginfo_t info; \
250 info.si_signo = signr; \
251 info.si_errno = 0; \
252 info.si_code = sicode; \
253 info.si_addr = (void __user *)siaddr; \
Ingo Molnarb5964402008-02-26 11:15:50 +0100254 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200255 == NOTIFY_STOP) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100256 return; \
Alexander van Heukelum61aef7d2008-09-09 21:55:56 +0200257 conditional_sti(regs); \
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200258 do_trap(trapnr, signr, str, regs, error_code, &info); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200261DO_ERROR_INFO(0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
262DO_ERROR(4, SIGSEGV, "overflow", overflow)
263DO_ERROR(5, SIGSEGV, "bounds", bounds)
264DO_ERROR_INFO(6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
Alexander van Heukelum51bc1ed2008-09-09 21:56:03 +0200265DO_ERROR(9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
Alexander van Heukelum6bf77bf2008-09-09 21:56:04 +0200266DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
Alexander van Heukelum36d936c2008-09-09 21:56:05 +0200267DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200268#ifdef CONFIG_X86_32
Alexander van Heukelumf5ca8182008-09-09 21:56:06 +0200269DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200270#endif
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200271DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200273#ifdef CONFIG_X86_64
274/* Runs on IST stack */
275dotraplinkage void do_stack_segment(struct pt_regs *regs, long error_code)
276{
277 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
278 12, SIGBUS) == NOTIFY_STOP)
279 return;
280 preempt_conditional_sti(regs);
281 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
282 preempt_conditional_cli(regs);
283}
284
285dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code)
286{
287 static const char str[] = "double fault";
288 struct task_struct *tsk = current;
289
290 /* Return not checked because double check cannot be ignored */
291 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
292
293 tsk->thread.error_code = error_code;
294 tsk->thread.trap_no = 8;
295
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100296 /*
297 * This is always a kernel trap and never fixable (and thus must
298 * never return).
299 */
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200300 for (;;)
301 die(str, regs, error_code);
302}
303#endif
304
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200305dotraplinkage void __kprobes
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200306do_general_protection(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200308 struct task_struct *tsk;
Ingo Molnarb5964402008-02-26 11:15:50 +0100309
Alexander van Heukelumc6df0d72008-09-09 21:56:07 +0200310 conditional_sti(regs);
311
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200312#ifdef CONFIG_X86_32
Alexander van Heukelumae821572008-09-30 18:41:35 +0200313 if (lazy_iobitmap_copy()) {
314 /* restart the faulting instruction */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return;
316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300318 if (regs->flags & X86_VM_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 goto gp_in_vm86;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200320#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200322 tsk = current;
Vincent Hanquez717b5942005-06-23 00:08:45 -0700323 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 goto gp_in_kernel;
325
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200326 tsk->thread.error_code = error_code;
327 tsk->thread.trap_no = 13;
Ingo Molnarb5964402008-02-26 11:15:50 +0100328
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200329 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
330 printk_ratelimit()) {
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200331 printk(KERN_INFO
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200332 "%s[%d] general protection ip:%lx sp:%lx error:%lx",
333 tsk->comm, task_pid_nr(tsk),
334 regs->ip, regs->sp, error_code);
Andi Kleen03252912008-01-30 13:33:18 +0100335 print_vma_addr(" in ", regs->ip);
336 printk("\n");
337 }
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200338
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200339 force_sig(SIGSEGV, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return;
341
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200342#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343gp_in_vm86:
344 local_irq_enable();
345 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
346 return;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200347#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349gp_in_kernel:
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200350 if (fixup_exception(regs))
351 return;
352
353 tsk->thread.error_code = error_code;
354 tsk->thread.trap_no = 13;
355 if (notify_die(DIE_GPF, "general protection fault", regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 error_code, 13, SIGSEGV) == NOTIFY_STOP)
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200357 return;
358 die("general protection fault", regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200361static notrace __kprobes void
Ingo Molnarb5964402008-02-26 11:15:50 +0100362mem_parity_error(unsigned char reason, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Ingo Molnarb5964402008-02-26 11:15:50 +0100364 printk(KERN_EMERG
365 "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
366 reason, smp_processor_id());
367
368 printk(KERN_EMERG
369 "You have some hardware problem, likely on the PCI bus.\n");
Dave Jiangc0d12172007-07-19 01:49:46 -0700370
371#if defined(CONFIG_EDAC)
Ingo Molnarb5964402008-02-26 11:15:50 +0100372 if (edac_handler_set()) {
Dave Jiangc0d12172007-07-19 01:49:46 -0700373 edac_atomic_assert_error();
374 return;
375 }
376#endif
377
Don Zickus8da5adda2006-09-26 10:52:27 +0200378 if (panic_on_unrecovered_nmi)
Ingo Molnarb5964402008-02-26 11:15:50 +0100379 panic("NMI: Not continuing");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Don Zickusc41c5cd2006-09-26 10:52:27 +0200381 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 /* Clear and disable the memory parity error line. */
Alexander van Heukelum79704792008-10-03 22:00:36 +0200384 reason = (reason & 0xf) | 4;
385 outb(reason, 0x61);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200388static notrace __kprobes void
Ingo Molnarb5964402008-02-26 11:15:50 +0100389io_check_error(unsigned char reason, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 unsigned long i;
392
Dave Jones9c107802006-01-09 20:51:32 -0800393 printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 show_registers(regs);
395
396 /* Re-enable the IOCK line, wait for a few seconds */
397 reason = (reason & 0xf) | 8;
398 outb(reason, 0x61);
Ingo Molnarb5964402008-02-26 11:15:50 +0100399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 i = 2000;
Ingo Molnarb5964402008-02-26 11:15:50 +0100401 while (--i)
402 udelay(1000);
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 reason &= ~8;
405 outb(reason, 0x61);
406}
407
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200408static notrace __kprobes void
Ingo Molnarb5964402008-02-26 11:15:50 +0100409unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200411 if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) ==
412 NOTIFY_STOP)
Jason Wesseld3597522008-02-15 14:55:53 -0600413 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414#ifdef CONFIG_MCA
Ingo Molnarb5964402008-02-26 11:15:50 +0100415 /*
416 * Might actually be able to figure out what the guilty party
417 * is:
418 */
419 if (MCA_bus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 mca_handle_nmi();
421 return;
422 }
423#endif
Ingo Molnarb5964402008-02-26 11:15:50 +0100424 printk(KERN_EMERG
425 "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
426 reason, smp_processor_id());
427
Don Zickusc41c5cd2006-09-26 10:52:27 +0200428 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
Don Zickus8da5adda2006-09-26 10:52:27 +0200429 if (panic_on_unrecovered_nmi)
Ingo Molnarb5964402008-02-26 11:15:50 +0100430 panic("NMI: Not continuing");
Don Zickus8da5adda2006-09-26 10:52:27 +0200431
Don Zickusc41c5cd2006-09-26 10:52:27 +0200432 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200435static notrace __kprobes void default_do_nmi(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 unsigned char reason = 0;
Alexander van Heukelumabd34802008-07-02 18:39:01 +0200438 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Alexander van Heukelumabd34802008-07-02 18:39:01 +0200440 cpu = smp_processor_id();
441
442 /* Only the BSP gets external NMIs from the system. */
443 if (!cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 reason = get_nmi_reason();
Ingo Molnarb5964402008-02-26 11:15:50 +0100445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (!(reason & 0xc0)) {
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800447 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200448 == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return;
450#ifdef CONFIG_X86_LOCAL_APIC
451 /*
452 * Ok, so this is none of the documented NMI sources,
453 * so it must be the NMI watchdog.
454 */
Don Zickus3adbbcc2006-09-26 10:52:26 +0200455 if (nmi_watchdog_tick(regs, reason))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return;
Alexander van Heukelumabd34802008-07-02 18:39:01 +0200457 if (!do_nmi_callback(regs, cpu))
Don Zickus3adbbcc2006-09-26 10:52:26 +0200458 unknown_nmi_error(reason, regs);
Ingo Molnarb5964402008-02-26 11:15:50 +0100459#else
460 unknown_nmi_error(reason, regs);
461#endif
Don Zickus2fbe7b22006-09-26 10:52:27 +0200462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return;
464 }
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800465 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return;
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200467
468 /* AK: following checks seem to be broken on modern chipsets. FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (reason & 0x80)
470 mem_parity_error(reason, regs);
471 if (reason & 0x40)
472 io_check_error(reason, regs);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200473#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /*
475 * Reassert NMI in case it became active meanwhile
Ingo Molnarb5964402008-02-26 11:15:50 +0100476 * as it's edge-triggered:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 */
478 reassert_nmi();
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200479#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200482dotraplinkage notrace __kprobes void
483do_nmi(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 nmi_enter();
486
Hiroshi Shimamoto915b0d02008-12-08 19:19:26 -0800487 inc_irq_stat(__nmi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Andi Kleen8f4e9562007-07-22 11:12:32 +0200489 if (!ignore_nmis)
490 default_do_nmi(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 nmi_exit();
493}
494
Andi Kleen8f4e9562007-07-22 11:12:32 +0200495void stop_nmi(void)
496{
497 acpi_nmi_disable();
498 ignore_nmis++;
499}
500
501void restart_nmi(void)
502{
503 ignore_nmis--;
504 acpi_nmi_enable();
505}
506
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200507/* May run on IST stack. */
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200508dotraplinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Alexander van Heukelumb94da1e2008-09-09 21:55:58 +0200510#ifdef CONFIG_KPROBES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
512 == NOTIFY_STOP)
Stas Sergeev48c882112005-05-01 08:58:49 -0700513 return;
Alexander van Heukelumb94da1e2008-09-09 21:55:58 +0200514#else
515 if (notify_die(DIE_TRAP, "int3", regs, error_code, 3, SIGTRAP)
516 == NOTIFY_STOP)
517 return;
518#endif
Ingo Molnarb5964402008-02-26 11:15:50 +0100519
Alexander van Heukelum4915a352008-10-03 22:00:34 +0200520 preempt_conditional_sti(regs);
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200521 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
Alexander van Heukelum4915a352008-10-03 22:00:34 +0200522 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200525#ifdef CONFIG_X86_64
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100526/*
527 * Help handler running on IST stack to switch back to user stack
528 * for scheduling or signal handling. The actual stack switch is done in
529 * entry.S
530 */
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200531asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
532{
533 struct pt_regs *regs = eregs;
534 /* Did already sync */
535 if (eregs == (struct pt_regs *)eregs->sp)
536 ;
537 /* Exception from user space */
538 else if (user_mode(eregs))
539 regs = task_pt_regs(current);
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100540 /*
541 * Exception from kernel and interrupts are enabled. Move to
542 * kernel process stack.
543 */
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200544 else if (eregs->flags & X86_EFLAGS_IF)
545 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
546 if (eregs != regs)
547 *regs = *eregs;
548 return regs;
549}
550#endif
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552/*
553 * Our handling of the processor debug registers is non-trivial.
554 * We do not clear them on entry and exit from the kernel. Therefore
555 * it is possible to get a watchpoint trap here from inside the kernel.
556 * However, the code in ./ptrace.c has ensured that the user can
557 * only set watchpoints on userspace addresses. Therefore the in-kernel
558 * watchpoint trap can only occur in code which is reading/writing
559 * from user space. Such code must not hold kernel locks (since it
560 * can equally take a page fault), therefore it is safe to call
561 * force_sig_info even though that claims and releases locks.
Ingo Molnarb5964402008-02-26 11:15:50 +0100562 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 * Code in ./signal.c ensures that the debug control register
564 * is restored before we deliver any signal, and therefore that
565 * user code runs with the correct debug control register even though
566 * we clear it here.
567 *
568 * Being careful here means that we don't have to be as careful in a
569 * lot of more complicated places (task switching can be a bit lazy
570 * about restoring all the debug state, and ptrace doesn't have to
571 * find every occurrence of the TF bit that could be saved away even
572 * by user code)
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200573 *
574 * May run on IST stack.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 */
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200576dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 struct task_struct *tsk = current;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200579 unsigned long condition;
Srinivasa Dsda654b72008-09-23 15:23:52 +0530580 int si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Vincent Hanquez1cc6f122005-06-23 00:08:43 -0700582 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Roland McGrath10faa812008-01-30 13:30:54 +0100584 /*
585 * The processor cleared BTF, so don't mark that we need it set.
586 */
587 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
588 tsk->thread.debugctlmsr = 0;
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200591 SIGTRAP) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /* It's safe to allow irq's after DR6 has been saved */
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200595 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /* Mask out spurious debug traps due to lazy DR7 setting */
598 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
Roland McGrath0f534092008-01-30 13:30:59 +0100599 if (!tsk->thread.debugreg7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 goto clear_dr7;
601 }
602
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200603#ifdef CONFIG_X86_32
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300604 if (regs->flags & X86_VM_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 goto debug_vm86;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200606#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /* Save debug status register where ptrace can see it */
Roland McGrath0f534092008-01-30 13:30:59 +0100609 tsk->thread.debugreg6 = condition;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 /*
612 * Single-stepping through TF: make sure we ignore any events in
613 * kernel space (but re-enable TF when returning to user mode).
614 */
615 if (condition & DR_STEP) {
Vincent Hanquez717b5942005-06-23 00:08:45 -0700616 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 goto clear_TF_reenable;
618 }
619
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200620 si_code = get_si_code(condition);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 /* Ok, finally something we can handle */
Srinivasa Dsda654b72008-09-23 15:23:52 +0530622 send_sigtrap(tsk, regs, error_code, si_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Ingo Molnarb5964402008-02-26 11:15:50 +0100624 /*
625 * Disable additional traps. They'll be re-enabled when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 * the signal is delivered.
627 */
628clear_dr7:
Vincent Hanquez1cc6f122005-06-23 00:08:43 -0700629 set_debugreg(0, 7);
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200630 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return;
632
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200633#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634debug_vm86:
Thomas Gleixnerbe716612009-01-13 23:36:34 +0100635 /* reenable preemption: handle_vm86_trap() might sleep */
636 dec_preempt_count();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
Thomas Gleixnerbe716612009-01-13 23:36:34 +0100638 conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200640#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642clear_TF_reenable:
643 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
Ingo Molnar60930152008-03-30 11:45:23 +0200644 regs->flags &= ~X86_EFLAGS_TF;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200645 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return;
647}
648
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200649#ifdef CONFIG_X86_64
650static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
651{
652 if (fixup_exception(regs))
653 return 1;
654
655 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
656 /* Illegal floating point operation in the kernel */
657 current->thread.trap_no = trapnr;
658 die(str, regs, 0);
659 return 0;
660}
661#endif
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663/*
664 * Note that we play around with the 'TS' bit in an attempt to get
665 * the correct behaviour even in the presence of the asynchronous
666 * IRQ13 behaviour
667 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100668void math_error(void __user *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Ingo Molnarb5964402008-02-26 11:15:50 +0100670 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 siginfo_t info;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800672 unsigned short cwd, swd, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 /*
675 * Save the info for the exception handler and clear the error.
676 */
677 task = current;
678 save_init_fpu(task);
679 task->thread.trap_no = 16;
680 task->thread.error_code = 0;
681 info.si_signo = SIGFPE;
682 info.si_errno = 0;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100683 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /*
685 * (~cwd & swd) will mask out exceptions that are not set to unmasked
686 * status. 0x3f is the exception bits in these regs, 0x200 is the
687 * C1 reg you need in case of a stack fault, 0x040 is the stack
688 * fault bit. We should only be taking one exception at a time,
689 * so if this combination doesn't produce any single exception,
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200690 * then we have a bad program that isn't synchronizing its FPU usage
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 * and it will suffer the consequences since we won't be able to
692 * fully reproduce the context of the exception
693 */
694 cwd = get_fpu_cwd(task);
695 swd = get_fpu_swd(task);
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800696
H. Peter Anvina73ad332008-12-25 10:39:01 -0800697 err = swd & ~cwd;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800698
699 if (err & 0x001) { /* Invalid op */
Ingo Molnarb5964402008-02-26 11:15:50 +0100700 /*
701 * swd & 0x240 == 0x040: Stack Underflow
702 * swd & 0x240 == 0x240: Stack Overflow
703 * User must clear the SF bit (0x40) if set
704 */
705 info.si_code = FPE_FLTINV;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800706 } else if (err & 0x004) { /* Divide by Zero */
Ingo Molnarb5964402008-02-26 11:15:50 +0100707 info.si_code = FPE_FLTDIV;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800708 } else if (err & 0x008) { /* Overflow */
Ingo Molnarb5964402008-02-26 11:15:50 +0100709 info.si_code = FPE_FLTOVF;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800710 } else if (err & 0x012) { /* Denormal, Underflow */
711 info.si_code = FPE_FLTUND;
712 } else if (err & 0x020) { /* Precision */
Ingo Molnarb5964402008-02-26 11:15:50 +0100713 info.si_code = FPE_FLTRES;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800714 } else {
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100715 /*
716 * If we're using IRQ 13, or supposedly even some trap 16
717 * implementations, it's possible we get a spurious trap...
718 */
H. Peter Anvina73ad332008-12-25 10:39:01 -0800719 return; /* Spurious trap, no error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721 force_sig_info(SIGFPE, &info, task);
722}
723
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200724dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Alexander van Heukelum252d28f2008-09-09 21:56:09 +0200726 conditional_sti(regs);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200727
728#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 ignore_fpu_irq = 1;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200730#else
731 if (!user_mode(regs) &&
732 kernel_math_error(regs, "kernel x87 math error", 16))
733 return;
734#endif
735
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100736 math_error((void __user *)regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100739static void simd_math_error(void __user *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Ingo Molnarb5964402008-02-26 11:15:50 +0100741 struct task_struct *task;
Ingo Molnarb5964402008-02-26 11:15:50 +0100742 siginfo_t info;
Alexander van Heukelum7b4fd4b2008-07-02 01:33:14 +0200743 unsigned short mxcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 /*
746 * Save the info for the exception handler and clear the error.
747 */
748 task = current;
749 save_init_fpu(task);
750 task->thread.trap_no = 19;
751 task->thread.error_code = 0;
752 info.si_signo = SIGFPE;
753 info.si_errno = 0;
754 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100755 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /*
757 * The SIMD FPU exceptions are handled a little differently, as there
758 * is only a single status/control register. Thus, to determine which
759 * unmasked exception was caught we must mask the exception mask bits
760 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
761 */
762 mxcsr = get_fpu_mxcsr(task);
763 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
Ingo Molnarb5964402008-02-26 11:15:50 +0100764 case 0x000:
765 default:
766 break;
767 case 0x001: /* Invalid Op */
768 info.si_code = FPE_FLTINV;
769 break;
770 case 0x002: /* Denormalize */
771 case 0x010: /* Underflow */
772 info.si_code = FPE_FLTUND;
773 break;
774 case 0x004: /* Zero Divide */
775 info.si_code = FPE_FLTDIV;
776 break;
777 case 0x008: /* Overflow */
778 info.si_code = FPE_FLTOVF;
779 break;
780 case 0x020: /* Precision */
781 info.si_code = FPE_FLTRES;
782 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784 force_sig_info(SIGFPE, &info, task);
785}
786
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200787dotraplinkage void
788do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Alexander van Heukelumb939bde2008-09-09 21:56:12 +0200790 conditional_sti(regs);
791
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200792#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (cpu_has_xmm) {
794 /* Handle SIMD FPU exceptions on PIII+ processors. */
795 ignore_fpu_irq = 1;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100796 simd_math_error((void __user *)regs->ip);
Ingo Molnarb5964402008-02-26 11:15:50 +0100797 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
Ingo Molnarb5964402008-02-26 11:15:50 +0100799 /*
800 * Handle strange cache flush from user space exception
801 * in all other cases. This is undocumented behaviour.
802 */
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300803 if (regs->flags & X86_VM_MASK) {
Ingo Molnarb5964402008-02-26 11:15:50 +0100804 handle_vm86_fault((struct kernel_vm86_regs *)regs, error_code);
805 return;
806 }
807 current->thread.trap_no = 19;
808 current->thread.error_code = error_code;
809 die_if_kernel("cache flush denied", regs, error_code);
810 force_sig(SIGSEGV, current);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200811#else
812 if (!user_mode(regs) &&
813 kernel_math_error(regs, "kernel simd math error", 19))
814 return;
815 simd_math_error((void __user *)regs->ip);
816#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200819dotraplinkage void
820do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Alexander van Heukelumcf819782008-09-09 21:56:08 +0200822 conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823#if 0
824 /* No need to warn about this any longer. */
Ingo Molnarb5964402008-02-26 11:15:50 +0100825 printk(KERN_INFO "Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826#endif
827}
828
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200829#ifdef CONFIG_X86_32
Ingo Molnarb5964402008-02-26 11:15:50 +0100830unsigned long patch_espfix_desc(unsigned long uesp, unsigned long kesp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Glauber Costa736f12b2008-05-27 20:14:51 -0700832 struct desc_struct *gdt = get_cpu_gdt_table(smp_processor_id());
Stas Sergeevbe44d2a2006-12-07 02:14:01 +0100833 unsigned long base = (kesp - uesp) & -THREAD_SIZE;
834 unsigned long new_kesp = kesp - base;
835 unsigned long lim_pages = (new_kesp | (THREAD_SIZE - 1)) >> PAGE_SHIFT;
836 __u64 desc = *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS];
Ingo Molnarb5964402008-02-26 11:15:50 +0100837
Stas Sergeevbe44d2a2006-12-07 02:14:01 +0100838 /* Set up base for espfix segment */
Ingo Molnarb5964402008-02-26 11:15:50 +0100839 desc &= 0x00f0ff0000000000ULL;
840 desc |= ((((__u64)base) << 16) & 0x000000ffffff0000ULL) |
Stas Sergeevbe44d2a2006-12-07 02:14:01 +0100841 ((((__u64)base) << 32) & 0xff00000000000000ULL) |
842 ((((__u64)lim_pages) << 32) & 0x000f000000000000ULL) |
843 (lim_pages & 0xffff);
844 *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS] = desc;
Ingo Molnarb5964402008-02-26 11:15:50 +0100845
Stas Sergeevbe44d2a2006-12-07 02:14:01 +0100846 return new_kesp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200848#else
849asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
850{
851}
852
853asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
854{
855}
856#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858/*
Ingo Molnarb5964402008-02-26 11:15:50 +0100859 * 'math_state_restore()' saves the current math information in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 * old math state array, and gets the new ones from the current task
861 *
862 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
863 * Don't touch unless you *really* know how it works.
864 *
865 * Must be called with kernel preemption disabled (in this case,
866 * local interrupts are disabled at the call-site in entry.S).
867 */
Chuck Ebbertacc20762006-12-07 02:14:01 +0100868asmlinkage void math_state_restore(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 struct thread_info *thread = current_thread_info();
871 struct task_struct *tsk = thread->task;
872
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700873 if (!tsk_used_math(tsk)) {
874 local_irq_enable();
875 /*
876 * does a slab alloc which can sleep
877 */
878 if (init_fpu(tsk)) {
879 /*
880 * ran out of memory!
881 */
882 do_group_exit(SIGKILL);
883 return;
884 }
885 local_irq_disable();
886 }
887
Ingo Molnarb5964402008-02-26 11:15:50 +0100888 clts(); /* Allow maths ops (or we recurse) */
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200889#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 restore_fpu(tsk);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200891#else
892 /*
893 * Paranoid restore. send a SIGSEGV if we fail to restore the state.
894 */
895 if (unlikely(restore_fpu_checking(tsk))) {
896 stts();
897 force_sig(SIGSEGV, tsk);
898 return;
899 }
900#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 thread->status |= TS_USEDFPU; /* So we fnsave on switch_to() */
Chuck Ebbertacc20762006-12-07 02:14:01 +0100902 tsk->fpu_counter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
Rusty Russell5992b6d2007-07-19 01:49:21 -0700904EXPORT_SYMBOL_GPL(math_state_restore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906#ifndef CONFIG_MATH_EMULATION
Tejun Heod3157602009-02-09 22:17:39 +0900907void math_emulate(struct math_emu_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Ingo Molnarb5964402008-02-26 11:15:50 +0100909 printk(KERN_EMERG
910 "math-emulation not enabled and no coprocessor found.\n");
911 printk(KERN_EMERG "killing %s.\n", current->comm);
912 force_sig(SIGFPE, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 schedule();
914}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915#endif /* CONFIG_MATH_EMULATION */
916
Tejun Heod3157602009-02-09 22:17:39 +0900917dotraplinkage void __kprobes do_device_not_available(struct pt_regs regs)
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200918{
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200919#ifdef CONFIG_X86_32
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200920 if (read_cr0() & X86_CR0_EM) {
Tejun Heod3157602009-02-09 22:17:39 +0900921 struct math_emu_info info = { };
922
923 conditional_sti(&regs);
924
925 info.regs = &regs;
926 math_emulate(&info);
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200927 } else {
928 math_state_restore(); /* interrupts still off */
Tejun Heod3157602009-02-09 22:17:39 +0900929 conditional_sti(&regs);
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200930 }
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200931#else
932 math_state_restore();
933#endif
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200934}
935
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200936#ifdef CONFIG_X86_32
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200937dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200938{
939 siginfo_t info;
940 local_irq_enable();
941
942 info.si_signo = SIGILL;
943 info.si_errno = 0;
944 info.si_code = ILL_BADSTK;
945 info.si_addr = 0;
946 if (notify_die(DIE_TRAP, "iret exception",
947 regs, error_code, 32, SIGILL) == NOTIFY_STOP)
948 return;
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200949 do_trap(32, SIGILL, "iret exception", regs, error_code, &info);
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200950}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200951#endif
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953void __init trap_init(void)
954{
Rusty Russelldbeb2be2007-10-19 20:35:03 +0200955 int i;
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957#ifdef CONFIG_EISA
Ingo Molnar927222b2008-01-30 13:33:49 +0100958 void __iomem *p = early_ioremap(0x0FFFD9, 4);
Ingo Molnarb5964402008-02-26 11:15:50 +0100959
960 if (readl(p) == 'E' + ('I'<<8) + ('S'<<16) + ('A'<<24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 EISA_bus = 1;
Ingo Molnar927222b2008-01-30 13:33:49 +0100962 early_iounmap(p, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963#endif
964
Alexander van Heukelum976382d2008-09-09 21:55:57 +0200965 set_intr_gate(0, &divide_error);
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200966 set_intr_gate_ist(1, &debug, DEBUG_STACK);
967 set_intr_gate_ist(2, &nmi, NMI_STACK);
968 /* int3 can be called from all */
969 set_system_intr_gate_ist(3, &int3, DEBUG_STACK);
970 /* int4 can be called from all */
971 set_system_intr_gate(4, &overflow);
Alexander van Heukelum64f644c2008-09-09 21:56:00 +0200972 set_intr_gate(5, &bounds);
Alexander van Heukelum12394cf2008-09-09 21:56:01 +0200973 set_intr_gate(6, &invalid_op);
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200974 set_intr_gate(7, &device_not_available);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200975#ifdef CONFIG_X86_32
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200976 set_task_gate(8, GDT_ENTRY_DOUBLEFAULT_TSS);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200977#else
978 set_intr_gate_ist(8, &double_fault, DOUBLEFAULT_STACK);
979#endif
Alexander van Heukelum51bc1ed2008-09-09 21:56:03 +0200980 set_intr_gate(9, &coprocessor_segment_overrun);
Alexander van Heukelum6bf77bf2008-09-09 21:56:04 +0200981 set_intr_gate(10, &invalid_TSS);
Alexander van Heukelum36d936c2008-09-09 21:56:05 +0200982 set_intr_gate(11, &segment_not_present);
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200983 set_intr_gate_ist(12, &stack_segment, STACKFAULT_STACK);
Alexander van Heukelumc6df0d72008-09-09 21:56:07 +0200984 set_intr_gate(13, &general_protection);
Ingo Molnarb5964402008-02-26 11:15:50 +0100985 set_intr_gate(14, &page_fault);
Alexander van Heukelumcf819782008-09-09 21:56:08 +0200986 set_intr_gate(15, &spurious_interrupt_bug);
Alexander van Heukelum252d28f2008-09-09 21:56:09 +0200987 set_intr_gate(16, &coprocessor_error);
Alexander van Heukelum5feedfd2008-09-09 21:56:10 +0200988 set_intr_gate(17, &alignment_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989#ifdef CONFIG_X86_MCE
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200990 set_intr_gate_ist(18, &machine_check, MCE_STACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991#endif
Alexander van Heukelumb939bde2008-09-09 21:56:12 +0200992 set_intr_gate(19, &simd_coprocessor_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200994#ifdef CONFIG_IA32_EMULATION
995 set_system_intr_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
996#endif
997
998#ifdef CONFIG_X86_32
Jan Beulichd43c6e82006-01-06 00:11:49 -0800999 if (cpu_has_fxsr) {
Jan Beulichd43c6e82006-01-06 00:11:49 -08001000 printk(KERN_INFO "Enabling fast FPU save and restore... ");
1001 set_in_cr4(X86_CR4_OSFXSR);
1002 printk("done.\n");
1003 }
1004 if (cpu_has_xmm) {
Ingo Molnarb5964402008-02-26 11:15:50 +01001005 printk(KERN_INFO
1006 "Enabling unmasked SIMD FPU exception support... ");
Jan Beulichd43c6e82006-01-06 00:11:49 -08001007 set_in_cr4(X86_CR4_OSXMMEXCPT);
1008 printk("done.\n");
1009 }
1010
Alexander van Heukelum699d2932008-10-03 22:00:32 +02001011 set_system_trap_gate(SYSCALL_VECTOR, &system_call);
Yinghai Lub77b8812008-12-19 15:23:44 -08001012#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Ingo Molnarb5964402008-02-26 11:15:50 +01001014 /* Reserve all the builtin and the syscall vector: */
Rusty Russelldbeb2be2007-10-19 20:35:03 +02001015 for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
1016 set_bit(i, used_vectors);
Ingo Molnarb5964402008-02-26 11:15:50 +01001017
Yinghai Lub77b8812008-12-19 15:23:44 -08001018#ifdef CONFIG_X86_64
1019 set_bit(IA32_SYSCALL_VECTOR, used_vectors);
1020#else
Rusty Russelldbeb2be2007-10-19 20:35:03 +02001021 set_bit(SYSCALL_VECTOR, used_vectors);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +02001022#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /*
Ingo Molnarb5964402008-02-26 11:15:50 +01001024 * Should be a barrier for any external CPU state:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 */
1026 cpu_init();
1027
Alexander van Heukelum081f75b2008-10-03 22:00:39 +02001028#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 trap_init_hook();
Alexander van Heukelum081f75b2008-10-03 22:00:39 +02001030#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}