coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
sys.c
Go to the documentation of this file.
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 overridden by the
39 * user library.
40 *
41 ****************************************************************************/
42 
43 #include <arch/io.h>
44 #include <x86emu/x86emu.h>
45 #include <x86emu/regs.h>
47 #include "debug.h"
48 #include "prim_ops.h"
49 
50 #ifdef IN_MODULE
51 #include "xf86_ansic.h"
52 #else
53 #endif
54 /*------------------------- Global Variables ------------------------------*/
55 
56 X86EMU_sysEnv _X86EMU_env; /* Global emulator machine state */
57 X86EMU_intrFuncs _X86EMU_intrTab[256];
58 
59 /*----------------------------- Implementation ----------------------------*/
60 
61 /* compute a pointer. This replaces code scattered all over the place! */
62 static u8 *mem_ptr(u32 addr, int size)
63 {
64  u8 *retaddr = 0;
65 
66  if (addr > M.mem_size - size) {
67  DB(printf("%s: address %#x out of range!\n", __func__, addr);)
68  HALT_SYS();
69  }
70  if (addr < 0x200) {
71  //printf("%x:%x updating int vector 0x%x\n",
72  // M.x86.R_CS, M.x86.R_IP, addr >> 2);
73  }
74  retaddr = (u8 *) (M.mem_base + addr);
75 
76  return retaddr;
77 }
78 
79 /****************************************************************************
80 PARAMETERS:
81 addr - Emulator memory address to read
82 
83 RETURNS:
84 Byte value read from emulator memory.
85 
86 REMARKS:
87 Reads a byte value from the emulator memory.
88 ****************************************************************************/
90 {
91  u8 val;
92  u8 *ptr;
93 
94  ptr = mem_ptr(addr, 1);
95 
96  val = *ptr;
97  DB(if (DEBUG_MEM_TRACE())
98  printf("%#08x 1 -> %#x\n", addr, val);)
99  return val;
100 }
101 
102 /****************************************************************************
103 PARAMETERS:
104 addr - Emulator memory address to read
105 
106 RETURNS:
107 Word value read from emulator memory.
108 
109 REMARKS:
110 Reads a word value from the emulator memory.
111 ****************************************************************************/
113 {
114  u16 val = 0;
115  u8 *ptr;
116 
117  ptr = mem_ptr(addr, 2);
118  val = *(u16 *) (ptr);
119 
120  DB(if (DEBUG_MEM_TRACE())
121  printf("%#08x 2 -> %#x\n", addr, val);)
122  return val;
123 }
124 
125 /****************************************************************************
126 PARAMETERS:
127 addr - Emulator memory address to read
128 
129 RETURNS:
130 Long value read from emulator memory.
131 REMARKS:
132 Reads a long value from the emulator memory.
133 ****************************************************************************/
135 {
136  u32 val = 0;
137  u8 *ptr;
138 
139  ptr = mem_ptr(addr, 4);
140  val = *(u32 *) (ptr);
141 
142  DB(if (DEBUG_MEM_TRACE())
143  printf("%#08x 4 -> %#x\n", addr, val);)
144  return val;
145 }
146 
147 /****************************************************************************
148 PARAMETERS:
149 addr - Emulator memory address to read
150 val - Value to store
151 
152 REMARKS:
153 Writes a byte value to emulator memory.
154 ****************************************************************************/
156 {
157  u8 *ptr;
158 
159  ptr = mem_ptr(addr, 1);
160  *(u8 *) (ptr) = val;
161 
162  DB(if (DEBUG_MEM_TRACE())
163  printf("%#08x 1 <- %#x\n", addr, val);)
164 }
165 
166 /****************************************************************************
167 PARAMETERS:
168 addr - Emulator memory address to read
169 val - Value to store
170 
171 REMARKS:
172 Writes a word value to emulator memory.
173 ****************************************************************************/
175 {
176  u8 *ptr;
177 
178  ptr = mem_ptr(addr, 2);
179  *(u16 *) (ptr) = val;
180 
181  DB(if (DEBUG_MEM_TRACE())
182  printf("%#08x 2 <- %#x\n", addr, val);)
183 }
184 
185 /****************************************************************************
186 PARAMETERS:
187 addr - Emulator memory address to read
188 val - Value to store
189 
190 REMARKS:
191 Writes a long value to emulator memory.
192 ****************************************************************************/
194 {
195  u8 *ptr;
196 
197  ptr = mem_ptr(addr, 4);
198  *(u32 *) (ptr) = val;
199 
200  DB(if (DEBUG_MEM_TRACE())
201  printf("%#08x 4 <- %#x\n", addr, val);)
202 }
203 
204 /****************************************************************************
205 PARAMETERS:
206 addr - PIO address to read
207 RETURN:
208 0
209 REMARKS:
210 Default PIO byte read function. Doesn't perform real inb.
211 ****************************************************************************/
213 {
214  DB(if (DEBUG_IO_TRACE())
215  printf("inb %#04x\n", addr);)
216  return inb(addr);
217 }
218 
219 /****************************************************************************
220 PARAMETERS:
221 addr - PIO address to read
222 RETURN:
223 0
224 REMARKS:
225 Default PIO word read function. Doesn't perform real inw.
226 ****************************************************************************/
228 {
229  DB(if (DEBUG_IO_TRACE())
230  printf("inw %#04x\n", addr);)
231  return inw(addr);
232 }
233 
234 /****************************************************************************
235 PARAMETERS:
236 addr - PIO address to read
237 RETURN:
238 0
239 REMARKS:
240 Default PIO long read function. Doesn't perform real inl.
241 ****************************************************************************/
243 {
244  DB(if (DEBUG_IO_TRACE())
245  printf("inl %#04x\n", addr);)
246  return inl(addr);
247 }
248 
249 /****************************************************************************
250 PARAMETERS:
251 addr - PIO address to write
252 val - Value to store
253 REMARKS:
254 Default PIO byte write function. Doesn't perform real outb.
255 ****************************************************************************/
257 {
258  DB(if (DEBUG_IO_TRACE())
259  printf("outb %#02x -> %#04x\n", val, addr);)
260  outb(val, addr);
261  return;
262 }
263 
264 /****************************************************************************
265 PARAMETERS:
266 addr - PIO address to write
267 val - Value to store
268 REMARKS:
269 Default PIO word write function. Doesn't perform real outw.
270 ****************************************************************************/
272 {
273  DB(if (DEBUG_IO_TRACE())
274  printf("outw %#04x -> %#04x\n", val, addr);)
275  outw(val, addr);
276  return;
277 }
278 
279 /****************************************************************************
280 PARAMETERS:
281 addr - PIO address to write
282 val - Value to store
283 REMARKS:
284 Default PIO ;ong write function. Doesn't perform real outl.
285 ****************************************************************************/
287 {
288  DB(if (DEBUG_IO_TRACE())
289  printf("outl %#08x -> %#04x\n", val, addr);)
290 
291  outl(val, addr);
292  return;
293 }
294 
295 /*------------------------- Global Variables ------------------------------*/
296 
297 u8(X86APIP sys_rdb) (u32 addr) = rdb;
298 u16(X86APIP sys_rdw) (u32 addr) = rdw;
299 u32(X86APIP sys_rdl) (u32 addr) = rdl;
300 void (X86APIP sys_wrb) (u32 addr, u8 val) = wrb;
301 void (X86APIP sys_wrw) (u32 addr, u16 val) = wrw;
302 void (X86APIP sys_wrl) (u32 addr, u32 val) = wrl;
303 u8(X86APIP sys_inb) (X86EMU_pioAddr addr) = p_inb;
304 u16(X86APIP sys_inw) (X86EMU_pioAddr addr) = p_inw;
305 u32(X86APIP sys_inl) (X86EMU_pioAddr addr) = p_inl;
306 void (X86APIP sys_outb) (X86EMU_pioAddr addr, u8 val) = p_outb;
307 void (X86APIP sys_outw) (X86EMU_pioAddr addr, u16 val) = p_outw;
308 void (X86APIP sys_outl) (X86EMU_pioAddr addr, u32 val) = p_outl;
309 
310 /*----------------------------- Setup -------------------------------------*/
311 
312 /****************************************************************************
313 PARAMETERS:
314 funcs - New memory function pointers to make active
315 
316 REMARKS:
317 This function is used to set the pointers to functions which access
318 memory space, allowing the user application to override these functions
319 and hook them out as necessary for their application.
320 ****************************************************************************/
322 {
323  sys_rdb = funcs->rdb;
324  sys_rdw = funcs->rdw;
325  sys_rdl = funcs->rdl;
326  sys_wrb = funcs->wrb;
327  sys_wrw = funcs->wrw;
328  sys_wrl = funcs->wrl;
329 }
330 
331 /****************************************************************************
332 PARAMETERS:
333 funcs - New programmed I/O function pointers to make active
334 
335 REMARKS:
336 This function is used to set the pointers to functions which access
337 I/O space, allowing the user application to override these functions
338 and hook them out as necessary for their application.
339 ****************************************************************************/
341 {
342  sys_inb = funcs->inb;
343  sys_inw = funcs->inw;
344  sys_inl = funcs->inl;
345  sys_outb = funcs->outb;
346  sys_outw = funcs->outw;
347  sys_outl = funcs->outl;
348 }
349 
350 /****************************************************************************
351 PARAMETERS:
352 funcs - New interrupt vector table to make active
353 
354 REMARKS:
355 This function is used to set the pointers to functions which handle
356 interrupt processing in the emulator, allowing the user application to
357 hook interrupts as necessary for their application. Any interrupts that
358 are not hooked by the user application, and reflected and handled internally
359 in the emulator via the interrupt vector table. This allows the application
360 to get control when the code being emulated executes specific software
361 interrupts.
362 ****************************************************************************/
363 void X86EMU_setupIntrFuncs(X86EMU_intrFuncs funcs[])
364 {
365  int i;
366 
367  for (i = 0; i < 256; i++)
368  _X86EMU_intrTab[i] = NULL;
369  if (funcs) {
370  for (i = 0; i < 256; i++)
371  _X86EMU_intrTab[i] = funcs[i];
372  }
373 }
374 
375 /****************************************************************************
376 PARAMETERS:
377 int - New software interrupt to prepare for
378 
379 REMARKS:
380 This function is used to set up the emulator state to execute a software
381 interrupt. This can be used by the user application code to allow an
382 interrupt to be hooked, examined and then reflected back to the emulator
383 so that the code in the emulator will continue processing the software
384 interrupt as per normal. This essentially allows system code to actively
385 hook and handle certain software interrupts as necessary.
386 ****************************************************************************/
387 void X86EMU_prepareForInt(int num)
388 {
389  push_word((u16) M.x86.R_FLG);
390  CLEAR_FLAG(F_IF);
391  CLEAR_FLAG(F_TF);
392  push_word(M.x86.R_CS);
393  M.x86.R_CS = mem_access_word(num * 4 + 2);
394  push_word(M.x86.R_IP);
395  M.x86.R_IP = mem_access_word(num * 4);
396  M.x86.intr = 0;
397 }
398 
399 void X86EMU_setMemBase(void *base, size_t size)
400 {
401  M.mem_base = (unsigned long) base;
402  M.mem_size = size;
403 }
static u32 addr
Definition: cirrus.c:14
u8 inb(u16 port)
void outb(u8 val, u16 port)
u16 inw(u16 port)
u32 inl(u16 port)
void outl(u32 val, u16 port)
void outw(u16 val, u16 port)
u16 X86EMU_pioAddr
Definition: types.h:46
#define printf(x...)
Definition: debug.h:47
#define DB(x)
Definition: debug.h:197
#define DEBUG_MEM_TRACE()
Definition: debug.h:106
#define DEBUG_IO_TRACE()
Definition: debug.h:107
u16 mem_access_word(int addr)
Definition: prim_ops.c:2381
void push_word(u16 w)
Definition: prim_ops.c:2394
#define F_TF
Definition: regs.h:198
#define M
Definition: regs.h:327
#define F_IF
Definition: regs.h:199
#define CLEAR_FLAG(flag)
Definition: regs.h:205
uintptr_t base
Definition: uart.c:17
#define NULL
Definition: stddef.h:19
uint32_t u32
Definition: stdint.h:51
uint16_t u16
Definition: stdint.h:48
uint8_t u8
Definition: stdint.h:45
u32 X86API rdl(u32 addr)
Definition: sys.c:134
void X86API wrl(u32 addr, u32 val)
Definition: sys.c:193
u16(X86APIP sys_rdw)(u32 addr)
u8 val
Definition: sys.c:300
X86EMU_intrFuncs _X86EMU_intrTab[256]
Definition: sys.c:57
void X86EMU_setMemBase(void *base, size_t size)
Definition: sys.c:399
static void X86API p_outw(X86EMU_pioAddr addr, u16 val)
Definition: sys.c:271
void X86EMU_setupPioFuncs(X86EMU_pioFuncs *funcs)
Definition: sys.c:340
u8(X86APIP sys_rdb)(u32 addr)
void(X86APIP sys_wrb)(u32 addr
static u8 * mem_ptr(u32 addr, int size)
Definition: sys.c:62
void X86EMU_setupMemFuncs(X86EMU_memFuncs *funcs)
Definition: sys.c:321
void X86API wrb(u32 addr, u8 val)
Definition: sys.c:155
u32(X86APIP sys_rdl)(u32 addr)
u16 X86API rdw(u32 addr)
Definition: sys.c:112
static u16 X86API p_inw(X86EMU_pioAddr addr)
Definition: sys.c:227
static u8 X86API p_inb(X86EMU_pioAddr addr)
Definition: sys.c:212
static void X86API p_outb(X86EMU_pioAddr addr, u8 val)
Definition: sys.c:256
static void X86API p_outl(X86EMU_pioAddr addr, u32 val)
Definition: sys.c:286
void X86EMU_setupIntrFuncs(X86EMU_intrFuncs funcs[])
Definition: sys.c:363
void X86API wrw(u32 addr, u16 val)
Definition: sys.c:174
X86EMU_sysEnv _X86EMU_env
Definition: sys.c:56
void X86EMU_prepareForInt(int num)
Definition: sys.c:387
u8 X86API rdb(u32 addr)
Definition: sys.c:89
static u32 X86API p_inl(X86EMU_pioAddr addr)
Definition: sys.c:242
#define HALT_SYS()
Definition: x86emu.h:160
#define X86API
Definition: x86emu.h:50
#define X86APIP
Definition: x86emu.h:51