3 /* GLOBAL VARIABLES *********************************************************/
5 extern RTL_TIME_ZONE_INFORMATION ExpTimeZoneInfo
;
6 extern LARGE_INTEGER ExpTimeZoneBias
;
7 extern ULONG ExpTimeZoneId
;
8 extern ULONG ExpTickCountMultiplier
;
9 extern ULONG ExpLastTimeZoneBias
;
10 extern POBJECT_TYPE ExEventPairObjectType
;
11 extern POBJECT_TYPE _ExEventObjectType
, _ExSemaphoreObjectType
;
12 extern FAST_MUTEX ExpEnvironmentLock
;
13 extern ERESOURCE ExpFirmwareTableResource
;
14 extern LIST_ENTRY ExpFirmwareTableProviderListHead
;
15 extern BOOLEAN ExpIsWinPEMode
;
16 extern LIST_ENTRY ExpSystemResourcesList
;
17 extern ULONG ExpAnsiCodePageDataOffset
, ExpOemCodePageDataOffset
;
18 extern ULONG ExpUnicodeCaseTableDataOffset
;
19 extern PVOID ExpNlsSectionPointer
;
20 extern ULONG NtGlobalFlag
;
21 extern UNICODE_STRING NtSystemRoot
;
22 extern ULONG ExpInitializationPhase
;
23 extern ULONG ExpAltTimeZoneBias
;
24 extern LIST_ENTRY ExSystemLookasideListHead
;
25 extern PCALLBACK_OBJECT PowerStateCallback
;
26 extern LIST_ENTRY ExPoolLookasideListHead
;
27 extern LIST_ENTRY ExpNonPagedLookasideListHead
;
28 extern LIST_ENTRY ExpPagedLookasideListHead
;
29 extern KSPIN_LOCK ExpNonPagedLookasideListLock
;
30 extern KSPIN_LOCK ExpPagedLookasideListLock
;
31 extern ULONG ExCriticalWorkerThreads
;
32 extern ULONG ExDelayedWorkerThreads
;
34 extern PVOID ExpDefaultErrorPort
;
35 extern PEPROCESS ExpDefaultErrorPortProcess
;
38 * NT/Cm Version Info variables
40 extern ULONG NtMajorVersion
;
41 extern ULONG NtMinorVersion
;
42 extern ULONG NtBuildNumber
;
43 extern ULONG CmNtSpBuildNumber
;
44 extern ULONG CmNtCSDVersion
;
45 extern ULONG CmNtCSDReleaseType
;
46 extern UNICODE_STRING CmVersionString
;
47 extern UNICODE_STRING CmCSDVersionString
;
48 extern CHAR NtBuildLab
[];
52 * WinDBG Debugger Worker State Machine data (see dbgctrl.c)
54 typedef enum _WINKD_WORKER_STATE
58 WinKdWorkerInitialized
61 extern WORK_QUEUE_ITEM ExpDebuggerWorkItem
;
62 extern WINKD_WORKER_STATE ExpDebuggerWork
;
63 extern PEPROCESS ExpDebuggerProcessAttach
;
64 extern PEPROCESS ExpDebuggerProcessKill
;
65 extern ULONG_PTR ExpDebuggerPageIn
;
67 VOID NTAPI
ExpDebuggerWorker(IN PVOID Context
);
68 // #endif /* _WINKD_ */
71 #define HANDLE_LOW_BITS (PAGE_SHIFT - 4)
72 #define HANDLE_HIGH_BITS (PAGE_SHIFT - 3)
74 #define HANDLE_LOW_BITS (PAGE_SHIFT - 3)
75 #define HANDLE_HIGH_BITS (PAGE_SHIFT - 2)
77 #define HANDLE_TAG_BITS (2)
78 #define HANDLE_INDEX_BITS (HANDLE_LOW_BITS + 2*HANDLE_HIGH_BITS)
79 #define KERNEL_FLAG_BITS (sizeof(PVOID)*8 - HANDLE_INDEX_BITS - HANDLE_TAG_BITS)
81 typedef union _EXHANDLE
85 ULONG_PTR TagBits
: HANDLE_TAG_BITS
;
86 ULONG_PTR Index
: HANDLE_INDEX_BITS
;
87 ULONG_PTR KernelFlag
: KERNEL_FLAG_BITS
;
91 ULONG_PTR TagBits2
: HANDLE_TAG_BITS
;
92 ULONG_PTR LowIndex
: HANDLE_LOW_BITS
;
93 ULONG_PTR MidIndex
: HANDLE_HIGH_BITS
;
94 ULONG_PTR HighIndex
: HANDLE_HIGH_BITS
;
95 ULONG_PTR KernelFlag2
: KERNEL_FLAG_BITS
;
97 HANDLE GenericHandleOverlay
;
100 } EXHANDLE
, *PEXHANDLE
;
102 typedef struct _ETIMER
107 LIST_ENTRY ActiveTimerListEntry
;
110 BOOLEAN ApcAssociated
;
112 LIST_ENTRY WakeTimerListEntry
;
117 PCALLBACK_OBJECT
*CallbackObject
;
121 typedef struct _HARDERROR_USER_PARAMETERS
123 ULONG_PTR Parameters
[MAXIMUM_HARDERROR_PARAMETERS
];
124 UNICODE_STRING Strings
[MAXIMUM_HARDERROR_PARAMETERS
];
125 WCHAR Buffer
[ANYSIZE_ARRAY
];
126 } HARDERROR_USER_PARAMETERS
, *PHARDERROR_USER_PARAMETERS
;
128 #define MAX_FAST_REFS 7
130 #define ExAcquireRundownProtection _ExAcquireRundownProtection
131 #define ExReleaseRundownProtection _ExReleaseRundownProtection
132 #define ExInitializeRundownProtection _ExInitializeRundownProtection
133 #define ExWaitForRundownProtectionRelease _ExWaitForRundownProtectionRelease
134 #define ExRundownCompleted _ExRundownCompleted
135 #define ExGetPreviousMode KeGetPreviousMode
139 // Various bits tagged on the handle or handle table
141 #define EXHANDLE_TABLE_ENTRY_LOCK_BIT 1
142 #define FREE_HANDLE_MASK -1
145 // Number of entries in each table level
147 #define LOW_LEVEL_ENTRIES (PAGE_SIZE / sizeof(HANDLE_TABLE_ENTRY))
148 #define MID_LEVEL_ENTRIES (PAGE_SIZE / sizeof(PHANDLE_TABLE_ENTRY))
149 #define HIGH_LEVEL_ENTRIES (16777216 / (LOW_LEVEL_ENTRIES * MID_LEVEL_ENTRIES))
152 // Maximum index in each table level before we need another table
154 #define MAX_LOW_INDEX LOW_LEVEL_ENTRIES
155 #define MAX_MID_INDEX (MID_LEVEL_ENTRIES * LOW_LEVEL_ENTRIES)
156 #define MAX_HIGH_INDEX (MID_LEVEL_ENTRIES * MID_LEVEL_ENTRIES * LOW_LEVEL_ENTRIES)
158 #define ExpChangeRundown(x, y, z) (ULONG_PTR)InterlockedCompareExchangePointer(&x->Ptr, (PVOID)y, (PVOID)z)
159 #define ExpChangePushlock(x, y, z) InterlockedCompareExchangePointer((PVOID*)x, (PVOID)y, (PVOID)z)
160 #define ExpSetRundown(x, y) InterlockedExchangePointer(&x->Ptr, (PVOID)y)
165 IN PSYSTEM_POOLTAG_INFORMATION SystemInformation
,
166 IN ULONG SystemInformationLength
,
167 IN OUT PULONG ReturnLength OPTIONAL
170 typedef struct _UUID_CACHED_VALUES_STRUCT
174 UCHAR ClockSeqHiAndReserved
;
177 } UUID_CACHED_VALUES_STRUCT
, *PUUID_CACHED_VALUES_STRUCT
;
179 /* INITIALIZATION FUNCTIONS *************************************************/
192 Phase1Initialization(
199 ExpInitializePushLocks(VOID
);
203 ExRefreshTimeZoneInformation(
204 IN PLARGE_INTEGER SystemBootTime
210 ExpInitializeWorkerThreads(VOID
);
214 ExSwapinWorkerThreads(IN BOOLEAN AllowSwap
);
219 ExpInitLookasideLists(VOID
);
224 ExInitializeSystemLookasideList(
225 IN PGENERAL_LOOKASIDE List
,
229 IN USHORT MaximumDepth
,
230 IN PLIST_ENTRY ListHead
236 ExpInitializeCallbacks(VOID
);
246 ExpInitializeExecutive(
248 IN PLOADER_PARAMETER_BLOCK LoaderBlock
253 ExShutdownSystem(VOID
);
258 ExpInitializeEventImplementation(VOID
);
263 ExpInitializeKeyedEventImplementation(VOID
);
268 ExpInitializeEventPairImplementation(VOID
);
273 ExpInitializeSemaphoreImplementation(VOID
);
278 ExpInitializeMutantImplementation(VOID
);
283 ExpInitializeTimerImplementation(VOID
);
288 ExpInitializeProfileImplementation(VOID
);
293 ExpResourceInitialization(VOID
);
298 ExInitPoolLookasidePointers(VOID
);
300 /* Callback Functions ********************************************************/
304 ExInitializeCallBack(
305 IN OUT PEX_CALLBACK Callback
308 PEX_CALLBACK_ROUTINE_BLOCK
311 IN PEX_CALLBACK_FUNCTION Function
,
318 IN PEX_CALLBACK_ROUTINE_BLOCK CallbackRoutineBlock
323 ExCompareExchangeCallBack (
324 IN OUT PEX_CALLBACK CallBack
,
325 IN PEX_CALLBACK_ROUTINE_BLOCK NewBlock
,
326 IN PEX_CALLBACK_ROUTINE_BLOCK OldBlock
329 PEX_CALLBACK_ROUTINE_BLOCK
331 ExReferenceCallBackBlock(
332 IN OUT PEX_CALLBACK CallBack
337 ExDereferenceCallBackBlock(
338 IN OUT PEX_CALLBACK CallBack
,
339 IN PEX_CALLBACK_ROUTINE_BLOCK CallbackRoutineBlock
342 PEX_CALLBACK_FUNCTION
344 ExGetCallBackBlockRoutine(
345 IN PEX_CALLBACK_ROUTINE_BLOCK CallbackRoutineBlock
350 ExGetCallBackBlockContext(
351 IN PEX_CALLBACK_ROUTINE_BLOCK CallbackRoutineBlock
357 IN PEX_CALLBACK_ROUTINE_BLOCK CallbackRoutineBlock
360 /* Rundown Functions ********************************************************/
364 ExfInitializeRundownProtection(
365 OUT PEX_RUNDOWN_REF RunRef
370 ExfReInitializeRundownProtection(
371 OUT PEX_RUNDOWN_REF RunRef
376 ExfAcquireRundownProtection(
377 IN OUT PEX_RUNDOWN_REF RunRef
382 ExfAcquireRundownProtectionEx(
383 IN OUT PEX_RUNDOWN_REF RunRef
,
389 ExfReleaseRundownProtection(
390 IN OUT PEX_RUNDOWN_REF RunRef
395 ExfReleaseRundownProtectionEx(
396 IN OUT PEX_RUNDOWN_REF RunRef
,
403 OUT PEX_RUNDOWN_REF RunRef
408 ExfWaitForRundownProtectionRelease(
409 IN OUT PEX_RUNDOWN_REF RunRef
412 /* HANDLE TABLE FUNCTIONS ***************************************************/
415 (NTAPI
*PEX_SWEEP_HANDLE_CALLBACK
)(
416 PHANDLE_TABLE_ENTRY HandleTableEntry
,
422 (NTAPI
*PEX_DUPLICATE_HANDLE_CALLBACK
)(
423 IN PEPROCESS Process
,
424 IN PHANDLE_TABLE HandleTable
,
425 IN PHANDLE_TABLE_ENTRY HandleTableEntry
,
426 IN PHANDLE_TABLE_ENTRY NewEntry
430 (NTAPI
*PEX_CHANGE_HANDLE_CALLBACK
)(
431 PHANDLE_TABLE_ENTRY HandleTableEntry
,
438 ExpInitializeHandleTables(
445 IN PEPROCESS Process OPTIONAL
450 ExUnlockHandleTableEntry(
451 IN PHANDLE_TABLE HandleTable
,
452 IN PHANDLE_TABLE_ENTRY HandleTableEntry
458 IN PHANDLE_TABLE HandleTable
,
459 IN PHANDLE_TABLE_ENTRY HandleTableEntry
464 ExDestroyHandleTable(
465 IN PHANDLE_TABLE HandleTable
,
466 IN PVOID DestroyHandleProcedure OPTIONAL
472 IN PHANDLE_TABLE HandleTable
,
474 IN PHANDLE_TABLE_ENTRY HandleTableEntry OPTIONAL
479 ExMapHandleToPointer(
480 IN PHANDLE_TABLE HandleTable
,
487 IN PEPROCESS Process
,
488 IN PHANDLE_TABLE HandleTable
,
489 IN PEX_DUPLICATE_HANDLE_CALLBACK DupHandleProcedure
,
496 IN PHANDLE_TABLE HandleTable
,
498 IN PEX_CHANGE_HANDLE_CALLBACK ChangeRoutine
,
505 IN PHANDLE_TABLE HandleTable
,
506 IN PEX_SWEEP_HANDLE_CALLBACK EnumHandleProcedure
,
512 ExpLookupHandleTableEntry(
513 IN PHANDLE_TABLE HandleTable
,
519 ExpLockHandleTableEntry(
520 IN PHANDLE_TABLE HandleTable
,
521 IN PHANDLE_TABLE_ENTRY HandleTableEntry
524 /* PSEH EXCEPTION HANDLING **************************************************/
528 ExSystemExceptionFilter(VOID
);
530 /* CALLBACKS *****************************************************************/
534 ExDoCallBack(IN OUT PEX_CALLBACK Callback
,
539 PEX_CALLBACK_ROUTINE_BLOCK CallbackBlock
;
540 PEX_CALLBACK_FUNCTION Function
;
542 /* Reference the block */
543 CallbackBlock
= ExReferenceCallBackBlock(Callback
);
546 /* Get the function */
547 Function
= ExGetCallBackBlockRoutine(CallbackBlock
);
549 /* Do the callback */
550 Function(Context
, Argument1
, Argument2
);
552 /* Now dereference it */
553 ExDereferenceCallBackBlock(Callback
, CallbackBlock
);
557 /* FAST REFS ******************************************************************/
561 ExGetObjectFastReference(IN EX_FAST_REF FastRef
)
563 /* Return the unbiased pointer */
564 return (PVOID
)(FastRef
.Value
& ~MAX_FAST_REFS
);
569 ExGetCountFastReference(IN EX_FAST_REF FastRef
)
571 /* Return the reference count */
572 return (ULONG
)FastRef
.RefCnt
;
577 ExInitializeFastReference(OUT PEX_FAST_REF FastRef
,
578 IN OPTIONAL PVOID Object
)
581 ASSERT((((ULONG_PTR
)Object
) & MAX_FAST_REFS
) == 0);
583 /* Check if an object is being set */
586 /* Clear the field */
587 FastRef
->Object
= NULL
;
591 /* Otherwise, we assume the object was referenced and is ready */
592 FastRef
->Value
= (ULONG_PTR
)Object
| MAX_FAST_REFS
;
598 ExAcquireFastReference(IN OUT PEX_FAST_REF FastRef
)
600 EX_FAST_REF OldValue
, NewValue
;
602 /* Start reference loop */
605 /* Get the current reference count */
609 /* Increase the reference count */
610 NewValue
.Value
= OldValue
.Value
- 1;
611 NewValue
.Object
= ExpChangePushlock(&FastRef
->Object
,
614 if (NewValue
.Object
!= OldValue
.Object
) continue;
621 /* Return the old value */
627 ExInsertFastReference(IN OUT PEX_FAST_REF FastRef
,
630 EX_FAST_REF OldValue
, NewValue
;
633 ASSERT(!(((ULONG_PTR
)Object
) & MAX_FAST_REFS
));
635 /* Start update loop */
638 /* Get the current reference count */
641 /* Check if the current count is too high or if the pointer changed */
642 if (((OldValue
.RefCnt
+ MAX_FAST_REFS
) > MAX_FAST_REFS
) ||
643 ((OldValue
.Value
&~ MAX_FAST_REFS
) != (ULONG_PTR
)Object
))
649 /* Update the reference count */
650 NewValue
.Value
= OldValue
.Value
+ MAX_FAST_REFS
;
651 NewValue
.Object
= ExpChangePushlock(&FastRef
->Object
,
654 if (NewValue
.Object
!= OldValue
.Object
) continue;
666 ExReleaseFastReference(IN PEX_FAST_REF FastRef
,
669 EX_FAST_REF OldValue
, NewValue
;
672 ASSERT(Object
!= NULL
);
673 ASSERT(!(((ULONG_PTR
)Object
) & MAX_FAST_REFS
));
675 /* Start reference loop */
678 /* Get the current reference count */
681 /* Check if we're full if if the pointer changed */
682 if ((OldValue
.Value
^ (ULONG_PTR
)Object
) >= MAX_FAST_REFS
) return FALSE
;
684 /* Decrease the reference count */
685 NewValue
.Value
= OldValue
.Value
+ 1;
686 NewValue
.Object
= ExpChangePushlock(&FastRef
->Object
,
689 if (NewValue
.Object
!= OldValue
.Object
) continue;
701 ExSwapFastReference(IN PEX_FAST_REF FastRef
,
704 EX_FAST_REF NewValue
, OldValue
;
707 ASSERT((((ULONG_PTR
)Object
) & MAX_FAST_REFS
) == 0);
709 /* Check if an object is being set */
712 /* Clear the field */
713 NewValue
.Object
= NULL
;
717 /* Otherwise, we assume the object was referenced and is ready */
718 NewValue
.Value
= (ULONG_PTR
)Object
| MAX_FAST_REFS
;
721 /* Update the object */
722 OldValue
.Object
= InterlockedExchangePointer(&FastRef
->Object
, NewValue
.Object
);
728 ExCompareSwapFastReference(IN PEX_FAST_REF FastRef
,
732 EX_FAST_REF OldValue
, NewValue
;
734 /* Sanity check and start swap loop */
735 ASSERT(!(((ULONG_PTR
)Object
) & MAX_FAST_REFS
));
738 /* Get the current value */
741 /* Make sure there's enough references to swap */
742 if (!((OldValue
.Value
^ (ULONG_PTR
)OldObject
) <= MAX_FAST_REFS
)) break;
744 /* Check if we have an object to swap */
747 /* Set up the value with maximum fast references */
748 NewValue
.Value
= (ULONG_PTR
)Object
| MAX_FAST_REFS
;
752 /* Write the object address itself (which is empty) */
753 NewValue
.Value
= (ULONG_PTR
)Object
;
756 /* Do the actual compare exchange */
757 NewValue
.Object
= ExpChangePushlock(&FastRef
->Object
,
760 if (NewValue
.Object
!= OldValue
.Object
) continue;
766 /* Return the old value */
770 /* RUNDOWN *******************************************************************/
774 ExGetRunRefForGivenProcessor(IN PEX_RUNDOWN_REF_CACHE_AWARE RunRefCacheAware
,
777 return (PEX_RUNDOWN_REF
)((ULONG_PTR
)RunRefCacheAware
->RunRefs
+
778 RunRefCacheAware
->RunRefSize
*
779 (ProcNumber
% RunRefCacheAware
->Number
));
783 * @name ExfAcquireRundownProtection
786 * The ExfAcquireRundownProtection routine acquires rundown protection for
787 * the specified descriptor.
790 * Pointer to a rundown reference descriptor.
792 * @return TRUE if access to the protected structure was granted, FALSE otherwise.
794 * @remarks This is the internal macro for system use only.In case the rundown
795 * was active, then the slow-path will be called through the exported
801 _ExAcquireRundownProtection(IN PEX_RUNDOWN_REF RunRef
)
803 ULONG_PTR Value
, NewValue
;
805 /* Get the current value and mask the active bit */
806 Value
= RunRef
->Count
&~ EX_RUNDOWN_ACTIVE
;
808 /* Add a reference */
809 NewValue
= Value
+ EX_RUNDOWN_COUNT_INC
;
811 /* Change the value */
812 NewValue
= ExpChangeRundown(RunRef
, NewValue
, Value
);
813 if (NewValue
!= Value
)
815 /* Rundown was active, use long path */
816 return ExfAcquireRundownProtection(RunRef
);
824 * @name ExReleaseRundownProtection
827 * The ExReleaseRundownProtection routine releases rundown protection for
828 * the specified descriptor.
831 * Pointer to a rundown reference descriptor.
833 * @return TRUE if access to the protected structure was granted, FALSE otherwise.
835 * @remarks This is the internal macro for system use only.In case the rundown
836 * was active, then the slow-path will be called through the exported
842 _ExReleaseRundownProtection(IN PEX_RUNDOWN_REF RunRef
)
844 ULONG_PTR Value
, NewValue
;
846 /* Get the current value and mask the active bit */
847 Value
= RunRef
->Count
&~ EX_RUNDOWN_ACTIVE
;
849 /* Remove a reference */
850 NewValue
= Value
- EX_RUNDOWN_COUNT_INC
;
852 /* Change the value */
853 NewValue
= ExpChangeRundown(RunRef
, NewValue
, Value
);
855 /* Check if the rundown was active */
856 if (NewValue
!= Value
)
858 /* Rundown was active, use long path */
859 ExfReleaseRundownProtection(RunRef
);
864 ASSERT((Value
>= EX_RUNDOWN_COUNT_INC
) || (KeNumberProcessors
> 1));
869 * @name ExInitializeRundownProtection
872 * The ExInitializeRundownProtection routine initializes a rundown
873 * protection descriptor.
876 * Pointer to a rundown reference descriptor.
880 * @remarks This is the internal macro for system use only.
885 _ExInitializeRundownProtection(IN PEX_RUNDOWN_REF RunRef
)
887 /* Set the count to zero */
892 * @name ExWaitForRundownProtectionRelease
895 * The ExWaitForRundownProtectionRelease routine waits until the specified
896 * rundown descriptor has been released.
899 * Pointer to a rundown reference descriptor.
903 * @remarks This is the internal macro for system use only. If a wait is actually
904 * necessary, then the slow path is taken through the exported function.
909 _ExWaitForRundownProtectionRelease(IN PEX_RUNDOWN_REF RunRef
)
913 /* Set the active bit */
914 Value
= ExpChangeRundown(RunRef
, EX_RUNDOWN_ACTIVE
, 0);
915 if ((Value
) && (Value
!= EX_RUNDOWN_ACTIVE
))
917 /* If the the rundown wasn't already active, then take the long path */
918 ExfWaitForRundownProtectionRelease(RunRef
);
923 * @name ExRundownCompleted
926 * The ExRundownCompleted routine completes the rundown of the specified
927 * descriptor by setting the active bit.
930 * Pointer to a rundown reference descriptor.
934 * @remarks This is the internal macro for system use only.
939 _ExRundownCompleted(IN PEX_RUNDOWN_REF RunRef
)
942 ASSERT((RunRef
->Count
& EX_RUNDOWN_ACTIVE
) != 0);
944 /* Mark the counter as active */
945 ExpSetRundown(RunRef
, EX_RUNDOWN_ACTIVE
);
948 /* PUSHLOCKS *****************************************************************/
950 /* FIXME: VERIFY THESE! */
955 IN PEX_PUSH_LOCK PushLock
,
962 IN PEX_PUSH_LOCK PushLock
,
963 IN PVOID CurrentWaitBlock
968 ExWaitForUnblockPushLock(
969 IN PEX_PUSH_LOCK PushLock
,
974 * @name _ExInitializePushLock
977 * The _ExInitializePushLock macro initializes a PushLock.
980 * Pointer to the pushlock which is to be initialized.
989 _ExInitializePushLock(OUT PEX_PUSH_LOCK PushLock
)
991 /* Set the value to 0 */
994 #define ExInitializePushLock _ExInitializePushLock
997 * @name ExAcquirePushLockExclusive
1000 * The ExAcquirePushLockExclusive macro exclusively acquires a PushLock.
1003 * Pointer to the pushlock which is to be acquired.
1007 * @remarks The function attempts the quickest route to acquire the lock, which is
1008 * to simply set the lock bit.
1009 * However, if the pushlock is already shared, the slower path is taken.
1011 * Callers of ExAcquirePushLockShared must be running at IRQL <= APC_LEVEL.
1012 * This macro should usually be paired up with KeAcquireCriticalRegion.
1017 ExAcquirePushLockExclusive(PEX_PUSH_LOCK PushLock
)
1019 /* Try acquiring the lock */
1020 if (InterlockedBitTestAndSet((PLONG
)PushLock
, EX_PUSH_LOCK_LOCK_V
))
1022 /* Someone changed it, use the slow path */
1023 ExfAcquirePushLockExclusive(PushLock
);
1027 ASSERT(PushLock
->Locked
);
1031 * @name ExTryToAcquirePushLockExclusive
1034 * The ExAcquirePushLockExclusive macro exclusively acquires a PushLock.
1037 * Pointer to the pushlock which is to be acquired.
1041 * @remarks The function attempts the quickest route to acquire the lock, which is
1042 * to simply set the lock bit.
1043 * However, if the pushlock is already shared, the slower path is taken.
1045 * Callers of ExAcquirePushLockShared must be running at IRQL <= APC_LEVEL.
1046 * This macro should usually be paired up with KeAcquireCriticalRegion.
1051 ExTryToAcquirePushLockExclusive(PEX_PUSH_LOCK PushLock
)
1053 /* Try acquiring the lock */
1054 if (InterlockedBitTestAndSet((PLONG
)PushLock
, EX_PUSH_LOCK_LOCK_V
))
1061 ASSERT (PushLock
->Locked
);
1066 * @name ExAcquirePushLockShared
1069 * The ExAcquirePushLockShared macro acquires a shared PushLock.
1072 * Pointer to the pushlock which is to be acquired.
1076 * @remarks The function attempts the quickest route to acquire the lock, which is
1077 * to simply set the lock bit and set the share count to one.
1078 * However, if the pushlock is already shared, the slower path is taken.
1080 * Callers of ExAcquirePushLockShared must be running at IRQL <= APC_LEVEL.
1081 * This macro should usually be paired up with KeAcquireCriticalRegion.
1086 ExAcquirePushLockShared(PEX_PUSH_LOCK PushLock
)
1088 EX_PUSH_LOCK NewValue
;
1090 /* Try acquiring the lock */
1091 NewValue
.Value
= EX_PUSH_LOCK_LOCK
| EX_PUSH_LOCK_SHARE_INC
;
1092 if (ExpChangePushlock(PushLock
, NewValue
.Ptr
, 0))
1094 /* Someone changed it, use the slow path */
1095 ExfAcquirePushLockShared(PushLock
);
1099 ASSERT(PushLock
->Locked
);
1103 * @name ExConvertPushLockSharedToExclusive
1106 * The ExConvertPushLockSharedToExclusive macro converts an exclusive
1107 * pushlock to a shared pushlock.
1110 * Pointer to the pushlock which is to be converted.
1112 * @return FALSE if conversion failed, TRUE otherwise.
1114 * @remarks The function attempts the quickest route to convert the lock, which is
1115 * to simply set the lock bit and remove any other bits.
1120 ExConvertPushLockSharedToExclusive(IN PEX_PUSH_LOCK PushLock
)
1122 EX_PUSH_LOCK OldValue
;
1124 /* Set the expected old value */
1125 OldValue
.Value
= EX_PUSH_LOCK_LOCK
| EX_PUSH_LOCK_SHARE_INC
;
1127 /* Try converting the lock */
1128 if (ExpChangePushlock(PushLock
, EX_PUSH_LOCK_LOCK
, OldValue
.Value
) !=
1131 /* Conversion failed */
1136 ASSERT(PushLock
->Locked
);
1141 * @name ExWaitOnPushLock
1144 * The ExWaitOnPushLock macro acquires and instantly releases a pushlock.
1147 * Pointer to a pushlock.
1151 * @remarks The function attempts to get any exclusive waiters out of their slow
1152 * path by forcing an instant acquire/release operation.
1154 * Callers of ExWaitOnPushLock must be running at IRQL <= APC_LEVEL.
1159 ExWaitOnPushLock(PEX_PUSH_LOCK PushLock
)
1161 /* Check if we're locked */
1162 if (PushLock
->Locked
)
1164 /* Acquire the lock */
1165 ExfAcquirePushLockExclusive(PushLock
);
1166 ASSERT(PushLock
->Locked
);
1169 ExfReleasePushLockExclusive(PushLock
);
1174 * @name ExReleasePushLockShared
1177 * The ExReleasePushLockShared macro releases a previously acquired PushLock.
1180 * Pointer to a previously acquired pushlock.
1184 * @remarks The function attempts the quickest route to release the lock, which is
1185 * to simply decrease the share count and remove the lock bit.
1186 * However, if the pushlock is being waited on then the long path is taken.
1188 * Callers of ExReleasePushLockShared must be running at IRQL <= APC_LEVEL.
1189 * This macro should usually be paired up with KeLeaveCriticalRegion.
1194 ExReleasePushLockShared(PEX_PUSH_LOCK PushLock
)
1196 EX_PUSH_LOCK OldValue
;
1199 ASSERT(PushLock
->Locked
);
1201 /* Try to clear the pushlock */
1202 OldValue
.Value
= EX_PUSH_LOCK_LOCK
| EX_PUSH_LOCK_SHARE_INC
;
1203 if (ExpChangePushlock(PushLock
, 0, OldValue
.Ptr
) != OldValue
.Ptr
)
1205 /* There are still other people waiting on it */
1206 ExfReleasePushLockShared(PushLock
);
1211 * @name ExReleasePushLockExclusive
1214 * The ExReleasePushLockExclusive macro releases a previously
1215 * exclusively acquired PushLock.
1218 * Pointer to a previously acquired pushlock.
1222 * @remarks The function attempts the quickest route to release the lock, which is
1223 * to simply clear the locked bit.
1224 * However, if the pushlock is being waited on, the slow path is taken
1225 * in an attempt to wake up the lock.
1227 * Callers of ExReleasePushLockExclusive must be running at IRQL <= APC_LEVEL.
1228 * This macro should usually be paired up with KeLeaveCriticalRegion.
1233 ExReleasePushLockExclusive(PEX_PUSH_LOCK PushLock
)
1235 EX_PUSH_LOCK OldValue
;
1238 ASSERT(PushLock
->Locked
);
1240 /* Unlock the pushlock */
1241 OldValue
.Value
= InterlockedExchangeAddSizeT((PSIZE_T
)PushLock
,
1242 -(SSIZE_T
)EX_PUSH_LOCK_LOCK
);
1245 ASSERT(OldValue
.Locked
);
1246 ASSERT(OldValue
.Waiting
|| OldValue
.Shared
== 0);
1248 /* Check if anyone is waiting on it and it's not already waking*/
1249 if ((OldValue
.Waiting
) && !(OldValue
.Waking
))
1252 ExfTryToWakePushLock(PushLock
);
1257 * @name ExReleasePushLock
1260 * The ExReleasePushLock macro releases a previously acquired PushLock.
1263 * Pointer to a previously acquired pushlock.
1267 * @remarks The function attempts the quickest route to release the lock, which is
1268 * to simply clear all the fields and decrease the share count if required.
1269 * However, if the pushlock is being waited on then the long path is taken.
1271 * Callers of ExReleasePushLock must be running at IRQL <= APC_LEVEL.
1272 * This macro should usually be paired up with KeLeaveCriticalRegion.
1277 ExReleasePushLock(PEX_PUSH_LOCK PushLock
)
1279 EX_PUSH_LOCK OldValue
= *PushLock
;
1280 EX_PUSH_LOCK NewValue
;
1283 ASSERT(OldValue
.Locked
);
1285 /* Check if the pushlock is shared */
1286 if (OldValue
.Shared
> 1)
1288 /* Decrease the share count */
1289 NewValue
.Value
= OldValue
.Value
- EX_PUSH_LOCK_SHARE_INC
;
1293 /* Clear the pushlock entirely */
1297 /* Check if nobody is waiting on us and try clearing the lock here */
1298 if ((OldValue
.Waiting
) ||
1299 (ExpChangePushlock(PushLock
, NewValue
.Ptr
, OldValue
.Ptr
) !=
1302 /* We have waiters, use the long path */
1303 ExfReleasePushLock(PushLock
);
1307 /* FAST MUTEX INLINES *********************************************************/
1311 _ExAcquireFastMutexUnsafe(IN PFAST_MUTEX FastMutex
)
1313 PKTHREAD Thread
= KeGetCurrentThread();
1316 ASSERT((KeGetCurrentIrql() == APC_LEVEL
) ||
1317 (Thread
->CombinedApcDisable
!= 0) ||
1318 (Thread
->Teb
== NULL
) ||
1319 (Thread
->Teb
>= (PTEB
)MM_SYSTEM_RANGE_START
));
1320 ASSERT(FastMutex
->Owner
!= Thread
);
1322 /* Decrease the count */
1323 if (InterlockedDecrement(&FastMutex
->Count
))
1325 /* Someone is still holding it, use slow path */
1326 KiAcquireFastMutex(FastMutex
);
1330 FastMutex
->Owner
= Thread
;
1335 _ExReleaseFastMutexUnsafe(IN OUT PFAST_MUTEX FastMutex
)
1337 ASSERT((KeGetCurrentIrql() == APC_LEVEL
) ||
1338 (KeGetCurrentThread()->CombinedApcDisable
!= 0) ||
1339 (KeGetCurrentThread()->Teb
== NULL
) ||
1340 (KeGetCurrentThread()->Teb
>= (PTEB
)MM_SYSTEM_RANGE_START
));
1341 ASSERT(FastMutex
->Owner
== KeGetCurrentThread());
1343 /* Erase the owner */
1344 FastMutex
->Owner
= NULL
;
1346 /* Increase the count */
1347 if (InterlockedIncrement(&FastMutex
->Count
) <= 0)
1349 /* Someone was waiting for it, signal the waiter */
1350 KeSetEventBoostPriority(&FastMutex
->Event
, NULL
);
1356 _ExAcquireFastMutex(IN PFAST_MUTEX FastMutex
)
1359 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
1361 /* Raise IRQL to APC */
1362 KeRaiseIrql(APC_LEVEL
, &OldIrql
);
1364 /* Decrease the count */
1365 if (InterlockedDecrement(&FastMutex
->Count
))
1367 /* Someone is still holding it, use slow path */
1368 KiAcquireFastMutex(FastMutex
);
1371 /* Set the owner and IRQL */
1372 FastMutex
->Owner
= KeGetCurrentThread();
1373 FastMutex
->OldIrql
= OldIrql
;
1378 _ExReleaseFastMutex(IN OUT PFAST_MUTEX FastMutex
)
1381 ASSERT(KeGetCurrentIrql() == APC_LEVEL
);
1383 /* Erase the owner */
1384 FastMutex
->Owner
= NULL
;
1385 OldIrql
= (KIRQL
)FastMutex
->OldIrql
;
1387 /* Increase the count */
1388 if (InterlockedIncrement(&FastMutex
->Count
) <= 0)
1390 /* Someone was waiting for it, signal the waiter */
1391 KeSetEventBoostPriority(&FastMutex
->Event
, NULL
);
1394 /* Lower IRQL back */
1395 KeLowerIrql(OldIrql
);
1400 _ExTryToAcquireFastMutex(IN OUT PFAST_MUTEX FastMutex
)
1403 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
1405 /* Raise to APC_LEVEL */
1406 KeRaiseIrql(APC_LEVEL
, &OldIrql
);
1408 /* Check if we can quickly acquire it */
1409 if (InterlockedCompareExchange(&FastMutex
->Count
, 0, 1) == 1)
1411 /* We have, set us as owners */
1412 FastMutex
->Owner
= KeGetCurrentThread();
1413 FastMutex
->OldIrql
= OldIrql
;
1418 /* Acquire attempt failed */
1419 KeLowerIrql(OldIrql
);
1427 _ExEnterCriticalRegionAndAcquireFastMutexUnsafe(IN OUT PFAST_MUTEX FastMutex
)
1429 /* Enter the Critical Region */
1430 KeEnterCriticalRegion();
1432 /* Acquire the mutex unsafely */
1433 _ExAcquireFastMutexUnsafe(FastMutex
);
1438 _ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(IN OUT PFAST_MUTEX FastMutex
)
1440 /* Release the mutex unsafely */
1441 _ExReleaseFastMutexUnsafe(FastMutex
);
1443 /* Leave the critical region */
1444 KeLeaveCriticalRegion();
1447 /* OTHER FUNCTIONS **********************************************************/
1451 ExTryToAcquireResourceExclusiveLite(
1452 IN PERESOURCE Resource
1456 ExpSetTimeZoneInformation(
1457 IN PRTL_TIME_ZONE_INFORMATION TimeZoneInformation
1462 ExAcquireTimeRefreshLock(
1468 ExReleaseTimeRefreshLock(
1474 ExUpdateSystemTimeFromCmos(
1475 IN BOOLEAN UpdateInterruptTime
,
1476 IN ULONG MaxSepInSeconds
1481 ExAllocateLocallyUniqueId(
1482 OUT LUID
*LocallyUniqueId
1495 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1502 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1505 #define InterlockedDecrementUL(Addend) \
1506 (ULONG)InterlockedDecrement((PLONG)(Addend))
1508 #define InterlockedIncrementUL(Addend) \
1509 (ULONG)InterlockedIncrement((PLONG)(Addend))
1511 #define InterlockedExchangeUL(Target, Value) \
1512 (ULONG)InterlockedExchange((PLONG)(Target), (LONG)(Value))
1514 #define InterlockedExchangeAddUL(Addend, Value) \
1515 (ULONG)InterlockedExchangeAdd((PLONG)(Addend), (LONG)(Value))
1517 #define InterlockedCompareExchangeUL(Destination, Exchange, Comperand) \
1518 (ULONG)InterlockedCompareExchange((PLONG)(Destination), (LONG)(Exchange), (LONG)(Comperand))
1520 #define InterlockedCompareExchangeSizeT(Destination, Exchange, Comperand) \
1521 (SIZE_T)InterlockedCompareExchangePointer((PVOID*)(Destination), (PVOID)(SIZE_T)(Exchange), (PVOID)(SIZE_T)(Comperand))
1523 #define ExfInterlockedCompareExchange64UL(Destination, Exchange, Comperand) \
1524 (ULONGLONG)ExfInterlockedCompareExchange64((PLONGLONG)(Destination), (PLONGLONG)(Exchange), (PLONGLONG)(Comperand))