2 * Copyright (c) 2000-2007 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>
48 #define PAUSE rep; nop
51 * When performance isn't the only concern, it's
52 * nice to build stack frames...
54 #define BUILD_STACK_FRAMES (GPROF || \
55 ((MACH_LDEBUG || ETAP_LOCK_TRACE) && MACH_KDB))
57 #if BUILD_STACK_FRAMES
59 /* Stack-frame-relative: */
64 #define LEAF_ENTRY(name) \
69 #define LEAF_ENTRY2(n1,n2) \
79 #else /* BUILD_STACK_FRAMES */
81 /* Stack-pointer-relative: */
86 #define LEAF_ENTRY(name) \
89 #define LEAF_ENTRY2(n1,n2) \
96 #endif /* BUILD_STACK_FRAMES */
99 /* Non-leaf routines always have a stack frame: */
101 #define NONLEAF_ENTRY(name) \
106 #define NONLEAF_ENTRY2(n1,n2) \
112 #define NONLEAF_RET \
118 #define M_LOCKED MUTEX_LOCKED(%edx)
119 #define M_WAITERS MUTEX_WAITERS(%edx)
120 #define M_PROMOTED_PRI MUTEX_PROMOTED_PRI(%edx)
121 #define M_ITAG MUTEX_ITAG(%edx)
122 #define M_PTR MUTEX_PTR(%edx)
124 #define M_TYPE MUTEX_TYPE(%edx)
125 #define M_PC MUTEX_PC(%edx)
126 #define M_THREAD MUTEX_THREAD(%edx)
127 #endif /* MACH_LDEBUG */
130 #define CX(addr,reg) addr(,reg,4)
134 * Routines for general lock debugging.
138 * Checks for expected lock types and calls "panic" on
139 * mismatch. Detects calls to Mutex functions with
140 * type simplelock and vice versa.
142 #define CHECK_MUTEX_TYPE() \
143 cmpl $ MUTEX_TAG,M_TYPE ; \
149 2: String "not a mutex!" ; \
154 * If one or more simplelocks are currently held by a thread,
155 * an attempt to acquire a mutex will cause this check to fail
156 * (since a mutex lock may context switch, holding a simplelock
157 * is not a good thing).
160 #define CHECK_PREEMPTION_LEVEL() \
161 cmpl $0,%gs:CPU_PREEMPTION_LEVEL ; \
167 2: String "preemption_level != 0!" ; \
171 #define CHECK_PREEMPTION_LEVEL()
174 #define CHECK_NO_SIMPLELOCKS() \
175 cmpl $0,%gs:CPU_SIMPLE_LOCK_COUNT ; \
181 2: String "simple_locks_held!" ; \
186 * Verifies return to the correct thread in "unlock" situations.
188 #define CHECK_THREAD(thd) \
189 movl %gs:CPU_ACTIVE_THREAD,%ecx ; \
198 2: String "wrong thread!" ; \
202 #define CHECK_MYLOCK(thd) \
203 movl %gs:CPU_ACTIVE_THREAD,%ecx ; \
212 2: String "mylock attempt!" ; \
216 #define METER_SIMPLE_LOCK_LOCK(reg) \
218 call EXT(meter_simple_lock) ; \
221 #define METER_SIMPLE_LOCK_UNLOCK(reg) \
223 call EXT(meter_simple_unlock) ; \
226 #else /* MACH_LDEBUG */
227 #define CHECK_MUTEX_TYPE()
228 #define CHECK_SIMPLE_LOCK_TYPE
229 #define CHECK_THREAD(thd)
230 #define CHECK_PREEMPTION_LEVEL()
231 #define CHECK_NO_SIMPLELOCKS()
232 #define CHECK_MYLOCK(thd)
233 #define METER_SIMPLE_LOCK_LOCK(reg)
234 #define METER_SIMPLE_LOCK_UNLOCK(reg)
235 #endif /* MACH_LDEBUG */
238 #define PREEMPTION_DISABLE \
239 incl %gs:CPU_PREEMPTION_LEVEL
242 #define PREEMPTION_ENABLE \
243 decl %gs:CPU_PREEMPTION_LEVEL ; \
246 testl $ EFL_IF,(%esp) ; \
249 movl %gs:CPU_PENDING_AST,%eax ; \
250 testl $ AST_URGENT,%eax ; \
252 movl %gs:CPU_INTERRUPT_LEVEL,%eax ; \
265 #define LOCKSTAT_LABEL(lab) \
273 .globl _lockstat_probe
274 .globl _lockstat_probemap
276 #define LOCKSTAT_RECORD(id, lck) \
279 sub $0x38,%esp /* size of dtrace_probe args */ ; \
280 movl _lockstat_probemap + (id * 4),%eax ; \
291 movl lck,4(%esp) /* copy lock pointer to arg 1 */ ; \
294 call *_lockstat_probe ; \
296 /* ret - left to subsequent code, e.g. return values */
298 #define LOCKSTAT_RECORD2(id, lck, arg) \
301 sub $0x38,%esp /* size of dtrace_probe args */ ; \
302 movl _lockstat_probemap + (id * 4),%eax ; \
313 movl lck,4(%esp) /* copy lock pointer to arg 1 */ ; \
316 call *_lockstat_probe ; \
318 /* ret - left to subsequent code, e.g. return values */
323 * void hw_lock_init(hw_lock_t)
325 * Initialize a hardware lock.
327 LEAF_ENTRY(hw_lock_init)
328 movl L_ARG0,%edx /* fetch lock pointer */
329 movl $0,(%edx) /* clear the lock */
334 * void hw_lock_byte_init(uint8_t *)
336 * Initialize a hardware byte lock.
338 LEAF_ENTRY(hw_lock_byte_init)
339 movl L_ARG0,%edx /* fetch lock pointer */
340 movb $0,(%edx) /* clear the lock */
344 * void hw_lock_lock(hw_lock_t)
346 * Acquire lock, spinning until it becomes available.
347 * MACH_RT: also return with preemption disabled.
349 LEAF_ENTRY(hw_lock_lock)
350 movl L_ARG0,%edx /* fetch lock pointer */
352 movl %gs:CPU_ACTIVE_THREAD,%ecx
356 testl %eax,%eax /* lock locked? */
357 jne 3f /* branch if so */
358 lock; cmpxchgl %ecx,(%edx) /* try to acquire the HW lock */
360 movl $1,%eax /* In case this was a timeout call */
361 LEAF_RET /* if yes, then nothing left to do */
363 PAUSE /* pause for hyper-threading */
364 jmp 1b /* try again */
367 * void hw_lock_byte_lock(uint8_t *lock_byte)
369 * Acquire byte sized lock operand, spinning until it becomes available.
370 * MACH_RT: also return with preemption disabled.
373 LEAF_ENTRY(hw_lock_byte_lock)
374 movl L_ARG0,%edx /* Load lock pointer */
376 movl $1, %ecx /* Set lock value */
378 movb (%edx), %al /* Load byte at address */
379 testb %al,%al /* lock locked? */
380 jne 3f /* branch if so */
381 lock; cmpxchgb %cl,(%edx) /* attempt atomic compare exchange */
383 LEAF_RET /* if yes, then nothing left to do */
385 PAUSE /* pause for hyper-threading */
386 jmp 1b /* try again */
389 * unsigned int hw_lock_to(hw_lock_t, unsigned int)
391 * Acquire lock, spinning until it becomes available or timeout.
392 * MACH_RT: also return with preemption disabled.
394 LEAF_ENTRY(hw_lock_to)
396 movl L_ARG0,%edx /* fetch lock pointer */
397 movl %gs:CPU_ACTIVE_THREAD,%ecx
399 * Attempt to grab the lock immediately
400 * - fastpath without timeout nonsense.
404 testl %eax,%eax /* lock locked? */
405 jne 2f /* branch if so */
406 lock; cmpxchgl %ecx,(%edx) /* try to acquire the HW lock */
407 jne 2f /* branch on failure */
412 #define INNER_LOOP_COUNT 1000
414 * Failed to get the lock so set the timeout
415 * and then spin re-checking the lock but pausing
416 * every so many (INNER_LOOP_COUNT) spins to check for timeout.
418 movl L_ARG1,%ecx /* fetch timeout */
423 rdtsc /* read cyclecount into %edx:%eax */
425 addl %ecx,%eax /* fetch and timeout */
426 adcl $0,%edx /* add carry */
428 mov %eax,%ebx /* %ecx:%ebx is the timeout expiry */
431 * The inner-loop spin to look for the lock being freed.
433 mov $(INNER_LOOP_COUNT),%edx
435 PAUSE /* pause for hyper-threading */
436 movl (%edi),%eax /* spin checking lock value in cache */
438 je 6f /* zero => unlocked, try to grab it */
439 decl %edx /* decrement inner loop count */
440 jnz 5b /* time to check for timeout? */
443 * Here after spinning INNER_LOOP_COUNT times, check for timeout
445 rdtsc /* cyclecount into %edx:%eax */
447 cmpl %ecx,%edx /* compare high-order 32-bits */
448 jb 4b /* continue spinning if less, or */
449 cmpl %ebx,%eax /* compare low-order 32-bits */
450 jb 4b /* continue if less, else bail */
451 xor %eax,%eax /* with 0 return value */
458 * Here to try to grab the lock that now appears to be free
461 movl %gs:CPU_ACTIVE_THREAD,%edx
462 lock; cmpxchgl %edx,(%edi) /* try to acquire the HW lock */
463 jne 4b /* no - spin again */
464 movl $1,%eax /* yes */
470 * void hw_lock_unlock(hw_lock_t)
472 * Unconditionally release lock.
473 * MACH_RT: release preemption level.
475 LEAF_ENTRY(hw_lock_unlock)
476 movl L_ARG0,%edx /* fetch lock pointer */
477 movl $0,(%edx) /* clear the lock */
481 * void hw_lock_byte_unlock(uint8_t *lock_byte)
483 * Unconditionally release byte sized lock operand.
484 * MACH_RT: release preemption level.
487 LEAF_ENTRY(hw_lock_byte_unlock)
488 movl L_ARG0,%edx /* Load lock pointer */
489 movb $0,(%edx) /* Clear the lock byte */
494 * void i386_lock_unlock_with_flush(hw_lock_t)
496 * Unconditionally release lock, followed by a cacheline flush of
497 * the line corresponding to the lock dword. This routine is currently
498 * used with certain locks which are susceptible to lock starvation,
499 * minimizing cache affinity for lock acquisitions. A queued spinlock
500 * or other mechanism that ensures fairness would obviate the need
501 * for this routine, but ideally few or no spinlocks should exhibit
502 * enough contention to require such measures.
503 * MACH_RT: release preemption level.
505 LEAF_ENTRY(i386_lock_unlock_with_flush)
506 movl L_ARG0,%edx /* Fetch lock pointer */
507 movl $0,(%edx) /* Clear the lock */
508 mfence /* Serialize prior stores */
509 clflush (%edx) /* Write back and invalidate line */
514 * unsigned int hw_lock_try(hw_lock_t)
515 * MACH_RT: returns with preemption disabled on success.
517 LEAF_ENTRY(hw_lock_try)
518 movl L_ARG0,%edx /* fetch lock pointer */
520 movl %gs:CPU_ACTIVE_THREAD,%ecx
525 lock; cmpxchgl %ecx,(%edx) /* try to acquire the HW lock */
528 movl $1,%eax /* success */
532 PREEMPTION_ENABLE /* failure: release preemption... */
533 xorl %eax,%eax /* ...and return failure */
537 * unsigned int hw_lock_held(hw_lock_t)
538 * MACH_RT: doesn't change preemption state.
539 * N.B. Racy, of course.
541 LEAF_ENTRY(hw_lock_held)
542 movl L_ARG0,%edx /* fetch lock pointer */
544 movl (%edx),%eax /* check lock value */
547 cmovne %ecx,%eax /* 0 => unlocked, 1 => locked */
550 LEAF_ENTRY(mutex_init)
551 movl L_ARG0,%edx /* fetch lock pointer */
553 movl %eax,M_ILK /* clear interlock */
554 movl %eax,M_LOCKED /* clear locked flag */
555 movw %ax,M_WAITERS /* init waiter count */
556 movw %ax,M_PROMOTED_PRI
559 movl $ MUTEX_TAG,M_TYPE /* set lock type */
560 movl %eax,M_PC /* init caller pc */
561 movl %eax,M_THREAD /* and owning thread */
567 * Reader-writer lock fastpaths. These currently exist for the
568 * shared lock acquire and release paths (where they reduce overhead
569 * considerably)--more can be added as necessary (DRK).
573 * These should reflect the layout of the bitfield embedded within
574 * the lck_rw_t structure (see i386/locks.h).
576 #define LCK_RW_INTERLOCK 0x1
577 #define LCK_RW_WANT_UPGRADE 0x2
578 #define LCK_RW_WANT_WRITE 0x4
579 #define LCK_R_WAITING 0x8
580 #define LCK_W_WAITING 0x10
582 #define RW_LOCK_SHARED_MASK ((LCK_RW_INTERLOCK<<16) | \
583 ((LCK_RW_WANT_UPGRADE|LCK_RW_WANT_WRITE) << 24))
585 * void lck_rw_lock_shared(lck_rw_t*)
589 Entry(lck_rw_lock_shared)
592 movl (%edx), %eax /* Load state bitfield and interlock */
593 testl $(RW_LOCK_SHARED_MASK), %eax /* Eligible for fastpath? */
596 incl %ecx /* Increment reader refcount */
598 cmpxchgl %ecx, (%edx) /* Attempt atomic exchange */
603 * Dtrace lockstat event: LS_LCK_RW_LOCK_SHARED_ACQUIRE
604 * Implemented by swapping between return and no-op instructions.
605 * See bsd/dev/dtrace/lockstat.c.
607 LOCKSTAT_LABEL(_lck_rw_lock_shared_lockstat_patch_point)
609 /* Fall thru when patched, counting on lock pointer in %edx */
610 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_SHARED_ACQUIRE, %edx)
618 jmp EXT(lck_rw_lock_shared_gen)
622 * lck_rw_type_t lck_rw_done(lck_rw_t*)
627 rwl_release_error_str:
628 .asciz "Releasing non-exclusive RW lock without a reader refcount!"
631 #define RW_LOCK_RELEASE_MASK ((LCK_RW_INTERLOCK<<16) | \
632 ((LCK_RW_WANT_UPGRADE|LCK_RW_WANT_WRITE|LCK_R_WAITING|LCK_W_WAITING) << 24))
636 movl (%edx), %eax /* Load state bitfield and interlock */
637 testl $(RW_LOCK_RELEASE_MASK), %eax /* Eligible for fastpath? */
640 /* Assert refcount */
641 testl $(0xFFFF), %ecx
643 movl $(rwl_release_error_str), S_ARG0
646 decl %ecx /* Decrement reader count */
648 cmpxchgl %ecx, (%edx)
650 movl $(RW_SHARED), %eax /* Indicate that the lock was shared */
652 /* Dtrace lockstat probe: LS_RW_DONE_RELEASE as reader */
653 LOCKSTAT_LABEL(_lck_rw_done_lockstat_patch_point)
656 * Note: Dtrace's convention is 0 ==> reader, which is
657 * a different absolute value than $(RW_SHARED)
658 * %edx contains the lock address already from the above
660 LOCKSTAT_RECORD2(LS_LCK_RW_DONE_RELEASE, %edx, $0)
661 movl $(RW_SHARED), %eax /* Indicate that the lock was shared */
669 jmp EXT(lck_rw_done_gen)
672 NONLEAF_ENTRY2(mutex_lock_spin,_mutex_lock_spin)
674 movl B_ARG0,%edx /* fetch lock pointer */
675 pushf /* save interrupt state */
678 CHECK_NO_SIMPLELOCKS()
679 CHECK_PREEMPTION_LEVEL()
681 movl M_ILK,%eax /* read interlock */
682 testl %eax,%eax /* unlocked? */
683 jne Lmls_ilk_loop /* no, go spin */
685 cli /* disable interrupts */
686 movl %gs:CPU_ACTIVE_THREAD,%ecx
688 /* eax == 0 at this point */
689 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
690 jne Lmls_ilk_fail /* branch on failure to spin loop */
692 movl M_LOCKED,%ecx /* get lock owner */
693 testl %ecx,%ecx /* is the mutex locked? */
694 jne Lml_fail /* yes, fall back to a normal mutex lock */
695 movl $(MUTEX_LOCKED_AS_SPIN),M_LOCKED /* indicate ownership as a spin lock */
698 movl %gs:CPU_ACTIVE_THREAD,%ecx
704 popf /* restore interrupt state */
705 leave /* return with the interlock held */
707 LOCKSTAT_LABEL(_mutex_lock_spin_lockstat_patch_point)
709 /* %edx contains the lock address from above */
710 LOCKSTAT_RECORD(LS_MUTEX_LOCK_SPIN_ACQUIRE, %edx)
715 popf /* restore interrupt state */
716 pushf /* resave interrupt state on stack */
720 movl M_ILK,%eax /* read interlock */
721 testl %eax,%eax /* unlocked? */
722 je Lmls_retry /* yes, go for it */
723 jmp Lmls_ilk_loop /* no, keep spinning */
726 NONLEAF_ENTRY2(mutex_lock,_mutex_lock)
728 movl B_ARG0,%edx /* fetch lock pointer */
729 pushf /* save interrupt state */
732 CHECK_NO_SIMPLELOCKS()
733 CHECK_PREEMPTION_LEVEL()
735 movl M_ILK,%eax /* is interlock held */
737 jne Lml_ilk_loop /* yes, go do the spin loop */
739 cli /* disable interrupts */
740 movl %gs:CPU_ACTIVE_THREAD,%ecx
742 /* eax == 0 at this point */
743 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
744 jne Lml_ilk_fail /* branch on failure to spin loop */
746 movl M_LOCKED,%ecx /* get lock owner */
747 testl %ecx,%ecx /* is the mutex locked? */
748 jne Lml_fail /* yes, we lose */
750 movl %gs:CPU_ACTIVE_THREAD,%ecx
758 cmpw $0,M_WAITERS /* are there any waiters? */
759 jne Lml_waiters /* yes, more work to do */
764 popf /* restore interrupt state */
767 LOCKSTAT_LABEL(_mutex_lock_lockstat_patch_point)
769 /* %edx still contains the lock pointer */
770 LOCKSTAT_RECORD(LS_MUTEX_LOCK_ACQUIRE, %edx)
775 * We got the mutex, but there are waiters. Update information
779 pushl %edx /* save mutex address */
781 call EXT(lck_mtx_lock_acquire)
783 popl %edx /* restore mutex address */
788 popf /* restore interrupt state */
789 pushf /* resave interrupt state on stack */
793 movl M_ILK,%eax /* read interlock */
794 testl %eax,%eax /* unlocked? */
795 je Lml_retry /* yes, go try to grab it */
796 jmp Lml_ilk_loop /* no - keep spinning */
800 * Check if the owner is on another processor and therefore
801 * we should try to spin before blocking.
803 testl $(OnProc),ACT_SPF(%ecx)
807 * Here if owner is on another processor:
808 * - release the interlock
809 * - spin on the holder until release or timeout
810 * - in either case re-acquire the interlock
811 * - if released, acquire it
812 * - otherwise drop thru to block.
815 movl %eax,M_ILK /* zero interlock */
817 pushf /* restore interrupt state */
819 push %edx /* lock address */
820 call EXT(lck_mtx_lock_spinwait) /* call out to do spinning */
822 movl B_ARG0,%edx /* refetch mutex address */
824 /* Re-acquire interlock - interrupts currently enabled */
825 movl M_ILK,%eax /* is interlock held */
827 jne Lml_ilk_reloop /* yes, go do the spin loop */
829 cli /* disable interrupts */
830 movl %gs:CPU_ACTIVE_THREAD,%ecx
832 /* eax == 0 at this point */
833 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
834 jne Lml_ilk_refail /* branch on failure to spin loop */
836 movl M_LOCKED,%ecx /* get lock owner */
837 testl %ecx,%ecx /* is the mutex free? */
838 je Lml_acquire /* yes, acquire */
841 CHECK_MYLOCK(M_THREAD)
843 pushl %edx /* push mutex address */
844 call EXT(lck_mtx_lock_wait) /* wait for the lock */
845 addl $8,%esp /* returns with interlock dropped */
846 movl B_ARG0,%edx /* refetch mutex address */
847 jmp Lml_restart /* and start over */
850 popf /* restore interrupt state */
851 pushf /* resave interrupt state on stack */
855 movl M_ILK,%eax /* read interlock */
856 testl %eax,%eax /* unlocked? */
857 je Lml_reget_retry /* yes, go try to grab it */
858 jmp Lml_ilk_reloop /* no - keep spinning */
862 NONLEAF_ENTRY2(mutex_try_spin,_mutex_try_spin)
864 movl B_ARG0,%edx /* fetch lock pointer */
865 pushf /* save interrupt state */
868 CHECK_NO_SIMPLELOCKS()
871 testl %eax,%eax /* is the interlock held? */
872 jne Lmts_ilk_loop /* yes, go to spin loop */
874 cli /* disable interrupts */
875 movl %gs:CPU_ACTIVE_THREAD,%ecx
877 /* eax == 0 at this point */
878 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
879 jne Lmts_ilk_fail /* branch on failure to spin loop */
881 movl M_LOCKED,%ecx /* get lock owner */
882 testl %ecx,%ecx /* is the mutex locked? */
883 jne Lmt_fail /* yes, we lose */
885 movl $(MUTEX_LOCKED_AS_SPIN),M_LOCKED /* indicate ownership as a spin lock */
888 movl %gs:CPU_ACTIVE_THREAD,%ecx
893 PREEMPTION_DISABLE /* no, return with interlock held */
894 popf /* restore interrupt state */
898 LOCKSTAT_LABEL(_mutex_try_spin_lockstat_patch_point)
900 /* %edx inherits the lock pointer from above */
901 LOCKSTAT_RECORD(LS_MUTEX_TRY_SPIN_ACQUIRE, %edx)
907 popf /* restore interrupt state */
908 pushf /* resave interrupt state on stack */
913 * need to do this check outside of the interlock in
914 * case this lock is held as a simple lock which means
915 * we won't be able to take the interlock
918 testl %eax,%eax /* is the mutex locked? */
919 jne Lmt_fail_no_ilk /* yes, go return failure */
921 movl M_ILK,%eax /* read interlock */
922 testl %eax,%eax /* unlocked? */
923 je Lmts_retry /* yes, go try to grab it */
924 jmp Lmts_ilk_loop /* keep spinning */
928 NONLEAF_ENTRY2(mutex_try,_mutex_try)
930 movl B_ARG0,%edx /* fetch lock pointer */
931 pushf /* save interrupt state */
934 CHECK_NO_SIMPLELOCKS()
936 movl M_ILK,%eax /* read interlock */
937 testl %eax,%eax /* unlocked? */
938 jne Lmt_ilk_loop /* yes, go try to grab it */
940 cli /* disable interrupts */
941 movl %gs:CPU_ACTIVE_THREAD,%ecx
943 /* eax == 0 at this point */
944 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
945 jne Lmt_ilk_fail /* branch on failure to spin loop */
947 movl M_LOCKED,%ecx /* get lock owner */
948 testl %ecx,%ecx /* is the mutex locked? */
949 jne Lmt_fail /* yes, we lose */
951 movl %gs:CPU_ACTIVE_THREAD,%ecx
959 cmpw $0,M_WAITERS /* are there any waiters? */
960 jne Lmt_waiters /* yes, more work to do */
964 popf /* restore interrupt state */
969 LOCKSTAT_LABEL(_mutex_try_lockstat_patch_point)
971 /* inherit the lock pointer in %edx from above */
972 LOCKSTAT_RECORD(LS_MUTEX_TRY_LOCK_ACQUIRE, %edx)
978 pushl %edx /* save mutex address */
980 call EXT(lck_mtx_lock_acquire)
982 popl %edx /* restore mutex address */
986 popf /* restore interrupt state */
987 pushf /* resave interrupt state on stack */
992 * need to do this check outside of the interlock in
993 * case this lock is held as a simple lock which means
994 * we won't be able to take the interlock
996 movl M_LOCKED,%eax /* get lock owner */
997 testl %eax,%eax /* is the mutex locked? */
998 jne Lmt_fail_no_ilk /* yes, go return failure */
1000 movl M_ILK,%eax /* read interlock */
1001 testl %eax,%eax /* unlocked? */
1002 je Lmt_retry /* yes, go try to grab it */
1003 jmp Lmt_ilk_loop /* no - keep spinning */
1011 popf /* restore interrupt state */
1016 LEAF_ENTRY(mutex_convert_spin)
1017 movl L_ARG0,%edx /* fetch lock pointer */
1019 movl M_LOCKED,%ecx /* is this the spin variant of the mutex */
1020 cmpl $(MUTEX_LOCKED_AS_SPIN),%ecx
1021 jne Lmcs_exit /* already owned as a mutex, just return */
1023 movl M_ILK,%ecx /* convert from spin version to mutex */
1024 movl %ecx,M_LOCKED /* take control of the mutex */
1026 cmpw $0,M_WAITERS /* are there any waiters? */
1027 jne Lmcs_waiters /* yes, more work to do */
1031 movl %ecx,M_ILK /* clear interlock */
1035 LOCKSTAT_LABEL(_mutex_convert_spin_lockstat_patch_point)
1037 /* inherit %edx from above */
1038 LOCKSTAT_RECORD(LS_MUTEX_CONVERT_SPIN_ACQUIRE, %edx)
1044 pushl %edx /* save mutex address */
1046 call EXT(lck_mtx_lock_acquire)
1048 popl %edx /* restore mutex address */
1053 NONLEAF_ENTRY(mutex_unlock)
1054 movl B_ARG0,%edx /* fetch lock pointer */
1056 movl M_LOCKED,%ecx /* is this the spin variant of the mutex */
1057 cmpl $(MUTEX_LOCKED_AS_SPIN),%ecx
1058 jne Lmu_enter /* no, go treat like a real mutex */
1060 cmpw $0,M_WAITERS /* are there any waiters? */
1061 jne Lmus_wakeup /* yes, more work to do */
1065 movl %ecx,M_LOCKED /* yes, clear the spin indicator */
1066 movl %ecx,M_ILK /* release the interlock */
1067 PREEMPTION_ENABLE /* and re-enable preemption */
1070 LOCKSTAT_LABEL(_mutex_unlock_lockstat_patch_point)
1072 /* inherit lock pointer in %edx from above */
1073 LOCKSTAT_RECORD(LS_MUTEX_UNLOCK_RELEASE, %edx)
1078 pushl %edx /* save mutex address */
1079 pushl %edx /* push mutex address */
1080 call EXT(lck_mtx_unlockspin_wakeup) /* yes, wake a thread */
1082 popl %edx /* restore mutex pointer */
1086 pushf /* save interrupt state */
1089 CHECK_THREAD(M_THREAD)
1091 movl M_ILK,%eax /* read interlock */
1092 testl %eax,%eax /* unlocked? */
1093 jne Lmu_ilk_loop /* yes, go try to grab it */
1095 cli /* disable interrupts */
1096 movl %gs:CPU_ACTIVE_THREAD,%ecx
1098 /* eax == 0 at this point */
1099 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
1100 jne Lmu_ilk_fail /* branch on failure to spin loop */
1102 cmpw $0,M_WAITERS /* are there any waiters? */
1103 jne Lmu_wakeup /* yes, more work to do */
1107 movl $0,M_THREAD /* disown thread */
1110 movl %ecx,M_LOCKED /* unlock the mutex */
1111 movl %ecx,M_ILK /* release the interlock */
1112 popf /* restore interrupt state */
1115 LOCKSTAT_LABEL(_mutex_unlock2_lockstat_patch_point)
1117 /* inherit %edx from above */
1118 LOCKSTAT_RECORD(LS_MUTEX_UNLOCK_RELEASE, %edx)
1123 popf /* restore interrupt state */
1124 pushf /* resave interrupt state on stack */
1128 movl M_ILK,%eax /* read interlock */
1129 testl %eax,%eax /* unlocked? */
1130 je Lmu_retry /* yes, go try to grab it */
1131 jmp Lmu_ilk_loop /* no - keep spinning */
1135 pushl %edx /* push mutex address */
1136 call EXT(lck_mtx_unlock_wakeup)/* yes, wake a thread */
1138 movl B_ARG0,%edx /* restore lock pointer */
1142 * void lck_mtx_assert(lck_mtx_t* l, unsigned int)
1143 * void _mutex_assert(mutex_t, unsigned int)
1144 * Takes the address of a lock, and an assertion type as parameters.
1145 * The assertion can take one of two forms determine by the type
1146 * parameter: either the lock is held by the current thread, and the
1147 * type is LCK_MTX_ASSERT_OWNED, or it isn't and the type is
1148 * LCK_MTX_ASSERT_NOT_OWNED. Calls panic on assertion failure.
1152 Entry(lck_mtx_assert)
1153 Entry(_mutex_assert)
1154 movl S_ARG0,%edx /* Load lock address */
1155 movl %gs:CPU_ACTIVE_THREAD,%ecx /* Load current thread */
1157 cmpl $(MUTEX_IND),M_ITAG /* Is this an indirect mutex? */
1158 cmove M_PTR,%edx /* If so, take indirection */
1160 movl M_LOCKED,%eax /* Load lock word */
1161 cmpl $(MUTEX_LOCKED_AS_SPIN),%eax /* check for spin variant */
1162 cmove M_ILK,%eax /* yes, spin lock owner is in the interlock */
1164 cmpl $(MUTEX_ASSERT_OWNED),S_ARG1 /* Determine assert type */
1165 jne 2f /* Assert ownership? */
1166 cmpl %eax,%ecx /* Current thread match? */
1167 jne 3f /* no, go panic */
1168 1: /* yes, we own it */
1169 ret /* just return */
1171 cmpl %eax,%ecx /* Current thread match? */
1172 jne 1b /* No, return */
1173 movl %edx,S_ARG1 /* Prep assertion failure */
1174 movl $(mutex_assert_owned_str),S_ARG0
1177 movl %edx,S_ARG1 /* Prep assertion failure */
1178 movl $(mutex_assert_not_owned_str),S_ARG0
1183 mutex_assert_not_owned_str:
1184 .asciz "mutex (%p) not owned\n"
1185 mutex_assert_owned_str:
1186 .asciz "mutex (%p) owned\n"
1189 /* This preprocessor define controls whether the R-M-W update of the
1190 * per-group statistics elements are atomic (LOCK-prefixed)
1191 * Enabled by default.
1193 #define ATOMIC_STAT_UPDATES 1
1195 #if defined(ATOMIC_STAT_UPDATES)
1196 #define LOCK_IF_ATOMIC_STAT_UPDATES lock
1198 #define LOCK_IF_ATOMIC_STAT_UPDATES
1199 #endif /* ATOMIC_STAT_UPDATES */
1204 * lck_mtx_try_lock()
1205 * lck_mutex_unlock()
1206 * lck_mtx_lock_spin()
1207 * lck_mtx_convert_spin()
1209 * These are variants of mutex_lock(), mutex_try(), mutex_unlock()
1210 * mutex_lock_spin and mutex_convert_spin without
1211 * DEBUG checks (which require fields not present in lck_mtx_t's).
1214 NONLEAF_ENTRY(lck_mtx_lock_spin)
1216 movl B_ARG0,%edx /* fetch lock pointer */
1217 pushf /* save interrupt state */
1219 CHECK_NO_SIMPLELOCKS()
1220 CHECK_PREEMPTION_LEVEL()
1222 movl M_ILK,%eax /* read interlock */
1223 testl %eax,%eax /* unlocked? */
1224 jne Llmls_eval_ilk /* no, go see if indirect */
1226 cli /* disable interrupts */
1227 movl %gs:CPU_ACTIVE_THREAD,%ecx
1229 /* eax == 0 at this point */
1230 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
1231 jne Llmls_ilk_fail /* branch on failure to spin loop */
1233 movl M_LOCKED,%ecx /* get lock owner */
1234 testl %ecx,%ecx /* is the mutex locked? */
1235 jne Llml_fail /* yes, fall back to a normal mutex */
1238 movl $(MUTEX_LOCKED_AS_SPIN),M_LOCKED /* indicate ownership as a spin lock */
1240 popf /* restore interrupt state */
1241 NONLEAF_RET /* return with the interlock held */
1244 popf /* restore interrupt state */
1245 pushf /* resave interrupt state on stack */
1249 movl M_ILK,%eax /* read interlock */
1250 testl %eax,%eax /* unlocked? */
1251 je Llmls_retry /* yes - go try to grab it */
1253 cmpl $(MUTEX_DESTROYED),%eax /* check to see if its marked destroyed */
1254 jne Llmls_ilk_loop /* no - keep spinning */
1257 call EXT(lck_mtx_interlock_panic)
1259 * shouldn't return from here, but just in case
1266 cmpl $(MUTEX_IND),M_ITAG /* Is this an indirect mutex? */
1267 cmove M_PTR,%edx /* If so, take indirection */
1268 jne Llmls_ilk_loop /* If not, go to spin loop */
1271 pushl %esi /* Used to hold the lock group ptr */
1272 pushl %edi /* Used for stat update records */
1273 movl MUTEX_GRP(%edx),%esi /* Load lock group */
1274 xorl %edi,%edi /* Clear stat update records */
1275 /* 64-bit increment of acquire attempt statistic (per-group) */
1276 LOCK_IF_ATOMIC_STAT_UPDATES
1277 addl $1, GRP_MTX_STAT_UTIL(%esi)
1279 incl GRP_MTX_STAT_UTIL+4(%esi)
1281 movl M_ILK,%eax /* read interlock */
1282 testl %eax,%eax /* unlocked? */
1283 jne Llmls_ext_ilk_loop /* no, go to spin loop */
1285 cli /* disable interrupts */
1286 movl %gs:CPU_ACTIVE_THREAD,%ecx
1288 /* eax == 0 at this point */
1289 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
1290 jne Llmls_ext_ilk_fail /* branch on failure to retry */
1292 movl M_LOCKED,%ecx /* get lock owner */
1293 testl %ecx,%ecx /* is the mutex locked? */
1294 jne Llml_ext_fail /* yes, we lose */
1302 * Slow path: call out to do the spinning.
1306 popf /* restore interrupt state */
1310 movl M_ILK,%eax /* read interlock */
1311 testl %eax,%eax /* unlocked? */
1312 je Llmls_ext_retry /* yes - go try to grab it */
1314 cmpl $(MUTEX_DESTROYED),%eax /* check to see if its marked destroyed */
1315 jne Llmls_ext_ilk_loop /* no - keep spinning */
1318 call EXT(lck_mtx_interlock_panic)
1320 * shouldn't return from here, but just in case
1323 jmp Llmls_ext_ilk_loop /* no - keep spinning */
1327 NONLEAF_ENTRY(lck_mtx_lock)
1329 movl B_ARG0,%edx /* fetch lock pointer */
1330 pushf /* save interrupt state */
1332 CHECK_NO_SIMPLELOCKS()
1333 CHECK_PREEMPTION_LEVEL()
1335 movl M_ILK,%eax /* read interlock */
1336 testl %eax,%eax /* unlocked? */
1337 jne Llml_eval_ilk /* no, go see if indirect */
1339 cli /* disable interrupts */
1340 movl %gs:CPU_ACTIVE_THREAD,%ecx
1342 /* eax == 0 at this point */
1343 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
1344 jne Llml_ilk_fail /* branch on failure to spin loop */
1346 movl M_LOCKED,%ecx /* get lock owner */
1347 testl %ecx,%ecx /* is the mutex locked? */
1348 jne Llml_fail /* yes, we lose */
1350 movl %gs:CPU_ACTIVE_THREAD,%ecx
1353 cmpw $0,M_WAITERS /* are there any waiters? */
1354 jne Lml_waiters /* yes, more work to do */
1359 popf /* restore interrupt state */
1362 LOCKSTAT_LABEL(_lck_mtx_lock_lockstat_patch_point)
1364 /* inherit lock pointer in %edx above */
1365 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_ACQUIRE, %edx)
1370 pushl %edx /* save mutex address */
1372 call EXT(lck_mtx_lock_acquire)
1374 popl %edx /* restore mutex address */
1379 popf /* restore interrupt state */
1380 pushf /* resave interrupt state on stack */
1384 movl M_ILK,%eax /* read interlock */
1385 testl %eax,%eax /* unlocked? */
1386 je Llml_retry /* yes - go try to grab it */
1388 cmpl $(MUTEX_DESTROYED),%eax /* check to see if its marked destroyed */
1389 jne Llml_ilk_loop /* no - keep spinning */
1392 call EXT(lck_mtx_interlock_panic)
1394 * shouldn't return from here, but just in case
1397 jmp Llml_ilk_loop /* no - keep spinning */
1401 * Check if the owner is on another processor and therefore
1402 * we should try to spin before blocking.
1404 testl $(OnProc),ACT_SPF(%ecx)
1408 * Here if owner is on another processor:
1409 * - release the interlock
1410 * - spin on the holder until release or timeout
1411 * - in either case re-acquire the interlock
1412 * - if released, acquire it
1413 * - otherwise drop thru to block.
1416 movl %eax,M_ILK /* zero interlock */
1418 pushf /* restore interrupt state */
1419 pushl %edx /* save mutex address */
1421 call EXT(lck_mtx_lock_spinwait)
1423 popl %edx /* restore mutex address */
1425 /* Re-acquire interlock */
1426 movl M_ILK,%eax /* read interlock */
1427 testl %eax,%eax /* unlocked? */
1428 jne Llml_ilk_refail /* no, go to spin loop */
1430 cli /* disable interrupts */
1431 movl %gs:CPU_ACTIVE_THREAD,%ecx
1433 /* eax == 0 at this point */
1434 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
1435 jne Llml_ilk_refail /* branch on failure to retry */
1437 movl M_LOCKED,%ecx /* get lock owner */
1438 testl %ecx,%ecx /* is the mutex free? */
1439 je Llml_acquire /* yes, acquire */
1442 CHECK_MYLOCK(M_THREAD)
1443 pushl %edx /* save mutex address */
1445 pushl %edx /* push mutex address */
1447 * N.B.: lck_mtx_lock_wait is called here with interrupts disabled
1448 * Consider reworking.
1450 call EXT(lck_mtx_lock_wait) /* wait for the lock */
1452 popl %edx /* restore mutex address */
1453 jmp Llml_restart /* and start over */
1456 popf /* restore interrupt state */
1457 pushf /* resave interrupt state on stack */
1461 movl M_ILK,%eax /* read interlock */
1462 testl %eax,%eax /* unlocked? */
1463 je Llml_reget_retry /* yes - go try to grab it */
1465 cmpl $(MUTEX_DESTROYED),%eax /* check to see if its marked destroyed */
1466 jne Llml_ilk_reloop /* no - keep spinning */
1469 call EXT(lck_mtx_interlock_panic)
1471 * shouldn't return from here, but just in case
1474 jmp Llml_ilk_reloop /* no - keep spinning */
1478 cmpl $(MUTEX_IND),M_ITAG /* Is this an indirect mutex? */
1479 cmove M_PTR,%edx /* If so, take indirection */
1480 jne Llml_ilk_loop /* If not, go to spin loop */
1483 * Entry into statistics codepath for lck_mtx_lock:
1484 * EDX: real lock pointer
1485 * first dword on stack contains flags
1488 /* Enable this preprocessor define to record the first miss alone
1489 * By default, we count every miss, hence multiple misses may be
1490 * recorded for a single lock acquire attempt via lck_mtx_lock
1492 #undef LOG_FIRST_MISS_ALONE
1495 * N.B.: On x86, statistics are currently recorded for all indirect mutexes.
1496 * Also, only the acquire attempt count (GRP_MTX_STAT_UTIL) is maintained
1497 * as a 64-bit quantity (this matches the existing PowerPC implementation,
1498 * and the new x86 specific statistics are also maintained as 32-bit
1503 pushl %esi /* Used to hold the lock group ptr */
1504 pushl %edi /* Used for stat update records */
1505 movl MUTEX_GRP(%edx),%esi /* Load lock group */
1506 xorl %edi,%edi /* Clear stat update records */
1507 /* 64-bit increment of acquire attempt statistic (per-group) */
1508 LOCK_IF_ATOMIC_STAT_UPDATES
1509 addl $1, GRP_MTX_STAT_UTIL(%esi)
1511 incl GRP_MTX_STAT_UTIL+4(%esi)
1513 movl M_ILK,%eax /* read interlock */
1514 testl %eax,%eax /* unlocked? */
1515 jne Llml_ext_ilk_loop /* no, go to spin loop */
1518 movl %gs:CPU_ACTIVE_THREAD,%ecx
1520 /* eax == 0 at this point */
1521 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
1522 jne Llml_ext_ilk_fail /* branch on failure to retry */
1524 movl M_LOCKED,%ecx /* get lock owner */
1525 testl %ecx,%ecx /* is the mutex locked? */
1526 jne Llml_ext_fail /* yes, we lose */
1529 movl %gs:CPU_ACTIVE_THREAD,%ecx
1532 cmpw $0,M_WAITERS /* are there any waiters? */
1533 jne Llml_ext_waiters /* yes, more work to do */
1540 popf /* restore interrupt state */
1543 LOCKSTAT_LABEL(_lck_mtx_lock_ext_lockstat_patch_point)
1545 /* inherit lock pointer in %edx above */
1546 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_LOCK_ACQUIRE, %edx)
1551 pushl %edx /* save mutex address */
1553 call EXT(lck_mtx_lock_acquire)
1555 popl %edx /* restore mutex address */
1562 popf /* restore interrupt state */
1566 movl M_ILK,%eax /* read interlock */
1567 testl %eax,%eax /* unlocked? */
1568 je Llml_ext_get_hw /* yes - go try to grab it */
1570 cmpl $(MUTEX_DESTROYED),%eax /* check to see if its marked destroyed */
1571 jne Llml_ext_ilk_loop /* no - keep spinning */
1574 call EXT(lck_mtx_interlock_panic)
1576 * shouldn't return from here, but just in case
1579 jmp Llml_ext_ilk_loop
1583 #ifdef LOG_FIRST_MISS_ALONE
1586 #endif /* LOG_FIRST_MISS_ALONE */
1587 /* Record that a lock acquire attempt missed (per-group statistic) */
1588 LOCK_IF_ATOMIC_STAT_UPDATES
1589 incl GRP_MTX_STAT_MISS(%esi)
1590 #ifdef LOG_FIRST_MISS_ALONE
1592 #endif /* LOG_FIRST_MISS_ALONE */
1595 * Check if the owner is on another processor and therefore
1596 * we should try to spin before blocking.
1598 testl $(OnProc),ACT_SPF(%ecx)
1601 * Record the "direct wait" statistic, which indicates if a
1602 * miss proceeded to block directly without spinning--occurs
1603 * if the owner of the mutex isn't running on another processor
1604 * at the time of the check.
1606 LOCK_IF_ATOMIC_STAT_UPDATES
1607 incl GRP_MTX_STAT_DIRECT_WAIT(%esi)
1611 * Here if owner is on another processor:
1612 * - release the interlock
1613 * - spin on the holder until release or timeout
1614 * - in either case re-acquire the interlock
1615 * - if released, acquire it
1616 * - otherwise drop thru to block.
1619 movl %eax,M_ILK /* zero interlock */
1621 pushl 8(%esp) /* Make another copy of EFLAGS image */
1622 popf /* Restore interrupt state */
1623 pushl %edx /* save mutex address */
1625 call EXT(lck_mtx_lock_spinwait)
1627 popl %edx /* restore mutex address */
1629 /* Re-acquire interlock */
1630 movl M_ILK,%eax /* read interlock */
1631 testl %eax,%eax /* unlocked? */
1632 jne Llml_ext_ilk_refail /* no, go to spin loop */
1633 Llml_ext_reget_retry:
1634 cli /* disable interrupts */
1635 movl %gs:CPU_ACTIVE_THREAD,%ecx
1637 /* eax == 0 at this point */
1638 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
1639 jne Llml_ext_ilk_refail /* branch on failure to spin loop */
1641 movl M_LOCKED,%ecx /* get lock owner */
1642 testl %ecx,%ecx /* is the mutex free? */
1643 je Llml_ext_acquire /* yes, acquire */
1646 /* If we wanted to count waits just once per lock acquire, we'd
1647 * skip over the stat update here
1649 LOCK_IF_ATOMIC_STAT_UPDATES
1650 /* Record that a lock miss proceeded to block */
1651 incl GRP_MTX_STAT_WAIT(%esi)
1653 CHECK_MYLOCK(M_THREAD)
1654 pushl %edx /* save mutex address */
1656 pushl %edx /* push mutex address */
1658 * N.B.: lck_mtx_lock_wait is called here with interrupts disabled
1659 * Consider reworking.
1661 call EXT(lck_mtx_lock_wait) /* wait for the lock */
1663 popl %edx /* restore mutex address */
1664 jmp Llml_ext_restart /* and start over */
1666 Llml_ext_ilk_refail:
1669 popf /* restore interrupt state */
1671 Llml_ext_ilk_reloop:
1673 movl M_ILK,%eax /* read interlock */
1674 testl %eax,%eax /* unlocked? */
1675 je Llml_ext_reget_retry /* yes - go try to grab it */
1677 cmpl $(MUTEX_DESTROYED),%eax /* check to see if its marked destroyed */
1678 jne Llml_ext_ilk_reloop /* no - keep spinning */
1681 call EXT(lck_mtx_interlock_panic)
1683 * shouldn't return from here, but just in case
1686 jmp Llml_ext_ilk_reloop
1690 NONLEAF_ENTRY(lck_mtx_try_lock_spin)
1692 movl B_ARG0,%edx /* fetch lock pointer */
1693 pushf /* save interrupt state */
1695 CHECK_NO_SIMPLELOCKS()
1696 CHECK_PREEMPTION_LEVEL()
1698 movl M_ILK,%eax /* read interlock */
1699 testl %eax,%eax /* unlocked? */
1700 jne Llmts_eval_ilk /* no, go see if indirect */
1702 cli /* disable interrupts */
1703 movl %gs:CPU_ACTIVE_THREAD,%ecx
1705 /* eax == 0 at this point */
1706 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
1707 jne Llmts_ilk_fail /* branch on failure to retry */
1709 movl M_LOCKED,%ecx /* get lock owner */
1710 testl %ecx,%ecx /* is the mutex locked? */
1711 jne Llmt_fail /* yes, we lose */
1713 movl $(MUTEX_LOCKED_AS_SPIN),M_LOCKED /* no, indicate ownership as a spin lock */
1714 PREEMPTION_DISABLE /* and return with interlock held */
1716 movl $1,%eax /* return success */
1717 popf /* restore interrupt state */
1720 LOCKSTAT_LABEL(_lck_mtx_try_lock_spin_lockstat_patch_point)
1722 /* inherit lock pointer in %edx above */
1723 LOCKSTAT_RECORD(LS_LCK_MTX_TRY_SPIN_LOCK_ACQUIRE, %edx)
1724 movl $1,%eax /* return success */
1729 popf /* restore interrupt state */
1730 pushf /* resave interrupt state */
1735 * need to do this check outside of the interlock in
1736 * case this lock is held as a simple lock which means
1737 * we won't be able to take the interlock
1739 movl M_LOCKED,%eax /* get lock owner */
1740 testl %eax,%eax /* is the mutex locked? */
1741 jne Llmt_fail_no_ilk /* yes, go return failure */
1743 movl M_ILK,%eax /* read interlock */
1744 testl %eax,%eax /* unlocked? */
1745 je Llmts_retry /* yes - go try to grab it */
1747 cmpl $(MUTEX_DESTROYED),%eax /* check to see if its marked destroyed */
1748 jne Llmts_ilk_loop /* no - keep spinning */
1751 call EXT(lck_mtx_interlock_panic)
1753 * shouldn't return from here, but just in case
1759 cmpl $(MUTEX_IND),M_ITAG /* Is this an indirect mutex? */
1760 cmove M_PTR,%edx /* If so, take indirection */
1761 jne Llmts_ilk_loop /* If not, go to spin loop */
1764 * bump counter on indirect lock
1766 pushl %esi /* Used to hold the lock group ptr */
1767 movl MUTEX_GRP(%edx),%esi /* Load lock group */
1768 /* 64-bit increment of acquire attempt statistic (per-group) */
1769 LOCK_IF_ATOMIC_STAT_UPDATES
1770 addl $1, GRP_MTX_STAT_UTIL(%esi)
1772 incl GRP_MTX_STAT_UTIL+4(%esi)
1779 NONLEAF_ENTRY(lck_mtx_try_lock)
1781 movl B_ARG0,%edx /* fetch lock pointer */
1782 pushf /* save interrupt state */
1784 CHECK_NO_SIMPLELOCKS()
1785 CHECK_PREEMPTION_LEVEL()
1787 movl M_ILK,%eax /* read interlock */
1788 testl %eax,%eax /* unlocked? */
1789 jne Llmt_eval_ilk /* no, go see if indirect */
1791 cli /* disable interrupts */
1792 movl %gs:CPU_ACTIVE_THREAD,%ecx
1794 /* eax == 0 at this point */
1795 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
1796 jne Llmt_ilk_fail /* branch on failure to retry */
1798 movl M_LOCKED,%ecx /* get lock owner */
1799 testl %ecx,%ecx /* is the mutex locked? */
1800 jne Llmt_fail /* yes, we lose */
1802 movl %gs:CPU_ACTIVE_THREAD,%ecx
1805 cmpw $0,M_WAITERS /* are there any waiters? */
1806 jne Llmt_waiters /* yes, more work to do */
1811 popf /* restore interrupt state */
1813 movl $1,%eax /* return success */
1816 /* Dtrace probe: LS_LCK_MTX_TRY_LOCK_ACQUIRE */
1817 LOCKSTAT_LABEL(_lck_mtx_try_lock_lockstat_patch_point)
1819 /* inherit lock pointer in %edx from above */
1820 LOCKSTAT_RECORD(LS_LCK_MTX_TRY_LOCK_ACQUIRE, %edx)
1821 movl $1,%eax /* return success */
1826 pushl %edx /* save mutex address */
1828 call EXT(lck_mtx_lock_acquire)
1830 popl %edx /* restore mutex address */
1834 popf /* restore interrupt state */
1835 pushf /* resave interrupt state */
1840 * need to do this check outside of the interlock in
1841 * case this lock is held as a simple lock which means
1842 * we won't be able to take the interlock
1844 movl M_LOCKED,%eax /* get lock owner */
1845 testl %eax,%eax /* is the mutex locked? */
1846 jne Llmt_fail_no_ilk /* yes, go return failure */
1848 movl M_ILK,%eax /* read interlock */
1849 testl %eax,%eax /* unlocked? */
1850 je Llmt_retry /* yes - go try to grab it */
1852 cmpl $(MUTEX_DESTROYED),%eax /* check to see if its marked destroyed */
1853 jne Llmt_ilk_loop /* no - keep spinning */
1856 call EXT(lck_mtx_interlock_panic)
1858 * shouldn't return from here, but just in case
1864 xorl %eax,%eax /* Zero interlock value */
1868 popf /* restore interrupt state */
1871 jne Llmt_fail_indirect
1874 /* Note that we don't record a dtrace event for trying and missing */
1878 pushl %esi /* Used to hold the lock group ptr */
1879 movl MUTEX_GRP(%edx),%esi /* Load lock group */
1881 /* Record mutex acquire attempt miss statistic */
1882 LOCK_IF_ATOMIC_STAT_UPDATES
1883 incl GRP_MTX_STAT_MISS(%esi)
1890 cmpl $(MUTEX_IND),M_ITAG /* Is this an indirect mutex? */
1891 cmove M_PTR,%edx /* If so, take indirection */
1892 jne Llmt_ilk_loop /* If not, go to spin loop */
1895 * bump counter for indirect lock
1897 pushl %esi /* Used to hold the lock group ptr */
1898 movl MUTEX_GRP(%edx),%esi /* Load lock group */
1900 /* 64-bit increment of acquire attempt statistic (per-group) */
1901 LOCK_IF_ATOMIC_STAT_UPDATES
1902 addl $1, GRP_MTX_STAT_UTIL(%esi)
1904 incl GRP_MTX_STAT_UTIL+4(%esi)
1911 LEAF_ENTRY(lck_mtx_convert_spin)
1912 movl L_ARG0,%edx /* fetch lock pointer */
1914 cmpl $(MUTEX_IND),M_ITAG /* Is this an indirect mutex? */
1915 cmove M_PTR,%edx /* If so, take indirection */
1917 movl M_LOCKED,%ecx /* is this the spin variant of the mutex */
1918 cmpl $(MUTEX_LOCKED_AS_SPIN),%ecx
1919 jne Llmcs_exit /* already owned as a mutex, just return */
1921 movl M_ILK,%ecx /* convert from spin version to mutex */
1922 movl %ecx,M_LOCKED /* take control of the mutex */
1924 cmpw $0,M_WAITERS /* are there any waiters? */
1925 jne Llmcs_waiters /* yes, more work to do */
1929 movl %ecx,M_ILK /* clear interlock */
1935 pushl %edx /* save mutex address */
1937 call EXT(lck_mtx_lock_acquire)
1939 popl %edx /* restore mutex address */
1944 NONLEAF_ENTRY(lck_mtx_unlock)
1946 movl B_ARG0,%edx /* fetch lock pointer */
1948 cmpl $(MUTEX_IND),M_ITAG /* Is this an indirect mutex? */
1949 cmove M_PTR,%edx /* If so, take indirection */
1951 movl M_LOCKED,%ecx /* is this the spin variant of the mutex */
1952 cmpl $(MUTEX_LOCKED_AS_SPIN),%ecx
1953 jne Llmu_enter /* no, go treat like a real mutex */
1955 cmpw $0,M_WAITERS /* are there any waiters? */
1956 jne Llmus_wakeup /* yes, more work to do */
1960 movl %eax,M_LOCKED /* clear spin indicator */
1961 movl %eax,M_ILK /* release the interlock */
1963 PREEMPTION_ENABLE /* and re-enable preemption */
1966 /* Dtrace: LS_LCK_MTX_UNLOCK_RELEASE */
1967 LOCKSTAT_LABEL(_lck_mtx_unlock_lockstat_patch_point)
1969 /* inherit lock pointer in %edx from above */
1970 LOCKSTAT_RECORD(LS_LCK_MTX_UNLOCK_RELEASE, %edx)
1975 pushl %edx /* save mutex address */
1976 pushl %edx /* push mutex address */
1977 call EXT(lck_mtx_unlockspin_wakeup) /* yes, wake a thread */
1979 popl %edx /* restore mutex pointer */
1984 pushf /* save interrupt state */
1986 movl M_ILK,%eax /* read interlock */
1987 testl %eax,%eax /* unlocked? */
1988 jne Llmu_ilk_loop /* no - go to spin loop */
1990 cli /* disable interrupts */
1991 movl %gs:CPU_ACTIVE_THREAD,%ecx
1993 /* eax == 0 at this point */
1994 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
1995 jne Llmu_ilk_fail /* branch on failure to spin loop */
1997 cmpw $0,M_WAITERS /* are there any waiters? */
1998 jne Llmu_wakeup /* yes, more work to do */
2002 movl %ecx,M_LOCKED /* unlock the mutex */
2003 movl %ecx,M_ILK /* clear the interlock */
2005 popf /* restore interrupt state */
2008 LOCKSTAT_LABEL(_lck_mtx_unlock2_lockstat_patch_point)
2010 /* inherit lock pointer in %edx above */
2011 LOCKSTAT_RECORD(LS_LCK_MTX_UNLOCK_RELEASE, %edx)
2016 popf /* restore interrupt state */
2017 pushf /* resave interrupt state */
2021 movl M_ILK,%eax /* read interlock */
2022 testl %eax,%eax /* unlocked? */
2023 je Llmu_retry /* yes - go try to grab it */
2025 cmpl $(MUTEX_DESTROYED),%eax /* check to see if its marked destroyed */
2026 jne Llmu_ilk_loop /* no - keep spinning */
2029 call EXT(lck_mtx_interlock_panic)
2031 * shouldn't return from here, but just in case
2037 pushl %edx /* save mutex address */
2039 pushl %edx /* push mutex address */
2040 call EXT(lck_mtx_unlock_wakeup)/* yes, wake a thread */
2042 popl %edx /* restore mutex pointer */
2044 movl %ecx,M_LOCKED /* unlock the mutex */
2048 popf /* restore interrupt state */
2052 /* Dtrace: LS_LCK_MTX_EXT_UNLOCK_RELEASE */
2053 LOCKSTAT_LABEL(_lck_mtx_ext_unlock_lockstat_patch_point)
2055 /* inherit lock pointer in %edx from above */
2056 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_UNLOCK_RELEASE, %edx)
2061 LEAF_ENTRY(lck_mtx_ilk_unlock)
2062 movl L_ARG0,%edx /* no indirection here */
2070 LEAF_ENTRY(_disable_preemption)
2073 #endif /* MACH_RT */
2076 LEAF_ENTRY(_enable_preemption)
2079 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
2081 pushl %gs:CPU_PREEMPTION_LEVEL
2086 2: String "_enable_preemption: preemption_level(%d) < 0!"
2089 #endif /* MACH_ASSERT */
2091 #endif /* MACH_RT */
2094 LEAF_ENTRY(_enable_preemption_no_check)
2097 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
2103 2: String "_enable_preemption_no_check: preemption_level <= 0!"
2106 #endif /* MACH_ASSERT */
2107 _ENABLE_PREEMPTION_NO_CHECK
2108 #endif /* MACH_RT */
2112 LEAF_ENTRY(_mp_disable_preemption)
2115 #endif /* MACH_RT */
2118 LEAF_ENTRY(_mp_enable_preemption)
2121 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
2123 pushl %gs:CPU_PREEMPTION_LEVEL
2128 2: String "_mp_enable_preemption: preemption_level (%d) <= 0!"
2131 #endif /* MACH_ASSERT */
2133 #endif /* MACH_RT */
2136 LEAF_ENTRY(_mp_enable_preemption_no_check)
2139 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
2145 2: String "_mp_enable_preemption_no_check: preemption_level <= 0!"
2148 #endif /* MACH_ASSERT */
2149 _ENABLE_PREEMPTION_NO_CHECK
2150 #endif /* MACH_RT */
2154 LEAF_ENTRY(i_bit_set)
2161 LEAF_ENTRY(i_bit_clear)
2169 LEAF_ENTRY(bit_lock)
2179 LEAF_ENTRY(bit_lock_try)
2185 LEAF_RET /* %eax better not be null ! */
2190 LEAF_ENTRY(bit_unlock)
2198 * Atomic primitives, prototyped in kern/simple_lock.h
2200 LEAF_ENTRY(hw_atomic_add)
2201 movl L_ARG0, %ecx /* Load address of operand */
2202 movl L_ARG1, %eax /* Load addend */
2205 xaddl %eax, (%ecx) /* Atomic exchange and add */
2206 addl %edx, %eax /* Calculate result */
2209 LEAF_ENTRY(hw_atomic_sub)
2210 movl L_ARG0, %ecx /* Load address of operand */
2211 movl L_ARG1, %eax /* Load subtrahend */
2215 xaddl %eax, (%ecx) /* Atomic exchange and add */
2216 addl %edx, %eax /* Calculate result */
2219 LEAF_ENTRY(hw_atomic_or)
2220 movl L_ARG0, %ecx /* Load address of operand */
2223 movl L_ARG1, %edx /* Load mask */
2226 cmpxchgl %edx, (%ecx) /* Atomic CAS */
2228 movl %edx, %eax /* Result */
2231 * A variant of hw_atomic_or which doesn't return a value.
2232 * The implementation is thus comparatively more efficient.
2235 LEAF_ENTRY(hw_atomic_or_noret)
2236 movl L_ARG0, %ecx /* Load address of operand */
2237 movl L_ARG1, %edx /* Load mask */
2239 orl %edx, (%ecx) /* Atomic OR */
2242 LEAF_ENTRY(hw_atomic_and)
2243 movl L_ARG0, %ecx /* Load address of operand */
2246 movl L_ARG1, %edx /* Load mask */
2249 cmpxchgl %edx, (%ecx) /* Atomic CAS */
2251 movl %edx, %eax /* Result */
2254 * A variant of hw_atomic_and which doesn't return a value.
2255 * The implementation is thus comparatively more efficient.
2258 LEAF_ENTRY(hw_atomic_and_noret)
2259 movl L_ARG0, %ecx /* Load address of operand */
2260 movl L_ARG1, %edx /* Load mask */
2262 andl %edx, (%ecx) /* Atomic OR */