traps.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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/string.h>
  24. #include <linux/delay.h>
  25. #include <linux/errno.h>
  26. #include <linux/kexec.h>
  27. #include <linux/sched.h>
  28. #include <linux/timer.h>
  29. #include <linux/init.h>
  30. #include <linux/bug.h>
  31. #include <linux/nmi.h>
  32. #include <linux/mm.h>
  33. #include <linux/smp.h>
  34. #include <linux/io.h>
  35. #ifdef CONFIG_EISA
  36. #include <linux/ioport.h>
  37. #include <linux/eisa.h>
  38. #endif
  39. #if defined(CONFIG_EDAC)
  40. #include <linux/edac.h>
  41. #endif
  42. #include <asm/kmemcheck.h>
  43. #include <asm/stacktrace.h>
  44. #include <asm/processor.h>
  45. #include <asm/debugreg.h>
  46. #include <linux/atomic.h>
  47. #include <asm/ftrace.h>
  48. #include <asm/traps.h>
  49. #include <asm/desc.h>
  50. #include <asm/i387.h>
  51. #include <asm/fpu-internal.h>
  52. #include <asm/mce.h>
  53. #include <asm/fixmap.h>
  54. #include <asm/mach_traps.h>
  55. #ifdef CONFIG_X86_64
  56. #include <asm/x86_init.h>
  57. #include <asm/pgalloc.h>
  58. #include <asm/proto.h>
  59. /* No need to be aligned, but done to keep all IDTs defined the same way. */
  60. gate_desc debug_idt_table[NR_VECTORS] __page_aligned_bss;
  61. #else
  62. #include <asm/processor-flags.h>
  63. #include <asm/setup.h>
  64. asmlinkage int system_call(void);
  65. #endif
  66. /* Must be page-aligned because the real IDT is used in a fixmap. */
  67. gate_desc idt_table[NR_VECTORS] __page_aligned_bss;
  68. DECLARE_BITMAP(used_vectors, NR_VECTORS);
  69. EXPORT_SYMBOL_GPL(used_vectors);
  70. static inline void conditional_sti(struct pt_regs *regs)
  71. {
  72. if (regs->flags & X86_EFLAGS_IF)
  73. local_irq_enable();
  74. }
  75. static inline void preempt_conditional_sti(struct pt_regs *regs)
  76. {
  77. inc_preempt_count();
  78. if (regs->flags & X86_EFLAGS_IF)
  79. local_irq_enable();
  80. }
  81. static inline void conditional_cli(struct pt_regs *regs)
  82. {
  83. if (regs->flags & X86_EFLAGS_IF)
  84. local_irq_disable();
  85. }
  86. static inline void preempt_conditional_cli(struct pt_regs *regs)
  87. {
  88. if (regs->flags & X86_EFLAGS_IF)
  89. local_irq_disable();
  90. dec_preempt_count();
  91. }
  92. static int __kprobes
  93. do_trap_no_signal(struct task_struct *tsk, int trapnr, char *str,
  94. struct pt_regs *regs, long error_code)
  95. {
  96. #ifdef CONFIG_X86_32
  97. if (regs->flags & X86_VM_MASK) {
  98. /*
  99. * Traps 0, 1, 3, 4, and 5 should be forwarded to vm86.
  100. * On nmi (interrupt 2), do_trap should not be called.
  101. */
  102. if (trapnr < X86_TRAP_UD) {
  103. if (!handle_vm86_trap((struct kernel_vm86_regs *) regs,
  104. error_code, trapnr))
  105. return 0;
  106. }
  107. return -1;
  108. }
  109. #endif
  110. if (!user_mode(regs)) {
  111. if (!fixup_exception(regs)) {
  112. tsk->thread.error_code = error_code;
  113. tsk->thread.trap_nr = trapnr;
  114. die(str, regs, error_code);
  115. }
  116. return 0;
  117. }
  118. return -1;
  119. }
  120. static void __kprobes
  121. do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
  122. long error_code, siginfo_t *info)
  123. {
  124. struct task_struct *tsk = current;
  125. if (!do_trap_no_signal(tsk, trapnr, str, regs, error_code))
  126. return;
  127. /*
  128. * We want error_code and trap_nr set for userspace faults and
  129. * kernelspace faults which result in die(), but not
  130. * kernelspace faults which are fixed up. die() gives the
  131. * process no chance to handle the signal and notice the
  132. * kernel fault information, so that won't result in polluting
  133. * the information about previously queued, but not yet
  134. * delivered, faults. See also do_general_protection below.
  135. */
  136. tsk->thread.error_code = error_code;
  137. tsk->thread.trap_nr = trapnr;
  138. #ifdef CONFIG_X86_64
  139. if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
  140. printk_ratelimit()) {
  141. pr_info("%s[%d] trap %s ip:%lx sp:%lx error:%lx",
  142. tsk->comm, tsk->pid, str,
  143. regs->ip, regs->sp, error_code);
  144. print_vma_addr(" in ", regs->ip);
  145. pr_cont("\n");
  146. }
  147. #endif
  148. if (info)
  149. force_sig_info(signr, info, tsk);
  150. else
  151. force_sig(signr, tsk);
  152. }
  153. #define DO_ERROR(trapnr, signr, str, name) \
  154. dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
  155. { \
  156. enum ctx_state prev_state; \
  157. \
  158. prev_state = exception_enter(); \
  159. if (notify_die(DIE_TRAP, str, regs, error_code, \
  160. trapnr, signr) == NOTIFY_STOP) { \
  161. exception_exit(prev_state); \
  162. return; \
  163. } \
  164. conditional_sti(regs); \
  165. do_trap(trapnr, signr, str, regs, error_code, NULL); \
  166. exception_exit(prev_state); \
  167. }
  168. #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
  169. dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
  170. { \
  171. siginfo_t info; \
  172. enum ctx_state prev_state; \
  173. \
  174. info.si_signo = signr; \
  175. info.si_errno = 0; \
  176. info.si_code = sicode; \
  177. info.si_addr = (void __user *)siaddr; \
  178. prev_state = exception_enter(); \
  179. if (notify_die(DIE_TRAP, str, regs, error_code, \
  180. trapnr, signr) == NOTIFY_STOP) { \
  181. exception_exit(prev_state); \
  182. return; \
  183. } \
  184. conditional_sti(regs); \
  185. do_trap(trapnr, signr, str, regs, error_code, &info); \
  186. exception_exit(prev_state); \
  187. }
  188. DO_ERROR_INFO(X86_TRAP_DE, SIGFPE, "divide error", divide_error, FPE_INTDIV,
  189. regs->ip)
  190. DO_ERROR(X86_TRAP_OF, SIGSEGV, "overflow", overflow)
  191. DO_ERROR(X86_TRAP_BR, SIGSEGV, "bounds", bounds)
  192. DO_ERROR_INFO(X86_TRAP_UD, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN,
  193. regs->ip)
  194. DO_ERROR(X86_TRAP_OLD_MF, SIGFPE, "coprocessor segment overrun",
  195. coprocessor_segment_overrun)
  196. DO_ERROR(X86_TRAP_TS, SIGSEGV, "invalid TSS", invalid_TSS)
  197. DO_ERROR(X86_TRAP_NP, SIGBUS, "segment not present", segment_not_present)
  198. #ifdef CONFIG_X86_32
  199. DO_ERROR(X86_TRAP_SS, SIGBUS, "stack segment", stack_segment)
  200. #endif
  201. DO_ERROR_INFO(X86_TRAP_AC, SIGBUS, "alignment check", alignment_check,
  202. BUS_ADRALN, 0)
  203. #ifdef CONFIG_X86_64
  204. /* Runs on IST stack */
  205. dotraplinkage void do_stack_segment(struct pt_regs *regs, long error_code)
  206. {
  207. enum ctx_state prev_state;
  208. prev_state = exception_enter();
  209. if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
  210. X86_TRAP_SS, SIGBUS) != NOTIFY_STOP) {
  211. preempt_conditional_sti(regs);
  212. do_trap(X86_TRAP_SS, SIGBUS, "stack segment", regs, error_code, NULL);
  213. preempt_conditional_cli(regs);
  214. }
  215. exception_exit(prev_state);
  216. }
  217. dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code)
  218. {
  219. static const char str[] = "double fault";
  220. struct task_struct *tsk = current;
  221. exception_enter();
  222. /* Return not checked because double check cannot be ignored */
  223. notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_DF, SIGSEGV);
  224. tsk->thread.error_code = error_code;
  225. tsk->thread.trap_nr = X86_TRAP_DF;
  226. #ifdef CONFIG_DOUBLEFAULT
  227. df_debug(regs, error_code);
  228. #endif
  229. /*
  230. * This is always a kernel trap and never fixable (and thus must
  231. * never return).
  232. */
  233. for (;;)
  234. die(str, regs, error_code);
  235. }
  236. #endif
  237. dotraplinkage void __kprobes
  238. do_general_protection(struct pt_regs *regs, long error_code)
  239. {
  240. struct task_struct *tsk;
  241. enum ctx_state prev_state;
  242. prev_state = exception_enter();
  243. conditional_sti(regs);
  244. #ifdef CONFIG_X86_32
  245. if (regs->flags & X86_VM_MASK) {
  246. local_irq_enable();
  247. handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
  248. goto exit;
  249. }
  250. #endif
  251. tsk = current;
  252. if (!user_mode(regs)) {
  253. if (fixup_exception(regs))
  254. goto exit;
  255. tsk->thread.error_code = error_code;
  256. tsk->thread.trap_nr = X86_TRAP_GP;
  257. if (notify_die(DIE_GPF, "general protection fault", regs, error_code,
  258. X86_TRAP_GP, SIGSEGV) != NOTIFY_STOP)
  259. die("general protection fault", regs, error_code);
  260. goto exit;
  261. }
  262. tsk->thread.error_code = error_code;
  263. tsk->thread.trap_nr = X86_TRAP_GP;
  264. if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
  265. printk_ratelimit()) {
  266. pr_info("%s[%d] general protection ip:%lx sp:%lx error:%lx",
  267. tsk->comm, task_pid_nr(tsk),
  268. regs->ip, regs->sp, error_code);
  269. print_vma_addr(" in ", regs->ip);
  270. pr_cont("\n");
  271. }
  272. force_sig(SIGSEGV, tsk);
  273. exit:
  274. exception_exit(prev_state);
  275. }
  276. /* May run on IST stack. */
  277. dotraplinkage void __kprobes notrace do_int3(struct pt_regs *regs, long error_code)
  278. {
  279. enum ctx_state prev_state;
  280. #ifdef CONFIG_DYNAMIC_FTRACE
  281. /*
  282. * ftrace must be first, everything else may cause a recursive crash.
  283. * See note by declaration of modifying_ftrace_code in ftrace.c
  284. */
  285. if (unlikely(atomic_read(&modifying_ftrace_code)) &&
  286. ftrace_int3_handler(regs))
  287. return;
  288. #endif
  289. prev_state = exception_enter();
  290. #ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
  291. if (kgdb_ll_trap(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
  292. SIGTRAP) == NOTIFY_STOP)
  293. goto exit;
  294. #endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
  295. if (notify_die(DIE_INT3, "int3", regs, error_code, X86_TRAP_BP,
  296. SIGTRAP) == NOTIFY_STOP)
  297. goto exit;
  298. /*
  299. * Let others (NMI) know that the debug stack is in use
  300. * as we may switch to the interrupt stack.
  301. */
  302. debug_stack_usage_inc();
  303. preempt_conditional_sti(regs);
  304. do_trap(X86_TRAP_BP, SIGTRAP, "int3", regs, error_code, NULL);
  305. preempt_conditional_cli(regs);
  306. debug_stack_usage_dec();
  307. exit:
  308. exception_exit(prev_state);
  309. }
  310. #ifdef CONFIG_X86_64
  311. /*
  312. * Help handler running on IST stack to switch back to user stack
  313. * for scheduling or signal handling. The actual stack switch is done in
  314. * entry.S
  315. */
  316. asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
  317. {
  318. struct pt_regs *regs = eregs;
  319. /* Did already sync */
  320. if (eregs == (struct pt_regs *)eregs->sp)
  321. ;
  322. /* Exception from user space */
  323. else if (user_mode(eregs))
  324. regs = task_pt_regs(current);
  325. /*
  326. * Exception from kernel and interrupts are enabled. Move to
  327. * kernel process stack.
  328. */
  329. else if (eregs->flags & X86_EFLAGS_IF)
  330. regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
  331. if (eregs != regs)
  332. *regs = *eregs;
  333. return regs;
  334. }
  335. #endif
  336. /*
  337. * Our handling of the processor debug registers is non-trivial.
  338. * We do not clear them on entry and exit from the kernel. Therefore
  339. * it is possible to get a watchpoint trap here from inside the kernel.
  340. * However, the code in ./ptrace.c has ensured that the user can
  341. * only set watchpoints on userspace addresses. Therefore the in-kernel
  342. * watchpoint trap can only occur in code which is reading/writing
  343. * from user space. Such code must not hold kernel locks (since it
  344. * can equally take a page fault), therefore it is safe to call
  345. * force_sig_info even though that claims and releases locks.
  346. *
  347. * Code in ./signal.c ensures that the debug control register
  348. * is restored before we deliver any signal, and therefore that
  349. * user code runs with the correct debug control register even though
  350. * we clear it here.
  351. *
  352. * Being careful here means that we don't have to be as careful in a
  353. * lot of more complicated places (task switching can be a bit lazy
  354. * about restoring all the debug state, and ptrace doesn't have to
  355. * find every occurrence of the TF bit that could be saved away even
  356. * by user code)
  357. *
  358. * May run on IST stack.
  359. */
  360. dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code)
  361. {
  362. struct task_struct *tsk = current;
  363. enum ctx_state prev_state;
  364. int user_icebp = 0;
  365. unsigned long dr6;
  366. int si_code;
  367. prev_state = exception_enter();
  368. get_debugreg(dr6, 6);
  369. /* Filter out all the reserved bits which are preset to 1 */
  370. dr6 &= ~DR6_RESERVED;
  371. /*
  372. * If dr6 has no reason to give us about the origin of this trap,
  373. * then it's very likely the result of an icebp/int01 trap.
  374. * User wants a sigtrap for that.
  375. */
  376. if (!dr6 && user_mode(regs))
  377. user_icebp = 1;
  378. /* Catch kmemcheck conditions first of all! */
  379. if ((dr6 & DR_STEP) && kmemcheck_trap(regs))
  380. goto exit;
  381. /* DR6 may or may not be cleared by the CPU */
  382. set_debugreg(0, 6);
  383. /*
  384. * The processor cleared BTF, so don't mark that we need it set.
  385. */
  386. clear_tsk_thread_flag(tsk, TIF_BLOCKSTEP);
  387. /* Store the virtualized DR6 value */
  388. tsk->thread.debugreg6 = dr6;
  389. if (notify_die(DIE_DEBUG, "debug", regs, (long)&dr6, error_code,
  390. SIGTRAP) == NOTIFY_STOP)
  391. goto exit;
  392. /*
  393. * Let others (NMI) know that the debug stack is in use
  394. * as we may switch to the interrupt stack.
  395. */
  396. debug_stack_usage_inc();
  397. /* It's safe to allow irq's after DR6 has been saved */
  398. preempt_conditional_sti(regs);
  399. if (regs->flags & X86_VM_MASK) {
  400. handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code,
  401. X86_TRAP_DB);
  402. preempt_conditional_cli(regs);
  403. debug_stack_usage_dec();
  404. goto exit;
  405. }
  406. /*
  407. * Single-stepping through system calls: ignore any exceptions in
  408. * kernel space, but re-enable TF when returning to user mode.
  409. *
  410. * We already checked v86 mode above, so we can check for kernel mode
  411. * by just checking the CPL of CS.
  412. */
  413. if ((dr6 & DR_STEP) && !user_mode(regs)) {
  414. tsk->thread.debugreg6 &= ~DR_STEP;
  415. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  416. regs->flags &= ~X86_EFLAGS_TF;
  417. }
  418. si_code = get_si_code(tsk->thread.debugreg6);
  419. if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp)
  420. send_sigtrap(tsk, regs, error_code, si_code);
  421. preempt_conditional_cli(regs);
  422. debug_stack_usage_dec();
  423. exit:
  424. exception_exit(prev_state);
  425. }
  426. /*
  427. * Note that we play around with the 'TS' bit in an attempt to get
  428. * the correct behaviour even in the presence of the asynchronous
  429. * IRQ13 behaviour
  430. */
  431. void math_error(struct pt_regs *regs, int error_code, int trapnr)
  432. {
  433. struct task_struct *task = current;
  434. siginfo_t info;
  435. unsigned short err;
  436. char *str = (trapnr == X86_TRAP_MF) ? "fpu exception" :
  437. "simd exception";
  438. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, SIGFPE) == NOTIFY_STOP)
  439. return;
  440. conditional_sti(regs);
  441. if (!user_mode_vm(regs))
  442. {
  443. if (!fixup_exception(regs)) {
  444. task->thread.error_code = error_code;
  445. task->thread.trap_nr = trapnr;
  446. die(str, regs, error_code);
  447. }
  448. return;
  449. }
  450. /*
  451. * Save the info for the exception handler and clear the error.
  452. */
  453. save_init_fpu(task);
  454. task->thread.trap_nr = trapnr;
  455. task->thread.error_code = error_code;
  456. info.si_signo = SIGFPE;
  457. info.si_errno = 0;
  458. info.si_addr = (void __user *)regs->ip;
  459. if (trapnr == X86_TRAP_MF) {
  460. unsigned short cwd, swd;
  461. /*
  462. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  463. * status. 0x3f is the exception bits in these regs, 0x200 is the
  464. * C1 reg you need in case of a stack fault, 0x040 is the stack
  465. * fault bit. We should only be taking one exception at a time,
  466. * so if this combination doesn't produce any single exception,
  467. * then we have a bad program that isn't synchronizing its FPU usage
  468. * and it will suffer the consequences since we won't be able to
  469. * fully reproduce the context of the exception
  470. */
  471. cwd = get_fpu_cwd(task);
  472. swd = get_fpu_swd(task);
  473. err = swd & ~cwd;
  474. } else {
  475. /*
  476. * The SIMD FPU exceptions are handled a little differently, as there
  477. * is only a single status/control register. Thus, to determine which
  478. * unmasked exception was caught we must mask the exception mask bits
  479. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  480. */
  481. unsigned short mxcsr = get_fpu_mxcsr(task);
  482. err = ~(mxcsr >> 7) & mxcsr;
  483. }
  484. if (err & 0x001) { /* Invalid op */
  485. /*
  486. * swd & 0x240 == 0x040: Stack Underflow
  487. * swd & 0x240 == 0x240: Stack Overflow
  488. * User must clear the SF bit (0x40) if set
  489. */
  490. info.si_code = FPE_FLTINV;
  491. } else if (err & 0x004) { /* Divide by Zero */
  492. info.si_code = FPE_FLTDIV;
  493. } else if (err & 0x008) { /* Overflow */
  494. info.si_code = FPE_FLTOVF;
  495. } else if (err & 0x012) { /* Denormal, Underflow */
  496. info.si_code = FPE_FLTUND;
  497. } else if (err & 0x020) { /* Precision */
  498. info.si_code = FPE_FLTRES;
  499. } else {
  500. /*
  501. * If we're using IRQ 13, or supposedly even some trap
  502. * X86_TRAP_MF implementations, it's possible
  503. * we get a spurious trap, which is not an error.
  504. */
  505. return;
  506. }
  507. force_sig_info(SIGFPE, &info, task);
  508. }
  509. dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
  510. {
  511. enum ctx_state prev_state;
  512. prev_state = exception_enter();
  513. math_error(regs, error_code, X86_TRAP_MF);
  514. exception_exit(prev_state);
  515. }
  516. dotraplinkage void
  517. do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
  518. {
  519. enum ctx_state prev_state;
  520. prev_state = exception_enter();
  521. math_error(regs, error_code, X86_TRAP_XF);
  522. exception_exit(prev_state);
  523. }
  524. dotraplinkage void
  525. do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
  526. {
  527. conditional_sti(regs);
  528. #if 0
  529. /* No need to warn about this any longer. */
  530. pr_info("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
  531. #endif
  532. }
  533. asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
  534. {
  535. }
  536. asmlinkage void __attribute__((weak)) smp_threshold_interrupt(void)
  537. {
  538. }
  539. /*
  540. * 'math_state_restore()' saves the current math information in the
  541. * old math state array, and gets the new ones from the current task
  542. *
  543. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  544. * Don't touch unless you *really* know how it works.
  545. *
  546. * Must be called with kernel preemption disabled (eg with local
  547. * local interrupts as in the case of do_device_not_available).
  548. */
  549. void math_state_restore(void)
  550. {
  551. struct task_struct *tsk = current;
  552. if (!tsk_used_math(tsk)) {
  553. local_irq_enable();
  554. /*
  555. * does a slab alloc which can sleep
  556. */
  557. if (init_fpu(tsk)) {
  558. /*
  559. * ran out of memory!
  560. */
  561. do_group_exit(SIGKILL);
  562. return;
  563. }
  564. local_irq_disable();
  565. }
  566. __thread_fpu_begin(tsk);
  567. /*
  568. * Paranoid restore. send a SIGSEGV if we fail to restore the state.
  569. */
  570. if (unlikely(restore_fpu_checking(tsk))) {
  571. drop_init_fpu(tsk);
  572. force_sig(SIGSEGV, tsk);
  573. return;
  574. }
  575. tsk->fpu_counter++;
  576. }
  577. EXPORT_SYMBOL_GPL(math_state_restore);
  578. dotraplinkage void __kprobes
  579. do_device_not_available(struct pt_regs *regs, long error_code)
  580. {
  581. enum ctx_state prev_state;
  582. prev_state = exception_enter();
  583. BUG_ON(use_eager_fpu());
  584. #ifdef CONFIG_MATH_EMULATION
  585. if (read_cr0() & X86_CR0_EM) {
  586. struct math_emu_info info = { };
  587. conditional_sti(regs);
  588. info.regs = regs;
  589. math_emulate(&info);
  590. exception_exit(prev_state);
  591. return;
  592. }
  593. #endif
  594. math_state_restore(); /* interrupts still off */
  595. #ifdef CONFIG_X86_32
  596. conditional_sti(regs);
  597. #endif
  598. exception_exit(prev_state);
  599. }
  600. #ifdef CONFIG_X86_32
  601. dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
  602. {
  603. siginfo_t info;
  604. enum ctx_state prev_state;
  605. prev_state = exception_enter();
  606. local_irq_enable();
  607. info.si_signo = SIGILL;
  608. info.si_errno = 0;
  609. info.si_code = ILL_BADSTK;
  610. info.si_addr = NULL;
  611. if (notify_die(DIE_TRAP, "iret exception", regs, error_code,
  612. X86_TRAP_IRET, SIGILL) != NOTIFY_STOP) {
  613. do_trap(X86_TRAP_IRET, SIGILL, "iret exception", regs, error_code,
  614. &info);
  615. }
  616. exception_exit(prev_state);
  617. }
  618. #endif
  619. /* Set of traps needed for early debugging. */
  620. void __init early_trap_init(void)
  621. {
  622. set_intr_gate_ist(X86_TRAP_DB, &debug, DEBUG_STACK);
  623. /* int3 can be called from all */
  624. set_system_intr_gate_ist(X86_TRAP_BP, &int3, DEBUG_STACK);
  625. #ifdef CONFIG_X86_32
  626. set_intr_gate(X86_TRAP_PF, &page_fault);
  627. #endif
  628. load_idt(&idt_descr);
  629. }
  630. void __init early_trap_pf_init(void)
  631. {
  632. #ifdef CONFIG_X86_64
  633. set_intr_gate(X86_TRAP_PF, &page_fault);
  634. #endif
  635. }
  636. void __init trap_init(void)
  637. {
  638. int i;
  639. #ifdef CONFIG_EISA
  640. void __iomem *p = early_ioremap(0x0FFFD9, 4);
  641. if (readl(p) == 'E' + ('I'<<8) + ('S'<<16) + ('A'<<24))
  642. EISA_bus = 1;
  643. early_iounmap(p, 4);
  644. #endif
  645. set_intr_gate(X86_TRAP_DE, &divide_error);
  646. set_intr_gate_ist(X86_TRAP_NMI, &nmi, NMI_STACK);
  647. /* int4 can be called from all */
  648. set_system_intr_gate(X86_TRAP_OF, &overflow);
  649. set_intr_gate(X86_TRAP_BR, &bounds);
  650. set_intr_gate(X86_TRAP_UD, &invalid_op);
  651. set_intr_gate(X86_TRAP_NM, &device_not_available);
  652. #ifdef CONFIG_X86_32
  653. set_task_gate(X86_TRAP_DF, GDT_ENTRY_DOUBLEFAULT_TSS);
  654. #else
  655. set_intr_gate_ist(X86_TRAP_DF, &double_fault, DOUBLEFAULT_STACK);
  656. #endif
  657. set_intr_gate(X86_TRAP_OLD_MF, &coprocessor_segment_overrun);
  658. set_intr_gate(X86_TRAP_TS, &invalid_TSS);
  659. set_intr_gate(X86_TRAP_NP, &segment_not_present);
  660. set_intr_gate_ist(X86_TRAP_SS, &stack_segment, STACKFAULT_STACK);
  661. set_intr_gate(X86_TRAP_GP, &general_protection);
  662. set_intr_gate(X86_TRAP_SPURIOUS, &spurious_interrupt_bug);
  663. set_intr_gate(X86_TRAP_MF, &coprocessor_error);
  664. set_intr_gate(X86_TRAP_AC, &alignment_check);
  665. #ifdef CONFIG_X86_MCE
  666. set_intr_gate_ist(X86_TRAP_MC, &machine_check, MCE_STACK);
  667. #endif
  668. set_intr_gate(X86_TRAP_XF, &simd_coprocessor_error);
  669. /* Reserve all the builtin and the syscall vector: */
  670. for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
  671. set_bit(i, used_vectors);
  672. #ifdef CONFIG_IA32_EMULATION
  673. set_system_intr_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
  674. set_bit(IA32_SYSCALL_VECTOR, used_vectors);
  675. #endif
  676. #ifdef CONFIG_X86_32
  677. set_system_trap_gate(SYSCALL_VECTOR, &system_call);
  678. set_bit(SYSCALL_VECTOR, used_vectors);
  679. #endif
  680. /*
  681. * Set the IDT descriptor to a fixed read-only location, so that the
  682. * "sidt" instruction will not leak the location of the kernel, and
  683. * to defend the IDT against arbitrary memory write vulnerabilities.
  684. * It will be reloaded in cpu_init() */
  685. __set_fixmap(FIX_RO_IDT, __pa_symbol(idt_table), PAGE_KERNEL_RO);
  686. idt_descr.address = fix_to_virt(FIX_RO_IDT);
  687. /*
  688. * Should be a barrier for any external CPU state:
  689. */
  690. cpu_init();
  691. x86_init.irqs.trap_init();
  692. #ifdef CONFIG_X86_64
  693. memcpy(&debug_idt_table, &idt_table, IDT_ENTRIES * 16);
  694. set_nmi_gate(X86_TRAP_DB, &debug);
  695. set_nmi_gate(X86_TRAP_BP, &int3);
  696. #endif
  697. }