traps.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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. DO_ERROR(X86_TRAP_SS, SIGBUS, "stack segment", stack_segment)
  206. DO_ERROR(X86_TRAP_AC, SIGBUS, "alignment check", alignment_check)
  207. #ifdef CONFIG_X86_64
  208. /* Runs on IST stack */
  209. dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code)
  210. {
  211. static const char str[] = "double fault";
  212. struct task_struct *tsk = current;
  213. #ifdef CONFIG_X86_ESPFIX64
  214. extern unsigned char native_irq_return_iret[];
  215. /*
  216. * If IRET takes a non-IST fault on the espfix64 stack, then we
  217. * end up promoting it to a doublefault. In that case, modify
  218. * the stack to make it look like we just entered the #GP
  219. * handler from user space, similar to bad_iret.
  220. */
  221. if (((long)regs->sp >> PGDIR_SHIFT) == ESPFIX_PGD_ENTRY &&
  222. regs->cs == __KERNEL_CS &&
  223. regs->ip == (unsigned long)native_irq_return_iret)
  224. {
  225. struct pt_regs *normal_regs = task_pt_regs(current);
  226. /* Fake a #GP(0) from userspace. */
  227. memmove(&normal_regs->ip, (void *)regs->sp, 5*8);
  228. normal_regs->orig_ax = 0; /* Missing (lost) #GP error code */
  229. regs->ip = (unsigned long)general_protection;
  230. regs->sp = (unsigned long)&normal_regs->orig_ax;
  231. return;
  232. }
  233. #endif
  234. exception_enter();
  235. /* Return not checked because double check cannot be ignored */
  236. notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_DF, SIGSEGV);
  237. tsk->thread.error_code = error_code;
  238. tsk->thread.trap_nr = X86_TRAP_DF;
  239. #ifdef CONFIG_DOUBLEFAULT
  240. df_debug(regs, error_code);
  241. #endif
  242. /*
  243. * This is always a kernel trap and never fixable (and thus must
  244. * never return).
  245. */
  246. for (;;)
  247. die(str, regs, error_code);
  248. }
  249. #endif
  250. dotraplinkage void
  251. do_general_protection(struct pt_regs *regs, long error_code)
  252. {
  253. struct task_struct *tsk;
  254. enum ctx_state prev_state;
  255. prev_state = exception_enter();
  256. conditional_sti(regs);
  257. #ifdef CONFIG_X86_32
  258. if (regs->flags & X86_VM_MASK) {
  259. local_irq_enable();
  260. handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
  261. goto exit;
  262. }
  263. #endif
  264. tsk = current;
  265. if (!user_mode(regs)) {
  266. if (fixup_exception(regs))
  267. goto exit;
  268. tsk->thread.error_code = error_code;
  269. tsk->thread.trap_nr = X86_TRAP_GP;
  270. if (notify_die(DIE_GPF, "general protection fault", regs, error_code,
  271. X86_TRAP_GP, SIGSEGV) != NOTIFY_STOP)
  272. die("general protection fault", regs, error_code);
  273. goto exit;
  274. }
  275. tsk->thread.error_code = error_code;
  276. tsk->thread.trap_nr = X86_TRAP_GP;
  277. if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
  278. printk_ratelimit()) {
  279. pr_info("%s[%d] general protection ip:%lx sp:%lx error:%lx",
  280. tsk->comm, task_pid_nr(tsk),
  281. regs->ip, regs->sp, error_code);
  282. print_vma_addr(" in ", regs->ip);
  283. pr_cont("\n");
  284. }
  285. force_sig_info(SIGSEGV, SEND_SIG_PRIV, tsk);
  286. exit:
  287. exception_exit(prev_state);
  288. }
  289. NOKPROBE_SYMBOL(do_general_protection);
  290. /* May run on IST stack. */
  291. dotraplinkage void notrace do_int3(struct pt_regs *regs, long error_code)
  292. {
  293. enum ctx_state prev_state;
  294. #ifdef CONFIG_DYNAMIC_FTRACE
  295. /*
  296. * ftrace must be first, everything else may cause a recursive crash.
  297. * See note by declaration of modifying_ftrace_code in ftrace.c
  298. */
  299. if (unlikely(atomic_read(&modifying_ftrace_code)) &&
  300. ftrace_int3_handler(regs))
  301. return;
  302. #endif
  303. if (poke_int3_handler(regs))
  304. return;
  305. prev_state = exception_enter();
  306. #ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
  307. if (kgdb_ll_trap(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
  308. SIGTRAP) == NOTIFY_STOP)
  309. goto exit;
  310. #endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
  311. #ifdef CONFIG_KPROBES
  312. if (kprobe_int3_handler(regs))
  313. goto exit;
  314. #endif
  315. if (notify_die(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
  316. SIGTRAP) == NOTIFY_STOP)
  317. goto exit;
  318. /*
  319. * Let others (NMI) know that the debug stack is in use
  320. * as we may switch to the interrupt stack.
  321. */
  322. debug_stack_usage_inc();
  323. preempt_conditional_sti(regs);
  324. do_trap(X86_TRAP_BP, SIGTRAP, "int3", regs, error_code, NULL);
  325. preempt_conditional_cli(regs);
  326. debug_stack_usage_dec();
  327. exit:
  328. exception_exit(prev_state);
  329. }
  330. NOKPROBE_SYMBOL(do_int3);
  331. #ifdef CONFIG_X86_64
  332. /*
  333. * Help handler running on IST stack to switch back to user stack
  334. * for scheduling or signal handling. The actual stack switch is done in
  335. * entry.S
  336. */
  337. asmlinkage __visible notrace struct pt_regs *sync_regs(struct pt_regs *eregs)
  338. {
  339. struct pt_regs *regs = eregs;
  340. /* Did already sync */
  341. if (eregs == (struct pt_regs *)eregs->sp)
  342. ;
  343. /* Exception from user space */
  344. else if (user_mode(eregs))
  345. regs = task_pt_regs(current);
  346. /*
  347. * Exception from kernel and interrupts are enabled. Move to
  348. * kernel process stack.
  349. */
  350. else if (eregs->flags & X86_EFLAGS_IF)
  351. regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
  352. if (eregs != regs)
  353. *regs = *eregs;
  354. return regs;
  355. }
  356. NOKPROBE_SYMBOL(sync_regs);
  357. struct bad_iret_stack {
  358. void *error_entry_ret;
  359. struct pt_regs regs;
  360. };
  361. asmlinkage __visible notrace
  362. struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s)
  363. {
  364. /*
  365. * This is called from entry_64.S early in handling a fault
  366. * caused by a bad iret to user mode. To handle the fault
  367. * correctly, we want move our stack frame to task_pt_regs
  368. * and we want to pretend that the exception came from the
  369. * iret target.
  370. */
  371. struct bad_iret_stack *new_stack =
  372. container_of(task_pt_regs(current),
  373. struct bad_iret_stack, regs);
  374. /* Copy the IRET target to the new stack. */
  375. memmove(&new_stack->regs.ip, (void *)s->regs.sp, 5*8);
  376. /* Copy the remainder of the stack from the current stack. */
  377. memmove(new_stack, s, offsetof(struct bad_iret_stack, regs.ip));
  378. BUG_ON(!user_mode_vm(&new_stack->regs));
  379. return new_stack;
  380. }
  381. NOKPROBE_SYMBOL(fixup_bad_iret);
  382. #endif
  383. /*
  384. * Our handling of the processor debug registers is non-trivial.
  385. * We do not clear them on entry and exit from the kernel. Therefore
  386. * it is possible to get a watchpoint trap here from inside the kernel.
  387. * However, the code in ./ptrace.c has ensured that the user can
  388. * only set watchpoints on userspace addresses. Therefore the in-kernel
  389. * watchpoint trap can only occur in code which is reading/writing
  390. * from user space. Such code must not hold kernel locks (since it
  391. * can equally take a page fault), therefore it is safe to call
  392. * force_sig_info even though that claims and releases locks.
  393. *
  394. * Code in ./signal.c ensures that the debug control register
  395. * is restored before we deliver any signal, and therefore that
  396. * user code runs with the correct debug control register even though
  397. * we clear it here.
  398. *
  399. * Being careful here means that we don't have to be as careful in a
  400. * lot of more complicated places (task switching can be a bit lazy
  401. * about restoring all the debug state, and ptrace doesn't have to
  402. * find every occurrence of the TF bit that could be saved away even
  403. * by user code)
  404. *
  405. * May run on IST stack.
  406. */
  407. dotraplinkage void do_debug(struct pt_regs *regs, long error_code)
  408. {
  409. struct task_struct *tsk = current;
  410. enum ctx_state prev_state;
  411. int user_icebp = 0;
  412. unsigned long dr6;
  413. int si_code;
  414. prev_state = exception_enter();
  415. get_debugreg(dr6, 6);
  416. /* Filter out all the reserved bits which are preset to 1 */
  417. dr6 &= ~DR6_RESERVED;
  418. /*
  419. * If dr6 has no reason to give us about the origin of this trap,
  420. * then it's very likely the result of an icebp/int01 trap.
  421. * User wants a sigtrap for that.
  422. */
  423. if (!dr6 && user_mode(regs))
  424. user_icebp = 1;
  425. /* Catch kmemcheck conditions first of all! */
  426. if ((dr6 & DR_STEP) && kmemcheck_trap(regs))
  427. goto exit;
  428. /* DR6 may or may not be cleared by the CPU */
  429. set_debugreg(0, 6);
  430. /*
  431. * The processor cleared BTF, so don't mark that we need it set.
  432. */
  433. clear_tsk_thread_flag(tsk, TIF_BLOCKSTEP);
  434. /* Store the virtualized DR6 value */
  435. tsk->thread.debugreg6 = dr6;
  436. #ifdef CONFIG_KPROBES
  437. if (kprobe_debug_handler(regs))
  438. goto exit;
  439. #endif
  440. if (notify_die(DIE_DEBUG, "debug", regs, (long)&dr6, error_code,
  441. SIGTRAP) == NOTIFY_STOP)
  442. goto exit;
  443. /*
  444. * Let others (NMI) know that the debug stack is in use
  445. * as we may switch to the interrupt stack.
  446. */
  447. debug_stack_usage_inc();
  448. /* It's safe to allow irq's after DR6 has been saved */
  449. preempt_conditional_sti(regs);
  450. if (regs->flags & X86_VM_MASK) {
  451. handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code,
  452. X86_TRAP_DB);
  453. preempt_conditional_cli(regs);
  454. debug_stack_usage_dec();
  455. goto exit;
  456. }
  457. /*
  458. * Single-stepping through system calls: ignore any exceptions in
  459. * kernel space, but re-enable TF when returning to user mode.
  460. *
  461. * We already checked v86 mode above, so we can check for kernel mode
  462. * by just checking the CPL of CS.
  463. */
  464. if ((dr6 & DR_STEP) && !user_mode(regs)) {
  465. tsk->thread.debugreg6 &= ~DR_STEP;
  466. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  467. regs->flags &= ~X86_EFLAGS_TF;
  468. }
  469. si_code = get_si_code(tsk->thread.debugreg6);
  470. if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp)
  471. send_sigtrap(tsk, regs, error_code, si_code);
  472. preempt_conditional_cli(regs);
  473. debug_stack_usage_dec();
  474. exit:
  475. exception_exit(prev_state);
  476. }
  477. NOKPROBE_SYMBOL(do_debug);
  478. /*
  479. * Note that we play around with the 'TS' bit in an attempt to get
  480. * the correct behaviour even in the presence of the asynchronous
  481. * IRQ13 behaviour
  482. */
  483. static void math_error(struct pt_regs *regs, int error_code, int trapnr)
  484. {
  485. struct task_struct *task = current;
  486. siginfo_t info;
  487. unsigned short err;
  488. char *str = (trapnr == X86_TRAP_MF) ? "fpu exception" :
  489. "simd exception";
  490. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, SIGFPE) == NOTIFY_STOP)
  491. return;
  492. conditional_sti(regs);
  493. if (!user_mode_vm(regs))
  494. {
  495. if (!fixup_exception(regs)) {
  496. task->thread.error_code = error_code;
  497. task->thread.trap_nr = trapnr;
  498. die(str, regs, error_code);
  499. }
  500. return;
  501. }
  502. /*
  503. * Save the info for the exception handler and clear the error.
  504. */
  505. save_init_fpu(task);
  506. task->thread.trap_nr = trapnr;
  507. task->thread.error_code = error_code;
  508. info.si_signo = SIGFPE;
  509. info.si_errno = 0;
  510. info.si_addr = (void __user *)uprobe_get_trap_addr(regs);
  511. if (trapnr == X86_TRAP_MF) {
  512. unsigned short cwd, swd;
  513. /*
  514. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  515. * status. 0x3f is the exception bits in these regs, 0x200 is the
  516. * C1 reg you need in case of a stack fault, 0x040 is the stack
  517. * fault bit. We should only be taking one exception at a time,
  518. * so if this combination doesn't produce any single exception,
  519. * then we have a bad program that isn't synchronizing its FPU usage
  520. * and it will suffer the consequences since we won't be able to
  521. * fully reproduce the context of the exception
  522. */
  523. cwd = get_fpu_cwd(task);
  524. swd = get_fpu_swd(task);
  525. err = swd & ~cwd;
  526. } else {
  527. /*
  528. * The SIMD FPU exceptions are handled a little differently, as there
  529. * is only a single status/control register. Thus, to determine which
  530. * unmasked exception was caught we must mask the exception mask bits
  531. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  532. */
  533. unsigned short mxcsr = get_fpu_mxcsr(task);
  534. err = ~(mxcsr >> 7) & mxcsr;
  535. }
  536. if (err & 0x001) { /* Invalid op */
  537. /*
  538. * swd & 0x240 == 0x040: Stack Underflow
  539. * swd & 0x240 == 0x240: Stack Overflow
  540. * User must clear the SF bit (0x40) if set
  541. */
  542. info.si_code = FPE_FLTINV;
  543. } else if (err & 0x004) { /* Divide by Zero */
  544. info.si_code = FPE_FLTDIV;
  545. } else if (err & 0x008) { /* Overflow */
  546. info.si_code = FPE_FLTOVF;
  547. } else if (err & 0x012) { /* Denormal, Underflow */
  548. info.si_code = FPE_FLTUND;
  549. } else if (err & 0x020) { /* Precision */
  550. info.si_code = FPE_FLTRES;
  551. } else {
  552. /*
  553. * If we're using IRQ 13, or supposedly even some trap
  554. * X86_TRAP_MF implementations, it's possible
  555. * we get a spurious trap, which is not an error.
  556. */
  557. return;
  558. }
  559. force_sig_info(SIGFPE, &info, task);
  560. }
  561. dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
  562. {
  563. enum ctx_state prev_state;
  564. prev_state = exception_enter();
  565. math_error(regs, error_code, X86_TRAP_MF);
  566. exception_exit(prev_state);
  567. }
  568. dotraplinkage void
  569. do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
  570. {
  571. enum ctx_state prev_state;
  572. prev_state = exception_enter();
  573. math_error(regs, error_code, X86_TRAP_XF);
  574. exception_exit(prev_state);
  575. }
  576. dotraplinkage void
  577. do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
  578. {
  579. conditional_sti(regs);
  580. #if 0
  581. /* No need to warn about this any longer. */
  582. pr_info("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
  583. #endif
  584. }
  585. asmlinkage __visible void __attribute__((weak)) smp_thermal_interrupt(void)
  586. {
  587. }
  588. asmlinkage __visible void __attribute__((weak)) smp_threshold_interrupt(void)
  589. {
  590. }
  591. /*
  592. * 'math_state_restore()' saves the current math information in the
  593. * old math state array, and gets the new ones from the current task
  594. *
  595. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  596. * Don't touch unless you *really* know how it works.
  597. *
  598. * Must be called with kernel preemption disabled (eg with local
  599. * local interrupts as in the case of do_device_not_available).
  600. */
  601. void math_state_restore(void)
  602. {
  603. struct task_struct *tsk = current;
  604. if (!tsk_used_math(tsk)) {
  605. local_irq_enable();
  606. /*
  607. * does a slab alloc which can sleep
  608. */
  609. if (init_fpu(tsk)) {
  610. /*
  611. * ran out of memory!
  612. */
  613. do_group_exit(SIGKILL);
  614. return;
  615. }
  616. local_irq_disable();
  617. }
  618. __thread_fpu_begin(tsk);
  619. /*
  620. * Paranoid restore. send a SIGSEGV if we fail to restore the state.
  621. */
  622. if (unlikely(restore_fpu_checking(tsk))) {
  623. drop_init_fpu(tsk);
  624. force_sig_info(SIGSEGV, SEND_SIG_PRIV, tsk);
  625. return;
  626. }
  627. tsk->thread.fpu_counter++;
  628. }
  629. EXPORT_SYMBOL_GPL(math_state_restore);
  630. dotraplinkage void
  631. do_device_not_available(struct pt_regs *regs, long error_code)
  632. {
  633. enum ctx_state prev_state;
  634. prev_state = exception_enter();
  635. BUG_ON(use_eager_fpu());
  636. #ifdef CONFIG_MATH_EMULATION
  637. if (read_cr0() & X86_CR0_EM) {
  638. struct math_emu_info info = { };
  639. conditional_sti(regs);
  640. info.regs = regs;
  641. math_emulate(&info);
  642. exception_exit(prev_state);
  643. return;
  644. }
  645. #endif
  646. math_state_restore(); /* interrupts still off */
  647. #ifdef CONFIG_X86_32
  648. conditional_sti(regs);
  649. #endif
  650. exception_exit(prev_state);
  651. }
  652. NOKPROBE_SYMBOL(do_device_not_available);
  653. #ifdef CONFIG_X86_32
  654. dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
  655. {
  656. siginfo_t info;
  657. enum ctx_state prev_state;
  658. prev_state = exception_enter();
  659. local_irq_enable();
  660. info.si_signo = SIGILL;
  661. info.si_errno = 0;
  662. info.si_code = ILL_BADSTK;
  663. info.si_addr = NULL;
  664. if (notify_die(DIE_TRAP, "iret exception", regs, error_code,
  665. X86_TRAP_IRET, SIGILL) != NOTIFY_STOP) {
  666. do_trap(X86_TRAP_IRET, SIGILL, "iret exception", regs, error_code,
  667. &info);
  668. }
  669. exception_exit(prev_state);
  670. }
  671. #endif
  672. /* Set of traps needed for early debugging. */
  673. void __init early_trap_init(void)
  674. {
  675. set_intr_gate_ist(X86_TRAP_DB, &debug, DEBUG_STACK);
  676. /* int3 can be called from all */
  677. set_system_intr_gate_ist(X86_TRAP_BP, &int3, DEBUG_STACK);
  678. #ifdef CONFIG_X86_32
  679. set_intr_gate(X86_TRAP_PF, page_fault);
  680. #endif
  681. load_idt(&idt_descr);
  682. }
  683. void __init early_trap_pf_init(void)
  684. {
  685. #ifdef CONFIG_X86_64
  686. set_intr_gate(X86_TRAP_PF, page_fault);
  687. #endif
  688. }
  689. void __init trap_init(void)
  690. {
  691. int i;
  692. #ifdef CONFIG_EISA
  693. void __iomem *p = early_ioremap(0x0FFFD9, 4);
  694. if (readl(p) == 'E' + ('I'<<8) + ('S'<<16) + ('A'<<24))
  695. EISA_bus = 1;
  696. early_iounmap(p, 4);
  697. #endif
  698. set_intr_gate(X86_TRAP_DE, divide_error);
  699. set_intr_gate_ist(X86_TRAP_NMI, &nmi, NMI_STACK);
  700. /* int4 can be called from all */
  701. set_system_intr_gate(X86_TRAP_OF, &overflow);
  702. set_intr_gate(X86_TRAP_BR, bounds);
  703. set_intr_gate(X86_TRAP_UD, invalid_op);
  704. set_intr_gate(X86_TRAP_NM, device_not_available);
  705. #ifdef CONFIG_X86_32
  706. set_task_gate(X86_TRAP_DF, GDT_ENTRY_DOUBLEFAULT_TSS);
  707. #else
  708. set_intr_gate_ist(X86_TRAP_DF, &double_fault, DOUBLEFAULT_STACK);
  709. #endif
  710. set_intr_gate(X86_TRAP_OLD_MF, coprocessor_segment_overrun);
  711. set_intr_gate(X86_TRAP_TS, invalid_TSS);
  712. set_intr_gate(X86_TRAP_NP, segment_not_present);
  713. set_intr_gate(X86_TRAP_SS, stack_segment);
  714. set_intr_gate(X86_TRAP_GP, general_protection);
  715. set_intr_gate(X86_TRAP_SPURIOUS, spurious_interrupt_bug);
  716. set_intr_gate(X86_TRAP_MF, coprocessor_error);
  717. set_intr_gate(X86_TRAP_AC, alignment_check);
  718. #ifdef CONFIG_X86_MCE
  719. set_intr_gate_ist(X86_TRAP_MC, &machine_check, MCE_STACK);
  720. #endif
  721. set_intr_gate(X86_TRAP_XF, simd_coprocessor_error);
  722. /* Reserve all the builtin and the syscall vector: */
  723. for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
  724. set_bit(i, used_vectors);
  725. #ifdef CONFIG_IA32_EMULATION
  726. set_system_intr_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
  727. set_bit(IA32_SYSCALL_VECTOR, used_vectors);
  728. #endif
  729. #ifdef CONFIG_X86_32
  730. set_system_trap_gate(SYSCALL_VECTOR, &system_call);
  731. set_bit(SYSCALL_VECTOR, used_vectors);
  732. #endif
  733. /*
  734. * Set the IDT descriptor to a fixed read-only location, so that the
  735. * "sidt" instruction will not leak the location of the kernel, and
  736. * to defend the IDT against arbitrary memory write vulnerabilities.
  737. * It will be reloaded in cpu_init() */
  738. __set_fixmap(FIX_RO_IDT, __pa_symbol(idt_table), PAGE_KERNEL_RO);
  739. idt_descr.address = fix_to_virt(FIX_RO_IDT);
  740. /*
  741. * Should be a barrier for any external CPU state:
  742. */
  743. cpu_init();
  744. x86_init.irqs.trap_init();
  745. #ifdef CONFIG_X86_64
  746. memcpy(&debug_idt_table, &idt_table, IDT_ENTRIES * 16);
  747. set_nmi_gate(X86_TRAP_DB, &debug);
  748. set_nmi_gate(X86_TRAP_BP, &int3);
  749. #endif
  750. }