fpu.c 12 KB

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