traps.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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/mce.h>
  53. #include <asm/mach_traps.h>
  54. #ifdef CONFIG_X86_64
  55. #include <asm/pgalloc.h>
  56. #include <asm/proto.h>
  57. #else
  58. #include <asm/processor-flags.h>
  59. #include <asm/setup.h>
  60. #include <asm/traps.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. if (panic_on_io_nmi)
  298. panic("NMI IOCK error: Not continuing");
  299. /* Re-enable the IOCK line, wait for a few seconds */
  300. reason = (reason & 0xf) | 8;
  301. outb(reason, 0x61);
  302. i = 2000;
  303. while (--i)
  304. udelay(1000);
  305. reason &= ~8;
  306. outb(reason, 0x61);
  307. }
  308. static notrace __kprobes void
  309. unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
  310. {
  311. if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) ==
  312. NOTIFY_STOP)
  313. return;
  314. #ifdef CONFIG_MCA
  315. /*
  316. * Might actually be able to figure out what the guilty party
  317. * is:
  318. */
  319. if (MCA_bus) {
  320. mca_handle_nmi();
  321. return;
  322. }
  323. #endif
  324. printk(KERN_EMERG
  325. "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
  326. reason, smp_processor_id());
  327. printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
  328. if (panic_on_unrecovered_nmi)
  329. panic("NMI: Not continuing");
  330. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  331. }
  332. static notrace __kprobes void default_do_nmi(struct pt_regs *regs)
  333. {
  334. unsigned char reason = 0;
  335. int cpu;
  336. cpu = smp_processor_id();
  337. /* Only the BSP gets external NMIs from the system. */
  338. if (!cpu)
  339. reason = get_nmi_reason();
  340. if (!(reason & 0xc0)) {
  341. if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
  342. == NOTIFY_STOP)
  343. return;
  344. #ifdef CONFIG_X86_LOCAL_APIC
  345. /*
  346. * Ok, so this is none of the documented NMI sources,
  347. * so it must be the NMI watchdog.
  348. */
  349. if (nmi_watchdog_tick(regs, reason))
  350. return;
  351. if (!do_nmi_callback(regs, cpu))
  352. unknown_nmi_error(reason, regs);
  353. #else
  354. unknown_nmi_error(reason, regs);
  355. #endif
  356. return;
  357. }
  358. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
  359. return;
  360. /* AK: following checks seem to be broken on modern chipsets. FIXME */
  361. if (reason & 0x80)
  362. mem_parity_error(reason, regs);
  363. if (reason & 0x40)
  364. io_check_error(reason, regs);
  365. #ifdef CONFIG_X86_32
  366. /*
  367. * Reassert NMI in case it became active meanwhile
  368. * as it's edge-triggered:
  369. */
  370. reassert_nmi();
  371. #endif
  372. }
  373. dotraplinkage notrace __kprobes void
  374. do_nmi(struct pt_regs *regs, long error_code)
  375. {
  376. nmi_enter();
  377. inc_irq_stat(__nmi_count);
  378. if (!ignore_nmis)
  379. default_do_nmi(regs);
  380. nmi_exit();
  381. }
  382. void stop_nmi(void)
  383. {
  384. acpi_nmi_disable();
  385. ignore_nmis++;
  386. }
  387. void restart_nmi(void)
  388. {
  389. ignore_nmis--;
  390. acpi_nmi_enable();
  391. }
  392. /* May run on IST stack. */
  393. dotraplinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)
  394. {
  395. #ifdef CONFIG_KPROBES
  396. if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
  397. == NOTIFY_STOP)
  398. return;
  399. #else
  400. if (notify_die(DIE_TRAP, "int3", regs, error_code, 3, SIGTRAP)
  401. == NOTIFY_STOP)
  402. return;
  403. #endif
  404. preempt_conditional_sti(regs);
  405. do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
  406. preempt_conditional_cli(regs);
  407. }
  408. #ifdef CONFIG_X86_64
  409. /*
  410. * Help handler running on IST stack to switch back to user stack
  411. * for scheduling or signal handling. The actual stack switch is done in
  412. * entry.S
  413. */
  414. asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
  415. {
  416. struct pt_regs *regs = eregs;
  417. /* Did already sync */
  418. if (eregs == (struct pt_regs *)eregs->sp)
  419. ;
  420. /* Exception from user space */
  421. else if (user_mode(eregs))
  422. regs = task_pt_regs(current);
  423. /*
  424. * Exception from kernel and interrupts are enabled. Move to
  425. * kernel process stack.
  426. */
  427. else if (eregs->flags & X86_EFLAGS_IF)
  428. regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
  429. if (eregs != regs)
  430. *regs = *eregs;
  431. return regs;
  432. }
  433. #endif
  434. /*
  435. * Our handling of the processor debug registers is non-trivial.
  436. * We do not clear them on entry and exit from the kernel. Therefore
  437. * it is possible to get a watchpoint trap here from inside the kernel.
  438. * However, the code in ./ptrace.c has ensured that the user can
  439. * only set watchpoints on userspace addresses. Therefore the in-kernel
  440. * watchpoint trap can only occur in code which is reading/writing
  441. * from user space. Such code must not hold kernel locks (since it
  442. * can equally take a page fault), therefore it is safe to call
  443. * force_sig_info even though that claims and releases locks.
  444. *
  445. * Code in ./signal.c ensures that the debug control register
  446. * is restored before we deliver any signal, and therefore that
  447. * user code runs with the correct debug control register even though
  448. * we clear it here.
  449. *
  450. * Being careful here means that we don't have to be as careful in a
  451. * lot of more complicated places (task switching can be a bit lazy
  452. * about restoring all the debug state, and ptrace doesn't have to
  453. * find every occurrence of the TF bit that could be saved away even
  454. * by user code)
  455. *
  456. * May run on IST stack.
  457. */
  458. dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code)
  459. {
  460. struct task_struct *tsk = current;
  461. unsigned long condition;
  462. int si_code;
  463. get_debugreg(condition, 6);
  464. /* Catch kmemcheck conditions first of all! */
  465. if (condition & DR_STEP && kmemcheck_trap(regs))
  466. return;
  467. /*
  468. * The processor cleared BTF, so don't mark that we need it set.
  469. */
  470. clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
  471. tsk->thread.debugctlmsr = 0;
  472. if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
  473. SIGTRAP) == NOTIFY_STOP)
  474. return;
  475. /* It's safe to allow irq's after DR6 has been saved */
  476. preempt_conditional_sti(regs);
  477. /* Mask out spurious debug traps due to lazy DR7 setting */
  478. if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
  479. if (!tsk->thread.debugreg7)
  480. goto clear_dr7;
  481. }
  482. #ifdef CONFIG_X86_32
  483. if (regs->flags & X86_VM_MASK)
  484. goto debug_vm86;
  485. #endif
  486. /* Save debug status register where ptrace can see it */
  487. tsk->thread.debugreg6 = condition;
  488. /*
  489. * Single-stepping through TF: make sure we ignore any events in
  490. * kernel space (but re-enable TF when returning to user mode).
  491. */
  492. if (condition & DR_STEP) {
  493. if (!user_mode(regs))
  494. goto clear_TF_reenable;
  495. }
  496. si_code = get_si_code(condition);
  497. /* Ok, finally something we can handle */
  498. send_sigtrap(tsk, regs, error_code, si_code);
  499. /*
  500. * Disable additional traps. They'll be re-enabled when
  501. * the signal is delivered.
  502. */
  503. clear_dr7:
  504. set_debugreg(0, 7);
  505. preempt_conditional_cli(regs);
  506. return;
  507. #ifdef CONFIG_X86_32
  508. debug_vm86:
  509. /* reenable preemption: handle_vm86_trap() might sleep */
  510. dec_preempt_count();
  511. handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
  512. conditional_cli(regs);
  513. return;
  514. #endif
  515. clear_TF_reenable:
  516. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  517. regs->flags &= ~X86_EFLAGS_TF;
  518. preempt_conditional_cli(regs);
  519. return;
  520. }
  521. #ifdef CONFIG_X86_64
  522. static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
  523. {
  524. if (fixup_exception(regs))
  525. return 1;
  526. notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
  527. /* Illegal floating point operation in the kernel */
  528. current->thread.trap_no = trapnr;
  529. die(str, regs, 0);
  530. return 0;
  531. }
  532. #endif
  533. /*
  534. * Note that we play around with the 'TS' bit in an attempt to get
  535. * the correct behaviour even in the presence of the asynchronous
  536. * IRQ13 behaviour
  537. */
  538. void math_error(void __user *ip)
  539. {
  540. struct task_struct *task;
  541. siginfo_t info;
  542. unsigned short cwd, swd, err;
  543. /*
  544. * Save the info for the exception handler and clear the error.
  545. */
  546. task = current;
  547. save_init_fpu(task);
  548. task->thread.trap_no = 16;
  549. task->thread.error_code = 0;
  550. info.si_signo = SIGFPE;
  551. info.si_errno = 0;
  552. info.si_addr = ip;
  553. /*
  554. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  555. * status. 0x3f is the exception bits in these regs, 0x200 is the
  556. * C1 reg you need in case of a stack fault, 0x040 is the stack
  557. * fault bit. We should only be taking one exception at a time,
  558. * so if this combination doesn't produce any single exception,
  559. * then we have a bad program that isn't synchronizing its FPU usage
  560. * and it will suffer the consequences since we won't be able to
  561. * fully reproduce the context of the exception
  562. */
  563. cwd = get_fpu_cwd(task);
  564. swd = get_fpu_swd(task);
  565. err = swd & ~cwd;
  566. if (err & 0x001) { /* Invalid op */
  567. /*
  568. * swd & 0x240 == 0x040: Stack Underflow
  569. * swd & 0x240 == 0x240: Stack Overflow
  570. * User must clear the SF bit (0x40) if set
  571. */
  572. info.si_code = FPE_FLTINV;
  573. } else if (err & 0x004) { /* Divide by Zero */
  574. info.si_code = FPE_FLTDIV;
  575. } else if (err & 0x008) { /* Overflow */
  576. info.si_code = FPE_FLTOVF;
  577. } else if (err & 0x012) { /* Denormal, Underflow */
  578. info.si_code = FPE_FLTUND;
  579. } else if (err & 0x020) { /* Precision */
  580. info.si_code = FPE_FLTRES;
  581. } else {
  582. /*
  583. * If we're using IRQ 13, or supposedly even some trap 16
  584. * implementations, it's possible we get a spurious trap...
  585. */
  586. return; /* Spurious trap, no error */
  587. }
  588. force_sig_info(SIGFPE, &info, task);
  589. }
  590. dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
  591. {
  592. conditional_sti(regs);
  593. #ifdef CONFIG_X86_32
  594. ignore_fpu_irq = 1;
  595. #else
  596. if (!user_mode(regs) &&
  597. kernel_math_error(regs, "kernel x87 math error", 16))
  598. return;
  599. #endif
  600. math_error((void __user *)regs->ip);
  601. }
  602. static void simd_math_error(void __user *ip)
  603. {
  604. struct task_struct *task;
  605. siginfo_t info;
  606. unsigned short mxcsr;
  607. /*
  608. * Save the info for the exception handler and clear the error.
  609. */
  610. task = current;
  611. save_init_fpu(task);
  612. task->thread.trap_no = 19;
  613. task->thread.error_code = 0;
  614. info.si_signo = SIGFPE;
  615. info.si_errno = 0;
  616. info.si_code = __SI_FAULT;
  617. info.si_addr = ip;
  618. /*
  619. * The SIMD FPU exceptions are handled a little differently, as there
  620. * is only a single status/control register. Thus, to determine which
  621. * unmasked exception was caught we must mask the exception mask bits
  622. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  623. */
  624. mxcsr = get_fpu_mxcsr(task);
  625. switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
  626. case 0x000:
  627. default:
  628. break;
  629. case 0x001: /* Invalid Op */
  630. info.si_code = FPE_FLTINV;
  631. break;
  632. case 0x002: /* Denormalize */
  633. case 0x010: /* Underflow */
  634. info.si_code = FPE_FLTUND;
  635. break;
  636. case 0x004: /* Zero Divide */
  637. info.si_code = FPE_FLTDIV;
  638. break;
  639. case 0x008: /* Overflow */
  640. info.si_code = FPE_FLTOVF;
  641. break;
  642. case 0x020: /* Precision */
  643. info.si_code = FPE_FLTRES;
  644. break;
  645. }
  646. force_sig_info(SIGFPE, &info, task);
  647. }
  648. dotraplinkage void
  649. do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
  650. {
  651. conditional_sti(regs);
  652. #ifdef CONFIG_X86_32
  653. if (cpu_has_xmm) {
  654. /* Handle SIMD FPU exceptions on PIII+ processors. */
  655. ignore_fpu_irq = 1;
  656. simd_math_error((void __user *)regs->ip);
  657. return;
  658. }
  659. /*
  660. * Handle strange cache flush from user space exception
  661. * in all other cases. This is undocumented behaviour.
  662. */
  663. if (regs->flags & X86_VM_MASK) {
  664. handle_vm86_fault((struct kernel_vm86_regs *)regs, error_code);
  665. return;
  666. }
  667. current->thread.trap_no = 19;
  668. current->thread.error_code = error_code;
  669. die_if_kernel("cache flush denied", regs, error_code);
  670. force_sig(SIGSEGV, current);
  671. #else
  672. if (!user_mode(regs) &&
  673. kernel_math_error(regs, "kernel simd math error", 19))
  674. return;
  675. simd_math_error((void __user *)regs->ip);
  676. #endif
  677. }
  678. dotraplinkage void
  679. do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
  680. {
  681. conditional_sti(regs);
  682. #if 0
  683. /* No need to warn about this any longer. */
  684. printk(KERN_INFO "Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
  685. #endif
  686. }
  687. #ifdef CONFIG_X86_32
  688. unsigned long patch_espfix_desc(unsigned long uesp, unsigned long kesp)
  689. {
  690. struct desc_struct *gdt = get_cpu_gdt_table(smp_processor_id());
  691. unsigned long base = (kesp - uesp) & -THREAD_SIZE;
  692. unsigned long new_kesp = kesp - base;
  693. unsigned long lim_pages = (new_kesp | (THREAD_SIZE - 1)) >> PAGE_SHIFT;
  694. __u64 desc = *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS];
  695. /* Set up base for espfix segment */
  696. desc &= 0x00f0ff0000000000ULL;
  697. desc |= ((((__u64)base) << 16) & 0x000000ffffff0000ULL) |
  698. ((((__u64)base) << 32) & 0xff00000000000000ULL) |
  699. ((((__u64)lim_pages) << 32) & 0x000f000000000000ULL) |
  700. (lim_pages & 0xffff);
  701. *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS] = desc;
  702. return new_kesp;
  703. }
  704. #endif
  705. asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
  706. {
  707. }
  708. asmlinkage void __attribute__((weak)) smp_threshold_interrupt(void)
  709. {
  710. }
  711. /*
  712. * 'math_state_restore()' saves the current math information in the
  713. * old math state array, and gets the new ones from the current task
  714. *
  715. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  716. * Don't touch unless you *really* know how it works.
  717. *
  718. * Must be called with kernel preemption disabled (in this case,
  719. * local interrupts are disabled at the call-site in entry.S).
  720. */
  721. asmlinkage void math_state_restore(void)
  722. {
  723. struct thread_info *thread = current_thread_info();
  724. struct task_struct *tsk = thread->task;
  725. if (!tsk_used_math(tsk)) {
  726. local_irq_enable();
  727. /*
  728. * does a slab alloc which can sleep
  729. */
  730. if (init_fpu(tsk)) {
  731. /*
  732. * ran out of memory!
  733. */
  734. do_group_exit(SIGKILL);
  735. return;
  736. }
  737. local_irq_disable();
  738. }
  739. clts(); /* Allow maths ops (or we recurse) */
  740. /*
  741. * Paranoid restore. send a SIGSEGV if we fail to restore the state.
  742. */
  743. if (unlikely(restore_fpu_checking(tsk))) {
  744. stts();
  745. force_sig(SIGSEGV, tsk);
  746. return;
  747. }
  748. thread->status |= TS_USEDFPU; /* So we fnsave on switch_to() */
  749. tsk->fpu_counter++;
  750. }
  751. EXPORT_SYMBOL_GPL(math_state_restore);
  752. #ifndef CONFIG_MATH_EMULATION
  753. void math_emulate(struct math_emu_info *info)
  754. {
  755. printk(KERN_EMERG
  756. "math-emulation not enabled and no coprocessor found.\n");
  757. printk(KERN_EMERG "killing %s.\n", current->comm);
  758. force_sig(SIGFPE, current);
  759. schedule();
  760. }
  761. #endif /* CONFIG_MATH_EMULATION */
  762. dotraplinkage void __kprobes
  763. do_device_not_available(struct pt_regs *regs, long error_code)
  764. {
  765. #ifdef CONFIG_X86_32
  766. if (read_cr0() & X86_CR0_EM) {
  767. struct math_emu_info info = { };
  768. conditional_sti(regs);
  769. info.regs = regs;
  770. math_emulate(&info);
  771. } else {
  772. math_state_restore(); /* interrupts still off */
  773. conditional_sti(regs);
  774. }
  775. #else
  776. math_state_restore();
  777. #endif
  778. }
  779. #ifdef CONFIG_X86_32
  780. dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
  781. {
  782. siginfo_t info;
  783. local_irq_enable();
  784. info.si_signo = SIGILL;
  785. info.si_errno = 0;
  786. info.si_code = ILL_BADSTK;
  787. info.si_addr = NULL;
  788. if (notify_die(DIE_TRAP, "iret exception",
  789. regs, error_code, 32, SIGILL) == NOTIFY_STOP)
  790. return;
  791. do_trap(32, SIGILL, "iret exception", regs, error_code, &info);
  792. }
  793. #endif
  794. void __init trap_init(void)
  795. {
  796. int i;
  797. #ifdef CONFIG_EISA
  798. void __iomem *p = early_ioremap(0x0FFFD9, 4);
  799. if (readl(p) == 'E' + ('I'<<8) + ('S'<<16) + ('A'<<24))
  800. EISA_bus = 1;
  801. early_iounmap(p, 4);
  802. #endif
  803. set_intr_gate(0, &divide_error);
  804. set_intr_gate_ist(1, &debug, DEBUG_STACK);
  805. set_intr_gate_ist(2, &nmi, NMI_STACK);
  806. /* int3 can be called from all */
  807. set_system_intr_gate_ist(3, &int3, DEBUG_STACK);
  808. /* int4 can be called from all */
  809. set_system_intr_gate(4, &overflow);
  810. set_intr_gate(5, &bounds);
  811. set_intr_gate(6, &invalid_op);
  812. set_intr_gate(7, &device_not_available);
  813. #ifdef CONFIG_X86_32
  814. set_task_gate(8, GDT_ENTRY_DOUBLEFAULT_TSS);
  815. #else
  816. set_intr_gate_ist(8, &double_fault, DOUBLEFAULT_STACK);
  817. #endif
  818. set_intr_gate(9, &coprocessor_segment_overrun);
  819. set_intr_gate(10, &invalid_TSS);
  820. set_intr_gate(11, &segment_not_present);
  821. set_intr_gate_ist(12, &stack_segment, STACKFAULT_STACK);
  822. set_intr_gate(13, &general_protection);
  823. set_intr_gate(14, &page_fault);
  824. set_intr_gate(15, &spurious_interrupt_bug);
  825. set_intr_gate(16, &coprocessor_error);
  826. set_intr_gate(17, &alignment_check);
  827. #ifdef CONFIG_X86_MCE
  828. set_intr_gate_ist(18, &machine_check, MCE_STACK);
  829. #endif
  830. set_intr_gate(19, &simd_coprocessor_error);
  831. /* Reserve all the builtin and the syscall vector: */
  832. for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
  833. set_bit(i, used_vectors);
  834. #ifdef CONFIG_IA32_EMULATION
  835. set_system_intr_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
  836. set_bit(IA32_SYSCALL_VECTOR, used_vectors);
  837. #endif
  838. #ifdef CONFIG_X86_32
  839. if (cpu_has_fxsr) {
  840. printk(KERN_INFO "Enabling fast FPU save and restore... ");
  841. set_in_cr4(X86_CR4_OSFXSR);
  842. printk("done.\n");
  843. }
  844. if (cpu_has_xmm) {
  845. printk(KERN_INFO
  846. "Enabling unmasked SIMD FPU exception support... ");
  847. set_in_cr4(X86_CR4_OSXMMEXCPT);
  848. printk("done.\n");
  849. }
  850. set_system_trap_gate(SYSCALL_VECTOR, &system_call);
  851. set_bit(SYSCALL_VECTOR, used_vectors);
  852. #endif
  853. /*
  854. * Should be a barrier for any external CPU state:
  855. */
  856. cpu_init();
  857. #ifdef CONFIG_X86_32
  858. x86_quirk_trap_init();
  859. #endif
  860. }