fpu.c 15 KB

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