fpu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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/system.h>
  19. #include <asm/fpu.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.fpu.hard.status)),
  85. "r"(FPSCR_RCHG), "r"(FPSCR_INIT)
  86. :"memory");
  87. disable_fpu();
  88. }
  89. static 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.fpu), "r"(FPSCR_RCHG)
  132. :"memory");
  133. disable_fpu();
  134. }
  135. /*
  136. * Load the FPU with signalling NANS. This bit pattern we're using
  137. * has the property that no matter wether considered as single or as
  138. * double precision represents signaling NANS.
  139. */
  140. static void fpu_init(void)
  141. {
  142. enable_fpu();
  143. asm volatile ( "lds %0, fpul\n\t"
  144. "lds %1, fpscr\n\t"
  145. "fsts fpul, fr0\n\t"
  146. "fsts fpul, fr1\n\t"
  147. "fsts fpul, fr2\n\t"
  148. "fsts fpul, fr3\n\t"
  149. "fsts fpul, fr4\n\t"
  150. "fsts fpul, fr5\n\t"
  151. "fsts fpul, fr6\n\t"
  152. "fsts fpul, fr7\n\t"
  153. "fsts fpul, fr8\n\t"
  154. "fsts fpul, fr9\n\t"
  155. "fsts fpul, fr10\n\t"
  156. "fsts fpul, fr11\n\t"
  157. "fsts fpul, fr12\n\t"
  158. "fsts fpul, fr13\n\t"
  159. "fsts fpul, fr14\n\t"
  160. "fsts fpul, fr15\n\t"
  161. "frchg\n\t"
  162. "fsts fpul, fr0\n\t"
  163. "fsts fpul, fr1\n\t"
  164. "fsts fpul, fr2\n\t"
  165. "fsts fpul, fr3\n\t"
  166. "fsts fpul, fr4\n\t"
  167. "fsts fpul, fr5\n\t"
  168. "fsts fpul, fr6\n\t"
  169. "fsts fpul, fr7\n\t"
  170. "fsts fpul, fr8\n\t"
  171. "fsts fpul, fr9\n\t"
  172. "fsts fpul, fr10\n\t"
  173. "fsts fpul, fr11\n\t"
  174. "fsts fpul, fr12\n\t"
  175. "fsts fpul, fr13\n\t"
  176. "fsts fpul, fr14\n\t"
  177. "fsts fpul, fr15\n\t"
  178. "frchg\n\t"
  179. "lds %2, fpscr\n\t"
  180. : /* no output */
  181. :"r" (0), "r"(FPSCR_RCHG), "r"(FPSCR_INIT));
  182. disable_fpu();
  183. }
  184. /**
  185. * denormal_to_double - Given denormalized float number,
  186. * store double float
  187. *
  188. * @fpu: Pointer to sh_fpu_hard structure
  189. * @n: Index to FP register
  190. */
  191. static void denormal_to_double(struct sh_fpu_hard_struct *fpu, int n)
  192. {
  193. unsigned long du, dl;
  194. unsigned long x = fpu->fpul;
  195. int exp = 1023 - 126;
  196. if (x != 0 && (x & 0x7f800000) == 0) {
  197. du = (x & 0x80000000);
  198. while ((x & 0x00800000) == 0) {
  199. x <<= 1;
  200. exp--;
  201. }
  202. x &= 0x007fffff;
  203. du |= (exp << 20) | (x >> 3);
  204. dl = x << 29;
  205. fpu->fp_regs[n] = du;
  206. fpu->fp_regs[n + 1] = dl;
  207. }
  208. }
  209. /**
  210. * ieee_fpe_handler - Handle denormalized number exception
  211. *
  212. * @regs: Pointer to register structure
  213. *
  214. * Returns 1 when it's handled (should not cause exception).
  215. */
  216. static int ieee_fpe_handler(struct pt_regs *regs)
  217. {
  218. unsigned short insn = *(unsigned short *)regs->pc;
  219. unsigned short finsn;
  220. unsigned long nextpc;
  221. int nib[4] = {
  222. (insn >> 12) & 0xf,
  223. (insn >> 8) & 0xf,
  224. (insn >> 4) & 0xf,
  225. insn & 0xf
  226. };
  227. if (nib[0] == 0xb || (nib[0] == 0x4 && nib[2] == 0x0 && nib[3] == 0xb))
  228. regs->pr = regs->pc + 4; /* bsr & jsr */
  229. if (nib[0] == 0xa || nib[0] == 0xb) {
  230. /* bra & bsr */
  231. nextpc = regs->pc + 4 + ((short)((insn & 0xfff) << 4) >> 3);
  232. finsn = *(unsigned short *)(regs->pc + 2);
  233. } else if (nib[0] == 0x8 && nib[1] == 0xd) {
  234. /* bt/s */
  235. if (regs->sr & 1)
  236. nextpc = regs->pc + 4 + ((char)(insn & 0xff) << 1);
  237. else
  238. nextpc = regs->pc + 4;
  239. finsn = *(unsigned short *)(regs->pc + 2);
  240. } else if (nib[0] == 0x8 && nib[1] == 0xf) {
  241. /* bf/s */
  242. if (regs->sr & 1)
  243. nextpc = regs->pc + 4;
  244. else
  245. nextpc = regs->pc + 4 + ((char)(insn & 0xff) << 1);
  246. finsn = *(unsigned short *)(regs->pc + 2);
  247. } else if (nib[0] == 0x4 && nib[3] == 0xb &&
  248. (nib[2] == 0x0 || nib[2] == 0x2)) {
  249. /* jmp & jsr */
  250. nextpc = regs->regs[nib[1]];
  251. finsn = *(unsigned short *)(regs->pc + 2);
  252. } else if (nib[0] == 0x0 && nib[3] == 0x3 &&
  253. (nib[2] == 0x0 || nib[2] == 0x2)) {
  254. /* braf & bsrf */
  255. nextpc = regs->pc + 4 + regs->regs[nib[1]];
  256. finsn = *(unsigned short *)(regs->pc + 2);
  257. } else if (insn == 0x000b) {
  258. /* rts */
  259. nextpc = regs->pr;
  260. finsn = *(unsigned short *)(regs->pc + 2);
  261. } else {
  262. nextpc = regs->pc + instruction_size(insn);
  263. finsn = insn;
  264. }
  265. if ((finsn & 0xf1ff) == 0xf0ad) {
  266. /* fcnvsd */
  267. struct task_struct *tsk = current;
  268. if ((tsk->thread.fpu.hard.fpscr & FPSCR_CAUSE_ERROR))
  269. /* FPU error */
  270. denormal_to_double(&tsk->thread.fpu.hard,
  271. (finsn >> 8) & 0xf);
  272. else
  273. return 0;
  274. regs->pc = nextpc;
  275. return 1;
  276. } else if ((finsn & 0xf00f) == 0xf002) {
  277. /* fmul */
  278. struct task_struct *tsk = current;
  279. int fpscr;
  280. int n, m, prec;
  281. unsigned int hx, hy;
  282. n = (finsn >> 8) & 0xf;
  283. m = (finsn >> 4) & 0xf;
  284. hx = tsk->thread.fpu.hard.fp_regs[n];
  285. hy = tsk->thread.fpu.hard.fp_regs[m];
  286. fpscr = tsk->thread.fpu.hard.fpscr;
  287. prec = fpscr & FPSCR_DBL_PRECISION;
  288. if ((fpscr & FPSCR_CAUSE_ERROR)
  289. && (prec && ((hx & 0x7fffffff) < 0x00100000
  290. || (hy & 0x7fffffff) < 0x00100000))) {
  291. long long llx, lly;
  292. /* FPU error because of denormal (doubles) */
  293. llx = ((long long)hx << 32)
  294. | tsk->thread.fpu.hard.fp_regs[n + 1];
  295. lly = ((long long)hy << 32)
  296. | tsk->thread.fpu.hard.fp_regs[m + 1];
  297. llx = float64_mul(llx, lly);
  298. tsk->thread.fpu.hard.fp_regs[n] = llx >> 32;
  299. tsk->thread.fpu.hard.fp_regs[n + 1] = llx & 0xffffffff;
  300. } else if ((fpscr & FPSCR_CAUSE_ERROR)
  301. && (!prec && ((hx & 0x7fffffff) < 0x00800000
  302. || (hy & 0x7fffffff) < 0x00800000))) {
  303. /* FPU error because of denormal (floats) */
  304. hx = float32_mul(hx, hy);
  305. tsk->thread.fpu.hard.fp_regs[n] = hx;
  306. } else
  307. return 0;
  308. regs->pc = nextpc;
  309. return 1;
  310. } else if ((finsn & 0xf00e) == 0xf000) {
  311. /* fadd, fsub */
  312. struct task_struct *tsk = current;
  313. int fpscr;
  314. int n, m, prec;
  315. unsigned int hx, hy;
  316. n = (finsn >> 8) & 0xf;
  317. m = (finsn >> 4) & 0xf;
  318. hx = tsk->thread.fpu.hard.fp_regs[n];
  319. hy = tsk->thread.fpu.hard.fp_regs[m];
  320. fpscr = tsk->thread.fpu.hard.fpscr;
  321. prec = fpscr & FPSCR_DBL_PRECISION;
  322. if ((fpscr & FPSCR_CAUSE_ERROR)
  323. && (prec && ((hx & 0x7fffffff) < 0x00100000
  324. || (hy & 0x7fffffff) < 0x00100000))) {
  325. long long llx, lly;
  326. /* FPU error because of denormal (doubles) */
  327. llx = ((long long)hx << 32)
  328. | tsk->thread.fpu.hard.fp_regs[n + 1];
  329. lly = ((long long)hy << 32)
  330. | tsk->thread.fpu.hard.fp_regs[m + 1];
  331. if ((finsn & 0xf00f) == 0xf000)
  332. llx = float64_add(llx, lly);
  333. else
  334. llx = float64_sub(llx, lly);
  335. tsk->thread.fpu.hard.fp_regs[n] = llx >> 32;
  336. tsk->thread.fpu.hard.fp_regs[n + 1] = llx & 0xffffffff;
  337. } else if ((fpscr & FPSCR_CAUSE_ERROR)
  338. && (!prec && ((hx & 0x7fffffff) < 0x00800000
  339. || (hy & 0x7fffffff) < 0x00800000))) {
  340. /* FPU error because of denormal (floats) */
  341. if ((finsn & 0xf00f) == 0xf000)
  342. hx = float32_add(hx, hy);
  343. else
  344. hx = float32_sub(hx, hy);
  345. tsk->thread.fpu.hard.fp_regs[n] = hx;
  346. } else
  347. return 0;
  348. regs->pc = nextpc;
  349. return 1;
  350. } else if ((finsn & 0xf003) == 0xf003) {
  351. /* fdiv */
  352. struct task_struct *tsk = current;
  353. int fpscr;
  354. int n, m, prec;
  355. unsigned int hx, hy;
  356. n = (finsn >> 8) & 0xf;
  357. m = (finsn >> 4) & 0xf;
  358. hx = tsk->thread.fpu.hard.fp_regs[n];
  359. hy = tsk->thread.fpu.hard.fp_regs[m];
  360. fpscr = tsk->thread.fpu.hard.fpscr;
  361. prec = fpscr & FPSCR_DBL_PRECISION;
  362. if ((fpscr & FPSCR_CAUSE_ERROR)
  363. && (prec && ((hx & 0x7fffffff) < 0x00100000
  364. || (hy & 0x7fffffff) < 0x00100000))) {
  365. long long llx, lly;
  366. /* FPU error because of denormal (doubles) */
  367. llx = ((long long)hx << 32)
  368. | tsk->thread.fpu.hard.fp_regs[n + 1];
  369. lly = ((long long)hy << 32)
  370. | tsk->thread.fpu.hard.fp_regs[m + 1];
  371. llx = float64_div(llx, lly);
  372. tsk->thread.fpu.hard.fp_regs[n] = llx >> 32;
  373. tsk->thread.fpu.hard.fp_regs[n + 1] = llx & 0xffffffff;
  374. } else if ((fpscr & FPSCR_CAUSE_ERROR)
  375. && (!prec && ((hx & 0x7fffffff) < 0x00800000
  376. || (hy & 0x7fffffff) < 0x00800000))) {
  377. /* FPU error because of denormal (floats) */
  378. hx = float32_div(hx, hy);
  379. tsk->thread.fpu.hard.fp_regs[n] = hx;
  380. } else
  381. return 0;
  382. regs->pc = nextpc;
  383. return 1;
  384. } else if ((finsn & 0xf0bd) == 0xf0bd) {
  385. /* fcnvds - double to single precision convert */
  386. struct task_struct *tsk = current;
  387. int m;
  388. unsigned int hx;
  389. m = (finsn >> 8) & 0x7;
  390. hx = tsk->thread.fpu.hard.fp_regs[m];
  391. if ((tsk->thread.fpu.hard.fpscr & FPSCR_CAUSE_ERROR)
  392. && ((hx & 0x7fffffff) < 0x00100000)) {
  393. /* subnormal double to float conversion */
  394. long long llx;
  395. llx = ((long long)tsk->thread.fpu.hard.fp_regs[m] << 32)
  396. | tsk->thread.fpu.hard.fp_regs[m + 1];
  397. tsk->thread.fpu.hard.fpul = float64_to_float32(llx);
  398. } else
  399. return 0;
  400. regs->pc = nextpc;
  401. return 1;
  402. }
  403. return 0;
  404. }
  405. void float_raise(unsigned int flags)
  406. {
  407. fpu_exception_flags |= flags;
  408. }
  409. int float_rounding_mode(void)
  410. {
  411. struct task_struct *tsk = current;
  412. int roundingMode = FPSCR_ROUNDING_MODE(tsk->thread.fpu.hard.fpscr);
  413. return roundingMode;
  414. }
  415. BUILD_TRAP_HANDLER(fpu_error)
  416. {
  417. struct task_struct *tsk = current;
  418. TRAP_HANDLER_DECL;
  419. __unlazy_fpu(tsk, regs);
  420. fpu_exception_flags = 0;
  421. if (ieee_fpe_handler(regs)) {
  422. tsk->thread.fpu.hard.fpscr &=
  423. ~(FPSCR_CAUSE_MASK | FPSCR_FLAG_MASK);
  424. tsk->thread.fpu.hard.fpscr |= fpu_exception_flags;
  425. /* Set the FPSCR flag as well as cause bits - simply
  426. * replicate the cause */
  427. tsk->thread.fpu.hard.fpscr |= (fpu_exception_flags >> 10);
  428. grab_fpu(regs);
  429. restore_fpu(tsk);
  430. task_thread_info(tsk)->status |= TS_USEDFPU;
  431. if ((((tsk->thread.fpu.hard.fpscr & FPSCR_ENABLE_MASK) >> 7) &
  432. (fpu_exception_flags >> 2)) == 0) {
  433. return;
  434. }
  435. }
  436. force_sig(SIGFPE, tsk);
  437. }
  438. void fpu_state_restore(struct pt_regs *regs)
  439. {
  440. struct task_struct *tsk = current;
  441. grab_fpu(regs);
  442. if (unlikely(!user_mode(regs))) {
  443. printk(KERN_ERR "BUG: FPU is used in kernel mode.\n");
  444. BUG();
  445. return;
  446. }
  447. if (likely(used_math())) {
  448. /* Using the FPU again. */
  449. restore_fpu(tsk);
  450. } else {
  451. /* First time FPU user. */
  452. fpu_init();
  453. set_used_math();
  454. }
  455. task_thread_info(tsk)->status |= TS_USEDFPU;
  456. tsk->fpu_counter++;
  457. }
  458. BUILD_TRAP_HANDLER(fpu_state_restore)
  459. {
  460. TRAP_HANDLER_DECL;
  461. fpu_state_restore(regs);
  462. }