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. emu_printall
  3. exception
  4. real_2op_NaN
  5. arith_invalid
  6. divide_by_zero
  7. set_precision_flag
  8. set_precision_flag_up
  9. set_precision_flag_down
  10. denormal_operand
  11. arith_overflow
  12. arith_underflow
  13. stack_overflow
  14. stack_underflow
  15. stack_underflow_i
  16. stack_underflow_pop

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

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