blob: bebe0346346103309061c4c3890f5acc81f3045a [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 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/sched.h>
Alexey Dobriyan129f6942005-06-23 00:08:33 -070010#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <asm/processor.h>
12#include <asm/i387.h>
13#include <asm/math_emu.h>
14#include <asm/sigcontext.h>
15#include <asm/user.h>
16#include <asm/ptrace.h>
17#include <asm/uaccess.h>
18
19#ifdef CONFIG_MATH_EMULATION
20#define HAVE_HWFP (boot_cpu_data.hard_math)
21#else
22#define HAVE_HWFP 1
23#endif
24
Andreas Mohr7b0c2d92006-06-23 02:04:26 -070025static unsigned long mxcsr_feature_mask __read_mostly = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27void mxcsr_feature_mask_init(void)
28{
29 unsigned long mask = 0;
30 clts();
31 if (cpu_has_fxsr) {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +010032 memset(&current->thread.i387.fxsave, 0,
33 sizeof(struct i387_fxsave_struct));
34 asm volatile("fxsave %0" : : "m" (current->thread.i387.fxsave));
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 mask = current->thread.i387.fxsave.mxcsr_mask;
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +010036 if (mask == 0)
37 mask = 0x0000ffbf;
38 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 mxcsr_feature_mask &= mask;
40 stts();
41}
42
43/*
44 * The _current_ task is using the FPU for the first time
45 * so initialize it and set the mxcsr to its default
46 * value at reset if we support XMM instructions and then
47 * remeber the current task has used the FPU.
48 */
49void init_fpu(struct task_struct *tsk)
50{
51 if (cpu_has_fxsr) {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +010052 memset(&tsk->thread.i387.fxsave, 0,
53 sizeof(struct i387_fxsave_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 tsk->thread.i387.fxsave.cwd = 0x37f;
55 if (cpu_has_xmm)
56 tsk->thread.i387.fxsave.mxcsr = 0x1f80;
57 } else {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +010058 memset(&tsk->thread.i387.fsave, 0,
59 sizeof(struct i387_fsave_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 tsk->thread.i387.fsave.cwd = 0xffff037fu;
61 tsk->thread.i387.fsave.swd = 0xffff0000u;
62 tsk->thread.i387.fsave.twd = 0xffffffffu;
63 tsk->thread.i387.fsave.fos = 0xffff0000u;
64 }
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +010065 /* only the device not available exception
66 * or ptrace can call init_fpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 set_stopped_child_used_math(tsk);
68}
69
70/*
71 * FPU lazy state save handling.
72 */
73
74void kernel_fpu_begin(void)
75{
76 struct thread_info *thread = current_thread_info();
77
78 preempt_disable();
79 if (thread->status & TS_USEDFPU) {
80 __save_init_fpu(thread->task);
81 return;
82 }
83 clts();
84}
Alexey Dobriyan129f6942005-06-23 00:08:33 -070085EXPORT_SYMBOL_GPL(kernel_fpu_begin);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/*
88 * FPU tag word conversions.
89 */
90
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +010091static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +010094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +010096 tmp = ~twd;
97 tmp = (tmp | (tmp >> 1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
98 /* and move the valid bits to the lower byte. */
99 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
100 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
101 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
102
103 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100106static inline unsigned long twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 struct _fpxreg *st = NULL;
109 unsigned long tos = (fxsave->swd >> 11) & 7;
110 unsigned long twd = (unsigned long) fxsave->twd;
111 unsigned long tag;
112 unsigned long ret = 0xffff0000u;
113 int i;
114
115#define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
116
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100117 for (i = 0; i < 8; i++) {
118 if (twd & 0x1) {
119 st = FPREG_ADDR(fxsave, (i - tos) & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100121 switch (st->exponent & 0x7fff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 case 0x7fff:
123 tag = 2; /* Special */
124 break;
125 case 0x0000:
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100126 if (!st->significand[0] &&
127 !st->significand[1] &&
128 !st->significand[2] &&
129 !st->significand[3]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 tag = 1; /* Zero */
131 } else {
132 tag = 2; /* Special */
133 }
134 break;
135 default:
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100136 if (st->significand[3] & 0x8000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 tag = 0; /* Valid */
138 } else {
139 tag = 2; /* Special */
140 }
141 break;
142 }
143 } else {
144 tag = 3; /* Empty */
145 }
146 ret |= (tag << (2 * i));
147 twd = twd >> 1;
148 }
149 return ret;
150}
151
152/*
153 * FPU state interaction.
154 */
155
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100156unsigned short get_fpu_cwd(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100158 if (cpu_has_fxsr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return tsk->thread.i387.fxsave.cwd;
160 } else {
161 return (unsigned short)tsk->thread.i387.fsave.cwd;
162 }
163}
164
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100165unsigned short get_fpu_swd(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100167 if (cpu_has_fxsr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return tsk->thread.i387.fxsave.swd;
169 } else {
170 return (unsigned short)tsk->thread.i387.fsave.swd;
171 }
172}
173
174#if 0
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100175unsigned short get_fpu_twd(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100177 if (cpu_has_fxsr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return tsk->thread.i387.fxsave.twd;
179 } else {
180 return (unsigned short)tsk->thread.i387.fsave.twd;
181 }
182}
183#endif /* 0 */
184
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100185unsigned short get_fpu_mxcsr(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100187 if (cpu_has_xmm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return tsk->thread.i387.fxsave.mxcsr;
189 } else {
190 return 0x1f80;
191 }
192}
193
194#if 0
195
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100196void set_fpu_cwd(struct task_struct *tsk, unsigned short cwd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100198 if (cpu_has_fxsr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 tsk->thread.i387.fxsave.cwd = cwd;
200 } else {
201 tsk->thread.i387.fsave.cwd = ((long)cwd | 0xffff0000u);
202 }
203}
204
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100205void set_fpu_swd(struct task_struct *tsk, unsigned short swd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100207 if (cpu_has_fxsr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 tsk->thread.i387.fxsave.swd = swd;
209 } else {
210 tsk->thread.i387.fsave.swd = ((long)swd | 0xffff0000u);
211 }
212}
213
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100214void set_fpu_twd(struct task_struct *tsk, unsigned short twd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100216 if (cpu_has_fxsr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 tsk->thread.i387.fxsave.twd = twd_i387_to_fxsr(twd);
218 } else {
219 tsk->thread.i387.fsave.twd = ((long)twd | 0xffff0000u);
220 }
221}
222
223#endif /* 0 */
224
225/*
226 * FXSR floating point environment conversions.
227 */
228
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100229static int convert_fxsr_to_user(struct _fpstate __user *buf,
230 struct i387_fxsave_struct *fxsave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 unsigned long env[7];
233 struct _fpreg __user *to;
234 struct _fpxreg *from;
235 int i;
236
237 env[0] = (unsigned long)fxsave->cwd | 0xffff0000ul;
238 env[1] = (unsigned long)fxsave->swd | 0xffff0000ul;
239 env[2] = twd_fxsr_to_i387(fxsave);
240 env[3] = fxsave->fip;
241 env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
242 env[5] = fxsave->foo;
243 env[6] = fxsave->fos;
244
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100245 if (__copy_to_user(buf, env, 7 * sizeof(unsigned long)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return 1;
247
248 to = &buf->_st[0];
249 from = (struct _fpxreg *) &fxsave->st_space[0];
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100250 for (i = 0; i < 8; i++, to++, from++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 unsigned long __user *t = (unsigned long __user *)to;
252 unsigned long *f = (unsigned long *)from;
253
254 if (__put_user(*f, t) ||
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100255 __put_user(*(f + 1), t + 1) ||
256 __put_user(from->exponent, &to->exponent))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return 1;
258 }
259 return 0;
260}
261
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100262static int convert_fxsr_from_user(struct i387_fxsave_struct *fxsave,
263 struct _fpstate __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 unsigned long env[7];
266 struct _fpxreg *to;
267 struct _fpreg __user *from;
268 int i;
269
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100270 if (__copy_from_user(env, buf, 7 * sizeof(long)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return 1;
272
273 fxsave->cwd = (unsigned short)(env[0] & 0xffff);
274 fxsave->swd = (unsigned short)(env[1] & 0xffff);
275 fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
276 fxsave->fip = env[3];
277 fxsave->fop = (unsigned short)((env[4] & 0xffff0000ul) >> 16);
278 fxsave->fcs = (env[4] & 0xffff);
279 fxsave->foo = env[5];
280 fxsave->fos = env[6];
281
282 to = (struct _fpxreg *) &fxsave->st_space[0];
283 from = &buf->_st[0];
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100284 for (i = 0; i < 8; i++, to++, from++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 unsigned long *t = (unsigned long *)to;
286 unsigned long __user *f = (unsigned long __user *)from;
287
288 if (__get_user(*t, f) ||
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100289 __get_user(*(t + 1), f + 1) ||
290 __get_user(to->exponent, &from->exponent))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return 1;
292 }
293 return 0;
294}
295
296/*
297 * Signal frame handlers.
298 */
299
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100300static inline int save_i387_fsave(struct _fpstate __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 struct task_struct *tsk = current;
303
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100304 unlazy_fpu(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 tsk->thread.i387.fsave.status = tsk->thread.i387.fsave.swd;
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100306 if (__copy_to_user(buf, &tsk->thread.i387.fsave,
307 sizeof(struct i387_fsave_struct)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return -1;
309 return 1;
310}
311
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100312static int save_i387_fxsave(struct _fpstate __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 struct task_struct *tsk = current;
315 int err = 0;
316
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100317 unlazy_fpu(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100319 if (convert_fxsr_to_user(buf, &tsk->thread.i387.fxsave))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return -1;
321
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100322 err |= __put_user(tsk->thread.i387.fxsave.swd, &buf->status);
323 err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
324 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return -1;
326
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100327 if (__copy_to_user(&buf->_fxsr_env[0], &tsk->thread.i387.fxsave,
328 sizeof(struct i387_fxsave_struct)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return -1;
330 return 1;
331}
332
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100333int save_i387(struct _fpstate __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100335 if (!used_math())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return 0;
337
338 /* This will cause a "finit" to be triggered by the next
339 * attempted FPU operation by the 'current' process.
340 */
341 clear_used_math();
342
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100343 if (HAVE_HWFP) {
344 if (cpu_has_fxsr) {
345 return save_i387_fxsave(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 } else {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100347 return save_i387_fsave(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349 } else {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100350 return save_i387_soft(&current->thread.i387.soft, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352}
353
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100354static inline int restore_i387_fsave(struct _fpstate __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 struct task_struct *tsk = current;
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100357 clear_fpu(tsk);
358 return __copy_from_user(&tsk->thread.i387.fsave, buf,
359 sizeof(struct i387_fsave_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100362static int restore_i387_fxsave(struct _fpstate __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 int err;
365 struct task_struct *tsk = current;
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100366 clear_fpu(tsk);
367 err = __copy_from_user(&tsk->thread.i387.fxsave, &buf->_fxsr_env[0],
368 sizeof(struct i387_fxsave_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /* mxcsr reserved bits must be masked to zero for security reasons */
370 tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100371 return err ? 1 : convert_fxsr_from_user(&tsk->thread.i387.fxsave, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100374int restore_i387(struct _fpstate __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 int err;
377
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100378 if (HAVE_HWFP) {
379 if (cpu_has_fxsr) {
380 err = restore_i387_fxsave(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 } else {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100382 err = restore_i387_fsave(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384 } else {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100385 err = restore_i387_soft(&current->thread.i387.soft, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387 set_used_math();
388 return err;
389}
390
391/*
392 * ptrace request handlers.
393 */
394
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100395static inline int get_fpregs_fsave(struct user_i387_struct __user *buf,
396 struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100398 return __copy_to_user(buf, &tsk->thread.i387.fsave,
399 sizeof(struct user_i387_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100402static inline int get_fpregs_fxsave(struct user_i387_struct __user *buf,
403 struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100405 return convert_fxsr_to_user((struct _fpstate __user *)buf,
406 &tsk->thread.i387.fxsave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100409int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100411 if (HAVE_HWFP) {
412 if (cpu_has_fxsr) {
413 return get_fpregs_fxsave(buf, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 } else {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100415 return get_fpregs_fsave(buf, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 } else {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100418 return save_i387_soft(&tsk->thread.i387.soft,
419 (struct _fpstate __user *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421}
422
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100423static inline int set_fpregs_fsave(struct task_struct *tsk,
424 struct user_i387_struct __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100426 return __copy_from_user(&tsk->thread.i387.fsave, buf,
427 sizeof(struct user_i387_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100430static inline int set_fpregs_fxsave(struct task_struct *tsk,
431 struct user_i387_struct __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100433 return convert_fxsr_from_user(&tsk->thread.i387.fxsave,
434 (struct _fpstate __user *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100437int set_fpregs(struct task_struct *tsk, struct user_i387_struct __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100439 if (HAVE_HWFP) {
440 if (cpu_has_fxsr) {
441 return set_fpregs_fxsave(tsk, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 } else {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100443 return set_fpregs_fsave(tsk, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445 } else {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100446 return restore_i387_soft(&tsk->thread.i387.soft,
447 (struct _fpstate __user *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449}
450
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100451int get_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100453 if (cpu_has_fxsr) {
454 if (__copy_to_user(buf, &tsk->thread.i387.fxsave,
455 sizeof(struct user_fxsr_struct)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return -EFAULT;
457 return 0;
458 } else {
459 return -EIO;
460 }
461}
462
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100463int set_fpxregs(struct task_struct *tsk, struct user_fxsr_struct __user *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 int ret = 0;
466
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100467 if (cpu_has_fxsr) {
468 if (__copy_from_user(&tsk->thread.i387.fxsave, buf,
469 sizeof(struct user_fxsr_struct)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 ret = -EFAULT;
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100471 /* mxcsr reserved bits must be masked to zero
472 * for security reasons */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
474 } else {
475 ret = -EIO;
476 }
477 return ret;
478}
479
480/*
481 * FPU state for core dumps.
482 */
483
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100484static inline void copy_fpu_fsave(struct task_struct *tsk,
485 struct user_i387_struct *fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100487 memcpy(fpu, &tsk->thread.i387.fsave,
488 sizeof(struct user_i387_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100491static inline void copy_fpu_fxsave(struct task_struct *tsk,
492 struct user_i387_struct *fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 unsigned short *to;
495 unsigned short *from;
496 int i;
497
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100498 memcpy(fpu, &tsk->thread.i387.fxsave, 7 * sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 to = (unsigned short *)&fpu->st_space[0];
501 from = (unsigned short *)&tsk->thread.i387.fxsave.st_space[0];
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100502 for (i = 0; i < 8; i++, to += 5, from += 8)
503 memcpy(to, from, 5 * sizeof(unsigned short));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100506int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 int fpvalid;
509 struct task_struct *tsk = current;
510
511 fpvalid = !!used_math();
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100512 if (fpvalid) {
513 unlazy_fpu(tsk);
514 if (cpu_has_fxsr) {
515 copy_fpu_fxsave(tsk, fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 } else {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100517 copy_fpu_fsave(tsk, fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 }
520
521 return fpvalid;
522}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700523EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525int dump_task_fpu(struct task_struct *tsk, struct user_i387_struct *fpu)
526{
527 int fpvalid = !!tsk_used_math(tsk);
528
529 if (fpvalid) {
530 if (tsk == current)
531 unlazy_fpu(tsk);
532 if (cpu_has_fxsr)
533 copy_fpu_fxsave(tsk, fpu);
534 else
535 copy_fpu_fsave(tsk, fpu);
536 }
537 return fpvalid;
538}
539
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100540int dump_task_extended_fpu(struct task_struct *tsk,
541 struct user_fxsr_struct *fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 int fpvalid = tsk_used_math(tsk) && cpu_has_fxsr;
544
545 if (fpvalid) {
546 if (tsk == current)
547 unlazy_fpu(tsk);
548 memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(*fpu));
549 }
550 return fpvalid;
551}