fpu.c 15 KB

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