traps.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
  4. *
  5. * Pentium III FXSR, SSE support
  6. * Gareth Hughes <gareth@valinux.com>, May 2000
  7. */
  8. /*
  9. * Handle hardware traps and faults.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/context_tracking.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/kallsyms.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/kprobes.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/kdebug.h>
  19. #include <linux/kgdb.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/uprobes.h>
  24. #include <linux/string.h>
  25. #include <linux/delay.h>
  26. #include <linux/errno.h>
  27. #include <linux/kexec.h>
  28. #include <linux/sched.h>
  29. #include <linux/timer.h>
  30. #include <linux/init.h>
  31. #include <linux/bug.h>
  32. #include <linux/nmi.h>
  33. #include <linux/mm.h>
  34. #include <linux/smp.h>
  35. #include <linux/io.h>
  36. #ifdef CONFIG_EISA
  37. #include <linux/ioport.h>
  38. #include <linux/eisa.h>
  39. #endif
  40. #if defined(CONFIG_EDAC)
  41. #include <linux/edac.h>
  42. #endif
  43. #include <asm/kmemcheck.h>
  44. #include <asm/stacktrace.h>
  45. #include <asm/processor.h>
  46. #include <asm/debugreg.h>
  47. #include <linux/atomic.h>
  48. #include <asm/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. void ist_enter(struct pt_regs *regs)
  87. {
  88. if (user_mode(regs)) {
  89. RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
  90. } else {
  91. /*
  92. * We might have interrupted pretty much anything. In
  93. * fact, if we're a machine check, we can even interrupt
  94. * NMI processing. We don't want in_nmi() to return true,
  95. * but we need to notify RCU.
  96. */
  97. rcu_nmi_enter();
  98. }
  99. /*
  100. * We are atomic because we're on the IST stack; or we're on
  101. * x86_32, in which case we still shouldn't schedule; or we're
  102. * on x86_64 and entered from user mode, in which case we're
  103. * still atomic unless ist_begin_non_atomic is called.
  104. */
  105. preempt_count_add(HARDIRQ_OFFSET);
  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_count_sub(HARDIRQ_OFFSET);
  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_count_sub(HARDIRQ_OFFSET);
  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_count_add(HARDIRQ_OFFSET);
  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_X86_64
  257. /* Runs on IST stack */
  258. dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code)
  259. {
  260. static const char str[] = "double fault";
  261. struct task_struct *tsk = current;
  262. #ifdef CONFIG_X86_ESPFIX64
  263. extern unsigned char native_irq_return_iret[];
  264. /*
  265. * If IRET takes a non-IST fault on the espfix64 stack, then we
  266. * end up promoting it to a doublefault. In that case, modify
  267. * the stack to make it look like we just entered the #GP
  268. * handler from user space, similar to bad_iret.
  269. *
  270. * No need for ist_enter here because we don't use RCU.
  271. */
  272. if (((long)regs->sp >> PGDIR_SHIFT) == ESPFIX_PGD_ENTRY &&
  273. regs->cs == __KERNEL_CS &&
  274. regs->ip == (unsigned long)native_irq_return_iret)
  275. {
  276. struct pt_regs *normal_regs = task_pt_regs(current);
  277. /* Fake a #GP(0) from userspace. */
  278. memmove(&normal_regs->ip, (void *)regs->sp, 5*8);
  279. normal_regs->orig_ax = 0; /* Missing (lost) #GP error code */
  280. regs->ip = (unsigned long)general_protection;
  281. regs->sp = (unsigned long)&normal_regs->orig_ax;
  282. return;
  283. }
  284. #endif
  285. ist_enter(regs);
  286. notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_DF, SIGSEGV);
  287. tsk->thread.error_code = error_code;
  288. tsk->thread.trap_nr = X86_TRAP_DF;
  289. #ifdef CONFIG_DOUBLEFAULT
  290. df_debug(regs, error_code);
  291. #endif
  292. /*
  293. * This is always a kernel trap and never fixable (and thus must
  294. * never return).
  295. */
  296. for (;;)
  297. die(str, regs, error_code);
  298. }
  299. #endif
  300. dotraplinkage void do_bounds(struct pt_regs *regs, long error_code)
  301. {
  302. const struct mpx_bndcsr *bndcsr;
  303. siginfo_t *info;
  304. RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
  305. if (notify_die(DIE_TRAP, "bounds", regs, error_code,
  306. X86_TRAP_BR, SIGSEGV) == NOTIFY_STOP)
  307. return;
  308. cond_local_irq_enable(regs);
  309. if (!user_mode(regs))
  310. die("bounds", regs, error_code);
  311. if (!cpu_feature_enabled(X86_FEATURE_MPX)) {
  312. /* The exception is not from Intel MPX */
  313. goto exit_trap;
  314. }
  315. /*
  316. * We need to look at BNDSTATUS to resolve this exception.
  317. * A NULL here might mean that it is in its 'init state',
  318. * which is all zeros which indicates MPX was not
  319. * responsible for the exception.
  320. */
  321. bndcsr = get_xsave_field_ptr(XFEATURE_MASK_BNDCSR);
  322. if (!bndcsr)
  323. goto exit_trap;
  324. trace_bounds_exception_mpx(bndcsr);
  325. /*
  326. * The error code field of the BNDSTATUS register communicates status
  327. * information of a bound range exception #BR or operation involving
  328. * bound directory.
  329. */
  330. switch (bndcsr->bndstatus & MPX_BNDSTA_ERROR_CODE) {
  331. case 2: /* Bound directory has invalid entry. */
  332. if (mpx_handle_bd_fault())
  333. goto exit_trap;
  334. break; /* Success, it was handled */
  335. case 1: /* Bound violation. */
  336. info = mpx_generate_siginfo(regs);
  337. if (IS_ERR(info)) {
  338. /*
  339. * We failed to decode the MPX instruction. Act as if
  340. * the exception was not caused by MPX.
  341. */
  342. goto exit_trap;
  343. }
  344. /*
  345. * Success, we decoded the instruction and retrieved
  346. * an 'info' containing the address being accessed
  347. * which caused the exception. This information
  348. * allows and application to possibly handle the
  349. * #BR exception itself.
  350. */
  351. do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, error_code, info);
  352. kfree(info);
  353. break;
  354. case 0: /* No exception caused by Intel MPX operations. */
  355. goto exit_trap;
  356. default:
  357. die("bounds", regs, error_code);
  358. }
  359. return;
  360. exit_trap:
  361. /*
  362. * This path out is for all the cases where we could not
  363. * handle the exception in some way (like allocating a
  364. * table or telling userspace about it. We will also end
  365. * up here if the kernel has MPX turned off at compile
  366. * time..
  367. */
  368. do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, error_code, NULL);
  369. }
  370. dotraplinkage void
  371. do_general_protection(struct pt_regs *regs, long error_code)
  372. {
  373. struct task_struct *tsk;
  374. RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
  375. cond_local_irq_enable(regs);
  376. if (v8086_mode(regs)) {
  377. local_irq_enable();
  378. handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
  379. return;
  380. }
  381. tsk = current;
  382. if (!user_mode(regs)) {
  383. if (fixup_exception(regs, X86_TRAP_GP))
  384. return;
  385. tsk->thread.error_code = error_code;
  386. tsk->thread.trap_nr = X86_TRAP_GP;
  387. if (notify_die(DIE_GPF, "general protection fault", regs, error_code,
  388. X86_TRAP_GP, SIGSEGV) != NOTIFY_STOP)
  389. die("general protection fault", regs, error_code);
  390. return;
  391. }
  392. tsk->thread.error_code = error_code;
  393. tsk->thread.trap_nr = X86_TRAP_GP;
  394. if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
  395. printk_ratelimit()) {
  396. pr_info("%s[%d] general protection ip:%lx sp:%lx error:%lx",
  397. tsk->comm, task_pid_nr(tsk),
  398. regs->ip, regs->sp, error_code);
  399. print_vma_addr(" in ", regs->ip);
  400. pr_cont("\n");
  401. }
  402. force_sig_info(SIGSEGV, SEND_SIG_PRIV, tsk);
  403. }
  404. NOKPROBE_SYMBOL(do_general_protection);
  405. /* May run on IST stack. */
  406. dotraplinkage void notrace do_int3(struct pt_regs *regs, long error_code)
  407. {
  408. #ifdef CONFIG_DYNAMIC_FTRACE
  409. /*
  410. * ftrace must be first, everything else may cause a recursive crash.
  411. * See note by declaration of modifying_ftrace_code in ftrace.c
  412. */
  413. if (unlikely(atomic_read(&modifying_ftrace_code)) &&
  414. ftrace_int3_handler(regs))
  415. return;
  416. #endif
  417. if (poke_int3_handler(regs))
  418. return;
  419. ist_enter(regs);
  420. RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
  421. #ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
  422. if (kgdb_ll_trap(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
  423. SIGTRAP) == NOTIFY_STOP)
  424. goto exit;
  425. #endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
  426. #ifdef CONFIG_KPROBES
  427. if (kprobe_int3_handler(regs))
  428. goto exit;
  429. #endif
  430. if (notify_die(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
  431. SIGTRAP) == NOTIFY_STOP)
  432. goto exit;
  433. /*
  434. * Let others (NMI) know that the debug stack is in use
  435. * as we may switch to the interrupt stack.
  436. */
  437. debug_stack_usage_inc();
  438. preempt_disable();
  439. cond_local_irq_enable(regs);
  440. do_trap(X86_TRAP_BP, SIGTRAP, "int3", regs, error_code, NULL);
  441. cond_local_irq_disable(regs);
  442. preempt_enable_no_resched();
  443. debug_stack_usage_dec();
  444. exit:
  445. ist_exit(regs);
  446. }
  447. NOKPROBE_SYMBOL(do_int3);
  448. #ifdef CONFIG_X86_64
  449. /*
  450. * Help handler running on IST stack to switch off the IST stack if the
  451. * interrupted code was in user mode. The actual stack switch is done in
  452. * entry_64.S
  453. */
  454. asmlinkage __visible notrace struct pt_regs *sync_regs(struct pt_regs *eregs)
  455. {
  456. struct pt_regs *regs = task_pt_regs(current);
  457. *regs = *eregs;
  458. return regs;
  459. }
  460. NOKPROBE_SYMBOL(sync_regs);
  461. struct bad_iret_stack {
  462. void *error_entry_ret;
  463. struct pt_regs regs;
  464. };
  465. asmlinkage __visible notrace
  466. struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s)
  467. {
  468. /*
  469. * This is called from entry_64.S early in handling a fault
  470. * caused by a bad iret to user mode. To handle the fault
  471. * correctly, we want move our stack frame to task_pt_regs
  472. * and we want to pretend that the exception came from the
  473. * iret target.
  474. */
  475. struct bad_iret_stack *new_stack =
  476. container_of(task_pt_regs(current),
  477. struct bad_iret_stack, regs);
  478. /* Copy the IRET target to the new stack. */
  479. memmove(&new_stack->regs.ip, (void *)s->regs.sp, 5*8);
  480. /* Copy the remainder of the stack from the current stack. */
  481. memmove(new_stack, s, offsetof(struct bad_iret_stack, regs.ip));
  482. BUG_ON(!user_mode(&new_stack->regs));
  483. return new_stack;
  484. }
  485. NOKPROBE_SYMBOL(fixup_bad_iret);
  486. #endif
  487. static bool is_sysenter_singlestep(struct pt_regs *regs)
  488. {
  489. /*
  490. * We don't try for precision here. If we're anywhere in the region of
  491. * code that can be single-stepped in the SYSENTER entry path, then
  492. * assume that this is a useless single-step trap due to SYSENTER
  493. * being invoked with TF set. (We don't know in advance exactly
  494. * which instructions will be hit because BTF could plausibly
  495. * be set.)
  496. */
  497. #ifdef CONFIG_X86_32
  498. return (regs->ip - (unsigned long)__begin_SYSENTER_singlestep_region) <
  499. (unsigned long)__end_SYSENTER_singlestep_region -
  500. (unsigned long)__begin_SYSENTER_singlestep_region;
  501. #elif defined(CONFIG_IA32_EMULATION)
  502. return (regs->ip - (unsigned long)entry_SYSENTER_compat) <
  503. (unsigned long)__end_entry_SYSENTER_compat -
  504. (unsigned long)entry_SYSENTER_compat;
  505. #else
  506. return false;
  507. #endif
  508. }
  509. /*
  510. * Our handling of the processor debug registers is non-trivial.
  511. * We do not clear them on entry and exit from the kernel. Therefore
  512. * it is possible to get a watchpoint trap here from inside the kernel.
  513. * However, the code in ./ptrace.c has ensured that the user can
  514. * only set watchpoints on userspace addresses. Therefore the in-kernel
  515. * watchpoint trap can only occur in code which is reading/writing
  516. * from user space. Such code must not hold kernel locks (since it
  517. * can equally take a page fault), therefore it is safe to call
  518. * force_sig_info even though that claims and releases locks.
  519. *
  520. * Code in ./signal.c ensures that the debug control register
  521. * is restored before we deliver any signal, and therefore that
  522. * user code runs with the correct debug control register even though
  523. * we clear it here.
  524. *
  525. * Being careful here means that we don't have to be as careful in a
  526. * lot of more complicated places (task switching can be a bit lazy
  527. * about restoring all the debug state, and ptrace doesn't have to
  528. * find every occurrence of the TF bit that could be saved away even
  529. * by user code)
  530. *
  531. * May run on IST stack.
  532. */
  533. dotraplinkage void do_debug(struct pt_regs *regs, long error_code)
  534. {
  535. struct task_struct *tsk = current;
  536. int user_icebp = 0;
  537. unsigned long dr6;
  538. int si_code;
  539. ist_enter(regs);
  540. get_debugreg(dr6, 6);
  541. /*
  542. * The Intel SDM says:
  543. *
  544. * Certain debug exceptions may clear bits 0-3. The remaining
  545. * contents of the DR6 register are never cleared by the
  546. * processor. To avoid confusion in identifying debug
  547. * exceptions, debug handlers should clear the register before
  548. * returning to the interrupted task.
  549. *
  550. * Keep it simple: clear DR6 immediately.
  551. */
  552. set_debugreg(0, 6);
  553. /* Filter out all the reserved bits which are preset to 1 */
  554. dr6 &= ~DR6_RESERVED;
  555. /*
  556. * The SDM says "The processor clears the BTF flag when it
  557. * generates a debug exception." Clear TIF_BLOCKSTEP to keep
  558. * TIF_BLOCKSTEP in sync with the hardware BTF flag.
  559. */
  560. clear_tsk_thread_flag(tsk, TIF_BLOCKSTEP);
  561. if (unlikely(!user_mode(regs) && (dr6 & DR_STEP) &&
  562. is_sysenter_singlestep(regs))) {
  563. dr6 &= ~DR_STEP;
  564. if (!dr6)
  565. goto exit;
  566. /*
  567. * else we might have gotten a single-step trap and hit a
  568. * watchpoint at the same time, in which case we should fall
  569. * through and handle the watchpoint.
  570. */
  571. }
  572. /*
  573. * If dr6 has no reason to give us about the origin of this trap,
  574. * then it's very likely the result of an icebp/int01 trap.
  575. * User wants a sigtrap for that.
  576. */
  577. if (!dr6 && user_mode(regs))
  578. user_icebp = 1;
  579. /* Catch kmemcheck conditions! */
  580. if ((dr6 & DR_STEP) && kmemcheck_trap(regs))
  581. goto exit;
  582. /* Store the virtualized DR6 value */
  583. tsk->thread.debugreg6 = dr6;
  584. #ifdef CONFIG_KPROBES
  585. if (kprobe_debug_handler(regs))
  586. goto exit;
  587. #endif
  588. if (notify_die(DIE_DEBUG, "debug", regs, (long)&dr6, error_code,
  589. SIGTRAP) == NOTIFY_STOP)
  590. goto exit;
  591. /*
  592. * Let others (NMI) know that the debug stack is in use
  593. * as we may switch to the interrupt stack.
  594. */
  595. debug_stack_usage_inc();
  596. /* It's safe to allow irq's after DR6 has been saved */
  597. preempt_disable();
  598. cond_local_irq_enable(regs);
  599. if (v8086_mode(regs)) {
  600. handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code,
  601. X86_TRAP_DB);
  602. cond_local_irq_disable(regs);
  603. preempt_enable_no_resched();
  604. debug_stack_usage_dec();
  605. goto exit;
  606. }
  607. if (WARN_ON_ONCE((dr6 & DR_STEP) && !user_mode(regs))) {
  608. /*
  609. * Historical junk that used to handle SYSENTER single-stepping.
  610. * This should be unreachable now. If we survive for a while
  611. * without anyone hitting this warning, we'll turn this into
  612. * an oops.
  613. */
  614. tsk->thread.debugreg6 &= ~DR_STEP;
  615. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  616. regs->flags &= ~X86_EFLAGS_TF;
  617. }
  618. si_code = get_si_code(tsk->thread.debugreg6);
  619. if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp)
  620. send_sigtrap(tsk, regs, error_code, si_code);
  621. cond_local_irq_disable(regs);
  622. preempt_enable_no_resched();
  623. debug_stack_usage_dec();
  624. exit:
  625. #if defined(CONFIG_X86_32)
  626. /*
  627. * This is the most likely code path that involves non-trivial use
  628. * of the SYSENTER stack. Check that we haven't overrun it.
  629. */
  630. WARN(this_cpu_read(cpu_tss.SYSENTER_stack_canary) != STACK_END_MAGIC,
  631. "Overran or corrupted SYSENTER stack\n");
  632. #endif
  633. ist_exit(regs);
  634. }
  635. NOKPROBE_SYMBOL(do_debug);
  636. /*
  637. * Note that we play around with the 'TS' bit in an attempt to get
  638. * the correct behaviour even in the presence of the asynchronous
  639. * IRQ13 behaviour
  640. */
  641. static void math_error(struct pt_regs *regs, int error_code, int trapnr)
  642. {
  643. struct task_struct *task = current;
  644. struct fpu *fpu = &task->thread.fpu;
  645. siginfo_t info;
  646. char *str = (trapnr == X86_TRAP_MF) ? "fpu exception" :
  647. "simd exception";
  648. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, SIGFPE) == NOTIFY_STOP)
  649. return;
  650. cond_local_irq_enable(regs);
  651. if (!user_mode(regs)) {
  652. if (!fixup_exception(regs, trapnr)) {
  653. task->thread.error_code = error_code;
  654. task->thread.trap_nr = trapnr;
  655. die(str, regs, error_code);
  656. }
  657. return;
  658. }
  659. /*
  660. * Save the info for the exception handler and clear the error.
  661. */
  662. fpu__save(fpu);
  663. task->thread.trap_nr = trapnr;
  664. task->thread.error_code = error_code;
  665. info.si_signo = SIGFPE;
  666. info.si_errno = 0;
  667. info.si_addr = (void __user *)uprobe_get_trap_addr(regs);
  668. info.si_code = fpu__exception_code(fpu, trapnr);
  669. /* Retry when we get spurious exceptions: */
  670. if (!info.si_code)
  671. return;
  672. force_sig_info(SIGFPE, &info, task);
  673. }
  674. dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
  675. {
  676. RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
  677. math_error(regs, error_code, X86_TRAP_MF);
  678. }
  679. dotraplinkage void
  680. do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
  681. {
  682. RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
  683. math_error(regs, error_code, X86_TRAP_XF);
  684. }
  685. dotraplinkage void
  686. do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
  687. {
  688. cond_local_irq_enable(regs);
  689. }
  690. dotraplinkage void
  691. do_device_not_available(struct pt_regs *regs, long error_code)
  692. {
  693. RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
  694. #ifdef CONFIG_MATH_EMULATION
  695. if (!boot_cpu_has(X86_FEATURE_FPU) && (read_cr0() & X86_CR0_EM)) {
  696. struct math_emu_info info = { };
  697. cond_local_irq_enable(regs);
  698. info.regs = regs;
  699. math_emulate(&info);
  700. return;
  701. }
  702. #endif
  703. fpu__restore(&current->thread.fpu); /* interrupts still off */
  704. #ifdef CONFIG_X86_32
  705. cond_local_irq_enable(regs);
  706. #endif
  707. }
  708. NOKPROBE_SYMBOL(do_device_not_available);
  709. #ifdef CONFIG_X86_32
  710. dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
  711. {
  712. siginfo_t info;
  713. RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");
  714. local_irq_enable();
  715. info.si_signo = SIGILL;
  716. info.si_errno = 0;
  717. info.si_code = ILL_BADSTK;
  718. info.si_addr = NULL;
  719. if (notify_die(DIE_TRAP, "iret exception", regs, error_code,
  720. X86_TRAP_IRET, SIGILL) != NOTIFY_STOP) {
  721. do_trap(X86_TRAP_IRET, SIGILL, "iret exception", regs, error_code,
  722. &info);
  723. }
  724. }
  725. #endif
  726. /* Set of traps needed for early debugging. */
  727. void __init early_trap_init(void)
  728. {
  729. /*
  730. * Don't use IST to set DEBUG_STACK as it doesn't work until TSS
  731. * is ready in cpu_init() <-- trap_init(). Before trap_init(),
  732. * CPU runs at ring 0 so it is impossible to hit an invalid
  733. * stack. Using the original stack works well enough at this
  734. * early stage. DEBUG_STACK will be equipped after cpu_init() in
  735. * trap_init().
  736. *
  737. * We don't need to set trace_idt_table like set_intr_gate(),
  738. * since we don't have trace_debug and it will be reset to
  739. * 'debug' in trap_init() by set_intr_gate_ist().
  740. */
  741. set_intr_gate_notrace(X86_TRAP_DB, debug);
  742. /* int3 can be called from all */
  743. set_system_intr_gate(X86_TRAP_BP, &int3);
  744. #ifdef CONFIG_X86_32
  745. set_intr_gate(X86_TRAP_PF, page_fault);
  746. #endif
  747. load_idt(&idt_descr);
  748. }
  749. void __init early_trap_pf_init(void)
  750. {
  751. #ifdef CONFIG_X86_64
  752. set_intr_gate(X86_TRAP_PF, page_fault);
  753. #endif
  754. }
  755. void __init trap_init(void)
  756. {
  757. int i;
  758. #ifdef CONFIG_EISA
  759. void __iomem *p = early_ioremap(0x0FFFD9, 4);
  760. if (readl(p) == 'E' + ('I'<<8) + ('S'<<16) + ('A'<<24))
  761. EISA_bus = 1;
  762. early_iounmap(p, 4);
  763. #endif
  764. set_intr_gate(X86_TRAP_DE, divide_error);
  765. set_intr_gate_ist(X86_TRAP_NMI, &nmi, NMI_STACK);
  766. /* int4 can be called from all */
  767. set_system_intr_gate(X86_TRAP_OF, &overflow);
  768. set_intr_gate(X86_TRAP_BR, bounds);
  769. set_intr_gate(X86_TRAP_UD, invalid_op);
  770. set_intr_gate(X86_TRAP_NM, device_not_available);
  771. #ifdef CONFIG_X86_32
  772. set_task_gate(X86_TRAP_DF, GDT_ENTRY_DOUBLEFAULT_TSS);
  773. #else
  774. set_intr_gate_ist(X86_TRAP_DF, &double_fault, DOUBLEFAULT_STACK);
  775. #endif
  776. set_intr_gate(X86_TRAP_OLD_MF, coprocessor_segment_overrun);
  777. set_intr_gate(X86_TRAP_TS, invalid_TSS);
  778. set_intr_gate(X86_TRAP_NP, segment_not_present);
  779. set_intr_gate(X86_TRAP_SS, stack_segment);
  780. set_intr_gate(X86_TRAP_GP, general_protection);
  781. set_intr_gate(X86_TRAP_SPURIOUS, spurious_interrupt_bug);
  782. set_intr_gate(X86_TRAP_MF, coprocessor_error);
  783. set_intr_gate(X86_TRAP_AC, alignment_check);
  784. #ifdef CONFIG_X86_MCE
  785. set_intr_gate_ist(X86_TRAP_MC, &machine_check, MCE_STACK);
  786. #endif
  787. set_intr_gate(X86_TRAP_XF, simd_coprocessor_error);
  788. /* Reserve all the builtin and the syscall vector: */
  789. for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
  790. set_bit(i, used_vectors);
  791. #ifdef CONFIG_IA32_EMULATION
  792. set_system_intr_gate(IA32_SYSCALL_VECTOR, entry_INT80_compat);
  793. set_bit(IA32_SYSCALL_VECTOR, used_vectors);
  794. #endif
  795. #ifdef CONFIG_X86_32
  796. set_system_intr_gate(IA32_SYSCALL_VECTOR, entry_INT80_32);
  797. set_bit(IA32_SYSCALL_VECTOR, used_vectors);
  798. #endif
  799. /*
  800. * Set the IDT descriptor to a fixed read-only location, so that the
  801. * "sidt" instruction will not leak the location of the kernel, and
  802. * to defend the IDT against arbitrary memory write vulnerabilities.
  803. * It will be reloaded in cpu_init() */
  804. __set_fixmap(FIX_RO_IDT, __pa_symbol(idt_table), PAGE_KERNEL_RO);
  805. idt_descr.address = fix_to_virt(FIX_RO_IDT);
  806. /*
  807. * Should be a barrier for any external CPU state:
  808. */
  809. cpu_init();
  810. /*
  811. * X86_TRAP_DB and X86_TRAP_BP have been set
  812. * in early_trap_init(). However, ITS works only after
  813. * cpu_init() loads TSS. See comments in early_trap_init().
  814. */
  815. set_intr_gate_ist(X86_TRAP_DB, &debug, DEBUG_STACK);
  816. /* int3 can be called from all */
  817. set_system_intr_gate_ist(X86_TRAP_BP, &int3, DEBUG_STACK);
  818. x86_init.irqs.trap_init();
  819. #ifdef CONFIG_X86_64
  820. memcpy(&debug_idt_table, &idt_table, IDT_ENTRIES * 16);
  821. set_nmi_gate(X86_TRAP_DB, &debug);
  822. set_nmi_gate(X86_TRAP_BP, &int3);
  823. #endif
  824. }