math.c 9.7 KB

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