1 // SPDX-License-Identifier: GPL-2.0
3 * Save/restore floating point context for signal handlers.
5 * Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
7 * FIXME! These routines can be optimized in big endian case.
9 #include <linux/sched/signal.h>
10 #include <linux/signal.h>
11 #include <asm/processor.h>
14 #include <asm/traps.h>
16 /* The PR (precision) bit in the FP Status Register must be clear when
17 * an frchg instruction is executed, otherwise the instruction is undefined.
18 * Executing frchg with PR set causes a trap on some SH4 implementations.
21 #define FPSCR_RCHG 0x00000000
25 * Save FPU registers onto task structure.
27 void save_fpu(struct task_struct *tsk)
32 asm volatile("sts.l fpul, @-%0\n\t"
33 "sts.l fpscr, @-%0\n\t"
34 "fmov.s fr15, @-%0\n\t"
35 "fmov.s fr14, @-%0\n\t"
36 "fmov.s fr13, @-%0\n\t"
37 "fmov.s fr12, @-%0\n\t"
38 "fmov.s fr11, @-%0\n\t"
39 "fmov.s fr10, @-%0\n\t"
40 "fmov.s fr9, @-%0\n\t"
41 "fmov.s fr8, @-%0\n\t"
42 "fmov.s fr7, @-%0\n\t"
43 "fmov.s fr6, @-%0\n\t"
44 "fmov.s fr5, @-%0\n\t"
45 "fmov.s fr4, @-%0\n\t"
46 "fmov.s fr3, @-%0\n\t"
47 "fmov.s fr2, @-%0\n\t"
48 "fmov.s fr1, @-%0\n\t"
49 "fmov.s fr0, @-%0\n\t"
52 : "0" ((char *)(&tsk->thread.xstate->hardfpu.status)),
60 void restore_fpu(struct task_struct *tsk)
65 asm volatile("fmov.s @%0+, fr0\n\t"
66 "fmov.s @%0+, fr1\n\t"
67 "fmov.s @%0+, fr2\n\t"
68 "fmov.s @%0+, fr3\n\t"
69 "fmov.s @%0+, fr4\n\t"
70 "fmov.s @%0+, fr5\n\t"
71 "fmov.s @%0+, fr6\n\t"
72 "fmov.s @%0+, fr7\n\t"
73 "fmov.s @%0+, fr8\n\t"
74 "fmov.s @%0+, fr9\n\t"
75 "fmov.s @%0+, fr10\n\t"
76 "fmov.s @%0+, fr11\n\t"
77 "fmov.s @%0+, fr12\n\t"
78 "fmov.s @%0+, fr13\n\t"
79 "fmov.s @%0+, fr14\n\t"
80 "fmov.s @%0+, fr15\n\t"
81 "lds.l @%0+, fpscr\n\t"
82 "lds.l @%0+, fpul\n\t"
84 : "0" (tsk->thread.xstate), "r" (FPSCR_RCHG)
90 * Emulate arithmetic ops on denormalized number for some FPU insns.
93 /* denormalized float * float */
94 static int denormal_mulf(int hx, int hy)
97 unsigned long long m, n;
100 ix = hx & 0x7fffffff;
101 iy = hy & 0x7fffffff;
102 if (iy < 0x00800000 || ix == 0)
103 return ((hx ^ hy) & 0x80000000);
105 exp = (iy & 0x7f800000) >> 23;
107 iy = (iy & 0x007fffff) | 0x00800000;
108 m = (unsigned long long)ix * iy;
111 while (n) { n >>= 1; w++; }
113 /* FIXME: use guard bits */
116 ix = ((int) (m >> (w - 23)) & 0x007fffff) | (exp << 23);
117 else if (exp + 22 >= 0)
118 ix = (int) (m >> (w - 22 - exp)) & 0x007fffff;
122 ix |= (hx ^ hy) & 0x80000000;
126 /* denormalized double * double */
127 static void mult64(unsigned long long x, unsigned long long y,
128 unsigned long long *highp, unsigned long long *lowp)
130 unsigned long long sub0, sub1, sub2, sub3;
131 unsigned long long high, low;
133 sub0 = (x >> 32) * (unsigned long) (y >> 32);
134 sub1 = (x & 0xffffffffLL) * (unsigned long) (y >> 32);
135 sub2 = (x >> 32) * (unsigned long) (y & 0xffffffffLL);
136 sub3 = (x & 0xffffffffLL) * (unsigned long) (y & 0xffffffffLL);
139 sub3 += (sub1 << 32);
143 sub3 += (sub2 << 32);
147 high += (sub1 >> 32) + (sub2 >> 32);
153 static inline long long rshift64(unsigned long long mh,
154 unsigned long long ml, int n)
157 return mh >> (n - 64);
158 return (mh << (64 - n)) | (ml >> n);
161 static long long denormal_muld(long long hx, long long hy)
163 unsigned long long ix, iy;
164 unsigned long long mh, ml, nh, nl;
167 ix = hx & 0x7fffffffffffffffLL;
168 iy = hy & 0x7fffffffffffffffLL;
169 if (iy < 0x0010000000000000LL || ix == 0)
170 return ((hx ^ hy) & 0x8000000000000000LL);
172 exp = (iy & 0x7ff0000000000000LL) >> 52;
173 ix &= 0x000fffffffffffffLL;
174 iy = (iy & 0x000fffffffffffffLL) | 0x0010000000000000LL;
175 mult64(ix, iy, &mh, &ml);
180 while (nh) { nh >>= 1; w++;}
183 while (nl) { nl >>= 1; w++;}
185 /* FIXME: use guard bits */
186 exp += w - 1022 - 52 * 2;
188 ix = (rshift64(mh, ml, w - 52) & 0x000fffffffffffffLL)
189 | ((long long)exp << 52);
190 else if (exp + 51 >= 0)
191 ix = rshift64(mh, ml, w - 51 - exp) & 0x000fffffffffffffLL;
195 ix |= (hx ^ hy) & 0x8000000000000000LL;
199 /* ix - iy where iy: denormal and ix, iy >= 0 */
200 static int denormal_subf1(unsigned int ix, unsigned int iy)
208 exp = (ix & 0x7f800000) >> 23;
215 frac = (ix & 0x007fffff) | 0x00800000;
217 while (frac < 0x00800000) {
223 return (exp << 23) | (frac & 0x007fffff);
226 /* ix + iy where iy: denormal and ix, iy >= 0 */
227 static int denormal_addf1(unsigned int ix, unsigned int iy)
235 exp = (ix & 0x7f800000) >> 23;
242 frac = (ix & 0x007fffff) | 0x00800000;
244 if (frac >= 0x01000000) {
249 return (exp << 23) | (frac & 0x007fffff);
252 static int denormal_addf(int hx, int hy)
257 if ((hx ^ hy) & 0x80000000) {
258 sign = hx & 0x80000000;
259 ix = hx & 0x7fffffff;
260 iy = hy & 0x7fffffff;
261 if (iy < 0x00800000) {
262 ix = denormal_subf1(ix, iy);
268 ix = denormal_subf1(iy, ix);
272 sign = hx & 0x80000000;
273 ix = hx & 0x7fffffff;
274 iy = hy & 0x7fffffff;
276 ix = denormal_addf1(ix, iy);
278 ix = denormal_addf1(iy, ix);
284 /* ix - iy where iy: denormal and ix, iy >= 0 */
285 static long long denormal_subd1(unsigned long long ix, unsigned long long iy)
290 if (ix < 0x0010000000000000LL)
293 exp = (ix & 0x7ff0000000000000LL) >> 52;
300 frac = (ix & 0x000fffffffffffffLL) | 0x0010000000000000LL;
302 while (frac < 0x0010000000000000LL) {
308 return ((long long)exp << 52) | (frac & 0x000fffffffffffffLL);
311 /* ix + iy where iy: denormal and ix, iy >= 0 */
312 static long long denormal_addd1(unsigned long long ix, unsigned long long iy)
317 if (ix < 0x0010000000000000LL)
320 exp = (ix & 0x7ff0000000000000LL) >> 52;
327 frac = (ix & 0x000fffffffffffffLL) | 0x0010000000000000LL;
329 if (frac >= 0x0020000000000000LL) {
334 return (exp << 52) | (frac & 0x000fffffffffffffLL);
337 static long long denormal_addd(long long hx, long long hy)
339 unsigned long long ix, iy;
342 if ((hx ^ hy) & 0x8000000000000000LL) {
343 sign = hx & 0x8000000000000000LL;
344 ix = hx & 0x7fffffffffffffffLL;
345 iy = hy & 0x7fffffffffffffffLL;
346 if (iy < 0x0010000000000000LL) {
347 ix = denormal_subd1(ix, iy);
350 sign ^= 0x8000000000000000LL;
353 ix = denormal_subd1(iy, ix);
354 sign ^= 0x8000000000000000LL;
357 sign = hx & 0x8000000000000000LL;
358 ix = hx & 0x7fffffffffffffffLL;
359 iy = hy & 0x7fffffffffffffffLL;
360 if (iy < 0x0010000000000000LL)
361 ix = denormal_addd1(ix, iy);
363 ix = denormal_addd1(iy, ix);
370 * denormal_to_double - Given denormalized float number,
373 * @fpu: Pointer to sh_fpu_hard structure
374 * @n: Index to FP register
377 denormal_to_double (struct sh_fpu_hard_struct *fpu, int n)
379 unsigned long du, dl;
380 unsigned long x = fpu->fpul;
381 int exp = 1023 - 126;
383 if (x != 0 && (x & 0x7f800000) == 0) {
384 du = (x & 0x80000000);
385 while ((x & 0x00800000) == 0) {
390 du |= (exp << 20) | (x >> 3);
393 fpu->fp_regs[n] = du;
394 fpu->fp_regs[n+1] = dl;
399 * ieee_fpe_handler - Handle denormalized number exception
401 * @regs: Pointer to register structure
403 * Returns 1 when it's handled (should not cause exception).
406 ieee_fpe_handler (struct pt_regs *regs)
408 unsigned short insn = *(unsigned short *) regs->pc;
409 unsigned short finsn;
410 unsigned long nextpc;
418 (nib[0] == 0x4 && nib[2] == 0x0 && nib[3] == 0xb)) /* bsr & jsr */
419 regs->pr = regs->pc + 4;
420 if (nib[0] == 0xa || nib[0] == 0xb) { /* bra & bsr */
421 nextpc = regs->pc + 4 + ((short) ((insn & 0xfff) << 4) >> 3);
422 finsn = *(unsigned short *) (regs->pc + 2);
423 } else if (nib[0] == 0x8 && nib[1] == 0xd) { /* bt/s */
425 nextpc = regs->pc + 4 + ((char) (insn & 0xff) << 1);
427 nextpc = regs->pc + 4;
428 finsn = *(unsigned short *) (regs->pc + 2);
429 } else if (nib[0] == 0x8 && nib[1] == 0xf) { /* bf/s */
431 nextpc = regs->pc + 4;
433 nextpc = regs->pc + 4 + ((char) (insn & 0xff) << 1);
434 finsn = *(unsigned short *) (regs->pc + 2);
435 } else if (nib[0] == 0x4 && nib[3] == 0xb &&
436 (nib[2] == 0x0 || nib[2] == 0x2)) { /* jmp & jsr */
437 nextpc = regs->regs[nib[1]];
438 finsn = *(unsigned short *) (regs->pc + 2);
439 } else if (nib[0] == 0x0 && nib[3] == 0x3 &&
440 (nib[2] == 0x0 || nib[2] == 0x2)) { /* braf & bsrf */
441 nextpc = regs->pc + 4 + regs->regs[nib[1]];
442 finsn = *(unsigned short *) (regs->pc + 2);
443 } else if (insn == 0x000b) { /* rts */
445 finsn = *(unsigned short *) (regs->pc + 2);
447 nextpc = regs->pc + 2;
451 #define FPSCR_FPU_ERROR (1 << 17)
453 if ((finsn & 0xf1ff) == 0xf0ad) { /* fcnvsd */
454 struct task_struct *tsk = current;
456 if ((tsk->thread.xstate->hardfpu.fpscr & FPSCR_FPU_ERROR)) {
458 denormal_to_double (&tsk->thread.xstate->hardfpu,
465 } else if ((finsn & 0xf00f) == 0xf002) { /* fmul */
466 struct task_struct *tsk = current;
471 n = (finsn >> 8) & 0xf;
472 m = (finsn >> 4) & 0xf;
473 hx = tsk->thread.xstate->hardfpu.fp_regs[n];
474 hy = tsk->thread.xstate->hardfpu.fp_regs[m];
475 fpscr = tsk->thread.xstate->hardfpu.fpscr;
476 prec = fpscr & (1 << 19);
478 if ((fpscr & FPSCR_FPU_ERROR)
479 && (prec && ((hx & 0x7fffffff) < 0x00100000
480 || (hy & 0x7fffffff) < 0x00100000))) {
483 /* FPU error because of denormal */
484 llx = ((long long) hx << 32)
485 | tsk->thread.xstate->hardfpu.fp_regs[n+1];
486 lly = ((long long) hy << 32)
487 | tsk->thread.xstate->hardfpu.fp_regs[m+1];
488 if ((hx & 0x7fffffff) >= 0x00100000)
489 llx = denormal_muld(lly, llx);
491 llx = denormal_muld(llx, lly);
492 tsk->thread.xstate->hardfpu.fp_regs[n] = llx >> 32;
493 tsk->thread.xstate->hardfpu.fp_regs[n+1] = llx & 0xffffffff;
494 } else if ((fpscr & FPSCR_FPU_ERROR)
495 && (!prec && ((hx & 0x7fffffff) < 0x00800000
496 || (hy & 0x7fffffff) < 0x00800000))) {
497 /* FPU error because of denormal */
498 if ((hx & 0x7fffffff) >= 0x00800000)
499 hx = denormal_mulf(hy, hx);
501 hx = denormal_mulf(hx, hy);
502 tsk->thread.xstate->hardfpu.fp_regs[n] = hx;
508 } else if ((finsn & 0xf00e) == 0xf000) { /* fadd, fsub */
509 struct task_struct *tsk = current;
514 n = (finsn >> 8) & 0xf;
515 m = (finsn >> 4) & 0xf;
516 hx = tsk->thread.xstate->hardfpu.fp_regs[n];
517 hy = tsk->thread.xstate->hardfpu.fp_regs[m];
518 fpscr = tsk->thread.xstate->hardfpu.fpscr;
519 prec = fpscr & (1 << 19);
521 if ((fpscr & FPSCR_FPU_ERROR)
522 && (prec && ((hx & 0x7fffffff) < 0x00100000
523 || (hy & 0x7fffffff) < 0x00100000))) {
526 /* FPU error because of denormal */
527 llx = ((long long) hx << 32)
528 | tsk->thread.xstate->hardfpu.fp_regs[n+1];
529 lly = ((long long) hy << 32)
530 | tsk->thread.xstate->hardfpu.fp_regs[m+1];
531 if ((finsn & 0xf00f) == 0xf000)
532 llx = denormal_addd(llx, lly);
534 llx = denormal_addd(llx, lly ^ (1LL << 63));
535 tsk->thread.xstate->hardfpu.fp_regs[n] = llx >> 32;
536 tsk->thread.xstate->hardfpu.fp_regs[n+1] = llx & 0xffffffff;
537 } else if ((fpscr & FPSCR_FPU_ERROR)
538 && (!prec && ((hx & 0x7fffffff) < 0x00800000
539 || (hy & 0x7fffffff) < 0x00800000))) {
540 /* FPU error because of denormal */
541 if ((finsn & 0xf00f) == 0xf000)
542 hx = denormal_addf(hx, hy);
544 hx = denormal_addf(hx, hy ^ 0x80000000);
545 tsk->thread.xstate->hardfpu.fp_regs[n] = hx;
556 BUILD_TRAP_HANDLER(fpu_error)
558 struct task_struct *tsk = current;
561 __unlazy_fpu(tsk, regs);
562 if (ieee_fpe_handler(regs)) {
563 tsk->thread.xstate->hardfpu.fpscr &=
564 ~(FPSCR_CAUSE_MASK | FPSCR_FLAG_MASK);
567 task_thread_info(tsk)->status |= TS_USEDFPU;