]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/locks.h
xnu-3789.60.24.tar.gz
[apple/xnu.git] / osfmk / i386 / locks.h
1 /*
2 * Copyright (c) 2004-2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #ifndef _I386_LOCKS_H_
30 #define _I386_LOCKS_H_
31
32 #include <sys/appleapiopts.h>
33 #include <kern/kern_types.h>
34
35 #ifdef MACH_KERNEL_PRIVATE
36
37 #include <i386/hw_lock_types.h>
38
39 extern unsigned int LcksOpts;
40
41 #define enaLkDeb 0x00000001 /* Request debug in default attribute */
42 #define enaLkStat 0x00000002 /* Request statistic in default attribute */
43 #define disLkRWPrio 0x00000004 /* Disable RW lock priority promotion */
44
45 #endif /* MACH_KERNEL_PRIVATE */
46
47 #if defined(MACH_KERNEL_PRIVATE)
48 typedef struct {
49 volatile uintptr_t interlock;
50 #if MACH_LDEBUG
51 unsigned long lck_spin_pad[9]; /* XXX - usimple_lock_data_t */
52 #endif
53 } lck_spin_t;
54
55 #define LCK_SPIN_TAG_DESTROYED 0x00002007 /* lock marked as Destroyed */
56
57 #else /* MACH_KERNEL_PRIVATE */
58 #ifdef KERNEL_PRIVATE
59 typedef struct {
60 unsigned long opaque[10];
61 } lck_spin_t;
62 #else /* KERNEL_PRIVATE */
63 typedef struct __lck_spin_t__ lck_spin_t;
64 #endif
65 #endif
66
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
70 */
71 typedef struct _lck_mtx_ {
72 union {
73 struct {
74 volatile uintptr_t lck_mtx_owner;
75 union {
76 struct {
77 volatile uint32_t
78 lck_mtx_waiters:16,
79 lck_mtx_pri:8,
80 lck_mtx_ilocked:1,
81 lck_mtx_mlocked:1,
82 lck_mtx_promoted:1,
83 lck_mtx_spin:1,
84 lck_mtx_is_ext:1,
85 lck_mtx_pad3:3;
86 };
87 uint32_t lck_mtx_state;
88 };
89 /* Pad field used as a canary, initialized to ~0 */
90 uint32_t lck_mtx_pad32;
91 };
92 struct {
93 struct _lck_mtx_ext_ *lck_mtx_ptr;
94 uint32_t lck_mtx_tag;
95 uint32_t lck_mtx_pad32_2;
96 };
97 };
98 } lck_mtx_t;
99
100 /* This pattern must subsume the interlocked, mlocked and spin bits */
101 #define LCK_MTX_TAG_INDIRECT 0x07ff1007 /* lock marked as Indirect */
102 #define LCK_MTX_TAG_DESTROYED 0x07fe2007 /* lock marked as Destroyed */
103
104 /* Adaptive spin before blocking */
105 extern uint64_t MutexSpin;
106 extern int lck_mtx_lock_spinwait_x86(lck_mtx_t *mutex);
107 extern void lck_mtx_lock_wait_x86(lck_mtx_t *mutex);
108 extern void lck_mtx_lock_acquire_x86(lck_mtx_t *mutex);
109 extern void lck_mtx_unlock_wakeup_x86(lck_mtx_t *mutex, int prior_lock_state);
110
111 extern void lck_mtx_lock_mark_destroyed(lck_mtx_t *mutex);
112 extern int lck_mtx_lock_grab_mutex(lck_mtx_t *mutex);
113
114 extern void hw_lock_byte_init(volatile uint8_t *lock_byte);
115 extern void hw_lock_byte_lock(volatile uint8_t *lock_byte);
116 extern void hw_lock_byte_unlock(volatile uint8_t *lock_byte);
117
118 typedef struct {
119 unsigned int type;
120 unsigned int pad4;
121 vm_offset_t pc;
122 vm_offset_t thread;
123 } lck_mtx_deb_t;
124
125 #define MUTEX_TAG 0x4d4d
126
127 typedef struct {
128 unsigned int lck_mtx_stat_data;
129 } lck_mtx_stat_t;
130
131 typedef struct _lck_mtx_ext_ {
132 lck_mtx_t lck_mtx;
133 struct _lck_grp_ *lck_mtx_grp;
134 unsigned int lck_mtx_attr;
135 unsigned int lck_mtx_pad1;
136 lck_mtx_deb_t lck_mtx_deb;
137 uint64_t lck_mtx_stat;
138 unsigned int lck_mtx_pad2[2];
139 } lck_mtx_ext_t;
140
141 #define LCK_MTX_ATTR_DEBUG 0x1
142 #define LCK_MTX_ATTR_DEBUGb 0
143 #define LCK_MTX_ATTR_STAT 0x2
144 #define LCK_MTX_ATTR_STATb 1
145
146 #define LCK_MTX_EVENT(lck) ((event_t)(((unsigned int*)(lck))+(sizeof(lck_mtx_t)-1)/sizeof(unsigned int)))
147 #define LCK_EVENT_TO_MUTEX(event) ((lck_mtx_t *)(uintptr_t)(((unsigned int *)(event)) - ((sizeof(lck_mtx_t)-1)/sizeof(unsigned int))))
148
149 #else /* MACH_KERNEL_PRIVATE */
150 #ifdef XNU_KERNEL_PRIVATE
151 typedef struct {
152 unsigned long opaque[2];
153 } lck_mtx_t;
154
155 typedef struct {
156 unsigned long opaque[10];
157 } lck_mtx_ext_t;
158 #else
159 #ifdef KERNEL_PRIVATE
160 typedef struct {
161 unsigned long opaque[2];
162 } lck_mtx_t;
163
164 typedef struct {
165 unsigned long opaque[10];
166 } lck_mtx_ext_t;
167
168 #else
169 typedef struct __lck_mtx_t__ lck_mtx_t;
170 typedef struct __lck_mtx_ext_t__ lck_mtx_ext_t;
171 #endif
172 #endif
173 #endif
174
175 #ifdef MACH_KERNEL_PRIVATE
176 #pragma pack(1) /* Make sure the structure stays as we defined it */
177 typedef struct _lck_rw_t_internal_ {
178 volatile uint16_t lck_rw_shared_count; /* No. of accepted readers */
179 volatile uint8_t lck_rw_interlock; /* Interlock byte */
180 volatile uint8_t
181 lck_rw_priv_excl:1, /* Writers prioritized if set */
182 lck_rw_want_upgrade:1, /* Read-to-write upgrade waiting */
183 lck_rw_want_write:1, /* Writer waiting or locked for write */
184 lck_r_waiting:1, /* Reader is sleeping on lock */
185 lck_w_waiting:1, /* Writer is sleeping on lock */
186 lck_rw_can_sleep:1, /* Can attempts to lock go to sleep? */
187 lck_rw_padb6:2; /* padding */
188
189 uint32_t lck_rw_tag; /* This can be obsoleted when stats
190 * are in
191 */
192 uint32_t lck_rw_pad8;
193 uint32_t lck_rw_pad12;
194 } lck_rw_t;
195 #pragma pack()
196
197 #define LCK_RW_ATTR_DEBUG 0x1
198 #define LCK_RW_ATTR_DEBUGb 0
199 #define LCK_RW_ATTR_STAT 0x2
200 #define LCK_RW_ATTR_STATb 1
201 #define LCK_RW_ATTR_READ_PRI 0x3
202 #define LCK_RW_ATTR_READ_PRIb 2
203 #define LCK_RW_ATTR_DIS_THREAD 0x40000000
204 #define LCK_RW_ATTR_DIS_THREADb 30
205 #define LCK_RW_ATTR_DIS_MYLOCK 0x10000000
206 #define LCK_RW_ATTR_DIS_MYLOCKb 28
207
208 #define LCK_RW_TAG_DESTROYED 0x00002007 /* lock marked as Destroyed */
209
210 #define RW_LOCK_READER_EVENT(x) ((event_t) (((unsigned char*) (x)) + (offsetof(lck_rw_t, lck_rw_tag))))
211 #define RW_LOCK_WRITER_EVENT(x) ((event_t) (((unsigned char*) (x)) + (offsetof(lck_rw_t, lck_rw_pad8))))
212 #define READ_EVENT_TO_RWLOCK(x) ((lck_rw_t *)(((unsigned char*)(x) - (offsetof(lck_rw_t, lck_rw_tag)))))
213 #define WRITE_EVENT_TO_RWLOCK(x) ((lck_rw_t *)(((unsigned char*)(x) - (offsetof(lck_rw_t, lck_rw_pad8)))))
214
215 #if LOCK_PRIVATE
216
217 #define disable_preemption_for_thread(t) ((cpu_data_t GS_RELATIVE *)0UL)->cpu_preemption_level++
218
219 #define LCK_MTX_THREAD_TO_STATE(t) ((uintptr_t)t)
220 #define PLATFORM_LCK_ILOCK 0
221
222 #define LOCK_SNOOP_SPINS 1000
223 #define LOCK_PRETEST 1
224
225 /* Spinlock panic deadline, in mach_absolute_time units (ns on i386) */
226 #define LOCK_PANIC_TIMEOUT 0xf00000 /* 250 ms (huge) */
227
228 #endif // LOCK_PRIVATE
229
230 #else
231 #ifdef KERNEL_PRIVATE
232 #pragma pack(1)
233 typedef struct {
234 uint32_t opaque[3];
235 uint32_t opaque4;
236 } lck_rw_t;
237 #pragma pack()
238 #else
239 typedef struct __lck_rw_t__ lck_rw_t;
240 #endif
241 #endif
242
243 #ifdef MACH_KERNEL_PRIVATE
244
245 extern void kernel_preempt_check (void);
246
247 #endif /* MACH_KERNEL_PRIVATE */
248
249 #endif /* _I386_LOCKS_H_ */