VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/win/SUPDrv-win.cpp@ 7272

Last change on this file since 7272 was 7272, checked in by vboxsync, 17 years ago

Use MP functions in the runtime.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.9 KB
Line 
1/* $Id: SUPDrv-win.cpp 7272 2008-03-04 12:33:43Z vboxsync $ */
2/** @file
3 * VirtualBox Support Driver - Windows NT specific parts.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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
29/*******************************************************************************
30* Header Files *
31*******************************************************************************/
32#include "SUPDRV.h"
33#include <excpt.h>
34#include <iprt/assert.h>
35#include <iprt/process.h>
36
37
38/*******************************************************************************
39* Defined Constants And Macros *
40*******************************************************************************/
41/** The support service name. */
42#define SERVICE_NAME "VBoxDrv"
43/** Win32 Device name. */
44#define DEVICE_NAME "\\\\.\\VBoxDrv"
45/** NT Device name. */
46#define DEVICE_NAME_NT L"\\Device\\VBoxDrv"
47/** Win Symlink name. */
48#define DEVICE_NAME_DOS L"\\DosDevices\\VBoxDrv"
49/** The Pool tag (VBox). */
50#define SUPDRV_NT_POOL_TAG 'xoBV'
51
52
53/*******************************************************************************
54* Structures and Typedefs *
55*******************************************************************************/
56#if 0 //def RT_ARCH_AMD64
57typedef struct SUPDRVEXECMEM
58{
59 PMDL pMdl;
60 void *pvMapping;
61 void *pvAllocation;
62} SUPDRVEXECMEM, *PSUPDRVEXECMEM;
63#endif
64
65
66/*******************************************************************************
67* Internal Functions *
68*******************************************************************************/
69static void _stdcall VBoxDrvNtUnload(PDRIVER_OBJECT pDrvObj);
70static NTSTATUS _stdcall VBoxDrvNtCreate(PDEVICE_OBJECT pDevObj, PIRP pIrp);
71static NTSTATUS _stdcall VBoxDrvNtClose(PDEVICE_OBJECT pDevObj, PIRP pIrp);
72static NTSTATUS _stdcall VBoxDrvNtDeviceControl(PDEVICE_OBJECT pDevObj, PIRP pIrp);
73static int VBoxDrvNtDeviceControlSlow(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PIRP pIrp, PIO_STACK_LOCATION pStack);
74static NTSTATUS _stdcall VBoxDrvNtNotSupportedStub(PDEVICE_OBJECT pDevObj, PIRP pIrp);
75static NTSTATUS VBoxDrvNtErr2NtStatus(int rc);
76static NTSTATUS VBoxDrvNtGipInit(PSUPDRVDEVEXT pDevExt);
77static void VBoxDrvNtGipTerm(PSUPDRVDEVEXT pDevExt);
78static void _stdcall VBoxDrvNtGipTimer(IN PKDPC pDpc, IN PVOID pvUser, IN PVOID SystemArgument1, IN PVOID SystemArgument2);
79static void _stdcall VBoxDrvNtGipPerCpuDpc(IN PKDPC pDpc, IN PVOID pvUser, IN PVOID SystemArgument1, IN PVOID SystemArgument2);
80
81
82/*******************************************************************************
83* Exported Functions *
84*******************************************************************************/
85__BEGIN_DECLS
86ULONG _stdcall DriverEntry(PDRIVER_OBJECT pDrvObj, PUNICODE_STRING pRegPath);
87__END_DECLS
88
89
90/**
91 * Driver entry point.
92 *
93 * @returns appropriate status code.
94 * @param pDrvObj Pointer to driver object.
95 * @param pRegPath Registry base path.
96 */
97ULONG _stdcall DriverEntry(PDRIVER_OBJECT pDrvObj, PUNICODE_STRING pRegPath)
98{
99 NTSTATUS rc;
100 dprintf(("VBoxDrv::DriverEntry\n"));
101
102 /*
103 * Create device.
104 * (That means creating a device object and a symbolic link so the DOS
105 * subsystems (OS/2, win32, ++) can access the device.)
106 */
107 UNICODE_STRING DevName;
108 RtlInitUnicodeString(&DevName, DEVICE_NAME_NT);
109 PDEVICE_OBJECT pDevObj;
110 rc = IoCreateDevice(pDrvObj, sizeof(SUPDRVDEVEXT), &DevName, FILE_DEVICE_UNKNOWN, 0, FALSE, &pDevObj);
111 if (NT_SUCCESS(rc))
112 {
113 UNICODE_STRING DosName;
114 RtlInitUnicodeString(&DosName, DEVICE_NAME_DOS);
115 rc = IoCreateSymbolicLink(&DosName, &DevName);
116 if (NT_SUCCESS(rc))
117 {
118 /*
119 * Initialize the device extension.
120 */
121 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;
122 memset(pDevExt, 0, sizeof(*pDevExt));
123 int vrc = supdrvInitDevExt(pDevExt);
124 if (!vrc)
125 {
126 /*
127 * Inititalize the GIP.
128 */
129 rc = VBoxDrvNtGipInit(pDevExt);
130 if (NT_SUCCESS(rc))
131 {
132 /*
133 * Setup the driver entry points in pDrvObj.
134 */
135 pDrvObj->DriverUnload = VBoxDrvNtUnload;
136 pDrvObj->MajorFunction[IRP_MJ_CREATE] = VBoxDrvNtCreate;
137 pDrvObj->MajorFunction[IRP_MJ_CLOSE] = VBoxDrvNtClose;
138 pDrvObj->MajorFunction[IRP_MJ_DEVICE_CONTROL] = VBoxDrvNtDeviceControl;
139 pDrvObj->MajorFunction[IRP_MJ_READ] = VBoxDrvNtNotSupportedStub;
140 pDrvObj->MajorFunction[IRP_MJ_WRITE] = VBoxDrvNtNotSupportedStub;
141 /* more? */
142 dprintf(("VBoxDrv::DriverEntry returning STATUS_SUCCESS\n"));
143 return STATUS_SUCCESS;
144 }
145 dprintf(("VBoxDrvNtGipInit failed with rc=%#x!\n", rc));
146
147 supdrvDeleteDevExt(pDevExt);
148 }
149 else
150 {
151 dprintf(("supdrvInitDevExit failed with vrc=%d!\n", vrc));
152 rc = VBoxDrvNtErr2NtStatus(vrc);
153 }
154
155 IoDeleteSymbolicLink(&DosName);
156 }
157 else
158 dprintf(("IoCreateSymbolicLink failed with rc=%#x!\n", rc));
159
160 IoDeleteDevice(pDevObj);
161 }
162 else
163 dprintf(("IoCreateDevice failed with rc=%#x!\n", rc));
164
165 if (NT_SUCCESS(rc))
166 rc = STATUS_INVALID_PARAMETER;
167 dprintf(("VBoxDrv::DriverEntry returning %#x\n", rc));
168 return rc;
169}
170
171
172/**
173 * Unload the driver.
174 *
175 * @param pDrvObj Driver object.
176 */
177void _stdcall VBoxDrvNtUnload(PDRIVER_OBJECT pDrvObj)
178{
179 dprintf(("VBoxDrvNtUnload\n"));
180 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDrvObj->DeviceObject->DeviceExtension;
181
182 /*
183 * We ASSUME that it's not possible to unload a driver with open handles.
184 * Start by deleting the symbolic link
185 */
186 UNICODE_STRING DosName;
187 RtlInitUnicodeString(&DosName, DEVICE_NAME_DOS);
188 NTSTATUS rc = IoDeleteSymbolicLink(&DosName);
189
190 /*
191 * Terminate the GIP page and delete the device extension.
192 */
193 VBoxDrvNtGipTerm(pDevExt);
194 supdrvDeleteDevExt(pDevExt);
195 IoDeleteDevice(pDrvObj->DeviceObject);
196}
197
198
199/**
200 * Create (i.e. Open) file entry point.
201 *
202 * @param pDevObj Device object.
203 * @param pIrp Request packet.
204 */
205NTSTATUS _stdcall VBoxDrvNtCreate(PDEVICE_OBJECT pDevObj, PIRP pIrp)
206{
207 dprintf(("VBoxDrvNtCreate\n"));
208 PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
209 PFILE_OBJECT pFileObj = pStack->FileObject;
210 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;
211
212 /*
213 * We are not remotely similar to a directory...
214 * (But this is possible.)
215 */
216 if (pStack->Parameters.Create.Options & FILE_DIRECTORY_FILE)
217 {
218 pIrp->IoStatus.Status = STATUS_NOT_A_DIRECTORY;
219 pIrp->IoStatus.Information = 0;
220 IoCompleteRequest(pIrp, IO_NO_INCREMENT);
221 return STATUS_NOT_A_DIRECTORY;
222 }
223
224 /*
225 * Call common code for the rest.
226 */
227 pFileObj->FsContext = NULL;
228 PSUPDRVSESSION pSession;
229 int rc = supdrvCreateSession(pDevExt, &pSession);
230 if (!rc)
231 {
232 pSession->Uid = NIL_RTUID;
233 pSession->Gid = NIL_RTGID;
234 pSession->Process = RTProcSelf();
235 pSession->R0Process = RTR0ProcHandleSelf();
236 pFileObj->FsContext = pSession;
237 }
238
239 NTSTATUS rcNt = pIrp->IoStatus.Status = VBoxDrvNtErr2NtStatus(rc);
240 pIrp->IoStatus.Information = 0;
241 IoCompleteRequest(pIrp, IO_NO_INCREMENT);
242
243 return rcNt;
244}
245
246
247/**
248 * Close file entry point.
249 *
250 * @param pDevObj Device object.
251 * @param pIrp Request packet.
252 */
253NTSTATUS _stdcall VBoxDrvNtClose(PDEVICE_OBJECT pDevObj, PIRP pIrp)
254{
255 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;
256 PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
257 PFILE_OBJECT pFileObj = pStack->FileObject;
258 dprintf(("VBoxDrvNtClose: pDevExt=%p pFileObj=%p pSession=%p\n",
259 pDevExt, pFileObj, pFileObj->FsContext));
260 supdrvCloseSession(pDevExt, (PSUPDRVSESSION)pFileObj->FsContext);
261 pFileObj->FsContext = NULL;
262 pIrp->IoStatus.Information = 0;
263 pIrp->IoStatus.Status = STATUS_SUCCESS;
264 IoCompleteRequest(pIrp, IO_NO_INCREMENT);
265
266 return STATUS_SUCCESS;
267}
268
269
270/**
271 * Device I/O Control entry point.
272 *
273 * @param pDevObj Device object.
274 * @param pIrp Request packet.
275 */
276NTSTATUS _stdcall VBoxDrvNtDeviceControl(PDEVICE_OBJECT pDevObj, PIRP pIrp)
277{
278 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;
279 PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
280 PSUPDRVSESSION pSession = (PSUPDRVSESSION)pStack->FileObject->FsContext;
281
282 /*
283 * Deal with the two high-speed IOCtl that takes it's arguments from
284 * the session and iCmd, and only returns a VBox status code.
285 */
286 ULONG ulCmd = pStack->Parameters.DeviceIoControl.IoControlCode;
287 if ( ulCmd == SUP_IOCTL_FAST_DO_RAW_RUN
288 || ulCmd == SUP_IOCTL_FAST_DO_HWACC_RUN
289 || ulCmd == SUP_IOCTL_FAST_DO_NOP)
290 {
291 KIRQL oldIrql;
292 int rc;
293
294 /* Raise the IRQL to DISPATCH_LEVEl to prevent Windows from rescheduling us to another CPU/core. */
295 Assert(KeGetCurrentIrql() <= DISPATCH_LEVEL);
296 KeRaiseIrql(DISPATCH_LEVEL, &oldIrql);
297 rc = supdrvIOCtlFast(ulCmd, pDevExt, pSession);
298 KeLowerIrql(oldIrql);
299
300 /* Complete the I/O request. */
301 NTSTATUS rcNt = pIrp->IoStatus.Status = STATUS_SUCCESS;
302 pIrp->IoStatus.Information = sizeof(rc);
303 __try
304 {
305 *(int *)pIrp->UserBuffer = rc;
306 }
307 __except(EXCEPTION_EXECUTE_HANDLER)
308 {
309 rcNt = pIrp->IoStatus.Status = GetExceptionCode();
310 dprintf(("VBoxSupDrvDeviceContorl: Exception Code %#x\n", rcNt));
311 }
312 IoCompleteRequest(pIrp, IO_NO_INCREMENT);
313 return rcNt;
314 }
315
316 return VBoxDrvNtDeviceControlSlow(pDevExt, pSession, pIrp, pStack);
317}
318
319
320/**
321 * Worker for VBoxDrvNtDeviceControl that takes the slow IOCtl functions.
322 *
323 * @returns NT status code.
324 *
325 * @param pDevObj Device object.
326 * @param pSession The session.
327 * @param pIrp Request packet.
328 * @param pStack The stack location containing the DeviceControl parameters.
329 */
330static int VBoxDrvNtDeviceControlSlow(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PIRP pIrp, PIO_STACK_LOCATION pStack)
331{
332 NTSTATUS rcNt;
333 unsigned cbOut = 0;
334 int rc = 0;
335 dprintf2(("VBoxDrvNtDeviceControlSlow(%p,%p): ioctl=%#x pBuf=%p cbIn=%#x cbOut=%#x pSession=%p\n",
336 pDevExt, pIrp, pStack->Parameters.DeviceIoControl.IoControlCode,
337 pIrp->AssociatedIrp.SystemBuffer, pStack->Parameters.DeviceIoControl.InputBufferLength,
338 pStack->Parameters.DeviceIoControl.OutputBufferLength, pSession));
339
340#ifdef RT_ARCH_AMD64
341 /* Don't allow 32-bit processes to do any I/O controls. */
342 if (!IoIs32bitProcess(pIrp))
343#endif
344 {
345 /* Verify that it's a buffered CTL. */
346 if ((pStack->Parameters.DeviceIoControl.IoControlCode & 0x3) == METHOD_BUFFERED)
347 {
348 /* Verify that the sizes in the request header are correct. */
349 PSUPREQHDR pHdr = (PSUPREQHDR)pIrp->AssociatedIrp.SystemBuffer;
350 if ( pStack->Parameters.DeviceIoControl.InputBufferLength >= sizeof(*pHdr)
351 && pStack->Parameters.DeviceIoControl.InputBufferLength == pHdr->cbIn
352 && pStack->Parameters.DeviceIoControl.OutputBufferLength == pHdr->cbOut)
353 {
354 /*
355 * Do the job.
356 */
357 rc = supdrvIOCtl(pStack->Parameters.DeviceIoControl.IoControlCode, pDevExt, pSession, pHdr);
358 if (!rc)
359 {
360 rcNt = STATUS_SUCCESS;
361 cbOut = pHdr->cbOut;
362 if (cbOut > pStack->Parameters.DeviceIoControl.OutputBufferLength)
363 {
364 cbOut = pStack->Parameters.DeviceIoControl.OutputBufferLength;
365 OSDBGPRINT(("VBoxDrvLinuxIOCtl: too much output! %#x > %#x; uCmd=%#x!\n",
366 pHdr->cbOut, cbOut, pStack->Parameters.DeviceIoControl.IoControlCode));
367 }
368 }
369 else
370 rcNt = STATUS_INVALID_PARAMETER;
371 dprintf2(("VBoxDrvNtDeviceControlSlow: returns %#x cbOut=%d rc=%#x\n", rcNt, cbOut, rc));
372 }
373 else
374 {
375 dprintf(("VBoxDrvNtDeviceControlSlow: Mismatching sizes (%#x) - Hdr=%#lx/%#lx Irp=%#lx/%#lx!\n",
376 pStack->Parameters.DeviceIoControl.IoControlCode,
377 pStack->Parameters.DeviceIoControl.InputBufferLength >= sizeof(*pHdr) ? pHdr->cbIn : 0,
378 pStack->Parameters.DeviceIoControl.InputBufferLength >= sizeof(*pHdr) ? pHdr->cbOut : 0,
379 pStack->Parameters.DeviceIoControl.InputBufferLength,
380 pStack->Parameters.DeviceIoControl.OutputBufferLength));
381 rcNt = STATUS_INVALID_PARAMETER;
382 }
383 }
384 else
385 {
386 dprintf(("VBoxDrvNtDeviceControlSlow: not buffered request (%#x) - not supported\n",
387 pStack->Parameters.DeviceIoControl.IoControlCode));
388 rcNt = STATUS_NOT_SUPPORTED;
389 }
390 }
391#ifdef RT_ARCH_AMD64
392 else
393 {
394 dprintf(("VBoxDrvNtDeviceControlSlow: WOW64 req - not supported\n"));
395 rcNt = STATUS_NOT_SUPPORTED;
396 }
397#endif
398
399 /* complete the request. */
400 pIrp->IoStatus.Status = rcNt;
401 pIrp->IoStatus.Information = cbOut;
402 IoCompleteRequest(pIrp, IO_NO_INCREMENT);
403 return rcNt;
404}
405
406
407/**
408 * Stub function for functions we don't implemented.
409 *
410 * @returns STATUS_NOT_SUPPORTED
411 * @param pDevObj Device object.
412 * @param pIrp IRP.
413 */
414NTSTATUS _stdcall VBoxDrvNtNotSupportedStub(PDEVICE_OBJECT pDevObj, PIRP pIrp)
415{
416 dprintf(("VBoxDrvNtNotSupportedStub\n"));
417 pDevObj = pDevObj;
418
419 pIrp->IoStatus.Information = 0;
420 pIrp->IoStatus.Status = STATUS_NOT_SUPPORTED;
421 IoCompleteRequest(pIrp, IO_NO_INCREMENT);
422
423 return STATUS_NOT_SUPPORTED;
424}
425
426
427/**
428 * Initializes any OS specific object creator fields.
429 */
430void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
431{
432 NOREF(pObj);
433 NOREF(pSession);
434}
435
436
437/**
438 * Checks if the session can access the object.
439 *
440 * @returns true if a decision has been made.
441 * @returns false if the default access policy should be applied.
442 *
443 * @param pObj The object in question.
444 * @param pSession The session wanting to access the object.
445 * @param pszObjName The object name, can be NULL.
446 * @param prc Where to store the result when returning true.
447 */
448bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
449{
450 NOREF(pObj);
451 NOREF(pSession);
452 NOREF(pszObjName);
453 NOREF(prc);
454 return false;
455}
456
457/**
458 * Gets the monotone timestamp (nano seconds).
459 * @returns NanoTS.
460 */
461static inline uint64_t supdrvOSMonotime(void)
462{
463 return (uint64_t)KeQueryInterruptTime() * 100;
464}
465
466
467/**
468 * Initializes the GIP.
469 *
470 * @returns NT status code.
471 * @param pDevExt Instance data. GIP stuff may be updated.
472 */
473static NTSTATUS VBoxDrvNtGipInit(PSUPDRVDEVEXT pDevExt)
474{
475 dprintf2(("VBoxSupDrvTermGip:\n"));
476
477 /*
478 * Try allocate the memory.
479 * Make sure it's below 4GB for 32-bit GC support
480 */
481 NTSTATUS rc;
482 PHYSICAL_ADDRESS Phys;
483 Phys.HighPart = 0;
484 Phys.LowPart = ~0;
485 PSUPGLOBALINFOPAGE pGip = (PSUPGLOBALINFOPAGE)MmAllocateContiguousMemory(PAGE_SIZE, Phys);
486 if (pGip)
487 {
488 if (!((uintptr_t)pGip & (PAGE_SIZE - 1)))
489 {
490 pDevExt->pGipMdl = IoAllocateMdl(pGip, PAGE_SIZE, FALSE, FALSE, NULL);
491 if (pDevExt->pGipMdl)
492 {
493 MmBuildMdlForNonPagedPool(pDevExt->pGipMdl);
494
495 /*
496 * Figure the timer interval and frequency.
497 * It turns out trying 1023Hz doesn't work. So, we'll set the max Hz at 128 for now.
498 */
499 ExSetTimerResolution(156250, TRUE);
500 ULONG ulClockIntervalActual = ExSetTimerResolution(0, FALSE);
501 ULONG ulClockInterval = RT_MAX(ulClockIntervalActual, 78125); /* 1/128 */
502 ULONG ulClockFreq = 10000000 / ulClockInterval;
503 pDevExt->ulGipTimerInterval = ulClockInterval / 10000; /* ms */
504
505 /*
506 * Call common initialization routine.
507 */
508 Phys = MmGetPhysicalAddress(pGip); /* could perhaps use the Mdl, not that it looks much better */
509 supdrvGipInit(pDevExt, pGip, (RTHCPHYS)Phys.QuadPart, supdrvOSMonotime(), ulClockFreq);
510
511 /*
512 * Initialize the timer.
513 */
514 KeInitializeTimerEx(&pDevExt->GipTimer, SynchronizationTimer);
515 KeInitializeDpc(&pDevExt->GipDpc, VBoxDrvNtGipTimer, pDevExt);
516
517 /*
518 * Initialize the DPCs we're using to update the per-cpu GIP data.
519 * (Not sure if we need to be this careful with KeSetTargetProcessorDpc...)
520 */
521 UNICODE_STRING RoutineName;
522 RtlInitUnicodeString(&RoutineName, L"KeSetTargetProcessorDpc");
523 VOID (*pfnKeSetTargetProcessorDpc)(IN PRKDPC, IN CCHAR) = (VOID (*)(IN PRKDPC, IN CCHAR))MmGetSystemRoutineAddress(&RoutineName);
524
525 for (unsigned i = 0; i < RT_ELEMENTS(pDevExt->aGipCpuDpcs); i++)
526 {
527 KeInitializeDpc(&pDevExt->aGipCpuDpcs[i], VBoxDrvNtGipPerCpuDpc, pGip);
528 KeSetImportanceDpc(&pDevExt->aGipCpuDpcs[i], HighImportance);
529 if (pfnKeSetTargetProcessorDpc)
530 pfnKeSetTargetProcessorDpc(&pDevExt->aGipCpuDpcs[i], i);
531 }
532
533 dprintf(("VBoxDrvNtGipInit: ulClockFreq=%ld ulClockInterval=%ld ulClockIntervalActual=%ld Phys=%x%08x\n",
534 ulClockFreq, ulClockInterval, ulClockIntervalActual, Phys.HighPart, Phys.LowPart));
535 return STATUS_SUCCESS;
536 }
537
538 dprintf(("VBoxSupDrvInitGip: IoAllocateMdl failed for %p/PAGE_SIZE\n", pGip));
539 rc = STATUS_NO_MEMORY;
540 }
541 else
542 {
543 dprintf(("VBoxSupDrvInitGip: GIP memory is not page aligned! pGip=%p\n", pGip));
544 rc = STATUS_INVALID_ADDRESS;
545 }
546 MmFreeContiguousMemory(pGip);
547 }
548 else
549 {
550 dprintf(("VBoxSupDrvInitGip: no cont memory.\n"));
551 rc = STATUS_NO_MEMORY;
552 }
553 return rc;
554}
555
556
557/**
558 * Terminates the GIP.
559 *
560 * @returns negative errno.
561 * @param pDevExt Instance data. GIP stuff may be updated.
562 */
563static void VBoxDrvNtGipTerm(PSUPDRVDEVEXT pDevExt)
564{
565 dprintf(("VBoxSupDrvTermGip:\n"));
566 PSUPGLOBALINFOPAGE pGip;
567
568 /*
569 * Cancel the timer and wait on DPCs if it was still pending.
570 */
571 if (KeCancelTimer(&pDevExt->GipTimer))
572 {
573 UNICODE_STRING RoutineName;
574 RtlInitUnicodeString(&RoutineName, L"KeFlushQueuedDpcs");
575 VOID (*pfnKeFlushQueuedDpcs)(VOID) = (VOID (*)(VOID))MmGetSystemRoutineAddress(&RoutineName);
576 if (pfnKeFlushQueuedDpcs)
577 pfnKeFlushQueuedDpcs();
578 }
579
580 /*
581 * Uninitialize the content.
582 */
583 pGip = pDevExt->pGip;
584 pDevExt->pGip = NULL;
585 if (pGip)
586 {
587 supdrvGipTerm(pGip);
588
589 /*
590 * Free the page.
591 */
592 if (pDevExt->pGipMdl)
593 {
594 IoFreeMdl(pDevExt->pGipMdl);
595 pDevExt->pGipMdl = NULL;
596 }
597 MmFreeContiguousMemory(pGip);
598 }
599}
600
601
602/**
603 * Timer callback function.
604 * The pvUser parameter is the pDevExt pointer.
605 */
606static void _stdcall VBoxDrvNtGipTimer(IN PKDPC pDpc, IN PVOID pvUser, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
607{
608 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pvUser;
609 PSUPGLOBALINFOPAGE pGip = pDevExt->pGip;
610 if (pGip)
611 {
612 if (pGip->u32Mode != SUPGIPMODE_ASYNC_TSC)
613 supdrvGipUpdate(pGip, supdrvOSMonotime());
614 else
615 {
616 KIRQL oldIrql;
617
618 /* KeQueryActiveProcessors must be executed at IRQL < DISPATCH_LEVEL */
619 Assert(KeGetCurrentIrql() < DISPATCH_LEVEL);
620 KAFFINITY Mask = KeQueryActiveProcessors();
621
622 /* Raise the IRQL to DISPATCH_LEVEL so we can't be rescheduled to another cpu */
623 KeRaiseIrql(DISPATCH_LEVEL, &oldIrql);
624
625 /*
626 * We cannot do other than assume a 1:1 relationship between the
627 * affinity mask and the process despite the warnings in the docs.
628 * If someone knows a better way to get this done, please let bird know.
629 */
630 unsigned iSelf = KeGetCurrentProcessorNumber();
631
632 for (unsigned i = 0; i < RT_ELEMENTS(pDevExt->aGipCpuDpcs); i++)
633 {
634 if ( i != iSelf
635 && (Mask & RT_BIT_64(i)))
636 KeInsertQueueDpc(&pDevExt->aGipCpuDpcs[i], 0, 0);
637 }
638
639 /* Run the normal update. */
640 supdrvGipUpdate(pGip, supdrvOSMonotime());
641
642 KeLowerIrql(oldIrql);
643 }
644 }
645}
646
647
648/**
649 * Per cpu callback callback function.
650 * The pvUser parameter is the pGip pointer.
651 */
652static void _stdcall VBoxDrvNtGipPerCpuDpc(IN PKDPC pDpc, IN PVOID pvUser, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
653{
654 PSUPGLOBALINFOPAGE pGip = (PSUPGLOBALINFOPAGE)pvUser;
655 supdrvGipUpdatePerCpu(pGip, supdrvOSMonotime(), ASMGetApicId());
656}
657
658
659/**
660 * Maps the GIP into user space.
661 *
662 * @returns negative errno.
663 * @param pDevExt Instance data.
664 */
665int VBOXCALL supdrvOSGipMap(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE *ppGip)
666{
667 dprintf2(("supdrvOSGipMap: ppGip=%p (pDevExt->pGipMdl=%p)\n", ppGip, pDevExt->pGipMdl));
668
669 /*
670 * Map into user space.
671 */
672 int rc = 0;
673 void *pv = NULL;
674 __try
675 {
676 *ppGip = (PSUPGLOBALINFOPAGE)MmMapLockedPagesSpecifyCache(pDevExt->pGipMdl, UserMode, MmCached, NULL, FALSE, NormalPagePriority);
677 }
678 __except(EXCEPTION_EXECUTE_HANDLER)
679 {
680 NTSTATUS rcNt = GetExceptionCode();
681 dprintf(("supdrvOsGipMap: Exception Code %#x\n", rcNt));
682 rc = SUPDRV_ERR_LOCK_FAILED;
683 }
684
685 dprintf2(("supdrvOSGipMap: returns %d, *ppGip=%p\n", rc, *ppGip));
686 return 0;
687}
688
689
690/**
691 * Maps the GIP into user space.
692 *
693 * @returns negative errno.
694 * @param pDevExt Instance data.
695 */
696int VBOXCALL supdrvOSGipUnmap(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip)
697{
698 dprintf2(("supdrvOSGipUnmap: pGip=%p (pGipMdl=%p)\n", pGip, pDevExt->pGipMdl));
699
700 int rc = 0;
701 __try
702 {
703 MmUnmapLockedPages((void *)pGip, pDevExt->pGipMdl);
704 }
705 __except(EXCEPTION_EXECUTE_HANDLER)
706 {
707 NTSTATUS rcNt = GetExceptionCode();
708 dprintf(("supdrvOSGipUnmap: Exception Code %#x\n", rcNt));
709 rc = SUPDRV_ERR_GENERAL_FAILURE;
710 }
711 dprintf2(("supdrvOSGipUnmap: returns %d\n", rc));
712 return rc;
713}
714
715
716/**
717 * Resumes the GIP updating.
718 *
719 * @param pDevExt Instance data.
720 */
721void VBOXCALL supdrvOSGipResume(PSUPDRVDEVEXT pDevExt)
722{
723 dprintf2(("supdrvOSGipResume:\n"));
724 LARGE_INTEGER DueTime;
725 DueTime.QuadPart = -10000; /* 1ms, relative */
726 KeSetTimerEx(&pDevExt->GipTimer, DueTime, pDevExt->ulGipTimerInterval, &pDevExt->GipDpc);
727}
728
729
730/**
731 * Suspends the GIP updating.
732 *
733 * @param pDevExt Instance data.
734 */
735void VBOXCALL supdrvOSGipSuspend(PSUPDRVDEVEXT pDevExt)
736{
737 dprintf2(("supdrvOSGipSuspend:\n"));
738 KeCancelTimer(&pDevExt->GipTimer);
739#ifdef RT_ARCH_AMD64
740 ExSetTimerResolution(0, FALSE);
741#endif
742}
743
744
745/**
746 * Get the current CPU count.
747 * @returns Number of cpus.
748 */
749unsigned VBOXCALL supdrvOSGetCPUCount(void)
750{
751 KAFFINITY Mask = KeQueryActiveProcessors();
752 unsigned cCpus = 0;
753 unsigned iBit;
754 for (iBit = 0; iBit < sizeof(Mask) * 8; iBit++)
755 if (Mask & RT_BIT_64(iBit))
756 cCpus++;
757 if (cCpus == 0) /* paranoia */
758 cCpus = 1;
759 return cCpus;
760}
761
762
763/**
764 * Force async tsc mode (stub).
765 */
766bool VBOXCALL supdrvOSGetForcedAsyncTscMode(void)
767{
768 return false;
769}
770
771
772/**
773 * Converts a supdrv error code to an nt status code.
774 *
775 * @returns corresponding nt status code.
776 * @param rc supdrv error code (SUPDRV_ERR_* defines).
777 */
778static NTSTATUS VBoxDrvNtErr2NtStatus(int rc)
779{
780 switch (rc)
781 {
782 case 0: return STATUS_SUCCESS;
783 case SUPDRV_ERR_GENERAL_FAILURE: return STATUS_NOT_SUPPORTED;
784 case SUPDRV_ERR_INVALID_PARAM: return STATUS_INVALID_PARAMETER;
785 case SUPDRV_ERR_INVALID_MAGIC: return STATUS_UNKNOWN_REVISION;
786 case SUPDRV_ERR_INVALID_HANDLE: return STATUS_INVALID_HANDLE;
787 case SUPDRV_ERR_INVALID_POINTER: return STATUS_INVALID_ADDRESS;
788 case SUPDRV_ERR_LOCK_FAILED: return STATUS_NOT_LOCKED;
789 case SUPDRV_ERR_ALREADY_LOADED: return STATUS_IMAGE_ALREADY_LOADED;
790 case SUPDRV_ERR_PERMISSION_DENIED: return STATUS_ACCESS_DENIED;
791 case SUPDRV_ERR_VERSION_MISMATCH: return STATUS_REVISION_MISMATCH;
792 }
793
794 return STATUS_UNSUCCESSFUL;
795}
796
797
798/** Runtime assert implementation for Native Win32 Ring-0. */
799RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
800{
801 DbgPrint("\n!!Assertion Failed!!\n"
802 "Expression: %s\n"
803 "Location : %s(%d) %s\n",
804 pszExpr, pszFile, uLine, pszFunction);
805}
806
807int VBOXCALL mymemcmp(const void *pv1, const void *pv2, size_t cb)
808{
809 const uint8_t *pb1 = (const uint8_t *)pv1;
810 const uint8_t *pb2 = (const uint8_t *)pv2;
811 for (; cb > 0; cb--, pb1++, pb2++)
812 if (*pb1 != *pb2)
813 return *pb1 - *pb2;
814 return 0;
815}
816
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette