]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/lock.h
xnu-792.25.20.tar.gz
[apple/xnu.git] / osfmk / i386 / lock.h
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (C) 1998 Apple Computer
24 * All Rights Reserved
25 */
26 /*
27 * @OSF_COPYRIGHT@
28 */
29 /*
30 * Mach Operating System
31 * Copyright (c) 1991,1990 Carnegie Mellon University
32 * All Rights Reserved.
33 *
34 * Permission to use, copy, modify and distribute this software and its
35 * documentation is hereby granted, provided that both the copyright
36 * notice and this permission notice appear in all copies of the
37 * software, derivative works or modified versions, and any portions
38 * thereof, and that both notices appear in supporting documentation.
39 *
40 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
41 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
42 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 *
44 * Carnegie Mellon requests users of this software to return to
45 *
46 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
47 * School of Computer Science
48 * Carnegie Mellon University
49 * Pittsburgh PA 15213-3890
50 *
51 * any improvements or extensions that they make and grant Carnegie Mellon
52 * the rights to redistribute these changes.
53 */
54
55 /*
56 */
57
58 /*
59 * Machine-dependent simple locks for the i386.
60 */
61 #ifdef KERNEL_PRIVATE
62
63 #ifndef _I386_LOCK_H_
64 #define _I386_LOCK_H_
65
66 #include <sys/appleapiopts.h>
67
68 #ifdef __APPLE_API_PRIVATE
69
70 #ifdef MACH_KERNEL_PRIVATE
71
72 #include <kern/macro_help.h>
73 #include <kern/assert.h>
74 #include <i386/hw_lock_types.h>
75 #include <i386/locks.h>
76
77 #include <mach_rt.h>
78 #include <mach_ldebug.h>
79
80 typedef struct {
81 lck_mtx_t lck_mtx; /* inlined lck_mtx, need to be first */
82 #if MACH_LDEBUG
83 int type;
84 #define MUTEX_TAG 0x4d4d
85 vm_offset_t pc;
86 vm_offset_t thread;
87 #endif /* MACH_LDEBUG */
88 } mutex_t;
89
90 typedef lck_rw_t lock_t;
91
92 extern unsigned int LockTimeOut; /* Number of hardware ticks of a lock timeout */
93
94
95 #if defined(__GNUC__)
96
97 /*
98 * General bit-lock routines.
99 */
100
101 #define bit_lock(bit,l) \
102 __asm__ volatile(" jmp 1f \n \
103 0: btl %0, %1 \n \
104 jb 0b \n \
105 1: lock \n \
106 btsl %0,%1 \n \
107 jb 0b" : \
108 : \
109 "r" (bit), "m" (*(volatile int *)(l)) : \
110 "memory");
111
112 #define bit_unlock(bit,l) \
113 __asm__ volatile(" lock \n \
114 btrl %0,%1" : \
115 : \
116 "r" (bit), "m" (*(volatile int *)(l)));
117
118 /*
119 * Set or clear individual bits in a long word.
120 * The locked access is needed only to lock access
121 * to the word, not to individual bits.
122 */
123
124 #define i_bit_set(bit,l) \
125 __asm__ volatile(" lock \n \
126 btsl %0,%1" : \
127 : \
128 "r" (bit), "m" (*(volatile int *)(l)));
129
130 #define i_bit_clear(bit,l) \
131 __asm__ volatile(" lock \n \
132 btrl %0,%1" : \
133 : \
134 "r" (bit), "m" (*(volatile int *)(l)));
135
136 static inline unsigned long i_bit_isset(unsigned int test, volatile unsigned long *word)
137 {
138 int bit;
139
140 __asm__ volatile("btl %2,%1\n\tsbbl %0,%0" : "=r" (bit)
141 : "m" (word), "ir" (test));
142 return bit;
143 }
144
145 static inline char xchgb(volatile char * cp, char new);
146
147 static inline void atomic_incl(volatile long * p, long delta);
148 static inline void atomic_incs(volatile short * p, short delta);
149 static inline void atomic_incb(volatile char * p, char delta);
150
151 static inline void atomic_decl(volatile long * p, long delta);
152 static inline void atomic_decs(volatile short * p, short delta);
153 static inline void atomic_decb(volatile char * p, char delta);
154
155 static inline long atomic_getl(const volatile long * p);
156 static inline short atomic_gets(const volatile short * p);
157 static inline char atomic_getb(const volatile char * p);
158
159 static inline void atomic_setl(volatile long * p, long value);
160 static inline void atomic_sets(volatile short * p, short value);
161 static inline void atomic_setb(volatile char * p, char value);
162
163 static inline char xchgb(volatile char * cp, char new)
164 {
165 register char old = new;
166
167 __asm__ volatile (" xchgb %0,%2" :
168 "=q" (old) :
169 "0" (new), "m" (*(volatile char *)cp) : "memory");
170 return (old);
171 }
172
173 /*
174 * Compare and exchange:
175 * - returns failure (0) if the location did not contain the old value,
176 * - returns success (1) if the location was set to the new value.
177 */
178 static inline uint32_t
179 atomic_cmpxchg(uint32_t *p, uint32_t old, uint32_t new)
180 {
181 uint32_t res = old;
182
183 asm volatile(
184 "lock; cmpxchgl %1,%2; \n\t"
185 " setz %%al; \n\t"
186 " movzbl %%al,%0"
187 : "+a" (res) /* %0: old value to compare, returns success */
188 : "r" (new), /* %1: new value to set */
189 "m" (*(p)) /* %2: memory address */
190 : "memory");
191 return (res);
192 }
193
194 static inline void atomic_incl(volatile long * p, long delta)
195 {
196 __asm__ volatile (" lock \n \
197 addl %0,%1" : \
198 : \
199 "r" (delta), "m" (*(volatile long *)p));
200 }
201
202 static inline void atomic_incs(volatile short * p, short delta)
203 {
204 __asm__ volatile (" lock \n \
205 addw %0,%1" : \
206 : \
207 "q" (delta), "m" (*(volatile short *)p));
208 }
209
210 static inline void atomic_incb(volatile char * p, char delta)
211 {
212 __asm__ volatile (" lock \n \
213 addb %0,%1" : \
214 : \
215 "q" (delta), "m" (*(volatile char *)p));
216 }
217
218 static inline void atomic_decl(volatile long * p, long delta)
219 {
220 __asm__ volatile (" lock \n \
221 subl %0,%1" : \
222 : \
223 "r" (delta), "m" (*(volatile long *)p));
224 }
225
226 static inline int atomic_decl_and_test(volatile long * p, long delta)
227 {
228 uint8_t ret;
229 asm volatile (
230 " lock \n\t"
231 " subl %1,%2 \n\t"
232 " sete %0"
233 : "=qm" (ret)
234 : "r" (delta), "m" (*(volatile long *)p));
235 return ret;
236 }
237
238 static inline void atomic_decs(volatile short * p, short delta)
239 {
240 __asm__ volatile (" lock \n \
241 subw %0,%1" : \
242 : \
243 "q" (delta), "m" (*(volatile short *)p));
244 }
245
246 static inline void atomic_decb(volatile char * p, char delta)
247 {
248 __asm__ volatile (" lock \n \
249 subb %0,%1" : \
250 : \
251 "q" (delta), "m" (*(volatile char *)p));
252 }
253
254 static inline long atomic_getl(const volatile long * p)
255 {
256 return (*p);
257 }
258
259 static inline short atomic_gets(const volatile short * p)
260 {
261 return (*p);
262 }
263
264 static inline char atomic_getb(const volatile char * p)
265 {
266 return (*p);
267 }
268
269 static inline void atomic_setl(volatile long * p, long value)
270 {
271 *p = value;
272 }
273
274 static inline void atomic_sets(volatile short * p, short value)
275 {
276 *p = value;
277 }
278
279 static inline void atomic_setb(volatile char * p, char value)
280 {
281 *p = value;
282 }
283
284
285 #else /* !defined(__GNUC__) */
286
287 extern void i_bit_set(
288 int index,
289 void *addr);
290
291 extern void i_bit_clear(
292 int index,
293 void *addr);
294
295 extern void bit_lock(
296 int index,
297 void *addr);
298
299 extern void bit_unlock(
300 int index,
301 void *addr);
302
303 /*
304 * All other routines defined in __GNUC__ case lack
305 * definitions otherwise. - XXX
306 */
307
308 #endif /* !defined(__GNUC__) */
309
310 extern void kernel_preempt_check (void);
311
312 #endif /* MACH_KERNEL_PRIVATE */
313
314 #endif /* __APLE_API_PRIVATE */
315
316 #endif /* _I386_LOCK_H_ */
317
318 #endif /* KERNEL_PRIVATE */