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
);
241 ExpUuidInitialization(VOID
);
246 ExLuidInitialization(VOID
);
251 ExpInitializeExecutive(
253 IN PLOADER_PARAMETER_BLOCK LoaderBlock
258 ExShutdownSystem(VOID
);
263 ExpInitializeEventImplementation(VOID
);
268 ExpInitializeKeyedEventImplementation(VOID
);
273 ExpInitializeEventPairImplementation(VOID
);
278 ExpInitializeSemaphoreImplementation(VOID
);
283 ExpInitializeMutantImplementation(VOID
);
288 ExpInitializeTimerImplementation(VOID
);
293 ExpInitializeProfileImplementation(VOID
);
298 ExpResourceInitialization(VOID
);
303 ExInitPoolLookasidePointers(VOID
);
305 /* Callback Functions ********************************************************/
309 ExInitializeCallBack(
310 IN OUT PEX_CALLBACK Callback
313 PEX_CALLBACK_ROUTINE_BLOCK
316 IN PEX_CALLBACK_FUNCTION Function
,
323 IN PEX_CALLBACK_ROUTINE_BLOCK CallbackRoutineBlock
328 ExCompareExchangeCallBack (
329 IN OUT PEX_CALLBACK CallBack
,
330 IN PEX_CALLBACK_ROUTINE_BLOCK NewBlock
,
331 IN PEX_CALLBACK_ROUTINE_BLOCK OldBlock
334 PEX_CALLBACK_ROUTINE_BLOCK
336 ExReferenceCallBackBlock(
337 IN OUT PEX_CALLBACK CallBack
342 ExDereferenceCallBackBlock(
343 IN OUT PEX_CALLBACK CallBack
,
344 IN PEX_CALLBACK_ROUTINE_BLOCK CallbackRoutineBlock
347 PEX_CALLBACK_FUNCTION
349 ExGetCallBackBlockRoutine(
350 IN PEX_CALLBACK_ROUTINE_BLOCK CallbackRoutineBlock
355 ExGetCallBackBlockContext(
356 IN PEX_CALLBACK_ROUTINE_BLOCK CallbackRoutineBlock
362 IN PEX_CALLBACK_ROUTINE_BLOCK CallbackRoutineBlock
365 /* Rundown Functions ********************************************************/
369 ExfInitializeRundownProtection(
370 OUT PEX_RUNDOWN_REF RunRef
375 ExfReInitializeRundownProtection(
376 OUT PEX_RUNDOWN_REF RunRef
381 ExfAcquireRundownProtection(
382 IN OUT PEX_RUNDOWN_REF RunRef
387 ExfAcquireRundownProtectionEx(
388 IN OUT PEX_RUNDOWN_REF RunRef
,
394 ExfReleaseRundownProtection(
395 IN OUT PEX_RUNDOWN_REF RunRef
400 ExfReleaseRundownProtectionEx(
401 IN OUT PEX_RUNDOWN_REF RunRef
,
408 OUT PEX_RUNDOWN_REF RunRef
413 ExfWaitForRundownProtectionRelease(
414 IN OUT PEX_RUNDOWN_REF RunRef
417 /* HANDLE TABLE FUNCTIONS ***************************************************/
420 (NTAPI
*PEX_SWEEP_HANDLE_CALLBACK
)(
421 PHANDLE_TABLE_ENTRY HandleTableEntry
,
427 (NTAPI
*PEX_DUPLICATE_HANDLE_CALLBACK
)(
428 IN PEPROCESS Process
,
429 IN PHANDLE_TABLE HandleTable
,
430 IN PHANDLE_TABLE_ENTRY HandleTableEntry
,
431 IN PHANDLE_TABLE_ENTRY NewEntry
435 (NTAPI
*PEX_CHANGE_HANDLE_CALLBACK
)(
436 PHANDLE_TABLE_ENTRY HandleTableEntry
,
443 ExpInitializeHandleTables(
450 IN PEPROCESS Process OPTIONAL
455 ExUnlockHandleTableEntry(
456 IN PHANDLE_TABLE HandleTable
,
457 IN PHANDLE_TABLE_ENTRY HandleTableEntry
463 IN PHANDLE_TABLE HandleTable
,
464 IN PHANDLE_TABLE_ENTRY HandleTableEntry
469 ExDestroyHandleTable(
470 IN PHANDLE_TABLE HandleTable
,
471 IN PVOID DestroyHandleProcedure OPTIONAL
477 IN PHANDLE_TABLE HandleTable
,
479 IN PHANDLE_TABLE_ENTRY HandleTableEntry OPTIONAL
484 ExMapHandleToPointer(
485 IN PHANDLE_TABLE HandleTable
,
492 IN PEPROCESS Process
,
493 IN PHANDLE_TABLE HandleTable
,
494 IN PEX_DUPLICATE_HANDLE_CALLBACK DupHandleProcedure
,
501 IN PHANDLE_TABLE HandleTable
,
503 IN PEX_CHANGE_HANDLE_CALLBACK ChangeRoutine
,
510 IN PHANDLE_TABLE HandleTable
,
511 IN PEX_SWEEP_HANDLE_CALLBACK EnumHandleProcedure
,
517 ExpLookupHandleTableEntry(
518 IN PHANDLE_TABLE HandleTable
,
524 ExpLockHandleTableEntry(
525 IN PHANDLE_TABLE HandleTable
,
526 IN PHANDLE_TABLE_ENTRY HandleTableEntry
529 /* PSEH EXCEPTION HANDLING **************************************************/
533 ExSystemExceptionFilter(VOID
);
535 /* CALLBACKS *****************************************************************/
539 ExDoCallBack(IN OUT PEX_CALLBACK Callback
,
544 PEX_CALLBACK_ROUTINE_BLOCK CallbackBlock
;
545 PEX_CALLBACK_FUNCTION Function
;
547 /* Reference the block */
548 CallbackBlock
= ExReferenceCallBackBlock(Callback
);
551 /* Get the function */
552 Function
= ExGetCallBackBlockRoutine(CallbackBlock
);
554 /* Do the callback */
555 Function(Context
, Argument1
, Argument2
);
557 /* Now dereference it */
558 ExDereferenceCallBackBlock(Callback
, CallbackBlock
);
562 /* FAST REFS ******************************************************************/
566 ExGetObjectFastReference(IN EX_FAST_REF FastRef
)
568 /* Return the unbiased pointer */
569 return (PVOID
)(FastRef
.Value
& ~MAX_FAST_REFS
);
574 ExGetCountFastReference(IN EX_FAST_REF FastRef
)
576 /* Return the reference count */
577 return (ULONG
)FastRef
.RefCnt
;
582 ExInitializeFastReference(OUT PEX_FAST_REF FastRef
,
583 IN OPTIONAL PVOID Object
)
586 ASSERT((((ULONG_PTR
)Object
) & MAX_FAST_REFS
) == 0);
588 /* Check if an object is being set */
591 /* Clear the field */
592 FastRef
->Object
= NULL
;
596 /* Otherwise, we assume the object was referenced and is ready */
597 FastRef
->Value
= (ULONG_PTR
)Object
| MAX_FAST_REFS
;
603 ExAcquireFastReference(IN OUT PEX_FAST_REF FastRef
)
605 EX_FAST_REF OldValue
, NewValue
;
607 /* Start reference loop */
610 /* Get the current reference count */
614 /* Increase the reference count */
615 NewValue
.Value
= OldValue
.Value
- 1;
616 NewValue
.Object
= ExpChangePushlock(&FastRef
->Object
,
619 if (NewValue
.Object
!= OldValue
.Object
) continue;
626 /* Return the old value */
632 ExInsertFastReference(IN OUT PEX_FAST_REF FastRef
,
635 EX_FAST_REF OldValue
, NewValue
;
638 ASSERT(!(((ULONG_PTR
)Object
) & MAX_FAST_REFS
));
640 /* Start update loop */
643 /* Get the current reference count */
646 /* Check if the current count is too high or if the pointer changed */
647 if (((OldValue
.RefCnt
+ MAX_FAST_REFS
) > MAX_FAST_REFS
) ||
648 ((OldValue
.Value
&~ MAX_FAST_REFS
) != (ULONG_PTR
)Object
))
654 /* Update the reference count */
655 NewValue
.Value
= OldValue
.Value
+ MAX_FAST_REFS
;
656 NewValue
.Object
= ExpChangePushlock(&FastRef
->Object
,
659 if (NewValue
.Object
!= OldValue
.Object
) continue;
671 ExReleaseFastReference(IN PEX_FAST_REF FastRef
,
674 EX_FAST_REF OldValue
, NewValue
;
677 ASSERT(Object
!= NULL
);
678 ASSERT(!(((ULONG_PTR
)Object
) & MAX_FAST_REFS
));
680 /* Start reference loop */
683 /* Get the current reference count */
686 /* Check if we're full if if the pointer changed */
687 if ((OldValue
.Value
^ (ULONG_PTR
)Object
) >= MAX_FAST_REFS
) return FALSE
;
689 /* Decrease the reference count */
690 NewValue
.Value
= OldValue
.Value
+ 1;
691 NewValue
.Object
= ExpChangePushlock(&FastRef
->Object
,
694 if (NewValue
.Object
!= OldValue
.Object
) continue;
706 ExSwapFastReference(IN PEX_FAST_REF FastRef
,
709 EX_FAST_REF NewValue
, OldValue
;
712 ASSERT((((ULONG_PTR
)Object
) & MAX_FAST_REFS
) == 0);
714 /* Check if an object is being set */
717 /* Clear the field */
718 NewValue
.Object
= NULL
;
722 /* Otherwise, we assume the object was referenced and is ready */
723 NewValue
.Value
= (ULONG_PTR
)Object
| MAX_FAST_REFS
;
726 /* Update the object */
727 OldValue
.Object
= InterlockedExchangePointer(&FastRef
->Object
, NewValue
.Object
);
733 ExCompareSwapFastReference(IN PEX_FAST_REF FastRef
,
737 EX_FAST_REF OldValue
, NewValue
;
739 /* Sanity check and start swap loop */
740 ASSERT(!(((ULONG_PTR
)Object
) & MAX_FAST_REFS
));
743 /* Get the current value */
746 /* Make sure there's enough references to swap */
747 if (!((OldValue
.Value
^ (ULONG_PTR
)OldObject
) <= MAX_FAST_REFS
)) break;
749 /* Check if we have an object to swap */
752 /* Set up the value with maximum fast references */
753 NewValue
.Value
= (ULONG_PTR
)Object
| MAX_FAST_REFS
;
757 /* Write the object address itself (which is empty) */
758 NewValue
.Value
= (ULONG_PTR
)Object
;
761 /* Do the actual compare exchange */
762 NewValue
.Object
= ExpChangePushlock(&FastRef
->Object
,
765 if (NewValue
.Object
!= OldValue
.Object
) continue;
771 /* Return the old value */
775 /* RUNDOWN *******************************************************************/
779 ExGetRunRefForGivenProcessor(IN PEX_RUNDOWN_REF_CACHE_AWARE RunRefCacheAware
,
782 return (PEX_RUNDOWN_REF
)((ULONG_PTR
)RunRefCacheAware
->RunRefs
+
783 RunRefCacheAware
->RunRefSize
*
784 (ProcNumber
% RunRefCacheAware
->Number
));
788 * @name ExfAcquireRundownProtection
791 * The ExfAcquireRundownProtection routine acquires rundown protection for
792 * the specified descriptor.
795 * Pointer to a rundown reference descriptor.
797 * @return TRUE if access to the protected structure was granted, FALSE otherwise.
799 * @remarks This is the internal macro for system use only.In case the rundown
800 * was active, then the slow-path will be called through the exported
806 _ExAcquireRundownProtection(IN PEX_RUNDOWN_REF RunRef
)
808 ULONG_PTR Value
, NewValue
;
810 /* Get the current value and mask the active bit */
811 Value
= RunRef
->Count
&~ EX_RUNDOWN_ACTIVE
;
813 /* Add a reference */
814 NewValue
= Value
+ EX_RUNDOWN_COUNT_INC
;
816 /* Change the value */
817 NewValue
= ExpChangeRundown(RunRef
, NewValue
, Value
);
818 if (NewValue
!= Value
)
820 /* Rundown was active, use long path */
821 return ExfAcquireRundownProtection(RunRef
);
829 * @name ExReleaseRundownProtection
832 * The ExReleaseRundownProtection routine releases rundown protection for
833 * the specified descriptor.
836 * Pointer to a rundown reference descriptor.
838 * @return TRUE if access to the protected structure was granted, FALSE otherwise.
840 * @remarks This is the internal macro for system use only.In case the rundown
841 * was active, then the slow-path will be called through the exported
847 _ExReleaseRundownProtection(IN PEX_RUNDOWN_REF RunRef
)
849 ULONG_PTR Value
, NewValue
;
851 /* Get the current value and mask the active bit */
852 Value
= RunRef
->Count
&~ EX_RUNDOWN_ACTIVE
;
854 /* Remove a reference */
855 NewValue
= Value
- EX_RUNDOWN_COUNT_INC
;
857 /* Change the value */
858 NewValue
= ExpChangeRundown(RunRef
, NewValue
, Value
);
860 /* Check if the rundown was active */
861 if (NewValue
!= Value
)
863 /* Rundown was active, use long path */
864 ExfReleaseRundownProtection(RunRef
);
869 ASSERT((Value
>= EX_RUNDOWN_COUNT_INC
) || (KeNumberProcessors
> 1));
874 * @name ExInitializeRundownProtection
877 * The ExInitializeRundownProtection routine initializes a rundown
878 * protection descriptor.
881 * Pointer to a rundown reference descriptor.
885 * @remarks This is the internal macro for system use only.
890 _ExInitializeRundownProtection(IN PEX_RUNDOWN_REF RunRef
)
892 /* Set the count to zero */
897 * @name ExWaitForRundownProtectionRelease
900 * The ExWaitForRundownProtectionRelease routine waits until the specified
901 * rundown descriptor has been released.
904 * Pointer to a rundown reference descriptor.
908 * @remarks This is the internal macro for system use only. If a wait is actually
909 * necessary, then the slow path is taken through the exported function.
914 _ExWaitForRundownProtectionRelease(IN PEX_RUNDOWN_REF RunRef
)
918 /* Set the active bit */
919 Value
= ExpChangeRundown(RunRef
, EX_RUNDOWN_ACTIVE
, 0);
920 if ((Value
) && (Value
!= EX_RUNDOWN_ACTIVE
))
922 /* If the the rundown wasn't already active, then take the long path */
923 ExfWaitForRundownProtectionRelease(RunRef
);
928 * @name ExRundownCompleted
931 * The ExRundownCompleted routine completes the rundown of the specified
932 * descriptor by setting the active bit.
935 * Pointer to a rundown reference descriptor.
939 * @remarks This is the internal macro for system use only.
944 _ExRundownCompleted(IN PEX_RUNDOWN_REF RunRef
)
947 ASSERT((RunRef
->Count
& EX_RUNDOWN_ACTIVE
) != 0);
949 /* Mark the counter as active */
950 ExpSetRundown(RunRef
, EX_RUNDOWN_ACTIVE
);
953 /* PUSHLOCKS *****************************************************************/
955 /* FIXME: VERIFY THESE! */
960 IN PEX_PUSH_LOCK PushLock
,
967 IN PEX_PUSH_LOCK PushLock
,
968 IN PVOID CurrentWaitBlock
973 ExWaitForUnblockPushLock(
974 IN PEX_PUSH_LOCK PushLock
,
979 * @name _ExInitializePushLock
982 * The _ExInitializePushLock macro initializes a PushLock.
985 * Pointer to the pushlock which is to be initialized.
994 _ExInitializePushLock(OUT PEX_PUSH_LOCK PushLock
)
996 /* Set the value to 0 */
999 #define ExInitializePushLock _ExInitializePushLock
1002 * @name ExAcquirePushLockExclusive
1005 * The ExAcquirePushLockExclusive macro exclusively acquires a PushLock.
1008 * Pointer to the pushlock which is to be acquired.
1012 * @remarks The function attempts the quickest route to acquire the lock, which is
1013 * to simply set the lock bit.
1014 * However, if the pushlock is already shared, the slower path is taken.
1016 * Callers of ExAcquirePushLockShared must be running at IRQL <= APC_LEVEL.
1017 * This macro should usually be paired up with KeAcquireCriticalRegion.
1022 ExAcquirePushLockExclusive(PEX_PUSH_LOCK PushLock
)
1024 /* Try acquiring the lock */
1025 if (InterlockedBitTestAndSet((PLONG
)PushLock
, EX_PUSH_LOCK_LOCK_V
))
1027 /* Someone changed it, use the slow path */
1028 ExfAcquirePushLockExclusive(PushLock
);
1032 ASSERT(PushLock
->Locked
);
1036 * @name ExTryToAcquirePushLockExclusive
1039 * The ExAcquirePushLockExclusive macro exclusively acquires a PushLock.
1042 * Pointer to the pushlock which is to be acquired.
1046 * @remarks The function attempts the quickest route to acquire the lock, which is
1047 * to simply set the lock bit.
1048 * However, if the pushlock is already shared, the slower path is taken.
1050 * Callers of ExAcquirePushLockShared must be running at IRQL <= APC_LEVEL.
1051 * This macro should usually be paired up with KeAcquireCriticalRegion.
1056 ExTryToAcquirePushLockExclusive(PEX_PUSH_LOCK PushLock
)
1058 /* Try acquiring the lock */
1059 if (InterlockedBitTestAndSet((PLONG
)PushLock
, EX_PUSH_LOCK_LOCK_V
))
1066 ASSERT (PushLock
->Locked
);
1071 * @name ExAcquirePushLockShared
1074 * The ExAcquirePushLockShared macro acquires a shared PushLock.
1077 * Pointer to the pushlock which is to be acquired.
1081 * @remarks The function attempts the quickest route to acquire the lock, which is
1082 * to simply set the lock bit and set the share count to one.
1083 * However, if the pushlock is already shared, the slower path is taken.
1085 * Callers of ExAcquirePushLockShared must be running at IRQL <= APC_LEVEL.
1086 * This macro should usually be paired up with KeAcquireCriticalRegion.
1091 ExAcquirePushLockShared(PEX_PUSH_LOCK PushLock
)
1093 EX_PUSH_LOCK NewValue
;
1095 /* Try acquiring the lock */
1096 NewValue
.Value
= EX_PUSH_LOCK_LOCK
| EX_PUSH_LOCK_SHARE_INC
;
1097 if (ExpChangePushlock(PushLock
, NewValue
.Ptr
, 0))
1099 /* Someone changed it, use the slow path */
1100 ExfAcquirePushLockShared(PushLock
);
1104 ASSERT(PushLock
->Locked
);
1108 * @name ExConvertPushLockSharedToExclusive
1111 * The ExConvertPushLockSharedToExclusive macro converts an exclusive
1112 * pushlock to a shared pushlock.
1115 * Pointer to the pushlock which is to be converted.
1117 * @return FALSE if conversion failed, TRUE otherwise.
1119 * @remarks The function attempts the quickest route to convert the lock, which is
1120 * to simply set the lock bit and remove any other bits.
1125 ExConvertPushLockSharedToExclusive(IN PEX_PUSH_LOCK PushLock
)
1127 EX_PUSH_LOCK OldValue
;
1129 /* Set the expected old value */
1130 OldValue
.Value
= EX_PUSH_LOCK_LOCK
| EX_PUSH_LOCK_SHARE_INC
;
1132 /* Try converting the lock */
1133 if (ExpChangePushlock(PushLock
, EX_PUSH_LOCK_LOCK
, OldValue
.Value
) !=
1136 /* Conversion failed */
1141 ASSERT(PushLock
->Locked
);
1146 * @name ExWaitOnPushLock
1149 * The ExWaitOnPushLock macro acquires and instantly releases a pushlock.
1152 * Pointer to a pushlock.
1156 * @remarks The function attempts to get any exclusive waiters out of their slow
1157 * path by forcing an instant acquire/release operation.
1159 * Callers of ExWaitOnPushLock must be running at IRQL <= APC_LEVEL.
1164 ExWaitOnPushLock(PEX_PUSH_LOCK PushLock
)
1166 /* Check if we're locked */
1167 if (PushLock
->Locked
)
1169 /* Acquire the lock */
1170 ExfAcquirePushLockExclusive(PushLock
);
1171 ASSERT(PushLock
->Locked
);
1174 ExfReleasePushLockExclusive(PushLock
);
1179 * @name ExReleasePushLockShared
1182 * The ExReleasePushLockShared macro releases a previously acquired PushLock.
1185 * Pointer to a previously acquired pushlock.
1189 * @remarks The function attempts the quickest route to release the lock, which is
1190 * to simply decrease the share count and remove the lock bit.
1191 * However, if the pushlock is being waited on then the long path is taken.
1193 * Callers of ExReleasePushLockShared must be running at IRQL <= APC_LEVEL.
1194 * This macro should usually be paired up with KeLeaveCriticalRegion.
1199 ExReleasePushLockShared(PEX_PUSH_LOCK PushLock
)
1201 EX_PUSH_LOCK OldValue
;
1204 ASSERT(PushLock
->Locked
);
1206 /* Try to clear the pushlock */
1207 OldValue
.Value
= EX_PUSH_LOCK_LOCK
| EX_PUSH_LOCK_SHARE_INC
;
1208 if (ExpChangePushlock(PushLock
, 0, OldValue
.Ptr
) != OldValue
.Ptr
)
1210 /* There are still other people waiting on it */
1211 ExfReleasePushLockShared(PushLock
);
1216 * @name ExReleasePushLockExclusive
1219 * The ExReleasePushLockExclusive macro releases a previously
1220 * exclusively acquired PushLock.
1223 * Pointer to a previously acquired pushlock.
1227 * @remarks The function attempts the quickest route to release the lock, which is
1228 * to simply clear the locked bit.
1229 * However, if the pushlock is being waited on, the slow path is taken
1230 * in an attempt to wake up the lock.
1232 * Callers of ExReleasePushLockExclusive must be running at IRQL <= APC_LEVEL.
1233 * This macro should usually be paired up with KeLeaveCriticalRegion.
1238 ExReleasePushLockExclusive(PEX_PUSH_LOCK PushLock
)
1240 EX_PUSH_LOCK OldValue
;
1243 ASSERT(PushLock
->Locked
);
1245 /* Unlock the pushlock */
1246 OldValue
.Value
= InterlockedExchangeAddSizeT((PSIZE_T
)PushLock
,
1247 -(SSIZE_T
)EX_PUSH_LOCK_LOCK
);
1250 ASSERT(OldValue
.Locked
);
1251 ASSERT(OldValue
.Waiting
|| OldValue
.Shared
== 0);
1253 /* Check if anyone is waiting on it and it's not already waking*/
1254 if ((OldValue
.Waiting
) && !(OldValue
.Waking
))
1257 ExfTryToWakePushLock(PushLock
);
1262 * @name ExReleasePushLock
1265 * The ExReleasePushLock macro releases a previously acquired PushLock.
1268 * Pointer to a previously acquired pushlock.
1272 * @remarks The function attempts the quickest route to release the lock, which is
1273 * to simply clear all the fields and decrease the share count if required.
1274 * However, if the pushlock is being waited on then the long path is taken.
1276 * Callers of ExReleasePushLock must be running at IRQL <= APC_LEVEL.
1277 * This macro should usually be paired up with KeLeaveCriticalRegion.
1282 ExReleasePushLock(PEX_PUSH_LOCK PushLock
)
1284 EX_PUSH_LOCK OldValue
= *PushLock
;
1285 EX_PUSH_LOCK NewValue
;
1288 ASSERT(OldValue
.Locked
);
1290 /* Check if the pushlock is shared */
1291 if (OldValue
.Shared
> 1)
1293 /* Decrease the share count */
1294 NewValue
.Value
= OldValue
.Value
- EX_PUSH_LOCK_SHARE_INC
;
1298 /* Clear the pushlock entirely */
1302 /* Check if nobody is waiting on us and try clearing the lock here */
1303 if ((OldValue
.Waiting
) ||
1304 (ExpChangePushlock(PushLock
, NewValue
.Ptr
, OldValue
.Ptr
) !=
1307 /* We have waiters, use the long path */
1308 ExfReleasePushLock(PushLock
);
1312 /* FAST MUTEX INLINES *********************************************************/
1316 _ExAcquireFastMutexUnsafe(IN PFAST_MUTEX FastMutex
)
1318 PKTHREAD Thread
= KeGetCurrentThread();
1321 ASSERT((KeGetCurrentIrql() == APC_LEVEL
) ||
1322 (Thread
->CombinedApcDisable
!= 0) ||
1323 (Thread
->Teb
== NULL
) ||
1324 (Thread
->Teb
>= (PTEB
)MM_SYSTEM_RANGE_START
));
1325 ASSERT(FastMutex
->Owner
!= Thread
);
1327 /* Decrease the count */
1328 if (InterlockedDecrement(&FastMutex
->Count
))
1330 /* Someone is still holding it, use slow path */
1331 KiAcquireFastMutex(FastMutex
);
1335 FastMutex
->Owner
= Thread
;
1340 _ExReleaseFastMutexUnsafe(IN OUT PFAST_MUTEX FastMutex
)
1342 ASSERT((KeGetCurrentIrql() == APC_LEVEL
) ||
1343 (KeGetCurrentThread()->CombinedApcDisable
!= 0) ||
1344 (KeGetCurrentThread()->Teb
== NULL
) ||
1345 (KeGetCurrentThread()->Teb
>= (PTEB
)MM_SYSTEM_RANGE_START
));
1346 ASSERT(FastMutex
->Owner
== KeGetCurrentThread());
1348 /* Erase the owner */
1349 FastMutex
->Owner
= NULL
;
1351 /* Increase the count */
1352 if (InterlockedIncrement(&FastMutex
->Count
) <= 0)
1354 /* Someone was waiting for it, signal the waiter */
1355 KeSetEventBoostPriority(&FastMutex
->Event
, NULL
);
1361 _ExAcquireFastMutex(IN PFAST_MUTEX FastMutex
)
1364 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
1366 /* Raise IRQL to APC */
1367 KeRaiseIrql(APC_LEVEL
, &OldIrql
);
1369 /* Decrease the count */
1370 if (InterlockedDecrement(&FastMutex
->Count
))
1372 /* Someone is still holding it, use slow path */
1373 KiAcquireFastMutex(FastMutex
);
1376 /* Set the owner and IRQL */
1377 FastMutex
->Owner
= KeGetCurrentThread();
1378 FastMutex
->OldIrql
= OldIrql
;
1383 _ExReleaseFastMutex(IN OUT PFAST_MUTEX FastMutex
)
1386 ASSERT(KeGetCurrentIrql() == APC_LEVEL
);
1388 /* Erase the owner */
1389 FastMutex
->Owner
= NULL
;
1390 OldIrql
= (KIRQL
)FastMutex
->OldIrql
;
1392 /* Increase the count */
1393 if (InterlockedIncrement(&FastMutex
->Count
) <= 0)
1395 /* Someone was waiting for it, signal the waiter */
1396 KeSetEventBoostPriority(&FastMutex
->Event
, NULL
);
1399 /* Lower IRQL back */
1400 KeLowerIrql(OldIrql
);
1405 _ExTryToAcquireFastMutex(IN OUT PFAST_MUTEX FastMutex
)
1408 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
1410 /* Raise to APC_LEVEL */
1411 KeRaiseIrql(APC_LEVEL
, &OldIrql
);
1413 /* Check if we can quickly acquire it */
1414 if (InterlockedCompareExchange(&FastMutex
->Count
, 0, 1) == 1)
1416 /* We have, set us as owners */
1417 FastMutex
->Owner
= KeGetCurrentThread();
1418 FastMutex
->OldIrql
= OldIrql
;
1423 /* Acquire attempt failed */
1424 KeLowerIrql(OldIrql
);
1432 _ExEnterCriticalRegionAndAcquireFastMutexUnsafe(IN OUT PFAST_MUTEX FastMutex
)
1434 /* Enter the Critical Region */
1435 KeEnterCriticalRegion();
1437 /* Acquire the mutex unsafely */
1438 _ExAcquireFastMutexUnsafe(FastMutex
);
1443 _ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(IN OUT PFAST_MUTEX FastMutex
)
1445 /* Release the mutex unsafely */
1446 _ExReleaseFastMutexUnsafe(FastMutex
);
1448 /* Leave the critical region */
1449 KeLeaveCriticalRegion();
1452 /* OTHER FUNCTIONS **********************************************************/
1456 ExTryToAcquireResourceExclusiveLite(
1457 IN PERESOURCE Resource
1461 ExpSetTimeZoneInformation(
1462 IN PRTL_TIME_ZONE_INFORMATION TimeZoneInformation
1467 ExAcquireTimeRefreshLock(
1473 ExReleaseTimeRefreshLock(
1479 ExUpdateSystemTimeFromCmos(
1480 IN BOOLEAN UpdateInterruptTime
,
1481 IN ULONG MaxSepInSeconds
1486 ExAllocateLocallyUniqueId(
1487 OUT LUID
*LocallyUniqueId
1500 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1507 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1510 #define InterlockedDecrementUL(Addend) \
1511 (ULONG)InterlockedDecrement((PLONG)(Addend))
1513 #define InterlockedIncrementUL(Addend) \
1514 (ULONG)InterlockedIncrement((PLONG)(Addend))
1516 #define InterlockedExchangeUL(Target, Value) \
1517 (ULONG)InterlockedExchange((PLONG)(Target), (LONG)(Value))
1519 #define InterlockedExchangeAddUL(Addend, Value) \
1520 (ULONG)InterlockedExchangeAdd((PLONG)(Addend), (LONG)(Value))
1522 #define InterlockedCompareExchangeUL(Destination, Exchange, Comperand) \
1523 (ULONG)InterlockedCompareExchange((PLONG)(Destination), (LONG)(Exchange), (LONG)(Comperand))
1525 #define InterlockedCompareExchangeSizeT(Destination, Exchange, Comperand) \
1526 (SIZE_T)InterlockedCompareExchangePointer((PVOID*)(Destination), (PVOID)(SIZE_T)(Exchange), (PVOID)(SIZE_T)(Comperand))
1528 #define ExfInterlockedCompareExchange64UL(Destination, Exchange, Comperand) \
1529 (ULONGLONG)ExfInterlockedCompareExchange64((PLONGLONG)(Destination), (PLONGLONG)(Exchange), (PLONGLONG)(Comperand))