blob: 953172af6e511919730963fdd6496f879d898095 [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/*
10 * 'Traps.c' handles hardware traps and faults after we have saved some
11 * state in 'asm.s'.
12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kallsyms.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010015#include <linux/spinlock.h>
16#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kprobes.h>
Andrew Morton1e2af922006-09-27 01:51:15 -070018#include <linux/uaccess.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010019#include <linux/utsname.h>
20#include <linux/kdebug.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/ptrace.h>
24#include <linux/string.h>
25#include <linux/unwind.h>
26#include <linux/delay.h>
27#include <linux/errno.h>
28#include <linux/kexec.h>
29#include <linux/sched.h>
30#include <linux/timer.h>
31#include <linux/init.h>
Jeremy Fitzhardinge91768d62006-12-08 02:36:21 -080032#include <linux/bug.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010033#include <linux/nmi.h>
34#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#ifdef CONFIG_EISA
37#include <linux/ioport.h>
38#include <linux/eisa.h>
39#endif
40
41#ifdef CONFIG_MCA
42#include <linux/mca.h>
43#endif
44
Dave Jiangc0d12172007-07-19 01:49:46 -070045#if defined(CONFIG_EDAC)
46#include <linux/edac.h>
47#endif
48
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +020049#include <asm/processor-flags.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010050#include <asm/arch_hooks.h>
51#include <asm/stacktrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/debugreg.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010054#include <asm/atomic.h>
55#include <asm/system.h>
56#include <asm/unwind.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/desc.h>
58#include <asm/i387.h>
59#include <asm/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <asm/smp.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010061#include <asm/io.h>
Jaswinder Singh6ac8d512008-07-15 21:09:13 +053062#include <asm/traps.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#include "mach_traps.h"
Alexander van Heukelumeb642f62008-09-09 21:56:11 +020065#include "cpu/mcheck/mce.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Rusty Russelldbeb2be2007-10-19 20:35:03 +020067DECLARE_BITMAP(used_vectors, NR_VECTORS);
68EXPORT_SYMBOL_GPL(used_vectors);
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070asmlinkage 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 } } }, };
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Alexander van Heukelumbadc7652008-07-02 01:30:30 +020083static int ignore_nmis;
Alan Sterne041c682006-03-27 01:16:30 -080084
Alexander van Heukelum762db432008-09-09 21:55:55 +020085static inline void conditional_sti(struct pt_regs *regs)
86{
87 if (regs->flags & X86_EFLAGS_IF)
88 local_irq_enable();
89}
90
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +020091static inline void preempt_conditional_sti(struct pt_regs *regs)
92{
93 inc_preempt_count();
94 if (regs->flags & X86_EFLAGS_IF)
95 local_irq_enable();
96}
97
98static inline void preempt_conditional_cli(struct pt_regs *regs)
99{
100 if (regs->flags & X86_EFLAGS_IF)
101 local_irq_disable();
102 dec_preempt_count();
103}
104
Ingo Molnarb5964402008-02-26 11:15:50 +0100105static inline void
106die_if_kernel(const char *str, struct pt_regs *regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Vincent Hanquez717b5942005-06-23 00:08:45 -0700108 if (!user_mode_vm(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 die(str, regs, err);
110}
111
Alexander van Heukelumae821572008-09-30 18:41:35 +0200112/*
113 * Perform the lazy TSS's I/O bitmap copy. If the TSS has an
114 * invalid offset set (the LAZY one) and the faulting thread has
115 * a valid I/O bitmap pointer, we copy the I/O bitmap in the TSS,
116 * we set the offset field correctly and return 1.
117 */
118static int lazy_iobitmap_copy(void)
119{
120 struct thread_struct *thread;
121 struct tss_struct *tss;
122 int cpu;
123
124 cpu = get_cpu();
125 tss = &per_cpu(init_tss, cpu);
126 thread = &current->thread;
127
128 if (tss->x86_tss.io_bitmap_base == INVALID_IO_BITMAP_OFFSET_LAZY &&
129 thread->io_bitmap_ptr) {
130 memcpy(tss->io_bitmap, thread->io_bitmap_ptr,
131 thread->io_bitmap_max);
132 /*
133 * If the previously set map was extending to higher ports
134 * than the current one, pad extra space with 0xff (no access).
135 */
136 if (thread->io_bitmap_max < tss->io_bitmap_max) {
137 memset((char *) tss->io_bitmap +
138 thread->io_bitmap_max, 0xff,
139 tss->io_bitmap_max - thread->io_bitmap_max);
140 }
141 tss->io_bitmap_max = thread->io_bitmap_max;
142 tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
143 tss->io_bitmap_owner = thread;
144 put_cpu();
145
146 return 1;
147 }
148 put_cpu();
149
150 return 0;
151}
152
Ingo Molnarb5964402008-02-26 11:15:50 +0100153static void __kprobes
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200154do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
Ingo Molnarb5964402008-02-26 11:15:50 +0100155 long error_code, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700157 struct task_struct *tsk = current;
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700158
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300159 if (regs->flags & X86_VM_MASK) {
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200160 /*
161 * traps 0, 1, 3, 4, and 5 should be forwarded to vm86.
162 * On nmi (interrupt 2), do_trap should not be called.
163 */
164 if (trapnr < 6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 goto vm86_trap;
166 goto trap_signal;
167 }
168
Vincent Hanquez717b5942005-06-23 00:08:45 -0700169 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 goto kernel_trap;
171
Ingo Molnarb5964402008-02-26 11:15:50 +0100172trap_signal:
173 /*
174 * We want error_code and trap_no set for userspace faults and
175 * kernelspace faults which result in die(), but not
176 * kernelspace faults which are fixed up. die() gives the
177 * process no chance to handle the signal and notice the
178 * kernel fault information, so that won't result in polluting
179 * the information about previously queued, but not yet
180 * delivered, faults. See also do_general_protection below.
181 */
182 tsk->thread.error_code = error_code;
183 tsk->thread.trap_no = trapnr;
184
185 if (info)
186 force_sig_info(signr, info, tsk);
187 else
188 force_sig(signr, tsk);
189 return;
190
191kernel_trap:
192 if (!fixup_exception(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200193 tsk->thread.error_code = error_code;
194 tsk->thread.trap_no = trapnr;
Ingo Molnarb5964402008-02-26 11:15:50 +0100195 die(str, regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
Ingo Molnarb5964402008-02-26 11:15:50 +0100197 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Ingo Molnarb5964402008-02-26 11:15:50 +0100199vm86_trap:
200 if (handle_vm86_trap((struct kernel_vm86_regs *) regs,
201 error_code, trapnr))
202 goto trap_signal;
203 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Ingo Molnarb5964402008-02-26 11:15:50 +0100206#define DO_ERROR(trapnr, signr, str, name) \
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200207dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100208{ \
209 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200210 == NOTIFY_STOP) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100211 return; \
Alexander van Heukelum61aef7d2008-09-09 21:55:56 +0200212 conditional_sti(regs); \
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200213 do_trap(trapnr, signr, str, regs, error_code, NULL); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200216#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200217dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100218{ \
219 siginfo_t info; \
220 info.si_signo = signr; \
221 info.si_errno = 0; \
222 info.si_code = sicode; \
223 info.si_addr = (void __user *)siaddr; \
Ingo Molnarb5964402008-02-26 11:15:50 +0100224 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200225 == NOTIFY_STOP) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100226 return; \
Alexander van Heukelum61aef7d2008-09-09 21:55:56 +0200227 conditional_sti(regs); \
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200228 do_trap(trapnr, signr, str, regs, error_code, &info); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200231DO_ERROR_INFO(0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
232DO_ERROR(4, SIGSEGV, "overflow", overflow)
233DO_ERROR(5, SIGSEGV, "bounds", bounds)
234DO_ERROR_INFO(6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
Alexander van Heukelum51bc1ed2008-09-09 21:56:03 +0200235DO_ERROR(9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
Alexander van Heukelum6bf77bf2008-09-09 21:56:04 +0200236DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
Alexander van Heukelum36d936c2008-09-09 21:56:05 +0200237DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
Alexander van Heukelumf5ca8182008-09-09 21:56:06 +0200238DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200239DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200241dotraplinkage void __kprobes
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200242do_general_protection(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200244 struct task_struct *tsk;
Ingo Molnarb5964402008-02-26 11:15:50 +0100245
Alexander van Heukelumc6df0d72008-09-09 21:56:07 +0200246 conditional_sti(regs);
247
Alexander van Heukelumae821572008-09-30 18:41:35 +0200248 if (lazy_iobitmap_copy()) {
249 /* restart the faulting instruction */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return;
251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300253 if (regs->flags & X86_VM_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto gp_in_vm86;
255
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200256 tsk = current;
Vincent Hanquez717b5942005-06-23 00:08:45 -0700257 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 goto gp_in_kernel;
259
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200260 tsk->thread.error_code = error_code;
261 tsk->thread.trap_no = 13;
Ingo Molnarb5964402008-02-26 11:15:50 +0100262
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200263 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
264 printk_ratelimit()) {
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200265 printk(KERN_INFO
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200266 "%s[%d] general protection ip:%lx sp:%lx error:%lx",
267 tsk->comm, task_pid_nr(tsk),
268 regs->ip, regs->sp, error_code);
Andi Kleen03252912008-01-30 13:33:18 +0100269 print_vma_addr(" in ", regs->ip);
270 printk("\n");
271 }
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200272
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200273 force_sig(SIGSEGV, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return;
275
276gp_in_vm86:
277 local_irq_enable();
278 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
279 return;
280
281gp_in_kernel:
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200282 if (fixup_exception(regs))
283 return;
284
285 tsk->thread.error_code = error_code;
286 tsk->thread.trap_no = 13;
287 if (notify_die(DIE_GPF, "general protection fault", regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 error_code, 13, SIGSEGV) == NOTIFY_STOP)
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200289 return;
290 die("general protection fault", regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200293static notrace __kprobes void
Ingo Molnarb5964402008-02-26 11:15:50 +0100294mem_parity_error(unsigned char reason, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Ingo Molnarb5964402008-02-26 11:15:50 +0100296 printk(KERN_EMERG
297 "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
298 reason, smp_processor_id());
299
300 printk(KERN_EMERG
301 "You have some hardware problem, likely on the PCI bus.\n");
Dave Jiangc0d12172007-07-19 01:49:46 -0700302
303#if defined(CONFIG_EDAC)
Ingo Molnarb5964402008-02-26 11:15:50 +0100304 if (edac_handler_set()) {
Dave Jiangc0d12172007-07-19 01:49:46 -0700305 edac_atomic_assert_error();
306 return;
307 }
308#endif
309
Don Zickus8da5add2006-09-26 10:52:27 +0200310 if (panic_on_unrecovered_nmi)
Ingo Molnarb5964402008-02-26 11:15:50 +0100311 panic("NMI: Not continuing");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Don Zickusc41c5cd2006-09-26 10:52:27 +0200313 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 /* Clear and disable the memory parity error line. */
316 clear_mem_error(reason);
317}
318
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200319static notrace __kprobes void
Ingo Molnarb5964402008-02-26 11:15:50 +0100320io_check_error(unsigned char reason, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 unsigned long i;
323
Dave Jones9c107802006-01-09 20:51:32 -0800324 printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 show_registers(regs);
326
327 /* Re-enable the IOCK line, wait for a few seconds */
328 reason = (reason & 0xf) | 8;
329 outb(reason, 0x61);
Ingo Molnarb5964402008-02-26 11:15:50 +0100330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 i = 2000;
Ingo Molnarb5964402008-02-26 11:15:50 +0100332 while (--i)
333 udelay(1000);
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 reason &= ~8;
336 outb(reason, 0x61);
337}
338
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200339static notrace __kprobes void
Ingo Molnarb5964402008-02-26 11:15:50 +0100340unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Jason Wesseld3597522008-02-15 14:55:53 -0600342 if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
343 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344#ifdef CONFIG_MCA
Ingo Molnarb5964402008-02-26 11:15:50 +0100345 /*
346 * Might actually be able to figure out what the guilty party
347 * is:
348 */
349 if (MCA_bus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 mca_handle_nmi();
351 return;
352 }
353#endif
Ingo Molnarb5964402008-02-26 11:15:50 +0100354 printk(KERN_EMERG
355 "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
356 reason, smp_processor_id());
357
Don Zickusc41c5cd2006-09-26 10:52:27 +0200358 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
Don Zickus8da5add2006-09-26 10:52:27 +0200359 if (panic_on_unrecovered_nmi)
Ingo Molnarb5964402008-02-26 11:15:50 +0100360 panic("NMI: Not continuing");
Don Zickus8da5add2006-09-26 10:52:27 +0200361
Don Zickusc41c5cd2006-09-26 10:52:27 +0200362 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365static DEFINE_SPINLOCK(nmi_print_lock);
366
Cyrill Gorcunovddca03c2008-05-24 19:36:31 +0400367void notrace __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Cyrill Gorcunovddca03c2008-05-24 19:36:31 +0400369 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
George Anzinger748f2ed2005-09-03 15:56:48 -0700370 return;
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 spin_lock(&nmi_print_lock);
373 /*
374 * We are in trouble anyway, lets at least try
Ingo Molnarb5964402008-02-26 11:15:50 +0100375 * to get a message out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 */
377 bust_spinlocks(1);
Cyrill Gorcunovddca03c2008-05-24 19:36:31 +0400378 printk(KERN_EMERG "%s", str);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100379 printk(" on CPU%d, ip %08lx, registers:\n",
380 smp_processor_id(), regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 show_registers(regs);
Cyrill Gorcunovddca03c2008-05-24 19:36:31 +0400382 if (do_panic)
383 panic("Non maskable interrupt");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 console_silent();
385 spin_unlock(&nmi_print_lock);
386 bust_spinlocks(0);
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700387
Ingo Molnarb5964402008-02-26 11:15:50 +0100388 /*
389 * If we are in kernel we are probably nested up pretty bad
390 * and might aswell get out now while we still can:
391 */
Jan Beulichdb753bd2006-03-23 02:59:46 -0800392 if (!user_mode_vm(regs)) {
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700393 current->thread.trap_no = 2;
394 crash_kexec(regs);
395 }
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 do_exit(SIGSEGV);
398}
399
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200400static notrace __kprobes void default_do_nmi(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 unsigned char reason = 0;
Alexander van Heukelumabd34802008-07-02 18:39:01 +0200403 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Alexander van Heukelumabd34802008-07-02 18:39:01 +0200405 cpu = smp_processor_id();
406
407 /* Only the BSP gets external NMIs from the system. */
408 if (!cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 reason = get_nmi_reason();
Ingo Molnarb5964402008-02-26 11:15:50 +0100410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (!(reason & 0xc0)) {
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800412 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200413 == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return;
415#ifdef CONFIG_X86_LOCAL_APIC
416 /*
417 * Ok, so this is none of the documented NMI sources,
418 * so it must be the NMI watchdog.
419 */
Don Zickus3adbbcc2006-09-26 10:52:26 +0200420 if (nmi_watchdog_tick(regs, reason))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return;
Alexander van Heukelumabd34802008-07-02 18:39:01 +0200422 if (!do_nmi_callback(regs, cpu))
Don Zickus3adbbcc2006-09-26 10:52:26 +0200423 unknown_nmi_error(reason, regs);
Ingo Molnarb5964402008-02-26 11:15:50 +0100424#else
425 unknown_nmi_error(reason, regs);
426#endif
Don Zickus2fbe7b22006-09-26 10:52:27 +0200427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return;
429 }
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800430 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return;
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200432
433 /* AK: following checks seem to be broken on modern chipsets. FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (reason & 0x80)
435 mem_parity_error(reason, regs);
436 if (reason & 0x40)
437 io_check_error(reason, regs);
438 /*
439 * Reassert NMI in case it became active meanwhile
Ingo Molnarb5964402008-02-26 11:15:50 +0100440 * as it's edge-triggered:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 */
442 reassert_nmi();
443}
444
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200445dotraplinkage notrace __kprobes void
446do_nmi(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 int cpu;
449
450 nmi_enter();
451
452 cpu = smp_processor_id();
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 ++nmi_count(cpu);
455
Andi Kleen8f4e9562007-07-22 11:12:32 +0200456 if (!ignore_nmis)
457 default_do_nmi(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 nmi_exit();
460}
461
Andi Kleen8f4e9562007-07-22 11:12:32 +0200462void stop_nmi(void)
463{
464 acpi_nmi_disable();
465 ignore_nmis++;
466}
467
468void restart_nmi(void)
469{
470 ignore_nmis--;
471 acpi_nmi_enable();
472}
473
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200474dotraplinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Alexander van Heukelumb94da1e2008-09-09 21:55:58 +0200476#ifdef CONFIG_KPROBES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
478 == NOTIFY_STOP)
Stas Sergeev48c882112005-05-01 08:58:49 -0700479 return;
Alexander van Heukelum762db432008-09-09 21:55:55 +0200480 conditional_sti(regs);
Alexander van Heukelumb94da1e2008-09-09 21:55:58 +0200481#else
482 if (notify_die(DIE_TRAP, "int3", regs, error_code, 3, SIGTRAP)
483 == NOTIFY_STOP)
484 return;
485#endif
Ingo Molnarb5964402008-02-26 11:15:50 +0100486
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200487 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490/*
491 * Our handling of the processor debug registers is non-trivial.
492 * We do not clear them on entry and exit from the kernel. Therefore
493 * it is possible to get a watchpoint trap here from inside the kernel.
494 * However, the code in ./ptrace.c has ensured that the user can
495 * only set watchpoints on userspace addresses. Therefore the in-kernel
496 * watchpoint trap can only occur in code which is reading/writing
497 * from user space. Such code must not hold kernel locks (since it
498 * can equally take a page fault), therefore it is safe to call
499 * force_sig_info even though that claims and releases locks.
Ingo Molnarb5964402008-02-26 11:15:50 +0100500 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * Code in ./signal.c ensures that the debug control register
502 * is restored before we deliver any signal, and therefore that
503 * user code runs with the correct debug control register even though
504 * we clear it here.
505 *
506 * Being careful here means that we don't have to be as careful in a
507 * lot of more complicated places (task switching can be a bit lazy
508 * about restoring all the debug state, and ptrace doesn't have to
509 * find every occurrence of the TF bit that could be saved away even
510 * by user code)
511 */
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200512dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 struct task_struct *tsk = current;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200515 unsigned long condition;
Srinivasa Dsda654b72008-09-23 15:23:52 +0530516 int si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Vincent Hanquez1cc6f122005-06-23 00:08:43 -0700518 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Roland McGrath10faa812008-01-30 13:30:54 +0100520 /*
521 * The processor cleared BTF, so don't mark that we need it set.
522 */
523 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
524 tsk->thread.debugctlmsr = 0;
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200527 SIGTRAP) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /* It's safe to allow irq's after DR6 has been saved */
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200531 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* Mask out spurious debug traps due to lazy DR7 setting */
534 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
Roland McGrath0f534092008-01-30 13:30:59 +0100535 if (!tsk->thread.debugreg7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 goto clear_dr7;
537 }
538
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300539 if (regs->flags & X86_VM_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 goto debug_vm86;
541
542 /* Save debug status register where ptrace can see it */
Roland McGrath0f534092008-01-30 13:30:59 +0100543 tsk->thread.debugreg6 = condition;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 /*
546 * Single-stepping through TF: make sure we ignore any events in
547 * kernel space (but re-enable TF when returning to user mode).
548 */
549 if (condition & DR_STEP) {
Vincent Hanquez717b5942005-06-23 00:08:45 -0700550 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 goto clear_TF_reenable;
552 }
553
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200554 si_code = get_si_code(condition);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* Ok, finally something we can handle */
Srinivasa Dsda654b72008-09-23 15:23:52 +0530556 send_sigtrap(tsk, regs, error_code, si_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Ingo Molnarb5964402008-02-26 11:15:50 +0100558 /*
559 * Disable additional traps. They'll be re-enabled when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 * the signal is delivered.
561 */
562clear_dr7:
Vincent Hanquez1cc6f122005-06-23 00:08:43 -0700563 set_debugreg(0, 7);
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200564 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return;
566
567debug_vm86:
568 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200569 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return;
571
572clear_TF_reenable:
573 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
Ingo Molnar60930152008-03-30 11:45:23 +0200574 regs->flags &= ~X86_EFLAGS_TF;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200575 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return;
577}
578
579/*
580 * Note that we play around with the 'TS' bit in an attempt to get
581 * the correct behaviour even in the presence of the asynchronous
582 * IRQ13 behaviour
583 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100584void math_error(void __user *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Ingo Molnarb5964402008-02-26 11:15:50 +0100586 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 siginfo_t info;
Alexander van Heukelum7b4fd4b2008-07-02 01:33:14 +0200588 unsigned short cwd, swd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 /*
591 * Save the info for the exception handler and clear the error.
592 */
593 task = current;
594 save_init_fpu(task);
595 task->thread.trap_no = 16;
596 task->thread.error_code = 0;
597 info.si_signo = SIGFPE;
598 info.si_errno = 0;
599 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100600 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 /*
602 * (~cwd & swd) will mask out exceptions that are not set to unmasked
603 * status. 0x3f is the exception bits in these regs, 0x200 is the
604 * C1 reg you need in case of a stack fault, 0x040 is the stack
605 * fault bit. We should only be taking one exception at a time,
606 * so if this combination doesn't produce any single exception,
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200607 * then we have a bad program that isn't synchronizing its FPU usage
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * and it will suffer the consequences since we won't be able to
609 * fully reproduce the context of the exception
610 */
611 cwd = get_fpu_cwd(task);
612 swd = get_fpu_swd(task);
Chuck Ebbertb1daec32005-08-23 21:36:40 -0400613 switch (swd & ~cwd & 0x3f) {
Ingo Molnarb5964402008-02-26 11:15:50 +0100614 case 0x000: /* No unmasked exception */
615 return;
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200616 default: /* Multiple exceptions */
Ingo Molnarb5964402008-02-26 11:15:50 +0100617 break;
618 case 0x001: /* Invalid Op */
619 /*
620 * swd & 0x240 == 0x040: Stack Underflow
621 * swd & 0x240 == 0x240: Stack Overflow
622 * User must clear the SF bit (0x40) if set
623 */
624 info.si_code = FPE_FLTINV;
625 break;
626 case 0x002: /* Denormalize */
627 case 0x010: /* Underflow */
628 info.si_code = FPE_FLTUND;
629 break;
630 case 0x004: /* Zero Divide */
631 info.si_code = FPE_FLTDIV;
632 break;
633 case 0x008: /* Overflow */
634 info.si_code = FPE_FLTOVF;
635 break;
636 case 0x020: /* Precision */
637 info.si_code = FPE_FLTRES;
638 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640 force_sig_info(SIGFPE, &info, task);
641}
642
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200643dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Alexander van Heukelum252d28f2008-09-09 21:56:09 +0200645 conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 ignore_fpu_irq = 1;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100647 math_error((void __user *)regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100650static void simd_math_error(void __user *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Ingo Molnarb5964402008-02-26 11:15:50 +0100652 struct task_struct *task;
Ingo Molnarb5964402008-02-26 11:15:50 +0100653 siginfo_t info;
Alexander van Heukelum7b4fd4b2008-07-02 01:33:14 +0200654 unsigned short mxcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 /*
657 * Save the info for the exception handler and clear the error.
658 */
659 task = current;
660 save_init_fpu(task);
661 task->thread.trap_no = 19;
662 task->thread.error_code = 0;
663 info.si_signo = SIGFPE;
664 info.si_errno = 0;
665 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100666 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /*
668 * The SIMD FPU exceptions are handled a little differently, as there
669 * is only a single status/control register. Thus, to determine which
670 * unmasked exception was caught we must mask the exception mask bits
671 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
672 */
673 mxcsr = get_fpu_mxcsr(task);
674 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
Ingo Molnarb5964402008-02-26 11:15:50 +0100675 case 0x000:
676 default:
677 break;
678 case 0x001: /* Invalid Op */
679 info.si_code = FPE_FLTINV;
680 break;
681 case 0x002: /* Denormalize */
682 case 0x010: /* Underflow */
683 info.si_code = FPE_FLTUND;
684 break;
685 case 0x004: /* Zero Divide */
686 info.si_code = FPE_FLTDIV;
687 break;
688 case 0x008: /* Overflow */
689 info.si_code = FPE_FLTOVF;
690 break;
691 case 0x020: /* Precision */
692 info.si_code = FPE_FLTRES;
693 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695 force_sig_info(SIGFPE, &info, task);
696}
697
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200698dotraplinkage void
699do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Alexander van Heukelumb939bde2008-09-09 21:56:12 +0200701 conditional_sti(regs);
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (cpu_has_xmm) {
704 /* Handle SIMD FPU exceptions on PIII+ processors. */
705 ignore_fpu_irq = 1;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100706 simd_math_error((void __user *)regs->ip);
Ingo Molnarb5964402008-02-26 11:15:50 +0100707 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
Ingo Molnarb5964402008-02-26 11:15:50 +0100709 /*
710 * Handle strange cache flush from user space exception
711 * in all other cases. This is undocumented behaviour.
712 */
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300713 if (regs->flags & X86_VM_MASK) {
Ingo Molnarb5964402008-02-26 11:15:50 +0100714 handle_vm86_fault((struct kernel_vm86_regs *)regs, error_code);
715 return;
716 }
717 current->thread.trap_no = 19;
718 current->thread.error_code = error_code;
719 die_if_kernel("cache flush denied", regs, error_code);
720 force_sig(SIGSEGV, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200723dotraplinkage void
724do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Alexander van Heukelumcf819782008-09-09 21:56:08 +0200726 conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727#if 0
728 /* No need to warn about this any longer. */
Ingo Molnarb5964402008-02-26 11:15:50 +0100729 printk(KERN_INFO "Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730#endif
731}
732
Ingo Molnarb5964402008-02-26 11:15:50 +0100733unsigned long patch_espfix_desc(unsigned long uesp, unsigned long kesp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Glauber Costa736f12b2008-05-27 20:14:51 -0700735 struct desc_struct *gdt = get_cpu_gdt_table(smp_processor_id());
Stas Sergeevbe44d2a2006-12-07 02:14:01 +0100736 unsigned long base = (kesp - uesp) & -THREAD_SIZE;
737 unsigned long new_kesp = kesp - base;
738 unsigned long lim_pages = (new_kesp | (THREAD_SIZE - 1)) >> PAGE_SHIFT;
739 __u64 desc = *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS];
Ingo Molnarb5964402008-02-26 11:15:50 +0100740
Stas Sergeevbe44d2a2006-12-07 02:14:01 +0100741 /* Set up base for espfix segment */
Ingo Molnarb5964402008-02-26 11:15:50 +0100742 desc &= 0x00f0ff0000000000ULL;
743 desc |= ((((__u64)base) << 16) & 0x000000ffffff0000ULL) |
Stas Sergeevbe44d2a2006-12-07 02:14:01 +0100744 ((((__u64)base) << 32) & 0xff00000000000000ULL) |
745 ((((__u64)lim_pages) << 32) & 0x000f000000000000ULL) |
746 (lim_pages & 0xffff);
747 *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS] = desc;
Ingo Molnarb5964402008-02-26 11:15:50 +0100748
Stas Sergeevbe44d2a2006-12-07 02:14:01 +0100749 return new_kesp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
752/*
Ingo Molnarb5964402008-02-26 11:15:50 +0100753 * 'math_state_restore()' saves the current math information in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 * old math state array, and gets the new ones from the current task
755 *
756 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
757 * Don't touch unless you *really* know how it works.
758 *
759 * Must be called with kernel preemption disabled (in this case,
760 * local interrupts are disabled at the call-site in entry.S).
761 */
Chuck Ebbertacc20762006-12-07 02:14:01 +0100762asmlinkage void math_state_restore(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 struct thread_info *thread = current_thread_info();
765 struct task_struct *tsk = thread->task;
766
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700767 if (!tsk_used_math(tsk)) {
768 local_irq_enable();
769 /*
770 * does a slab alloc which can sleep
771 */
772 if (init_fpu(tsk)) {
773 /*
774 * ran out of memory!
775 */
776 do_group_exit(SIGKILL);
777 return;
778 }
779 local_irq_disable();
780 }
781
Ingo Molnarb5964402008-02-26 11:15:50 +0100782 clts(); /* Allow maths ops (or we recurse) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 restore_fpu(tsk);
784 thread->status |= TS_USEDFPU; /* So we fnsave on switch_to() */
Chuck Ebbertacc20762006-12-07 02:14:01 +0100785 tsk->fpu_counter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
Rusty Russell5992b6d2007-07-19 01:49:21 -0700787EXPORT_SYMBOL_GPL(math_state_restore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789#ifndef CONFIG_MATH_EMULATION
790
791asmlinkage void math_emulate(long arg)
792{
Ingo Molnarb5964402008-02-26 11:15:50 +0100793 printk(KERN_EMERG
794 "math-emulation not enabled and no coprocessor found.\n");
795 printk(KERN_EMERG "killing %s.\n", current->comm);
796 force_sig(SIGFPE, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 schedule();
798}
799
800#endif /* CONFIG_MATH_EMULATION */
801
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200802dotraplinkage void __kprobes
803do_device_not_available(struct pt_regs *regs, long error)
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200804{
805 if (read_cr0() & X86_CR0_EM) {
806 conditional_sti(regs);
807 math_emulate(0);
808 } else {
809 math_state_restore(); /* interrupts still off */
810 conditional_sti(regs);
811 }
812}
813
Alexander van Heukelumeb642f62008-09-09 21:56:11 +0200814#ifdef CONFIG_X86_MCE
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200815dotraplinkage void __kprobes do_machine_check(struct pt_regs *regs, long error)
Alexander van Heukelumeb642f62008-09-09 21:56:11 +0200816{
817 conditional_sti(regs);
818 machine_check_vector(regs, error);
819}
820#endif
821
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200822dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200823{
824 siginfo_t info;
825 local_irq_enable();
826
827 info.si_signo = SIGILL;
828 info.si_errno = 0;
829 info.si_code = ILL_BADSTK;
830 info.si_addr = 0;
831 if (notify_die(DIE_TRAP, "iret exception",
832 regs, error_code, 32, SIGILL) == NOTIFY_STOP)
833 return;
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200834 do_trap(32, SIGILL, "iret exception", regs, error_code, &info);
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200835}
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837void __init trap_init(void)
838{
Rusty Russelldbeb2be2007-10-19 20:35:03 +0200839 int i;
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841#ifdef CONFIG_EISA
Ingo Molnar927222b2008-01-30 13:33:49 +0100842 void __iomem *p = early_ioremap(0x0FFFD9, 4);
Ingo Molnarb5964402008-02-26 11:15:50 +0100843
844 if (readl(p) == 'E' + ('I'<<8) + ('S'<<16) + ('A'<<24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 EISA_bus = 1;
Ingo Molnar927222b2008-01-30 13:33:49 +0100846 early_iounmap(p, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847#endif
848
Alexander van Heukelum976382d2008-09-09 21:55:57 +0200849 set_intr_gate(0, &divide_error);
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200850 set_intr_gate(1, &debug);
851 set_intr_gate(2, &nmi);
852 set_system_intr_gate(3, &int3); /* int3 can be called from all */
Alexander van Heukelum8d6f9d62008-09-09 21:55:59 +0200853 set_system_intr_gate(4, &overflow); /* int4 can be called from all */
Alexander van Heukelum64f644c2008-09-09 21:56:00 +0200854 set_intr_gate(5, &bounds);
Alexander van Heukelum12394cf2008-09-09 21:56:01 +0200855 set_intr_gate(6, &invalid_op);
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200856 set_intr_gate(7, &device_not_available);
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200857 set_task_gate(8, GDT_ENTRY_DOUBLEFAULT_TSS);
Alexander van Heukelum51bc1ed2008-09-09 21:56:03 +0200858 set_intr_gate(9, &coprocessor_segment_overrun);
Alexander van Heukelum6bf77bf2008-09-09 21:56:04 +0200859 set_intr_gate(10, &invalid_TSS);
Alexander van Heukelum36d936c2008-09-09 21:56:05 +0200860 set_intr_gate(11, &segment_not_present);
Alexander van Heukelumf5ca8182008-09-09 21:56:06 +0200861 set_intr_gate(12, &stack_segment);
Alexander van Heukelumc6df0d72008-09-09 21:56:07 +0200862 set_intr_gate(13, &general_protection);
Ingo Molnarb5964402008-02-26 11:15:50 +0100863 set_intr_gate(14, &page_fault);
Alexander van Heukelumcf819782008-09-09 21:56:08 +0200864 set_intr_gate(15, &spurious_interrupt_bug);
Alexander van Heukelum252d28f2008-09-09 21:56:09 +0200865 set_intr_gate(16, &coprocessor_error);
Alexander van Heukelum5feedfd2008-09-09 21:56:10 +0200866 set_intr_gate(17, &alignment_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867#ifdef CONFIG_X86_MCE
Alexander van Heukelumeb642f62008-09-09 21:56:11 +0200868 set_intr_gate(18, &machine_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869#endif
Alexander van Heukelumb939bde2008-09-09 21:56:12 +0200870 set_intr_gate(19, &simd_coprocessor_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Jan Beulichd43c6e82006-01-06 00:11:49 -0800872 if (cpu_has_fxsr) {
Jan Beulichd43c6e82006-01-06 00:11:49 -0800873 printk(KERN_INFO "Enabling fast FPU save and restore... ");
874 set_in_cr4(X86_CR4_OSFXSR);
875 printk("done.\n");
876 }
877 if (cpu_has_xmm) {
Ingo Molnarb5964402008-02-26 11:15:50 +0100878 printk(KERN_INFO
879 "Enabling unmasked SIMD FPU exception support... ");
Jan Beulichd43c6e82006-01-06 00:11:49 -0800880 set_in_cr4(X86_CR4_OSXMMEXCPT);
881 printk("done.\n");
882 }
883
Ingo Molnarb5964402008-02-26 11:15:50 +0100884 set_system_gate(SYSCALL_VECTOR, &system_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Ingo Molnarb5964402008-02-26 11:15:50 +0100886 /* Reserve all the builtin and the syscall vector: */
Rusty Russelldbeb2be2007-10-19 20:35:03 +0200887 for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
888 set_bit(i, used_vectors);
Ingo Molnarb5964402008-02-26 11:15:50 +0100889
Rusty Russelldbeb2be2007-10-19 20:35:03 +0200890 set_bit(SYSCALL_VECTOR, used_vectors);
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 /*
Ingo Molnarb5964402008-02-26 11:15:50 +0100893 * Should be a barrier for any external CPU state:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 */
895 cpu_init();
896
897 trap_init_hook();
898}