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 <mach_traps.h>
  52. #ifdef CONFIG_X86_64
  53. #include <asm/pgalloc.h>
  54. #include <asm/proto.h>
  55. #include <asm/pda.h>
  56. #else
  57. #include <asm/processor-flags.h>
  58. #include <asm/arch_hooks.h>
  59. #include <asm/traps.h>
  60. #include "cpu/mcheck/mce.h"
  61. asmlinkage int system_call(void);
  62. /* Do we ignore FPU interrupts ? */
  63. char ignore_fpu_irq;
  64. /*
  65. * The IDT has to be page-aligned to simplify the Pentium
  66. * F0 0F bug workaround.. We have a special link segment
  67. * for this.
  68. */
  69. gate_desc idt_table[256]
  70. __attribute__((__section__(".data.idt"))) = { { { { 0, 0 } } }, };
  71. #endif
  72. DECLARE_BITMAP(used_vectors, NR_VECTORS);
  73. EXPORT_SYMBOL_GPL(used_vectors);
  74. static int ignore_nmis;
  75. static inline void conditional_sti(struct pt_regs *regs)
  76. {
  77. if (regs->flags & X86_EFLAGS_IF)
  78. local_irq_enable();
  79. }
  80. static inline void preempt_conditional_sti(struct pt_regs *regs)
  81. {
  82. inc_preempt_count();
  83. if (regs->flags & X86_EFLAGS_IF)
  84. local_irq_enable();
  85. }
  86. static inline void conditional_cli(struct pt_regs *regs)
  87. {
  88. if (regs->flags & X86_EFLAGS_IF)
  89. local_irq_disable();
  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. /*
  258. * This is always a kernel trap and never fixable (and thus must
  259. * never return).
  260. */
  261. for (;;)
  262. die(str, regs, error_code);
  263. }
  264. #endif
  265. dotraplinkage void __kprobes
  266. do_general_protection(struct pt_regs *regs, long error_code)
  267. {
  268. struct task_struct *tsk;
  269. conditional_sti(regs);
  270. #ifdef CONFIG_X86_32
  271. if (lazy_iobitmap_copy()) {
  272. /* restart the faulting instruction */
  273. return;
  274. }
  275. if (regs->flags & X86_VM_MASK)
  276. goto gp_in_vm86;
  277. #endif
  278. tsk = current;
  279. if (!user_mode(regs))
  280. goto gp_in_kernel;
  281. tsk->thread.error_code = error_code;
  282. tsk->thread.trap_no = 13;
  283. if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
  284. printk_ratelimit()) {
  285. printk(KERN_INFO
  286. "%s[%d] general protection ip:%lx sp:%lx error:%lx",
  287. tsk->comm, task_pid_nr(tsk),
  288. regs->ip, regs->sp, error_code);
  289. print_vma_addr(" in ", regs->ip);
  290. printk("\n");
  291. }
  292. force_sig(SIGSEGV, tsk);
  293. return;
  294. #ifdef CONFIG_X86_32
  295. gp_in_vm86:
  296. local_irq_enable();
  297. handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
  298. return;
  299. #endif
  300. gp_in_kernel:
  301. if (fixup_exception(regs))
  302. return;
  303. tsk->thread.error_code = error_code;
  304. tsk->thread.trap_no = 13;
  305. if (notify_die(DIE_GPF, "general protection fault", regs,
  306. error_code, 13, SIGSEGV) == NOTIFY_STOP)
  307. return;
  308. die("general protection fault", regs, error_code);
  309. }
  310. static notrace __kprobes void
  311. mem_parity_error(unsigned char reason, struct pt_regs *regs)
  312. {
  313. printk(KERN_EMERG
  314. "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
  315. reason, smp_processor_id());
  316. printk(KERN_EMERG
  317. "You have some hardware problem, likely on the PCI bus.\n");
  318. #if defined(CONFIG_EDAC)
  319. if (edac_handler_set()) {
  320. edac_atomic_assert_error();
  321. return;
  322. }
  323. #endif
  324. if (panic_on_unrecovered_nmi)
  325. panic("NMI: Not continuing");
  326. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  327. /* Clear and disable the memory parity error line. */
  328. reason = (reason & 0xf) | 4;
  329. outb(reason, 0x61);
  330. }
  331. static notrace __kprobes void
  332. io_check_error(unsigned char reason, struct pt_regs *regs)
  333. {
  334. unsigned long i;
  335. printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
  336. show_registers(regs);
  337. /* Re-enable the IOCK line, wait for a few seconds */
  338. reason = (reason & 0xf) | 8;
  339. outb(reason, 0x61);
  340. i = 2000;
  341. while (--i)
  342. udelay(1000);
  343. reason &= ~8;
  344. outb(reason, 0x61);
  345. }
  346. static notrace __kprobes void
  347. unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
  348. {
  349. if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) ==
  350. NOTIFY_STOP)
  351. return;
  352. #ifdef CONFIG_MCA
  353. /*
  354. * Might actually be able to figure out what the guilty party
  355. * is:
  356. */
  357. if (MCA_bus) {
  358. mca_handle_nmi();
  359. return;
  360. }
  361. #endif
  362. printk(KERN_EMERG
  363. "Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
  364. reason, smp_processor_id());
  365. printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
  366. if (panic_on_unrecovered_nmi)
  367. panic("NMI: Not continuing");
  368. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  369. }
  370. static notrace __kprobes void default_do_nmi(struct pt_regs *regs)
  371. {
  372. unsigned char reason = 0;
  373. int cpu;
  374. cpu = smp_processor_id();
  375. /* Only the BSP gets external NMIs from the system. */
  376. if (!cpu)
  377. reason = get_nmi_reason();
  378. if (!(reason & 0xc0)) {
  379. if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
  380. == NOTIFY_STOP)
  381. return;
  382. #ifdef CONFIG_X86_LOCAL_APIC
  383. /*
  384. * Ok, so this is none of the documented NMI sources,
  385. * so it must be the NMI watchdog.
  386. */
  387. if (nmi_watchdog_tick(regs, reason))
  388. return;
  389. if (!do_nmi_callback(regs, cpu))
  390. unknown_nmi_error(reason, regs);
  391. #else
  392. unknown_nmi_error(reason, regs);
  393. #endif
  394. return;
  395. }
  396. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
  397. return;
  398. /* AK: following checks seem to be broken on modern chipsets. FIXME */
  399. if (reason & 0x80)
  400. mem_parity_error(reason, regs);
  401. if (reason & 0x40)
  402. io_check_error(reason, regs);
  403. #ifdef CONFIG_X86_32
  404. /*
  405. * Reassert NMI in case it became active meanwhile
  406. * as it's edge-triggered:
  407. */
  408. reassert_nmi();
  409. #endif
  410. }
  411. dotraplinkage notrace __kprobes void
  412. do_nmi(struct pt_regs *regs, long error_code)
  413. {
  414. nmi_enter();
  415. inc_irq_stat(__nmi_count);
  416. if (!ignore_nmis)
  417. default_do_nmi(regs);
  418. nmi_exit();
  419. }
  420. void stop_nmi(void)
  421. {
  422. acpi_nmi_disable();
  423. ignore_nmis++;
  424. }
  425. void restart_nmi(void)
  426. {
  427. ignore_nmis--;
  428. acpi_nmi_enable();
  429. }
  430. /* May run on IST stack. */
  431. dotraplinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)
  432. {
  433. #ifdef CONFIG_KPROBES
  434. if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
  435. == NOTIFY_STOP)
  436. return;
  437. #else
  438. if (notify_die(DIE_TRAP, "int3", regs, error_code, 3, SIGTRAP)
  439. == NOTIFY_STOP)
  440. return;
  441. #endif
  442. preempt_conditional_sti(regs);
  443. do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
  444. preempt_conditional_cli(regs);
  445. }
  446. #ifdef CONFIG_X86_64
  447. /*
  448. * Help handler running on IST stack to switch back to user stack
  449. * for scheduling or signal handling. The actual stack switch is done in
  450. * entry.S
  451. */
  452. asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
  453. {
  454. struct pt_regs *regs = eregs;
  455. /* Did already sync */
  456. if (eregs == (struct pt_regs *)eregs->sp)
  457. ;
  458. /* Exception from user space */
  459. else if (user_mode(eregs))
  460. regs = task_pt_regs(current);
  461. /*
  462. * Exception from kernel and interrupts are enabled. Move to
  463. * kernel process stack.
  464. */
  465. else if (eregs->flags & X86_EFLAGS_IF)
  466. regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
  467. if (eregs != regs)
  468. *regs = *eregs;
  469. return regs;
  470. }
  471. #endif
  472. /*
  473. * Our handling of the processor debug registers is non-trivial.
  474. * We do not clear them on entry and exit from the kernel. Therefore
  475. * it is possible to get a watchpoint trap here from inside the kernel.
  476. * However, the code in ./ptrace.c has ensured that the user can
  477. * only set watchpoints on userspace addresses. Therefore the in-kernel
  478. * watchpoint trap can only occur in code which is reading/writing
  479. * from user space. Such code must not hold kernel locks (since it
  480. * can equally take a page fault), therefore it is safe to call
  481. * force_sig_info even though that claims and releases locks.
  482. *
  483. * Code in ./signal.c ensures that the debug control register
  484. * is restored before we deliver any signal, and therefore that
  485. * user code runs with the correct debug control register even though
  486. * we clear it here.
  487. *
  488. * Being careful here means that we don't have to be as careful in a
  489. * lot of more complicated places (task switching can be a bit lazy
  490. * about restoring all the debug state, and ptrace doesn't have to
  491. * find every occurrence of the TF bit that could be saved away even
  492. * by user code)
  493. *
  494. * May run on IST stack.
  495. */
  496. dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code)
  497. {
  498. struct task_struct *tsk = current;
  499. unsigned long condition;
  500. int si_code;
  501. get_debugreg(condition, 6);
  502. /*
  503. * The processor cleared BTF, so don't mark that we need it set.
  504. */
  505. clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
  506. tsk->thread.debugctlmsr = 0;
  507. if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
  508. SIGTRAP) == NOTIFY_STOP)
  509. return;
  510. /* It's safe to allow irq's after DR6 has been saved */
  511. preempt_conditional_sti(regs);
  512. /* Mask out spurious debug traps due to lazy DR7 setting */
  513. if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
  514. if (!tsk->thread.debugreg7)
  515. goto clear_dr7;
  516. }
  517. #ifdef CONFIG_X86_32
  518. if (regs->flags & X86_VM_MASK)
  519. goto debug_vm86;
  520. #endif
  521. /* Save debug status register where ptrace can see it */
  522. tsk->thread.debugreg6 = condition;
  523. /*
  524. * Single-stepping through TF: make sure we ignore any events in
  525. * kernel space (but re-enable TF when returning to user mode).
  526. */
  527. if (condition & DR_STEP) {
  528. if (!user_mode(regs))
  529. goto clear_TF_reenable;
  530. }
  531. si_code = get_si_code(condition);
  532. /* Ok, finally something we can handle */
  533. send_sigtrap(tsk, regs, error_code, si_code);
  534. /*
  535. * Disable additional traps. They'll be re-enabled when
  536. * the signal is delivered.
  537. */
  538. clear_dr7:
  539. set_debugreg(0, 7);
  540. preempt_conditional_cli(regs);
  541. return;
  542. #ifdef CONFIG_X86_32
  543. debug_vm86:
  544. /* reenable preemption: handle_vm86_trap() might sleep */
  545. dec_preempt_count();
  546. handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
  547. conditional_cli(regs);
  548. return;
  549. #endif
  550. clear_TF_reenable:
  551. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  552. regs->flags &= ~X86_EFLAGS_TF;
  553. preempt_conditional_cli(regs);
  554. return;
  555. }
  556. #ifdef CONFIG_X86_64
  557. static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
  558. {
  559. if (fixup_exception(regs))
  560. return 1;
  561. notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
  562. /* Illegal floating point operation in the kernel */
  563. current->thread.trap_no = trapnr;
  564. die(str, regs, 0);
  565. return 0;
  566. }
  567. #endif
  568. /*
  569. * Note that we play around with the 'TS' bit in an attempt to get
  570. * the correct behaviour even in the presence of the asynchronous
  571. * IRQ13 behaviour
  572. */
  573. void math_error(void __user *ip)
  574. {
  575. struct task_struct *task;
  576. siginfo_t info;
  577. unsigned short cwd, swd, err;
  578. /*
  579. * Save the info for the exception handler and clear the error.
  580. */
  581. task = current;
  582. save_init_fpu(task);
  583. task->thread.trap_no = 16;
  584. task->thread.error_code = 0;
  585. info.si_signo = SIGFPE;
  586. info.si_errno = 0;
  587. info.si_addr = ip;
  588. /*
  589. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  590. * status. 0x3f is the exception bits in these regs, 0x200 is the
  591. * C1 reg you need in case of a stack fault, 0x040 is the stack
  592. * fault bit. We should only be taking one exception at a time,
  593. * so if this combination doesn't produce any single exception,
  594. * then we have a bad program that isn't synchronizing its FPU usage
  595. * and it will suffer the consequences since we won't be able to
  596. * fully reproduce the context of the exception
  597. */
  598. cwd = get_fpu_cwd(task);
  599. swd = get_fpu_swd(task);
  600. err = swd & ~cwd;
  601. if (err & 0x001) { /* Invalid op */
  602. /*
  603. * swd & 0x240 == 0x040: Stack Underflow
  604. * swd & 0x240 == 0x240: Stack Overflow
  605. * User must clear the SF bit (0x40) if set
  606. */
  607. info.si_code = FPE_FLTINV;
  608. } else if (err & 0x004) { /* Divide by Zero */
  609. info.si_code = FPE_FLTDIV;
  610. } else if (err & 0x008) { /* Overflow */
  611. info.si_code = FPE_FLTOVF;
  612. } else if (err & 0x012) { /* Denormal, Underflow */
  613. info.si_code = FPE_FLTUND;
  614. } else if (err & 0x020) { /* Precision */
  615. info.si_code = FPE_FLTRES;
  616. } else {
  617. /*
  618. * If we're using IRQ 13, or supposedly even some trap 16
  619. * implementations, it's possible we get a spurious trap...
  620. */
  621. return; /* Spurious trap, no error */
  622. }
  623. force_sig_info(SIGFPE, &info, task);
  624. }
  625. dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
  626. {
  627. conditional_sti(regs);
  628. #ifdef CONFIG_X86_32
  629. ignore_fpu_irq = 1;
  630. #else
  631. if (!user_mode(regs) &&
  632. kernel_math_error(regs, "kernel x87 math error", 16))
  633. return;
  634. #endif
  635. math_error((void __user *)regs->ip);
  636. }
  637. static void simd_math_error(void __user *ip)
  638. {
  639. struct task_struct *task;
  640. siginfo_t info;
  641. unsigned short mxcsr;
  642. /*
  643. * Save the info for the exception handler and clear the error.
  644. */
  645. task = current;
  646. save_init_fpu(task);
  647. task->thread.trap_no = 19;
  648. task->thread.error_code = 0;
  649. info.si_signo = SIGFPE;
  650. info.si_errno = 0;
  651. info.si_code = __SI_FAULT;
  652. info.si_addr = ip;
  653. /*
  654. * The SIMD FPU exceptions are handled a little differently, as there
  655. * is only a single status/control register. Thus, to determine which
  656. * unmasked exception was caught we must mask the exception mask bits
  657. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  658. */
  659. mxcsr = get_fpu_mxcsr(task);
  660. switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
  661. case 0x000:
  662. default:
  663. break;
  664. case 0x001: /* Invalid Op */
  665. info.si_code = FPE_FLTINV;
  666. break;
  667. case 0x002: /* Denormalize */
  668. case 0x010: /* Underflow */
  669. info.si_code = FPE_FLTUND;
  670. break;
  671. case 0x004: /* Zero Divide */
  672. info.si_code = FPE_FLTDIV;
  673. break;
  674. case 0x008: /* Overflow */
  675. info.si_code = FPE_FLTOVF;
  676. break;
  677. case 0x020: /* Precision */
  678. info.si_code = FPE_FLTRES;
  679. break;
  680. }
  681. force_sig_info(SIGFPE, &info, task);
  682. }
  683. dotraplinkage void
  684. do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
  685. {
  686. conditional_sti(regs);
  687. #ifdef CONFIG_X86_32
  688. if (cpu_has_xmm) {
  689. /* Handle SIMD FPU exceptions on PIII+ processors. */
  690. ignore_fpu_irq = 1;
  691. simd_math_error((void __user *)regs->ip);
  692. return;
  693. }
  694. /*
  695. * Handle strange cache flush from user space exception
  696. * in all other cases. This is undocumented behaviour.
  697. */
  698. if (regs->flags & X86_VM_MASK) {
  699. handle_vm86_fault((struct kernel_vm86_regs *)regs, error_code);
  700. return;
  701. }
  702. current->thread.trap_no = 19;
  703. current->thread.error_code = error_code;
  704. die_if_kernel("cache flush denied", regs, error_code);
  705. force_sig(SIGSEGV, current);
  706. #else
  707. if (!user_mode(regs) &&
  708. kernel_math_error(regs, "kernel simd math error", 19))
  709. return;
  710. simd_math_error((void __user *)regs->ip);
  711. #endif
  712. }
  713. dotraplinkage void
  714. do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
  715. {
  716. conditional_sti(regs);
  717. #if 0
  718. /* No need to warn about this any longer. */
  719. printk(KERN_INFO "Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
  720. #endif
  721. }
  722. #ifdef CONFIG_X86_32
  723. unsigned long patch_espfix_desc(unsigned long uesp, unsigned long kesp)
  724. {
  725. struct desc_struct *gdt = get_cpu_gdt_table(smp_processor_id());
  726. unsigned long base = (kesp - uesp) & -THREAD_SIZE;
  727. unsigned long new_kesp = kesp - base;
  728. unsigned long lim_pages = (new_kesp | (THREAD_SIZE - 1)) >> PAGE_SHIFT;
  729. __u64 desc = *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS];
  730. /* Set up base for espfix segment */
  731. desc &= 0x00f0ff0000000000ULL;
  732. desc |= ((((__u64)base) << 16) & 0x000000ffffff0000ULL) |
  733. ((((__u64)base) << 32) & 0xff00000000000000ULL) |
  734. ((((__u64)lim_pages) << 32) & 0x000f000000000000ULL) |
  735. (lim_pages & 0xffff);
  736. *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS] = desc;
  737. return new_kesp;
  738. }
  739. #else
  740. asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
  741. {
  742. }
  743. asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
  744. {
  745. }
  746. #endif
  747. /*
  748. * 'math_state_restore()' saves the current math information in the
  749. * old math state array, and gets the new ones from the current task
  750. *
  751. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  752. * Don't touch unless you *really* know how it works.
  753. *
  754. * Must be called with kernel preemption disabled (in this case,
  755. * local interrupts are disabled at the call-site in entry.S).
  756. */
  757. asmlinkage void math_state_restore(void)
  758. {
  759. struct thread_info *thread = current_thread_info();
  760. struct task_struct *tsk = thread->task;
  761. if (!tsk_used_math(tsk)) {
  762. local_irq_enable();
  763. /*
  764. * does a slab alloc which can sleep
  765. */
  766. if (init_fpu(tsk)) {
  767. /*
  768. * ran out of memory!
  769. */
  770. do_group_exit(SIGKILL);
  771. return;
  772. }
  773. local_irq_disable();
  774. }
  775. clts(); /* Allow maths ops (or we recurse) */
  776. #ifdef CONFIG_X86_32
  777. restore_fpu(tsk);
  778. #else
  779. /*
  780. * Paranoid restore. send a SIGSEGV if we fail to restore the state.
  781. */
  782. if (unlikely(restore_fpu_checking(tsk))) {
  783. stts();
  784. force_sig(SIGSEGV, tsk);
  785. return;
  786. }
  787. #endif
  788. thread->status |= TS_USEDFPU; /* So we fnsave on switch_to() */
  789. tsk->fpu_counter++;
  790. }
  791. EXPORT_SYMBOL_GPL(math_state_restore);
  792. #ifndef CONFIG_MATH_EMULATION
  793. void math_emulate(struct math_emu_info *info)
  794. {
  795. printk(KERN_EMERG
  796. "math-emulation not enabled and no coprocessor found.\n");
  797. printk(KERN_EMERG "killing %s.\n", current->comm);
  798. force_sig(SIGFPE, current);
  799. schedule();
  800. }
  801. #endif /* CONFIG_MATH_EMULATION */
  802. dotraplinkage void __kprobes do_device_not_available(struct pt_regs regs)
  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. trap_init_hook();
  901. #endif
  902. }