traps.c 27 KB

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