My Project
Loading...
Searching...
No Matches
spinlock.c
Go to the documentation of this file.
1/*
2 * PROJECT: MatanelOS Kernel
3 * LICENSE: GPLv3
4 * PURPOSE: Spinlock Types and Function Declarations. (MS)
5 */
6#ifndef X86_SPINLOCK_H
7#define X86_SPINLOCK_H
8
9#include "../../includes/ms.h"
10#include "../../includes/me.h"
11
12void
14 IN PSPINLOCK lock,
15 IN PIRQL OldIrql
16)
17
18/*++
19
20 Routine description : Acquires a spinlock, raises IRQL to DISPATCH_LEVEL.
21
22 Arguments:
23
24 [IN] Pointer to SPINLOCK object.
25 [IN] Pointer to Old IRQL variable.
26
27 Return Values:
28
29 None.
30
31--*/
32
33{
34 if (!lock) return;
35 // spin until we grab the lock.
37 while (__sync_lock_test_and_set(&lock->locked, 1)) {
38 __asm__ volatile("pause" ::: "memory"); /* x86 pause — CPU relax hint */
39 }
40 // Memory barrier to prevent instruction reordering
41 __asm__ volatile("" ::: "memory");
42}
43
44void
46 IN PSPINLOCK lock,
47 IN IRQL OldIrql
48)
49
50/*++
51
52 Routine description : Releases a spinlock, restores previous IRQL.
53
54 Arguments:
55
56 [IN] Pointer to SPINLOCK object.
57 [IN] Old IRQL given by MsAcquireSpinlock
58
59 Return Values:
60
61 None.
62
63--*/
64
65{
66 if (!lock) return;
67 // Memory barrier before release
68 __asm__ volatile("" ::: "memory");
69 __sync_lock_release(&lock->locked);
70 MeLowerIrql(OldIrql);
71}
72
73void
75 IN PSPINLOCK Lock
76)
77
78{
79 // Make sure we are at DPC level or above
81 // Bugcheck.
84 (void*)Lock,
85 (void*)MeGetCurrentIrql(),
86 NULL,
87 NULL
88 );
89 }
90
91 // Acquire the spinlock.
92 while (__sync_lock_test_and_set(&Lock->locked, 1)) {
93 __asm__ volatile("pause" ::: "memory"); /* x86 pause — CPU relax hint */
94 }
95 // Memory barrier to prevent instruction reordering
96 __asm__ volatile("" ::: "memory");
97}
98
99void
101 IN PSPINLOCK Lock
102)
103
104{
105 // Make sure we are at DPC level or above
107 // Bugcheck.
110 (void*)Lock,
111 (void*)MeGetCurrentIrql(),
112 NULL,
113 NULL
114 );
115 }
116
117 // Release the spinlock.
118 __asm__ volatile("" ::: "memory");
119 __sync_lock_release(&Lock->locked);
120}
121
122#endif
#define IN
Definition annotations.h:7
NORETURN void MeBugCheckEx(IN enum _BUGCHECK_CODES BugCheckCode, IN void *BugCheckParameter1, IN void *BugCheckParameter2, IN void *BugCheckParameter3, IN void *BugCheckParameter4)
Definition bugcheck.c:305
@ DISPATCH_LEVEL
Definition core.h:15
enum _IRQL IRQL
enum _IRQL * PIRQL
void MeRaiseIrql(IN IRQL NewIrql, OUT PIRQL OldIrql)
Definition irql.c:57
void MeLowerIrql(IN IRQL NewIrql)
Definition irql.c:97
@ IRQL_NOT_GREATER_OR_EQUAL
Definition me.h:98
FORCEINLINE IRQL MeGetCurrentIrql(void)
Definition me.h:402
struct _SPINLOCK * PSPINLOCK
void MsAcquireSpinlock(IN PSPINLOCK lock, IN PIRQL OldIrql)
Definition spinlock.c:13
void MsReleaseSpinlockFromDpcLevel(IN PSPINLOCK Lock)
Definition spinlock.c:100
void MsReleaseSpinlock(IN PSPINLOCK lock, IN IRQL OldIrql)
Definition spinlock.c:45
void MsAcquireSpinlockAtDpcLevel(IN PSPINLOCK Lock)
Definition spinlock.c:74