blob: 245a71db401af0e50be0deeba56d680c278ca2fc [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
Suresh Siddhab1a74bf2012-09-20 11:01:49 -070076void __kernel_fpu_begin(void)
Linus Torvalds8546c002012-02-21 10:25:45 -080077{
78 struct task_struct *me = current;
79
Linus Torvalds8546c002012-02-21 10:25:45 -080080 if (__thread_has_fpu(me)) {
81 __save_init_fpu(me);
82 __thread_clear_has_fpu(me);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -070083 /* We do 'stts()' in __kernel_fpu_end() */
Suresh Siddha5d2bd702012-09-06 14:58:52 -070084 } else if (!use_eager_fpu()) {
Alex Shic6ae41e2012-05-11 15:35:27 +080085 this_cpu_write(fpu_owner_task, NULL);
Linus Torvalds8546c002012-02-21 10:25:45 -080086 clts();
87 }
88}
Suresh Siddhab1a74bf2012-09-20 11:01:49 -070089EXPORT_SYMBOL(__kernel_fpu_begin);
Linus Torvalds8546c002012-02-21 10:25:45 -080090
Suresh Siddhab1a74bf2012-09-20 11:01:49 -070091void __kernel_fpu_end(void)
Linus Torvalds8546c002012-02-21 10:25:45 -080092{
Suresh Siddha5d2bd702012-09-06 14:58:52 -070093 if (use_eager_fpu())
Suresh Siddha304bced2012-08-24 14:13:02 -070094 math_state_restore();
95 else
96 stts();
Linus Torvalds8546c002012-02-21 10:25:45 -080097}
Suresh Siddhab1a74bf2012-09-20 11:01:49 -070098EXPORT_SYMBOL(__kernel_fpu_end);
Linus Torvalds8546c002012-02-21 10:25:45 -080099
100void unlazy_fpu(struct task_struct *tsk)
101{
102 preempt_disable();
103 if (__thread_has_fpu(tsk)) {
104 __save_init_fpu(tsk);
105 __thread_fpu_end(tsk);
106 } else
107 tsk->fpu_counter = 0;
108 preempt_enable();
109}
110EXPORT_SYMBOL(unlazy_fpu);
111
Suresh Siddha72a671c2012-07-24 16:05:29 -0700112unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
Suresh Siddha61c46282008-03-10 15:28:04 -0700113unsigned int xstate_size;
Xiaotian Fengf45755b2010-08-13 15:19:11 +0800114EXPORT_SYMBOL_GPL(xstate_size);
Suresh Siddha61c46282008-03-10 15:28:04 -0700115static struct i387_fxsave_struct fx_scratch __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Linus Torvalds1361b832012-02-21 13:19:22 -0800117static void __cpuinit mxcsr_feature_mask_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 unsigned long mask = 0;
Ingo Molnarf6689642008-03-05 15:37:32 +0100120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700122 memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct));
123 asm volatile("fxsave %0" : : "m" (fx_scratch));
124 mask = fx_scratch.mxcsr_mask;
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100125 if (mask == 0)
126 mask = 0x0000ffbf;
127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 mxcsr_feature_mask &= mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Robert Richter0e49bf62010-07-21 19:03:52 +0200131static void __cpuinit init_thread_xstate(void)
Suresh Siddha61c46282008-03-10 15:28:04 -0700132{
Robert Richter0e49bf62010-07-21 19:03:52 +0200133 /*
134 * Note that xstate_size might be overwriten later during
135 * xsave_init().
136 */
137
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700138 if (!HAVE_HWFP) {
Robert Richter1f999ab2010-07-21 19:03:57 +0200139 /*
140 * Disable xsave as we do not support it if i387
141 * emulation is enabled.
142 */
143 setup_clear_cpu_cap(X86_FEATURE_XSAVE);
144 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700145 xstate_size = sizeof(struct i387_soft_struct);
146 return;
147 }
148
Suresh Siddha61c46282008-03-10 15:28:04 -0700149 if (cpu_has_fxsr)
150 xstate_size = sizeof(struct i387_fxsave_struct);
Suresh Siddha61c46282008-03-10 15:28:04 -0700151 else
152 xstate_size = sizeof(struct i387_fsave_struct);
Suresh Siddha61c46282008-03-10 15:28:04 -0700153}
154
Roland McGrath44210112008-01-30 13:31:50 +0100155/*
156 * Called at bootup to set up the initial FPU state that is later cloned
157 * into all processes.
158 */
Robert Richter0e49bf62010-07-21 19:03:52 +0200159
Roland McGrath44210112008-01-30 13:31:50 +0100160void __cpuinit fpu_init(void)
161{
Brian Gerst6ac8bac2010-09-03 21:17:09 -0400162 unsigned long cr0;
163 unsigned long cr4_mask = 0;
Ingo Molnarf6689642008-03-05 15:37:32 +0100164
Brian Gerst6ac8bac2010-09-03 21:17:09 -0400165 if (cpu_has_fxsr)
166 cr4_mask |= X86_CR4_OSFXSR;
167 if (cpu_has_xmm)
168 cr4_mask |= X86_CR4_OSXMMEXCPT;
169 if (cr4_mask)
170 set_in_cr4(cr4_mask);
Roland McGrath44210112008-01-30 13:31:50 +0100171
Brian Gerst6ac8bac2010-09-03 21:17:09 -0400172 cr0 = read_cr0();
173 cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
174 if (!HAVE_HWFP)
175 cr0 |= X86_CR0_EM;
176 write_cr0(cr0);
Roland McGrath44210112008-01-30 13:31:50 +0100177
Fenghua Yu6f5298c2012-11-13 11:32:50 -0800178 /*
179 * init_thread_xstate is only called once to avoid overriding
180 * xstate_size during boot time or during CPU hotplug.
181 */
182 if (xstate_size == 0)
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700183 init_thread_xstate();
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700184
Roland McGrath44210112008-01-30 13:31:50 +0100185 mxcsr_feature_mask_init();
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700186 xsave_init();
187 eager_fpu_init();
Roland McGrath44210112008-01-30 13:31:50 +0100188}
Robert Richter0e49bf62010-07-21 19:03:52 +0200189
Sheng Yang5ee481d2010-05-17 17:22:23 +0800190void fpu_finit(struct fpu *fpu)
Avi Kivity86603282010-05-06 11:45:46 +0300191{
Avi Kivity86603282010-05-06 11:45:46 +0300192 if (!HAVE_HWFP) {
193 finit_soft_fpu(&fpu->state->soft);
194 return;
195 }
Avi Kivity86603282010-05-06 11:45:46 +0300196
197 if (cpu_has_fxsr) {
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700198 fx_finit(&fpu->state->fxsave);
Avi Kivity86603282010-05-06 11:45:46 +0300199 } else {
200 struct i387_fsave_struct *fp = &fpu->state->fsave;
201 memset(fp, 0, xstate_size);
202 fp->cwd = 0xffff037fu;
203 fp->swd = 0xffff0000u;
204 fp->twd = 0xffffffffu;
205 fp->fos = 0xffff0000u;
206 }
207}
Sheng Yang5ee481d2010-05-17 17:22:23 +0800208EXPORT_SYMBOL_GPL(fpu_finit);
Avi Kivity86603282010-05-06 11:45:46 +0300209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*
211 * The _current_ task is using the FPU for the first time
212 * so initialize it and set the mxcsr to its default
213 * value at reset if we support XMM instructions and then
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300214 * remember the current task has used the FPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700216int init_fpu(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Avi Kivity86603282010-05-06 11:45:46 +0300218 int ret;
219
Roland McGrath44210112008-01-30 13:31:50 +0100220 if (tsk_used_math(tsk)) {
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700221 if (HAVE_HWFP && tsk == current)
Roland McGrath44210112008-01-30 13:31:50 +0100222 unlazy_fpu(tsk);
Oleg Nesterov089f9fb2012-04-16 22:48:15 +0200223 tsk->thread.fpu.last_cpu = ~0;
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700224 return 0;
225 }
226
227 /*
228 * Memory allocation at the first usage of the FPU and other state.
229 */
Avi Kivity86603282010-05-06 11:45:46 +0300230 ret = fpu_alloc(&tsk->thread.fpu);
231 if (ret)
232 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100233
Avi Kivity86603282010-05-06 11:45:46 +0300234 fpu_finit(&tsk->thread.fpu);
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 set_stopped_child_used_math(tsk);
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
Avi Kivitye5c30142011-01-11 12:15:54 +0200239EXPORT_SYMBOL_GPL(init_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800241/*
242 * The xstateregs_active() routine is the same as the fpregs_active() routine,
243 * as the "regset->n" for the xstate regset will be updated based on the feature
244 * capabilites supported by the xsave.
245 */
Roland McGrath44210112008-01-30 13:31:50 +0100246int fpregs_active(struct task_struct *target, const struct user_regset *regset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Roland McGrath44210112008-01-30 13:31:50 +0100248 return tsk_used_math(target) ? regset->n : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
Roland McGrath44210112008-01-30 13:31:50 +0100250
251int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
252{
253 return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
254}
255
256int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
257 unsigned int pos, unsigned int count,
258 void *kbuf, void __user *ubuf)
259{
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700260 int ret;
261
Roland McGrath44210112008-01-30 13:31:50 +0100262 if (!cpu_has_fxsr)
263 return -ENODEV;
264
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700265 ret = init_fpu(target);
266 if (ret)
267 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100268
Suresh Siddha29104e12010-07-19 16:05:49 -0700269 sanitize_i387_state(target);
270
Roland McGrath44210112008-01-30 13:31:50 +0100271 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300272 &target->thread.fpu.state->fxsave, 0, -1);
Roland McGrath44210112008-01-30 13:31:50 +0100273}
274
275int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
276 unsigned int pos, unsigned int count,
277 const void *kbuf, const void __user *ubuf)
278{
279 int ret;
280
281 if (!cpu_has_fxsr)
282 return -ENODEV;
283
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700284 ret = init_fpu(target);
285 if (ret)
286 return ret;
287
Suresh Siddha29104e12010-07-19 16:05:49 -0700288 sanitize_i387_state(target);
289
Roland McGrath44210112008-01-30 13:31:50 +0100290 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300291 &target->thread.fpu.state->fxsave, 0, -1);
Roland McGrath44210112008-01-30 13:31:50 +0100292
293 /*
294 * mxcsr reserved bits must be masked to zero for security reasons.
295 */
Avi Kivity86603282010-05-06 11:45:46 +0300296 target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
Roland McGrath44210112008-01-30 13:31:50 +0100297
Suresh Siddha42deec62008-07-29 10:29:26 -0700298 /*
299 * update the header bits in the xsave header, indicating the
300 * presence of FP and SSE state.
301 */
302 if (cpu_has_xsave)
Avi Kivity86603282010-05-06 11:45:46 +0300303 target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FPSSE;
Suresh Siddha42deec62008-07-29 10:29:26 -0700304
Roland McGrath44210112008-01-30 13:31:50 +0100305 return ret;
306}
307
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800308int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
309 unsigned int pos, unsigned int count,
310 void *kbuf, void __user *ubuf)
311{
312 int ret;
313
314 if (!cpu_has_xsave)
315 return -ENODEV;
316
317 ret = init_fpu(target);
318 if (ret)
319 return ret;
320
321 /*
Suresh Siddhaff7fbc72010-02-22 14:51:33 -0800322 * Copy the 48bytes defined by the software first into the xstate
323 * memory layout in the thread struct, so that we can copy the entire
324 * xstateregs to the user using one user_regset_copyout().
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800325 */
Avi Kivity86603282010-05-06 11:45:46 +0300326 memcpy(&target->thread.fpu.state->fxsave.sw_reserved,
Suresh Siddhaff7fbc72010-02-22 14:51:33 -0800327 xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800328
329 /*
Suresh Siddhaff7fbc72010-02-22 14:51:33 -0800330 * Copy the xstate memory layout.
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800331 */
332 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300333 &target->thread.fpu.state->xsave, 0, -1);
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800334 return ret;
335}
336
337int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
338 unsigned int pos, unsigned int count,
339 const void *kbuf, const void __user *ubuf)
340{
341 int ret;
342 struct xsave_hdr_struct *xsave_hdr;
343
344 if (!cpu_has_xsave)
345 return -ENODEV;
346
347 ret = init_fpu(target);
348 if (ret)
349 return ret;
350
351 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300352 &target->thread.fpu.state->xsave, 0, -1);
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800353
354 /*
355 * mxcsr reserved bits must be masked to zero for security reasons.
356 */
Avi Kivity86603282010-05-06 11:45:46 +0300357 target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800358
Avi Kivity86603282010-05-06 11:45:46 +0300359 xsave_hdr = &target->thread.fpu.state->xsave.xsave_hdr;
Suresh Siddha5b3efd52010-02-11 11:50:59 -0800360
361 xsave_hdr->xstate_bv &= pcntxt_mask;
362 /*
363 * These bits must be zero.
364 */
365 xsave_hdr->reserved1[0] = xsave_hdr->reserved1[1] = 0;
366
367 return ret;
368}
369
Roland McGrath44210112008-01-30 13:31:50 +0100370#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372/*
373 * FPU tag word conversions.
374 */
375
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100376static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100381 tmp = ~twd;
Roland McGrath44210112008-01-30 13:31:50 +0100382 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100383 /* and move the valid bits to the lower byte. */
384 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
385 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
386 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
Ingo Molnarf6689642008-03-05 15:37:32 +0100387
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100388 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
Phil Carmody497888c2011-07-14 15:07:13 +0300391#define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16)
Roland McGrath44210112008-01-30 13:31:50 +0100392#define FP_EXP_TAG_VALID 0
393#define FP_EXP_TAG_ZERO 1
394#define FP_EXP_TAG_SPECIAL 2
395#define FP_EXP_TAG_EMPTY 3
396
397static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Roland McGrath44210112008-01-30 13:31:50 +0100399 struct _fpxreg *st;
400 u32 tos = (fxsave->swd >> 11) & 7;
401 u32 twd = (unsigned long) fxsave->twd;
402 u32 tag;
403 u32 ret = 0xffff0000u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 int i;
405
Roland McGrath44210112008-01-30 13:31:50 +0100406 for (i = 0; i < 8; i++, twd >>= 1) {
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100407 if (twd & 0x1) {
408 st = FPREG_ADDR(fxsave, (i - tos) & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100410 switch (st->exponent & 0x7fff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 case 0x7fff:
Roland McGrath44210112008-01-30 13:31:50 +0100412 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 break;
414 case 0x0000:
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100415 if (!st->significand[0] &&
416 !st->significand[1] &&
417 !st->significand[2] &&
Roland McGrath44210112008-01-30 13:31:50 +0100418 !st->significand[3])
419 tag = FP_EXP_TAG_ZERO;
420 else
421 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 break;
423 default:
Roland McGrath44210112008-01-30 13:31:50 +0100424 if (st->significand[3] & 0x8000)
425 tag = FP_EXP_TAG_VALID;
426 else
427 tag = FP_EXP_TAG_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 break;
429 }
430 } else {
Roland McGrath44210112008-01-30 13:31:50 +0100431 tag = FP_EXP_TAG_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Roland McGrath44210112008-01-30 13:31:50 +0100433 ret |= tag << (2 * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435 return ret;
436}
437
438/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 * FXSR floating point environment conversions.
440 */
441
Suresh Siddha72a671c2012-07-24 16:05:29 -0700442void
Ingo Molnarf6689642008-03-05 15:37:32 +0100443convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Avi Kivity86603282010-05-06 11:45:46 +0300445 struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100446 struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
447 struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int i;
449
Roland McGrath44210112008-01-30 13:31:50 +0100450 env->cwd = fxsave->cwd | 0xffff0000u;
451 env->swd = fxsave->swd | 0xffff0000u;
452 env->twd = twd_fxsr_to_i387(fxsave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Roland McGrath44210112008-01-30 13:31:50 +0100454#ifdef CONFIG_X86_64
455 env->fip = fxsave->rip;
456 env->foo = fxsave->rdp;
Brian Gerst10c11f32010-09-03 21:17:13 -0400457 /*
458 * should be actually ds/cs at fpu exception time, but
459 * that information is not available in 64bit mode.
460 */
461 env->fcs = task_pt_regs(tsk)->cs;
Roland McGrath44210112008-01-30 13:31:50 +0100462 if (tsk == current) {
Brian Gerst10c11f32010-09-03 21:17:13 -0400463 savesegment(ds, env->fos);
Roland McGrath44210112008-01-30 13:31:50 +0100464 } else {
Brian Gerst10c11f32010-09-03 21:17:13 -0400465 env->fos = tsk->thread.ds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
Brian Gerst10c11f32010-09-03 21:17:13 -0400467 env->fos |= 0xffff0000;
Roland McGrath44210112008-01-30 13:31:50 +0100468#else
469 env->fip = fxsave->fip;
Jan Beulich609b5292008-03-05 08:35:14 +0000470 env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
Roland McGrath44210112008-01-30 13:31:50 +0100471 env->foo = fxsave->foo;
472 env->fos = fxsave->fos;
473#endif
474
475 for (i = 0; i < 8; ++i)
476 memcpy(&to[i], &from[i], sizeof(to[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Suresh Siddha72a671c2012-07-24 16:05:29 -0700479void convert_to_fxsr(struct task_struct *tsk,
480 const struct user_i387_ia32_struct *env)
Roland McGrath44210112008-01-30 13:31:50 +0100481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Avi Kivity86603282010-05-06 11:45:46 +0300483 struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
Roland McGrath44210112008-01-30 13:31:50 +0100484 struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
485 struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 int i;
487
Roland McGrath44210112008-01-30 13:31:50 +0100488 fxsave->cwd = env->cwd;
489 fxsave->swd = env->swd;
490 fxsave->twd = twd_i387_to_fxsr(env->twd);
491 fxsave->fop = (u16) ((u32) env->fcs >> 16);
492#ifdef CONFIG_X86_64
493 fxsave->rip = env->fip;
494 fxsave->rdp = env->foo;
495 /* cs and ds ignored */
496#else
497 fxsave->fip = env->fip;
498 fxsave->fcs = (env->fcs & 0xffff);
499 fxsave->foo = env->foo;
500 fxsave->fos = env->fos;
501#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Roland McGrath44210112008-01-30 13:31:50 +0100503 for (i = 0; i < 8; ++i)
504 memcpy(&to[i], &from[i], sizeof(from[0]));
505}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Roland McGrath44210112008-01-30 13:31:50 +0100507int fpregs_get(struct task_struct *target, const struct user_regset *regset,
508 unsigned int pos, unsigned int count,
509 void *kbuf, void __user *ubuf)
510{
511 struct user_i387_ia32_struct env;
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700512 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700514 ret = init_fpu(target);
515 if (ret)
516 return ret;
Roland McGrath44210112008-01-30 13:31:50 +0100517
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700518 if (!HAVE_HWFP)
519 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
520
Ingo Molnarf6689642008-03-05 15:37:32 +0100521 if (!cpu_has_fxsr) {
Roland McGrath44210112008-01-30 13:31:50 +0100522 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300523 &target->thread.fpu.state->fsave, 0,
Suresh Siddha61c46282008-03-10 15:28:04 -0700524 -1);
Ingo Molnarf6689642008-03-05 15:37:32 +0100525 }
Roland McGrath44210112008-01-30 13:31:50 +0100526
Suresh Siddha29104e12010-07-19 16:05:49 -0700527 sanitize_i387_state(target);
528
Roland McGrath44210112008-01-30 13:31:50 +0100529 if (kbuf && pos == 0 && count == sizeof(env)) {
530 convert_from_fxsr(kbuf, target);
531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
Roland McGrath44210112008-01-30 13:31:50 +0100533
534 convert_from_fxsr(&env, target);
Ingo Molnarf6689642008-03-05 15:37:32 +0100535
Roland McGrath44210112008-01-30 13:31:50 +0100536 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
537}
538
539int fpregs_set(struct task_struct *target, const struct user_regset *regset,
540 unsigned int pos, unsigned int count,
541 const void *kbuf, const void __user *ubuf)
542{
543 struct user_i387_ia32_struct env;
544 int ret;
545
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700546 ret = init_fpu(target);
547 if (ret)
548 return ret;
549
Suresh Siddha29104e12010-07-19 16:05:49 -0700550 sanitize_i387_state(target);
551
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700552 if (!HAVE_HWFP)
553 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
554
Ingo Molnarf6689642008-03-05 15:37:32 +0100555 if (!cpu_has_fxsr) {
Roland McGrath44210112008-01-30 13:31:50 +0100556 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Avi Kivity86603282010-05-06 11:45:46 +0300557 &target->thread.fpu.state->fsave, 0, -1);
Ingo Molnarf6689642008-03-05 15:37:32 +0100558 }
Roland McGrath44210112008-01-30 13:31:50 +0100559
560 if (pos > 0 || count < sizeof(env))
561 convert_from_fxsr(&env, target);
562
563 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
564 if (!ret)
565 convert_to_fxsr(target, &env);
566
Suresh Siddha42deec62008-07-29 10:29:26 -0700567 /*
568 * update the header bit in the xsave header, indicating the
569 * presence of FP.
570 */
571 if (cpu_has_xsave)
Avi Kivity86603282010-05-06 11:45:46 +0300572 target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FP;
Roland McGrath44210112008-01-30 13:31:50 +0100573 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 * FPU state for core dumps.
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100578 * This is only used for a.out dumps now.
579 * It is declared generically using elf_fpregset_t (which is
580 * struct user_i387_struct) but is in fact only used for 32-bit
581 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
Cyrill Gorcunov3b095a02008-01-30 13:31:26 +0100583int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct task_struct *tsk = current;
Ingo Molnarf6689642008-03-05 15:37:32 +0100586 int fpvalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 fpvalid = !!used_math();
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100589 if (fpvalid)
590 fpvalid = !fpregs_get(tsk, NULL,
591 0, sizeof(struct user_i387_ia32_struct),
592 fpu, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 return fpvalid;
595}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700596EXPORT_SYMBOL(dump_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Roland McGrath60b3b9a2008-01-30 13:31:55 +0100598#endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */