2 * Save/restore floating point context for signal handlers.
4 * Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
10 * FIXME! These routines can be optimized in big endian case.
12 #include <linux/sched.h>
13 #include <linux/signal.h>
14 #include <asm/processor.h>
18 /* The PR (precision) bit in the FP Status Register must be clear when
19 * an frchg instruction is executed, otherwise the instruction is undefined.
20 * Executing frchg with PR set causes a trap on some SH4 implementations.
23 #define FPSCR_RCHG 0x00000000
27 * Save FPU registers onto task structure.
28 * Assume called with FPU enabled (SR.FD=0).
31 save_fpu(struct task_struct
*tsk
, struct pt_regs
*regs
)
35 clear_tsk_thread_flag(tsk
, TIF_USEDFPU
);
37 asm volatile("sts.l fpul, @-%0\n\t"
38 "sts.l fpscr, @-%0\n\t"
39 "fmov.s fr15, @-%0\n\t"
40 "fmov.s fr14, @-%0\n\t"
41 "fmov.s fr13, @-%0\n\t"
42 "fmov.s fr12, @-%0\n\t"
43 "fmov.s fr11, @-%0\n\t"
44 "fmov.s fr10, @-%0\n\t"
45 "fmov.s fr9, @-%0\n\t"
46 "fmov.s fr8, @-%0\n\t"
47 "fmov.s fr7, @-%0\n\t"
48 "fmov.s fr6, @-%0\n\t"
49 "fmov.s fr5, @-%0\n\t"
50 "fmov.s fr4, @-%0\n\t"
51 "fmov.s fr3, @-%0\n\t"
52 "fmov.s fr2, @-%0\n\t"
53 "fmov.s fr1, @-%0\n\t"
54 "fmov.s fr0, @-%0\n\t"
57 : "0" ((char *)(&tsk
->thread
.fpu
.hard
.status
)),
67 restore_fpu(struct task_struct
*tsk
)
72 asm volatile("fmov.s @%0+, fr0\n\t"
73 "fmov.s @%0+, fr1\n\t"
74 "fmov.s @%0+, fr2\n\t"
75 "fmov.s @%0+, fr3\n\t"
76 "fmov.s @%0+, fr4\n\t"
77 "fmov.s @%0+, fr5\n\t"
78 "fmov.s @%0+, fr6\n\t"
79 "fmov.s @%0+, fr7\n\t"
80 "fmov.s @%0+, fr8\n\t"
81 "fmov.s @%0+, fr9\n\t"
82 "fmov.s @%0+, fr10\n\t"
83 "fmov.s @%0+, fr11\n\t"
84 "fmov.s @%0+, fr12\n\t"
85 "fmov.s @%0+, fr13\n\t"
86 "fmov.s @%0+, fr14\n\t"
87 "fmov.s @%0+, fr15\n\t"
88 "lds.l @%0+, fpscr\n\t"
89 "lds.l @%0+, fpul\n\t"
91 : "0" (&tsk
->thread
.fpu
), "r" (FPSCR_RCHG
)
97 * Load the FPU with signalling NANS. This bit pattern we're using
98 * has the property that no matter wether considered as single or as
99 * double precission represents signaling NANS.
106 asm volatile("lds %0, fpul\n\t"
117 "fsts fpul, fr10\n\t"
118 "fsts fpul, fr11\n\t"
119 "fsts fpul, fr12\n\t"
120 "fsts fpul, fr13\n\t"
121 "fsts fpul, fr14\n\t"
122 "fsts fpul, fr15\n\t"
125 : "r" (0), "r" (FPSCR_RCHG
), "r" (FPSCR_INIT
));
130 * Emulate arithmetic ops on denormalized number for some FPU insns.
133 /* denormalized float * float */
134 static int denormal_mulf(int hx
, int hy
)
137 unsigned long long m
, n
;
140 ix
= hx
& 0x7fffffff;
141 iy
= hy
& 0x7fffffff;
142 if (iy
< 0x00800000 || ix
== 0)
143 return ((hx
^ hy
) & 0x80000000);
145 exp
= (iy
& 0x7f800000) >> 23;
147 iy
= (iy
& 0x007fffff) | 0x00800000;
148 m
= (unsigned long long)ix
* iy
;
151 while (n
) { n
>>= 1; w
++; }
153 /* FIXME: use guard bits */
156 ix
= ((int) (m
>> (w
- 23)) & 0x007fffff) | (exp
<< 23);
157 else if (exp
+ 22 >= 0)
158 ix
= (int) (m
>> (w
- 22 - exp
)) & 0x007fffff;
162 ix
|= (hx
^ hy
) & 0x80000000;
166 /* denormalized double * double */
167 static void mult64(unsigned long long x
, unsigned long long y
,
168 unsigned long long *highp
, unsigned long long *lowp
)
170 unsigned long long sub0
, sub1
, sub2
, sub3
;
171 unsigned long long high
, low
;
173 sub0
= (x
>> 32) * (unsigned long) (y
>> 32);
174 sub1
= (x
& 0xffffffffLL
) * (unsigned long) (y
>> 32);
175 sub2
= (x
>> 32) * (unsigned long) (y
& 0xffffffffLL
);
176 sub3
= (x
& 0xffffffffLL
) * (unsigned long) (y
& 0xffffffffLL
);
179 sub3
+= (sub1
<< 32);
183 sub3
+= (sub2
<< 32);
187 high
+= (sub1
>> 32) + (sub2
>> 32);
193 static inline long long rshift64(unsigned long long mh
,
194 unsigned long long ml
, int n
)
197 return mh
>> (n
- 64);
198 return (mh
<< (64 - n
)) | (ml
>> n
);
201 static long long denormal_muld(long long hx
, long long hy
)
203 unsigned long long ix
, iy
;
204 unsigned long long mh
, ml
, nh
, nl
;
207 ix
= hx
& 0x7fffffffffffffffLL
;
208 iy
= hy
& 0x7fffffffffffffffLL
;
209 if (iy
< 0x0010000000000000LL
|| ix
== 0)
210 return ((hx
^ hy
) & 0x8000000000000000LL
);
212 exp
= (iy
& 0x7ff0000000000000LL
) >> 52;
213 ix
&= 0x000fffffffffffffLL
;
214 iy
= (iy
& 0x000fffffffffffffLL
) | 0x0010000000000000LL
;
215 mult64(ix
, iy
, &mh
, &ml
);
220 while (nh
) { nh
>>= 1; w
++;}
223 while (nl
) { nl
>>= 1; w
++;}
225 /* FIXME: use guard bits */
226 exp
+= w
- 1022 - 52 * 2;
228 ix
= (rshift64(mh
, ml
, w
- 52) & 0x000fffffffffffffLL
)
229 | ((long long)exp
<< 52);
230 else if (exp
+ 51 >= 0)
231 ix
= rshift64(mh
, ml
, w
- 51 - exp
) & 0x000fffffffffffffLL
;
235 ix
|= (hx
^ hy
) & 0x8000000000000000LL
;
239 /* ix - iy where iy: denormal and ix, iy >= 0 */
240 static int denormal_subf1(unsigned int ix
, unsigned int iy
)
248 exp
= (ix
& 0x7f800000) >> 23;
255 frac
= (ix
& 0x007fffff) | 0x00800000;
257 while (frac
< 0x00800000) {
263 return (exp
<< 23) | (frac
& 0x007fffff);
266 /* ix + iy where iy: denormal and ix, iy >= 0 */
267 static int denormal_addf1(unsigned int ix
, unsigned int iy
)
275 exp
= (ix
& 0x7f800000) >> 23;
282 frac
= (ix
& 0x007fffff) | 0x00800000;
284 if (frac
>= 0x01000000) {
289 return (exp
<< 23) | (frac
& 0x007fffff);
292 static int denormal_addf(int hx
, int hy
)
297 if ((hx
^ hy
) & 0x80000000) {
298 sign
= hx
& 0x80000000;
299 ix
= hx
& 0x7fffffff;
300 iy
= hy
& 0x7fffffff;
301 if (iy
< 0x00800000) {
302 ix
= denormal_subf1(ix
, iy
);
308 ix
= denormal_subf1(iy
, ix
);
312 sign
= hx
& 0x80000000;
313 ix
= hx
& 0x7fffffff;
314 iy
= hy
& 0x7fffffff;
316 ix
= denormal_addf1(ix
, iy
);
318 ix
= denormal_addf1(iy
, ix
);
324 /* ix - iy where iy: denormal and ix, iy >= 0 */
325 static long long denormal_subd1(unsigned long long ix
, unsigned long long iy
)
330 if (ix
< 0x0010000000000000LL
)
333 exp
= (ix
& 0x7ff0000000000000LL
) >> 52;
340 frac
= (ix
& 0x000fffffffffffffLL
) | 0x0010000000000000LL
;
342 while (frac
< 0x0010000000000000LL
) {
348 return ((long long)exp
<< 52) | (frac
& 0x000fffffffffffffLL
);
351 /* ix + iy where iy: denormal and ix, iy >= 0 */
352 static long long denormal_addd1(unsigned long long ix
, unsigned long long iy
)
357 if (ix
< 0x0010000000000000LL
)
360 exp
= (ix
& 0x7ff0000000000000LL
) >> 52;
367 frac
= (ix
& 0x000fffffffffffffLL
) | 0x0010000000000000LL
;
369 if (frac
>= 0x0020000000000000LL
) {
374 return (exp
<< 52) | (frac
& 0x000fffffffffffffLL
);
377 static long long denormal_addd(long long hx
, long long hy
)
379 unsigned long long ix
, iy
;
382 if ((hx
^ hy
) & 0x8000000000000000LL
) {
383 sign
= hx
& 0x8000000000000000LL
;
384 ix
= hx
& 0x7fffffffffffffffLL
;
385 iy
= hy
& 0x7fffffffffffffffLL
;
386 if (iy
< 0x0010000000000000LL
) {
387 ix
= denormal_subd1(ix
, iy
);
390 sign
^= 0x8000000000000000LL
;
393 ix
= denormal_subd1(iy
, ix
);
394 sign
^= 0x8000000000000000LL
;
397 sign
= hx
& 0x8000000000000000LL
;
398 ix
= hx
& 0x7fffffffffffffffLL
;
399 iy
= hy
& 0x7fffffffffffffffLL
;
400 if (iy
< 0x0010000000000000LL
)
401 ix
= denormal_addd1(ix
, iy
);
403 ix
= denormal_addd1(iy
, ix
);
410 * denormal_to_double - Given denormalized float number,
413 * @fpu: Pointer to sh_fpu_hard structure
414 * @n: Index to FP register
417 denormal_to_double (struct sh_fpu_hard_struct
*fpu
, int n
)
419 unsigned long du
, dl
;
420 unsigned long x
= fpu
->fpul
;
421 int exp
= 1023 - 126;
423 if (x
!= 0 && (x
& 0x7f800000) == 0) {
424 du
= (x
& 0x80000000);
425 while ((x
& 0x00800000) == 0) {
430 du
|= (exp
<< 20) | (x
>> 3);
433 fpu
->fp_regs
[n
] = du
;
434 fpu
->fp_regs
[n
+1] = dl
;
439 * ieee_fpe_handler - Handle denormalized number exception
441 * @regs: Pointer to register structure
443 * Returns 1 when it's handled (should not cause exception).
446 ieee_fpe_handler (struct pt_regs
*regs
)
448 unsigned short insn
= *(unsigned short *) regs
->pc
;
449 unsigned short finsn
;
450 unsigned long nextpc
;
458 (nib
[0] == 0x4 && nib
[2] == 0x0 && nib
[3] == 0xb)) /* bsr & jsr */
459 regs
->pr
= regs
->pc
+ 4;
460 if (nib
[0] == 0xa || nib
[0] == 0xb) { /* bra & bsr */
461 nextpc
= regs
->pc
+ 4 + ((short) ((insn
& 0xfff) << 4) >> 3);
462 finsn
= *(unsigned short *) (regs
->pc
+ 2);
463 } else if (nib
[0] == 0x8 && nib
[1] == 0xd) { /* bt/s */
465 nextpc
= regs
->pc
+ 4 + ((char) (insn
& 0xff) << 1);
467 nextpc
= regs
->pc
+ 4;
468 finsn
= *(unsigned short *) (regs
->pc
+ 2);
469 } else if (nib
[0] == 0x8 && nib
[1] == 0xf) { /* bf/s */
471 nextpc
= regs
->pc
+ 4;
473 nextpc
= regs
->pc
+ 4 + ((char) (insn
& 0xff) << 1);
474 finsn
= *(unsigned short *) (regs
->pc
+ 2);
475 } else if (nib
[0] == 0x4 && nib
[3] == 0xb &&
476 (nib
[2] == 0x0 || nib
[2] == 0x2)) { /* jmp & jsr */
477 nextpc
= regs
->regs
[nib
[1]];
478 finsn
= *(unsigned short *) (regs
->pc
+ 2);
479 } else if (nib
[0] == 0x0 && nib
[3] == 0x3 &&
480 (nib
[2] == 0x0 || nib
[2] == 0x2)) { /* braf & bsrf */
481 nextpc
= regs
->pc
+ 4 + regs
->regs
[nib
[1]];
482 finsn
= *(unsigned short *) (regs
->pc
+ 2);
483 } else if (insn
== 0x000b) { /* rts */
485 finsn
= *(unsigned short *) (regs
->pc
+ 2);
487 nextpc
= regs
->pc
+ 2;
491 #define FPSCR_FPU_ERROR (1 << 17)
493 if ((finsn
& 0xf1ff) == 0xf0ad) { /* fcnvsd */
494 struct task_struct
*tsk
= current
;
496 if ((tsk
->thread
.fpu
.hard
.fpscr
& FPSCR_FPU_ERROR
)) {
498 denormal_to_double (&tsk
->thread
.fpu
.hard
,
505 } else if ((finsn
& 0xf00f) == 0xf002) { /* fmul */
506 struct task_struct
*tsk
= current
;
511 n
= (finsn
>> 8) & 0xf;
512 m
= (finsn
>> 4) & 0xf;
513 hx
= tsk
->thread
.fpu
.hard
.fp_regs
[n
];
514 hy
= tsk
->thread
.fpu
.hard
.fp_regs
[m
];
515 fpscr
= tsk
->thread
.fpu
.hard
.fpscr
;
516 prec
= fpscr
& (1 << 19);
518 if ((fpscr
& FPSCR_FPU_ERROR
)
519 && (prec
&& ((hx
& 0x7fffffff) < 0x00100000
520 || (hy
& 0x7fffffff) < 0x00100000))) {
523 /* FPU error because of denormal */
524 llx
= ((long long) hx
<< 32)
525 | tsk
->thread
.fpu
.hard
.fp_regs
[n
+1];
526 lly
= ((long long) hy
<< 32)
527 | tsk
->thread
.fpu
.hard
.fp_regs
[m
+1];
528 if ((hx
& 0x7fffffff) >= 0x00100000)
529 llx
= denormal_muld(lly
, llx
);
531 llx
= denormal_muld(llx
, lly
);
532 tsk
->thread
.fpu
.hard
.fp_regs
[n
] = llx
>> 32;
533 tsk
->thread
.fpu
.hard
.fp_regs
[n
+1] = llx
& 0xffffffff;
534 } else if ((fpscr
& FPSCR_FPU_ERROR
)
535 && (!prec
&& ((hx
& 0x7fffffff) < 0x00800000
536 || (hy
& 0x7fffffff) < 0x00800000))) {
537 /* FPU error because of denormal */
538 if ((hx
& 0x7fffffff) >= 0x00800000)
539 hx
= denormal_mulf(hy
, hx
);
541 hx
= denormal_mulf(hx
, hy
);
542 tsk
->thread
.fpu
.hard
.fp_regs
[n
] = hx
;
548 } else if ((finsn
& 0xf00e) == 0xf000) { /* fadd, fsub */
549 struct task_struct
*tsk
= current
;
554 n
= (finsn
>> 8) & 0xf;
555 m
= (finsn
>> 4) & 0xf;
556 hx
= tsk
->thread
.fpu
.hard
.fp_regs
[n
];
557 hy
= tsk
->thread
.fpu
.hard
.fp_regs
[m
];
558 fpscr
= tsk
->thread
.fpu
.hard
.fpscr
;
559 prec
= fpscr
& (1 << 19);
561 if ((fpscr
& FPSCR_FPU_ERROR
)
562 && (prec
&& ((hx
& 0x7fffffff) < 0x00100000
563 || (hy
& 0x7fffffff) < 0x00100000))) {
566 /* FPU error because of denormal */
567 llx
= ((long long) hx
<< 32)
568 | tsk
->thread
.fpu
.hard
.fp_regs
[n
+1];
569 lly
= ((long long) hy
<< 32)
570 | tsk
->thread
.fpu
.hard
.fp_regs
[m
+1];
571 if ((finsn
& 0xf00f) == 0xf000)
572 llx
= denormal_addd(llx
, lly
);
574 llx
= denormal_addd(llx
, lly
^ (1LL << 63));
575 tsk
->thread
.fpu
.hard
.fp_regs
[n
] = llx
>> 32;
576 tsk
->thread
.fpu
.hard
.fp_regs
[n
+1] = llx
& 0xffffffff;
577 } else if ((fpscr
& FPSCR_FPU_ERROR
)
578 && (!prec
&& ((hx
& 0x7fffffff) < 0x00800000
579 || (hy
& 0x7fffffff) < 0x00800000))) {
580 /* FPU error because of denormal */
581 if ((finsn
& 0xf00f) == 0xf000)
582 hx
= denormal_addf(hx
, hy
);
584 hx
= denormal_addf(hx
, hy
^ 0x80000000);
585 tsk
->thread
.fpu
.hard
.fp_regs
[n
] = hx
;
596 BUILD_TRAP_HANDLER(fpu_error
)
598 struct task_struct
*tsk
= current
;
602 if (ieee_fpe_handler(regs
)) {
603 tsk
->thread
.fpu
.hard
.fpscr
&=
604 ~(FPSCR_CAUSE_MASK
| FPSCR_FLAG_MASK
);
607 set_tsk_thread_flag(tsk
, TIF_USEDFPU
);
611 force_sig(SIGFPE
, tsk
);
614 BUILD_TRAP_HANDLER(fpu_state_restore
)
616 struct task_struct
*tsk
= current
;
620 if (!user_mode(regs
)) {
621 printk(KERN_ERR
"BUG: FPU is used in kernel mode.\n");
626 /* Using the FPU again. */
629 /* First time FPU user. */
633 set_tsk_thread_flag(tsk
, TIF_USEDFPU
);