diff options
Diffstat (limited to 'sys/compat/x86bios')
-rw-r--r-- | sys/compat/x86bios/x86bios.c | 184 | ||||
-rw-r--r-- | sys/compat/x86bios/x86bios.h | 138 | ||||
-rw-r--r-- | sys/compat/x86bios/x86bios_alloc.c | 81 |
3 files changed, 403 insertions, 0 deletions
diff --git a/sys/compat/x86bios/x86bios.c b/sys/compat/x86bios/x86bios.c new file mode 100644 index 0000000..5cb6bdf --- /dev/null +++ b/sys/compat/x86bios/x86bios.c @@ -0,0 +1,184 @@ +/*- + * Written by paradox <ddkprog@yahoo.com> + * Public domain. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include "opt_x86bios.h" + +#include <sys/param.h> +#include <sys/kernel.h> +#include <sys/lock.h> +#include <sys/module.h> +#include <sys/mutex.h> + +#include <vm/vm.h> +#include <vm/pmap.h> + +#include <machine/cpufunc.h> + +#include <contrib/x86emu/x86emu.h> +#include <contrib/x86emu/x86emu_regs.h> +#include <compat/x86bios/x86bios.h> + +u_char *pbiosMem = NULL; +static u_char *pbiosStack = NULL; + +int busySegMap[5]; + +static struct x86emu x86bios_emu; + +static struct mtx x86bios_lock; + +static uint8_t +x86bios_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 +x86bios_emu_inw(struct x86emu *emu, uint16_t port) +{ + + if (port >= 0x80 && port < 0x88) /* POST status register */ + return (0); + return (inw(port)); +} + +static uint32_t +x86bios_emu_inl(struct x86emu *emu, uint16_t port) +{ + + if (port >= 0x80 && port < 0x88) /* POST status register */ + return (0); + return (inl(port)); +} + +static void +x86bios_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 +x86bios_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 +x86bios_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_spin(&x86bios_lock); + + memcpy(&x86bios_emu.x86, regs, sizeof(*regs)); + x86emu_exec_intr(&x86bios_emu, intno); + memcpy(regs, &x86bios_emu.x86, sizeof(*regs)); + + mtx_unlock_spin(&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_SPIN); + + /* Can pbiosMem be NULL here? */ + pbiosMem = pmap_mapbios(0x0, MAPPED_MEMORY_SIZE); + + memset(&x86bios_emu, 0, sizeof(x86bios_emu)); + x86emu_init_default(&x86bios_emu); + + x86bios_emu.emu_inb = x86bios_emu_inb; + x86bios_emu.emu_inw = x86bios_emu_inw; + x86bios_emu.emu_inl = x86bios_emu_inl; + x86bios_emu.emu_outb = x86bios_emu_outb; + x86bios_emu.emu_outw = x86bios_emu_outw; + x86bios_emu.emu_outl = x86bios_emu_outl; + + x86bios_emu.mem_base = (char *)pbiosMem; + x86bios_emu.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_CPU, SI_ORDER_ANY); +MODULE_VERSION(x86bios, 1); diff --git a/sys/compat/x86bios/x86bios.h b/sys/compat/x86bios/x86bios.h new file mode 100644 index 0000000..3785f3c --- /dev/null +++ b/sys/compat/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/compat/x86bios/x86bios_alloc.c b/sys/compat/x86bios/x86bios_alloc.c new file mode 100644 index 0000000..624a75c --- /dev/null +++ b/sys/compat/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 <compat/x86bios/x86bios.h> + +extern u_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 = ((u_char *)pbuf - pbiosMem) / 4096; + + for (i = busySeg; i < (busySeg + count); i++) + busySegMap[i] = 0; +} |