internal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 _ASM_X86_FPU_INTERNAL_H
  10. #define _ASM_X86_FPU_INTERNAL_H
  11. #include <linux/regset.h>
  12. #include <linux/compat.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <asm/user.h>
  16. #include <asm/fpu/api.h>
  17. #include <asm/fpu/xstate.h>
  18. #define MXCSR_DEFAULT 0x1f80
  19. extern unsigned int mxcsr_feature_mask;
  20. extern void fpu__init_cpu(void);
  21. extern void fpu__init_system_xstate(void);
  22. extern void fpu__init_cpu_xstate(void);
  23. extern void fpu__init_system(struct cpuinfo_x86 *c);
  24. extern void fpu__activate_curr(struct fpu *fpu);
  25. extern void fpstate_init(struct fpu *fpu);
  26. extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);
  27. /*
  28. * High level FPU state handling functions:
  29. */
  30. extern void fpu__save(struct fpu *fpu);
  31. extern void fpu__restore(void);
  32. extern int fpu__restore_sig(void __user *buf, int ia32_frame);
  33. extern void fpu__drop(struct fpu *fpu);
  34. extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
  35. extern void fpu__clear(struct fpu *fpu);
  36. extern void fpu__init_check_bugs(void);
  37. extern void fpu__resume_cpu(void);
  38. DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
  39. extern user_regset_active_fn regset_fpregs_active, regset_xregset_fpregs_active;
  40. extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get,
  41. xstateregs_get;
  42. extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set,
  43. xstateregs_set;
  44. /*
  45. * xstateregs_active == regset_fpregs_active. Please refer to the comment
  46. * at the definition of regset_fpregs_active.
  47. */
  48. #define xstateregs_active regset_fpregs_active
  49. #ifdef CONFIG_MATH_EMULATION
  50. extern void finit_soft_fpu(struct i387_soft_struct *soft);
  51. #else
  52. static inline void finit_soft_fpu(struct i387_soft_struct *soft) {}
  53. #endif
  54. /*
  55. * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
  56. * on this CPU.
  57. *
  58. * This will disable any lazy FPU state restore of the current FPU state,
  59. * but if the current thread owns the FPU, it will still be saved by.
  60. */
  61. static inline void __cpu_disable_lazy_restore(unsigned int cpu)
  62. {
  63. per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
  64. }
  65. static inline int fpu_want_lazy_restore(struct fpu *fpu, unsigned int cpu)
  66. {
  67. return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
  68. }
  69. #define X87_FSW_ES (1 << 7) /* Exception Summary */
  70. static __always_inline __pure bool use_eager_fpu(void)
  71. {
  72. return static_cpu_has_safe(X86_FEATURE_EAGER_FPU);
  73. }
  74. static __always_inline __pure bool use_xsaveopt(void)
  75. {
  76. return static_cpu_has_safe(X86_FEATURE_XSAVEOPT);
  77. }
  78. static __always_inline __pure bool use_xsave(void)
  79. {
  80. return static_cpu_has_safe(X86_FEATURE_XSAVE);
  81. }
  82. static __always_inline __pure bool use_fxsr(void)
  83. {
  84. return static_cpu_has_safe(X86_FEATURE_FXSR);
  85. }
  86. static inline void fx_finit(struct i387_fxsave_struct *fx)
  87. {
  88. fx->cwd = 0x37f;
  89. fx->mxcsr = MXCSR_DEFAULT;
  90. }
  91. extern void fpstate_sanitize_xstate(struct fpu *fpu);
  92. #define user_insn(insn, output, input...) \
  93. ({ \
  94. int err; \
  95. asm volatile(ASM_STAC "\n" \
  96. "1:" #insn "\n\t" \
  97. "2: " ASM_CLAC "\n" \
  98. ".section .fixup,\"ax\"\n" \
  99. "3: movl $-1,%[err]\n" \
  100. " jmp 2b\n" \
  101. ".previous\n" \
  102. _ASM_EXTABLE(1b, 3b) \
  103. : [err] "=r" (err), output \
  104. : "0"(0), input); \
  105. err; \
  106. })
  107. #define check_insn(insn, output, input...) \
  108. ({ \
  109. int err; \
  110. asm volatile("1:" #insn "\n\t" \
  111. "2:\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), output \
  118. : "0"(0), input); \
  119. err; \
  120. })
  121. static inline int fsave_user(struct i387_fsave_struct __user *fx)
  122. {
  123. return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
  124. }
  125. static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
  126. {
  127. if (config_enabled(CONFIG_X86_32))
  128. return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
  129. else if (config_enabled(CONFIG_AS_FXSAVEQ))
  130. return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
  131. /* See comment in fpu_fxsave() below. */
  132. return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
  133. }
  134. static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
  135. {
  136. if (config_enabled(CONFIG_X86_32))
  137. return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
  138. else if (config_enabled(CONFIG_AS_FXSAVEQ))
  139. return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
  140. /* See comment in fpu_fxsave() below. */
  141. return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
  142. "m" (*fx));
  143. }
  144. static inline int fxrstor_user(struct i387_fxsave_struct __user *fx)
  145. {
  146. if (config_enabled(CONFIG_X86_32))
  147. return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
  148. else if (config_enabled(CONFIG_AS_FXSAVEQ))
  149. return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
  150. /* See comment in fpu_fxsave() below. */
  151. return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
  152. "m" (*fx));
  153. }
  154. static inline int frstor_checking(struct i387_fsave_struct *fx)
  155. {
  156. return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
  157. }
  158. static inline int frstor_user(struct i387_fsave_struct __user *fx)
  159. {
  160. return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
  161. }
  162. static inline void fpu_fxsave(struct fpu *fpu)
  163. {
  164. if (config_enabled(CONFIG_X86_32))
  165. asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state.fxsave));
  166. else if (config_enabled(CONFIG_AS_FXSAVEQ))
  167. asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state.fxsave));
  168. else {
  169. /* Using "rex64; fxsave %0" is broken because, if the memory
  170. * operand uses any extended registers for addressing, a second
  171. * REX prefix will be generated (to the assembler, rex64
  172. * followed by semicolon is a separate instruction), and hence
  173. * the 64-bitness is lost.
  174. *
  175. * Using "fxsaveq %0" would be the ideal choice, but is only
  176. * supported starting with gas 2.16.
  177. *
  178. * Using, as a workaround, the properly prefixed form below
  179. * isn't accepted by any binutils version so far released,
  180. * complaining that the same type of prefix is used twice if
  181. * an extended register is needed for addressing (fix submitted
  182. * to mainline 2005-11-21).
  183. *
  184. * asm volatile("rex64/fxsave %0" : "=m" (fpu->state.fxsave));
  185. *
  186. * This, however, we can work around by forcing the compiler to
  187. * select an addressing mode that doesn't require extended
  188. * registers.
  189. */
  190. asm volatile( "rex64/fxsave (%[fx])"
  191. : "=m" (fpu->state.fxsave)
  192. : [fx] "R" (&fpu->state.fxsave));
  193. }
  194. }
  195. /*
  196. * These must be called with preempt disabled. Returns
  197. * 'true' if the FPU state is still intact and we can
  198. * keep registers active.
  199. *
  200. * The legacy FNSAVE instruction cleared all FPU state
  201. * unconditionally, so registers are essentially destroyed.
  202. * Modern FPU state can be kept in registers, if there are
  203. * no pending FP exceptions.
  204. */
  205. static inline int copy_fpregs_to_fpstate(struct fpu *fpu)
  206. {
  207. if (likely(use_xsave())) {
  208. xsave_state(&fpu->state.xsave);
  209. return 1;
  210. }
  211. if (likely(use_fxsr())) {
  212. fpu_fxsave(fpu);
  213. return 1;
  214. }
  215. /*
  216. * Legacy FPU register saving, FNSAVE always clears FPU registers,
  217. * so we have to mark them inactive:
  218. */
  219. asm volatile("fnsave %[fx]; fwait" : [fx] "=m" (fpu->state.fsave));
  220. return 0;
  221. }
  222. static inline int __copy_fpstate_to_fpregs(struct fpu *fpu)
  223. {
  224. if (use_xsave())
  225. return fpu_xrstor_checking(&fpu->state.xsave);
  226. else if (use_fxsr())
  227. return fxrstor_checking(&fpu->state.fxsave);
  228. else
  229. return frstor_checking(&fpu->state.fsave);
  230. }
  231. static inline int copy_fpstate_to_fpregs(struct fpu *fpu)
  232. {
  233. /*
  234. * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
  235. * pending. Clear the x87 state here by setting it to fixed values.
  236. * "m" is a random variable that should be in L1.
  237. */
  238. if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
  239. asm volatile(
  240. "fnclex\n\t"
  241. "emms\n\t"
  242. "fildl %P[addr]" /* set F?P to defined value */
  243. : : [addr] "m" (fpu->fpregs_active));
  244. }
  245. return __copy_fpstate_to_fpregs(fpu);
  246. }
  247. /*
  248. * Wrap lazy FPU TS handling in a 'hw fpregs activation/deactivation'
  249. * idiom, which is then paired with the sw-flag (fpregs_active) later on:
  250. */
  251. static inline void __fpregs_activate_hw(void)
  252. {
  253. if (!use_eager_fpu())
  254. clts();
  255. }
  256. static inline void __fpregs_deactivate_hw(void)
  257. {
  258. if (!use_eager_fpu())
  259. stts();
  260. }
  261. /* Must be paired with an 'stts' (fpregs_deactivate_hw()) after! */
  262. static inline void __fpregs_deactivate(struct fpu *fpu)
  263. {
  264. fpu->fpregs_active = 0;
  265. this_cpu_write(fpu_fpregs_owner_ctx, NULL);
  266. }
  267. /* Must be paired with a 'clts' (fpregs_activate_hw()) before! */
  268. static inline void __fpregs_activate(struct fpu *fpu)
  269. {
  270. fpu->fpregs_active = 1;
  271. this_cpu_write(fpu_fpregs_owner_ctx, fpu);
  272. }
  273. /*
  274. * The question "does this thread have fpu access?"
  275. * is slightly racy, since preemption could come in
  276. * and revoke it immediately after the test.
  277. *
  278. * However, even in that very unlikely scenario,
  279. * we can just assume we have FPU access - typically
  280. * to save the FP state - we'll just take a #NM
  281. * fault and get the FPU access back.
  282. */
  283. static inline int fpregs_active(void)
  284. {
  285. return current->thread.fpu.fpregs_active;
  286. }
  287. /*
  288. * Encapsulate the CR0.TS handling together with the
  289. * software flag.
  290. *
  291. * These generally need preemption protection to work,
  292. * do try to avoid using these on their own.
  293. */
  294. static inline void fpregs_activate(struct fpu *fpu)
  295. {
  296. __fpregs_activate_hw();
  297. __fpregs_activate(fpu);
  298. }
  299. static inline void fpregs_deactivate(struct fpu *fpu)
  300. {
  301. __fpregs_deactivate(fpu);
  302. __fpregs_deactivate_hw();
  303. }
  304. static inline void restore_init_xstate(void)
  305. {
  306. if (use_xsave())
  307. xrstor_state(&init_xstate_ctx, -1);
  308. else
  309. fxrstor_checking(&init_xstate_ctx.i387);
  310. }
  311. /*
  312. * Definitions for the eXtended Control Register instructions
  313. */
  314. #define XCR_XFEATURE_ENABLED_MASK 0x00000000
  315. static inline u64 xgetbv(u32 index)
  316. {
  317. u32 eax, edx;
  318. asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
  319. : "=a" (eax), "=d" (edx)
  320. : "c" (index));
  321. return eax + ((u64)edx << 32);
  322. }
  323. static inline void xsetbv(u32 index, u64 value)
  324. {
  325. u32 eax = value;
  326. u32 edx = value >> 32;
  327. asm volatile(".byte 0x0f,0x01,0xd1" /* xsetbv */
  328. : : "a" (eax), "d" (edx), "c" (index));
  329. }
  330. /*
  331. * FPU state switching for scheduling.
  332. *
  333. * This is a two-stage process:
  334. *
  335. * - switch_fpu_prepare() saves the old state and
  336. * sets the new state of the CR0.TS bit. This is
  337. * done within the context of the old process.
  338. *
  339. * - switch_fpu_finish() restores the new state as
  340. * necessary.
  341. */
  342. typedef struct { int preload; } fpu_switch_t;
  343. static inline fpu_switch_t
  344. switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
  345. {
  346. fpu_switch_t fpu;
  347. /*
  348. * If the task has used the math, pre-load the FPU on xsave processors
  349. * or if the past 5 consecutive context-switches used math.
  350. */
  351. fpu.preload = new_fpu->fpstate_active &&
  352. (use_eager_fpu() || new_fpu->counter > 5);
  353. if (old_fpu->fpregs_active) {
  354. if (!copy_fpregs_to_fpstate(old_fpu))
  355. old_fpu->last_cpu = -1;
  356. else
  357. old_fpu->last_cpu = cpu;
  358. /* But leave fpu_fpregs_owner_ctx! */
  359. old_fpu->fpregs_active = 0;
  360. /* Don't change CR0.TS if we just switch! */
  361. if (fpu.preload) {
  362. new_fpu->counter++;
  363. __fpregs_activate(new_fpu);
  364. prefetch(&new_fpu->state);
  365. } else {
  366. __fpregs_deactivate_hw();
  367. }
  368. } else {
  369. old_fpu->counter = 0;
  370. old_fpu->last_cpu = -1;
  371. if (fpu.preload) {
  372. new_fpu->counter++;
  373. if (fpu_want_lazy_restore(new_fpu, cpu))
  374. fpu.preload = 0;
  375. else
  376. prefetch(&new_fpu->state);
  377. fpregs_activate(new_fpu);
  378. }
  379. }
  380. return fpu;
  381. }
  382. /*
  383. * By the time this gets called, we've already cleared CR0.TS and
  384. * given the process the FPU if we are going to preload the FPU
  385. * state - all we need to do is to conditionally restore the register
  386. * state itself.
  387. */
  388. static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch)
  389. {
  390. if (fpu_switch.preload) {
  391. if (unlikely(copy_fpstate_to_fpregs(new_fpu)))
  392. fpu__clear(new_fpu);
  393. }
  394. }
  395. /*
  396. * Signal frame handlers...
  397. */
  398. extern int copy_fpstate_to_sigframe(void __user *buf, void __user *fx, int size);
  399. /*
  400. * Needs to be preemption-safe.
  401. *
  402. * NOTE! user_fpu_begin() must be used only immediately before restoring
  403. * the save state. It does not do any saving/restoring on its own. In
  404. * lazy FPU mode, it is just an optimization to avoid a #NM exception,
  405. * the task can lose the FPU right after preempt_enable().
  406. */
  407. static inline void user_fpu_begin(void)
  408. {
  409. struct fpu *fpu = &current->thread.fpu;
  410. preempt_disable();
  411. if (!fpregs_active())
  412. fpregs_activate(fpu);
  413. preempt_enable();
  414. }
  415. /*
  416. * i387 state interaction
  417. */
  418. static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
  419. {
  420. if (cpu_has_fxsr) {
  421. return tsk->thread.fpu.state.fxsave.cwd;
  422. } else {
  423. return (unsigned short)tsk->thread.fpu.state.fsave.cwd;
  424. }
  425. }
  426. static inline unsigned short get_fpu_swd(struct task_struct *tsk)
  427. {
  428. if (cpu_has_fxsr) {
  429. return tsk->thread.fpu.state.fxsave.swd;
  430. } else {
  431. return (unsigned short)tsk->thread.fpu.state.fsave.swd;
  432. }
  433. }
  434. static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
  435. {
  436. if (cpu_has_xmm) {
  437. return tsk->thread.fpu.state.fxsave.mxcsr;
  438. } else {
  439. return MXCSR_DEFAULT;
  440. }
  441. }
  442. #endif /* _ASM_X86_FPU_INTERNAL_H */