blob: 14ca1dc7a7039f37cb5cb3f5cfa16186af12224f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1994 Linus Torvalds
3 *
4 * Pentium III FXSR, SSE support
5 * General FPU state handling cleanups
6 * Gareth Hughes <gareth@valinux.com>, May 2000
7 */
Alexey Dobriyan129f6942005-06-23 00:08:33 -07008#include <linux/module.h>
Roland McGrath44210112008-01-30 13:31:50 +01009#include <linux/regset.h>
Ingo Molnarf6689642008-03-05 15:37:32 +010010#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Ingo Molnarf6689642008-03-05 15:37:32 +010012
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/sigcontext.h>
Ingo Molnarf6689642008-03-05 15:37:32 +010014#include <asm/processor.h>
15#include <asm/math_emu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/uaccess.h>
Ingo Molnarf6689642008-03-05 15:37:32 +010017#include <asm/ptrace.h>
18#include <asm/i387.h>
19#include <asm/user.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Roland McGrath44210112008-01-30 13:31:50 +010021#ifdef CONFIG_X86_64
Ingo Molnarf6689642008-03-05 15:37:32 +010022# include <asm/sigcontext32.h>
23# include <asm/user32.h>
Roland McGrath44210112008-01-30 13:31:50 +010024#else
Suresh Siddhaab513702008-07-29 10:29:22 -070025# define save_i387_xstate_ia32 save_i387_xstate
26# define restore_i387_xstate_ia32 restore_i387_xstate
Ingo Molnarf6689642008-03-05 15:37:32 +010027# define _fpstate_ia32 _fpstate
Suresh Siddhaab513702008-07-29 10:29:22 -070028# define _xstate_ia32 _xstate
Suresh Siddha3c1c7f12008-07-29 10:29:21 -070029# define sig_xstate_ia32_size sig_xstate_size
Suresh Siddhac37b5ef2008-07-29 10:29:25 -070030# define fx_sw_reserved_ia32 fx_sw_reserved
Ingo Molnarf6689642008-03-05 15:37:32 +010031# define user_i387_ia32_struct user_i387_struct
32# define user32_fxsr_struct user_fxsr_struct
Roland McGrath44210112008-01-30 13:31:50 +010033#endif
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#ifdef CONFIG_MATH_EMULATION
Ingo Molnarf6689642008-03-05 15:37:32 +010036# define HAVE_HWFP (boot_cpu_data.hard_math)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#else
Ingo Molnarf6689642008-03-05 15:37:32 +010038# define HAVE_HWFP 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#endif
40
Ingo Molnarf6689642008-03-05 15:37:32 +010041static unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
Suresh Siddha61c46282008-03-10 15:28:04 -070042unsigned int xstate_size;
Suresh Siddha3c1c7f12008-07-29 10:29:21 -070043unsigned int sig_xstate_ia32_size = sizeof(struct _fpstate_ia32);
Suresh Siddha61c46282008-03-10 15:28:04 -070044static struct i387_fxsave_struct fx_scratch __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Suresh Siddha61c46282008-03-10 15:28:04 -070046void __cpuinit mxcsr_feature_mask_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 unsigned long mask = 0;
Ingo Molnarf6689642008-03-05 15:37:32 +010049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 clts();
51 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -070052 memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct));
53 asm volatile("fxsave %0" : : "m" (fx_scratch));
54 mask = fx_scratch.mxcsr_mask;
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +010055 if (mask == 0)
56 mask = 0x0000ffbf;
57 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 mxcsr_feature_mask &= mask;
59 stts();
60}
61
Rakib Mullick9bc646f2008-11-20 19:08:45 +060062void __cpuinit init_thread_xstate(void)
Suresh Siddha61c46282008-03-10 15:28:04 -070063{
Suresh Siddhae8a496a2008-05-23 16:26:37 -070064 if (!HAVE_HWFP) {
65 xstate_size = sizeof(struct i387_soft_struct);
66 return;
67 }
68
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070069 if (cpu_has_xsave) {
70 xsave_cntxt_init();
71 return;
72 }
73
Suresh Siddha61c46282008-03-10 15:28:04 -070074 if (cpu_has_fxsr)
75 xstate_size = sizeof(struct i387_fxsave_struct);
76#ifdef CONFIG_X86_32
77 else
78 xstate_size = sizeof(struct i387_fsave_struct);
79#endif
Suresh Siddha61c46282008-03-10 15:28:04 -070080}
81
Roland McGrath44210112008-01-30 13:31:50 +010082#ifdef CONFIG_X86_64
83/*
84 * Called at bootup to set up the initial FPU state that is later cloned
85 * into all processes.
86 */
87void __cpuinit fpu_init(void)
88{
89 unsigned long oldcr0 = read_cr0();
Ingo Molnarf6689642008-03-05 15:37:32 +010090
Roland McGrath44210112008-01-30 13:31:50 +010091 set_in_cr4(X86_CR4_OSFXSR);
92 set_in_cr4(X86_CR4_OSXMMEXCPT);
93
Ingo Molnarf6689642008-03-05 15:37:32 +010094 write_cr0(oldcr0 & ~(X86_CR0_TS|X86_CR0_EM)); /* clear TS and EM */
Roland McGrath44210112008-01-30 13:31:50 +010095
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070096 /*
97 * Boot processor to setup the FP and extended state context info.
98 */
99 if (!smp_processor_id())
100 init_thread_xstate();
101 xsave_init();
102
Roland McGrath44210112008-01-30 13:31:50 +0100103 mxcsr_feature_mask_init();
104 /* clean state in init */
Avi Kivityc9ad4882010-05-06 11:45:45 +0300105 current_thread_info()->status = 0;
Roland McGrath44210112008-01-30 13:31:50 +0100106 clear_used_math();
107}
108#endif /* CONFIG_X86_64 */
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/*
111 * The _current_ task is using the FPU for the first time
112 * so initialize it and set the mxcsr to its default
113 * value at reset if we support XMM instructions and then
114 * remeber the current task has used the FPU.
115 */
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700116int init_fpu(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Roland McGrath44210112008-01-30 13:31:50 +0100118 if (tsk_used_math(tsk)) {
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700119 if (HAVE_HWFP && tsk == current)
Roland McGrath44210112008-01-30 13:31:50 +0100120 unlazy_fpu(tsk);
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700121 return 0;
122 }
123
124 /*
125 * Memory allocation at the first usage of the FPU and other state.
126 */
127 if (!tsk->thread.xstate) {
128 tsk->thread.xstate = kmem_cache_alloc(task_xstate_cachep,
129 GFP_KERNEL);
130 if (!tsk->thread.xstate)
131 return -ENOMEM;
Roland McGrath44210112008-01-30 13:31:50 +0100132 }
133
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700134#ifdef CONFIG_X86_32
135 if (!HAVE_HWFP) {
136 memset(tsk->thread.xstate, 0, xstate_size);
Daniel Glöcknerab9e1852009-03-04 19:42:27 +0100137 finit_task(tsk);
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700138 set_stopped_child_used_math(tsk);
139 return 0;
140 }
141#endif
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700144 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
145
146 memset(fx, 0, xstate_size);
147 fx->cwd = 0x37f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (cpu_has_xmm)
Suresh Siddha61c46282008-03-10 15:28:04 -0700149 fx->mxcsr = MXCSR_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 } else {
Suresh Siddha61c46282008-03-10 15:28:04 -0700151 struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
152 memset(fp, 0, xstate_size);
153 fp->cwd = 0xffff037fu;
154 fp->swd = 0xffff0000u;
155 fp->twd = 0xffffffffu;
156 fp->fos = 0xffff0000u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
Roland McGrath44210112008-01-30 13:31:50 +0100158 /*
159 * Only the device not available exception or ptrace can call init_fpu.
160 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 set_stopped_child_used_math(tsk);
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800165/*
166 * The xstateregs_active() routine is the same as the fpregs_active() routine,
167 * as the "regset->n" for the xstate regset will be updated based on the feature
168 * capabilites supported by the xsave.
169 */
Roland McGrath44210112008-01-30 13:31:50 +0100170int fpregs_active(struct task_struct *target, const struct user_regset *regset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Roland McGrath44210112008-01-30 13:31:50 +0100172 return tsk_used_math(target) ? regset->n : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
Roland McGrath44210112008-01-30 13:31:50 +0100174
175int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
176{
177 return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
178}
179
180int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
181 unsigned int pos, unsigned int count,
182 void *kbuf, void __user *ubuf)
183{
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700184 int ret;
185
Roland McGrath44210112008-01-30 13:31:50 +0100186 if (!cpu_has_fxsr)
187 return -ENODEV;
188
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700189 ret = init_fpu(target);
190 if (ret)
191 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100192
193 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700194 &target->thread.xstate->fxsave, 0, -1);
Roland McGrath44210112008-01-30 13:31:50 +0100195}
196
197int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
198 unsigned int pos, unsigned int count,
199 const void *kbuf, const void __user *ubuf)
200{
201 int ret;
202
203 if (!cpu_has_fxsr)
204 return -ENODEV;
205
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700206 ret = init_fpu(target);
207 if (ret)
208 return ret;
209
Roland McGrath44210112008-01-30 13:31:50 +0100210 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700211 &target->thread.xstate->fxsave, 0, -1);
Roland McGrath44210112008-01-30 13:31:50 +0100212
213 /*
214 * mxcsr reserved bits must be masked to zero for security reasons.
215 */
Suresh Siddha61c46282008-03-10 15:28:04 -0700216 target->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
Roland McGrath44210112008-01-30 13:31:50 +0100217
Suresh Siddha42deec62008-07-29 10:29:26 -0700218 /*
219 * update the header bits in the xsave header, indicating the
220 * presence of FP and SSE state.
221 */
222 if (cpu_has_xsave)
223 target->thread.xstate->xsave.xsave_hdr.xstate_bv |= XSTATE_FPSSE;
224
Roland McGrath44210112008-01-30 13:31:50 +0100225 return ret;
226}
227
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800228int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
229 unsigned int pos, unsigned int count,
230 void *kbuf, void __user *ubuf)
231{
232 int ret;
233
234 if (!cpu_has_xsave)
235 return -ENODEV;
236
237 ret = init_fpu(target);
238 if (ret)
239 return ret;
240
241 /*
Suresh Siddhaff7fbc72010-02-22 14:51:33 -0800242 * Copy the 48bytes defined by the software first into the xstate
243 * memory layout in the thread struct, so that we can copy the entire
244 * xstateregs to the user using one user_regset_copyout().
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800245 */
Suresh Siddhaff7fbc72010-02-22 14:51:33 -0800246 memcpy(&target->thread.xstate->fxsave.sw_reserved,
247 xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800248
249 /*
Suresh Siddhaff7fbc72010-02-22 14:51:33 -0800250 * Copy the xstate memory layout.
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800251 */
252 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Suresh Siddhaff7fbc72010-02-22 14:51:33 -0800253 &target->thread.xstate->xsave, 0, -1);
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800254 return ret;
255}
256
257int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
258 unsigned int pos, unsigned int count,
259 const void *kbuf, const void __user *ubuf)
260{
261 int ret;
262 struct xsave_hdr_struct *xsave_hdr;
263
264 if (!cpu_has_xsave)
265 return -ENODEV;
266
267 ret = init_fpu(target);
268 if (ret)
269 return ret;
270
271 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
272 &target->thread.xstate->xsave, 0, -1);
273
274 /*
275 * mxcsr reserved bits must be masked to zero for security reasons.
276 */
277 target->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
278
279 xsave_hdr = &target->thread.xstate->xsave.xsave_hdr;
280
281 xsave_hdr->xstate_bv &= pcntxt_mask;
282 /*
283 * These bits must be zero.
284 */
285 xsave_hdr->reserved1[0] = xsave_hdr->reserved1[1] = 0;
286
287 return ret;
288}
289
Roland McGrath44210112008-01-30 13:31:50 +0100290#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/*
293 * FPU tag word conversions.
294 */
295
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100296static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100301 tmp = ~twd;
Roland McGrath44210112008-01-30 13:31:50 +0100302 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100303 /* and move the valid bits to the lower byte. */
304 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
305 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
306 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
Ingo Molnarf6689642008-03-05 15:37:32 +0100307
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100308 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
Roland McGrath44210112008-01-30 13:31:50 +0100311#define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
312#define FP_EXP_TAG_VALID 0
313#define FP_EXP_TAG_ZERO 1
314#define FP_EXP_TAG_SPECIAL 2
315#define FP_EXP_TAG_EMPTY 3
316
317static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Roland McGrath44210112008-01-30 13:31:50 +0100319 struct _fpxreg *st;
320 u32 tos = (fxsave->swd >> 11) & 7;
321 u32 twd = (unsigned long) fxsave->twd;
322 u32 tag;
323 u32 ret = 0xffff0000u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 int i;
325
Roland McGrath44210112008-01-30 13:31:50 +0100326 for (i = 0; i < 8; i++, twd >>= 1) {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100327 if (twd & 0x1) {
328 st = FPREG_ADDR(fxsave, (i - tos) & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100330 switch (st->exponent & 0x7fff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 case 0x7fff:
Roland McGrath44210112008-01-30 13:31:50 +0100332 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 break;
334 case 0x0000:
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100335 if (!st->significand[0] &&
336 !st->significand[1] &&
337 !st->significand[2] &&
Roland McGrath44210112008-01-30 13:31:50 +0100338 !st->significand[3])
339 tag = FP_EXP_TAG_ZERO;
340 else
341 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 break;
343 default:
Roland McGrath44210112008-01-30 13:31:50 +0100344 if (st->significand[3] & 0x8000)
345 tag = FP_EXP_TAG_VALID;
346 else
347 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 break;
349 }
350 } else {
Roland McGrath44210112008-01-30 13:31:50 +0100351 tag = FP_EXP_TAG_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Roland McGrath44210112008-01-30 13:31:50 +0100353 ret |= tag << (2 * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355 return ret;
356}
357
358/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * FXSR floating point environment conversions.
360 */
361
Ingo Molnarf6689642008-03-05 15:37:32 +0100362static void
363convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Suresh Siddha61c46282008-03-10 15:28:04 -0700365 struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100366 struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
367 struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 int i;
369
Roland McGrath44210112008-01-30 13:31:50 +0100370 env->cwd = fxsave->cwd | 0xffff0000u;
371 env->swd = fxsave->swd | 0xffff0000u;
372 env->twd = twd_fxsr_to_i387(fxsave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Roland McGrath44210112008-01-30 13:31:50 +0100374#ifdef CONFIG_X86_64
375 env->fip = fxsave->rip;
376 env->foo = fxsave->rdp;
377 if (tsk == current) {
378 /*
379 * should be actually ds/cs at fpu exception time, but
380 * that information is not available in 64bit mode.
381 */
Ingo Molnarf6689642008-03-05 15:37:32 +0100382 asm("mov %%ds, %[fos]" : [fos] "=r" (env->fos));
383 asm("mov %%cs, %[fcs]" : [fcs] "=r" (env->fcs));
Roland McGrath44210112008-01-30 13:31:50 +0100384 } else {
385 struct pt_regs *regs = task_pt_regs(tsk);
Ingo Molnarf6689642008-03-05 15:37:32 +0100386
Roland McGrath44210112008-01-30 13:31:50 +0100387 env->fos = 0xffff0000 | tsk->thread.ds;
388 env->fcs = regs->cs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
Roland McGrath44210112008-01-30 13:31:50 +0100390#else
391 env->fip = fxsave->fip;
Jan Beulich609b5292008-03-05 08:35:14 +0000392 env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
Roland McGrath44210112008-01-30 13:31:50 +0100393 env->foo = fxsave->foo;
394 env->fos = fxsave->fos;
395#endif
396
397 for (i = 0; i < 8; ++i)
398 memcpy(&to[i], &from[i], sizeof(to[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Roland McGrath44210112008-01-30 13:31:50 +0100401static void convert_to_fxsr(struct task_struct *tsk,
402 const struct user_i387_ia32_struct *env)
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Suresh Siddha61c46282008-03-10 15:28:04 -0700405 struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100406 struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
407 struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int i;
409
Roland McGrath44210112008-01-30 13:31:50 +0100410 fxsave->cwd = env->cwd;
411 fxsave->swd = env->swd;
412 fxsave->twd = twd_i387_to_fxsr(env->twd);
413 fxsave->fop = (u16) ((u32) env->fcs >> 16);
414#ifdef CONFIG_X86_64
415 fxsave->rip = env->fip;
416 fxsave->rdp = env->foo;
417 /* cs and ds ignored */
418#else
419 fxsave->fip = env->fip;
420 fxsave->fcs = (env->fcs & 0xffff);
421 fxsave->foo = env->foo;
422 fxsave->fos = env->fos;
423#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Roland McGrath44210112008-01-30 13:31:50 +0100425 for (i = 0; i < 8; ++i)
426 memcpy(&to[i], &from[i], sizeof(from[0]));
427}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Roland McGrath44210112008-01-30 13:31:50 +0100429int fpregs_get(struct task_struct *target, const struct user_regset *regset,
430 unsigned int pos, unsigned int count,
431 void *kbuf, void __user *ubuf)
432{
433 struct user_i387_ia32_struct env;
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700434 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700436 ret = init_fpu(target);
437 if (ret)
438 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100439
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700440 if (!HAVE_HWFP)
441 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
442
Ingo Molnarf6689642008-03-05 15:37:32 +0100443 if (!cpu_has_fxsr) {
Roland McGrath44210112008-01-30 13:31:50 +0100444 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700445 &target->thread.xstate->fsave, 0,
446 -1);
Ingo Molnarf6689642008-03-05 15:37:32 +0100447 }
Roland McGrath44210112008-01-30 13:31:50 +0100448
449 if (kbuf && pos == 0 && count == sizeof(env)) {
450 convert_from_fxsr(kbuf, target);
451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
Roland McGrath44210112008-01-30 13:31:50 +0100453
454 convert_from_fxsr(&env, target);
Ingo Molnarf6689642008-03-05 15:37:32 +0100455
Roland McGrath44210112008-01-30 13:31:50 +0100456 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
457}
458
459int fpregs_set(struct task_struct *target, const struct user_regset *regset,
460 unsigned int pos, unsigned int count,
461 const void *kbuf, const void __user *ubuf)
462{
463 struct user_i387_ia32_struct env;
464 int ret;
465
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700466 ret = init_fpu(target);
467 if (ret)
468 return ret;
469
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700470 if (!HAVE_HWFP)
471 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
472
Ingo Molnarf6689642008-03-05 15:37:32 +0100473 if (!cpu_has_fxsr) {
Roland McGrath44210112008-01-30 13:31:50 +0100474 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700475 &target->thread.xstate->fsave, 0, -1);
Ingo Molnarf6689642008-03-05 15:37:32 +0100476 }
Roland McGrath44210112008-01-30 13:31:50 +0100477
478 if (pos > 0 || count < sizeof(env))
479 convert_from_fxsr(&env, target);
480
481 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
482 if (!ret)
483 convert_to_fxsr(target, &env);
484
Suresh Siddha42deec62008-07-29 10:29:26 -0700485 /*
486 * update the header bit in the xsave header, indicating the
487 * presence of FP.
488 */
489 if (cpu_has_xsave)
490 target->thread.xstate->xsave.xsave_hdr.xstate_bv |= XSTATE_FP;
Roland McGrath44210112008-01-30 13:31:50 +0100491 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
494/*
495 * Signal frame handlers.
496 */
497
Roland McGrath44210112008-01-30 13:31:50 +0100498static inline int save_i387_fsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 struct task_struct *tsk = current;
Suresh Siddha61c46282008-03-10 15:28:04 -0700501 struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Suresh Siddha61c46282008-03-10 15:28:04 -0700503 fp->status = fp->swd;
504 if (__copy_to_user(buf, fp, sizeof(struct i387_fsave_struct)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return -1;
506 return 1;
507}
508
Roland McGrath44210112008-01-30 13:31:50 +0100509static int save_i387_fxsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 struct task_struct *tsk = current;
Suresh Siddha61c46282008-03-10 15:28:04 -0700512 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100513 struct user_i387_ia32_struct env;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 int err = 0;
515
Roland McGrath44210112008-01-30 13:31:50 +0100516 convert_from_fxsr(&env, tsk);
517 if (__copy_to_user(buf, &env, sizeof(env)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return -1;
519
Suresh Siddha61c46282008-03-10 15:28:04 -0700520 err |= __put_user(fx->swd, &buf->status);
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100521 err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
522 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return -1;
524
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700525 if (__copy_to_user(&buf->_fxsr_env[0], fx, xstate_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return -1;
527 return 1;
528}
529
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700530static int save_i387_xsave(void __user *buf)
531{
Suresh Siddha04944b72008-10-07 14:04:28 -0700532 struct task_struct *tsk = current;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700533 struct _fpstate_ia32 __user *fx = buf;
534 int err = 0;
535
Suresh Siddha04944b72008-10-07 14:04:28 -0700536 /*
537 * For legacy compatible, we always set FP/SSE bits in the bit
538 * vector while saving the state to the user context.
539 * This will enable us capturing any changes(during sigreturn) to
540 * the FP/SSE bits by the legacy applications which don't touch
541 * xstate_bv in the xsave header.
542 *
543 * xsave aware applications can change the xstate_bv in the xsave
544 * header as well as change any contents in the memory layout.
545 * xrestore as part of sigreturn will capture all the changes.
546 */
547 tsk->thread.xstate->xsave.xsave_hdr.xstate_bv |= XSTATE_FPSSE;
548
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700549 if (save_i387_fxsave(fx) < 0)
550 return -1;
551
552 err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved_ia32,
553 sizeof(struct _fpx_sw_bytes));
554 err |= __put_user(FP_XSTATE_MAGIC2,
555 (__u32 __user *) (buf + sig_xstate_ia32_size
556 - FP_XSTATE_MAGIC2_SIZE));
557 if (err)
558 return -1;
559
560 return 1;
561}
562
Suresh Siddhaab513702008-07-29 10:29:22 -0700563int save_i387_xstate_ia32(void __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Suresh Siddhaab513702008-07-29 10:29:22 -0700565 struct _fpstate_ia32 __user *fp = (struct _fpstate_ia32 __user *) buf;
566 struct task_struct *tsk = current;
567
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100568 if (!used_math())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700570
571 if (!access_ok(VERIFY_WRITE, buf, sig_xstate_ia32_size))
572 return -EACCES;
Ingo Molnarf6689642008-03-05 15:37:32 +0100573 /*
574 * This will cause a "finit" to be triggered by the next
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 * attempted FPU operation by the 'current' process.
576 */
577 clear_used_math();
578
Ingo Molnarf6689642008-03-05 15:37:32 +0100579 if (!HAVE_HWFP) {
Roland McGrath44210112008-01-30 13:31:50 +0100580 return fpregs_soft_get(current, NULL,
581 0, sizeof(struct user_i387_ia32_struct),
Suresh Siddhaab513702008-07-29 10:29:22 -0700582 NULL, fp) ? -1 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
Ingo Molnarf6689642008-03-05 15:37:32 +0100584
Suresh Siddhaab513702008-07-29 10:29:22 -0700585 unlazy_fpu(tsk);
586
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700587 if (cpu_has_xsave)
588 return save_i387_xsave(fp);
Ingo Molnarf6689642008-03-05 15:37:32 +0100589 if (cpu_has_fxsr)
Suresh Siddhaab513702008-07-29 10:29:22 -0700590 return save_i387_fxsave(fp);
Ingo Molnarf6689642008-03-05 15:37:32 +0100591 else
Suresh Siddhaab513702008-07-29 10:29:22 -0700592 return save_i387_fsave(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
Roland McGrath44210112008-01-30 13:31:50 +0100595static inline int restore_i387_fsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 struct task_struct *tsk = current;
Ingo Molnarf6689642008-03-05 15:37:32 +0100598
Suresh Siddha61c46282008-03-10 15:28:04 -0700599 return __copy_from_user(&tsk->thread.xstate->fsave, buf,
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100600 sizeof(struct i387_fsave_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700603static int restore_i387_fxsave(struct _fpstate_ia32 __user *buf,
604 unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 struct task_struct *tsk = current;
Roland McGrath44210112008-01-30 13:31:50 +0100607 struct user_i387_ia32_struct env;
Ingo Molnarf6689642008-03-05 15:37:32 +0100608 int err;
609
Suresh Siddha61c46282008-03-10 15:28:04 -0700610 err = __copy_from_user(&tsk->thread.xstate->fxsave, &buf->_fxsr_env[0],
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700611 size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* mxcsr reserved bits must be masked to zero for security reasons */
Suresh Siddha61c46282008-03-10 15:28:04 -0700613 tsk->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
Roland McGrath44210112008-01-30 13:31:50 +0100614 if (err || __copy_from_user(&env, buf, sizeof(env)))
615 return 1;
616 convert_to_fxsr(tsk, &env);
Ingo Molnarf6689642008-03-05 15:37:32 +0100617
Roland McGrath44210112008-01-30 13:31:50 +0100618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700621static int restore_i387_xsave(void __user *buf)
622{
623 struct _fpx_sw_bytes fx_sw_user;
624 struct _fpstate_ia32 __user *fx_user =
625 ((struct _fpstate_ia32 __user *) buf);
626 struct i387_fxsave_struct __user *fx =
627 (struct i387_fxsave_struct __user *) &fx_user->_fxsr_env[0];
628 struct xsave_hdr_struct *xsave_hdr =
629 &current->thread.xstate->xsave.xsave_hdr;
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700630 u64 mask;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700631 int err;
632
633 if (check_for_xstate(fx, buf, &fx_sw_user))
634 goto fx_only;
635
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700636 mask = fx_sw_user.xstate_bv;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700637
638 err = restore_i387_fxsave(buf, fx_sw_user.xstate_size);
639
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700640 xsave_hdr->xstate_bv &= pcntxt_mask;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700641 /*
642 * These bits must be zero.
643 */
644 xsave_hdr->reserved1[0] = xsave_hdr->reserved1[1] = 0;
645
646 /*
647 * Init the state that is not present in the memory layout
648 * and enabled by the OS.
649 */
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700650 mask = ~(pcntxt_mask & ~mask);
651 xsave_hdr->xstate_bv &= mask;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700652
653 return err;
654fx_only:
655 /*
656 * Couldn't find the extended state information in the memory
657 * layout. Restore the FP/SSE and init the other extended state
658 * enabled by the OS.
659 */
660 xsave_hdr->xstate_bv = XSTATE_FPSSE;
661 return restore_i387_fxsave(buf, sizeof(struct i387_fxsave_struct));
662}
663
Suresh Siddhaab513702008-07-29 10:29:22 -0700664int restore_i387_xstate_ia32(void __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 int err;
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700667 struct task_struct *tsk = current;
Suresh Siddhaab513702008-07-29 10:29:22 -0700668 struct _fpstate_ia32 __user *fp = (struct _fpstate_ia32 __user *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700670 if (HAVE_HWFP)
Suresh Siddhafd3c3ed2008-05-07 12:09:52 -0700671 clear_fpu(tsk);
672
Suresh Siddhaab513702008-07-29 10:29:22 -0700673 if (!buf) {
674 if (used_math()) {
675 clear_fpu(tsk);
676 clear_used_math();
677 }
678
679 return 0;
680 } else
681 if (!access_ok(VERIFY_READ, buf, sig_xstate_ia32_size))
682 return -EACCES;
683
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700684 if (!used_math()) {
685 err = init_fpu(tsk);
686 if (err)
687 return err;
688 }
Suresh Siddhafd3c3ed2008-05-07 12:09:52 -0700689
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700690 if (HAVE_HWFP) {
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700691 if (cpu_has_xsave)
692 err = restore_i387_xsave(buf);
693 else if (cpu_has_fxsr)
694 err = restore_i387_fxsave(fp, sizeof(struct
695 i387_fxsave_struct));
Ingo Molnarf6689642008-03-05 15:37:32 +0100696 else
Suresh Siddhaab513702008-07-29 10:29:22 -0700697 err = restore_i387_fsave(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 } else {
Roland McGrath44210112008-01-30 13:31:50 +0100699 err = fpregs_soft_set(current, NULL,
700 0, sizeof(struct user_i387_ia32_struct),
Suresh Siddhaab513702008-07-29 10:29:22 -0700701 NULL, fp) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703 set_used_math();
Ingo Molnarf6689642008-03-05 15:37:32 +0100704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return err;
706}
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708/*
709 * FPU state for core dumps.
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100710 * This is only used for a.out dumps now.
711 * It is declared generically using elf_fpregset_t (which is
712 * struct user_i387_struct) but is in fact only used for 32-bit
713 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100715int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 struct task_struct *tsk = current;
Ingo Molnarf6689642008-03-05 15:37:32 +0100718 int fpvalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 fpvalid = !!used_math();
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100721 if (fpvalid)
722 fpvalid = !fpregs_get(tsk, NULL,
723 0, sizeof(struct user_i387_ia32_struct),
724 fpu, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 return fpvalid;
727}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700728EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100730#endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */