traps.c 25 KB

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