fpu-internal.h 15 KB

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