xstate.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __X86_KERNEL_FPU_XSTATE_H
  3. #define __X86_KERNEL_FPU_XSTATE_H
  4. #include <asm/cpufeature.h>
  5. #include <asm/fpu/xstate.h>
  6. #include <asm/fpu/xcr.h>
  7. #ifdef CONFIG_X86_64
  8. DECLARE_PER_CPU(u64, xfd_state);
  9. #endif
  10. static inline void xstate_init_xcomp_bv(struct xregs_state *xsave, u64 mask)
  11. {
  12. /*
  13. * XRSTORS requires these bits set in xcomp_bv, or it will
  14. * trigger #GP:
  15. */
  16. if (cpu_feature_enabled(X86_FEATURE_XCOMPACTED))
  17. xsave->header.xcomp_bv = mask | XCOMP_BV_COMPACTED_FORMAT;
  18. }
  19. static inline u64 xstate_get_group_perm(bool guest)
  20. {
  21. struct fpu *fpu = &current->group_leader->thread.fpu;
  22. struct fpu_state_perm *perm;
  23. /* Pairs with WRITE_ONCE() in xstate_request_perm() */
  24. perm = guest ? &fpu->guest_perm : &fpu->perm;
  25. return READ_ONCE(perm->__state_perm);
  26. }
  27. static inline u64 xstate_get_host_group_perm(void)
  28. {
  29. return xstate_get_group_perm(false);
  30. }
  31. enum xstate_copy_mode {
  32. XSTATE_COPY_FP,
  33. XSTATE_COPY_FX,
  34. XSTATE_COPY_XSAVE,
  35. };
  36. struct membuf;
  37. extern void __copy_xstate_to_uabi_buf(struct membuf to, struct fpstate *fpstate,
  38. u64 xfeatures, u32 pkru_val,
  39. enum xstate_copy_mode copy_mode);
  40. extern void copy_xstate_to_uabi_buf(struct membuf to, struct task_struct *tsk,
  41. enum xstate_copy_mode mode);
  42. extern int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf, u32 *pkru);
  43. extern int copy_sigframe_from_user_to_xstate(struct task_struct *tsk, const void __user *ubuf);
  44. extern void fpu__init_cpu_xstate(void);
  45. extern void fpu__init_system_xstate(unsigned int legacy_size);
  46. extern void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr);
  47. static inline u64 xfeatures_mask_supervisor(void)
  48. {
  49. return fpu_kernel_cfg.max_features & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
  50. }
  51. static inline u64 xfeatures_mask_independent(void)
  52. {
  53. if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR))
  54. return XFEATURE_MASK_INDEPENDENT & ~XFEATURE_MASK_LBR;
  55. return XFEATURE_MASK_INDEPENDENT;
  56. }
  57. /* XSAVE/XRSTOR wrapper functions */
  58. #ifdef CONFIG_X86_64
  59. #define REX_PREFIX "0x48, "
  60. #else
  61. #define REX_PREFIX
  62. #endif
  63. /* These macros all use (%edi)/(%rdi) as the single memory argument. */
  64. #define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27"
  65. #define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37"
  66. #define XSAVEC ".byte " REX_PREFIX "0x0f,0xc7,0x27"
  67. #define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f"
  68. #define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f"
  69. #define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
  70. /*
  71. * After this @err contains 0 on success or the trap number when the
  72. * operation raises an exception.
  73. */
  74. #define XSTATE_OP(op, st, lmask, hmask, err) \
  75. asm volatile("1:" op "\n\t" \
  76. "xor %[err], %[err]\n" \
  77. "2:\n\t" \
  78. _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_FAULT_MCE_SAFE) \
  79. : [err] "=a" (err) \
  80. : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
  81. : "memory")
  82. /*
  83. * If XSAVES is enabled, it replaces XSAVEC because it supports supervisor
  84. * states in addition to XSAVEC.
  85. *
  86. * Otherwise if XSAVEC is enabled, it replaces XSAVEOPT because it supports
  87. * compacted storage format in addition to XSAVEOPT.
  88. *
  89. * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
  90. * supports modified optimization which is not supported by XSAVE.
  91. *
  92. * We use XSAVE as a fallback.
  93. *
  94. * The 661 label is defined in the ALTERNATIVE* macros as the address of the
  95. * original instruction which gets replaced. We need to use it here as the
  96. * address of the instruction where we might get an exception at.
  97. */
  98. #define XSTATE_XSAVE(st, lmask, hmask, err) \
  99. asm volatile(ALTERNATIVE_3(XSAVE, \
  100. XSAVEOPT, X86_FEATURE_XSAVEOPT, \
  101. XSAVEC, X86_FEATURE_XSAVEC, \
  102. XSAVES, X86_FEATURE_XSAVES) \
  103. "\n" \
  104. "xor %[err], %[err]\n" \
  105. "3:\n" \
  106. _ASM_EXTABLE_TYPE_REG(661b, 3b, EX_TYPE_EFAULT_REG, %[err]) \
  107. : [err] "=r" (err) \
  108. : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
  109. : "memory")
  110. /*
  111. * Use XRSTORS to restore context if it is enabled. XRSTORS supports compact
  112. * XSAVE area format.
  113. */
  114. #define XSTATE_XRESTORE(st, lmask, hmask) \
  115. asm volatile(ALTERNATIVE(XRSTOR, \
  116. XRSTORS, X86_FEATURE_XSAVES) \
  117. "\n" \
  118. "3:\n" \
  119. _ASM_EXTABLE_TYPE(661b, 3b, EX_TYPE_FPU_RESTORE) \
  120. : \
  121. : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
  122. : "memory")
  123. #if defined(CONFIG_X86_64) && defined(CONFIG_X86_DEBUG_FPU)
  124. extern void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor);
  125. #else
  126. static inline void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor) { }
  127. #endif
  128. #ifdef CONFIG_X86_64
  129. static inline void xfd_update_state(struct fpstate *fpstate)
  130. {
  131. if (fpu_state_size_dynamic()) {
  132. u64 xfd = fpstate->xfd;
  133. if (__this_cpu_read(xfd_state) != xfd) {
  134. wrmsrl(MSR_IA32_XFD, xfd);
  135. __this_cpu_write(xfd_state, xfd);
  136. }
  137. }
  138. }
  139. extern int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu);
  140. #else
  141. static inline void xfd_update_state(struct fpstate *fpstate) { }
  142. static inline int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu) {
  143. return -EPERM;
  144. }
  145. #endif
  146. /*
  147. * Save processor xstate to xsave area.
  148. *
  149. * Uses either XSAVE or XSAVEOPT or XSAVES depending on the CPU features
  150. * and command line options. The choice is permanent until the next reboot.
  151. */
  152. static inline void os_xsave(struct fpstate *fpstate)
  153. {
  154. u64 mask = fpstate->xfeatures;
  155. u32 lmask = mask;
  156. u32 hmask = mask >> 32;
  157. int err;
  158. WARN_ON_FPU(!alternatives_patched);
  159. xfd_validate_state(fpstate, mask, false);
  160. XSTATE_XSAVE(&fpstate->regs.xsave, lmask, hmask, err);
  161. /* We should never fault when copying to a kernel buffer: */
  162. WARN_ON_FPU(err);
  163. }
  164. /*
  165. * Restore processor xstate from xsave area.
  166. *
  167. * Uses XRSTORS when XSAVES is used, XRSTOR otherwise.
  168. */
  169. static inline void os_xrstor(struct fpstate *fpstate, u64 mask)
  170. {
  171. u32 lmask = mask;
  172. u32 hmask = mask >> 32;
  173. xfd_validate_state(fpstate, mask, true);
  174. XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
  175. }
  176. /* Restore of supervisor state. Does not require XFD */
  177. static inline void os_xrstor_supervisor(struct fpstate *fpstate)
  178. {
  179. u64 mask = xfeatures_mask_supervisor();
  180. u32 lmask = mask;
  181. u32 hmask = mask >> 32;
  182. XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
  183. }
  184. /*
  185. * XSAVE itself always writes all requested xfeatures. Removing features
  186. * from the request bitmap reduces the features which are written.
  187. * Generate a mask of features which must be written to a sigframe. The
  188. * unset features can be optimized away and not written.
  189. *
  190. * This optimization is user-visible. Only use for states where
  191. * uninitialized sigframe contents are tolerable, like dynamic features.
  192. *
  193. * Users of buffers produced with this optimization must check XSTATE_BV
  194. * to determine which features have been optimized out.
  195. */
  196. static inline u64 xfeatures_need_sigframe_write(void)
  197. {
  198. u64 xfeaures_to_write;
  199. /* In-use features must be written: */
  200. xfeaures_to_write = xfeatures_in_use();
  201. /* Also write all non-optimizable sigframe features: */
  202. xfeaures_to_write |= XFEATURE_MASK_USER_SUPPORTED &
  203. ~XFEATURE_MASK_SIGFRAME_INITOPT;
  204. return xfeaures_to_write;
  205. }
  206. /*
  207. * Save xstate to user space xsave area.
  208. *
  209. * We don't use modified optimization because xrstor/xrstors might track
  210. * a different application.
  211. *
  212. * We don't use compacted format xsave area for backward compatibility for
  213. * old applications which don't understand the compacted format of the
  214. * xsave area.
  215. *
  216. * The caller has to zero buf::header before calling this because XSAVE*
  217. * does not touch the reserved fields in the header.
  218. */
  219. static inline int xsave_to_user_sigframe(struct xregs_state __user *buf)
  220. {
  221. /*
  222. * Include the features which are not xsaved/rstored by the kernel
  223. * internally, e.g. PKRU. That's user space ABI and also required
  224. * to allow the signal handler to modify PKRU.
  225. */
  226. struct fpstate *fpstate = current->thread.fpu.fpstate;
  227. u64 mask = fpstate->user_xfeatures;
  228. u32 lmask;
  229. u32 hmask;
  230. int err;
  231. /* Optimize away writing unnecessary xfeatures: */
  232. if (fpu_state_size_dynamic())
  233. mask &= xfeatures_need_sigframe_write();
  234. lmask = mask;
  235. hmask = mask >> 32;
  236. xfd_validate_state(fpstate, mask, false);
  237. stac();
  238. XSTATE_OP(XSAVE, buf, lmask, hmask, err);
  239. clac();
  240. return err;
  241. }
  242. /*
  243. * Restore xstate from user space xsave area.
  244. */
  245. static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64 mask)
  246. {
  247. struct xregs_state *xstate = ((__force struct xregs_state *)buf);
  248. u32 lmask = mask;
  249. u32 hmask = mask >> 32;
  250. int err;
  251. xfd_validate_state(current->thread.fpu.fpstate, mask, true);
  252. stac();
  253. XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
  254. clac();
  255. return err;
  256. }
  257. /*
  258. * Restore xstate from kernel space xsave area, return an error code instead of
  259. * an exception.
  260. */
  261. static inline int os_xrstor_safe(struct fpstate *fpstate, u64 mask)
  262. {
  263. struct xregs_state *xstate = &fpstate->regs.xsave;
  264. u32 lmask = mask;
  265. u32 hmask = mask >> 32;
  266. int err;
  267. /* Ensure that XFD is up to date */
  268. xfd_update_state(fpstate);
  269. if (cpu_feature_enabled(X86_FEATURE_XSAVES))
  270. XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
  271. else
  272. XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
  273. return err;
  274. }
  275. #endif