/arch/x86/include/asm/i387.h

https://bitbucket.org/ndreys/linux-sunxi · C++ Header · 627 lines · 394 code · 72 blank · 161 comment · 37 complexity · 96f776a9fa69db8fb8d4ccdfeb76f546 MD5 · raw file

  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 void __math_state_restore(struct task_struct *);
  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. /*
  189. * These must be called with preempt disabled. Returns
  190. * 'true' if the FPU state is still intact.
  191. */
  192. static inline int fpu_save_init(struct fpu *fpu)
  193. {
  194. if (use_xsave()) {
  195. fpu_xsave(fpu);
  196. /*
  197. * xsave header may indicate the init state of the FP.
  198. */
  199. if (!(fpu->state->xsave.xsave_hdr.xstate_bv & XSTATE_FP))
  200. return 1;
  201. } else if (use_fxsr()) {
  202. fpu_fxsave(fpu);
  203. } else {
  204. asm volatile("fnsave %[fx]; fwait"
  205. : [fx] "=m" (fpu->state->fsave));
  206. return 0;
  207. }
  208. /*
  209. * If exceptions are pending, we need to clear them so
  210. * that we don't randomly get exceptions later.
  211. *
  212. * FIXME! Is this perhaps only true for the old-style
  213. * irq13 case? Maybe we could leave the x87 state
  214. * intact otherwise?
  215. */
  216. if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES)) {
  217. asm volatile("fnclex");
  218. return 0;
  219. }
  220. return 1;
  221. }
  222. static inline int __save_init_fpu(struct task_struct *tsk)
  223. {
  224. return fpu_save_init(&tsk->thread.fpu);
  225. }
  226. static inline int fpu_fxrstor_checking(struct fpu *fpu)
  227. {
  228. return fxrstor_checking(&fpu->state->fxsave);
  229. }
  230. static inline int fpu_restore_checking(struct fpu *fpu)
  231. {
  232. if (use_xsave())
  233. return fpu_xrstor_checking(fpu);
  234. else
  235. return fpu_fxrstor_checking(fpu);
  236. }
  237. static inline int restore_fpu_checking(struct task_struct *tsk)
  238. {
  239. return fpu_restore_checking(&tsk->thread.fpu);
  240. }
  241. /*
  242. * Software FPU state helpers. Careful: these need to
  243. * be preemption protection *and* they need to be
  244. * properly paired with the CR0.TS changes!
  245. */
  246. static inline int __thread_has_fpu(struct task_struct *tsk)
  247. {
  248. return tsk->thread.has_fpu;
  249. }
  250. /* Must be paired with an 'stts' after! */
  251. static inline void __thread_clear_has_fpu(struct task_struct *tsk)
  252. {
  253. tsk->thread.has_fpu = 0;
  254. }
  255. /* Must be paired with a 'clts' before! */
  256. static inline void __thread_set_has_fpu(struct task_struct *tsk)
  257. {
  258. tsk->thread.has_fpu = 1;
  259. }
  260. /*
  261. * Encapsulate the CR0.TS handling together with the
  262. * software flag.
  263. *
  264. * These generally need preemption protection to work,
  265. * do try to avoid using these on their own.
  266. */
  267. static inline void __thread_fpu_end(struct task_struct *tsk)
  268. {
  269. __thread_clear_has_fpu(tsk);
  270. stts();
  271. }
  272. static inline void __thread_fpu_begin(struct task_struct *tsk)
  273. {
  274. clts();
  275. __thread_set_has_fpu(tsk);
  276. }
  277. /*
  278. * FPU state switching for scheduling.
  279. *
  280. * This is a two-stage process:
  281. *
  282. * - switch_fpu_prepare() saves the old state and
  283. * sets the new state of the CR0.TS bit. This is
  284. * done within the context of the old process.
  285. *
  286. * - switch_fpu_finish() restores the new state as
  287. * necessary.
  288. */
  289. typedef struct { int preload; } fpu_switch_t;
  290. /*
  291. * FIXME! We could do a totally lazy restore, but we need to
  292. * add a per-cpu "this was the task that last touched the FPU
  293. * on this CPU" variable, and the task needs to have a "I last
  294. * touched the FPU on this CPU" and check them.
  295. *
  296. * We don't do that yet, so "fpu_lazy_restore()" always returns
  297. * false, but some day..
  298. */
  299. #define fpu_lazy_restore(tsk) (0)
  300. #define fpu_lazy_state_intact(tsk) do { } while (0)
  301. static inline fpu_switch_t switch_fpu_prepare(struct task_struct *old, struct task_struct *new)
  302. {
  303. fpu_switch_t fpu;
  304. fpu.preload = tsk_used_math(new) && new->fpu_counter > 5;
  305. if (__thread_has_fpu(old)) {
  306. if (__save_init_fpu(old))
  307. fpu_lazy_state_intact(old);
  308. __thread_clear_has_fpu(old);
  309. old->fpu_counter++;
  310. /* Don't change CR0.TS if we just switch! */
  311. if (fpu.preload) {
  312. __thread_set_has_fpu(new);
  313. prefetch(new->thread.fpu.state);
  314. } else
  315. stts();
  316. } else {
  317. old->fpu_counter = 0;
  318. if (fpu.preload) {
  319. if (fpu_lazy_restore(new))
  320. fpu.preload = 0;
  321. else
  322. prefetch(new->thread.fpu.state);
  323. __thread_fpu_begin(new);
  324. }
  325. }
  326. return fpu;
  327. }
  328. /*
  329. * By the time this gets called, we've already cleared CR0.TS and
  330. * given the process the FPU if we are going to preload the FPU
  331. * state - all we need to do is to conditionally restore the register
  332. * state itself.
  333. */
  334. static inline void switch_fpu_finish(struct task_struct *new, fpu_switch_t fpu)
  335. {
  336. if (fpu.preload)
  337. __math_state_restore(new);
  338. }
  339. /*
  340. * Signal frame handlers...
  341. */
  342. extern int save_i387_xstate(void __user *buf);
  343. extern int restore_i387_xstate(void __user *buf);
  344. static inline void __clear_fpu(struct task_struct *tsk)
  345. {
  346. if (__thread_has_fpu(tsk)) {
  347. /* Ignore delayed exceptions from user space */
  348. asm volatile("1: fwait\n"
  349. "2:\n"
  350. _ASM_EXTABLE(1b, 2b));
  351. __thread_fpu_end(tsk);
  352. }
  353. }
  354. /*
  355. * Were we in an interrupt that interrupted kernel mode?
  356. *
  357. * We can do a kernel_fpu_begin/end() pair *ONLY* if that
  358. * pair does nothing at all: the thread must not have fpu (so
  359. * that we don't try to save the FPU state), and TS must
  360. * be set (so that the clts/stts pair does nothing that is
  361. * visible in the interrupted kernel thread).
  362. */
  363. static inline bool interrupted_kernel_fpu_idle(void)
  364. {
  365. return !__thread_has_fpu(current) &&
  366. (read_cr0() & X86_CR0_TS);
  367. }
  368. /*
  369. * Were we in user mode (or vm86 mode) when we were
  370. * interrupted?
  371. *
  372. * Doing kernel_fpu_begin/end() is ok if we are running
  373. * in an interrupt context from user mode - we'll just
  374. * save the FPU state as required.
  375. */
  376. static inline bool interrupted_user_mode(void)
  377. {
  378. struct pt_regs *regs = get_irq_regs();
  379. return regs && user_mode_vm(regs);
  380. }
  381. /*
  382. * Can we use the FPU in kernel mode with the
  383. * whole "kernel_fpu_begin/end()" sequence?
  384. *
  385. * It's always ok in process context (ie "not interrupt")
  386. * but it is sometimes ok even from an irq.
  387. */
  388. static inline bool irq_fpu_usable(void)
  389. {
  390. return !in_interrupt() ||
  391. interrupted_user_mode() ||
  392. interrupted_kernel_fpu_idle();
  393. }
  394. static inline void kernel_fpu_begin(void)
  395. {
  396. struct task_struct *me = current;
  397. WARN_ON_ONCE(!irq_fpu_usable());
  398. preempt_disable();
  399. if (__thread_has_fpu(me)) {
  400. __save_init_fpu(me);
  401. __thread_clear_has_fpu(me);
  402. /* We do 'stts()' in kernel_fpu_end() */
  403. } else
  404. clts();
  405. }
  406. static inline void kernel_fpu_end(void)
  407. {
  408. stts();
  409. preempt_enable();
  410. }
  411. /*
  412. * Some instructions like VIA's padlock instructions generate a spurious
  413. * DNA fault but don't modify SSE registers. And these instructions
  414. * get used from interrupt context as well. To prevent these kernel instructions
  415. * in interrupt context interacting wrongly with other user/kernel fpu usage, we
  416. * should use them only in the context of irq_ts_save/restore()
  417. */
  418. static inline int irq_ts_save(void)
  419. {
  420. /*
  421. * If in process context and not atomic, we can take a spurious DNA fault.
  422. * Otherwise, doing clts() in process context requires disabling preemption
  423. * or some heavy lifting like kernel_fpu_begin()
  424. */
  425. if (!in_atomic())
  426. return 0;
  427. if (read_cr0() & X86_CR0_TS) {
  428. clts();
  429. return 1;
  430. }
  431. return 0;
  432. }
  433. static inline void irq_ts_restore(int TS_state)
  434. {
  435. if (TS_state)
  436. stts();
  437. }
  438. /*
  439. * The question "does this thread have fpu access?"
  440. * is slightly racy, since preemption could come in
  441. * and revoke it immediately after the test.
  442. *
  443. * However, even in that very unlikely scenario,
  444. * we can just assume we have FPU access - typically
  445. * to save the FP state - we'll just take a #NM
  446. * fault and get the FPU access back.
  447. *
  448. * The actual user_fpu_begin/end() functions
  449. * need to be preemption-safe, though.
  450. *
  451. * NOTE! user_fpu_end() must be used only after you
  452. * have saved the FP state, and user_fpu_begin() must
  453. * be used only immediately before restoring it.
  454. * These functions do not do any save/restore on
  455. * their own.
  456. */
  457. static inline int user_has_fpu(void)
  458. {
  459. return __thread_has_fpu(current);
  460. }
  461. static inline void user_fpu_end(void)
  462. {
  463. preempt_disable();
  464. __thread_fpu_end(current);
  465. preempt_enable();
  466. }
  467. static inline void user_fpu_begin(void)
  468. {
  469. preempt_disable();
  470. if (!user_has_fpu())
  471. __thread_fpu_begin(current);
  472. preempt_enable();
  473. }
  474. /*
  475. * These disable preemption on their own and are safe
  476. */
  477. static inline void save_init_fpu(struct task_struct *tsk)
  478. {
  479. WARN_ON_ONCE(!__thread_has_fpu(tsk));
  480. preempt_disable();
  481. __save_init_fpu(tsk);
  482. __thread_fpu_end(tsk);
  483. preempt_enable();
  484. }
  485. static inline void unlazy_fpu(struct task_struct *tsk)
  486. {
  487. preempt_disable();
  488. if (__thread_has_fpu(tsk)) {
  489. __save_init_fpu(tsk);
  490. __thread_fpu_end(tsk);
  491. } else
  492. tsk->fpu_counter = 0;
  493. preempt_enable();
  494. }
  495. static inline void clear_fpu(struct task_struct *tsk)
  496. {
  497. preempt_disable();
  498. __clear_fpu(tsk);
  499. preempt_enable();
  500. }
  501. /*
  502. * i387 state interaction
  503. */
  504. static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
  505. {
  506. if (cpu_has_fxsr) {
  507. return tsk->thread.fpu.state->fxsave.cwd;
  508. } else {
  509. return (unsigned short)tsk->thread.fpu.state->fsave.cwd;
  510. }
  511. }
  512. static inline unsigned short get_fpu_swd(struct task_struct *tsk)
  513. {
  514. if (cpu_has_fxsr) {
  515. return tsk->thread.fpu.state->fxsave.swd;
  516. } else {
  517. return (unsigned short)tsk->thread.fpu.state->fsave.swd;
  518. }
  519. }
  520. static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
  521. {
  522. if (cpu_has_xmm) {
  523. return tsk->thread.fpu.state->fxsave.mxcsr;
  524. } else {
  525. return MXCSR_DEFAULT;
  526. }
  527. }
  528. static bool fpu_allocated(struct fpu *fpu)
  529. {
  530. return fpu->state != NULL;
  531. }
  532. static inline int fpu_alloc(struct fpu *fpu)
  533. {
  534. if (fpu_allocated(fpu))
  535. return 0;
  536. fpu->state = kmem_cache_alloc(task_xstate_cachep, GFP_KERNEL);
  537. if (!fpu->state)
  538. return -ENOMEM;
  539. WARN_ON((unsigned long)fpu->state & 15);
  540. return 0;
  541. }
  542. static inline void fpu_free(struct fpu *fpu)
  543. {
  544. if (fpu->state) {
  545. kmem_cache_free(task_xstate_cachep, fpu->state);
  546. fpu->state = NULL;
  547. }
  548. }
  549. static inline void fpu_copy(struct fpu *dst, struct fpu *src)
  550. {
  551. memcpy(dst->state, src->state, xstate_size);
  552. }
  553. extern void fpu_finit(struct fpu *fpu);
  554. #endif /* __ASSEMBLY__ */
  555. #endif /* _ASM_X86_I387_H */