2 * Copyright (c) 2004-2012 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 #ifndef _I386_LOCKS_H_
30 #define _I386_LOCKS_H_
32 #include <sys/appleapiopts.h>
33 #include <kern/kern_types.h>
34 #include <kern/assert.h>
36 #ifdef MACH_KERNEL_PRIVATE
38 #include <i386/hw_lock_types.h>
40 #define enaLkDeb 0x00000001 /* Request debug in default attribute */
41 #define enaLkStat 0x00000002 /* Request statistic in default attribute */
42 #define disLkRWPrio 0x00000004 /* Disable RW lock priority promotion */
43 #define enaLkTimeStat 0x00000008 /* Request time statistics in default attribute */
45 #endif /* MACH_KERNEL_PRIVATE */
47 #if defined(MACH_KERNEL_PRIVATE)
49 volatile uintptr_t interlock
;
51 unsigned long lck_spin_pad
[9]; /* XXX - usimple_lock_data_t */
55 #define LCK_SPIN_TAG_DESTROYED 0x00002007 /* lock marked as Destroyed */
57 #else /* MACH_KERNEL_PRIVATE */
60 unsigned long opaque
[10];
62 #else /* KERNEL_PRIVATE */
63 typedef struct __lck_spin_t__ lck_spin_t
;
67 #ifdef MACH_KERNEL_PRIVATE
68 /* The definition of this structure, including the layout of the
69 * state bitfield, is tailored to the asm implementation in i386_lock.s
71 typedef struct _lck_mtx_
{
74 volatile uintptr_t lck_mtx_owner
;
79 lck_mtx_pri
:8, // unused
82 lck_mtx_promoted
:1, // unused
87 uint32_t lck_mtx_state
;
89 /* Pad field used as a canary, initialized to ~0 */
90 uint32_t lck_mtx_pad32
;
93 struct _lck_mtx_ext_
*lck_mtx_ptr
;
95 uint32_t lck_mtx_pad32_2
;
100 #define LCK_MTX_WAITERS_MSK 0x0000ffff
101 #define LCK_MTX_WAITER 0x00000001
102 #define LCK_MTX_PRIORITY_MSK 0x00ff0000
103 #define LCK_MTX_ILOCKED_MSK 0x01000000
104 #define LCK_MTX_MLOCKED_MSK 0x02000000
105 #define LCK_MTX_SPIN_MSK 0x08000000
107 /* This pattern must subsume the interlocked, mlocked and spin bits */
108 #define LCK_MTX_TAG_INDIRECT 0x07ff1007 /* lock marked as Indirect */
109 #define LCK_MTX_TAG_DESTROYED 0x07fe2007 /* lock marked as Destroyed */
111 /* Adaptive spin before blocking */
112 extern uint64_t MutexSpin
;
113 extern uint64_t low_MutexSpin
;
114 extern int64_t high_MutexSpin
;
116 typedef enum lck_mtx_spinwait_ret_type
{
117 LCK_MTX_SPINWAIT_ACQUIRED
= 0,
119 LCK_MTX_SPINWAIT_SPUN_HIGH_THR
= 1,
120 LCK_MTX_SPINWAIT_SPUN_OWNER_NOT_CORE
= 2,
121 LCK_MTX_SPINWAIT_SPUN_NO_WINDOW_CONTENTION
= 3,
122 LCK_MTX_SPINWAIT_SPUN_SLIDING_THR
= 4,
124 LCK_MTX_SPINWAIT_NO_SPIN
= 5,
125 } lck_mtx_spinwait_ret_type_t
;
127 extern lck_mtx_spinwait_ret_type_t
lck_mtx_lock_spinwait_x86(lck_mtx_t
*mutex
);
129 extern void lck_mtx_lock_wait_x86(lck_mtx_t
*mutex
, struct turnstile
**ts
);
130 extern void lck_mtx_lock_acquire_x86(lck_mtx_t
*mutex
);
132 extern void lck_mtx_lock_slow(lck_mtx_t
*lock
);
133 extern boolean_t
lck_mtx_try_lock_slow(lck_mtx_t
*lock
);
134 extern void lck_mtx_unlock_slow(lck_mtx_t
*lock
);
135 extern void lck_mtx_lock_spin_slow(lck_mtx_t
*lock
);
136 extern boolean_t
lck_mtx_try_lock_spin_slow(lck_mtx_t
*lock
);
137 extern void hw_lock_byte_init(volatile uint8_t *lock_byte
);
138 extern void hw_lock_byte_lock(volatile uint8_t *lock_byte
);
139 extern void hw_lock_byte_unlock(volatile uint8_t *lock_byte
);
148 #define MUTEX_TAG 0x4d4d
151 unsigned int lck_mtx_stat_data
;
154 typedef struct _lck_mtx_ext_
{
156 struct _lck_grp_
*lck_mtx_grp
;
157 unsigned int lck_mtx_attr
;
158 unsigned int lck_mtx_pad1
;
159 lck_mtx_deb_t lck_mtx_deb
;
160 uint64_t lck_mtx_stat
;
161 unsigned int lck_mtx_pad2
[2];
164 #define LCK_MTX_ATTR_DEBUG 0x1
165 #define LCK_MTX_ATTR_DEBUGb 0
166 #define LCK_MTX_ATTR_STAT 0x2
167 #define LCK_MTX_ATTR_STATb 1
169 #define LCK_MTX_EVENT(lck) ((event_t)(((unsigned int*)(lck))+(sizeof(lck_mtx_t)-1)/sizeof(unsigned int)))
170 #define LCK_EVENT_TO_MUTEX(event) ((lck_mtx_t *)(uintptr_t)(((unsigned int *)(event)) - ((sizeof(lck_mtx_t)-1)/sizeof(unsigned int))))
172 #else /* MACH_KERNEL_PRIVATE */
173 #ifdef XNU_KERNEL_PRIVATE
175 unsigned long opaque
[2];
179 unsigned long opaque
[10];
182 #ifdef KERNEL_PRIVATE
184 unsigned long opaque
[2];
188 unsigned long opaque
[10];
192 typedef struct __lck_mtx_t__ lck_mtx_t
;
193 typedef struct __lck_mtx_ext_t__ lck_mtx_ext_t
;
198 #ifdef MACH_KERNEL_PRIVATE
199 typedef union _lck_rw_t_internal_
{
201 volatile uint16_t lck_rw_shared_count
; /* No. of accepted readers */
202 volatile uint8_t lck_rw_interlock
; /* Interlock byte */
204 lck_rw_priv_excl
:1, /* Writers prioritized if set */
205 lck_rw_want_upgrade
:1, /* Read-to-write upgrade waiting */
206 lck_rw_want_write
:1, /* Writer waiting or locked for write */
207 lck_r_waiting
:1, /* Reader is sleeping on lock */
208 lck_w_waiting
:1, /* Writer is sleeping on lock */
209 lck_rw_can_sleep
:1, /* Can attempts to lock go to sleep? */
210 lck_rw_padb6
:2; /* padding */
211 uint32_t lck_rw_tag
; /* This can be obsoleted when stats are in */
212 thread_t lck_rw_owner
; /* Unused */
215 uint32_t data
; /* Single word for count, ilk, and bitfields */
216 uint32_t lck_rw_pad4
;
217 uint32_t lck_rw_pad8
;
218 uint32_t lck_rw_pad12
;
221 #define LCK_RW_T_SIZE 16
223 static_assert(sizeof(lck_rw_t
) == LCK_RW_T_SIZE
);
225 #define LCK_RW_SHARED_SHIFT 0
226 #define LCK_RW_INTERLOCK_BIT 16
227 #define LCK_RW_PRIV_EXCL_BIT 24
228 #define LCK_RW_WANT_UPGRADE_BIT 25
229 #define LCK_RW_WANT_EXCL_BIT 26
230 #define LCK_RW_R_WAITING_BIT 27
231 #define LCK_RW_W_WAITING_BIT 28
232 #define LCK_RW_CAN_SLEEP_BIT 29
234 #define LCK_RW_INTERLOCK (1 << LCK_RW_INTERLOCK_BIT)
235 #define LCK_RW_WANT_UPGRADE (1 << LCK_RW_WANT_UPGRADE_BIT)
236 #define LCK_RW_WANT_EXCL (1 << LCK_RW_WANT_EXCL_BIT)
237 #define LCK_RW_R_WAITING (1 << LCK_RW_R_WAITING_BIT)
238 #define LCK_RW_W_WAITING (1 << LCK_RW_W_WAITING_BIT)
239 #define LCK_RW_PRIV_EXCL (1 << LCK_RW_PRIV_EXCL_BIT)
240 #define LCK_RW_TAG_VALID (1 << LCK_RW_TAG_VALID_BIT)
241 #define LCK_RW_SHARED_MASK (0xffff << LCK_RW_SHARED_SHIFT)
242 #define LCK_RW_SHARED_READER (1 << LCK_RW_SHARED_SHIFT)
244 #define LCK_RW_WANT_WRITE LCK_RW_WANT_EXCL
247 #define LCK_RW_ATTR_DEBUG 0x1
248 #define LCK_RW_ATTR_DEBUGb 0
249 #define LCK_RW_ATTR_STAT 0x2
250 #define LCK_RW_ATTR_STATb 1
251 #define LCK_RW_ATTR_READ_PRI 0x3
252 #define LCK_RW_ATTR_READ_PRIb 2
253 #define LCK_RW_ATTR_DIS_THREAD 0x40000000
254 #define LCK_RW_ATTR_DIS_THREADb 30
255 #define LCK_RW_ATTR_DIS_MYLOCK 0x10000000
256 #define LCK_RW_ATTR_DIS_MYLOCKb 28
258 #define LCK_RW_TAG_DESTROYED 0x00002007 /* lock marked as Destroyed */
260 #define RW_LOCK_READER_EVENT(x) ((event_t) (((unsigned char*) (x)) + (offsetof(lck_rw_t, lck_rw_tag))))
261 #define RW_LOCK_WRITER_EVENT(x) ((event_t) (((unsigned char*) (x)) + (offsetof(lck_rw_t, lck_rw_pad8))))
262 #define READ_EVENT_TO_RWLOCK(x) ((lck_rw_t *)(((unsigned char*)(x) - (offsetof(lck_rw_t, lck_rw_tag)))))
263 #define WRITE_EVENT_TO_RWLOCK(x) ((lck_rw_t *)(((unsigned char*)(x) - (offsetof(lck_rw_t, lck_rw_pad8)))))
267 #define disable_preemption_for_thread(t) disable_preemption_internal()
268 #define preemption_disabled_for_thread(t) (get_preemption_level() > 0)
270 #define LCK_MTX_THREAD_TO_STATE(t) ((uintptr_t)t)
271 #define PLATFORM_LCK_ILOCK 0
273 #define LOCK_SNOOP_SPINS 1000
274 #define LOCK_PRETEST 1
276 /* hw_lock_lock static panic deadline, in timebase units. hw_lock_to() uses
277 * LockTimeoutTSC computed at startup
279 #define LOCK_PANIC_TIMEOUT 0xf000000 /* 251e6 TSC ticks */
281 #endif // LOCK_PRIVATE
284 #ifdef KERNEL_PRIVATE
292 typedef struct __lck_rw_t__ lck_rw_t
;
296 #ifdef MACH_KERNEL_PRIVATE
298 extern void kernel_preempt_check(void);
300 #endif /* MACH_KERNEL_PRIVATE */
301 #endif /* _I386_LOCKS_H_ */