xstate.h 9.7 KB

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