i387.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 int init_fpu(struct task_struct *child);
  22. extern asmlinkage void math_state_restore(void);
  23. extern void init_thread_xstate(void);
  24. extern user_regset_active_fn fpregs_active, xfpregs_active;
  25. extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get;
  26. extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set;
  27. #ifdef CONFIG_IA32_EMULATION
  28. struct _fpstate_ia32;
  29. extern int save_i387_ia32(struct _fpstate_ia32 __user *buf);
  30. extern int restore_i387_ia32(struct _fpstate_ia32 __user *buf);
  31. #endif
  32. #ifdef CONFIG_X86_64
  33. /* Ignore delayed exceptions from user space */
  34. static inline void tolerant_fwait(void)
  35. {
  36. asm volatile("1: fwait\n"
  37. "2:\n"
  38. _ASM_EXTABLE(1b, 2b));
  39. }
  40. static inline int restore_fpu_checking(struct i387_fxsave_struct *fx)
  41. {
  42. int err;
  43. asm volatile("1: rex64/fxrstor (%[fx])\n\t"
  44. "2:\n"
  45. ".section .fixup,\"ax\"\n"
  46. "3: movl $-1,%[err]\n"
  47. " jmp 2b\n"
  48. ".previous\n"
  49. _ASM_EXTABLE(1b, 3b)
  50. : [err] "=r" (err)
  51. #if 0 /* See comment in __save_init_fpu() below. */
  52. : [fx] "r" (fx), "m" (*fx), "0" (0));
  53. #else
  54. : [fx] "cdaSDb" (fx), "m" (*fx), "0" (0));
  55. #endif
  56. if (unlikely(err))
  57. init_fpu(current);
  58. return err;
  59. }
  60. #define X87_FSW_ES (1 << 7) /* Exception Summary */
  61. /* AMD CPUs don't save/restore FDP/FIP/FOP unless an exception
  62. is pending. Clear the x87 state here by setting it to fixed
  63. values. The kernel data segment can be sometimes 0 and sometimes
  64. new user value. Both should be ok.
  65. Use the PDA as safe address because it should be already in L1. */
  66. static inline void clear_fpu_state(struct i387_fxsave_struct *fx)
  67. {
  68. if (unlikely(fx->swd & X87_FSW_ES))
  69. asm volatile("fnclex");
  70. alternative_input(ASM_NOP8 ASM_NOP2,
  71. " emms\n" /* clear stack tags */
  72. " fildl %%gs:0", /* load to clear state */
  73. X86_FEATURE_FXSAVE_LEAK);
  74. }
  75. static inline int save_i387_checking(struct i387_fxsave_struct __user *fx)
  76. {
  77. int err;
  78. asm volatile("1: rex64/fxsave (%[fx])\n\t"
  79. "2:\n"
  80. ".section .fixup,\"ax\"\n"
  81. "3: movl $-1,%[err]\n"
  82. " jmp 2b\n"
  83. ".previous\n"
  84. _ASM_EXTABLE(1b, 3b)
  85. : [err] "=r" (err), "=m" (*fx)
  86. #if 0 /* See comment in __fxsave_clear() below. */
  87. : [fx] "r" (fx), "0" (0));
  88. #else
  89. : [fx] "cdaSDb" (fx), "0" (0));
  90. #endif
  91. if (unlikely(err) &&
  92. __clear_user(fx, sizeof(struct i387_fxsave_struct)))
  93. err = -EFAULT;
  94. /* No need to clear here because the caller clears USED_MATH */
  95. return err;
  96. }
  97. static inline void __save_init_fpu(struct task_struct *tsk)
  98. {
  99. /* Using "rex64; fxsave %0" is broken because, if the memory operand
  100. uses any extended registers for addressing, a second REX prefix
  101. will be generated (to the assembler, rex64 followed by semicolon
  102. is a separate instruction), and hence the 64-bitness is lost. */
  103. #if 0
  104. /* Using "fxsaveq %0" would be the ideal choice, but is only supported
  105. starting with gas 2.16. */
  106. __asm__ __volatile__("fxsaveq %0"
  107. : "=m" (tsk->thread.xstate->fxsave));
  108. #elif 0
  109. /* Using, as a workaround, the properly prefixed form below isn't
  110. accepted by any binutils version so far released, complaining that
  111. the same type of prefix is used twice if an extended register is
  112. needed for addressing (fix submitted to mainline 2005-11-21). */
  113. __asm__ __volatile__("rex64/fxsave %0"
  114. : "=m" (tsk->thread.xstate->fxsave));
  115. #else
  116. /* This, however, we can work around by forcing the compiler to select
  117. an addressing mode that doesn't require extended registers. */
  118. __asm__ __volatile__("rex64/fxsave (%1)"
  119. : "=m" (tsk->thread.xstate->fxsave)
  120. : "cdaSDb" (&tsk->thread.xstate->fxsave));
  121. #endif
  122. clear_fpu_state(&tsk->thread.xstate->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.xstate->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 *)
  141. buf);
  142. if (err)
  143. return err;
  144. task_thread_info(tsk)->status &= ~TS_USEDFPU;
  145. stts();
  146. } else {
  147. if (__copy_to_user(buf, &tsk->thread.xstate->fxsave,
  148. sizeof(struct i387_fxsave_struct)))
  149. return -1;
  150. }
  151. return 1;
  152. }
  153. /*
  154. * This restores directly out of user space. Exceptions are handled.
  155. */
  156. static inline int restore_i387(struct _fpstate __user *buf)
  157. {
  158. struct task_struct *tsk = current;
  159. int err;
  160. if (!used_math()) {
  161. err = init_fpu(tsk);
  162. if (err)
  163. return err;
  164. }
  165. if (!(task_thread_info(current)->status & TS_USEDFPU)) {
  166. clts();
  167. task_thread_info(current)->status |= TS_USEDFPU;
  168. }
  169. return restore_fpu_checking((__force struct i387_fxsave_struct *)buf);
  170. }
  171. #else /* CONFIG_X86_32 */
  172. static inline void tolerant_fwait(void)
  173. {
  174. asm volatile("fnclex ; fwait");
  175. }
  176. static inline void restore_fpu(struct task_struct *tsk)
  177. {
  178. /*
  179. * The "nop" is needed to make the instructions the same
  180. * length.
  181. */
  182. alternative_input(
  183. "nop ; frstor %1",
  184. "fxrstor %1",
  185. X86_FEATURE_FXSR,
  186. "m" (tsk->thread.xstate->fxsave));
  187. }
  188. /* We need a safe address that is cheap to find and that is already
  189. in L1 during context switch. The best choices are unfortunately
  190. different for UP and SMP */
  191. #ifdef CONFIG_SMP
  192. #define safe_address (__per_cpu_offset[0])
  193. #else
  194. #define safe_address (kstat_cpu(0).cpustat.user)
  195. #endif
  196. /*
  197. * These must be called with preempt disabled
  198. */
  199. static inline void __save_init_fpu(struct task_struct *tsk)
  200. {
  201. /* Use more nops than strictly needed in case the compiler
  202. varies code */
  203. alternative_input(
  204. "fnsave %[fx] ;fwait;" GENERIC_NOP8 GENERIC_NOP4,
  205. "fxsave %[fx]\n"
  206. "bt $7,%[fsw] ; jnc 1f ; fnclex\n1:",
  207. X86_FEATURE_FXSR,
  208. [fx] "m" (tsk->thread.xstate->fxsave),
  209. [fsw] "m" (tsk->thread.xstate->fxsave.swd) : "memory");
  210. /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception
  211. is pending. Clear the x87 state here by setting it to fixed
  212. values. safe_address is a random variable that should be in L1 */
  213. alternative_input(
  214. GENERIC_NOP8 GENERIC_NOP2,
  215. "emms\n\t" /* clear stack tags */
  216. "fildl %[addr]", /* set F?P to defined value */
  217. X86_FEATURE_FXSAVE_LEAK,
  218. [addr] "m" (safe_address));
  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 */