fpu.c 14 KB

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