traps.c 24 KB

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