win-pvdrivers

changeset 809:ff77e2f26a3e

Improvements to textmode install
author James Harper <james.harper@bendigoit.com.au>
date Tue Jul 27 11:37:50 2010 +1000 (2010-07-27)
parents 5d6bc6c93112
children 1293cb31aa0b
files xenpci/xenpci.c xenpci/xenpci.inx xenpci/xenpci_fdo.c xenpci/xenpci_highsync.c
line diff
     1.1 --- a/xenpci/xenpci.c	Tue Jul 27 11:36:58 2010 +1000
     1.2 +++ b/xenpci/xenpci.c	Tue Jul 27 11:37:50 2010 +1000
     1.3 @@ -491,6 +491,7 @@ XenPci_FixLoadOrder()
     1.4      && wdf_load_group_index < xenpci_group_index
     1.5      && xenpci_group_index < boot_bus_extender_index)))
     1.6    {
     1.7 +    FUNCTION_EXIT();
     1.8      return; /* our work here is done */
     1.9    }
    1.10    for (i = 0; i < WdfCollectionGetCount(old_load_order); i++)
    1.11 @@ -558,161 +559,144 @@ DriverEntry(PDRIVER_OBJECT DriverObject,
    1.12    NTSTATUS status = STATUS_SUCCESS;
    1.13    WDF_DRIVER_CONFIG config;
    1.14    WDFDRIVER driver;
    1.15 +  WDF_OBJECT_ATTRIBUTES parent_attributes;
    1.16    PCONFIGURATION_INFORMATION conf_info;
    1.17 -  WCHAR *SystemStartOptions;
    1.18 -  UNICODE_STRING RegKeyName;
    1.19 -  UNICODE_STRING RegValueName;
    1.20 -  HANDLE RegHandle;
    1.21 -  OBJECT_ATTRIBUTES RegObjectAttributes;
    1.22 -  char Buf[300];// Sometimes bigger then 200 if system reboot from crash
    1.23 -  ULONG BufLen = 300;
    1.24 -  PKEY_VALUE_PARTIAL_INFORMATION KeyPartialValue;
    1.25 +  WDFKEY control_key;
    1.26    WDFKEY param_key;
    1.27 +  ULONG always_patch = 0;
    1.28 +  ULONG always_hide = 0;
    1.29 +  DECLARE_CONST_UNICODE_STRING(control_key_name, L"\\Registry\\Machine\\System\\CurrentControlSet\\Control");
    1.30 +  DECLARE_CONST_UNICODE_STRING(system_start_options_name, L"SystemStartOptions");
    1.31 +  DECLARE_CONST_UNICODE_STRING(txt_always_hide_name, L"txt_hide_qemu_always");
    1.32 +  DECLARE_CONST_UNICODE_STRING(hide_devices_name, L"hide_devices");
    1.33 +  DECLARE_CONST_UNICODE_STRING(txt_always_patch_name, L"txt_patch_tpr_always");
    1.34 +  WDFSTRING wdf_system_start_options;
    1.35 +  UNICODE_STRING system_start_options;
    1.36    
    1.37    UNREFERENCED_PARAMETER(RegistryPath);
    1.38  
    1.39 +  FUNCTION_ENTER();
    1.40 +
    1.41    KdPrint((__DRIVER_NAME " " VER_FILEVERSION_STR "\n"));
    1.42  
    1.43 -  FUNCTION_ENTER();
    1.44 +  #if DBG
    1.45 +  XenPci_HookDbgPrint();
    1.46 +  #endif
    1.47 +  /* again after enabling DbgPrint hooking */
    1.48 +  KdPrint((__DRIVER_NAME " " VER_FILEVERSION_STR "\n"));
    1.49 +
    1.50 +  WDF_DRIVER_CONFIG_INIT(&config, XenPci_EvtDeviceAdd);
    1.51 +  config.EvtDriverUnload = XenPci_EvtDriverUnload;
    1.52 +  status = WdfDriverCreate(DriverObject, RegistryPath, WDF_NO_OBJECT_ATTRIBUTES, &config, &driver);
    1.53 +  if (!NT_SUCCESS(status))
    1.54 +  {
    1.55 +    KdPrint((__DRIVER_NAME "     WdfDriverCreate failed with status 0x%x\n", status));
    1.56 +    FUNCTION_EXIT();
    1.57 +    #if DBG
    1.58 +    XenPci_UnHookDbgPrint();
    1.59 +    #endif
    1.60 +    return status;
    1.61 +  }
    1.62 +  WDF_OBJECT_ATTRIBUTES_INIT(&parent_attributes);
    1.63 +  parent_attributes.ParentObject = driver;
    1.64 +  
    1.65 +  status = WdfDriverOpenParametersRegistryKey(driver, KEY_QUERY_VALUE, &parent_attributes, &param_key);
    1.66 +  if (!NT_SUCCESS(status))
    1.67 +  {
    1.68 +    KdPrint(("Error opening parameters key %08x\n", status));
    1.69 +    goto error;
    1.70 +  }
    1.71  
    1.72    status = AuxKlibInitialize();
    1.73    if(!NT_SUCCESS(status))
    1.74    {
    1.75 -    KdPrint((__DRIVER_NAME "     AuxKlibInitialize failed %08x - expect a crash soon\n", status));
    1.76 +    KdPrint((__DRIVER_NAME "     AuxKlibInitialize failed %08x\n", status));
    1.77 +    goto error;
    1.78    }
    1.79 -  
    1.80 -  #if DBG
    1.81 -  XenPci_HookDbgPrint();
    1.82 -  #endif
    1.83  
    1.84    XenPci_FixLoadOrder();
    1.85  
    1.86 -  RtlInitUnicodeString(&RegKeyName, L"\\Registry\\Machine\\System\\CurrentControlSet\\Control");
    1.87 -  InitializeObjectAttributes(&RegObjectAttributes, &RegKeyName, OBJ_CASE_INSENSITIVE, NULL, NULL);
    1.88 -  status = ZwOpenKey(&RegHandle, KEY_READ, &RegObjectAttributes);
    1.89 -  if(!NT_SUCCESS(status))
    1.90 -  {
    1.91 -    KdPrint((__DRIVER_NAME "     ZwOpenKey returned %08x\n", status));
    1.92 -  }
    1.93 -
    1.94 -  RtlInitUnicodeString(&RegValueName, L"SystemStartOptions");
    1.95 -  status = ZwQueryValueKey(RegHandle, &RegValueName, KeyValuePartialInformation, Buf, BufLen, &BufLen);
    1.96 -  if(!NT_SUCCESS(status))
    1.97 +  RtlInitUnicodeString(&system_start_options, L"failed to read");
    1.98 +  status = WdfRegistryOpenKey(NULL, &control_key_name, GENERIC_READ, &parent_attributes, &control_key);
    1.99 +  if (NT_SUCCESS(status))
   1.100    {
   1.101 -    KdPrint((__DRIVER_NAME "     ZwQueryKeyValue returned %08x\n", status));
   1.102 +    status = WdfStringCreate(NULL, &parent_attributes, &wdf_system_start_options);
   1.103 +    status = WdfRegistryQueryString(control_key, &system_start_options_name, wdf_system_start_options);
   1.104 +    if (NT_SUCCESS(status))
   1.105 +      WdfStringGetUnicodeString(wdf_system_start_options, &system_start_options);
   1.106    }
   1.107 -  else
   1.108 -    ZwClose(RegHandle);
   1.109 -  KeyPartialValue = (PKEY_VALUE_PARTIAL_INFORMATION)Buf;
   1.110 -  SystemStartOptions = (WCHAR *)KeyPartialValue->Data;
   1.111 +  WdfRegistryClose(control_key);
   1.112  
   1.113 -  KdPrint((__DRIVER_NAME "     SystemStartOptions = %S\n", SystemStartOptions));
   1.114 +  KdPrint((__DRIVER_NAME "     SystemStartOptions = %S\n", system_start_options));
   1.115    
   1.116 -  if (wcsstr(SystemStartOptions, L"PATCHTPR"))
   1.117 +  always_patch = 0;
   1.118 +  WdfRegistryQueryULong(param_key, &txt_always_patch_name, &always_patch);
   1.119 +  if (always_patch || (system_start_options.Buffer && wcsstr(system_start_options.Buffer, L"PATCHTPR")))
   1.120    {
   1.121 +    DECLARE_CONST_UNICODE_STRING(verifier_key_name, L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\Session Manager\\Memory Management");
   1.122      WDFKEY memory_key;
   1.123 -    UNICODE_STRING verifier_key_name;
   1.124 -    UNICODE_STRING verifier_value_name;
   1.125      ULONG verifier_value;
   1.126      
   1.127      KdPrint((__DRIVER_NAME "     PATCHTPR found\n"));
   1.128      
   1.129 -    RtlInitUnicodeString(&verifier_key_name, L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\Session Manager\\Memory Management");
   1.130 -    status = WdfRegistryOpenKey(NULL, &verifier_key_name, KEY_READ, WDF_NO_OBJECT_ATTRIBUTES, &memory_key);
   1.131 -    if (!NT_SUCCESS(status))
   1.132 +    tpr_patch_requested = TRUE;
   1.133 +    status = WdfRegistryOpenKey(NULL, &verifier_key_name, KEY_READ, &parent_attributes, &memory_key);
   1.134 +    if (NT_SUCCESS(status))
   1.135      {
   1.136 -      tpr_patch_requested = TRUE;
   1.137 -    }  
   1.138 -    else
   1.139 -    {
   1.140 -      RtlInitUnicodeString(&verifier_value_name, L"VerifyDriverLevel");
   1.141 +      DECLARE_CONST_UNICODE_STRING(verifier_value_name, L"VerifyDriverLevel");
   1.142        status = WdfRegistryQueryULong(memory_key, &verifier_value_name, &verifier_value);
   1.143        if (NT_SUCCESS(status) && verifier_value != 0)
   1.144        {
   1.145          KdPrint((__DRIVER_NAME "     Verifier active - not patching\n"));
   1.146 -      }
   1.147 -      else
   1.148 -      {
   1.149 -        tpr_patch_requested = TRUE;
   1.150 +        tpr_patch_requested = FALSE;
   1.151        }
   1.152        WdfRegistryClose(memory_key);
   1.153      }
   1.154    }
   1.155 -  
   1.156 -  WDF_DRIVER_CONFIG_INIT(&config, XenPci_EvtDeviceAdd);
   1.157 -  config.EvtDriverUnload = XenPci_EvtDriverUnload;
   1.158 -  status = WdfDriverCreate(DriverObject, RegistryPath, WDF_NO_OBJECT_ATTRIBUTES, &config, &driver);
   1.159 -
   1.160 -  WdfCollectionCreate(WDF_NO_OBJECT_ATTRIBUTES, &qemu_hide_devices);
   1.161 -
   1.162 -  if (!NT_SUCCESS(status)) {
   1.163 -    KdPrint((__DRIVER_NAME "     WdfDriverCreate failed with status 0x%x\n", status));
   1.164 -    FUNCTION_EXIT();
   1.165 -    return status;
   1.166 -  }
   1.167 -
   1.168 -  qemu_hide_flags_value = 0;
   1.169  
   1.170 -  if (wcsstr(SystemStartOptions, L"NOGPLPV"))
   1.171 -    KdPrint((__DRIVER_NAME "     NOGPLPV found\n"));
   1.172 -  conf_info = IoGetConfigurationInformation();
   1.173 -  
   1.174 -  status = WdfDriverOpenParametersRegistryKey(driver, KEY_QUERY_VALUE, WDF_NO_OBJECT_ATTRIBUTES, &param_key);
   1.175 -  if (NT_SUCCESS(status))
   1.176 +  WdfCollectionCreate(&parent_attributes, &qemu_hide_devices);
   1.177 +  WdfRegistryQueryULong(param_key, &txt_always_hide_name, &always_hide);
   1.178 +  conf_info = IoGetConfigurationInformation();      
   1.179 +  if (always_hide || ((conf_info == NULL || conf_info->DiskCount == 0)
   1.180 +      && !(system_start_options.Buffer && wcsstr(system_start_options.Buffer, L"NOGPLPV"))
   1.181 +      && !*InitSafeBootMode))
   1.182    {
   1.183 -    ULONG always_hide = 0;
   1.184 -    DECLARE_CONST_UNICODE_STRING(always_hide_name, L"hide_qemu_always");
   1.185 -    
   1.186 -    WdfRegistryQueryULong(param_key, &always_hide_name, &always_hide);
   1.187 -    if (always_hide || ((conf_info == NULL || conf_info->DiskCount == 0)
   1.188 -        && !wcsstr(SystemStartOptions, L"NOGPLPV")
   1.189 -        && !*InitSafeBootMode))
   1.190 +    if (!(system_start_options.Buffer && wcsstr(system_start_options.Buffer, L"GPLPVUSEFILTERHIDE")) && XenPci_CheckHideQemuDevices())
   1.191      {
   1.192 -      if (wcsstr(SystemStartOptions, L"GPLPVUSEFILTERHIDE") == 0 && XenPci_CheckHideQemuDevices())
   1.193 +      DECLARE_CONST_UNICODE_STRING(qemu_hide_flags_name, L"qemu_hide_flags");
   1.194 +      DECLARE_CONST_UNICODE_STRING(txt_qemu_hide_flags_name, L"txt_qemu_hide_flags");
   1.195 +      WDFCOLLECTION qemu_hide_flags;
   1.196 +      ULONG i;
   1.197 +
   1.198 +      WdfCollectionCreate(&parent_attributes, &qemu_hide_flags);
   1.199 +      WdfRegistryQueryMultiString(param_key, &qemu_hide_flags_name, &parent_attributes, qemu_hide_flags);
   1.200 +      WdfRegistryQueryMultiString(param_key, &txt_qemu_hide_flags_name, &parent_attributes, qemu_hide_flags);
   1.201 +      for (i = 0; i < WdfCollectionGetCount(qemu_hide_flags); i++)
   1.202        {
   1.203 -        DECLARE_CONST_UNICODE_STRING(qemu_hide_flags_name, L"qemu_hide_flags");
   1.204 -        WDFCOLLECTION qemu_hide_flags;
   1.205 -        ULONG i;
   1.206 -        
   1.207 -        WdfCollectionCreate(WDF_NO_OBJECT_ATTRIBUTES, &qemu_hide_flags);
   1.208 -        status = WdfRegistryQueryMultiString(param_key, &qemu_hide_flags_name, WDF_NO_OBJECT_ATTRIBUTES, qemu_hide_flags);
   1.209 -        if (!NT_SUCCESS(status))
   1.210 -        {
   1.211 -          KdPrint(("Error reading parameters/qemu_hide_flags value %08x\n", status));
   1.212 -        }
   1.213 -        else
   1.214 -        {
   1.215 -          for (i = 0; i < WdfCollectionGetCount(qemu_hide_flags); i++)
   1.216 -          {
   1.217 -            ULONG value;
   1.218 -            WDFSTRING wdf_string = WdfCollectionGetItem(qemu_hide_flags, i);
   1.219 -            UNICODE_STRING unicode_string;
   1.220 -            WdfStringGetUnicodeString(wdf_string, &unicode_string);
   1.221 -            KdPrint(("\n", status));
   1.222 -            status = RtlUnicodeStringToInteger(&unicode_string, 0, &value);
   1.223 -            qemu_hide_flags_value |= value;
   1.224 -          }
   1.225 -        }
   1.226 -        XenPci_HideQemuDevices();
   1.227 +        ULONG value;
   1.228 +        WDFSTRING wdf_string = WdfCollectionGetItem(qemu_hide_flags, i);
   1.229 +        UNICODE_STRING unicode_string;
   1.230 +        WdfStringGetUnicodeString(wdf_string, &unicode_string);
   1.231 +        status = RtlUnicodeStringToInteger(&unicode_string, 0, &value);
   1.232 +        qemu_hide_flags_value |= value;
   1.233        }
   1.234 -      else
   1.235 -      {
   1.236 -        DECLARE_CONST_UNICODE_STRING(hide_devices_name, L"hide_devices");
   1.237 -        status = WdfRegistryQueryMultiString(param_key, &hide_devices_name, WDF_NO_OBJECT_ATTRIBUTES, qemu_hide_devices);
   1.238 -        if (!NT_SUCCESS(status))
   1.239 -        {
   1.240 -          KdPrint(("Error reading parameters/hide_devices value %08x\n", status));
   1.241 -        }
   1.242 -      }
   1.243 +      WdfObjectDelete(qemu_hide_flags);
   1.244 +      XenPci_HideQemuDevices();
   1.245      }
   1.246 -    WdfRegistryClose(param_key);
   1.247 +    else
   1.248 +    {
   1.249 +      WdfRegistryQueryMultiString(param_key, &hide_devices_name, &parent_attributes, qemu_hide_devices);      
   1.250 +    }
   1.251    }
   1.252 -  else
   1.253 -  {
   1.254 -    KdPrint(("Error opening parameters key %08x\n", status));
   1.255 -  }
   1.256 +  WdfRegistryClose(param_key);
   1.257 +  FUNCTION_EXIT();
   1.258 +  return STATUS_SUCCESS;
   1.259  
   1.260 +error:
   1.261 +  #if DBG
   1.262 +  XenPci_UnHookDbgPrint();
   1.263 +  #endif
   1.264 +  KdPrint(("Failed, returning %08x\n", status));
   1.265    FUNCTION_EXIT();
   1.266 -
   1.267 -  return STATUS_SUCCESS;
   1.268 +  return status;
   1.269  }
     2.1 --- a/xenpci/xenpci.inx	Tue Jul 27 11:36:58 2010 +1000
     2.2 +++ b/xenpci/xenpci.inx	Tue Jul 27 11:37:50 2010 +1000
     2.3 @@ -65,6 +65,11 @@ HKLM,SYSTEM\CurrentControlSet\Control\Cl
     2.4  HKLM,SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318},UpperFilters,0x00018002,XenHide
     2.5  HKLM,SYSTEM\CurrentControlSet\Control\Class\{4D36E96A-E325-11CE-BFC1-08002BE10318},UpperFilters,0x00018002,XenHide
     2.6  HKLM,SYSTEM\CurrentControlSet\Control\Class\{4D36E97B-E325-11CE-BFC1-08002BE10318},UpperFilters,0x00018002,XenHide
     2.7 +; Remove anything that might have been added during text mode setup
     2.8 +HKR,"Parameters", "txt_qemu_hide_flags"
     2.9 +HKR,"Parameters", "txt_hide_qemu_always"
    2.10 +HKR,"Parameters", "txt_patch_tpr_always"
    2.11 +
    2.12  
    2.13  [DestinationDirs]
    2.14  XenPCI_Device_CoInstaller_CopyFiles = 11
     3.1 --- a/xenpci/xenpci_fdo.c	Tue Jul 27 11:36:58 2010 +1000
     3.2 +++ b/xenpci/xenpci_fdo.c	Tue Jul 27 11:37:50 2010 +1000
     3.3 @@ -731,6 +731,7 @@ XenPci_EvtDeviceD0Entry(WDFDEVICE device
     3.4      {
     3.5        XenPci_MapHalThenPatchKernel(xpdd);
     3.6        xpdd->tpr_patched = TRUE;
     3.7 +      xpdd->removable = FALSE;
     3.8      }
     3.9      GntTbl_Init(xpdd);
    3.10      EvtChn_Init(xpdd);
     4.1 --- a/xenpci/xenpci_highsync.c	Tue Jul 27 11:36:58 2010 +1000
     4.2 +++ b/xenpci/xenpci_highsync.c	Tue Jul 27 11:37:50 2010 +1000
     4.3 @@ -31,7 +31,8 @@ we need these intrinsics as even going t
     4.4  
     4.5  struct {
     4.6    volatile ULONG        do_spin;
     4.7 -  volatile LONG         nr_spinning;
     4.8 +  volatile LONG         nr_procs_at_dispatch_level;
     4.9 +  volatile LONG         nr_spinning_at_sync_level;
    4.10    KDPC                  dpcs[MAX_VIRT_CPUS];
    4.11    KEVENT                highsync_complete_event;
    4.12    KIRQL                 sync_level;
    4.13 @@ -57,9 +58,18 @@ XenPci_HighSyncCallFunction0(
    4.14  
    4.15    FUNCTION_ENTER();
    4.16    ActiveProcessorCount = (ULONG)KeNumberProcessors;
    4.17 +  InterlockedIncrement(&highsync_info->nr_procs_at_dispatch_level);
    4.18 +  if (highsync_info->sync_level > DISPATCH_LEVEL)
    4.19 +  {
    4.20 +    while (highsync_info->nr_procs_at_dispatch_level < (LONG)ActiveProcessorCount)
    4.21 +    {
    4.22 +      KeStallExecutionProcessor(1);
    4.23 +      KeMemoryBarrier();
    4.24 +    }
    4.25 +  }
    4.26    _disable(); //__asm cli;  
    4.27    KeRaiseIrql(highsync_info->sync_level, &old_irql);
    4.28 -  while (highsync_info->nr_spinning < (LONG)ActiveProcessorCount - 1)
    4.29 +  while (highsync_info->nr_spinning_at_sync_level < (LONG)ActiveProcessorCount - 1)
    4.30    {
    4.31      KeStallExecutionProcessor(1);
    4.32      KeMemoryBarrier();
    4.33 @@ -69,13 +79,13 @@ XenPci_HighSyncCallFunction0(
    4.34    _enable(); //__asm sti;
    4.35    highsync_info->do_spin = FALSE;
    4.36    KeMemoryBarrier();  
    4.37 -  
    4.38    /* wait for all the other processors to complete spinning, just in case it matters */
    4.39 -  while (highsync_info->nr_spinning)
    4.40 +  while (highsync_info->nr_spinning_at_sync_level)
    4.41    {
    4.42      KeStallExecutionProcessor(1);
    4.43      KeMemoryBarrier();
    4.44    }
    4.45 +  InterlockedDecrement(&highsync_info->nr_procs_at_dispatch_level);
    4.46    KeSetEvent(&highsync_info->highsync_complete_event, IO_NO_INCREMENT, FALSE);
    4.47  
    4.48    FUNCTION_EXIT();
    4.49 @@ -89,6 +99,7 @@ XenPci_HighSyncCallFunctionN(
    4.50    PVOID SystemArgument2)
    4.51  {
    4.52    highsync_info_t *highsync_info = Context;
    4.53 +  ULONG ActiveProcessorCount;
    4.54    KIRQL old_irql;
    4.55    
    4.56    UNREFERENCED_PARAMETER(Dpc);
    4.57 @@ -99,9 +110,19 @@ XenPci_HighSyncCallFunctionN(
    4.58    FUNCTION_MSG("(CPU = %d)\n", KeGetCurrentProcessorNumber());
    4.59  
    4.60    KdPrint((__DRIVER_NAME "     CPU %d spinning...\n", KeGetCurrentProcessorNumber()));
    4.61 +  InterlockedIncrement(&highsync_info->nr_procs_at_dispatch_level);
    4.62 +  if (highsync_info->sync_level > DISPATCH_LEVEL)
    4.63 +  {
    4.64 +    ActiveProcessorCount = (ULONG)KeNumberProcessors;
    4.65 +    while (highsync_info->nr_procs_at_dispatch_level < (LONG)ActiveProcessorCount)
    4.66 +    {
    4.67 +      KeStallExecutionProcessor(1);
    4.68 +      KeMemoryBarrier();
    4.69 +    }
    4.70 +  }
    4.71    _disable(); //__asm cli;  
    4.72    KeRaiseIrql(highsync_info->sync_level, &old_irql);
    4.73 -  InterlockedIncrement(&highsync_info->nr_spinning);
    4.74 +  InterlockedIncrement(&highsync_info->nr_spinning_at_sync_level);
    4.75    while(highsync_info->do_spin)
    4.76    {
    4.77      KeStallExecutionProcessor(1);
    4.78 @@ -110,7 +131,8 @@ XenPci_HighSyncCallFunctionN(
    4.79    highsync_info->functionN(highsync_info->context);
    4.80    KeLowerIrql(old_irql);
    4.81    _enable(); //__asm sti;
    4.82 -  InterlockedDecrement(&highsync_info->nr_spinning);
    4.83 +  InterlockedDecrement(&highsync_info->nr_spinning_at_sync_level);
    4.84 +  InterlockedDecrement(&highsync_info->nr_procs_at_dispatch_level);
    4.85    FUNCTION_EXIT();
    4.86    return;
    4.87  }