i387.h 12 KB

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