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 | *******************************************************************************/
|
---|
39 | extern "C"
|
---|
40 | {
|
---|
41 | static NTSTATUS findPCIDevice(PULONG pBusNumber, PPCI_SLOT_NUMBER pSlotNumber);
|
---|
42 | static 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 | */
|
---|
57 | NTSTATUS 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 | // store a reference to ourself
|
---|
115 | pDevExt->deviceObject = deviceObject;
|
---|
116 | // store bus and slot number we've queried before
|
---|
117 | pDevExt->busNumber = busNumber;
|
---|
118 | pDevExt->slotNumber = slotNumber;
|
---|
119 |
|
---|
120 | //
|
---|
121 | // let's have a look at what our PCI adapter offers
|
---|
122 | //
|
---|
123 | dprintf(("VBoxGuest::ntCreateDevice: starting to scan PCI resources of VBoxGuest\n"));
|
---|
124 | // assign the PCI resources
|
---|
125 | PCM_RESOURCE_LIST resourceList;
|
---|
126 | UNICODE_STRING classNameString;
|
---|
127 | RtlInitUnicodeString(&classNameString, L"VBoxGuestAdapter");
|
---|
128 | rc = HalAssignSlotResources(pRegPath, &classNameString,
|
---|
129 | pDrvObj, pDevObj,
|
---|
130 | PCIBus, busNumber, slotNumber,
|
---|
131 | &resourceList);
|
---|
132 | if (!NT_SUCCESS(rc))
|
---|
133 | {
|
---|
134 | dprintf(("VBoxGuest::ntCreateDevice: HalAssignSlotResources failed with rc=%#x!\n", rc));
|
---|
135 | freeDeviceResources(pDrvObj, pDevObj);
|
---|
136 | return rc;
|
---|
137 | }
|
---|
138 |
|
---|
139 | rc = VBoxScanPCIResourceList(resourceList, pDevExt);
|
---|
140 |
|
---|
141 | rc = VbglInit (pDevExt->startPortAddress, pDevExt->pVMMDevMemory);
|
---|
142 | if (!VBOX_SUCCESS(rc))
|
---|
143 | {
|
---|
144 | dprintf(("VBoxGuest::START_DEVICE: VbglInit failed. rc = %d\n", rc));
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | rc = VbglGRAlloc ((VMMDevRequestHeader **)&pDevExt->irqAckEvents, sizeof (VMMDevEvents), VMMDevReq_AcknowledgeEvents);
|
---|
149 | if (!VBOX_SUCCESS(rc))
|
---|
150 | {
|
---|
151 | dprintf(("VBoxGuest::START_DEVICE: VbglAlloc failed for irqAckEvents. rc = %d\n", rc));
|
---|
152 | }
|
---|
153 | rc = VbglGRAlloc ((VMMDevRequestHeader **)&pDevExt->powerStateRequest, sizeof (VMMDevPowerStateRequest), VMMDevReq_SetPowerStatus);
|
---|
154 | if (!VBOX_SUCCESS(rc))
|
---|
155 | {
|
---|
156 | dprintf(("VBoxGuest::START_DEVICE: VbglAlloc failed for powerStateRequest. rc = %d\n", rc));
|
---|
157 | }
|
---|
158 |
|
---|
159 | #if 0
|
---|
160 | //
|
---|
161 | // now proceed to the busmaster DMA stuff
|
---|
162 | //
|
---|
163 |
|
---|
164 | DEVICE_DESCRIPTION deviceDescription;
|
---|
165 | ULONG numberOfMapRegisters;
|
---|
166 | deviceDescription.Version = DEVICE_DESCRIPTION_VERSION;
|
---|
167 | deviceDescription.Master = TRUE;
|
---|
168 | deviceDescription.ScatterGather = TRUE;
|
---|
169 | deviceDescription.BusNumber = pDevExt->busNumber;
|
---|
170 | deviceDescription.InterfaceType = PCIBus;
|
---|
171 | deviceDescription.MaximumLength = MAXIMUM_TRANSFER_LENGTH;
|
---|
172 | pDevExt->adapterObject = HalGetAdapter(&deviceDescription, &numberOfMapRegisters);
|
---|
173 | if (pDevExt->adapterObject == NULL)
|
---|
174 | {
|
---|
175 | dprintf(("VBoxGuest::ntCreateDevice: HalGetAdapter failed!\n"));
|
---|
176 | freeDeviceResources(pDrvObj, pDevObj);
|
---|
177 | return rc;
|
---|
178 | }
|
---|
179 |
|
---|
180 | // @todo allocate S/G buffer
|
---|
181 | #endif
|
---|
182 |
|
---|
183 |
|
---|
184 | //
|
---|
185 | // it's time to map the I/O and memory spaces
|
---|
186 | //
|
---|
187 |
|
---|
188 | // Map physical address of VMMDev memory
|
---|
189 | rc = hlpVBoxMapVMMDevMemory (pDevExt);
|
---|
190 | if (!NT_SUCCESS(rc))
|
---|
191 | {
|
---|
192 | dprintf(("VBoxGuest::ntCreateDevice: Unable to map VMMDev Memory, rc=%#x!\n", rc));
|
---|
193 | freeDeviceResources(pDrvObj, pDevObj);
|
---|
194 | return rc;
|
---|
195 | }
|
---|
196 |
|
---|
197 | //
|
---|
198 | // now we need an ISR and DPC
|
---|
199 | //
|
---|
200 |
|
---|
201 | // register DPC routine
|
---|
202 | dprintf(("VBoxGuest::ntCreateDevice: initializing DPC...\n"));
|
---|
203 | IoInitializeDpcRequest(pDevExt->deviceObject, VBoxGuestDpcHandler);
|
---|
204 | // get an interrupt vector
|
---|
205 | ULONG vector;
|
---|
206 | KIRQL irql;
|
---|
207 | KAFFINITY affinity;
|
---|
208 | // only proceed if the device provides an interrupt
|
---|
209 | if (pDevExt->interruptLevel || pDevExt->interruptVector)
|
---|
210 | {
|
---|
211 | vector = HalGetInterruptVector(PCIBus,
|
---|
212 | pDevExt->busNumber,
|
---|
213 | pDevExt->interruptLevel,
|
---|
214 | pDevExt->interruptVector,
|
---|
215 | &irql,
|
---|
216 | &affinity);
|
---|
217 | dprintf(("VBoxGuest::ntCreateDevice: HalGetInterruptVector returns vector %u\n", vector));
|
---|
218 | rc = IoConnectInterrupt(&pDevExt->interruptObject, // out: interrupt object
|
---|
219 | (PKSERVICE_ROUTINE)VBoxGuestIsrHandler, // ISR
|
---|
220 | pDevExt, // context
|
---|
221 | NULL, // optional spinlock
|
---|
222 | vector, // interrupt vector
|
---|
223 | irql, // interrupt level
|
---|
224 | irql, // interrupt level
|
---|
225 | pDevExt->interruptMode, // LevelSensitive or Latched
|
---|
226 | TRUE, // shareable interrupt
|
---|
227 | affinity, // CPU affinity
|
---|
228 | FALSE); // don't save FPU stack
|
---|
229 | if (!NT_SUCCESS(rc))
|
---|
230 | {
|
---|
231 | dprintf(("VBoxGuest::ntCreateDevice: Unable to connect interrupt, rc=%#x!\n", rc));
|
---|
232 | pDevExt->interruptObject = NULL;
|
---|
233 | freeDeviceResources(pDrvObj, pDevObj);
|
---|
234 | return rc;
|
---|
235 | }
|
---|
236 | dprintf(("VBoxGuest::ntCreateDevice: IRQ connected!\n"));
|
---|
237 | }
|
---|
238 |
|
---|
239 | if (NT_SUCCESS(rc))
|
---|
240 | {
|
---|
241 | // create our thread to inform the VBoxMouse driver
|
---|
242 | rc = createThreads(pDevExt);
|
---|
243 | }
|
---|
244 |
|
---|
245 | if (NT_SUCCESS(rc))
|
---|
246 | {
|
---|
247 | // initialize the event notification semaphore
|
---|
248 | KeInitializeEvent(&pDevExt->keventNotification, NotificationEvent, FALSE);
|
---|
249 |
|
---|
250 | /* Preallocated constant timeout 250ms for HGCM async waiter. */
|
---|
251 | pDevExt->HGCMWaitTimeout.QuadPart = 250;
|
---|
252 | pDevExt->HGCMWaitTimeout.QuadPart *= -10000; /* relative in 100ns units */
|
---|
253 | }
|
---|
254 |
|
---|
255 | rc = hlpVBoxReportGuestInfo (pDevExt);
|
---|
256 | if (!NT_SUCCESS(rc))
|
---|
257 | {
|
---|
258 | dprintf(("VBoxGuest::AddDevice: could not report information to host, rc = %d, exiting!\n", rc));
|
---|
259 | freeDeviceResources(pDrvObj, pDevObj);
|
---|
260 | return STATUS_UNSUCCESSFUL;
|
---|
261 | }
|
---|
262 |
|
---|
263 | /** @todo cleanup on failure */
|
---|
264 |
|
---|
265 | VBoxInitMemBalloon(pDevExt);
|
---|
266 |
|
---|
267 | // ready to rumble!
|
---|
268 | pDevExt->devState = WORKING;
|
---|
269 | dprintf(("returning from createDevice with rc = 0x%x\n", rc));
|
---|
270 | return rc;
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Helper function to handle the PCI device lookup
|
---|
276 | *
|
---|
277 | * @returns NT error codes
|
---|
278 | */
|
---|
279 | static NTSTATUS findPCIDevice(PULONG pBusNumber, PPCI_SLOT_NUMBER pSlotNumber)
|
---|
280 | {
|
---|
281 | NTSTATUS rc;
|
---|
282 |
|
---|
283 | ULONG busNumber;
|
---|
284 | ULONG deviceNumber;
|
---|
285 | ULONG functionNumber;
|
---|
286 | PCI_SLOT_NUMBER slotNumber;
|
---|
287 | PCI_COMMON_CONFIG pciData;
|
---|
288 |
|
---|
289 | dprintf(("findPCIDevice\n"));
|
---|
290 |
|
---|
291 | rc = STATUS_DEVICE_DOES_NOT_EXIST;
|
---|
292 | slotNumber.u.AsULONG = 0;
|
---|
293 | // scan each bus
|
---|
294 | for (busNumber = 0; busNumber < PCI_MAX_BUSES; busNumber++)
|
---|
295 | {
|
---|
296 | // scan each device
|
---|
297 | for (deviceNumber = 0; deviceNumber < PCI_MAX_DEVICES; deviceNumber++)
|
---|
298 | {
|
---|
299 | slotNumber.u.bits.DeviceNumber = deviceNumber;
|
---|
300 | // scan each function (not really required...)
|
---|
301 | for (functionNumber = 0; functionNumber < PCI_MAX_FUNCTION; functionNumber++)
|
---|
302 | {
|
---|
303 | slotNumber.u.bits.FunctionNumber = functionNumber;
|
---|
304 | // have a look at what's in this slot
|
---|
305 | if (!HalGetBusData(PCIConfiguration, busNumber, slotNumber.u.AsULONG,
|
---|
306 | &pciData, sizeof(ULONG)))
|
---|
307 | {
|
---|
308 | // no such bus, we're done with it
|
---|
309 | deviceNumber = PCI_MAX_DEVICES;
|
---|
310 | break;
|
---|
311 | }
|
---|
312 |
|
---|
313 | if (pciData.VendorID == PCI_INVALID_VENDORID)
|
---|
314 | {
|
---|
315 | // we have to proceed to the next function
|
---|
316 | continue;
|
---|
317 | }
|
---|
318 |
|
---|
319 | // check if it's another device
|
---|
320 | if ((pciData.VendorID != VMMDEV_VENDORID) ||
|
---|
321 | (pciData.DeviceID != VMMDEV_DEVICEID))
|
---|
322 | {
|
---|
323 | continue;
|
---|
324 | }
|
---|
325 |
|
---|
326 | // Hooray, we've found it!
|
---|
327 | dprintf(("device found!\n"));
|
---|
328 | *pBusNumber = busNumber;
|
---|
329 | *pSlotNumber = slotNumber;
|
---|
330 | rc = STATUS_SUCCESS;
|
---|
331 | }
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|
335 | return rc;
|
---|
336 | }
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Helper function to cleanup resources
|
---|
340 | *
|
---|
341 | * @param pDrvObj Driver object.
|
---|
342 | * @param pDevObj Device object.
|
---|
343 | */
|
---|
344 | static void freeDeviceResources(PDRIVER_OBJECT pDrvObj, PDEVICE_OBJECT pDevObj)
|
---|
345 | {
|
---|
346 | PVBOXGUESTDEVEXT pDevExt = (PVBOXGUESTDEVEXT)pDevObj->DeviceExtension;
|
---|
347 |
|
---|
348 | // if there's no device extension, we're screwed
|
---|
349 | if (!pDevExt)
|
---|
350 | {
|
---|
351 | dprintf(("freeDeviceResources: FATAL ERROR! device extension pointer is NULL! Not freeing resources!\n"));
|
---|
352 | return;
|
---|
353 | }
|
---|
354 |
|
---|
355 | // indicate that the device is no longer ready
|
---|
356 | pDevExt->devState = STOPPED;
|
---|
357 |
|
---|
358 | // disconnect interrupts
|
---|
359 | if (pDevExt->interruptObject)
|
---|
360 | {
|
---|
361 | IoDisconnectInterrupt(pDevExt->interruptObject);
|
---|
362 | }
|
---|
363 |
|
---|
364 | // unmap mem/io resources
|
---|
365 | hlpVBoxUnmapVMMDevMemory (pDevExt);
|
---|
366 |
|
---|
367 | VBoxCleanupMemBalloon(pDevExt);
|
---|
368 | }
|
---|
369 |
|
---|