Linux-2.6.12-rc2
[linux-2.6/next.git] / arch / i386 / math-emu / errors.c
bloba1b0d22f697858b335165a68acd2c487eb8ad403
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 +---------------------------------------------------------------------------*/
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 +---------------------------------------------------------------------------*/
20 #include <linux/signal.h>
22 #include <asm/uaccess.h>
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"
32 /* */
33 #undef PRINT_MESSAGES
34 /* */
37 #if 0
38 void Un_impl(void)
40 u_char byte1, FPU_modrm;
41 unsigned long address = FPU_ORIG_EIP;
43 RE_ENTRANT_CHECK_OFF;
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 while ( 1 )
50 FPU_get_user(byte1, (u_char __user *) address);
51 if ( (byte1 & 0xf8) == 0xd8 ) break;
52 printk("[%02x]", byte1);
53 address++;
55 printk("%02x ", byte1);
56 FPU_get_user(FPU_modrm, 1 + (u_char __user *) address);
58 if (FPU_modrm >= 0300)
59 printk("%02x (%02x+%d)\n", FPU_modrm, FPU_modrm & 0xf8, FPU_modrm & 7);
60 else
61 printk("/%d\n", (FPU_modrm >> 3) & 7);
63 else
65 printk("cs selector = %04x\n", FPU_CS);
68 RE_ENTRANT_CHECK_ON;
70 EXCEPTION(EX_Invalid);
73 #endif /* 0 */
77 Called for opcodes which are illegal and which are known to result in a
78 SIGILL with a real 80486.
80 void FPU_illegal(void)
82 math_abort(FPU_info,SIGILL);
87 void FPU_printall(void)
89 int i;
90 static const char *tag_desc[] = { "Valid", "Zero", "ERROR", "Empty",
91 "DeNorm", "Inf", "NaN" };
92 u_char byte1, FPU_modrm;
93 unsigned long address = FPU_ORIG_EIP;
95 RE_ENTRANT_CHECK_OFF;
96 /* No need to check access_ok(), we have previously fetched these bytes. */
97 printk("At %p:", (void *) address);
98 if ( FPU_CS == __USER_CS )
100 #define MAX_PRINTED_BYTES 20
101 for ( i = 0; i < MAX_PRINTED_BYTES; i++ )
103 FPU_get_user(byte1, (u_char __user *) address);
104 if ( (byte1 & 0xf8) == 0xd8 )
106 printk(" %02x", byte1);
107 break;
109 printk(" [%02x]", byte1);
110 address++;
112 if ( i == MAX_PRINTED_BYTES )
113 printk(" [more..]\n");
114 else
116 FPU_get_user(FPU_modrm, 1 + (u_char __user *) address);
118 if (FPU_modrm >= 0300)
119 printk(" %02x (%02x+%d)\n", FPU_modrm, FPU_modrm & 0xf8, FPU_modrm & 7);
120 else
121 printk(" /%d, mod=%d rm=%d\n",
122 (FPU_modrm >> 3) & 7, (FPU_modrm >> 6) & 3, FPU_modrm & 7);
125 else
127 printk("%04x\n", FPU_CS);
130 partial_status = status_word();
132 #ifdef DEBUGGING
133 if ( partial_status & SW_Backward ) printk("SW: backward compatibility\n");
134 if ( partial_status & SW_C3 ) printk("SW: condition bit 3\n");
135 if ( partial_status & SW_C2 ) printk("SW: condition bit 2\n");
136 if ( partial_status & SW_C1 ) printk("SW: condition bit 1\n");
137 if ( partial_status & SW_C0 ) printk("SW: condition bit 0\n");
138 if ( partial_status & SW_Summary ) printk("SW: exception summary\n");
139 if ( partial_status & SW_Stack_Fault ) printk("SW: stack fault\n");
140 if ( partial_status & SW_Precision ) printk("SW: loss of precision\n");
141 if ( partial_status & SW_Underflow ) printk("SW: underflow\n");
142 if ( partial_status & SW_Overflow ) printk("SW: overflow\n");
143 if ( partial_status & SW_Zero_Div ) printk("SW: divide by zero\n");
144 if ( partial_status & SW_Denorm_Op ) printk("SW: denormalized operand\n");
145 if ( partial_status & SW_Invalid ) printk("SW: invalid operation\n");
146 #endif /* DEBUGGING */
148 printk(" SW: b=%d st=%ld es=%d sf=%d cc=%d%d%d%d ef=%d%d%d%d%d%d\n",
149 partial_status & 0x8000 ? 1 : 0, /* busy */
150 (partial_status & 0x3800) >> 11, /* stack top pointer */
151 partial_status & 0x80 ? 1 : 0, /* Error summary status */
152 partial_status & 0x40 ? 1 : 0, /* Stack flag */
153 partial_status & SW_C3?1:0, partial_status & SW_C2?1:0, /* cc */
154 partial_status & SW_C1?1:0, partial_status & SW_C0?1:0, /* cc */
155 partial_status & SW_Precision?1:0, partial_status & SW_Underflow?1:0,
156 partial_status & SW_Overflow?1:0, partial_status & SW_Zero_Div?1:0,
157 partial_status & SW_Denorm_Op?1:0, partial_status & SW_Invalid?1:0);
159 printk(" CW: ic=%d rc=%ld%ld pc=%ld%ld iem=%d ef=%d%d%d%d%d%d\n",
160 control_word & 0x1000 ? 1 : 0,
161 (control_word & 0x800) >> 11, (control_word & 0x400) >> 10,
162 (control_word & 0x200) >> 9, (control_word & 0x100) >> 8,
163 control_word & 0x80 ? 1 : 0,
164 control_word & SW_Precision?1:0, control_word & SW_Underflow?1:0,
165 control_word & SW_Overflow?1:0, control_word & SW_Zero_Div?1:0,
166 control_word & SW_Denorm_Op?1:0, control_word & SW_Invalid?1:0);
168 for ( i = 0; i < 8; i++ )
170 FPU_REG *r = &st(i);
171 u_char tagi = FPU_gettagi(i);
172 switch (tagi)
174 case TAG_Empty:
175 continue;
176 break;
177 case TAG_Zero:
178 case TAG_Special:
179 tagi = FPU_Special(r);
180 case TAG_Valid:
181 printk("st(%d) %c .%04lx %04lx %04lx %04lx e%+-6d ", i,
182 getsign(r) ? '-' : '+',
183 (long)(r->sigh >> 16),
184 (long)(r->sigh & 0xFFFF),
185 (long)(r->sigl >> 16),
186 (long)(r->sigl & 0xFFFF),
187 exponent(r) - EXP_BIAS + 1);
188 break;
189 default:
190 printk("Whoops! Error in errors.c: tag%d is %d ", i, tagi);
191 continue;
192 break;
194 printk("%s\n", tag_desc[(int) (unsigned) tagi]);
197 RE_ENTRANT_CHECK_ON;
201 static struct {
202 int type;
203 const char *name;
204 } exception_names[] = {
205 { EX_StackOver, "stack overflow" },
206 { EX_StackUnder, "stack underflow" },
207 { EX_Precision, "loss of precision" },
208 { EX_Underflow, "underflow" },
209 { EX_Overflow, "overflow" },
210 { EX_ZeroDiv, "divide by zero" },
211 { EX_Denormal, "denormalized operand" },
212 { EX_Invalid, "invalid operation" },
213 { EX_INTERNAL, "INTERNAL BUG in "FPU_VERSION },
214 { 0, NULL }
218 EX_INTERNAL is always given with a code which indicates where the
219 error was detected.
221 Internal error types:
222 0x14 in fpu_etc.c
223 0x1nn in a *.c file:
224 0x101 in reg_add_sub.c
225 0x102 in reg_mul.c
226 0x104 in poly_atan.c
227 0x105 in reg_mul.c
228 0x107 in fpu_trig.c
229 0x108 in reg_compare.c
230 0x109 in reg_compare.c
231 0x110 in reg_add_sub.c
232 0x111 in fpe_entry.c
233 0x112 in fpu_trig.c
234 0x113 in errors.c
235 0x115 in fpu_trig.c
236 0x116 in fpu_trig.c
237 0x117 in fpu_trig.c
238 0x118 in fpu_trig.c
239 0x119 in fpu_trig.c
240 0x120 in poly_atan.c
241 0x121 in reg_compare.c
242 0x122 in reg_compare.c
243 0x123 in reg_compare.c
244 0x125 in fpu_trig.c
245 0x126 in fpu_entry.c
246 0x127 in poly_2xm1.c
247 0x128 in fpu_entry.c
248 0x129 in fpu_entry.c
249 0x130 in get_address.c
250 0x131 in get_address.c
251 0x132 in get_address.c
252 0x133 in get_address.c
253 0x140 in load_store.c
254 0x141 in load_store.c
255 0x150 in poly_sin.c
256 0x151 in poly_sin.c
257 0x160 in reg_ld_str.c
258 0x161 in reg_ld_str.c
259 0x162 in reg_ld_str.c
260 0x163 in reg_ld_str.c
261 0x164 in reg_ld_str.c
262 0x170 in fpu_tags.c
263 0x171 in fpu_tags.c
264 0x172 in fpu_tags.c
265 0x180 in reg_convert.c
266 0x2nn in an *.S file:
267 0x201 in reg_u_add.S
268 0x202 in reg_u_div.S
269 0x203 in reg_u_div.S
270 0x204 in reg_u_div.S
271 0x205 in reg_u_mul.S
272 0x206 in reg_u_sub.S
273 0x207 in wm_sqrt.S
274 0x208 in reg_div.S
275 0x209 in reg_u_sub.S
276 0x210 in reg_u_sub.S
277 0x211 in reg_u_sub.S
278 0x212 in reg_u_sub.S
279 0x213 in wm_sqrt.S
280 0x214 in wm_sqrt.S
281 0x215 in wm_sqrt.S
282 0x220 in reg_norm.S
283 0x221 in reg_norm.S
284 0x230 in reg_round.S
285 0x231 in reg_round.S
286 0x232 in reg_round.S
287 0x233 in reg_round.S
288 0x234 in reg_round.S
289 0x235 in reg_round.S
290 0x236 in reg_round.S
291 0x240 in div_Xsig.S
292 0x241 in div_Xsig.S
293 0x242 in div_Xsig.S
296 asmlinkage void FPU_exception(int n)
298 int i, int_type;
300 int_type = 0; /* Needed only to stop compiler warnings */
301 if ( n & EX_INTERNAL )
303 int_type = n - EX_INTERNAL;
304 n = EX_INTERNAL;
305 /* Set lots of exception bits! */
306 partial_status |= (SW_Exc_Mask | SW_Summary | SW_Backward);
308 else
310 /* Extract only the bits which we use to set the status word */
311 n &= (SW_Exc_Mask);
312 /* Set the corresponding exception bit */
313 partial_status |= n;
314 /* Set summary bits iff exception isn't masked */
315 if ( partial_status & ~control_word & CW_Exceptions )
316 partial_status |= (SW_Summary | SW_Backward);
317 if ( n & (SW_Stack_Fault | EX_Precision) )
319 if ( !(n & SW_C1) )
320 /* This bit distinguishes over- from underflow for a stack fault,
321 and roundup from round-down for precision loss. */
322 partial_status &= ~SW_C1;
326 RE_ENTRANT_CHECK_OFF;
327 if ( (~control_word & n & CW_Exceptions) || (n == EX_INTERNAL) )
329 #ifdef PRINT_MESSAGES
330 /* My message from the sponsor */
331 printk(FPU_VERSION" "__DATE__" (C) W. Metzenthen.\n");
332 #endif /* PRINT_MESSAGES */
334 /* Get a name string for error reporting */
335 for (i=0; exception_names[i].type; i++)
336 if ( (exception_names[i].type & n) == exception_names[i].type )
337 break;
339 if (exception_names[i].type)
341 #ifdef PRINT_MESSAGES
342 printk("FP Exception: %s!\n", exception_names[i].name);
343 #endif /* PRINT_MESSAGES */
345 else
346 printk("FPU emulator: Unknown Exception: 0x%04x!\n", n);
348 if ( n == EX_INTERNAL )
350 printk("FPU emulator: Internal error type 0x%04x\n", int_type);
351 FPU_printall();
353 #ifdef PRINT_MESSAGES
354 else
355 FPU_printall();
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.
364 RE_ENTRANT_CHECK_ON;
366 #ifdef __DEBUG__
367 math_abort(FPU_info,SIGFPE);
368 #endif /* __DEBUG__ */
373 /* Real operation attempted on a NaN. */
374 /* Returns < 0 if the exception is unmasked */
375 int real_1op_NaN(FPU_REG *a)
377 int signalling, isNaN;
379 isNaN = (exponent(a) == EXP_OVER) && (a->sigh & 0x80000000);
381 /* The default result for the case of two "equal" NaNs (signs may
382 differ) is chosen to reproduce 80486 behaviour */
383 signalling = isNaN && !(a->sigh & 0x40000000);
385 if ( !signalling )
387 if ( !isNaN ) /* pseudo-NaN, or other unsupported? */
389 if ( control_word & CW_Invalid )
391 /* Masked response */
392 reg_copy(&CONST_QNaN, a);
394 EXCEPTION(EX_Invalid);
395 return (!(control_word & CW_Invalid) ? FPU_Exception : 0) | TAG_Special;
397 return TAG_Special;
400 if ( control_word & CW_Invalid )
402 /* The masked response */
403 if ( !(a->sigh & 0x80000000) ) /* pseudo-NaN ? */
405 reg_copy(&CONST_QNaN, a);
407 /* ensure a Quiet NaN */
408 a->sigh |= 0x40000000;
411 EXCEPTION(EX_Invalid);
413 return (!(control_word & CW_Invalid) ? FPU_Exception : 0) | TAG_Special;
417 /* Real operation attempted on two operands, one a NaN. */
418 /* Returns < 0 if the exception is unmasked */
419 int real_2op_NaN(FPU_REG const *b, u_char tagb,
420 int deststnr,
421 FPU_REG const *defaultNaN)
423 FPU_REG *dest = &st(deststnr);
424 FPU_REG const *a = dest;
425 u_char taga = FPU_gettagi(deststnr);
426 FPU_REG const *x;
427 int signalling, unsupported;
429 if ( taga == TAG_Special )
430 taga = FPU_Special(a);
431 if ( tagb == TAG_Special )
432 tagb = FPU_Special(b);
434 /* TW_NaN is also used for unsupported data types. */
435 unsupported = ((taga == TW_NaN)
436 && !((exponent(a) == EXP_OVER) && (a->sigh & 0x80000000)))
437 || ((tagb == TW_NaN)
438 && !((exponent(b) == EXP_OVER) && (b->sigh & 0x80000000)));
439 if ( unsupported )
441 if ( control_word & CW_Invalid )
443 /* Masked response */
444 FPU_copy_to_regi(&CONST_QNaN, TAG_Special, deststnr);
446 EXCEPTION(EX_Invalid);
447 return (!(control_word & CW_Invalid) ? FPU_Exception : 0) | TAG_Special;
450 if (taga == TW_NaN)
452 x = a;
453 if (tagb == TW_NaN)
455 signalling = !(a->sigh & b->sigh & 0x40000000);
456 if ( significand(b) > significand(a) )
457 x = b;
458 else if ( significand(b) == significand(a) )
460 /* The default result for the case of two "equal" NaNs (signs may
461 differ) is chosen to reproduce 80486 behaviour */
462 x = defaultNaN;
465 else
467 /* return the quiet version of the NaN in a */
468 signalling = !(a->sigh & 0x40000000);
471 else
472 #ifdef PARANOID
473 if (tagb == TW_NaN)
474 #endif /* PARANOID */
476 signalling = !(b->sigh & 0x40000000);
477 x = b;
479 #ifdef PARANOID
480 else
482 signalling = 0;
483 EXCEPTION(EX_INTERNAL|0x113);
484 x = &CONST_QNaN;
486 #endif /* PARANOID */
488 if ( (!signalling) || (control_word & CW_Invalid) )
490 if ( ! x )
491 x = b;
493 if ( !(x->sigh & 0x80000000) ) /* pseudo-NaN ? */
494 x = &CONST_QNaN;
496 FPU_copy_to_regi(x, TAG_Special, deststnr);
498 if ( !signalling )
499 return TAG_Special;
501 /* ensure a Quiet NaN */
502 dest->sigh |= 0x40000000;
505 EXCEPTION(EX_Invalid);
507 return (!(control_word & CW_Invalid) ? FPU_Exception : 0) | TAG_Special;
511 /* Invalid arith operation on Valid registers */
512 /* Returns < 0 if the exception is unmasked */
513 asmlinkage int arith_invalid(int deststnr)
516 EXCEPTION(EX_Invalid);
518 if ( control_word & CW_Invalid )
520 /* The masked response */
521 FPU_copy_to_regi(&CONST_QNaN, TAG_Special, deststnr);
524 return (!(control_word & CW_Invalid) ? FPU_Exception : 0) | TAG_Valid;
529 /* Divide a finite number by zero */
530 asmlinkage int FPU_divide_by_zero(int deststnr, u_char sign)
532 FPU_REG *dest = &st(deststnr);
533 int tag = TAG_Valid;
535 if ( control_word & CW_ZeroDiv )
537 /* The masked response */
538 FPU_copy_to_regi(&CONST_INF, TAG_Special, deststnr);
539 setsign(dest, sign);
540 tag = TAG_Special;
543 EXCEPTION(EX_ZeroDiv);
545 return (!(control_word & CW_ZeroDiv) ? FPU_Exception : 0) | tag;
550 /* This may be called often, so keep it lean */
551 int set_precision_flag(int flags)
553 if ( control_word & CW_Precision )
555 partial_status &= ~(SW_C1 & flags);
556 partial_status |= flags; /* The masked response */
557 return 0;
559 else
561 EXCEPTION(flags);
562 return 1;
567 /* This may be called often, so keep it lean */
568 asmlinkage void set_precision_flag_up(void)
570 if ( control_word & CW_Precision )
571 partial_status |= (SW_Precision | SW_C1); /* The masked response */
572 else
573 EXCEPTION(EX_Precision | SW_C1);
577 /* This may be called often, so keep it lean */
578 asmlinkage void set_precision_flag_down(void)
580 if ( control_word & CW_Precision )
581 { /* The masked response */
582 partial_status &= ~SW_C1;
583 partial_status |= SW_Precision;
585 else
586 EXCEPTION(EX_Precision);
590 asmlinkage int denormal_operand(void)
592 if ( control_word & CW_Denormal )
593 { /* The masked response */
594 partial_status |= SW_Denorm_Op;
595 return TAG_Special;
597 else
599 EXCEPTION(EX_Denormal);
600 return TAG_Special | FPU_Exception;
605 asmlinkage int arith_overflow(FPU_REG *dest)
607 int tag = TAG_Valid;
609 if ( control_word & CW_Overflow )
611 /* The masked response */
612 /* ###### The response here depends upon the rounding mode */
613 reg_copy(&CONST_INF, dest);
614 tag = TAG_Special;
616 else
618 /* Subtract the magic number from the exponent */
619 addexponent(dest, (-3 * (1 << 13)));
622 EXCEPTION(EX_Overflow);
623 if ( control_word & CW_Overflow )
625 /* The overflow exception is masked. */
626 /* By definition, precision is lost.
627 The roundup bit (C1) is also set because we have
628 "rounded" upwards to Infinity. */
629 EXCEPTION(EX_Precision | SW_C1);
630 return tag;
633 return tag;
638 asmlinkage int arith_underflow(FPU_REG *dest)
640 int tag = TAG_Valid;
642 if ( control_word & CW_Underflow )
644 /* The masked response */
645 if ( exponent16(dest) <= EXP_UNDER - 63 )
647 reg_copy(&CONST_Z, dest);
648 partial_status &= ~SW_C1; /* Round down. */
649 tag = TAG_Zero;
651 else
653 stdexp(dest);
656 else
658 /* Add the magic number to the exponent. */
659 addexponent(dest, (3 * (1 << 13)) + EXTENDED_Ebias);
662 EXCEPTION(EX_Underflow);
663 if ( control_word & CW_Underflow )
665 /* The underflow exception is masked. */
666 EXCEPTION(EX_Precision);
667 return tag;
670 return tag;
675 void FPU_stack_overflow(void)
678 if ( control_word & CW_Invalid )
680 /* The masked response */
681 top--;
682 FPU_copy_to_reg0(&CONST_QNaN, TAG_Special);
685 EXCEPTION(EX_StackOver);
687 return;
692 void FPU_stack_underflow(void)
695 if ( control_word & CW_Invalid )
697 /* The masked response */
698 FPU_copy_to_reg0(&CONST_QNaN, TAG_Special);
701 EXCEPTION(EX_StackUnder);
703 return;
708 void FPU_stack_underflow_i(int i)
711 if ( control_word & CW_Invalid )
713 /* The masked response */
714 FPU_copy_to_regi(&CONST_QNaN, TAG_Special, i);
717 EXCEPTION(EX_StackUnder);
719 return;
724 void FPU_stack_underflow_pop(int i)
727 if ( control_word & CW_Invalid )
729 /* The masked response */
730 FPU_copy_to_regi(&CONST_QNaN, TAG_Special, i);
731 FPU_pop();
734 EXCEPTION(EX_StackUnder);
736 return;