2 * Copyright (c) 2003 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 <ppc/asm.h> // EXT, LEXT
25 #include <machine/cpu_capabilities.h>
26 #include <machine/commpage.h>
30 .globl EXT(spinlock_32_try_mp)
31 .globl EXT(spinlock_32_try_up)
32 .globl EXT(spinlock_32_lock_mp)
33 .globl EXT(spinlock_32_lock_up)
34 .globl EXT(spinlock_32_unlock_mp)
35 .globl EXT(spinlock_32_unlock_up)
37 .globl EXT(spinlock_64_try_mp)
38 .globl EXT(spinlock_64_try_up)
39 .globl EXT(spinlock_64_lock_mp)
40 .globl EXT(spinlock_64_lock_up)
41 .globl EXT(spinlock_64_unlock_mp)
42 .globl EXT(spinlock_64_unlock_up)
44 .globl EXT(spinlock_relinquish)
46 #define MP_SPIN_TRIES 1000
49 // The user mode spinlock library. There are many versions,
50 // in order to take advantage of a few special cases:
51 // - no barrier instructions (SYNC,ISYNC) are needed if UP
52 // - 64-bit processors can use LWSYNC instead of SYNC (if MP)
53 // - branch hints appropriate to the processor (+ vs ++ etc)
54 // - potentially custom relinquish strategies (not used at present)
55 // - fixes for errata as necessary
66 isync // cancel speculative execution
73 COMMPAGE_DESCRIPTOR(spinlock_32_try_mp,_COMM_PAGE_SPINLOCK_TRY,0,k64Bit+kUP,0)
90 COMMPAGE_DESCRIPTOR(spinlock_32_try_up,_COMM_PAGE_SPINLOCK_TRY,kUP,k64Bit,0)
100 isync // cancel speculative execution
101 beqlr+ // we return void
104 subic. r5,r5,1 // try again before relinquish?
106 ba _COMM_PAGE_RELINQUISH
108 COMMPAGE_DESCRIPTOR(spinlock_32_lock_mp,_COMM_PAGE_SPINLOCK_LOCK,0,k64Bit+kUP,0)
115 bnea- _COMM_PAGE_RELINQUISH // always depress on UP (let lock owner run)
117 beqlr+ // we return void
120 COMMPAGE_DESCRIPTOR(spinlock_32_lock_up,_COMM_PAGE_SPINLOCK_LOCK,kUP,k64Bit,0)
123 spinlock_32_unlock_mp:
125 sync // complete prior stores before unlock
129 COMMPAGE_DESCRIPTOR(spinlock_32_unlock_mp,_COMM_PAGE_SPINLOCK_UNLOCK,0,k64Bit+kUP,0)
132 spinlock_32_unlock_up:
137 COMMPAGE_DESCRIPTOR(spinlock_32_unlock_up,_COMM_PAGE_SPINLOCK_UNLOCK,kUP,k64Bit,0)
148 isync // cancel speculative execution
153 stwcx. r5,r6,r1 // clear the pending reservation (using red zone)
154 li r3,0 // Pass failure
157 COMMPAGE_DESCRIPTOR(spinlock_64_try_mp,_COMM_PAGE_SPINLOCK_TRY,k64Bit,kUP,0)
172 stwcx. r5,r6,r1 // clear the pending reservation (using red zone)
176 COMMPAGE_DESCRIPTOR(spinlock_64_try_up,_COMM_PAGE_SPINLOCK_TRY,k64Bit+kUP,0,0)
186 isync // cancel speculative execution
187 beqlr++ // we return void
191 stwcx. r3,r6,r1 // clear the pending reservation (using red zone)
192 subic. r5,r5,1 // try again before relinquish?
193 bne-- 1b // mispredict this one (a cheap back-off)
194 ba _COMM_PAGE_RELINQUISH
196 COMMPAGE_DESCRIPTOR(spinlock_64_lock_mp,_COMM_PAGE_SPINLOCK_LOCK,k64Bit,kUP,0)
205 beqlr++ // we return void
207 2: // always relinquish on UP (let lock owner run)
209 stwcx. r3,r6,r1 // clear the pending reservation (using red zone)
210 ba _COMM_PAGE_RELINQUISH
212 COMMPAGE_DESCRIPTOR(spinlock_64_lock_up,_COMM_PAGE_SPINLOCK_LOCK,k64Bit+kUP,0,0)
215 spinlock_64_unlock_mp:
217 lwsync // complete prior stores before unlock
221 COMMPAGE_DESCRIPTOR(spinlock_64_unlock_mp,_COMM_PAGE_SPINLOCK_UNLOCK,k64Bit,kUP,0)
224 spinlock_64_unlock_up:
229 COMMPAGE_DESCRIPTOR(spinlock_64_unlock_up,_COMM_PAGE_SPINLOCK_UNLOCK,k64Bit+kUP,0,0)
233 mr r12,r3 // preserve lockword ptr across relinquish
234 li r3,0 // THREAD_NULL
235 li r4,1 // SWITCH_OPTION_DEPRESS
236 li r5,1 // timeout (ms)
237 li r0,-61 // SYSCALL_THREAD_SWITCH
240 ba _COMM_PAGE_SPINLOCK_LOCK
242 COMMPAGE_DESCRIPTOR(spinlock_relinquish,_COMM_PAGE_RELINQUISH,0,0,0)