1
2
3
4
5
6
7
8
9
10
11#include <asm/uaccess.h>
12#include <asm/fpu.h>
13#include <asm/elf.h>
14#include <asm/exceptions.h>
15
16#ifdef CONFIG_LAZY_SAVE_FPU
17struct task_struct *fpu_state_owner;
18#endif
19
20
21
22
23asmlinkage void fpu_disabled_in_kernel(struct pt_regs *regs)
24{
25 die_if_no_fixup("An FPU Disabled exception happened in kernel space\n",
26 regs, EXCEP_FPU_DISABLED);
27}
28
29
30
31
32
33
34asmlinkage void fpu_exception(struct pt_regs *regs, enum exception_code code)
35{
36 struct task_struct *tsk = current;
37 siginfo_t info;
38 u32 fpcr;
39
40 if (!user_mode(regs))
41 die_if_no_fixup("An FPU Operation exception happened in"
42 " kernel space\n",
43 regs, code);
44
45 if (!is_using_fpu(tsk))
46 die_if_no_fixup("An FPU Operation exception happened,"
47 " but the FPU is not in use",
48 regs, code);
49
50 info.si_signo = SIGFPE;
51 info.si_errno = 0;
52 info.si_addr = (void *) tsk->thread.uregs->pc;
53 info.si_code = FPE_FLTINV;
54
55 unlazy_fpu(tsk);
56
57 fpcr = tsk->thread.fpu_state.fpcr;
58
59 if (fpcr & FPCR_EC_Z)
60 info.si_code = FPE_FLTDIV;
61 else if (fpcr & FPCR_EC_O)
62 info.si_code = FPE_FLTOVF;
63 else if (fpcr & FPCR_EC_U)
64 info.si_code = FPE_FLTUND;
65 else if (fpcr & FPCR_EC_I)
66 info.si_code = FPE_FLTRES;
67
68 force_sig_info(SIGFPE, &info, tsk);
69}
70
71
72
73
74int fpu_setup_sigcontext(struct fpucontext *fpucontext)
75{
76 struct task_struct *tsk = current;
77
78 if (!is_using_fpu(tsk))
79 return 0;
80
81
82
83
84
85 preempt_disable();
86
87#ifndef CONFIG_LAZY_SAVE_FPU
88 if (tsk->thread.fpu_flags & THREAD_HAS_FPU) {
89 fpu_save(&tsk->thread.fpu_state);
90 tsk->thread.uregs->epsw &= ~EPSW_FE;
91 tsk->thread.fpu_flags &= ~THREAD_HAS_FPU;
92 }
93#else
94 if (fpu_state_owner == tsk) {
95 fpu_save(&tsk->thread.fpu_state);
96 fpu_state_owner->thread.uregs->epsw &= ~EPSW_FE;
97 fpu_state_owner = NULL;
98 }
99#endif
100
101 preempt_enable();
102
103
104 clear_using_fpu(tsk);
105
106
107 if (copy_to_user(fpucontext,
108 &tsk->thread.fpu_state,
109 min(sizeof(struct fpu_state_struct),
110 sizeof(struct fpucontext))))
111 return -1;
112
113 return 1;
114}
115
116
117
118
119void fpu_kill_state(struct task_struct *tsk)
120{
121
122 preempt_disable();
123
124#ifndef CONFIG_LAZY_SAVE_FPU
125 if (tsk->thread.fpu_flags & THREAD_HAS_FPU) {
126 tsk->thread.uregs->epsw &= ~EPSW_FE;
127 tsk->thread.fpu_flags &= ~THREAD_HAS_FPU;
128 }
129#else
130 if (fpu_state_owner == tsk) {
131 fpu_state_owner->thread.uregs->epsw &= ~EPSW_FE;
132 fpu_state_owner = NULL;
133 }
134#endif
135
136 preempt_enable();
137
138
139 clear_using_fpu(tsk);
140}
141
142
143
144
145int fpu_restore_sigcontext(struct fpucontext *fpucontext)
146{
147 struct task_struct *tsk = current;
148 int ret;
149
150
151 ret = copy_from_user(&tsk->thread.fpu_state, fpucontext,
152 min(sizeof(struct fpu_state_struct),
153 sizeof(struct fpucontext)));
154 if (!ret)
155 set_using_fpu(tsk);
156
157 return ret;
158}
159
160
161
162
163int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpreg)
164{
165 struct task_struct *tsk = current;
166 int fpvalid;
167
168 fpvalid = is_using_fpu(tsk);
169 if (fpvalid) {
170 unlazy_fpu(tsk);
171 memcpy(fpreg, &tsk->thread.fpu_state, sizeof(*fpreg));
172 }
173
174 return fpvalid;
175}
176