Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / platform / pc32 / gnu / fpemul / load_store.c
1 /*
2  *  load_store.c
3  *
4  * This file contains most of the code to interpret the FPU instructions
5  * which load and store from user memory.
6  *
7  *
8  * Copyright (C) 1992,1993,1994
9  *                       W. Metzenthen, 22 Parker St, Ormond, Vic 3163,
10  *                       Australia.  E-mail   billm@vaxc.cc.monash.edu.au
11  * All rights reserved.
12  *
13  * This copyright notice covers the redistribution and use of the
14  * FPU emulator developed by W. Metzenthen. It covers only its use
15  * in the 386BSD, FreeBSD and NetBSD operating systems. Any other
16  * use is not permitted under this copyright.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must include information specifying
24  *    that source code for the emulator is freely available and include
25  *    either:
26  *      a) an offer to provide the source code for a nominal distribution
27  *         fee, or
28  *      b) list at least two alternative methods whereby the source
29  *         can be obtained, e.g. a publically accessible bulletin board
30  *         and an anonymous ftp site from which the software can be
31  *         downloaded.
32  * 3. All advertising materials specifically mentioning features or use of
33  *    this emulator must acknowledge that it was developed by W. Metzenthen.
34  * 4. The name of W. Metzenthen may not be used to endorse or promote
35  *    products derived from this software without specific prior written
36  *    permission.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
39  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
40  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
41  * W. METZENTHEN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
45  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
46  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  *
49  *
50  * The purpose of this copyright, based upon the Berkeley copyright, is to
51  * ensure that the covered software remains freely available to everyone.
52  *
53  * The software (with necessary differences) is also available, but under
54  * the terms of the GNU copyleft, for the Linux operating system and for
55  * the djgpp ms-dos extender.
56  *
57  * W. Metzenthen   June 1994.
58  *
59  *
60  * $FreeBSD: src/sys/gnu/i386/fpemul/load_store.c,v 1.13 1999/08/28 00:42:52 peter 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 #include <sys/param.h>
72 #include <sys/proc.h>
73 #include <sys/systm.h>
74 #include <machine/pcb.h>
75
76 #include <gnu/i386/fpemul/fpu_emu.h>
77 #include <gnu/i386/fpemul/fpu_system.h>
78 #include <gnu/i386/fpemul/status_w.h>
79
80
81 #define _NONE_ 0                /* FPU_st0_ptr etc not needed */
82 #define _REG0_ 1                /* Will be storing st(0) */
83 #define _PUSH_ 3                /* Need to check for space to push onto stack */
84 #define _null_ 4                /* Function illegal or not implemented */
85
86 #define pop_0() { pop_ptr->tag = TW_Empty; top++; }
87
88
89 static unsigned char type_table[32] = {
90         _PUSH_, _PUSH_, _PUSH_, _PUSH_,
91         _null_, _null_, _null_, _null_,
92         _REG0_, _REG0_, _REG0_, _REG0_,
93         _REG0_, _REG0_, _REG0_, _REG0_,
94         _NONE_, _null_, _NONE_, _PUSH_,
95         _NONE_, _PUSH_, _null_, _PUSH_,
96         _NONE_, _null_, _NONE_, _REG0_,
97         _NONE_, _REG0_, _NONE_, _REG0_
98 };
99
100 void
101 load_store_instr(char type)
102 {
103         FPU_REG *pop_ptr;       /* We need a version of FPU_st0_ptr which
104                                  * won't change. */
105
106         pop_ptr = NULL;         /* Initialized just to stop compiler warnings. */
107
108
109         switch (type_table[(int) (unsigned) type]) {
110         case _NONE_:
111                 break;
112         case _REG0_:
113                 pop_ptr = &st(0);       /* Some of these instructions pop
114                                          * after storing */
115
116                 FPU_st0_ptr = pop_ptr;  /* Set the global variables. */
117                 FPU_st0_tag = FPU_st0_ptr->tag;
118                 break;
119         case _PUSH_:
120                 {
121                         pop_ptr = &st(-1);
122                         if (pop_ptr->tag != TW_Empty) {
123                                 stack_overflow();
124                                 return;
125                         }
126                         top--;
127                 }
128                 break;
129         case _null_:
130                 Un_impl();
131                 return;
132 #ifdef PARANOID
133         default:
134                 return EXCEPTION(EX_INTERNAL);
135 #endif                          /* PARANOID */
136         }
137
138         switch (type) {
139         case 000:               /* fld m32real */
140                 reg_load_single();
141                 setcc(0);       /* Clear the SW_C1 bit, "other bits undefined" */
142                 reg_move(&FPU_loaded_data, pop_ptr);
143                 break;
144         case 001:               /* fild m32int */
145                 reg_load_int32();
146                 setcc(0);       /* Clear the SW_C1 bit, "other bits undefined" */
147                 reg_move(&FPU_loaded_data, pop_ptr);
148                 break;
149         case 002:               /* fld m64real */
150                 reg_load_double();
151                 setcc(0);       /* Clear the SW_C1 bit, "other bits undefined" */
152                 reg_move(&FPU_loaded_data, pop_ptr);
153                 break;
154         case 003:               /* fild m16int */
155                 reg_load_int16();
156                 setcc(0);       /* Clear the SW_C1 bit, "other bits undefined" */
157                 reg_move(&FPU_loaded_data, pop_ptr);
158                 break;
159         case 010:               /* fst m32real */
160                 reg_store_single();
161                 break;
162         case 011:               /* fist m32int */
163                 reg_store_int32();
164                 break;
165         case 012:               /* fst m64real */
166                 reg_store_double();
167                 break;
168         case 013:               /* fist m16int */
169                 reg_store_int16();
170                 break;
171         case 014:               /* fstp m32real */
172                 if (reg_store_single())
173                         pop_0();/* pop only if the number was actually stored
174                                  * (see the 80486 manual p16-28) */
175                 break;
176         case 015:               /* fistp m32int */
177                 if (reg_store_int32())
178                         pop_0();/* pop only if the number was actually stored
179                                  * (see the 80486 manual p16-28) */
180                 break;
181         case 016:               /* fstp m64real */
182                 if (reg_store_double())
183                         pop_0();/* pop only if the number was actually stored
184                                  * (see the 80486 manual p16-28) */
185                 break;
186         case 017:               /* fistp m16int */
187                 if (reg_store_int16())
188                         pop_0();/* pop only if the number was actually stored
189                                  * (see the 80486 manual p16-28) */
190                 break;
191         case 020:               /* fldenv  m14/28byte */
192                 fldenv();
193                 break;
194         case 022:               /* frstor m94/108byte */
195                 frstor();
196                 break;
197         case 023:               /* fbld m80dec */
198                 reg_load_bcd();
199                 setcc(0);       /* Clear the SW_C1 bit, "other bits undefined" */
200                 reg_move(&FPU_loaded_data, pop_ptr);
201                 break;
202         case 024:               /* fldcw */
203                 REENTRANT_CHECK(OFF);
204                 control_word = fusword((unsigned short *) FPU_data_address);
205                 REENTRANT_CHECK(ON);
206 #ifdef NO_UNDERFLOW_TRAP
207                 if (!(control_word & EX_Underflow)) {
208                         control_word |= EX_Underflow;
209                 }
210 #endif
211                 /* We want no net effect: */
212                 FPU_data_address = (void *) (intptr_t) data_operand_offset;
213                 FPU_entry_eip = ip_offset;      /* We want no net effect */
214                 break;
215         case 025:               /* fld m80real */
216                 reg_load_extended();
217                 setcc(0);       /* Clear the SW_C1 bit, "other bits undefined" */
218                 reg_move(&FPU_loaded_data, pop_ptr);
219                 break;
220         case 027:               /* fild m64int */
221                 reg_load_int64();
222                 setcc(0);       /* Clear the SW_C1 bit, "other bits undefined" */
223                 reg_move(&FPU_loaded_data, pop_ptr);
224                 break;
225         case 030:               /* fstenv  m14/28byte */
226                 fstenv();
227                 /* We want no net effect: */
228                 FPU_data_address = (void *) (intptr_t) data_operand_offset;
229                 FPU_entry_eip = ip_offset;      /* We want no net effect */
230                 break;
231         case 032:               /* fsave */
232                 fsave();
233                 /* We want no net effect: */
234                 FPU_data_address = (void *) (intptr_t) data_operand_offset;
235                 FPU_entry_eip = ip_offset;      /* We want no net effect */
236                 break;
237         case 033:               /* fbstp m80dec */
238                 if (reg_store_bcd())
239                         pop_0();/* pop only if the number was actually stored
240                                  * (see the 80486 manual p16-28) */
241                 break;
242         case 034:               /* fstcw m16int */
243                 REENTRANT_CHECK(OFF);
244 /*                  verify_area(VERIFY_WRITE, FPU_data_address, 2);*/
245                 susword( (short *) FPU_data_address,control_word);
246                 REENTRANT_CHECK(ON);
247                 /* We want no net effect: */
248                 FPU_data_address = (void *) (intptr_t ) data_operand_offset;
249                 FPU_entry_eip = ip_offset;      /* We want no net effect */
250                 break;
251         case 035:               /* fstp m80real */
252                 if (reg_store_extended())
253                         pop_0();/* pop only if the number was actually stored
254                                  * (see the 80486 manual p16-28) */
255                 break;
256         case 036:               /* fstsw m2byte */
257                 status_word &= ~SW_Top;
258                 status_word |= (top & 7) << SW_Top_Shift;
259                 REENTRANT_CHECK(OFF);
260 /*                  verify_area(VERIFY_WRITE, FPU_data_address, 2);*/
261                 susword( (short *) FPU_data_address,status_word);
262                 REENTRANT_CHECK(ON);
263                 /* We want no net effect: */
264                 FPU_data_address = (void *) (intptr_t) data_operand_offset;
265                 FPU_entry_eip = ip_offset;      /* We want no net effect */
266                 break;
267         case 037:               /* fistp m64int */
268                 if (reg_store_int64())
269                         pop_0();/* pop only if the number was actually stored
270                                  * (see the 80486 manual p16-28) */
271                 break;
272         }
273 }