rtld_lock.c revision 216695
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: head/libexec/rtld-elf/rtld_lock.c 216695 2010-12-25 08:51:20Z kib $
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
57typedef struct Struct_Lock {
58	volatile u_int lock;
59	void *base;
60} Lock;
61
62static sigset_t fullsigmask, oldsigmask;
63static int thread_flag;
64
65static void *
66def_lock_create()
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
95static void
96def_lock_destroy(void *lock)
97{
98    Lock *l = (Lock *)lock;
99
100    free(l->base);
101}
102
103static void
104def_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
113static void
114def_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
128static void
129def_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
141static int
142def_thread_set_flag(int mask)
143{
144	int old_val = thread_flag;
145	thread_flag |= mask;
146	return (old_val);
147}
148
149static int
150def_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 */
160static struct RtldLockInfo lockinfo;
161static struct RtldLockInfo deflockinfo;
162
163static __inline int
164thread_mask_set(int mask)
165{
166	return lockinfo.thread_set_flag(mask);
167}
168
169static __inline void
170thread_mask_clear(int mask)
171{
172	lockinfo.thread_clr_flag(mask);
173}
174
175#define	RTLD_LOCK_CNT	3
176struct rtld_lock {
177	void	*handle;
178	int	 mask;
179} rtld_locks[RTLD_LOCK_CNT];
180
181rtld_lock_t	rtld_bind_lock = &rtld_locks[0];
182rtld_lock_t	rtld_libc_lock = &rtld_locks[1];
183rtld_lock_t	rtld_phdr_lock = &rtld_locks[2];
184
185#define print_ebp(str) do {register long ebp asm("ebp"); printf("%s 0x%0lx\n", str, ebp);} while (0)
186
187void
188rlock_acquire(rtld_lock_t lock, RtldLockState *lockstate)
189{
190
191	if (lockstate == NULL)
192		return;
193
194	if (thread_mask_set(lock->mask) & lock->mask) {
195		dbg("rlock_acquire: recursed");
196		lockstate->lockstate = RTLD_LOCK_UNLOCKED;
197		return;
198	}
199	lockinfo.rlock_acquire(lock->handle);
200	lockstate->lockstate = RTLD_LOCK_RLOCKED;
201}
202
203void
204wlock_acquire(rtld_lock_t lock, RtldLockState *lockstate)
205{
206
207	if (lockstate == NULL)
208		return;
209
210	if (thread_mask_set(lock->mask) & lock->mask) {
211		dbg("wlock_acquire: recursed");
212		lockstate->lockstate = RTLD_LOCK_UNLOCKED;
213		return;
214	}
215	lockinfo.wlock_acquire(lock->handle);
216	lockstate->lockstate = RTLD_LOCK_WLOCKED;
217}
218
219void
220lock_release(rtld_lock_t lock, RtldLockState *lockstate)
221{
222
223	if (lockstate == NULL)
224		return;
225
226	switch (lockstate->lockstate) {
227	case RTLD_LOCK_UNLOCKED:
228		break;
229	case RTLD_LOCK_RLOCKED:
230	case RTLD_LOCK_WLOCKED:
231		thread_mask_clear(lock->mask);
232		lockinfo.lock_release(lock->handle);
233		break;
234	default:
235		assert(0);
236	}
237}
238
239void
240lock_upgrade(rtld_lock_t lock, RtldLockState *lockstate)
241{
242
243	if (lockstate == NULL)
244		return;
245
246	lock_release(lock, lockstate);
247	wlock_acquire(lock, lockstate);
248}
249
250void
251lock_restart_for_upgrade(RtldLockState *lockstate)
252{
253
254	if (lockstate == NULL)
255		return;
256
257	switch (lockstate->lockstate) {
258	case RTLD_LOCK_UNLOCKED:
259	case RTLD_LOCK_WLOCKED:
260		break;
261	case RTLD_LOCK_RLOCKED:
262		longjmp(lockstate->env, 1);
263		break;
264	default:
265		assert(0);
266	}
267}
268
269void
270lockdflt_init()
271{
272    int i;
273
274    deflockinfo.rtli_version  = RTLI_VERSION;
275    deflockinfo.lock_create   = def_lock_create;
276    deflockinfo.lock_destroy  = def_lock_destroy;
277    deflockinfo.rlock_acquire = def_rlock_acquire;
278    deflockinfo.wlock_acquire = def_wlock_acquire;
279    deflockinfo.lock_release  = def_lock_release;
280    deflockinfo.thread_set_flag = def_thread_set_flag;
281    deflockinfo.thread_clr_flag = def_thread_clr_flag;
282    deflockinfo.at_fork = NULL;
283
284    for (i = 0; i < RTLD_LOCK_CNT; i++) {
285	    rtld_locks[i].mask   = (1 << i);
286	    rtld_locks[i].handle = NULL;
287    }
288
289    memcpy(&lockinfo, &deflockinfo, sizeof(lockinfo));
290    _rtld_thread_init(NULL);
291    /*
292     * Construct a mask to block all signals except traps which might
293     * conceivably be generated within the dynamic linker itself.
294     */
295    sigfillset(&fullsigmask);
296    sigdelset(&fullsigmask, SIGILL);
297    sigdelset(&fullsigmask, SIGTRAP);
298    sigdelset(&fullsigmask, SIGABRT);
299    sigdelset(&fullsigmask, SIGEMT);
300    sigdelset(&fullsigmask, SIGFPE);
301    sigdelset(&fullsigmask, SIGBUS);
302    sigdelset(&fullsigmask, SIGSEGV);
303    sigdelset(&fullsigmask, SIGSYS);
304}
305
306/*
307 * Callback function to allow threads implementation to
308 * register their own locking primitives if the default
309 * one is not suitable.
310 * The current context should be the only context
311 * executing at the invocation time.
312 */
313void
314_rtld_thread_init(struct RtldLockInfo *pli)
315{
316	int flags, i;
317	void *locks[RTLD_LOCK_CNT];
318
319	/* disable all locking while this function is running */
320	flags =	thread_mask_set(~0);
321
322	if (pli == NULL)
323		pli = &deflockinfo;
324
325
326	for (i = 0; i < RTLD_LOCK_CNT; i++)
327		if ((locks[i] = pli->lock_create()) == NULL)
328			break;
329
330	if (i < RTLD_LOCK_CNT) {
331		while (--i >= 0)
332			pli->lock_destroy(locks[i]);
333		abort();
334	}
335
336	for (i = 0; i < RTLD_LOCK_CNT; i++) {
337		if (rtld_locks[i].handle == NULL)
338			continue;
339		if (flags & rtld_locks[i].mask)
340			lockinfo.lock_release(rtld_locks[i].handle);
341		lockinfo.lock_destroy(rtld_locks[i].handle);
342	}
343
344	for (i = 0; i < RTLD_LOCK_CNT; i++) {
345		rtld_locks[i].handle = locks[i];
346		if (flags & rtld_locks[i].mask)
347			pli->wlock_acquire(rtld_locks[i].handle);
348	}
349
350	lockinfo.lock_create = pli->lock_create;
351	lockinfo.lock_destroy = pli->lock_destroy;
352	lockinfo.rlock_acquire = pli->rlock_acquire;
353	lockinfo.wlock_acquire = pli->wlock_acquire;
354	lockinfo.lock_release  = pli->lock_release;
355	lockinfo.thread_set_flag = pli->thread_set_flag;
356	lockinfo.thread_clr_flag = pli->thread_clr_flag;
357	lockinfo.at_fork = pli->at_fork;
358
359	/* restore thread locking state, this time with new locks */
360	thread_mask_clear(~0);
361	thread_mask_set(flags);
362	dbg("_rtld_thread_init: done");
363}
364
365void
366_rtld_atfork_pre(int *locks)
367{
368	RtldLockState ls[2];
369
370	wlock_acquire(rtld_phdr_lock, &ls[0]);
371	rlock_acquire(rtld_bind_lock, &ls[1]);
372
373	/* XXXKIB: I am really sorry for this. */
374	locks[0] = ls[1].lockstate;
375	locks[2] = ls[0].lockstate;
376}
377
378void
379_rtld_atfork_post(int *locks)
380{
381	RtldLockState ls[2];
382
383	bzero(ls, sizeof(ls));
384	ls[0].lockstate = locks[2];
385	ls[1].lockstate = locks[0];
386	lock_release(rtld_bind_lock, &ls[1]);
387	lock_release(rtld_phdr_lock, &ls[0]);
388}
389