traps.c 28 KB

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