traps.c 27 KB

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