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