win-pvdrivers

view xenpci/xenpci.c @ 729:7f984f45dfe4

Fix device hiding regression
author James Harper <james.harper@bendigoit.com.au>
date Sat Jan 02 10:17:19 2010 +1100 (2010-01-02)
parents 5bdb7251370c
children f43c89df09b2
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 #define INITGUID
21 #include "xenpci.h"
22 #include <aux_klib.h>
23 #include <stdlib.h>
25 #define SYSRQ_PATH "control/sysrq"
26 #define SHUTDOWN_PATH "control/shutdown"
27 #define BALLOON_PATH "memory/target"
29 #pragma warning(disable : 4200) // zero-sized array
31 /* Not really necessary but keeps PREfast happy */
32 DRIVER_INITIALIZE DriverEntry;
33 static EVT_WDF_DRIVER_DEVICE_ADD XenPci_EvtDeviceAdd;
34 static EVT_WDF_DEVICE_USAGE_NOTIFICATION XenPci_EvtDeviceUsageNotification;
35 static EVT_WDF_DEVICE_PREPARE_HARDWARE XenHide_EvtDevicePrepareHardware;
37 static VOID
38 XenPci_EvtDeviceUsageNotification(WDFDEVICE device, WDF_SPECIAL_FILE_TYPE notification_type, BOOLEAN is_in_notification_path)
39 {
40 FUNCTION_ENTER();
42 UNREFERENCED_PARAMETER(device);
43 UNREFERENCED_PARAMETER(is_in_notification_path);
45 switch (notification_type)
46 {
47 case WdfSpecialFilePaging:
48 KdPrint((__DRIVER_NAME " notification_type = Paging, flag = %d\n", is_in_notification_path));
49 break;
50 case WdfSpecialFileHibernation:
51 KdPrint((__DRIVER_NAME " notification_type = Hibernation, flag = %d\n", is_in_notification_path));
52 break;
53 case WdfSpecialFileDump:
54 KdPrint((__DRIVER_NAME " notification_type = Dump, flag = %d\n", is_in_notification_path));
55 break;
56 default:
57 KdPrint((__DRIVER_NAME " notification_type = %d, flag = %d\n", notification_type, is_in_notification_path));
58 break;
59 }
61 FUNCTION_EXIT();
62 }
64 static NTSTATUS
65 XenPci_EvtDeviceAdd_XenPci(WDFDRIVER driver, PWDFDEVICE_INIT device_init)
66 {
67 NTSTATUS status;
68 // PDEVICE_OBJECT fdo = NULL;
69 // PNP_BUS_INFORMATION busInfo;
70 // DECLARE_CONST_UNICODE_STRING(DeviceName, L"\\Device\\XenShutdown");
71 // DECLARE_CONST_UNICODE_STRING(SymbolicName, L"\\DosDevices\\XenShutdown");
72 WDF_CHILD_LIST_CONFIG child_list_config;
73 WDFDEVICE device;
74 PXENPCI_DEVICE_DATA xpdd;
75 UNICODE_STRING reference;
76 WDF_OBJECT_ATTRIBUTES device_attributes;
77 PNP_BUS_INFORMATION pbi;
78 WDF_PNPPOWER_EVENT_CALLBACKS pnp_power_callbacks;
79 WDF_INTERRUPT_CONFIG interrupt_config;
80 WDF_OBJECT_ATTRIBUTES file_attributes;
81 WDF_FILEOBJECT_CONFIG file_config;
82 WDF_IO_QUEUE_CONFIG queue_config;
83 WDFCOLLECTION veto_devices;
84 WDFKEY param_key;
85 DECLARE_CONST_UNICODE_STRING(veto_devices_name, L"veto_devices");
86 WDF_DEVICE_POWER_CAPABILITIES power_capabilities;
87 int i;
89 UNREFERENCED_PARAMETER(driver);
91 FUNCTION_ENTER();
93 WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnp_power_callbacks);
94 pnp_power_callbacks.EvtDeviceD0Entry = XenPci_EvtDeviceD0Entry;
95 pnp_power_callbacks.EvtDeviceD0EntryPostInterruptsEnabled = XenPci_EvtDeviceD0EntryPostInterruptsEnabled;
96 pnp_power_callbacks.EvtDeviceD0Exit = XenPci_EvtDeviceD0Exit;
97 pnp_power_callbacks.EvtDeviceD0ExitPreInterruptsDisabled = XenPci_EvtDeviceD0ExitPreInterruptsDisabled;
98 pnp_power_callbacks.EvtDevicePrepareHardware = XenPci_EvtDevicePrepareHardware;
99 pnp_power_callbacks.EvtDeviceReleaseHardware = XenPci_EvtDeviceReleaseHardware;
100 pnp_power_callbacks.EvtDeviceQueryRemove = XenPci_EvtDeviceQueryRemove;
101 pnp_power_callbacks.EvtDeviceUsageNotification = XenPci_EvtDeviceUsageNotification;
103 WdfDeviceInitSetPnpPowerEventCallbacks(device_init, &pnp_power_callbacks);
105 WdfDeviceInitSetDeviceType(device_init, FILE_DEVICE_BUS_EXTENDER);
106 WdfDeviceInitSetExclusive(device_init, FALSE);
108 WDF_CHILD_LIST_CONFIG_INIT(&child_list_config, sizeof(XENPCI_PDO_IDENTIFICATION_DESCRIPTION), XenPci_EvtChildListCreateDevice);
109 child_list_config.EvtChildListScanForChildren = XenPci_EvtChildListScanForChildren;
110 WdfFdoInitSetDefaultChildListConfig(device_init, &child_list_config, WDF_NO_OBJECT_ATTRIBUTES);
112 WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&file_attributes, XENPCI_DEVICE_INTERFACE_DATA);
113 WDF_FILEOBJECT_CONFIG_INIT(&file_config, XenPci_EvtDeviceFileCreate, XenPci_EvtFileClose, XenPci_EvtFileCleanup);
114 WdfDeviceInitSetFileObjectConfig(device_init, &file_config, &file_attributes);
116 WdfDeviceInitSetIoType(device_init, WdfDeviceIoBuffered);
118 WdfDeviceInitSetPowerNotPageable(device_init);
120 WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&device_attributes, XENPCI_DEVICE_DATA);
121 status = WdfDeviceCreate(&device_init, &device_attributes, &device);
122 if (!NT_SUCCESS(status))
123 {
124 KdPrint(("Error creating device %08x\n", status));
125 return status;
126 }
128 xpdd = GetXpdd(device);
129 xpdd->wdf_device = device;
130 xpdd->child_list = WdfFdoGetDefaultChildList(device);
132 WdfCollectionCreate(WDF_NO_OBJECT_ATTRIBUTES, &veto_devices);
133 status = WdfDriverOpenParametersRegistryKey(driver, KEY_QUERY_VALUE, WDF_NO_OBJECT_ATTRIBUTES, &param_key);
134 if (NT_SUCCESS(status))
135 {
136 status = WdfRegistryQueryMultiString(param_key, &veto_devices_name, WDF_NO_OBJECT_ATTRIBUTES, veto_devices);
137 if (!NT_SUCCESS(status))
138 {
139 KdPrint(("Error reading parameters/veto_devices value %08x\n", status));
140 }
141 WdfRegistryClose(param_key);
142 }
143 else
144 {
145 KdPrint(("Error opening parameters key %08x\n", status));
146 }
148 InitializeListHead(&xpdd->veto_list);
149 for (i = 0; i < (int)WdfCollectionGetCount(veto_devices); i++)
150 {
151 WDFOBJECT ws;
152 UNICODE_STRING val;
153 ANSI_STRING s;
154 PVOID entry;
155 ws = WdfCollectionGetItem(veto_devices, i);
156 WdfStringGetUnicodeString(ws, &val);
157 RtlUnicodeStringToAnsiString(&s, &val, TRUE);
158 entry = ExAllocatePoolWithTag(NonPagedPool, sizeof(LIST_ENTRY) + s.Length + 1, XENPCI_POOL_TAG);
159 memcpy((PUCHAR)entry + sizeof(LIST_ENTRY), s.Buffer, s.Length + 1);
160 RtlFreeAnsiString(&s);
161 InsertTailList(&xpdd->veto_list, (PLIST_ENTRY)entry);
162 }
163 WDF_DEVICE_POWER_CAPABILITIES_INIT(&power_capabilities);
164 power_capabilities.DeviceD1 = WdfTrue;
165 power_capabilities.WakeFromD1 = WdfTrue;
166 power_capabilities.DeviceWake = PowerDeviceD1;
167 power_capabilities.DeviceState[PowerSystemWorking] = PowerDeviceD1;
168 power_capabilities.DeviceState[PowerSystemSleeping1] = PowerDeviceD1;
169 power_capabilities.DeviceState[PowerSystemSleeping2] = PowerDeviceD2;
170 power_capabilities.DeviceState[PowerSystemSleeping3] = PowerDeviceD2;
171 power_capabilities.DeviceState[PowerSystemHibernate] = PowerDeviceD3;
172 power_capabilities.DeviceState[PowerSystemShutdown] = PowerDeviceD3;
173 WdfDeviceSetPowerCapabilities(device, &power_capabilities);
175 WdfDeviceSetSpecialFileSupport(device, WdfSpecialFilePaging, TRUE);
176 WdfDeviceSetSpecialFileSupport(device, WdfSpecialFileHibernation, TRUE);
177 WdfDeviceSetSpecialFileSupport(device, WdfSpecialFileDump, TRUE);
179 WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(&queue_config, WdfIoQueueDispatchParallel);
180 queue_config.EvtIoDefault = XenPci_EvtIoDefault;
181 status = WdfIoQueueCreate(device, &queue_config, WDF_NO_OBJECT_ATTRIBUTES, &xpdd->io_queue);
182 if (!NT_SUCCESS(status)) {
183 KdPrint(("Error creating queue 0x%x\n", status));
184 return status;
185 }
187 WDF_INTERRUPT_CONFIG_INIT(&interrupt_config, EvtChn_EvtInterruptIsr, NULL);
188 interrupt_config.EvtInterruptEnable = EvtChn_EvtInterruptEnable;
189 interrupt_config.EvtInterruptDisable = EvtChn_EvtInterruptDisable;
191 status = WdfInterruptCreate(device, &interrupt_config, WDF_NO_OBJECT_ATTRIBUTES, &xpdd->interrupt);
192 if (!NT_SUCCESS(status))
193 {
194 KdPrint(("Error creating interrupt 0x%x\n", status));
195 return status;
196 }
198 RtlInitUnicodeString(&reference, L"xenbus");
199 status = WdfDeviceCreateDeviceInterface(device, &GUID_DEVINTERFACE_XENBUS, &reference);
200 if (!NT_SUCCESS(status)) {
201 KdPrint(("Error registering device interface 0x%x\n", status));
202 return status;
203 }
205 RtlInitUnicodeString(&reference, L"evtchn");
206 status = WdfDeviceCreateDeviceInterface(device, &GUID_DEVINTERFACE_EVTCHN, &reference);
207 if (!NT_SUCCESS(status)) {
208 KdPrint(("Error registering device interface 0x%x\n", status));
209 return status;
210 }
212 RtlInitUnicodeString(&reference, L"gntdev");
213 status = WdfDeviceCreateDeviceInterface(device, &GUID_DEVINTERFACE_GNTDEV, &reference);
214 if (!NT_SUCCESS(status)) {
215 KdPrint(("Error registering device interface 0x%x\n", status));
216 return status;
217 }
219 pbi.BusTypeGuid = GUID_BUS_TYPE_XEN;
220 pbi.LegacyBusType = PNPBus;
221 pbi.BusNumber = 0;
222 WdfDeviceSetBusInformationForChildren(device, &pbi);
224 xpdd->removable = TRUE;
226 FUNCTION_EXIT();
227 return status;
228 }
230 NTSTATUS
231 XenHide_EvtDevicePrepareHardware (WDFDEVICE device, WDFCMRESLIST resources_raw, WDFCMRESLIST resources_translated)
232 {
233 UNREFERENCED_PARAMETER(device);
234 UNREFERENCED_PARAMETER(resources_raw);
235 UNREFERENCED_PARAMETER(resources_translated);
236 FUNCTION_ENTER();
237 FUNCTION_EXIT();
238 return STATUS_UNSUCCESSFUL;
239 }
241 static BOOLEAN
242 XenPci_IdSuffixMatches(PWDFDEVICE_INIT device_init, PWCHAR matching_id)
243 {
244 NTSTATUS status;
245 WDFMEMORY memory;
246 ULONG remaining;
247 size_t string_length;
248 PWCHAR ids;
249 PWCHAR ptr;
250 size_t ids_length;
251 ULONG properties[] = {DevicePropertyCompatibleIDs, DevicePropertyHardwareID};
252 int i;
254 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
255 for (i = 0; i < ARRAY_SIZE(properties); i++)
256 {
258 status = WdfFdoInitAllocAndQueryProperty(device_init, properties[i], NonPagedPool, WDF_NO_OBJECT_ATTRIBUTES, &memory);
259 if (!NT_SUCCESS(status))
260 continue;
261 ids = WdfMemoryGetBuffer(memory, &ids_length);
263 if (!NT_SUCCESS(status))
264 {
265 // KdPrint((__DRIVER_NAME " i = %d, status = %x, ids_length = %d\n", i, status, ids_length));
266 continue;
267 }
269 remaining = (ULONG)ids_length / 2;
270 for (ptr = ids; *ptr != 0; ptr += string_length + 1)
271 {
272 RtlStringCchLengthW(ptr, remaining, &string_length);
273 remaining -= (ULONG)string_length + 1;
274 if (string_length >= wcslen(matching_id))
275 {
276 ptr += string_length - wcslen(matching_id);
277 string_length = wcslen(matching_id);
278 }
279 // KdPrint((__DRIVER_NAME " Comparing '%S' and '%S'\n", ptr, matching_id));
280 if (wcscmp(ptr, matching_id) == 0)
281 {
282 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " (Match)\n"));
283 WdfObjectDelete(memory);
284 return TRUE;
285 }
286 }
287 WdfObjectDelete(memory);
288 }
289 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " (No match)\n"));
290 return FALSE;
291 }
293 ULONG qemu_filtered;
294 ULONG qemu_filtered_by_qemu;
296 static NTSTATUS
297 XenPci_EvtDeviceAdd_XenHide(WDFDRIVER driver, PWDFDEVICE_INIT device_init)
298 {
299 NTSTATUS status;
300 WDFMEMORY memory;
301 PWCHAR device_description;
302 WDF_PNPPOWER_EVENT_CALLBACKS pnp_power_callbacks;
303 WDF_OBJECT_ATTRIBUTES device_attributes;
304 BOOLEAN hide_required = FALSE;
305 WDFDEVICE device;
306 WDFKEY param_key;
307 DECLARE_CONST_UNICODE_STRING(hide_devices_name, L"hide_devices");
308 WDFCOLLECTION hide_devices;
309 ULONG i;
311 UNREFERENCED_PARAMETER(driver);
313 FUNCTION_ENTER();
315 WdfCollectionCreate(WDF_NO_OBJECT_ATTRIBUTES, &hide_devices);
316 status = WdfDriverOpenParametersRegistryKey(driver, KEY_QUERY_VALUE, WDF_NO_OBJECT_ATTRIBUTES, &param_key);
317 if (NT_SUCCESS(status))
318 {
319 status = WdfRegistryQueryMultiString(param_key, &hide_devices_name, WDF_NO_OBJECT_ATTRIBUTES, hide_devices);
320 if (!NT_SUCCESS(status))
321 {
322 KdPrint(("Error reading parameters/hide_devices value %08x\n", status));
323 }
324 WdfRegistryClose(param_key);
325 }
326 else
327 {
328 KdPrint(("Error opening parameters key %08x\n", status));
329 }
330 status = WdfFdoInitAllocAndQueryProperty(device_init, DevicePropertyDeviceDescription, NonPagedPool, WDF_NO_OBJECT_ATTRIBUTES, &memory);
331 if (NT_SUCCESS(status))
332 {
333 device_description = WdfMemoryGetBuffer(memory, NULL);
334 }
335 else
336 {
337 device_description = L"<unknown device>";
338 }
340 for (i = 0; i < WdfCollectionGetCount(hide_devices); i++)
341 {
342 WDFSTRING wdf_string = WdfCollectionGetItem(hide_devices, i);
343 UNICODE_STRING unicode_string;
344 WdfStringGetUnicodeString(wdf_string, &unicode_string);
345 if (XenPci_IdSuffixMatches(device_init, unicode_string.Buffer))
346 {
347 hide_required = TRUE;
348 break;
349 }
350 }
351 #if 0
352 /* hide only specific devices */
353 if (XenPci_IdSuffixMatches(device_init, L"VEN_8086&DEV_7010")) // Qemu IDE
354 {
355 hide_required = TRUE;
356 }
357 else if (XenPci_IdSuffixMatches(device_init, L"VEN_1000&DEV_0012"))// Qemu SCSI
358 {
359 hide_required = TRUE;
360 }
361 else if (XenPci_IdSuffixMatches(device_init, L"VEN_10EC&DEV_8139")) // Qemu Network
362 {
363 hide_required = TRUE;
364 }
365 #endif
366 if (!hide_required)
367 {
368 WdfObjectDelete(memory);
369 KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " (filter not required for %S)\n", device_description));
370 return STATUS_SUCCESS;
371 }
373 KdPrint((__DRIVER_NAME " Installing Filter for %S\n", device_description));
375 WdfFdoInitSetFilter(device_init);
376 WdfDeviceInitSetDeviceType(device_init, FILE_DEVICE_UNKNOWN);
377 WdfDeviceInitSetExclusive(device_init, FALSE);
379 WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnp_power_callbacks);
380 pnp_power_callbacks.EvtDevicePrepareHardware = XenHide_EvtDevicePrepareHardware;
381 WdfDeviceInitSetPnpPowerEventCallbacks(device_init, &pnp_power_callbacks);
383 WDF_OBJECT_ATTRIBUTES_INIT(&device_attributes);
384 status = WdfDeviceCreate(&device_init, &device_attributes, &device);
385 if (!NT_SUCCESS(status))
386 {
387 KdPrint(("Error creating device %08x\n", status));
388 WdfObjectDelete(memory);
389 FUNCTION_EXIT();
390 return status;
391 }
393 WdfObjectDelete(memory);
394 FUNCTION_EXIT();
396 return status;
397 }
399 static NTSTATUS
400 XenPci_EvtDeviceAdd(WDFDRIVER driver, PWDFDEVICE_INIT device_init)
401 {
402 if (XenPci_IdSuffixMatches(device_init, L"VEN_5853&DEV_0001"))
403 {
404 KdPrint((__DRIVER_NAME " Xen PCI device found - must be fdo\n"));
405 return XenPci_EvtDeviceAdd_XenPci(driver, device_init);
406 }
407 else if (qemu_filtered && !qemu_filtered_by_qemu)
408 {
409 KdPrint((__DRIVER_NAME " Xen PCI device not found - must be filter\n"));
410 return XenPci_EvtDeviceAdd_XenHide(driver, device_init);
411 }
412 else
413 return STATUS_SUCCESS;
414 }
416 ULONG qemu_protocol_version;
417 ULONG tpr_patch_requested;
418 extern PULONG InitSafeBootMode;
420 VOID
421 XenPci_HideQemuDevices()
422 {
423 qemu_filtered_by_qemu = FALSE;
424 if (READ_PORT_USHORT(XEN_IOPORT_MAGIC) == 0x49d2)
425 {
426 qemu_protocol_version = READ_PORT_UCHAR(XEN_IOPORT_VERSION);
427 KdPrint((__DRIVER_NAME " Version = %d\n", qemu_protocol_version));
428 switch(qemu_protocol_version)
429 {
430 case 1:
431 WRITE_PORT_USHORT(XEN_IOPORT_PRODUCT, XEN_PV_PRODUCT_NUMBER);
432 WRITE_PORT_ULONG(XEN_IOPORT_BUILD, XEN_PV_PRODUCT_BUILD);
433 if (READ_PORT_USHORT(XEN_IOPORT_MAGIC) != 0x49d2)
434 {
435 KdPrint((__DRIVER_NAME " Blacklisted\n"));
436 break;
437 }
438 /* fall through */
439 case 0:
440 qemu_filtered_by_qemu = TRUE;
441 WRITE_PORT_USHORT(XEN_IOPORT_DEVICE_MASK, QEMU_UNPLUG_ALL_IDE_DISKS|QEMU_UNPLUG_ALL_NICS);
442 KdPrint((__DRIVER_NAME " Disabled qemu devices\n"));
443 break;
444 default:
445 KdPrint((__DRIVER_NAME " Unknown qemu version %d\n", qemu_protocol_version));
446 break;
447 }
448 }
449 }
451 /*
452 make sure the load order is System Reserved, Dummy Group, WdfLoadGroup, XenPCI, Boot Bus Extender
453 */
455 static VOID
456 XenPci_FixLoadOrder()
457 {
458 NTSTATUS status;
459 WDFCOLLECTION old_load_order, new_load_order;
460 DECLARE_CONST_UNICODE_STRING(sgo_name, L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\ServiceGroupOrder");
461 DECLARE_CONST_UNICODE_STRING(list_name, L"List");
462 WDFKEY sgo_key;
463 ULONG i;
464 LONG dummy_group_index = -1;
465 LONG boot_bus_extender_index = -1;
466 LONG xenpci_group_index = -1;
467 LONG wdf_load_group_index = -1;
468 DECLARE_CONST_UNICODE_STRING(dummy_group_name, L"Dummy Group");
469 DECLARE_CONST_UNICODE_STRING(wdf_load_group_name, L"WdfLoadGroup");
470 DECLARE_CONST_UNICODE_STRING(xenpci_group_name, L"XenPCI Group");
471 DECLARE_CONST_UNICODE_STRING(boot_bus_extender_name, L"Boot Bus Extender");
473 FUNCTION_ENTER();
475 status = WdfRegistryOpenKey(NULL, &sgo_name, KEY_QUERY_VALUE, WDF_NO_OBJECT_ATTRIBUTES, &sgo_key);
476 if (!NT_SUCCESS(status))
477 {
478 KdPrint((__DRIVER_NAME " Error opening ServiceGroupOrder key %08x\n", status));
479 return;
480 }
481 WdfCollectionCreate(WDF_NO_OBJECT_ATTRIBUTES, &old_load_order);
482 WdfCollectionCreate(WDF_NO_OBJECT_ATTRIBUTES, &new_load_order);
483 status = WdfRegistryQueryMultiString(sgo_key, &list_name, WDF_NO_OBJECT_ATTRIBUTES, old_load_order);
484 if (!NT_SUCCESS(status))
485 {
486 KdPrint((__DRIVER_NAME " Error reading ServiceGroupOrder\\List value %08x\n", status));
487 WdfObjectDelete(new_load_order);
488 WdfObjectDelete(old_load_order);
489 return;
490 }
491 KdPrint((__DRIVER_NAME " Current Order:\n"));
492 for (i = 0; i < WdfCollectionGetCount(old_load_order); i++)
493 {
494 WDFOBJECT ws = WdfCollectionGetItem(old_load_order, i);
495 UNICODE_STRING val;
496 WdfStringGetUnicodeString(ws, &val);
497 if (!RtlCompareUnicodeString(&val, &dummy_group_name, TRUE))
498 dummy_group_index = (ULONG)i;
499 if (!RtlCompareUnicodeString(&val, &wdf_load_group_name, TRUE))
500 wdf_load_group_index = (ULONG)i;
501 if (!RtlCompareUnicodeString(&val, &xenpci_group_name, TRUE))
502 xenpci_group_index = (ULONG)i;
503 if (!RtlCompareUnicodeString(&val, &boot_bus_extender_name, TRUE))
504 boot_bus_extender_index = (ULONG)i;
505 KdPrint((__DRIVER_NAME " %wZ\n", &val));
506 }
507 KdPrint((__DRIVER_NAME " dummy_group_index = %d\n", dummy_group_index));
508 KdPrint((__DRIVER_NAME " wdf_load_group_index = %d\n", wdf_load_group_index));
509 KdPrint((__DRIVER_NAME " xenpci_group_index = %d\n", xenpci_group_index));
510 KdPrint((__DRIVER_NAME " boot_bus_extender_index = %d\n", boot_bus_extender_index));
511 if (boot_bus_extender_index == -1)
512 {
513 WdfObjectDelete(new_load_order);
514 WdfObjectDelete(old_load_order);
515 WdfRegistryClose(sgo_key);
516 return; /* something is very wrong */
517 }
518 if (dummy_group_index == 1 && (wdf_load_group_index == -1 ||
519 (dummy_group_index < wdf_load_group_index
520 && wdf_load_group_index < xenpci_group_index
521 && xenpci_group_index < boot_bus_extender_index)))
522 {
523 return; /* our work here is done */
524 }
525 for (i = 0; i < WdfCollectionGetCount(old_load_order); i++)
526 {
527 WDFOBJECT ws;
528 if (i == 1)
529 {
530 WDFSTRING tmp_wdf_string;
531 WdfStringCreate(&dummy_group_name, WDF_NO_OBJECT_ATTRIBUTES, &tmp_wdf_string);
532 WdfCollectionAdd(new_load_order, tmp_wdf_string);
533 WdfObjectDelete(tmp_wdf_string);
534 }
535 if (i == 1 && wdf_load_group_index != -1)
536 {
537 WDFSTRING tmp_wdf_string;
538 WdfStringCreate(&wdf_load_group_name, WDF_NO_OBJECT_ATTRIBUTES, &tmp_wdf_string);
539 WdfCollectionAdd(new_load_order, tmp_wdf_string);
540 WdfObjectDelete(tmp_wdf_string);
541 }
542 if (i == 1)
543 {
544 WDFSTRING tmp_wdf_string;
545 WdfStringCreate(&xenpci_group_name, WDF_NO_OBJECT_ATTRIBUTES, &tmp_wdf_string);
546 WdfCollectionAdd(new_load_order, tmp_wdf_string);
547 WdfObjectDelete(tmp_wdf_string);
548 }
549 if (i == (ULONG)dummy_group_index || i == (ULONG)wdf_load_group_index || i == (ULONG)xenpci_group_index)
550 continue;
551 ws = WdfCollectionGetItem(old_load_order, i);
552 WdfCollectionAdd(new_load_order, ws);
553 }
554 WdfRegistryAssignMultiString(sgo_key, &list_name, new_load_order);
555 KdPrint((__DRIVER_NAME " New Order:\n"));
556 for (i = 0; i < WdfCollectionGetCount(new_load_order); i++)
557 {
558 WDFOBJECT ws = WdfCollectionGetItem(new_load_order, i);
559 UNICODE_STRING val;
560 WdfStringGetUnicodeString(ws, &val);
561 KdPrint((__DRIVER_NAME " %wZ\n", &val));
562 }
563 WdfObjectDelete(new_load_order);
564 WdfObjectDelete(old_load_order);
565 WdfRegistryClose(sgo_key);
567 FUNCTION_EXIT();
569 return;
570 }
572 NTSTATUS
573 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
574 {
575 NTSTATUS status = STATUS_SUCCESS;
576 WDF_DRIVER_CONFIG config;
577 WDFDRIVER driver;
578 PCONFIGURATION_INFORMATION conf_info;
579 WCHAR *SystemStartOptions;
580 UNICODE_STRING RegKeyName;
581 UNICODE_STRING RegValueName;
582 HANDLE RegHandle;
583 OBJECT_ATTRIBUTES RegObjectAttributes;
584 char Buf[300];// Sometimes bigger then 200 if system reboot from crash
585 ULONG BufLen = 300;
586 PKEY_VALUE_PARTIAL_INFORMATION KeyPartialValue;
588 UNREFERENCED_PARAMETER(RegistryPath);
590 KdPrint((__DRIVER_NAME " " VER_FILEVERSION_STR "\n"));
592 FUNCTION_ENTER();
594 status = AuxKlibInitialize();
595 if(!NT_SUCCESS(status))
596 {
597 KdPrint((__DRIVER_NAME " AuxKlibInitialize failed %08x - expect a crash soon\n", status));
598 }
600 XenPci_HookDbgPrint();
602 XenPci_FixLoadOrder();
604 RtlInitUnicodeString(&RegKeyName, L"\\Registry\\Machine\\System\\CurrentControlSet\\Control");
605 InitializeObjectAttributes(&RegObjectAttributes, &RegKeyName, OBJ_CASE_INSENSITIVE, NULL, NULL);
606 status = ZwOpenKey(&RegHandle, KEY_READ, &RegObjectAttributes);
607 if(!NT_SUCCESS(status))
608 {
609 KdPrint((__DRIVER_NAME " ZwOpenKey returned %08x\n", status));
610 }
612 RtlInitUnicodeString(&RegValueName, L"SystemStartOptions");
613 status = ZwQueryValueKey(RegHandle, &RegValueName, KeyValuePartialInformation, Buf, BufLen, &BufLen);
614 if(!NT_SUCCESS(status))
615 {
616 KdPrint((__DRIVER_NAME " ZwQueryKeyValue returned %08x\n", status));
617 }
618 else
619 ZwClose(RegHandle);
620 KeyPartialValue = (PKEY_VALUE_PARTIAL_INFORMATION)Buf;
621 SystemStartOptions = (WCHAR *)KeyPartialValue->Data;
623 KdPrint((__DRIVER_NAME " SystemStartOptions = %S\n", SystemStartOptions));
625 if (wcsstr(SystemStartOptions, L"PATCHTPR"))
626 {
627 WDFKEY memory_key;
628 UNICODE_STRING verifier_key_name;
629 UNICODE_STRING verifier_value_name;
630 ULONG verifier_value;
632 KdPrint((__DRIVER_NAME " PATCHTPR found\n"));
634 RtlInitUnicodeString(&verifier_key_name, L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\Session Manager\\Memory Management");
635 status = WdfRegistryOpenKey(NULL, &verifier_key_name, KEY_READ, WDF_NO_OBJECT_ATTRIBUTES, &memory_key);
636 if (!NT_SUCCESS(status))
637 {
638 tpr_patch_requested = TRUE;
639 }
640 else
641 {
642 RtlInitUnicodeString(&verifier_value_name, L"VerifyDriverLevel");
643 status = WdfRegistryQueryULong(memory_key, &verifier_value_name, &verifier_value);
644 if (NT_SUCCESS(status) && verifier_value != 0)
645 {
646 KdPrint((__DRIVER_NAME " Verifier active - not patching\n"));
647 }
648 else
649 {
650 tpr_patch_requested = TRUE;
651 }
652 WdfRegistryClose(memory_key);
653 }
654 }
656 if (wcsstr(SystemStartOptions, L"NOGPLPV"))
657 KdPrint((__DRIVER_NAME " NOGPLPV found\n"));
658 conf_info = IoGetConfigurationInformation();
659 if ((conf_info == NULL || conf_info->DiskCount == 0)
660 && !wcsstr(SystemStartOptions, L"NOGPLPV")
661 && !*InitSafeBootMode)
662 {
663 qemu_filtered = TRUE;
664 /* see if the qemu method of disabling the PCI devices exists */
665 XenPci_HideQemuDevices();
666 }
668 if (qemu_filtered)
669 KdPrint((__DRIVER_NAME " PV Devices Active\n"));
670 else
671 KdPrint((__DRIVER_NAME " PV Devices Inactive\n"));
673 WDF_DRIVER_CONFIG_INIT(&config, XenPci_EvtDeviceAdd);
674 status = WdfDriverCreate(DriverObject, RegistryPath, WDF_NO_OBJECT_ATTRIBUTES, &config, &driver);
676 if (!NT_SUCCESS(status)) {
677 KdPrint((__DRIVER_NAME " WdfDriverCreate failed with status 0x%x\n", status));
678 }
680 FUNCTION_EXIT();
682 return status;
683 }