MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / arch / x86_64 / kernel / i387.c
blob6bc3d64e0095f8b3116bf3d155af2b72f03244f9
1 /*
2 * linux/arch/x86_64/kernel/i387.c
4 * Copyright (C) 1994 Linus Torvalds
5 * Copyright (C) 2002 Andi Kleen, SuSE Labs
7 * Pentium III FXSR, SSE support
8 * General FPU state handling cleanups
9 * Gareth Hughes <gareth@valinux.com>, May 2000
11 * x86-64 rework 2002 Andi Kleen.
12 * Does direct fxsave in and out of user space now for signal handlers.
13 * All the FSAVE<->FXSAVE conversion code has been moved to the 32bit emulation,
14 * the 64bit user space sees a FXSAVE frame directly.
17 #include <linux/config.h>
18 #include <linux/sched.h>
19 #include <linux/init.h>
20 #include <asm/processor.h>
21 #include <asm/i387.h>
22 #include <asm/sigcontext.h>
23 #include <asm/user.h>
24 #include <asm/ptrace.h>
25 #include <asm/uaccess.h>
27 unsigned int mxcsr_feature_mask = 0xffffffff;
29 void mxcsr_feature_mask_init(void)
31 unsigned int mask;
32 clts();
33 memset(&current->thread.i387.fxsave, 0, sizeof(struct i387_fxsave_struct));
34 asm volatile("fxsave %0" : : "m" (current->thread.i387.fxsave));
35 mask = current->thread.i387.fxsave.mxcsr_mask;
36 if (mask == 0) mask = 0x0000ffbf;
37 mxcsr_feature_mask &= mask;
38 stts();
42 * Called at bootup to set up the initial FPU state that is later cloned
43 * into all processes.
45 void __init fpu_init(void)
47 unsigned long oldcr0 = read_cr0();
48 extern void __bad_fxsave_alignment(void);
50 if (offsetof(struct task_struct, thread.i387.fxsave) & 15)
51 __bad_fxsave_alignment();
52 set_in_cr4(X86_CR4_OSFXSR);
53 set_in_cr4(X86_CR4_OSXMMEXCPT);
55 write_cr0(oldcr0 & ~((1UL<<3)|(1UL<<2))); /* clear TS and EM */
57 mxcsr_feature_mask_init();
58 /* clean state in init */
59 current_thread_info()->status = 0;
60 current->used_math = 0;
63 void init_fpu(struct task_struct *child)
65 if (child->used_math) {
66 if (child == current)
67 unlazy_fpu(child);
68 return;
70 memset(&child->thread.i387.fxsave, 0, sizeof(struct i387_fxsave_struct));
71 child->thread.i387.fxsave.cwd = 0x37f;
72 child->thread.i387.fxsave.mxcsr = 0x1f80;
73 child->used_math = 1;
77 * Signal frame handlers.
80 int save_i387(struct _fpstate __user *buf)
82 struct task_struct *tsk = current;
83 int err = 0;
86 extern void bad_user_i387_struct(void);
87 if (sizeof(struct user_i387_struct) != sizeof(tsk->thread.i387.fxsave))
88 bad_user_i387_struct();
91 if ((unsigned long)buf % 16)
92 printk("save_i387: bad fpstate %p\n",buf);
94 if (!tsk->used_math)
95 return 0;
96 tsk->used_math = 0; /* trigger finit */
97 if (tsk->thread_info->status & TS_USEDFPU) {
98 err = save_i387_checking((struct i387_fxsave_struct __user *)buf);
99 if (err) return err;
100 stts();
101 } else {
102 if (__copy_to_user(buf, &tsk->thread.i387.fxsave,
103 sizeof(struct i387_fxsave_struct)))
104 return -1;
106 return 1;
110 * ptrace request handlers.
113 int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *tsk)
115 init_fpu(tsk);
116 return __copy_to_user(buf, &tsk->thread.i387.fxsave,
117 sizeof(struct user_i387_struct)) ? -EFAULT : 0;
120 int set_fpregs(struct task_struct *tsk, struct user_i387_struct __user *buf)
122 if (__copy_from_user(&tsk->thread.i387.fxsave, buf,
123 sizeof(struct user_i387_struct)))
124 return -EFAULT;
125 return 0;
129 * FPU state for core dumps.
132 int dump_fpu( struct pt_regs *regs, struct user_i387_struct *fpu )
134 struct task_struct *tsk = current;
136 if (!tsk->used_math)
137 return 0;
139 unlazy_fpu(tsk);
140 memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(struct user_i387_struct));
141 return 1;
144 int dump_task_fpu(struct task_struct *tsk, struct user_i387_struct *fpu)
146 int fpvalid = tsk->used_math;
148 if (fpvalid) {
149 if (tsk == current)
150 unlazy_fpu(tsk);
151 memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(struct user_i387_struct));
153 return fpvalid;