decode_exc.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:11k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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., 675 Mass Ave, Cambridge, MA 02139, 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.     
  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. update_trap_counts(Fpu_register, aflags, bflags, trap_counts);
  319. return SIGNALCODE(SIGFPE, FPE_FLTINV);
  320.   case DIVISIONBYZEROEXCEPTION:
  321. update_trap_counts(Fpu_register, aflags, bflags, trap_counts);
  322.    return SIGNALCODE(SIGFPE, FPE_FLTDIV);
  323.   case INEXACTEXCEPTION:
  324. update_trap_counts(Fpu_register, aflags, bflags, trap_counts);
  325. return SIGNALCODE(SIGFPE, FPE_FLTRES);
  326.   default:
  327. update_trap_counts(Fpu_register, aflags, bflags, trap_counts);
  328. printk(__FILE__ "(%d) Unknown FPU exception 0x%xn",
  329. __LINE__, Excp_type(exception_index));
  330. return SIGNALCODE(SIGILL, ILL_COPROC);
  331.   case NOEXCEPTION: /* no exception */
  332. /*
  333.  * Clear exception register in case 
  334.  * other fields are non-zero.
  335.  */
  336. Clear_excp_register(exception_index);
  337. break;
  338. }
  339.     }
  340.     /*
  341.      * No real exceptions occurred.
  342.      */
  343.     Clear_tbit();
  344.     update_trap_counts(Fpu_register, aflags, bflags, trap_counts);
  345.     return(NOTRAP);
  346. }