1 // SPDX-License-Identifier: GPL-2.0
2 /*---------------------------------------------------------------------------+
5 | The error handling functions for wm-FPU-emu |
7 | Copyright (C) 1992,1993,1994,1996 |
8 | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
9 | E-mail billm@jacobi.maths.monash.edu.au |
12 +---------------------------------------------------------------------------*/
14 /*---------------------------------------------------------------------------+
16 | The file contains code which accesses user memory. |
17 | Emulator static data may change when user memory is accessed, due to |
18 | other processes using the emulator while swapping is in progress. |
19 +---------------------------------------------------------------------------*/
21 #include <linux/signal.h>
23 #include <linux/uaccess.h>
26 #include "fpu_system.h"
27 #include "exception.h"
29 #include "control_w.h"
30 #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
) {
48 FPU_get_user(byte1
, (u_char __user
*) address
);
49 if ((byte1
& 0xf8) == 0xd8)
51 printk("[%02x]", byte1
);
54 printk("%02x ", byte1
);
55 FPU_get_user(FPU_modrm
, 1 + (u_char __user
*) address
);
57 if (FPU_modrm
>= 0300)
58 printk("%02x (%02x+%d)\n", FPU_modrm
, FPU_modrm
& 0xf8,
61 printk("/%d\n", (FPU_modrm
>> 3) & 7);
63 printk("cs selector = %04x\n", FPU_CS
);
68 EXCEPTION(EX_Invalid
);
74 Called for opcodes which are illegal and which are known to result in a
75 SIGILL with a real 80486.
77 void FPU_illegal(void)
79 math_abort(FPU_info
, SIGILL
);
82 void FPU_printall(void)
85 static const char *tag_desc
[] = { "Valid", "Zero", "ERROR", "Empty",
86 "DeNorm", "Inf", "NaN"
88 u_char byte1
, FPU_modrm
;
89 unsigned long address
= FPU_ORIG_EIP
;
92 /* No need to check access_ok(), we have previously fetched these bytes. */
93 printk("At %p:", (void *)address
);
94 if (FPU_CS
== __USER_CS
) {
95 #define MAX_PRINTED_BYTES 20
96 for (i
= 0; i
< MAX_PRINTED_BYTES
; i
++) {
97 FPU_get_user(byte1
, (u_char __user
*) address
);
98 if ((byte1
& 0xf8) == 0xd8) {
99 printk(" %02x", byte1
);
102 printk(" [%02x]", byte1
);
105 if (i
== MAX_PRINTED_BYTES
)
106 printk(" [more..]\n");
108 FPU_get_user(FPU_modrm
, 1 + (u_char __user
*) address
);
110 if (FPU_modrm
>= 0300)
111 printk(" %02x (%02x+%d)\n", FPU_modrm
,
112 FPU_modrm
& 0xf8, FPU_modrm
& 7);
114 printk(" /%d, mod=%d rm=%d\n",
115 (FPU_modrm
>> 3) & 7,
116 (FPU_modrm
>> 6) & 3, FPU_modrm
& 7);
119 printk("%04x\n", FPU_CS
);
122 partial_status
= status_word();
125 if (partial_status
& SW_Backward
)
126 printk("SW: backward compatibility\n");
127 if (partial_status
& SW_C3
)
128 printk("SW: condition bit 3\n");
129 if (partial_status
& SW_C2
)
130 printk("SW: condition bit 2\n");
131 if (partial_status
& SW_C1
)
132 printk("SW: condition bit 1\n");
133 if (partial_status
& SW_C0
)
134 printk("SW: condition bit 0\n");
135 if (partial_status
& SW_Summary
)
136 printk("SW: exception summary\n");
137 if (partial_status
& SW_Stack_Fault
)
138 printk("SW: stack fault\n");
139 if (partial_status
& SW_Precision
)
140 printk("SW: loss of precision\n");
141 if (partial_status
& SW_Underflow
)
142 printk("SW: underflow\n");
143 if (partial_status
& SW_Overflow
)
144 printk("SW: overflow\n");
145 if (partial_status
& SW_Zero_Div
)
146 printk("SW: divide by zero\n");
147 if (partial_status
& SW_Denorm_Op
)
148 printk("SW: denormalized operand\n");
149 if (partial_status
& SW_Invalid
)
150 printk("SW: invalid operation\n");
151 #endif /* DEBUGGING */
153 printk(" SW: b=%d st=%d es=%d sf=%d cc=%d%d%d%d ef=%d%d%d%d%d%d\n", partial_status
& 0x8000 ? 1 : 0, /* busy */
154 (partial_status
& 0x3800) >> 11, /* stack top pointer */
155 partial_status
& 0x80 ? 1 : 0, /* Error summary status */
156 partial_status
& 0x40 ? 1 : 0, /* Stack flag */
157 partial_status
& SW_C3
? 1 : 0, partial_status
& SW_C2
? 1 : 0, /* cc */
158 partial_status
& SW_C1
? 1 : 0, partial_status
& SW_C0
? 1 : 0, /* cc */
159 partial_status
& SW_Precision
? 1 : 0,
160 partial_status
& SW_Underflow
? 1 : 0,
161 partial_status
& SW_Overflow
? 1 : 0,
162 partial_status
& SW_Zero_Div
? 1 : 0,
163 partial_status
& SW_Denorm_Op
? 1 : 0,
164 partial_status
& SW_Invalid
? 1 : 0);
166 printk(" CW: ic=%d rc=%d%d pc=%d%d iem=%d ef=%d%d%d%d%d%d\n",
167 control_word
& 0x1000 ? 1 : 0,
168 (control_word
& 0x800) >> 11, (control_word
& 0x400) >> 10,
169 (control_word
& 0x200) >> 9, (control_word
& 0x100) >> 8,
170 control_word
& 0x80 ? 1 : 0,
171 control_word
& SW_Precision
? 1 : 0,
172 control_word
& SW_Underflow
? 1 : 0,
173 control_word
& SW_Overflow
? 1 : 0,
174 control_word
& SW_Zero_Div
? 1 : 0,
175 control_word
& SW_Denorm_Op
? 1 : 0,
176 control_word
& SW_Invalid
? 1 : 0);
178 for (i
= 0; i
< 8; i
++) {
180 u_char tagi
= FPU_gettagi(i
);
187 /* Update tagi for the printk below */
188 tagi
= FPU_Special(r
);
191 printk("st(%d) %c .%04lx %04lx %04lx %04lx e%+-6d ", i
,
192 getsign(r
) ? '-' : '+',
193 (long)(r
->sigh
>> 16),
194 (long)(r
->sigh
& 0xFFFF),
195 (long)(r
->sigl
>> 16),
196 (long)(r
->sigl
& 0xFFFF),
197 exponent(r
) - EXP_BIAS
+ 1);
200 printk("Whoops! Error in errors.c: tag%d is %d ", i
,
204 printk("%s\n", tag_desc
[(int)(unsigned)tagi
]);
214 } exception_names
[] = {
216 EX_StackOver
, "stack overflow"}, {
217 EX_StackUnder
, "stack underflow"}, {
218 EX_Precision
, "loss of precision"}, {
219 EX_Underflow
, "underflow"}, {
220 EX_Overflow
, "overflow"}, {
221 EX_ZeroDiv
, "divide by zero"}, {
222 EX_Denormal
, "denormalized operand"}, {
223 EX_Invalid
, "invalid operation"}, {
224 EX_INTERNAL
, "INTERNAL BUG in " FPU_VERSION
}, {
229 EX_INTERNAL is always given with a code which indicates where the
232 Internal error types:
235 0x101 in reg_add_sub.c
240 0x108 in reg_compare.c
241 0x109 in reg_compare.c
242 0x110 in reg_add_sub.c
252 0x121 in reg_compare.c
253 0x122 in reg_compare.c
254 0x123 in reg_compare.c
260 0x130 in get_address.c
261 0x131 in get_address.c
262 0x132 in get_address.c
263 0x133 in get_address.c
264 0x140 in load_store.c
265 0x141 in load_store.c
268 0x160 in reg_ld_str.c
269 0x161 in reg_ld_str.c
270 0x162 in reg_ld_str.c
271 0x163 in reg_ld_str.c
272 0x164 in reg_ld_str.c
276 0x180 in reg_convert.c
277 0x2nn in an *.S file:
307 asmlinkage __visible
void FPU_exception(int n
)
311 int_type
= 0; /* Needed only to stop compiler warnings */
312 if (n
& EX_INTERNAL
) {
313 int_type
= n
- EX_INTERNAL
;
315 /* Set lots of exception bits! */
316 partial_status
|= (SW_Exc_Mask
| SW_Summary
| SW_Backward
);
318 /* Extract only the bits which we use to set the status word */
320 /* Set the corresponding exception bit */
322 /* Set summary bits iff exception isn't masked */
323 if (partial_status
& ~control_word
& CW_Exceptions
)
324 partial_status
|= (SW_Summary
| SW_Backward
);
325 if (n
& (SW_Stack_Fault
| EX_Precision
)) {
327 /* This bit distinguishes over- from underflow for a stack fault,
328 and roundup from round-down for precision loss. */
329 partial_status
&= ~SW_C1
;
333 RE_ENTRANT_CHECK_OFF
;
334 if ((~control_word
& n
& CW_Exceptions
) || (n
== EX_INTERNAL
)) {
335 /* Get a name string for error reporting */
336 for (i
= 0; exception_names
[i
].type
; i
++)
337 if ((exception_names
[i
].type
& n
) ==
338 exception_names
[i
].type
)
341 if (exception_names
[i
].type
) {
342 #ifdef PRINT_MESSAGES
343 printk("FP Exception: %s!\n", exception_names
[i
].name
);
344 #endif /* PRINT_MESSAGES */
346 printk("FPU emulator: Unknown Exception: 0x%04x!\n", n
);
348 if (n
== EX_INTERNAL
) {
349 printk("FPU emulator: Internal error type 0x%04x\n",
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__ */
372 /* Real operation attempted on a NaN. */
373 /* Returns < 0 if the exception is unmasked */
374 int real_1op_NaN(FPU_REG
*a
)
376 int signalling
, isNaN
;
378 isNaN
= (exponent(a
) == EXP_OVER
) && (a
->sigh
& 0x80000000);
380 /* The default result for the case of two "equal" NaNs (signs may
381 differ) is chosen to reproduce 80486 behaviour */
382 signalling
= isNaN
&& !(a
->sigh
& 0x40000000);
385 if (!isNaN
) { /* pseudo-NaN, or other unsupported? */
386 if (control_word
& CW_Invalid
) {
387 /* Masked response */
388 reg_copy(&CONST_QNaN
, a
);
390 EXCEPTION(EX_Invalid
);
391 return (!(control_word
& CW_Invalid
) ? FPU_Exception
:
397 if (control_word
& CW_Invalid
) {
398 /* The masked response */
399 if (!(a
->sigh
& 0x80000000)) { /* pseudo-NaN ? */
400 reg_copy(&CONST_QNaN
, a
);
402 /* ensure a Quiet NaN */
403 a
->sigh
|= 0x40000000;
406 EXCEPTION(EX_Invalid
);
408 return (!(control_word
& CW_Invalid
) ? FPU_Exception
: 0) | TAG_Special
;
411 /* Real operation attempted on two operands, one a NaN. */
412 /* Returns < 0 if the exception is unmasked */
413 int real_2op_NaN(FPU_REG
const *b
, u_char tagb
,
414 int deststnr
, FPU_REG
const *defaultNaN
)
416 FPU_REG
*dest
= &st(deststnr
);
417 FPU_REG
const *a
= dest
;
418 u_char taga
= FPU_gettagi(deststnr
);
420 int signalling
, unsupported
;
422 if (taga
== TAG_Special
)
423 taga
= FPU_Special(a
);
424 if (tagb
== TAG_Special
)
425 tagb
= FPU_Special(b
);
427 /* TW_NaN is also used for unsupported data types. */
428 unsupported
= ((taga
== TW_NaN
)
429 && !((exponent(a
) == EXP_OVER
)
430 && (a
->sigh
& 0x80000000)))
432 && !((exponent(b
) == EXP_OVER
) && (b
->sigh
& 0x80000000)));
434 if (control_word
& CW_Invalid
) {
435 /* Masked response */
436 FPU_copy_to_regi(&CONST_QNaN
, TAG_Special
, deststnr
);
438 EXCEPTION(EX_Invalid
);
439 return (!(control_word
& CW_Invalid
) ? FPU_Exception
: 0) |
443 if (taga
== TW_NaN
) {
445 if (tagb
== TW_NaN
) {
446 signalling
= !(a
->sigh
& b
->sigh
& 0x40000000);
447 if (significand(b
) > significand(a
))
449 else if (significand(b
) == significand(a
)) {
450 /* The default result for the case of two "equal" NaNs (signs may
451 differ) is chosen to reproduce 80486 behaviour */
455 /* return the quiet version of the NaN in a */
456 signalling
= !(a
->sigh
& 0x40000000);
461 #endif /* PARANOID */
463 signalling
= !(b
->sigh
& 0x40000000);
469 EXCEPTION(EX_INTERNAL
| 0x113);
472 #endif /* PARANOID */
474 if ((!signalling
) || (control_word
& CW_Invalid
)) {
478 if (!(x
->sigh
& 0x80000000)) /* pseudo-NaN ? */
481 FPU_copy_to_regi(x
, TAG_Special
, deststnr
);
486 /* ensure a Quiet NaN */
487 dest
->sigh
|= 0x40000000;
490 EXCEPTION(EX_Invalid
);
492 return (!(control_word
& CW_Invalid
) ? FPU_Exception
: 0) | TAG_Special
;
495 /* Invalid arith operation on Valid registers */
496 /* Returns < 0 if the exception is unmasked */
497 asmlinkage __visible
int arith_invalid(int deststnr
)
500 EXCEPTION(EX_Invalid
);
502 if (control_word
& CW_Invalid
) {
503 /* The masked response */
504 FPU_copy_to_regi(&CONST_QNaN
, TAG_Special
, deststnr
);
507 return (!(control_word
& CW_Invalid
) ? FPU_Exception
: 0) | TAG_Valid
;
511 /* Divide a finite number by zero */
512 asmlinkage __visible
int FPU_divide_by_zero(int deststnr
, u_char sign
)
514 FPU_REG
*dest
= &st(deststnr
);
517 if (control_word
& CW_ZeroDiv
) {
518 /* The masked response */
519 FPU_copy_to_regi(&CONST_INF
, TAG_Special
, deststnr
);
524 EXCEPTION(EX_ZeroDiv
);
526 return (!(control_word
& CW_ZeroDiv
) ? FPU_Exception
: 0) | tag
;
530 /* This may be called often, so keep it lean */
531 int set_precision_flag(int flags
)
533 if (control_word
& CW_Precision
) {
534 partial_status
&= ~(SW_C1
& flags
);
535 partial_status
|= flags
; /* The masked response */
543 /* This may be called often, so keep it lean */
544 asmlinkage __visible
void set_precision_flag_up(void)
546 if (control_word
& CW_Precision
)
547 partial_status
|= (SW_Precision
| SW_C1
); /* The masked response */
549 EXCEPTION(EX_Precision
| SW_C1
);
552 /* This may be called often, so keep it lean */
553 asmlinkage __visible
void set_precision_flag_down(void)
555 if (control_word
& CW_Precision
) { /* The masked response */
556 partial_status
&= ~SW_C1
;
557 partial_status
|= SW_Precision
;
559 EXCEPTION(EX_Precision
);
562 asmlinkage __visible
int denormal_operand(void)
564 if (control_word
& CW_Denormal
) { /* The masked response */
565 partial_status
|= SW_Denorm_Op
;
568 EXCEPTION(EX_Denormal
);
569 return TAG_Special
| FPU_Exception
;
573 asmlinkage __visible
int arith_overflow(FPU_REG
*dest
)
577 if (control_word
& CW_Overflow
) {
578 /* The masked response */
579 /* ###### The response here depends upon the rounding mode */
580 reg_copy(&CONST_INF
, dest
);
583 /* Subtract the magic number from the exponent */
584 addexponent(dest
, (-3 * (1 << 13)));
587 EXCEPTION(EX_Overflow
);
588 if (control_word
& CW_Overflow
) {
589 /* The overflow exception is masked. */
590 /* By definition, precision is lost.
591 The roundup bit (C1) is also set because we have
592 "rounded" upwards to Infinity. */
593 EXCEPTION(EX_Precision
| SW_C1
);
601 asmlinkage __visible
int arith_underflow(FPU_REG
*dest
)
605 if (control_word
& CW_Underflow
) {
606 /* The masked response */
607 if (exponent16(dest
) <= EXP_UNDER
- 63) {
608 reg_copy(&CONST_Z
, dest
);
609 partial_status
&= ~SW_C1
; /* Round down. */
615 /* Add the magic number to the exponent. */
616 addexponent(dest
, (3 * (1 << 13)) + EXTENDED_Ebias
);
619 EXCEPTION(EX_Underflow
);
620 if (control_word
& CW_Underflow
) {
621 /* The underflow exception is masked. */
622 EXCEPTION(EX_Precision
);
630 void FPU_stack_overflow(void)
633 if (control_word
& CW_Invalid
) {
634 /* The masked response */
636 FPU_copy_to_reg0(&CONST_QNaN
, TAG_Special
);
639 EXCEPTION(EX_StackOver
);
645 void FPU_stack_underflow(void)
648 if (control_word
& CW_Invalid
) {
649 /* The masked response */
650 FPU_copy_to_reg0(&CONST_QNaN
, TAG_Special
);
653 EXCEPTION(EX_StackUnder
);
659 void FPU_stack_underflow_i(int i
)
662 if (control_word
& CW_Invalid
) {
663 /* The masked response */
664 FPU_copy_to_regi(&CONST_QNaN
, TAG_Special
, i
);
667 EXCEPTION(EX_StackUnder
);
673 void FPU_stack_underflow_pop(int i
)
676 if (control_word
& CW_Invalid
) {
677 /* The masked response */
678 FPU_copy_to_regi(&CONST_QNaN
, TAG_Special
, i
);
682 EXCEPTION(EX_StackUnder
);