kernel
Loading...
Searching...
No Matches
ms.h
Go to the documentation of this file.
1#ifndef X86_MATANEL_SYNCHRONIZATION_H
2#define X86_MATANEL_SYNCHRONIZATION_H
3
4/*++
5
6Module Name:
7
8 ms.h
9
10Purpose:
11
12 This module contains the header files & prototypes required for synchronization in a threaded - multiprocessing system.
13
14Author:
15
16 slep (Matanel) 2025.
17
18Revision History:
19
20--*/
21
22#include <stddef.h>
23#include <stdint.h>
24#include <stdbool.h>
25#include "../mtstatus.h"
26#include "annotations.h"
27#include "core.h"
28
29// ------------------ STRUCTURES ------------------
30
36typedef struct _SPINLOCK {
37 volatile uint32_t locked; /* 0 = unlocked, 1 = locked */
39
46
47typedef struct _RUNDOWN_REF {
48 uint64_t Count; // Reference count, bit 0-62 is used for reference counting, bit 63 is used to signify the object is being terminated. (teardown flag)
50
51// Each CPU has its own lock, they do not use the global scheduler lock.
57
61typedef enum _EVENT_TYPE {
62 NotificationEvent, /* wake all waiting threads */
63 SynchronizationEvent /* wake one thread at a time */
65
70typedef struct _EVENT {
71 enum _EVENT_TYPE type; /* Notification vs Synchronization */
72 volatile bool signaled; /* current state */
73 struct _SPINLOCK lock; /* protects signaled + waitingQueue */
74 struct _Queue waitingQueue; /* threads waiting on this event */
76
83typedef struct _MUTEX {
84 uint32_t ownerTid; /* owning thread id (0 if none) */
85 struct _EVENT SynchEvent; /* event used for waking waiters */
86 bool locked; /* fast-check boolean (protected by lock) */
87 struct _SPINLOCK lock; /* protects ownerTid/locked and wait list */
88 struct _ETHREAD* ownerThread; /* pointer to current thread that holds the mutex */
90
91typedef struct _PUSH_LOCK {
92 union {
93 struct {
94 uint64_t Locked : 1;
95 uint64_t Waiting : 1;
96 uint64_t Waking : 1;
97 uint64_t MultipleShared : 1;
98 uint64_t Shared : 60;
99 };
100 uint64_t Value;
101 void* Pointer;
102 };
104
105typedef struct _PUSH_LOCK_WAIT_BLOCK {
106 union {
107 struct _PUSH_LOCK_WAIT_BLOCK* Next; // Links to the next waiter in the stack
108 struct _PUSH_LOCK_WAIT_BLOCK* Last; // Only used if this is the Head node (optimization)
109 };
110
111 EVENT WakeEvent; // The event the thread sleeps on
112 uint32_t Flags; // 1 = Exclusive, 2 = Shared
113 uint32_t ShareCount; // If we interrupt readers, we save their count here
114 bool Signaled; // Optimization to avoid touching the Event if not needed
116
117#define PL_FLAGS_EXCLUSIVE 0x1
118#define PL_FLAGS_SHARED 0x2
119
120// Bit definitions for the PUSH_LOCK->Value
121#define PL_LOCK_BIT 0x1 // Bit 0: Locked Exclusive
122#define PL_WAIT_BIT 0x2 // Bit 1: There are waiters
123#define PL_WAKE_BIT 0x4 // Bit 2: Waking (optimization)
124#define PL_FLAG_MASK 0xF // Bottom 4 bits are flags
125#define PL_SHARE_INC 0x10 // Shared count starts at Bit 4
126
127// ------------------ FUNCTIONS ------------------
128
129//#ifndef MT_UP
130void
132 IN PSPINLOCK lock,
133 IN PIRQL OldIrql
134);
135
136void
138 IN PSPINLOCK lock,
139 IN IRQL OldIrql
140);
141/*
142#else
143#undef MsAcquireSpinlock
144#undef MsReleaseSpinlock
145
146#define MsAcquireSpinlock(x, y) (NULL) // NO-OP
147#define MsReleaseSpinlock(x, y) (NULL) // NO-OP
148#endif
149*/
150
153 IN PMUTEX mut
154);
155
158 IN PMUTEX mut
159);
160
163 IN PMUTEX mut
164);
165
166bool
168 IN PRUNDOWN_REF rundown
169);
170
171void
173 IN PRUNDOWN_REF rundown
174);
175
176void
178 IN PRUNDOWN_REF rundown
179);
180
183 IN PEVENT event
184);
185
188 IN PEVENT event
189);
190
191void
193 IN PSPINLOCK Lock
194);
195
196void
198 IN PSPINLOCK Lock
199);
200
201void
203 IN PUSH_LOCK* Lock
204);
205
206void
208 IN PUSH_LOCK* Lock
209);
210
211void
213 IN PUSH_LOCK* Lock
214);
215
216void
218 IN PUSH_LOCK* Lock
219);
220
222void
225)
226
227{
228 Head->Flink = Head;
229 Head->Blink = Head;
230}
231
232// ->>>> CRASHES IN THESE FUNCTIONS USUALLY BECAUSE INITIALIZELISTHEAD WASNT USED ON THE DOUBLY LINKED LIST !!!!!!!
233
235void
239)
240
241{
243 // The last element is the one before Head (circular list style)
244 Blink = Head->Blink;
245 Entry->Flink = Head; // New entry points forward to Head
246 Entry->Blink = Blink; // New entry points back to old last node
247 Blink->Flink = Entry; // Old last node points forward to new entry
248 Head->Blink = Entry; // Head points back to new entry
249}
250
252void
256)
257{
259
260 // The first element is the one after Head (circular list)
261 First = Head->Flink;
262
263 Entry->Flink = First; // Entry -> next = old first
264 Entry->Blink = Head; // Entry -> prev = head
265
266 First->Blink = Entry; // old first -> prev = entry
267 Head->Flink = Entry; // head -> next = entry
268}
269
274)
275
276{
279
280 Entry = Head->Flink;
281 if (Entry == Head) {
282 // List is empty
283 return NULL;
284 }
285
286 Flink = Entry->Flink;
287 Head->Flink = Flink;
288 Flink->Blink = Head;
289
290 // Clear links
291 Entry->Flink = Entry->Blink = NULL;
292 return Entry;
293}
294
296void
299)
300{
303
304 Flink = Entry->Flink;
305 Blink = Entry->Blink;
306
307 /* Normal (minimal) unlink — identical to Windows' RemoveEntryList */
308 Blink->Flink = Flink;
309 Flink->Blink = Blink;
310
311 // Sanitize the removed entry so it doesn't look valid
312 Entry->Flink = Entry;
313 Entry->Blink = Entry;
314}
315
316
317/* Interlocked push: atomically push Entry onto *ListHeadPtr.
318 ListHeadPtr is PSINGLE_LINKED_LIST* (address of the head pointer).
319 Usage: InterlockedPushEntry(&Descriptor->FreeListHead.Next, &Header->Metadata.FreeListEntry);
320 */
322void
324 PSINGLE_LINKED_LIST* ListHeadPtr, /* &head_ptr */
325 PSINGLE_LINKED_LIST Entry /* entry->Next must be valid memory */
326)
327{
328 PSINGLE_LINKED_LIST oldHead;
329 do {
330 oldHead = __atomic_load_n(ListHeadPtr, __ATOMIC_RELAXED);
331 Entry->Next = oldHead;
332 /* try to replace head with Entry */
333 } while (!__atomic_compare_exchange_n(
334 ListHeadPtr, /* target */
335 &oldHead, /* expected (updated on failure) */
336 Entry, /* desired */
337 /*weak*/ false,
338 __ATOMIC_RELEASE, /* success: release so prior stores are visible */
339 __ATOMIC_RELAXED)); /* failure: relaxed */
340}
341
342/* Interlocked pop: atomically pop and return the old head (or NULL).
343 Returns the popped entry pointer.
344 Usage: InterlockedPopEntry(&Descriptor->FreeListHead.Next);
345 */
349 PSINGLE_LINKED_LIST* ListHeadPtr
350)
351{
352 PSINGLE_LINKED_LIST oldHead;
354
355 do {
356 oldHead = __atomic_load_n(ListHeadPtr, __ATOMIC_ACQUIRE);
357 if (oldHead == NULL)
358 return NULL;
359 next = oldHead->Next;
360 /* try to set head to next */
361 } while (!__atomic_compare_exchange_n(
362 ListHeadPtr,
363 &oldHead,
364 next,
365 /*weak*/ false,
366 __ATOMIC_ACQ_REL, /* success: acquire+release to pair with push */
367 __ATOMIC_RELAXED)); /* failure ordering */
368 return oldHead;
369}
370
371#endif // X86_MATANEL_SYNCHRONIZATION_H
#define FORCEINLINE
Definition annotations.h:23
#define IN
Definition annotations.h:8
struct _DOUBLY_LINKED_LIST * PDOUBLY_LINKED_LIST
enum _IRQL IRQL
enum _IRQL * PIRQL
ETHREAD * PETHREAD
Definition core.h:44
struct _SINGLE_LINKED_LIST * PSINGLE_LINKED_LIST
void MsAcquirePushLockExclusive(IN PUSH_LOCK *Lock)
Definition pushlock.c:80
struct _PUSH_LOCK PUSH_LOCK
struct _EVENT * PEVENT
void MsAcquireSpinlock(IN PSPINLOCK lock, IN PIRQL OldIrql)
Definition spinlock.c:13
struct _RUNDOWN_REF * PRUNDOWN_REF
MTSTATUS MsWaitForEvent(IN PEVENT event)
Definition events.c:117
void MsAcquirePushLockShared(IN PUSH_LOCK *Lock)
Definition pushlock.c:150
FORCEINLINE PDOUBLY_LINKED_LIST RemoveHeadList(PDOUBLY_LINKED_LIST Head)
Definition ms.h:272
struct _RUNDOWN_REF RUNDOWN_REF
struct _MUTEX * PMUTEX
FORCEINLINE void InitializeListHead(PDOUBLY_LINKED_LIST Head)
Definition ms.h:223
struct _PUSH_LOCK_WAIT_BLOCK PUSH_LOCK_WAIT_BLOCK
MTSTATUS MsReleaseMutexObject(IN PMUTEX mut)
Definition mutex.c:135
_EVENT_TYPE
Definition ms.h:61
@ NotificationEvent
Definition ms.h:62
@ SynchronizationEvent
Definition ms.h:63
bool MsAcquireRundownProtection(IN PRUNDOWN_REF rundown)
Definition rundown.c:8
FORCEINLINE PSINGLE_LINKED_LIST InterlockedPopEntry(PSINGLE_LINKED_LIST *ListHeadPtr)
Definition ms.h:348
FORCEINLINE void InterlockedPushEntry(PSINGLE_LINKED_LIST *ListHeadPtr, PSINGLE_LINKED_LIST Entry)
Definition ms.h:323
void MsReleaseRundownProtection(IN PRUNDOWN_REF rundown)
Definition rundown.c:41
MTSTATUS MsInitializeMutexObject(IN PMUTEX mut)
Definition mutex.c:13
enum _EVENT_TYPE EVENT_TYPE
void MsReleasePushLockExclusive(IN PUSH_LOCK *Lock)
Definition pushlock.c:99
MTSTATUS MsSetEvent(IN PEVENT event)
Definition events.c:13
struct _Queue Queue
void MsWaitForRundownProtectionRelease(IN PRUNDOWN_REF rundown)
Definition rundown.c:64
FORCEINLINE void InsertTailList(PDOUBLY_LINKED_LIST Head, PDOUBLY_LINKED_LIST Entry)
Definition ms.h:236
MTSTATUS MsAcquireMutexObject(IN PMUTEX mut)
Definition mutex.c:73
void MsReleaseSpinlockFromDpcLevel(IN PSPINLOCK Lock)
Definition spinlock.c:100
void MsReleaseSpinlock(IN PSPINLOCK lock, IN IRQL OldIrql)
Definition spinlock.c:45
struct _SPINLOCK * PSPINLOCK
void MsAcquireSpinlockAtDpcLevel(IN PSPINLOCK Lock)
Definition spinlock.c:74
struct _MUTEX MUTEX
FORCEINLINE void InsertHeadList(PDOUBLY_LINKED_LIST Head, PDOUBLY_LINKED_LIST Entry)
Definition ms.h:253
void MsReleasePushLockShared(IN PUSH_LOCK *Lock)
Definition pushlock.c:179
struct _PUSH_LOCK_WAIT_BLOCK * PPUSH_LOCK_WAIT_BLOCK
struct _SPINLOCK SPINLOCK
struct _EVENT EVENT
FORCEINLINE void RemoveEntryList(PDOUBLY_LINKED_LIST Entry)
Definition ms.h:297
int32_t MTSTATUS
Definition mtstatus.h:12
struct _DOUBLY_LINKED_LIST * Blink
Definition core.h:30
struct _DOUBLY_LINKED_LIST * Flink
Definition core.h:31
Definition ps.h:182
Definition ms.h:70
enum _EVENT_TYPE type
Definition ms.h:71
struct _Queue waitingQueue
Definition ms.h:74
volatile bool signaled
Definition ms.h:72
struct _SPINLOCK lock
Definition ms.h:73
Definition ms.h:83
bool locked
Definition ms.h:86
uint32_t ownerTid
Definition ms.h:84
struct _ETHREAD * ownerThread
Definition ms.h:88
struct _EVENT SynchEvent
Definition ms.h:85
struct _SPINLOCK lock
Definition ms.h:87
struct _PUSH_LOCK_WAIT_BLOCK * Last
Definition ms.h:108
struct _PUSH_LOCK_WAIT_BLOCK * Next
Definition ms.h:107
uint32_t ShareCount
Definition ms.h:113
uint32_t Flags
Definition ms.h:112
uint64_t Waking
Definition ms.h:96
uint64_t Waiting
Definition ms.h:95
void * Pointer
Definition ms.h:101
uint64_t Locked
Definition ms.h:94
uint64_t Shared
Definition ms.h:98
uint64_t Value
Definition ms.h:100
uint64_t MultipleShared
Definition ms.h:97
Definition ms.h:52
PETHREAD tail
Definition ms.h:54
PETHREAD head
Definition ms.h:53
SPINLOCK lock
Definition ms.h:55
uint64_t Count
Definition ms.h:48
struct _SINGLE_LINKED_LIST * Next
Definition core.h:26
Definition ms.h:36
volatile uint32_t locked
Definition ms.h:37