2 * Copyright (c) 2006, David Xu <davidxu@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * A lockless rwlock for rtld.
33 #include <sys/cdefs.h>
35 #include <sys/syscall.h>
40 #include "libc_private.h"
41 #include "rtld_lock.h"
42 #include "thr_private.h"
47 static int _thr_rtld_clr_flag(int);
48 static void *_thr_rtld_lock_create(void);
49 static void _thr_rtld_lock_destroy(void *);
50 static void _thr_rtld_lock_release(void *);
51 static void _thr_rtld_rlock_acquire(void *);
52 static int _thr_rtld_set_flag(int);
53 static void _thr_rtld_wlock_acquire(void *);
57 char _pad[CACHE_LINE_SIZE - sizeof(struct urwlock)];
60 static struct rtld_lock lock_place[MAX_RTLD_LOCKS] __aligned(CACHE_LINE_SIZE);
61 static int busy_places;
64 _thr_rtld_lock_create(void)
68 static const char fail[] = "_thr_rtld_lock_create failed\n";
70 for (locki = 0; locki < MAX_RTLD_LOCKS; locki++) {
71 if ((busy_places & (1 << locki)) == 0)
74 if (locki == MAX_RTLD_LOCKS) {
75 write(2, fail, sizeof(fail) - 1);
78 busy_places |= (1 << locki);
80 l = &lock_place[locki];
81 l->lock.rw_flags = URWLOCK_PREFER_READER;
86 _thr_rtld_lock_destroy(void *lock)
91 locki = (struct rtld_lock *)lock - &lock_place[0];
92 for (i = 0; i < sizeof(struct rtld_lock); ++i)
93 ((char *)lock)[i] = 0;
94 busy_places &= ~(1 << locki);
97 #define SAVE_ERRNO() { \
98 if (curthread != _thr_initial) \
99 errsave = curthread->error; \
104 #define RESTORE_ERRNO() { \
105 if (curthread != _thr_initial) \
106 curthread->error = errsave; \
112 _thr_rtld_rlock_acquire(void *lock)
114 struct pthread *curthread;
118 curthread = _get_curthread();
120 l = (struct rtld_lock *)lock;
122 THR_CRITICAL_ENTER(curthread);
123 while (_thr_rwlock_rdlock(&l->lock, 0, NULL) != 0)
125 curthread->rdlock_count++;
130 _thr_rtld_wlock_acquire(void *lock)
132 struct pthread *curthread;
136 curthread = _get_curthread();
138 l = (struct rtld_lock *)lock;
140 THR_CRITICAL_ENTER(curthread);
141 while (_thr_rwlock_wrlock(&l->lock, NULL) != 0)
147 _thr_rtld_lock_release(void *lock)
149 struct pthread *curthread;
154 curthread = _get_curthread();
156 l = (struct rtld_lock *)lock;
158 state = l->lock.rw_state;
159 if (_thr_rwlock_unlock(&l->lock) == 0) {
160 if ((state & URWLOCK_WRITE_OWNER) == 0)
161 curthread->rdlock_count--;
162 THR_CRITICAL_LEAVE(curthread);
168 _thr_rtld_set_flag(int mask __unused)
171 * The caller's code in rtld-elf is broken, it is not signal safe,
172 * just return zero to fool it.
178 _thr_rtld_clr_flag(int mask __unused)
186 struct RtldLockInfo li;
187 struct pthread *curthread;
192 curthread = _get_curthread();
194 /* force to resolve _umtx_op PLT */
195 _umtx_op_err((struct umtx *)&dummy, UMTX_OP_WAKE, 1, 0, 0);
197 /* force to resolve errno() PLT */
200 /* force to resolve memcpy PLT */
201 memcpy(&dummy, &dummy, sizeof(dummy));
203 mprotect(NULL, 0, 0);
204 _rtld_get_stack_prot();
206 li.lock_create = _thr_rtld_lock_create;
207 li.lock_destroy = _thr_rtld_lock_destroy;
208 li.rlock_acquire = _thr_rtld_rlock_acquire;
209 li.wlock_acquire = _thr_rtld_wlock_acquire;
210 li.lock_release = _thr_rtld_lock_release;
211 li.thread_set_flag = _thr_rtld_set_flag;
212 li.thread_clr_flag = _thr_rtld_clr_flag;
216 * Preresolve the symbols needed for the fork interposer. We
217 * call _rtld_atfork_pre() and _rtld_atfork_post() with NULL
218 * argument to indicate that no actual locking inside the
219 * functions should happen. Neither rtld compat locks nor
220 * libthr rtld locks cannot work there:
221 * - compat locks do not handle the case of two locks taken
222 * in write mode (the signal mask for the thread is corrupted);
223 * - libthr locks would work, but locked rtld_bind_lock prevents
224 * symbol resolution for _rtld_atfork_post.
226 _rtld_atfork_pre(NULL);
227 _rtld_atfork_post(NULL);
233 /* mask signals, also force to resolve __sys_sigprocmask PLT */
234 _thr_signal_block(curthread);
235 _rtld_thread_init(&li);
236 _thr_signal_unblock(curthread);
238 uc_len = __getcontextx_size();
241 __fillcontextx2((char *)uc);