traps.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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/delay.h>
  23. #include <linux/errno.h>
  24. #include <linux/kexec.h>
  25. #include <linux/sched.h>
  26. #include <linux/timer.h>
  27. #include <linux/init.h>
  28. #include <linux/bug.h>
  29. #include <linux/nmi.h>
  30. #include <linux/mm.h>
  31. #include <linux/smp.h>
  32. #include <linux/io.h>
  33. #ifdef CONFIG_EISA
  34. #include <linux/ioport.h>
  35. #include <linux/eisa.h>
  36. #endif
  37. #ifdef CONFIG_MCA
  38. #include <linux/mca.h>
  39. #endif
  40. #if defined(CONFIG_EDAC)
  41. #include <linux/edac.h>
  42. #endif
  43. #include <asm/stacktrace.h>
  44. #include <asm/processor.h>
  45. #include <asm/debugreg.h>
  46. #include <asm/atomic.h>
  47. #include <asm/system.h>
  48. #include <asm/traps.h>
  49. #include <asm/desc.h>
  50. #include <asm/i387.h>
  51. #include <mach_traps.h>
  52. #ifdef CONFIG_X86_64
  53. #include <asm/pgalloc.h>
  54. #include <asm/proto.h>
  55. #else
  56. #include <asm/processor-flags.h>
  57. #include <asm/arch_hooks.h>
  58. #include <asm/traps.h>
  59. #include "cpu/mcheck/mce.h"
  60. asmlinkage int system_call(void);
  61. /* Do we ignore FPU interrupts ? */
  62. char ignore_fpu_irq;
  63. /*
  64. * The IDT has to be page-aligned to simplify the Pentium
  65. * F0 0F bug workaround.. We have a special link segment
  66. * for this.
  67. */
  68. gate_desc idt_table[256]
  69. __attribute__((__section__(".data.idt"))) = { { { { 0, 0 } } }, };
  70. #endif
  71. DECLARE_BITMAP(used_vectors, NR_VECTORS);
  72. EXPORT_SYMBOL_GPL(used_vectors);
  73. static int ignore_nmis;
  74. static inline void conditional_sti(struct pt_regs *regs)
  75. {
  76. if (regs->flags & X86_EFLAGS_IF)
  77. local_irq_enable();
  78. }
  79. static inline void preempt_conditional_sti(struct pt_regs *regs)
  80. {
  81. inc_preempt_count();
  82. if (regs->flags & X86_EFLAGS_IF)
  83. local_irq_enable();
  84. }
  85. static inline void preempt_conditional_cli(struct pt_regs *regs)
  86. {
  87. if (regs->flags & X86_EFLAGS_IF)
  88. local_irq_disable();
  89. dec_preempt_count();
  90. }
  91. #ifdef CONFIG_X86_32
  92. static inline void
  93. die_if_kernel(const char *str, struct pt_regs *regs, long err)
  94. {
  95. if (!user_mode_vm(regs))
  96. die(str, regs, err);
  97. }
  98. /*
  99. * Perform the lazy TSS's I/O bitmap copy. If the TSS has an
  100. * invalid offset set (the LAZY one) and the faulting thread has
  101. * a valid I/O bitmap pointer, we copy the I/O bitmap in the TSS,
  102. * we set the offset field correctly and return 1.
  103. */
  104. static int lazy_iobitmap_copy(void)
  105. {
  106. struct thread_struct *thread;
  107. struct tss_struct *tss;
  108. int cpu;
  109. cpu = get_cpu();
  110. tss = &per_cpu(init_tss, cpu);
  111. thread = &current->thread;
  112. if (tss->x86_tss.io_bitmap_base == INVALID_IO_BITMAP_OFFSET_LAZY &&
  113. thread->io_bitmap_ptr) {
  114. memcpy(tss->io_bitmap, thread->io_bitmap_ptr,
  115. thread->io_bitmap_max);
  116. /*
  117. * If the previously set map was extending to higher ports
  118. * than the current one, pad extra space with 0xff (no access).
  119. */
  120. if (thread->io_bitmap_max < tss->io_bitmap_max) {
  121. memset((char *) tss->io_bitmap +
  122. thread->io_bitmap_max, 0xff,
  123. tss->io_bitmap_max - thread->io_bitmap_max);
  124. }
  125. tss->io_bitmap_max = thread->io_bitmap_max;
  126. tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
  127. tss->io_bitmap_owner = thread;
  128. put_cpu();
  129. return 1;
  130. }
  131. put_cpu();
  132. return 0;
  133. }
  134. #endif
  135. static void __kprobes
  136. do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
  137. long error_code, siginfo_t *info)
  138. {
  139. struct task_struct *tsk = current;
  140. #ifdef CONFIG_X86_32
  141. if (regs->flags & X86_VM_MASK) {
  142. /*
  143. * traps 0, 1, 3, 4, and 5 should be forwarded to vm86.
  144. * On nmi (interrupt 2), do_trap should not be called.
  145. */
  146. if (trapnr < 6)
  147. goto vm86_trap;
  148. goto trap_signal;
  149. }
  150. #endif
  151. if (!user_mode(regs))
  152. goto kernel_trap;
  153. #ifdef CONFIG_X86_32
  154. trap_signal:
  155. #endif
  156. /*
  157. * We want error_code and trap_no set for userspace faults and
  158. * kernelspace faults which result in die(), but not
  159. * kernelspace faults which are fixed up. die() gives the
  160. * process no chance to handle the signal and notice the
  161. * kernel fault information, so that won't result in polluting
  162. * the information about previously queued, but not yet
  163. * delivered, faults. See also do_general_protection below.
  164. */
  165. tsk->thread.error_code = error_code;
  166. tsk->thread.trap_no = trapnr;
  167. #ifdef CONFIG_X86_64
  168. if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
  169. printk_ratelimit()) {
  170. printk(KERN_INFO
  171. "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
  172. tsk->comm, tsk->pid, str,
  173. regs->ip, regs->sp, error_code);
  174. print_vma_addr(" in ", regs->ip);
  175. printk("\n");
  176. }
  177. #endif
  178. if (info)
  179. force_sig_info(signr, info, tsk);
  180. else
  181. force_sig(signr, tsk);
  182. return;
  183. kernel_trap:
  184. if (!fixup_exception(regs)) {
  185. tsk->thread.error_code = error_code;
  186. tsk->thread.trap_no = trapnr;
  187. die(str, regs, error_code);
  188. }
  189. return;
  190. #ifdef CONFIG_X86_32
  191. vm86_trap:
  192. if (handle_vm86_trap((struct kernel_vm86_regs *) regs,
  193. error_code, trapnr))
  194. goto trap_signal;
  195. return;
  196. #endif
  197. }
  198. #define DO_ERROR(trapnr, signr, str, name) \
  199. dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
  200. { \
  201. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  202. == NOTIFY_STOP) \
  203. return; \
  204. conditional_sti(regs); \
  205. do_trap(trapnr, signr, str, regs, error_code, NULL); \
  206. }
  207. #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
  208. dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
  209. { \
  210. siginfo_t info; \
  211. info.si_signo = signr; \
  212. info.si_errno = 0; \
  213. info.si_code = sicode; \
  214. info.si_addr = (void __user *)siaddr; \
  215. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  216. == NOTIFY_STOP) \
  217. return; \
  218. conditional_sti(regs); \
  219. do_trap(trapnr, signr, str, regs, error_code, &info); \
  220. }
  221. DO_ERROR_INFO(0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
  222. DO_ERROR(4, SIGSEGV, "overflow", overflow)
  223. DO_ERROR(5, SIGSEGV, "bounds", bounds)
  224. DO_ERROR_INFO(6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
  225. DO_ERROR(9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
  226. DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
  227. DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
  228. #ifdef CONFIG_X86_32
  229. DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
  230. #endif
  231. DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
  232. #ifdef CONFIG_X86_64
  233. /* Runs on IST stack */
  234. dotraplinkage void do_stack_segment(struct pt_regs *regs, long error_code)
  235. {
  236. if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
  237. 12, SIGBUS) == NOTIFY_STOP)
  238. return;
  239. preempt_conditional_sti(regs);
  240. do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
  241. preempt_conditional_cli(regs);
  242. }
  243. dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code)
  244. {
  245. static const char str[] = "double fault";
  246. struct task_struct *tsk = current;
  247. /* Return not checked because double check cannot be ignored */
  248. notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
  249. tsk->thread.error_code = error_code;
  250. tsk->thread.trap_no = 8;
  251. /*
  252. * This is always a kernel trap and never fixable (and thus must
  253. * never return).
  254. */
  255. for (;;)
  256. die(str, regs, error_code);
  257. }
  258. #endif
  259. dotraplinkage void __kprobes
  260. do_general_protection(struct pt_regs *regs, long error_code)
  261. {
  262. struct task_struct *tsk;
  263. conditional_sti(regs);
  264. #ifdef CONFIG_X86_32
  265. if (lazy_iobitmap_copy()) {
  266. /* restart the faulting instruction */
  267. return;
  268. }
  269. if (regs->flags & X86_VM_MASK)
  270. goto gp_in_vm86;
  271. #endif
  272. tsk = current;
  273. if (!user_mode(regs))
  274. goto gp_in_kernel;
  275. tsk->thread.error_code = error_code;
  276. tsk->thread.trap_no = 13;
  277. if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
  278. printk_ratelimit()) {
  279. printk(KERN_INFO
  280. "%s[%d] general protection ip:%lx sp:%lx error:%lx",
  281. tsk->comm, task_pid_nr(tsk),
  282. regs->ip, regs->sp, error_code);
  283. print_vma_addr(" in ", regs->ip);
  284. printk("\n");
  285. }
  286. force_sig(SIGSEGV, tsk);
  287. return;
  288. #ifdef CONFIG_X86_32
  289. gp_in_vm86:
  290. local_irq_enable();
  291. handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
  292. return;
  293. #endif
  294. gp_in_kernel:
  295. if (fixup_exception(regs))
  296. return;
  297. tsk->thread.error_code = error_code;
  298. tsk->thread.trap_no = 13;
  299. if (notify_die(DIE_GPF, "general protection fault", regs,
  300. error_code, 13, SIGSEGV) == NOTIFY_STOP)
  301. return;
  302. die("general protection fault", regs, error_code);
  303. }
  304. static notrace __kprobes void
  305. mem_parity_error(unsigned char reason, struct pt_regs *regs)
  306. {
  307. printk(KERN_EMERG
  308. "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
  309. reason, smp_processor_id());
  310. printk(KERN_EMERG
  311. "You have some hardware problem, likely on the PCI bus.\n");
  312. #if defined(CONFIG_EDAC)
  313. if (edac_handler_set()) {
  314. edac_atomic_assert_error();
  315. return;
  316. }
  317. #endif
  318. if (panic_on_unrecovered_nmi)
  319. panic("NMI: Not continuing");
  320. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  321. /* Clear and disable the memory parity error line. */
  322. reason = (reason & 0xf) | 4;
  323. outb(reason, 0x61);
  324. }
  325. static notrace __kprobes void
  326. io_check_error(unsigned char reason, struct pt_regs *regs)
  327. {
  328. unsigned long i;
  329. printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
  330. show_registers(regs);
  331. /* Re-enable the IOCK line, wait for a few seconds */
  332. reason = (reason & 0xf) | 8;
  333. outb(reason, 0x61);
  334. i = 2000;
  335. while (--i)
  336. udelay(1000);
  337. reason &= ~8;
  338. outb(reason, 0x61);
  339. }
  340. static notrace __kprobes void
  341. unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
  342. {
  343. if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) ==
  344. NOTIFY_STOP)
  345. return;
  346. #ifdef CONFIG_MCA
  347. /*
  348. * Might actually be able to figure out what the guilty party
  349. * is:
  350. */
  351. if (MCA_bus) {
  352. mca_handle_nmi();
  353. return;
  354. }
  355. #endif
  356. printk(KERN_EMERG
  357. "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
  358. reason, smp_processor_id());
  359. printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
  360. if (panic_on_unrecovered_nmi)
  361. panic("NMI: Not continuing");
  362. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  363. }
  364. static notrace __kprobes void default_do_nmi(struct pt_regs *regs)
  365. {
  366. unsigned char reason = 0;
  367. int cpu;
  368. cpu = smp_processor_id();
  369. /* Only the BSP gets external NMIs from the system. */
  370. if (!cpu)
  371. reason = get_nmi_reason();
  372. if (!(reason & 0xc0)) {
  373. if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
  374. == NOTIFY_STOP)
  375. return;
  376. #ifdef CONFIG_X86_LOCAL_APIC
  377. /*
  378. * Ok, so this is none of the documented NMI sources,
  379. * so it must be the NMI watchdog.
  380. */
  381. if (nmi_watchdog_tick(regs, reason))
  382. return;
  383. if (!do_nmi_callback(regs, cpu))
  384. unknown_nmi_error(reason, regs);
  385. #else
  386. unknown_nmi_error(reason, regs);
  387. #endif
  388. return;
  389. }
  390. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
  391. return;
  392. /* AK: following checks seem to be broken on modern chipsets. FIXME */
  393. if (reason & 0x80)
  394. mem_parity_error(reason, regs);
  395. if (reason & 0x40)
  396. io_check_error(reason, regs);
  397. #ifdef CONFIG_X86_32
  398. /*
  399. * Reassert NMI in case it became active meanwhile
  400. * as it's edge-triggered:
  401. */
  402. reassert_nmi();
  403. #endif
  404. }
  405. dotraplinkage notrace __kprobes void
  406. do_nmi(struct pt_regs *regs, long error_code)
  407. {
  408. nmi_enter();
  409. inc_irq_stat(__nmi_count);
  410. if (!ignore_nmis)
  411. default_do_nmi(regs);
  412. nmi_exit();
  413. }
  414. void stop_nmi(void)
  415. {
  416. acpi_nmi_disable();
  417. ignore_nmis++;
  418. }
  419. void restart_nmi(void)
  420. {
  421. ignore_nmis--;
  422. acpi_nmi_enable();
  423. }
  424. /* May run on IST stack. */
  425. dotraplinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)
  426. {
  427. #ifdef CONFIG_KPROBES
  428. if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
  429. == NOTIFY_STOP)
  430. return;
  431. #else
  432. if (notify_die(DIE_TRAP, "int3", regs, error_code, 3, SIGTRAP)
  433. == NOTIFY_STOP)
  434. return;
  435. #endif
  436. preempt_conditional_sti(regs);
  437. do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
  438. preempt_conditional_cli(regs);
  439. }
  440. #ifdef CONFIG_X86_64
  441. /*
  442. * Help handler running on IST stack to switch back to user stack
  443. * for scheduling or signal handling. The actual stack switch is done in
  444. * entry.S
  445. */
  446. asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
  447. {
  448. struct pt_regs *regs = eregs;
  449. /* Did already sync */
  450. if (eregs == (struct pt_regs *)eregs->sp)
  451. ;
  452. /* Exception from user space */
  453. else if (user_mode(eregs))
  454. regs = task_pt_regs(current);
  455. /*
  456. * Exception from kernel and interrupts are enabled. Move to
  457. * kernel process stack.
  458. */
  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;
  593. if (err & 0x001) { /* Invalid op */
  594. /*
  595. * swd & 0x240 == 0x040: Stack Underflow
  596. * swd & 0x240 == 0x240: Stack Overflow
  597. * User must clear the SF bit (0x40) if set
  598. */
  599. info.si_code = FPE_FLTINV;
  600. } else if (err & 0x004) { /* Divide by Zero */
  601. info.si_code = FPE_FLTDIV;
  602. } else if (err & 0x008) { /* Overflow */
  603. info.si_code = FPE_FLTOVF;
  604. } else if (err & 0x012) { /* Denormal, Underflow */
  605. info.si_code = FPE_FLTUND;
  606. } else if (err & 0x020) { /* Precision */
  607. info.si_code = FPE_FLTRES;
  608. } else {
  609. /*
  610. * If we're using IRQ 13, or supposedly even some trap 16
  611. * implementations, it's possible we get a spurious trap...
  612. */
  613. return; /* Spurious trap, no error */
  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. void math_emulate(struct math_emu_info *info)
  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_code)
  796. {
  797. #ifdef CONFIG_X86_32
  798. if (read_cr0() & X86_CR0_EM) {
  799. struct math_emu_info info = { };
  800. conditional_sti(regs);
  801. info.regs = regs;
  802. math_emulate(&info);
  803. } else {
  804. math_state_restore(); /* interrupts still off */
  805. conditional_sti(regs);
  806. }
  807. #else
  808. math_state_restore();
  809. #endif
  810. }
  811. #ifdef CONFIG_X86_32
  812. dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
  813. {
  814. siginfo_t info;
  815. local_irq_enable();
  816. info.si_signo = SIGILL;
  817. info.si_errno = 0;
  818. info.si_code = ILL_BADSTK;
  819. info.si_addr = 0;
  820. if (notify_die(DIE_TRAP, "iret exception",
  821. regs, error_code, 32, SIGILL) == NOTIFY_STOP)
  822. return;
  823. do_trap(32, SIGILL, "iret exception", regs, error_code, &info);
  824. }
  825. #endif
  826. void __init trap_init(void)
  827. {
  828. int i;
  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. #endif
  880. /* Reserve all the builtin and the syscall vector: */
  881. for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
  882. set_bit(i, used_vectors);
  883. #ifdef CONFIG_X86_64
  884. set_bit(IA32_SYSCALL_VECTOR, used_vectors);
  885. #else
  886. set_bit(SYSCALL_VECTOR, used_vectors);
  887. #endif
  888. /*
  889. * Should be a barrier for any external CPU state:
  890. */
  891. cpu_init();
  892. #ifdef CONFIG_X86_32
  893. trap_init_hook();
  894. #endif
  895. }