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.
29 void save_fpu(struct task_struct *tsk)
34 asm volatile("sts.l fpul, @-%0\n\t"
35 "sts.l fpscr, @-%0\n\t"
36 "fmov.s fr15, @-%0\n\t"
37 "fmov.s fr14, @-%0\n\t"
38 "fmov.s fr13, @-%0\n\t"
39 "fmov.s fr12, @-%0\n\t"
40 "fmov.s fr11, @-%0\n\t"
41 "fmov.s fr10, @-%0\n\t"
42 "fmov.s fr9, @-%0\n\t"
43 "fmov.s fr8, @-%0\n\t"
44 "fmov.s fr7, @-%0\n\t"
45 "fmov.s fr6, @-%0\n\t"
46 "fmov.s fr5, @-%0\n\t"
47 "fmov.s fr4, @-%0\n\t"
48 "fmov.s fr3, @-%0\n\t"
49 "fmov.s fr2, @-%0\n\t"
50 "fmov.s fr1, @-%0\n\t"
51 "fmov.s fr0, @-%0\n\t"
54 : "0" ((char *)(&tsk->thread.xstate->hardfpu.status)),
62 void restore_fpu(struct task_struct *tsk)
67 asm volatile("fmov.s @%0+, fr0\n\t"
68 "fmov.s @%0+, fr1\n\t"
69 "fmov.s @%0+, fr2\n\t"
70 "fmov.s @%0+, fr3\n\t"
71 "fmov.s @%0+, fr4\n\t"
72 "fmov.s @%0+, fr5\n\t"
73 "fmov.s @%0+, fr6\n\t"
74 "fmov.s @%0+, fr7\n\t"
75 "fmov.s @%0+, fr8\n\t"
76 "fmov.s @%0+, fr9\n\t"
77 "fmov.s @%0+, fr10\n\t"
78 "fmov.s @%0+, fr11\n\t"
79 "fmov.s @%0+, fr12\n\t"
80 "fmov.s @%0+, fr13\n\t"
81 "fmov.s @%0+, fr14\n\t"
82 "fmov.s @%0+, fr15\n\t"
83 "lds.l @%0+, fpscr\n\t"
84 "lds.l @%0+, fpul\n\t"
86 : "0" (tsk->thread.xstate), "r" (FPSCR_RCHG)
92 * Emulate arithmetic ops on denormalized number for some FPU insns.
95 /* denormalized float * float */
96 static int denormal_mulf(int hx, int hy)
99 unsigned long long m, n;
102 ix = hx & 0x7fffffff;
103 iy = hy & 0x7fffffff;
104 if (iy < 0x00800000 || ix == 0)
105 return ((hx ^ hy) & 0x80000000);
107 exp = (iy & 0x7f800000) >> 23;
109 iy = (iy & 0x007fffff) | 0x00800000;
110 m = (unsigned long long)ix * iy;
113 while (n) { n >>= 1; w++; }
115 /* FIXME: use guard bits */
118 ix = ((int) (m >> (w - 23)) & 0x007fffff) | (exp << 23);
119 else if (exp + 22 >= 0)
120 ix = (int) (m >> (w - 22 - exp)) & 0x007fffff;
124 ix |= (hx ^ hy) & 0x80000000;
128 /* denormalized double * double */
129 static void mult64(unsigned long long x, unsigned long long y,
130 unsigned long long *highp, unsigned long long *lowp)
132 unsigned long long sub0, sub1, sub2, sub3;
133 unsigned long long high, low;
135 sub0 = (x >> 32) * (unsigned long) (y >> 32);
136 sub1 = (x & 0xffffffffLL) * (unsigned long) (y >> 32);
137 sub2 = (x >> 32) * (unsigned long) (y & 0xffffffffLL);
138 sub3 = (x & 0xffffffffLL) * (unsigned long) (y & 0xffffffffLL);
141 sub3 += (sub1 << 32);
145 sub3 += (sub2 << 32);
149 high += (sub1 >> 32) + (sub2 >> 32);
155 static inline long long rshift64(unsigned long long mh,
156 unsigned long long ml, int n)
159 return mh >> (n - 64);
160 return (mh << (64 - n)) | (ml >> n);
163 static long long denormal_muld(long long hx, long long hy)
165 unsigned long long ix, iy;
166 unsigned long long mh, ml, nh, nl;
169 ix = hx & 0x7fffffffffffffffLL;
170 iy = hy & 0x7fffffffffffffffLL;
171 if (iy < 0x0010000000000000LL || ix == 0)
172 return ((hx ^ hy) & 0x8000000000000000LL);
174 exp = (iy & 0x7ff0000000000000LL) >> 52;
175 ix &= 0x000fffffffffffffLL;
176 iy = (iy & 0x000fffffffffffffLL) | 0x0010000000000000LL;
177 mult64(ix, iy, &mh, &ml);
182 while (nh) { nh >>= 1; w++;}
185 while (nl) { nl >>= 1; w++;}
187 /* FIXME: use guard bits */
188 exp += w - 1022 - 52 * 2;
190 ix = (rshift64(mh, ml, w - 52) & 0x000fffffffffffffLL)
191 | ((long long)exp << 52);
192 else if (exp + 51 >= 0)
193 ix = rshift64(mh, ml, w - 51 - exp) & 0x000fffffffffffffLL;
197 ix |= (hx ^ hy) & 0x8000000000000000LL;
201 /* ix - iy where iy: denormal and ix, iy >= 0 */
202 static int denormal_subf1(unsigned int ix, unsigned int iy)
210 exp = (ix & 0x7f800000) >> 23;
217 frac = (ix & 0x007fffff) | 0x00800000;
219 while (frac < 0x00800000) {
225 return (exp << 23) | (frac & 0x007fffff);
228 /* ix + iy where iy: denormal and ix, iy >= 0 */
229 static int denormal_addf1(unsigned int ix, unsigned int iy)
237 exp = (ix & 0x7f800000) >> 23;
244 frac = (ix & 0x007fffff) | 0x00800000;
246 if (frac >= 0x01000000) {
251 return (exp << 23) | (frac & 0x007fffff);
254 static int denormal_addf(int hx, int hy)
259 if ((hx ^ hy) & 0x80000000) {
260 sign = hx & 0x80000000;
261 ix = hx & 0x7fffffff;
262 iy = hy & 0x7fffffff;
263 if (iy < 0x00800000) {
264 ix = denormal_subf1(ix, iy);
270 ix = denormal_subf1(iy, ix);
274 sign = hx & 0x80000000;
275 ix = hx & 0x7fffffff;
276 iy = hy & 0x7fffffff;
278 ix = denormal_addf1(ix, iy);
280 ix = denormal_addf1(iy, ix);
286 /* ix - iy where iy: denormal and ix, iy >= 0 */
287 static long long denormal_subd1(unsigned long long ix, unsigned long long iy)
292 if (ix < 0x0010000000000000LL)
295 exp = (ix & 0x7ff0000000000000LL) >> 52;
302 frac = (ix & 0x000fffffffffffffLL) | 0x0010000000000000LL;
304 while (frac < 0x0010000000000000LL) {
310 return ((long long)exp << 52) | (frac & 0x000fffffffffffffLL);
313 /* ix + iy where iy: denormal and ix, iy >= 0 */
314 static long long denormal_addd1(unsigned long long ix, unsigned long long iy)
319 if (ix < 0x0010000000000000LL)
322 exp = (ix & 0x7ff0000000000000LL) >> 52;
329 frac = (ix & 0x000fffffffffffffLL) | 0x0010000000000000LL;
331 if (frac >= 0x0020000000000000LL) {
336 return (exp << 52) | (frac & 0x000fffffffffffffLL);
339 static long long denormal_addd(long long hx, long long hy)
341 unsigned long long ix, iy;
344 if ((hx ^ hy) & 0x8000000000000000LL) {
345 sign = hx & 0x8000000000000000LL;
346 ix = hx & 0x7fffffffffffffffLL;
347 iy = hy & 0x7fffffffffffffffLL;
348 if (iy < 0x0010000000000000LL) {
349 ix = denormal_subd1(ix, iy);
352 sign ^= 0x8000000000000000LL;
355 ix = denormal_subd1(iy, ix);
356 sign ^= 0x8000000000000000LL;
359 sign = hx & 0x8000000000000000LL;
360 ix = hx & 0x7fffffffffffffffLL;
361 iy = hy & 0x7fffffffffffffffLL;
362 if (iy < 0x0010000000000000LL)
363 ix = denormal_addd1(ix, iy);
365 ix = denormal_addd1(iy, ix);
372 * denormal_to_double - Given denormalized float number,
375 * @fpu: Pointer to sh_fpu_hard structure
376 * @n: Index to FP register
379 denormal_to_double (struct sh_fpu_hard_struct *fpu, int n)
381 unsigned long du, dl;
382 unsigned long x = fpu->fpul;
383 int exp = 1023 - 126;
385 if (x != 0 && (x & 0x7f800000) == 0) {
386 du = (x & 0x80000000);
387 while ((x & 0x00800000) == 0) {
392 du |= (exp << 20) | (x >> 3);
395 fpu->fp_regs[n] = du;
396 fpu->fp_regs[n+1] = dl;
401 * ieee_fpe_handler - Handle denormalized number exception
403 * @regs: Pointer to register structure
405 * Returns 1 when it's handled (should not cause exception).
408 ieee_fpe_handler (struct pt_regs *regs)
410 unsigned short insn = *(unsigned short *) regs->pc;
411 unsigned short finsn;
412 unsigned long nextpc;
420 (nib[0] == 0x4 && nib[2] == 0x0 && nib[3] == 0xb)) /* bsr & jsr */
421 regs->pr = regs->pc + 4;
422 if (nib[0] == 0xa || nib[0] == 0xb) { /* bra & bsr */
423 nextpc = regs->pc + 4 + ((short) ((insn & 0xfff) << 4) >> 3);
424 finsn = *(unsigned short *) (regs->pc + 2);
425 } else if (nib[0] == 0x8 && nib[1] == 0xd) { /* bt/s */
427 nextpc = regs->pc + 4 + ((char) (insn & 0xff) << 1);
429 nextpc = regs->pc + 4;
430 finsn = *(unsigned short *) (regs->pc + 2);
431 } else if (nib[0] == 0x8 && nib[1] == 0xf) { /* bf/s */
433 nextpc = regs->pc + 4;
435 nextpc = regs->pc + 4 + ((char) (insn & 0xff) << 1);
436 finsn = *(unsigned short *) (regs->pc + 2);
437 } else if (nib[0] == 0x4 && nib[3] == 0xb &&
438 (nib[2] == 0x0 || nib[2] == 0x2)) { /* jmp & jsr */
439 nextpc = regs->regs[nib[1]];
440 finsn = *(unsigned short *) (regs->pc + 2);
441 } else if (nib[0] == 0x0 && nib[3] == 0x3 &&
442 (nib[2] == 0x0 || nib[2] == 0x2)) { /* braf & bsrf */
443 nextpc = regs->pc + 4 + regs->regs[nib[1]];
444 finsn = *(unsigned short *) (regs->pc + 2);
445 } else if (insn == 0x000b) { /* rts */
447 finsn = *(unsigned short *) (regs->pc + 2);
449 nextpc = regs->pc + 2;
453 #define FPSCR_FPU_ERROR (1 << 17)
455 if ((finsn & 0xf1ff) == 0xf0ad) { /* fcnvsd */
456 struct task_struct *tsk = current;
458 if ((tsk->thread.xstate->hardfpu.fpscr & FPSCR_FPU_ERROR)) {
460 denormal_to_double (&tsk->thread.xstate->hardfpu,
467 } else if ((finsn & 0xf00f) == 0xf002) { /* fmul */
468 struct task_struct *tsk = current;
473 n = (finsn >> 8) & 0xf;
474 m = (finsn >> 4) & 0xf;
475 hx = tsk->thread.xstate->hardfpu.fp_regs[n];
476 hy = tsk->thread.xstate->hardfpu.fp_regs[m];
477 fpscr = tsk->thread.xstate->hardfpu.fpscr;
478 prec = fpscr & (1 << 19);
480 if ((fpscr & FPSCR_FPU_ERROR)
481 && (prec && ((hx & 0x7fffffff) < 0x00100000
482 || (hy & 0x7fffffff) < 0x00100000))) {
485 /* FPU error because of denormal */
486 llx = ((long long) hx << 32)
487 | tsk->thread.xstate->hardfpu.fp_regs[n+1];
488 lly = ((long long) hy << 32)
489 | tsk->thread.xstate->hardfpu.fp_regs[m+1];
490 if ((hx & 0x7fffffff) >= 0x00100000)
491 llx = denormal_muld(lly, llx);
493 llx = denormal_muld(llx, lly);
494 tsk->thread.xstate->hardfpu.fp_regs[n] = llx >> 32;
495 tsk->thread.xstate->hardfpu.fp_regs[n+1] = llx & 0xffffffff;
496 } else if ((fpscr & FPSCR_FPU_ERROR)
497 && (!prec && ((hx & 0x7fffffff) < 0x00800000
498 || (hy & 0x7fffffff) < 0x00800000))) {
499 /* FPU error because of denormal */
500 if ((hx & 0x7fffffff) >= 0x00800000)
501 hx = denormal_mulf(hy, hx);
503 hx = denormal_mulf(hx, hy);
504 tsk->thread.xstate->hardfpu.fp_regs[n] = hx;
510 } else if ((finsn & 0xf00e) == 0xf000) { /* fadd, fsub */
511 struct task_struct *tsk = current;
516 n = (finsn >> 8) & 0xf;
517 m = (finsn >> 4) & 0xf;
518 hx = tsk->thread.xstate->hardfpu.fp_regs[n];
519 hy = tsk->thread.xstate->hardfpu.fp_regs[m];
520 fpscr = tsk->thread.xstate->hardfpu.fpscr;
521 prec = fpscr & (1 << 19);
523 if ((fpscr & FPSCR_FPU_ERROR)
524 && (prec && ((hx & 0x7fffffff) < 0x00100000
525 || (hy & 0x7fffffff) < 0x00100000))) {
528 /* FPU error because of denormal */
529 llx = ((long long) hx << 32)
530 | tsk->thread.xstate->hardfpu.fp_regs[n+1];
531 lly = ((long long) hy << 32)
532 | tsk->thread.xstate->hardfpu.fp_regs[m+1];
533 if ((finsn & 0xf00f) == 0xf000)
534 llx = denormal_addd(llx, lly);
536 llx = denormal_addd(llx, lly ^ (1LL << 63));
537 tsk->thread.xstate->hardfpu.fp_regs[n] = llx >> 32;
538 tsk->thread.xstate->hardfpu.fp_regs[n+1] = llx & 0xffffffff;
539 } else if ((fpscr & FPSCR_FPU_ERROR)
540 && (!prec && ((hx & 0x7fffffff) < 0x00800000
541 || (hy & 0x7fffffff) < 0x00800000))) {
542 /* FPU error because of denormal */
543 if ((finsn & 0xf00f) == 0xf000)
544 hx = denormal_addf(hx, hy);
546 hx = denormal_addf(hx, hy ^ 0x80000000);
547 tsk->thread.xstate->hardfpu.fp_regs[n] = hx;
558 BUILD_TRAP_HANDLER(fpu_error)
560 struct task_struct *tsk = current;
563 __unlazy_fpu(tsk, regs);
564 if (ieee_fpe_handler(regs)) {
565 tsk->thread.xstate->hardfpu.fpscr &=
566 ~(FPSCR_CAUSE_MASK | FPSCR_FLAG_MASK);
569 task_thread_info(tsk)->status |= TS_USEDFPU;
573 force_sig(SIGFPE, tsk);