win-pvdrivers

changeset 832:4db4fd1c87fb

Change grant table to use real memory space which fixes a hibernate crash.
Change resume from hibernate path to fix a crash. This may in turn break scsiport.
author James Harper <james.harper@bendigoit.com.au>
date Sat Jan 22 15:49:01 2011 +1100 (2011-01-22)
parents d0130ea66500
children 66e79b88f301
files xenpci/gnttbl.c xenpci/xenpci.h xenpci/xenpci_fdo.c xenpci/xenpci_pdo.c
line diff
     1.1 --- a/xenpci/gnttbl.c	Sat Jan 22 15:47:16 2011 +1100
     1.2 +++ b/xenpci/gnttbl.c	Sat Jan 22 15:49:01 2011 +1100
     1.3 @@ -67,17 +67,20 @@ GntTbl_Map(PVOID Context, unsigned int s
     1.4    struct xen_add_to_physmap xatp;
     1.5    unsigned int i = end_idx;
     1.6  
     1.7 +  FUNCTION_ENTER();
     1.8    /* Loop backwards, so that the first hypercall has the largest index,  ensuring that the table will grow only once.  */
     1.9    do {
    1.10      xatp.domid = DOMID_SELF;
    1.11      xatp.idx = i;
    1.12      xatp.space = XENMAPSPACE_grant_table;
    1.13 -    xatp.gpfn = (xen_pfn_t)(xpdd->gnttab_table_physical.QuadPart >> PAGE_SHIFT) + i;
    1.14 +    //xatp.gpfn = (xen_pfn_t)(xpdd->gnttab_table_physical.QuadPart >> PAGE_SHIFT) + i;
    1.15 +    xatp.gpfn = (xen_pfn_t)MmGetMdlPfnArray(xpdd->gnttab_mdl)[i];
    1.16      if (HYPERVISOR_memory_op(xpdd, XENMEM_add_to_physmap, &xatp))
    1.17      {
    1.18 -      KdPrint((__DRIVER_NAME "     ***ERROR MAPPING FRAME***\n"));
    1.19 +      KdPrint((__DRIVER_NAME "     *** ERROR MAPPING FRAME %d ***\n", i));
    1.20      }
    1.21    } while (i-- > start_idx);
    1.22 +  FUNCTION_EXIT();
    1.23  
    1.24    return 0;
    1.25  }
    1.26 @@ -188,6 +191,34 @@ GntTbl_Init(PXENPCI_DEVICE_DATA xpdd)
    1.27    #endif
    1.28    xpdd->gnttab_table_copy = ExAllocatePoolWithTag(NonPagedPool, xpdd->grant_frames * PAGE_SIZE, XENPCI_POOL_TAG);
    1.29    ASSERT(xpdd->gnttab_table_copy); // lazy
    1.30 +  xpdd->gnttab_table = ExAllocatePoolWithTag(NonPagedPool, xpdd->grant_frames * PAGE_SIZE, XENPCI_POOL_TAG);
    1.31 +  ASSERT(xpdd->gnttab_table); // lazy
    1.32 +  /* dom0 crashes if we allocate the wrong amount of memory here! */
    1.33 +  xpdd->gnttab_mdl = IoAllocateMdl(xpdd->gnttab_table, xpdd->grant_frames * PAGE_SIZE, FALSE, FALSE, NULL);
    1.34 +  ASSERT(xpdd->gnttab_mdl); // lazy
    1.35 +  MmBuildMdlForNonPagedPool(xpdd->gnttab_mdl);
    1.36 +
    1.37 +  for (i = 0; i < (int)xpdd->grant_frames; i++)
    1.38 +  {
    1.39 +    struct xen_memory_reservation reservation;
    1.40 +    xen_pfn_t pfn;
    1.41 +    ULONG ret;
    1.42 +    
    1.43 +    reservation.address_bits = 0;
    1.44 +    reservation.extent_order = 0;
    1.45 +    reservation.domid = DOMID_SELF;
    1.46 +    reservation.nr_extents = 1;
    1.47 +    #pragma warning(disable: 4127) /* conditional expression is constant */
    1.48 +    pfn = (xen_pfn_t)MmGetMdlPfnArray(xpdd->gnttab_mdl)[i];
    1.49 +    KdPrint((__DRIVER_NAME "     pfn = %x\n", (ULONG)pfn));
    1.50 +    set_xen_guest_handle(reservation.extent_start, &pfn);
    1.51 +    
    1.52 +    KdPrint((__DRIVER_NAME "     Calling HYPERVISOR_memory_op - pfn = %x\n", (ULONG)pfn));
    1.53 +    ret = HYPERVISOR_memory_op(xpdd, XENMEM_decrease_reservation, &reservation);
    1.54 +    KdPrint((__DRIVER_NAME "     decreased %d pages for grant table frame %d\n", ret, i));
    1.55 +  }
    1.56 +
    1.57 +#if 0
    1.58    xpdd->gnttab_table_physical = XenPci_AllocMMIO(xpdd, PAGE_SIZE * xpdd->grant_frames);
    1.59    xpdd->gnttab_table = MmMapIoSpace(xpdd->gnttab_table_physical, PAGE_SIZE * xpdd->grant_frames, MmNonCached);
    1.60    if (!xpdd->gnttab_table)
    1.61 @@ -196,6 +227,7 @@ GntTbl_Init(PXENPCI_DEVICE_DATA xpdd)
    1.62      // this should be a show stopper...
    1.63      return;
    1.64    }
    1.65 +#endif
    1.66  
    1.67    stack_new(&xpdd->gnttab_ss, grant_entries);
    1.68    
     2.1 --- a/xenpci/xenpci.h	Sat Jan 22 15:47:16 2011 +1100
     2.2 +++ b/xenpci/xenpci.h	Sat Jan 22 15:49:01 2011 +1100
     2.3 @@ -153,11 +153,12 @@ typedef struct {
     2.4    /* grant related */
     2.5    struct stack_state *gnttab_ss;
     2.6    grant_entry_t *gnttab_table;
     2.7 +  PMDL gnttab_mdl;
     2.8    grant_entry_t *gnttab_table_copy;
     2.9    #if DBG
    2.10    PULONG gnttab_tag;
    2.11    #endif
    2.12 -  PHYSICAL_ADDRESS gnttab_table_physical;
    2.13 +  //PHYSICAL_ADDRESS gnttab_table_physical;
    2.14    //grant_ref_t *gnttab_list;
    2.15    //int gnttab_list_free;
    2.16    //KSPIN_LOCK grant_lock;
     3.1 --- a/xenpci/xenpci_fdo.c	Sat Jan 22 15:47:16 2011 +1100
     3.2 +++ b/xenpci/xenpci_fdo.c	Sat Jan 22 15:49:01 2011 +1100
     3.3 @@ -690,8 +690,6 @@ XenPci_EvtDeviceD0Entry(WDFDEVICE device
     3.4  {
     3.5    NTSTATUS status = STATUS_SUCCESS;
     3.6    PXENPCI_DEVICE_DATA xpdd = GetXpdd(device);
     3.7 -  ULONG i;
     3.8 -  ULONG ret;
     3.9  
    3.10    FUNCTION_ENTER();
    3.11  
    3.12 @@ -738,6 +736,8 @@ XenPci_EvtDeviceD0Entry(WDFDEVICE device
    3.13      GntTbl_Init(xpdd);
    3.14      EvtChn_Init(xpdd);
    3.15  
    3.16 +    /* need to give some memory back to xen to balance the books */
    3.17 +#if 0
    3.18      for (i = 0; i < NR_GRANT_FRAMES + 1; i++)
    3.19      {
    3.20        struct xen_memory_reservation reservation;
    3.21 @@ -751,10 +751,13 @@ XenPci_EvtDeviceD0Entry(WDFDEVICE device
    3.22        #pragma warning(disable: 4127) /* conditional expression is constant */
    3.23        set_xen_guest_handle(reservation.extent_start, &pfn);
    3.24        
    3.25 -      //KdPrint((__DRIVER_NAME "     Calling HYPERVISOR_memory_op - pfn = %x\n", (ULONG)pfn));
    3.26 +      KdPrint((__DRIVER_NAME "     Calling HYPERVISOR_memory_op - pfn = %x\n", (ULONG)pfn));
    3.27        ret = HYPERVISOR_memory_op(xpdd, XENMEM_decrease_reservation, &reservation);
    3.28 -      //KdPrint((__DRIVER_NAME "     decreased %d pages\n", ret));
    3.29 +      KdPrint((__DRIVER_NAME "     decreased %d pages\n", ret));
    3.30 +      *(PUCHAR*)MmGetMdlVirtualAddress(mdl) = (UCHAR)0x42;
    3.31 +      KdPrint((__DRIVER_NAME "     touched decreased page\n"));
    3.32      }
    3.33 +#endif
    3.34      
    3.35    // use the memory_op(unsigned int op, void *arg) hypercall to adjust memory
    3.36    // use XENMEM_increase_reservation and XENMEM_decrease_reservation
     4.1 --- a/xenpci/xenpci_pdo.c	Sat Jan 22 15:47:16 2011 +1100
     4.2 +++ b/xenpci/xenpci_pdo.c	Sat Jan 22 15:49:01 2011 +1100
     4.3 @@ -1107,13 +1107,14 @@ XenPciPdo_EvtDeviceD0Entry(WDFDEVICE dev
     4.4    {
     4.5    }
     4.6  
     4.7 +#if 0
     4.8    if (previous_state == WdfPowerDevicePrepareForHibernation || previous_state == WdfPowerDeviceD3 || previous_state == WdfPowerDeviceD3Final)
     4.9    {
    4.10      xppdd->requested_resources_ptr = xppdd->requested_resources_start;
    4.11      xppdd->assigned_resources_start = xppdd->assigned_resources_ptr = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);
    4.12    }
    4.13 -
    4.14    XenConfig_InitConfigPage(device);
    4.15 +#endif
    4.16  
    4.17    status = XenPci_GetBackendAndAddWatch(device);
    4.18    if (!NT_SUCCESS(status))
    4.19 @@ -1122,7 +1123,31 @@ XenPciPdo_EvtDeviceD0Entry(WDFDEVICE dev
    4.20      FUNCTION_EXIT_STATUS(status);
    4.21      return status;
    4.22    }
    4.23 -  status = XenPci_XenConfigDevice(device);
    4.24 +
    4.25 +  if (previous_state == WdfPowerDeviceD3 || previous_state == WdfPowerDeviceD3Final)
    4.26 +  {
    4.27 +    xppdd->requested_resources_ptr = xppdd->requested_resources_start;
    4.28 +    xppdd->assigned_resources_start = xppdd->assigned_resources_ptr = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);
    4.29 +    XenConfig_InitConfigPage(device);
    4.30 +    status = XenPci_XenConfigDevice(device);
    4.31 +  }
    4.32 +  else if (previous_state == WdfPowerDevicePrepareForHibernation)
    4.33 +  {
    4.34 +    PVOID src, dst;
    4.35 +    
    4.36 +    ADD_XEN_INIT_REQ(&xppdd->requested_resources_ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
    4.37 +    src = xppdd->requested_resources_start;
    4.38 +    xppdd->requested_resources_ptr = xppdd->requested_resources_start = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);;
    4.39 +    xppdd->assigned_resources_ptr = xppdd->assigned_resources_start;
    4.40 +
    4.41 +    dst = MmMapIoSpace(xppdd->config_page_phys, xppdd->config_page_length, MmNonCached);
    4.42 +
    4.43 +    status = XenPci_XenConfigDeviceSpecifyBuffers(device, src, dst);
    4.44 +
    4.45 +    MmUnmapIoSpace(dst, xppdd->config_page_length);
    4.46 +    ExFreePoolWithTag(src, XENPCI_POOL_TAG);
    4.47 +  }
    4.48 +
    4.49    if (!NT_SUCCESS(status))
    4.50    {
    4.51      RtlStringCbPrintfA(path, ARRAY_SIZE(path), "%s/state", xppdd->backend_path);
    4.52 @@ -1424,7 +1449,7 @@ XenPci_EvtChildListCreateDevice(WDFCHILD
    4.53      KdPrint((__DRIVER_NAME "     WdfDeviceAddQueryInterface failed - %08x\n", status));
    4.54      return status;
    4.55    }
    4.56 -  
    4.57 +
    4.58    RtlStringCbCopyA(xppdd->path, ARRAY_SIZE(xppdd->path), identification->path);
    4.59    RtlStringCbCopyA(xppdd->device, ARRAY_SIZE(xppdd->device), identification->device);
    4.60    xppdd->index = identification->index;
    4.61 @@ -1551,9 +1576,9 @@ XenPci_Pdo_Resume(WDFDEVICE device)
    4.62        src = xppdd->requested_resources_start;
    4.63        xppdd->requested_resources_ptr = xppdd->requested_resources_start = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENPCI_POOL_TAG);;
    4.64        xppdd->assigned_resources_ptr = xppdd->assigned_resources_start;
    4.65 -      
    4.66 +
    4.67        dst = MmMapIoSpace(xppdd->config_page_phys, xppdd->config_page_length, MmNonCached);
    4.68 -      
    4.69 +
    4.70        status = XenPci_XenConfigDeviceSpecifyBuffers(device, src, dst);
    4.71  
    4.72        MmUnmapIoSpace(dst, xppdd->config_page_length);