VirtualBox

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

Last change on this file since 4833 was 4831, checked in by vboxsync, 18 years ago

Removed the old MM code.

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