blob: e22a9a9dce8a81a95e99c502be9d92c2a3af3e77 [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{
Suresh Siddhae8a496a2008-05-23 16:26:37 -070059 if (!HAVE_HWFP) {
60 xstate_size = sizeof(struct i387_soft_struct);
61 return;
62 }
63
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070064 if (cpu_has_xsave) {
65 xsave_cntxt_init();
66 return;
67 }
68
Suresh Siddha61c46282008-03-10 15:28:04 -070069 if (cpu_has_fxsr)
70 xstate_size = sizeof(struct i387_fxsave_struct);
71#ifdef CONFIG_X86_32
72 else
73 xstate_size = sizeof(struct i387_fsave_struct);
74#endif
Suresh Siddha61c46282008-03-10 15:28:04 -070075}
76
Roland McGrath44210112008-01-30 13:31:50 +010077#ifdef CONFIG_X86_64
78/*
79 * Called at bootup to set up the initial FPU state that is later cloned
80 * into all processes.
81 */
82void __cpuinit fpu_init(void)
83{
84 unsigned long oldcr0 = read_cr0();
Ingo Molnarf6689642008-03-05 15:37:32 +010085
Roland McGrath44210112008-01-30 13:31:50 +010086 set_in_cr4(X86_CR4_OSFXSR);
87 set_in_cr4(X86_CR4_OSXMMEXCPT);
88
Ingo Molnarf6689642008-03-05 15:37:32 +010089 write_cr0(oldcr0 & ~(X86_CR0_TS|X86_CR0_EM)); /* clear TS and EM */
Roland McGrath44210112008-01-30 13:31:50 +010090
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070091 /*
92 * Boot processor to setup the FP and extended state context info.
93 */
94 if (!smp_processor_id())
95 init_thread_xstate();
96 xsave_init();
97
Roland McGrath44210112008-01-30 13:31:50 +010098 mxcsr_feature_mask_init();
99 /* clean state in init */
100 current_thread_info()->status = 0;
101 clear_used_math();
102}
103#endif /* CONFIG_X86_64 */
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*
106 * The _current_ task is using the FPU for the first time
107 * so initialize it and set the mxcsr to its default
108 * value at reset if we support XMM instructions and then
109 * remeber the current task has used the FPU.
110 */
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700111int init_fpu(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Roland McGrath44210112008-01-30 13:31:50 +0100113 if (tsk_used_math(tsk)) {
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700114 if (HAVE_HWFP && tsk == current)
Roland McGrath44210112008-01-30 13:31:50 +0100115 unlazy_fpu(tsk);
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700116 return 0;
117 }
118
119 /*
120 * Memory allocation at the first usage of the FPU and other state.
121 */
122 if (!tsk->thread.xstate) {
123 tsk->thread.xstate = kmem_cache_alloc(task_xstate_cachep,
124 GFP_KERNEL);
125 if (!tsk->thread.xstate)
126 return -ENOMEM;
Roland McGrath44210112008-01-30 13:31:50 +0100127 }
128
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700129#ifdef CONFIG_X86_32
130 if (!HAVE_HWFP) {
131 memset(tsk->thread.xstate, 0, xstate_size);
132 finit();
133 set_stopped_child_used_math(tsk);
134 return 0;
135 }
136#endif
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700139 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
140
141 memset(fx, 0, xstate_size);
142 fx->cwd = 0x37f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (cpu_has_xmm)
Suresh Siddha61c46282008-03-10 15:28:04 -0700144 fx->mxcsr = MXCSR_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 } else {
Suresh Siddha61c46282008-03-10 15:28:04 -0700146 struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
147 memset(fp, 0, xstate_size);
148 fp->cwd = 0xffff037fu;
149 fp->swd = 0xffff0000u;
150 fp->twd = 0xffffffffu;
151 fp->fos = 0xffff0000u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
Roland McGrath44210112008-01-30 13:31:50 +0100153 /*
154 * Only the device not available exception or ptrace can call init_fpu.
155 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 set_stopped_child_used_math(tsk);
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700157 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Roland McGrath44210112008-01-30 13:31:50 +0100160int fpregs_active(struct task_struct *target, const struct user_regset *regset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Roland McGrath44210112008-01-30 13:31:50 +0100162 return tsk_used_math(target) ? regset->n : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
Roland McGrath44210112008-01-30 13:31:50 +0100164
165int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
166{
167 return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
168}
169
170int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
171 unsigned int pos, unsigned int count,
172 void *kbuf, void __user *ubuf)
173{
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700174 int ret;
175
Roland McGrath44210112008-01-30 13:31:50 +0100176 if (!cpu_has_fxsr)
177 return -ENODEV;
178
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700179 ret = init_fpu(target);
180 if (ret)
181 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100182
183 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700184 &target->thread.xstate->fxsave, 0, -1);
Roland McGrath44210112008-01-30 13:31:50 +0100185}
186
187int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
188 unsigned int pos, unsigned int count,
189 const void *kbuf, const void __user *ubuf)
190{
191 int ret;
192
193 if (!cpu_has_fxsr)
194 return -ENODEV;
195
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700196 ret = init_fpu(target);
197 if (ret)
198 return ret;
199
Roland McGrath44210112008-01-30 13:31:50 +0100200 set_stopped_child_used_math(target);
201
202 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700203 &target->thread.xstate->fxsave, 0, -1);
Roland McGrath44210112008-01-30 13:31:50 +0100204
205 /*
206 * mxcsr reserved bits must be masked to zero for security reasons.
207 */
Suresh Siddha61c46282008-03-10 15:28:04 -0700208 target->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
Roland McGrath44210112008-01-30 13:31:50 +0100209
210 return ret;
211}
212
213#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/*
216 * FPU tag word conversions.
217 */
218
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100219static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100224 tmp = ~twd;
Roland McGrath44210112008-01-30 13:31:50 +0100225 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100226 /* and move the valid bits to the lower byte. */
227 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
228 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
229 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
Ingo Molnarf6689642008-03-05 15:37:32 +0100230
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100231 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Roland McGrath44210112008-01-30 13:31:50 +0100234#define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
235#define FP_EXP_TAG_VALID 0
236#define FP_EXP_TAG_ZERO 1
237#define FP_EXP_TAG_SPECIAL 2
238#define FP_EXP_TAG_EMPTY 3
239
240static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Roland McGrath44210112008-01-30 13:31:50 +0100242 struct _fpxreg *st;
243 u32 tos = (fxsave->swd >> 11) & 7;
244 u32 twd = (unsigned long) fxsave->twd;
245 u32 tag;
246 u32 ret = 0xffff0000u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 int i;
248
Roland McGrath44210112008-01-30 13:31:50 +0100249 for (i = 0; i < 8; i++, twd >>= 1) {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100250 if (twd & 0x1) {
251 st = FPREG_ADDR(fxsave, (i - tos) & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100253 switch (st->exponent & 0x7fff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 case 0x7fff:
Roland McGrath44210112008-01-30 13:31:50 +0100255 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 break;
257 case 0x0000:
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100258 if (!st->significand[0] &&
259 !st->significand[1] &&
260 !st->significand[2] &&
Roland McGrath44210112008-01-30 13:31:50 +0100261 !st->significand[3])
262 tag = FP_EXP_TAG_ZERO;
263 else
264 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 break;
266 default:
Roland McGrath44210112008-01-30 13:31:50 +0100267 if (st->significand[3] & 0x8000)
268 tag = FP_EXP_TAG_VALID;
269 else
270 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 break;
272 }
273 } else {
Roland McGrath44210112008-01-30 13:31:50 +0100274 tag = FP_EXP_TAG_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
Roland McGrath44210112008-01-30 13:31:50 +0100276 ret |= tag << (2 * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278 return ret;
279}
280
281/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * FXSR floating point environment conversions.
283 */
284
Ingo Molnarf6689642008-03-05 15:37:32 +0100285static void
286convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Suresh Siddha61c46282008-03-10 15:28:04 -0700288 struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100289 struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
290 struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 int i;
292
Roland McGrath44210112008-01-30 13:31:50 +0100293 env->cwd = fxsave->cwd | 0xffff0000u;
294 env->swd = fxsave->swd | 0xffff0000u;
295 env->twd = twd_fxsr_to_i387(fxsave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Roland McGrath44210112008-01-30 13:31:50 +0100297#ifdef CONFIG_X86_64
298 env->fip = fxsave->rip;
299 env->foo = fxsave->rdp;
300 if (tsk == current) {
301 /*
302 * should be actually ds/cs at fpu exception time, but
303 * that information is not available in 64bit mode.
304 */
Ingo Molnarf6689642008-03-05 15:37:32 +0100305 asm("mov %%ds, %[fos]" : [fos] "=r" (env->fos));
306 asm("mov %%cs, %[fcs]" : [fcs] "=r" (env->fcs));
Roland McGrath44210112008-01-30 13:31:50 +0100307 } else {
308 struct pt_regs *regs = task_pt_regs(tsk);
Ingo Molnarf6689642008-03-05 15:37:32 +0100309
Roland McGrath44210112008-01-30 13:31:50 +0100310 env->fos = 0xffff0000 | tsk->thread.ds;
311 env->fcs = regs->cs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
Roland McGrath44210112008-01-30 13:31:50 +0100313#else
314 env->fip = fxsave->fip;
Jan Beulich609b5292008-03-05 08:35:14 +0000315 env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
Roland McGrath44210112008-01-30 13:31:50 +0100316 env->foo = fxsave->foo;
317 env->fos = fxsave->fos;
318#endif
319
320 for (i = 0; i < 8; ++i)
321 memcpy(&to[i], &from[i], sizeof(to[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Roland McGrath44210112008-01-30 13:31:50 +0100324static void convert_to_fxsr(struct task_struct *tsk,
325 const struct user_i387_ia32_struct *env)
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Suresh Siddha61c46282008-03-10 15:28:04 -0700328 struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100329 struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
330 struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 int i;
332
Roland McGrath44210112008-01-30 13:31:50 +0100333 fxsave->cwd = env->cwd;
334 fxsave->swd = env->swd;
335 fxsave->twd = twd_i387_to_fxsr(env->twd);
336 fxsave->fop = (u16) ((u32) env->fcs >> 16);
337#ifdef CONFIG_X86_64
338 fxsave->rip = env->fip;
339 fxsave->rdp = env->foo;
340 /* cs and ds ignored */
341#else
342 fxsave->fip = env->fip;
343 fxsave->fcs = (env->fcs & 0xffff);
344 fxsave->foo = env->foo;
345 fxsave->fos = env->fos;
346#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Roland McGrath44210112008-01-30 13:31:50 +0100348 for (i = 0; i < 8; ++i)
349 memcpy(&to[i], &from[i], sizeof(from[0]));
350}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Roland McGrath44210112008-01-30 13:31:50 +0100352int fpregs_get(struct task_struct *target, const struct user_regset *regset,
353 unsigned int pos, unsigned int count,
354 void *kbuf, void __user *ubuf)
355{
356 struct user_i387_ia32_struct env;
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700357 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700359 ret = init_fpu(target);
360 if (ret)
361 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100362
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700363 if (!HAVE_HWFP)
364 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
365
Ingo Molnarf6689642008-03-05 15:37:32 +0100366 if (!cpu_has_fxsr) {
Roland McGrath44210112008-01-30 13:31:50 +0100367 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700368 &target->thread.xstate->fsave, 0,
369 -1);
Ingo Molnarf6689642008-03-05 15:37:32 +0100370 }
Roland McGrath44210112008-01-30 13:31:50 +0100371
372 if (kbuf && pos == 0 && count == sizeof(env)) {
373 convert_from_fxsr(kbuf, target);
374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
Roland McGrath44210112008-01-30 13:31:50 +0100376
377 convert_from_fxsr(&env, target);
Ingo Molnarf6689642008-03-05 15:37:32 +0100378
Roland McGrath44210112008-01-30 13:31:50 +0100379 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
380}
381
382int fpregs_set(struct task_struct *target, const struct user_regset *regset,
383 unsigned int pos, unsigned int count,
384 const void *kbuf, const void __user *ubuf)
385{
386 struct user_i387_ia32_struct env;
387 int ret;
388
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700389 ret = init_fpu(target);
390 if (ret)
391 return ret;
392
Roland McGrath44210112008-01-30 13:31:50 +0100393 set_stopped_child_used_math(target);
394
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700395 if (!HAVE_HWFP)
396 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
397
Ingo Molnarf6689642008-03-05 15:37:32 +0100398 if (!cpu_has_fxsr) {
Roland McGrath44210112008-01-30 13:31:50 +0100399 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700400 &target->thread.xstate->fsave, 0, -1);
Ingo Molnarf6689642008-03-05 15:37:32 +0100401 }
Roland McGrath44210112008-01-30 13:31:50 +0100402
403 if (pos > 0 || count < sizeof(env))
404 convert_from_fxsr(&env, target);
405
406 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
407 if (!ret)
408 convert_to_fxsr(target, &env);
409
410 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
413/*
414 * Signal frame handlers.
415 */
416
Roland McGrath44210112008-01-30 13:31:50 +0100417static inline int save_i387_fsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 struct task_struct *tsk = current;
Suresh Siddha61c46282008-03-10 15:28:04 -0700420 struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100422 unlazy_fpu(tsk);
Suresh Siddha61c46282008-03-10 15:28:04 -0700423 fp->status = fp->swd;
424 if (__copy_to_user(buf, fp, sizeof(struct i387_fsave_struct)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return -1;
426 return 1;
427}
428
Roland McGrath44210112008-01-30 13:31:50 +0100429static int save_i387_fxsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 struct task_struct *tsk = current;
Suresh Siddha61c46282008-03-10 15:28:04 -0700432 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100433 struct user_i387_ia32_struct env;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 int err = 0;
435
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100436 unlazy_fpu(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Roland McGrath44210112008-01-30 13:31:50 +0100438 convert_from_fxsr(&env, tsk);
439 if (__copy_to_user(buf, &env, sizeof(env)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return -1;
441
Suresh Siddha61c46282008-03-10 15:28:04 -0700442 err |= __put_user(fx->swd, &buf->status);
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100443 err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
444 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return -1;
446
Suresh Siddha61c46282008-03-10 15:28:04 -0700447 if (__copy_to_user(&buf->_fxsr_env[0], fx,
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100448 sizeof(struct i387_fxsave_struct)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return -1;
450 return 1;
451}
452
Roland McGrath44210112008-01-30 13:31:50 +0100453int save_i387_ia32(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100455 if (!used_math())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return 0;
Ingo Molnarf6689642008-03-05 15:37:32 +0100457 /*
458 * This will cause a "finit" to be triggered by the next
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 * attempted FPU operation by the 'current' process.
460 */
461 clear_used_math();
462
Ingo Molnarf6689642008-03-05 15:37:32 +0100463 if (!HAVE_HWFP) {
Roland McGrath44210112008-01-30 13:31:50 +0100464 return fpregs_soft_get(current, NULL,
465 0, sizeof(struct user_i387_ia32_struct),
466 NULL, buf) ? -1 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
Ingo Molnarf6689642008-03-05 15:37:32 +0100468
469 if (cpu_has_fxsr)
470 return save_i387_fxsave(buf);
471 else
472 return save_i387_fsave(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Roland McGrath44210112008-01-30 13:31:50 +0100475static inline int restore_i387_fsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 struct task_struct *tsk = current;
Ingo Molnarf6689642008-03-05 15:37:32 +0100478
Suresh Siddha61c46282008-03-10 15:28:04 -0700479 return __copy_from_user(&tsk->thread.xstate->fsave, buf,
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100480 sizeof(struct i387_fsave_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Roland McGrath44210112008-01-30 13:31:50 +0100483static int restore_i387_fxsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 struct task_struct *tsk = current;
Roland McGrath44210112008-01-30 13:31:50 +0100486 struct user_i387_ia32_struct env;
Ingo Molnarf6689642008-03-05 15:37:32 +0100487 int err;
488
Suresh Siddha61c46282008-03-10 15:28:04 -0700489 err = __copy_from_user(&tsk->thread.xstate->fxsave, &buf->_fxsr_env[0],
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100490 sizeof(struct i387_fxsave_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /* mxcsr reserved bits must be masked to zero for security reasons */
Suresh Siddha61c46282008-03-10 15:28:04 -0700492 tsk->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
Roland McGrath44210112008-01-30 13:31:50 +0100493 if (err || __copy_from_user(&env, buf, sizeof(env)))
494 return 1;
495 convert_to_fxsr(tsk, &env);
Ingo Molnarf6689642008-03-05 15:37:32 +0100496
Roland McGrath44210112008-01-30 13:31:50 +0100497 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Roland McGrath44210112008-01-30 13:31:50 +0100500int restore_i387_ia32(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 int err;
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700503 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700505 if (HAVE_HWFP)
Suresh Siddhafd3c3ed2008-05-07 12:09:52 -0700506 clear_fpu(tsk);
507
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700508 if (!used_math()) {
509 err = init_fpu(tsk);
510 if (err)
511 return err;
512 }
Suresh Siddhafd3c3ed2008-05-07 12:09:52 -0700513
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700514 if (HAVE_HWFP) {
Ingo Molnarf6689642008-03-05 15:37:32 +0100515 if (cpu_has_fxsr)
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100516 err = restore_i387_fxsave(buf);
Ingo Molnarf6689642008-03-05 15:37:32 +0100517 else
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100518 err = restore_i387_fsave(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 } else {
Roland McGrath44210112008-01-30 13:31:50 +0100520 err = fpregs_soft_set(current, NULL,
521 0, sizeof(struct user_i387_ia32_struct),
522 NULL, buf) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524 set_used_math();
Ingo Molnarf6689642008-03-05 15:37:32 +0100525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return err;
527}
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529/*
530 * FPU state for core dumps.
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100531 * This is only used for a.out dumps now.
532 * It is declared generically using elf_fpregset_t (which is
533 * struct user_i387_struct) but is in fact only used for 32-bit
534 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100536int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct task_struct *tsk = current;
Ingo Molnarf6689642008-03-05 15:37:32 +0100539 int fpvalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 fpvalid = !!used_math();
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100542 if (fpvalid)
543 fpvalid = !fpregs_get(tsk, NULL,
544 0, sizeof(struct user_i387_ia32_struct),
545 fpu, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 return fpvalid;
548}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700549EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100551#endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */