i387.h 10 KB

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