blob: e03cc952f233db9a2d77a1592798a96cf68a0674 [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>
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/sigcontext.h>
Ingo Molnarf6689642008-03-05 15:37:32 +010013#include <asm/processor.h>
14#include <asm/math_emu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <asm/uaccess.h>
Ingo Molnarf6689642008-03-05 15:37:32 +010016#include <asm/ptrace.h>
17#include <asm/i387.h>
18#include <asm/user.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Roland McGrath44210112008-01-30 13:31:50 +010020#ifdef CONFIG_X86_64
Ingo Molnarf6689642008-03-05 15:37:32 +010021# include <asm/sigcontext32.h>
22# include <asm/user32.h>
Roland McGrath44210112008-01-30 13:31:50 +010023#else
Ingo Molnarf6689642008-03-05 15:37:32 +010024# define save_i387_ia32 save_i387
25# define restore_i387_ia32 restore_i387
26# define _fpstate_ia32 _fpstate
27# define user_i387_ia32_struct user_i387_struct
28# define user32_fxsr_struct user_fxsr_struct
Roland McGrath44210112008-01-30 13:31:50 +010029#endif
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#ifdef CONFIG_MATH_EMULATION
Ingo Molnarf6689642008-03-05 15:37:32 +010032# define HAVE_HWFP (boot_cpu_data.hard_math)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#else
Ingo Molnarf6689642008-03-05 15:37:32 +010034# define HAVE_HWFP 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#endif
36
Ingo Molnarf6689642008-03-05 15:37:32 +010037static unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
Suresh Siddha61c46282008-03-10 15:28:04 -070038unsigned int xstate_size;
39static struct i387_fxsave_struct fx_scratch __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Suresh Siddha61c46282008-03-10 15:28:04 -070041void __cpuinit mxcsr_feature_mask_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 unsigned long mask = 0;
Ingo Molnarf6689642008-03-05 15:37:32 +010044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 clts();
46 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -070047 memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct));
48 asm volatile("fxsave %0" : : "m" (fx_scratch));
49 mask = fx_scratch.mxcsr_mask;
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +010050 if (mask == 0)
51 mask = 0x0000ffbf;
52 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 mxcsr_feature_mask &= mask;
54 stts();
55}
56
Suresh Siddha61c46282008-03-10 15:28:04 -070057void __init init_thread_xstate(void)
58{
59 if (cpu_has_fxsr)
60 xstate_size = sizeof(struct i387_fxsave_struct);
61#ifdef CONFIG_X86_32
62 else
63 xstate_size = sizeof(struct i387_fsave_struct);
64#endif
Suresh Siddha61c46282008-03-10 15:28:04 -070065}
66
Roland McGrath44210112008-01-30 13:31:50 +010067#ifdef CONFIG_X86_64
68/*
69 * Called at bootup to set up the initial FPU state that is later cloned
70 * into all processes.
71 */
72void __cpuinit fpu_init(void)
73{
74 unsigned long oldcr0 = read_cr0();
Ingo Molnarf6689642008-03-05 15:37:32 +010075
Roland McGrath44210112008-01-30 13:31:50 +010076 set_in_cr4(X86_CR4_OSFXSR);
77 set_in_cr4(X86_CR4_OSXMMEXCPT);
78
Ingo Molnarf6689642008-03-05 15:37:32 +010079 write_cr0(oldcr0 & ~(X86_CR0_TS|X86_CR0_EM)); /* clear TS and EM */
Roland McGrath44210112008-01-30 13:31:50 +010080
81 mxcsr_feature_mask_init();
82 /* clean state in init */
83 current_thread_info()->status = 0;
84 clear_used_math();
85}
86#endif /* CONFIG_X86_64 */
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/*
89 * The _current_ task is using the FPU for the first time
90 * so initialize it and set the mxcsr to its default
91 * value at reset if we support XMM instructions and then
92 * remeber the current task has used the FPU.
93 */
Suresh Siddhaaa283f42008-03-10 15:28:05 -070094int init_fpu(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Roland McGrath44210112008-01-30 13:31:50 +010096 if (tsk_used_math(tsk)) {
97 if (tsk == current)
98 unlazy_fpu(tsk);
Suresh Siddhaaa283f42008-03-10 15:28:05 -070099 return 0;
100 }
101
102 /*
103 * Memory allocation at the first usage of the FPU and other state.
104 */
105 if (!tsk->thread.xstate) {
106 tsk->thread.xstate = kmem_cache_alloc(task_xstate_cachep,
107 GFP_KERNEL);
108 if (!tsk->thread.xstate)
109 return -ENOMEM;
Roland McGrath44210112008-01-30 13:31:50 +0100110 }
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700113 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
114
115 memset(fx, 0, xstate_size);
116 fx->cwd = 0x37f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (cpu_has_xmm)
Suresh Siddha61c46282008-03-10 15:28:04 -0700118 fx->mxcsr = MXCSR_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 } else {
Suresh Siddha61c46282008-03-10 15:28:04 -0700120 struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
121 memset(fp, 0, xstate_size);
122 fp->cwd = 0xffff037fu;
123 fp->swd = 0xffff0000u;
124 fp->twd = 0xffffffffu;
125 fp->fos = 0xffff0000u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
Roland McGrath44210112008-01-30 13:31:50 +0100127 /*
128 * Only the device not available exception or ptrace can call init_fpu.
129 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 set_stopped_child_used_math(tsk);
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Roland McGrath44210112008-01-30 13:31:50 +0100134int fpregs_active(struct task_struct *target, const struct user_regset *regset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Roland McGrath44210112008-01-30 13:31:50 +0100136 return tsk_used_math(target) ? regset->n : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
Roland McGrath44210112008-01-30 13:31:50 +0100138
139int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
140{
141 return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
142}
143
144int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
145 unsigned int pos, unsigned int count,
146 void *kbuf, void __user *ubuf)
147{
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700148 int ret;
149
Roland McGrath44210112008-01-30 13:31:50 +0100150 if (!cpu_has_fxsr)
151 return -ENODEV;
152
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700153 ret = init_fpu(target);
154 if (ret)
155 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100156
157 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700158 &target->thread.xstate->fxsave, 0, -1);
Roland McGrath44210112008-01-30 13:31:50 +0100159}
160
161int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
162 unsigned int pos, unsigned int count,
163 const void *kbuf, const void __user *ubuf)
164{
165 int ret;
166
167 if (!cpu_has_fxsr)
168 return -ENODEV;
169
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700170 ret = init_fpu(target);
171 if (ret)
172 return ret;
173
Roland McGrath44210112008-01-30 13:31:50 +0100174 set_stopped_child_used_math(target);
175
176 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700177 &target->thread.xstate->fxsave, 0, -1);
Roland McGrath44210112008-01-30 13:31:50 +0100178
179 /*
180 * mxcsr reserved bits must be masked to zero for security reasons.
181 */
Suresh Siddha61c46282008-03-10 15:28:04 -0700182 target->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
Roland McGrath44210112008-01-30 13:31:50 +0100183
184 return ret;
185}
186
187#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
190 * FPU tag word conversions.
191 */
192
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100193static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100198 tmp = ~twd;
Roland McGrath44210112008-01-30 13:31:50 +0100199 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100200 /* and move the valid bits to the lower byte. */
201 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
202 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
203 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
Ingo Molnarf6689642008-03-05 15:37:32 +0100204
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100205 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Roland McGrath44210112008-01-30 13:31:50 +0100208#define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
209#define FP_EXP_TAG_VALID 0
210#define FP_EXP_TAG_ZERO 1
211#define FP_EXP_TAG_SPECIAL 2
212#define FP_EXP_TAG_EMPTY 3
213
214static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Roland McGrath44210112008-01-30 13:31:50 +0100216 struct _fpxreg *st;
217 u32 tos = (fxsave->swd >> 11) & 7;
218 u32 twd = (unsigned long) fxsave->twd;
219 u32 tag;
220 u32 ret = 0xffff0000u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 int i;
222
Roland McGrath44210112008-01-30 13:31:50 +0100223 for (i = 0; i < 8; i++, twd >>= 1) {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100224 if (twd & 0x1) {
225 st = FPREG_ADDR(fxsave, (i - tos) & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100227 switch (st->exponent & 0x7fff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 case 0x7fff:
Roland McGrath44210112008-01-30 13:31:50 +0100229 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 break;
231 case 0x0000:
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100232 if (!st->significand[0] &&
233 !st->significand[1] &&
234 !st->significand[2] &&
Roland McGrath44210112008-01-30 13:31:50 +0100235 !st->significand[3])
236 tag = FP_EXP_TAG_ZERO;
237 else
238 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 break;
240 default:
Roland McGrath44210112008-01-30 13:31:50 +0100241 if (st->significand[3] & 0x8000)
242 tag = FP_EXP_TAG_VALID;
243 else
244 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 break;
246 }
247 } else {
Roland McGrath44210112008-01-30 13:31:50 +0100248 tag = FP_EXP_TAG_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Roland McGrath44210112008-01-30 13:31:50 +0100250 ret |= tag << (2 * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 return ret;
253}
254
255/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 * FXSR floating point environment conversions.
257 */
258
Ingo Molnarf6689642008-03-05 15:37:32 +0100259static void
260convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Suresh Siddha61c46282008-03-10 15:28:04 -0700262 struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100263 struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
264 struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 int i;
266
Roland McGrath44210112008-01-30 13:31:50 +0100267 env->cwd = fxsave->cwd | 0xffff0000u;
268 env->swd = fxsave->swd | 0xffff0000u;
269 env->twd = twd_fxsr_to_i387(fxsave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Roland McGrath44210112008-01-30 13:31:50 +0100271#ifdef CONFIG_X86_64
272 env->fip = fxsave->rip;
273 env->foo = fxsave->rdp;
274 if (tsk == current) {
275 /*
276 * should be actually ds/cs at fpu exception time, but
277 * that information is not available in 64bit mode.
278 */
Ingo Molnarf6689642008-03-05 15:37:32 +0100279 asm("mov %%ds, %[fos]" : [fos] "=r" (env->fos));
280 asm("mov %%cs, %[fcs]" : [fcs] "=r" (env->fcs));
Roland McGrath44210112008-01-30 13:31:50 +0100281 } else {
282 struct pt_regs *regs = task_pt_regs(tsk);
Ingo Molnarf6689642008-03-05 15:37:32 +0100283
Roland McGrath44210112008-01-30 13:31:50 +0100284 env->fos = 0xffff0000 | tsk->thread.ds;
285 env->fcs = regs->cs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Roland McGrath44210112008-01-30 13:31:50 +0100287#else
288 env->fip = fxsave->fip;
Jan Beulich609b5292008-03-05 08:35:14 +0000289 env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
Roland McGrath44210112008-01-30 13:31:50 +0100290 env->foo = fxsave->foo;
291 env->fos = fxsave->fos;
292#endif
293
294 for (i = 0; i < 8; ++i)
295 memcpy(&to[i], &from[i], sizeof(to[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Roland McGrath44210112008-01-30 13:31:50 +0100298static void convert_to_fxsr(struct task_struct *tsk,
299 const struct user_i387_ia32_struct *env)
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Suresh Siddha61c46282008-03-10 15:28:04 -0700302 struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100303 struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
304 struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 int i;
306
Roland McGrath44210112008-01-30 13:31:50 +0100307 fxsave->cwd = env->cwd;
308 fxsave->swd = env->swd;
309 fxsave->twd = twd_i387_to_fxsr(env->twd);
310 fxsave->fop = (u16) ((u32) env->fcs >> 16);
311#ifdef CONFIG_X86_64
312 fxsave->rip = env->fip;
313 fxsave->rdp = env->foo;
314 /* cs and ds ignored */
315#else
316 fxsave->fip = env->fip;
317 fxsave->fcs = (env->fcs & 0xffff);
318 fxsave->foo = env->foo;
319 fxsave->fos = env->fos;
320#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Roland McGrath44210112008-01-30 13:31:50 +0100322 for (i = 0; i < 8; ++i)
323 memcpy(&to[i], &from[i], sizeof(from[0]));
324}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Roland McGrath44210112008-01-30 13:31:50 +0100326int fpregs_get(struct task_struct *target, const struct user_regset *regset,
327 unsigned int pos, unsigned int count,
328 void *kbuf, void __user *ubuf)
329{
330 struct user_i387_ia32_struct env;
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700331 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Roland McGrath44210112008-01-30 13:31:50 +0100333 if (!HAVE_HWFP)
334 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
335
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700336 ret = init_fpu(target);
337 if (ret)
338 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100339
Ingo Molnarf6689642008-03-05 15:37:32 +0100340 if (!cpu_has_fxsr) {
Roland McGrath44210112008-01-30 13:31:50 +0100341 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700342 &target->thread.xstate->fsave, 0,
343 -1);
Ingo Molnarf6689642008-03-05 15:37:32 +0100344 }
Roland McGrath44210112008-01-30 13:31:50 +0100345
346 if (kbuf && pos == 0 && count == sizeof(env)) {
347 convert_from_fxsr(kbuf, target);
348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Roland McGrath44210112008-01-30 13:31:50 +0100350
351 convert_from_fxsr(&env, target);
Ingo Molnarf6689642008-03-05 15:37:32 +0100352
Roland McGrath44210112008-01-30 13:31:50 +0100353 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
354}
355
356int fpregs_set(struct task_struct *target, const struct user_regset *regset,
357 unsigned int pos, unsigned int count,
358 const void *kbuf, const void __user *ubuf)
359{
360 struct user_i387_ia32_struct env;
361 int ret;
362
363 if (!HAVE_HWFP)
364 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
365
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700366 ret = init_fpu(target);
367 if (ret)
368 return ret;
369
Roland McGrath44210112008-01-30 13:31:50 +0100370 set_stopped_child_used_math(target);
371
Ingo Molnarf6689642008-03-05 15:37:32 +0100372 if (!cpu_has_fxsr) {
Roland McGrath44210112008-01-30 13:31:50 +0100373 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700374 &target->thread.xstate->fsave, 0, -1);
Ingo Molnarf6689642008-03-05 15:37:32 +0100375 }
Roland McGrath44210112008-01-30 13:31:50 +0100376
377 if (pos > 0 || count < sizeof(env))
378 convert_from_fxsr(&env, target);
379
380 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
381 if (!ret)
382 convert_to_fxsr(target, &env);
383
384 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
387/*
388 * Signal frame handlers.
389 */
390
Roland McGrath44210112008-01-30 13:31:50 +0100391static inline int save_i387_fsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 struct task_struct *tsk = current;
Suresh Siddha61c46282008-03-10 15:28:04 -0700394 struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100396 unlazy_fpu(tsk);
Suresh Siddha61c46282008-03-10 15:28:04 -0700397 fp->status = fp->swd;
398 if (__copy_to_user(buf, fp, sizeof(struct i387_fsave_struct)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return -1;
400 return 1;
401}
402
Roland McGrath44210112008-01-30 13:31:50 +0100403static int save_i387_fxsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 struct task_struct *tsk = current;
Suresh Siddha61c46282008-03-10 15:28:04 -0700406 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100407 struct user_i387_ia32_struct env;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int err = 0;
409
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100410 unlazy_fpu(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Roland McGrath44210112008-01-30 13:31:50 +0100412 convert_from_fxsr(&env, tsk);
413 if (__copy_to_user(buf, &env, sizeof(env)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return -1;
415
Suresh Siddha61c46282008-03-10 15:28:04 -0700416 err |= __put_user(fx->swd, &buf->status);
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100417 err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
418 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return -1;
420
Suresh Siddha61c46282008-03-10 15:28:04 -0700421 if (__copy_to_user(&buf->_fxsr_env[0], fx,
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100422 sizeof(struct i387_fxsave_struct)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return -1;
424 return 1;
425}
426
Roland McGrath44210112008-01-30 13:31:50 +0100427int save_i387_ia32(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100429 if (!used_math())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return 0;
Ingo Molnarf6689642008-03-05 15:37:32 +0100431 /*
432 * This will cause a "finit" to be triggered by the next
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * attempted FPU operation by the 'current' process.
434 */
435 clear_used_math();
436
Ingo Molnarf6689642008-03-05 15:37:32 +0100437 if (!HAVE_HWFP) {
Roland McGrath44210112008-01-30 13:31:50 +0100438 return fpregs_soft_get(current, NULL,
439 0, sizeof(struct user_i387_ia32_struct),
440 NULL, buf) ? -1 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Ingo Molnarf6689642008-03-05 15:37:32 +0100442
443 if (cpu_has_fxsr)
444 return save_i387_fxsave(buf);
445 else
446 return save_i387_fsave(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Roland McGrath44210112008-01-30 13:31:50 +0100449static inline int restore_i387_fsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 struct task_struct *tsk = current;
Ingo Molnarf6689642008-03-05 15:37:32 +0100452
Suresh Siddha61c46282008-03-10 15:28:04 -0700453 return __copy_from_user(&tsk->thread.xstate->fsave, buf,
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100454 sizeof(struct i387_fsave_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Roland McGrath44210112008-01-30 13:31:50 +0100457static int restore_i387_fxsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 struct task_struct *tsk = current;
Roland McGrath44210112008-01-30 13:31:50 +0100460 struct user_i387_ia32_struct env;
Ingo Molnarf6689642008-03-05 15:37:32 +0100461 int err;
462
Suresh Siddha61c46282008-03-10 15:28:04 -0700463 err = __copy_from_user(&tsk->thread.xstate->fxsave, &buf->_fxsr_env[0],
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100464 sizeof(struct i387_fxsave_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /* mxcsr reserved bits must be masked to zero for security reasons */
Suresh Siddha61c46282008-03-10 15:28:04 -0700466 tsk->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
Roland McGrath44210112008-01-30 13:31:50 +0100467 if (err || __copy_from_user(&env, buf, sizeof(env)))
468 return 1;
469 convert_to_fxsr(tsk, &env);
Ingo Molnarf6689642008-03-05 15:37:32 +0100470
Roland McGrath44210112008-01-30 13:31:50 +0100471 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Roland McGrath44210112008-01-30 13:31:50 +0100474int restore_i387_ia32(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 int err;
477
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100478 if (HAVE_HWFP) {
Suresh Siddhafd3c3ed2008-05-07 12:09:52 -0700479 struct task_struct *tsk = current;
480
481 clear_fpu(tsk);
482
483 if (!used_math()) {
484 err = init_fpu(tsk);
485 if (err)
486 return err;
487 }
488
Ingo Molnarf6689642008-03-05 15:37:32 +0100489 if (cpu_has_fxsr)
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100490 err = restore_i387_fxsave(buf);
Ingo Molnarf6689642008-03-05 15:37:32 +0100491 else
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100492 err = restore_i387_fsave(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 } else {
Roland McGrath44210112008-01-30 13:31:50 +0100494 err = fpregs_soft_set(current, NULL,
495 0, sizeof(struct user_i387_ia32_struct),
496 NULL, buf) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498 set_used_math();
Ingo Molnarf6689642008-03-05 15:37:32 +0100499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return err;
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503/*
504 * FPU state for core dumps.
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100505 * This is only used for a.out dumps now.
506 * It is declared generically using elf_fpregset_t (which is
507 * struct user_i387_struct) but is in fact only used for 32-bit
508 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100510int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 struct task_struct *tsk = current;
Ingo Molnarf6689642008-03-05 15:37:32 +0100513 int fpvalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 fpvalid = !!used_math();
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100516 if (fpvalid)
517 fpvalid = !fpregs_get(tsk, NULL,
518 0, sizeof(struct user_i387_ia32_struct),
519 fpu, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 return fpvalid;
522}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700523EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100525#endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */