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