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 */
424 addl %ecx,%eax /* fetch and timeout */
425 adcl $0,%edx /* add carry */
427 mov %eax,%ebx /* %ecx:%ebx is the timeout expiry */
430 * The inner-loop spin to look for the lock being freed.
432 mov $(INNER_LOOP_COUNT),%edx
434 PAUSE /* pause for hyper-threading */
435 movl (%edi),%eax /* spin checking lock value in cache */
437 je 6f /* zero => unlocked, try to grab it */
438 decl %edx /* decrement inner loop count */
439 jnz 5b /* time to check for timeout? */
442 * Here after spinning INNER_LOOP_COUNT times, check for timeout
444 rdtsc /* cyclecount into %edx:%eax */
445 cmpl %ecx,%edx /* compare high-order 32-bits */
446 jb 4b /* continue spinning if less, or */
447 cmpl %ebx,%eax /* compare low-order 32-bits */
448 jb 4b /* continue if less, else bail */
449 xor %eax,%eax /* with 0 return value */
456 * Here to try to grab the lock that now appears to be free
459 movl %gs:CPU_ACTIVE_THREAD,%edx
460 lock; cmpxchgl %edx,(%edi) /* try to acquire the HW lock */
461 jne 4b /* no - spin again */
462 movl $1,%eax /* yes */
468 * void hw_lock_unlock(hw_lock_t)
470 * Unconditionally release lock.
471 * MACH_RT: release preemption level.
473 LEAF_ENTRY(hw_lock_unlock)
474 movl L_ARG0,%edx /* fetch lock pointer */
475 movl $0,(%edx) /* clear the lock */
479 * void hw_lock_byte_unlock(uint8_t *lock_byte)
481 * Unconditionally release byte sized lock operand.
482 * MACH_RT: release preemption level.
485 LEAF_ENTRY(hw_lock_byte_unlock)
486 movl L_ARG0,%edx /* Load lock pointer */
487 movb $0,(%edx) /* Clear the lock byte */
492 * void i386_lock_unlock_with_flush(hw_lock_t)
494 * Unconditionally release lock, followed by a cacheline flush of
495 * the line corresponding to the lock dword. This routine is currently
496 * used with certain locks which are susceptible to lock starvation,
497 * minimizing cache affinity for lock acquisitions. A queued spinlock
498 * or other mechanism that ensures fairness would obviate the need
499 * for this routine, but ideally few or no spinlocks should exhibit
500 * enough contention to require such measures.
501 * MACH_RT: release preemption level.
503 LEAF_ENTRY(i386_lock_unlock_with_flush)
504 movl L_ARG0,%edx /* Fetch lock pointer */
505 movl $0,(%edx) /* Clear the lock */
506 mfence /* Serialize prior stores */
507 clflush (%edx) /* Write back and invalidate line */
512 * unsigned int hw_lock_try(hw_lock_t)
513 * MACH_RT: returns with preemption disabled on success.
515 LEAF_ENTRY(hw_lock_try)
516 movl L_ARG0,%edx /* fetch lock pointer */
518 movl %gs:CPU_ACTIVE_THREAD,%ecx
523 lock; cmpxchgl %ecx,(%edx) /* try to acquire the HW lock */
526 movl $1,%eax /* success */
530 PREEMPTION_ENABLE /* failure: release preemption... */
531 xorl %eax,%eax /* ...and return failure */
535 * unsigned int hw_lock_held(hw_lock_t)
536 * MACH_RT: doesn't change preemption state.
537 * N.B. Racy, of course.
539 LEAF_ENTRY(hw_lock_held)
540 movl L_ARG0,%edx /* fetch lock pointer */
542 movl (%edx),%eax /* check lock value */
545 cmovne %ecx,%eax /* 0 => unlocked, 1 => locked */
548 LEAF_ENTRY(mutex_init)
549 movl L_ARG0,%edx /* fetch lock pointer */
551 movl %eax,M_ILK /* clear interlock */
552 movl %eax,M_LOCKED /* clear locked flag */
553 movw %ax,M_WAITERS /* init waiter count */
554 movw %ax,M_PROMOTED_PRI
557 movl $ MUTEX_TAG,M_TYPE /* set lock type */
558 movl %eax,M_PC /* init caller pc */
559 movl %eax,M_THREAD /* and owning thread */
565 * Reader-writer lock fastpaths. These currently exist for the
566 * shared lock acquire and release paths (where they reduce overhead
567 * considerably)--more can be added as necessary (DRK).
571 * These should reflect the layout of the bitfield embedded within
572 * the lck_rw_t structure (see i386/locks.h).
574 #define LCK_RW_INTERLOCK 0x1
575 #define LCK_RW_WANT_UPGRADE 0x2
576 #define LCK_RW_WANT_WRITE 0x4
577 #define LCK_R_WAITING 0x8
578 #define LCK_W_WAITING 0x10
580 #define RW_LOCK_SHARED_MASK ((LCK_RW_INTERLOCK<<16) | \
581 ((LCK_RW_WANT_UPGRADE|LCK_RW_WANT_WRITE) << 24))
583 * void lck_rw_lock_shared(lck_rw_t*)
587 Entry(lck_rw_lock_shared)
590 movl (%edx), %eax /* Load state bitfield and interlock */
591 testl $(RW_LOCK_SHARED_MASK), %eax /* Eligible for fastpath? */
594 incl %ecx /* Increment reader refcount */
596 cmpxchgl %ecx, (%edx) /* Attempt atomic exchange */
601 * Dtrace lockstat event: LS_LCK_RW_LOCK_SHARED_ACQUIRE
602 * Implemented by swapping between return and no-op instructions.
603 * See bsd/dev/dtrace/lockstat.c.
605 LOCKSTAT_LABEL(_lck_rw_lock_shared_lockstat_patch_point)
607 /* Fall thru when patched, counting on lock pointer in %edx */
608 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_SHARED_ACQUIRE, %edx)
616 jmp EXT(lck_rw_lock_shared_gen)
620 * lck_rw_type_t lck_rw_done(lck_rw_t*)
625 rwl_release_error_str:
626 .asciz "Releasing non-exclusive RW lock without a reader refcount!"
629 #define RW_LOCK_RELEASE_MASK ((LCK_RW_INTERLOCK<<16) | \
630 ((LCK_RW_WANT_UPGRADE|LCK_RW_WANT_WRITE|LCK_R_WAITING|LCK_W_WAITING) << 24))
634 movl (%edx), %eax /* Load state bitfield and interlock */
635 testl $(RW_LOCK_RELEASE_MASK), %eax /* Eligible for fastpath? */
638 /* Assert refcount */
639 testl $(0xFFFF), %ecx
641 movl $(rwl_release_error_str), S_ARG0
644 decl %ecx /* Decrement reader count */
646 cmpxchgl %ecx, (%edx)
648 movl $(RW_SHARED), %eax /* Indicate that the lock was shared */
650 /* Dtrace lockstat probe: LS_RW_DONE_RELEASE as reader */
651 LOCKSTAT_LABEL(_lck_rw_done_lockstat_patch_point)
654 * Note: Dtrace's convention is 0 ==> reader, which is
655 * a different absolute value than $(RW_SHARED)
656 * %edx contains the lock address already from the above
658 LOCKSTAT_RECORD2(LS_LCK_RW_DONE_RELEASE, %edx, $0)
659 movl $(RW_SHARED), %eax /* Indicate that the lock was shared */
667 jmp EXT(lck_rw_done_gen)
670 NONLEAF_ENTRY2(mutex_lock_spin,_mutex_lock_spin)
672 movl B_ARG0,%edx /* fetch lock pointer */
673 pushf /* save interrupt state */
676 CHECK_NO_SIMPLELOCKS()
677 CHECK_PREEMPTION_LEVEL()
679 movl M_ILK,%eax /* read interlock */
680 testl %eax,%eax /* unlocked? */
681 jne Lmls_ilk_loop /* no, go spin */
683 cli /* disable interrupts */
684 movl %gs:CPU_ACTIVE_THREAD,%ecx
686 /* eax == 0 at this point */
687 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
688 jne Lmls_ilk_fail /* branch on failure to spin loop */
690 movl M_LOCKED,%ecx /* get lock owner */
691 testl %ecx,%ecx /* is the mutex locked? */
692 jne Lml_fail /* yes, fall back to a normal mutex lock */
693 movl $(MUTEX_LOCKED_AS_SPIN),M_LOCKED /* indicate ownership as a spin lock */
696 movl %gs:CPU_ACTIVE_THREAD,%ecx
702 popf /* restore interrupt state */
703 leave /* return with the interlock held */
705 LOCKSTAT_LABEL(_mutex_lock_spin_lockstat_patch_point)
707 /* %edx contains the lock address from above */
708 LOCKSTAT_RECORD(LS_MUTEX_LOCK_SPIN_ACQUIRE, %edx)
713 popf /* restore interrupt state */
714 pushf /* resave interrupt state on stack */
718 movl M_ILK,%eax /* read interlock */
719 testl %eax,%eax /* unlocked? */
720 je Lmls_retry /* yes, go for it */
721 jmp Lmls_ilk_loop /* no, keep spinning */
724 NONLEAF_ENTRY2(mutex_lock,_mutex_lock)
726 movl B_ARG0,%edx /* fetch lock pointer */
727 pushf /* save interrupt state */
730 CHECK_NO_SIMPLELOCKS()
731 CHECK_PREEMPTION_LEVEL()
733 movl M_ILK,%eax /* is interlock held */
735 jne Lml_ilk_loop /* yes, go do the spin loop */
737 cli /* disable interrupts */
738 movl %gs:CPU_ACTIVE_THREAD,%ecx
740 /* eax == 0 at this point */
741 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
742 jne Lml_ilk_fail /* branch on failure to spin loop */
744 movl M_LOCKED,%ecx /* get lock owner */
745 testl %ecx,%ecx /* is the mutex locked? */
746 jne Lml_fail /* yes, we lose */
748 movl %gs:CPU_ACTIVE_THREAD,%ecx
756 cmpw $0,M_WAITERS /* are there any waiters? */
757 jne Lml_waiters /* yes, more work to do */
762 popf /* restore interrupt state */
765 LOCKSTAT_LABEL(_mutex_lock_lockstat_patch_point)
767 /* %edx still contains the lock pointer */
768 LOCKSTAT_RECORD(LS_MUTEX_LOCK_ACQUIRE, %edx)
773 * We got the mutex, but there are waiters. Update information
777 pushl %edx /* save mutex address */
779 call EXT(lck_mtx_lock_acquire)
781 popl %edx /* restore mutex address */
786 popf /* restore interrupt state */
787 pushf /* resave interrupt state on stack */
791 movl M_ILK,%eax /* read interlock */
792 testl %eax,%eax /* unlocked? */
793 je Lml_retry /* yes, go try to grab it */
794 jmp Lml_ilk_loop /* no - keep spinning */
798 * Check if the owner is on another processor and therefore
799 * we should try to spin before blocking.
801 testl $(OnProc),ACT_SPF(%ecx)
805 * Here if owner is on another processor:
806 * - release the interlock
807 * - spin on the holder until release or timeout
808 * - in either case re-acquire the interlock
809 * - if released, acquire it
810 * - otherwise drop thru to block.
813 movl %eax,M_ILK /* zero interlock */
815 pushf /* restore interrupt state */
817 push %edx /* lock address */
818 call EXT(lck_mtx_lock_spinwait) /* call out to do spinning */
820 movl B_ARG0,%edx /* refetch mutex address */
822 /* Re-acquire interlock - interrupts currently enabled */
823 movl M_ILK,%eax /* is interlock held */
825 jne Lml_ilk_reloop /* yes, go do the spin loop */
827 cli /* disable interrupts */
828 movl %gs:CPU_ACTIVE_THREAD,%ecx
830 /* eax == 0 at this point */
831 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
832 jne Lml_ilk_refail /* branch on failure to spin loop */
834 movl M_LOCKED,%ecx /* get lock owner */
835 testl %ecx,%ecx /* is the mutex free? */
836 je Lml_acquire /* yes, acquire */
839 CHECK_MYLOCK(M_THREAD)
841 pushl %edx /* push mutex address */
842 call EXT(lck_mtx_lock_wait) /* wait for the lock */
843 addl $8,%esp /* returns with interlock dropped */
844 movl B_ARG0,%edx /* refetch mutex address */
845 jmp Lml_restart /* and start over */
848 popf /* restore interrupt state */
849 pushf /* resave interrupt state on stack */
853 movl M_ILK,%eax /* read interlock */
854 testl %eax,%eax /* unlocked? */
855 je Lml_reget_retry /* yes, go try to grab it */
856 jmp Lml_ilk_reloop /* no - keep spinning */
860 NONLEAF_ENTRY2(mutex_try_spin,_mutex_try_spin)
862 movl B_ARG0,%edx /* fetch lock pointer */
863 pushf /* save interrupt state */
866 CHECK_NO_SIMPLELOCKS()
869 testl %eax,%eax /* is the interlock held? */
870 jne Lmts_ilk_loop /* yes, go to spin loop */
872 cli /* disable interrupts */
873 movl %gs:CPU_ACTIVE_THREAD,%ecx
875 /* eax == 0 at this point */
876 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
877 jne Lmts_ilk_fail /* branch on failure to spin loop */
879 movl M_LOCKED,%ecx /* get lock owner */
880 testl %ecx,%ecx /* is the mutex locked? */
881 jne Lmt_fail /* yes, we lose */
883 movl $(MUTEX_LOCKED_AS_SPIN),M_LOCKED /* indicate ownership as a spin lock */
886 movl %gs:CPU_ACTIVE_THREAD,%ecx
891 PREEMPTION_DISABLE /* no, return with interlock held */
892 popf /* restore interrupt state */
896 LOCKSTAT_LABEL(_mutex_try_spin_lockstat_patch_point)
898 /* %edx inherits the lock pointer from above */
899 LOCKSTAT_RECORD(LS_MUTEX_TRY_SPIN_ACQUIRE, %edx)
905 popf /* restore interrupt state */
906 pushf /* resave interrupt state on stack */
911 * need to do this check outside of the interlock in
912 * case this lock is held as a simple lock which means
913 * we won't be able to take the interlock
916 testl %eax,%eax /* is the mutex locked? */
917 jne Lmt_fail_no_ilk /* yes, go return failure */
919 movl M_ILK,%eax /* read interlock */
920 testl %eax,%eax /* unlocked? */
921 je Lmts_retry /* yes, go try to grab it */
922 jmp Lmts_ilk_loop /* keep spinning */
926 NONLEAF_ENTRY2(mutex_try,_mutex_try)
928 movl B_ARG0,%edx /* fetch lock pointer */
929 pushf /* save interrupt state */
932 CHECK_NO_SIMPLELOCKS()
934 movl M_ILK,%eax /* read interlock */
935 testl %eax,%eax /* unlocked? */
936 jne Lmt_ilk_loop /* yes, go try to grab it */
938 cli /* disable interrupts */
939 movl %gs:CPU_ACTIVE_THREAD,%ecx
941 /* eax == 0 at this point */
942 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
943 jne Lmt_ilk_fail /* branch on failure to spin loop */
945 movl M_LOCKED,%ecx /* get lock owner */
946 testl %ecx,%ecx /* is the mutex locked? */
947 jne Lmt_fail /* yes, we lose */
949 movl %gs:CPU_ACTIVE_THREAD,%ecx
957 cmpw $0,M_WAITERS /* are there any waiters? */
958 jne Lmt_waiters /* yes, more work to do */
962 popf /* restore interrupt state */
967 LOCKSTAT_LABEL(_mutex_try_lockstat_patch_point)
969 /* inherit the lock pointer in %edx from above */
970 LOCKSTAT_RECORD(LS_MUTEX_TRY_LOCK_ACQUIRE, %edx)
976 pushl %edx /* save mutex address */
978 call EXT(lck_mtx_lock_acquire)
980 popl %edx /* restore mutex address */
984 popf /* restore interrupt state */
985 pushf /* resave interrupt state on stack */
990 * need to do this check outside of the interlock in
991 * case this lock is held as a simple lock which means
992 * we won't be able to take the interlock
994 movl M_LOCKED,%eax /* get lock owner */
995 testl %eax,%eax /* is the mutex locked? */
996 jne Lmt_fail_no_ilk /* yes, go return failure */
998 movl M_ILK,%eax /* read interlock */
999 testl %eax,%eax /* unlocked? */
1000 je Lmt_retry /* yes, go try to grab it */
1001 jmp Lmt_ilk_loop /* no - keep spinning */
1009 popf /* restore interrupt state */
1014 LEAF_ENTRY(mutex_convert_spin)
1015 movl L_ARG0,%edx /* fetch lock pointer */
1017 movl M_LOCKED,%ecx /* is this the spin variant of the mutex */
1018 cmpl $(MUTEX_LOCKED_AS_SPIN),%ecx
1019 jne Lmcs_exit /* already owned as a mutex, just return */
1021 movl M_ILK,%ecx /* convert from spin version to mutex */
1022 movl %ecx,M_LOCKED /* take control of the mutex */
1024 cmpw $0,M_WAITERS /* are there any waiters? */
1025 jne Lmcs_waiters /* yes, more work to do */
1029 movl %ecx,M_ILK /* clear interlock */
1033 LOCKSTAT_LABEL(_mutex_convert_spin_lockstat_patch_point)
1035 /* inherit %edx from above */
1036 LOCKSTAT_RECORD(LS_MUTEX_CONVERT_SPIN_ACQUIRE, %edx)
1042 pushl %edx /* save mutex address */
1044 call EXT(lck_mtx_lock_acquire)
1046 popl %edx /* restore mutex address */
1051 NONLEAF_ENTRY(mutex_unlock)
1052 movl B_ARG0,%edx /* fetch lock pointer */
1054 movl M_LOCKED,%ecx /* is this the spin variant of the mutex */
1055 cmpl $(MUTEX_LOCKED_AS_SPIN),%ecx
1056 jne Lmu_enter /* no, go treat like a real mutex */
1058 cmpw $0,M_WAITERS /* are there any waiters? */
1059 jne Lmus_wakeup /* yes, more work to do */
1063 movl %ecx,M_LOCKED /* yes, clear the spin indicator */
1064 movl %ecx,M_ILK /* release the interlock */
1065 PREEMPTION_ENABLE /* and re-enable preemption */
1068 LOCKSTAT_LABEL(_mutex_unlock_lockstat_patch_point)
1070 /* inherit lock pointer in %edx from above */
1071 LOCKSTAT_RECORD(LS_MUTEX_UNLOCK_RELEASE, %edx)
1076 pushl %edx /* save mutex address */
1077 pushl %edx /* push mutex address */
1078 call EXT(lck_mtx_unlockspin_wakeup) /* yes, wake a thread */
1080 popl %edx /* restore mutex pointer */
1084 pushf /* save interrupt state */
1087 CHECK_THREAD(M_THREAD)
1089 movl M_ILK,%eax /* read interlock */
1090 testl %eax,%eax /* unlocked? */
1091 jne Lmu_ilk_loop /* yes, go try to grab it */
1093 cli /* disable interrupts */
1094 movl %gs:CPU_ACTIVE_THREAD,%ecx
1096 /* eax == 0 at this point */
1097 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
1098 jne Lmu_ilk_fail /* branch on failure to spin loop */
1100 cmpw $0,M_WAITERS /* are there any waiters? */
1101 jne Lmu_wakeup /* yes, more work to do */
1105 movl $0,M_THREAD /* disown thread */
1108 movl %ecx,M_LOCKED /* unlock the mutex */
1109 movl %ecx,M_ILK /* release the interlock */
1110 popf /* restore interrupt state */
1113 LOCKSTAT_LABEL(_mutex_unlock2_lockstat_patch_point)
1115 /* inherit %edx from above */
1116 LOCKSTAT_RECORD(LS_MUTEX_UNLOCK_RELEASE, %edx)
1121 popf /* restore interrupt state */
1122 pushf /* resave interrupt state on stack */
1126 movl M_ILK,%eax /* read interlock */
1127 testl %eax,%eax /* unlocked? */
1128 je Lmu_retry /* yes, go try to grab it */
1129 jmp Lmu_ilk_loop /* no - keep spinning */
1133 pushl %edx /* push mutex address */
1134 call EXT(lck_mtx_unlock_wakeup)/* yes, wake a thread */
1136 movl B_ARG0,%edx /* restore lock pointer */
1140 * void lck_mtx_assert(lck_mtx_t* l, unsigned int)
1141 * void _mutex_assert(mutex_t, unsigned int)
1142 * Takes the address of a lock, and an assertion type as parameters.
1143 * The assertion can take one of two forms determine by the type
1144 * parameter: either the lock is held by the current thread, and the
1145 * type is LCK_MTX_ASSERT_OWNED, or it isn't and the type is
1146 * LCK_MTX_ASSERT_NOT_OWNED. Calls panic on assertion failure.
1150 Entry(lck_mtx_assert)
1151 Entry(_mutex_assert)
1152 movl S_ARG0,%edx /* Load lock address */
1153 movl %gs:CPU_ACTIVE_THREAD,%ecx /* Load current thread */
1155 cmpl $(MUTEX_IND),M_ITAG /* Is this an indirect mutex? */
1156 cmove M_PTR,%edx /* If so, take indirection */
1158 movl M_LOCKED,%eax /* Load lock word */
1159 cmpl $(MUTEX_LOCKED_AS_SPIN),%eax /* check for spin variant */
1160 cmove M_ILK,%eax /* yes, spin lock owner is in the interlock */
1162 cmpl $(MUTEX_ASSERT_OWNED),S_ARG1 /* Determine assert type */
1163 jne 2f /* Assert ownership? */
1164 cmpl %eax,%ecx /* Current thread match? */
1165 jne 3f /* no, go panic */
1166 1: /* yes, we own it */
1167 ret /* just return */
1169 cmpl %eax,%ecx /* Current thread match? */
1170 jne 1b /* No, return */
1171 movl %edx,S_ARG1 /* Prep assertion failure */
1172 movl $(mutex_assert_owned_str),S_ARG0
1175 movl %edx,S_ARG1 /* Prep assertion failure */
1176 movl $(mutex_assert_not_owned_str),S_ARG0
1181 mutex_assert_not_owned_str:
1182 .asciz "mutex (%p) not owned\n"
1183 mutex_assert_owned_str:
1184 .asciz "mutex (%p) owned\n"
1187 /* This preprocessor define controls whether the R-M-W update of the
1188 * per-group statistics elements are atomic (LOCK-prefixed)
1189 * Enabled by default.
1191 #define ATOMIC_STAT_UPDATES 1
1193 #if defined(ATOMIC_STAT_UPDATES)
1194 #define LOCK_IF_ATOMIC_STAT_UPDATES lock
1196 #define LOCK_IF_ATOMIC_STAT_UPDATES
1197 #endif /* ATOMIC_STAT_UPDATES */
1202 * lck_mtx_try_lock()
1203 * lck_mutex_unlock()
1204 * lck_mtx_lock_spin()
1205 * lck_mtx_convert_spin()
1207 * These are variants of mutex_lock(), mutex_try(), mutex_unlock()
1208 * mutex_lock_spin and mutex_convert_spin without
1209 * DEBUG checks (which require fields not present in lck_mtx_t's).
1212 NONLEAF_ENTRY(lck_mtx_lock_spin)
1214 movl B_ARG0,%edx /* fetch lock pointer */
1215 pushf /* save interrupt state */
1217 CHECK_NO_SIMPLELOCKS()
1218 CHECK_PREEMPTION_LEVEL()
1220 movl M_ILK,%eax /* read interlock */
1221 testl %eax,%eax /* unlocked? */
1222 jne Llmls_eval_ilk /* no, go see if indirect */
1224 cli /* disable interrupts */
1225 movl %gs:CPU_ACTIVE_THREAD,%ecx
1227 /* eax == 0 at this point */
1228 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
1229 jne Llmls_ilk_fail /* branch on failure to spin loop */
1231 movl M_LOCKED,%ecx /* get lock owner */
1232 testl %ecx,%ecx /* is the mutex locked? */
1233 jne Llml_fail /* yes, fall back to a normal mutex */
1236 movl $(MUTEX_LOCKED_AS_SPIN),M_LOCKED /* indicate ownership as a spin lock */
1238 popf /* restore interrupt state */
1239 NONLEAF_RET /* return with the interlock held */
1242 popf /* restore interrupt state */
1243 pushf /* resave interrupt state on stack */
1247 movl M_ILK,%eax /* read interlock */
1248 testl %eax,%eax /* unlocked? */
1249 je Llmls_retry /* yes - go try to grab it */
1251 cmpl $(MUTEX_DESTROYED),%eax /* check to see if its marked destroyed */
1252 jne Llmls_ilk_loop /* no - keep spinning */
1255 call EXT(lck_mtx_interlock_panic)
1257 * shouldn't return from here, but just in case
1264 cmpl $(MUTEX_IND),M_ITAG /* Is this an indirect mutex? */
1265 cmove M_PTR,%edx /* If so, take indirection */
1266 jne Llmls_ilk_loop /* If not, go to spin loop */
1269 pushl %esi /* Used to hold the lock group ptr */
1270 pushl %edi /* Used for stat update records */
1271 movl MUTEX_GRP(%edx),%esi /* Load lock group */
1272 xorl %edi,%edi /* Clear stat update records */
1273 /* 64-bit increment of acquire attempt statistic (per-group) */
1274 LOCK_IF_ATOMIC_STAT_UPDATES
1275 addl $1, GRP_MTX_STAT_UTIL(%esi)
1277 incl GRP_MTX_STAT_UTIL+4(%esi)
1279 movl M_ILK,%eax /* read interlock */
1280 testl %eax,%eax /* unlocked? */
1281 jne Llmls_ext_ilk_loop /* no, go to spin loop */
1283 cli /* disable interrupts */
1284 movl %gs:CPU_ACTIVE_THREAD,%ecx
1286 /* eax == 0 at this point */
1287 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
1288 jne Llmls_ext_ilk_fail /* branch on failure to retry */
1290 movl M_LOCKED,%ecx /* get lock owner */
1291 testl %ecx,%ecx /* is the mutex locked? */
1292 jne Llml_ext_fail /* yes, we lose */
1300 * Slow path: call out to do the spinning.
1304 popf /* restore interrupt state */
1308 movl M_ILK,%eax /* read interlock */
1309 testl %eax,%eax /* unlocked? */
1310 je Llmls_ext_retry /* yes - go try to grab it */
1312 cmpl $(MUTEX_DESTROYED),%eax /* check to see if its marked destroyed */
1313 jne Llmls_ext_ilk_loop /* no - keep spinning */
1316 call EXT(lck_mtx_interlock_panic)
1318 * shouldn't return from here, but just in case
1321 jmp Llmls_ext_ilk_loop /* no - keep spinning */
1325 NONLEAF_ENTRY(lck_mtx_lock)
1327 movl B_ARG0,%edx /* fetch lock pointer */
1328 pushf /* save interrupt state */
1330 CHECK_NO_SIMPLELOCKS()
1331 CHECK_PREEMPTION_LEVEL()
1333 movl M_ILK,%eax /* read interlock */
1334 testl %eax,%eax /* unlocked? */
1335 jne Llml_eval_ilk /* no, go see if indirect */
1337 cli /* disable interrupts */
1338 movl %gs:CPU_ACTIVE_THREAD,%ecx
1340 /* eax == 0 at this point */
1341 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
1342 jne Llml_ilk_fail /* branch on failure to spin loop */
1344 movl M_LOCKED,%ecx /* get lock owner */
1345 testl %ecx,%ecx /* is the mutex locked? */
1346 jne Llml_fail /* yes, we lose */
1348 movl %gs:CPU_ACTIVE_THREAD,%ecx
1351 cmpw $0,M_WAITERS /* are there any waiters? */
1352 jne Lml_waiters /* yes, more work to do */
1357 popf /* restore interrupt state */
1360 LOCKSTAT_LABEL(_lck_mtx_lock_lockstat_patch_point)
1362 /* inherit lock pointer in %edx above */
1363 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_ACQUIRE, %edx)
1368 pushl %edx /* save mutex address */
1370 call EXT(lck_mtx_lock_acquire)
1372 popl %edx /* restore mutex address */
1377 popf /* restore interrupt state */
1378 pushf /* resave interrupt state on stack */
1382 movl M_ILK,%eax /* read interlock */
1383 testl %eax,%eax /* unlocked? */
1384 je Llml_retry /* yes - go try to grab it */
1386 cmpl $(MUTEX_DESTROYED),%eax /* check to see if its marked destroyed */
1387 jne Llml_ilk_loop /* no - keep spinning */
1390 call EXT(lck_mtx_interlock_panic)
1392 * shouldn't return from here, but just in case
1395 jmp Llml_ilk_loop /* no - keep spinning */
1399 * Check if the owner is on another processor and therefore
1400 * we should try to spin before blocking.
1402 testl $(OnProc),ACT_SPF(%ecx)
1406 * Here if owner is on another processor:
1407 * - release the interlock
1408 * - spin on the holder until release or timeout
1409 * - in either case re-acquire the interlock
1410 * - if released, acquire it
1411 * - otherwise drop thru to block.
1414 movl %eax,M_ILK /* zero interlock */
1416 pushf /* restore interrupt state */
1417 pushl %edx /* save mutex address */
1419 call EXT(lck_mtx_lock_spinwait)
1421 popl %edx /* restore mutex address */
1423 /* Re-acquire interlock */
1424 movl M_ILK,%eax /* read interlock */
1425 testl %eax,%eax /* unlocked? */
1426 jne Llml_ilk_refail /* no, go to spin loop */
1428 cli /* disable interrupts */
1429 movl %gs:CPU_ACTIVE_THREAD,%ecx
1431 /* eax == 0 at this point */
1432 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
1433 jne Llml_ilk_refail /* branch on failure to retry */
1435 movl M_LOCKED,%ecx /* get lock owner */
1436 testl %ecx,%ecx /* is the mutex free? */
1437 je Llml_acquire /* yes, acquire */
1440 CHECK_MYLOCK(M_THREAD)
1441 pushl %edx /* save mutex address */
1443 pushl %edx /* push mutex address */
1445 * N.B.: lck_mtx_lock_wait is called here with interrupts disabled
1446 * Consider reworking.
1448 call EXT(lck_mtx_lock_wait) /* wait for the lock */
1450 popl %edx /* restore mutex address */
1451 jmp Llml_restart /* and start over */
1454 popf /* restore interrupt state */
1455 pushf /* resave interrupt state on stack */
1459 movl M_ILK,%eax /* read interlock */
1460 testl %eax,%eax /* unlocked? */
1461 je Llml_reget_retry /* yes - go try to grab it */
1463 cmpl $(MUTEX_DESTROYED),%eax /* check to see if its marked destroyed */
1464 jne Llml_ilk_reloop /* no - keep spinning */
1467 call EXT(lck_mtx_interlock_panic)
1469 * shouldn't return from here, but just in case
1472 jmp Llml_ilk_reloop /* no - keep spinning */
1476 cmpl $(MUTEX_IND),M_ITAG /* Is this an indirect mutex? */
1477 cmove M_PTR,%edx /* If so, take indirection */
1478 jne Llml_ilk_loop /* If not, go to spin loop */
1481 * Entry into statistics codepath for lck_mtx_lock:
1482 * EDX: real lock pointer
1483 * first dword on stack contains flags
1486 /* Enable this preprocessor define to record the first miss alone
1487 * By default, we count every miss, hence multiple misses may be
1488 * recorded for a single lock acquire attempt via lck_mtx_lock
1490 #undef LOG_FIRST_MISS_ALONE
1493 * N.B.: On x86, statistics are currently recorded for all indirect mutexes.
1494 * Also, only the acquire attempt count (GRP_MTX_STAT_UTIL) is maintained
1495 * as a 64-bit quantity (this matches the existing PowerPC implementation,
1496 * and the new x86 specific statistics are also maintained as 32-bit
1501 pushl %esi /* Used to hold the lock group ptr */
1502 pushl %edi /* Used for stat update records */
1503 movl MUTEX_GRP(%edx),%esi /* Load lock group */
1504 xorl %edi,%edi /* Clear stat update records */
1505 /* 64-bit increment of acquire attempt statistic (per-group) */
1506 LOCK_IF_ATOMIC_STAT_UPDATES
1507 addl $1, GRP_MTX_STAT_UTIL(%esi)
1509 incl GRP_MTX_STAT_UTIL+4(%esi)
1511 movl M_ILK,%eax /* read interlock */
1512 testl %eax,%eax /* unlocked? */
1513 jne Llml_ext_ilk_loop /* no, go to spin loop */
1516 movl %gs:CPU_ACTIVE_THREAD,%ecx
1518 /* eax == 0 at this point */
1519 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
1520 jne Llml_ext_ilk_fail /* branch on failure to retry */
1522 movl M_LOCKED,%ecx /* get lock owner */
1523 testl %ecx,%ecx /* is the mutex locked? */
1524 jne Llml_ext_fail /* yes, we lose */
1527 movl %gs:CPU_ACTIVE_THREAD,%ecx
1530 cmpw $0,M_WAITERS /* are there any waiters? */
1531 jne Llml_ext_waiters /* yes, more work to do */
1538 popf /* restore interrupt state */
1541 LOCKSTAT_LABEL(_lck_mtx_lock_ext_lockstat_patch_point)
1543 /* inherit lock pointer in %edx above */
1544 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_LOCK_ACQUIRE, %edx)
1549 pushl %edx /* save mutex address */
1551 call EXT(lck_mtx_lock_acquire)
1553 popl %edx /* restore mutex address */
1560 popf /* restore interrupt state */
1564 movl M_ILK,%eax /* read interlock */
1565 testl %eax,%eax /* unlocked? */
1566 je Llml_ext_get_hw /* yes - go try to grab it */
1568 cmpl $(MUTEX_DESTROYED),%eax /* check to see if its marked destroyed */
1569 jne Llml_ext_ilk_loop /* no - keep spinning */
1572 call EXT(lck_mtx_interlock_panic)
1574 * shouldn't return from here, but just in case
1577 jmp Llml_ext_ilk_loop
1581 #ifdef LOG_FIRST_MISS_ALONE
1584 #endif /* LOG_FIRST_MISS_ALONE */
1585 /* Record that a lock acquire attempt missed (per-group statistic) */
1586 LOCK_IF_ATOMIC_STAT_UPDATES
1587 incl GRP_MTX_STAT_MISS(%esi)
1588 #ifdef LOG_FIRST_MISS_ALONE
1590 #endif /* LOG_FIRST_MISS_ALONE */
1593 * Check if the owner is on another processor and therefore
1594 * we should try to spin before blocking.
1596 testl $(OnProc),ACT_SPF(%ecx)
1599 * Record the "direct wait" statistic, which indicates if a
1600 * miss proceeded to block directly without spinning--occurs
1601 * if the owner of the mutex isn't running on another processor
1602 * at the time of the check.
1604 LOCK_IF_ATOMIC_STAT_UPDATES
1605 incl GRP_MTX_STAT_DIRECT_WAIT(%esi)
1609 * Here if owner is on another processor:
1610 * - release the interlock
1611 * - spin on the holder until release or timeout
1612 * - in either case re-acquire the interlock
1613 * - if released, acquire it
1614 * - otherwise drop thru to block.
1617 movl %eax,M_ILK /* zero interlock */
1619 pushl 8(%esp) /* Make another copy of EFLAGS image */
1620 popf /* Restore interrupt state */
1621 pushl %edx /* save mutex address */
1623 call EXT(lck_mtx_lock_spinwait)
1625 popl %edx /* restore mutex address */
1627 /* Re-acquire interlock */
1628 movl M_ILK,%eax /* read interlock */
1629 testl %eax,%eax /* unlocked? */
1630 jne Llml_ext_ilk_refail /* no, go to spin loop */
1631 Llml_ext_reget_retry:
1632 cli /* disable interrupts */
1633 movl %gs:CPU_ACTIVE_THREAD,%ecx
1635 /* eax == 0 at this point */
1636 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
1637 jne Llml_ext_ilk_refail /* branch on failure to spin loop */
1639 movl M_LOCKED,%ecx /* get lock owner */
1640 testl %ecx,%ecx /* is the mutex free? */
1641 je Llml_ext_acquire /* yes, acquire */
1644 /* If we wanted to count waits just once per lock acquire, we'd
1645 * skip over the stat update here
1647 LOCK_IF_ATOMIC_STAT_UPDATES
1648 /* Record that a lock miss proceeded to block */
1649 incl GRP_MTX_STAT_WAIT(%esi)
1651 CHECK_MYLOCK(M_THREAD)
1652 pushl %edx /* save mutex address */
1654 pushl %edx /* push mutex address */
1656 * N.B.: lck_mtx_lock_wait is called here with interrupts disabled
1657 * Consider reworking.
1659 call EXT(lck_mtx_lock_wait) /* wait for the lock */
1661 popl %edx /* restore mutex address */
1662 jmp Llml_ext_restart /* and start over */
1664 Llml_ext_ilk_refail:
1667 popf /* restore interrupt state */
1669 Llml_ext_ilk_reloop:
1671 movl M_ILK,%eax /* read interlock */
1672 testl %eax,%eax /* unlocked? */
1673 je Llml_ext_reget_retry /* yes - go try to grab it */
1675 cmpl $(MUTEX_DESTROYED),%eax /* check to see if its marked destroyed */
1676 jne Llml_ext_ilk_reloop /* no - keep spinning */
1679 call EXT(lck_mtx_interlock_panic)
1681 * shouldn't return from here, but just in case
1684 jmp Llml_ext_ilk_reloop
1688 NONLEAF_ENTRY(lck_mtx_try_lock_spin)
1690 movl B_ARG0,%edx /* fetch lock pointer */
1691 pushf /* save interrupt state */
1693 CHECK_NO_SIMPLELOCKS()
1694 CHECK_PREEMPTION_LEVEL()
1696 movl M_ILK,%eax /* read interlock */
1697 testl %eax,%eax /* unlocked? */
1698 jne Llmts_eval_ilk /* no, go see if indirect */
1700 cli /* disable interrupts */
1701 movl %gs:CPU_ACTIVE_THREAD,%ecx
1703 /* eax == 0 at this point */
1704 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
1705 jne Llmts_ilk_fail /* branch on failure to retry */
1707 movl M_LOCKED,%ecx /* get lock owner */
1708 testl %ecx,%ecx /* is the mutex locked? */
1709 jne Llmt_fail /* yes, we lose */
1711 movl $(MUTEX_LOCKED_AS_SPIN),M_LOCKED /* no, indicate ownership as a spin lock */
1712 PREEMPTION_DISABLE /* and return with interlock held */
1714 movl $1,%eax /* return success */
1715 popf /* restore interrupt state */
1718 LOCKSTAT_LABEL(_lck_mtx_try_lock_spin_lockstat_patch_point)
1720 /* inherit lock pointer in %edx above */
1721 LOCKSTAT_RECORD(LS_LCK_MTX_TRY_SPIN_LOCK_ACQUIRE, %edx)
1722 movl $1,%eax /* return success */
1727 popf /* restore interrupt state */
1728 pushf /* resave interrupt state */
1733 * need to do this check outside of the interlock in
1734 * case this lock is held as a simple lock which means
1735 * we won't be able to take the interlock
1737 movl M_LOCKED,%eax /* get lock owner */
1738 testl %eax,%eax /* is the mutex locked? */
1739 jne Llmt_fail_no_ilk /* yes, go return failure */
1741 movl M_ILK,%eax /* read interlock */
1742 testl %eax,%eax /* unlocked? */
1743 je Llmts_retry /* yes - go try to grab it */
1745 cmpl $(MUTEX_DESTROYED),%eax /* check to see if its marked destroyed */
1746 jne Llmts_ilk_loop /* no - keep spinning */
1749 call EXT(lck_mtx_interlock_panic)
1751 * shouldn't return from here, but just in case
1757 cmpl $(MUTEX_IND),M_ITAG /* Is this an indirect mutex? */
1758 cmove M_PTR,%edx /* If so, take indirection */
1759 jne Llmts_ilk_loop /* If not, go to spin loop */
1762 * bump counter on indirect lock
1764 pushl %esi /* Used to hold the lock group ptr */
1765 movl MUTEX_GRP(%edx),%esi /* Load lock group */
1766 /* 64-bit increment of acquire attempt statistic (per-group) */
1767 LOCK_IF_ATOMIC_STAT_UPDATES
1768 addl $1, GRP_MTX_STAT_UTIL(%esi)
1770 incl GRP_MTX_STAT_UTIL+4(%esi)
1777 NONLEAF_ENTRY(lck_mtx_try_lock)
1779 movl B_ARG0,%edx /* fetch lock pointer */
1780 pushf /* save interrupt state */
1782 CHECK_NO_SIMPLELOCKS()
1783 CHECK_PREEMPTION_LEVEL()
1785 movl M_ILK,%eax /* read interlock */
1786 testl %eax,%eax /* unlocked? */
1787 jne Llmt_eval_ilk /* no, go see if indirect */
1789 cli /* disable interrupts */
1790 movl %gs:CPU_ACTIVE_THREAD,%ecx
1792 /* eax == 0 at this point */
1793 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
1794 jne Llmt_ilk_fail /* branch on failure to retry */
1796 movl M_LOCKED,%ecx /* get lock owner */
1797 testl %ecx,%ecx /* is the mutex locked? */
1798 jne Llmt_fail /* yes, we lose */
1800 movl %gs:CPU_ACTIVE_THREAD,%ecx
1803 cmpw $0,M_WAITERS /* are there any waiters? */
1804 jne Llmt_waiters /* yes, more work to do */
1809 popf /* restore interrupt state */
1811 movl $1,%eax /* return success */
1814 /* Dtrace probe: LS_LCK_MTX_TRY_LOCK_ACQUIRE */
1815 LOCKSTAT_LABEL(_lck_mtx_try_lock_lockstat_patch_point)
1817 /* inherit lock pointer in %edx from above */
1818 LOCKSTAT_RECORD(LS_LCK_MTX_TRY_LOCK_ACQUIRE, %edx)
1819 movl $1,%eax /* return success */
1824 pushl %edx /* save mutex address */
1826 call EXT(lck_mtx_lock_acquire)
1828 popl %edx /* restore mutex address */
1832 popf /* restore interrupt state */
1833 pushf /* resave interrupt state */
1838 * need to do this check outside of the interlock in
1839 * case this lock is held as a simple lock which means
1840 * we won't be able to take the interlock
1842 movl M_LOCKED,%eax /* get lock owner */
1843 testl %eax,%eax /* is the mutex locked? */
1844 jne Llmt_fail_no_ilk /* yes, go return failure */
1846 movl M_ILK,%eax /* read interlock */
1847 testl %eax,%eax /* unlocked? */
1848 je Llmt_retry /* yes - go try to grab it */
1850 cmpl $(MUTEX_DESTROYED),%eax /* check to see if its marked destroyed */
1851 jne Llmt_ilk_loop /* no - keep spinning */
1854 call EXT(lck_mtx_interlock_panic)
1856 * shouldn't return from here, but just in case
1862 xorl %eax,%eax /* Zero interlock value */
1866 popf /* restore interrupt state */
1869 jne Llmt_fail_indirect
1872 /* Note that we don't record a dtrace event for trying and missing */
1876 pushl %esi /* Used to hold the lock group ptr */
1877 movl MUTEX_GRP(%edx),%esi /* Load lock group */
1879 /* Record mutex acquire attempt miss statistic */
1880 LOCK_IF_ATOMIC_STAT_UPDATES
1881 incl GRP_MTX_STAT_MISS(%esi)
1888 cmpl $(MUTEX_IND),M_ITAG /* Is this an indirect mutex? */
1889 cmove M_PTR,%edx /* If so, take indirection */
1890 jne Llmt_ilk_loop /* If not, go to spin loop */
1893 * bump counter for indirect lock
1895 pushl %esi /* Used to hold the lock group ptr */
1896 movl MUTEX_GRP(%edx),%esi /* Load lock group */
1898 /* 64-bit increment of acquire attempt statistic (per-group) */
1899 LOCK_IF_ATOMIC_STAT_UPDATES
1900 addl $1, GRP_MTX_STAT_UTIL(%esi)
1902 incl GRP_MTX_STAT_UTIL+4(%esi)
1909 LEAF_ENTRY(lck_mtx_convert_spin)
1910 movl L_ARG0,%edx /* fetch lock pointer */
1912 cmpl $(MUTEX_IND),M_ITAG /* Is this an indirect mutex? */
1913 cmove M_PTR,%edx /* If so, take indirection */
1915 movl M_LOCKED,%ecx /* is this the spin variant of the mutex */
1916 cmpl $(MUTEX_LOCKED_AS_SPIN),%ecx
1917 jne Llmcs_exit /* already owned as a mutex, just return */
1919 movl M_ILK,%ecx /* convert from spin version to mutex */
1920 movl %ecx,M_LOCKED /* take control of the mutex */
1922 cmpw $0,M_WAITERS /* are there any waiters? */
1923 jne Llmcs_waiters /* yes, more work to do */
1927 movl %ecx,M_ILK /* clear interlock */
1933 pushl %edx /* save mutex address */
1935 call EXT(lck_mtx_lock_acquire)
1937 popl %edx /* restore mutex address */
1942 NONLEAF_ENTRY(lck_mtx_unlock)
1944 movl B_ARG0,%edx /* fetch lock pointer */
1946 cmpl $(MUTEX_IND),M_ITAG /* Is this an indirect mutex? */
1947 cmove M_PTR,%edx /* If so, take indirection */
1949 movl M_LOCKED,%ecx /* is this the spin variant of the mutex */
1950 cmpl $(MUTEX_LOCKED_AS_SPIN),%ecx
1951 jne Llmu_enter /* no, go treat like a real mutex */
1953 cmpw $0,M_WAITERS /* are there any waiters? */
1954 jne Llmus_wakeup /* yes, more work to do */
1958 movl %eax,M_LOCKED /* clear spin indicator */
1959 movl %eax,M_ILK /* release the interlock */
1961 PREEMPTION_ENABLE /* and re-enable preemption */
1964 /* Dtrace: LS_LCK_MTX_UNLOCK_RELEASE */
1965 LOCKSTAT_LABEL(_lck_mtx_unlock_lockstat_patch_point)
1967 /* inherit lock pointer in %edx from above */
1968 LOCKSTAT_RECORD(LS_LCK_MTX_UNLOCK_RELEASE, %edx)
1973 pushl %edx /* save mutex address */
1974 pushl %edx /* push mutex address */
1975 call EXT(lck_mtx_unlockspin_wakeup) /* yes, wake a thread */
1977 popl %edx /* restore mutex pointer */
1982 pushf /* save interrupt state */
1984 movl M_ILK,%eax /* read interlock */
1985 testl %eax,%eax /* unlocked? */
1986 jne Llmu_ilk_loop /* no - go to spin loop */
1988 cli /* disable interrupts */
1989 movl %gs:CPU_ACTIVE_THREAD,%ecx
1991 /* eax == 0 at this point */
1992 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
1993 jne Llmu_ilk_fail /* branch on failure to spin loop */
1995 cmpw $0,M_WAITERS /* are there any waiters? */
1996 jne Llmu_wakeup /* yes, more work to do */
2000 movl %ecx,M_LOCKED /* unlock the mutex */
2001 movl %ecx,M_ILK /* clear the interlock */
2003 popf /* restore interrupt state */
2006 LOCKSTAT_LABEL(_lck_mtx_unlock2_lockstat_patch_point)
2008 /* inherit lock pointer in %edx above */
2009 LOCKSTAT_RECORD(LS_LCK_MTX_UNLOCK_RELEASE, %edx)
2014 popf /* restore interrupt state */
2015 pushf /* resave interrupt state */
2019 movl M_ILK,%eax /* read interlock */
2020 testl %eax,%eax /* unlocked? */
2021 je Llmu_retry /* yes - go try to grab it */
2023 cmpl $(MUTEX_DESTROYED),%eax /* check to see if its marked destroyed */
2024 jne Llmu_ilk_loop /* no - keep spinning */
2027 call EXT(lck_mtx_interlock_panic)
2029 * shouldn't return from here, but just in case
2035 pushl %edx /* save mutex address */
2037 pushl %edx /* push mutex address */
2038 call EXT(lck_mtx_unlock_wakeup)/* yes, wake a thread */
2040 popl %edx /* restore mutex pointer */
2042 movl %ecx,M_LOCKED /* unlock the mutex */
2046 popf /* restore interrupt state */
2050 /* Dtrace: LS_LCK_MTX_EXT_UNLOCK_RELEASE */
2051 LOCKSTAT_LABEL(_lck_mtx_ext_unlock_lockstat_patch_point)
2053 /* inherit lock pointer in %edx from above */
2054 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_UNLOCK_RELEASE, %edx)
2059 LEAF_ENTRY(lck_mtx_ilk_unlock)
2060 movl L_ARG0,%edx /* no indirection here */
2068 LEAF_ENTRY(_disable_preemption)
2071 #endif /* MACH_RT */
2074 LEAF_ENTRY(_enable_preemption)
2077 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
2079 pushl %gs:CPU_PREEMPTION_LEVEL
2084 2: String "_enable_preemption: preemption_level(%d) < 0!"
2087 #endif /* MACH_ASSERT */
2089 #endif /* MACH_RT */
2092 LEAF_ENTRY(_enable_preemption_no_check)
2095 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
2101 2: String "_enable_preemption_no_check: preemption_level <= 0!"
2104 #endif /* MACH_ASSERT */
2105 _ENABLE_PREEMPTION_NO_CHECK
2106 #endif /* MACH_RT */
2110 LEAF_ENTRY(_mp_disable_preemption)
2113 #endif /* MACH_RT */
2116 LEAF_ENTRY(_mp_enable_preemption)
2119 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
2121 pushl %gs:CPU_PREEMPTION_LEVEL
2126 2: String "_mp_enable_preemption: preemption_level (%d) <= 0!"
2129 #endif /* MACH_ASSERT */
2131 #endif /* MACH_RT */
2134 LEAF_ENTRY(_mp_enable_preemption_no_check)
2137 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
2143 2: String "_mp_enable_preemption_no_check: preemption_level <= 0!"
2146 #endif /* MACH_ASSERT */
2147 _ENABLE_PREEMPTION_NO_CHECK
2148 #endif /* MACH_RT */
2152 LEAF_ENTRY(i_bit_set)
2159 LEAF_ENTRY(i_bit_clear)
2167 LEAF_ENTRY(bit_lock)
2177 LEAF_ENTRY(bit_lock_try)
2183 LEAF_RET /* %eax better not be null ! */
2188 LEAF_ENTRY(bit_unlock)
2196 * Atomic primitives, prototyped in kern/simple_lock.h
2198 LEAF_ENTRY(hw_atomic_add)
2199 movl L_ARG0, %ecx /* Load address of operand */
2200 movl L_ARG1, %eax /* Load addend */
2203 xaddl %eax, (%ecx) /* Atomic exchange and add */
2204 addl %edx, %eax /* Calculate result */
2207 LEAF_ENTRY(hw_atomic_sub)
2208 movl L_ARG0, %ecx /* Load address of operand */
2209 movl L_ARG1, %eax /* Load subtrahend */
2213 xaddl %eax, (%ecx) /* Atomic exchange and add */
2214 addl %edx, %eax /* Calculate result */
2217 LEAF_ENTRY(hw_atomic_or)
2218 movl L_ARG0, %ecx /* Load address of operand */
2221 movl L_ARG1, %edx /* Load mask */
2224 cmpxchgl %edx, (%ecx) /* Atomic CAS */
2226 movl %edx, %eax /* Result */
2229 * A variant of hw_atomic_or which doesn't return a value.
2230 * The implementation is thus comparatively more efficient.
2233 LEAF_ENTRY(hw_atomic_or_noret)
2234 movl L_ARG0, %ecx /* Load address of operand */
2235 movl L_ARG1, %edx /* Load mask */
2237 orl %edx, (%ecx) /* Atomic OR */
2240 LEAF_ENTRY(hw_atomic_and)
2241 movl L_ARG0, %ecx /* Load address of operand */
2244 movl L_ARG1, %edx /* Load mask */
2247 cmpxchgl %edx, (%ecx) /* Atomic CAS */
2249 movl %edx, %eax /* Result */
2252 * A variant of hw_atomic_and which doesn't return a value.
2253 * The implementation is thus comparatively more efficient.
2256 LEAF_ENTRY(hw_atomic_and_noret)
2257 movl L_ARG0, %ecx /* Load address of operand */
2258 movl L_ARG1, %edx /* Load mask */
2260 andl %edx, (%ecx) /* Atomic OR */