[10258] | 1 | /* $Id: SUPR0IdcClient-win.c 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * VirtualBox Support Driver - IDC Client Lib, Windows Specific Code.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2008-2023 Oracle and/or its affiliates.
|
---|
[10258] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[10258] | 11 | *
|
---|
[96407] | 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
[10258] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[10258] | 29 | * CDDL are applicable instead of those of the GPL.
|
---|
| 30 | *
|
---|
| 31 | * You may elect to license modified versions of this file under the
|
---|
| 32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[10258] | 35 | */
|
---|
| 36 |
|
---|
[57358] | 37 |
|
---|
| 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[10258] | 41 | #include "../SUPR0IdcClientInternal.h"
|
---|
[76474] | 42 | #include <iprt/errcore.h>
|
---|
[10258] | 43 |
|
---|
| 44 |
|
---|
[57358] | 45 | /*********************************************************************************************************************************
|
---|
| 46 | * Defined Constants And Macros *
|
---|
| 47 | *********************************************************************************************************************************/
|
---|
[10258] | 48 | /** NT Device name. */
|
---|
| 49 | #define DEVICE_NAME_NT L"\\Device\\VBoxDrv"
|
---|
| 50 |
|
---|
| 51 |
|
---|
[10260] | 52 | /**
|
---|
| 53 | * Internal I/O Control call worker.
|
---|
| 54 | *
|
---|
| 55 | * @returns VBox status code.
|
---|
| 56 | * @param pDeviceObject The device object to call.
|
---|
[10261] | 57 | * @param pFileObject The file object for the connection.
|
---|
[10263] | 58 | * @param uReq The request.
|
---|
[10260] | 59 | * @param pReq The request packet.
|
---|
| 60 | */
|
---|
[10263] | 61 | static int supR0IdcNtCallInternal(PDEVICE_OBJECT pDeviceObject, PFILE_OBJECT pFileObject, uint32_t uReq, PSUPDRVIDCREQHDR pReq)
|
---|
[10258] | 62 | {
|
---|
| 63 | int rc;
|
---|
| 64 | IO_STATUS_BLOCK IoStatusBlock;
|
---|
| 65 | KEVENT Event;
|
---|
| 66 | PIRP pIrp;
|
---|
[10261] | 67 | NTSTATUS rcNt;
|
---|
[10258] | 68 |
|
---|
| 69 | /*
|
---|
| 70 | * Build the request.
|
---|
| 71 | */
|
---|
| 72 | KeInitializeEvent(&Event, NotificationEvent, FALSE);
|
---|
[10263] | 73 | pIrp = IoBuildDeviceIoControlRequest(uReq, /* IoControlCode */
|
---|
[10258] | 74 | pDeviceObject,
|
---|
| 75 | pReq, /* InputBuffer */
|
---|
| 76 | pReq->cb, /* InputBufferLength */
|
---|
| 77 | pReq, /* OutputBuffer */
|
---|
| 78 | pReq->cb, /* OutputBufferLength */
|
---|
| 79 | TRUE, /* InternalDeviceIoControl (=> IRP_MJ_INTERNAL_DEVICE_CONTROL) */
|
---|
| 80 | &Event, /* Event */
|
---|
| 81 | &IoStatusBlock); /* IoStatusBlock */
|
---|
| 82 | if (pIrp)
|
---|
| 83 | {
|
---|
[10261] | 84 | IoGetNextIrpStackLocation(pIrp)->FileObject = pFileObject;
|
---|
| 85 |
|
---|
[10258] | 86 | /*
|
---|
| 87 | * Call the driver, wait for an async request to complete (should never happen).
|
---|
| 88 | */
|
---|
[10261] | 89 | rcNt = IoCallDriver(pDeviceObject, pIrp);
|
---|
[10258] | 90 | if (rcNt == STATUS_PENDING)
|
---|
| 91 | {
|
---|
| 92 | rcNt = KeWaitForSingleObject(&Event, /* Object */
|
---|
| 93 | Executive, /* WaitReason */
|
---|
| 94 | KernelMode, /* WaitMode */
|
---|
[33540] | 95 | FALSE, /* Alertable */
|
---|
[10258] | 96 | NULL); /* TimeOut */
|
---|
| 97 | rcNt = IoStatusBlock.Status;
|
---|
| 98 | }
|
---|
| 99 | if (NT_SUCCESS(rcNt))
|
---|
| 100 | rc = pReq->rc;
|
---|
| 101 | else
|
---|
| 102 | rc = RTErrConvertFromNtStatus(rcNt);
|
---|
| 103 | }
|
---|
| 104 | else
|
---|
| 105 | rc = VERR_NO_MEMORY;
|
---|
| 106 | return rc;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[10263] | 109 |
|
---|
[10258] | 110 | int VBOXCALL supR0IdcNativeOpen(PSUPDRVIDCHANDLE pHandle, PSUPDRVIDCREQCONNECT pReq)
|
---|
| 111 | {
|
---|
| 112 | PDEVICE_OBJECT pDeviceObject = NULL;
|
---|
| 113 | PFILE_OBJECT pFileObject = NULL;
|
---|
| 114 | UNICODE_STRING wszDeviceName;
|
---|
| 115 | NTSTATUS rcNt;
|
---|
| 116 | int rc;
|
---|
| 117 |
|
---|
| 118 | /*
|
---|
| 119 | * Get the device object pointer.
|
---|
| 120 | */
|
---|
| 121 | RtlInitUnicodeString(&wszDeviceName, DEVICE_NAME_NT);
|
---|
| 122 | rcNt = IoGetDeviceObjectPointer(&wszDeviceName, FILE_ALL_ACCESS, &pFileObject, &pDeviceObject);
|
---|
| 123 | if (NT_SUCCESS(rcNt))
|
---|
| 124 | {
|
---|
| 125 | /*
|
---|
| 126 | * Make the connection call.
|
---|
| 127 | */
|
---|
[10261] | 128 | rc = supR0IdcNtCallInternal(pDeviceObject, pFileObject, SUPDRV_IDC_REQ_CONNECT, &pReq->Hdr);
|
---|
[10258] | 129 | if (RT_SUCCESS(rc))
|
---|
| 130 | {
|
---|
| 131 | pHandle->s.pDeviceObject = pDeviceObject;
|
---|
[38321] | 132 | pHandle->s.pFileObject = pFileObject;
|
---|
[10258] | 133 | return rc;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | /* only the file object. */
|
---|
| 137 | ObDereferenceObject(pFileObject);
|
---|
| 138 | }
|
---|
| 139 | else
|
---|
| 140 | rc = RTErrConvertFromNtStatus(rcNt);
|
---|
| 141 |
|
---|
| 142 | pHandle->s.pDeviceObject = NULL;
|
---|
| 143 | pHandle->s.pFileObject = NULL;
|
---|
| 144 | return rc;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
| 148 | int VBOXCALL supR0IdcNativeClose(PSUPDRVIDCHANDLE pHandle, PSUPDRVIDCREQHDR pReq)
|
---|
| 149 | {
|
---|
[10261] | 150 | PFILE_OBJECT pFileObject = pHandle->s.pFileObject;
|
---|
| 151 | int rc = supR0IdcNtCallInternal(pHandle->s.pDeviceObject, pFileObject, SUPDRV_IDC_REQ_DISCONNECT, pReq);
|
---|
[10258] | 152 | if (RT_SUCCESS(rc))
|
---|
| 153 | {
|
---|
| 154 | pHandle->s.pDeviceObject = NULL;
|
---|
| 155 | pHandle->s.pFileObject = NULL;
|
---|
| 156 | ObDereferenceObject(pFileObject);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | return rc;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 |
|
---|
[10263] | 163 | int VBOXCALL supR0IdcNativeCall(PSUPDRVIDCHANDLE pHandle, uint32_t uReq, PSUPDRVIDCREQHDR pReq)
|
---|
[10258] | 164 | {
|
---|
[10263] | 165 | return supR0IdcNtCallInternal(pHandle->s.pDeviceObject, pHandle->s.pFileObject, uReq, pReq);
|
---|
[10258] | 166 | }
|
---|
| 167 |
|
---|