VBoxGuest.cpp revision 0213888ad929b42ba0ff6a3d3fb12dd350ae0ea1
/** @file
*
* VBoxGuest -- VirtualBox Win32 guest support driver
*
* Copyright (C) 2006 InnoTek Systemberatung GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License as published by the Free Software Foundation,
* in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
* distribution. VirtualBox OSE is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY of any kind.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*
*/
// enable backdoor logging
//#define LOG_ENABLED
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "VBoxGuest_Internal.h"
#ifdef TARGET_NT4
#include "NTLegacy.h"
#else
#include "VBoxGuestPnP.h"
#endif
#include "Helper.h"
#include <excpt.h>
#include <stdio.h>
#include <VBox/VBoxGuestLib.h>
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
extern "C"
{
}
/*******************************************************************************
* Exported Functions *
*******************************************************************************/
#ifdef ALLOC_PRAGMA
#endif
/**
* Driver entry point.
*
* @returns appropriate status code.
* @param pDrvObj Pointer to driver object.
* @param pRegPath Registry base path.
*/
{
dprintf(("VBoxGuest::DriverEntry: running on Windows NT version %d.%d, build %d\n", majorVersion, minorVersion, buildNumber));
switch (majorVersion)
{
case 6:
break;
case 5:
switch (minorVersion)
{
case 2:
winVersion = WIN2K3;
break;
case 1:
winVersion = WINXP;
break;
case 0:
winVersion = WIN2K;
break;
default:
dprintf(("VBoxGuest::DriverEntry: unknown version of Windows, refusing!\n"));
return STATUS_DRIVER_UNABLE_TO_LOAD;
}
break;
case 4:
winVersion = WINNT4;
break;
default:
dprintf(("VBoxGuest::DriverEntry: NT4 required!\n"));
return STATUS_DRIVER_UNABLE_TO_LOAD;
}
/*
* Setup the driver entry points in pDrvObj.
*/
#ifdef TARGET_NT4
#else
// pDrvObj->MajorFunction[IRP_MJ_SYSTEM_CONTROL] = VBoxGuestSystemControl;
#endif
return rc;
}
#ifndef TARGET_NT4
/**
* Handle request from the Plug & Play subsystem
*
* @returns NT status code
* @param pDrvObj Driver object
* @param pDevObj Device object
*/
{
dprintf(("VBoxGuest::VBoxGuestAddDevice\n"));
/*
* Create device.
*/
rc = IoCreateDevice(pDrvObj, sizeof(VBOXGUESTDEVEXT), &devName, FILE_DEVICE_UNKNOWN, 0, FALSE, &deviceObject);
if (!NT_SUCCESS(rc))
{
return rc;
}
if (!NT_SUCCESS(rc))
{
return rc;
}
/*
* Setup the device extension.
*/
{
dprintf(("VBoxGuest::VBoxGuestAddDevice: IoAttachDeviceToDeviceStack did not give a nextLowerDrive\n"));
return STATUS_DEVICE_NOT_CONNECTED;
}
// driver is ready now
return rc;
}
#endif
/**
* Unload the driver.
*
* @param pDrvObj Driver object.
*/
{
dprintf(("VBoxGuest::VBoxGuestUnload\n"));
#ifdef TARGET_NT4
if (pDevExt->workerThread)
{
dprintf(("VBoxGuest::VBoxGuestUnload: waiting for the worker thread to terminate...\n"));
dprintf(("VBoxGuest::VBoxGuestUnload: returned from KeWaitForSingleObject for worker thread\n"));
}
if (pDevExt->idleThread)
{
dprintf(("VBoxGuest::VBoxGuestUnload: waiting for the idle thread to terminate...\n"));
dprintf(("VBoxGuest::VBoxGuestUnload: returned from KeWaitForSingleObject for idle thread\n"));
}
/*
* I don't think it's possible to unload a driver which processes have
* opened, at least we'll blindly assume that here.
*/
#endif
dprintf(("VBoxGuest::VBoxGuestUnload: returning\n"));
}
/**
* Create (i.e. Open) file entry point.
*
* @param pDevObj Device object.
* @param pIrp Request packet.
*/
{
dprintf(("VBoxGuest::VBoxGuestCreate\n"));
/*
* We are not remotely similar to a directory...
* (But this is possible.)
*/
{
dprintf(("VBoxGuest::VBoxGuestCreate: we're not a directory!\n"));
return STATUS_NOT_A_DIRECTORY;
}
return rcNt;
}
/**
* Close file entry point.
*
* @param pDevObj Device object.
* @param pIrp Request packet.
*/
{
dprintf(("VBoxGuest::VBoxGuestClose\n"));
dprintf(("VBoxGuest::VBoxGuestClose: pDevExt=%p pFileObj=%p pSession=%p\n",
return STATUS_SUCCESS;
}
#ifdef VBOX_HGCM
{
dprintf(("VBoxHGCMCallback\n"));
// this code is subject to "lost wakeup" bug
// -- malc
{
/* Specifying UserMode so killing the user process will abort the wait. */
);
if (rc != STATUS_WAIT_0)
{
dprintf(("VBoxHGCMCallback: The external event was signalled or the wait timed out or terminated.\n"));
break;
}
}
return;
}
{
{
dprintf(("VBoxGuest::vboxHGCMVerifyIOBuffers: OutputBufferLength %d < %d\n",
return STATUS_INVALID_PARAMETER;
}
{
dprintf(("VBoxGuest::vboxHGCMVerifyIOBuffers: InputBufferLength %d < %d\n",
return STATUS_INVALID_PARAMETER;
}
return STATUS_SUCCESS;
}
#endif /* VBOX_HGCM */
static bool
{
_asm {
ret;
}
}
{
}
{
_asm {
ret;
}
}
{
bool result = false;
if (VBOX_SUCCESS (rc))
{
{
dprintf (("VBoxGuest::VBoxGuestDeviceControl: error issuing request to VMMDev! "
}
else
{
result = true;
}
}
return result;
}
/**
* Device I/O Control entry point.
*
* @param pDevObj Device object.
* @param pIrp Request packet.
*/
{
dprintf(("VBoxGuest::VBoxGuestDeviceControl\n"));
unsigned cbOut = 0;
{
{
dprintf(("VBoxGuest::VBoxGuestDeviceControl: IOCTL_VBOXGUEST_GETVMMDEVPORT\n"));
{
break;
}
cbOut = sizeof(VBoxGuestPortInfo);
break;
}
{
/* Need to be extended to support multiple waiters for an event,
* array of counters for each event, event mask is computed, each
* time a wait event is arrived.
*/
dprintf(("VBoxGuest::VBoxGuestDeviceControl: IOCTL_VBOXGUEST_WAITEVENT\n"));
{
dprintf(("VBoxGuest::VBoxGuestDeviceControl: OutputBufferLength %d < sizeof(VBoxGuestWaitEventInfo)\n",
break;
}
dprintf(("VBoxGuest::VBoxGuestDeviceControl: InputBufferLength %d < sizeof(VBoxGuestWaitEventInfo)\n",
break;
}
dprintf (("VBoxGuest::VBoxGuestDeviceControl: Invalid input mask %#x\n",
break;
}
eventInfo->u32EventFlagsOut = 0;
for (;;)
{
// following code is subject to "lost wakeup" bug
// -- malc
if (fEventPending)
{
break;
}
dprintf(("IOCTL_VBOXGUEST_WAITEVENT: Wait returned %d -> event %x\n", rc, eventInfo->u32EventFlagsOut));
if (rc != STATUS_SUCCESS)
{
/* There was a timeout or wait was interrupted, etc. */
break;
}
}
cbOut = sizeof(VBoxGuestWaitEventInfo);
break;
}
{
dprintf(("VBoxGuest::VBoxGuestDeviceControl: IOCTL_VBOXGUEST_VMMREQUEST\n"));
#define CHECK_SIZE(s) \
{ \
dprintf(("VBoxGuest::VBoxGuestDeviceControl: OutputBufferLength %d < %d\n", \
break; \
} \
dprintf(("VBoxGuest::VBoxGuestDeviceControl: InputBufferLength %d < %d\n", \
break; \
}
/* get the request header */
CHECK_SIZE(sizeof(VMMDevRequestHeader));
{
break;
}
/* make sure the buffers suit the request */
/* just perform the request */
int rc = VbglGRAlloc((VMMDevRequestHeader **)&req, vmmdevGetRequestSize(requestHeader->requestType), requestHeader->requestType);
if (VBOX_SUCCESS(rc))
{
/* copy the request information */
{
dprintf(("VBoxGuest::VBoxGuestDeviceControl IOCTL_VBOXGUEST_VMMREQUEST: error issuing request to VMMDev!"
}
else
{
/* copy result */
}
}
else
{
}
break;
}
{
dprintf (("VBoxGuest::VBoxGuestDeviceControl: InputBufferLength %d < %d\n",
sizeof (VBoxGuestFilterMaskInfo)));
break;
}
{
}
break;
}
#ifdef VBOX_HGCM
/* HGCM offers blocking IOCTLSs just like waitevent and actually
* uses the same waiting code.
*/
{
dprintf(("VBoxGuest::VBoxGuestDeviceControl: IOCTL_VBOXGUEST_HGCM_CONNECT\n"));
{
dprintf(("VBoxGuest::VBoxGuestDeviceControl: OutputBufferLength %d != sizeof(VBoxGuestHGCMConnectInfo) %d\n",
break;
}
dprintf(("VBoxGuest::VBoxGuestDeviceControl: InputBufferLength %d != sizeof(VBoxGuestHGCMConnectInfo) %d\n",
break;
}
/* If request will be processed asynchronously, execution will
* go to VBoxHGCMCallback. There it will wait for the request event, signalled from IRQ.
* On IRQ arrival, the VBoxHGCMCallback(s) will check the request memory and, if completion
* flag is set, returns.
*/
if (VBOX_FAILURE(rc))
{
}
else
{
}
} break;
{
dprintf(("VBoxGuest::VBoxGuestDeviceControl: IOCTL_VBOXGUEST_HGCM_DISCONNECT\n"));
{
dprintf(("VBoxGuest::VBoxGuestDeviceControl: OutputBufferLength %d != sizeof(VBoxGuestHGCMDisconnectInfo) %d\n",
break;
}
dprintf(("VBoxGuest::VBoxGuestDeviceControl: InputBufferLength %d != sizeof(VBoxGuestHGCMDisconnectInfo) %d\n",
break;
}
/* If request will be processed asynchronously, execution will
* go to VBoxHGCMCallback. There it will wait for the request event, signalled from IRQ.
* On IRQ arrival, the VBoxHGCMCallback(s) will check the request memory and, if completion
* flag is set, returns.
*/
if (VBOX_FAILURE(rc))
{
}
else
{
}
} break;
{
dprintf(("VBoxGuest::VBoxGuestDeviceControl: IOCTL_VBOXGUEST_HGCM_CALL\n"));
sizeof (VBoxGuestHGCMCallInfo));
if (Status != STATUS_SUCCESS)
{
break;
}
if (VBOX_FAILURE(rc))
{
}
else
{
}
} break;
#endif /* VBOX_HGCM */
default:
break;
}
return Status;
}
/**
* IRP_MJ_SHUTDOWN handler
*
* @returns NT status code
* @param pDevObj Device object.
* @param pIrp IRP.
*/
{
dprintf(("VBoxGuest::VBoxGuestShutdown\n"));
int rc = VbglGRAlloc ((VMMDevRequestHeader **)&req, sizeof (VMMDevPowerStateRequest), VMMDevReq_SetPowerStatus);
if (VBOX_SUCCESS(rc))
{
{
dprintf(("VBoxGuest::PowerStateRequest: error performing request to VMMDev."
}
}
return STATUS_SUCCESS;
}
/**
* Stub function for functions we don't implemented.
*
* @returns STATUS_NOT_SUPPORTED
* @param pDevObj Device object.
* @param pIrp IRP.
*/
{
dprintf(("VBoxGuest::VBoxGuestNotSupportedStub\n"));
return STATUS_NOT_SUPPORTED;
}
/**
* DPC handler
*
* @param dpc DPC descriptor.
* @param pDevObj Device object.
* @param irp Interrupt request packet.
* @param context Context specific pointer.
*/
{
/* Unblock handlers waiting for arrived events.
*
* Events are very low things, there is one event flag (1 or more bit)
* for each event. Each event is processed by exactly one handler.
*
* Assume that we trust additions and that other drivers will
* handle its respective events without trying to fetch all events.
*
* Anyway design assures that wrong event processing will affect only guest.
*
* Event handler calls VMMDev IOCTL for waiting an event.
* It supplies event mask. IOCTL blocks on EventNotification.
* Here we just signal an the EventNotification to all waiting
* threads, the IOCTL handler analyzes events and either
* return to caller or blocks again.
*
* If we do not have too many events this is a simple and good
* approach. Other way is to have as many Event objects as the callers
* and wake up only callers waiting for the specific event.
*
* Now with the 'wake up all' appoach we probably do not need the DPC
* handler and can signal event directly from ISR.
*
*/
dprintf(("VBoxGuest::VBoxGuestDpcHandler\n"));
}
/**
* ISR handler
*
* @return BOOLEAN indicates whether the IRQ came from us (TRUE) or not (FALSE)
* @param interrupt Interrupt that was triggered.
* @param serviceContext Context specific pointer.
*/
{
dprintf(("VBoxGuest::VBoxGuestIsrHandler haveEvents = %d\n",
/*
* now we have to find out whether it was our IRQ. Read the event mask
* from our device to see if there are any pending events
*/
{
/* Acknowlegde events. */
{
dprintf(("VBoxGuest::VBoxGuestIsrHandler: acknowledge events succeeded %#x\n",
}
else
{
/* This can't be actually. This is sign of a serious problem. */
dprintf(("VBoxGuest::VBoxGuestIsrHandler: "
"acknowledge events failed rc = %d, header rc = %d\n",
}
/* Mark IRQ as taken, there were events for us. */
}
return fIRQTaken;
}
/**
* Worker thread to do periodic things such as synchronize the
* system time and notify other drivers of events.
*
* @param pDevExt device extension pointer
*/
{
dprintf(("VBoxGuest::vboxWorkerThread entered\n"));
int rc = VbglGRAlloc ((VMMDevRequestHeader **)&req, sizeof (VMMDevReqHostTime), VMMDevReq_GetHostTime);
if (VBOX_FAILURE(rc))
{
dprintf(("VBoxGuest::vboxWorkerThread: could not allocate request buffer, exiting rc = %d!\n", rc));
return;
}
/* perform the hypervisor address space reservation */
do
{
/*
* Do the time sync
*/
{
#define TICKSPERSEC 10000000
#define TICKSPERMSEC 10000
#define SECSPERDAY 86400
{
// Windows was originally designed in 1601...
dprintf(("VBoxGuest::vboxWorkerThread: synching time with host time (msec/UTC): %llu\n", hostTime));
}
else
{
dprintf(("VBoxGuest::PowerStateRequest: error performing request to VMMDev."
}
}
/*
* Go asleep unless we're supposed to terminate
*/
if (!pDevExt->stopThread)
{
{
}
}
} while (!pDevExt->stopThread);
dprintf(("VBoxGuest::vboxWorkerThread: we've been asked to terminate!\n"));
/* free our request buffer */
if (pDevExt->workerThread)
{
}
dprintf(("VBoxGuest::vboxWorkerThread: now really gone!\n"));
}
/**
* Create driver worker threads
*
* @returns NTSTATUS NT status code
* @param pDevExt VBoxGuest device extension
*/
{
dprintf(("VBoxGuest::createThreads\n"));
// first setup the request semaphore
// the API has slightly changed after NT4
#ifdef TARGET_NT4
#ifdef OBJ_KERNEL_HANDLE
#endif
#define OBJ_KERNEL_HANDLE 0
#endif
/*
* The worker thread
*/
NULL,
NULL,
NULL);
(HANDLE)0L,
NULL,
pDevExt);
NULL,
NULL);
/*
* The idle thread
*/
#if 0 /// @todo Windows "sees" that time is lost and reports 100% usage
(HANDLE)0L,
NULL,
pDevExt);
NULL,
NULL);
#endif
return rc;
}
/**
* Helper routine to reserve address space for the hypervisor
* and communicate its position.
*
* @param pDevExt Device extension structure.
*/
{
// @todo rc handling
int rc = VbglGRAlloc ((VMMDevRequestHeader **)&req, sizeof (VMMDevReqHypervisorInfo), VMMDevReq_GetHypervisorInfo);
if (VBOX_SUCCESS(rc))
{
req->hypervisorStart = 0;
req->hypervisorSize = 0;
{
if (!hypervisorSize)
{
dprintf(("VBoxGuest::reserveHypervisorMemory: host returned 0, not doing anything\n"));
return;
}
dprintf(("VBoxGuest::reserveHypervisorMemory: host wants %u bytes of hypervisor address space\n", hypervisorSize));
// Map fictive physical memory into the kernel address space to reserve virtual
// address space. This API does not perform any checks but just allocate the
// The hypervisor only likes 4MB aligned virtual addresses, so we have to allocate
// 4MB more than we are actually supposed to in order to guarantee that. Maybe we
// can come up with a less lavish algorithm lateron.
if (!pDevExt->hypervisorMapping)
{
dprintf(("VBoxGuest::reserveHypervisorMemory: MmMapIoSpace returned NULL!\n"));
return;
}
dprintf(("VBoxGuest::reserveHypervisorMemory: MmMapIoSpace returned %p\n", pDevExt->hypervisorMapping));
dprintf(("VBoxGuest::reserveHypervisorMemory: communicating %p to host\n",
/* align at 4MB */
/* issue request */
{
dprintf(("VBoxGuest::reserveHypervisorMemory: error communicating physical address to VMMDev!"
}
}
else
{
dprintf(("VBoxGuest::reserveHypervisorMemory: request failed with rc %d, VMMDev rc = %Vrc\n", rc, req->header.rc));
}
}
return;
}
/**
* Helper function to unregister a virtual address space mapping
*
* @param pDevExt Device extension
*/
{
int rc = VbglGRAlloc ((VMMDevRequestHeader **)&req, sizeof (VMMDevReqHypervisorInfo), VMMDevReq_SetHypervisorInfo);
if (VBOX_SUCCESS(rc))
{
/* tell the hypervisor that the mapping is no longer available */
req->hypervisorStart = 0;
req->hypervisorSize = 0;
{
dprintf(("VBoxGuest::unreserveHypervisorMemory: error communicating physical address to VMMDev!"
}
}
if (!pDevExt->hypervisorMapping)
{
dprintf(("VBoxGuest::unreserveHypervisorMemory: there is no mapping, returning\n"));
return;
}
// unmap fictive IO space
dprintf(("VBoxGuest::unreserveHypervisorMemmory: done\n"));
}
/**
* Idle thread that runs at the lowest priority possible
* and whenever scheduled, makes a VMMDev call to give up
* timeslices. This is so prevent Windows from thinking that
* nothing is happening on the machine and doing stupid things
* that would steal time from other VMs it doesn't know of.
*
* @param pDevExt device extension pointer
*/
{
dprintf(("VBoxGuest::vboxIdleThread entered\n"));
/* set priority as low as possible */
/* allocate VMMDev request structure */
int rc = VbglGRAlloc((VMMDevRequestHeader **)&req, sizeof (VMMDevReqHypervisorInfo), VMMDevReq_Idle);
if (VBOX_FAILURE(rc))
{
dprintf(("VBoxGuest::vboxIdleThread: error %Vrc allocating request structure!\n"));
return;
}
do
{
//dprintf(("VBoxGuest: performing idle request..\n"));
/* perform idle request */
} while (!pDevExt->stopThread);
dprintf(("VBoxGuest::vboxIdleThread leaving\n"));
}