root/drivers/FPU-emu/errors.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. Un_impl
  2. FPU_illegal
  3. emu_printall
  4. exception
  5. real_2op_NaN
  6. arith_invalid
  7. divide_by_zero
  8. set_precision_flag
  9. set_precision_flag_up
  10. set_precision_flag_down
  11. denormal_operand
  12. arith_overflow
  13. arith_underflow
  14. stack_overflow
  15. stack_underflow
  16. stack_underflow_i
  17. stack_underflow_pop

   1 /*---------------------------------------------------------------------------+
   2  |  errors.c                                                                 |
   3  |                                                                           |
   4  |  The error handling functions for wm-FPU-emu                              |
   5  |                                                                           |
   6  | Copyright (C) 1992,1993,1994                                              |
   7  |                       W. Metzenthen, 22 Parker St, Ormond, Vic 3163,      |
   8  |                       Australia.  E-mail   billm@vaxc.cc.monash.edu.au    |
   9  |                                                                           |
  10  |                                                                           |
  11  +---------------------------------------------------------------------------*/
  12 
  13 /*---------------------------------------------------------------------------+
  14  | Note:                                                                     |
  15  |    The file contains code which accesses user memory.                     |
  16  |    Emulator static data may change when user memory is accessed, due to   |
  17  |    other processes using the emulator while swapping is in progress.      |
  18  +---------------------------------------------------------------------------*/
  19 
  20 #include <linux/signal.h>
  21 
  22 #include <asm/segment.h>
  23 
  24 #include "fpu_system.h"
  25 #include "exception.h"
  26 #include "fpu_emu.h"
  27 #include "status_w.h"
  28 #include "control_w.h"
  29 #include "reg_constant.h"
  30 #include "version.h"
  31 
  32 /* */
  33 #undef PRINT_MESSAGES
  34 /* */
  35 
  36 
  37 void Un_impl(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  38 {
  39   unsigned char byte1, FPU_modrm;
  40   unsigned long address = FPU_ORIG_EIP;
  41 
  42   RE_ENTRANT_CHECK_OFF;
  43   /* No need to verify_area(), we have previously fetched these bytes. */
  44   printk("Unimplemented FPU Opcode at eip=%p : ", (void *) address);
  45   while ( 1 )
  46     {
  47       byte1 = get_fs_byte((unsigned char *) address);
  48       if ( (byte1 & 0xf8) == 0xd8 ) break;
  49       printk("[%02x]", byte1);
  50       address++;
  51     }
  52   printk("%02x ", byte1);
  53   FPU_modrm = get_fs_byte(1 + (unsigned char *) address);
  54 
  55   if (FPU_modrm >= 0300)
  56     printk("%02x (%02x+%d)\n", FPU_modrm, FPU_modrm & 0xf8, FPU_modrm & 7);
  57   else
  58     printk("/%d\n", (FPU_modrm >> 3) & 7);
  59   RE_ENTRANT_CHECK_ON;
  60 
  61   EXCEPTION(EX_Invalid);
  62 
  63 }
  64 
  65 
  66 /*
  67    Called for opcodes which are illegal and which are known to result in a
  68    SIGILL with a real 80486.
  69    */
  70 void FPU_illegal(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  71 {
  72   math_abort(FPU_info,SIGILL);
  73 }
  74 
  75 
  76 
  77 void emu_printall()
     /* [previous][next][first][last][top][bottom][index][help] */
  78 {
  79   int i;
  80   static char *tag_desc[] = { "Valid", "Zero", "ERROR", "ERROR",
  81                               "DeNorm", "Inf", "NaN", "Empty" };
  82   unsigned char byte1, FPU_modrm;
  83   unsigned long address = FPU_ORIG_EIP;
  84 
  85   RE_ENTRANT_CHECK_OFF;
  86   /* No need to verify_area(), we have previously fetched these bytes. */
  87   printk("At %p:", (void *) address);
  88 #define MAX_PRINTED_BYTES 20
  89   for ( i = 0; i < MAX_PRINTED_BYTES; i++ )
  90     {
  91       byte1 = get_fs_byte((unsigned char *) address);
  92       if ( (byte1 & 0xf8) == 0xd8 )
  93         {
  94           printk(" %02x", byte1);
  95           break;
  96         }
  97       printk(" [%02x]", byte1);
  98       address++;
  99     }
 100   if ( i == MAX_PRINTED_BYTES ) printk(" [more..]");
 101   printk("\n");
 102   FPU_modrm = get_fs_byte(1 + (unsigned char *) address);
 103   partial_status = status_word();
 104 
 105 #ifdef DEBUGGING
 106 if ( partial_status & SW_Backward )    printk("SW: backward compatibility\n");
 107 if ( partial_status & SW_C3 )          printk("SW: condition bit 3\n");
 108 if ( partial_status & SW_C2 )          printk("SW: condition bit 2\n");
 109 if ( partial_status & SW_C1 )          printk("SW: condition bit 1\n");
 110 if ( partial_status & SW_C0 )          printk("SW: condition bit 0\n");
 111 if ( partial_status & SW_Summary )     printk("SW: exception summary\n");
 112 if ( partial_status & SW_Stack_Fault ) printk("SW: stack fault\n");
 113 if ( partial_status & SW_Precision )   printk("SW: loss of precision\n");
 114 if ( partial_status & SW_Underflow )   printk("SW: underflow\n");
 115 if ( partial_status & SW_Overflow )    printk("SW: overflow\n");
 116 if ( partial_status & SW_Zero_Div )    printk("SW: divide by zero\n");
 117 if ( partial_status & SW_Denorm_Op )   printk("SW: denormalized operand\n");
 118 if ( partial_status & SW_Invalid )     printk("SW: invalid operation\n");
 119 #endif DEBUGGING
 120 
 121   if (FPU_modrm >= 0300)
 122     printk("%02x (%02x+%d)\n", FPU_modrm, FPU_modrm & 0xf8, FPU_modrm & 7);
 123   else
 124     printk("/%d, mod=%d rm=%d\n",
 125            (FPU_modrm >> 3) & 7, (FPU_modrm >> 6) & 3, FPU_modrm & 7);
 126 
 127   printk(" SW: b=%d st=%ld es=%d sf=%d cc=%d%d%d%d ef=%d%d%d%d%d%d\n",
 128          partial_status & 0x8000 ? 1 : 0,   /* busy */
 129          (partial_status & 0x3800) >> 11,   /* stack top pointer */
 130          partial_status & 0x80 ? 1 : 0,     /* Error summary status */
 131          partial_status & 0x40 ? 1 : 0,     /* Stack flag */
 132          partial_status & SW_C3?1:0, partial_status & SW_C2?1:0, /* cc */
 133          partial_status & SW_C1?1:0, partial_status & SW_C0?1:0, /* cc */
 134          partial_status & SW_Precision?1:0, partial_status & SW_Underflow?1:0,
 135          partial_status & SW_Overflow?1:0, partial_status & SW_Zero_Div?1:0,
 136          partial_status & SW_Denorm_Op?1:0, partial_status & SW_Invalid?1:0);
 137   
 138 printk(" CW: ic=%d rc=%ld%ld pc=%ld%ld iem=%d     ef=%d%d%d%d%d%d\n",
 139          control_word & 0x1000 ? 1 : 0,
 140          (control_word & 0x800) >> 11, (control_word & 0x400) >> 10,
 141          (control_word & 0x200) >> 9, (control_word & 0x100) >> 8,
 142          control_word & 0x80 ? 1 : 0,
 143          control_word & SW_Precision?1:0, control_word & SW_Underflow?1:0,
 144          control_word & SW_Overflow?1:0, control_word & SW_Zero_Div?1:0,
 145          control_word & SW_Denorm_Op?1:0, control_word & SW_Invalid?1:0);
 146 
 147   for ( i = 0; i < 8; i++ )
 148     {
 149       FPU_REG *r = &st(i);
 150       switch (r->tag)
 151         {
 152         case TW_Empty:
 153           continue;
 154           break;
 155         case TW_Zero:
 156 #if 0
 157           printk("st(%d)  %c .0000 0000 0000 0000         ",
 158                  i, r->sign ? '-' : '+');
 159           break;
 160 #endif
 161         case TW_Valid:
 162         case TW_NaN:
 163 /*      case TW_Denormal: */
 164         case TW_Infinity:
 165           printk("st(%d)  %c .%04lx %04lx %04lx %04lx e%+-6ld ", i,
 166                  r->sign ? '-' : '+',
 167                  (long)(r->sigh >> 16),
 168                  (long)(r->sigh & 0xFFFF),
 169                  (long)(r->sigl >> 16),
 170                  (long)(r->sigl & 0xFFFF),
 171                  r->exp - EXP_BIAS + 1);
 172           break;
 173         default:
 174           printk("Whoops! Error in errors.c      ");
 175           break;
 176         }
 177       printk("%s\n", tag_desc[(int) (unsigned) r->tag]);
 178     }
 179 
 180   printk("[data] %c .%04lx %04lx %04lx %04lx e%+-6ld ",
 181          FPU_loaded_data.sign ? '-' : '+',
 182          (long)(FPU_loaded_data.sigh >> 16),
 183          (long)(FPU_loaded_data.sigh & 0xFFFF),
 184          (long)(FPU_loaded_data.sigl >> 16),
 185          (long)(FPU_loaded_data.sigl & 0xFFFF),
 186          FPU_loaded_data.exp - EXP_BIAS + 1);
 187   printk("%s\n", tag_desc[(int) (unsigned) FPU_loaded_data.tag]);
 188   RE_ENTRANT_CHECK_ON;
 189 
 190 }
 191 
 192 static struct {
 193   int type;
 194   char *name;
 195 } exception_names[] = {
 196   { EX_StackOver, "stack overflow" },
 197   { EX_StackUnder, "stack underflow" },
 198   { EX_Precision, "loss of precision" },
 199   { EX_Underflow, "underflow" },
 200   { EX_Overflow, "overflow" },
 201   { EX_ZeroDiv, "divide by zero" },
 202   { EX_Denormal, "denormalized operand" },
 203   { EX_Invalid, "invalid operation" },
 204   { EX_INTERNAL, "INTERNAL BUG in "FPU_VERSION },
 205   { 0, NULL }
 206 };
 207 
 208 /*
 209  EX_INTERNAL is always given with a code which indicates where the
 210  error was detected.
 211 
 212  Internal error types:
 213        0      in load_store.c
 214        0x14   in fpu_etc.c
 215        0x1nn  in a *.c file:
 216               0x101  in reg_add_sub.c
 217               0x102  in reg_mul.c
 218               0x103  in poly_sin.c
 219               0x104  in poly_atan.c
 220               0x105  in reg_mul.c
 221               0x106  in reg_ld_str.c
 222               0x107  in fpu_trig.c
 223               0x108  in reg_compare.c
 224               0x109  in reg_compare.c
 225               0x110  in reg_add_sub.c
 226               0x111  in fpe_entry.c
 227               0x112  in fpu_trig.c
 228               0x113  in errors.c
 229               0x114  in reg_ld_str.c
 230               0x115  in fpu_trig.c
 231               0x116  in fpu_trig.c
 232               0x117  in fpu_trig.c
 233               0x118  in fpu_trig.c
 234               0x119  in fpu_trig.c
 235               0x120  in poly_atan.c
 236               0x121  in reg_compare.c
 237               0x122  in reg_compare.c
 238               0x123  in reg_compare.c
 239               0x125  in fpu_trig.c
 240               0x126  in fpu_entry.c
 241               0x127  in poly_2xm1.c
 242               0x128  in fpu_entry.c
 243               0x130  in get_address.c
 244        0x2nn  in an *.S file:
 245               0x201  in reg_u_add.S
 246               0x202  in reg_u_div.S
 247               0x203  in reg_u_div.S
 248               0x204  in reg_u_div.S
 249               0x205  in reg_u_mul.S
 250               0x206  in reg_u_sub.S
 251               0x207  in wm_sqrt.S
 252               0x208  in reg_div.S
 253               0x209  in reg_u_sub.S
 254               0x210  in reg_u_sub.S
 255               0x211  in reg_u_sub.S
 256               0x212  in reg_u_sub.S
 257               0x213  in wm_sqrt.S
 258               0x214  in wm_sqrt.S
 259               0x215  in wm_sqrt.S
 260               0x220  in reg_norm.S
 261               0x221  in reg_norm.S
 262               0x230  in reg_round.S
 263               0x231  in reg_round.S
 264               0x232  in reg_round.S
 265               0x233  in reg_round.S
 266               0x234  in reg_round.S
 267               0x235  in reg_round.S
 268               0x236  in reg_round.S
 269  */
 270 
 271 void exception(int n)
     /* [previous][next][first][last][top][bottom][index][help] */
 272 {
 273   int i, int_type;
 274 
 275   int_type = 0;         /* Needed only to stop compiler warnings */
 276   if ( n & EX_INTERNAL )
 277     {
 278       int_type = n - EX_INTERNAL;
 279       n = EX_INTERNAL;
 280       /* Set lots of exception bits! */
 281       partial_status |= (SW_Exc_Mask | SW_Summary | SW_Backward);
 282     }
 283   else
 284     {
 285       /* Extract only the bits which we use to set the status word */
 286       n &= (SW_Exc_Mask);
 287       /* Set the corresponding exception bit */
 288       partial_status |= n;
 289       /* Set summary bits iff exception isn't masked */
 290       if ( partial_status & ~control_word & CW_Exceptions )
 291         partial_status |= (SW_Summary | SW_Backward);
 292       if ( n & (SW_Stack_Fault | EX_Precision) )
 293         {
 294           if ( !(n & SW_C1) )
 295             /* This bit distinguishes over- from underflow for a stack fault,
 296                and roundup from round-down for precision loss. */
 297             partial_status &= ~SW_C1;
 298         }
 299     }
 300 
 301   RE_ENTRANT_CHECK_OFF;
 302   if ( (~control_word & n & CW_Exceptions) || (n == EX_INTERNAL) )
 303     {
 304 #ifdef PRINT_MESSAGES
 305       /* My message from the sponsor */
 306       printk(FPU_VERSION" "__DATE__" (C) W. Metzenthen.\n");
 307 #endif PRINT_MESSAGES
 308       
 309       /* Get a name string for error reporting */
 310       for (i=0; exception_names[i].type; i++)
 311         if ( (exception_names[i].type & n) == exception_names[i].type )
 312           break;
 313       
 314       if (exception_names[i].type)
 315         {
 316 #ifdef PRINT_MESSAGES
 317           printk("FP Exception: %s!\n", exception_names[i].name);
 318 #endif PRINT_MESSAGES
 319         }
 320       else
 321         printk("FPU emulator: Unknown Exception: 0x%04x!\n", n);
 322       
 323       if ( n == EX_INTERNAL )
 324         {
 325           printk("FPU emulator: Internal error type 0x%04x\n", int_type);
 326           emu_printall();
 327         }
 328 #ifdef PRINT_MESSAGES
 329       else
 330         emu_printall();
 331 #endif PRINT_MESSAGES
 332 
 333       /*
 334        * The 80486 generates an interrupt on the next non-control FPU
 335        * instruction. So we need some means of flagging it.
 336        * We use the ES (Error Summary) bit for this, assuming that
 337        * this is the way a real FPU does it (until I can check it out),
 338        * if not, then some method such as the following kludge might
 339        * be needed.
 340        */
 341 /*      regs[0].tag |= TW_FPU_Interrupt; */
 342     }
 343   RE_ENTRANT_CHECK_ON;
 344 
 345 #ifdef __DEBUG__
 346   math_abort(FPU_info,SIGFPE);
 347 #endif __DEBUG__
 348 
 349 }
 350 
 351 
 352 /* Real operation attempted on two operands, one a NaN. */
 353 /* Returns nz if the exception is unmasked */
 354 asmlinkage int real_2op_NaN(FPU_REG const *a, FPU_REG const *b, FPU_REG *dest)
     /* [previous][next][first][last][top][bottom][index][help] */
 355 {
 356   FPU_REG const *x;
 357   int signalling;
 358 
 359   /* The default result for the case of two "equal" NaNs (signs may
 360      differ) is chosen to reproduce 80486 behaviour */
 361   x = a;
 362   if (a->tag == TW_NaN)
 363     {
 364       if (b->tag == TW_NaN)
 365         {
 366           signalling = !(a->sigh & b->sigh & 0x40000000);
 367           /* find the "larger" */
 368           if ( significand(a) < significand(b) )
 369             x = b;
 370         }
 371       else
 372         {
 373           /* return the quiet version of the NaN in a */
 374           signalling = !(a->sigh & 0x40000000);
 375         }
 376     }
 377   else
 378 #ifdef PARANOID
 379     if (b->tag == TW_NaN)
 380 #endif PARANOID
 381     {
 382       signalling = !(b->sigh & 0x40000000);
 383       x = b;
 384     }
 385 #ifdef PARANOID
 386   else
 387     {
 388       signalling = 0;
 389       EXCEPTION(EX_INTERNAL|0x113);
 390       x = &CONST_QNaN;
 391     }
 392 #endif PARANOID
 393 
 394   if ( !signalling )
 395     {
 396       if ( !(x->sigh & 0x80000000) )  /* pseudo-NaN ? */
 397         x = &CONST_QNaN;
 398       reg_move(x, dest);
 399       return 0;
 400     }
 401 
 402   if ( control_word & CW_Invalid )
 403     {
 404       /* The masked response */
 405       if ( !(x->sigh & 0x80000000) )  /* pseudo-NaN ? */
 406         x = &CONST_QNaN;
 407       reg_move(x, dest);
 408       /* ensure a Quiet NaN */
 409       dest->sigh |= 0x40000000;
 410     }
 411 
 412   EXCEPTION(EX_Invalid);
 413   
 414   return !(control_word & CW_Invalid);
 415 }
 416 
 417 
 418 /* Invalid arith operation on Valid registers */
 419 /* Returns nz if the exception is unmasked */
 420 asmlinkage int arith_invalid(FPU_REG *dest)
     /* [previous][next][first][last][top][bottom][index][help] */
 421 {
 422 
 423   EXCEPTION(EX_Invalid);
 424   
 425   if ( control_word & CW_Invalid )
 426     {
 427       /* The masked response */
 428       reg_move(&CONST_QNaN, dest);
 429     }
 430   
 431   return !(control_word & CW_Invalid);
 432 
 433 }
 434 
 435 
 436 /* Divide a finite number by zero */
 437 asmlinkage int divide_by_zero(int sign, FPU_REG *dest)
     /* [previous][next][first][last][top][bottom][index][help] */
 438 {
 439 
 440   if ( control_word & CW_ZeroDiv )
 441     {
 442       /* The masked response */
 443       reg_move(&CONST_INF, dest);
 444       dest->sign = (unsigned char)sign;
 445     }
 446  
 447   EXCEPTION(EX_ZeroDiv);
 448 
 449   return !(control_word & CW_ZeroDiv);
 450 
 451 }
 452 
 453 
 454 /* This may be called often, so keep it lean */
 455 int set_precision_flag(int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 456 {
 457   if ( control_word & CW_Precision )
 458     {
 459       partial_status &= ~(SW_C1 & flags);
 460       partial_status |= flags;   /* The masked response */
 461       return 0;
 462     }
 463   else
 464     {
 465       exception(flags);
 466       return 1;
 467     }
 468 }
 469 
 470 
 471 /* This may be called often, so keep it lean */
 472 asmlinkage void set_precision_flag_up(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 473 {
 474   if ( control_word & CW_Precision )
 475     partial_status |= (SW_Precision | SW_C1);   /* The masked response */
 476   else
 477     exception(EX_Precision | SW_C1);
 478 
 479 }
 480 
 481 
 482 /* This may be called often, so keep it lean */
 483 asmlinkage void set_precision_flag_down(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 484 {
 485   if ( control_word & CW_Precision )
 486     {   /* The masked response */
 487       partial_status &= ~SW_C1;
 488       partial_status |= SW_Precision;
 489     }
 490   else
 491     exception(EX_Precision);
 492 }
 493 
 494 
 495 asmlinkage int denormal_operand(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 496 {
 497   if ( control_word & CW_Denormal )
 498     {   /* The masked response */
 499       partial_status |= SW_Denorm_Op;
 500       return 0;
 501     }
 502   else
 503     {
 504       exception(EX_Denormal);
 505       return 1;
 506     }
 507 }
 508 
 509 
 510 asmlinkage int arith_overflow(FPU_REG *dest)
     /* [previous][next][first][last][top][bottom][index][help] */
 511 {
 512 
 513   if ( control_word & CW_Overflow )
 514     {
 515       char sign;
 516       /* The masked response */
 517 /* ###### The response here depends upon the rounding mode */
 518       sign = dest->sign;
 519       reg_move(&CONST_INF, dest);
 520       dest->sign = sign;
 521     }
 522   else
 523     {
 524       /* Subtract the magic number from the exponent */
 525       dest->exp -= (3 * (1 << 13));
 526     }
 527 
 528   EXCEPTION(EX_Overflow);
 529   if ( control_word & CW_Overflow )
 530     {
 531       /* The overflow exception is masked. */
 532       /* By definition, precision is lost.
 533          The roundup bit (C1) is also set because we have
 534          "rounded" upwards to Infinity. */
 535       EXCEPTION(EX_Precision | SW_C1);
 536       return !(control_word & CW_Precision);
 537     }
 538 
 539   return !(control_word & CW_Overflow);
 540 
 541 }
 542 
 543 
 544 asmlinkage int arith_underflow(FPU_REG *dest)
     /* [previous][next][first][last][top][bottom][index][help] */
 545 {
 546 
 547   if ( control_word & CW_Underflow )
 548     {
 549       /* The masked response */
 550       if ( dest->exp <= EXP_UNDER - 63 )
 551         {
 552           reg_move(&CONST_Z, dest);
 553           partial_status &= ~SW_C1;       /* Round down. */
 554         }
 555     }
 556   else
 557     {
 558       /* Add the magic number to the exponent. */
 559       dest->exp += (3 * (1 << 13));
 560     }
 561 
 562   EXCEPTION(EX_Underflow);
 563   if ( control_word & CW_Underflow )
 564     {
 565       /* The underflow exception is masked. */
 566       EXCEPTION(EX_Precision);
 567       return !(control_word & CW_Precision);
 568     }
 569 
 570   return !(control_word & CW_Underflow);
 571 
 572 }
 573 
 574 
 575 void stack_overflow(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 576 {
 577 
 578  if ( control_word & CW_Invalid )
 579     {
 580       /* The masked response */
 581       top--;
 582       reg_move(&CONST_QNaN, FPU_st0_ptr = &st(0));
 583     }
 584 
 585   EXCEPTION(EX_StackOver);
 586 
 587   return;
 588 
 589 }
 590 
 591 
 592 void stack_underflow(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 593 {
 594 
 595  if ( control_word & CW_Invalid )
 596     {
 597       /* The masked response */
 598       reg_move(&CONST_QNaN, FPU_st0_ptr);
 599     }
 600 
 601   EXCEPTION(EX_StackUnder);
 602 
 603   return;
 604 
 605 }
 606 
 607 
 608 void stack_underflow_i(int i)
     /* [previous][next][first][last][top][bottom][index][help] */
 609 {
 610 
 611  if ( control_word & CW_Invalid )
 612     {
 613       /* The masked response */
 614       reg_move(&CONST_QNaN, &(st(i)));
 615     }
 616 
 617   EXCEPTION(EX_StackUnder);
 618 
 619   return;
 620 
 621 }
 622 
 623 
 624 void stack_underflow_pop(int i)
     /* [previous][next][first][last][top][bottom][index][help] */
 625 {
 626 
 627  if ( control_word & CW_Invalid )
 628     {
 629       /* The masked response */
 630       reg_move(&CONST_QNaN, &(st(i)));
 631       pop();
 632     }
 633 
 634   EXCEPTION(EX_StackUnder);
 635 
 636   return;
 637 
 638 }
 639 

/* [previous][next][first][last][top][bottom][index][help] */