2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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.
39 #include <platforms.h>
40 #include <mach_ldebug.h>
42 #include <i386/eflags.h>
43 #include <i386/trap.h>
44 #include <config_dtrace.h>
49 #define PAUSE rep; nop
51 #include <i386/pal_lock_asm.h>
54 * When performance isn't the only concern, it's
55 * nice to build stack frames...
57 #define BUILD_STACK_FRAMES (GPROF)
59 #if BUILD_STACK_FRAMES
61 /* Stack-frame-relative: */
66 #define LEAF_ENTRY(name) \
71 #define LEAF_ENTRY2(n1,n2) \
81 #else /* BUILD_STACK_FRAMES */
83 /* Stack-pointer-relative: */
88 #define LEAF_ENTRY(name) \
91 #define LEAF_ENTRY2(n1,n2) \
98 #endif /* BUILD_STACK_FRAMES */
101 /* Non-leaf routines always have a stack frame: */
103 #define NONLEAF_ENTRY(name) \
108 #define NONLEAF_ENTRY2(n1,n2) \
114 #define NONLEAF_RET \
119 /* For x86_64, the varargs ABI requires that %al indicate
120 * how many SSE register contain arguments. In our case, 0 */
122 #define ALIGN_STACK() subl $8, %esp; andl $0xFFFFFFF0, %esp ;
123 #define LOAD_STRING_ARG0(label) movl $##label, (%esp) ;
124 #define LOAD_ARG1(x) mov x, 4(%esp) ;
125 #define LOAD_PTR_ARG1(x) mov x, 4(%esp) ;
126 #define CALL_PANIC() call EXT(panic) ;
128 #define ALIGN_STACK() and $0xFFFFFFFFFFFFFFF0, %rsp ;
129 #define LOAD_STRING_ARG0(label) leaq label(%rip), %rdi ;
130 #define LOAD_ARG1(x) mov x, %esi ;
131 #define LOAD_PTR_ARG1(x) mov x, %rsi ;
132 #define CALL_PANIC() xorb %al,%al ; call EXT(panic) ;
135 #define CHECK_UNLOCK(current, owner) \
136 cmp current, owner ; \
139 LOAD_STRING_ARG0(2f) ; \
143 2: String "Mutex unlock attempted from non-owner thread"; \
149 * Routines for general lock debugging.
153 * Checks for expected lock types and calls "panic" on
154 * mismatch. Detects calls to Mutex functions with
155 * type simplelock and vice versa.
157 #define CHECK_MUTEX_TYPE() \
158 cmpl $ MUTEX_TAG,M_TYPE ; \
161 LOAD_STRING_ARG0(2f) ; \
165 2: String "not a mutex!" ; \
170 * If one or more simplelocks are currently held by a thread,
171 * an attempt to acquire a mutex will cause this check to fail
172 * (since a mutex lock may context switch, holding a simplelock
173 * is not a good thing).
176 #define CHECK_PREEMPTION_LEVEL() \
177 cmpl $0,%gs:CPU_HIBERNATE ; \
179 cmpl $0,%gs:CPU_PREEMPTION_LEVEL ; \
182 movl %gs:CPU_PREEMPTION_LEVEL, %eax ; \
184 LOAD_STRING_ARG0(2f) ; \
188 2: String "preemption_level(%d) != 0!" ; \
192 #define CHECK_PREEMPTION_LEVEL()
195 #define CHECK_MYLOCK(current, owner) \
196 cmp current, owner ; \
199 LOAD_STRING_ARG0(2f) ; \
203 2: String "Attempt to recursively lock a non-recursive lock"; \
207 #else /* MACH_LDEBUG */
208 #define CHECK_MUTEX_TYPE()
209 #define CHECK_PREEMPTION_LEVEL()
210 #define CHECK_MYLOCK(thd)
211 #endif /* MACH_LDEBUG */
213 #define PREEMPTION_DISABLE \
214 incl %gs:CPU_PREEMPTION_LEVEL
216 #define PREEMPTION_LEVEL_DEBUG 1
217 #if PREEMPTION_LEVEL_DEBUG
218 #define PREEMPTION_ENABLE \
219 decl %gs:CPU_PREEMPTION_LEVEL ; \
222 testl $AST_URGENT,%gs:CPU_PENDING_AST ; \
225 testl $EFL_IF, S_PC ; \
231 call _preemption_underflow_panic ; \
236 #define PREEMPTION_ENABLE \
237 decl %gs:CPU_PREEMPTION_LEVEL ; \
239 testl $AST_URGENT,%gs:CPU_PENDING_AST ; \
242 testl $EFL_IF, S_PC ; \
255 .globl _lockstat_probe
256 .globl _lockstat_probemap
259 * LOCKSTAT_LABEL creates a dtrace symbol which contains
260 * a pointer into the lock code function body. At that
261 * point is a "ret" instruction that can be patched into
265 #if defined(__i386__)
267 #define LOCKSTAT_LABEL(lab) \
275 #define LOCKSTAT_RECORD(id, lck) \
278 sub $0x38,%esp /* size of dtrace_probe args */ ; \
279 movl _lockstat_probemap + (id * 4),%eax ; \
290 movl lck,4(%esp) /* copy lock pointer to arg 1 */ ; \
293 call *_lockstat_probe ; \
295 /* ret - left to subsequent code, e.g. return values */
297 #elif defined(__x86_64__)
298 #define LOCKSTAT_LABEL(lab) \
306 #define LOCKSTAT_RECORD(id, lck) \
309 movl _lockstat_probemap + (id * 4)(%rip),%eax ; \
318 call *_lockstat_probe(%rip) ; \
320 /* ret - left to subsequent code, e.g. return values */
322 #error Unsupported architecture
324 #endif /* CONFIG_DTRACE */
327 * For most routines, the hw_lock_t pointer is loaded into a
328 * register initially, and then either a byte or register-sized
329 * word is loaded/stored to the pointer
332 #if defined(__i386__)
333 #define HW_LOCK_REGISTER %edx
334 #define LOAD_HW_LOCK_REGISTER mov L_ARG0, HW_LOCK_REGISTER
335 #define HW_LOCK_THREAD_REGISTER %ecx
336 #define LOAD_HW_LOCK_THREAD_REGISTER mov %gs:CPU_ACTIVE_THREAD, HW_LOCK_THREAD_REGISTER
337 #define HW_LOCK_MOV_WORD movl
338 #define HW_LOCK_EXAM_REGISTER %eax
339 #elif defined(__x86_64__)
340 #define HW_LOCK_REGISTER %rdi
341 #define LOAD_HW_LOCK_REGISTER
342 #define HW_LOCK_THREAD_REGISTER %rcx
343 #define LOAD_HW_LOCK_THREAD_REGISTER mov %gs:CPU_ACTIVE_THREAD, HW_LOCK_THREAD_REGISTER
344 #define HW_LOCK_MOV_WORD movq
345 #define HW_LOCK_EXAM_REGISTER %rax
347 #error Unsupported architecture
351 * void hw_lock_init(hw_lock_t)
353 * Initialize a hardware lock.
355 LEAF_ENTRY(hw_lock_init)
356 LOAD_HW_LOCK_REGISTER /* fetch lock pointer */
357 HW_LOCK_MOV_WORD $0, (HW_LOCK_REGISTER) /* clear the lock */
362 * void hw_lock_byte_init(volatile uint8_t *)
364 * Initialize a hardware byte lock.
366 LEAF_ENTRY(hw_lock_byte_init)
367 LOAD_HW_LOCK_REGISTER /* fetch lock pointer */
368 movb $0, (HW_LOCK_REGISTER) /* clear the lock */
372 * void hw_lock_lock(hw_lock_t)
374 * Acquire lock, spinning until it becomes available.
375 * MACH_RT: also return with preemption disabled.
377 LEAF_ENTRY(hw_lock_lock)
378 LOAD_HW_LOCK_REGISTER /* fetch lock pointer */
379 LOAD_HW_LOCK_THREAD_REGISTER /* get thread pointer */
383 mov (HW_LOCK_REGISTER), HW_LOCK_EXAM_REGISTER
384 test HW_LOCK_EXAM_REGISTER,HW_LOCK_EXAM_REGISTER /* lock locked? */
385 jne 3f /* branch if so */
386 lock; cmpxchg HW_LOCK_THREAD_REGISTER,(HW_LOCK_REGISTER) /* try to acquire the HW lock */
388 movl $1,%eax /* In case this was a timeout call */
389 LEAF_RET /* if yes, then nothing left to do */
391 PAUSE /* pause for hyper-threading */
392 jmp 1b /* try again */
395 * void hw_lock_byte_lock(uint8_t *lock_byte)
397 * Acquire byte sized lock operand, spinning until it becomes available.
398 * MACH_RT: also return with preemption disabled.
401 LEAF_ENTRY(hw_lock_byte_lock)
402 LOAD_HW_LOCK_REGISTER /* Load lock pointer */
404 movl $1, %ecx /* Set lock value */
406 movb (HW_LOCK_REGISTER), %al /* Load byte at address */
407 testb %al,%al /* lock locked? */
408 jne 3f /* branch if so */
409 lock; cmpxchg %cl,(HW_LOCK_REGISTER) /* attempt atomic compare exchange */
411 LEAF_RET /* if yes, then nothing left to do */
413 PAUSE /* pause for hyper-threading */
414 jmp 1b /* try again */
417 * unsigned int hw_lock_to(hw_lock_t, unsigned int)
419 * Acquire lock, spinning until it becomes available or timeout.
420 * MACH_RT: also return with preemption disabled.
422 LEAF_ENTRY(hw_lock_to)
424 LOAD_HW_LOCK_REGISTER /* fetch lock pointer */
425 LOAD_HW_LOCK_THREAD_REGISTER
428 * Attempt to grab the lock immediately
429 * - fastpath without timeout nonsense.
433 mov (HW_LOCK_REGISTER), HW_LOCK_EXAM_REGISTER
434 test HW_LOCK_EXAM_REGISTER,HW_LOCK_EXAM_REGISTER /* lock locked? */
435 jne 2f /* branch if so */
436 lock; cmpxchg HW_LOCK_THREAD_REGISTER,(HW_LOCK_REGISTER) /* try to acquire the HW lock */
437 jne 2f /* branch on failure */
442 #define INNER_LOOP_COUNT 1000
444 * Failed to get the lock so set the timeout
445 * and then spin re-checking the lock but pausing
446 * every so many (INNER_LOOP_COUNT) spins to check for timeout.
449 movl L_ARG1,%ecx /* fetch timeout */
455 rdtsc /* read cyclecount into %edx:%eax */
456 addl %ecx,%eax /* fetch and timeout */
457 adcl $0,%edx /* add carry */
459 mov %eax,%ebx /* %ecx:%ebx is the timeout expiry */
460 mov %edi, %edx /* load lock back into %edx */
464 rdtsc /* read cyclecount into %edx:%eax */
466 orq %rdx, %rax /* load 64-bit quantity into %rax */
467 addq %rax, %rsi /* %rsi is the timeout expiry */
472 * The inner-loop spin to look for the lock being freed.
475 mov $(INNER_LOOP_COUNT),%edi
477 mov $(INNER_LOOP_COUNT),%r9
480 PAUSE /* pause for hyper-threading */
481 mov (HW_LOCK_REGISTER),HW_LOCK_EXAM_REGISTER /* spin checking lock value in cache */
482 test HW_LOCK_EXAM_REGISTER,HW_LOCK_EXAM_REGISTER
483 je 6f /* zero => unlocked, try to grab it */
485 decl %edi /* decrement inner loop count */
487 decq %r9 /* decrement inner loop count */
489 jnz 5b /* time to check for timeout? */
492 * Here after spinning INNER_LOOP_COUNT times, check for timeout
495 mov %edx,%edi /* Save %edx */
497 rdtsc /* cyclecount into %edx:%eax */
498 xchg %edx,%edi /* cyclecount into %edi:%eax */
499 cmpl %ecx,%edi /* compare high-order 32-bits */
500 jb 4b /* continue spinning if less, or */
501 cmpl %ebx,%eax /* compare low-order 32-bits */
502 jb 4b /* continue if less, else bail */
503 xor %eax,%eax /* with 0 return value */
508 rdtsc /* cyclecount into %edx:%eax */
510 orq %rdx, %rax /* load 64-bit quantity into %rax */
511 cmpq %rsi, %rax /* compare to timeout */
512 jb 4b /* continue spinning if less, or */
513 xor %rax,%rax /* with 0 return value */
520 * Here to try to grab the lock that now appears to be free
523 LOAD_HW_LOCK_THREAD_REGISTER
524 lock; cmpxchg HW_LOCK_THREAD_REGISTER,(HW_LOCK_REGISTER) /* try to acquire the HW lock */
525 jne 4b /* no - spin again */
526 movl $1,%eax /* yes */
536 * void hw_lock_unlock(hw_lock_t)
538 * Unconditionally release lock.
539 * MACH_RT: release preemption level.
541 LEAF_ENTRY(hw_lock_unlock)
542 LOAD_HW_LOCK_REGISTER /* fetch lock pointer */
543 HW_LOCK_MOV_WORD $0, (HW_LOCK_REGISTER) /* clear the lock */
548 * void hw_lock_byte_unlock(uint8_t *lock_byte)
550 * Unconditionally release byte sized lock operand.
551 * MACH_RT: release preemption level.
554 LEAF_ENTRY(hw_lock_byte_unlock)
555 LOAD_HW_LOCK_REGISTER /* Load lock pointer */
556 movb $0, (HW_LOCK_REGISTER) /* Clear the lock byte */
561 * unsigned int hw_lock_try(hw_lock_t)
562 * MACH_RT: returns with preemption disabled on success.
564 LEAF_ENTRY(hw_lock_try)
565 LOAD_HW_LOCK_REGISTER /* fetch lock pointer */
566 LOAD_HW_LOCK_THREAD_REGISTER
569 mov (HW_LOCK_REGISTER),HW_LOCK_EXAM_REGISTER
570 test HW_LOCK_EXAM_REGISTER,HW_LOCK_EXAM_REGISTER
572 lock; cmpxchg HW_LOCK_THREAD_REGISTER,(HW_LOCK_REGISTER) /* try to acquire the HW lock */
575 movl $1,%eax /* success */
579 PREEMPTION_ENABLE /* failure: release preemption... */
580 xorl %eax,%eax /* ...and return failure */
584 * unsigned int hw_lock_held(hw_lock_t)
585 * MACH_RT: doesn't change preemption state.
586 * N.B. Racy, of course.
588 LEAF_ENTRY(hw_lock_held)
589 LOAD_HW_LOCK_REGISTER /* fetch lock pointer */
590 mov (HW_LOCK_REGISTER),HW_LOCK_EXAM_REGISTER /* check lock value */
591 test HW_LOCK_EXAM_REGISTER,HW_LOCK_EXAM_REGISTER
593 cmovne %ecx,%eax /* 0 => unlocked, 1 => locked */
598 * Reader-writer lock fastpaths. These currently exist for the
599 * shared lock acquire, the exclusive lock acquire, the shared to
600 * exclusive upgrade and the release paths (where they reduce overhead
601 * considerably) -- these are by far the most frequently used routines
603 * The following should reflect the layout of the bitfield embedded within
604 * the lck_rw_t structure (see i386/locks.h).
606 #define LCK_RW_INTERLOCK (0x1 << 16)
608 #define LCK_RW_PRIV_EXCL (0x1 << 24)
609 #define LCK_RW_WANT_UPGRADE (0x2 << 24)
610 #define LCK_RW_WANT_WRITE (0x4 << 24)
611 #define LCK_R_WAITING (0x8 << 24)
612 #define LCK_W_WAITING (0x10 << 24)
614 #define LCK_RW_SHARED_MASK (0xffff)
617 * For most routines, the lck_rw_t pointer is loaded into a
618 * register initially, and the flags bitfield loaded into another
619 * register and examined
622 #if defined(__i386__)
623 #define LCK_RW_REGISTER %edx
624 #define LOAD_LCK_RW_REGISTER mov S_ARG0, LCK_RW_REGISTER
625 #define LCK_RW_FLAGS_REGISTER %eax
626 #define LOAD_LCK_RW_FLAGS_REGISTER mov (LCK_RW_REGISTER), LCK_RW_FLAGS_REGISTER
627 #elif defined(__x86_64__)
628 #define LCK_RW_REGISTER %rdi
629 #define LOAD_LCK_RW_REGISTER
630 #define LCK_RW_FLAGS_REGISTER %eax
631 #define LOAD_LCK_RW_FLAGS_REGISTER mov (LCK_RW_REGISTER), LCK_RW_FLAGS_REGISTER
633 #error Unsupported architecture
636 #define RW_LOCK_SHARED_MASK (LCK_RW_INTERLOCK | LCK_RW_WANT_UPGRADE | LCK_RW_WANT_WRITE)
638 * void lck_rw_lock_shared(lck_rw_t *)
641 Entry(lck_rw_lock_shared)
644 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield and interlock */
645 testl $(RW_LOCK_SHARED_MASK), %eax /* Eligible for fastpath? */
648 movl %eax, %ecx /* original value in %eax for cmpxchgl */
649 incl %ecx /* Increment reader refcount */
651 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
656 * Dtrace lockstat event: LS_LCK_RW_LOCK_SHARED_ACQUIRE
657 * Implemented by swapping between return and no-op instructions.
658 * See bsd/dev/dtrace/lockstat.c.
660 LOCKSTAT_LABEL(_lck_rw_lock_shared_lockstat_patch_point)
663 Fall thru when patched, counting on lock pointer in LCK_RW_REGISTER
665 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_SHARED_ACQUIRE, LCK_RW_REGISTER)
672 jmp EXT(lck_rw_lock_shared_gen)
676 #define RW_TRY_LOCK_SHARED_MASK (LCK_RW_WANT_UPGRADE | LCK_RW_WANT_WRITE)
678 * void lck_rw_try_lock_shared(lck_rw_t *)
681 Entry(lck_rw_try_lock_shared)
684 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield and interlock */
685 testl $(LCK_RW_INTERLOCK), %eax
687 testl $(RW_TRY_LOCK_SHARED_MASK), %eax
688 jne 3f /* lock is busy */
690 movl %eax, %ecx /* original value in %eax for cmpxchgl */
691 incl %ecx /* Increment reader refcount */
693 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
699 * Dtrace lockstat event: LS_LCK_RW_TRY_LOCK_SHARED_ACQUIRE
700 * Implemented by swapping between return and no-op instructions.
701 * See bsd/dev/dtrace/lockstat.c.
703 LOCKSTAT_LABEL(_lck_rw_try_lock_shared_lockstat_patch_point)
705 /* Fall thru when patched, counting on lock pointer in LCK_RW_REGISTER */
706 LOCKSTAT_RECORD(LS_LCK_RW_TRY_LOCK_SHARED_ACQUIRE, LCK_RW_REGISTER)
708 movl $1, %eax /* return TRUE */
718 #define RW_LOCK_EXCLUSIVE_HELD (LCK_RW_WANT_WRITE | LCK_RW_WANT_UPGRADE)
720 * int lck_rw_grab_shared(lck_rw_t *)
723 Entry(lck_rw_grab_shared)
726 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield and interlock */
727 testl $(LCK_RW_INTERLOCK), %eax
729 testl $(RW_LOCK_EXCLUSIVE_HELD), %eax
732 movl %eax, %ecx /* original value in %eax for cmpxchgl */
733 incl %ecx /* Increment reader refcount */
735 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
738 movl $1, %eax /* return success */
741 testl $(LCK_RW_SHARED_MASK), %eax
743 testl $(LCK_RW_PRIV_EXCL), %eax
746 xorl %eax, %eax /* return failure */
754 #define RW_LOCK_EXCLUSIVE_MASK (LCK_RW_SHARED_MASK | LCK_RW_INTERLOCK | \
755 LCK_RW_WANT_UPGRADE | LCK_RW_WANT_WRITE)
757 * void lck_rw_lock_exclusive(lck_rw_t*)
760 Entry(lck_rw_lock_exclusive)
763 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield, interlock and shared count */
764 testl $(RW_LOCK_EXCLUSIVE_MASK), %eax /* Eligible for fastpath? */
765 jne 3f /* no, go slow */
767 movl %eax, %ecx /* original value in %eax for cmpxchgl */
768 orl $(LCK_RW_WANT_WRITE), %ecx
770 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
775 * Dtrace lockstat event: LS_LCK_RW_LOCK_EXCL_ACQUIRE
776 * Implemented by swapping between return and no-op instructions.
777 * See bsd/dev/dtrace/lockstat.c.
779 LOCKSTAT_LABEL(_lck_rw_lock_exclusive_lockstat_patch_point)
781 /* Fall thru when patched, counting on lock pointer in LCK_RW_REGISTER */
782 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_EXCL_ACQUIRE, LCK_RW_REGISTER)
789 jmp EXT(lck_rw_lock_exclusive_gen)
793 #define RW_TRY_LOCK_EXCLUSIVE_MASK (LCK_RW_SHARED_MASK | LCK_RW_WANT_UPGRADE | LCK_RW_WANT_WRITE)
795 * void lck_rw_try_lock_exclusive(lck_rw_t *)
797 * Tries to get a write lock.
799 * Returns FALSE if the lock is not held on return.
801 Entry(lck_rw_try_lock_exclusive)
804 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield, interlock and shared count */
805 testl $(LCK_RW_INTERLOCK), %eax
807 testl $(RW_TRY_LOCK_EXCLUSIVE_MASK), %eax
808 jne 3f /* can't get it */
810 movl %eax, %ecx /* original value in %eax for cmpxchgl */
811 orl $(LCK_RW_WANT_WRITE), %ecx
813 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
819 * Dtrace lockstat event: LS_LCK_RW_TRY_LOCK_EXCL_ACQUIRE
820 * Implemented by swapping between return and no-op instructions.
821 * See bsd/dev/dtrace/lockstat.c.
823 LOCKSTAT_LABEL(_lck_rw_try_lock_exclusive_lockstat_patch_point)
825 /* Fall thru when patched, counting on lock pointer in LCK_RW_REGISTER */
826 LOCKSTAT_RECORD(LS_LCK_RW_TRY_LOCK_EXCL_ACQUIRE, LCK_RW_REGISTER)
828 movl $1, %eax /* return TRUE */
834 xorl %eax, %eax /* return FALSE */
840 * void lck_rw_lock_shared_to_exclusive(lck_rw_t*)
842 * fastpath can be taken if
843 * the current rw_shared_count == 1
844 * AND the interlock is clear
845 * AND RW_WANT_UPGRADE is not set
847 * note that RW_WANT_WRITE could be set, but will not
848 * be indicative of an exclusive hold since we have
849 * a read count on the lock that we have not yet released
850 * we can blow by that state since the lck_rw_lock_exclusive
851 * function will block until rw_shared_count == 0 and
852 * RW_WANT_UPGRADE is clear... it does this check behind
853 * the interlock which we are also checking for
855 * to make the transition we must be able to atomically
856 * set RW_WANT_UPGRADE and get rid of the read count we hold
858 Entry(lck_rw_lock_shared_to_exclusive)
861 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield, interlock and shared count */
862 testl $(LCK_RW_INTERLOCK), %eax
864 testl $(LCK_RW_WANT_UPGRADE), %eax
867 movl %eax, %ecx /* original value in %eax for cmpxchgl */
868 orl $(LCK_RW_WANT_UPGRADE), %ecx /* ask for WANT_UPGRADE */
869 decl %ecx /* and shed our read count */
871 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
873 /* we now own the WANT_UPGRADE */
874 testl $(LCK_RW_SHARED_MASK), %ecx /* check to see if all of the readers are drained */
875 jne 8f /* if not, we need to go wait */
880 * Dtrace lockstat event: LS_LCK_RW_LOCK_SHARED_TO_EXCL_UPGRADE
881 * Implemented by swapping between return and no-op instructions.
882 * See bsd/dev/dtrace/lockstat.c.
884 LOCKSTAT_LABEL(_lck_rw_lock_shared_to_exclusive_lockstat_patch_point)
886 /* Fall thru when patched, counting on lock pointer in LCK_RW_REGISTER */
887 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_SHARED_TO_EXCL_UPGRADE, LCK_RW_REGISTER)
889 movl $1, %eax /* return success */
892 2: /* someone else already holds WANT_UPGRADE */
893 movl %eax, %ecx /* original value in %eax for cmpxchgl */
894 decl %ecx /* shed our read count */
895 testl $(LCK_RW_SHARED_MASK), %ecx
896 jne 3f /* we were the last reader */
897 andl $(~LCK_W_WAITING), %ecx /* so clear the wait indicator */
900 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
904 pushl %eax /* go check to see if we need to */
905 push %edx /* wakeup anyone */
906 call EXT(lck_rw_lock_shared_to_exclusive_failure)
909 mov %eax, %esi /* put old flags as second arg */
910 /* lock is alread in %rdi */
911 call EXT(lck_rw_lock_shared_to_exclusive_failure)
913 ret /* and pass the failure return along */
918 jmp EXT(lck_rw_lock_shared_to_exclusive_success)
923 rwl_release_error_str:
924 .asciz "Releasing non-exclusive RW lock without a reader refcount!"
928 * lck_rw_type_t lck_rw_done(lck_rw_t *)
934 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield, interlock and reader count */
935 testl $(LCK_RW_INTERLOCK), %eax
936 jne 7f /* wait for interlock to clear */
938 movl %eax, %ecx /* keep original value in %eax for cmpxchgl */
939 testl $(LCK_RW_SHARED_MASK), %ecx /* if reader count == 0, must be exclusive lock */
941 decl %ecx /* Decrement reader count */
942 testl $(LCK_RW_SHARED_MASK), %ecx /* if reader count has now gone to 0, check for waiters */
946 testl $(LCK_RW_WANT_UPGRADE), %ecx
948 andl $(~LCK_RW_WANT_UPGRADE), %ecx
951 testl $(LCK_RW_WANT_WRITE), %ecx
952 je 8f /* lock is not 'owned', go panic */
953 andl $(~LCK_RW_WANT_WRITE), %ecx
956 * test the original values to match what
957 * lck_rw_done_gen is going to do to determine
958 * which wakeups need to happen...
960 * if !(fake_lck->lck_rw_priv_excl && fake_lck->lck_w_waiting)
962 testl $(LCK_W_WAITING), %eax
964 andl $(~LCK_W_WAITING), %ecx
966 testl $(LCK_RW_PRIV_EXCL), %eax
969 andl $(~LCK_R_WAITING), %ecx
972 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
978 call EXT(lck_rw_done_gen)
981 mov %eax,%esi /* old flags in %rsi */
982 /* lock is in %rdi already */
983 call EXT(lck_rw_done_gen)
991 LOAD_STRING_ARG0(rwl_release_error_str)
997 * lck_rw_type_t lck_rw_lock_exclusive_to_shared(lck_rw_t *)
1000 Entry(lck_rw_lock_exclusive_to_shared)
1001 LOAD_LCK_RW_REGISTER
1003 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield, interlock and reader count */
1004 testl $(LCK_RW_INTERLOCK), %eax
1005 jne 6f /* wait for interlock to clear */
1007 movl %eax, %ecx /* keep original value in %eax for cmpxchgl */
1008 incl %ecx /* Increment reader count */
1010 testl $(LCK_RW_WANT_UPGRADE), %ecx
1012 andl $(~LCK_RW_WANT_UPGRADE), %ecx
1015 andl $(~LCK_RW_WANT_WRITE), %ecx
1018 * test the original values to match what
1019 * lck_rw_lock_exclusive_to_shared_gen is going to do to determine
1020 * which wakeups need to happen...
1022 * if !(fake_lck->lck_rw_priv_excl && fake_lck->lck_w_waiting)
1024 testl $(LCK_W_WAITING), %eax
1026 testl $(LCK_RW_PRIV_EXCL), %eax
1029 andl $(~LCK_R_WAITING), %ecx
1032 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
1038 call EXT(lck_rw_lock_exclusive_to_shared_gen)
1042 call EXT(lck_rw_lock_exclusive_to_shared_gen)
1052 * int lck_rw_grab_want(lck_rw_t *)
1055 Entry(lck_rw_grab_want)
1056 LOAD_LCK_RW_REGISTER
1058 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield, interlock and reader count */
1059 testl $(LCK_RW_INTERLOCK), %eax
1060 jne 3f /* wait for interlock to clear */
1061 testl $(LCK_RW_WANT_WRITE), %eax /* want_write has been grabbed by someone else */
1062 jne 2f /* go return failure */
1064 movl %eax, %ecx /* original value in %eax for cmpxchgl */
1065 orl $(LCK_RW_WANT_WRITE), %ecx
1067 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
1069 /* we now own want_write */
1070 movl $1, %eax /* return success */
1073 xorl %eax, %eax /* return failure */
1080 #define RW_LOCK_SHARED_OR_UPGRADE_MASK (LCK_RW_SHARED_MASK | LCK_RW_INTERLOCK | LCK_RW_WANT_UPGRADE)
1082 * int lck_rw_held_read_or_upgrade(lck_rw_t *)
1085 Entry(lck_rw_held_read_or_upgrade)
1086 LOAD_LCK_RW_REGISTER
1087 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield, interlock and reader count */
1088 andl $(RW_LOCK_SHARED_OR_UPGRADE_MASK), %eax
1094 * N.B.: On x86, statistics are currently recorded for all indirect mutexes.
1095 * Also, only the acquire attempt count (GRP_MTX_STAT_UTIL) is maintained
1096 * as a 64-bit quantity (this matches the existing PowerPC implementation,
1097 * and the new x86 specific statistics are also maintained as 32-bit
1101 * Enable this preprocessor define to record the first miss alone
1102 * By default, we count every miss, hence multiple misses may be
1103 * recorded for a single lock acquire attempt via lck_mtx_lock
1105 #undef LOG_FIRST_MISS_ALONE
1108 * This preprocessor define controls whether the R-M-W update of the
1109 * per-group statistics elements are atomic (LOCK-prefixed)
1110 * Enabled by default.
1112 #define ATOMIC_STAT_UPDATES 1
1114 #if defined(ATOMIC_STAT_UPDATES)
1115 #define LOCK_IF_ATOMIC_STAT_UPDATES lock
1117 #define LOCK_IF_ATOMIC_STAT_UPDATES
1118 #endif /* ATOMIC_STAT_UPDATES */
1122 * For most routines, the lck_mtx_t pointer is loaded into a
1123 * register initially, and the owner field checked for indirection.
1124 * Eventually the lock owner is loaded into a register and examined.
1127 #define M_OWNER MUTEX_OWNER
1128 #define M_PTR MUTEX_PTR
1129 #define M_STATE MUTEX_STATE
1131 #if defined(__i386__)
1133 #define LMTX_ARG0 B_ARG0
1134 #define LMTX_ARG1 B_ARG1
1135 #define LMTX_REG %edx
1136 #define LMTX_A_REG %eax
1137 #define LMTX_A_REG32 %eax
1138 #define LMTX_C_REG %ecx
1139 #define LMTX_C_REG32 %ecx
1140 #define LMTX_RET_REG %eax
1141 #define LMTX_RET_REG32 %eax
1142 #define LMTX_LGROUP_REG %esi
1143 #define LMTX_SSTATE_REG %edi
1144 #define LOAD_LMTX_REG(arg) mov arg, LMTX_REG
1145 #define LMTX_CHK_EXTENDED cmp LMTX_REG, LMTX_ARG0
1146 #define LMTX_ASSERT_OWNED cmpl $(MUTEX_ASSERT_OWNED), LMTX_ARG1
1148 #define LMTX_ENTER_EXTENDED \
1149 mov M_PTR(LMTX_REG), LMTX_REG ; \
1150 push LMTX_LGROUP_REG ; \
1151 push LMTX_SSTATE_REG ; \
1152 xor LMTX_SSTATE_REG, LMTX_SSTATE_REG ; \
1153 mov MUTEX_GRP(LMTX_REG), LMTX_LGROUP_REG ; \
1154 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1155 addl $1, GRP_MTX_STAT_UTIL(LMTX_LGROUP_REG) ; \
1157 incl GRP_MTX_STAT_UTIL+4(LMTX_LGROUP_REG) ; \
1160 #define LMTX_EXIT_EXTENDED \
1161 pop LMTX_SSTATE_REG ; \
1165 #define LMTX_CHK_EXTENDED_EXIT \
1166 cmp LMTX_REG, LMTX_ARG0 ; \
1168 pop LMTX_SSTATE_REG ; \
1169 pop LMTX_LGROUP_REG ; \
1173 #if LOG_FIRST_MISS_ALONE
1174 #define LMTX_UPDATE_MISS \
1175 test $1, LMTX_SSTATE_REG ; \
1177 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1178 incl GRP_MTX_STAT_MISS(LMTX_LGROUP_REG) ; \
1179 or $1, LMTX_SSTATE_REG ; \
1182 #define LMTX_UPDATE_MISS \
1183 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1184 incl GRP_MTX_STAT_MISS(LMTX_LGROUP_REG)
1188 #if LOG_FIRST_MISS_ALONE
1189 #define LMTX_UPDATE_WAIT \
1190 test $2, LMTX_SSTATE_REG ; \
1192 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1193 incl GRP_MTX_STAT_WAIT(LMTX_LGROUP_REG) ; \
1194 or $2, LMTX_SSTATE_REG ; \
1197 #define LMTX_UPDATE_WAIT \
1198 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1199 incl GRP_MTX_STAT_WAIT(LMTX_LGROUP_REG)
1204 * Record the "direct wait" statistic, which indicates if a
1205 * miss proceeded to block directly without spinning--occurs
1206 * if the owner of the mutex isn't running on another processor
1207 * at the time of the check.
1209 #define LMTX_UPDATE_DIRECT_WAIT \
1210 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1211 incl GRP_MTX_STAT_DIRECT_WAIT(LMTX_LGROUP_REG)
1214 #define LMTX_CALLEXT1(func_name) \
1217 call EXT(func_name) ; \
1221 #define LMTX_CALLEXT2(func_name, reg) \
1225 call EXT(func_name) ; \
1229 #elif defined(__x86_64__)
1231 #define LMTX_ARG0 %rdi
1232 #define LMTX_ARG1 %rsi
1233 #define LMTX_REG_ORIG %rdi
1234 #define LMTX_REG %rdx
1235 #define LMTX_A_REG %rax
1236 #define LMTX_A_REG32 %eax
1237 #define LMTX_C_REG %rcx
1238 #define LMTX_C_REG32 %ecx
1239 #define LMTX_RET_REG %rax
1240 #define LMTX_RET_REG32 %eax
1241 #define LMTX_LGROUP_REG %r10
1242 #define LMTX_SSTATE_REG %r11
1243 #define LOAD_LMTX_REG(arg) mov %rdi, %rdx
1244 #define LMTX_CHK_EXTENDED cmp LMTX_REG, LMTX_REG_ORIG
1245 #define LMTX_ASSERT_OWNED cmp $(MUTEX_ASSERT_OWNED), LMTX_ARG1
1247 #define LMTX_ENTER_EXTENDED \
1248 mov M_PTR(LMTX_REG), LMTX_REG ; \
1249 xor LMTX_SSTATE_REG, LMTX_SSTATE_REG ; \
1250 mov MUTEX_GRP(LMTX_REG), LMTX_LGROUP_REG ; \
1251 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1252 incq GRP_MTX_STAT_UTIL(LMTX_LGROUP_REG)
1254 #define LMTX_EXIT_EXTENDED
1256 #define LMTX_CHK_EXTENDED_EXIT
1259 #if LOG_FIRST_MISS_ALONE
1260 #define LMTX_UPDATE_MISS \
1261 test $1, LMTX_SSTATE_REG ; \
1263 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1264 incl GRP_MTX_STAT_MISS(LMTX_LGROUP_REG) ; \
1265 or $1, LMTX_SSTATE_REG ; \
1268 #define LMTX_UPDATE_MISS \
1269 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1270 incl GRP_MTX_STAT_MISS(LMTX_LGROUP_REG)
1274 #if LOG_FIRST_MISS_ALONE
1275 #define LMTX_UPDATE_WAIT \
1276 test $2, LMTX_SSTATE_REG ; \
1278 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1279 incl GRP_MTX_STAT_WAIT(LMTX_LGROUP_REG) ; \
1280 or $2, LMTX_SSTATE_REG ; \
1283 #define LMTX_UPDATE_WAIT \
1284 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1285 incl GRP_MTX_STAT_WAIT(LMTX_LGROUP_REG)
1290 * Record the "direct wait" statistic, which indicates if a
1291 * miss proceeded to block directly without spinning--occurs
1292 * if the owner of the mutex isn't running on another processor
1293 * at the time of the check.
1295 #define LMTX_UPDATE_DIRECT_WAIT \
1296 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1297 incl GRP_MTX_STAT_DIRECT_WAIT(LMTX_LGROUP_REG)
1300 #define LMTX_CALLEXT1(func_name) \
1301 LMTX_CHK_EXTENDED ; \
1303 push LMTX_LGROUP_REG ; \
1304 push LMTX_SSTATE_REG ; \
1305 12: push LMTX_REG_ORIG ; \
1307 mov LMTX_REG, LMTX_ARG0 ; \
1308 call EXT(func_name) ; \
1310 pop LMTX_REG_ORIG ; \
1311 LMTX_CHK_EXTENDED ; \
1313 pop LMTX_SSTATE_REG ; \
1314 pop LMTX_LGROUP_REG ; \
1317 #define LMTX_CALLEXT2(func_name, reg) \
1318 LMTX_CHK_EXTENDED ; \
1320 push LMTX_LGROUP_REG ; \
1321 push LMTX_SSTATE_REG ; \
1322 12: push LMTX_REG_ORIG ; \
1324 mov reg, LMTX_ARG1 ; \
1325 mov LMTX_REG, LMTX_ARG0 ; \
1326 call EXT(func_name) ; \
1328 pop LMTX_REG_ORIG ; \
1329 LMTX_CHK_EXTENDED ; \
1331 pop LMTX_SSTATE_REG ; \
1332 pop LMTX_LGROUP_REG ; \
1336 #error Unsupported architecture
1340 #define M_WAITERS_MSK 0x0000ffff
1341 #define M_PRIORITY_MSK 0x00ff0000
1342 #define M_ILOCKED_MSK 0x01000000
1343 #define M_MLOCKED_MSK 0x02000000
1344 #define M_PROMOTED_MSK 0x04000000
1345 #define M_SPIN_MSK 0x08000000
1348 * void lck_mtx_assert(lck_mtx_t* l, unsigned int)
1349 * Takes the address of a lock, and an assertion type as parameters.
1350 * The assertion can take one of two forms determine by the type
1351 * parameter: either the lock is held by the current thread, and the
1352 * type is LCK_MTX_ASSERT_OWNED, or it isn't and the type is
1353 * LCK_MTX_ASSERT_NOTOWNED. Calls panic on assertion failure.
1357 NONLEAF_ENTRY(lck_mtx_assert)
1358 LOAD_LMTX_REG(B_ARG0) /* Load lock address */
1359 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG /* Load current thread */
1361 mov M_STATE(LMTX_REG), LMTX_C_REG32
1362 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex? */
1364 mov M_PTR(LMTX_REG), LMTX_REG /* If so, take indirection */
1366 mov M_OWNER(LMTX_REG), LMTX_C_REG /* Load owner */
1368 jne 2f /* Assert ownership? */
1369 cmp LMTX_A_REG, LMTX_C_REG /* Current thread match? */
1370 jne 3f /* no, go panic */
1371 testl $(M_ILOCKED_MSK | M_MLOCKED_MSK), M_STATE(LMTX_REG)
1373 1: /* yes, we own it */
1376 cmp LMTX_A_REG, LMTX_C_REG /* Current thread match? */
1377 jne 1b /* No, return */
1379 LOAD_PTR_ARG1(LMTX_REG)
1380 LOAD_STRING_ARG0(mutex_assert_owned_str)
1384 LOAD_PTR_ARG1(LMTX_REG)
1385 LOAD_STRING_ARG0(mutex_assert_not_owned_str)
1392 LOAD_PTR_ARG1(LMTX_REG)
1393 LOAD_STRING_ARG0(mutex_interlock_destroyed_str)
1398 mutex_assert_not_owned_str:
1399 .asciz "mutex (%p) not owned\n"
1400 mutex_assert_owned_str:
1401 .asciz "mutex (%p) owned\n"
1402 mutex_interlock_destroyed_str:
1403 .asciz "trying to interlock destroyed mutex (%p)"
1410 * lck_mtx_try_lock()
1412 * lck_mtx_lock_spin()
1413 * lck_mtx_lock_spin_always()
1414 * lck_mtx_convert_spin()
1416 NONLEAF_ENTRY(lck_mtx_lock_spin_always)
1417 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
1418 jmp Llmls_avoid_check
1420 NONLEAF_ENTRY(lck_mtx_lock_spin)
1421 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
1423 CHECK_PREEMPTION_LEVEL()
1425 mov M_STATE(LMTX_REG), LMTX_C_REG32
1426 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32 /* is the interlock or mutex held */
1428 Llmls_try: /* no - can't be INDIRECT, DESTROYED or locked */
1429 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
1430 or $(M_ILOCKED_MSK | M_SPIN_MSK), LMTX_C_REG32
1434 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
1435 jne Llmls_busy_disabled
1437 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG
1438 mov LMTX_A_REG, M_OWNER(LMTX_REG) /* record owner of interlock */
1440 test LMTX_A_REG, LMTX_A_REG
1442 incl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
1444 #endif /* MACH_LDEBUG */
1446 LMTX_CHK_EXTENDED_EXIT
1447 /* return with the interlock held and preemption disabled */
1450 LOCKSTAT_LABEL(_lck_mtx_lock_spin_lockstat_patch_point)
1452 /* inherit lock pointer in LMTX_REG above */
1453 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_SPIN_ACQUIRE, LMTX_REG)
1458 test $M_ILOCKED_MSK, LMTX_C_REG32 /* is the interlock held */
1459 jz Llml_contended /* no, must have been the mutex */
1461 cmp $(MUTEX_DESTROYED), LMTX_C_REG32 /* check to see if its marked destroyed */
1462 je lck_mtx_destroyed
1463 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex */
1464 jne Llmls_loop /* no... must be interlocked */
1468 mov M_STATE(LMTX_REG), LMTX_C_REG32
1469 test $(M_SPIN_MSK), LMTX_C_REG32
1472 LMTX_UPDATE_MISS /* M_SPIN_MSK was set, so M_ILOCKED_MSK must also be present */
1475 mov M_STATE(LMTX_REG), LMTX_C_REG32
1477 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32
1479 test $(M_MLOCKED_MSK), LMTX_C_REG32
1480 jnz Llml_contended /* mutex owned by someone else, go contend for it */
1483 Llmls_busy_disabled:
1489 NONLEAF_ENTRY(lck_mtx_lock)
1490 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
1492 CHECK_PREEMPTION_LEVEL()
1494 mov M_STATE(LMTX_REG), LMTX_C_REG32
1495 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32 /* is the interlock or mutex held */
1497 Llml_try: /* no - can't be INDIRECT, DESTROYED or locked */
1498 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
1499 or $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32
1503 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
1504 jne Llml_busy_disabled
1506 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG
1507 mov LMTX_A_REG, M_OWNER(LMTX_REG) /* record owner of mutex */
1509 test LMTX_A_REG, LMTX_A_REG
1511 incl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
1513 #endif /* MACH_LDEBUG */
1515 testl $(M_WAITERS_MSK), M_STATE(LMTX_REG)
1518 LMTX_CALLEXT1(lck_mtx_lock_acquire_x86)
1521 andl $(~M_ILOCKED_MSK), M_STATE(LMTX_REG)
1524 LMTX_CHK_EXTENDED /* is this an extended mutex */
1529 LOCKSTAT_LABEL(_lck_mtx_lock_lockstat_patch_point)
1531 /* inherit lock pointer in LMTX_REG above */
1532 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_ACQUIRE, LMTX_REG)
1539 LOCKSTAT_LABEL(_lck_mtx_lock_ext_lockstat_patch_point)
1541 /* inherit lock pointer in LMTX_REG above */
1542 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_LOCK_ACQUIRE, LMTX_REG)
1548 test $M_ILOCKED_MSK, LMTX_C_REG32 /* is the interlock held */
1549 jz Llml_contended /* no, must have been the mutex */
1551 cmp $(MUTEX_DESTROYED), LMTX_C_REG32 /* check to see if its marked destroyed */
1552 je lck_mtx_destroyed
1553 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex? */
1554 jne Llml_loop /* no... must be interlocked */
1558 mov M_STATE(LMTX_REG), LMTX_C_REG32
1559 test $(M_SPIN_MSK), LMTX_C_REG32
1562 LMTX_UPDATE_MISS /* M_SPIN_MSK was set, so M_ILOCKED_MSK must also be present */
1565 mov M_STATE(LMTX_REG), LMTX_C_REG32
1567 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32
1569 test $(M_MLOCKED_MSK), LMTX_C_REG32
1570 jnz Llml_contended /* mutex owned by someone else, go contend for it */
1579 LMTX_CHK_EXTENDED /* is this an extended mutex */
1583 LMTX_CALLEXT1(lck_mtx_lock_spinwait_x86)
1585 test LMTX_RET_REG, LMTX_RET_REG
1586 jz Llml_acquired /* acquired mutex, interlock held and preemption disabled */
1588 cmp $1, LMTX_RET_REG /* check for direct wait status */
1590 LMTX_CHK_EXTENDED /* is this an extended mutex */
1592 LMTX_UPDATE_DIRECT_WAIT
1594 mov M_STATE(LMTX_REG), LMTX_C_REG32
1595 test $(M_ILOCKED_MSK), LMTX_C_REG32
1598 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
1599 or $(M_ILOCKED_MSK), LMTX_C_REG32 /* try to take the interlock */
1603 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
1606 test $(M_MLOCKED_MSK), LMTX_C_REG32 /* we've got the interlock and */
1608 or $(M_MLOCKED_MSK), LMTX_C_REG32 /* the mutex is free... grab it directly */
1609 mov LMTX_C_REG32, M_STATE(LMTX_REG)
1611 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG
1612 mov LMTX_A_REG, M_OWNER(LMTX_REG) /* record owner of mutex */
1614 test LMTX_A_REG, LMTX_A_REG
1616 incl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
1618 #endif /* MACH_LDEBUG */
1621 testl $(M_WAITERS_MSK), M_STATE(LMTX_REG)
1623 mov M_OWNER(LMTX_REG), LMTX_A_REG
1624 mov TH_WAS_PROMOTED_ON_WAKEUP(LMTX_A_REG), LMTX_A_REG32
1625 test LMTX_A_REG32, LMTX_A_REG32
1628 LMTX_CALLEXT1(lck_mtx_lock_acquire_x86)
1631 3: /* interlock held, mutex busy */
1632 LMTX_CHK_EXTENDED /* is this an extended mutex */
1636 LMTX_CALLEXT1(lck_mtx_lock_wait_x86)
1646 NONLEAF_ENTRY(lck_mtx_try_lock_spin)
1647 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
1649 mov M_STATE(LMTX_REG), LMTX_C_REG32
1650 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32 /* is the interlock or mutex held */
1652 Llmts_try: /* no - can't be INDIRECT, DESTROYED or locked */
1653 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
1654 or $(M_ILOCKED_MSK | M_SPIN_MSK), LMTX_C_REG
1658 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
1659 jne Llmts_busy_disabled
1661 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG
1662 mov LMTX_A_REG, M_OWNER(LMTX_REG) /* record owner of mutex */
1664 test LMTX_A_REG, LMTX_A_REG
1666 incl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
1668 #endif /* MACH_LDEBUG */
1670 LMTX_CHK_EXTENDED_EXIT
1674 mov $1, LMTX_RET_REG /* return success */
1675 LOCKSTAT_LABEL(_lck_mtx_try_lock_spin_lockstat_patch_point)
1677 /* inherit lock pointer in LMTX_REG above */
1678 LOCKSTAT_RECORD(LS_LCK_MTX_TRY_SPIN_LOCK_ACQUIRE, LMTX_REG)
1680 mov $1, LMTX_RET_REG /* return success */
1684 test $(M_ILOCKED_MSK), LMTX_C_REG32 /* is the interlock held */
1685 jz Llmts_fail /* no, must be held as a mutex */
1687 cmp $(MUTEX_DESTROYED), LMTX_C_REG32 /* check to see if its marked destroyed */
1688 je lck_mtx_destroyed
1689 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex? */
1695 mov M_STATE(LMTX_REG), LMTX_C_REG32
1697 test $(M_MLOCKED_MSK | M_SPIN_MSK), LMTX_C_REG32
1699 test $(M_ILOCKED_MSK), LMTX_C_REG32
1703 Llmts_busy_disabled:
1709 NONLEAF_ENTRY(lck_mtx_try_lock)
1710 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
1712 mov M_STATE(LMTX_REG), LMTX_C_REG32
1713 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32 /* is the interlock or mutex held */
1715 Llmt_try: /* no - can't be INDIRECT, DESTROYED or locked */
1716 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
1717 or $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32
1721 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
1722 jne Llmt_busy_disabled
1724 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG
1725 mov LMTX_A_REG, M_OWNER(LMTX_REG) /* record owner of mutex */
1727 test LMTX_A_REG, LMTX_A_REG
1729 incl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
1731 #endif /* MACH_LDEBUG */
1733 LMTX_CHK_EXTENDED_EXIT
1735 test $(M_WAITERS_MSK), LMTX_C_REG32
1738 LMTX_CALLEXT1(lck_mtx_lock_acquire_x86)
1740 andl $(~M_ILOCKED_MSK), M_STATE(LMTX_REG)
1745 mov $1, LMTX_RET_REG /* return success */
1746 /* Dtrace probe: LS_LCK_MTX_TRY_LOCK_ACQUIRE */
1747 LOCKSTAT_LABEL(_lck_mtx_try_lock_lockstat_patch_point)
1749 /* inherit lock pointer in LMTX_REG from above */
1750 LOCKSTAT_RECORD(LS_LCK_MTX_TRY_LOCK_ACQUIRE, LMTX_REG)
1752 mov $1, LMTX_RET_REG /* return success */
1756 test $(M_ILOCKED_MSK), LMTX_C_REG32 /* is the interlock held */
1757 jz Llmt_fail /* no, must be held as a mutex */
1759 cmp $(MUTEX_DESTROYED), LMTX_C_REG32 /* check to see if its marked destroyed */
1760 je lck_mtx_destroyed
1761 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex? */
1767 mov M_STATE(LMTX_REG), LMTX_C_REG32
1769 test $(M_MLOCKED_MSK | M_SPIN_MSK), LMTX_C_REG32
1771 test $(M_ILOCKED_MSK), LMTX_C_REG32
1782 LMTX_CHK_EXTENDED /* is this an extended mutex */
1787 xor LMTX_RET_REG, LMTX_RET_REG
1792 NONLEAF_ENTRY(lck_mtx_convert_spin)
1793 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
1795 mov M_STATE(LMTX_REG), LMTX_C_REG32
1796 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex? */
1798 mov M_PTR(LMTX_REG), LMTX_REG /* If so, take indirection */
1799 mov M_STATE(LMTX_REG), LMTX_C_REG32
1801 test $(M_MLOCKED_MSK), LMTX_C_REG32 /* already owned as a mutex, just return */
1803 test $(M_WAITERS_MSK), LMTX_C_REG32 /* are there any waiters? */
1806 LMTX_CALLEXT1(lck_mtx_lock_acquire_x86)
1807 mov M_STATE(LMTX_REG), LMTX_C_REG32
1809 and $(~(M_ILOCKED_MSK | M_SPIN_MSK)), LMTX_C_REG32 /* convert from spin version to mutex */
1810 or $(M_MLOCKED_MSK), LMTX_C_REG32
1811 mov LMTX_C_REG32, M_STATE(LMTX_REG) /* since I own the interlock, I don't need an atomic update */
1819 #if defined(__i386__)
1820 NONLEAF_ENTRY(lck_mtx_unlock)
1821 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
1822 mov M_OWNER(LMTX_REG), LMTX_A_REG
1823 test LMTX_A_REG, LMTX_A_REG
1827 NONLEAF_ENTRY(lck_mtx_unlock_darwin10)
1829 NONLEAF_ENTRY(lck_mtx_unlock)
1831 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
1833 mov M_STATE(LMTX_REG), LMTX_C_REG32
1835 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex? */
1839 test $(M_MLOCKED_MSK), LMTX_C_REG32 /* check for full mutex */
1842 test $(M_ILOCKED_MSK), LMTX_C_REG /* have to wait for interlock to clear */
1845 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
1846 and $(~M_MLOCKED_MSK), LMTX_C_REG32 /* drop mutex */
1847 or $(M_ILOCKED_MSK), LMTX_C_REG32 /* pick up interlock */
1851 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
1852 jne Llmu_busy_disabled /* branch on failure to spin loop */
1855 xor LMTX_A_REG, LMTX_A_REG
1856 mov LMTX_A_REG, M_OWNER(LMTX_REG)
1857 mov LMTX_C_REG, LMTX_A_REG /* keep original state in %ecx for later evaluation */
1858 and $(~(M_ILOCKED_MSK | M_SPIN_MSK | M_PROMOTED_MSK)), LMTX_A_REG
1860 test $(M_WAITERS_MSK), LMTX_A_REG32
1862 dec LMTX_A_REG32 /* decrement waiter count */
1864 mov LMTX_A_REG32, M_STATE(LMTX_REG) /* since I own the interlock, I don't need an atomic update */
1867 /* perform lock statistics after drop to prevent delay */
1868 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG
1869 test LMTX_A_REG, LMTX_A_REG
1871 decl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
1873 #endif /* MACH_LDEBUG */
1875 test $(M_PROMOTED_MSK | M_WAITERS_MSK), LMTX_C_REG32
1878 LMTX_CALLEXT2(lck_mtx_unlock_wakeup_x86, LMTX_C_REG)
1887 /* Dtrace: LS_LCK_MTX_UNLOCK_RELEASE */
1888 LOCKSTAT_LABEL(_lck_mtx_unlock_lockstat_patch_point)
1890 /* inherit lock pointer in LMTX_REG from above */
1891 LOCKSTAT_RECORD(LS_LCK_MTX_UNLOCK_RELEASE, LMTX_REG)
1897 /* Dtrace: LS_LCK_MTX_EXT_UNLOCK_RELEASE */
1898 LOCKSTAT_LABEL(_lck_mtx_ext_unlock_lockstat_patch_point)
1900 /* inherit lock pointer in LMTX_REG from above */
1901 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_UNLOCK_RELEASE, LMTX_REG)
1910 mov M_STATE(LMTX_REG), LMTX_C_REG32
1914 mov M_PTR(LMTX_REG), LMTX_REG
1915 mov M_OWNER(LMTX_REG), LMTX_A_REG
1916 mov %gs:CPU_ACTIVE_THREAD, LMTX_C_REG
1917 CHECK_UNLOCK(LMTX_C_REG, LMTX_A_REG)
1918 mov M_STATE(LMTX_REG), LMTX_C_REG32
1923 LEAF_ENTRY(lck_mtx_ilk_unlock)
1924 LOAD_LMTX_REG(L_ARG0) /* fetch lock pointer - no indirection here */
1926 andl $(~M_ILOCKED_MSK), M_STATE(LMTX_REG)
1928 PREEMPTION_ENABLE /* need to re-enable preemption */
1934 LEAF_ENTRY(lck_mtx_lock_grab_mutex)
1935 LOAD_LMTX_REG(L_ARG0) /* fetch lock pointer - no indirection here */
1937 mov M_STATE(LMTX_REG), LMTX_C_REG32
1939 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32 /* can't have the mutex yet */
1942 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
1943 or $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32
1947 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
1948 jne 2f /* branch on failure to spin loop */
1950 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG
1951 mov LMTX_A_REG, M_OWNER(LMTX_REG) /* record owner of mutex */
1953 test LMTX_A_REG, LMTX_A_REG
1955 incl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
1957 #endif /* MACH_LDEBUG */
1959 mov $1, LMTX_RET_REG /* return success */
1964 xor LMTX_RET_REG, LMTX_RET_REG /* return failure */
1969 LEAF_ENTRY(lck_mtx_lock_mark_destroyed)
1970 LOAD_LMTX_REG(L_ARG0)
1972 mov M_STATE(LMTX_REG), LMTX_C_REG32
1973 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex? */
1976 movl $(MUTEX_DESTROYED), M_STATE(LMTX_REG) /* convert to destroyed state */
1979 test $(M_ILOCKED_MSK), LMTX_C_REG /* have to wait for interlock to clear */
1983 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
1984 or $(M_ILOCKED_MSK), LMTX_C_REG32
1986 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
1987 jne 4f /* branch on failure to spin loop */
1988 movl $(MUTEX_DESTROYED), M_STATE(LMTX_REG) /* convert to destroyed state */
1991 LEAF_RET /* return with M_ILOCKED set */
1998 LEAF_ENTRY(preemption_underflow_panic)
2000 incl %gs:CPU_PREEMPTION_LEVEL
2002 LOAD_STRING_ARG0(16f)
2006 16: String "Preemption level underflow, possible cause unlocking an unlocked mutex or spinlock"
2010 LEAF_ENTRY(_disable_preemption)
2013 #endif /* MACH_RT */
2016 LEAF_ENTRY(_enable_preemption)
2019 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
2022 pushl %gs:CPU_PREEMPTION_LEVEL
2024 movl %gs:CPU_PREEMPTION_LEVEL,%esi
2027 LOAD_STRING_ARG0(_enable_preemption_less_than_zero)
2031 _enable_preemption_less_than_zero:
2032 .asciz "_enable_preemption: preemption_level(%d) < 0!"
2035 #endif /* MACH_ASSERT */
2037 #endif /* MACH_RT */
2040 LEAF_ENTRY(_enable_preemption_no_check)
2043 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
2046 LOAD_STRING_ARG0(_enable_preemption_no_check_less_than_zero)
2050 _enable_preemption_no_check_less_than_zero:
2051 .asciz "_enable_preemption_no_check: preemption_level <= 0!"
2054 #endif /* MACH_ASSERT */
2055 _ENABLE_PREEMPTION_NO_CHECK
2056 #endif /* MACH_RT */
2060 LEAF_ENTRY(_mp_disable_preemption)
2063 #endif /* MACH_RT */
2066 LEAF_ENTRY(_mp_enable_preemption)
2069 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
2072 pushl %gs:CPU_PREEMPTION_LEVEL
2074 movl %gs:CPU_PREEMPTION_LEVEL,%esi
2077 LOAD_STRING_ARG0(_mp_enable_preemption_less_than_zero)
2081 _mp_enable_preemption_less_than_zero:
2082 .asciz "_mp_enable_preemption: preemption_level (%d) <= 0!"
2085 #endif /* MACH_ASSERT */
2087 #endif /* MACH_RT */
2090 LEAF_ENTRY(_mp_enable_preemption_no_check)
2093 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
2096 LOAD_STRING_ARG0(_mp_enable_preemption_no_check_less_than_zero)
2100 _mp_enable_preemption_no_check_less_than_zero:
2101 .asciz "_mp_enable_preemption_no_check: preemption_level <= 0!"
2104 #endif /* MACH_ASSERT */
2105 _ENABLE_PREEMPTION_NO_CHECK
2106 #endif /* MACH_RT */
2111 LEAF_ENTRY(i_bit_set)
2118 LEAF_ENTRY(i_bit_clear)
2126 LEAF_ENTRY(bit_lock)
2136 LEAF_ENTRY(bit_lock_try)
2142 LEAF_RET /* %eax better not be null ! */
2147 LEAF_ENTRY(bit_unlock)
2155 * Atomic primitives, prototyped in kern/simple_lock.h
2157 LEAF_ENTRY(hw_atomic_add)
2158 movl L_ARG0, %ecx /* Load address of operand */
2159 movl L_ARG1, %eax /* Load addend */
2162 xaddl %eax, (%ecx) /* Atomic exchange and add */
2163 addl %edx, %eax /* Calculate result */
2166 LEAF_ENTRY(hw_atomic_sub)
2167 movl L_ARG0, %ecx /* Load address of operand */
2168 movl L_ARG1, %eax /* Load subtrahend */
2172 xaddl %eax, (%ecx) /* Atomic exchange and add */
2173 addl %edx, %eax /* Calculate result */
2176 LEAF_ENTRY(hw_atomic_or)
2177 movl L_ARG0, %ecx /* Load address of operand */
2180 movl L_ARG1, %edx /* Load mask */
2183 cmpxchgl %edx, (%ecx) /* Atomic CAS */
2185 movl %edx, %eax /* Result */
2188 * A variant of hw_atomic_or which doesn't return a value.
2189 * The implementation is thus comparatively more efficient.
2192 LEAF_ENTRY(hw_atomic_or_noret)
2193 movl L_ARG0, %ecx /* Load address of operand */
2194 movl L_ARG1, %edx /* Load mask */
2196 orl %edx, (%ecx) /* Atomic OR */
2199 LEAF_ENTRY(hw_atomic_and)
2200 movl L_ARG0, %ecx /* Load address of operand */
2203 movl L_ARG1, %edx /* Load mask */
2206 cmpxchgl %edx, (%ecx) /* Atomic CAS */
2208 movl %edx, %eax /* Result */
2211 * A variant of hw_atomic_and which doesn't return a value.
2212 * The implementation is thus comparatively more efficient.
2215 LEAF_ENTRY(hw_atomic_and_noret)
2216 movl L_ARG0, %ecx /* Load address of operand */
2217 movl L_ARG1, %edx /* Load mask */
2219 andl %edx, (%ecx) /* Atomic AND */
2222 #else /* !__i386__ */
2224 LEAF_ENTRY(i_bit_set)
2229 LEAF_ENTRY(i_bit_clear)
2235 LEAF_ENTRY(bit_lock)
2243 LEAF_ENTRY(bit_lock_try)
2253 LEAF_ENTRY(bit_unlock)
2260 * Atomic primitives, prototyped in kern/simple_lock.h
2262 LEAF_ENTRY(hw_atomic_add)
2269 movl %esi, %eax /* Load addend */
2270 lock xaddl %eax, (%rdi) /* Atomic exchange and add */
2271 addl %esi, %eax /* Calculate result */
2274 LEAF_ENTRY(hw_atomic_sub)
2283 lock xaddl %eax, (%rdi) /* Atomic exchange and add */
2284 addl %esi, %eax /* Calculate result */
2287 LEAF_ENTRY(hw_atomic_or)
2296 movl %esi, %edx /* Load mask */
2298 lock cmpxchgl %edx, (%rdi) /* Atomic CAS */
2300 movl %edx, %eax /* Result */
2303 * A variant of hw_atomic_or which doesn't return a value.
2304 * The implementation is thus comparatively more efficient.
2307 LEAF_ENTRY(hw_atomic_or_noret)
2315 orl %esi, (%rdi) /* Atomic OR */
2319 LEAF_ENTRY(hw_atomic_and)
2328 movl %esi, %edx /* Load mask */
2330 lock cmpxchgl %edx, (%rdi) /* Atomic CAS */
2332 movl %edx, %eax /* Result */
2335 * A variant of hw_atomic_and which doesn't return a value.
2336 * The implementation is thus comparatively more efficient.
2339 LEAF_ENTRY(hw_atomic_and_noret)
2346 lock andl %esi, (%rdi) /* Atomic OR */
2349 #endif /* !__i386 __ */