1 /****************************************************************************
3 * Realmode X86 Emulator Library
5 * Copyright (C) 1996-1999 SciTech Software, Inc.
6 * Copyright (C) David Mosberger-Tang
7 * Copyright (C) 1999 Egbert Eich
9 * ========================================================================
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.
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.
29 * ========================================================================
33 * Developer: Kendall Bennett
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 with limited usefulness. For real
38 * uses these functions will most likely be overriden by the
41 ****************************************************************************/
42 /* $XFree86: xc/extras/x86emu/src/x86emu/sys.c,v 1.5 2000/08/23 22:10:01 tsi Exp $ */
44 #include <x86emu/x86emu.h>
45 #include <x86emu/regs.h>
48 #if 1 /* Coreboot needs to map prinkf to printk. */
55 #include "xf86_ansic.h"
59 /*------------------------- Global Variables ------------------------------*/
61 X86EMU_sysEnv _X86EMU_env; /* Global emulator machine state */
62 X86EMU_intrFuncs _X86EMU_intrTab[256];
64 /*----------------------------- Implementation ----------------------------*/
66 /* compute a pointer. This replaces code scattered all over the place! */
67 static u8 *mem_ptr(u32 addr, int size)
71 if (addr > M.mem_size - size) {
72 DB(printk("mem_ptr: address %#x out of range!\n", addr);)
76 //printk("%x:%x updating int vector 0x%x\n",
77 // M.x86.R_CS, M.x86.R_IP, addr >> 2);
79 retaddr = (u8 *) (M.mem_base + addr);
84 /****************************************************************************
86 addr - Emulator memory address to read
89 Byte value read from emulator memory.
92 Reads a byte value from the emulator memory.
93 ****************************************************************************/
94 u8 X86API rdb(u32 addr)
99 ptr = mem_ptr(addr, 1);
102 DB(if (DEBUG_MEM_TRACE())
103 printk("%#08x 1 -> %#x\n", addr, val);)
107 /****************************************************************************
109 addr - Emulator memory address to read
112 Word value read from emulator memory.
115 Reads a word value from the emulator memory.
116 ****************************************************************************/
117 u16 X86API rdw(u32 addr)
122 ptr = mem_ptr(addr, 2);
123 val = *(u16 *) (ptr);
125 DB(if (DEBUG_MEM_TRACE())
126 printk("%#08x 2 -> %#x\n", addr, val);)
130 /****************************************************************************
132 addr - Emulator memory address to read
135 Long value read from emulator memory.
137 Reads a long value from the emulator memory.
138 ****************************************************************************/
139 u32 X86API rdl(u32 addr)
144 ptr = mem_ptr(addr, 4);
145 val = *(u32 *) (ptr);
147 DB(if (DEBUG_MEM_TRACE())
148 printk("%#08x 4 -> %#x\n", addr, val);)
152 /****************************************************************************
154 addr - Emulator memory address to read
158 Writes a byte value to emulator memory.
159 ****************************************************************************/
160 void X86API wrb(u32 addr, u8 val)
164 ptr = mem_ptr(addr, 1);
167 DB(if (DEBUG_MEM_TRACE())
168 printk("%#08x 1 <- %#x\n", addr, val);)
171 /****************************************************************************
173 addr - Emulator memory address to read
177 Writes a word value to emulator memory.
178 ****************************************************************************/
179 void X86API wrw(u32 addr, u16 val)
183 ptr = mem_ptr(addr, 2);
184 *(u16 *) (ptr) = val;
186 DB(if (DEBUG_MEM_TRACE())
187 printk("%#08x 2 <- %#x\n", addr, val);)
190 /****************************************************************************
192 addr - Emulator memory address to read
196 Writes a long value to emulator memory.
197 ****************************************************************************/
198 void X86API wrl(u32 addr, u32 val)
202 ptr = mem_ptr(addr, 4);
203 *(u32 *) (ptr) = val;
205 DB(if (DEBUG_MEM_TRACE())
206 printk("%#08x 4 <- %#x\n", addr, val);)
211 /****************************************************************************
213 addr - PIO address to read
217 Default PIO byte read function. Doesn't perform real inb.
218 ****************************************************************************/
219 static u8 X86API p_inb(X86EMU_pioAddr addr)
221 DB(if (DEBUG_IO_TRACE())
222 printk("inb %#04x \n", addr);)
226 /****************************************************************************
228 addr - PIO address to read
232 Default PIO word read function. Doesn't perform real inw.
233 ****************************************************************************/
234 static u16 X86API p_inw(X86EMU_pioAddr addr)
236 DB(if (DEBUG_IO_TRACE())
237 printk("inw %#04x \n", addr);)
241 /****************************************************************************
243 addr - PIO address to read
247 Default PIO long read function. Doesn't perform real inl.
248 ****************************************************************************/
249 static u32 X86API p_inl(X86EMU_pioAddr addr)
251 DB(if (DEBUG_IO_TRACE())
252 printk("inl %#04x \n", addr);)
256 /****************************************************************************
258 addr - PIO address to write
261 Default PIO byte write function. Doesn't perform real outb.
262 ****************************************************************************/
263 static void X86API p_outb(X86EMU_pioAddr addr, u8 val)
265 DB(if (DEBUG_IO_TRACE())
266 printk("outb %#02x -> %#04x \n", val, addr);)
271 /****************************************************************************
273 addr - PIO address to write
276 Default PIO word write function. Doesn't perform real outw.
277 ****************************************************************************/
278 static void X86API p_outw(X86EMU_pioAddr addr, u16 val)
280 DB(if (DEBUG_IO_TRACE())
281 printk("outw %#04x -> %#04x \n", val, addr);)
286 /****************************************************************************
288 addr - PIO address to write
291 Default PIO ;ong write function. Doesn't perform real outl.
292 ****************************************************************************/
293 static void X86API p_outl(X86EMU_pioAddr addr, u32 val)
295 DB(if (DEBUG_IO_TRACE())
296 printk("outl %#08x -> %#04x \n", val, addr);)
302 /*------------------------- Global Variables ------------------------------*/
304 u8(X86APIP sys_rdb) (u32 addr) = rdb;
305 u16(X86APIP sys_rdw) (u32 addr) = rdw;
306 u32(X86APIP sys_rdl) (u32 addr) = rdl;
307 void (X86APIP sys_wrb) (u32 addr, u8 val) = wrb;
308 void (X86APIP sys_wrw) (u32 addr, u16 val) = wrw;
309 void (X86APIP sys_wrl) (u32 addr, u32 val) = wrl;
310 u8(X86APIP sys_inb) (X86EMU_pioAddr addr) = p_inb;
311 u16(X86APIP sys_inw) (X86EMU_pioAddr addr) = p_inw;
312 u32(X86APIP sys_inl) (X86EMU_pioAddr addr) = p_inl;
313 void (X86APIP sys_outb) (X86EMU_pioAddr addr, u8 val) = p_outb;
314 void (X86APIP sys_outw) (X86EMU_pioAddr addr, u16 val) = p_outw;
315 void (X86APIP sys_outl) (X86EMU_pioAddr addr, u32 val) = p_outl;
317 /*----------------------------- Setup -------------------------------------*/
319 /****************************************************************************
321 funcs - New memory function pointers to make active
324 This function is used to set the pointers to functions which access
325 memory space, allowing the user application to override these functions
326 and hook them out as necessary for their application.
327 ****************************************************************************/
328 void X86EMU_setupMemFuncs(X86EMU_memFuncs * funcs)
330 sys_rdb = funcs->rdb;
331 sys_rdw = funcs->rdw;
332 sys_rdl = funcs->rdl;
333 sys_wrb = funcs->wrb;
334 sys_wrw = funcs->wrw;
335 sys_wrl = funcs->wrl;
338 /****************************************************************************
340 funcs - New programmed I/O function pointers to make active
343 This function is used to set the pointers to functions which access
344 I/O space, allowing the user application to override these functions
345 and hook them out as necessary for their application.
346 ****************************************************************************/
347 void X86EMU_setupPioFuncs(X86EMU_pioFuncs * funcs)
349 sys_inb = funcs->inb;
350 sys_inw = funcs->inw;
351 sys_inl = funcs->inl;
352 sys_outb = funcs->outb;
353 sys_outw = funcs->outw;
354 sys_outl = funcs->outl;
357 /****************************************************************************
359 funcs - New interrupt vector table to make active
362 This function is used to set the pointers to functions which handle
363 interrupt processing in the emulator, allowing the user application to
364 hook interrupts as necessary for their application. Any interrupts that
365 are not hooked by the user application, and reflected and handled internally
366 in the emulator via the interrupt vector table. This allows the application
367 to get control when the code being emulated executes specific software
369 ****************************************************************************/
370 void X86EMU_setupIntrFuncs(X86EMU_intrFuncs funcs[])
374 for (i = 0; i < 256; i++)
375 _X86EMU_intrTab[i] = NULL;
377 for (i = 0; i < 256; i++)
378 _X86EMU_intrTab[i] = funcs[i];
382 /****************************************************************************
384 int - New software interrupt to prepare for
387 This function is used to set up the emulator state to exceute a software
388 interrupt. This can be used by the user application code to allow an
389 interrupt to be hooked, examined and then reflected back to the emulator
390 so that the code in the emulator will continue processing the software
391 interrupt as per normal. This essentially allows system code to actively
392 hook and handle certain software interrupts as necessary.
393 ****************************************************************************/
394 void X86EMU_prepareForInt(int num)
396 push_word((u16) M.x86.R_FLG);
399 push_word(M.x86.R_CS);
400 M.x86.R_CS = mem_access_word(num * 4 + 2);
401 push_word(M.x86.R_IP);
402 M.x86.R_IP = mem_access_word(num * 4);
406 void X86EMU_setMemBase(void *base, size_t size)
408 M.mem_base = (unsigned long) base;