ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / i386 / math-emu / errors.c
1 /*---------------------------------------------------------------------------+
2  |  errors.c                                                                 |
3  |                                                                           |
4  |  The error handling functions for wm-FPU-emu                              |
5  |                                                                           |
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                |
9  |                                                                           |
10  |                                                                           |
11  +---------------------------------------------------------------------------*/
12
13 /*---------------------------------------------------------------------------+
14  | Note:                                                                     |
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  +---------------------------------------------------------------------------*/
19
20 #include <linux/signal.h>
21
22 #include <asm/uaccess.h>
23
24 #include "fpu_emu.h"
25 #include "fpu_system.h"
26 #include "exception.h"
27 #include "status_w.h"
28 #include "control_w.h"
29 #include "reg_constant.h"
30 #include "version.h"
31
32 /* */
33 #undef PRINT_MESSAGES
34 /* */
35
36
37 void Un_impl(void)
38 {
39   u_char byte1, FPU_modrm;
40   unsigned long address = FPU_ORIG_EIP;
41
42   RE_ENTRANT_CHECK_OFF;
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 )
46     {
47       while ( 1 )
48         {
49           FPU_get_user(byte1, (u_char *) address);
50           if ( (byte1 & 0xf8) == 0xd8 ) break;
51           printk("[%02x]", byte1);
52           address++;
53         }
54       printk("%02x ", byte1);
55       FPU_get_user(FPU_modrm, 1 + (u_char *) address);
56       
57       if (FPU_modrm >= 0300)
58         printk("%02x (%02x+%d)\n", FPU_modrm, FPU_modrm & 0xf8, FPU_modrm & 7);
59       else
60         printk("/%d\n", (FPU_modrm >> 3) & 7);
61     }
62   else
63     {
64       printk("cs selector = %04x\n", FPU_CS);
65     }
66
67   RE_ENTRANT_CHECK_ON;
68
69   EXCEPTION(EX_Invalid);
70
71 }
72
73
74 /*
75    Called for opcodes which are illegal and which are known to result in a
76    SIGILL with a real 80486.
77    */
78 void FPU_illegal(void)
79 {
80   math_abort(FPU_info,SIGILL);
81 }
82
83
84
85 void FPU_printall(void)
86 {
87   int i;
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;
92
93   RE_ENTRANT_CHECK_OFF;
94   /* No need to verify_area(), we have previously fetched these bytes. */
95   printk("At %p:", (void *) address);
96   if ( FPU_CS == __USER_CS )
97     {
98 #define MAX_PRINTED_BYTES 20
99       for ( i = 0; i < MAX_PRINTED_BYTES; i++ )
100         {
101           FPU_get_user(byte1, (u_char *) address);
102           if ( (byte1 & 0xf8) == 0xd8 )
103             {
104               printk(" %02x", byte1);
105               break;
106             }
107           printk(" [%02x]", byte1);
108           address++;
109         }
110       if ( i == MAX_PRINTED_BYTES )
111         printk(" [more..]\n");
112       else
113         {
114           FPU_get_user(FPU_modrm, 1 + (u_char *) address);
115           
116           if (FPU_modrm >= 0300)
117             printk(" %02x (%02x+%d)\n", FPU_modrm, FPU_modrm & 0xf8, FPU_modrm & 7);
118           else
119             printk(" /%d, mod=%d rm=%d\n",
120                    (FPU_modrm >> 3) & 7, (FPU_modrm >> 6) & 3, FPU_modrm & 7);
121         }
122     }
123   else
124     {
125       printk("%04x\n", FPU_CS);
126     }
127
128   partial_status = status_word();
129
130 #ifdef DEBUGGING
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");
144 #endif /* DEBUGGING */
145
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);
156   
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);
165
166   for ( i = 0; i < 8; i++ )
167     {
168       FPU_REG *r = &st(i);
169       u_char tagi = FPU_gettagi(i);
170       switch (tagi)
171         {
172         case TAG_Empty:
173           continue;
174           break;
175         case TAG_Zero:
176         case TAG_Special:
177           tagi = FPU_Special(r);
178         case TAG_Valid:
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);
186           break;
187         default:
188           printk("Whoops! Error in errors.c: tag%d is %d ", i, tagi);
189           continue;
190           break;
191         }
192       printk("%s\n", tag_desc[(int) (unsigned) tagi]);
193     }
194
195   RE_ENTRANT_CHECK_ON;
196
197 }
198
199 static struct {
200   int type;
201   const char *name;
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 },
212   { 0, NULL }
213 };
214
215 /*
216  EX_INTERNAL is always given with a code which indicates where the
217  error was detected.
218
219  Internal error types:
220        0x14   in fpu_etc.c
221        0x1nn  in a *.c file:
222               0x101  in reg_add_sub.c
223               0x102  in reg_mul.c
224               0x104  in poly_atan.c
225               0x105  in reg_mul.c
226               0x107  in fpu_trig.c
227               0x108  in reg_compare.c
228               0x109  in reg_compare.c
229               0x110  in reg_add_sub.c
230               0x111  in fpe_entry.c
231               0x112  in fpu_trig.c
232               0x113  in errors.c
233               0x115  in fpu_trig.c
234               0x116  in fpu_trig.c
235               0x117  in fpu_trig.c
236               0x118  in fpu_trig.c
237               0x119  in fpu_trig.c
238               0x120  in poly_atan.c
239               0x121  in reg_compare.c
240               0x122  in reg_compare.c
241               0x123  in reg_compare.c
242               0x125  in fpu_trig.c
243               0x126  in fpu_entry.c
244               0x127  in poly_2xm1.c
245               0x128  in fpu_entry.c
246               0x129  in fpu_entry.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
253               0x150  in poly_sin.c
254               0x151  in poly_sin.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
260               0x170  in fpu_tags.c
261               0x171  in fpu_tags.c
262               0x172  in fpu_tags.c
263               0x180  in reg_convert.c
264        0x2nn  in an *.S file:
265               0x201  in reg_u_add.S
266               0x202  in reg_u_div.S
267               0x203  in reg_u_div.S
268               0x204  in reg_u_div.S
269               0x205  in reg_u_mul.S
270               0x206  in reg_u_sub.S
271               0x207  in wm_sqrt.S
272               0x208  in reg_div.S
273               0x209  in reg_u_sub.S
274               0x210  in reg_u_sub.S
275               0x211  in reg_u_sub.S
276               0x212  in reg_u_sub.S
277               0x213  in wm_sqrt.S
278               0x214  in wm_sqrt.S
279               0x215  in wm_sqrt.S
280               0x220  in reg_norm.S
281               0x221  in reg_norm.S
282               0x230  in reg_round.S
283               0x231  in reg_round.S
284               0x232  in reg_round.S
285               0x233  in reg_round.S
286               0x234  in reg_round.S
287               0x235  in reg_round.S
288               0x236  in reg_round.S
289               0x240  in div_Xsig.S
290               0x241  in div_Xsig.S
291               0x242  in div_Xsig.S
292  */
293
294 asmlinkage void FPU_exception(int n)
295 {
296   int i, int_type;
297
298   int_type = 0;         /* Needed only to stop compiler warnings */
299   if ( n & EX_INTERNAL )
300     {
301       int_type = n - EX_INTERNAL;
302       n = EX_INTERNAL;
303       /* Set lots of exception bits! */
304       partial_status |= (SW_Exc_Mask | SW_Summary | SW_Backward);
305     }
306   else
307     {
308       /* Extract only the bits which we use to set the status word */
309       n &= (SW_Exc_Mask);
310       /* Set the corresponding exception bit */
311       partial_status |= n;
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) )
316         {
317           if ( !(n & SW_C1) )
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;
321         }
322     }
323
324   RE_ENTRANT_CHECK_OFF;
325   if ( (~control_word & n & CW_Exceptions) || (n == EX_INTERNAL) )
326     {
327 #ifdef PRINT_MESSAGES
328       /* My message from the sponsor */
329       printk(FPU_VERSION" "__DATE__" (C) W. Metzenthen.\n");
330 #endif /* PRINT_MESSAGES */
331       
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 )
335           break;
336       
337       if (exception_names[i].type)
338         {
339 #ifdef PRINT_MESSAGES
340           printk("FP Exception: %s!\n", exception_names[i].name);
341 #endif /* PRINT_MESSAGES */
342         }
343       else
344         printk("FPU emulator: Unknown Exception: 0x%04x!\n", n);
345       
346       if ( n == EX_INTERNAL )
347         {
348           printk("FPU emulator: Internal error type 0x%04x\n", int_type);
349           FPU_printall();
350         }
351 #ifdef PRINT_MESSAGES
352       else
353         FPU_printall();
354 #endif /* PRINT_MESSAGES */
355
356       /*
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.
360        */
361     }
362   RE_ENTRANT_CHECK_ON;
363
364 #ifdef __DEBUG__
365   math_abort(FPU_info,SIGFPE);
366 #endif /* __DEBUG__ */
367
368 }
369
370
371 /* Real operation attempted on a NaN. */
372 /* Returns < 0 if the exception is unmasked */
373 int real_1op_NaN(FPU_REG *a)
374 {
375   int signalling, isNaN;
376
377   isNaN = (exponent(a) == EXP_OVER) && (a->sigh & 0x80000000);
378
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);
382
383   if ( !signalling )
384     {
385       if ( !isNaN )  /* pseudo-NaN, or other unsupported? */
386         {
387           if ( control_word & CW_Invalid )
388             {
389               /* Masked response */
390               reg_copy(&CONST_QNaN, a);
391             }
392           EXCEPTION(EX_Invalid);
393           return (!(control_word & CW_Invalid) ? FPU_Exception : 0) | TAG_Special;
394         }
395       return TAG_Special;
396     }
397
398   if ( control_word & CW_Invalid )
399     {
400       /* The masked response */
401       if ( !(a->sigh & 0x80000000) )  /* pseudo-NaN ? */
402         {
403           reg_copy(&CONST_QNaN, a);
404         }
405       /* ensure a Quiet NaN */
406       a->sigh |= 0x40000000;
407     }
408
409   EXCEPTION(EX_Invalid);
410
411   return (!(control_word & CW_Invalid) ? FPU_Exception : 0) | TAG_Special;
412 }
413
414
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,
418                  int deststnr,
419                  FPU_REG const *defaultNaN)
420 {
421   FPU_REG *dest = &st(deststnr);
422   FPU_REG const *a = dest;
423   u_char taga = FPU_gettagi(deststnr);
424   FPU_REG const *x;
425   int signalling, unsupported;
426
427   if ( taga == TAG_Special )
428     taga = FPU_Special(a);
429   if ( tagb == TAG_Special )
430     tagb = FPU_Special(b);
431
432   /* TW_NaN is also used for unsupported data types. */
433   unsupported = ((taga == TW_NaN)
434                  && !((exponent(a) == EXP_OVER) && (a->sigh & 0x80000000)))
435     || ((tagb == TW_NaN)
436         && !((exponent(b) == EXP_OVER) && (b->sigh & 0x80000000)));
437   if ( unsupported )
438     {
439       if ( control_word & CW_Invalid )
440         {
441           /* Masked response */
442           FPU_copy_to_regi(&CONST_QNaN, TAG_Special, deststnr);
443         }
444       EXCEPTION(EX_Invalid);
445       return (!(control_word & CW_Invalid) ? FPU_Exception : 0) | TAG_Special;
446     }
447
448   if (taga == TW_NaN)
449     {
450       x = a;
451       if (tagb == TW_NaN)
452         {
453           signalling = !(a->sigh & b->sigh & 0x40000000);
454           if ( significand(b) > significand(a) )
455             x = b;
456           else if ( significand(b) == significand(a) )
457             {
458               /* The default result for the case of two "equal" NaNs (signs may
459                  differ) is chosen to reproduce 80486 behaviour */
460               x = defaultNaN;
461             }
462         }
463       else
464         {
465           /* return the quiet version of the NaN in a */
466           signalling = !(a->sigh & 0x40000000);
467         }
468     }
469   else
470 #ifdef PARANOID
471     if (tagb == TW_NaN)
472 #endif /* PARANOID */
473     {
474       signalling = !(b->sigh & 0x40000000);
475       x = b;
476     }
477 #ifdef PARANOID
478   else
479     {
480       signalling = 0;
481       EXCEPTION(EX_INTERNAL|0x113);
482       x = &CONST_QNaN;
483     }
484 #endif /* PARANOID */
485
486   if ( (!signalling) || (control_word & CW_Invalid) )
487     {
488       if ( ! x )
489         x = b;
490
491       if ( !(x->sigh & 0x80000000) )  /* pseudo-NaN ? */
492         x = &CONST_QNaN;
493
494       FPU_copy_to_regi(x, TAG_Special, deststnr);
495
496       if ( !signalling )
497         return TAG_Special;
498
499       /* ensure a Quiet NaN */
500       dest->sigh |= 0x40000000;
501     }
502
503   EXCEPTION(EX_Invalid);
504
505   return (!(control_word & CW_Invalid) ? FPU_Exception : 0) | TAG_Special;
506 }
507
508
509 /* Invalid arith operation on Valid registers */
510 /* Returns < 0 if the exception is unmasked */
511 asmlinkage int arith_invalid(int deststnr)
512 {
513
514   EXCEPTION(EX_Invalid);
515   
516   if ( control_word & CW_Invalid )
517     {
518       /* The masked response */
519       FPU_copy_to_regi(&CONST_QNaN, TAG_Special, deststnr);
520     }
521   
522   return (!(control_word & CW_Invalid) ? FPU_Exception : 0) | TAG_Valid;
523
524 }
525
526
527 /* Divide a finite number by zero */
528 asmlinkage int FPU_divide_by_zero(int deststnr, u_char sign)
529 {
530   FPU_REG *dest = &st(deststnr);
531   int tag = TAG_Valid;
532
533   if ( control_word & CW_ZeroDiv )
534     {
535       /* The masked response */
536       FPU_copy_to_regi(&CONST_INF, TAG_Special, deststnr);
537       setsign(dest, sign);
538       tag = TAG_Special;
539     }
540  
541   EXCEPTION(EX_ZeroDiv);
542
543   return (!(control_word & CW_ZeroDiv) ? FPU_Exception : 0) | tag;
544
545 }
546
547
548 /* This may be called often, so keep it lean */
549 int set_precision_flag(int flags)
550 {
551   if ( control_word & CW_Precision )
552     {
553       partial_status &= ~(SW_C1 & flags);
554       partial_status |= flags;   /* The masked response */
555       return 0;
556     }
557   else
558     {
559       EXCEPTION(flags);
560       return 1;
561     }
562 }
563
564
565 /* This may be called often, so keep it lean */
566 asmlinkage void set_precision_flag_up(void)
567 {
568   if ( control_word & CW_Precision )
569     partial_status |= (SW_Precision | SW_C1);   /* The masked response */
570   else
571     EXCEPTION(EX_Precision | SW_C1);
572 }
573
574
575 /* This may be called often, so keep it lean */
576 asmlinkage void set_precision_flag_down(void)
577 {
578   if ( control_word & CW_Precision )
579     {   /* The masked response */
580       partial_status &= ~SW_C1;
581       partial_status |= SW_Precision;
582     }
583   else
584     EXCEPTION(EX_Precision);
585 }
586
587
588 asmlinkage int denormal_operand(void)
589 {
590   if ( control_word & CW_Denormal )
591     {   /* The masked response */
592       partial_status |= SW_Denorm_Op;
593       return TAG_Special;
594     }
595   else
596     {
597       EXCEPTION(EX_Denormal);
598       return TAG_Special | FPU_Exception;
599     }
600 }
601
602
603 asmlinkage int arith_overflow(FPU_REG *dest)
604 {
605   int tag = TAG_Valid;
606
607   if ( control_word & CW_Overflow )
608     {
609       /* The masked response */
610 /* ###### The response here depends upon the rounding mode */
611       reg_copy(&CONST_INF, dest);
612       tag = TAG_Special;
613     }
614   else
615     {
616       /* Subtract the magic number from the exponent */
617       addexponent(dest, (-3 * (1 << 13)));
618     }
619
620   EXCEPTION(EX_Overflow);
621   if ( control_word & CW_Overflow )
622     {
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);
628       return tag;
629     }
630
631   return tag;
632
633 }
634
635
636 asmlinkage int arith_underflow(FPU_REG *dest)
637 {
638   int tag = TAG_Valid;
639
640   if ( control_word & CW_Underflow )
641     {
642       /* The masked response */
643       if ( exponent16(dest) <= EXP_UNDER - 63 )
644         {
645           reg_copy(&CONST_Z, dest);
646           partial_status &= ~SW_C1;       /* Round down. */
647           tag = TAG_Zero;
648         }
649       else
650         {
651           stdexp(dest);
652         }
653     }
654   else
655     {
656       /* Add the magic number to the exponent. */
657       addexponent(dest, (3 * (1 << 13)) + EXTENDED_Ebias);
658     }
659
660   EXCEPTION(EX_Underflow);
661   if ( control_word & CW_Underflow )
662     {
663       /* The underflow exception is masked. */
664       EXCEPTION(EX_Precision);
665       return tag;
666     }
667
668   return tag;
669
670 }
671
672
673 void FPU_stack_overflow(void)
674 {
675
676  if ( control_word & CW_Invalid )
677     {
678       /* The masked response */
679       top--;
680       FPU_copy_to_reg0(&CONST_QNaN, TAG_Special);
681     }
682
683   EXCEPTION(EX_StackOver);
684
685   return;
686
687 }
688
689
690 void FPU_stack_underflow(void)
691 {
692
693  if ( control_word & CW_Invalid )
694     {
695       /* The masked response */
696       FPU_copy_to_reg0(&CONST_QNaN, TAG_Special);
697     }
698
699   EXCEPTION(EX_StackUnder);
700
701   return;
702
703 }
704
705
706 void FPU_stack_underflow_i(int i)
707 {
708
709  if ( control_word & CW_Invalid )
710     {
711       /* The masked response */
712       FPU_copy_to_regi(&CONST_QNaN, TAG_Special, i);
713     }
714
715   EXCEPTION(EX_StackUnder);
716
717   return;
718
719 }
720
721
722 void FPU_stack_underflow_pop(int i)
723 {
724
725  if ( control_word & CW_Invalid )
726     {
727       /* The masked response */
728       FPU_copy_to_regi(&CONST_QNaN, TAG_Special, i);
729       FPU_pop();
730     }
731
732   EXCEPTION(EX_StackUnder);
733
734   return;
735
736 }
737