fpu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * Save/restore floating point context for signal handlers.
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
  9. * Copyright (C) 2006 ST Microelectronics Ltd. (denorm support)
  10. *
  11. * FIXME! These routines have not been tested for big endian case.
  12. */
  13. #include <linux/sched.h>
  14. #include <linux/signal.h>
  15. #include <linux/io.h>
  16. #include <cpu/fpu.h>
  17. #include <asm/processor.h>
  18. #include <asm/system.h>
  19. #include <asm/fpu.h>
  20. /* The PR (precision) bit in the FP Status Register must be clear when
  21. * an frchg instruction is executed, otherwise the instruction is undefined.
  22. * Executing frchg with PR set causes a trap on some SH4 implementations.
  23. */
  24. #define FPSCR_RCHG 0x00000000
  25. extern unsigned long long float64_div(unsigned long long a,
  26. unsigned long long b);
  27. extern unsigned long int float32_div(unsigned long int a, unsigned long int b);
  28. extern unsigned long long float64_mul(unsigned long long a,
  29. unsigned long long b);
  30. extern unsigned long int float32_mul(unsigned long int a, unsigned long int b);
  31. extern unsigned long long float64_add(unsigned long long a,
  32. unsigned long long b);
  33. extern unsigned long int float32_add(unsigned long int a, unsigned long int b);
  34. extern unsigned long long float64_sub(unsigned long long a,
  35. unsigned long long b);
  36. extern unsigned long int float32_sub(unsigned long int a, unsigned long int b);
  37. extern unsigned long int float64_to_float32(unsigned long long a);
  38. static unsigned int fpu_exception_flags;
  39. /*
  40. * Save FPU registers onto task structure.
  41. * Assume called with FPU enabled (SR.FD=0).
  42. */
  43. void save_fpu(struct task_struct *tsk, struct pt_regs *regs)
  44. {
  45. unsigned long dummy;
  46. clear_tsk_thread_flag(tsk, TIF_USEDFPU);
  47. enable_fpu();
  48. asm volatile ("sts.l fpul, @-%0\n\t"
  49. "sts.l fpscr, @-%0\n\t"
  50. "lds %2, fpscr\n\t"
  51. "frchg\n\t"
  52. "fmov.s fr15, @-%0\n\t"
  53. "fmov.s fr14, @-%0\n\t"
  54. "fmov.s fr13, @-%0\n\t"
  55. "fmov.s fr12, @-%0\n\t"
  56. "fmov.s fr11, @-%0\n\t"
  57. "fmov.s fr10, @-%0\n\t"
  58. "fmov.s fr9, @-%0\n\t"
  59. "fmov.s fr8, @-%0\n\t"
  60. "fmov.s fr7, @-%0\n\t"
  61. "fmov.s fr6, @-%0\n\t"
  62. "fmov.s fr5, @-%0\n\t"
  63. "fmov.s fr4, @-%0\n\t"
  64. "fmov.s fr3, @-%0\n\t"
  65. "fmov.s fr2, @-%0\n\t"
  66. "fmov.s fr1, @-%0\n\t"
  67. "fmov.s fr0, @-%0\n\t"
  68. "frchg\n\t"
  69. "fmov.s fr15, @-%0\n\t"
  70. "fmov.s fr14, @-%0\n\t"
  71. "fmov.s fr13, @-%0\n\t"
  72. "fmov.s fr12, @-%0\n\t"
  73. "fmov.s fr11, @-%0\n\t"
  74. "fmov.s fr10, @-%0\n\t"
  75. "fmov.s fr9, @-%0\n\t"
  76. "fmov.s fr8, @-%0\n\t"
  77. "fmov.s fr7, @-%0\n\t"
  78. "fmov.s fr6, @-%0\n\t"
  79. "fmov.s fr5, @-%0\n\t"
  80. "fmov.s fr4, @-%0\n\t"
  81. "fmov.s fr3, @-%0\n\t"
  82. "fmov.s fr2, @-%0\n\t"
  83. "fmov.s fr1, @-%0\n\t"
  84. "fmov.s fr0, @-%0\n\t"
  85. "lds %3, fpscr\n\t":"=r" (dummy)
  86. :"0"((char *)(&tsk->thread.fpu.hard.status)),
  87. "r"(FPSCR_RCHG), "r"(FPSCR_INIT)
  88. :"memory");
  89. disable_fpu();
  90. release_fpu(regs);
  91. }
  92. static void restore_fpu(struct task_struct *tsk)
  93. {
  94. unsigned long dummy;
  95. enable_fpu();
  96. asm volatile ("lds %2, fpscr\n\t"
  97. "fmov.s @%0+, fr0\n\t"
  98. "fmov.s @%0+, fr1\n\t"
  99. "fmov.s @%0+, fr2\n\t"
  100. "fmov.s @%0+, fr3\n\t"
  101. "fmov.s @%0+, fr4\n\t"
  102. "fmov.s @%0+, fr5\n\t"
  103. "fmov.s @%0+, fr6\n\t"
  104. "fmov.s @%0+, fr7\n\t"
  105. "fmov.s @%0+, fr8\n\t"
  106. "fmov.s @%0+, fr9\n\t"
  107. "fmov.s @%0+, fr10\n\t"
  108. "fmov.s @%0+, fr11\n\t"
  109. "fmov.s @%0+, fr12\n\t"
  110. "fmov.s @%0+, fr13\n\t"
  111. "fmov.s @%0+, fr14\n\t"
  112. "fmov.s @%0+, fr15\n\t"
  113. "frchg\n\t"
  114. "fmov.s @%0+, fr0\n\t"
  115. "fmov.s @%0+, fr1\n\t"
  116. "fmov.s @%0+, fr2\n\t"
  117. "fmov.s @%0+, fr3\n\t"
  118. "fmov.s @%0+, fr4\n\t"
  119. "fmov.s @%0+, fr5\n\t"
  120. "fmov.s @%0+, fr6\n\t"
  121. "fmov.s @%0+, fr7\n\t"
  122. "fmov.s @%0+, fr8\n\t"
  123. "fmov.s @%0+, fr9\n\t"
  124. "fmov.s @%0+, fr10\n\t"
  125. "fmov.s @%0+, fr11\n\t"
  126. "fmov.s @%0+, fr12\n\t"
  127. "fmov.s @%0+, fr13\n\t"
  128. "fmov.s @%0+, fr14\n\t"
  129. "fmov.s @%0+, fr15\n\t"
  130. "frchg\n\t"
  131. "lds.l @%0+, fpscr\n\t"
  132. "lds.l @%0+, fpul\n\t"
  133. :"=r" (dummy)
  134. :"0"(&tsk->thread.fpu), "r"(FPSCR_RCHG)
  135. :"memory");
  136. disable_fpu();
  137. }
  138. /*
  139. * Load the FPU with signalling NANS. This bit pattern we're using
  140. * has the property that no matter wether considered as single or as
  141. * double precision represents signaling NANS.
  142. */
  143. static void fpu_init(void)
  144. {
  145. enable_fpu();
  146. asm volatile ( "lds %0, fpul\n\t"
  147. "lds %1, fpscr\n\t"
  148. "fsts fpul, fr0\n\t"
  149. "fsts fpul, fr1\n\t"
  150. "fsts fpul, fr2\n\t"
  151. "fsts fpul, fr3\n\t"
  152. "fsts fpul, fr4\n\t"
  153. "fsts fpul, fr5\n\t"
  154. "fsts fpul, fr6\n\t"
  155. "fsts fpul, fr7\n\t"
  156. "fsts fpul, fr8\n\t"
  157. "fsts fpul, fr9\n\t"
  158. "fsts fpul, fr10\n\t"
  159. "fsts fpul, fr11\n\t"
  160. "fsts fpul, fr12\n\t"
  161. "fsts fpul, fr13\n\t"
  162. "fsts fpul, fr14\n\t"
  163. "fsts fpul, fr15\n\t"
  164. "frchg\n\t"
  165. "fsts fpul, fr0\n\t"
  166. "fsts fpul, fr1\n\t"
  167. "fsts fpul, fr2\n\t"
  168. "fsts fpul, fr3\n\t"
  169. "fsts fpul, fr4\n\t"
  170. "fsts fpul, fr5\n\t"
  171. "fsts fpul, fr6\n\t"
  172. "fsts fpul, fr7\n\t"
  173. "fsts fpul, fr8\n\t"
  174. "fsts fpul, fr9\n\t"
  175. "fsts fpul, fr10\n\t"
  176. "fsts fpul, fr11\n\t"
  177. "fsts fpul, fr12\n\t"
  178. "fsts fpul, fr13\n\t"
  179. "fsts fpul, fr14\n\t"
  180. "fsts fpul, fr15\n\t"
  181. "frchg\n\t"
  182. "lds %2, fpscr\n\t"
  183. : /* no output */
  184. :"r" (0), "r"(FPSCR_RCHG), "r"(FPSCR_INIT));
  185. disable_fpu();
  186. }
  187. /**
  188. * denormal_to_double - Given denormalized float number,
  189. * store double float
  190. *
  191. * @fpu: Pointer to sh_fpu_hard structure
  192. * @n: Index to FP register
  193. */
  194. static void denormal_to_double(struct sh_fpu_hard_struct *fpu, int n)
  195. {
  196. unsigned long du, dl;
  197. unsigned long x = fpu->fpul;
  198. int exp = 1023 - 126;
  199. if (x != 0 && (x & 0x7f800000) == 0) {
  200. du = (x & 0x80000000);
  201. while ((x & 0x00800000) == 0) {
  202. x <<= 1;
  203. exp--;
  204. }
  205. x &= 0x007fffff;
  206. du |= (exp << 20) | (x >> 3);
  207. dl = x << 29;
  208. fpu->fp_regs[n] = du;
  209. fpu->fp_regs[n + 1] = dl;
  210. }
  211. }
  212. /**
  213. * ieee_fpe_handler - Handle denormalized number exception
  214. *
  215. * @regs: Pointer to register structure
  216. *
  217. * Returns 1 when it's handled (should not cause exception).
  218. */
  219. static int ieee_fpe_handler(struct pt_regs *regs)
  220. {
  221. unsigned short insn = *(unsigned short *)regs->pc;
  222. unsigned short finsn;
  223. unsigned long nextpc;
  224. int nib[4] = {
  225. (insn >> 12) & 0xf,
  226. (insn >> 8) & 0xf,
  227. (insn >> 4) & 0xf,
  228. insn & 0xf
  229. };
  230. if (nib[0] == 0xb || (nib[0] == 0x4 && nib[2] == 0x0 && nib[3] == 0xb))
  231. regs->pr = regs->pc + 4; /* bsr & jsr */
  232. if (nib[0] == 0xa || nib[0] == 0xb) {
  233. /* bra & bsr */
  234. nextpc = regs->pc + 4 + ((short)((insn & 0xfff) << 4) >> 3);
  235. finsn = *(unsigned short *)(regs->pc + 2);
  236. } else if (nib[0] == 0x8 && nib[1] == 0xd) {
  237. /* bt/s */
  238. if (regs->sr & 1)
  239. nextpc = regs->pc + 4 + ((char)(insn & 0xff) << 1);
  240. else
  241. nextpc = regs->pc + 4;
  242. finsn = *(unsigned short *)(regs->pc + 2);
  243. } else if (nib[0] == 0x8 && nib[1] == 0xf) {
  244. /* bf/s */
  245. if (regs->sr & 1)
  246. nextpc = regs->pc + 4;
  247. else
  248. nextpc = regs->pc + 4 + ((char)(insn & 0xff) << 1);
  249. finsn = *(unsigned short *)(regs->pc + 2);
  250. } else if (nib[0] == 0x4 && nib[3] == 0xb &&
  251. (nib[2] == 0x0 || nib[2] == 0x2)) {
  252. /* jmp & jsr */
  253. nextpc = regs->regs[nib[1]];
  254. finsn = *(unsigned short *)(regs->pc + 2);
  255. } else if (nib[0] == 0x0 && nib[3] == 0x3 &&
  256. (nib[2] == 0x0 || nib[2] == 0x2)) {
  257. /* braf & bsrf */
  258. nextpc = regs->pc + 4 + regs->regs[nib[1]];
  259. finsn = *(unsigned short *)(regs->pc + 2);
  260. } else if (insn == 0x000b) {
  261. /* rts */
  262. nextpc = regs->pr;
  263. finsn = *(unsigned short *)(regs->pc + 2);
  264. } else {
  265. nextpc = regs->pc + instruction_size(insn);
  266. finsn = insn;
  267. }
  268. if ((finsn & 0xf1ff) == 0xf0ad) {
  269. /* fcnvsd */
  270. struct task_struct *tsk = current;
  271. save_fpu(tsk, regs);
  272. if ((tsk->thread.fpu.hard.fpscr & FPSCR_CAUSE_ERROR))
  273. /* FPU error */
  274. denormal_to_double(&tsk->thread.fpu.hard,
  275. (finsn >> 8) & 0xf);
  276. else
  277. return 0;
  278. regs->pc = nextpc;
  279. return 1;
  280. } else if ((finsn & 0xf00f) == 0xf002) {
  281. /* fmul */
  282. struct task_struct *tsk = current;
  283. int fpscr;
  284. int n, m, prec;
  285. unsigned int hx, hy;
  286. n = (finsn >> 8) & 0xf;
  287. m = (finsn >> 4) & 0xf;
  288. hx = tsk->thread.fpu.hard.fp_regs[n];
  289. hy = tsk->thread.fpu.hard.fp_regs[m];
  290. fpscr = tsk->thread.fpu.hard.fpscr;
  291. prec = fpscr & FPSCR_DBL_PRECISION;
  292. if ((fpscr & FPSCR_CAUSE_ERROR)
  293. && (prec && ((hx & 0x7fffffff) < 0x00100000
  294. || (hy & 0x7fffffff) < 0x00100000))) {
  295. long long llx, lly;
  296. /* FPU error because of denormal (doubles) */
  297. llx = ((long long)hx << 32)
  298. | tsk->thread.fpu.hard.fp_regs[n + 1];
  299. lly = ((long long)hy << 32)
  300. | tsk->thread.fpu.hard.fp_regs[m + 1];
  301. llx = float64_mul(llx, lly);
  302. tsk->thread.fpu.hard.fp_regs[n] = llx >> 32;
  303. tsk->thread.fpu.hard.fp_regs[n + 1] = llx & 0xffffffff;
  304. } else if ((fpscr & FPSCR_CAUSE_ERROR)
  305. && (!prec && ((hx & 0x7fffffff) < 0x00800000
  306. || (hy & 0x7fffffff) < 0x00800000))) {
  307. /* FPU error because of denormal (floats) */
  308. hx = float32_mul(hx, hy);
  309. tsk->thread.fpu.hard.fp_regs[n] = hx;
  310. } else
  311. return 0;
  312. regs->pc = nextpc;
  313. return 1;
  314. } else if ((finsn & 0xf00e) == 0xf000) {
  315. /* fadd, fsub */
  316. struct task_struct *tsk = current;
  317. int fpscr;
  318. int n, m, prec;
  319. unsigned int hx, hy;
  320. n = (finsn >> 8) & 0xf;
  321. m = (finsn >> 4) & 0xf;
  322. hx = tsk->thread.fpu.hard.fp_regs[n];
  323. hy = tsk->thread.fpu.hard.fp_regs[m];
  324. fpscr = tsk->thread.fpu.hard.fpscr;
  325. prec = fpscr & FPSCR_DBL_PRECISION;
  326. if ((fpscr & FPSCR_CAUSE_ERROR)
  327. && (prec && ((hx & 0x7fffffff) < 0x00100000
  328. || (hy & 0x7fffffff) < 0x00100000))) {
  329. long long llx, lly;
  330. /* FPU error because of denormal (doubles) */
  331. llx = ((long long)hx << 32)
  332. | tsk->thread.fpu.hard.fp_regs[n + 1];
  333. lly = ((long long)hy << 32)
  334. | tsk->thread.fpu.hard.fp_regs[m + 1];
  335. if ((finsn & 0xf00f) == 0xf000)
  336. llx = float64_add(llx, lly);
  337. else
  338. llx = float64_sub(llx, lly);
  339. tsk->thread.fpu.hard.fp_regs[n] = llx >> 32;
  340. tsk->thread.fpu.hard.fp_regs[n + 1] = llx & 0xffffffff;
  341. } else if ((fpscr & FPSCR_CAUSE_ERROR)
  342. && (!prec && ((hx & 0x7fffffff) < 0x00800000
  343. || (hy & 0x7fffffff) < 0x00800000))) {
  344. /* FPU error because of denormal (floats) */
  345. if ((finsn & 0xf00f) == 0xf000)
  346. hx = float32_add(hx, hy);
  347. else
  348. hx = float32_sub(hx, hy);
  349. tsk->thread.fpu.hard.fp_regs[n] = hx;
  350. } else
  351. return 0;
  352. regs->pc = nextpc;
  353. return 1;
  354. } else if ((finsn & 0xf003) == 0xf003) {
  355. /* fdiv */
  356. struct task_struct *tsk = current;
  357. int fpscr;
  358. int n, m, prec;
  359. unsigned int hx, hy;
  360. n = (finsn >> 8) & 0xf;
  361. m = (finsn >> 4) & 0xf;
  362. hx = tsk->thread.fpu.hard.fp_regs[n];
  363. hy = tsk->thread.fpu.hard.fp_regs[m];
  364. fpscr = tsk->thread.fpu.hard.fpscr;
  365. prec = fpscr & FPSCR_DBL_PRECISION;
  366. if ((fpscr & FPSCR_CAUSE_ERROR)
  367. && (prec && ((hx & 0x7fffffff) < 0x00100000
  368. || (hy & 0x7fffffff) < 0x00100000))) {
  369. long long llx, lly;
  370. /* FPU error because of denormal (doubles) */
  371. llx = ((long long)hx << 32)
  372. | tsk->thread.fpu.hard.fp_regs[n + 1];
  373. lly = ((long long)hy << 32)
  374. | tsk->thread.fpu.hard.fp_regs[m + 1];
  375. llx = float64_div(llx, lly);
  376. tsk->thread.fpu.hard.fp_regs[n] = llx >> 32;
  377. tsk->thread.fpu.hard.fp_regs[n + 1] = llx & 0xffffffff;
  378. } else if ((fpscr & FPSCR_CAUSE_ERROR)
  379. && (!prec && ((hx & 0x7fffffff) < 0x00800000
  380. || (hy & 0x7fffffff) < 0x00800000))) {
  381. /* FPU error because of denormal (floats) */
  382. hx = float32_div(hx, hy);
  383. tsk->thread.fpu.hard.fp_regs[n] = hx;
  384. } else
  385. return 0;
  386. regs->pc = nextpc;
  387. return 1;
  388. } else if ((finsn & 0xf0bd) == 0xf0bd) {
  389. /* fcnvds - double to single precision convert */
  390. struct task_struct *tsk = current;
  391. int m;
  392. unsigned int hx;
  393. m = (finsn >> 8) & 0x7;
  394. hx = tsk->thread.fpu.hard.fp_regs[m];
  395. if ((tsk->thread.fpu.hard.fpscr & FPSCR_CAUSE_ERROR)
  396. && ((hx & 0x7fffffff) < 0x00100000)) {
  397. /* subnormal double to float conversion */
  398. long long llx;
  399. llx = ((long long)tsk->thread.fpu.hard.fp_regs[m] << 32)
  400. | tsk->thread.fpu.hard.fp_regs[m + 1];
  401. tsk->thread.fpu.hard.fpul = float64_to_float32(llx);
  402. } else
  403. return 0;
  404. regs->pc = nextpc;
  405. return 1;
  406. }
  407. return 0;
  408. }
  409. void float_raise(unsigned int flags)
  410. {
  411. fpu_exception_flags |= flags;
  412. }
  413. int float_rounding_mode(void)
  414. {
  415. struct task_struct *tsk = current;
  416. int roundingMode = FPSCR_ROUNDING_MODE(tsk->thread.fpu.hard.fpscr);
  417. return roundingMode;
  418. }
  419. BUILD_TRAP_HANDLER(fpu_error)
  420. {
  421. struct task_struct *tsk = current;
  422. TRAP_HANDLER_DECL;
  423. save_fpu(tsk, regs);
  424. fpu_exception_flags = 0;
  425. if (ieee_fpe_handler(regs)) {
  426. tsk->thread.fpu.hard.fpscr &=
  427. ~(FPSCR_CAUSE_MASK | FPSCR_FLAG_MASK);
  428. tsk->thread.fpu.hard.fpscr |= fpu_exception_flags;
  429. /* Set the FPSCR flag as well as cause bits - simply
  430. * replicate the cause */
  431. tsk->thread.fpu.hard.fpscr |= (fpu_exception_flags >> 10);
  432. grab_fpu(regs);
  433. restore_fpu(tsk);
  434. set_tsk_thread_flag(tsk, TIF_USEDFPU);
  435. if ((((tsk->thread.fpu.hard.fpscr & FPSCR_ENABLE_MASK) >> 7) &
  436. (fpu_exception_flags >> 2)) == 0) {
  437. return;
  438. }
  439. }
  440. force_sig(SIGFPE, tsk);
  441. }
  442. BUILD_TRAP_HANDLER(fpu_state_restore)
  443. {
  444. struct task_struct *tsk = current;
  445. TRAP_HANDLER_DECL;
  446. grab_fpu(regs);
  447. if (!user_mode(regs)) {
  448. printk(KERN_ERR "BUG: FPU is used in kernel mode.\n");
  449. return;
  450. }
  451. if (used_math()) {
  452. /* Using the FPU again. */
  453. restore_fpu(tsk);
  454. } else {
  455. /* First time FPU user. */
  456. fpu_init();
  457. set_used_math();
  458. }
  459. set_tsk_thread_flag(tsk, TIF_USEDFPU);
  460. }