blob: 076739863d24bd664f2dad06f96e839651a2f863 [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>
23#include <linux/unwind.h>
24#include <linux/delay.h>
25#include <linux/errno.h>
26#include <linux/kexec.h>
27#include <linux/sched.h>
28#include <linux/timer.h>
29#include <linux/init.h>
Jeremy Fitzhardinge91768d62006-12-08 02:36:21 -080030#include <linux/bug.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010031#include <linux/nmi.h>
32#include <linux/mm.h>
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020033#include <linux/smp.h>
34#include <linux/io.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
Ingo Molnarb5964402008-02-26 11:15:50 +010049#include <asm/stacktrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/processor.h>
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020051#include <asm/kmemcheck.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/debugreg.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010053#include <asm/atomic.h>
54#include <asm/system.h>
55#include <asm/unwind.h>
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020056#include <asm/traps.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/desc.h>
58#include <asm/i387.h>
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +020059
60#include <mach_traps.h>
61
62#include <asm/processor-flags.h>
63#include <asm/arch_hooks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <asm/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <asm/smp.h>
Ingo Molnarb5964402008-02-26 11:15:50 +010066#include <asm/io.h>
Jaswinder Singh6ac8d512008-07-15 21:09:13 +053067#include <asm/traps.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Alexander van Heukelumeb642f62008-09-09 21:56:11 +020069#include "cpu/mcheck/mce.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Rusty Russelldbeb2be2007-10-19 20:35:03 +020071DECLARE_BITMAP(used_vectors, NR_VECTORS);
72EXPORT_SYMBOL_GPL(used_vectors);
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074asmlinkage int system_call(void);
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/* Do we ignore FPU interrupts ? */
Ingo Molnarb5964402008-02-26 11:15:50 +010077char ignore_fpu_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/*
80 * The IDT has to be page-aligned to simplify the Pentium
81 * F0 0F bug workaround.. We have a special link segment
82 * for this.
83 */
Glauber de Oliveira Costa010d4f82008-01-30 13:31:12 +010084gate_desc idt_table[256]
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +010085 __attribute__((__section__(".data.idt"))) = { { { { 0, 0 } } }, };
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
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
102static inline void preempt_conditional_cli(struct pt_regs *regs)
103{
104 if (regs->flags & X86_EFLAGS_IF)
105 local_irq_disable();
106 dec_preempt_count();
107}
108
Ingo Molnarb5964402008-02-26 11:15:50 +0100109static inline void
110die_if_kernel(const char *str, struct pt_regs *regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Vincent Hanquez717b5942005-06-23 00:08:45 -0700112 if (!user_mode_vm(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 die(str, regs, err);
114}
115
Alexander van Heukelumae821572008-09-30 18:41:35 +0200116/*
117 * Perform the lazy TSS's I/O bitmap copy. If the TSS has an
118 * invalid offset set (the LAZY one) and the faulting thread has
119 * a valid I/O bitmap pointer, we copy the I/O bitmap in the TSS,
120 * we set the offset field correctly and return 1.
121 */
122static int lazy_iobitmap_copy(void)
123{
124 struct thread_struct *thread;
125 struct tss_struct *tss;
126 int cpu;
127
128 cpu = get_cpu();
129 tss = &per_cpu(init_tss, cpu);
130 thread = &current->thread;
131
132 if (tss->x86_tss.io_bitmap_base == INVALID_IO_BITMAP_OFFSET_LAZY &&
133 thread->io_bitmap_ptr) {
134 memcpy(tss->io_bitmap, thread->io_bitmap_ptr,
135 thread->io_bitmap_max);
136 /*
137 * If the previously set map was extending to higher ports
138 * than the current one, pad extra space with 0xff (no access).
139 */
140 if (thread->io_bitmap_max < tss->io_bitmap_max) {
141 memset((char *) tss->io_bitmap +
142 thread->io_bitmap_max, 0xff,
143 tss->io_bitmap_max - thread->io_bitmap_max);
144 }
145 tss->io_bitmap_max = thread->io_bitmap_max;
146 tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
147 tss->io_bitmap_owner = thread;
148 put_cpu();
149
150 return 1;
151 }
152 put_cpu();
153
154 return 0;
155}
156
Ingo Molnarb5964402008-02-26 11:15:50 +0100157static void __kprobes
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200158do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
Ingo Molnarb5964402008-02-26 11:15:50 +0100159 long error_code, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700161 struct task_struct *tsk = current;
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700162
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300163 if (regs->flags & X86_VM_MASK) {
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200164 /*
165 * traps 0, 1, 3, 4, and 5 should be forwarded to vm86.
166 * On nmi (interrupt 2), do_trap should not be called.
167 */
168 if (trapnr < 6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 goto vm86_trap;
170 goto trap_signal;
171 }
172
Vincent Hanquez717b5942005-06-23 00:08:45 -0700173 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 goto kernel_trap;
175
Ingo Molnarb5964402008-02-26 11:15:50 +0100176trap_signal:
177 /*
178 * We want error_code and trap_no set for userspace faults and
179 * kernelspace faults which result in die(), but not
180 * kernelspace faults which are fixed up. die() gives the
181 * process no chance to handle the signal and notice the
182 * kernel fault information, so that won't result in polluting
183 * the information about previously queued, but not yet
184 * delivered, faults. See also do_general_protection below.
185 */
186 tsk->thread.error_code = error_code;
187 tsk->thread.trap_no = trapnr;
188
189 if (info)
190 force_sig_info(signr, info, tsk);
191 else
192 force_sig(signr, tsk);
193 return;
194
195kernel_trap:
196 if (!fixup_exception(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200197 tsk->thread.error_code = error_code;
198 tsk->thread.trap_no = trapnr;
Ingo Molnarb5964402008-02-26 11:15:50 +0100199 die(str, regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
Ingo Molnarb5964402008-02-26 11:15:50 +0100201 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Ingo Molnarb5964402008-02-26 11:15:50 +0100203vm86_trap:
204 if (handle_vm86_trap((struct kernel_vm86_regs *) regs,
205 error_code, trapnr))
206 goto trap_signal;
207 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Ingo Molnarb5964402008-02-26 11:15:50 +0100210#define DO_ERROR(trapnr, signr, str, name) \
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200211dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100212{ \
213 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200214 == NOTIFY_STOP) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100215 return; \
Alexander van Heukelum61aef7d2008-09-09 21:55:56 +0200216 conditional_sti(regs); \
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200217 do_trap(trapnr, signr, str, regs, error_code, NULL); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200220#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200221dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100222{ \
223 siginfo_t info; \
224 info.si_signo = signr; \
225 info.si_errno = 0; \
226 info.si_code = sicode; \
227 info.si_addr = (void __user *)siaddr; \
Ingo Molnarb5964402008-02-26 11:15:50 +0100228 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200229 == NOTIFY_STOP) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100230 return; \
Alexander van Heukelum61aef7d2008-09-09 21:55:56 +0200231 conditional_sti(regs); \
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200232 do_trap(trapnr, signr, str, regs, error_code, &info); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200235DO_ERROR_INFO(0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
236DO_ERROR(4, SIGSEGV, "overflow", overflow)
237DO_ERROR(5, SIGSEGV, "bounds", bounds)
238DO_ERROR_INFO(6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
Alexander van Heukelum51bc1ed2008-09-09 21:56:03 +0200239DO_ERROR(9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
Alexander van Heukelum6bf77bf2008-09-09 21:56:04 +0200240DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
Alexander van Heukelum36d936c2008-09-09 21:56:05 +0200241DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
Alexander van Heukelumf5ca8182008-09-09 21:56:06 +0200242DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200243DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200245dotraplinkage void __kprobes
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200246do_general_protection(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200248 struct task_struct *tsk;
Ingo Molnarb5964402008-02-26 11:15:50 +0100249
Alexander van Heukelumc6df0d72008-09-09 21:56:07 +0200250 conditional_sti(regs);
251
Alexander van Heukelumae821572008-09-30 18:41:35 +0200252 if (lazy_iobitmap_copy()) {
253 /* restart the faulting instruction */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return;
255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300257 if (regs->flags & X86_VM_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 goto gp_in_vm86;
259
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200260 tsk = current;
Vincent Hanquez717b5942005-06-23 00:08:45 -0700261 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 goto gp_in_kernel;
263
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200264 tsk->thread.error_code = error_code;
265 tsk->thread.trap_no = 13;
Ingo Molnarb5964402008-02-26 11:15:50 +0100266
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200267 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
268 printk_ratelimit()) {
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200269 printk(KERN_INFO
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200270 "%s[%d] general protection ip:%lx sp:%lx error:%lx",
271 tsk->comm, task_pid_nr(tsk),
272 regs->ip, regs->sp, error_code);
Andi Kleen03252912008-01-30 13:33:18 +0100273 print_vma_addr(" in ", regs->ip);
274 printk("\n");
275 }
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200276
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200277 force_sig(SIGSEGV, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return;
279
280gp_in_vm86:
281 local_irq_enable();
282 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
283 return;
284
285gp_in_kernel:
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200286 if (fixup_exception(regs))
287 return;
288
289 tsk->thread.error_code = error_code;
290 tsk->thread.trap_no = 13;
291 if (notify_die(DIE_GPF, "general protection fault", regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 error_code, 13, SIGSEGV) == NOTIFY_STOP)
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200293 return;
294 die("general protection fault", regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200297static notrace __kprobes void
Ingo Molnarb5964402008-02-26 11:15:50 +0100298mem_parity_error(unsigned char reason, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Ingo Molnarb5964402008-02-26 11:15:50 +0100300 printk(KERN_EMERG
301 "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
302 reason, smp_processor_id());
303
304 printk(KERN_EMERG
305 "You have some hardware problem, likely on the PCI bus.\n");
Dave Jiangc0d12172007-07-19 01:49:46 -0700306
307#if defined(CONFIG_EDAC)
Ingo Molnarb5964402008-02-26 11:15:50 +0100308 if (edac_handler_set()) {
Dave Jiangc0d12172007-07-19 01:49:46 -0700309 edac_atomic_assert_error();
310 return;
311 }
312#endif
313
Don Zickus8da5add2006-09-26 10:52:27 +0200314 if (panic_on_unrecovered_nmi)
Ingo Molnarb5964402008-02-26 11:15:50 +0100315 panic("NMI: Not continuing");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Don Zickusc41c5cd2006-09-26 10:52:27 +0200317 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 /* Clear and disable the memory parity error line. */
Alexander van Heukelum79704792008-10-03 22:00:36 +0200320 reason = (reason & 0xf) | 4;
321 outb(reason, 0x61);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200324static notrace __kprobes void
Ingo Molnarb5964402008-02-26 11:15:50 +0100325io_check_error(unsigned char reason, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 unsigned long i;
328
Dave Jones9c107802006-01-09 20:51:32 -0800329 printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 show_registers(regs);
331
332 /* Re-enable the IOCK line, wait for a few seconds */
333 reason = (reason & 0xf) | 8;
334 outb(reason, 0x61);
Ingo Molnarb5964402008-02-26 11:15:50 +0100335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 i = 2000;
Ingo Molnarb5964402008-02-26 11:15:50 +0100337 while (--i)
338 udelay(1000);
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 reason &= ~8;
341 outb(reason, 0x61);
342}
343
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200344static notrace __kprobes void
Ingo Molnarb5964402008-02-26 11:15:50 +0100345unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200347 if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) ==
348 NOTIFY_STOP)
Jason Wesseld3597522008-02-15 14:55:53 -0600349 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350#ifdef CONFIG_MCA
Ingo Molnarb5964402008-02-26 11:15:50 +0100351 /*
352 * Might actually be able to figure out what the guilty party
353 * is:
354 */
355 if (MCA_bus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 mca_handle_nmi();
357 return;
358 }
359#endif
Ingo Molnarb5964402008-02-26 11:15:50 +0100360 printk(KERN_EMERG
361 "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
362 reason, smp_processor_id());
363
Don Zickusc41c5cd2006-09-26 10:52:27 +0200364 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
Don Zickus8da5add2006-09-26 10:52:27 +0200365 if (panic_on_unrecovered_nmi)
Ingo Molnarb5964402008-02-26 11:15:50 +0100366 panic("NMI: Not continuing");
Don Zickus8da5add2006-09-26 10:52:27 +0200367
Don Zickusc41c5cd2006-09-26 10:52:27 +0200368 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
371static DEFINE_SPINLOCK(nmi_print_lock);
372
Cyrill Gorcunovddca03c2008-05-24 19:36:31 +0400373void notrace __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Cyrill Gorcunovddca03c2008-05-24 19:36:31 +0400375 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
George Anzinger748f2ed2005-09-03 15:56:48 -0700376 return;
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 spin_lock(&nmi_print_lock);
379 /*
380 * We are in trouble anyway, lets at least try
Ingo Molnarb5964402008-02-26 11:15:50 +0100381 * to get a message out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
383 bust_spinlocks(1);
Cyrill Gorcunovddca03c2008-05-24 19:36:31 +0400384 printk(KERN_EMERG "%s", str);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100385 printk(" on CPU%d, ip %08lx, registers:\n",
386 smp_processor_id(), regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 show_registers(regs);
Cyrill Gorcunovddca03c2008-05-24 19:36:31 +0400388 if (do_panic)
389 panic("Non maskable interrupt");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 console_silent();
391 spin_unlock(&nmi_print_lock);
392 bust_spinlocks(0);
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700393
Ingo Molnarb5964402008-02-26 11:15:50 +0100394 /*
395 * If we are in kernel we are probably nested up pretty bad
396 * and might aswell get out now while we still can:
397 */
Jan Beulichdb753bd2006-03-23 02:59:46 -0800398 if (!user_mode_vm(regs)) {
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700399 current->thread.trap_no = 2;
400 crash_kexec(regs);
401 }
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 do_exit(SIGSEGV);
404}
405
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200406static notrace __kprobes void default_do_nmi(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 unsigned char reason = 0;
Alexander van Heukelumabd34802008-07-02 18:39:01 +0200409 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Alexander van Heukelumabd34802008-07-02 18:39:01 +0200411 cpu = smp_processor_id();
412
413 /* Only the BSP gets external NMIs from the system. */
414 if (!cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 reason = get_nmi_reason();
Ingo Molnarb5964402008-02-26 11:15:50 +0100416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (!(reason & 0xc0)) {
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800418 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200419 == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return;
421#ifdef CONFIG_X86_LOCAL_APIC
422 /*
423 * Ok, so this is none of the documented NMI sources,
424 * so it must be the NMI watchdog.
425 */
Don Zickus3adbbcc2006-09-26 10:52:26 +0200426 if (nmi_watchdog_tick(regs, reason))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return;
Alexander van Heukelumabd34802008-07-02 18:39:01 +0200428 if (!do_nmi_callback(regs, cpu))
Don Zickus3adbbcc2006-09-26 10:52:26 +0200429 unknown_nmi_error(reason, regs);
Ingo Molnarb5964402008-02-26 11:15:50 +0100430#else
431 unknown_nmi_error(reason, regs);
432#endif
Don Zickus2fbe7b22006-09-26 10:52:27 +0200433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return;
435 }
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800436 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return;
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200438
439 /* AK: following checks seem to be broken on modern chipsets. FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (reason & 0x80)
441 mem_parity_error(reason, regs);
442 if (reason & 0x40)
443 io_check_error(reason, regs);
444 /*
445 * Reassert NMI in case it became active meanwhile
Ingo Molnarb5964402008-02-26 11:15:50 +0100446 * as it's edge-triggered:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 */
448 reassert_nmi();
449}
450
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200451dotraplinkage notrace __kprobes void
452do_nmi(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 nmi_enter();
455
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200456 { int cpu; cpu = smp_processor_id(); ++nmi_count(cpu); }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Andi Kleen8f4e9562007-07-22 11:12:32 +0200458 if (!ignore_nmis)
459 default_do_nmi(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 nmi_exit();
462}
463
Andi Kleen8f4e9562007-07-22 11:12:32 +0200464void stop_nmi(void)
465{
466 acpi_nmi_disable();
467 ignore_nmis++;
468}
469
470void restart_nmi(void)
471{
472 ignore_nmis--;
473 acpi_nmi_enable();
474}
475
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200476/* May run on IST stack. */
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200477dotraplinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Alexander van Heukelumb94da1e2008-09-09 21:55:58 +0200479#ifdef CONFIG_KPROBES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
481 == NOTIFY_STOP)
Stas Sergeev48c882112005-05-01 08:58:49 -0700482 return;
Alexander van Heukelumb94da1e2008-09-09 21:55:58 +0200483#else
484 if (notify_die(DIE_TRAP, "int3", regs, error_code, 3, SIGTRAP)
485 == NOTIFY_STOP)
486 return;
487#endif
Ingo Molnarb5964402008-02-26 11:15:50 +0100488
Alexander van Heukelum4915a352008-10-03 22:00:34 +0200489 preempt_conditional_sti(regs);
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200490 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
Alexander van Heukelum4915a352008-10-03 22:00:34 +0200491 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494/*
495 * Our handling of the processor debug registers is non-trivial.
496 * We do not clear them on entry and exit from the kernel. Therefore
497 * it is possible to get a watchpoint trap here from inside the kernel.
498 * However, the code in ./ptrace.c has ensured that the user can
499 * only set watchpoints on userspace addresses. Therefore the in-kernel
500 * watchpoint trap can only occur in code which is reading/writing
501 * from user space. Such code must not hold kernel locks (since it
502 * can equally take a page fault), therefore it is safe to call
503 * force_sig_info even though that claims and releases locks.
Ingo Molnarb5964402008-02-26 11:15:50 +0100504 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 * Code in ./signal.c ensures that the debug control register
506 * is restored before we deliver any signal, and therefore that
507 * user code runs with the correct debug control register even though
508 * we clear it here.
509 *
510 * Being careful here means that we don't have to be as careful in a
511 * lot of more complicated places (task switching can be a bit lazy
512 * about restoring all the debug state, and ptrace doesn't have to
513 * find every occurrence of the TF bit that could be saved away even
514 * by user code)
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200515 *
516 * May run on IST stack.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 */
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200518dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 struct task_struct *tsk = current;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200521 unsigned long condition;
Srinivasa Dsda654b72008-09-23 15:23:52 +0530522 int si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Vincent Hanquez1cc6f122005-06-23 00:08:43 -0700524 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Roland McGrath10faa812008-01-30 13:30:54 +0100526 /*
527 * The processor cleared BTF, so don't mark that we need it set.
528 */
529 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
530 tsk->thread.debugctlmsr = 0;
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200533 SIGTRAP) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /* It's safe to allow irq's after DR6 has been saved */
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200537 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 /* Mask out spurious debug traps due to lazy DR7 setting */
540 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
Roland McGrath0f534092008-01-30 13:30:59 +0100541 if (!tsk->thread.debugreg7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 goto clear_dr7;
543 }
544
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300545 if (regs->flags & X86_VM_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 goto debug_vm86;
547
548 /* Save debug status register where ptrace can see it */
Roland McGrath0f534092008-01-30 13:30:59 +0100549 tsk->thread.debugreg6 = condition;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /*
552 * Single-stepping through TF: make sure we ignore any events in
553 * kernel space (but re-enable TF when returning to user mode).
554 */
555 if (condition & DR_STEP) {
Vincent Hanquez717b5942005-06-23 00:08:45 -0700556 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 goto clear_TF_reenable;
558 }
559
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200560 si_code = get_si_code(condition);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* Ok, finally something we can handle */
Srinivasa Dsda654b72008-09-23 15:23:52 +0530562 send_sigtrap(tsk, regs, error_code, si_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Ingo Molnarb5964402008-02-26 11:15:50 +0100564 /*
565 * Disable additional traps. They'll be re-enabled when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 * the signal is delivered.
567 */
568clear_dr7:
Vincent Hanquez1cc6f122005-06-23 00:08:43 -0700569 set_debugreg(0, 7);
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200570 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return;
572
573debug_vm86:
574 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200575 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return;
577
578clear_TF_reenable:
579 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
Ingo Molnar60930152008-03-30 11:45:23 +0200580 regs->flags &= ~X86_EFLAGS_TF;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200581 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return;
583}
584
585/*
586 * Note that we play around with the 'TS' bit in an attempt to get
587 * the correct behaviour even in the presence of the asynchronous
588 * IRQ13 behaviour
589 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100590void math_error(void __user *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Ingo Molnarb5964402008-02-26 11:15:50 +0100592 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 siginfo_t info;
Alexander van Heukelum7b4fd4b2008-07-02 01:33:14 +0200594 unsigned short cwd, swd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 /*
597 * Save the info for the exception handler and clear the error.
598 */
599 task = current;
600 save_init_fpu(task);
601 task->thread.trap_no = 16;
602 task->thread.error_code = 0;
603 info.si_signo = SIGFPE;
604 info.si_errno = 0;
605 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100606 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /*
608 * (~cwd & swd) will mask out exceptions that are not set to unmasked
609 * status. 0x3f is the exception bits in these regs, 0x200 is the
610 * C1 reg you need in case of a stack fault, 0x040 is the stack
611 * fault bit. We should only be taking one exception at a time,
612 * so if this combination doesn't produce any single exception,
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200613 * then we have a bad program that isn't synchronizing its FPU usage
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 * and it will suffer the consequences since we won't be able to
615 * fully reproduce the context of the exception
616 */
617 cwd = get_fpu_cwd(task);
618 swd = get_fpu_swd(task);
Chuck Ebbertb1daec32005-08-23 21:36:40 -0400619 switch (swd & ~cwd & 0x3f) {
Ingo Molnarb5964402008-02-26 11:15:50 +0100620 case 0x000: /* No unmasked exception */
621 return;
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200622 default: /* Multiple exceptions */
Ingo Molnarb5964402008-02-26 11:15:50 +0100623 break;
624 case 0x001: /* Invalid Op */
625 /*
626 * swd & 0x240 == 0x040: Stack Underflow
627 * swd & 0x240 == 0x240: Stack Overflow
628 * User must clear the SF bit (0x40) if set
629 */
630 info.si_code = FPE_FLTINV;
631 break;
632 case 0x002: /* Denormalize */
633 case 0x010: /* Underflow */
634 info.si_code = FPE_FLTUND;
635 break;
636 case 0x004: /* Zero Divide */
637 info.si_code = FPE_FLTDIV;
638 break;
639 case 0x008: /* Overflow */
640 info.si_code = FPE_FLTOVF;
641 break;
642 case 0x020: /* Precision */
643 info.si_code = FPE_FLTRES;
644 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646 force_sig_info(SIGFPE, &info, task);
647}
648
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200649dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Alexander van Heukelum252d28f2008-09-09 21:56:09 +0200651 conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 ignore_fpu_irq = 1;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100653 math_error((void __user *)regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100656static void simd_math_error(void __user *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Ingo Molnarb5964402008-02-26 11:15:50 +0100658 struct task_struct *task;
Ingo Molnarb5964402008-02-26 11:15:50 +0100659 siginfo_t info;
Alexander van Heukelum7b4fd4b2008-07-02 01:33:14 +0200660 unsigned short mxcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 /*
663 * Save the info for the exception handler and clear the error.
664 */
665 task = current;
666 save_init_fpu(task);
667 task->thread.trap_no = 19;
668 task->thread.error_code = 0;
669 info.si_signo = SIGFPE;
670 info.si_errno = 0;
671 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100672 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /*
674 * The SIMD FPU exceptions are handled a little differently, as there
675 * is only a single status/control register. Thus, to determine which
676 * unmasked exception was caught we must mask the exception mask bits
677 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
678 */
679 mxcsr = get_fpu_mxcsr(task);
680 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
Ingo Molnarb5964402008-02-26 11:15:50 +0100681 case 0x000:
682 default:
683 break;
684 case 0x001: /* Invalid Op */
685 info.si_code = FPE_FLTINV;
686 break;
687 case 0x002: /* Denormalize */
688 case 0x010: /* Underflow */
689 info.si_code = FPE_FLTUND;
690 break;
691 case 0x004: /* Zero Divide */
692 info.si_code = FPE_FLTDIV;
693 break;
694 case 0x008: /* Overflow */
695 info.si_code = FPE_FLTOVF;
696 break;
697 case 0x020: /* Precision */
698 info.si_code = FPE_FLTRES;
699 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
701 force_sig_info(SIGFPE, &info, task);
702}
703
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200704dotraplinkage void
705do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Alexander van Heukelumb939bde2008-09-09 21:56:12 +0200707 conditional_sti(regs);
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (cpu_has_xmm) {
710 /* Handle SIMD FPU exceptions on PIII+ processors. */
711 ignore_fpu_irq = 1;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100712 simd_math_error((void __user *)regs->ip);
Ingo Molnarb5964402008-02-26 11:15:50 +0100713 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
Ingo Molnarb5964402008-02-26 11:15:50 +0100715 /*
716 * Handle strange cache flush from user space exception
717 * in all other cases. This is undocumented behaviour.
718 */
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300719 if (regs->flags & X86_VM_MASK) {
Ingo Molnarb5964402008-02-26 11:15:50 +0100720 handle_vm86_fault((struct kernel_vm86_regs *)regs, error_code);
721 return;
722 }
723 current->thread.trap_no = 19;
724 current->thread.error_code = error_code;
725 die_if_kernel("cache flush denied", regs, error_code);
726 force_sig(SIGSEGV, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200729dotraplinkage void
730do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Alexander van Heukelumcf819782008-09-09 21:56:08 +0200732 conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733#if 0
734 /* No need to warn about this any longer. */
Ingo Molnarb5964402008-02-26 11:15:50 +0100735 printk(KERN_INFO "Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736#endif
737}
738
Ingo Molnarb5964402008-02-26 11:15:50 +0100739unsigned long patch_espfix_desc(unsigned long uesp, unsigned long kesp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Glauber Costa736f12b2008-05-27 20:14:51 -0700741 struct desc_struct *gdt = get_cpu_gdt_table(smp_processor_id());
Stas Sergeevbe44d2a2006-12-07 02:14:01 +0100742 unsigned long base = (kesp - uesp) & -THREAD_SIZE;
743 unsigned long new_kesp = kesp - base;
744 unsigned long lim_pages = (new_kesp | (THREAD_SIZE - 1)) >> PAGE_SHIFT;
745 __u64 desc = *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS];
Ingo Molnarb5964402008-02-26 11:15:50 +0100746
Stas Sergeevbe44d2a2006-12-07 02:14:01 +0100747 /* Set up base for espfix segment */
Ingo Molnarb5964402008-02-26 11:15:50 +0100748 desc &= 0x00f0ff0000000000ULL;
749 desc |= ((((__u64)base) << 16) & 0x000000ffffff0000ULL) |
Stas Sergeevbe44d2a2006-12-07 02:14:01 +0100750 ((((__u64)base) << 32) & 0xff00000000000000ULL) |
751 ((((__u64)lim_pages) << 32) & 0x000f000000000000ULL) |
752 (lim_pages & 0xffff);
753 *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS] = desc;
Ingo Molnarb5964402008-02-26 11:15:50 +0100754
Stas Sergeevbe44d2a2006-12-07 02:14:01 +0100755 return new_kesp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
758/*
Ingo Molnarb5964402008-02-26 11:15:50 +0100759 * 'math_state_restore()' saves the current math information in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 * old math state array, and gets the new ones from the current task
761 *
762 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
763 * Don't touch unless you *really* know how it works.
764 *
765 * Must be called with kernel preemption disabled (in this case,
766 * local interrupts are disabled at the call-site in entry.S).
767 */
Chuck Ebbertacc20762006-12-07 02:14:01 +0100768asmlinkage void math_state_restore(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 struct thread_info *thread = current_thread_info();
771 struct task_struct *tsk = thread->task;
772
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700773 if (!tsk_used_math(tsk)) {
774 local_irq_enable();
775 /*
776 * does a slab alloc which can sleep
777 */
778 if (init_fpu(tsk)) {
779 /*
780 * ran out of memory!
781 */
782 do_group_exit(SIGKILL);
783 return;
784 }
785 local_irq_disable();
786 }
787
Ingo Molnarb5964402008-02-26 11:15:50 +0100788 clts(); /* Allow maths ops (or we recurse) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 restore_fpu(tsk);
790 thread->status |= TS_USEDFPU; /* So we fnsave on switch_to() */
Chuck Ebbertacc20762006-12-07 02:14:01 +0100791 tsk->fpu_counter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
Rusty Russell5992b6d2007-07-19 01:49:21 -0700793EXPORT_SYMBOL_GPL(math_state_restore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795#ifndef CONFIG_MATH_EMULATION
796
797asmlinkage void math_emulate(long arg)
798{
Ingo Molnarb5964402008-02-26 11:15:50 +0100799 printk(KERN_EMERG
800 "math-emulation not enabled and no coprocessor found.\n");
801 printk(KERN_EMERG "killing %s.\n", current->comm);
802 force_sig(SIGFPE, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 schedule();
804}
805
806#endif /* CONFIG_MATH_EMULATION */
807
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200808dotraplinkage void __kprobes
809do_device_not_available(struct pt_regs *regs, long error)
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200810{
811 if (read_cr0() & X86_CR0_EM) {
812 conditional_sti(regs);
813 math_emulate(0);
814 } else {
815 math_state_restore(); /* interrupts still off */
816 conditional_sti(regs);
817 }
818}
819
Alexander van Heukelumeb642f62008-09-09 21:56:11 +0200820#ifdef CONFIG_X86_MCE
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200821dotraplinkage void __kprobes do_machine_check(struct pt_regs *regs, long error)
Alexander van Heukelumeb642f62008-09-09 21:56:11 +0200822{
823 conditional_sti(regs);
824 machine_check_vector(regs, error);
825}
826#endif
827
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200828dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200829{
830 siginfo_t info;
831 local_irq_enable();
832
833 info.si_signo = SIGILL;
834 info.si_errno = 0;
835 info.si_code = ILL_BADSTK;
836 info.si_addr = 0;
837 if (notify_die(DIE_TRAP, "iret exception",
838 regs, error_code, 32, SIGILL) == NOTIFY_STOP)
839 return;
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200840 do_trap(32, SIGILL, "iret exception", regs, error_code, &info);
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200841}
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843void __init trap_init(void)
844{
Rusty Russelldbeb2be2007-10-19 20:35:03 +0200845 int i;
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847#ifdef CONFIG_EISA
Ingo Molnar927222b2008-01-30 13:33:49 +0100848 void __iomem *p = early_ioremap(0x0FFFD9, 4);
Ingo Molnarb5964402008-02-26 11:15:50 +0100849
850 if (readl(p) == 'E' + ('I'<<8) + ('S'<<16) + ('A'<<24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 EISA_bus = 1;
Ingo Molnar927222b2008-01-30 13:33:49 +0100852 early_iounmap(p, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853#endif
854
Alexander van Heukelum976382d2008-09-09 21:55:57 +0200855 set_intr_gate(0, &divide_error);
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200856 set_intr_gate_ist(1, &debug, DEBUG_STACK);
857 set_intr_gate_ist(2, &nmi, NMI_STACK);
858 /* int3 can be called from all */
859 set_system_intr_gate_ist(3, &int3, DEBUG_STACK);
860 /* int4 can be called from all */
861 set_system_intr_gate(4, &overflow);
Alexander van Heukelum64f644c2008-09-09 21:56:00 +0200862 set_intr_gate(5, &bounds);
Alexander van Heukelum12394cf2008-09-09 21:56:01 +0200863 set_intr_gate(6, &invalid_op);
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200864 set_intr_gate(7, &device_not_available);
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200865 set_task_gate(8, GDT_ENTRY_DOUBLEFAULT_TSS);
Alexander van Heukelum51bc1ed2008-09-09 21:56:03 +0200866 set_intr_gate(9, &coprocessor_segment_overrun);
Alexander van Heukelum6bf77bf2008-09-09 21:56:04 +0200867 set_intr_gate(10, &invalid_TSS);
Alexander van Heukelum36d936c2008-09-09 21:56:05 +0200868 set_intr_gate(11, &segment_not_present);
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200869 set_intr_gate_ist(12, &stack_segment, STACKFAULT_STACK);
Alexander van Heukelumc6df0d72008-09-09 21:56:07 +0200870 set_intr_gate(13, &general_protection);
Ingo Molnarb5964402008-02-26 11:15:50 +0100871 set_intr_gate(14, &page_fault);
Alexander van Heukelumcf819782008-09-09 21:56:08 +0200872 set_intr_gate(15, &spurious_interrupt_bug);
Alexander van Heukelum252d28f2008-09-09 21:56:09 +0200873 set_intr_gate(16, &coprocessor_error);
Alexander van Heukelum5feedfd2008-09-09 21:56:10 +0200874 set_intr_gate(17, &alignment_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875#ifdef CONFIG_X86_MCE
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200876 set_intr_gate_ist(18, &machine_check, MCE_STACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877#endif
Alexander van Heukelumb939bde2008-09-09 21:56:12 +0200878 set_intr_gate(19, &simd_coprocessor_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Jan Beulichd43c6e82006-01-06 00:11:49 -0800880 if (cpu_has_fxsr) {
Jan Beulichd43c6e82006-01-06 00:11:49 -0800881 printk(KERN_INFO "Enabling fast FPU save and restore... ");
882 set_in_cr4(X86_CR4_OSFXSR);
883 printk("done.\n");
884 }
885 if (cpu_has_xmm) {
Ingo Molnarb5964402008-02-26 11:15:50 +0100886 printk(KERN_INFO
887 "Enabling unmasked SIMD FPU exception support... ");
Jan Beulichd43c6e82006-01-06 00:11:49 -0800888 set_in_cr4(X86_CR4_OSXMMEXCPT);
889 printk("done.\n");
890 }
891
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200892 set_system_trap_gate(SYSCALL_VECTOR, &system_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Ingo Molnarb5964402008-02-26 11:15:50 +0100894 /* Reserve all the builtin and the syscall vector: */
Rusty Russelldbeb2be2007-10-19 20:35:03 +0200895 for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
896 set_bit(i, used_vectors);
Ingo Molnarb5964402008-02-26 11:15:50 +0100897
Rusty Russelldbeb2be2007-10-19 20:35:03 +0200898 set_bit(SYSCALL_VECTOR, used_vectors);
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 /*
Ingo Molnarb5964402008-02-26 11:15:50 +0100901 * Should be a barrier for any external CPU state:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 */
903 cpu_init();
904
905 trap_init_hook();
906}