i387_64.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (C) 1994 Linus Torvalds
  3. * Copyright (C) 2002 Andi Kleen, SuSE Labs
  4. *
  5. * Pentium III FXSR, SSE support
  6. * General FPU state handling cleanups
  7. * Gareth Hughes <gareth@valinux.com>, May 2000
  8. *
  9. * x86-64 rework 2002 Andi Kleen.
  10. * Does direct fxsave in and out of user space now for signal handlers.
  11. * All the FSAVE<->FXSAVE conversion code has been moved to the 32bit emulation,
  12. * the 64bit user space sees a FXSAVE frame directly.
  13. */
  14. #include <linux/sched.h>
  15. #include <linux/init.h>
  16. #include <asm/processor.h>
  17. #include <asm/i387.h>
  18. #include <asm/sigcontext.h>
  19. #include <asm/user.h>
  20. #include <asm/ptrace.h>
  21. #include <asm/uaccess.h>
  22. unsigned int mxcsr_feature_mask __read_mostly = 0xffffffff;
  23. void mxcsr_feature_mask_init(void)
  24. {
  25. unsigned int mask;
  26. clts();
  27. memset(&current->thread.i387.fxsave, 0, sizeof(struct i387_fxsave_struct));
  28. asm volatile("fxsave %0" : : "m" (current->thread.i387.fxsave));
  29. mask = current->thread.i387.fxsave.mxcsr_mask;
  30. if (mask == 0) mask = 0x0000ffbf;
  31. mxcsr_feature_mask &= mask;
  32. stts();
  33. }
  34. /*
  35. * Called at bootup to set up the initial FPU state that is later cloned
  36. * into all processes.
  37. */
  38. void __cpuinit fpu_init(void)
  39. {
  40. unsigned long oldcr0 = read_cr0();
  41. extern void __bad_fxsave_alignment(void);
  42. if (offsetof(struct task_struct, thread.i387.fxsave) & 15)
  43. __bad_fxsave_alignment();
  44. set_in_cr4(X86_CR4_OSFXSR);
  45. set_in_cr4(X86_CR4_OSXMMEXCPT);
  46. write_cr0(oldcr0 & ~((1UL<<3)|(1UL<<2))); /* clear TS and EM */
  47. mxcsr_feature_mask_init();
  48. /* clean state in init */
  49. current_thread_info()->status = 0;
  50. clear_used_math();
  51. }
  52. void init_fpu(struct task_struct *child)
  53. {
  54. if (tsk_used_math(child)) {
  55. if (child == current)
  56. unlazy_fpu(child);
  57. return;
  58. }
  59. memset(&child->thread.i387.fxsave, 0, sizeof(struct i387_fxsave_struct));
  60. child->thread.i387.fxsave.cwd = 0x37f;
  61. child->thread.i387.fxsave.mxcsr = 0x1f80;
  62. /* only the device not available exception or ptrace can call init_fpu */
  63. set_stopped_child_used_math(child);
  64. }
  65. /*
  66. * ptrace request handlers.
  67. */
  68. int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *tsk)
  69. {
  70. init_fpu(tsk);
  71. return __copy_to_user(buf, &tsk->thread.i387.fxsave,
  72. sizeof(struct user_i387_struct)) ? -EFAULT : 0;
  73. }
  74. int set_fpregs(struct task_struct *tsk, struct user_i387_struct __user *buf)
  75. {
  76. if (__copy_from_user(&tsk->thread.i387.fxsave, buf,
  77. sizeof(struct user_i387_struct)))
  78. return -EFAULT;
  79. return 0;
  80. }
  81. /*
  82. * FPU state for core dumps.
  83. */
  84. int dump_fpu( struct pt_regs *regs, struct user_i387_struct *fpu )
  85. {
  86. struct task_struct *tsk = current;
  87. if (!used_math())
  88. return 0;
  89. unlazy_fpu(tsk);
  90. memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(struct user_i387_struct));
  91. return 1;
  92. }
  93. int dump_task_fpu(struct task_struct *tsk, struct user_i387_struct *fpu)
  94. {
  95. int fpvalid = !!tsk_used_math(tsk);
  96. if (fpvalid) {
  97. if (tsk == current)
  98. unlazy_fpu(tsk);
  99. memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(struct user_i387_struct));
  100. }
  101. return fpvalid;
  102. }