fpu.c 8.5 KB

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