VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/VBoxGuest/VBoxGuestPnP.cpp@ 7392

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

The Giant CDDL Dual-License Header Change.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.1 KB
Line 
1/** @file
2 *
3 * VBoxGuest -- VirtualBox Win32 guest support driver PnP code
4 *
5 * Copyright (C) 2006-2007 innotek GmbH
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// enable backdoor logging
17//#define LOG_ENABLED
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include "VBoxGuestPnP.h"
23#include "Helper.h"
24#include <VBox/err.h>
25
26#include <VBox/VBoxGuestLib.h>
27
28/*******************************************************************************
29* Defined Constants And Macros *
30*******************************************************************************/
31
32
33extern "C"
34{
35static NTSTATUS sendIrpSynchronously(PDEVICE_OBJECT pDevObj, PIRP pIrp, BOOLEAN fStrict);
36static NTSTATUS pnpIrpComplete(PDEVICE_OBJECT pDevObj, PIRP pIrp, PKEVENT event);
37static VOID showDeviceResources(PCM_PARTIAL_RESOURCE_LIST pResourceList);
38}
39
40#ifdef ALLOC_PRAGMA
41#pragma alloc_text (PAGE, VBoxGuestPnP)
42#pragma alloc_text (PAGE, VBoxGuestPower)
43#pragma alloc_text (PAGE, sendIrpSynchronously)
44#pragma alloc_text (PAGE, showDeviceResources)
45#endif
46
47/* reenable logging, this was #undef'ed on iprt/log.h for RING0 */
48#define LOG_ENABLED
49
50/*******************************************************************************
51* Internal Functions *
52*******************************************************************************/
53
54/**
55 * Irp completion routine for PnP Irps we send.
56 *
57 * @param pDevObj Device object.
58 * @param pIrp Request packet.
59 * @param event Semaphore.
60 * @return NT status code
61 */
62static NTSTATUS pnpIrpComplete(PDEVICE_OBJECT pDevObj, PIRP pIrp, PKEVENT event)
63{
64 KeSetEvent(event, 0, FALSE);
65 return STATUS_MORE_PROCESSING_REQUIRED;
66}
67
68/**
69 * Helper to send a PnP IRP and wait until it's done.
70 *
71 * @param pDevObj Device object.
72 * @param pIrp Request packet.
73 * @param fStrict When set, returns an error if the IRP gives an error.
74 * @return NT status code
75 */
76static NTSTATUS sendIrpSynchronously(PDEVICE_OBJECT pDevObj, PIRP pIrp, BOOLEAN fStrict)
77{
78 KEVENT event;
79
80 KeInitializeEvent(&event, SynchronizationEvent, FALSE);
81
82 IoCopyCurrentIrpStackLocationToNext(pIrp);
83 IoSetCompletionRoutine(pIrp, (PIO_COMPLETION_ROUTINE)pnpIrpComplete, &event, TRUE, TRUE, TRUE);
84
85 NTSTATUS rc = IoCallDriver(pDevObj, pIrp);
86
87 if (rc == STATUS_PENDING)
88 {
89 KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
90 rc = pIrp->IoStatus.Status;
91 }
92
93 if (!fStrict
94 && (rc == STATUS_NOT_SUPPORTED || rc == STATUS_INVALID_DEVICE_REQUEST))
95 {
96 rc = STATUS_SUCCESS;
97 }
98
99 dprintf(("VBoxGuest::sendIrpSynchronously: returning 0x%x\n", rc));
100
101 return rc;
102}
103
104
105/**
106 * PnP Request handler.
107 *
108 * @param pDevObj Device object.
109 * @param pIrp Request packet.
110 */
111NTSTATUS VBoxGuestPnP(PDEVICE_OBJECT pDevObj, PIRP pIrp)
112{
113 PVBOXGUESTDEVEXT pDevExt = (PVBOXGUESTDEVEXT)pDevObj->DeviceExtension;
114 PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
115 NTSTATUS rc = STATUS_SUCCESS;
116
117#ifdef LOG_ENABLED
118 static char* fcnname[] =
119 {
120 "IRP_MN_START_DEVICE",
121 "IRP_MN_QUERY_REMOVE_DEVICE",
122 "IRP_MN_REMOVE_DEVICE",
123 "IRP_MN_CANCEL_REMOVE_DEVICE",
124 "IRP_MN_STOP_DEVICE",
125 "IRP_MN_QUERY_STOP_DEVICE",
126 "IRP_MN_CANCEL_STOP_DEVICE",
127 "IRP_MN_QUERY_DEVICE_RELATIONS",
128 "IRP_MN_QUERY_INTERFACE",
129 "IRP_MN_QUERY_CAPABILITIES",
130 "IRP_MN_QUERY_RESOURCES",
131 "IRP_MN_QUERY_RESOURCE_REQUIREMENTS",
132 "IRP_MN_QUERY_DEVICE_TEXT",
133 "IRP_MN_FILTER_RESOURCE_REQUIREMENTS",
134 "",
135 "IRP_MN_READ_CONFIG",
136 "IRP_MN_WRITE_CONFIG",
137 "IRP_MN_EJECT",
138 "IRP_MN_SET_LOCK",
139 "IRP_MN_QUERY_ID",
140 "IRP_MN_QUERY_PNP_DEVICE_STATE",
141 "IRP_MN_QUERY_BUS_INFORMATION",
142 "IRP_MN_DEVICE_USAGE_NOTIFICATION",
143 "IRP_MN_SURPRISE_REMOVAL",
144 };
145 dprintf(("VBoxGuest::VBoxGuestPnp: MinorFunction: %s\n", pStack->MinorFunction < (sizeof(fcnname)/sizeof(fcnname[0])) ? fcnname[pStack->MinorFunction] : "unknown"));
146#endif
147 switch (pStack->MinorFunction)
148 {
149 case IRP_MN_START_DEVICE:
150 {
151 rc = sendIrpSynchronously(pDevExt->nextLowerDriver, pIrp, TRUE);
152
153 if (NT_SUCCESS(rc) && NT_SUCCESS(pIrp->IoStatus.Status))
154 {
155 dprintf(("VBoxGuest::START_DEVICE: pStack->Parameters.StartDevice.AllocatedResources = %p\n", pStack->Parameters.StartDevice.AllocatedResources));
156
157 if (!pStack->Parameters.StartDevice.AllocatedResources)
158 {
159 dprintf(("VBoxGuest::START_DEVICE: no resources, pDevExt = %p, nextLowerDriver = %p!!!\n", pDevExt, pDevExt? pDevExt->nextLowerDriver: NULL));
160 rc = STATUS_UNSUCCESSFUL;
161 }
162 else
163 {
164 showDeviceResources(&pStack->Parameters.StartDevice.AllocatedResources->List[0].PartialResourceList);
165
166 VBoxScanPCIResourceList(pStack->Parameters.StartDevice.AllocatedResourcesTranslated,
167 pDevExt);
168
169 /** @todo cleanup and merging codepath with NT */
170 int rcVBox;
171 rcVBox = VbglInit (pDevExt->startPortAddress, pDevExt->pVMMDevMemory);
172 if (!VBOX_SUCCESS(rcVBox))
173 {
174 dprintf(("VBoxGuest::START_DEVICE: VbglInit failed. rcVBox = %d\n", rcVBox));
175 rc = STATUS_UNSUCCESSFUL;
176 }
177
178 if (NT_SUCCESS(rc))
179 {
180 rcVBox = VbglGRAlloc ((VMMDevRequestHeader **)&pDevExt->irqAckEvents, sizeof (VMMDevEvents), VMMDevReq_AcknowledgeEvents);
181 if (!VBOX_SUCCESS(rc))
182 {
183 dprintf(("VBoxGuest::START_DEVICE: VbglAlloc failed. rcVBox = %d\n", rcVBox));
184 rc = STATUS_UNSUCCESSFUL;
185 }
186 }
187
188 if (NT_SUCCESS(rc))
189 {
190 // Map physical address of VMMDev memory
191 rc = hlpVBoxMapVMMDevMemory(pDevExt);
192 if (!NT_SUCCESS(rc))
193 {
194 dprintf(("VBoxGuest::START_DEVICE: can't map physical memory, rc = %d\n", rc));
195 }
196 }
197
198 if (NT_SUCCESS(rc))
199 {
200 rc = hlpVBoxReportGuestInfo (pDevExt);
201 if (!NT_SUCCESS(rc))
202 {
203 dprintf(("VBoxGuest::START_DEVICE: could not report information to host, rc = %d\n", rc));
204 }
205 }
206
207 if (NT_SUCCESS(rc))
208 {
209 // register DPC and ISR
210 dprintf(("VBoxGuest::VBoxGuestPnp: initializing DPC...\n"));
211 IoInitializeDpcRequest(pDevExt->deviceObject, VBoxGuestDpcHandler);
212
213 rc = IoConnectInterrupt(&pDevExt->interruptObject, // out: interrupt object
214 (PKSERVICE_ROUTINE)VBoxGuestIsrHandler, // ISR
215 pDevExt, // context
216 NULL, // optional spinlock
217 pDevExt->interruptVector, // interrupt vector
218 (KIRQL)pDevExt->interruptLevel, // interrupt level
219 (KIRQL)pDevExt->interruptLevel, // interrupt level
220 pDevExt->interruptMode, // LevelSensitive or Latched
221 TRUE, // shareable interrupt
222 pDevExt->interruptAffinity, // CPU affinity
223 FALSE); // don't save FPU stack
224 if (!NT_SUCCESS(rc))
225 {
226 dprintf(("VBoxGuest::VBoxGuestPnp: could not connect interrupt: rc = 0x%x\n", rc));
227 }
228 }
229 }
230 }
231 if (NT_SUCCESS(rc))
232 {
233 createThreads(pDevExt);
234
235 // initialize the event notification semaphore
236 KeInitializeEvent(&pDevExt->keventNotification, NotificationEvent, FALSE);
237
238 VBoxInitMemBalloon(pDevExt);
239
240 // ready to rumble!
241 dprintf(("VBoxGuest::VBoxGuestPnp: device is ready!\n"));
242 pDevExt->devState = WORKING;
243 } else
244 {
245 dprintf(("VBoxGuest::VBoxGuestPnp: error: rc = 0x%x\n", rc));
246
247 // need to unmap memory in case of errors
248 hlpVBoxUnmapVMMDevMemory (pDevExt);
249 }
250 pIrp->IoStatus.Status = rc;
251 pIrp->IoStatus.Information = 0;
252 IoCompleteRequest(pIrp, IO_NO_INCREMENT);
253 break;
254 }
255
256 case IRP_MN_QUERY_REMOVE_DEVICE:
257 {
258#ifdef VBOX_REBOOT_ON_UNINSTALL
259 /* The device can not be removed without a reboot. */
260 if (pDevExt->devState == WORKING)
261 {
262 pDevExt->devState = PENDINGREMOVE;
263 }
264 pIrp->IoStatus.Status = STATUS_UNSUCCESSFUL;
265 pIrp->IoStatus.Information = 0;
266 IoCompleteRequest(pIrp, IO_NO_INCREMENT);
267 rc = STATUS_UNSUCCESSFUL;
268
269 dprintf(("VBoxGuest::VBoxGuestPnp: refuse with rc = %p\n", pIrp->IoStatus.Status));
270#else
271 pIrp->IoStatus.Status = STATUS_SUCCESS;
272 if (pDevExt->devState == WORKING)
273 {
274 pDevExt->devState = PENDINGREMOVE;
275 }
276 IoSkipCurrentIrpStackLocation(pIrp);
277 rc = IoCallDriver(pDevExt->nextLowerDriver, pIrp);
278#endif /* VBOX_REBOOT_ON_UNINSTALL */
279 break;
280 }
281
282 case IRP_MN_REMOVE_DEVICE:
283 {
284 /* @todo merge Remove and Stop, make a helper for common actions */
285 pIrp->IoStatus.Status = STATUS_SUCCESS;
286
287 unreserveHypervisorMemory(pDevExt);
288
289 if (pDevExt->workerThread)
290 {
291 dprintf(("VBoxGuest::VBoxGuestPnp: waiting for the worker thread to terminate...\n"));
292 pDevExt->stopThread = TRUE;
293 KeSetEvent(&pDevExt->workerThreadRequest, 0, FALSE);
294 KeWaitForSingleObject(pDevExt->workerThread,
295 Executive, KernelMode, FALSE, NULL);
296 dprintf(("VBoxGuest::VBoxGuestPnp: returned from KeWaitForSingleObject for worker thread\n"));
297 }
298
299 if (pDevExt->idleThread)
300 {
301 dprintf(("VBoxGuest::VBoxGuestPnp: waiting for the idle thread to terminate...\n"));
302 pDevExt->stopThread = TRUE;
303 KeWaitForSingleObject(pDevExt->idleThread,
304 Executive, KernelMode, FALSE, NULL);
305 dprintf(("VBoxGuest::VBoxGuestPnp: returned from KeWaitForSingleObject for idle thread\n"));
306 }
307
308 VbglTerminate ();
309
310 // according to MSDN we have to unmap previously mapped memory
311 hlpVBoxUnmapVMMDevMemory (pDevExt);
312
313 if (pDevExt->nextLowerDriver != NULL)
314 {
315 IoDetachDevice(pDevExt->nextLowerDriver);
316 }
317 UNICODE_STRING win32Name;
318 RtlInitUnicodeString(&win32Name, VBOXGUEST_DEVICE_NAME_DOS);
319 IoDeleteSymbolicLink(&win32Name);
320 IoDeleteDevice(pDevObj);
321 pDevExt->devState = REMOVED;
322 IoSkipCurrentIrpStackLocation(pIrp);
323 rc = IoCallDriver(pDevExt->nextLowerDriver, pIrp);
324 break;
325 }
326
327 case IRP_MN_QUERY_STOP_DEVICE:
328 {
329#ifdef VBOX_REBOOT_ON_UNINSTALL
330 dprintf(("VBoxGuest::VBoxGuestPnp: refuse\n"));
331 /* The device can not be stopped without a reboot. */
332 if (pDevExt->devState == WORKING)
333 {
334 pDevExt->devState = PENDINGSTOP;
335 }
336 pIrp->IoStatus.Status = STATUS_UNSUCCESSFUL;
337 IoCompleteRequest(pIrp, IO_NO_INCREMENT);
338 rc = STATUS_UNSUCCESSFUL;
339#else
340 pIrp->IoStatus.Status = STATUS_SUCCESS;
341 if (pDevExt->devState == WORKING)
342 {
343 pDevExt->devState = PENDINGSTOP;
344 }
345 IoSkipCurrentIrpStackLocation(pIrp);
346 rc = IoCallDriver(pDevExt->nextLowerDriver, pIrp);
347#endif /* VBOX_REBOOT_ON_UNINSTALL */
348 break;
349 }
350
351 case IRP_MN_STOP_DEVICE:
352 {
353 pIrp->IoStatus.Status = STATUS_SUCCESS;
354 if (pDevExt->devState == PENDINGSTOP)
355 {
356 VbglTerminate ();
357
358 // according to MSDN we have to unmap previously mapped memory
359 hlpVBoxUnmapVMMDevMemory (pDevExt);
360
361 pDevExt->devState = STOPPED;
362 dprintf(("VBoxGuest::VBoxGuestPnp: device has been disabled\n"));
363 } else
364 {
365 dprintf(("VBoxGuest::VBoxGuestPnp: devState not PENDINGSTOP but %d\n", pDevExt->devState));
366 }
367 IoSkipCurrentIrpStackLocation(pIrp);
368 rc = IoCallDriver(pDevExt->nextLowerDriver, pIrp);
369 break;
370 }
371
372 default:
373 {
374 IoSkipCurrentIrpStackLocation(pIrp);
375 rc = IoCallDriver(pDevExt->nextLowerDriver, pIrp);
376 }
377
378 }
379 return rc;
380}
381
382
383/**
384 * Debug helper to dump a device resource list.
385 *
386 * @param pResourceList list of device resources.
387 */
388static VOID showDeviceResources(PCM_PARTIAL_RESOURCE_LIST pResourceList)
389{
390#ifdef LOG_ENABLED
391 PCM_PARTIAL_RESOURCE_DESCRIPTOR resource = pResourceList->PartialDescriptors;
392 ULONG nres = pResourceList->Count;
393 ULONG i;
394
395 for (i = 0; i < nres; ++i, ++resource)
396 {
397 ULONG type = resource->Type;
398
399 static char* name[] =
400 {
401 "CmResourceTypeNull",
402 "CmResourceTypePort",
403 "CmResourceTypeInterrupt",
404 "CmResourceTypeMemory",
405 "CmResourceTypeDma",
406 "CmResourceTypeDeviceSpecific",
407 "CmResourceTypeBusNumber",
408 "CmResourceTypeDevicePrivate",
409 "CmResourceTypeAssignedResource",
410 "CmResourceTypeSubAllocateFrom",
411 };
412
413 dprintf(("VBoxGuest::showDeviceResources: type %s", type < (sizeof(name)/sizeof(name[0])) ? name[type] : "unknown"));
414
415 switch (type)
416 {
417 case CmResourceTypePort:
418 case CmResourceTypeMemory:
419 dprintf(("VBoxGuest::showDeviceResources: start %8X%8.8lX length %X\n",
420 resource->u.Port.Start.HighPart, resource->u.Port.Start.LowPart,
421 resource->u.Port.Length));
422 break;
423
424 case CmResourceTypeInterrupt:
425 dprintf(("VBoxGuest::showDeviceResources: level %X, vector %X, affinity %X\n",
426 resource->u.Interrupt.Level, resource->u.Interrupt.Vector,
427 resource->u.Interrupt.Affinity));
428 break;
429
430 case CmResourceTypeDma:
431 dprintf(("VBoxGuest::showDeviceResources: channel %d, port %X\n",
432 resource->u.Dma.Channel, resource->u.Dma.Port));
433 break;
434
435 default:
436 dprintf(("\n"));
437 break;
438 }
439 }
440#endif
441}
442
443/**
444 * Handle the power completion event.
445 *
446 * @returns NT status code
447 * @param devObj targetted device object
448 * @param irp IO request packet
449 * @param context context value passed to IoSetCompletionRoutine in VBoxGuestPower
450 */
451NTSTATUS VBoxGuestPowerComplete(IN PDEVICE_OBJECT devObj,
452 IN PIRP irp, IN PVOID context)
453{
454 PIO_STACK_LOCATION irpSp;
455 PVBOXGUESTDEVEXT devExt = (PVBOXGUESTDEVEXT)context;
456
457 ASSERT(devExt);
458 ASSERT(devExt->signature == DEVICE_EXTENSION_SIGNATURE);
459
460 irpSp = IoGetCurrentIrpStackLocation(irp);
461 ASSERT(irpSp->MajorFunction == IRP_MJ_POWER);
462
463 if (NT_SUCCESS(irp->IoStatus.Status))
464 {
465 switch (irpSp->MinorFunction)
466 {
467 case IRP_MN_SET_POWER:
468
469 switch (irpSp->Parameters.Power.Type)
470 {
471 case DevicePowerState:
472 switch (irpSp->Parameters.Power.State.DeviceState)
473 {
474 case PowerDeviceD0:
475 break;
476 }
477 break;
478 }
479 break;
480 }
481 }
482
483 return STATUS_SUCCESS;
484}
485
486
487/**
488 * Handle the Power requests.
489 *
490 * @returns NT status code
491 * @param pDevObj device object
492 * @param pIrp IRP
493 */
494NTSTATUS VBoxGuestPower(PDEVICE_OBJECT pDevObj, PIRP pIrp)
495{
496 PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
497 PVBOXGUESTDEVEXT pDevExt = (PVBOXGUESTDEVEXT)pDevObj->DeviceExtension;
498 POWER_STATE_TYPE powerType;
499 POWER_STATE powerState;
500 POWER_ACTION powerAction;
501
502 dprintf(("VBoxGuest::VBoxGuestPower\n"));
503
504 powerType = pStack->Parameters.Power.Type;
505 powerAction = pStack->Parameters.Power.ShutdownType;
506 powerState = pStack->Parameters.Power.State;
507
508 switch (pStack->MinorFunction)
509 {
510 case IRP_MN_SET_POWER:
511 {
512 dprintf(("VBoxGuest::VBoxGuestPower: IRP_MN_SET_POWER\n"));
513 switch (powerType)
514 {
515 case SystemPowerState:
516 {
517 dprintf(("VBoxGuest::VBoxGuestPower: SystemPowerState\n"));
518 switch (powerAction)
519 {
520 case PowerActionShutdownReset:
521 {
522 dprintf(("VBoxGuest::VBoxGuestPower: power action reset!\n"));
523 /* tell the VMM that we no longer support mouse pointer integration */
524
525 VMMDevReqMouseStatus *req = NULL;
526
527 int rc = VbglGRAlloc ((VMMDevRequestHeader **)&req, sizeof (VMMDevReqMouseStatus), VMMDevReq_SetMouseStatus);
528
529 if (VBOX_SUCCESS(rc))
530 {
531 req->mouseFeatures = 0;
532 req->pointerXPos = 0;
533 req->pointerYPos = 0;
534
535 rc = VbglGRPerform (&req->header);
536
537 if (VBOX_FAILURE(rc) || VBOX_FAILURE(req->header.rc))
538 {
539 dprintf(("VBoxGuest::PowerStateRequest: error communicating new power status to VMMDev."
540 "rc = %d, VMMDev rc = %Vrc\n", rc, req->header.rc));
541 }
542
543 VbglGRFree (&req->header);
544 }
545 break;
546 }
547
548 case PowerActionShutdown:
549 case PowerActionShutdownOff:
550 {
551 dprintf(("VBoxGuest::VBoxGuestPower: power action shutdown!\n"));
552 if (powerState.SystemState >= PowerSystemShutdown)
553 {
554 dprintf(("VBoxGuest::VBoxGuestPower: Telling the VMMDev to close the VM...\n"));
555 VMMDevPowerStateRequest *req = NULL;
556
557 int rc = VbglGRAlloc ((VMMDevRequestHeader **)&req, sizeof (VMMDevPowerStateRequest), VMMDevReq_SetPowerStatus);
558
559 if (VBOX_SUCCESS(rc))
560 {
561 req->powerState = VMMDevPowerState_PowerOff;
562
563 rc = VbglGRPerform (&req->header);
564
565 if (VBOX_FAILURE(rc) || VBOX_FAILURE(req->header.rc))
566 {
567 dprintf(("VBoxGuest::PowerStateRequest: error communicating new power status to VMMDev."
568 "rc = %d, VMMDev rc = %Vrc\n", rc, req->header.rc));
569 }
570
571 VbglGRFree (&req->header);
572 }
573 }
574 break;
575 }
576 }
577 break;
578 }
579 default:
580 break;
581 }
582 break;
583 }
584 default:
585 break;
586 }
587
588 /*
589 * Whether we are completing or relaying this power IRP,
590 * we must call PoStartNextPowerIrp.
591 */
592 PoStartNextPowerIrp(pIrp);
593
594 /*
595 * Send the IRP down the driver stack,
596 * using PoCallDriver (not IoCallDriver, as for non-power irps).
597 */
598 IoCopyCurrentIrpStackLocationToNext(pIrp);
599 IoSetCompletionRoutine(pIrp,
600 VBoxGuestPowerComplete,
601 (PVOID)pDevExt,
602 TRUE,
603 TRUE,
604 TRUE);
605 return PoCallDriver(pDevExt->nextLowerDriver, pIrp);
606}
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