traps.c 28 KB

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