blob: 6782e3983865c4daf1be142705e23072bdaae4d5 [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 Siddha5d2bd702012-09-06 14:58:52 -070025 * For now, with eagerfpu we will return interrupted kernel FPU
26 * state as not-idle. TBD: Ideally we can change the return value
Suresh Siddha304bced2012-08-24 14:13:02 -070027 * to something like __thread_has_fpu(current). But we need to
28 * be careful of doing __thread_clear_has_fpu() before saving
29 * the FPU etc for supporting nested uses etc. For now, take
30 * the simple route!
31 *
32 * On others, we can do a kernel_fpu_begin/end() pair *ONLY* if that
Linus Torvalds8546c002012-02-21 10:25:45 -080033 * pair does nothing at all: the thread must not have fpu (so
34 * that we don't try to save the FPU state), and TS must
35 * be set (so that the clts/stts pair does nothing that is
36 * visible in the interrupted kernel thread).
37 */
38static inline bool interrupted_kernel_fpu_idle(void)
39{
Suresh Siddha5d2bd702012-09-06 14:58:52 -070040 if (use_eager_fpu())
Suresh Siddha304bced2012-08-24 14:13:02 -070041 return 0;
42
Linus Torvalds8546c002012-02-21 10:25:45 -080043 return !__thread_has_fpu(current) &&
44 (read_cr0() & X86_CR0_TS);
45}
46
47/*
48 * Were we in user mode (or vm86 mode) when we were
49 * interrupted?
50 *
51 * Doing kernel_fpu_begin/end() is ok if we are running
52 * in an interrupt context from user mode - we'll just
53 * save the FPU state as required.
54 */
55static inline bool interrupted_user_mode(void)
56{
57 struct pt_regs *regs = get_irq_regs();
58 return regs && user_mode_vm(regs);
59}
60
61/*
62 * Can we use the FPU in kernel mode with the
63 * whole "kernel_fpu_begin/end()" sequence?
64 *
65 * It's always ok in process context (ie "not interrupt")
66 * but it is sometimes ok even from an irq.
67 */
68bool irq_fpu_usable(void)
69{
70 return !in_interrupt() ||
71 interrupted_user_mode() ||
72 interrupted_kernel_fpu_idle();
73}
74EXPORT_SYMBOL(irq_fpu_usable);
75
76void kernel_fpu_begin(void)
77{
78 struct task_struct *me = current;
79
80 WARN_ON_ONCE(!irq_fpu_usable());
81 preempt_disable();
82 if (__thread_has_fpu(me)) {
83 __save_init_fpu(me);
84 __thread_clear_has_fpu(me);
85 /* We do 'stts()' in kernel_fpu_end() */
Suresh Siddha5d2bd702012-09-06 14:58:52 -070086 } else if (!use_eager_fpu()) {
Alex Shic6ae41e2012-05-11 15:35:27 +080087 this_cpu_write(fpu_owner_task, NULL);
Linus Torvalds8546c002012-02-21 10:25:45 -080088 clts();
89 }
90}
91EXPORT_SYMBOL(kernel_fpu_begin);
92
93void kernel_fpu_end(void)
94{
Suresh Siddha5d2bd702012-09-06 14:58:52 -070095 if (use_eager_fpu())
Suresh Siddha304bced2012-08-24 14:13:02 -070096 math_state_restore();
97 else
98 stts();
Linus Torvalds8546c002012-02-21 10:25:45 -080099 preempt_enable();
100}
101EXPORT_SYMBOL(kernel_fpu_end);
102
103void unlazy_fpu(struct task_struct *tsk)
104{
105 preempt_disable();
106 if (__thread_has_fpu(tsk)) {
107 __save_init_fpu(tsk);
108 __thread_fpu_end(tsk);
109 } else
110 tsk->fpu_counter = 0;
111 preempt_enable();
112}
113EXPORT_SYMBOL(unlazy_fpu);
114
Suresh Siddha72a671c2012-07-24 16:05:29 -0700115unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
Suresh Siddha61c46282008-03-10 15:28:04 -0700116unsigned int xstate_size;
Xiaotian Fengf45755b2010-08-13 15:19:11 +0800117EXPORT_SYMBOL_GPL(xstate_size);
Suresh Siddha61c46282008-03-10 15:28:04 -0700118static struct i387_fxsave_struct fx_scratch __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Linus Torvalds1361b832012-02-21 13:19:22 -0800120static void __cpuinit mxcsr_feature_mask_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 unsigned long mask = 0;
Ingo Molnarf6689642008-03-05 15:37:32 +0100123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700125 memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct));
126 asm volatile("fxsave %0" : : "m" (fx_scratch));
127 mask = fx_scratch.mxcsr_mask;
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100128 if (mask == 0)
129 mask = 0x0000ffbf;
130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 mxcsr_feature_mask &= mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Robert Richter0e49bf62010-07-21 19:03:52 +0200134static void __cpuinit init_thread_xstate(void)
Suresh Siddha61c46282008-03-10 15:28:04 -0700135{
Robert Richter0e49bf62010-07-21 19:03:52 +0200136 /*
137 * Note that xstate_size might be overwriten later during
138 * xsave_init().
139 */
140
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700141 if (!HAVE_HWFP) {
Robert Richter1f999ab2010-07-21 19:03:57 +0200142 /*
143 * Disable xsave as we do not support it if i387
144 * emulation is enabled.
145 */
146 setup_clear_cpu_cap(X86_FEATURE_XSAVE);
147 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700148 xstate_size = sizeof(struct i387_soft_struct);
149 return;
150 }
151
Suresh Siddha61c46282008-03-10 15:28:04 -0700152 if (cpu_has_fxsr)
153 xstate_size = sizeof(struct i387_fxsave_struct);
Suresh Siddha61c46282008-03-10 15:28:04 -0700154 else
155 xstate_size = sizeof(struct i387_fsave_struct);
Suresh Siddha61c46282008-03-10 15:28:04 -0700156}
157
Roland McGrath44210112008-01-30 13:31:50 +0100158/*
159 * Called at bootup to set up the initial FPU state that is later cloned
160 * into all processes.
161 */
Robert Richter0e49bf62010-07-21 19:03:52 +0200162
Roland McGrath44210112008-01-30 13:31:50 +0100163void __cpuinit fpu_init(void)
164{
Brian Gerst6ac8bac2010-09-03 21:17:09 -0400165 unsigned long cr0;
166 unsigned long cr4_mask = 0;
Ingo Molnarf6689642008-03-05 15:37:32 +0100167
Brian Gerst6ac8bac2010-09-03 21:17:09 -0400168 if (cpu_has_fxsr)
169 cr4_mask |= X86_CR4_OSFXSR;
170 if (cpu_has_xmm)
171 cr4_mask |= X86_CR4_OSXMMEXCPT;
172 if (cr4_mask)
173 set_in_cr4(cr4_mask);
Roland McGrath44210112008-01-30 13:31:50 +0100174
Brian Gerst6ac8bac2010-09-03 21:17:09 -0400175 cr0 = read_cr0();
176 cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
177 if (!HAVE_HWFP)
178 cr0 |= X86_CR0_EM;
179 write_cr0(cr0);
Roland McGrath44210112008-01-30 13:31:50 +0100180
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700181 if (!smp_processor_id())
182 init_thread_xstate();
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700183
Roland McGrath44210112008-01-30 13:31:50 +0100184 mxcsr_feature_mask_init();
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700185 xsave_init();
186 eager_fpu_init();
Roland McGrath44210112008-01-30 13:31:50 +0100187}
Robert Richter0e49bf62010-07-21 19:03:52 +0200188
Sheng Yang5ee481d2010-05-17 17:22:23 +0800189void fpu_finit(struct fpu *fpu)
Avi Kivity86603282010-05-06 11:45:46 +0300190{
Avi Kivity86603282010-05-06 11:45:46 +0300191 if (!HAVE_HWFP) {
192 finit_soft_fpu(&fpu->state->soft);
193 return;
194 }
Avi Kivity86603282010-05-06 11:45:46 +0300195
196 if (cpu_has_fxsr) {
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700197 fx_finit(&fpu->state->fxsave);
Avi Kivity86603282010-05-06 11:45:46 +0300198 } else {
199 struct i387_fsave_struct *fp = &fpu->state->fsave;
200 memset(fp, 0, xstate_size);
201 fp->cwd = 0xffff037fu;
202 fp->swd = 0xffff0000u;
203 fp->twd = 0xffffffffu;
204 fp->fos = 0xffff0000u;
205 }
206}
Sheng Yang5ee481d2010-05-17 17:22:23 +0800207EXPORT_SYMBOL_GPL(fpu_finit);
Avi Kivity86603282010-05-06 11:45:46 +0300208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
210 * The _current_ task is using the FPU for the first time
211 * so initialize it and set the mxcsr to its default
212 * value at reset if we support XMM instructions and then
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300213 * remember the current task has used the FPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700215int init_fpu(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Avi Kivity86603282010-05-06 11:45:46 +0300217 int ret;
218
Roland McGrath44210112008-01-30 13:31:50 +0100219 if (tsk_used_math(tsk)) {
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700220 if (HAVE_HWFP && tsk == current)
Roland McGrath44210112008-01-30 13:31:50 +0100221 unlazy_fpu(tsk);
Oleg Nesterov089f9fb2012-04-16 22:48:15 +0200222 tsk->thread.fpu.last_cpu = ~0;
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700223 return 0;
224 }
225
226 /*
227 * Memory allocation at the first usage of the FPU and other state.
228 */
Avi Kivity86603282010-05-06 11:45:46 +0300229 ret = fpu_alloc(&tsk->thread.fpu);
230 if (ret)
231 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100232
Avi Kivity86603282010-05-06 11:45:46 +0300233 fpu_finit(&tsk->thread.fpu);
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 set_stopped_child_used_math(tsk);
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
Avi Kivitye5c30142011-01-11 12:15:54 +0200238EXPORT_SYMBOL_GPL(init_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800240/*
241 * The xstateregs_active() routine is the same as the fpregs_active() routine,
242 * as the "regset->n" for the xstate regset will be updated based on the feature
243 * capabilites supported by the xsave.
244 */
Roland McGrath44210112008-01-30 13:31:50 +0100245int fpregs_active(struct task_struct *target, const struct user_regset *regset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Roland McGrath44210112008-01-30 13:31:50 +0100247 return tsk_used_math(target) ? regset->n : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
Roland McGrath44210112008-01-30 13:31:50 +0100249
250int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
251{
252 return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
253}
254
255int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
256 unsigned int pos, unsigned int count,
257 void *kbuf, void __user *ubuf)
258{
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700259 int ret;
260
Roland McGrath44210112008-01-30 13:31:50 +0100261 if (!cpu_has_fxsr)
262 return -ENODEV;
263
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700264 ret = init_fpu(target);
265 if (ret)
266 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100267
Suresh Siddha29104e12010-07-19 16:05:49 -0700268 sanitize_i387_state(target);
269
Roland McGrath44210112008-01-30 13:31:50 +0100270 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300271 &target->thread.fpu.state->fxsave, 0, -1);
Roland McGrath44210112008-01-30 13:31:50 +0100272}
273
274int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
275 unsigned int pos, unsigned int count,
276 const void *kbuf, const void __user *ubuf)
277{
278 int ret;
279
280 if (!cpu_has_fxsr)
281 return -ENODEV;
282
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700283 ret = init_fpu(target);
284 if (ret)
285 return ret;
286
Suresh Siddha29104e12010-07-19 16:05:49 -0700287 sanitize_i387_state(target);
288
Roland McGrath44210112008-01-30 13:31:50 +0100289 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300290 &target->thread.fpu.state->fxsave, 0, -1);
Roland McGrath44210112008-01-30 13:31:50 +0100291
292 /*
293 * mxcsr reserved bits must be masked to zero for security reasons.
294 */
Avi Kivity86603282010-05-06 11:45:46 +0300295 target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
Roland McGrath44210112008-01-30 13:31:50 +0100296
Suresh Siddha42deec62008-07-29 10:29:26 -0700297 /*
298 * update the header bits in the xsave header, indicating the
299 * presence of FP and SSE state.
300 */
301 if (cpu_has_xsave)
Avi Kivity86603282010-05-06 11:45:46 +0300302 target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FPSSE;
Suresh Siddha42deec62008-07-29 10:29:26 -0700303
Roland McGrath44210112008-01-30 13:31:50 +0100304 return ret;
305}
306
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800307int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
308 unsigned int pos, unsigned int count,
309 void *kbuf, void __user *ubuf)
310{
311 int ret;
312
313 if (!cpu_has_xsave)
314 return -ENODEV;
315
316 ret = init_fpu(target);
317 if (ret)
318 return ret;
319
320 /*
Suresh Siddhaff7fbc72010-02-22 14:51:33 -0800321 * Copy the 48bytes defined by the software first into the xstate
322 * memory layout in the thread struct, so that we can copy the entire
323 * xstateregs to the user using one user_regset_copyout().
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800324 */
Avi Kivity86603282010-05-06 11:45:46 +0300325 memcpy(&target->thread.fpu.state->fxsave.sw_reserved,
Suresh Siddhaff7fbc72010-02-22 14:51:33 -0800326 xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800327
328 /*
Suresh Siddhaff7fbc72010-02-22 14:51:33 -0800329 * Copy the xstate memory layout.
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800330 */
331 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300332 &target->thread.fpu.state->xsave, 0, -1);
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800333 return ret;
334}
335
336int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
337 unsigned int pos, unsigned int count,
338 const void *kbuf, const void __user *ubuf)
339{
340 int ret;
341 struct xsave_hdr_struct *xsave_hdr;
342
343 if (!cpu_has_xsave)
344 return -ENODEV;
345
346 ret = init_fpu(target);
347 if (ret)
348 return ret;
349
350 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300351 &target->thread.fpu.state->xsave, 0, -1);
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800352
353 /*
354 * mxcsr reserved bits must be masked to zero for security reasons.
355 */
Avi Kivity86603282010-05-06 11:45:46 +0300356 target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800357
Avi Kivity86603282010-05-06 11:45:46 +0300358 xsave_hdr = &target->thread.fpu.state->xsave.xsave_hdr;
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800359
360 xsave_hdr->xstate_bv &= pcntxt_mask;
361 /*
362 * These bits must be zero.
363 */
364 xsave_hdr->reserved1[0] = xsave_hdr->reserved1[1] = 0;
365
366 return ret;
367}
368
Roland McGrath44210112008-01-30 13:31:50 +0100369#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371/*
372 * FPU tag word conversions.
373 */
374
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100375static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100380 tmp = ~twd;
Roland McGrath44210112008-01-30 13:31:50 +0100381 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100382 /* and move the valid bits to the lower byte. */
383 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
384 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
385 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
Ingo Molnarf6689642008-03-05 15:37:32 +0100386
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100387 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Phil Carmody497888c2011-07-14 15:07:13 +0300390#define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16)
Roland McGrath44210112008-01-30 13:31:50 +0100391#define FP_EXP_TAG_VALID 0
392#define FP_EXP_TAG_ZERO 1
393#define FP_EXP_TAG_SPECIAL 2
394#define FP_EXP_TAG_EMPTY 3
395
396static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Roland McGrath44210112008-01-30 13:31:50 +0100398 struct _fpxreg *st;
399 u32 tos = (fxsave->swd >> 11) & 7;
400 u32 twd = (unsigned long) fxsave->twd;
401 u32 tag;
402 u32 ret = 0xffff0000u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 int i;
404
Roland McGrath44210112008-01-30 13:31:50 +0100405 for (i = 0; i < 8; i++, twd >>= 1) {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100406 if (twd & 0x1) {
407 st = FPREG_ADDR(fxsave, (i - tos) & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100409 switch (st->exponent & 0x7fff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 case 0x7fff:
Roland McGrath44210112008-01-30 13:31:50 +0100411 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 break;
413 case 0x0000:
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100414 if (!st->significand[0] &&
415 !st->significand[1] &&
416 !st->significand[2] &&
Roland McGrath44210112008-01-30 13:31:50 +0100417 !st->significand[3])
418 tag = FP_EXP_TAG_ZERO;
419 else
420 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 break;
422 default:
Roland McGrath44210112008-01-30 13:31:50 +0100423 if (st->significand[3] & 0x8000)
424 tag = FP_EXP_TAG_VALID;
425 else
426 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 break;
428 }
429 } else {
Roland McGrath44210112008-01-30 13:31:50 +0100430 tag = FP_EXP_TAG_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
Roland McGrath44210112008-01-30 13:31:50 +0100432 ret |= tag << (2 * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434 return ret;
435}
436
437/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 * FXSR floating point environment conversions.
439 */
440
Suresh Siddha72a671c2012-07-24 16:05:29 -0700441void
Ingo Molnarf6689642008-03-05 15:37:32 +0100442convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Avi Kivity86603282010-05-06 11:45:46 +0300444 struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100445 struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
446 struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 int i;
448
Roland McGrath44210112008-01-30 13:31:50 +0100449 env->cwd = fxsave->cwd | 0xffff0000u;
450 env->swd = fxsave->swd | 0xffff0000u;
451 env->twd = twd_fxsr_to_i387(fxsave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Roland McGrath44210112008-01-30 13:31:50 +0100453#ifdef CONFIG_X86_64
454 env->fip = fxsave->rip;
455 env->foo = fxsave->rdp;
Brian Gerst10c11f32010-09-03 21:17:13 -0400456 /*
457 * should be actually ds/cs at fpu exception time, but
458 * that information is not available in 64bit mode.
459 */
460 env->fcs = task_pt_regs(tsk)->cs;
Roland McGrath44210112008-01-30 13:31:50 +0100461 if (tsk == current) {
Brian Gerst10c11f32010-09-03 21:17:13 -0400462 savesegment(ds, env->fos);
Roland McGrath44210112008-01-30 13:31:50 +0100463 } else {
Brian Gerst10c11f32010-09-03 21:17:13 -0400464 env->fos = tsk->thread.ds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Brian Gerst10c11f32010-09-03 21:17:13 -0400466 env->fos |= 0xffff0000;
Roland McGrath44210112008-01-30 13:31:50 +0100467#else
468 env->fip = fxsave->fip;
Jan Beulich609b5292008-03-05 08:35:14 +0000469 env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
Roland McGrath44210112008-01-30 13:31:50 +0100470 env->foo = fxsave->foo;
471 env->fos = fxsave->fos;
472#endif
473
474 for (i = 0; i < 8; ++i)
475 memcpy(&to[i], &from[i], sizeof(to[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Suresh Siddha72a671c2012-07-24 16:05:29 -0700478void convert_to_fxsr(struct task_struct *tsk,
479 const struct user_i387_ia32_struct *env)
Roland McGrath44210112008-01-30 13:31:50 +0100480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Avi Kivity86603282010-05-06 11:45:46 +0300482 struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100483 struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
484 struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int i;
486
Roland McGrath44210112008-01-30 13:31:50 +0100487 fxsave->cwd = env->cwd;
488 fxsave->swd = env->swd;
489 fxsave->twd = twd_i387_to_fxsr(env->twd);
490 fxsave->fop = (u16) ((u32) env->fcs >> 16);
491#ifdef CONFIG_X86_64
492 fxsave->rip = env->fip;
493 fxsave->rdp = env->foo;
494 /* cs and ds ignored */
495#else
496 fxsave->fip = env->fip;
497 fxsave->fcs = (env->fcs & 0xffff);
498 fxsave->foo = env->foo;
499 fxsave->fos = env->fos;
500#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Roland McGrath44210112008-01-30 13:31:50 +0100502 for (i = 0; i < 8; ++i)
503 memcpy(&to[i], &from[i], sizeof(from[0]));
504}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Roland McGrath44210112008-01-30 13:31:50 +0100506int fpregs_get(struct task_struct *target, const struct user_regset *regset,
507 unsigned int pos, unsigned int count,
508 void *kbuf, void __user *ubuf)
509{
510 struct user_i387_ia32_struct env;
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700511 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700513 ret = init_fpu(target);
514 if (ret)
515 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100516
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700517 if (!HAVE_HWFP)
518 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
519
Ingo Molnarf6689642008-03-05 15:37:32 +0100520 if (!cpu_has_fxsr) {
Roland McGrath44210112008-01-30 13:31:50 +0100521 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300522 &target->thread.fpu.state->fsave, 0,
Suresh Siddha61c46282008-03-10 15:28:04 -0700523 -1);
Ingo Molnarf6689642008-03-05 15:37:32 +0100524 }
Roland McGrath44210112008-01-30 13:31:50 +0100525
Suresh Siddha29104e12010-07-19 16:05:49 -0700526 sanitize_i387_state(target);
527
Roland McGrath44210112008-01-30 13:31:50 +0100528 if (kbuf && pos == 0 && count == sizeof(env)) {
529 convert_from_fxsr(kbuf, target);
530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Roland McGrath44210112008-01-30 13:31:50 +0100532
533 convert_from_fxsr(&env, target);
Ingo Molnarf6689642008-03-05 15:37:32 +0100534
Roland McGrath44210112008-01-30 13:31:50 +0100535 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
536}
537
538int fpregs_set(struct task_struct *target, const struct user_regset *regset,
539 unsigned int pos, unsigned int count,
540 const void *kbuf, const void __user *ubuf)
541{
542 struct user_i387_ia32_struct env;
543 int ret;
544
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700545 ret = init_fpu(target);
546 if (ret)
547 return ret;
548
Suresh Siddha29104e12010-07-19 16:05:49 -0700549 sanitize_i387_state(target);
550
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700551 if (!HAVE_HWFP)
552 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
553
Ingo Molnarf6689642008-03-05 15:37:32 +0100554 if (!cpu_has_fxsr) {
Roland McGrath44210112008-01-30 13:31:50 +0100555 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300556 &target->thread.fpu.state->fsave, 0, -1);
Ingo Molnarf6689642008-03-05 15:37:32 +0100557 }
Roland McGrath44210112008-01-30 13:31:50 +0100558
559 if (pos > 0 || count < sizeof(env))
560 convert_from_fxsr(&env, target);
561
562 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
563 if (!ret)
564 convert_to_fxsr(target, &env);
565
Suresh Siddha42deec62008-07-29 10:29:26 -0700566 /*
567 * update the header bit in the xsave header, indicating the
568 * presence of FP.
569 */
570 if (cpu_has_xsave)
Avi Kivity86603282010-05-06 11:45:46 +0300571 target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FP;
Roland McGrath44210112008-01-30 13:31:50 +0100572 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
575/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 * FPU state for core dumps.
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100577 * This is only used for a.out dumps now.
578 * It is declared generically using elf_fpregset_t (which is
579 * struct user_i387_struct) but is in fact only used for 32-bit
580 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100582int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 struct task_struct *tsk = current;
Ingo Molnarf6689642008-03-05 15:37:32 +0100585 int fpvalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 fpvalid = !!used_math();
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100588 if (fpvalid)
589 fpvalid = !fpregs_get(tsk, NULL,
590 0, sizeof(struct user_i387_ia32_struct),
591 fpu, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 return fpvalid;
594}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700595EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100597#endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */