i387.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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(void);
  29. extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);
  30. extern user_regset_active_fn fpregs_active, xfpregs_active;
  31. extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get,
  32. xstateregs_get;
  33. extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set,
  34. xstateregs_set;
  35. /*
  36. * xstateregs_active == fpregs_active. Please refer to the comment
  37. * at the definition of fpregs_active.
  38. */
  39. #define xstateregs_active fpregs_active
  40. extern struct _fpx_sw_bytes fx_sw_reserved;
  41. #ifdef CONFIG_IA32_EMULATION
  42. extern unsigned int sig_xstate_ia32_size;
  43. extern struct _fpx_sw_bytes fx_sw_reserved_ia32;
  44. struct _fpstate_ia32;
  45. struct _xstate_ia32;
  46. extern int save_i387_xstate_ia32(void __user *buf);
  47. extern int restore_i387_xstate_ia32(void __user *buf);
  48. #endif
  49. #ifdef CONFIG_MATH_EMULATION
  50. extern void finit_soft_fpu(struct i387_soft_struct *soft);
  51. #else
  52. static inline void finit_soft_fpu(struct i387_soft_struct *soft) {}
  53. #endif
  54. #define X87_FSW_ES (1 << 7) /* Exception Summary */
  55. static __always_inline __pure bool use_xsaveopt(void)
  56. {
  57. return static_cpu_has(X86_FEATURE_XSAVEOPT);
  58. }
  59. static __always_inline __pure bool use_xsave(void)
  60. {
  61. return static_cpu_has(X86_FEATURE_XSAVE);
  62. }
  63. static __always_inline __pure bool use_fxsr(void)
  64. {
  65. return static_cpu_has(X86_FEATURE_FXSR);
  66. }
  67. extern void __sanitize_i387_state(struct task_struct *);
  68. static inline void sanitize_i387_state(struct task_struct *tsk)
  69. {
  70. if (!use_xsaveopt())
  71. return;
  72. __sanitize_i387_state(tsk);
  73. }
  74. #ifdef CONFIG_X86_64
  75. static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
  76. {
  77. int err;
  78. /* See comment in fxsave() below. */
  79. #ifdef CONFIG_AS_FXSAVEQ
  80. asm volatile("1: fxrstorq %[fx]\n\t"
  81. "2:\n"
  82. ".section .fixup,\"ax\"\n"
  83. "3: movl $-1,%[err]\n"
  84. " jmp 2b\n"
  85. ".previous\n"
  86. _ASM_EXTABLE(1b, 3b)
  87. : [err] "=r" (err)
  88. : [fx] "m" (*fx), "0" (0));
  89. #else
  90. asm volatile("1: rex64/fxrstor (%[fx])\n\t"
  91. "2:\n"
  92. ".section .fixup,\"ax\"\n"
  93. "3: movl $-1,%[err]\n"
  94. " jmp 2b\n"
  95. ".previous\n"
  96. _ASM_EXTABLE(1b, 3b)
  97. : [err] "=r" (err)
  98. : [fx] "R" (fx), "m" (*fx), "0" (0));
  99. #endif
  100. return err;
  101. }
  102. static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
  103. {
  104. int err;
  105. /*
  106. * Clear the bytes not touched by the fxsave and reserved
  107. * for the SW usage.
  108. */
  109. err = __clear_user(&fx->sw_reserved,
  110. sizeof(struct _fpx_sw_bytes));
  111. if (unlikely(err))
  112. return -EFAULT;
  113. /* See comment in fxsave() below. */
  114. #ifdef CONFIG_AS_FXSAVEQ
  115. asm volatile("1: fxsaveq %[fx]\n\t"
  116. "2:\n"
  117. ".section .fixup,\"ax\"\n"
  118. "3: movl $-1,%[err]\n"
  119. " jmp 2b\n"
  120. ".previous\n"
  121. _ASM_EXTABLE(1b, 3b)
  122. : [err] "=r" (err), [fx] "=m" (*fx)
  123. : "0" (0));
  124. #else
  125. asm volatile("1: rex64/fxsave (%[fx])\n\t"
  126. "2:\n"
  127. ".section .fixup,\"ax\"\n"
  128. "3: movl $-1,%[err]\n"
  129. " jmp 2b\n"
  130. ".previous\n"
  131. _ASM_EXTABLE(1b, 3b)
  132. : [err] "=r" (err), "=m" (*fx)
  133. : [fx] "R" (fx), "0" (0));
  134. #endif
  135. if (unlikely(err) &&
  136. __clear_user(fx, sizeof(struct i387_fxsave_struct)))
  137. err = -EFAULT;
  138. /* No need to clear here because the caller clears USED_MATH */
  139. return err;
  140. }
  141. static inline void fpu_fxsave(struct fpu *fpu)
  142. {
  143. /* Using "rex64; fxsave %0" is broken because, if the memory operand
  144. uses any extended registers for addressing, a second REX prefix
  145. will be generated (to the assembler, rex64 followed by semicolon
  146. is a separate instruction), and hence the 64-bitness is lost. */
  147. #ifdef CONFIG_AS_FXSAVEQ
  148. /* Using "fxsaveq %0" would be the ideal choice, but is only supported
  149. starting with gas 2.16. */
  150. __asm__ __volatile__("fxsaveq %0"
  151. : "=m" (fpu->state->fxsave));
  152. #else
  153. /* Using, as a workaround, the properly prefixed form below isn't
  154. accepted by any binutils version so far released, complaining that
  155. the same type of prefix is used twice if an extended register is
  156. needed for addressing (fix submitted to mainline 2005-11-21).
  157. asm volatile("rex64/fxsave %0"
  158. : "=m" (fpu->state->fxsave));
  159. This, however, we can work around by forcing the compiler to select
  160. an addressing mode that doesn't require extended registers. */
  161. asm volatile("rex64/fxsave (%[fx])"
  162. : "=m" (fpu->state->fxsave)
  163. : [fx] "R" (&fpu->state->fxsave));
  164. #endif
  165. }
  166. #else /* CONFIG_X86_32 */
  167. /* perform fxrstor iff the processor has extended states, otherwise frstor */
  168. static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
  169. {
  170. /*
  171. * The "nop" is needed to make the instructions the same
  172. * length.
  173. */
  174. alternative_input(
  175. "nop ; frstor %1",
  176. "fxrstor %1",
  177. X86_FEATURE_FXSR,
  178. "m" (*fx));
  179. return 0;
  180. }
  181. static inline void fpu_fxsave(struct fpu *fpu)
  182. {
  183. asm volatile("fxsave %[fx]"
  184. : [fx] "=m" (fpu->state->fxsave));
  185. }
  186. #endif /* CONFIG_X86_64 */
  187. /*
  188. * These must be called with preempt disabled. Returns
  189. * 'true' if the FPU state is still intact.
  190. */
  191. static inline int fpu_save_init(struct fpu *fpu)
  192. {
  193. if (use_xsave()) {
  194. fpu_xsave(fpu);
  195. /*
  196. * xsave header may indicate the init state of the FP.
  197. */
  198. if (!(fpu->state->xsave.xsave_hdr.xstate_bv & XSTATE_FP))
  199. return 1;
  200. } else if (use_fxsr()) {
  201. fpu_fxsave(fpu);
  202. } else {
  203. asm volatile("fnsave %[fx]; fwait"
  204. : [fx] "=m" (fpu->state->fsave));
  205. return 0;
  206. }
  207. /*
  208. * If exceptions are pending, we need to clear them so
  209. * that we don't randomly get exceptions later.
  210. *
  211. * FIXME! Is this perhaps only true for the old-style
  212. * irq13 case? Maybe we could leave the x87 state
  213. * intact otherwise?
  214. */
  215. if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES)) {
  216. asm volatile("fnclex");
  217. return 0;
  218. }
  219. return 1;
  220. }
  221. static inline int __save_init_fpu(struct task_struct *tsk)
  222. {
  223. return fpu_save_init(&tsk->thread.fpu);
  224. }
  225. static inline int fpu_fxrstor_checking(struct fpu *fpu)
  226. {
  227. return fxrstor_checking(&fpu->state->fxsave);
  228. }
  229. static inline int fpu_restore_checking(struct fpu *fpu)
  230. {
  231. if (use_xsave())
  232. return fpu_xrstor_checking(fpu);
  233. else
  234. return fpu_fxrstor_checking(fpu);
  235. }
  236. static inline int restore_fpu_checking(struct task_struct *tsk)
  237. {
  238. /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception
  239. is pending. Clear the x87 state here by setting it to fixed
  240. values. "m" is a random variable that should be in L1 */
  241. alternative_input(
  242. ASM_NOP8 ASM_NOP2,
  243. "emms\n\t" /* clear stack tags */
  244. "fildl %P[addr]", /* set F?P to defined value */
  245. X86_FEATURE_FXSAVE_LEAK,
  246. [addr] "m" (tsk->thread.has_fpu));
  247. return fpu_restore_checking(&tsk->thread.fpu);
  248. }
  249. /*
  250. * Software FPU state helpers. Careful: these need to
  251. * be preemption protection *and* they need to be
  252. * properly paired with the CR0.TS changes!
  253. */
  254. static inline int __thread_has_fpu(struct task_struct *tsk)
  255. {
  256. return tsk->thread.has_fpu;
  257. }
  258. /* Must be paired with an 'stts' after! */
  259. static inline void __thread_clear_has_fpu(struct task_struct *tsk)
  260. {
  261. tsk->thread.has_fpu = 0;
  262. }
  263. /* Must be paired with a 'clts' before! */
  264. static inline void __thread_set_has_fpu(struct task_struct *tsk)
  265. {
  266. tsk->thread.has_fpu = 1;
  267. }
  268. /*
  269. * Encapsulate the CR0.TS handling together with the
  270. * software flag.
  271. *
  272. * These generally need preemption protection to work,
  273. * do try to avoid using these on their own.
  274. */
  275. static inline void __thread_fpu_end(struct task_struct *tsk)
  276. {
  277. __thread_clear_has_fpu(tsk);
  278. stts();
  279. }
  280. static inline void __thread_fpu_begin(struct task_struct *tsk)
  281. {
  282. clts();
  283. __thread_set_has_fpu(tsk);
  284. }
  285. /*
  286. * FPU state switching for scheduling.
  287. *
  288. * This is a two-stage process:
  289. *
  290. * - switch_fpu_prepare() saves the old state and
  291. * sets the new state of the CR0.TS bit. This is
  292. * done within the context of the old process.
  293. *
  294. * - switch_fpu_finish() restores the new state as
  295. * necessary.
  296. */
  297. typedef struct { int preload; } fpu_switch_t;
  298. /*
  299. * FIXME! We could do a totally lazy restore, but we need to
  300. * add a per-cpu "this was the task that last touched the FPU
  301. * on this CPU" variable, and the task needs to have a "I last
  302. * touched the FPU on this CPU" and check them.
  303. *
  304. * We don't do that yet, so "fpu_lazy_restore()" always returns
  305. * false, but some day..
  306. */
  307. #define fpu_lazy_restore(tsk) (0)
  308. #define fpu_lazy_state_intact(tsk) do { } while (0)
  309. static inline fpu_switch_t switch_fpu_prepare(struct task_struct *old, struct task_struct *new)
  310. {
  311. fpu_switch_t fpu;
  312. fpu.preload = tsk_used_math(new) && new->fpu_counter > 5;
  313. if (__thread_has_fpu(old)) {
  314. if (__save_init_fpu(old))
  315. fpu_lazy_state_intact(old);
  316. __thread_clear_has_fpu(old);
  317. /* Don't change CR0.TS if we just switch! */
  318. if (fpu.preload) {
  319. new->fpu_counter++;
  320. __thread_set_has_fpu(new);
  321. prefetch(new->thread.fpu.state);
  322. } else
  323. stts();
  324. } else {
  325. old->fpu_counter = 0;
  326. if (fpu.preload) {
  327. new->fpu_counter++;
  328. if (fpu_lazy_restore(new))
  329. fpu.preload = 0;
  330. else
  331. prefetch(new->thread.fpu.state);
  332. __thread_fpu_begin(new);
  333. }
  334. }
  335. return fpu;
  336. }
  337. /*
  338. * By the time this gets called, we've already cleared CR0.TS and
  339. * given the process the FPU if we are going to preload the FPU
  340. * state - all we need to do is to conditionally restore the register
  341. * state itself.
  342. */
  343. static inline void switch_fpu_finish(struct task_struct *new, fpu_switch_t fpu)
  344. {
  345. if (fpu.preload) {
  346. if (unlikely(restore_fpu_checking(new)))
  347. __thread_fpu_end(new);
  348. }
  349. }
  350. /*
  351. * Signal frame handlers...
  352. */
  353. extern int save_i387_xstate(void __user *buf);
  354. extern int restore_i387_xstate(void __user *buf);
  355. static inline void __clear_fpu(struct task_struct *tsk)
  356. {
  357. if (__thread_has_fpu(tsk)) {
  358. /* Ignore delayed exceptions from user space */
  359. asm volatile("1: fwait\n"
  360. "2:\n"
  361. _ASM_EXTABLE(1b, 2b));
  362. __thread_fpu_end(tsk);
  363. }
  364. }
  365. /*
  366. * Were we in an interrupt that interrupted kernel mode?
  367. *
  368. * We can do a kernel_fpu_begin/end() pair *ONLY* if that
  369. * pair does nothing at all: the thread must not have fpu (so
  370. * that we don't try to save the FPU state), and TS must
  371. * be set (so that the clts/stts pair does nothing that is
  372. * visible in the interrupted kernel thread).
  373. */
  374. static inline bool interrupted_kernel_fpu_idle(void)
  375. {
  376. return !__thread_has_fpu(current) &&
  377. (read_cr0() & X86_CR0_TS);
  378. }
  379. /*
  380. * Were we in user mode (or vm86 mode) when we were
  381. * interrupted?
  382. *
  383. * Doing kernel_fpu_begin/end() is ok if we are running
  384. * in an interrupt context from user mode - we'll just
  385. * save the FPU state as required.
  386. */
  387. static inline bool interrupted_user_mode(void)
  388. {
  389. struct pt_regs *regs = get_irq_regs();
  390. return regs && user_mode_vm(regs);
  391. }
  392. /*
  393. * Can we use the FPU in kernel mode with the
  394. * whole "kernel_fpu_begin/end()" sequence?
  395. *
  396. * It's always ok in process context (ie "not interrupt")
  397. * but it is sometimes ok even from an irq.
  398. */
  399. static inline bool irq_fpu_usable(void)
  400. {
  401. return !in_interrupt() ||
  402. interrupted_user_mode() ||
  403. interrupted_kernel_fpu_idle();
  404. }
  405. static inline void kernel_fpu_begin(void)
  406. {
  407. struct task_struct *me = current;
  408. WARN_ON_ONCE(!irq_fpu_usable());
  409. preempt_disable();
  410. if (__thread_has_fpu(me)) {
  411. __save_init_fpu(me);
  412. __thread_clear_has_fpu(me);
  413. /* We do 'stts()' in kernel_fpu_end() */
  414. } else
  415. clts();
  416. }
  417. static inline void kernel_fpu_end(void)
  418. {
  419. stts();
  420. preempt_enable();
  421. }
  422. /*
  423. * Some instructions like VIA's padlock instructions generate a spurious
  424. * DNA fault but don't modify SSE registers. And these instructions
  425. * get used from interrupt context as well. To prevent these kernel instructions
  426. * in interrupt context interacting wrongly with other user/kernel fpu usage, we
  427. * should use them only in the context of irq_ts_save/restore()
  428. */
  429. static inline int irq_ts_save(void)
  430. {
  431. /*
  432. * If in process context and not atomic, we can take a spurious DNA fault.
  433. * Otherwise, doing clts() in process context requires disabling preemption
  434. * or some heavy lifting like kernel_fpu_begin()
  435. */
  436. if (!in_atomic())
  437. return 0;
  438. if (read_cr0() & X86_CR0_TS) {
  439. clts();
  440. return 1;
  441. }
  442. return 0;
  443. }
  444. static inline void irq_ts_restore(int TS_state)
  445. {
  446. if (TS_state)
  447. stts();
  448. }
  449. /*
  450. * The question "does this thread have fpu access?"
  451. * is slightly racy, since preemption could come in
  452. * and revoke it immediately after the test.
  453. *
  454. * However, even in that very unlikely scenario,
  455. * we can just assume we have FPU access - typically
  456. * to save the FP state - we'll just take a #NM
  457. * fault and get the FPU access back.
  458. *
  459. * The actual user_fpu_begin/end() functions
  460. * need to be preemption-safe, though.
  461. *
  462. * NOTE! user_fpu_end() must be used only after you
  463. * have saved the FP state, and user_fpu_begin() must
  464. * be used only immediately before restoring it.
  465. * These functions do not do any save/restore on
  466. * their own.
  467. */
  468. static inline int user_has_fpu(void)
  469. {
  470. return __thread_has_fpu(current);
  471. }
  472. static inline void user_fpu_end(void)
  473. {
  474. preempt_disable();
  475. __thread_fpu_end(current);
  476. preempt_enable();
  477. }
  478. static inline void user_fpu_begin(void)
  479. {
  480. preempt_disable();
  481. if (!user_has_fpu())
  482. __thread_fpu_begin(current);
  483. preempt_enable();
  484. }
  485. /*
  486. * These disable preemption on their own and are safe
  487. */
  488. static inline void save_init_fpu(struct task_struct *tsk)
  489. {
  490. WARN_ON_ONCE(!__thread_has_fpu(tsk));
  491. preempt_disable();
  492. __save_init_fpu(tsk);
  493. __thread_fpu_end(tsk);
  494. preempt_enable();
  495. }
  496. static inline void unlazy_fpu(struct task_struct *tsk)
  497. {
  498. preempt_disable();
  499. if (__thread_has_fpu(tsk)) {
  500. __save_init_fpu(tsk);
  501. __thread_fpu_end(tsk);
  502. } else
  503. tsk->fpu_counter = 0;
  504. preempt_enable();
  505. }
  506. static inline void clear_fpu(struct task_struct *tsk)
  507. {
  508. preempt_disable();
  509. __clear_fpu(tsk);
  510. preempt_enable();
  511. }
  512. /*
  513. * i387 state interaction
  514. */
  515. static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
  516. {
  517. if (cpu_has_fxsr) {
  518. return tsk->thread.fpu.state->fxsave.cwd;
  519. } else {
  520. return (unsigned short)tsk->thread.fpu.state->fsave.cwd;
  521. }
  522. }
  523. static inline unsigned short get_fpu_swd(struct task_struct *tsk)
  524. {
  525. if (cpu_has_fxsr) {
  526. return tsk->thread.fpu.state->fxsave.swd;
  527. } else {
  528. return (unsigned short)tsk->thread.fpu.state->fsave.swd;
  529. }
  530. }
  531. static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
  532. {
  533. if (cpu_has_xmm) {
  534. return tsk->thread.fpu.state->fxsave.mxcsr;
  535. } else {
  536. return MXCSR_DEFAULT;
  537. }
  538. }
  539. static bool fpu_allocated(struct fpu *fpu)
  540. {
  541. return fpu->state != NULL;
  542. }
  543. static inline int fpu_alloc(struct fpu *fpu)
  544. {
  545. if (fpu_allocated(fpu))
  546. return 0;
  547. fpu->state = kmem_cache_alloc(task_xstate_cachep, GFP_KERNEL);
  548. if (!fpu->state)
  549. return -ENOMEM;
  550. WARN_ON((unsigned long)fpu->state & 15);
  551. return 0;
  552. }
  553. static inline void fpu_free(struct fpu *fpu)
  554. {
  555. if (fpu->state) {
  556. kmem_cache_free(task_xstate_cachep, fpu->state);
  557. fpu->state = NULL;
  558. }
  559. }
  560. static inline void fpu_copy(struct fpu *dst, struct fpu *src)
  561. {
  562. memcpy(dst->state, src->state, xstate_size);
  563. }
  564. extern void fpu_finit(struct fpu *fpu);
  565. #endif /* __ASSEMBLY__ */
  566. #endif /* _ASM_X86_I387_H */