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>
53 #include "rtld_machdep.h"
55 #define WAFLAG 0x1 /* A writer holds the lock */
56 #define RC_INCR 0x2 /* Adjusts count of readers desiring lock */
58 typedef struct Struct_Lock {
63 static sigset_t fullsigmask, oldsigmask;
64 static int thread_flag;
75 * Arrange for the lock to occupy its own cache line. First, we
76 * optimistically allocate just a cache line, hoping that malloc
77 * will give us a well-aligned block of memory. If that doesn't
78 * work, we allocate a larger block and take a well-aligned cache
81 base = xmalloc(CACHE_LINE_SIZE);
83 if ((uintptr_t)p % CACHE_LINE_SIZE != 0) {
85 base = xmalloc(2 * CACHE_LINE_SIZE);
87 if ((r = (uintptr_t)p % CACHE_LINE_SIZE) != 0)
88 p += CACHE_LINE_SIZE - r;
97 def_lock_destroy(void *lock)
99 Lock *l = (Lock *)lock;
105 def_rlock_acquire(void *lock)
107 Lock *l = (Lock *)lock;
109 atomic_add_acq_int(&l->lock, RC_INCR);
110 while (l->lock & WAFLAG)
115 def_wlock_acquire(void *lock)
117 Lock *l = (Lock *)lock;
118 sigset_t tmp_oldsigmask;
121 sigprocmask(SIG_BLOCK, &fullsigmask, &tmp_oldsigmask);
122 if (atomic_cmpset_acq_int(&l->lock, 0, WAFLAG))
124 sigprocmask(SIG_SETMASK, &tmp_oldsigmask, NULL);
126 oldsigmask = tmp_oldsigmask;
130 def_lock_release(void *lock)
132 Lock *l = (Lock *)lock;
134 if ((l->lock & WAFLAG) == 0)
135 atomic_add_rel_int(&l->lock, -RC_INCR);
137 atomic_add_rel_int(&l->lock, -WAFLAG);
138 sigprocmask(SIG_SETMASK, &oldsigmask, NULL);
143 def_thread_set_flag(int mask)
145 int old_val = thread_flag;
151 def_thread_clr_flag(int mask)
153 int old_val = thread_flag;
154 thread_flag &= ~mask;
159 * Public interface exposed to the rest of the dynamic linker.
161 static struct RtldLockInfo lockinfo;
162 static struct RtldLockInfo deflockinfo;
165 thread_mask_set(int mask)
167 return lockinfo.thread_set_flag(mask);
171 thread_mask_clear(int mask)
173 lockinfo.thread_clr_flag(mask);
176 #define RTLD_LOCK_CNT 3
180 } rtld_locks[RTLD_LOCK_CNT];
182 rtld_lock_t rtld_bind_lock = &rtld_locks[0];
183 rtld_lock_t rtld_libc_lock = &rtld_locks[1];
184 rtld_lock_t rtld_phdr_lock = &rtld_locks[2];
187 rlock_acquire(rtld_lock_t lock)
189 if (thread_mask_set(lock->mask) & lock->mask) {
190 dbg("rlock_acquire: recursed");
193 lockinfo.rlock_acquire(lock->handle);
198 wlock_acquire(rtld_lock_t lock)
200 if (thread_mask_set(lock->mask) & lock->mask) {
201 dbg("wlock_acquire: recursed");
204 lockinfo.wlock_acquire(lock->handle);
209 rlock_release(rtld_lock_t lock, int locked)
213 thread_mask_clear(lock->mask);
214 lockinfo.lock_release(lock->handle);
218 wlock_release(rtld_lock_t lock, int locked)
222 thread_mask_clear(lock->mask);
223 lockinfo.lock_release(lock->handle);
231 deflockinfo.rtli_version = RTLI_VERSION;
232 deflockinfo.lock_create = def_lock_create;
233 deflockinfo.lock_destroy = def_lock_destroy;
234 deflockinfo.rlock_acquire = def_rlock_acquire;
235 deflockinfo.wlock_acquire = def_wlock_acquire;
236 deflockinfo.lock_release = def_lock_release;
237 deflockinfo.thread_set_flag = def_thread_set_flag;
238 deflockinfo.thread_clr_flag = def_thread_clr_flag;
239 deflockinfo.at_fork = NULL;
241 for (i = 0; i < RTLD_LOCK_CNT; i++) {
242 rtld_locks[i].mask = (1 << i);
243 rtld_locks[i].handle = NULL;
246 memcpy(&lockinfo, &deflockinfo, sizeof(lockinfo));
247 _rtld_thread_init(NULL);
249 * Construct a mask to block all signals except traps which might
250 * conceivably be generated within the dynamic linker itself.
252 sigfillset(&fullsigmask);
253 sigdelset(&fullsigmask, SIGILL);
254 sigdelset(&fullsigmask, SIGTRAP);
255 sigdelset(&fullsigmask, SIGABRT);
256 sigdelset(&fullsigmask, SIGEMT);
257 sigdelset(&fullsigmask, SIGFPE);
258 sigdelset(&fullsigmask, SIGBUS);
259 sigdelset(&fullsigmask, SIGSEGV);
260 sigdelset(&fullsigmask, SIGSYS);
264 * Callback function to allow threads implementation to
265 * register their own locking primitives if the default
266 * one is not suitable.
267 * The current context should be the only context
268 * executing at the invocation time.
271 _rtld_thread_init(struct RtldLockInfo *pli)
274 void *locks[RTLD_LOCK_CNT];
276 /* disable all locking while this function is running */
277 flags = thread_mask_set(~0);
283 for (i = 0; i < RTLD_LOCK_CNT; i++)
284 if ((locks[i] = pli->lock_create()) == NULL)
287 if (i < RTLD_LOCK_CNT) {
289 pli->lock_destroy(locks[i]);
293 for (i = 0; i < RTLD_LOCK_CNT; i++) {
294 if (rtld_locks[i].handle == NULL)
296 if (flags & rtld_locks[i].mask)
297 lockinfo.lock_release(rtld_locks[i].handle);
298 lockinfo.lock_destroy(rtld_locks[i].handle);
301 for (i = 0; i < RTLD_LOCK_CNT; i++) {
302 rtld_locks[i].handle = locks[i];
303 if (flags & rtld_locks[i].mask)
304 pli->wlock_acquire(rtld_locks[i].handle);
307 lockinfo.lock_create = pli->lock_create;
308 lockinfo.lock_destroy = pli->lock_destroy;
309 lockinfo.rlock_acquire = pli->rlock_acquire;
310 lockinfo.wlock_acquire = pli->wlock_acquire;
311 lockinfo.lock_release = pli->lock_release;
312 lockinfo.thread_set_flag = pli->thread_set_flag;
313 lockinfo.thread_clr_flag = pli->thread_clr_flag;
314 lockinfo.at_fork = pli->at_fork;
316 /* restore thread locking state, this time with new locks */
317 thread_mask_clear(~0);
318 thread_mask_set(flags);
319 dbg("_rtld_thread_init: done");
323 _rtld_atfork_pre(int *locks)
326 locks[2] = wlock_acquire(rtld_phdr_lock);
327 locks[0] = rlock_acquire(rtld_bind_lock);
331 _rtld_atfork_post(int *locks)
334 rlock_release(rtld_bind_lock, locks[0]);
335 wlock_release(rtld_phdr_lock, locks[2]);