i387.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * include/asm-x86_64/i387.h
  3. *
  4. * Copyright (C) 1994 Linus Torvalds
  5. *
  6. * Pentium III FXSR, SSE support
  7. * General FPU state handling cleanups
  8. * Gareth Hughes <gareth@valinux.com>, May 2000
  9. * x86-64 work by Andi Kleen 2002
  10. */
  11. #ifndef __ASM_X86_64_I387_H
  12. #define __ASM_X86_64_I387_H
  13. #include <linux/sched.h>
  14. #include <asm/processor.h>
  15. #include <asm/sigcontext.h>
  16. #include <asm/user.h>
  17. #include <asm/thread_info.h>
  18. #include <asm/uaccess.h>
  19. extern void fpu_init(void);
  20. extern unsigned int mxcsr_feature_mask;
  21. extern void mxcsr_feature_mask_init(void);
  22. extern void init_fpu(struct task_struct *child);
  23. extern int save_i387(struct _fpstate __user *buf);
  24. /*
  25. * FPU lazy state save handling...
  26. */
  27. #define unlazy_fpu(tsk) do { \
  28. if (task_thread_info(tsk)->status & TS_USEDFPU) \
  29. save_init_fpu(tsk); \
  30. } while (0)
  31. /* Ignore delayed exceptions from user space */
  32. static inline void tolerant_fwait(void)
  33. {
  34. asm volatile("1: fwait\n"
  35. "2:\n"
  36. " .section __ex_table,\"a\"\n"
  37. " .align 8\n"
  38. " .quad 1b,2b\n"
  39. " .previous\n");
  40. }
  41. #define clear_fpu(tsk) do { \
  42. if (task_thread_info(tsk)->status & TS_USEDFPU) { \
  43. tolerant_fwait(); \
  44. task_thread_info(tsk)->status &= ~TS_USEDFPU; \
  45. stts(); \
  46. } \
  47. } while (0)
  48. /*
  49. * ptrace request handers...
  50. */
  51. extern int get_fpregs(struct user_i387_struct __user *buf,
  52. struct task_struct *tsk);
  53. extern int set_fpregs(struct task_struct *tsk,
  54. struct user_i387_struct __user *buf);
  55. /*
  56. * i387 state interaction
  57. */
  58. #define get_fpu_mxcsr(t) ((t)->thread.i387.fxsave.mxcsr)
  59. #define get_fpu_cwd(t) ((t)->thread.i387.fxsave.cwd)
  60. #define get_fpu_fxsr_twd(t) ((t)->thread.i387.fxsave.twd)
  61. #define get_fpu_swd(t) ((t)->thread.i387.fxsave.swd)
  62. #define set_fpu_cwd(t,val) ((t)->thread.i387.fxsave.cwd = (val))
  63. #define set_fpu_swd(t,val) ((t)->thread.i387.fxsave.swd = (val))
  64. #define set_fpu_fxsr_twd(t,val) ((t)->thread.i387.fxsave.twd = (val))
  65. static inline int restore_fpu_checking(struct i387_fxsave_struct *fx)
  66. {
  67. int err;
  68. asm volatile("1: rex64/fxrstor (%[fx])\n\t"
  69. "2:\n"
  70. ".section .fixup,\"ax\"\n"
  71. "3: movl $-1,%[err]\n"
  72. " jmp 2b\n"
  73. ".previous\n"
  74. ".section __ex_table,\"a\"\n"
  75. " .align 8\n"
  76. " .quad 1b,3b\n"
  77. ".previous"
  78. : [err] "=r" (err)
  79. #if 0 /* See comment in __fxsave_clear() below. */
  80. : [fx] "r" (fx), "m" (*fx), "0" (0));
  81. #else
  82. : [fx] "cdaSDb" (fx), "m" (*fx), "0" (0));
  83. #endif
  84. if (unlikely(err))
  85. init_fpu(current);
  86. return err;
  87. }
  88. static inline int save_i387_checking(struct i387_fxsave_struct __user *fx)
  89. {
  90. int err;
  91. asm volatile("1: rex64/fxsave (%[fx])\n\t"
  92. "2:\n"
  93. ".section .fixup,\"ax\"\n"
  94. "3: movl $-1,%[err]\n"
  95. " jmp 2b\n"
  96. ".previous\n"
  97. ".section __ex_table,\"a\"\n"
  98. " .align 8\n"
  99. " .quad 1b,3b\n"
  100. ".previous"
  101. : [err] "=r" (err), "=m" (*fx)
  102. #if 0 /* See comment in __fxsave_clear() below. */
  103. : [fx] "r" (fx), "0" (0));
  104. #else
  105. : [fx] "cdaSDb" (fx), "0" (0));
  106. #endif
  107. if (unlikely(err))
  108. __clear_user(fx, sizeof(struct i387_fxsave_struct));
  109. return err;
  110. }
  111. static inline void __fxsave_clear(struct task_struct *tsk)
  112. {
  113. /* Using "rex64; fxsave %0" is broken because, if the memory operand
  114. uses any extended registers for addressing, a second REX prefix
  115. will be generated (to the assembler, rex64 followed by semicolon
  116. is a separate instruction), and hence the 64-bitness is lost. */
  117. #if 0
  118. /* Using "fxsaveq %0" would be the ideal choice, but is only supported
  119. starting with gas 2.16. */
  120. __asm__ __volatile__("fxsaveq %0"
  121. : "=m" (tsk->thread.i387.fxsave));
  122. #elif 0
  123. /* Using, as a workaround, the properly prefixed form below isn't
  124. accepted by any binutils version so far released, complaining that
  125. the same type of prefix is used twice if an extended register is
  126. needed for addressing (fix submitted to mainline 2005-11-21). */
  127. __asm__ __volatile__("rex64/fxsave %0"
  128. : "=m" (tsk->thread.i387.fxsave));
  129. #else
  130. /* This, however, we can work around by forcing the compiler to select
  131. an addressing mode that doesn't require extended registers. */
  132. __asm__ __volatile__("rex64/fxsave %P2(%1)"
  133. : "=m" (tsk->thread.i387.fxsave)
  134. : "cdaSDb" (tsk),
  135. "i" (offsetof(__typeof__(*tsk),
  136. thread.i387.fxsave)));
  137. #endif
  138. __asm__ __volatile__("fnclex");
  139. }
  140. static inline void kernel_fpu_begin(void)
  141. {
  142. struct thread_info *me = current_thread_info();
  143. preempt_disable();
  144. if (me->status & TS_USEDFPU) {
  145. __fxsave_clear(me->task);
  146. me->status &= ~TS_USEDFPU;
  147. return;
  148. }
  149. clts();
  150. }
  151. static inline void kernel_fpu_end(void)
  152. {
  153. stts();
  154. preempt_enable();
  155. }
  156. static inline void save_init_fpu(struct task_struct *tsk)
  157. {
  158. __fxsave_clear(tsk);
  159. task_thread_info(tsk)->status &= ~TS_USEDFPU;
  160. stts();
  161. }
  162. /*
  163. * This restores directly out of user space. Exceptions are handled.
  164. */
  165. static inline int restore_i387(struct _fpstate __user *buf)
  166. {
  167. return restore_fpu_checking((__force struct i387_fxsave_struct *)buf);
  168. }
  169. #endif /* __ASM_X86_64_I387_H */