VirtualBox

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

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

Added SUPR0ExecuteCallback. Currently a stub.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 27.4 KB
Line 
1/* $Id: SUPDrv-win.cpp 7206 2008-02-28 16:42:10Z 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 * Executes a callback handler on a specific cpu or all cpus
459 *
460 * @returns IPRT status code.
461 * @param pSession The session.
462 * @param pfnCallback Callback handler
463 * @param pvUser The first user argument.
464 * @param uCpu Cpu id or SUPDRVEXECCALLBACK_CPU_ALL for all cpus
465 */
466int VBOXCALL supdrvOSExecuteCallback(PSUPDRVSESSION pSession, PFNSUPDRVEXECCALLBACK pfnCallback, void *pvUser, unsigned uCpu)
467{
468 NOREF(pSession);
469 NOREF(pfnCallback);
470 NOREF(pvUser);
471 NOREF(uCpu);
472 /** @todo */
473 return VERR_NOT_IMPLEMENTED;
474}
475
476/**
477 * Gets the monotone timestamp (nano seconds).
478 * @returns NanoTS.
479 */
480static inline uint64_t supdrvOSMonotime(void)
481{
482 return (uint64_t)KeQueryInterruptTime() * 100;
483}
484
485
486/**
487 * Initializes the GIP.
488 *
489 * @returns NT status code.
490 * @param pDevExt Instance data. GIP stuff may be updated.
491 */
492static NTSTATUS VBoxDrvNtGipInit(PSUPDRVDEVEXT pDevExt)
493{
494 dprintf2(("VBoxSupDrvTermGip:\n"));
495
496 /*
497 * Try allocate the memory.
498 * Make sure it's below 4GB for 32-bit GC support
499 */
500 NTSTATUS rc;
501 PHYSICAL_ADDRESS Phys;
502 Phys.HighPart = 0;
503 Phys.LowPart = ~0;
504 PSUPGLOBALINFOPAGE pGip = (PSUPGLOBALINFOPAGE)MmAllocateContiguousMemory(PAGE_SIZE, Phys);
505 if (pGip)
506 {
507 if (!((uintptr_t)pGip & (PAGE_SIZE - 1)))
508 {
509 pDevExt->pGipMdl = IoAllocateMdl(pGip, PAGE_SIZE, FALSE, FALSE, NULL);
510 if (pDevExt->pGipMdl)
511 {
512 MmBuildMdlForNonPagedPool(pDevExt->pGipMdl);
513
514 /*
515 * Figure the timer interval and frequency.
516 * It turns out trying 1023Hz doesn't work. So, we'll set the max Hz at 128 for now.
517 */
518 ExSetTimerResolution(156250, TRUE);
519 ULONG ulClockIntervalActual = ExSetTimerResolution(0, FALSE);
520 ULONG ulClockInterval = RT_MAX(ulClockIntervalActual, 78125); /* 1/128 */
521 ULONG ulClockFreq = 10000000 / ulClockInterval;
522 pDevExt->ulGipTimerInterval = ulClockInterval / 10000; /* ms */
523
524 /*
525 * Call common initialization routine.
526 */
527 Phys = MmGetPhysicalAddress(pGip); /* could perhaps use the Mdl, not that it looks much better */
528 supdrvGipInit(pDevExt, pGip, (RTHCPHYS)Phys.QuadPart, supdrvOSMonotime(), ulClockFreq);
529
530 /*
531 * Initialize the timer.
532 */
533 KeInitializeTimerEx(&pDevExt->GipTimer, SynchronizationTimer);
534 KeInitializeDpc(&pDevExt->GipDpc, VBoxDrvNtGipTimer, pDevExt);
535
536 /*
537 * Initialize the DPCs we're using to update the per-cpu GIP data.
538 * (Not sure if we need to be this careful with KeSetTargetProcessorDpc...)
539 */
540 UNICODE_STRING RoutineName;
541 RtlInitUnicodeString(&RoutineName, L"KeSetTargetProcessorDpc");
542 VOID (*pfnKeSetTargetProcessorDpc)(IN PRKDPC, IN CCHAR) = (VOID (*)(IN PRKDPC, IN CCHAR))MmGetSystemRoutineAddress(&RoutineName);
543
544 for (unsigned i = 0; i < RT_ELEMENTS(pDevExt->aGipCpuDpcs); i++)
545 {
546 KeInitializeDpc(&pDevExt->aGipCpuDpcs[i], VBoxDrvNtGipPerCpuDpc, pGip);
547 KeSetImportanceDpc(&pDevExt->aGipCpuDpcs[i], HighImportance);
548 if (pfnKeSetTargetProcessorDpc)
549 pfnKeSetTargetProcessorDpc(&pDevExt->aGipCpuDpcs[i], i);
550 }
551
552 dprintf(("VBoxDrvNtGipInit: ulClockFreq=%ld ulClockInterval=%ld ulClockIntervalActual=%ld Phys=%x%08x\n",
553 ulClockFreq, ulClockInterval, ulClockIntervalActual, Phys.HighPart, Phys.LowPart));
554 return STATUS_SUCCESS;
555 }
556
557 dprintf(("VBoxSupDrvInitGip: IoAllocateMdl failed for %p/PAGE_SIZE\n", pGip));
558 rc = STATUS_NO_MEMORY;
559 }
560 else
561 {
562 dprintf(("VBoxSupDrvInitGip: GIP memory is not page aligned! pGip=%p\n", pGip));
563 rc = STATUS_INVALID_ADDRESS;
564 }
565 MmFreeContiguousMemory(pGip);
566 }
567 else
568 {
569 dprintf(("VBoxSupDrvInitGip: no cont memory.\n"));
570 rc = STATUS_NO_MEMORY;
571 }
572 return rc;
573}
574
575
576/**
577 * Terminates the GIP.
578 *
579 * @returns negative errno.
580 * @param pDevExt Instance data. GIP stuff may be updated.
581 */
582static void VBoxDrvNtGipTerm(PSUPDRVDEVEXT pDevExt)
583{
584 dprintf(("VBoxSupDrvTermGip:\n"));
585 PSUPGLOBALINFOPAGE pGip;
586
587 /*
588 * Cancel the timer and wait on DPCs if it was still pending.
589 */
590 if (KeCancelTimer(&pDevExt->GipTimer))
591 {
592 UNICODE_STRING RoutineName;
593 RtlInitUnicodeString(&RoutineName, L"KeFlushQueuedDpcs");
594 VOID (*pfnKeFlushQueuedDpcs)(VOID) = (VOID (*)(VOID))MmGetSystemRoutineAddress(&RoutineName);
595 if (pfnKeFlushQueuedDpcs)
596 pfnKeFlushQueuedDpcs();
597 }
598
599 /*
600 * Uninitialize the content.
601 */
602 pGip = pDevExt->pGip;
603 pDevExt->pGip = NULL;
604 if (pGip)
605 {
606 supdrvGipTerm(pGip);
607
608 /*
609 * Free the page.
610 */
611 if (pDevExt->pGipMdl)
612 {
613 IoFreeMdl(pDevExt->pGipMdl);
614 pDevExt->pGipMdl = NULL;
615 }
616 MmFreeContiguousMemory(pGip);
617 }
618}
619
620
621/**
622 * Timer callback function.
623 * The pvUser parameter is the pDevExt pointer.
624 */
625static void _stdcall VBoxDrvNtGipTimer(IN PKDPC pDpc, IN PVOID pvUser, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
626{
627 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pvUser;
628 PSUPGLOBALINFOPAGE pGip = pDevExt->pGip;
629 if (pGip)
630 {
631 if (pGip->u32Mode != SUPGIPMODE_ASYNC_TSC)
632 supdrvGipUpdate(pGip, supdrvOSMonotime());
633 else
634 {
635 KIRQL oldIrql;
636
637 Assert(KeGetCurrentIrql() <= DISPATCH_LEVEL);
638 KeRaiseIrql(DISPATCH_LEVEL, &oldIrql);
639
640 /*
641 * We cannot do other than assume a 1:1 relation ship between the
642 * affinity mask and the process despite the warnings in the docs.
643 * If someone knows a better way to get this done, please let bird know.
644 */
645 /** @todo our IRQL is too high for KeQueryActiveProcessors!! */
646 unsigned iSelf = KeGetCurrentProcessorNumber();
647 KAFFINITY Mask = KeQueryActiveProcessors();
648
649 for (unsigned i = 0; i < RT_ELEMENTS(pDevExt->aGipCpuDpcs); i++)
650 {
651 if ( i != iSelf
652 && (Mask & RT_BIT_64(i)))
653 KeInsertQueueDpc(&pDevExt->aGipCpuDpcs[i], 0, 0);
654 }
655
656 /* Run the normal update. */
657 supdrvGipUpdate(pGip, supdrvOSMonotime());
658
659 KeLowerIrql(oldIrql);
660 }
661 }
662}
663
664
665/**
666 * Per cpu callback callback function.
667 * The pvUser parameter is the pGip pointer.
668 */
669static void _stdcall VBoxDrvNtGipPerCpuDpc(IN PKDPC pDpc, IN PVOID pvUser, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
670{
671 PSUPGLOBALINFOPAGE pGip = (PSUPGLOBALINFOPAGE)pvUser;
672 supdrvGipUpdatePerCpu(pGip, supdrvOSMonotime(), ASMGetApicId());
673}
674
675
676/**
677 * Maps the GIP into user space.
678 *
679 * @returns negative errno.
680 * @param pDevExt Instance data.
681 */
682int VBOXCALL supdrvOSGipMap(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE *ppGip)
683{
684 dprintf2(("supdrvOSGipMap: ppGip=%p (pDevExt->pGipMdl=%p)\n", ppGip, pDevExt->pGipMdl));
685
686 /*
687 * Map into user space.
688 */
689 int rc = 0;
690 void *pv = NULL;
691 __try
692 {
693 *ppGip = (PSUPGLOBALINFOPAGE)MmMapLockedPagesSpecifyCache(pDevExt->pGipMdl, UserMode, MmCached, NULL, FALSE, NormalPagePriority);
694 }
695 __except(EXCEPTION_EXECUTE_HANDLER)
696 {
697 NTSTATUS rcNt = GetExceptionCode();
698 dprintf(("supdrvOsGipMap: Exception Code %#x\n", rcNt));
699 rc = SUPDRV_ERR_LOCK_FAILED;
700 }
701
702 dprintf2(("supdrvOSGipMap: returns %d, *ppGip=%p\n", rc, *ppGip));
703 return 0;
704}
705
706
707/**
708 * Maps the GIP into user space.
709 *
710 * @returns negative errno.
711 * @param pDevExt Instance data.
712 */
713int VBOXCALL supdrvOSGipUnmap(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip)
714{
715 dprintf2(("supdrvOSGipUnmap: pGip=%p (pGipMdl=%p)\n", pGip, pDevExt->pGipMdl));
716
717 int rc = 0;
718 __try
719 {
720 MmUnmapLockedPages((void *)pGip, pDevExt->pGipMdl);
721 }
722 __except(EXCEPTION_EXECUTE_HANDLER)
723 {
724 NTSTATUS rcNt = GetExceptionCode();
725 dprintf(("supdrvOSGipUnmap: Exception Code %#x\n", rcNt));
726 rc = SUPDRV_ERR_GENERAL_FAILURE;
727 }
728 dprintf2(("supdrvOSGipUnmap: returns %d\n", rc));
729 return rc;
730}
731
732
733/**
734 * Resumes the GIP updating.
735 *
736 * @param pDevExt Instance data.
737 */
738void VBOXCALL supdrvOSGipResume(PSUPDRVDEVEXT pDevExt)
739{
740 dprintf2(("supdrvOSGipResume:\n"));
741 LARGE_INTEGER DueTime;
742 DueTime.QuadPart = -10000; /* 1ms, relative */
743 KeSetTimerEx(&pDevExt->GipTimer, DueTime, pDevExt->ulGipTimerInterval, &pDevExt->GipDpc);
744}
745
746
747/**
748 * Suspends the GIP updating.
749 *
750 * @param pDevExt Instance data.
751 */
752void VBOXCALL supdrvOSGipSuspend(PSUPDRVDEVEXT pDevExt)
753{
754 dprintf2(("supdrvOSGipSuspend:\n"));
755 KeCancelTimer(&pDevExt->GipTimer);
756#ifdef RT_ARCH_AMD64
757 ExSetTimerResolution(0, FALSE);
758#endif
759}
760
761
762/**
763 * Get the current CPU count.
764 * @returns Number of cpus.
765 */
766unsigned VBOXCALL supdrvOSGetCPUCount(void)
767{
768 KAFFINITY Mask = KeQueryActiveProcessors();
769 unsigned cCpus = 0;
770 unsigned iBit;
771 for (iBit = 0; iBit < sizeof(Mask) * 8; iBit++)
772 if (Mask & RT_BIT_64(iBit))
773 cCpus++;
774 if (cCpus == 0) /* paranoia */
775 cCpus = 1;
776 return cCpus;
777}
778
779
780/**
781 * Force async tsc mode (stub).
782 */
783bool VBOXCALL supdrvOSGetForcedAsyncTscMode(void)
784{
785 return false;
786}
787
788
789/**
790 * Converts a supdrv error code to an nt status code.
791 *
792 * @returns corresponding nt status code.
793 * @param rc supdrv error code (SUPDRV_ERR_* defines).
794 */
795static NTSTATUS VBoxDrvNtErr2NtStatus(int rc)
796{
797 switch (rc)
798 {
799 case 0: return STATUS_SUCCESS;
800 case SUPDRV_ERR_GENERAL_FAILURE: return STATUS_NOT_SUPPORTED;
801 case SUPDRV_ERR_INVALID_PARAM: return STATUS_INVALID_PARAMETER;
802 case SUPDRV_ERR_INVALID_MAGIC: return STATUS_UNKNOWN_REVISION;
803 case SUPDRV_ERR_INVALID_HANDLE: return STATUS_INVALID_HANDLE;
804 case SUPDRV_ERR_INVALID_POINTER: return STATUS_INVALID_ADDRESS;
805 case SUPDRV_ERR_LOCK_FAILED: return STATUS_NOT_LOCKED;
806 case SUPDRV_ERR_ALREADY_LOADED: return STATUS_IMAGE_ALREADY_LOADED;
807 case SUPDRV_ERR_PERMISSION_DENIED: return STATUS_ACCESS_DENIED;
808 case SUPDRV_ERR_VERSION_MISMATCH: return STATUS_REVISION_MISMATCH;
809 }
810
811 return STATUS_UNSUCCESSFUL;
812}
813
814
815/** Runtime assert implementation for Native Win32 Ring-0. */
816RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
817{
818 DbgPrint("\n!!Assertion Failed!!\n"
819 "Expression: %s\n"
820 "Location : %s(%d) %s\n",
821 pszExpr, pszFile, uLine, pszFunction);
822}
823
824int VBOXCALL mymemcmp(const void *pv1, const void *pv2, size_t cb)
825{
826 const uint8_t *pb1 = (const uint8_t *)pv1;
827 const uint8_t *pb2 = (const uint8_t *)pv2;
828 for (; cb > 0; cb--, pb1++, pb2++)
829 if (*pb1 != *pb2)
830 return *pb1 - *pb2;
831 return 0;
832}
833
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