fpu.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* $Id: fpu.c,v 1.4 2004/01/13 05:52:11 kkojima Exp $
  2. *
  3. * linux/arch/sh/kernel/fpu.c
  4. *
  5. * Save/restore floating point context for signal handlers.
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. *
  11. * Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
  12. *
  13. * FIXME! These routines can be optimized in big endian case.
  14. */
  15. #include <linux/sched.h>
  16. #include <linux/signal.h>
  17. #include <asm/processor.h>
  18. #include <asm/io.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. /*
  25. * Save FPU registers onto task structure.
  26. * Assume called with FPU enabled (SR.FD=0).
  27. */
  28. void
  29. save_fpu(struct task_struct *tsk, struct pt_regs *regs)
  30. {
  31. unsigned long dummy;
  32. clear_tsk_thread_flag(tsk, TIF_USEDFPU);
  33. enable_fpu();
  34. asm volatile("sts.l fpul, @-%0\n\t"
  35. "sts.l fpscr, @-%0\n\t"
  36. "lds %2, fpscr\n\t"
  37. "frchg\n\t"
  38. "fmov.s fr15, @-%0\n\t"
  39. "fmov.s fr14, @-%0\n\t"
  40. "fmov.s fr13, @-%0\n\t"
  41. "fmov.s fr12, @-%0\n\t"
  42. "fmov.s fr11, @-%0\n\t"
  43. "fmov.s fr10, @-%0\n\t"
  44. "fmov.s fr9, @-%0\n\t"
  45. "fmov.s fr8, @-%0\n\t"
  46. "fmov.s fr7, @-%0\n\t"
  47. "fmov.s fr6, @-%0\n\t"
  48. "fmov.s fr5, @-%0\n\t"
  49. "fmov.s fr4, @-%0\n\t"
  50. "fmov.s fr3, @-%0\n\t"
  51. "fmov.s fr2, @-%0\n\t"
  52. "fmov.s fr1, @-%0\n\t"
  53. "fmov.s fr0, @-%0\n\t"
  54. "frchg\n\t"
  55. "fmov.s fr15, @-%0\n\t"
  56. "fmov.s fr14, @-%0\n\t"
  57. "fmov.s fr13, @-%0\n\t"
  58. "fmov.s fr12, @-%0\n\t"
  59. "fmov.s fr11, @-%0\n\t"
  60. "fmov.s fr10, @-%0\n\t"
  61. "fmov.s fr9, @-%0\n\t"
  62. "fmov.s fr8, @-%0\n\t"
  63. "fmov.s fr7, @-%0\n\t"
  64. "fmov.s fr6, @-%0\n\t"
  65. "fmov.s fr5, @-%0\n\t"
  66. "fmov.s fr4, @-%0\n\t"
  67. "fmov.s fr3, @-%0\n\t"
  68. "fmov.s fr2, @-%0\n\t"
  69. "fmov.s fr1, @-%0\n\t"
  70. "fmov.s fr0, @-%0\n\t"
  71. "lds %3, fpscr\n\t"
  72. : "=r" (dummy)
  73. : "0" ((char *)(&tsk->thread.fpu.hard.status)),
  74. "r" (FPSCR_RCHG),
  75. "r" (FPSCR_INIT)
  76. : "memory");
  77. disable_fpu();
  78. release_fpu(regs);
  79. }
  80. static void
  81. restore_fpu(struct task_struct *tsk)
  82. {
  83. unsigned long dummy;
  84. enable_fpu();
  85. asm volatile("lds %2, fpscr\n\t"
  86. "fmov.s @%0+, fr0\n\t"
  87. "fmov.s @%0+, fr1\n\t"
  88. "fmov.s @%0+, fr2\n\t"
  89. "fmov.s @%0+, fr3\n\t"
  90. "fmov.s @%0+, fr4\n\t"
  91. "fmov.s @%0+, fr5\n\t"
  92. "fmov.s @%0+, fr6\n\t"
  93. "fmov.s @%0+, fr7\n\t"
  94. "fmov.s @%0+, fr8\n\t"
  95. "fmov.s @%0+, fr9\n\t"
  96. "fmov.s @%0+, fr10\n\t"
  97. "fmov.s @%0+, fr11\n\t"
  98. "fmov.s @%0+, fr12\n\t"
  99. "fmov.s @%0+, fr13\n\t"
  100. "fmov.s @%0+, fr14\n\t"
  101. "fmov.s @%0+, fr15\n\t"
  102. "frchg\n\t"
  103. "fmov.s @%0+, fr0\n\t"
  104. "fmov.s @%0+, fr1\n\t"
  105. "fmov.s @%0+, fr2\n\t"
  106. "fmov.s @%0+, fr3\n\t"
  107. "fmov.s @%0+, fr4\n\t"
  108. "fmov.s @%0+, fr5\n\t"
  109. "fmov.s @%0+, fr6\n\t"
  110. "fmov.s @%0+, fr7\n\t"
  111. "fmov.s @%0+, fr8\n\t"
  112. "fmov.s @%0+, fr9\n\t"
  113. "fmov.s @%0+, fr10\n\t"
  114. "fmov.s @%0+, fr11\n\t"
  115. "fmov.s @%0+, fr12\n\t"
  116. "fmov.s @%0+, fr13\n\t"
  117. "fmov.s @%0+, fr14\n\t"
  118. "fmov.s @%0+, fr15\n\t"
  119. "frchg\n\t"
  120. "lds.l @%0+, fpscr\n\t"
  121. "lds.l @%0+, fpul\n\t"
  122. : "=r" (dummy)
  123. : "0" (&tsk->thread.fpu), "r" (FPSCR_RCHG)
  124. : "memory");
  125. disable_fpu();
  126. }
  127. /*
  128. * Load the FPU with signalling NANS. This bit pattern we're using
  129. * has the property that no matter wether considered as single or as
  130. * double precission represents signaling NANS.
  131. */
  132. static void
  133. fpu_init(void)
  134. {
  135. enable_fpu();
  136. asm volatile("lds %0, fpul\n\t"
  137. "lds %1, fpscr\n\t"
  138. "fsts fpul, fr0\n\t"
  139. "fsts fpul, fr1\n\t"
  140. "fsts fpul, fr2\n\t"
  141. "fsts fpul, fr3\n\t"
  142. "fsts fpul, fr4\n\t"
  143. "fsts fpul, fr5\n\t"
  144. "fsts fpul, fr6\n\t"
  145. "fsts fpul, fr7\n\t"
  146. "fsts fpul, fr8\n\t"
  147. "fsts fpul, fr9\n\t"
  148. "fsts fpul, fr10\n\t"
  149. "fsts fpul, fr11\n\t"
  150. "fsts fpul, fr12\n\t"
  151. "fsts fpul, fr13\n\t"
  152. "fsts fpul, fr14\n\t"
  153. "fsts fpul, fr15\n\t"
  154. "frchg\n\t"
  155. "fsts fpul, fr0\n\t"
  156. "fsts fpul, fr1\n\t"
  157. "fsts fpul, fr2\n\t"
  158. "fsts fpul, fr3\n\t"
  159. "fsts fpul, fr4\n\t"
  160. "fsts fpul, fr5\n\t"
  161. "fsts fpul, fr6\n\t"
  162. "fsts fpul, fr7\n\t"
  163. "fsts fpul, fr8\n\t"
  164. "fsts fpul, fr9\n\t"
  165. "fsts fpul, fr10\n\t"
  166. "fsts fpul, fr11\n\t"
  167. "fsts fpul, fr12\n\t"
  168. "fsts fpul, fr13\n\t"
  169. "fsts fpul, fr14\n\t"
  170. "fsts fpul, fr15\n\t"
  171. "frchg\n\t"
  172. "lds %2, fpscr\n\t"
  173. : /* no output */
  174. : "r" (0), "r" (FPSCR_RCHG), "r" (FPSCR_INIT));
  175. disable_fpu();
  176. }
  177. /**
  178. * denormal_to_double - Given denormalized float number,
  179. * store double float
  180. *
  181. * @fpu: Pointer to sh_fpu_hard structure
  182. * @n: Index to FP register
  183. */
  184. static void
  185. denormal_to_double (struct sh_fpu_hard_struct *fpu, int n)
  186. {
  187. unsigned long du, dl;
  188. unsigned long x = fpu->fpul;
  189. int exp = 1023 - 126;
  190. if (x != 0 && (x & 0x7f800000) == 0) {
  191. du = (x & 0x80000000);
  192. while ((x & 0x00800000) == 0) {
  193. x <<= 1;
  194. exp--;
  195. }
  196. x &= 0x007fffff;
  197. du |= (exp << 20) | (x >> 3);
  198. dl = x << 29;
  199. fpu->fp_regs[n] = du;
  200. fpu->fp_regs[n+1] = dl;
  201. }
  202. }
  203. /**
  204. * ieee_fpe_handler - Handle denormalized number exception
  205. *
  206. * @regs: Pointer to register structure
  207. *
  208. * Returns 1 when it's handled (should not cause exception).
  209. */
  210. static int
  211. ieee_fpe_handler (struct pt_regs *regs)
  212. {
  213. unsigned short insn = *(unsigned short *) regs->pc;
  214. unsigned short finsn;
  215. unsigned long nextpc;
  216. int nib[4] = {
  217. (insn >> 12) & 0xf,
  218. (insn >> 8) & 0xf,
  219. (insn >> 4) & 0xf,
  220. insn & 0xf};
  221. if (nib[0] == 0xb ||
  222. (nib[0] == 0x4 && nib[2] == 0x0 && nib[3] == 0xb)) /* bsr & jsr */
  223. regs->pr = regs->pc + 4;
  224. if (nib[0] == 0xa || nib[0] == 0xb) { /* bra & bsr */
  225. nextpc = regs->pc + 4 + ((short) ((insn & 0xfff) << 4) >> 3);
  226. finsn = *(unsigned short *) (regs->pc + 2);
  227. } else if (nib[0] == 0x8 && nib[1] == 0xd) { /* bt/s */
  228. if (regs->sr & 1)
  229. nextpc = regs->pc + 4 + ((char) (insn & 0xff) << 1);
  230. else
  231. nextpc = regs->pc + 4;
  232. finsn = *(unsigned short *) (regs->pc + 2);
  233. } else if (nib[0] == 0x8 && nib[1] == 0xf) { /* bf/s */
  234. if (regs->sr & 1)
  235. nextpc = regs->pc + 4;
  236. else
  237. nextpc = regs->pc + 4 + ((char) (insn & 0xff) << 1);
  238. finsn = *(unsigned short *) (regs->pc + 2);
  239. } else if (nib[0] == 0x4 && nib[3] == 0xb &&
  240. (nib[2] == 0x0 || nib[2] == 0x2)) { /* jmp & jsr */
  241. nextpc = regs->regs[nib[1]];
  242. finsn = *(unsigned short *) (regs->pc + 2);
  243. } else if (nib[0] == 0x0 && nib[3] == 0x3 &&
  244. (nib[2] == 0x0 || nib[2] == 0x2)) { /* braf & bsrf */
  245. nextpc = regs->pc + 4 + regs->regs[nib[1]];
  246. finsn = *(unsigned short *) (regs->pc + 2);
  247. } else if (insn == 0x000b) { /* rts */
  248. nextpc = regs->pr;
  249. finsn = *(unsigned short *) (regs->pc + 2);
  250. } else {
  251. nextpc = regs->pc + 2;
  252. finsn = insn;
  253. }
  254. if ((finsn & 0xf1ff) == 0xf0ad) { /* fcnvsd */
  255. struct task_struct *tsk = current;
  256. save_fpu(tsk, regs);
  257. if ((tsk->thread.fpu.hard.fpscr & (1 << 17))) {
  258. /* FPU error */
  259. denormal_to_double (&tsk->thread.fpu.hard,
  260. (finsn >> 8) & 0xf);
  261. tsk->thread.fpu.hard.fpscr &=
  262. ~(FPSCR_CAUSE_MASK | FPSCR_FLAG_MASK);
  263. grab_fpu(regs);
  264. restore_fpu(tsk);
  265. set_tsk_thread_flag(tsk, TIF_USEDFPU);
  266. } else {
  267. tsk->thread.trap_no = 11;
  268. tsk->thread.error_code = 0;
  269. force_sig(SIGFPE, tsk);
  270. }
  271. regs->pc = nextpc;
  272. return 1;
  273. }
  274. return 0;
  275. }
  276. asmlinkage void
  277. do_fpu_error(unsigned long r4, unsigned long r5, unsigned long r6, unsigned long r7,
  278. struct pt_regs regs)
  279. {
  280. struct task_struct *tsk = current;
  281. if (ieee_fpe_handler (&regs))
  282. return;
  283. regs.pc += 2;
  284. save_fpu(tsk, &regs);
  285. tsk->thread.trap_no = 11;
  286. tsk->thread.error_code = 0;
  287. force_sig(SIGFPE, tsk);
  288. }
  289. asmlinkage void
  290. do_fpu_state_restore(unsigned long r4, unsigned long r5, unsigned long r6,
  291. unsigned long r7, struct pt_regs regs)
  292. {
  293. struct task_struct *tsk = current;
  294. grab_fpu(&regs);
  295. if (!user_mode(&regs)) {
  296. printk(KERN_ERR "BUG: FPU is used in kernel mode.\n");
  297. return;
  298. }
  299. if (used_math()) {
  300. /* Using the FPU again. */
  301. restore_fpu(tsk);
  302. } else {
  303. /* First time FPU user. */
  304. fpu_init();
  305. set_used_math();
  306. }
  307. set_tsk_thread_flag(tsk, TIF_USEDFPU);
  308. }