fpu.c 14 KB

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