diff options
Diffstat (limited to 'network/trans/WFPSampler/sys/Framework_PowerStates.cpp')
| -rw-r--r-- | network/trans/WFPSampler/sys/Framework_PowerStates.cpp | 145 |
1 files changed, 123 insertions, 22 deletions
diff --git a/network/trans/WFPSampler/sys/Framework_PowerStates.cpp b/network/trans/WFPSampler/sys/Framework_PowerStates.cpp index ed67b11f..c615f8a1 100644 --- a/network/trans/WFPSampler/sys/Framework_PowerStates.cpp +++ b/network/trans/WFPSampler/sys/Framework_PowerStates.cpp @@ -20,6 +20,118 @@ #include "Framework_WFPSamplerCalloutDriver.h" /// . #include "Framework_PowerStates.tmh" /// $(OBJ_PATH)\$(O)\ +KGUARDED_MUTEX guardedMutex = {0}; + +/** + @framework_function="ActOnPowerStateEnter" + + Purpose: Passive function to handle entering a power managed state. Prior to dropping into a power managed <br> + state, unregister the callouts. This prevents the driver from potentially taking any more <br> + references on NBLs, and instead causes the filters to return BLOCK. This should also allow adequate <br> + time to drain currently queued NBLs. <br> + <br> + Notes: <br> + <br> + MSDN_Ref: https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/wdm/nc-wdm-io_workitem_routine <br> +*/ +_IRQL_requires_(PASSIVE_LEVEL) +_IRQL_requires_same_ +_Function_class_(IO_WORKITEM_ROUTINE) +VOID ActOnPowerStateEnter(_In_ PDEVICE_OBJECT pDeviceObject, + _Inout_opt_ PVOID pContext) +{ +#if DBG + + DbgPrintEx(DPFLTR_IHVNETWORK_ID, + DPFLTR_INFO_LEVEL, + " ---> ActOnPowerStateEnter()\n"); + +#endif /// DBG + + UNREFERENCED_PARAMETER(pDeviceObject); + UNREFERENCED_PARAMETER(pContext); + + KeAcquireGuardedMutex(&guardedMutex); + + if(g_calloutsRegistered == TRUE) + { + NTSTATUS status = STATUS_SUCCESS; + + status = KrnlHlprExposedCalloutsUnregister(); + HLPR_BAIL_ON_FAILURE(status); + + g_calloutsRegistered = FALSE; + } + + HLPR_BAIL_LABEL: + + KeReleaseGuardedMutex(&guardedMutex); + +#if DBG + + DbgPrintEx(DPFLTR_IHVNETWORK_ID, + DPFLTR_INFO_LEVEL, + " <--- ActOnPowerStateEnter()\n"); + +#endif /// DBG + + return; +} + +/** + @framework_function="ActOnPowerStateExit" + + Purpose: Passive function to handle exiting a power managed state. When coming out of a power managed state, <br> + register the callouts so normal processing of NBLs will commence. <br> + <br> + Notes: <br> + <br> + MSDN_Ref: https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/wdm/nc-wdm-io_workitem_routine <br> +*/ +_IRQL_requires_(PASSIVE_LEVEL) +_IRQL_requires_same_ +_Function_class_(IO_WORKITEM_ROUTINE) +VOID ActOnPowerStateExit(_In_ PDEVICE_OBJECT pDeviceObject, + _Inout_opt_ PVOID pContext) +{ +#if DBG + + DbgPrintEx(DPFLTR_IHVNETWORK_ID, + DPFLTR_INFO_LEVEL, + " ---> ActOnPowerStateExit()\n"); + +#endif /// DBG + + UNREFERENCED_PARAMETER(pDeviceObject); + UNREFERENCED_PARAMETER(pContext); + + KeAcquireGuardedMutex(&guardedMutex); + + if(g_calloutsRegistered == FALSE) + { + NTSTATUS status = STATUS_SUCCESS; + + status = KrnlHlprExposedCalloutsRegister(); + HLPR_BAIL_ON_FAILURE(status); + + g_calloutsRegistered = TRUE; + } + + HLPR_BAIL_LABEL: + + KeReleaseGuardedMutex(&guardedMutex); + +#if DBG + + DbgPrintEx(DPFLTR_IHVNETWORK_ID, + DPFLTR_INFO_LEVEL, + " <--- ActOnPowerStateExit()\n"); + +#endif /// DBG + + return; +} + /** @framework_function="PowerStateCallback" @@ -47,36 +159,23 @@ VOID PowerStateCallback(_In_ VOID* pCallbackContext, if(pPowerStateEvent == (VOID*)PO_CB_SYSTEM_STATE_LOCK) { - NTSTATUS status = STATUS_SUCCESS; - if(pEventSpecifics) { /// entering the ON state (S0), so return operation to normal - if(g_calloutsRegistered == FALSE) - { - status = KrnlHlprExposedCalloutsRegister(); - HLPR_BAIL_ON_FAILURE(status); - - g_calloutsRegistered = TRUE; - } + IoQueueWorkItem(g_pPowerStateExitIOWorkItem, + ActOnPowerStateExit, + DelayedWorkQueue, + 0); } else { - /// leaving the ON state (S0) to sleep (S1/S2/S3) or hibernate (S4) - /// Unregister the callouts so no more injection will take place. By default, the filters - /// invoking the callouts will return block. - if(g_calloutsRegistered == TRUE) - { - status = KrnlHlprExposedCalloutsUnregister(); - HLPR_BAIL_ON_FAILURE(status); - - g_calloutsRegistered = FALSE; - } + IoQueueWorkItem(g_pPowerStateEnterIOWorkItem, + ActOnPowerStateEnter, + DelayedWorkQueue, + 0); } } - HLPR_BAIL_LABEL: - #if DBG DbgPrintEx(DPFLTR_IHVNETWORK_ID, @@ -89,7 +188,7 @@ VOID PowerStateCallback(_In_ VOID* pCallbackContext, } /** - @framework_function="RegisterPowerStateChangeCallback" + @framework_function="UnregisterPowerStateChangeCallback" Purpose: Unregister a callback that handled notifications of power state changes. <br> <br> @@ -168,6 +267,8 @@ NTSTATUS RegisterPowerStateChangeCallback(_Inout_ DEVICE_EXTENSION* pDeviceExten OBJECT_ATTRIBUTES objectAttributes = {0}; UNICODE_STRING unicodeString = {0}; + KeInitializeGuardedMutex(&guardedMutex); + RtlInitUnicodeString(&unicodeString, L"\\Callback\\PowerState"); |
