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 /* INITIALIZATION FUNCTIONS *************************************************/
182 Phase1Initialization(
188 ExpInitializePushLocks(VOID
);
192 ExRefreshTimeZoneInformation(
193 IN PLARGE_INTEGER SystemBootTime
198 ExpInitializeWorkerThreads(VOID
);
202 ExSwapinWorkerThreads(IN BOOLEAN AllowSwap
);
206 ExpInitLookasideLists(VOID
);
210 ExInitializeSystemLookasideList(
211 IN PGENERAL_LOOKASIDE List
,
215 IN USHORT MaximumDepth
,
216 IN PLIST_ENTRY ListHead
221 ExpInitializeCallbacks(VOID
);
229 ExpInitializeExecutive(
231 IN PLOADER_PARAMETER_BLOCK LoaderBlock
236 ExShutdownSystem(VOID
);
240 ExpInitializeEventImplementation(VOID
);
244 ExpInitializeKeyedEventImplementation(VOID
);
248 ExpInitializeEventPairImplementation(VOID
);
252 ExpInitializeSemaphoreImplementation(VOID
);
256 ExpInitializeMutantImplementation(VOID
);
260 ExpInitializeTimerImplementation(VOID
);
264 ExpInitializeProfileImplementation(VOID
);
268 ExpResourceInitialization(VOID
);
272 ExInitPoolLookasidePointers(VOID
);
274 /* Callback Functions ********************************************************/
278 ExInitializeCallBack(
279 IN OUT PEX_CALLBACK Callback
282 PEX_CALLBACK_ROUTINE_BLOCK
285 IN PEX_CALLBACK_FUNCTION Function
,
292 IN PEX_CALLBACK_ROUTINE_BLOCK CallbackRoutineBlock
297 ExCompareExchangeCallBack (
298 IN OUT PEX_CALLBACK CallBack
,
299 IN PEX_CALLBACK_ROUTINE_BLOCK NewBlock
,
300 IN PEX_CALLBACK_ROUTINE_BLOCK OldBlock
303 PEX_CALLBACK_ROUTINE_BLOCK
305 ExReferenceCallBackBlock(
306 IN OUT PEX_CALLBACK CallBack
311 ExDereferenceCallBackBlock(
312 IN OUT PEX_CALLBACK CallBack
,
313 IN PEX_CALLBACK_ROUTINE_BLOCK CallbackRoutineBlock
316 PEX_CALLBACK_FUNCTION
318 ExGetCallBackBlockRoutine(
319 IN PEX_CALLBACK_ROUTINE_BLOCK CallbackRoutineBlock
324 ExGetCallBackBlockContext(
325 IN PEX_CALLBACK_ROUTINE_BLOCK CallbackRoutineBlock
331 IN PEX_CALLBACK_ROUTINE_BLOCK CallbackRoutineBlock
334 /* Rundown Functions ********************************************************/
338 ExfInitializeRundownProtection(
339 OUT PEX_RUNDOWN_REF RunRef
344 ExfReInitializeRundownProtection(
345 OUT PEX_RUNDOWN_REF RunRef
350 ExfAcquireRundownProtection(
351 IN OUT PEX_RUNDOWN_REF RunRef
356 ExfAcquireRundownProtectionEx(
357 IN OUT PEX_RUNDOWN_REF RunRef
,
363 ExfReleaseRundownProtection(
364 IN OUT PEX_RUNDOWN_REF RunRef
369 ExfReleaseRundownProtectionEx(
370 IN OUT PEX_RUNDOWN_REF RunRef
,
377 OUT PEX_RUNDOWN_REF RunRef
382 ExfWaitForRundownProtectionRelease(
383 IN OUT PEX_RUNDOWN_REF RunRef
386 /* HANDLE TABLE FUNCTIONS ***************************************************/
389 (NTAPI
*PEX_SWEEP_HANDLE_CALLBACK
)(
390 PHANDLE_TABLE_ENTRY HandleTableEntry
,
396 (NTAPI
*PEX_DUPLICATE_HANDLE_CALLBACK
)(
397 IN PEPROCESS Process
,
398 IN PHANDLE_TABLE HandleTable
,
399 IN PHANDLE_TABLE_ENTRY HandleTableEntry
,
400 IN PHANDLE_TABLE_ENTRY NewEntry
404 (NTAPI
*PEX_CHANGE_HANDLE_CALLBACK
)(
405 PHANDLE_TABLE_ENTRY HandleTableEntry
,
411 ExpInitializeHandleTables(
418 IN PEPROCESS Process OPTIONAL
423 ExUnlockHandleTableEntry(
424 IN PHANDLE_TABLE HandleTable
,
425 IN PHANDLE_TABLE_ENTRY HandleTableEntry
431 IN PHANDLE_TABLE HandleTable
,
432 IN PHANDLE_TABLE_ENTRY HandleTableEntry
437 ExDestroyHandleTable(
438 IN PHANDLE_TABLE HandleTable
,
439 IN PVOID DestroyHandleProcedure OPTIONAL
445 IN PHANDLE_TABLE HandleTable
,
447 IN PHANDLE_TABLE_ENTRY HandleTableEntry OPTIONAL
452 ExMapHandleToPointer(
453 IN PHANDLE_TABLE HandleTable
,
460 IN PEPROCESS Process
,
461 IN PHANDLE_TABLE HandleTable
,
462 IN PEX_DUPLICATE_HANDLE_CALLBACK DupHandleProcedure
,
469 IN PHANDLE_TABLE HandleTable
,
471 IN PEX_CHANGE_HANDLE_CALLBACK ChangeRoutine
,
478 IN PHANDLE_TABLE HandleTable
,
479 IN PEX_SWEEP_HANDLE_CALLBACK EnumHandleProcedure
,
485 ExpLookupHandleTableEntry(
486 IN PHANDLE_TABLE HandleTable
,
492 ExpLockHandleTableEntry(
493 IN PHANDLE_TABLE HandleTable
,
494 IN PHANDLE_TABLE_ENTRY HandleTableEntry
497 /* PSEH EXCEPTION HANDLING **************************************************/
501 ExSystemExceptionFilter(VOID
);
503 /* CALLBACKS *****************************************************************/
507 ExDoCallBack(IN OUT PEX_CALLBACK Callback
,
512 PEX_CALLBACK_ROUTINE_BLOCK CallbackBlock
;
513 PEX_CALLBACK_FUNCTION Function
;
515 /* Reference the block */
516 CallbackBlock
= ExReferenceCallBackBlock(Callback
);
519 /* Get the function */
520 Function
= ExGetCallBackBlockRoutine(CallbackBlock
);
522 /* Do the callback */
523 Function(Context
, Argument1
, Argument2
);
525 /* Now dereference it */
526 ExDereferenceCallBackBlock(Callback
, CallbackBlock
);
530 /* FAST REFS ******************************************************************/
534 ExGetObjectFastReference(IN EX_FAST_REF FastRef
)
536 /* Return the unbiased pointer */
537 return (PVOID
)(FastRef
.Value
& ~MAX_FAST_REFS
);
542 ExGetCountFastReference(IN EX_FAST_REF FastRef
)
544 /* Return the reference count */
545 return (ULONG
)FastRef
.RefCnt
;
550 ExInitializeFastReference(OUT PEX_FAST_REF FastRef
,
551 IN OPTIONAL PVOID Object
)
554 ASSERT((((ULONG_PTR
)Object
) & MAX_FAST_REFS
) == 0);
556 /* Check if an object is being set */
559 /* Clear the field */
560 FastRef
->Object
= NULL
;
564 /* Otherwise, we assume the object was referenced and is ready */
565 FastRef
->Value
= (ULONG_PTR
)Object
| MAX_FAST_REFS
;
571 ExAcquireFastReference(IN OUT PEX_FAST_REF FastRef
)
573 EX_FAST_REF OldValue
, NewValue
;
575 /* Start reference loop */
578 /* Get the current reference count */
582 /* Increase the reference count */
583 NewValue
.Value
= OldValue
.Value
- 1;
584 NewValue
.Object
= ExpChangePushlock(&FastRef
->Object
,
587 if (NewValue
.Object
!= OldValue
.Object
) continue;
594 /* Return the old value */
600 ExInsertFastReference(IN OUT PEX_FAST_REF FastRef
,
603 EX_FAST_REF OldValue
, NewValue
;
606 ASSERT(!(((ULONG_PTR
)Object
) & MAX_FAST_REFS
));
608 /* Start update loop */
611 /* Get the current reference count */
614 /* Check if the current count is too high or if the pointer changed */
615 if (((OldValue
.RefCnt
+ MAX_FAST_REFS
) > MAX_FAST_REFS
) ||
616 ((OldValue
.Value
&~ MAX_FAST_REFS
) != (ULONG_PTR
)Object
))
622 /* Update the reference count */
623 NewValue
.Value
= OldValue
.Value
+ MAX_FAST_REFS
;
624 NewValue
.Object
= ExpChangePushlock(&FastRef
->Object
,
627 if (NewValue
.Object
!= OldValue
.Object
) continue;
639 ExReleaseFastReference(IN PEX_FAST_REF FastRef
,
642 EX_FAST_REF OldValue
, NewValue
;
645 ASSERT(Object
!= NULL
);
646 ASSERT(!(((ULONG_PTR
)Object
) & MAX_FAST_REFS
));
648 /* Start reference loop */
651 /* Get the current reference count */
654 /* Check if we're full if if the pointer changed */
655 if ((OldValue
.Value
^ (ULONG_PTR
)Object
) >= MAX_FAST_REFS
) return FALSE
;
657 /* Decrease the reference count */
658 NewValue
.Value
= OldValue
.Value
+ 1;
659 NewValue
.Object
= ExpChangePushlock(&FastRef
->Object
,
662 if (NewValue
.Object
!= OldValue
.Object
) continue;
674 ExSwapFastReference(IN PEX_FAST_REF FastRef
,
677 EX_FAST_REF NewValue
, OldValue
;
680 ASSERT((((ULONG_PTR
)Object
) & MAX_FAST_REFS
) == 0);
682 /* Check if an object is being set */
685 /* Clear the field */
686 NewValue
.Object
= NULL
;
690 /* Otherwise, we assume the object was referenced and is ready */
691 NewValue
.Value
= (ULONG_PTR
)Object
| MAX_FAST_REFS
;
694 /* Update the object */
695 OldValue
.Object
= InterlockedExchangePointer(&FastRef
->Object
, NewValue
.Object
);
701 ExCompareSwapFastReference(IN PEX_FAST_REF FastRef
,
705 EX_FAST_REF OldValue
, NewValue
;
707 /* Sanity check and start swap loop */
708 ASSERT(!(((ULONG_PTR
)Object
) & MAX_FAST_REFS
));
711 /* Get the current value */
714 /* Make sure there's enough references to swap */
715 if (!((OldValue
.Value
^ (ULONG_PTR
)OldObject
) <= MAX_FAST_REFS
)) break;
717 /* Check if we have an object to swap */
720 /* Set up the value with maximum fast references */
721 NewValue
.Value
= (ULONG_PTR
)Object
| MAX_FAST_REFS
;
725 /* Write the object address itself (which is empty) */
726 NewValue
.Value
= (ULONG_PTR
)Object
;
729 /* Do the actual compare exchange */
730 NewValue
.Object
= ExpChangePushlock(&FastRef
->Object
,
733 if (NewValue
.Object
!= OldValue
.Object
) continue;
739 /* Return the old value */
743 /* RUNDOWN *******************************************************************/
746 * @name ExfAcquireRundownProtection
749 * The ExfAcquireRundownProtection routine acquires rundown protection for
750 * the specified descriptor.
753 * Pointer to a rundown reference descriptor.
755 * @return TRUE if access to the protected structure was granted, FALSE otherwise.
757 * @remarks This is the internal macro for system use only.In case the rundown
758 * was active, then the slow-path will be called through the exported
764 _ExAcquireRundownProtection(IN PEX_RUNDOWN_REF RunRef
)
766 ULONG_PTR Value
, NewValue
;
768 /* Get the current value and mask the active bit */
769 Value
= RunRef
->Count
&~ EX_RUNDOWN_ACTIVE
;
771 /* Add a reference */
772 NewValue
= Value
+ EX_RUNDOWN_COUNT_INC
;
774 /* Change the value */
775 NewValue
= ExpChangeRundown(RunRef
, NewValue
, Value
);
776 if (NewValue
!= Value
)
778 /* Rundown was active, use long path */
779 return ExfAcquireRundownProtection(RunRef
);
787 * @name ExReleaseRundownProtection
790 * The ExReleaseRundownProtection routine releases rundown protection for
791 * the specified descriptor.
794 * Pointer to a rundown reference descriptor.
796 * @return TRUE if access to the protected structure was granted, FALSE otherwise.
798 * @remarks This is the internal macro for system use only.In case the rundown
799 * was active, then the slow-path will be called through the exported
805 _ExReleaseRundownProtection(IN PEX_RUNDOWN_REF RunRef
)
807 ULONG_PTR Value
, NewValue
;
809 /* Get the current value and mask the active bit */
810 Value
= RunRef
->Count
&~ EX_RUNDOWN_ACTIVE
;
812 /* Remove a reference */
813 NewValue
= Value
- EX_RUNDOWN_COUNT_INC
;
815 /* Change the value */
816 NewValue
= ExpChangeRundown(RunRef
, NewValue
, Value
);
818 /* Check if the rundown was active */
819 if (NewValue
!= Value
)
821 /* Rundown was active, use long path */
822 ExfReleaseRundownProtection(RunRef
);
827 ASSERT((Value
>= EX_RUNDOWN_COUNT_INC
) || (KeNumberProcessors
> 1));
832 * @name ExInitializeRundownProtection
835 * The ExInitializeRundownProtection routine initializes a rundown
836 * protection descriptor.
839 * Pointer to a rundown reference descriptor.
843 * @remarks This is the internal macro for system use only.
848 _ExInitializeRundownProtection(IN PEX_RUNDOWN_REF RunRef
)
850 /* Set the count to zero */
855 * @name ExWaitForRundownProtectionRelease
858 * The ExWaitForRundownProtectionRelease routine waits until the specified
859 * rundown descriptor has been released.
862 * Pointer to a rundown reference descriptor.
866 * @remarks This is the internal macro for system use only. If a wait is actually
867 * necessary, then the slow path is taken through the exported function.
872 _ExWaitForRundownProtectionRelease(IN PEX_RUNDOWN_REF RunRef
)
876 /* Set the active bit */
877 Value
= ExpChangeRundown(RunRef
, EX_RUNDOWN_ACTIVE
, 0);
878 if ((Value
) && (Value
!= EX_RUNDOWN_ACTIVE
))
880 /* If the the rundown wasn't already active, then take the long path */
881 ExfWaitForRundownProtectionRelease(RunRef
);
886 * @name ExRundownCompleted
889 * The ExRundownCompleted routine completes the rundown of the specified
890 * descriptor by setting the active bit.
893 * Pointer to a rundown reference descriptor.
897 * @remarks This is the internal macro for system use only.
902 _ExRundownCompleted(IN PEX_RUNDOWN_REF RunRef
)
905 ASSERT((RunRef
->Count
& EX_RUNDOWN_ACTIVE
) != 0);
907 /* Mark the counter as active */
908 ExpSetRundown(RunRef
, EX_RUNDOWN_ACTIVE
);
911 /* PUSHLOCKS *****************************************************************/
913 /* FIXME: VERIFY THESE! */
918 IN PEX_PUSH_LOCK PushLock
,
925 IN PEX_PUSH_LOCK PushLock
,
926 IN PVOID CurrentWaitBlock
931 ExWaitForUnblockPushLock(
932 IN PEX_PUSH_LOCK PushLock
,
937 * @name _ExInitializePushLock
940 * The _ExInitializePushLock macro initializes a PushLock.
943 * Pointer to the pushlock which is to be initialized.
952 _ExInitializePushLock(OUT PEX_PUSH_LOCK PushLock
)
954 /* Set the value to 0 */
957 #define ExInitializePushLock _ExInitializePushLock
960 * @name ExAcquirePushLockExclusive
963 * The ExAcquirePushLockExclusive macro exclusively acquires a PushLock.
966 * Pointer to the pushlock which is to be acquired.
970 * @remarks The function attempts the quickest route to acquire the lock, which is
971 * to simply set the lock bit.
972 * However, if the pushlock is already shared, the slower path is taken.
974 * Callers of ExAcquirePushLockShared must be running at IRQL <= APC_LEVEL.
975 * This macro should usually be paired up with KeAcquireCriticalRegion.
980 ExAcquirePushLockExclusive(PEX_PUSH_LOCK PushLock
)
982 /* Try acquiring the lock */
983 if (InterlockedBitTestAndSet((PLONG
)PushLock
, EX_PUSH_LOCK_LOCK_V
))
985 /* Someone changed it, use the slow path */
986 ExfAcquirePushLockExclusive(PushLock
);
990 ASSERT(PushLock
->Locked
);
994 * @name ExTryToAcquirePushLockExclusive
997 * The ExAcquirePushLockExclusive macro exclusively acquires a PushLock.
1000 * Pointer to the pushlock which is to be acquired.
1004 * @remarks The function attempts the quickest route to acquire the lock, which is
1005 * to simply set the lock bit.
1006 * However, if the pushlock is already shared, the slower path is taken.
1008 * Callers of ExAcquirePushLockShared must be running at IRQL <= APC_LEVEL.
1009 * This macro should usually be paired up with KeAcquireCriticalRegion.
1014 ExTryToAcquirePushLockExclusive(PEX_PUSH_LOCK PushLock
)
1016 /* Try acquiring the lock */
1017 if (InterlockedBitTestAndSet((PLONG
)PushLock
, EX_PUSH_LOCK_LOCK_V
))
1024 ASSERT (PushLock
->Locked
);
1029 * @name ExAcquirePushLockShared
1032 * The ExAcquirePushLockShared macro acquires a shared PushLock.
1035 * Pointer to the pushlock which is to be acquired.
1039 * @remarks The function attempts the quickest route to acquire the lock, which is
1040 * to simply set the lock bit and set the share count to one.
1041 * However, if the pushlock is already shared, the slower path is taken.
1043 * Callers of ExAcquirePushLockShared must be running at IRQL <= APC_LEVEL.
1044 * This macro should usually be paired up with KeAcquireCriticalRegion.
1049 ExAcquirePushLockShared(PEX_PUSH_LOCK PushLock
)
1051 EX_PUSH_LOCK NewValue
;
1053 /* Try acquiring the lock */
1054 NewValue
.Value
= EX_PUSH_LOCK_LOCK
| EX_PUSH_LOCK_SHARE_INC
;
1055 if (ExpChangePushlock(PushLock
, NewValue
.Ptr
, 0))
1057 /* Someone changed it, use the slow path */
1058 ExfAcquirePushLockShared(PushLock
);
1062 ASSERT(PushLock
->Locked
);
1066 * @name ExConvertPushLockSharedToExclusive
1069 * The ExConvertPushLockSharedToExclusive macro converts an exclusive
1070 * pushlock to a shared pushlock.
1073 * Pointer to the pushlock which is to be converted.
1075 * @return FALSE if conversion failed, TRUE otherwise.
1077 * @remarks The function attempts the quickest route to convert the lock, which is
1078 * to simply set the lock bit and remove any other bits.
1083 ExConvertPushLockSharedToExclusive(IN PEX_PUSH_LOCK PushLock
)
1085 EX_PUSH_LOCK OldValue
;
1087 /* Set the expected old value */
1088 OldValue
.Value
= EX_PUSH_LOCK_LOCK
| EX_PUSH_LOCK_SHARE_INC
;
1090 /* Try converting the lock */
1091 if (ExpChangePushlock(PushLock
, EX_PUSH_LOCK_LOCK
, OldValue
.Value
) !=
1094 /* Conversion failed */
1099 ASSERT(PushLock
->Locked
);
1104 * @name ExWaitOnPushLock
1107 * The ExWaitOnPushLock macro acquires and instantly releases a pushlock.
1110 * Pointer to a pushlock.
1114 * @remarks The function attempts to get any exclusive waiters out of their slow
1115 * path by forcing an instant acquire/release operation.
1117 * Callers of ExWaitOnPushLock must be running at IRQL <= APC_LEVEL.
1122 ExWaitOnPushLock(PEX_PUSH_LOCK PushLock
)
1124 /* Check if we're locked */
1125 if (PushLock
->Locked
)
1127 /* Acquire the lock */
1128 ExfAcquirePushLockExclusive(PushLock
);
1129 ASSERT(PushLock
->Locked
);
1132 ExfReleasePushLockExclusive(PushLock
);
1137 * @name ExReleasePushLockShared
1140 * The ExReleasePushLockShared macro releases a previously acquired PushLock.
1143 * Pointer to a previously acquired pushlock.
1147 * @remarks The function attempts the quickest route to release the lock, which is
1148 * to simply decrease the share count and remove the lock bit.
1149 * However, if the pushlock is being waited on then the long path is taken.
1151 * Callers of ExReleasePushLockShared must be running at IRQL <= APC_LEVEL.
1152 * This macro should usually be paired up with KeLeaveCriticalRegion.
1157 ExReleasePushLockShared(PEX_PUSH_LOCK PushLock
)
1159 EX_PUSH_LOCK OldValue
;
1162 ASSERT(PushLock
->Locked
);
1164 /* Try to clear the pushlock */
1165 OldValue
.Value
= EX_PUSH_LOCK_LOCK
| EX_PUSH_LOCK_SHARE_INC
;
1166 if (ExpChangePushlock(PushLock
, 0, OldValue
.Ptr
) != OldValue
.Ptr
)
1168 /* There are still other people waiting on it */
1169 ExfReleasePushLockShared(PushLock
);
1174 * @name ExReleasePushLockExclusive
1177 * The ExReleasePushLockExclusive macro releases a previously
1178 * exclusively acquired PushLock.
1181 * Pointer to a previously acquired pushlock.
1185 * @remarks The function attempts the quickest route to release the lock, which is
1186 * to simply clear the locked bit.
1187 * However, if the pushlock is being waited on, the slow path is taken
1188 * in an attempt to wake up the lock.
1190 * Callers of ExReleasePushLockExclusive must be running at IRQL <= APC_LEVEL.
1191 * This macro should usually be paired up with KeLeaveCriticalRegion.
1196 ExReleasePushLockExclusive(PEX_PUSH_LOCK PushLock
)
1198 EX_PUSH_LOCK OldValue
;
1201 ASSERT(PushLock
->Locked
);
1203 /* Unlock the pushlock */
1204 OldValue
.Value
= InterlockedExchangeAddSizeT((PSIZE_T
)PushLock
,
1205 -(SSIZE_T
)EX_PUSH_LOCK_LOCK
);
1208 ASSERT(OldValue
.Locked
);
1209 ASSERT(OldValue
.Waiting
|| OldValue
.Shared
== 0);
1211 /* Check if anyone is waiting on it and it's not already waking*/
1212 if ((OldValue
.Waiting
) && !(OldValue
.Waking
))
1215 ExfTryToWakePushLock(PushLock
);
1220 * @name ExReleasePushLock
1223 * The ExReleasePushLock macro releases a previously acquired PushLock.
1226 * Pointer to a previously acquired pushlock.
1230 * @remarks The function attempts the quickest route to release the lock, which is
1231 * to simply clear all the fields and decrease the share count if required.
1232 * However, if the pushlock is being waited on then the long path is taken.
1234 * Callers of ExReleasePushLock must be running at IRQL <= APC_LEVEL.
1235 * This macro should usually be paired up with KeLeaveCriticalRegion.
1240 ExReleasePushLock(PEX_PUSH_LOCK PushLock
)
1242 EX_PUSH_LOCK OldValue
= *PushLock
;
1243 EX_PUSH_LOCK NewValue
;
1246 ASSERT(OldValue
.Locked
);
1248 /* Check if the pushlock is shared */
1249 if (OldValue
.Shared
> 1)
1251 /* Decrease the share count */
1252 NewValue
.Value
= OldValue
.Value
- EX_PUSH_LOCK_SHARE_INC
;
1256 /* Clear the pushlock entirely */
1260 /* Check if nobody is waiting on us and try clearing the lock here */
1261 if ((OldValue
.Waiting
) ||
1262 (ExpChangePushlock(PushLock
, NewValue
.Ptr
, OldValue
.Ptr
) !=
1265 /* We have waiters, use the long path */
1266 ExfReleasePushLock(PushLock
);
1270 /* FAST MUTEX INLINES *********************************************************/
1274 _ExAcquireFastMutexUnsafe(IN PFAST_MUTEX FastMutex
)
1276 PKTHREAD Thread
= KeGetCurrentThread();
1279 ASSERT((KeGetCurrentIrql() == APC_LEVEL
) ||
1280 (Thread
->CombinedApcDisable
!= 0) ||
1281 (Thread
->Teb
== NULL
) ||
1282 (Thread
->Teb
>= (PTEB
)MM_SYSTEM_RANGE_START
));
1283 ASSERT(FastMutex
->Owner
!= Thread
);
1285 /* Decrease the count */
1286 if (InterlockedDecrement(&FastMutex
->Count
))
1288 /* Someone is still holding it, use slow path */
1289 KiAcquireFastMutex(FastMutex
);
1293 FastMutex
->Owner
= Thread
;
1298 _ExReleaseFastMutexUnsafe(IN OUT PFAST_MUTEX FastMutex
)
1300 ASSERT((KeGetCurrentIrql() == APC_LEVEL
) ||
1301 (KeGetCurrentThread()->CombinedApcDisable
!= 0) ||
1302 (KeGetCurrentThread()->Teb
== NULL
) ||
1303 (KeGetCurrentThread()->Teb
>= (PTEB
)MM_SYSTEM_RANGE_START
));
1304 ASSERT(FastMutex
->Owner
== KeGetCurrentThread());
1306 /* Erase the owner */
1307 FastMutex
->Owner
= NULL
;
1309 /* Increase the count */
1310 if (InterlockedIncrement(&FastMutex
->Count
) <= 0)
1312 /* Someone was waiting for it, signal the waiter */
1313 KeSetEventBoostPriority(&FastMutex
->Event
, NULL
);
1319 _ExAcquireFastMutex(IN PFAST_MUTEX FastMutex
)
1322 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
1324 /* Raise IRQL to APC */
1325 KeRaiseIrql(APC_LEVEL
, &OldIrql
);
1327 /* Decrease the count */
1328 if (InterlockedDecrement(&FastMutex
->Count
))
1330 /* Someone is still holding it, use slow path */
1331 KiAcquireFastMutex(FastMutex
);
1334 /* Set the owner and IRQL */
1335 FastMutex
->Owner
= KeGetCurrentThread();
1336 FastMutex
->OldIrql
= OldIrql
;
1341 _ExReleaseFastMutex(IN OUT PFAST_MUTEX FastMutex
)
1344 ASSERT(KeGetCurrentIrql() == APC_LEVEL
);
1346 /* Erase the owner */
1347 FastMutex
->Owner
= NULL
;
1348 OldIrql
= (KIRQL
)FastMutex
->OldIrql
;
1350 /* Increase the count */
1351 if (InterlockedIncrement(&FastMutex
->Count
) <= 0)
1353 /* Someone was waiting for it, signal the waiter */
1354 KeSetEventBoostPriority(&FastMutex
->Event
, NULL
);
1357 /* Lower IRQL back */
1358 KeLowerIrql(OldIrql
);
1363 _ExTryToAcquireFastMutex(IN OUT PFAST_MUTEX FastMutex
)
1366 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
1368 /* Raise to APC_LEVEL */
1369 KeRaiseIrql(APC_LEVEL
, &OldIrql
);
1371 /* Check if we can quickly acquire it */
1372 if (InterlockedCompareExchange(&FastMutex
->Count
, 0, 1) == 1)
1374 /* We have, set us as owners */
1375 FastMutex
->Owner
= KeGetCurrentThread();
1376 FastMutex
->OldIrql
= OldIrql
;
1381 /* Acquire attempt failed */
1382 KeLowerIrql(OldIrql
);
1390 _ExEnterCriticalRegionAndAcquireFastMutexUnsafe(IN OUT PFAST_MUTEX FastMutex
)
1392 /* Enter the Critical Region */
1393 KeEnterCriticalRegion();
1395 /* Acquire the mutex unsafely */
1396 _ExAcquireFastMutexUnsafe(FastMutex
);
1401 _ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(IN OUT PFAST_MUTEX FastMutex
)
1403 /* Release the mutex unsafely */
1404 _ExReleaseFastMutexUnsafe(FastMutex
);
1406 /* Leave the critical region */
1407 KeLeaveCriticalRegion();
1410 /* OTHER FUNCTIONS **********************************************************/
1414 ExTryToAcquireResourceExclusiveLite(
1415 IN PERESOURCE Resource
1419 ExpSetTimeZoneInformation(
1420 IN PRTL_TIME_ZONE_INFORMATION TimeZoneInformation
1425 ExAcquireTimeRefreshLock(
1431 ExReleaseTimeRefreshLock(
1437 ExUpdateSystemTimeFromCmos(
1438 IN BOOLEAN UpdateInterruptTime
,
1439 IN ULONG MaxSepInSeconds
1444 ExAllocateLocallyUniqueId(
1445 OUT LUID
*LocallyUniqueId
1457 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1463 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1466 #define InterlockedDecrementUL(Addend) \
1467 (ULONG)InterlockedDecrement((PLONG)(Addend))
1469 #define InterlockedIncrementUL(Addend) \
1470 (ULONG)InterlockedIncrement((PLONG)(Addend))
1472 #define InterlockedExchangeUL(Target, Value) \
1473 (ULONG)InterlockedExchange((PLONG)(Target), (LONG)(Value))
1475 #define InterlockedExchangeAddUL(Addend, Value) \
1476 (ULONG)InterlockedExchangeAdd((PLONG)(Addend), (LONG)(Value))
1478 #define InterlockedCompareExchangeUL(Destination, Exchange, Comperand) \
1479 (ULONG)InterlockedCompareExchange((PLONG)(Destination), (LONG)(Exchange), (LONG)(Comperand))
1481 #define InterlockedCompareExchangeSizeT(Destination, Exchange, Comperand) \
1482 (SIZE_T)InterlockedCompareExchangePointer((PVOID*)(Destination), (PVOID)(SIZE_T)(Exchange), (PVOID)(SIZE_T)(Comperand))
1484 #define ExfInterlockedCompareExchange64UL(Destination, Exchange, Comperand) \
1485 (ULONGLONG)ExfInterlockedCompareExchange64((PLONGLONG)(Destination), (PLONGLONG)(Exchange), (PLONGLONG)(Comperand))