0206c915748cc5c346c8cb5df5347fd75ea57b43
[firefly-linux-kernel-4.4.55.git] / arch / x86 / kernel / traps_32.c
1 /*
2  *  Copyright (C) 1991, 1992  Linus Torvalds
3  *  Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
4  *
5  *  Pentium III FXSR, SSE support
6  *      Gareth Hughes <gareth@valinux.com>, May 2000
7  */
8
9 /*
10  * 'Traps.c' handles hardware traps and faults after we have saved some
11  * state in 'asm.s'.
12  */
13 #include <linux/interrupt.h>
14 #include <linux/kallsyms.h>
15 #include <linux/spinlock.h>
16 #include <linux/highmem.h>
17 #include <linux/kprobes.h>
18 #include <linux/uaccess.h>
19 #include <linux/utsname.h>
20 #include <linux/kdebug.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/ptrace.h>
24 #include <linux/string.h>
25 #include <linux/unwind.h>
26 #include <linux/delay.h>
27 #include <linux/errno.h>
28 #include <linux/kexec.h>
29 #include <linux/sched.h>
30 #include <linux/timer.h>
31 #include <linux/init.h>
32 #include <linux/bug.h>
33 #include <linux/nmi.h>
34 #include <linux/mm.h>
35
36 #ifdef CONFIG_EISA
37 #include <linux/ioport.h>
38 #include <linux/eisa.h>
39 #endif
40
41 #ifdef CONFIG_MCA
42 #include <linux/mca.h>
43 #endif
44
45 #if defined(CONFIG_EDAC)
46 #include <linux/edac.h>
47 #endif
48
49 #include <asm/processor-flags.h>
50 #include <asm/arch_hooks.h>
51 #include <asm/stacktrace.h>
52 #include <asm/processor.h>
53 #include <asm/debugreg.h>
54 #include <asm/atomic.h>
55 #include <asm/system.h>
56 #include <asm/unwind.h>
57 #include <asm/desc.h>
58 #include <asm/i387.h>
59 #include <asm/nmi.h>
60 #include <asm/smp.h>
61 #include <asm/io.h>
62 #include <asm/traps.h>
63
64 #include "mach_traps.h"
65 #include "cpu/mcheck/mce.h"
66
67 DECLARE_BITMAP(used_vectors, NR_VECTORS);
68 EXPORT_SYMBOL_GPL(used_vectors);
69
70 asmlinkage int system_call(void);
71
72 /* Do we ignore FPU interrupts ? */
73 char ignore_fpu_irq;
74
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  */
80 gate_desc idt_table[256]
81         __attribute__((__section__(".data.idt"))) = { { { { 0, 0 } } }, };
82
83 static int ignore_nmis;
84
85 static inline void conditional_sti(struct pt_regs *regs)
86 {
87         if (regs->flags & X86_EFLAGS_IF)
88                 local_irq_enable();
89 }
90
91 static inline void
92 die_if_kernel(const char *str, struct pt_regs *regs, long err)
93 {
94         if (!user_mode_vm(regs))
95                 die(str, regs, err);
96 }
97
98 /*
99  * Perform the lazy TSS's I/O bitmap copy. If the TSS has an
100  * invalid offset set (the LAZY one) and the faulting thread has
101  * a valid I/O bitmap pointer, we copy the I/O bitmap in the TSS,
102  * we set the offset field correctly and return 1.
103  */
104 static int lazy_iobitmap_copy(void)
105 {
106         struct thread_struct *thread;
107         struct tss_struct *tss;
108         int cpu;
109
110         cpu = get_cpu();
111         tss = &per_cpu(init_tss, cpu);
112         thread = &current->thread;
113
114         if (tss->x86_tss.io_bitmap_base == INVALID_IO_BITMAP_OFFSET_LAZY &&
115             thread->io_bitmap_ptr) {
116                 memcpy(tss->io_bitmap, thread->io_bitmap_ptr,
117                        thread->io_bitmap_max);
118                 /*
119                  * If the previously set map was extending to higher ports
120                  * than the current one, pad extra space with 0xff (no access).
121                  */
122                 if (thread->io_bitmap_max < tss->io_bitmap_max) {
123                         memset((char *) tss->io_bitmap +
124                                 thread->io_bitmap_max, 0xff,
125                                 tss->io_bitmap_max - thread->io_bitmap_max);
126                 }
127                 tss->io_bitmap_max = thread->io_bitmap_max;
128                 tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
129                 tss->io_bitmap_owner = thread;
130                 put_cpu();
131
132                 return 1;
133         }
134         put_cpu();
135
136         return 0;
137 }
138
139 static void __kprobes
140 do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
141         long error_code, siginfo_t *info)
142 {
143         struct task_struct *tsk = current;
144
145         if (regs->flags & X86_VM_MASK) {
146                 /*
147                  * traps 0, 1, 3, 4, and 5 should be forwarded to vm86.
148                  * On nmi (interrupt 2), do_trap should not be called.
149                  */
150                 if (trapnr < 6)
151                         goto vm86_trap;
152                 goto trap_signal;
153         }
154
155         if (!user_mode(regs))
156                 goto kernel_trap;
157
158 trap_signal:
159         /*
160          * We want error_code and trap_no set for userspace faults and
161          * kernelspace faults which result in die(), but not
162          * kernelspace faults which are fixed up.  die() gives the
163          * process no chance to handle the signal and notice the
164          * kernel fault information, so that won't result in polluting
165          * the information about previously queued, but not yet
166          * delivered, faults.  See also do_general_protection below.
167          */
168         tsk->thread.error_code = error_code;
169         tsk->thread.trap_no = trapnr;
170
171         if (info)
172                 force_sig_info(signr, info, tsk);
173         else
174                 force_sig(signr, tsk);
175         return;
176
177 kernel_trap:
178         if (!fixup_exception(regs)) {
179                 tsk->thread.error_code = error_code;
180                 tsk->thread.trap_no = trapnr;
181                 die(str, regs, error_code);
182         }
183         return;
184
185 vm86_trap:
186         if (handle_vm86_trap((struct kernel_vm86_regs *) regs,
187                                                 error_code, trapnr))
188                 goto trap_signal;
189         return;
190 }
191
192 #define DO_ERROR(trapnr, signr, str, name)                              \
193 void do_##name(struct pt_regs *regs, long error_code)                   \
194 {                                                                       \
195         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr)  \
196                                                         == NOTIFY_STOP) \
197                 return;                                                 \
198         conditional_sti(regs);                                          \
199         do_trap(trapnr, signr, str, regs, error_code, NULL);            \
200 }
201
202 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr)         \
203 void do_##name(struct pt_regs *regs, long error_code)                   \
204 {                                                                       \
205         siginfo_t info;                                                 \
206         info.si_signo = signr;                                          \
207         info.si_errno = 0;                                              \
208         info.si_code = sicode;                                          \
209         info.si_addr = (void __user *)siaddr;                           \
210         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr)  \
211                                                         == NOTIFY_STOP) \
212                 return;                                                 \
213         conditional_sti(regs);                                          \
214         do_trap(trapnr, signr, str, regs, error_code, &info);           \
215 }
216
217 DO_ERROR_INFO(0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
218 DO_ERROR(4, SIGSEGV, "overflow", overflow)
219 DO_ERROR(5, SIGSEGV, "bounds", bounds)
220 DO_ERROR_INFO(6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
221 DO_ERROR(9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
222 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
223 DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
224 DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
225 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
226
227 void __kprobes
228 do_general_protection(struct pt_regs *regs, long error_code)
229 {
230         struct task_struct *tsk;
231
232         conditional_sti(regs);
233
234         if (lazy_iobitmap_copy()) {
235                 /* restart the faulting instruction */
236                 return;
237         }
238
239         if (regs->flags & X86_VM_MASK)
240                 goto gp_in_vm86;
241
242         tsk = current;
243         if (!user_mode(regs))
244                 goto gp_in_kernel;
245
246         tsk->thread.error_code = error_code;
247         tsk->thread.trap_no = 13;
248
249         if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
250                         printk_ratelimit()) {
251                 printk(KERN_INFO
252                         "%s[%d] general protection ip:%lx sp:%lx error:%lx",
253                         tsk->comm, task_pid_nr(tsk),
254                         regs->ip, regs->sp, error_code);
255                 print_vma_addr(" in ", regs->ip);
256                 printk("\n");
257         }
258
259         force_sig(SIGSEGV, tsk);
260         return;
261
262 gp_in_vm86:
263         local_irq_enable();
264         handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
265         return;
266
267 gp_in_kernel:
268         if (fixup_exception(regs))
269                 return;
270
271         tsk->thread.error_code = error_code;
272         tsk->thread.trap_no = 13;
273         if (notify_die(DIE_GPF, "general protection fault", regs,
274                                 error_code, 13, SIGSEGV) == NOTIFY_STOP)
275                 return;
276         die("general protection fault", regs, error_code);
277 }
278
279 static notrace __kprobes void
280 mem_parity_error(unsigned char reason, struct pt_regs *regs)
281 {
282         printk(KERN_EMERG
283                 "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
284                         reason, smp_processor_id());
285
286         printk(KERN_EMERG
287                 "You have some hardware problem, likely on the PCI bus.\n");
288
289 #if defined(CONFIG_EDAC)
290         if (edac_handler_set()) {
291                 edac_atomic_assert_error();
292                 return;
293         }
294 #endif
295
296         if (panic_on_unrecovered_nmi)
297                 panic("NMI: Not continuing");
298
299         printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
300
301         /* Clear and disable the memory parity error line. */
302         clear_mem_error(reason);
303 }
304
305 static notrace __kprobes void
306 io_check_error(unsigned char reason, struct pt_regs *regs)
307 {
308         unsigned long i;
309
310         printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
311         show_registers(regs);
312
313         /* Re-enable the IOCK line, wait for a few seconds */
314         reason = (reason & 0xf) | 8;
315         outb(reason, 0x61);
316
317         i = 2000;
318         while (--i)
319                 udelay(1000);
320
321         reason &= ~8;
322         outb(reason, 0x61);
323 }
324
325 static notrace __kprobes void
326 unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
327 {
328         if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
329                 return;
330 #ifdef CONFIG_MCA
331         /*
332          * Might actually be able to figure out what the guilty party
333          * is:
334          */
335         if (MCA_bus) {
336                 mca_handle_nmi();
337                 return;
338         }
339 #endif
340         printk(KERN_EMERG
341                 "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
342                         reason, smp_processor_id());
343
344         printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
345         if (panic_on_unrecovered_nmi)
346                 panic("NMI: Not continuing");
347
348         printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
349 }
350
351 static DEFINE_SPINLOCK(nmi_print_lock);
352
353 void notrace __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
354 {
355         if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
356                 return;
357
358         spin_lock(&nmi_print_lock);
359         /*
360         * We are in trouble anyway, lets at least try
361         * to get a message out:
362         */
363         bust_spinlocks(1);
364         printk(KERN_EMERG "%s", str);
365         printk(" on CPU%d, ip %08lx, registers:\n",
366                 smp_processor_id(), regs->ip);
367         show_registers(regs);
368         if (do_panic)
369                 panic("Non maskable interrupt");
370         console_silent();
371         spin_unlock(&nmi_print_lock);
372         bust_spinlocks(0);
373
374         /*
375          * If we are in kernel we are probably nested up pretty bad
376          * and might aswell get out now while we still can:
377          */
378         if (!user_mode_vm(regs)) {
379                 current->thread.trap_no = 2;
380                 crash_kexec(regs);
381         }
382
383         do_exit(SIGSEGV);
384 }
385
386 static notrace __kprobes void default_do_nmi(struct pt_regs *regs)
387 {
388         unsigned char reason = 0;
389         int cpu;
390
391         cpu = smp_processor_id();
392
393         /* Only the BSP gets external NMIs from the system. */
394         if (!cpu)
395                 reason = get_nmi_reason();
396
397         if (!(reason & 0xc0)) {
398                 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
399                                                                 == NOTIFY_STOP)
400                         return;
401 #ifdef CONFIG_X86_LOCAL_APIC
402                 /*
403                  * Ok, so this is none of the documented NMI sources,
404                  * so it must be the NMI watchdog.
405                  */
406                 if (nmi_watchdog_tick(regs, reason))
407                         return;
408                 if (!do_nmi_callback(regs, cpu))
409                         unknown_nmi_error(reason, regs);
410 #else
411                 unknown_nmi_error(reason, regs);
412 #endif
413
414                 return;
415         }
416         if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
417                 return;
418
419         /* AK: following checks seem to be broken on modern chipsets. FIXME */
420         if (reason & 0x80)
421                 mem_parity_error(reason, regs);
422         if (reason & 0x40)
423                 io_check_error(reason, regs);
424         /*
425          * Reassert NMI in case it became active meanwhile
426          * as it's edge-triggered:
427          */
428         reassert_nmi();
429 }
430
431 notrace __kprobes void do_nmi(struct pt_regs *regs, long error_code)
432 {
433         int cpu;
434
435         nmi_enter();
436
437         cpu = smp_processor_id();
438
439         ++nmi_count(cpu);
440
441         if (!ignore_nmis)
442                 default_do_nmi(regs);
443
444         nmi_exit();
445 }
446
447 void stop_nmi(void)
448 {
449         acpi_nmi_disable();
450         ignore_nmis++;
451 }
452
453 void restart_nmi(void)
454 {
455         ignore_nmis--;
456         acpi_nmi_enable();
457 }
458
459 void __kprobes do_int3(struct pt_regs *regs, long error_code)
460 {
461 #ifdef CONFIG_KPROBES
462         if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
463                         == NOTIFY_STOP)
464                 return;
465         conditional_sti(regs);
466 #else
467         if (notify_die(DIE_TRAP, "int3", regs, error_code, 3, SIGTRAP)
468                         == NOTIFY_STOP)
469                 return;
470 #endif
471
472         do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
473 }
474
475 /*
476  * Our handling of the processor debug registers is non-trivial.
477  * We do not clear them on entry and exit from the kernel. Therefore
478  * it is possible to get a watchpoint trap here from inside the kernel.
479  * However, the code in ./ptrace.c has ensured that the user can
480  * only set watchpoints on userspace addresses. Therefore the in-kernel
481  * watchpoint trap can only occur in code which is reading/writing
482  * from user space. Such code must not hold kernel locks (since it
483  * can equally take a page fault), therefore it is safe to call
484  * force_sig_info even though that claims and releases locks.
485  *
486  * Code in ./signal.c ensures that the debug control register
487  * is restored before we deliver any signal, and therefore that
488  * user code runs with the correct debug control register even though
489  * we clear it here.
490  *
491  * Being careful here means that we don't have to be as careful in a
492  * lot of more complicated places (task switching can be a bit lazy
493  * about restoring all the debug state, and ptrace doesn't have to
494  * find every occurrence of the TF bit that could be saved away even
495  * by user code)
496  */
497 void __kprobes do_debug(struct pt_regs *regs, long error_code)
498 {
499         struct task_struct *tsk = current;
500         unsigned int condition;
501         int si_code;
502
503         get_debugreg(condition, 6);
504
505         /*
506          * The processor cleared BTF, so don't mark that we need it set.
507          */
508         clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
509         tsk->thread.debugctlmsr = 0;
510
511         if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
512                                                 SIGTRAP) == NOTIFY_STOP)
513                 return;
514         /* It's safe to allow irq's after DR6 has been saved */
515         if (regs->flags & X86_EFLAGS_IF)
516                 local_irq_enable();
517
518         /* Mask out spurious debug traps due to lazy DR7 setting */
519         if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
520                 if (!tsk->thread.debugreg7)
521                         goto clear_dr7;
522         }
523
524         if (regs->flags & X86_VM_MASK)
525                 goto debug_vm86;
526
527         /* Save debug status register where ptrace can see it */
528         tsk->thread.debugreg6 = condition;
529
530         /*
531          * Single-stepping through TF: make sure we ignore any events in
532          * kernel space (but re-enable TF when returning to user mode).
533          */
534         if (condition & DR_STEP) {
535                 /*
536                  * We already checked v86 mode above, so we can
537                  * check for kernel mode by just checking the CPL
538                  * of CS.
539                  */
540                 if (!user_mode(regs))
541                         goto clear_TF_reenable;
542         }
543
544         si_code = get_si_code((unsigned long)condition);
545         /* Ok, finally something we can handle */
546         send_sigtrap(tsk, regs, error_code, si_code);
547
548         /*
549          * Disable additional traps. They'll be re-enabled when
550          * the signal is delivered.
551          */
552 clear_dr7:
553         set_debugreg(0, 7);
554         return;
555
556 debug_vm86:
557         handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
558         return;
559
560 clear_TF_reenable:
561         set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
562         regs->flags &= ~X86_EFLAGS_TF;
563         return;
564 }
565
566 /*
567  * Note that we play around with the 'TS' bit in an attempt to get
568  * the correct behaviour even in the presence of the asynchronous
569  * IRQ13 behaviour
570  */
571 void math_error(void __user *ip)
572 {
573         struct task_struct *task;
574         siginfo_t info;
575         unsigned short cwd, swd;
576
577         /*
578          * Save the info for the exception handler and clear the error.
579          */
580         task = current;
581         save_init_fpu(task);
582         task->thread.trap_no = 16;
583         task->thread.error_code = 0;
584         info.si_signo = SIGFPE;
585         info.si_errno = 0;
586         info.si_code = __SI_FAULT;
587         info.si_addr = ip;
588         /*
589          * (~cwd & swd) will mask out exceptions that are not set to unmasked
590          * status.  0x3f is the exception bits in these regs, 0x200 is the
591          * C1 reg you need in case of a stack fault, 0x040 is the stack
592          * fault bit.  We should only be taking one exception at a time,
593          * so if this combination doesn't produce any single exception,
594          * then we have a bad program that isn't synchronizing its FPU usage
595          * and it will suffer the consequences since we won't be able to
596          * fully reproduce the context of the exception
597          */
598         cwd = get_fpu_cwd(task);
599         swd = get_fpu_swd(task);
600         switch (swd & ~cwd & 0x3f) {
601         case 0x000: /* No unmasked exception */
602                 return;
603         default: /* Multiple exceptions */
604                 break;
605         case 0x001: /* Invalid Op */
606                 /*
607                  * swd & 0x240 == 0x040: Stack Underflow
608                  * swd & 0x240 == 0x240: Stack Overflow
609                  * User must clear the SF bit (0x40) if set
610                  */
611                 info.si_code = FPE_FLTINV;
612                 break;
613         case 0x002: /* Denormalize */
614         case 0x010: /* Underflow */
615                 info.si_code = FPE_FLTUND;
616                 break;
617         case 0x004: /* Zero Divide */
618                 info.si_code = FPE_FLTDIV;
619                 break;
620         case 0x008: /* Overflow */
621                 info.si_code = FPE_FLTOVF;
622                 break;
623         case 0x020: /* Precision */
624                 info.si_code = FPE_FLTRES;
625                 break;
626         }
627         force_sig_info(SIGFPE, &info, task);
628 }
629
630 void do_coprocessor_error(struct pt_regs *regs, long error_code)
631 {
632         conditional_sti(regs);
633         ignore_fpu_irq = 1;
634         math_error((void __user *)regs->ip);
635 }
636
637 static void simd_math_error(void __user *ip)
638 {
639         struct task_struct *task;
640         siginfo_t info;
641         unsigned short mxcsr;
642
643         /*
644          * Save the info for the exception handler and clear the error.
645          */
646         task = current;
647         save_init_fpu(task);
648         task->thread.trap_no = 19;
649         task->thread.error_code = 0;
650         info.si_signo = SIGFPE;
651         info.si_errno = 0;
652         info.si_code = __SI_FAULT;
653         info.si_addr = ip;
654         /*
655          * The SIMD FPU exceptions are handled a little differently, as there
656          * is only a single status/control register.  Thus, to determine which
657          * unmasked exception was caught we must mask the exception mask bits
658          * at 0x1f80, and then use these to mask the exception bits at 0x3f.
659          */
660         mxcsr = get_fpu_mxcsr(task);
661         switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
662         case 0x000:
663         default:
664                 break;
665         case 0x001: /* Invalid Op */
666                 info.si_code = FPE_FLTINV;
667                 break;
668         case 0x002: /* Denormalize */
669         case 0x010: /* Underflow */
670                 info.si_code = FPE_FLTUND;
671                 break;
672         case 0x004: /* Zero Divide */
673                 info.si_code = FPE_FLTDIV;
674                 break;
675         case 0x008: /* Overflow */
676                 info.si_code = FPE_FLTOVF;
677                 break;
678         case 0x020: /* Precision */
679                 info.si_code = FPE_FLTRES;
680                 break;
681         }
682         force_sig_info(SIGFPE, &info, task);
683 }
684
685 void do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
686 {
687         conditional_sti(regs);
688
689         if (cpu_has_xmm) {
690                 /* Handle SIMD FPU exceptions on PIII+ processors. */
691                 ignore_fpu_irq = 1;
692                 simd_math_error((void __user *)regs->ip);
693                 return;
694         }
695         /*
696          * Handle strange cache flush from user space exception
697          * in all other cases.  This is undocumented behaviour.
698          */
699         if (regs->flags & X86_VM_MASK) {
700                 handle_vm86_fault((struct kernel_vm86_regs *)regs, error_code);
701                 return;
702         }
703         current->thread.trap_no = 19;
704         current->thread.error_code = error_code;
705         die_if_kernel("cache flush denied", regs, error_code);
706         force_sig(SIGSEGV, current);
707 }
708
709 void do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
710 {
711         conditional_sti(regs);
712 #if 0
713         /* No need to warn about this any longer. */
714         printk(KERN_INFO "Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
715 #endif
716 }
717
718 unsigned long patch_espfix_desc(unsigned long uesp, unsigned long kesp)
719 {
720         struct desc_struct *gdt = get_cpu_gdt_table(smp_processor_id());
721         unsigned long base = (kesp - uesp) & -THREAD_SIZE;
722         unsigned long new_kesp = kesp - base;
723         unsigned long lim_pages = (new_kesp | (THREAD_SIZE - 1)) >> PAGE_SHIFT;
724         __u64 desc = *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS];
725
726         /* Set up base for espfix segment */
727         desc &= 0x00f0ff0000000000ULL;
728         desc |= ((((__u64)base) << 16) & 0x000000ffffff0000ULL) |
729                 ((((__u64)base) << 32) & 0xff00000000000000ULL) |
730                 ((((__u64)lim_pages) << 32) & 0x000f000000000000ULL) |
731                 (lim_pages & 0xffff);
732         *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS] = desc;
733
734         return new_kesp;
735 }
736
737 /*
738  * 'math_state_restore()' saves the current math information in the
739  * old math state array, and gets the new ones from the current task
740  *
741  * Careful.. There are problems with IBM-designed IRQ13 behaviour.
742  * Don't touch unless you *really* know how it works.
743  *
744  * Must be called with kernel preemption disabled (in this case,
745  * local interrupts are disabled at the call-site in entry.S).
746  */
747 asmlinkage void math_state_restore(void)
748 {
749         struct thread_info *thread = current_thread_info();
750         struct task_struct *tsk = thread->task;
751
752         if (!tsk_used_math(tsk)) {
753                 local_irq_enable();
754                 /*
755                  * does a slab alloc which can sleep
756                  */
757                 if (init_fpu(tsk)) {
758                         /*
759                          * ran out of memory!
760                          */
761                         do_group_exit(SIGKILL);
762                         return;
763                 }
764                 local_irq_disable();
765         }
766
767         clts();                         /* Allow maths ops (or we recurse) */
768         restore_fpu(tsk);
769         thread->status |= TS_USEDFPU;   /* So we fnsave on switch_to() */
770         tsk->fpu_counter++;
771 }
772 EXPORT_SYMBOL_GPL(math_state_restore);
773
774 #ifndef CONFIG_MATH_EMULATION
775
776 asmlinkage void math_emulate(long arg)
777 {
778         printk(KERN_EMERG
779                 "math-emulation not enabled and no coprocessor found.\n");
780         printk(KERN_EMERG "killing %s.\n", current->comm);
781         force_sig(SIGFPE, current);
782         schedule();
783 }
784
785 #endif /* CONFIG_MATH_EMULATION */
786
787 void __kprobes do_device_not_available(struct pt_regs *regs, long error)
788 {
789         if (read_cr0() & X86_CR0_EM) {
790                 conditional_sti(regs);
791                 math_emulate(0);
792         } else {
793                 math_state_restore(); /* interrupts still off */
794                 conditional_sti(regs);
795         }
796 }
797
798 #ifdef CONFIG_X86_MCE
799 void __kprobes do_machine_check(struct pt_regs *regs, long error)
800 {
801         conditional_sti(regs);
802         machine_check_vector(regs, error);
803 }
804 #endif
805
806 void do_iret_error(struct pt_regs *regs, long error_code)
807 {
808         siginfo_t info;
809         local_irq_enable();
810
811         info.si_signo = SIGILL;
812         info.si_errno = 0;
813         info.si_code = ILL_BADSTK;
814         info.si_addr = 0;
815         if (notify_die(DIE_TRAP, "iret exception",
816                         regs, error_code, 32, SIGILL) == NOTIFY_STOP)
817                 return;
818         do_trap(32, SIGILL, "iret exception", regs, error_code, &info);
819 }
820
821 void __init trap_init(void)
822 {
823         int i;
824
825 #ifdef CONFIG_EISA
826         void __iomem *p = early_ioremap(0x0FFFD9, 4);
827
828         if (readl(p) == 'E' + ('I'<<8) + ('S'<<16) + ('A'<<24))
829                 EISA_bus = 1;
830         early_iounmap(p, 4);
831 #endif
832
833         set_intr_gate(0, &divide_error);
834         set_intr_gate(1, &debug);
835         set_intr_gate(2, &nmi);
836         set_system_intr_gate(3, &int3); /* int3 can be called from all */
837         set_system_intr_gate(4, &overflow); /* int4 can be called from all */
838         set_intr_gate(5, &bounds);
839         set_intr_gate(6, &invalid_op);
840         set_intr_gate(7, &device_not_available);
841         set_task_gate(8, GDT_ENTRY_DOUBLEFAULT_TSS);
842         set_intr_gate(9, &coprocessor_segment_overrun);
843         set_intr_gate(10, &invalid_TSS);
844         set_intr_gate(11, &segment_not_present);
845         set_intr_gate(12, &stack_segment);
846         set_intr_gate(13, &general_protection);
847         set_intr_gate(14, &page_fault);
848         set_intr_gate(15, &spurious_interrupt_bug);
849         set_intr_gate(16, &coprocessor_error);
850         set_intr_gate(17, &alignment_check);
851 #ifdef CONFIG_X86_MCE
852         set_intr_gate(18, &machine_check);
853 #endif
854         set_intr_gate(19, &simd_coprocessor_error);
855
856         if (cpu_has_fxsr) {
857                 printk(KERN_INFO "Enabling fast FPU save and restore... ");
858                 set_in_cr4(X86_CR4_OSFXSR);
859                 printk("done.\n");
860         }
861         if (cpu_has_xmm) {
862                 printk(KERN_INFO
863                         "Enabling unmasked SIMD FPU exception support... ");
864                 set_in_cr4(X86_CR4_OSXMMEXCPT);
865                 printk("done.\n");
866         }
867
868         set_system_gate(SYSCALL_VECTOR, &system_call);
869
870         /* Reserve all the builtin and the syscall vector: */
871         for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
872                 set_bit(i, used_vectors);
873
874         set_bit(SYSCALL_VECTOR, used_vectors);
875
876         /*
877          * Should be a barrier for any external CPU state:
878          */
879         cpu_init();
880
881         trap_init_hook();
882 }