i387.h 9.1 KB

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