win-pvdrivers

changeset 333:0b08bdc622a6

major changes to xenhide. Now actually hides the qemu devices once more.
author James Harper <james.harper@bendigoit.com.au>
date Sun Jun 22 14:45:50 2008 +1000 (2008-06-22)
parents ee215c1108eb
children 506fe9b4c642
files common.inc xenhide/xenhide.c xenhide/xenhide.h xenpci/xenpci.inx
line diff
     1.1 --- a/common.inc	Thu Jun 19 11:11:03 2008 +1000
     1.2 +++ b/common.inc	Sun Jun 22 14:45:50 2008 +1000
     1.3 @@ -1,4 +1,4 @@
     1.4 -VERSION=0.9.10.0
     1.5 +VERSION=0.9.10.1
     1.6  TARGETPATH=..\Target\$(DDK_TARGET_OS)
     1.7  MSC_WARNING_LEVEL=/W4
     1.8  INCLUDES = ..\common\include;..\common\include\public
     2.1 --- a/xenhide/xenhide.c	Thu Jun 19 11:11:03 2008 +1000
     2.2 +++ b/xenhide/xenhide.c	Sun Jun 22 14:45:50 2008 +1000
     2.3 @@ -38,6 +38,7 @@ XenHide_AddDevice();
     2.4  #endif
     2.5  
     2.6  static BOOLEAN gplpv;
     2.7 +static XENHIDE_DRIVER_DATA xenhide_global_data;
     2.8  
     2.9  static NTSTATUS
    2.10  XenHide_Power(PDEVICE_OBJECT device_object, PIRP irp)
    2.11 @@ -155,11 +156,60 @@ DriverEntry(PDRIVER_OBJECT DriverObject,
    2.12    DriverObject->MajorFunction[IRP_MJ_POWER] = XenHide_Power;
    2.13    DriverObject->DriverExtension->AddDevice = XenHide_AddDevice;
    2.14  
    2.15 +  RtlZeroMemory(&xenhide_global_data, sizeof(XENHIDE_DRIVER_DATA));
    2.16 +
    2.17 +  InitializeListHead(&xenhide_global_data.hide_list_head);
    2.18 +  KeInitializeSpinLock(&xenhide_global_data.hide_list_lock);
    2.19 +  KeInitializeEvent(&xenhide_global_data.hide_list_event, SynchronizationEvent, FALSE);
    2.20 +
    2.21    KdPrint((__DRIVER_NAME " <-- DriverEntry\n"));
    2.22  
    2.23    return status;
    2.24  }
    2.25  
    2.26 +static BOOLEAN
    2.27 +XenHide_IdSuffixMatches(PDEVICE_OBJECT pdo, PWCHAR matching_id)
    2.28 +{
    2.29 +  NTSTATUS status;
    2.30 +  ULONG remaining;
    2.31 +  ULONG string_length;
    2.32 +  WCHAR ids[512];
    2.33 +  PWCHAR ptr;
    2.34 +  ULONG ids_length;
    2.35 +  int i;
    2.36 +  
    2.37 +  for (i = 0; i < 2; i++)
    2.38 +  {
    2.39 +    if (i == 0)
    2.40 +      status = IoGetDeviceProperty(pdo, DevicePropertyCompatibleIDs, sizeof(ids), ids, &ids_length);
    2.41 +    else
    2.42 +      status = IoGetDeviceProperty(pdo, DevicePropertyHardwareID, sizeof(ids), ids, &ids_length);
    2.43 +      
    2.44 +    if (!NT_SUCCESS(status))
    2.45 +    {
    2.46 +      KdPrint((__DRIVER_NAME "     i = %d, status = %x, ids_length = %d\n", i, status, ids_length));
    2.47 +      continue;
    2.48 +    }
    2.49 +    
    2.50 +    remaining = ids_length / 2;
    2.51 +    for (ptr = ids; *ptr != 0; ptr += string_length + 1)
    2.52 +    {
    2.53 +      RtlStringCchLengthW(ptr, remaining, (size_t *)&string_length);
    2.54 +      remaining -= string_length - 1;
    2.55 +      if (string_length >= wcslen(matching_id))
    2.56 +      {
    2.57 +        ptr += string_length - wcslen(matching_id);
    2.58 +        string_length -= wcslen(matching_id);
    2.59 +      }
    2.60 +      KdPrint((__DRIVER_NAME "     Comparing '%S' and '%S'\n", ptr, matching_id));
    2.61 +      if (wcscmp(ptr, matching_id) == 0)
    2.62 +       return TRUE;
    2.63 +    }
    2.64 +  }
    2.65 +  KdPrint((__DRIVER_NAME "     No match\n"));  
    2.66 +  return FALSE;
    2.67 +}
    2.68 +
    2.69  static NTSTATUS
    2.70  XenHide_AddDevice(
    2.71    PDRIVER_OBJECT DriverObject,
    2.72 @@ -170,43 +220,77 @@ XenHide_AddDevice(
    2.73    PDEVICE_OBJECT deviceObject = NULL;
    2.74    PXENHIDE_DEVICE_DATA xhdd;
    2.75    ULONG length;
    2.76 -  WCHAR buffer[256];
    2.77 -//  size_t StrLen;
    2.78 -//  int Match;
    2.79 -//  PWCHAR Ptr;
    2.80    GUID bus_type;
    2.81 +  WCHAR device_description[256];
    2.82 +  KIRQL old_irql;
    2.83 +  USHORT hide_type;
    2.84 +  PXENHIDE_HIDE_LIST_ENTRY list_entry;
    2.85  
    2.86 -//  KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
    2.87 +  KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
    2.88  
    2.89 -  length = 256;
    2.90 -  status = IoGetDeviceProperty(PhysicalDeviceObject, DevicePropertyDeviceDescription, length, buffer, &length);
    2.91 -//  if (!NT_SUCCESS(status))
    2.92 -//    KdPrint((__DRIVER_NAME "     (failed to get DevicePropertyDeviceDescription %08x)\n", status));
    2.93 -//  else
    2.94 -//    KdPrint((__DRIVER_NAME "     Description = %S\n", buffer));
    2.95 +  
    2.96 +  length = 512;
    2.97 +  status = IoGetDeviceProperty(PhysicalDeviceObject, DevicePropertyDeviceDescription, length, device_description, &length);
    2.98 +  if (!NT_SUCCESS(status))
    2.99 +  {
   2.100 +    device_description[0] = 0;
   2.101 +  }
   2.102 +
   2.103 +  KdPrint((__DRIVER_NAME "     Checking '%S'\n", device_description));
   2.104  
   2.105    length = sizeof(GUID);
   2.106    status = IoGetDeviceProperty(PhysicalDeviceObject, DevicePropertyBusTypeGuid, length, &bus_type, &length);
   2.107    if (!NT_SUCCESS(status))
   2.108    {
   2.109 -//    KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " (failed to get DevicePropertyBusTypeGuid %08x)\n", status));
   2.110 +    RtlZeroMemory(&bus_type, sizeof(GUID));
   2.111 +  }
   2.112 +
   2.113 +  hide_type = XENHIDE_TYPE_NONE;
   2.114 +  if (gplpv)
   2.115 +  {
   2.116 +    /* hide only specific devices */
   2.117 +    if (XenHide_IdSuffixMatches(PhysicalDeviceObject, L"VEN_8086&DEV_7010")
   2.118 +      || XenHide_IdSuffixMatches(PhysicalDeviceObject, L"VEN_10EC&DEV_8139"))
   2.119 +    {
   2.120 +      hide_type = XENHIDE_TYPE_DEVICE;
   2.121 +    }
   2.122 +    else if (XenHide_IdSuffixMatches(PhysicalDeviceObject, L"PNP0A03"))
   2.123 +    {
   2.124 +      hide_type = XENHIDE_TYPE_PCI_BUS;
   2.125 +    }
   2.126 +  }
   2.127 +  else
   2.128 +  {
   2.129 +    /* hide everything on the xen bus */
   2.130 +    if (memcmp(&bus_type, &GUID_BUS_TYPE_XEN, sizeof(GUID)) == 0)
   2.131 +      hide_type = XENHIDE_TYPE_DEVICE;
   2.132 +  }
   2.133 +
   2.134 +  if (hide_type == XENHIDE_TYPE_NONE)
   2.135 +  {
   2.136 +    KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " (filter not required for %S)\n", device_description));
   2.137      return STATUS_SUCCESS;
   2.138    }
   2.139  
   2.140 -  if (!gplpv && memcmp(&bus_type, &GUID_BUS_TYPE_XEN, sizeof(GUID)) != 0)
   2.141 +  KdPrint((__DRIVER_NAME "     Installing Filter for %S\n", device_description));
   2.142 +
   2.143 +  if (gplpv && hide_type == XENHIDE_TYPE_DEVICE)
   2.144    {
   2.145 -//    KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " (gplpv == FALSE && bus_type != GUID_BUS_TYPE_XEN)\n"));
   2.146 -    return STATUS_SUCCESS;
   2.147 +    KeAcquireSpinLock(&xenhide_global_data.hide_list_lock, &old_irql);
   2.148 +    list_entry = ExAllocatePoolWithTag(PagedPool, sizeof(XENHIDE_HIDE_LIST_ENTRY), XENHIDE_POOL_TAG);
   2.149 +    list_entry->pdo = PhysicalDeviceObject;
   2.150 +    InsertTailList(&xenhide_global_data.hide_list_head, (PLIST_ENTRY)list_entry);
   2.151 +    KeReleaseSpinLock(&xenhide_global_data.hide_list_lock, old_irql);
   2.152 +    KeSetEvent(&xenhide_global_data.hide_list_event, IO_NO_INCREMENT, FALSE);
   2.153 +    ASSERT(xenhide_global_data.pci_bus_pdo);
   2.154 +    IoInvalidateDeviceRelations(xenhide_global_data.pci_bus_pdo, BusRelations);
   2.155    }
   2.156 -  
   2.157 -  if (gplpv && memcmp(&bus_type, &GUID_BUS_TYPE_PCI, sizeof(GUID)) != 0)
   2.158 +  else if (hide_type == XENHIDE_TYPE_PCI_BUS)
   2.159    {
   2.160 -//    KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " (gplpv == TRUE && bus_type != GUID_BUS_TYPE_PCI)\n"));
   2.161 -    return STATUS_SUCCESS;
   2.162 +    ASSERT(!xenhide_global_data.pci_bus_pdo);
   2.163 +    xenhide_global_data.pci_bus_pdo = PhysicalDeviceObject;
   2.164    }
   2.165  
   2.166 -//  KdPrint((__DRIVER_NAME "     Installing Filter\n"));
   2.167 -
   2.168    status = IoCreateDevice (DriverObject,
   2.169      sizeof(XENHIDE_DEVICE_DATA),
   2.170      NULL,
   2.171 @@ -217,6 +301,8 @@ XenHide_AddDevice(
   2.172  
   2.173    xhdd = (PXENHIDE_DEVICE_DATA)deviceObject->DeviceExtension;
   2.174  
   2.175 +  xhdd->hide_type = hide_type;
   2.176 +  
   2.177    xhdd->lower_do = IoAttachDeviceToDeviceStack(
   2.178      deviceObject, PhysicalDeviceObject);
   2.179    deviceObject->Flags |= xhdd->lower_do->Flags;
   2.180 @@ -230,7 +316,7 @@ XenHide_AddDevice(
   2.181  
   2.182    deviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
   2.183  
   2.184 -//  KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
   2.185 +  KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
   2.186  
   2.187    return STATUS_SUCCESS;
   2.188  }
   2.189 @@ -285,9 +371,7 @@ XenHide_SendAndWaitForIrp(PDEVICE_OBJECT
   2.190  
   2.191    if (status == STATUS_PENDING)
   2.192    {
   2.193 -//    KdPrint((__DRIVER_NAME "     waiting ...\n"));
   2.194      KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
   2.195 -//    KdPrint((__DRIVER_NAME "     ... done\n"));
   2.196      status = irp->IoStatus.Status;
   2.197    }
   2.198  
   2.199 @@ -302,31 +386,74 @@ XenHide_Pnp(PDEVICE_OBJECT device_object
   2.200    NTSTATUS status = STATUS_SUCCESS;
   2.201    PIO_STACK_LOCATION stack;
   2.202    PXENHIDE_DEVICE_DATA xhdd = (PXENHIDE_DEVICE_DATA)device_object->DeviceExtension;
   2.203 +  PDEVICE_RELATIONS relations;
   2.204 +  PXENHIDE_HIDE_LIST_ENTRY list_entry;
   2.205 +  ULONG i, j;
   2.206 +  KIRQL old_irql;
   2.207  
   2.208 -//  KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
   2.209 +  KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
   2.210  
   2.211    stack = IoGetCurrentIrpStackLocation(irp);
   2.212  
   2.213    switch (stack->MinorFunction) {
   2.214    case IRP_MN_START_DEVICE:
   2.215 -    status = irp->IoStatus.Status = STATUS_UNSUCCESSFUL;
   2.216 -    IoCompleteRequest(irp, IO_NO_INCREMENT);
   2.217 +    KdPrint((__DRIVER_NAME "     IRP_MN_START_DEVICE\n"));
   2.218 +    if (xhdd->hide_type == XENHIDE_TYPE_DEVICE)
   2.219 +    {
   2.220 +      KdPrint((__DRIVER_NAME "     hide_type == XENHIDE_TYPE_DEVICE\n"));
   2.221 +      status = irp->IoStatus.Status = STATUS_UNSUCCESSFUL;
   2.222 +      IoCompleteRequest(irp, IO_NO_INCREMENT);
   2.223 +    }
   2.224 +    else
   2.225 +    {
   2.226 +      KdPrint((__DRIVER_NAME "     hide_type != XENHIDE_TYPE_DEVICE\n"));
   2.227 +      IoSkipCurrentIrpStackLocation(irp);
   2.228 +      status = IoCallDriver(xhdd->lower_do, irp);
   2.229 +    }
   2.230      break;
   2.231 -  case IRP_MN_QUERY_CAPABILITIES:
   2.232 -//    KdPrint((__DRIVER_NAME "     IRP_MN_QUERY_CAPABILITIES\n"));
   2.233 -    stack->Parameters.DeviceCapabilities.Capabilities->NoDisplayInUI = 1;
   2.234 -    status = XenHide_SendAndWaitForIrp(device_object, irp);
   2.235 -    status = irp->IoStatus.Status = STATUS_SUCCESS;
   2.236 -    IoCompleteRequest(irp, IO_NO_INCREMENT);
   2.237 -//    KdPrint((__DRIVER_NAME " <-- " __FUNCTION__"\n"));
   2.238 -    return status;
   2.239 +  case IRP_MN_QUERY_DEVICE_RELATIONS:
   2.240 +    if (xhdd->hide_type == XENHIDE_TYPE_PCI_BUS && stack->Parameters.QueryDeviceRelations.Type == BusRelations)
   2.241 +    {
   2.242 +      KdPrint((__DRIVER_NAME "     IRP_MN_QUERY_DEVICE_RELATIONS - BusRelations\n"));
   2.243 +      IoMarkIrpPending(irp);
   2.244 +      status = XenHide_SendAndWaitForIrp(device_object, irp);
   2.245 +      relations = (PDEVICE_RELATIONS)irp->IoStatus.Information;
   2.246 +      for (i = 0, j = 0; i < relations->Count; i++)
   2.247 +      {
   2.248 +        if (i != j)
   2.249 +          relations->Objects[j] = relations->Objects[i];
   2.250 +        KeAcquireSpinLock(&xenhide_global_data.hide_list_lock, &old_irql);
   2.251 +        list_entry = (PXENHIDE_HIDE_LIST_ENTRY)xenhide_global_data.hide_list_head.Flink;
   2.252 +        while (list_entry != (PXENHIDE_HIDE_LIST_ENTRY)&xenhide_global_data.hide_list_head)
   2.253 +        {
   2.254 +          if (relations->Objects[i] == list_entry->pdo)
   2.255 +          {
   2.256 +            KdPrint((__DRIVER_NAME "     Hiding %p\n", relations->Objects[i]));
   2.257 +            break;
   2.258 +          }
   2.259 +          list_entry = (PXENHIDE_HIDE_LIST_ENTRY)list_entry->entry.Flink;
   2.260 +        }
   2.261 +        if (list_entry == (PXENHIDE_HIDE_LIST_ENTRY)&xenhide_global_data.hide_list_head)
   2.262 +          j++;
   2.263 +        KeReleaseSpinLock(&xenhide_global_data.hide_list_lock, old_irql);
   2.264 +      }
   2.265 +      relations->Count = j;
   2.266 +      irp->IoStatus.Status = status;
   2.267 +      IoCompleteRequest (irp, IO_NO_INCREMENT);
   2.268 +    }
   2.269 +    else
   2.270 +    {
   2.271 +      IoSkipCurrentIrpStackLocation(irp);
   2.272 +      status = IoCallDriver(xhdd->lower_do, irp);
   2.273 +    }
   2.274 +    break;
   2.275    default:
   2.276      IoSkipCurrentIrpStackLocation(irp);
   2.277      status = IoCallDriver(xhdd->lower_do, irp);
   2.278      break;
   2.279    }
   2.280  
   2.281 -//  KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " (returning with status %08x)\n", status));
   2.282 +  KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ " (returning with status %08x)\n", status));
   2.283  
   2.284    return status;
   2.285  }
     3.1 --- a/xenhide/xenhide.h	Thu Jun 19 11:11:03 2008 +1000
     3.2 +++ b/xenhide/xenhide.h	Sun Jun 22 14:45:50 2008 +1000
     3.3 @@ -32,19 +32,37 @@ Foundation, Inc., 51 Franklin Street, Fi
     3.4  
     3.5  #include <xen_guids.h>
     3.6  
     3.7 +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
     3.8 +
     3.9  #define XENHIDE_POOL_TAG (ULONG) 'XHID'
    3.10  
    3.11  //{CD433FE7-954F-4D51-BE29-D8A38DFA1108}
    3.12  //DEFINE_GUID(GUID_XENHIDE_IFACE, 0xCD433FE7, 0x954F, 0x4D51, 0xBE, 0x29, 0xD8, 0xA3, 0x8D, 0xFA, 0x11, 0x08);
    3.13  
    3.14 -#define XENHIDE_TYPE_PCI 1
    3.15 -#define XENHIDE_TYPE_HIDE 2
    3.16 +#define XENHIDE_TYPE_NONE 0
    3.17 +#define XENHIDE_TYPE_DEVICE 1
    3.18 +#define XENHIDE_TYPE_PCI_BUS 2
    3.19  
    3.20 -struct {
    3.21 +typedef struct {
    3.22    PDEVICE_OBJECT filter_do;
    3.23    PDEVICE_OBJECT pdo;
    3.24    PDEVICE_OBJECT lower_do;
    3.25    IO_REMOVE_LOCK RemoveLock;
    3.26 -} typedef XENHIDE_DEVICE_DATA, *PXENHIDE_DEVICE_DATA;
    3.27 +  USHORT hide_type;
    3.28 +} XENHIDE_DEVICE_DATA, *PXENHIDE_DEVICE_DATA;
    3.29 +
    3.30 +typedef struct
    3.31 +{
    3.32 +  LIST_ENTRY entry;
    3.33 +  PDEVICE_OBJECT pdo;
    3.34 +} XENHIDE_HIDE_LIST_ENTRY,  *PXENHIDE_HIDE_LIST_ENTRY;
    3.35 +
    3.36 +typedef struct
    3.37 +{
    3.38 +  PDEVICE_OBJECT pci_bus_pdo;
    3.39 +  LIST_ENTRY hide_list_head;
    3.40 +  KSPIN_LOCK hide_list_lock;
    3.41 +  KEVENT hide_list_event;
    3.42 +} XENHIDE_DRIVER_DATA, *PXENHIDE_DRIVER_DATA;
    3.43  
    3.44  #endif
     4.1 --- a/xenpci/xenpci.inx	Thu Jun 19 11:11:03 2008 +1000
     4.2 +++ b/xenpci/xenpci.inx	Sun Jun 22 14:45:50 2008 +1000
     4.3 @@ -64,6 +64,10 @@ StartType      = 0
     4.4  ErrorControl   = 1
     4.5  LoadOrderGroup = Boot Bus Extender
     4.6  ServiceBinary  = %12%\xenhide.sys
     4.7 +AddReg         = XenHide_Service_AddReg
     4.8 +
     4.9 +[XenHide_Service_AddReg]
    4.10 +HKLM,SYSTEM\CurrentControlSet\Control\Class\{4D36E97D-E325-11CE-BFC1-08002BE10318},UpperFilters,0x00010008,XenHide
    4.11  
    4.12  [Strings]
    4.13  XenGplPv = "Xen GPL PV Driver Developers"