2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1989 Carnegie-Mellon University
34 * All rights reserved. The CMU software License Agreement specifies
35 * the terms and conditions for use and redistribution.
39 #include <mach_ldebug.h>
41 #include <i386/eflags.h>
42 #include <i386/trap.h>
43 #include <config_dtrace.h>
48 #define PAUSE rep; nop
50 #include <i386/pal_lock_asm.h>
52 #define LEAF_ENTRY(name) \
55 #define LEAF_ENTRY2(n1,n2) \
62 /* Non-leaf routines always have a stack frame: */
64 #define NONLEAF_ENTRY(name) \
68 #define NONLEAF_ENTRY2(n1,n2) \
78 /* For x86_64, the varargs ABI requires that %al indicate
79 * how many SSE register contain arguments. In our case, 0 */
80 #define ALIGN_STACK() and $0xFFFFFFFFFFFFFFF0, %rsp ;
81 #define LOAD_STRING_ARG0(label) leaq label(%rip), %rdi ;
82 #define LOAD_ARG1(x) mov x, %esi ;
83 #define LOAD_PTR_ARG1(x) mov x, %rsi ;
84 #define CALL_PANIC() xorb %al,%al ; call EXT(panic) ;
86 #define CHECK_UNLOCK(current, owner) \
87 cmp current, owner ; \
90 LOAD_STRING_ARG0(2f) ; \
94 2: String "Mutex unlock attempted from non-owner thread"; \
100 * Routines for general lock debugging.
104 * Checks for expected lock types and calls "panic" on
105 * mismatch. Detects calls to Mutex functions with
106 * type simplelock and vice versa.
108 #define CHECK_MUTEX_TYPE() \
109 cmpl $ MUTEX_TAG,M_TYPE ; \
112 LOAD_STRING_ARG0(2f) ; \
116 2: String "not a mutex!" ; \
121 * If one or more simplelocks are currently held by a thread,
122 * an attempt to acquire a mutex will cause this check to fail
123 * (since a mutex lock may context switch, holding a simplelock
124 * is not a good thing).
127 #define CHECK_PREEMPTION_LEVEL() \
128 cmpl $0,%gs:CPU_HIBERNATE ; \
130 cmpl $0,%gs:CPU_PREEMPTION_LEVEL ; \
133 movl %gs:CPU_PREEMPTION_LEVEL, %eax ; \
135 LOAD_STRING_ARG0(2f) ; \
139 2: String "preemption_level(%d) != 0!" ; \
143 #define CHECK_PREEMPTION_LEVEL()
146 #define CHECK_MYLOCK(current, owner) \
147 cmp current, owner ; \
150 LOAD_STRING_ARG0(2f) ; \
154 2: String "Attempt to recursively lock a non-recursive lock"; \
158 #else /* MACH_LDEBUG */
159 #define CHECK_MUTEX_TYPE()
160 #define CHECK_PREEMPTION_LEVEL()
161 #define CHECK_MYLOCK(thd)
162 #endif /* MACH_LDEBUG */
164 #define PREEMPTION_DISABLE \
165 incl %gs:CPU_PREEMPTION_LEVEL
167 #define PREEMPTION_LEVEL_DEBUG 1
168 #if PREEMPTION_LEVEL_DEBUG
169 #define PREEMPTION_ENABLE \
170 decl %gs:CPU_PREEMPTION_LEVEL ; \
173 testl $AST_URGENT,%gs:CPU_PENDING_AST ; \
176 testl $EFL_IF, S_PC ; \
182 call _preemption_underflow_panic ; \
187 #define PREEMPTION_ENABLE \
188 decl %gs:CPU_PREEMPTION_LEVEL ; \
190 testl $AST_URGENT,%gs:CPU_PENDING_AST ; \
193 testl $EFL_IF, S_PC ; \
206 .globl _lockstat_probe
207 .globl _lockstat_probemap
210 * LOCKSTAT_LABEL creates a dtrace symbol which contains
211 * a pointer into the lock code function body. At that
212 * point is a "ret" instruction that can be patched into
216 #define LOCKSTAT_LABEL(lab) \
224 #define LOCKSTAT_RECORD(id, lck) \
227 movl _lockstat_probemap + (id * 4)(%rip),%eax ; \
236 call *_lockstat_probe(%rip) ; \
238 /* ret - left to subsequent code, e.g. return values */
240 #endif /* CONFIG_DTRACE */
243 * For most routines, the hw_lock_t pointer is loaded into a
244 * register initially, and then either a byte or register-sized
245 * word is loaded/stored to the pointer
249 * void hw_lock_init(hw_lock_t)
251 * Initialize a hardware lock.
253 LEAF_ENTRY(hw_lock_init)
254 movq $0, (%rdi) /* clear the lock */
259 * void hw_lock_byte_init(volatile uint8_t *)
261 * Initialize a hardware byte lock.
263 LEAF_ENTRY(hw_lock_byte_init)
264 movb $0, (%rdi) /* clear the lock */
268 * void hw_lock_lock(hw_lock_t)
270 * Acquire lock, spinning until it becomes available.
271 * MACH_RT: also return with preemption disabled.
273 LEAF_ENTRY(hw_lock_lock)
274 mov %gs:CPU_ACTIVE_THREAD, %rcx /* get thread pointer */
279 test %rax,%rax /* lock locked? */
280 jne 3f /* branch if so */
281 lock; cmpxchg %rcx,(%rdi) /* try to acquire the HW lock */
283 movl $1,%eax /* In case this was a timeout call */
284 LEAF_RET /* if yes, then nothing left to do */
286 PAUSE /* pause for hyper-threading */
287 jmp 1b /* try again */
290 * void hw_lock_byte_lock(uint8_t *lock_byte)
292 * Acquire byte sized lock operand, spinning until it becomes available.
293 * MACH_RT: also return with preemption disabled.
296 LEAF_ENTRY(hw_lock_byte_lock)
298 movl $1, %ecx /* Set lock value */
300 movb (%rdi), %al /* Load byte at address */
301 testb %al,%al /* lock locked? */
302 jne 3f /* branch if so */
303 lock; cmpxchg %cl,(%rdi) /* attempt atomic compare exchange */
305 LEAF_RET /* if yes, then nothing left to do */
307 PAUSE /* pause for hyper-threading */
308 jmp 1b /* try again */
311 * unsigned int hw_lock_to(hw_lock_t, unsigned int)
313 * Acquire lock, spinning until it becomes available or timeout.
314 * MACH_RT: also return with preemption disabled.
316 LEAF_ENTRY(hw_lock_to)
318 mov %gs:CPU_ACTIVE_THREAD, %rcx
321 * Attempt to grab the lock immediately
322 * - fastpath without timeout nonsense.
327 test %rax,%rax /* lock locked? */
328 jne 2f /* branch if so */
329 lock; cmpxchg %rcx,(%rdi) /* try to acquire the HW lock */
330 jne 2f /* branch on failure */
335 #define INNER_LOOP_COUNT 1000
337 * Failed to get the lock so set the timeout
338 * and then spin re-checking the lock but pausing
339 * every so many (INNER_LOOP_COUNT) spins to check for timeout.
343 rdtsc /* read cyclecount into %edx:%eax */
345 orq %rdx, %rax /* load 64-bit quantity into %rax */
346 addq %rax, %rsi /* %rsi is the timeout expiry */
350 * The inner-loop spin to look for the lock being freed.
352 mov $(INNER_LOOP_COUNT),%r9
354 PAUSE /* pause for hyper-threading */
355 mov (%rdi),%rax /* spin checking lock value in cache */
357 je 6f /* zero => unlocked, try to grab it */
358 decq %r9 /* decrement inner loop count */
359 jnz 5b /* time to check for timeout? */
362 * Here after spinning INNER_LOOP_COUNT times, check for timeout
365 rdtsc /* cyclecount into %edx:%eax */
367 orq %rdx, %rax /* load 64-bit quantity into %rax */
368 cmpq %rsi, %rax /* compare to timeout */
369 jb 4b /* continue spinning if less, or */
370 xor %rax,%rax /* with 0 return value */
376 * Here to try to grab the lock that now appears to be free
379 mov %gs:CPU_ACTIVE_THREAD, %rcx
380 lock; cmpxchg %rcx,(%rdi) /* try to acquire the HW lock */
381 jne 4b /* no - spin again */
382 movl $1,%eax /* yes */
387 * void hw_lock_unlock(hw_lock_t)
389 * Unconditionally release lock.
390 * MACH_RT: release preemption level.
392 LEAF_ENTRY(hw_lock_unlock)
393 movq $0, (%rdi) /* clear the lock */
398 * void hw_lock_byte_unlock(uint8_t *lock_byte)
400 * Unconditionally release byte sized lock operand.
401 * MACH_RT: release preemption level.
404 LEAF_ENTRY(hw_lock_byte_unlock)
405 movb $0, (%rdi) /* Clear the lock byte */
410 * unsigned int hw_lock_try(hw_lock_t)
411 * MACH_RT: returns with preemption disabled on success.
413 LEAF_ENTRY(hw_lock_try)
414 mov %gs:CPU_ACTIVE_THREAD, %rcx
420 lock; cmpxchg %rcx,(%rdi) /* try to acquire the HW lock */
423 movl $1,%eax /* success */
427 PREEMPTION_ENABLE /* failure: release preemption... */
428 xorl %eax,%eax /* ...and return failure */
432 * unsigned int hw_lock_held(hw_lock_t)
433 * MACH_RT: doesn't change preemption state.
434 * N.B. Racy, of course.
436 LEAF_ENTRY(hw_lock_held)
437 mov (%rdi),%rax /* check lock value */
440 cmovne %ecx,%eax /* 0 => unlocked, 1 => locked */
445 * Reader-writer lock fastpaths. These currently exist for the
446 * shared lock acquire, the exclusive lock acquire, the shared to
447 * exclusive upgrade and the release paths (where they reduce overhead
448 * considerably) -- these are by far the most frequently used routines
450 * The following should reflect the layout of the bitfield embedded within
451 * the lck_rw_t structure (see i386/locks.h).
453 #define LCK_RW_INTERLOCK (0x1 << 16)
455 #define LCK_RW_PRIV_EXCL (0x1 << 24)
456 #define LCK_RW_WANT_UPGRADE (0x2 << 24)
457 #define LCK_RW_WANT_WRITE (0x4 << 24)
458 #define LCK_R_WAITING (0x8 << 24)
459 #define LCK_W_WAITING (0x10 << 24)
461 #define LCK_RW_SHARED_MASK (0xffff)
464 * For most routines, the lck_rw_t pointer is loaded into a
465 * register initially, and the flags bitfield loaded into another
466 * register and examined
469 #define RW_LOCK_SHARED_MASK (LCK_RW_INTERLOCK | LCK_RW_WANT_UPGRADE | LCK_RW_WANT_WRITE)
471 * void lck_rw_lock_shared(lck_rw_t *)
474 Entry(lck_rw_lock_shared)
475 mov %gs:CPU_ACTIVE_THREAD, %rcx /* Load thread pointer */
476 incl TH_RWLOCK_COUNT(%rcx) /* Increment count before atomic CAS */
478 mov (%rdi), %eax /* Load state bitfield and interlock */
479 testl $(RW_LOCK_SHARED_MASK), %eax /* Eligible for fastpath? */
482 movl %eax, %ecx /* original value in %eax for cmpxchgl */
483 incl %ecx /* Increment reader refcount */
485 cmpxchgl %ecx, (%rdi) /* Attempt atomic exchange */
490 * Dtrace lockstat event: LS_LCK_RW_LOCK_SHARED_ACQUIRE
491 * Implemented by swapping between return and no-op instructions.
492 * See bsd/dev/dtrace/lockstat.c.
494 LOCKSTAT_LABEL(_lck_rw_lock_shared_lockstat_patch_point)
497 Fall thru when patched, counting on lock pointer in %rdi
499 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_SHARED_ACQUIRE, %rdi)
506 jmp EXT(lck_rw_lock_shared_gen)
510 #define RW_TRY_LOCK_SHARED_MASK (LCK_RW_WANT_UPGRADE | LCK_RW_WANT_WRITE)
512 * void lck_rw_try_lock_shared(lck_rw_t *)
515 Entry(lck_rw_try_lock_shared)
517 mov (%rdi), %eax /* Load state bitfield and interlock */
518 testl $(LCK_RW_INTERLOCK), %eax
520 testl $(RW_TRY_LOCK_SHARED_MASK), %eax
521 jne 3f /* lock is busy */
523 movl %eax, %ecx /* original value in %eax for cmpxchgl */
524 incl %ecx /* Increment reader refcount */
526 cmpxchgl %ecx, (%rdi) /* Attempt atomic exchange */
529 mov %gs:CPU_ACTIVE_THREAD, %rcx /* Load thread pointer */
530 incl TH_RWLOCK_COUNT(%rcx) /* Increment count on success. */
531 /* There is a 3 instr window where preemption may not notice rwlock_count after cmpxchg */
536 * Dtrace lockstat event: LS_LCK_RW_TRY_LOCK_SHARED_ACQUIRE
537 * Implemented by swapping between return and no-op instructions.
538 * See bsd/dev/dtrace/lockstat.c.
540 LOCKSTAT_LABEL(_lck_rw_try_lock_shared_lockstat_patch_point)
542 /* Fall thru when patched, counting on lock pointer in %rdi */
543 LOCKSTAT_RECORD(LS_LCK_RW_TRY_LOCK_SHARED_ACQUIRE, %rdi)
545 movl $1, %eax /* return TRUE */
555 #define RW_LOCK_EXCLUSIVE_HELD (LCK_RW_WANT_WRITE | LCK_RW_WANT_UPGRADE)
557 * int lck_rw_grab_shared(lck_rw_t *)
560 Entry(lck_rw_grab_shared)
562 mov (%rdi), %eax /* Load state bitfield and interlock */
563 testl $(LCK_RW_INTERLOCK), %eax
565 testl $(RW_LOCK_EXCLUSIVE_HELD), %eax
568 movl %eax, %ecx /* original value in %eax for cmpxchgl */
569 incl %ecx /* Increment reader refcount */
571 cmpxchgl %ecx, (%rdi) /* Attempt atomic exchange */
574 movl $1, %eax /* return success */
577 testl $(LCK_RW_SHARED_MASK), %eax
579 testl $(LCK_RW_PRIV_EXCL), %eax
582 xorl %eax, %eax /* return failure */
590 #define RW_LOCK_EXCLUSIVE_MASK (LCK_RW_SHARED_MASK | LCK_RW_INTERLOCK | \
591 LCK_RW_WANT_UPGRADE | LCK_RW_WANT_WRITE)
593 * void lck_rw_lock_exclusive(lck_rw_t*)
596 Entry(lck_rw_lock_exclusive)
597 mov %gs:CPU_ACTIVE_THREAD, %rcx /* Load thread pointer */
598 incl TH_RWLOCK_COUNT(%rcx) /* Increment count before atomic CAS */
600 mov (%rdi), %eax /* Load state bitfield, interlock and shared count */
601 testl $(RW_LOCK_EXCLUSIVE_MASK), %eax /* Eligible for fastpath? */
602 jne 3f /* no, go slow */
604 movl %eax, %ecx /* original value in %eax for cmpxchgl */
605 orl $(LCK_RW_WANT_WRITE), %ecx
607 cmpxchgl %ecx, (%rdi) /* Attempt atomic exchange */
612 * Dtrace lockstat event: LS_LCK_RW_LOCK_EXCL_ACQUIRE
613 * Implemented by swapping between return and no-op instructions.
614 * See bsd/dev/dtrace/lockstat.c.
616 LOCKSTAT_LABEL(_lck_rw_lock_exclusive_lockstat_patch_point)
618 /* Fall thru when patched, counting on lock pointer in %rdi */
619 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_EXCL_ACQUIRE, %rdi)
626 jmp EXT(lck_rw_lock_exclusive_gen)
630 #define RW_TRY_LOCK_EXCLUSIVE_MASK (LCK_RW_SHARED_MASK | LCK_RW_WANT_UPGRADE | LCK_RW_WANT_WRITE)
632 * void lck_rw_try_lock_exclusive(lck_rw_t *)
634 * Tries to get a write lock.
636 * Returns FALSE if the lock is not held on return.
638 Entry(lck_rw_try_lock_exclusive)
640 mov (%rdi), %eax /* Load state bitfield, interlock and shared count */
641 testl $(LCK_RW_INTERLOCK), %eax
643 testl $(RW_TRY_LOCK_EXCLUSIVE_MASK), %eax
644 jne 3f /* can't get it */
646 movl %eax, %ecx /* original value in %eax for cmpxchgl */
647 orl $(LCK_RW_WANT_WRITE), %ecx
649 cmpxchgl %ecx, (%rdi) /* Attempt atomic exchange */
652 mov %gs:CPU_ACTIVE_THREAD, %rcx /* Load thread pointer */
653 incl TH_RWLOCK_COUNT(%rcx) /* Increment count on success. */
654 /* There is a 3 instr window where preemption may not notice rwlock_count after cmpxchg */
659 * Dtrace lockstat event: LS_LCK_RW_TRY_LOCK_EXCL_ACQUIRE
660 * Implemented by swapping between return and no-op instructions.
661 * See bsd/dev/dtrace/lockstat.c.
663 LOCKSTAT_LABEL(_lck_rw_try_lock_exclusive_lockstat_patch_point)
665 /* Fall thru when patched, counting on lock pointer in %rdi */
666 LOCKSTAT_RECORD(LS_LCK_RW_TRY_LOCK_EXCL_ACQUIRE, %rdi)
668 movl $1, %eax /* return TRUE */
674 xorl %eax, %eax /* return FALSE */
680 * void lck_rw_lock_shared_to_exclusive(lck_rw_t*)
682 * fastpath can be taken if
683 * the current rw_shared_count == 1
684 * AND the interlock is clear
685 * AND RW_WANT_UPGRADE is not set
687 * note that RW_WANT_WRITE could be set, but will not
688 * be indicative of an exclusive hold since we have
689 * a read count on the lock that we have not yet released
690 * we can blow by that state since the lck_rw_lock_exclusive
691 * function will block until rw_shared_count == 0 and
692 * RW_WANT_UPGRADE is clear... it does this check behind
693 * the interlock which we are also checking for
695 * to make the transition we must be able to atomically
696 * set RW_WANT_UPGRADE and get rid of the read count we hold
698 Entry(lck_rw_lock_shared_to_exclusive)
700 mov (%rdi), %eax /* Load state bitfield, interlock and shared count */
701 testl $(LCK_RW_INTERLOCK), %eax
703 testl $(LCK_RW_WANT_UPGRADE), %eax
706 movl %eax, %ecx /* original value in %eax for cmpxchgl */
707 orl $(LCK_RW_WANT_UPGRADE), %ecx /* ask for WANT_UPGRADE */
708 decl %ecx /* and shed our read count */
710 cmpxchgl %ecx, (%rdi) /* Attempt atomic exchange */
712 /* we now own the WANT_UPGRADE */
713 testl $(LCK_RW_SHARED_MASK), %ecx /* check to see if all of the readers are drained */
714 jne 8f /* if not, we need to go wait */
719 * Dtrace lockstat event: LS_LCK_RW_LOCK_SHARED_TO_EXCL_UPGRADE
720 * Implemented by swapping between return and no-op instructions.
721 * See bsd/dev/dtrace/lockstat.c.
723 LOCKSTAT_LABEL(_lck_rw_lock_shared_to_exclusive_lockstat_patch_point)
725 /* Fall thru when patched, counting on lock pointer in %rdi */
726 LOCKSTAT_RECORD(LS_LCK_RW_LOCK_SHARED_TO_EXCL_UPGRADE, %rdi)
728 movl $1, %eax /* return success */
731 2: /* someone else already holds WANT_UPGRADE */
732 movl %eax, %ecx /* original value in %eax for cmpxchgl */
733 decl %ecx /* shed our read count */
734 testl $(LCK_RW_SHARED_MASK), %ecx
735 jne 3f /* we were the last reader */
736 andl $(~LCK_W_WAITING), %ecx /* so clear the wait indicator */
739 cmpxchgl %ecx, (%rdi) /* Attempt atomic exchange */
742 mov %eax, %esi /* put old flags as second arg */
743 /* lock is alread in %rdi */
744 call EXT(lck_rw_lock_shared_to_exclusive_failure)
745 ret /* and pass the failure return along */
750 jmp EXT(lck_rw_lock_shared_to_exclusive_success)
755 rwl_release_error_str:
756 .asciz "Releasing non-exclusive RW lock without a reader refcount!"
760 * lck_rw_type_t lck_rw_done(lck_rw_t *)
765 mov (%rdi), %eax /* Load state bitfield, interlock and reader count */
766 testl $(LCK_RW_INTERLOCK), %eax
767 jne 7f /* wait for interlock to clear */
769 movl %eax, %ecx /* keep original value in %eax for cmpxchgl */
770 testl $(LCK_RW_SHARED_MASK), %ecx /* if reader count == 0, must be exclusive lock */
772 decl %ecx /* Decrement reader count */
773 testl $(LCK_RW_SHARED_MASK), %ecx /* if reader count has now gone to 0, check for waiters */
777 testl $(LCK_RW_WANT_UPGRADE), %ecx
779 andl $(~LCK_RW_WANT_UPGRADE), %ecx
782 testl $(LCK_RW_WANT_WRITE), %ecx
783 je 8f /* lock is not 'owned', go panic */
784 andl $(~LCK_RW_WANT_WRITE), %ecx
787 * test the original values to match what
788 * lck_rw_done_gen is going to do to determine
789 * which wakeups need to happen...
791 * if !(fake_lck->lck_rw_priv_excl && fake_lck->lck_w_waiting)
793 testl $(LCK_W_WAITING), %eax
795 andl $(~LCK_W_WAITING), %ecx
797 testl $(LCK_RW_PRIV_EXCL), %eax
800 andl $(~LCK_R_WAITING), %ecx
803 cmpxchgl %ecx, (%rdi) /* Attempt atomic exchange */
806 mov %eax,%esi /* old flags in %rsi */
807 /* lock is in %rdi already */
808 call EXT(lck_rw_done_gen)
815 LOAD_STRING_ARG0(rwl_release_error_str)
821 * lck_rw_type_t lck_rw_lock_exclusive_to_shared(lck_rw_t *)
824 Entry(lck_rw_lock_exclusive_to_shared)
826 mov (%rdi), %eax /* Load state bitfield, interlock and reader count */
827 testl $(LCK_RW_INTERLOCK), %eax
828 jne 6f /* wait for interlock to clear */
830 movl %eax, %ecx /* keep original value in %eax for cmpxchgl */
831 incl %ecx /* Increment reader count */
833 testl $(LCK_RW_WANT_UPGRADE), %ecx
835 andl $(~LCK_RW_WANT_UPGRADE), %ecx
838 andl $(~LCK_RW_WANT_WRITE), %ecx
841 * test the original values to match what
842 * lck_rw_lock_exclusive_to_shared_gen is going to do to determine
843 * which wakeups need to happen...
845 * if !(fake_lck->lck_rw_priv_excl && fake_lck->lck_w_waiting)
847 testl $(LCK_W_WAITING), %eax
849 testl $(LCK_RW_PRIV_EXCL), %eax
852 andl $(~LCK_R_WAITING), %ecx
855 cmpxchgl %ecx, (%rdi) /* Attempt atomic exchange */
859 call EXT(lck_rw_lock_exclusive_to_shared_gen)
868 * int lck_rw_grab_want(lck_rw_t *)
871 Entry(lck_rw_grab_want)
873 mov (%rdi), %eax /* Load state bitfield, interlock and reader count */
874 testl $(LCK_RW_INTERLOCK), %eax
875 jne 3f /* wait for interlock to clear */
876 testl $(LCK_RW_WANT_WRITE), %eax /* want_write has been grabbed by someone else */
877 jne 2f /* go return failure */
879 movl %eax, %ecx /* original value in %eax for cmpxchgl */
880 orl $(LCK_RW_WANT_WRITE), %ecx
882 cmpxchgl %ecx, (%rdi) /* Attempt atomic exchange */
884 /* we now own want_write */
885 movl $1, %eax /* return success */
888 xorl %eax, %eax /* return failure */
895 #define RW_LOCK_SHARED_OR_UPGRADE_MASK (LCK_RW_SHARED_MASK | LCK_RW_INTERLOCK | LCK_RW_WANT_UPGRADE)
897 * int lck_rw_held_read_or_upgrade(lck_rw_t *)
900 Entry(lck_rw_held_read_or_upgrade)
902 andl $(RW_LOCK_SHARED_OR_UPGRADE_MASK), %eax
908 * N.B.: On x86, statistics are currently recorded for all indirect mutexes.
909 * Also, only the acquire attempt count (GRP_MTX_STAT_UTIL) is maintained
910 * as a 64-bit quantity (this matches the existing PowerPC implementation,
911 * and the new x86 specific statistics are also maintained as 32-bit
915 * Enable this preprocessor define to record the first miss alone
916 * By default, we count every miss, hence multiple misses may be
917 * recorded for a single lock acquire attempt via lck_mtx_lock
919 #undef LOG_FIRST_MISS_ALONE
922 * This preprocessor define controls whether the R-M-W update of the
923 * per-group statistics elements are atomic (LOCK-prefixed)
924 * Enabled by default.
926 #define ATOMIC_STAT_UPDATES 1
928 #if defined(ATOMIC_STAT_UPDATES)
929 #define LOCK_IF_ATOMIC_STAT_UPDATES lock
931 #define LOCK_IF_ATOMIC_STAT_UPDATES
932 #endif /* ATOMIC_STAT_UPDATES */
936 * For most routines, the lck_mtx_t pointer is loaded into a
937 * register initially, and the owner field checked for indirection.
938 * Eventually the lock owner is loaded into a register and examined.
941 #define M_OWNER MUTEX_OWNER
942 #define M_PTR MUTEX_PTR
943 #define M_STATE MUTEX_STATE
946 #define LMTX_ENTER_EXTENDED \
947 mov M_PTR(%rdx), %rdx ; \
949 mov MUTEX_GRP(%rdx), %r10 ; \
950 LOCK_IF_ATOMIC_STAT_UPDATES ; \
951 incq GRP_MTX_STAT_UTIL(%r10)
954 #if LOG_FIRST_MISS_ALONE
955 #define LMTX_UPDATE_MISS \
958 LOCK_IF_ATOMIC_STAT_UPDATES ; \
959 incl GRP_MTX_STAT_MISS(%r10) ; \
963 #define LMTX_UPDATE_MISS \
964 LOCK_IF_ATOMIC_STAT_UPDATES ; \
965 incl GRP_MTX_STAT_MISS(%r10)
969 #if LOG_FIRST_MISS_ALONE
970 #define LMTX_UPDATE_WAIT \
973 LOCK_IF_ATOMIC_STAT_UPDATES ; \
974 incl GRP_MTX_STAT_WAIT(%r10) ; \
978 #define LMTX_UPDATE_WAIT \
979 LOCK_IF_ATOMIC_STAT_UPDATES ; \
980 incl GRP_MTX_STAT_WAIT(%r10)
985 * Record the "direct wait" statistic, which indicates if a
986 * miss proceeded to block directly without spinning--occurs
987 * if the owner of the mutex isn't running on another processor
988 * at the time of the check.
990 #define LMTX_UPDATE_DIRECT_WAIT \
991 LOCK_IF_ATOMIC_STAT_UPDATES ; \
992 incl GRP_MTX_STAT_DIRECT_WAIT(%r10)
995 #define LMTX_CALLEXT1(func_name) \
1003 call EXT(func_name) ; \
1012 #define LMTX_CALLEXT2(func_name, reg) \
1021 call EXT(func_name) ; \
1031 #define M_WAITERS_MSK 0x0000ffff
1032 #define M_PRIORITY_MSK 0x00ff0000
1033 #define M_ILOCKED_MSK 0x01000000
1034 #define M_MLOCKED_MSK 0x02000000
1035 #define M_PROMOTED_MSK 0x04000000
1036 #define M_SPIN_MSK 0x08000000
1039 * void lck_mtx_assert(lck_mtx_t* l, unsigned int)
1040 * Takes the address of a lock, and an assertion type as parameters.
1041 * The assertion can take one of two forms determine by the type
1042 * parameter: either the lock is held by the current thread, and the
1043 * type is LCK_MTX_ASSERT_OWNED, or it isn't and the type is
1044 * LCK_MTX_ASSERT_NOTOWNED. Calls panic on assertion failure.
1048 NONLEAF_ENTRY(lck_mtx_assert)
1049 mov %rdi, %rdx /* Load lock address */
1050 mov %gs:CPU_ACTIVE_THREAD, %rax /* Load current thread */
1052 mov M_STATE(%rdx), %ecx
1053 cmp $(MUTEX_IND), %ecx /* Is this an indirect mutex? */
1055 mov M_PTR(%rdx), %rdx /* If so, take indirection */
1057 mov M_OWNER(%rdx), %rcx /* Load owner */
1058 cmp $(MUTEX_ASSERT_OWNED), %rsi
1059 jne 2f /* Assert ownership? */
1060 cmp %rax, %rcx /* Current thread match? */
1061 jne 3f /* no, go panic */
1062 testl $(M_ILOCKED_MSK | M_MLOCKED_MSK), M_STATE(%rdx)
1064 1: /* yes, we own it */
1067 cmp %rax, %rcx /* Current thread match? */
1068 jne 1b /* No, return */
1071 LOAD_STRING_ARG0(mutex_assert_owned_str)
1076 LOAD_STRING_ARG0(mutex_assert_not_owned_str)
1084 LOAD_STRING_ARG0(mutex_interlock_destroyed_str)
1089 mutex_assert_not_owned_str:
1090 .asciz "mutex (%p) not owned\n"
1091 mutex_assert_owned_str:
1092 .asciz "mutex (%p) owned\n"
1093 mutex_interlock_destroyed_str:
1094 .asciz "trying to interlock destroyed mutex (%p)"
1101 * lck_mtx_try_lock()
1103 * lck_mtx_lock_spin()
1104 * lck_mtx_lock_spin_always()
1105 * lck_mtx_try_lock_spin()
1106 * lck_mtx_try_lock_spin_always()
1107 * lck_mtx_convert_spin()
1109 NONLEAF_ENTRY(lck_mtx_lock_spin_always)
1110 mov %rdi, %rdx /* fetch lock pointer */
1111 jmp Llmls_avoid_check
1113 NONLEAF_ENTRY(lck_mtx_lock_spin)
1114 mov %rdi, %rdx /* fetch lock pointer */
1116 CHECK_PREEMPTION_LEVEL()
1118 mov M_STATE(%rdx), %ecx
1119 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), %ecx /* is the interlock or mutex held */
1121 Llmls_try: /* no - can't be INDIRECT, DESTROYED or locked */
1122 mov %rcx, %rax /* eax contains snapshot for cmpxchgl */
1123 or $(M_ILOCKED_MSK | M_SPIN_MSK), %ecx
1127 cmpxchg %ecx, M_STATE(%rdx) /* atomic compare and exchange */
1128 jne Llmls_busy_disabled
1130 mov %gs:CPU_ACTIVE_THREAD, %rax
1131 mov %rax, M_OWNER(%rdx) /* record owner of interlock */
1135 incl TH_MUTEX_COUNT(%rax) /* lock statistic */
1137 #endif /* MACH_LDEBUG */
1139 /* return with the interlock held and preemption disabled */
1142 LOCKSTAT_LABEL(_lck_mtx_lock_spin_lockstat_patch_point)
1144 /* inherit lock pointer in %rdx above */
1145 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_SPIN_ACQUIRE, %rdx)
1150 test $M_ILOCKED_MSK, %ecx /* is the interlock held */
1151 jz Llml_contended /* no, must have been the mutex */
1153 cmp $(MUTEX_DESTROYED), %ecx /* check to see if its marked destroyed */
1154 je lck_mtx_destroyed
1155 cmp $(MUTEX_IND), %ecx /* Is this an indirect mutex */
1156 jne Llmls_loop /* no... must be interlocked */
1160 mov M_STATE(%rdx), %ecx
1161 test $(M_SPIN_MSK), %ecx
1164 LMTX_UPDATE_MISS /* M_SPIN_MSK was set, so M_ILOCKED_MSK must also be present */
1167 mov M_STATE(%rdx), %ecx
1169 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), %ecx
1171 test $(M_MLOCKED_MSK), %ecx
1172 jnz Llml_contended /* mutex owned by someone else, go contend for it */
1175 Llmls_busy_disabled:
1181 NONLEAF_ENTRY(lck_mtx_lock)
1182 mov %rdi, %rdx /* fetch lock pointer */
1184 CHECK_PREEMPTION_LEVEL()
1186 mov M_STATE(%rdx), %ecx
1187 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), %ecx /* is the interlock or mutex held */
1189 Llml_try: /* no - can't be INDIRECT, DESTROYED or locked */
1190 mov %rcx, %rax /* eax contains snapshot for cmpxchgl */
1191 or $(M_ILOCKED_MSK | M_MLOCKED_MSK), %ecx
1195 cmpxchg %ecx, M_STATE(%rdx) /* atomic compare and exchange */
1196 jne Llml_busy_disabled
1198 mov %gs:CPU_ACTIVE_THREAD, %rax
1199 mov %rax, M_OWNER(%rdx) /* record owner of mutex */
1203 incl TH_MUTEX_COUNT(%rax) /* lock statistic */
1205 #endif /* MACH_LDEBUG */
1207 testl $(M_WAITERS_MSK), M_STATE(%rdx)
1210 LMTX_CALLEXT1(lck_mtx_lock_acquire_x86)
1213 andl $(~M_ILOCKED_MSK), M_STATE(%rdx)
1216 cmp %rdx, %rdi /* is this an extended mutex */
1221 LOCKSTAT_LABEL(_lck_mtx_lock_lockstat_patch_point)
1223 /* inherit lock pointer in %rdx above */
1224 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_ACQUIRE, %rdx)
1230 LOCKSTAT_LABEL(_lck_mtx_lock_ext_lockstat_patch_point)
1232 /* inherit lock pointer in %rdx above */
1233 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_LOCK_ACQUIRE, %rdx)
1239 test $M_ILOCKED_MSK, %ecx /* is the interlock held */
1240 jz Llml_contended /* no, must have been the mutex */
1242 cmp $(MUTEX_DESTROYED), %ecx /* check to see if its marked destroyed */
1243 je lck_mtx_destroyed
1244 cmp $(MUTEX_IND), %ecx /* Is this an indirect mutex? */
1245 jne Llml_loop /* no... must be interlocked */
1249 mov M_STATE(%rdx), %ecx
1250 test $(M_SPIN_MSK), %ecx
1253 LMTX_UPDATE_MISS /* M_SPIN_MSK was set, so M_ILOCKED_MSK must also be present */
1256 mov M_STATE(%rdx), %ecx
1258 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), %ecx
1260 test $(M_MLOCKED_MSK), %ecx
1261 jnz Llml_contended /* mutex owned by someone else, go contend for it */
1270 cmp %rdx, %rdi /* is this an extended mutex */
1274 LMTX_CALLEXT1(lck_mtx_lock_spinwait_x86)
1277 jz Llml_acquired /* acquired mutex, interlock held and preemption disabled */
1279 cmp $1, %rax /* check for direct wait status */
1281 cmp %rdx, %rdi /* is this an extended mutex */
1283 LMTX_UPDATE_DIRECT_WAIT
1285 mov M_STATE(%rdx), %ecx
1286 test $(M_ILOCKED_MSK), %ecx
1289 mov %rcx, %rax /* eax contains snapshot for cmpxchgl */
1290 or $(M_ILOCKED_MSK), %ecx /* try to take the interlock */
1294 cmpxchg %ecx, M_STATE(%rdx) /* atomic compare and exchange */
1297 test $(M_MLOCKED_MSK), %ecx /* we've got the interlock and */
1299 or $(M_MLOCKED_MSK), %ecx /* the mutex is free... grab it directly */
1300 mov %ecx, M_STATE(%rdx)
1302 mov %gs:CPU_ACTIVE_THREAD, %rax
1303 mov %rax, M_OWNER(%rdx) /* record owner of mutex */
1307 incl TH_MUTEX_COUNT(%rax) /* lock statistic */
1309 #endif /* MACH_LDEBUG */
1312 testl $(M_WAITERS_MSK), M_STATE(%rdx)
1314 mov M_OWNER(%rdx), %rax
1315 mov TH_WAS_PROMOTED_ON_WAKEUP(%rax), %eax
1319 LMTX_CALLEXT1(lck_mtx_lock_acquire_x86)
1322 3: /* interlock held, mutex busy */
1323 cmp %rdx, %rdi /* is this an extended mutex */
1327 LMTX_CALLEXT1(lck_mtx_lock_wait_x86)
1336 NONLEAF_ENTRY(lck_mtx_try_lock_spin_always)
1337 mov %rdi, %rdx /* fetch lock pointer */
1338 jmp Llmts_avoid_check
1340 NONLEAF_ENTRY(lck_mtx_try_lock_spin)
1341 mov %rdi, %rdx /* fetch lock pointer */
1344 mov M_STATE(%rdx), %ecx
1345 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), %ecx /* is the interlock or mutex held */
1347 Llmts_try: /* no - can't be INDIRECT, DESTROYED or locked */
1348 mov %rcx, %rax /* eax contains snapshot for cmpxchgl */
1349 or $(M_ILOCKED_MSK | M_SPIN_MSK), %rcx
1353 cmpxchg %ecx, M_STATE(%rdx) /* atomic compare and exchange */
1354 jne Llmts_busy_disabled
1356 mov %gs:CPU_ACTIVE_THREAD, %rax
1357 mov %rax, M_OWNER(%rdx) /* record owner of mutex */
1361 incl TH_MUTEX_COUNT(%rax) /* lock statistic */
1363 #endif /* MACH_LDEBUG */
1368 mov $1, %rax /* return success */
1369 LOCKSTAT_LABEL(_lck_mtx_try_lock_spin_lockstat_patch_point)
1371 /* inherit lock pointer in %rdx above */
1372 LOCKSTAT_RECORD(LS_LCK_MTX_TRY_SPIN_LOCK_ACQUIRE, %rdx)
1374 mov $1, %rax /* return success */
1378 test $(M_ILOCKED_MSK), %ecx /* is the interlock held */
1379 jz Llmts_fail /* no, must be held as a mutex */
1381 cmp $(MUTEX_DESTROYED), %ecx /* check to see if its marked destroyed */
1382 je lck_mtx_destroyed
1383 cmp $(MUTEX_IND), %ecx /* Is this an indirect mutex? */
1389 mov M_STATE(%rdx), %ecx
1391 test $(M_MLOCKED_MSK | M_SPIN_MSK), %ecx
1393 test $(M_ILOCKED_MSK), %ecx
1397 Llmts_busy_disabled:
1403 NONLEAF_ENTRY(lck_mtx_try_lock)
1404 mov %rdi, %rdx /* fetch lock pointer */
1406 mov M_STATE(%rdx), %ecx
1407 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), %ecx /* is the interlock or mutex held */
1409 Llmt_try: /* no - can't be INDIRECT, DESTROYED or locked */
1410 mov %rcx, %rax /* eax contains snapshot for cmpxchgl */
1411 or $(M_ILOCKED_MSK | M_MLOCKED_MSK), %ecx
1415 cmpxchg %ecx, M_STATE(%rdx) /* atomic compare and exchange */
1416 jne Llmt_busy_disabled
1418 mov %gs:CPU_ACTIVE_THREAD, %rax
1419 mov %rax, M_OWNER(%rdx) /* record owner of mutex */
1423 incl TH_MUTEX_COUNT(%rax) /* lock statistic */
1425 #endif /* MACH_LDEBUG */
1427 test $(M_WAITERS_MSK), %ecx
1430 LMTX_CALLEXT1(lck_mtx_lock_acquire_x86)
1432 andl $(~M_ILOCKED_MSK), M_STATE(%rdx)
1437 mov $1, %rax /* return success */
1438 /* Dtrace probe: LS_LCK_MTX_TRY_LOCK_ACQUIRE */
1439 LOCKSTAT_LABEL(_lck_mtx_try_lock_lockstat_patch_point)
1441 /* inherit lock pointer in %rdx from above */
1442 LOCKSTAT_RECORD(LS_LCK_MTX_TRY_LOCK_ACQUIRE, %rdx)
1444 mov $1, %rax /* return success */
1448 test $(M_ILOCKED_MSK), %ecx /* is the interlock held */
1449 jz Llmt_fail /* no, must be held as a mutex */
1451 cmp $(MUTEX_DESTROYED), %ecx /* check to see if its marked destroyed */
1452 je lck_mtx_destroyed
1453 cmp $(MUTEX_IND), %ecx /* Is this an indirect mutex? */
1459 mov M_STATE(%rdx), %ecx
1461 test $(M_MLOCKED_MSK | M_SPIN_MSK), %ecx
1463 test $(M_ILOCKED_MSK), %ecx
1474 cmp %rdx, %rdi /* is this an extended mutex */
1483 NONLEAF_ENTRY(lck_mtx_convert_spin)
1484 mov %rdi, %rdx /* fetch lock pointer */
1486 mov M_STATE(%rdx), %ecx
1487 cmp $(MUTEX_IND), %ecx /* Is this an indirect mutex? */
1489 mov M_PTR(%rdx), %rdx /* If so, take indirection */
1490 mov M_STATE(%rdx), %ecx
1492 test $(M_MLOCKED_MSK), %ecx /* already owned as a mutex, just return */
1494 test $(M_WAITERS_MSK), %ecx /* are there any waiters? */
1497 LMTX_CALLEXT1(lck_mtx_lock_acquire_x86)
1498 mov M_STATE(%rdx), %ecx
1500 and $(~(M_ILOCKED_MSK | M_SPIN_MSK)), %ecx /* convert from spin version to mutex */
1501 or $(M_MLOCKED_MSK), %ecx
1502 mov %ecx, M_STATE(%rdx) /* since I own the interlock, I don't need an atomic update */
1510 NONLEAF_ENTRY(lck_mtx_unlock)
1511 mov %rdi, %rdx /* fetch lock pointer */
1513 mov M_STATE(%rdx), %ecx
1515 cmp $(MUTEX_IND), %ecx /* Is this an indirect mutex? */
1519 test $(M_MLOCKED_MSK), %ecx /* check for full mutex */
1522 test $(M_ILOCKED_MSK), %rcx /* have to wait for interlock to clear */
1525 mov %rcx, %rax /* eax contains snapshot for cmpxchgl */
1526 and $(~M_MLOCKED_MSK), %ecx /* drop mutex */
1527 or $(M_ILOCKED_MSK), %ecx /* pick up interlock */
1531 cmpxchg %ecx, M_STATE(%rdx) /* atomic compare and exchange */
1532 jne Llmu_busy_disabled /* branch on failure to spin loop */
1536 mov %rax, M_OWNER(%rdx)
1537 mov %rcx, %rax /* keep original state in %ecx for later evaluation */
1538 and $(~(M_ILOCKED_MSK | M_SPIN_MSK | M_PROMOTED_MSK)), %rax
1540 test $(M_WAITERS_MSK), %eax
1542 dec %eax /* decrement waiter count */
1544 mov %eax, M_STATE(%rdx) /* since I own the interlock, I don't need an atomic update */
1547 /* perform lock statistics after drop to prevent delay */
1548 mov %gs:CPU_ACTIVE_THREAD, %rax
1551 decl TH_MUTEX_COUNT(%rax) /* lock statistic */
1553 #endif /* MACH_LDEBUG */
1555 test $(M_PROMOTED_MSK | M_WAITERS_MSK), %ecx
1558 LMTX_CALLEXT2(lck_mtx_unlock_wakeup_x86, %rcx)
1567 /* Dtrace: LS_LCK_MTX_UNLOCK_RELEASE */
1568 LOCKSTAT_LABEL(_lck_mtx_unlock_lockstat_patch_point)
1570 /* inherit lock pointer in %rdx from above */
1571 LOCKSTAT_RECORD(LS_LCK_MTX_UNLOCK_RELEASE, %rdx)
1577 /* Dtrace: LS_LCK_MTX_EXT_UNLOCK_RELEASE */
1578 LOCKSTAT_LABEL(_lck_mtx_ext_unlock_lockstat_patch_point)
1580 /* inherit lock pointer in %rdx from above */
1581 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_UNLOCK_RELEASE, %rdx)
1590 mov M_STATE(%rdx), %ecx
1594 mov M_PTR(%rdx), %rdx
1595 mov M_OWNER(%rdx), %rax
1596 mov %gs:CPU_ACTIVE_THREAD, %rcx
1597 CHECK_UNLOCK(%rcx, %rax)
1598 mov M_STATE(%rdx), %ecx
1603 LEAF_ENTRY(lck_mtx_ilk_unlock)
1604 mov %rdi, %rdx /* fetch lock pointer - no indirection here */
1606 andl $(~M_ILOCKED_MSK), M_STATE(%rdx)
1608 PREEMPTION_ENABLE /* need to re-enable preemption */
1614 LEAF_ENTRY(lck_mtx_lock_grab_mutex)
1615 mov %rdi, %rdx /* fetch lock pointer - no indirection here */
1617 mov M_STATE(%rdx), %ecx
1619 test $(M_ILOCKED_MSK | M_MLOCKED_MSK), %ecx /* can't have the mutex yet */
1622 mov %rcx, %rax /* eax contains snapshot for cmpxchgl */
1623 or $(M_ILOCKED_MSK | M_MLOCKED_MSK), %ecx
1627 cmpxchg %ecx, M_STATE(%rdx) /* atomic compare and exchange */
1628 jne 2f /* branch on failure to spin loop */
1630 mov %gs:CPU_ACTIVE_THREAD, %rax
1631 mov %rax, M_OWNER(%rdx) /* record owner of mutex */
1635 incl TH_MUTEX_COUNT(%rax) /* lock statistic */
1637 #endif /* MACH_LDEBUG */
1639 mov $1, %rax /* return success */
1644 xor %rax, %rax /* return failure */
1649 LEAF_ENTRY(lck_mtx_lock_mark_destroyed)
1652 mov M_STATE(%rdx), %ecx
1653 cmp $(MUTEX_IND), %ecx /* Is this an indirect mutex? */
1656 movl $(MUTEX_DESTROYED), M_STATE(%rdx) /* convert to destroyed state */
1659 test $(M_ILOCKED_MSK), %rcx /* have to wait for interlock to clear */
1663 mov %rcx, %rax /* eax contains snapshot for cmpxchgl */
1664 or $(M_ILOCKED_MSK), %ecx
1666 cmpxchg %ecx, M_STATE(%rdx) /* atomic compare and exchange */
1667 jne 4f /* branch on failure to spin loop */
1668 movl $(MUTEX_DESTROYED), M_STATE(%rdx) /* convert to destroyed state */
1671 LEAF_RET /* return with M_ILOCKED set */
1678 LEAF_ENTRY(preemption_underflow_panic)
1680 incl %gs:CPU_PREEMPTION_LEVEL
1682 LOAD_STRING_ARG0(16f)
1686 16: String "Preemption level underflow, possible cause unlocking an unlocked mutex or spinlock"
1690 LEAF_ENTRY(_disable_preemption)
1693 #endif /* MACH_RT */
1696 LEAF_ENTRY(_enable_preemption)
1699 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
1701 movl %gs:CPU_PREEMPTION_LEVEL,%esi
1703 LOAD_STRING_ARG0(_enable_preemption_less_than_zero)
1707 _enable_preemption_less_than_zero:
1708 .asciz "_enable_preemption: preemption_level(%d) < 0!"
1711 #endif /* MACH_ASSERT */
1713 #endif /* MACH_RT */
1716 LEAF_ENTRY(_enable_preemption_no_check)
1719 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
1722 LOAD_STRING_ARG0(_enable_preemption_no_check_less_than_zero)
1726 _enable_preemption_no_check_less_than_zero:
1727 .asciz "_enable_preemption_no_check: preemption_level <= 0!"
1730 #endif /* MACH_ASSERT */
1731 _ENABLE_PREEMPTION_NO_CHECK
1732 #endif /* MACH_RT */
1736 LEAF_ENTRY(_mp_disable_preemption)
1739 #endif /* MACH_RT */
1742 LEAF_ENTRY(_mp_enable_preemption)
1745 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
1747 movl %gs:CPU_PREEMPTION_LEVEL,%esi
1749 LOAD_STRING_ARG0(_mp_enable_preemption_less_than_zero)
1753 _mp_enable_preemption_less_than_zero:
1754 .asciz "_mp_enable_preemption: preemption_level (%d) <= 0!"
1757 #endif /* MACH_ASSERT */
1759 #endif /* MACH_RT */
1762 LEAF_ENTRY(_mp_enable_preemption_no_check)
1765 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
1768 LOAD_STRING_ARG0(_mp_enable_preemption_no_check_less_than_zero)
1772 _mp_enable_preemption_no_check_less_than_zero:
1773 .asciz "_mp_enable_preemption_no_check: preemption_level <= 0!"
1776 #endif /* MACH_ASSERT */
1777 _ENABLE_PREEMPTION_NO_CHECK
1778 #endif /* MACH_RT */
1782 * Atomic primitives, prototyped in kern/simple_lock.h
1784 LEAF_ENTRY(hw_atomic_add)
1791 movl %esi, %eax /* Load addend */
1792 lock xaddl %eax, (%rdi) /* Atomic exchange and add */
1793 addl %esi, %eax /* Calculate result */
1796 LEAF_ENTRY(hw_atomic_sub)
1805 lock xaddl %eax, (%rdi) /* Atomic exchange and add */
1806 addl %esi, %eax /* Calculate result */
1809 LEAF_ENTRY(hw_atomic_or)
1818 movl %esi, %edx /* Load mask */
1820 lock cmpxchgl %edx, (%rdi) /* Atomic CAS */
1822 movl %edx, %eax /* Result */
1825 * A variant of hw_atomic_or which doesn't return a value.
1826 * The implementation is thus comparatively more efficient.
1829 LEAF_ENTRY(hw_atomic_or_noret)
1837 orl %esi, (%rdi) /* Atomic OR */
1841 LEAF_ENTRY(hw_atomic_and)
1850 movl %esi, %edx /* Load mask */
1852 lock cmpxchgl %edx, (%rdi) /* Atomic CAS */
1854 movl %edx, %eax /* Result */
1857 * A variant of hw_atomic_and which doesn't return a value.
1858 * The implementation is thus comparatively more efficient.
1861 LEAF_ENTRY(hw_atomic_and_noret)
1868 lock andl %esi, (%rdi) /* Atomic OR */