Sync our gcc5 manual pages with gcc-5.4.0
[dragonfly.git] / libexec / rtld-elf / rtld_lock.c
CommitLineData
fcf53d9b
JM
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
f4f4bfd5 26 * $FreeBSD$
fcf53d9b
JM
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
98247283
MD
50#include <stdio.h>
51#include <sys/file.h>
52
fcf53d9b
JM
53#include "debug.h"
54#include "rtld.h"
55#include "rtld_machdep.h"
56
98247283
MD
57extern pid_t __sys_getpid(void);
58
fcf53d9b
JM
59#define WAFLAG 0x1 /* A writer holds the lock */
60#define RC_INCR 0x2 /* Adjusts count of readers desiring lock */
61
98247283 62struct Struct_Lock {
fcf53d9b 63 volatile u_int lock;
98247283
MD
64 int tid; /* owner (exclusive) */
65 int count; /* recursion (exclusive) */
66 sigset_t savesigmask; /* first exclusive owner sets mask */
67} __cachealign;
fcf53d9b 68
98247283 69#define cpu_ccfence() __asm __volatile("" : : : "memory")
fcf53d9b 70
98247283 71static sigset_t fullsigmask;
fcf53d9b 72
98247283
MD
73struct Struct_Lock phdr_lock;
74struct Struct_Lock bind_lock;
75struct Struct_Lock libc_lock;
fcf53d9b 76
98247283
MD
77rtld_lock_t rtld_phdr_lock = &phdr_lock;
78rtld_lock_t rtld_bind_lock = &bind_lock;
79rtld_lock_t rtld_libc_lock = &libc_lock;
fcf53d9b 80
98247283
MD
81void
82rlock_acquire(rtld_lock_t lock, RtldLockState *state)
83{
84 int v;
85 int tid = 0;
86
87 v = lock->lock;
88 cpu_ccfence();
89 for (;;) {
90 if ((v & WAFLAG) == 0) {
91 if (atomic_fcmpset_int(&lock->lock, &v, v + RC_INCR)) {
92 state->lockstate = RTLD_LOCK_RLOCKED;
93 break;
94 }
95 } else {
96 if (tid == 0)
97 tid = lwp_gettid();
98 if (lock->tid == tid) {
99 ++lock->count;
100 state->lockstate = RTLD_LOCK_WLOCKED;
101 break;
102 }
103 umtx_sleep(&lock->lock, v, 0);
104 v = lock->lock;
105 cpu_ccfence();
106 }
107 }
fcf53d9b
JM
108}
109
fcf53d9b 110void
98247283 111wlock_acquire(rtld_lock_t lock, RtldLockState *state)
fcf53d9b 112{
98247283
MD
113 sigset_t tmp_oldsigmask;
114 int tid = lwp_gettid();
fcf53d9b 115
98247283
MD
116 if (lock->tid == tid) {
117 ++lock->count;
118 state->lockstate = RTLD_LOCK_WLOCKED;
fcf53d9b 119 return;
98247283 120 }
fcf53d9b 121
98247283
MD
122 sigprocmask(SIG_BLOCK, &fullsigmask, &tmp_oldsigmask);
123 for (;;) {
124 if (atomic_cmpset_acq_int(&lock->lock, 0, WAFLAG))
125 break;
126 umtx_sleep(&lock->lock, 0, 0);
fcf53d9b 127 }
98247283
MD
128 lock->tid = tid;
129 lock->count = 1;
130 lock->savesigmask = tmp_oldsigmask;
131 state->lockstate = RTLD_LOCK_WLOCKED;
fcf53d9b
JM
132}
133
134void
98247283 135lock_release(rtld_lock_t lock, RtldLockState *state)
fcf53d9b 136{
98247283
MD
137 sigset_t tmp_oldsigmask;
138 int v;
fcf53d9b 139
98247283 140 if (state->lockstate == RTLD_LOCK_UNLOCKED)
fcf53d9b 141 return;
98247283
MD
142 if ((lock->lock & WAFLAG) == 0) {
143 v = atomic_fetchadd_int(&lock->lock, -RC_INCR) - RC_INCR;
144 if (v == 0)
145 umtx_wakeup(&lock->lock, 0);
146 } else if (--lock->count == 0) {
147 tmp_oldsigmask = lock->savesigmask;
148 lock->tid = 0;
149 v = atomic_fetchadd_int(&lock->lock, -WAFLAG) - WAFLAG;
150 if (v == 0)
151 umtx_wakeup(&lock->lock, 0);
152 sigprocmask(SIG_SETMASK, &tmp_oldsigmask, NULL);
fcf53d9b 153 }
98247283 154 state->lockstate = RTLD_LOCK_UNLOCKED;
fcf53d9b
JM
155}
156
98247283 157static
fcf53d9b 158void
98247283 159lock_reset(rtld_lock_t lock)
fcf53d9b 160{
98247283 161 memset(lock, 0, sizeof(*lock));
fcf53d9b
JM
162}
163
164void
98247283 165lock_upgrade(rtld_lock_t lock, RtldLockState *state)
fcf53d9b 166{
98247283 167 if (state == NULL)
fcf53d9b 168 return;
98247283
MD
169 if (state->lockstate == RTLD_LOCK_RLOCKED) {
170 lock_release(lock, state);
171 wlock_acquire(lock, state);
172 }
fcf53d9b
JM
173}
174
175void
98247283 176lock_restart_for_upgrade(RtldLockState *state)
fcf53d9b 177{
98247283 178 if (state == NULL)
fcf53d9b 179 return;
98247283 180 switch (state->lockstate) {
fcf53d9b
JM
181 case RTLD_LOCK_UNLOCKED:
182 case RTLD_LOCK_WLOCKED:
183 break;
184 case RTLD_LOCK_RLOCKED:
98247283 185 siglongjmp(state->env, 1);
fcf53d9b
JM
186 break;
187 default:
188 assert(0);
189 }
190}
191
192void
472de6d1 193lockdflt_init(void)
fcf53d9b 194{
98247283
MD
195 /*
196 * Construct a mask to block all signals except traps which might
197 * conceivably be generated within the dynamic linker itself.
198 */
199 sigfillset(&fullsigmask);
200 sigdelset(&fullsigmask, SIGILL);
201 sigdelset(&fullsigmask, SIGTRAP);
202 sigdelset(&fullsigmask, SIGABRT);
203 sigdelset(&fullsigmask, SIGEMT);
204 sigdelset(&fullsigmask, SIGFPE);
205 sigdelset(&fullsigmask, SIGBUS);
206 sigdelset(&fullsigmask, SIGSEGV);
207 sigdelset(&fullsigmask, SIGSYS);
fcf53d9b 208
98247283 209 _rtld_thread_init(NULL);
fcf53d9b
JM
210}
211
212/*
98247283 213 * (also called by pthreads)
fcf53d9b
JM
214 */
215void
98247283 216_rtld_thread_init(void *dummy __unused)
fcf53d9b 217{
98247283
MD
218 lock_reset(rtld_phdr_lock);
219 lock_reset(rtld_bind_lock);
220 lock_reset(rtld_libc_lock);
fcf53d9b 221}
e19be507 222
98247283
MD
223static RtldLockState fork_states[3];
224
e19be507
MD
225void
226_rtld_thread_prefork(void)
227{
98247283
MD
228 wlock_acquire(rtld_phdr_lock, &fork_states[0]);
229 wlock_acquire(rtld_bind_lock, &fork_states[1]);
230 wlock_acquire(rtld_libc_lock, &fork_states[2]);
e19be507
MD
231}
232
233void
234_rtld_thread_postfork(void)
235{
98247283
MD
236 lock_release(rtld_libc_lock, &fork_states[2]);
237 lock_release(rtld_bind_lock, &fork_states[1]);
238 lock_release(rtld_phdr_lock, &fork_states[0]);
e19be507
MD
239}
240
e19be507
MD
241void
242_rtld_thread_childfork(void)
243{
98247283
MD
244 sigset_t tmp_oldsigmask;
245
246 lock_reset(rtld_phdr_lock);
247 lock_reset(rtld_bind_lock);
248 tmp_oldsigmask = rtld_libc_lock->savesigmask;
249 lock_reset(rtld_libc_lock);
250 sigprocmask(SIG_SETMASK, &tmp_oldsigmask, NULL);
e19be507 251}