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

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