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