fpu-internal.h 13 KB

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