1 /*---------------------------------------------------------------------------+
4 | The error handling functions for wm-FPU-emu |
6 | Copyright (C) 1992,1993,1994,1996 |
7 | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
8 | E-mail billm@jacobi.maths.monash.edu.au |
11 +---------------------------------------------------------------------------*/
13 /*---------------------------------------------------------------------------+
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 +---------------------------------------------------------------------------*/
20 #include <linux/signal.h>
22 #include <asm/uaccess.h>
25 #include "fpu_system.h"
26 #include "exception.h"
28 #include "control_w.h"
29 #include "reg_constant.h"
40 u_char byte1, FPU_modrm;
41 unsigned long address = FPU_ORIG_EIP;
44 /* No need to check access_ok(), we have previously fetched these bytes. */
45 printk("Unimplemented FPU Opcode at eip=%p : ", (void __user *) address);
46 if ( FPU_CS == __USER_CS )
50 FPU_get_user(byte1, (u_char __user *) address);
51 if ( (byte1 & 0xf8) == 0xd8 ) break;
52 printk("[%02x]", byte1);
55 printk("%02x ", byte1);
56 FPU_get_user(FPU_modrm, 1 + (u_char __user *) address);
58 if (FPU_modrm >= 0300)
59 printk("%02x (%02x+%d)\n", FPU_modrm, FPU_modrm & 0xf8, FPU_modrm & 7);
61 printk("/%d\n", (FPU_modrm >> 3) & 7);
65 printk("cs selector = %04x\n", FPU_CS);
70 EXCEPTION(EX_Invalid);
77 Called for opcodes which are illegal and which are known to result in a
78 SIGILL with a real 80486.
80 void FPU_illegal(void)
82 math_abort(FPU_info,SIGILL);
87 void FPU_printall(void)
90 static const char *tag_desc[] = { "Valid", "Zero", "ERROR", "Empty",
91 "DeNorm", "Inf", "NaN" };
92 u_char byte1, FPU_modrm;
93 unsigned long address = FPU_ORIG_EIP;
96 /* No need to check access_ok(), we have previously fetched these bytes. */
97 printk("At %p:", (void *) address);
98 if ( FPU_CS == __USER_CS )
100 #define MAX_PRINTED_BYTES 20
101 for ( i = 0; i < MAX_PRINTED_BYTES; i++ )
103 FPU_get_user(byte1, (u_char __user *) address);
104 if ( (byte1 & 0xf8) == 0xd8 )
106 printk(" %02x", byte1);
109 printk(" [%02x]", byte1);
112 if ( i == MAX_PRINTED_BYTES )
113 printk(" [more..]\n");
116 FPU_get_user(FPU_modrm, 1 + (u_char __user *) address);
118 if (FPU_modrm >= 0300)
119 printk(" %02x (%02x+%d)\n", FPU_modrm, FPU_modrm & 0xf8, FPU_modrm & 7);
121 printk(" /%d, mod=%d rm=%d\n",
122 (FPU_modrm >> 3) & 7, (FPU_modrm >> 6) & 3, FPU_modrm & 7);
127 printk("%04x\n", FPU_CS);
130 partial_status = status_word();
133 if ( partial_status & SW_Backward ) printk("SW: backward compatibility\n");
134 if ( partial_status & SW_C3 ) printk("SW: condition bit 3\n");
135 if ( partial_status & SW_C2 ) printk("SW: condition bit 2\n");
136 if ( partial_status & SW_C1 ) printk("SW: condition bit 1\n");
137 if ( partial_status & SW_C0 ) printk("SW: condition bit 0\n");
138 if ( partial_status & SW_Summary ) printk("SW: exception summary\n");
139 if ( partial_status & SW_Stack_Fault ) printk("SW: stack fault\n");
140 if ( partial_status & SW_Precision ) printk("SW: loss of precision\n");
141 if ( partial_status & SW_Underflow ) printk("SW: underflow\n");
142 if ( partial_status & SW_Overflow ) printk("SW: overflow\n");
143 if ( partial_status & SW_Zero_Div ) printk("SW: divide by zero\n");
144 if ( partial_status & SW_Denorm_Op ) printk("SW: denormalized operand\n");
145 if ( partial_status & SW_Invalid ) printk("SW: invalid operation\n");
146 #endif /* DEBUGGING */
148 printk(" SW: b=%d st=%ld es=%d sf=%d cc=%d%d%d%d ef=%d%d%d%d%d%d\n",
149 partial_status & 0x8000 ? 1 : 0, /* busy */
150 (partial_status & 0x3800) >> 11, /* stack top pointer */
151 partial_status & 0x80 ? 1 : 0, /* Error summary status */
152 partial_status & 0x40 ? 1 : 0, /* Stack flag */
153 partial_status & SW_C3?1:0, partial_status & SW_C2?1:0, /* cc */
154 partial_status & SW_C1?1:0, partial_status & SW_C0?1:0, /* cc */
155 partial_status & SW_Precision?1:0, partial_status & SW_Underflow?1:0,
156 partial_status & SW_Overflow?1:0, partial_status & SW_Zero_Div?1:0,
157 partial_status & SW_Denorm_Op?1:0, partial_status & SW_Invalid?1:0);
159 printk(" CW: ic=%d rc=%ld%ld pc=%ld%ld iem=%d ef=%d%d%d%d%d%d\n",
160 control_word & 0x1000 ? 1 : 0,
161 (control_word & 0x800) >> 11, (control_word & 0x400) >> 10,
162 (control_word & 0x200) >> 9, (control_word & 0x100) >> 8,
163 control_word & 0x80 ? 1 : 0,
164 control_word & SW_Precision?1:0, control_word & SW_Underflow?1:0,
165 control_word & SW_Overflow?1:0, control_word & SW_Zero_Div?1:0,
166 control_word & SW_Denorm_Op?1:0, control_word & SW_Invalid?1:0);
168 for ( i = 0; i < 8; i++ )
171 u_char tagi = FPU_gettagi(i);
179 tagi = FPU_Special(r);
181 printk("st(%d) %c .%04lx %04lx %04lx %04lx e%+-6d ", i,
182 getsign(r) ? '-' : '+',
183 (long)(r->sigh >> 16),
184 (long)(r->sigh & 0xFFFF),
185 (long)(r->sigl >> 16),
186 (long)(r->sigl & 0xFFFF),
187 exponent(r) - EXP_BIAS + 1);
190 printk("Whoops! Error in errors.c: tag%d is %d ", i, tagi);
194 printk("%s\n", tag_desc[(int) (unsigned) tagi]);
204 } exception_names[] = {
205 { EX_StackOver, "stack overflow" },
206 { EX_StackUnder, "stack underflow" },
207 { EX_Precision, "loss of precision" },
208 { EX_Underflow, "underflow" },
209 { EX_Overflow, "overflow" },
210 { EX_ZeroDiv, "divide by zero" },
211 { EX_Denormal, "denormalized operand" },
212 { EX_Invalid, "invalid operation" },
213 { EX_INTERNAL, "INTERNAL BUG in "FPU_VERSION },
218 EX_INTERNAL is always given with a code which indicates where the
221 Internal error types:
224 0x101 in reg_add_sub.c
229 0x108 in reg_compare.c
230 0x109 in reg_compare.c
231 0x110 in reg_add_sub.c
241 0x121 in reg_compare.c
242 0x122 in reg_compare.c
243 0x123 in reg_compare.c
249 0x130 in get_address.c
250 0x131 in get_address.c
251 0x132 in get_address.c
252 0x133 in get_address.c
253 0x140 in load_store.c
254 0x141 in load_store.c
257 0x160 in reg_ld_str.c
258 0x161 in reg_ld_str.c
259 0x162 in reg_ld_str.c
260 0x163 in reg_ld_str.c
261 0x164 in reg_ld_str.c
265 0x180 in reg_convert.c
266 0x2nn in an *.S file:
296 asmlinkage void FPU_exception(int n)
300 int_type = 0; /* Needed only to stop compiler warnings */
301 if ( n & EX_INTERNAL )
303 int_type = n - EX_INTERNAL;
305 /* Set lots of exception bits! */
306 partial_status |= (SW_Exc_Mask | SW_Summary | SW_Backward);
310 /* Extract only the bits which we use to set the status word */
312 /* Set the corresponding exception bit */
314 /* Set summary bits iff exception isn't masked */
315 if ( partial_status & ~control_word & CW_Exceptions )
316 partial_status |= (SW_Summary | SW_Backward);
317 if ( n & (SW_Stack_Fault | EX_Precision) )
320 /* This bit distinguishes over- from underflow for a stack fault,
321 and roundup from round-down for precision loss. */
322 partial_status &= ~SW_C1;
326 RE_ENTRANT_CHECK_OFF;
327 if ( (~control_word & n & CW_Exceptions) || (n == EX_INTERNAL) )
329 #ifdef PRINT_MESSAGES
330 /* My message from the sponsor */
331 printk(FPU_VERSION" "__DATE__" (C) W. Metzenthen.\n");
332 #endif /* PRINT_MESSAGES */
334 /* Get a name string for error reporting */
335 for (i=0; exception_names[i].type; i++)
336 if ( (exception_names[i].type & n) == exception_names[i].type )
339 if (exception_names[i].type)
341 #ifdef PRINT_MESSAGES
342 printk("FP Exception: %s!\n", exception_names[i].name);
343 #endif /* PRINT_MESSAGES */
346 printk("FPU emulator: Unknown Exception: 0x%04x!\n", n);
348 if ( n == EX_INTERNAL )
350 printk("FPU emulator: Internal error type 0x%04x\n", int_type);
353 #ifdef PRINT_MESSAGES
356 #endif /* PRINT_MESSAGES */
359 * The 80486 generates an interrupt on the next non-control FPU
360 * instruction. So we need some means of flagging it.
361 * We use the ES (Error Summary) bit for this.
367 math_abort(FPU_info,SIGFPE);
368 #endif /* __DEBUG__ */
373 /* Real operation attempted on a NaN. */
374 /* Returns < 0 if the exception is unmasked */
375 int real_1op_NaN(FPU_REG *a)
377 int signalling, isNaN;
379 isNaN = (exponent(a) == EXP_OVER) && (a->sigh & 0x80000000);
381 /* The default result for the case of two "equal" NaNs (signs may
382 differ) is chosen to reproduce 80486 behaviour */
383 signalling = isNaN && !(a->sigh & 0x40000000);
387 if ( !isNaN ) /* pseudo-NaN, or other unsupported? */
389 if ( control_word & CW_Invalid )
391 /* Masked response */
392 reg_copy(&CONST_QNaN, a);
394 EXCEPTION(EX_Invalid);
395 return (!(control_word & CW_Invalid) ? FPU_Exception : 0) | TAG_Special;
400 if ( control_word & CW_Invalid )
402 /* The masked response */
403 if ( !(a->sigh & 0x80000000) ) /* pseudo-NaN ? */
405 reg_copy(&CONST_QNaN, a);
407 /* ensure a Quiet NaN */
408 a->sigh |= 0x40000000;
411 EXCEPTION(EX_Invalid);
413 return (!(control_word & CW_Invalid) ? FPU_Exception : 0) | TAG_Special;
417 /* Real operation attempted on two operands, one a NaN. */
418 /* Returns < 0 if the exception is unmasked */
419 int real_2op_NaN(FPU_REG const *b, u_char tagb,
421 FPU_REG const *defaultNaN)
423 FPU_REG *dest = &st(deststnr);
424 FPU_REG const *a = dest;
425 u_char taga = FPU_gettagi(deststnr);
427 int signalling, unsupported;
429 if ( taga == TAG_Special )
430 taga = FPU_Special(a);
431 if ( tagb == TAG_Special )
432 tagb = FPU_Special(b);
434 /* TW_NaN is also used for unsupported data types. */
435 unsupported = ((taga == TW_NaN)
436 && !((exponent(a) == EXP_OVER) && (a->sigh & 0x80000000)))
438 && !((exponent(b) == EXP_OVER) && (b->sigh & 0x80000000)));
441 if ( control_word & CW_Invalid )
443 /* Masked response */
444 FPU_copy_to_regi(&CONST_QNaN, TAG_Special, deststnr);
446 EXCEPTION(EX_Invalid);
447 return (!(control_word & CW_Invalid) ? FPU_Exception : 0) | TAG_Special;
455 signalling = !(a->sigh & b->sigh & 0x40000000);
456 if ( significand(b) > significand(a) )
458 else if ( significand(b) == significand(a) )
460 /* The default result for the case of two "equal" NaNs (signs may
461 differ) is chosen to reproduce 80486 behaviour */
467 /* return the quiet version of the NaN in a */
468 signalling = !(a->sigh & 0x40000000);
474 #endif /* PARANOID */
476 signalling = !(b->sigh & 0x40000000);
483 EXCEPTION(EX_INTERNAL|0x113);
486 #endif /* PARANOID */
488 if ( (!signalling) || (control_word & CW_Invalid) )
493 if ( !(x->sigh & 0x80000000) ) /* pseudo-NaN ? */
496 FPU_copy_to_regi(x, TAG_Special, deststnr);
501 /* ensure a Quiet NaN */
502 dest->sigh |= 0x40000000;
505 EXCEPTION(EX_Invalid);
507 return (!(control_word & CW_Invalid) ? FPU_Exception : 0) | TAG_Special;
511 /* Invalid arith operation on Valid registers */
512 /* Returns < 0 if the exception is unmasked */
513 asmlinkage int arith_invalid(int deststnr)
516 EXCEPTION(EX_Invalid);
518 if ( control_word & CW_Invalid )
520 /* The masked response */
521 FPU_copy_to_regi(&CONST_QNaN, TAG_Special, deststnr);
524 return (!(control_word & CW_Invalid) ? FPU_Exception : 0) | TAG_Valid;
529 /* Divide a finite number by zero */
530 asmlinkage int FPU_divide_by_zero(int deststnr, u_char sign)
532 FPU_REG *dest = &st(deststnr);
535 if ( control_word & CW_ZeroDiv )
537 /* The masked response */
538 FPU_copy_to_regi(&CONST_INF, TAG_Special, deststnr);
543 EXCEPTION(EX_ZeroDiv);
545 return (!(control_word & CW_ZeroDiv) ? FPU_Exception : 0) | tag;
550 /* This may be called often, so keep it lean */
551 int set_precision_flag(int flags)
553 if ( control_word & CW_Precision )
555 partial_status &= ~(SW_C1 & flags);
556 partial_status |= flags; /* The masked response */
567 /* This may be called often, so keep it lean */
568 asmlinkage void set_precision_flag_up(void)
570 if ( control_word & CW_Precision )
571 partial_status |= (SW_Precision | SW_C1); /* The masked response */
573 EXCEPTION(EX_Precision | SW_C1);
577 /* This may be called often, so keep it lean */
578 asmlinkage void set_precision_flag_down(void)
580 if ( control_word & CW_Precision )
581 { /* The masked response */
582 partial_status &= ~SW_C1;
583 partial_status |= SW_Precision;
586 EXCEPTION(EX_Precision);
590 asmlinkage int denormal_operand(void)
592 if ( control_word & CW_Denormal )
593 { /* The masked response */
594 partial_status |= SW_Denorm_Op;
599 EXCEPTION(EX_Denormal);
600 return TAG_Special | FPU_Exception;
605 asmlinkage int arith_overflow(FPU_REG *dest)
609 if ( control_word & CW_Overflow )
611 /* The masked response */
612 /* ###### The response here depends upon the rounding mode */
613 reg_copy(&CONST_INF, dest);
618 /* Subtract the magic number from the exponent */
619 addexponent(dest, (-3 * (1 << 13)));
622 EXCEPTION(EX_Overflow);
623 if ( control_word & CW_Overflow )
625 /* The overflow exception is masked. */
626 /* By definition, precision is lost.
627 The roundup bit (C1) is also set because we have
628 "rounded" upwards to Infinity. */
629 EXCEPTION(EX_Precision | SW_C1);
638 asmlinkage int arith_underflow(FPU_REG *dest)
642 if ( control_word & CW_Underflow )
644 /* The masked response */
645 if ( exponent16(dest) <= EXP_UNDER - 63 )
647 reg_copy(&CONST_Z, dest);
648 partial_status &= ~SW_C1; /* Round down. */
658 /* Add the magic number to the exponent. */
659 addexponent(dest, (3 * (1 << 13)) + EXTENDED_Ebias);
662 EXCEPTION(EX_Underflow);
663 if ( control_word & CW_Underflow )
665 /* The underflow exception is masked. */
666 EXCEPTION(EX_Precision);
675 void FPU_stack_overflow(void)
678 if ( control_word & CW_Invalid )
680 /* The masked response */
682 FPU_copy_to_reg0(&CONST_QNaN, TAG_Special);
685 EXCEPTION(EX_StackOver);
692 void FPU_stack_underflow(void)
695 if ( control_word & CW_Invalid )
697 /* The masked response */
698 FPU_copy_to_reg0(&CONST_QNaN, TAG_Special);
701 EXCEPTION(EX_StackUnder);
708 void FPU_stack_underflow_i(int i)
711 if ( control_word & CW_Invalid )
713 /* The masked response */
714 FPU_copy_to_regi(&CONST_QNaN, TAG_Special, i);
717 EXCEPTION(EX_StackUnder);
724 void FPU_stack_underflow_pop(int i)
727 if ( control_word & CW_Invalid )
729 /* The masked response */
730 FPU_copy_to_regi(&CONST_QNaN, TAG_Special, i);
734 EXCEPTION(EX_StackUnder);