6f5a3d29c938edc1e9e4091527a90828c3681fad
[coreboot.git] / src / devices / emulator / x86emu / sys.c
1 /****************************************************************************
2 *
3 *                                               Realmode X86 Emulator Library
4 *
5 *               Copyright (C) 1996-1999 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 with limited usefulness. For real
38 *                               uses these functions will most likely be overriden by the
39 *                               user library.
40 *
41 ****************************************************************************/
42 /* $XFree86: xc/extras/x86emu/src/x86emu/sys.c,v 1.5 2000/08/23 22:10:01 tsi Exp $ */
43
44 #include <x86emu/x86emu.h>
45 #include <x86emu/regs.h>
46 #include "debug.h"
47 #include "prim_ops.h"
48 #ifdef COREBOOT_VERSION /* Coreboot needs to map printf to printk. */
49 #include "arch/io.h"
50 #else
51 #include <sys/io.h>
52 #endif
53
54 #ifdef IN_MODULE
55 #include "xf86_ansic.h"
56 #else
57 #include <string.h>
58 #endif
59 /*------------------------- Global Variables ------------------------------*/
60
61 X86EMU_sysEnv _X86EMU_env;      /* Global emulator machine state */
62 X86EMU_intrFuncs _X86EMU_intrTab[256];
63
64 /*----------------------------- Implementation ----------------------------*/
65
66 /* compute a pointer. This replaces code scattered all over the place! */
67 u8 *mem_ptr(u32 addr, int size)
68 {
69         u8 *retaddr = 0;
70
71         if (addr > M.mem_size - size) {
72                 DB(printk("mem_ptr: address %#x out of range!\n", addr);)
73                     HALT_SYS();
74         }
75         if (addr < 0x200) {
76                 //printk("%x:%x updating int vector 0x%x\n",
77                 //       M.x86.R_CS, M.x86.R_IP, addr >> 2);
78         }
79         retaddr = (u8 *) (M.mem_base + addr);
80
81         return retaddr;
82 }
83
84 /****************************************************************************
85 PARAMETERS:
86 addr    - Emulator memory address to read
87
88 RETURNS:
89 Byte value read from emulator memory.
90
91 REMARKS:
92 Reads a byte value from the emulator memory. 
93 ****************************************************************************/
94 u8 X86API rdb(u32 addr)
95 {
96         u8 val;
97         u8 *ptr;
98
99         ptr = mem_ptr(addr, 1);
100
101         val = *ptr;
102         DB(if (DEBUG_MEM_TRACE())
103            printk("%#08x 1 -> %#x\n", addr, val);)
104                 return val;
105 }
106
107 /****************************************************************************
108 PARAMETERS:
109 addr    - Emulator memory address to read
110
111 RETURNS:
112 Word value read from emulator memory.
113
114 REMARKS:
115 Reads a word value from the emulator memory.
116 ****************************************************************************/
117 u16 X86API rdw(u32 addr)
118 {
119         u16 val = 0;
120         u8 *ptr;
121
122         ptr = mem_ptr(addr, 2);
123         val = *(u16 *) (ptr);
124
125         DB(if (DEBUG_MEM_TRACE())
126            printk("%#08x 2 -> %#x\n", addr, val);)
127         return val;
128 }
129
130 /****************************************************************************
131 PARAMETERS:
132 addr    - Emulator memory address to read
133
134 RETURNS:
135 Long value read from emulator memory.
136 REMARKS:
137 Reads a long value from the emulator memory. 
138 ****************************************************************************/
139 u32 X86API rdl(u32 addr)
140 {
141         u32 val = 0;
142         u8 *ptr;
143
144         ptr = mem_ptr(addr, 4);
145         val = *(u32 *) (ptr);
146
147         DB(if (DEBUG_MEM_TRACE())
148            printk("%#08x 4 -> %#x\n", addr, val);)
149         return val;
150 }
151
152 /****************************************************************************
153 PARAMETERS:
154 addr    - Emulator memory address to read
155 val             - Value to store
156
157 REMARKS:
158 Writes a byte value to emulator memory.
159 ****************************************************************************/
160 void X86API wrb(u32 addr, u8 val)
161 {
162         u8 *ptr;
163
164         ptr = mem_ptr(addr, 1);
165         *(u8 *) (ptr) = val;
166
167         DB(if (DEBUG_MEM_TRACE())
168            printk("%#08x 1 <- %#x\n", addr, val);)
169 }
170
171 /****************************************************************************
172 PARAMETERS:
173 addr    - Emulator memory address to read
174 val             - Value to store
175
176 REMARKS:
177 Writes a word value to emulator memory.
178 ****************************************************************************/
179 void X86API wrw(u32 addr, u16 val)
180 {
181         u8 *ptr;
182
183         ptr = mem_ptr(addr, 2);
184         *(u16 *) (ptr) = val;
185
186         DB(if (DEBUG_MEM_TRACE())
187            printk("%#08x 2 <- %#x\n", addr, val);)
188 }
189
190 /****************************************************************************
191 PARAMETERS:
192 addr    - Emulator memory address to read
193 val             - Value to store
194
195 REMARKS:
196 Writes a long value to emulator memory. 
197 ****************************************************************************/
198 void X86API wrl(u32 addr, u32 val)
199 {
200         u8 *ptr;
201
202         ptr = mem_ptr(addr, 4);
203         *(u32 *) (ptr) = val;
204
205         DB(if (DEBUG_MEM_TRACE())
206            printk("%#08x 4 <- %#x\n", addr, val);)
207
208
209 }
210
211 /****************************************************************************
212 PARAMETERS:
213 addr    - PIO address to read
214 RETURN:
215 0
216 REMARKS:
217 Default PIO byte read function. Doesn't perform real inb.
218 ****************************************************************************/
219 static u8 X86API p_inb(X86EMU_pioAddr addr)
220 {
221         DB(if (DEBUG_IO_TRACE())
222                 printk("inb %#04x \n", addr);)
223         return inb(addr);
224 }
225
226 /****************************************************************************
227 PARAMETERS:
228 addr    - PIO address to read
229 RETURN:
230 0
231 REMARKS:
232 Default PIO word read function. Doesn't perform real inw.
233 ****************************************************************************/
234 static u16 X86API p_inw(X86EMU_pioAddr addr)
235 {
236         DB(if (DEBUG_IO_TRACE())
237                 printk("inw %#04x \n", addr);)
238         return inw(addr);
239 }
240
241 /****************************************************************************
242 PARAMETERS:
243 addr    - PIO address to read
244 RETURN:
245 0
246 REMARKS:
247 Default PIO long read function. Doesn't perform real inl.
248 ****************************************************************************/
249 static u32 X86API p_inl(X86EMU_pioAddr addr)
250 {
251         DB(if (DEBUG_IO_TRACE())
252                 printk("inl %#04x \n", addr);)
253         return inl(addr);
254 }
255
256 /****************************************************************************
257 PARAMETERS:
258 addr    - PIO address to write
259 val     - Value to store
260 REMARKS:
261 Default PIO byte write function. Doesn't perform real outb.
262 ****************************************************************************/
263 static void X86API p_outb(X86EMU_pioAddr addr, u8 val)
264 {
265         DB(if (DEBUG_IO_TRACE())
266                 printk("outb %#02x -> %#04x \n", val, addr);)
267         outb(val, addr);
268         return;
269 }
270
271 /****************************************************************************
272 PARAMETERS:
273 addr    - PIO address to write
274 val     - Value to store
275 REMARKS:
276 Default PIO word write function. Doesn't perform real outw.
277 ****************************************************************************/
278 static void X86API p_outw(X86EMU_pioAddr addr, u16 val)
279 {
280         DB(if (DEBUG_IO_TRACE())
281                 printk("outw %#04x -> %#04x \n", val, addr);)
282         outw(val, addr);
283         return;
284 }
285
286 /****************************************************************************
287 PARAMETERS:
288 addr    - PIO address to write
289 val     - Value to store
290 REMARKS:
291 Default PIO ;ong write function. Doesn't perform real outl.
292 ****************************************************************************/
293 static void X86API p_outl(X86EMU_pioAddr addr, u32 val)
294 {
295         DB(if (DEBUG_IO_TRACE())
296                printk("outl %#08x -> %#04x \n", val, addr);)
297
298         outl(val, addr);
299         return;
300 }
301
302 /*------------------------- Global Variables ------------------------------*/
303
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;
316
317 /*----------------------------- Setup -------------------------------------*/
318
319 /****************************************************************************
320 PARAMETERS:
321 funcs   - New memory function pointers to make active
322
323 REMARKS:
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)
329 {
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;
336 }
337
338 /****************************************************************************
339 PARAMETERS:
340 funcs   - New programmed I/O function pointers to make active
341
342 REMARKS:
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)
348 {
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;
355 }
356
357 /****************************************************************************
358 PARAMETERS:
359 funcs   - New interrupt vector table to make active
360
361 REMARKS:
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
368 interrupts.
369 ****************************************************************************/
370 void X86EMU_setupIntrFuncs(X86EMU_intrFuncs funcs[])
371 {
372         int i;
373
374         for (i = 0; i < 256; i++)
375                 _X86EMU_intrTab[i] = NULL;
376         if (funcs) {
377                 for (i = 0; i < 256; i++)
378                         _X86EMU_intrTab[i] = funcs[i];
379         }
380 }
381
382 /****************************************************************************
383 PARAMETERS:
384 int     - New software interrupt to prepare for
385
386 REMARKS:
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)
395 {
396         push_word((u16) M.x86.R_FLG);
397         CLEAR_FLAG(F_IF);
398         CLEAR_FLAG(F_TF);
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);
403         M.x86.intr = 0;
404 }
405
406 void X86EMU_setMemBase(void *base, size_t size)
407 {
408         M.mem_base = (unsigned long) base;
409         M.mem_size = size;
410 }