2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright 1999, 2000 John D. Polstra.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
27 * from: FreeBSD: src/libexec/rtld-elf/sparc64/lockdflt.c,v 1.3 2002/10/09
32 * Thread locking implementation for the dynamic linker.
34 * We use the "simple, non-scalable reader-preference lock" from:
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.
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.
47 #include <sys/param.h>
48 #include <sys/signalvar.h>
55 #include "rtld_machdep.h"
56 #include "rtld_libc.h"
58 void _rtld_thread_init(struct RtldLockInfo *) __exported;
59 void _rtld_atfork_pre(int *) __exported;
60 void _rtld_atfork_post(int *) __exported;
62 static char def_dlerror_msg[512];
63 static int def_dlerror_seen_val = 1;
68 return (def_dlerror_msg);
72 def_dlerror_seen(void)
74 return (&def_dlerror_seen_val);
77 #define WAFLAG 0x1 /* A writer holds the lock */
78 #define RC_INCR 0x2 /* Adjusts count of readers desiring lock */
80 typedef struct Struct_Lock {
85 static sigset_t fullsigmask, oldsigmask;
86 static int thread_flag, wnested;
87 static uint32_t fsigblock;
98 * Arrange for the lock to occupy its own cache line. First, we
99 * optimistically allocate just a cache line, hoping that malloc
100 * will give us a well-aligned block of memory. If that doesn't
101 * work, we allocate a larger block and take a well-aligned cache
104 base = xmalloc(CACHE_LINE_SIZE);
106 if ((uintptr_t)p % CACHE_LINE_SIZE != 0) {
108 base = xmalloc(2 * CACHE_LINE_SIZE);
110 if ((r = (uintptr_t)p % CACHE_LINE_SIZE) != 0)
111 p += CACHE_LINE_SIZE - r;
120 def_lock_destroy(void *lock)
122 Lock *l = (Lock *)lock;
128 def_rlock_acquire(void *lock)
130 Lock *l = (Lock *)lock;
132 atomic_add_acq_int(&l->lock, RC_INCR);
133 while (l->lock & WAFLAG)
138 sig_fastunblock(void)
142 assert((fsigblock & ~SIGFASTBLOCK_FLAGS) >= SIGFASTBLOCK_INC);
143 oldval = atomic_fetchadd_32(&fsigblock, -SIGFASTBLOCK_INC);
144 if (oldval == (SIGFASTBLOCK_PEND | SIGFASTBLOCK_INC))
145 __sys_sigfastblock(SIGFASTBLOCK_UNBLOCK, NULL);
149 def_wlock_acquire(void *lock)
152 sigset_t tmp_oldsigmask;
155 if (ld_fast_sigblock) {
157 atomic_add_32(&fsigblock, SIGFASTBLOCK_INC);
158 if (atomic_cmpset_acq_int(&l->lock, 0, WAFLAG))
164 sigprocmask(SIG_BLOCK, &fullsigmask, &tmp_oldsigmask);
165 if (atomic_cmpset_acq_int(&l->lock, 0, WAFLAG))
167 sigprocmask(SIG_SETMASK, &tmp_oldsigmask, NULL);
169 if (atomic_fetchadd_int(&wnested, 1) == 0)
170 oldsigmask = tmp_oldsigmask;
175 def_lock_release(void *lock)
180 if ((l->lock & WAFLAG) == 0)
181 atomic_add_rel_int(&l->lock, -RC_INCR);
183 atomic_add_rel_int(&l->lock, -WAFLAG);
184 if (ld_fast_sigblock)
186 else if (atomic_fetchadd_int(&wnested, -1) == 1)
187 sigprocmask(SIG_SETMASK, &oldsigmask, NULL);
192 def_thread_set_flag(int mask)
194 int old_val = thread_flag;
200 def_thread_clr_flag(int mask)
202 int old_val = thread_flag;
203 thread_flag &= ~mask;
208 * Public interface exposed to the rest of the dynamic linker.
210 struct RtldLockInfo lockinfo;
211 static struct RtldLockInfo deflockinfo;
214 thread_mask_set(int mask)
216 return lockinfo.thread_set_flag(mask);
220 thread_mask_clear(int mask)
222 lockinfo.thread_clr_flag(mask);
225 #define RTLD_LOCK_CNT 3
226 static struct rtld_lock {
229 } rtld_locks[RTLD_LOCK_CNT];
231 rtld_lock_t rtld_bind_lock = &rtld_locks[0];
232 rtld_lock_t rtld_libc_lock = &rtld_locks[1];
233 rtld_lock_t rtld_phdr_lock = &rtld_locks[2];
236 rlock_acquire(rtld_lock_t lock, RtldLockState *lockstate)
239 if (lockstate == NULL)
242 if (thread_mask_set(lock->mask) & lock->mask) {
243 dbg("rlock_acquire: recursed");
244 lockstate->lockstate = RTLD_LOCK_UNLOCKED;
247 lockinfo.rlock_acquire(lock->handle);
248 lockstate->lockstate = RTLD_LOCK_RLOCKED;
252 wlock_acquire(rtld_lock_t lock, RtldLockState *lockstate)
255 if (lockstate == NULL)
258 if (thread_mask_set(lock->mask) & lock->mask) {
259 dbg("wlock_acquire: recursed");
260 lockstate->lockstate = RTLD_LOCK_UNLOCKED;
263 lockinfo.wlock_acquire(lock->handle);
264 lockstate->lockstate = RTLD_LOCK_WLOCKED;
268 lock_release(rtld_lock_t lock, RtldLockState *lockstate)
271 if (lockstate == NULL)
274 switch (lockstate->lockstate) {
275 case RTLD_LOCK_UNLOCKED:
277 case RTLD_LOCK_RLOCKED:
278 case RTLD_LOCK_WLOCKED:
279 thread_mask_clear(lock->mask);
280 lockinfo.lock_release(lock->handle);
288 lock_upgrade(rtld_lock_t lock, RtldLockState *lockstate)
291 if (lockstate == NULL)
294 lock_release(lock, lockstate);
295 wlock_acquire(lock, lockstate);
299 lock_restart_for_upgrade(RtldLockState *lockstate)
302 if (lockstate == NULL)
305 switch (lockstate->lockstate) {
306 case RTLD_LOCK_UNLOCKED:
307 case RTLD_LOCK_WLOCKED:
309 case RTLD_LOCK_RLOCKED:
310 siglongjmp(lockstate->env, 1);
318 dlerror_dflt_init(void)
320 lockinfo.dlerror_loc = def_dlerror_loc;
321 lockinfo.dlerror_loc_sz = sizeof(def_dlerror_msg);
322 lockinfo.dlerror_seen = def_dlerror_seen;
330 deflockinfo.rtli_version = RTLI_VERSION;
331 deflockinfo.lock_create = def_lock_create;
332 deflockinfo.lock_destroy = def_lock_destroy;
333 deflockinfo.rlock_acquire = def_rlock_acquire;
334 deflockinfo.wlock_acquire = def_wlock_acquire;
335 deflockinfo.lock_release = def_lock_release;
336 deflockinfo.thread_set_flag = def_thread_set_flag;
337 deflockinfo.thread_clr_flag = def_thread_clr_flag;
338 deflockinfo.at_fork = NULL;
339 deflockinfo.dlerror_loc = def_dlerror_loc;
340 deflockinfo.dlerror_loc_sz = sizeof(def_dlerror_msg);
341 deflockinfo.dlerror_seen = def_dlerror_seen;
343 for (i = 0; i < RTLD_LOCK_CNT; i++) {
344 rtld_locks[i].mask = (1 << i);
345 rtld_locks[i].handle = NULL;
348 memcpy(&lockinfo, &deflockinfo, sizeof(lockinfo));
349 _rtld_thread_init(NULL);
350 if (ld_fast_sigblock) {
351 __sys_sigfastblock(SIGFASTBLOCK_SETPTR, &fsigblock);
354 * Construct a mask to block all signals. Note that
355 * blocked traps mean that the process is terminated
356 * if trap occurs while we are in locked section, with
357 * the default settings for kern.forcesigexit.
359 sigfillset(&fullsigmask);
364 * Callback function to allow threads implementation to
365 * register their own locking primitives if the default
366 * one is not suitable.
367 * The current context should be the only context
368 * executing at the invocation time.
371 _rtld_thread_init(struct RtldLockInfo *pli)
373 const Obj_Entry *obj;
375 void *locks[RTLD_LOCK_CNT];
379 lockinfo.rtli_version = RTLI_VERSION;
381 lockinfo.rtli_version = RTLI_VERSION_ONE;
382 obj = obj_from_addr(pli->lock_create);
384 symlook_init(&req, "_pli_rtli_version");
385 res = symlook_obj(&req, obj);
387 lockinfo.rtli_version = pli->rtli_version;
391 /* disable all locking while this function is running */
392 flags = thread_mask_set(~0);
396 else if (ld_fast_sigblock) {
398 __sys_sigfastblock(SIGFASTBLOCK_UNSETPTR, NULL);
401 for (i = 0; i < RTLD_LOCK_CNT; i++)
402 if ((locks[i] = pli->lock_create()) == NULL)
405 if (i < RTLD_LOCK_CNT) {
407 pli->lock_destroy(locks[i]);
411 for (i = 0; i < RTLD_LOCK_CNT; i++) {
412 if (rtld_locks[i].handle == NULL)
414 if (flags & rtld_locks[i].mask)
415 lockinfo.lock_release(rtld_locks[i].handle);
416 lockinfo.lock_destroy(rtld_locks[i].handle);
419 for (i = 0; i < RTLD_LOCK_CNT; i++) {
420 rtld_locks[i].handle = locks[i];
421 if (flags & rtld_locks[i].mask)
422 pli->wlock_acquire(rtld_locks[i].handle);
425 lockinfo.lock_create = pli->lock_create;
426 lockinfo.lock_destroy = pli->lock_destroy;
427 lockinfo.rlock_acquire = pli->rlock_acquire;
428 lockinfo.wlock_acquire = pli->wlock_acquire;
429 lockinfo.lock_release = pli->lock_release;
430 lockinfo.thread_set_flag = pli->thread_set_flag;
431 lockinfo.thread_clr_flag = pli->thread_clr_flag;
432 lockinfo.at_fork = pli->at_fork;
433 if (lockinfo.rtli_version > RTLI_VERSION_ONE && pli != NULL) {
434 strlcpy(pli->dlerror_loc(), lockinfo.dlerror_loc(),
435 lockinfo.dlerror_loc_sz);
436 lockinfo.dlerror_loc = pli->dlerror_loc;
437 lockinfo.dlerror_loc_sz = pli->dlerror_loc_sz;
438 lockinfo.dlerror_seen = pli->dlerror_seen;
441 /* restore thread locking state, this time with new locks */
442 thread_mask_clear(~0);
443 thread_mask_set(flags);
444 dbg("_rtld_thread_init: done");
448 _rtld_atfork_pre(int *locks)
456 * Warning: this did not worked well with the rtld compat
457 * locks above, when the thread signal mask was corrupted (set
458 * to all signals blocked) if two locks were taken
459 * simultaneously in the write mode. The caller of the
460 * _rtld_atfork_pre() must provide the working implementation
461 * of the locks anyway, and libthr locks are fine.
463 wlock_acquire(rtld_phdr_lock, &ls[0]);
464 wlock_acquire(rtld_bind_lock, &ls[1]);
466 /* XXXKIB: I am really sorry for this. */
467 locks[0] = ls[1].lockstate;
468 locks[2] = ls[0].lockstate;
472 _rtld_atfork_post(int *locks)
479 bzero(ls, sizeof(ls));
480 ls[0].lockstate = locks[2];
481 ls[1].lockstate = locks[0];
482 lock_release(rtld_bind_lock, &ls[1]);
483 lock_release(rtld_phdr_lock, &ls[0]);