fpu-internal.h 16 KB

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