xref: /freebsd/libexec/rtld-elf/rtld_lock.c (revision 39beb93c)
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 <signal.h>
46 #include <stdlib.h>
47 #include <time.h>
48 
49 #include "debug.h"
50 #include "rtld.h"
51 #include "rtld_machdep.h"
52 
53 #define WAFLAG		0x1	/* A writer holds the lock */
54 #define RC_INCR		0x2	/* Adjusts count of readers desiring lock */
55 
56 typedef struct Struct_Lock {
57 	volatile u_int lock;
58 	void *base;
59 } Lock;
60 
61 static sigset_t fullsigmask, oldsigmask;
62 static int thread_flag;
63 
64 static void *
65 def_lock_create()
66 {
67     void *base;
68     char *p;
69     uintptr_t r;
70     Lock *l;
71 
72     /*
73      * Arrange for the lock to occupy its own cache line.  First, we
74      * optimistically allocate just a cache line, hoping that malloc
75      * will give us a well-aligned block of memory.  If that doesn't
76      * work, we allocate a larger block and take a well-aligned cache
77      * line from it.
78      */
79     base = xmalloc(CACHE_LINE_SIZE);
80     p = (char *)base;
81     if ((uintptr_t)p % CACHE_LINE_SIZE != 0) {
82 	free(base);
83 	base = xmalloc(2 * CACHE_LINE_SIZE);
84 	p = (char *)base;
85 	if ((r = (uintptr_t)p % CACHE_LINE_SIZE) != 0)
86 	    p += CACHE_LINE_SIZE - r;
87     }
88     l = (Lock *)p;
89     l->base = base;
90     l->lock = 0;
91     return l;
92 }
93 
94 static void
95 def_lock_destroy(void *lock)
96 {
97     Lock *l = (Lock *)lock;
98 
99     free(l->base);
100 }
101 
102 static void
103 def_rlock_acquire(void *lock)
104 {
105     Lock *l = (Lock *)lock;
106 
107     atomic_add_acq_int(&l->lock, RC_INCR);
108     while (l->lock & WAFLAG)
109 	    ;	/* Spin */
110 }
111 
112 static void
113 def_wlock_acquire(void *lock)
114 {
115     Lock *l = (Lock *)lock;
116     sigset_t tmp_oldsigmask;
117 
118     for ( ; ; ) {
119 	sigprocmask(SIG_BLOCK, &fullsigmask, &tmp_oldsigmask);
120 	if (atomic_cmpset_acq_int(&l->lock, 0, WAFLAG))
121 	    break;
122 	sigprocmask(SIG_SETMASK, &tmp_oldsigmask, NULL);
123     }
124     oldsigmask = tmp_oldsigmask;
125 }
126 
127 static void
128 def_lock_release(void *lock)
129 {
130     Lock *l = (Lock *)lock;
131 
132     if ((l->lock & WAFLAG) == 0)
133     	atomic_add_rel_int(&l->lock, -RC_INCR);
134     else {
135     	atomic_add_rel_int(&l->lock, -WAFLAG);
136     	sigprocmask(SIG_SETMASK, &oldsigmask, NULL);
137     }
138 }
139 
140 static int
141 def_thread_set_flag(int mask)
142 {
143 	int old_val = thread_flag;
144 	thread_flag |= mask;
145 	return (old_val);
146 }
147 
148 static int
149 def_thread_clr_flag(int mask)
150 {
151 	int old_val = thread_flag;
152 	thread_flag &= ~mask;
153 	return (old_val);
154 }
155 
156 /*
157  * Public interface exposed to the rest of the dynamic linker.
158  */
159 static struct RtldLockInfo lockinfo;
160 static struct RtldLockInfo deflockinfo;
161 
162 static __inline int
163 thread_mask_set(int mask)
164 {
165 	return lockinfo.thread_set_flag(mask);
166 }
167 
168 static __inline void
169 thread_mask_clear(int mask)
170 {
171 	lockinfo.thread_clr_flag(mask);
172 }
173 
174 #define	RTLD_LOCK_CNT	3
175 struct rtld_lock {
176 	void	*handle;
177 	int	 mask;
178 } rtld_locks[RTLD_LOCK_CNT];
179 
180 rtld_lock_t	rtld_bind_lock = &rtld_locks[0];
181 rtld_lock_t	rtld_libc_lock = &rtld_locks[1];
182 rtld_lock_t	rtld_phdr_lock = &rtld_locks[2];
183 
184 int
185 rlock_acquire(rtld_lock_t lock)
186 {
187 	if (thread_mask_set(lock->mask) & lock->mask) {
188 	    dbg("rlock_acquire: recursed");
189 	    return (0);
190 	}
191 	lockinfo.rlock_acquire(lock->handle);
192 	return (1);
193 }
194 
195 int
196 wlock_acquire(rtld_lock_t lock)
197 {
198 	if (thread_mask_set(lock->mask) & lock->mask) {
199 	    dbg("wlock_acquire: recursed");
200 	    return (0);
201 	}
202 	lockinfo.wlock_acquire(lock->handle);
203 	return (1);
204 }
205 
206 void
207 rlock_release(rtld_lock_t lock, int locked)
208 {
209 	if (locked == 0)
210 	    return;
211 	thread_mask_clear(lock->mask);
212 	lockinfo.lock_release(lock->handle);
213 }
214 
215 void
216 wlock_release(rtld_lock_t lock, int locked)
217 {
218 	if (locked == 0)
219 	    return;
220 	thread_mask_clear(lock->mask);
221 	lockinfo.lock_release(lock->handle);
222 }
223 
224 void
225 lockdflt_init()
226 {
227     int i;
228 
229     deflockinfo.rtli_version  = RTLI_VERSION;
230     deflockinfo.lock_create   = def_lock_create;
231     deflockinfo.lock_destroy  = def_lock_destroy;
232     deflockinfo.rlock_acquire = def_rlock_acquire;
233     deflockinfo.wlock_acquire = def_wlock_acquire;
234     deflockinfo.lock_release  = def_lock_release;
235     deflockinfo.thread_set_flag = def_thread_set_flag;
236     deflockinfo.thread_clr_flag = def_thread_clr_flag;
237     deflockinfo.at_fork = NULL;
238 
239     for (i = 0; i < RTLD_LOCK_CNT; i++) {
240 	    rtld_locks[i].mask   = (1 << i);
241 	    rtld_locks[i].handle = NULL;
242     }
243 
244     memcpy(&lockinfo, &deflockinfo, sizeof(lockinfo));
245     _rtld_thread_init(NULL);
246     /*
247      * Construct a mask to block all signals except traps which might
248      * conceivably be generated within the dynamic linker itself.
249      */
250     sigfillset(&fullsigmask);
251     sigdelset(&fullsigmask, SIGILL);
252     sigdelset(&fullsigmask, SIGTRAP);
253     sigdelset(&fullsigmask, SIGABRT);
254     sigdelset(&fullsigmask, SIGEMT);
255     sigdelset(&fullsigmask, SIGFPE);
256     sigdelset(&fullsigmask, SIGBUS);
257     sigdelset(&fullsigmask, SIGSEGV);
258     sigdelset(&fullsigmask, SIGSYS);
259 }
260 
261 /*
262  * Callback function to allow threads implementation to
263  * register their own locking primitives if the default
264  * one is not suitable.
265  * The current context should be the only context
266  * executing at the invocation time.
267  */
268 void
269 _rtld_thread_init(struct RtldLockInfo *pli)
270 {
271 	int flags, i;
272 	void *locks[RTLD_LOCK_CNT];
273 
274 	/* disable all locking while this function is running */
275 	flags =	thread_mask_set(~0);
276 
277 	if (pli == NULL)
278 		pli = &deflockinfo;
279 
280 
281 	for (i = 0; i < RTLD_LOCK_CNT; i++)
282 		if ((locks[i] = pli->lock_create()) == NULL)
283 			break;
284 
285 	if (i < RTLD_LOCK_CNT) {
286 		while (--i >= 0)
287 			pli->lock_destroy(locks[i]);
288 		abort();
289 	}
290 
291 	for (i = 0; i < RTLD_LOCK_CNT; i++) {
292 		if (rtld_locks[i].handle == NULL)
293 			continue;
294 		if (flags & rtld_locks[i].mask)
295 			lockinfo.lock_release(rtld_locks[i].handle);
296 		lockinfo.lock_destroy(rtld_locks[i].handle);
297 	}
298 
299 	for (i = 0; i < RTLD_LOCK_CNT; i++) {
300 		rtld_locks[i].handle = locks[i];
301 		if (flags & rtld_locks[i].mask)
302 			pli->wlock_acquire(rtld_locks[i].handle);
303 	}
304 
305 	lockinfo.lock_create = pli->lock_create;
306 	lockinfo.lock_destroy = pli->lock_destroy;
307 	lockinfo.rlock_acquire = pli->rlock_acquire;
308 	lockinfo.wlock_acquire = pli->wlock_acquire;
309 	lockinfo.lock_release  = pli->lock_release;
310 	lockinfo.thread_set_flag = pli->thread_set_flag;
311 	lockinfo.thread_clr_flag = pli->thread_clr_flag;
312 	lockinfo.at_fork = pli->at_fork;
313 
314 	/* restore thread locking state, this time with new locks */
315 	thread_mask_clear(~0);
316 	thread_mask_set(flags);
317 	dbg("_rtld_thread_init: done");
318 }
319 
320 void
321 _rtld_atfork_pre(int *locks)
322 {
323 
324 	locks[2] = wlock_acquire(rtld_phdr_lock);
325 	locks[0] = rlock_acquire(rtld_bind_lock);
326 }
327 
328 void
329 _rtld_atfork_post(int *locks)
330 {
331 
332 	rlock_release(rtld_bind_lock, locks[0]);
333 	wlock_release(rtld_phdr_lock, locks[2]);
334 }
335