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 || \
58 ((MACH_LDEBUG) && MACH_KDB))
60 #if BUILD_STACK_FRAMES
62 /* Stack-frame-relative: */
67 #define LEAF_ENTRY(name) \
72 #define LEAF_ENTRY2(n1,n2) \
82 #else /* BUILD_STACK_FRAMES */
84 /* Stack-pointer-relative: */
89 #define LEAF_ENTRY(name) \
92 #define LEAF_ENTRY2(n1,n2) \
99 #endif /* BUILD_STACK_FRAMES */
102 /* Non-leaf routines always have a stack frame: */
104 #define NONLEAF_ENTRY(name) \
109 #define NONLEAF_ENTRY2(n1,n2) \
115 #define NONLEAF_RET \
120 /* For x86_64, the varargs ABI requires that %al indicate
121 * how many SSE register contain arguments. In our case, 0 */
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) ;
127 #define CALL_PANIC() call EXT(panic) ;
129 #define ALIGN_STACK() and $0xFFFFFFFFFFFFFFF0, %rsp ;
130 #define LOAD_STRING_ARG0(label) leaq label(%rip), %rdi ;
131 #define LOAD_ARG1(x) mov x, %esi ;
132 #define LOAD_PTR_ARG1(x) mov x, %rsi ;
133 #define CALL_PANIC() xorb %al,%al ; call EXT(panic) ;
136 #define CHECK_UNLOCK(current, owner) \
137 cmp current, owner ; \
140 LOAD_STRING_ARG0(2f) ; \
144 2: String "Mutex unlock attempted from non-owner thread"; \
150 * Routines for general lock debugging.
154 * Checks for expected lock types and calls "panic" on
155 * mismatch. Detects calls to Mutex functions with
156 * type simplelock and vice versa.
158 #define CHECK_MUTEX_TYPE() \
159 cmpl $ MUTEX_TAG,M_TYPE ; \
162 LOAD_STRING_ARG0(2f) ; \
166 2: String "not a mutex!" ; \
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).
177 #define CHECK_PREEMPTION_LEVEL() \
178 cmpl $0,%gs:CPU_HIBERNATE ; \
180 cmpl $0,%gs:CPU_PREEMPTION_LEVEL ; \
183 movl %gs:CPU_PREEMPTION_LEVEL, %eax ; \
185 LOAD_STRING_ARG0(2f) ; \
189 2: String "preemption_level(%d) != 0!" ; \
193 #define CHECK_PREEMPTION_LEVEL()
196 #define CHECK_MYLOCK(current, owner) \
197 cmp current, owner ; \
200 LOAD_STRING_ARG0(2f) ; \
204 2: String "Attempt to recursively lock a non-recursive lock"; \
208 #else /* MACH_LDEBUG */
209 #define CHECK_MUTEX_TYPE()
210 #define CHECK_PREEMPTION_LEVEL()
211 #define CHECK_MYLOCK(thd)
212 #endif /* MACH_LDEBUG */
214 #define PREEMPTION_DISABLE \
215 incl %gs:CPU_PREEMPTION_LEVEL
218 #define PREEMPTION_LEVEL_DEBUG 1
220 #if PREEMPTION_LEVEL_DEBUG
221 #define PREEMPTION_ENABLE \
222 decl %gs:CPU_PREEMPTION_LEVEL ; \
225 testl $AST_URGENT,%gs:CPU_PENDING_AST ; \
228 testl $EFL_IF, S_PC ; \
234 call _preemption_underflow_panic ; \
239 #define PREEMPTION_ENABLE \
240 decl %gs:CPU_PREEMPTION_LEVEL ; \
242 testl $AST_URGENT,%gs:CPU_PENDING_AST ; \
245 testl $EFL_IF, S_PC ; \
258 .globl _lockstat_probe
259 .globl _lockstat_probemap
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
268 #if defined(__i386__)
270 #define LOCKSTAT_LABEL(lab) \
278 #define LOCKSTAT_RECORD(id, lck) \
281 sub $0x38,%esp /* size of dtrace_probe args */ ; \
282 movl _lockstat_probemap + (id * 4),%eax ; \
293 movl lck,4(%esp) /* copy lock pointer to arg 1 */ ; \
296 call *_lockstat_probe ; \
298 /* ret - left to subsequent code, e.g. return values */
300 #elif defined(__x86_64__)
301 #define LOCKSTAT_LABEL(lab) \
309 #define LOCKSTAT_RECORD(id, lck) \
312 movl _lockstat_probemap + (id * 4)(%rip),%eax ; \
321 call *_lockstat_probe(%rip) ; \
323 /* ret - left to subsequent code, e.g. return values */
325 #error Unsupported architecture
327 #endif /* CONFIG_DTRACE */
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
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
350 #error Unsupported architecture
354 * void hw_lock_init(hw_lock_t)
356 * Initialize a hardware lock.
358 LEAF_ENTRY(hw_lock_init)
359 LOAD_HW_LOCK_REGISTER /* fetch lock pointer */
360 HW_LOCK_MOV_WORD $0, (HW_LOCK_REGISTER) /* clear the lock */
365 * void hw_lock_byte_init(uint8_t *)
367 * Initialize a hardware byte lock.
369 LEAF_ENTRY(hw_lock_byte_init)
370 LOAD_HW_LOCK_REGISTER /* fetch lock pointer */
371 movb $0, (HW_LOCK_REGISTER) /* clear the lock */
375 * void hw_lock_lock(hw_lock_t)
377 * Acquire lock, spinning until it becomes available.
378 * MACH_RT: also return with preemption disabled.
380 LEAF_ENTRY(hw_lock_lock)
381 LOAD_HW_LOCK_REGISTER /* fetch lock pointer */
382 LOAD_HW_LOCK_THREAD_REGISTER /* get thread pointer */
386 mov (HW_LOCK_REGISTER), HW_LOCK_EXAM_REGISTER
387 test HW_LOCK_EXAM_REGISTER,HW_LOCK_EXAM_REGISTER /* lock locked? */
388 jne 3f /* branch if so */
389 lock; cmpxchg HW_LOCK_THREAD_REGISTER,(HW_LOCK_REGISTER) /* try to acquire the HW lock */
391 movl $1,%eax /* In case this was a timeout call */
392 LEAF_RET /* if yes, then nothing left to do */
394 PAUSE /* pause for hyper-threading */
395 jmp 1b /* try again */
398 * void hw_lock_byte_lock(uint8_t *lock_byte)
400 * Acquire byte sized lock operand, spinning until it becomes available.
401 * MACH_RT: also return with preemption disabled.
404 LEAF_ENTRY(hw_lock_byte_lock)
405 LOAD_HW_LOCK_REGISTER /* Load lock pointer */
407 movl $1, %ecx /* Set lock value */
409 movb (HW_LOCK_REGISTER), %al /* Load byte at address */
410 testb %al,%al /* lock locked? */
411 jne 3f /* branch if so */
412 lock; cmpxchg %cl,(HW_LOCK_REGISTER) /* attempt atomic compare exchange */
414 LEAF_RET /* if yes, then nothing left to do */
416 PAUSE /* pause for hyper-threading */
417 jmp 1b /* try again */
420 * unsigned int hw_lock_to(hw_lock_t, unsigned int)
422 * Acquire lock, spinning until it becomes available or timeout.
423 * MACH_RT: also return with preemption disabled.
425 LEAF_ENTRY(hw_lock_to)
427 LOAD_HW_LOCK_REGISTER /* fetch lock pointer */
428 LOAD_HW_LOCK_THREAD_REGISTER
431 * Attempt to grab the lock immediately
432 * - fastpath without timeout nonsense.
436 mov (HW_LOCK_REGISTER), HW_LOCK_EXAM_REGISTER
437 test HW_LOCK_EXAM_REGISTER,HW_LOCK_EXAM_REGISTER /* lock locked? */
438 jne 2f /* branch if so */
439 lock; cmpxchg HW_LOCK_THREAD_REGISTER,(HW_LOCK_REGISTER) /* try to acquire the HW lock */
440 jne 2f /* branch on failure */
445 #define INNER_LOOP_COUNT 1000
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.
452 movl L_ARG1,%ecx /* fetch timeout */
458 rdtsc /* read cyclecount into %edx:%eax */
460 addl %ecx,%eax /* fetch and timeout */
461 adcl $0,%edx /* add carry */
463 mov %eax,%ebx /* %ecx:%ebx is the timeout expiry */
464 mov %edi, %edx /* load lock back into %edx */
468 rdtsc /* read cyclecount into %edx:%eax */
471 orq %rdx, %rax /* load 64-bit quantity into %rax */
472 addq %rax, %rsi /* %rsi is the timeout expiry */
477 * The inner-loop spin to look for the lock being freed.
480 mov $(INNER_LOOP_COUNT),%edi
482 mov $(INNER_LOOP_COUNT),%r9
485 PAUSE /* pause for hyper-threading */
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
488 je 6f /* zero => unlocked, try to grab it */
490 decl %edi /* decrement inner loop count */
492 decq %r9 /* decrement inner loop count */
494 jnz 5b /* time to check for timeout? */
497 * Here after spinning INNER_LOOP_COUNT times, check for timeout
500 mov %edx,%edi /* Save %edx */
502 rdtsc /* cyclecount into %edx:%eax */
504 xchg %edx,%edi /* cyclecount into %edi:%eax */
505 cmpl %ecx,%edi /* compare high-order 32-bits */
506 jb 4b /* continue spinning if less, or */
507 cmpl %ebx,%eax /* compare low-order 32-bits */
508 jb 4b /* continue if less, else bail */
509 xor %eax,%eax /* with 0 return value */
514 rdtsc /* cyclecount into %edx:%eax */
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 */
527 * Here to try to grab the lock that now appears to be free
530 LOAD_HW_LOCK_THREAD_REGISTER
531 lock; cmpxchg HW_LOCK_THREAD_REGISTER,(HW_LOCK_REGISTER) /* try to acquire the HW lock */
532 jne 4b /* no - spin again */
533 movl $1,%eax /* yes */
543 * void hw_lock_unlock(hw_lock_t)
545 * Unconditionally release lock.
546 * MACH_RT: release preemption level.
548 LEAF_ENTRY(hw_lock_unlock)
549 LOAD_HW_LOCK_REGISTER /* fetch lock pointer */
550 HW_LOCK_MOV_WORD $0, (HW_LOCK_REGISTER) /* clear the lock */
555 * void hw_lock_byte_unlock(uint8_t *lock_byte)
557 * Unconditionally release byte sized lock operand.
558 * MACH_RT: release preemption level.
561 LEAF_ENTRY(hw_lock_byte_unlock)
562 LOAD_HW_LOCK_REGISTER /* Load lock pointer */
563 movb $0, (HW_LOCK_REGISTER) /* Clear the lock byte */
568 * unsigned int hw_lock_try(hw_lock_t)
569 * MACH_RT: returns with preemption disabled on success.
571 LEAF_ENTRY(hw_lock_try)
572 LOAD_HW_LOCK_REGISTER /* fetch lock pointer */
573 LOAD_HW_LOCK_THREAD_REGISTER
576 mov (HW_LOCK_REGISTER),HW_LOCK_EXAM_REGISTER
577 test HW_LOCK_EXAM_REGISTER,HW_LOCK_EXAM_REGISTER
579 lock; cmpxchg HW_LOCK_THREAD_REGISTER,(HW_LOCK_REGISTER) /* try to acquire the HW lock */
582 movl $1,%eax /* success */
586 PREEMPTION_ENABLE /* failure: release preemption... */
587 xorl %eax,%eax /* ...and return failure */
591 * unsigned int hw_lock_held(hw_lock_t)
592 * MACH_RT: doesn't change preemption state.
593 * N.B. Racy, of course.
595 LEAF_ENTRY(hw_lock_held)
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
600 cmovne %ecx,%eax /* 0 => unlocked, 1 => locked */
605 * Reader-writer lock fastpaths. These currently exist for the
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
610 * The following should reflect the layout of the bitfield embedded within
611 * the lck_rw_t structure (see i386/locks.h).
613 #define LCK_RW_INTERLOCK (0x1 << 16)
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)
621 #define LCK_RW_SHARED_MASK (0xffff)
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
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
640 #error Unsupported architecture
643 #define RW_LOCK_SHARED_MASK (LCK_RW_INTERLOCK | LCK_RW_WANT_UPGRADE | LCK_RW_WANT_WRITE)
645 * void lck_rw_lock_shared(lck_rw_t *)
648 Entry(lck_rw_lock_shared)
651 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield and interlock */
652 testl $(RW_LOCK_SHARED_MASK), %eax /* Eligible for fastpath? */
655 movl %eax, %ecx /* original value in %eax for cmpxchgl */
656 incl %ecx /* Increment reader refcount */
658 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
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.
667 LOCKSTAT_LABEL(_lck_rw_lock_shared_lockstat_patch_point)
670 Fall thru when patched, counting on lock pointer in LCK_RW_REGISTER
672 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_SHARED_ACQUIRE, LCK_RW_REGISTER)
679 jmp EXT(lck_rw_lock_shared_gen)
683 #define RW_TRY_LOCK_SHARED_MASK (LCK_RW_WANT_UPGRADE | LCK_RW_WANT_WRITE)
685 * void lck_rw_try_lock_shared(lck_rw_t *)
688 Entry(lck_rw_try_lock_shared)
691 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield and interlock */
692 testl $(LCK_RW_INTERLOCK), %eax
694 testl $(RW_TRY_LOCK_SHARED_MASK), %eax
695 jne 3f /* lock is busy */
697 movl %eax, %ecx /* original value in %eax for cmpxchgl */
698 incl %ecx /* Increment reader refcount */
700 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
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.
710 LOCKSTAT_LABEL(_lck_rw_try_lock_shared_lockstat_patch_point)
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)
715 movl $1, %eax /* return TRUE */
725 #define RW_LOCK_EXCLUSIVE_HELD (LCK_RW_WANT_WRITE | LCK_RW_WANT_UPGRADE)
727 * int lck_rw_grab_shared(lck_rw_t *)
730 Entry(lck_rw_grab_shared)
733 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield and interlock */
734 testl $(LCK_RW_INTERLOCK), %eax
736 testl $(RW_LOCK_EXCLUSIVE_HELD), %eax
739 movl %eax, %ecx /* original value in %eax for cmpxchgl */
740 incl %ecx /* Increment reader refcount */
742 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
745 movl $1, %eax /* return success */
748 testl $(LCK_RW_SHARED_MASK), %eax
750 testl $(LCK_RW_PRIV_EXCL), %eax
753 xorl %eax, %eax /* return failure */
761 #define RW_LOCK_EXCLUSIVE_MASK (LCK_RW_SHARED_MASK | LCK_RW_INTERLOCK | \
762 LCK_RW_WANT_UPGRADE | LCK_RW_WANT_WRITE)
764 * void lck_rw_lock_exclusive(lck_rw_t*)
767 Entry(lck_rw_lock_exclusive)
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 */
774 movl %eax, %ecx /* original value in %eax for cmpxchgl */
775 orl $(LCK_RW_WANT_WRITE), %ecx
777 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
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.
786 LOCKSTAT_LABEL(_lck_rw_lock_exclusive_lockstat_patch_point)
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)
796 jmp EXT(lck_rw_lock_exclusive_gen)
800 #define RW_TRY_LOCK_EXCLUSIVE_MASK (LCK_RW_SHARED_MASK | LCK_RW_WANT_UPGRADE | LCK_RW_WANT_WRITE)
802 * void lck_rw_try_lock_exclusive(lck_rw_t *)
804 * Tries to get a write lock.
806 * Returns FALSE if the lock is not held on return.
808 Entry(lck_rw_try_lock_exclusive)
811 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield, interlock and shared count */
812 testl $(LCK_RW_INTERLOCK), %eax
814 testl $(RW_TRY_LOCK_EXCLUSIVE_MASK), %eax
815 jne 3f /* can't get it */
817 movl %eax, %ecx /* original value in %eax for cmpxchgl */
818 orl $(LCK_RW_WANT_WRITE), %ecx
820 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
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.
830 LOCKSTAT_LABEL(_lck_rw_try_lock_exclusive_lockstat_patch_point)
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)
835 movl $1, %eax /* return TRUE */
841 xorl %eax, %eax /* return FALSE */
847 * void lck_rw_lock_shared_to_exclusive(lck_rw_t*)
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
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
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
865 Entry(lck_rw_lock_shared_to_exclusive)
868 LOAD_LCK_RW_FLAGS_REGISTER /* Load state bitfield, interlock and shared count */
869 testl $(LCK_RW_INTERLOCK), %eax
871 testl $(LCK_RW_WANT_UPGRADE), %eax
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 */
878 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
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 */
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.
891 LOCKSTAT_LABEL(_lck_rw_lock_shared_to_exclusive_lockstat_patch_point)
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)
896 movl $1, %eax /* return success */
899 2: /* 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 */
907 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
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)
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)
920 ret /* and pass the failure return along */
925 jmp EXT(lck_rw_lock_shared_to_exclusive_success)
930 rwl_release_error_str:
931 .asciz "Releasing non-exclusive RW lock without a reader refcount!"
935 * lck_rw_type_t lck_rw_done(lck_rw_t *)
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 */
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 */
948 decl %ecx /* Decrement reader count */
949 testl $(LCK_RW_SHARED_MASK), %ecx /* if reader count has now gone to 0, check for waiters */
953 testl $(LCK_RW_WANT_UPGRADE), %ecx
955 andl $(~LCK_RW_WANT_UPGRADE), %ecx
958 testl $(LCK_RW_WANT_WRITE), %ecx
959 je 8f /* lock is not 'owned', go panic */
960 andl $(~LCK_RW_WANT_WRITE), %ecx
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...
967 * if !(fake_lck->lck_rw_priv_excl && fake_lck->lck_w_waiting)
969 testl $(LCK_W_WAITING), %eax
971 andl $(~LCK_W_WAITING), %ecx
973 testl $(LCK_RW_PRIV_EXCL), %eax
976 andl $(~LCK_R_WAITING), %ecx
979 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
985 call EXT(lck_rw_done_gen)
988 mov %eax,%esi /* old flags in %rsi */
989 /* lock is in %rdi already */
990 call EXT(lck_rw_done_gen)
998 LOAD_STRING_ARG0(rwl_release_error_str)
1004 * lck_rw_type_t lck_rw_lock_exclusive_to_shared(lck_rw_t *)
1007 Entry(lck_rw_lock_exclusive_to_shared)
1008 LOAD_LCK_RW_REGISTER
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 */
1014 movl %eax, %ecx /* keep original value in %eax for cmpxchgl */
1015 incl %ecx /* Increment reader count */
1017 testl $(LCK_RW_WANT_UPGRADE), %ecx
1019 andl $(~LCK_RW_WANT_UPGRADE), %ecx
1022 andl $(~LCK_RW_WANT_WRITE), %ecx
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...
1029 * if !(fake_lck->lck_rw_priv_excl && fake_lck->lck_w_waiting)
1031 testl $(LCK_W_WAITING), %eax
1033 testl $(LCK_RW_PRIV_EXCL), %eax
1036 andl $(~LCK_R_WAITING), %ecx
1039 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
1045 call EXT(lck_rw_lock_exclusive_to_shared_gen)
1049 call EXT(lck_rw_lock_exclusive_to_shared_gen)
1059 * int lck_rw_grab_want(lck_rw_t *)
1062 Entry(lck_rw_grab_want)
1063 LOAD_LCK_RW_REGISTER
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 */
1071 movl %eax, %ecx /* original value in %eax for cmpxchgl */
1072 orl $(LCK_RW_WANT_WRITE), %ecx
1074 cmpxchgl %ecx, (LCK_RW_REGISTER) /* Attempt atomic exchange */
1076 /* we now own want_write */
1077 movl $1, %eax /* return success */
1080 xorl %eax, %eax /* return failure */
1087 #define RW_LOCK_SHARED_OR_UPGRADE_MASK (LCK_RW_SHARED_MASK | LCK_RW_INTERLOCK | LCK_RW_WANT_UPGRADE)
1089 * int lck_rw_held_read_or_upgrade(lck_rw_t *)
1092 Entry(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
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
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
1112 #undef LOG_FIRST_MISS_ALONE
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.
1119 #define ATOMIC_STAT_UPDATES 1
1121 #if defined(ATOMIC_STAT_UPDATES)
1122 #define LOCK_IF_ATOMIC_STAT_UPDATES lock
1124 #define LOCK_IF_ATOMIC_STAT_UPDATES
1125 #endif /* ATOMIC_STAT_UPDATES */
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.
1134 #define M_OWNER MUTEX_OWNER
1135 #define M_PTR MUTEX_PTR
1136 #define M_STATE MUTEX_STATE
1138 #if defined(__i386__)
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
1147 #define LMTX_RET_REG %eax
1148 #define LMTX_RET_REG32 %eax
1149 #define LMTX_LGROUP_REG %esi
1150 #define LMTX_SSTATE_REG %edi
1151 #define LOAD_LMTX_REG(arg) mov arg, LMTX_REG
1152 #define LMTX_CHK_EXTENDED cmp LMTX_REG, LMTX_ARG0
1153 #define LMTX_ASSERT_OWNED cmpl $(MUTEX_ASSERT_OWNED), LMTX_ARG1
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) ; \
1164 incl GRP_MTX_STAT_UTIL+4(LMTX_LGROUP_REG) ; \
1167 #define LMTX_EXIT_EXTENDED \
1168 pop LMTX_SSTATE_REG ; \
1172 #define LMTX_CHK_EXTENDED_EXIT \
1173 cmp LMTX_REG, LMTX_ARG0 ; \
1175 pop LMTX_SSTATE_REG ; \
1176 pop LMTX_LGROUP_REG ; \
1180 #if LOG_FIRST_MISS_ALONE
1181 #define LMTX_UPDATE_MISS \
1182 test $1, LMTX_SSTATE_REG ; \
1184 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1185 incl GRP_MTX_STAT_MISS(LMTX_LGROUP_REG) ; \
1186 or $1, LMTX_SSTATE_REG ; \
1189 #define LMTX_UPDATE_MISS \
1190 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1191 incl GRP_MTX_STAT_MISS(LMTX_LGROUP_REG)
1195 #if LOG_FIRST_MISS_ALONE
1196 #define LMTX_UPDATE_WAIT \
1197 test $2, LMTX_SSTATE_REG ; \
1199 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1200 incl GRP_MTX_STAT_WAIT(LMTX_LGROUP_REG) ; \
1201 or $2, LMTX_SSTATE_REG ; \
1204 #define LMTX_UPDATE_WAIT \
1205 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1206 incl GRP_MTX_STAT_WAIT(LMTX_LGROUP_REG)
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.
1216 #define LMTX_UPDATE_DIRECT_WAIT \
1217 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1218 incl GRP_MTX_STAT_DIRECT_WAIT(LMTX_LGROUP_REG)
1221 #define LMTX_CALLEXT1(func_name) \
1224 call EXT(func_name) ; \
1228 #define LMTX_CALLEXT2(func_name, reg) \
1232 call EXT(func_name) ; \
1236 #elif defined(__x86_64__)
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
1246 #define LMTX_RET_REG %rax
1247 #define LMTX_RET_REG32 %eax
1248 #define LMTX_LGROUP_REG %r10
1249 #define LMTX_SSTATE_REG %r11
1250 #define LOAD_LMTX_REG(arg) mov %rdi, %rdx
1251 #define LMTX_CHK_EXTENDED cmp LMTX_REG, LMTX_REG_ORIG
1252 #define LMTX_ASSERT_OWNED cmp $(MUTEX_ASSERT_OWNED), LMTX_ARG1
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)
1261 #define LMTX_EXIT_EXTENDED
1263 #define LMTX_CHK_EXTENDED_EXIT
1266 #if LOG_FIRST_MISS_ALONE
1267 #define LMTX_UPDATE_MISS \
1268 test $1, LMTX_SSTATE_REG ; \
1270 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1271 incl GRP_MTX_STAT_MISS(LMTX_LGROUP_REG) ; \
1272 or $1, LMTX_SSTATE_REG ; \
1275 #define LMTX_UPDATE_MISS \
1276 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1277 incl GRP_MTX_STAT_MISS(LMTX_LGROUP_REG)
1281 #if LOG_FIRST_MISS_ALONE
1282 #define LMTX_UPDATE_WAIT \
1283 test $2, LMTX_SSTATE_REG ; \
1285 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1286 incl GRP_MTX_STAT_WAIT(LMTX_LGROUP_REG) ; \
1287 or $2, LMTX_SSTATE_REG ; \
1290 #define LMTX_UPDATE_WAIT \
1291 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1292 incl GRP_MTX_STAT_WAIT(LMTX_LGROUP_REG)
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.
1302 #define LMTX_UPDATE_DIRECT_WAIT \
1303 LOCK_IF_ATOMIC_STAT_UPDATES ; \
1304 incl GRP_MTX_STAT_DIRECT_WAIT(LMTX_LGROUP_REG)
1307 #define LMTX_CALLEXT1(func_name) \
1308 LMTX_CHK_EXTENDED ; \
1310 push LMTX_LGROUP_REG ; \
1311 push LMTX_SSTATE_REG ; \
1312 12: push LMTX_REG_ORIG ; \
1314 mov LMTX_REG, LMTX_ARG0 ; \
1315 call EXT(func_name) ; \
1317 pop LMTX_REG_ORIG ; \
1318 LMTX_CHK_EXTENDED ; \
1320 pop LMTX_SSTATE_REG ; \
1321 pop LMTX_LGROUP_REG ; \
1324 #define LMTX_CALLEXT2(func_name, reg) \
1325 LMTX_CHK_EXTENDED ; \
1327 push LMTX_LGROUP_REG ; \
1328 push LMTX_SSTATE_REG ; \
1329 12: push LMTX_REG_ORIG ; \
1331 mov reg, LMTX_ARG1 ; \
1332 mov LMTX_REG, LMTX_ARG0 ; \
1333 call EXT(func_name) ; \
1335 pop LMTX_REG_ORIG ; \
1336 LMTX_CHK_EXTENDED ; \
1338 pop LMTX_SSTATE_REG ; \
1339 pop LMTX_LGROUP_REG ; \
1343 #error Unsupported architecture
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
1355 * void lck_mtx_assert(lck_mtx_t* l, unsigned int)
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
1360 * LCK_MTX_ASSERT_NOTOWNED. Calls panic on assertion failure.
1364 NONLEAF_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 */
1368 mov M_STATE(LMTX_REG), LMTX_C_REG32
1369 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex? */
1371 mov M_PTR(LMTX_REG), LMTX_REG /* If so, take indirection */
1373 mov M_OWNER(LMTX_REG), LMTX_C_REG /* Load owner */
1375 jne 2f /* Assert ownership? */
1376 cmp LMTX_A_REG, LMTX_C_REG /* Current thread match? */
1377 jne 3f /* no, go panic */
1378 testl $(M_ILOCKED_MSK | M_MLOCKED_MSK), M_STATE(LMTX_REG)
1380 1: /* yes, we own it */
1383 cmp LMTX_A_REG, LMTX_C_REG /* Current thread match? */
1384 jne 1b /* No, return */
1386 LOAD_PTR_ARG1(LMTX_REG)
1387 LOAD_STRING_ARG0(mutex_assert_owned_str)
1391 LOAD_PTR_ARG1(LMTX_REG)
1392 LOAD_STRING_ARG0(mutex_assert_not_owned_str)
1399 LOAD_PTR_ARG1(LMTX_REG)
1400 LOAD_STRING_ARG0(mutex_interlock_destroyed_str)
1405 mutex_assert_not_owned_str:
1406 .asciz "mutex (%p) not owned\n"
1407 mutex_assert_owned_str:
1408 .asciz "mutex (%p) owned\n"
1409 mutex_interlock_destroyed_str:
1410 .asciz "trying to interlock destroyed mutex (%p)"
1417 * lck_mtx_try_lock()
1419 * lck_mtx_lock_spin()
1420 * lck_mtx_lock_spin_always()
1421 * lck_mtx_convert_spin()
1423 NONLEAF_ENTRY(lck_mtx_lock_spin_always)
1424 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
1425 jmp Llmls_avoid_check
1427 NONLEAF_ENTRY(lck_mtx_lock_spin)
1428 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
1430 CHECK_PREEMPTION_LEVEL()
1432 mov M_STATE(LMTX_REG), LMTX_C_REG32
1433 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32 /* is the interlock or mutex held */
1435 Llmls_try: /* no - can't be INDIRECT, DESTROYED or locked */
1436 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
1437 or $(M_ILOCKED_MSK | M_SPIN_MSK), LMTX_C_REG32
1441 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
1442 jne Llmls_busy_disabled
1444 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG
1445 mov LMTX_A_REG, M_OWNER(LMTX_REG) /* record owner of interlock */
1447 test LMTX_A_REG, LMTX_A_REG
1449 incl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
1451 #endif /* MACH_LDEBUG */
1453 LMTX_CHK_EXTENDED_EXIT
1454 /* return with the interlock held and preemption disabled */
1457 LOCKSTAT_LABEL(_lck_mtx_lock_spin_lockstat_patch_point)
1459 /* inherit lock pointer in LMTX_REG above */
1460 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_SPIN_ACQUIRE, LMTX_REG)
1465 test $M_ILOCKED_MSK, LMTX_C_REG32 /* is the interlock held */
1466 jz Llml_contended /* no, must have been the mutex */
1468 cmp $(MUTEX_DESTROYED), LMTX_C_REG32 /* check to see if its marked destroyed */
1469 je lck_mtx_destroyed
1470 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex */
1471 jne Llmls_loop /* no... must be interlocked */
1475 mov M_STATE(LMTX_REG), LMTX_C_REG32
1476 test $(M_SPIN_MSK), LMTX_C_REG32
1479 LMTX_UPDATE_MISS /* M_SPIN_MSK was set, so M_ILOCKED_MSK must also be present */
1482 mov M_STATE(LMTX_REG), LMTX_C_REG32
1484 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32
1486 test $(M_MLOCKED_MSK), LMTX_C_REG32
1487 jnz Llml_contended /* mutex owned by someone else, go contend for it */
1490 Llmls_busy_disabled:
1496 NONLEAF_ENTRY(lck_mtx_lock)
1497 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
1499 CHECK_PREEMPTION_LEVEL()
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 */
1504 Llml_try: /* no - can't be INDIRECT, DESTROYED or locked */
1505 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
1506 or $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32
1510 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
1511 jne Llml_busy_disabled
1513 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG
1514 mov LMTX_A_REG, M_OWNER(LMTX_REG) /* record owner of mutex */
1516 test LMTX_A_REG, LMTX_A_REG
1518 incl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
1520 #endif /* MACH_LDEBUG */
1522 testl $(M_WAITERS_MSK), M_STATE(LMTX_REG)
1525 LMTX_CALLEXT1(lck_mtx_lock_acquire_x86)
1528 andl $(~M_ILOCKED_MSK), M_STATE(LMTX_REG)
1531 LMTX_CHK_EXTENDED /* is this an extended mutex */
1536 LOCKSTAT_LABEL(_lck_mtx_lock_lockstat_patch_point)
1538 /* inherit lock pointer in LMTX_REG above */
1539 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_ACQUIRE, LMTX_REG)
1546 LOCKSTAT_LABEL(_lck_mtx_lock_ext_lockstat_patch_point)
1548 /* inherit lock pointer in LMTX_REG above */
1549 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_LOCK_ACQUIRE, LMTX_REG)
1555 test $M_ILOCKED_MSK, LMTX_C_REG32 /* is the interlock held */
1556 jz Llml_contended /* no, must have been the mutex */
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 */
1565 mov M_STATE(LMTX_REG), LMTX_C_REG32
1566 test $(M_SPIN_MSK), LMTX_C_REG32
1569 LMTX_UPDATE_MISS /* M_SPIN_MSK was set, so M_ILOCKED_MSK must also be present */
1572 mov M_STATE(LMTX_REG), LMTX_C_REG32
1574 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32
1576 test $(M_MLOCKED_MSK), LMTX_C_REG32
1577 jnz Llml_contended /* mutex owned by someone else, go contend for it */
1586 LMTX_CHK_EXTENDED /* is this an extended mutex */
1590 LMTX_CALLEXT1(lck_mtx_lock_spinwait_x86)
1592 test LMTX_RET_REG, LMTX_RET_REG
1593 jz Llml_acquired /* acquired mutex, interlock held and preemption disabled */
1595 cmp $1, LMTX_RET_REG /* check for direct wait status */
1597 LMTX_CHK_EXTENDED /* is this an extended mutex */
1599 LMTX_UPDATE_DIRECT_WAIT
1601 mov M_STATE(LMTX_REG), LMTX_C_REG32
1602 test $(M_ILOCKED_MSK), LMTX_C_REG32
1605 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
1606 or $(M_ILOCKED_MSK), LMTX_C_REG32 /* try to take the interlock */
1610 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
1613 test $(M_MLOCKED_MSK), LMTX_C_REG32 /* we've got the interlock and */
1615 or $(M_MLOCKED_MSK), LMTX_C_REG32 /* the mutex is free... grab it directly */
1616 mov LMTX_C_REG32, M_STATE(LMTX_REG)
1618 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG
1619 mov LMTX_A_REG, M_OWNER(LMTX_REG) /* record owner of mutex */
1621 test LMTX_A_REG, LMTX_A_REG
1623 incl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
1625 #endif /* MACH_LDEBUG */
1628 testl $(M_WAITERS_MSK), M_STATE(LMTX_REG)
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
1635 LMTX_CALLEXT1(lck_mtx_lock_acquire_x86)
1638 3: /* interlock held, mutex busy */
1639 LMTX_CHK_EXTENDED /* is this an extended mutex */
1643 LMTX_CALLEXT1(lck_mtx_lock_wait_x86)
1653 NONLEAF_ENTRY(lck_mtx_try_lock_spin)
1654 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
1656 mov M_STATE(LMTX_REG), LMTX_C_REG32
1657 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32 /* is the interlock or mutex held */
1659 Llmts_try: /* no - can't be INDIRECT, DESTROYED or locked */
1660 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
1661 or $(M_ILOCKED_MSK | M_SPIN_MSK), LMTX_C_REG
1665 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
1666 jne Llmts_busy_disabled
1668 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG
1669 mov LMTX_A_REG, M_OWNER(LMTX_REG) /* record owner of mutex */
1671 test LMTX_A_REG, LMTX_A_REG
1673 incl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
1675 #endif /* MACH_LDEBUG */
1677 LMTX_CHK_EXTENDED_EXIT
1681 mov $1, LMTX_RET_REG /* return success */
1682 LOCKSTAT_LABEL(_lck_mtx_try_lock_spin_lockstat_patch_point)
1684 /* inherit lock pointer in LMTX_REG above */
1685 LOCKSTAT_RECORD(LS_LCK_MTX_TRY_SPIN_LOCK_ACQUIRE, LMTX_REG)
1687 mov $1, LMTX_RET_REG /* return success */
1691 test $(M_ILOCKED_MSK), LMTX_C_REG32 /* is the interlock held */
1692 jz Llmts_fail /* no, must be held as a mutex */
1694 cmp $(MUTEX_DESTROYED), LMTX_C_REG32 /* check to see if its marked destroyed */
1695 je lck_mtx_destroyed
1696 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex? */
1702 mov M_STATE(LMTX_REG), LMTX_C_REG32
1704 test $(M_MLOCKED_MSK | M_SPIN_MSK), LMTX_C_REG32
1706 test $(M_ILOCKED_MSK), LMTX_C_REG32
1710 Llmts_busy_disabled:
1716 NONLEAF_ENTRY(lck_mtx_try_lock)
1717 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
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 */
1722 Llmt_try: /* no - can't be INDIRECT, DESTROYED or locked */
1723 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
1724 or $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32
1728 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
1729 jne Llmt_busy_disabled
1731 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG
1732 mov LMTX_A_REG, M_OWNER(LMTX_REG) /* record owner of mutex */
1734 test LMTX_A_REG, LMTX_A_REG
1736 incl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
1738 #endif /* MACH_LDEBUG */
1740 LMTX_CHK_EXTENDED_EXIT
1742 test $(M_WAITERS_MSK), LMTX_C_REG32
1745 LMTX_CALLEXT1(lck_mtx_lock_acquire_x86)
1747 andl $(~M_ILOCKED_MSK), M_STATE(LMTX_REG)
1752 mov $1, LMTX_RET_REG /* return success */
1753 /* Dtrace probe: LS_LCK_MTX_TRY_LOCK_ACQUIRE */
1754 LOCKSTAT_LABEL(_lck_mtx_try_lock_lockstat_patch_point)
1756 /* inherit lock pointer in LMTX_REG from above */
1757 LOCKSTAT_RECORD(LS_LCK_MTX_TRY_LOCK_ACQUIRE, LMTX_REG)
1759 mov $1, LMTX_RET_REG /* return success */
1763 test $(M_ILOCKED_MSK), LMTX_C_REG32 /* is the interlock held */
1764 jz Llmt_fail /* no, must be held as a mutex */
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? */
1774 mov M_STATE(LMTX_REG), LMTX_C_REG32
1776 test $(M_MLOCKED_MSK | M_SPIN_MSK), LMTX_C_REG32
1778 test $(M_ILOCKED_MSK), LMTX_C_REG32
1789 LMTX_CHK_EXTENDED /* is this an extended mutex */
1794 xor LMTX_RET_REG, LMTX_RET_REG
1799 NONLEAF_ENTRY(lck_mtx_convert_spin)
1800 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
1802 mov M_STATE(LMTX_REG), LMTX_C_REG32
1803 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex? */
1805 mov M_PTR(LMTX_REG), LMTX_REG /* If so, take indirection */
1806 mov M_STATE(LMTX_REG), LMTX_C_REG32
1808 test $(M_MLOCKED_MSK), LMTX_C_REG32 /* already owned as a mutex, just return */
1810 test $(M_WAITERS_MSK), LMTX_C_REG32 /* are there any waiters? */
1813 LMTX_CALLEXT1(lck_mtx_lock_acquire_x86)
1814 mov M_STATE(LMTX_REG), LMTX_C_REG32
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
1818 mov LMTX_C_REG32, M_STATE(LMTX_REG) /* since I own the interlock, I don't need an atomic update */
1826 #if defined(__i386__)
1827 NONLEAF_ENTRY(lck_mtx_unlock)
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
1834 NONLEAF_ENTRY(lck_mtx_unlock_darwin10)
1836 NONLEAF_ENTRY(lck_mtx_unlock)
1838 LOAD_LMTX_REG(B_ARG0) /* fetch lock pointer */
1840 mov M_STATE(LMTX_REG), LMTX_C_REG32
1842 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex? */
1846 test $(M_MLOCKED_MSK), LMTX_C_REG32 /* check for full mutex */
1849 test $(M_ILOCKED_MSK), LMTX_C_REG /* have to wait for interlock to clear */
1852 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
1853 and $(~M_MLOCKED_MSK), LMTX_C_REG32 /* drop mutex */
1854 or $(M_ILOCKED_MSK), LMTX_C_REG32 /* pick up interlock */
1858 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
1859 jne Llmu_busy_disabled /* branch on failure to spin loop */
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 */
1865 and $(~(M_ILOCKED_MSK | M_SPIN_MSK | M_PROMOTED_MSK)), LMTX_A_REG
1867 test $(M_WAITERS_MSK), LMTX_A_REG32
1869 dec LMTX_A_REG32 /* decrement waiter count */
1871 mov LMTX_A_REG32, M_STATE(LMTX_REG) /* since I own the interlock, I don't need an atomic update */
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
1878 decl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
1880 #endif /* MACH_LDEBUG */
1882 test $(M_PROMOTED_MSK | M_WAITERS_MSK), LMTX_C_REG32
1885 LMTX_CALLEXT2(lck_mtx_unlock_wakeup_x86, LMTX_C_REG)
1894 /* Dtrace: LS_LCK_MTX_UNLOCK_RELEASE */
1895 LOCKSTAT_LABEL(_lck_mtx_unlock_lockstat_patch_point)
1897 /* inherit lock pointer in LMTX_REG from above */
1898 LOCKSTAT_RECORD(LS_LCK_MTX_UNLOCK_RELEASE, LMTX_REG)
1904 /* Dtrace: LS_LCK_MTX_EXT_UNLOCK_RELEASE */
1905 LOCKSTAT_LABEL(_lck_mtx_ext_unlock_lockstat_patch_point)
1907 /* inherit lock pointer in LMTX_REG from above */
1908 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_UNLOCK_RELEASE, LMTX_REG)
1917 mov M_STATE(LMTX_REG), LMTX_C_REG32
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)
1925 mov M_STATE(LMTX_REG), LMTX_C_REG32
1930 LEAF_ENTRY(lck_mtx_ilk_unlock)
1931 LOAD_LMTX_REG(L_ARG0) /* fetch lock pointer - no indirection here */
1933 andl $(~M_ILOCKED_MSK), M_STATE(LMTX_REG)
1935 PREEMPTION_ENABLE /* need to re-enable preemption */
1941 LEAF_ENTRY(lck_mtx_lock_grab_mutex)
1942 LOAD_LMTX_REG(L_ARG0) /* fetch lock pointer - no indirection here */
1944 mov M_STATE(LMTX_REG), LMTX_C_REG32
1946 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32 /* can't have the mutex yet */
1949 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
1950 or $(M_ILOCKED_MSK | M_MLOCKED_MSK), LMTX_C_REG32
1954 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
1955 jne 2f /* branch on failure to spin loop */
1957 mov %gs:CPU_ACTIVE_THREAD, LMTX_A_REG
1958 mov LMTX_A_REG, M_OWNER(LMTX_REG) /* record owner of mutex */
1960 test LMTX_A_REG, LMTX_A_REG
1962 incl TH_MUTEX_COUNT(LMTX_A_REG) /* lock statistic */
1964 #endif /* MACH_LDEBUG */
1966 mov $1, LMTX_RET_REG /* return success */
1971 xor LMTX_RET_REG, LMTX_RET_REG /* return failure */
1976 LEAF_ENTRY(lck_mtx_lock_mark_destroyed)
1977 LOAD_LMTX_REG(L_ARG0)
1979 mov M_STATE(LMTX_REG), LMTX_C_REG32
1980 cmp $(MUTEX_IND), LMTX_C_REG32 /* Is this an indirect mutex? */
1983 movl $(MUTEX_DESTROYED), M_STATE(LMTX_REG) /* convert to destroyed state */
1986 test $(M_ILOCKED_MSK), LMTX_C_REG /* have to wait for interlock to clear */
1990 mov LMTX_C_REG, LMTX_A_REG /* eax contains snapshot for cmpxchgl */
1991 or $(M_ILOCKED_MSK), LMTX_C_REG32
1993 cmpxchg LMTX_C_REG32, M_STATE(LMTX_REG) /* atomic compare and exchange */
1994 jne 4f /* branch on failure to spin loop */
1995 movl $(MUTEX_DESTROYED), M_STATE(LMTX_REG) /* convert to destroyed state */
1998 LEAF_RET /* return with M_ILOCKED set */
2005 LEAF_ENTRY(preemption_underflow_panic)
2007 incl %gs:CPU_PREEMPTION_LEVEL
2009 LOAD_STRING_ARG0(16f)
2013 16: String "Preemption level underflow, possible cause unlocking an unlocked mutex or spinlock"
2017 LEAF_ENTRY(_disable_preemption)
2020 #endif /* MACH_RT */
2023 LEAF_ENTRY(_enable_preemption)
2026 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
2029 pushl %gs:CPU_PREEMPTION_LEVEL
2031 movl %gs:CPU_PREEMPTION_LEVEL,%esi
2034 LOAD_STRING_ARG0(_enable_preemption_less_than_zero)
2038 _enable_preemption_less_than_zero:
2039 .asciz "_enable_preemption: preemption_level(%d) < 0!"
2042 #endif /* MACH_ASSERT */
2044 #endif /* MACH_RT */
2047 LEAF_ENTRY(_enable_preemption_no_check)
2050 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
2053 LOAD_STRING_ARG0(_enable_preemption_no_check_less_than_zero)
2057 _enable_preemption_no_check_less_than_zero:
2058 .asciz "_enable_preemption_no_check: preemption_level <= 0!"
2061 #endif /* MACH_ASSERT */
2062 _ENABLE_PREEMPTION_NO_CHECK
2063 #endif /* MACH_RT */
2067 LEAF_ENTRY(_mp_disable_preemption)
2070 #endif /* MACH_RT */
2073 LEAF_ENTRY(_mp_enable_preemption)
2076 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
2079 pushl %gs:CPU_PREEMPTION_LEVEL
2081 movl %gs:CPU_PREEMPTION_LEVEL,%esi
2084 LOAD_STRING_ARG0(_mp_enable_preemption_less_than_zero)
2088 _mp_enable_preemption_less_than_zero:
2089 .asciz "_mp_enable_preemption: preemption_level (%d) <= 0!"
2092 #endif /* MACH_ASSERT */
2094 #endif /* MACH_RT */
2097 LEAF_ENTRY(_mp_enable_preemption_no_check)
2100 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
2103 LOAD_STRING_ARG0(_mp_enable_preemption_no_check_less_than_zero)
2107 _mp_enable_preemption_no_check_less_than_zero:
2108 .asciz "_mp_enable_preemption_no_check: preemption_level <= 0!"
2111 #endif /* MACH_ASSERT */
2112 _ENABLE_PREEMPTION_NO_CHECK
2113 #endif /* MACH_RT */
2118 LEAF_ENTRY(i_bit_set)
2125 LEAF_ENTRY(i_bit_clear)
2133 LEAF_ENTRY(bit_lock)
2143 LEAF_ENTRY(bit_lock_try)
2149 LEAF_RET /* %eax better not be null ! */
2154 LEAF_ENTRY(bit_unlock)
2162 * Atomic primitives, prototyped in kern/simple_lock.h
2164 LEAF_ENTRY(hw_atomic_add)
2165 movl L_ARG0, %ecx /* Load address of operand */
2166 movl L_ARG1, %eax /* Load addend */
2169 xaddl %eax, (%ecx) /* Atomic exchange and add */
2170 addl %edx, %eax /* Calculate result */
2173 LEAF_ENTRY(hw_atomic_sub)
2174 movl L_ARG0, %ecx /* Load address of operand */
2175 movl L_ARG1, %eax /* Load subtrahend */
2179 xaddl %eax, (%ecx) /* Atomic exchange and add */
2180 addl %edx, %eax /* Calculate result */
2183 LEAF_ENTRY(hw_atomic_or)
2184 movl L_ARG0, %ecx /* Load address of operand */
2187 movl L_ARG1, %edx /* Load mask */
2190 cmpxchgl %edx, (%ecx) /* Atomic CAS */
2192 movl %edx, %eax /* Result */
2195 * A variant of hw_atomic_or which doesn't return a value.
2196 * The implementation is thus comparatively more efficient.
2199 LEAF_ENTRY(hw_atomic_or_noret)
2200 movl L_ARG0, %ecx /* Load address of operand */
2201 movl L_ARG1, %edx /* Load mask */
2203 orl %edx, (%ecx) /* Atomic OR */
2206 LEAF_ENTRY(hw_atomic_and)
2207 movl L_ARG0, %ecx /* Load address of operand */
2210 movl L_ARG1, %edx /* Load mask */
2213 cmpxchgl %edx, (%ecx) /* Atomic CAS */
2215 movl %edx, %eax /* Result */
2218 * A variant of hw_atomic_and which doesn't return a value.
2219 * The implementation is thus comparatively more efficient.
2222 LEAF_ENTRY(hw_atomic_and_noret)
2223 movl L_ARG0, %ecx /* Load address of operand */
2224 movl L_ARG1, %edx /* Load mask */
2226 andl %edx, (%ecx) /* Atomic AND */
2229 #else /* !__i386__ */
2231 LEAF_ENTRY(i_bit_set)
2236 LEAF_ENTRY(i_bit_clear)
2242 LEAF_ENTRY(bit_lock)
2250 LEAF_ENTRY(bit_lock_try)
2260 LEAF_ENTRY(bit_unlock)
2267 * Atomic primitives, prototyped in kern/simple_lock.h
2269 LEAF_ENTRY(hw_atomic_add)
2270 movl %esi, %eax /* Load addend */
2272 xaddl %eax, (%rdi) /* Atomic exchange and add */
2273 addl %esi, %eax /* Calculate result */
2276 LEAF_ENTRY(hw_atomic_sub)
2280 xaddl %eax, (%rdi) /* Atomic exchange and add */
2281 addl %esi, %eax /* Calculate result */
2284 LEAF_ENTRY(hw_atomic_or)
2287 movl %esi, %edx /* Load mask */
2290 cmpxchgl %edx, (%rdi) /* Atomic CAS */
2292 movl %edx, %eax /* Result */
2295 * A variant of hw_atomic_or which doesn't return a value.
2296 * The implementation is thus comparatively more efficient.
2299 LEAF_ENTRY(hw_atomic_or_noret)
2301 orl %esi, (%rdi) /* Atomic OR */
2305 LEAF_ENTRY(hw_atomic_and)
2308 movl %esi, %edx /* Load mask */
2311 cmpxchgl %edx, (%rdi) /* Atomic CAS */
2313 movl %edx, %eax /* Result */
2316 * A variant of hw_atomic_and which doesn't return a value.
2317 * The implementation is thus comparatively more efficient.
2320 LEAF_ENTRY(hw_atomic_and_noret)
2322 andl %esi, (%rdi) /* Atomic OR */
2325 #endif /* !__i386 __ */