traps.c 26 KB

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