traps.c 22 KB

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