blob: 528557470ddb4efcb8088c7ab29cc0295fb14cf6 [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 *
Suresh Siddha304bced2012-08-24 14:13:02 -070025 * For now, on xsave platforms we will return interrupted
26 * kernel FPU as not-idle. TBD: As we use non-lazy FPU restore
27 * for xsave platforms, ideally we can change the return value
28 * to something like __thread_has_fpu(current). But we need to
29 * be careful of doing __thread_clear_has_fpu() before saving
30 * the FPU etc for supporting nested uses etc. For now, take
31 * the simple route!
32 *
33 * On others, we can do a kernel_fpu_begin/end() pair *ONLY* if that
Linus Torvalds8546c002012-02-21 10:25:45 -080034 * pair does nothing at all: the thread must not have fpu (so
35 * that we don't try to save the FPU state), and TS must
36 * be set (so that the clts/stts pair does nothing that is
37 * visible in the interrupted kernel thread).
38 */
39static inline bool interrupted_kernel_fpu_idle(void)
40{
Suresh Siddha304bced2012-08-24 14:13:02 -070041 if (use_xsave())
42 return 0;
43
Linus Torvalds8546c002012-02-21 10:25:45 -080044 return !__thread_has_fpu(current) &&
45 (read_cr0() & X86_CR0_TS);
46}
47
48/*
49 * Were we in user mode (or vm86 mode) when we were
50 * interrupted?
51 *
52 * Doing kernel_fpu_begin/end() is ok if we are running
53 * in an interrupt context from user mode - we'll just
54 * save the FPU state as required.
55 */
56static inline bool interrupted_user_mode(void)
57{
58 struct pt_regs *regs = get_irq_regs();
59 return regs && user_mode_vm(regs);
60}
61
62/*
63 * Can we use the FPU in kernel mode with the
64 * whole "kernel_fpu_begin/end()" sequence?
65 *
66 * It's always ok in process context (ie "not interrupt")
67 * but it is sometimes ok even from an irq.
68 */
69bool irq_fpu_usable(void)
70{
71 return !in_interrupt() ||
72 interrupted_user_mode() ||
73 interrupted_kernel_fpu_idle();
74}
75EXPORT_SYMBOL(irq_fpu_usable);
76
77void kernel_fpu_begin(void)
78{
79 struct task_struct *me = current;
80
81 WARN_ON_ONCE(!irq_fpu_usable());
82 preempt_disable();
83 if (__thread_has_fpu(me)) {
84 __save_init_fpu(me);
85 __thread_clear_has_fpu(me);
86 /* We do 'stts()' in kernel_fpu_end() */
Suresh Siddha304bced2012-08-24 14:13:02 -070087 } else if (!use_xsave()) {
Alex Shic6ae41e2012-05-11 15:35:27 +080088 this_cpu_write(fpu_owner_task, NULL);
Linus Torvalds8546c002012-02-21 10:25:45 -080089 clts();
90 }
91}
92EXPORT_SYMBOL(kernel_fpu_begin);
93
94void kernel_fpu_end(void)
95{
Suresh Siddha304bced2012-08-24 14:13:02 -070096 if (use_xsave())
97 math_state_restore();
98 else
99 stts();
Linus Torvalds8546c002012-02-21 10:25:45 -0800100 preempt_enable();
101}
102EXPORT_SYMBOL(kernel_fpu_end);
103
104void unlazy_fpu(struct task_struct *tsk)
105{
106 preempt_disable();
107 if (__thread_has_fpu(tsk)) {
108 __save_init_fpu(tsk);
109 __thread_fpu_end(tsk);
110 } else
111 tsk->fpu_counter = 0;
112 preempt_enable();
113}
114EXPORT_SYMBOL(unlazy_fpu);
115
Suresh Siddha72a671c2012-07-24 16:05:29 -0700116unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
Suresh Siddha61c46282008-03-10 15:28:04 -0700117unsigned int xstate_size;
Xiaotian Fengf45755b2010-08-13 15:19:11 +0800118EXPORT_SYMBOL_GPL(xstate_size);
Suresh Siddha61c46282008-03-10 15:28:04 -0700119static struct i387_fxsave_struct fx_scratch __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvalds1361b832012-02-21 13:19:22 -0800121static void __cpuinit mxcsr_feature_mask_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 unsigned long mask = 0;
Ingo Molnarf6689642008-03-05 15:37:32 +0100124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 clts();
126 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700127 memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct));
128 asm volatile("fxsave %0" : : "m" (fx_scratch));
129 mask = fx_scratch.mxcsr_mask;
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100130 if (mask == 0)
131 mask = 0x0000ffbf;
132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 mxcsr_feature_mask &= mask;
134 stts();
135}
136
Robert Richter0e49bf62010-07-21 19:03:52 +0200137static void __cpuinit init_thread_xstate(void)
Suresh Siddha61c46282008-03-10 15:28:04 -0700138{
Robert Richter0e49bf62010-07-21 19:03:52 +0200139 /*
140 * Note that xstate_size might be overwriten later during
141 * xsave_init().
142 */
143
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700144 if (!HAVE_HWFP) {
Robert Richter1f999ab2010-07-21 19:03:57 +0200145 /*
146 * Disable xsave as we do not support it if i387
147 * emulation is enabled.
148 */
149 setup_clear_cpu_cap(X86_FEATURE_XSAVE);
150 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700151 xstate_size = sizeof(struct i387_soft_struct);
152 return;
153 }
154
Suresh Siddha61c46282008-03-10 15:28:04 -0700155 if (cpu_has_fxsr)
156 xstate_size = sizeof(struct i387_fxsave_struct);
Suresh Siddha61c46282008-03-10 15:28:04 -0700157 else
158 xstate_size = sizeof(struct i387_fsave_struct);
Suresh Siddha61c46282008-03-10 15:28:04 -0700159}
160
Roland McGrath44210112008-01-30 13:31:50 +0100161/*
162 * Called at bootup to set up the initial FPU state that is later cloned
163 * into all processes.
164 */
Robert Richter0e49bf62010-07-21 19:03:52 +0200165
Roland McGrath44210112008-01-30 13:31:50 +0100166void __cpuinit fpu_init(void)
167{
Brian Gerst6ac8bac2010-09-03 21:17:09 -0400168 unsigned long cr0;
169 unsigned long cr4_mask = 0;
Ingo Molnarf6689642008-03-05 15:37:32 +0100170
Brian Gerst6ac8bac2010-09-03 21:17:09 -0400171 if (cpu_has_fxsr)
172 cr4_mask |= X86_CR4_OSFXSR;
173 if (cpu_has_xmm)
174 cr4_mask |= X86_CR4_OSXMMEXCPT;
175 if (cr4_mask)
176 set_in_cr4(cr4_mask);
Roland McGrath44210112008-01-30 13:31:50 +0100177
Brian Gerst6ac8bac2010-09-03 21:17:09 -0400178 cr0 = read_cr0();
179 cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
180 if (!HAVE_HWFP)
181 cr0 |= X86_CR0_EM;
182 write_cr0(cr0);
Roland McGrath44210112008-01-30 13:31:50 +0100183
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700184 if (!smp_processor_id())
185 init_thread_xstate();
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700186
Roland McGrath44210112008-01-30 13:31:50 +0100187 mxcsr_feature_mask_init();
188 /* clean state in init */
Avi Kivityc9ad4882010-05-06 11:45:45 +0300189 current_thread_info()->status = 0;
Roland McGrath44210112008-01-30 13:31:50 +0100190 clear_used_math();
191}
Robert Richter0e49bf62010-07-21 19:03:52 +0200192
Sheng Yang5ee481d2010-05-17 17:22:23 +0800193void fpu_finit(struct fpu *fpu)
Avi Kivity86603282010-05-06 11:45:46 +0300194{
Avi Kivity86603282010-05-06 11:45:46 +0300195 if (!HAVE_HWFP) {
196 finit_soft_fpu(&fpu->state->soft);
197 return;
198 }
Avi Kivity86603282010-05-06 11:45:46 +0300199
200 if (cpu_has_fxsr) {
201 struct i387_fxsave_struct *fx = &fpu->state->fxsave;
202
203 memset(fx, 0, xstate_size);
204 fx->cwd = 0x37f;
205 if (cpu_has_xmm)
206 fx->mxcsr = MXCSR_DEFAULT;
207 } else {
208 struct i387_fsave_struct *fp = &fpu->state->fsave;
209 memset(fp, 0, xstate_size);
210 fp->cwd = 0xffff037fu;
211 fp->swd = 0xffff0000u;
212 fp->twd = 0xffffffffu;
213 fp->fos = 0xffff0000u;
214 }
215}
Sheng Yang5ee481d2010-05-17 17:22:23 +0800216EXPORT_SYMBOL_GPL(fpu_finit);
Avi Kivity86603282010-05-06 11:45:46 +0300217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/*
219 * The _current_ task is using the FPU for the first time
220 * so initialize it and set the mxcsr to its default
221 * value at reset if we support XMM instructions and then
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300222 * remember the current task has used the FPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 */
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700224int init_fpu(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Avi Kivity86603282010-05-06 11:45:46 +0300226 int ret;
227
Roland McGrath44210112008-01-30 13:31:50 +0100228 if (tsk_used_math(tsk)) {
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700229 if (HAVE_HWFP && tsk == current)
Roland McGrath44210112008-01-30 13:31:50 +0100230 unlazy_fpu(tsk);
Oleg Nesterov089f9fb2012-04-16 22:48:15 +0200231 tsk->thread.fpu.last_cpu = ~0;
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700232 return 0;
233 }
234
235 /*
236 * Memory allocation at the first usage of the FPU and other state.
237 */
Avi Kivity86603282010-05-06 11:45:46 +0300238 ret = fpu_alloc(&tsk->thread.fpu);
239 if (ret)
240 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100241
Avi Kivity86603282010-05-06 11:45:46 +0300242 fpu_finit(&tsk->thread.fpu);
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 set_stopped_child_used_math(tsk);
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700245 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
Avi Kivitye5c30142011-01-11 12:15:54 +0200247EXPORT_SYMBOL_GPL(init_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800249/*
250 * The xstateregs_active() routine is the same as the fpregs_active() routine,
251 * as the "regset->n" for the xstate regset will be updated based on the feature
252 * capabilites supported by the xsave.
253 */
Roland McGrath44210112008-01-30 13:31:50 +0100254int fpregs_active(struct task_struct *target, const struct user_regset *regset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Roland McGrath44210112008-01-30 13:31:50 +0100256 return tsk_used_math(target) ? regset->n : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
Roland McGrath44210112008-01-30 13:31:50 +0100258
259int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
260{
261 return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
262}
263
264int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
265 unsigned int pos, unsigned int count,
266 void *kbuf, void __user *ubuf)
267{
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700268 int ret;
269
Roland McGrath44210112008-01-30 13:31:50 +0100270 if (!cpu_has_fxsr)
271 return -ENODEV;
272
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700273 ret = init_fpu(target);
274 if (ret)
275 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100276
Suresh Siddha29104e12010-07-19 16:05:49 -0700277 sanitize_i387_state(target);
278
Roland McGrath44210112008-01-30 13:31:50 +0100279 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300280 &target->thread.fpu.state->fxsave, 0, -1);
Roland McGrath44210112008-01-30 13:31:50 +0100281}
282
283int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
284 unsigned int pos, unsigned int count,
285 const void *kbuf, const void __user *ubuf)
286{
287 int ret;
288
289 if (!cpu_has_fxsr)
290 return -ENODEV;
291
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700292 ret = init_fpu(target);
293 if (ret)
294 return ret;
295
Suresh Siddha29104e12010-07-19 16:05:49 -0700296 sanitize_i387_state(target);
297
Roland McGrath44210112008-01-30 13:31:50 +0100298 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300299 &target->thread.fpu.state->fxsave, 0, -1);
Roland McGrath44210112008-01-30 13:31:50 +0100300
301 /*
302 * mxcsr reserved bits must be masked to zero for security reasons.
303 */
Avi Kivity86603282010-05-06 11:45:46 +0300304 target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
Roland McGrath44210112008-01-30 13:31:50 +0100305
Suresh Siddha42deec62008-07-29 10:29:26 -0700306 /*
307 * update the header bits in the xsave header, indicating the
308 * presence of FP and SSE state.
309 */
310 if (cpu_has_xsave)
Avi Kivity86603282010-05-06 11:45:46 +0300311 target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FPSSE;
Suresh Siddha42deec62008-07-29 10:29:26 -0700312
Roland McGrath44210112008-01-30 13:31:50 +0100313 return ret;
314}
315
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800316int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
317 unsigned int pos, unsigned int count,
318 void *kbuf, void __user *ubuf)
319{
320 int ret;
321
322 if (!cpu_has_xsave)
323 return -ENODEV;
324
325 ret = init_fpu(target);
326 if (ret)
327 return ret;
328
329 /*
Suresh Siddhaff7fbc72010-02-22 14:51:33 -0800330 * Copy the 48bytes defined by the software first into the xstate
331 * memory layout in the thread struct, so that we can copy the entire
332 * xstateregs to the user using one user_regset_copyout().
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800333 */
Avi Kivity86603282010-05-06 11:45:46 +0300334 memcpy(&target->thread.fpu.state->fxsave.sw_reserved,
Suresh Siddhaff7fbc72010-02-22 14:51:33 -0800335 xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800336
337 /*
Suresh Siddhaff7fbc72010-02-22 14:51:33 -0800338 * Copy the xstate memory layout.
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800339 */
340 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300341 &target->thread.fpu.state->xsave, 0, -1);
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800342 return ret;
343}
344
345int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
346 unsigned int pos, unsigned int count,
347 const void *kbuf, const void __user *ubuf)
348{
349 int ret;
350 struct xsave_hdr_struct *xsave_hdr;
351
352 if (!cpu_has_xsave)
353 return -ENODEV;
354
355 ret = init_fpu(target);
356 if (ret)
357 return ret;
358
359 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300360 &target->thread.fpu.state->xsave, 0, -1);
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800361
362 /*
363 * mxcsr reserved bits must be masked to zero for security reasons.
364 */
Avi Kivity86603282010-05-06 11:45:46 +0300365 target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800366
Avi Kivity86603282010-05-06 11:45:46 +0300367 xsave_hdr = &target->thread.fpu.state->xsave.xsave_hdr;
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800368
369 xsave_hdr->xstate_bv &= pcntxt_mask;
370 /*
371 * These bits must be zero.
372 */
373 xsave_hdr->reserved1[0] = xsave_hdr->reserved1[1] = 0;
374
375 return ret;
376}
377
Roland McGrath44210112008-01-30 13:31:50 +0100378#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380/*
381 * FPU tag word conversions.
382 */
383
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100384static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100389 tmp = ~twd;
Roland McGrath44210112008-01-30 13:31:50 +0100390 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100391 /* and move the valid bits to the lower byte. */
392 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
393 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
394 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
Ingo Molnarf6689642008-03-05 15:37:32 +0100395
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100396 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
Phil Carmody497888c2011-07-14 15:07:13 +0300399#define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16)
Roland McGrath44210112008-01-30 13:31:50 +0100400#define FP_EXP_TAG_VALID 0
401#define FP_EXP_TAG_ZERO 1
402#define FP_EXP_TAG_SPECIAL 2
403#define FP_EXP_TAG_EMPTY 3
404
405static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Roland McGrath44210112008-01-30 13:31:50 +0100407 struct _fpxreg *st;
408 u32 tos = (fxsave->swd >> 11) & 7;
409 u32 twd = (unsigned long) fxsave->twd;
410 u32 tag;
411 u32 ret = 0xffff0000u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 int i;
413
Roland McGrath44210112008-01-30 13:31:50 +0100414 for (i = 0; i < 8; i++, twd >>= 1) {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100415 if (twd & 0x1) {
416 st = FPREG_ADDR(fxsave, (i - tos) & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100418 switch (st->exponent & 0x7fff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 case 0x7fff:
Roland McGrath44210112008-01-30 13:31:50 +0100420 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 break;
422 case 0x0000:
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100423 if (!st->significand[0] &&
424 !st->significand[1] &&
425 !st->significand[2] &&
Roland McGrath44210112008-01-30 13:31:50 +0100426 !st->significand[3])
427 tag = FP_EXP_TAG_ZERO;
428 else
429 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 break;
431 default:
Roland McGrath44210112008-01-30 13:31:50 +0100432 if (st->significand[3] & 0x8000)
433 tag = FP_EXP_TAG_VALID;
434 else
435 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 break;
437 }
438 } else {
Roland McGrath44210112008-01-30 13:31:50 +0100439 tag = FP_EXP_TAG_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
Roland McGrath44210112008-01-30 13:31:50 +0100441 ret |= tag << (2 * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443 return ret;
444}
445
446/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 * FXSR floating point environment conversions.
448 */
449
Suresh Siddha72a671c2012-07-24 16:05:29 -0700450void
Ingo Molnarf6689642008-03-05 15:37:32 +0100451convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Avi Kivity86603282010-05-06 11:45:46 +0300453 struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100454 struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
455 struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 int i;
457
Roland McGrath44210112008-01-30 13:31:50 +0100458 env->cwd = fxsave->cwd | 0xffff0000u;
459 env->swd = fxsave->swd | 0xffff0000u;
460 env->twd = twd_fxsr_to_i387(fxsave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Roland McGrath44210112008-01-30 13:31:50 +0100462#ifdef CONFIG_X86_64
463 env->fip = fxsave->rip;
464 env->foo = fxsave->rdp;
Brian Gerst10c11f32010-09-03 21:17:13 -0400465 /*
466 * should be actually ds/cs at fpu exception time, but
467 * that information is not available in 64bit mode.
468 */
469 env->fcs = task_pt_regs(tsk)->cs;
Roland McGrath44210112008-01-30 13:31:50 +0100470 if (tsk == current) {
Brian Gerst10c11f32010-09-03 21:17:13 -0400471 savesegment(ds, env->fos);
Roland McGrath44210112008-01-30 13:31:50 +0100472 } else {
Brian Gerst10c11f32010-09-03 21:17:13 -0400473 env->fos = tsk->thread.ds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
Brian Gerst10c11f32010-09-03 21:17:13 -0400475 env->fos |= 0xffff0000;
Roland McGrath44210112008-01-30 13:31:50 +0100476#else
477 env->fip = fxsave->fip;
Jan Beulich609b5292008-03-05 08:35:14 +0000478 env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
Roland McGrath44210112008-01-30 13:31:50 +0100479 env->foo = fxsave->foo;
480 env->fos = fxsave->fos;
481#endif
482
483 for (i = 0; i < 8; ++i)
484 memcpy(&to[i], &from[i], sizeof(to[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Suresh Siddha72a671c2012-07-24 16:05:29 -0700487void convert_to_fxsr(struct task_struct *tsk,
488 const struct user_i387_ia32_struct *env)
Roland McGrath44210112008-01-30 13:31:50 +0100489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Avi Kivity86603282010-05-06 11:45:46 +0300491 struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100492 struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
493 struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 int i;
495
Roland McGrath44210112008-01-30 13:31:50 +0100496 fxsave->cwd = env->cwd;
497 fxsave->swd = env->swd;
498 fxsave->twd = twd_i387_to_fxsr(env->twd);
499 fxsave->fop = (u16) ((u32) env->fcs >> 16);
500#ifdef CONFIG_X86_64
501 fxsave->rip = env->fip;
502 fxsave->rdp = env->foo;
503 /* cs and ds ignored */
504#else
505 fxsave->fip = env->fip;
506 fxsave->fcs = (env->fcs & 0xffff);
507 fxsave->foo = env->foo;
508 fxsave->fos = env->fos;
509#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Roland McGrath44210112008-01-30 13:31:50 +0100511 for (i = 0; i < 8; ++i)
512 memcpy(&to[i], &from[i], sizeof(from[0]));
513}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Roland McGrath44210112008-01-30 13:31:50 +0100515int fpregs_get(struct task_struct *target, const struct user_regset *regset,
516 unsigned int pos, unsigned int count,
517 void *kbuf, void __user *ubuf)
518{
519 struct user_i387_ia32_struct env;
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700520 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700522 ret = init_fpu(target);
523 if (ret)
524 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100525
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700526 if (!HAVE_HWFP)
527 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
528
Ingo Molnarf6689642008-03-05 15:37:32 +0100529 if (!cpu_has_fxsr) {
Roland McGrath44210112008-01-30 13:31:50 +0100530 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300531 &target->thread.fpu.state->fsave, 0,
Suresh Siddha61c46282008-03-10 15:28:04 -0700532 -1);
Ingo Molnarf6689642008-03-05 15:37:32 +0100533 }
Roland McGrath44210112008-01-30 13:31:50 +0100534
Suresh Siddha29104e12010-07-19 16:05:49 -0700535 sanitize_i387_state(target);
536
Roland McGrath44210112008-01-30 13:31:50 +0100537 if (kbuf && pos == 0 && count == sizeof(env)) {
538 convert_from_fxsr(kbuf, target);
539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Roland McGrath44210112008-01-30 13:31:50 +0100541
542 convert_from_fxsr(&env, target);
Ingo Molnarf6689642008-03-05 15:37:32 +0100543
Roland McGrath44210112008-01-30 13:31:50 +0100544 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
545}
546
547int fpregs_set(struct task_struct *target, const struct user_regset *regset,
548 unsigned int pos, unsigned int count,
549 const void *kbuf, const void __user *ubuf)
550{
551 struct user_i387_ia32_struct env;
552 int ret;
553
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700554 ret = init_fpu(target);
555 if (ret)
556 return ret;
557
Suresh Siddha29104e12010-07-19 16:05:49 -0700558 sanitize_i387_state(target);
559
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700560 if (!HAVE_HWFP)
561 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
562
Ingo Molnarf6689642008-03-05 15:37:32 +0100563 if (!cpu_has_fxsr) {
Roland McGrath44210112008-01-30 13:31:50 +0100564 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300565 &target->thread.fpu.state->fsave, 0, -1);
Ingo Molnarf6689642008-03-05 15:37:32 +0100566 }
Roland McGrath44210112008-01-30 13:31:50 +0100567
568 if (pos > 0 || count < sizeof(env))
569 convert_from_fxsr(&env, target);
570
571 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
572 if (!ret)
573 convert_to_fxsr(target, &env);
574
Suresh Siddha42deec62008-07-29 10:29:26 -0700575 /*
576 * update the header bit in the xsave header, indicating the
577 * presence of FP.
578 */
579 if (cpu_has_xsave)
Avi Kivity86603282010-05-06 11:45:46 +0300580 target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FP;
Roland McGrath44210112008-01-30 13:31:50 +0100581 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
584/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 * FPU state for core dumps.
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100586 * This is only used for a.out dumps now.
587 * It is declared generically using elf_fpregset_t (which is
588 * struct user_i387_struct) but is in fact only used for 32-bit
589 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100591int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 struct task_struct *tsk = current;
Ingo Molnarf6689642008-03-05 15:37:32 +0100594 int fpvalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 fpvalid = !!used_math();
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100597 if (fpvalid)
598 fpvalid = !fpregs_get(tsk, NULL,
599 0, sizeof(struct user_i387_ia32_struct),
600 fpu, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 return fpvalid;
603}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700604EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100606#endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */