patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / include / asm-x86_64 / i387.h
1 /*
2  * include/asm-x86_64/i387.h
3  *
4  * Copyright (C) 1994 Linus Torvalds
5  *
6  * Pentium III FXSR, SSE support
7  * General FPU state handling cleanups
8  *      Gareth Hughes <gareth@valinux.com>, May 2000
9  * x86-64 work by Andi Kleen 2002
10  */
11
12 #ifndef __ASM_X86_64_I387_H
13 #define __ASM_X86_64_I387_H
14
15 #include <linux/sched.h>
16 #include <asm/processor.h>
17 #include <asm/sigcontext.h>
18 #include <asm/user.h>
19 #include <asm/thread_info.h>
20 #include <asm/uaccess.h>
21
22 extern void fpu_init(void);
23 extern unsigned int mxcsr_feature_mask;
24 extern void mxcsr_feature_mask_init(void);
25 extern void init_fpu(struct task_struct *child);
26 extern int save_i387(struct _fpstate __user *buf);
27
28 static inline int need_signal_i387(struct task_struct *me) 
29
30         if (!me->used_math)
31                 return 0;
32         me->used_math = 0; 
33         if (me->thread_info->status & TS_USEDFPU)
34                 return 0;
35         return 1;
36
37
38 /*
39  * FPU lazy state save handling...
40  */
41
42 #define kernel_fpu_end() stts()
43
44 #define unlazy_fpu(tsk) do { \
45         if ((tsk)->thread_info->status & TS_USEDFPU) \
46                 save_init_fpu(tsk); \
47 } while (0)
48
49 #define clear_fpu(tsk) do { \
50         if ((tsk)->thread_info->status & TS_USEDFPU) {          \
51                 asm volatile("fnclex ; fwait");                 \
52                 (tsk)->thread_info->status &= ~TS_USEDFPU;      \
53                 stts();                                         \
54         }                                                       \
55 } while (0)
56
57 /*
58  * ptrace request handers...
59  */
60 extern int get_fpregs(struct user_i387_struct __user *buf,
61                       struct task_struct *tsk);
62 extern int set_fpregs(struct task_struct *tsk,
63                       struct user_i387_struct __user *buf);
64
65 /*
66  * i387 state interaction
67  */
68 #define get_fpu_mxcsr(t) ((t)->thread.i387.fxsave.mxcsr)
69 #define get_fpu_cwd(t) ((t)->thread.i387.fxsave.cwd)
70 #define get_fpu_fxsr_twd(t) ((t)->thread.i387.fxsave.twd)
71 #define get_fpu_swd(t) ((t)->thread.i387.fxsave.swd)
72 #define set_fpu_cwd(t,val) ((t)->thread.i387.fxsave.cwd = (val))
73 #define set_fpu_swd(t,val) ((t)->thread.i387.fxsave.swd = (val))
74 #define set_fpu_fxsr_twd(t,val) ((t)->thread.i387.fxsave.twd = (val))
75
76 static inline int restore_fpu_checking(struct i387_fxsave_struct *fx) 
77
78         int err;
79         asm volatile("1:  rex64 ; fxrstor (%[fx])\n\t"
80                      "2:\n"
81                      ".section .fixup,\"ax\"\n"
82                      "3:  movl $-1,%[err]\n"
83                      "    jmp  2b\n"
84                      ".previous\n"
85                      ".section __ex_table,\"a\"\n"
86                      "   .align 8\n"
87                      "   .quad  1b,3b\n"
88                      ".previous"
89                      : [err] "=r" (err)
90                      : [fx] "r" (fx), "0" (0)); 
91         if (unlikely(err))
92                 init_fpu(current);
93         return err;
94
95
96 static inline int save_i387_checking(struct i387_fxsave_struct __user *fx) 
97
98         int err;
99         asm volatile("1:  rex64 ; fxsave (%[fx])\n\t"
100                      "2:\n"
101                      ".section .fixup,\"ax\"\n"
102                      "3:  movl $-1,%[err]\n"
103                      "    jmp  2b\n"
104                      ".previous\n"
105                      ".section __ex_table,\"a\"\n"
106                      "   .align 8\n"
107                      "   .quad  1b,3b\n"
108                      ".previous"
109                      : [err] "=r" (err)
110                      : [fx] "r" (fx), "0" (0)); 
111         if (unlikely(err))
112                 __clear_user(fx, sizeof(struct i387_fxsave_struct));
113         return err;
114
115
116 static inline void kernel_fpu_begin(void)
117 {
118         struct thread_info *me = current_thread_info();
119         if (me->status & TS_USEDFPU) { 
120                 asm volatile("rex64 ; fxsave %0 ; fnclex"
121                               : "=m" (me->task->thread.i387.fxsave));
122                 me->status &= ~TS_USEDFPU;
123                 return;
124         }
125         clts();
126 }
127
128 static inline void save_init_fpu( struct task_struct *tsk )
129 {
130         asm volatile( "fxsave %0 ; fnclex"
131                       : "=m" (tsk->thread.i387.fxsave));
132         tsk->thread_info->status &= ~TS_USEDFPU;
133         stts();
134 }
135
136 /* 
137  * This restores directly out of user space. Exceptions are handled.
138  */
139 static inline int restore_i387(struct _fpstate __user *buf)
140 {
141         return restore_fpu_checking((struct i387_fxsave_struct *)buf);
142 }
143
144 #endif /* __ASM_X86_64_I387_H */