1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright 1999, 2000 John D. Polstra. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 * from: FreeBSD: src/libexec/rtld-elf/sparc64/lockdflt.c,v 1.3 2002/10/09 28 * $FreeBSD$ 29 */ 30 31 /* 32 * Thread locking implementation for the dynamic linker. 33 * 34 * We use the "simple, non-scalable reader-preference lock" from: 35 * 36 * J. M. Mellor-Crummey and M. L. Scott. "Scalable Reader-Writer 37 * Synchronization for Shared-Memory Multiprocessors." 3rd ACM Symp. on 38 * Principles and Practice of Parallel Programming, April 1991. 39 * 40 * In this algorithm the lock is a single word. Its low-order bit is 41 * set when a writer holds the lock. The remaining high-order bits 42 * contain a count of readers desiring the lock. The algorithm requires 43 * atomic "compare_and_store" and "add" operations, which we take 44 * from machine/atomic.h. 45 */ 46 47 #include <sys/param.h> 48 #include <signal.h> 49 #include <stdlib.h> 50 #include <time.h> 51 52 #include "debug.h" 53 #include "rtld.h" 54 #include "rtld_machdep.h" 55 56 void _rtld_thread_init(struct RtldLockInfo *) __exported; 57 void _rtld_atfork_pre(int *) __exported; 58 void _rtld_atfork_post(int *) __exported; 59 60 #define WAFLAG 0x1 /* A writer holds the lock */ 61 #define RC_INCR 0x2 /* Adjusts count of readers desiring lock */ 62 63 typedef struct Struct_Lock { 64 volatile u_int lock; 65 void *base; 66 } Lock; 67 68 static sigset_t fullsigmask, oldsigmask; 69 static int thread_flag, wnested; 70 71 static void * 72 def_lock_create(void) 73 { 74 void *base; 75 char *p; 76 uintptr_t r; 77 Lock *l; 78 79 /* 80 * Arrange for the lock to occupy its own cache line. First, we 81 * optimistically allocate just a cache line, hoping that malloc 82 * will give us a well-aligned block of memory. If that doesn't 83 * work, we allocate a larger block and take a well-aligned cache 84 * line from it. 85 */ 86 base = xmalloc(CACHE_LINE_SIZE); 87 p = (char *)base; 88 if ((uintptr_t)p % CACHE_LINE_SIZE != 0) { 89 free(base); 90 base = xmalloc(2 * CACHE_LINE_SIZE); 91 p = (char *)base; 92 if ((r = (uintptr_t)p % CACHE_LINE_SIZE) != 0) 93 p += CACHE_LINE_SIZE - r; 94 } 95 l = (Lock *)p; 96 l->base = base; 97 l->lock = 0; 98 return l; 99 } 100 101 static void 102 def_lock_destroy(void *lock) 103 { 104 Lock *l = (Lock *)lock; 105 106 free(l->base); 107 } 108 109 static void 110 def_rlock_acquire(void *lock) 111 { 112 Lock *l = (Lock *)lock; 113 114 atomic_add_acq_int(&l->lock, RC_INCR); 115 while (l->lock & WAFLAG) 116 ; /* Spin */ 117 } 118 119 static void 120 def_wlock_acquire(void *lock) 121 { 122 Lock *l; 123 sigset_t tmp_oldsigmask; 124 125 l = (Lock *)lock; 126 for (;;) { 127 sigprocmask(SIG_BLOCK, &fullsigmask, &tmp_oldsigmask); 128 if (atomic_cmpset_acq_int(&l->lock, 0, WAFLAG)) 129 break; 130 sigprocmask(SIG_SETMASK, &tmp_oldsigmask, NULL); 131 } 132 if (atomic_fetchadd_int(&wnested, 1) == 0) 133 oldsigmask = tmp_oldsigmask; 134 } 135 136 static void 137 def_lock_release(void *lock) 138 { 139 Lock *l; 140 141 l = (Lock *)lock; 142 if ((l->lock & WAFLAG) == 0) 143 atomic_add_rel_int(&l->lock, -RC_INCR); 144 else { 145 assert(wnested > 0); 146 atomic_add_rel_int(&l->lock, -WAFLAG); 147 if (atomic_fetchadd_int(&wnested, -1) == 1) 148 sigprocmask(SIG_SETMASK, &oldsigmask, NULL); 149 } 150 } 151 152 static int 153 def_thread_set_flag(int mask) 154 { 155 int old_val = thread_flag; 156 thread_flag |= mask; 157 return (old_val); 158 } 159 160 static int 161 def_thread_clr_flag(int mask) 162 { 163 int old_val = thread_flag; 164 thread_flag &= ~mask; 165 return (old_val); 166 } 167 168 /* 169 * Public interface exposed to the rest of the dynamic linker. 170 */ 171 static struct RtldLockInfo lockinfo; 172 static struct RtldLockInfo deflockinfo; 173 174 static __inline int 175 thread_mask_set(int mask) 176 { 177 return lockinfo.thread_set_flag(mask); 178 } 179 180 static __inline void 181 thread_mask_clear(int mask) 182 { 183 lockinfo.thread_clr_flag(mask); 184 } 185 186 #define RTLD_LOCK_CNT 3 187 static struct rtld_lock { 188 void *handle; 189 int mask; 190 } rtld_locks[RTLD_LOCK_CNT]; 191 192 rtld_lock_t rtld_bind_lock = &rtld_locks[0]; 193 rtld_lock_t rtld_libc_lock = &rtld_locks[1]; 194 rtld_lock_t rtld_phdr_lock = &rtld_locks[2]; 195 196 void 197 rlock_acquire(rtld_lock_t lock, RtldLockState *lockstate) 198 { 199 200 if (lockstate == NULL) 201 return; 202 203 if (thread_mask_set(lock->mask) & lock->mask) { 204 dbg("rlock_acquire: recursed"); 205 lockstate->lockstate = RTLD_LOCK_UNLOCKED; 206 return; 207 } 208 lockinfo.rlock_acquire(lock->handle); 209 lockstate->lockstate = RTLD_LOCK_RLOCKED; 210 } 211 212 void 213 wlock_acquire(rtld_lock_t lock, RtldLockState *lockstate) 214 { 215 216 if (lockstate == NULL) 217 return; 218 219 if (thread_mask_set(lock->mask) & lock->mask) { 220 dbg("wlock_acquire: recursed"); 221 lockstate->lockstate = RTLD_LOCK_UNLOCKED; 222 return; 223 } 224 lockinfo.wlock_acquire(lock->handle); 225 lockstate->lockstate = RTLD_LOCK_WLOCKED; 226 } 227 228 void 229 lock_release(rtld_lock_t lock, RtldLockState *lockstate) 230 { 231 232 if (lockstate == NULL) 233 return; 234 235 switch (lockstate->lockstate) { 236 case RTLD_LOCK_UNLOCKED: 237 break; 238 case RTLD_LOCK_RLOCKED: 239 case RTLD_LOCK_WLOCKED: 240 thread_mask_clear(lock->mask); 241 lockinfo.lock_release(lock->handle); 242 break; 243 default: 244 assert(0); 245 } 246 } 247 248 void 249 lock_upgrade(rtld_lock_t lock, RtldLockState *lockstate) 250 { 251 252 if (lockstate == NULL) 253 return; 254 255 lock_release(lock, lockstate); 256 wlock_acquire(lock, lockstate); 257 } 258 259 void 260 lock_restart_for_upgrade(RtldLockState *lockstate) 261 { 262 263 if (lockstate == NULL) 264 return; 265 266 switch (lockstate->lockstate) { 267 case RTLD_LOCK_UNLOCKED: 268 case RTLD_LOCK_WLOCKED: 269 break; 270 case RTLD_LOCK_RLOCKED: 271 siglongjmp(lockstate->env, 1); 272 break; 273 default: 274 assert(0); 275 } 276 } 277 278 void 279 lockdflt_init(void) 280 { 281 int i; 282 283 deflockinfo.rtli_version = RTLI_VERSION; 284 deflockinfo.lock_create = def_lock_create; 285 deflockinfo.lock_destroy = def_lock_destroy; 286 deflockinfo.rlock_acquire = def_rlock_acquire; 287 deflockinfo.wlock_acquire = def_wlock_acquire; 288 deflockinfo.lock_release = def_lock_release; 289 deflockinfo.thread_set_flag = def_thread_set_flag; 290 deflockinfo.thread_clr_flag = def_thread_clr_flag; 291 deflockinfo.at_fork = NULL; 292 293 for (i = 0; i < RTLD_LOCK_CNT; i++) { 294 rtld_locks[i].mask = (1 << i); 295 rtld_locks[i].handle = NULL; 296 } 297 298 memcpy(&lockinfo, &deflockinfo, sizeof(lockinfo)); 299 _rtld_thread_init(NULL); 300 /* 301 * Construct a mask to block all signals except traps which might 302 * conceivably be generated within the dynamic linker itself. 303 */ 304 sigfillset(&fullsigmask); 305 sigdelset(&fullsigmask, SIGILL); 306 sigdelset(&fullsigmask, SIGTRAP); 307 sigdelset(&fullsigmask, SIGABRT); 308 sigdelset(&fullsigmask, SIGEMT); 309 sigdelset(&fullsigmask, SIGFPE); 310 sigdelset(&fullsigmask, SIGBUS); 311 sigdelset(&fullsigmask, SIGSEGV); 312 sigdelset(&fullsigmask, SIGSYS); 313 } 314 315 /* 316 * Callback function to allow threads implementation to 317 * register their own locking primitives if the default 318 * one is not suitable. 319 * The current context should be the only context 320 * executing at the invocation time. 321 */ 322 void 323 _rtld_thread_init(struct RtldLockInfo *pli) 324 { 325 int flags, i; 326 void *locks[RTLD_LOCK_CNT]; 327 328 /* disable all locking while this function is running */ 329 flags = thread_mask_set(~0); 330 331 if (pli == NULL) 332 pli = &deflockinfo; 333 334 335 for (i = 0; i < RTLD_LOCK_CNT; i++) 336 if ((locks[i] = pli->lock_create()) == NULL) 337 break; 338 339 if (i < RTLD_LOCK_CNT) { 340 while (--i >= 0) 341 pli->lock_destroy(locks[i]); 342 abort(); 343 } 344 345 for (i = 0; i < RTLD_LOCK_CNT; i++) { 346 if (rtld_locks[i].handle == NULL) 347 continue; 348 if (flags & rtld_locks[i].mask) 349 lockinfo.lock_release(rtld_locks[i].handle); 350 lockinfo.lock_destroy(rtld_locks[i].handle); 351 } 352 353 for (i = 0; i < RTLD_LOCK_CNT; i++) { 354 rtld_locks[i].handle = locks[i]; 355 if (flags & rtld_locks[i].mask) 356 pli->wlock_acquire(rtld_locks[i].handle); 357 } 358 359 lockinfo.lock_create = pli->lock_create; 360 lockinfo.lock_destroy = pli->lock_destroy; 361 lockinfo.rlock_acquire = pli->rlock_acquire; 362 lockinfo.wlock_acquire = pli->wlock_acquire; 363 lockinfo.lock_release = pli->lock_release; 364 lockinfo.thread_set_flag = pli->thread_set_flag; 365 lockinfo.thread_clr_flag = pli->thread_clr_flag; 366 lockinfo.at_fork = pli->at_fork; 367 368 /* restore thread locking state, this time with new locks */ 369 thread_mask_clear(~0); 370 thread_mask_set(flags); 371 dbg("_rtld_thread_init: done"); 372 } 373 374 void 375 _rtld_atfork_pre(int *locks) 376 { 377 RtldLockState ls[2]; 378 379 if (locks == NULL) 380 return; 381 382 /* 383 * Warning: this did not worked well with the rtld compat 384 * locks above, when the thread signal mask was corrupted (set 385 * to all signals blocked) if two locks were taken 386 * simultaneously in the write mode. The caller of the 387 * _rtld_atfork_pre() must provide the working implementation 388 * of the locks anyway, and libthr locks are fine. 389 */ 390 wlock_acquire(rtld_phdr_lock, &ls[0]); 391 wlock_acquire(rtld_bind_lock, &ls[1]); 392 393 /* XXXKIB: I am really sorry for this. */ 394 locks[0] = ls[1].lockstate; 395 locks[2] = ls[0].lockstate; 396 } 397 398 void 399 _rtld_atfork_post(int *locks) 400 { 401 RtldLockState ls[2]; 402 403 if (locks == NULL) 404 return; 405 406 bzero(ls, sizeof(ls)); 407 ls[0].lockstate = locks[2]; 408 ls[1].lockstate = locks[0]; 409 lock_release(rtld_bind_lock, &ls[1]); 410 lock_release(rtld_phdr_lock, &ls[0]); 411 } 412