2 * Copyright 1999, 2000 John D. Polstra.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * from: FreeBSD: src/libexec/rtld-elf/sparc64/lockdflt.c,v 1.3 2002/10/09
30 * Thread locking implementation for the dynamic linker.
32 * We use the "simple, non-scalable reader-preference lock" from:
34 * J. M. Mellor-Crummey and M. L. Scott. "Scalable Reader-Writer
35 * Synchronization for Shared-Memory Multiprocessors." 3rd ACM Symp. on
36 * Principles and Practice of Parallel Programming, April 1991.
38 * In this algorithm the lock is a single word. Its low-order bit is
39 * set when a writer holds the lock. The remaining high-order bits
40 * contain a count of readers desiring the lock. The algorithm requires
41 * atomic "compare_and_store" and "add" operations, which we implement
42 * using assembly language sequences in "rtld_start.S".
45 #include <sys/param.h>
52 #include "rtld_machdep.h"
54 void _rtld_thread_init(struct RtldLockInfo *) __exported;
55 void _rtld_atfork_pre(int *) __exported;
56 void _rtld_atfork_post(int *) __exported;
58 #define WAFLAG 0x1 /* A writer holds the lock */
59 #define RC_INCR 0x2 /* Adjusts count of readers desiring lock */
61 typedef struct Struct_Lock {
66 static sigset_t fullsigmask, oldsigmask;
67 static int thread_flag;
78 * Arrange for the lock to occupy its own cache line. First, we
79 * optimistically allocate just a cache line, hoping that malloc
80 * will give us a well-aligned block of memory. If that doesn't
81 * work, we allocate a larger block and take a well-aligned cache
84 base = xmalloc(CACHE_LINE_SIZE);
86 if ((uintptr_t)p % CACHE_LINE_SIZE != 0) {
88 base = xmalloc(2 * CACHE_LINE_SIZE);
90 if ((r = (uintptr_t)p % CACHE_LINE_SIZE) != 0)
91 p += CACHE_LINE_SIZE - r;
100 def_lock_destroy(void *lock)
102 Lock *l = (Lock *)lock;
108 def_rlock_acquire(void *lock)
110 Lock *l = (Lock *)lock;
112 atomic_add_acq_int(&l->lock, RC_INCR);
113 while (l->lock & WAFLAG)
118 def_wlock_acquire(void *lock)
120 Lock *l = (Lock *)lock;
121 sigset_t tmp_oldsigmask;
124 sigprocmask(SIG_BLOCK, &fullsigmask, &tmp_oldsigmask);
125 if (atomic_cmpset_acq_int(&l->lock, 0, WAFLAG))
127 sigprocmask(SIG_SETMASK, &tmp_oldsigmask, NULL);
129 oldsigmask = tmp_oldsigmask;
133 def_lock_release(void *lock)
135 Lock *l = (Lock *)lock;
137 if ((l->lock & WAFLAG) == 0)
138 atomic_add_rel_int(&l->lock, -RC_INCR);
140 atomic_add_rel_int(&l->lock, -WAFLAG);
141 sigprocmask(SIG_SETMASK, &oldsigmask, NULL);
146 def_thread_set_flag(int mask)
148 int old_val = thread_flag;
154 def_thread_clr_flag(int mask)
156 int old_val = thread_flag;
157 thread_flag &= ~mask;
162 * Public interface exposed to the rest of the dynamic linker.
164 static struct RtldLockInfo lockinfo;
165 static struct RtldLockInfo deflockinfo;
168 thread_mask_set(int mask)
170 return lockinfo.thread_set_flag(mask);
174 thread_mask_clear(int mask)
176 lockinfo.thread_clr_flag(mask);
179 #define RTLD_LOCK_CNT 3
183 } rtld_locks[RTLD_LOCK_CNT];
185 rtld_lock_t rtld_bind_lock = &rtld_locks[0];
186 rtld_lock_t rtld_libc_lock = &rtld_locks[1];
187 rtld_lock_t rtld_phdr_lock = &rtld_locks[2];
190 rlock_acquire(rtld_lock_t lock, RtldLockState *lockstate)
193 if (lockstate == NULL)
196 if (thread_mask_set(lock->mask) & lock->mask) {
197 dbg("rlock_acquire: recursed");
198 lockstate->lockstate = RTLD_LOCK_UNLOCKED;
201 lockinfo.rlock_acquire(lock->handle);
202 lockstate->lockstate = RTLD_LOCK_RLOCKED;
206 wlock_acquire(rtld_lock_t lock, RtldLockState *lockstate)
209 if (lockstate == NULL)
212 if (thread_mask_set(lock->mask) & lock->mask) {
213 dbg("wlock_acquire: recursed");
214 lockstate->lockstate = RTLD_LOCK_UNLOCKED;
217 lockinfo.wlock_acquire(lock->handle);
218 lockstate->lockstate = RTLD_LOCK_WLOCKED;
222 lock_release(rtld_lock_t lock, RtldLockState *lockstate)
225 if (lockstate == NULL)
228 switch (lockstate->lockstate) {
229 case RTLD_LOCK_UNLOCKED:
231 case RTLD_LOCK_RLOCKED:
232 case RTLD_LOCK_WLOCKED:
233 thread_mask_clear(lock->mask);
234 lockinfo.lock_release(lock->handle);
242 lock_upgrade(rtld_lock_t lock, RtldLockState *lockstate)
245 if (lockstate == NULL)
248 lock_release(lock, lockstate);
249 wlock_acquire(lock, lockstate);
253 lock_restart_for_upgrade(RtldLockState *lockstate)
256 if (lockstate == NULL)
259 switch (lockstate->lockstate) {
260 case RTLD_LOCK_UNLOCKED:
261 case RTLD_LOCK_WLOCKED:
263 case RTLD_LOCK_RLOCKED:
264 siglongjmp(lockstate->env, 1);
276 deflockinfo.rtli_version = RTLI_VERSION;
277 deflockinfo.lock_create = def_lock_create;
278 deflockinfo.lock_destroy = def_lock_destroy;
279 deflockinfo.rlock_acquire = def_rlock_acquire;
280 deflockinfo.wlock_acquire = def_wlock_acquire;
281 deflockinfo.lock_release = def_lock_release;
282 deflockinfo.thread_set_flag = def_thread_set_flag;
283 deflockinfo.thread_clr_flag = def_thread_clr_flag;
284 deflockinfo.at_fork = NULL;
286 for (i = 0; i < RTLD_LOCK_CNT; i++) {
287 rtld_locks[i].mask = (1 << i);
288 rtld_locks[i].handle = NULL;
291 memcpy(&lockinfo, &deflockinfo, sizeof(lockinfo));
292 _rtld_thread_init(NULL);
294 * Construct a mask to block all signals except traps which might
295 * conceivably be generated within the dynamic linker itself.
297 sigfillset(&fullsigmask);
298 sigdelset(&fullsigmask, SIGILL);
299 sigdelset(&fullsigmask, SIGTRAP);
300 sigdelset(&fullsigmask, SIGABRT);
301 sigdelset(&fullsigmask, SIGEMT);
302 sigdelset(&fullsigmask, SIGFPE);
303 sigdelset(&fullsigmask, SIGBUS);
304 sigdelset(&fullsigmask, SIGSEGV);
305 sigdelset(&fullsigmask, SIGSYS);
309 * Callback function to allow threads implementation to
310 * register their own locking primitives if the default
311 * one is not suitable.
312 * The current context should be the only context
313 * executing at the invocation time.
316 _rtld_thread_init(struct RtldLockInfo *pli)
319 void *locks[RTLD_LOCK_CNT];
321 /* disable all locking while this function is running */
322 flags = thread_mask_set(~0);
328 for (i = 0; i < RTLD_LOCK_CNT; i++)
329 if ((locks[i] = pli->lock_create()) == NULL)
332 if (i < RTLD_LOCK_CNT) {
334 pli->lock_destroy(locks[i]);
338 for (i = 0; i < RTLD_LOCK_CNT; i++) {
339 if (rtld_locks[i].handle == NULL)
341 if (flags & rtld_locks[i].mask)
342 lockinfo.lock_release(rtld_locks[i].handle);
343 lockinfo.lock_destroy(rtld_locks[i].handle);
346 for (i = 0; i < RTLD_LOCK_CNT; i++) {
347 rtld_locks[i].handle = locks[i];
348 if (flags & rtld_locks[i].mask)
349 pli->wlock_acquire(rtld_locks[i].handle);
352 lockinfo.lock_create = pli->lock_create;
353 lockinfo.lock_destroy = pli->lock_destroy;
354 lockinfo.rlock_acquire = pli->rlock_acquire;
355 lockinfo.wlock_acquire = pli->wlock_acquire;
356 lockinfo.lock_release = pli->lock_release;
357 lockinfo.thread_set_flag = pli->thread_set_flag;
358 lockinfo.thread_clr_flag = pli->thread_clr_flag;
359 lockinfo.at_fork = pli->at_fork;
361 /* restore thread locking state, this time with new locks */
362 thread_mask_clear(~0);
363 thread_mask_set(flags);
364 dbg("_rtld_thread_init: done");
368 _rtld_atfork_pre(int *locks)
376 * Warning: this does not work with the rtld compat locks
377 * above, since the thread signal mask is corrupted (set to
378 * all signals blocked) if two locks are taken in write mode.
379 * The caller of the _rtld_atfork_pre() must provide the
380 * working implementation of the locks, and libthr locks are
383 wlock_acquire(rtld_phdr_lock, &ls[0]);
384 wlock_acquire(rtld_bind_lock, &ls[1]);
386 /* XXXKIB: I am really sorry for this. */
387 locks[0] = ls[1].lockstate;
388 locks[2] = ls[0].lockstate;
392 _rtld_atfork_post(int *locks)
399 bzero(ls, sizeof(ls));
400 ls[0].lockstate = locks[2];
401 ls[1].lockstate = locks[0];
402 lock_release(rtld_bind_lock, &ls[1]);
403 lock_release(rtld_phdr_lock, &ls[0]);