xref: /freebsd/libexec/rtld-elf/rtld_lock.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
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 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 #if __i386__
141 /*
142  * Import a  crude exclusive lock implementation for i386 processors.
143  * This file will be removed once i386 support is deprecated in favor
144  * of i486+.
145  */
146 #include "i386/lockdflt.c"
147 
148 #endif
149 
150 static int
151 def_thread_set_flag(int mask)
152 {
153 	int old_val = thread_flag;
154 	thread_flag |= mask;
155 	return (old_val);
156 }
157 
158 static int
159 def_thread_clr_flag(int mask)
160 {
161 	int old_val = thread_flag;
162 	thread_flag &= ~mask;
163 	return (old_val);
164 }
165 
166 /*
167  * Public interface exposed to the rest of the dynamic linker.
168  */
169 static struct RtldLockInfo lockinfo;
170 static struct RtldLockInfo deflockinfo;
171 
172 static __inline int
173 thread_mask_set(int mask)
174 {
175 	return lockinfo.thread_set_flag(mask);
176 }
177 
178 static __inline void
179 thread_mask_clear(int mask)
180 {
181 	lockinfo.thread_clr_flag(mask);
182 }
183 
184 #define	RTLD_LOCK_CNT	2
185 struct rtld_lock {
186 	void	*handle;
187 	int	 mask;
188 } rtld_locks[RTLD_LOCK_CNT];
189 
190 rtld_lock_t	rtld_bind_lock = &rtld_locks[0];
191 rtld_lock_t	rtld_libc_lock = &rtld_locks[1];
192 
193 int
194 rlock_acquire(rtld_lock_t lock)
195 {
196 	if (thread_mask_set(lock->mask)) {
197 	    dbg("rlock_acquire: recursed");
198 	    return (0);
199 	}
200 	lockinfo.rlock_acquire(lock->handle);
201 	return (1);
202 }
203 
204 int
205 wlock_acquire(rtld_lock_t lock)
206 {
207 	if (thread_mask_set(lock->mask)) {
208 	    dbg("wlock_acquire: recursed");
209 	    return (0);
210 	}
211 	lockinfo.wlock_acquire(lock->handle);
212 	return (1);
213 }
214 
215 void
216 rlock_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 wlock_release(rtld_lock_t lock, int locked)
226 {
227 	if (locked == 0)
228 	    return;
229 	thread_mask_clear(lock->mask);
230 	lockinfo.lock_release(lock->handle);
231 }
232 
233 void
234 lockdflt_init()
235 {
236     int i;
237 
238     deflockinfo.rtli_version  = RTLI_VERSION;
239     deflockinfo.lock_create   = def_lock_create;
240     deflockinfo.lock_destroy  = def_lock_destroy;
241     deflockinfo.rlock_acquire = def_rlock_acquire;
242     deflockinfo.wlock_acquire = def_wlock_acquire;
243     deflockinfo.lock_release  = def_lock_release;
244     deflockinfo.thread_set_flag = def_thread_set_flag;
245     deflockinfo.thread_clr_flag = def_thread_clr_flag;
246     deflockinfo.at_fork = NULL;
247 
248     for (i = 0; i < RTLD_LOCK_CNT; i++) {
249 	    rtld_locks[i].mask   = (1 << i);
250 	    rtld_locks[i].handle = NULL;
251     }
252 
253 #if __i386__
254     if (!cpu_supports_cmpxchg()) {
255 	/* It's a cruddy old 80386. */
256 	deflockinfo.rlock_acquire = lock80386_acquire;
257 	deflockinfo.wlock_acquire = lock80386_acquire;
258 	deflockinfo.lock_release  = lock80386_release;
259     }
260 #endif
261 
262     memcpy(&lockinfo, &deflockinfo, sizeof(lockinfo));
263     _rtld_thread_init(NULL);
264     /*
265      * Construct a mask to block all signals except traps which might
266      * conceivably be generated within the dynamic linker itself.
267      */
268     sigfillset(&fullsigmask);
269     sigdelset(&fullsigmask, SIGILL);
270     sigdelset(&fullsigmask, SIGTRAP);
271     sigdelset(&fullsigmask, SIGABRT);
272     sigdelset(&fullsigmask, SIGEMT);
273     sigdelset(&fullsigmask, SIGFPE);
274     sigdelset(&fullsigmask, SIGBUS);
275     sigdelset(&fullsigmask, SIGSEGV);
276     sigdelset(&fullsigmask, SIGSYS);
277 }
278 
279 /*
280  * Callback function to allow threads implementation to
281  * register their own locking primitives if the default
282  * one is not suitable.
283  * The current context should be the only context
284  * executing at the invocation time.
285  */
286 void
287 _rtld_thread_init(struct RtldLockInfo *pli)
288 {
289 	int flags, i;
290 	void *locks[RTLD_LOCK_CNT];
291 
292 	/* disable all locking while this function is running */
293 	flags =	thread_mask_set(~0);
294 
295 	if (pli == NULL)
296 		pli = &deflockinfo;
297 
298 
299 	for (i = 0; i < RTLD_LOCK_CNT; i++)
300 		if ((locks[i] = pli->lock_create()) == NULL)
301 			break;
302 
303 	if (i < RTLD_LOCK_CNT) {
304 		while (--i >= 0)
305 			pli->lock_destroy(locks[i]);
306 		abort();
307 	}
308 
309 	for (i = 0; i < RTLD_LOCK_CNT; i++) {
310 		if (rtld_locks[i].handle == NULL)
311 			continue;
312 		if (flags & rtld_locks[i].mask)
313 			lockinfo.lock_release(rtld_locks[i].handle);
314 		lockinfo.lock_destroy(rtld_locks[i].handle);
315 	}
316 
317 	for (i = 0; i < RTLD_LOCK_CNT; i++) {
318 		rtld_locks[i].handle = locks[i];
319 		if (flags & rtld_locks[i].mask)
320 			pli->wlock_acquire(rtld_locks[i].handle);
321 	}
322 
323 	lockinfo.lock_create = pli->lock_create;
324 	lockinfo.lock_destroy = pli->lock_destroy;
325 	lockinfo.rlock_acquire = pli->rlock_acquire;
326 	lockinfo.wlock_acquire = pli->wlock_acquire;
327 	lockinfo.lock_release  = pli->lock_release;
328 	lockinfo.thread_set_flag = pli->thread_set_flag;
329 	lockinfo.thread_clr_flag = pli->thread_clr_flag;
330 	lockinfo.at_fork = pli->at_fork;
331 
332 	/* restore thread locking state, this time with new locks */
333 	thread_mask_clear(~0);
334 	thread_mask_set(flags);
335 	dbg("_rtld_thread_init: done");
336 }
337