x86/fpu: Move fpu_alloc() out of line
[deliverable/linux.git] / arch / x86 / kernel / i387.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * 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 */
129f6946 8#include <linux/module.h>
44210111 9#include <linux/regset.h>
f668964e 10#include <linux/sched.h>
5a0e3ad6 11#include <linux/slab.h>
f668964e
IM
12
13#include <asm/sigcontext.h>
1da177e4 14#include <asm/processor.h>
1da177e4 15#include <asm/math_emu.h>
375074cc 16#include <asm/tlbflush.h>
1da177e4 17#include <asm/uaccess.h>
f668964e
IM
18#include <asm/ptrace.h>
19#include <asm/i387.h>
1361b83a 20#include <asm/fpu-internal.h>
f668964e 21#include <asm/user.h>
1da177e4 22
14e153ef
ON
23static DEFINE_PER_CPU(bool, in_kernel_fpu);
24
7575637a
ON
25void kernel_fpu_disable(void)
26{
27 WARN_ON(this_cpu_read(in_kernel_fpu));
28 this_cpu_write(in_kernel_fpu, true);
29}
30
31void kernel_fpu_enable(void)
32{
33 this_cpu_write(in_kernel_fpu, false);
34}
35
8546c008
LT
36/*
37 * Were we in an interrupt that interrupted kernel mode?
38 *
304bceda 39 * On others, we can do a kernel_fpu_begin/end() pair *ONLY* if that
8546c008
LT
40 * pair does nothing at all: the thread must not have fpu (so
41 * that we don't try to save the FPU state), and TS must
42 * be set (so that the clts/stts pair does nothing that is
43 * visible in the interrupted kernel thread).
5187b28f 44 *
4b2e762e
ON
45 * Except for the eagerfpu case when we return true; in the likely case
46 * the thread has FPU but we are not going to set/clear TS.
8546c008
LT
47 */
48static inline bool interrupted_kernel_fpu_idle(void)
49{
14e153ef
ON
50 if (this_cpu_read(in_kernel_fpu))
51 return false;
52
5d2bd700 53 if (use_eager_fpu())
4b2e762e 54 return true;
304bceda 55
8546c008
LT
56 return !__thread_has_fpu(current) &&
57 (read_cr0() & X86_CR0_TS);
58}
59
60/*
61 * Were we in user mode (or vm86 mode) when we were
62 * interrupted?
63 *
64 * Doing kernel_fpu_begin/end() is ok if we are running
65 * in an interrupt context from user mode - we'll just
66 * save the FPU state as required.
67 */
68static inline bool interrupted_user_mode(void)
69{
70 struct pt_regs *regs = get_irq_regs();
f39b6f0e 71 return regs && user_mode(regs);
8546c008
LT
72}
73
74/*
75 * Can we use the FPU in kernel mode with the
76 * whole "kernel_fpu_begin/end()" sequence?
77 *
78 * It's always ok in process context (ie "not interrupt")
79 * but it is sometimes ok even from an irq.
80 */
81bool irq_fpu_usable(void)
82{
83 return !in_interrupt() ||
84 interrupted_user_mode() ||
85 interrupted_kernel_fpu_idle();
86}
87EXPORT_SYMBOL(irq_fpu_usable);
88
b1a74bf8 89void __kernel_fpu_begin(void)
8546c008
LT
90{
91 struct task_struct *me = current;
92
14e153ef
ON
93 this_cpu_write(in_kernel_fpu, true);
94
8546c008 95 if (__thread_has_fpu(me)) {
5187b28f 96 __save_init_fpu(me);
7aeccb83 97 } else {
c6ae41e7 98 this_cpu_write(fpu_owner_task, NULL);
7aeccb83
ON
99 if (!use_eager_fpu())
100 clts();
8546c008
LT
101 }
102}
b1a74bf8 103EXPORT_SYMBOL(__kernel_fpu_begin);
8546c008 104
b1a74bf8 105void __kernel_fpu_end(void)
8546c008 106{
33a3ebdc
ON
107 struct task_struct *me = current;
108
109 if (__thread_has_fpu(me)) {
110 if (WARN_ON(restore_fpu_checking(me)))
b85e67d1 111 fpu_reset_state(me);
33a3ebdc 112 } else if (!use_eager_fpu()) {
304bceda 113 stts();
731bd6a9 114 }
14e153ef
ON
115
116 this_cpu_write(in_kernel_fpu, false);
8546c008 117}
b1a74bf8 118EXPORT_SYMBOL(__kernel_fpu_end);
8546c008 119
4af08f2f
IM
120/*
121 * Save the FPU state (initialize it if necessary):
87cdb98a
IM
122 *
123 * This only ever gets called for the current task.
4af08f2f 124 */
0a781551 125void fpu__save(struct task_struct *tsk)
8546c008 126{
87cdb98a
IM
127 WARN_ON(tsk != current);
128
8546c008
LT
129 preempt_disable();
130 if (__thread_has_fpu(tsk)) {
1a2a7f4e
ON
131 if (use_eager_fpu()) {
132 __save_fpu(tsk);
133 } else {
134 __save_init_fpu(tsk);
135 __thread_fpu_end(tsk);
136 }
a9241ea5 137 }
8546c008
LT
138 preempt_enable();
139}
4af08f2f 140EXPORT_SYMBOL_GPL(fpu__save);
8546c008 141
72a671ce 142unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
61c4628b 143unsigned int xstate_size;
f45755b8 144EXPORT_SYMBOL_GPL(xstate_size);
148f9bb8 145static struct i387_fxsave_struct fx_scratch;
1da177e4 146
148f9bb8 147static void mxcsr_feature_mask_init(void)
1da177e4
LT
148{
149 unsigned long mask = 0;
f668964e 150
1da177e4 151 if (cpu_has_fxsr) {
61c4628b 152 memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct));
eaa5a990 153 asm volatile("fxsave %0" : "+m" (fx_scratch));
61c4628b 154 mask = fx_scratch.mxcsr_mask;
3b095a04
CG
155 if (mask == 0)
156 mask = 0x0000ffbf;
157 }
1da177e4 158 mxcsr_feature_mask &= mask;
1da177e4
LT
159}
160
148f9bb8 161static void init_thread_xstate(void)
61c4628b 162{
0e49bf66
RR
163 /*
164 * Note that xstate_size might be overwriten later during
165 * xsave_init().
166 */
167
60e019eb 168 if (!cpu_has_fpu) {
1f999ab5
RR
169 /*
170 * Disable xsave as we do not support it if i387
171 * emulation is enabled.
172 */
173 setup_clear_cpu_cap(X86_FEATURE_XSAVE);
174 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
e8a496ac
SS
175 xstate_size = sizeof(struct i387_soft_struct);
176 return;
177 }
178
61c4628b
SS
179 if (cpu_has_fxsr)
180 xstate_size = sizeof(struct i387_fxsave_struct);
61c4628b
SS
181 else
182 xstate_size = sizeof(struct i387_fsave_struct);
61c4628b
SS
183}
184
44210111
RM
185/*
186 * Called at bootup to set up the initial FPU state that is later cloned
187 * into all processes.
188 */
0e49bf66 189
148f9bb8 190void fpu_init(void)
44210111 191{
6ac8bac2
BG
192 unsigned long cr0;
193 unsigned long cr4_mask = 0;
44210111 194
60e019eb
PA
195#ifndef CONFIG_MATH_EMULATION
196 if (!cpu_has_fpu) {
197 pr_emerg("No FPU found and no math emulation present\n");
198 pr_emerg("Giving up\n");
199 for (;;)
200 asm volatile("hlt");
201 }
202#endif
6ac8bac2
BG
203 if (cpu_has_fxsr)
204 cr4_mask |= X86_CR4_OSFXSR;
205 if (cpu_has_xmm)
206 cr4_mask |= X86_CR4_OSXMMEXCPT;
207 if (cr4_mask)
375074cc 208 cr4_set_bits(cr4_mask);
6ac8bac2
BG
209
210 cr0 = read_cr0();
211 cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
60e019eb 212 if (!cpu_has_fpu)
6ac8bac2
BG
213 cr0 |= X86_CR0_EM;
214 write_cr0(cr0);
44210111 215
6f5298c2
FY
216 /*
217 * init_thread_xstate is only called once to avoid overriding
218 * xstate_size during boot time or during CPU hotplug.
219 */
220 if (xstate_size == 0)
dc1e35c6 221 init_thread_xstate();
dc1e35c6 222
44210111 223 mxcsr_feature_mask_init();
5d2bd700
SS
224 xsave_init();
225 eager_fpu_init();
44210111 226}
0e49bf66 227
5ee481da 228void fpu_finit(struct fpu *fpu)
1da177e4 229{
60e019eb 230 if (!cpu_has_fpu) {
86603283
AK
231 finit_soft_fpu(&fpu->state->soft);
232 return;
e8a496ac 233 }
e8a496ac 234
1d23c451
ON
235 memset(fpu->state, 0, xstate_size);
236
1da177e4 237 if (cpu_has_fxsr) {
5d2bd700 238 fx_finit(&fpu->state->fxsave);
1da177e4 239 } else {
86603283 240 struct i387_fsave_struct *fp = &fpu->state->fsave;
61c4628b
SS
241 fp->cwd = 0xffff037fu;
242 fp->swd = 0xffff0000u;
243 fp->twd = 0xffffffffu;
244 fp->fos = 0xffff0000u;
1da177e4 245 }
86603283 246}
5ee481da 247EXPORT_SYMBOL_GPL(fpu_finit);
86603283 248
6fbe6712
IM
249int fpu_alloc(struct fpu *fpu)
250{
251 if (fpu->state)
252 return 0;
253 fpu->state = kmem_cache_alloc(task_xstate_cachep, GFP_KERNEL);
254 if (!fpu->state)
255 return -ENOMEM;
256 WARN_ON((unsigned long)fpu->state & 15);
257 return 0;
258}
259EXPORT_SYMBOL_GPL(fpu_alloc);
260
97185c95
IM
261/*
262 * Allocate the backing store for the current task's FPU registers
263 * and initialize the registers themselves as well.
264 *
265 * Can fail.
266 */
267int fpstate_alloc_init(struct task_struct *curr)
268{
269 int ret;
270
271 if (WARN_ON_ONCE(curr != current))
272 return -EINVAL;
273 if (WARN_ON_ONCE(curr->flags & PF_USED_MATH))
274 return -EINVAL;
275
276 /*
277 * Memory allocation at the first usage of the FPU and other state.
278 */
279 ret = fpu_alloc(&curr->thread.fpu);
280 if (ret)
281 return ret;
282
283 fpu_finit(&curr->thread.fpu);
284
285 /* Safe to do for the current task: */
286 curr->flags |= PF_USED_MATH;
287
288 return 0;
289}
290EXPORT_SYMBOL_GPL(fpstate_alloc_init);
291
86603283
AK
292/*
293 * The _current_ task is using the FPU for the first time
294 * so initialize it and set the mxcsr to its default
295 * value at reset if we support XMM instructions and then
0d2eb44f 296 * remember the current task has used the FPU.
86603283 297 */
67e97fc2 298static int fpu__unlazy_stopped(struct task_struct *child)
86603283
AK
299{
300 int ret;
301
67e97fc2
IM
302 if (WARN_ON_ONCE(child == current))
303 return -EINVAL;
304
071ae621 305 if (child->flags & PF_USED_MATH) {
67e97fc2 306 task_disable_lazy_fpu_restore(child);
86603283
AK
307 return 0;
308 }
309
44210111 310 /*
86603283 311 * Memory allocation at the first usage of the FPU and other state.
44210111 312 */
67e97fc2 313 ret = fpu_alloc(&child->thread.fpu);
86603283
AK
314 if (ret)
315 return ret;
316
67e97fc2 317 fpu_finit(&child->thread.fpu);
86603283 318
071ae621
IM
319 /* Safe to do for stopped child tasks: */
320 child->flags |= PF_USED_MATH;
321
aa283f49 322 return 0;
1da177e4
LT
323}
324
5b3efd50
SS
325/*
326 * The xstateregs_active() routine is the same as the fpregs_active() routine,
327 * as the "regset->n" for the xstate regset will be updated based on the feature
328 * capabilites supported by the xsave.
329 */
44210111
RM
330int fpregs_active(struct task_struct *target, const struct user_regset *regset)
331{
332 return tsk_used_math(target) ? regset->n : 0;
333}
1da177e4 334
44210111 335int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
1da177e4 336{
44210111
RM
337 return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
338}
1da177e4 339
44210111
RM
340int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
341 unsigned int pos, unsigned int count,
342 void *kbuf, void __user *ubuf)
343{
aa283f49
SS
344 int ret;
345
44210111
RM
346 if (!cpu_has_fxsr)
347 return -ENODEV;
348
67e97fc2 349 ret = fpu__unlazy_stopped(target);
aa283f49
SS
350 if (ret)
351 return ret;
44210111 352
29104e10
SS
353 sanitize_i387_state(target);
354
44210111 355 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
86603283 356 &target->thread.fpu.state->fxsave, 0, -1);
1da177e4 357}
44210111
RM
358
359int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
360 unsigned int pos, unsigned int count,
361 const void *kbuf, const void __user *ubuf)
362{
363 int ret;
364
365 if (!cpu_has_fxsr)
366 return -ENODEV;
367
67e97fc2 368 ret = fpu__unlazy_stopped(target);
aa283f49
SS
369 if (ret)
370 return ret;
371
29104e10
SS
372 sanitize_i387_state(target);
373
44210111 374 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
86603283 375 &target->thread.fpu.state->fxsave, 0, -1);
44210111
RM
376
377 /*
378 * mxcsr reserved bits must be masked to zero for security reasons.
379 */
86603283 380 target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
44210111 381
42deec6f
SS
382 /*
383 * update the header bits in the xsave header, indicating the
384 * presence of FP and SSE state.
385 */
386 if (cpu_has_xsave)
86603283 387 target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FPSSE;
42deec6f 388
44210111
RM
389 return ret;
390}
391
5b3efd50
SS
392int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
393 unsigned int pos, unsigned int count,
394 void *kbuf, void __user *ubuf)
395{
18ecb3bf 396 struct xsave_struct *xsave;
5b3efd50
SS
397 int ret;
398
399 if (!cpu_has_xsave)
400 return -ENODEV;
401
67e97fc2 402 ret = fpu__unlazy_stopped(target);
5b3efd50
SS
403 if (ret)
404 return ret;
405
18ecb3bf
BP
406 xsave = &target->thread.fpu.state->xsave;
407
5b3efd50 408 /*
ff7fbc72
SS
409 * Copy the 48bytes defined by the software first into the xstate
410 * memory layout in the thread struct, so that we can copy the entire
411 * xstateregs to the user using one user_regset_copyout().
5b3efd50 412 */
e7f180dc
ON
413 memcpy(&xsave->i387.sw_reserved,
414 xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
5b3efd50 415 /*
ff7fbc72 416 * Copy the xstate memory layout.
5b3efd50 417 */
e7f180dc 418 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
5b3efd50
SS
419 return ret;
420}
421
422int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
423 unsigned int pos, unsigned int count,
424 const void *kbuf, const void __user *ubuf)
425{
18ecb3bf 426 struct xsave_struct *xsave;
5b3efd50 427 int ret;
5b3efd50
SS
428
429 if (!cpu_has_xsave)
430 return -ENODEV;
431
67e97fc2 432 ret = fpu__unlazy_stopped(target);
5b3efd50
SS
433 if (ret)
434 return ret;
435
18ecb3bf
BP
436 xsave = &target->thread.fpu.state->xsave;
437
e7f180dc 438 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
5b3efd50
SS
439 /*
440 * mxcsr reserved bits must be masked to zero for security reasons.
441 */
e7f180dc
ON
442 xsave->i387.mxcsr &= mxcsr_feature_mask;
443 xsave->xsave_hdr.xstate_bv &= pcntxt_mask;
5b3efd50
SS
444 /*
445 * These bits must be zero.
446 */
e7f180dc 447 memset(&xsave->xsave_hdr.reserved, 0, 48);
5b3efd50
SS
448 return ret;
449}
450
44210111 451#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1da177e4 452
1da177e4
LT
453/*
454 * FPU tag word conversions.
455 */
456
3b095a04 457static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
1da177e4
LT
458{
459 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
3b095a04 460
1da177e4 461 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
3b095a04 462 tmp = ~twd;
44210111 463 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
3b095a04
CG
464 /* and move the valid bits to the lower byte. */
465 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
466 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
467 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
f668964e 468
3b095a04 469 return tmp;
1da177e4
LT
470}
471
497888cf 472#define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16)
44210111
RM
473#define FP_EXP_TAG_VALID 0
474#define FP_EXP_TAG_ZERO 1
475#define FP_EXP_TAG_SPECIAL 2
476#define FP_EXP_TAG_EMPTY 3
477
478static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
479{
480 struct _fpxreg *st;
481 u32 tos = (fxsave->swd >> 11) & 7;
482 u32 twd = (unsigned long) fxsave->twd;
483 u32 tag;
484 u32 ret = 0xffff0000u;
485 int i;
1da177e4 486
44210111 487 for (i = 0; i < 8; i++, twd >>= 1) {
3b095a04
CG
488 if (twd & 0x1) {
489 st = FPREG_ADDR(fxsave, (i - tos) & 7);
1da177e4 490
3b095a04 491 switch (st->exponent & 0x7fff) {
1da177e4 492 case 0x7fff:
44210111 493 tag = FP_EXP_TAG_SPECIAL;
1da177e4
LT
494 break;
495 case 0x0000:
3b095a04
CG
496 if (!st->significand[0] &&
497 !st->significand[1] &&
498 !st->significand[2] &&
44210111
RM
499 !st->significand[3])
500 tag = FP_EXP_TAG_ZERO;
501 else
502 tag = FP_EXP_TAG_SPECIAL;
1da177e4
LT
503 break;
504 default:
44210111
RM
505 if (st->significand[3] & 0x8000)
506 tag = FP_EXP_TAG_VALID;
507 else
508 tag = FP_EXP_TAG_SPECIAL;
1da177e4
LT
509 break;
510 }
511 } else {
44210111 512 tag = FP_EXP_TAG_EMPTY;
1da177e4 513 }
44210111 514 ret |= tag << (2 * i);
1da177e4
LT
515 }
516 return ret;
517}
518
519/*
44210111 520 * FXSR floating point environment conversions.
1da177e4
LT
521 */
522
72a671ce 523void
f668964e 524convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
1da177e4 525{
86603283 526 struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
44210111
RM
527 struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
528 struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
529 int i;
1da177e4 530
44210111
RM
531 env->cwd = fxsave->cwd | 0xffff0000u;
532 env->swd = fxsave->swd | 0xffff0000u;
533 env->twd = twd_fxsr_to_i387(fxsave);
534
535#ifdef CONFIG_X86_64
536 env->fip = fxsave->rip;
537 env->foo = fxsave->rdp;
10c11f30
BG
538 /*
539 * should be actually ds/cs at fpu exception time, but
540 * that information is not available in 64bit mode.
541 */
542 env->fcs = task_pt_regs(tsk)->cs;
44210111 543 if (tsk == current) {
10c11f30 544 savesegment(ds, env->fos);
1da177e4 545 } else {
10c11f30 546 env->fos = tsk->thread.ds;
1da177e4 547 }
10c11f30 548 env->fos |= 0xffff0000;
44210111
RM
549#else
550 env->fip = fxsave->fip;
609b5297 551 env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
44210111
RM
552 env->foo = fxsave->foo;
553 env->fos = fxsave->fos;
554#endif
1da177e4 555
44210111
RM
556 for (i = 0; i < 8; ++i)
557 memcpy(&to[i], &from[i], sizeof(to[0]));
1da177e4
LT
558}
559
72a671ce
SS
560void convert_to_fxsr(struct task_struct *tsk,
561 const struct user_i387_ia32_struct *env)
1da177e4 562
1da177e4 563{
86603283 564 struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
44210111
RM
565 struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
566 struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
567 int i;
1da177e4 568
44210111
RM
569 fxsave->cwd = env->cwd;
570 fxsave->swd = env->swd;
571 fxsave->twd = twd_i387_to_fxsr(env->twd);
572 fxsave->fop = (u16) ((u32) env->fcs >> 16);
573#ifdef CONFIG_X86_64
574 fxsave->rip = env->fip;
575 fxsave->rdp = env->foo;
576 /* cs and ds ignored */
577#else
578 fxsave->fip = env->fip;
579 fxsave->fcs = (env->fcs & 0xffff);
580 fxsave->foo = env->foo;
581 fxsave->fos = env->fos;
582#endif
1da177e4 583
44210111
RM
584 for (i = 0; i < 8; ++i)
585 memcpy(&to[i], &from[i], sizeof(from[0]));
1da177e4
LT
586}
587
44210111
RM
588int fpregs_get(struct task_struct *target, const struct user_regset *regset,
589 unsigned int pos, unsigned int count,
590 void *kbuf, void __user *ubuf)
1da177e4 591{
44210111 592 struct user_i387_ia32_struct env;
aa283f49 593 int ret;
1da177e4 594
67e97fc2 595 ret = fpu__unlazy_stopped(target);
aa283f49
SS
596 if (ret)
597 return ret;
1da177e4 598
60e019eb 599 if (!static_cpu_has(X86_FEATURE_FPU))
e8a496ac
SS
600 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
601
60e019eb 602 if (!cpu_has_fxsr)
44210111 603 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
86603283 604 &target->thread.fpu.state->fsave, 0,
61c4628b 605 -1);
1da177e4 606
29104e10
SS
607 sanitize_i387_state(target);
608
44210111
RM
609 if (kbuf && pos == 0 && count == sizeof(env)) {
610 convert_from_fxsr(kbuf, target);
611 return 0;
1da177e4 612 }
44210111
RM
613
614 convert_from_fxsr(&env, target);
f668964e 615
44210111 616 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
1da177e4
LT
617}
618
44210111
RM
619int fpregs_set(struct task_struct *target, const struct user_regset *regset,
620 unsigned int pos, unsigned int count,
621 const void *kbuf, const void __user *ubuf)
1da177e4 622{
44210111
RM
623 struct user_i387_ia32_struct env;
624 int ret;
1da177e4 625
67e97fc2 626 ret = fpu__unlazy_stopped(target);
aa283f49
SS
627 if (ret)
628 return ret;
629
29104e10
SS
630 sanitize_i387_state(target);
631
60e019eb 632 if (!static_cpu_has(X86_FEATURE_FPU))
e8a496ac
SS
633 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
634
60e019eb 635 if (!cpu_has_fxsr)
44210111 636 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
60e019eb
PA
637 &target->thread.fpu.state->fsave, 0,
638 -1);
44210111
RM
639
640 if (pos > 0 || count < sizeof(env))
641 convert_from_fxsr(&env, target);
642
643 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
644 if (!ret)
645 convert_to_fxsr(target, &env);
646
42deec6f
SS
647 /*
648 * update the header bit in the xsave header, indicating the
649 * presence of FP.
650 */
651 if (cpu_has_xsave)
86603283 652 target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FP;
44210111 653 return ret;
1da177e4
LT
654}
655
1da177e4
LT
656/*
657 * FPU state for core dumps.
60b3b9af
RM
658 * This is only used for a.out dumps now.
659 * It is declared generically using elf_fpregset_t (which is
660 * struct user_i387_struct) but is in fact only used for 32-bit
661 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
1da177e4 662 */
3b095a04 663int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
1da177e4 664{
1da177e4 665 struct task_struct *tsk = current;
f668964e 666 int fpvalid;
1da177e4
LT
667
668 fpvalid = !!used_math();
60b3b9af
RM
669 if (fpvalid)
670 fpvalid = !fpregs_get(tsk, NULL,
671 0, sizeof(struct user_i387_ia32_struct),
672 fpu, NULL);
1da177e4
LT
673
674 return fpvalid;
675}
129f6946 676EXPORT_SYMBOL(dump_fpu);
1da177e4 677
60b3b9af 678#endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */
60e019eb
PA
679
680static int __init no_387(char *s)
681{
682 setup_clear_cpu_cap(X86_FEATURE_FPU);
683 return 1;
684}
685
686__setup("no387", no_387);
687
1a7dc0db
IM
688/*
689 * Set the X86_FEATURE_FPU CPU-capability bit based on
690 * trying to execute an actual sequence of FPU instructions:
691 */
692void fpu__detect(struct cpuinfo_x86 *c)
60e019eb
PA
693{
694 unsigned long cr0;
695 u16 fsw, fcw;
696
697 fsw = fcw = 0xffff;
698
699 cr0 = read_cr0();
700 cr0 &= ~(X86_CR0_TS | X86_CR0_EM);
701 write_cr0(cr0);
702
703 asm volatile("fninit ; fnstsw %0 ; fnstcw %1"
704 : "+m" (fsw), "+m" (fcw));
705
706 if (fsw == 0 && (fcw & 0x103f) == 0x003f)
707 set_cpu_cap(c, X86_FEATURE_FPU);
708 else
709 clear_cpu_cap(c, X86_FEATURE_FPU);
710
711 /* The final cr0 value is set in fpu_init() */
712}
This page took 0.799873 seconds and 5 git commands to generate.