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>
54 #include "rtld_machdep.h"
55 #include "rtld_libc.h"
57 void _rtld_thread_init(struct RtldLockInfo *) __exported;
58 void _rtld_atfork_pre(int *) __exported;
59 void _rtld_atfork_post(int *) __exported;
61 #define WAFLAG 0x1 /* A writer holds the lock */
62 #define RC_INCR 0x2 /* Adjusts count of readers desiring lock */
64 typedef struct Struct_Lock {
69 static sigset_t fullsigmask, oldsigmask;
70 static int thread_flag, wnested;
81 * Arrange for the lock to occupy its own cache line. First, we
82 * optimistically allocate just a cache line, hoping that malloc
83 * will give us a well-aligned block of memory. If that doesn't
84 * work, we allocate a larger block and take a well-aligned cache
87 base = xmalloc(CACHE_LINE_SIZE);
89 if ((uintptr_t)p % CACHE_LINE_SIZE != 0) {
91 base = xmalloc(2 * CACHE_LINE_SIZE);
93 if ((r = (uintptr_t)p % CACHE_LINE_SIZE) != 0)
94 p += CACHE_LINE_SIZE - r;
103 def_lock_destroy(void *lock)
105 Lock *l = (Lock *)lock;
111 def_rlock_acquire(void *lock)
113 Lock *l = (Lock *)lock;
115 atomic_add_acq_int(&l->lock, RC_INCR);
116 while (l->lock & WAFLAG)
121 def_wlock_acquire(void *lock)
124 sigset_t tmp_oldsigmask;
128 sigprocmask(SIG_BLOCK, &fullsigmask, &tmp_oldsigmask);
129 if (atomic_cmpset_acq_int(&l->lock, 0, WAFLAG))
131 sigprocmask(SIG_SETMASK, &tmp_oldsigmask, NULL);
133 if (atomic_fetchadd_int(&wnested, 1) == 0)
134 oldsigmask = tmp_oldsigmask;
138 def_lock_release(void *lock)
143 if ((l->lock & WAFLAG) == 0)
144 atomic_add_rel_int(&l->lock, -RC_INCR);
147 atomic_add_rel_int(&l->lock, -WAFLAG);
148 if (atomic_fetchadd_int(&wnested, -1) == 1)
149 sigprocmask(SIG_SETMASK, &oldsigmask, NULL);
154 def_thread_set_flag(int mask)
156 int old_val = thread_flag;
162 def_thread_clr_flag(int mask)
164 int old_val = thread_flag;
165 thread_flag &= ~mask;
170 * Public interface exposed to the rest of the dynamic linker.
172 static struct RtldLockInfo lockinfo;
173 static struct RtldLockInfo deflockinfo;
176 thread_mask_set(int mask)
178 return lockinfo.thread_set_flag(mask);
182 thread_mask_clear(int mask)
184 lockinfo.thread_clr_flag(mask);
187 #define RTLD_LOCK_CNT 3
188 static struct rtld_lock {
191 } rtld_locks[RTLD_LOCK_CNT];
193 rtld_lock_t rtld_bind_lock = &rtld_locks[0];
194 rtld_lock_t rtld_libc_lock = &rtld_locks[1];
195 rtld_lock_t rtld_phdr_lock = &rtld_locks[2];
198 rlock_acquire(rtld_lock_t lock, RtldLockState *lockstate)
201 if (lockstate == NULL)
204 if (thread_mask_set(lock->mask) & lock->mask) {
205 dbg("rlock_acquire: recursed");
206 lockstate->lockstate = RTLD_LOCK_UNLOCKED;
209 lockinfo.rlock_acquire(lock->handle);
210 lockstate->lockstate = RTLD_LOCK_RLOCKED;
214 wlock_acquire(rtld_lock_t lock, RtldLockState *lockstate)
217 if (lockstate == NULL)
220 if (thread_mask_set(lock->mask) & lock->mask) {
221 dbg("wlock_acquire: recursed");
222 lockstate->lockstate = RTLD_LOCK_UNLOCKED;
225 lockinfo.wlock_acquire(lock->handle);
226 lockstate->lockstate = RTLD_LOCK_WLOCKED;
230 lock_release(rtld_lock_t lock, RtldLockState *lockstate)
233 if (lockstate == NULL)
236 switch (lockstate->lockstate) {
237 case RTLD_LOCK_UNLOCKED:
239 case RTLD_LOCK_RLOCKED:
240 case RTLD_LOCK_WLOCKED:
241 thread_mask_clear(lock->mask);
242 lockinfo.lock_release(lock->handle);
250 lock_upgrade(rtld_lock_t lock, RtldLockState *lockstate)
253 if (lockstate == NULL)
256 lock_release(lock, lockstate);
257 wlock_acquire(lock, lockstate);
261 lock_restart_for_upgrade(RtldLockState *lockstate)
264 if (lockstate == NULL)
267 switch (lockstate->lockstate) {
268 case RTLD_LOCK_UNLOCKED:
269 case RTLD_LOCK_WLOCKED:
271 case RTLD_LOCK_RLOCKED:
272 siglongjmp(lockstate->env, 1);
284 deflockinfo.rtli_version = RTLI_VERSION;
285 deflockinfo.lock_create = def_lock_create;
286 deflockinfo.lock_destroy = def_lock_destroy;
287 deflockinfo.rlock_acquire = def_rlock_acquire;
288 deflockinfo.wlock_acquire = def_wlock_acquire;
289 deflockinfo.lock_release = def_lock_release;
290 deflockinfo.thread_set_flag = def_thread_set_flag;
291 deflockinfo.thread_clr_flag = def_thread_clr_flag;
292 deflockinfo.at_fork = NULL;
294 for (i = 0; i < RTLD_LOCK_CNT; i++) {
295 rtld_locks[i].mask = (1 << i);
296 rtld_locks[i].handle = NULL;
299 memcpy(&lockinfo, &deflockinfo, sizeof(lockinfo));
300 _rtld_thread_init(NULL);
302 * Construct a mask to block all signals except traps which might
303 * conceivably be generated within the dynamic linker itself.
305 sigfillset(&fullsigmask);
306 sigdelset(&fullsigmask, SIGILL);
307 sigdelset(&fullsigmask, SIGTRAP);
308 sigdelset(&fullsigmask, SIGABRT);
309 sigdelset(&fullsigmask, SIGEMT);
310 sigdelset(&fullsigmask, SIGFPE);
311 sigdelset(&fullsigmask, SIGBUS);
312 sigdelset(&fullsigmask, SIGSEGV);
313 sigdelset(&fullsigmask, SIGSYS);
317 * Callback function to allow threads implementation to
318 * register their own locking primitives if the default
319 * one is not suitable.
320 * The current context should be the only context
321 * executing at the invocation time.
324 _rtld_thread_init(struct RtldLockInfo *pli)
327 void *locks[RTLD_LOCK_CNT];
329 /* disable all locking while this function is running */
330 flags = thread_mask_set(~0);
336 for (i = 0; i < RTLD_LOCK_CNT; i++)
337 if ((locks[i] = pli->lock_create()) == NULL)
340 if (i < RTLD_LOCK_CNT) {
342 pli->lock_destroy(locks[i]);
346 for (i = 0; i < RTLD_LOCK_CNT; i++) {
347 if (rtld_locks[i].handle == NULL)
349 if (flags & rtld_locks[i].mask)
350 lockinfo.lock_release(rtld_locks[i].handle);
351 lockinfo.lock_destroy(rtld_locks[i].handle);
354 for (i = 0; i < RTLD_LOCK_CNT; i++) {
355 rtld_locks[i].handle = locks[i];
356 if (flags & rtld_locks[i].mask)
357 pli->wlock_acquire(rtld_locks[i].handle);
360 lockinfo.lock_create = pli->lock_create;
361 lockinfo.lock_destroy = pli->lock_destroy;
362 lockinfo.rlock_acquire = pli->rlock_acquire;
363 lockinfo.wlock_acquire = pli->wlock_acquire;
364 lockinfo.lock_release = pli->lock_release;
365 lockinfo.thread_set_flag = pli->thread_set_flag;
366 lockinfo.thread_clr_flag = pli->thread_clr_flag;
367 lockinfo.at_fork = pli->at_fork;
369 /* restore thread locking state, this time with new locks */
370 thread_mask_clear(~0);
371 thread_mask_set(flags);
372 dbg("_rtld_thread_init: done");
376 _rtld_atfork_pre(int *locks)
384 * Warning: this did not worked well with the rtld compat
385 * locks above, when the thread signal mask was corrupted (set
386 * to all signals blocked) if two locks were taken
387 * simultaneously in the write mode. The caller of the
388 * _rtld_atfork_pre() must provide the working implementation
389 * of the locks anyway, and libthr locks are fine.
391 wlock_acquire(rtld_phdr_lock, &ls[0]);
392 wlock_acquire(rtld_bind_lock, &ls[1]);
394 /* XXXKIB: I am really sorry for this. */
395 locks[0] = ls[1].lockstate;
396 locks[2] = ls[0].lockstate;
400 _rtld_atfork_post(int *locks)
407 bzero(ls, sizeof(ls));
408 ls[0].lockstate = locks[2];
409 ls[1].lockstate = locks[0];
410 lock_release(rtld_bind_lock, &ls[1]);
411 lock_release(rtld_phdr_lock, &ls[0]);