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>
17 #include <asm/traps.h>
19 /* The PR (precision) bit in the FP Status Register must be clear when
20 * an frchg instruction is executed, otherwise the instruction is undefined.
21 * Executing frchg with PR set causes a trap on some SH4 implementations.
24 #define FPSCR_RCHG 0x00000000
28 * Save FPU registers onto task structure.
30 void save_fpu(struct task_struct *tsk)
35 asm volatile("sts.l fpul, @-%0\n\t"
36 "sts.l fpscr, @-%0\n\t"
37 "fmov.s fr15, @-%0\n\t"
38 "fmov.s fr14, @-%0\n\t"
39 "fmov.s fr13, @-%0\n\t"
40 "fmov.s fr12, @-%0\n\t"
41 "fmov.s fr11, @-%0\n\t"
42 "fmov.s fr10, @-%0\n\t"
43 "fmov.s fr9, @-%0\n\t"
44 "fmov.s fr8, @-%0\n\t"
45 "fmov.s fr7, @-%0\n\t"
46 "fmov.s fr6, @-%0\n\t"
47 "fmov.s fr5, @-%0\n\t"
48 "fmov.s fr4, @-%0\n\t"
49 "fmov.s fr3, @-%0\n\t"
50 "fmov.s fr2, @-%0\n\t"
51 "fmov.s fr1, @-%0\n\t"
52 "fmov.s fr0, @-%0\n\t"
55 : "0" ((char *)(&tsk->thread.xstate->hardfpu.status)),
63 void restore_fpu(struct task_struct *tsk)
68 asm volatile("fmov.s @%0+, fr0\n\t"
69 "fmov.s @%0+, fr1\n\t"
70 "fmov.s @%0+, fr2\n\t"
71 "fmov.s @%0+, fr3\n\t"
72 "fmov.s @%0+, fr4\n\t"
73 "fmov.s @%0+, fr5\n\t"
74 "fmov.s @%0+, fr6\n\t"
75 "fmov.s @%0+, fr7\n\t"
76 "fmov.s @%0+, fr8\n\t"
77 "fmov.s @%0+, fr9\n\t"
78 "fmov.s @%0+, fr10\n\t"
79 "fmov.s @%0+, fr11\n\t"
80 "fmov.s @%0+, fr12\n\t"
81 "fmov.s @%0+, fr13\n\t"
82 "fmov.s @%0+, fr14\n\t"
83 "fmov.s @%0+, fr15\n\t"
84 "lds.l @%0+, fpscr\n\t"
85 "lds.l @%0+, fpul\n\t"
87 : "0" (tsk->thread.xstate), "r" (FPSCR_RCHG)
93 * Emulate arithmetic ops on denormalized number for some FPU insns.
96 /* denormalized float * float */
97 static int denormal_mulf(int hx, int hy)
100 unsigned long long m, n;
103 ix = hx & 0x7fffffff;
104 iy = hy & 0x7fffffff;
105 if (iy < 0x00800000 || ix == 0)
106 return ((hx ^ hy) & 0x80000000);
108 exp = (iy & 0x7f800000) >> 23;
110 iy = (iy & 0x007fffff) | 0x00800000;
111 m = (unsigned long long)ix * iy;
114 while (n) { n >>= 1; w++; }
116 /* FIXME: use guard bits */
119 ix = ((int) (m >> (w - 23)) & 0x007fffff) | (exp << 23);
120 else if (exp + 22 >= 0)
121 ix = (int) (m >> (w - 22 - exp)) & 0x007fffff;
125 ix |= (hx ^ hy) & 0x80000000;
129 /* denormalized double * double */
130 static void mult64(unsigned long long x, unsigned long long y,
131 unsigned long long *highp, unsigned long long *lowp)
133 unsigned long long sub0, sub1, sub2, sub3;
134 unsigned long long high, low;
136 sub0 = (x >> 32) * (unsigned long) (y >> 32);
137 sub1 = (x & 0xffffffffLL) * (unsigned long) (y >> 32);
138 sub2 = (x >> 32) * (unsigned long) (y & 0xffffffffLL);
139 sub3 = (x & 0xffffffffLL) * (unsigned long) (y & 0xffffffffLL);
142 sub3 += (sub1 << 32);
146 sub3 += (sub2 << 32);
150 high += (sub1 >> 32) + (sub2 >> 32);
156 static inline long long rshift64(unsigned long long mh,
157 unsigned long long ml, int n)
160 return mh >> (n - 64);
161 return (mh << (64 - n)) | (ml >> n);
164 static long long denormal_muld(long long hx, long long hy)
166 unsigned long long ix, iy;
167 unsigned long long mh, ml, nh, nl;
170 ix = hx & 0x7fffffffffffffffLL;
171 iy = hy & 0x7fffffffffffffffLL;
172 if (iy < 0x0010000000000000LL || ix == 0)
173 return ((hx ^ hy) & 0x8000000000000000LL);
175 exp = (iy & 0x7ff0000000000000LL) >> 52;
176 ix &= 0x000fffffffffffffLL;
177 iy = (iy & 0x000fffffffffffffLL) | 0x0010000000000000LL;
178 mult64(ix, iy, &mh, &ml);
183 while (nh) { nh >>= 1; w++;}
186 while (nl) { nl >>= 1; w++;}
188 /* FIXME: use guard bits */
189 exp += w - 1022 - 52 * 2;
191 ix = (rshift64(mh, ml, w - 52) & 0x000fffffffffffffLL)
192 | ((long long)exp << 52);
193 else if (exp + 51 >= 0)
194 ix = rshift64(mh, ml, w - 51 - exp) & 0x000fffffffffffffLL;
198 ix |= (hx ^ hy) & 0x8000000000000000LL;
202 /* ix - iy where iy: denormal and ix, iy >= 0 */
203 static int denormal_subf1(unsigned int ix, unsigned int iy)
211 exp = (ix & 0x7f800000) >> 23;
218 frac = (ix & 0x007fffff) | 0x00800000;
220 while (frac < 0x00800000) {
226 return (exp << 23) | (frac & 0x007fffff);
229 /* ix + iy where iy: denormal and ix, iy >= 0 */
230 static int denormal_addf1(unsigned int ix, unsigned int iy)
238 exp = (ix & 0x7f800000) >> 23;
245 frac = (ix & 0x007fffff) | 0x00800000;
247 if (frac >= 0x01000000) {
252 return (exp << 23) | (frac & 0x007fffff);
255 static int denormal_addf(int hx, int hy)
260 if ((hx ^ hy) & 0x80000000) {
261 sign = hx & 0x80000000;
262 ix = hx & 0x7fffffff;
263 iy = hy & 0x7fffffff;
264 if (iy < 0x00800000) {
265 ix = denormal_subf1(ix, iy);
271 ix = denormal_subf1(iy, ix);
275 sign = hx & 0x80000000;
276 ix = hx & 0x7fffffff;
277 iy = hy & 0x7fffffff;
279 ix = denormal_addf1(ix, iy);
281 ix = denormal_addf1(iy, ix);
287 /* ix - iy where iy: denormal and ix, iy >= 0 */
288 static long long denormal_subd1(unsigned long long ix, unsigned long long iy)
293 if (ix < 0x0010000000000000LL)
296 exp = (ix & 0x7ff0000000000000LL) >> 52;
303 frac = (ix & 0x000fffffffffffffLL) | 0x0010000000000000LL;
305 while (frac < 0x0010000000000000LL) {
311 return ((long long)exp << 52) | (frac & 0x000fffffffffffffLL);
314 /* ix + iy where iy: denormal and ix, iy >= 0 */
315 static long long denormal_addd1(unsigned long long ix, unsigned long long iy)
320 if (ix < 0x0010000000000000LL)
323 exp = (ix & 0x7ff0000000000000LL) >> 52;
330 frac = (ix & 0x000fffffffffffffLL) | 0x0010000000000000LL;
332 if (frac >= 0x0020000000000000LL) {
337 return (exp << 52) | (frac & 0x000fffffffffffffLL);
340 static long long denormal_addd(long long hx, long long hy)
342 unsigned long long ix, iy;
345 if ((hx ^ hy) & 0x8000000000000000LL) {
346 sign = hx & 0x8000000000000000LL;
347 ix = hx & 0x7fffffffffffffffLL;
348 iy = hy & 0x7fffffffffffffffLL;
349 if (iy < 0x0010000000000000LL) {
350 ix = denormal_subd1(ix, iy);
353 sign ^= 0x8000000000000000LL;
356 ix = denormal_subd1(iy, ix);
357 sign ^= 0x8000000000000000LL;
360 sign = hx & 0x8000000000000000LL;
361 ix = hx & 0x7fffffffffffffffLL;
362 iy = hy & 0x7fffffffffffffffLL;
363 if (iy < 0x0010000000000000LL)
364 ix = denormal_addd1(ix, iy);
366 ix = denormal_addd1(iy, ix);
373 * denormal_to_double - Given denormalized float number,
376 * @fpu: Pointer to sh_fpu_hard structure
377 * @n: Index to FP register
380 denormal_to_double (struct sh_fpu_hard_struct *fpu, int n)
382 unsigned long du, dl;
383 unsigned long x = fpu->fpul;
384 int exp = 1023 - 126;
386 if (x != 0 && (x & 0x7f800000) == 0) {
387 du = (x & 0x80000000);
388 while ((x & 0x00800000) == 0) {
393 du |= (exp << 20) | (x >> 3);
396 fpu->fp_regs[n] = du;
397 fpu->fp_regs[n+1] = dl;
402 * ieee_fpe_handler - Handle denormalized number exception
404 * @regs: Pointer to register structure
406 * Returns 1 when it's handled (should not cause exception).
409 ieee_fpe_handler (struct pt_regs *regs)
411 unsigned short insn = *(unsigned short *) regs->pc;
412 unsigned short finsn;
413 unsigned long nextpc;
421 (nib[0] == 0x4 && nib[2] == 0x0 && nib[3] == 0xb)) /* bsr & jsr */
422 regs->pr = regs->pc + 4;
423 if (nib[0] == 0xa || nib[0] == 0xb) { /* bra & bsr */
424 nextpc = regs->pc + 4 + ((short) ((insn & 0xfff) << 4) >> 3);
425 finsn = *(unsigned short *) (regs->pc + 2);
426 } else if (nib[0] == 0x8 && nib[1] == 0xd) { /* bt/s */
428 nextpc = regs->pc + 4 + ((char) (insn & 0xff) << 1);
430 nextpc = regs->pc + 4;
431 finsn = *(unsigned short *) (regs->pc + 2);
432 } else if (nib[0] == 0x8 && nib[1] == 0xf) { /* bf/s */
434 nextpc = regs->pc + 4;
436 nextpc = regs->pc + 4 + ((char) (insn & 0xff) << 1);
437 finsn = *(unsigned short *) (regs->pc + 2);
438 } else if (nib[0] == 0x4 && nib[3] == 0xb &&
439 (nib[2] == 0x0 || nib[2] == 0x2)) { /* jmp & jsr */
440 nextpc = regs->regs[nib[1]];
441 finsn = *(unsigned short *) (regs->pc + 2);
442 } else if (nib[0] == 0x0 && nib[3] == 0x3 &&
443 (nib[2] == 0x0 || nib[2] == 0x2)) { /* braf & bsrf */
444 nextpc = regs->pc + 4 + regs->regs[nib[1]];
445 finsn = *(unsigned short *) (regs->pc + 2);
446 } else if (insn == 0x000b) { /* rts */
448 finsn = *(unsigned short *) (regs->pc + 2);
450 nextpc = regs->pc + 2;
454 #define FPSCR_FPU_ERROR (1 << 17)
456 if ((finsn & 0xf1ff) == 0xf0ad) { /* fcnvsd */
457 struct task_struct *tsk = current;
459 if ((tsk->thread.xstate->hardfpu.fpscr & FPSCR_FPU_ERROR)) {
461 denormal_to_double (&tsk->thread.xstate->hardfpu,
468 } else if ((finsn & 0xf00f) == 0xf002) { /* fmul */
469 struct task_struct *tsk = current;
474 n = (finsn >> 8) & 0xf;
475 m = (finsn >> 4) & 0xf;
476 hx = tsk->thread.xstate->hardfpu.fp_regs[n];
477 hy = tsk->thread.xstate->hardfpu.fp_regs[m];
478 fpscr = tsk->thread.xstate->hardfpu.fpscr;
479 prec = fpscr & (1 << 19);
481 if ((fpscr & FPSCR_FPU_ERROR)
482 && (prec && ((hx & 0x7fffffff) < 0x00100000
483 || (hy & 0x7fffffff) < 0x00100000))) {
486 /* FPU error because of denormal */
487 llx = ((long long) hx << 32)
488 | tsk->thread.xstate->hardfpu.fp_regs[n+1];
489 lly = ((long long) hy << 32)
490 | tsk->thread.xstate->hardfpu.fp_regs[m+1];
491 if ((hx & 0x7fffffff) >= 0x00100000)
492 llx = denormal_muld(lly, llx);
494 llx = denormal_muld(llx, lly);
495 tsk->thread.xstate->hardfpu.fp_regs[n] = llx >> 32;
496 tsk->thread.xstate->hardfpu.fp_regs[n+1] = llx & 0xffffffff;
497 } else if ((fpscr & FPSCR_FPU_ERROR)
498 && (!prec && ((hx & 0x7fffffff) < 0x00800000
499 || (hy & 0x7fffffff) < 0x00800000))) {
500 /* FPU error because of denormal */
501 if ((hx & 0x7fffffff) >= 0x00800000)
502 hx = denormal_mulf(hy, hx);
504 hx = denormal_mulf(hx, hy);
505 tsk->thread.xstate->hardfpu.fp_regs[n] = hx;
511 } else if ((finsn & 0xf00e) == 0xf000) { /* fadd, fsub */
512 struct task_struct *tsk = current;
517 n = (finsn >> 8) & 0xf;
518 m = (finsn >> 4) & 0xf;
519 hx = tsk->thread.xstate->hardfpu.fp_regs[n];
520 hy = tsk->thread.xstate->hardfpu.fp_regs[m];
521 fpscr = tsk->thread.xstate->hardfpu.fpscr;
522 prec = fpscr & (1 << 19);
524 if ((fpscr & FPSCR_FPU_ERROR)
525 && (prec && ((hx & 0x7fffffff) < 0x00100000
526 || (hy & 0x7fffffff) < 0x00100000))) {
529 /* FPU error because of denormal */
530 llx = ((long long) hx << 32)
531 | tsk->thread.xstate->hardfpu.fp_regs[n+1];
532 lly = ((long long) hy << 32)
533 | tsk->thread.xstate->hardfpu.fp_regs[m+1];
534 if ((finsn & 0xf00f) == 0xf000)
535 llx = denormal_addd(llx, lly);
537 llx = denormal_addd(llx, lly ^ (1LL << 63));
538 tsk->thread.xstate->hardfpu.fp_regs[n] = llx >> 32;
539 tsk->thread.xstate->hardfpu.fp_regs[n+1] = llx & 0xffffffff;
540 } else if ((fpscr & FPSCR_FPU_ERROR)
541 && (!prec && ((hx & 0x7fffffff) < 0x00800000
542 || (hy & 0x7fffffff) < 0x00800000))) {
543 /* FPU error because of denormal */
544 if ((finsn & 0xf00f) == 0xf000)
545 hx = denormal_addf(hx, hy);
547 hx = denormal_addf(hx, hy ^ 0x80000000);
548 tsk->thread.xstate->hardfpu.fp_regs[n] = hx;
559 BUILD_TRAP_HANDLER(fpu_error)
561 struct task_struct *tsk = current;
564 __unlazy_fpu(tsk, regs);
565 if (ieee_fpe_handler(regs)) {
566 tsk->thread.xstate->hardfpu.fpscr &=
567 ~(FPSCR_CAUSE_MASK | FPSCR_FLAG_MASK);
570 task_thread_info(tsk)->status |= TS_USEDFPU;
574 force_sig(SIGFPE, tsk);