2 * Copyright (c) 2003-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
23 #include <sys/appleapiopts.h>
24 #include <machine/cpu_capabilities.h>
25 #include <machine/commpage.h>
26 #include <mach/i386/syscall_sw.h>
30 #define MP_SPIN_TRIES 1024
44 COMMPAGE_DESCRIPTOR(spin_lock_try_up,_COMM_PAGE_SPINLOCK_TRY,kUP,0)
58 COMMPAGE_DESCRIPTOR(spin_lock_try_mp,_COMM_PAGE_SPINLOCK_TRY,0,kUP)
67 jnz,pn 1f /* predict not taken */
70 /* failed to get lock so relinquish the processor immediately on UP */
72 pushl $1 /* SWITCH_OPTION_DEPRESS */
73 pushl $0 /* THREAD_NULL */
74 pushl $0 /* push dummy stack ret addr */
75 movl $-61,%eax /* SYSCALL_THREAD_SWITCH */
77 addl $16, %esp /* adjust stack*/
80 COMMPAGE_DESCRIPTOR(spin_lock_up,_COMM_PAGE_SPINLOCK_LOCK,kUP,0)
91 jnz,pn 1f /* predict not taken */
95 movl $(MP_SPIN_TRIES), %edx
99 jz,pt 0b /* favor success and slow down spin loop */
101 jnz,pn 2b /* slow down spin loop with a mispredict */
102 /* failed to get lock after spinning so relinquish */
104 pushl $1 /* SWITCH_OPTION_DEPRESS */
105 pushl $0 /* THREAD_NULL */
106 pushl $0 /* push dummy stack ret addr */
107 movl $-61,%eax /* SYSCALL_THREAD_SWITCH */
109 addl $16, %esp /* adjust stack*/
112 COMMPAGE_DESCRIPTOR(spin_lock_mp,_COMM_PAGE_SPINLOCK_LOCK,0,kUP)
121 COMMPAGE_DESCRIPTOR(spin_unlock,_COMM_PAGE_SPINLOCK_UNLOCK,0,0)
124 /* ============================ 64-bit versions follow ===================== */
131 Lspin_lock_try_up_64:
134 cmpxchgl %edx, (%rdi)
139 COMMPAGE_DESCRIPTOR(spin_lock_try_up_64,_COMM_PAGE_SPINLOCK_TRY,kUP,0)
143 Lspin_lock_try_mp_64:
147 cmpxchgl %edx, (%rdi)
152 COMMPAGE_DESCRIPTOR(spin_lock_try_mp_64,_COMM_PAGE_SPINLOCK_TRY,0,kUP)
162 jnz,pn 1f /* predict not taken */
165 /* failed to get lock so relinquish the processor immediately on UP */
166 xorl %edi,%edi /* THREAD_NULL */
167 movl $1,%esi /* SWITCH_OPTION_DEPRESS */
168 movl $1,%edx /* 1 ms */
169 movl $(SYSCALL_CONSTRUCT_MACH(61)),%eax /* 61 = thread_switch */
173 COMMPAGE_DESCRIPTOR(spin_lock_up_64,_COMM_PAGE_SPINLOCK_LOCK,kUP,0)
185 jnz,pn 1f /* predict not taken */
189 movl $(MP_SPIN_TRIES), %edx
190 2: /* spin for awhile before relinquish */
196 /* failed to get lock after spinning so relinquish */
197 xorl %edi,%edi /* THREAD_NULL */
198 movl $1,%esi /* SWITCH_OPTION_DEPRESS */
199 movl $1,%edx /* 1 ms */
200 movl $(SYSCALL_CONSTRUCT_MACH(61)),%eax /* 61 = thread_switch */
204 COMMPAGE_DESCRIPTOR(spin_lock_mp_64,_COMM_PAGE_SPINLOCK_LOCK,0,kUP)
212 COMMPAGE_DESCRIPTOR(spin_unlock_64,_COMM_PAGE_SPINLOCK_UNLOCK,0,0)