VirtualBox

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

Last change on this file since 35263 was 34551, checked in by vboxsync, 14 years ago

VBoxGuest-win: superfluous

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

© 2023 Oracle
ContactPrivacy policyTerms of Use