traps.c 25 KB

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