traps.c 22 KB

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