fpu-internal.h 16 KB

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