2 #include <linux/sched.h>
3 #include <linux/signal.h>
4 #include <asm/processor.h>
8 /* The PR (precision) bit in the FP Status Register must be clear when
9 * an frchg instruction is executed, otherwise the instruction is undefined.
10 * Executing frchg with PR set causes a trap on some SH4 implementations.
13 #define FPSCR_RCHG 0x00000000
17 * Save FPU registers onto task structure.
19 void save_fpu(struct task_struct
*tsk
)
24 asm volatile("sts.l fpul, @-%0\n\t"
25 "sts.l fpscr, @-%0\n\t"
26 "fmov.s fr15, @-%0\n\t"
27 "fmov.s fr14, @-%0\n\t"
28 "fmov.s fr13, @-%0\n\t"
29 "fmov.s fr12, @-%0\n\t"
30 "fmov.s fr11, @-%0\n\t"
31 "fmov.s fr10, @-%0\n\t"
32 "fmov.s fr9, @-%0\n\t"
33 "fmov.s fr8, @-%0\n\t"
34 "fmov.s fr7, @-%0\n\t"
35 "fmov.s fr6, @-%0\n\t"
36 "fmov.s fr5, @-%0\n\t"
37 "fmov.s fr4, @-%0\n\t"
38 "fmov.s fr3, @-%0\n\t"
39 "fmov.s fr2, @-%0\n\t"
40 "fmov.s fr1, @-%0\n\t"
41 "fmov.s fr0, @-%0\n\t"
44 : "0" ((char *)(&tsk
->thread
.xstate
->hardfpu
.status
)),
52 void restore_fpu(struct task_struct
*tsk
)
57 asm volatile("fmov.s @%0+, fr0\n\t"
58 "fmov.s @%0+, fr1\n\t"
59 "fmov.s @%0+, fr2\n\t"
60 "fmov.s @%0+, fr3\n\t"
61 "fmov.s @%0+, fr4\n\t"
62 "fmov.s @%0+, fr5\n\t"
63 "fmov.s @%0+, fr6\n\t"
64 "fmov.s @%0+, fr7\n\t"
65 "fmov.s @%0+, fr8\n\t"
66 "fmov.s @%0+, fr9\n\t"
67 "fmov.s @%0+, fr10\n\t"
68 "fmov.s @%0+, fr11\n\t"
69 "fmov.s @%0+, fr12\n\t"
70 "fmov.s @%0+, fr13\n\t"
71 "fmov.s @%0+, fr14\n\t"
72 "fmov.s @%0+, fr15\n\t"
73 "lds.l @%0+, fpscr\n\t"
74 "lds.l @%0+, fpul\n\t"
76 : "0" (tsk
->thread
.xstate
), "r" (FPSCR_RCHG
)
82 * Emulate arithmetic ops on denormalized number for some FPU insns.
85 /* denormalized float * float */
86 static int denormal_mulf(int hx
, int hy
)
89 unsigned long long m
, n
;
94 if (iy
< 0x00800000 || ix
== 0)
95 return ((hx
^ hy
) & 0x80000000);
97 exp
= (iy
& 0x7f800000) >> 23;
99 iy
= (iy
& 0x007fffff) | 0x00800000;
100 m
= (unsigned long long)ix
* iy
;
103 while (n
) { n
>>= 1; w
++; }
107 ix
= ((int) (m
>> (w
- 23)) & 0x007fffff) | (exp
<< 23);
108 else if (exp
+ 22 >= 0)
109 ix
= (int) (m
>> (w
- 22 - exp
)) & 0x007fffff;
113 ix
|= (hx
^ hy
) & 0x80000000;
117 /* denormalized double * double */
118 static void mult64(unsigned long long x
, unsigned long long y
,
119 unsigned long long *highp
, unsigned long long *lowp
)
121 unsigned long long sub0
, sub1
, sub2
, sub3
;
122 unsigned long long high
, low
;
124 sub0
= (x
>> 32) * (unsigned long) (y
>> 32);
125 sub1
= (x
& 0xffffffffLL
) * (unsigned long) (y
>> 32);
126 sub2
= (x
>> 32) * (unsigned long) (y
& 0xffffffffLL
);
127 sub3
= (x
& 0xffffffffLL
) * (unsigned long) (y
& 0xffffffffLL
);
130 sub3
+= (sub1
<< 32);
134 sub3
+= (sub2
<< 32);
138 high
+= (sub1
>> 32) + (sub2
>> 32);
144 static inline long long rshift64(unsigned long long mh
,
145 unsigned long long ml
, int n
)
148 return mh
>> (n
- 64);
149 return (mh
<< (64 - n
)) | (ml
>> n
);
152 static long long denormal_muld(long long hx
, long long hy
)
154 unsigned long long ix
, iy
;
155 unsigned long long mh
, ml
, nh
, nl
;
158 ix
= hx
& 0x7fffffffffffffffLL
;
159 iy
= hy
& 0x7fffffffffffffffLL
;
160 if (iy
< 0x0010000000000000LL
|| ix
== 0)
161 return ((hx
^ hy
) & 0x8000000000000000LL
);
163 exp
= (iy
& 0x7ff0000000000000LL
) >> 52;
164 ix
&= 0x000fffffffffffffLL
;
165 iy
= (iy
& 0x000fffffffffffffLL
) | 0x0010000000000000LL
;
166 mult64(ix
, iy
, &mh
, &ml
);
171 while (nh
) { nh
>>= 1; w
++;}
174 while (nl
) { nl
>>= 1; w
++;}
176 exp
+= w
- 1022 - 52 * 2;
178 ix
= (rshift64(mh
, ml
, w
- 52) & 0x000fffffffffffffLL
)
179 | ((long long)exp
<< 52);
180 else if (exp
+ 51 >= 0)
181 ix
= rshift64(mh
, ml
, w
- 51 - exp
) & 0x000fffffffffffffLL
;
185 ix
|= (hx
^ hy
) & 0x8000000000000000LL
;
189 /* ix - iy where iy: denormal and ix, iy >= 0 */
190 static int denormal_subf1(unsigned int ix
, unsigned int iy
)
198 exp
= (ix
& 0x7f800000) >> 23;
205 frac
= (ix
& 0x007fffff) | 0x00800000;
207 while (frac
< 0x00800000) {
213 return (exp
<< 23) | (frac
& 0x007fffff);
216 /* ix + iy where iy: denormal and ix, iy >= 0 */
217 static int denormal_addf1(unsigned int ix
, unsigned int iy
)
225 exp
= (ix
& 0x7f800000) >> 23;
232 frac
= (ix
& 0x007fffff) | 0x00800000;
234 if (frac
>= 0x01000000) {
239 return (exp
<< 23) | (frac
& 0x007fffff);
242 static int denormal_addf(int hx
, int hy
)
247 if ((hx
^ hy
) & 0x80000000) {
248 sign
= hx
& 0x80000000;
249 ix
= hx
& 0x7fffffff;
250 iy
= hy
& 0x7fffffff;
251 if (iy
< 0x00800000) {
252 ix
= denormal_subf1(ix
, iy
);
258 ix
= denormal_subf1(iy
, ix
);
262 sign
= hx
& 0x80000000;
263 ix
= hx
& 0x7fffffff;
264 iy
= hy
& 0x7fffffff;
266 ix
= denormal_addf1(ix
, iy
);
268 ix
= denormal_addf1(iy
, ix
);
274 /* ix - iy where iy: denormal and ix, iy >= 0 */
275 static long long denormal_subd1(unsigned long long ix
, unsigned long long iy
)
280 if (ix
< 0x0010000000000000LL
)
283 exp
= (ix
& 0x7ff0000000000000LL
) >> 52;
290 frac
= (ix
& 0x000fffffffffffffLL
) | 0x0010000000000000LL
;
292 while (frac
< 0x0010000000000000LL
) {
298 return ((long long)exp
<< 52) | (frac
& 0x000fffffffffffffLL
);
301 /* ix + iy where iy: denormal and ix, iy >= 0 */
302 static long long denormal_addd1(unsigned long long ix
, unsigned long long iy
)
307 if (ix
< 0x0010000000000000LL
)
310 exp
= (ix
& 0x7ff0000000000000LL
) >> 52;
317 frac
= (ix
& 0x000fffffffffffffLL
) | 0x0010000000000000LL
;
319 if (frac
>= 0x0020000000000000LL
) {
324 return (exp
<< 52) | (frac
& 0x000fffffffffffffLL
);
327 static long long denormal_addd(long long hx
, long long hy
)
329 unsigned long long ix
, iy
;
332 if ((hx
^ hy
) & 0x8000000000000000LL
) {
333 sign
= hx
& 0x8000000000000000LL
;
334 ix
= hx
& 0x7fffffffffffffffLL
;
335 iy
= hy
& 0x7fffffffffffffffLL
;
336 if (iy
< 0x0010000000000000LL
) {
337 ix
= denormal_subd1(ix
, iy
);
340 sign
^= 0x8000000000000000LL
;
343 ix
= denormal_subd1(iy
, ix
);
344 sign
^= 0x8000000000000000LL
;
347 sign
= hx
& 0x8000000000000000LL
;
348 ix
= hx
& 0x7fffffffffffffffLL
;
349 iy
= hy
& 0x7fffffffffffffffLL
;
350 if (iy
< 0x0010000000000000LL
)
351 ix
= denormal_addd1(ix
, iy
);
353 ix
= denormal_addd1(iy
, ix
);
360 * denormal_to_double - Given denormalized float number,
363 * @fpu: Pointer to sh_fpu_hard structure
364 * @n: Index to FP register
367 denormal_to_double (struct sh_fpu_hard_struct
*fpu
, int n
)
369 unsigned long du
, dl
;
370 unsigned long x
= fpu
->fpul
;
371 int exp
= 1023 - 126;
373 if (x
!= 0 && (x
& 0x7f800000) == 0) {
374 du
= (x
& 0x80000000);
375 while ((x
& 0x00800000) == 0) {
380 du
|= (exp
<< 20) | (x
>> 3);
383 fpu
->fp_regs
[n
] = du
;
384 fpu
->fp_regs
[n
+1] = dl
;
389 * ieee_fpe_handler - Handle denormalized number exception
391 * @regs: Pointer to register structure
393 * Returns 1 when it's handled (should not cause exception).
396 ieee_fpe_handler (struct pt_regs
*regs
)
398 unsigned short insn
= *(unsigned short *) regs
->pc
;
399 unsigned short finsn
;
400 unsigned long nextpc
;
408 (nib
[0] == 0x4 && nib
[2] == 0x0 && nib
[3] == 0xb)) /* bsr & jsr */
409 regs
->pr
= regs
->pc
+ 4;
410 if (nib
[0] == 0xa || nib
[0] == 0xb) { /* bra & bsr */
411 nextpc
= regs
->pc
+ 4 + ((short) ((insn
& 0xfff) << 4) >> 3);
412 finsn
= *(unsigned short *) (regs
->pc
+ 2);
413 } else if (nib
[0] == 0x8 && nib
[1] == 0xd) { /* bt/s */
415 nextpc
= regs
->pc
+ 4 + ((char) (insn
& 0xff) << 1);
417 nextpc
= regs
->pc
+ 4;
418 finsn
= *(unsigned short *) (regs
->pc
+ 2);
419 } else if (nib
[0] == 0x8 && nib
[1] == 0xf) { /* bf/s */
421 nextpc
= regs
->pc
+ 4;
423 nextpc
= regs
->pc
+ 4 + ((char) (insn
& 0xff) << 1);
424 finsn
= *(unsigned short *) (regs
->pc
+ 2);
425 } else if (nib
[0] == 0x4 && nib
[3] == 0xb &&
426 (nib
[2] == 0x0 || nib
[2] == 0x2)) { /* jmp & jsr */
427 nextpc
= regs
->regs
[nib
[1]];
428 finsn
= *(unsigned short *) (regs
->pc
+ 2);
429 } else if (nib
[0] == 0x0 && nib
[3] == 0x3 &&
430 (nib
[2] == 0x0 || nib
[2] == 0x2)) { /* braf & bsrf */
431 nextpc
= regs
->pc
+ 4 + regs
->regs
[nib
[1]];
432 finsn
= *(unsigned short *) (regs
->pc
+ 2);
433 } else if (insn
== 0x000b) { /* rts */
435 finsn
= *(unsigned short *) (regs
->pc
+ 2);
437 nextpc
= regs
->pc
+ 2;
441 #define FPSCR_FPU_ERROR (1 << 17)
443 if ((finsn
& 0xf1ff) == 0xf0ad) { /* fcnvsd */
444 struct task_struct
*tsk
= current
;
446 if ((tsk
->thread
.xstate
->hardfpu
.fpscr
& FPSCR_FPU_ERROR
)) {
448 denormal_to_double (&tsk
->thread
.xstate
->hardfpu
,
455 } else if ((finsn
& 0xf00f) == 0xf002) { /* fmul */
456 struct task_struct
*tsk
= current
;
461 n
= (finsn
>> 8) & 0xf;
462 m
= (finsn
>> 4) & 0xf;
463 hx
= tsk
->thread
.xstate
->hardfpu
.fp_regs
[n
];
464 hy
= tsk
->thread
.xstate
->hardfpu
.fp_regs
[m
];
465 fpscr
= tsk
->thread
.xstate
->hardfpu
.fpscr
;
466 prec
= fpscr
& (1 << 19);
468 if ((fpscr
& FPSCR_FPU_ERROR
)
469 && (prec
&& ((hx
& 0x7fffffff) < 0x00100000
470 || (hy
& 0x7fffffff) < 0x00100000))) {
473 /* FPU error because of denormal */
474 llx
= ((long long) hx
<< 32)
475 | tsk
->thread
.xstate
->hardfpu
.fp_regs
[n
+1];
476 lly
= ((long long) hy
<< 32)
477 | tsk
->thread
.xstate
->hardfpu
.fp_regs
[m
+1];
478 if ((hx
& 0x7fffffff) >= 0x00100000)
479 llx
= denormal_muld(lly
, llx
);
481 llx
= denormal_muld(llx
, lly
);
482 tsk
->thread
.xstate
->hardfpu
.fp_regs
[n
] = llx
>> 32;
483 tsk
->thread
.xstate
->hardfpu
.fp_regs
[n
+1] = llx
& 0xffffffff;
484 } else if ((fpscr
& FPSCR_FPU_ERROR
)
485 && (!prec
&& ((hx
& 0x7fffffff) < 0x00800000
486 || (hy
& 0x7fffffff) < 0x00800000))) {
487 /* FPU error because of denormal */
488 if ((hx
& 0x7fffffff) >= 0x00800000)
489 hx
= denormal_mulf(hy
, hx
);
491 hx
= denormal_mulf(hx
, hy
);
492 tsk
->thread
.xstate
->hardfpu
.fp_regs
[n
] = hx
;
498 } else if ((finsn
& 0xf00e) == 0xf000) { /* fadd, fsub */
499 struct task_struct
*tsk
= current
;
504 n
= (finsn
>> 8) & 0xf;
505 m
= (finsn
>> 4) & 0xf;
506 hx
= tsk
->thread
.xstate
->hardfpu
.fp_regs
[n
];
507 hy
= tsk
->thread
.xstate
->hardfpu
.fp_regs
[m
];
508 fpscr
= tsk
->thread
.xstate
->hardfpu
.fpscr
;
509 prec
= fpscr
& (1 << 19);
511 if ((fpscr
& FPSCR_FPU_ERROR
)
512 && (prec
&& ((hx
& 0x7fffffff) < 0x00100000
513 || (hy
& 0x7fffffff) < 0x00100000))) {
516 /* FPU error because of denormal */
517 llx
= ((long long) hx
<< 32)
518 | tsk
->thread
.xstate
->hardfpu
.fp_regs
[n
+1];
519 lly
= ((long long) hy
<< 32)
520 | tsk
->thread
.xstate
->hardfpu
.fp_regs
[m
+1];
521 if ((finsn
& 0xf00f) == 0xf000)
522 llx
= denormal_addd(llx
, lly
);
524 llx
= denormal_addd(llx
, lly
^ (1LL << 63));
525 tsk
->thread
.xstate
->hardfpu
.fp_regs
[n
] = llx
>> 32;
526 tsk
->thread
.xstate
->hardfpu
.fp_regs
[n
+1] = llx
& 0xffffffff;
527 } else if ((fpscr
& FPSCR_FPU_ERROR
)
528 && (!prec
&& ((hx
& 0x7fffffff) < 0x00800000
529 || (hy
& 0x7fffffff) < 0x00800000))) {
530 /* FPU error because of denormal */
531 if ((finsn
& 0xf00f) == 0xf000)
532 hx
= denormal_addf(hx
, hy
);
534 hx
= denormal_addf(hx
, hy
^ 0x80000000);
535 tsk
->thread
.xstate
->hardfpu
.fp_regs
[n
] = hx
;
546 BUILD_TRAP_HANDLER(fpu_error
)
548 struct task_struct
*tsk
= current
;
551 __unlazy_fpu(tsk
, regs
);
552 if (ieee_fpe_handler(regs
)) {
553 tsk
->thread
.xstate
->hardfpu
.fpscr
&=
554 ~(FPSCR_CAUSE_MASK
| FPSCR_FLAG_MASK
);
557 task_thread_info(tsk
)->status
|= TS_USEDFPU
;
561 force_sig(SIGFPE
, tsk
);