traps.c 29 KB

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