traps.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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. inc_irq_stat(__nmi_count);
  414. if (!ignore_nmis)
  415. default_do_nmi(regs);
  416. nmi_exit();
  417. }
  418. void stop_nmi(void)
  419. {
  420. acpi_nmi_disable();
  421. ignore_nmis++;
  422. }
  423. void restart_nmi(void)
  424. {
  425. ignore_nmis--;
  426. acpi_nmi_enable();
  427. }
  428. /* May run on IST stack. */
  429. dotraplinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)
  430. {
  431. #ifdef CONFIG_KPROBES
  432. if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
  433. == NOTIFY_STOP)
  434. return;
  435. #else
  436. if (notify_die(DIE_TRAP, "int3", regs, error_code, 3, SIGTRAP)
  437. == NOTIFY_STOP)
  438. return;
  439. #endif
  440. preempt_conditional_sti(regs);
  441. do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
  442. preempt_conditional_cli(regs);
  443. }
  444. #ifdef CONFIG_X86_64
  445. /* Help handler running on IST stack to switch back to user stack
  446. for scheduling or signal handling. The actual stack switch is done in
  447. entry.S */
  448. asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
  449. {
  450. struct pt_regs *regs = eregs;
  451. /* Did already sync */
  452. if (eregs == (struct pt_regs *)eregs->sp)
  453. ;
  454. /* Exception from user space */
  455. else if (user_mode(eregs))
  456. regs = task_pt_regs(current);
  457. /* Exception from kernel and interrupts are enabled. Move to
  458. kernel process stack. */
  459. else if (eregs->flags & X86_EFLAGS_IF)
  460. regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
  461. if (eregs != regs)
  462. *regs = *eregs;
  463. return regs;
  464. }
  465. #endif
  466. /*
  467. * Our handling of the processor debug registers is non-trivial.
  468. * We do not clear them on entry and exit from the kernel. Therefore
  469. * it is possible to get a watchpoint trap here from inside the kernel.
  470. * However, the code in ./ptrace.c has ensured that the user can
  471. * only set watchpoints on userspace addresses. Therefore the in-kernel
  472. * watchpoint trap can only occur in code which is reading/writing
  473. * from user space. Such code must not hold kernel locks (since it
  474. * can equally take a page fault), therefore it is safe to call
  475. * force_sig_info even though that claims and releases locks.
  476. *
  477. * Code in ./signal.c ensures that the debug control register
  478. * is restored before we deliver any signal, and therefore that
  479. * user code runs with the correct debug control register even though
  480. * we clear it here.
  481. *
  482. * Being careful here means that we don't have to be as careful in a
  483. * lot of more complicated places (task switching can be a bit lazy
  484. * about restoring all the debug state, and ptrace doesn't have to
  485. * find every occurrence of the TF bit that could be saved away even
  486. * by user code)
  487. *
  488. * May run on IST stack.
  489. */
  490. dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code)
  491. {
  492. struct task_struct *tsk = current;
  493. unsigned long condition;
  494. int si_code;
  495. get_debugreg(condition, 6);
  496. /*
  497. * The processor cleared BTF, so don't mark that we need it set.
  498. */
  499. clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
  500. tsk->thread.debugctlmsr = 0;
  501. if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
  502. SIGTRAP) == NOTIFY_STOP)
  503. return;
  504. /* It's safe to allow irq's after DR6 has been saved */
  505. preempt_conditional_sti(regs);
  506. /* Mask out spurious debug traps due to lazy DR7 setting */
  507. if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
  508. if (!tsk->thread.debugreg7)
  509. goto clear_dr7;
  510. }
  511. #ifdef CONFIG_X86_32
  512. if (regs->flags & X86_VM_MASK)
  513. goto debug_vm86;
  514. #endif
  515. /* Save debug status register where ptrace can see it */
  516. tsk->thread.debugreg6 = condition;
  517. /*
  518. * Single-stepping through TF: make sure we ignore any events in
  519. * kernel space (but re-enable TF when returning to user mode).
  520. */
  521. if (condition & DR_STEP) {
  522. if (!user_mode(regs))
  523. goto clear_TF_reenable;
  524. }
  525. si_code = get_si_code(condition);
  526. /* Ok, finally something we can handle */
  527. send_sigtrap(tsk, regs, error_code, si_code);
  528. /*
  529. * Disable additional traps. They'll be re-enabled when
  530. * the signal is delivered.
  531. */
  532. clear_dr7:
  533. set_debugreg(0, 7);
  534. preempt_conditional_cli(regs);
  535. return;
  536. #ifdef CONFIG_X86_32
  537. debug_vm86:
  538. handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
  539. preempt_conditional_cli(regs);
  540. return;
  541. #endif
  542. clear_TF_reenable:
  543. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  544. regs->flags &= ~X86_EFLAGS_TF;
  545. preempt_conditional_cli(regs);
  546. return;
  547. }
  548. #ifdef CONFIG_X86_64
  549. static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
  550. {
  551. if (fixup_exception(regs))
  552. return 1;
  553. notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
  554. /* Illegal floating point operation in the kernel */
  555. current->thread.trap_no = trapnr;
  556. die(str, regs, 0);
  557. return 0;
  558. }
  559. #endif
  560. /*
  561. * Note that we play around with the 'TS' bit in an attempt to get
  562. * the correct behaviour even in the presence of the asynchronous
  563. * IRQ13 behaviour
  564. */
  565. void math_error(void __user *ip)
  566. {
  567. struct task_struct *task;
  568. siginfo_t info;
  569. unsigned short cwd, swd, err;
  570. /*
  571. * Save the info for the exception handler and clear the error.
  572. */
  573. task = current;
  574. save_init_fpu(task);
  575. task->thread.trap_no = 16;
  576. task->thread.error_code = 0;
  577. info.si_signo = SIGFPE;
  578. info.si_errno = 0;
  579. info.si_addr = ip;
  580. /*
  581. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  582. * status. 0x3f is the exception bits in these regs, 0x200 is the
  583. * C1 reg you need in case of a stack fault, 0x040 is the stack
  584. * fault bit. We should only be taking one exception at a time,
  585. * so if this combination doesn't produce any single exception,
  586. * then we have a bad program that isn't synchronizing its FPU usage
  587. * and it will suffer the consequences since we won't be able to
  588. * fully reproduce the context of the exception
  589. */
  590. cwd = get_fpu_cwd(task);
  591. swd = get_fpu_swd(task);
  592. err = swd & ~cwd & 0x3f;
  593. #if CONFIG_X86_32
  594. if (!err)
  595. return;
  596. #endif
  597. if (err & 0x001) { /* Invalid op */
  598. /*
  599. * swd & 0x240 == 0x040: Stack Underflow
  600. * swd & 0x240 == 0x240: Stack Overflow
  601. * User must clear the SF bit (0x40) if set
  602. */
  603. info.si_code = FPE_FLTINV;
  604. } else if (err & 0x004) { /* Divide by Zero */
  605. info.si_code = FPE_FLTDIV;
  606. } else if (err & 0x008) { /* Overflow */
  607. info.si_code = FPE_FLTOVF;
  608. } else if (err & 0x012) { /* Denormal, Underflow */
  609. info.si_code = FPE_FLTUND;
  610. } else if (err & 0x020) { /* Precision */
  611. info.si_code = FPE_FLTRES;
  612. } else {
  613. info.si_code = __SI_FAULT|SI_KERNEL; /* WTF? */
  614. }
  615. force_sig_info(SIGFPE, &info, task);
  616. }
  617. dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
  618. {
  619. conditional_sti(regs);
  620. #ifdef CONFIG_X86_32
  621. ignore_fpu_irq = 1;
  622. #else
  623. if (!user_mode(regs) &&
  624. kernel_math_error(regs, "kernel x87 math error", 16))
  625. return;
  626. #endif
  627. math_error((void __user *)regs->ip);
  628. }
  629. static void simd_math_error(void __user *ip)
  630. {
  631. struct task_struct *task;
  632. siginfo_t info;
  633. unsigned short mxcsr;
  634. /*
  635. * Save the info for the exception handler and clear the error.
  636. */
  637. task = current;
  638. save_init_fpu(task);
  639. task->thread.trap_no = 19;
  640. task->thread.error_code = 0;
  641. info.si_signo = SIGFPE;
  642. info.si_errno = 0;
  643. info.si_code = __SI_FAULT;
  644. info.si_addr = ip;
  645. /*
  646. * The SIMD FPU exceptions are handled a little differently, as there
  647. * is only a single status/control register. Thus, to determine which
  648. * unmasked exception was caught we must mask the exception mask bits
  649. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  650. */
  651. mxcsr = get_fpu_mxcsr(task);
  652. switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
  653. case 0x000:
  654. default:
  655. break;
  656. case 0x001: /* Invalid Op */
  657. info.si_code = FPE_FLTINV;
  658. break;
  659. case 0x002: /* Denormalize */
  660. case 0x010: /* Underflow */
  661. info.si_code = FPE_FLTUND;
  662. break;
  663. case 0x004: /* Zero Divide */
  664. info.si_code = FPE_FLTDIV;
  665. break;
  666. case 0x008: /* Overflow */
  667. info.si_code = FPE_FLTOVF;
  668. break;
  669. case 0x020: /* Precision */
  670. info.si_code = FPE_FLTRES;
  671. break;
  672. }
  673. force_sig_info(SIGFPE, &info, task);
  674. }
  675. dotraplinkage void
  676. do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
  677. {
  678. conditional_sti(regs);
  679. #ifdef CONFIG_X86_32
  680. if (cpu_has_xmm) {
  681. /* Handle SIMD FPU exceptions on PIII+ processors. */
  682. ignore_fpu_irq = 1;
  683. simd_math_error((void __user *)regs->ip);
  684. return;
  685. }
  686. /*
  687. * Handle strange cache flush from user space exception
  688. * in all other cases. This is undocumented behaviour.
  689. */
  690. if (regs->flags & X86_VM_MASK) {
  691. handle_vm86_fault((struct kernel_vm86_regs *)regs, error_code);
  692. return;
  693. }
  694. current->thread.trap_no = 19;
  695. current->thread.error_code = error_code;
  696. die_if_kernel("cache flush denied", regs, error_code);
  697. force_sig(SIGSEGV, current);
  698. #else
  699. if (!user_mode(regs) &&
  700. kernel_math_error(regs, "kernel simd math error", 19))
  701. return;
  702. simd_math_error((void __user *)regs->ip);
  703. #endif
  704. }
  705. dotraplinkage void
  706. do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
  707. {
  708. conditional_sti(regs);
  709. #if 0
  710. /* No need to warn about this any longer. */
  711. printk(KERN_INFO "Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
  712. #endif
  713. }
  714. #ifdef CONFIG_X86_32
  715. unsigned long patch_espfix_desc(unsigned long uesp, unsigned long kesp)
  716. {
  717. struct desc_struct *gdt = get_cpu_gdt_table(smp_processor_id());
  718. unsigned long base = (kesp - uesp) & -THREAD_SIZE;
  719. unsigned long new_kesp = kesp - base;
  720. unsigned long lim_pages = (new_kesp | (THREAD_SIZE - 1)) >> PAGE_SHIFT;
  721. __u64 desc = *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS];
  722. /* Set up base for espfix segment */
  723. desc &= 0x00f0ff0000000000ULL;
  724. desc |= ((((__u64)base) << 16) & 0x000000ffffff0000ULL) |
  725. ((((__u64)base) << 32) & 0xff00000000000000ULL) |
  726. ((((__u64)lim_pages) << 32) & 0x000f000000000000ULL) |
  727. (lim_pages & 0xffff);
  728. *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS] = desc;
  729. return new_kesp;
  730. }
  731. #else
  732. asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
  733. {
  734. }
  735. asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
  736. {
  737. }
  738. #endif
  739. /*
  740. * 'math_state_restore()' saves the current math information in the
  741. * old math state array, and gets the new ones from the current task
  742. *
  743. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  744. * Don't touch unless you *really* know how it works.
  745. *
  746. * Must be called with kernel preemption disabled (in this case,
  747. * local interrupts are disabled at the call-site in entry.S).
  748. */
  749. asmlinkage void math_state_restore(void)
  750. {
  751. struct thread_info *thread = current_thread_info();
  752. struct task_struct *tsk = thread->task;
  753. if (!tsk_used_math(tsk)) {
  754. local_irq_enable();
  755. /*
  756. * does a slab alloc which can sleep
  757. */
  758. if (init_fpu(tsk)) {
  759. /*
  760. * ran out of memory!
  761. */
  762. do_group_exit(SIGKILL);
  763. return;
  764. }
  765. local_irq_disable();
  766. }
  767. clts(); /* Allow maths ops (or we recurse) */
  768. #ifdef CONFIG_X86_32
  769. restore_fpu(tsk);
  770. #else
  771. /*
  772. * Paranoid restore. send a SIGSEGV if we fail to restore the state.
  773. */
  774. if (unlikely(restore_fpu_checking(tsk))) {
  775. stts();
  776. force_sig(SIGSEGV, tsk);
  777. return;
  778. }
  779. #endif
  780. thread->status |= TS_USEDFPU; /* So we fnsave on switch_to() */
  781. tsk->fpu_counter++;
  782. }
  783. EXPORT_SYMBOL_GPL(math_state_restore);
  784. #ifndef CONFIG_MATH_EMULATION
  785. asmlinkage void math_emulate(long arg)
  786. {
  787. printk(KERN_EMERG
  788. "math-emulation not enabled and no coprocessor found.\n");
  789. printk(KERN_EMERG "killing %s.\n", current->comm);
  790. force_sig(SIGFPE, current);
  791. schedule();
  792. }
  793. #endif /* CONFIG_MATH_EMULATION */
  794. dotraplinkage void __kprobes
  795. do_device_not_available(struct pt_regs *regs, long error)
  796. {
  797. #ifdef CONFIG_X86_32
  798. if (read_cr0() & X86_CR0_EM) {
  799. conditional_sti(regs);
  800. math_emulate(0);
  801. } else {
  802. math_state_restore(); /* interrupts still off */
  803. conditional_sti(regs);
  804. }
  805. #else
  806. math_state_restore();
  807. #endif
  808. }
  809. #ifdef CONFIG_X86_32
  810. dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
  811. {
  812. siginfo_t info;
  813. local_irq_enable();
  814. info.si_signo = SIGILL;
  815. info.si_errno = 0;
  816. info.si_code = ILL_BADSTK;
  817. info.si_addr = 0;
  818. if (notify_die(DIE_TRAP, "iret exception",
  819. regs, error_code, 32, SIGILL) == NOTIFY_STOP)
  820. return;
  821. do_trap(32, SIGILL, "iret exception", regs, error_code, &info);
  822. }
  823. #endif
  824. void __init trap_init(void)
  825. {
  826. #ifdef CONFIG_X86_32
  827. int i;
  828. #endif
  829. #ifdef CONFIG_EISA
  830. void __iomem *p = early_ioremap(0x0FFFD9, 4);
  831. if (readl(p) == 'E' + ('I'<<8) + ('S'<<16) + ('A'<<24))
  832. EISA_bus = 1;
  833. early_iounmap(p, 4);
  834. #endif
  835. set_intr_gate(0, &divide_error);
  836. set_intr_gate_ist(1, &debug, DEBUG_STACK);
  837. set_intr_gate_ist(2, &nmi, NMI_STACK);
  838. /* int3 can be called from all */
  839. set_system_intr_gate_ist(3, &int3, DEBUG_STACK);
  840. /* int4 can be called from all */
  841. set_system_intr_gate(4, &overflow);
  842. set_intr_gate(5, &bounds);
  843. set_intr_gate(6, &invalid_op);
  844. set_intr_gate(7, &device_not_available);
  845. #ifdef CONFIG_X86_32
  846. set_task_gate(8, GDT_ENTRY_DOUBLEFAULT_TSS);
  847. #else
  848. set_intr_gate_ist(8, &double_fault, DOUBLEFAULT_STACK);
  849. #endif
  850. set_intr_gate(9, &coprocessor_segment_overrun);
  851. set_intr_gate(10, &invalid_TSS);
  852. set_intr_gate(11, &segment_not_present);
  853. set_intr_gate_ist(12, &stack_segment, STACKFAULT_STACK);
  854. set_intr_gate(13, &general_protection);
  855. set_intr_gate(14, &page_fault);
  856. set_intr_gate(15, &spurious_interrupt_bug);
  857. set_intr_gate(16, &coprocessor_error);
  858. set_intr_gate(17, &alignment_check);
  859. #ifdef CONFIG_X86_MCE
  860. set_intr_gate_ist(18, &machine_check, MCE_STACK);
  861. #endif
  862. set_intr_gate(19, &simd_coprocessor_error);
  863. #ifdef CONFIG_IA32_EMULATION
  864. set_system_intr_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
  865. #endif
  866. #ifdef CONFIG_X86_32
  867. if (cpu_has_fxsr) {
  868. printk(KERN_INFO "Enabling fast FPU save and restore... ");
  869. set_in_cr4(X86_CR4_OSFXSR);
  870. printk("done.\n");
  871. }
  872. if (cpu_has_xmm) {
  873. printk(KERN_INFO
  874. "Enabling unmasked SIMD FPU exception support... ");
  875. set_in_cr4(X86_CR4_OSXMMEXCPT);
  876. printk("done.\n");
  877. }
  878. set_system_trap_gate(SYSCALL_VECTOR, &system_call);
  879. /* Reserve all the builtin and the syscall vector: */
  880. for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
  881. set_bit(i, used_vectors);
  882. set_bit(SYSCALL_VECTOR, used_vectors);
  883. #endif
  884. /*
  885. * Should be a barrier for any external CPU state:
  886. */
  887. cpu_init();
  888. #ifdef CONFIG_X86_32
  889. trap_init_hook();
  890. #endif
  891. }