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"
39 u_char byte1
, FPU_modrm
;
40 unsigned long address
= FPU_ORIG_EIP
;
43 /* No need to verify_area(), we have previously fetched these bytes. */
44 printk("Unimplemented FPU Opcode at eip=%p : ", (void *) address
);
45 if ( FPU_CS
== __USER_CS
)
49 FPU_get_user(byte1
, (u_char
*) address
);
50 if ( (byte1
& 0xf8) == 0xd8 ) break;
51 printk("[%02x]", byte1
);
54 printk("%02x ", byte1
);
55 FPU_get_user(FPU_modrm
, 1 + (u_char
*) address
);
57 if (FPU_modrm
>= 0300)
58 printk("%02x (%02x+%d)\n", FPU_modrm
, FPU_modrm
& 0xf8, FPU_modrm
& 7);
60 printk("/%d\n", (FPU_modrm
>> 3) & 7);
64 printk("cs selector = %04x\n", FPU_CS
);
69 EXCEPTION(EX_Invalid
);
75 Called for opcodes which are illegal and which are known to result in a
76 SIGILL with a real 80486.
78 void FPU_illegal(void)
80 math_abort(FPU_info
,SIGILL
);
85 void FPU_printall(void)
88 static const char *tag_desc
[] = { "Valid", "Zero", "ERROR", "Empty",
89 "DeNorm", "Inf", "NaN" };
90 u_char byte1
, FPU_modrm
;
91 unsigned long address
= FPU_ORIG_EIP
;
94 /* No need to verify_area(), we have previously fetched these bytes. */
95 printk("At %p:", (void *) address
);
96 if ( FPU_CS
== __USER_CS
)
98 #define MAX_PRINTED_BYTES 20
99 for ( i
= 0; i
< MAX_PRINTED_BYTES
; i
++ )
101 FPU_get_user(byte1
, (u_char
*) address
);
102 if ( (byte1
& 0xf8) == 0xd8 )
104 printk(" %02x", byte1
);
107 printk(" [%02x]", byte1
);
110 if ( i
== MAX_PRINTED_BYTES
)
111 printk(" [more..]\n");
114 FPU_get_user(FPU_modrm
, 1 + (u_char
*) address
);
116 if (FPU_modrm
>= 0300)
117 printk(" %02x (%02x+%d)\n", FPU_modrm
, FPU_modrm
& 0xf8, FPU_modrm
& 7);
119 printk(" /%d, mod=%d rm=%d\n",
120 (FPU_modrm
>> 3) & 7, (FPU_modrm
>> 6) & 3, FPU_modrm
& 7);
125 printk("%04x\n", FPU_CS
);
128 partial_status
= status_word();
131 if ( partial_status
& SW_Backward
) printk("SW: backward compatibility\n");
132 if ( partial_status
& SW_C3
) printk("SW: condition bit 3\n");
133 if ( partial_status
& SW_C2
) printk("SW: condition bit 2\n");
134 if ( partial_status
& SW_C1
) printk("SW: condition bit 1\n");
135 if ( partial_status
& SW_C0
) printk("SW: condition bit 0\n");
136 if ( partial_status
& SW_Summary
) printk("SW: exception summary\n");
137 if ( partial_status
& SW_Stack_Fault
) printk("SW: stack fault\n");
138 if ( partial_status
& SW_Precision
) printk("SW: loss of precision\n");
139 if ( partial_status
& SW_Underflow
) printk("SW: underflow\n");
140 if ( partial_status
& SW_Overflow
) printk("SW: overflow\n");
141 if ( partial_status
& SW_Zero_Div
) printk("SW: divide by zero\n");
142 if ( partial_status
& SW_Denorm_Op
) printk("SW: denormalized operand\n");
143 if ( partial_status
& SW_Invalid
) printk("SW: invalid operation\n");
146 printk(" SW: b=%d st=%ld es=%d sf=%d cc=%d%d%d%d ef=%d%d%d%d%d%d\n",
147 partial_status
& 0x8000 ? 1 : 0, /* busy */
148 (partial_status
& 0x3800) >> 11, /* stack top pointer */
149 partial_status
& 0x80 ? 1 : 0, /* Error summary status */
150 partial_status
& 0x40 ? 1 : 0, /* Stack flag */
151 partial_status
& SW_C3
?1:0, partial_status
& SW_C2
?1:0, /* cc */
152 partial_status
& SW_C1
?1:0, partial_status
& SW_C0
?1:0, /* cc */
153 partial_status
& SW_Precision
?1:0, partial_status
& SW_Underflow
?1:0,
154 partial_status
& SW_Overflow
?1:0, partial_status
& SW_Zero_Div
?1:0,
155 partial_status
& SW_Denorm_Op
?1:0, partial_status
& SW_Invalid
?1:0);
157 printk(" CW: ic=%d rc=%ld%ld pc=%ld%ld iem=%d ef=%d%d%d%d%d%d\n",
158 control_word
& 0x1000 ? 1 : 0,
159 (control_word
& 0x800) >> 11, (control_word
& 0x400) >> 10,
160 (control_word
& 0x200) >> 9, (control_word
& 0x100) >> 8,
161 control_word
& 0x80 ? 1 : 0,
162 control_word
& SW_Precision
?1:0, control_word
& SW_Underflow
?1:0,
163 control_word
& SW_Overflow
?1:0, control_word
& SW_Zero_Div
?1:0,
164 control_word
& SW_Denorm_Op
?1:0, control_word
& SW_Invalid
?1:0);
166 for ( i
= 0; i
< 8; i
++ )
169 u_char tagi
= FPU_gettagi(i
);
177 tagi
= FPU_Special(r
);
179 printk("st(%d) %c .%04lx %04lx %04lx %04lx e%+-6d ", i
,
180 getsign(r
) ? '-' : '+',
181 (long)(r
->sigh
>> 16),
182 (long)(r
->sigh
& 0xFFFF),
183 (long)(r
->sigl
>> 16),
184 (long)(r
->sigl
& 0xFFFF),
185 exponent(r
) - EXP_BIAS
+ 1);
188 printk("Whoops! Error in errors.c: tag%d is %d ", i
, tagi
);
192 printk("%s\n", tag_desc
[(int) (unsigned) tagi
]);
202 } exception_names
[] = {
203 { EX_StackOver
, "stack overflow" },
204 { EX_StackUnder
, "stack underflow" },
205 { EX_Precision
, "loss of precision" },
206 { EX_Underflow
, "underflow" },
207 { EX_Overflow
, "overflow" },
208 { EX_ZeroDiv
, "divide by zero" },
209 { EX_Denormal
, "denormalized operand" },
210 { EX_Invalid
, "invalid operation" },
211 { EX_INTERNAL
, "INTERNAL BUG in "FPU_VERSION
},
216 EX_INTERNAL is always given with a code which indicates where the
219 Internal error types:
222 0x101 in reg_add_sub.c
227 0x108 in reg_compare.c
228 0x109 in reg_compare.c
229 0x110 in reg_add_sub.c
239 0x121 in reg_compare.c
240 0x122 in reg_compare.c
241 0x123 in reg_compare.c
247 0x130 in get_address.c
248 0x131 in get_address.c
249 0x132 in get_address.c
250 0x133 in get_address.c
251 0x140 in load_store.c
252 0x141 in load_store.c
255 0x160 in reg_ld_str.c
256 0x161 in reg_ld_str.c
257 0x162 in reg_ld_str.c
258 0x163 in reg_ld_str.c
259 0x164 in reg_ld_str.c
263 0x180 in reg_convert.c
264 0x2nn in an *.S file:
294 void FPU_exception(int n
)
298 int_type
= 0; /* Needed only to stop compiler warnings */
299 if ( n
& EX_INTERNAL
)
301 int_type
= n
- EX_INTERNAL
;
303 /* Set lots of exception bits! */
304 partial_status
|= (SW_Exc_Mask
| SW_Summary
| SW_Backward
);
308 /* Extract only the bits which we use to set the status word */
310 /* Set the corresponding exception bit */
312 /* Set summary bits iff exception isn't masked */
313 if ( partial_status
& ~control_word
& CW_Exceptions
)
314 partial_status
|= (SW_Summary
| SW_Backward
);
315 if ( n
& (SW_Stack_Fault
| EX_Precision
) )
318 /* This bit distinguishes over- from underflow for a stack fault,
319 and roundup from round-down for precision loss. */
320 partial_status
&= ~SW_C1
;
324 RE_ENTRANT_CHECK_OFF
;
325 if ( (~control_word
& n
& CW_Exceptions
) || (n
== EX_INTERNAL
) )
327 #ifdef PRINT_MESSAGES
328 /* My message from the sponsor */
329 printk(FPU_VERSION
" "__DATE__
" (C) W. Metzenthen.\n");
330 #endif PRINT_MESSAGES
332 /* Get a name string for error reporting */
333 for (i
=0; exception_names
[i
].type
; i
++)
334 if ( (exception_names
[i
].type
& n
) == exception_names
[i
].type
)
337 if (exception_names
[i
].type
)
339 #ifdef PRINT_MESSAGES
340 printk("FP Exception: %s!\n", exception_names
[i
].name
);
341 #endif PRINT_MESSAGES
344 printk("FPU emulator: Unknown Exception: 0x%04x!\n", n
);
346 if ( n
== EX_INTERNAL
)
348 printk("FPU emulator: Internal error type 0x%04x\n", int_type
);
351 #ifdef PRINT_MESSAGES
354 #endif PRINT_MESSAGES
357 * The 80486 generates an interrupt on the next non-control FPU
358 * instruction. So we need some means of flagging it.
359 * We use the ES (Error Summary) bit for this.
365 math_abort(FPU_info
,SIGFPE
);
371 /* Real operation attempted on a NaN. */
372 /* Returns < 0 if the exception is unmasked */
373 int real_1op_NaN(FPU_REG
*a
)
375 int signalling
, isNaN
;
377 isNaN
= (exponent(a
) == EXP_OVER
) && (a
->sigh
& 0x80000000);
379 /* The default result for the case of two "equal" NaNs (signs may
380 differ) is chosen to reproduce 80486 behaviour */
381 signalling
= isNaN
&& !(a
->sigh
& 0x40000000);
385 if ( !isNaN
) /* pseudo-NaN, or other unsupported? */
387 if ( control_word
& CW_Invalid
)
389 /* Masked response */
390 reg_copy(&CONST_QNaN
, a
);
392 EXCEPTION(EX_Invalid
);
393 return (!(control_word
& CW_Invalid
) ? FPU_Exception
: 0) | TAG_Special
;
398 if ( control_word
& CW_Invalid
)
400 /* The masked response */
401 if ( !(a
->sigh
& 0x80000000) ) /* pseudo-NaN ? */
403 reg_copy(&CONST_QNaN
, a
);
405 /* ensure a Quiet NaN */
406 a
->sigh
|= 0x40000000;
409 EXCEPTION(EX_Invalid
);
411 return (!(control_word
& CW_Invalid
) ? FPU_Exception
: 0) | TAG_Special
;
415 /* Real operation attempted on two operands, one a NaN. */
416 /* Returns < 0 if the exception is unmasked */
417 int real_2op_NaN(FPU_REG
const *b
, u_char tagb
,
419 FPU_REG
const *defaultNaN
)
421 FPU_REG
*dest
= &st(deststnr
);
422 FPU_REG
const *a
= dest
;
423 u_char taga
= FPU_gettagi(deststnr
);
425 int signalling
, unsupported
;
427 if ( taga
== TAG_Special
)
428 taga
= FPU_Special(a
);
429 if ( tagb
== TAG_Special
)
430 tagb
= FPU_Special(b
);
432 /* TW_NaN is also used for unsupported data types. */
433 unsupported
= ((taga
== TW_NaN
)
434 && !((exponent(a
) == EXP_OVER
) && (a
->sigh
& 0x80000000)))
436 && !((exponent(b
) == EXP_OVER
) && (b
->sigh
& 0x80000000)));
439 if ( control_word
& CW_Invalid
)
441 /* Masked response */
442 FPU_copy_to_regi(&CONST_QNaN
, TAG_Special
, deststnr
);
444 EXCEPTION(EX_Invalid
);
445 return (!(control_word
& CW_Invalid
) ? FPU_Exception
: 0) | TAG_Special
;
453 signalling
= !(a
->sigh
& b
->sigh
& 0x40000000);
454 if ( significand(b
) > significand(a
) )
456 else if ( significand(b
) == significand(a
) )
458 /* The default result for the case of two "equal" NaNs (signs may
459 differ) is chosen to reproduce 80486 behaviour */
465 /* return the quiet version of the NaN in a */
466 signalling
= !(a
->sigh
& 0x40000000);
474 signalling
= !(b
->sigh
& 0x40000000);
481 EXCEPTION(EX_INTERNAL
|0x113);
486 if ( (!signalling
) || (control_word
& CW_Invalid
) )
491 if ( !(x
->sigh
& 0x80000000) ) /* pseudo-NaN ? */
494 FPU_copy_to_regi(x
, TAG_Special
, deststnr
);
499 /* ensure a Quiet NaN */
500 dest
->sigh
|= 0x40000000;
503 EXCEPTION(EX_Invalid
);
505 return (!(control_word
& CW_Invalid
) ? FPU_Exception
: 0) | TAG_Special
;
509 /* Invalid arith operation on Valid registers */
510 /* Returns < 0 if the exception is unmasked */
511 asmlinkage
int arith_invalid(int deststnr
)
514 EXCEPTION(EX_Invalid
);
516 if ( control_word
& CW_Invalid
)
518 /* The masked response */
519 FPU_copy_to_regi(&CONST_QNaN
, TAG_Special
, deststnr
);
522 return (!(control_word
& CW_Invalid
) ? FPU_Exception
: 0) | TAG_Valid
;
527 /* Divide a finite number by zero */
528 asmlinkage
int FPU_divide_by_zero(int deststnr
, u_char sign
)
530 FPU_REG
*dest
= &st(deststnr
);
533 if ( control_word
& CW_ZeroDiv
)
535 /* The masked response */
536 FPU_copy_to_regi(&CONST_INF
, TAG_Special
, deststnr
);
541 EXCEPTION(EX_ZeroDiv
);
543 return (!(control_word
& CW_ZeroDiv
) ? FPU_Exception
: 0) | tag
;
548 /* This may be called often, so keep it lean */
549 int set_precision_flag(int flags
)
551 if ( control_word
& CW_Precision
)
553 partial_status
&= ~(SW_C1
& flags
);
554 partial_status
|= flags
; /* The masked response */
565 /* This may be called often, so keep it lean */
566 asmlinkage
void set_precision_flag_up(void)
568 if ( control_word
& CW_Precision
)
569 partial_status
|= (SW_Precision
| SW_C1
); /* The masked response */
571 EXCEPTION(EX_Precision
| SW_C1
);
575 /* This may be called often, so keep it lean */
576 asmlinkage
void set_precision_flag_down(void)
578 if ( control_word
& CW_Precision
)
579 { /* The masked response */
580 partial_status
&= ~SW_C1
;
581 partial_status
|= SW_Precision
;
584 EXCEPTION(EX_Precision
);
588 asmlinkage
int denormal_operand(void)
590 if ( control_word
& CW_Denormal
)
591 { /* The masked response */
592 partial_status
|= SW_Denorm_Op
;
597 EXCEPTION(EX_Denormal
);
598 return TAG_Special
| FPU_Exception
;
603 asmlinkage
int arith_overflow(FPU_REG
*dest
)
607 if ( control_word
& CW_Overflow
)
609 /* The masked response */
610 /* ###### The response here depends upon the rounding mode */
611 reg_copy(&CONST_INF
, dest
);
616 /* Subtract the magic number from the exponent */
617 addexponent(dest
, (-3 * (1 << 13)));
620 EXCEPTION(EX_Overflow
);
621 if ( control_word
& CW_Overflow
)
623 /* The overflow exception is masked. */
624 /* By definition, precision is lost.
625 The roundup bit (C1) is also set because we have
626 "rounded" upwards to Infinity. */
627 EXCEPTION(EX_Precision
| SW_C1
);
636 asmlinkage
int arith_underflow(FPU_REG
*dest
)
640 if ( control_word
& CW_Underflow
)
642 /* The masked response */
643 if ( exponent16(dest
) <= EXP_UNDER
- 63 )
645 reg_copy(&CONST_Z
, dest
);
646 partial_status
&= ~SW_C1
; /* Round down. */
656 /* Add the magic number to the exponent. */
657 addexponent(dest
, (3 * (1 << 13)) + EXTENDED_Ebias
);
660 EXCEPTION(EX_Underflow
);
661 if ( control_word
& CW_Underflow
)
663 /* The underflow exception is masked. */
664 EXCEPTION(EX_Precision
);
673 void FPU_stack_overflow(void)
676 if ( control_word
& CW_Invalid
)
678 /* The masked response */
680 FPU_copy_to_reg0(&CONST_QNaN
, TAG_Special
);
683 EXCEPTION(EX_StackOver
);
690 void FPU_stack_underflow(void)
693 if ( control_word
& CW_Invalid
)
695 /* The masked response */
696 FPU_copy_to_reg0(&CONST_QNaN
, TAG_Special
);
699 EXCEPTION(EX_StackUnder
);
706 void FPU_stack_underflow_i(int i
)
709 if ( control_word
& CW_Invalid
)
711 /* The masked response */
712 FPU_copy_to_regi(&CONST_QNaN
, TAG_Special
, i
);
715 EXCEPTION(EX_StackUnder
);
722 void FPU_stack_underflow_pop(int i
)
725 if ( control_word
& CW_Invalid
)
727 /* The masked response */
728 FPU_copy_to_regi(&CONST_QNaN
, TAG_Special
, i
);
732 EXCEPTION(EX_StackUnder
);