xref: /aosp_15_r20/external/coreboot/src/device/oprom/x86emu/sys.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
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>
46 #include <device/oprom/include/io.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! */
mem_ptr(u32 addr,int size)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 ****************************************************************************/
rdb(u32 addr)89 u8 X86API rdb(u32 addr)
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 ****************************************************************************/
rdw(u32 addr)112 u16 X86API rdw(u32 addr)
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 ****************************************************************************/
rdl(u32 addr)134 u32 X86API rdl(u32 addr)
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 ****************************************************************************/
wrb(u32 addr,u8 val)155 void X86API wrb(u32 addr, u8 val)
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 ****************************************************************************/
wrw(u32 addr,u16 val)174 void X86API wrw(u32 addr, u16 val)
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 ****************************************************************************/
wrl(u32 addr,u32 val)193 void X86API wrl(u32 addr, u32 val)
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 ****************************************************************************/
p_inb(X86EMU_pioAddr addr)212 static u8 X86API p_inb(X86EMU_pioAddr addr)
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 ****************************************************************************/
p_inw(X86EMU_pioAddr addr)227 static u16 X86API p_inw(X86EMU_pioAddr addr)
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 ****************************************************************************/
p_inl(X86EMU_pioAddr addr)242 static u32 X86API p_inl(X86EMU_pioAddr addr)
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 ****************************************************************************/
p_outb(X86EMU_pioAddr addr,u8 val)256 static void X86API p_outb(X86EMU_pioAddr addr, u8 val)
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 ****************************************************************************/
p_outw(X86EMU_pioAddr addr,u16 val)271 static void X86API p_outw(X86EMU_pioAddr addr, u16 val)
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 ****************************************************************************/
p_outl(X86EMU_pioAddr addr,u32 val)286 static void X86API p_outl(X86EMU_pioAddr addr, u32 val)
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 ****************************************************************************/
X86EMU_setupMemFuncs(X86EMU_memFuncs * funcs)321 void X86EMU_setupMemFuncs(X86EMU_memFuncs * funcs)
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 ****************************************************************************/
X86EMU_setupPioFuncs(X86EMU_pioFuncs * funcs)340 void X86EMU_setupPioFuncs(X86EMU_pioFuncs * funcs)
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 ****************************************************************************/
X86EMU_setupIntrFuncs(X86EMU_intrFuncs funcs[])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 ****************************************************************************/
X86EMU_prepareForInt(int num)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 
X86EMU_setMemBase(void * base,size_t size)399 void X86EMU_setMemBase(void *base, size_t size)
400 {
401 	M.mem_base = (unsigned long) base;
402 	M.mem_size = size;
403 }
404