SUPDrv-win.cpp revision 8f50537150bbf274d26335ff88bbe36d0e8358f6
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * VBoxDrv - The VirtualBox Support Driver - Windows NT specifics.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * available from http://www.virtualbox.org. This file is free software;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * you can redistribute it and/or modify it under the terms of the GNU
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * General Public License (GPL) as published by the Free Software
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * The contents of this file may alternatively be used under the terms
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * of the Common Development and Distribution License Version 1.0
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * VirtualBox OSE distribution, in which case the provisions of the
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * CDDL are applicable instead of those of the GPL.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * You may elect to license modified versions of this file under the
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * terms and conditions of either the GPL or the CDDL or both.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * additional information or have any questions.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/*******************************************************************************
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync* Header Files *
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync*******************************************************************************/
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/*******************************************************************************
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync* Defined Constants And Macros *
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync*******************************************************************************/
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync/** The support service name. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/** Win32 Device name. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/** NT Device name. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/** Win Symlink name. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/** The Pool tag (VBox). */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/*******************************************************************************
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync* Structures and Typedefs *
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync*******************************************************************************/
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync#if 0 //def RT_ARCH_AMD64
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/*******************************************************************************
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync* Internal Functions *
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync*******************************************************************************/
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncstatic void _stdcall VBoxDrvNtUnload(PDRIVER_OBJECT pDrvObj);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncstatic NTSTATUS _stdcall VBoxDrvNtCreate(PDEVICE_OBJECT pDevObj, PIRP pIrp);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncstatic NTSTATUS _stdcall VBoxDrvNtClose(PDEVICE_OBJECT pDevObj, PIRP pIrp);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncstatic NTSTATUS _stdcall VBoxDrvNtDeviceControl(PDEVICE_OBJECT pDevObj, PIRP pIrp);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncstatic int VBoxDrvNtDeviceControlSlow(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PIRP pIrp, PIO_STACK_LOCATION pStack);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncstatic NTSTATUS _stdcall VBoxDrvNtInternalDeviceControl(PDEVICE_OBJECT pDevObj, PIRP pIrp);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncstatic NTSTATUS _stdcall VBoxDrvNtNotSupportedStub(PDEVICE_OBJECT pDevObj, PIRP pIrp);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/*******************************************************************************
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync* Exported Functions *
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync*******************************************************************************/
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncULONG _stdcall DriverEntry(PDRIVER_OBJECT pDrvObj, PUNICODE_STRING pRegPath);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Driver entry point.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @returns appropriate status code.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param pDrvObj Pointer to driver object.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param pRegPath Registry base path.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncULONG _stdcall DriverEntry(PDRIVER_OBJECT pDrvObj, PUNICODE_STRING pRegPath)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Create device.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * (That means creating a device object and a symbolic link so the DOS
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsync * subsystems (OS/2, win32, ++) can access the device.)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync rc = IoCreateDevice(pDrvObj, sizeof(SUPDRVDEVEXT), &DevName, FILE_DEVICE_UNKNOWN, 0, FALSE, &pDevObj);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Initialize the device extension.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Setup the driver entry points in pDrvObj.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync pDrvObj->MajorFunction[IRP_MJ_CREATE] = VBoxDrvNtCreate;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync pDrvObj->MajorFunction[IRP_MJ_CLOSE] = VBoxDrvNtClose;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync pDrvObj->MajorFunction[IRP_MJ_DEVICE_CONTROL] = VBoxDrvNtDeviceControl;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync//#if 0 /** @todo test IDC on windows. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync pDrvObj->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = VBoxDrvNtInternalDeviceControl;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync pDrvObj->MajorFunction[IRP_MJ_READ] = VBoxDrvNtNotSupportedStub;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync pDrvObj->MajorFunction[IRP_MJ_WRITE] = VBoxDrvNtNotSupportedStub;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /* more? */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync dprintf(("VBoxDrv::DriverEntry returning STATUS_SUCCESS\n"));
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync dprintf(("supdrvInitDevExit failed with vrc=%d!\n", vrc));
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync dprintf(("IoCreateSymbolicLink failed with rc=%#x!\n", rc));
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync dprintf(("IoCreateDevice failed with rc=%#x!\n", rc));
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync dprintf(("VBoxDrv::DriverEntry returning %#x\n", rc));
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Unload the driver.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param pDrvObj Driver object.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncvoid _stdcall VBoxDrvNtUnload(PDRIVER_OBJECT pDrvObj)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDrvObj->DeviceObject->DeviceExtension;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync dprintf(("VBoxDrvNtUnload at irql %d\n", KeGetCurrentIrql()));
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * We ASSUME that it's not possible to unload a driver with open handles.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Start by deleting the symbolic link
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Terminate the GIP page and delete the device extension.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Create (i.e. Open) file entry point.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param pDevObj Device object.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param pIrp Request packet.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncNTSTATUS _stdcall VBoxDrvNtCreate(PDEVICE_OBJECT pDevObj, PIRP pIrp)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * We are not remotely similar to a directory...
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * (But this is possible.)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (pStack->Parameters.Create.Options & FILE_DIRECTORY_FILE)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Call common code for the rest.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync//#if 0 /** @todo check if this works, consider OBJ_KERNEL_HANDLE too. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync // bool fUser = true;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync int rc = supdrvCreateSession(pDevExt, fUser, &pSession);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync NTSTATUS rcNt = pIrp->IoStatus.Status = VBoxDrvNtErr2NtStatus(rc);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Close file entry point.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param pDevObj Device object.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param pIrp Request packet.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncNTSTATUS _stdcall VBoxDrvNtClose(PDEVICE_OBJECT pDevObj, PIRP pIrp)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync dprintf(("VBoxDrvNtClose: pDevExt=%p pFileObj=%p pSession=%p\n",
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync supdrvCloseSession(pDevExt, (PSUPDRVSESSION)pFileObj->FsContext);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Device I/O Control entry point.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param pDevObj Device object.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param pIrp Request packet.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncNTSTATUS _stdcall VBoxDrvNtDeviceControl(PDEVICE_OBJECT pDevObj, PIRP pIrp)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync PSUPDRVSESSION pSession = (PSUPDRVSESSION)pStack->FileObject->FsContext;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Deal with the two high-speed IOCtl that takes it's arguments from
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * the session and iCmd, and only returns a VBox status code.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Note: The previous method of returning the rc prior to IOC version
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * 7.4 has been abandond, we're no longer compatible with that
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * interface.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync ULONG ulCmd = pStack->Parameters.DeviceIoControl.IoControlCode;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /* Raise the IRQL to DISPATCH_LEVEl to prevent Windows from rescheduling us to another CPU/core. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync int rc = supdrvIOCtlFast(ulCmd, pDevExt, pSession);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /* Complete the I/O request. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync NTSTATUS rcNt = pIrp->IoStatus.Status = RT_SUCCESS(rc) ? STATUS_SUCCESS : STATUS_INVALID_PARAMETER;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync return VBoxDrvNtDeviceControlSlow(pDevExt, pSession, pIrp, pStack);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Worker for VBoxDrvNtDeviceControl that takes the slow IOCtl functions.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @returns NT status code.
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync * @param pDevObj Device object.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param pSession The session.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param pIrp Request packet.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param pStack The stack location containing the DeviceControl parameters.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncstatic int VBoxDrvNtDeviceControlSlow(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PIRP pIrp, PIO_STACK_LOCATION pStack)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync unsigned cbOut = 0;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync dprintf2(("VBoxDrvNtDeviceControlSlow(%p,%p): ioctl=%#x pBuf=%p cbIn=%#x cbOut=%#x pSession=%p\n",
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync pDevExt, pIrp, pStack->Parameters.DeviceIoControl.IoControlCode,
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync pIrp->AssociatedIrp.SystemBuffer, pStack->Parameters.DeviceIoControl.InputBufferLength,
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync pStack->Parameters.DeviceIoControl.OutputBufferLength, pSession));
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync /* Don't allow 32-bit processes to do any I/O controls. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /* Verify that it's a buffered CTL. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if ((pStack->Parameters.DeviceIoControl.IoControlCode & 0x3) == METHOD_BUFFERED)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /* Verify that the sizes in the request header are correct. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync PSUPREQHDR pHdr = (PSUPREQHDR)pIrp->AssociatedIrp.SystemBuffer;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if ( pStack->Parameters.DeviceIoControl.InputBufferLength >= sizeof(*pHdr)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync && pStack->Parameters.DeviceIoControl.InputBufferLength == pHdr->cbIn
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync && pStack->Parameters.DeviceIoControl.OutputBufferLength == pHdr->cbOut)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Do the job.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync rc = supdrvIOCtl(pStack->Parameters.DeviceIoControl.IoControlCode, pDevExt, pSession, pHdr);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (cbOut > pStack->Parameters.DeviceIoControl.OutputBufferLength)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync cbOut = pStack->Parameters.DeviceIoControl.OutputBufferLength;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync OSDBGPRINT(("VBoxDrvLinuxIOCtl: too much output! %#x > %#x; uCmd=%#x!\n",
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync pHdr->cbOut, cbOut, pStack->Parameters.DeviceIoControl.IoControlCode));
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync dprintf2(("VBoxDrvNtDeviceControlSlow: returns %#x cbOut=%d rc=%#x\n", rcNt, cbOut, rc));
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync dprintf(("VBoxDrvNtDeviceControlSlow: Mismatching sizes (%#x) - Hdr=%#lx/%#lx Irp=%#lx/%#lx!\n",
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync pStack->Parameters.DeviceIoControl.InputBufferLength >= sizeof(*pHdr) ? pHdr->cbIn : 0,
#ifdef RT_ARCH_AMD64
return rcNt;
unsigned cbOut = 0;
int rc = 0;
dprintf2(("VBoxDrvNtInternalDeviceControl(%p,%p): ioctl=%#x pBuf=%p cbIn=%#x cbOut=%#x pSession=%p\n",
if ( ( !pSession
if (!rc)
return rcNt;
return STATUS_NOT_SUPPORTED;
bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
switch (rc)
case 0: return STATUS_SUCCESS;
return STATUS_UNSUCCESSFUL;