traps_32.c 22 KB

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