traps_32.c 22 KB

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