More Makefile cleanups, otherwise mainly noticeable are the netfilter fix
[davej-history.git] / arch / i386 / math-emu / errors.c
blobaff145f8a2773421718bda43a666808d9d2e0d7f
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 void Un_impl(void)
39 u_char byte1, FPU_modrm;
40 unsigned long address = FPU_ORIG_EIP;
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 )
47 while ( 1 )
49 FPU_get_user(byte1, (u_char *) address);
50 if ( (byte1 & 0xf8) == 0xd8 ) break;
51 printk("[%02x]", byte1);
52 address++;
54 printk("%02x ", byte1);
55 FPU_get_user(FPU_modrm, 1 + (u_char *) address);
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);
62 else
64 printk("cs selector = %04x\n", FPU_CS);
67 RE_ENTRANT_CHECK_ON;
69 EXCEPTION(EX_Invalid);
75 Called for opcodes which are illegal and which are known to result in a
76 SIGILL with a real 80486.
78 void FPU_illegal(void)
80 math_abort(FPU_info,SIGILL);
85 void FPU_printall(void)
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;
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 )
98 #define MAX_PRINTED_BYTES 20
99 for ( i = 0; i < MAX_PRINTED_BYTES; i++ )
101 FPU_get_user(byte1, (u_char *) address);
102 if ( (byte1 & 0xf8) == 0xd8 )
104 printk(" %02x", byte1);
105 break;
107 printk(" [%02x]", byte1);
108 address++;
110 if ( i == MAX_PRINTED_BYTES )
111 printk(" [more..]\n");
112 else
114 FPU_get_user(FPU_modrm, 1 + (u_char *) address);
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);
123 else
125 printk("%04x\n", FPU_CS);
128 partial_status = status_word();
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
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);
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);
166 for ( i = 0; i < 8; i++ )
168 FPU_REG *r = &st(i);
169 u_char tagi = FPU_gettagi(i);
170 switch (tagi)
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;
192 printk("%s\n", tag_desc[(int) (unsigned) tagi]);
195 RE_ENTRANT_CHECK_ON;
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 }
216 EX_INTERNAL is always given with a code which indicates where the
217 error was detected.
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
294 void FPU_exception(int n)
296 int i, int_type;
298 int_type = 0; /* Needed only to stop compiler warnings */
299 if ( n & EX_INTERNAL )
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);
306 else
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) )
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;
324 RE_ENTRANT_CHECK_OFF;
325 if ( (~control_word & n & CW_Exceptions) || (n == EX_INTERNAL) )
327 #ifdef PRINT_MESSAGES
328 /* My message from the sponsor */
329 printk(FPU_VERSION" "__DATE__" (C) W. Metzenthen.\n");
330 #endif PRINT_MESSAGES
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;
337 if (exception_names[i].type)
339 #ifdef PRINT_MESSAGES
340 printk("FP Exception: %s!\n", exception_names[i].name);
341 #endif PRINT_MESSAGES
343 else
344 printk("FPU emulator: Unknown Exception: 0x%04x!\n", n);
346 if ( n == EX_INTERNAL )
348 printk("FPU emulator: Internal error type 0x%04x\n", int_type);
349 FPU_printall();
351 #ifdef PRINT_MESSAGES
352 else
353 FPU_printall();
354 #endif PRINT_MESSAGES
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.
362 RE_ENTRANT_CHECK_ON;
364 #ifdef __DEBUG__
365 math_abort(FPU_info,SIGFPE);
366 #endif __DEBUG__
371 /* Real operation attempted on a NaN. */
372 /* Returns < 0 if the exception is unmasked */
373 int real_1op_NaN(FPU_REG *a)
375 int signalling, isNaN;
377 isNaN = (exponent(a) == EXP_OVER) && (a->sigh & 0x80000000);
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);
383 if ( !signalling )
385 if ( !isNaN ) /* pseudo-NaN, or other unsupported? */
387 if ( control_word & CW_Invalid )
389 /* Masked response */
390 reg_copy(&CONST_QNaN, a);
392 EXCEPTION(EX_Invalid);
393 return (!(control_word & CW_Invalid) ? FPU_Exception : 0) | TAG_Special;
395 return TAG_Special;
398 if ( control_word & CW_Invalid )
400 /* The masked response */
401 if ( !(a->sigh & 0x80000000) ) /* pseudo-NaN ? */
403 reg_copy(&CONST_QNaN, a);
405 /* ensure a Quiet NaN */
406 a->sigh |= 0x40000000;
409 EXCEPTION(EX_Invalid);
411 return (!(control_word & CW_Invalid) ? FPU_Exception : 0) | TAG_Special;
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)
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;
427 if ( taga == TAG_Special )
428 taga = FPU_Special(a);
429 if ( tagb == TAG_Special )
430 tagb = FPU_Special(b);
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 )
439 if ( control_word & CW_Invalid )
441 /* Masked response */
442 FPU_copy_to_regi(&CONST_QNaN, TAG_Special, deststnr);
444 EXCEPTION(EX_Invalid);
445 return (!(control_word & CW_Invalid) ? FPU_Exception : 0) | TAG_Special;
448 if (taga == TW_NaN)
450 x = a;
451 if (tagb == TW_NaN)
453 signalling = !(a->sigh & b->sigh & 0x40000000);
454 if ( significand(b) > significand(a) )
455 x = b;
456 else if ( significand(b) == significand(a) )
458 /* The default result for the case of two "equal" NaNs (signs may
459 differ) is chosen to reproduce 80486 behaviour */
460 x = defaultNaN;
463 else
465 /* return the quiet version of the NaN in a */
466 signalling = !(a->sigh & 0x40000000);
469 else
470 #ifdef PARANOID
471 if (tagb == TW_NaN)
472 #endif PARANOID
474 signalling = !(b->sigh & 0x40000000);
475 x = b;
477 #ifdef PARANOID
478 else
480 signalling = 0;
481 EXCEPTION(EX_INTERNAL|0x113);
482 x = &CONST_QNaN;
484 #endif PARANOID
486 if ( (!signalling) || (control_word & CW_Invalid) )
488 if ( ! x )
489 x = b;
491 if ( !(x->sigh & 0x80000000) ) /* pseudo-NaN ? */
492 x = &CONST_QNaN;
494 FPU_copy_to_regi(x, TAG_Special, deststnr);
496 if ( !signalling )
497 return TAG_Special;
499 /* ensure a Quiet NaN */
500 dest->sigh |= 0x40000000;
503 EXCEPTION(EX_Invalid);
505 return (!(control_word & CW_Invalid) ? FPU_Exception : 0) | TAG_Special;
509 /* Invalid arith operation on Valid registers */
510 /* Returns < 0 if the exception is unmasked */
511 asmlinkage int arith_invalid(int deststnr)
514 EXCEPTION(EX_Invalid);
516 if ( control_word & CW_Invalid )
518 /* The masked response */
519 FPU_copy_to_regi(&CONST_QNaN, TAG_Special, deststnr);
522 return (!(control_word & CW_Invalid) ? FPU_Exception : 0) | TAG_Valid;
527 /* Divide a finite number by zero */
528 asmlinkage int FPU_divide_by_zero(int deststnr, u_char sign)
530 FPU_REG *dest = &st(deststnr);
531 int tag = TAG_Valid;
533 if ( control_word & CW_ZeroDiv )
535 /* The masked response */
536 FPU_copy_to_regi(&CONST_INF, TAG_Special, deststnr);
537 setsign(dest, sign);
538 tag = TAG_Special;
541 EXCEPTION(EX_ZeroDiv);
543 return (!(control_word & CW_ZeroDiv) ? FPU_Exception : 0) | tag;
548 /* This may be called often, so keep it lean */
549 int set_precision_flag(int flags)
551 if ( control_word & CW_Precision )
553 partial_status &= ~(SW_C1 & flags);
554 partial_status |= flags; /* The masked response */
555 return 0;
557 else
559 EXCEPTION(flags);
560 return 1;
565 /* This may be called often, so keep it lean */
566 asmlinkage void set_precision_flag_up(void)
568 if ( control_word & CW_Precision )
569 partial_status |= (SW_Precision | SW_C1); /* The masked response */
570 else
571 EXCEPTION(EX_Precision | SW_C1);
575 /* This may be called often, so keep it lean */
576 asmlinkage void set_precision_flag_down(void)
578 if ( control_word & CW_Precision )
579 { /* The masked response */
580 partial_status &= ~SW_C1;
581 partial_status |= SW_Precision;
583 else
584 EXCEPTION(EX_Precision);
588 asmlinkage int denormal_operand(void)
590 if ( control_word & CW_Denormal )
591 { /* The masked response */
592 partial_status |= SW_Denorm_Op;
593 return TAG_Special;
595 else
597 EXCEPTION(EX_Denormal);
598 return TAG_Special | FPU_Exception;
603 asmlinkage int arith_overflow(FPU_REG *dest)
605 int tag = TAG_Valid;
607 if ( control_word & CW_Overflow )
609 /* The masked response */
610 /* ###### The response here depends upon the rounding mode */
611 reg_copy(&CONST_INF, dest);
612 tag = TAG_Special;
614 else
616 /* Subtract the magic number from the exponent */
617 addexponent(dest, (-3 * (1 << 13)));
620 EXCEPTION(EX_Overflow);
621 if ( control_word & CW_Overflow )
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;
631 return tag;
636 asmlinkage int arith_underflow(FPU_REG *dest)
638 int tag = TAG_Valid;
640 if ( control_word & CW_Underflow )
642 /* The masked response */
643 if ( exponent16(dest) <= EXP_UNDER - 63 )
645 reg_copy(&CONST_Z, dest);
646 partial_status &= ~SW_C1; /* Round down. */
647 tag = TAG_Zero;
649 else
651 stdexp(dest);
654 else
656 /* Add the magic number to the exponent. */
657 addexponent(dest, (3 * (1 << 13)) + EXTENDED_Ebias);
660 EXCEPTION(EX_Underflow);
661 if ( control_word & CW_Underflow )
663 /* The underflow exception is masked. */
664 EXCEPTION(EX_Precision);
665 return tag;
668 return tag;
673 void FPU_stack_overflow(void)
676 if ( control_word & CW_Invalid )
678 /* The masked response */
679 top--;
680 FPU_copy_to_reg0(&CONST_QNaN, TAG_Special);
683 EXCEPTION(EX_StackOver);
685 return;
690 void FPU_stack_underflow(void)
693 if ( control_word & CW_Invalid )
695 /* The masked response */
696 FPU_copy_to_reg0(&CONST_QNaN, TAG_Special);
699 EXCEPTION(EX_StackUnder);
701 return;
706 void FPU_stack_underflow_i(int i)
709 if ( control_word & CW_Invalid )
711 /* The masked response */
712 FPU_copy_to_regi(&CONST_QNaN, TAG_Special, i);
715 EXCEPTION(EX_StackUnder);
717 return;
722 void FPU_stack_underflow_pop(int i)
725 if ( control_word & CW_Invalid )
727 /* The masked response */
728 FPU_copy_to_regi(&CONST_QNaN, TAG_Special, i);
729 FPU_pop();
732 EXCEPTION(EX_StackUnder);
734 return;