2 * Copyright (c) 2000-2012 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 <mach_ldebug.h>
41 #include <i386/eflags.h>
42 #include <i386/trap.h>
43 #include <config_dtrace.h>
48 #define PAUSE rep; nop
50 #include <i386/pal_lock_asm.h>
52 #define LEAF_ENTRY(name) \
55 #define LEAF_ENTRY2(n1,n2) \
62 /* Non-leaf routines always have a stack frame: */
64 #define NONLEAF_ENTRY(name) \
68 #define NONLEAF_ENTRY2(n1,n2) \
78 /* For x86_64, the varargs ABI requires that %al indicate
79 * how many SSE register contain arguments. In our case, 0 */
80 #define ALIGN_STACK() and $0xFFFFFFFFFFFFFFF0, %rsp ;
81 #define LOAD_STRING_ARG0(label) leaq label(%rip), %rdi ;
82 #define LOAD_ARG1(x) mov x, %esi ;
83 #define LOAD_PTR_ARG1(x) mov x, %rsi ;
84 #define CALL_PANIC() xorb %al,%al ; call EXT(panic) ;
86 #define CHECK_UNLOCK(current, owner) \
87 cmp current, owner ; \
90 LOAD_STRING_ARG0(2f) ; \
94 2: String "Mutex unlock attempted from non-owner thread"; \
100 * Routines for general lock debugging.
104 * Checks for expected lock types and calls "panic" on
105 * mismatch. Detects calls to Mutex functions with
106 * type simplelock and vice versa.
108 #define CHECK_MUTEX_TYPE() \
109 cmpl $ MUTEX_TAG,M_TYPE ; \
112 LOAD_STRING_ARG0(2f) ; \
116 2: String "not a mutex!" ; \
121 * If one or more simplelocks are currently held by a thread,
122 * an attempt to acquire a mutex will cause this check to fail
123 * (since a mutex lock may context switch, holding a simplelock
124 * is not a good thing).
127 #define CHECK_PREEMPTION_LEVEL() \
128 cmpl $0,%gs:CPU_HIBERNATE ; \
130 cmpl $0,%gs:CPU_PREEMPTION_LEVEL ; \
133 movl %gs:CPU_PREEMPTION_LEVEL, %eax ; \
135 LOAD_STRING_ARG0(2f) ; \
139 2: String "preemption_level(%d) != 0!" ; \
143 #define CHECK_PREEMPTION_LEVEL()
146 #define CHECK_MYLOCK(current, owner) \
147 cmp current, owner ; \
150 LOAD_STRING_ARG0(2f) ; \
154 2: String "Attempt to recursively lock a non-recursive lock"; \
158 #else /* MACH_LDEBUG */
159 #define CHECK_MUTEX_TYPE()
160 #define CHECK_PREEMPTION_LEVEL()
161 #define CHECK_MYLOCK(thd)
162 #endif /* MACH_LDEBUG */
164 #define PREEMPTION_DISABLE \
165 incl %gs:CPU_PREEMPTION_LEVEL
167 #define PREEMPTION_LEVEL_DEBUG 1
168 #if PREEMPTION_LEVEL_DEBUG
169 #define PREEMPTION_ENABLE \
170 decl %gs:CPU_PREEMPTION_LEVEL ; \
173 testl $AST_URGENT,%gs:CPU_PENDING_AST ; \
176 testl $EFL_IF, S_PC ; \
182 call _preemption_underflow_panic ; \
187 #define PREEMPTION_ENABLE \
188 decl %gs:CPU_PREEMPTION_LEVEL ; \
190 testl $AST_URGENT,%gs:CPU_PENDING_AST ; \
193 testl $EFL_IF, S_PC ; \
206 .globl _lockstat_probe
207 .globl _lockstat_probemap
210 * LOCKSTAT_LABEL creates a dtrace symbol which contains
211 * a pointer into the lock code function body. At that
212 * point is a "ret" instruction that can be patched into
216 #define LOCKSTAT_LABEL(lab) \
224 #define LOCKSTAT_RECORD(id, lck) \
227 movl _lockstat_probemap + (id * 4)(%rip),%eax ; \
236 call *_lockstat_probe(%rip) ; \
238 /* ret - left to subsequent code, e.g. return values */
240 #endif /* CONFIG_DTRACE */
243 * For most routines, the hw_lock_t pointer is loaded into a
244 * register initially, and then either a byte or register-sized
245 * word is loaded/stored to the pointer
249 * void hw_lock_byte_init(volatile uint8_t *)
251 * Initialize a hardware byte lock.
253 LEAF_ENTRY(hw_lock_byte_init)
254 movb $0, (%rdi) /* clear the lock */
258 * void hw_lock_byte_lock(uint8_t *lock_byte)
260 * Acquire byte sized lock operand, spinning until it becomes available.
261 * MACH_RT: also return with preemption disabled.
264 LEAF_ENTRY(hw_lock_byte_lock)
266 movl $1, %ecx /* Set lock value */
268 movb (%rdi), %al /* Load byte at address */
269 testb %al,%al /* lock locked? */
270 jne 3f /* branch if so */
271 lock; cmpxchg %cl,(%rdi) /* attempt atomic compare exchange */
273 LEAF_RET /* if yes, then nothing left to do */
275 PAUSE /* pause for hyper-threading */
276 jmp 1b /* try again */
279 * void hw_lock_byte_unlock(uint8_t *lock_byte)
281 * Unconditionally release byte sized lock operand.
282 * MACH_RT: release preemption level.
285 LEAF_ENTRY(hw_lock_byte_unlock)
286 movb $0, (%rdi) /* Clear the lock byte */
291 * Reader-writer lock fastpaths. These currently exist for the
292 * shared lock acquire, the exclusive lock acquire, the shared to
293 * exclusive upgrade and the release paths (where they reduce overhead
294 * considerably) -- these are by far the most frequently used routines
296 * The following should reflect the layout of the bitfield embedded within
297 * the lck_rw_t structure (see i386/locks.h).
299 #define LCK_RW_INTERLOCK (0x1 << 16)
301 #define LCK_RW_PRIV_EXCL (0x1 << 24)
302 #define LCK_RW_WANT_UPGRADE (0x2 << 24)
303 #define LCK_RW_WANT_WRITE (0x4 << 24)
304 #define LCK_R_WAITING (0x8 << 24)
305 #define LCK_W_WAITING (0x10 << 24)
307 #define LCK_RW_SHARED_MASK (0xffff)
310 * For most routines, the lck_rw_t pointer is loaded into a
311 * register initially, and the flags bitfield loaded into another
312 * register and examined
315 #define RW_LOCK_SHARED_MASK (LCK_RW_INTERLOCK | LCK_RW_WANT_UPGRADE | LCK_RW_WANT_WRITE)
317 * void lck_rw_lock_shared(lck_rw_t *)
320 Entry(lck_rw_lock_shared)
321 mov %gs:CPU_ACTIVE_THREAD, %rcx /* Load thread pointer */
322 incl TH_RWLOCK_COUNT(%rcx) /* Increment count before atomic CAS */
324 mov (%rdi), %eax /* Load state bitfield and interlock */
325 testl $(RW_LOCK_SHARED_MASK), %eax /* Eligible for fastpath? */
328 movl %eax, %ecx /* original value in %eax for cmpxchgl */
329 incl %ecx /* Increment reader refcount */
331 cmpxchgl %ecx, (%rdi) /* Attempt atomic exchange */
336 * Dtrace lockstat event: LS_LCK_RW_LOCK_SHARED_ACQUIRE
337 * Implemented by swapping between return and no-op instructions.
338 * See bsd/dev/dtrace/lockstat.c.
340 LOCKSTAT_LABEL(_lck_rw_lock_shared_lockstat_patch_point)
343 Fall thru when patched, counting on lock pointer in %rdi
345 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_SHARED_ACQUIRE, %rdi)
352 jmp EXT(lck_rw_lock_shared_gen)
356 #define RW_TRY_LOCK_SHARED_MASK (LCK_RW_WANT_UPGRADE | LCK_RW_WANT_WRITE)
358 * void lck_rw_try_lock_shared(lck_rw_t *)
361 Entry(lck_rw_try_lock_shared)
363 mov (%rdi), %eax /* Load state bitfield and interlock */
364 testl $(LCK_RW_INTERLOCK), %eax
366 testl $(RW_TRY_LOCK_SHARED_MASK), %eax
367 jne 3f /* lock is busy */
369 movl %eax, %ecx /* original value in %eax for cmpxchgl */
370 incl %ecx /* Increment reader refcount */
372 cmpxchgl %ecx, (%rdi) /* Attempt atomic exchange */
375 mov %gs:CPU_ACTIVE_THREAD, %rcx /* Load thread pointer */
376 incl TH_RWLOCK_COUNT(%rcx) /* Increment count on success. */
377 /* There is a 3 instr window where preemption may not notice rwlock_count after cmpxchg */
382 * Dtrace lockstat event: LS_LCK_RW_TRY_LOCK_SHARED_ACQUIRE
383 * Implemented by swapping between return and no-op instructions.
384 * See bsd/dev/dtrace/lockstat.c.
386 LOCKSTAT_LABEL(_lck_rw_try_lock_shared_lockstat_patch_point)
388 /* Fall thru when patched, counting on lock pointer in %rdi */
389 LOCKSTAT_RECORD(LS_LCK_RW_TRY_LOCK_SHARED_ACQUIRE, %rdi)
391 movl $1, %eax /* return TRUE */
401 #define RW_LOCK_EXCLUSIVE_HELD (LCK_RW_WANT_WRITE | LCK_RW_WANT_UPGRADE)
403 * int lck_rw_grab_shared(lck_rw_t *)
406 Entry(lck_rw_grab_shared)
408 mov (%rdi), %eax /* Load state bitfield and interlock */
409 testl $(LCK_RW_INTERLOCK), %eax
411 testl $(RW_LOCK_EXCLUSIVE_HELD), %eax
414 movl %eax, %ecx /* original value in %eax for cmpxchgl */
415 incl %ecx /* Increment reader refcount */
417 cmpxchgl %ecx, (%rdi) /* Attempt atomic exchange */
420 movl $1, %eax /* return success */
423 testl $(LCK_RW_SHARED_MASK), %eax
425 testl $(LCK_RW_PRIV_EXCL), %eax
428 xorl %eax, %eax /* return failure */
436 #define RW_LOCK_EXCLUSIVE_MASK (LCK_RW_SHARED_MASK | LCK_RW_INTERLOCK | \
437 LCK_RW_WANT_UPGRADE | LCK_RW_WANT_WRITE)
439 * void lck_rw_lock_exclusive(lck_rw_t*)
442 Entry(lck_rw_lock_exclusive)
443 mov %gs:CPU_ACTIVE_THREAD, %rcx /* Load thread pointer */
444 incl TH_RWLOCK_COUNT(%rcx) /* Increment count before atomic CAS */
446 mov (%rdi), %eax /* Load state bitfield, interlock and shared count */
447 testl $(RW_LOCK_EXCLUSIVE_MASK), %eax /* Eligible for fastpath? */
448 jne 3f /* no, go slow */
450 movl %eax, %ecx /* original value in %eax for cmpxchgl */
451 orl $(LCK_RW_WANT_WRITE), %ecx
453 cmpxchgl %ecx, (%rdi) /* Attempt atomic exchange */
458 * Dtrace lockstat event: LS_LCK_RW_LOCK_EXCL_ACQUIRE
459 * Implemented by swapping between return and no-op instructions.
460 * See bsd/dev/dtrace/lockstat.c.
462 LOCKSTAT_LABEL(_lck_rw_lock_exclusive_lockstat_patch_point)
464 /* Fall thru when patched, counting on lock pointer in %rdi */
465 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_EXCL_ACQUIRE, %rdi)
472 jmp EXT(lck_rw_lock_exclusive_gen)
476 #define RW_TRY_LOCK_EXCLUSIVE_MASK (LCK_RW_SHARED_MASK | LCK_RW_WANT_UPGRADE | LCK_RW_WANT_WRITE)
478 * void lck_rw_try_lock_exclusive(lck_rw_t *)
480 * Tries to get a write lock.
482 * Returns FALSE if the lock is not held on return.
484 Entry(lck_rw_try_lock_exclusive)
486 mov (%rdi), %eax /* Load state bitfield, interlock and shared count */
487 testl $(LCK_RW_INTERLOCK), %eax
489 testl $(RW_TRY_LOCK_EXCLUSIVE_MASK), %eax
490 jne 3f /* can't get it */
492 movl %eax, %ecx /* original value in %eax for cmpxchgl */
493 orl $(LCK_RW_WANT_WRITE), %ecx
495 cmpxchgl %ecx, (%rdi) /* Attempt atomic exchange */
498 mov %gs:CPU_ACTIVE_THREAD, %rcx /* Load thread pointer */
499 incl TH_RWLOCK_COUNT(%rcx) /* Increment count on success. */
500 /* There is a 3 instr window where preemption may not notice rwlock_count after cmpxchg */
505 * Dtrace lockstat event: LS_LCK_RW_TRY_LOCK_EXCL_ACQUIRE
506 * Implemented by swapping between return and no-op instructions.
507 * See bsd/dev/dtrace/lockstat.c.
509 LOCKSTAT_LABEL(_lck_rw_try_lock_exclusive_lockstat_patch_point)
511 /* Fall thru when patched, counting on lock pointer in %rdi */
512 LOCKSTAT_RECORD(LS_LCK_RW_TRY_LOCK_EXCL_ACQUIRE, %rdi)
514 movl $1, %eax /* return TRUE */
520 xorl %eax, %eax /* return FALSE */
526 * void lck_rw_lock_shared_to_exclusive(lck_rw_t*)
528 * fastpath can be taken if
529 * the current rw_shared_count == 1
530 * AND the interlock is clear
531 * AND RW_WANT_UPGRADE is not set
533 * note that RW_WANT_WRITE could be set, but will not
534 * be indicative of an exclusive hold since we have
535 * a read count on the lock that we have not yet released
536 * we can blow by that state since the lck_rw_lock_exclusive
537 * function will block until rw_shared_count == 0 and
538 * RW_WANT_UPGRADE is clear... it does this check behind
539 * the interlock which we are also checking for
541 * to make the transition we must be able to atomically
542 * set RW_WANT_UPGRADE and get rid of the read count we hold
544 Entry(lck_rw_lock_shared_to_exclusive)
546 mov (%rdi), %eax /* Load state bitfield, interlock and shared count */
547 testl $(LCK_RW_INTERLOCK), %eax
549 testl $(LCK_RW_WANT_UPGRADE), %eax
552 movl %eax, %ecx /* original value in %eax for cmpxchgl */
553 orl $(LCK_RW_WANT_UPGRADE), %ecx /* ask for WANT_UPGRADE */
554 decl %ecx /* and shed our read count */
556 cmpxchgl %ecx, (%rdi) /* Attempt atomic exchange */
558 /* we now own the WANT_UPGRADE */
559 testl $(LCK_RW_SHARED_MASK), %ecx /* check to see if all of the readers are drained */
560 jne 8f /* if not, we need to go wait */
565 * Dtrace lockstat event: LS_LCK_RW_LOCK_SHARED_TO_EXCL_UPGRADE
566 * Implemented by swapping between return and no-op instructions.
567 * See bsd/dev/dtrace/lockstat.c.
569 LOCKSTAT_LABEL(_lck_rw_lock_shared_to_exclusive_lockstat_patch_point)
571 /* Fall thru when patched, counting on lock pointer in %rdi */
572 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_SHARED_TO_EXCL_UPGRADE, %rdi)
574 movl $1, %eax /* return success */
577 2: /* someone else already holds WANT_UPGRADE */
578 movl %eax, %ecx /* original value in %eax for cmpxchgl */
579 decl %ecx /* shed our read count */
580 testl $(LCK_RW_SHARED_MASK), %ecx
581 jne 3f /* we were the last reader */
582 andl $(~LCK_W_WAITING), %ecx /* so clear the wait indicator */
585 cmpxchgl %ecx, (%rdi) /* Attempt atomic exchange */
588 mov %eax, %esi /* put old flags as second arg */
589 /* lock is alread in %rdi */
590 call EXT(lck_rw_lock_shared_to_exclusive_failure)
591 ret /* and pass the failure return along */
596 jmp EXT(lck_rw_lock_shared_to_exclusive_success)
601 rwl_release_error_str:
602 .asciz "Releasing non-exclusive RW lock without a reader refcount!"
606 * lck_rw_type_t lck_rw_done(lck_rw_t *)
611 mov (%rdi), %eax /* Load state bitfield, interlock and reader count */
612 testl $(LCK_RW_INTERLOCK), %eax
613 jne 7f /* wait for interlock to clear */
615 movl %eax, %ecx /* keep original value in %eax for cmpxchgl */
616 testl $(LCK_RW_SHARED_MASK), %ecx /* if reader count == 0, must be exclusive lock */
618 decl %ecx /* Decrement reader count */
619 testl $(LCK_RW_SHARED_MASK), %ecx /* if reader count has now gone to 0, check for waiters */
623 testl $(LCK_RW_WANT_UPGRADE), %ecx
625 andl $(~LCK_RW_WANT_UPGRADE), %ecx
628 testl $(LCK_RW_WANT_WRITE), %ecx
629 je 8f /* lock is not 'owned', go panic */
630 andl $(~LCK_RW_WANT_WRITE), %ecx
633 * test the original values to match what
634 * lck_rw_done_gen is going to do to determine
635 * which wakeups need to happen...
637 * if !(fake_lck->lck_rw_priv_excl && fake_lck->lck_w_waiting)
639 testl $(LCK_W_WAITING), %eax
641 andl $(~LCK_W_WAITING), %ecx
643 testl $(LCK_RW_PRIV_EXCL), %eax
646 andl $(~LCK_R_WAITING), %ecx
649 cmpxchgl %ecx, (%rdi) /* Attempt atomic exchange */
652 mov %eax,%esi /* old flags in %rsi */
653 /* lock is in %rdi already */
654 call EXT(lck_rw_done_gen)
661 LOAD_STRING_ARG0(rwl_release_error_str)
667 * lck_rw_type_t lck_rw_lock_exclusive_to_shared(lck_rw_t *)
670 Entry(lck_rw_lock_exclusive_to_shared)
672 mov (%rdi), %eax /* Load state bitfield, interlock and reader count */
673 testl $(LCK_RW_INTERLOCK), %eax
674 jne 6f /* wait for interlock to clear */
676 movl %eax, %ecx /* keep original value in %eax for cmpxchgl */
677 incl %ecx /* Increment reader count */
679 testl $(LCK_RW_WANT_UPGRADE), %ecx
681 andl $(~LCK_RW_WANT_UPGRADE), %ecx
684 andl $(~LCK_RW_WANT_WRITE), %ecx
687 * test the original values to match what
688 * lck_rw_lock_exclusive_to_shared_gen is going to do to determine
689 * which wakeups need to happen...
691 * if !(fake_lck->lck_rw_priv_excl && fake_lck->lck_w_waiting)
693 testl $(LCK_W_WAITING), %eax
695 testl $(LCK_RW_PRIV_EXCL), %eax
698 andl $(~LCK_R_WAITING), %ecx
701 cmpxchgl %ecx, (%rdi) /* Attempt atomic exchange */
705 call EXT(lck_rw_lock_exclusive_to_shared_gen)
714 * int lck_rw_grab_want(lck_rw_t *)
717 Entry(lck_rw_grab_want)
719 mov (%rdi), %eax /* Load state bitfield, interlock and reader count */
720 testl $(LCK_RW_INTERLOCK), %eax
721 jne 3f /* wait for interlock to clear */
722 testl $(LCK_RW_WANT_WRITE), %eax /* want_write has been grabbed by someone else */
723 jne 2f /* go return failure */
725 movl %eax, %ecx /* original value in %eax for cmpxchgl */
726 orl $(LCK_RW_WANT_WRITE), %ecx
728 cmpxchgl %ecx, (%rdi) /* Attempt atomic exchange */
730 /* we now own want_write */
731 movl $1, %eax /* return success */
734 xorl %eax, %eax /* return failure */
741 #define RW_LOCK_SHARED_OR_UPGRADE_MASK (LCK_RW_SHARED_MASK | LCK_RW_INTERLOCK | LCK_RW_WANT_UPGRADE)
743 * int lck_rw_held_read_or_upgrade(lck_rw_t *)
746 Entry(lck_rw_held_read_or_upgrade)
748 andl $(RW_LOCK_SHARED_OR_UPGRADE_MASK), %eax
754 * N.B.: On x86, statistics are currently recorded for all indirect mutexes.
755 * Also, only the acquire attempt count (GRP_MTX_STAT_UTIL) is maintained
756 * as a 64-bit quantity (this matches the existing PowerPC implementation,
757 * and the new x86 specific statistics are also maintained as 32-bit
761 * Enable this preprocessor define to record the first miss alone
762 * By default, we count every miss, hence multiple misses may be
763 * recorded for a single lock acquire attempt via lck_mtx_lock
765 #undef LOG_FIRST_MISS_ALONE
768 * This preprocessor define controls whether the R-M-W update of the
769 * per-group statistics elements are atomic (LOCK-prefixed)
770 * Enabled by default.
772 #define ATOMIC_STAT_UPDATES 1
774 #if defined(ATOMIC_STAT_UPDATES)
775 #define LOCK_IF_ATOMIC_STAT_UPDATES lock
777 #define LOCK_IF_ATOMIC_STAT_UPDATES
778 #endif /* ATOMIC_STAT_UPDATES */
782 * For most routines, the lck_mtx_t pointer is loaded into a
783 * register initially, and the owner field checked for indirection.
784 * Eventually the lock owner is loaded into a register and examined.
787 #define M_OWNER MUTEX_OWNER
788 #define M_PTR MUTEX_PTR
789 #define M_STATE MUTEX_STATE
792 #define LMTX_ENTER_EXTENDED \
793 mov M_PTR(%rdx), %rdx ; \
795 mov MUTEX_GRP(%rdx), %r10 ; \
796 LOCK_IF_ATOMIC_STAT_UPDATES ; \
797 incq GRP_MTX_STAT_UTIL(%r10)
800 #if LOG_FIRST_MISS_ALONE
801 #define LMTX_UPDATE_MISS \
804 LOCK_IF_ATOMIC_STAT_UPDATES ; \
805 incl GRP_MTX_STAT_MISS(%r10) ; \
809 #define LMTX_UPDATE_MISS \
810 LOCK_IF_ATOMIC_STAT_UPDATES ; \
811 incl GRP_MTX_STAT_MISS(%r10)
815 #if LOG_FIRST_MISS_ALONE
816 #define LMTX_UPDATE_WAIT \
819 LOCK_IF_ATOMIC_STAT_UPDATES ; \
820 incl GRP_MTX_STAT_WAIT(%r10) ; \
824 #define LMTX_UPDATE_WAIT \
825 LOCK_IF_ATOMIC_STAT_UPDATES ; \
826 incl GRP_MTX_STAT_WAIT(%r10)
831 * Record the "direct wait" statistic, which indicates if a
832 * miss proceeded to block directly without spinning--occurs
833 * if the owner of the mutex isn't running on another processor
834 * at the time of the check.
836 #define LMTX_UPDATE_DIRECT_WAIT \
837 LOCK_IF_ATOMIC_STAT_UPDATES ; \
838 incl GRP_MTX_STAT_DIRECT_WAIT(%r10)
841 #define LMTX_CALLEXT1(func_name) \
849 call EXT(func_name) ; \
858 #define LMTX_CALLEXT2(func_name, reg) \
867 call EXT(func_name) ; \
877 #define M_WAITERS_MSK 0x0000ffff
878 #define M_PRIORITY_MSK 0x00ff0000
879 #define M_ILOCKED_MSK 0x01000000
880 #define M_MLOCKED_MSK 0x02000000
881 #define M_PROMOTED_MSK 0x04000000
882 #define M_SPIN_MSK 0x08000000
885 * void lck_mtx_assert(lck_mtx_t* l, unsigned int)
886 * Takes the address of a lock, and an assertion type as parameters.
887 * The assertion can take one of two forms determine by the type
888 * parameter: either the lock is held by the current thread, and the
889 * type is LCK_MTX_ASSERT_OWNED, or it isn't and the type is
890 * LCK_MTX_ASSERT_NOTOWNED. Calls panic on assertion failure.
894 NONLEAF_ENTRY(lck_mtx_assert)
895 mov %rdi, %rdx /* Load lock address */
896 mov %gs:CPU_ACTIVE_THREAD, %rax /* Load current thread */
898 mov M_STATE(%rdx), %ecx
899 cmp $(MUTEX_IND), %ecx /* Is this an indirect mutex? */
901 mov M_PTR(%rdx), %rdx /* If so, take indirection */
903 mov M_OWNER(%rdx), %rcx /* Load owner */
904 cmp $(MUTEX_ASSERT_OWNED), %rsi
905 jne 2f /* Assert ownership? */
906 cmp %rax, %rcx /* Current thread match? */
907 jne 3f /* no, go panic */
908 testl $(M_ILOCKED_MSK | M_MLOCKED_MSK), M_STATE(%rdx)
910 1: /* yes, we own it */
913 cmp %rax, %rcx /* Current thread match? */
914 jne 1b /* No, return */
917 LOAD_STRING_ARG0(mutex_assert_owned_str)
922 LOAD_STRING_ARG0(mutex_assert_not_owned_str)
930 LOAD_STRING_ARG0(mutex_interlock_destroyed_str)
935 mutex_assert_not_owned_str:
936 .asciz "mutex (%p) not owned\n"
937 mutex_assert_owned_str:
938 .asciz "mutex (%p) owned\n"
939 mutex_interlock_destroyed_str:
940 .asciz "trying to interlock destroyed mutex (%p)"
949 * lck_mtx_lock_spin()
950 * lck_mtx_lock_spin_always()
951 * lck_mtx_try_lock_spin()
952 * lck_mtx_try_lock_spin_always()
953 * lck_mtx_convert_spin()
955 NONLEAF_ENTRY(lck_mtx_lock_spin_always)
956 mov %rdi, %rdx /* fetch lock pointer */
957 jmp Llmls_avoid_check
959 NONLEAF_ENTRY(lck_mtx_lock_spin)
960 mov %rdi, %rdx /* fetch lock pointer */
962 CHECK_PREEMPTION_LEVEL()
964 mov M_STATE(%rdx), %ecx
965 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), %ecx /* is the interlock or mutex held */
967 Llmls_try: /* no - can't be INDIRECT, DESTROYED or locked */
968 mov %rcx, %rax /* eax contains snapshot for cmpxchgl */
969 or $(M_ILOCKED_MSK | M_SPIN_MSK), %ecx
973 cmpxchg %ecx, M_STATE(%rdx) /* atomic compare and exchange */
974 jne Llmls_busy_disabled
976 mov %gs:CPU_ACTIVE_THREAD, %rax
977 mov %rax, M_OWNER(%rdx) /* record owner of interlock */
981 incl TH_MUTEX_COUNT(%rax) /* lock statistic */
983 #endif /* MACH_LDEBUG */
985 /* return with the interlock held and preemption disabled */
988 LOCKSTAT_LABEL(_lck_mtx_lock_spin_lockstat_patch_point)
990 /* inherit lock pointer in %rdx above */
991 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_SPIN_ACQUIRE, %rdx)
996 test $M_ILOCKED_MSK, %ecx /* is the interlock held */
997 jz Llml_contended /* no, must have been the mutex */
999 cmp $(MUTEX_DESTROYED), %ecx /* check to see if its marked destroyed */
1000 je lck_mtx_destroyed
1001 cmp $(MUTEX_IND), %ecx /* Is this an indirect mutex */
1002 jne Llmls_loop /* no... must be interlocked */
1006 mov M_STATE(%rdx), %ecx
1007 test $(M_SPIN_MSK), %ecx
1010 LMTX_UPDATE_MISS /* M_SPIN_MSK was set, so M_ILOCKED_MSK must also be present */
1013 mov M_STATE(%rdx), %ecx
1015 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), %ecx
1017 test $(M_MLOCKED_MSK), %ecx
1018 jnz Llml_contended /* mutex owned by someone else, go contend for it */
1021 Llmls_busy_disabled:
1027 NONLEAF_ENTRY(lck_mtx_lock)
1028 mov %rdi, %rdx /* fetch lock pointer */
1030 CHECK_PREEMPTION_LEVEL()
1032 mov M_STATE(%rdx), %ecx
1033 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), %ecx /* is the interlock or mutex held */
1035 Llml_try: /* no - can't be INDIRECT, DESTROYED or locked */
1036 mov %rcx, %rax /* eax contains snapshot for cmpxchgl */
1037 or $(M_ILOCKED_MSK | M_MLOCKED_MSK), %ecx
1041 cmpxchg %ecx, M_STATE(%rdx) /* atomic compare and exchange */
1042 jne Llml_busy_disabled
1044 mov %gs:CPU_ACTIVE_THREAD, %rax
1045 mov %rax, M_OWNER(%rdx) /* record owner of mutex */
1049 incl TH_MUTEX_COUNT(%rax) /* lock statistic */
1051 #endif /* MACH_LDEBUG */
1053 testl $(M_WAITERS_MSK), M_STATE(%rdx)
1056 LMTX_CALLEXT1(lck_mtx_lock_acquire_x86)
1059 andl $(~M_ILOCKED_MSK), M_STATE(%rdx)
1062 cmp %rdx, %rdi /* is this an extended mutex */
1067 LOCKSTAT_LABEL(_lck_mtx_lock_lockstat_patch_point)
1069 /* inherit lock pointer in %rdx above */
1070 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_ACQUIRE, %rdx)
1076 LOCKSTAT_LABEL(_lck_mtx_lock_ext_lockstat_patch_point)
1078 /* inherit lock pointer in %rdx above */
1079 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_LOCK_ACQUIRE, %rdx)
1085 test $M_ILOCKED_MSK, %ecx /* is the interlock held */
1086 jz Llml_contended /* no, must have been the mutex */
1088 cmp $(MUTEX_DESTROYED), %ecx /* check to see if its marked destroyed */
1089 je lck_mtx_destroyed
1090 cmp $(MUTEX_IND), %ecx /* Is this an indirect mutex? */
1091 jne Llml_loop /* no... must be interlocked */
1095 mov M_STATE(%rdx), %ecx
1096 test $(M_SPIN_MSK), %ecx
1099 LMTX_UPDATE_MISS /* M_SPIN_MSK was set, so M_ILOCKED_MSK must also be present */
1102 mov M_STATE(%rdx), %ecx
1104 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), %ecx
1106 test $(M_MLOCKED_MSK), %ecx
1107 jnz Llml_contended /* mutex owned by someone else, go contend for it */
1116 cmp %rdx, %rdi /* is this an extended mutex */
1120 LMTX_CALLEXT1(lck_mtx_lock_spinwait_x86)
1123 jz Llml_acquired /* acquired mutex, interlock held and preemption disabled */
1125 cmp $1, %rax /* check for direct wait status */
1127 cmp %rdx, %rdi /* is this an extended mutex */
1129 LMTX_UPDATE_DIRECT_WAIT
1131 mov M_STATE(%rdx), %ecx
1132 test $(M_ILOCKED_MSK), %ecx
1135 mov %rcx, %rax /* eax contains snapshot for cmpxchgl */
1136 or $(M_ILOCKED_MSK), %ecx /* try to take the interlock */
1140 cmpxchg %ecx, M_STATE(%rdx) /* atomic compare and exchange */
1143 test $(M_MLOCKED_MSK), %ecx /* we've got the interlock and */
1145 or $(M_MLOCKED_MSK), %ecx /* the mutex is free... grab it directly */
1146 mov %ecx, M_STATE(%rdx)
1148 mov %gs:CPU_ACTIVE_THREAD, %rax
1149 mov %rax, M_OWNER(%rdx) /* record owner of mutex */
1153 incl TH_MUTEX_COUNT(%rax) /* lock statistic */
1155 #endif /* MACH_LDEBUG */
1158 testl $(M_WAITERS_MSK), M_STATE(%rdx)
1160 mov M_OWNER(%rdx), %rax
1161 mov TH_WAS_PROMOTED_ON_WAKEUP(%rax), %eax
1165 LMTX_CALLEXT1(lck_mtx_lock_acquire_x86)
1168 3: /* interlock held, mutex busy */
1169 cmp %rdx, %rdi /* is this an extended mutex */
1173 LMTX_CALLEXT1(lck_mtx_lock_wait_x86)
1182 NONLEAF_ENTRY(lck_mtx_try_lock_spin_always)
1183 mov %rdi, %rdx /* fetch lock pointer */
1184 jmp Llmts_avoid_check
1186 NONLEAF_ENTRY(lck_mtx_try_lock_spin)
1187 mov %rdi, %rdx /* fetch lock pointer */
1190 mov M_STATE(%rdx), %ecx
1191 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), %ecx /* is the interlock or mutex held */
1193 Llmts_try: /* no - can't be INDIRECT, DESTROYED or locked */
1194 mov %rcx, %rax /* eax contains snapshot for cmpxchgl */
1195 or $(M_ILOCKED_MSK | M_SPIN_MSK), %rcx
1199 cmpxchg %ecx, M_STATE(%rdx) /* atomic compare and exchange */
1200 jne Llmts_busy_disabled
1202 mov %gs:CPU_ACTIVE_THREAD, %rax
1203 mov %rax, M_OWNER(%rdx) /* record owner of mutex */
1207 incl TH_MUTEX_COUNT(%rax) /* lock statistic */
1209 #endif /* MACH_LDEBUG */
1214 mov $1, %rax /* return success */
1215 LOCKSTAT_LABEL(_lck_mtx_try_lock_spin_lockstat_patch_point)
1217 /* inherit lock pointer in %rdx above */
1218 LOCKSTAT_RECORD(LS_LCK_MTX_TRY_SPIN_LOCK_ACQUIRE, %rdx)
1220 mov $1, %rax /* return success */
1224 test $(M_ILOCKED_MSK), %ecx /* is the interlock held */
1225 jz Llmts_fail /* no, must be held as a mutex */
1227 cmp $(MUTEX_DESTROYED), %ecx /* check to see if its marked destroyed */
1228 je lck_mtx_destroyed
1229 cmp $(MUTEX_IND), %ecx /* Is this an indirect mutex? */
1235 mov M_STATE(%rdx), %ecx
1237 test $(M_MLOCKED_MSK | M_SPIN_MSK), %ecx
1239 test $(M_ILOCKED_MSK), %ecx
1243 Llmts_busy_disabled:
1249 NONLEAF_ENTRY(lck_mtx_try_lock)
1250 mov %rdi, %rdx /* fetch lock pointer */
1252 mov M_STATE(%rdx), %ecx
1253 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), %ecx /* is the interlock or mutex held */
1255 Llmt_try: /* no - can't be INDIRECT, DESTROYED or locked */
1256 mov %rcx, %rax /* eax contains snapshot for cmpxchgl */
1257 or $(M_ILOCKED_MSK | M_MLOCKED_MSK), %ecx
1261 cmpxchg %ecx, M_STATE(%rdx) /* atomic compare and exchange */
1262 jne Llmt_busy_disabled
1264 mov %gs:CPU_ACTIVE_THREAD, %rax
1265 mov %rax, M_OWNER(%rdx) /* record owner of mutex */
1269 incl TH_MUTEX_COUNT(%rax) /* lock statistic */
1271 #endif /* MACH_LDEBUG */
1273 test $(M_WAITERS_MSK), %ecx
1276 LMTX_CALLEXT1(lck_mtx_lock_acquire_x86)
1278 andl $(~M_ILOCKED_MSK), M_STATE(%rdx)
1283 mov $1, %rax /* return success */
1284 /* Dtrace probe: LS_LCK_MTX_TRY_LOCK_ACQUIRE */
1285 LOCKSTAT_LABEL(_lck_mtx_try_lock_lockstat_patch_point)
1287 /* inherit lock pointer in %rdx from above */
1288 LOCKSTAT_RECORD(LS_LCK_MTX_TRY_LOCK_ACQUIRE, %rdx)
1290 mov $1, %rax /* return success */
1294 test $(M_ILOCKED_MSK), %ecx /* is the interlock held */
1295 jz Llmt_fail /* no, must be held as a mutex */
1297 cmp $(MUTEX_DESTROYED), %ecx /* check to see if its marked destroyed */
1298 je lck_mtx_destroyed
1299 cmp $(MUTEX_IND), %ecx /* Is this an indirect mutex? */
1305 mov M_STATE(%rdx), %ecx
1307 test $(M_MLOCKED_MSK | M_SPIN_MSK), %ecx
1309 test $(M_ILOCKED_MSK), %ecx
1320 cmp %rdx, %rdi /* is this an extended mutex */
1329 NONLEAF_ENTRY(lck_mtx_convert_spin)
1330 mov %rdi, %rdx /* fetch lock pointer */
1332 mov M_STATE(%rdx), %ecx
1333 cmp $(MUTEX_IND), %ecx /* Is this an indirect mutex? */
1335 mov M_PTR(%rdx), %rdx /* If so, take indirection */
1336 mov M_STATE(%rdx), %ecx
1338 test $(M_MLOCKED_MSK), %ecx /* already owned as a mutex, just return */
1340 test $(M_WAITERS_MSK), %ecx /* are there any waiters? */
1343 LMTX_CALLEXT1(lck_mtx_lock_acquire_x86)
1344 mov M_STATE(%rdx), %ecx
1346 and $(~(M_ILOCKED_MSK | M_SPIN_MSK)), %ecx /* convert from spin version to mutex */
1347 or $(M_MLOCKED_MSK), %ecx
1348 mov %ecx, M_STATE(%rdx) /* since I own the interlock, I don't need an atomic update */
1356 NONLEAF_ENTRY(lck_mtx_unlock)
1357 mov %rdi, %rdx /* fetch lock pointer */
1359 mov M_STATE(%rdx), %ecx
1361 cmp $(MUTEX_IND), %ecx /* Is this an indirect mutex? */
1365 test $(M_MLOCKED_MSK), %ecx /* check for full mutex */
1368 test $(M_ILOCKED_MSK), %rcx /* have to wait for interlock to clear */
1371 mov %rcx, %rax /* eax contains snapshot for cmpxchgl */
1372 and $(~M_MLOCKED_MSK), %ecx /* drop mutex */
1373 or $(M_ILOCKED_MSK), %ecx /* pick up interlock */
1377 cmpxchg %ecx, M_STATE(%rdx) /* atomic compare and exchange */
1378 jne Llmu_busy_disabled /* branch on failure to spin loop */
1382 mov %rax, M_OWNER(%rdx)
1383 mov %rcx, %rax /* keep original state in %ecx for later evaluation */
1384 and $(~(M_ILOCKED_MSK | M_SPIN_MSK | M_PROMOTED_MSK)), %rax
1386 test $(M_WAITERS_MSK), %eax
1388 dec %eax /* decrement waiter count */
1390 mov %eax, M_STATE(%rdx) /* since I own the interlock, I don't need an atomic update */
1393 /* perform lock statistics after drop to prevent delay */
1394 mov %gs:CPU_ACTIVE_THREAD, %rax
1397 decl TH_MUTEX_COUNT(%rax) /* lock statistic */
1399 #endif /* MACH_LDEBUG */
1401 test $(M_PROMOTED_MSK | M_WAITERS_MSK), %ecx
1404 LMTX_CALLEXT2(lck_mtx_unlock_wakeup_x86, %rcx)
1413 /* Dtrace: LS_LCK_MTX_UNLOCK_RELEASE */
1414 LOCKSTAT_LABEL(_lck_mtx_unlock_lockstat_patch_point)
1416 /* inherit lock pointer in %rdx from above */
1417 LOCKSTAT_RECORD(LS_LCK_MTX_UNLOCK_RELEASE, %rdx)
1423 /* Dtrace: LS_LCK_MTX_EXT_UNLOCK_RELEASE */
1424 LOCKSTAT_LABEL(_lck_mtx_ext_unlock_lockstat_patch_point)
1426 /* inherit lock pointer in %rdx from above */
1427 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_UNLOCK_RELEASE, %rdx)
1436 mov M_STATE(%rdx), %ecx
1440 mov M_PTR(%rdx), %rdx
1441 mov M_OWNER(%rdx), %rax
1442 mov %gs:CPU_ACTIVE_THREAD, %rcx
1443 CHECK_UNLOCK(%rcx, %rax)
1444 mov M_STATE(%rdx), %ecx
1449 LEAF_ENTRY(lck_mtx_ilk_try_lock)
1450 mov %rdi, %rdx /* fetch lock pointer - no indirection here */
1452 mov M_STATE(%rdx), %ecx
1454 test $(M_ILOCKED_MSK), %ecx /* can't have the interlock yet */
1457 mov %rcx, %rax /* eax contains snapshot for cmpxchgl */
1458 or $(M_ILOCKED_MSK), %ecx
1462 cmpxchg %ecx, M_STATE(%rdx) /* atomic compare and exchange */
1463 jne 2f /* return failure after re-enabling preemption */
1465 mov $1, %rax /* return success with preemption disabled */
1468 PREEMPTION_ENABLE /* need to re-enable preemption */
1470 xor %rax, %rax /* return failure */
1474 LEAF_ENTRY(lck_mtx_ilk_unlock)
1475 mov %rdi, %rdx /* fetch lock pointer - no indirection here */
1477 andl $(~M_ILOCKED_MSK), M_STATE(%rdx)
1479 PREEMPTION_ENABLE /* need to re-enable preemption */
1484 LEAF_ENTRY(lck_mtx_lock_grab_mutex)
1485 mov %rdi, %rdx /* fetch lock pointer - no indirection here */
1487 mov M_STATE(%rdx), %ecx
1489 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), %ecx /* can't have the mutex yet */
1492 mov %rcx, %rax /* eax contains snapshot for cmpxchgl */
1493 or $(M_ILOCKED_MSK | M_MLOCKED_MSK), %ecx
1497 cmpxchg %ecx, M_STATE(%rdx) /* atomic compare and exchange */
1498 jne 2f /* branch on failure to spin loop */
1500 mov %gs:CPU_ACTIVE_THREAD, %rax
1501 mov %rax, M_OWNER(%rdx) /* record owner of mutex */
1505 incl TH_MUTEX_COUNT(%rax) /* lock statistic */
1507 #endif /* MACH_LDEBUG */
1509 mov $1, %rax /* return success */
1514 xor %rax, %rax /* return failure */
1519 LEAF_ENTRY(lck_mtx_lock_mark_destroyed)
1522 mov M_STATE(%rdx), %ecx
1523 cmp $(MUTEX_IND), %ecx /* Is this an indirect mutex? */
1526 movl $(MUTEX_DESTROYED), M_STATE(%rdx) /* convert to destroyed state */
1529 test $(M_ILOCKED_MSK), %rcx /* have to wait for interlock to clear */
1533 mov %rcx, %rax /* eax contains snapshot for cmpxchgl */
1534 or $(M_ILOCKED_MSK), %ecx
1536 cmpxchg %ecx, M_STATE(%rdx) /* atomic compare and exchange */
1537 jne 4f /* branch on failure to spin loop */
1538 movl $(MUTEX_DESTROYED), M_STATE(%rdx) /* convert to destroyed state */
1541 LEAF_RET /* return with M_ILOCKED set */
1548 LEAF_ENTRY(preemption_underflow_panic)
1550 incl %gs:CPU_PREEMPTION_LEVEL
1552 LOAD_STRING_ARG0(16f)
1556 16: String "Preemption level underflow, possible cause unlocking an unlocked mutex or spinlock"