blob: 7932338d7cb3112c7222121e513db0ca71190fa0 [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
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
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200109#ifdef CONFIG_X86_32
Ingo Molnarb5964402008-02-26 11:15:50 +0100110static inline void
111die_if_kernel(const char *str, struct pt_regs *regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Vincent Hanquez717b5942005-06-23 00:08:45 -0700113 if (!user_mode_vm(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 die(str, regs, err);
115}
116
Alexander van Heukelumae821572008-09-30 18:41:35 +0200117/*
118 * Perform the lazy TSS's I/O bitmap copy. If the TSS has an
119 * invalid offset set (the LAZY one) and the faulting thread has
120 * a valid I/O bitmap pointer, we copy the I/O bitmap in the TSS,
121 * we set the offset field correctly and return 1.
122 */
123static int lazy_iobitmap_copy(void)
124{
125 struct thread_struct *thread;
126 struct tss_struct *tss;
127 int cpu;
128
129 cpu = get_cpu();
130 tss = &per_cpu(init_tss, cpu);
131 thread = &current->thread;
132
133 if (tss->x86_tss.io_bitmap_base == INVALID_IO_BITMAP_OFFSET_LAZY &&
134 thread->io_bitmap_ptr) {
135 memcpy(tss->io_bitmap, thread->io_bitmap_ptr,
136 thread->io_bitmap_max);
137 /*
138 * If the previously set map was extending to higher ports
139 * than the current one, pad extra space with 0xff (no access).
140 */
141 if (thread->io_bitmap_max < tss->io_bitmap_max) {
142 memset((char *) tss->io_bitmap +
143 thread->io_bitmap_max, 0xff,
144 tss->io_bitmap_max - thread->io_bitmap_max);
145 }
146 tss->io_bitmap_max = thread->io_bitmap_max;
147 tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
148 tss->io_bitmap_owner = thread;
149 put_cpu();
150
151 return 1;
152 }
153 put_cpu();
154
155 return 0;
156}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200157#endif
Alexander van Heukelumae821572008-09-30 18:41:35 +0200158
Ingo Molnarb5964402008-02-26 11:15:50 +0100159static void __kprobes
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200160do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
Ingo Molnarb5964402008-02-26 11:15:50 +0100161 long error_code, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700163 struct task_struct *tsk = current;
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700164
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200165#ifdef CONFIG_X86_32
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300166 if (regs->flags & X86_VM_MASK) {
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200167 /*
168 * traps 0, 1, 3, 4, and 5 should be forwarded to vm86.
169 * On nmi (interrupt 2), do_trap should not be called.
170 */
171 if (trapnr < 6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 goto vm86_trap;
173 goto trap_signal;
174 }
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200175#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Vincent Hanquez717b5942005-06-23 00:08:45 -0700177 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 goto kernel_trap;
179
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200180#ifdef CONFIG_X86_32
Ingo Molnarb5964402008-02-26 11:15:50 +0100181trap_signal:
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200182#endif
Ingo Molnarb5964402008-02-26 11:15:50 +0100183 /*
184 * We want error_code and trap_no set for userspace faults and
185 * kernelspace faults which result in die(), but not
186 * kernelspace faults which are fixed up. die() gives the
187 * process no chance to handle the signal and notice the
188 * kernel fault information, so that won't result in polluting
189 * the information about previously queued, but not yet
190 * delivered, faults. See also do_general_protection below.
191 */
192 tsk->thread.error_code = error_code;
193 tsk->thread.trap_no = trapnr;
194
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200195#ifdef CONFIG_X86_64
196 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
197 printk_ratelimit()) {
198 printk(KERN_INFO
199 "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
200 tsk->comm, tsk->pid, str,
201 regs->ip, regs->sp, error_code);
202 print_vma_addr(" in ", regs->ip);
203 printk("\n");
204 }
205#endif
206
Ingo Molnarb5964402008-02-26 11:15:50 +0100207 if (info)
208 force_sig_info(signr, info, tsk);
209 else
210 force_sig(signr, tsk);
211 return;
212
213kernel_trap:
214 if (!fixup_exception(regs)) {
Andi Kleend1895182007-05-02 19:27:05 +0200215 tsk->thread.error_code = error_code;
216 tsk->thread.trap_no = trapnr;
Ingo Molnarb5964402008-02-26 11:15:50 +0100217 die(str, regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
Ingo Molnarb5964402008-02-26 11:15:50 +0100219 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200221#ifdef CONFIG_X86_32
Ingo Molnarb5964402008-02-26 11:15:50 +0100222vm86_trap:
223 if (handle_vm86_trap((struct kernel_vm86_regs *) regs,
224 error_code, trapnr))
225 goto trap_signal;
226 return;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200227#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Ingo Molnarb5964402008-02-26 11:15:50 +0100230#define DO_ERROR(trapnr, signr, str, name) \
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200231dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100232{ \
233 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200234 == NOTIFY_STOP) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100235 return; \
Alexander van Heukelum61aef7d2008-09-09 21:55:56 +0200236 conditional_sti(regs); \
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200237 do_trap(trapnr, signr, str, regs, error_code, NULL); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200240#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200241dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100242{ \
243 siginfo_t info; \
244 info.si_signo = signr; \
245 info.si_errno = 0; \
246 info.si_code = sicode; \
247 info.si_addr = (void __user *)siaddr; \
Ingo Molnarb5964402008-02-26 11:15:50 +0100248 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200249 == NOTIFY_STOP) \
Ingo Molnarb5964402008-02-26 11:15:50 +0100250 return; \
Alexander van Heukelum61aef7d2008-09-09 21:55:56 +0200251 conditional_sti(regs); \
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200252 do_trap(trapnr, signr, str, regs, error_code, &info); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200255DO_ERROR_INFO(0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
256DO_ERROR(4, SIGSEGV, "overflow", overflow)
257DO_ERROR(5, SIGSEGV, "bounds", bounds)
258DO_ERROR_INFO(6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
Alexander van Heukelum51bc1ed2008-09-09 21:56:03 +0200259DO_ERROR(9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
Alexander van Heukelum6bf77bf2008-09-09 21:56:04 +0200260DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
Alexander van Heukelum36d936c2008-09-09 21:56:05 +0200261DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200262#ifdef CONFIG_X86_32
Alexander van Heukelumf5ca8182008-09-09 21:56:06 +0200263DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200264#endif
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200265DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200267#ifdef CONFIG_X86_64
268/* Runs on IST stack */
269dotraplinkage void do_stack_segment(struct pt_regs *regs, long error_code)
270{
271 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
272 12, SIGBUS) == NOTIFY_STOP)
273 return;
274 preempt_conditional_sti(regs);
275 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
276 preempt_conditional_cli(regs);
277}
278
279dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code)
280{
281 static const char str[] = "double fault";
282 struct task_struct *tsk = current;
283
284 /* Return not checked because double check cannot be ignored */
285 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
286
287 tsk->thread.error_code = error_code;
288 tsk->thread.trap_no = 8;
289
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100290 /*
291 * This is always a kernel trap and never fixable (and thus must
292 * never return).
293 */
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200294 for (;;)
295 die(str, regs, error_code);
296}
297#endif
298
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200299dotraplinkage void __kprobes
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200300do_general_protection(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200302 struct task_struct *tsk;
Ingo Molnarb5964402008-02-26 11:15:50 +0100303
Alexander van Heukelumc6df0d72008-09-09 21:56:07 +0200304 conditional_sti(regs);
305
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200306#ifdef CONFIG_X86_32
Alexander van Heukelumae821572008-09-30 18:41:35 +0200307 if (lazy_iobitmap_copy()) {
308 /* restart the faulting instruction */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return;
310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300312 if (regs->flags & X86_VM_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 goto gp_in_vm86;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200314#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200316 tsk = current;
Vincent Hanquez717b5942005-06-23 00:08:45 -0700317 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 goto gp_in_kernel;
319
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200320 tsk->thread.error_code = error_code;
321 tsk->thread.trap_no = 13;
Ingo Molnarb5964402008-02-26 11:15:50 +0100322
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200323 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
324 printk_ratelimit()) {
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200325 printk(KERN_INFO
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200326 "%s[%d] general protection ip:%lx sp:%lx error:%lx",
327 tsk->comm, task_pid_nr(tsk),
328 regs->ip, regs->sp, error_code);
Andi Kleen03252912008-01-30 13:33:18 +0100329 print_vma_addr(" in ", regs->ip);
330 printk("\n");
331 }
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200332
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200333 force_sig(SIGSEGV, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return;
335
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200336#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337gp_in_vm86:
338 local_irq_enable();
339 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
340 return;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200341#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343gp_in_kernel:
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200344 if (fixup_exception(regs))
345 return;
346
347 tsk->thread.error_code = error_code;
348 tsk->thread.trap_no = 13;
349 if (notify_die(DIE_GPF, "general protection fault", regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 error_code, 13, SIGSEGV) == NOTIFY_STOP)
Alexander van Heukelum13485ab2008-07-02 01:32:04 +0200351 return;
352 die("general protection fault", regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200355static notrace __kprobes void
Ingo Molnarb5964402008-02-26 11:15:50 +0100356mem_parity_error(unsigned char reason, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Ingo Molnarb5964402008-02-26 11:15:50 +0100358 printk(KERN_EMERG
359 "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
360 reason, smp_processor_id());
361
362 printk(KERN_EMERG
363 "You have some hardware problem, likely on the PCI bus.\n");
Dave Jiangc0d12172007-07-19 01:49:46 -0700364
365#if defined(CONFIG_EDAC)
Ingo Molnarb5964402008-02-26 11:15:50 +0100366 if (edac_handler_set()) {
Dave Jiangc0d12172007-07-19 01:49:46 -0700367 edac_atomic_assert_error();
368 return;
369 }
370#endif
371
Don Zickus8da5adda2006-09-26 10:52:27 +0200372 if (panic_on_unrecovered_nmi)
Ingo Molnarb5964402008-02-26 11:15:50 +0100373 panic("NMI: Not continuing");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Don Zickusc41c5cd2006-09-26 10:52:27 +0200375 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 /* Clear and disable the memory parity error line. */
Alexander van Heukelum79704792008-10-03 22:00:36 +0200378 reason = (reason & 0xf) | 4;
379 outb(reason, 0x61);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200382static notrace __kprobes void
Ingo Molnarb5964402008-02-26 11:15:50 +0100383io_check_error(unsigned char reason, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 unsigned long i;
386
Dave Jones9c107802006-01-09 20:51:32 -0800387 printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 show_registers(regs);
389
390 /* Re-enable the IOCK line, wait for a few seconds */
391 reason = (reason & 0xf) | 8;
392 outb(reason, 0x61);
Ingo Molnarb5964402008-02-26 11:15:50 +0100393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 i = 2000;
Ingo Molnarb5964402008-02-26 11:15:50 +0100395 while (--i)
396 udelay(1000);
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 reason &= ~8;
399 outb(reason, 0x61);
400}
401
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200402static notrace __kprobes void
Ingo Molnarb5964402008-02-26 11:15:50 +0100403unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200405 if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) ==
406 NOTIFY_STOP)
Jason Wesseld3597522008-02-15 14:55:53 -0600407 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408#ifdef CONFIG_MCA
Ingo Molnarb5964402008-02-26 11:15:50 +0100409 /*
410 * Might actually be able to figure out what the guilty party
411 * is:
412 */
413 if (MCA_bus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 mca_handle_nmi();
415 return;
416 }
417#endif
Ingo Molnarb5964402008-02-26 11:15:50 +0100418 printk(KERN_EMERG
419 "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
420 reason, smp_processor_id());
421
Don Zickusc41c5cd2006-09-26 10:52:27 +0200422 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
Don Zickus8da5adda2006-09-26 10:52:27 +0200423 if (panic_on_unrecovered_nmi)
Ingo Molnarb5964402008-02-26 11:15:50 +0100424 panic("NMI: Not continuing");
Don Zickus8da5adda2006-09-26 10:52:27 +0200425
Don Zickusc41c5cd2006-09-26 10:52:27 +0200426 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200429static notrace __kprobes void default_do_nmi(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 unsigned char reason = 0;
Alexander van Heukelumabd34802008-07-02 18:39:01 +0200432 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Alexander van Heukelumabd34802008-07-02 18:39:01 +0200434 cpu = smp_processor_id();
435
436 /* Only the BSP gets external NMIs from the system. */
437 if (!cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 reason = get_nmi_reason();
Ingo Molnarb5964402008-02-26 11:15:50 +0100439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (!(reason & 0xc0)) {
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800441 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200442 == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return;
444#ifdef CONFIG_X86_LOCAL_APIC
445 /*
446 * Ok, so this is none of the documented NMI sources,
447 * so it must be the NMI watchdog.
448 */
Don Zickus3adbbcc2006-09-26 10:52:26 +0200449 if (nmi_watchdog_tick(regs, reason))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return;
Alexander van Heukelumabd34802008-07-02 18:39:01 +0200451 if (!do_nmi_callback(regs, cpu))
Don Zickus3adbbcc2006-09-26 10:52:26 +0200452 unknown_nmi_error(reason, regs);
Ingo Molnarb5964402008-02-26 11:15:50 +0100453#else
454 unknown_nmi_error(reason, regs);
455#endif
Don Zickus2fbe7b22006-09-26 10:52:27 +0200456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return;
458 }
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800459 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return;
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200461
462 /* AK: following checks seem to be broken on modern chipsets. FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (reason & 0x80)
464 mem_parity_error(reason, regs);
465 if (reason & 0x40)
466 io_check_error(reason, regs);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200467#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /*
469 * Reassert NMI in case it became active meanwhile
Ingo Molnarb5964402008-02-26 11:15:50 +0100470 * as it's edge-triggered:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 */
472 reassert_nmi();
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200473#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200476dotraplinkage notrace __kprobes void
477do_nmi(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 nmi_enter();
480
Hiroshi Shimamoto915b0d02008-12-08 19:19:26 -0800481 inc_irq_stat(__nmi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Andi Kleen8f4e9562007-07-22 11:12:32 +0200483 if (!ignore_nmis)
484 default_do_nmi(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 nmi_exit();
487}
488
Andi Kleen8f4e9562007-07-22 11:12:32 +0200489void stop_nmi(void)
490{
491 acpi_nmi_disable();
492 ignore_nmis++;
493}
494
495void restart_nmi(void)
496{
497 ignore_nmis--;
498 acpi_nmi_enable();
499}
500
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200501/* May run on IST stack. */
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200502dotraplinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Alexander van Heukelumb94da1e2008-09-09 21:55:58 +0200504#ifdef CONFIG_KPROBES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
506 == NOTIFY_STOP)
Stas Sergeev48c882112005-05-01 08:58:49 -0700507 return;
Alexander van Heukelumb94da1e2008-09-09 21:55:58 +0200508#else
509 if (notify_die(DIE_TRAP, "int3", regs, error_code, 3, SIGTRAP)
510 == NOTIFY_STOP)
511 return;
512#endif
Ingo Molnarb5964402008-02-26 11:15:50 +0100513
Alexander van Heukelum4915a352008-10-03 22:00:34 +0200514 preempt_conditional_sti(regs);
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200515 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
Alexander van Heukelum4915a352008-10-03 22:00:34 +0200516 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200519#ifdef CONFIG_X86_64
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100520/*
521 * Help handler running on IST stack to switch back to user stack
522 * for scheduling or signal handling. The actual stack switch is done in
523 * entry.S
524 */
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200525asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
526{
527 struct pt_regs *regs = eregs;
528 /* Did already sync */
529 if (eregs == (struct pt_regs *)eregs->sp)
530 ;
531 /* Exception from user space */
532 else if (user_mode(eregs))
533 regs = task_pt_regs(current);
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100534 /*
535 * Exception from kernel and interrupts are enabled. Move to
536 * kernel process stack.
537 */
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200538 else if (eregs->flags & X86_EFLAGS_IF)
539 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
540 if (eregs != regs)
541 *regs = *eregs;
542 return regs;
543}
544#endif
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546/*
547 * Our handling of the processor debug registers is non-trivial.
548 * We do not clear them on entry and exit from the kernel. Therefore
549 * it is possible to get a watchpoint trap here from inside the kernel.
550 * However, the code in ./ptrace.c has ensured that the user can
551 * only set watchpoints on userspace addresses. Therefore the in-kernel
552 * watchpoint trap can only occur in code which is reading/writing
553 * from user space. Such code must not hold kernel locks (since it
554 * can equally take a page fault), therefore it is safe to call
555 * force_sig_info even though that claims and releases locks.
Ingo Molnarb5964402008-02-26 11:15:50 +0100556 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 * Code in ./signal.c ensures that the debug control register
558 * is restored before we deliver any signal, and therefore that
559 * user code runs with the correct debug control register even though
560 * we clear it here.
561 *
562 * Being careful here means that we don't have to be as careful in a
563 * lot of more complicated places (task switching can be a bit lazy
564 * about restoring all the debug state, and ptrace doesn't have to
565 * find every occurrence of the TF bit that could be saved away even
566 * by user code)
Alexander van Heukelumc1d518c2008-10-03 23:17:11 +0200567 *
568 * May run on IST stack.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 */
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200570dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 struct task_struct *tsk = current;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200573 unsigned long condition;
Srinivasa Dsda654b72008-09-23 15:23:52 +0530574 int si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Vincent Hanquez1cc6f122005-06-23 00:08:43 -0700576 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Roland McGrath10faa812008-01-30 13:30:54 +0100578 /*
579 * The processor cleared BTF, so don't mark that we need it set.
580 */
581 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
582 tsk->thread.debugctlmsr = 0;
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200585 SIGTRAP) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /* It's safe to allow irq's after DR6 has been saved */
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200589 preempt_conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 /* Mask out spurious debug traps due to lazy DR7 setting */
592 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
Roland McGrath0f534092008-01-30 13:30:59 +0100593 if (!tsk->thread.debugreg7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 goto clear_dr7;
595 }
596
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200597#ifdef CONFIG_X86_32
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300598 if (regs->flags & X86_VM_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 goto debug_vm86;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200600#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /* Save debug status register where ptrace can see it */
Roland McGrath0f534092008-01-30 13:30:59 +0100603 tsk->thread.debugreg6 = condition;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /*
606 * Single-stepping through TF: make sure we ignore any events in
607 * kernel space (but re-enable TF when returning to user mode).
608 */
609 if (condition & DR_STEP) {
Vincent Hanquez717b5942005-06-23 00:08:45 -0700610 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 goto clear_TF_reenable;
612 }
613
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200614 si_code = get_si_code(condition);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /* Ok, finally something we can handle */
Srinivasa Dsda654b72008-09-23 15:23:52 +0530616 send_sigtrap(tsk, regs, error_code, si_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Ingo Molnarb5964402008-02-26 11:15:50 +0100618 /*
619 * Disable additional traps. They'll be re-enabled when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 * the signal is delivered.
621 */
622clear_dr7:
Vincent Hanquez1cc6f122005-06-23 00:08:43 -0700623 set_debugreg(0, 7);
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200624 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return;
626
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200627#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628debug_vm86:
629 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200630 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200632#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634clear_TF_reenable:
635 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
Ingo Molnar60930152008-03-30 11:45:23 +0200636 regs->flags &= ~X86_EFLAGS_TF;
Alexander van Heukelum3d2a71a2008-09-30 18:41:37 +0200637 preempt_conditional_cli(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return;
639}
640
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200641#ifdef CONFIG_X86_64
642static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
643{
644 if (fixup_exception(regs))
645 return 1;
646
647 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
648 /* Illegal floating point operation in the kernel */
649 current->thread.trap_no = trapnr;
650 die(str, regs, 0);
651 return 0;
652}
653#endif
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/*
656 * Note that we play around with the 'TS' bit in an attempt to get
657 * the correct behaviour even in the presence of the asynchronous
658 * IRQ13 behaviour
659 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100660void math_error(void __user *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Ingo Molnarb5964402008-02-26 11:15:50 +0100662 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 siginfo_t info;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800664 unsigned short cwd, swd, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 /*
667 * Save the info for the exception handler and clear the error.
668 */
669 task = current;
670 save_init_fpu(task);
671 task->thread.trap_no = 16;
672 task->thread.error_code = 0;
673 info.si_signo = SIGFPE;
674 info.si_errno = 0;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100675 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /*
677 * (~cwd & swd) will mask out exceptions that are not set to unmasked
678 * status. 0x3f is the exception bits in these regs, 0x200 is the
679 * C1 reg you need in case of a stack fault, 0x040 is the stack
680 * fault bit. We should only be taking one exception at a time,
681 * so if this combination doesn't produce any single exception,
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200682 * then we have a bad program that isn't synchronizing its FPU usage
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 * and it will suffer the consequences since we won't be able to
684 * fully reproduce the context of the exception
685 */
686 cwd = get_fpu_cwd(task);
687 swd = get_fpu_swd(task);
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800688
H. Peter Anvina73ad332008-12-25 10:39:01 -0800689 err = swd & ~cwd;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800690
691 if (err & 0x001) { /* Invalid op */
Ingo Molnarb5964402008-02-26 11:15:50 +0100692 /*
693 * swd & 0x240 == 0x040: Stack Underflow
694 * swd & 0x240 == 0x240: Stack Overflow
695 * User must clear the SF bit (0x40) if set
696 */
697 info.si_code = FPE_FLTINV;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800698 } else if (err & 0x004) { /* Divide by Zero */
Ingo Molnarb5964402008-02-26 11:15:50 +0100699 info.si_code = FPE_FLTDIV;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800700 } else if (err & 0x008) { /* Overflow */
Ingo Molnarb5964402008-02-26 11:15:50 +0100701 info.si_code = FPE_FLTOVF;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800702 } else if (err & 0x012) { /* Denormal, Underflow */
703 info.si_code = FPE_FLTUND;
704 } else if (err & 0x020) { /* Precision */
Ingo Molnarb5964402008-02-26 11:15:50 +0100705 info.si_code = FPE_FLTRES;
H. Peter Anvinadf77ba2008-12-22 17:56:05 -0800706 } else {
Ingo Molnarbd8b96d2008-12-26 09:20:22 +0100707 /*
708 * If we're using IRQ 13, or supposedly even some trap 16
709 * implementations, it's possible we get a spurious trap...
710 */
H. Peter Anvina73ad332008-12-25 10:39:01 -0800711 return; /* Spurious trap, no error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713 force_sig_info(SIGFPE, &info, task);
714}
715
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200716dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Alexander van Heukelum252d28f2008-09-09 21:56:09 +0200718 conditional_sti(regs);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200719
720#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 ignore_fpu_irq = 1;
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200722#else
723 if (!user_mode(regs) &&
724 kernel_math_error(regs, "kernel x87 math error", 16))
725 return;
726#endif
727
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100728 math_error((void __user *)regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100731static void simd_math_error(void __user *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Ingo Molnarb5964402008-02-26 11:15:50 +0100733 struct task_struct *task;
Ingo Molnarb5964402008-02-26 11:15:50 +0100734 siginfo_t info;
Alexander van Heukelum7b4fd4b2008-07-02 01:33:14 +0200735 unsigned short mxcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 /*
738 * Save the info for the exception handler and clear the error.
739 */
740 task = current;
741 save_init_fpu(task);
742 task->thread.trap_no = 19;
743 task->thread.error_code = 0;
744 info.si_signo = SIGFPE;
745 info.si_errno = 0;
746 info.si_code = __SI_FAULT;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100747 info.si_addr = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /*
749 * The SIMD FPU exceptions are handled a little differently, as there
750 * is only a single status/control register. Thus, to determine which
751 * unmasked exception was caught we must mask the exception mask bits
752 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
753 */
754 mxcsr = get_fpu_mxcsr(task);
755 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
Ingo Molnarb5964402008-02-26 11:15:50 +0100756 case 0x000:
757 default:
758 break;
759 case 0x001: /* Invalid Op */
760 info.si_code = FPE_FLTINV;
761 break;
762 case 0x002: /* Denormalize */
763 case 0x010: /* Underflow */
764 info.si_code = FPE_FLTUND;
765 break;
766 case 0x004: /* Zero Divide */
767 info.si_code = FPE_FLTDIV;
768 break;
769 case 0x008: /* Overflow */
770 info.si_code = FPE_FLTOVF;
771 break;
772 case 0x020: /* Precision */
773 info.si_code = FPE_FLTRES;
774 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776 force_sig_info(SIGFPE, &info, task);
777}
778
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200779dotraplinkage void
780do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Alexander van Heukelumb939bde2008-09-09 21:56:12 +0200782 conditional_sti(regs);
783
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200784#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (cpu_has_xmm) {
786 /* Handle SIMD FPU exceptions on PIII+ processors. */
787 ignore_fpu_irq = 1;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100788 simd_math_error((void __user *)regs->ip);
Ingo Molnarb5964402008-02-26 11:15:50 +0100789 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
Ingo Molnarb5964402008-02-26 11:15:50 +0100791 /*
792 * Handle strange cache flush from user space exception
793 * in all other cases. This is undocumented behaviour.
794 */
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300795 if (regs->flags & X86_VM_MASK) {
Ingo Molnarb5964402008-02-26 11:15:50 +0100796 handle_vm86_fault((struct kernel_vm86_regs *)regs, error_code);
797 return;
798 }
799 current->thread.trap_no = 19;
800 current->thread.error_code = error_code;
801 die_if_kernel("cache flush denied", regs, error_code);
802 force_sig(SIGSEGV, current);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200803#else
804 if (!user_mode(regs) &&
805 kernel_math_error(regs, "kernel simd math error", 19))
806 return;
807 simd_math_error((void __user *)regs->ip);
808#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200811dotraplinkage void
812do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Alexander van Heukelumcf819782008-09-09 21:56:08 +0200814 conditional_sti(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815#if 0
816 /* No need to warn about this any longer. */
Ingo Molnarb5964402008-02-26 11:15:50 +0100817 printk(KERN_INFO "Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818#endif
819}
820
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200821#ifdef CONFIG_X86_32
Ingo Molnarb5964402008-02-26 11:15:50 +0100822unsigned long patch_espfix_desc(unsigned long uesp, unsigned long kesp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Glauber Costa736f12b2008-05-27 20:14:51 -0700824 struct desc_struct *gdt = get_cpu_gdt_table(smp_processor_id());
Stas Sergeevbe44d2a2006-12-07 02:14:01 +0100825 unsigned long base = (kesp - uesp) & -THREAD_SIZE;
826 unsigned long new_kesp = kesp - base;
827 unsigned long lim_pages = (new_kesp | (THREAD_SIZE - 1)) >> PAGE_SHIFT;
828 __u64 desc = *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS];
Ingo Molnarb5964402008-02-26 11:15:50 +0100829
Stas Sergeevbe44d2a2006-12-07 02:14:01 +0100830 /* Set up base for espfix segment */
Ingo Molnarb5964402008-02-26 11:15:50 +0100831 desc &= 0x00f0ff0000000000ULL;
832 desc |= ((((__u64)base) << 16) & 0x000000ffffff0000ULL) |
Stas Sergeevbe44d2a2006-12-07 02:14:01 +0100833 ((((__u64)base) << 32) & 0xff00000000000000ULL) |
834 ((((__u64)lim_pages) << 32) & 0x000f000000000000ULL) |
835 (lim_pages & 0xffff);
836 *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS] = desc;
Ingo Molnarb5964402008-02-26 11:15:50 +0100837
Stas Sergeevbe44d2a2006-12-07 02:14:01 +0100838 return new_kesp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200840#else
841asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
842{
843}
844
845asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
846{
847}
848#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850/*
Ingo Molnarb5964402008-02-26 11:15:50 +0100851 * 'math_state_restore()' saves the current math information in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 * old math state array, and gets the new ones from the current task
853 *
854 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
855 * Don't touch unless you *really* know how it works.
856 *
857 * Must be called with kernel preemption disabled (in this case,
858 * local interrupts are disabled at the call-site in entry.S).
859 */
Chuck Ebbertacc20762006-12-07 02:14:01 +0100860asmlinkage void math_state_restore(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 struct thread_info *thread = current_thread_info();
863 struct task_struct *tsk = thread->task;
864
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700865 if (!tsk_used_math(tsk)) {
866 local_irq_enable();
867 /*
868 * does a slab alloc which can sleep
869 */
870 if (init_fpu(tsk)) {
871 /*
872 * ran out of memory!
873 */
874 do_group_exit(SIGKILL);
875 return;
876 }
877 local_irq_disable();
878 }
879
Ingo Molnarb5964402008-02-26 11:15:50 +0100880 clts(); /* Allow maths ops (or we recurse) */
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200881#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 restore_fpu(tsk);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200883#else
884 /*
885 * Paranoid restore. send a SIGSEGV if we fail to restore the state.
886 */
887 if (unlikely(restore_fpu_checking(tsk))) {
888 stts();
889 force_sig(SIGSEGV, tsk);
890 return;
891 }
892#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 thread->status |= TS_USEDFPU; /* So we fnsave on switch_to() */
Chuck Ebbertacc20762006-12-07 02:14:01 +0100894 tsk->fpu_counter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
Rusty Russell5992b6d2007-07-19 01:49:21 -0700896EXPORT_SYMBOL_GPL(math_state_restore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898#ifndef CONFIG_MATH_EMULATION
Tejun Heod3157602009-02-09 22:17:39 +0900899void math_emulate(struct math_emu_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Ingo Molnarb5964402008-02-26 11:15:50 +0100901 printk(KERN_EMERG
902 "math-emulation not enabled and no coprocessor found.\n");
903 printk(KERN_EMERG "killing %s.\n", current->comm);
904 force_sig(SIGFPE, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 schedule();
906}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907#endif /* CONFIG_MATH_EMULATION */
908
Tejun Heod3157602009-02-09 22:17:39 +0900909dotraplinkage void __kprobes do_device_not_available(struct pt_regs regs)
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200910{
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200911#ifdef CONFIG_X86_32
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200912 if (read_cr0() & X86_CR0_EM) {
Tejun Heod3157602009-02-09 22:17:39 +0900913 struct math_emu_info info = { };
914
915 conditional_sti(&regs);
916
917 info.regs = &regs;
918 math_emulate(&info);
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200919 } else {
920 math_state_restore(); /* interrupts still off */
Tejun Heod3157602009-02-09 22:17:39 +0900921 conditional_sti(&regs);
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200922 }
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200923#else
924 math_state_restore();
925#endif
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200926}
927
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200928#ifdef CONFIG_X86_32
Alexander van Heukelume407d6202008-09-30 18:41:36 +0200929dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200930{
931 siginfo_t info;
932 local_irq_enable();
933
934 info.si_signo = SIGILL;
935 info.si_errno = 0;
936 info.si_code = ILL_BADSTK;
937 info.si_addr = 0;
938 if (notify_die(DIE_TRAP, "iret exception",
939 regs, error_code, 32, SIGILL) == NOTIFY_STOP)
940 return;
Alexander van Heukelum3c1326f2008-09-26 14:03:08 +0200941 do_trap(32, SIGILL, "iret exception", regs, error_code, &info);
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200942}
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200943#endif
Alexander van Heukelumf8e08702008-09-09 21:56:13 +0200944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945void __init trap_init(void)
946{
Rusty Russelldbeb2be2007-10-19 20:35:03 +0200947 int i;
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949#ifdef CONFIG_EISA
Ingo Molnar927222b2008-01-30 13:33:49 +0100950 void __iomem *p = early_ioremap(0x0FFFD9, 4);
Ingo Molnarb5964402008-02-26 11:15:50 +0100951
952 if (readl(p) == 'E' + ('I'<<8) + ('S'<<16) + ('A'<<24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 EISA_bus = 1;
Ingo Molnar927222b2008-01-30 13:33:49 +0100954 early_iounmap(p, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955#endif
956
Alexander van Heukelum976382d2008-09-09 21:55:57 +0200957 set_intr_gate(0, &divide_error);
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200958 set_intr_gate_ist(1, &debug, DEBUG_STACK);
959 set_intr_gate_ist(2, &nmi, NMI_STACK);
960 /* int3 can be called from all */
961 set_system_intr_gate_ist(3, &int3, DEBUG_STACK);
962 /* int4 can be called from all */
963 set_system_intr_gate(4, &overflow);
Alexander van Heukelum64f644c2008-09-09 21:56:00 +0200964 set_intr_gate(5, &bounds);
Alexander van Heukelum12394cf2008-09-09 21:56:01 +0200965 set_intr_gate(6, &invalid_op);
Alexander van Heukelum7643e9b2008-09-09 21:56:02 +0200966 set_intr_gate(7, &device_not_available);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200967#ifdef CONFIG_X86_32
Alexander van Heukeluma8c1be92008-07-02 01:29:44 +0200968 set_task_gate(8, GDT_ENTRY_DOUBLEFAULT_TSS);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200969#else
970 set_intr_gate_ist(8, &double_fault, DOUBLEFAULT_STACK);
971#endif
Alexander van Heukelum51bc1ed2008-09-09 21:56:03 +0200972 set_intr_gate(9, &coprocessor_segment_overrun);
Alexander van Heukelum6bf77bf2008-09-09 21:56:04 +0200973 set_intr_gate(10, &invalid_TSS);
Alexander van Heukelum36d936c2008-09-09 21:56:05 +0200974 set_intr_gate(11, &segment_not_present);
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200975 set_intr_gate_ist(12, &stack_segment, STACKFAULT_STACK);
Alexander van Heukelumc6df0d72008-09-09 21:56:07 +0200976 set_intr_gate(13, &general_protection);
Ingo Molnarb5964402008-02-26 11:15:50 +0100977 set_intr_gate(14, &page_fault);
Alexander van Heukelumcf819782008-09-09 21:56:08 +0200978 set_intr_gate(15, &spurious_interrupt_bug);
Alexander van Heukelum252d28f2008-09-09 21:56:09 +0200979 set_intr_gate(16, &coprocessor_error);
Alexander van Heukelum5feedfd2008-09-09 21:56:10 +0200980 set_intr_gate(17, &alignment_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981#ifdef CONFIG_X86_MCE
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200982 set_intr_gate_ist(18, &machine_check, MCE_STACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983#endif
Alexander van Heukelumb939bde2008-09-09 21:56:12 +0200984 set_intr_gate(19, &simd_coprocessor_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Alexander van Heukelum081f75b2008-10-03 22:00:39 +0200986#ifdef CONFIG_IA32_EMULATION
987 set_system_intr_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
988#endif
989
990#ifdef CONFIG_X86_32
Jan Beulichd43c6e82006-01-06 00:11:49 -0800991 if (cpu_has_fxsr) {
Jan Beulichd43c6e82006-01-06 00:11:49 -0800992 printk(KERN_INFO "Enabling fast FPU save and restore... ");
993 set_in_cr4(X86_CR4_OSFXSR);
994 printk("done.\n");
995 }
996 if (cpu_has_xmm) {
Ingo Molnarb5964402008-02-26 11:15:50 +0100997 printk(KERN_INFO
998 "Enabling unmasked SIMD FPU exception support... ");
Jan Beulichd43c6e82006-01-06 00:11:49 -0800999 set_in_cr4(X86_CR4_OSXMMEXCPT);
1000 printk("done.\n");
1001 }
1002
Alexander van Heukelum699d2932008-10-03 22:00:32 +02001003 set_system_trap_gate(SYSCALL_VECTOR, &system_call);
Yinghai Lub77b8812008-12-19 15:23:44 -08001004#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Ingo Molnarb5964402008-02-26 11:15:50 +01001006 /* Reserve all the builtin and the syscall vector: */
Rusty Russelldbeb2be2007-10-19 20:35:03 +02001007 for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
1008 set_bit(i, used_vectors);
Ingo Molnarb5964402008-02-26 11:15:50 +01001009
Yinghai Lub77b8812008-12-19 15:23:44 -08001010#ifdef CONFIG_X86_64
1011 set_bit(IA32_SYSCALL_VECTOR, used_vectors);
1012#else
Rusty Russelldbeb2be2007-10-19 20:35:03 +02001013 set_bit(SYSCALL_VECTOR, used_vectors);
Alexander van Heukelum081f75b2008-10-03 22:00:39 +02001014#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 /*
Ingo Molnarb5964402008-02-26 11:15:50 +01001016 * Should be a barrier for any external CPU state:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 */
1018 cpu_init();
1019
Alexander van Heukelum081f75b2008-10-03 22:00:39 +02001020#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 trap_init_hook();
Alexander van Heukelum081f75b2008-10-03 22:00:39 +02001022#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}