traps.c 26 KB

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