fpu.c 12 KB

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