1 /****************************************************************************
2 *
3 *                       Realmode X86 Emulator Library
4 *
5 *               Copyright (C) 1991-2004 SciTech Software, Inc.
6 *                    Copyright (C) David Mosberger-Tang
7 *                      Copyright (C) 1999 Egbert Eich
8 *
9 *  ========================================================================
10 *
11 *  Permission to use, copy, modify, distribute, and sell this software and
12 *  its documentation for any purpose is hereby granted without fee,
13 *  provided that the above copyright notice appear in all copies and that
14 *  both that copyright notice and this permission notice appear in
15 *  supporting documentation, and that the name of the authors not be used
16 *  in advertising or publicity pertaining to distribution of the software
17 *  without specific, written prior permission.  The authors makes no
18 *  representations about the suitability of this software for any purpose.
19 *  It is provided "as is" without express or implied warranty.
20 *
21 *  THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
22 *  INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
23 *  EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
24 *  CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
25 *  USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
26 *  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
27 *  PERFORMANCE OF THIS SOFTWARE.
28 *
29 *  ========================================================================
30 *
31 * Language:     ANSI C
32 * Environment:  Any
33 * Developer:    Kendall Bennett
34 *
35 * Description:  This file includes subroutines which are related to
36 *               programmed I/O and memory access. Included in this module
37 *               are default functions that do nothing. For real uses these
38 *               functions will have to be overriden by the user library.
39 *
40 ****************************************************************************/
41 
42 #include "x86emu/x86emui.h"
43 
44 /*------------------------- Global Variables ------------------------------*/
45 
46 X86EMU_sysEnv       _X86EMU_env;        /* Global emulator machine state */
47 X86EMU_intrFuncs    _X86EMU_intrTab[256];
48 
49 /*----------------------------- Implementation ----------------------------*/
50 
51 /****************************************************************************
52 PARAMETERS:
53 addr    - Emulator memory address to read
54 
55 RETURNS:
56 Byte value read from emulator memory.
57 
58 REMARKS:
59 Reads a byte value from the emulator memory.
60 ****************************************************************************/
rdb(u32 addr)61 u8 X86API rdb(
62     u32 addr)
63 {
64     return 0;
65 }
66 
67 /****************************************************************************
68 PARAMETERS:
69 addr    - Emulator memory address to read
70 
71 RETURNS:
72 Word value read from emulator memory.
73 
74 REMARKS:
75 Reads a word value from the emulator memory.
76 ****************************************************************************/
rdw(u32 addr)77 u16 X86API rdw(
78     u32 addr)
79 {
80     return 0;
81 }
82 
83 /****************************************************************************
84 PARAMETERS:
85 addr    - Emulator memory address to read
86 
87 RETURNS:
88 Long value read from emulator memory.
89 REMARKS:
90 Reads a long value from the emulator memory.
91 ****************************************************************************/
rdl(u32 addr)92 u32 X86API rdl(
93     u32 addr)
94 {
95     return 0;
96 }
97 
98 /****************************************************************************
99 PARAMETERS:
100 addr    - Emulator memory address to read
101 val     - Value to store
102 
103 REMARKS:
104 Writes a byte value to emulator memory.
105 ****************************************************************************/
wrb(u32 addr,u8 val)106 void X86API wrb(
107     u32 addr,
108     u8 val)
109 {
110 }
111 
112 /****************************************************************************
113 PARAMETERS:
114 addr    - Emulator memory address to read
115 val     - Value to store
116 
117 REMARKS:
118 Writes a word value to emulator memory.
119 ****************************************************************************/
wrw(u32 addr,u16 val)120 void X86API wrw(
121     u32 addr,
122     u16 val)
123 {
124 }
125 
126 /****************************************************************************
127 PARAMETERS:
128 addr    - Emulator memory address to read
129 val     - Value to store
130 
131 REMARKS:
132 Writes a long value to emulator memory.
133 ****************************************************************************/
wrl(u32 addr,u32 val)134 void X86API wrl(
135     u32 addr,
136     u32 val)
137 {
138 }
139 
140 /****************************************************************************
141 PARAMETERS:
142 addr    - PIO address to read
143 RETURN:
144 0
145 REMARKS:
146 Default PIO byte read function. Doesn't perform real inb.
147 ****************************************************************************/
p_inb(X86EMU_pioAddr addr)148 static u8 X86API p_inb(
149     X86EMU_pioAddr addr)
150 {
151 DB( if (DEBUG_IO_TRACE())
152         printk("inb %#04x \n", addr);)
153     return 0;
154 }
155 
156 /****************************************************************************
157 PARAMETERS:
158 addr    - PIO address to read
159 RETURN:
160 0
161 REMARKS:
162 Default PIO word read function. Doesn't perform real inw.
163 ****************************************************************************/
p_inw(X86EMU_pioAddr addr)164 static u16 X86API p_inw(
165     X86EMU_pioAddr addr)
166 {
167 DB( if (DEBUG_IO_TRACE())
168         printk("inw %#04x \n", addr);)
169     return 0;
170 }
171 
172 /****************************************************************************
173 PARAMETERS:
174 addr    - PIO address to read
175 RETURN:
176 0
177 REMARKS:
178 Default PIO long read function. Doesn't perform real inl.
179 ****************************************************************************/
p_inl(X86EMU_pioAddr addr)180 static u32 X86API p_inl(
181     X86EMU_pioAddr addr)
182 {
183 DB( if (DEBUG_IO_TRACE())
184         printk("inl %#04x \n", addr);)
185     return 0;
186 }
187 
188 /****************************************************************************
189 PARAMETERS:
190 addr    - PIO address to write
191 val     - Value to store
192 REMARKS:
193 Default PIO byte write function. Doesn't perform real outb.
194 ****************************************************************************/
p_outb(X86EMU_pioAddr addr,u8 val)195 static void X86API p_outb(
196     X86EMU_pioAddr addr,
197     u8 val)
198 {
199 DB( if (DEBUG_IO_TRACE())
200         printk("outb %#02x -> %#04x \n", val, addr);)
201     return;
202 }
203 
204 /****************************************************************************
205 PARAMETERS:
206 addr    - PIO address to write
207 val     - Value to store
208 REMARKS:
209 Default PIO word write function. Doesn't perform real outw.
210 ****************************************************************************/
p_outw(X86EMU_pioAddr addr,u16 val)211 static void X86API p_outw(
212     X86EMU_pioAddr addr,
213     u16 val)
214 {
215 DB( if (DEBUG_IO_TRACE())
216         printk("outw %#04x -> %#04x \n", val, addr);)
217     return;
218 }
219 
220 /****************************************************************************
221 PARAMETERS:
222 addr    - PIO address to write
223 val     - Value to store
224 REMARKS:
225 Default PIO ;ong write function. Doesn't perform real outl.
226 ****************************************************************************/
p_outl(X86EMU_pioAddr addr,u32 val)227 static void X86API p_outl(
228     X86EMU_pioAddr addr,
229     u32 val)
230 {
231 DB( if (DEBUG_IO_TRACE())
232         printk("outl %#08x -> %#04x \n", val, addr);)
233     return;
234 }
235 
236 /*------------------------- Global Variables ------------------------------*/
237 
238 u8      (X86APIP sys_rdb)(u32 addr)                         = rdb;
239 u16     (X86APIP sys_rdw)(u32 addr)                         = rdw;
240 u32     (X86APIP sys_rdl)(u32 addr)                         = rdl;
241 void    (X86APIP sys_wrb)(u32 addr,u8 val)                  = wrb;
242 void    (X86APIP sys_wrw)(u32 addr,u16 val)                 = wrw;
243 void    (X86APIP sys_wrl)(u32 addr,u32 val)                 = wrl;
244 u8      (X86APIP sys_inb)(X86EMU_pioAddr addr)              = p_inb;
245 u16     (X86APIP sys_inw)(X86EMU_pioAddr addr)              = p_inw;
246 u32     (X86APIP sys_inl)(X86EMU_pioAddr addr)              = p_inl;
247 void    (X86APIP sys_outb)(X86EMU_pioAddr addr, u8 val)     = p_outb;
248 void    (X86APIP sys_outw)(X86EMU_pioAddr addr, u16 val)    = p_outw;
249 void    (X86APIP sys_outl)(X86EMU_pioAddr addr, u32 val)    = p_outl;
250 
251 /*----------------------------- Setup -------------------------------------*/
252 
253 /****************************************************************************
254 PARAMETERS:
255 funcs   - New memory function pointers to make active
256 
257 REMARKS:
258 This function is used to set the pointers to functions which access
259 memory space, allowing the user application to override these functions
260 and hook them out as necessary for their application.
261 ****************************************************************************/
X86EMU_setupMemFuncs(X86EMU_memFuncs * funcs)262 void X86EMU_setupMemFuncs(
263     X86EMU_memFuncs *funcs)
264 {
265     sys_rdb = funcs->rdb;
266     sys_rdw = funcs->rdw;
267     sys_rdl = funcs->rdl;
268     sys_wrb = funcs->wrb;
269     sys_wrw = funcs->wrw;
270     sys_wrl = funcs->wrl;
271 }
272 
273 /****************************************************************************
274 PARAMETERS:
275 funcs   - New programmed I/O function pointers to make active
276 
277 REMARKS:
278 This function is used to set the pointers to functions which access
279 I/O space, allowing the user application to override these functions
280 and hook them out as necessary for their application.
281 ****************************************************************************/
X86EMU_setupPioFuncs(X86EMU_pioFuncs * funcs)282 void X86EMU_setupPioFuncs(
283     X86EMU_pioFuncs *funcs)
284 {
285     sys_inb = funcs->inb;
286     sys_inw = funcs->inw;
287     sys_inl = funcs->inl;
288     sys_outb = funcs->outb;
289     sys_outw = funcs->outw;
290     sys_outl = funcs->outl;
291 }
292 
293 /****************************************************************************
294 PARAMETERS:
295 funcs   - New interrupt vector table to make active
296 
297 REMARKS:
298 This function is used to set the pointers to functions which handle
299 interrupt processing in the emulator, allowing the user application to
300 hook interrupts as necessary for their application. Any interrupts that
301 are not hooked by the user application, and reflected and handled internally
302 in the emulator via the interrupt vector table. This allows the application
303 to get control when the code being emulated executes specific software
304 interrupts.
305 ****************************************************************************/
X86EMU_setupIntrFuncs(X86EMU_intrFuncs funcs[])306 void X86EMU_setupIntrFuncs(
307     X86EMU_intrFuncs funcs[])
308 {
309     int i;
310 
311     for (i=0; i < 256; i++)
312         _X86EMU_intrTab[i] = NULL;
313     if (funcs) {
314         for (i = 0; i < 256; i++)
315             _X86EMU_intrTab[i] = funcs[i];
316         }
317 }
318 
319 /****************************************************************************
320 PARAMETERS:
321 int - New software interrupt to prepare for
322 
323 REMARKS:
324 This function is used to set up the emulator state to exceute a software
325 interrupt. This can be used by the user application code to allow an
326 interrupt to be hooked, examined and then reflected back to the emulator
327 so that the code in the emulator will continue processing the software
328 interrupt as per normal. This essentially allows system code to actively
329 hook and handle certain software interrupts as necessary.
330 ****************************************************************************/
X86EMU_prepareForInt(int num)331 void X86EMU_prepareForInt(
332     int num)
333 {
334     push_word((u16)M.x86.R_FLG);
335     CLEAR_FLAG(F_IF);
336     CLEAR_FLAG(F_TF);
337     push_word(M.x86.R_CS);
338     M.x86.R_CS = mem_access_word(num * 4 + 2);
339     push_word(M.x86.R_IP);
340     M.x86.R_IP = mem_access_word(num * 4);
341     M.x86.intr = 0;
342 }
343 
344