4 * The error handling functions for wm-FPU-emu
7 * Copyright (C) 1992,1993,1994
8 * W. Metzenthen, 22 Parker St, Ormond, Vic 3163,
9 * Australia. E-mail billm@vaxc.cc.monash.edu.au
10 * All rights reserved.
12 * This copyright notice covers the redistribution and use of the
13 * FPU emulator developed by W. Metzenthen. It covers only its use
14 * in the 386BSD, FreeBSD and NetBSD operating systems. Any other
15 * use is not permitted under this copyright.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must include information specifying
23 * that source code for the emulator is freely available and include
25 * a) an offer to provide the source code for a nominal distribution
27 * b) list at least two alternative methods whereby the source
28 * can be obtained, e.g. a publically accessible bulletin board
29 * and an anonymous ftp site from which the software can be
31 * 3. All advertising materials specifically mentioning features or use of
32 * this emulator must acknowledge that it was developed by W. Metzenthen.
33 * 4. The name of W. Metzenthen may not be used to endorse or promote
34 * products derived from this software without specific prior written
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
38 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
39 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
40 * W. METZENTHEN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
41 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
42 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
43 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
44 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
45 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
46 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49 * The purpose of this copyright, based upon the Berkeley copyright, is to
50 * ensure that the covered software remains freely available to everyone.
52 * The software (with necessary differences) is also available, but under
53 * the terms of the GNU copyleft, for the Linux operating system and for
54 * the djgpp ms-dos extender.
56 * W. Metzenthen June 1994.
59 * $FreeBSD: src/sys/gnu/i386/fpemul/errors.c,v 1.10 1999/08/28 00:42:49 peter Exp $
60 * $DragonFly: src/sys/i386/gnu/fpemul/Attic/errors.c,v 1.3 2003/08/07 21:17:20 dillon Exp $
64 /*---------------------------------------------------------------------------+
66 | The file contains code which accesses user memory. |
67 | Emulator static data may change when user memory is accessed, due to |
68 | other processes using the emulator while swapping is in progress. |
69 +---------------------------------------------------------------------------*/
75 #include <sys/param.h>
76 #include <sys/systm.h>
78 #include <machine/pcb.h>
81 #include "fpu_system.h"
82 #include "exception.h"
84 #include "control_w.h"
85 #include "reg_constant.h"
96 unsigned char byte1, FPU_modrm;
99 byte1 = fubyte((unsigned char *) FPU_ORIG_EIP);
100 FPU_modrm = fubyte(1 + (unsigned char *) FPU_ORIG_EIP);
102 printf("Unimplemented FPU Opcode at eip=%#08x : %02x ",
103 FPU_ORIG_EIP, byte1);
105 if (FPU_modrm >= 0300)
106 printf("%02x (%02x+%d)\n", FPU_modrm, FPU_modrm & 0xf8, FPU_modrm & 7);
108 printf("/%d\n", (FPU_modrm >> 3) & 7);
111 EXCEPTION(EX_Invalid);
122 static char *tag_desc[] = {"Valid", "Zero", "ERROR", "ERROR",
123 "DeNorm", "Inf", "NaN", "Empty"};
124 unsigned char byte1, FPU_modrm;
126 REENTRANT_CHECK(OFF);
127 byte1 = fubyte((unsigned char *) FPU_ORIG_EIP);
128 FPU_modrm = fubyte(1 + (unsigned char *) FPU_ORIG_EIP);
131 if (status_word & SW_Backward)
132 printf("SW: backward compatibility\n");
133 if (status_word & SW_C3)
134 printf("SW: condition bit 3\n");
135 if (status_word & SW_C2)
136 printf("SW: condition bit 2\n");
137 if (status_word & SW_C1)
138 printf("SW: condition bit 1\n");
139 if (status_word & SW_C0)
140 printf("SW: condition bit 0\n");
141 if (status_word & SW_Summary)
142 printf("SW: exception summary\n");
143 if (status_word & SW_Stack_Fault)
144 printf("SW: stack fault\n");
145 if (status_word & SW_Precision)
146 printf("SW: loss of precision\n");
147 if (status_word & SW_Underflow)
148 printf("SW: underflow\n");
149 if (status_word & SW_Overflow)
150 printf("SW: overflow\n");
151 if (status_word & SW_Zero_Div)
152 printf("SW: divide by zero\n");
153 if (status_word & SW_Denorm_Op)
154 printf("SW: denormalized operand\n");
155 if (status_word & SW_Invalid)
156 printf("SW: invalid operation\n");
157 #endif /* DEBUGGING */
159 status_word = status_word & ~SW_Top;
160 status_word |= (top & 7) << SW_Top_Shift;
162 printf("At %#08x: %02x ", FPU_ORIG_EIP, byte1);
163 if (FPU_modrm >= 0300)
164 printf("%02x (%02x+%d)\n", FPU_modrm, FPU_modrm & 0xf8, FPU_modrm & 7);
166 printf("/%d, mod=%d rm=%d\n",
167 (FPU_modrm >> 3) & 7, (FPU_modrm >> 6) & 3, FPU_modrm & 7);
169 printf(" SW: b=%d st=%d es=%d sf=%d cc=%d%d%d%d ef=%d%d%d%d%d%d\n",
170 status_word & 0x8000 ? 1 : 0, /* busy */
171 (int)((status_word & 0x3800) >> 11), /* stack top pointer */
172 status_word & 0x80 ? 1 : 0, /* Error summary status */
173 status_word & 0x40 ? 1 : 0, /* Stack flag */
174 status_word & SW_C3 ? 1 : 0, status_word & SW_C2 ? 1 : 0, /* cc */
175 status_word & SW_C1 ? 1 : 0, status_word & SW_C0 ? 1 : 0, /* cc */
176 status_word & SW_Precision ? 1 : 0, status_word & SW_Underflow ? 1 : 0,
177 status_word & SW_Overflow ? 1 : 0, status_word & SW_Zero_Div ? 1 : 0,
178 status_word & SW_Denorm_Op ? 1 : 0, status_word & SW_Invalid ? 1 : 0);
180 printf(" CW: ic=%d rc=%d%d pc=%d%d iem=%d ef=%d%d%d%d%d%d\n",
181 control_word & 0x1000 ? 1 : 0,
182 (int)((control_word & 0x800) >> 11),
183 (int)((control_word & 0x400) >> 10),
184 (int)((control_word & 0x200) >> 9),
185 (int)((control_word & 0x100) >> 8),
186 control_word & 0x80 ? 1 : 0,
187 control_word & SW_Precision ? 1 : 0, control_word & SW_Underflow ? 1 : 0,
188 control_word & SW_Overflow ? 1 : 0, control_word & SW_Zero_Div ? 1 : 0,
189 control_word & SW_Denorm_Op ? 1 : 0, control_word & SW_Invalid ? 1 : 0);
191 for (i = 0; i < 8; i++) {
198 printf("st(%d) %c .0000 0000 0000 0000 ",
199 i, r->sign ? '-' : '+');
205 printf("st(%d) %c .%04lx %04lx %04lx %04lx e%+-6ld ",
206 i, r->sign ? '-' : '+', r->sigh >> 16,
207 r->sigh & 0xFFFF, r->sigl >> 16, r->sigl & 0xFFFF,
208 r->exp - EXP_BIAS + 1);
211 printf("Whoops! Error in errors.c ");
214 printf("%s\n", tag_desc[(int) (unsigned) r->tag]);
217 printf("[data] %c .%04lx %04lx %04lx %04lx e%+-6ld ",
218 FPU_loaded_data.sign ? '-' : '+', FPU_loaded_data.sigh >> 16,
219 FPU_loaded_data.sigh & 0xFFFF, FPU_loaded_data.sigl >> 16,
220 FPU_loaded_data.sigl & 0xFFFF, FPU_loaded_data.exp - EXP_BIAS + 1);
221 printf("%s\n", tag_desc[(int) (unsigned) FPU_loaded_data.tag]);
229 } exception_names[] = {
231 EX_StackOver, "stack overflow"
234 EX_StackUnder, "stack underflow"
237 EX_Precision, "loss of precision"
240 EX_Underflow, "underflow"
243 EX_Overflow, "overflow"
246 EX_ZeroDiv, "divide by zero"
249 EX_Denormal, "denormalized operand"
252 EX_Invalid, "invalid operation"
255 EX_INTERNAL, "INTERNAL BUG in " FPU_VERSION
262 EX_INTERNAL is always given with a code which indicates where the
265 Internal error types:
268 0x101 in reg_add_sub.c
275 0x108 in reg_compare.c
276 0x109 in reg_compare.c
277 0x110 in reg_add_sub.c
280 0x113 in reg_add_sub.c
281 0x114 in reg_ld_str.c
288 0x121 in reg_compare.c
289 0x122 in reg_compare.c
290 0x123 in reg_compare.c
291 0x2nn in an *.s file:
317 int_type = 0; /* Needed only to stop compiler warnings */
318 if (n & EX_INTERNAL) {
319 int_type = n - EX_INTERNAL;
321 /* Set lots of exception bits! */
322 status_word |= (SW_Exc_Mask | SW_Summary | FPU_BUSY);
324 /* Extract only the bits which we use to set the status word */
326 /* Set the corresponding exception bit */
328 if (status_word & ~control_word & CW_Exceptions)
329 status_word |= SW_Summary;
330 if (n & (SW_Stack_Fault | EX_Precision)) {
332 /* This bit distinguishes over- from underflow
333 * for a stack fault, and roundup from
334 * round-down for precision loss. */
335 status_word &= ~SW_C1;
339 REENTRANT_CHECK(OFF);
340 if ((~control_word & n & CW_Exceptions) || (n == EX_INTERNAL)) {
341 #ifdef PRINT_MESSAGES
342 /* My message from the sponsor */
343 printf(FPU_VERSION " " __DATE__ " (C) W. Metzenthen.\n");
344 #endif /* PRINT_MESSAGES */
346 /* Get a name string for error reporting */
347 for (i = 0; exception_names[i].type; i++)
348 if ((exception_names[i].type & n) == exception_names[i].type)
351 if (exception_names[i].type) {
352 #ifdef PRINT_MESSAGES
353 printf("FP Exception: %s!\n", exception_names[i].name);
354 #endif /* PRINT_MESSAGES */
356 printf("FP emulator: Unknown Exception: 0x%04x!\n", n);
358 if (n == EX_INTERNAL) {
359 printf("FP emulator: Internal error type 0x%04x\n", int_type);
362 #ifdef PRINT_MESSAGES
365 #endif /* PRINT_MESSAGES */
367 /* The 80486 generates an interrupt on the next non-control
368 * FPU instruction. So we need some means of flagging it. We
369 * use the ES (Error Summary) bit for this, assuming that this
370 * is the way a real FPU does it (until I can check it out),
371 * if not, then some method such as the following kludge might
373 /* regs[0].tag |= TW_FPU_Interrupt; */
379 #endif /* __DEBUG__ */
384 /* Real operation attempted on two operands, one a NaN */
386 real_2op_NaN(FPU_REG * a, FPU_REG * b, FPU_REG * dest)
392 if (a->tag == TW_NaN) {
393 if (b->tag == TW_NaN) {
394 signalling = !(a->sigh & b->sigh & 0x40000000);
395 /* find the "larger" */
396 if (*(long long *) &(a->sigl) < *(long long *) &(b->sigl))
399 /* return the quiet version of the NaN in a */
400 signalling = !(a->sigh & 0x40000000);
404 if (b->tag == TW_NaN)
405 #endif /* PARANOID */
407 signalling = !(b->sigh & 0x40000000);
413 EXCEPTION(EX_INTERNAL | 0x113);
416 #endif /* PARANOID */
419 if (!(x->sigh & 0x80000000)) /* pseudo-NaN ? */
424 if (control_word & CW_Invalid) {
425 /* The masked response */
426 if (!(x->sigh & 0x80000000)) /* pseudo-NaN ? */
429 /* ensure a Quiet NaN */
430 dest->sigh |= 0x40000000;
432 EXCEPTION(EX_Invalid);
436 /* Invalid arith operation on Valid registers */
438 arith_invalid(FPU_REG * dest)
441 if (control_word & CW_Invalid) {
442 /* The masked response */
443 reg_move(&CONST_QNaN, dest);
445 EXCEPTION(EX_Invalid);
452 /* Divide a finite number by zero */
454 divide_by_zero(int sign, FPU_REG * dest)
457 if (control_word & CW_ZeroDiv) {
458 /* The masked response */
459 reg_move(&CONST_INF, dest);
460 dest->sign = (unsigned char) sign;
462 EXCEPTION(EX_ZeroDiv);
469 /* This may be called often, so keep it lean */
471 set_precision_flag_up(void)
473 if (control_word & CW_Precision)
474 status_word |= (SW_Precision | SW_C1); /* The masked response */
476 exception(EX_Precision | SW_C1);
481 /* This may be called often, so keep it lean */
483 set_precision_flag_down(void)
485 if (control_word & CW_Precision) { /* The masked response */
486 status_word &= ~SW_C1;
487 status_word |= SW_Precision;
489 exception(EX_Precision);
494 denormal_operand(void)
496 if (control_word & CW_Denormal) { /* The masked response */
497 status_word |= SW_Denorm_Op;
500 exception(EX_Denormal);
507 arith_overflow(FPU_REG * dest)
510 if (control_word & CW_Overflow) {
512 /* The masked response */
513 /* **** The response here depends upon the rounding mode */
515 reg_move(&CONST_INF, dest);
518 /* Subtract the magic number from the exponent */
519 dest->exp -= (3 * (1 << 13));
522 /* By definition, precision is lost. It appears that the roundup bit
523 * (C1) is also set by convention. */
524 EXCEPTION(EX_Overflow | EX_Precision | SW_C1);
532 arith_underflow(FPU_REG * dest)
535 if (control_word & CW_Underflow) {
536 /* The masked response */
537 if (dest->exp <= EXP_UNDER - 63)
538 reg_move(&CONST_Z, dest);
540 /* Add the magic number to the exponent */
541 dest->exp += (3 * (1 << 13));
544 EXCEPTION(EX_Underflow);
554 if (control_word & CW_Invalid) {
555 /* The masked response */
557 reg_move(&CONST_QNaN, FPU_st0_ptr = &st(0));
559 EXCEPTION(EX_StackOver);
567 stack_underflow(void)
570 if (control_word & CW_Invalid) {
571 /* The masked response */
572 reg_move(&CONST_QNaN, FPU_st0_ptr);
574 EXCEPTION(EX_StackUnder);
582 stack_underflow_i(int i)
585 if (control_word & CW_Invalid) {
586 /* The masked response */
587 reg_move(&CONST_QNaN, &(st(i)));
589 EXCEPTION(EX_StackUnder);
597 stack_underflow_pop(int i)
600 if (control_word & CW_Invalid) {
601 /* The masked response */
602 reg_move(&CONST_QNaN, &(st(i)));
605 EXCEPTION(EX_StackUnder);