1 #ifndef __ASM_X86_XSAVE_H
2 #define __ASM_X86_XSAVE_H
4 #include <linux/types.h>
5 #include <asm/processor.h>
6 #include <linux/uaccess.h>
8 /* Bit 63 of XCR0 is reserved for future expansion */
9 #define XSTATE_EXTEND_MASK (~(XSTATE_FPSSE | (1ULL << 63)))
11 #define XSTATE_CPUID 0x0000000d
13 #define FXSAVE_SIZE 512
15 #define XSAVE_HDR_SIZE 64
16 #define XSAVE_HDR_OFFSET FXSAVE_SIZE
18 #define XSAVE_YMM_SIZE 256
19 #define XSAVE_YMM_OFFSET (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET)
21 /* Supported features which support lazy state saving */
22 #define XSTATE_LAZY (XSTATE_FP | XSTATE_SSE | XSTATE_YMM \
23 | XSTATE_OPMASK | XSTATE_ZMM_Hi256 | XSTATE_Hi16_ZMM)
25 /* Supported features which require eager state saving */
26 #define XSTATE_EAGER (XSTATE_BNDREGS | XSTATE_BNDCSR)
28 /* All currently supported features */
29 #define XCNTXT_MASK (XSTATE_LAZY | XSTATE_EAGER)
32 #define REX_PREFIX "0x48, "
37 extern unsigned int xstate_size;
38 extern u64 xfeatures_mask;
39 extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
41 extern void update_regset_xstate_info(unsigned int size, u64 xstate_mask);
43 /* These macros all use (%edi)/(%rdi) as the single memory argument. */
44 #define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27"
45 #define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37"
46 #define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f"
47 #define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f"
48 #define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
50 #define xstate_fault ".section .fixup,\"ax\"\n" \
51 "3: movl $-1,%[err]\n" \
54 _ASM_EXTABLE(1b, 3b) \
58 * This function is called only during boot time when x86 caps are not set
59 * up and alternative can not be used yet.
61 static inline int copy_xregs_to_kernel_booting(struct xregs_state *fx)
65 u32 hmask = mask >> 32;
68 WARN_ON(system_state != SYSTEM_BOOTING);
70 if (boot_cpu_has(X86_FEATURE_XSAVES))
71 asm volatile("1:"XSAVES"\n\t"
74 : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
77 asm volatile("1:"XSAVE"\n\t"
80 : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
86 * This function is called only during boot time when x86 caps are not set
87 * up and alternative can not be used yet.
89 static inline int copy_kernel_to_xregs_booting(struct xregs_state *fx, u64 mask)
92 u32 hmask = mask >> 32;
95 WARN_ON(system_state != SYSTEM_BOOTING);
97 if (boot_cpu_has(X86_FEATURE_XSAVES))
98 asm volatile("1:"XRSTORS"\n\t"
101 : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
104 asm volatile("1:"XRSTOR"\n\t"
107 : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
113 * Save processor xstate to xsave area.
115 static inline int copy_xregs_to_kernel(struct xregs_state *fx)
119 u32 hmask = mask >> 32;
122 WARN_ON(!alternatives_patched);
125 * If xsaves is enabled, xsaves replaces xsaveopt because
126 * it supports compact format and supervisor states in addition to
127 * modified optimization in xsaveopt.
129 * Otherwise, if xsaveopt is enabled, xsaveopt replaces xsave
130 * because xsaveopt supports modified optimization which is not
131 * supported by xsave.
133 * If none of xsaves and xsaveopt is enabled, use xsave.
138 X86_FEATURE_XSAVEOPT,
141 [fx] "D" (fx), "a" (lmask), "d" (hmask) :
143 asm volatile("2:\n\t"
152 * Restore processor xstate from xsave area.
154 static inline int copy_kernel_to_xregs(struct xregs_state *fx, u64 mask)
158 u32 hmask = mask >> 32;
161 * Use xrstors to restore context if it is enabled. xrstors supports
162 * compacted format of xsave area which is not supported by xrstor.
168 "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
180 * Save xstate to user space xsave area.
182 * We don't use modified optimization because xrstor/xrstors might track
183 * a different application.
185 * We don't use compacted format xsave area for
186 * backward compatibility for old applications which don't understand
187 * compacted format of xsave area.
189 static inline int copy_xregs_to_user(struct xregs_state __user *buf)
194 * Clear the xsave header first, so that reserved fields are
195 * initialized to zero.
197 err = __clear_user(&buf->header, sizeof(buf->header));
201 __asm__ __volatile__(ASM_STAC "\n"
205 : "D" (buf), "a" (-1), "d" (-1), "0" (0)
211 * Restore xstate from user space xsave area.
213 static inline int copy_user_to_xregs(struct xregs_state __user *buf, u64 mask)
216 struct xregs_state *xstate = ((__force struct xregs_state *)buf);
218 u32 hmask = mask >> 32;
220 __asm__ __volatile__(ASM_STAC "\n"
224 : "D" (xstate), "a" (lmask), "d" (hmask), "0" (0)
225 : "memory"); /* memory required? */
229 void *get_xsave_addr(struct xregs_state *xsave, int xstate);