traps.c 22 KB

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