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

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