win-pvdrivers

changeset 638:70c3a7839b4e 0.10.0.89

Add bug check reporting via qemu.
Don't do TPR patch if the verifier is enabled (crashes).
author James Harper <james.harper@bendigoit.com.au>
date Sun Aug 23 13:51:28 2009 +1000 (2009-08-23)
parents e85cb1aba991
children 16108e228997
files xenpci/xenpci.c xenpci/xenpci_fdo.c xenpci/xenpci_patch_kernel.c
line diff
     1.1 --- a/xenpci/xenpci.c	Thu Aug 20 20:54:28 2009 +1000
     1.2 +++ b/xenpci/xenpci.c	Sun Aug 23 13:51:28 2009 +1000
     1.3 @@ -19,6 +19,7 @@ Foundation, Inc., 51 Franklin Street, Fi
     1.4  
     1.5  #define INITGUID
     1.6  #include "xenpci.h"
     1.7 +#include <aux_klib.h>
     1.8  #include <stdlib.h>
     1.9  
    1.10  #define SYSRQ_PATH "control/sysrq"
    1.11 @@ -548,6 +549,28 @@ XenPci_FixLoadOrder()
    1.12    return;
    1.13  }
    1.14  
    1.15 +KBUGCHECK_CALLBACK_RECORD callback_record;
    1.16 +
    1.17 +static VOID
    1.18 +XenPci_BugcheckCallback(PVOID buffer, ULONG length)
    1.19 +{
    1.20 +  NTSTATUS status;
    1.21 +  KBUGCHECK_DATA bugcheck_data;
    1.22 +  
    1.23 +  UNREFERENCED_PARAMETER(buffer);
    1.24 +  UNREFERENCED_PARAMETER(length);
    1.25 +  
    1.26 +  bugcheck_data.BugCheckDataSize  = sizeof(bugcheck_data);
    1.27 +  status = AuxKlibGetBugCheckData(&bugcheck_data);
    1.28 +  if(!NT_SUCCESS(status))
    1.29 +  {
    1.30 +    KdPrint((__DRIVER_NAME "     AuxKlibGetBugCheckData returned %08x\n", status));
    1.31 +    return;
    1.32 +  }
    1.33 +  KdPrint((__DRIVER_NAME "     Bug check 0x%08X (0x%p, 0x%p, 0x%p, 0x%p)\n",
    1.34 +    bugcheck_data.BugCheckCode, bugcheck_data.Parameter1, bugcheck_data.Parameter2, bugcheck_data.Parameter3, bugcheck_data.Parameter4));
    1.35 +}
    1.36 +
    1.37  NTSTATUS
    1.38  DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
    1.39  {
    1.40 @@ -563,15 +586,23 @@ DriverEntry(PDRIVER_OBJECT DriverObject,
    1.41    char Buf[300];// Sometimes bigger then 200 if system reboot from crash
    1.42    ULONG BufLen = 300;
    1.43    PKEY_VALUE_PARTIAL_INFORMATION KeyPartialValue;
    1.44 -#if 0
    1.45 -  WDF_TIMER_CONFIG  timer_config;
    1.46 -  OBJECT_ATTRIBUTES timer_attributes;
    1.47 -#endif
    1.48 -
    1.49 +  
    1.50    UNREFERENCED_PARAMETER(RegistryPath);
    1.51  
    1.52    FUNCTION_ENTER();
    1.53 +
    1.54 +  status = AuxKlibInitialize();
    1.55 +  if(!NT_SUCCESS(status))
    1.56 +  {
    1.57 +    KdPrint((__DRIVER_NAME "     AuxKlibInitialize failed %08x - expect a crash soon\n", status));
    1.58 +  }
    1.59    
    1.60 +  KeInitializeCallbackRecord(&callback_record);
    1.61 +  if (!KeRegisterBugCheckCallback(&callback_record, XenPci_BugcheckCallback, NULL, 0, (PUCHAR)"XenPci"))
    1.62 +  {
    1.63 +    KdPrint((__DRIVER_NAME "     KeRegisterBugCheckCallback failed\n"));
    1.64 +  }
    1.65 +
    1.66    XenPci_FixLoadOrder();
    1.67  
    1.68    RtlInitUnicodeString(&RegKeyName, L"\\Registry\\Machine\\System\\CurrentControlSet\\Control");
    1.69 @@ -597,8 +628,33 @@ DriverEntry(PDRIVER_OBJECT DriverObject,
    1.70    
    1.71    if (wcsstr(SystemStartOptions, L"PATCHTPR"))
    1.72    {
    1.73 +    WDFKEY memory_key;
    1.74 +    UNICODE_STRING verifier_key_name;
    1.75 +    UNICODE_STRING verifier_value_name;
    1.76 +    ULONG verifier_value;
    1.77 +    
    1.78      KdPrint((__DRIVER_NAME "     PATCHTPR found\n"));
    1.79 -    tpr_patch_requested = TRUE;
    1.80 +    
    1.81 +    RtlInitUnicodeString(&verifier_key_name, L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\Session Manager\\Memory Management");
    1.82 +    status = WdfRegistryOpenKey(NULL, &verifier_key_name, KEY_READ, WDF_NO_OBJECT_ATTRIBUTES, &memory_key);
    1.83 +    if (!NT_SUCCESS(status))
    1.84 +    {
    1.85 +      tpr_patch_requested = TRUE;
    1.86 +    }  
    1.87 +    else
    1.88 +    {
    1.89 +      RtlInitUnicodeString(&verifier_value_name, L"VerifyDriverLevel");
    1.90 +      status = WdfRegistryQueryULong(memory_key, &verifier_value_name, &verifier_value);
    1.91 +      if (NT_SUCCESS(status) && verifier_value != 0)
    1.92 +      {
    1.93 +        KdPrint((__DRIVER_NAME "     Verifier active - not patching\n"));
    1.94 +      }
    1.95 +      else
    1.96 +      {
    1.97 +        tpr_patch_requested = TRUE;
    1.98 +      }
    1.99 +      WdfRegistryClose(memory_key);
   1.100 +    }
   1.101    }
   1.102    
   1.103    if (wcsstr(SystemStartOptions, L"NOGPLPV"))
     2.1 --- a/xenpci/xenpci_fdo.c	Thu Aug 20 20:54:28 2009 +1000
     2.2 +++ b/xenpci/xenpci_fdo.c	Sun Aug 23 13:51:28 2009 +1000
     2.3 @@ -35,7 +35,6 @@ XenPci_MapHalThenPatchKernel(PXENPCI_DEV
     2.4     
     2.5    FUNCTION_ENTER();
     2.6  
     2.7 -  status = AuxKlibInitialize();
     2.8    amei = NULL;
     2.9    /* buffer size could change between requesting and allocating - need to loop until we are successful */
    2.10    while ((status = AuxKlibQueryModuleInformation(&module_info_buffer_size, sizeof(AUX_MODULE_EXTENDED_INFO), amei)) == STATUS_BUFFER_TOO_SMALL || amei == NULL)
     3.1 --- a/xenpci/xenpci_patch_kernel.c	Thu Aug 20 20:54:28 2009 +1000
     3.2 +++ b/xenpci/xenpci_patch_kernel.c	Sun Aug 23 13:51:28 2009 +1000
     3.3 @@ -198,7 +198,7 @@ static patch_t patches[] =
     3.4  static BOOLEAN
     3.5  XenPci_TestAndPatchInstruction(PVOID address)
     3.6  {
     3.7 -	PUCHAR instruction = address;
     3.8 +  PUCHAR instruction = address;
     3.9    ULONG i;
    3.10    /* don't declare patches[] on the stack - windows gets grumpy if we allocate too much space on the stack at HIGH_LEVEL */
    3.11    
    3.12 @@ -207,9 +207,7 @@ XenPci_TestAndPatchInstruction(PVOID add
    3.13      if (memcmp(address, patches[i].bytes, patches[i].match_size) == 0)
    3.14        break;
    3.15    }
    3.16 -  if (patches[i].patch_type == PATCH_NONE)
    3.17 -    return FALSE;
    3.18 -    
    3.19 +
    3.20    switch (patches[i].patch_type)
    3.21    {
    3.22    case PATCH_1B4:
    3.23 @@ -230,10 +228,9 @@ XenPci_TestAndPatchInstruction(PVOID add
    3.24      InsertCallRel32(instruction + 2, patches[i].function);
    3.25      break;
    3.26    default:
    3.27 -    /* wtf? */
    3.28 -    break;
    3.29 +    return FALSE;
    3.30    }
    3.31 -	return TRUE;
    3.32 +  return TRUE;
    3.33  }
    3.34  
    3.35  typedef struct {
    3.36 @@ -255,8 +252,7 @@ XenPci_DoPatchKernel0(PVOID context)
    3.37  
    3.38    FUNCTION_ENTER();
    3.39  
    3.40 -	high_level_tpr = SaveTpr();
    3.41 -
    3.42 +  high_level_tpr = SaveTpr();
    3.43    /* we know all the other CPUs are at HIGH_LEVEL so set them all to the same as cpu 0 */
    3.44    for (i = 1; i < MAX_VIRT_CPUS; i++)
    3.45      SaveTprProcValue(i, high_level_tpr);
    3.46 @@ -270,9 +266,10 @@ XenPci_DoPatchKernel0(PVOID context)
    3.47      }
    3.48      else if (*(PULONG)((PUCHAR)pi->base + i) == LAPIC_TASKPRI)
    3.49      {
    3.50 -       potential_patch_positions[potential_patch_position_index++] = (PUCHAR)pi->base + i;
    3.51 +      potential_patch_positions[potential_patch_position_index++] = (PUCHAR)pi->base + i;
    3.52      }
    3.53    }
    3.54 +
    3.55    for (i = 0; i < patch_position_index; i++)
    3.56      KdPrint((__DRIVER_NAME "     Patch added at %p\n", patch_positions[i]));
    3.57