2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 * Mach Operating System
28 * Copyright (c) 1989 Carnegie-Mellon University
29 * All rights reserved. The CMU software License Agreement specifies
30 * the terms and conditions for use and redistribution.
34 #include <platforms.h>
35 #include <mach_ldebug.h>
40 #define PAUSE rep; nop
43 * When performance isn't the only concern, it's
44 * nice to build stack frames...
46 #define BUILD_STACK_FRAMES (GPROF || \
47 ((MACH_LDEBUG || ETAP_LOCK_TRACE) && MACH_KDB))
49 #if BUILD_STACK_FRAMES
51 /* STack-frame-relative: */
56 #define LEAF_ENTRY(name) \
61 #define LEAF_ENTRY2(n1,n2) \
71 #else /* BUILD_STACK_FRAMES */
73 /* Stack-pointer-relative: */
78 #define LEAF_ENTRY(name) \
81 #define LEAF_ENTRY2(n1,n2) \
88 #endif /* BUILD_STACK_FRAMES */
91 /* Non-leaf routines always have a stack frame: */
93 #define NONLEAF_ENTRY(name) \
98 #define NONLEAF_ENTRY2(n1,n2) \
104 #define NONLEAF_RET \
110 #define M_LOCKED MUTEX_LOCKED(%edx)
111 #define M_WAITERS MUTEX_WAITERS(%edx)
112 #define M_PROMOTED_PRI MUTEX_PROMOTED_PRI(%edx)
113 #define M_ITAG MUTEX_ITAG(%edx)
114 #define M_PTR MUTEX_PTR(%edx)
116 #define M_TYPE MUTEX_TYPE(%edx)
117 #define M_PC MUTEX_PC(%edx)
118 #define M_THREAD MUTEX_THREAD(%edx)
119 #endif /* MACH_LDEBUG */
122 #define CX(addr,reg) addr(,reg,4)
126 * Routines for general lock debugging.
128 #define S_TYPE SLOCK_TYPE(%edx)
129 #define S_PC SLOCK_PC(%edx)
130 #define S_THREAD SLOCK_THREAD(%edx)
131 #define S_DURATIONH SLOCK_DURATIONH(%edx)
132 #define S_DURATIONL SLOCK_DURATIONL(%edx)
135 * Checks for expected lock types and calls "panic" on
136 * mismatch. Detects calls to Mutex functions with
137 * type simplelock and vice versa.
139 #define CHECK_MUTEX_TYPE() \
140 cmpl $ MUTEX_TAG,M_TYPE ; \
146 2: String "not a mutex!" ; \
150 #define CHECK_SIMPLE_LOCK_TYPE() \
151 cmpl $ USLOCK_TAG,S_TYPE ; \
157 2: String "not a simple lock!" ; \
162 * If one or more simplelocks are currently held by a thread,
163 * an attempt to acquire a mutex will cause this check to fail
164 * (since a mutex lock may context switch, holding a simplelock
165 * is not a good thing).
168 #define CHECK_PREEMPTION_LEVEL() \
169 cmpl $0,%gs:CPU_PREEMPTION_LEVEL ; \
175 2: String "preemption_level != 0!" ; \
179 #define CHECK_PREEMPTION_LEVEL()
182 #define CHECK_NO_SIMPLELOCKS() \
183 cmpl $0,%gs:CPU_SIMPLE_LOCK_COUNT ; \
189 2: String "simple_locks_held!" ; \
194 * Verifies return to the correct thread in "unlock" situations.
196 #define CHECK_THREAD(thd) \
197 movl %gs:CPU_ACTIVE_THREAD,%ecx ; \
206 2: String "wrong thread!" ; \
210 #define CHECK_MYLOCK(thd) \
211 movl %gs:CPU_ACTIVE_THREAD,%ecx ; \
220 2: String "mylock attempt!" ; \
224 #define METER_SIMPLE_LOCK_LOCK(reg) \
226 call EXT(meter_simple_lock) ; \
229 #define METER_SIMPLE_LOCK_UNLOCK(reg) \
231 call EXT(meter_simple_unlock) ; \
234 #else /* MACH_LDEBUG */
235 #define CHECK_MUTEX_TYPE()
236 #define CHECK_SIMPLE_LOCK_TYPE
237 #define CHECK_THREAD(thd)
238 #define CHECK_PREEMPTION_LEVEL()
239 #define CHECK_NO_SIMPLELOCKS()
240 #define CHECK_MYLOCK(thd)
241 #define METER_SIMPLE_LOCK_LOCK(reg)
242 #define METER_SIMPLE_LOCK_UNLOCK(reg)
243 #endif /* MACH_LDEBUG */
247 * void hw_lock_init(hw_lock_t)
249 * Initialize a hardware lock.
251 LEAF_ENTRY(hw_lock_init)
252 movl L_ARG0,%edx /* fetch lock pointer */
253 movl $0,0(%edx) /* clear the lock */
257 * void hw_lock_lock(hw_lock_t)
259 * Acquire lock, spinning until it becomes available.
260 * MACH_RT: also return with preemption disabled.
262 LEAF_ENTRY(hw_lock_lock)
263 movl L_ARG0,%edx /* fetch lock pointer */
266 1: DISABLE_PREEMPTION
268 testl %eax,%eax /* lock locked? */
269 jne 3f /* branch if so */
270 lock; cmpxchgl %ecx,0(%edx) /* try to acquire the HW lock */
272 movl $1,%eax /* In case this was a timeout call */
273 LEAF_RET /* if yes, then nothing left to do */
275 3: ENABLE_PREEMPTION /* no reason we can't be preemptable */
276 PAUSE /* pause for hyper-threading */
277 jmp 1b /* try again */
280 * unsigned int hw_lock_to(hw_lock_t, unsigned int)
282 * Acquire lock, spinning until it becomes available or timeout.
283 * MACH_RT: also return with preemption disabled.
285 LEAF_ENTRY(hw_lock_to)
287 movl L_ARG0,%edx /* fetch lock pointer */
290 * Attempt to grab the lock immediately
291 * - fastpath without timeout nonsense.
295 testl %eax,%eax /* lock locked? */
296 jne 2f /* branch if so */
297 lock; cmpxchgl %ecx,0(%edx) /* try to acquire the HW lock */
298 jne 2f /* branch on failure */
303 #define INNER_LOOP_COUNT 1000
305 * Failed to get the lock so set the timeout
306 * and then spin re-checking the lock but pausing
307 * every so many (INNER_LOOP_COUNT) spins to check for timeout.
309 movl L_ARG1,%ecx /* fetch timeout */
314 rdtsc /* read cyclecount into %edx:%eax */
315 addl %ecx,%eax /* fetch and timeout */
316 adcl $0,%edx /* add carry */
318 mov %eax,%ebx /* %ecx:%ebx is the timeout expiry */
320 ENABLE_PREEMPTION /* no reason not to be preempted now */
323 * The inner-loop spin to look for the lock being freed.
325 mov $(INNER_LOOP_COUNT),%edx
327 PAUSE /* pause for hyper-threading */
328 movl 0(%edi),%eax /* spin checking lock value in cache */
330 je 6f /* zero => unlocked, try to grab it */
331 decl %edx /* decrement inner loop count */
332 jnz 5b /* time to check for timeout? */
335 * Here after spinning INNER_LOOP_COUNT times, check for timeout
337 rdtsc /* cyclecount into %edx:%eax */
338 cmpl %ecx,%edx /* compare high-order 32-bits */
339 jb 4b /* continue spinning if less, or */
340 cmpl %ebx,%eax /* compare low-order 32-bits */
341 jb 5b /* continue if less, else bail */
342 xor %eax,%eax /* with 0 return value */
349 * Here to try to grab the lock that now appears to be free
352 movl 8+L_PC,%edx /* calling pc (8+ for pushed regs) */
354 lock; cmpxchgl %edx,0(%edi) /* try to acquire the HW lock */
355 jne 3b /* no - spin again */
356 movl $1,%eax /* yes */
362 * void hw_lock_unlock(hw_lock_t)
364 * Unconditionally release lock.
365 * MACH_RT: release preemption level.
367 LEAF_ENTRY(hw_lock_unlock)
368 movl L_ARG0,%edx /* fetch lock pointer */
369 movl $0,0(%edx) /* clear the lock */
374 * unsigned int hw_lock_try(hw_lock_t)
375 * MACH_RT: returns with preemption disabled on success.
377 LEAF_ENTRY(hw_lock_try)
378 movl L_ARG0,%edx /* fetch lock pointer */
385 lock; cmpxchgl %ecx,0(%edx) /* try to acquire the HW lock */
388 movl $1,%eax /* success */
391 1: ENABLE_PREEMPTION /* failure: release preemption... */
392 xorl %eax,%eax /* ...and return failure */
396 * unsigned int hw_lock_held(hw_lock_t)
397 * MACH_RT: doesn't change preemption state.
398 * N.B. Racy, of course.
400 LEAF_ENTRY(hw_lock_held)
401 movl L_ARG0,%edx /* fetch lock pointer */
403 movl 0(%edx),%eax /* check lock value */
406 cmovne %ecx,%eax /* 0 => unlocked, 1 => locked */
409 LEAF_ENTRY(mutex_init)
410 movl L_ARG0,%edx /* fetch lock pointer */
412 movl %eax,M_ILK /* clear interlock */
413 movl %eax,M_LOCKED /* clear locked flag */
414 movw %ax,M_WAITERS /* init waiter count */
415 movw %ax,M_PROMOTED_PRI
418 movl $ MUTEX_TAG,M_TYPE /* set lock type */
419 movl %eax,M_PC /* init caller pc */
420 movl %eax,M_THREAD /* and owning thread */
425 NONLEAF_ENTRY2(mutex_lock,_mutex_lock)
427 movl B_ARG0,%edx /* fetch lock pointer */
430 CHECK_NO_SIMPLELOCKS()
431 CHECK_PREEMPTION_LEVEL()
433 pushf /* save interrupt state */
434 cli /* disable interrupts */
440 movl M_ILK,%eax /* read interlock */
441 testl %eax,%eax /* unlocked? */
442 je 1f /* yes - attempt to lock it */
443 PAUSE /* no - pause */
444 jmp ml_get_hw /* try again */
446 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
447 jne ml_get_hw /* branch on failure to retry */
449 movl M_LOCKED,%ecx /* get lock owner */
450 testl %ecx,%ecx /* is the mutex locked? */
451 jne ml_fail /* yes, we lose */
452 movl %gs:CPU_ACTIVE_THREAD,%ecx
461 pushl %edx /* save mutex address */
463 call EXT(lck_mtx_lock_acquire)
465 popl %edx /* restore mutex address */
470 popf /* restore interrupt state */
476 CHECK_MYLOCK(M_THREAD)
478 pushl %edx /* push mutex address */
479 call EXT(lck_mtx_lock_wait) /* wait for the lock */
481 movl B_ARG0,%edx /* refetch mutex address */
482 jmp ml_retry /* and try again */
484 NONLEAF_ENTRY2(mutex_try,_mutex_try)
486 movl B_ARG0,%edx /* fetch lock pointer */
489 CHECK_NO_SIMPLELOCKS()
493 pushf /* save interrupt state */
494 cli /* disable interrupts */
497 movl M_ILK,%eax /* read interlock */
498 testl %eax,%eax /* unlocked? */
499 je 1f /* yes - attempt to lock it */
500 PAUSE /* no - pause */
501 jmp mt_get_hw /* try again */
503 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
504 jne mt_get_hw /* branch on failure to retry */
506 movl M_LOCKED,%ecx /* get lock owner */
507 testl %ecx,%ecx /* is the mutex locked? */
508 jne mt_fail /* yes, we lose */
509 movl %gs:CPU_ACTIVE_THREAD,%ecx
518 pushl %edx /* save mutex address */
520 call EXT(lck_mtx_lock_acquire)
522 popl %edx /* restore mutex address */
527 popf /* restore interrupt state */
537 popf /* restore interrupt state */
543 NONLEAF_ENTRY(mutex_unlock)
544 movl B_ARG0,%edx /* fetch lock pointer */
547 CHECK_THREAD(M_THREAD)
551 pushf /* save interrupt state */
552 cli /* disable interrupts */
555 movl M_ILK,%eax /* read interlock */
556 testl %eax,%eax /* unlocked? */
557 je 1f /* yes - attempt to lock it */
558 PAUSE /* no - pause */
559 jmp mu_get_hw /* try again */
561 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
562 jne mu_get_hw /* branch on failure to retry */
564 cmpw $0,M_WAITERS /* are there any waiters? */
565 jne mu_wakeup /* yes, more work to do */
570 movl $0,M_THREAD /* disown thread */
574 movl %ecx,M_LOCKED /* unlock the mutex */
578 popf /* restore interrupt state */
584 pushl %edx /* push mutex address */
585 call EXT(lck_mtx_unlock_wakeup)/* yes, wake a thread */
587 movl B_ARG0,%edx /* restore lock pointer */
595 * These are variants of mutex_lock(), mutex_try() and mutex_unlock() without
596 * DEBUG checks (which require fields not present in lck_mtx_t's).
598 NONLEAF_ENTRY(lck_mtx_lock)
600 movl B_ARG0,%edx /* fetch lock pointer */
601 cmpl $(MUTEX_IND),M_ITAG /* is this indirect? */
602 cmove M_PTR,%edx /* yes - take indirection */
604 CHECK_NO_SIMPLELOCKS()
605 CHECK_PREEMPTION_LEVEL()
607 pushf /* save interrupt state */
608 cli /* disable interrupts */
614 movl M_ILK,%eax /* read interlock */
615 testl %eax,%eax /* unlocked? */
616 je 1f /* yes - attempt to lock it */
617 PAUSE /* no - pause */
618 jmp lml_get_hw /* try again */
620 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
621 jne lml_get_hw /* branch on failure to retry */
623 movl M_LOCKED,%ecx /* get lock owner */
624 testl %ecx,%ecx /* is the mutex locked? */
625 jne lml_fail /* yes, we lose */
626 movl %gs:CPU_ACTIVE_THREAD,%ecx
629 pushl %edx /* save mutex address */
631 call EXT(lck_mtx_lock_acquire)
633 popl %edx /* restore mutex address */
638 popf /* restore interrupt state */
643 CHECK_MYLOCK(M_THREAD)
644 pushl %edx /* save mutex address */
646 pushl %edx /* push mutex address */
647 call EXT(lck_mtx_lock_wait) /* wait for the lock */
649 popl %edx /* restore mutex address */
650 jmp lml_retry /* and try again */
652 NONLEAF_ENTRY(lck_mtx_try_lock)
654 movl B_ARG0,%edx /* fetch lock pointer */
655 cmpl $(MUTEX_IND),M_ITAG /* is this indirect? */
656 cmove M_PTR,%edx /* yes - take indirection */
658 CHECK_NO_SIMPLELOCKS()
659 CHECK_PREEMPTION_LEVEL()
663 pushf /* save interrupt state */
664 cli /* disable interrupts */
667 movl M_ILK,%eax /* read interlock */
668 testl %eax,%eax /* unlocked? */
669 je 1f /* yes - attempt to lock it */
670 PAUSE /* no - pause */
671 jmp lmt_get_hw /* try again */
673 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
674 jne lmt_get_hw /* branch on failure to retry */
676 movl M_LOCKED,%ecx /* get lock owner */
677 testl %ecx,%ecx /* is the mutex locked? */
678 jne lmt_fail /* yes, we lose */
679 movl %gs:CPU_ACTIVE_THREAD,%ecx
682 pushl %edx /* save mutex address */
684 call EXT(lck_mtx_lock_acquire)
686 popl %edx /* restore mutex address */
691 popf /* restore interrupt state */
693 movl $1,%eax /* return success */
700 popf /* restore interrupt state */
702 xorl %eax,%eax /* return failure */
705 NONLEAF_ENTRY(lck_mtx_unlock)
707 movl B_ARG0,%edx /* fetch lock pointer */
708 cmpl $(MUTEX_IND),M_ITAG /* is this indirect? */
709 cmove M_PTR,%edx /* yes - take indirection */
713 pushf /* save interrupt state */
714 cli /* disable interrupts */
717 movl M_ILK,%eax /* read interlock */
718 testl %eax,%eax /* unlocked? */
719 je 1f /* yes - attempt to lock it */
720 PAUSE /* no - pause */
721 jmp lmu_get_hw /* try again */
723 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
724 jne lmu_get_hw /* branch on failure to retry */
726 cmpw $0,M_WAITERS /* are there any waiters? */
727 jne lmu_wakeup /* yes, more work to do */
731 movl %ecx,M_LOCKED /* unlock the mutex */
735 popf /* restore interrupt state */
740 pushl %edx /* save mutex address */
742 pushl %edx /* push mutex address */
743 call EXT(lck_mtx_unlock_wakeup)/* yes, wake a thread */
745 popl %edx /* restore mutex pointer */
748 LEAF_ENTRY(lck_mtx_ilk_unlock)
749 movl L_ARG0,%edx /* no indirection here */
756 LEAF_ENTRY(_disable_preemption)
762 LEAF_ENTRY(_enable_preemption)
765 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
767 pushl %gs:CPU_PREEMPTION_LEVEL
772 2: String "_enable_preemption: preemption_level(%d) < 0!"
775 #endif /* MACH_ASSERT */
780 LEAF_ENTRY(_enable_preemption_no_check)
783 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
789 2: String "_enable_preemption_no_check: preemption_level <= 0!"
792 #endif /* MACH_ASSERT */
793 _ENABLE_PREEMPTION_NO_CHECK
798 LEAF_ENTRY(_mp_disable_preemption)
804 LEAF_ENTRY(_mp_enable_preemption)
807 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
809 pushl %gs:CPU_PREEMPTION_LEVEL
814 2: String "_mp_enable_preemption: preemption_level (%d) <= 0!"
817 #endif /* MACH_ASSERT */
822 LEAF_ENTRY(_mp_enable_preemption_no_check)
825 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
831 2: String "_mp_enable_preemption_no_check: preemption_level <= 0!"
834 #endif /* MACH_ASSERT */
835 _ENABLE_PREEMPTION_NO_CHECK
840 LEAF_ENTRY(i_bit_set)
847 LEAF_ENTRY(i_bit_clear)
863 LEAF_ENTRY(bit_lock_try)
869 LEAF_RET /* %eax better not be null ! */
874 LEAF_ENTRY(bit_unlock)