blob: 7daf3a011dd94dda358e3abd68f9038e8fbe1d16 [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
Suresh Siddhaab513702008-07-29 10:29:22 -070024# define save_i387_xstate_ia32 save_i387_xstate
25# define restore_i387_xstate_ia32 restore_i387_xstate
Ingo Molnarf6689642008-03-05 15:37:32 +010026# define _fpstate_ia32 _fpstate
Suresh Siddhaab513702008-07-29 10:29:22 -070027# define _xstate_ia32 _xstate
Suresh Siddha3c1c7f12008-07-29 10:29:21 -070028# define sig_xstate_ia32_size sig_xstate_size
Ingo Molnarf6689642008-03-05 15:37:32 +010029# define user_i387_ia32_struct user_i387_struct
30# define user32_fxsr_struct user_fxsr_struct
Roland McGrath44210112008-01-30 13:31:50 +010031#endif
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#ifdef CONFIG_MATH_EMULATION
Ingo Molnarf6689642008-03-05 15:37:32 +010034# define HAVE_HWFP (boot_cpu_data.hard_math)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#else
Ingo Molnarf6689642008-03-05 15:37:32 +010036# define HAVE_HWFP 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#endif
38
Ingo Molnarf6689642008-03-05 15:37:32 +010039static unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
Suresh Siddha61c46282008-03-10 15:28:04 -070040unsigned int xstate_size;
Suresh Siddha3c1c7f12008-07-29 10:29:21 -070041unsigned int sig_xstate_ia32_size = sizeof(struct _fpstate_ia32);
Suresh Siddha61c46282008-03-10 15:28:04 -070042static struct i387_fxsave_struct fx_scratch __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Suresh Siddha61c46282008-03-10 15:28:04 -070044void __cpuinit mxcsr_feature_mask_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 unsigned long mask = 0;
Ingo Molnarf6689642008-03-05 15:37:32 +010047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 clts();
49 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -070050 memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct));
51 asm volatile("fxsave %0" : : "m" (fx_scratch));
52 mask = fx_scratch.mxcsr_mask;
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +010053 if (mask == 0)
54 mask = 0x0000ffbf;
55 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 mxcsr_feature_mask &= mask;
57 stts();
58}
59
Suresh Siddha61c46282008-03-10 15:28:04 -070060void __init init_thread_xstate(void)
61{
Suresh Siddhae8a496a2008-05-23 16:26:37 -070062 if (!HAVE_HWFP) {
63 xstate_size = sizeof(struct i387_soft_struct);
64 return;
65 }
66
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070067 if (cpu_has_xsave) {
68 xsave_cntxt_init();
69 return;
70 }
71
Suresh Siddha61c46282008-03-10 15:28:04 -070072 if (cpu_has_fxsr)
73 xstate_size = sizeof(struct i387_fxsave_struct);
74#ifdef CONFIG_X86_32
75 else
76 xstate_size = sizeof(struct i387_fsave_struct);
77#endif
Suresh Siddha61c46282008-03-10 15:28:04 -070078}
79
Roland McGrath44210112008-01-30 13:31:50 +010080#ifdef CONFIG_X86_64
81/*
82 * Called at bootup to set up the initial FPU state that is later cloned
83 * into all processes.
84 */
85void __cpuinit fpu_init(void)
86{
87 unsigned long oldcr0 = read_cr0();
Ingo Molnarf6689642008-03-05 15:37:32 +010088
Roland McGrath44210112008-01-30 13:31:50 +010089 set_in_cr4(X86_CR4_OSFXSR);
90 set_in_cr4(X86_CR4_OSXMMEXCPT);
91
Ingo Molnarf6689642008-03-05 15:37:32 +010092 write_cr0(oldcr0 & ~(X86_CR0_TS|X86_CR0_EM)); /* clear TS and EM */
Roland McGrath44210112008-01-30 13:31:50 +010093
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070094 /*
95 * Boot processor to setup the FP and extended state context info.
96 */
97 if (!smp_processor_id())
98 init_thread_xstate();
99 xsave_init();
100
Roland McGrath44210112008-01-30 13:31:50 +0100101 mxcsr_feature_mask_init();
102 /* clean state in init */
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700103 if (cpu_has_xsave)
104 current_thread_info()->status = TS_XSAVE;
105 else
106 current_thread_info()->status = 0;
Roland McGrath44210112008-01-30 13:31:50 +0100107 clear_used_math();
108}
109#endif /* CONFIG_X86_64 */
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/*
112 * The _current_ task is using the FPU for the first time
113 * so initialize it and set the mxcsr to its default
114 * value at reset if we support XMM instructions and then
115 * remeber the current task has used the FPU.
116 */
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700117int init_fpu(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Roland McGrath44210112008-01-30 13:31:50 +0100119 if (tsk_used_math(tsk)) {
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700120 if (HAVE_HWFP && tsk == current)
Roland McGrath44210112008-01-30 13:31:50 +0100121 unlazy_fpu(tsk);
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700122 return 0;
123 }
124
125 /*
126 * Memory allocation at the first usage of the FPU and other state.
127 */
128 if (!tsk->thread.xstate) {
129 tsk->thread.xstate = kmem_cache_alloc(task_xstate_cachep,
130 GFP_KERNEL);
131 if (!tsk->thread.xstate)
132 return -ENOMEM;
Roland McGrath44210112008-01-30 13:31:50 +0100133 }
134
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700135#ifdef CONFIG_X86_32
136 if (!HAVE_HWFP) {
137 memset(tsk->thread.xstate, 0, xstate_size);
138 finit();
139 set_stopped_child_used_math(tsk);
140 return 0;
141 }
142#endif
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700145 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
146
147 memset(fx, 0, xstate_size);
148 fx->cwd = 0x37f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (cpu_has_xmm)
Suresh Siddha61c46282008-03-10 15:28:04 -0700150 fx->mxcsr = MXCSR_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 } else {
Suresh Siddha61c46282008-03-10 15:28:04 -0700152 struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
153 memset(fp, 0, xstate_size);
154 fp->cwd = 0xffff037fu;
155 fp->swd = 0xffff0000u;
156 fp->twd = 0xffffffffu;
157 fp->fos = 0xffff0000u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
Roland McGrath44210112008-01-30 13:31:50 +0100159 /*
160 * Only the device not available exception or ptrace can call init_fpu.
161 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 set_stopped_child_used_math(tsk);
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700163 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Roland McGrath44210112008-01-30 13:31:50 +0100166int fpregs_active(struct task_struct *target, const struct user_regset *regset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Roland McGrath44210112008-01-30 13:31:50 +0100168 return tsk_used_math(target) ? regset->n : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
Roland McGrath44210112008-01-30 13:31:50 +0100170
171int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
172{
173 return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
174}
175
176int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
177 unsigned int pos, unsigned int count,
178 void *kbuf, void __user *ubuf)
179{
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700180 int ret;
181
Roland McGrath44210112008-01-30 13:31:50 +0100182 if (!cpu_has_fxsr)
183 return -ENODEV;
184
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700185 ret = init_fpu(target);
186 if (ret)
187 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100188
189 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700190 &target->thread.xstate->fxsave, 0, -1);
Roland McGrath44210112008-01-30 13:31:50 +0100191}
192
193int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
194 unsigned int pos, unsigned int count,
195 const void *kbuf, const void __user *ubuf)
196{
197 int ret;
198
199 if (!cpu_has_fxsr)
200 return -ENODEV;
201
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700202 ret = init_fpu(target);
203 if (ret)
204 return ret;
205
Roland McGrath44210112008-01-30 13:31:50 +0100206 set_stopped_child_used_math(target);
207
208 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700209 &target->thread.xstate->fxsave, 0, -1);
Roland McGrath44210112008-01-30 13:31:50 +0100210
211 /*
212 * mxcsr reserved bits must be masked to zero for security reasons.
213 */
Suresh Siddha61c46282008-03-10 15:28:04 -0700214 target->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
Roland McGrath44210112008-01-30 13:31:50 +0100215
216 return ret;
217}
218
219#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/*
222 * FPU tag word conversions.
223 */
224
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100225static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100230 tmp = ~twd;
Roland McGrath44210112008-01-30 13:31:50 +0100231 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100232 /* and move the valid bits to the lower byte. */
233 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
234 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
235 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
Ingo Molnarf6689642008-03-05 15:37:32 +0100236
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100237 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Roland McGrath44210112008-01-30 13:31:50 +0100240#define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
241#define FP_EXP_TAG_VALID 0
242#define FP_EXP_TAG_ZERO 1
243#define FP_EXP_TAG_SPECIAL 2
244#define FP_EXP_TAG_EMPTY 3
245
246static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Roland McGrath44210112008-01-30 13:31:50 +0100248 struct _fpxreg *st;
249 u32 tos = (fxsave->swd >> 11) & 7;
250 u32 twd = (unsigned long) fxsave->twd;
251 u32 tag;
252 u32 ret = 0xffff0000u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 int i;
254
Roland McGrath44210112008-01-30 13:31:50 +0100255 for (i = 0; i < 8; i++, twd >>= 1) {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100256 if (twd & 0x1) {
257 st = FPREG_ADDR(fxsave, (i - tos) & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100259 switch (st->exponent & 0x7fff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 case 0x7fff:
Roland McGrath44210112008-01-30 13:31:50 +0100261 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 break;
263 case 0x0000:
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100264 if (!st->significand[0] &&
265 !st->significand[1] &&
266 !st->significand[2] &&
Roland McGrath44210112008-01-30 13:31:50 +0100267 !st->significand[3])
268 tag = FP_EXP_TAG_ZERO;
269 else
270 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 break;
272 default:
Roland McGrath44210112008-01-30 13:31:50 +0100273 if (st->significand[3] & 0x8000)
274 tag = FP_EXP_TAG_VALID;
275 else
276 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 break;
278 }
279 } else {
Roland McGrath44210112008-01-30 13:31:50 +0100280 tag = FP_EXP_TAG_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
Roland McGrath44210112008-01-30 13:31:50 +0100282 ret |= tag << (2 * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284 return ret;
285}
286
287/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 * FXSR floating point environment conversions.
289 */
290
Ingo Molnarf6689642008-03-05 15:37:32 +0100291static void
292convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Suresh Siddha61c46282008-03-10 15:28:04 -0700294 struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100295 struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
296 struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 int i;
298
Roland McGrath44210112008-01-30 13:31:50 +0100299 env->cwd = fxsave->cwd | 0xffff0000u;
300 env->swd = fxsave->swd | 0xffff0000u;
301 env->twd = twd_fxsr_to_i387(fxsave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Roland McGrath44210112008-01-30 13:31:50 +0100303#ifdef CONFIG_X86_64
304 env->fip = fxsave->rip;
305 env->foo = fxsave->rdp;
306 if (tsk == current) {
307 /*
308 * should be actually ds/cs at fpu exception time, but
309 * that information is not available in 64bit mode.
310 */
Ingo Molnarf6689642008-03-05 15:37:32 +0100311 asm("mov %%ds, %[fos]" : [fos] "=r" (env->fos));
312 asm("mov %%cs, %[fcs]" : [fcs] "=r" (env->fcs));
Roland McGrath44210112008-01-30 13:31:50 +0100313 } else {
314 struct pt_regs *regs = task_pt_regs(tsk);
Ingo Molnarf6689642008-03-05 15:37:32 +0100315
Roland McGrath44210112008-01-30 13:31:50 +0100316 env->fos = 0xffff0000 | tsk->thread.ds;
317 env->fcs = regs->cs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
Roland McGrath44210112008-01-30 13:31:50 +0100319#else
320 env->fip = fxsave->fip;
Jan Beulich609b5292008-03-05 08:35:14 +0000321 env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
Roland McGrath44210112008-01-30 13:31:50 +0100322 env->foo = fxsave->foo;
323 env->fos = fxsave->fos;
324#endif
325
326 for (i = 0; i < 8; ++i)
327 memcpy(&to[i], &from[i], sizeof(to[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Roland McGrath44210112008-01-30 13:31:50 +0100330static void convert_to_fxsr(struct task_struct *tsk,
331 const struct user_i387_ia32_struct *env)
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Suresh Siddha61c46282008-03-10 15:28:04 -0700334 struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100335 struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
336 struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 int i;
338
Roland McGrath44210112008-01-30 13:31:50 +0100339 fxsave->cwd = env->cwd;
340 fxsave->swd = env->swd;
341 fxsave->twd = twd_i387_to_fxsr(env->twd);
342 fxsave->fop = (u16) ((u32) env->fcs >> 16);
343#ifdef CONFIG_X86_64
344 fxsave->rip = env->fip;
345 fxsave->rdp = env->foo;
346 /* cs and ds ignored */
347#else
348 fxsave->fip = env->fip;
349 fxsave->fcs = (env->fcs & 0xffff);
350 fxsave->foo = env->foo;
351 fxsave->fos = env->fos;
352#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Roland McGrath44210112008-01-30 13:31:50 +0100354 for (i = 0; i < 8; ++i)
355 memcpy(&to[i], &from[i], sizeof(from[0]));
356}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Roland McGrath44210112008-01-30 13:31:50 +0100358int fpregs_get(struct task_struct *target, const struct user_regset *regset,
359 unsigned int pos, unsigned int count,
360 void *kbuf, void __user *ubuf)
361{
362 struct user_i387_ia32_struct env;
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700363 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700365 ret = init_fpu(target);
366 if (ret)
367 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100368
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700369 if (!HAVE_HWFP)
370 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
371
Ingo Molnarf6689642008-03-05 15:37:32 +0100372 if (!cpu_has_fxsr) {
Roland McGrath44210112008-01-30 13:31:50 +0100373 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700374 &target->thread.xstate->fsave, 0,
375 -1);
Ingo Molnarf6689642008-03-05 15:37:32 +0100376 }
Roland McGrath44210112008-01-30 13:31:50 +0100377
378 if (kbuf && pos == 0 && count == sizeof(env)) {
379 convert_from_fxsr(kbuf, target);
380 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
Roland McGrath44210112008-01-30 13:31:50 +0100382
383 convert_from_fxsr(&env, target);
Ingo Molnarf6689642008-03-05 15:37:32 +0100384
Roland McGrath44210112008-01-30 13:31:50 +0100385 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
386}
387
388int fpregs_set(struct task_struct *target, const struct user_regset *regset,
389 unsigned int pos, unsigned int count,
390 const void *kbuf, const void __user *ubuf)
391{
392 struct user_i387_ia32_struct env;
393 int ret;
394
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700395 ret = init_fpu(target);
396 if (ret)
397 return ret;
398
Roland McGrath44210112008-01-30 13:31:50 +0100399 set_stopped_child_used_math(target);
400
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700401 if (!HAVE_HWFP)
402 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
403
Ingo Molnarf6689642008-03-05 15:37:32 +0100404 if (!cpu_has_fxsr) {
Roland McGrath44210112008-01-30 13:31:50 +0100405 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700406 &target->thread.xstate->fsave, 0, -1);
Ingo Molnarf6689642008-03-05 15:37:32 +0100407 }
Roland McGrath44210112008-01-30 13:31:50 +0100408
409 if (pos > 0 || count < sizeof(env))
410 convert_from_fxsr(&env, target);
411
412 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
413 if (!ret)
414 convert_to_fxsr(target, &env);
415
416 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419/*
420 * Signal frame handlers.
421 */
422
Roland McGrath44210112008-01-30 13:31:50 +0100423static inline int save_i387_fsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct task_struct *tsk = current;
Suresh Siddha61c46282008-03-10 15:28:04 -0700426 struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
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
Roland McGrath44210112008-01-30 13:31:50 +0100441 convert_from_fxsr(&env, tsk);
442 if (__copy_to_user(buf, &env, sizeof(env)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return -1;
444
Suresh Siddha61c46282008-03-10 15:28:04 -0700445 err |= __put_user(fx->swd, &buf->status);
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100446 err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
447 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return -1;
449
Suresh Siddha61c46282008-03-10 15:28:04 -0700450 if (__copy_to_user(&buf->_fxsr_env[0], fx,
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100451 sizeof(struct i387_fxsave_struct)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return -1;
453 return 1;
454}
455
Suresh Siddhaab513702008-07-29 10:29:22 -0700456int save_i387_xstate_ia32(void __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Suresh Siddhaab513702008-07-29 10:29:22 -0700458 struct _fpstate_ia32 __user *fp = (struct _fpstate_ia32 __user *) buf;
459 struct task_struct *tsk = current;
460
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100461 if (!used_math())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700463
464 if (!access_ok(VERIFY_WRITE, buf, sig_xstate_ia32_size))
465 return -EACCES;
Ingo Molnarf6689642008-03-05 15:37:32 +0100466 /*
467 * This will cause a "finit" to be triggered by the next
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 * attempted FPU operation by the 'current' process.
469 */
470 clear_used_math();
471
Ingo Molnarf6689642008-03-05 15:37:32 +0100472 if (!HAVE_HWFP) {
Roland McGrath44210112008-01-30 13:31:50 +0100473 return fpregs_soft_get(current, NULL,
474 0, sizeof(struct user_i387_ia32_struct),
Suresh Siddhaab513702008-07-29 10:29:22 -0700475 NULL, fp) ? -1 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
Ingo Molnarf6689642008-03-05 15:37:32 +0100477
Suresh Siddhaab513702008-07-29 10:29:22 -0700478 unlazy_fpu(tsk);
479
Ingo Molnarf6689642008-03-05 15:37:32 +0100480 if (cpu_has_fxsr)
Suresh Siddhaab513702008-07-29 10:29:22 -0700481 return save_i387_fxsave(fp);
Ingo Molnarf6689642008-03-05 15:37:32 +0100482 else
Suresh Siddhaab513702008-07-29 10:29:22 -0700483 return save_i387_fsave(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Roland McGrath44210112008-01-30 13:31:50 +0100486static inline int restore_i387_fsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 struct task_struct *tsk = current;
Ingo Molnarf6689642008-03-05 15:37:32 +0100489
Suresh Siddha61c46282008-03-10 15:28:04 -0700490 return __copy_from_user(&tsk->thread.xstate->fsave, buf,
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100491 sizeof(struct i387_fsave_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
Roland McGrath44210112008-01-30 13:31:50 +0100494static int restore_i387_fxsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 struct task_struct *tsk = current;
Roland McGrath44210112008-01-30 13:31:50 +0100497 struct user_i387_ia32_struct env;
Ingo Molnarf6689642008-03-05 15:37:32 +0100498 int err;
499
Suresh Siddha61c46282008-03-10 15:28:04 -0700500 err = __copy_from_user(&tsk->thread.xstate->fxsave, &buf->_fxsr_env[0],
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100501 sizeof(struct i387_fxsave_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /* mxcsr reserved bits must be masked to zero for security reasons */
Suresh Siddha61c46282008-03-10 15:28:04 -0700503 tsk->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
Roland McGrath44210112008-01-30 13:31:50 +0100504 if (err || __copy_from_user(&env, buf, sizeof(env)))
505 return 1;
506 convert_to_fxsr(tsk, &env);
Ingo Molnarf6689642008-03-05 15:37:32 +0100507
Roland McGrath44210112008-01-30 13:31:50 +0100508 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
Suresh Siddhaab513702008-07-29 10:29:22 -0700511int restore_i387_xstate_ia32(void __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 int err;
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700514 struct task_struct *tsk = current;
Suresh Siddhaab513702008-07-29 10:29:22 -0700515 struct _fpstate_ia32 __user *fp = (struct _fpstate_ia32 __user *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700517 if (HAVE_HWFP)
Suresh Siddhafd3c3ed2008-05-07 12:09:52 -0700518 clear_fpu(tsk);
519
Suresh Siddhaab513702008-07-29 10:29:22 -0700520 if (!buf) {
521 if (used_math()) {
522 clear_fpu(tsk);
523 clear_used_math();
524 }
525
526 return 0;
527 } else
528 if (!access_ok(VERIFY_READ, buf, sig_xstate_ia32_size))
529 return -EACCES;
530
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700531 if (!used_math()) {
532 err = init_fpu(tsk);
533 if (err)
534 return err;
535 }
Suresh Siddhafd3c3ed2008-05-07 12:09:52 -0700536
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700537 if (HAVE_HWFP) {
Ingo Molnarf6689642008-03-05 15:37:32 +0100538 if (cpu_has_fxsr)
Suresh Siddhaab513702008-07-29 10:29:22 -0700539 err = restore_i387_fxsave(fp);
Ingo Molnarf6689642008-03-05 15:37:32 +0100540 else
Suresh Siddhaab513702008-07-29 10:29:22 -0700541 err = restore_i387_fsave(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 } else {
Roland McGrath44210112008-01-30 13:31:50 +0100543 err = fpregs_soft_set(current, NULL,
544 0, sizeof(struct user_i387_ia32_struct),
Suresh Siddhaab513702008-07-29 10:29:22 -0700545 NULL, fp) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547 set_used_math();
Ingo Molnarf6689642008-03-05 15:37:32 +0100548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return err;
550}
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552/*
553 * FPU state for core dumps.
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100554 * This is only used for a.out dumps now.
555 * It is declared generically using elf_fpregset_t (which is
556 * struct user_i387_struct) but is in fact only used for 32-bit
557 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100559int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 struct task_struct *tsk = current;
Ingo Molnarf6689642008-03-05 15:37:32 +0100562 int fpvalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 fpvalid = !!used_math();
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100565 if (fpvalid)
566 fpvalid = !fpregs_get(tsk, NULL,
567 0, sizeof(struct user_i387_ia32_struct),
568 fpu, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 return fpvalid;
571}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700572EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100574#endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */