blob: baf632b221d43366b1f026b28add587e249e5840 [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>
Suresh Siddha61c46282008-03-10 15:28:04 -070011#include <linux/bootmem.h>
Ingo Molnarf6689642008-03-05 15:37:32 +010012
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/sigcontext.h>
Ingo Molnarf6689642008-03-05 15:37:32 +010014#include <asm/processor.h>
15#include <asm/math_emu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/uaccess.h>
Ingo Molnarf6689642008-03-05 15:37:32 +010017#include <asm/ptrace.h>
18#include <asm/i387.h>
19#include <asm/user.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Roland McGrath44210112008-01-30 13:31:50 +010021#ifdef CONFIG_X86_64
Ingo Molnarf6689642008-03-05 15:37:32 +010022# include <asm/sigcontext32.h>
23# include <asm/user32.h>
Roland McGrath44210112008-01-30 13:31:50 +010024#else
Ingo Molnarf6689642008-03-05 15:37:32 +010025# define save_i387_ia32 save_i387
26# define restore_i387_ia32 restore_i387
27# define _fpstate_ia32 _fpstate
28# 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;
40static struct i387_fxsave_struct fx_scratch __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Suresh Siddha61c46282008-03-10 15:28:04 -070042void __cpuinit mxcsr_feature_mask_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 unsigned long mask = 0;
Ingo Molnarf6689642008-03-05 15:37:32 +010045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 clts();
47 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -070048 memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct));
49 asm volatile("fxsave %0" : : "m" (fx_scratch));
50 mask = fx_scratch.mxcsr_mask;
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +010051 if (mask == 0)
52 mask = 0x0000ffbf;
53 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 mxcsr_feature_mask &= mask;
55 stts();
56}
57
Suresh Siddha61c46282008-03-10 15:28:04 -070058void __init init_thread_xstate(void)
59{
60 if (cpu_has_fxsr)
61 xstate_size = sizeof(struct i387_fxsave_struct);
62#ifdef CONFIG_X86_32
63 else
64 xstate_size = sizeof(struct i387_fsave_struct);
65#endif
66 init_task.thread.xstate = alloc_bootmem(xstate_size);
67}
68
Roland McGrath44210112008-01-30 13:31:50 +010069#ifdef CONFIG_X86_64
70/*
71 * Called at bootup to set up the initial FPU state that is later cloned
72 * into all processes.
73 */
74void __cpuinit fpu_init(void)
75{
76 unsigned long oldcr0 = read_cr0();
Ingo Molnarf6689642008-03-05 15:37:32 +010077
Roland McGrath44210112008-01-30 13:31:50 +010078 set_in_cr4(X86_CR4_OSFXSR);
79 set_in_cr4(X86_CR4_OSXMMEXCPT);
80
Ingo Molnarf6689642008-03-05 15:37:32 +010081 write_cr0(oldcr0 & ~(X86_CR0_TS|X86_CR0_EM)); /* clear TS and EM */
Roland McGrath44210112008-01-30 13:31:50 +010082
83 mxcsr_feature_mask_init();
84 /* clean state in init */
85 current_thread_info()->status = 0;
86 clear_used_math();
87}
88#endif /* CONFIG_X86_64 */
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/*
91 * The _current_ task is using the FPU for the first time
92 * so initialize it and set the mxcsr to its default
93 * value at reset if we support XMM instructions and then
94 * remeber the current task has used the FPU.
95 */
96void init_fpu(struct task_struct *tsk)
97{
Roland McGrath44210112008-01-30 13:31:50 +010098 if (tsk_used_math(tsk)) {
99 if (tsk == current)
100 unlazy_fpu(tsk);
101 return;
102 }
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700105 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
106
107 memset(fx, 0, xstate_size);
108 fx->cwd = 0x37f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (cpu_has_xmm)
Suresh Siddha61c46282008-03-10 15:28:04 -0700110 fx->mxcsr = MXCSR_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 } else {
Suresh Siddha61c46282008-03-10 15:28:04 -0700112 struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
113 memset(fp, 0, xstate_size);
114 fp->cwd = 0xffff037fu;
115 fp->swd = 0xffff0000u;
116 fp->twd = 0xffffffffu;
117 fp->fos = 0xffff0000u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
Roland McGrath44210112008-01-30 13:31:50 +0100119 /*
120 * Only the device not available exception or ptrace can call init_fpu.
121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 set_stopped_child_used_math(tsk);
123}
124
Roland McGrath44210112008-01-30 13:31:50 +0100125int fpregs_active(struct task_struct *target, const struct user_regset *regset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Roland McGrath44210112008-01-30 13:31:50 +0100127 return tsk_used_math(target) ? regset->n : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
Roland McGrath44210112008-01-30 13:31:50 +0100129
130int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
131{
132 return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
133}
134
135int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
136 unsigned int pos, unsigned int count,
137 void *kbuf, void __user *ubuf)
138{
139 if (!cpu_has_fxsr)
140 return -ENODEV;
141
Suresh Siddha18a86222008-03-03 13:01:08 -0800142 init_fpu(target);
Roland McGrath44210112008-01-30 13:31:50 +0100143
144 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700145 &target->thread.xstate->fxsave, 0, -1);
Roland McGrath44210112008-01-30 13:31:50 +0100146}
147
148int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
149 unsigned int pos, unsigned int count,
150 const void *kbuf, const void __user *ubuf)
151{
152 int ret;
153
154 if (!cpu_has_fxsr)
155 return -ENODEV;
156
Suresh Siddha18a86222008-03-03 13:01:08 -0800157 init_fpu(target);
Roland McGrath44210112008-01-30 13:31:50 +0100158 set_stopped_child_used_math(target);
159
160 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700161 &target->thread.xstate->fxsave, 0, -1);
Roland McGrath44210112008-01-30 13:31:50 +0100162
163 /*
164 * mxcsr reserved bits must be masked to zero for security reasons.
165 */
Suresh Siddha61c46282008-03-10 15:28:04 -0700166 target->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
Roland McGrath44210112008-01-30 13:31:50 +0100167
168 return ret;
169}
170
171#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/*
174 * FPU tag word conversions.
175 */
176
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100177static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100182 tmp = ~twd;
Roland McGrath44210112008-01-30 13:31:50 +0100183 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100184 /* and move the valid bits to the lower byte. */
185 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
186 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
187 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
Ingo Molnarf6689642008-03-05 15:37:32 +0100188
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100189 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Roland McGrath44210112008-01-30 13:31:50 +0100192#define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
193#define FP_EXP_TAG_VALID 0
194#define FP_EXP_TAG_ZERO 1
195#define FP_EXP_TAG_SPECIAL 2
196#define FP_EXP_TAG_EMPTY 3
197
198static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Roland McGrath44210112008-01-30 13:31:50 +0100200 struct _fpxreg *st;
201 u32 tos = (fxsave->swd >> 11) & 7;
202 u32 twd = (unsigned long) fxsave->twd;
203 u32 tag;
204 u32 ret = 0xffff0000u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 int i;
206
Roland McGrath44210112008-01-30 13:31:50 +0100207 for (i = 0; i < 8; i++, twd >>= 1) {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100208 if (twd & 0x1) {
209 st = FPREG_ADDR(fxsave, (i - tos) & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100211 switch (st->exponent & 0x7fff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 case 0x7fff:
Roland McGrath44210112008-01-30 13:31:50 +0100213 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 break;
215 case 0x0000:
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100216 if (!st->significand[0] &&
217 !st->significand[1] &&
218 !st->significand[2] &&
Roland McGrath44210112008-01-30 13:31:50 +0100219 !st->significand[3])
220 tag = FP_EXP_TAG_ZERO;
221 else
222 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 break;
224 default:
Roland McGrath44210112008-01-30 13:31:50 +0100225 if (st->significand[3] & 0x8000)
226 tag = FP_EXP_TAG_VALID;
227 else
228 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 break;
230 }
231 } else {
Roland McGrath44210112008-01-30 13:31:50 +0100232 tag = FP_EXP_TAG_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
Roland McGrath44210112008-01-30 13:31:50 +0100234 ret |= tag << (2 * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236 return ret;
237}
238
239/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 * FXSR floating point environment conversions.
241 */
242
Ingo Molnarf6689642008-03-05 15:37:32 +0100243static void
244convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Suresh Siddha61c46282008-03-10 15:28:04 -0700246 struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100247 struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
248 struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 int i;
250
Roland McGrath44210112008-01-30 13:31:50 +0100251 env->cwd = fxsave->cwd | 0xffff0000u;
252 env->swd = fxsave->swd | 0xffff0000u;
253 env->twd = twd_fxsr_to_i387(fxsave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Roland McGrath44210112008-01-30 13:31:50 +0100255#ifdef CONFIG_X86_64
256 env->fip = fxsave->rip;
257 env->foo = fxsave->rdp;
258 if (tsk == current) {
259 /*
260 * should be actually ds/cs at fpu exception time, but
261 * that information is not available in 64bit mode.
262 */
Ingo Molnarf6689642008-03-05 15:37:32 +0100263 asm("mov %%ds, %[fos]" : [fos] "=r" (env->fos));
264 asm("mov %%cs, %[fcs]" : [fcs] "=r" (env->fcs));
Roland McGrath44210112008-01-30 13:31:50 +0100265 } else {
266 struct pt_regs *regs = task_pt_regs(tsk);
Ingo Molnarf6689642008-03-05 15:37:32 +0100267
Roland McGrath44210112008-01-30 13:31:50 +0100268 env->fos = 0xffff0000 | tsk->thread.ds;
269 env->fcs = regs->cs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
Roland McGrath44210112008-01-30 13:31:50 +0100271#else
272 env->fip = fxsave->fip;
Jan Beulich609b5292008-03-05 08:35:14 +0000273 env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
Roland McGrath44210112008-01-30 13:31:50 +0100274 env->foo = fxsave->foo;
275 env->fos = fxsave->fos;
276#endif
277
278 for (i = 0; i < 8; ++i)
279 memcpy(&to[i], &from[i], sizeof(to[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
Roland McGrath44210112008-01-30 13:31:50 +0100282static void convert_to_fxsr(struct task_struct *tsk,
283 const struct user_i387_ia32_struct *env)
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Suresh Siddha61c46282008-03-10 15:28:04 -0700286 struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100287 struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
288 struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 int i;
290
Roland McGrath44210112008-01-30 13:31:50 +0100291 fxsave->cwd = env->cwd;
292 fxsave->swd = env->swd;
293 fxsave->twd = twd_i387_to_fxsr(env->twd);
294 fxsave->fop = (u16) ((u32) env->fcs >> 16);
295#ifdef CONFIG_X86_64
296 fxsave->rip = env->fip;
297 fxsave->rdp = env->foo;
298 /* cs and ds ignored */
299#else
300 fxsave->fip = env->fip;
301 fxsave->fcs = (env->fcs & 0xffff);
302 fxsave->foo = env->foo;
303 fxsave->fos = env->fos;
304#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Roland McGrath44210112008-01-30 13:31:50 +0100306 for (i = 0; i < 8; ++i)
307 memcpy(&to[i], &from[i], sizeof(from[0]));
308}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Roland McGrath44210112008-01-30 13:31:50 +0100310int fpregs_get(struct task_struct *target, const struct user_regset *regset,
311 unsigned int pos, unsigned int count,
312 void *kbuf, void __user *ubuf)
313{
314 struct user_i387_ia32_struct env;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Roland McGrath44210112008-01-30 13:31:50 +0100316 if (!HAVE_HWFP)
317 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
318
Suresh Siddha18a86222008-03-03 13:01:08 -0800319 init_fpu(target);
Roland McGrath44210112008-01-30 13:31:50 +0100320
Ingo Molnarf6689642008-03-05 15:37:32 +0100321 if (!cpu_has_fxsr) {
Roland McGrath44210112008-01-30 13:31:50 +0100322 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700323 &target->thread.xstate->fsave, 0,
324 -1);
Ingo Molnarf6689642008-03-05 15:37:32 +0100325 }
Roland McGrath44210112008-01-30 13:31:50 +0100326
327 if (kbuf && pos == 0 && count == sizeof(env)) {
328 convert_from_fxsr(kbuf, target);
329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Roland McGrath44210112008-01-30 13:31:50 +0100331
332 convert_from_fxsr(&env, target);
Ingo Molnarf6689642008-03-05 15:37:32 +0100333
Roland McGrath44210112008-01-30 13:31:50 +0100334 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
335}
336
337int fpregs_set(struct task_struct *target, const struct user_regset *regset,
338 unsigned int pos, unsigned int count,
339 const void *kbuf, const void __user *ubuf)
340{
341 struct user_i387_ia32_struct env;
342 int ret;
343
344 if (!HAVE_HWFP)
345 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
346
Suresh Siddha18a86222008-03-03 13:01:08 -0800347 init_fpu(target);
Roland McGrath44210112008-01-30 13:31:50 +0100348 set_stopped_child_used_math(target);
349
Ingo Molnarf6689642008-03-05 15:37:32 +0100350 if (!cpu_has_fxsr) {
Roland McGrath44210112008-01-30 13:31:50 +0100351 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Suresh Siddha61c46282008-03-10 15:28:04 -0700352 &target->thread.xstate->fsave, 0, -1);
Ingo Molnarf6689642008-03-05 15:37:32 +0100353 }
Roland McGrath44210112008-01-30 13:31:50 +0100354
355 if (pos > 0 || count < sizeof(env))
356 convert_from_fxsr(&env, target);
357
358 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
359 if (!ret)
360 convert_to_fxsr(target, &env);
361
362 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365/*
366 * Signal frame handlers.
367 */
368
Roland McGrath44210112008-01-30 13:31:50 +0100369static inline int save_i387_fsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 struct task_struct *tsk = current;
Suresh Siddha61c46282008-03-10 15:28:04 -0700372 struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100374 unlazy_fpu(tsk);
Suresh Siddha61c46282008-03-10 15:28:04 -0700375 fp->status = fp->swd;
376 if (__copy_to_user(buf, fp, sizeof(struct i387_fsave_struct)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return -1;
378 return 1;
379}
380
Roland McGrath44210112008-01-30 13:31:50 +0100381static int save_i387_fxsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 struct task_struct *tsk = current;
Suresh Siddha61c46282008-03-10 15:28:04 -0700384 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100385 struct user_i387_ia32_struct env;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 int err = 0;
387
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100388 unlazy_fpu(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Roland McGrath44210112008-01-30 13:31:50 +0100390 convert_from_fxsr(&env, tsk);
391 if (__copy_to_user(buf, &env, sizeof(env)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return -1;
393
Suresh Siddha61c46282008-03-10 15:28:04 -0700394 err |= __put_user(fx->swd, &buf->status);
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100395 err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
396 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return -1;
398
Suresh Siddha61c46282008-03-10 15:28:04 -0700399 if (__copy_to_user(&buf->_fxsr_env[0], fx,
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100400 sizeof(struct i387_fxsave_struct)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return -1;
402 return 1;
403}
404
Roland McGrath44210112008-01-30 13:31:50 +0100405int save_i387_ia32(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100407 if (!used_math())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return 0;
Ingo Molnarf6689642008-03-05 15:37:32 +0100409 /*
410 * This will cause a "finit" to be triggered by the next
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 * attempted FPU operation by the 'current' process.
412 */
413 clear_used_math();
414
Ingo Molnarf6689642008-03-05 15:37:32 +0100415 if (!HAVE_HWFP) {
Roland McGrath44210112008-01-30 13:31:50 +0100416 return fpregs_soft_get(current, NULL,
417 0, sizeof(struct user_i387_ia32_struct),
418 NULL, buf) ? -1 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
Ingo Molnarf6689642008-03-05 15:37:32 +0100420
421 if (cpu_has_fxsr)
422 return save_i387_fxsave(buf);
423 else
424 return save_i387_fsave(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Roland McGrath44210112008-01-30 13:31:50 +0100427static inline int restore_i387_fsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 struct task_struct *tsk = current;
Ingo Molnarf6689642008-03-05 15:37:32 +0100430
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100431 clear_fpu(tsk);
Suresh Siddha61c46282008-03-10 15:28:04 -0700432 return __copy_from_user(&tsk->thread.xstate->fsave, buf,
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100433 sizeof(struct i387_fsave_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Roland McGrath44210112008-01-30 13:31:50 +0100436static int restore_i387_fxsave(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 struct task_struct *tsk = current;
Roland McGrath44210112008-01-30 13:31:50 +0100439 struct user_i387_ia32_struct env;
Ingo Molnarf6689642008-03-05 15:37:32 +0100440 int err;
441
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100442 clear_fpu(tsk);
Suresh Siddha61c46282008-03-10 15:28:04 -0700443 err = __copy_from_user(&tsk->thread.xstate->fxsave, &buf->_fxsr_env[0],
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100444 sizeof(struct i387_fxsave_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /* mxcsr reserved bits must be masked to zero for security reasons */
Suresh Siddha61c46282008-03-10 15:28:04 -0700446 tsk->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
Roland McGrath44210112008-01-30 13:31:50 +0100447 if (err || __copy_from_user(&env, buf, sizeof(env)))
448 return 1;
449 convert_to_fxsr(tsk, &env);
Ingo Molnarf6689642008-03-05 15:37:32 +0100450
Roland McGrath44210112008-01-30 13:31:50 +0100451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Roland McGrath44210112008-01-30 13:31:50 +0100454int restore_i387_ia32(struct _fpstate_ia32 __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 int err;
457
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100458 if (HAVE_HWFP) {
Ingo Molnarf6689642008-03-05 15:37:32 +0100459 if (cpu_has_fxsr)
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100460 err = restore_i387_fxsave(buf);
Ingo Molnarf6689642008-03-05 15:37:32 +0100461 else
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100462 err = restore_i387_fsave(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 } else {
Roland McGrath44210112008-01-30 13:31:50 +0100464 err = fpregs_soft_set(current, NULL,
465 0, sizeof(struct user_i387_ia32_struct),
466 NULL, buf) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468 set_used_math();
Ingo Molnarf6689642008-03-05 15:37:32 +0100469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return err;
471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473/*
474 * FPU state for core dumps.
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100475 * This is only used for a.out dumps now.
476 * It is declared generically using elf_fpregset_t (which is
477 * struct user_i387_struct) but is in fact only used for 32-bit
478 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100480int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 struct task_struct *tsk = current;
Ingo Molnarf6689642008-03-05 15:37:32 +0100483 int fpvalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 fpvalid = !!used_math();
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100486 if (fpvalid)
487 fpvalid = !fpregs_get(tsk, NULL,
488 0, sizeof(struct user_i387_ia32_struct),
489 fpu, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 return fpvalid;
492}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700493EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100495#endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */