i387.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 <linux/hardirq.h>
  15. #include <asm/asm.h>
  16. #include <asm/processor.h>
  17. #include <asm/sigcontext.h>
  18. #include <asm/user.h>
  19. #include <asm/uaccess.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 int dump_fpu(struct pt_regs *, struct user_i387_struct *);
  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. struct _fpstate_ia32;
  31. extern int save_i387_ia32(struct _fpstate_ia32 __user *buf);
  32. extern int restore_i387_ia32(struct _fpstate_ia32 __user *buf);
  33. #endif
  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 restore_fpu_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. #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. #else /* CONFIG_X86_32 */
  126. extern void finit(void);
  127. static inline void tolerant_fwait(void)
  128. {
  129. asm volatile("fnclex ; fwait");
  130. }
  131. static inline void restore_fpu(struct task_struct *tsk)
  132. {
  133. /*
  134. * The "nop" is needed to make the instructions the same
  135. * length.
  136. */
  137. alternative_input(
  138. "nop ; frstor %1",
  139. "fxrstor %1",
  140. X86_FEATURE_FXSR,
  141. "m" (tsk->thread.xstate->fxsave));
  142. }
  143. /* We need a safe address that is cheap to find and that is already
  144. in L1 during context switch. The best choices are unfortunately
  145. different for UP and SMP */
  146. #ifdef CONFIG_SMP
  147. #define safe_address (__per_cpu_offset[0])
  148. #else
  149. #define safe_address (kstat_cpu(0).cpustat.user)
  150. #endif
  151. /*
  152. * These must be called with preempt disabled
  153. */
  154. static inline void __save_init_fpu(struct task_struct *tsk)
  155. {
  156. /* Use more nops than strictly needed in case the compiler
  157. varies code */
  158. alternative_input(
  159. "fnsave %[fx] ;fwait;" GENERIC_NOP8 GENERIC_NOP4,
  160. "fxsave %[fx]\n"
  161. "bt $7,%[fsw] ; jnc 1f ; fnclex\n1:",
  162. X86_FEATURE_FXSR,
  163. [fx] "m" (tsk->thread.xstate->fxsave),
  164. [fsw] "m" (tsk->thread.xstate->fxsave.swd) : "memory");
  165. /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception
  166. is pending. Clear the x87 state here by setting it to fixed
  167. values. safe_address is a random variable that should be in L1 */
  168. alternative_input(
  169. GENERIC_NOP8 GENERIC_NOP2,
  170. "emms\n\t" /* clear stack tags */
  171. "fildl %[addr]", /* set F?P to defined value */
  172. X86_FEATURE_FXSAVE_LEAK,
  173. [addr] "m" (safe_address));
  174. task_thread_info(tsk)->status &= ~TS_USEDFPU;
  175. }
  176. /*
  177. * Signal frame handlers...
  178. */
  179. extern int save_i387(struct _fpstate __user *buf);
  180. extern int restore_i387(struct _fpstate __user *buf);
  181. #endif /* CONFIG_X86_64 */
  182. static inline void __unlazy_fpu(struct task_struct *tsk)
  183. {
  184. if (task_thread_info(tsk)->status & TS_USEDFPU) {
  185. __save_init_fpu(tsk);
  186. stts();
  187. } else
  188. tsk->fpu_counter = 0;
  189. }
  190. static inline void __clear_fpu(struct task_struct *tsk)
  191. {
  192. if (task_thread_info(tsk)->status & TS_USEDFPU) {
  193. tolerant_fwait();
  194. task_thread_info(tsk)->status &= ~TS_USEDFPU;
  195. stts();
  196. }
  197. }
  198. static inline void kernel_fpu_begin(void)
  199. {
  200. struct thread_info *me = current_thread_info();
  201. preempt_disable();
  202. if (me->status & TS_USEDFPU)
  203. __save_init_fpu(me->task);
  204. else
  205. clts();
  206. }
  207. static inline void kernel_fpu_end(void)
  208. {
  209. stts();
  210. preempt_enable();
  211. }
  212. /*
  213. * Some instructions like VIA's padlock instructions generate a spurious
  214. * DNA fault but don't modify SSE registers. And these instructions
  215. * get used from interrupt context aswell. To prevent these kernel instructions
  216. * in interrupt context interact wrongly with other user/kernel fpu usage, we
  217. * should use them only in the context of irq_ts_save/restore()
  218. */
  219. static inline int irq_ts_save(void)
  220. {
  221. /*
  222. * If we are in process context, we are ok to take a spurious DNA fault.
  223. * Otherwise, doing clts() in process context require pre-emption to
  224. * be disabled or some heavy lifting like kernel_fpu_begin()
  225. */
  226. if (!in_interrupt())
  227. return 0;
  228. if (read_cr0() & X86_CR0_TS) {
  229. clts();
  230. return 1;
  231. }
  232. return 0;
  233. }
  234. static inline void irq_ts_restore(int TS_state)
  235. {
  236. if (TS_state)
  237. stts();
  238. }
  239. #ifdef CONFIG_X86_64
  240. static inline void save_init_fpu(struct task_struct *tsk)
  241. {
  242. __save_init_fpu(tsk);
  243. stts();
  244. }
  245. #define unlazy_fpu __unlazy_fpu
  246. #define clear_fpu __clear_fpu
  247. #else /* CONFIG_X86_32 */
  248. /*
  249. * These disable preemption on their own and are safe
  250. */
  251. static inline void save_init_fpu(struct task_struct *tsk)
  252. {
  253. preempt_disable();
  254. __save_init_fpu(tsk);
  255. stts();
  256. preempt_enable();
  257. }
  258. static inline void unlazy_fpu(struct task_struct *tsk)
  259. {
  260. preempt_disable();
  261. __unlazy_fpu(tsk);
  262. preempt_enable();
  263. }
  264. static inline void clear_fpu(struct task_struct *tsk)
  265. {
  266. preempt_disable();
  267. __clear_fpu(tsk);
  268. preempt_enable();
  269. }
  270. #endif /* CONFIG_X86_64 */
  271. /*
  272. * i387 state interaction
  273. */
  274. static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
  275. {
  276. if (cpu_has_fxsr) {
  277. return tsk->thread.xstate->fxsave.cwd;
  278. } else {
  279. return (unsigned short)tsk->thread.xstate->fsave.cwd;
  280. }
  281. }
  282. static inline unsigned short get_fpu_swd(struct task_struct *tsk)
  283. {
  284. if (cpu_has_fxsr) {
  285. return tsk->thread.xstate->fxsave.swd;
  286. } else {
  287. return (unsigned short)tsk->thread.xstate->fsave.swd;
  288. }
  289. }
  290. static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
  291. {
  292. if (cpu_has_xmm) {
  293. return tsk->thread.xstate->fxsave.mxcsr;
  294. } else {
  295. return MXCSR_DEFAULT;
  296. }
  297. }
  298. #endif /* ASM_X86__I387_H */