math.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * arch/sh/math-emu/math.c
  3. *
  4. * Copyright (C) 2006 Takashi YOSHII <takasi-y@ops.dti.ne.jp>
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/types.h>
  13. #include <linux/sched/signal.h>
  14. #include <linux/signal.h>
  15. #include <linux/perf_event.h>
  16. #include <linux/uaccess.h>
  17. #include <asm/fpu.h>
  18. #include <asm/processor.h>
  19. #include <asm/io.h>
  20. #include "sfp-util.h"
  21. #include <math-emu/soft-fp.h>
  22. #include <math-emu/single.h>
  23. #include <math-emu/double.h>
  24. #define FPUL (fregs->fpul)
  25. #define FPSCR (fregs->fpscr)
  26. #define FPSCR_RM (FPSCR&3)
  27. #define FPSCR_DN ((FPSCR>>18)&1)
  28. #define FPSCR_PR ((FPSCR>>19)&1)
  29. #define FPSCR_SZ ((FPSCR>>20)&1)
  30. #define FPSCR_FR ((FPSCR>>21)&1)
  31. #define FPSCR_MASK 0x003fffffUL
  32. #define BANK(n) (n^(FPSCR_FR?16:0))
  33. #define FR ((unsigned long*)(fregs->fp_regs))
  34. #define FR0 (FR[BANK(0)])
  35. #define FRn (FR[BANK(n)])
  36. #define FRm (FR[BANK(m)])
  37. #define DR ((unsigned long long*)(fregs->fp_regs))
  38. #define DRn (DR[BANK(n)/2])
  39. #define DRm (DR[BANK(m)/2])
  40. #define XREG(n) (n^16)
  41. #define XFn (FR[BANK(XREG(n))])
  42. #define XFm (FR[BANK(XREG(m))])
  43. #define XDn (DR[BANK(XREG(n))/2])
  44. #define XDm (DR[BANK(XREG(m))/2])
  45. #define R0 (regs->regs[0])
  46. #define Rn (regs->regs[n])
  47. #define Rm (regs->regs[m])
  48. #define MWRITE(d,a) ({if(put_user(d, (typeof (d) __user *)a)) return -EFAULT;})
  49. #define MREAD(d,a) ({if(get_user(d, (typeof (d) __user *)a)) return -EFAULT;})
  50. #define PACK_S(r,f) FP_PACK_SP(&r,f)
  51. #define UNPACK_S(f,r) FP_UNPACK_SP(f,&r)
  52. #define PACK_D(r,f) \
  53. {u32 t[2]; FP_PACK_DP(t,f); ((u32*)&r)[0]=t[1]; ((u32*)&r)[1]=t[0];}
  54. #define UNPACK_D(f,r) \
  55. {u32 t[2]; t[0]=((u32*)&r)[1]; t[1]=((u32*)&r)[0]; FP_UNPACK_DP(f,t);}
  56. // 2 args instructions.
  57. #define BOTH_PRmn(op,x) \
  58. FP_DECL_EX; if(FPSCR_PR) op(D,x,DRm,DRn); else op(S,x,FRm,FRn);
  59. #define CMP_X(SZ,R,M,N) do{ \
  60. FP_DECL_##SZ(Fm); FP_DECL_##SZ(Fn); \
  61. UNPACK_##SZ(Fm, M); UNPACK_##SZ(Fn, N); \
  62. FP_CMP_##SZ(R, Fn, Fm, 2); }while(0)
  63. #define EQ_X(SZ,R,M,N) do{ \
  64. FP_DECL_##SZ(Fm); FP_DECL_##SZ(Fn); \
  65. UNPACK_##SZ(Fm, M); UNPACK_##SZ(Fn, N); \
  66. FP_CMP_EQ_##SZ(R, Fn, Fm); }while(0)
  67. #define CMP(OP) ({ int r; BOTH_PRmn(OP##_X,r); r; })
  68. static int
  69. fcmp_gt(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
  70. {
  71. if (CMP(CMP) > 0)
  72. regs->sr |= 1;
  73. else
  74. regs->sr &= ~1;
  75. return 0;
  76. }
  77. static int
  78. fcmp_eq(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
  79. {
  80. if (CMP(CMP /*EQ*/) == 0)
  81. regs->sr |= 1;
  82. else
  83. regs->sr &= ~1;
  84. return 0;
  85. }
  86. #define ARITH_X(SZ,OP,M,N) do{ \
  87. FP_DECL_##SZ(Fm); FP_DECL_##SZ(Fn); FP_DECL_##SZ(Fr); \
  88. UNPACK_##SZ(Fm, M); UNPACK_##SZ(Fn, N); \
  89. FP_##OP##_##SZ(Fr, Fn, Fm); \
  90. PACK_##SZ(N, Fr); }while(0)
  91. static int
  92. fadd(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
  93. {
  94. BOTH_PRmn(ARITH_X, ADD);
  95. return 0;
  96. }
  97. static int
  98. fsub(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
  99. {
  100. BOTH_PRmn(ARITH_X, SUB);
  101. return 0;
  102. }
  103. static int
  104. fmul(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
  105. {
  106. BOTH_PRmn(ARITH_X, MUL);
  107. return 0;
  108. }
  109. static int
  110. fdiv(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
  111. {
  112. BOTH_PRmn(ARITH_X, DIV);
  113. return 0;
  114. }
  115. static int
  116. fmac(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
  117. {
  118. FP_DECL_EX;
  119. FP_DECL_S(Fr);
  120. FP_DECL_S(Ft);
  121. FP_DECL_S(F0);
  122. FP_DECL_S(Fm);
  123. FP_DECL_S(Fn);
  124. UNPACK_S(F0, FR0);
  125. UNPACK_S(Fm, FRm);
  126. UNPACK_S(Fn, FRn);
  127. FP_MUL_S(Ft, Fm, F0);
  128. FP_ADD_S(Fr, Fn, Ft);
  129. PACK_S(FRn, Fr);
  130. return 0;
  131. }
  132. // to process fmov's extension (odd n for DR access XD).
  133. #define FMOV_EXT(x) if(x&1) x+=16-1
  134. static int
  135. fmov_idx_reg(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
  136. int n)
  137. {
  138. if (FPSCR_SZ) {
  139. FMOV_EXT(n);
  140. MREAD(FRn, Rm + R0 + 4);
  141. n++;
  142. MREAD(FRn, Rm + R0);
  143. } else {
  144. MREAD(FRn, Rm + R0);
  145. }
  146. return 0;
  147. }
  148. static int
  149. fmov_mem_reg(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
  150. int n)
  151. {
  152. if (FPSCR_SZ) {
  153. FMOV_EXT(n);
  154. MREAD(FRn, Rm + 4);
  155. n++;
  156. MREAD(FRn, Rm);
  157. } else {
  158. MREAD(FRn, Rm);
  159. }
  160. return 0;
  161. }
  162. static int
  163. fmov_inc_reg(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
  164. int n)
  165. {
  166. if (FPSCR_SZ) {
  167. FMOV_EXT(n);
  168. MREAD(FRn, Rm + 4);
  169. n++;
  170. MREAD(FRn, Rm);
  171. Rm += 8;
  172. } else {
  173. MREAD(FRn, Rm);
  174. Rm += 4;
  175. }
  176. return 0;
  177. }
  178. static int
  179. fmov_reg_idx(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
  180. int n)
  181. {
  182. if (FPSCR_SZ) {
  183. FMOV_EXT(m);
  184. MWRITE(FRm, Rn + R0 + 4);
  185. m++;
  186. MWRITE(FRm, Rn + R0);
  187. } else {
  188. MWRITE(FRm, Rn + R0);
  189. }
  190. return 0;
  191. }
  192. static int
  193. fmov_reg_mem(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
  194. int n)
  195. {
  196. if (FPSCR_SZ) {
  197. FMOV_EXT(m);
  198. MWRITE(FRm, Rn + 4);
  199. m++;
  200. MWRITE(FRm, Rn);
  201. } else {
  202. MWRITE(FRm, Rn);
  203. }
  204. return 0;
  205. }
  206. static int
  207. fmov_reg_dec(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
  208. int n)
  209. {
  210. if (FPSCR_SZ) {
  211. FMOV_EXT(m);
  212. Rn -= 8;
  213. MWRITE(FRm, Rn + 4);
  214. m++;
  215. MWRITE(FRm, Rn);
  216. } else {
  217. Rn -= 4;
  218. MWRITE(FRm, Rn);
  219. }
  220. return 0;
  221. }
  222. static int
  223. fmov_reg_reg(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
  224. int n)
  225. {
  226. if (FPSCR_SZ) {
  227. FMOV_EXT(m);
  228. FMOV_EXT(n);
  229. DRn = DRm;
  230. } else {
  231. FRn = FRm;
  232. }
  233. return 0;
  234. }
  235. static int
  236. fnop_mn(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
  237. {
  238. return -EINVAL;
  239. }
  240. // 1 arg instructions.
  241. #define NOTYETn(i) static int i(struct sh_fpu_soft_struct *fregs, int n) \
  242. { printk( #i " not yet done.\n"); return 0; }
  243. NOTYETn(ftrv)
  244. NOTYETn(fsqrt)
  245. NOTYETn(fipr)
  246. NOTYETn(fsca)
  247. NOTYETn(fsrra)
  248. #define EMU_FLOAT_X(SZ,N) do { \
  249. FP_DECL_##SZ(Fn); \
  250. FP_FROM_INT_##SZ(Fn, FPUL, 32, int); \
  251. PACK_##SZ(N, Fn); }while(0)
  252. static int ffloat(struct sh_fpu_soft_struct *fregs, int n)
  253. {
  254. FP_DECL_EX;
  255. if (FPSCR_PR)
  256. EMU_FLOAT_X(D, DRn);
  257. else
  258. EMU_FLOAT_X(S, FRn);
  259. return 0;
  260. }
  261. #define EMU_FTRC_X(SZ,N) do { \
  262. FP_DECL_##SZ(Fn); \
  263. UNPACK_##SZ(Fn, N); \
  264. FP_TO_INT_##SZ(FPUL, Fn, 32, 1); }while(0)
  265. static int ftrc(struct sh_fpu_soft_struct *fregs, int n)
  266. {
  267. FP_DECL_EX;
  268. if (FPSCR_PR)
  269. EMU_FTRC_X(D, DRn);
  270. else
  271. EMU_FTRC_X(S, FRn);
  272. return 0;
  273. }
  274. static int fcnvsd(struct sh_fpu_soft_struct *fregs, int n)
  275. {
  276. FP_DECL_EX;
  277. FP_DECL_S(Fn);
  278. FP_DECL_D(Fr);
  279. UNPACK_S(Fn, FPUL);
  280. FP_CONV(D, S, 2, 1, Fr, Fn);
  281. PACK_D(DRn, Fr);
  282. return 0;
  283. }
  284. static int fcnvds(struct sh_fpu_soft_struct *fregs, int n)
  285. {
  286. FP_DECL_EX;
  287. FP_DECL_D(Fn);
  288. FP_DECL_S(Fr);
  289. UNPACK_D(Fn, DRn);
  290. FP_CONV(S, D, 1, 2, Fr, Fn);
  291. PACK_S(FPUL, Fr);
  292. return 0;
  293. }
  294. static int fxchg(struct sh_fpu_soft_struct *fregs, int flag)
  295. {
  296. FPSCR ^= flag;
  297. return 0;
  298. }
  299. static int fsts(struct sh_fpu_soft_struct *fregs, int n)
  300. {
  301. FRn = FPUL;
  302. return 0;
  303. }
  304. static int flds(struct sh_fpu_soft_struct *fregs, int n)
  305. {
  306. FPUL = FRn;
  307. return 0;
  308. }
  309. static int fneg(struct sh_fpu_soft_struct *fregs, int n)
  310. {
  311. FRn ^= (1 << (_FP_W_TYPE_SIZE - 1));
  312. return 0;
  313. }
  314. static int fabs(struct sh_fpu_soft_struct *fregs, int n)
  315. {
  316. FRn &= ~(1 << (_FP_W_TYPE_SIZE - 1));
  317. return 0;
  318. }
  319. static int fld0(struct sh_fpu_soft_struct *fregs, int n)
  320. {
  321. FRn = 0;
  322. return 0;
  323. }
  324. static int fld1(struct sh_fpu_soft_struct *fregs, int n)
  325. {
  326. FRn = (_FP_EXPBIAS_S << (_FP_FRACBITS_S - 1));
  327. return 0;
  328. }
  329. static int fnop_n(struct sh_fpu_soft_struct *fregs, int n)
  330. {
  331. return -EINVAL;
  332. }
  333. /// Instruction decoders.
  334. static int id_fxfd(struct sh_fpu_soft_struct *, int);
  335. static int id_fnxd(struct sh_fpu_soft_struct *, struct pt_regs *, int, int);
  336. static int (*fnxd[])(struct sh_fpu_soft_struct *, int) = {
  337. fsts, flds, ffloat, ftrc, fneg, fabs, fsqrt, fsrra,
  338. fld0, fld1, fcnvsd, fcnvds, fnop_n, fnop_n, fipr, id_fxfd
  339. };
  340. static int (*fnmx[])(struct sh_fpu_soft_struct *, struct pt_regs *, int, int) = {
  341. fadd, fsub, fmul, fdiv, fcmp_eq, fcmp_gt, fmov_idx_reg, fmov_reg_idx,
  342. fmov_mem_reg, fmov_inc_reg, fmov_reg_mem, fmov_reg_dec,
  343. fmov_reg_reg, id_fnxd, fmac, fnop_mn};
  344. static int id_fxfd(struct sh_fpu_soft_struct *fregs, int x)
  345. {
  346. const int flag[] = { FPSCR_SZ, FPSCR_PR, FPSCR_FR, 0 };
  347. switch (x & 3) {
  348. case 3:
  349. fxchg(fregs, flag[x >> 2]);
  350. break;
  351. case 1:
  352. ftrv(fregs, x - 1);
  353. break;
  354. default:
  355. fsca(fregs, x);
  356. }
  357. return 0;
  358. }
  359. static int
  360. id_fnxd(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int x, int n)
  361. {
  362. return (fnxd[x])(fregs, n);
  363. }
  364. static int
  365. id_fnmx(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, u16 code)
  366. {
  367. int n = (code >> 8) & 0xf, m = (code >> 4) & 0xf, x = code & 0xf;
  368. return (fnmx[x])(fregs, regs, m, n);
  369. }
  370. static int
  371. id_sys(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, u16 code)
  372. {
  373. int n = ((code >> 8) & 0xf);
  374. unsigned long *reg = (code & 0x0010) ? &FPUL : &FPSCR;
  375. switch (code & 0xf0ff) {
  376. case 0x005a:
  377. case 0x006a:
  378. Rn = *reg;
  379. break;
  380. case 0x405a:
  381. case 0x406a:
  382. *reg = Rn;
  383. break;
  384. case 0x4052:
  385. case 0x4062:
  386. Rn -= 4;
  387. MWRITE(*reg, Rn);
  388. break;
  389. case 0x4056:
  390. case 0x4066:
  391. MREAD(*reg, Rn);
  392. Rn += 4;
  393. break;
  394. default:
  395. return -EINVAL;
  396. }
  397. return 0;
  398. }
  399. static int fpu_emulate(u16 code, struct sh_fpu_soft_struct *fregs, struct pt_regs *regs)
  400. {
  401. if ((code & 0xf000) == 0xf000)
  402. return id_fnmx(fregs, regs, code);
  403. else
  404. return id_sys(fregs, regs, code);
  405. }
  406. /**
  407. * fpu_init - Initialize FPU registers
  408. * @fpu: Pointer to software emulated FPU registers.
  409. */
  410. static void fpu_init(struct sh_fpu_soft_struct *fpu)
  411. {
  412. int i;
  413. fpu->fpscr = FPSCR_INIT;
  414. fpu->fpul = 0;
  415. for (i = 0; i < 16; i++) {
  416. fpu->fp_regs[i] = 0;
  417. fpu->xfp_regs[i]= 0;
  418. }
  419. }
  420. /**
  421. * do_fpu_inst - Handle reserved instructions for FPU emulation
  422. * @inst: instruction code.
  423. * @regs: registers on stack.
  424. */
  425. int do_fpu_inst(unsigned short inst, struct pt_regs *regs)
  426. {
  427. struct task_struct *tsk = current;
  428. struct sh_fpu_soft_struct *fpu = &(tsk->thread.xstate->softfpu);
  429. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
  430. if (!(task_thread_info(tsk)->status & TS_USEDFPU)) {
  431. /* initialize once. */
  432. fpu_init(fpu);
  433. task_thread_info(tsk)->status |= TS_USEDFPU;
  434. }
  435. return fpu_emulate(inst, fpu, regs);
  436. }