aa3ff0cca9a100bf1f1ee3e7a2e04a9a1c99ab67
[linux-2.6-microblaze.git] / arch / x86 / include / asm / 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 #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)))
21
22 #define FXSAVE_SIZE     512
23
24 #define XSAVE_HDR_SIZE      64
25 #define XSAVE_HDR_OFFSET    FXSAVE_SIZE
26
27 #define XSAVE_YMM_SIZE      256
28 #define XSAVE_YMM_OFFSET    (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET)
29
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)
33
34 /* Supported features which require eager state saving */
35 #define XSTATE_EAGER    (XSTATE_BNDREGS | XSTATE_BNDCSR)
36
37 /* All currently supported features */
38 #define XCNTXT_MASK     (XSTATE_LAZY | XSTATE_EAGER)
39
40 #ifdef CONFIG_X86_64
41 #define REX_PREFIX      "0x48, "
42 #else
43 #define REX_PREFIX
44 #endif
45
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;
50
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);
54
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"
60
61 #define xstate_fault    ".section .fixup,\"ax\"\n"      \
62                         "3:  movl $-1,%[err]\n"         \
63                         "    jmp  2b\n"                 \
64                         ".previous\n"                   \
65                         _ASM_EXTABLE(1b, 3b)            \
66                         : [err] "=r" (err)
67
68 /*
69  * This function is called only during boot time when x86 caps are not set
70  * up and alternative can not be used yet.
71  */
72 static int xsave_state_booting(struct xsave_struct *fx, u64 mask)
73 {
74         u32 lmask = mask;
75         u32 hmask = mask >> 32;
76         int err = 0;
77
78         WARN_ON(system_state != SYSTEM_BOOTING);
79
80         if (boot_cpu_has(X86_FEATURE_XSAVES))
81                 asm volatile("1:"XSAVES"\n\t"
82                         "2:\n\t"
83                         : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
84                         :   "memory");
85         else
86                 asm volatile("1:"XSAVE"\n\t"
87                         "2:\n\t"
88                         : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
89                         :   "memory");
90
91         asm volatile(xstate_fault
92                      : "0" (0)
93                      : "memory");
94
95         return err;
96 }
97
98 /*
99  * This function is called only during boot time when x86 caps are not set
100  * up and alternative can not be used yet.
101  */
102 static inline int xrstor_state_booting(struct xsave_struct *fx, u64 mask)
103 {
104         u32 lmask = mask;
105         u32 hmask = mask >> 32;
106         int err = 0;
107
108         WARN_ON(system_state != SYSTEM_BOOTING);
109
110         if (boot_cpu_has(X86_FEATURE_XSAVES))
111                 asm volatile("1:"XRSTORS"\n\t"
112                         "2:\n\t"
113                         : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
114                         :   "memory");
115         else
116                 asm volatile("1:"XRSTOR"\n\t"
117                         "2:\n\t"
118                         : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
119                         :   "memory");
120
121         asm volatile(xstate_fault
122                      : "0" (0)
123                      : "memory");
124
125         return err;
126 }
127
128 /*
129  * Save processor xstate to xsave area.
130  */
131 static inline int xsave_state(struct xsave_struct *fx, u64 mask)
132 {
133         u32 lmask = mask;
134         u32 hmask = mask >> 32;
135         int err = 0;
136
137         /*
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.
141          *
142          * Otherwise, if xsaveopt is enabled, xsaveopt replaces xsave
143          * because xsaveopt supports modified optimization which is not
144          * supported by xsave.
145          *
146          * If none of xsaves and xsaveopt is enabled, use xsave.
147          */
148         alternative_input_2(
149                 "1:"XSAVE,
150                 "1:"XSAVEOPT,
151                 X86_FEATURE_XSAVEOPT,
152                 "1:"XSAVES,
153                 X86_FEATURE_XSAVES,
154                 [fx] "D" (fx), "a" (lmask), "d" (hmask) :
155                 "memory");
156         asm volatile("2:\n\t"
157                      xstate_fault
158                      : "0" (0)
159                      : "memory");
160
161         return err;
162 }
163
164 /*
165  * Restore processor xstate from xsave area.
166  */
167 static inline int xrstor_state(struct xsave_struct *fx, u64 mask)
168 {
169         int err = 0;
170         u32 lmask = mask;
171         u32 hmask = mask >> 32;
172
173         /*
174          * Use xrstors to restore context if it is enabled. xrstors supports
175          * compacted format of xsave area which is not supported by xrstor.
176          */
177         alternative_input(
178                 "1: " XRSTOR,
179                 "1: " XRSTORS,
180                 X86_FEATURE_XSAVES,
181                 "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
182                 : "memory");
183
184         asm volatile("2:\n"
185                      xstate_fault
186                      : "0" (0)
187                      : "memory");
188
189         return err;
190 }
191
192 /*
193  * Save xstate context for old process during context switch.
194  */
195 static inline void fpu_xsave(struct fpu *fpu)
196 {
197         xsave_state(&fpu->state->xsave, -1);
198 }
199
200 /*
201  * Restore xstate context for new process during context switch.
202  */
203 static inline int fpu_xrstor_checking(struct xsave_struct *fx)
204 {
205         return xrstor_state(fx, -1);
206 }
207
208 /*
209  * Save xstate to user space xsave area.
210  *
211  * We don't use modified optimization because xrstor/xrstors might track
212  * a different application.
213  *
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.
217  */
218 static inline int xsave_user(struct xsave_struct __user *buf)
219 {
220         int err;
221
222         /*
223          * Clear the xsave header first, so that reserved fields are
224          * initialized to zero.
225          */
226         err = __clear_user(&buf->xsave_hdr, sizeof(buf->xsave_hdr));
227         if (unlikely(err))
228                 return -EFAULT;
229
230         __asm__ __volatile__(ASM_STAC "\n"
231                              "1:"XSAVE"\n"
232                              "2: " ASM_CLAC "\n"
233                              xstate_fault
234                              : "D" (buf), "a" (-1), "d" (-1), "0" (0)
235                              : "memory");
236         return err;
237 }
238
239 /*
240  * Restore xstate from user space xsave area.
241  */
242 static inline int xrestore_user(struct xsave_struct __user *buf, u64 mask)
243 {
244         int err = 0;
245         struct xsave_struct *xstate = ((__force struct xsave_struct *)buf);
246         u32 lmask = mask;
247         u32 hmask = mask >> 32;
248
249         __asm__ __volatile__(ASM_STAC "\n"
250                              "1:"XRSTOR"\n"
251                              "2: " ASM_CLAC "\n"
252                              xstate_fault
253                              : "D" (xstate), "a" (lmask), "d" (hmask), "0" (0)
254                              : "memory");       /* memory required? */
255         return err;
256 }
257
258 #endif