1 | /* $Id: SUPDrv-win.cpp 37249 2011-05-30 10:03:45Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxDrv - The VirtualBox Support Driver - Windows NT specifics.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /*******************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #define LOG_GROUP LOG_GROUP_SUP_DRV
|
---|
31 | #include "../SUPDrvInternal.h"
|
---|
32 | #include <excpt.h>
|
---|
33 | #include <ntimage.h>
|
---|
34 |
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/initterm.h>
|
---|
37 | #include <iprt/mem.h>
|
---|
38 | #include <iprt/process.h>
|
---|
39 | #include <iprt/power.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 | #include <VBox/log.h>
|
---|
42 |
|
---|
43 |
|
---|
44 | /*******************************************************************************
|
---|
45 | * Defined Constants And Macros *
|
---|
46 | *******************************************************************************/
|
---|
47 | /** The support service name. */
|
---|
48 | #define SERVICE_NAME "VBoxDrv"
|
---|
49 | /** Win32 Device name. */
|
---|
50 | #define DEVICE_NAME "\\\\.\\VBoxDrv"
|
---|
51 | /** NT Device name. */
|
---|
52 | #define DEVICE_NAME_NT L"\\Device\\VBoxDrv"
|
---|
53 | /** Win Symlink name. */
|
---|
54 | #define DEVICE_NAME_DOS L"\\DosDevices\\VBoxDrv"
|
---|
55 | /** The Pool tag (VBox). */
|
---|
56 | #define SUPDRV_NT_POOL_TAG 'xoBV'
|
---|
57 |
|
---|
58 |
|
---|
59 | /*******************************************************************************
|
---|
60 | * Structures and Typedefs *
|
---|
61 | *******************************************************************************/
|
---|
62 | #if 0 //def RT_ARCH_AMD64
|
---|
63 | typedef struct SUPDRVEXECMEM
|
---|
64 | {
|
---|
65 | PMDL pMdl;
|
---|
66 | void *pvMapping;
|
---|
67 | void *pvAllocation;
|
---|
68 | } SUPDRVEXECMEM, *PSUPDRVEXECMEM;
|
---|
69 | #endif
|
---|
70 |
|
---|
71 |
|
---|
72 | /*******************************************************************************
|
---|
73 | * Internal Functions *
|
---|
74 | *******************************************************************************/
|
---|
75 | static void _stdcall VBoxDrvNtUnload(PDRIVER_OBJECT pDrvObj);
|
---|
76 | static NTSTATUS _stdcall VBoxDrvNtCreate(PDEVICE_OBJECT pDevObj, PIRP pIrp);
|
---|
77 | static NTSTATUS _stdcall VBoxDrvNtClose(PDEVICE_OBJECT pDevObj, PIRP pIrp);
|
---|
78 | static NTSTATUS _stdcall VBoxDrvNtDeviceControl(PDEVICE_OBJECT pDevObj, PIRP pIrp);
|
---|
79 | static int VBoxDrvNtDeviceControlSlow(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PIRP pIrp, PIO_STACK_LOCATION pStack);
|
---|
80 | static NTSTATUS _stdcall VBoxDrvNtInternalDeviceControl(PDEVICE_OBJECT pDevObj, PIRP pIrp);
|
---|
81 | static VOID _stdcall VBoxPowerDispatchCallback(PVOID pCallbackContext, PVOID pArgument1, PVOID pArgument2);
|
---|
82 | static NTSTATUS _stdcall VBoxDrvNtNotSupportedStub(PDEVICE_OBJECT pDevObj, PIRP pIrp);
|
---|
83 | static NTSTATUS VBoxDrvNtErr2NtStatus(int rc);
|
---|
84 |
|
---|
85 |
|
---|
86 | /*******************************************************************************
|
---|
87 | * Exported Functions *
|
---|
88 | *******************************************************************************/
|
---|
89 | RT_C_DECLS_BEGIN
|
---|
90 | ULONG _stdcall DriverEntry(PDRIVER_OBJECT pDrvObj, PUNICODE_STRING pRegPath);
|
---|
91 | RT_C_DECLS_END
|
---|
92 |
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Driver entry point.
|
---|
96 | *
|
---|
97 | * @returns appropriate status code.
|
---|
98 | * @param pDrvObj Pointer to driver object.
|
---|
99 | * @param pRegPath Registry base path.
|
---|
100 | */
|
---|
101 | ULONG _stdcall DriverEntry(PDRIVER_OBJECT pDrvObj, PUNICODE_STRING pRegPath)
|
---|
102 | {
|
---|
103 | NTSTATUS rc;
|
---|
104 |
|
---|
105 | /*
|
---|
106 | * Create device.
|
---|
107 | * (That means creating a device object and a symbolic link so the DOS
|
---|
108 | * subsystems (OS/2, win32, ++) can access the device.)
|
---|
109 | */
|
---|
110 | UNICODE_STRING DevName;
|
---|
111 | RtlInitUnicodeString(&DevName, DEVICE_NAME_NT);
|
---|
112 | PDEVICE_OBJECT pDevObj;
|
---|
113 | rc = IoCreateDevice(pDrvObj, sizeof(SUPDRVDEVEXT), &DevName, FILE_DEVICE_UNKNOWN, 0, FALSE, &pDevObj);
|
---|
114 | if (NT_SUCCESS(rc))
|
---|
115 | {
|
---|
116 | UNICODE_STRING DosName;
|
---|
117 | RtlInitUnicodeString(&DosName, DEVICE_NAME_DOS);
|
---|
118 | rc = IoCreateSymbolicLink(&DosName, &DevName);
|
---|
119 | if (NT_SUCCESS(rc))
|
---|
120 | {
|
---|
121 | int vrc = RTR0Init(0);
|
---|
122 | if (RT_SUCCESS(vrc))
|
---|
123 | {
|
---|
124 | Log(("VBoxDrv::DriverEntry\n"));
|
---|
125 |
|
---|
126 | /*
|
---|
127 | * Initialize the device extension.
|
---|
128 | */
|
---|
129 | PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;
|
---|
130 | memset(pDevExt, 0, sizeof(*pDevExt));
|
---|
131 |
|
---|
132 | vrc = supdrvInitDevExt(pDevExt, sizeof(SUPDRVSESSION));
|
---|
133 | if (!vrc)
|
---|
134 | {
|
---|
135 | /*
|
---|
136 | * Setup the driver entry points in pDrvObj.
|
---|
137 | */
|
---|
138 | pDrvObj->DriverUnload = VBoxDrvNtUnload;
|
---|
139 | pDrvObj->MajorFunction[IRP_MJ_CREATE] = VBoxDrvNtCreate;
|
---|
140 | pDrvObj->MajorFunction[IRP_MJ_CLOSE] = VBoxDrvNtClose;
|
---|
141 | pDrvObj->MajorFunction[IRP_MJ_DEVICE_CONTROL] = VBoxDrvNtDeviceControl;
|
---|
142 | //#if 0 /** @todo test IDC on windows. */
|
---|
143 | pDrvObj->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = VBoxDrvNtInternalDeviceControl;
|
---|
144 | //#endif
|
---|
145 | pDrvObj->MajorFunction[IRP_MJ_READ] = VBoxDrvNtNotSupportedStub;
|
---|
146 | pDrvObj->MajorFunction[IRP_MJ_WRITE] = VBoxDrvNtNotSupportedStub;
|
---|
147 |
|
---|
148 | /* more? */
|
---|
149 |
|
---|
150 | /* Register ourselves for power state changes. */
|
---|
151 | UNICODE_STRING CallbackName;
|
---|
152 | OBJECT_ATTRIBUTES Attr;
|
---|
153 |
|
---|
154 | RtlInitUnicodeString(&CallbackName, L"\\Callback\\PowerState");
|
---|
155 | InitializeObjectAttributes(&Attr, &CallbackName, OBJ_CASE_INSENSITIVE, NULL, NULL);
|
---|
156 |
|
---|
157 | rc = ExCreateCallback(&pDevExt->pObjPowerCallback, &Attr, TRUE, TRUE);
|
---|
158 | if (rc == STATUS_SUCCESS)
|
---|
159 | pDevExt->hPowerCallback = ExRegisterCallback(pDevExt->pObjPowerCallback, VBoxPowerDispatchCallback, pDevObj);
|
---|
160 |
|
---|
161 | Log(("VBoxDrv::DriverEntry returning STATUS_SUCCESS\n"));
|
---|
162 | return STATUS_SUCCESS;
|
---|
163 | }
|
---|
164 |
|
---|
165 | Log(("supdrvInitDevExit failed with vrc=%d!\n", vrc));
|
---|
166 | rc = VBoxDrvNtErr2NtStatus(vrc);
|
---|
167 |
|
---|
168 | IoDeleteSymbolicLink(&DosName);
|
---|
169 | RTR0Term();
|
---|
170 | }
|
---|
171 | else
|
---|
172 | {
|
---|
173 | Log(("RTR0Init failed with vrc=%d!\n", vrc));
|
---|
174 | rc = VBoxDrvNtErr2NtStatus(vrc);
|
---|
175 | }
|
---|
176 | }
|
---|
177 | else
|
---|
178 | Log(("IoCreateSymbolicLink failed with rc=%#x!\n", rc));
|
---|
179 |
|
---|
180 | IoDeleteDevice(pDevObj);
|
---|
181 | }
|
---|
182 | else
|
---|
183 | Log(("IoCreateDevice failed with rc=%#x!\n", rc));
|
---|
184 |
|
---|
185 | if (NT_SUCCESS(rc))
|
---|
186 | rc = STATUS_INVALID_PARAMETER;
|
---|
187 | Log(("VBoxDrv::DriverEntry returning %#x\n", rc));
|
---|
188 | return rc;
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Unload the driver.
|
---|
194 | *
|
---|
195 | * @param pDrvObj Driver object.
|
---|
196 | */
|
---|
197 | void _stdcall VBoxDrvNtUnload(PDRIVER_OBJECT pDrvObj)
|
---|
198 | {
|
---|
199 | PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDrvObj->DeviceObject->DeviceExtension;
|
---|
200 |
|
---|
201 | Log(("VBoxDrvNtUnload at irql %d\n", KeGetCurrentIrql()));
|
---|
202 |
|
---|
203 | /* Clean up the power callback registration. */
|
---|
204 | if (pDevExt->hPowerCallback)
|
---|
205 | ExUnregisterCallback(pDevExt->hPowerCallback);
|
---|
206 | if (pDevExt->pObjPowerCallback)
|
---|
207 | ObDereferenceObject(pDevExt->pObjPowerCallback);
|
---|
208 |
|
---|
209 | /*
|
---|
210 | * We ASSUME that it's not possible to unload a driver with open handles.
|
---|
211 | * Start by deleting the symbolic link
|
---|
212 | */
|
---|
213 | UNICODE_STRING DosName;
|
---|
214 | RtlInitUnicodeString(&DosName, DEVICE_NAME_DOS);
|
---|
215 | NTSTATUS rc = IoDeleteSymbolicLink(&DosName);
|
---|
216 |
|
---|
217 | /*
|
---|
218 | * Terminate the GIP page and delete the device extension.
|
---|
219 | */
|
---|
220 | supdrvDeleteDevExt(pDevExt);
|
---|
221 | RTR0Term();
|
---|
222 | IoDeleteDevice(pDrvObj->DeviceObject);
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Create (i.e. Open) file entry point.
|
---|
228 | *
|
---|
229 | * @param pDevObj Device object.
|
---|
230 | * @param pIrp Request packet.
|
---|
231 | */
|
---|
232 | NTSTATUS _stdcall VBoxDrvNtCreate(PDEVICE_OBJECT pDevObj, PIRP pIrp)
|
---|
233 | {
|
---|
234 | Log(("VBoxDrvNtCreate\n"));
|
---|
235 | PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
|
---|
236 | PFILE_OBJECT pFileObj = pStack->FileObject;
|
---|
237 | PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;
|
---|
238 |
|
---|
239 | /*
|
---|
240 | * We are not remotely similar to a directory...
|
---|
241 | * (But this is possible.)
|
---|
242 | */
|
---|
243 | if (pStack->Parameters.Create.Options & FILE_DIRECTORY_FILE)
|
---|
244 | {
|
---|
245 | pIrp->IoStatus.Status = STATUS_NOT_A_DIRECTORY;
|
---|
246 | pIrp->IoStatus.Information = 0;
|
---|
247 | IoCompleteRequest(pIrp, IO_NO_INCREMENT);
|
---|
248 | return STATUS_NOT_A_DIRECTORY;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /*
|
---|
252 | * Call common code for the rest.
|
---|
253 | */
|
---|
254 | pFileObj->FsContext = NULL;
|
---|
255 | PSUPDRVSESSION pSession;
|
---|
256 | //#if 0 /** @todo check if this works, consider OBJ_KERNEL_HANDLE too. */
|
---|
257 | bool fUser = pIrp->RequestorMode != KernelMode;
|
---|
258 | //#else
|
---|
259 | // bool fUser = true;
|
---|
260 | //#endif
|
---|
261 | int rc = supdrvCreateSession(pDevExt, fUser, &pSession);
|
---|
262 | if (!rc)
|
---|
263 | pFileObj->FsContext = pSession;
|
---|
264 |
|
---|
265 | NTSTATUS rcNt = pIrp->IoStatus.Status = VBoxDrvNtErr2NtStatus(rc);
|
---|
266 | pIrp->IoStatus.Information = 0;
|
---|
267 | IoCompleteRequest(pIrp, IO_NO_INCREMENT);
|
---|
268 |
|
---|
269 | return rcNt;
|
---|
270 | }
|
---|
271 |
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Close file entry point.
|
---|
275 | *
|
---|
276 | * @param pDevObj Device object.
|
---|
277 | * @param pIrp Request packet.
|
---|
278 | */
|
---|
279 | NTSTATUS _stdcall VBoxDrvNtClose(PDEVICE_OBJECT pDevObj, PIRP pIrp)
|
---|
280 | {
|
---|
281 | PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;
|
---|
282 | PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
|
---|
283 | PFILE_OBJECT pFileObj = pStack->FileObject;
|
---|
284 | Log(("VBoxDrvNtClose: pDevExt=%p pFileObj=%p pSession=%p\n",
|
---|
285 | pDevExt, pFileObj, pFileObj->FsContext));
|
---|
286 | supdrvCloseSession(pDevExt, (PSUPDRVSESSION)pFileObj->FsContext);
|
---|
287 | pFileObj->FsContext = NULL;
|
---|
288 | pIrp->IoStatus.Information = 0;
|
---|
289 | pIrp->IoStatus.Status = STATUS_SUCCESS;
|
---|
290 | IoCompleteRequest(pIrp, IO_NO_INCREMENT);
|
---|
291 |
|
---|
292 | return STATUS_SUCCESS;
|
---|
293 | }
|
---|
294 |
|
---|
295 |
|
---|
296 | /**
|
---|
297 | * Device I/O Control entry point.
|
---|
298 | *
|
---|
299 | * @param pDevObj Device object.
|
---|
300 | * @param pIrp Request packet.
|
---|
301 | */
|
---|
302 | NTSTATUS _stdcall VBoxDrvNtDeviceControl(PDEVICE_OBJECT pDevObj, PIRP pIrp)
|
---|
303 | {
|
---|
304 | PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;
|
---|
305 | PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
|
---|
306 | PSUPDRVSESSION pSession = (PSUPDRVSESSION)pStack->FileObject->FsContext;
|
---|
307 |
|
---|
308 | /*
|
---|
309 | * Deal with the two high-speed IOCtl that takes it's arguments from
|
---|
310 | * the session and iCmd, and only returns a VBox status code.
|
---|
311 | *
|
---|
312 | * Note: The previous method of returning the rc prior to IOC version
|
---|
313 | * 7.4 has been abandond, we're no longer compatible with that
|
---|
314 | * interface.
|
---|
315 | */
|
---|
316 | ULONG ulCmd = pStack->Parameters.DeviceIoControl.IoControlCode;
|
---|
317 | if ( ulCmd == SUP_IOCTL_FAST_DO_RAW_RUN
|
---|
318 | || ulCmd == SUP_IOCTL_FAST_DO_HWACC_RUN
|
---|
319 | || ulCmd == SUP_IOCTL_FAST_DO_NOP)
|
---|
320 | {
|
---|
321 | #ifdef VBOX_WITH_VMMR0_DISABLE_PREEMPTION
|
---|
322 | int rc = supdrvIOCtlFast(ulCmd, (unsigned)(uintptr_t)pIrp->UserBuffer /* VMCPU id */, pDevExt, pSession);
|
---|
323 | #else
|
---|
324 | /* Raise the IRQL to DISPATCH_LEVEL to prevent Windows from rescheduling us to another CPU/core. */
|
---|
325 | Assert(KeGetCurrentIrql() <= DISPATCH_LEVEL);
|
---|
326 | KIRQL oldIrql;
|
---|
327 | KeRaiseIrql(DISPATCH_LEVEL, &oldIrql);
|
---|
328 | int rc = supdrvIOCtlFast(ulCmd, (unsigned)(uintptr_t)pIrp->UserBuffer /* VMCPU id */, pDevExt, pSession);
|
---|
329 | KeLowerIrql(oldIrql);
|
---|
330 | #endif
|
---|
331 |
|
---|
332 | /* Complete the I/O request. */
|
---|
333 | NTSTATUS rcNt = pIrp->IoStatus.Status = RT_SUCCESS(rc) ? STATUS_SUCCESS : STATUS_INVALID_PARAMETER;
|
---|
334 | IoCompleteRequest(pIrp, IO_NO_INCREMENT);
|
---|
335 | return rcNt;
|
---|
336 | }
|
---|
337 |
|
---|
338 | return VBoxDrvNtDeviceControlSlow(pDevExt, pSession, pIrp, pStack);
|
---|
339 | }
|
---|
340 |
|
---|
341 |
|
---|
342 | /**
|
---|
343 | * Worker for VBoxDrvNtDeviceControl that takes the slow IOCtl functions.
|
---|
344 | *
|
---|
345 | * @returns NT status code.
|
---|
346 | *
|
---|
347 | * @param pDevObj Device object.
|
---|
348 | * @param pSession The session.
|
---|
349 | * @param pIrp Request packet.
|
---|
350 | * @param pStack The stack location containing the DeviceControl parameters.
|
---|
351 | */
|
---|
352 | static int VBoxDrvNtDeviceControlSlow(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PIRP pIrp, PIO_STACK_LOCATION pStack)
|
---|
353 | {
|
---|
354 | NTSTATUS rcNt;
|
---|
355 | unsigned cbOut = 0;
|
---|
356 | int rc = 0;
|
---|
357 | Log2(("VBoxDrvNtDeviceControlSlow(%p,%p): ioctl=%#x pBuf=%p cbIn=%#x cbOut=%#x pSession=%p\n",
|
---|
358 | pDevExt, pIrp, pStack->Parameters.DeviceIoControl.IoControlCode,
|
---|
359 | pIrp->AssociatedIrp.SystemBuffer, pStack->Parameters.DeviceIoControl.InputBufferLength,
|
---|
360 | pStack->Parameters.DeviceIoControl.OutputBufferLength, pSession));
|
---|
361 |
|
---|
362 | #ifdef RT_ARCH_AMD64
|
---|
363 | /* Don't allow 32-bit processes to do any I/O controls. */
|
---|
364 | if (!IoIs32bitProcess(pIrp))
|
---|
365 | #endif
|
---|
366 | {
|
---|
367 | /* Verify that it's a buffered CTL. */
|
---|
368 | if ((pStack->Parameters.DeviceIoControl.IoControlCode & 0x3) == METHOD_BUFFERED)
|
---|
369 | {
|
---|
370 | /* Verify that the sizes in the request header are correct. */
|
---|
371 | PSUPREQHDR pHdr = (PSUPREQHDR)pIrp->AssociatedIrp.SystemBuffer;
|
---|
372 | if ( pStack->Parameters.DeviceIoControl.InputBufferLength >= sizeof(*pHdr)
|
---|
373 | && pStack->Parameters.DeviceIoControl.InputBufferLength == pHdr->cbIn
|
---|
374 | && pStack->Parameters.DeviceIoControl.OutputBufferLength == pHdr->cbOut)
|
---|
375 | {
|
---|
376 | /*
|
---|
377 | * Do the job.
|
---|
378 | */
|
---|
379 | rc = supdrvIOCtl(pStack->Parameters.DeviceIoControl.IoControlCode, pDevExt, pSession, pHdr);
|
---|
380 | if (!rc)
|
---|
381 | {
|
---|
382 | rcNt = STATUS_SUCCESS;
|
---|
383 | cbOut = pHdr->cbOut;
|
---|
384 | if (cbOut > pStack->Parameters.DeviceIoControl.OutputBufferLength)
|
---|
385 | {
|
---|
386 | cbOut = pStack->Parameters.DeviceIoControl.OutputBufferLength;
|
---|
387 | OSDBGPRINT(("VBoxDrvLinuxIOCtl: too much output! %#x > %#x; uCmd=%#x!\n",
|
---|
388 | pHdr->cbOut, cbOut, pStack->Parameters.DeviceIoControl.IoControlCode));
|
---|
389 | }
|
---|
390 | }
|
---|
391 | else
|
---|
392 | rcNt = STATUS_INVALID_PARAMETER;
|
---|
393 | Log2(("VBoxDrvNtDeviceControlSlow: returns %#x cbOut=%d rc=%#x\n", rcNt, cbOut, rc));
|
---|
394 | }
|
---|
395 | else
|
---|
396 | {
|
---|
397 | Log(("VBoxDrvNtDeviceControlSlow: Mismatching sizes (%#x) - Hdr=%#lx/%#lx Irp=%#lx/%#lx!\n",
|
---|
398 | pStack->Parameters.DeviceIoControl.IoControlCode,
|
---|
399 | pStack->Parameters.DeviceIoControl.InputBufferLength >= sizeof(*pHdr) ? pHdr->cbIn : 0,
|
---|
400 | pStack->Parameters.DeviceIoControl.InputBufferLength >= sizeof(*pHdr) ? pHdr->cbOut : 0,
|
---|
401 | pStack->Parameters.DeviceIoControl.InputBufferLength,
|
---|
402 | pStack->Parameters.DeviceIoControl.OutputBufferLength));
|
---|
403 | rcNt = STATUS_INVALID_PARAMETER;
|
---|
404 | }
|
---|
405 | }
|
---|
406 | else
|
---|
407 | {
|
---|
408 | Log(("VBoxDrvNtDeviceControlSlow: not buffered request (%#x) - not supported\n",
|
---|
409 | pStack->Parameters.DeviceIoControl.IoControlCode));
|
---|
410 | rcNt = STATUS_NOT_SUPPORTED;
|
---|
411 | }
|
---|
412 | }
|
---|
413 | #ifdef RT_ARCH_AMD64
|
---|
414 | else
|
---|
415 | {
|
---|
416 | Log(("VBoxDrvNtDeviceControlSlow: WOW64 req - not supported\n"));
|
---|
417 | rcNt = STATUS_NOT_SUPPORTED;
|
---|
418 | }
|
---|
419 | #endif
|
---|
420 |
|
---|
421 | /* complete the request. */
|
---|
422 | pIrp->IoStatus.Status = rcNt;
|
---|
423 | pIrp->IoStatus.Information = cbOut;
|
---|
424 | IoCompleteRequest(pIrp, IO_NO_INCREMENT);
|
---|
425 | return rcNt;
|
---|
426 | }
|
---|
427 |
|
---|
428 |
|
---|
429 | /**
|
---|
430 | * Internal Device I/O Control entry point, used for IDC.
|
---|
431 | *
|
---|
432 | * @param pDevObj Device object.
|
---|
433 | * @param pIrp Request packet.
|
---|
434 | */
|
---|
435 | NTSTATUS _stdcall VBoxDrvNtInternalDeviceControl(PDEVICE_OBJECT pDevObj, PIRP pIrp)
|
---|
436 | {
|
---|
437 | PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;
|
---|
438 | PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
|
---|
439 | PFILE_OBJECT pFileObj = pStack ? pStack->FileObject : NULL;
|
---|
440 | PSUPDRVSESSION pSession = pFileObj ? (PSUPDRVSESSION)pFileObj->FsContext : NULL;
|
---|
441 | NTSTATUS rcNt;
|
---|
442 | unsigned cbOut = 0;
|
---|
443 | int rc = 0;
|
---|
444 | Log2(("VBoxDrvNtInternalDeviceControl(%p,%p): ioctl=%#x pBuf=%p cbIn=%#x cbOut=%#x pSession=%p\n",
|
---|
445 | pDevExt, pIrp, pStack->Parameters.DeviceIoControl.IoControlCode,
|
---|
446 | pIrp->AssociatedIrp.SystemBuffer, pStack->Parameters.DeviceIoControl.InputBufferLength,
|
---|
447 | pStack->Parameters.DeviceIoControl.OutputBufferLength, pSession));
|
---|
448 |
|
---|
449 | /** @todo IDC on NT: figure when to create the session and that stuff... */
|
---|
450 |
|
---|
451 | /* Verify that it's a buffered CTL. */
|
---|
452 | if ((pStack->Parameters.DeviceIoControl.IoControlCode & 0x3) == METHOD_BUFFERED)
|
---|
453 | {
|
---|
454 | /* Verify the pDevExt in the session. */
|
---|
455 | if ( ( !pSession
|
---|
456 | && pStack->Parameters.DeviceIoControl.IoControlCode == SUPDRV_IDC_REQ_CONNECT)
|
---|
457 | || ( VALID_PTR(pSession)
|
---|
458 | && pSession->pDevExt == pDevExt))
|
---|
459 | {
|
---|
460 | /* Verify that the size in the request header is correct. */
|
---|
461 | PSUPDRVIDCREQHDR pHdr = (PSUPDRVIDCREQHDR)pIrp->AssociatedIrp.SystemBuffer;
|
---|
462 | if ( pStack->Parameters.DeviceIoControl.InputBufferLength >= sizeof(*pHdr)
|
---|
463 | && pStack->Parameters.DeviceIoControl.InputBufferLength == pHdr->cb
|
---|
464 | && pStack->Parameters.DeviceIoControl.OutputBufferLength == pHdr->cb)
|
---|
465 | {
|
---|
466 | /*
|
---|
467 | * Do the job.
|
---|
468 | */
|
---|
469 | rc = supdrvIDC(pStack->Parameters.DeviceIoControl.IoControlCode, pDevExt, pSession, pHdr);
|
---|
470 | if (!rc)
|
---|
471 | {
|
---|
472 | rcNt = STATUS_SUCCESS;
|
---|
473 | cbOut = pHdr->cb;
|
---|
474 | }
|
---|
475 | else
|
---|
476 | rcNt = STATUS_INVALID_PARAMETER;
|
---|
477 | Log2(("VBoxDrvNtInternalDeviceControl: returns %#x/rc=%#x\n", rcNt, rc));
|
---|
478 | }
|
---|
479 | else
|
---|
480 | {
|
---|
481 | Log(("VBoxDrvNtInternalDeviceControl: Mismatching sizes (%#x) - Hdr=%#lx Irp=%#lx/%#lx!\n",
|
---|
482 | pStack->Parameters.DeviceIoControl.IoControlCode,
|
---|
483 | pStack->Parameters.DeviceIoControl.InputBufferLength >= sizeof(*pHdr) ? pHdr->cb : 0,
|
---|
484 | pStack->Parameters.DeviceIoControl.InputBufferLength,
|
---|
485 | pStack->Parameters.DeviceIoControl.OutputBufferLength));
|
---|
486 | rcNt = STATUS_INVALID_PARAMETER;
|
---|
487 | }
|
---|
488 | }
|
---|
489 | else
|
---|
490 | rcNt = STATUS_NOT_SUPPORTED;
|
---|
491 | }
|
---|
492 | else
|
---|
493 | {
|
---|
494 | Log(("VBoxDrvNtInternalDeviceControl: not buffered request (%#x) - not supported\n",
|
---|
495 | pStack->Parameters.DeviceIoControl.IoControlCode));
|
---|
496 | rcNt = STATUS_NOT_SUPPORTED;
|
---|
497 | }
|
---|
498 |
|
---|
499 | /* complete the request. */
|
---|
500 | pIrp->IoStatus.Status = rcNt;
|
---|
501 | pIrp->IoStatus.Information = cbOut;
|
---|
502 | IoCompleteRequest(pIrp, IO_NO_INCREMENT);
|
---|
503 | return rcNt;
|
---|
504 | }
|
---|
505 |
|
---|
506 |
|
---|
507 | /**
|
---|
508 | * Stub function for functions we don't implemented.
|
---|
509 | *
|
---|
510 | * @returns STATUS_NOT_SUPPORTED
|
---|
511 | * @param pDevObj Device object.
|
---|
512 | * @param pIrp IRP.
|
---|
513 | */
|
---|
514 | NTSTATUS _stdcall VBoxDrvNtNotSupportedStub(PDEVICE_OBJECT pDevObj, PIRP pIrp)
|
---|
515 | {
|
---|
516 | Log(("VBoxDrvNtNotSupportedStub\n"));
|
---|
517 | pDevObj = pDevObj;
|
---|
518 |
|
---|
519 | pIrp->IoStatus.Information = 0;
|
---|
520 | pIrp->IoStatus.Status = STATUS_NOT_SUPPORTED;
|
---|
521 | IoCompleteRequest(pIrp, IO_NO_INCREMENT);
|
---|
522 |
|
---|
523 | return STATUS_NOT_SUPPORTED;
|
---|
524 | }
|
---|
525 |
|
---|
526 |
|
---|
527 | /**
|
---|
528 | * ExRegisterCallback handler for power events
|
---|
529 | *
|
---|
530 | * @param pCallbackContext User supplied parameter (pDevObj)
|
---|
531 | * @param pArgument1 First argument
|
---|
532 | * @param pArgument2 Second argument
|
---|
533 | */
|
---|
534 | VOID _stdcall VBoxPowerDispatchCallback(PVOID pCallbackContext, PVOID pArgument1, PVOID pArgument2)
|
---|
535 | {
|
---|
536 | PDEVICE_OBJECT pDevObj = (PDEVICE_OBJECT)pCallbackContext;
|
---|
537 |
|
---|
538 | Log(("VBoxPowerDispatchCallback: %x %x\n", pArgument1, pArgument2));
|
---|
539 |
|
---|
540 | /* Power change imminent? */
|
---|
541 | if ((unsigned)pArgument1 == PO_CB_SYSTEM_STATE_LOCK)
|
---|
542 | {
|
---|
543 | if ((unsigned)pArgument2 == 0)
|
---|
544 | Log(("VBoxPowerDispatchCallback: about to go into suspend mode!\n"));
|
---|
545 | else
|
---|
546 | Log(("VBoxPowerDispatchCallback: resumed!\n"));
|
---|
547 |
|
---|
548 | /* Inform any clients that have registered themselves with IPRT. */
|
---|
549 | RTPowerSignalEvent(((unsigned)pArgument2 == 0) ? RTPOWEREVENT_SUSPEND : RTPOWEREVENT_RESUME);
|
---|
550 | }
|
---|
551 | }
|
---|
552 |
|
---|
553 |
|
---|
554 | /**
|
---|
555 | * Initializes any OS specific object creator fields.
|
---|
556 | */
|
---|
557 | void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
|
---|
558 | {
|
---|
559 | NOREF(pObj);
|
---|
560 | NOREF(pSession);
|
---|
561 | }
|
---|
562 |
|
---|
563 |
|
---|
564 | /**
|
---|
565 | * Checks if the session can access the object.
|
---|
566 | *
|
---|
567 | * @returns true if a decision has been made.
|
---|
568 | * @returns false if the default access policy should be applied.
|
---|
569 | *
|
---|
570 | * @param pObj The object in question.
|
---|
571 | * @param pSession The session wanting to access the object.
|
---|
572 | * @param pszObjName The object name, can be NULL.
|
---|
573 | * @param prc Where to store the result when returning true.
|
---|
574 | */
|
---|
575 | bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
|
---|
576 | {
|
---|
577 | NOREF(pObj);
|
---|
578 | NOREF(pSession);
|
---|
579 | NOREF(pszObjName);
|
---|
580 | NOREF(prc);
|
---|
581 | return false;
|
---|
582 | }
|
---|
583 |
|
---|
584 |
|
---|
585 | /**
|
---|
586 | * Force async tsc mode (stub).
|
---|
587 | */
|
---|
588 | bool VBOXCALL supdrvOSGetForcedAsyncTscMode(PSUPDRVDEVEXT pDevExt)
|
---|
589 | {
|
---|
590 | return false;
|
---|
591 | }
|
---|
592 |
|
---|
593 |
|
---|
594 | #define MY_SystemLoadGdiDriverInSystemSpaceInformation 54
|
---|
595 | #define MY_SystemUnloadGdiDriverInformation 27
|
---|
596 |
|
---|
597 | typedef struct MYSYSTEMGDIDRIVERINFO
|
---|
598 | {
|
---|
599 | UNICODE_STRING Name; /**< In: image file name. */
|
---|
600 | PVOID ImageAddress; /**< Out: the load address. */
|
---|
601 | PVOID SectionPointer; /**< Out: section object. */
|
---|
602 | PVOID EntryPointer; /**< Out: entry point address. */
|
---|
603 | PVOID ExportSectionPointer; /**< Out: export directory/section. */
|
---|
604 | ULONG ImageLength; /**< Out: SizeOfImage. */
|
---|
605 | } MYSYSTEMGDIDRIVERINFO;
|
---|
606 |
|
---|
607 | extern "C" __declspec(dllimport) NTSTATUS NTAPI ZwSetSystemInformation(ULONG, PVOID, ULONG);
|
---|
608 |
|
---|
609 | int VBOXCALL supdrvOSLdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const char *pszFilename)
|
---|
610 | {
|
---|
611 | pImage->pvNtSectionObj = NULL;
|
---|
612 | pImage->hMemLock = NIL_RTR0MEMOBJ;
|
---|
613 |
|
---|
614 | #ifdef VBOX_WITHOUT_NATIVE_R0_LOADER
|
---|
615 | # ifndef RT_ARCH_X86
|
---|
616 | # error "VBOX_WITHOUT_NATIVE_R0_LOADER is only safe on x86."
|
---|
617 | # endif
|
---|
618 | NOREF(pDevExt); NOREF(pszFilename); NOREF(pImage);
|
---|
619 | return VERR_NOT_SUPPORTED;
|
---|
620 |
|
---|
621 | #else
|
---|
622 | /*
|
---|
623 | * Convert the filename from DOS UTF-8 to NT UTF-16.
|
---|
624 | */
|
---|
625 | size_t cwcFilename;
|
---|
626 | int rc = RTStrCalcUtf16LenEx(pszFilename, RTSTR_MAX, &cwcFilename);
|
---|
627 | if (RT_FAILURE(rc))
|
---|
628 | return rc;
|
---|
629 |
|
---|
630 | PRTUTF16 pwcsFilename = (PRTUTF16)RTMemTmpAlloc((4 + cwcFilename + 1) * sizeof(RTUTF16));
|
---|
631 | if (!pwcsFilename)
|
---|
632 | return VERR_NO_TMP_MEMORY;
|
---|
633 |
|
---|
634 | pwcsFilename[0] = '\\';
|
---|
635 | pwcsFilename[1] = '?';
|
---|
636 | pwcsFilename[2] = '?';
|
---|
637 | pwcsFilename[3] = '\\';
|
---|
638 | PRTUTF16 pwcsTmp = &pwcsFilename[4];
|
---|
639 | rc = RTStrToUtf16Ex(pszFilename, RTSTR_MAX, &pwcsTmp, cwcFilename + 1, NULL);
|
---|
640 | if (RT_SUCCESS(rc))
|
---|
641 | {
|
---|
642 | /*
|
---|
643 | * Try load it.
|
---|
644 | */
|
---|
645 | MYSYSTEMGDIDRIVERINFO Info;
|
---|
646 | RtlInitUnicodeString(&Info.Name, pwcsFilename);
|
---|
647 | Info.ImageAddress = NULL;
|
---|
648 | Info.SectionPointer = NULL;
|
---|
649 | Info.EntryPointer = NULL;
|
---|
650 | Info.ExportSectionPointer = NULL;
|
---|
651 | Info.ImageLength = 0;
|
---|
652 |
|
---|
653 | NTSTATUS rcNt = ZwSetSystemInformation(MY_SystemLoadGdiDriverInSystemSpaceInformation, &Info, sizeof(Info));
|
---|
654 | if (NT_SUCCESS(rcNt))
|
---|
655 | {
|
---|
656 | pImage->pvImage = Info.ImageAddress;
|
---|
657 | pImage->pvNtSectionObj = Info.SectionPointer;
|
---|
658 | Log(("ImageAddress=%p SectionPointer=%p ImageLength=%#x cbImageBits=%#x rcNt=%#x '%ls'\n",
|
---|
659 | Info.ImageAddress, Info.SectionPointer, Info.ImageLength, pImage->cbImageBits, rcNt, Info.Name.Buffer));
|
---|
660 | # ifdef DEBUG_bird
|
---|
661 | SUPR0Printf("ImageAddress=%p SectionPointer=%p ImageLength=%#x cbImageBits=%#x rcNt=%#x '%ws'\n",
|
---|
662 | Info.ImageAddress, Info.SectionPointer, Info.ImageLength, pImage->cbImageBits, rcNt, Info.Name.Buffer);
|
---|
663 | # endif
|
---|
664 | if (pImage->cbImageBits == Info.ImageLength)
|
---|
665 | {
|
---|
666 | /*
|
---|
667 | * Lock down the entire image, just to be on the safe side.
|
---|
668 | */
|
---|
669 | rc = RTR0MemObjLockKernel(&pImage->hMemLock, pImage->pvImage, pImage->cbImageBits, RTMEM_PROT_READ);
|
---|
670 | if (RT_FAILURE(rc))
|
---|
671 | {
|
---|
672 | pImage->hMemLock = NIL_RTR0MEMOBJ;
|
---|
673 | supdrvOSLdrUnload(pDevExt, pImage);
|
---|
674 | }
|
---|
675 | }
|
---|
676 | else
|
---|
677 | {
|
---|
678 | supdrvOSLdrUnload(pDevExt, pImage);
|
---|
679 | rc = VERR_LDR_MISMATCH_NATIVE;
|
---|
680 | }
|
---|
681 | }
|
---|
682 | else
|
---|
683 | {
|
---|
684 | Log(("rcNt=%#x '%ls'\n", rcNt, pwcsFilename));
|
---|
685 | SUPR0Printf("VBoxDrv: rcNt=%x '%ws'\n", rcNt, pwcsFilename);
|
---|
686 | switch (rcNt)
|
---|
687 | {
|
---|
688 | case /* 0xc0000003 */ STATUS_INVALID_INFO_CLASS:
|
---|
689 | # ifdef RT_ARCH_AMD64
|
---|
690 | /* Unwind will crash and BSOD, so no fallback here! */
|
---|
691 | rc = VERR_NOT_IMPLEMENTED;
|
---|
692 | # else
|
---|
693 | /*
|
---|
694 | * Use the old way of loading the modules.
|
---|
695 | *
|
---|
696 | * Note! We do *NOT* try class 26 because it will probably
|
---|
697 | * not work correctly on terminal servers and such.
|
---|
698 | */
|
---|
699 | rc = VERR_NOT_SUPPORTED;
|
---|
700 | # endif
|
---|
701 | break;
|
---|
702 | case /* 0xc0000034 */ STATUS_OBJECT_NAME_NOT_FOUND:
|
---|
703 | rc = VERR_MODULE_NOT_FOUND;
|
---|
704 | break;
|
---|
705 | case /* 0xC0000263 */ STATUS_DRIVER_ENTRYPOINT_NOT_FOUND:
|
---|
706 | rc = VERR_LDR_IMPORTED_SYMBOL_NOT_FOUND;
|
---|
707 | break;
|
---|
708 | case 0xC0000428 /* STATUS_INVALID_IMAGE_HASH */ :
|
---|
709 | rc = VERR_LDR_IMAGE_HASH;
|
---|
710 | break;
|
---|
711 | case 0xC000010E /* STATUS_IMAGE_ALREADY_LOADED */ :
|
---|
712 | Log(("WARNING: see #4853 for cause of this failure on Windows 7 x64\n"));
|
---|
713 | rc = VERR_ALREADY_LOADED;
|
---|
714 | break;
|
---|
715 | default:
|
---|
716 | rc = VERR_LDR_GENERAL_FAILURE;
|
---|
717 | break;
|
---|
718 | }
|
---|
719 |
|
---|
720 | pImage->pvNtSectionObj = NULL;
|
---|
721 | }
|
---|
722 | }
|
---|
723 |
|
---|
724 | RTMemTmpFree(pwcsFilename);
|
---|
725 | NOREF(pDevExt);
|
---|
726 | return rc;
|
---|
727 | #endif
|
---|
728 | }
|
---|
729 |
|
---|
730 |
|
---|
731 | int VBOXCALL supdrvOSLdrValidatePointer(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, void *pv, const uint8_t *pbImageBits)
|
---|
732 | {
|
---|
733 | NOREF(pDevExt); NOREF(pImage); NOREF(pv); NOREF(pbImageBits);
|
---|
734 | return VINF_SUCCESS;
|
---|
735 | }
|
---|
736 |
|
---|
737 |
|
---|
738 | /**
|
---|
739 | * memcmp + log.
|
---|
740 | *
|
---|
741 | * @returns Same as memcmp.
|
---|
742 | * @param pImage The image.
|
---|
743 | * @param pbImageBits The image bits ring-3 uploads.
|
---|
744 | * @param uRva The RVA to start comparing at.
|
---|
745 | * @param cb The number of bytes to compare.
|
---|
746 | */
|
---|
747 | static int supdrvNtCompare(PSUPDRVLDRIMAGE pImage, const uint8_t *pbImageBits, uint32_t uRva, uint32_t cb)
|
---|
748 | {
|
---|
749 | int iDiff = memcmp((uint8_t const *)pImage->pvImage + uRva, pbImageBits + uRva, cb);
|
---|
750 | if (iDiff)
|
---|
751 | {
|
---|
752 | uint32_t cbLeft = cb;
|
---|
753 | const uint8_t *pbNativeBits = (const uint8_t *)pImage->pvImage;
|
---|
754 | for (size_t off = uRva; cbLeft > 0; off++, cbLeft--)
|
---|
755 | if (pbNativeBits[off] != pbImageBits[off])
|
---|
756 | {
|
---|
757 | char szBytes[128];
|
---|
758 | RTStrPrintf(szBytes, sizeof(szBytes), "native: %.*Rhxs our: %.*Rhxs",
|
---|
759 | RT_MIN(12, cbLeft), &pbNativeBits[off],
|
---|
760 | RT_MIN(12, cbLeft), &pbImageBits[off]);
|
---|
761 | SUPR0Printf("VBoxDrv: Mismatch at %#x of %s: %s\n", off, pImage->szName, szBytes);
|
---|
762 | break;
|
---|
763 | }
|
---|
764 | }
|
---|
765 | return iDiff;
|
---|
766 | }
|
---|
767 |
|
---|
768 | int VBOXCALL supdrvOSLdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const uint8_t *pbImageBits, PSUPLDRLOAD pReq)
|
---|
769 | {
|
---|
770 | NOREF(pDevExt); NOREF(pReq);
|
---|
771 | if (pImage->pvNtSectionObj)
|
---|
772 | {
|
---|
773 | /*
|
---|
774 | * Usually, the entire image matches exactly.
|
---|
775 | */
|
---|
776 | if (!memcmp(pImage->pvImage, pbImageBits, pImage->cbImageBits))
|
---|
777 | return VINF_SUCCESS;
|
---|
778 |
|
---|
779 | /*
|
---|
780 | * However, on Windows Server 2003 (sp2 x86) both import thunk tables
|
---|
781 | * are fixed up and we typically get a mismatch in the INIT section.
|
---|
782 | *
|
---|
783 | * So, lets see if everything matches when excluding the
|
---|
784 | * OriginalFirstThunk tables. To make life simpler, set the max number
|
---|
785 | * of imports to 16 and just record and sort the locations that needs
|
---|
786 | * to be excluded from the comparison.
|
---|
787 | */
|
---|
788 | IMAGE_NT_HEADERS const *pNtHdrs;
|
---|
789 | pNtHdrs = (IMAGE_NT_HEADERS const *)(pbImageBits
|
---|
790 | + ( *(uint16_t *)pbImageBits == IMAGE_DOS_SIGNATURE
|
---|
791 | ? ((IMAGE_DOS_HEADER const *)pbImageBits)->e_lfanew
|
---|
792 | : 0));
|
---|
793 | if ( pNtHdrs->Signature == IMAGE_NT_SIGNATURE
|
---|
794 | && pNtHdrs->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR_MAGIC
|
---|
795 | && pNtHdrs->OptionalHeader.NumberOfRvaAndSizes > IMAGE_DIRECTORY_ENTRY_IMPORT
|
---|
796 | && pNtHdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size >= sizeof(IMAGE_IMPORT_DESCRIPTOR)
|
---|
797 | && pNtHdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress > sizeof(IMAGE_NT_HEADERS)
|
---|
798 | && pNtHdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress < pImage->cbImageBits
|
---|
799 | )
|
---|
800 | {
|
---|
801 | struct MyRegion
|
---|
802 | {
|
---|
803 | uint32_t uRva;
|
---|
804 | uint32_t cb;
|
---|
805 | } aExcludeRgns[16];
|
---|
806 | unsigned cExcludeRgns = 0;
|
---|
807 | uint32_t cImpsLeft = pNtHdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size
|
---|
808 | / sizeof(IMAGE_IMPORT_DESCRIPTOR);
|
---|
809 | IMAGE_IMPORT_DESCRIPTOR const *pImp;
|
---|
810 | pImp = (IMAGE_IMPORT_DESCRIPTOR const *)(pbImageBits
|
---|
811 | + pNtHdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
|
---|
812 | while ( cImpsLeft-- > 0
|
---|
813 | && cExcludeRgns < RT_ELEMENTS(aExcludeRgns))
|
---|
814 | {
|
---|
815 | uint32_t uRvaThunk = pImp->OriginalFirstThunk;
|
---|
816 | if ( uRvaThunk > sizeof(IMAGE_NT_HEADERS)
|
---|
817 | && uRvaThunk <= pImage->cbImageBits - sizeof(IMAGE_THUNK_DATA)
|
---|
818 | && uRvaThunk != pImp->FirstThunk)
|
---|
819 | {
|
---|
820 | /* Find the size of the thunk table. */
|
---|
821 | IMAGE_THUNK_DATA const *paThunk = (IMAGE_THUNK_DATA const *)(pbImageBits + uRvaThunk);
|
---|
822 | uint32_t cMaxThunks = (pImage->cbImageBits - uRvaThunk) / sizeof(IMAGE_THUNK_DATA);
|
---|
823 | uint32_t cThunks = 0;
|
---|
824 | while (cThunks < cMaxThunks && paThunk[cThunks].u1.Function != 0)
|
---|
825 | cThunks++;
|
---|
826 |
|
---|
827 | /* Ordered table insert. */
|
---|
828 | unsigned i = 0;
|
---|
829 | for (; i < cExcludeRgns; i++)
|
---|
830 | if (uRvaThunk < aExcludeRgns[i].uRva)
|
---|
831 | break;
|
---|
832 | if (i != cExcludeRgns)
|
---|
833 | memmove(&aExcludeRgns[i + 1], &aExcludeRgns[i], (cExcludeRgns - i) * sizeof(aExcludeRgns[0]));
|
---|
834 | aExcludeRgns[i].uRva = uRvaThunk;
|
---|
835 | aExcludeRgns[i].cb = cThunks * sizeof(IMAGE_THUNK_DATA);
|
---|
836 | cExcludeRgns++;
|
---|
837 | }
|
---|
838 |
|
---|
839 | /* advance */
|
---|
840 | pImp++;
|
---|
841 | }
|
---|
842 |
|
---|
843 | /*
|
---|
844 | * Ok, do the comparison.
|
---|
845 | */
|
---|
846 | int iDiff = 0;
|
---|
847 | uint32_t uRvaNext = 0;
|
---|
848 | for (unsigned i = 0; !iDiff && i < cExcludeRgns; i++)
|
---|
849 | {
|
---|
850 | if (uRvaNext < aExcludeRgns[i].uRva)
|
---|
851 | iDiff = supdrvNtCompare(pImage, pbImageBits, uRvaNext, aExcludeRgns[i].uRva - uRvaNext);
|
---|
852 | uRvaNext = aExcludeRgns[i].uRva + aExcludeRgns[i].cb;
|
---|
853 | }
|
---|
854 | if (!iDiff && uRvaNext < pImage->cbImageBits)
|
---|
855 | iDiff = supdrvNtCompare(pImage, pbImageBits, uRvaNext, pImage->cbImageBits - uRvaNext);
|
---|
856 | if (!iDiff)
|
---|
857 | return VINF_SUCCESS;
|
---|
858 | }
|
---|
859 | else
|
---|
860 | supdrvNtCompare(pImage, pbImageBits, 0, pImage->cbImageBits);
|
---|
861 | return VERR_LDR_MISMATCH_NATIVE;
|
---|
862 | }
|
---|
863 | return VERR_INTERNAL_ERROR_4;
|
---|
864 | }
|
---|
865 |
|
---|
866 |
|
---|
867 | void VBOXCALL supdrvOSLdrUnload(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
|
---|
868 | {
|
---|
869 | if (pImage->pvNtSectionObj)
|
---|
870 | {
|
---|
871 | if (pImage->hMemLock != NIL_RTR0MEMOBJ)
|
---|
872 | {
|
---|
873 | RTR0MemObjFree(pImage->hMemLock, false /*fFreeMappings*/);
|
---|
874 | pImage->hMemLock = NIL_RTR0MEMOBJ;
|
---|
875 | }
|
---|
876 |
|
---|
877 | NTSTATUS rcNt = ZwSetSystemInformation(MY_SystemUnloadGdiDriverInformation,
|
---|
878 | &pImage->pvNtSectionObj, sizeof(pImage->pvNtSectionObj));
|
---|
879 | if (rcNt != STATUS_SUCCESS)
|
---|
880 | SUPR0Printf("VBoxDrv: failed to unload '%s', rcNt=%#x\n", pImage->szName, rcNt);
|
---|
881 | pImage->pvNtSectionObj = NULL;
|
---|
882 | }
|
---|
883 | NOREF(pDevExt);
|
---|
884 | }
|
---|
885 |
|
---|
886 |
|
---|
887 | /**
|
---|
888 | * Converts an IPRT error code to an nt status code.
|
---|
889 | *
|
---|
890 | * @returns corresponding nt status code.
|
---|
891 | * @param rc IPRT error status code.
|
---|
892 | */
|
---|
893 | static NTSTATUS VBoxDrvNtErr2NtStatus(int rc)
|
---|
894 | {
|
---|
895 | switch (rc)
|
---|
896 | {
|
---|
897 | case VINF_SUCCESS: return STATUS_SUCCESS;
|
---|
898 | case VERR_GENERAL_FAILURE: return STATUS_NOT_SUPPORTED;
|
---|
899 | case VERR_INVALID_PARAMETER: return STATUS_INVALID_PARAMETER;
|
---|
900 | case VERR_INVALID_MAGIC: return STATUS_UNKNOWN_REVISION;
|
---|
901 | case VERR_INVALID_HANDLE: return STATUS_INVALID_HANDLE;
|
---|
902 | case VERR_INVALID_POINTER: return STATUS_INVALID_ADDRESS;
|
---|
903 | case VERR_LOCK_FAILED: return STATUS_NOT_LOCKED;
|
---|
904 | case VERR_ALREADY_LOADED: return STATUS_IMAGE_ALREADY_LOADED;
|
---|
905 | case VERR_PERMISSION_DENIED: return STATUS_ACCESS_DENIED;
|
---|
906 | case VERR_VERSION_MISMATCH: return STATUS_REVISION_MISMATCH;
|
---|
907 | }
|
---|
908 |
|
---|
909 | return STATUS_UNSUCCESSFUL;
|
---|
910 | }
|
---|
911 |
|
---|
912 |
|
---|
913 |
|
---|
914 | /** @todo use the nocrt stuff? */
|
---|
915 | int VBOXCALL mymemcmp(const void *pv1, const void *pv2, size_t cb)
|
---|
916 | {
|
---|
917 | const uint8_t *pb1 = (const uint8_t *)pv1;
|
---|
918 | const uint8_t *pb2 = (const uint8_t *)pv2;
|
---|
919 | for (; cb > 0; cb--, pb1++, pb2++)
|
---|
920 | if (*pb1 != *pb2)
|
---|
921 | return *pb1 - *pb2;
|
---|
922 | return 0;
|
---|
923 | }
|
---|
924 |
|
---|
925 |
|
---|
926 | #if 0 /* See alternative in SUPDrvA-win.asm */
|
---|
927 | /**
|
---|
928 | * Alternative version of SUPR0Printf for Windows.
|
---|
929 | *
|
---|
930 | * @returns 0.
|
---|
931 | * @param pszFormat The format string.
|
---|
932 | */
|
---|
933 | SUPR0DECL(int) SUPR0Printf(const char *pszFormat, ...)
|
---|
934 | {
|
---|
935 | va_list va;
|
---|
936 | char szMsg[512];
|
---|
937 |
|
---|
938 | va_start(va, pszFormat);
|
---|
939 | size_t cch = RTStrPrintfV(szMsg, sizeof(szMsg) - 1, pszFormat, va);
|
---|
940 | szMsg[sizeof(szMsg) - 1] = '\0';
|
---|
941 | va_end(va);
|
---|
942 |
|
---|
943 | RTLogWriteDebugger(szMsg, cch);
|
---|
944 | return 0;
|
---|
945 | }
|
---|
946 | #endif
|
---|