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.
127 #define S_TYPE SLOCK_TYPE(%edx)
128 #define S_PC SLOCK_PC(%edx)
129 #define S_THREAD SLOCK_THREAD(%edx)
130 #define S_DURATIONH SLOCK_DURATIONH(%edx)
131 #define S_DURATIONL SLOCK_DURATIONL(%edx)
134 * Checks for expected lock types and calls "panic" on
135 * mismatch. Detects calls to Mutex functions with
136 * type simplelock and vice versa.
138 #define CHECK_MUTEX_TYPE() \
139 cmpl $ MUTEX_TAG,M_TYPE ; \
145 2: String "not a mutex!" ; \
149 #define CHECK_SIMPLE_LOCK_TYPE() \
150 cmpl $ USLOCK_TAG,S_TYPE ; \
156 2: String "not a simple lock!" ; \
161 * If one or more simplelocks are currently held by a thread,
162 * an attempt to acquire a mutex will cause this check to fail
163 * (since a mutex lock may context switch, holding a simplelock
164 * is not a good thing).
167 #define CHECK_PREEMPTION_LEVEL() \
168 cmpl $0,%gs:CPU_PREEMPTION_LEVEL ; \
174 2: String "preemption_level != 0!" ; \
178 #define CHECK_PREEMPTION_LEVEL()
181 #define CHECK_NO_SIMPLELOCKS() \
182 cmpl $0,%gs:CPU_SIMPLE_LOCK_COUNT ; \
188 2: String "simple_locks_held!" ; \
193 * Verifies return to the correct thread in "unlock" situations.
195 #define CHECK_THREAD(thd) \
196 movl %gs:CPU_ACTIVE_THREAD,%ecx ; \
205 2: String "wrong thread!" ; \
209 #define CHECK_MYLOCK(thd) \
210 movl %gs:CPU_ACTIVE_THREAD,%ecx ; \
219 2: String "mylock attempt!" ; \
223 #define METER_SIMPLE_LOCK_LOCK(reg) \
225 call EXT(meter_simple_lock) ; \
228 #define METER_SIMPLE_LOCK_UNLOCK(reg) \
230 call EXT(meter_simple_unlock) ; \
233 #else /* MACH_LDEBUG */
234 #define CHECK_MUTEX_TYPE()
235 #define CHECK_SIMPLE_LOCK_TYPE
236 #define CHECK_THREAD(thd)
237 #define CHECK_PREEMPTION_LEVEL()
238 #define CHECK_NO_SIMPLELOCKS()
239 #define CHECK_MYLOCK(thd)
240 #define METER_SIMPLE_LOCK_LOCK(reg)
241 #define METER_SIMPLE_LOCK_UNLOCK(reg)
242 #endif /* MACH_LDEBUG */
246 * void hw_lock_init(hw_lock_t)
248 * Initialize a hardware lock.
250 LEAF_ENTRY(hw_lock_init)
251 movl L_ARG0,%edx /* fetch lock pointer */
252 movl $0,0(%edx) /* clear the lock */
256 * void hw_lock_lock(hw_lock_t)
258 * Acquire lock, spinning until it becomes available.
259 * MACH_RT: also return with preemption disabled.
261 LEAF_ENTRY(hw_lock_lock)
262 movl L_ARG0,%edx /* fetch lock pointer */
265 1: DISABLE_PREEMPTION
267 testl %eax,%eax /* lock locked? */
268 jne 3f /* branch if so */
269 lock; cmpxchgl %ecx,0(%edx) /* try to acquire the HW lock */
271 movl $1,%eax /* In case this was a timeout call */
272 LEAF_RET /* if yes, then nothing left to do */
274 3: ENABLE_PREEMPTION /* no reason we can't be preemptable */
275 PAUSE /* pause for hyper-threading */
276 jmp 1b /* try again */
279 * unsigned int hw_lock_to(hw_lock_t, unsigned int)
281 * Acquire lock, spinning until it becomes available or timeout.
282 * MACH_RT: also return with preemption disabled.
284 LEAF_ENTRY(hw_lock_to)
286 movl L_ARG0,%edx /* fetch lock pointer */
289 * Attempt to grab the lock immediately
290 * - fastpath without timeout nonsense.
294 testl %eax,%eax /* lock locked? */
295 jne 2f /* branch if so */
296 lock; cmpxchgl %ecx,0(%edx) /* try to acquire the HW lock */
297 jne 2f /* branch on failure */
302 #define INNER_LOOP_COUNT 1000
304 * Failed to get the lock so set the timeout
305 * and then spin re-checking the lock but pausing
306 * every so many (INNER_LOOP_COUNT) spins to check for timeout.
308 movl L_ARG1,%ecx /* fetch timeout */
313 rdtsc /* read cyclecount into %edx:%eax */
314 addl %ecx,%eax /* fetch and timeout */
315 adcl $0,%edx /* add carry */
317 mov %eax,%ebx /* %ecx:%ebx is the timeout expiry */
319 ENABLE_PREEMPTION /* no reason not to be preempted now */
322 * The inner-loop spin to look for the lock being freed.
324 mov $(INNER_LOOP_COUNT),%edx
326 PAUSE /* pause for hyper-threading */
327 movl 0(%edi),%eax /* spin checking lock value in cache */
329 je 6f /* zero => unlocked, try to grab it */
330 decl %edx /* decrement inner loop count */
331 jnz 5b /* time to check for timeout? */
334 * Here after spinning INNER_LOOP_COUNT times, check for timeout
336 rdtsc /* cyclecount into %edx:%eax */
337 cmpl %ecx,%edx /* compare high-order 32-bits */
338 jb 4b /* continue spinning if less, or */
339 cmpl %ebx,%eax /* compare low-order 32-bits */
340 jb 5b /* continue if less, else bail */
341 xor %eax,%eax /* with 0 return value */
348 * Here to try to grab the lock that now appears to be free
351 movl 8+L_PC,%edx /* calling pc (8+ for pushed regs) */
353 lock; cmpxchgl %edx,0(%edi) /* try to acquire the HW lock */
354 jne 3b /* no - spin again */
355 movl $1,%eax /* yes */
361 * void hw_lock_unlock(hw_lock_t)
363 * Unconditionally release lock.
364 * MACH_RT: release preemption level.
366 LEAF_ENTRY(hw_lock_unlock)
367 movl L_ARG0,%edx /* fetch lock pointer */
368 movl $0,0(%edx) /* clear the lock */
373 * unsigned int hw_lock_try(hw_lock_t)
374 * MACH_RT: returns with preemption disabled on success.
376 LEAF_ENTRY(hw_lock_try)
377 movl L_ARG0,%edx /* fetch lock pointer */
384 lock; cmpxchgl %ecx,0(%edx) /* try to acquire the HW lock */
387 movl $1,%eax /* success */
390 1: ENABLE_PREEMPTION /* failure: release preemption... */
391 xorl %eax,%eax /* ...and return failure */
395 * unsigned int hw_lock_held(hw_lock_t)
396 * MACH_RT: doesn't change preemption state.
397 * N.B. Racy, of course.
399 LEAF_ENTRY(hw_lock_held)
400 movl L_ARG0,%edx /* fetch lock pointer */
402 movl 0(%edx),%eax /* check lock value */
405 cmovne %ecx,%eax /* 0 => unlocked, 1 => locked */
408 LEAF_ENTRY(mutex_init)
409 movl L_ARG0,%edx /* fetch lock pointer */
411 movl %eax,M_ILK /* clear interlock */
412 movl %eax,M_LOCKED /* clear locked flag */
413 movw %ax,M_WAITERS /* init waiter count */
414 movw %ax,M_PROMOTED_PRI
417 movl $ MUTEX_TAG,M_TYPE /* set lock type */
418 movl %eax,M_PC /* init caller pc */
419 movl %eax,M_THREAD /* and owning thread */
424 NONLEAF_ENTRY2(mutex_lock,_mutex_lock)
426 movl B_ARG0,%edx /* fetch lock pointer */
429 CHECK_NO_SIMPLELOCKS()
430 CHECK_PREEMPTION_LEVEL()
432 pushf /* save interrupt state */
433 cli /* disable interrupts */
439 movl M_ILK,%eax /* read interlock */
440 testl %eax,%eax /* unlocked? */
441 je 1f /* yes - attempt to lock it */
442 PAUSE /* no - pause */
443 jmp ml_get_hw /* try again */
445 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
446 jne ml_get_hw /* branch on failure to retry */
448 movl M_LOCKED,%ecx /* get lock owner */
449 testl %ecx,%ecx /* is the mutex locked? */
450 jne ml_fail /* yes, we lose */
451 movl %gs:CPU_ACTIVE_THREAD,%ecx
460 pushl %edx /* save mutex address */
462 call EXT(lck_mtx_lock_acquire)
464 popl %edx /* restore mutex address */
469 popf /* restore interrupt state */
475 CHECK_MYLOCK(M_THREAD)
477 pushl %edx /* push mutex address */
478 call EXT(lck_mtx_lock_wait) /* wait for the lock */
480 movl B_ARG0,%edx /* refetch mutex address */
481 jmp ml_retry /* and try again */
483 NONLEAF_ENTRY2(mutex_try,_mutex_try)
485 movl B_ARG0,%edx /* fetch lock pointer */
488 CHECK_NO_SIMPLELOCKS()
492 pushf /* save interrupt state */
493 cli /* disable interrupts */
496 movl M_ILK,%eax /* read interlock */
497 testl %eax,%eax /* unlocked? */
498 je 1f /* yes - attempt to lock it */
499 PAUSE /* no - pause */
500 jmp mt_get_hw /* try again */
502 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
503 jne mt_get_hw /* branch on failure to retry */
505 movl M_LOCKED,%ecx /* get lock owner */
506 testl %ecx,%ecx /* is the mutex locked? */
507 jne mt_fail /* yes, we lose */
508 movl %gs:CPU_ACTIVE_THREAD,%ecx
517 pushl %edx /* save mutex address */
519 call EXT(lck_mtx_lock_acquire)
521 popl %edx /* restore mutex address */
526 popf /* restore interrupt state */
536 popf /* restore interrupt state */
542 NONLEAF_ENTRY(mutex_unlock)
543 movl B_ARG0,%edx /* fetch lock pointer */
546 CHECK_THREAD(M_THREAD)
550 pushf /* save interrupt state */
551 cli /* disable interrupts */
554 movl M_ILK,%eax /* read interlock */
555 testl %eax,%eax /* unlocked? */
556 je 1f /* yes - attempt to lock it */
557 PAUSE /* no - pause */
558 jmp mu_get_hw /* try again */
560 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
561 jne mu_get_hw /* branch on failure to retry */
563 cmpw $0,M_WAITERS /* are there any waiters? */
564 jne mu_wakeup /* yes, more work to do */
569 movl $0,M_THREAD /* disown thread */
573 movl %ecx,M_LOCKED /* unlock the mutex */
577 popf /* restore interrupt state */
583 pushl %edx /* push mutex address */
584 call EXT(lck_mtx_unlock_wakeup)/* yes, wake a thread */
586 movl B_ARG0,%edx /* restore lock pointer */
594 * These are variants of mutex_lock(), mutex_try() and mutex_unlock() without
595 * DEBUG checks (which require fields not present in lck_mtx_t's).
597 NONLEAF_ENTRY(lck_mtx_lock)
599 movl B_ARG0,%edx /* fetch lock pointer */
600 cmpl $(MUTEX_IND),M_ITAG /* is this indirect? */
601 cmove M_PTR,%edx /* yes - take indirection */
603 CHECK_NO_SIMPLELOCKS()
604 CHECK_PREEMPTION_LEVEL()
606 pushf /* save interrupt state */
607 cli /* disable interrupts */
613 movl M_ILK,%eax /* read interlock */
614 testl %eax,%eax /* unlocked? */
615 je 1f /* yes - attempt to lock it */
616 PAUSE /* no - pause */
617 jmp lml_get_hw /* try again */
619 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
620 jne lml_get_hw /* branch on failure to retry */
622 movl M_LOCKED,%ecx /* get lock owner */
623 testl %ecx,%ecx /* is the mutex locked? */
624 jne lml_fail /* yes, we lose */
625 movl %gs:CPU_ACTIVE_THREAD,%ecx
628 pushl %edx /* save mutex address */
630 call EXT(lck_mtx_lock_acquire)
632 popl %edx /* restore mutex address */
637 popf /* restore interrupt state */
642 CHECK_MYLOCK(M_THREAD)
643 pushl %edx /* save mutex address */
645 pushl %edx /* push mutex address */
646 call EXT(lck_mtx_lock_wait) /* wait for the lock */
648 popl %edx /* restore mutex address */
649 jmp lml_retry /* and try again */
651 NONLEAF_ENTRY(lck_mtx_try_lock)
653 movl B_ARG0,%edx /* fetch lock pointer */
654 cmpl $(MUTEX_IND),M_ITAG /* is this indirect? */
655 cmove M_PTR,%edx /* yes - take indirection */
657 CHECK_NO_SIMPLELOCKS()
658 CHECK_PREEMPTION_LEVEL()
662 pushf /* save interrupt state */
663 cli /* disable interrupts */
666 movl M_ILK,%eax /* read interlock */
667 testl %eax,%eax /* unlocked? */
668 je 1f /* yes - attempt to lock it */
669 PAUSE /* no - pause */
670 jmp lmt_get_hw /* try again */
672 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
673 jne lmt_get_hw /* branch on failure to retry */
675 movl M_LOCKED,%ecx /* get lock owner */
676 testl %ecx,%ecx /* is the mutex locked? */
677 jne lmt_fail /* yes, we lose */
678 movl %gs:CPU_ACTIVE_THREAD,%ecx
681 pushl %edx /* save mutex address */
683 call EXT(lck_mtx_lock_acquire)
685 popl %edx /* restore mutex address */
690 popf /* restore interrupt state */
692 movl $1,%eax /* return success */
699 popf /* restore interrupt state */
701 xorl %eax,%eax /* return failure */
704 NONLEAF_ENTRY(lck_mtx_unlock)
706 movl B_ARG0,%edx /* fetch lock pointer */
707 cmpl $(MUTEX_IND),M_ITAG /* is this indirect? */
708 cmove M_PTR,%edx /* yes - take indirection */
712 pushf /* save interrupt state */
713 cli /* disable interrupts */
716 movl M_ILK,%eax /* read interlock */
717 testl %eax,%eax /* unlocked? */
718 je 1f /* yes - attempt to lock it */
719 PAUSE /* no - pause */
720 jmp lmu_get_hw /* try again */
722 lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
723 jne lmu_get_hw /* branch on failure to retry */
725 cmpw $0,M_WAITERS /* are there any waiters? */
726 jne lmu_wakeup /* yes, more work to do */
730 movl %ecx,M_LOCKED /* unlock the mutex */
734 popf /* restore interrupt state */
739 pushl %edx /* save mutex address */
741 pushl %edx /* push mutex address */
742 call EXT(lck_mtx_unlock_wakeup)/* yes, wake a thread */
744 popl %edx /* restore mutex pointer */
747 LEAF_ENTRY(lck_mtx_ilk_unlock)
748 movl L_ARG0,%edx /* no indirection here */
755 LEAF_ENTRY(_disable_preemption)
761 LEAF_ENTRY(_enable_preemption)
764 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
766 pushl %gs:CPU_PREEMPTION_LEVEL
771 2: String "_enable_preemption: preemption_level(%d) < 0!"
774 #endif /* MACH_ASSERT */
779 LEAF_ENTRY(_enable_preemption_no_check)
782 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
788 2: String "_enable_preemption_no_check: preemption_level <= 0!"
791 #endif /* MACH_ASSERT */
792 _ENABLE_PREEMPTION_NO_CHECK
797 LEAF_ENTRY(_mp_disable_preemption)
803 LEAF_ENTRY(_mp_enable_preemption)
806 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
808 pushl %gs:CPU_PREEMPTION_LEVEL
813 2: String "_mp_enable_preemption: preemption_level (%d) <= 0!"
816 #endif /* MACH_ASSERT */
821 LEAF_ENTRY(_mp_enable_preemption_no_check)
824 cmpl $0,%gs:CPU_PREEMPTION_LEVEL
830 2: String "_mp_enable_preemption_no_check: preemption_level <= 0!"
833 #endif /* MACH_ASSERT */
834 _ENABLE_PREEMPTION_NO_CHECK
839 LEAF_ENTRY(i_bit_set)
846 LEAF_ENTRY(i_bit_clear)
862 LEAF_ENTRY(bit_lock_try)
868 LEAF_RET /* %eax better not be null ! */
873 LEAF_ENTRY(bit_unlock)