i387.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Copyright (C) 1994 Linus Torvalds
  3. *
  4. * Pentium III FXSR, SSE support
  5. * General FPU state handling cleanups
  6. * Gareth Hughes <gareth@valinux.com>, May 2000
  7. * x86-64 work by Andi Kleen 2002
  8. */
  9. #ifndef _ASM_X86_I387_H
  10. #define _ASM_X86_I387_H
  11. #include <linux/sched.h>
  12. #include <linux/kernel_stat.h>
  13. #include <linux/regset.h>
  14. #include <asm/asm.h>
  15. #include <asm/processor.h>
  16. #include <asm/sigcontext.h>
  17. #include <asm/user.h>
  18. #include <asm/uaccess.h>
  19. extern void fpu_init(void);
  20. extern void mxcsr_feature_mask_init(void);
  21. extern void init_fpu(struct task_struct *child);
  22. extern asmlinkage void math_state_restore(void);
  23. extern user_regset_active_fn fpregs_active, xfpregs_active;
  24. extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get;
  25. extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set;
  26. #ifdef CONFIG_IA32_EMULATION
  27. struct _fpstate_ia32;
  28. extern int save_i387_ia32(struct _fpstate_ia32 __user *buf);
  29. extern int restore_i387_ia32(struct _fpstate_ia32 __user *buf);
  30. #endif
  31. #ifdef CONFIG_X86_64
  32. /* Ignore delayed exceptions from user space */
  33. static inline void tolerant_fwait(void)
  34. {
  35. asm volatile("1: fwait\n"
  36. "2:\n"
  37. _ASM_EXTABLE(1b,2b));
  38. }
  39. static inline int restore_fpu_checking(struct i387_fxsave_struct *fx)
  40. {
  41. int err;
  42. asm volatile("1: rex64/fxrstor (%[fx])\n\t"
  43. "2:\n"
  44. ".section .fixup,\"ax\"\n"
  45. "3: movl $-1,%[err]\n"
  46. " jmp 2b\n"
  47. ".previous\n"
  48. _ASM_EXTABLE(1b,3b)
  49. : [err] "=r" (err)
  50. #if 0 /* See comment in __save_init_fpu() below. */
  51. : [fx] "r" (fx), "m" (*fx), "0" (0));
  52. #else
  53. : [fx] "cdaSDb" (fx), "m" (*fx), "0" (0));
  54. #endif
  55. if (unlikely(err))
  56. init_fpu(current);
  57. return err;
  58. }
  59. #define X87_FSW_ES (1 << 7) /* Exception Summary */
  60. /* AMD CPUs don't save/restore FDP/FIP/FOP unless an exception
  61. is pending. Clear the x87 state here by setting it to fixed
  62. values. The kernel data segment can be sometimes 0 and sometimes
  63. new user value. Both should be ok.
  64. Use the PDA as safe address because it should be already in L1. */
  65. static inline void clear_fpu_state(struct i387_fxsave_struct *fx)
  66. {
  67. if (unlikely(fx->swd & X87_FSW_ES))
  68. asm volatile("fnclex");
  69. alternative_input(ASM_NOP8 ASM_NOP2,
  70. " emms\n" /* clear stack tags */
  71. " fildl %%gs:0", /* load to clear state */
  72. X86_FEATURE_FXSAVE_LEAK);
  73. }
  74. static inline int save_i387_checking(struct i387_fxsave_struct __user *fx)
  75. {
  76. int err;
  77. asm volatile("1: rex64/fxsave (%[fx])\n\t"
  78. "2:\n"
  79. ".section .fixup,\"ax\"\n"
  80. "3: movl $-1,%[err]\n"
  81. " jmp 2b\n"
  82. ".previous\n"
  83. _ASM_EXTABLE(1b,3b)
  84. : [err] "=r" (err), "=m" (*fx)
  85. #if 0 /* See comment in __fxsave_clear() below. */
  86. : [fx] "r" (fx), "0" (0));
  87. #else
  88. : [fx] "cdaSDb" (fx), "0" (0));
  89. #endif
  90. if (unlikely(err) && __clear_user(fx, sizeof(struct i387_fxsave_struct)))
  91. err = -EFAULT;
  92. /* No need to clear here because the caller clears USED_MATH */
  93. return err;
  94. }
  95. static inline void __save_init_fpu(struct task_struct *tsk)
  96. {
  97. /* Using "rex64; fxsave %0" is broken because, if the memory operand
  98. uses any extended registers for addressing, a second REX prefix
  99. will be generated (to the assembler, rex64 followed by semicolon
  100. is a separate instruction), and hence the 64-bitness is lost. */
  101. #if 0
  102. /* Using "fxsaveq %0" would be the ideal choice, but is only supported
  103. starting with gas 2.16. */
  104. __asm__ __volatile__("fxsaveq %0"
  105. : "=m" (tsk->thread.i387.fxsave));
  106. #elif 0
  107. /* Using, as a workaround, the properly prefixed form below isn't
  108. accepted by any binutils version so far released, complaining that
  109. the same type of prefix is used twice if an extended register is
  110. needed for addressing (fix submitted to mainline 2005-11-21). */
  111. __asm__ __volatile__("rex64/fxsave %0"
  112. : "=m" (tsk->thread.i387.fxsave));
  113. #else
  114. /* This, however, we can work around by forcing the compiler to select
  115. an addressing mode that doesn't require extended registers. */
  116. __asm__ __volatile__("rex64/fxsave %P2(%1)"
  117. : "=m" (tsk->thread.i387.fxsave)
  118. : "cdaSDb" (tsk),
  119. "i" (offsetof(__typeof__(*tsk),
  120. thread.i387.fxsave)));
  121. #endif
  122. clear_fpu_state(&tsk->thread.i387.fxsave);
  123. task_thread_info(tsk)->status &= ~TS_USEDFPU;
  124. }
  125. /*
  126. * Signal frame handlers.
  127. */
  128. static inline int save_i387(struct _fpstate __user *buf)
  129. {
  130. struct task_struct *tsk = current;
  131. int err = 0;
  132. BUILD_BUG_ON(sizeof(struct user_i387_struct) !=
  133. sizeof(tsk->thread.i387.fxsave));
  134. if ((unsigned long)buf % 16)
  135. printk("save_i387: bad fpstate %p\n", buf);
  136. if (!used_math())
  137. return 0;
  138. clear_used_math(); /* trigger finit */
  139. if (task_thread_info(tsk)->status & TS_USEDFPU) {
  140. err = save_i387_checking((struct i387_fxsave_struct __user *)buf);
  141. if (err) return err;
  142. task_thread_info(tsk)->status &= ~TS_USEDFPU;
  143. stts();
  144. } else {
  145. if (__copy_to_user(buf, &tsk->thread.i387.fxsave,
  146. sizeof(struct i387_fxsave_struct)))
  147. return -1;
  148. }
  149. return 1;
  150. }
  151. /*
  152. * This restores directly out of user space. Exceptions are handled.
  153. */
  154. static inline int restore_i387(struct _fpstate __user *buf)
  155. {
  156. set_used_math();
  157. if (!(task_thread_info(current)->status & TS_USEDFPU)) {
  158. clts();
  159. task_thread_info(current)->status |= TS_USEDFPU;
  160. }
  161. return restore_fpu_checking((__force struct i387_fxsave_struct *)buf);
  162. }
  163. #else /* CONFIG_X86_32 */
  164. static inline void tolerant_fwait(void)
  165. {
  166. asm volatile("fnclex ; fwait");
  167. }
  168. static inline void restore_fpu(struct task_struct *tsk)
  169. {
  170. /*
  171. * The "nop" is needed to make the instructions the same
  172. * length.
  173. */
  174. alternative_input(
  175. "nop ; frstor %1",
  176. "fxrstor %1",
  177. X86_FEATURE_FXSR,
  178. "m" ((tsk)->thread.i387.fxsave));
  179. }
  180. /* We need a safe address that is cheap to find and that is already
  181. in L1 during context switch. The best choices are unfortunately
  182. different for UP and SMP */
  183. #ifdef CONFIG_SMP
  184. #define safe_address (__per_cpu_offset[0])
  185. #else
  186. #define safe_address (kstat_cpu(0).cpustat.user)
  187. #endif
  188. /*
  189. * These must be called with preempt disabled
  190. */
  191. static inline void __save_init_fpu(struct task_struct *tsk)
  192. {
  193. /* Use more nops than strictly needed in case the compiler
  194. varies code */
  195. alternative_input(
  196. "fnsave %[fx] ;fwait;" GENERIC_NOP8 GENERIC_NOP4,
  197. "fxsave %[fx]\n"
  198. "bt $7,%[fsw] ; jnc 1f ; fnclex\n1:",
  199. X86_FEATURE_FXSR,
  200. [fx] "m" (tsk->thread.i387.fxsave),
  201. [fsw] "m" (tsk->thread.i387.fxsave.swd) : "memory");
  202. /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception
  203. is pending. Clear the x87 state here by setting it to fixed
  204. values. safe_address is a random variable that should be in L1 */
  205. alternative_input(
  206. GENERIC_NOP8 GENERIC_NOP2,
  207. "emms\n\t" /* clear stack tags */
  208. "fildl %[addr]", /* set F?P to defined value */
  209. X86_FEATURE_FXSAVE_LEAK,
  210. [addr] "m" (safe_address));
  211. task_thread_info(tsk)->status &= ~TS_USEDFPU;
  212. }
  213. /*
  214. * Signal frame handlers...
  215. */
  216. extern int save_i387(struct _fpstate __user *buf);
  217. extern int restore_i387(struct _fpstate __user *buf);
  218. #endif /* CONFIG_X86_64 */
  219. static inline void __unlazy_fpu(struct task_struct *tsk)
  220. {
  221. if (task_thread_info(tsk)->status & TS_USEDFPU) {
  222. __save_init_fpu(tsk);
  223. stts();
  224. } else
  225. tsk->fpu_counter = 0;
  226. }
  227. static inline void __clear_fpu(struct task_struct *tsk)
  228. {
  229. if (task_thread_info(tsk)->status & TS_USEDFPU) {
  230. tolerant_fwait();
  231. task_thread_info(tsk)->status &= ~TS_USEDFPU;
  232. stts();
  233. }
  234. }
  235. static inline void kernel_fpu_begin(void)
  236. {
  237. struct thread_info *me = current_thread_info();
  238. preempt_disable();
  239. if (me->status & TS_USEDFPU)
  240. __save_init_fpu(me->task);
  241. else
  242. clts();
  243. }
  244. static inline void kernel_fpu_end(void)
  245. {
  246. stts();
  247. preempt_enable();
  248. }
  249. #ifdef CONFIG_X86_64
  250. static inline void save_init_fpu(struct task_struct *tsk)
  251. {
  252. __save_init_fpu(tsk);
  253. stts();
  254. }
  255. #define unlazy_fpu __unlazy_fpu
  256. #define clear_fpu __clear_fpu
  257. #else /* CONFIG_X86_32 */
  258. /*
  259. * These disable preemption on their own and are safe
  260. */
  261. static inline void save_init_fpu(struct task_struct *tsk)
  262. {
  263. preempt_disable();
  264. __save_init_fpu(tsk);
  265. stts();
  266. preempt_enable();
  267. }
  268. static inline void unlazy_fpu(struct task_struct *tsk)
  269. {
  270. preempt_disable();
  271. __unlazy_fpu(tsk);
  272. preempt_enable();
  273. }
  274. static inline void clear_fpu(struct task_struct *tsk)
  275. {
  276. preempt_disable();
  277. __clear_fpu(tsk);
  278. preempt_enable();
  279. }
  280. #endif /* CONFIG_X86_64 */
  281. /*
  282. * i387 state interaction
  283. */
  284. static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
  285. {
  286. if (cpu_has_fxsr) {
  287. return tsk->thread.i387.fxsave.cwd;
  288. } else {
  289. return (unsigned short)tsk->thread.i387.fsave.cwd;
  290. }
  291. }
  292. static inline unsigned short get_fpu_swd(struct task_struct *tsk)
  293. {
  294. if (cpu_has_fxsr) {
  295. return tsk->thread.i387.fxsave.swd;
  296. } else {
  297. return (unsigned short)tsk->thread.i387.fsave.swd;
  298. }
  299. }
  300. static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
  301. {
  302. if (cpu_has_xmm) {
  303. return tsk->thread.i387.fxsave.mxcsr;
  304. } else {
  305. return MXCSR_DEFAULT;
  306. }
  307. }
  308. #endif /* _ASM_X86_I387_H */