2012-04-09 15:56:20 +02:00
|
|
|
/* VirtualBox driver - by D.C. van Moolenbroek */
|
|
|
|
/*
|
|
|
|
* This driver currently performs two tasks:
|
|
|
|
* - synchronizing to the host system time;
|
|
|
|
* - providing an interface for HGCM communication with the host system.
|
|
|
|
*/
|
2013-12-03 11:51:21 +01:00
|
|
|
#include <minix/sysutil.h>
|
2011-11-23 18:15:43 +01:00
|
|
|
#include <minix/drivers.h>
|
|
|
|
#include <minix/driver.h>
|
|
|
|
#include <minix/optset.h>
|
|
|
|
#include <machine/pci.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2012-04-09 15:56:20 +02:00
|
|
|
#include "vmmdev.h"
|
|
|
|
#include "proto.h"
|
2011-11-23 18:15:43 +01:00
|
|
|
|
|
|
|
#define DEFAULT_INTERVAL 1 /* check host time every second */
|
|
|
|
#define DEFAULT_DRIFT 2 /* update time if delta is >= 2 secs */
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
static void *vir_ptr;
|
|
|
|
static phys_bytes phys_ptr;
|
|
|
|
static port_t port;
|
|
|
|
static u32_t ticks;
|
|
|
|
static int interval;
|
|
|
|
static int drift;
|
|
|
|
|
2012-04-09 15:56:20 +02:00
|
|
|
static unsigned int irq;
|
|
|
|
static int hook_id;
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
static struct optset optset_table[] = {
|
2011-11-23 18:15:43 +01:00
|
|
|
{ "interval", OPT_INT, &interval, 10 },
|
|
|
|
{ "drift", OPT_INT, &drift, 10 },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* vbox_request *
|
|
|
|
*===========================================================================*/
|
2012-04-09 15:56:20 +02:00
|
|
|
int vbox_request(struct VMMDevRequestHeader *header, phys_bytes addr,
|
|
|
|
int type, size_t size)
|
2011-11-23 18:15:43 +01:00
|
|
|
{
|
|
|
|
/* Perform a VirtualBox backdoor request. */
|
|
|
|
int r;
|
|
|
|
|
2012-04-09 15:56:20 +02:00
|
|
|
header->size = size;
|
|
|
|
header->version = VMMDEV_BACKDOOR_VERSION;
|
|
|
|
header->type = type;
|
|
|
|
header->result = VMMDEV_ERR_GENERIC;
|
2011-11-23 18:15:43 +01:00
|
|
|
|
2012-04-09 15:56:20 +02:00
|
|
|
if ((r = sys_outl(port, addr)) != OK)
|
2011-11-23 18:15:43 +01:00
|
|
|
panic("device I/O failed: %d", r);
|
|
|
|
|
2012-04-09 15:56:20 +02:00
|
|
|
return header->result;
|
2011-11-23 18:15:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* vbox_init *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
static int vbox_init(int UNUSED(type), sef_init_info_t *UNUSED(info))
|
2011-11-23 18:15:43 +01:00
|
|
|
{
|
|
|
|
/* Initialize the device. */
|
|
|
|
int devind;
|
|
|
|
u16_t vid, did;
|
|
|
|
struct VMMDevReportGuestInfo *req;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
interval = DEFAULT_INTERVAL;
|
|
|
|
drift = DEFAULT_DRIFT;
|
|
|
|
|
|
|
|
if (env_argc > 1)
|
|
|
|
optset_parse(optset_table, env_argv[1]);
|
|
|
|
|
|
|
|
pci_init();
|
|
|
|
|
|
|
|
r = pci_first_dev(&devind, &vid, &did);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (r != 1)
|
|
|
|
panic("backdoor device not found");
|
|
|
|
|
2012-04-09 15:56:20 +02:00
|
|
|
if (vid == VMMDEV_PCI_VID && did == VMMDEV_PCI_DID)
|
2011-11-23 18:15:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
r = pci_next_dev(&devind, &vid, &did);
|
|
|
|
}
|
|
|
|
|
|
|
|
pci_reserve(devind);
|
|
|
|
|
2012-03-08 00:01:25 +01:00
|
|
|
port = pci_attr_r32(devind, PCI_BAR) & PCI_BAR_IO_MASK;
|
|
|
|
|
2012-04-09 15:56:20 +02:00
|
|
|
irq = pci_attr_r8(devind, PCI_ILR);
|
2012-10-23 14:23:26 +02:00
|
|
|
hook_id = 0;
|
2012-04-09 15:56:20 +02:00
|
|
|
|
|
|
|
if ((r = sys_irqsetpolicy(irq, 0 /* IRQ_REENABLE */, &hook_id)) != OK)
|
|
|
|
panic("unable to register IRQ: %d", r);
|
|
|
|
|
|
|
|
if ((r = sys_irqenable(&hook_id)) != OK)
|
|
|
|
panic("unable to enable IRQ: %d", r);
|
|
|
|
|
2011-11-23 18:15:43 +01:00
|
|
|
if ((vir_ptr = alloc_contig(VMMDEV_BUF_SIZE, 0, &phys_ptr)) == NULL)
|
|
|
|
panic("unable to allocate memory");
|
|
|
|
|
|
|
|
req = (struct VMMDevReportGuestInfo *) vir_ptr;
|
2012-04-09 15:56:20 +02:00
|
|
|
req->add_version = VMMDEV_GUEST_VERSION;
|
|
|
|
req->os_type = VMMDEV_GUEST_OS_OTHER;
|
2011-11-23 18:15:43 +01:00
|
|
|
|
2012-04-09 15:56:20 +02:00
|
|
|
if ((r = vbox_request(&req->header, phys_ptr,
|
|
|
|
VMMDEV_REQ_REPORTGUESTINFO, sizeof(*req))) !=
|
2011-11-23 18:15:43 +01:00
|
|
|
VMMDEV_ERR_OK)
|
|
|
|
panic("backdoor device not functioning");
|
|
|
|
|
|
|
|
ticks = sys_hz() * interval;
|
|
|
|
|
|
|
|
sys_setalarm(ticks, 0);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2012-04-09 15:56:20 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* vbox_intr *
|
|
|
|
*===========================================================================*/
|
|
|
|
static void vbox_intr(void)
|
|
|
|
{
|
|
|
|
/* Process an interrupt. */
|
|
|
|
struct VMMDevEvents *req;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
req = (struct VMMDevEvents *) vir_ptr;
|
|
|
|
req->events = 0;
|
|
|
|
|
|
|
|
/* If we cannot retrieve the events mask, we cannot do anything with
|
|
|
|
* this or any future interrupt either, so return without reenabling
|
|
|
|
* interrupts.
|
|
|
|
*/
|
|
|
|
if ((r = vbox_request(&req->header, phys_ptr,
|
|
|
|
VMMDEV_REQ_ACKNOWLEDGEEVENTS, sizeof(*req))) !=
|
|
|
|
VMMDEV_ERR_OK) {
|
|
|
|
printf("VBOX: unable to retrieve event mask (%d)\n", r);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req->events & VMMDEV_EVENT_HGCM)
|
|
|
|
hgcm_intr();
|
|
|
|
|
|
|
|
if ((r = sys_irqenable(&hook_id)) != OK)
|
|
|
|
panic("unable to reenable IRQ: %d", r);
|
|
|
|
}
|
|
|
|
|
2011-11-23 18:15:43 +01:00
|
|
|
/*===========================================================================*
|
|
|
|
* vbox_update_time *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
static void vbox_update_time(void)
|
2011-11-23 18:15:43 +01:00
|
|
|
{
|
|
|
|
/* Update the current time if it has drifted too far. */
|
|
|
|
struct VMMDevReqHostTime *req;
|
|
|
|
time_t otime, ntime;
|
|
|
|
|
|
|
|
req = (struct VMMDevReqHostTime *) vir_ptr;
|
|
|
|
|
2012-04-09 15:56:20 +02:00
|
|
|
if (vbox_request(&req->header, phys_ptr, VMMDEV_REQ_HOSTTIME,
|
|
|
|
sizeof(*req)) == VMMDEV_ERR_OK) {
|
2011-11-23 18:15:43 +01:00
|
|
|
time(&otime); /* old time */
|
|
|
|
|
2013-09-20 19:47:18 +02:00
|
|
|
ntime = req->time / 1000; /* new time */
|
2011-11-23 18:15:43 +01:00
|
|
|
|
|
|
|
/* Make time go forward, if the difference exceeds the drift
|
|
|
|
* threshold. Never make time go backward.
|
|
|
|
*/
|
2013-09-20 19:47:18 +02:00
|
|
|
if ((ntime - otime) >= drift)
|
2011-11-23 18:15:43 +01:00
|
|
|
stime(&ntime);
|
|
|
|
}
|
|
|
|
|
|
|
|
sys_setalarm(ticks, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* vbox_signal *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
static void vbox_signal(int signo)
|
2011-11-23 18:15:43 +01:00
|
|
|
{
|
|
|
|
/* Process a signal. If it is a SIGTERM, terminate immediately. */
|
|
|
|
|
|
|
|
if (signo != SIGTERM) return;
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* sef_local_startup *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
static void sef_local_startup(void)
|
2011-11-23 18:15:43 +01:00
|
|
|
{
|
|
|
|
/* Perform local SEF initialization. */
|
|
|
|
|
|
|
|
sef_setcb_init_fresh(vbox_init);
|
|
|
|
sef_setcb_init_restart(vbox_init);
|
|
|
|
|
|
|
|
sef_setcb_signal_handler(vbox_signal);
|
|
|
|
|
|
|
|
sef_startup();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* main *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int main(int argc, char **argv)
|
2011-11-23 18:15:43 +01:00
|
|
|
{
|
|
|
|
/* The main message loop. */
|
|
|
|
message m;
|
|
|
|
int r, ipc_status;
|
|
|
|
|
|
|
|
env_setargs(argc, argv);
|
|
|
|
sef_local_startup();
|
|
|
|
|
|
|
|
while (TRUE) {
|
|
|
|
if ((r = driver_receive(ANY, &m, &ipc_status)) != OK)
|
|
|
|
panic("driver_receive failed: %d", r);
|
|
|
|
|
|
|
|
if (is_ipc_notify(ipc_status)) {
|
|
|
|
switch (m.m_source) {
|
2012-04-09 15:56:20 +02:00
|
|
|
case HARDWARE:
|
|
|
|
vbox_intr();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2011-11-23 18:15:43 +01:00
|
|
|
case CLOCK:
|
|
|
|
vbox_update_time();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf("VBOX: received notify from %d\n",
|
|
|
|
m.m_source);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-04-09 15:56:20 +02:00
|
|
|
hgcm_message(&m, ipc_status);
|
2011-11-23 18:15:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|