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
 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               0x220  in reg_norm.S
 254               0x221  in reg_norm.S
 255               0x230  in reg_round.S
 256               0x231  in reg_round.S
 257               0x232  in reg_round.S
 258               0x233  in reg_round.S
 259               0x234  in reg_round.S
 260               0x235  in reg_round.S
 261  */
 262 
 263 void exception(int n)
     /* [previous][next][first][last][top][bottom][index][help] */
 264 {
 265   int i, int_type;
 266 
 267   int_type = 0;         /* Needed only to stop compiler warnings */
 268   if ( n & EX_INTERNAL )
 269     {
 270       int_type = n - EX_INTERNAL;
 271       n = EX_INTERNAL;
 272       /* Set lots of exception bits! */
 273       partial_status |= (SW_Exc_Mask | SW_Summary | SW_Backward);
 274     }
 275   else
 276     {
 277       /* Extract only the bits which we use to set the status word */
 278       n &= (SW_Exc_Mask);
 279       /* Set the corresponding exception bit */
 280       partial_status |= n;
 281       /* Set summary bits iff exception isn't masked */
 282       if ( partial_status & ~control_word & CW_Exceptions )
 283         partial_status |= (SW_Summary | SW_Backward);
 284       if ( n & (SW_Stack_Fault | EX_Precision) )
 285         {
 286           if ( !(n & SW_C1) )
 287             /* This bit distinguishes over- from underflow for a stack fault,
 288                and roundup from round-down for precision loss. */
 289             partial_status &= ~SW_C1;
 290         }
 291     }
 292 
 293   RE_ENTRANT_CHECK_OFF;
 294   if ( (~control_word & n & CW_Exceptions) || (n == EX_INTERNAL) )
 295     {
 296 #ifdef PRINT_MESSAGES
 297       /* My message from the sponsor */
 298       printk(FPU_VERSION" "__DATE__" (C) W. Metzenthen.\n");
 299 #endif PRINT_MESSAGES
 300       
 301       /* Get a name string for error reporting */
 302       for (i=0; exception_names[i].type; i++)
 303         if ( (exception_names[i].type & n) == exception_names[i].type )
 304           break;
 305       
 306       if (exception_names[i].type)
 307         {
 308 #ifdef PRINT_MESSAGES
 309           printk("FP Exception: %s!\n", exception_names[i].name);
 310 #endif PRINT_MESSAGES
 311         }
 312       else
 313         printk("FP emulator: Unknown Exception: 0x%04x!\n", n);
 314       
 315       if ( n == EX_INTERNAL )
 316         {
 317           printk("FP emulator: Internal error type 0x%04x\n", int_type);
 318           emu_printall();
 319         }
 320 #ifdef PRINT_MESSAGES
 321       else
 322         emu_printall();
 323 #endif PRINT_MESSAGES
 324 
 325       /*
 326        * The 80486 generates an interrupt on the next non-control FPU
 327        * instruction. So we need some means of flagging it.
 328        * We use the ES (Error Summary) bit for this, assuming that
 329        * this is the way a real FPU does it (until I can check it out),
 330        * if not, then some method such as the following kludge might
 331        * be needed.
 332        */
 333 /*      regs[0].tag |= TW_FPU_Interrupt; */
 334     }
 335   RE_ENTRANT_CHECK_ON;
 336 
 337 #ifdef __DEBUG__
 338   math_abort(FPU_info,SIGFPE);
 339 #endif __DEBUG__
 340 
 341 }
 342 
 343 
 344 /* Real operation attempted on two operands, one a NaN. */
 345 /* Returns nz if the exception is unmasked */
 346 asmlinkage int real_2op_NaN(FPU_REG const *a, FPU_REG const *b, FPU_REG *dest)
     /* [previous][next][first][last][top][bottom][index][help] */
 347 {
 348   FPU_REG const *x;
 349   int signalling;
 350 
 351   /* The default result for the case of two "equal" NaNs (signs may
 352      differ) is chosen to reproduce 80486 behaviour */
 353   x = a;
 354   if (a->tag == TW_NaN)
 355     {
 356       if (b->tag == TW_NaN)
 357         {
 358           signalling = !(a->sigh & b->sigh & 0x40000000);
 359           /* find the "larger" */
 360           if ( significand(a) < significand(b) )
 361             x = b;
 362         }
 363       else
 364         {
 365           /* return the quiet version of the NaN in a */
 366           signalling = !(a->sigh & 0x40000000);
 367         }
 368     }
 369   else
 370 #ifdef PARANOID
 371     if (b->tag == TW_NaN)
 372 #endif PARANOID
 373     {
 374       signalling = !(b->sigh & 0x40000000);
 375       x = b;
 376     }
 377 #ifdef PARANOID
 378   else
 379     {
 380       signalling = 0;
 381       EXCEPTION(EX_INTERNAL|0x113);
 382       x = &CONST_QNaN;
 383     }
 384 #endif PARANOID
 385 
 386   if ( !signalling )
 387     {
 388       if ( !(x->sigh & 0x80000000) )  /* pseudo-NaN ? */
 389         x = &CONST_QNaN;
 390       reg_move(x, dest);
 391       return 0;
 392     }
 393 
 394   if ( control_word & CW_Invalid )
 395     {
 396       /* The masked response */
 397       if ( !(x->sigh & 0x80000000) )  /* pseudo-NaN ? */
 398         x = &CONST_QNaN;
 399       reg_move(x, dest);
 400       /* ensure a Quiet NaN */
 401       dest->sigh |= 0x40000000;
 402     }
 403 
 404   EXCEPTION(EX_Invalid);
 405   
 406   return !(control_word & CW_Invalid);
 407 }
 408 
 409 
 410 /* Invalid arith operation on Valid registers */
 411 /* Returns nz if the exception is unmasked */
 412 asmlinkage int arith_invalid(FPU_REG *dest)
     /* [previous][next][first][last][top][bottom][index][help] */
 413 {
 414 
 415   EXCEPTION(EX_Invalid);
 416   
 417   if ( control_word & CW_Invalid )
 418     {
 419       /* The masked response */
 420       reg_move(&CONST_QNaN, dest);
 421     }
 422   
 423   return !(control_word & CW_Invalid);
 424 
 425 }
 426 
 427 
 428 /* Divide a finite number by zero */
 429 asmlinkage int divide_by_zero(int sign, FPU_REG *dest)
     /* [previous][next][first][last][top][bottom][index][help] */
 430 {
 431 
 432   if ( control_word & CW_ZeroDiv )
 433     {
 434       /* The masked response */
 435       reg_move(&CONST_INF, dest);
 436       dest->sign = (unsigned char)sign;
 437     }
 438  
 439   EXCEPTION(EX_ZeroDiv);
 440 
 441   return !(control_word & CW_ZeroDiv);
 442 
 443 }
 444 
 445 
 446 /* This may be called often, so keep it lean */
 447 int set_precision_flag(int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 448 {
 449   if ( control_word & CW_Precision )
 450     {
 451       partial_status &= ~(SW_C1 & flags);
 452       partial_status |= flags;   /* The masked response */
 453       return 0;
 454     }
 455   else
 456     {
 457       exception(flags);
 458       return 1;
 459     }
 460 }
 461 
 462 
 463 /* This may be called often, so keep it lean */
 464 asmlinkage void set_precision_flag_up(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 465 {
 466   if ( control_word & CW_Precision )
 467     partial_status |= (SW_Precision | SW_C1);   /* The masked response */
 468   else
 469     exception(EX_Precision | SW_C1);
 470 
 471 }
 472 
 473 
 474 /* This may be called often, so keep it lean */
 475 asmlinkage void set_precision_flag_down(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 476 {
 477   if ( control_word & CW_Precision )
 478     {   /* The masked response */
 479       partial_status &= ~SW_C1;
 480       partial_status |= SW_Precision;
 481     }
 482   else
 483     exception(EX_Precision);
 484 }
 485 
 486 
 487 asmlinkage int denormal_operand(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 488 {
 489   if ( control_word & CW_Denormal )
 490     {   /* The masked response */
 491       partial_status |= SW_Denorm_Op;
 492       return 0;
 493     }
 494   else
 495     {
 496       exception(EX_Denormal);
 497       return 1;
 498     }
 499 }
 500 
 501 
 502 asmlinkage int arith_overflow(FPU_REG *dest)
     /* [previous][next][first][last][top][bottom][index][help] */
 503 {
 504 
 505   if ( control_word & CW_Overflow )
 506     {
 507       char sign;
 508       /* The masked response */
 509 /* ###### The response here depends upon the rounding mode */
 510       sign = dest->sign;
 511       reg_move(&CONST_INF, dest);
 512       dest->sign = sign;
 513     }
 514   else
 515     {
 516       /* Subtract the magic number from the exponent */
 517       dest->exp -= (3 * (1 << 13));
 518     }
 519 
 520   EXCEPTION(EX_Overflow);
 521   if ( control_word & CW_Overflow )
 522     {
 523       /* The overflow exception is masked. */
 524       /* By definition, precision is lost.
 525          The roundup bit (C1) is also set because we have
 526          "rounded" upwards to Infinity. */
 527       EXCEPTION(EX_Precision | SW_C1);
 528       return !(control_word & CW_Precision);
 529     }
 530 
 531   return !(control_word & CW_Overflow);
 532 
 533 }
 534 
 535 
 536 asmlinkage int arith_underflow(FPU_REG *dest)
     /* [previous][next][first][last][top][bottom][index][help] */
 537 {
 538 
 539   if ( control_word & CW_Underflow )
 540     {
 541       /* The masked response */
 542       if ( dest->exp <= EXP_UNDER - 63 )
 543         {
 544           reg_move(&CONST_Z, dest);
 545           partial_status &= ~SW_C1;       /* Round down. */
 546         }
 547     }
 548   else
 549     {
 550       /* Add the magic number to the exponent. */
 551       dest->exp += (3 * (1 << 13));
 552     }
 553 
 554   EXCEPTION(EX_Underflow);
 555   if ( control_word & CW_Underflow )
 556     {
 557       /* The underflow exception is masked. */
 558       EXCEPTION(EX_Precision);
 559       return !(control_word & CW_Precision);
 560     }
 561 
 562   return !(control_word & CW_Underflow);
 563 
 564 }
 565 
 566 
 567 void stack_overflow(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 568 {
 569 
 570  if ( control_word & CW_Invalid )
 571     {
 572       /* The masked response */
 573       top--;
 574       reg_move(&CONST_QNaN, FPU_st0_ptr = &st(0));
 575     }
 576 
 577   EXCEPTION(EX_StackOver);
 578 
 579   return;
 580 
 581 }
 582 
 583 
 584 void stack_underflow(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 585 {
 586 
 587  if ( control_word & CW_Invalid )
 588     {
 589       /* The masked response */
 590       reg_move(&CONST_QNaN, FPU_st0_ptr);
 591     }
 592 
 593   EXCEPTION(EX_StackUnder);
 594 
 595   return;
 596 
 597 }
 598 
 599 
 600 void stack_underflow_i(int i)
     /* [previous][next][first][last][top][bottom][index][help] */
 601 {
 602 
 603  if ( control_word & CW_Invalid )
 604     {
 605       /* The masked response */
 606       reg_move(&CONST_QNaN, &(st(i)));
 607     }
 608 
 609   EXCEPTION(EX_StackUnder);
 610 
 611   return;
 612 
 613 }
 614 
 615 
 616 void stack_underflow_pop(int i)
     /* [previous][next][first][last][top][bottom][index][help] */
 617 {
 618 
 619  if ( control_word & CW_Invalid )
 620     {
 621       /* The masked response */
 622       reg_move(&CONST_QNaN, &(st(i)));
 623       pop();
 624     }
 625 
 626   EXCEPTION(EX_StackUnder);
 627 
 628   return;
 629 
 630 }
 631 

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