]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/i386_lock.s
xnu-1699.22.73.tar.gz
[apple/xnu.git] / osfmk / i386 / i386_lock.s
CommitLineData
1c79356b 1/*
6d2010ae 2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1989 Carnegie-Mellon University
34 * All rights reserved. The CMU software License Agreement specifies
35 * the terms and conditions for use and redistribution.
36 */
37
1c79356b
A
38#include <mach_rt.h>
39#include <platforms.h>
40#include <mach_ldebug.h>
41#include <i386/asm.h>
2d21ac55
A
42#include <i386/eflags.h>
43#include <i386/trap.h>
44#include <config_dtrace.h>
b0d623f7
A
45#include <i386/mp.h>
46
9bccf70c 47#include "assym.s"
1c79356b 48
91447636
A
49#define PAUSE rep; nop
50
6d2010ae 51#include <i386/pal_lock_asm.h>
b0d623f7 52
1c79356b
A
53/*
54 * When performance isn't the only concern, it's
55 * nice to build stack frames...
56 */
91447636 57#define BUILD_STACK_FRAMES (GPROF || \
b0d623f7 58 ((MACH_LDEBUG) && MACH_KDB))
1c79356b
A
59
60#if BUILD_STACK_FRAMES
61
2d21ac55 62/* Stack-frame-relative: */
91447636
A
63#define L_PC B_PC
64#define L_ARG0 B_ARG0
65#define L_ARG1 B_ARG1
66
67#define LEAF_ENTRY(name) \
68 Entry(name); \
69 FRAME; \
70 MCOUNT
71
72#define LEAF_ENTRY2(n1,n2) \
73 Entry(n1); \
74 Entry(n2); \
75 FRAME; \
76 MCOUNT
77
78#define LEAF_RET \
79 EMARF; \
80 ret
1c79356b 81
91447636 82#else /* BUILD_STACK_FRAMES */
1c79356b 83
91447636
A
84/* Stack-pointer-relative: */
85#define L_PC S_PC
86#define L_ARG0 S_ARG0
87#define L_ARG1 S_ARG1
88
89#define LEAF_ENTRY(name) \
90 Entry(name)
91
92#define LEAF_ENTRY2(n1,n2) \
93 Entry(n1); \
94 Entry(n2)
95
96#define LEAF_RET \
97 ret
1c79356b 98
91447636 99#endif /* BUILD_STACK_FRAMES */
1c79356b 100
91447636
A
101
102/* Non-leaf routines always have a stack frame: */
103
104#define NONLEAF_ENTRY(name) \
105 Entry(name); \
106 FRAME; \
107 MCOUNT
108
109#define NONLEAF_ENTRY2(n1,n2) \
110 Entry(n1); \
111 Entry(n2); \
112 FRAME; \
113 MCOUNT
114
115#define NONLEAF_RET \
116 EMARF; \
117 ret
1c79356b
A
118
119
b0d623f7
A
120/* For x86_64, the varargs ABI requires that %al indicate
121 * how many SSE register contain arguments. In our case, 0 */
122#if __i386__
6d2010ae
A
123#define ALIGN_STACK() subl $8, %esp; andl $0xFFFFFFF0, %esp ;
124#define LOAD_STRING_ARG0(label) movl $##label, (%esp) ;
125#define LOAD_ARG1(x) mov x, 4(%esp) ;
126#define LOAD_PTR_ARG1(x) mov x, 4(%esp) ;
b0d623f7
A
127#define CALL_PANIC() call EXT(panic) ;
128#else
6d2010ae 129#define ALIGN_STACK() and $0xFFFFFFFFFFFFFFF0, %rsp ;
b0d623f7 130#define LOAD_STRING_ARG0(label) leaq label(%rip), %rdi ;
6d2010ae
A
131#define LOAD_ARG1(x) mov x, %esi ;
132#define LOAD_PTR_ARG1(x) mov x, %rsi ;
b0d623f7
A
133#define CALL_PANIC() xorb %al,%al ; call EXT(panic) ;
134#endif
1c79356b 135
b0d623f7
A
136#define CHECK_UNLOCK(current, owner) \
137 cmp current, owner ; \
138 je 1f ; \
6d2010ae 139 ALIGN_STACK() ; \
b0d623f7
A
140 LOAD_STRING_ARG0(2f) ; \
141 CALL_PANIC() ; \
142 hlt ; \
143 .data ; \
1442: String "Mutex unlock attempted from non-owner thread"; \
145 .text ; \
1461:
1c79356b
A
147
148#if MACH_LDEBUG
149/*
150 * Routines for general lock debugging.
151 */
1c79356b
A
152
153/*
154 * Checks for expected lock types and calls "panic" on
155 * mismatch. Detects calls to Mutex functions with
156 * type simplelock and vice versa.
157 */
158#define CHECK_MUTEX_TYPE() \
9bccf70c 159 cmpl $ MUTEX_TAG,M_TYPE ; \
1c79356b 160 je 1f ; \
6d2010ae 161 ALIGN_STACK() ; \
b0d623f7
A
162 LOAD_STRING_ARG0(2f) ; \
163 CALL_PANIC() ; \
1c79356b
A
164 hlt ; \
165 .data ; \
1662: String "not a mutex!" ; \
167 .text ; \
1681:
169
1c79356b
A
170/*
171 * If one or more simplelocks are currently held by a thread,
172 * an attempt to acquire a mutex will cause this check to fail
173 * (since a mutex lock may context switch, holding a simplelock
174 * is not a good thing).
175 */
91447636 176#if MACH_RT
1c79356b 177#define CHECK_PREEMPTION_LEVEL() \
b0d623f7
A
178 cmpl $0,%gs:CPU_HIBERNATE ; \
179 jne 1f ; \
91447636 180 cmpl $0,%gs:CPU_PREEMPTION_LEVEL ; \
1c79356b 181 je 1f ; \
6d2010ae
A
182 ALIGN_STACK() ; \
183 movl %gs:CPU_PREEMPTION_LEVEL, %eax ; \
184 LOAD_ARG1(%eax) ; \
b0d623f7
A
185 LOAD_STRING_ARG0(2f) ; \
186 CALL_PANIC() ; \
1c79356b
A
187 hlt ; \
188 .data ; \
b0d623f7 1892: String "preemption_level(%d) != 0!" ; \
1c79356b
A
190 .text ; \
1911:
192#else /* MACH_RT */
193#define CHECK_PREEMPTION_LEVEL()
194#endif /* MACH_RT */
195
b0d623f7
A
196#define CHECK_MYLOCK(current, owner) \
197 cmp current, owner ; \
1c79356b 198 jne 1f ; \
6d2010ae 199 ALIGN_STACK() ; \
b0d623f7
A
200 LOAD_STRING_ARG0(2f) ; \
201 CALL_PANIC() ; \
1c79356b
A
202 hlt ; \
203 .data ; \
b0d623f7 2042: String "Attempt to recursively lock a non-recursive lock"; \
1c79356b
A
205 .text ; \
2061:
207
1c79356b
A
208#else /* MACH_LDEBUG */
209#define CHECK_MUTEX_TYPE()
1c79356b 210#define CHECK_PREEMPTION_LEVEL()
1c79356b 211#define CHECK_MYLOCK(thd)
1c79356b
A
212#endif /* MACH_LDEBUG */
213
2d21ac55 214#define PREEMPTION_DISABLE \
6d2010ae
A
215 incl %gs:CPU_PREEMPTION_LEVEL
216
217#if MACH_LDEBUG || 1
218#define PREEMPTION_LEVEL_DEBUG 1
219#endif
220#if PREEMPTION_LEVEL_DEBUG
2d21ac55
A
221#define PREEMPTION_ENABLE \
222 decl %gs:CPU_PREEMPTION_LEVEL ; \
6d2010ae
A
223 js 17f ; \
224 jnz 19f ; \
225 testl $AST_URGENT,%gs:CPU_PENDING_AST ; \
226 jz 19f ; \
b0d623f7 227 PUSHF ; \
6d2010ae
A
228 testl $EFL_IF, S_PC ; \
229 jz 18f ; \
b0d623f7 230 POPF ; \
2d21ac55 231 int $(T_PREEMPT) ; \
6d2010ae
A
232 jmp 19f ; \
23317: \
234 call _preemption_underflow_panic ; \
23518: \
b0d623f7 236 POPF ; \
6d2010ae
A
23719:
238#else
239#define PREEMPTION_ENABLE \
240 decl %gs:CPU_PREEMPTION_LEVEL ; \
241 jnz 19f ; \
242 testl $AST_URGENT,%gs:CPU_PENDING_AST ; \
243 jz 19f ; \
244 PUSHF ; \
245 testl $EFL_IF, S_PC ; \
246 jz 18f ; \
247 POPF ; \
248 int $(T_PREEMPT) ; \
249 jmp 19f ; \
25018: \
251 POPF ; \
25219:
253#endif
2d21ac55 254
2d21ac55
A
255
256#if CONFIG_DTRACE
b0d623f7
A
257
258 .globl _lockstat_probe
259 .globl _lockstat_probemap
260
261/*
262 * LOCKSTAT_LABEL creates a dtrace symbol which contains
263 * a pointer into the lock code function body. At that
264 * point is a "ret" instruction that can be patched into
265 * a "nop"
266 */
267
268#if defined(__i386__)
269
2d21ac55
A
270#define LOCKSTAT_LABEL(lab) \
271 .data ;\
272 .globl lab ;\
273 lab: ;\
274 .long 9f ;\
275 .text ;\
276 9:
277
2d21ac55
A
278#define LOCKSTAT_RECORD(id, lck) \
279 push %ebp ; \
280 mov %esp,%ebp ; \
281 sub $0x38,%esp /* size of dtrace_probe args */ ; \
282 movl _lockstat_probemap + (id * 4),%eax ; \
283 test %eax,%eax ; \
284 je 9f ; \
285 movl $0,36(%esp) ; \
286 movl $0,40(%esp) ; \
287 movl $0,28(%esp) ; \
288 movl $0,32(%esp) ; \
289 movl $0,20(%esp) ; \
290 movl $0,24(%esp) ; \
291 movl $0,12(%esp) ; \
292 movl $0,16(%esp) ; \
293 movl lck,4(%esp) /* copy lock pointer to arg 1 */ ; \
294 movl $0,8(%esp) ; \
295 movl %eax,(%esp) ; \
296 call *_lockstat_probe ; \
2979: leave
298 /* ret - left to subsequent code, e.g. return values */
299
b0d623f7
A
300#elif defined(__x86_64__)
301#define LOCKSTAT_LABEL(lab) \
302 .data ;\
303 .globl lab ;\
304 lab: ;\
305 .quad 9f ;\
306 .text ;\
307 9:
308
309#define LOCKSTAT_RECORD(id, lck) \
310 push %rbp ; \
311 mov %rsp,%rbp ; \
312 movl _lockstat_probemap + (id * 4)(%rip),%eax ; \
313 test %eax,%eax ; \
314 je 9f ; \
315 mov lck, %rsi ; \
316 mov %rax, %rdi ; \
317 mov $0, %rdx ; \
318 mov $0, %rcx ; \
319 mov $0, %r8 ; \
320 mov $0, %r9 ; \
321 call *_lockstat_probe(%rip) ; \
2d21ac55
A
3229: leave
323 /* ret - left to subsequent code, e.g. return values */
b0d623f7
A
324#else
325#error Unsupported architecture
2d21ac55 326#endif
b0d623f7 327#endif /* CONFIG_DTRACE */
2d21ac55 328
b0d623f7
A
329/*
330 * For most routines, the hw_lock_t pointer is loaded into a
331 * register initially, and then either a byte or register-sized
332 * word is loaded/stored to the pointer
333 */
334
335#if defined(__i386__)
336#define HW_LOCK_REGISTER %edx
337#define LOAD_HW_LOCK_REGISTER mov L_ARG0, HW_LOCK_REGISTER
338#define HW_LOCK_THREAD_REGISTER %ecx
339#define LOAD_HW_LOCK_THREAD_REGISTER mov %gs:CPU_ACTIVE_THREAD, HW_LOCK_THREAD_REGISTER
340#define HW_LOCK_MOV_WORD movl
341#define HW_LOCK_EXAM_REGISTER %eax
342#elif defined(__x86_64__)
343#define HW_LOCK_REGISTER %rdi
344#define LOAD_HW_LOCK_REGISTER
345#define HW_LOCK_THREAD_REGISTER %rcx
346#define LOAD_HW_LOCK_THREAD_REGISTER mov %gs:CPU_ACTIVE_THREAD, HW_LOCK_THREAD_REGISTER
347#define HW_LOCK_MOV_WORD movq
348#define HW_LOCK_EXAM_REGISTER %rax
349#else
350#error Unsupported architecture
351#endif
2d21ac55 352
1c79356b
A
353/*
354 * void hw_lock_init(hw_lock_t)
355 *
356 * Initialize a hardware lock.
357 */
91447636 358LEAF_ENTRY(hw_lock_init)
b0d623f7
A
359 LOAD_HW_LOCK_REGISTER /* fetch lock pointer */
360 HW_LOCK_MOV_WORD $0, (HW_LOCK_REGISTER) /* clear the lock */
2d21ac55
A
361 LEAF_RET
362
363
364/*
365 * void hw_lock_byte_init(uint8_t *)
366 *
367 * Initialize a hardware byte lock.
368 */
369LEAF_ENTRY(hw_lock_byte_init)
b0d623f7
A
370 LOAD_HW_LOCK_REGISTER /* fetch lock pointer */
371 movb $0, (HW_LOCK_REGISTER) /* clear the lock */
91447636 372 LEAF_RET
1c79356b
A
373
374/*
375 * void hw_lock_lock(hw_lock_t)
376 *
377 * Acquire lock, spinning until it becomes available.
378 * MACH_RT: also return with preemption disabled.
379 */
91447636 380LEAF_ENTRY(hw_lock_lock)
b0d623f7
A
381 LOAD_HW_LOCK_REGISTER /* fetch lock pointer */
382 LOAD_HW_LOCK_THREAD_REGISTER /* get thread pointer */
383
2d21ac55 384 PREEMPTION_DISABLE
0c530ab8 3851:
b0d623f7
A
386 mov (HW_LOCK_REGISTER), HW_LOCK_EXAM_REGISTER
387 test HW_LOCK_EXAM_REGISTER,HW_LOCK_EXAM_REGISTER /* lock locked? */
91447636 388 jne 3f /* branch if so */
b0d623f7 389 lock; cmpxchg HW_LOCK_THREAD_REGISTER,(HW_LOCK_REGISTER) /* try to acquire the HW lock */
1c79356b 390 jne 3f
9bccf70c 391 movl $1,%eax /* In case this was a timeout call */
91447636 392 LEAF_RET /* if yes, then nothing left to do */
0c530ab8 3933:
91447636
A
394 PAUSE /* pause for hyper-threading */
395 jmp 1b /* try again */
1c79356b 396
2d21ac55
A
397/*
398 * void hw_lock_byte_lock(uint8_t *lock_byte)
399 *
400 * Acquire byte sized lock operand, spinning until it becomes available.
401 * MACH_RT: also return with preemption disabled.
402 */
403
404LEAF_ENTRY(hw_lock_byte_lock)
b0d623f7 405 LOAD_HW_LOCK_REGISTER /* Load lock pointer */
2d21ac55
A
406 PREEMPTION_DISABLE
407 movl $1, %ecx /* Set lock value */
4081:
b0d623f7 409 movb (HW_LOCK_REGISTER), %al /* Load byte at address */
2d21ac55
A
410 testb %al,%al /* lock locked? */
411 jne 3f /* branch if so */
b0d623f7 412 lock; cmpxchg %cl,(HW_LOCK_REGISTER) /* attempt atomic compare exchange */
2d21ac55
A
413 jne 3f
414 LEAF_RET /* if yes, then nothing left to do */
4153:
416 PAUSE /* pause for hyper-threading */
417 jmp 1b /* try again */
418
55e303ae
A
419/*
420 * unsigned int hw_lock_to(hw_lock_t, unsigned int)
421 *
422 * Acquire lock, spinning until it becomes available or timeout.
423 * MACH_RT: also return with preemption disabled.
424 */
91447636 425LEAF_ENTRY(hw_lock_to)
55e303ae 4261:
b0d623f7
A
427 LOAD_HW_LOCK_REGISTER /* fetch lock pointer */
428 LOAD_HW_LOCK_THREAD_REGISTER
429
55e303ae
A
430 /*
431 * Attempt to grab the lock immediately
432 * - fastpath without timeout nonsense.
433 */
2d21ac55 434 PREEMPTION_DISABLE
b0d623f7
A
435
436 mov (HW_LOCK_REGISTER), HW_LOCK_EXAM_REGISTER
437 test HW_LOCK_EXAM_REGISTER,HW_LOCK_EXAM_REGISTER /* lock locked? */
91447636 438 jne 2f /* branch if so */
b0d623f7 439 lock; cmpxchg HW_LOCK_THREAD_REGISTER,(HW_LOCK_REGISTER) /* try to acquire the HW lock */
91447636 440 jne 2f /* branch on failure */
55e303ae 441 movl $1,%eax
91447636 442 LEAF_RET
55e303ae
A
443
4442:
445#define INNER_LOOP_COUNT 1000
446 /*
447 * Failed to get the lock so set the timeout
448 * and then spin re-checking the lock but pausing
449 * every so many (INNER_LOOP_COUNT) spins to check for timeout.
450 */
b0d623f7 451#if __i386__
55e303ae
A
452 movl L_ARG1,%ecx /* fetch timeout */
453 push %edi
454 push %ebx
455 mov %edx,%edi
456
c910b4d9 457 lfence
55e303ae 458 rdtsc /* read cyclecount into %edx:%eax */
593a1d5f 459 lfence
55e303ae
A
460 addl %ecx,%eax /* fetch and timeout */
461 adcl $0,%edx /* add carry */
462 mov %edx,%ecx
463 mov %eax,%ebx /* %ecx:%ebx is the timeout expiry */
b0d623f7
A
464 mov %edi, %edx /* load lock back into %edx */
465#else
466 push %r9
467 lfence
468 rdtsc /* read cyclecount into %edx:%eax */
469 lfence
470 shlq $32, %rdx
471 orq %rdx, %rax /* load 64-bit quantity into %rax */
472 addq %rax, %rsi /* %rsi is the timeout expiry */
473#endif
474
55e303ae
A
4754:
476 /*
477 * The inner-loop spin to look for the lock being freed.
478 */
b0d623f7
A
479#if __i386__
480 mov $(INNER_LOOP_COUNT),%edi
481#else
482 mov $(INNER_LOOP_COUNT),%r9
483#endif
55e303ae 4845:
91447636 485 PAUSE /* pause for hyper-threading */
b0d623f7
A
486 mov (HW_LOCK_REGISTER),HW_LOCK_EXAM_REGISTER /* spin checking lock value in cache */
487 test HW_LOCK_EXAM_REGISTER,HW_LOCK_EXAM_REGISTER
55e303ae 488 je 6f /* zero => unlocked, try to grab it */
b0d623f7
A
489#if __i386__
490 decl %edi /* decrement inner loop count */
491#else
492 decq %r9 /* decrement inner loop count */
493#endif
55e303ae 494 jnz 5b /* time to check for timeout? */
b0d623f7 495
55e303ae
A
496 /*
497 * Here after spinning INNER_LOOP_COUNT times, check for timeout
498 */
b0d623f7
A
499#if __i386__
500 mov %edx,%edi /* Save %edx */
c910b4d9 501 lfence
55e303ae 502 rdtsc /* cyclecount into %edx:%eax */
593a1d5f 503 lfence
b0d623f7
A
504 xchg %edx,%edi /* cyclecount into %edi:%eax */
505 cmpl %ecx,%edi /* compare high-order 32-bits */
55e303ae
A
506 jb 4b /* continue spinning if less, or */
507 cmpl %ebx,%eax /* compare low-order 32-bits */
0c530ab8 508 jb 4b /* continue if less, else bail */
55e303ae
A
509 xor %eax,%eax /* with 0 return value */
510 pop %ebx
511 pop %edi
b0d623f7
A
512#else
513 lfence
514 rdtsc /* cyclecount into %edx:%eax */
515 lfence
516 shlq $32, %rdx
517 orq %rdx, %rax /* load 64-bit quantity into %rax */
518 cmpq %rsi, %rax /* compare to timeout */
519 jb 4b /* continue spinning if less, or */
520 xor %rax,%rax /* with 0 return value */
521 pop %r9
522#endif
91447636 523 LEAF_RET
55e303ae
A
524
5256:
526 /*
527 * Here to try to grab the lock that now appears to be free
528 * after contention.
529 */
b0d623f7
A
530 LOAD_HW_LOCK_THREAD_REGISTER
531 lock; cmpxchg HW_LOCK_THREAD_REGISTER,(HW_LOCK_REGISTER) /* try to acquire the HW lock */
0c530ab8 532 jne 4b /* no - spin again */
55e303ae 533 movl $1,%eax /* yes */
b0d623f7 534#if __i386__
55e303ae
A
535 pop %ebx
536 pop %edi
b0d623f7
A
537#else
538 pop %r9
539#endif
91447636 540 LEAF_RET
55e303ae 541
1c79356b
A
542/*
543 * void hw_lock_unlock(hw_lock_t)
544 *
545 * Unconditionally release lock.
546 * MACH_RT: release preemption level.
547 */
91447636 548LEAF_ENTRY(hw_lock_unlock)
b0d623f7
A
549 LOAD_HW_LOCK_REGISTER /* fetch lock pointer */
550 HW_LOCK_MOV_WORD $0, (HW_LOCK_REGISTER) /* clear the lock */
2d21ac55 551 PREEMPTION_ENABLE
91447636 552 LEAF_RET
b0d623f7 553
2d21ac55
A
554/*
555 * void hw_lock_byte_unlock(uint8_t *lock_byte)
556 *
557 * Unconditionally release byte sized lock operand.
558 * MACH_RT: release preemption level.
559 */
1c79356b 560
2d21ac55 561LEAF_ENTRY(hw_lock_byte_unlock)
b0d623f7
A
562 LOAD_HW_LOCK_REGISTER /* Load lock pointer */
563 movb $0, (HW_LOCK_REGISTER) /* Clear the lock byte */
2d21ac55 564 PREEMPTION_ENABLE
0c530ab8
A
565 LEAF_RET
566
1c79356b
A
567/*
568 * unsigned int hw_lock_try(hw_lock_t)
569 * MACH_RT: returns with preemption disabled on success.
570 */
91447636 571LEAF_ENTRY(hw_lock_try)
b0d623f7
A
572 LOAD_HW_LOCK_REGISTER /* fetch lock pointer */
573 LOAD_HW_LOCK_THREAD_REGISTER
2d21ac55 574 PREEMPTION_DISABLE
b0d623f7
A
575
576 mov (HW_LOCK_REGISTER),HW_LOCK_EXAM_REGISTER
577 test HW_LOCK_EXAM_REGISTER,HW_LOCK_EXAM_REGISTER
91447636 578 jne 1f
b0d623f7 579 lock; cmpxchg HW_LOCK_THREAD_REGISTER,(HW_LOCK_REGISTER) /* try to acquire the HW lock */
91447636 580 jne 1f
b0d623f7 581
1c79356b 582 movl $1,%eax /* success */
91447636 583 LEAF_RET
1c79356b 584
0c530ab8 5851:
2d21ac55 586 PREEMPTION_ENABLE /* failure: release preemption... */
1c79356b 587 xorl %eax,%eax /* ...and return failure */
91447636 588 LEAF_RET
1c79356b
A
589
590/*
591 * unsigned int hw_lock_held(hw_lock_t)
592 * MACH_RT: doesn't change preemption state.
593 * N.B. Racy, of course.
594 */
91447636 595LEAF_ENTRY(hw_lock_held)
b0d623f7
A
596 LOAD_HW_LOCK_REGISTER /* fetch lock pointer */
597 mov (HW_LOCK_REGISTER),HW_LOCK_EXAM_REGISTER /* check lock value */
598 test HW_LOCK_EXAM_REGISTER,HW_LOCK_EXAM_REGISTER
55e303ae 599 movl $1,%ecx
91447636
A
600 cmovne %ecx,%eax /* 0 => unlocked, 1 => locked */
601 LEAF_RET
1c79356b 602
1c79356b 603
2d21ac55
A
604/*
605 * Reader-writer lock fastpaths. These currently exist for the
b0d623f7
A
606 * shared lock acquire, the exclusive lock acquire, the shared to
607 * exclusive upgrade and the release paths (where they reduce overhead
608 * considerably) -- these are by far the most frequently used routines
609 *
610 * The following should reflect the layout of the bitfield embedded within
611 * the lck_rw_t structure (see i386/locks.h).
2d21ac55 612 */
b0d623f7
A
613#define LCK_RW_INTERLOCK (0x1 << 16)
614
615#define LCK_RW_PRIV_EXCL (0x1 << 24)
616#define LCK_RW_WANT_UPGRADE (0x2 << 24)
617#define LCK_RW_WANT_WRITE (0x4 << 24)
618#define LCK_R_WAITING (0x8 << 24)
619#define LCK_W_WAITING (0x10 << 24)
620
621#define LCK_RW_SHARED_MASK (0xffff)
2d21ac55
A
622
623/*
b0d623f7
A
624 * For most routines, the lck_rw_t pointer is loaded into a
625 * register initially, and the flags bitfield loaded into another
626 * register and examined
2d21ac55 627 */
b0d623f7
A
628
629#if defined(__i386__)
630#define LCK_RW_REGISTER %edx
631#define LOAD_LCK_RW_REGISTER mov S_ARG0, LCK_RW_REGISTER
632#define LCK_RW_FLAGS_REGISTER %eax
633#define LOAD_LCK_RW_FLAGS_REGISTER mov (LCK_RW_REGISTER), LCK_RW_FLAGS_REGISTER
634#elif defined(__x86_64__)
635#define LCK_RW_REGISTER %rdi
636#define LOAD_LCK_RW_REGISTER
637#define LCK_RW_FLAGS_REGISTER %eax
638#define LOAD_LCK_RW_FLAGS_REGISTER mov (LCK_RW_REGISTER), LCK_RW_FLAGS_REGISTER
639#else
640#error Unsupported architecture
641#endif
642
643#define RW_LOCK_SHARED_MASK (LCK_RW_INTERLOCK | LCK_RW_WANT_UPGRADE | LCK_RW_WANT_WRITE)
2d21ac55 644/*
b0d623f7 645 * void lck_rw_lock_shared(lck_rw_t *)
2d21ac55
A
646 *
647 */
2d21ac55 648Entry(lck_rw_lock_shared)
b0d623f7 649 LOAD_LCK_RW_REGISTER
2d21ac55 6501:
b0d623f7 651 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield and interlock */
2d21ac55
A
652 testl $(RW_LOCK_SHARED_MASK), %eax /* Eligible for fastpath? */
653 jne 3f
b0d623f7
A
654
655 movl %eax, %ecx /* original value in %eax for cmpxchgl */
2d21ac55
A
656 incl %ecx /* Increment reader refcount */
657 lock
b0d623f7 658 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
2d21ac55
A
659 jne 2f
660
661#if CONFIG_DTRACE
662 /*
663 * Dtrace lockstat event: LS_LCK_RW_LOCK_SHARED_ACQUIRE
664 * Implemented by swapping between return and no-op instructions.
665 * See bsd/dev/dtrace/lockstat.c.
666 */
667 LOCKSTAT_LABEL(_lck_rw_lock_shared_lockstat_patch_point)
668 ret
6d2010ae
A
669 /*
670 Fall thru when patched, counting on lock pointer in LCK_RW_REGISTER
671 */
672 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_SHARED_ACQUIRE, LCK_RW_REGISTER)
2d21ac55
A
673#endif
674 ret
2d21ac55
A
6752:
676 PAUSE
677 jmp 1b
6783:
679 jmp EXT(lck_rw_lock_shared_gen)
680
681
b0d623f7
A
682
683#define RW_TRY_LOCK_SHARED_MASK (LCK_RW_WANT_UPGRADE | LCK_RW_WANT_WRITE)
2d21ac55 684/*
b0d623f7 685 * void lck_rw_try_lock_shared(lck_rw_t *)
2d21ac55
A
686 *
687 */
b0d623f7
A
688Entry(lck_rw_try_lock_shared)
689 LOAD_LCK_RW_REGISTER
2d21ac55 6901:
b0d623f7
A
691 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield and interlock */
692 testl $(LCK_RW_INTERLOCK), %eax
693 jne 2f
694 testl $(RW_TRY_LOCK_SHARED_MASK), %eax
695 jne 3f /* lock is busy */
696
697 movl %eax, %ecx /* original value in %eax for cmpxchgl */
698 incl %ecx /* Increment reader refcount */
2d21ac55 699 lock
b0d623f7 700 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
2d21ac55 701 jne 2f
b0d623f7 702
2d21ac55 703#if CONFIG_DTRACE
b0d623f7 704 movl $1, %eax
2d21ac55 705 /*
b0d623f7
A
706 * Dtrace lockstat event: LS_LCK_RW_TRY_LOCK_SHARED_ACQUIRE
707 * Implemented by swapping between return and no-op instructions.
708 * See bsd/dev/dtrace/lockstat.c.
2d21ac55 709 */
b0d623f7
A
710 LOCKSTAT_LABEL(_lck_rw_try_lock_shared_lockstat_patch_point)
711 ret
712 /* Fall thru when patched, counting on lock pointer in LCK_RW_REGISTER */
713 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_SHARED_ACQUIRE, LCK_RW_REGISTER)
2d21ac55 714#endif
b0d623f7 715 movl $1, %eax /* return TRUE */
2d21ac55 716 ret
2d21ac55
A
7172:
718 PAUSE
719 jmp 1b
7203:
b0d623f7
A
721 xorl %eax, %eax
722 ret
1c79356b 723
2d21ac55 724
b0d623f7
A
725#define RW_LOCK_EXCLUSIVE_HELD (LCK_RW_WANT_WRITE | LCK_RW_WANT_UPGRADE)
726/*
727 * int lck_rw_grab_shared(lck_rw_t *)
728 *
729 */
730Entry(lck_rw_grab_shared)
731 LOAD_LCK_RW_REGISTER
7321:
733 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield and interlock */
734 testl $(LCK_RW_INTERLOCK), %eax
735 jne 5f
736 testl $(RW_LOCK_EXCLUSIVE_HELD), %eax
737 jne 3f
7382:
739 movl %eax, %ecx /* original value in %eax for cmpxchgl */
740 incl %ecx /* Increment reader refcount */
741 lock
742 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
743 jne 4f
744
745 movl $1, %eax /* return success */
2d21ac55 746 ret
b0d623f7
A
7473:
748 testl $(LCK_RW_SHARED_MASK), %eax
749 je 4f
750 testl $(LCK_RW_PRIV_EXCL), %eax
751 je 2b
7524:
753 xorl %eax, %eax /* return failure */
2d21ac55 754 ret
b0d623f7 7555:
2d21ac55 756 PAUSE
b0d623f7 757 jmp 1b
0c530ab8 758
91447636 759
b0d623f7
A
760
761#define RW_LOCK_EXCLUSIVE_MASK (LCK_RW_SHARED_MASK | LCK_RW_INTERLOCK | \
762 LCK_RW_WANT_UPGRADE | LCK_RW_WANT_WRITE)
763/*
764 * void lck_rw_lock_exclusive(lck_rw_t*)
765 *
766 */
767Entry(lck_rw_lock_exclusive)
768 LOAD_LCK_RW_REGISTER
7691:
770 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield, interlock and shared count */
771 testl $(RW_LOCK_EXCLUSIVE_MASK), %eax /* Eligible for fastpath? */
772 jne 3f /* no, go slow */
1c79356b 773
b0d623f7
A
774 movl %eax, %ecx /* original value in %eax for cmpxchgl */
775 orl $(LCK_RW_WANT_WRITE), %ecx
776 lock
777 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
778 jne 2f
0c530ab8 779
2d21ac55 780#if CONFIG_DTRACE
b0d623f7
A
781 /*
782 * Dtrace lockstat event: LS_LCK_RW_LOCK_EXCL_ACQUIRE
783 * Implemented by swapping between return and no-op instructions.
784 * See bsd/dev/dtrace/lockstat.c.
785 */
786 LOCKSTAT_LABEL(_lck_rw_lock_exclusive_lockstat_patch_point)
2d21ac55 787 ret
b0d623f7
A
788 /* Fall thru when patched, counting on lock pointer in LCK_RW_REGISTER */
789 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_SHARED_ACQUIRE, LCK_RW_REGISTER)
2d21ac55
A
790#endif
791 ret
b0d623f7 7922:
2d21ac55 793 PAUSE
b0d623f7
A
794 jmp 1b
7953:
796 jmp EXT(lck_rw_lock_exclusive_gen)
0c530ab8 797
2d21ac55
A
798
799
b0d623f7
A
800#define RW_TRY_LOCK_EXCLUSIVE_MASK (LCK_RW_SHARED_MASK | LCK_RW_WANT_UPGRADE | LCK_RW_WANT_WRITE)
801/*
802 * void lck_rw_try_lock_exclusive(lck_rw_t *)
803 *
804 * Tries to get a write lock.
805 *
806 * Returns FALSE if the lock is not held on return.
807 */
808Entry(lck_rw_try_lock_exclusive)
809 LOAD_LCK_RW_REGISTER
8101:
811 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield, interlock and shared count */
812 testl $(LCK_RW_INTERLOCK), %eax
813 jne 2f
814 testl $(RW_TRY_LOCK_EXCLUSIVE_MASK), %eax
815 jne 3f /* can't get it */
2d21ac55 816
b0d623f7
A
817 movl %eax, %ecx /* original value in %eax for cmpxchgl */
818 orl $(LCK_RW_WANT_WRITE), %ecx
819 lock
820 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
821 jne 2f
2d21ac55 822
2d21ac55 823#if CONFIG_DTRACE
b0d623f7
A
824 movl $1, %eax
825 /*
826 * Dtrace lockstat event: LS_LCK_RW_TRY_LOCK_EXCL_ACQUIRE
827 * Implemented by swapping between return and no-op instructions.
828 * See bsd/dev/dtrace/lockstat.c.
829 */
830 LOCKSTAT_LABEL(_lck_rw_try_lock_exclusive_lockstat_patch_point)
2d21ac55 831 ret
b0d623f7
A
832 /* Fall thru when patched, counting on lock pointer in LCK_RW_REGISTER */
833 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_SHARED_ACQUIRE, LCK_RW_REGISTER)
2d21ac55 834#endif
b0d623f7 835 movl $1, %eax /* return TRUE */
2d21ac55 836 ret
b0d623f7 8372:
2d21ac55 838 PAUSE
b0d623f7
A
839 jmp 1b
8403:
841 xorl %eax, %eax /* return FALSE */
842 ret
2d21ac55 843
0c530ab8 844
1c79356b 845
b0d623f7
A
846/*
847 * void lck_rw_lock_shared_to_exclusive(lck_rw_t*)
848 *
849 * fastpath can be taken if
850 * the current rw_shared_count == 1
851 * AND the interlock is clear
852 * AND RW_WANT_UPGRADE is not set
853 *
854 * note that RW_WANT_WRITE could be set, but will not
855 * be indicative of an exclusive hold since we have
856 * a read count on the lock that we have not yet released
857 * we can blow by that state since the lck_rw_lock_exclusive
858 * function will block until rw_shared_count == 0 and
859 * RW_WANT_UPGRADE is clear... it does this check behind
860 * the interlock which we are also checking for
861 *
862 * to make the transition we must be able to atomically
863 * set RW_WANT_UPGRADE and get rid of the read count we hold
864 */
865Entry(lck_rw_lock_shared_to_exclusive)
866 LOAD_LCK_RW_REGISTER
8671:
868 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield, interlock and shared count */
869 testl $(LCK_RW_INTERLOCK), %eax
870 jne 7f
871 testl $(LCK_RW_WANT_UPGRADE), %eax
872 jne 2f
1c79356b 873
b0d623f7
A
874 movl %eax, %ecx /* original value in %eax for cmpxchgl */
875 orl $(LCK_RW_WANT_UPGRADE), %ecx /* ask for WANT_UPGRADE */
876 decl %ecx /* and shed our read count */
877 lock
878 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
879 jne 7f
880 /* we now own the WANT_UPGRADE */
881 testl $(LCK_RW_SHARED_MASK), %ecx /* check to see if all of the readers are drained */
882 jne 8f /* if not, we need to go wait */
1c79356b 883
2d21ac55 884#if CONFIG_DTRACE
b0d623f7
A
885 movl $1, %eax
886 /*
887 * Dtrace lockstat event: LS_LCK_RW_LOCK_SHARED_TO_EXCL_UPGRADE
888 * Implemented by swapping between return and no-op instructions.
889 * See bsd/dev/dtrace/lockstat.c.
890 */
891 LOCKSTAT_LABEL(_lck_rw_lock_shared_to_exclusive_lockstat_patch_point)
2d21ac55 892 ret
b0d623f7
A
893 /* Fall thru when patched, counting on lock pointer in LCK_RW_REGISTER */
894 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_SHARED_ACQUIRE, LCK_RW_REGISTER)
2d21ac55 895#endif
b0d623f7 896 movl $1, %eax /* return success */
2d21ac55 897 ret
b0d623f7
A
898
8992: /* someone else already holds WANT_UPGRADE */
900 movl %eax, %ecx /* original value in %eax for cmpxchgl */
901 decl %ecx /* shed our read count */
902 testl $(LCK_RW_SHARED_MASK), %ecx
903 jne 3f /* we were the last reader */
904 andl $(~LCK_W_WAITING), %ecx /* so clear the wait indicator */
9053:
906 lock
907 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
908 jne 7f
909
910#if __i386__
911 pushl %eax /* go check to see if we need to */
912 push %edx /* wakeup anyone */
913 call EXT(lck_rw_lock_shared_to_exclusive_failure)
914 addl $8, %esp
915#else
916 mov %eax, %esi /* put old flags as second arg */
917 /* lock is alread in %rdi */
918 call EXT(lck_rw_lock_shared_to_exclusive_failure)
919#endif
920 ret /* and pass the failure return along */
9217:
922 PAUSE
923 jmp 1b
9248:
925 jmp EXT(lck_rw_lock_shared_to_exclusive_success)
1c79356b 926
0c530ab8 927
b0d623f7
A
928
929 .cstring
930rwl_release_error_str:
931 .asciz "Releasing non-exclusive RW lock without a reader refcount!"
932 .text
933
934/*
935 * lck_rw_type_t lck_rw_done(lck_rw_t *)
936 *
937 */
938Entry(lck_rw_done)
939 LOAD_LCK_RW_REGISTER
9401:
941 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield, interlock and reader count */
942 testl $(LCK_RW_INTERLOCK), %eax
943 jne 7f /* wait for interlock to clear */
944
945 movl %eax, %ecx /* keep original value in %eax for cmpxchgl */
946 testl $(LCK_RW_SHARED_MASK), %ecx /* if reader count == 0, must be exclusive lock */
947 je 2f
948 decl %ecx /* Decrement reader count */
949 testl $(LCK_RW_SHARED_MASK), %ecx /* if reader count has now gone to 0, check for waiters */
950 je 4f
951 jmp 6f
9522:
953 testl $(LCK_RW_WANT_UPGRADE), %ecx
954 je 3f
955 andl $(~LCK_RW_WANT_UPGRADE), %ecx
956 jmp 4f
9573:
958 testl $(LCK_RW_WANT_WRITE), %ecx
959 je 8f /* lock is not 'owned', go panic */
960 andl $(~LCK_RW_WANT_WRITE), %ecx
9614:
0c530ab8 962 /*
b0d623f7
A
963 * test the original values to match what
964 * lck_rw_done_gen is going to do to determine
965 * which wakeups need to happen...
966 *
967 * if !(fake_lck->lck_rw_priv_excl && fake_lck->lck_w_waiting)
968 */
969 testl $(LCK_W_WAITING), %eax
970 je 5f
971 andl $(~LCK_W_WAITING), %ecx
972
973 testl $(LCK_RW_PRIV_EXCL), %eax
974 jne 6f
9755:
976 andl $(~LCK_R_WAITING), %ecx
9776:
978 lock
979 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
980 jne 7f
981
982#if __i386__
983 pushl %eax
984 push %edx
985 call EXT(lck_rw_done_gen)
986 addl $8, %esp
987#else
988 mov %eax,%esi /* old flags in %rsi */
989 /* lock is in %rdi already */
990 call EXT(lck_rw_done_gen)
991#endif
992 ret
9937:
994 PAUSE
995 jmp 1b
9968:
6d2010ae 997 ALIGN_STACK()
b0d623f7
A
998 LOAD_STRING_ARG0(rwl_release_error_str)
999 CALL_PANIC()
1000
1c79356b 1001
b0d623f7
A
1002
1003/*
1004 * lck_rw_type_t lck_rw_lock_exclusive_to_shared(lck_rw_t *)
1005 *
1006 */
1007Entry(lck_rw_lock_exclusive_to_shared)
1008 LOAD_LCK_RW_REGISTER
10091:
1010 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield, interlock and reader count */
1011 testl $(LCK_RW_INTERLOCK), %eax
1012 jne 6f /* wait for interlock to clear */
1013
1014 movl %eax, %ecx /* keep original value in %eax for cmpxchgl */
1015 incl %ecx /* Increment reader count */
1016
1017 testl $(LCK_RW_WANT_UPGRADE), %ecx
1018 je 2f
1019 andl $(~LCK_RW_WANT_UPGRADE), %ecx
1020 jmp 3f
10212:
1022 andl $(~LCK_RW_WANT_WRITE), %ecx
10233:
1024 /*
1025 * test the original values to match what
1026 * lck_rw_lock_exclusive_to_shared_gen is going to do to determine
1027 * which wakeups need to happen...
1028 *
1029 * if !(fake_lck->lck_rw_priv_excl && fake_lck->lck_w_waiting)
1030 */
1031 testl $(LCK_W_WAITING), %eax
1032 je 4f
1033 testl $(LCK_RW_PRIV_EXCL), %eax
1034 jne 5f
10354:
1036 andl $(~LCK_R_WAITING), %ecx
10375:
1038 lock
1039 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
1040 jne 6f
1041
1042#if __i386__
1043 pushl %eax
1044 push %edx
1045 call EXT(lck_rw_lock_exclusive_to_shared_gen)
1046 addl $8, %esp
1047#else
1048 mov %eax,%esi
1049 call EXT(lck_rw_lock_exclusive_to_shared_gen)
1050#endif
1051 ret
10526:
1053 PAUSE
1054 jmp 1b
2d21ac55 1055
2d21ac55 1056
2d21ac55 1057
b0d623f7
A
1058/*
1059 * int lck_rw_grab_want(lck_rw_t *)
1060 *
1061 */
1062Entry(lck_rw_grab_want)
1063 LOAD_LCK_RW_REGISTER
10641:
1065 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield, interlock and reader count */
1066 testl $(LCK_RW_INTERLOCK), %eax
1067 jne 3f /* wait for interlock to clear */
1068 testl $(LCK_RW_WANT_WRITE), %eax /* want_write has been grabbed by someone else */
1069 jne 2f /* go return failure */
2d21ac55 1070
b0d623f7
A
1071 movl %eax, %ecx /* original value in %eax for cmpxchgl */
1072 orl $(LCK_RW_WANT_WRITE), %ecx
1073 lock
1074 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
1075 jne 2f
1076 /* we now own want_write */
1077 movl $1, %eax /* return success */
2d21ac55 1078 ret
b0d623f7
A
10792:
1080 xorl %eax, %eax /* return failure */
2d21ac55 1081 ret
b0d623f7
A
10823:
1083 PAUSE
1084 jmp 1b
2d21ac55 1085
b0d623f7
A
1086
1087#define RW_LOCK_SHARED_OR_UPGRADE_MASK (LCK_RW_SHARED_MASK | LCK_RW_INTERLOCK | LCK_RW_WANT_UPGRADE)
1088/*
1089 * int lck_rw_held_read_or_upgrade(lck_rw_t *)
1090 *
1091 */
1092Entry(lck_rw_held_read_or_upgrade)
1093 LOAD_LCK_RW_REGISTER
1094 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield, interlock and reader count */
1095 andl $(RW_LOCK_SHARED_OR_UPGRADE_MASK), %eax
1096 ret
2d21ac55 1097
2d21ac55
A
1098
1099
b0d623f7
A
1100/*
1101 * N.B.: On x86, statistics are currently recorded for all indirect mutexes.
1102 * Also, only the acquire attempt count (GRP_MTX_STAT_UTIL) is maintained
1103 * as a 64-bit quantity (this matches the existing PowerPC implementation,
1104 * and the new x86 specific statistics are also maintained as 32-bit
1105 * quantities).
1106 *
1107 *
1108 * Enable this preprocessor define to record the first miss alone
1109 * By default, we count every miss, hence multiple misses may be
1110 * recorded for a single lock acquire attempt via lck_mtx_lock
1111 */
1112#undef LOG_FIRST_MISS_ALONE
1c79356b 1113
b0d623f7
A
1114/*
1115 * This preprocessor define controls whether the R-M-W update of the
1116 * per-group statistics elements are atomic (LOCK-prefixed)
1117 * Enabled by default.
1118 */
1119#define ATOMIC_STAT_UPDATES 1
1c79356b 1120
b0d623f7
A
1121#if defined(ATOMIC_STAT_UPDATES)
1122#define LOCK_IF_ATOMIC_STAT_UPDATES lock
1123#else
1124#define LOCK_IF_ATOMIC_STAT_UPDATES
1125#endif /* ATOMIC_STAT_UPDATES */
2d21ac55 1126
2d21ac55 1127
b0d623f7
A
1128/*
1129 * For most routines, the lck_mtx_t pointer is loaded into a
1130 * register initially, and the owner field checked for indirection.
1131 * Eventually the lock owner is loaded into a register and examined.
1132 */
1133
1134#define M_OWNER MUTEX_OWNER
1135#define M_PTR MUTEX_PTR
1136#define M_STATE MUTEX_STATE
1137
1138#if defined(__i386__)
1139
1140#define LMTX_ARG0 B_ARG0
1141#define LMTX_ARG1 B_ARG1
1142#define LMTX_REG %edx
1143#define LMTX_A_REG %eax
1144#define LMTX_A_REG32 %eax
1145#define LMTX_C_REG %ecx
1146#define LMTX_C_REG32 %ecx
b0d623f7 1147#define LMTX_RET_REG %eax
6d2010ae 1148#define LMTX_RET_REG32 %eax
b0d623f7
A
1149#define LMTX_LGROUP_REG %esi
1150#define LMTX_SSTATE_REG %edi
1151#define LOAD_LMTX_REG(arg) mov arg, LMTX_REG
b0d623f7
A
1152#define LMTX_CHK_EXTENDED cmp LMTX_REG, LMTX_ARG0
1153#define LMTX_ASSERT_OWNED cmpl $(MUTEX_ASSERT_OWNED), LMTX_ARG1
1154
1155#define LMTX_ENTER_EXTENDED \
1156 mov M_PTR(LMTX_REG), LMTX_REG ; \
1157 push LMTX_LGROUP_REG ; \
1158 push LMTX_SSTATE_REG ; \
1159 xor LMTX_SSTATE_REG, LMTX_SSTATE_REG ; \
1160 mov MUTEX_GRP(LMTX_REG), LMTX_LGROUP_REG ; \
1161 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1162 addl $1, GRP_MTX_STAT_UTIL(LMTX_LGROUP_REG) ; \
1163 jnc 11f ; \
1164 incl GRP_MTX_STAT_UTIL+4(LMTX_LGROUP_REG) ; \
116511:
1166
1167#define LMTX_EXIT_EXTENDED \
1168 pop LMTX_SSTATE_REG ; \
1169 pop LMTX_LGROUP_REG
1170
1171
1172#define LMTX_CHK_EXTENDED_EXIT \
1173 cmp LMTX_REG, LMTX_ARG0 ; \
1174 je 12f ; \
1175 pop LMTX_SSTATE_REG ; \
1176 pop LMTX_LGROUP_REG ; \
117712:
1178
1179
1180#if LOG_FIRST_MISS_ALONE
1181#define LMTX_UPDATE_MISS \
1182 test $1, LMTX_SSTATE_REG ; \
1183 jnz 11f ; \
1184 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1185 incl GRP_MTX_STAT_MISS(LMTX_LGROUP_REG) ; \
1186 or $1, LMTX_SSTATE_REG ; \
118711:
1188#else
1189#define LMTX_UPDATE_MISS \
1190 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1191 incl GRP_MTX_STAT_MISS(LMTX_LGROUP_REG)
2d21ac55 1192#endif
1c79356b 1193
b0d623f7
A
1194
1195#if LOG_FIRST_MISS_ALONE
1196#define LMTX_UPDATE_WAIT \
1197 test $2, LMTX_SSTATE_REG ; \
1198 jnz 11f ; \
1199 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1200 incl GRP_MTX_STAT_WAIT(LMTX_LGROUP_REG) ; \
1201 or $2, LMTX_SSTATE_REG ; \
120211:
1203#else
1204#define LMTX_UPDATE_WAIT \
1205 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1206 incl GRP_MTX_STAT_WAIT(LMTX_LGROUP_REG)
1207#endif
2d21ac55 1208
b0d623f7
A
1209
1210/*
1211 * Record the "direct wait" statistic, which indicates if a
1212 * miss proceeded to block directly without spinning--occurs
1213 * if the owner of the mutex isn't running on another processor
1214 * at the time of the check.
1215 */
1216#define LMTX_UPDATE_DIRECT_WAIT \
1217 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1218 incl GRP_MTX_STAT_DIRECT_WAIT(LMTX_LGROUP_REG)
1c79356b 1219
b0d623f7
A
1220
1221#define LMTX_CALLEXT1(func_name) \
1222 push LMTX_REG ; \
1223 push LMTX_REG ; \
1224 call EXT(func_name) ; \
1225 add $4, %esp ; \
1226 pop LMTX_REG
1227
1228#define LMTX_CALLEXT2(func_name, reg) \
1229 push LMTX_REG ; \
1230 push reg ; \
1231 push LMTX_REG ; \
1232 call EXT(func_name) ; \
1233 add $8, %esp ; \
1234 pop LMTX_REG
1235
1236#elif defined(__x86_64__)
1237
1238#define LMTX_ARG0 %rdi
1239#define LMTX_ARG1 %rsi
1240#define LMTX_REG_ORIG %rdi
1241#define LMTX_REG %rdx
1242#define LMTX_A_REG %rax
1243#define LMTX_A_REG32 %eax
1244#define LMTX_C_REG %rcx
1245#define LMTX_C_REG32 %ecx
b0d623f7 1246#define LMTX_RET_REG %rax
6d2010ae 1247#define LMTX_RET_REG32 %eax
b0d623f7
A
1248#define LMTX_LGROUP_REG %r10
1249#define LMTX_SSTATE_REG %r11
1250#define LOAD_LMTX_REG(arg) mov %rdi, %rdx
b0d623f7
A
1251#define LMTX_CHK_EXTENDED cmp LMTX_REG, LMTX_REG_ORIG
1252#define LMTX_ASSERT_OWNED cmp $(MUTEX_ASSERT_OWNED), LMTX_ARG1
1253
1254#define LMTX_ENTER_EXTENDED \
1255 mov M_PTR(LMTX_REG), LMTX_REG ; \
1256 xor LMTX_SSTATE_REG, LMTX_SSTATE_REG ; \
1257 mov MUTEX_GRP(LMTX_REG), LMTX_LGROUP_REG ; \
1258 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1259 incq GRP_MTX_STAT_UTIL(LMTX_LGROUP_REG)
1260
1261#define LMTX_EXIT_EXTENDED
1262
1263#define LMTX_CHK_EXTENDED_EXIT
1264
1265
1266#if LOG_FIRST_MISS_ALONE
1267#define LMTX_UPDATE_MISS \
1268 test $1, LMTX_SSTATE_REG ; \
1269 jnz 11f ; \
1270 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1271 incl GRP_MTX_STAT_MISS(LMTX_LGROUP_REG) ; \
1272 or $1, LMTX_SSTATE_REG ; \
127311:
1274#else
1275#define LMTX_UPDATE_MISS \
1276 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1277 incl GRP_MTX_STAT_MISS(LMTX_LGROUP_REG)
1278#endif
1279
2d21ac55 1280
b0d623f7
A
1281#if LOG_FIRST_MISS_ALONE
1282#define LMTX_UPDATE_WAIT \
1283 test $2, LMTX_SSTATE_REG ; \
1284 jnz 11f ; \
1285 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1286 incl GRP_MTX_STAT_WAIT(LMTX_LGROUP_REG) ; \
1287 or $2, LMTX_SSTATE_REG ; \
128811:
1289#else
1290#define LMTX_UPDATE_WAIT \
1291 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1292 incl GRP_MTX_STAT_WAIT(LMTX_LGROUP_REG)
1293#endif
0c530ab8 1294
1c79356b 1295
b0d623f7
A
1296/*
1297 * Record the "direct wait" statistic, which indicates if a
1298 * miss proceeded to block directly without spinning--occurs
1299 * if the owner of the mutex isn't running on another processor
1300 * at the time of the check.
1301 */
1302#define LMTX_UPDATE_DIRECT_WAIT \
1303 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1304 incl GRP_MTX_STAT_DIRECT_WAIT(LMTX_LGROUP_REG)
91447636 1305
b0d623f7
A
1306
1307#define LMTX_CALLEXT1(func_name) \
1308 LMTX_CHK_EXTENDED ; \
1309 je 12f ; \
1310 push LMTX_LGROUP_REG ; \
1311 push LMTX_SSTATE_REG ; \
131212: push LMTX_REG_ORIG ; \
1313 push LMTX_REG ; \
1314 mov LMTX_REG, LMTX_ARG0 ; \
1315 call EXT(func_name) ; \
1316 pop LMTX_REG ; \
1317 pop LMTX_REG_ORIG ; \
1318 LMTX_CHK_EXTENDED ; \
1319 je 12f ; \
1320 pop LMTX_SSTATE_REG ; \
1321 pop LMTX_LGROUP_REG ; \
132212:
1323
1324#define LMTX_CALLEXT2(func_name, reg) \
1325 LMTX_CHK_EXTENDED ; \
1326 je 12f ; \
1327 push LMTX_LGROUP_REG ; \
1328 push LMTX_SSTATE_REG ; \
132912: push LMTX_REG_ORIG ; \
1330 push LMTX_REG ; \
1331 mov reg, LMTX_ARG1 ; \
1332 mov LMTX_REG, LMTX_ARG0 ; \
1333 call EXT(func_name) ; \
1334 pop LMTX_REG ; \
1335 pop LMTX_REG_ORIG ; \
1336 LMTX_CHK_EXTENDED ; \
1337 je 12f ; \
1338 pop LMTX_SSTATE_REG ; \
1339 pop LMTX_LGROUP_REG ; \
134012:
6d2010ae 1341
b0d623f7
A
1342#else
1343#error Unsupported architecture
2d21ac55 1344#endif
1c79356b 1345
2d21ac55 1346
b0d623f7
A
1347#define M_WAITERS_MSK 0x0000ffff
1348#define M_PRIORITY_MSK 0x00ff0000
1349#define M_ILOCKED_MSK 0x01000000
1350#define M_MLOCKED_MSK 0x02000000
1351#define M_PROMOTED_MSK 0x04000000
1352#define M_SPIN_MSK 0x08000000
1353
2d21ac55
A
1354/*
1355 * void lck_mtx_assert(lck_mtx_t* l, unsigned int)
2d21ac55
A
1356 * Takes the address of a lock, and an assertion type as parameters.
1357 * The assertion can take one of two forms determine by the type
1358 * parameter: either the lock is held by the current thread, and the
1359 * type is LCK_MTX_ASSERT_OWNED, or it isn't and the type is
b0d623f7 1360 * LCK_MTX_ASSERT_NOTOWNED. Calls panic on assertion failure.
2d21ac55
A
1361 *
1362 */
1363
b0d623f7
A
1364NONLEAF_ENTRY(lck_mtx_assert)
1365 LOAD_LMTX_REG(B_ARG0) /* Load lock address */
1366 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG /* Load current thread */
2d21ac55 1367
6d2010ae
A
1368 mov M_STATE(LMTX_REG), LMTX_C_REG32
1369 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex? */
1370 jne 0f
1371 mov M_PTR(LMTX_REG), LMTX_REG /* If so, take indirection */
13720:
b0d623f7
A
1373 mov M_OWNER(LMTX_REG), LMTX_C_REG /* Load owner */
1374 LMTX_ASSERT_OWNED
2d21ac55 1375 jne 2f /* Assert ownership? */
b0d623f7 1376 cmp LMTX_A_REG, LMTX_C_REG /* Current thread match? */
2d21ac55 1377 jne 3f /* no, go panic */
b0d623f7
A
1378 testl $(M_ILOCKED_MSK | M_MLOCKED_MSK), M_STATE(LMTX_REG)
1379 je 3f
2d21ac55 13801: /* yes, we own it */
b0d623f7 1381 NONLEAF_RET
2d21ac55 13822:
b0d623f7 1383 cmp LMTX_A_REG, LMTX_C_REG /* Current thread match? */
2d21ac55 1384 jne 1b /* No, return */
6d2010ae
A
1385 ALIGN_STACK()
1386 LOAD_PTR_ARG1(LMTX_REG)
b0d623f7 1387 LOAD_STRING_ARG0(mutex_assert_owned_str)
2d21ac55
A
1388 jmp 4f
13893:
6d2010ae
A
1390 ALIGN_STACK()
1391 LOAD_PTR_ARG1(LMTX_REG)
b0d623f7 1392 LOAD_STRING_ARG0(mutex_assert_not_owned_str)
2d21ac55 13934:
b0d623f7
A
1394 CALL_PANIC()
1395
1396
1397lck_mtx_destroyed:
6d2010ae
A
1398 ALIGN_STACK()
1399 LOAD_PTR_ARG1(LMTX_REG)
b0d623f7
A
1400 LOAD_STRING_ARG0(mutex_interlock_destroyed_str)
1401 CALL_PANIC()
1402
2d21ac55
A
1403
1404.data
1405mutex_assert_not_owned_str:
1406 .asciz "mutex (%p) not owned\n"
1407mutex_assert_owned_str:
1408 .asciz "mutex (%p) owned\n"
b0d623f7
A
1409mutex_interlock_destroyed_str:
1410 .asciz "trying to interlock destroyed mutex (%p)"
2d21ac55
A
1411.text
1412
2d21ac55
A
1413
1414
91447636
A
1415/*
1416 * lck_mtx_lock()
1417 * lck_mtx_try_lock()
b0d623f7 1418 * lck_mtx_unlock()
2d21ac55 1419 * lck_mtx_lock_spin()
6d2010ae 1420 * lck_mtx_lock_spin_always()
2d21ac55 1421 * lck_mtx_convert_spin()
91447636 1422 */
6d2010ae
A
1423NONLEAF_ENTRY(lck_mtx_lock_spin_always)
1424 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
1425 jmp Llmls_avoid_check
1426
2d21ac55 1427NONLEAF_ENTRY(lck_mtx_lock_spin)
b0d623f7 1428 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
1c79356b 1429
91447636 1430 CHECK_PREEMPTION_LEVEL()
6d2010ae 1431Llmls_avoid_check:
b0d623f7 1432 mov M_STATE(LMTX_REG), LMTX_C_REG32
6d2010ae
A
1433 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32 /* is the interlock or mutex held */
1434 jnz Llmls_slow
1435Llmls_try: /* no - can't be INDIRECT, DESTROYED or locked */
b0d623f7 1436 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
6d2010ae
A
1437 or $(M_ILOCKED_MSK | M_SPIN_MSK), LMTX_C_REG32
1438
1439 PREEMPTION_DISABLE
b0d623f7
A
1440 lock
1441 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
6d2010ae 1442 jne Llmls_busy_disabled
2d21ac55 1443
b0d623f7
A
1444 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG
1445 mov LMTX_A_REG, M_OWNER(LMTX_REG) /* record owner of interlock */
6d2010ae
A
1446#if MACH_LDEBUG
1447 test LMTX_A_REG, LMTX_A_REG
1448 jz 1f
1449 incl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
14501:
1451#endif /* MACH_LDEBUG */
0c530ab8 1452
b0d623f7
A
1453 LMTX_CHK_EXTENDED_EXIT
1454 /* return with the interlock held and preemption disabled */
2d21ac55
A
1455 leave
1456#if CONFIG_DTRACE
b0d623f7 1457 LOCKSTAT_LABEL(_lck_mtx_lock_spin_lockstat_patch_point)
2d21ac55 1458 ret
b0d623f7
A
1459 /* inherit lock pointer in LMTX_REG above */
1460 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_SPIN_ACQUIRE, LMTX_REG)
2d21ac55
A
1461#endif
1462 ret
0c530ab8 1463
6d2010ae
A
1464Llmls_slow:
1465 test $M_ILOCKED_MSK, LMTX_C_REG32 /* is the interlock held */
1466 jz Llml_contended /* no, must have been the mutex */
2d21ac55 1467
6d2010ae 1468 cmp $(MUTEX_DESTROYED), LMTX_C_REG32 /* check to see if its marked destroyed */
b0d623f7 1469 je lck_mtx_destroyed
6d2010ae
A
1470 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex */
1471 jne Llmls_loop /* no... must be interlocked */
5d5c5d0d 1472
b0d623f7 1473 LMTX_ENTER_EXTENDED
0c530ab8 1474
b0d623f7 1475 mov M_STATE(LMTX_REG), LMTX_C_REG32
6d2010ae
A
1476 test $(M_SPIN_MSK), LMTX_C_REG32
1477 jz Llmls_loop1
2d21ac55 1478
6d2010ae
A
1479 LMTX_UPDATE_MISS /* M_SPIN_MSK was set, so M_ILOCKED_MSK must also be present */
1480Llmls_loop:
2d21ac55 1481 PAUSE
b0d623f7 1482 mov M_STATE(LMTX_REG), LMTX_C_REG32
6d2010ae
A
1483Llmls_loop1:
1484 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32
1485 jz Llmls_try
1486 test $(M_MLOCKED_MSK), LMTX_C_REG32
1487 jnz Llml_contended /* mutex owned by someone else, go contend for it */
1488 jmp Llmls_loop
1489
1490Llmls_busy_disabled:
1491 PREEMPTION_ENABLE
1492 jmp Llmls_loop
2d21ac55 1493
9bccf70c 1494
6d2010ae
A
1495
1496NONLEAF_ENTRY(lck_mtx_lock)
1497 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
1498
1499 CHECK_PREEMPTION_LEVEL()
1500
1501 mov M_STATE(LMTX_REG), LMTX_C_REG32
1502 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32 /* is the interlock or mutex held */
1503 jnz Llml_slow
1504Llml_try: /* no - can't be INDIRECT, DESTROYED or locked */
b0d623f7 1505 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
6d2010ae
A
1506 or $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32
1507
1508 PREEMPTION_DISABLE
b0d623f7
A
1509 lock
1510 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
6d2010ae 1511 jne Llml_busy_disabled
2d21ac55 1512
b0d623f7
A
1513 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG
1514 mov LMTX_A_REG, M_OWNER(LMTX_REG) /* record owner of mutex */
6d2010ae
A
1515#if MACH_LDEBUG
1516 test LMTX_A_REG, LMTX_A_REG
1517 jz 1f
1518 incl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
15191:
1520#endif /* MACH_LDEBUG */
2d21ac55 1521
b0d623f7 1522 testl $(M_WAITERS_MSK), M_STATE(LMTX_REG)
6d2010ae 1523 jz Llml_finish
2d21ac55 1524
b0d623f7 1525 LMTX_CALLEXT1(lck_mtx_lock_acquire_x86)
6d2010ae
A
1526
1527Llml_finish:
1528 andl $(~M_ILOCKED_MSK), M_STATE(LMTX_REG)
1529 PREEMPTION_ENABLE
1530
b0d623f7
A
1531 LMTX_CHK_EXTENDED /* is this an extended mutex */
1532 jne 2f
2d21ac55 1533
b0d623f7
A
1534 leave
1535#if CONFIG_DTRACE
1536 LOCKSTAT_LABEL(_lck_mtx_lock_lockstat_patch_point)
1537 ret
1538 /* inherit lock pointer in LMTX_REG above */
1539 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_ACQUIRE, LMTX_REG)
1540#endif
1541 ret
15422:
1543 LMTX_EXIT_EXTENDED
2d21ac55
A
1544 leave
1545#if CONFIG_DTRACE
1546 LOCKSTAT_LABEL(_lck_mtx_lock_ext_lockstat_patch_point)
1547 ret
b0d623f7
A
1548 /* inherit lock pointer in LMTX_REG above */
1549 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_LOCK_ACQUIRE, LMTX_REG)
2d21ac55
A
1550#endif
1551 ret
6d2010ae
A
1552
1553
1554Llml_slow:
1555 test $M_ILOCKED_MSK, LMTX_C_REG32 /* is the interlock held */
1556 jz Llml_contended /* no, must have been the mutex */
2d21ac55 1557
6d2010ae
A
1558 cmp $(MUTEX_DESTROYED), LMTX_C_REG32 /* check to see if its marked destroyed */
1559 je lck_mtx_destroyed
1560 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex? */
1561 jne Llml_loop /* no... must be interlocked */
1562
1563 LMTX_ENTER_EXTENDED
1564
1565 mov M_STATE(LMTX_REG), LMTX_C_REG32
1566 test $(M_SPIN_MSK), LMTX_C_REG32
1567 jz Llml_loop1
1568
1569 LMTX_UPDATE_MISS /* M_SPIN_MSK was set, so M_ILOCKED_MSK must also be present */
1570Llml_loop:
1571 PAUSE
1572 mov M_STATE(LMTX_REG), LMTX_C_REG32
1573Llml_loop1:
1574 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32
1575 jz Llml_try
1576 test $(M_MLOCKED_MSK), LMTX_C_REG32
1577 jnz Llml_contended /* mutex owned by someone else, go contend for it */
1578 jmp Llml_loop
1579
1580Llml_busy_disabled:
1581 PREEMPTION_ENABLE
1582 jmp Llml_loop
2d21ac55 1583
6d2010ae 1584
b0d623f7
A
1585Llml_contended:
1586 LMTX_CHK_EXTENDED /* is this an extended mutex */
1587 je 0f
1588 LMTX_UPDATE_MISS
15890:
1590 LMTX_CALLEXT1(lck_mtx_lock_spinwait_x86)
1591
1592 test LMTX_RET_REG, LMTX_RET_REG
6d2010ae
A
1593 jz Llml_acquired /* acquired mutex, interlock held and preemption disabled */
1594
b0d623f7
A
1595 cmp $1, LMTX_RET_REG /* check for direct wait status */
1596 je 2f
1597 LMTX_CHK_EXTENDED /* is this an extended mutex */
1598 je 2f
1599 LMTX_UPDATE_DIRECT_WAIT
16002:
1601 mov M_STATE(LMTX_REG), LMTX_C_REG32
6d2010ae
A
1602 test $(M_ILOCKED_MSK), LMTX_C_REG32
1603 jnz 6f
b0d623f7 1604
b0d623f7 1605 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
6d2010ae
A
1606 or $(M_ILOCKED_MSK), LMTX_C_REG32 /* try to take the interlock */
1607
1608 PREEMPTION_DISABLE
b0d623f7
A
1609 lock
1610 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
1611 jne 5f
2d21ac55 1612
6d2010ae
A
1613 test $(M_MLOCKED_MSK), LMTX_C_REG32 /* we've got the interlock and */
1614 jnz 3f
1615 or $(M_MLOCKED_MSK), LMTX_C_REG32 /* the mutex is free... grab it directly */
1616 mov LMTX_C_REG32, M_STATE(LMTX_REG)
b0d623f7
A
1617
1618 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG
1619 mov LMTX_A_REG, M_OWNER(LMTX_REG) /* record owner of mutex */
6d2010ae
A
1620#if MACH_LDEBUG
1621 test LMTX_A_REG, LMTX_A_REG
1622 jz 1f
1623 incl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
16241:
1625#endif /* MACH_LDEBUG */
2d21ac55 1626
6d2010ae
A
1627Llml_acquired:
1628 testl $(M_WAITERS_MSK), M_STATE(LMTX_REG)
1629 jnz 1f
1630 mov M_OWNER(LMTX_REG), LMTX_A_REG
1631 mov TH_WAS_PROMOTED_ON_WAKEUP(LMTX_A_REG), LMTX_A_REG32
1632 test LMTX_A_REG32, LMTX_A_REG32
1633 jz Llml_finish
16341:
1635 LMTX_CALLEXT1(lck_mtx_lock_acquire_x86)
1636 jmp Llml_finish
b0d623f7 1637
6d2010ae 16383: /* interlock held, mutex busy */
b0d623f7
A
1639 LMTX_CHK_EXTENDED /* is this an extended mutex */
1640 je 4f
1641 LMTX_UPDATE_WAIT
16424:
1643 LMTX_CALLEXT1(lck_mtx_lock_wait_x86)
1644 jmp Llml_contended
16455:
6d2010ae 1646 PREEMPTION_ENABLE
b0d623f7
A
16476:
1648 PAUSE
1649 jmp 2b
2d21ac55
A
1650
1651
b0d623f7 1652
2d21ac55 1653NONLEAF_ENTRY(lck_mtx_try_lock_spin)
b0d623f7 1654 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
1c79356b 1655
b0d623f7 1656 mov M_STATE(LMTX_REG), LMTX_C_REG32
6d2010ae
A
1657 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32 /* is the interlock or mutex held */
1658 jnz Llmts_slow
1659Llmts_try: /* no - can't be INDIRECT, DESTROYED or locked */
b0d623f7
A
1660 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
1661 or $(M_ILOCKED_MSK | M_SPIN_MSK), LMTX_C_REG
6d2010ae
A
1662
1663 PREEMPTION_DISABLE
b0d623f7
A
1664 lock
1665 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
6d2010ae 1666 jne Llmts_busy_disabled
2d21ac55 1667
b0d623f7
A
1668 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG
1669 mov LMTX_A_REG, M_OWNER(LMTX_REG) /* record owner of mutex */
6d2010ae
A
1670#if MACH_LDEBUG
1671 test LMTX_A_REG, LMTX_A_REG
1672 jz 1f
1673 incl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
16741:
1675#endif /* MACH_LDEBUG */
2d21ac55 1676
b0d623f7 1677 LMTX_CHK_EXTENDED_EXIT
2d21ac55 1678 leave
b0d623f7 1679
2d21ac55 1680#if CONFIG_DTRACE
b0d623f7 1681 mov $1, LMTX_RET_REG /* return success */
2d21ac55
A
1682 LOCKSTAT_LABEL(_lck_mtx_try_lock_spin_lockstat_patch_point)
1683 ret
b0d623f7
A
1684 /* inherit lock pointer in LMTX_REG above */
1685 LOCKSTAT_RECORD(LS_LCK_MTX_TRY_SPIN_LOCK_ACQUIRE, LMTX_REG)
2d21ac55 1686#endif
b0d623f7 1687 mov $1, LMTX_RET_REG /* return success */
2d21ac55
A
1688 ret
1689
6d2010ae
A
1690Llmts_slow:
1691 test $(M_ILOCKED_MSK), LMTX_C_REG32 /* is the interlock held */
1692 jz Llmts_fail /* no, must be held as a mutex */
2d21ac55 1693
6d2010ae 1694 cmp $(MUTEX_DESTROYED), LMTX_C_REG32 /* check to see if its marked destroyed */
b0d623f7 1695 je lck_mtx_destroyed
6d2010ae
A
1696 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex? */
1697 jne Llmts_loop1
2d21ac55 1698
b0d623f7 1699 LMTX_ENTER_EXTENDED
6d2010ae 1700Llmts_loop:
b0d623f7
A
1701 PAUSE
1702 mov M_STATE(LMTX_REG), LMTX_C_REG32
6d2010ae
A
1703Llmts_loop1:
1704 test $(M_MLOCKED_MSK | M_SPIN_MSK), LMTX_C_REG32
1705 jnz Llmts_fail
1706 test $(M_ILOCKED_MSK), LMTX_C_REG32
1707 jz Llmts_try
1708 jmp Llmts_loop
1709
1710Llmts_busy_disabled:
1711 PREEMPTION_ENABLE
1712 jmp Llmts_loop
1713
1714
1715
1716NONLEAF_ENTRY(lck_mtx_try_lock)
1717 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
b0d623f7 1718
6d2010ae
A
1719 mov M_STATE(LMTX_REG), LMTX_C_REG32
1720 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32 /* is the interlock or mutex held */
1721 jnz Llmt_slow
1722Llmt_try: /* no - can't be INDIRECT, DESTROYED or locked */
b0d623f7 1723 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
6d2010ae
A
1724 or $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32
1725
1726 PREEMPTION_DISABLE
b0d623f7
A
1727 lock
1728 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
6d2010ae 1729 jne Llmt_busy_disabled
9bccf70c 1730
b0d623f7
A
1731 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG
1732 mov LMTX_A_REG, M_OWNER(LMTX_REG) /* record owner of mutex */
6d2010ae
A
1733#if MACH_LDEBUG
1734 test LMTX_A_REG, LMTX_A_REG
1735 jz 1f
1736 incl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
17371:
1738#endif /* MACH_LDEBUG */
1c79356b 1739
b0d623f7
A
1740 LMTX_CHK_EXTENDED_EXIT
1741
6d2010ae
A
1742 test $(M_WAITERS_MSK), LMTX_C_REG32
1743 jz 0f
1744
b0d623f7 1745 LMTX_CALLEXT1(lck_mtx_lock_acquire_x86)
6d2010ae
A
17460:
1747 andl $(~M_ILOCKED_MSK), M_STATE(LMTX_REG)
1748 PREEMPTION_ENABLE
b0d623f7 1749
6d2010ae 1750 leave
2d21ac55 1751#if CONFIG_DTRACE
b0d623f7 1752 mov $1, LMTX_RET_REG /* return success */
2d21ac55
A
1753 /* Dtrace probe: LS_LCK_MTX_TRY_LOCK_ACQUIRE */
1754 LOCKSTAT_LABEL(_lck_mtx_try_lock_lockstat_patch_point)
1755 ret
b0d623f7
A
1756 /* inherit lock pointer in LMTX_REG from above */
1757 LOCKSTAT_RECORD(LS_LCK_MTX_TRY_LOCK_ACQUIRE, LMTX_REG)
1758#endif
1759 mov $1, LMTX_RET_REG /* return success */
2d21ac55 1760 ret
1c79356b 1761
6d2010ae
A
1762Llmt_slow:
1763 test $(M_ILOCKED_MSK), LMTX_C_REG32 /* is the interlock held */
1764 jz Llmt_fail /* no, must be held as a mutex */
1765
1766 cmp $(MUTEX_DESTROYED), LMTX_C_REG32 /* check to see if its marked destroyed */
1767 je lck_mtx_destroyed
1768 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex? */
1769 jne Llmt_loop
1770
1771 LMTX_ENTER_EXTENDED
1772Llmt_loop:
1773 PAUSE
1774 mov M_STATE(LMTX_REG), LMTX_C_REG32
1775Llmt_loop1:
1776 test $(M_MLOCKED_MSK | M_SPIN_MSK), LMTX_C_REG32
1777 jnz Llmt_fail
1778 test $(M_ILOCKED_MSK), LMTX_C_REG32
1779 jz Llmt_try
1780 jmp Llmt_loop
1781
1782Llmt_busy_disabled:
1783 PREEMPTION_ENABLE
1784 jmp Llmt_loop
1785
0c530ab8
A
1786
1787Llmt_fail:
b0d623f7
A
1788Llmts_fail:
1789 LMTX_CHK_EXTENDED /* is this an extended mutex */
1790 je 0f
1791 LMTX_UPDATE_MISS
1792 LMTX_EXIT_EXTENDED
17930:
1794 xor LMTX_RET_REG, LMTX_RET_REG
91447636 1795 NONLEAF_RET
1c79356b 1796
2d21ac55
A
1797
1798
b0d623f7
A
1799NONLEAF_ENTRY(lck_mtx_convert_spin)
1800 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
2d21ac55 1801
6d2010ae
A
1802 mov M_STATE(LMTX_REG), LMTX_C_REG32
1803 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex? */
1804 jne 0f
1805 mov M_PTR(LMTX_REG), LMTX_REG /* If so, take indirection */
1806 mov M_STATE(LMTX_REG), LMTX_C_REG32
18070:
1808 test $(M_MLOCKED_MSK), LMTX_C_REG32 /* already owned as a mutex, just return */
1809 jnz 2f
1810 test $(M_WAITERS_MSK), LMTX_C_REG32 /* are there any waiters? */
1811 jz 1f
2d21ac55 1812
6d2010ae 1813 LMTX_CALLEXT1(lck_mtx_lock_acquire_x86)
b0d623f7 1814 mov M_STATE(LMTX_REG), LMTX_C_REG32
b0d623f7 18151:
6d2010ae
A
1816 and $(~(M_ILOCKED_MSK | M_SPIN_MSK)), LMTX_C_REG32 /* convert from spin version to mutex */
1817 or $(M_MLOCKED_MSK), LMTX_C_REG32
b0d623f7 1818 mov LMTX_C_REG32, M_STATE(LMTX_REG) /* since I own the interlock, I don't need an atomic update */
2d21ac55 1819
6d2010ae 1820 PREEMPTION_ENABLE
b0d623f7
A
18212:
1822 NONLEAF_RET
2d21ac55 1823
6d2010ae 1824
2d21ac55 1825
b0d623f7 1826#if defined(__i386__)
91447636 1827NONLEAF_ENTRY(lck_mtx_unlock)
b0d623f7
A
1828 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
1829 mov M_OWNER(LMTX_REG), LMTX_A_REG
1830 test LMTX_A_REG, LMTX_A_REG
6d2010ae 1831 jnz Llmu_entry
b0d623f7
A
1832 leave
1833 ret
1834NONLEAF_ENTRY(lck_mtx_unlock_darwin10)
1835#else
1836NONLEAF_ENTRY(lck_mtx_unlock)
1837#endif
1838 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
6d2010ae
A
1839Llmu_entry:
1840 mov M_STATE(LMTX_REG), LMTX_C_REG32
b0d623f7 1841Llmu_prim:
6d2010ae 1842 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex? */
b0d623f7 1843 je Llmu_ext
1c79356b 1844
6d2010ae
A
1845Llmu_chktype:
1846 test $(M_MLOCKED_MSK), LMTX_C_REG32 /* check for full mutex */
1847 jz Llmu_unlock
1848Llmu_mutex:
b0d623f7 1849 test $(M_ILOCKED_MSK), LMTX_C_REG /* have to wait for interlock to clear */
6d2010ae 1850 jnz Llmu_busy
b0d623f7 1851
b0d623f7 1852 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
6d2010ae
A
1853 and $(~M_MLOCKED_MSK), LMTX_C_REG32 /* drop mutex */
1854 or $(M_ILOCKED_MSK), LMTX_C_REG32 /* pick up interlock */
1855
1856 PREEMPTION_DISABLE
b0d623f7
A
1857 lock
1858 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
6d2010ae 1859 jne Llmu_busy_disabled /* branch on failure to spin loop */
b0d623f7 1860
6d2010ae 1861Llmu_unlock:
b0d623f7
A
1862 xor LMTX_A_REG, LMTX_A_REG
1863 mov LMTX_A_REG, M_OWNER(LMTX_REG)
1864 mov LMTX_C_REG, LMTX_A_REG /* keep original state in %ecx for later evaluation */
6d2010ae
A
1865 and $(~(M_ILOCKED_MSK | M_SPIN_MSK | M_PROMOTED_MSK)), LMTX_A_REG
1866
1867 test $(M_WAITERS_MSK), LMTX_A_REG32
1868 jz 2f
1869 dec LMTX_A_REG32 /* decrement waiter count */
b0d623f7 18702:
6d2010ae
A
1871 mov LMTX_A_REG32, M_STATE(LMTX_REG) /* since I own the interlock, I don't need an atomic update */
1872
1873#if MACH_LDEBUG
1874 /* perform lock statistics after drop to prevent delay */
1875 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG
1876 test LMTX_A_REG, LMTX_A_REG
1877 jz 1f
1878 decl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
18791:
1880#endif /* MACH_LDEBUG */
1881
1882 test $(M_PROMOTED_MSK | M_WAITERS_MSK), LMTX_C_REG32
1883 jz 3f
b0d623f7
A
1884
1885 LMTX_CALLEXT2(lck_mtx_unlock_wakeup_x86, LMTX_C_REG)
18863:
6d2010ae
A
1887 PREEMPTION_ENABLE
1888
b0d623f7
A
1889 LMTX_CHK_EXTENDED
1890 jne 4f
2d21ac55 1891
2d21ac55
A
1892 leave
1893#if CONFIG_DTRACE
1894 /* Dtrace: LS_LCK_MTX_UNLOCK_RELEASE */
1895 LOCKSTAT_LABEL(_lck_mtx_unlock_lockstat_patch_point)
1896 ret
b0d623f7
A
1897 /* inherit lock pointer in LMTX_REG from above */
1898 LOCKSTAT_RECORD(LS_LCK_MTX_UNLOCK_RELEASE, LMTX_REG)
2d21ac55
A
1899#endif
1900 ret
b0d623f7 19014:
2d21ac55
A
1902 leave
1903#if CONFIG_DTRACE
b0d623f7
A
1904 /* Dtrace: LS_LCK_MTX_EXT_UNLOCK_RELEASE */
1905 LOCKSTAT_LABEL(_lck_mtx_ext_unlock_lockstat_patch_point)
2d21ac55 1906 ret
b0d623f7
A
1907 /* inherit lock pointer in LMTX_REG from above */
1908 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_UNLOCK_RELEASE, LMTX_REG)
2d21ac55
A
1909#endif
1910 ret
6d2010ae
A
1911
1912
1913Llmu_busy_disabled:
1914 PREEMPTION_ENABLE
1915Llmu_busy:
b0d623f7
A
1916 PAUSE
1917 mov M_STATE(LMTX_REG), LMTX_C_REG32
6d2010ae
A
1918 jmp Llmu_mutex
1919
b0d623f7
A
1920Llmu_ext:
1921 mov M_PTR(LMTX_REG), LMTX_REG
1922 mov M_OWNER(LMTX_REG), LMTX_A_REG
1923 mov %gs:CPU_ACTIVE_THREAD, LMTX_C_REG
1924 CHECK_UNLOCK(LMTX_C_REG, LMTX_A_REG)
b0d623f7 1925 mov M_STATE(LMTX_REG), LMTX_C_REG32
6d2010ae 1926 jmp Llmu_chktype
b0d623f7 1927
1c79356b 1928
2d21ac55 1929
b0d623f7
A
1930LEAF_ENTRY(lck_mtx_ilk_unlock)
1931 LOAD_LMTX_REG(L_ARG0) /* fetch lock pointer - no indirection here */
0c530ab8 1932
b0d623f7 1933 andl $(~M_ILOCKED_MSK), M_STATE(LMTX_REG)
2d21ac55 1934
b0d623f7 1935 PREEMPTION_ENABLE /* need to re-enable preemption */
2d21ac55 1936
b0d623f7
A
1937 LEAF_RET
1938
2d21ac55 1939
b0d623f7
A
1940
1941LEAF_ENTRY(lck_mtx_lock_grab_mutex)
1942 LOAD_LMTX_REG(L_ARG0) /* fetch lock pointer - no indirection here */
2d21ac55 1943
b0d623f7 1944 mov M_STATE(LMTX_REG), LMTX_C_REG32
1c79356b 1945
6d2010ae
A
1946 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32 /* can't have the mutex yet */
1947 jnz 3f
1c79356b 1948
b0d623f7 1949 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
6d2010ae
A
1950 or $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32
1951
1952 PREEMPTION_DISABLE
b0d623f7
A
1953 lock
1954 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
1955 jne 2f /* branch on failure to spin loop */
1c79356b 1956
b0d623f7
A
1957 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG
1958 mov LMTX_A_REG, M_OWNER(LMTX_REG) /* record owner of mutex */
6d2010ae
A
1959#if MACH_LDEBUG
1960 test LMTX_A_REG, LMTX_A_REG
1961 jz 1f
1962 incl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
19631:
1964#endif /* MACH_LDEBUG */
b0d623f7
A
1965
1966 mov $1, LMTX_RET_REG /* return success */
1967 LEAF_RET
19682:
6d2010ae
A
1969 PREEMPTION_ENABLE
19703:
b0d623f7 1971 xor LMTX_RET_REG, LMTX_RET_REG /* return failure */
91447636 1972 LEAF_RET
b0d623f7
A
1973
1974
2d21ac55 1975
b0d623f7
A
1976LEAF_ENTRY(lck_mtx_lock_mark_destroyed)
1977 LOAD_LMTX_REG(L_ARG0)
19781:
6d2010ae
A
1979 mov M_STATE(LMTX_REG), LMTX_C_REG32
1980 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex? */
b0d623f7
A
1981 jne 2f
1982
6d2010ae 1983 movl $(MUTEX_DESTROYED), M_STATE(LMTX_REG) /* convert to destroyed state */
b0d623f7
A
1984 jmp 3f
19852:
b0d623f7 1986 test $(M_ILOCKED_MSK), LMTX_C_REG /* have to wait for interlock to clear */
6d2010ae 1987 jnz 5f
b0d623f7 1988
6d2010ae 1989 PREEMPTION_DISABLE
b0d623f7 1990 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
6d2010ae 1991 or $(M_ILOCKED_MSK), LMTX_C_REG32
b0d623f7
A
1992 lock
1993 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
1994 jne 4f /* branch on failure to spin loop */
6d2010ae
A
1995 movl $(MUTEX_DESTROYED), M_STATE(LMTX_REG) /* convert to destroyed state */
1996 PREEMPTION_ENABLE
b0d623f7
A
19973:
1998 LEAF_RET /* return with M_ILOCKED set */
19994:
6d2010ae 2000 PREEMPTION_ENABLE
b0d623f7
A
20015:
2002 PAUSE
2003 jmp 1b
2004
6d2010ae
A
2005LEAF_ENTRY(preemption_underflow_panic)
2006 FRAME
2007 incl %gs:CPU_PREEMPTION_LEVEL
2008 ALIGN_STACK()
2009 LOAD_STRING_ARG0(16f)
2010 CALL_PANIC()
2011 hlt
2012 .data
201316: String "Preemption level underflow, possible cause unlocking an unlocked mutex or spinlock"
2014 .text
2015
2016
91447636 2017LEAF_ENTRY(_disable_preemption)
1c79356b 2018#if MACH_RT
6d2010ae 2019 PREEMPTION_DISABLE
1c79356b 2020#endif /* MACH_RT */
91447636 2021 LEAF_RET
1c79356b 2022
91447636 2023LEAF_ENTRY(_enable_preemption)
1c79356b
A
2024#if MACH_RT
2025#if MACH_ASSERT
91447636 2026 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
1c79356b 2027 jg 1f
b0d623f7 2028#if __i386__
91447636 2029 pushl %gs:CPU_PREEMPTION_LEVEL
b0d623f7
A
2030#else
2031 movl %gs:CPU_PREEMPTION_LEVEL,%esi
2032#endif
6d2010ae 2033 ALIGN_STACK()
b0d623f7
A
2034 LOAD_STRING_ARG0(_enable_preemption_less_than_zero)
2035 CALL_PANIC()
1c79356b 2036 hlt
b0d623f7
A
2037 .cstring
2038_enable_preemption_less_than_zero:
2039 .asciz "_enable_preemption: preemption_level(%d) < 0!"
1c79356b
A
2040 .text
20411:
2042#endif /* MACH_ASSERT */
6d2010ae 2043 PREEMPTION_ENABLE
1c79356b 2044#endif /* MACH_RT */
91447636 2045 LEAF_RET
1c79356b 2046
91447636 2047LEAF_ENTRY(_enable_preemption_no_check)
1c79356b
A
2048#if MACH_RT
2049#if MACH_ASSERT
91447636 2050 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
1c79356b 2051 jg 1f
6d2010ae 2052 ALIGN_STACK()
b0d623f7
A
2053 LOAD_STRING_ARG0(_enable_preemption_no_check_less_than_zero)
2054 CALL_PANIC()
1c79356b 2055 hlt
b0d623f7
A
2056 .cstring
2057_enable_preemption_no_check_less_than_zero:
2058 .asciz "_enable_preemption_no_check: preemption_level <= 0!"
1c79356b
A
2059 .text
20601:
2061#endif /* MACH_ASSERT */
91447636 2062 _ENABLE_PREEMPTION_NO_CHECK
1c79356b 2063#endif /* MACH_RT */
91447636 2064 LEAF_RET
1c79356b
A
2065
2066
91447636
A
2067LEAF_ENTRY(_mp_disable_preemption)
2068#if MACH_RT
6d2010ae 2069 PREEMPTION_DISABLE
91447636
A
2070#endif /* MACH_RT */
2071 LEAF_RET
1c79356b 2072
91447636
A
2073LEAF_ENTRY(_mp_enable_preemption)
2074#if MACH_RT
1c79356b 2075#if MACH_ASSERT
91447636 2076 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
1c79356b 2077 jg 1f
b0d623f7 2078#if __i386__
91447636 2079 pushl %gs:CPU_PREEMPTION_LEVEL
b0d623f7
A
2080#else
2081 movl %gs:CPU_PREEMPTION_LEVEL,%esi
2082#endif
6d2010ae 2083 ALIGN_PANIC()
b0d623f7
A
2084 LOAD_STRING_ARG0(_mp_enable_preemption_less_than_zero)
2085 CALL_PANIC()
1c79356b 2086 hlt
b0d623f7
A
2087 .cstring
2088_mp_enable_preemption_less_than_zero:
2089 .asciz "_mp_enable_preemption: preemption_level (%d) <= 0!"
1c79356b
A
2090 .text
20911:
2092#endif /* MACH_ASSERT */
6d2010ae 2093 PREEMPTION_ENABLE
91447636
A
2094#endif /* MACH_RT */
2095 LEAF_RET
1c79356b 2096
91447636
A
2097LEAF_ENTRY(_mp_enable_preemption_no_check)
2098#if MACH_RT
1c79356b 2099#if MACH_ASSERT
91447636 2100 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
1c79356b 2101 jg 1f
6d2010ae 2102 ALIGN_STACK()
b0d623f7
A
2103 LOAD_STRING_ARG0(_mp_enable_preemption_no_check_less_than_zero)
2104 CALL_PANIC()
1c79356b 2105 hlt
b0d623f7
A
2106 .cstring
2107_mp_enable_preemption_no_check_less_than_zero:
2108 .asciz "_mp_enable_preemption_no_check: preemption_level <= 0!"
1c79356b
A
2109 .text
21101:
2111#endif /* MACH_ASSERT */
91447636
A
2112 _ENABLE_PREEMPTION_NO_CHECK
2113#endif /* MACH_RT */
2114 LEAF_RET
1c79356b 2115
b0d623f7 2116#if __i386__
1c79356b 2117
91447636
A
2118LEAF_ENTRY(i_bit_set)
2119 movl L_ARG0,%edx
2120 movl L_ARG1,%eax
1c79356b 2121 lock
c0fea474 2122 bts %edx,(%eax)
91447636 2123 LEAF_RET
1c79356b 2124
91447636
A
2125LEAF_ENTRY(i_bit_clear)
2126 movl L_ARG0,%edx
2127 movl L_ARG1,%eax
1c79356b 2128 lock
c0fea474 2129 btr %edx,(%eax)
91447636 2130 LEAF_RET
1c79356b 2131
2d21ac55 2132
91447636
A
2133LEAF_ENTRY(bit_lock)
2134 movl L_ARG0,%ecx
2135 movl L_ARG1,%eax
1c79356b
A
21361:
2137 lock
2138 bts %ecx,(%eax)
2139 jb 1b
91447636 2140 LEAF_RET
1c79356b 2141
2d21ac55 2142
91447636
A
2143LEAF_ENTRY(bit_lock_try)
2144 movl L_ARG0,%ecx
2145 movl L_ARG1,%eax
1c79356b
A
2146 lock
2147 bts %ecx,(%eax)
2148 jb bit_lock_failed
91447636 2149 LEAF_RET /* %eax better not be null ! */
1c79356b
A
2150bit_lock_failed:
2151 xorl %eax,%eax
91447636 2152 LEAF_RET
1c79356b 2153
91447636
A
2154LEAF_ENTRY(bit_unlock)
2155 movl L_ARG0,%ecx
2156 movl L_ARG1,%eax
1c79356b
A
2157 lock
2158 btr %ecx,(%eax)
91447636 2159 LEAF_RET
2d21ac55
A
2160
2161/*
2162 * Atomic primitives, prototyped in kern/simple_lock.h
2163 */
2164LEAF_ENTRY(hw_atomic_add)
2165 movl L_ARG0, %ecx /* Load address of operand */
2166 movl L_ARG1, %eax /* Load addend */
2167 movl %eax, %edx
2168 lock
2169 xaddl %eax, (%ecx) /* Atomic exchange and add */
2170 addl %edx, %eax /* Calculate result */
2171 LEAF_RET
2172
2173LEAF_ENTRY(hw_atomic_sub)
2174 movl L_ARG0, %ecx /* Load address of operand */
2175 movl L_ARG1, %eax /* Load subtrahend */
2176 negl %eax
2177 movl %eax, %edx
2178 lock
2179 xaddl %eax, (%ecx) /* Atomic exchange and add */
2180 addl %edx, %eax /* Calculate result */
2181 LEAF_RET
2182
2183LEAF_ENTRY(hw_atomic_or)
2184 movl L_ARG0, %ecx /* Load address of operand */
2185 movl (%ecx), %eax
21861:
2187 movl L_ARG1, %edx /* Load mask */
2188 orl %eax, %edx
2189 lock
2190 cmpxchgl %edx, (%ecx) /* Atomic CAS */
2191 jne 1b
2192 movl %edx, %eax /* Result */
2193 LEAF_RET
2194/*
2195 * A variant of hw_atomic_or which doesn't return a value.
2196 * The implementation is thus comparatively more efficient.
2197 */
2198
2199LEAF_ENTRY(hw_atomic_or_noret)
2200 movl L_ARG0, %ecx /* Load address of operand */
2201 movl L_ARG1, %edx /* Load mask */
2202 lock
2203 orl %edx, (%ecx) /* Atomic OR */
2204 LEAF_RET
2205
2206LEAF_ENTRY(hw_atomic_and)
2207 movl L_ARG0, %ecx /* Load address of operand */
2208 movl (%ecx), %eax
22091:
2210 movl L_ARG1, %edx /* Load mask */
2211 andl %eax, %edx
2212 lock
2213 cmpxchgl %edx, (%ecx) /* Atomic CAS */
2214 jne 1b
2215 movl %edx, %eax /* Result */
2216 LEAF_RET
2217/*
2218 * A variant of hw_atomic_and which doesn't return a value.
2219 * The implementation is thus comparatively more efficient.
2220 */
2221
2222LEAF_ENTRY(hw_atomic_and_noret)
2223 movl L_ARG0, %ecx /* Load address of operand */
2224 movl L_ARG1, %edx /* Load mask */
2225 lock
b0d623f7
A
2226 andl %edx, (%ecx) /* Atomic AND */
2227 LEAF_RET
2228
2229#else /* !__i386__ */
2230
2231LEAF_ENTRY(i_bit_set)
2232 lock
2233 bts %edi,(%rsi)
2234 LEAF_RET
2235
2236LEAF_ENTRY(i_bit_clear)
2237 lock
2238 btr %edi,(%rsi)
2239 LEAF_RET
2240
2241
2242LEAF_ENTRY(bit_lock)
22431:
2244 lock
2245 bts %edi,(%rsi)
2246 jb 1b
2247 LEAF_RET
2248
2249
2250LEAF_ENTRY(bit_lock_try)
2251 lock
2252 bts %edi,(%rsi)
2253 jb bit_lock_failed
2254 movl $1, %eax
2d21ac55 2255 LEAF_RET
b0d623f7
A
2256bit_lock_failed:
2257 xorl %eax,%eax
2258 LEAF_RET
2259
2260LEAF_ENTRY(bit_unlock)
2261 lock
2262 btr %edi,(%rsi)
2263 LEAF_RET
2264
2265
2266/*
2267 * Atomic primitives, prototyped in kern/simple_lock.h
2268 */
2269LEAF_ENTRY(hw_atomic_add)
2270 movl %esi, %eax /* Load addend */
2271 lock
2272 xaddl %eax, (%rdi) /* Atomic exchange and add */
2273 addl %esi, %eax /* Calculate result */
2274 LEAF_RET
2275
2276LEAF_ENTRY(hw_atomic_sub)
2277 negl %esi
2278 movl %esi, %eax
2279 lock
2280 xaddl %eax, (%rdi) /* Atomic exchange and add */
2281 addl %esi, %eax /* Calculate result */
2282 LEAF_RET
2283
2284LEAF_ENTRY(hw_atomic_or)
2285 movl (%rdi), %eax
22861:
2287 movl %esi, %edx /* Load mask */
2288 orl %eax, %edx
2289 lock
2290 cmpxchgl %edx, (%rdi) /* Atomic CAS */
2291 jne 1b
2292 movl %edx, %eax /* Result */
2293 LEAF_RET
2294/*
2295 * A variant of hw_atomic_or which doesn't return a value.
2296 * The implementation is thus comparatively more efficient.
2297 */
2298
2299LEAF_ENTRY(hw_atomic_or_noret)
2300 lock
2301 orl %esi, (%rdi) /* Atomic OR */
2302 LEAF_RET
2303
2304
2305LEAF_ENTRY(hw_atomic_and)
2306 movl (%rdi), %eax
23071:
2308 movl %esi, %edx /* Load mask */
2309 andl %eax, %edx
2310 lock
2311 cmpxchgl %edx, (%rdi) /* Atomic CAS */
2312 jne 1b
2313 movl %edx, %eax /* Result */
2314 LEAF_RET
2315/*
2316 * A variant of hw_atomic_and which doesn't return a value.
2317 * The implementation is thus comparatively more efficient.
2318 */
2319
2320LEAF_ENTRY(hw_atomic_and_noret)
2321 lock
2322 andl %esi, (%rdi) /* Atomic OR */
2323 LEAF_RET
2324
2325#endif /* !__i386 __ */