math.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Copyright (C) 1999 Eddie C. Dost (ecd@atecom.com)
  3. */
  4. #include <linux/types.h>
  5. #include <linux/sched.h>
  6. #include <asm/uaccess.h>
  7. #include <asm/reg.h>
  8. #include <asm/switch_to.h>
  9. #include <asm/sfp-machine.h>
  10. #include <math-emu/double.h>
  11. #define FLOATFUNC(x) extern int x(void *, void *, void *, void *)
  12. /* The instructions list which may be not implemented by a hardware FPU */
  13. FLOATFUNC(fre);
  14. FLOATFUNC(frsqrtes);
  15. FLOATFUNC(fsqrt);
  16. FLOATFUNC(fsqrts);
  17. FLOATFUNC(mtfsf);
  18. FLOATFUNC(mtfsfi);
  19. #ifdef CONFIG_MATH_EMULATION_HW_UNIMPLEMENTED
  20. #undef FLOATFUNC(x)
  21. #define FLOATFUNC(x) static inline int x(void *op1, void *op2, void *op3, \
  22. void *op4) { }
  23. #endif
  24. FLOATFUNC(fadd);
  25. FLOATFUNC(fadds);
  26. FLOATFUNC(fdiv);
  27. FLOATFUNC(fdivs);
  28. FLOATFUNC(fmul);
  29. FLOATFUNC(fmuls);
  30. FLOATFUNC(fsub);
  31. FLOATFUNC(fsubs);
  32. FLOATFUNC(fmadd);
  33. FLOATFUNC(fmadds);
  34. FLOATFUNC(fmsub);
  35. FLOATFUNC(fmsubs);
  36. FLOATFUNC(fnmadd);
  37. FLOATFUNC(fnmadds);
  38. FLOATFUNC(fnmsub);
  39. FLOATFUNC(fnmsubs);
  40. FLOATFUNC(fctiw);
  41. FLOATFUNC(fctiwz);
  42. FLOATFUNC(frsp);
  43. FLOATFUNC(fcmpo);
  44. FLOATFUNC(fcmpu);
  45. FLOATFUNC(mcrfs);
  46. FLOATFUNC(mffs);
  47. FLOATFUNC(mtfsb0);
  48. FLOATFUNC(mtfsb1);
  49. FLOATFUNC(lfd);
  50. FLOATFUNC(lfs);
  51. FLOATFUNC(stfd);
  52. FLOATFUNC(stfs);
  53. FLOATFUNC(stfiwx);
  54. FLOATFUNC(fabs);
  55. FLOATFUNC(fmr);
  56. FLOATFUNC(fnabs);
  57. FLOATFUNC(fneg);
  58. /* Optional */
  59. FLOATFUNC(fres);
  60. FLOATFUNC(frsqrte);
  61. FLOATFUNC(fsel);
  62. #define OP31 0x1f /* 31 */
  63. #define LFS 0x30 /* 48 */
  64. #define LFSU 0x31 /* 49 */
  65. #define LFD 0x32 /* 50 */
  66. #define LFDU 0x33 /* 51 */
  67. #define STFS 0x34 /* 52 */
  68. #define STFSU 0x35 /* 53 */
  69. #define STFD 0x36 /* 54 */
  70. #define STFDU 0x37 /* 55 */
  71. #define OP59 0x3b /* 59 */
  72. #define OP63 0x3f /* 63 */
  73. /* Opcode 31: */
  74. /* X-Form: */
  75. #define LFSX 0x217 /* 535 */
  76. #define LFSUX 0x237 /* 567 */
  77. #define LFDX 0x257 /* 599 */
  78. #define LFDUX 0x277 /* 631 */
  79. #define STFSX 0x297 /* 663 */
  80. #define STFSUX 0x2b7 /* 695 */
  81. #define STFDX 0x2d7 /* 727 */
  82. #define STFDUX 0x2f7 /* 759 */
  83. #define STFIWX 0x3d7 /* 983 */
  84. /* Opcode 59: */
  85. /* A-Form: */
  86. #define FDIVS 0x012 /* 18 */
  87. #define FSUBS 0x014 /* 20 */
  88. #define FADDS 0x015 /* 21 */
  89. #define FSQRTS 0x016 /* 22 */
  90. #define FRES 0x018 /* 24 */
  91. #define FMULS 0x019 /* 25 */
  92. #define FRSQRTES 0x01a /* 26 */
  93. #define FMSUBS 0x01c /* 28 */
  94. #define FMADDS 0x01d /* 29 */
  95. #define FNMSUBS 0x01e /* 30 */
  96. #define FNMADDS 0x01f /* 31 */
  97. /* Opcode 63: */
  98. /* A-Form: */
  99. #define FDIV 0x012 /* 18 */
  100. #define FSUB 0x014 /* 20 */
  101. #define FADD 0x015 /* 21 */
  102. #define FSQRT 0x016 /* 22 */
  103. #define FSEL 0x017 /* 23 */
  104. #define FRE 0x018 /* 24 */
  105. #define FMUL 0x019 /* 25 */
  106. #define FRSQRTE 0x01a /* 26 */
  107. #define FMSUB 0x01c /* 28 */
  108. #define FMADD 0x01d /* 29 */
  109. #define FNMSUB 0x01e /* 30 */
  110. #define FNMADD 0x01f /* 31 */
  111. /* X-Form: */
  112. #define FCMPU 0x000 /* 0 */
  113. #define FRSP 0x00c /* 12 */
  114. #define FCTIW 0x00e /* 14 */
  115. #define FCTIWZ 0x00f /* 15 */
  116. #define FCMPO 0x020 /* 32 */
  117. #define MTFSB1 0x026 /* 38 */
  118. #define FNEG 0x028 /* 40 */
  119. #define MCRFS 0x040 /* 64 */
  120. #define MTFSB0 0x046 /* 70 */
  121. #define FMR 0x048 /* 72 */
  122. #define MTFSFI 0x086 /* 134 */
  123. #define FNABS 0x088 /* 136 */
  124. #define FABS 0x108 /* 264 */
  125. #define MFFS 0x247 /* 583 */
  126. #define MTFSF 0x2c7 /* 711 */
  127. #define AB 2
  128. #define AC 3
  129. #define ABC 4
  130. #define D 5
  131. #define DU 6
  132. #define X 7
  133. #define XA 8
  134. #define XB 9
  135. #define XCR 11
  136. #define XCRB 12
  137. #define XCRI 13
  138. #define XCRL 16
  139. #define XE 14
  140. #define XEU 15
  141. #define XFLB 10
  142. static int
  143. record_exception(struct pt_regs *regs, int eflag)
  144. {
  145. u32 fpscr;
  146. fpscr = __FPU_FPSCR;
  147. if (eflag) {
  148. fpscr |= FPSCR_FX;
  149. if (eflag & EFLAG_OVERFLOW)
  150. fpscr |= FPSCR_OX;
  151. if (eflag & EFLAG_UNDERFLOW)
  152. fpscr |= FPSCR_UX;
  153. if (eflag & EFLAG_DIVZERO)
  154. fpscr |= FPSCR_ZX;
  155. if (eflag & EFLAG_INEXACT)
  156. fpscr |= FPSCR_XX;
  157. if (eflag & EFLAG_INVALID)
  158. fpscr |= FPSCR_VX;
  159. if (eflag & EFLAG_VXSNAN)
  160. fpscr |= FPSCR_VXSNAN;
  161. if (eflag & EFLAG_VXISI)
  162. fpscr |= FPSCR_VXISI;
  163. if (eflag & EFLAG_VXIDI)
  164. fpscr |= FPSCR_VXIDI;
  165. if (eflag & EFLAG_VXZDZ)
  166. fpscr |= FPSCR_VXZDZ;
  167. if (eflag & EFLAG_VXIMZ)
  168. fpscr |= FPSCR_VXIMZ;
  169. if (eflag & EFLAG_VXVC)
  170. fpscr |= FPSCR_VXVC;
  171. if (eflag & EFLAG_VXSOFT)
  172. fpscr |= FPSCR_VXSOFT;
  173. if (eflag & EFLAG_VXSQRT)
  174. fpscr |= FPSCR_VXSQRT;
  175. if (eflag & EFLAG_VXCVI)
  176. fpscr |= FPSCR_VXCVI;
  177. }
  178. // fpscr &= ~(FPSCR_VX);
  179. if (fpscr & (FPSCR_VXSNAN | FPSCR_VXISI | FPSCR_VXIDI |
  180. FPSCR_VXZDZ | FPSCR_VXIMZ | FPSCR_VXVC |
  181. FPSCR_VXSOFT | FPSCR_VXSQRT | FPSCR_VXCVI))
  182. fpscr |= FPSCR_VX;
  183. fpscr &= ~(FPSCR_FEX);
  184. if (((fpscr & FPSCR_VX) && (fpscr & FPSCR_VE)) ||
  185. ((fpscr & FPSCR_OX) && (fpscr & FPSCR_OE)) ||
  186. ((fpscr & FPSCR_UX) && (fpscr & FPSCR_UE)) ||
  187. ((fpscr & FPSCR_ZX) && (fpscr & FPSCR_ZE)) ||
  188. ((fpscr & FPSCR_XX) && (fpscr & FPSCR_XE)))
  189. fpscr |= FPSCR_FEX;
  190. __FPU_FPSCR = fpscr;
  191. return (fpscr & FPSCR_FEX) ? 1 : 0;
  192. }
  193. int
  194. do_mathemu(struct pt_regs *regs)
  195. {
  196. void *op0 = 0, *op1 = 0, *op2 = 0, *op3 = 0;
  197. unsigned long pc = regs->nip;
  198. signed short sdisp;
  199. u32 insn = 0;
  200. int idx = 0;
  201. int (*func)(void *, void *, void *, void *);
  202. int type = 0;
  203. int eflag, trap;
  204. if (get_user(insn, (u32 *)pc))
  205. return -EFAULT;
  206. switch (insn >> 26) {
  207. case LFS: func = lfs; type = D; break;
  208. case LFSU: func = lfs; type = DU; break;
  209. case LFD: func = lfd; type = D; break;
  210. case LFDU: func = lfd; type = DU; break;
  211. case STFS: func = stfs; type = D; break;
  212. case STFSU: func = stfs; type = DU; break;
  213. case STFD: func = stfd; type = D; break;
  214. case STFDU: func = stfd; type = DU; break;
  215. case OP31:
  216. switch ((insn >> 1) & 0x3ff) {
  217. case LFSX: func = lfs; type = XE; break;
  218. case LFSUX: func = lfs; type = XEU; break;
  219. case LFDX: func = lfd; type = XE; break;
  220. case LFDUX: func = lfd; type = XEU; break;
  221. case STFSX: func = stfs; type = XE; break;
  222. case STFSUX: func = stfs; type = XEU; break;
  223. case STFDX: func = stfd; type = XE; break;
  224. case STFDUX: func = stfd; type = XEU; break;
  225. case STFIWX: func = stfiwx; type = XE; break;
  226. default:
  227. goto illegal;
  228. }
  229. break;
  230. case OP59:
  231. switch ((insn >> 1) & 0x1f) {
  232. case FDIVS: func = fdivs; type = AB; break;
  233. case FSUBS: func = fsubs; type = AB; break;
  234. case FADDS: func = fadds; type = AB; break;
  235. case FSQRTS: func = fsqrts; type = XB; break;
  236. case FRES: func = fres; type = XB; break;
  237. case FMULS: func = fmuls; type = AC; break;
  238. case FRSQRTES: func = frsqrtes;type = XB; break;
  239. case FMSUBS: func = fmsubs; type = ABC; break;
  240. case FMADDS: func = fmadds; type = ABC; break;
  241. case FNMSUBS: func = fnmsubs; type = ABC; break;
  242. case FNMADDS: func = fnmadds; type = ABC; break;
  243. default:
  244. goto illegal;
  245. }
  246. break;
  247. case OP63:
  248. if (insn & 0x20) {
  249. switch ((insn >> 1) & 0x1f) {
  250. case FDIV: func = fdiv; type = AB; break;
  251. case FSUB: func = fsub; type = AB; break;
  252. case FADD: func = fadd; type = AB; break;
  253. case FSQRT: func = fsqrt; type = XB; break;
  254. case FRE: func = fre; type = XB; break;
  255. case FSEL: func = fsel; type = ABC; break;
  256. case FMUL: func = fmul; type = AC; break;
  257. case FRSQRTE: func = frsqrte; type = XB; break;
  258. case FMSUB: func = fmsub; type = ABC; break;
  259. case FMADD: func = fmadd; type = ABC; break;
  260. case FNMSUB: func = fnmsub; type = ABC; break;
  261. case FNMADD: func = fnmadd; type = ABC; break;
  262. default:
  263. goto illegal;
  264. }
  265. break;
  266. }
  267. switch ((insn >> 1) & 0x3ff) {
  268. case FCMPU: func = fcmpu; type = XCR; break;
  269. case FRSP: func = frsp; type = XB; break;
  270. case FCTIW: func = fctiw; type = XB; break;
  271. case FCTIWZ: func = fctiwz; type = XB; break;
  272. case FCMPO: func = fcmpo; type = XCR; break;
  273. case MTFSB1: func = mtfsb1; type = XCRB; break;
  274. case FNEG: func = fneg; type = XB; break;
  275. case MCRFS: func = mcrfs; type = XCRL; break;
  276. case MTFSB0: func = mtfsb0; type = XCRB; break;
  277. case FMR: func = fmr; type = XB; break;
  278. case MTFSFI: func = mtfsfi; type = XCRI; break;
  279. case FNABS: func = fnabs; type = XB; break;
  280. case FABS: func = fabs; type = XB; break;
  281. case MFFS: func = mffs; type = X; break;
  282. case MTFSF: func = mtfsf; type = XFLB; break;
  283. default:
  284. goto illegal;
  285. }
  286. break;
  287. default:
  288. goto illegal;
  289. }
  290. switch (type) {
  291. case AB:
  292. op0 = (void *)&current->thread.TS_FPR((insn >> 21) & 0x1f);
  293. op1 = (void *)&current->thread.TS_FPR((insn >> 16) & 0x1f);
  294. op2 = (void *)&current->thread.TS_FPR((insn >> 11) & 0x1f);
  295. break;
  296. case AC:
  297. op0 = (void *)&current->thread.TS_FPR((insn >> 21) & 0x1f);
  298. op1 = (void *)&current->thread.TS_FPR((insn >> 16) & 0x1f);
  299. op2 = (void *)&current->thread.TS_FPR((insn >> 6) & 0x1f);
  300. break;
  301. case ABC:
  302. op0 = (void *)&current->thread.TS_FPR((insn >> 21) & 0x1f);
  303. op1 = (void *)&current->thread.TS_FPR((insn >> 16) & 0x1f);
  304. op2 = (void *)&current->thread.TS_FPR((insn >> 11) & 0x1f);
  305. op3 = (void *)&current->thread.TS_FPR((insn >> 6) & 0x1f);
  306. break;
  307. case D:
  308. idx = (insn >> 16) & 0x1f;
  309. sdisp = (insn & 0xffff);
  310. op0 = (void *)&current->thread.TS_FPR((insn >> 21) & 0x1f);
  311. op1 = (void *)((idx ? regs->gpr[idx] : 0) + sdisp);
  312. break;
  313. case DU:
  314. idx = (insn >> 16) & 0x1f;
  315. if (!idx)
  316. goto illegal;
  317. sdisp = (insn & 0xffff);
  318. op0 = (void *)&current->thread.TS_FPR((insn >> 21) & 0x1f);
  319. op1 = (void *)(regs->gpr[idx] + sdisp);
  320. break;
  321. case X:
  322. op0 = (void *)&current->thread.TS_FPR((insn >> 21) & 0x1f);
  323. break;
  324. case XA:
  325. op0 = (void *)&current->thread.TS_FPR((insn >> 21) & 0x1f);
  326. op1 = (void *)&current->thread.TS_FPR((insn >> 16) & 0x1f);
  327. break;
  328. case XB:
  329. op0 = (void *)&current->thread.TS_FPR((insn >> 21) & 0x1f);
  330. op1 = (void *)&current->thread.TS_FPR((insn >> 11) & 0x1f);
  331. break;
  332. case XE:
  333. idx = (insn >> 16) & 0x1f;
  334. op0 = (void *)&current->thread.TS_FPR((insn >> 21) & 0x1f);
  335. op1 = (void *)((idx ? regs->gpr[idx] : 0)
  336. + regs->gpr[(insn >> 11) & 0x1f]);
  337. break;
  338. case XEU:
  339. idx = (insn >> 16) & 0x1f;
  340. if (!idx)
  341. goto illegal;
  342. op0 = (void *)&current->thread.TS_FPR((insn >> 21) & 0x1f);
  343. op1 = (void *)(regs->gpr[idx]
  344. + regs->gpr[(insn >> 11) & 0x1f]);
  345. break;
  346. case XCR:
  347. op0 = (void *)&regs->ccr;
  348. op1 = (void *)((insn >> 23) & 0x7);
  349. op2 = (void *)&current->thread.TS_FPR((insn >> 16) & 0x1f);
  350. op3 = (void *)&current->thread.TS_FPR((insn >> 11) & 0x1f);
  351. break;
  352. case XCRL:
  353. op0 = (void *)&regs->ccr;
  354. op1 = (void *)((insn >> 23) & 0x7);
  355. op2 = (void *)((insn >> 18) & 0x7);
  356. break;
  357. case XCRB:
  358. op0 = (void *)((insn >> 21) & 0x1f);
  359. break;
  360. case XCRI:
  361. op0 = (void *)((insn >> 23) & 0x7);
  362. op1 = (void *)((insn >> 12) & 0xf);
  363. break;
  364. case XFLB:
  365. op0 = (void *)((insn >> 17) & 0xff);
  366. op1 = (void *)&current->thread.TS_FPR((insn >> 11) & 0x1f);
  367. break;
  368. default:
  369. goto illegal;
  370. }
  371. /*
  372. * If we support a HW FPU, we need to ensure the FP state
  373. * is flushed into the thread_struct before attempting
  374. * emulation
  375. */
  376. flush_fp_to_thread(current);
  377. eflag = func(op0, op1, op2, op3);
  378. if (insn & 1) {
  379. regs->ccr &= ~(0x0f000000);
  380. regs->ccr |= (__FPU_FPSCR >> 4) & 0x0f000000;
  381. }
  382. trap = record_exception(regs, eflag);
  383. if (trap)
  384. return 1;
  385. switch (type) {
  386. case DU:
  387. case XEU:
  388. regs->gpr[idx] = (unsigned long)op1;
  389. break;
  390. default:
  391. break;
  392. }
  393. regs->nip += 4;
  394. return 0;
  395. illegal:
  396. return -ENOSYS;
  397. }