xref: /dragonfly/libexec/rtld-elf/rtld_lock.c (revision 50caca1a)
1fcf53d9bSJohn Marino /*-
2fcf53d9bSJohn Marino  * Copyright 1999, 2000 John D. Polstra.
3fcf53d9bSJohn Marino  * All rights reserved.
4fcf53d9bSJohn Marino  *
5fcf53d9bSJohn Marino  * Redistribution and use in source and binary forms, with or without
6fcf53d9bSJohn Marino  * modification, are permitted provided that the following conditions
7fcf53d9bSJohn Marino  * are met:
8fcf53d9bSJohn Marino  * 1. Redistributions of source code must retain the above copyright
9fcf53d9bSJohn Marino  *    notice, this list of conditions and the following disclaimer.
10fcf53d9bSJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
11fcf53d9bSJohn Marino  *    notice, this list of conditions and the following disclaimer in the
12fcf53d9bSJohn Marino  *    documentation and/or other materials provided with the distribution.
13fcf53d9bSJohn Marino  *
14fcf53d9bSJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15fcf53d9bSJohn Marino  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16fcf53d9bSJohn Marino  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17fcf53d9bSJohn Marino  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18fcf53d9bSJohn Marino  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19fcf53d9bSJohn Marino  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20fcf53d9bSJohn Marino  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21fcf53d9bSJohn Marino  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22fcf53d9bSJohn Marino  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23fcf53d9bSJohn Marino  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24fcf53d9bSJohn Marino  *
25fcf53d9bSJohn Marino  *	from: FreeBSD: src/libexec/rtld-elf/sparc64/lockdflt.c,v 1.3 2002/10/09
26f4f4bfd5SJohn Marino  * $FreeBSD$
27fcf53d9bSJohn Marino  */
28fcf53d9bSJohn Marino 
29fcf53d9bSJohn Marino /*
30fcf53d9bSJohn Marino  * Thread locking implementation for the dynamic linker.
31fcf53d9bSJohn Marino  *
32fcf53d9bSJohn Marino  * We use the "simple, non-scalable reader-preference lock" from:
33fcf53d9bSJohn Marino  *
34fcf53d9bSJohn Marino  *   J. M. Mellor-Crummey and M. L. Scott. "Scalable Reader-Writer
35fcf53d9bSJohn Marino  *   Synchronization for Shared-Memory Multiprocessors." 3rd ACM Symp. on
36fcf53d9bSJohn Marino  *   Principles and Practice of Parallel Programming, April 1991.
37fcf53d9bSJohn Marino  *
38fcf53d9bSJohn Marino  * In this algorithm the lock is a single word.  Its low-order bit is
39fcf53d9bSJohn Marino  * set when a writer holds the lock.  The remaining high-order bits
40fcf53d9bSJohn Marino  * contain a count of readers desiring the lock.  The algorithm requires
41fcf53d9bSJohn Marino  * atomic "compare_and_store" and "add" operations, which we implement
42fcf53d9bSJohn Marino  * using assembly language sequences in "rtld_start.S".
43fcf53d9bSJohn Marino  */
44fcf53d9bSJohn Marino 
45fcf53d9bSJohn Marino #include <sys/param.h>
46fcf53d9bSJohn Marino #include <signal.h>
47fcf53d9bSJohn Marino #include <stdlib.h>
48fcf53d9bSJohn Marino #include <time.h>
49fcf53d9bSJohn Marino 
5098247283SMatthew Dillon #include <stdio.h>
5198247283SMatthew Dillon #include <sys/file.h>
5298247283SMatthew Dillon 
53*50caca1aSMatthew Dillon #include <machine/sysarch.h>
54*50caca1aSMatthew Dillon #include <machine/tls.h>
55*50caca1aSMatthew Dillon 
56fcf53d9bSJohn Marino #include "debug.h"
57fcf53d9bSJohn Marino #include "rtld.h"
58fcf53d9bSJohn Marino #include "rtld_machdep.h"
59fcf53d9bSJohn Marino 
6098247283SMatthew Dillon extern pid_t __sys_getpid(void);
6198247283SMatthew Dillon 
62fcf53d9bSJohn Marino #define WAFLAG		0x1	/* A writer holds the lock */
63*50caca1aSMatthew Dillon #define SLFLAG		0x2	/* Sleep pending on lock */
64*50caca1aSMatthew Dillon #define RC_INCR		0x4	/* Adjusts count of readers desiring lock */
65fcf53d9bSJohn Marino 
6698247283SMatthew Dillon struct Struct_Lock {
67fcf53d9bSJohn Marino 	volatile u_int lock;
6898247283SMatthew Dillon 	int count;		/* recursion (exclusive) */
69*50caca1aSMatthew Dillon 	void *owner;		/* owner (exclusive) - tls_get_tcb() */
7098247283SMatthew Dillon 	sigset_t savesigmask;	/* first exclusive owner sets mask */
7198247283SMatthew Dillon } __cachealign;
72fcf53d9bSJohn Marino 
7398247283SMatthew Dillon #define cpu_ccfence()	__asm __volatile("" : : : "memory")
74fcf53d9bSJohn Marino 
7598247283SMatthew Dillon static sigset_t fullsigmask;
7698247283SMatthew Dillon 
7798247283SMatthew Dillon struct Struct_Lock phdr_lock;
7898247283SMatthew Dillon struct Struct_Lock bind_lock;
7998247283SMatthew Dillon struct Struct_Lock libc_lock;
8098247283SMatthew Dillon 
8198247283SMatthew Dillon rtld_lock_t	rtld_phdr_lock = &phdr_lock;
8298247283SMatthew Dillon rtld_lock_t	rtld_bind_lock = &bind_lock;
8398247283SMatthew Dillon rtld_lock_t	rtld_libc_lock = &libc_lock;
8498247283SMatthew Dillon 
85*50caca1aSMatthew Dillon static int _rtld_isthreaded;
86*50caca1aSMatthew Dillon 
87*50caca1aSMatthew Dillon void _rtld_setthreaded(int threaded);
88*50caca1aSMatthew Dillon 
89*50caca1aSMatthew Dillon void
_rtld_setthreaded(int threaded)90*50caca1aSMatthew Dillon _rtld_setthreaded(int threaded)
91*50caca1aSMatthew Dillon {
92*50caca1aSMatthew Dillon 	_rtld_isthreaded = threaded;
93*50caca1aSMatthew Dillon }
94*50caca1aSMatthew Dillon 
95*50caca1aSMatthew Dillon static __inline
96*50caca1aSMatthew Dillon void *
myid(void)97*50caca1aSMatthew Dillon myid(void)
98*50caca1aSMatthew Dillon {
99*50caca1aSMatthew Dillon 	if (_rtld_isthreaded) {
100*50caca1aSMatthew Dillon 		return(tls_get_tcb());
101*50caca1aSMatthew Dillon 	}
102*50caca1aSMatthew Dillon 	return (void *)(intptr_t)1;
103*50caca1aSMatthew Dillon }
104*50caca1aSMatthew Dillon 
10598247283SMatthew Dillon void
rlock_acquire(rtld_lock_t lock,RtldLockState * state)10698247283SMatthew Dillon rlock_acquire(rtld_lock_t lock, RtldLockState *state)
107fcf53d9bSJohn Marino {
108*50caca1aSMatthew Dillon 	void *tid = myid();
10998247283SMatthew Dillon 	int v;
110fcf53d9bSJohn Marino 
11198247283SMatthew Dillon 	v = lock->lock;
11298247283SMatthew Dillon 	cpu_ccfence();
113fcf53d9bSJohn Marino 	for (;;) {
11498247283SMatthew Dillon 		if ((v & WAFLAG) == 0) {
11598247283SMatthew Dillon 			if (atomic_fcmpset_int(&lock->lock, &v, v + RC_INCR)) {
11698247283SMatthew Dillon 				state->lockstate = RTLD_LOCK_RLOCKED;
117fcf53d9bSJohn Marino 				break;
11898247283SMatthew Dillon 			}
11998247283SMatthew Dillon 		} else {
120*50caca1aSMatthew Dillon 			if (lock->owner == tid) {
12198247283SMatthew Dillon 				++lock->count;
12298247283SMatthew Dillon 				state->lockstate = RTLD_LOCK_WLOCKED;
12398247283SMatthew Dillon 				break;
12498247283SMatthew Dillon 			}
125*50caca1aSMatthew Dillon 			if (atomic_fcmpset_int(&lock->lock, &v, v | SLFLAG)) {
12698247283SMatthew Dillon 				umtx_sleep(&lock->lock, v, 0);
12798247283SMatthew Dillon 			}
12898247283SMatthew Dillon 		}
129*50caca1aSMatthew Dillon 		cpu_ccfence();
130*50caca1aSMatthew Dillon 	}
13198247283SMatthew Dillon }
13298247283SMatthew Dillon 
13398247283SMatthew Dillon void
wlock_acquire(rtld_lock_t lock,RtldLockState * state)13498247283SMatthew Dillon wlock_acquire(rtld_lock_t lock, RtldLockState *state)
13598247283SMatthew Dillon {
136*50caca1aSMatthew Dillon 	void *tid = myid();
13798247283SMatthew Dillon 	sigset_t tmp_oldsigmask;
138*50caca1aSMatthew Dillon 	int v;
13998247283SMatthew Dillon 
140*50caca1aSMatthew Dillon 	if (lock->owner == tid) {
14198247283SMatthew Dillon 		++lock->count;
14298247283SMatthew Dillon 		state->lockstate = RTLD_LOCK_WLOCKED;
14398247283SMatthew Dillon 		return;
14498247283SMatthew Dillon 	}
14598247283SMatthew Dillon 
14698247283SMatthew Dillon 	sigprocmask(SIG_BLOCK, &fullsigmask, &tmp_oldsigmask);
147*50caca1aSMatthew Dillon 	v = lock->lock;
14898247283SMatthew Dillon 	for (;;) {
149*50caca1aSMatthew Dillon 		if ((v & ~SLFLAG) == 0) {
150*50caca1aSMatthew Dillon 			if (atomic_fcmpset_int(&lock->lock, &v, WAFLAG))
15198247283SMatthew Dillon 				break;
152*50caca1aSMatthew Dillon 		} else {
153*50caca1aSMatthew Dillon 			if (atomic_fcmpset_int(&lock->lock, &v, v | SLFLAG)) {
154*50caca1aSMatthew Dillon 				umtx_sleep(&lock->lock, v, 0);
15598247283SMatthew Dillon 			}
156*50caca1aSMatthew Dillon 		}
157*50caca1aSMatthew Dillon 		cpu_ccfence();
158*50caca1aSMatthew Dillon 	}
159*50caca1aSMatthew Dillon 	lock->owner = tid;
16098247283SMatthew Dillon 	lock->count = 1;
16198247283SMatthew Dillon 	lock->savesigmask = tmp_oldsigmask;
16298247283SMatthew Dillon 	state->lockstate = RTLD_LOCK_WLOCKED;
16398247283SMatthew Dillon }
16498247283SMatthew Dillon 
16598247283SMatthew Dillon void
lock_release(rtld_lock_t lock,RtldLockState * state)16698247283SMatthew Dillon lock_release(rtld_lock_t lock, RtldLockState *state)
16798247283SMatthew Dillon {
16898247283SMatthew Dillon 	sigset_t tmp_oldsigmask;
16998247283SMatthew Dillon 	int v;
17098247283SMatthew Dillon 
17198247283SMatthew Dillon 	if (state->lockstate == RTLD_LOCK_UNLOCKED)
17298247283SMatthew Dillon 		return;
17398247283SMatthew Dillon 	if ((lock->lock & WAFLAG) == 0) {
17498247283SMatthew Dillon 		v = atomic_fetchadd_int(&lock->lock, -RC_INCR) - RC_INCR;
175*50caca1aSMatthew Dillon 		if (v == SLFLAG) {
176*50caca1aSMatthew Dillon 			atomic_clear_int(&lock->lock, SLFLAG);
17798247283SMatthew Dillon 			umtx_wakeup(&lock->lock, 0);
178*50caca1aSMatthew Dillon 		}
17998247283SMatthew Dillon 	} else if (--lock->count == 0) {
18098247283SMatthew Dillon 		tmp_oldsigmask = lock->savesigmask;
181*50caca1aSMatthew Dillon 		lock->owner = NULL;
18298247283SMatthew Dillon 		v = atomic_fetchadd_int(&lock->lock, -WAFLAG) - WAFLAG;
183*50caca1aSMatthew Dillon 		if (v == SLFLAG) {
184*50caca1aSMatthew Dillon 			atomic_clear_int(&lock->lock, SLFLAG);
18598247283SMatthew Dillon 			umtx_wakeup(&lock->lock, 0);
186*50caca1aSMatthew Dillon 		}
187fcf53d9bSJohn Marino 		sigprocmask(SIG_SETMASK, &tmp_oldsigmask, NULL);
188fcf53d9bSJohn Marino 	}
18998247283SMatthew Dillon 	state->lockstate = RTLD_LOCK_UNLOCKED;
190fcf53d9bSJohn Marino }
191fcf53d9bSJohn Marino 
19298247283SMatthew Dillon static
193fcf53d9bSJohn Marino void
lock_reset(rtld_lock_t lock)19498247283SMatthew Dillon lock_reset(rtld_lock_t lock)
195fcf53d9bSJohn Marino {
19698247283SMatthew Dillon 	memset(lock, 0, sizeof(*lock));
197fcf53d9bSJohn Marino }
198fcf53d9bSJohn Marino 
199fcf53d9bSJohn Marino void
lock_upgrade(rtld_lock_t lock,RtldLockState * state)20098247283SMatthew Dillon lock_upgrade(rtld_lock_t lock, RtldLockState *state)
201fcf53d9bSJohn Marino {
20298247283SMatthew Dillon 	if (state == NULL)
203fcf53d9bSJohn Marino 		return;
20498247283SMatthew Dillon 	if (state->lockstate == RTLD_LOCK_RLOCKED) {
20598247283SMatthew Dillon 		lock_release(lock, state);
20698247283SMatthew Dillon 		wlock_acquire(lock, state);
207fcf53d9bSJohn Marino 	}
208fcf53d9bSJohn Marino }
209fcf53d9bSJohn Marino 
210fcf53d9bSJohn Marino void
lock_restart_for_upgrade(RtldLockState * state)21198247283SMatthew Dillon lock_restart_for_upgrade(RtldLockState *state)
212fcf53d9bSJohn Marino {
21398247283SMatthew Dillon 	if (state == NULL)
214fcf53d9bSJohn Marino 		return;
21598247283SMatthew Dillon 	switch (state->lockstate) {
216fcf53d9bSJohn Marino 	case RTLD_LOCK_UNLOCKED:
217fcf53d9bSJohn Marino 	case RTLD_LOCK_WLOCKED:
218fcf53d9bSJohn Marino 		break;
219fcf53d9bSJohn Marino 	case RTLD_LOCK_RLOCKED:
22098247283SMatthew Dillon 		siglongjmp(state->env, 1);
221fcf53d9bSJohn Marino 		break;
222fcf53d9bSJohn Marino 	default:
223fcf53d9bSJohn Marino 		assert(0);
224fcf53d9bSJohn Marino 	}
225fcf53d9bSJohn Marino }
226fcf53d9bSJohn Marino 
227fcf53d9bSJohn Marino void
lockdflt_init(void)228472de6d1SSascha Wildner lockdflt_init(void)
229fcf53d9bSJohn Marino {
230fcf53d9bSJohn Marino 	/*
231fcf53d9bSJohn Marino 	 * Construct a mask to block all signals except traps which might
232fcf53d9bSJohn Marino 	 * conceivably be generated within the dynamic linker itself.
233fcf53d9bSJohn Marino 	 */
234fcf53d9bSJohn Marino 	sigfillset(&fullsigmask);
235fcf53d9bSJohn Marino 	sigdelset(&fullsigmask, SIGILL);
236fcf53d9bSJohn Marino 	sigdelset(&fullsigmask, SIGTRAP);
237fcf53d9bSJohn Marino 	sigdelset(&fullsigmask, SIGABRT);
238fcf53d9bSJohn Marino 	sigdelset(&fullsigmask, SIGEMT);
239fcf53d9bSJohn Marino 	sigdelset(&fullsigmask, SIGFPE);
240fcf53d9bSJohn Marino 	sigdelset(&fullsigmask, SIGBUS);
241fcf53d9bSJohn Marino 	sigdelset(&fullsigmask, SIGSEGV);
242fcf53d9bSJohn Marino 	sigdelset(&fullsigmask, SIGSYS);
24398247283SMatthew Dillon 
24498247283SMatthew Dillon 	_rtld_thread_init(NULL);
245fcf53d9bSJohn Marino }
246fcf53d9bSJohn Marino 
247fcf53d9bSJohn Marino /*
24898247283SMatthew Dillon  * (also called by pthreads)
249fcf53d9bSJohn Marino  */
250fcf53d9bSJohn Marino void
_rtld_thread_init(void * dummy __unused)25198247283SMatthew Dillon _rtld_thread_init(void *dummy __unused)
252fcf53d9bSJohn Marino {
25398247283SMatthew Dillon 	lock_reset(rtld_phdr_lock);
25498247283SMatthew Dillon 	lock_reset(rtld_bind_lock);
25598247283SMatthew Dillon 	lock_reset(rtld_libc_lock);
256e19be507SMatthew Dillon }
257fcf53d9bSJohn Marino 
25898247283SMatthew Dillon static RtldLockState fork_states[3];
259e19be507SMatthew Dillon 
260e19be507SMatthew Dillon void
_rtld_thread_prefork(void)261e19be507SMatthew Dillon _rtld_thread_prefork(void)
262e19be507SMatthew Dillon {
26398247283SMatthew Dillon 	wlock_acquire(rtld_phdr_lock, &fork_states[0]);
26498247283SMatthew Dillon 	wlock_acquire(rtld_bind_lock, &fork_states[1]);
26598247283SMatthew Dillon 	wlock_acquire(rtld_libc_lock, &fork_states[2]);
266e19be507SMatthew Dillon }
267e19be507SMatthew Dillon 
268e19be507SMatthew Dillon void
_rtld_thread_postfork(void)269e19be507SMatthew Dillon _rtld_thread_postfork(void)
270e19be507SMatthew Dillon {
27198247283SMatthew Dillon 	lock_release(rtld_libc_lock, &fork_states[2]);
27298247283SMatthew Dillon 	lock_release(rtld_bind_lock, &fork_states[1]);
27398247283SMatthew Dillon 	lock_release(rtld_phdr_lock, &fork_states[0]);
274e19be507SMatthew Dillon }
275e19be507SMatthew Dillon 
276e19be507SMatthew Dillon void
_rtld_thread_childfork(void)277e19be507SMatthew Dillon _rtld_thread_childfork(void)
278e19be507SMatthew Dillon {
27998247283SMatthew Dillon 	sigset_t tmp_oldsigmask;
28098247283SMatthew Dillon 
28198247283SMatthew Dillon 	lock_reset(rtld_phdr_lock);
28298247283SMatthew Dillon 	lock_reset(rtld_bind_lock);
28398247283SMatthew Dillon 	tmp_oldsigmask = rtld_libc_lock->savesigmask;
28498247283SMatthew Dillon 	lock_reset(rtld_libc_lock);
28598247283SMatthew Dillon 	sigprocmask(SIG_SETMASK, &tmp_oldsigmask, NULL);
286e19be507SMatthew Dillon }
287