traps_32.c 22 KB

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