blob: c9a6d68b8d623b84d169f61c4680c194ea8d137a [file] [log] [blame]
Suresh Siddhadc1e35c2008-07-29 10:29:19 -07001#ifndef __ASM_X86_XSAVE_H
2#define __ASM_X86_XSAVE_H
3
H. Peter Anvin6152e4b2008-07-29 17:23:16 -07004#include <linux/types.h>
Suresh Siddhadc1e35c2008-07-29 10:29:19 -07005#include <asm/processor.h>
Suresh Siddhadc1e35c2008-07-29 10:29:19 -07006
Robert Richteree813d52010-07-21 19:03:54 +02007#define XSTATE_CPUID 0x0000000d
Suresh Siddhadc1e35c2008-07-29 10:29:19 -07008
Fenghua Yuc2bc11f2014-02-20 13:24:51 -08009#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
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070017
18#define XSTATE_FPSSE (XSTATE_FP | XSTATE_SSE)
Chao Peng612263b2014-10-22 17:35:24 +080019#define XSTATE_AVX512 (XSTATE_OPMASK | XSTATE_ZMM_Hi256 | XSTATE_Hi16_ZMM)
Liu, Jinsong56c103e2014-02-21 17:39:02 +000020/* Bit 63 of XCR0 is reserved for future expansion */
21#define XSTATE_EXTEND_MASK (~(XSTATE_FPSSE | (1ULL << 63)))
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070022
23#define FXSAVE_SIZE 512
24
Sheng Yang2d5b5a62010-06-13 17:29:39 +080025#define XSAVE_HDR_SIZE 64
26#define XSAVE_HDR_OFFSET FXSAVE_SIZE
27
28#define XSAVE_YMM_SIZE 256
29#define XSAVE_YMM_OFFSET (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET)
Sheng Yang5ee481d2010-05-17 17:22:23 +080030
Qiaowei Rene7d820a2013-12-05 17:15:34 +080031/* Supported features which support lazy state saving */
Fenghua Yuc2bc11f2014-02-20 13:24:51 -080032#define XSTATE_LAZY (XSTATE_FP | XSTATE_SSE | XSTATE_YMM \
33 | XSTATE_OPMASK | XSTATE_ZMM_Hi256 | XSTATE_Hi16_ZMM)
Qiaowei Rene7d820a2013-12-05 17:15:34 +080034
35/* Supported features which require eager state saving */
36#define XSTATE_EAGER (XSTATE_BNDREGS | XSTATE_BNDCSR)
37
38/* All currently supported features */
39#define XCNTXT_MASK (XSTATE_LAZY | XSTATE_EAGER)
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070040
Suresh Siddhab359e8a2008-07-29 10:29:20 -070041#ifdef CONFIG_X86_64
42#define REX_PREFIX "0x48, "
43#else
44#define REX_PREFIX
45#endif
46
H. Peter Anvin6152e4b2008-07-29 17:23:16 -070047extern unsigned int xstate_size;
48extern u64 pcntxt_mask;
Suresh Siddha5b3efd52010-02-11 11:50:59 -080049extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
Suresh Siddha304bced2012-08-24 14:13:02 -070050extern struct xsave_struct *init_xstate_buf;
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070051
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070052extern void xsave_init(void);
Suresh Siddha5b3efd52010-02-11 11:50:59 -080053extern void update_regset_xstate_info(unsigned int size, u64 xstate_mask);
Suresh Siddhab359e8a2008-07-29 10:29:20 -070054extern int init_fpu(struct task_struct *child);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070055
H. Peter Anvinc9e5a5a2014-05-30 08:19:21 -070056/* These macros all use (%edi)/(%rdi) as the single memory argument. */
Fenghua Yu200b08a2014-05-29 11:12:34 -070057#define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27"
58#define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37"
59#define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f"
60#define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f"
61#define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
62
Fenghua Yub84e7052014-05-29 11:12:35 -070063#define xstate_fault ".section .fixup,\"ax\"\n" \
64 "3: movl $-1,%[err]\n" \
65 " jmp 2b\n" \
66 ".previous\n" \
67 _ASM_EXTABLE(1b, 3b) \
68 : [err] "=r" (err)
69
Fenghua Yuf31a9f72014-05-29 11:12:36 -070070/*
Fenghua Yuadb9d522014-05-29 11:12:40 -070071 * This function is called only during boot time when x86 caps are not set
72 * up and alternative can not be used yet.
73 */
Borislav Petkovd0f2dd12014-06-17 12:21:08 +020074static inline int xsave_state_booting(struct xsave_struct *fx, u64 mask)
Fenghua Yuadb9d522014-05-29 11:12:40 -070075{
76 u32 lmask = mask;
77 u32 hmask = mask >> 32;
78 int err = 0;
79
80 WARN_ON(system_state != SYSTEM_BOOTING);
81
82 if (boot_cpu_has(X86_FEATURE_XSAVES))
83 asm volatile("1:"XSAVES"\n\t"
84 "2:\n\t"
Quentin Casasnovas06c81732015-03-05 13:19:22 +010085 xstate_fault
86 : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
Fenghua Yuadb9d522014-05-29 11:12:40 -070087 : "memory");
88 else
89 asm volatile("1:"XSAVE"\n\t"
90 "2:\n\t"
Quentin Casasnovas06c81732015-03-05 13:19:22 +010091 xstate_fault
92 : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
Fenghua Yuadb9d522014-05-29 11:12:40 -070093 : "memory");
Fenghua Yuadb9d522014-05-29 11:12:40 -070094 return err;
95}
96
97/*
98 * This function is called only during boot time when x86 caps are not set
99 * up and alternative can not be used yet.
100 */
101static inline int xrstor_state_booting(struct xsave_struct *fx, u64 mask)
102{
103 u32 lmask = mask;
104 u32 hmask = mask >> 32;
105 int err = 0;
106
107 WARN_ON(system_state != SYSTEM_BOOTING);
108
109 if (boot_cpu_has(X86_FEATURE_XSAVES))
110 asm volatile("1:"XRSTORS"\n\t"
111 "2:\n\t"
Quentin Casasnovas06c81732015-03-05 13:19:22 +0100112 xstate_fault
113 : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
Fenghua Yuadb9d522014-05-29 11:12:40 -0700114 : "memory");
115 else
116 asm volatile("1:"XRSTOR"\n\t"
117 "2:\n\t"
Quentin Casasnovas06c81732015-03-05 13:19:22 +0100118 xstate_fault
119 : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
Fenghua Yuadb9d522014-05-29 11:12:40 -0700120 : "memory");
Fenghua Yuadb9d522014-05-29 11:12:40 -0700121 return err;
122}
123
124/*
Fenghua Yuf31a9f72014-05-29 11:12:36 -0700125 * Save processor xstate to xsave area.
126 */
127static inline int xsave_state(struct xsave_struct *fx, u64 mask)
128{
129 u32 lmask = mask;
130 u32 hmask = mask >> 32;
131 int err = 0;
132
133 /*
134 * If xsaves is enabled, xsaves replaces xsaveopt because
135 * it supports compact format and supervisor states in addition to
136 * modified optimization in xsaveopt.
137 *
138 * Otherwise, if xsaveopt is enabled, xsaveopt replaces xsave
139 * because xsaveopt supports modified optimization which is not
140 * supported by xsave.
141 *
142 * If none of xsaves and xsaveopt is enabled, use xsave.
143 */
144 alternative_input_2(
145 "1:"XSAVE,
Quentin Casasnovas06c81732015-03-05 13:19:22 +0100146 XSAVEOPT,
Fenghua Yuf31a9f72014-05-29 11:12:36 -0700147 X86_FEATURE_XSAVEOPT,
Quentin Casasnovas06c81732015-03-05 13:19:22 +0100148 XSAVES,
Fenghua Yuf31a9f72014-05-29 11:12:36 -0700149 X86_FEATURE_XSAVES,
150 [fx] "D" (fx), "a" (lmask), "d" (hmask) :
151 "memory");
152 asm volatile("2:\n\t"
153 xstate_fault
154 : "0" (0)
155 : "memory");
156
157 return err;
158}
159
160/*
161 * Restore processor xstate from xsave area.
162 */
163static inline int xrstor_state(struct xsave_struct *fx, u64 mask)
164{
165 int err = 0;
166 u32 lmask = mask;
167 u32 hmask = mask >> 32;
168
169 /*
170 * Use xrstors to restore context if it is enabled. xrstors supports
171 * compacted format of xsave area which is not supported by xrstor.
172 */
173 alternative_input(
174 "1: " XRSTOR,
Quentin Casasnovas06c81732015-03-05 13:19:22 +0100175 XRSTORS,
Fenghua Yuf31a9f72014-05-29 11:12:36 -0700176 X86_FEATURE_XSAVES,
177 "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
178 : "memory");
179
180 asm volatile("2:\n"
181 xstate_fault
182 : "0" (0)
183 : "memory");
184
185 return err;
186}
187
Fenghua Yuf9de3142014-05-29 11:12:37 -0700188/*
189 * Save xstate context for old process during context switch.
190 */
191static inline void fpu_xsave(struct fpu *fpu)
192{
193 xsave_state(&fpu->state->xsave, -1);
194}
195
196/*
197 * Restore xstate context for new process during context switch.
198 */
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700199static inline int fpu_xrstor_checking(struct xsave_struct *fx)
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700200{
Fenghua Yuf9de3142014-05-29 11:12:37 -0700201 return xrstor_state(fx, -1);
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700202}
203
Fenghua Yufacbf4d2014-05-29 11:12:38 -0700204/*
205 * Save xstate to user space xsave area.
206 *
207 * We don't use modified optimization because xrstor/xrstors might track
208 * a different application.
209 *
210 * We don't use compacted format xsave area for
211 * backward compatibility for old applications which don't understand
212 * compacted format of xsave area.
213 */
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700214static inline int xsave_user(struct xsave_struct __user *buf)
Suresh Siddha9dc89c02008-07-29 10:29:23 -0700215{
216 int err;
Suresh Siddha8e221b62010-06-22 16:23:37 -0700217
218 /*
219 * Clear the xsave header first, so that reserved fields are
220 * initialized to zero.
221 */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700222 err = __clear_user(&buf->xsave_hdr, sizeof(buf->xsave_hdr));
Suresh Siddha8e221b62010-06-22 16:23:37 -0700223 if (unlikely(err))
224 return -EFAULT;
225
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700226 __asm__ __volatile__(ASM_STAC "\n"
Fenghua Yufacbf4d2014-05-29 11:12:38 -0700227 "1:"XSAVE"\n"
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700228 "2: " ASM_CLAC "\n"
Fenghua Yufacbf4d2014-05-29 11:12:38 -0700229 xstate_fault
Suresh Siddha9dc89c02008-07-29 10:29:23 -0700230 : "D" (buf), "a" (-1), "d" (-1), "0" (0)
231 : "memory");
Suresh Siddha9dc89c02008-07-29 10:29:23 -0700232 return err;
233}
234
Fenghua Yufacbf4d2014-05-29 11:12:38 -0700235/*
236 * Restore xstate from user space xsave area.
237 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700238static inline int xrestore_user(struct xsave_struct __user *buf, u64 mask)
Suresh Siddha9dc89c02008-07-29 10:29:23 -0700239{
Fenghua Yufacbf4d2014-05-29 11:12:38 -0700240 int err = 0;
Suresh Siddha9dc89c02008-07-29 10:29:23 -0700241 struct xsave_struct *xstate = ((__force struct xsave_struct *)buf);
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700242 u32 lmask = mask;
243 u32 hmask = mask >> 32;
Suresh Siddha9dc89c02008-07-29 10:29:23 -0700244
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700245 __asm__ __volatile__(ASM_STAC "\n"
Fenghua Yufacbf4d2014-05-29 11:12:38 -0700246 "1:"XRSTOR"\n"
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700247 "2: " ASM_CLAC "\n"
Fenghua Yufacbf4d2014-05-29 11:12:38 -0700248 xstate_fault
Suresh Siddha9dc89c02008-07-29 10:29:23 -0700249 : "D" (xstate), "a" (lmask), "d" (hmask), "0" (0)
250 : "memory"); /* memory required? */
251 return err;
252}
253
Fenghua Yu7496d642014-05-29 11:12:44 -0700254void *get_xsave_addr(struct xsave_struct *xsave, int xstate);
255void setup_xstate_comp(void);
256
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700257#endif