traps_64.c 26 KB

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