traps.c 19 KB

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