1 #ifndef __ASM_X86_XSAVE_H
2 #define __ASM_X86_XSAVE_H
4 #include <linux/types.h>
5 #include <asm/processor.h>
7 #define XSTATE_CPUID 0x0000000d
10 #define XSTATE_SSE 0x2
11 #define XSTATE_YMM 0x4
12 #define XSTATE_BNDREGS 0x8
13 #define XSTATE_BNDCSR 0x10
14 #define XSTATE_OPMASK 0x20
15 #define XSTATE_ZMM_Hi256 0x40
16 #define XSTATE_Hi16_ZMM 0x80
18 #define XSTATE_FPSSE (XSTATE_FP | XSTATE_SSE)
19 /* Bit 63 of XCR0 is reserved for future expansion */
20 #define XSTATE_EXTEND_MASK (~(XSTATE_FPSSE | (1ULL << 63)))
22 #define FXSAVE_SIZE 512
24 #define XSAVE_HDR_SIZE 64
25 #define XSAVE_HDR_OFFSET FXSAVE_SIZE
27 #define XSAVE_YMM_SIZE 256
28 #define XSAVE_YMM_OFFSET (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET)
30 /* Supported features which support lazy state saving */
31 #define XSTATE_LAZY (XSTATE_FP | XSTATE_SSE | XSTATE_YMM \
32 | XSTATE_OPMASK | XSTATE_ZMM_Hi256 | XSTATE_Hi16_ZMM)
34 /* Supported features which require eager state saving */
35 #define XSTATE_EAGER (XSTATE_BNDREGS | XSTATE_BNDCSR)
37 /* All currently supported features */
38 #define XCNTXT_MASK (XSTATE_LAZY | XSTATE_EAGER)
41 #define REX_PREFIX "0x48, "
46 extern unsigned int xstate_size;
47 extern u64 pcntxt_mask;
48 extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
49 extern struct xsave_struct *init_xstate_buf;
51 extern void xsave_init(void);
52 extern void update_regset_xstate_info(unsigned int size, u64 xstate_mask);
53 extern int init_fpu(struct task_struct *child);
55 #define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27"
56 #define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37"
57 #define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f"
58 #define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f"
59 #define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
61 #define xstate_fault ".section .fixup,\"ax\"\n" \
62 "3: movl $-1,%[err]\n" \
65 _ASM_EXTABLE(1b, 3b) \
69 * This function is called only during boot time when x86 caps are not set
70 * up and alternative can not be used yet.
72 static int xsave_state_booting(struct xsave_struct *fx, u64 mask)
75 u32 hmask = mask >> 32;
78 WARN_ON(system_state != SYSTEM_BOOTING);
80 if (boot_cpu_has(X86_FEATURE_XSAVES))
81 asm volatile("1:"XSAVES"\n\t"
83 : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
86 asm volatile("1:"XSAVE"\n\t"
88 : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
91 asm volatile(xstate_fault
99 * This function is called only during boot time when x86 caps are not set
100 * up and alternative can not be used yet.
102 static inline int xrstor_state_booting(struct xsave_struct *fx, u64 mask)
105 u32 hmask = mask >> 32;
108 WARN_ON(system_state != SYSTEM_BOOTING);
110 if (boot_cpu_has(X86_FEATURE_XSAVES))
111 asm volatile("1:"XRSTORS"\n\t"
113 : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
116 asm volatile("1:"XRSTOR"\n\t"
118 : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
121 asm volatile(xstate_fault
129 * Save processor xstate to xsave area.
131 static inline int xsave_state(struct xsave_struct *fx, u64 mask)
134 u32 hmask = mask >> 32;
138 * If xsaves is enabled, xsaves replaces xsaveopt because
139 * it supports compact format and supervisor states in addition to
140 * modified optimization in xsaveopt.
142 * Otherwise, if xsaveopt is enabled, xsaveopt replaces xsave
143 * because xsaveopt supports modified optimization which is not
144 * supported by xsave.
146 * If none of xsaves and xsaveopt is enabled, use xsave.
151 X86_FEATURE_XSAVEOPT,
154 [fx] "D" (fx), "a" (lmask), "d" (hmask) :
156 asm volatile("2:\n\t"
165 * Restore processor xstate from xsave area.
167 static inline int xrstor_state(struct xsave_struct *fx, u64 mask)
171 u32 hmask = mask >> 32;
174 * Use xrstors to restore context if it is enabled. xrstors supports
175 * compacted format of xsave area which is not supported by xrstor.
181 "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
193 * Save xstate context for old process during context switch.
195 static inline void fpu_xsave(struct fpu *fpu)
197 xsave_state(&fpu->state->xsave, -1);
201 * Restore xstate context for new process during context switch.
203 static inline int fpu_xrstor_checking(struct xsave_struct *fx)
205 return xrstor_state(fx, -1);
209 * Save xstate to user space xsave area.
211 * We don't use modified optimization because xrstor/xrstors might track
212 * a different application.
214 * We don't use compacted format xsave area for
215 * backward compatibility for old applications which don't understand
216 * compacted format of xsave area.
218 static inline int xsave_user(struct xsave_struct __user *buf)
223 * Clear the xsave header first, so that reserved fields are
224 * initialized to zero.
226 err = __clear_user(&buf->xsave_hdr, sizeof(buf->xsave_hdr));
230 __asm__ __volatile__(ASM_STAC "\n"
234 : "D" (buf), "a" (-1), "d" (-1), "0" (0)
240 * Restore xstate from user space xsave area.
242 static inline int xrestore_user(struct xsave_struct __user *buf, u64 mask)
245 struct xsave_struct *xstate = ((__force struct xsave_struct *)buf);
247 u32 hmask = mask >> 32;
249 __asm__ __volatile__(ASM_STAC "\n"
253 : "D" (xstate), "a" (lmask), "d" (hmask), "0" (0)
254 : "memory"); /* memory required? */