fpu.c 12 KB

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