i387.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. #ifndef __ASSEMBLY__
  12. #include <linux/sched.h>
  13. #include <linux/kernel_stat.h>
  14. #include <linux/regset.h>
  15. #include <linux/hardirq.h>
  16. #include <linux/slab.h>
  17. #include <asm/asm.h>
  18. #include <asm/cpufeature.h>
  19. #include <asm/processor.h>
  20. #include <asm/sigcontext.h>
  21. #include <asm/user.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/xsave.h>
  24. extern unsigned int sig_xstate_size;
  25. extern void fpu_init(void);
  26. extern void mxcsr_feature_mask_init(void);
  27. extern int init_fpu(struct task_struct *child);
  28. extern asmlinkage void math_state_restore(void);
  29. extern void __math_state_restore(void);
  30. extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);
  31. extern user_regset_active_fn fpregs_active, xfpregs_active;
  32. extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get,
  33. xstateregs_get;
  34. extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set,
  35. xstateregs_set;
  36. /*
  37. * xstateregs_active == fpregs_active. Please refer to the comment
  38. * at the definition of fpregs_active.
  39. */
  40. #define xstateregs_active fpregs_active
  41. extern struct _fpx_sw_bytes fx_sw_reserved;
  42. #ifdef CONFIG_IA32_EMULATION
  43. extern unsigned int sig_xstate_ia32_size;
  44. extern struct _fpx_sw_bytes fx_sw_reserved_ia32;
  45. struct _fpstate_ia32;
  46. struct _xstate_ia32;
  47. extern int save_i387_xstate_ia32(void __user *buf);
  48. extern int restore_i387_xstate_ia32(void __user *buf);
  49. #endif
  50. #ifdef CONFIG_MATH_EMULATION
  51. extern void finit_soft_fpu(struct i387_soft_struct *soft);
  52. #else
  53. static inline void finit_soft_fpu(struct i387_soft_struct *soft) {}
  54. #endif
  55. #define X87_FSW_ES (1 << 7) /* Exception Summary */
  56. static __always_inline __pure bool use_xsaveopt(void)
  57. {
  58. return static_cpu_has(X86_FEATURE_XSAVEOPT);
  59. }
  60. static __always_inline __pure bool use_xsave(void)
  61. {
  62. return static_cpu_has(X86_FEATURE_XSAVE);
  63. }
  64. static __always_inline __pure bool use_fxsr(void)
  65. {
  66. return static_cpu_has(X86_FEATURE_FXSR);
  67. }
  68. extern void __sanitize_i387_state(struct task_struct *);
  69. static inline void sanitize_i387_state(struct task_struct *tsk)
  70. {
  71. if (!use_xsaveopt())
  72. return;
  73. __sanitize_i387_state(tsk);
  74. }
  75. #ifdef CONFIG_X86_64
  76. static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
  77. {
  78. int err;
  79. /* See comment in fxsave() below. */
  80. #ifdef CONFIG_AS_FXSAVEQ
  81. asm volatile("1: fxrstorq %[fx]\n\t"
  82. "2:\n"
  83. ".section .fixup,\"ax\"\n"
  84. "3: movl $-1,%[err]\n"
  85. " jmp 2b\n"
  86. ".previous\n"
  87. _ASM_EXTABLE(1b, 3b)
  88. : [err] "=r" (err)
  89. : [fx] "m" (*fx), "0" (0));
  90. #else
  91. asm volatile("1: rex64/fxrstor (%[fx])\n\t"
  92. "2:\n"
  93. ".section .fixup,\"ax\"\n"
  94. "3: movl $-1,%[err]\n"
  95. " jmp 2b\n"
  96. ".previous\n"
  97. _ASM_EXTABLE(1b, 3b)
  98. : [err] "=r" (err)
  99. : [fx] "R" (fx), "m" (*fx), "0" (0));
  100. #endif
  101. return err;
  102. }
  103. static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
  104. {
  105. int err;
  106. /*
  107. * Clear the bytes not touched by the fxsave and reserved
  108. * for the SW usage.
  109. */
  110. err = __clear_user(&fx->sw_reserved,
  111. sizeof(struct _fpx_sw_bytes));
  112. if (unlikely(err))
  113. return -EFAULT;
  114. /* See comment in fxsave() below. */
  115. #ifdef CONFIG_AS_FXSAVEQ
  116. asm volatile("1: fxsaveq %[fx]\n\t"
  117. "2:\n"
  118. ".section .fixup,\"ax\"\n"
  119. "3: movl $-1,%[err]\n"
  120. " jmp 2b\n"
  121. ".previous\n"
  122. _ASM_EXTABLE(1b, 3b)
  123. : [err] "=r" (err), [fx] "=m" (*fx)
  124. : "0" (0));
  125. #else
  126. asm volatile("1: rex64/fxsave (%[fx])\n\t"
  127. "2:\n"
  128. ".section .fixup,\"ax\"\n"
  129. "3: movl $-1,%[err]\n"
  130. " jmp 2b\n"
  131. ".previous\n"
  132. _ASM_EXTABLE(1b, 3b)
  133. : [err] "=r" (err), "=m" (*fx)
  134. : [fx] "R" (fx), "0" (0));
  135. #endif
  136. if (unlikely(err) &&
  137. __clear_user(fx, sizeof(struct i387_fxsave_struct)))
  138. err = -EFAULT;
  139. /* No need to clear here because the caller clears USED_MATH */
  140. return err;
  141. }
  142. static inline void fpu_fxsave(struct fpu *fpu)
  143. {
  144. /* Using "rex64; fxsave %0" is broken because, if the memory operand
  145. uses any extended registers for addressing, a second REX prefix
  146. will be generated (to the assembler, rex64 followed by semicolon
  147. is a separate instruction), and hence the 64-bitness is lost. */
  148. #ifdef CONFIG_AS_FXSAVEQ
  149. /* Using "fxsaveq %0" would be the ideal choice, but is only supported
  150. starting with gas 2.16. */
  151. __asm__ __volatile__("fxsaveq %0"
  152. : "=m" (fpu->state->fxsave));
  153. #else
  154. /* Using, as a workaround, the properly prefixed form below isn't
  155. accepted by any binutils version so far released, complaining that
  156. the same type of prefix is used twice if an extended register is
  157. needed for addressing (fix submitted to mainline 2005-11-21).
  158. asm volatile("rex64/fxsave %0"
  159. : "=m" (fpu->state->fxsave));
  160. This, however, we can work around by forcing the compiler to select
  161. an addressing mode that doesn't require extended registers. */
  162. asm volatile("rex64/fxsave (%[fx])"
  163. : "=m" (fpu->state->fxsave)
  164. : [fx] "R" (&fpu->state->fxsave));
  165. #endif
  166. }
  167. #else /* CONFIG_X86_32 */
  168. /* perform fxrstor iff the processor has extended states, otherwise frstor */
  169. static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
  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" (*fx));
  180. return 0;
  181. }
  182. static inline void fpu_fxsave(struct fpu *fpu)
  183. {
  184. asm volatile("fxsave %[fx]"
  185. : [fx] "=m" (fpu->state->fxsave));
  186. }
  187. #endif /* CONFIG_X86_64 */
  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 fpu_save_init(struct fpu *fpu)
  200. {
  201. if (use_xsave()) {
  202. fpu_xsave(fpu);
  203. /*
  204. * xsave header may indicate the init state of the FP.
  205. */
  206. if (!(fpu->state->xsave.xsave_hdr.xstate_bv & XSTATE_FP))
  207. return;
  208. } else if (use_fxsr()) {
  209. fpu_fxsave(fpu);
  210. } else {
  211. asm volatile("fnsave %[fx]; fwait"
  212. : [fx] "=m" (fpu->state->fsave));
  213. return;
  214. }
  215. if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES))
  216. asm volatile("fnclex");
  217. /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception
  218. is pending. Clear the x87 state here by setting it to fixed
  219. values. safe_address is a random variable that should be in L1 */
  220. alternative_input(
  221. ASM_NOP8 ASM_NOP2,
  222. "emms\n\t" /* clear stack tags */
  223. "fildl %P[addr]", /* set F?P to defined value */
  224. X86_FEATURE_FXSAVE_LEAK,
  225. [addr] "m" (safe_address));
  226. }
  227. static inline void __save_init_fpu(struct task_struct *tsk)
  228. {
  229. fpu_save_init(&tsk->thread.fpu);
  230. task_thread_info(tsk)->status &= ~TS_USEDFPU;
  231. }
  232. static inline int fpu_fxrstor_checking(struct fpu *fpu)
  233. {
  234. return fxrstor_checking(&fpu->state->fxsave);
  235. }
  236. static inline int fpu_restore_checking(struct fpu *fpu)
  237. {
  238. if (use_xsave())
  239. return fpu_xrstor_checking(fpu);
  240. else
  241. return fpu_fxrstor_checking(fpu);
  242. }
  243. static inline int restore_fpu_checking(struct task_struct *tsk)
  244. {
  245. return fpu_restore_checking(&tsk->thread.fpu);
  246. }
  247. /*
  248. * Signal frame handlers...
  249. */
  250. extern int save_i387_xstate(void __user *buf);
  251. extern int restore_i387_xstate(void __user *buf);
  252. static inline void __unlazy_fpu(struct task_struct *tsk)
  253. {
  254. if (task_thread_info(tsk)->status & TS_USEDFPU) {
  255. __save_init_fpu(tsk);
  256. stts();
  257. } else
  258. tsk->fpu_counter = 0;
  259. }
  260. static inline void __clear_fpu(struct task_struct *tsk)
  261. {
  262. if (task_thread_info(tsk)->status & TS_USEDFPU) {
  263. /* Ignore delayed exceptions from user space */
  264. asm volatile("1: fwait\n"
  265. "2:\n"
  266. _ASM_EXTABLE(1b, 2b));
  267. task_thread_info(tsk)->status &= ~TS_USEDFPU;
  268. stts();
  269. }
  270. }
  271. static inline void kernel_fpu_begin(void)
  272. {
  273. struct thread_info *me = current_thread_info();
  274. preempt_disable();
  275. if (me->status & TS_USEDFPU)
  276. __save_init_fpu(me->task);
  277. else
  278. clts();
  279. }
  280. static inline void kernel_fpu_end(void)
  281. {
  282. stts();
  283. preempt_enable();
  284. }
  285. static inline bool irq_fpu_usable(void)
  286. {
  287. struct pt_regs *regs;
  288. return !in_interrupt() || !(regs = get_irq_regs()) || \
  289. user_mode(regs) || (read_cr0() & X86_CR0_TS);
  290. }
  291. /*
  292. * Some instructions like VIA's padlock instructions generate a spurious
  293. * DNA fault but don't modify SSE registers. And these instructions
  294. * get used from interrupt context as well. To prevent these kernel instructions
  295. * in interrupt context interacting wrongly with other user/kernel fpu usage, we
  296. * should use them only in the context of irq_ts_save/restore()
  297. */
  298. static inline int irq_ts_save(void)
  299. {
  300. /*
  301. * If in process context and not atomic, we can take a spurious DNA fault.
  302. * Otherwise, doing clts() in process context requires disabling preemption
  303. * or some heavy lifting like kernel_fpu_begin()
  304. */
  305. if (!in_atomic())
  306. return 0;
  307. if (read_cr0() & X86_CR0_TS) {
  308. clts();
  309. return 1;
  310. }
  311. return 0;
  312. }
  313. static inline void irq_ts_restore(int TS_state)
  314. {
  315. if (TS_state)
  316. stts();
  317. }
  318. /*
  319. * These disable preemption on their own and are safe
  320. */
  321. static inline void save_init_fpu(struct task_struct *tsk)
  322. {
  323. preempt_disable();
  324. __save_init_fpu(tsk);
  325. stts();
  326. preempt_enable();
  327. }
  328. static inline void unlazy_fpu(struct task_struct *tsk)
  329. {
  330. preempt_disable();
  331. __unlazy_fpu(tsk);
  332. preempt_enable();
  333. }
  334. static inline void clear_fpu(struct task_struct *tsk)
  335. {
  336. preempt_disable();
  337. __clear_fpu(tsk);
  338. preempt_enable();
  339. }
  340. /*
  341. * i387 state interaction
  342. */
  343. static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
  344. {
  345. if (cpu_has_fxsr) {
  346. return tsk->thread.fpu.state->fxsave.cwd;
  347. } else {
  348. return (unsigned short)tsk->thread.fpu.state->fsave.cwd;
  349. }
  350. }
  351. static inline unsigned short get_fpu_swd(struct task_struct *tsk)
  352. {
  353. if (cpu_has_fxsr) {
  354. return tsk->thread.fpu.state->fxsave.swd;
  355. } else {
  356. return (unsigned short)tsk->thread.fpu.state->fsave.swd;
  357. }
  358. }
  359. static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
  360. {
  361. if (cpu_has_xmm) {
  362. return tsk->thread.fpu.state->fxsave.mxcsr;
  363. } else {
  364. return MXCSR_DEFAULT;
  365. }
  366. }
  367. static bool fpu_allocated(struct fpu *fpu)
  368. {
  369. return fpu->state != NULL;
  370. }
  371. static inline int fpu_alloc(struct fpu *fpu)
  372. {
  373. if (fpu_allocated(fpu))
  374. return 0;
  375. fpu->state = kmem_cache_alloc(task_xstate_cachep, GFP_KERNEL);
  376. if (!fpu->state)
  377. return -ENOMEM;
  378. WARN_ON((unsigned long)fpu->state & 15);
  379. return 0;
  380. }
  381. static inline void fpu_free(struct fpu *fpu)
  382. {
  383. if (fpu->state) {
  384. kmem_cache_free(task_xstate_cachep, fpu->state);
  385. fpu->state = NULL;
  386. }
  387. }
  388. static inline void fpu_copy(struct fpu *dst, struct fpu *src)
  389. {
  390. memcpy(dst->state, src->state, xstate_size);
  391. }
  392. extern void fpu_finit(struct fpu *fpu);
  393. #endif /* __ASSEMBLY__ */
  394. #endif /* _ASM_X86_I387_H */