i387.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. #include <asm/xsave.h>
  21. extern unsigned int sig_xstate_size;
  22. extern void fpu_init(void);
  23. extern void mxcsr_feature_mask_init(void);
  24. extern int init_fpu(struct task_struct *child);
  25. extern asmlinkage void math_state_restore(void);
  26. extern void init_thread_xstate(void);
  27. extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);
  28. extern user_regset_active_fn fpregs_active, xfpregs_active;
  29. extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get;
  30. extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set;
  31. extern struct _fpx_sw_bytes fx_sw_reserved;
  32. #ifdef CONFIG_IA32_EMULATION
  33. extern unsigned int sig_xstate_ia32_size;
  34. extern struct _fpx_sw_bytes fx_sw_reserved_ia32;
  35. struct _fpstate_ia32;
  36. struct _xstate_ia32;
  37. extern int save_i387_xstate_ia32(void __user *buf);
  38. extern int restore_i387_xstate_ia32(void __user *buf);
  39. #endif
  40. #define X87_FSW_ES (1 << 7) /* Exception Summary */
  41. #ifdef CONFIG_X86_64
  42. /* Ignore delayed exceptions from user space */
  43. static inline void tolerant_fwait(void)
  44. {
  45. asm volatile("1: fwait\n"
  46. "2:\n"
  47. _ASM_EXTABLE(1b, 2b));
  48. }
  49. static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
  50. {
  51. int err;
  52. asm volatile("1: rex64/fxrstor (%[fx])\n\t"
  53. "2:\n"
  54. ".section .fixup,\"ax\"\n"
  55. "3: movl $-1,%[err]\n"
  56. " jmp 2b\n"
  57. ".previous\n"
  58. _ASM_EXTABLE(1b, 3b)
  59. : [err] "=r" (err)
  60. #if 0 /* See comment in fxsave() below. */
  61. : [fx] "r" (fx), "m" (*fx), "0" (0));
  62. #else
  63. : [fx] "cdaSDb" (fx), "m" (*fx), "0" (0));
  64. #endif
  65. return err;
  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 fxsave_user(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() 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. #ifdef CONFIG_MATH_EMULATION
  148. extern void finit_task(struct task_struct *tsk);
  149. #else
  150. static inline void finit_task(struct task_struct *tsk)
  151. {
  152. }
  153. #endif
  154. static inline void tolerant_fwait(void)
  155. {
  156. asm volatile("fnclex ; fwait");
  157. }
  158. /* perform fxrstor iff the processor has extended states, otherwise frstor */
  159. static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
  160. {
  161. /*
  162. * The "nop" is needed to make the instructions the same
  163. * length.
  164. */
  165. alternative_input(
  166. "nop ; frstor %1",
  167. "fxrstor %1",
  168. X86_FEATURE_FXSR,
  169. "m" (*fx));
  170. return 0;
  171. }
  172. /* We need a safe address that is cheap to find and that is already
  173. in L1 during context switch. The best choices are unfortunately
  174. different for UP and SMP */
  175. #ifdef CONFIG_SMP
  176. #define safe_address (__per_cpu_offset[0])
  177. #else
  178. #define safe_address (kstat_cpu(0).cpustat.user)
  179. #endif
  180. /*
  181. * These must be called with preempt disabled
  182. */
  183. static inline void __save_init_fpu(struct task_struct *tsk)
  184. {
  185. if (task_thread_info(tsk)->status & TS_XSAVE) {
  186. struct xsave_struct *xstate = &tsk->thread.xstate->xsave;
  187. struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
  188. xsave(tsk);
  189. /*
  190. * xsave header may indicate the init state of the FP.
  191. */
  192. if (!(xstate->xsave_hdr.xstate_bv & XSTATE_FP))
  193. goto end;
  194. if (unlikely(fx->swd & X87_FSW_ES))
  195. asm volatile("fnclex");
  196. /*
  197. * we can do a simple return here or be paranoid :)
  198. */
  199. goto clear_state;
  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. clear_state:
  211. /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception
  212. is pending. Clear the x87 state here by setting it to fixed
  213. values. safe_address is a random variable that should be in L1 */
  214. alternative_input(
  215. GENERIC_NOP8 GENERIC_NOP2,
  216. "emms\n\t" /* clear stack tags */
  217. "fildl %[addr]", /* set F?P to defined value */
  218. X86_FEATURE_FXSAVE_LEAK,
  219. [addr] "m" (safe_address));
  220. end:
  221. task_thread_info(tsk)->status &= ~TS_USEDFPU;
  222. }
  223. #endif /* CONFIG_X86_64 */
  224. static inline int restore_fpu_checking(struct task_struct *tsk)
  225. {
  226. if (task_thread_info(tsk)->status & TS_XSAVE)
  227. return xrstor_checking(&tsk->thread.xstate->xsave);
  228. else
  229. return fxrstor_checking(&tsk->thread.xstate->fxsave);
  230. }
  231. /*
  232. * Signal frame handlers...
  233. */
  234. extern int save_i387_xstate(void __user *buf);
  235. extern int restore_i387_xstate(void __user *buf);
  236. static inline void __unlazy_fpu(struct task_struct *tsk)
  237. {
  238. if (task_thread_info(tsk)->status & TS_USEDFPU) {
  239. __save_init_fpu(tsk);
  240. stts();
  241. } else
  242. tsk->fpu_counter = 0;
  243. }
  244. static inline void __clear_fpu(struct task_struct *tsk)
  245. {
  246. if (task_thread_info(tsk)->status & TS_USEDFPU) {
  247. tolerant_fwait();
  248. task_thread_info(tsk)->status &= ~TS_USEDFPU;
  249. stts();
  250. }
  251. }
  252. static inline void kernel_fpu_begin(void)
  253. {
  254. struct thread_info *me = current_thread_info();
  255. preempt_disable();
  256. if (me->status & TS_USEDFPU)
  257. __save_init_fpu(me->task);
  258. else
  259. clts();
  260. }
  261. static inline void kernel_fpu_end(void)
  262. {
  263. stts();
  264. preempt_enable();
  265. }
  266. /*
  267. * Some instructions like VIA's padlock instructions generate a spurious
  268. * DNA fault but don't modify SSE registers. And these instructions
  269. * get used from interrupt context as well. To prevent these kernel instructions
  270. * in interrupt context interacting wrongly with other user/kernel fpu usage, we
  271. * should use them only in the context of irq_ts_save/restore()
  272. */
  273. static inline int irq_ts_save(void)
  274. {
  275. /*
  276. * If in process context and not atomic, we can take a spurious DNA fault.
  277. * Otherwise, doing clts() in process context requires disabling preemption
  278. * or some heavy lifting like kernel_fpu_begin()
  279. */
  280. if (!in_atomic())
  281. return 0;
  282. if (read_cr0() & X86_CR0_TS) {
  283. clts();
  284. return 1;
  285. }
  286. return 0;
  287. }
  288. static inline void irq_ts_restore(int TS_state)
  289. {
  290. if (TS_state)
  291. stts();
  292. }
  293. #ifdef CONFIG_X86_64
  294. static inline void save_init_fpu(struct task_struct *tsk)
  295. {
  296. __save_init_fpu(tsk);
  297. stts();
  298. }
  299. #define unlazy_fpu __unlazy_fpu
  300. #define clear_fpu __clear_fpu
  301. #else /* CONFIG_X86_32 */
  302. /*
  303. * These disable preemption on their own and are safe
  304. */
  305. static inline void save_init_fpu(struct task_struct *tsk)
  306. {
  307. preempt_disable();
  308. __save_init_fpu(tsk);
  309. stts();
  310. preempt_enable();
  311. }
  312. static inline void unlazy_fpu(struct task_struct *tsk)
  313. {
  314. preempt_disable();
  315. __unlazy_fpu(tsk);
  316. preempt_enable();
  317. }
  318. static inline void clear_fpu(struct task_struct *tsk)
  319. {
  320. preempt_disable();
  321. __clear_fpu(tsk);
  322. preempt_enable();
  323. }
  324. #endif /* CONFIG_X86_64 */
  325. /*
  326. * i387 state interaction
  327. */
  328. static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
  329. {
  330. if (cpu_has_fxsr) {
  331. return tsk->thread.xstate->fxsave.cwd;
  332. } else {
  333. return (unsigned short)tsk->thread.xstate->fsave.cwd;
  334. }
  335. }
  336. static inline unsigned short get_fpu_swd(struct task_struct *tsk)
  337. {
  338. if (cpu_has_fxsr) {
  339. return tsk->thread.xstate->fxsave.swd;
  340. } else {
  341. return (unsigned short)tsk->thread.xstate->fsave.swd;
  342. }
  343. }
  344. static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
  345. {
  346. if (cpu_has_xmm) {
  347. return tsk->thread.xstate->fxsave.mxcsr;
  348. } else {
  349. return MXCSR_DEFAULT;
  350. }
  351. }
  352. #endif /* _ASM_X86_I387_H */