2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1989 Carnegie-Mellon University
28 * All rights reserved. The CMU software License Agreement specifies
29 * the terms and conditions for use and redistribution.
33 #include <platforms.h>
34 #include <mach_ldebug.h>
39 #define PAUSE rep; nop
42 * When performance isn't the only concern, it's
43 * nice to build stack frames...
45 #define BUILD_STACK_FRAMES (GPROF || \
46 ((MACH_LDEBUG || ETAP_LOCK_TRACE) && MACH_KDB))
48 #if BUILD_STACK_FRAMES
50 /* STack-frame-relative: */
55 #define LEAF_ENTRY(name) \
60 #define LEAF_ENTRY2(n1,n2) \
70 #else /* BUILD_STACK_FRAMES */
72 /* Stack-pointer-relative: */
77 #define LEAF_ENTRY(name) \
80 #define LEAF_ENTRY2(n1,n2) \
87 #endif /* BUILD_STACK_FRAMES */
90 /* Non-leaf routines always have a stack frame: */
92 #define NONLEAF_ENTRY(name) \
97 #define NONLEAF_ENTRY2(n1,n2) \
103 #define NONLEAF_RET \
109 #define M_LOCKED MUTEX_LOCKED(%edx)
110 #define M_WAITERS MUTEX_WAITERS(%edx)
111 #define M_PROMOTED_PRI MUTEX_PROMOTED_PRI(%edx)
112 #define M_ITAG MUTEX_ITAG(%edx)
113 #define M_PTR MUTEX_PTR(%edx)
115 #define M_TYPE MUTEX_TYPE(%edx)
116 #define M_PC MUTEX_PC(%edx)
117 #define M_THREAD MUTEX_THREAD(%edx)
118 #endif /* MACH_LDEBUG */
121 #define CX(addr,reg) addr(,reg,4)
125 * Routines for general lock debugging.
129 * Checks for expected lock types and calls "panic" on
130 * mismatch. Detects calls to Mutex functions with
131 * type simplelock and vice versa.
133 #define CHECK_MUTEX_TYPE() \
134 cmpl $ MUTEX_TAG,M_TYPE ; \
140 2: String "not a mutex!" ; \
145 * If one or more simplelocks are currently held by a thread,
146 * an attempt to acquire a mutex will cause this check to fail
147 * (since a mutex lock may context switch, holding a simplelock
148 * is not a good thing).
151 #define CHECK_PREEMPTION_LEVEL() \
152 cmpl $0,%gs:CPU_PREEMPTION_LEVEL ; \
158 2: String "preemption_level != 0!" ; \
162 #define CHECK_PREEMPTION_LEVEL()
165 #define CHECK_NO_SIMPLELOCKS() \
166 cmpl $0,%gs:CPU_SIMPLE_LOCK_COUNT ; \
172 2: String "simple_locks_held!" ; \
177 * Verifies return to the correct thread in "unlock" situations.
179 #define CHECK_THREAD(thd) \
180 movl %gs:CPU_ACTIVE_THREAD,%ecx ; \
189 2: String "wrong thread!" ; \
193 #define CHECK_MYLOCK(thd) \
194 movl %gs:CPU_ACTIVE_THREAD,%ecx ; \
203 2: String "mylock attempt!" ; \
207 #define METER_SIMPLE_LOCK_LOCK(reg) \
209 call EXT(meter_simple_lock) ; \
212 #define METER_SIMPLE_LOCK_UNLOCK(reg) \
214 call EXT(meter_simple_unlock) ; \
217 #else /* MACH_LDEBUG */
218 #define CHECK_MUTEX_TYPE()
219 #define CHECK_SIMPLE_LOCK_TYPE
220 #define CHECK_THREAD(thd)
221 #define CHECK_PREEMPTION_LEVEL()
222 #define CHECK_NO_SIMPLELOCKS()
223 #define CHECK_MYLOCK(thd)
224 #define METER_SIMPLE_LOCK_LOCK(reg)
225 #define METER_SIMPLE_LOCK_UNLOCK(reg)
226 #endif /* MACH_LDEBUG */
230 * void hw_lock_init(hw_lock_t)
232 * Initialize a hardware lock.
234 LEAF_ENTRY(hw_lock_init)
235 movl L_ARG0,%edx /* fetch lock pointer */
236 movl $0,0(%edx) /* clear the lock */
240 * void hw_lock_lock(hw_lock_t)
242 * Acquire lock, spinning until it becomes available.
243 * MACH_RT: also return with preemption disabled.
245 LEAF_ENTRY(hw_lock_lock)
246 movl L_ARG0,%edx /* fetch lock pointer */
248 movl %gs:CPU_ACTIVE_THREAD,%ecx
252 testl %eax,%eax /* lock locked? */
253 jne 3f /* branch if so */
254 lock; cmpxchgl %ecx,0(%edx) /* try to acquire the HW lock */
256 movl $1,%eax /* In case this was a timeout call */
257 LEAF_RET /* if yes, then nothing left to do */
259 PAUSE /* pause for hyper-threading */
260 jmp 1b /* try again */
263 * unsigned int hw_lock_to(hw_lock_t, unsigned int)
265 * Acquire lock, spinning until it becomes available or timeout.
266 * MACH_RT: also return with preemption disabled.
268 LEAF_ENTRY(hw_lock_to)
270 movl L_ARG0,%edx /* fetch lock pointer */
271 movl %gs:CPU_ACTIVE_THREAD,%ecx
273 * Attempt to grab the lock immediately
274 * - fastpath without timeout nonsense.
278 testl %eax,%eax /* lock locked? */
279 jne 2f /* branch if so */
280 lock; cmpxchgl %ecx,0(%edx) /* try to acquire the HW lock */
281 jne 2f /* branch on failure */
286 #define INNER_LOOP_COUNT 1000
288 * Failed to get the lock so set the timeout
289 * and then spin re-checking the lock but pausing
290 * every so many (INNER_LOOP_COUNT) spins to check for timeout.
292 movl L_ARG1,%ecx /* fetch timeout */
297 rdtsc /* read cyclecount into %edx:%eax */
298 addl %ecx,%eax /* fetch and timeout */
299 adcl $0,%edx /* add carry */
301 mov %eax,%ebx /* %ecx:%ebx is the timeout expiry */
304 * The inner-loop spin to look for the lock being freed.
306 mov $(INNER_LOOP_COUNT),%edx
308 PAUSE /* pause for hyper-threading */
309 movl 0(%edi),%eax /* spin checking lock value in cache */
311 je 6f /* zero => unlocked, try to grab it */
312 decl %edx /* decrement inner loop count */
313 jnz 5b /* time to check for timeout? */
316 * Here after spinning INNER_LOOP_COUNT times, check for timeout
318 rdtsc /* cyclecount into %edx:%eax */
319 cmpl %ecx,%edx /* compare high-order 32-bits */
320 jb 4b /* continue spinning if less, or */
321 cmpl %ebx,%eax /* compare low-order 32-bits */
322 jb 4b /* continue if less, else bail */
323 xor %eax,%eax /* with 0 return value */
330 * Here to try to grab the lock that now appears to be free
333 movl %gs:CPU_ACTIVE_THREAD,%edx
334 lock; cmpxchgl %edx,0(%edi) /* try to acquire the HW lock */
335 jne 4b /* no - spin again */
336 movl $1,%eax /* yes */
342 * void hw_lock_unlock(hw_lock_t)
344 * Unconditionally release lock.
345 * MACH_RT: release preemption level.
347 LEAF_ENTRY(hw_lock_unlock)
348 movl L_ARG0,%edx /* fetch lock pointer */
349 movl $0,0(%edx) /* clear the lock */
354 * unsigned int hw_lock_try(hw_lock_t)
355 * MACH_RT: returns with preemption disabled on success.
357 LEAF_ENTRY(hw_lock_try)
358 movl L_ARG0,%edx /* fetch lock pointer */
360 movl %gs:CPU_ACTIVE_THREAD,%ecx
365 lock; cmpxchgl %ecx,0(%edx) /* try to acquire the HW lock */
368 movl $1,%eax /* success */
372 ENABLE_PREEMPTION /* failure: release preemption... */
373 xorl %eax,%eax /* ...and return failure */
377 * unsigned int hw_lock_held(hw_lock_t)
378 * MACH_RT: doesn't change preemption state.
379 * N.B. Racy, of course.
381 LEAF_ENTRY(hw_lock_held)
382 movl L_ARG0,%edx /* fetch lock pointer */
384 movl 0(%edx),%eax /* check lock value */
387 cmovne %ecx,%eax /* 0 => unlocked, 1 => locked */
390 LEAF_ENTRY(mutex_init)
391 movl L_ARG0,%edx /* fetch lock pointer */
393 movl %eax,M_ILK /* clear interlock */
394 movl %eax,M_LOCKED /* clear locked flag */
395 movw %ax,M_WAITERS /* init waiter count */
396 movw %ax,M_PROMOTED_PRI
399 movl $ MUTEX_TAG,M_TYPE /* set lock type */
400 movl %eax,M_PC /* init caller pc */
401 movl %eax,M_THREAD /* and owning thread */
406 NONLEAF_ENTRY2(mutex_lock,_mutex_lock)
408 movl B_ARG0,%edx /* fetch lock pointer */
411 CHECK_NO_SIMPLELOCKS()
412 CHECK_PREEMPTION_LEVEL()
414 pushf /* save interrupt state */
415 cli /* disable interrupts */
417 movl %gs:CPU_ACTIVE_THREAD,%ecx
420 movl M_ILK,%eax /* read interlock */
421 testl %eax,%eax /* unlocked? */
422 jne Lml_ilk_fail /* no - take the slow path */
424 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
425 jne Lml_get_hw /* branch on failure to retry */
427 movl M_LOCKED,%ecx /* get lock owner */
428 testl %ecx,%ecx /* is the mutex locked? */
429 jne Lml_fail /* yes, we lose */
431 movl %gs:CPU_ACTIVE_THREAD,%ecx
440 cmpw $0,M_WAITERS /* are there any waiters? */
441 jne Lml_waiters /* yes, more work to do */
446 popf /* restore interrupt state */
451 pushl %edx /* save mutex address */
453 call EXT(lck_mtx_lock_acquire)
455 popl %edx /* restore mutex address */
460 * Slow path: call out to do the spinning.
462 pushl %edx /* lock address */
463 call EXT(lck_mtx_interlock_spin)
464 popl %edx /* lock pointer */
465 jmp Lml_retry /* try again */
469 n Check if the owner is on another processor and therefore
470 * we should try to spin before blocking.
472 testl $(OnProc),ACT_SPF(%ecx)
476 * Here if owner is on another processor:
477 * - release the interlock
478 * - spin on the holder until release or timeout
479 * - in either case re-acquire the interlock
480 * - if released, acquire it
481 * - otherwise drop thru to block.
484 movl %eax,M_ILK /* zero interlock */
486 pushf /* restore interrupt state */
488 push %edx /* lock address */
489 call EXT(lck_mtx_lock_spin) /* call out to do spinning */
491 movl B_ARG0,%edx /* refetch mutex address */
493 /* Re-acquire interlock */
494 cli /* disable interrupts */
496 movl %gs:CPU_ACTIVE_THREAD,%ecx
499 movl M_ILK,%eax /* read interlock */
500 testl %eax,%eax /* unlocked? */
501 jne Lml_ilk_refail /* no - slow path */
503 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
504 jne Lml_reget_hw /* branch on failure to retry */
506 movl M_LOCKED,%ecx /* get lock owner */
507 testl %ecx,%ecx /* is the mutex free? */
508 je Lml_acquire /* yes, acquire */
511 CHECK_MYLOCK(M_THREAD)
513 pushl %edx /* push mutex address */
514 call EXT(lck_mtx_lock_wait) /* wait for the lock */
516 movl B_ARG0,%edx /* refetch mutex address */
517 cli /* ensure interrupts disabled */
518 jmp Lml_retry /* and try again */
522 * Slow path: call out to do the spinning.
524 pushl %edx /* lock address */
525 call EXT(lck_mtx_interlock_spin)
526 popl %edx /* lock pointer */
527 jmp Lml_reget_retry /* try again */
529 NONLEAF_ENTRY2(mutex_try,_mutex_try)
531 movl B_ARG0,%edx /* fetch lock pointer */
534 CHECK_NO_SIMPLELOCKS()
536 pushf /* save interrupt state */
537 cli /* disable interrupts */
539 movl %gs:CPU_ACTIVE_THREAD,%ecx
542 movl M_ILK,%eax /* read interlock */
543 testl %eax,%eax /* unlocked? */
544 jne Lmt_ilk_fail /* no - slow path */
546 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
547 jne Lmt_get_hw /* branch on failure to retry */
549 movl M_LOCKED,%ecx /* get lock owner */
550 testl %ecx,%ecx /* is the mutex locked? */
551 jne Lmt_fail /* yes, we lose */
552 movl %gs:CPU_ACTIVE_THREAD,%ecx
561 cmpl $0,M_WAITERS /* are there any waiters? */
562 jne Lmt_waiters /* yes, more work to do */
566 popf /* restore interrupt state */
573 pushl %edx /* save mutex address */
575 call EXT(lck_mtx_lock_acquire)
577 popl %edx /* restore mutex address */
582 * Slow path: call out to do the spinning.
584 pushl %edx /* lock address */
585 call EXT(lck_mtx_interlock_spin)
586 popl %edx /* lock pointer */
587 jmp Lmt_retry /* try again */
593 popf /* restore interrupt state */
599 NONLEAF_ENTRY(mutex_unlock)
600 movl B_ARG0,%edx /* fetch lock pointer */
603 CHECK_THREAD(M_THREAD)
605 pushf /* save interrupt state */
606 cli /* disable interrupts */
608 movl %gs:CPU_ACTIVE_THREAD,%ecx
611 movl M_ILK,%eax /* read interlock */
612 testl %eax,%eax /* unlocked? */
613 jne Lmu_ilk_fail /* no - slow path */
615 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
616 jne Lmu_get_hw /* branch on failure to retry */
618 cmpw $0,M_WAITERS /* are there any waiters? */
619 jne Lmu_wakeup /* yes, more work to do */
624 movl $0,M_THREAD /* disown thread */
628 movl %ecx,M_LOCKED /* unlock the mutex */
632 popf /* restore interrupt state */
638 * Slow path: call out to do the spinning.
640 pushl %edx /* lock address */
641 call EXT(lck_mtx_interlock_spin)
642 popl %edx /* lock pointer */
643 jmp Lmu_retry /* try again */
647 pushl %edx /* push mutex address */
648 call EXT(lck_mtx_unlock_wakeup)/* yes, wake a thread */
650 movl B_ARG0,%edx /* restore lock pointer */
658 * These are variants of mutex_lock(), mutex_try() and mutex_unlock() without
659 * DEBUG checks (which require fields not present in lck_mtx_t's).
661 NONLEAF_ENTRY(lck_mtx_lock)
663 movl B_ARG0,%edx /* fetch lock pointer */
664 cmpl $(MUTEX_IND),M_ITAG /* is this indirect? */
665 cmove M_PTR,%edx /* yes - take indirection */
667 CHECK_NO_SIMPLELOCKS()
668 CHECK_PREEMPTION_LEVEL()
670 pushf /* save interrupt state */
671 cli /* disable interrupts */
673 movl %gs:CPU_ACTIVE_THREAD,%ecx
676 movl M_ILK,%eax /* read interlock */
677 testl %eax,%eax /* unlocked? */
678 jne Llml_ilk_fail /* no - slow path */
680 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
681 jne Llml_get_hw /* branch on failure to retry */
683 movl M_LOCKED,%ecx /* get lock owner */
684 testl %ecx,%ecx /* is the mutex locked? */
685 jne Llml_fail /* yes, we lose */
687 movl %gs:CPU_ACTIVE_THREAD,%ecx
690 cmpl $0,M_WAITERS /* are there any waiters? */
691 jne Llml_waiters /* yes, more work to do */
696 popf /* restore interrupt state */
701 pushl %edx /* save mutex address */
703 call EXT(lck_mtx_lock_acquire)
705 popl %edx /* restore mutex address */
710 * Slow path: call out to do the spinning.
712 pushl %edx /* lock address */
713 call EXT(lck_mtx_interlock_spin)
714 popl %edx /* lock pointer */
715 jmp Llml_retry /* try again */
719 * Check if the owner is on another processor and therefore
720 * we should try to spin before blocking.
722 testl $(OnProc),ACT_SPF(%ecx)
726 * Here if owner is on another processor:
727 * - release the interlock
728 * - spin on the holder until release or timeout
729 * - in either case re-acquire the interlock
730 * - if released, acquire it
731 * - otherwise drop thru to block.
734 movl %eax,M_ILK /* zero interlock */
736 pushf /* restore interrupt state */
738 pushl %edx /* save mutex address */
740 call EXT(lck_mtx_lock_spin)
742 popl %edx /* restore mutex address */
744 /* Re-acquire interlock */
745 cli /* disable interrupts */
747 movl %gs:CPU_ACTIVE_THREAD,%ecx
750 movl M_ILK,%eax /* read interlock */
751 testl %eax,%eax /* unlocked? */
752 jne Llml_ilk_refail /* no - slow path */
754 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
755 jne Llml_reget_hw /* branch on failure to retry */
757 movl M_LOCKED,%ecx /* get lock owner */
758 testl %ecx,%ecx /* is the mutex free? */
759 je Llml_acquire /* yes, acquire */
762 CHECK_MYLOCK(M_THREAD)
763 pushl %edx /* save mutex address */
765 pushl %edx /* push mutex address */
766 call EXT(lck_mtx_lock_wait) /* wait for the lock */
768 popl %edx /* restore mutex address */
769 cli /* ensure interrupts disabled */
770 jmp Llml_retry /* and try again */
774 * Slow path: call out to do the spinning.
776 pushl %edx /* lock address */
777 call EXT(lck_mtx_interlock_spin)
778 popl %edx /* lock pointer */
779 jmp Llml_reget_retry /* try again */
781 NONLEAF_ENTRY(lck_mtx_try_lock)
783 movl B_ARG0,%edx /* fetch lock pointer */
784 cmpl $(MUTEX_IND),M_ITAG /* is this indirect? */
785 cmove M_PTR,%edx /* yes - take indirection */
787 CHECK_NO_SIMPLELOCKS()
788 CHECK_PREEMPTION_LEVEL()
790 pushf /* save interrupt state */
791 cli /* disable interrupts */
793 movl %gs:CPU_ACTIVE_THREAD,%ecx
796 movl M_ILK,%eax /* read interlock */
797 testl %eax,%eax /* unlocked? */
798 jne Llmt_ilk_fail /* no - slow path */
800 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
801 jne Llmt_get_hw /* branch on failure to retry */
803 movl M_LOCKED,%ecx /* get lock owner */
804 testl %ecx,%ecx /* is the mutex locked? */
805 jne Llmt_fail /* yes, we lose */
806 movl %gs:CPU_ACTIVE_THREAD,%ecx
809 cmpl $0,M_WAITERS /* are there any waiters? */
810 jne Llmt_waiters /* yes, more work to do */
815 popf /* restore interrupt state */
817 movl $1,%eax /* return success */
821 pushl %edx /* save mutex address */
823 call EXT(lck_mtx_lock_acquire)
825 popl %edx /* restore mutex address */
830 * Slow path: call out to do the spinning.
832 pushl %edx /* lock address */
833 call EXT(lck_mtx_interlock_spin)
834 popl %edx /* lock pointer */
835 jmp Llmt_retry /* try again */
841 popf /* restore interrupt state */
843 xorl %eax,%eax /* return failure */
846 NONLEAF_ENTRY(lck_mtx_unlock)
848 movl B_ARG0,%edx /* fetch lock pointer */
849 cmpl $(MUTEX_IND),M_ITAG /* is this indirect? */
850 cmove M_PTR,%edx /* yes - take indirection */
852 pushf /* save interrupt state */
853 cli /* disable interrupts */
855 movl %gs:CPU_ACTIVE_THREAD,%ecx
858 movl M_ILK,%eax /* read interlock */
859 testl %eax,%eax /* unlocked? */
860 jne Llmu_ilk_fail /* no - slow path */
862 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
863 jne Llmu_get_hw /* branch on failure to retry */
865 cmpw $0,M_WAITERS /* are there any waiters? */
866 jne Llmu_wakeup /* yes, more work to do */
870 movl %ecx,M_LOCKED /* unlock the mutex */
874 popf /* restore interrupt state */
880 * Slow path: call out to do the spinning.
882 pushl %edx /* lock address */
883 call EXT(lck_mtx_interlock_spin)
884 popl %edx /* lock pointer */
885 jmp Llmu_retry /* try again */
888 pushl %edx /* save mutex address */
890 pushl %edx /* push mutex address */
891 call EXT(lck_mtx_unlock_wakeup)/* yes, wake a thread */
893 popl %edx /* restore mutex pointer */
896 LEAF_ENTRY(lck_mtx_ilk_unlock)
897 movl L_ARG0,%edx /* no indirection here */
904 LEAF_ENTRY(_disable_preemption)
910 LEAF_ENTRY(_enable_preemption)
913 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
915 pushl %gs:CPU_PREEMPTION_LEVEL
920 2: String "_enable_preemption: preemption_level(%d) < 0!"
923 #endif /* MACH_ASSERT */
928 LEAF_ENTRY(_enable_preemption_no_check)
931 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
937 2: String "_enable_preemption_no_check: preemption_level <= 0!"
940 #endif /* MACH_ASSERT */
941 _ENABLE_PREEMPTION_NO_CHECK
946 LEAF_ENTRY(_mp_disable_preemption)
952 LEAF_ENTRY(_mp_enable_preemption)
955 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
957 pushl %gs:CPU_PREEMPTION_LEVEL
962 2: String "_mp_enable_preemption: preemption_level (%d) <= 0!"
965 #endif /* MACH_ASSERT */
970 LEAF_ENTRY(_mp_enable_preemption_no_check)
973 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
979 2: String "_mp_enable_preemption_no_check: preemption_level <= 0!"
982 #endif /* MACH_ASSERT */
983 _ENABLE_PREEMPTION_NO_CHECK
988 LEAF_ENTRY(i_bit_set)
995 LEAF_ENTRY(i_bit_clear)
1002 LEAF_ENTRY(bit_lock)
1011 LEAF_ENTRY(bit_lock_try)
1017 LEAF_RET /* %eax better not be null ! */
1022 LEAF_ENTRY(bit_unlock)