traps.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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/export.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/sched/task_stack.h>
  30. #include <linux/timer.h>
  31. #include <linux/init.h>
  32. #include <linux/bug.h>
  33. #include <linux/nmi.h>
  34. #include <linux/mm.h>
  35. #include <linux/smp.h>
  36. #include <linux/io.h>
  37. #if defined(CONFIG_EDAC)
  38. #include <linux/edac.h>
  39. #endif
  40. #include <asm/stacktrace.h>
  41. #include <asm/processor.h>
  42. #include <asm/debugreg.h>
  43. #include <linux/atomic.h>
  44. #include <asm/text-patching.h>
  45. #include <asm/ftrace.h>
  46. #include <asm/traps.h>
  47. #include <asm/desc.h>
  48. #include <asm/fpu/internal.h>
  49. #include <asm/cpu_entry_area.h>
  50. #include <asm/mce.h>
  51. #include <asm/fixmap.h>
  52. #include <asm/mach_traps.h>
  53. #include <asm/alternative.h>
  54. #include <asm/fpu/xstate.h>
  55. #include <asm/trace/mpx.h>
  56. #include <asm/mpx.h>
  57. #include <asm/vm86.h>
  58. #include <asm/umip.h>
  59. #ifdef CONFIG_X86_64
  60. #include <asm/x86_init.h>
  61. #include <asm/pgalloc.h>
  62. #include <asm/proto.h>
  63. #else
  64. #include <asm/processor-flags.h>
  65. #include <asm/setup.h>
  66. #include <asm/proto.h>
  67. #endif
  68. DECLARE_BITMAP(system_vectors, NR_VECTORS);
  69. static inline void cond_local_irq_enable(struct pt_regs *regs)
  70. {
  71. if (regs->flags & X86_EFLAGS_IF)
  72. local_irq_enable();
  73. }
  74. static inline void cond_local_irq_disable(struct pt_regs *regs)
  75. {
  76. if (regs->flags & X86_EFLAGS_IF)
  77. local_irq_disable();
  78. }
  79. /*
  80. * In IST context, we explicitly disable preemption. This serves two
  81. * purposes: it makes it much less likely that we would accidentally
  82. * schedule in IST context and it will force a warning if we somehow
  83. * manage to schedule by accident.
  84. */
  85. void ist_enter(struct pt_regs *regs)
  86. {
  87. if (user_mode(regs)) {
  88. RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
  89. } else {
  90. /*
  91. * We might have interrupted pretty much anything. In
  92. * fact, if we're a machine check, we can even interrupt
  93. * NMI processing. We don't want in_nmi() to return true,
  94. * but we need to notify RCU.
  95. */
  96. rcu_nmi_enter();
  97. }
  98. preempt_disable();
  99. /* This code is a bit fragile. Test it. */
  100. RCU_LOCKDEP_WARN(!rcu_is_watching(), "ist_enter didn't work");
  101. }
  102. void ist_exit(struct pt_regs *regs)
  103. {
  104. preempt_enable_no_resched();
  105. if (!user_mode(regs))
  106. rcu_nmi_exit();
  107. }
  108. /**
  109. * ist_begin_non_atomic() - begin a non-atomic section in an IST exception
  110. * @regs: regs passed to the IST exception handler
  111. *
  112. * IST exception handlers normally cannot schedule. As a special
  113. * exception, if the exception interrupted userspace code (i.e.
  114. * user_mode(regs) would return true) and the exception was not
  115. * a double fault, it can be safe to schedule. ist_begin_non_atomic()
  116. * begins a non-atomic section within an ist_enter()/ist_exit() region.
  117. * Callers are responsible for enabling interrupts themselves inside
  118. * the non-atomic section, and callers must call ist_end_non_atomic()
  119. * before ist_exit().
  120. */
  121. void ist_begin_non_atomic(struct pt_regs *regs)
  122. {
  123. BUG_ON(!user_mode(regs));
  124. /*
  125. * Sanity check: we need to be on the normal thread stack. This
  126. * will catch asm bugs and any attempt to use ist_preempt_enable
  127. * from double_fault.
  128. */
  129. BUG_ON(!on_thread_stack());
  130. preempt_enable_no_resched();
  131. }
  132. /**
  133. * ist_end_non_atomic() - begin a non-atomic section in an IST exception
  134. *
  135. * Ends a non-atomic section started with ist_begin_non_atomic().
  136. */
  137. void ist_end_non_atomic(void)
  138. {
  139. preempt_disable();
  140. }
  141. int is_valid_bugaddr(unsigned long addr)
  142. {
  143. unsigned short ud;
  144. if (addr < TASK_SIZE_MAX)
  145. return 0;
  146. if (probe_kernel_address((unsigned short *)addr, ud))
  147. return 0;
  148. return ud == INSN_UD0 || ud == INSN_UD2;
  149. }
  150. int fixup_bug(struct pt_regs *regs, int trapnr)
  151. {
  152. if (trapnr != X86_TRAP_UD)
  153. return 0;
  154. switch (report_bug(regs->ip, regs)) {
  155. case BUG_TRAP_TYPE_NONE:
  156. case BUG_TRAP_TYPE_BUG:
  157. break;
  158. case BUG_TRAP_TYPE_WARN:
  159. regs->ip += LEN_UD2;
  160. return 1;
  161. }
  162. return 0;
  163. }
  164. static nokprobe_inline int
  165. do_trap_no_signal(struct task_struct *tsk, int trapnr, char *str,
  166. struct pt_regs *regs, long error_code)
  167. {
  168. if (v8086_mode(regs)) {
  169. /*
  170. * Traps 0, 1, 3, 4, and 5 should be forwarded to vm86.
  171. * On nmi (interrupt 2), do_trap should not be called.
  172. */
  173. if (trapnr < X86_TRAP_UD) {
  174. if (!handle_vm86_trap((struct kernel_vm86_regs *) regs,
  175. error_code, trapnr))
  176. return 0;
  177. }
  178. return -1;
  179. }
  180. if (!user_mode(regs)) {
  181. if (fixup_exception(regs, trapnr))
  182. return 0;
  183. tsk->thread.error_code = error_code;
  184. tsk->thread.trap_nr = trapnr;
  185. die(str, regs, error_code);
  186. }
  187. return -1;
  188. }
  189. static siginfo_t *fill_trap_info(struct pt_regs *regs, int signr, int trapnr,
  190. siginfo_t *info)
  191. {
  192. unsigned long siaddr;
  193. int sicode;
  194. switch (trapnr) {
  195. default:
  196. return SEND_SIG_PRIV;
  197. case X86_TRAP_DE:
  198. sicode = FPE_INTDIV;
  199. siaddr = uprobe_get_trap_addr(regs);
  200. break;
  201. case X86_TRAP_UD:
  202. sicode = ILL_ILLOPN;
  203. siaddr = uprobe_get_trap_addr(regs);
  204. break;
  205. case X86_TRAP_AC:
  206. sicode = BUS_ADRALN;
  207. siaddr = 0;
  208. break;
  209. }
  210. info->si_signo = signr;
  211. info->si_errno = 0;
  212. info->si_code = sicode;
  213. info->si_addr = (void __user *)siaddr;
  214. return info;
  215. }
  216. static void
  217. do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
  218. long error_code, siginfo_t *info)
  219. {
  220. struct task_struct *tsk = current;
  221. if (!do_trap_no_signal(tsk, trapnr, str, regs, error_code))
  222. return;
  223. /*
  224. * We want error_code and trap_nr set for userspace faults and
  225. * kernelspace faults which result in die(), but not
  226. * kernelspace faults which are fixed up. die() gives the
  227. * process no chance to handle the signal and notice the
  228. * kernel fault information, so that won't result in polluting
  229. * the information about previously queued, but not yet
  230. * delivered, faults. See also do_general_protection below.
  231. */
  232. tsk->thread.error_code = error_code;
  233. tsk->thread.trap_nr = trapnr;
  234. if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
  235. printk_ratelimit()) {
  236. pr_info("%s[%d] trap %s ip:%lx sp:%lx error:%lx",
  237. tsk->comm, tsk->pid, str,
  238. regs->ip, regs->sp, error_code);
  239. print_vma_addr(KERN_CONT " in ", regs->ip);
  240. pr_cont("\n");
  241. }
  242. force_sig_info(signr, info ?: SEND_SIG_PRIV, tsk);
  243. }
  244. NOKPROBE_SYMBOL(do_trap);
  245. static void do_error_trap(struct pt_regs *regs, long error_code, char *str,
  246. unsigned long trapnr, int signr)
  247. {
  248. siginfo_t info;
  249. RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
  250. /*
  251. * WARN*()s end up here; fix them up before we call the
  252. * notifier chain.
  253. */
  254. if (!user_mode(regs) && fixup_bug(regs, trapnr))
  255. return;
  256. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) !=
  257. NOTIFY_STOP) {
  258. cond_local_irq_enable(regs);
  259. clear_siginfo(&info);
  260. do_trap(trapnr, signr, str, regs, error_code,
  261. fill_trap_info(regs, signr, trapnr, &info));
  262. }
  263. }
  264. #define DO_ERROR(trapnr, signr, str, name) \
  265. dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
  266. { \
  267. do_error_trap(regs, error_code, str, trapnr, signr); \
  268. }
  269. DO_ERROR(X86_TRAP_DE, SIGFPE, "divide error", divide_error)
  270. DO_ERROR(X86_TRAP_OF, SIGSEGV, "overflow", overflow)
  271. DO_ERROR(X86_TRAP_UD, SIGILL, "invalid opcode", invalid_op)
  272. DO_ERROR(X86_TRAP_OLD_MF, SIGFPE, "coprocessor segment overrun",coprocessor_segment_overrun)
  273. DO_ERROR(X86_TRAP_TS, SIGSEGV, "invalid TSS", invalid_TSS)
  274. DO_ERROR(X86_TRAP_NP, SIGBUS, "segment not present", segment_not_present)
  275. DO_ERROR(X86_TRAP_SS, SIGBUS, "stack segment", stack_segment)
  276. DO_ERROR(X86_TRAP_AC, SIGBUS, "alignment check", alignment_check)
  277. #ifdef CONFIG_VMAP_STACK
  278. __visible void __noreturn handle_stack_overflow(const char *message,
  279. struct pt_regs *regs,
  280. unsigned long fault_address)
  281. {
  282. printk(KERN_EMERG "BUG: stack guard page was hit at %p (stack is %p..%p)\n",
  283. (void *)fault_address, current->stack,
  284. (char *)current->stack + THREAD_SIZE - 1);
  285. die(message, regs, 0);
  286. /* Be absolutely certain we don't return. */
  287. panic(message);
  288. }
  289. #endif
  290. #ifdef CONFIG_X86_64
  291. /* Runs on IST stack */
  292. dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code)
  293. {
  294. static const char str[] = "double fault";
  295. struct task_struct *tsk = current;
  296. #ifdef CONFIG_VMAP_STACK
  297. unsigned long cr2;
  298. #endif
  299. #ifdef CONFIG_X86_ESPFIX64
  300. extern unsigned char native_irq_return_iret[];
  301. /*
  302. * If IRET takes a non-IST fault on the espfix64 stack, then we
  303. * end up promoting it to a doublefault. In that case, take
  304. * advantage of the fact that we're not using the normal (TSS.sp0)
  305. * stack right now. We can write a fake #GP(0) frame at TSS.sp0
  306. * and then modify our own IRET frame so that, when we return,
  307. * we land directly at the #GP(0) vector with the stack already
  308. * set up according to its expectations.
  309. *
  310. * The net result is that our #GP handler will think that we
  311. * entered from usermode with the bad user context.
  312. *
  313. * No need for ist_enter here because we don't use RCU.
  314. */
  315. if (((long)regs->sp >> P4D_SHIFT) == ESPFIX_PGD_ENTRY &&
  316. regs->cs == __KERNEL_CS &&
  317. regs->ip == (unsigned long)native_irq_return_iret)
  318. {
  319. struct pt_regs *gpregs = (struct pt_regs *)this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1;
  320. /*
  321. * regs->sp points to the failing IRET frame on the
  322. * ESPFIX64 stack. Copy it to the entry stack. This fills
  323. * in gpregs->ss through gpregs->ip.
  324. *
  325. */
  326. memmove(&gpregs->ip, (void *)regs->sp, 5*8);
  327. gpregs->orig_ax = 0; /* Missing (lost) #GP error code */
  328. /*
  329. * Adjust our frame so that we return straight to the #GP
  330. * vector with the expected RSP value. This is safe because
  331. * we won't enable interupts or schedule before we invoke
  332. * general_protection, so nothing will clobber the stack
  333. * frame we just set up.
  334. */
  335. regs->ip = (unsigned long)general_protection;
  336. regs->sp = (unsigned long)&gpregs->orig_ax;
  337. return;
  338. }
  339. #endif
  340. ist_enter(regs);
  341. notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_DF, SIGSEGV);
  342. tsk->thread.error_code = error_code;
  343. tsk->thread.trap_nr = X86_TRAP_DF;
  344. #ifdef CONFIG_VMAP_STACK
  345. /*
  346. * If we overflow the stack into a guard page, the CPU will fail
  347. * to deliver #PF and will send #DF instead. Similarly, if we
  348. * take any non-IST exception while too close to the bottom of
  349. * the stack, the processor will get a page fault while
  350. * delivering the exception and will generate a double fault.
  351. *
  352. * According to the SDM (footnote in 6.15 under "Interrupt 14 -
  353. * Page-Fault Exception (#PF):
  354. *
  355. * Processors update CR2 whenever a page fault is detected. If a
  356. * second page fault occurs while an earlier page fault is being
  357. * delivered, the faulting linear address of the second fault will
  358. * overwrite the contents of CR2 (replacing the previous
  359. * address). These updates to CR2 occur even if the page fault
  360. * results in a double fault or occurs during the delivery of a
  361. * double fault.
  362. *
  363. * The logic below has a small possibility of incorrectly diagnosing
  364. * some errors as stack overflows. For example, if the IDT or GDT
  365. * gets corrupted such that #GP delivery fails due to a bad descriptor
  366. * causing #GP and we hit this condition while CR2 coincidentally
  367. * points to the stack guard page, we'll think we overflowed the
  368. * stack. Given that we're going to panic one way or another
  369. * if this happens, this isn't necessarily worth fixing.
  370. *
  371. * If necessary, we could improve the test by only diagnosing
  372. * a stack overflow if the saved RSP points within 47 bytes of
  373. * the bottom of the stack: if RSP == tsk_stack + 48 and we
  374. * take an exception, the stack is already aligned and there
  375. * will be enough room SS, RSP, RFLAGS, CS, RIP, and a
  376. * possible error code, so a stack overflow would *not* double
  377. * fault. With any less space left, exception delivery could
  378. * fail, and, as a practical matter, we've overflowed the
  379. * stack even if the actual trigger for the double fault was
  380. * something else.
  381. */
  382. cr2 = read_cr2();
  383. if ((unsigned long)task_stack_page(tsk) - 1 - cr2 < PAGE_SIZE)
  384. handle_stack_overflow("kernel stack overflow (double-fault)", regs, cr2);
  385. #endif
  386. #ifdef CONFIG_DOUBLEFAULT
  387. df_debug(regs, error_code);
  388. #endif
  389. /*
  390. * This is always a kernel trap and never fixable (and thus must
  391. * never return).
  392. */
  393. for (;;)
  394. die(str, regs, error_code);
  395. }
  396. #endif
  397. dotraplinkage void do_bounds(struct pt_regs *regs, long error_code)
  398. {
  399. const struct mpx_bndcsr *bndcsr;
  400. siginfo_t *info;
  401. RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
  402. if (notify_die(DIE_TRAP, "bounds", regs, error_code,
  403. X86_TRAP_BR, SIGSEGV) == NOTIFY_STOP)
  404. return;
  405. cond_local_irq_enable(regs);
  406. if (!user_mode(regs))
  407. die("bounds", regs, error_code);
  408. if (!cpu_feature_enabled(X86_FEATURE_MPX)) {
  409. /* The exception is not from Intel MPX */
  410. goto exit_trap;
  411. }
  412. /*
  413. * We need to look at BNDSTATUS to resolve this exception.
  414. * A NULL here might mean that it is in its 'init state',
  415. * which is all zeros which indicates MPX was not
  416. * responsible for the exception.
  417. */
  418. bndcsr = get_xsave_field_ptr(XFEATURE_MASK_BNDCSR);
  419. if (!bndcsr)
  420. goto exit_trap;
  421. trace_bounds_exception_mpx(bndcsr);
  422. /*
  423. * The error code field of the BNDSTATUS register communicates status
  424. * information of a bound range exception #BR or operation involving
  425. * bound directory.
  426. */
  427. switch (bndcsr->bndstatus & MPX_BNDSTA_ERROR_CODE) {
  428. case 2: /* Bound directory has invalid entry. */
  429. if (mpx_handle_bd_fault())
  430. goto exit_trap;
  431. break; /* Success, it was handled */
  432. case 1: /* Bound violation. */
  433. info = mpx_generate_siginfo(regs);
  434. if (IS_ERR(info)) {
  435. /*
  436. * We failed to decode the MPX instruction. Act as if
  437. * the exception was not caused by MPX.
  438. */
  439. goto exit_trap;
  440. }
  441. /*
  442. * Success, we decoded the instruction and retrieved
  443. * an 'info' containing the address being accessed
  444. * which caused the exception. This information
  445. * allows and application to possibly handle the
  446. * #BR exception itself.
  447. */
  448. do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, error_code, info);
  449. kfree(info);
  450. break;
  451. case 0: /* No exception caused by Intel MPX operations. */
  452. goto exit_trap;
  453. default:
  454. die("bounds", regs, error_code);
  455. }
  456. return;
  457. exit_trap:
  458. /*
  459. * This path out is for all the cases where we could not
  460. * handle the exception in some way (like allocating a
  461. * table or telling userspace about it. We will also end
  462. * up here if the kernel has MPX turned off at compile
  463. * time..
  464. */
  465. do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, error_code, NULL);
  466. }
  467. dotraplinkage void
  468. do_general_protection(struct pt_regs *regs, long error_code)
  469. {
  470. struct task_struct *tsk;
  471. RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
  472. cond_local_irq_enable(regs);
  473. if (static_cpu_has(X86_FEATURE_UMIP)) {
  474. if (user_mode(regs) && fixup_umip_exception(regs))
  475. return;
  476. }
  477. if (v8086_mode(regs)) {
  478. local_irq_enable();
  479. handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
  480. return;
  481. }
  482. tsk = current;
  483. if (!user_mode(regs)) {
  484. if (fixup_exception(regs, X86_TRAP_GP))
  485. return;
  486. tsk->thread.error_code = error_code;
  487. tsk->thread.trap_nr = X86_TRAP_GP;
  488. if (notify_die(DIE_GPF, "general protection fault", regs, error_code,
  489. X86_TRAP_GP, SIGSEGV) != NOTIFY_STOP)
  490. die("general protection fault", regs, error_code);
  491. return;
  492. }
  493. tsk->thread.error_code = error_code;
  494. tsk->thread.trap_nr = X86_TRAP_GP;
  495. if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
  496. printk_ratelimit()) {
  497. pr_info("%s[%d] general protection ip:%lx sp:%lx error:%lx",
  498. tsk->comm, task_pid_nr(tsk),
  499. regs->ip, regs->sp, error_code);
  500. print_vma_addr(KERN_CONT " in ", regs->ip);
  501. pr_cont("\n");
  502. }
  503. force_sig_info(SIGSEGV, SEND_SIG_PRIV, tsk);
  504. }
  505. NOKPROBE_SYMBOL(do_general_protection);
  506. dotraplinkage void notrace do_int3(struct pt_regs *regs, long error_code)
  507. {
  508. #ifdef CONFIG_DYNAMIC_FTRACE
  509. /*
  510. * ftrace must be first, everything else may cause a recursive crash.
  511. * See note by declaration of modifying_ftrace_code in ftrace.c
  512. */
  513. if (unlikely(atomic_read(&modifying_ftrace_code)) &&
  514. ftrace_int3_handler(regs))
  515. return;
  516. #endif
  517. if (poke_int3_handler(regs))
  518. return;
  519. /*
  520. * Use ist_enter despite the fact that we don't use an IST stack.
  521. * We can be called from a kprobe in non-CONTEXT_KERNEL kernel
  522. * mode or even during context tracking state changes.
  523. *
  524. * This means that we can't schedule. That's okay.
  525. */
  526. ist_enter(regs);
  527. RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
  528. #ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
  529. if (kgdb_ll_trap(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
  530. SIGTRAP) == NOTIFY_STOP)
  531. goto exit;
  532. #endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
  533. #ifdef CONFIG_KPROBES
  534. if (kprobe_int3_handler(regs))
  535. goto exit;
  536. #endif
  537. if (notify_die(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
  538. SIGTRAP) == NOTIFY_STOP)
  539. goto exit;
  540. cond_local_irq_enable(regs);
  541. do_trap(X86_TRAP_BP, SIGTRAP, "int3", regs, error_code, NULL);
  542. cond_local_irq_disable(regs);
  543. exit:
  544. ist_exit(regs);
  545. }
  546. NOKPROBE_SYMBOL(do_int3);
  547. #ifdef CONFIG_X86_64
  548. /*
  549. * Help handler running on a per-cpu (IST or entry trampoline) stack
  550. * to switch to the normal thread stack if the interrupted code was in
  551. * user mode. The actual stack switch is done in entry_64.S
  552. */
  553. asmlinkage __visible notrace struct pt_regs *sync_regs(struct pt_regs *eregs)
  554. {
  555. struct pt_regs *regs = (struct pt_regs *)this_cpu_read(cpu_current_top_of_stack) - 1;
  556. if (regs != eregs)
  557. *regs = *eregs;
  558. return regs;
  559. }
  560. NOKPROBE_SYMBOL(sync_regs);
  561. struct bad_iret_stack {
  562. void *error_entry_ret;
  563. struct pt_regs regs;
  564. };
  565. asmlinkage __visible notrace
  566. struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s)
  567. {
  568. /*
  569. * This is called from entry_64.S early in handling a fault
  570. * caused by a bad iret to user mode. To handle the fault
  571. * correctly, we want to move our stack frame to where it would
  572. * be had we entered directly on the entry stack (rather than
  573. * just below the IRET frame) and we want to pretend that the
  574. * exception came from the IRET target.
  575. */
  576. struct bad_iret_stack *new_stack =
  577. (struct bad_iret_stack *)this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1;
  578. /* Copy the IRET target to the new stack. */
  579. memmove(&new_stack->regs.ip, (void *)s->regs.sp, 5*8);
  580. /* Copy the remainder of the stack from the current stack. */
  581. memmove(new_stack, s, offsetof(struct bad_iret_stack, regs.ip));
  582. BUG_ON(!user_mode(&new_stack->regs));
  583. return new_stack;
  584. }
  585. NOKPROBE_SYMBOL(fixup_bad_iret);
  586. #endif
  587. static bool is_sysenter_singlestep(struct pt_regs *regs)
  588. {
  589. /*
  590. * We don't try for precision here. If we're anywhere in the region of
  591. * code that can be single-stepped in the SYSENTER entry path, then
  592. * assume that this is a useless single-step trap due to SYSENTER
  593. * being invoked with TF set. (We don't know in advance exactly
  594. * which instructions will be hit because BTF could plausibly
  595. * be set.)
  596. */
  597. #ifdef CONFIG_X86_32
  598. return (regs->ip - (unsigned long)__begin_SYSENTER_singlestep_region) <
  599. (unsigned long)__end_SYSENTER_singlestep_region -
  600. (unsigned long)__begin_SYSENTER_singlestep_region;
  601. #elif defined(CONFIG_IA32_EMULATION)
  602. return (regs->ip - (unsigned long)entry_SYSENTER_compat) <
  603. (unsigned long)__end_entry_SYSENTER_compat -
  604. (unsigned long)entry_SYSENTER_compat;
  605. #else
  606. return false;
  607. #endif
  608. }
  609. /*
  610. * Our handling of the processor debug registers is non-trivial.
  611. * We do not clear them on entry and exit from the kernel. Therefore
  612. * it is possible to get a watchpoint trap here from inside the kernel.
  613. * However, the code in ./ptrace.c has ensured that the user can
  614. * only set watchpoints on userspace addresses. Therefore the in-kernel
  615. * watchpoint trap can only occur in code which is reading/writing
  616. * from user space. Such code must not hold kernel locks (since it
  617. * can equally take a page fault), therefore it is safe to call
  618. * force_sig_info even though that claims and releases locks.
  619. *
  620. * Code in ./signal.c ensures that the debug control register
  621. * is restored before we deliver any signal, and therefore that
  622. * user code runs with the correct debug control register even though
  623. * we clear it here.
  624. *
  625. * Being careful here means that we don't have to be as careful in a
  626. * lot of more complicated places (task switching can be a bit lazy
  627. * about restoring all the debug state, and ptrace doesn't have to
  628. * find every occurrence of the TF bit that could be saved away even
  629. * by user code)
  630. *
  631. * May run on IST stack.
  632. */
  633. dotraplinkage void do_debug(struct pt_regs *regs, long error_code)
  634. {
  635. struct task_struct *tsk = current;
  636. int user_icebp = 0;
  637. unsigned long dr6;
  638. int si_code;
  639. ist_enter(regs);
  640. get_debugreg(dr6, 6);
  641. /*
  642. * The Intel SDM says:
  643. *
  644. * Certain debug exceptions may clear bits 0-3. The remaining
  645. * contents of the DR6 register are never cleared by the
  646. * processor. To avoid confusion in identifying debug
  647. * exceptions, debug handlers should clear the register before
  648. * returning to the interrupted task.
  649. *
  650. * Keep it simple: clear DR6 immediately.
  651. */
  652. set_debugreg(0, 6);
  653. /* Filter out all the reserved bits which are preset to 1 */
  654. dr6 &= ~DR6_RESERVED;
  655. /*
  656. * The SDM says "The processor clears the BTF flag when it
  657. * generates a debug exception." Clear TIF_BLOCKSTEP to keep
  658. * TIF_BLOCKSTEP in sync with the hardware BTF flag.
  659. */
  660. clear_tsk_thread_flag(tsk, TIF_BLOCKSTEP);
  661. if (unlikely(!user_mode(regs) && (dr6 & DR_STEP) &&
  662. is_sysenter_singlestep(regs))) {
  663. dr6 &= ~DR_STEP;
  664. if (!dr6)
  665. goto exit;
  666. /*
  667. * else we might have gotten a single-step trap and hit a
  668. * watchpoint at the same time, in which case we should fall
  669. * through and handle the watchpoint.
  670. */
  671. }
  672. /*
  673. * If dr6 has no reason to give us about the origin of this trap,
  674. * then it's very likely the result of an icebp/int01 trap.
  675. * User wants a sigtrap for that.
  676. */
  677. if (!dr6 && user_mode(regs))
  678. user_icebp = 1;
  679. /* Store the virtualized DR6 value */
  680. tsk->thread.debugreg6 = dr6;
  681. #ifdef CONFIG_KPROBES
  682. if (kprobe_debug_handler(regs))
  683. goto exit;
  684. #endif
  685. if (notify_die(DIE_DEBUG, "debug", regs, (long)&dr6, error_code,
  686. SIGTRAP) == NOTIFY_STOP)
  687. goto exit;
  688. /*
  689. * Let others (NMI) know that the debug stack is in use
  690. * as we may switch to the interrupt stack.
  691. */
  692. debug_stack_usage_inc();
  693. /* It's safe to allow irq's after DR6 has been saved */
  694. cond_local_irq_enable(regs);
  695. if (v8086_mode(regs)) {
  696. handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code,
  697. X86_TRAP_DB);
  698. cond_local_irq_disable(regs);
  699. debug_stack_usage_dec();
  700. goto exit;
  701. }
  702. if (WARN_ON_ONCE((dr6 & DR_STEP) && !user_mode(regs))) {
  703. /*
  704. * Historical junk that used to handle SYSENTER single-stepping.
  705. * This should be unreachable now. If we survive for a while
  706. * without anyone hitting this warning, we'll turn this into
  707. * an oops.
  708. */
  709. tsk->thread.debugreg6 &= ~DR_STEP;
  710. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  711. regs->flags &= ~X86_EFLAGS_TF;
  712. }
  713. si_code = get_si_code(tsk->thread.debugreg6);
  714. if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp)
  715. send_sigtrap(tsk, regs, error_code, si_code);
  716. cond_local_irq_disable(regs);
  717. debug_stack_usage_dec();
  718. exit:
  719. ist_exit(regs);
  720. }
  721. NOKPROBE_SYMBOL(do_debug);
  722. /*
  723. * Note that we play around with the 'TS' bit in an attempt to get
  724. * the correct behaviour even in the presence of the asynchronous
  725. * IRQ13 behaviour
  726. */
  727. static void math_error(struct pt_regs *regs, int error_code, int trapnr)
  728. {
  729. struct task_struct *task = current;
  730. struct fpu *fpu = &task->thread.fpu;
  731. siginfo_t info;
  732. char *str = (trapnr == X86_TRAP_MF) ? "fpu exception" :
  733. "simd exception";
  734. cond_local_irq_enable(regs);
  735. if (!user_mode(regs)) {
  736. if (fixup_exception(regs, trapnr))
  737. return;
  738. task->thread.error_code = error_code;
  739. task->thread.trap_nr = trapnr;
  740. if (notify_die(DIE_TRAP, str, regs, error_code,
  741. trapnr, SIGFPE) != NOTIFY_STOP)
  742. die(str, regs, error_code);
  743. return;
  744. }
  745. /*
  746. * Save the info for the exception handler and clear the error.
  747. */
  748. fpu__save(fpu);
  749. task->thread.trap_nr = trapnr;
  750. task->thread.error_code = error_code;
  751. clear_siginfo(&info);
  752. info.si_signo = SIGFPE;
  753. info.si_errno = 0;
  754. info.si_addr = (void __user *)uprobe_get_trap_addr(regs);
  755. info.si_code = fpu__exception_code(fpu, trapnr);
  756. /* Retry when we get spurious exceptions: */
  757. if (!info.si_code)
  758. return;
  759. force_sig_info(SIGFPE, &info, task);
  760. }
  761. dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
  762. {
  763. RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
  764. math_error(regs, error_code, X86_TRAP_MF);
  765. }
  766. dotraplinkage void
  767. do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
  768. {
  769. RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
  770. math_error(regs, error_code, X86_TRAP_XF);
  771. }
  772. dotraplinkage void
  773. do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
  774. {
  775. cond_local_irq_enable(regs);
  776. }
  777. dotraplinkage void
  778. do_device_not_available(struct pt_regs *regs, long error_code)
  779. {
  780. unsigned long cr0;
  781. RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
  782. #ifdef CONFIG_MATH_EMULATION
  783. if (!boot_cpu_has(X86_FEATURE_FPU) && (read_cr0() & X86_CR0_EM)) {
  784. struct math_emu_info info = { };
  785. cond_local_irq_enable(regs);
  786. info.regs = regs;
  787. math_emulate(&info);
  788. return;
  789. }
  790. #endif
  791. /* This should not happen. */
  792. cr0 = read_cr0();
  793. if (WARN(cr0 & X86_CR0_TS, "CR0.TS was set")) {
  794. /* Try to fix it up and carry on. */
  795. write_cr0(cr0 & ~X86_CR0_TS);
  796. } else {
  797. /*
  798. * Something terrible happened, and we're better off trying
  799. * to kill the task than getting stuck in a never-ending
  800. * loop of #NM faults.
  801. */
  802. die("unexpected #NM exception", regs, error_code);
  803. }
  804. }
  805. NOKPROBE_SYMBOL(do_device_not_available);
  806. #ifdef CONFIG_X86_32
  807. dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
  808. {
  809. siginfo_t info;
  810. RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
  811. local_irq_enable();
  812. clear_siginfo(&info);
  813. info.si_signo = SIGILL;
  814. info.si_errno = 0;
  815. info.si_code = ILL_BADSTK;
  816. info.si_addr = NULL;
  817. if (notify_die(DIE_TRAP, "iret exception", regs, error_code,
  818. X86_TRAP_IRET, SIGILL) != NOTIFY_STOP) {
  819. do_trap(X86_TRAP_IRET, SIGILL, "iret exception", regs, error_code,
  820. &info);
  821. }
  822. }
  823. #endif
  824. void __init trap_init(void)
  825. {
  826. /* Init cpu_entry_area before IST entries are set up */
  827. setup_cpu_entry_areas();
  828. idt_setup_traps();
  829. /*
  830. * Set the IDT descriptor to a fixed read-only location, so that the
  831. * "sidt" instruction will not leak the location of the kernel, and
  832. * to defend the IDT against arbitrary memory write vulnerabilities.
  833. * It will be reloaded in cpu_init() */
  834. cea_set_pte(CPU_ENTRY_AREA_RO_IDT_VADDR, __pa_symbol(idt_table),
  835. PAGE_KERNEL_RO);
  836. idt_descr.address = CPU_ENTRY_AREA_RO_IDT;
  837. /*
  838. * Should be a barrier for any external CPU state:
  839. */
  840. cpu_init();
  841. idt_setup_ist_traps();
  842. x86_init.irqs.trap_init();
  843. idt_setup_debugidt_traps();
  844. }