traps_32.c 22 KB

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