i387.h 10 KB

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