Loading...
1/*
2 * Save/restore floating point context for signal handlers.
3 *
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
6 * for more details.
7 *
8 * Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
9 * Copyright (C) 2006 ST Microelectronics Ltd. (denorm support)
10 *
11 * FIXME! These routines have not been tested for big endian case.
12 */
13#include <linux/sched.h>
14#include <linux/signal.h>
15#include <linux/io.h>
16#include <cpu/fpu.h>
17#include <asm/processor.h>
18#include <asm/fpu.h>
19#include <asm/traps.h>
20
21/* The PR (precision) bit in the FP Status Register must be clear when
22 * an frchg instruction is executed, otherwise the instruction is undefined.
23 * Executing frchg with PR set causes a trap on some SH4 implementations.
24 */
25
26#define FPSCR_RCHG 0x00000000
27extern unsigned long long float64_div(unsigned long long a,
28 unsigned long long b);
29extern unsigned long int float32_div(unsigned long int a, unsigned long int b);
30extern unsigned long long float64_mul(unsigned long long a,
31 unsigned long long b);
32extern unsigned long int float32_mul(unsigned long int a, unsigned long int b);
33extern unsigned long long float64_add(unsigned long long a,
34 unsigned long long b);
35extern unsigned long int float32_add(unsigned long int a, unsigned long int b);
36extern unsigned long long float64_sub(unsigned long long a,
37 unsigned long long b);
38extern unsigned long int float32_sub(unsigned long int a, unsigned long int b);
39extern unsigned long int float64_to_float32(unsigned long long a);
40static unsigned int fpu_exception_flags;
41
42/*
43 * Save FPU registers onto task structure.
44 */
45void save_fpu(struct task_struct *tsk)
46{
47 unsigned long dummy;
48
49 enable_fpu();
50 asm volatile ("sts.l fpul, @-%0\n\t"
51 "sts.l fpscr, @-%0\n\t"
52 "lds %2, fpscr\n\t"
53 "frchg\n\t"
54 "fmov.s fr15, @-%0\n\t"
55 "fmov.s fr14, @-%0\n\t"
56 "fmov.s fr13, @-%0\n\t"
57 "fmov.s fr12, @-%0\n\t"
58 "fmov.s fr11, @-%0\n\t"
59 "fmov.s fr10, @-%0\n\t"
60 "fmov.s fr9, @-%0\n\t"
61 "fmov.s fr8, @-%0\n\t"
62 "fmov.s fr7, @-%0\n\t"
63 "fmov.s fr6, @-%0\n\t"
64 "fmov.s fr5, @-%0\n\t"
65 "fmov.s fr4, @-%0\n\t"
66 "fmov.s fr3, @-%0\n\t"
67 "fmov.s fr2, @-%0\n\t"
68 "fmov.s fr1, @-%0\n\t"
69 "fmov.s fr0, @-%0\n\t"
70 "frchg\n\t"
71 "fmov.s fr15, @-%0\n\t"
72 "fmov.s fr14, @-%0\n\t"
73 "fmov.s fr13, @-%0\n\t"
74 "fmov.s fr12, @-%0\n\t"
75 "fmov.s fr11, @-%0\n\t"
76 "fmov.s fr10, @-%0\n\t"
77 "fmov.s fr9, @-%0\n\t"
78 "fmov.s fr8, @-%0\n\t"
79 "fmov.s fr7, @-%0\n\t"
80 "fmov.s fr6, @-%0\n\t"
81 "fmov.s fr5, @-%0\n\t"
82 "fmov.s fr4, @-%0\n\t"
83 "fmov.s fr3, @-%0\n\t"
84 "fmov.s fr2, @-%0\n\t"
85 "fmov.s fr1, @-%0\n\t"
86 "fmov.s fr0, @-%0\n\t"
87 "lds %3, fpscr\n\t":"=r" (dummy)
88 :"0"((char *)(&tsk->thread.xstate->hardfpu.status)),
89 "r"(FPSCR_RCHG), "r"(FPSCR_INIT)
90 :"memory");
91
92 disable_fpu();
93}
94
95void restore_fpu(struct task_struct *tsk)
96{
97 unsigned long dummy;
98
99 enable_fpu();
100 asm volatile ("lds %2, fpscr\n\t"
101 "fmov.s @%0+, fr0\n\t"
102 "fmov.s @%0+, fr1\n\t"
103 "fmov.s @%0+, fr2\n\t"
104 "fmov.s @%0+, fr3\n\t"
105 "fmov.s @%0+, fr4\n\t"
106 "fmov.s @%0+, fr5\n\t"
107 "fmov.s @%0+, fr6\n\t"
108 "fmov.s @%0+, fr7\n\t"
109 "fmov.s @%0+, fr8\n\t"
110 "fmov.s @%0+, fr9\n\t"
111 "fmov.s @%0+, fr10\n\t"
112 "fmov.s @%0+, fr11\n\t"
113 "fmov.s @%0+, fr12\n\t"
114 "fmov.s @%0+, fr13\n\t"
115 "fmov.s @%0+, fr14\n\t"
116 "fmov.s @%0+, fr15\n\t"
117 "frchg\n\t"
118 "fmov.s @%0+, fr0\n\t"
119 "fmov.s @%0+, fr1\n\t"
120 "fmov.s @%0+, fr2\n\t"
121 "fmov.s @%0+, fr3\n\t"
122 "fmov.s @%0+, fr4\n\t"
123 "fmov.s @%0+, fr5\n\t"
124 "fmov.s @%0+, fr6\n\t"
125 "fmov.s @%0+, fr7\n\t"
126 "fmov.s @%0+, fr8\n\t"
127 "fmov.s @%0+, fr9\n\t"
128 "fmov.s @%0+, fr10\n\t"
129 "fmov.s @%0+, fr11\n\t"
130 "fmov.s @%0+, fr12\n\t"
131 "fmov.s @%0+, fr13\n\t"
132 "fmov.s @%0+, fr14\n\t"
133 "fmov.s @%0+, fr15\n\t"
134 "frchg\n\t"
135 "lds.l @%0+, fpscr\n\t"
136 "lds.l @%0+, fpul\n\t"
137 :"=r" (dummy)
138 :"0" (tsk->thread.xstate), "r" (FPSCR_RCHG)
139 :"memory");
140 disable_fpu();
141}
142
143/**
144 * denormal_to_double - Given denormalized float number,
145 * store double float
146 *
147 * @fpu: Pointer to sh_fpu_hard structure
148 * @n: Index to FP register
149 */
150static void denormal_to_double(struct sh_fpu_hard_struct *fpu, int n)
151{
152 unsigned long du, dl;
153 unsigned long x = fpu->fpul;
154 int exp = 1023 - 126;
155
156 if (x != 0 && (x & 0x7f800000) == 0) {
157 du = (x & 0x80000000);
158 while ((x & 0x00800000) == 0) {
159 x <<= 1;
160 exp--;
161 }
162 x &= 0x007fffff;
163 du |= (exp << 20) | (x >> 3);
164 dl = x << 29;
165
166 fpu->fp_regs[n] = du;
167 fpu->fp_regs[n + 1] = dl;
168 }
169}
170
171/**
172 * ieee_fpe_handler - Handle denormalized number exception
173 *
174 * @regs: Pointer to register structure
175 *
176 * Returns 1 when it's handled (should not cause exception).
177 */
178static int ieee_fpe_handler(struct pt_regs *regs)
179{
180 unsigned short insn = *(unsigned short *)regs->pc;
181 unsigned short finsn;
182 unsigned long nextpc;
183 int nib[4] = {
184 (insn >> 12) & 0xf,
185 (insn >> 8) & 0xf,
186 (insn >> 4) & 0xf,
187 insn & 0xf
188 };
189
190 if (nib[0] == 0xb || (nib[0] == 0x4 && nib[2] == 0x0 && nib[3] == 0xb))
191 regs->pr = regs->pc + 4; /* bsr & jsr */
192
193 if (nib[0] == 0xa || nib[0] == 0xb) {
194 /* bra & bsr */
195 nextpc = regs->pc + 4 + ((short)((insn & 0xfff) << 4) >> 3);
196 finsn = *(unsigned short *)(regs->pc + 2);
197 } else if (nib[0] == 0x8 && nib[1] == 0xd) {
198 /* bt/s */
199 if (regs->sr & 1)
200 nextpc = regs->pc + 4 + ((char)(insn & 0xff) << 1);
201 else
202 nextpc = regs->pc + 4;
203 finsn = *(unsigned short *)(regs->pc + 2);
204 } else if (nib[0] == 0x8 && nib[1] == 0xf) {
205 /* bf/s */
206 if (regs->sr & 1)
207 nextpc = regs->pc + 4;
208 else
209 nextpc = regs->pc + 4 + ((char)(insn & 0xff) << 1);
210 finsn = *(unsigned short *)(regs->pc + 2);
211 } else if (nib[0] == 0x4 && nib[3] == 0xb &&
212 (nib[2] == 0x0 || nib[2] == 0x2)) {
213 /* jmp & jsr */
214 nextpc = regs->regs[nib[1]];
215 finsn = *(unsigned short *)(regs->pc + 2);
216 } else if (nib[0] == 0x0 && nib[3] == 0x3 &&
217 (nib[2] == 0x0 || nib[2] == 0x2)) {
218 /* braf & bsrf */
219 nextpc = regs->pc + 4 + regs->regs[nib[1]];
220 finsn = *(unsigned short *)(regs->pc + 2);
221 } else if (insn == 0x000b) {
222 /* rts */
223 nextpc = regs->pr;
224 finsn = *(unsigned short *)(regs->pc + 2);
225 } else {
226 nextpc = regs->pc + instruction_size(insn);
227 finsn = insn;
228 }
229
230 if ((finsn & 0xf1ff) == 0xf0ad) {
231 /* fcnvsd */
232 struct task_struct *tsk = current;
233
234 if ((tsk->thread.xstate->hardfpu.fpscr & FPSCR_CAUSE_ERROR))
235 /* FPU error */
236 denormal_to_double(&tsk->thread.xstate->hardfpu,
237 (finsn >> 8) & 0xf);
238 else
239 return 0;
240
241 regs->pc = nextpc;
242 return 1;
243 } else if ((finsn & 0xf00f) == 0xf002) {
244 /* fmul */
245 struct task_struct *tsk = current;
246 int fpscr;
247 int n, m, prec;
248 unsigned int hx, hy;
249
250 n = (finsn >> 8) & 0xf;
251 m = (finsn >> 4) & 0xf;
252 hx = tsk->thread.xstate->hardfpu.fp_regs[n];
253 hy = tsk->thread.xstate->hardfpu.fp_regs[m];
254 fpscr = tsk->thread.xstate->hardfpu.fpscr;
255 prec = fpscr & FPSCR_DBL_PRECISION;
256
257 if ((fpscr & FPSCR_CAUSE_ERROR)
258 && (prec && ((hx & 0x7fffffff) < 0x00100000
259 || (hy & 0x7fffffff) < 0x00100000))) {
260 long long llx, lly;
261
262 /* FPU error because of denormal (doubles) */
263 llx = ((long long)hx << 32)
264 | tsk->thread.xstate->hardfpu.fp_regs[n + 1];
265 lly = ((long long)hy << 32)
266 | tsk->thread.xstate->hardfpu.fp_regs[m + 1];
267 llx = float64_mul(llx, lly);
268 tsk->thread.xstate->hardfpu.fp_regs[n] = llx >> 32;
269 tsk->thread.xstate->hardfpu.fp_regs[n + 1] = llx & 0xffffffff;
270 } else if ((fpscr & FPSCR_CAUSE_ERROR)
271 && (!prec && ((hx & 0x7fffffff) < 0x00800000
272 || (hy & 0x7fffffff) < 0x00800000))) {
273 /* FPU error because of denormal (floats) */
274 hx = float32_mul(hx, hy);
275 tsk->thread.xstate->hardfpu.fp_regs[n] = hx;
276 } else
277 return 0;
278
279 regs->pc = nextpc;
280 return 1;
281 } else if ((finsn & 0xf00e) == 0xf000) {
282 /* fadd, fsub */
283 struct task_struct *tsk = current;
284 int fpscr;
285 int n, m, prec;
286 unsigned int hx, hy;
287
288 n = (finsn >> 8) & 0xf;
289 m = (finsn >> 4) & 0xf;
290 hx = tsk->thread.xstate->hardfpu.fp_regs[n];
291 hy = tsk->thread.xstate->hardfpu.fp_regs[m];
292 fpscr = tsk->thread.xstate->hardfpu.fpscr;
293 prec = fpscr & FPSCR_DBL_PRECISION;
294
295 if ((fpscr & FPSCR_CAUSE_ERROR)
296 && (prec && ((hx & 0x7fffffff) < 0x00100000
297 || (hy & 0x7fffffff) < 0x00100000))) {
298 long long llx, lly;
299
300 /* FPU error because of denormal (doubles) */
301 llx = ((long long)hx << 32)
302 | tsk->thread.xstate->hardfpu.fp_regs[n + 1];
303 lly = ((long long)hy << 32)
304 | tsk->thread.xstate->hardfpu.fp_regs[m + 1];
305 if ((finsn & 0xf00f) == 0xf000)
306 llx = float64_add(llx, lly);
307 else
308 llx = float64_sub(llx, lly);
309 tsk->thread.xstate->hardfpu.fp_regs[n] = llx >> 32;
310 tsk->thread.xstate->hardfpu.fp_regs[n + 1] = llx & 0xffffffff;
311 } else if ((fpscr & FPSCR_CAUSE_ERROR)
312 && (!prec && ((hx & 0x7fffffff) < 0x00800000
313 || (hy & 0x7fffffff) < 0x00800000))) {
314 /* FPU error because of denormal (floats) */
315 if ((finsn & 0xf00f) == 0xf000)
316 hx = float32_add(hx, hy);
317 else
318 hx = float32_sub(hx, hy);
319 tsk->thread.xstate->hardfpu.fp_regs[n] = hx;
320 } else
321 return 0;
322
323 regs->pc = nextpc;
324 return 1;
325 } else if ((finsn & 0xf003) == 0xf003) {
326 /* fdiv */
327 struct task_struct *tsk = current;
328 int fpscr;
329 int n, m, prec;
330 unsigned int hx, hy;
331
332 n = (finsn >> 8) & 0xf;
333 m = (finsn >> 4) & 0xf;
334 hx = tsk->thread.xstate->hardfpu.fp_regs[n];
335 hy = tsk->thread.xstate->hardfpu.fp_regs[m];
336 fpscr = tsk->thread.xstate->hardfpu.fpscr;
337 prec = fpscr & FPSCR_DBL_PRECISION;
338
339 if ((fpscr & FPSCR_CAUSE_ERROR)
340 && (prec && ((hx & 0x7fffffff) < 0x00100000
341 || (hy & 0x7fffffff) < 0x00100000))) {
342 long long llx, lly;
343
344 /* FPU error because of denormal (doubles) */
345 llx = ((long long)hx << 32)
346 | tsk->thread.xstate->hardfpu.fp_regs[n + 1];
347 lly = ((long long)hy << 32)
348 | tsk->thread.xstate->hardfpu.fp_regs[m + 1];
349
350 llx = float64_div(llx, lly);
351
352 tsk->thread.xstate->hardfpu.fp_regs[n] = llx >> 32;
353 tsk->thread.xstate->hardfpu.fp_regs[n + 1] = llx & 0xffffffff;
354 } else if ((fpscr & FPSCR_CAUSE_ERROR)
355 && (!prec && ((hx & 0x7fffffff) < 0x00800000
356 || (hy & 0x7fffffff) < 0x00800000))) {
357 /* FPU error because of denormal (floats) */
358 hx = float32_div(hx, hy);
359 tsk->thread.xstate->hardfpu.fp_regs[n] = hx;
360 } else
361 return 0;
362
363 regs->pc = nextpc;
364 return 1;
365 } else if ((finsn & 0xf0bd) == 0xf0bd) {
366 /* fcnvds - double to single precision convert */
367 struct task_struct *tsk = current;
368 int m;
369 unsigned int hx;
370
371 m = (finsn >> 8) & 0x7;
372 hx = tsk->thread.xstate->hardfpu.fp_regs[m];
373
374 if ((tsk->thread.xstate->hardfpu.fpscr & FPSCR_CAUSE_ERROR)
375 && ((hx & 0x7fffffff) < 0x00100000)) {
376 /* subnormal double to float conversion */
377 long long llx;
378
379 llx = ((long long)tsk->thread.xstate->hardfpu.fp_regs[m] << 32)
380 | tsk->thread.xstate->hardfpu.fp_regs[m + 1];
381
382 tsk->thread.xstate->hardfpu.fpul = float64_to_float32(llx);
383 } else
384 return 0;
385
386 regs->pc = nextpc;
387 return 1;
388 }
389
390 return 0;
391}
392
393void float_raise(unsigned int flags)
394{
395 fpu_exception_flags |= flags;
396}
397
398int float_rounding_mode(void)
399{
400 struct task_struct *tsk = current;
401 int roundingMode = FPSCR_ROUNDING_MODE(tsk->thread.xstate->hardfpu.fpscr);
402 return roundingMode;
403}
404
405BUILD_TRAP_HANDLER(fpu_error)
406{
407 struct task_struct *tsk = current;
408 TRAP_HANDLER_DECL;
409
410 __unlazy_fpu(tsk, regs);
411 fpu_exception_flags = 0;
412 if (ieee_fpe_handler(regs)) {
413 tsk->thread.xstate->hardfpu.fpscr &=
414 ~(FPSCR_CAUSE_MASK | FPSCR_FLAG_MASK);
415 tsk->thread.xstate->hardfpu.fpscr |= fpu_exception_flags;
416 /* Set the FPSCR flag as well as cause bits - simply
417 * replicate the cause */
418 tsk->thread.xstate->hardfpu.fpscr |= (fpu_exception_flags >> 10);
419 grab_fpu(regs);
420 restore_fpu(tsk);
421 task_thread_info(tsk)->status |= TS_USEDFPU;
422 if ((((tsk->thread.xstate->hardfpu.fpscr & FPSCR_ENABLE_MASK) >> 7) &
423 (fpu_exception_flags >> 2)) == 0) {
424 return;
425 }
426 }
427
428 force_sig(SIGFPE, tsk);
429}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Save/restore floating point context for signal handlers.
4 *
5 * Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
6 * Copyright (C) 2006 ST Microelectronics Ltd. (denorm support)
7 *
8 * FIXME! These routines have not been tested for big endian case.
9 */
10#include <linux/sched/signal.h>
11#include <linux/io.h>
12#include <cpu/fpu.h>
13#include <asm/processor.h>
14#include <asm/fpu.h>
15#include <asm/traps.h>
16
17/* The PR (precision) bit in the FP Status Register must be clear when
18 * an frchg instruction is executed, otherwise the instruction is undefined.
19 * Executing frchg with PR set causes a trap on some SH4 implementations.
20 */
21
22#define FPSCR_RCHG 0x00000000
23extern unsigned long long float64_div(unsigned long long a,
24 unsigned long long b);
25extern unsigned long int float32_div(unsigned long int a, unsigned long int b);
26extern unsigned long long float64_mul(unsigned long long a,
27 unsigned long long b);
28extern unsigned long int float32_mul(unsigned long int a, unsigned long int b);
29extern unsigned long long float64_add(unsigned long long a,
30 unsigned long long b);
31extern unsigned long int float32_add(unsigned long int a, unsigned long int b);
32extern unsigned long long float64_sub(unsigned long long a,
33 unsigned long long b);
34extern unsigned long int float32_sub(unsigned long int a, unsigned long int b);
35extern unsigned long int float64_to_float32(unsigned long long a);
36static unsigned int fpu_exception_flags;
37
38/*
39 * Save FPU registers onto task structure.
40 */
41void save_fpu(struct task_struct *tsk)
42{
43 unsigned long dummy;
44
45 enable_fpu();
46 asm volatile ("sts.l fpul, @-%0\n\t"
47 "sts.l fpscr, @-%0\n\t"
48 "lds %2, fpscr\n\t"
49 "frchg\n\t"
50 "fmov.s fr15, @-%0\n\t"
51 "fmov.s fr14, @-%0\n\t"
52 "fmov.s fr13, @-%0\n\t"
53 "fmov.s fr12, @-%0\n\t"
54 "fmov.s fr11, @-%0\n\t"
55 "fmov.s fr10, @-%0\n\t"
56 "fmov.s fr9, @-%0\n\t"
57 "fmov.s fr8, @-%0\n\t"
58 "fmov.s fr7, @-%0\n\t"
59 "fmov.s fr6, @-%0\n\t"
60 "fmov.s fr5, @-%0\n\t"
61 "fmov.s fr4, @-%0\n\t"
62 "fmov.s fr3, @-%0\n\t"
63 "fmov.s fr2, @-%0\n\t"
64 "fmov.s fr1, @-%0\n\t"
65 "fmov.s fr0, @-%0\n\t"
66 "frchg\n\t"
67 "fmov.s fr15, @-%0\n\t"
68 "fmov.s fr14, @-%0\n\t"
69 "fmov.s fr13, @-%0\n\t"
70 "fmov.s fr12, @-%0\n\t"
71 "fmov.s fr11, @-%0\n\t"
72 "fmov.s fr10, @-%0\n\t"
73 "fmov.s fr9, @-%0\n\t"
74 "fmov.s fr8, @-%0\n\t"
75 "fmov.s fr7, @-%0\n\t"
76 "fmov.s fr6, @-%0\n\t"
77 "fmov.s fr5, @-%0\n\t"
78 "fmov.s fr4, @-%0\n\t"
79 "fmov.s fr3, @-%0\n\t"
80 "fmov.s fr2, @-%0\n\t"
81 "fmov.s fr1, @-%0\n\t"
82 "fmov.s fr0, @-%0\n\t"
83 "lds %3, fpscr\n\t":"=r" (dummy)
84 :"0"((char *)(&tsk->thread.xstate->hardfpu.status)),
85 "r"(FPSCR_RCHG), "r"(FPSCR_INIT)
86 :"memory");
87
88 disable_fpu();
89}
90
91void restore_fpu(struct task_struct *tsk)
92{
93 unsigned long dummy;
94
95 enable_fpu();
96 asm volatile ("lds %2, fpscr\n\t"
97 "fmov.s @%0+, fr0\n\t"
98 "fmov.s @%0+, fr1\n\t"
99 "fmov.s @%0+, fr2\n\t"
100 "fmov.s @%0+, fr3\n\t"
101 "fmov.s @%0+, fr4\n\t"
102 "fmov.s @%0+, fr5\n\t"
103 "fmov.s @%0+, fr6\n\t"
104 "fmov.s @%0+, fr7\n\t"
105 "fmov.s @%0+, fr8\n\t"
106 "fmov.s @%0+, fr9\n\t"
107 "fmov.s @%0+, fr10\n\t"
108 "fmov.s @%0+, fr11\n\t"
109 "fmov.s @%0+, fr12\n\t"
110 "fmov.s @%0+, fr13\n\t"
111 "fmov.s @%0+, fr14\n\t"
112 "fmov.s @%0+, fr15\n\t"
113 "frchg\n\t"
114 "fmov.s @%0+, fr0\n\t"
115 "fmov.s @%0+, fr1\n\t"
116 "fmov.s @%0+, fr2\n\t"
117 "fmov.s @%0+, fr3\n\t"
118 "fmov.s @%0+, fr4\n\t"
119 "fmov.s @%0+, fr5\n\t"
120 "fmov.s @%0+, fr6\n\t"
121 "fmov.s @%0+, fr7\n\t"
122 "fmov.s @%0+, fr8\n\t"
123 "fmov.s @%0+, fr9\n\t"
124 "fmov.s @%0+, fr10\n\t"
125 "fmov.s @%0+, fr11\n\t"
126 "fmov.s @%0+, fr12\n\t"
127 "fmov.s @%0+, fr13\n\t"
128 "fmov.s @%0+, fr14\n\t"
129 "fmov.s @%0+, fr15\n\t"
130 "frchg\n\t"
131 "lds.l @%0+, fpscr\n\t"
132 "lds.l @%0+, fpul\n\t"
133 :"=r" (dummy)
134 :"0" (tsk->thread.xstate), "r" (FPSCR_RCHG)
135 :"memory");
136 disable_fpu();
137}
138
139/**
140 * denormal_to_double - Given denormalized float number,
141 * store double float
142 *
143 * @fpu: Pointer to sh_fpu_hard structure
144 * @n: Index to FP register
145 */
146static void denormal_to_double(struct sh_fpu_hard_struct *fpu, int n)
147{
148 unsigned long du, dl;
149 unsigned long x = fpu->fpul;
150 int exp = 1023 - 126;
151
152 if (x != 0 && (x & 0x7f800000) == 0) {
153 du = (x & 0x80000000);
154 while ((x & 0x00800000) == 0) {
155 x <<= 1;
156 exp--;
157 }
158 x &= 0x007fffff;
159 du |= (exp << 20) | (x >> 3);
160 dl = x << 29;
161
162 fpu->fp_regs[n] = du;
163 fpu->fp_regs[n + 1] = dl;
164 }
165}
166
167/**
168 * ieee_fpe_handler - Handle denormalized number exception
169 *
170 * @regs: Pointer to register structure
171 *
172 * Returns 1 when it's handled (should not cause exception).
173 */
174static int ieee_fpe_handler(struct pt_regs *regs)
175{
176 unsigned short insn = *(unsigned short *)regs->pc;
177 unsigned short finsn;
178 unsigned long nextpc;
179 int nib[4] = {
180 (insn >> 12) & 0xf,
181 (insn >> 8) & 0xf,
182 (insn >> 4) & 0xf,
183 insn & 0xf
184 };
185
186 if (nib[0] == 0xb || (nib[0] == 0x4 && nib[2] == 0x0 && nib[3] == 0xb))
187 regs->pr = regs->pc + 4; /* bsr & jsr */
188
189 if (nib[0] == 0xa || nib[0] == 0xb) {
190 /* bra & bsr */
191 nextpc = regs->pc + 4 + ((short)((insn & 0xfff) << 4) >> 3);
192 finsn = *(unsigned short *)(regs->pc + 2);
193 } else if (nib[0] == 0x8 && nib[1] == 0xd) {
194 /* bt/s */
195 if (regs->sr & 1)
196 nextpc = regs->pc + 4 + ((char)(insn & 0xff) << 1);
197 else
198 nextpc = regs->pc + 4;
199 finsn = *(unsigned short *)(regs->pc + 2);
200 } else if (nib[0] == 0x8 && nib[1] == 0xf) {
201 /* bf/s */
202 if (regs->sr & 1)
203 nextpc = regs->pc + 4;
204 else
205 nextpc = regs->pc + 4 + ((char)(insn & 0xff) << 1);
206 finsn = *(unsigned short *)(regs->pc + 2);
207 } else if (nib[0] == 0x4 && nib[3] == 0xb &&
208 (nib[2] == 0x0 || nib[2] == 0x2)) {
209 /* jmp & jsr */
210 nextpc = regs->regs[nib[1]];
211 finsn = *(unsigned short *)(regs->pc + 2);
212 } else if (nib[0] == 0x0 && nib[3] == 0x3 &&
213 (nib[2] == 0x0 || nib[2] == 0x2)) {
214 /* braf & bsrf */
215 nextpc = regs->pc + 4 + regs->regs[nib[1]];
216 finsn = *(unsigned short *)(regs->pc + 2);
217 } else if (insn == 0x000b) {
218 /* rts */
219 nextpc = regs->pr;
220 finsn = *(unsigned short *)(regs->pc + 2);
221 } else {
222 nextpc = regs->pc + instruction_size(insn);
223 finsn = insn;
224 }
225
226 if ((finsn & 0xf1ff) == 0xf0ad) {
227 /* fcnvsd */
228 struct task_struct *tsk = current;
229
230 if ((tsk->thread.xstate->hardfpu.fpscr & FPSCR_CAUSE_ERROR))
231 /* FPU error */
232 denormal_to_double(&tsk->thread.xstate->hardfpu,
233 (finsn >> 8) & 0xf);
234 else
235 return 0;
236
237 regs->pc = nextpc;
238 return 1;
239 } else if ((finsn & 0xf00f) == 0xf002) {
240 /* fmul */
241 struct task_struct *tsk = current;
242 int fpscr;
243 int n, m, prec;
244 unsigned int hx, hy;
245
246 n = (finsn >> 8) & 0xf;
247 m = (finsn >> 4) & 0xf;
248 hx = tsk->thread.xstate->hardfpu.fp_regs[n];
249 hy = tsk->thread.xstate->hardfpu.fp_regs[m];
250 fpscr = tsk->thread.xstate->hardfpu.fpscr;
251 prec = fpscr & FPSCR_DBL_PRECISION;
252
253 if ((fpscr & FPSCR_CAUSE_ERROR)
254 && (prec && ((hx & 0x7fffffff) < 0x00100000
255 || (hy & 0x7fffffff) < 0x00100000))) {
256 long long llx, lly;
257
258 /* FPU error because of denormal (doubles) */
259 llx = ((long long)hx << 32)
260 | tsk->thread.xstate->hardfpu.fp_regs[n + 1];
261 lly = ((long long)hy << 32)
262 | tsk->thread.xstate->hardfpu.fp_regs[m + 1];
263 llx = float64_mul(llx, lly);
264 tsk->thread.xstate->hardfpu.fp_regs[n] = llx >> 32;
265 tsk->thread.xstate->hardfpu.fp_regs[n + 1] = llx & 0xffffffff;
266 } else if ((fpscr & FPSCR_CAUSE_ERROR)
267 && (!prec && ((hx & 0x7fffffff) < 0x00800000
268 || (hy & 0x7fffffff) < 0x00800000))) {
269 /* FPU error because of denormal (floats) */
270 hx = float32_mul(hx, hy);
271 tsk->thread.xstate->hardfpu.fp_regs[n] = hx;
272 } else
273 return 0;
274
275 regs->pc = nextpc;
276 return 1;
277 } else if ((finsn & 0xf00e) == 0xf000) {
278 /* fadd, fsub */
279 struct task_struct *tsk = current;
280 int fpscr;
281 int n, m, prec;
282 unsigned int hx, hy;
283
284 n = (finsn >> 8) & 0xf;
285 m = (finsn >> 4) & 0xf;
286 hx = tsk->thread.xstate->hardfpu.fp_regs[n];
287 hy = tsk->thread.xstate->hardfpu.fp_regs[m];
288 fpscr = tsk->thread.xstate->hardfpu.fpscr;
289 prec = fpscr & FPSCR_DBL_PRECISION;
290
291 if ((fpscr & FPSCR_CAUSE_ERROR)
292 && (prec && ((hx & 0x7fffffff) < 0x00100000
293 || (hy & 0x7fffffff) < 0x00100000))) {
294 long long llx, lly;
295
296 /* FPU error because of denormal (doubles) */
297 llx = ((long long)hx << 32)
298 | tsk->thread.xstate->hardfpu.fp_regs[n + 1];
299 lly = ((long long)hy << 32)
300 | tsk->thread.xstate->hardfpu.fp_regs[m + 1];
301 if ((finsn & 0xf00f) == 0xf000)
302 llx = float64_add(llx, lly);
303 else
304 llx = float64_sub(llx, lly);
305 tsk->thread.xstate->hardfpu.fp_regs[n] = llx >> 32;
306 tsk->thread.xstate->hardfpu.fp_regs[n + 1] = llx & 0xffffffff;
307 } else if ((fpscr & FPSCR_CAUSE_ERROR)
308 && (!prec && ((hx & 0x7fffffff) < 0x00800000
309 || (hy & 0x7fffffff) < 0x00800000))) {
310 /* FPU error because of denormal (floats) */
311 if ((finsn & 0xf00f) == 0xf000)
312 hx = float32_add(hx, hy);
313 else
314 hx = float32_sub(hx, hy);
315 tsk->thread.xstate->hardfpu.fp_regs[n] = hx;
316 } else
317 return 0;
318
319 regs->pc = nextpc;
320 return 1;
321 } else if ((finsn & 0xf003) == 0xf003) {
322 /* fdiv */
323 struct task_struct *tsk = current;
324 int fpscr;
325 int n, m, prec;
326 unsigned int hx, hy;
327
328 n = (finsn >> 8) & 0xf;
329 m = (finsn >> 4) & 0xf;
330 hx = tsk->thread.xstate->hardfpu.fp_regs[n];
331 hy = tsk->thread.xstate->hardfpu.fp_regs[m];
332 fpscr = tsk->thread.xstate->hardfpu.fpscr;
333 prec = fpscr & FPSCR_DBL_PRECISION;
334
335 if ((fpscr & FPSCR_CAUSE_ERROR)
336 && (prec && ((hx & 0x7fffffff) < 0x00100000
337 || (hy & 0x7fffffff) < 0x00100000))) {
338 long long llx, lly;
339
340 /* FPU error because of denormal (doubles) */
341 llx = ((long long)hx << 32)
342 | tsk->thread.xstate->hardfpu.fp_regs[n + 1];
343 lly = ((long long)hy << 32)
344 | tsk->thread.xstate->hardfpu.fp_regs[m + 1];
345
346 llx = float64_div(llx, lly);
347
348 tsk->thread.xstate->hardfpu.fp_regs[n] = llx >> 32;
349 tsk->thread.xstate->hardfpu.fp_regs[n + 1] = llx & 0xffffffff;
350 } else if ((fpscr & FPSCR_CAUSE_ERROR)
351 && (!prec && ((hx & 0x7fffffff) < 0x00800000
352 || (hy & 0x7fffffff) < 0x00800000))) {
353 /* FPU error because of denormal (floats) */
354 hx = float32_div(hx, hy);
355 tsk->thread.xstate->hardfpu.fp_regs[n] = hx;
356 } else
357 return 0;
358
359 regs->pc = nextpc;
360 return 1;
361 } else if ((finsn & 0xf0bd) == 0xf0bd) {
362 /* fcnvds - double to single precision convert */
363 struct task_struct *tsk = current;
364 int m;
365 unsigned int hx;
366
367 m = (finsn >> 8) & 0x7;
368 hx = tsk->thread.xstate->hardfpu.fp_regs[m];
369
370 if ((tsk->thread.xstate->hardfpu.fpscr & FPSCR_CAUSE_ERROR)
371 && ((hx & 0x7fffffff) < 0x00100000)) {
372 /* subnormal double to float conversion */
373 long long llx;
374
375 llx = ((long long)tsk->thread.xstate->hardfpu.fp_regs[m] << 32)
376 | tsk->thread.xstate->hardfpu.fp_regs[m + 1];
377
378 tsk->thread.xstate->hardfpu.fpul = float64_to_float32(llx);
379 } else
380 return 0;
381
382 regs->pc = nextpc;
383 return 1;
384 }
385
386 return 0;
387}
388
389void float_raise(unsigned int flags)
390{
391 fpu_exception_flags |= flags;
392}
393
394int float_rounding_mode(void)
395{
396 struct task_struct *tsk = current;
397 int roundingMode = FPSCR_ROUNDING_MODE(tsk->thread.xstate->hardfpu.fpscr);
398 return roundingMode;
399}
400
401BUILD_TRAP_HANDLER(fpu_error)
402{
403 struct task_struct *tsk = current;
404 TRAP_HANDLER_DECL;
405
406 __unlazy_fpu(tsk, regs);
407 fpu_exception_flags = 0;
408 if (ieee_fpe_handler(regs)) {
409 tsk->thread.xstate->hardfpu.fpscr &=
410 ~(FPSCR_CAUSE_MASK | FPSCR_FLAG_MASK);
411 tsk->thread.xstate->hardfpu.fpscr |= fpu_exception_flags;
412 /* Set the FPSCR flag as well as cause bits - simply
413 * replicate the cause */
414 tsk->thread.xstate->hardfpu.fpscr |= (fpu_exception_flags >> 10);
415 grab_fpu(regs);
416 restore_fpu(tsk);
417 task_thread_info(tsk)->status |= TS_USEDFPU;
418 if ((((tsk->thread.xstate->hardfpu.fpscr & FPSCR_ENABLE_MASK) >> 7) &
419 (fpu_exception_flags >> 2)) == 0) {
420 return;
421 }
422 }
423
424 force_sig(SIGFPE);
425}