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

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