traps.c 24 KB

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