My first commit.
[dragonfly.git] / sys / i386 / gnu / fpemul / fpu_entry.c
1 /*
2  *  fpu_entry.c
3  *
4  * The entry function 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  * $FreeBSD: src/sys/gnu/i386/fpemul/fpu_entry.c,v 1.23 1999/10/12 02:23:14 msmith Exp $
59  * $DragonFly: src/sys/i386/gnu/fpemul/Attic/fpu_entry.c,v 1.5 2004/12/08 20:36:39 joerg Exp $
60  *
61  */
62
63 /*---------------------------------------------------------------------------+
64  | Note:                                                                     |
65  |    The file contains code which accesses user memory.                     |
66  |    Emulator static data may change when user memory is accessed, due to   |
67  |    other processes using the emulator while swapping is in progress.      |
68  +---------------------------------------------------------------------------*/
69
70 /*---------------------------------------------------------------------------+
71  | math_emulate() is the sole entry point for wm-FPU-emu                     |
72  +---------------------------------------------------------------------------*/
73
74 #include "opt_depricated.h"
75 #ifndef I_WANT_DEPRICATED_STUFF
76 #error "Add options I_WANT_DEPRICATED_STUFF to your kernel config and send a mail to kernel@"
77 #endif
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/proc.h>
82 #include <sys/kernel.h>
83
84 #include <machine/cpu.h>
85 #include <machine/pcb.h>
86
87 #include "fpu_emu.h"
88 #include "fpu_system.h"
89 #include "exception.h"
90 #include "status_w.h"
91
92
93 #define __BAD__ Un_impl         /* Not implemented */
94
95 #define FPU_LOOKAHEAD   1       /* For performance boost */
96
97 #if FPU_LOOKAHEAD != 0          /* I think thet we have to limit the */
98 #define LOOKAHEAD_LIMIT 7       /* Max number of lookahead instructions*/
99 #endif                          /* Or else a prog consisting of a million */
100                                 /* fnops will spend all its time in kernel*/
101
102 #ifndef NO_UNDOC_CODE           /* Un-documented FPU op-codes supported by
103                                  * default. */
104
105 /* WARNING: These codes are not documented by Intel in their 80486 manual
106    and may not work on FPU clones or later Intel FPUs. */
107
108 /* Changes to support the un-doc codes provided by Linus Torvalds. */
109
110 #define _d9_d8_ fstp_i          /* unofficial code (19) */
111 #define _dc_d0_ fcom_st         /* unofficial code (14) */
112 #define _dc_d8_ fcompst         /* unofficial code (1c) */
113 #define _dd_c8_ fxch_i          /* unofficial code (0d) */
114 #define _de_d0_ fcompst         /* unofficial code (16) */
115 #define _df_c0_ ffreep          /* unofficial code (07) ffree + pop */
116 #define _df_c8_ fxch_i          /* unofficial code (0f) */
117 #define _df_d0_ fstp_i          /* unofficial code (17) */
118 #define _df_d8_ fstp_i          /* unofficial code (1f) */
119
120 static FUNC st_instr_table[64] = {
121         fadd__, fld_i_, __BAD__, __BAD__, fadd_i, ffree_, faddp_, _df_c0_,
122         fmul__, fxch_i, __BAD__, __BAD__, fmul_i, _dd_c8_, fmulp_, _df_c8_,
123         fcom_st, fp_nop, __BAD__, __BAD__, _dc_d0_, fst_i_, _de_d0_, _df_d0_,
124         fcompst, _d9_d8_, __BAD__, __BAD__, _dc_d8_, fstp_i, fcompp, _df_d8_,
125         fsub__, fp_etc, __BAD__, finit_, fsubri, fucom_, fsubrp, fstsw_,
126         fsubr_, fconst, fucompp, __BAD__, fsub_i, fucomp, fsubp_, __BAD__,
127         fdiv__, trig_a, __BAD__, __BAD__, fdivri, __BAD__, fdivrp, __BAD__,
128         fdivr_, trig_b, __BAD__, __BAD__, fdiv_i, __BAD__, fdivp_, __BAD__,
129 };
130 #else                           /* Support only documented FPU op-codes */
131
132 static FUNC st_instr_table[64] = {
133         fadd__, fld_i_, __BAD__, __BAD__, fadd_i, ffree_, faddp_, __BAD__,
134         fmul__, fxch_i, __BAD__, __BAD__, fmul_i, __BAD__, fmulp_, __BAD__,
135         fcom_st, fp_nop, __BAD__, __BAD__, __BAD__, fst_i_, __BAD__, __BAD__,
136         fcompst, __BAD__, __BAD__, __BAD__, __BAD__, fstp_i, fcompp, __BAD__,
137         fsub__, fp_etc, __BAD__, finit_, fsubri, fucom_, fsubrp, fstsw_,
138         fsubr_, fconst, fucompp, __BAD__, fsub_i, fucomp, fsubp_, __BAD__,
139         fdiv__, trig_a, __BAD__, __BAD__, fdivri, __BAD__, fdivrp, __BAD__,
140         fdivr_, trig_b, __BAD__, __BAD__, fdiv_i, __BAD__, fdivp_, __BAD__,
141 };
142 #endif                          /* NO_UNDOC_CODE */
143
144
145 #define _NONE_ 0                /* Take no special action */
146 #define _REG0_ 1                /* Need to check for not empty st(0) */
147 #define _REGI_ 2                /* Need to check for not empty st(0) and
148                                  * st(rm) */
149 #define _REGi_ 0                /* Uses st(rm) */
150 #define _PUSH_ 3                /* Need to check for space to push onto stack */
151 #define _null_ 4                /* Function illegal or not implemented */
152 #define _REGIi 5                /* Uses st(0) and st(rm), result to st(rm) */
153 #define _REGIp 6                /* Uses st(0) and st(rm), result to st(rm)
154                                  * then pop */
155 #define _REGIc 0                /* Compare st(0) and st(rm) */
156 #define _REGIn 0                /* Uses st(0) and st(rm), but handle checks
157                                  * later */
158
159 #ifndef NO_UNDOC_CODE
160
161 /* Un-documented FPU op-codes supported by default. (see above) */
162
163 static unsigned char type_table[64] = {
164         _REGI_, _NONE_, _null_, _null_, _REGIi, _REGi_, _REGIp, _REGi_,
165         _REGI_, _REGIn, _null_, _null_, _REGIi, _REGI_, _REGIp, _REGI_,
166         _REGIc, _NONE_, _null_, _null_, _REGIc, _REG0_, _REGIc, _REG0_,
167         _REGIc, _REG0_, _null_, _null_, _REGIc, _REG0_, _REGIc, _REG0_,
168         _REGI_, _NONE_, _null_, _NONE_, _REGIi, _REGIc, _REGIp, _NONE_,
169         _REGI_, _NONE_, _REGIc, _null_, _REGIi, _REGIc, _REGIp, _null_,
170         _REGI_, _NONE_, _null_, _null_, _REGIi, _null_, _REGIp, _null_,
171         _REGI_, _NONE_, _null_, _null_, _REGIi, _null_, _REGIp, _null_
172 };
173 #else                           /* Support only documented FPU op-codes */
174
175 static unsigned char type_table[64] = {
176         _REGI_, _NONE_, _null_, _null_, _REGIi, _REGi_, _REGIp, _null_,
177         _REGI_, _REGIn, _null_, _null_, _REGIi, _null_, _REGIp, _null_,
178         _REGIc, _NONE_, _null_, _null_, _null_, _REG0_, _null_, _null_,
179         _REGIc, _null_, _null_, _null_, _null_, _REG0_, _REGIc, _null_,
180         _REGI_, _NONE_, _null_, _NONE_, _REGIi, _REGIc, _REGIp, _NONE_,
181         _REGI_, _NONE_, _REGIc, _null_, _REGIi, _REGIc, _REGIp, _null_,
182         _REGI_, _NONE_, _null_, _null_, _REGIi, _null_, _REGIp, _null_,
183         _REGI_, _NONE_, _null_, _null_, _REGIi, _null_, _REGIp, _null_
184 };
185 #endif                          /* NO_UNDOC_CODE */
186
187 /* Be careful when using any of these global variables...
188    they might change if swapping is triggered */
189 unsigned char FPU_rm;
190 char    FPU_st0_tag;
191 FPU_REG *FPU_st0_ptr;
192
193 #ifdef PARANOID
194 char    emulating = 0;
195 #endif                          /* PARANOID */
196
197 #define bswapw(x) __asm__("xchgb %%al,%%ah":"=a" (x):"0" ((short)x))
198 #define math_abort(signo) \
199     FPU_EIP = FPU_ORIG_EIP;REENTRANT_CHECK(OFF);return(signo);
200
201 static int
202 math_emulate(struct trapframe * tframe)
203 {
204
205         unsigned char FPU_modrm;
206         unsigned short code;
207 #ifdef LOOKAHEAD_LIMIT
208         int lookahead_limit = LOOKAHEAD_LIMIT;
209 #endif
210 #ifdef PARANOID
211         if (emulating) {
212                 printf("ERROR: wm-FPU-emu is not RE-ENTRANT!\n");
213         }
214         REENTRANT_CHECK(ON);
215 #endif                          /* PARANOID */
216
217         KKASSERT(curproc);
218         if ((((struct pcb *) curproc->p_addr)->pcb_flags & FP_SOFTFP) == 0) {
219                 finit();
220                 control_word = __INITIAL_NPXCW__;
221                 ((struct pcb *) curproc->p_addr)->pcb_flags |= FP_SOFTFP;
222         }
223         FPU_info = tframe;
224         FPU_ORIG_EIP = FPU_EIP; /* --pink-- */
225
226         if (FPU_CS != 0x001f) {
227                 printf("math_emulate: %x : %x\n", FPU_CS, FPU_EIP);
228                 panic("FPU emulation in kernel");
229         }
230 #ifdef notyet
231         /* We cannot handle emulation in v86-mode */
232         if (FPU_EFLAGS & 0x00020000) {
233                 FPU_ORIG_EIP = FPU_EIP;
234                 math_abort(FPU_info, SIGILL);
235         }
236 #endif
237
238         FPU_lookahead = FPU_LOOKAHEAD;
239         if (curproc->p_flag & P_TRACED)
240                 FPU_lookahead = 0;
241
242 do_another_FPU_instruction:
243
244         REENTRANT_CHECK(OFF);
245         code = fuword((u_int *) FPU_EIP);
246         REENTRANT_CHECK(ON);
247         if ((code & 0xff) == 0x9b) {    /* fwait */
248                 if (status_word & SW_Summary)
249                         goto do_the_FPU_interrupt;
250                 else {
251                         FPU_EIP++;
252                         goto FPU_instruction_done;
253                 }
254         }
255         if (status_word & SW_Summary) {
256                 /* Ignore the error for now if the current instruction is a
257                  * no-wait control instruction */
258                 /* The 80486 manual contradicts itself on this topic, so I use
259                  * the following list of such instructions until I can check
260                  * on a real 80486: fninit, fnstenv, fnsave, fnstsw, fnstenv,
261                  * fnclex. */
262                 if (!((((code & 0xf803) == 0xe003) ||   /* fnclex, fninit,
263                                                          * fnstsw */
264                             (((code & 0x3003) == 0x3001) &&     /* fnsave, fnstcw,
265                                                                  * fnstenv, fnstsw */
266                                 ((code & 0xc000) != 0xc000))))) {
267                         /* This is a guess about what a real FPU might do to
268                          * this bit: */
269 /*        status_word &= ~SW_Summary; ****/
270
271                         /* We need to simulate the action of the kernel to FPU
272                          * interrupts here. Currently, the "real FPU" part of
273                          * the kernel (0.99.10) clears the exception flags,
274                          * sets the registers to empty, and passes information
275                          * back to the interrupted process via the cs selector
276                          * and operand selector, so we do the same. */
277         do_the_FPU_interrupt:
278                         cs_selector &= 0xffff0000;
279                         cs_selector |= (status_word & ~SW_Top) | ((top & 7) << SW_Top_Shift);
280                         operand_selector = tag_word();
281                         status_word = 0;
282                         top = 0;
283                         {
284                                 int     r;
285                                 for (r = 0; r < 8; r++) {
286                                         regs[r].tag = TW_Empty;
287                                 }
288                         }
289                         REENTRANT_CHECK(OFF);
290                         math_abort(SIGFPE);
291                 }
292         }
293         FPU_entry_eip = FPU_ORIG_EIP = FPU_EIP;
294
295         if ((code & 0xff) == 0x66) {    /* size prefix */
296                 FPU_EIP++;
297                 REENTRANT_CHECK(OFF);
298                 code = fuword((u_int *) FPU_EIP);
299                 REENTRANT_CHECK(ON);
300         }
301         FPU_EIP += 2;
302
303         FPU_modrm = code >> 8;
304         FPU_rm = FPU_modrm & 7;
305
306         if (FPU_modrm < 0300) {
307                 /* All of these instructions use the mod/rm byte to get a data
308                  * address */
309                 get_address(FPU_modrm);
310                 if (!(code & 1)) {
311                         unsigned short status1 = status_word;
312                         FPU_st0_ptr = &st(0);
313                         FPU_st0_tag = FPU_st0_ptr->tag;
314
315                         /* Stack underflow has priority */
316                         if (NOT_EMPTY_0) {
317                                 switch ((code >> 1) & 3) {
318                                 case 0:
319                                         reg_load_single();
320                                         break;
321                                 case 1:
322                                         reg_load_int32();
323                                         break;
324                                 case 2:
325                                         reg_load_double();
326                                         break;
327                                 case 3:
328                                         reg_load_int16();
329                                         break;
330                                 }
331
332                                 /* No more access to user memory, it is safe
333                                  * to use static data now */
334                                 FPU_st0_ptr = &st(0);
335                                 FPU_st0_tag = FPU_st0_ptr->tag;
336
337                                 /* NaN operands have the next priority. */
338                                 /* We have to delay looking at st(0) until
339                                  * after loading the data, because that data
340                                  * might contain an SNaN */
341                                 if ((FPU_st0_tag == TW_NaN) ||
342                                     (FPU_loaded_data.tag == TW_NaN)) {
343                                         /* Restore the status word; we might
344                                          * have loaded a denormal. */
345                                         status_word = status1;
346                                         if ((FPU_modrm & 0x30) == 0x10) {
347                                                 /* fcom or fcomp */
348                                                 EXCEPTION(EX_Invalid);
349                                                 setcc(SW_C3 | SW_C2 | SW_C0);
350                                                 if (FPU_modrm & 0x08)
351                                                         pop();  /* fcomp, so we pop. */
352                                         } else
353                                                 real_2op_NaN(FPU_st0_ptr, &FPU_loaded_data, FPU_st0_ptr);
354                                         goto reg_mem_instr_done;
355                                 }
356                                 switch ((FPU_modrm >> 3) & 7) {
357                                 case 0: /* fadd */
358                                         reg_add(FPU_st0_ptr, &FPU_loaded_data, FPU_st0_ptr, control_word);
359                                         break;
360                                 case 1: /* fmul */
361                                         reg_mul(FPU_st0_ptr, &FPU_loaded_data, FPU_st0_ptr, control_word);
362                                         break;
363                                 case 2: /* fcom */
364                                         compare_st_data();
365                                         break;
366                                 case 3: /* fcomp */
367                                         compare_st_data();
368                                         pop();
369                                         break;
370                                 case 4: /* fsub */
371                                         reg_sub(FPU_st0_ptr, &FPU_loaded_data, FPU_st0_ptr, control_word);
372                                         break;
373                                 case 5: /* fsubr */
374                                         reg_sub(&FPU_loaded_data, FPU_st0_ptr, FPU_st0_ptr, control_word);
375                                         break;
376                                 case 6: /* fdiv */
377                                         reg_div(FPU_st0_ptr, &FPU_loaded_data, FPU_st0_ptr, control_word);
378                                         break;
379                                 case 7: /* fdivr */
380                                         if (FPU_st0_tag == TW_Zero)
381                                                 status_word = status1;  /* Undo any denorm tag,
382                                                                          * zero-divide has
383                                                                          * priority. */
384                                         reg_div(&FPU_loaded_data, FPU_st0_ptr, FPU_st0_ptr, control_word);
385                                         break;
386                                 }
387                         } else {
388                                 if ((FPU_modrm & 0x30) == 0x10) {
389                                         /* The instruction is fcom or fcomp */
390                                         EXCEPTION(EX_StackUnder);
391                                         setcc(SW_C3 | SW_C2 | SW_C0);
392                                         if (FPU_modrm & 0x08)
393                                                 pop();  /* fcomp, Empty or not,
394                                                          * we pop. */
395                                 } else
396                                         stack_underflow();
397                         }
398                 } else {
399                         load_store_instr(((FPU_modrm & 0x38) | (code & 6)) >> 1);
400                 }
401
402 reg_mem_instr_done:
403
404                 data_operand_offset = (intptr_t) (void *) FPU_data_address;
405         } else {
406                 /* None of these instructions access user memory */
407                 unsigned char instr_index = (FPU_modrm & 0x38) | (code & 7);
408
409                 FPU_st0_ptr = &st(0);
410                 FPU_st0_tag = FPU_st0_ptr->tag;
411                 switch (type_table[(int) instr_index]) {
412                 case _NONE_:    /* also _REGIc: _REGIn */
413                         break;
414                 case _REG0_:
415                         if (!NOT_EMPTY_0) {
416                                 stack_underflow();
417                                 goto FPU_instruction_done;
418                         }
419                         break;
420                 case _REGIi:
421                         if (!NOT_EMPTY_0 || !NOT_EMPTY(FPU_rm)) {
422                                 stack_underflow_i(FPU_rm);
423                                 goto FPU_instruction_done;
424                         }
425                         break;
426                 case _REGIp:
427                         if (!NOT_EMPTY_0 || !NOT_EMPTY(FPU_rm)) {
428                                 stack_underflow_i(FPU_rm);
429                                 pop();
430                                 goto FPU_instruction_done;
431                         }
432                         break;
433                 case _REGI_:
434                         if (!NOT_EMPTY_0 || !NOT_EMPTY(FPU_rm)) {
435                                 stack_underflow();
436                                 goto FPU_instruction_done;
437                         }
438                         break;
439                 case _PUSH_:    /* Only used by the fld st(i) instruction */
440                         break;
441                 case _null_:
442                         Un_impl();
443                         goto FPU_instruction_done;
444                 default:
445                         EXCEPTION(EX_INTERNAL | 0x111);
446                         goto FPU_instruction_done;
447                 }
448                 (*st_instr_table[(int) instr_index]) ();
449         }
450
451 FPU_instruction_done:
452
453         ip_offset = FPU_entry_eip;
454         bswapw(code);
455         *(1 + (unsigned short *) &cs_selector) = code & 0x7ff;
456
457 #ifdef DEBUG
458         REENTRANT_CHECK(OFF);
459         emu_printall();
460         REENTRANT_CHECK(ON);
461 #endif                          /* DEBUG */
462 #ifdef LOOKAHEAD_LIMIT
463 if (--lookahead_limit)
464 #endif
465         if (FPU_lookahead) {
466                 unsigned char next;
467
468                 /* (This test should generate no machine code) */
469                 while (1) {
470                         REENTRANT_CHECK(OFF);
471                         next = fubyte((u_char *) FPU_EIP);
472                         REENTRANT_CHECK(ON);
473                         if (((next & 0xf8) == 0xd8) || (next == 0x9b)) {        /* fwait */
474                                 goto do_another_FPU_instruction;
475                         } else
476                                 if (next == 0x66) {     /* size prefix */
477                                         REENTRANT_CHECK(OFF);
478                                         next = fubyte((u_char *) (FPU_EIP + 1));
479                                         REENTRANT_CHECK(ON);
480                                         if ((next & 0xf8) == 0xd8) {
481                                                 FPU_EIP++;
482                                                 goto do_another_FPU_instruction;
483                                         }
484                                 }
485                         break;
486                 }
487         }
488         REENTRANT_CHECK(OFF);
489         return (0);             /* --pink-- */
490 }
491
492 static int
493 gnufpu_modevent(module_t mod, int type, void *unused) 
494 {
495         switch (type) {
496         case MOD_LOAD:
497                 if (pmath_emulate) {
498                         printf("Another Math emulator already present\n");
499                         return EACCES;
500                 }
501                 pmath_emulate = math_emulate;
502                 if (bootverbose)
503                         printf("GPL Math emulator present\n");
504                 break;
505         case MOD_UNLOAD:
506                 if (pmath_emulate != math_emulate) {
507                         printf("Cannot unload another math emulator\n");
508                         return EACCES;
509                 }
510                 pmath_emulate = 0;
511                 if (bootverbose)
512                         printf("GPL Math emulator unloaded\n");
513                 break;
514         default:
515                 break;
516         }
517         return 0;
518                 
519 }
520 static moduledata_t gnufpumod = {
521         "gnufpu",
522         gnufpu_modevent,
523         0
524 };
525 DECLARE_MODULE(gnufpu, gnufpumod, SI_SUB_DRIVERS, SI_ORDER_ANY);