fpu.c 15 KB

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