x86, hwmon: Package Level Thermal/Power: pkgtemp hwmon driver
[deliverable/linux.git] / arch / x86 / kernel / xsave.c
CommitLineData
dc1e35c6
SS
1/*
2 * xsave/xrstor support.
3 *
4 * Author: Suresh Siddha <suresh.b.siddha@intel.com>
5 */
6#include <linux/bootmem.h>
7#include <linux/compat.h>
8#include <asm/i387.h>
c37b5efe
SS
9#ifdef CONFIG_IA32_EMULATION
10#include <asm/sigcontext32.h>
11#endif
6152e4b1 12#include <asm/xcr.h>
dc1e35c6
SS
13
14/*
15 * Supported feature mask by the CPU and the kernel.
16 */
6152e4b1 17u64 pcntxt_mask;
dc1e35c6 18
c37b5efe
SS
19struct _fpx_sw_bytes fx_sw_reserved;
20#ifdef CONFIG_IA32_EMULATION
21struct _fpx_sw_bytes fx_sw_reserved_ia32;
22#endif
23
24/*
25 * Check for the presence of extended state information in the
26 * user fpstate pointer in the sigcontext.
27 */
28int check_for_xstate(struct i387_fxsave_struct __user *buf,
29 void __user *fpstate,
30 struct _fpx_sw_bytes *fx_sw_user)
31{
32 int min_xstate_size = sizeof(struct i387_fxsave_struct) +
33 sizeof(struct xsave_hdr_struct);
34 unsigned int magic2;
35 int err;
36
37 err = __copy_from_user(fx_sw_user, &buf->sw_reserved[0],
38 sizeof(struct _fpx_sw_bytes));
c37b5efe 39 if (err)
d6d4d420 40 return -EFAULT;
c37b5efe
SS
41
42 /*
43 * First Magic check failed.
44 */
45 if (fx_sw_user->magic1 != FP_XSTATE_MAGIC1)
d6d4d420 46 return -EINVAL;
c37b5efe
SS
47
48 /*
49 * Check for error scenarios.
50 */
51 if (fx_sw_user->xstate_size < min_xstate_size ||
52 fx_sw_user->xstate_size > xstate_size ||
53 fx_sw_user->xstate_size > fx_sw_user->extended_size)
d6d4d420 54 return -EINVAL;
c37b5efe
SS
55
56 err = __get_user(magic2, (__u32 *) (((void *)fpstate) +
57 fx_sw_user->extended_size -
58 FP_XSTATE_MAGIC2_SIZE));
d6d4d420
DC
59 if (err)
60 return err;
c37b5efe
SS
61 /*
62 * Check for the presence of second magic word at the end of memory
63 * layout. This detects the case where the user just copied the legacy
64 * fpstate layout with out copying the extended state information
65 * in the memory layout.
66 */
d6d4d420
DC
67 if (magic2 != FP_XSTATE_MAGIC2)
68 return -EFAULT;
c37b5efe
SS
69
70 return 0;
71}
72
ab513701
SS
73#ifdef CONFIG_X86_64
74/*
75 * Signal frame handlers.
76 */
77
78int save_i387_xstate(void __user *buf)
79{
80 struct task_struct *tsk = current;
81 int err = 0;
82
83 if (!access_ok(VERIFY_WRITE, buf, sig_xstate_size))
84 return -EACCES;
85
f65bc214 86 BUG_ON(sig_xstate_size < xstate_size);
ab513701 87
c37b5efe 88 if ((unsigned long)buf % 64)
ab513701
SS
89 printk("save_i387_xstate: bad fpstate %p\n", buf);
90
91 if (!used_math())
92 return 0;
06c38d5e 93
ab513701 94 if (task_thread_info(tsk)->status & TS_USEDFPU) {
ed405958
SS
95 /*
96 * Start with clearing the user buffer. This will present a
97 * clean context for the bytes not touched by the fxsave/xsave.
98 */
9f482807
IM
99 err = __clear_user(buf, sig_xstate_size);
100 if (err)
101 return err;
ed405958 102
c9ad4882 103 if (use_xsave())
c37b5efe
SS
104 err = xsave_user(buf);
105 else
106 err = fxsave_user(buf);
107
ab513701
SS
108 if (err)
109 return err;
110 task_thread_info(tsk)->status &= ~TS_USEDFPU;
111 stts();
112 } else {
86603283 113 if (__copy_to_user(buf, &tsk->thread.fpu.state->fxsave,
ab513701
SS
114 xstate_size))
115 return -1;
116 }
c37b5efe 117
06c38d5e
SS
118 clear_used_math(); /* trigger finit */
119
c9ad4882 120 if (use_xsave()) {
c37b5efe 121 struct _fpstate __user *fx = buf;
04944b79
SS
122 struct _xstate __user *x = buf;
123 u64 xstate_bv;
c37b5efe
SS
124
125 err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved,
126 sizeof(struct _fpx_sw_bytes));
127
128 err |= __put_user(FP_XSTATE_MAGIC2,
129 (__u32 __user *) (buf + sig_xstate_size
130 - FP_XSTATE_MAGIC2_SIZE));
04944b79
SS
131
132 /*
133 * Read the xstate_bv which we copied (directly from the cpu or
134 * from the state in task struct) to the user buffers and
135 * set the FP/SSE bits.
136 */
137 err |= __get_user(xstate_bv, &x->xstate_hdr.xstate_bv);
138
139 /*
140 * For legacy compatible, we always set FP/SSE bits in the bit
141 * vector while saving the state to the user context. This will
142 * enable us capturing any changes(during sigreturn) to
143 * the FP/SSE bits by the legacy applications which don't touch
144 * xstate_bv in the xsave header.
145 *
146 * xsave aware apps can change the xstate_bv in the xsave
147 * header as well as change any contents in the memory layout.
148 * xrestore as part of sigreturn will capture all the changes.
149 */
150 xstate_bv |= XSTATE_FPSSE;
151
152 err |= __put_user(xstate_bv, &x->xstate_hdr.xstate_bv);
153
f364eada
SS
154 if (err)
155 return err;
c37b5efe
SS
156 }
157
ab513701
SS
158 return 1;
159}
160
c37b5efe
SS
161/*
162 * Restore the extended state if present. Otherwise, restore the FP/SSE
163 * state.
164 */
7820b756 165static int restore_user_xstate(void __user *buf)
c37b5efe
SS
166{
167 struct _fpx_sw_bytes fx_sw_user;
6152e4b1 168 u64 mask;
c37b5efe
SS
169 int err;
170
171 if (((unsigned long)buf % 64) ||
172 check_for_xstate(buf, buf, &fx_sw_user))
173 goto fx_only;
174
6152e4b1 175 mask = fx_sw_user.xstate_bv;
c37b5efe
SS
176
177 /*
178 * restore the state passed by the user.
179 */
6152e4b1 180 err = xrestore_user(buf, mask);
c37b5efe
SS
181 if (err)
182 return err;
183
184 /*
185 * init the state skipped by the user.
186 */
6152e4b1 187 mask = pcntxt_mask & ~mask;
c37b5efe 188
6152e4b1 189 xrstor_state(init_xstate_buf, mask);
c37b5efe
SS
190
191 return 0;
192
193fx_only:
194 /*
195 * couldn't find the extended state information in the
196 * memory layout. Restore just the FP/SSE and init all
197 * the other extended state.
198 */
6152e4b1 199 xrstor_state(init_xstate_buf, pcntxt_mask & ~XSTATE_FPSSE);
c37b5efe
SS
200 return fxrstor_checking((__force struct i387_fxsave_struct *)buf);
201}
202
ab513701
SS
203/*
204 * This restores directly out of user space. Exceptions are handled.
205 */
206int restore_i387_xstate(void __user *buf)
207{
208 struct task_struct *tsk = current;
c37b5efe 209 int err = 0;
ab513701
SS
210
211 if (!buf) {
c37b5efe
SS
212 if (used_math())
213 goto clear;
ab513701
SS
214 return 0;
215 } else
216 if (!access_ok(VERIFY_READ, buf, sig_xstate_size))
217 return -EACCES;
218
219 if (!used_math()) {
220 err = init_fpu(tsk);
221 if (err)
222 return err;
223 }
224
225 if (!(task_thread_info(current)->status & TS_USEDFPU)) {
226 clts();
227 task_thread_info(current)->status |= TS_USEDFPU;
228 }
c9ad4882 229 if (use_xsave())
c37b5efe
SS
230 err = restore_user_xstate(buf);
231 else
232 err = fxrstor_checking((__force struct i387_fxsave_struct *)
233 buf);
ab513701
SS
234 if (unlikely(err)) {
235 /*
236 * Encountered an error while doing the restore from the
237 * user buffer, clear the fpu state.
238 */
c37b5efe 239clear:
ab513701
SS
240 clear_fpu(tsk);
241 clear_used_math();
242 }
243 return err;
244}
245#endif
246
c37b5efe
SS
247/*
248 * Prepare the SW reserved portion of the fxsave memory layout, indicating
249 * the presence of the extended state information in the memory layout
250 * pointed by the fpstate pointer in the sigcontext.
251 * This will be saved when ever the FP and extended state context is
252 * saved on the user stack during the signal handler delivery to the user.
253 */
8bcad30f 254static void prepare_fx_sw_frame(void)
c37b5efe
SS
255{
256 int size_extended = (xstate_size - sizeof(struct i387_fxsave_struct)) +
257 FP_XSTATE_MAGIC2_SIZE;
258
259 sig_xstate_size = sizeof(struct _fpstate) + size_extended;
260
261#ifdef CONFIG_IA32_EMULATION
262 sig_xstate_ia32_size = sizeof(struct _fpstate_ia32) + size_extended;
263#endif
264
265 memset(&fx_sw_reserved, 0, sizeof(fx_sw_reserved));
266
267 fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
268 fx_sw_reserved.extended_size = sig_xstate_size;
6152e4b1 269 fx_sw_reserved.xstate_bv = pcntxt_mask;
c37b5efe
SS
270 fx_sw_reserved.xstate_size = xstate_size;
271#ifdef CONFIG_IA32_EMULATION
272 memcpy(&fx_sw_reserved_ia32, &fx_sw_reserved,
273 sizeof(struct _fpx_sw_bytes));
274 fx_sw_reserved_ia32.extended_size = sig_xstate_ia32_size;
275#endif
276}
277
dc1e35c6
SS
278/*
279 * Represents init state for the supported extended state.
280 */
281struct xsave_struct *init_xstate_buf;
282
3c1c7f10
SS
283#ifdef CONFIG_X86_64
284unsigned int sig_xstate_size = sizeof(struct _fpstate);
285#endif
286
dc1e35c6
SS
287/*
288 * Enable the extended processor state save/restore feature
289 */
290void __cpuinit xsave_init(void)
291{
292 if (!cpu_has_xsave)
293 return;
294
295 set_in_cr4(X86_CR4_OSXSAVE);
296
297 /*
298 * Enable all the features that the HW is capable of
299 * and the Linux kernel is aware of.
dc1e35c6 300 */
6152e4b1 301 xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
dc1e35c6
SS
302}
303
304/*
305 * setup the xstate image representing the init state
306 */
a19aac85 307static void __init setup_xstate_init(void)
dc1e35c6
SS
308{
309 init_xstate_buf = alloc_bootmem(xstate_size);
310 init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
311}
312
313/*
314 * Enable and initialize the xsave feature.
315 */
bfe085f6 316void __ref xsave_cntxt_init(void)
dc1e35c6
SS
317{
318 unsigned int eax, ebx, ecx, edx;
319
320 cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
6152e4b1 321 pcntxt_mask = eax + ((u64)edx << 32);
dc1e35c6 322
6152e4b1
PA
323 if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
324 printk(KERN_ERR "FP/SSE not shown under xsave features 0x%llx\n",
325 pcntxt_mask);
dc1e35c6
SS
326 BUG();
327 }
328
329 /*
a30469e7 330 * Support only the state known to OS.
dc1e35c6 331 */
6152e4b1 332 pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
dc1e35c6
SS
333 xsave_init();
334
335 /*
336 * Recompute the context size for enabled features
337 */
338 cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
dc1e35c6
SS
339 xstate_size = ebx;
340
5b3efd50 341 update_regset_xstate_info(xstate_size, pcntxt_mask);
c37b5efe
SS
342 prepare_fx_sw_frame();
343
dc1e35c6
SS
344 setup_xstate_init();
345
6152e4b1 346 printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%llx, "
dc1e35c6 347 "cntxt size 0x%x\n",
6152e4b1 348 pcntxt_mask, xstate_size);
dc1e35c6 349}
This page took 0.179619 seconds and 5 git commands to generate.