2 * Copyright (c) 2003-2009 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 #include <sys/appleapiopts.h>
30 #include <machine/cpu_capabilities.h>
31 #include <machine/commpage.h>
32 #include <mach/i386/syscall_sw.h>
34 #define _PTHREAD_TSD_OFFSET32 0x48
35 #define _PTHREAD_TSD_OFFSET64 0x60
38 /* These routines do not need to be on the copmmpage on Intel. They are for now
39 * to avoid revlock, but the code should move to Libc, and we should eventually remove
42 COMMPAGE_FUNCTION_START(pthread_getspecific, 32, 4)
44 movl %gs:_PTHREAD_TSD_OFFSET32(,%eax,4), %eax
46 COMMPAGE_DESCRIPTOR(pthread_getspecific,_COMM_PAGE_PTHREAD_GETSPECIFIC,0,0)
48 COMMPAGE_FUNCTION_START(pthread_self, 32, 4)
49 movl %gs:_PTHREAD_TSD_OFFSET32, %eax
51 COMMPAGE_DESCRIPTOR(pthread_self,_COMM_PAGE_PTHREAD_SELF,0,0)
53 /* the 64-bit versions: */
54 COMMPAGE_FUNCTION_START(pthread_getspecific_64, 64, 4)
55 movq %gs:_PTHREAD_TSD_OFFSET64(,%rdi,8), %rax
57 COMMPAGE_DESCRIPTOR(pthread_getspecific_64,_COMM_PAGE_PTHREAD_GETSPECIFIC,0,0)
59 COMMPAGE_FUNCTION_START(pthread_self_64, 64, 4)
60 movq %gs:_PTHREAD_TSD_OFFSET64, %rax
62 COMMPAGE_DESCRIPTOR(pthread_self_64,_COMM_PAGE_PTHREAD_SELF,0,0)
65 /* Temporary definitions. Replace by #including the correct file when available. */
67 #define PTHRW_EBIT 0x01
68 #define PTHRW_LBIT 0x02
69 #define PTHRW_YBIT 0x04
70 #define PTHRW_WBIT 0x08
71 #define PTHRW_UBIT 0x10
72 #define PTHRW_RETRYBIT 0x20
73 #define PTHRW_TRYLKBIT 0x40
75 #define PTHRW_INC 0x100
76 #define PTHRW_BIT_MASK 0x000000ff;
78 #define PTHRW_COUNT_SHIFT 8
79 #define PTHRW_COUNT_MASK 0xffffff00
80 #define PTHRW_MAX_READERS 0xffffff00
82 #define KSYN_MLWAIT 301 /* mutex lock wait syscall */
84 #define PTHRW_STATUS_ACQUIRED 0
85 #define PTHRW_STATUS_SYSCALL 1
86 #define PTHRW_STATUS_ERROR 2
93 /* PREEMPTION FREE ZONE (PFZ)
95 * A portion of the commpage is speacial-cased by the kernel to be "preemption free",
96 * ie as if we had disabled interrupts in user mode. This facilitates writing
97 * "nearly-lockless" code, for example code that must be serialized by a spinlock but
98 * which we do not want to preempt while the spinlock is held.
100 * The PFZ is implemented by collecting all the "preemption-free" code into a single
101 * contiguous region of the commpage. Register %ebx is used as a flag register;
102 * before entering the PFZ, %ebx is cleared. If some event occurs that would normally
103 * result in a premption while in the PFZ, the kernel sets %ebx nonzero instead of
104 * preempting. Then, when the routine leaves the PFZ we check %ebx and
105 * if nonzero execute a special "pfz_exit" syscall to take the delayed preemption.
107 * PFZ code must bound the amount of time spent in the PFZ, in order to control
108 * latency. Backward branches are dangerous and must not be used in a way that
109 * could inadvertently create a long-running loop.
111 * Because we need to avoid being preempted between changing the mutex stateword
112 * and entering the kernel to relinquish, some low-level pthread mutex manipulations
113 * are located in the PFZ.
117 /* int // we return 0 on acquire, 1 on syscall
118 * pthread_mutex_lock( uint32_t *lvalp, // ptr to mutex LVAL/UVAL pair
119 * int flags, // flags to pass kernel if we do syscall
120 * uint64_t mtid, // my Thread ID
121 * uint32_t mask, // bits to test in LVAL (ie, EBIT etc)
122 * uint64_t *tidp, // ptr to TID field of mutex
123 * int *syscall_return ); // if syscall, return value stored here
125 COMMPAGE_FUNCTION_START(pthread_mutex_lock, 32, 4)
126 pushl %ebp // set up frame for backtrace
131 xorl %ebx,%ebx // clear "preemption pending" flag
132 movl 20(%esp),%edi // %edi == ptr to LVAL/UVAL structure
133 lea 20(%esp),%esi // %esi == ptr to argument list
134 movl _COMM_PAGE_SPIN_COUNT, %edx
135 movl 16(%esi),%ecx // get mask (ie, PTHRW_EBIT etc)
137 testl PTHRW_LVAL(%edi),%ecx // is mutex available?
138 jz 2f // yes, it is available
140 decl %edx // decrement max spin count
141 jnz 1b // keep spinning
143 COMMPAGE_CALL(_COMM_PAGE_PFZ_MUTEX_LOCK,_COMM_PAGE_MUTEX_LOCK,pthread_mutex_lock)
144 testl %ebx,%ebx // pending preemption?
146 pushl %eax // save return value across sysenter
147 COMMPAGE_CALL(_COMM_PAGE_PREEMPT,_COMM_PAGE_MUTEX_LOCK,pthread_mutex_lock)
155 COMMPAGE_DESCRIPTOR(pthread_mutex_lock,_COMM_PAGE_MUTEX_LOCK,0,0)
158 /* Internal routine to handle pthread mutex lock operation. This is in the PFZ.
159 * %edi == ptr to LVAL/UVAL pair
160 * %esi == ptr to argument list on stack
161 * %ebx == preempion pending flag (kernel sets nonzero if we should preempt)
163 COMMPAGE_FUNCTION_START(pfz_mutex_lock, 32, 4)
164 pushl %ebp // set up frame for backtrace
167 movl 16(%esi),%ecx // get mask (ie, PTHRW_EBIT etc)
169 movl PTHRW_LVAL(%edi),%eax // get mutex LVAL
170 testl %eax,%ecx // is mutex available?
173 /* lock is available (if we act fast) */
174 lea PTHRW_INC(%eax),%edx // copy original lval and bump sequence count
175 orl $PTHRW_EBIT, %edx // set EBIT
177 cmpxchgl %edx,PTHRW_LVAL(%edi) // try to acquire lock for real
180 testl %ebx,%ebx // kernel trying to preempt us?
181 jz 2b // no, so loop and try again
182 COMMPAGE_CALL(_COMM_PAGE_PREEMPT,_COMM_PAGE_PFZ_MUTEX_LOCK,pfz_mutex_lock)
183 jmp 1b // loop to try again
185 /* we acquired the mutex */
187 movl 20(%esi),%eax // get ptr to TID field of mutex
188 movl 8(%esi),%ecx // get 64-bit mtid
190 movl %ecx,0(%eax) // store my TID in mutex structure
192 movl $PTHRW_STATUS_ACQUIRED,%eax
196 /* cannot acquire mutex, so update seq count, set "W", and block in kernel */
197 /* this is where we cannot tolerate preemption or being killed */
199 lea PTHRW_INC(%eax),%edx // copy original lval and bump sequence count
200 orl $PTHRW_WBIT, %edx // set WBIT
202 cmpxchgl %edx,PTHRW_LVAL(%edi) // try to update lock status atomically
204 movl 20(%esi),%eax // get ptr to TID field of mutex
205 pushl 4(%esi) // arg 5: flags from arg list
206 pushl 4(%eax) // arg 4: tid field from mutex
208 pushl PTHRW_UVAL(%edi) // arg 3: uval field from mutex
209 pushl %edx // arg 2: new value of mutex lval field
210 pushl %edi // arg 1: ptr to LVAL/UVAL pair in mutex
211 call 6f // make ksyn_mlwait call
212 jc 6f // immediately reissue syscall if error
213 movl 24(%esi),%edx // get ptr to syscall_return arg
214 movl %eax,(%edx) // save syscall return value
215 movl $PTHRW_STATUS_SYSCALL,%eax // we had to make syscall
216 addl $28,%esp // pop off syscall args and return address
217 popl %ebp // pop off frame ptr
220 /* subroutine to make a ksyn_mlwait syscall */
222 movl (%esp),%edx // get return address but leave on stack
223 movl %esp,%ecx // save stack ptr here
224 movl $KSYN_MLWAIT,%eax // get syscall code
225 orl $0x00180000,%eax // copy 24 bytes of arguments in trampoline
226 xorl %ebx,%ebx // clear preemption flag
228 COMMPAGE_DESCRIPTOR(pfz_mutex_lock,_COMM_PAGE_PFZ_MUTEX_LOCK,0,0)
232 /************************* x86_64 versions follow **************************/
236 /* int // we return 0 on acquire, 1 on syscall
237 * pthread_mutex_lock( uint32_t *lvalp, // ptr to mutex LVAL/UVAL pair
238 * int flags, // flags to pass kernel if we do syscall
239 * uint64_t mtid, // my Thread ID
240 * uint32_t mask, // bits to test in LVAL (ie, EBIT etc)
241 * uint64_t *tidp, // ptr to TID field of mutex
242 * int *syscall_return ); // if syscall, return value stored here
249 * %r9 = &syscall_return
251 COMMPAGE_FUNCTION_START(pthread_mutex_lock_64, 64, 4)
252 pushq %rbp // set up frame for backtrace
255 xorl %ebx,%ebx // clear "preemption pending" flag
256 movl _COMM_PAGE_32_TO_64(_COMM_PAGE_SPIN_COUNT), %eax
258 testl PTHRW_LVAL(%rdi),%ecx // is mutex available?
259 jz 2f // yes, it is available
261 decl %eax // decrement max spin count
262 jnz 1b // keep spinning
264 COMMPAGE_CALL(_COMM_PAGE_PFZ_MUTEX_LOCK,_COMM_PAGE_MUTEX_LOCK,pthread_mutex_lock_64)
265 testl %ebx,%ebx // pending preemption?
267 COMMPAGE_CALL(_COMM_PAGE_PREEMPT,_COMM_PAGE_MUTEX_LOCK,pthread_mutex_lock_64)
272 COMMPAGE_DESCRIPTOR(pthread_mutex_lock_64,_COMM_PAGE_MUTEX_LOCK,0,0)
275 /* Internal routine to handle pthread mutex lock operation. This is in the PFZ.
281 * %r9 = &syscall_return
282 * %ebx = preempion pending flag (kernel sets nonzero if we should preempt)
284 COMMPAGE_FUNCTION_START(pfz_mutex_lock_64, 64, 4)
285 pushq %rbp // set up frame for backtrace
288 movl PTHRW_LVAL(%rdi),%eax // get old lval from mutex
290 testl %eax,%ecx // can we acquire the lock?
293 /* lock is available (if we act fast) */
294 lea PTHRW_INC(%rax),%r11 // copy original lval and bump sequence count
295 orl $PTHRW_EBIT, %r11d // set EBIT
297 cmpxchgl %r11d,PTHRW_LVAL(%rdi) // try to acquire lock
300 testl %ebx,%ebx // kernel trying to preempt us?
301 jz 2b // no, so loop and try again
302 COMMPAGE_CALL(_COMM_PAGE_PREEMPT,_COMM_PAGE_PFZ_MUTEX_LOCK,pfz_mutex_lock_64)
303 jmp 1b // loop to try again
305 /* we acquired the mutex */
307 movq %rdx,(%r8) // store mtid in mutex structure
308 movl $PTHRW_STATUS_ACQUIRED,%eax
312 /* cannot acquire mutex, so update seq count and block in kernel */
313 /* this is where we cannot tolerate preemption or being killed */
315 lea PTHRW_INC(%rax),%r11 // copy original lval and bump sequence count
316 orl $PTHRW_WBIT, %r11d // set WBIT
318 cmpxchgl %r11d,PTHRW_LVAL(%rdi) // try to update lock status atomically
320 movq (%r8),%r10 // arg 4: tid field from mutex [NB: passed in R10]
321 movl %esi,%r8d // arg 5: flags from arg list
322 movl PTHRW_UVAL(%rdi),%edx // arg 3: uval field from mutex
323 movl %r11d,%esi // arg 2: new value of mutex lval field
324 // arg 1: LVAL/UVAL ptr already in %rdi
326 movl $(SYSCALL_CONSTRUCT_UNIX(KSYN_MLWAIT)),%eax
327 pushq %rdx // some syscalls destroy %rdx so save it
328 xorl %ebx,%ebx // clear preemption flag
330 popq %rdx // restore in case we need to re-execute syscall
331 jc 6b // immediately re-execute syscall if error
332 movl %eax,(%r9) // store kernel return value
333 movl $PTHRW_STATUS_SYSCALL,%eax // we made syscall
336 COMMPAGE_DESCRIPTOR(pfz_mutex_lock_64,_COMM_PAGE_PFZ_MUTEX_LOCK,0,0)