2 * Copyright (c) 2000-2016 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) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
57 #define ATOMIC_PRIVATE 1
58 #define LOCK_PRIVATE 1
60 #include <mach_ldebug.h>
63 #include <mach/kern_return.h>
64 #include <mach/mach_host_server.h>
65 #include <mach_debug/lockgroup_info.h>
67 #include <kern/locks.h>
68 #include <kern/misc_protos.h>
69 #include <kern/kalloc.h>
70 #include <kern/thread.h>
71 #include <kern/processor.h>
72 #include <kern/sched_prim.h>
73 #include <kern/debug.h>
74 #include <machine/atomic.h>
75 #include <machine/machine_cpu.h>
79 #include <sys/kdebug.h>
83 * We need only enough declarations from the BSD-side to be able to
84 * test if our probe is active, and to call __dtrace_probe(). Setting
85 * NEED_DTRACE_DEFS gets a local copy of those definitions pulled in.
87 #define NEED_DTRACE_DEFS
88 #include <../bsd/sys/lockstat.h>
91 #define LCK_MTX_SLEEP_CODE 0
92 #define LCK_MTX_SLEEP_DEADLINE_CODE 1
93 #define LCK_MTX_LCK_WAIT_CODE 2
94 #define LCK_MTX_UNLCK_WAKEUP_CODE 3
97 #define ALIGN_TEST(p,t) do{if((uintptr_t)p&(sizeof(t)-1)) __builtin_trap();}while(0)
99 #define ALIGN_TEST(p,t) do{}while(0)
102 /* Silence the volatile to _Atomic cast warning */
103 #define ATOMIC_CAST(t,p) ((_Atomic t*)(uintptr_t)(p))
105 /* Enforce program order of loads and stores. */
106 #define ordered_load(target, type) \
107 __c11_atomic_load((_Atomic type *)(target), memory_order_relaxed)
108 #define ordered_store(target, type, value) \
109 __c11_atomic_store((_Atomic type *)(target), value, memory_order_relaxed)
111 #define ordered_load_hw(lock) ordered_load(&(lock)->lock_data, uintptr_t)
112 #define ordered_store_hw(lock, value) ordered_store(&(lock)->lock_data, uintptr_t, (value))
114 #define NOINLINE __attribute__((noinline))
117 static queue_head_t lck_grp_queue
;
118 static unsigned int lck_grp_cnt
;
120 decl_lck_mtx_data(static,lck_grp_lock
)
121 static lck_mtx_ext_t lck_grp_lock_ext
;
123 lck_grp_attr_t LockDefaultGroupAttr
;
124 lck_grp_t LockCompatGroup
;
125 lck_attr_t LockDefaultLckAttr
;
127 #if CONFIG_DTRACE && __SMP__
128 #if defined (__x86_64__)
129 uint64_t dtrace_spin_threshold
= 500; // 500ns
130 #elif defined(__arm__) || defined(__arm64__)
131 uint64_t dtrace_spin_threshold
= LOCK_PANIC_TIMEOUT
/ 1000000; // 500ns
136 * Routine: lck_mod_init
144 * Obtain "lcks" options:this currently controls lock statistics
146 if (!PE_parse_boot_argn("lcks", &LcksOpts
, sizeof (LcksOpts
)))
150 #if (DEVELOPMENT || DEBUG) && defined(__x86_64__)
151 if (!PE_parse_boot_argn("-disable_mtx_chk", &LckDisablePreemptCheck
, sizeof (LckDisablePreemptCheck
)))
152 LckDisablePreemptCheck
= 0;
153 #endif /* (DEVELOPMENT || DEBUG) && defined(__x86_64__) */
155 queue_init(&lck_grp_queue
);
158 * Need to bootstrap the LockCompatGroup instead of calling lck_grp_init() here. This avoids
159 * grabbing the lck_grp_lock before it is initialized.
162 bzero(&LockCompatGroup
, sizeof(lck_grp_t
));
163 (void) strncpy(LockCompatGroup
.lck_grp_name
, "Compatibility APIs", LCK_GRP_MAX_NAME
);
165 if (LcksOpts
& enaLkStat
)
166 LockCompatGroup
.lck_grp_attr
= LCK_GRP_ATTR_STAT
;
168 LockCompatGroup
.lck_grp_attr
= LCK_ATTR_NONE
;
170 LockCompatGroup
.lck_grp_refcnt
= 1;
172 enqueue_tail(&lck_grp_queue
, (queue_entry_t
)&LockCompatGroup
);
175 lck_grp_attr_setdefault(&LockDefaultGroupAttr
);
176 lck_attr_setdefault(&LockDefaultLckAttr
);
178 lck_mtx_init_ext(&lck_grp_lock
, &lck_grp_lock_ext
, &LockCompatGroup
, &LockDefaultLckAttr
);
182 * Routine: lck_grp_attr_alloc_init
186 lck_grp_attr_alloc_init(
189 lck_grp_attr_t
*attr
;
191 if ((attr
= (lck_grp_attr_t
*)kalloc(sizeof(lck_grp_attr_t
))) != 0)
192 lck_grp_attr_setdefault(attr
);
199 * Routine: lck_grp_attr_setdefault
203 lck_grp_attr_setdefault(
204 lck_grp_attr_t
*attr
)
206 if (LcksOpts
& enaLkStat
)
207 attr
->grp_attr_val
= LCK_GRP_ATTR_STAT
;
209 attr
->grp_attr_val
= 0;
214 * Routine: lck_grp_attr_setstat
218 lck_grp_attr_setstat(
219 lck_grp_attr_t
*attr
)
221 (void)hw_atomic_or(&attr
->grp_attr_val
, LCK_GRP_ATTR_STAT
);
226 * Routine: lck_grp_attr_free
231 lck_grp_attr_t
*attr
)
233 kfree(attr
, sizeof(lck_grp_attr_t
));
238 * Routine: lck_grp_alloc_init
243 const char* grp_name
,
244 lck_grp_attr_t
*attr
)
248 if ((grp
= (lck_grp_t
*)kalloc(sizeof(lck_grp_t
))) != 0)
249 lck_grp_init(grp
, grp_name
, attr
);
255 * Routine: lck_grp_init
259 lck_grp_init(lck_grp_t
* grp
, const char * grp_name
, lck_grp_attr_t
* attr
)
261 /* make sure locking infrastructure has been initialized */
262 assert(lck_grp_cnt
> 0);
264 bzero((void *)grp
, sizeof(lck_grp_t
));
266 (void)strlcpy(grp
->lck_grp_name
, grp_name
, LCK_GRP_MAX_NAME
);
268 if (attr
!= LCK_GRP_ATTR_NULL
)
269 grp
->lck_grp_attr
= attr
->grp_attr_val
;
270 else if (LcksOpts
& enaLkStat
)
271 grp
->lck_grp_attr
= LCK_GRP_ATTR_STAT
;
273 grp
->lck_grp_attr
= LCK_ATTR_NONE
;
275 grp
->lck_grp_refcnt
= 1;
277 lck_mtx_lock(&lck_grp_lock
);
278 enqueue_tail(&lck_grp_queue
, (queue_entry_t
)grp
);
280 lck_mtx_unlock(&lck_grp_lock
);
284 * Routine: lck_grp_free
291 lck_mtx_lock(&lck_grp_lock
);
293 (void)remque((queue_entry_t
)grp
);
294 lck_mtx_unlock(&lck_grp_lock
);
295 lck_grp_deallocate(grp
);
300 * Routine: lck_grp_reference
307 (void)hw_atomic_add(&grp
->lck_grp_refcnt
, 1);
312 * Routine: lck_grp_deallocate
319 if (hw_atomic_sub(&grp
->lck_grp_refcnt
, 1) == 0)
320 kfree(grp
, sizeof(lck_grp_t
));
324 * Routine: lck_grp_lckcnt_incr
332 unsigned int *lckcnt
;
336 lckcnt
= &grp
->lck_grp_spincnt
;
339 lckcnt
= &grp
->lck_grp_mtxcnt
;
342 lckcnt
= &grp
->lck_grp_rwcnt
;
345 return panic("lck_grp_lckcnt_incr(): invalid lock type: %d\n", lck_type
);
348 (void)hw_atomic_add(lckcnt
, 1);
352 * Routine: lck_grp_lckcnt_decr
360 unsigned int *lckcnt
;
365 lckcnt
= &grp
->lck_grp_spincnt
;
368 lckcnt
= &grp
->lck_grp_mtxcnt
;
371 lckcnt
= &grp
->lck_grp_rwcnt
;
374 panic("lck_grp_lckcnt_decr(): invalid lock type: %d\n", lck_type
);
378 updated
= (int)hw_atomic_sub(lckcnt
, 1);
379 assert(updated
>= 0);
383 * Routine: lck_attr_alloc_init
392 if ((attr
= (lck_attr_t
*)kalloc(sizeof(lck_attr_t
))) != 0)
393 lck_attr_setdefault(attr
);
400 * Routine: lck_attr_setdefault
407 #if __arm__ || __arm64__
408 /* <rdar://problem/4404579>: Using LCK_ATTR_DEBUG here causes panic at boot time for arm */
409 attr
->lck_attr_val
= LCK_ATTR_NONE
;
410 #elif __i386__ || __x86_64__
412 if (LcksOpts
& enaLkDeb
)
413 attr
->lck_attr_val
= LCK_ATTR_DEBUG
;
415 attr
->lck_attr_val
= LCK_ATTR_NONE
;
417 attr
->lck_attr_val
= LCK_ATTR_DEBUG
;
420 #error Unknown architecture.
426 * Routine: lck_attr_setdebug
432 (void)hw_atomic_or(&attr
->lck_attr_val
, LCK_ATTR_DEBUG
);
436 * Routine: lck_attr_setdebug
442 (void)hw_atomic_and(&attr
->lck_attr_val
, ~LCK_ATTR_DEBUG
);
447 * Routine: lck_attr_rw_shared_priority
450 lck_attr_rw_shared_priority(
453 (void)hw_atomic_or(&attr
->lck_attr_val
, LCK_ATTR_RW_SHARED_PRIORITY
);
458 * Routine: lck_attr_free
464 kfree(attr
, sizeof(lck_attr_t
));
468 * Routine: hw_lock_init
470 * Initialize a hardware lock.
473 hw_lock_init(hw_lock_t lock
)
475 ordered_store_hw(lock
, 0);
479 * Routine: hw_lock_lock_contended
481 * Spin until lock is acquired or timeout expires.
482 * timeout is in mach_absolute_time ticks. Called with
483 * preemption disabled.
487 static unsigned int NOINLINE
488 hw_lock_lock_contended(hw_lock_t lock
, uintptr_t data
, uint64_t timeout
, boolean_t do_panic
)
491 uintptr_t holder
= lock
->lock_data
;
495 timeout
= LOCK_PANIC_TIMEOUT
;
498 boolean_t dtrace_enabled
= lockstat_probemap
[LS_LCK_SPIN_LOCK_SPIN
] != 0;
499 if (__improbable(dtrace_enabled
))
500 begin
= mach_absolute_time();
503 for (i
= 0; i
< LOCK_SNOOP_SPINS
; i
++) {
505 #if (!__ARM_ENABLE_WFE_) || (LOCK_PRETEST)
506 holder
= ordered_load_hw(lock
);
510 if (atomic_compare_exchange(&lock
->lock_data
, 0, data
,
511 memory_order_acquire_smp
, TRUE
)) {
513 if (__improbable(dtrace_enabled
)) {
514 uint64_t spintime
= mach_absolute_time() - begin
;
515 if (spintime
> dtrace_spin_threshold
)
516 LOCKSTAT_RECORD2(LS_LCK_SPIN_LOCK_SPIN
, lock
, spintime
, dtrace_spin_threshold
);
523 end
= ml_get_timebase() + timeout
;
525 else if (ml_get_timebase() >= end
)
529 // Capture the actual time spent blocked, which may be higher than the timeout
530 // if a misbehaving interrupt stole this thread's CPU time.
531 panic("Spinlock timeout after %llu ticks, %p = %lx",
532 (ml_get_timebase() - end
+ timeout
), lock
, holder
);
539 * Routine: hw_lock_lock
541 * Acquire lock, spinning until it becomes available,
542 * return with preemption disabled.
545 hw_lock_lock(hw_lock_t lock
)
550 thread
= current_thread();
551 disable_preemption_for_thread(thread
);
552 state
= LCK_MTX_THREAD_TO_STATE(thread
) | PLATFORM_LCK_ILOCK
;
556 if (ordered_load_hw(lock
))
558 #endif // LOCK_PRETEST
559 if (atomic_compare_exchange(&lock
->lock_data
, 0, state
,
560 memory_order_acquire_smp
, TRUE
)) {
565 #endif // LOCK_PRETEST
566 hw_lock_lock_contended(lock
, state
, 0, TRUE
);
570 panic("Spinlock held %p", lock
);
571 lock
->lock_data
= state
;
574 LOCKSTAT_RECORD(LS_LCK_SPIN_LOCK_ACQUIRE
, lock
, 0);
580 * Routine: hw_lock_to
582 * Acquire lock, spinning until it becomes available or timeout.
583 * Timeout is in mach_absolute_time ticks, return with
584 * preemption disabled.
587 hw_lock_to(hw_lock_t lock
, uint64_t timeout
)
591 unsigned int success
= 0;
593 thread
= current_thread();
594 disable_preemption_for_thread(thread
);
595 state
= LCK_MTX_THREAD_TO_STATE(thread
) | PLATFORM_LCK_ILOCK
;
599 if (ordered_load_hw(lock
))
601 #endif // LOCK_PRETEST
602 if (atomic_compare_exchange(&lock
->lock_data
, 0, state
,
603 memory_order_acquire_smp
, TRUE
)) {
609 #endif // LOCK_PRETEST
610 success
= hw_lock_lock_contended(lock
, state
, timeout
, FALSE
);
614 if (ordered_load_hw(lock
) == 0) {
615 ordered_store_hw(lock
, state
);
621 LOCKSTAT_RECORD(LS_LCK_SPIN_LOCK_ACQUIRE
, lock
, 0);
627 * Routine: hw_lock_try
629 * returns with preemption disabled on success.
632 hw_lock_try(hw_lock_t lock
)
634 thread_t thread
= current_thread();
636 #if LOCK_TRY_DISABLE_INT
639 intmask
= disable_interrupts();
641 disable_preemption_for_thread(thread
);
642 #endif // LOCK_TRY_DISABLE_INT
646 if (ordered_load_hw(lock
))
648 #endif // LOCK_PRETEST
649 success
= atomic_compare_exchange(&lock
->lock_data
, 0, LCK_MTX_THREAD_TO_STATE(thread
) | PLATFORM_LCK_ILOCK
,
650 memory_order_acquire_smp
, FALSE
);
652 if (lock
->lock_data
== 0) {
653 lock
->lock_data
= LCK_MTX_THREAD_TO_STATE(thread
) | PLATFORM_LCK_ILOCK
;
658 #if LOCK_TRY_DISABLE_INT
660 disable_preemption_for_thread(thread
);
663 #endif // LOCK_PRETEST
664 restore_interrupts(intmask
);
668 #endif // LOCK_PRETEST
671 #endif // LOCK_TRY_DISABLE_INT
674 LOCKSTAT_RECORD(LS_LCK_SPIN_LOCK_ACQUIRE
, lock
, 0);
680 * Routine: hw_lock_unlock
682 * Unconditionally release lock, release preemption level.
685 hw_lock_unlock(hw_lock_t lock
)
687 __c11_atomic_store((_Atomic
uintptr_t *)&lock
->lock_data
, 0, memory_order_release_smp
);
688 #if __arm__ || __arm64__
689 // ARM tests are only for open-source exclusion
691 #endif // __arm__ || __arm64__
693 LOCKSTAT_RECORD(LS_LCK_SPIN_UNLOCK_RELEASE
, lock
, 0);
694 #endif /* CONFIG_DTRACE */
699 * Routine hw_lock_held, doesn't change preemption state.
700 * N.B. Racy, of course.
703 hw_lock_held(hw_lock_t lock
)
705 return (ordered_load_hw(lock
) != 0);
709 * Routine: lck_spin_sleep
714 lck_sleep_action_t lck_sleep_action
,
716 wait_interrupt_t interruptible
)
720 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
721 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
723 res
= assert_wait(event
, interruptible
);
724 if (res
== THREAD_WAITING
) {
725 lck_spin_unlock(lck
);
726 res
= thread_block(THREAD_CONTINUE_NULL
);
727 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
))
731 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
732 lck_spin_unlock(lck
);
739 * Routine: lck_spin_sleep_deadline
742 lck_spin_sleep_deadline(
744 lck_sleep_action_t lck_sleep_action
,
746 wait_interrupt_t interruptible
,
751 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
752 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
754 res
= assert_wait_deadline(event
, interruptible
, deadline
);
755 if (res
== THREAD_WAITING
) {
756 lck_spin_unlock(lck
);
757 res
= thread_block(THREAD_CONTINUE_NULL
);
758 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
))
762 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
763 lck_spin_unlock(lck
);
770 * Routine: lck_mtx_clear_promoted
772 * Handle clearing of TH_SFLAG_PROMOTED,
773 * adjusting thread priority as needed.
775 * Called with thread lock held
778 lck_mtx_clear_promoted (
780 __kdebug_only
uintptr_t trace_lck
)
782 thread
->sched_flags
&= ~TH_SFLAG_PROMOTED
;
784 if (thread
->sched_flags
& TH_SFLAG_RW_PROMOTED
) {
785 /* Thread still has a RW lock promotion */
786 } else if (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) {
787 KERNEL_DEBUG_CONSTANT(
788 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_DEMOTE
) | DBG_FUNC_NONE
,
789 thread
->sched_pri
, DEPRESSPRI
, 0, trace_lck
, 0);
790 set_sched_pri(thread
, DEPRESSPRI
);
792 if (thread
->base_pri
< thread
->sched_pri
) {
793 KERNEL_DEBUG_CONSTANT(
794 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_DEMOTE
) | DBG_FUNC_NONE
,
795 thread
->sched_pri
, thread
->base_pri
, 0, trace_lck
, 0);
797 thread_recompute_sched_pri(thread
, FALSE
);
803 * Routine: lck_mtx_sleep
808 lck_sleep_action_t lck_sleep_action
,
810 wait_interrupt_t interruptible
)
813 thread_t thread
= current_thread();
815 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_SLEEP_CODE
) | DBG_FUNC_START
,
816 VM_KERNEL_UNSLIDE_OR_PERM(lck
), (int)lck_sleep_action
, VM_KERNEL_UNSLIDE_OR_PERM(event
), (int)interruptible
, 0);
818 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
819 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
821 if (lck_sleep_action
& LCK_SLEEP_PROMOTED_PRI
) {
823 * We overload the RW lock promotion to give us a priority ceiling
824 * during the time that this thread is asleep, so that when it
825 * is re-awakened (and not yet contending on the mutex), it is
826 * runnable at a reasonably high priority.
828 thread
->rwlock_count
++;
831 res
= assert_wait(event
, interruptible
);
832 if (res
== THREAD_WAITING
) {
834 res
= thread_block(THREAD_CONTINUE_NULL
);
835 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
)) {
836 if ((lck_sleep_action
& LCK_SLEEP_SPIN
))
837 lck_mtx_lock_spin(lck
);
838 else if ((lck_sleep_action
& LCK_SLEEP_SPIN_ALWAYS
))
839 lck_mtx_lock_spin_always(lck
);
845 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
848 if (lck_sleep_action
& LCK_SLEEP_PROMOTED_PRI
) {
849 if ((thread
->rwlock_count
-- == 1 /* field now 0 */) && (thread
->sched_flags
& TH_SFLAG_RW_PROMOTED
)) {
850 /* sched_flags checked without lock, but will be rechecked while clearing */
851 lck_rw_clear_promotion(thread
);
855 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_SLEEP_CODE
) | DBG_FUNC_END
, (int)res
, 0, 0, 0, 0);
862 * Routine: lck_mtx_sleep_deadline
865 lck_mtx_sleep_deadline(
867 lck_sleep_action_t lck_sleep_action
,
869 wait_interrupt_t interruptible
,
873 thread_t thread
= current_thread();
875 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_SLEEP_DEADLINE_CODE
) | DBG_FUNC_START
,
876 VM_KERNEL_UNSLIDE_OR_PERM(lck
), (int)lck_sleep_action
, VM_KERNEL_UNSLIDE_OR_PERM(event
), (int)interruptible
, 0);
878 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
879 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
881 if (lck_sleep_action
& LCK_SLEEP_PROMOTED_PRI
) {
883 * See lck_mtx_sleep().
885 thread
->rwlock_count
++;
888 res
= assert_wait_deadline(event
, interruptible
, deadline
);
889 if (res
== THREAD_WAITING
) {
891 res
= thread_block(THREAD_CONTINUE_NULL
);
892 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
)) {
893 if ((lck_sleep_action
& LCK_SLEEP_SPIN
))
894 lck_mtx_lock_spin(lck
);
900 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
903 if (lck_sleep_action
& LCK_SLEEP_PROMOTED_PRI
) {
904 if ((thread
->rwlock_count
-- == 1 /* field now 0 */) && (thread
->sched_flags
& TH_SFLAG_RW_PROMOTED
)) {
905 /* sched_flags checked without lock, but will be rechecked while clearing */
906 lck_rw_clear_promotion(thread
);
910 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_SLEEP_DEADLINE_CODE
) | DBG_FUNC_END
, (int)res
, 0, 0, 0, 0);
916 * Routine: lck_mtx_lock_wait
918 * Invoked in order to wait on contention.
920 * Called with the interlock locked and
921 * returns it unlocked.
928 thread_t self
= current_thread();
930 __kdebug_only
uintptr_t trace_lck
= VM_KERNEL_UNSLIDE_OR_PERM(lck
);
931 __kdebug_only
uintptr_t trace_holder
= VM_KERNEL_UNSLIDE_OR_PERM(holder
);
933 spl_t s
= splsched();
935 uint64_t sleep_start
= 0;
937 if (lockstat_probemap
[LS_LCK_MTX_LOCK_BLOCK
] || lockstat_probemap
[LS_LCK_MTX_EXT_LOCK_BLOCK
]) {
938 sleep_start
= mach_absolute_time();
942 if (lck
->lck_mtx_tag
!= LCK_MTX_TAG_INDIRECT
)
945 mutex
= &lck
->lck_mtx_ptr
->lck_mtx
;
947 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_LCK_WAIT_CODE
) | DBG_FUNC_START
, trace_lck
, trace_holder
, 0, 0, 0);
949 priority
= self
->sched_pri
;
950 if (priority
< self
->base_pri
)
951 priority
= self
->base_pri
;
952 if (priority
< BASEPRI_DEFAULT
)
953 priority
= BASEPRI_DEFAULT
;
955 /* Do not promote past promotion ceiling */
956 priority
= MIN(priority
, MAXPRI_PROMOTE
);
959 if (mutex
->lck_mtx_pri
== 0) {
960 holder
->promotions
++;
961 holder
->sched_flags
|= TH_SFLAG_PROMOTED
;
964 if (mutex
->lck_mtx_pri
< priority
&& holder
->sched_pri
< priority
) {
965 KERNEL_DEBUG_CONSTANT(
966 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_PROMOTE
) | DBG_FUNC_NONE
,
967 holder
->sched_pri
, priority
, trace_holder
, trace_lck
, 0);
968 set_sched_pri(holder
, priority
);
970 thread_unlock(holder
);
973 if (mutex
->lck_mtx_pri
< priority
)
974 mutex
->lck_mtx_pri
= priority
;
975 if (self
->pending_promoter
[self
->pending_promoter_index
] == NULL
) {
976 self
->pending_promoter
[self
->pending_promoter_index
] = mutex
;
977 mutex
->lck_mtx_waiters
++;
980 if (self
->pending_promoter
[self
->pending_promoter_index
] != mutex
) {
981 self
->pending_promoter
[++self
->pending_promoter_index
] = mutex
;
982 mutex
->lck_mtx_waiters
++;
985 thread_set_pending_block_hint(self
, kThreadWaitKernelMutex
);
986 assert_wait(LCK_MTX_EVENT(mutex
), THREAD_UNINT
);
987 lck_mtx_ilk_unlock(mutex
);
989 thread_block(THREAD_CONTINUE_NULL
);
991 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_LCK_WAIT_CODE
) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
994 * Record the DTrace lockstat probe for blocking, block time
995 * measured from when we were entered.
998 if (lck
->lck_mtx_tag
!= LCK_MTX_TAG_INDIRECT
) {
999 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_BLOCK
, lck
,
1000 mach_absolute_time() - sleep_start
);
1002 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_LOCK_BLOCK
, lck
,
1003 mach_absolute_time() - sleep_start
);
1010 * Routine: lck_mtx_lock_acquire
1012 * Invoked on acquiring the mutex when there is
1015 * Returns the current number of waiters.
1017 * Called with the interlock locked.
1020 lck_mtx_lock_acquire(
1023 thread_t thread
= current_thread();
1027 __kdebug_only
uintptr_t trace_lck
= VM_KERNEL_UNSLIDE_OR_PERM(lck
);
1029 if (lck
->lck_mtx_tag
!= LCK_MTX_TAG_INDIRECT
)
1032 mutex
= &lck
->lck_mtx_ptr
->lck_mtx
;
1034 if (thread
->pending_promoter
[thread
->pending_promoter_index
] == mutex
) {
1035 thread
->pending_promoter
[thread
->pending_promoter_index
] = NULL
;
1036 if (thread
->pending_promoter_index
> 0)
1037 thread
->pending_promoter_index
--;
1038 mutex
->lck_mtx_waiters
--;
1041 if (mutex
->lck_mtx_waiters
)
1042 priority
= mutex
->lck_mtx_pri
;
1044 mutex
->lck_mtx_pri
= 0;
1048 if (priority
|| thread
->was_promoted_on_wakeup
) {
1050 thread_lock(thread
);
1053 thread
->promotions
++;
1054 thread
->sched_flags
|= TH_SFLAG_PROMOTED
;
1055 if (thread
->sched_pri
< priority
) {
1056 KERNEL_DEBUG_CONSTANT(
1057 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_PROMOTE
) | DBG_FUNC_NONE
,
1058 thread
->sched_pri
, priority
, 0, trace_lck
, 0);
1059 /* Do not promote past promotion ceiling */
1060 assert(priority
<= MAXPRI_PROMOTE
);
1061 set_sched_pri(thread
, priority
);
1064 if (thread
->was_promoted_on_wakeup
) {
1065 thread
->was_promoted_on_wakeup
= 0;
1066 if (thread
->promotions
== 0)
1067 lck_mtx_clear_promoted(thread
, trace_lck
);
1070 thread_unlock(thread
);
1075 if (lockstat_probemap
[LS_LCK_MTX_LOCK_ACQUIRE
] || lockstat_probemap
[LS_LCK_MTX_EXT_LOCK_ACQUIRE
]) {
1076 if (lck
->lck_mtx_tag
!= LCK_MTX_TAG_INDIRECT
) {
1077 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_ACQUIRE
, lck
, 0);
1079 LOCKSTAT_RECORD(LS_LCK_MTX_EXT_LOCK_ACQUIRE
, lck
, 0);
1083 return (mutex
->lck_mtx_waiters
);
1087 * Routine: lck_mtx_unlock_wakeup
1089 * Invoked on unlock when there is contention.
1091 * Called with the interlock locked.
1094 lck_mtx_unlock_wakeup (
1098 thread_t thread
= current_thread();
1100 __kdebug_only
uintptr_t trace_lck
= VM_KERNEL_UNSLIDE_OR_PERM(lck
);
1102 if (lck
->lck_mtx_tag
!= LCK_MTX_TAG_INDIRECT
)
1105 mutex
= &lck
->lck_mtx_ptr
->lck_mtx
;
1107 if (thread
!= holder
)
1108 panic("lck_mtx_unlock_wakeup: mutex %p holder %p\n", mutex
, holder
);
1110 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_UNLCK_WAKEUP_CODE
) | DBG_FUNC_START
, trace_lck
, VM_KERNEL_UNSLIDE_OR_PERM(holder
), 0, 0, 0);
1112 assert(mutex
->lck_mtx_waiters
> 0);
1113 if (mutex
->lck_mtx_waiters
> 1)
1114 thread_wakeup_one_with_pri(LCK_MTX_EVENT(lck
), lck
->lck_mtx_pri
);
1116 thread_wakeup_one(LCK_MTX_EVENT(lck
));
1118 if (thread
->promotions
> 0) {
1119 spl_t s
= splsched();
1121 thread_lock(thread
);
1122 if (--thread
->promotions
== 0 && (thread
->sched_flags
& TH_SFLAG_PROMOTED
))
1123 lck_mtx_clear_promoted(thread
, trace_lck
);
1124 thread_unlock(thread
);
1128 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_UNLCK_WAKEUP_CODE
) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
1132 lck_mtx_unlockspin_wakeup (
1135 assert(lck
->lck_mtx_waiters
> 0);
1136 thread_wakeup_one(LCK_MTX_EVENT(lck
));
1138 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS
, LCK_MTX_UNLCK_WAKEUP_CODE
) | DBG_FUNC_NONE
, VM_KERNEL_UNSLIDE_OR_PERM(lck
), 0, 0, 1, 0);
1141 * When there are waiters, we skip the hot-patch spot in the
1142 * fastpath, so we record it here.
1144 LOCKSTAT_RECORD(LS_LCK_MTX_UNLOCK_RELEASE
, lck
, 0);
1150 * Routine: mutex_pause
1152 * Called by former callers of simple_lock_pause().
1154 #define MAX_COLLISION_COUNTS 32
1155 #define MAX_COLLISION 8
1157 unsigned int max_collision_count
[MAX_COLLISION_COUNTS
];
1159 uint32_t collision_backoffs
[MAX_COLLISION
] = {
1160 10, 50, 100, 200, 400, 600, 800, 1000
1165 mutex_pause(uint32_t collisions
)
1167 wait_result_t wait_result
;
1170 if (collisions
>= MAX_COLLISION_COUNTS
)
1171 collisions
= MAX_COLLISION_COUNTS
- 1;
1172 max_collision_count
[collisions
]++;
1174 if (collisions
>= MAX_COLLISION
)
1175 collisions
= MAX_COLLISION
- 1;
1176 back_off
= collision_backoffs
[collisions
];
1178 wait_result
= assert_wait_timeout((event_t
)mutex_pause
, THREAD_UNINT
, back_off
, NSEC_PER_USEC
);
1179 assert(wait_result
== THREAD_WAITING
);
1181 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1182 assert(wait_result
== THREAD_TIMED_OUT
);
1186 unsigned int mutex_yield_wait
= 0;
1187 unsigned int mutex_yield_no_wait
= 0;
1196 lck_mtx_assert(lck
, LCK_MTX_ASSERT_OWNED
);
1199 if (lck
->lck_mtx_tag
== LCK_MTX_TAG_INDIRECT
)
1200 waiters
= lck
->lck_mtx_ptr
->lck_mtx
.lck_mtx_waiters
;
1202 waiters
= lck
->lck_mtx_waiters
;
1205 mutex_yield_no_wait
++;
1208 lck_mtx_unlock(lck
);
1216 * Routine: lck_rw_sleep
1221 lck_sleep_action_t lck_sleep_action
,
1223 wait_interrupt_t interruptible
)
1226 lck_rw_type_t lck_rw_type
;
1227 thread_t thread
= current_thread();
1229 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
1230 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
1232 if (lck_sleep_action
& LCK_SLEEP_PROMOTED_PRI
) {
1234 * Although we are dropping the RW lock, the intent in most cases
1235 * is that this thread remains as an observer, since it may hold
1236 * some secondary resource, but must yield to avoid deadlock. In
1237 * this situation, make sure that the thread is boosted to the
1238 * RW lock ceiling while blocked, so that it can re-acquire the
1239 * RW lock at that priority.
1241 thread
->rwlock_count
++;
1244 res
= assert_wait(event
, interruptible
);
1245 if (res
== THREAD_WAITING
) {
1246 lck_rw_type
= lck_rw_done(lck
);
1247 res
= thread_block(THREAD_CONTINUE_NULL
);
1248 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
)) {
1249 if (!(lck_sleep_action
& (LCK_SLEEP_SHARED
|LCK_SLEEP_EXCLUSIVE
)))
1250 lck_rw_lock(lck
, lck_rw_type
);
1251 else if (lck_sleep_action
& LCK_SLEEP_EXCLUSIVE
)
1252 lck_rw_lock_exclusive(lck
);
1254 lck_rw_lock_shared(lck
);
1258 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
1259 (void)lck_rw_done(lck
);
1261 if (lck_sleep_action
& LCK_SLEEP_PROMOTED_PRI
) {
1262 if ((thread
->rwlock_count
-- == 1 /* field now 0 */) && (thread
->sched_flags
& TH_SFLAG_RW_PROMOTED
)) {
1263 /* sched_flags checked without lock, but will be rechecked while clearing */
1265 /* Only if the caller wanted the lck_rw_t returned unlocked should we drop to 0 */
1266 assert(lck_sleep_action
& LCK_SLEEP_UNLOCK
);
1268 lck_rw_clear_promotion(thread
);
1277 * Routine: lck_rw_sleep_deadline
1280 lck_rw_sleep_deadline(
1282 lck_sleep_action_t lck_sleep_action
,
1284 wait_interrupt_t interruptible
,
1288 lck_rw_type_t lck_rw_type
;
1289 thread_t thread
= current_thread();
1291 if ((lck_sleep_action
& ~LCK_SLEEP_MASK
) != 0)
1292 panic("Invalid lock sleep action %x\n", lck_sleep_action
);
1294 if (lck_sleep_action
& LCK_SLEEP_PROMOTED_PRI
) {
1295 thread
->rwlock_count
++;
1298 res
= assert_wait_deadline(event
, interruptible
, deadline
);
1299 if (res
== THREAD_WAITING
) {
1300 lck_rw_type
= lck_rw_done(lck
);
1301 res
= thread_block(THREAD_CONTINUE_NULL
);
1302 if (!(lck_sleep_action
& LCK_SLEEP_UNLOCK
)) {
1303 if (!(lck_sleep_action
& (LCK_SLEEP_SHARED
|LCK_SLEEP_EXCLUSIVE
)))
1304 lck_rw_lock(lck
, lck_rw_type
);
1305 else if (lck_sleep_action
& LCK_SLEEP_EXCLUSIVE
)
1306 lck_rw_lock_exclusive(lck
);
1308 lck_rw_lock_shared(lck
);
1312 if (lck_sleep_action
& LCK_SLEEP_UNLOCK
)
1313 (void)lck_rw_done(lck
);
1315 if (lck_sleep_action
& LCK_SLEEP_PROMOTED_PRI
) {
1316 if ((thread
->rwlock_count
-- == 1 /* field now 0 */) && (thread
->sched_flags
& TH_SFLAG_RW_PROMOTED
)) {
1317 /* sched_flags checked without lock, but will be rechecked while clearing */
1319 /* Only if the caller wanted the lck_rw_t returned unlocked should we drop to 0 */
1320 assert(lck_sleep_action
& LCK_SLEEP_UNLOCK
);
1322 lck_rw_clear_promotion(thread
);
1330 * Reader-writer lock promotion
1332 * We support a limited form of reader-writer
1333 * lock promotion whose effects are:
1335 * * Qualifying threads have decay disabled
1336 * * Scheduler priority is reset to a floor of
1337 * of their statically assigned priority
1338 * or BASEPRI_BACKGROUND
1340 * The rationale is that lck_rw_ts do not have
1341 * a single owner, so we cannot apply a directed
1342 * priority boost from all waiting threads
1343 * to all holding threads without maintaining
1344 * lists of all shared owners and all waiting
1345 * threads for every lock.
1347 * Instead (and to preserve the uncontended fast-
1348 * path), acquiring (or attempting to acquire)
1349 * a RW lock in shared or exclusive lock increments
1350 * a per-thread counter. Only if that thread stops
1351 * making forward progress (for instance blocking
1352 * on a mutex, or being preempted) do we consult
1353 * the counter and apply the priority floor.
1354 * When the thread becomes runnable again (or in
1355 * the case of preemption it never stopped being
1356 * runnable), it has the priority boost and should
1357 * be in a good position to run on the CPU and
1358 * release all RW locks (at which point the priority
1359 * boost is cleared).
1361 * Care must be taken to ensure that priority
1362 * boosts are not retained indefinitely, since unlike
1363 * mutex priority boosts (where the boost is tied
1364 * to the mutex lifecycle), the boost is tied
1365 * to the thread and independent of any particular
1366 * lck_rw_t. Assertions are in place on return
1367 * to userspace so that the boost is not held
1370 * The routines that increment/decrement the
1371 * per-thread counter should err on the side of
1372 * incrementing any time a preemption is possible
1373 * and the lock would be visible to the rest of the
1374 * system as held (so it should be incremented before
1375 * interlocks are dropped/preemption is enabled, or
1376 * before a CAS is executed to acquire the lock).
1381 * lck_rw_clear_promotion: Undo priority promotions when the last RW
1382 * lock is released by a thread (if a promotion was active)
1384 void lck_rw_clear_promotion(thread_t thread
)
1386 assert(thread
->rwlock_count
== 0);
1388 /* Cancel any promotions if the thread had actually blocked while holding a RW lock */
1389 spl_t s
= splsched();
1391 thread_lock(thread
);
1393 if (thread
->sched_flags
& TH_SFLAG_RW_PROMOTED
) {
1394 thread
->sched_flags
&= ~TH_SFLAG_RW_PROMOTED
;
1396 if (thread
->sched_flags
& TH_SFLAG_PROMOTED
) {
1397 /* Thread still has a mutex promotion */
1398 } else if (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) {
1399 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_RW_DEMOTE
) | DBG_FUNC_NONE
,
1400 (uintptr_t)thread_tid(thread
), thread
->sched_pri
, DEPRESSPRI
, 0, 0);
1402 set_sched_pri(thread
, DEPRESSPRI
);
1404 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_RW_DEMOTE
) | DBG_FUNC_NONE
,
1405 (uintptr_t)thread_tid(thread
), thread
->sched_pri
, thread
->base_pri
, 0, 0);
1407 thread_recompute_sched_pri(thread
, FALSE
);
1411 thread_unlock(thread
);
1416 * Callout from context switch if the thread goes
1417 * off core with a positive rwlock_count
1419 * Called at splsched with the thread locked
1422 lck_rw_set_promotion_locked(thread_t thread
)
1424 if (LcksOpts
& disLkRWPrio
)
1429 priority
= thread
->sched_pri
;
1431 if (priority
< thread
->base_pri
)
1432 priority
= thread
->base_pri
;
1433 if (priority
< BASEPRI_BACKGROUND
)
1434 priority
= BASEPRI_BACKGROUND
;
1436 if ((thread
->sched_pri
< priority
) ||
1437 !(thread
->sched_flags
& TH_SFLAG_RW_PROMOTED
)) {
1438 KERNEL_DEBUG_CONSTANT(
1439 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_RW_PROMOTE
) | DBG_FUNC_NONE
,
1440 (uintptr_t)thread_tid(thread
), thread
->sched_pri
,
1441 thread
->base_pri
, priority
, 0);
1443 thread
->sched_flags
|= TH_SFLAG_RW_PROMOTED
;
1445 if (thread
->sched_pri
< priority
)
1446 set_sched_pri(thread
, priority
);
1451 host_lockgroup_info(
1453 lockgroup_info_array_t
*lockgroup_infop
,
1454 mach_msg_type_number_t
*lockgroup_infoCntp
)
1456 lockgroup_info_t
*lockgroup_info_base
;
1457 lockgroup_info_t
*lockgroup_info
;
1458 vm_offset_t lockgroup_info_addr
;
1459 vm_size_t lockgroup_info_size
;
1460 vm_size_t lockgroup_info_vmsize
;
1466 if (host
== HOST_NULL
)
1467 return KERN_INVALID_HOST
;
1469 lck_mtx_lock(&lck_grp_lock
);
1471 lockgroup_info_size
= lck_grp_cnt
* sizeof(*lockgroup_info
);
1472 lockgroup_info_vmsize
= round_page(lockgroup_info_size
);
1473 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1474 &lockgroup_info_addr
, lockgroup_info_vmsize
, VM_KERN_MEMORY_IPC
);
1475 if (kr
!= KERN_SUCCESS
) {
1476 lck_mtx_unlock(&lck_grp_lock
);
1480 lockgroup_info_base
= (lockgroup_info_t
*) lockgroup_info_addr
;
1481 lck_grp
= (lck_grp_t
*)queue_first(&lck_grp_queue
);
1482 lockgroup_info
= lockgroup_info_base
;
1484 for (i
= 0; i
< lck_grp_cnt
; i
++) {
1486 lockgroup_info
->lock_spin_cnt
= lck_grp
->lck_grp_spincnt
;
1487 lockgroup_info
->lock_spin_util_cnt
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_util_cnt
;
1488 lockgroup_info
->lock_spin_held_cnt
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_held_cnt
;
1489 lockgroup_info
->lock_spin_miss_cnt
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_miss_cnt
;
1490 lockgroup_info
->lock_spin_held_max
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_held_max
;
1491 lockgroup_info
->lock_spin_held_cum
= lck_grp
->lck_grp_stat
.lck_grp_spin_stat
.lck_grp_spin_held_cum
;
1493 lockgroup_info
->lock_mtx_cnt
= lck_grp
->lck_grp_mtxcnt
;
1494 lockgroup_info
->lock_mtx_util_cnt
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_util_cnt
;
1495 lockgroup_info
->lock_mtx_held_cnt
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_held_cnt
;
1496 lockgroup_info
->lock_mtx_miss_cnt
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_miss_cnt
;
1497 lockgroup_info
->lock_mtx_wait_cnt
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_wait_cnt
;
1498 lockgroup_info
->lock_mtx_held_max
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_held_max
;
1499 lockgroup_info
->lock_mtx_held_cum
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_held_cum
;
1500 lockgroup_info
->lock_mtx_wait_max
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_wait_max
;
1501 lockgroup_info
->lock_mtx_wait_cum
= lck_grp
->lck_grp_stat
.lck_grp_mtx_stat
.lck_grp_mtx_wait_cum
;
1503 lockgroup_info
->lock_rw_cnt
= lck_grp
->lck_grp_rwcnt
;
1504 lockgroup_info
->lock_rw_util_cnt
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_util_cnt
;
1505 lockgroup_info
->lock_rw_held_cnt
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_held_cnt
;
1506 lockgroup_info
->lock_rw_miss_cnt
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_miss_cnt
;
1507 lockgroup_info
->lock_rw_wait_cnt
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_wait_cnt
;
1508 lockgroup_info
->lock_rw_held_max
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_held_max
;
1509 lockgroup_info
->lock_rw_held_cum
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_held_cum
;
1510 lockgroup_info
->lock_rw_wait_max
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_wait_max
;
1511 lockgroup_info
->lock_rw_wait_cum
= lck_grp
->lck_grp_stat
.lck_grp_rw_stat
.lck_grp_rw_wait_cum
;
1513 (void) strncpy(lockgroup_info
->lockgroup_name
,lck_grp
->lck_grp_name
, LOCKGROUP_MAX_NAME
);
1515 lck_grp
= (lck_grp_t
*)(queue_next((queue_entry_t
)(lck_grp
)));
1519 *lockgroup_infoCntp
= lck_grp_cnt
;
1520 lck_mtx_unlock(&lck_grp_lock
);
1522 if (lockgroup_info_size
!= lockgroup_info_vmsize
)
1523 bzero((char *)lockgroup_info
, lockgroup_info_vmsize
- lockgroup_info_size
);
1525 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)lockgroup_info_addr
,
1526 (vm_map_size_t
)lockgroup_info_size
, TRUE
, ©
);
1527 assert(kr
== KERN_SUCCESS
);
1529 *lockgroup_infop
= (lockgroup_info_t
*) copy
;
1531 return(KERN_SUCCESS
);
1535 * Atomic primitives, prototyped in kern/simple_lock.h
1536 * Noret versions are more efficient on some architectures
1540 hw_atomic_add(volatile uint32_t *dest
, uint32_t delt
)
1542 ALIGN_TEST(dest
,uint32_t);
1543 return __c11_atomic_fetch_add(ATOMIC_CAST(uint32_t,dest
), delt
, memory_order_relaxed
) + delt
;
1547 hw_atomic_sub(volatile uint32_t *dest
, uint32_t delt
)
1549 ALIGN_TEST(dest
,uint32_t);
1550 return __c11_atomic_fetch_sub(ATOMIC_CAST(uint32_t,dest
), delt
, memory_order_relaxed
) - delt
;
1554 hw_atomic_or(volatile uint32_t *dest
, uint32_t mask
)
1556 ALIGN_TEST(dest
,uint32_t);
1557 return __c11_atomic_fetch_or(ATOMIC_CAST(uint32_t,dest
), mask
, memory_order_relaxed
) | mask
;
1561 hw_atomic_or_noret(volatile uint32_t *dest
, uint32_t mask
)
1563 ALIGN_TEST(dest
,uint32_t);
1564 __c11_atomic_fetch_or(ATOMIC_CAST(uint32_t,dest
), mask
, memory_order_relaxed
);
1568 hw_atomic_and(volatile uint32_t *dest
, uint32_t mask
)
1570 ALIGN_TEST(dest
,uint32_t);
1571 return __c11_atomic_fetch_and(ATOMIC_CAST(uint32_t,dest
), mask
, memory_order_relaxed
) & mask
;
1575 hw_atomic_and_noret(volatile uint32_t *dest
, uint32_t mask
)
1577 ALIGN_TEST(dest
,uint32_t);
1578 __c11_atomic_fetch_and(ATOMIC_CAST(uint32_t,dest
), mask
, memory_order_relaxed
);
1582 hw_compare_and_store(uint32_t oldval
, uint32_t newval
, volatile uint32_t *dest
)
1584 ALIGN_TEST(dest
,uint32_t);
1585 return __c11_atomic_compare_exchange_strong(ATOMIC_CAST(uint32_t,dest
), &oldval
, newval
,
1586 memory_order_acq_rel_smp
, memory_order_relaxed
);