math.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. #include <linux/module.h>
  2. #include <linux/types.h>
  3. #include <linux/kernel.h>
  4. #include <linux/sched.h>
  5. #include <asm/uaccess.h>
  6. #include "sfp-util.h"
  7. #include <math-emu/soft-fp.h>
  8. #include <math-emu/single.h>
  9. #include <math-emu/double.h>
  10. #define OPC_PAL 0x00
  11. #define OPC_INTA 0x10
  12. #define OPC_INTL 0x11
  13. #define OPC_INTS 0x12
  14. #define OPC_INTM 0x13
  15. #define OPC_FLTC 0x14
  16. #define OPC_FLTV 0x15
  17. #define OPC_FLTI 0x16
  18. #define OPC_FLTL 0x17
  19. #define OPC_MISC 0x18
  20. #define OPC_JSR 0x1a
  21. #define FOP_SRC_S 0
  22. #define FOP_SRC_T 2
  23. #define FOP_SRC_Q 3
  24. #define FOP_FNC_ADDx 0
  25. #define FOP_FNC_CVTQL 0
  26. #define FOP_FNC_SUBx 1
  27. #define FOP_FNC_MULx 2
  28. #define FOP_FNC_DIVx 3
  29. #define FOP_FNC_CMPxUN 4
  30. #define FOP_FNC_CMPxEQ 5
  31. #define FOP_FNC_CMPxLT 6
  32. #define FOP_FNC_CMPxLE 7
  33. #define FOP_FNC_SQRTx 11
  34. #define FOP_FNC_CVTxS 12
  35. #define FOP_FNC_CVTxT 14
  36. #define FOP_FNC_CVTxQ 15
  37. #define MISC_TRAPB 0x0000
  38. #define MISC_EXCB 0x0400
  39. extern unsigned long alpha_read_fp_reg (unsigned long reg);
  40. extern void alpha_write_fp_reg (unsigned long reg, unsigned long val);
  41. extern unsigned long alpha_read_fp_reg_s (unsigned long reg);
  42. extern void alpha_write_fp_reg_s (unsigned long reg, unsigned long val);
  43. #ifdef MODULE
  44. MODULE_DESCRIPTION("FP Software completion module");
  45. extern long (*alpha_fp_emul_imprecise)(struct pt_regs *, unsigned long);
  46. extern long (*alpha_fp_emul) (unsigned long pc);
  47. static long (*save_emul_imprecise)(struct pt_regs *, unsigned long);
  48. static long (*save_emul) (unsigned long pc);
  49. long do_alpha_fp_emul_imprecise(struct pt_regs *, unsigned long);
  50. long do_alpha_fp_emul(unsigned long);
  51. int init_module(void)
  52. {
  53. save_emul_imprecise = alpha_fp_emul_imprecise;
  54. save_emul = alpha_fp_emul;
  55. alpha_fp_emul_imprecise = do_alpha_fp_emul_imprecise;
  56. alpha_fp_emul = do_alpha_fp_emul;
  57. return 0;
  58. }
  59. void cleanup_module(void)
  60. {
  61. alpha_fp_emul_imprecise = save_emul_imprecise;
  62. alpha_fp_emul = save_emul;
  63. }
  64. #undef alpha_fp_emul_imprecise
  65. #define alpha_fp_emul_imprecise do_alpha_fp_emul_imprecise
  66. #undef alpha_fp_emul
  67. #define alpha_fp_emul do_alpha_fp_emul
  68. #endif /* MODULE */
  69. /*
  70. * Emulate the floating point instruction at address PC. Returns -1 if the
  71. * instruction to be emulated is illegal (such as with the opDEC trap), else
  72. * the SI_CODE for a SIGFPE signal, else 0 if everything's ok.
  73. *
  74. * Notice that the kernel does not and cannot use FP regs. This is good
  75. * because it means that instead of saving/restoring all fp regs, we simply
  76. * stick the result of the operation into the appropriate register.
  77. */
  78. long
  79. alpha_fp_emul (unsigned long pc)
  80. {
  81. FP_DECL_EX;
  82. FP_DECL_S(SA); FP_DECL_S(SB); FP_DECL_S(SR);
  83. FP_DECL_D(DA); FP_DECL_D(DB); FP_DECL_D(DR);
  84. unsigned long fa, fb, fc, func, mode, src;
  85. unsigned long res, va, vb, vc, swcr, fpcr;
  86. __u32 insn;
  87. long si_code;
  88. get_user(insn, (__u32 __user *)pc);
  89. fc = (insn >> 0) & 0x1f; /* destination register */
  90. fb = (insn >> 16) & 0x1f;
  91. fa = (insn >> 21) & 0x1f;
  92. func = (insn >> 5) & 0xf;
  93. src = (insn >> 9) & 0x3;
  94. mode = (insn >> 11) & 0x3;
  95. fpcr = rdfpcr();
  96. swcr = swcr_update_status(current_thread_info()->ieee_state, fpcr);
  97. if (mode == 3) {
  98. /* Dynamic -- get rounding mode from fpcr. */
  99. mode = (fpcr >> FPCR_DYN_SHIFT) & 3;
  100. }
  101. switch (src) {
  102. case FOP_SRC_S:
  103. va = alpha_read_fp_reg_s(fa);
  104. vb = alpha_read_fp_reg_s(fb);
  105. FP_UNPACK_SP(SA, &va);
  106. FP_UNPACK_SP(SB, &vb);
  107. switch (func) {
  108. case FOP_FNC_SUBx:
  109. FP_SUB_S(SR, SA, SB);
  110. goto pack_s;
  111. case FOP_FNC_ADDx:
  112. FP_ADD_S(SR, SA, SB);
  113. goto pack_s;
  114. case FOP_FNC_MULx:
  115. FP_MUL_S(SR, SA, SB);
  116. goto pack_s;
  117. case FOP_FNC_DIVx:
  118. FP_DIV_S(SR, SA, SB);
  119. goto pack_s;
  120. case FOP_FNC_SQRTx:
  121. FP_SQRT_S(SR, SB);
  122. goto pack_s;
  123. }
  124. goto bad_insn;
  125. case FOP_SRC_T:
  126. va = alpha_read_fp_reg(fa);
  127. vb = alpha_read_fp_reg(fb);
  128. if ((func & ~3) == FOP_FNC_CMPxUN) {
  129. FP_UNPACK_RAW_DP(DA, &va);
  130. FP_UNPACK_RAW_DP(DB, &vb);
  131. if (!DA_e && !_FP_FRAC_ZEROP_1(DA)) {
  132. FP_SET_EXCEPTION(FP_EX_DENORM);
  133. if (FP_DENORM_ZERO)
  134. _FP_FRAC_SET_1(DA, _FP_ZEROFRAC_1);
  135. }
  136. if (!DB_e && !_FP_FRAC_ZEROP_1(DB)) {
  137. FP_SET_EXCEPTION(FP_EX_DENORM);
  138. if (FP_DENORM_ZERO)
  139. _FP_FRAC_SET_1(DB, _FP_ZEROFRAC_1);
  140. }
  141. FP_CMP_D(res, DA, DB, 3);
  142. vc = 0x4000000000000000UL;
  143. /* CMPTEQ, CMPTUN don't trap on QNaN,
  144. while CMPTLT and CMPTLE do */
  145. if (res == 3
  146. && ((func & 3) >= 2
  147. || FP_ISSIGNAN_D(DA)
  148. || FP_ISSIGNAN_D(DB))) {
  149. FP_SET_EXCEPTION(FP_EX_INVALID);
  150. }
  151. switch (func) {
  152. case FOP_FNC_CMPxUN: if (res != 3) vc = 0; break;
  153. case FOP_FNC_CMPxEQ: if (res) vc = 0; break;
  154. case FOP_FNC_CMPxLT: if (res != -1) vc = 0; break;
  155. case FOP_FNC_CMPxLE: if ((long)res > 0) vc = 0; break;
  156. }
  157. goto done_d;
  158. }
  159. FP_UNPACK_DP(DA, &va);
  160. FP_UNPACK_DP(DB, &vb);
  161. switch (func) {
  162. case FOP_FNC_SUBx:
  163. FP_SUB_D(DR, DA, DB);
  164. goto pack_d;
  165. case FOP_FNC_ADDx:
  166. FP_ADD_D(DR, DA, DB);
  167. goto pack_d;
  168. case FOP_FNC_MULx:
  169. FP_MUL_D(DR, DA, DB);
  170. goto pack_d;
  171. case FOP_FNC_DIVx:
  172. FP_DIV_D(DR, DA, DB);
  173. goto pack_d;
  174. case FOP_FNC_SQRTx:
  175. FP_SQRT_D(DR, DB);
  176. goto pack_d;
  177. case FOP_FNC_CVTxS:
  178. /* It is irritating that DEC encoded CVTST with
  179. SRC == T_floating. It is also interesting that
  180. the bit used to tell the two apart is /U... */
  181. if (insn & 0x2000) {
  182. FP_CONV(S,D,1,1,SR,DB);
  183. goto pack_s;
  184. } else {
  185. vb = alpha_read_fp_reg_s(fb);
  186. FP_UNPACK_SP(SB, &vb);
  187. DR_c = DB_c;
  188. DR_s = DB_s;
  189. DR_e = DB_e + (1024 - 128);
  190. DR_f = SB_f << (52 - 23);
  191. goto pack_d;
  192. }
  193. case FOP_FNC_CVTxQ:
  194. if (DB_c == FP_CLS_NAN
  195. && (_FP_FRAC_HIGH_RAW_D(DB) & _FP_QNANBIT_D)) {
  196. /* AAHB Table B-2 says QNaN should not trigger INV */
  197. vc = 0;
  198. } else
  199. FP_TO_INT_ROUND_D(vc, DB, 64, 2);
  200. goto done_d;
  201. }
  202. goto bad_insn;
  203. case FOP_SRC_Q:
  204. vb = alpha_read_fp_reg(fb);
  205. switch (func) {
  206. case FOP_FNC_CVTQL:
  207. /* Notice: We can get here only due to an integer
  208. overflow. Such overflows are reported as invalid
  209. ops. We return the result the hw would have
  210. computed. */
  211. vc = ((vb & 0xc0000000) << 32 | /* sign and msb */
  212. (vb & 0x3fffffff) << 29); /* rest of the int */
  213. FP_SET_EXCEPTION (FP_EX_INVALID);
  214. goto done_d;
  215. case FOP_FNC_CVTxS:
  216. FP_FROM_INT_S(SR, ((long)vb), 64, long);
  217. goto pack_s;
  218. case FOP_FNC_CVTxT:
  219. FP_FROM_INT_D(DR, ((long)vb), 64, long);
  220. goto pack_d;
  221. }
  222. goto bad_insn;
  223. }
  224. goto bad_insn;
  225. pack_s:
  226. FP_PACK_SP(&vc, SR);
  227. if ((_fex & FP_EX_UNDERFLOW) && (swcr & IEEE_MAP_UMZ))
  228. vc = 0;
  229. alpha_write_fp_reg_s(fc, vc);
  230. goto done;
  231. pack_d:
  232. FP_PACK_DP(&vc, DR);
  233. if ((_fex & FP_EX_UNDERFLOW) && (swcr & IEEE_MAP_UMZ))
  234. vc = 0;
  235. done_d:
  236. alpha_write_fp_reg(fc, vc);
  237. goto done;
  238. /*
  239. * Take the appropriate action for each possible
  240. * floating-point result:
  241. *
  242. * - Set the appropriate bits in the FPCR
  243. * - If the specified exception is enabled in the FPCR,
  244. * return. The caller (entArith) will dispatch
  245. * the appropriate signal to the translated program.
  246. *
  247. * In addition, properly track the exception state in software
  248. * as described in the Alpha Architecture Handbook section 4.7.7.3.
  249. */
  250. done:
  251. if (_fex) {
  252. /* Record exceptions in software control word. */
  253. swcr |= (_fex << IEEE_STATUS_TO_EXCSUM_SHIFT);
  254. current_thread_info()->ieee_state
  255. |= (_fex << IEEE_STATUS_TO_EXCSUM_SHIFT);
  256. /* Update hardware control register. */
  257. fpcr &= (~FPCR_MASK | FPCR_DYN_MASK);
  258. fpcr |= ieee_swcr_to_fpcr(swcr);
  259. wrfpcr(fpcr);
  260. /* Do we generate a signal? */
  261. _fex = _fex & swcr & IEEE_TRAP_ENABLE_MASK;
  262. si_code = 0;
  263. if (_fex) {
  264. if (_fex & IEEE_TRAP_ENABLE_DNO) si_code = FPE_FLTUND;
  265. if (_fex & IEEE_TRAP_ENABLE_INE) si_code = FPE_FLTRES;
  266. if (_fex & IEEE_TRAP_ENABLE_UNF) si_code = FPE_FLTUND;
  267. if (_fex & IEEE_TRAP_ENABLE_OVF) si_code = FPE_FLTOVF;
  268. if (_fex & IEEE_TRAP_ENABLE_DZE) si_code = FPE_FLTDIV;
  269. if (_fex & IEEE_TRAP_ENABLE_INV) si_code = FPE_FLTINV;
  270. }
  271. return si_code;
  272. }
  273. /* We used to write the destination register here, but DEC FORTRAN
  274. requires that the result *always* be written... so we do the write
  275. immediately after the operations above. */
  276. return 0;
  277. bad_insn:
  278. printk(KERN_ERR "alpha_fp_emul: Invalid FP insn %#x at %#lx\n",
  279. insn, pc);
  280. return -1;
  281. }
  282. long
  283. alpha_fp_emul_imprecise (struct pt_regs *regs, unsigned long write_mask)
  284. {
  285. unsigned long trigger_pc = regs->pc - 4;
  286. unsigned long insn, opcode, rc, si_code = 0;
  287. /*
  288. * Turn off the bits corresponding to registers that are the
  289. * target of instructions that set bits in the exception
  290. * summary register. We have some slack doing this because a
  291. * register that is the target of a trapping instruction can
  292. * be written at most once in the trap shadow.
  293. *
  294. * Branches, jumps, TRAPBs, EXCBs and calls to PALcode all
  295. * bound the trap shadow, so we need not look any further than
  296. * up to the first occurrence of such an instruction.
  297. */
  298. while (write_mask) {
  299. get_user(insn, (__u32 __user *)(trigger_pc));
  300. opcode = insn >> 26;
  301. rc = insn & 0x1f;
  302. switch (opcode) {
  303. case OPC_PAL:
  304. case OPC_JSR:
  305. case 0x30 ... 0x3f: /* branches */
  306. goto egress;
  307. case OPC_MISC:
  308. switch (insn & 0xffff) {
  309. case MISC_TRAPB:
  310. case MISC_EXCB:
  311. goto egress;
  312. default:
  313. break;
  314. }
  315. break;
  316. case OPC_INTA:
  317. case OPC_INTL:
  318. case OPC_INTS:
  319. case OPC_INTM:
  320. write_mask &= ~(1UL << rc);
  321. break;
  322. case OPC_FLTC:
  323. case OPC_FLTV:
  324. case OPC_FLTI:
  325. case OPC_FLTL:
  326. write_mask &= ~(1UL << (rc + 32));
  327. break;
  328. }
  329. if (!write_mask) {
  330. /* Re-execute insns in the trap-shadow. */
  331. regs->pc = trigger_pc + 4;
  332. si_code = alpha_fp_emul(trigger_pc);
  333. goto egress;
  334. }
  335. trigger_pc -= 4;
  336. }
  337. egress:
  338. return si_code;
  339. }