2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006, David Xu <davidxu@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * A lockless rwlock for rtld.
35 #include <sys/cdefs.h>
37 #include <sys/syscall.h>
42 #include "libc_private.h"
43 #include "rtld_lock.h"
44 #include "thr_private.h"
49 static int _thr_rtld_clr_flag(int);
50 static void *_thr_rtld_lock_create(void);
51 static void _thr_rtld_lock_destroy(void *);
52 static void _thr_rtld_lock_release(void *);
53 static void _thr_rtld_rlock_acquire(void *);
54 static int _thr_rtld_set_flag(int);
55 static void _thr_rtld_wlock_acquire(void *);
59 char _pad[CACHE_LINE_SIZE - sizeof(struct urwlock)];
62 static struct rtld_lock lock_place[MAX_RTLD_LOCKS] __aligned(CACHE_LINE_SIZE);
63 static int busy_places;
66 _thr_rtld_lock_create(void)
70 static const char fail[] = "_thr_rtld_lock_create failed\n";
72 for (locki = 0; locki < MAX_RTLD_LOCKS; locki++) {
73 if ((busy_places & (1 << locki)) == 0)
76 if (locki == MAX_RTLD_LOCKS) {
77 write(2, fail, sizeof(fail) - 1);
80 busy_places |= (1 << locki);
82 l = &lock_place[locki];
83 l->lock.rw_flags = URWLOCK_PREFER_READER;
88 _thr_rtld_lock_destroy(void *lock)
93 locki = (struct rtld_lock *)lock - &lock_place[0];
94 for (i = 0; i < sizeof(struct rtld_lock); ++i)
95 ((char *)lock)[i] = 0;
96 busy_places &= ~(1 << locki);
99 #define SAVE_ERRNO() { \
100 if (curthread != _thr_initial) \
101 errsave = curthread->error; \
106 #define RESTORE_ERRNO() { \
107 if (curthread != _thr_initial) \
108 curthread->error = errsave; \
114 _thr_rtld_rlock_acquire(void *lock)
116 struct pthread *curthread;
120 curthread = _get_curthread();
122 l = (struct rtld_lock *)lock;
124 THR_CRITICAL_ENTER(curthread);
125 while (_thr_rwlock_rdlock(&l->lock, 0, NULL) != 0)
127 curthread->rdlock_count++;
132 _thr_rtld_wlock_acquire(void *lock)
134 struct pthread *curthread;
138 curthread = _get_curthread();
140 l = (struct rtld_lock *)lock;
142 THR_CRITICAL_ENTER(curthread);
143 while (_thr_rwlock_wrlock(&l->lock, NULL) != 0)
149 _thr_rtld_lock_release(void *lock)
151 struct pthread *curthread;
156 curthread = _get_curthread();
158 l = (struct rtld_lock *)lock;
160 state = l->lock.rw_state;
161 if (_thr_rwlock_unlock(&l->lock) == 0) {
162 if ((state & URWLOCK_WRITE_OWNER) == 0)
163 curthread->rdlock_count--;
164 THR_CRITICAL_LEAVE(curthread);
170 _thr_rtld_set_flag(int mask __unused)
173 * The caller's code in rtld-elf is broken, it is not signal safe,
174 * just return zero to fool it.
180 _thr_rtld_clr_flag(int mask __unused)
186 * ABI bug workaround: This symbol must be present for rtld to accept
187 * RTLI_VERSION from RtldLockInfo
189 extern char _pli_rtli_version;
190 char _pli_rtli_version;
193 _thr_dlerror_loc(void)
195 struct pthread *curthread;
197 curthread = _get_curthread();
198 return (curthread->dlerror_msg);
202 _thr_dlerror_seen(void)
204 struct pthread *curthread;
206 curthread = _get_curthread();
207 return (&curthread->dlerror_seen);
213 struct RtldLockInfo li;
214 struct pthread *curthread;
219 curthread = _get_curthread();
221 /* force to resolve _umtx_op PLT */
222 _umtx_op_err((struct umtx *)&dummy, UMTX_OP_WAKE, 1, 0, 0);
224 /* force to resolve errno() PLT */
227 /* force to resolve memcpy PLT */
228 memcpy(&dummy, &dummy, sizeof(dummy));
230 mprotect(NULL, 0, 0);
231 _rtld_get_stack_prot();
233 li.rtli_version = RTLI_VERSION;
234 li.lock_create = _thr_rtld_lock_create;
235 li.lock_destroy = _thr_rtld_lock_destroy;
236 li.rlock_acquire = _thr_rtld_rlock_acquire;
237 li.wlock_acquire = _thr_rtld_wlock_acquire;
238 li.lock_release = _thr_rtld_lock_release;
239 li.thread_set_flag = _thr_rtld_set_flag;
240 li.thread_clr_flag = _thr_rtld_clr_flag;
242 li.dlerror_loc = _thr_dlerror_loc;
243 li.dlerror_loc_sz = sizeof(curthread->dlerror_msg);
244 li.dlerror_seen = _thr_dlerror_seen;
247 * Preresolve the symbols needed for the fork interposer. We
248 * call _rtld_atfork_pre() and _rtld_atfork_post() with NULL
249 * argument to indicate that no actual locking inside the
250 * functions should happen. Neither rtld compat locks nor
251 * libthr rtld locks cannot work there:
252 * - compat locks do not handle the case of two locks taken
253 * in write mode (the signal mask for the thread is corrupted);
254 * - libthr locks would work, but locked rtld_bind_lock prevents
255 * symbol resolution for _rtld_atfork_post.
257 _rtld_atfork_pre(NULL);
258 _rtld_atfork_post(NULL);
264 /* mask signals, also force to resolve __sys_sigprocmask PLT */
265 _thr_signal_block(curthread);
266 _rtld_thread_init(&li);
267 _thr_signal_unblock(curthread);
268 _thr_signal_block_check_fast();
269 _thr_signal_block_setup(curthread);
271 uc_len = __getcontextx_size();
274 __fillcontextx2((char *)uc);