1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
if NT_INST
else
TITLE "Spin Locks"
;++
;
; Copyright (c) 1989 Microsoft Corporation
;
; Module Name:
;
; spinlock.asm
;
; Abstract:
;
; This module implements the routines for acquiring and releasing
; spin locks.
;
; Author:
;
; Bryan Willman (bryanwi) 13 Dec 89
;
; Environment:
;
; Kernel mode only.
;
; Revision History:
;
; Ken Reneris (kenr) 22-Jan-1991
; Removed KeAcquireSpinLock macros, and made functions
;--
PAGE
.386p
include ks386.inc
include callconv.inc ; calling convention macros
include i386\kimacro.inc
include mac386.inc
EXTRNP KfRaiseIrql,1,IMPORT,FASTCALL
EXTRNP KfLowerIrql,1,IMPORT,FASTCALL
EXTRNP _KeGetCurrentIrql,0,IMPORT
EXTRNP _KeBugCheck,1
_TEXT$00 SEGMENT PARA PUBLIC 'CODE'
ASSUME DS:FLAT, ES:FLAT, SS:NOTHING, FS:NOTHING, GS:NOTHING
PAGE
SUBTTL "Acquire Kernel Spin Lock"
;++
;
; VOID
; KeInializeSpinLock (
; IN PKSPIN_LOCK SpinLock,
;
; Routine Description:
;
; This function initializes a SpinLock
;
; Arguments:
;
; SpinLock (TOS+4) - Supplies a pointer to an kernel spin lock.
;
; Return Value:
;
; None.
;
;--
cPublicProc _KeInitializeSpinLock ,1
cPublicFpo 1,0
mov eax, dword ptr [esp+4]
mov dword ptr [eax], 0
stdRET _KeInitializeSpinLock
stdENDP _KeInitializeSpinLock
PAGE
SUBTTL "Ke Acquire Spin Lock At DPC Level"
;++
;
; VOID
; KefAcquireSpinLockAtDpcLevel (
; IN PKSPIN_LOCK SpinLock
; )
;
; Routine Description:
;
; This function acquires a kernel spin lock.
;
; N.B. This function assumes that the current IRQL is set properly.
; It neither raises nor lowers IRQL.
;
; Arguments:
;
; (ecx) SpinLock - Supplies a pointer to an kernel spin lock.
;
; Return Value:
;
; None.
;
;--
align 16
cPublicFastCall KefAcquireSpinLockAtDpcLevel, 1
cPublicFpo 0, 0
if DBG
push ecx
stdCall _KeGetCurrentIrql
pop ecx
cmp al, DISPATCH_LEVEL
jne short asld50
endif
ifdef NT_UP
fstRET KefAcquireSpinLockAtDpcLevel
else
;
; Attempt to assert the lock
;
asld10: ACQUIRE_SPINLOCK ecx,<short asld20>
fstRET KefAcquireSpinLockAtDpcLevel
;
; Lock is owned, spin till it looks free, then go get it again.
;
align 4
asld20: SPIN_ON_SPINLOCK ecx,<short asld10>
endif
if DBG
asld50: stdCall _KeBugCheck, <IRQL_NOT_GREATER_OR_EQUAL>
endif
fstENDP KefAcquireSpinLockAtDpcLevel
;++
;
; VOID
; KeAcquireSpinLockAtDpcLevel (
; IN PKSPIN_LOCK SpinLock
; )
;
; Routine Description:
;
; Thunk for standard call callers
;
;--
cPublicProc _KeAcquireSpinLockAtDpcLevel, 1
cPublicFpo 1,0
ifndef NT_UP
mov ecx,[esp+4] ; SpinLock
aslc10: ACQUIRE_SPINLOCK ecx,<short aslc20>
stdRET _KeAcquireSpinLockAtDpcLevel
aslc20: SPIN_ON_SPINLOCK ecx,<short aslc10>
endif
stdRET _KeAcquireSpinLockAtDpcLevel
stdENDP _KeAcquireSpinLockAtDpcLevel
PAGE
SUBTTL "Ke Release Spin Lock From Dpc Level"
;++
;
; VOID
; KefReleaseSpinLockFromDpcLevel (
; IN PKSPIN_LOCK SpinLock
; )
;
; Routine Description:
;
; This function releases a kernel spin lock.
;
; N.B. This function assumes that the current IRQL is set properly.
; It neither raises nor lowers IRQL.
;
; Arguments:
;
; (ecx) SpinLock - Supplies a pointer to an executive spin lock.
;
; Return Value:
;
; None.
;
;--
align 16
cPublicFastCall KefReleaseSpinLockFromDpcLevel ,1
cPublicFpo 0,0
ifndef NT_UP
RELEASE_SPINLOCK ecx
endif
fstRET KefReleaseSpinLockFromDpcLevel
fstENDP KefReleaseSpinLockFromDpcLevel
;++
;
; VOID
; KeReleaseSpinLockFromDpcLevel (
; IN PKSPIN_LOCK SpinLock
; )
;
; Routine Description:
;
; Thunk for standard call callers
;
;--
cPublicProc _KeReleaseSpinLockFromDpcLevel, 1
cPublicFpo 1,0
ifndef NT_UP
mov ecx, [esp+4] ; (ecx) = SpinLock
RELEASE_SPINLOCK ecx
endif
stdRET _KeReleaseSpinLockFromDpcLevel
stdENDP _KeReleaseSpinLockFromDpcLevel
PAGE
SUBTTL "Ki Acquire Kernel Spin Lock"
;++
;
; VOID
; FASTCALL
; KiAcquireSpinLock (
; IN PKSPIN_LOCK SpinLock
; )
;
; Routine Description:
;
; This function acquires a kernel spin lock.
;
; N.B. This function assumes that the current IRQL is set properly.
; It neither raises nor lowers IRQL.
;
; Arguments:
;
; (ecx) SpinLock - Supplies a pointer to an kernel spin lock.
;
; Return Value:
;
; None.
;
;--
align 16
cPublicFastCall KiAcquireSpinLock ,1
cPublicFpo 0,0
ifndef NT_UP
;
; Attempt to assert the lock
;
asl10: ACQUIRE_SPINLOCK ecx,<short asl20>
fstRET KiAcquireSpinLock
;
; Lock is owned, spin till it looks free, then go get it again.
;
align 4
asl20: SPIN_ON_SPINLOCK ecx,<short asl10>
else
fstRET KiAcquireSpinLock
endif
fstENDP KiAcquireSpinLock
PAGE
SUBTTL "Ki Release Kernel Spin Lock"
;++
;
; VOID
; FASTCALL
; KiReleaseSpinLock (
; IN PKSPIN_LOCK SpinLock
; )
;
; Routine Description:
;
; This function releases a kernel spin lock.
;
; N.B. This function assumes that the current IRQL is set properly.
; It neither raises nor lowers IRQL.
;
; Arguments:
;
; (ecx) SpinLock - Supplies a pointer to an executive spin lock.
;
; Return Value:
;
; None.
;
;--
align 16
cPublicFastCall KiReleaseSpinLock ,1
cPublicFpo 0,0
ifndef NT_UP
RELEASE_SPINLOCK ecx
endif
fstRET KiReleaseSpinLock
fstENDP KiReleaseSpinLock
PAGE
SUBTTL "Try to acquire Kernel Spin Lock"
;++
;
; BOOLEAN
; KeTryToAcquireSpinLock (
; IN PKSPIN_LOCK SpinLock,
; OUT PKIRQL OldIrql
; )
;
; Routine Description:
;
; This function attempts acquires a kernel spin lock. If the
; spinlock is busy, it is not acquire and FALSE is returned.
;
; Arguments:
;
; SpinLock (TOS+4) - Supplies a pointer to an kernel spin lock.
; OldIrql (TOS+8) = Location to store old irql
;
; Return Value:
; TRUE - Spinlock was acquired & irql was raise
; FALSE - SpinLock was not acquired - irql is unchanged.
;
;--
align dword
cPublicProc _KeTryToAcquireSpinLock ,2
cPublicFpo 2,0
ifdef NT_UP
; UP Version of KeTryToAcquireSpinLock
mov ecx, DISPATCH_LEVEL
fstCall KfRaiseIrql
mov ecx, [esp+8] ; (ecx) -> ptr to OldIrql
mov [ecx], al ; save OldIrql
mov eax, 1 ; Return TRUE
stdRET _KeTryToAcquireSpinLock
else
; MP Version of KeTryToAcquireSpinLock
mov edx,[esp+4] ; (edx) -> spinlock
;
; First check the spinlock without asserting a lock
;
TEST_SPINLOCK edx,<short ttsl10>
;
; Spinlock looks free raise irql & try to acquire it
;
;
; raise to dispatch_level
;
mov ecx, DISPATCH_LEVEL
fstCall KfRaiseIrql
mov edx, [esp+4] ; (edx) -> spinlock
mov ecx, [esp+8] ; (ecx) = Return OldIrql
ACQUIRE_SPINLOCK edx,<short ttsl20>
mov [ecx], al ; save OldIrql
mov eax, 1 ; spinlock was acquired, return TRUE
stdRET _KeTryToAcquireSpinLock
ttsl10:
xor eax, eax ; return FALSE
stdRET _KeTryToAcquireSpinLock
ttsl20:
mov cl, al ; (cl) = OldIrql
fstCall KfLowerIrql ; spinlock was busy, restore irql
xor eax, eax ; return FALSE
stdRET _KeTryToAcquireSpinLock
endif
stdENDP _KeTryToAcquireSpinLock
PAGE
SUBTTL "Ki Try to acquire Kernel Spin Lock"
;++
;
; BOOLEAN
; KiTryToAcquireSpinLock (
; IN PKSPIN_LOCK SpinLock
; )
;
; Routine Description:
;
; This function attempts acquires a kernel spin lock. If the
; spinlock is busy, it is not acquire and FALSE is returned.
;
; Arguments:
;
; SpinLock (TOS+4) - Supplies a pointer to an kernel spin lock.
;
; Return Value:
; TRUE - Spinlock was acquired
; FALSE - SpinLock was not acquired
;
;--
align dword
cPublicProc _KiTryToAcquireSpinLock ,1
cPublicFpo 1,0
ifndef NT_UP
mov eax,[esp+4] ; (eax) -> spinlock
;
; First check the spinlock without asserting a lock
;
TEST_SPINLOCK eax,<short atsl20>
;
; Spinlock looks free try to acquire it
;
ACQUIRE_SPINLOCK eax,<short atsl20>
endif
mov eax, 1 ; spinlock was acquired, return TRUE
stdRET _KiTryToAcquireSpinLock
ifndef NT_UP
atsl20:
xor eax, eax ; return FALSE
stdRET _KiTryToAcquireSpinLock
endif
stdENDP _KiTryToAcquireSpinLock
_TEXT$00 ends
endif ; NT_INST
end
|