traps.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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. /*
  266. * ftrace must be first, everything else may cause a recursive crash.
  267. * See note by declaration of modifying_ftrace_code in ftrace.c
  268. */
  269. if (unlikely(atomic_read(&modifying_ftrace_code)) &&
  270. ftrace_int3_handler(regs))
  271. return;
  272. #endif
  273. #ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
  274. if (kgdb_ll_trap(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
  275. SIGTRAP) == NOTIFY_STOP)
  276. return;
  277. #endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
  278. if (notify_die(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
  279. SIGTRAP) == NOTIFY_STOP)
  280. return;
  281. /*
  282. * Let others (NMI) know that the debug stack is in use
  283. * as we may switch to the interrupt stack.
  284. */
  285. debug_stack_usage_inc();
  286. preempt_conditional_sti(regs);
  287. do_trap(X86_TRAP_BP, SIGTRAP, "int3", regs, error_code, NULL);
  288. preempt_conditional_cli(regs);
  289. debug_stack_usage_dec();
  290. }
  291. #ifdef CONFIG_X86_64
  292. /*
  293. * Help handler running on IST stack to switch back to user stack
  294. * for scheduling or signal handling. The actual stack switch is done in
  295. * entry.S
  296. */
  297. asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
  298. {
  299. struct pt_regs *regs = eregs;
  300. /* Did already sync */
  301. if (eregs == (struct pt_regs *)eregs->sp)
  302. ;
  303. /* Exception from user space */
  304. else if (user_mode(eregs))
  305. regs = task_pt_regs(current);
  306. /*
  307. * Exception from kernel and interrupts are enabled. Move to
  308. * kernel process stack.
  309. */
  310. else if (eregs->flags & X86_EFLAGS_IF)
  311. regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
  312. if (eregs != regs)
  313. *regs = *eregs;
  314. return regs;
  315. }
  316. #endif
  317. /*
  318. * Our handling of the processor debug registers is non-trivial.
  319. * We do not clear them on entry and exit from the kernel. Therefore
  320. * it is possible to get a watchpoint trap here from inside the kernel.
  321. * However, the code in ./ptrace.c has ensured that the user can
  322. * only set watchpoints on userspace addresses. Therefore the in-kernel
  323. * watchpoint trap can only occur in code which is reading/writing
  324. * from user space. Such code must not hold kernel locks (since it
  325. * can equally take a page fault), therefore it is safe to call
  326. * force_sig_info even though that claims and releases locks.
  327. *
  328. * Code in ./signal.c ensures that the debug control register
  329. * is restored before we deliver any signal, and therefore that
  330. * user code runs with the correct debug control register even though
  331. * we clear it here.
  332. *
  333. * Being careful here means that we don't have to be as careful in a
  334. * lot of more complicated places (task switching can be a bit lazy
  335. * about restoring all the debug state, and ptrace doesn't have to
  336. * find every occurrence of the TF bit that could be saved away even
  337. * by user code)
  338. *
  339. * May run on IST stack.
  340. */
  341. dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code)
  342. {
  343. struct task_struct *tsk = current;
  344. int user_icebp = 0;
  345. unsigned long dr6;
  346. int si_code;
  347. get_debugreg(dr6, 6);
  348. /* Filter out all the reserved bits which are preset to 1 */
  349. dr6 &= ~DR6_RESERVED;
  350. /*
  351. * If dr6 has no reason to give us about the origin of this trap,
  352. * then it's very likely the result of an icebp/int01 trap.
  353. * User wants a sigtrap for that.
  354. */
  355. if (!dr6 && user_mode(regs))
  356. user_icebp = 1;
  357. /* Catch kmemcheck conditions first of all! */
  358. if ((dr6 & DR_STEP) && kmemcheck_trap(regs))
  359. return;
  360. /* DR6 may or may not be cleared by the CPU */
  361. set_debugreg(0, 6);
  362. /*
  363. * The processor cleared BTF, so don't mark that we need it set.
  364. */
  365. clear_tsk_thread_flag(tsk, TIF_BLOCKSTEP);
  366. /* Store the virtualized DR6 value */
  367. tsk->thread.debugreg6 = dr6;
  368. if (notify_die(DIE_DEBUG, "debug", regs, PTR_ERR(&dr6), error_code,
  369. SIGTRAP) == NOTIFY_STOP)
  370. return;
  371. /*
  372. * Let others (NMI) know that the debug stack is in use
  373. * as we may switch to the interrupt stack.
  374. */
  375. debug_stack_usage_inc();
  376. /* It's safe to allow irq's after DR6 has been saved */
  377. preempt_conditional_sti(regs);
  378. if (regs->flags & X86_VM_MASK) {
  379. handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code,
  380. X86_TRAP_DB);
  381. preempt_conditional_cli(regs);
  382. debug_stack_usage_dec();
  383. return;
  384. }
  385. /*
  386. * Single-stepping through system calls: ignore any exceptions in
  387. * kernel space, but re-enable TF when returning to user mode.
  388. *
  389. * We already checked v86 mode above, so we can check for kernel mode
  390. * by just checking the CPL of CS.
  391. */
  392. if ((dr6 & DR_STEP) && !user_mode(regs)) {
  393. tsk->thread.debugreg6 &= ~DR_STEP;
  394. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  395. regs->flags &= ~X86_EFLAGS_TF;
  396. }
  397. si_code = get_si_code(tsk->thread.debugreg6);
  398. if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp)
  399. send_sigtrap(tsk, regs, error_code, si_code);
  400. preempt_conditional_cli(regs);
  401. debug_stack_usage_dec();
  402. return;
  403. }
  404. /*
  405. * Note that we play around with the 'TS' bit in an attempt to get
  406. * the correct behaviour even in the presence of the asynchronous
  407. * IRQ13 behaviour
  408. */
  409. void math_error(struct pt_regs *regs, int error_code, int trapnr)
  410. {
  411. struct task_struct *task = current;
  412. siginfo_t info;
  413. unsigned short err;
  414. char *str = (trapnr == X86_TRAP_MF) ? "fpu exception" :
  415. "simd exception";
  416. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, SIGFPE) == NOTIFY_STOP)
  417. return;
  418. conditional_sti(regs);
  419. if (!user_mode_vm(regs))
  420. {
  421. if (!fixup_exception(regs)) {
  422. task->thread.error_code = error_code;
  423. task->thread.trap_nr = trapnr;
  424. die(str, regs, error_code);
  425. }
  426. return;
  427. }
  428. /*
  429. * Save the info for the exception handler and clear the error.
  430. */
  431. save_init_fpu(task);
  432. task->thread.trap_nr = trapnr;
  433. task->thread.error_code = error_code;
  434. info.si_signo = SIGFPE;
  435. info.si_errno = 0;
  436. info.si_addr = (void __user *)regs->ip;
  437. if (trapnr == X86_TRAP_MF) {
  438. unsigned short cwd, swd;
  439. /*
  440. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  441. * status. 0x3f is the exception bits in these regs, 0x200 is the
  442. * C1 reg you need in case of a stack fault, 0x040 is the stack
  443. * fault bit. We should only be taking one exception at a time,
  444. * so if this combination doesn't produce any single exception,
  445. * then we have a bad program that isn't synchronizing its FPU usage
  446. * and it will suffer the consequences since we won't be able to
  447. * fully reproduce the context of the exception
  448. */
  449. cwd = get_fpu_cwd(task);
  450. swd = get_fpu_swd(task);
  451. err = swd & ~cwd;
  452. } else {
  453. /*
  454. * The SIMD FPU exceptions are handled a little differently, as there
  455. * is only a single status/control register. Thus, to determine which
  456. * unmasked exception was caught we must mask the exception mask bits
  457. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  458. */
  459. unsigned short mxcsr = get_fpu_mxcsr(task);
  460. err = ~(mxcsr >> 7) & mxcsr;
  461. }
  462. if (err & 0x001) { /* Invalid op */
  463. /*
  464. * swd & 0x240 == 0x040: Stack Underflow
  465. * swd & 0x240 == 0x240: Stack Overflow
  466. * User must clear the SF bit (0x40) if set
  467. */
  468. info.si_code = FPE_FLTINV;
  469. } else if (err & 0x004) { /* Divide by Zero */
  470. info.si_code = FPE_FLTDIV;
  471. } else if (err & 0x008) { /* Overflow */
  472. info.si_code = FPE_FLTOVF;
  473. } else if (err & 0x012) { /* Denormal, Underflow */
  474. info.si_code = FPE_FLTUND;
  475. } else if (err & 0x020) { /* Precision */
  476. info.si_code = FPE_FLTRES;
  477. } else {
  478. /*
  479. * If we're using IRQ 13, or supposedly even some trap
  480. * X86_TRAP_MF implementations, it's possible
  481. * we get a spurious trap, which is not an error.
  482. */
  483. return;
  484. }
  485. force_sig_info(SIGFPE, &info, task);
  486. }
  487. dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
  488. {
  489. #ifdef CONFIG_X86_32
  490. ignore_fpu_irq = 1;
  491. #endif
  492. math_error(regs, error_code, X86_TRAP_MF);
  493. }
  494. dotraplinkage void
  495. do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
  496. {
  497. math_error(regs, error_code, X86_TRAP_XF);
  498. }
  499. dotraplinkage void
  500. do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
  501. {
  502. conditional_sti(regs);
  503. #if 0
  504. /* No need to warn about this any longer. */
  505. printk(KERN_INFO "Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
  506. #endif
  507. }
  508. asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
  509. {
  510. }
  511. asmlinkage void __attribute__((weak)) smp_threshold_interrupt(void)
  512. {
  513. }
  514. /*
  515. * 'math_state_restore()' saves the current math information in the
  516. * old math state array, and gets the new ones from the current task
  517. *
  518. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  519. * Don't touch unless you *really* know how it works.
  520. *
  521. * Must be called with kernel preemption disabled (eg with local
  522. * local interrupts as in the case of do_device_not_available).
  523. */
  524. void math_state_restore(void)
  525. {
  526. struct task_struct *tsk = current;
  527. if (!tsk_used_math(tsk)) {
  528. local_irq_enable();
  529. /*
  530. * does a slab alloc which can sleep
  531. */
  532. if (init_fpu(tsk)) {
  533. /*
  534. * ran out of memory!
  535. */
  536. do_group_exit(SIGKILL);
  537. return;
  538. }
  539. local_irq_disable();
  540. }
  541. __thread_fpu_begin(tsk);
  542. /*
  543. * Paranoid restore. send a SIGSEGV if we fail to restore the state.
  544. */
  545. if (unlikely(restore_fpu_checking(tsk))) {
  546. __thread_fpu_end(tsk);
  547. force_sig(SIGSEGV, tsk);
  548. return;
  549. }
  550. tsk->fpu_counter++;
  551. }
  552. EXPORT_SYMBOL_GPL(math_state_restore);
  553. dotraplinkage void __kprobes
  554. do_device_not_available(struct pt_regs *regs, long error_code)
  555. {
  556. #ifdef CONFIG_MATH_EMULATION
  557. if (read_cr0() & X86_CR0_EM) {
  558. struct math_emu_info info = { };
  559. conditional_sti(regs);
  560. info.regs = regs;
  561. math_emulate(&info);
  562. return;
  563. }
  564. #endif
  565. math_state_restore(); /* interrupts still off */
  566. #ifdef CONFIG_X86_32
  567. conditional_sti(regs);
  568. #endif
  569. }
  570. #ifdef CONFIG_X86_32
  571. dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
  572. {
  573. siginfo_t info;
  574. local_irq_enable();
  575. info.si_signo = SIGILL;
  576. info.si_errno = 0;
  577. info.si_code = ILL_BADSTK;
  578. info.si_addr = NULL;
  579. if (notify_die(DIE_TRAP, "iret exception", regs, error_code,
  580. X86_TRAP_IRET, SIGILL) == NOTIFY_STOP)
  581. return;
  582. do_trap(X86_TRAP_IRET, SIGILL, "iret exception", regs, error_code,
  583. &info);
  584. }
  585. #endif
  586. /* Set of traps needed for early debugging. */
  587. void __init early_trap_init(void)
  588. {
  589. set_intr_gate_ist(X86_TRAP_DB, &debug, DEBUG_STACK);
  590. /* int3 can be called from all */
  591. set_system_intr_gate_ist(X86_TRAP_BP, &int3, DEBUG_STACK);
  592. set_intr_gate(X86_TRAP_PF, &page_fault);
  593. load_idt(&idt_descr);
  594. }
  595. void __init trap_init(void)
  596. {
  597. int i;
  598. #ifdef CONFIG_EISA
  599. void __iomem *p = early_ioremap(0x0FFFD9, 4);
  600. if (readl(p) == 'E' + ('I'<<8) + ('S'<<16) + ('A'<<24))
  601. EISA_bus = 1;
  602. early_iounmap(p, 4);
  603. #endif
  604. set_intr_gate(X86_TRAP_DE, &divide_error);
  605. set_intr_gate_ist(X86_TRAP_NMI, &nmi, NMI_STACK);
  606. /* int4 can be called from all */
  607. set_system_intr_gate(X86_TRAP_OF, &overflow);
  608. set_intr_gate(X86_TRAP_BR, &bounds);
  609. set_intr_gate(X86_TRAP_UD, &invalid_op);
  610. set_intr_gate(X86_TRAP_NM, &device_not_available);
  611. #ifdef CONFIG_X86_32
  612. set_task_gate(X86_TRAP_DF, GDT_ENTRY_DOUBLEFAULT_TSS);
  613. #else
  614. set_intr_gate_ist(X86_TRAP_DF, &double_fault, DOUBLEFAULT_STACK);
  615. #endif
  616. set_intr_gate(X86_TRAP_OLD_MF, &coprocessor_segment_overrun);
  617. set_intr_gate(X86_TRAP_TS, &invalid_TSS);
  618. set_intr_gate(X86_TRAP_NP, &segment_not_present);
  619. set_intr_gate_ist(X86_TRAP_SS, &stack_segment, STACKFAULT_STACK);
  620. set_intr_gate(X86_TRAP_GP, &general_protection);
  621. set_intr_gate(X86_TRAP_SPURIOUS, &spurious_interrupt_bug);
  622. set_intr_gate(X86_TRAP_MF, &coprocessor_error);
  623. set_intr_gate(X86_TRAP_AC, &alignment_check);
  624. #ifdef CONFIG_X86_MCE
  625. set_intr_gate_ist(X86_TRAP_MC, &machine_check, MCE_STACK);
  626. #endif
  627. set_intr_gate(X86_TRAP_XF, &simd_coprocessor_error);
  628. /* Reserve all the builtin and the syscall vector: */
  629. for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
  630. set_bit(i, used_vectors);
  631. #ifdef CONFIG_IA32_EMULATION
  632. set_system_intr_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
  633. set_bit(IA32_SYSCALL_VECTOR, used_vectors);
  634. #endif
  635. #ifdef CONFIG_X86_32
  636. set_system_trap_gate(SYSCALL_VECTOR, &system_call);
  637. set_bit(SYSCALL_VECTOR, used_vectors);
  638. #endif
  639. /*
  640. * Should be a barrier for any external CPU state:
  641. */
  642. cpu_init();
  643. x86_init.irqs.trap_init();
  644. #ifdef CONFIG_X86_64
  645. memcpy(&nmi_idt_table, &idt_table, IDT_ENTRIES * 16);
  646. set_nmi_gate(X86_TRAP_DB, &debug);
  647. set_nmi_gate(X86_TRAP_BP, &int3);
  648. #endif
  649. }