VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/VBoxGuest-win.cpp@ 41642

Last change on this file since 41642 was 41642, checked in by vboxsync, 12 years ago

VBoxGuest: Use the EventSpinlock when setting the mouse callback and made the ISR take down the callback info while owning that spinlock. Cleanups.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 49.8 KB
Line 
1/** @file
2 *
3 * VBoxGuest - Windows specifics.
4 *
5 * Copyright (C) 2010 Oracle Corporation
6 *
7 * This file is part of VirtualBox Open Source Edition (OSE), as
8 * available from http://www.virtualbox.org. This file is free software;
9 * you can redistribute it and/or modify it under the terms of the GNU
10 * General Public License (GPL) as published by the Free Software
11 * Foundation, in version 2 as it comes in the "COPYING" file of the
12 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
13 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
14 */
15
16/*******************************************************************************
17* Header Files *
18*******************************************************************************/
19#define LOG_GROUP LOG_GROUP_SUP_DRV
20#include "VBoxGuest-win.h"
21#include "VBoxGuestInternal.h"
22
23#include <iprt/asm.h>
24
25#include <VBox/log.h>
26#include <VBox/VBoxGuestLib.h>
27
28/*
29 * XP DDK #defines ExFreePool to ExFreePoolWithTag. The latter does not exist
30 * on NT4, so... The same for ExAllocatePool.
31 */
32#ifdef TARGET_NT4
33# undef ExAllocatePool
34# undef ExFreePool
35#endif
36
37/*******************************************************************************
38* Internal Functions *
39*******************************************************************************/
40RT_C_DECLS_BEGIN
41static NTSTATUS vboxguestwinAddDevice(PDRIVER_OBJECT pDrvObj, PDEVICE_OBJECT pDevObj);
42static void vboxguestwinUnload(PDRIVER_OBJECT pDrvObj);
43static NTSTATUS vboxguestwinCreate(PDEVICE_OBJECT pDevObj, PIRP pIrp);
44static NTSTATUS vboxguestwinClose(PDEVICE_OBJECT pDevObj, PIRP pIrp);
45static NTSTATUS vboxguestwinIOCtl(PDEVICE_OBJECT pDevObj, PIRP pIrp);
46static NTSTATUS vboxguestwinInternalIOCtl(PDEVICE_OBJECT pDevObj, PIRP pIrp);
47static NTSTATUS vboxguestwinRegistryReadDWORD(ULONG ulRoot, PCWSTR pwszPath, PWSTR pwszName, PULONG puValue);
48static NTSTATUS vboxguestwinSystemControl(PDEVICE_OBJECT pDevObj, PIRP pIrp);
49static NTSTATUS vboxguestwinShutdown(PDEVICE_OBJECT pDevObj, PIRP pIrp);
50static NTSTATUS vboxguestwinNotSupportedStub(PDEVICE_OBJECT pDevObj, PIRP pIrp);
51#ifdef DEBUG
52static void vboxguestwinDoTests(void);
53#endif
54RT_C_DECLS_END
55
56
57/*******************************************************************************
58* Exported Functions *
59*******************************************************************************/
60RT_C_DECLS_BEGIN
61ULONG DriverEntry(PDRIVER_OBJECT pDrvObj, PUNICODE_STRING pRegPath);
62RT_C_DECLS_END
63
64#ifdef ALLOC_PRAGMA
65#pragma alloc_text (INIT, DriverEntry)
66#pragma alloc_text (PAGE, vboxguestwinAddDevice)
67#pragma alloc_text (PAGE, vboxguestwinUnload)
68#pragma alloc_text (PAGE, vboxguestwinCreate)
69#pragma alloc_text (PAGE, vboxguestwinClose)
70#pragma alloc_text (PAGE, vboxguestwinIOCtl)
71#pragma alloc_text (PAGE, vboxguestwinShutdown)
72#pragma alloc_text (PAGE, vboxguestwinNotSupportedStub)
73#pragma alloc_text (PAGE, vboxguestwinScanPCIResourceList)
74#endif
75
76/** The detected Windows version. */
77winVersion_t g_winVersion;
78
79/**
80 * Driver entry point.
81 *
82 * @returns appropriate status code.
83 * @param pDrvObj Pointer to driver object.
84 * @param pRegPath Registry base path.
85 */
86ULONG DriverEntry(PDRIVER_OBJECT pDrvObj, PUNICODE_STRING pRegPath)
87{
88 NTSTATUS rc = STATUS_SUCCESS;
89
90 Log(("VBoxGuest::DriverEntry. Driver built: %s %s\n", __DATE__, __TIME__));
91
92 ULONG majorVersion;
93 ULONG minorVersion;
94 ULONG buildNumber;
95 BOOLEAN bCheckedBuild = PsGetVersion(&majorVersion, &minorVersion, &buildNumber, NULL);
96 Log(("VBoxGuest::DriverEntry: Running on Windows NT version %d.%d, build %d\n", majorVersion, minorVersion, buildNumber));
97 if (bCheckedBuild)
98 Log(("VBoxGuest::DriverEntry: Running on a Windows checked build (debug)!\n"));
99#ifdef DEBUG
100 vboxguestwinDoTests();
101#endif
102 switch (majorVersion)
103 {
104 case 6: /* Windows Vista or Windows 7 (based on minor ver) */
105 switch (minorVersion)
106 {
107 case 0: /* Note: Also could be Windows 2008 Server! */
108 g_winVersion = WINVISTA;
109 break;
110 case 1: /* Note: Also could be Windows 2008 Server R2! */
111 g_winVersion = WIN7;
112 break;
113 case 2:
114 g_winVersion = WIN8;
115 break;
116 default:
117 Log(("VBoxGuest::DriverEntry: Unknown version of Windows (%u.%u), refusing!\n",
118 majorVersion, minorVersion));
119 rc = STATUS_DRIVER_UNABLE_TO_LOAD;
120 break;
121 }
122 break;
123 case 5:
124 switch (minorVersion)
125 {
126 case 2:
127 g_winVersion = WIN2K3;
128 break;
129 case 1:
130 g_winVersion = WINXP;
131 break;
132 case 0:
133 g_winVersion = WIN2K;
134 break;
135 default:
136 Log(("VBoxGuest::DriverEntry: Unknown version of Windows (%u.%u), refusing!\n",
137 majorVersion, minorVersion));
138 rc = STATUS_DRIVER_UNABLE_TO_LOAD;
139 }
140 break;
141 case 4:
142 g_winVersion = WINNT4;
143 break;
144 default:
145 Log(("VBoxGuest::DriverEntry: At least Windows NT4 required!\n"));
146 rc = STATUS_DRIVER_UNABLE_TO_LOAD;
147 }
148
149 if (NT_SUCCESS(rc))
150 {
151 /*
152 * Setup the driver entry points in pDrvObj.
153 */
154 pDrvObj->DriverUnload = vboxguestwinUnload;
155 pDrvObj->MajorFunction[IRP_MJ_CREATE] = vboxguestwinCreate;
156 pDrvObj->MajorFunction[IRP_MJ_CLOSE] = vboxguestwinClose;
157 pDrvObj->MajorFunction[IRP_MJ_DEVICE_CONTROL] = vboxguestwinIOCtl;
158 pDrvObj->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = vboxguestwinInternalIOCtl;
159 pDrvObj->MajorFunction[IRP_MJ_SHUTDOWN] = vboxguestwinShutdown;
160 pDrvObj->MajorFunction[IRP_MJ_READ] = vboxguestwinNotSupportedStub;
161 pDrvObj->MajorFunction[IRP_MJ_WRITE] = vboxguestwinNotSupportedStub;
162#ifdef TARGET_NT4
163 rc = vboxguestwinnt4CreateDevice(pDrvObj, NULL /* pDevObj */, pRegPath);
164#else
165 pDrvObj->MajorFunction[IRP_MJ_PNP] = vboxguestwinPnP;
166 pDrvObj->MajorFunction[IRP_MJ_POWER] = vboxguestwinPower;
167 pDrvObj->MajorFunction[IRP_MJ_SYSTEM_CONTROL] = vboxguestwinSystemControl;
168 pDrvObj->DriverExtension->AddDevice = (PDRIVER_ADD_DEVICE)vboxguestwinAddDevice;
169#endif
170 }
171
172 Log(("VBoxGuest::DriverEntry returning %#x\n", rc));
173 return rc;
174}
175
176
177#ifndef TARGET_NT4
178/**
179 * Handle request from the Plug & Play subsystem.
180 *
181 * @returns NT status code
182 * @param pDrvObj Driver object
183 * @param pDevObj Device object
184 */
185static NTSTATUS vboxguestwinAddDevice(PDRIVER_OBJECT pDrvObj, PDEVICE_OBJECT pDevObj)
186{
187 NTSTATUS rc;
188 Log(("VBoxGuest::vboxguestwinGuestAddDevice\n"));
189
190 /*
191 * Create device.
192 */
193 PDEVICE_OBJECT pDeviceObject = NULL;
194 PVBOXGUESTDEVEXT pDevExt = NULL;
195 UNICODE_STRING devName;
196 UNICODE_STRING win32Name;
197 RtlInitUnicodeString(&devName, VBOXGUEST_DEVICE_NAME_NT);
198 rc = IoCreateDevice(pDrvObj, sizeof(VBOXGUESTDEVEXT), &devName, FILE_DEVICE_UNKNOWN, 0, FALSE, &pDeviceObject);
199 if (NT_SUCCESS(rc))
200 {
201 /*
202 * Create symbolic link (DOS devices).
203 */
204 RtlInitUnicodeString(&win32Name, VBOXGUEST_DEVICE_NAME_DOS);
205 rc = IoCreateSymbolicLink(&win32Name, &devName);
206 if (NT_SUCCESS(rc))
207 {
208 /*
209 * Setup the device extension.
210 */
211 pDevExt = (PVBOXGUESTDEVEXT)pDeviceObject->DeviceExtension;
212 RtlZeroMemory(pDevExt, sizeof(VBOXGUESTDEVEXT));
213
214 KeInitializeSpinLock(&pDevExt->win.s.MouseEventAccessLock);
215
216 pDevExt->win.s.pDeviceObject = pDeviceObject;
217 pDevExt->win.s.prevDevState = STOPPED;
218 pDevExt->win.s.devState = STOPPED;
219
220 pDevExt->win.s.pNextLowerDriver = IoAttachDeviceToDeviceStack(pDeviceObject, pDevObj);
221 if (pDevExt->win.s.pNextLowerDriver == NULL)
222 {
223 Log(("VBoxGuest::vboxguestwinGuestAddDevice: IoAttachDeviceToDeviceStack did not give a nextLowerDriver!\n"));
224 rc = STATUS_DEVICE_NOT_CONNECTED;
225 }
226 }
227 else
228 Log(("VBoxGuest::vboxguestwinGuestAddDevice: IoCreateSymbolicLink failed with rc=%#x!\n", rc));
229 }
230 else
231 Log(("VBoxGuest::vboxguestwinGuestAddDevice: IoCreateDevice failed with rc=%#x!\n", rc));
232
233 if (NT_SUCCESS(rc))
234 {
235 /*
236 * If we reached this point we're fine with the basic driver setup,
237 * so continue to init our own things.
238 */
239#ifdef VBOX_WITH_GUEST_BUGCHECK_DETECTION
240 vboxguestwinBugCheckCallback(pDevExt); /* Ignore failure! */
241#endif
242 /* VBoxGuestPower is pageable; ensure we are not called at elevated IRQL */
243 pDeviceObject->Flags |= DO_POWER_PAGABLE;
244
245 /* Driver is ready now. */
246 pDeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
247 }
248
249 /* Cleanup on error. */
250 if (NT_ERROR(rc))
251 {
252 if (pDevExt)
253 {
254 if (pDevExt->win.s.pNextLowerDriver)
255 IoDetachDevice(pDevExt->win.s.pNextLowerDriver);
256 }
257 IoDeleteSymbolicLink(&win32Name);
258 if (pDeviceObject)
259 IoDeleteDevice(pDeviceObject);
260 }
261
262 Log(("VBoxGuest::vboxguestwinGuestAddDevice: returning with rc = 0x%x\n", rc));
263 return rc;
264}
265#endif
266
267
268/**
269 * Debug helper to dump a device resource list.
270 *
271 * @param pResourceList list of device resources.
272 */
273static void vboxguestwinShowDeviceResources(PCM_PARTIAL_RESOURCE_LIST pResourceList)
274{
275#ifdef LOG_ENABLED
276 PCM_PARTIAL_RESOURCE_DESCRIPTOR resource = pResourceList->PartialDescriptors;
277 ULONG nres = pResourceList->Count;
278 ULONG i;
279
280 for (i = 0; i < nres; ++i, ++resource)
281 {
282 ULONG uType = resource->Type;
283 static char* aszName[] =
284 {
285 "CmResourceTypeNull",
286 "CmResourceTypePort",
287 "CmResourceTypeInterrupt",
288 "CmResourceTypeMemory",
289 "CmResourceTypeDma",
290 "CmResourceTypeDeviceSpecific",
291 "CmResourceTypeBusNumber",
292 "CmResourceTypeDevicePrivate",
293 "CmResourceTypeAssignedResource",
294 "CmResourceTypeSubAllocateFrom",
295 };
296
297 Log(("VBoxGuest::vboxguestwinShowDeviceResources: Type %s",
298 uType < (sizeof(aszName) / sizeof(aszName[0]))
299 ? aszName[uType] : "Unknown"));
300
301 switch (uType)
302 {
303 case CmResourceTypePort:
304 case CmResourceTypeMemory:
305 Log(("VBoxGuest::vboxguestwinShowDeviceResources: Start %8X%8.8lX length %X\n",
306 resource->u.Port.Start.HighPart, resource->u.Port.Start.LowPart,
307 resource->u.Port.Length));
308 break;
309
310 case CmResourceTypeInterrupt:
311 Log(("VBoxGuest::vboxguestwinShowDeviceResources: Level %X, Vector %X, Affinity %X\n",
312 resource->u.Interrupt.Level, resource->u.Interrupt.Vector,
313 resource->u.Interrupt.Affinity));
314 break;
315
316 case CmResourceTypeDma:
317 Log(("VBoxGuest::vboxguestwinShowDeviceResources: Channel %d, Port %X\n",
318 resource->u.Dma.Channel, resource->u.Dma.Port));
319 break;
320
321 default:
322 Log(("\n"));
323 break;
324 }
325 }
326#endif
327}
328
329
330/**
331 * Global initialisation stuff (PnP + NT4 legacy).
332 *
333 * @param pDevObj Device object.
334 * @param pIrp Request packet.
335 */
336#ifndef TARGET_NT4
337NTSTATUS vboxguestwinInit(PDEVICE_OBJECT pDevObj, PIRP pIrp)
338#else
339NTSTATUS vboxguestwinInit(PDRIVER_OBJECT pDrvObj, PDEVICE_OBJECT pDevObj, PUNICODE_STRING pRegPath)
340#endif
341{
342 PVBOXGUESTDEVEXT pDevExt = (PVBOXGUESTDEVEXT)pDevObj->DeviceExtension;
343#ifndef TARGET_NT4
344 PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
345#endif
346
347 Log(("VBoxGuest::vboxguestwinInit\n"));
348
349 int rc = STATUS_SUCCESS;
350#ifdef TARGET_NT4
351 /*
352 * Let's have a look at what our PCI adapter offers.
353 */
354 Log(("VBoxGuest::vboxguestwinInit: Starting to scan PCI resources of VBoxGuest ...\n"));
355
356 /* Assign the PCI resources. */
357 PCM_RESOURCE_LIST pResourceList = NULL;
358 UNICODE_STRING classNameString;
359 RtlInitUnicodeString(&classNameString, L"VBoxGuestAdapter");
360 rc = HalAssignSlotResources(pRegPath, &classNameString,
361 pDrvObj, pDevObj,
362 PCIBus, pDevExt->win.s.busNumber, pDevExt->win.s.slotNumber,
363 &pResourceList);
364 if (pResourceList && pResourceList->Count > 0)
365 vboxguestwinShowDeviceResources(&pResourceList->List[0].PartialResourceList);
366 if (NT_SUCCESS(rc))
367 rc = vboxguestwinScanPCIResourceList(pResourceList, pDevExt);
368#else
369 if (pStack->Parameters.StartDevice.AllocatedResources->Count > 0)
370 vboxguestwinShowDeviceResources(&pStack->Parameters.StartDevice.AllocatedResources->List[0].PartialResourceList);
371 if (NT_SUCCESS(rc))
372 rc = vboxguestwinScanPCIResourceList(pStack->Parameters.StartDevice.AllocatedResourcesTranslated,
373 pDevExt);
374#endif
375 if (NT_SUCCESS(rc))
376 {
377 /*
378 * Map physical address of VMMDev memory into MMIO region
379 * and init the common device extension bits.
380 */
381 void *pvMMIOBase = NULL;
382 uint32_t cbMMIO = 0;
383 rc = vboxguestwinMapVMMDevMemory(pDevExt,
384 pDevExt->win.s.vmmDevPhysMemoryAddress,
385 pDevExt->win.s.vmmDevPhysMemoryLength,
386 &pvMMIOBase,
387 &cbMMIO);
388 if (NT_SUCCESS(rc))
389 {
390 pDevExt->pVMMDevMemory = (VMMDevMemory *)pvMMIOBase;
391
392 Log(("VBoxGuest::vboxguestwinInit: pvMMIOBase = 0x%p, pDevExt = 0x%p, pDevExt->pVMMDevMemory = 0x%p\n",
393 pvMMIOBase, pDevExt, pDevExt ? pDevExt->pVMMDevMemory : NULL));
394
395 int vrc = VBoxGuestInitDevExt(pDevExt,
396 pDevExt->IOPortBase,
397 pvMMIOBase, cbMMIO,
398 vboxguestwinVersionToOSType(g_winVersion),
399 VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
400 if (RT_FAILURE(vrc))
401 {
402 Log(("VBoxGuest::vboxguestwinInit: Could not init device extension, rc = %Rrc!\n", vrc));
403 rc = STATUS_DEVICE_CONFIGURATION_ERROR;
404 }
405 }
406 else
407 Log(("VBoxGuest::vboxguestwinInit: Could not map physical address of VMMDev, rc = 0x%x!\n", rc));
408 }
409
410 if (NT_SUCCESS(rc))
411 {
412 int vrc = VbglGRAlloc((VMMDevRequestHeader **)&pDevExt->win.s.pPowerStateRequest,
413 sizeof (VMMDevPowerStateRequest), VMMDevReq_SetPowerStatus);
414 if (RT_FAILURE(vrc))
415 {
416 Log(("VBoxGuest::vboxguestwinInit: Alloc for pPowerStateRequest failed, rc = %Rrc\n", vrc));
417 rc = STATUS_UNSUCCESSFUL;
418 }
419 }
420
421 if (NT_SUCCESS(rc))
422 {
423 /*
424 * Register DPC and ISR.
425 */
426 Log(("VBoxGuest::vboxguestwinInit: Initializing DPC/ISR ...\n"));
427
428 IoInitializeDpcRequest(pDevExt->win.s.pDeviceObject, vboxguestwinDpcHandler);
429#ifdef TARGET_NT4
430 ULONG uInterruptVector;
431 KIRQL irqLevel;
432 /* Get an interrupt vector. */
433 /* Only proceed if the device provides an interrupt. */
434 if ( pDevExt->win.s.interruptLevel
435 || pDevExt->win.s.interruptVector)
436 {
437 Log(("VBoxGuest::vboxguestwinInit: Getting interrupt vector (HAL): Bus: %u, IRQL: %u, Vector: %u\n",
438 pDevExt->win.s.busNumber, pDevExt->win.s.interruptLevel, pDevExt->win.s.interruptVector));
439
440 uInterruptVector = HalGetInterruptVector(PCIBus,
441 pDevExt->win.s.busNumber,
442 pDevExt->win.s.interruptLevel,
443 pDevExt->win.s.interruptVector,
444 &irqLevel,
445 &pDevExt->win.s.interruptAffinity);
446 Log(("VBoxGuest::vboxguestwinInit: HalGetInterruptVector returns vector %u\n", uInterruptVector));
447 if (uInterruptVector == 0)
448 Log(("VBoxGuest::vboxguestwinInit: No interrupt vector found!\n"));
449 }
450 else
451 Log(("VBoxGuest::vboxguestwinInit: Device does not provide an interrupt!\n"));
452#endif
453 if (pDevExt->win.s.interruptVector)
454 {
455 Log(("VBoxGuest::vboxguestwinInit: Connecting interrupt ...\n"));
456
457 rc = IoConnectInterrupt(&pDevExt->win.s.pInterruptObject, /* Out: interrupt object. */
458 (PKSERVICE_ROUTINE)vboxguestwinIsrHandler, /* Our ISR handler. */
459 pDevExt, /* Device context. */
460 NULL, /* Optional spinlock. */
461#ifdef TARGET_NT4
462 uInterruptVector, /* Interrupt vector. */
463 irqLevel, /* Interrupt level. */
464 irqLevel, /* Interrupt level. */
465#else
466 pDevExt->win.s.interruptVector, /* Interrupt vector. */
467 (KIRQL)pDevExt->win.s.interruptLevel, /* Interrupt level. */
468 (KIRQL)pDevExt->win.s.interruptLevel, /* Interrupt level. */
469#endif
470 pDevExt->win.s.interruptMode, /* LevelSensitive or Latched. */
471 TRUE, /* Shareable interrupt. */
472 pDevExt->win.s.interruptAffinity, /* CPU affinity. */
473 FALSE); /* Don't save FPU stack. */
474 if (NT_ERROR(rc))
475 Log(("VBoxGuest::vboxguestwinInit: Could not connect interrupt, rc = 0x%x\n", rc));
476 }
477 else
478 Log(("VBoxGuest::vboxguestwinInit: No interrupt vector found!\n"));
479 }
480
481
482#ifdef VBOX_WITH_HGCM
483 Log(("VBoxGuest::vboxguestwinInit: Allocating kernel session data ...\n"));
484 int vrc = VBoxGuestCreateKernelSession(pDevExt, &pDevExt->win.s.pKernelSession);
485 if (RT_FAILURE(vrc))
486 {
487 Log(("VBoxGuest::vboxguestwinInit: Failed to allocated kernel session data! rc = %Rrc\n", rc));
488 rc = STATUS_UNSUCCESSFUL;
489 }
490#endif
491
492 if (RT_SUCCESS(rc))
493 {
494 ULONG ulValue = 0;
495 NTSTATUS s = vboxguestwinRegistryReadDWORD(RTL_REGISTRY_SERVICES, L"VBoxGuest", L"LoggingEnabled",
496 &ulValue);
497 if (NT_SUCCESS(s))
498 {
499 pDevExt->fLoggingEnabled = ulValue >= 0xFF;
500 if (pDevExt->fLoggingEnabled)
501 Log(("Logging to release log enabled (0x%x)", ulValue));
502 }
503
504 /* Ready to rumble! */
505 Log(("VBoxGuest::vboxguestwinInit: Device is ready!\n"));
506 VBOXGUEST_UPDATE_DEVSTATE(pDevExt, WORKING);
507 }
508 else
509 {
510 pDevExt->win.s.pInterruptObject = NULL;
511 }
512
513 Log(("VBoxGuest::vboxguestwinInit: Returned with rc = 0x%x\n", rc));
514 return rc;
515}
516
517
518/**
519 * Cleans up hardware resources.
520 * Do not delete DevExt here.
521 *
522 * @param pDrvObj Driver object.
523 */
524NTSTATUS vboxguestwinCleanup(PDEVICE_OBJECT pDevObj)
525{
526 Log(("VBoxGuest::vboxguestwinCleanup\n"));
527
528 PVBOXGUESTDEVEXT pDevExt = (PVBOXGUESTDEVEXT)pDevObj->DeviceExtension;
529 if (pDevExt)
530 {
531
532#if 0 /* @todo: test & enable cleaning global session data */
533#ifdef VBOX_WITH_HGCM
534 if (pDevExt->win.s.pKernelSession)
535 {
536 VBoxGuestCloseSession(pDevExt, pDevExt->win.s.pKernelSession);
537 pDevExt->win.s.pKernelSession = NULL;
538 }
539#endif
540#endif
541
542 if (pDevExt->win.s.pInterruptObject)
543 {
544 IoDisconnectInterrupt(pDevExt->win.s.pInterruptObject);
545 pDevExt->win.s.pInterruptObject = NULL;
546 }
547
548 /* @todo: cleanup the rest stuff */
549
550
551#ifdef VBOX_WITH_GUEST_BUGCHECK_DETECTION
552 hlpDeregisterBugCheckCallback(pDevExt); /* ignore failure! */
553#endif
554 /* According to MSDN we have to unmap previously mapped memory. */
555 vboxguestwinUnmapVMMDevMemory(pDevExt);
556 }
557 return STATUS_SUCCESS;
558}
559
560
561/**
562 * Unload the driver.
563 *
564 * @param pDrvObj Driver object.
565 */
566static void vboxguestwinUnload(PDRIVER_OBJECT pDrvObj)
567{
568 Log(("VBoxGuest::vboxguestwinGuestUnload\n"));
569#ifdef TARGET_NT4
570 vboxguestwinCleanup(pDrvObj->DeviceObject);
571
572 /* Destroy device extension and clean up everything else. */
573 if (pDrvObj->DeviceObject && pDrvObj->DeviceObject->DeviceExtension)
574 VBoxGuestDeleteDevExt((PVBOXGUESTDEVEXT)pDrvObj->DeviceObject->DeviceExtension);
575
576 /*
577 * I don't think it's possible to unload a driver which processes have
578 * opened, at least we'll blindly assume that here.
579 */
580 UNICODE_STRING win32Name;
581 RtlInitUnicodeString(&win32Name, VBOXGUEST_DEVICE_NAME_DOS);
582 NTSTATUS rc = IoDeleteSymbolicLink(&win32Name);
583
584 IoDeleteDevice(pDrvObj->DeviceObject);
585#else /* TARGET_NT4 */
586 /* On a PnP driver this routine will be called after
587 * IRP_MN_REMOVE_DEVICE (where we already did the cleanup),
588 * so don't do anything here (yet). */
589#endif
590
591 Log(("VBoxGuest::vboxguestwinGuestUnload: returning\n"));
592}
593
594
595/**
596 * Create (i.e. Open) file entry point.
597 *
598 * @param pDevObj Device object.
599 * @param pIrp Request packet.
600 */
601static NTSTATUS vboxguestwinCreate(PDEVICE_OBJECT pDevObj, PIRP pIrp)
602{
603 /** @todo AssertPtrReturn(pIrp); */
604 PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
605 /** @todo AssertPtrReturn(pStack); */
606 PFILE_OBJECT pFileObj = pStack->FileObject;
607 PVBOXGUESTDEVEXT pDevExt = (PVBOXGUESTDEVEXT)pDevObj->DeviceExtension;
608 NTSTATUS rc = STATUS_SUCCESS;
609
610 if (pDevExt->win.s.devState != WORKING)
611 {
612 Log(("VBoxGuest::vboxguestwinGuestCreate: device is not working currently: %d!\n",
613 pDevExt->win.s.devState));
614 rc = STATUS_UNSUCCESSFUL;
615 }
616 else if (pStack->Parameters.Create.Options & FILE_DIRECTORY_FILE)
617 {
618 /*
619 * We are not remotely similar to a directory...
620 * (But this is possible.)
621 */
622 Log(("VBoxGuest::vboxguestwinGuestCreate: Uhm, we're not a directory!\n"));
623 rc = STATUS_NOT_A_DIRECTORY;
624 }
625 else
626 {
627#ifdef VBOX_WITH_HGCM
628 if (pFileObj)
629 {
630 Log(("VBoxGuest::vboxguestwinGuestCreate: File object type = %d\n",
631 pFileObj->Type));
632
633 int vrc;
634 PVBOXGUESTSESSION pSession;
635 if (pFileObj->Type == 5 /* File Object */)
636 {
637 /*
638 * Create a session object if we have a valid file object. This session object
639 * exists for every R3 process.
640 */
641 vrc = VBoxGuestCreateUserSession(pDevExt, &pSession);
642 }
643 else
644 {
645 /* ... otherwise we've been called from R0! */
646 vrc = VBoxGuestCreateKernelSession(pDevExt, &pSession);
647 }
648 if (RT_SUCCESS(vrc))
649 pFileObj->FsContext = pSession;
650 }
651#endif
652 }
653
654 /* Complete the request! */
655 pIrp->IoStatus.Information = 0;
656 pIrp->IoStatus.Status = rc;
657 IoCompleteRequest(pIrp, IO_NO_INCREMENT);
658
659 Log(("VBoxGuest::vboxguestwinGuestCreate: Returning 0x%x\n", rc));
660 return rc;
661}
662
663
664/**
665 * Close file entry point.
666 *
667 * @param pDevObj Device object.
668 * @param pIrp Request packet.
669 */
670static NTSTATUS vboxguestwinClose(PDEVICE_OBJECT pDevObj, PIRP pIrp)
671{
672 PVBOXGUESTDEVEXT pDevExt = (PVBOXGUESTDEVEXT)pDevObj->DeviceExtension;
673 PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
674 PFILE_OBJECT pFileObj = pStack->FileObject;
675
676 Log(("VBoxGuest::vboxguestwinGuestClose: pDevExt=0x%p pFileObj=0x%p FsContext=0x%p\n",
677 pDevExt, pFileObj, pFileObj->FsContext));
678
679#ifdef VBOX_WITH_HGCM
680 /* Close both, R0 and R3 sessions. */
681 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pFileObj->FsContext;
682 if (pSession)
683 VBoxGuestCloseSession(pDevExt, pSession);
684#endif
685
686 pFileObj->FsContext = NULL;
687 pIrp->IoStatus.Information = 0;
688 pIrp->IoStatus.Status = STATUS_SUCCESS;
689 IoCompleteRequest(pIrp, IO_NO_INCREMENT);
690
691 return STATUS_SUCCESS;
692}
693
694
695/**
696 * Device I/O Control entry point.
697 *
698 * @param pDevObj Device object.
699 * @param pIrp Request packet.
700 */
701static NTSTATUS vboxguestwinIOCtl(PDEVICE_OBJECT pDevObj, PIRP pIrp)
702{
703 NTSTATUS Status = STATUS_SUCCESS;
704 PVBOXGUESTDEVEXT pDevExt = (PVBOXGUESTDEVEXT)pDevObj->DeviceExtension;
705 PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
706 unsigned int uCmd = (unsigned int)pStack->Parameters.DeviceIoControl.IoControlCode;
707
708 char *pBuf = (char *)pIrp->AssociatedIrp.SystemBuffer; /* All requests are buffered. */
709 size_t cbData = pStack->Parameters.DeviceIoControl.InputBufferLength;
710 unsigned cbOut = 0;
711
712 /* Do we have a file object associated?*/
713 PFILE_OBJECT pFileObj = pStack->FileObject;
714 PVBOXGUESTSESSION pSession = NULL;
715 if (pFileObj) /* ... then we might have a session object as well! */
716 pSession = (PVBOXGUESTSESSION)pFileObj->FsContext;
717
718 Log(("VBoxGuest::vboxguestwinIOCtl: uCmd=%u, pDevExt=0x%p, pSession=0x%p\n",
719 uCmd, pDevExt, pSession));
720
721 /* We don't have a session associated with the file object? So this seems
722 * to be a kernel call then. */
723 /** @todo r=bird: What on earth is this supposed to be? Each kernel session
724 * shall have its own context of course, no hacks, pleeease. */
725 if (pSession == NULL)
726 {
727 Log(("VBoxGuest::vboxguestwinIOCtl: Using kernel session data ...\n"));
728 pSession = pDevExt->win.s.pKernelSession;
729 }
730
731 /*
732 * First process Windows specific stuff which cannot be handled
733 * by the common code used on all other platforms. In the default case
734 * we then finally handle the common cases.
735 */
736 switch (uCmd)
737 {
738#ifdef VBOX_WITH_VRDP_SESSION_HANDLING
739 case VBOXGUEST_IOCTL_ENABLE_VRDP_SESSION:
740 {
741 LogRel(("VBoxGuest::vboxguestwinIOCtl: ENABLE_VRDP_SESSION: Currently: %sabled\n",
742 pDevExt->fVRDPEnabled? "en": "dis"));
743 if (!pDevExt->fVRDPEnabled)
744 {
745 KUSER_SHARED_DATA *pSharedUserData = (KUSER_SHARED_DATA *)KI_USER_SHARED_DATA;
746
747 pDevExt->fVRDPEnabled = true;
748 LogRel(("VBoxGuest::vboxguestwinIOCtl: ENABLE_VRDP_SESSION: Current active console ID: 0x%08X\n",
749 pSharedUserData->ActiveConsoleId));
750 pDevExt->ulOldActiveConsoleId = pSharedUserData->ActiveConsoleId;
751 pSharedUserData->ActiveConsoleId = 2;
752 }
753 break;
754 }
755
756 case VBOXGUEST_IOCTL_DISABLE_VRDP_SESSION:
757 {
758 LogRel(("VBoxGuest::vboxguestwinIOCtl: DISABLE_VRDP_SESSION: Currently: %sabled\n",
759 pDevExt->fVRDPEnabled? "en": "dis"));
760 if (pDevExt->fVRDPEnabled)
761 {
762 KUSER_SHARED_DATA *pSharedUserData = (KUSER_SHARED_DATA *)KI_USER_SHARED_DATA;
763
764 pDevExt->fVRDPEnabled = false;
765 Log(("VBoxGuest::vboxguestwinIOCtl: DISABLE_VRDP_SESSION: Current active console ID: 0x%08X\n",
766 pSharedUserData->ActiveConsoleId));
767 pSharedUserData->ActiveConsoleId = pDevExt->ulOldActiveConsoleId;
768 pDevExt->ulOldActiveConsoleId = 0;
769 }
770 break;
771 }
772#else
773 /* Add at least one (bogus) fall through case to shut up MSVC! */
774 case 0:
775#endif
776 default:
777 {
778 /*
779 * Process the common IOCtls.
780 */
781 size_t cbDataReturned;
782 int vrc = VBoxGuestCommonIOCtl(uCmd, pDevExt, pSession, pBuf, cbData, &cbDataReturned);
783
784 Log(("VBoxGuest::vboxguestwinGuestDeviceControl: rc=%Rrc, pBuf=0x%p, cbData=%u, cbDataReturned=%u\n",
785 vrc, pBuf, cbData, cbDataReturned));
786
787 if (RT_SUCCESS(vrc))
788 {
789 if (RT_UNLIKELY(cbDataReturned > cbData))
790 {
791 Log(("VBoxGuest::vboxguestwinGuestDeviceControl: Too much output data %u - expected %u!\n", cbDataReturned, cbData));
792 cbDataReturned = cbData;
793 Status = STATUS_BUFFER_TOO_SMALL;
794 }
795 if (cbDataReturned > 0)
796 cbOut = cbDataReturned;
797 }
798 else
799 {
800 if ( vrc == VERR_NOT_SUPPORTED
801 || vrc == VERR_INVALID_PARAMETER)
802 {
803 Status = STATUS_INVALID_PARAMETER;
804 }
805 else
806 Status = STATUS_UNSUCCESSFUL;
807 }
808 break;
809 }
810 }
811
812 pIrp->IoStatus.Status = Status;
813 pIrp->IoStatus.Information = cbOut;
814
815 IoCompleteRequest(pIrp, IO_NO_INCREMENT);
816
817 //Log(("VBoxGuest::vboxguestwinGuestDeviceControl: returned cbOut=%d rc=%#x\n", cbOut, Status));
818 return Status;
819}
820
821/**
822 * Internal Device I/O Control entry point.
823 *
824 * We do not want to allow some IOCTLs to be originated from user mode, this is
825 * why we have a different entry point for internal IOCTLs.
826 *
827 * @param pDevObj Device object.
828 * @param pIrp Request packet.
829 *
830 * @todo r=bird: This is no need for this extra function for the purpose of
831 * securing an IOCTL from user space access. VBoxGuestCommonIOCtl
832 * has a way to do this already, see VBOXGUEST_IOCTL_GETVMMDEVPORT.
833 */
834static NTSTATUS vboxguestwinInternalIOCtl(PDEVICE_OBJECT pDevObj, PIRP pIrp)
835{
836 NTSTATUS Status = STATUS_SUCCESS;
837 PVBOXGUESTDEVEXT pDevExt = (PVBOXGUESTDEVEXT)pDevObj->DeviceExtension;
838 PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
839 unsigned int uCmd = (unsigned int)pStack->Parameters.DeviceIoControl.IoControlCode;
840 bool fProcessed = false;
841 unsigned Info = 0;
842
843 switch (uCmd)
844 {
845 case VBOXGUEST_IOCTL_SET_MOUSE_NOTIFY_CALLBACK:
846 {
847 PVOID pvBuf = pStack->Parameters.Others.Argument1;
848 size_t cbData = (size_t)pStack->Parameters.Others.Argument2;
849 fProcessed = true;
850 if (cbData != sizeof(VBoxGuestMouseSetNotifyCallback))
851 {
852 AssertFailed();
853 Status = STATUS_INVALID_PARAMETER;
854 break;
855 }
856
857 KIRQL OldIrql;
858 VBoxGuestMouseSetNotifyCallback *pInfo = (VBoxGuestMouseSetNotifyCallback*)pvBuf;
859 /* we need a lock here to avoid concurrency with the set event functionality */
860 KeAcquireSpinLock(&pDevExt->win.s.MouseEventAccessLock, &OldIrql);
861 pDevExt->win.s.pfnMouseNotify = pInfo->pfnNotify;
862 pDevExt->win.s.pvMouseNotify = pInfo->pvNotify;
863 KeReleaseSpinLock(&pDevExt->win.s.MouseEventAccessLock, OldIrql);
864
865 Status = STATUS_SUCCESS;
866 break;
867 }
868
869 default:
870 break;
871 }
872
873
874 if (fProcessed)
875 {
876 pIrp->IoStatus.Status = Status;
877 pIrp->IoStatus.Information = Info;
878
879 IoCompleteRequest(pIrp, IO_NO_INCREMENT);
880 return Status;
881 }
882
883 return vboxguestwinIOCtl(pDevObj, pIrp);
884}
885
886
887/**
888 * IRP_MJ_SYSTEM_CONTROL handler.
889 *
890 * @returns NT status code
891 * @param pDevObj Device object.
892 * @param pIrp IRP.
893 */
894NTSTATUS vboxguestwinSystemControl(PDEVICE_OBJECT pDevObj, PIRP pIrp)
895{
896 PVBOXGUESTDEVEXT pDevExt = (PVBOXGUESTDEVEXT)pDevObj->DeviceExtension;
897
898 Log(("VBoxGuest::vboxguestwinGuestSystemControl\n"));
899
900 /* Always pass it on to the next driver. */
901 IoSkipCurrentIrpStackLocation(pIrp);
902
903 return IoCallDriver(pDevExt->win.s.pNextLowerDriver, pIrp);
904}
905
906
907/**
908 * IRP_MJ_SHUTDOWN handler.
909 *
910 * @returns NT status code
911 * @param pDevObj Device object.
912 * @param pIrp IRP.
913 */
914NTSTATUS vboxguestwinShutdown(PDEVICE_OBJECT pDevObj, PIRP pIrp)
915{
916 PVBOXGUESTDEVEXT pDevExt = (PVBOXGUESTDEVEXT)pDevObj->DeviceExtension;
917
918 Log(("VBoxGuest::vboxguestwinGuestShutdown\n"));
919
920 VMMDevPowerStateRequest *pReq = pDevExt->win.s.pPowerStateRequest;
921 if (pReq)
922 {
923 pReq->header.requestType = VMMDevReq_SetPowerStatus;
924 pReq->powerState = VMMDevPowerState_PowerOff;
925
926 int rc = VbglGRPerform(&pReq->header);
927 if (RT_FAILURE(rc))
928 {
929 Log(("VBoxGuest::vboxguestwinGuestShutdown: Error performing request to VMMDev! "
930 "rc = %Rrc\n", rc));
931 }
932 }
933 return STATUS_SUCCESS;
934}
935
936
937/**
938 * Stub function for functions we don't implemented.
939 *
940 * @returns STATUS_NOT_SUPPORTED
941 * @param pDevObj Device object.
942 * @param pIrp IRP.
943 */
944NTSTATUS vboxguestwinNotSupportedStub(PDEVICE_OBJECT pDevObj, PIRP pIrp)
945{
946 Log(("VBoxGuest::vboxguestwinGuestNotSupportedStub\n"));
947
948 pIrp->IoStatus.Information = 0;
949 pIrp->IoStatus.Status = STATUS_NOT_SUPPORTED;
950 IoCompleteRequest(pIrp, IO_NO_INCREMENT);
951
952 return STATUS_NOT_SUPPORTED;
953}
954
955
956/**
957 * DPC handler.
958 *
959 * @param pDPC DPC descriptor.
960 * @param pDevObj Device object.
961 * @param pIrp Interrupt request packet.
962 * @param pContext Context specific pointer.
963 */
964void vboxguestwinDpcHandler(PKDPC pDPC, PDEVICE_OBJECT pDevObj,
965 PIRP pIrp, PVOID pContext)
966{
967 PVBOXGUESTDEVEXT pDevExt = (PVBOXGUESTDEVEXT)pDevObj->DeviceExtension;
968 Log(("VBoxGuest::vboxguestwinGuestDpcHandler: pDevExt=0x%p\n", pDevExt));
969
970 /* test & reset the counter */
971 if (ASMAtomicXchgU32(&pDevExt->u32MousePosChangedSeq, 0))
972 {
973 Assert(KeGetCurrentIrql() == DISPATCH_LEVEL);
974 /* we need a lock here to avoid concurrency with the set event ioctl handler thread,
975 * i.e. to prevent the event from destroyed while we're using it */
976 KeAcquireSpinLockAtDpcLevel(&pDevExt->win.s.MouseEventAccessLock);
977 if (pDevExt->win.s.pfnMouseNotify)
978 {
979 pDevExt->win.s.pfnMouseNotify(pDevExt->win.s.pvMouseNotify);
980 }
981 KeReleaseSpinLockFromDpcLevel(&pDevExt->win.s.MouseEventAccessLock);
982 }
983
984 /* Process the wake-up list we were asked by the scheduling a DPC
985 * in vboxguestwinIsrHandler(). */
986 VBoxGuestWaitDoWakeUps(pDevExt);
987}
988
989
990/**
991 * ISR handler.
992 *
993 * @return BOOLEAN Indicates whether the IRQ came from us (TRUE) or not (FALSE).
994 * @param pInterrupt Interrupt that was triggered.
995 * @param pServiceContext Context specific pointer.
996 */
997BOOLEAN vboxguestwinIsrHandler(PKINTERRUPT pInterrupt, PVOID pServiceContext)
998{
999 PVBOXGUESTDEVEXT pDevExt = (PVBOXGUESTDEVEXT)pServiceContext;
1000 if (pDevExt == NULL)
1001 return FALSE;
1002
1003 /*Log(("VBoxGuest::vboxguestwinGuestIsrHandler: pDevExt = 0x%p, pVMMDevMemory = 0x%p\n",
1004 pDevExt, pDevExt ? pDevExt->pVMMDevMemory : NULL));*/
1005
1006 /* Enter the common ISR routine and do the actual work. */
1007 BOOLEAN fIRQTaken = VBoxGuestCommonISR(pDevExt);
1008
1009 /* If we need to wake up some events we do that in a DPC to make
1010 * sure we're called at the right IRQL. */
1011 if (fIRQTaken)
1012 {
1013 Log(("VBoxGuest::vboxguestwinGuestIsrHandler: IRQ was taken! pInterrupt = 0x%p, pDevExt = 0x%p\n",
1014 pInterrupt, pDevExt));
1015 if (ASMAtomicUoReadU32(&pDevExt->u32MousePosChangedSeq) || !RTListIsEmpty(&pDevExt->WakeUpList))
1016 {
1017 Log(("VBoxGuest::vboxguestwinGuestIsrHandler: Requesting DPC ...\n"));
1018 IoRequestDpc(pDevExt->win.s.pDeviceObject, pDevExt->win.s.pCurrentIrp, NULL);
1019 }
1020 }
1021 return fIRQTaken;
1022}
1023
1024
1025/*
1026 * Overridden routine for mouse polling events.
1027 *
1028 * @param pDevExt Device extension structure.
1029 */
1030void VBoxGuestNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt)
1031{
1032 NOREF(pDevExt);
1033 /* nothing to do here - i.e. since we can not KeSetEvent from ISR level,
1034 * we rely on the pDevExt->u32MousePosChangedSeq to be set to a non-zero value on a mouse event
1035 * and queue the DPC in our ISR routine in that case doing KeSetEvent from the DPC routine */
1036}
1037
1038
1039/**
1040 * Queries (gets) a DWORD value from the registry.
1041 *
1042 * @return NTSTATUS
1043 * @param ulRoot Relative path root. See RTL_REGISTRY_SERVICES or RTL_REGISTRY_ABSOLUTE.
1044 * @param pwszPath Path inside path root.
1045 * @param pwszName Actual value name to look up.
1046 * @param puValue On input this can specify the default value (if RTL_REGISTRY_OPTIONAL is
1047 * not specified in ulRoot), on output this will retrieve the looked up
1048 * registry value if found.
1049 */
1050NTSTATUS vboxguestwinRegistryReadDWORD(ULONG ulRoot, PCWSTR pwszPath, PWSTR pwszName,
1051 PULONG puValue)
1052{
1053 if (!pwszPath || !pwszName || !puValue)
1054 return STATUS_INVALID_PARAMETER;
1055
1056 ULONG ulDefault = *puValue;
1057
1058 RTL_QUERY_REGISTRY_TABLE tblQuery[2];
1059 RtlZeroMemory(tblQuery, sizeof(tblQuery));
1060 /** @todo Add RTL_QUERY_REGISTRY_TYPECHECK! */
1061 tblQuery[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
1062 tblQuery[0].Name = pwszName;
1063 tblQuery[0].EntryContext = puValue;
1064 tblQuery[0].DefaultType = REG_DWORD;
1065 tblQuery[0].DefaultData = &ulDefault;
1066 tblQuery[0].DefaultLength = sizeof(ULONG);
1067
1068 return RtlQueryRegistryValues(ulRoot,
1069 pwszPath,
1070 &tblQuery[0],
1071 NULL /* Context */,
1072 NULL /* Environment */);
1073}
1074
1075
1076/**
1077 * Helper to scan the PCI resource list and remember stuff.
1078 *
1079 * @param pResList Resource list
1080 * @param pDevExt Device extension
1081 */
1082NTSTATUS vboxguestwinScanPCIResourceList(PCM_RESOURCE_LIST pResList, PVBOXGUESTDEVEXT pDevExt)
1083{
1084 /* Enumerate the resource list. */
1085 Log(("VBoxGuest::vboxguestwinScanPCIResourceList: Found %d resources\n",
1086 pResList->List->PartialResourceList.Count));
1087
1088 NTSTATUS rc = STATUS_SUCCESS;
1089 PCM_PARTIAL_RESOURCE_DESCRIPTOR pPartialData = NULL;
1090 ULONG rangeCount = 0;
1091 ULONG cMMIORange = 0;
1092 PVBOXGUESTWINBASEADDRESS pBaseAddress = pDevExt->win.s.pciBaseAddress;
1093 for (ULONG i = 0; i < pResList->List->PartialResourceList.Count; i++)
1094 {
1095 pPartialData = &pResList->List->PartialResourceList.PartialDescriptors[i];
1096 switch (pPartialData->Type)
1097 {
1098 case CmResourceTypePort:
1099 {
1100 /* Overflow protection. */
1101 if (rangeCount < PCI_TYPE0_ADDRESSES)
1102 {
1103 Log(("VBoxGuest::vboxguestwinScanPCIResourceList: I/O range: Base = %08x:%08x, Length = %08x\n",
1104 pPartialData->u.Port.Start.HighPart,
1105 pPartialData->u.Port.Start.LowPart,
1106 pPartialData->u.Port.Length));
1107
1108 /* Save the IO port base. */
1109 /** @todo Not so good. */
1110 pDevExt->IOPortBase = (RTIOPORT)pPartialData->u.Port.Start.LowPart;
1111
1112 /* Save resource information. */
1113 pBaseAddress->RangeStart = pPartialData->u.Port.Start;
1114 pBaseAddress->RangeLength = pPartialData->u.Port.Length;
1115 pBaseAddress->RangeInMemory = FALSE;
1116 pBaseAddress->ResourceMapped = FALSE;
1117
1118 Log(("VBoxGuest::vboxguestwinScanPCIResourceList: I/O range for VMMDev found! Base = %08x:%08x, Length = %08x\n",
1119 pPartialData->u.Port.Start.HighPart,
1120 pPartialData->u.Port.Start.LowPart,
1121 pPartialData->u.Port.Length));
1122
1123 /* Next item ... */
1124 rangeCount++; pBaseAddress++;
1125 }
1126 break;
1127 }
1128
1129 case CmResourceTypeInterrupt:
1130 {
1131 Log(("VBoxGuest::vboxguestwinScanPCIResourceList: Interrupt: Level = %x, Vector = %x, Mode = %x\n",
1132 pPartialData->u.Interrupt.Level,
1133 pPartialData->u.Interrupt.Vector,
1134 pPartialData->Flags));
1135
1136 /* Save information. */
1137 pDevExt->win.s.interruptLevel = pPartialData->u.Interrupt.Level;
1138 pDevExt->win.s.interruptVector = pPartialData->u.Interrupt.Vector;
1139 pDevExt->win.s.interruptAffinity = pPartialData->u.Interrupt.Affinity;
1140
1141 /* Check interrupt mode. */
1142 if (pPartialData->Flags & CM_RESOURCE_INTERRUPT_LATCHED)
1143 {
1144 pDevExt->win.s.interruptMode = Latched;
1145 }
1146 else
1147 {
1148 pDevExt->win.s.interruptMode = LevelSensitive;
1149 }
1150 break;
1151 }
1152
1153 case CmResourceTypeMemory:
1154 {
1155 /* Overflow protection. */
1156 if (rangeCount < PCI_TYPE0_ADDRESSES)
1157 {
1158 Log(("VBoxGuest::vboxguestwinScanPCIResourceList: Memory range: Base = %08x:%08x, Length = %08x\n",
1159 pPartialData->u.Memory.Start.HighPart,
1160 pPartialData->u.Memory.Start.LowPart,
1161 pPartialData->u.Memory.Length));
1162
1163 /* We only care about read/write memory. */
1164 /** @todo Reconsider memory type. */
1165 if ( cMMIORange == 0 /* Only care about the first MMIO range (!!!). */
1166 && (pPartialData->Flags & VBOX_CM_PRE_VISTA_MASK) == CM_RESOURCE_MEMORY_READ_WRITE)
1167 {
1168 /* Save physical MMIO base + length for VMMDev. */
1169 pDevExt->win.s.vmmDevPhysMemoryAddress = pPartialData->u.Memory.Start;
1170 pDevExt->win.s.vmmDevPhysMemoryLength = (ULONG)pPartialData->u.Memory.Length;
1171
1172 /* Save resource information. */
1173 pBaseAddress->RangeStart = pPartialData->u.Memory.Start;
1174 pBaseAddress->RangeLength = pPartialData->u.Memory.Length;
1175 pBaseAddress->RangeInMemory = TRUE;
1176 pBaseAddress->ResourceMapped = FALSE;
1177
1178 Log(("VBoxGuest::vboxguestwinScanPCIResourceList: Memory range for VMMDev found! Base = %08x:%08x, Length = %08x\n",
1179 pPartialData->u.Memory.Start.HighPart,
1180 pPartialData->u.Memory.Start.LowPart,
1181 pPartialData->u.Memory.Length));
1182
1183 /* Next item ... */
1184 rangeCount++; pBaseAddress++; cMMIORange++;
1185 }
1186 else
1187 {
1188 Log(("VBoxGuest::vboxguestwinScanPCIResourceList: Ignoring memory: Flags = %08x\n",
1189 pPartialData->Flags));
1190 }
1191 }
1192 break;
1193 }
1194
1195 default:
1196 {
1197 Log(("VBoxGuest::vboxguestwinScanPCIResourceList: Unhandled resource found, type = %d\n", pPartialData->Type));
1198 break;
1199 }
1200 }
1201 }
1202
1203 /* Memorize the number of resources found. */
1204 pDevExt->win.s.pciAddressCount = rangeCount;
1205 return rc;
1206}
1207
1208
1209/**
1210 * Maps the I/O space from VMMDev to virtual kernel address space.
1211 *
1212 * @return NTSTATUS
1213 *
1214 * @param pDevExt The device extension.
1215 * @param physicalAdr Physical address to map.
1216 * @param ulLength Length (in bytes) to map.
1217 * @param ppvMMIOBase Pointer of mapped I/O base.
1218 * @param pcbMMIO Length of mapped I/O base.
1219 */
1220NTSTATUS vboxguestwinMapVMMDevMemory(PVBOXGUESTDEVEXT pDevExt, PHYSICAL_ADDRESS physicalAdr, ULONG ulLength,
1221 void **ppvMMIOBase, uint32_t *pcbMMIO)
1222{
1223 AssertPtrReturn(pDevExt, VERR_INVALID_POINTER);
1224 AssertPtrReturn(ppvMMIOBase, VERR_INVALID_POINTER);
1225 /* pcbMMIO is optional. */
1226
1227 NTSTATUS rc = STATUS_SUCCESS;
1228 if (physicalAdr.LowPart > 0) /* We're mapping below 4GB. */
1229 {
1230 VMMDevMemory *pVMMDevMemory = (VMMDevMemory *)MmMapIoSpace(physicalAdr, ulLength, MmNonCached);
1231 Log(("VBoxGuest::vboxguestwinMapVMMDevMemory: pVMMDevMemory = 0x%x\n", pVMMDevMemory));
1232 if (pVMMDevMemory)
1233 {
1234 Log(("VBoxGuest::vboxguestwinMapVMMDevMemory: VMMDevMemory: Version = 0x%x, Size = %d\n",
1235 pVMMDevMemory->u32Version, pVMMDevMemory->u32Size));
1236
1237 /* Check version of the structure; do we have the right memory version? */
1238 if (pVMMDevMemory->u32Version != VMMDEV_MEMORY_VERSION)
1239 {
1240 Log(("VBoxGuest::vboxguestwinMapVMMDevMemory: Wrong version (%u), refusing operation!\n",
1241 pVMMDevMemory->u32Version));
1242
1243 /* Not our version, refuse operation and unmap the memory. */
1244 vboxguestwinUnmapVMMDevMemory(pDevExt);
1245 rc = STATUS_UNSUCCESSFUL;
1246 }
1247 else
1248 {
1249 /* Save results. */
1250 *ppvMMIOBase = pVMMDevMemory;
1251 if (pcbMMIO) /* Optional. */
1252 *pcbMMIO = pVMMDevMemory->u32Size;
1253
1254 Log(("VBoxGuest::vboxguestwinMapVMMDevMemory: VMMDevMemory found and mapped! pvMMIOBase = 0x%p\n",
1255 *ppvMMIOBase));
1256 }
1257 }
1258 else
1259 rc = STATUS_UNSUCCESSFUL;
1260 }
1261 return rc;
1262}
1263
1264
1265/**
1266 * Unmaps the VMMDev I/O range from kernel space.
1267 *
1268 * @param pDevExt The device extension.
1269 */
1270void vboxguestwinUnmapVMMDevMemory(PVBOXGUESTDEVEXT pDevExt)
1271{
1272 Log(("VBoxGuest::vboxguestwinUnmapVMMDevMemory: pVMMDevMemory = 0x%x\n", pDevExt->pVMMDevMemory));
1273 if (pDevExt->pVMMDevMemory)
1274 {
1275 MmUnmapIoSpace((void*)pDevExt->pVMMDevMemory, pDevExt->win.s.vmmDevPhysMemoryLength);
1276 pDevExt->pVMMDevMemory = NULL;
1277 }
1278
1279 pDevExt->win.s.vmmDevPhysMemoryAddress.QuadPart = 0;
1280 pDevExt->win.s.vmmDevPhysMemoryLength = 0;
1281}
1282
1283
1284VBOXOSTYPE vboxguestwinVersionToOSType(winVersion_t winVer)
1285{
1286 VBOXOSTYPE enmOsType;
1287 switch (winVer)
1288 {
1289 case WINNT4:
1290 enmOsType = VBOXOSTYPE_WinNT4;
1291 break;
1292
1293 case WIN2K:
1294 enmOsType = VBOXOSTYPE_Win2k;
1295 break;
1296
1297 case WINXP:
1298#if ARCH_BITS == 64
1299 enmOsType = VBOXOSTYPE_WinXP_x64;
1300#else
1301 enmOsType = VBOXOSTYPE_WinXP;
1302#endif
1303 break;
1304
1305 case WIN2K3:
1306#if ARCH_BITS == 64
1307 enmOsType = VBOXOSTYPE_Win2k3_x64;
1308#else
1309 enmOsType = VBOXOSTYPE_Win2k3;
1310#endif
1311 break;
1312
1313 case WINVISTA:
1314#if ARCH_BITS == 64
1315 enmOsType = VBOXOSTYPE_WinVista_x64;
1316#else
1317 enmOsType = VBOXOSTYPE_WinVista;
1318#endif
1319 break;
1320
1321 case WIN7:
1322#if ARCH_BITS == 64
1323 enmOsType = VBOXOSTYPE_Win7_x64;
1324#else
1325 enmOsType = VBOXOSTYPE_Win7;
1326#endif
1327 break;
1328
1329 case WIN8:
1330#if ARCH_BITS == 64
1331 enmOsType = VBOXOSTYPE_Win8_x64;
1332#else
1333 enmOsType = VBOXOSTYPE_Win8;
1334#endif
1335 break;
1336
1337 default:
1338 /* We don't know, therefore NT family. */
1339 enmOsType = VBOXOSTYPE_WinNT;
1340 break;
1341 }
1342 return enmOsType;
1343}
1344
1345#ifdef DEBUG
1346
1347/**
1348 * A quick implementation of AtomicTestAndClear for uint32_t and multiple bits.
1349 */
1350static uint32_t vboxugestwinAtomicBitsTestAndClear(void *pu32Bits, uint32_t u32Mask)
1351{
1352 AssertPtrReturn(pu32Bits, 0);
1353 LogFlowFunc(("*pu32Bits=0x%x, u32Mask=0x%x\n", *(long *)pu32Bits,
1354 u32Mask));
1355 uint32_t u32Result = 0;
1356 uint32_t u32WorkingMask = u32Mask;
1357 int iBitOffset = ASMBitFirstSetU32 (u32WorkingMask);
1358
1359 while (iBitOffset > 0)
1360 {
1361 bool fSet = ASMAtomicBitTestAndClear(pu32Bits, iBitOffset - 1);
1362 if (fSet)
1363 u32Result |= 1 << (iBitOffset - 1);
1364 u32WorkingMask &= ~(1 << (iBitOffset - 1));
1365 iBitOffset = ASMBitFirstSetU32 (u32WorkingMask);
1366 }
1367 LogFlowFunc(("Returning 0x%x\n", u32Result));
1368 return u32Result;
1369}
1370
1371
1372static void vboxguestwinTestAtomicTestAndClearBitsU32(uint32_t u32Mask, uint32_t u32Bits,
1373 uint32_t u32Exp)
1374{
1375 ULONG u32Bits2 = u32Bits;
1376 uint32_t u32Result = vboxugestwinAtomicBitsTestAndClear(&u32Bits2, u32Mask);
1377 if ( u32Result != u32Exp
1378 || (u32Bits2 & u32Mask)
1379 || (u32Bits2 & u32Result)
1380 || ((u32Bits2 | u32Result) != u32Bits)
1381 )
1382 AssertLogRelMsgFailed(("%s: TEST FAILED: u32Mask=0x%x, u32Bits (before)=0x%x, u32Bits (after)=0x%x, u32Result=0x%x, u32Exp=ox%x\n",
1383 __PRETTY_FUNCTION__, u32Mask, u32Bits, u32Bits2,
1384 u32Result));
1385}
1386
1387
1388static void vboxguestwinDoTests()
1389{
1390 vboxguestwinTestAtomicTestAndClearBitsU32(0x00, 0x23, 0);
1391 vboxguestwinTestAtomicTestAndClearBitsU32(0x11, 0, 0);
1392 vboxguestwinTestAtomicTestAndClearBitsU32(0x11, 0x22, 0);
1393 vboxguestwinTestAtomicTestAndClearBitsU32(0x11, 0x23, 0x1);
1394 vboxguestwinTestAtomicTestAndClearBitsU32(0x11, 0x32, 0x10);
1395 vboxguestwinTestAtomicTestAndClearBitsU32(0x22, 0x23, 0x22);
1396}
1397
1398#endif /* DEBUG */
1399
Note: See TracBrowser for help on using the repository browser.

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