2 * Copyright (C) 1994 Linus Torvalds
3 * Copyright (C) 2002 Andi Kleen, SuSE Labs
5 * Pentium III FXSR, SSE support
6 * General FPU state handling cleanups
7 * Gareth Hughes <gareth@valinux.com>, May 2000
9 * x86-64 rework 2002 Andi Kleen.
10 * Does direct fxsave in and out of user space now for signal handlers.
11 * All the FSAVE<->FXSAVE conversion code has been moved to the 32bit emulation,
12 * the 64bit user space sees a FXSAVE frame directly.
15 #include <linux/sched.h>
16 #include <linux/init.h>
17 #include <asm/processor.h>
19 #include <asm/sigcontext.h>
21 #include <asm/ptrace.h>
22 #include <asm/uaccess.h>
24 unsigned int mxcsr_feature_mask __read_mostly
= 0xffffffff;
26 void mxcsr_feature_mask_init(void)
30 memset(¤t
->thread
.i387
.fxsave
, 0, sizeof(struct i387_fxsave_struct
));
31 asm volatile("fxsave %0" : : "m" (current
->thread
.i387
.fxsave
));
32 mask
= current
->thread
.i387
.fxsave
.mxcsr_mask
;
33 if (mask
== 0) mask
= 0x0000ffbf;
34 mxcsr_feature_mask
&= mask
;
39 * Called at bootup to set up the initial FPU state that is later cloned
42 void __cpuinit
fpu_init(void)
44 unsigned long oldcr0
= read_cr0();
45 extern void __bad_fxsave_alignment(void);
47 if (offsetof(struct task_struct
, thread
.i387
.fxsave
) & 15)
48 __bad_fxsave_alignment();
49 set_in_cr4(X86_CR4_OSFXSR
);
50 set_in_cr4(X86_CR4_OSXMMEXCPT
);
52 write_cr0(oldcr0
& ~((1UL<<3)|(1UL<<2))); /* clear TS and EM */
54 mxcsr_feature_mask_init();
55 /* clean state in init */
56 current_thread_info()->status
= 0;
60 void init_fpu(struct task_struct
*child
)
62 if (tsk_used_math(child
)) {
67 memset(&child
->thread
.i387
.fxsave
, 0, sizeof(struct i387_fxsave_struct
));
68 child
->thread
.i387
.fxsave
.cwd
= 0x37f;
69 child
->thread
.i387
.fxsave
.mxcsr
= 0x1f80;
70 /* only the device not available exception or ptrace can call init_fpu */
71 set_stopped_child_used_math(child
);
75 * Signal frame handlers.
78 int save_i387(struct _fpstate __user
*buf
)
80 struct task_struct
*tsk
= current
;
83 BUILD_BUG_ON(sizeof(struct user_i387_struct
) !=
84 sizeof(tsk
->thread
.i387
.fxsave
));
86 if ((unsigned long)buf
% 16)
87 printk("save_i387: bad fpstate %p\n",buf
);
91 clear_used_math(); /* trigger finit */
92 if (task_thread_info(tsk
)->status
& TS_USEDFPU
) {
93 err
= save_i387_checking((struct i387_fxsave_struct __user
*)buf
);
97 if (__copy_to_user(buf
, &tsk
->thread
.i387
.fxsave
,
98 sizeof(struct i387_fxsave_struct
)))
105 * ptrace request handlers.
108 int get_fpregs(struct user_i387_struct __user
*buf
, struct task_struct
*tsk
)
111 return __copy_to_user(buf
, &tsk
->thread
.i387
.fxsave
,
112 sizeof(struct user_i387_struct
)) ? -EFAULT
: 0;
115 int set_fpregs(struct task_struct
*tsk
, struct user_i387_struct __user
*buf
)
117 if (__copy_from_user(&tsk
->thread
.i387
.fxsave
, buf
,
118 sizeof(struct user_i387_struct
)))
124 * FPU state for core dumps.
127 int dump_fpu( struct pt_regs
*regs
, struct user_i387_struct
*fpu
)
129 struct task_struct
*tsk
= current
;
135 memcpy(fpu
, &tsk
->thread
.i387
.fxsave
, sizeof(struct user_i387_struct
));
139 int dump_task_fpu(struct task_struct
*tsk
, struct user_i387_struct
*fpu
)
141 int fpvalid
= !!tsk_used_math(tsk
);
146 memcpy(fpu
, &tsk
->thread
.i387
.fxsave
, sizeof(struct user_i387_struct
));