decode_exc.c 11 KB

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