i387.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. #include <asm/xsave.h>
  20. extern void fpu_init(void);
  21. extern void mxcsr_feature_mask_init(void);
  22. extern int init_fpu(struct task_struct *child);
  23. extern asmlinkage void math_state_restore(void);
  24. extern void init_thread_xstate(void);
  25. extern user_regset_active_fn fpregs_active, xfpregs_active;
  26. extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get;
  27. extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set;
  28. #ifdef CONFIG_IA32_EMULATION
  29. struct _fpstate_ia32;
  30. extern int save_i387_ia32(struct _fpstate_ia32 __user *buf);
  31. extern int restore_i387_ia32(struct _fpstate_ia32 __user *buf);
  32. #endif
  33. #define X87_FSW_ES (1 << 7) /* Exception Summary */
  34. #ifdef CONFIG_X86_64
  35. /* Ignore delayed exceptions from user space */
  36. static inline void tolerant_fwait(void)
  37. {
  38. asm volatile("1: fwait\n"
  39. "2:\n"
  40. _ASM_EXTABLE(1b, 2b));
  41. }
  42. static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
  43. {
  44. int err;
  45. asm volatile("1: rex64/fxrstor (%[fx])\n\t"
  46. "2:\n"
  47. ".section .fixup,\"ax\"\n"
  48. "3: movl $-1,%[err]\n"
  49. " jmp 2b\n"
  50. ".previous\n"
  51. _ASM_EXTABLE(1b, 3b)
  52. : [err] "=r" (err)
  53. #if 0 /* See comment in __save_init_fpu() below. */
  54. : [fx] "r" (fx), "m" (*fx), "0" (0));
  55. #else
  56. : [fx] "cdaSDb" (fx), "m" (*fx), "0" (0));
  57. #endif
  58. return err;
  59. }
  60. static inline int restore_fpu_checking(struct task_struct *tsk)
  61. {
  62. if (task_thread_info(tsk)->status & TS_XSAVE)
  63. return xrstor_checking(&tsk->thread.xstate->xsave);
  64. else
  65. return fxrstor_checking(&tsk->thread.xstate->fxsave);
  66. }
  67. /* AMD CPUs don't save/restore FDP/FIP/FOP unless an exception
  68. is pending. Clear the x87 state here by setting it to fixed
  69. values. The kernel data segment can be sometimes 0 and sometimes
  70. new user value. Both should be ok.
  71. Use the PDA as safe address because it should be already in L1. */
  72. static inline void clear_fpu_state(struct task_struct *tsk)
  73. {
  74. struct xsave_struct *xstate = &tsk->thread.xstate->xsave;
  75. struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
  76. /*
  77. * xsave header may indicate the init state of the FP.
  78. */
  79. if ((task_thread_info(tsk)->status & TS_XSAVE) &&
  80. !(xstate->xsave_hdr.xstate_bv & XSTATE_FP))
  81. return;
  82. if (unlikely(fx->swd & X87_FSW_ES))
  83. asm volatile("fnclex");
  84. alternative_input(ASM_NOP8 ASM_NOP2,
  85. " emms\n" /* clear stack tags */
  86. " fildl %%gs:0", /* load to clear state */
  87. X86_FEATURE_FXSAVE_LEAK);
  88. }
  89. static inline int save_i387_checking(struct i387_fxsave_struct __user *fx)
  90. {
  91. int err;
  92. asm volatile("1: rex64/fxsave (%[fx])\n\t"
  93. "2:\n"
  94. ".section .fixup,\"ax\"\n"
  95. "3: movl $-1,%[err]\n"
  96. " jmp 2b\n"
  97. ".previous\n"
  98. _ASM_EXTABLE(1b, 3b)
  99. : [err] "=r" (err), "=m" (*fx)
  100. #if 0 /* See comment in __fxsave_clear() below. */
  101. : [fx] "r" (fx), "0" (0));
  102. #else
  103. : [fx] "cdaSDb" (fx), "0" (0));
  104. #endif
  105. if (unlikely(err) &&
  106. __clear_user(fx, sizeof(struct i387_fxsave_struct)))
  107. err = -EFAULT;
  108. /* No need to clear here because the caller clears USED_MATH */
  109. return err;
  110. }
  111. static inline void fxsave(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.xstate->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.xstate->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 (%1)"
  133. : "=m" (tsk->thread.xstate->fxsave)
  134. : "cdaSDb" (&tsk->thread.xstate->fxsave));
  135. #endif
  136. }
  137. static inline void __save_init_fpu(struct task_struct *tsk)
  138. {
  139. if (task_thread_info(tsk)->status & TS_XSAVE)
  140. xsave(tsk);
  141. else
  142. fxsave(tsk);
  143. clear_fpu_state(tsk);
  144. task_thread_info(tsk)->status &= ~TS_USEDFPU;
  145. }
  146. #else /* CONFIG_X86_32 */
  147. extern void finit(void);
  148. static inline void tolerant_fwait(void)
  149. {
  150. asm volatile("fnclex ; fwait");
  151. }
  152. static inline void restore_fpu(struct task_struct *tsk)
  153. {
  154. if (task_thread_info(tsk)->status & TS_XSAVE) {
  155. xrstor_checking(&tsk->thread.xstate->xsave);
  156. return;
  157. }
  158. /*
  159. * The "nop" is needed to make the instructions the same
  160. * length.
  161. */
  162. alternative_input(
  163. "nop ; frstor %1",
  164. "fxrstor %1",
  165. X86_FEATURE_FXSR,
  166. "m" (tsk->thread.xstate->fxsave));
  167. }
  168. /* We need a safe address that is cheap to find and that is already
  169. in L1 during context switch. The best choices are unfortunately
  170. different for UP and SMP */
  171. #ifdef CONFIG_SMP
  172. #define safe_address (__per_cpu_offset[0])
  173. #else
  174. #define safe_address (kstat_cpu(0).cpustat.user)
  175. #endif
  176. /*
  177. * These must be called with preempt disabled
  178. */
  179. static inline void __save_init_fpu(struct task_struct *tsk)
  180. {
  181. if (task_thread_info(tsk)->status & TS_XSAVE) {
  182. struct xsave_struct *xstate = &tsk->thread.xstate->xsave;
  183. struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
  184. xsave(tsk);
  185. /*
  186. * xsave header may indicate the init state of the FP.
  187. */
  188. if (!(xstate->xsave_hdr.xstate_bv & XSTATE_FP))
  189. goto end;
  190. if (unlikely(fx->swd & X87_FSW_ES))
  191. asm volatile("fnclex");
  192. /*
  193. * we can do a simple return here or be paranoid :)
  194. */
  195. goto clear_state;
  196. }
  197. /* Use more nops than strictly needed in case the compiler
  198. varies code */
  199. alternative_input(
  200. "fnsave %[fx] ;fwait;" GENERIC_NOP8 GENERIC_NOP4,
  201. "fxsave %[fx]\n"
  202. "bt $7,%[fsw] ; jnc 1f ; fnclex\n1:",
  203. X86_FEATURE_FXSR,
  204. [fx] "m" (tsk->thread.xstate->fxsave),
  205. [fsw] "m" (tsk->thread.xstate->fxsave.swd) : "memory");
  206. clear_state:
  207. /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception
  208. is pending. Clear the x87 state here by setting it to fixed
  209. values. safe_address is a random variable that should be in L1 */
  210. alternative_input(
  211. GENERIC_NOP8 GENERIC_NOP2,
  212. "emms\n\t" /* clear stack tags */
  213. "fildl %[addr]", /* set F?P to defined value */
  214. X86_FEATURE_FXSAVE_LEAK,
  215. [addr] "m" (safe_address));
  216. end:
  217. task_thread_info(tsk)->status &= ~TS_USEDFPU;
  218. }
  219. /*
  220. * Signal frame handlers...
  221. */
  222. extern int save_i387(struct _fpstate __user *buf);
  223. extern int restore_i387(struct _fpstate __user *buf);
  224. #endif /* CONFIG_X86_64 */
  225. static inline void __unlazy_fpu(struct task_struct *tsk)
  226. {
  227. if (task_thread_info(tsk)->status & TS_USEDFPU) {
  228. __save_init_fpu(tsk);
  229. stts();
  230. } else
  231. tsk->fpu_counter = 0;
  232. }
  233. static inline void __clear_fpu(struct task_struct *tsk)
  234. {
  235. if (task_thread_info(tsk)->status & TS_USEDFPU) {
  236. tolerant_fwait();
  237. task_thread_info(tsk)->status &= ~TS_USEDFPU;
  238. stts();
  239. }
  240. }
  241. static inline void kernel_fpu_begin(void)
  242. {
  243. struct thread_info *me = current_thread_info();
  244. preempt_disable();
  245. if (me->status & TS_USEDFPU)
  246. __save_init_fpu(me->task);
  247. else
  248. clts();
  249. }
  250. static inline void kernel_fpu_end(void)
  251. {
  252. stts();
  253. preempt_enable();
  254. }
  255. #ifdef CONFIG_X86_64
  256. static inline void save_init_fpu(struct task_struct *tsk)
  257. {
  258. __save_init_fpu(tsk);
  259. stts();
  260. }
  261. #define unlazy_fpu __unlazy_fpu
  262. #define clear_fpu __clear_fpu
  263. #else /* CONFIG_X86_32 */
  264. /*
  265. * These disable preemption on their own and are safe
  266. */
  267. static inline void save_init_fpu(struct task_struct *tsk)
  268. {
  269. preempt_disable();
  270. __save_init_fpu(tsk);
  271. stts();
  272. preempt_enable();
  273. }
  274. static inline void unlazy_fpu(struct task_struct *tsk)
  275. {
  276. preempt_disable();
  277. __unlazy_fpu(tsk);
  278. preempt_enable();
  279. }
  280. static inline void clear_fpu(struct task_struct *tsk)
  281. {
  282. preempt_disable();
  283. __clear_fpu(tsk);
  284. preempt_enable();
  285. }
  286. #endif /* CONFIG_X86_64 */
  287. /*
  288. * i387 state interaction
  289. */
  290. static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
  291. {
  292. if (cpu_has_fxsr) {
  293. return tsk->thread.xstate->fxsave.cwd;
  294. } else {
  295. return (unsigned short)tsk->thread.xstate->fsave.cwd;
  296. }
  297. }
  298. static inline unsigned short get_fpu_swd(struct task_struct *tsk)
  299. {
  300. if (cpu_has_fxsr) {
  301. return tsk->thread.xstate->fxsave.swd;
  302. } else {
  303. return (unsigned short)tsk->thread.xstate->fsave.swd;
  304. }
  305. }
  306. static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
  307. {
  308. if (cpu_has_xmm) {
  309. return tsk->thread.xstate->fxsave.mxcsr;
  310. } else {
  311. return MXCSR_DEFAULT;
  312. }
  313. }
  314. #endif /* ASM_X86__I387_H */