From a1fc7aa628beedda24325ce4de8231ed03b2b184 Mon Sep 17 00:00:00 2001 From: Zebediah Figura Date: Tue, 29 Jan 2019 21:37:38 -0600 Subject: [PATCH 03/13] ntoskrnl.exe: Implement ExAcquireResourceSharedLite(). Signed-off-by: Zebediah Figura --- dlls/ntoskrnl.exe/ntoskrnl.exe.spec | 2 +- dlls/ntoskrnl.exe/sync.c | 79 +++++++++++++++++++++++++++++ include/ddk/wdm.h | 1 + 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/dlls/ntoskrnl.exe/ntoskrnl.exe.spec b/dlls/ntoskrnl.exe/ntoskrnl.exe.spec index d0874226..217f27ff 100644 --- a/dlls/ntoskrnl.exe/ntoskrnl.exe.spec +++ b/dlls/ntoskrnl.exe/ntoskrnl.exe.spec @@ -121,7 +121,7 @@ @ stdcall DbgQueryDebugFilterState(long long) @ stub DbgSetDebugFilterState @ stdcall ExAcquireResourceExclusiveLite(ptr long) -@ stub ExAcquireResourceSharedLite +@ stdcall ExAcquireResourceSharedLite(ptr long) @ stub ExAcquireSharedStarveExclusive @ stub ExAcquireSharedWaitForExclusive @ stub ExAllocateFromPagedLookasideList diff --git a/dlls/ntoskrnl.exe/sync.c b/dlls/ntoskrnl.exe/sync.c index 205914a7..b37a46e4 100644 --- a/dlls/ntoskrnl.exe/sync.c +++ b/dlls/ntoskrnl.exe/sync.c @@ -19,6 +19,7 @@ */ #include "config.h" +#include #include #include "ntstatus.h" @@ -687,6 +688,25 @@ NTSTATUS WINAPI ExInitializeResourceLite( ERESOURCE *resource ) return STATUS_SUCCESS; } +/* Find an existing entry in the shared owner list, or create a new one. */ +static OWNER_ENTRY *resource_get_shared_entry( ERESOURCE *resource, ERESOURCE_THREAD thread ) +{ + ULONG i, count; + + for (i = 0; i < resource->OwnerEntry.TableSize; ++i) + { + if (resource->OwnerTable[i].OwnerThread == thread) + return &resource->OwnerTable[i]; + } + + count = ++resource->OwnerEntry.TableSize; + resource->OwnerTable = heap_realloc(resource->OwnerTable, count * sizeof(*resource->OwnerTable)); + resource->OwnerTable[count - 1].OwnerThread = thread; + resource->OwnerTable[count - 1].OwnerCount = 0; + + return &resource->OwnerTable[count - 1]; +} + /*********************************************************************** * ExAcquireResourceExclusiveLite (NTOSKRNL.EXE.@) */ @@ -746,3 +766,62 @@ BOOLEAN WINAPI ExAcquireResourceExclusiveLite( ERESOURCE *resource, BOOLEAN wait return TRUE; } + +/*********************************************************************** + * ExAcquireResourceSharedLite (NTOSKRNL.EXE.@) + */ +BOOLEAN WINAPI ExAcquireResourceSharedLite( ERESOURCE *resource, BOOLEAN wait ) +{ + OWNER_ENTRY *entry; + KIRQL irql; + + TRACE("resource %p, wait %u.\n", resource, wait); + + KeAcquireSpinLock( &resource->SpinLock, &irql ); + + entry = resource_get_shared_entry( resource, (ERESOURCE_THREAD)KeGetCurrentThread() ); + + if (resource->Flag & ResourceOwnedExclusive) + { + if (resource->OwnerEntry.OwnerThread == (ERESOURCE_THREAD)KeGetCurrentThread()) + { + resource->ActiveEntries++; + KeReleaseSpinLock( &resource->SpinLock, irql ); + return TRUE; + } + } + else if (entry->OwnerCount || !resource->NumberOfExclusiveWaiters) + { + entry->OwnerCount++; + resource->ActiveEntries++; + KeReleaseSpinLock( &resource->SpinLock, irql ); + return TRUE; + } + + if (!wait) + { + KeReleaseSpinLock( &resource->SpinLock, irql ); + return FALSE; + } + + if (!resource->SharedWaiters) + { + resource->SharedWaiters = heap_alloc( sizeof(*resource->SharedWaiters) ); + KeInitializeSemaphore( resource->SharedWaiters, 0, INT_MAX ); + } + resource->NumberOfSharedWaiters++; + + KeReleaseSpinLock( &resource->SpinLock, irql ); + + KeWaitForSingleObject( resource->SharedWaiters, Executive, KernelMode, FALSE, NULL ); + + KeAcquireSpinLock( &resource->SpinLock, &irql ); + + entry->OwnerCount++; + resource->ActiveEntries++; + resource->NumberOfSharedWaiters--; + + KeReleaseSpinLock( &resource->SpinLock, irql ); + + return TRUE; +} diff --git a/include/ddk/wdm.h b/include/ddk/wdm.h index a8cbdc2e..14d4ba4e 100644 --- a/include/ddk/wdm.h +++ b/include/ddk/wdm.h @@ -1513,6 +1513,7 @@ static inline void IoSetCompletionRoutine(IRP *irp, PIO_COMPLETION_ROUTINE routi NTSTATUS WINAPI DbgQueryDebugFilterState(ULONG, ULONG); void WINAPI ExAcquireFastMutexUnsafe(PFAST_MUTEX); BOOLEAN WINAPI ExAcquireResourceExclusiveLite(ERESOURCE*,BOOLEAN); +BOOLEAN WINAPI ExAcquireResourceSharedLite(ERESOURCE*,BOOLEAN); PVOID WINAPI ExAllocatePool(POOL_TYPE,SIZE_T); PVOID WINAPI ExAllocatePoolWithQuota(POOL_TYPE,SIZE_T); PVOID WINAPI ExAllocatePoolWithTag(POOL_TYPE,SIZE_T,ULONG); -- 2.20.1