fpu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Save/restore floating point context for signal handlers.
  4. *
  5. * Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
  6. *
  7. * FIXME! These routines can be optimized in big endian case.
  8. */
  9. #include <linux/sched/signal.h>
  10. #include <linux/signal.h>
  11. #include <asm/processor.h>
  12. #include <asm/io.h>
  13. #include <asm/fpu.h>
  14. #include <asm/traps.h>
  15. /* The PR (precision) bit in the FP Status Register must be clear when
  16. * an frchg instruction is executed, otherwise the instruction is undefined.
  17. * Executing frchg with PR set causes a trap on some SH4 implementations.
  18. */
  19. #define FPSCR_RCHG 0x00000000
  20. /*
  21. * Save FPU registers onto task structure.
  22. */
  23. void save_fpu(struct task_struct *tsk)
  24. {
  25. unsigned long dummy;
  26. enable_fpu();
  27. asm volatile("sts.l fpul, @-%0\n\t"
  28. "sts.l fpscr, @-%0\n\t"
  29. "fmov.s fr15, @-%0\n\t"
  30. "fmov.s fr14, @-%0\n\t"
  31. "fmov.s fr13, @-%0\n\t"
  32. "fmov.s fr12, @-%0\n\t"
  33. "fmov.s fr11, @-%0\n\t"
  34. "fmov.s fr10, @-%0\n\t"
  35. "fmov.s fr9, @-%0\n\t"
  36. "fmov.s fr8, @-%0\n\t"
  37. "fmov.s fr7, @-%0\n\t"
  38. "fmov.s fr6, @-%0\n\t"
  39. "fmov.s fr5, @-%0\n\t"
  40. "fmov.s fr4, @-%0\n\t"
  41. "fmov.s fr3, @-%0\n\t"
  42. "fmov.s fr2, @-%0\n\t"
  43. "fmov.s fr1, @-%0\n\t"
  44. "fmov.s fr0, @-%0\n\t"
  45. "lds %3, fpscr\n\t"
  46. : "=r" (dummy)
  47. : "0" ((char *)(&tsk->thread.xstate->hardfpu.status)),
  48. "r" (FPSCR_RCHG),
  49. "r" (FPSCR_INIT)
  50. : "memory");
  51. disable_fpu();
  52. }
  53. void restore_fpu(struct task_struct *tsk)
  54. {
  55. unsigned long dummy;
  56. enable_fpu();
  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"
  75. : "=r" (dummy)
  76. : "0" (tsk->thread.xstate), "r" (FPSCR_RCHG)
  77. : "memory");
  78. disable_fpu();
  79. }
  80. /*
  81. * Emulate arithmetic ops on denormalized number for some FPU insns.
  82. */
  83. /* denormalized float * float */
  84. static int denormal_mulf(int hx, int hy)
  85. {
  86. unsigned int ix, iy;
  87. unsigned long long m, n;
  88. int exp, w;
  89. ix = hx & 0x7fffffff;
  90. iy = hy & 0x7fffffff;
  91. if (iy < 0x00800000 || ix == 0)
  92. return ((hx ^ hy) & 0x80000000);
  93. exp = (iy & 0x7f800000) >> 23;
  94. ix &= 0x007fffff;
  95. iy = (iy & 0x007fffff) | 0x00800000;
  96. m = (unsigned long long)ix * iy;
  97. n = m;
  98. w = -1;
  99. while (n) { n >>= 1; w++; }
  100. /* FIXME: use guard bits */
  101. exp += w - 126 - 46;
  102. if (exp > 0)
  103. ix = ((int) (m >> (w - 23)) & 0x007fffff) | (exp << 23);
  104. else if (exp + 22 >= 0)
  105. ix = (int) (m >> (w - 22 - exp)) & 0x007fffff;
  106. else
  107. ix = 0;
  108. ix |= (hx ^ hy) & 0x80000000;
  109. return ix;
  110. }
  111. /* denormalized double * double */
  112. static void mult64(unsigned long long x, unsigned long long y,
  113. unsigned long long *highp, unsigned long long *lowp)
  114. {
  115. unsigned long long sub0, sub1, sub2, sub3;
  116. unsigned long long high, low;
  117. sub0 = (x >> 32) * (unsigned long) (y >> 32);
  118. sub1 = (x & 0xffffffffLL) * (unsigned long) (y >> 32);
  119. sub2 = (x >> 32) * (unsigned long) (y & 0xffffffffLL);
  120. sub3 = (x & 0xffffffffLL) * (unsigned long) (y & 0xffffffffLL);
  121. low = sub3;
  122. high = 0LL;
  123. sub3 += (sub1 << 32);
  124. if (low > sub3)
  125. high++;
  126. low = sub3;
  127. sub3 += (sub2 << 32);
  128. if (low > sub3)
  129. high++;
  130. low = sub3;
  131. high += (sub1 >> 32) + (sub2 >> 32);
  132. high += sub0;
  133. *lowp = low;
  134. *highp = high;
  135. }
  136. static inline long long rshift64(unsigned long long mh,
  137. unsigned long long ml, int n)
  138. {
  139. if (n >= 64)
  140. return mh >> (n - 64);
  141. return (mh << (64 - n)) | (ml >> n);
  142. }
  143. static long long denormal_muld(long long hx, long long hy)
  144. {
  145. unsigned long long ix, iy;
  146. unsigned long long mh, ml, nh, nl;
  147. int exp, w;
  148. ix = hx & 0x7fffffffffffffffLL;
  149. iy = hy & 0x7fffffffffffffffLL;
  150. if (iy < 0x0010000000000000LL || ix == 0)
  151. return ((hx ^ hy) & 0x8000000000000000LL);
  152. exp = (iy & 0x7ff0000000000000LL) >> 52;
  153. ix &= 0x000fffffffffffffLL;
  154. iy = (iy & 0x000fffffffffffffLL) | 0x0010000000000000LL;
  155. mult64(ix, iy, &mh, &ml);
  156. nh = mh;
  157. nl = ml;
  158. w = -1;
  159. if (nh) {
  160. while (nh) { nh >>= 1; w++;}
  161. w += 64;
  162. } else
  163. while (nl) { nl >>= 1; w++;}
  164. /* FIXME: use guard bits */
  165. exp += w - 1022 - 52 * 2;
  166. if (exp > 0)
  167. ix = (rshift64(mh, ml, w - 52) & 0x000fffffffffffffLL)
  168. | ((long long)exp << 52);
  169. else if (exp + 51 >= 0)
  170. ix = rshift64(mh, ml, w - 51 - exp) & 0x000fffffffffffffLL;
  171. else
  172. ix = 0;
  173. ix |= (hx ^ hy) & 0x8000000000000000LL;
  174. return ix;
  175. }
  176. /* ix - iy where iy: denormal and ix, iy >= 0 */
  177. static int denormal_subf1(unsigned int ix, unsigned int iy)
  178. {
  179. int frac;
  180. int exp;
  181. if (ix < 0x00800000)
  182. return ix - iy;
  183. exp = (ix & 0x7f800000) >> 23;
  184. if (exp - 1 > 31)
  185. return ix;
  186. iy >>= exp - 1;
  187. if (iy == 0)
  188. return ix;
  189. frac = (ix & 0x007fffff) | 0x00800000;
  190. frac -= iy;
  191. while (frac < 0x00800000) {
  192. if (--exp == 0)
  193. return frac;
  194. frac <<= 1;
  195. }
  196. return (exp << 23) | (frac & 0x007fffff);
  197. }
  198. /* ix + iy where iy: denormal and ix, iy >= 0 */
  199. static int denormal_addf1(unsigned int ix, unsigned int iy)
  200. {
  201. int frac;
  202. int exp;
  203. if (ix < 0x00800000)
  204. return ix + iy;
  205. exp = (ix & 0x7f800000) >> 23;
  206. if (exp - 1 > 31)
  207. return ix;
  208. iy >>= exp - 1;
  209. if (iy == 0)
  210. return ix;
  211. frac = (ix & 0x007fffff) | 0x00800000;
  212. frac += iy;
  213. if (frac >= 0x01000000) {
  214. frac >>= 1;
  215. ++exp;
  216. }
  217. return (exp << 23) | (frac & 0x007fffff);
  218. }
  219. static int denormal_addf(int hx, int hy)
  220. {
  221. unsigned int ix, iy;
  222. int sign;
  223. if ((hx ^ hy) & 0x80000000) {
  224. sign = hx & 0x80000000;
  225. ix = hx & 0x7fffffff;
  226. iy = hy & 0x7fffffff;
  227. if (iy < 0x00800000) {
  228. ix = denormal_subf1(ix, iy);
  229. if ((int) ix < 0) {
  230. ix = -ix;
  231. sign ^= 0x80000000;
  232. }
  233. } else {
  234. ix = denormal_subf1(iy, ix);
  235. sign ^= 0x80000000;
  236. }
  237. } else {
  238. sign = hx & 0x80000000;
  239. ix = hx & 0x7fffffff;
  240. iy = hy & 0x7fffffff;
  241. if (iy < 0x00800000)
  242. ix = denormal_addf1(ix, iy);
  243. else
  244. ix = denormal_addf1(iy, ix);
  245. }
  246. return sign | ix;
  247. }
  248. /* ix - iy where iy: denormal and ix, iy >= 0 */
  249. static long long denormal_subd1(unsigned long long ix, unsigned long long iy)
  250. {
  251. long long frac;
  252. int exp;
  253. if (ix < 0x0010000000000000LL)
  254. return ix - iy;
  255. exp = (ix & 0x7ff0000000000000LL) >> 52;
  256. if (exp - 1 > 63)
  257. return ix;
  258. iy >>= exp - 1;
  259. if (iy == 0)
  260. return ix;
  261. frac = (ix & 0x000fffffffffffffLL) | 0x0010000000000000LL;
  262. frac -= iy;
  263. while (frac < 0x0010000000000000LL) {
  264. if (--exp == 0)
  265. return frac;
  266. frac <<= 1;
  267. }
  268. return ((long long)exp << 52) | (frac & 0x000fffffffffffffLL);
  269. }
  270. /* ix + iy where iy: denormal and ix, iy >= 0 */
  271. static long long denormal_addd1(unsigned long long ix, unsigned long long iy)
  272. {
  273. long long frac;
  274. long long exp;
  275. if (ix < 0x0010000000000000LL)
  276. return ix + iy;
  277. exp = (ix & 0x7ff0000000000000LL) >> 52;
  278. if (exp - 1 > 63)
  279. return ix;
  280. iy >>= exp - 1;
  281. if (iy == 0)
  282. return ix;
  283. frac = (ix & 0x000fffffffffffffLL) | 0x0010000000000000LL;
  284. frac += iy;
  285. if (frac >= 0x0020000000000000LL) {
  286. frac >>= 1;
  287. ++exp;
  288. }
  289. return (exp << 52) | (frac & 0x000fffffffffffffLL);
  290. }
  291. static long long denormal_addd(long long hx, long long hy)
  292. {
  293. unsigned long long ix, iy;
  294. long long sign;
  295. if ((hx ^ hy) & 0x8000000000000000LL) {
  296. sign = hx & 0x8000000000000000LL;
  297. ix = hx & 0x7fffffffffffffffLL;
  298. iy = hy & 0x7fffffffffffffffLL;
  299. if (iy < 0x0010000000000000LL) {
  300. ix = denormal_subd1(ix, iy);
  301. if ((int) ix < 0) {
  302. ix = -ix;
  303. sign ^= 0x8000000000000000LL;
  304. }
  305. } else {
  306. ix = denormal_subd1(iy, ix);
  307. sign ^= 0x8000000000000000LL;
  308. }
  309. } else {
  310. sign = hx & 0x8000000000000000LL;
  311. ix = hx & 0x7fffffffffffffffLL;
  312. iy = hy & 0x7fffffffffffffffLL;
  313. if (iy < 0x0010000000000000LL)
  314. ix = denormal_addd1(ix, iy);
  315. else
  316. ix = denormal_addd1(iy, ix);
  317. }
  318. return sign | ix;
  319. }
  320. /**
  321. * denormal_to_double - Given denormalized float number,
  322. * store double float
  323. *
  324. * @fpu: Pointer to sh_fpu_hard structure
  325. * @n: Index to FP register
  326. */
  327. static void
  328. denormal_to_double (struct sh_fpu_hard_struct *fpu, int n)
  329. {
  330. unsigned long du, dl;
  331. unsigned long x = fpu->fpul;
  332. int exp = 1023 - 126;
  333. if (x != 0 && (x & 0x7f800000) == 0) {
  334. du = (x & 0x80000000);
  335. while ((x & 0x00800000) == 0) {
  336. x <<= 1;
  337. exp--;
  338. }
  339. x &= 0x007fffff;
  340. du |= (exp << 20) | (x >> 3);
  341. dl = x << 29;
  342. fpu->fp_regs[n] = du;
  343. fpu->fp_regs[n+1] = dl;
  344. }
  345. }
  346. /**
  347. * ieee_fpe_handler - Handle denormalized number exception
  348. *
  349. * @regs: Pointer to register structure
  350. *
  351. * Returns 1 when it's handled (should not cause exception).
  352. */
  353. static int
  354. ieee_fpe_handler (struct pt_regs *regs)
  355. {
  356. unsigned short insn = *(unsigned short *) regs->pc;
  357. unsigned short finsn;
  358. unsigned long nextpc;
  359. int nib[4] = {
  360. (insn >> 12) & 0xf,
  361. (insn >> 8) & 0xf,
  362. (insn >> 4) & 0xf,
  363. insn & 0xf};
  364. if (nib[0] == 0xb ||
  365. (nib[0] == 0x4 && nib[2] == 0x0 && nib[3] == 0xb)) /* bsr & jsr */
  366. regs->pr = regs->pc + 4;
  367. if (nib[0] == 0xa || nib[0] == 0xb) { /* bra & bsr */
  368. nextpc = regs->pc + 4 + ((short) ((insn & 0xfff) << 4) >> 3);
  369. finsn = *(unsigned short *) (regs->pc + 2);
  370. } else if (nib[0] == 0x8 && nib[1] == 0xd) { /* bt/s */
  371. if (regs->sr & 1)
  372. nextpc = regs->pc + 4 + ((char) (insn & 0xff) << 1);
  373. else
  374. nextpc = regs->pc + 4;
  375. finsn = *(unsigned short *) (regs->pc + 2);
  376. } else if (nib[0] == 0x8 && nib[1] == 0xf) { /* bf/s */
  377. if (regs->sr & 1)
  378. nextpc = regs->pc + 4;
  379. else
  380. nextpc = regs->pc + 4 + ((char) (insn & 0xff) << 1);
  381. finsn = *(unsigned short *) (regs->pc + 2);
  382. } else if (nib[0] == 0x4 && nib[3] == 0xb &&
  383. (nib[2] == 0x0 || nib[2] == 0x2)) { /* jmp & jsr */
  384. nextpc = regs->regs[nib[1]];
  385. finsn = *(unsigned short *) (regs->pc + 2);
  386. } else if (nib[0] == 0x0 && nib[3] == 0x3 &&
  387. (nib[2] == 0x0 || nib[2] == 0x2)) { /* braf & bsrf */
  388. nextpc = regs->pc + 4 + regs->regs[nib[1]];
  389. finsn = *(unsigned short *) (regs->pc + 2);
  390. } else if (insn == 0x000b) { /* rts */
  391. nextpc = regs->pr;
  392. finsn = *(unsigned short *) (regs->pc + 2);
  393. } else {
  394. nextpc = regs->pc + 2;
  395. finsn = insn;
  396. }
  397. #define FPSCR_FPU_ERROR (1 << 17)
  398. if ((finsn & 0xf1ff) == 0xf0ad) { /* fcnvsd */
  399. struct task_struct *tsk = current;
  400. if ((tsk->thread.xstate->hardfpu.fpscr & FPSCR_FPU_ERROR)) {
  401. /* FPU error */
  402. denormal_to_double (&tsk->thread.xstate->hardfpu,
  403. (finsn >> 8) & 0xf);
  404. } else
  405. return 0;
  406. regs->pc = nextpc;
  407. return 1;
  408. } else if ((finsn & 0xf00f) == 0xf002) { /* fmul */
  409. struct task_struct *tsk = current;
  410. int fpscr;
  411. int n, m, prec;
  412. unsigned int hx, hy;
  413. n = (finsn >> 8) & 0xf;
  414. m = (finsn >> 4) & 0xf;
  415. hx = tsk->thread.xstate->hardfpu.fp_regs[n];
  416. hy = tsk->thread.xstate->hardfpu.fp_regs[m];
  417. fpscr = tsk->thread.xstate->hardfpu.fpscr;
  418. prec = fpscr & (1 << 19);
  419. if ((fpscr & FPSCR_FPU_ERROR)
  420. && (prec && ((hx & 0x7fffffff) < 0x00100000
  421. || (hy & 0x7fffffff) < 0x00100000))) {
  422. long long llx, lly;
  423. /* FPU error because of denormal */
  424. llx = ((long long) hx << 32)
  425. | tsk->thread.xstate->hardfpu.fp_regs[n+1];
  426. lly = ((long long) hy << 32)
  427. | tsk->thread.xstate->hardfpu.fp_regs[m+1];
  428. if ((hx & 0x7fffffff) >= 0x00100000)
  429. llx = denormal_muld(lly, llx);
  430. else
  431. llx = denormal_muld(llx, lly);
  432. tsk->thread.xstate->hardfpu.fp_regs[n] = llx >> 32;
  433. tsk->thread.xstate->hardfpu.fp_regs[n+1] = llx & 0xffffffff;
  434. } else if ((fpscr & FPSCR_FPU_ERROR)
  435. && (!prec && ((hx & 0x7fffffff) < 0x00800000
  436. || (hy & 0x7fffffff) < 0x00800000))) {
  437. /* FPU error because of denormal */
  438. if ((hx & 0x7fffffff) >= 0x00800000)
  439. hx = denormal_mulf(hy, hx);
  440. else
  441. hx = denormal_mulf(hx, hy);
  442. tsk->thread.xstate->hardfpu.fp_regs[n] = hx;
  443. } else
  444. return 0;
  445. regs->pc = nextpc;
  446. return 1;
  447. } else if ((finsn & 0xf00e) == 0xf000) { /* fadd, fsub */
  448. struct task_struct *tsk = current;
  449. int fpscr;
  450. int n, m, prec;
  451. unsigned int hx, hy;
  452. n = (finsn >> 8) & 0xf;
  453. m = (finsn >> 4) & 0xf;
  454. hx = tsk->thread.xstate->hardfpu.fp_regs[n];
  455. hy = tsk->thread.xstate->hardfpu.fp_regs[m];
  456. fpscr = tsk->thread.xstate->hardfpu.fpscr;
  457. prec = fpscr & (1 << 19);
  458. if ((fpscr & FPSCR_FPU_ERROR)
  459. && (prec && ((hx & 0x7fffffff) < 0x00100000
  460. || (hy & 0x7fffffff) < 0x00100000))) {
  461. long long llx, lly;
  462. /* FPU error because of denormal */
  463. llx = ((long long) hx << 32)
  464. | tsk->thread.xstate->hardfpu.fp_regs[n+1];
  465. lly = ((long long) hy << 32)
  466. | tsk->thread.xstate->hardfpu.fp_regs[m+1];
  467. if ((finsn & 0xf00f) == 0xf000)
  468. llx = denormal_addd(llx, lly);
  469. else
  470. llx = denormal_addd(llx, lly ^ (1LL << 63));
  471. tsk->thread.xstate->hardfpu.fp_regs[n] = llx >> 32;
  472. tsk->thread.xstate->hardfpu.fp_regs[n+1] = llx & 0xffffffff;
  473. } else if ((fpscr & FPSCR_FPU_ERROR)
  474. && (!prec && ((hx & 0x7fffffff) < 0x00800000
  475. || (hy & 0x7fffffff) < 0x00800000))) {
  476. /* FPU error because of denormal */
  477. if ((finsn & 0xf00f) == 0xf000)
  478. hx = denormal_addf(hx, hy);
  479. else
  480. hx = denormal_addf(hx, hy ^ 0x80000000);
  481. tsk->thread.xstate->hardfpu.fp_regs[n] = hx;
  482. } else
  483. return 0;
  484. regs->pc = nextpc;
  485. return 1;
  486. }
  487. return 0;
  488. }
  489. BUILD_TRAP_HANDLER(fpu_error)
  490. {
  491. struct task_struct *tsk = current;
  492. TRAP_HANDLER_DECL;
  493. __unlazy_fpu(tsk, regs);
  494. if (ieee_fpe_handler(regs)) {
  495. tsk->thread.xstate->hardfpu.fpscr &=
  496. ~(FPSCR_CAUSE_MASK | FPSCR_FLAG_MASK);
  497. grab_fpu(regs);
  498. restore_fpu(tsk);
  499. task_thread_info(tsk)->status |= TS_USEDFPU;
  500. return;
  501. }
  502. force_sig(SIGFPE);
  503. }