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 overridden by the user library.
39 *
40 ****************************************************************************/
41 
42 #include <common.h>
43 #include "x86emu/x86emui.h"
44 
45 /*------------------------- Global Variables ------------------------------*/
46 
47 X86EMU_sysEnv _X86EMU_env;	/* Global emulator machine state */
48 X86EMU_intrFuncs _X86EMU_intrTab[256];
49 
50 int debug_intr;
51 
52 /*----------------------------- Implementation ----------------------------*/
53 
54 /****************************************************************************
55 PARAMETERS:
56 addr    - Emulator memory address to read
57 
58 RETURNS:
59 Byte value read from emulator memory.
60 
61 REMARKS:
62 Reads a byte value from the emulator memory.
63 ****************************************************************************/
64 u8 X86API rdb(u32 addr)
65 {
66 	return 0;
67 }
68 
69 /****************************************************************************
70 PARAMETERS:
71 addr    - Emulator memory address to read
72 
73 RETURNS:
74 Word value read from emulator memory.
75 
76 REMARKS:
77 Reads a word value from the emulator memory.
78 ****************************************************************************/
79 u16 X86API rdw(u32 addr)
80 {
81 	return 0;
82 }
83 
84 /****************************************************************************
85 PARAMETERS:
86 addr    - Emulator memory address to read
87 
88 RETURNS:
89 Long value read from emulator memory.
90 REMARKS:
91 Reads a long value from the emulator memory.
92 ****************************************************************************/
93 u32 X86API rdl(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 ****************************************************************************/
106 void X86API wrb(u32 addr, u8 val)
107 {
108 }
109 
110 /****************************************************************************
111 PARAMETERS:
112 addr    - Emulator memory address to read
113 val     - Value to store
114 
115 REMARKS:
116 Writes a word value to emulator memory.
117 ****************************************************************************/
118 void X86API wrw(u32 addr, u16 val)
119 {
120 }
121 
122 /****************************************************************************
123 PARAMETERS:
124 addr    - Emulator memory address to read
125 val     - Value to store
126 
127 REMARKS:
128 Writes a long value to emulator memory.
129 ****************************************************************************/
130 void X86API wrl(u32 addr, u32 val)
131 {
132 }
133 
134 /****************************************************************************
135 PARAMETERS:
136 addr    - PIO address to read
137 RETURN:
138 0
139 REMARKS:
140 Default PIO byte read function. Doesn't perform real inb.
141 ****************************************************************************/
142 static u8 X86API p_inb(X86EMU_pioAddr addr)
143 {
144 	DB(if (DEBUG_IO_TRACE())
145 	   printk("inb %#04x \n", addr);)
146 		return 0;
147 }
148 
149 /****************************************************************************
150 PARAMETERS:
151 addr    - PIO address to read
152 RETURN:
153 0
154 REMARKS:
155 Default PIO word read function. Doesn't perform real inw.
156 ****************************************************************************/
157 static u16 X86API p_inw(X86EMU_pioAddr addr)
158 {
159 	DB(if (DEBUG_IO_TRACE())
160 	   printk("inw %#04x \n", addr);)
161 		return 0;
162 }
163 
164 /****************************************************************************
165 PARAMETERS:
166 addr    - PIO address to read
167 RETURN:
168 0
169 REMARKS:
170 Default PIO long read function. Doesn't perform real inl.
171 ****************************************************************************/
172 static u32 X86API p_inl(X86EMU_pioAddr addr)
173 {
174 	DB(if (DEBUG_IO_TRACE())
175 	   printk("inl %#04x \n", addr);)
176 		return 0;
177 }
178 
179 /****************************************************************************
180 PARAMETERS:
181 addr    - PIO address to write
182 val     - Value to store
183 REMARKS:
184 Default PIO byte write function. Doesn't perform real outb.
185 ****************************************************************************/
186 static void X86API p_outb(X86EMU_pioAddr addr, u8 val)
187 {
188 	DB(if (DEBUG_IO_TRACE())
189 	   printk("outb %#02x -> %#04x \n", val, addr);)
190 		return;
191 }
192 
193 /****************************************************************************
194 PARAMETERS:
195 addr    - PIO address to write
196 val     - Value to store
197 REMARKS:
198 Default PIO word write function. Doesn't perform real outw.
199 ****************************************************************************/
200 static void X86API p_outw(X86EMU_pioAddr addr, u16 val)
201 {
202 	DB(if (DEBUG_IO_TRACE())
203 	   printk("outw %#04x -> %#04x \n", val, addr);)
204 		return;
205 }
206 
207 /****************************************************************************
208 PARAMETERS:
209 addr    - PIO address to write
210 val     - Value to store
211 REMARKS:
212 Default PIO ;ong write function. Doesn't perform real outl.
213 ****************************************************************************/
214 static void X86API p_outl(X86EMU_pioAddr addr, u32 val)
215 {
216 	DB(if (DEBUG_IO_TRACE())
217 	   printk("outl %#08x -> %#04x \n", val, addr);)
218 		return;
219 }
220 
221 /*------------------------- Global Variables ------------------------------*/
222 
223 u8(X86APIP sys_rdb) (u32 addr) = rdb;
224 u16(X86APIP sys_rdw) (u32 addr) = rdw;
225 u32(X86APIP sys_rdl) (u32 addr) = rdl;
226 void (X86APIP sys_wrb) (u32 addr, u8 val) = wrb;
227 void (X86APIP sys_wrw) (u32 addr, u16 val) = wrw;
228 void (X86APIP sys_wrl) (u32 addr, u32 val) = wrl;
229 u8(X86APIP sys_inb) (X86EMU_pioAddr addr) = p_inb;
230 u16(X86APIP sys_inw) (X86EMU_pioAddr addr) = p_inw;
231 u32(X86APIP sys_inl) (X86EMU_pioAddr addr) = p_inl;
232 void (X86APIP sys_outb) (X86EMU_pioAddr addr, u8 val) = p_outb;
233 void (X86APIP sys_outw) (X86EMU_pioAddr addr, u16 val) = p_outw;
234 void (X86APIP sys_outl) (X86EMU_pioAddr addr, u32 val) = p_outl;
235 
236 /*----------------------------- Setup -------------------------------------*/
237 
238 /****************************************************************************
239 PARAMETERS:
240 funcs   - New memory function pointers to make active
241 
242 REMARKS:
243 This function is used to set the pointers to functions which access
244 memory space, allowing the user application to override these functions
245 and hook them out as necessary for their application.
246 ****************************************************************************/
247 void X86EMU_setupMemFuncs(X86EMU_memFuncs * funcs)
248 {
249 	sys_rdb = funcs->rdb;
250 	sys_rdw = funcs->rdw;
251 	sys_rdl = funcs->rdl;
252 	sys_wrb = funcs->wrb;
253 	sys_wrw = funcs->wrw;
254 	sys_wrl = funcs->wrl;
255 }
256 
257 /****************************************************************************
258 PARAMETERS:
259 funcs   - New programmed I/O function pointers to make active
260 
261 REMARKS:
262 This function is used to set the pointers to functions which access
263 I/O space, allowing the user application to override these functions
264 and hook them out as necessary for their application.
265 ****************************************************************************/
266 void X86EMU_setupPioFuncs(X86EMU_pioFuncs * funcs)
267 {
268 	sys_inb = funcs->inb;
269 	sys_inw = funcs->inw;
270 	sys_inl = funcs->inl;
271 	sys_outb = funcs->outb;
272 	sys_outw = funcs->outw;
273 	sys_outl = funcs->outl;
274 }
275 
276 void X86EMU_setupIntrFunc(int intnum, X86EMU_intrFuncs func)
277 {
278 	_X86EMU_intrTab[intnum] = func;
279 }
280 
281 /****************************************************************************
282 PARAMETERS:
283 funcs   - New interrupt vector table to make active
284 
285 REMARKS:
286 This function is used to set the pointers to functions which handle
287 interrupt processing in the emulator, allowing the user application to
288 hook interrupts as necessary for their application. Any interrupts that
289 are not hooked by the user application, and reflected and handled internally
290 in the emulator via the interrupt vector table. This allows the application
291 to get control when the code being emulated executes specific software
292 interrupts.
293 ****************************************************************************/
294 void X86EMU_setupIntrFuncs(X86EMU_intrFuncs funcs[])
295 {
296 	int i;
297 
298 	for (i = 0; i < 256; i++)
299 		_X86EMU_intrTab[i] = NULL;
300 	if (funcs) {
301 		for (i = 0; i < 256; i++)
302 			_X86EMU_intrTab[i] = funcs[i];
303 	}
304 }
305 
306 /****************************************************************************
307 PARAMETERS:
308 int - New software interrupt to prepare for
309 
310 REMARKS:
311 This function is used to set up the emulator state to exceute a software
312 interrupt. This can be used by the user application code to allow an
313 interrupt to be hooked, examined and then reflected back to the emulator
314 so that the code in the emulator will continue processing the software
315 interrupt as per normal. This essentially allows system code to actively
316 hook and handle certain software interrupts as necessary.
317 ****************************************************************************/
318 void X86EMU_prepareForInt(int num)
319 {
320 	push_word((u16) M.x86.R_FLG);
321 	CLEAR_FLAG(F_IF);
322 	CLEAR_FLAG(F_TF);
323 	push_word(M.x86.R_CS);
324 	M.x86.R_CS = mem_access_word(num * 4 + 2);
325 	push_word(M.x86.R_IP);
326 	M.x86.R_IP = mem_access_word(num * 4);
327 	M.x86.intr = 0;
328 }
329