traps.c 21 KB

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