Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | /* * Copyright (c) 2003 Apple Computer, Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * The contents of this file constitute Original Code as defined in and * are subject to the Apple Public Source License Version 1.1 (the * "License"). You may not use this file except in compliance with the * License. Please obtain a copy of the License at * http://www.apple.com/publicsource and read it before using this file. * * This Original Code and all software distributed under the License are * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the * License for the specific language governing rights and limitations * under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include <sys/appleapiopts.h> #include <ppc/asm.h> // EXT, LEXT #include <machine/cpu_capabilities.h> #include <machine/commpage.h> .text .align 2 #define MP_SPIN_TRIES 1000 /* The user mode spinlock library. There are many versions, * in order to take advantage of a few special cases: * - no barrier instructions (SYNC,ISYNC) are needed if UP * - 64-bit processors can use LWSYNC instead of SYNC (if MP) * - 32-bit processors can use ISYNC/EIEIO instead of SYNC (if MP) * - branch hints appropriate to the processor (+ vs ++ etc) * - potentially custom relinquish strategies (not used at present) * - fixes for errata as necessary * * The convention for lockwords is that 0==free and -1==locked. */ spinlock_32_try_mp: mr r5, r3 li r3, 1 1: lwarx r4,0,r5 li r6,-1 // locked == -1 cmpwi r4,0 bne- 2f stwcx. r6,0,r5 isync // cancel speculative execution beqlr+ b 1b 2: li r3,0 // we did not get the lock blr COMMPAGE_DESCRIPTOR(spinlock_32_try_mp,_COMM_PAGE_SPINLOCK_TRY,0,k64Bit+kUP,kCommPage32) spinlock_32_try_up: mr r5, r3 li r3, 1 1: lwarx r4,0,r5 li r6,-1 // locked == -1 cmpwi r4,0 bne- 2f stwcx. r6,0,r5 beqlr+ b 1b 2: li r3,0 // we did not get the lock blr COMMPAGE_DESCRIPTOR(spinlock_32_try_up,_COMM_PAGE_SPINLOCK_TRY,kUP,k64Bit,kCommPage32) spinlock_32_lock_mp: li r5,MP_SPIN_TRIES 1: lwarx r4,0,r3 li r6,-1 // locked == -1 cmpwi r4,0 bne- 2f stwcx. r6,0,r3 isync // cancel speculative execution beqlr+ // we return void b 1b 2: subic. r5,r5,1 // try again before relinquish? bne 1b ba _COMM_PAGE_RELINQUISH COMMPAGE_DESCRIPTOR(spinlock_32_lock_mp,_COMM_PAGE_SPINLOCK_LOCK,0,k64Bit+kUP,kCommPage32) spinlock_32_lock_up: 1: lwarx r4,0,r3 li r6,-1 // locked == -1 cmpwi r4,0 bnea- _COMM_PAGE_RELINQUISH // always depress on UP (let lock owner run) stwcx. r6,0,r3 beqlr+ // we return void b 1b COMMPAGE_DESCRIPTOR(spinlock_32_lock_up,_COMM_PAGE_SPINLOCK_LOCK,kUP,k64Bit,kCommPage32) spinlock_32_unlock_mp: li r4,0 isync // complete prior stores before unlock eieio // (using isync/eieio is faster than a sync) stw r4,0(r3) blr COMMPAGE_DESCRIPTOR(spinlock_32_unlock_mp,_COMM_PAGE_SPINLOCK_UNLOCK,0,k64Bit+kUP,kCommPage32) spinlock_32_unlock_up: li r4,0 stw r4,0(r3) blr COMMPAGE_DESCRIPTOR(spinlock_32_unlock_up,_COMM_PAGE_SPINLOCK_UNLOCK,kUP,k64Bit,kCommPage32) spinlock_64_try_mp: mr r5, r3 li r3, 1 1: lwarx r4,0,r5 li r6,-1 // locked == -1 cmpwi r4,0 bne-- 2f stwcx. r6,0,r5 isync // cancel speculative execution beqlr++ b 1b 2: li r6,-4 stwcx. r5,r6,r1 // clear the pending reservation (using red zone) li r3,0 // we did not get the lock blr COMMPAGE_DESCRIPTOR(spinlock_64_try_mp,_COMM_PAGE_SPINLOCK_TRY,k64Bit,kUP,kCommPageBoth) spinlock_64_try_up: mr r5, r3 li r3, 1 1: lwarx r4,0,r5 li r6,-1 // locked == -1 cmpwi r4,0 bne-- 2f stwcx. r6,0,r5 beqlr++ b 1b 2: li r6,-4 stwcx. r5,r6,r1 // clear the pending reservation (using red zone) li r3,0 // we did not get the lock blr COMMPAGE_DESCRIPTOR(spinlock_64_try_up,_COMM_PAGE_SPINLOCK_TRY,k64Bit+kUP,0,kCommPageBoth) spinlock_64_lock_mp: li r5,MP_SPIN_TRIES 1: lwarx r4,0,r3 li r6,-1 // locked == -1 cmpwi r4,0 bne-- 2f stwcx. r6,0,r3 isync // cancel speculative execution beqlr++ // we return void b 1b 2: li r6,-4 stwcx. r3,r6,r1 // clear the pending reservation (using red zone) subic. r5,r5,1 // try again before relinquish? bne-- 1b // mispredict this one (a cheap back-off) ba _COMM_PAGE_RELINQUISH COMMPAGE_DESCRIPTOR(spinlock_64_lock_mp,_COMM_PAGE_SPINLOCK_LOCK,k64Bit,kUP,kCommPageBoth) spinlock_64_lock_up: 1: lwarx r4,0,r3 li r6,-1 // locked == -1 cmpwi r4,0 bne-- 2f stwcx. r6,0,r3 beqlr++ // we return void b 1b 2: // always relinquish on UP (let lock owner run) li r6,-4 stwcx. r3,r6,r1 // clear the pending reservation (using red zone) ba _COMM_PAGE_RELINQUISH COMMPAGE_DESCRIPTOR(spinlock_64_lock_up,_COMM_PAGE_SPINLOCK_LOCK,k64Bit+kUP,0,kCommPageBoth) spinlock_64_unlock_mp: lwsync // complete prior stores before unlock li r4,0 stw r4,0(r3) blr COMMPAGE_DESCRIPTOR(spinlock_64_unlock_mp,_COMM_PAGE_SPINLOCK_UNLOCK,k64Bit,kUP,kCommPageBoth) spinlock_64_unlock_up: li r4,0 stw r4,0(r3) blr COMMPAGE_DESCRIPTOR(spinlock_64_unlock_up,_COMM_PAGE_SPINLOCK_UNLOCK,k64Bit+kUP,0,kCommPageBoth) spinlock_relinquish: mr r12,r3 // preserve lockword ptr across relinquish li r3,0 // THREAD_NULL li r4,1 // SWITCH_OPTION_DEPRESS li r5,1 // timeout (ms) li r0,-61 // SYSCALL_THREAD_SWITCH sc // relinquish mr r3,r12 ba _COMM_PAGE_SPINLOCK_LOCK COMMPAGE_DESCRIPTOR(spinlock_relinquish,_COMM_PAGE_RELINQUISH,0,0,kCommPageBoth) |