blob: ab6a2e8028ae2fceafdbb171b2f3f8bc56cccfdd [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.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>
Linus Torvalds1361b832012-02-21 13:19:22 -080019#include <asm/fpu-internal.h>
Ingo Molnarf6689642008-03-05 15:37:32 +010020#include <asm/user.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds8546c002012-02-21 10:25:45 -080022/*
23 * Were we in an interrupt that interrupted kernel mode?
24 *
25 * We can do a kernel_fpu_begin/end() pair *ONLY* if that
26 * pair does nothing at all: the thread must not have fpu (so
27 * that we don't try to save the FPU state), and TS must
28 * be set (so that the clts/stts pair does nothing that is
29 * visible in the interrupted kernel thread).
30 */
31static inline bool interrupted_kernel_fpu_idle(void)
32{
33 return !__thread_has_fpu(current) &&
34 (read_cr0() & X86_CR0_TS);
35}
36
37/*
38 * Were we in user mode (or vm86 mode) when we were
39 * interrupted?
40 *
41 * Doing kernel_fpu_begin/end() is ok if we are running
42 * in an interrupt context from user mode - we'll just
43 * save the FPU state as required.
44 */
45static inline bool interrupted_user_mode(void)
46{
47 struct pt_regs *regs = get_irq_regs();
48 return regs && user_mode_vm(regs);
49}
50
51/*
52 * Can we use the FPU in kernel mode with the
53 * whole "kernel_fpu_begin/end()" sequence?
54 *
55 * It's always ok in process context (ie "not interrupt")
56 * but it is sometimes ok even from an irq.
57 */
58bool irq_fpu_usable(void)
59{
60 return !in_interrupt() ||
61 interrupted_user_mode() ||
62 interrupted_kernel_fpu_idle();
63}
64EXPORT_SYMBOL(irq_fpu_usable);
65
66void kernel_fpu_begin(void)
67{
68 struct task_struct *me = current;
69
70 WARN_ON_ONCE(!irq_fpu_usable());
71 preempt_disable();
72 if (__thread_has_fpu(me)) {
73 __save_init_fpu(me);
74 __thread_clear_has_fpu(me);
75 /* We do 'stts()' in kernel_fpu_end() */
76 } else {
Alex Shic6ae41e2012-05-11 15:35:27 +080077 this_cpu_write(fpu_owner_task, NULL);
Linus Torvalds8546c002012-02-21 10:25:45 -080078 clts();
79 }
80}
81EXPORT_SYMBOL(kernel_fpu_begin);
82
83void kernel_fpu_end(void)
84{
85 stts();
86 preempt_enable();
87}
88EXPORT_SYMBOL(kernel_fpu_end);
89
90void unlazy_fpu(struct task_struct *tsk)
91{
92 preempt_disable();
93 if (__thread_has_fpu(tsk)) {
94 __save_init_fpu(tsk);
95 __thread_fpu_end(tsk);
96 } else
97 tsk->fpu_counter = 0;
98 preempt_enable();
99}
100EXPORT_SYMBOL(unlazy_fpu);
101
Suresh Siddha72a671c2012-07-24 16:05:29 -0700102unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
Suresh Siddha61c46282008-03-10 15:28:04 -0700103unsigned int xstate_size;
Xiaotian Fengf45755b2010-08-13 15:19:11 +0800104EXPORT_SYMBOL_GPL(xstate_size);
Suresh Siddha61c46282008-03-10 15:28:04 -0700105static struct i387_fxsave_struct fx_scratch __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Linus Torvalds1361b832012-02-21 13:19:22 -0800107static void __cpuinit mxcsr_feature_mask_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 unsigned long mask = 0;
Ingo Molnarf6689642008-03-05 15:37:32 +0100110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 clts();
112 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700113 memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct));
114 asm volatile("fxsave %0" : : "m" (fx_scratch));
115 mask = fx_scratch.mxcsr_mask;
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100116 if (mask == 0)
117 mask = 0x0000ffbf;
118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 mxcsr_feature_mask &= mask;
120 stts();
121}
122
Robert Richter0e49bf62010-07-21 19:03:52 +0200123static void __cpuinit init_thread_xstate(void)
Suresh Siddha61c46282008-03-10 15:28:04 -0700124{
Robert Richter0e49bf62010-07-21 19:03:52 +0200125 /*
126 * Note that xstate_size might be overwriten later during
127 * xsave_init().
128 */
129
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700130 if (!HAVE_HWFP) {
Robert Richter1f999ab2010-07-21 19:03:57 +0200131 /*
132 * Disable xsave as we do not support it if i387
133 * emulation is enabled.
134 */
135 setup_clear_cpu_cap(X86_FEATURE_XSAVE);
136 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700137 xstate_size = sizeof(struct i387_soft_struct);
138 return;
139 }
140
Suresh Siddha61c46282008-03-10 15:28:04 -0700141 if (cpu_has_fxsr)
142 xstate_size = sizeof(struct i387_fxsave_struct);
Suresh Siddha61c46282008-03-10 15:28:04 -0700143 else
144 xstate_size = sizeof(struct i387_fsave_struct);
Suresh Siddha61c46282008-03-10 15:28:04 -0700145}
146
Roland McGrath44210112008-01-30 13:31:50 +0100147/*
148 * Called at bootup to set up the initial FPU state that is later cloned
149 * into all processes.
150 */
Robert Richter0e49bf62010-07-21 19:03:52 +0200151
Roland McGrath44210112008-01-30 13:31:50 +0100152void __cpuinit fpu_init(void)
153{
Brian Gerst6ac8bac2010-09-03 21:17:09 -0400154 unsigned long cr0;
155 unsigned long cr4_mask = 0;
Ingo Molnarf6689642008-03-05 15:37:32 +0100156
Brian Gerst6ac8bac2010-09-03 21:17:09 -0400157 if (cpu_has_fxsr)
158 cr4_mask |= X86_CR4_OSFXSR;
159 if (cpu_has_xmm)
160 cr4_mask |= X86_CR4_OSXMMEXCPT;
161 if (cr4_mask)
162 set_in_cr4(cr4_mask);
Roland McGrath44210112008-01-30 13:31:50 +0100163
Brian Gerst6ac8bac2010-09-03 21:17:09 -0400164 cr0 = read_cr0();
165 cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
166 if (!HAVE_HWFP)
167 cr0 |= X86_CR0_EM;
168 write_cr0(cr0);
Roland McGrath44210112008-01-30 13:31:50 +0100169
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700170 if (!smp_processor_id())
171 init_thread_xstate();
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700172
Roland McGrath44210112008-01-30 13:31:50 +0100173 mxcsr_feature_mask_init();
174 /* clean state in init */
Avi Kivityc9ad4882010-05-06 11:45:45 +0300175 current_thread_info()->status = 0;
Roland McGrath44210112008-01-30 13:31:50 +0100176 clear_used_math();
177}
Robert Richter0e49bf62010-07-21 19:03:52 +0200178
Sheng Yang5ee481d2010-05-17 17:22:23 +0800179void fpu_finit(struct fpu *fpu)
Avi Kivity86603282010-05-06 11:45:46 +0300180{
Avi Kivity86603282010-05-06 11:45:46 +0300181 if (!HAVE_HWFP) {
182 finit_soft_fpu(&fpu->state->soft);
183 return;
184 }
Avi Kivity86603282010-05-06 11:45:46 +0300185
186 if (cpu_has_fxsr) {
187 struct i387_fxsave_struct *fx = &fpu->state->fxsave;
188
189 memset(fx, 0, xstate_size);
190 fx->cwd = 0x37f;
191 if (cpu_has_xmm)
192 fx->mxcsr = MXCSR_DEFAULT;
193 } else {
194 struct i387_fsave_struct *fp = &fpu->state->fsave;
195 memset(fp, 0, xstate_size);
196 fp->cwd = 0xffff037fu;
197 fp->swd = 0xffff0000u;
198 fp->twd = 0xffffffffu;
199 fp->fos = 0xffff0000u;
200 }
201}
Sheng Yang5ee481d2010-05-17 17:22:23 +0800202EXPORT_SYMBOL_GPL(fpu_finit);
Avi Kivity86603282010-05-06 11:45:46 +0300203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/*
205 * The _current_ task is using the FPU for the first time
206 * so initialize it and set the mxcsr to its default
207 * value at reset if we support XMM instructions and then
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300208 * remember the current task has used the FPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 */
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700210int init_fpu(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Avi Kivity86603282010-05-06 11:45:46 +0300212 int ret;
213
Roland McGrath44210112008-01-30 13:31:50 +0100214 if (tsk_used_math(tsk)) {
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700215 if (HAVE_HWFP && tsk == current)
Roland McGrath44210112008-01-30 13:31:50 +0100216 unlazy_fpu(tsk);
Oleg Nesterov089f9fb2012-04-16 22:48:15 +0200217 tsk->thread.fpu.last_cpu = ~0;
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700218 return 0;
219 }
220
221 /*
222 * Memory allocation at the first usage of the FPU and other state.
223 */
Avi Kivity86603282010-05-06 11:45:46 +0300224 ret = fpu_alloc(&tsk->thread.fpu);
225 if (ret)
226 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100227
Avi Kivity86603282010-05-06 11:45:46 +0300228 fpu_finit(&tsk->thread.fpu);
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 set_stopped_child_used_math(tsk);
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700231 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
Avi Kivitye5c30142011-01-11 12:15:54 +0200233EXPORT_SYMBOL_GPL(init_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800235/*
236 * The xstateregs_active() routine is the same as the fpregs_active() routine,
237 * as the "regset->n" for the xstate regset will be updated based on the feature
238 * capabilites supported by the xsave.
239 */
Roland McGrath44210112008-01-30 13:31:50 +0100240int fpregs_active(struct task_struct *target, const struct user_regset *regset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Roland McGrath44210112008-01-30 13:31:50 +0100242 return tsk_used_math(target) ? regset->n : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
Roland McGrath44210112008-01-30 13:31:50 +0100244
245int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
246{
247 return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
248}
249
250int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
251 unsigned int pos, unsigned int count,
252 void *kbuf, void __user *ubuf)
253{
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700254 int ret;
255
Roland McGrath44210112008-01-30 13:31:50 +0100256 if (!cpu_has_fxsr)
257 return -ENODEV;
258
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700259 ret = init_fpu(target);
260 if (ret)
261 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100262
Suresh Siddha29104e12010-07-19 16:05:49 -0700263 sanitize_i387_state(target);
264
Roland McGrath44210112008-01-30 13:31:50 +0100265 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300266 &target->thread.fpu.state->fxsave, 0, -1);
Roland McGrath44210112008-01-30 13:31:50 +0100267}
268
269int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
270 unsigned int pos, unsigned int count,
271 const void *kbuf, const void __user *ubuf)
272{
273 int ret;
274
275 if (!cpu_has_fxsr)
276 return -ENODEV;
277
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700278 ret = init_fpu(target);
279 if (ret)
280 return ret;
281
Suresh Siddha29104e12010-07-19 16:05:49 -0700282 sanitize_i387_state(target);
283
Roland McGrath44210112008-01-30 13:31:50 +0100284 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300285 &target->thread.fpu.state->fxsave, 0, -1);
Roland McGrath44210112008-01-30 13:31:50 +0100286
287 /*
288 * mxcsr reserved bits must be masked to zero for security reasons.
289 */
Avi Kivity86603282010-05-06 11:45:46 +0300290 target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
Roland McGrath44210112008-01-30 13:31:50 +0100291
Suresh Siddha42deec62008-07-29 10:29:26 -0700292 /*
293 * update the header bits in the xsave header, indicating the
294 * presence of FP and SSE state.
295 */
296 if (cpu_has_xsave)
Avi Kivity86603282010-05-06 11:45:46 +0300297 target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FPSSE;
Suresh Siddha42deec62008-07-29 10:29:26 -0700298
Roland McGrath44210112008-01-30 13:31:50 +0100299 return ret;
300}
301
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800302int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
303 unsigned int pos, unsigned int count,
304 void *kbuf, void __user *ubuf)
305{
306 int ret;
307
308 if (!cpu_has_xsave)
309 return -ENODEV;
310
311 ret = init_fpu(target);
312 if (ret)
313 return ret;
314
315 /*
Suresh Siddhaff7fbc72010-02-22 14:51:33 -0800316 * Copy the 48bytes defined by the software first into the xstate
317 * memory layout in the thread struct, so that we can copy the entire
318 * xstateregs to the user using one user_regset_copyout().
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800319 */
Avi Kivity86603282010-05-06 11:45:46 +0300320 memcpy(&target->thread.fpu.state->fxsave.sw_reserved,
Suresh Siddhaff7fbc72010-02-22 14:51:33 -0800321 xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800322
323 /*
Suresh Siddhaff7fbc72010-02-22 14:51:33 -0800324 * Copy the xstate memory layout.
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800325 */
326 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300327 &target->thread.fpu.state->xsave, 0, -1);
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800328 return ret;
329}
330
331int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
332 unsigned int pos, unsigned int count,
333 const void *kbuf, const void __user *ubuf)
334{
335 int ret;
336 struct xsave_hdr_struct *xsave_hdr;
337
338 if (!cpu_has_xsave)
339 return -ENODEV;
340
341 ret = init_fpu(target);
342 if (ret)
343 return ret;
344
345 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300346 &target->thread.fpu.state->xsave, 0, -1);
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800347
348 /*
349 * mxcsr reserved bits must be masked to zero for security reasons.
350 */
Avi Kivity86603282010-05-06 11:45:46 +0300351 target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800352
Avi Kivity86603282010-05-06 11:45:46 +0300353 xsave_hdr = &target->thread.fpu.state->xsave.xsave_hdr;
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800354
355 xsave_hdr->xstate_bv &= pcntxt_mask;
356 /*
357 * These bits must be zero.
358 */
359 xsave_hdr->reserved1[0] = xsave_hdr->reserved1[1] = 0;
360
361 return ret;
362}
363
Roland McGrath44210112008-01-30 13:31:50 +0100364#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366/*
367 * FPU tag word conversions.
368 */
369
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100370static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100375 tmp = ~twd;
Roland McGrath44210112008-01-30 13:31:50 +0100376 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100377 /* and move the valid bits to the lower byte. */
378 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
379 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
380 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
Ingo Molnarf6689642008-03-05 15:37:32 +0100381
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100382 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Phil Carmody497888c2011-07-14 15:07:13 +0300385#define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16)
Roland McGrath44210112008-01-30 13:31:50 +0100386#define FP_EXP_TAG_VALID 0
387#define FP_EXP_TAG_ZERO 1
388#define FP_EXP_TAG_SPECIAL 2
389#define FP_EXP_TAG_EMPTY 3
390
391static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Roland McGrath44210112008-01-30 13:31:50 +0100393 struct _fpxreg *st;
394 u32 tos = (fxsave->swd >> 11) & 7;
395 u32 twd = (unsigned long) fxsave->twd;
396 u32 tag;
397 u32 ret = 0xffff0000u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 int i;
399
Roland McGrath44210112008-01-30 13:31:50 +0100400 for (i = 0; i < 8; i++, twd >>= 1) {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100401 if (twd & 0x1) {
402 st = FPREG_ADDR(fxsave, (i - tos) & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100404 switch (st->exponent & 0x7fff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 case 0x7fff:
Roland McGrath44210112008-01-30 13:31:50 +0100406 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 break;
408 case 0x0000:
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100409 if (!st->significand[0] &&
410 !st->significand[1] &&
411 !st->significand[2] &&
Roland McGrath44210112008-01-30 13:31:50 +0100412 !st->significand[3])
413 tag = FP_EXP_TAG_ZERO;
414 else
415 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 break;
417 default:
Roland McGrath44210112008-01-30 13:31:50 +0100418 if (st->significand[3] & 0x8000)
419 tag = FP_EXP_TAG_VALID;
420 else
421 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 break;
423 }
424 } else {
Roland McGrath44210112008-01-30 13:31:50 +0100425 tag = FP_EXP_TAG_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
Roland McGrath44210112008-01-30 13:31:50 +0100427 ret |= tag << (2 * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 return ret;
430}
431
432/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * FXSR floating point environment conversions.
434 */
435
Suresh Siddha72a671c2012-07-24 16:05:29 -0700436void
Ingo Molnarf6689642008-03-05 15:37:32 +0100437convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Avi Kivity86603282010-05-06 11:45:46 +0300439 struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100440 struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
441 struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int i;
443
Roland McGrath44210112008-01-30 13:31:50 +0100444 env->cwd = fxsave->cwd | 0xffff0000u;
445 env->swd = fxsave->swd | 0xffff0000u;
446 env->twd = twd_fxsr_to_i387(fxsave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Roland McGrath44210112008-01-30 13:31:50 +0100448#ifdef CONFIG_X86_64
449 env->fip = fxsave->rip;
450 env->foo = fxsave->rdp;
Brian Gerst10c11f32010-09-03 21:17:13 -0400451 /*
452 * should be actually ds/cs at fpu exception time, but
453 * that information is not available in 64bit mode.
454 */
455 env->fcs = task_pt_regs(tsk)->cs;
Roland McGrath44210112008-01-30 13:31:50 +0100456 if (tsk == current) {
Brian Gerst10c11f32010-09-03 21:17:13 -0400457 savesegment(ds, env->fos);
Roland McGrath44210112008-01-30 13:31:50 +0100458 } else {
Brian Gerst10c11f32010-09-03 21:17:13 -0400459 env->fos = tsk->thread.ds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
Brian Gerst10c11f32010-09-03 21:17:13 -0400461 env->fos |= 0xffff0000;
Roland McGrath44210112008-01-30 13:31:50 +0100462#else
463 env->fip = fxsave->fip;
Jan Beulich609b5292008-03-05 08:35:14 +0000464 env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
Roland McGrath44210112008-01-30 13:31:50 +0100465 env->foo = fxsave->foo;
466 env->fos = fxsave->fos;
467#endif
468
469 for (i = 0; i < 8; ++i)
470 memcpy(&to[i], &from[i], sizeof(to[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Suresh Siddha72a671c2012-07-24 16:05:29 -0700473void convert_to_fxsr(struct task_struct *tsk,
474 const struct user_i387_ia32_struct *env)
Roland McGrath44210112008-01-30 13:31:50 +0100475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Avi Kivity86603282010-05-06 11:45:46 +0300477 struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100478 struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
479 struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 int i;
481
Roland McGrath44210112008-01-30 13:31:50 +0100482 fxsave->cwd = env->cwd;
483 fxsave->swd = env->swd;
484 fxsave->twd = twd_i387_to_fxsr(env->twd);
485 fxsave->fop = (u16) ((u32) env->fcs >> 16);
486#ifdef CONFIG_X86_64
487 fxsave->rip = env->fip;
488 fxsave->rdp = env->foo;
489 /* cs and ds ignored */
490#else
491 fxsave->fip = env->fip;
492 fxsave->fcs = (env->fcs & 0xffff);
493 fxsave->foo = env->foo;
494 fxsave->fos = env->fos;
495#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Roland McGrath44210112008-01-30 13:31:50 +0100497 for (i = 0; i < 8; ++i)
498 memcpy(&to[i], &from[i], sizeof(from[0]));
499}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Roland McGrath44210112008-01-30 13:31:50 +0100501int fpregs_get(struct task_struct *target, const struct user_regset *regset,
502 unsigned int pos, unsigned int count,
503 void *kbuf, void __user *ubuf)
504{
505 struct user_i387_ia32_struct env;
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700506 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700508 ret = init_fpu(target);
509 if (ret)
510 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100511
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700512 if (!HAVE_HWFP)
513 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
514
Ingo Molnarf6689642008-03-05 15:37:32 +0100515 if (!cpu_has_fxsr) {
Roland McGrath44210112008-01-30 13:31:50 +0100516 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300517 &target->thread.fpu.state->fsave, 0,
Suresh Siddha61c46282008-03-10 15:28:04 -0700518 -1);
Ingo Molnarf6689642008-03-05 15:37:32 +0100519 }
Roland McGrath44210112008-01-30 13:31:50 +0100520
Suresh Siddha29104e12010-07-19 16:05:49 -0700521 sanitize_i387_state(target);
522
Roland McGrath44210112008-01-30 13:31:50 +0100523 if (kbuf && pos == 0 && count == sizeof(env)) {
524 convert_from_fxsr(kbuf, target);
525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
Roland McGrath44210112008-01-30 13:31:50 +0100527
528 convert_from_fxsr(&env, target);
Ingo Molnarf6689642008-03-05 15:37:32 +0100529
Roland McGrath44210112008-01-30 13:31:50 +0100530 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
531}
532
533int fpregs_set(struct task_struct *target, const struct user_regset *regset,
534 unsigned int pos, unsigned int count,
535 const void *kbuf, const void __user *ubuf)
536{
537 struct user_i387_ia32_struct env;
538 int ret;
539
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700540 ret = init_fpu(target);
541 if (ret)
542 return ret;
543
Suresh Siddha29104e12010-07-19 16:05:49 -0700544 sanitize_i387_state(target);
545
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700546 if (!HAVE_HWFP)
547 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
548
Ingo Molnarf6689642008-03-05 15:37:32 +0100549 if (!cpu_has_fxsr) {
Roland McGrath44210112008-01-30 13:31:50 +0100550 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300551 &target->thread.fpu.state->fsave, 0, -1);
Ingo Molnarf6689642008-03-05 15:37:32 +0100552 }
Roland McGrath44210112008-01-30 13:31:50 +0100553
554 if (pos > 0 || count < sizeof(env))
555 convert_from_fxsr(&env, target);
556
557 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
558 if (!ret)
559 convert_to_fxsr(target, &env);
560
Suresh Siddha42deec62008-07-29 10:29:26 -0700561 /*
562 * update the header bit in the xsave header, indicating the
563 * presence of FP.
564 */
565 if (cpu_has_xsave)
Avi Kivity86603282010-05-06 11:45:46 +0300566 target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FP;
Roland McGrath44210112008-01-30 13:31:50 +0100567 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
570/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 * FPU state for core dumps.
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100572 * This is only used for a.out dumps now.
573 * It is declared generically using elf_fpregset_t (which is
574 * struct user_i387_struct) but is in fact only used for 32-bit
575 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100577int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 struct task_struct *tsk = current;
Ingo Molnarf6689642008-03-05 15:37:32 +0100580 int fpvalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 fpvalid = !!used_math();
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100583 if (fpvalid)
584 fpvalid = !fpregs_get(tsk, NULL,
585 0, sizeof(struct user_i387_ia32_struct),
586 fpu, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 return fpvalid;
589}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700590EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100592#endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */