traps.c 24 KB

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