blob: df9d210626acddd935d149e7915e73b75762f988 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/i386/traps.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * Pentium III FXSR, SSE support
7 * Gareth Hughes <gareth@valinux.com>, May 2000
8 */
9
10/*
11 * 'Traps.c' handles hardware traps and faults after we have saved some
12 * state in 'asm.s'.
13 */
14#include <linux/config.h>
15#include <linux/sched.h>
16#include <linux/kernel.h>
17#include <linux/string.h>
18#include <linux/errno.h>
19#include <linux/timer.h>
20#include <linux/mm.h>
21#include <linux/init.h>
22#include <linux/delay.h>
23#include <linux/spinlock.h>
24#include <linux/interrupt.h>
25#include <linux/highmem.h>
26#include <linux/kallsyms.h>
27#include <linux/ptrace.h>
28#include <linux/utsname.h>
29#include <linux/kprobes.h>
Alexander Nyberg6e274d12005-06-25 14:58:26 -070030#include <linux/kexec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#ifdef CONFIG_EISA
33#include <linux/ioport.h>
34#include <linux/eisa.h>
35#endif
36
37#ifdef CONFIG_MCA
38#include <linux/mca.h>
39#endif
40
41#include <asm/processor.h>
42#include <asm/system.h>
43#include <asm/uaccess.h>
44#include <asm/io.h>
45#include <asm/atomic.h>
46#include <asm/debugreg.h>
47#include <asm/desc.h>
48#include <asm/i387.h>
49#include <asm/nmi.h>
50
51#include <asm/smp.h>
52#include <asm/arch_hooks.h>
53#include <asm/kdebug.h>
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/module.h>
56
57#include "mach_traps.h"
58
59asmlinkage int system_call(void);
60
61struct desc_struct default_ldt[] = { { 0, 0 }, { 0, 0 }, { 0, 0 },
62 { 0, 0 }, { 0, 0 } };
63
64/* Do we ignore FPU interrupts ? */
65char ignore_fpu_irq = 0;
66
67/*
68 * The IDT has to be page-aligned to simplify the Pentium
69 * F0 0F bug workaround.. We have a special link segment
70 * for this.
71 */
72struct desc_struct idt_table[256] __attribute__((__section__(".data.idt"))) = { {0, 0}, };
73
74asmlinkage void divide_error(void);
75asmlinkage void debug(void);
76asmlinkage void nmi(void);
77asmlinkage void int3(void);
78asmlinkage void overflow(void);
79asmlinkage void bounds(void);
80asmlinkage void invalid_op(void);
81asmlinkage void device_not_available(void);
82asmlinkage void coprocessor_segment_overrun(void);
83asmlinkage void invalid_TSS(void);
84asmlinkage void segment_not_present(void);
85asmlinkage void stack_segment(void);
86asmlinkage void general_protection(void);
87asmlinkage void page_fault(void);
88asmlinkage void coprocessor_error(void);
89asmlinkage void simd_coprocessor_error(void);
90asmlinkage void alignment_check(void);
91asmlinkage void spurious_interrupt_bug(void);
92asmlinkage void machine_check(void);
93
94static int kstack_depth_to_print = 24;
Alan Sterne041c682006-03-27 01:16:30 -080095ATOMIC_NOTIFIER_HEAD(i386die_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97int register_die_notifier(struct notifier_block *nb)
98{
Jan Beulich101f12a2006-03-23 02:59:45 -080099 vmalloc_sync_all();
Alan Sterne041c682006-03-27 01:16:30 -0800100 return atomic_notifier_chain_register(&i386die_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700102EXPORT_SYMBOL(register_die_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Alan Sterne041c682006-03-27 01:16:30 -0800104int unregister_die_notifier(struct notifier_block *nb)
105{
106 return atomic_notifier_chain_unregister(&i386die_chain, nb);
107}
108EXPORT_SYMBOL(unregister_die_notifier);
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static inline int valid_stack_ptr(struct thread_info *tinfo, void *p)
111{
112 return p > (void *)tinfo &&
113 p < (void *)tinfo + THREAD_SIZE - 3;
114}
115
Chuck Ebbert4d7d8c82006-03-23 02:59:30 -0800116/*
117 * Print CONFIG_STACK_BACKTRACE_COLS address/symbol entries per line.
118 */
119static inline int print_addr_and_symbol(unsigned long addr, char *log_lvl,
120 int printed)
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800121{
Chuck Ebbert4d7d8c82006-03-23 02:59:30 -0800122 if (!printed)
123 printk(log_lvl);
124
125#if CONFIG_STACK_BACKTRACE_COLS == 1
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800126 printk(" [<%08lx>] ", addr);
Chuck Ebbert4d7d8c82006-03-23 02:59:30 -0800127#else
128 printk(" <%08lx> ", addr);
129#endif
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800130 print_symbol("%s", addr);
Chuck Ebbert4d7d8c82006-03-23 02:59:30 -0800131
132 printed = (printed + 1) % CONFIG_STACK_BACKTRACE_COLS;
Chuck Ebbert4d7d8c82006-03-23 02:59:30 -0800133 if (printed)
Chuck Ebbertc44b20d2006-05-20 14:59:52 -0700134 printk(" ");
Chuck Ebbert4d7d8c82006-03-23 02:59:30 -0800135 else
136 printk("\n");
137
138 return printed;
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800139}
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static inline unsigned long print_context_stack(struct thread_info *tinfo,
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800142 unsigned long *stack, unsigned long ebp,
143 char *log_lvl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 unsigned long addr;
Chuck Ebbert4d7d8c82006-03-23 02:59:30 -0800146 int printed = 0; /* nr of entries already printed on current line */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148#ifdef CONFIG_FRAME_POINTER
149 while (valid_stack_ptr(tinfo, (void *)ebp)) {
150 addr = *(unsigned long *)(ebp + 4);
Chuck Ebbert4d7d8c82006-03-23 02:59:30 -0800151 printed = print_addr_and_symbol(addr, log_lvl, printed);
Ingo Molnarb88d4f12006-06-23 02:04:20 -0700152 /*
153 * break out of recursive entries (such as
154 * end_of_stack_stop_unwind_function):
155 */
156 if (ebp == *(unsigned long *)ebp)
157 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 ebp = *(unsigned long *)ebp;
159 }
160#else
161 while (valid_stack_ptr(tinfo, stack)) {
162 addr = *stack++;
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800163 if (__kernel_text_address(addr))
Chuck Ebbert4d7d8c82006-03-23 02:59:30 -0800164 printed = print_addr_and_symbol(addr, log_lvl, printed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166#endif
Chuck Ebbert4d7d8c82006-03-23 02:59:30 -0800167 if (printed)
168 printk("\n");
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return ebp;
171}
172
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800173static void show_trace_log_lvl(struct task_struct *task,
174 unsigned long *stack, char *log_lvl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 unsigned long ebp;
177
178 if (!task)
179 task = current;
180
181 if (task == current) {
182 /* Grab ebp right from our regs */
183 asm ("movl %%ebp, %0" : "=r" (ebp) : );
184 } else {
185 /* ebp is the last reg pushed by switch_to */
186 ebp = *(unsigned long *) task->thread.esp;
187 }
188
189 while (1) {
190 struct thread_info *context;
191 context = (struct thread_info *)
192 ((unsigned long)stack & (~(THREAD_SIZE - 1)));
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800193 ebp = print_context_stack(context, stack, ebp, log_lvl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 stack = (unsigned long*)context->previous_esp;
195 if (!stack)
196 break;
Jean Delvarecc04ee92006-03-23 02:59:38 -0800197 printk("%s =======================\n", log_lvl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199}
200
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800201void show_trace(struct task_struct *task, unsigned long * stack)
202{
203 show_trace_log_lvl(task, stack, "");
204}
205
206static void show_stack_log_lvl(struct task_struct *task, unsigned long *esp,
207 char *log_lvl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 unsigned long *stack;
210 int i;
211
212 if (esp == NULL) {
213 if (task)
214 esp = (unsigned long*)task->thread.esp;
215 else
216 esp = (unsigned long *)&esp;
217 }
218
219 stack = esp;
220 for(i = 0; i < kstack_depth_to_print; i++) {
221 if (kstack_end(stack))
222 break;
Chuck Ebbert75874d52006-03-23 02:59:52 -0800223 if (i && ((i % 8) == 0))
224 printk("\n%s ", log_lvl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 printk("%08lx ", *stack++);
226 }
Chuck Ebbert75874d52006-03-23 02:59:52 -0800227 printk("\n%sCall Trace:\n", log_lvl);
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800228 show_trace_log_lvl(task, esp, log_lvl);
229}
230
231void show_stack(struct task_struct *task, unsigned long *esp)
232{
Chuck Ebbert75874d52006-03-23 02:59:52 -0800233 printk(" ");
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800234 show_stack_log_lvl(task, esp, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237/*
238 * The architecture-independent dump_stack generator
239 */
240void dump_stack(void)
241{
242 unsigned long stack;
243
244 show_trace(current, &stack);
245}
246
247EXPORT_SYMBOL(dump_stack);
248
249void show_registers(struct pt_regs *regs)
250{
251 int i;
252 int in_kernel = 1;
253 unsigned long esp;
254 unsigned short ss;
255
256 esp = (unsigned long) (&regs->esp);
Zachary Amsden0998e422005-09-03 15:56:43 -0700257 savesegment(ss, ss);
Jan Beulichdb753bd2006-03-23 02:59:46 -0800258 if (user_mode_vm(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 in_kernel = 0;
260 esp = regs->esp;
261 ss = regs->xss & 0xffff;
262 }
263 print_modules();
Dave Jones9c107802006-01-09 20:51:32 -0800264 printk(KERN_EMERG "CPU: %d\nEIP: %04x:[<%08lx>] %s VLI\n"
Chuck Ebbertb53e8f62006-02-04 23:28:04 -0800265 "EFLAGS: %08lx (%s %.*s) \n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 smp_processor_id(), 0xffff & regs->xcs, regs->eip,
Chuck Ebbertb53e8f62006-02-04 23:28:04 -0800267 print_tainted(), regs->eflags, system_utsname.release,
268 (int)strcspn(system_utsname.version, " "),
269 system_utsname.version);
Dave Jones9c107802006-01-09 20:51:32 -0800270 print_symbol(KERN_EMERG "EIP is at %s\n", regs->eip);
271 printk(KERN_EMERG "eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 regs->eax, regs->ebx, regs->ecx, regs->edx);
Dave Jones9c107802006-01-09 20:51:32 -0800273 printk(KERN_EMERG "esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 regs->esi, regs->edi, regs->ebp, esp);
Dave Jones9c107802006-01-09 20:51:32 -0800275 printk(KERN_EMERG "ds: %04x es: %04x ss: %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 regs->xds & 0xffff, regs->xes & 0xffff, ss);
Dave Jones9c107802006-01-09 20:51:32 -0800277 printk(KERN_EMERG "Process %s (pid: %d, threadinfo=%p task=%p)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 current->comm, current->pid, current_thread_info(), current);
279 /*
280 * When in-kernel, we also print out the stack and code at the
281 * time of the fault..
282 */
283 if (in_kernel) {
Domen Puncer3f3ae342005-06-25 14:58:44 -0700284 u8 __user *eip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Dave Jones9c107802006-01-09 20:51:32 -0800286 printk("\n" KERN_EMERG "Stack: ");
Chuck Ebbert7aa89742006-01-14 13:20:52 -0800287 show_stack_log_lvl(NULL, (unsigned long *)esp, KERN_EMERG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Dave Jones9c107802006-01-09 20:51:32 -0800289 printk(KERN_EMERG "Code: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Domen Puncer3f3ae342005-06-25 14:58:44 -0700291 eip = (u8 __user *)regs->eip - 43;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 for (i = 0; i < 64; i++, eip++) {
293 unsigned char c;
294
Domen Puncer3f3ae342005-06-25 14:58:44 -0700295 if (eip < (u8 __user *)PAGE_OFFSET || __get_user(c, eip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 printk(" Bad EIP value.");
297 break;
298 }
Domen Puncer3f3ae342005-06-25 14:58:44 -0700299 if (eip == (u8 __user *)regs->eip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 printk("<%02x> ", c);
301 else
302 printk("%02x ", c);
303 }
304 }
305 printk("\n");
306}
307
308static void handle_BUG(struct pt_regs *regs)
309{
310 unsigned short ud2;
311 unsigned short line;
312 char *file;
313 char c;
314 unsigned long eip;
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 eip = regs->eip;
317
318 if (eip < PAGE_OFFSET)
319 goto no_bug;
Domen Puncer3f3ae342005-06-25 14:58:44 -0700320 if (__get_user(ud2, (unsigned short __user *)eip))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 goto no_bug;
322 if (ud2 != 0x0b0f)
323 goto no_bug;
Domen Puncer3f3ae342005-06-25 14:58:44 -0700324 if (__get_user(line, (unsigned short __user *)(eip + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 goto bug;
Domen Puncer3f3ae342005-06-25 14:58:44 -0700326 if (__get_user(file, (char * __user *)(eip + 4)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 (unsigned long)file < PAGE_OFFSET || __get_user(c, file))
328 file = "<bad filename>";
329
Dave Jones9c107802006-01-09 20:51:32 -0800330 printk(KERN_EMERG "------------[ cut here ]------------\n");
331 printk(KERN_EMERG "kernel BUG at %s:%d!\n", file, line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333no_bug:
334 return;
335
336 /* Here we know it was a BUG but file-n-line is unavailable */
337bug:
Dave Jones9c107802006-01-09 20:51:32 -0800338 printk(KERN_EMERG "Kernel BUG\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700341/* This is gone through when something in the kernel
342 * has done something bad and is about to be terminated.
343*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344void die(const char * str, struct pt_regs * regs, long err)
345{
346 static struct {
347 spinlock_t lock;
348 u32 lock_owner;
349 int lock_owner_depth;
350 } die = {
351 .lock = SPIN_LOCK_UNLOCKED,
352 .lock_owner = -1,
353 .lock_owner_depth = 0
354 };
355 static int die_counter;
Jan Beuliche43d6742006-01-06 00:11:48 -0800356 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Andrew Mortondd287792006-03-23 03:00:57 -0800358 oops_enter();
359
Ingo Molnar39c715b2005-06-21 17:14:34 -0700360 if (die.lock_owner != raw_smp_processor_id()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 console_verbose();
Jan Beuliche43d6742006-01-06 00:11:48 -0800362 spin_lock_irqsave(&die.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 die.lock_owner = smp_processor_id();
364 die.lock_owner_depth = 0;
365 bust_spinlocks(1);
366 }
Jan Beuliche43d6742006-01-06 00:11:48 -0800367 else
368 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 if (++die.lock_owner_depth < 3) {
371 int nl = 0;
Randy Dunlap7bee5c02006-04-10 22:53:11 -0700372 unsigned long esp;
373 unsigned short ss;
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 handle_BUG(regs);
Dave Jones9c107802006-01-09 20:51:32 -0800376 printk(KERN_EMERG "%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377#ifdef CONFIG_PREEMPT
Dave Jones9c107802006-01-09 20:51:32 -0800378 printk(KERN_EMERG "PREEMPT ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 nl = 1;
380#endif
381#ifdef CONFIG_SMP
Dave Jones9c107802006-01-09 20:51:32 -0800382 if (!nl)
383 printk(KERN_EMERG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 printk("SMP ");
385 nl = 1;
386#endif
387#ifdef CONFIG_DEBUG_PAGEALLOC
Dave Jones9c107802006-01-09 20:51:32 -0800388 if (!nl)
389 printk(KERN_EMERG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 printk("DEBUG_PAGEALLOC");
391 nl = 1;
392#endif
393 if (nl)
394 printk("\n");
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800395 if (notify_die(DIE_OOPS, str, regs, err,
396 current->thread.trap_no, SIGSEGV) !=
Randy Dunlap7bee5c02006-04-10 22:53:11 -0700397 NOTIFY_STOP) {
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800398 show_registers(regs);
Randy Dunlap7bee5c02006-04-10 22:53:11 -0700399 /* Executive summary in case the oops scrolled away */
400 esp = (unsigned long) (&regs->esp);
401 savesegment(ss, ss);
402 if (user_mode(regs)) {
403 esp = regs->esp;
404 ss = regs->xss & 0xffff;
405 }
406 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->eip);
407 print_symbol("%s", regs->eip);
408 printk(" SS:ESP %04x:%08lx\n", ss, esp);
409 }
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800410 else
411 regs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 } else
Dave Jones9c107802006-01-09 20:51:32 -0800413 printk(KERN_EMERG "Recursive die() failure, output suppressed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 bust_spinlocks(0);
416 die.lock_owner = -1;
Jan Beuliche43d6742006-01-06 00:11:48 -0800417 spin_unlock_irqrestore(&die.lock, flags);
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700418
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800419 if (!regs)
420 return;
421
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700422 if (kexec_should_crash(current))
423 crash_kexec(regs);
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (in_interrupt())
426 panic("Fatal exception in interrupt");
427
428 if (panic_on_oops) {
429 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
430 ssleep(5);
431 panic("Fatal exception");
432 }
Andrew Mortondd287792006-03-23 03:00:57 -0800433 oops_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 do_exit(SIGSEGV);
435}
436
437static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err)
438{
Vincent Hanquez717b5942005-06-23 00:08:45 -0700439 if (!user_mode_vm(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 die(str, regs, err);
441}
442
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700443static void __kprobes do_trap(int trapnr, int signr, char *str, int vm86,
444 struct pt_regs * regs, long error_code,
445 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700447 struct task_struct *tsk = current;
448 tsk->thread.error_code = error_code;
449 tsk->thread.trap_no = trapnr;
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (regs->eflags & VM_MASK) {
452 if (vm86)
453 goto vm86_trap;
454 goto trap_signal;
455 }
456
Vincent Hanquez717b5942005-06-23 00:08:45 -0700457 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 goto kernel_trap;
459
460 trap_signal: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (info)
462 force_sig_info(signr, info, tsk);
463 else
464 force_sig(signr, tsk);
465 return;
466 }
467
468 kernel_trap: {
469 if (!fixup_exception(regs))
470 die(str, regs, error_code);
471 return;
472 }
473
474 vm86_trap: {
475 int ret = handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, trapnr);
476 if (ret) goto trap_signal;
477 return;
478 }
479}
480
481#define DO_ERROR(trapnr, signr, str, name) \
482fastcall void do_##name(struct pt_regs * regs, long error_code) \
483{ \
484 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
485 == NOTIFY_STOP) \
486 return; \
487 do_trap(trapnr, signr, str, 0, regs, error_code, NULL); \
488}
489
490#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
491fastcall void do_##name(struct pt_regs * regs, long error_code) \
492{ \
493 siginfo_t info; \
494 info.si_signo = signr; \
495 info.si_errno = 0; \
496 info.si_code = sicode; \
497 info.si_addr = (void __user *)siaddr; \
498 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
499 == NOTIFY_STOP) \
500 return; \
501 do_trap(trapnr, signr, str, 0, regs, error_code, &info); \
502}
503
504#define DO_VM86_ERROR(trapnr, signr, str, name) \
505fastcall void do_##name(struct pt_regs * regs, long error_code) \
506{ \
507 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
508 == NOTIFY_STOP) \
509 return; \
510 do_trap(trapnr, signr, str, 1, regs, error_code, NULL); \
511}
512
513#define DO_VM86_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
514fastcall void do_##name(struct pt_regs * regs, long error_code) \
515{ \
516 siginfo_t info; \
517 info.si_signo = signr; \
518 info.si_errno = 0; \
519 info.si_code = sicode; \
520 info.si_addr = (void __user *)siaddr; \
521 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
522 == NOTIFY_STOP) \
523 return; \
524 do_trap(trapnr, signr, str, 1, regs, error_code, &info); \
525}
526
527DO_VM86_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->eip)
528#ifndef CONFIG_KPROBES
529DO_VM86_ERROR( 3, SIGTRAP, "int3", int3)
530#endif
531DO_VM86_ERROR( 4, SIGSEGV, "overflow", overflow)
532DO_VM86_ERROR( 5, SIGSEGV, "bounds", bounds)
Chuck Ebbert631b0342006-01-03 22:36:14 -0500533DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->eip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
535DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
536DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
537DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
538DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
Linus Torvaldsa879cbb2005-04-29 09:38:44 -0700539DO_ERROR_INFO(32, SIGSEGV, "iret exception", iret_error, ILL_BADSTK, 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700541fastcall void __kprobes do_general_protection(struct pt_regs * regs,
542 long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 int cpu = get_cpu();
545 struct tss_struct *tss = &per_cpu(init_tss, cpu);
546 struct thread_struct *thread = &current->thread;
547
548 /*
549 * Perform the lazy TSS's I/O bitmap copy. If the TSS has an
550 * invalid offset set (the LAZY one) and the faulting thread has
551 * a valid I/O bitmap pointer, we copy the I/O bitmap in the TSS
552 * and we set the offset field correctly. Then we let the CPU to
553 * restart the faulting instruction.
554 */
555 if (tss->io_bitmap_base == INVALID_IO_BITMAP_OFFSET_LAZY &&
556 thread->io_bitmap_ptr) {
557 memcpy(tss->io_bitmap, thread->io_bitmap_ptr,
558 thread->io_bitmap_max);
559 /*
560 * If the previously set map was extending to higher ports
561 * than the current one, pad extra space with 0xff (no access).
562 */
563 if (thread->io_bitmap_max < tss->io_bitmap_max)
564 memset((char *) tss->io_bitmap +
565 thread->io_bitmap_max, 0xff,
566 tss->io_bitmap_max - thread->io_bitmap_max);
567 tss->io_bitmap_max = thread->io_bitmap_max;
568 tss->io_bitmap_base = IO_BITMAP_OFFSET;
Bart Oldemand5cd4aa2005-10-30 14:59:29 -0800569 tss->io_bitmap_owner = thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 put_cpu();
571 return;
572 }
573 put_cpu();
574
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700575 current->thread.error_code = error_code;
576 current->thread.trap_no = 13;
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (regs->eflags & VM_MASK)
579 goto gp_in_vm86;
580
Vincent Hanquez717b5942005-06-23 00:08:45 -0700581 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 goto gp_in_kernel;
583
584 current->thread.error_code = error_code;
585 current->thread.trap_no = 13;
586 force_sig(SIGSEGV, current);
587 return;
588
589gp_in_vm86:
590 local_irq_enable();
591 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
592 return;
593
594gp_in_kernel:
595 if (!fixup_exception(regs)) {
596 if (notify_die(DIE_GPF, "general protection fault", regs,
597 error_code, 13, SIGSEGV) == NOTIFY_STOP)
598 return;
599 die("general protection fault", regs, error_code);
600 }
601}
602
603static void mem_parity_error(unsigned char reason, struct pt_regs * regs)
604{
Dave Jones9c107802006-01-09 20:51:32 -0800605 printk(KERN_EMERG "Uhhuh. NMI received. Dazed and confused, but trying "
606 "to continue\n");
607 printk(KERN_EMERG "You probably have a hardware problem with your RAM "
608 "chips\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 /* Clear and disable the memory parity error line. */
611 clear_mem_error(reason);
612}
613
614static void io_check_error(unsigned char reason, struct pt_regs * regs)
615{
616 unsigned long i;
617
Dave Jones9c107802006-01-09 20:51:32 -0800618 printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 show_registers(regs);
620
621 /* Re-enable the IOCK line, wait for a few seconds */
622 reason = (reason & 0xf) | 8;
623 outb(reason, 0x61);
624 i = 2000;
625 while (--i) udelay(1000);
626 reason &= ~8;
627 outb(reason, 0x61);
628}
629
630static void unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
631{
632#ifdef CONFIG_MCA
633 /* Might actually be able to figure out what the guilty party
634 * is. */
635 if( MCA_bus ) {
636 mca_handle_nmi();
637 return;
638 }
639#endif
640 printk("Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
641 reason, smp_processor_id());
642 printk("Dazed and confused, but trying to continue\n");
643 printk("Do you have a strange power saving mode enabled?\n");
644}
645
646static DEFINE_SPINLOCK(nmi_print_lock);
647
648void die_nmi (struct pt_regs *regs, const char *msg)
649{
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800650 if (notify_die(DIE_NMIWATCHDOG, msg, regs, 0, 2, SIGINT) ==
George Anzinger748f2ed2005-09-03 15:56:48 -0700651 NOTIFY_STOP)
652 return;
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 spin_lock(&nmi_print_lock);
655 /*
656 * We are in trouble anyway, lets at least try
657 * to get a message out.
658 */
659 bust_spinlocks(1);
Dave Jones9c107802006-01-09 20:51:32 -0800660 printk(KERN_EMERG "%s", msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 printk(" on CPU%d, eip %08lx, registers:\n",
662 smp_processor_id(), regs->eip);
663 show_registers(regs);
Dave Jones9c107802006-01-09 20:51:32 -0800664 printk(KERN_EMERG "console shuts up ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 console_silent();
666 spin_unlock(&nmi_print_lock);
667 bust_spinlocks(0);
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700668
669 /* If we are in kernel we are probably nested up pretty bad
670 * and might aswell get out now while we still can.
671 */
Jan Beulichdb753bd2006-03-23 02:59:46 -0800672 if (!user_mode_vm(regs)) {
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700673 current->thread.trap_no = 2;
674 crash_kexec(regs);
675 }
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 do_exit(SIGSEGV);
678}
679
680static void default_do_nmi(struct pt_regs * regs)
681{
682 unsigned char reason = 0;
683
684 /* Only the BSP gets external NMIs from the system. */
685 if (!smp_processor_id())
686 reason = get_nmi_reason();
687
688 if (!(reason & 0xc0)) {
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800689 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 == NOTIFY_STOP)
691 return;
692#ifdef CONFIG_X86_LOCAL_APIC
693 /*
694 * Ok, so this is none of the documented NMI sources,
695 * so it must be the NMI watchdog.
696 */
697 if (nmi_watchdog) {
698 nmi_watchdog_tick(regs);
699 return;
700 }
701#endif
702 unknown_nmi_error(reason, regs);
703 return;
704 }
Jan Beulich20c0d2d2006-03-26 01:37:01 -0800705 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return;
707 if (reason & 0x80)
708 mem_parity_error(reason, regs);
709 if (reason & 0x40)
710 io_check_error(reason, regs);
711 /*
712 * Reassert NMI in case it became active meanwhile
713 * as it's edge-triggered.
714 */
715 reassert_nmi();
716}
717
718static int dummy_nmi_callback(struct pt_regs * regs, int cpu)
719{
720 return 0;
721}
722
723static nmi_callback_t nmi_callback = dummy_nmi_callback;
724
725fastcall void do_nmi(struct pt_regs * regs, long error_code)
726{
727 int cpu;
728
729 nmi_enter();
730
731 cpu = smp_processor_id();
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 ++nmi_count(cpu);
734
Paul E. McKenney19306052005-09-06 15:16:35 -0700735 if (!rcu_dereference(nmi_callback)(regs, cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 default_do_nmi(regs);
737
738 nmi_exit();
739}
740
741void set_nmi_callback(nmi_callback_t callback)
742{
Jan Beulich101f12a2006-03-23 02:59:45 -0800743 vmalloc_sync_all();
Paul E. McKenney19306052005-09-06 15:16:35 -0700744 rcu_assign_pointer(nmi_callback, callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700746EXPORT_SYMBOL_GPL(set_nmi_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748void unset_nmi_callback(void)
749{
750 nmi_callback = dummy_nmi_callback;
751}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700752EXPORT_SYMBOL_GPL(unset_nmi_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754#ifdef CONFIG_KPROBES
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700755fastcall void __kprobes do_int3(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
758 == NOTIFY_STOP)
Stas Sergeev48c882112005-05-01 08:58:49 -0700759 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /* This is an interrupt gate, because kprobes wants interrupts
761 disabled. Normal trap handlers don't. */
762 restore_interrupts(regs);
763 do_trap(3, SIGTRAP, "int3", 1, regs, error_code, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765#endif
766
767/*
768 * Our handling of the processor debug registers is non-trivial.
769 * We do not clear them on entry and exit from the kernel. Therefore
770 * it is possible to get a watchpoint trap here from inside the kernel.
771 * However, the code in ./ptrace.c has ensured that the user can
772 * only set watchpoints on userspace addresses. Therefore the in-kernel
773 * watchpoint trap can only occur in code which is reading/writing
774 * from user space. Such code must not hold kernel locks (since it
775 * can equally take a page fault), therefore it is safe to call
776 * force_sig_info even though that claims and releases locks.
777 *
778 * Code in ./signal.c ensures that the debug control register
779 * is restored before we deliver any signal, and therefore that
780 * user code runs with the correct debug control register even though
781 * we clear it here.
782 *
783 * Being careful here means that we don't have to be as careful in a
784 * lot of more complicated places (task switching can be a bit lazy
785 * about restoring all the debug state, and ptrace doesn't have to
786 * find every occurrence of the TF bit that could be saved away even
787 * by user code)
788 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700789fastcall void __kprobes do_debug(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 unsigned int condition;
792 struct task_struct *tsk = current;
793
Vincent Hanquez1cc6f122005-06-23 00:08:43 -0700794 get_debugreg(condition, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
797 SIGTRAP) == NOTIFY_STOP)
798 return;
799 /* It's safe to allow irq's after DR6 has been saved */
800 if (regs->eflags & X86_EFLAGS_IF)
801 local_irq_enable();
802
803 /* Mask out spurious debug traps due to lazy DR7 setting */
804 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
805 if (!tsk->thread.debugreg[7])
806 goto clear_dr7;
807 }
808
809 if (regs->eflags & VM_MASK)
810 goto debug_vm86;
811
812 /* Save debug status register where ptrace can see it */
813 tsk->thread.debugreg[6] = condition;
814
815 /*
816 * Single-stepping through TF: make sure we ignore any events in
817 * kernel space (but re-enable TF when returning to user mode).
818 */
819 if (condition & DR_STEP) {
820 /*
821 * We already checked v86 mode above, so we can
822 * check for kernel mode by just checking the CPL
823 * of CS.
824 */
Vincent Hanquez717b5942005-06-23 00:08:45 -0700825 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 goto clear_TF_reenable;
827 }
828
829 /* Ok, finally something we can handle */
830 send_sigtrap(tsk, regs, error_code);
831
832 /* Disable additional traps. They'll be re-enabled when
833 * the signal is delivered.
834 */
835clear_dr7:
Vincent Hanquez1cc6f122005-06-23 00:08:43 -0700836 set_debugreg(0, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return;
838
839debug_vm86:
840 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
841 return;
842
843clear_TF_reenable:
844 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
845 regs->eflags &= ~TF_MASK;
846 return;
847}
848
849/*
850 * Note that we play around with the 'TS' bit in an attempt to get
851 * the correct behaviour even in the presence of the asynchronous
852 * IRQ13 behaviour
853 */
854void math_error(void __user *eip)
855{
856 struct task_struct * task;
857 siginfo_t info;
858 unsigned short cwd, swd;
859
860 /*
861 * Save the info for the exception handler and clear the error.
862 */
863 task = current;
864 save_init_fpu(task);
865 task->thread.trap_no = 16;
866 task->thread.error_code = 0;
867 info.si_signo = SIGFPE;
868 info.si_errno = 0;
869 info.si_code = __SI_FAULT;
870 info.si_addr = eip;
871 /*
872 * (~cwd & swd) will mask out exceptions that are not set to unmasked
873 * status. 0x3f is the exception bits in these regs, 0x200 is the
874 * C1 reg you need in case of a stack fault, 0x040 is the stack
875 * fault bit. We should only be taking one exception at a time,
876 * so if this combination doesn't produce any single exception,
877 * then we have a bad program that isn't syncronizing its FPU usage
878 * and it will suffer the consequences since we won't be able to
879 * fully reproduce the context of the exception
880 */
881 cwd = get_fpu_cwd(task);
882 swd = get_fpu_swd(task);
Chuck Ebbertb1daec32005-08-23 21:36:40 -0400883 switch (swd & ~cwd & 0x3f) {
Chuck Ebbert33333372005-09-13 04:55:41 -0400884 case 0x000: /* No unmasked exception */
885 return;
886 default: /* Multiple exceptions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 break;
888 case 0x001: /* Invalid Op */
Chuck Ebbertb1daec32005-08-23 21:36:40 -0400889 /*
890 * swd & 0x240 == 0x040: Stack Underflow
891 * swd & 0x240 == 0x240: Stack Overflow
892 * User must clear the SF bit (0x40) if set
893 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 info.si_code = FPE_FLTINV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 break;
896 case 0x002: /* Denormalize */
897 case 0x010: /* Underflow */
898 info.si_code = FPE_FLTUND;
899 break;
900 case 0x004: /* Zero Divide */
901 info.si_code = FPE_FLTDIV;
902 break;
903 case 0x008: /* Overflow */
904 info.si_code = FPE_FLTOVF;
905 break;
906 case 0x020: /* Precision */
907 info.si_code = FPE_FLTRES;
908 break;
909 }
910 force_sig_info(SIGFPE, &info, task);
911}
912
913fastcall void do_coprocessor_error(struct pt_regs * regs, long error_code)
914{
915 ignore_fpu_irq = 1;
916 math_error((void __user *)regs->eip);
917}
918
919static void simd_math_error(void __user *eip)
920{
921 struct task_struct * task;
922 siginfo_t info;
923 unsigned short mxcsr;
924
925 /*
926 * Save the info for the exception handler and clear the error.
927 */
928 task = current;
929 save_init_fpu(task);
930 task->thread.trap_no = 19;
931 task->thread.error_code = 0;
932 info.si_signo = SIGFPE;
933 info.si_errno = 0;
934 info.si_code = __SI_FAULT;
935 info.si_addr = eip;
936 /*
937 * The SIMD FPU exceptions are handled a little differently, as there
938 * is only a single status/control register. Thus, to determine which
939 * unmasked exception was caught we must mask the exception mask bits
940 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
941 */
942 mxcsr = get_fpu_mxcsr(task);
943 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
944 case 0x000:
945 default:
946 break;
947 case 0x001: /* Invalid Op */
948 info.si_code = FPE_FLTINV;
949 break;
950 case 0x002: /* Denormalize */
951 case 0x010: /* Underflow */
952 info.si_code = FPE_FLTUND;
953 break;
954 case 0x004: /* Zero Divide */
955 info.si_code = FPE_FLTDIV;
956 break;
957 case 0x008: /* Overflow */
958 info.si_code = FPE_FLTOVF;
959 break;
960 case 0x020: /* Precision */
961 info.si_code = FPE_FLTRES;
962 break;
963 }
964 force_sig_info(SIGFPE, &info, task);
965}
966
967fastcall void do_simd_coprocessor_error(struct pt_regs * regs,
968 long error_code)
969{
970 if (cpu_has_xmm) {
971 /* Handle SIMD FPU exceptions on PIII+ processors. */
972 ignore_fpu_irq = 1;
973 simd_math_error((void __user *)regs->eip);
974 } else {
975 /*
976 * Handle strange cache flush from user space exception
977 * in all other cases. This is undocumented behaviour.
978 */
979 if (regs->eflags & VM_MASK) {
980 handle_vm86_fault((struct kernel_vm86_regs *)regs,
981 error_code);
982 return;
983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 current->thread.trap_no = 19;
985 current->thread.error_code = error_code;
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700986 die_if_kernel("cache flush denied", regs, error_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 force_sig(SIGSEGV, current);
988 }
989}
990
991fastcall void do_spurious_interrupt_bug(struct pt_regs * regs,
992 long error_code)
993{
994#if 0
995 /* No need to warn about this any longer. */
996 printk("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
997#endif
998}
999
1000fastcall void setup_x86_bogus_stack(unsigned char * stk)
1001{
1002 unsigned long *switch16_ptr, *switch32_ptr;
1003 struct pt_regs *regs;
1004 unsigned long stack_top, stack_bot;
1005 unsigned short iret_frame16_off;
1006 int cpu = smp_processor_id();
1007 /* reserve the space on 32bit stack for the magic switch16 pointer */
1008 memmove(stk, stk + 8, sizeof(struct pt_regs));
1009 switch16_ptr = (unsigned long *)(stk + sizeof(struct pt_regs));
1010 regs = (struct pt_regs *)stk;
1011 /* now the switch32 on 16bit stack */
1012 stack_bot = (unsigned long)&per_cpu(cpu_16bit_stack, cpu);
1013 stack_top = stack_bot + CPU_16BIT_STACK_SIZE;
1014 switch32_ptr = (unsigned long *)(stack_top - 8);
1015 iret_frame16_off = CPU_16BIT_STACK_SIZE - 8 - 20;
1016 /* copy iret frame on 16bit stack */
1017 memcpy((void *)(stack_bot + iret_frame16_off), &regs->eip, 20);
1018 /* fill in the switch pointers */
1019 switch16_ptr[0] = (regs->esp & 0xffff0000) | iret_frame16_off;
1020 switch16_ptr[1] = __ESPFIX_SS;
1021 switch32_ptr[0] = (unsigned long)stk + sizeof(struct pt_regs) +
1022 8 - CPU_16BIT_STACK_SIZE;
1023 switch32_ptr[1] = __KERNEL_DS;
1024}
1025
1026fastcall unsigned char * fixup_x86_bogus_stack(unsigned short sp)
1027{
1028 unsigned long *switch32_ptr;
1029 unsigned char *stack16, *stack32;
1030 unsigned long stack_top, stack_bot;
1031 int len;
1032 int cpu = smp_processor_id();
1033 stack_bot = (unsigned long)&per_cpu(cpu_16bit_stack, cpu);
1034 stack_top = stack_bot + CPU_16BIT_STACK_SIZE;
1035 switch32_ptr = (unsigned long *)(stack_top - 8);
1036 /* copy the data from 16bit stack to 32bit stack */
1037 len = CPU_16BIT_STACK_SIZE - 8 - sp;
1038 stack16 = (unsigned char *)(stack_bot + sp);
1039 stack32 = (unsigned char *)
1040 (switch32_ptr[0] + CPU_16BIT_STACK_SIZE - 8 - len);
1041 memcpy(stack32, stack16, len);
1042 return stack32;
1043}
1044
1045/*
1046 * 'math_state_restore()' saves the current math information in the
1047 * old math state array, and gets the new ones from the current task
1048 *
1049 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1050 * Don't touch unless you *really* know how it works.
1051 *
1052 * Must be called with kernel preemption disabled (in this case,
1053 * local interrupts are disabled at the call-site in entry.S).
1054 */
1055asmlinkage void math_state_restore(struct pt_regs regs)
1056{
1057 struct thread_info *thread = current_thread_info();
1058 struct task_struct *tsk = thread->task;
1059
1060 clts(); /* Allow maths ops (or we recurse) */
1061 if (!tsk_used_math(tsk))
1062 init_fpu(tsk);
1063 restore_fpu(tsk);
1064 thread->status |= TS_USEDFPU; /* So we fnsave on switch_to() */
1065}
1066
1067#ifndef CONFIG_MATH_EMULATION
1068
1069asmlinkage void math_emulate(long arg)
1070{
Dave Jones9c107802006-01-09 20:51:32 -08001071 printk(KERN_EMERG "math-emulation not enabled and no coprocessor found.\n");
1072 printk(KERN_EMERG "killing %s.\n",current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 force_sig(SIGFPE,current);
1074 schedule();
1075}
1076
1077#endif /* CONFIG_MATH_EMULATION */
1078
1079#ifdef CONFIG_X86_F00F_BUG
1080void __init trap_init_f00f_bug(void)
1081{
1082 __set_fixmap(FIX_F00F_IDT, __pa(&idt_table), PAGE_KERNEL_RO);
1083
1084 /*
1085 * Update the IDT descriptor and reload the IDT so that
1086 * it uses the read-only mapped virtual address.
1087 */
1088 idt_descr.address = fix_to_virt(FIX_F00F_IDT);
Zachary Amsden4d37e7e2005-09-03 15:56:38 -07001089 load_idt(&idt_descr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091#endif
1092
1093#define _set_gate(gate_addr,type,dpl,addr,seg) \
1094do { \
1095 int __d0, __d1; \
1096 __asm__ __volatile__ ("movw %%dx,%%ax\n\t" \
1097 "movw %4,%%dx\n\t" \
1098 "movl %%eax,%0\n\t" \
1099 "movl %%edx,%1" \
1100 :"=m" (*((long *) (gate_addr))), \
1101 "=m" (*(1+(long *) (gate_addr))), "=&a" (__d0), "=&d" (__d1) \
1102 :"i" ((short) (0x8000+(dpl<<13)+(type<<8))), \
1103 "3" ((char *) (addr)),"2" ((seg) << 16)); \
1104} while (0)
1105
1106
1107/*
1108 * This needs to use 'idt_table' rather than 'idt', and
1109 * thus use the _nonmapped_ version of the IDT, as the
1110 * Pentium F0 0F bugfix can have resulted in the mapped
1111 * IDT being write-protected.
1112 */
1113void set_intr_gate(unsigned int n, void *addr)
1114{
1115 _set_gate(idt_table+n,14,0,addr,__KERNEL_CS);
1116}
1117
1118/*
1119 * This routine sets up an interrupt gate at directory privilege level 3.
1120 */
1121static inline void set_system_intr_gate(unsigned int n, void *addr)
1122{
1123 _set_gate(idt_table+n, 14, 3, addr, __KERNEL_CS);
1124}
1125
1126static void __init set_trap_gate(unsigned int n, void *addr)
1127{
1128 _set_gate(idt_table+n,15,0,addr,__KERNEL_CS);
1129}
1130
1131static void __init set_system_gate(unsigned int n, void *addr)
1132{
1133 _set_gate(idt_table+n,15,3,addr,__KERNEL_CS);
1134}
1135
1136static void __init set_task_gate(unsigned int n, unsigned int gdt_entry)
1137{
1138 _set_gate(idt_table+n,5,0,0,(gdt_entry<<3));
1139}
1140
1141
1142void __init trap_init(void)
1143{
1144#ifdef CONFIG_EISA
1145 void __iomem *p = ioremap(0x0FFFD9, 4);
1146 if (readl(p) == 'E'+('I'<<8)+('S'<<16)+('A'<<24)) {
1147 EISA_bus = 1;
1148 }
1149 iounmap(p);
1150#endif
1151
1152#ifdef CONFIG_X86_LOCAL_APIC
1153 init_apic_mappings();
1154#endif
1155
1156 set_trap_gate(0,&divide_error);
1157 set_intr_gate(1,&debug);
1158 set_intr_gate(2,&nmi);
Jan Beulicheb05c322006-01-06 00:11:49 -08001159 set_system_intr_gate(3, &int3); /* int3/4 can be called from all */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 set_system_gate(4,&overflow);
Jan Beulicheb05c322006-01-06 00:11:49 -08001161 set_trap_gate(5,&bounds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 set_trap_gate(6,&invalid_op);
1163 set_trap_gate(7,&device_not_available);
1164 set_task_gate(8,GDT_ENTRY_DOUBLEFAULT_TSS);
1165 set_trap_gate(9,&coprocessor_segment_overrun);
1166 set_trap_gate(10,&invalid_TSS);
1167 set_trap_gate(11,&segment_not_present);
1168 set_trap_gate(12,&stack_segment);
1169 set_trap_gate(13,&general_protection);
1170 set_intr_gate(14,&page_fault);
1171 set_trap_gate(15,&spurious_interrupt_bug);
1172 set_trap_gate(16,&coprocessor_error);
1173 set_trap_gate(17,&alignment_check);
1174#ifdef CONFIG_X86_MCE
1175 set_trap_gate(18,&machine_check);
1176#endif
1177 set_trap_gate(19,&simd_coprocessor_error);
1178
Jan Beulichd43c6e82006-01-06 00:11:49 -08001179 if (cpu_has_fxsr) {
1180 /*
1181 * Verify that the FXSAVE/FXRSTOR data will be 16-byte aligned.
1182 * Generates a compile-time "error: zero width for bit-field" if
1183 * the alignment is wrong.
1184 */
1185 struct fxsrAlignAssert {
1186 int _:!(offsetof(struct task_struct,
1187 thread.i387.fxsave) & 15);
1188 };
1189
1190 printk(KERN_INFO "Enabling fast FPU save and restore... ");
1191 set_in_cr4(X86_CR4_OSFXSR);
1192 printk("done.\n");
1193 }
1194 if (cpu_has_xmm) {
1195 printk(KERN_INFO "Enabling unmasked SIMD FPU exception "
1196 "support... ");
1197 set_in_cr4(X86_CR4_OSXMMEXCPT);
1198 printk("done.\n");
1199 }
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 set_system_gate(SYSCALL_VECTOR,&system_call);
1202
1203 /*
1204 * Should be a barrier for any external CPU state.
1205 */
1206 cpu_init();
1207
1208 trap_init_hook();
1209}
1210
1211static int __init kstack_setup(char *s)
1212{
1213 kstack_depth_to_print = simple_strtoul(s, NULL, 0);
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001214 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216__setup("kstack=", kstack_setup);