traps.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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. #include <linux/interrupt.h>
  12. #include <linux/kallsyms.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/kprobes.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/utsname.h>
  17. #include <linux/kdebug.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/string.h>
  22. #include <linux/unwind.h>
  23. #include <linux/delay.h>
  24. #include <linux/errno.h>
  25. #include <linux/kexec.h>
  26. #include <linux/sched.h>
  27. #include <linux/timer.h>
  28. #include <linux/init.h>
  29. #include <linux/bug.h>
  30. #include <linux/nmi.h>
  31. #include <linux/mm.h>
  32. #include <linux/smp.h>
  33. #include <linux/io.h>
  34. #ifdef CONFIG_EISA
  35. #include <linux/ioport.h>
  36. #include <linux/eisa.h>
  37. #endif
  38. #ifdef CONFIG_MCA
  39. #include <linux/mca.h>
  40. #endif
  41. #if defined(CONFIG_EDAC)
  42. #include <linux/edac.h>
  43. #endif
  44. #include <asm/stacktrace.h>
  45. #include <asm/processor.h>
  46. #include <asm/debugreg.h>
  47. #include <asm/atomic.h>
  48. #include <asm/system.h>
  49. #include <asm/unwind.h>
  50. #include <asm/traps.h>
  51. #include <asm/desc.h>
  52. #include <asm/i387.h>
  53. #include <mach_traps.h>
  54. #ifdef CONFIG_X86_64
  55. #include <asm/pgalloc.h>
  56. #include <asm/proto.h>
  57. #include <asm/pda.h>
  58. #else
  59. #include <asm/processor-flags.h>
  60. #include <asm/arch_hooks.h>
  61. #include <asm/nmi.h>
  62. #include <asm/smp.h>
  63. #include <asm/io.h>
  64. #include <asm/traps.h>
  65. #include "cpu/mcheck/mce.h"
  66. DECLARE_BITMAP(used_vectors, NR_VECTORS);
  67. EXPORT_SYMBOL_GPL(used_vectors);
  68. asmlinkage int system_call(void);
  69. /* Do we ignore FPU interrupts ? */
  70. char ignore_fpu_irq;
  71. /*
  72. * The IDT has to be page-aligned to simplify the Pentium
  73. * F0 0F bug workaround.. We have a special link segment
  74. * for this.
  75. */
  76. gate_desc idt_table[256]
  77. __attribute__((__section__(".data.idt"))) = { { { { 0, 0 } } }, };
  78. #endif
  79. static int ignore_nmis;
  80. static inline void conditional_sti(struct pt_regs *regs)
  81. {
  82. if (regs->flags & X86_EFLAGS_IF)
  83. local_irq_enable();
  84. }
  85. static inline void preempt_conditional_sti(struct pt_regs *regs)
  86. {
  87. inc_preempt_count();
  88. if (regs->flags & X86_EFLAGS_IF)
  89. local_irq_enable();
  90. }
  91. static inline void preempt_conditional_cli(struct pt_regs *regs)
  92. {
  93. if (regs->flags & X86_EFLAGS_IF)
  94. local_irq_disable();
  95. dec_preempt_count();
  96. }
  97. #ifdef CONFIG_X86_32
  98. static inline void
  99. die_if_kernel(const char *str, struct pt_regs *regs, long err)
  100. {
  101. if (!user_mode_vm(regs))
  102. die(str, regs, err);
  103. }
  104. /*
  105. * Perform the lazy TSS's I/O bitmap copy. If the TSS has an
  106. * invalid offset set (the LAZY one) and the faulting thread has
  107. * a valid I/O bitmap pointer, we copy the I/O bitmap in the TSS,
  108. * we set the offset field correctly and return 1.
  109. */
  110. static int lazy_iobitmap_copy(void)
  111. {
  112. struct thread_struct *thread;
  113. struct tss_struct *tss;
  114. int cpu;
  115. cpu = get_cpu();
  116. tss = &per_cpu(init_tss, cpu);
  117. thread = &current->thread;
  118. if (tss->x86_tss.io_bitmap_base == INVALID_IO_BITMAP_OFFSET_LAZY &&
  119. thread->io_bitmap_ptr) {
  120. memcpy(tss->io_bitmap, thread->io_bitmap_ptr,
  121. thread->io_bitmap_max);
  122. /*
  123. * If the previously set map was extending to higher ports
  124. * than the current one, pad extra space with 0xff (no access).
  125. */
  126. if (thread->io_bitmap_max < tss->io_bitmap_max) {
  127. memset((char *) tss->io_bitmap +
  128. thread->io_bitmap_max, 0xff,
  129. tss->io_bitmap_max - thread->io_bitmap_max);
  130. }
  131. tss->io_bitmap_max = thread->io_bitmap_max;
  132. tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
  133. tss->io_bitmap_owner = thread;
  134. put_cpu();
  135. return 1;
  136. }
  137. put_cpu();
  138. return 0;
  139. }
  140. #endif
  141. static void __kprobes
  142. do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
  143. long error_code, siginfo_t *info)
  144. {
  145. struct task_struct *tsk = current;
  146. #ifdef CONFIG_X86_32
  147. if (regs->flags & X86_VM_MASK) {
  148. /*
  149. * traps 0, 1, 3, 4, and 5 should be forwarded to vm86.
  150. * On nmi (interrupt 2), do_trap should not be called.
  151. */
  152. if (trapnr < 6)
  153. goto vm86_trap;
  154. goto trap_signal;
  155. }
  156. #endif
  157. if (!user_mode(regs))
  158. goto kernel_trap;
  159. #ifdef CONFIG_X86_32
  160. trap_signal:
  161. #endif
  162. /*
  163. * We want error_code and trap_no set for userspace faults and
  164. * kernelspace faults which result in die(), but not
  165. * kernelspace faults which are fixed up. die() gives the
  166. * process no chance to handle the signal and notice the
  167. * kernel fault information, so that won't result in polluting
  168. * the information about previously queued, but not yet
  169. * delivered, faults. See also do_general_protection below.
  170. */
  171. tsk->thread.error_code = error_code;
  172. tsk->thread.trap_no = trapnr;
  173. #ifdef CONFIG_X86_64
  174. if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
  175. printk_ratelimit()) {
  176. printk(KERN_INFO
  177. "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
  178. tsk->comm, tsk->pid, str,
  179. regs->ip, regs->sp, error_code);
  180. print_vma_addr(" in ", regs->ip);
  181. printk("\n");
  182. }
  183. #endif
  184. if (info)
  185. force_sig_info(signr, info, tsk);
  186. else
  187. force_sig(signr, tsk);
  188. return;
  189. kernel_trap:
  190. if (!fixup_exception(regs)) {
  191. tsk->thread.error_code = error_code;
  192. tsk->thread.trap_no = trapnr;
  193. die(str, regs, error_code);
  194. }
  195. return;
  196. #ifdef CONFIG_X86_32
  197. vm86_trap:
  198. if (handle_vm86_trap((struct kernel_vm86_regs *) regs,
  199. error_code, trapnr))
  200. goto trap_signal;
  201. return;
  202. #endif
  203. }
  204. #define DO_ERROR(trapnr, signr, str, name) \
  205. dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
  206. { \
  207. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  208. == NOTIFY_STOP) \
  209. return; \
  210. conditional_sti(regs); \
  211. do_trap(trapnr, signr, str, regs, error_code, NULL); \
  212. }
  213. #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
  214. dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
  215. { \
  216. siginfo_t info; \
  217. info.si_signo = signr; \
  218. info.si_errno = 0; \
  219. info.si_code = sicode; \
  220. info.si_addr = (void __user *)siaddr; \
  221. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  222. == NOTIFY_STOP) \
  223. return; \
  224. conditional_sti(regs); \
  225. do_trap(trapnr, signr, str, regs, error_code, &info); \
  226. }
  227. DO_ERROR_INFO(0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
  228. DO_ERROR(4, SIGSEGV, "overflow", overflow)
  229. DO_ERROR(5, SIGSEGV, "bounds", bounds)
  230. DO_ERROR_INFO(6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
  231. DO_ERROR(9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
  232. DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
  233. DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
  234. #ifdef CONFIG_X86_32
  235. DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
  236. #endif
  237. DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
  238. #ifdef CONFIG_X86_64
  239. /* Runs on IST stack */
  240. dotraplinkage void do_stack_segment(struct pt_regs *regs, long error_code)
  241. {
  242. if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
  243. 12, SIGBUS) == NOTIFY_STOP)
  244. return;
  245. preempt_conditional_sti(regs);
  246. do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
  247. preempt_conditional_cli(regs);
  248. }
  249. dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code)
  250. {
  251. static const char str[] = "double fault";
  252. struct task_struct *tsk = current;
  253. /* Return not checked because double check cannot be ignored */
  254. notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
  255. tsk->thread.error_code = error_code;
  256. tsk->thread.trap_no = 8;
  257. /* This is always a kernel trap and never fixable (and thus must
  258. never return). */
  259. for (;;)
  260. die(str, regs, error_code);
  261. }
  262. #endif
  263. dotraplinkage void __kprobes
  264. do_general_protection(struct pt_regs *regs, long error_code)
  265. {
  266. struct task_struct *tsk;
  267. conditional_sti(regs);
  268. #ifdef CONFIG_X86_32
  269. if (lazy_iobitmap_copy()) {
  270. /* restart the faulting instruction */
  271. return;
  272. }
  273. if (regs->flags & X86_VM_MASK)
  274. goto gp_in_vm86;
  275. #endif
  276. tsk = current;
  277. if (!user_mode(regs))
  278. goto gp_in_kernel;
  279. tsk->thread.error_code = error_code;
  280. tsk->thread.trap_no = 13;
  281. if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
  282. printk_ratelimit()) {
  283. printk(KERN_INFO
  284. "%s[%d] general protection ip:%lx sp:%lx error:%lx",
  285. tsk->comm, task_pid_nr(tsk),
  286. regs->ip, regs->sp, error_code);
  287. print_vma_addr(" in ", regs->ip);
  288. printk("\n");
  289. }
  290. force_sig(SIGSEGV, tsk);
  291. return;
  292. #ifdef CONFIG_X86_32
  293. gp_in_vm86:
  294. local_irq_enable();
  295. handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
  296. return;
  297. #endif
  298. gp_in_kernel:
  299. if (fixup_exception(regs))
  300. return;
  301. tsk->thread.error_code = error_code;
  302. tsk->thread.trap_no = 13;
  303. if (notify_die(DIE_GPF, "general protection fault", regs,
  304. error_code, 13, SIGSEGV) == NOTIFY_STOP)
  305. return;
  306. die("general protection fault", regs, error_code);
  307. }
  308. static notrace __kprobes void
  309. mem_parity_error(unsigned char reason, struct pt_regs *regs)
  310. {
  311. printk(KERN_EMERG
  312. "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
  313. reason, smp_processor_id());
  314. printk(KERN_EMERG
  315. "You have some hardware problem, likely on the PCI bus.\n");
  316. #if defined(CONFIG_EDAC)
  317. if (edac_handler_set()) {
  318. edac_atomic_assert_error();
  319. return;
  320. }
  321. #endif
  322. if (panic_on_unrecovered_nmi)
  323. panic("NMI: Not continuing");
  324. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  325. /* Clear and disable the memory parity error line. */
  326. reason = (reason & 0xf) | 4;
  327. outb(reason, 0x61);
  328. }
  329. static notrace __kprobes void
  330. io_check_error(unsigned char reason, struct pt_regs *regs)
  331. {
  332. unsigned long i;
  333. printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
  334. show_registers(regs);
  335. /* Re-enable the IOCK line, wait for a few seconds */
  336. reason = (reason & 0xf) | 8;
  337. outb(reason, 0x61);
  338. i = 2000;
  339. while (--i)
  340. udelay(1000);
  341. reason &= ~8;
  342. outb(reason, 0x61);
  343. }
  344. static notrace __kprobes void
  345. unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
  346. {
  347. if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) ==
  348. NOTIFY_STOP)
  349. return;
  350. #ifdef CONFIG_MCA
  351. /*
  352. * Might actually be able to figure out what the guilty party
  353. * is:
  354. */
  355. if (MCA_bus) {
  356. mca_handle_nmi();
  357. return;
  358. }
  359. #endif
  360. printk(KERN_EMERG
  361. "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
  362. reason, smp_processor_id());
  363. printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
  364. if (panic_on_unrecovered_nmi)
  365. panic("NMI: Not continuing");
  366. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  367. }
  368. static notrace __kprobes void default_do_nmi(struct pt_regs *regs)
  369. {
  370. unsigned char reason = 0;
  371. int cpu;
  372. cpu = smp_processor_id();
  373. /* Only the BSP gets external NMIs from the system. */
  374. if (!cpu)
  375. reason = get_nmi_reason();
  376. if (!(reason & 0xc0)) {
  377. if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
  378. == NOTIFY_STOP)
  379. return;
  380. #ifdef CONFIG_X86_LOCAL_APIC
  381. /*
  382. * Ok, so this is none of the documented NMI sources,
  383. * so it must be the NMI watchdog.
  384. */
  385. if (nmi_watchdog_tick(regs, reason))
  386. return;
  387. if (!do_nmi_callback(regs, cpu))
  388. unknown_nmi_error(reason, regs);
  389. #else
  390. unknown_nmi_error(reason, regs);
  391. #endif
  392. return;
  393. }
  394. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
  395. return;
  396. /* AK: following checks seem to be broken on modern chipsets. FIXME */
  397. if (reason & 0x80)
  398. mem_parity_error(reason, regs);
  399. if (reason & 0x40)
  400. io_check_error(reason, regs);
  401. #ifdef CONFIG_X86_32
  402. /*
  403. * Reassert NMI in case it became active meanwhile
  404. * as it's edge-triggered:
  405. */
  406. reassert_nmi();
  407. #endif
  408. }
  409. dotraplinkage notrace __kprobes void
  410. do_nmi(struct pt_regs *regs, long error_code)
  411. {
  412. nmi_enter();
  413. #ifdef CONFIG_X86_32
  414. { int cpu; cpu = smp_processor_id(); ++nmi_count(cpu); }
  415. #else
  416. add_pda(__nmi_count, 1);
  417. #endif
  418. if (!ignore_nmis)
  419. default_do_nmi(regs);
  420. nmi_exit();
  421. }
  422. void stop_nmi(void)
  423. {
  424. acpi_nmi_disable();
  425. ignore_nmis++;
  426. }
  427. void restart_nmi(void)
  428. {
  429. ignore_nmis--;
  430. acpi_nmi_enable();
  431. }
  432. /* May run on IST stack. */
  433. dotraplinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)
  434. {
  435. #ifdef CONFIG_KPROBES
  436. if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
  437. == NOTIFY_STOP)
  438. return;
  439. #else
  440. if (notify_die(DIE_TRAP, "int3", regs, error_code, 3, SIGTRAP)
  441. == NOTIFY_STOP)
  442. return;
  443. #endif
  444. preempt_conditional_sti(regs);
  445. do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
  446. preempt_conditional_cli(regs);
  447. }
  448. #ifdef CONFIG_X86_64
  449. /* Help handler running on IST stack to switch back to user stack
  450. for scheduling or signal handling. The actual stack switch is done in
  451. entry.S */
  452. asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
  453. {
  454. struct pt_regs *regs = eregs;
  455. /* Did already sync */
  456. if (eregs == (struct pt_regs *)eregs->sp)
  457. ;
  458. /* Exception from user space */
  459. else if (user_mode(eregs))
  460. regs = task_pt_regs(current);
  461. /* Exception from kernel and interrupts are enabled. Move to
  462. kernel process stack. */
  463. else if (eregs->flags & X86_EFLAGS_IF)
  464. regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
  465. if (eregs != regs)
  466. *regs = *eregs;
  467. return regs;
  468. }
  469. #endif
  470. /*
  471. * Our handling of the processor debug registers is non-trivial.
  472. * We do not clear them on entry and exit from the kernel. Therefore
  473. * it is possible to get a watchpoint trap here from inside the kernel.
  474. * However, the code in ./ptrace.c has ensured that the user can
  475. * only set watchpoints on userspace addresses. Therefore the in-kernel
  476. * watchpoint trap can only occur in code which is reading/writing
  477. * from user space. Such code must not hold kernel locks (since it
  478. * can equally take a page fault), therefore it is safe to call
  479. * force_sig_info even though that claims and releases locks.
  480. *
  481. * Code in ./signal.c ensures that the debug control register
  482. * is restored before we deliver any signal, and therefore that
  483. * user code runs with the correct debug control register even though
  484. * we clear it here.
  485. *
  486. * Being careful here means that we don't have to be as careful in a
  487. * lot of more complicated places (task switching can be a bit lazy
  488. * about restoring all the debug state, and ptrace doesn't have to
  489. * find every occurrence of the TF bit that could be saved away even
  490. * by user code)
  491. *
  492. * May run on IST stack.
  493. */
  494. dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code)
  495. {
  496. struct task_struct *tsk = current;
  497. unsigned long condition;
  498. int si_code;
  499. get_debugreg(condition, 6);
  500. /*
  501. * The processor cleared BTF, so don't mark that we need it set.
  502. */
  503. clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
  504. tsk->thread.debugctlmsr = 0;
  505. if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
  506. SIGTRAP) == NOTIFY_STOP)
  507. return;
  508. /* It's safe to allow irq's after DR6 has been saved */
  509. preempt_conditional_sti(regs);
  510. /* Mask out spurious debug traps due to lazy DR7 setting */
  511. if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
  512. if (!tsk->thread.debugreg7)
  513. goto clear_dr7;
  514. }
  515. #ifdef CONFIG_X86_32
  516. if (regs->flags & X86_VM_MASK)
  517. goto debug_vm86;
  518. #endif
  519. /* Save debug status register where ptrace can see it */
  520. tsk->thread.debugreg6 = condition;
  521. /*
  522. * Single-stepping through TF: make sure we ignore any events in
  523. * kernel space (but re-enable TF when returning to user mode).
  524. */
  525. if (condition & DR_STEP) {
  526. if (!user_mode(regs))
  527. goto clear_TF_reenable;
  528. }
  529. si_code = get_si_code(condition);
  530. /* Ok, finally something we can handle */
  531. send_sigtrap(tsk, regs, error_code, si_code);
  532. /*
  533. * Disable additional traps. They'll be re-enabled when
  534. * the signal is delivered.
  535. */
  536. clear_dr7:
  537. set_debugreg(0, 7);
  538. preempt_conditional_cli(regs);
  539. return;
  540. #ifdef CONFIG_X86_32
  541. debug_vm86:
  542. handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
  543. preempt_conditional_cli(regs);
  544. return;
  545. #endif
  546. clear_TF_reenable:
  547. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  548. regs->flags &= ~X86_EFLAGS_TF;
  549. preempt_conditional_cli(regs);
  550. return;
  551. }
  552. #ifdef CONFIG_X86_64
  553. static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
  554. {
  555. if (fixup_exception(regs))
  556. return 1;
  557. notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
  558. /* Illegal floating point operation in the kernel */
  559. current->thread.trap_no = trapnr;
  560. die(str, regs, 0);
  561. return 0;
  562. }
  563. #endif
  564. /*
  565. * Note that we play around with the 'TS' bit in an attempt to get
  566. * the correct behaviour even in the presence of the asynchronous
  567. * IRQ13 behaviour
  568. */
  569. void math_error(void __user *ip)
  570. {
  571. struct task_struct *task;
  572. siginfo_t info;
  573. unsigned short cwd, swd;
  574. /*
  575. * Save the info for the exception handler and clear the error.
  576. */
  577. task = current;
  578. save_init_fpu(task);
  579. task->thread.trap_no = 16;
  580. task->thread.error_code = 0;
  581. info.si_signo = SIGFPE;
  582. info.si_errno = 0;
  583. info.si_code = __SI_FAULT;
  584. info.si_addr = ip;
  585. /*
  586. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  587. * status. 0x3f is the exception bits in these regs, 0x200 is the
  588. * C1 reg you need in case of a stack fault, 0x040 is the stack
  589. * fault bit. We should only be taking one exception at a time,
  590. * so if this combination doesn't produce any single exception,
  591. * then we have a bad program that isn't synchronizing its FPU usage
  592. * and it will suffer the consequences since we won't be able to
  593. * fully reproduce the context of the exception
  594. */
  595. cwd = get_fpu_cwd(task);
  596. swd = get_fpu_swd(task);
  597. switch (swd & ~cwd & 0x3f) {
  598. case 0x000: /* No unmasked exception */
  599. #ifdef CONFIG_X86_32
  600. return;
  601. #endif
  602. default: /* Multiple exceptions */
  603. break;
  604. case 0x001: /* Invalid Op */
  605. /*
  606. * swd & 0x240 == 0x040: Stack Underflow
  607. * swd & 0x240 == 0x240: Stack Overflow
  608. * User must clear the SF bit (0x40) if set
  609. */
  610. info.si_code = FPE_FLTINV;
  611. break;
  612. case 0x002: /* Denormalize */
  613. case 0x010: /* Underflow */
  614. info.si_code = FPE_FLTUND;
  615. break;
  616. case 0x004: /* Zero Divide */
  617. info.si_code = FPE_FLTDIV;
  618. break;
  619. case 0x008: /* Overflow */
  620. info.si_code = FPE_FLTOVF;
  621. break;
  622. case 0x020: /* Precision */
  623. info.si_code = FPE_FLTRES;
  624. break;
  625. }
  626. force_sig_info(SIGFPE, &info, task);
  627. }
  628. dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
  629. {
  630. conditional_sti(regs);
  631. #ifdef CONFIG_X86_32
  632. ignore_fpu_irq = 1;
  633. #else
  634. if (!user_mode(regs) &&
  635. kernel_math_error(regs, "kernel x87 math error", 16))
  636. return;
  637. #endif
  638. math_error((void __user *)regs->ip);
  639. }
  640. static void simd_math_error(void __user *ip)
  641. {
  642. struct task_struct *task;
  643. siginfo_t info;
  644. unsigned short mxcsr;
  645. /*
  646. * Save the info for the exception handler and clear the error.
  647. */
  648. task = current;
  649. save_init_fpu(task);
  650. task->thread.trap_no = 19;
  651. task->thread.error_code = 0;
  652. info.si_signo = SIGFPE;
  653. info.si_errno = 0;
  654. info.si_code = __SI_FAULT;
  655. info.si_addr = ip;
  656. /*
  657. * The SIMD FPU exceptions are handled a little differently, as there
  658. * is only a single status/control register. Thus, to determine which
  659. * unmasked exception was caught we must mask the exception mask bits
  660. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  661. */
  662. mxcsr = get_fpu_mxcsr(task);
  663. switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
  664. case 0x000:
  665. default:
  666. break;
  667. case 0x001: /* Invalid Op */
  668. info.si_code = FPE_FLTINV;
  669. break;
  670. case 0x002: /* Denormalize */
  671. case 0x010: /* Underflow */
  672. info.si_code = FPE_FLTUND;
  673. break;
  674. case 0x004: /* Zero Divide */
  675. info.si_code = FPE_FLTDIV;
  676. break;
  677. case 0x008: /* Overflow */
  678. info.si_code = FPE_FLTOVF;
  679. break;
  680. case 0x020: /* Precision */
  681. info.si_code = FPE_FLTRES;
  682. break;
  683. }
  684. force_sig_info(SIGFPE, &info, task);
  685. }
  686. dotraplinkage void
  687. do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
  688. {
  689. conditional_sti(regs);
  690. #ifdef CONFIG_X86_32
  691. if (cpu_has_xmm) {
  692. /* Handle SIMD FPU exceptions on PIII+ processors. */
  693. ignore_fpu_irq = 1;
  694. simd_math_error((void __user *)regs->ip);
  695. return;
  696. }
  697. /*
  698. * Handle strange cache flush from user space exception
  699. * in all other cases. This is undocumented behaviour.
  700. */
  701. if (regs->flags & X86_VM_MASK) {
  702. handle_vm86_fault((struct kernel_vm86_regs *)regs, error_code);
  703. return;
  704. }
  705. current->thread.trap_no = 19;
  706. current->thread.error_code = error_code;
  707. die_if_kernel("cache flush denied", regs, error_code);
  708. force_sig(SIGSEGV, current);
  709. #else
  710. if (!user_mode(regs) &&
  711. kernel_math_error(regs, "kernel simd math error", 19))
  712. return;
  713. simd_math_error((void __user *)regs->ip);
  714. #endif
  715. }
  716. dotraplinkage void
  717. do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
  718. {
  719. conditional_sti(regs);
  720. #if 0
  721. /* No need to warn about this any longer. */
  722. printk(KERN_INFO "Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
  723. #endif
  724. }
  725. #ifdef CONFIG_X86_32
  726. unsigned long patch_espfix_desc(unsigned long uesp, unsigned long kesp)
  727. {
  728. struct desc_struct *gdt = get_cpu_gdt_table(smp_processor_id());
  729. unsigned long base = (kesp - uesp) & -THREAD_SIZE;
  730. unsigned long new_kesp = kesp - base;
  731. unsigned long lim_pages = (new_kesp | (THREAD_SIZE - 1)) >> PAGE_SHIFT;
  732. __u64 desc = *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS];
  733. /* Set up base for espfix segment */
  734. desc &= 0x00f0ff0000000000ULL;
  735. desc |= ((((__u64)base) << 16) & 0x000000ffffff0000ULL) |
  736. ((((__u64)base) << 32) & 0xff00000000000000ULL) |
  737. ((((__u64)lim_pages) << 32) & 0x000f000000000000ULL) |
  738. (lim_pages & 0xffff);
  739. *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS] = desc;
  740. return new_kesp;
  741. }
  742. #else
  743. asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
  744. {
  745. }
  746. asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
  747. {
  748. }
  749. #endif
  750. /*
  751. * 'math_state_restore()' saves the current math information in the
  752. * old math state array, and gets the new ones from the current task
  753. *
  754. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  755. * Don't touch unless you *really* know how it works.
  756. *
  757. * Must be called with kernel preemption disabled (in this case,
  758. * local interrupts are disabled at the call-site in entry.S).
  759. */
  760. asmlinkage void math_state_restore(void)
  761. {
  762. struct thread_info *thread = current_thread_info();
  763. struct task_struct *tsk = thread->task;
  764. if (!tsk_used_math(tsk)) {
  765. local_irq_enable();
  766. /*
  767. * does a slab alloc which can sleep
  768. */
  769. if (init_fpu(tsk)) {
  770. /*
  771. * ran out of memory!
  772. */
  773. do_group_exit(SIGKILL);
  774. return;
  775. }
  776. local_irq_disable();
  777. }
  778. clts(); /* Allow maths ops (or we recurse) */
  779. #ifdef CONFIG_X86_32
  780. restore_fpu(tsk);
  781. #else
  782. /*
  783. * Paranoid restore. send a SIGSEGV if we fail to restore the state.
  784. */
  785. if (unlikely(restore_fpu_checking(tsk))) {
  786. stts();
  787. force_sig(SIGSEGV, tsk);
  788. return;
  789. }
  790. #endif
  791. thread->status |= TS_USEDFPU; /* So we fnsave on switch_to() */
  792. tsk->fpu_counter++;
  793. }
  794. EXPORT_SYMBOL_GPL(math_state_restore);
  795. #ifndef CONFIG_MATH_EMULATION
  796. asmlinkage void math_emulate(long arg)
  797. {
  798. printk(KERN_EMERG
  799. "math-emulation not enabled and no coprocessor found.\n");
  800. printk(KERN_EMERG "killing %s.\n", current->comm);
  801. force_sig(SIGFPE, current);
  802. schedule();
  803. }
  804. #endif /* CONFIG_MATH_EMULATION */
  805. dotraplinkage void __kprobes
  806. do_device_not_available(struct pt_regs *regs, long error)
  807. {
  808. #ifdef CONFIG_X86_32
  809. if (read_cr0() & X86_CR0_EM) {
  810. conditional_sti(regs);
  811. math_emulate(0);
  812. } else {
  813. math_state_restore(); /* interrupts still off */
  814. conditional_sti(regs);
  815. }
  816. #else
  817. math_state_restore();
  818. #endif
  819. }
  820. #ifdef CONFIG_X86_32
  821. dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
  822. {
  823. siginfo_t info;
  824. local_irq_enable();
  825. info.si_signo = SIGILL;
  826. info.si_errno = 0;
  827. info.si_code = ILL_BADSTK;
  828. info.si_addr = 0;
  829. if (notify_die(DIE_TRAP, "iret exception",
  830. regs, error_code, 32, SIGILL) == NOTIFY_STOP)
  831. return;
  832. do_trap(32, SIGILL, "iret exception", regs, error_code, &info);
  833. }
  834. #endif
  835. void __init trap_init(void)
  836. {
  837. #ifdef CONFIG_X86_32
  838. int i;
  839. #endif
  840. #ifdef CONFIG_EISA
  841. void __iomem *p = early_ioremap(0x0FFFD9, 4);
  842. if (readl(p) == 'E' + ('I'<<8) + ('S'<<16) + ('A'<<24))
  843. EISA_bus = 1;
  844. early_iounmap(p, 4);
  845. #endif
  846. set_intr_gate(0, &divide_error);
  847. set_intr_gate_ist(1, &debug, DEBUG_STACK);
  848. set_intr_gate_ist(2, &nmi, NMI_STACK);
  849. /* int3 can be called from all */
  850. set_system_intr_gate_ist(3, &int3, DEBUG_STACK);
  851. /* int4 can be called from all */
  852. set_system_intr_gate(4, &overflow);
  853. set_intr_gate(5, &bounds);
  854. set_intr_gate(6, &invalid_op);
  855. set_intr_gate(7, &device_not_available);
  856. #ifdef CONFIG_X86_32
  857. set_task_gate(8, GDT_ENTRY_DOUBLEFAULT_TSS);
  858. #else
  859. set_intr_gate_ist(8, &double_fault, DOUBLEFAULT_STACK);
  860. #endif
  861. set_intr_gate(9, &coprocessor_segment_overrun);
  862. set_intr_gate(10, &invalid_TSS);
  863. set_intr_gate(11, &segment_not_present);
  864. set_intr_gate_ist(12, &stack_segment, STACKFAULT_STACK);
  865. set_intr_gate(13, &general_protection);
  866. set_intr_gate(14, &page_fault);
  867. set_intr_gate(15, &spurious_interrupt_bug);
  868. set_intr_gate(16, &coprocessor_error);
  869. set_intr_gate(17, &alignment_check);
  870. #ifdef CONFIG_X86_MCE
  871. set_intr_gate_ist(18, &machine_check, MCE_STACK);
  872. #endif
  873. set_intr_gate(19, &simd_coprocessor_error);
  874. #ifdef CONFIG_IA32_EMULATION
  875. set_system_intr_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
  876. #endif
  877. #ifdef CONFIG_X86_32
  878. if (cpu_has_fxsr) {
  879. printk(KERN_INFO "Enabling fast FPU save and restore... ");
  880. set_in_cr4(X86_CR4_OSFXSR);
  881. printk("done.\n");
  882. }
  883. if (cpu_has_xmm) {
  884. printk(KERN_INFO
  885. "Enabling unmasked SIMD FPU exception support... ");
  886. set_in_cr4(X86_CR4_OSXMMEXCPT);
  887. printk("done.\n");
  888. }
  889. set_system_trap_gate(SYSCALL_VECTOR, &system_call);
  890. /* Reserve all the builtin and the syscall vector: */
  891. for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
  892. set_bit(i, used_vectors);
  893. set_bit(SYSCALL_VECTOR, used_vectors);
  894. #endif
  895. /*
  896. * Should be a barrier for any external CPU state:
  897. */
  898. cpu_init();
  899. #ifdef CONFIG_X86_32
  900. trap_init_hook();
  901. #endif
  902. }