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