traps.c 23 KB

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