fpu.c 13 KB

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