fpu.c 13 KB

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