1*341eb07dSderaadt /* $OpenBSD: x86emu_util.c,v 1.4 2009/06/06 06:05:27 deraadt Exp $ */ 2e208dfa2Spirofti /* $NetBSD: x86emu_util.c,v 1.2 2007/12/04 17:32:22 joerg Exp $ */ 3e208dfa2Spirofti 4e208dfa2Spirofti /**************************************************************************** 5e208dfa2Spirofti * 6e208dfa2Spirofti * Realmode X86 Emulator Library 7e208dfa2Spirofti * 8e208dfa2Spirofti * Copyright (C) 1996-1999 SciTech Software, Inc. 9e208dfa2Spirofti * Copyright (C) David Mosberger-Tang 10e208dfa2Spirofti * Copyright (C) 1999 Egbert Eich 11e208dfa2Spirofti * Copyright (C) 2007 Joerg Sonnenberger 12e208dfa2Spirofti * 13e208dfa2Spirofti * ======================================================================== 14e208dfa2Spirofti * 15e208dfa2Spirofti * Permission to use, copy, modify, distribute, and sell this software and 16e208dfa2Spirofti * its documentation for any purpose is hereby granted without fee, 17e208dfa2Spirofti * provided that the above copyright notice appear in all copies and that 18e208dfa2Spirofti * both that copyright notice and this permission notice appear in 19e208dfa2Spirofti * supporting documentation, and that the name of the authors not be used 20e208dfa2Spirofti * in advertising or publicity pertaining to distribution of the software 21e208dfa2Spirofti * without specific, written prior permission. The authors makes no 22e208dfa2Spirofti * representations about the suitability of this software for any purpose. 23e208dfa2Spirofti * It is provided "as is" without express or implied warranty. 24e208dfa2Spirofti * 25e208dfa2Spirofti * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 26e208dfa2Spirofti * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 27e208dfa2Spirofti * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 28e208dfa2Spirofti * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF 29e208dfa2Spirofti * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 30e208dfa2Spirofti * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 31e208dfa2Spirofti * PERFORMANCE OF THIS SOFTWARE. 32e208dfa2Spirofti * 33e208dfa2Spirofti ****************************************************************************/ 34e208dfa2Spirofti 35e208dfa2Spirofti #include <sys/param.h> 36e208dfa2Spirofti #include <sys/endian.h> 37e208dfa2Spirofti 384b013528Sderaadt #include <dev/x86emu/x86emu.h> 394b013528Sderaadt #include <dev/x86emu/x86emu_regs.h> 40e208dfa2Spirofti 41e208dfa2Spirofti 42e208dfa2Spirofti /**************************************************************************** 43e208dfa2Spirofti PARAMETERS: 44e208dfa2Spirofti addr - Emulator memory address to read 45e208dfa2Spirofti 46e208dfa2Spirofti RETURNS: 47e208dfa2Spirofti Byte value read from emulator memory. 48e208dfa2Spirofti 49e208dfa2Spirofti REMARKS: 50e208dfa2Spirofti Reads a byte value from the emulator memory. 51e208dfa2Spirofti ****************************************************************************/ 52e208dfa2Spirofti static uint8_t 536bf2980cSderaadt rdb(struct x86emu *emu, uint32_t addr) 54e208dfa2Spirofti { 55e208dfa2Spirofti if (addr > emu->mem_size - 1) 566bf2980cSderaadt x86emu_halt_sys(emu); 57e208dfa2Spirofti return emu->mem_base[addr]; 58e208dfa2Spirofti } 59e208dfa2Spirofti /**************************************************************************** 60e208dfa2Spirofti PARAMETERS: 61e208dfa2Spirofti addr - Emulator memory address to read 62e208dfa2Spirofti 63e208dfa2Spirofti RETURNS: 64e208dfa2Spirofti Word value read from emulator memory. 65e208dfa2Spirofti 66e208dfa2Spirofti REMARKS: 67e208dfa2Spirofti Reads a word value from the emulator memory. 68e208dfa2Spirofti ****************************************************************************/ 69e208dfa2Spirofti static uint16_t 706bf2980cSderaadt rdw(struct x86emu *emu, uint32_t addr) 71e208dfa2Spirofti { 72e208dfa2Spirofti if (addr > emu->mem_size - 2) 736bf2980cSderaadt x86emu_halt_sys(emu); 74*341eb07dSderaadt #ifdef __STRICT_ALIGNMENT 75*341eb07dSderaadt if (addr & 1) { 76*341eb07dSderaadt u_int8_t *a = emu->mem_base + addr; 77*341eb07dSderaadt u_int16_t r; 78*341eb07dSderaadt 79*341eb07dSderaadt r = ((*(a + 0) << 0) & 0x00ff) | 80*341eb07dSderaadt ((*(a + 1) << 8) & 0xff00); 81*341eb07dSderaadt return r; 82*341eb07dSderaadt } else 83*341eb07dSderaadt return letoh32(*(u_int32_t *)(emu->mem_base + addr)); 84*341eb07dSderaadt #else 85*341eb07dSderaadt return letoh16(*(u_int16_t *)(emu->mem_base + addr)); 86*341eb07dSderaadt #endif 87e208dfa2Spirofti } 88e208dfa2Spirofti /**************************************************************************** 89e208dfa2Spirofti PARAMETERS: 90e208dfa2Spirofti addr - Emulator memory address to read 91e208dfa2Spirofti 92e208dfa2Spirofti RETURNS: 93e208dfa2Spirofti Long value read from emulator memory. 94e208dfa2Spirofti REMARKS: 95e208dfa2Spirofti Reads a long value from the emulator memory. 96e208dfa2Spirofti ****************************************************************************/ 97e208dfa2Spirofti static uint32_t 986bf2980cSderaadt rdl(struct x86emu *emu, uint32_t addr) 99e208dfa2Spirofti { 100e208dfa2Spirofti if (addr > emu->mem_size - 4) 1016bf2980cSderaadt x86emu_halt_sys(emu); 102*341eb07dSderaadt #ifdef __STRICT_ALIGNMENT 103*341eb07dSderaadt if (addr & 3) { 104*341eb07dSderaadt u_int8_t *a = emu->mem_base + addr; 105*341eb07dSderaadt u_int32_t r; 106*341eb07dSderaadt 107*341eb07dSderaadt r = ((*(a + 0) << 0) & 0x000000ff) | 108*341eb07dSderaadt ((*(a + 1) << 8) & 0x0000ff00) | 109*341eb07dSderaadt ((*(a + 2) << 16) & 0x00ff0000) | 110*341eb07dSderaadt ((*(a + 3) << 24) & 0xff000000); 111*341eb07dSderaadt return r; 112*341eb07dSderaadt } else 113*341eb07dSderaadt return letoh32(*(u_int32_t *)(emu->mem_base + addr)); 114*341eb07dSderaadt #else 115*341eb07dSderaadt return letoh32(*(u_int32_t *)(emu->mem_base + addr)); 116*341eb07dSderaadt #endif 117e208dfa2Spirofti } 118e208dfa2Spirofti /**************************************************************************** 119e208dfa2Spirofti PARAMETERS: 120e208dfa2Spirofti addr - Emulator memory address to read 121e208dfa2Spirofti val - Value to store 122e208dfa2Spirofti 123e208dfa2Spirofti REMARKS: 124e208dfa2Spirofti Writes a byte value to emulator memory. 125e208dfa2Spirofti ****************************************************************************/ 126e208dfa2Spirofti static void 1276bf2980cSderaadt wrb(struct x86emu *emu, uint32_t addr, uint8_t val) 128e208dfa2Spirofti { 129e208dfa2Spirofti if (addr > emu->mem_size - 1) 1306bf2980cSderaadt x86emu_halt_sys(emu); 131e208dfa2Spirofti emu->mem_base[addr] = val; 132e208dfa2Spirofti } 133e208dfa2Spirofti /**************************************************************************** 134e208dfa2Spirofti PARAMETERS: 135e208dfa2Spirofti addr - Emulator memory address to read 136e208dfa2Spirofti val - Value to store 137e208dfa2Spirofti 138e208dfa2Spirofti REMARKS: 139e208dfa2Spirofti Writes a word value to emulator memory. 140e208dfa2Spirofti ****************************************************************************/ 141e208dfa2Spirofti static void 1426bf2980cSderaadt wrw(struct x86emu *emu, uint32_t addr, uint16_t val) 143e208dfa2Spirofti { 144e208dfa2Spirofti if (addr > emu->mem_size - 2) 1456bf2980cSderaadt x86emu_halt_sys(emu); 146*341eb07dSderaadt #ifdef __STRICT_ALIGNMENT 147*341eb07dSderaadt if (addr & 1) { 148*341eb07dSderaadt u_int8_t *a = emu->mem_base + addr; 149*341eb07dSderaadt 150*341eb07dSderaadt *((a + 0)) = (val >> 0) & 0xff; 151*341eb07dSderaadt *((a + 1)) = (val >> 8) & 0xff; 152*341eb07dSderaadt } else 153*341eb07dSderaadt *((u_int16_t *)(emu->mem_base + addr)) = htole16(val); 154*341eb07dSderaadt #else 155*341eb07dSderaadt *((u_int16_t *)(emu->mem_base + addr)) = htole16(val); 156*341eb07dSderaadt #endif 157e208dfa2Spirofti } 158e208dfa2Spirofti /**************************************************************************** 159e208dfa2Spirofti PARAMETERS: 160e208dfa2Spirofti addr - Emulator memory address to read 161e208dfa2Spirofti val - Value to store 162e208dfa2Spirofti 163e208dfa2Spirofti REMARKS: 164e208dfa2Spirofti Writes a long value to emulator memory. 165e208dfa2Spirofti ****************************************************************************/ 166e208dfa2Spirofti static void 1676bf2980cSderaadt wrl(struct x86emu *emu, uint32_t addr, uint32_t val) 168e208dfa2Spirofti { 169e208dfa2Spirofti if (addr > emu->mem_size - 4) 1706bf2980cSderaadt x86emu_halt_sys(emu); 171*341eb07dSderaadt #ifdef __STRICT_ALIGNMENT 172*341eb07dSderaadt if (addr & 3) { 173*341eb07dSderaadt u_int8_t *a = emu->mem_base + addr; 174*341eb07dSderaadt 175*341eb07dSderaadt *((a + 0) = (val >> 0) & 0xff; 176*341eb07dSderaadt *((a + 1) = (val >> 8) & 0xff; 177*341eb07dSderaadt *((a + 2) = (val >> 16) & 0xff; 178*341eb07dSderaadt *((a + 3) = (val >> 24) & 0xff; 179*341eb07dSderaadt } else 180*341eb07dSderaadt *((u_int32_t *)(emu->mem_base + addr)) = htole32(val); 181*341eb07dSderaadt #else 182*341eb07dSderaadt *((u_int32_t *)(emu->mem_base + addr)) = htole32(val); 183*341eb07dSderaadt #endif 184e208dfa2Spirofti } 185e208dfa2Spirofti 186e208dfa2Spirofti /*----------------------------- Setup -------------------------------------*/ 187e208dfa2Spirofti 188e208dfa2Spirofti void 1896bf2980cSderaadt x86emu_init_default(struct x86emu *emu) 190e208dfa2Spirofti { 191e208dfa2Spirofti int i; 192e208dfa2Spirofti 193e208dfa2Spirofti emu->emu_rdb = rdb; 194e208dfa2Spirofti emu->emu_rdw = rdw; 195e208dfa2Spirofti emu->emu_rdl = rdl; 196e208dfa2Spirofti emu->emu_wrb = wrb; 197e208dfa2Spirofti emu->emu_wrw = wrw; 198e208dfa2Spirofti emu->emu_wrl = wrl; 199e208dfa2Spirofti 200e208dfa2Spirofti for (i = 0; i < 256; i++) 2016bf2980cSderaadt emu->_x86emu_intrTab[i] = NULL; 202e208dfa2Spirofti } 203