traps.c 20 KB

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