xref: /dragonfly/libexec/rtld-elf/rtld_lock.c (revision 0fe220b4)
1 /*-
2  * Copyright 1999, 2000 John D. Polstra.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
24  *
25  *	from: FreeBSD: src/libexec/rtld-elf/sparc64/lockdflt.c,v 1.3 2002/10/09
26  * $FreeBSD$
27  */
28 
29 /*
30  * Thread locking implementation for the dynamic linker.
31  *
32  * We use the "simple, non-scalable reader-preference lock" from:
33  *
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.
37  *
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".
43  */
44 
45 #include <sys/param.h>
46 #include <signal.h>
47 #include <stdlib.h>
48 #include <time.h>
49 
50 #include "debug.h"
51 #include "rtld.h"
52 #include "rtld_machdep.h"
53 
54 #define WAFLAG		0x1	/* A writer holds the lock */
55 #define RC_INCR		0x2	/* Adjusts count of readers desiring lock */
56 
57 typedef struct Struct_Lock {
58 	volatile u_int lock;
59 	void *base;
60 } Lock;
61 
62 static sigset_t fullsigmask, oldsigmask;
63 static int thread_flag;
64 
65 static void *
66 def_lock_create(void)
67 {
68     void *base;
69     char *p;
70     uintptr_t r;
71     Lock *l;
72 
73     /*
74      * Arrange for the lock to occupy its own cache line.  First, we
75      * optimistically allocate just a cache line, hoping that malloc
76      * will give us a well-aligned block of memory.  If that doesn't
77      * work, we allocate a larger block and take a well-aligned cache
78      * line from it.
79      */
80     base = xmalloc(CACHE_LINE_SIZE);
81     p = (char *)base;
82     if ((uintptr_t)p % CACHE_LINE_SIZE != 0) {
83 	free(base);
84 	base = xmalloc(2 * CACHE_LINE_SIZE);
85 	p = (char *)base;
86 	if ((r = (uintptr_t)p % CACHE_LINE_SIZE) != 0)
87 	    p += CACHE_LINE_SIZE - r;
88     }
89     l = (Lock *)p;
90     l->base = base;
91     l->lock = 0;
92     return l;
93 }
94 
95 static void
96 def_lock_destroy(void *lock)
97 {
98     Lock *l = (Lock *)lock;
99 
100     free(l->base);
101 }
102 
103 static void
104 def_rlock_acquire(void *lock)
105 {
106     Lock *l = (Lock *)lock;
107 
108     atomic_add_acq_int(&l->lock, RC_INCR);
109     while (l->lock & WAFLAG)
110 	    ;	/* Spin */
111 }
112 
113 static void
114 def_wlock_acquire(void *lock)
115 {
116     Lock *l = (Lock *)lock;
117     sigset_t tmp_oldsigmask;
118 
119     for ( ; ; ) {
120 	sigprocmask(SIG_BLOCK, &fullsigmask, &tmp_oldsigmask);
121 	if (atomic_cmpset_acq_int(&l->lock, 0, WAFLAG))
122 	    break;
123 	sigprocmask(SIG_SETMASK, &tmp_oldsigmask, NULL);
124     }
125     oldsigmask = tmp_oldsigmask;
126 }
127 
128 static void
129 def_lock_release(void *lock)
130 {
131     Lock *l = (Lock *)lock;
132 
133     if ((l->lock & WAFLAG) == 0)
134 	atomic_add_rel_int(&l->lock, -RC_INCR);
135     else {
136 	atomic_add_rel_int(&l->lock, -WAFLAG);
137 	sigprocmask(SIG_SETMASK, &oldsigmask, NULL);
138     }
139 }
140 
141 static int
142 def_thread_set_flag(int mask)
143 {
144 	int old_val = thread_flag;
145 	thread_flag |= mask;
146 	return (old_val);
147 }
148 
149 static int
150 def_thread_clr_flag(int mask)
151 {
152 	int old_val = thread_flag;
153 	thread_flag &= ~mask;
154 	return (old_val);
155 }
156 
157 /*
158  * Public interface exposed to the rest of the dynamic linker.
159  */
160 static struct RtldLockInfo lockinfo;
161 static struct RtldLockInfo deflockinfo;
162 
163 static __inline int
164 thread_mask_set(int mask)
165 {
166 	return lockinfo.thread_set_flag(mask);
167 }
168 
169 static __inline void
170 thread_mask_clear(int mask)
171 {
172 	lockinfo.thread_clr_flag(mask);
173 }
174 
175 #define	RTLD_LOCK_CNT	3
176 struct rtld_lock {
177 	void	*handle;
178 	int	 mask;
179 	RtldLockState forkstate;
180 } rtld_locks[RTLD_LOCK_CNT];
181 
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];
185 
186 void
187 rlock_acquire(rtld_lock_t lock, RtldLockState *lockstate)
188 {
189 
190 	if (lockstate == NULL)
191 		return;
192 
193 	if (thread_mask_set(lock->mask) & lock->mask) {
194 		dbg("rlock_acquire: recursed");
195 		lockstate->lockstate = RTLD_LOCK_UNLOCKED;
196 		return;
197 	}
198 	lockinfo.rlock_acquire(lock->handle);
199 	lockstate->lockstate = RTLD_LOCK_RLOCKED;
200 }
201 
202 void
203 wlock_acquire(rtld_lock_t lock, RtldLockState *lockstate)
204 {
205 	if (lockstate == NULL)
206 		return;
207 
208 	if (thread_mask_set(lock->mask) & lock->mask) {
209 		dbg("wlock_acquire: recursed");
210 		lockstate->lockstate = RTLD_LOCK_UNLOCKED;
211 		return;
212 	}
213 	lockinfo.wlock_acquire(lock->handle);
214 	lockstate->lockstate = RTLD_LOCK_WLOCKED;
215 }
216 
217 void
218 lock_release(rtld_lock_t lock, RtldLockState *lockstate)
219 {
220 	if (lockstate == NULL)
221 		return;
222 
223 	switch (lockstate->lockstate) {
224 	case RTLD_LOCK_UNLOCKED:
225 		break;
226 	case RTLD_LOCK_RLOCKED:
227 	case RTLD_LOCK_WLOCKED:
228 		lockinfo.lock_release(lock->handle);
229 		thread_mask_clear(lock->mask);
230 		break;
231 	default:
232 		assert(0);
233 	}
234 }
235 
236 void
237 lock_upgrade(rtld_lock_t lock, RtldLockState *lockstate)
238 {
239 	if (lockstate == NULL)
240 		return;
241 
242 	lock_release(lock, lockstate);
243 	wlock_acquire(lock, lockstate);
244 }
245 
246 void
247 lock_restart_for_upgrade(RtldLockState *lockstate)
248 {
249 	if (lockstate == NULL)
250 		return;
251 
252 	switch (lockstate->lockstate) {
253 	case RTLD_LOCK_UNLOCKED:
254 	case RTLD_LOCK_WLOCKED:
255 		break;
256 	case RTLD_LOCK_RLOCKED:
257 		siglongjmp(lockstate->env, 1);
258 		break;
259 	default:
260 		assert(0);
261 	}
262 }
263 
264 void
265 lockdflt_init(void)
266 {
267     int i;
268 
269     deflockinfo.rtli_version  = RTLI_VERSION;
270     deflockinfo.lock_create   = def_lock_create;
271     deflockinfo.lock_destroy  = def_lock_destroy;
272     deflockinfo.rlock_acquire = def_rlock_acquire;
273     deflockinfo.wlock_acquire = def_wlock_acquire;
274     deflockinfo.lock_release  = def_lock_release;
275     deflockinfo.thread_set_flag = def_thread_set_flag;
276     deflockinfo.thread_clr_flag = def_thread_clr_flag;
277     deflockinfo.at_fork = NULL;
278 
279     for (i = 0; i < RTLD_LOCK_CNT; i++) {
280 	    rtld_locks[i].mask   = (1 << i);
281 	    rtld_locks[i].handle = NULL;
282     }
283 
284     memcpy(&lockinfo, &deflockinfo, sizeof(lockinfo));
285     _rtld_thread_init(NULL);
286     /*
287      * Construct a mask to block all signals except traps which might
288      * conceivably be generated within the dynamic linker itself.
289      */
290     sigfillset(&fullsigmask);
291     sigdelset(&fullsigmask, SIGILL);
292     sigdelset(&fullsigmask, SIGTRAP);
293     sigdelset(&fullsigmask, SIGABRT);
294     sigdelset(&fullsigmask, SIGEMT);
295     sigdelset(&fullsigmask, SIGFPE);
296     sigdelset(&fullsigmask, SIGBUS);
297     sigdelset(&fullsigmask, SIGSEGV);
298     sigdelset(&fullsigmask, SIGSYS);
299 }
300 
301 /*
302  * Callback function to allow threads implementation to
303  * register their own locking primitives if the default
304  * one is not suitable.
305  * The current context should be the only context
306  * executing at the invocation time.
307  */
308 void
309 _rtld_thread_init(struct RtldLockInfo *pli)
310 {
311 	int flags, i;
312 	void *locks[RTLD_LOCK_CNT];
313 
314 	/* disable all locking while this function is running */
315 	flags =	thread_mask_set(~0);
316 
317 	if (pli == NULL)
318 		pli = &deflockinfo;
319 
320 
321 	for (i = 0; i < RTLD_LOCK_CNT; i++) {
322 		if ((locks[i] = pli->lock_create()) == NULL)
323 			break;
324 	}
325 
326 	if (i < RTLD_LOCK_CNT) {
327 		while (--i >= 0) {
328 			pli->lock_destroy(locks[i]);
329 		}
330 		abort();
331 	}
332 
333 	for (i = 0; i < RTLD_LOCK_CNT; i++) {
334 		if (rtld_locks[i].handle == NULL)
335 			continue;
336 		if (flags & rtld_locks[i].mask)
337 			lockinfo.lock_release(rtld_locks[i].handle);
338 		lockinfo.lock_destroy(rtld_locks[i].handle);
339 	}
340 
341 	for (i = 0; i < RTLD_LOCK_CNT; i++) {
342 		rtld_locks[i].handle = locks[i];
343 		if (flags & rtld_locks[i].mask)
344 			pli->wlock_acquire(rtld_locks[i].handle);
345 	}
346 
347 	lockinfo.lock_create = pli->lock_create;
348 	lockinfo.lock_destroy = pli->lock_destroy;
349 	lockinfo.rlock_acquire = pli->rlock_acquire;
350 	lockinfo.wlock_acquire = pli->wlock_acquire;
351 	lockinfo.lock_release  = pli->lock_release;
352 	lockinfo.thread_set_flag = pli->thread_set_flag;
353 	lockinfo.thread_clr_flag = pli->thread_clr_flag;
354 	lockinfo.at_fork = pli->at_fork;
355 
356 	/* restore thread locking state, this time with new locks */
357 	thread_mask_clear(~0);
358 	thread_mask_set(flags);
359 	dbg("_rtld_thread_init: done");
360 }
361 
362 void
363 _rtld_thread_prefork(void)
364 {
365 	int i;
366 
367 	for (i = 0; i < RTLD_LOCK_CNT; i++) {
368 		if (rtld_locks[i].handle == NULL)
369 			continue;
370 		wlock_acquire(&rtld_locks[i], &rtld_locks[i].forkstate);
371 	}
372 }
373 
374 void
375 _rtld_thread_postfork(void)
376 {
377 	int i;
378 
379 	for (i = 0; i < RTLD_LOCK_CNT; i++) {
380 		if (rtld_locks[i].handle == NULL)
381 			continue;
382 		lock_release(&rtld_locks[i], &rtld_locks[i].forkstate);
383 	}
384 }
385 
386 /*
387  * pthreads already called _rtld_thread_init(NULL) in the child, so there
388  * is nothing for us to do here.
389  */
390 void
391 _rtld_thread_childfork(void)
392 {
393 }
394