]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/x86/include/asm/fpu/xsave.h
x86/fpu: Optimize fpu_copy() some more on lazy switching systems
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / include / asm / fpu / xsave.h
1 #ifndef __ASM_X86_XSAVE_H
2 #define __ASM_X86_XSAVE_H
3
4 #include <linux/types.h>
5 #include <asm/processor.h>
6
7 #define XSTATE_CPUID 0x0000000d
8
9 #define XSTATE_FP 0x1
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
17
18 /* The highest xstate bit above (of XSTATE_Hi16_ZMM): */
19 #define XFEATURES_NR_MAX 8
20
21 #define XSTATE_FPSSE (XSTATE_FP | XSTATE_SSE)
22 #define XSTATE_AVX512 (XSTATE_OPMASK | XSTATE_ZMM_Hi256 | XSTATE_Hi16_ZMM)
23 /* Bit 63 of XCR0 is reserved for future expansion */
24 #define XSTATE_EXTEND_MASK (~(XSTATE_FPSSE | (1ULL << 63)))
25
26 #define FXSAVE_SIZE 512
27
28 #define XSAVE_HDR_SIZE 64
29 #define XSAVE_HDR_OFFSET FXSAVE_SIZE
30
31 #define XSAVE_YMM_SIZE 256
32 #define XSAVE_YMM_OFFSET (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET)
33
34 /* Supported features which support lazy state saving */
35 #define XSTATE_LAZY (XSTATE_FP | XSTATE_SSE | XSTATE_YMM \
36 | XSTATE_OPMASK | XSTATE_ZMM_Hi256 | XSTATE_Hi16_ZMM)
37
38 /* Supported features which require eager state saving */
39 #define XSTATE_EAGER (XSTATE_BNDREGS | XSTATE_BNDCSR)
40
41 /* All currently supported features */
42 #define XCNTXT_MASK (XSTATE_LAZY | XSTATE_EAGER)
43
44 #ifdef CONFIG_X86_64
45 #define REX_PREFIX "0x48, "
46 #else
47 #define REX_PREFIX
48 #endif
49
50 extern unsigned int xstate_size;
51 extern u64 xfeatures_mask;
52 extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
53 extern struct xsave_struct init_xstate_ctx;
54
55 extern void update_regset_xstate_info(unsigned int size, u64 xstate_mask);
56
57 /* These macros all use (%edi)/(%rdi) as the single memory argument. */
58 #define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27"
59 #define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37"
60 #define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f"
61 #define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f"
62 #define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
63
64 #define xstate_fault ".section .fixup,\"ax\"\n" \
65 "3: movl $-1,%[err]\n" \
66 " jmp 2b\n" \
67 ".previous\n" \
68 _ASM_EXTABLE(1b, 3b) \
69 : [err] "=r" (err)
70
71 /*
72 * This function is called only during boot time when x86 caps are not set
73 * up and alternative can not be used yet.
74 */
75 static inline int xsave_state_booting(struct xsave_struct *fx)
76 {
77 u64 mask = -1;
78 u32 lmask = mask;
79 u32 hmask = mask >> 32;
80 int err = 0;
81
82 WARN_ON(system_state != SYSTEM_BOOTING);
83
84 if (boot_cpu_has(X86_FEATURE_XSAVES))
85 asm volatile("1:"XSAVES"\n\t"
86 "2:\n\t"
87 xstate_fault
88 : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
89 : "memory");
90 else
91 asm volatile("1:"XSAVE"\n\t"
92 "2:\n\t"
93 xstate_fault
94 : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
95 : "memory");
96 return err;
97 }
98
99 /*
100 * This function is called only during boot time when x86 caps are not set
101 * up and alternative can not be used yet.
102 */
103 static inline int xrstor_state_booting(struct xsave_struct *fx, u64 mask)
104 {
105 u32 lmask = mask;
106 u32 hmask = mask >> 32;
107 int err = 0;
108
109 WARN_ON(system_state != SYSTEM_BOOTING);
110
111 if (boot_cpu_has(X86_FEATURE_XSAVES))
112 asm volatile("1:"XRSTORS"\n\t"
113 "2:\n\t"
114 xstate_fault
115 : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
116 : "memory");
117 else
118 asm volatile("1:"XRSTOR"\n\t"
119 "2:\n\t"
120 xstate_fault
121 : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
122 : "memory");
123 return err;
124 }
125
126 /*
127 * Save processor xstate to xsave area.
128 */
129 static inline int xsave_state(struct xsave_struct *fx)
130 {
131 u64 mask = -1;
132 u32 lmask = mask;
133 u32 hmask = mask >> 32;
134 int err = 0;
135
136 WARN_ON(system_state == SYSTEM_BOOTING);
137
138 /*
139 * If xsaves is enabled, xsaves replaces xsaveopt because
140 * it supports compact format and supervisor states in addition to
141 * modified optimization in xsaveopt.
142 *
143 * Otherwise, if xsaveopt is enabled, xsaveopt replaces xsave
144 * because xsaveopt supports modified optimization which is not
145 * supported by xsave.
146 *
147 * If none of xsaves and xsaveopt is enabled, use xsave.
148 */
149 alternative_input_2(
150 "1:"XSAVE,
151 XSAVEOPT,
152 X86_FEATURE_XSAVEOPT,
153 XSAVES,
154 X86_FEATURE_XSAVES,
155 [fx] "D" (fx), "a" (lmask), "d" (hmask) :
156 "memory");
157 asm volatile("2:\n\t"
158 xstate_fault
159 : "0" (0)
160 : "memory");
161
162 return err;
163 }
164
165 /*
166 * Restore processor xstate from xsave area.
167 */
168 static inline int xrstor_state(struct xsave_struct *fx, u64 mask)
169 {
170 int err = 0;
171 u32 lmask = mask;
172 u32 hmask = mask >> 32;
173
174 /*
175 * Use xrstors to restore context if it is enabled. xrstors supports
176 * compacted format of xsave area which is not supported by xrstor.
177 */
178 alternative_input(
179 "1: " XRSTOR,
180 XRSTORS,
181 X86_FEATURE_XSAVES,
182 "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
183 : "memory");
184
185 asm volatile("2:\n"
186 xstate_fault
187 : "0" (0)
188 : "memory");
189
190 return err;
191 }
192
193 /*
194 * Restore xstate context for new process during context switch.
195 */
196 static inline int fpu_xrstor_checking(struct xsave_struct *fx)
197 {
198 return xrstor_state(fx, -1);
199 }
200
201 /*
202 * Save xstate to user space xsave area.
203 *
204 * We don't use modified optimization because xrstor/xrstors might track
205 * a different application.
206 *
207 * We don't use compacted format xsave area for
208 * backward compatibility for old applications which don't understand
209 * compacted format of xsave area.
210 */
211 static inline int xsave_user(struct xsave_struct __user *buf)
212 {
213 int err;
214
215 /*
216 * Clear the xsave header first, so that reserved fields are
217 * initialized to zero.
218 */
219 err = __clear_user(&buf->header, sizeof(buf->header));
220 if (unlikely(err))
221 return -EFAULT;
222
223 __asm__ __volatile__(ASM_STAC "\n"
224 "1:"XSAVE"\n"
225 "2: " ASM_CLAC "\n"
226 xstate_fault
227 : "D" (buf), "a" (-1), "d" (-1), "0" (0)
228 : "memory");
229 return err;
230 }
231
232 /*
233 * Restore xstate from user space xsave area.
234 */
235 static inline int xrestore_user(struct xsave_struct __user *buf, u64 mask)
236 {
237 int err = 0;
238 struct xsave_struct *xstate = ((__force struct xsave_struct *)buf);
239 u32 lmask = mask;
240 u32 hmask = mask >> 32;
241
242 __asm__ __volatile__(ASM_STAC "\n"
243 "1:"XRSTOR"\n"
244 "2: " ASM_CLAC "\n"
245 xstate_fault
246 : "D" (xstate), "a" (lmask), "d" (hmask), "0" (0)
247 : "memory"); /* memory required? */
248 return err;
249 }
250
251 void *get_xsave_addr(struct xsave_struct *xsave, int xstate);
252 void setup_xstate_comp(void);
253
254 #endif