VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/VBoxGuest/NTLegacy.cpp@ 17480

Last change on this file since 17480 was 17163, checked in by vboxsync, 16 years ago

crOpenGL: disconnect hgcm clients when process owning file handle is terminated (public bug #3243)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.0 KB
Line 
1/** @file
2 *
3 * VBoxGuest -- VirtualBox Win32 guest support driver
4 *
5 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
16 * Clara, CA 95054 USA or visit http://www.sun.com if you need
17 * additional information or have any questions.
18 */
19
20// enable backdoor logging
21//#define LOG_ENABLED
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#include "NTLegacy.h"
27#include "Helper.h"
28
29#include <VBox/VBoxGuestLib.h>
30
31/*******************************************************************************
32* Defined Constants And Macros *
33*******************************************************************************/
34
35
36/*******************************************************************************
37* Internal Functions *
38*******************************************************************************/
39extern "C"
40{
41static NTSTATUS findPCIDevice(PULONG pBusNumber, PPCI_SLOT_NUMBER pSlotNumber);
42static void freeDeviceResources(PDRIVER_OBJECT pDrvObj, PDEVICE_OBJECT pDevObj);
43}
44
45#ifdef ALLOC_PRAGMA
46#pragma alloc_text (INIT, ntCreateDevice)
47#pragma alloc_text (INIT, findPCIDevice)
48#pragma alloc_text (INIT, freeDeviceResources)
49#endif
50
51/**
52 * Helper function to create the device object
53 *
54 * @returns NT status code
55 * @param
56 */
57NTSTATUS ntCreateDevice(PDRIVER_OBJECT pDrvObj, PDEVICE_OBJECT pDevObj, PUNICODE_STRING pRegPath)
58{
59 ULONG busNumber, slotNumber;
60// ULONG i;
61 NTSTATUS rc = STATUS_SUCCESS;
62
63 dprintf(("VBoxGuest::ntCreateDevice: entered\n"));
64
65 // find our virtual PCI device
66 rc = findPCIDevice(&busNumber, (PCI_SLOT_NUMBER*)&slotNumber);
67 if (!NT_SUCCESS(rc))
68 {
69 dprintf(("VBoxGuest::createDevice: device not found, returning\n"));
70 return rc;
71 }
72
73 /*
74 * Create device.
75 */
76 PDEVICE_OBJECT deviceObject = NULL;
77 UNICODE_STRING DevName;
78 RtlInitUnicodeString(&DevName, VBOXGUEST_DEVICE_NAME_NT);
79 rc = IoCreateDevice(pDrvObj, sizeof(VBOXGUESTDEVEXT), &DevName, FILE_DEVICE_UNKNOWN, 0, FALSE, &deviceObject);
80 if (!NT_SUCCESS(rc))
81 {
82 dprintf(("VBoxGuest::ntCreateDevice: IoCreateDevice failed with rc=%#x!\n", rc));
83 return rc;
84 }
85 dprintf(("VBoxGuest::ntCreateDevice: device created\n"));
86 UNICODE_STRING DosName;
87 RtlInitUnicodeString(&DosName, VBOXGUEST_DEVICE_NAME_DOS);
88 rc = IoCreateSymbolicLink(&DosName, &DevName);
89 if (!NT_SUCCESS(rc))
90 {
91 dprintf(("VBoxGuest::ntCreateDevice: IoCreateSymbolicLink failed with rc=%#x!\n", rc));
92 IoDeleteDevice(deviceObject);
93 return rc;
94 }
95 dprintf(("VBoxGuest::ntCreateDevice: symlink created\n"));
96
97 /*
98 * Setup the device extension.
99 */
100 PVBOXGUESTDEVEXT pDevExt = (PVBOXGUESTDEVEXT)deviceObject->DeviceExtension;
101 RtlZeroMemory(pDevExt, sizeof(VBOXGUESTDEVEXT));
102
103 if (pDevObj)
104 {
105 pDevExt->nextLowerDriver = IoAttachDeviceToDeviceStack(deviceObject, pDevObj);
106 if (pDevExt->nextLowerDriver == NULL)
107 {
108 dprintf(("VBoxGuest::ntCreateDevice: IoAttachDeviceToDeviceStack did not give a nextLowerDrive\n"));
109 IoDeleteSymbolicLink(&DosName);
110 IoDeleteDevice(deviceObject);
111 return STATUS_NO_SUCH_DEVICE;
112 }
113
114#ifdef VBOX_WITH_HGCM
115 int rc2 = RTSpinlockCreate(&pDevExt->SessionSpinlock);
116 if (RT_FAILURE(rc2))
117 {
118 dprintf(("VBoxGuest::ntCreateDevice: RTSpinlockCreate failed\n"));
119 IoDetachDevice(pDevExt->nextLowerDriver);
120 IoDeleteSymbolicLink(&DosName);
121 IoDeleteDevice(deviceObject);
122 return STATUS_DRIVER_UNABLE_TO_LOAD;
123 }
124#endif
125 }
126 // store a reference to ourself
127 pDevExt->deviceObject = deviceObject;
128 // store bus and slot number we've queried before
129 pDevExt->busNumber = busNumber;
130 pDevExt->slotNumber = slotNumber;
131
132 //
133 // let's have a look at what our PCI adapter offers
134 //
135 dprintf(("VBoxGuest::ntCreateDevice: starting to scan PCI resources of VBoxGuest\n"));
136 // assign the PCI resources
137 PCM_RESOURCE_LIST resourceList;
138 UNICODE_STRING classNameString;
139 RtlInitUnicodeString(&classNameString, L"VBoxGuestAdapter");
140 rc = HalAssignSlotResources(pRegPath, &classNameString,
141 pDrvObj, pDevObj,
142 PCIBus, busNumber, slotNumber,
143 &resourceList);
144 if (!NT_SUCCESS(rc))
145 {
146 dprintf(("VBoxGuest::ntCreateDevice: HalAssignSlotResources failed with rc=%#x!\n", rc));
147 freeDeviceResources(pDrvObj, pDevObj);
148 return rc;
149 }
150
151 rc = VBoxScanPCIResourceList(resourceList, pDevExt);
152
153 rc = VbglInit (pDevExt->startPortAddress, pDevExt->pVMMDevMemory);
154 if (!RT_SUCCESS(rc))
155 {
156 dprintf(("VBoxGuest::START_DEVICE: VbglInit failed. rc = %d\n", rc));
157 }
158
159
160 rc = VbglGRAlloc ((VMMDevRequestHeader **)&pDevExt->irqAckEvents, sizeof (VMMDevEvents), VMMDevReq_AcknowledgeEvents);
161 if (!RT_SUCCESS(rc))
162 {
163 dprintf(("VBoxGuest::START_DEVICE: VbglAlloc failed for irqAckEvents. rc = %d\n", rc));
164 }
165 rc = VbglGRAlloc ((VMMDevRequestHeader **)&pDevExt->powerStateRequest, sizeof (VMMDevPowerStateRequest), VMMDevReq_SetPowerStatus);
166 if (!RT_SUCCESS(rc))
167 {
168 dprintf(("VBoxGuest::START_DEVICE: VbglAlloc failed for powerStateRequest. rc = %d\n", rc));
169 }
170
171#if 0
172 //
173 // now proceed to the busmaster DMA stuff
174 //
175
176 DEVICE_DESCRIPTION deviceDescription;
177 ULONG numberOfMapRegisters;
178 deviceDescription.Version = DEVICE_DESCRIPTION_VERSION;
179 deviceDescription.Master = TRUE;
180 deviceDescription.ScatterGather = TRUE;
181 deviceDescription.BusNumber = pDevExt->busNumber;
182 deviceDescription.InterfaceType = PCIBus;
183 deviceDescription.MaximumLength = MAXIMUM_TRANSFER_LENGTH;
184 pDevExt->adapterObject = HalGetAdapter(&deviceDescription, &numberOfMapRegisters);
185 if (pDevExt->adapterObject == NULL)
186 {
187 dprintf(("VBoxGuest::ntCreateDevice: HalGetAdapter failed!\n"));
188 freeDeviceResources(pDrvObj, pDevObj);
189 return rc;
190 }
191
192 // @todo allocate S/G buffer
193#endif
194
195
196 //
197 // it's time to map the I/O and memory spaces
198 //
199
200 // Map physical address of VMMDev memory
201 rc = hlpVBoxMapVMMDevMemory (pDevExt);
202 if (!NT_SUCCESS(rc))
203 {
204 dprintf(("VBoxGuest::ntCreateDevice: Unable to map VMMDev Memory, rc=%#x!\n", rc));
205 freeDeviceResources(pDrvObj, pDevObj);
206 return rc;
207 }
208
209 //
210 // now we need an ISR and DPC
211 //
212
213 // register DPC routine
214 dprintf(("VBoxGuest::ntCreateDevice: initializing DPC...\n"));
215 IoInitializeDpcRequest(pDevExt->deviceObject, VBoxGuestDpcHandler);
216 // get an interrupt vector
217 ULONG vector;
218 KIRQL irql;
219 KAFFINITY affinity;
220 // only proceed if the device provides an interrupt
221 if (pDevExt->interruptLevel || pDevExt->interruptVector)
222 {
223 vector = HalGetInterruptVector(PCIBus,
224 pDevExt->busNumber,
225 pDevExt->interruptLevel,
226 pDevExt->interruptVector,
227 &irql,
228 &affinity);
229 dprintf(("VBoxGuest::ntCreateDevice: HalGetInterruptVector returns vector %u\n", vector));
230 rc = IoConnectInterrupt(&pDevExt->interruptObject, // out: interrupt object
231 (PKSERVICE_ROUTINE)VBoxGuestIsrHandler, // ISR
232 pDevExt, // context
233 NULL, // optional spinlock
234 vector, // interrupt vector
235 irql, // interrupt level
236 irql, // interrupt level
237 pDevExt->interruptMode, // LevelSensitive or Latched
238 TRUE, // shareable interrupt
239 affinity, // CPU affinity
240 FALSE); // don't save FPU stack
241 if (!NT_SUCCESS(rc))
242 {
243 dprintf(("VBoxGuest::ntCreateDevice: Unable to connect interrupt, rc=%#x!\n", rc));
244 pDevExt->interruptObject = NULL;
245 freeDeviceResources(pDrvObj, pDevObj);
246 return rc;
247 }
248 dprintf(("VBoxGuest::ntCreateDevice: IRQ connected!\n"));
249 }
250
251 if (NT_SUCCESS(rc))
252 {
253 // create our thread to inform the VBoxMouse driver
254 rc = createThreads(pDevExt);
255 }
256
257 if (NT_SUCCESS(rc))
258 {
259 // initialize the event notification semaphore
260 KeInitializeEvent(&pDevExt->keventNotification, NotificationEvent, FALSE);
261
262 /* Preallocated constant timeout 250ms for HGCM async waiter. */
263 pDevExt->HGCMWaitTimeout.QuadPart = 250;
264 pDevExt->HGCMWaitTimeout.QuadPart *= -10000; /* relative in 100ns units */
265 }
266
267 rc = hlpVBoxReportGuestInfo (pDevExt);
268 if (!NT_SUCCESS(rc))
269 {
270 dprintf(("VBoxGuest::AddDevice: could not report information to host, rc = %d, exiting!\n", rc));
271 freeDeviceResources(pDrvObj, pDevObj);
272 return STATUS_UNSUCCESSFUL;
273 }
274
275 /** @todo cleanup on failure */
276
277 VBoxInitMemBalloon(pDevExt);
278
279 // ready to rumble!
280 pDevExt->devState = WORKING;
281 dprintf(("returning from createDevice with rc = 0x%x\n", rc));
282 return rc;
283}
284
285
286/**
287 * Helper function to handle the PCI device lookup
288 *
289 * @returns NT error codes
290 */
291static NTSTATUS findPCIDevice(PULONG pBusNumber, PPCI_SLOT_NUMBER pSlotNumber)
292{
293 NTSTATUS rc;
294
295 ULONG busNumber;
296 ULONG deviceNumber;
297 ULONG functionNumber;
298 PCI_SLOT_NUMBER slotNumber;
299 PCI_COMMON_CONFIG pciData;
300
301 dprintf(("findPCIDevice\n"));
302
303 rc = STATUS_DEVICE_DOES_NOT_EXIST;
304 slotNumber.u.AsULONG = 0;
305 // scan each bus
306 for (busNumber = 0; busNumber < PCI_MAX_BUSES; busNumber++)
307 {
308 // scan each device
309 for (deviceNumber = 0; deviceNumber < PCI_MAX_DEVICES; deviceNumber++)
310 {
311 slotNumber.u.bits.DeviceNumber = deviceNumber;
312 // scan each function (not really required...)
313 for (functionNumber = 0; functionNumber < PCI_MAX_FUNCTION; functionNumber++)
314 {
315 slotNumber.u.bits.FunctionNumber = functionNumber;
316 // have a look at what's in this slot
317 if (!HalGetBusData(PCIConfiguration, busNumber, slotNumber.u.AsULONG,
318 &pciData, sizeof(ULONG)))
319 {
320 // no such bus, we're done with it
321 deviceNumber = PCI_MAX_DEVICES;
322 break;
323 }
324
325 if (pciData.VendorID == PCI_INVALID_VENDORID)
326 {
327 // we have to proceed to the next function
328 continue;
329 }
330
331 // check if it's another device
332 if ((pciData.VendorID != VMMDEV_VENDORID) ||
333 (pciData.DeviceID != VMMDEV_DEVICEID))
334 {
335 continue;
336 }
337
338 // Hooray, we've found it!
339 dprintf(("device found!\n"));
340 *pBusNumber = busNumber;
341 *pSlotNumber = slotNumber;
342 rc = STATUS_SUCCESS;
343 }
344 }
345 }
346
347 return rc;
348}
349
350/**
351 * Helper function to cleanup resources
352 *
353 * @param pDrvObj Driver object.
354 * @param pDevObj Device object.
355 */
356static void freeDeviceResources(PDRIVER_OBJECT pDrvObj, PDEVICE_OBJECT pDevObj)
357{
358 PVBOXGUESTDEVEXT pDevExt = (PVBOXGUESTDEVEXT)pDevObj->DeviceExtension;
359
360 // if there's no device extension, we're screwed
361 if (!pDevExt)
362 {
363 dprintf(("freeDeviceResources: FATAL ERROR! device extension pointer is NULL! Not freeing resources!\n"));
364 return;
365 }
366
367 // indicate that the device is no longer ready
368 pDevExt->devState = STOPPED;
369
370 // disconnect interrupts
371 if (pDevExt->interruptObject)
372 {
373 IoDisconnectInterrupt(pDevExt->interruptObject);
374 }
375
376 // unmap mem/io resources
377 hlpVBoxUnmapVMMDevMemory (pDevExt);
378
379 VBoxCleanupMemBalloon(pDevExt);
380}
381
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