traps.c 25 KB

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