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

