xstate.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 __user *get_xsave_addr_user(struct xregs_state __user *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 fpu_kernel_cfg.independent_features & ~XFEATURE_MASK_LBR;
  55. return fpu_kernel_cfg.independent_features;
  56. }
  57. static inline int set_xfeature_in_sigframe(struct xregs_state __user *xbuf, u64 mask)
  58. {
  59. u64 xfeatures;
  60. int err;
  61. /* Read the xfeatures value already saved in the user buffer */
  62. err = __get_user(xfeatures, &xbuf->header.xfeatures);
  63. xfeatures |= mask;
  64. err |= __put_user(xfeatures, &xbuf->header.xfeatures);
  65. return err;
  66. }
  67. /*
  68. * Update the value of PKRU register that was already pushed onto the signal frame.
  69. */
  70. static inline int update_pkru_in_sigframe(struct xregs_state __user *buf, u32 pkru)
  71. {
  72. int err;
  73. if (unlikely(!cpu_feature_enabled(X86_FEATURE_OSPKE)))
  74. return 0;
  75. /* Mark PKRU as in-use so that it is restored correctly. */
  76. err = set_xfeature_in_sigframe(buf, XFEATURE_MASK_PKRU);
  77. if (err)
  78. return err;
  79. /* Update PKRU value in the userspace xsave buffer. */
  80. return __put_user(pkru, (unsigned int __user *)get_xsave_addr_user(buf, XFEATURE_PKRU));
  81. }
  82. /* XSAVE/XRSTOR wrapper functions */
  83. #ifdef CONFIG_X86_64
  84. #define REX_PREFIX "0x48, "
  85. #else
  86. #define REX_PREFIX
  87. #endif
  88. /* These macros all use (%edi)/(%rdi) as the single memory argument. */
  89. #define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27"
  90. #define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37"
  91. #define XSAVEC ".byte " REX_PREFIX "0x0f,0xc7,0x27"
  92. #define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f"
  93. #define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f"
  94. #define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
  95. /*
  96. * After this @err contains 0 on success or the trap number when the
  97. * operation raises an exception.
  98. */
  99. #define XSTATE_OP(op, st, lmask, hmask, err) \
  100. asm volatile("1:" op "\n\t" \
  101. "xor %[err], %[err]\n" \
  102. "2:\n\t" \
  103. _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_FAULT_MCE_SAFE) \
  104. : [err] "=a" (err) \
  105. : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
  106. : "memory")
  107. /*
  108. * If XSAVES is enabled, it replaces XSAVEC because it supports supervisor
  109. * states in addition to XSAVEC.
  110. *
  111. * Otherwise if XSAVEC is enabled, it replaces XSAVEOPT because it supports
  112. * compacted storage format in addition to XSAVEOPT.
  113. *
  114. * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
  115. * supports modified optimization which is not supported by XSAVE.
  116. *
  117. * Use XSAVE as a fallback.
  118. */
  119. #define XSTATE_XSAVE(st, lmask, hmask, err) \
  120. asm volatile("1: " ALTERNATIVE_3(XSAVE, \
  121. XSAVEOPT, X86_FEATURE_XSAVEOPT, \
  122. XSAVEC, X86_FEATURE_XSAVEC, \
  123. XSAVES, X86_FEATURE_XSAVES) \
  124. "\n" \
  125. "xor %[err], %[err]\n" \
  126. "3:\n" \
  127. _ASM_EXTABLE_TYPE_REG(1b, 3b, EX_TYPE_EFAULT_REG, %[err]) \
  128. : [err] "=r" (err) \
  129. : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
  130. : "memory")
  131. /*
  132. * Use XRSTORS to restore context if it is enabled. XRSTORS supports compact
  133. * XSAVE area format.
  134. */
  135. #define XSTATE_XRESTORE(st, lmask, hmask) \
  136. asm volatile("1: " ALTERNATIVE(XRSTOR, \
  137. XRSTORS, X86_FEATURE_XSAVES) \
  138. "\n" \
  139. "3:\n" \
  140. _ASM_EXTABLE_TYPE(1b, 3b, EX_TYPE_FPU_RESTORE) \
  141. : \
  142. : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
  143. : "memory")
  144. #if defined(CONFIG_X86_64) && defined(CONFIG_X86_DEBUG_FPU)
  145. extern void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor);
  146. #else
  147. static inline void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor) { }
  148. #endif
  149. #ifdef CONFIG_X86_64
  150. static inline void xfd_set_state(u64 xfd)
  151. {
  152. wrmsrl(MSR_IA32_XFD, xfd);
  153. __this_cpu_write(xfd_state, xfd);
  154. }
  155. static inline void xfd_update_state(struct fpstate *fpstate)
  156. {
  157. if (fpu_state_size_dynamic()) {
  158. u64 xfd = fpstate->xfd;
  159. if (__this_cpu_read(xfd_state) != xfd)
  160. xfd_set_state(xfd);
  161. }
  162. }
  163. extern int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu);
  164. #else
  165. static inline void xfd_set_state(u64 xfd) { }
  166. static inline void xfd_update_state(struct fpstate *fpstate) { }
  167. static inline int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu) {
  168. return -EPERM;
  169. }
  170. #endif
  171. /*
  172. * Save processor xstate to xsave area.
  173. *
  174. * Uses either XSAVE or XSAVEOPT or XSAVES depending on the CPU features
  175. * and command line options. The choice is permanent until the next reboot.
  176. */
  177. static inline void os_xsave(struct fpstate *fpstate)
  178. {
  179. u64 mask = fpstate->xfeatures;
  180. u32 lmask = mask;
  181. u32 hmask = mask >> 32;
  182. int err;
  183. WARN_ON_FPU(!alternatives_patched);
  184. xfd_validate_state(fpstate, mask, false);
  185. XSTATE_XSAVE(&fpstate->regs.xsave, lmask, hmask, err);
  186. /* We should never fault when copying to a kernel buffer: */
  187. WARN_ON_FPU(err);
  188. }
  189. /*
  190. * Restore processor xstate from xsave area.
  191. *
  192. * Uses XRSTORS when XSAVES is used, XRSTOR otherwise.
  193. */
  194. static inline void os_xrstor(struct fpstate *fpstate, u64 mask)
  195. {
  196. u32 lmask = mask;
  197. u32 hmask = mask >> 32;
  198. xfd_validate_state(fpstate, mask, true);
  199. XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
  200. }
  201. /* Restore of supervisor state. Does not require XFD */
  202. static inline void os_xrstor_supervisor(struct fpstate *fpstate)
  203. {
  204. u64 mask = xfeatures_mask_supervisor();
  205. u32 lmask = mask;
  206. u32 hmask = mask >> 32;
  207. XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
  208. }
  209. /*
  210. * XSAVE itself always writes all requested xfeatures. Removing features
  211. * from the request bitmap reduces the features which are written.
  212. * Generate a mask of features which must be written to a sigframe. The
  213. * unset features can be optimized away and not written.
  214. *
  215. * This optimization is user-visible. Only use for states where
  216. * uninitialized sigframe contents are tolerable, like dynamic features.
  217. *
  218. * Users of buffers produced with this optimization must check XSTATE_BV
  219. * to determine which features have been optimized out.
  220. */
  221. static inline u64 xfeatures_need_sigframe_write(void)
  222. {
  223. u64 xfeaures_to_write;
  224. /* In-use features must be written: */
  225. xfeaures_to_write = xfeatures_in_use();
  226. /* Also write all non-optimizable sigframe features: */
  227. xfeaures_to_write |= XFEATURE_MASK_USER_SUPPORTED &
  228. ~XFEATURE_MASK_SIGFRAME_INITOPT;
  229. return xfeaures_to_write;
  230. }
  231. /*
  232. * Save xstate to user space xsave area.
  233. *
  234. * We don't use modified optimization because xrstor/xrstors might track
  235. * a different application.
  236. *
  237. * We don't use compacted format xsave area for backward compatibility for
  238. * old applications which don't understand the compacted format of the
  239. * xsave area.
  240. *
  241. * The caller has to zero buf::header before calling this because XSAVE*
  242. * does not touch the reserved fields in the header.
  243. */
  244. static inline int xsave_to_user_sigframe(struct xregs_state __user *buf, u32 pkru)
  245. {
  246. /*
  247. * Include the features which are not xsaved/rstored by the kernel
  248. * internally, e.g. PKRU. That's user space ABI and also required
  249. * to allow the signal handler to modify PKRU.
  250. */
  251. struct fpstate *fpstate = current->thread.fpu.fpstate;
  252. u64 mask = fpstate->user_xfeatures;
  253. u32 lmask;
  254. u32 hmask;
  255. int err;
  256. /* Optimize away writing unnecessary xfeatures: */
  257. if (fpu_state_size_dynamic())
  258. mask &= xfeatures_need_sigframe_write();
  259. lmask = mask;
  260. hmask = mask >> 32;
  261. xfd_validate_state(fpstate, mask, false);
  262. stac();
  263. XSTATE_OP(XSAVE, buf, lmask, hmask, err);
  264. clac();
  265. if (!err)
  266. err = update_pkru_in_sigframe(buf, pkru);
  267. return err;
  268. }
  269. /*
  270. * Restore xstate from user space xsave area.
  271. */
  272. static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64 mask)
  273. {
  274. struct xregs_state *xstate = ((__force struct xregs_state *)buf);
  275. u32 lmask = mask;
  276. u32 hmask = mask >> 32;
  277. int err;
  278. xfd_validate_state(current->thread.fpu.fpstate, mask, true);
  279. stac();
  280. XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
  281. clac();
  282. return err;
  283. }
  284. /*
  285. * Restore xstate from kernel space xsave area, return an error code instead of
  286. * an exception.
  287. */
  288. static inline int os_xrstor_safe(struct fpstate *fpstate, u64 mask)
  289. {
  290. struct xregs_state *xstate = &fpstate->regs.xsave;
  291. u32 lmask = mask;
  292. u32 hmask = mask >> 32;
  293. int err;
  294. /* Ensure that XFD is up to date */
  295. xfd_update_state(fpstate);
  296. if (cpu_feature_enabled(X86_FEATURE_XSAVES))
  297. XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
  298. else
  299. XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
  300. return err;
  301. }
  302. #endif