decode_exc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Linux/PA-RISC Project (http://www.parisc-linux.org/)
  3. *
  4. * Floating-point emulation code
  5. * Copyright (C) 2001 Hewlett-Packard (Paul Bame) <bame@debian.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /*
  22. * BEGIN_DESC
  23. *
  24. * File:
  25. * @(#) pa/fp/decode_exc.c $ Revision: $
  26. *
  27. * Purpose:
  28. * <<please update with a synopsis of the functionality provided by this file>>
  29. *
  30. * External Interfaces:
  31. * <<the following list was autogenerated, please review>>
  32. * decode_fpu(Fpu_register, trap_counts)
  33. *
  34. * Internal Interfaces:
  35. * <<please update>>
  36. *
  37. * Theory:
  38. * <<please update with a overview of the operation of this file>>
  39. *
  40. * END_DESC
  41. */
  42. #include "float.h"
  43. #include "sgl_float.h"
  44. #include "dbl_float.h"
  45. #include "cnv_float.h"
  46. /* #include "types.h" */
  47. #include <asm/signal.h>
  48. #include <asm/siginfo.h>
  49. /* #include <machine/sys/mdep_private.h> */
  50. #undef Fpustatus_register
  51. #define Fpustatus_register Fpu_register[0]
  52. /* General definitions */
  53. #define DOESTRAP 1
  54. #define NOTRAP 0
  55. #define SIGNALCODE(signal, code) ((signal) << 24 | (code));
  56. #define copropbit 1<<31-2 /* bit position 2 */
  57. #define opclass 9 /* bits 21 & 22 */
  58. #define fmt 11 /* bits 19 & 20 */
  59. #define df 13 /* bits 17 & 18 */
  60. #define twobits 3 /* mask low-order 2 bits */
  61. #define fivebits 31 /* mask low-order 5 bits */
  62. #define MAX_EXCP_REG 7 /* number of excpeption registers to check */
  63. /* Exception register definitions */
  64. #define Excp_type(index) Exceptiontype(Fpu_register[index])
  65. #define Excp_instr(index) Instructionfield(Fpu_register[index])
  66. #define Clear_excp_register(index) Allexception(Fpu_register[index]) = 0
  67. #define Excp_format() \
  68. (current_ir >> ((current_ir>>opclass & twobits)==1 ? df : fmt) & twobits)
  69. /* Miscellaneous definitions */
  70. #define Fpu_sgl(index) Fpu_register[index*2]
  71. #define Fpu_dblp1(index) Fpu_register[index*2]
  72. #define Fpu_dblp2(index) Fpu_register[(index*2)+1]
  73. #define Fpu_quadp1(index) Fpu_register[index*2]
  74. #define Fpu_quadp2(index) Fpu_register[(index*2)+1]
  75. #define Fpu_quadp3(index) Fpu_register[(index*2)+2]
  76. #define Fpu_quadp4(index) Fpu_register[(index*2)+3]
  77. /* Single precision floating-point definitions */
  78. #ifndef Sgl_decrement
  79. # define Sgl_decrement(sgl_value) Sall(sgl_value)--
  80. #endif
  81. /* Double precision floating-point definitions */
  82. #ifndef Dbl_decrement
  83. # define Dbl_decrement(dbl_valuep1,dbl_valuep2) \
  84. if ((Dallp2(dbl_valuep2)--) == 0) Dallp1(dbl_valuep1)--
  85. #endif
  86. #define update_trap_counts(Fpu_register, aflags, bflags, trap_counts) { \
  87. aflags=(Fpu_register[0])>>27; /* assumes zero fill. 32 bit */ \
  88. Fpu_register[0] |= bflags; \
  89. }
  90. u_int
  91. decode_fpu(unsigned int Fpu_register[], unsigned int trap_counts[])
  92. {
  93. unsigned int current_ir, excp;
  94. int target, exception_index = 1;
  95. boolean inexact;
  96. unsigned int aflags;
  97. unsigned int bflags;
  98. unsigned int excptype;
  99. /* Keep stats on how many floating point exceptions (based on type)
  100. * that happen. Want to keep this overhead low, but still provide
  101. * some information to the customer. All exits from this routine
  102. * need to restore Fpu_register[0]
  103. */
  104. bflags=(Fpu_register[0] & 0xf8000000);
  105. Fpu_register[0] &= 0x07ffffff;
  106. /* exception_index is used to index the exception register queue. It
  107. * always points at the last register that contains a valid exception. A
  108. * zero value implies no exceptions (also the initialized value). Setting
  109. * the T-bit resets the exception_index to zero.
  110. */
  111. /*
  112. * Check for reserved-op exception. A reserved-op exception does not
  113. * set any exception registers nor does it set the T-bit. If the T-bit
  114. * is not set then a reserved-op exception occurred.
  115. *
  116. * At some point, we may want to report reserved op exceptions as
  117. * illegal instructions.
  118. */
  119. if (!Is_tbit_set()) {
  120. update_trap_counts(Fpu_register, aflags, bflags, trap_counts);
  121. return SIGNALCODE(SIGILL, ILL_COPROC);
  122. }
  123. /*
  124. * Is a coprocessor op.
  125. *
  126. * Now we need to determine what type of exception occurred.
  127. */
  128. for (exception_index=1; exception_index<=MAX_EXCP_REG; exception_index++) {
  129. current_ir = Excp_instr(exception_index);
  130. /*
  131. * On PA89: there are 5 different unimplemented exception
  132. * codes: 0x1, 0x9, 0xb, 0x3, and 0x23. PA-RISC 2.0 adds
  133. * another, 0x2b. Only these have the low order bit set.
  134. */
  135. excptype = Excp_type(exception_index);
  136. if (excptype & UNIMPLEMENTEDEXCEPTION) {
  137. /*
  138. * Clear T-bit and exception register so that
  139. * we can tell if a trap really occurs while
  140. * emulating the instruction.
  141. */
  142. Clear_tbit();
  143. Clear_excp_register(exception_index);
  144. /*
  145. * Now emulate this instruction. If a trap occurs,
  146. * fpudispatch will return a non-zero number
  147. */
  148. excp = fpudispatch(current_ir,excptype,0,Fpu_register);
  149. /* accumulate the status flags, don't lose them as in hpux */
  150. if (excp) {
  151. /*
  152. * We now need to make sure that the T-bit and the
  153. * exception register contain the correct values
  154. * before continuing.
  155. */
  156. /*
  157. * Set t-bit since it might still be needed for a
  158. * subsequent real trap (I don't understand fully -PB)
  159. */
  160. Set_tbit();
  161. /* some of the following code uses
  162. * Excp_type(exception_index) so fix that up */
  163. Set_exceptiontype_and_instr_field(excp,current_ir,
  164. Fpu_register[exception_index]);
  165. if (excp == UNIMPLEMENTEDEXCEPTION) {
  166. /*
  167. * it is really unimplemented, so restore the
  168. * TIMEX extended unimplemented exception code
  169. */
  170. excp = excptype;
  171. update_trap_counts(Fpu_register, aflags, bflags,
  172. trap_counts);
  173. return SIGNALCODE(SIGILL, ILL_COPROC);
  174. }
  175. /* some of the following code uses excptype, so
  176. * fix that up too */
  177. excptype = excp;
  178. }
  179. /* handle exceptions other than the real UNIMPLIMENTED the
  180. * same way as if the hardware had caused them */
  181. if (excp == NOEXCEPTION)
  182. /* For now use 'break', should technically be 'continue' */
  183. break;
  184. }
  185. /*
  186. * In PA89, the underflow exception has been extended to encode
  187. * additional information. The exception looks like pp01x0,
  188. * where x is 1 if inexact and pp represent the inexact bit (I)
  189. * and the round away bit (RA)
  190. */
  191. if (excptype & UNDERFLOWEXCEPTION) {
  192. /* check for underflow trap enabled */
  193. if (Is_underflowtrap_enabled()) {
  194. update_trap_counts(Fpu_register, aflags, bflags,
  195. trap_counts);
  196. return SIGNALCODE(SIGFPE, FPE_FLTUND);
  197. } else {
  198. /*
  199. * Isn't a real trap; we need to
  200. * return the default value.
  201. */
  202. target = current_ir & fivebits;
  203. #ifndef lint
  204. if (Ibit(Fpu_register[exception_index])) inexact = TRUE;
  205. else inexact = FALSE;
  206. #endif
  207. switch (Excp_format()) {
  208. case SGL:
  209. /*
  210. * If ra (round-away) is set, will
  211. * want to undo the rounding done
  212. * by the hardware.
  213. */
  214. if (Rabit(Fpu_register[exception_index]))
  215. Sgl_decrement(Fpu_sgl(target));
  216. /* now denormalize */
  217. sgl_denormalize(&Fpu_sgl(target),&inexact,Rounding_mode());
  218. break;
  219. case DBL:
  220. /*
  221. * If ra (round-away) is set, will
  222. * want to undo the rounding done
  223. * by the hardware.
  224. */
  225. if (Rabit(Fpu_register[exception_index]))
  226. Dbl_decrement(Fpu_dblp1(target),Fpu_dblp2(target));
  227. /* now denormalize */
  228. dbl_denormalize(&Fpu_dblp1(target),&Fpu_dblp2(target),
  229. &inexact,Rounding_mode());
  230. break;
  231. }
  232. if (inexact) Set_underflowflag();
  233. /*
  234. * Underflow can generate an inexact
  235. * exception. If inexact trap is enabled,
  236. * want to do an inexact trap, otherwise
  237. * set inexact flag.
  238. */
  239. if (inexact && Is_inexacttrap_enabled()) {
  240. /*
  241. * Set exception field of exception register
  242. * to inexact, parm field to zero.
  243. * Underflow bit should be cleared.
  244. */
  245. Set_exceptiontype(Fpu_register[exception_index],
  246. INEXACTEXCEPTION);
  247. Set_parmfield(Fpu_register[exception_index],0);
  248. update_trap_counts(Fpu_register, aflags, bflags,
  249. trap_counts);
  250. return SIGNALCODE(SIGFPE, FPE_FLTRES);
  251. }
  252. else {
  253. /*
  254. * Exception register needs to be cleared.
  255. * Inexact flag needs to be set if inexact.
  256. */
  257. Clear_excp_register(exception_index);
  258. if (inexact) Set_inexactflag();
  259. }
  260. }
  261. continue;
  262. }
  263. switch(Excp_type(exception_index)) {
  264. case OVERFLOWEXCEPTION:
  265. case OVERFLOWEXCEPTION | INEXACTEXCEPTION:
  266. /* check for overflow trap enabled */
  267. update_trap_counts(Fpu_register, aflags, bflags,
  268. trap_counts);
  269. if (Is_overflowtrap_enabled()) {
  270. update_trap_counts(Fpu_register, aflags, bflags,
  271. trap_counts);
  272. return SIGNALCODE(SIGFPE, FPE_FLTOVF);
  273. } else {
  274. /*
  275. * Isn't a real trap; we need to
  276. * return the default value.
  277. */
  278. target = current_ir & fivebits;
  279. switch (Excp_format()) {
  280. case SGL:
  281. Sgl_setoverflow(Fpu_sgl(target));
  282. break;
  283. case DBL:
  284. Dbl_setoverflow(Fpu_dblp1(target),Fpu_dblp2(target));
  285. break;
  286. }
  287. Set_overflowflag();
  288. /*
  289. * Overflow always generates an inexact
  290. * exception. If inexact trap is enabled,
  291. * want to do an inexact trap, otherwise
  292. * set inexact flag.
  293. */
  294. if (Is_inexacttrap_enabled()) {
  295. /*
  296. * Set exception field of exception
  297. * register to inexact. Overflow
  298. * bit should be cleared.
  299. */
  300. Set_exceptiontype(Fpu_register[exception_index],
  301. INEXACTEXCEPTION);
  302. update_trap_counts(Fpu_register, aflags, bflags,
  303. trap_counts);
  304. return SIGNALCODE(SIGFPE, FPE_FLTRES);
  305. }
  306. else {
  307. /*
  308. * Exception register needs to be cleared.
  309. * Inexact flag needs to be set.
  310. */
  311. Clear_excp_register(exception_index);
  312. Set_inexactflag();
  313. }
  314. }
  315. break;
  316. case INVALIDEXCEPTION:
  317. update_trap_counts(Fpu_register, aflags, bflags, trap_counts);
  318. return SIGNALCODE(SIGFPE, FPE_FLTINV);
  319. case DIVISIONBYZEROEXCEPTION:
  320. update_trap_counts(Fpu_register, aflags, bflags, trap_counts);
  321. return SIGNALCODE(SIGFPE, FPE_FLTDIV);
  322. case INEXACTEXCEPTION:
  323. update_trap_counts(Fpu_register, aflags, bflags, trap_counts);
  324. return SIGNALCODE(SIGFPE, FPE_FLTRES);
  325. default:
  326. update_trap_counts(Fpu_register, aflags, bflags, trap_counts);
  327. printk("%s(%d) Unknown FPU exception 0x%x\n", __FILE__,
  328. __LINE__, Excp_type(exception_index));
  329. return SIGNALCODE(SIGILL, ILL_COPROC);
  330. case NOEXCEPTION: /* no exception */
  331. /*
  332. * Clear exception register in case
  333. * other fields are non-zero.
  334. */
  335. Clear_excp_register(exception_index);
  336. break;
  337. }
  338. }
  339. /*
  340. * No real exceptions occurred.
  341. */
  342. Clear_tbit();
  343. update_trap_counts(Fpu_register, aflags, bflags, trap_counts);
  344. return(NOTRAP);
  345. }