fpu-internal.h 13 KB

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