- Use spin lock instead of default mutex for safety. INT/IRET instructions

save/clear/restore flags but emulated flags have no effect on the host.
I believe BIOS writers never meant to run their code in emulated
environment with interrupt enabled. :-)
- Use memcpy(3) instead of copying individual members.  I believe struct
x86regs was intentionally copied from the first half of struct x86emu_regs
for this very purpose.
- Fix some style nits and consistencies.

Reviewed by:	delphij, paradox (ddkprog yahoo com)
This commit is contained in:
jkim 2009-09-23 20:13:36 +00:00
parent 0ac2765e98
commit 8f4cddf2f3

View File

@ -9,16 +9,12 @@ __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/module.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>
@ -32,39 +28,43 @@ static u_char *pbiosStack = NULL;
int busySegMap[5];
static struct x86emu xbios86emu;
static struct x86emu x86bios_emu;
static struct mtx x86bios_lock;
static uint8_t
vm86_emu_inb(struct x86emu *emu, uint16_t port)
x86bios_emu_inb(struct x86emu *emu, uint16_t port)
{
if (port == 0xb2) /* APM scratch register */
return 0;
return (0);
if (port >= 0x80 && port < 0x88) /* POST status register */
return 0;
return inb(port);
return (0);
return (inb(port));
}
static uint16_t
vm86_emu_inw(struct x86emu *emu, uint16_t port)
x86bios_emu_inw(struct x86emu *emu, uint16_t port)
{
if (port >= 0x80 && port < 0x88) /* POST status register */
return 0;
return inw(port);
return (0);
return (inw(port));
}
static uint32_t
vm86_emu_inl(struct x86emu *emu, uint16_t port)
x86bios_emu_inl(struct x86emu *emu, uint16_t port)
{
if (port >= 0x80 && port < 0x88) /* POST status register */
return 0;
return inl(port);
return (0);
return (inl(port));
}
static void
vm86_emu_outb(struct x86emu *emu, uint16_t port, uint8_t val)
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 */
@ -73,16 +73,18 @@ vm86_emu_outb(struct x86emu *emu, uint16_t port, uint8_t val)
}
static void
vm86_emu_outw(struct x86emu *emu, uint16_t port, uint16_t val)
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
vm86_emu_outl(struct x86emu *emu, uint16_t port, uint32_t val)
x86bios_emu_outl(struct x86emu *emu, uint16_t port, uint32_t val)
{
if (port >= 0x80 && port < 0x88) /* POST status register */
return;
outl(port, val);
@ -91,59 +93,23 @@ vm86_emu_outl(struct x86emu *emu, uint16_t port, uint32_t val)
void
x86biosCall(struct x86regs *regs, int intno)
{
if (intno < 0 || intno > 255)
return;
mtx_lock(&x86bios_lock);
critical_enter();
mtx_lock_spin(&x86bios_lock);
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;
memcpy(&x86bios_emu.x86, regs, sizeof(*regs));
x86emu_exec_intr(&x86bios_emu, intno);
memcpy(regs, &x86bios_emu.x86, sizeof(*regs));
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);
mtx_unlock_spin(&x86bios_lock);
}
void *
x86biosOffs(uint32_t offs)
{
return (pbiosMem + offs);
}
@ -152,23 +118,23 @@ x86bios_init(void *arg __unused)
{
int offs;
mtx_init(&x86bios_lock, "x86bios lock", NULL, MTX_DEF);
mtx_init(&x86bios_lock, "x86bios lock", NULL, MTX_SPIN);
/* Can pbiosMem be NULL here? */
pbiosMem = pmap_mapbios(0x0, MAPPED_MEMORY_SIZE);
memset(&xbios86emu, 0, sizeof(xbios86emu));
x86emu_init_default(&xbios86emu);
memset(&x86bios_emu, 0, sizeof(x86bios_emu));
x86emu_init_default(&x86bios_emu);
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;
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;
xbios86emu.mem_base = (char *)pbiosMem;
xbios86emu.mem_size = 1024 * 1024;
x86bios_emu.mem_base = (char *)pbiosMem;
x86bios_emu.mem_size = 1024 * 1024;
memset(busySegMap, 0, sizeof(busySegMap));
@ -178,6 +144,7 @@ x86bios_init(void *arg __unused)
static void
x86bios_uninit(void *arg __unused)
{
x86biosFree(pbiosStack, 1);
if (pbiosMem)
@ -215,4 +182,3 @@ static moduledata_t x86bios_mod = {
DECLARE_MODULE(x86bios, x86bios_mod, SI_SUB_CPU, SI_ORDER_ANY);
MODULE_VERSION(x86bios, 1);