traps.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/interrupt.h>
  13. #include <linux/kallsyms.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/kprobes.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/kdebug.h>
  18. #include <linux/kgdb.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/ptrace.h>
  22. #include <linux/string.h>
  23. #include <linux/delay.h>
  24. #include <linux/errno.h>
  25. #include <linux/kexec.h>
  26. #include <linux/sched.h>
  27. #include <linux/timer.h>
  28. #include <linux/init.h>
  29. #include <linux/bug.h>
  30. #include <linux/nmi.h>
  31. #include <linux/mm.h>
  32. #include <linux/smp.h>
  33. #include <linux/io.h>
  34. #ifdef CONFIG_EISA
  35. #include <linux/ioport.h>
  36. #include <linux/eisa.h>
  37. #endif
  38. #if defined(CONFIG_EDAC)
  39. #include <linux/edac.h>
  40. #endif
  41. #include <asm/kmemcheck.h>
  42. #include <asm/stacktrace.h>
  43. #include <asm/processor.h>
  44. #include <asm/debugreg.h>
  45. #include <linux/atomic.h>
  46. #include <asm/ftrace.h>
  47. #include <asm/traps.h>
  48. #include <asm/desc.h>
  49. #include <asm/i387.h>
  50. #include <asm/fpu-internal.h>
  51. #include <asm/mce.h>
  52. #include <asm/context_tracking.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. /*
  63. * The IDT has to be page-aligned to simplify the Pentium
  64. * F0 0F bug workaround.
  65. */
  66. gate_desc idt_table[NR_VECTORS] __page_aligned_data = { { { { 0, 0 } } }, };
  67. #endif
  68. DECLARE_BITMAP(used_vectors, NR_VECTORS);
  69. EXPORT_SYMBOL_GPL(used_vectors);
  70. static inline void conditional_sti(struct pt_regs *regs)
  71. {
  72. if (regs->flags & X86_EFLAGS_IF)
  73. local_irq_enable();
  74. }
  75. static inline void preempt_conditional_sti(struct pt_regs *regs)
  76. {
  77. inc_preempt_count();
  78. if (regs->flags & X86_EFLAGS_IF)
  79. local_irq_enable();
  80. }
  81. static inline void conditional_cli(struct pt_regs *regs)
  82. {
  83. if (regs->flags & X86_EFLAGS_IF)
  84. local_irq_disable();
  85. }
  86. static inline void preempt_conditional_cli(struct pt_regs *regs)
  87. {
  88. if (regs->flags & X86_EFLAGS_IF)
  89. local_irq_disable();
  90. dec_preempt_count();
  91. }
  92. static int __kprobes
  93. do_trap_no_signal(struct task_struct *tsk, int trapnr, char *str,
  94. struct pt_regs *regs, long error_code)
  95. {
  96. #ifdef CONFIG_X86_32
  97. if (regs->flags & X86_VM_MASK) {
  98. /*
  99. * Traps 0, 1, 3, 4, and 5 should be forwarded to vm86.
  100. * On nmi (interrupt 2), do_trap should not be called.
  101. */
  102. if (trapnr < X86_TRAP_UD) {
  103. if (!handle_vm86_trap((struct kernel_vm86_regs *) regs,
  104. error_code, trapnr))
  105. return 0;
  106. }
  107. return -1;
  108. }
  109. #endif
  110. if (!user_mode(regs)) {
  111. if (!fixup_exception(regs)) {
  112. tsk->thread.error_code = error_code;
  113. tsk->thread.trap_nr = trapnr;
  114. die(str, regs, error_code);
  115. }
  116. return 0;
  117. }
  118. return -1;
  119. }
  120. static void __kprobes
  121. do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
  122. long error_code, siginfo_t *info)
  123. {
  124. struct task_struct *tsk = current;
  125. if (!do_trap_no_signal(tsk, trapnr, str, regs, error_code))
  126. return;
  127. /*
  128. * We want error_code and trap_nr 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_nr = trapnr;
  138. #ifdef CONFIG_X86_64
  139. if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
  140. printk_ratelimit()) {
  141. pr_info("%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. pr_cont("\n");
  146. }
  147. #endif
  148. if (info)
  149. force_sig_info(signr, info, tsk);
  150. else
  151. force_sig(signr, tsk);
  152. }
  153. #define DO_ERROR(trapnr, signr, str, name) \
  154. dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
  155. { \
  156. exception_enter(regs); \
  157. if (notify_die(DIE_TRAP, str, regs, error_code, \
  158. trapnr, signr) == NOTIFY_STOP) { \
  159. exception_exit(regs); \
  160. return; \
  161. } \
  162. conditional_sti(regs); \
  163. do_trap(trapnr, signr, str, regs, error_code, NULL); \
  164. exception_exit(regs); \
  165. }
  166. #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
  167. dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
  168. { \
  169. siginfo_t info; \
  170. info.si_signo = signr; \
  171. info.si_errno = 0; \
  172. info.si_code = sicode; \
  173. info.si_addr = (void __user *)siaddr; \
  174. exception_enter(regs); \
  175. if (notify_die(DIE_TRAP, str, regs, error_code, \
  176. trapnr, signr) == NOTIFY_STOP) { \
  177. exception_exit(regs); \
  178. return; \
  179. } \
  180. conditional_sti(regs); \
  181. do_trap(trapnr, signr, str, regs, error_code, &info); \
  182. exception_exit(regs); \
  183. }
  184. DO_ERROR_INFO(X86_TRAP_DE, SIGFPE, "divide error", divide_error, FPE_INTDIV,
  185. regs->ip)
  186. DO_ERROR(X86_TRAP_OF, SIGSEGV, "overflow", overflow)
  187. DO_ERROR(X86_TRAP_BR, SIGSEGV, "bounds", bounds)
  188. DO_ERROR_INFO(X86_TRAP_UD, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN,
  189. regs->ip)
  190. DO_ERROR(X86_TRAP_OLD_MF, SIGFPE, "coprocessor segment overrun",
  191. coprocessor_segment_overrun)
  192. DO_ERROR(X86_TRAP_TS, SIGSEGV, "invalid TSS", invalid_TSS)
  193. DO_ERROR(X86_TRAP_NP, SIGBUS, "segment not present", segment_not_present)
  194. #ifdef CONFIG_X86_32
  195. DO_ERROR(X86_TRAP_SS, SIGBUS, "stack segment", stack_segment)
  196. #endif
  197. DO_ERROR_INFO(X86_TRAP_AC, SIGBUS, "alignment check", alignment_check,
  198. BUS_ADRALN, 0)
  199. #ifdef CONFIG_X86_64
  200. /* Runs on IST stack */
  201. dotraplinkage void do_stack_segment(struct pt_regs *regs, long error_code)
  202. {
  203. exception_enter(regs);
  204. if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
  205. X86_TRAP_SS, SIGBUS) != NOTIFY_STOP) {
  206. preempt_conditional_sti(regs);
  207. do_trap(X86_TRAP_SS, SIGBUS, "stack segment", regs, error_code, NULL);
  208. preempt_conditional_cli(regs);
  209. }
  210. exception_exit(regs);
  211. }
  212. dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code)
  213. {
  214. static const char str[] = "double fault";
  215. struct task_struct *tsk = current;
  216. exception_enter(regs);
  217. /* Return not checked because double check cannot be ignored */
  218. notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_DF, SIGSEGV);
  219. tsk->thread.error_code = error_code;
  220. tsk->thread.trap_nr = X86_TRAP_DF;
  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. exception_enter(regs);
  234. conditional_sti(regs);
  235. #ifdef CONFIG_X86_32
  236. if (regs->flags & X86_VM_MASK) {
  237. local_irq_enable();
  238. handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
  239. goto exit;
  240. }
  241. #endif
  242. tsk = current;
  243. if (!user_mode(regs)) {
  244. if (fixup_exception(regs))
  245. goto exit;
  246. tsk->thread.error_code = error_code;
  247. tsk->thread.trap_nr = X86_TRAP_GP;
  248. if (notify_die(DIE_GPF, "general protection fault", regs, error_code,
  249. X86_TRAP_GP, SIGSEGV) != NOTIFY_STOP)
  250. die("general protection fault", regs, error_code);
  251. goto exit;
  252. }
  253. tsk->thread.error_code = error_code;
  254. tsk->thread.trap_nr = X86_TRAP_GP;
  255. if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
  256. printk_ratelimit()) {
  257. pr_info("%s[%d] general protection ip:%lx sp:%lx error:%lx",
  258. tsk->comm, task_pid_nr(tsk),
  259. regs->ip, regs->sp, error_code);
  260. print_vma_addr(" in ", regs->ip);
  261. pr_cont("\n");
  262. }
  263. force_sig(SIGSEGV, tsk);
  264. exit:
  265. exception_exit(regs);
  266. }
  267. /* May run on IST stack. */
  268. dotraplinkage void __kprobes notrace do_int3(struct pt_regs *regs, long error_code)
  269. {
  270. #ifdef CONFIG_DYNAMIC_FTRACE
  271. /*
  272. * ftrace must be first, everything else may cause a recursive crash.
  273. * See note by declaration of modifying_ftrace_code in ftrace.c
  274. */
  275. if (unlikely(atomic_read(&modifying_ftrace_code)) &&
  276. ftrace_int3_handler(regs))
  277. return;
  278. #endif
  279. exception_enter(regs);
  280. #ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
  281. if (kgdb_ll_trap(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
  282. SIGTRAP) == NOTIFY_STOP)
  283. goto exit;
  284. #endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
  285. if (notify_die(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
  286. SIGTRAP) == NOTIFY_STOP)
  287. goto exit;
  288. /*
  289. * Let others (NMI) know that the debug stack is in use
  290. * as we may switch to the interrupt stack.
  291. */
  292. debug_stack_usage_inc();
  293. preempt_conditional_sti(regs);
  294. do_trap(X86_TRAP_BP, SIGTRAP, "int3", regs, error_code, NULL);
  295. preempt_conditional_cli(regs);
  296. debug_stack_usage_dec();
  297. exit:
  298. exception_exit(regs);
  299. }
  300. #ifdef CONFIG_X86_64
  301. /*
  302. * Help handler running on IST stack to switch back to user stack
  303. * for scheduling or signal handling. The actual stack switch is done in
  304. * entry.S
  305. */
  306. asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
  307. {
  308. struct pt_regs *regs = eregs;
  309. /* Did already sync */
  310. if (eregs == (struct pt_regs *)eregs->sp)
  311. ;
  312. /* Exception from user space */
  313. else if (user_mode(eregs))
  314. regs = task_pt_regs(current);
  315. /*
  316. * Exception from kernel and interrupts are enabled. Move to
  317. * kernel process stack.
  318. */
  319. else if (eregs->flags & X86_EFLAGS_IF)
  320. regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
  321. if (eregs != regs)
  322. *regs = *eregs;
  323. return regs;
  324. }
  325. #endif
  326. /*
  327. * Our handling of the processor debug registers is non-trivial.
  328. * We do not clear them on entry and exit from the kernel. Therefore
  329. * it is possible to get a watchpoint trap here from inside the kernel.
  330. * However, the code in ./ptrace.c has ensured that the user can
  331. * only set watchpoints on userspace addresses. Therefore the in-kernel
  332. * watchpoint trap can only occur in code which is reading/writing
  333. * from user space. Such code must not hold kernel locks (since it
  334. * can equally take a page fault), therefore it is safe to call
  335. * force_sig_info even though that claims and releases locks.
  336. *
  337. * Code in ./signal.c ensures that the debug control register
  338. * is restored before we deliver any signal, and therefore that
  339. * user code runs with the correct debug control register even though
  340. * we clear it here.
  341. *
  342. * Being careful here means that we don't have to be as careful in a
  343. * lot of more complicated places (task switching can be a bit lazy
  344. * about restoring all the debug state, and ptrace doesn't have to
  345. * find every occurrence of the TF bit that could be saved away even
  346. * by user code)
  347. *
  348. * May run on IST stack.
  349. */
  350. dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code)
  351. {
  352. struct task_struct *tsk = current;
  353. int user_icebp = 0;
  354. unsigned long dr6;
  355. int si_code;
  356. exception_enter(regs);
  357. get_debugreg(dr6, 6);
  358. /* Filter out all the reserved bits which are preset to 1 */
  359. dr6 &= ~DR6_RESERVED;
  360. /*
  361. * If dr6 has no reason to give us about the origin of this trap,
  362. * then it's very likely the result of an icebp/int01 trap.
  363. * User wants a sigtrap for that.
  364. */
  365. if (!dr6 && user_mode(regs))
  366. user_icebp = 1;
  367. /* Catch kmemcheck conditions first of all! */
  368. if ((dr6 & DR_STEP) && kmemcheck_trap(regs))
  369. goto exit;
  370. /* DR6 may or may not be cleared by the CPU */
  371. set_debugreg(0, 6);
  372. /*
  373. * The processor cleared BTF, so don't mark that we need it set.
  374. */
  375. clear_tsk_thread_flag(tsk, TIF_BLOCKSTEP);
  376. /* Store the virtualized DR6 value */
  377. tsk->thread.debugreg6 = dr6;
  378. if (notify_die(DIE_DEBUG, "debug", regs, PTR_ERR(&dr6), error_code,
  379. SIGTRAP) == NOTIFY_STOP)
  380. goto exit;
  381. /*
  382. * Let others (NMI) know that the debug stack is in use
  383. * as we may switch to the interrupt stack.
  384. */
  385. debug_stack_usage_inc();
  386. /* It's safe to allow irq's after DR6 has been saved */
  387. preempt_conditional_sti(regs);
  388. if (regs->flags & X86_VM_MASK) {
  389. handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code,
  390. X86_TRAP_DB);
  391. preempt_conditional_cli(regs);
  392. debug_stack_usage_dec();
  393. goto exit;
  394. }
  395. /*
  396. * Single-stepping through system calls: ignore any exceptions in
  397. * kernel space, but re-enable TF when returning to user mode.
  398. *
  399. * We already checked v86 mode above, so we can check for kernel mode
  400. * by just checking the CPL of CS.
  401. */
  402. if ((dr6 & DR_STEP) && !user_mode(regs)) {
  403. tsk->thread.debugreg6 &= ~DR_STEP;
  404. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  405. regs->flags &= ~X86_EFLAGS_TF;
  406. }
  407. si_code = get_si_code(tsk->thread.debugreg6);
  408. if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp)
  409. send_sigtrap(tsk, regs, error_code, si_code);
  410. preempt_conditional_cli(regs);
  411. debug_stack_usage_dec();
  412. exit:
  413. exception_exit(regs);
  414. }
  415. /*
  416. * Note that we play around with the 'TS' bit in an attempt to get
  417. * the correct behaviour even in the presence of the asynchronous
  418. * IRQ13 behaviour
  419. */
  420. void math_error(struct pt_regs *regs, int error_code, int trapnr)
  421. {
  422. struct task_struct *task = current;
  423. siginfo_t info;
  424. unsigned short err;
  425. char *str = (trapnr == X86_TRAP_MF) ? "fpu exception" :
  426. "simd exception";
  427. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, SIGFPE) == NOTIFY_STOP)
  428. return;
  429. conditional_sti(regs);
  430. if (!user_mode_vm(regs))
  431. {
  432. if (!fixup_exception(regs)) {
  433. task->thread.error_code = error_code;
  434. task->thread.trap_nr = trapnr;
  435. die(str, regs, error_code);
  436. }
  437. return;
  438. }
  439. /*
  440. * Save the info for the exception handler and clear the error.
  441. */
  442. save_init_fpu(task);
  443. task->thread.trap_nr = trapnr;
  444. task->thread.error_code = error_code;
  445. info.si_signo = SIGFPE;
  446. info.si_errno = 0;
  447. info.si_addr = (void __user *)regs->ip;
  448. if (trapnr == X86_TRAP_MF) {
  449. unsigned short cwd, swd;
  450. /*
  451. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  452. * status. 0x3f is the exception bits in these regs, 0x200 is the
  453. * C1 reg you need in case of a stack fault, 0x040 is the stack
  454. * fault bit. We should only be taking one exception at a time,
  455. * so if this combination doesn't produce any single exception,
  456. * then we have a bad program that isn't synchronizing its FPU usage
  457. * and it will suffer the consequences since we won't be able to
  458. * fully reproduce the context of the exception
  459. */
  460. cwd = get_fpu_cwd(task);
  461. swd = get_fpu_swd(task);
  462. err = swd & ~cwd;
  463. } else {
  464. /*
  465. * The SIMD FPU exceptions are handled a little differently, as there
  466. * is only a single status/control register. Thus, to determine which
  467. * unmasked exception was caught we must mask the exception mask bits
  468. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  469. */
  470. unsigned short mxcsr = get_fpu_mxcsr(task);
  471. err = ~(mxcsr >> 7) & mxcsr;
  472. }
  473. if (err & 0x001) { /* Invalid op */
  474. /*
  475. * swd & 0x240 == 0x040: Stack Underflow
  476. * swd & 0x240 == 0x240: Stack Overflow
  477. * User must clear the SF bit (0x40) if set
  478. */
  479. info.si_code = FPE_FLTINV;
  480. } else if (err & 0x004) { /* Divide by Zero */
  481. info.si_code = FPE_FLTDIV;
  482. } else if (err & 0x008) { /* Overflow */
  483. info.si_code = FPE_FLTOVF;
  484. } else if (err & 0x012) { /* Denormal, Underflow */
  485. info.si_code = FPE_FLTUND;
  486. } else if (err & 0x020) { /* Precision */
  487. info.si_code = FPE_FLTRES;
  488. } else {
  489. /*
  490. * If we're using IRQ 13, or supposedly even some trap
  491. * X86_TRAP_MF implementations, it's possible
  492. * we get a spurious trap, which is not an error.
  493. */
  494. return;
  495. }
  496. force_sig_info(SIGFPE, &info, task);
  497. }
  498. dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
  499. {
  500. exception_enter(regs);
  501. math_error(regs, error_code, X86_TRAP_MF);
  502. exception_exit(regs);
  503. }
  504. dotraplinkage void
  505. do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
  506. {
  507. exception_enter(regs);
  508. math_error(regs, error_code, X86_TRAP_XF);
  509. exception_exit(regs);
  510. }
  511. dotraplinkage void
  512. do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
  513. {
  514. conditional_sti(regs);
  515. #if 0
  516. /* No need to warn about this any longer. */
  517. pr_info("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
  518. #endif
  519. }
  520. asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
  521. {
  522. }
  523. asmlinkage void __attribute__((weak)) smp_threshold_interrupt(void)
  524. {
  525. }
  526. /*
  527. * 'math_state_restore()' saves the current math information in the
  528. * old math state array, and gets the new ones from the current task
  529. *
  530. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  531. * Don't touch unless you *really* know how it works.
  532. *
  533. * Must be called with kernel preemption disabled (eg with local
  534. * local interrupts as in the case of do_device_not_available).
  535. */
  536. void math_state_restore(void)
  537. {
  538. struct task_struct *tsk = current;
  539. if (!tsk_used_math(tsk)) {
  540. local_irq_enable();
  541. /*
  542. * does a slab alloc which can sleep
  543. */
  544. if (init_fpu(tsk)) {
  545. /*
  546. * ran out of memory!
  547. */
  548. do_group_exit(SIGKILL);
  549. return;
  550. }
  551. local_irq_disable();
  552. }
  553. __thread_fpu_begin(tsk);
  554. /*
  555. * Paranoid restore. send a SIGSEGV if we fail to restore the state.
  556. */
  557. if (unlikely(restore_fpu_checking(tsk))) {
  558. drop_init_fpu(tsk);
  559. force_sig(SIGSEGV, tsk);
  560. return;
  561. }
  562. tsk->fpu_counter++;
  563. }
  564. EXPORT_SYMBOL_GPL(math_state_restore);
  565. dotraplinkage void __kprobes
  566. do_device_not_available(struct pt_regs *regs, long error_code)
  567. {
  568. exception_enter(regs);
  569. BUG_ON(use_eager_fpu());
  570. #ifdef CONFIG_MATH_EMULATION
  571. if (read_cr0() & X86_CR0_EM) {
  572. struct math_emu_info info = { };
  573. conditional_sti(regs);
  574. info.regs = regs;
  575. math_emulate(&info);
  576. exception_exit(regs);
  577. return;
  578. }
  579. #endif
  580. math_state_restore(); /* interrupts still off */
  581. #ifdef CONFIG_X86_32
  582. conditional_sti(regs);
  583. #endif
  584. exception_exit(regs);
  585. }
  586. #ifdef CONFIG_X86_32
  587. dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
  588. {
  589. siginfo_t info;
  590. exception_enter(regs);
  591. local_irq_enable();
  592. info.si_signo = SIGILL;
  593. info.si_errno = 0;
  594. info.si_code = ILL_BADSTK;
  595. info.si_addr = NULL;
  596. if (notify_die(DIE_TRAP, "iret exception", regs, error_code,
  597. X86_TRAP_IRET, SIGILL) != NOTIFY_STOP) {
  598. do_trap(X86_TRAP_IRET, SIGILL, "iret exception", regs, error_code,
  599. &info);
  600. }
  601. exception_exit(regs);
  602. }
  603. #endif
  604. /* Set of traps needed for early debugging. */
  605. void __init early_trap_init(void)
  606. {
  607. set_intr_gate_ist(X86_TRAP_DB, &debug, DEBUG_STACK);
  608. /* int3 can be called from all */
  609. set_system_intr_gate_ist(X86_TRAP_BP, &int3, DEBUG_STACK);
  610. #ifdef CONFIG_X86_32
  611. set_intr_gate(X86_TRAP_PF, &page_fault);
  612. #endif
  613. load_idt(&idt_descr);
  614. }
  615. void __init early_trap_pf_init(void)
  616. {
  617. #ifdef CONFIG_X86_64
  618. set_intr_gate(X86_TRAP_PF, &page_fault);
  619. #endif
  620. }
  621. void __init trap_init(void)
  622. {
  623. int i;
  624. #ifdef CONFIG_EISA
  625. void __iomem *p = early_ioremap(0x0FFFD9, 4);
  626. if (readl(p) == 'E' + ('I'<<8) + ('S'<<16) + ('A'<<24))
  627. EISA_bus = 1;
  628. early_iounmap(p, 4);
  629. #endif
  630. set_intr_gate(X86_TRAP_DE, &divide_error);
  631. set_intr_gate_ist(X86_TRAP_NMI, &nmi, NMI_STACK);
  632. /* int4 can be called from all */
  633. set_system_intr_gate(X86_TRAP_OF, &overflow);
  634. set_intr_gate(X86_TRAP_BR, &bounds);
  635. set_intr_gate(X86_TRAP_UD, &invalid_op);
  636. set_intr_gate(X86_TRAP_NM, &device_not_available);
  637. #ifdef CONFIG_X86_32
  638. set_task_gate(X86_TRAP_DF, GDT_ENTRY_DOUBLEFAULT_TSS);
  639. #else
  640. set_intr_gate_ist(X86_TRAP_DF, &double_fault, DOUBLEFAULT_STACK);
  641. #endif
  642. set_intr_gate(X86_TRAP_OLD_MF, &coprocessor_segment_overrun);
  643. set_intr_gate(X86_TRAP_TS, &invalid_TSS);
  644. set_intr_gate(X86_TRAP_NP, &segment_not_present);
  645. set_intr_gate_ist(X86_TRAP_SS, &stack_segment, STACKFAULT_STACK);
  646. set_intr_gate(X86_TRAP_GP, &general_protection);
  647. set_intr_gate(X86_TRAP_SPURIOUS, &spurious_interrupt_bug);
  648. set_intr_gate(X86_TRAP_MF, &coprocessor_error);
  649. set_intr_gate(X86_TRAP_AC, &alignment_check);
  650. #ifdef CONFIG_X86_MCE
  651. set_intr_gate_ist(X86_TRAP_MC, &machine_check, MCE_STACK);
  652. #endif
  653. set_intr_gate(X86_TRAP_XF, &simd_coprocessor_error);
  654. /* Reserve all the builtin and the syscall vector: */
  655. for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
  656. set_bit(i, used_vectors);
  657. #ifdef CONFIG_IA32_EMULATION
  658. set_system_intr_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
  659. set_bit(IA32_SYSCALL_VECTOR, used_vectors);
  660. #endif
  661. #ifdef CONFIG_X86_32
  662. set_system_trap_gate(SYSCALL_VECTOR, &system_call);
  663. set_bit(SYSCALL_VECTOR, used_vectors);
  664. #endif
  665. /*
  666. * Should be a barrier for any external CPU state:
  667. */
  668. cpu_init();
  669. x86_init.irqs.trap_init();
  670. #ifdef CONFIG_X86_64
  671. memcpy(&nmi_idt_table, &idt_table, IDT_ENTRIES * 16);
  672. set_nmi_gate(X86_TRAP_DB, &debug);
  673. set_nmi_gate(X86_TRAP_BP, &int3);
  674. #endif
  675. }