traps.c 27 KB

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