traps.c 25 KB

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