VirtualBox

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

Last change on this file since 38760 was 38760, checked in by vboxsync, 13 years ago

More Windows 8 fixes

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