xref: /openbmc/linux/arch/x86/include/asm/fpu/xstate.h (revision 50f408d96d4d1a945d2c50c5fd8ed400883edf0e)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_X86_XSAVE_H
3 #define __ASM_X86_XSAVE_H
4 
5 #include <linux/uaccess.h>
6 #include <linux/types.h>
7 
8 #include <asm/processor.h>
9 #include <asm/user.h>
10 
11 /* Bit 63 of XCR0 is reserved for future expansion */
12 #define XFEATURE_MASK_EXTEND	(~(XFEATURE_MASK_FPSSE | (1ULL << 63)))
13 
14 #define XSTATE_CPUID		0x0000000d
15 
16 #define FXSAVE_SIZE	512
17 
18 #define XSAVE_HDR_SIZE	    64
19 #define XSAVE_HDR_OFFSET    FXSAVE_SIZE
20 
21 #define XSAVE_YMM_SIZE	    256
22 #define XSAVE_YMM_OFFSET    (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET)
23 
24 /* All currently supported user features */
25 #define XFEATURE_MASK_USER_SUPPORTED (XFEATURE_MASK_FP | \
26 				      XFEATURE_MASK_SSE | \
27 				      XFEATURE_MASK_YMM | \
28 				      XFEATURE_MASK_OPMASK | \
29 				      XFEATURE_MASK_ZMM_Hi256 | \
30 				      XFEATURE_MASK_Hi16_ZMM	 | \
31 				      XFEATURE_MASK_PKRU | \
32 				      XFEATURE_MASK_BNDREGS | \
33 				      XFEATURE_MASK_BNDCSR)
34 
35 /* All currently supported supervisor features */
36 #define XFEATURE_MASK_SUPERVISOR_SUPPORTED (0)
37 
38 /*
39  * A supervisor state component may not always contain valuable information,
40  * and its size may be huge. Saving/restoring such supervisor state components
41  * at each context switch can cause high CPU and space overhead, which should
42  * be avoided. Such supervisor state components should only be saved/restored
43  * on demand. The on-demand dynamic supervisor features are set in this mask.
44  *
45  * Unlike the existing supported supervisor features, a dynamic supervisor
46  * feature does not allocate a buffer in task->fpu, and the corresponding
47  * supervisor state component cannot be saved/restored at each context switch.
48  *
49  * To support a dynamic supervisor feature, a developer should follow the
50  * dos and don'ts as below:
51  * - Do dynamically allocate a buffer for the supervisor state component.
52  * - Do manually invoke the XSAVES/XRSTORS instruction to save/restore the
53  *   state component to/from the buffer.
54  * - Don't set the bit corresponding to the dynamic supervisor feature in
55  *   IA32_XSS at run time, since it has been set at boot time.
56  */
57 #define XFEATURE_MASK_DYNAMIC (XFEATURE_MASK_LBR)
58 
59 /*
60  * Unsupported supervisor features. When a supervisor feature in this mask is
61  * supported in the future, move it to the supported supervisor feature mask.
62  */
63 #define XFEATURE_MASK_SUPERVISOR_UNSUPPORTED (XFEATURE_MASK_PT)
64 
65 /* All supervisor states including supported and unsupported states. */
66 #define XFEATURE_MASK_SUPERVISOR_ALL (XFEATURE_MASK_SUPERVISOR_SUPPORTED | \
67 				      XFEATURE_MASK_DYNAMIC | \
68 				      XFEATURE_MASK_SUPERVISOR_UNSUPPORTED)
69 
70 #ifdef CONFIG_X86_64
71 #define REX_PREFIX	"0x48, "
72 #else
73 #define REX_PREFIX
74 #endif
75 
76 extern u64 xfeatures_mask_all;
77 
78 static inline u64 xfeatures_mask_supervisor(void)
79 {
80 	return xfeatures_mask_all & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
81 }
82 
83 static inline u64 xfeatures_mask_user(void)
84 {
85 	return xfeatures_mask_all & XFEATURE_MASK_USER_SUPPORTED;
86 }
87 
88 static inline u64 xfeatures_mask_dynamic(void)
89 {
90 	if (!boot_cpu_has(X86_FEATURE_ARCH_LBR))
91 		return XFEATURE_MASK_DYNAMIC & ~XFEATURE_MASK_LBR;
92 
93 	return XFEATURE_MASK_DYNAMIC;
94 }
95 
96 extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
97 
98 extern void __init update_regset_xstate_info(unsigned int size,
99 					     u64 xstate_mask);
100 
101 void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr);
102 const void *get_xsave_field_ptr(int xfeature_nr);
103 int using_compacted_format(void);
104 int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int offset, unsigned int size);
105 int copy_xstate_to_user(void __user *ubuf, struct xregs_state *xsave, unsigned int offset, unsigned int size);
106 int copy_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf);
107 int copy_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf);
108 void copy_supervisor_to_kernel(struct xregs_state *xsave);
109 void copy_dynamic_supervisor_to_kernel(struct xregs_state *xstate, u64 mask);
110 void copy_kernel_to_dynamic_supervisor(struct xregs_state *xstate, u64 mask);
111 
112 
113 /* Validate an xstate header supplied by userspace (ptrace or sigreturn) */
114 int validate_user_xstate_header(const struct xstate_header *hdr);
115 
116 #endif
117