SUPDrv-win32.cpp revision 637f7ac058e8c4d9c93165c533a9441e397051b1
/** @file
*
* VBox host drivers - Ring-0 support drivers - Win32 host:
* Win32 host driver code
*/
/*
* 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "SUPDRV.h"
#include <excpt.h>
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** The support service name. */
#define SERVICE_NAME "VBoxDrv"
/** Win32 Device name. */
#define DEVICE_NAME "\\\\.\\VBoxDrv"
/** NT Device name. */
#define DEVICE_NAME_NT L"\\Device\\VBoxDrv"
/** Win32 Symlink name. */
#define DEVICE_NAME_DOS L"\\DosDevices\\VBoxDrv"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
#if 0 //def __AMD64__
typedef struct SUPDRVEXECMEM
{
void *pvMapping;
void *pvAllocation;
#endif
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static void _stdcall VBoxSupDrvGipTimer(IN PKDPC pDpc, IN PVOID pvUser, IN PVOID SystemArgument1, IN PVOID SystemArgument2);
static void _stdcall VBoxSupDrvGipPerCpuDpc(IN PKDPC pDpc, IN PVOID pvUser, IN PVOID SystemArgument1, IN PVOID SystemArgument2);
/*******************************************************************************
* Exported Functions *
*******************************************************************************/
/**
* Driver entry point.
*
* @returns appropriate status code.
* @param pDrvObj Pointer to driver object.
* @param pRegPath Registry base path.
*/
{
dprintf(("VBoxDrv::DriverEntry\n"));
/*
* Create device.
* (That means creating a device object and a symbolic link so the DOS
* subsystems (OS/2, win32, ++) can access the device.)
*/
rc = IoCreateDevice(pDrvObj, sizeof(SUPDRVDEVEXT), &DevName, FILE_DEVICE_UNKNOWN, 0, FALSE, &pDevObj);
if (NT_SUCCESS(rc))
{
if (NT_SUCCESS(rc))
{
/*
* Initialize the device extension.
*/
if (!vrc)
{
/*
* Inititalize the GIP.
*/
if (NT_SUCCESS(rc))
{
/*
* Setup the driver entry points in pDrvObj.
*/
/* more? */
dprintf(("VBoxDrv::DriverEntry returning STATUS_SUCCESS\n"));
return STATUS_SUCCESS;
}
else
}
else
{
}
}
else
}
else
if (NT_SUCCESS(rc))
return rc;
}
/**
* Unload the driver.
*
* @param pDrvObj Driver object.
*/
{
dprintf(("VBoxSupDrvUnload\n"));
/*
* We ASSUME that it's not possible to unload a driver with open handles.
* Start by deleting the symbolic link
*/
/*
* Terminate the GIP page and delete the device extension.
*/
}
/**
* Create (i.e. Open) file entry point.
*
* @param pDevObj Device object.
* @param pIrp Request packet.
*/
{
dprintf(("VBoxSupDrvCreate\n"));
/*
* We are not remotely similar to a directory...
* (But this is possible.)
*/
{
return STATUS_NOT_A_DIRECTORY;
}
/*
* Call common code for the rest.
*/
if (!rc)
{
}
return rcNt;
}
/**
* Close file entry point.
*
* @param pDevObj Device object.
* @param pIrp Request packet.
*/
{
dprintf(("VBoxSupDrvClose: pDevExt=%p pFileObj=%p pSession=%p\n",
return STATUS_SUCCESS;
}
/**
* Device I/O Control entry point.
*
* @param pDevObj Device object.
* @param pIrp Request packet.
*/
{
unsigned cbOut = 0;
dprintf2(("VBoxSupDrvDeviceControl(%p,%p): ioctl=%#x pBuf=%p cbIn=%#x cbOut=%#x pSession=%p\n",
#ifdef __WIN64__
/*
* Don't allow 32-bit processes to do any I/O controls.
*/
if (IoIs32bitProcess(pIrp))
{
dprintf(("VBoxSupDrvDeviceControl: returns STATUS_NOT_SUPPORTED - WOW64 req\n"));
return STATUS_NOT_SUPPORTED;
}
#endif
&cbOut);
/* sanity check. */
("cbOut is too large! cbOut=%d max=%d! ioctl=%#x\n",
/* complete the request. */
return rcNt;
}
/**
* Stub function for functions we don't implemented.
*
* @returns STATUS_NOT_SUPPORTED
* @param pDevObj Device object.
* @param pIrp IRP.
*/
{
dprintf(("VBoxSupDrvNotSupportedStub\n"));
return STATUS_NOT_SUPPORTED;
}
/**
* Initializes any OS specific object creator fields.
*/
{
}
/**
* Checks if the session can access the object.
*
* @returns true if a decision has been made.
* @returns false if the default access policy should be applied.
*
* @param pObj The object in question.
* @param pSession The session wanting to access the object.
* @param pszObjName The object name, can be NULL.
* @param prc Where to store the result when returning true.
*/
bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
{
return false;
}
/**
* OS Specific code for locking down memory.
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pMem Pointer to memory.
* This is not linked in anywhere.
* @param paPages Array which should be filled with the address of the physical pages.
*/
{
/* paranoia */
{
AssertMsgFailed(("Fool! No memory to lock!\n"));
return SUPDRV_ERR_INVALID_PARAM;
}
/*
* Calc the number of MDLs we need to allocate.
*/
cMdls++;
/*
* Allocate memory for the MDL pointer array.
*/
pMem->u.locked.papMdl = (PMDL *)ExAllocatePoolWithTag(NonPagedPool, sizeof(*pMem->u.locked.papMdl) * cMdls, 'vbox');
{
AssertMsgFailed(("shit, couldn't allocated %d bytes for the mdl pointer array!\n", sizeof(*pMem->u.locked.papMdl) * cMdls));
return SUPDRV_ERR_NO_MEMORY;
}
/*
* Loop locking down the sub parts of the memory.
*/
unsigned cbTotal = 0;
for (unsigned i = 0; i < cMdls; i++)
{
/*
* Calc the number of bytes to lock this time.
*/
if (cbCur > MAX_LOCK_MEM_SIZE)
if (cbCur == 0)
AssertMsgFailed(("cbCur: 0!\n"));
/*
* Allocate pMdl.
*/
if (!pMdl)
{
return SUPDRV_ERR_NO_MEMORY;
}
/*
* Lock the pages.
*/
{
}
{
rc = GetExceptionCode();
}
if (!NT_SUCCESS(rc))
{
/*
* Cleanup and fail.
*/
while (i-- > 0)
{
}
return SUPDRV_ERR_LOCK_FAILED;
}
/*
* Add MDL to array and update the pages.
*/
const uintptr_t *pauPFNs = (uintptr_t *)(pMdl + 1); /* ASSUMES ULONG_PTR == uintptr_t, NTDDK4 doesn't have ULONG_PTR. */
{
pPage++;
}
/* next */
}
/*
* Finish structure and return succesfully.
*/
dprintf2(("supdrvOSLockMemOne: pvR3=%p cb=%d cMdls=%d\n",
return 0;
}
/**
* Unlocks the memory pointed to by pv.
*
* @param pv Memory to unlock.
* @param cb Size of the memory (debug).
*/
{
dprintf2(("supdrvOSUnlockMemOne: pvR3=%p cb=%d cMdl=%p papMdl=%p\n",
{
}
}
/**
* OS Specific code for allocating page aligned memory with continuous fixed
* physical paged backing.
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pMem Memory reference record of the memory to be allocated.
* (This is not linked in anywhere.)
* @param ppvR0 Where to store the virtual address of the ring-0 mapping. (optional)
* @param ppvR3 Where to store the virtual address of the ring-3 mapping.
* @param pHCPhys Where to store the physical address.
*/
int VBOXCALL supdrvOSContAllocOne(PSUPDRVMEMREF pMem, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS pHCPhys)
{
/*
* Try allocate the memory.
*/
return SUPDRV_ERR_NO_MEMORY;
/*
* Map into user space.
*/
int rc = SUPDRV_ERR_NO_MEMORY;
{
{
{
/*
* Done, setup pMem and return values.
*/
#ifdef __AMD64__
#endif
if (ppvR0)
const uintptr_t *pauPFNs = (const uintptr_t *)(pMem->u.cont.pMdl + 1); /* ASSUMES ULONG_PTR == uintptr_t, NTDDK4 doesn't have ULONG_PTR. */
dprintf2(("supdrvOSContAllocOne: pvR0=%p pvR3=%p cb=%d pMdl=%p *pHCPhys=%VHp\n",
return 0;
}
}
{
}
}
return rc;
}
/**
* Frees contiguous memory.
*
* @param pMem Memory reference record of the memory to be freed.
*/
{
{
dprintf2(("supdrvOSContFreeOne: pvR0=%p pvR3=%p cb=%d pMdl=%p\n",
{
dprintf2(("MmUnmapLockedPages ok!\n"));
}
dprintf2(("IoFreeMdl ok!\n"));
dprintf2(("MmFreeContiguousMemory ok!\n"));
}
{
}
}
/**
* Allocates memory which mapped into both kernel and user space.
* The returned memory is page aligned and so is the allocation.
*
* @returns 0 on success.
* @returns SUPDRV_ERR_* on failure.
* @param pMem Memory reference record of the memory to be allocated.
* (This is not linked in anywhere.)
* @param ppvR0 Where to store the address of the Ring-0 mapping.
* @param ppvR3 Where to store the address of the Ring-3 mapping.
*/
{
/*
* Try allocate the memory.
*/
return SUPDRV_ERR_NO_MEMORY;
/*
* Map into user space.
*/
int rc = SUPDRV_ERR_NO_MEMORY;
{
{
{
/*
* Done, setup pMem and return values.
*/
dprintf2(("supdrvOSContAllocOne: pvR0=%p pvR3=%p cb=%d pMdl=%p\n",
return 0;
}
}
{
}
}
return rc;
}
/**
* Get the physical addresses of the pages in the allocation.
* This is called while inside bundle the spinlock.
*
* @param pMem Memory reference record of the memory.
* @param paPages Where to store the page addresses.
*/
{
const uintptr_t *pauPFNs = (const uintptr_t *)(pMem->u.mem.pMdl + 1); /* ASSUMES ULONG_PTR == uintptr_t, NTDDK doesn't have ULONG_PTR. */
{
}
}
/**
* Frees memory allocated by supdrvOSMemAllocOne().
*
* @param pMem Memory reference record of the memory to be free.
*/
{
{
dprintf2(("supdrvOSContFreeOne: pvR0=%p pvR3=%p cb=%d pMdl=%p\n",
{
dprintf2(("MmUnmapLockedPages ok!\n"));
}
dprintf2(("IoFreeMdl ok!\n"));
dprintf2(("MmFreeContiguousMemory ok!\n"));
}
{
}
}
/**
* Gets the monotone timestamp (nano seconds).
* @returns NanoTS.
*/
static inline uint64_t supdrvOSMonotime(void)
{
}
/**
* Initializes the GIP.
*
* @returns NT status code.
* @param pDevExt Instance data. GIP stuff may be updated.
*/
{
dprintf2(("VBoxSupDrvTermGip:\n"));
/*
* Try allocate the memory.
* Make sure it's below 4GB for 32-bit GC support
*/
if (pGip)
{
{
{
/*
* Figure the timer interval and frequency.
* It turns out trying 1023Hz doesn't work. So, we'll set the max Hz at 128 for now.
*/
/*
* Call common initialization routine.
*/
/*
* Initialize the timer.
*/
/*
* Initialize the DPCs we're using to update the per-cpu GIP data.
* (Not sure if we need to be this careful with KeSetTargetProcessorDpc...)
*/
VOID (*pfnKeSetTargetProcessorDpc)(IN PRKDPC, IN CCHAR) = (VOID (*)(IN PRKDPC, IN CCHAR))MmGetSystemRoutineAddress(&RoutineName);
{
}
dprintf(("VBoxSupDrvGipInit: ulClockFreq=%ld ulClockInterval=%ld ulClockIntervalActual=%ld Phys=%x%08x\n",
return STATUS_SUCCESS;
}
}
else
{
}
}
else
{
dprintf(("VBoxSupDrvInitGip: no cont memory.\n"));
}
return rc;
}
/**
* Terminates the GIP.
*
* @returns negative errno.
* @param pDevExt Instance data. GIP stuff may be updated.
*/
{
dprintf(("VBoxSupDrvTermGip:\n"));
/*
* Cancel the timer and wait on DPCs if it was still pending.
*/
{
if (pfnKeFlushQueuedDpcs)
}
/*
* Uninitialize the content.
*/
if (pGip)
{
/*
* Free the page.
*/
{
}
}
}
/**
* Timer callback function.
* The pvUser parameter is the pDevExt pointer.
*/
static void _stdcall VBoxSupDrvGipTimer(IN PKDPC pDpc, IN PVOID pvUser, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
{
if (pGip)
{
else
{
/*
* We cannot do other than assume a 1:1 relation ship between the
* affinity mask and the process despite the warnings in the docs.
* If someone knows a better way to get this done, please let bird know.
*/
unsigned iSelf = KeGetCurrentProcessorNumber();
{
if ( i != iSelf
}
/* Run the normal update. */
}
}
}
/**
* Per cpu callback callback function.
* The pvUser parameter is the pGip pointer.
*/
static void _stdcall VBoxSupDrvGipPerCpuDpc(IN PKDPC pDpc, IN PVOID pvUser, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
{
}
/**
* Maps the GIP into user space.
*
* @returns negative errno.
* @param pDevExt Instance data.
*/
{
/*
* Map into user space.
*/
int rc = 0;
{
}
{
}
return 0;
}
/**
* Maps the GIP into user space.
*
* @returns negative errno.
* @param pDevExt Instance data.
*/
{
int rc = 0;
{
}
{
}
return rc;
}
/**
* Resumes the GIP updating.
*
* @param pDevExt Instance data.
*/
{
dprintf2(("supdrvOSGipResume:\n"));
}
/**
* Suspends the GIP updating.
*
* @param pDevExt Instance data.
*/
{
dprintf2(("supdrvOSGipSuspend:\n"));
#ifdef __AMD64__
#endif
}
/**
* Allocate small amounts of memory which is does not have the NX bit set.
*
* @returns Pointer to the allocated memory
* @returns NULL if out of memory.
* @param cb Size of the memory block.
*/
{
#if 0 //def __AMD64__
if (pv)
{
/*
* Create a kernel mapping which we make PAGE_EXECUTE_READWRITE using
* the MmProtectMdlSystemAddress API.
*/
int rc = SUPDRV_ERR_NO_MEMORY;
if (pMdl)
{
{
if (pvMapping)
{
if (NT_SUCCESS(rc))
{
/*
* Create tracking structure and insert it into the list.
*/
return pvMapping;
}
}
}
{
}
}
ExFreePool(pv);
}
return NULL;
#else
return pv;
#endif
}
/**
* Get the current CPU count.
* @returns Number of cpus.
*/
unsigned VBOXCALL supdrvOSGetCPUCount(void)
{
unsigned cCpus = 0;
unsigned iBit;
cCpus++;
if (cCpus == 0) /* paranoia */
cCpus = 1;
return cCpus;
}
/**
* Force async tsc mode (stub).
*/
bool VBOXCALL supdrvOSGetForcedAsyncTscMode(void)
{
return false;
}
/**
* Converts a supdrv error code to an nt status code.
*
* @returns corresponding nt status code.
* @param rc supdrv error code (SUPDRV_ERR_* defines).
*/
{
switch (rc)
{
case 0: return STATUS_SUCCESS;
case SUPDRV_ERR_GENERAL_FAILURE: return STATUS_NOT_SUPPORTED;
case SUPDRV_ERR_INVALID_PARAM: return STATUS_INVALID_PARAMETER;
case SUPDRV_ERR_INVALID_MAGIC: return STATUS_UNKNOWN_REVISION;
case SUPDRV_ERR_INVALID_HANDLE: return STATUS_INVALID_HANDLE;
case SUPDRV_ERR_INVALID_POINTER: return STATUS_INVALID_ADDRESS;
case SUPDRV_ERR_LOCK_FAILED: return STATUS_NOT_LOCKED;
case SUPDRV_ERR_ALREADY_LOADED: return STATUS_IMAGE_ALREADY_LOADED;
case SUPDRV_ERR_PERMISSION_DENIED: return STATUS_ACCESS_DENIED;
case SUPDRV_ERR_VERSION_MISMATCH: return STATUS_REVISION_MISMATCH;
}
return STATUS_UNSUCCESSFUL;
}
/** Runtime assert implementation for Native Win32 Ring-0. */
RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
{
DbgPrint("\n!!Assertion Failed!!\n"
"Expression: %s\n"
"Location : %s(%d) %s\n",
}
{
return 0;
}