win-pvdrivers

view xenhide/xenhide.c @ 382:ecfd4ddaaf3d

remove dead code
author Andy Grover <andy.grover@oracle.com>
date Wed Jul 09 12:35:41 2008 -0700 (2008-07-09)
parents 649b05070ede
children abdcb860cfdf
line source
1 /*
2 PV Drivers for Windows Xen HVM Domains
3 Copyright (C) 2007 James Harper
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
20 #include "xenhide.h"
21 #include <stdlib.h>
23 DRIVER_INITIALIZE DriverEntry;
24 static NTSTATUS
25 XenHide_AddDevice(PDRIVER_OBJECT DriverObject, PDEVICE_OBJECT PhysicalDeviceObject);
26 static NTSTATUS
27 XenHide_Pass(PDEVICE_OBJECT DeviceObject, PIRP Irp);
28 static NTSTATUS
29 XenHide_Pnp(PDEVICE_OBJECT DeviceObject, PIRP Irp);
30 static NTSTATUS
31 XenHide_AddDevice();
32 //static NTSTATUS
33 //XenHide_Unload();
35 #ifdef ALLOC_PRAGMA
36 #pragma alloc_text (INIT, DriverEntry)
37 #pragma alloc_text (PAGE, XenHide_AddDevice)
38 #endif
40 static BOOLEAN gplpv;
41 static XENHIDE_DRIVER_DATA xenhide_global_data;
43 static NTSTATUS
44 XenHide_Power(PDEVICE_OBJECT device_object, PIRP irp)
45 {
46 NTSTATUS status;
47 PXENHIDE_DEVICE_DATA xhdd = (PXENHIDE_DEVICE_DATA)device_object->DeviceExtension;
49 PoStartNextPowerIrp(irp);
50 IoSkipCurrentIrpStackLocation(irp);
51 status = PoCallDriver(xhdd->lower_do, irp);
52 return status;
53 }
55 NTSTATUS
56 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
57 {
58 NTSTATUS status;
59 UNICODE_STRING RegKeyName;
60 UNICODE_STRING RegValueName;
61 HANDLE RegHandle;
62 OBJECT_ATTRIBUTES RegObjectAttributes;
63 char Buf[300];// Sometimes bigger then 200 if system reboot from crash
64 ULONG BufLen = 300;
65 PKEY_VALUE_PARTIAL_INFORMATION KeyPartialValue;
66 int State = 0;
67 size_t StartPos = 0;
68 WCHAR *SystemStartOptions;
69 size_t SystemStartOptionsLen;
70 size_t i;
72 UNREFERENCED_PARAMETER(RegistryPath);
74 KdPrint((__DRIVER_NAME " --> DriverEntry\n"));
76 RtlInitUnicodeString(&RegKeyName, L"\\Registry\\Machine\\System\\CurrentControlSet\\Control");
77 InitializeObjectAttributes(&RegObjectAttributes, &RegKeyName, OBJ_CASE_INSENSITIVE, NULL, NULL);
78 status = ZwOpenKey(&RegHandle, KEY_READ, &RegObjectAttributes);
79 if(!NT_SUCCESS(status))
80 {
81 KdPrint((__DRIVER_NAME " ZwOpenKey returned %08x\n", status));
82 }
84 RtlInitUnicodeString(&RegValueName, L"SystemStartOptions");
85 status = ZwQueryValueKey(RegHandle, &RegValueName, KeyValuePartialInformation, Buf, BufLen, &BufLen);
86 if(!NT_SUCCESS(status))
87 {
88 KdPrint((__DRIVER_NAME " ZwQueryKeyValue returned %08x\n", status));
89 }
90 else
91 ZwClose(RegHandle);
92 KeyPartialValue = (PKEY_VALUE_PARTIAL_INFORMATION)Buf;
93 SystemStartOptions = (WCHAR *)KeyPartialValue->Data;
95 gplpv = FALSE;
97 RtlStringCbLengthW(SystemStartOptions, KeyPartialValue->DataLength, &SystemStartOptionsLen);
99 for (i = 0; i <= SystemStartOptionsLen/2; i++)
100 {
101 //KdPrint((__DRIVER_NAME " pos = %d, state = %d, char = '%wc' (%d)\n", i, State, SystemStartOptions[i], SystemStartOptions[i]));
103 switch (State)
104 {
105 case 0:
106 if (SystemStartOptions[i] == L'G')
107 {
108 StartPos = (int)i;
109 State = 2;
110 } else if (SystemStartOptions[i] != L' ')
111 {
112 State = 1;
113 }
114 break;
115 case 1:
116 if (SystemStartOptions[i] == L' ')
117 State = 0;
118 break;
119 case 2:
120 if (SystemStartOptions[i] == L'P')
121 State = 3;
122 else
123 State = 0;
124 break;
125 case 3:
126 if (SystemStartOptions[i] == L'L')
127 State = 4;
128 else
129 State = 0;
130 break;
131 case 4:
132 if (SystemStartOptions[i] == L'P')
133 State = 5;
134 else
135 State = 0;
136 break;
137 case 5:
138 if (SystemStartOptions[i] == L'V')
139 State = 6;
140 else
141 State = 0;
142 break;
143 case 6:
144 if (SystemStartOptions[i] == L' ' || SystemStartOptions[i] == 0)
145 gplpv = TRUE;
146 State = 0;
147 break;
148 }
149 }
151 KdPrint((__DRIVER_NAME " gplpv = %d\n", gplpv));
153 for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
154 DriverObject->MajorFunction[i] = XenHide_Pass;
155 DriverObject->MajorFunction[IRP_MJ_PNP] = XenHide_Pnp;
156 DriverObject->MajorFunction[IRP_MJ_POWER] = XenHide_Power;
157 DriverObject->DriverExtension->AddDevice = XenHide_AddDevice;
159 RtlZeroMemory(&xenhide_global_data, sizeof(XENHIDE_DRIVER_DATA));
161 InitializeListHead(&xenhide_global_data.hide_list_head);
162 KeInitializeSpinLock(&xenhide_global_data.hide_list_lock);
163 KeInitializeEvent(&xenhide_global_data.hide_list_event, SynchronizationEvent, FALSE);
165 KdPrint((__DRIVER_NAME " <-- DriverEntry\n"));
167 return status;
168 }
170 static BOOLEAN
171 XenHide_IdSuffixMatches(PDEVICE_OBJECT pdo, PWCHAR matching_id)
172 {
173 NTSTATUS status;
174 ULONG remaining;
175 size_t string_length;
176 WCHAR ids[512];
177 PWCHAR ptr;
178 ULONG ids_length;
179 int i;
181 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
182 for (i = 0; i < 2; i++)
183 {
184 if (i == 0)
185 status = IoGetDeviceProperty(pdo, DevicePropertyCompatibleIDs, sizeof(ids), ids, &ids_length);
186 else
187 status = IoGetDeviceProperty(pdo, DevicePropertyHardwareID, sizeof(ids), ids, &ids_length);
189 if (!NT_SUCCESS(status))
190 {
191 // KdPrint((__DRIVER_NAME " i = %d, status = %x, ids_length = %d\n", i, status, ids_length));
192 continue;
193 }
195 remaining = ids_length / 2;
196 for (ptr = ids; *ptr != 0; ptr += string_length + 1)
197 {
198 RtlStringCchLengthW(ptr, remaining, &string_length);
199 remaining -= (ULONG)string_length + 1;
200 if (string_length >= wcslen(matching_id))
201 {
202 ptr += string_length - wcslen(matching_id);
203 string_length = (ULONG)wcslen(matching_id);
204 }
205 // KdPrint((__DRIVER_NAME " Comparing '%S' and '%S'\n", ptr, matching_id));
206 if (wcscmp(ptr, matching_id) == 0)
207 {
208 KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " (Match)\n"));
209 return TRUE;
210 }
211 }
212 }
213 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " (No match)\n"));
214 return FALSE;
215 }
217 static NTSTATUS
218 XenHide_AddDevice(
219 PDRIVER_OBJECT DriverObject,
220 PDEVICE_OBJECT PhysicalDeviceObject
221 )
222 {
223 NTSTATUS status;
224 PDEVICE_OBJECT deviceObject = NULL;
225 PXENHIDE_DEVICE_DATA xhdd;
226 ULONG length;
227 GUID bus_type;
228 WCHAR device_description[256];
229 KIRQL old_irql;
230 USHORT hide_type;
231 PXENHIDE_HIDE_LIST_ENTRY list_entry;
233 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
236 length = 512;
237 status = IoGetDeviceProperty(PhysicalDeviceObject, DevicePropertyDeviceDescription, length, device_description, &length);
238 if (!NT_SUCCESS(status))
239 {
240 device_description[0] = 0;
241 }
243 //KdPrint((__DRIVER_NAME " Checking '%S'\n", device_description));
245 length = sizeof(GUID);
246 status = IoGetDeviceProperty(PhysicalDeviceObject, DevicePropertyBusTypeGuid, length, &bus_type, &length);
247 if (!NT_SUCCESS(status))
248 {
249 RtlZeroMemory(&bus_type, sizeof(GUID));
250 }
252 hide_type = XENHIDE_TYPE_NONE;
253 if (gplpv)
254 {
255 /* hide only specific devices */
256 if (XenHide_IdSuffixMatches(PhysicalDeviceObject, L"VEN_8086&DEV_7010") // Qemu IDE
257 || XenHide_IdSuffixMatches(PhysicalDeviceObject, L"VEN_10EC&DEV_8139") // Qemu Network
258 || XenHide_IdSuffixMatches(PhysicalDeviceObject, L"VEN_1000&DEV_0012"))// Qemu SCSI
259 {
260 hide_type = XENHIDE_TYPE_DEVICE;
261 }
262 else if (XenHide_IdSuffixMatches(PhysicalDeviceObject, L"PNP0A03"))
263 {
264 hide_type = XENHIDE_TYPE_PCI_BUS;
265 }
266 }
267 else
268 {
269 /* hide everything on the xen bus */
270 if (memcmp(&bus_type, &GUID_BUS_TYPE_XEN, sizeof(GUID)) == 0)
271 hide_type = XENHIDE_TYPE_DEVICE;
272 }
274 if (hide_type == XENHIDE_TYPE_NONE)
275 {
276 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " (filter not required for %S)\n", device_description));
277 return STATUS_SUCCESS;
278 }
280 //KdPrint((__DRIVER_NAME " Installing Filter for %S\n", device_description));
282 if (gplpv && hide_type == XENHIDE_TYPE_DEVICE)
283 {
284 KeAcquireSpinLock(&xenhide_global_data.hide_list_lock, &old_irql);
285 list_entry = ExAllocatePoolWithTag(PagedPool, sizeof(XENHIDE_HIDE_LIST_ENTRY), XENHIDE_POOL_TAG);
286 list_entry->pdo = PhysicalDeviceObject;
287 InsertTailList(&xenhide_global_data.hide_list_head, (PLIST_ENTRY)list_entry);
288 KeReleaseSpinLock(&xenhide_global_data.hide_list_lock, old_irql);
289 KeSetEvent(&xenhide_global_data.hide_list_event, IO_NO_INCREMENT, FALSE);
290 ASSERT(xenhide_global_data.pci_bus_pdo);
291 IoInvalidateDeviceRelations(xenhide_global_data.pci_bus_pdo, BusRelations);
292 }
293 else if (hide_type == XENHIDE_TYPE_PCI_BUS)
294 {
295 ASSERT(!xenhide_global_data.pci_bus_pdo);
296 xenhide_global_data.pci_bus_pdo = PhysicalDeviceObject;
297 }
299 status = IoCreateDevice (DriverObject,
300 sizeof(XENHIDE_DEVICE_DATA),
301 NULL,
302 FILE_DEVICE_UNKNOWN,
303 FILE_DEVICE_SECURE_OPEN,
304 FALSE,
305 &deviceObject);
307 xhdd = (PXENHIDE_DEVICE_DATA)deviceObject->DeviceExtension;
309 xhdd->hide_type = hide_type;
311 xhdd->lower_do = IoAttachDeviceToDeviceStack(
312 deviceObject, PhysicalDeviceObject);
313 deviceObject->Flags |= xhdd->lower_do->Flags;
315 deviceObject->DeviceType = xhdd->lower_do->DeviceType;
317 deviceObject->Characteristics =
318 xhdd->lower_do->Characteristics;
320 xhdd->filter_do = deviceObject;
322 deviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
324 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
326 return STATUS_SUCCESS;
327 }
329 static NTSTATUS
330 XenHide_Pass(PDEVICE_OBJECT DeviceObject, PIRP Irp)
331 {
332 PXENHIDE_DEVICE_DATA xhdd = (PXENHIDE_DEVICE_DATA)DeviceObject->DeviceExtension;
333 NTSTATUS status;
335 IoSkipCurrentIrpStackLocation(Irp);
336 status = IoCallDriver(xhdd->lower_do, Irp);
337 return status;
338 }
340 static NTSTATUS
341 XenHide_Pnp_IoCompletion(PDEVICE_OBJECT device_object, PIRP irp, PVOID context)
342 {
343 PKEVENT event = (PKEVENT)context;
345 UNREFERENCED_PARAMETER(device_object);
347 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
349 if (irp->PendingReturned)
350 {
351 KeSetEvent(event, IO_NO_INCREMENT, FALSE);
352 }
354 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__"\n"));
356 return STATUS_MORE_PROCESSING_REQUIRED;
357 }
359 static NTSTATUS
360 XenHide_SendAndWaitForIrp(PDEVICE_OBJECT device_object, PIRP irp)
361 {
362 NTSTATUS status;
363 PXENHIDE_DEVICE_DATA xhdd = (PXENHIDE_DEVICE_DATA)device_object->DeviceExtension;
364 KEVENT event;
366 UNREFERENCED_PARAMETER(device_object);
368 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
370 KeInitializeEvent(&event, NotificationEvent, FALSE);
372 IoCopyCurrentIrpStackLocationToNext(irp);
373 IoSetCompletionRoutine(irp, XenHide_Pnp_IoCompletion, &event, TRUE, TRUE, TRUE);
375 status = IoCallDriver(xhdd->lower_do, irp);
377 if (status == STATUS_PENDING)
378 {
379 KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
380 status = irp->IoStatus.Status;
381 }
383 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__"\n"));
385 return status;
386 }
388 static NTSTATUS
389 XenHide_Pnp(PDEVICE_OBJECT device_object, PIRP irp)
390 {
391 NTSTATUS status = STATUS_SUCCESS;
392 PIO_STACK_LOCATION stack;
393 PXENHIDE_DEVICE_DATA xhdd = (PXENHIDE_DEVICE_DATA)device_object->DeviceExtension;
394 PDEVICE_RELATIONS relations;
395 PXENHIDE_HIDE_LIST_ENTRY list_entry;
396 ULONG i, j;
397 KIRQL old_irql;
399 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
401 stack = IoGetCurrentIrpStackLocation(irp);
403 switch (stack->MinorFunction) {
404 case IRP_MN_START_DEVICE:
405 //KdPrint((__DRIVER_NAME " IRP_MN_START_DEVICE\n"));
406 if (xhdd->hide_type == XENHIDE_TYPE_DEVICE)
407 {
408 //KdPrint((__DRIVER_NAME " hide_type == XENHIDE_TYPE_DEVICE\n"));
409 status = irp->IoStatus.Status = STATUS_UNSUCCESSFUL;
410 IoCompleteRequest(irp, IO_NO_INCREMENT);
411 }
412 else
413 {
414 //KdPrint((__DRIVER_NAME " hide_type != XENHIDE_TYPE_DEVICE\n"));
415 IoSkipCurrentIrpStackLocation(irp);
416 status = IoCallDriver(xhdd->lower_do, irp);
417 }
418 break;
419 case IRP_MN_QUERY_DEVICE_RELATIONS:
420 if (xhdd->hide_type == XENHIDE_TYPE_PCI_BUS && stack->Parameters.QueryDeviceRelations.Type == BusRelations)
421 {
422 //KdPrint((__DRIVER_NAME " IRP_MN_QUERY_DEVICE_RELATIONS - BusRelations\n"));
423 IoMarkIrpPending(irp);
424 status = XenHide_SendAndWaitForIrp(device_object, irp);
425 relations = (PDEVICE_RELATIONS)irp->IoStatus.Information;
426 for (i = 0, j = 0; i < relations->Count; i++)
427 {
428 if (i != j)
429 relations->Objects[j] = relations->Objects[i];
430 KeAcquireSpinLock(&xenhide_global_data.hide_list_lock, &old_irql);
431 list_entry = (PXENHIDE_HIDE_LIST_ENTRY)xenhide_global_data.hide_list_head.Flink;
432 while (list_entry != (PXENHIDE_HIDE_LIST_ENTRY)&xenhide_global_data.hide_list_head)
433 {
434 if (relations->Objects[i] == list_entry->pdo)
435 {
436 //KdPrint((__DRIVER_NAME " Hiding %p\n", relations->Objects[i]));
437 break;
438 }
439 list_entry = (PXENHIDE_HIDE_LIST_ENTRY)list_entry->entry.Flink;
440 }
441 if (list_entry == (PXENHIDE_HIDE_LIST_ENTRY)&xenhide_global_data.hide_list_head)
442 j++;
443 KeReleaseSpinLock(&xenhide_global_data.hide_list_lock, old_irql);
444 }
445 relations->Count = j;
446 irp->IoStatus.Status = status;
447 IoCompleteRequest (irp, IO_NO_INCREMENT);
448 }
449 else
450 {
451 IoSkipCurrentIrpStackLocation(irp);
452 status = IoCallDriver(xhdd->lower_do, irp);
453 }
454 break;
455 default:
456 IoSkipCurrentIrpStackLocation(irp);
457 status = IoCallDriver(xhdd->lower_do, irp);
458 break;
459 }
461 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " (returning with status %08x)\n", status));
463 return status;
464 }