blob: 51fb288a2c97e6e8a06989231d3273f3979d605b [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
Suresh Siddha3c1c7f12008-07-29 10:29:21 -070027# define sig_xstate_ia32_size sig_xstate_size
Ingo Molnarf6689642008-03-05 15:37:32 +010028# define user_i387_ia32_struct user_i387_struct
29# define user32_fxsr_struct user_fxsr_struct
Roland McGrath44210112008-01-30 13:31:50 +010030#endif
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#ifdef CONFIG_MATH_EMULATION
Ingo Molnarf6689642008-03-05 15:37:32 +010033# define HAVE_HWFP (boot_cpu_data.hard_math)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#else
Ingo Molnarf6689642008-03-05 15:37:32 +010035# define HAVE_HWFP 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#endif
37
Ingo Molnarf6689642008-03-05 15:37:32 +010038static unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
Suresh Siddha61c46282008-03-10 15:28:04 -070039unsigned int xstate_size;
Suresh Siddha3c1c7f12008-07-29 10:29:21 -070040unsigned int sig_xstate_ia32_size = sizeof(struct _fpstate_ia32);
Suresh Siddha61c46282008-03-10 15:28:04 -070041static struct i387_fxsave_struct fx_scratch __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Suresh Siddha61c46282008-03-10 15:28:04 -070043void __cpuinit mxcsr_feature_mask_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 unsigned long mask = 0;
Ingo Molnarf6689642008-03-05 15:37:32 +010046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 clts();
48 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -070049 memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct));
50 asm volatile("fxsave %0" : : "m" (fx_scratch));
51 mask = fx_scratch.mxcsr_mask;
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +010052 if (mask == 0)
53 mask = 0x0000ffbf;
54 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 mxcsr_feature_mask &= mask;
56 stts();
57}
58
Suresh Siddha61c46282008-03-10 15:28:04 -070059void __init init_thread_xstate(void)
60{
Suresh Siddhae8a496a2008-05-23 16:26:37 -070061 if (!HAVE_HWFP) {
62 xstate_size = sizeof(struct i387_soft_struct);
63 return;
64 }
65
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070066 if (cpu_has_xsave) {
67 xsave_cntxt_init();
68 return;
69 }
70
Suresh Siddha61c46282008-03-10 15:28:04 -070071 if (cpu_has_fxsr)
72 xstate_size = sizeof(struct i387_fxsave_struct);
73#ifdef CONFIG_X86_32
74 else
75 xstate_size = sizeof(struct i387_fsave_struct);
76#endif
Suresh Siddha61c46282008-03-10 15:28:04 -070077}
78
Roland McGrath44210112008-01-30 13:31:50 +010079#ifdef CONFIG_X86_64
80/*
81 * Called at bootup to set up the initial FPU state that is later cloned
82 * into all processes.
83 */
84void __cpuinit fpu_init(void)
85{
86 unsigned long oldcr0 = read_cr0();
Ingo Molnarf6689642008-03-05 15:37:32 +010087
Roland McGrath44210112008-01-30 13:31:50 +010088 set_in_cr4(X86_CR4_OSFXSR);
89 set_in_cr4(X86_CR4_OSXMMEXCPT);
90
Ingo Molnarf6689642008-03-05 15:37:32 +010091 write_cr0(oldcr0 & ~(X86_CR0_TS|X86_CR0_EM)); /* clear TS and EM */
Roland McGrath44210112008-01-30 13:31:50 +010092
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070093 /*
94 * Boot processor to setup the FP and extended state context info.
95 */
96 if (!smp_processor_id())
97 init_thread_xstate();
98 xsave_init();
99
Roland McGrath44210112008-01-30 13:31:50 +0100100 mxcsr_feature_mask_init();
101 /* clean state in init */
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700102 if (cpu_has_xsave)
103 current_thread_info()->status = TS_XSAVE;
104 else
105 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);
137 finit();
138 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
Roland McGrath44210112008-01-30 13:31:50 +0100165int fpregs_active(struct task_struct *target, const struct user_regset *regset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Roland McGrath44210112008-01-30 13:31:50 +0100167 return tsk_used_math(target) ? regset->n : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
Roland McGrath44210112008-01-30 13:31:50 +0100169
170int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
171{
172 return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
173}
174
175int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
176 unsigned int pos, unsigned int count,
177 void *kbuf, void __user *ubuf)
178{
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700179 int ret;
180
Roland McGrath44210112008-01-30 13:31:50 +0100181 if (!cpu_has_fxsr)
182 return -ENODEV;
183
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700184 ret = init_fpu(target);
185 if (ret)
186 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100187
188 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700189 &target->thread.xstate->fxsave, 0, -1);
Roland McGrath44210112008-01-30 13:31:50 +0100190}
191
192int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
193 unsigned int pos, unsigned int count,
194 const void *kbuf, const void __user *ubuf)
195{
196 int ret;
197
198 if (!cpu_has_fxsr)
199 return -ENODEV;
200
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700201 ret = init_fpu(target);
202 if (ret)
203 return ret;
204
Roland McGrath44210112008-01-30 13:31:50 +0100205 set_stopped_child_used_math(target);
206
207 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700208 &target->thread.xstate->fxsave, 0, -1);
Roland McGrath44210112008-01-30 13:31:50 +0100209
210 /*
211 * mxcsr reserved bits must be masked to zero for security reasons.
212 */
Suresh Siddha61c46282008-03-10 15:28:04 -0700213 target->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
Roland McGrath44210112008-01-30 13:31:50 +0100214
215 return ret;
216}
217
218#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/*
221 * FPU tag word conversions.
222 */
223
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100224static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100229 tmp = ~twd;
Roland McGrath44210112008-01-30 13:31:50 +0100230 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100231 /* and move the valid bits to the lower byte. */
232 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
233 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
234 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
Ingo Molnarf6689642008-03-05 15:37:32 +0100235
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100236 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Roland McGrath44210112008-01-30 13:31:50 +0100239#define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
240#define FP_EXP_TAG_VALID 0
241#define FP_EXP_TAG_ZERO 1
242#define FP_EXP_TAG_SPECIAL 2
243#define FP_EXP_TAG_EMPTY 3
244
245static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Roland McGrath44210112008-01-30 13:31:50 +0100247 struct _fpxreg *st;
248 u32 tos = (fxsave->swd >> 11) & 7;
249 u32 twd = (unsigned long) fxsave->twd;
250 u32 tag;
251 u32 ret = 0xffff0000u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 int i;
253
Roland McGrath44210112008-01-30 13:31:50 +0100254 for (i = 0; i < 8; i++, twd >>= 1) {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100255 if (twd & 0x1) {
256 st = FPREG_ADDR(fxsave, (i - tos) & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100258 switch (st->exponent & 0x7fff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 case 0x7fff:
Roland McGrath44210112008-01-30 13:31:50 +0100260 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 break;
262 case 0x0000:
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100263 if (!st->significand[0] &&
264 !st->significand[1] &&
265 !st->significand[2] &&
Roland McGrath44210112008-01-30 13:31:50 +0100266 !st->significand[3])
267 tag = FP_EXP_TAG_ZERO;
268 else
269 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 break;
271 default:
Roland McGrath44210112008-01-30 13:31:50 +0100272 if (st->significand[3] & 0x8000)
273 tag = FP_EXP_TAG_VALID;
274 else
275 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 break;
277 }
278 } else {
Roland McGrath44210112008-01-30 13:31:50 +0100279 tag = FP_EXP_TAG_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
Roland McGrath44210112008-01-30 13:31:50 +0100281 ret |= tag << (2 * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283 return ret;
284}
285
286/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * FXSR floating point environment conversions.
288 */
289
Ingo Molnarf6689642008-03-05 15:37:32 +0100290static void
291convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Suresh Siddha61c46282008-03-10 15:28:04 -0700293 struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100294 struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
295 struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 int i;
297
Roland McGrath44210112008-01-30 13:31:50 +0100298 env->cwd = fxsave->cwd | 0xffff0000u;
299 env->swd = fxsave->swd | 0xffff0000u;
300 env->twd = twd_fxsr_to_i387(fxsave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Roland McGrath44210112008-01-30 13:31:50 +0100302#ifdef CONFIG_X86_64
303 env->fip = fxsave->rip;
304 env->foo = fxsave->rdp;
305 if (tsk == current) {
306 /*
307 * should be actually ds/cs at fpu exception time, but
308 * that information is not available in 64bit mode.
309 */
Ingo Molnarf6689642008-03-05 15:37:32 +0100310 asm("mov %%ds, %[fos]" : [fos] "=r" (env->fos));
311 asm("mov %%cs, %[fcs]" : [fcs] "=r" (env->fcs));
Roland McGrath44210112008-01-30 13:31:50 +0100312 } else {
313 struct pt_regs *regs = task_pt_regs(tsk);
Ingo Molnarf6689642008-03-05 15:37:32 +0100314
Roland McGrath44210112008-01-30 13:31:50 +0100315 env->fos = 0xffff0000 | tsk->thread.ds;
316 env->fcs = regs->cs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
Roland McGrath44210112008-01-30 13:31:50 +0100318#else
319 env->fip = fxsave->fip;
Jan Beulich609b5292008-03-05 08:35:14 +0000320 env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
Roland McGrath44210112008-01-30 13:31:50 +0100321 env->foo = fxsave->foo;
322 env->fos = fxsave->fos;
323#endif
324
325 for (i = 0; i < 8; ++i)
326 memcpy(&to[i], &from[i], sizeof(to[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Roland McGrath44210112008-01-30 13:31:50 +0100329static void convert_to_fxsr(struct task_struct *tsk,
330 const struct user_i387_ia32_struct *env)
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Suresh Siddha61c46282008-03-10 15:28:04 -0700333 struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100334 struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
335 struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 int i;
337
Roland McGrath44210112008-01-30 13:31:50 +0100338 fxsave->cwd = env->cwd;
339 fxsave->swd = env->swd;
340 fxsave->twd = twd_i387_to_fxsr(env->twd);
341 fxsave->fop = (u16) ((u32) env->fcs >> 16);
342#ifdef CONFIG_X86_64
343 fxsave->rip = env->fip;
344 fxsave->rdp = env->foo;
345 /* cs and ds ignored */
346#else
347 fxsave->fip = env->fip;
348 fxsave->fcs = (env->fcs & 0xffff);
349 fxsave->foo = env->foo;
350 fxsave->fos = env->fos;
351#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Roland McGrath44210112008-01-30 13:31:50 +0100353 for (i = 0; i < 8; ++i)
354 memcpy(&to[i], &from[i], sizeof(from[0]));
355}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Roland McGrath44210112008-01-30 13:31:50 +0100357int fpregs_get(struct task_struct *target, const struct user_regset *regset,
358 unsigned int pos, unsigned int count,
359 void *kbuf, void __user *ubuf)
360{
361 struct user_i387_ia32_struct env;
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700362 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700364 ret = init_fpu(target);
365 if (ret)
366 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100367
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700368 if (!HAVE_HWFP)
369 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
370
Ingo Molnarf6689642008-03-05 15:37:32 +0100371 if (!cpu_has_fxsr) {
Roland McGrath44210112008-01-30 13:31:50 +0100372 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700373 &target->thread.xstate->fsave, 0,
374 -1);
Ingo Molnarf6689642008-03-05 15:37:32 +0100375 }
Roland McGrath44210112008-01-30 13:31:50 +0100376
377 if (kbuf && pos == 0 && count == sizeof(env)) {
378 convert_from_fxsr(kbuf, target);
379 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
Roland McGrath44210112008-01-30 13:31:50 +0100381
382 convert_from_fxsr(&env, target);
Ingo Molnarf6689642008-03-05 15:37:32 +0100383
Roland McGrath44210112008-01-30 13:31:50 +0100384 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
385}
386
387int fpregs_set(struct task_struct *target, const struct user_regset *regset,
388 unsigned int pos, unsigned int count,
389 const void *kbuf, const void __user *ubuf)
390{
391 struct user_i387_ia32_struct env;
392 int ret;
393
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700394 ret = init_fpu(target);
395 if (ret)
396 return ret;
397
Roland McGrath44210112008-01-30 13:31:50 +0100398 set_stopped_child_used_math(target);
399
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700400 if (!HAVE_HWFP)
401 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
402
Ingo Molnarf6689642008-03-05 15:37:32 +0100403 if (!cpu_has_fxsr) {
Roland McGrath44210112008-01-30 13:31:50 +0100404 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700405 &target->thread.xstate->fsave, 0, -1);
Ingo Molnarf6689642008-03-05 15:37:32 +0100406 }
Roland McGrath44210112008-01-30 13:31:50 +0100407
408 if (pos > 0 || count < sizeof(env))
409 convert_from_fxsr(&env, target);
410
411 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
412 if (!ret)
413 convert_to_fxsr(target, &env);
414
415 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
418/*
419 * Signal frame handlers.
420 */
421
Roland McGrath44210112008-01-30 13:31:50 +0100422static inline int save_i387_fsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct task_struct *tsk = current;
Suresh Siddha61c46282008-03-10 15:28:04 -0700425 struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100427 unlazy_fpu(tsk);
Suresh Siddha61c46282008-03-10 15:28:04 -0700428 fp->status = fp->swd;
429 if (__copy_to_user(buf, fp, sizeof(struct i387_fsave_struct)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return -1;
431 return 1;
432}
433
Roland McGrath44210112008-01-30 13:31:50 +0100434static int save_i387_fxsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 struct task_struct *tsk = current;
Suresh Siddha61c46282008-03-10 15:28:04 -0700437 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100438 struct user_i387_ia32_struct env;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 int err = 0;
440
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100441 unlazy_fpu(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Roland McGrath44210112008-01-30 13:31:50 +0100443 convert_from_fxsr(&env, tsk);
444 if (__copy_to_user(buf, &env, sizeof(env)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return -1;
446
Suresh Siddha61c46282008-03-10 15:28:04 -0700447 err |= __put_user(fx->swd, &buf->status);
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100448 err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
449 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return -1;
451
Suresh Siddha61c46282008-03-10 15:28:04 -0700452 if (__copy_to_user(&buf->_fxsr_env[0], fx,
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100453 sizeof(struct i387_fxsave_struct)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return -1;
455 return 1;
456}
457
Roland McGrath44210112008-01-30 13:31:50 +0100458int save_i387_ia32(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100460 if (!used_math())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return 0;
Ingo Molnarf6689642008-03-05 15:37:32 +0100462 /*
463 * This will cause a "finit" to be triggered by the next
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 * attempted FPU operation by the 'current' process.
465 */
466 clear_used_math();
467
Ingo Molnarf6689642008-03-05 15:37:32 +0100468 if (!HAVE_HWFP) {
Roland McGrath44210112008-01-30 13:31:50 +0100469 return fpregs_soft_get(current, NULL,
470 0, sizeof(struct user_i387_ia32_struct),
471 NULL, buf) ? -1 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
Ingo Molnarf6689642008-03-05 15:37:32 +0100473
474 if (cpu_has_fxsr)
475 return save_i387_fxsave(buf);
476 else
477 return save_i387_fsave(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Roland McGrath44210112008-01-30 13:31:50 +0100480static inline int restore_i387_fsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 struct task_struct *tsk = current;
Ingo Molnarf6689642008-03-05 15:37:32 +0100483
Suresh Siddha61c46282008-03-10 15:28:04 -0700484 return __copy_from_user(&tsk->thread.xstate->fsave, buf,
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100485 sizeof(struct i387_fsave_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Roland McGrath44210112008-01-30 13:31:50 +0100488static int restore_i387_fxsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 struct task_struct *tsk = current;
Roland McGrath44210112008-01-30 13:31:50 +0100491 struct user_i387_ia32_struct env;
Ingo Molnarf6689642008-03-05 15:37:32 +0100492 int err;
493
Suresh Siddha61c46282008-03-10 15:28:04 -0700494 err = __copy_from_user(&tsk->thread.xstate->fxsave, &buf->_fxsr_env[0],
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100495 sizeof(struct i387_fxsave_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /* mxcsr reserved bits must be masked to zero for security reasons */
Suresh Siddha61c46282008-03-10 15:28:04 -0700497 tsk->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
Roland McGrath44210112008-01-30 13:31:50 +0100498 if (err || __copy_from_user(&env, buf, sizeof(env)))
499 return 1;
500 convert_to_fxsr(tsk, &env);
Ingo Molnarf6689642008-03-05 15:37:32 +0100501
Roland McGrath44210112008-01-30 13:31:50 +0100502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Roland McGrath44210112008-01-30 13:31:50 +0100505int restore_i387_ia32(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 int err;
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700508 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700510 if (HAVE_HWFP)
Suresh Siddhafd3c3ed2008-05-07 12:09:52 -0700511 clear_fpu(tsk);
512
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700513 if (!used_math()) {
514 err = init_fpu(tsk);
515 if (err)
516 return err;
517 }
Suresh Siddhafd3c3ed2008-05-07 12:09:52 -0700518
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700519 if (HAVE_HWFP) {
Ingo Molnarf6689642008-03-05 15:37:32 +0100520 if (cpu_has_fxsr)
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100521 err = restore_i387_fxsave(buf);
Ingo Molnarf6689642008-03-05 15:37:32 +0100522 else
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100523 err = restore_i387_fsave(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 } else {
Roland McGrath44210112008-01-30 13:31:50 +0100525 err = fpregs_soft_set(current, NULL,
526 0, sizeof(struct user_i387_ia32_struct),
527 NULL, buf) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529 set_used_math();
Ingo Molnarf6689642008-03-05 15:37:32 +0100530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return err;
532}
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534/*
535 * FPU state for core dumps.
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100536 * This is only used for a.out dumps now.
537 * It is declared generically using elf_fpregset_t (which is
538 * struct user_i387_struct) but is in fact only used for 32-bit
539 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100541int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 struct task_struct *tsk = current;
Ingo Molnarf6689642008-03-05 15:37:32 +0100544 int fpvalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 fpvalid = !!used_math();
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100547 if (fpvalid)
548 fpvalid = !fpregs_get(tsk, NULL,
549 0, sizeof(struct user_i387_ia32_struct),
550 fpu, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 return fpvalid;
553}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700554EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100556#endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */