Merge from vendor branch DIFFUTILS:
[dragonfly.git] / sys / i386 / gnu / fpemul / errors.c
1 /*
2  *  errors.c
3  *
4  *  The error handling functions for wm-FPU-emu
5  *
6  *
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.
11  *
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.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
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
24  *    either:
25  *      a) an offer to provide the source code for a nominal distribution
26  *         fee, or
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
30  *         downloaded.
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
35  *    permission.
36  *
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.
47  *
48  *
49  * The purpose of this copyright, based upon the Berkeley copyright, is to
50  * ensure that the covered software remains freely available to everyone.
51  *
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.
55  *
56  * W. Metzenthen   June 1994.
57  *
58  *
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 $
61  *
62  */
63
64 /*---------------------------------------------------------------------------+
65  | Note:                                                                     |
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  +---------------------------------------------------------------------------*/
70
71
72
73
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/proc.h>
78 #include <machine/pcb.h>
79
80 #include "fpu_emu.h"
81 #include "fpu_system.h"
82 #include "exception.h"
83 #include "status_w.h"
84 #include "control_w.h"
85 #include "reg_constant.h"
86 #include "version.h"
87
88 /* */
89 #undef PRINT_MESSAGES
90 /* */
91
92
93 void
94 Un_impl(void)
95 {
96         unsigned char byte1, FPU_modrm;
97
98         REENTRANT_CHECK(OFF);
99         byte1 = fubyte((unsigned char *) FPU_ORIG_EIP);
100         FPU_modrm = fubyte(1 + (unsigned char *) FPU_ORIG_EIP);
101
102         printf("Unimplemented FPU Opcode at eip=%#08x : %02x ",
103             FPU_ORIG_EIP, byte1);
104
105         if (FPU_modrm >= 0300)
106                 printf("%02x (%02x+%d)\n", FPU_modrm, FPU_modrm & 0xf8, FPU_modrm & 7);
107         else
108                 printf("/%d\n", (FPU_modrm >> 3) & 7);
109         REENTRANT_CHECK(ON);
110
111         EXCEPTION(EX_Invalid);
112
113 }
114
115
116
117
118 void
119 emu_printall()
120 {
121         int     i;
122         static char *tag_desc[] = {"Valid", "Zero", "ERROR", "ERROR",
123         "DeNorm", "Inf", "NaN", "Empty"};
124         unsigned char byte1, FPU_modrm;
125
126         REENTRANT_CHECK(OFF);
127         byte1 = fubyte((unsigned char *) FPU_ORIG_EIP);
128         FPU_modrm = fubyte(1 + (unsigned char *) FPU_ORIG_EIP);
129
130 #ifdef DEBUGGING
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 */
158
159         status_word = status_word & ~SW_Top;
160         status_word |= (top & 7) << SW_Top_Shift;
161
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);
165         else
166                 printf("/%d, mod=%d rm=%d\n",
167                     (FPU_modrm >> 3) & 7, (FPU_modrm >> 6) & 3, FPU_modrm & 7);
168
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);
179
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);
190
191         for (i = 0; i < 8; i++) {
192                 FPU_REG *r = &st(i);
193                 switch (r->tag) {
194                 case TW_Empty:
195                         continue;
196                         break;
197                 case TW_Zero:
198                         printf("st(%d)  %c .0000 0000 0000 0000         ",
199                             i, r->sign ? '-' : '+');
200                         break;
201                 case TW_Valid:
202                 case TW_NaN:
203                 case TW_Denormal:
204                 case TW_Infinity:
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);
209                         break;
210                 default:
211                         printf("Whoops! Error in errors.c      ");
212                         break;
213                 }
214                 printf("%s\n", tag_desc[(int) (unsigned) r->tag]);
215         }
216
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]);
222         REENTRANT_CHECK(ON);
223
224 }
225
226 static struct {
227         int     type;
228         char   *name;
229 }       exception_names[] = {
230         {
231                 EX_StackOver, "stack overflow"
232         },
233         {
234                 EX_StackUnder, "stack underflow"
235         },
236         {
237                 EX_Precision, "loss of precision"
238         },
239         {
240                 EX_Underflow, "underflow"
241         },
242         {
243                 EX_Overflow, "overflow"
244         },
245         {
246                 EX_ZeroDiv, "divide by zero"
247         },
248         {
249                 EX_Denormal, "denormalized operand"
250         },
251         {
252                 EX_Invalid, "invalid operation"
253         },
254         {
255                 EX_INTERNAL, "INTERNAL BUG in " FPU_VERSION
256         },
257         {
258                 0, NULL
259         }
260 };
261 /*
262  EX_INTERNAL is always given with a code which indicates where the
263  error was detected.
264
265  Internal error types:
266        0x14   in e14.c
267        0x1nn  in a *.c file:
268               0x101  in reg_add_sub.c
269               0x102  in reg_mul.c
270               0x103  in poly_sin.c
271               0x104  in poly_tan.c
272               0x105  in reg_mul.c
273               0x106  in reg_mov.c
274               0x107  in fpu_trig.c
275               0x108  in reg_compare.c
276               0x109  in reg_compare.c
277               0x110  in reg_add_sub.c
278               0x111  in interface.c
279               0x112  in fpu_trig.c
280               0x113  in reg_add_sub.c
281               0x114  in reg_ld_str.c
282               0x115  in fpu_trig.c
283               0x116  in fpu_trig.c
284               0x117  in fpu_trig.c
285               0x118  in fpu_trig.c
286               0x119  in fpu_trig.c
287               0x120  in poly_atan.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:
292               0x201  in reg_u_add.S
293               0x202  in reg_u_div.S
294               0x203  in reg_u_div.S
295               0x204  in reg_u_div.S
296               0x205  in reg_u_mul.S
297               0x206  in reg_u_sub.S
298               0x207  in wm_sqrt.S
299               0x208  in reg_div.S
300               0x209  in reg_u_sub.S
301               0x210  in reg_u_sub.S
302               0x211  in reg_u_sub.S
303               0x212  in reg_u_sub.S
304               0x213  in wm_sqrt.S
305               0x214  in wm_sqrt.S
306               0x215  in wm_sqrt.S
307               0x216  in reg_round.S
308               0x217  in reg_round.S
309               0x218  in reg_round.S
310  */
311
312 void
313 exception(int n)
314 {
315         int     i, int_type;
316
317         int_type = 0;           /* Needed only to stop compiler warnings */
318         if (n & EX_INTERNAL) {
319                 int_type = n - EX_INTERNAL;
320                 n = EX_INTERNAL;
321                 /* Set lots of exception bits! */
322                 status_word |= (SW_Exc_Mask | SW_Summary | FPU_BUSY);
323         } else {
324                 /* Extract only the bits which we use to set the status word */
325                 n &= (SW_Exc_Mask);
326                 /* Set the corresponding exception bit */
327                 status_word |= n;
328                 if (status_word & ~control_word & CW_Exceptions)
329                         status_word |= SW_Summary;
330                 if (n & (SW_Stack_Fault | EX_Precision)) {
331                         if (!(n & SW_C1))
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;
336                 }
337         }
338
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 */
345
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)
349                                 break;
350
351                 if (exception_names[i].type) {
352 #ifdef PRINT_MESSAGES
353                         printf("FP Exception: %s!\n", exception_names[i].name);
354 #endif                          /* PRINT_MESSAGES */
355                 } else
356                         printf("FP emulator: Unknown Exception: 0x%04x!\n", n);
357
358                 if (n == EX_INTERNAL) {
359                         printf("FP emulator: Internal error type 0x%04x\n", int_type);
360                         emu_printall();
361                 }
362 #ifdef PRINT_MESSAGES
363                 else
364                         emu_printall();
365 #endif                          /* PRINT_MESSAGES */
366
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
372                  * be needed. */
373 /*      regs[0].tag |= TW_FPU_Interrupt; */
374         }
375         REENTRANT_CHECK(ON);
376
377 #ifdef __DEBUG__
378         math_abort(SIGFPE);
379 #endif                          /* __DEBUG__ */
380
381 }
382
383
384 /* Real operation attempted on two operands, one a NaN */
385 void
386 real_2op_NaN(FPU_REG * a, FPU_REG * b, FPU_REG * dest)
387 {
388         FPU_REG *x;
389         int     signalling;
390
391         x = a;
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))
397                                 x = b;
398                 } else {
399                         /* return the quiet version of the NaN in a */
400                         signalling = !(a->sigh & 0x40000000);
401                 }
402         } else
403 #ifdef PARANOID
404                 if (b->tag == TW_NaN)
405 #endif                          /* PARANOID */
406                 {
407                         signalling = !(b->sigh & 0x40000000);
408                         x = b;
409                 }
410 #ifdef PARANOID
411                 else {
412                         signalling = 0;
413                         EXCEPTION(EX_INTERNAL | 0x113);
414                         x = &CONST_QNaN;
415                 }
416 #endif                          /* PARANOID */
417
418         if (!signalling) {
419                 if (!(x->sigh & 0x80000000))    /* pseudo-NaN ? */
420                         x = &CONST_QNaN;
421                 reg_move(x, dest);
422                 return;
423         }
424         if (control_word & CW_Invalid) {
425                 /* The masked response */
426                 if (!(x->sigh & 0x80000000))    /* pseudo-NaN ? */
427                         x = &CONST_QNaN;
428                 reg_move(x, dest);
429                 /* ensure a Quiet NaN */
430                 dest->sigh |= 0x40000000;
431         }
432         EXCEPTION(EX_Invalid);
433
434         return;
435 }
436 /* Invalid arith operation on Valid registers */
437 void
438 arith_invalid(FPU_REG * dest)
439 {
440
441         if (control_word & CW_Invalid) {
442                 /* The masked response */
443                 reg_move(&CONST_QNaN, dest);
444         }
445         EXCEPTION(EX_Invalid);
446
447         return;
448
449 }
450
451
452 /* Divide a finite number by zero */
453 void
454 divide_by_zero(int sign, FPU_REG * dest)
455 {
456
457         if (control_word & CW_ZeroDiv) {
458                 /* The masked response */
459                 reg_move(&CONST_INF, dest);
460                 dest->sign = (unsigned char) sign;
461         }
462         EXCEPTION(EX_ZeroDiv);
463
464         return;
465
466 }
467
468
469 /* This may be called often, so keep it lean */
470 void
471 set_precision_flag_up(void)
472 {
473         if (control_word & CW_Precision)
474                 status_word |= (SW_Precision | SW_C1);  /* The masked response */
475         else
476                 exception(EX_Precision | SW_C1);
477
478 }
479
480
481 /* This may be called often, so keep it lean */
482 void
483 set_precision_flag_down(void)
484 {
485         if (control_word & CW_Precision) {      /* The masked response */
486                 status_word &= ~SW_C1;
487                 status_word |= SW_Precision;
488         } else
489                 exception(EX_Precision);
490 }
491
492
493 int
494 denormal_operand(void)
495 {
496         if (control_word & CW_Denormal) {       /* The masked response */
497                 status_word |= SW_Denorm_Op;
498                 return 0;
499         } else {
500                 exception(EX_Denormal);
501                 return 1;
502         }
503 }
504
505
506 void
507 arith_overflow(FPU_REG * dest)
508 {
509
510         if (control_word & CW_Overflow) {
511                 char    sign;
512                 /* The masked response */
513 /* **** The response here depends upon the rounding mode */
514                 sign = dest->sign;
515                 reg_move(&CONST_INF, dest);
516                 dest->sign = sign;
517         } else {
518                 /* Subtract the magic number from the exponent */
519                 dest->exp -= (3 * (1 << 13));
520         }
521
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);
525
526         return;
527
528 }
529
530
531 void
532 arith_underflow(FPU_REG * dest)
533 {
534
535         if (control_word & CW_Underflow) {
536                 /* The masked response */
537                 if (dest->exp <= EXP_UNDER - 63)
538                         reg_move(&CONST_Z, dest);
539         } else {
540                 /* Add the magic number to the exponent */
541                 dest->exp += (3 * (1 << 13));
542         }
543
544         EXCEPTION(EX_Underflow);
545
546         return;
547 }
548
549
550 void
551 stack_overflow(void)
552 {
553
554         if (control_word & CW_Invalid) {
555                 /* The masked response */
556                 top--;
557                 reg_move(&CONST_QNaN, FPU_st0_ptr = &st(0));
558         }
559         EXCEPTION(EX_StackOver);
560
561         return;
562
563 }
564
565
566 void
567 stack_underflow(void)
568 {
569
570         if (control_word & CW_Invalid) {
571                 /* The masked response */
572                 reg_move(&CONST_QNaN, FPU_st0_ptr);
573         }
574         EXCEPTION(EX_StackUnder);
575
576         return;
577
578 }
579
580
581 void
582 stack_underflow_i(int i)
583 {
584
585         if (control_word & CW_Invalid) {
586                 /* The masked response */
587                 reg_move(&CONST_QNaN, &(st(i)));
588         }
589         EXCEPTION(EX_StackUnder);
590
591         return;
592
593 }
594
595
596 void
597 stack_underflow_pop(int i)
598 {
599
600         if (control_word & CW_Invalid) {
601                 /* The masked response */
602                 reg_move(&CONST_QNaN, &(st(i)));
603                 pop();
604         }
605         EXCEPTION(EX_StackUnder);
606
607         return;
608
609 }