2 * Copyright (c) 2000-2018 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@
29 #define LOCK_PRIVATE 1
31 #include <mach_ldebug.h>
33 #include <kern/locks.h>
34 #include <kern/kalloc.h>
35 #include <kern/misc_protos.h>
36 #include <kern/thread.h>
37 #include <kern/processor.h>
38 #include <kern/cpu_data.h>
39 #include <kern/cpu_number.h>
40 #include <kern/sched_prim.h>
41 #include <kern/debug.h>
44 #include <i386/machine_routines.h> /* machine_timeout_suspended() */
45 #include <machine/atomic.h>
46 #include <machine/machine_cpu.h>
48 #include <machine/atomic.h>
49 #include <sys/kdebug.h>
50 #include <i386/locks_i386_inlines.h>
53 * Fast path routines for lck_mtx locking and unlocking functions.
54 * Fast paths will try a single compare and swap instruction to acquire/release the lock
55 * and interlock, and they will fall through the slow path in case it fails.
57 * These functions were previously implemented in x86 assembly,
58 * and some optimizations are in place in this c code to obtain a compiled code
59 * as performant and compact as the assembly version.
61 * To avoid to inline these functions and increase the kernel text size all functions have
62 * the __attribute__((noinline)) specified.
64 * The code is structured in such a way there are no calls to functions that will return
65 * on the context of the caller function, i.e. all functions called are or tail call functions
66 * or inline functions. The number of arguments of the tail call functions are less then six,
67 * so that they can be passed over registers and do not need to be pushed on stack.
68 * This allows the compiler to not create a stack frame for the functions.
70 * The file is compiled with momit-leaf-frame-pointer and O2.
73 #if DEVELOPMENT || DEBUG
76 * If one or more simplelocks are currently held by a thread,
77 * an attempt to acquire a mutex will cause this check to fail
78 * (since a mutex lock may context switch, holding a simplelock
79 * is not a good thing).
82 lck_mtx_check_preemption(void)
84 if (get_preemption_level() == 0) {
87 if (LckDisablePreemptCheck
) {
90 if (current_cpu_datap()->cpu_hibernate
) {
94 panic("preemption_level(%d) != 0\n", get_preemption_level());
97 #else /* DEVELOPMENT || DEBUG */
100 lck_mtx_check_preemption(void)
105 #endif /* DEVELOPMENT || DEBUG */
108 * Routine: lck_mtx_lock
110 * Locks a mutex for current thread.
111 * It tries the fast path first and
112 * falls through the slow path in case
115 * Interlock or mutex cannot be already held by current thread.
116 * In case of contention it might sleep.
118 __attribute__((noinline
))
123 uint32_t prev
, state
;
125 lck_mtx_check_preemption();
126 state
= ordered_load_mtx_state(lock
);
129 * Fast path only if the mutex is not held
130 * interlock is not contended and there are no waiters.
131 * Indirect mutexes will fall through the slow path as
132 * well as destroyed mutexes.
135 prev
= state
& ~(LCK_MTX_ILOCKED_MSK
| LCK_MTX_MLOCKED_MSK
| LCK_MTX_WAITERS_MSK
);
136 state
= prev
| LCK_MTX_ILOCKED_MSK
| LCK_MTX_MLOCKED_MSK
;
138 disable_preemption();
139 if (!os_atomic_cmpxchg(&lock
->lck_mtx_state
, prev
, state
, acquire
)) {
141 return lck_mtx_lock_slow(lock
);
144 /* mutex acquired, interlock acquired and preemption disabled */
146 thread_t thread
= current_thread();
147 /* record owner of mutex */
148 ordered_store_mtx_owner(lock
, (uintptr_t)thread
);
152 thread
->mutex_count
++; /* lock statistic */
156 /* release interlock and re-enable preemption */
157 lck_mtx_lock_finish_inline(lock
, state
, FALSE
);
161 * Routine: lck_mtx_try_lock
163 * Try to lock a mutex for current thread.
164 * It tries the fast path first and
165 * falls through the slow path in case
168 * Interlock or mutex cannot be already held by current thread.
170 * In case the mutex is held (either as spin or mutex)
171 * the function will fail, it will acquire the mutex otherwise.
173 __attribute__((noinline
))
178 uint32_t prev
, state
;
180 state
= ordered_load_mtx_state(lock
);
183 * Fast path only if the mutex is not held
184 * interlock is not contended and there are no waiters.
185 * Indirect mutexes will fall through the slow path as
186 * well as destroyed mutexes.
189 prev
= state
& ~(LCK_MTX_ILOCKED_MSK
| LCK_MTX_MLOCKED_MSK
| LCK_MTX_WAITERS_MSK
);
190 state
= prev
| LCK_MTX_ILOCKED_MSK
| LCK_MTX_MLOCKED_MSK
;
192 disable_preemption();
193 if (!os_atomic_cmpxchg(&lock
->lck_mtx_state
, prev
, state
, acquire
)) {
195 return lck_mtx_try_lock_slow(lock
);
198 /* mutex acquired, interlock acquired and preemption disabled */
200 thread_t thread
= current_thread();
201 /* record owner of mutex */
202 ordered_store_mtx_owner(lock
, (uintptr_t)thread
);
206 thread
->mutex_count
++; /* lock statistic */
210 /* release interlock and re-enable preemption */
211 lck_mtx_try_lock_finish_inline(lock
, state
);
217 * Routine: lck_mtx_lock_spin_always
219 * Try to lock a mutex as spin lock for current thread.
220 * It tries the fast path first and
221 * falls through the slow path in case
224 * Interlock or mutex cannot be already held by current thread.
226 * In case the mutex is held as mutex by another thread
227 * this function will switch behavior and try to acquire the lock as mutex.
229 * In case the mutex is held as spinlock it will spin contending
232 * In case of contention it might sleep.
234 __attribute__((noinline
))
236 lck_mtx_lock_spin_always(
239 uint32_t prev
, state
;
241 state
= ordered_load_mtx_state(lock
);
244 * Fast path only if the mutex is not held
245 * neither as mutex nor as spin and
246 * interlock is not contended.
247 * Indirect mutexes will fall through the slow path as
248 * well as destroyed mutexes.
251 /* Note LCK_MTX_SPIN_MSK is set only if LCK_MTX_ILOCKED_MSK is set */
252 prev
= state
& ~(LCK_MTX_ILOCKED_MSK
| LCK_MTX_MLOCKED_MSK
);
253 state
= prev
| LCK_MTX_ILOCKED_MSK
| LCK_MTX_SPIN_MSK
;
255 disable_preemption();
256 if (!os_atomic_cmpxchg(&lock
->lck_mtx_state
, prev
, state
, acquire
)) {
258 return lck_mtx_lock_spin_slow(lock
);
261 /* mutex acquired as spinlock, interlock acquired and preemption disabled */
263 thread_t thread
= current_thread();
264 /* record owner of mutex */
265 ordered_store_mtx_owner(lock
, (uintptr_t)thread
);
269 thread
->mutex_count
++; /* lock statistic */
274 LOCKSTAT_RECORD(LS_LCK_MTX_LOCK_SPIN_ACQUIRE
, lock
, 0);
276 /* return with the interlock held and preemption disabled */
281 * Routine: lck_mtx_lock_spin
283 * Try to lock a mutex as spin lock for current thread.
284 * It tries the fast path first and
285 * falls through the slow path in case
288 * Interlock or mutex cannot be already held by current thread.
290 * In case the mutex is held as mutex by another thread
291 * this function will switch behavior and try to acquire the lock as mutex.
293 * In case the mutex is held as spinlock it will spin contending
296 * In case of contention it might sleep.
302 lck_mtx_check_preemption();
303 lck_mtx_lock_spin_always(lock
);
307 * Routine: lck_mtx_try_lock_spin_always
309 * Try to lock a mutex as spin lock for current thread.
310 * It tries the fast path first and
311 * falls through the slow path in case
314 * Interlock or mutex cannot be already held by current thread.
316 * In case the mutex is held (either as spin or mutex)
317 * the function will fail, it will acquire the mutex as spin lock
321 __attribute__((noinline
))
323 lck_mtx_try_lock_spin_always(
326 uint32_t prev
, state
;
328 state
= ordered_load_mtx_state(lock
);
331 * Fast path only if the mutex is not held
332 * neither as mutex nor as spin and
333 * interlock is not contended.
334 * Indirect mutexes will fall through the slow path as
335 * well as destroyed mutexes.
338 /* Note LCK_MTX_SPIN_MSK is set only if LCK_MTX_ILOCKED_MSK is set */
339 prev
= state
& ~(LCK_MTX_ILOCKED_MSK
| LCK_MTX_MLOCKED_MSK
);
340 state
= prev
| LCK_MTX_ILOCKED_MSK
| LCK_MTX_SPIN_MSK
;
342 disable_preemption();
343 if (!os_atomic_cmpxchg(&lock
->lck_mtx_state
, prev
, state
, acquire
)) {
345 return lck_mtx_try_lock_spin_slow(lock
);
348 /* mutex acquired as spinlock, interlock acquired and preemption disabled */
350 thread_t thread
= current_thread();
351 /* record owner of mutex */
352 ordered_store_mtx_owner(lock
, (uintptr_t)thread
);
356 thread
->mutex_count
++; /* lock statistic */
361 LOCKSTAT_RECORD(LS_LCK_MTX_TRY_SPIN_LOCK_ACQUIRE
, lock
, 0);
364 /* return with the interlock held and preemption disabled */
369 * Routine: lck_mtx_try_lock_spin
371 * Try to lock a mutex as spin lock for current thread.
372 * It tries the fast path first and
373 * falls through the slow path in case
376 * Interlock or mutex cannot be already held by current thread.
378 * In case the mutex is held (either as spin or mutex)
379 * the function will fail, it will acquire the mutex as spin lock
384 lck_mtx_try_lock_spin(
387 return lck_mtx_try_lock_spin_always(lock
);
391 * Routine: lck_mtx_unlock
393 * Unlocks a mutex held by current thread.
394 * It tries the fast path first, and falls
395 * through the slow path in case waiters need to
398 * Interlock can be held, and the slow path will
399 * unlock the mutex for this case.
401 __attribute__((noinline
))
406 uint32_t prev
, state
;
408 state
= ordered_load_mtx_state(lock
);
410 if (state
& LCK_MTX_SPIN_MSK
) {
411 return lck_mtx_unlock_slow(lock
);
415 * Only full mutex will go through the fast path
416 * (if the lock was acquired as a spinlock it will
417 * fall through the slow path).
418 * If there are waiters it will fall
419 * through the slow path.
420 * If it is indirect it will fall through the slow path.
425 * interlock not held, no waiters, no promotion and mutex held.
427 prev
= state
& ~(LCK_MTX_ILOCKED_MSK
| LCK_MTX_WAITERS_MSK
);
428 prev
|= LCK_MTX_MLOCKED_MSK
;
430 state
= prev
| LCK_MTX_ILOCKED_MSK
;
431 state
&= ~LCK_MTX_MLOCKED_MSK
;
433 disable_preemption();
435 /* the memory order needs to be acquire because it is acquiring the interlock */
436 if (!os_atomic_cmpxchg(&lock
->lck_mtx_state
, prev
, state
, acquire
)) {
438 return lck_mtx_unlock_slow(lock
);
441 /* mutex released, interlock acquired and preemption disabled */
443 #if DEVELOPMENT | DEBUG
444 thread_t owner
= (thread_t
)lock
->lck_mtx_owner
;
445 if (__improbable(owner
!= current_thread())) {
446 lck_mtx_owner_check_panic(lock
);
451 ordered_store_mtx_owner(lock
, 0);
452 /* release interlock */
453 state
&= ~LCK_MTX_ILOCKED_MSK
;
454 ordered_store_mtx_state_release(lock
, state
);
457 thread_t thread
= current_thread();
459 thread
->mutex_count
--;
461 #endif /* MACH_LDEBUG */
463 /* re-enable preemption */
464 lck_mtx_unlock_finish_inline(lock
, FALSE
);