diff options
Diffstat (limited to 'sys/dev/x86bios')
-rw-r--r-- | sys/dev/x86bios/x86bios.c | 218 | ||||
-rw-r--r-- | sys/dev/x86bios/x86bios.h | 138 | ||||
-rw-r--r-- | sys/dev/x86bios/x86bios_alloc.c | 81 |
3 files changed, 437 insertions, 0 deletions
diff --git a/sys/dev/x86bios/x86bios.c b/sys/dev/x86bios/x86bios.c new file mode 100644 index 0000000..f531f1a --- /dev/null +++ b/sys/dev/x86bios/x86bios.c @@ -0,0 +1,218 @@ +/*- + * Written by paradox <ddkprog@yahoo.com> + * Public domain. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include "opt_x86bios.h" + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/module.h> +#include <sys/lock.h> +#include <sys/mutex.h> + +#include <vm/vm.h> +#include <vm/vm_extern.h> +#include <vm/vm_kern.h> +#include <vm/vm_param.h> +#include <vm/pmap.h> + +#include <machine/cpufunc.h> + +#include <contrib/x86emu/x86emu.h> +#include <contrib/x86emu/x86emu_regs.h> +#include <dev/x86bios/x86bios.h> + +unsigned char *pbiosMem = NULL; +static unsigned char *pbiosStack = NULL; + +int busySegMap[5]; + +static struct x86emu xbios86emu; + +static struct mtx x86bios_lock; + +static uint8_t +vm86_emu_inb(struct x86emu *emu, uint16_t port) +{ + if (port == 0xb2) /* APM scratch register */ + return 0; + if (port >= 0x80 && port < 0x88) /* POST status register */ + return 0; + return inb(port); +} + +static uint16_t +vm86_emu_inw(struct x86emu *emu, uint16_t port) +{ + if (port >= 0x80 && port < 0x88) /* POST status register */ + return 0; + return inw(port); +} + +static uint32_t +vm86_emu_inl(struct x86emu *emu, uint16_t port) +{ + if (port >= 0x80 && port < 0x88) /* POST status register */ + return 0; + return inl(port); +} + +static void +vm86_emu_outb(struct x86emu *emu, uint16_t port, uint8_t val) +{ + if (port == 0xb2) /* APM scratch register */ + return; + if (port >= 0x80 && port < 0x88) /* POST status register */ + return; + outb(port, val); +} + +static void +vm86_emu_outw(struct x86emu *emu, uint16_t port, uint16_t val) +{ + if (port >= 0x80 && port < 0x88) /* POST status register */ + return; + outw(port, val); +} + +static void +vm86_emu_outl(struct x86emu *emu, uint16_t port, uint32_t val) +{ + if (port >= 0x80 && port < 0x88) /* POST status register */ + return; + outl(port, val); +} + +void +x86biosCall(struct x86regs *regs, int intno) +{ + if (intno < 0 || intno > 255) + return; + + mtx_lock(&x86bios_lock); + critical_enter(); + + xbios86emu.x86.R_EAX = regs->R_EAX; + xbios86emu.x86.R_EBX = regs->R_EBX; + xbios86emu.x86.R_ECX = regs->R_ECX; + xbios86emu.x86.R_EDX = regs->R_EDX; + + xbios86emu.x86.R_ESP = regs->R_ESP; + xbios86emu.x86.R_EBP = regs->R_EBP; + xbios86emu.x86.R_ESI = regs->R_ESI; + xbios86emu.x86.R_EDI = regs->R_EDI; + xbios86emu.x86.R_EIP = regs->R_EIP; + xbios86emu.x86.R_EFLG = regs->R_EFLG; + + xbios86emu.x86.R_CS = regs->R_CS; + xbios86emu.x86.R_DS = regs->R_DS; + xbios86emu.x86.R_SS = regs->R_SS; + xbios86emu.x86.R_ES = regs->R_ES; + xbios86emu.x86.R_FS = regs->R_FS; + xbios86emu.x86.R_GS = regs->R_GS; + + x86emu_exec_intr(&xbios86emu, intno); + + regs->R_EAX = xbios86emu.x86.R_EAX; + regs->R_EBX = xbios86emu.x86.R_EBX; + regs->R_ECX = xbios86emu.x86.R_ECX; + regs->R_EDX = xbios86emu.x86.R_EDX; + + regs->R_ESP = xbios86emu.x86.R_ESP; + regs->R_EBP = xbios86emu.x86.R_EBP; + regs->R_ESI = xbios86emu.x86.R_ESI; + regs->R_EDI = xbios86emu.x86.R_EDI; + regs->R_EIP = xbios86emu.x86.R_EIP; + regs->R_EFLG = xbios86emu.x86.R_EFLG; + + regs->R_CS = xbios86emu.x86.R_CS; + regs->R_DS = xbios86emu.x86.R_DS; + regs->R_SS = xbios86emu.x86.R_SS; + regs->R_ES = xbios86emu.x86.R_ES; + regs->R_FS = xbios86emu.x86.R_FS; + regs->R_GS = xbios86emu.x86.R_GS; + + critical_exit(); + mtx_unlock(&x86bios_lock); +} + +void * +x86biosOffs(uint32_t offs) +{ + return (pbiosMem + offs); +} + +static void +x86bios_init(void *arg __unused) +{ + int offs; + + mtx_init(&x86bios_lock, "x86bios lock", NULL, MTX_DEF); + + /* Can pbiosMem be NULL here? */ + pbiosMem = pmap_mapbios(0x0, MAPPED_MEMORY_SIZE); + + memset(&xbios86emu, 0, sizeof(xbios86emu)); + x86emu_init_default(&xbios86emu); + + xbios86emu.emu_inb = vm86_emu_inb; + xbios86emu.emu_inw = vm86_emu_inw; + xbios86emu.emu_inl = vm86_emu_inl; + xbios86emu.emu_outb = vm86_emu_outb; + xbios86emu.emu_outw = vm86_emu_outw; + xbios86emu.emu_outl = vm86_emu_outl; + + xbios86emu.mem_base = (char *)pbiosMem; + xbios86emu.mem_size = 1024 * 1024; + + memset(busySegMap, 0, sizeof(busySegMap)); + + pbiosStack = x86biosAlloc(1, &offs); +} + +static void +x86bios_uninit(void *arg __unused) +{ + x86biosFree(pbiosStack, 1); + + if (pbiosMem) + pmap_unmapdev((vm_offset_t)pbiosMem, + MAPPED_MEMORY_SIZE); + + mtx_destroy(&x86bios_lock); +} + +static int +x86bios_modevent(module_t mod __unused, int type, void *data __unused) +{ + int err = 0; + + switch (type) { + case MOD_LOAD: + x86bios_init(NULL); + break; + case MOD_UNLOAD: + x86bios_uninit(NULL); + break; + default: + err = ENOTSUP; + break; + } + + return (err); +} + +static moduledata_t x86bios_mod = { + "x86bios", + x86bios_modevent, + NULL, +}; + +DECLARE_MODULE(x86bios, x86bios_mod, SI_SUB_KLD, SI_ORDER_ANY); +MODULE_VERSION(x86bios, 1); + diff --git a/sys/dev/x86bios/x86bios.h b/sys/dev/x86bios/x86bios.h new file mode 100644 index 0000000..3785f3c --- /dev/null +++ b/sys/dev/x86bios/x86bios.h @@ -0,0 +1,138 @@ +/*- + * Written by paradox <ddkprog@yahoo.com> + * Public domain. + * + * x86 registers were borrowed from x86emu.h x86emu_regs.h + * for compatability. + * + * $FreeBSD$ + */ + +#ifndef _X86BIOS_H_ +#define _X86BIOS_H_ + +#include <sys/types.h> +#include <sys/endian.h> +#include <sys/systm.h> + +#ifdef __BIG_ENDIAN__ + +struct x86_register32 { + uint32_t e_reg; +}; + +struct x86_register16 { + uint16_t filler0; + uint16_t x_reg; +}; + +struct x86_register8 { + uint8_t filler0, filler1; + uint8_t h_reg, l_reg; +}; + +#else /* !__BIG_ENDIAN__ */ + +struct x86_register32 { + uint32_t e_reg; +}; + +struct x86_register16 { + uint16_t x_reg; +}; + +struct x86_register8 { + uint8_t l_reg, h_reg; +}; + +#endif /* __BIG_ENDIAN__ */ + +union x86_register { + struct x86_register32 I32_reg; + struct x86_register16 I16_reg; + struct x86_register8 I8_reg; +}; + +struct x86regs { + uint16_t register_cs; + uint16_t register_ds; + uint16_t register_es; + uint16_t register_fs; + uint16_t register_gs; + uint16_t register_ss; + uint32_t register_flags; + union x86_register register_a; + union x86_register register_b; + union x86_register register_c; + union x86_register register_d; + + union x86_register register_sp; + union x86_register register_bp; + union x86_register register_si; + union x86_register register_di; + union x86_register register_ip; +}; + +typedef struct x86regs x86regs_t; + +/* 8 bit registers */ +#define R_AH register_a.I8_reg.h_reg +#define R_AL register_a.I8_reg.l_reg +#define R_BH register_b.I8_reg.h_reg +#define R_BL register_b.I8_reg.l_reg +#define R_CH register_c.I8_reg.h_reg +#define R_CL register_c.I8_reg.l_reg +#define R_DH register_d.I8_reg.h_reg +#define R_DL register_d.I8_reg.l_reg + +/* 16 bit registers */ +#define R_AX register_a.I16_reg.x_reg +#define R_BX register_b.I16_reg.x_reg +#define R_CX register_c.I16_reg.x_reg +#define R_DX register_d.I16_reg.x_reg + +/* 32 bit extended registers */ +#define R_EAX register_a.I32_reg.e_reg +#define R_EBX register_b.I32_reg.e_reg +#define R_ECX register_c.I32_reg.e_reg +#define R_EDX register_d.I32_reg.e_reg + +/* special registers */ +#define R_SP register_sp.I16_reg.x_reg +#define R_BP register_bp.I16_reg.x_reg +#define R_SI register_si.I16_reg.x_reg +#define R_DI register_di.I16_reg.x_reg +#define R_IP register_ip.I16_reg.x_reg +#define R_FLG register_flags + +/* special registers */ +#define R_ESP register_sp.I32_reg.e_reg +#define R_EBP register_bp.I32_reg.e_reg +#define R_ESI register_si.I32_reg.e_reg +#define R_EDI register_di.I32_reg.e_reg +#define R_EIP register_ip.I32_reg.e_reg +#define R_EFLG register_flags + +/* segment registers */ +#define R_CS register_cs +#define R_DS register_ds +#define R_SS register_ss +#define R_ES register_es +#define R_FS register_fs +#define R_GS register_gs + +#define SEG_ADDR(x) (((x) >> 4) & 0x00F000) +#define SEG_OFF(x) ((x) & 0x0FFFF) +#define FARP(x) ((le32toh(x) & 0xffff) + ((le32toh(x) >> 12) & 0xffff00)) + +#define MAPPED_MEMORY_SIZE 0xc00000 +#define PAGE_RESERV (4096*5) + +__BEGIN_DECLS +void x86biosCall(struct x86regs *regs, int intno); +void *x86biosAlloc(int count, int *segs); +void x86biosFree(void *pbuf, int count); +void *x86biosOffs(uint32_t offs); +__END_DECLS + +#endif /* !_X86BIOS_H_ */ diff --git a/sys/dev/x86bios/x86bios_alloc.c b/sys/dev/x86bios/x86bios_alloc.c new file mode 100644 index 0000000..e790e9d --- /dev/null +++ b/sys/dev/x86bios/x86bios_alloc.c @@ -0,0 +1,81 @@ +/*- + * Copyright (C) 1999 Egbert Eich + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and that + * both that copyright notice and this permission notice appear in + * supporting documentation, and that the name of the authors not be used + * in advertising or publicity pertaining to distribution of the software + * without specific, written prior permission. The authors makes no + * representations about the suitability of this software for any purpose. + * It is provided "as is" without express or implied warranty. + * + * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO + * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF + * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + * + * xserver/hw/xfree86/int10/generic.c + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> + +#include <dev/x86bios/x86bios.h> + +extern unsigned char *pbiosMem; +extern int busySegMap[5]; + +void * +x86biosAlloc(int count, int *segs) +{ + int i; + int j; + + /* find the free segblock of page */ + for (i = 0; i < (PAGE_RESERV - count); i++) + { + if (busySegMap[i] == 0) + { + /* find the capacity of segblock */ + for (j = i; j < (i + count); j++) + { + if (busySegMap[j] == 1) + break; + } + + if (j == (i + count)) + break; + i += count; + } + } + + if (i == (PAGE_RESERV - count)) + return NULL; + + /* make the segblock is used */ + for (j = i; j < (i + count); j++) + busySegMap[i] = 1; + + *segs = i * 4096; + + return (pbiosMem + *segs); +} + +void +x86biosFree(void *pbuf, int count) +{ + int i; + int busySeg; + + busySeg = ((unsigned char *)pbuf - (unsigned char *)pbiosMem)/4096; + + for (i = busySeg; i < (busySeg + count); i++) + busySegMap[i] = 0; +} |