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.
31 * A lockless rwlock for rtld.
33 #include <sys/cdefs.h>
39 #include "rtld_lock.h"
40 #include "thr_private.h"
45 static int _thr_rtld_clr_flag(int);
46 static void *_thr_rtld_lock_create(void);
47 static void _thr_rtld_lock_destroy(void *);
48 static void _thr_rtld_lock_release(void *);
49 static void _thr_rtld_rlock_acquire(void *);
50 static int _thr_rtld_set_flag(int);
51 static void _thr_rtld_wlock_acquire(void *);
55 char _pad[CACHE_LINE_SIZE - sizeof(struct urwlock)];
58 static struct rtld_lock lock_place[MAX_RTLD_LOCKS] __aligned(CACHE_LINE_SIZE);
59 static int busy_places;
62 _thr_rtld_lock_create(void)
66 static const char fail[] = "_thr_rtld_lock_create failed\n";
68 for (locki = 0; locki < MAX_RTLD_LOCKS; locki++) {
69 if ((busy_places & (1 << locki)) == 0)
72 if (locki == MAX_RTLD_LOCKS) {
73 write(2, fail, sizeof(fail) - 1);
76 busy_places |= (1 << locki);
78 l = &lock_place[locki];
79 l->lock.rw_flags = URWLOCK_PREFER_READER;
84 _thr_rtld_lock_destroy(void *lock)
89 locki = (struct rtld_lock *)lock - &lock_place[0];
90 for (i = 0; i < sizeof(struct rtld_lock); ++i)
91 ((char *)lock)[i] = 0;
92 busy_places &= ~(1 << locki);
95 #define SAVE_ERRNO() { \
96 if (curthread != _thr_initial) \
97 errsave = curthread->error; \
102 #define RESTORE_ERRNO() { \
103 if (curthread != _thr_initial) \
104 curthread->error = errsave; \
110 _thr_rtld_rlock_acquire(void *lock)
112 struct pthread *curthread;
116 curthread = _get_curthread();
118 l = (struct rtld_lock *)lock;
120 THR_CRITICAL_ENTER(curthread);
121 while (_thr_rwlock_rdlock(&l->lock, 0, NULL) != 0)
123 curthread->rdlock_count++;
128 _thr_rtld_wlock_acquire(void *lock)
130 struct pthread *curthread;
134 curthread = _get_curthread();
136 l = (struct rtld_lock *)lock;
138 THR_CRITICAL_ENTER(curthread);
139 while (_thr_rwlock_wrlock(&l->lock, NULL) != 0)
145 _thr_rtld_lock_release(void *lock)
147 struct pthread *curthread;
152 curthread = _get_curthread();
154 l = (struct rtld_lock *)lock;
156 state = l->lock.rw_state;
157 if (_thr_rwlock_unlock(&l->lock) == 0) {
158 if ((state & URWLOCK_WRITE_OWNER) == 0)
159 curthread->rdlock_count--;
160 THR_CRITICAL_LEAVE(curthread);
166 _thr_rtld_set_flag(int mask __unused)
169 * The caller's code in rtld-elf is broken, it is not signal safe,
170 * just return zero to fool it.
176 _thr_rtld_clr_flag(int mask __unused)
184 struct RtldLockInfo li;
185 struct pthread *curthread;
188 curthread = _get_curthread();
190 /* force to resolve _umtx_op PLT */
191 _umtx_op_err((struct umtx *)&dummy, UMTX_OP_WAKE, 1, 0, 0);
193 /* force to resolve errno() PLT */
196 /* force to resolve memcpy PLT */
197 memcpy(&dummy, &dummy, sizeof(dummy));
199 mprotect(NULL, 0, 0);
200 _rtld_get_stack_prot();
202 li.lock_create = _thr_rtld_lock_create;
203 li.lock_destroy = _thr_rtld_lock_destroy;
204 li.rlock_acquire = _thr_rtld_rlock_acquire;
205 li.wlock_acquire = _thr_rtld_wlock_acquire;
206 li.lock_release = _thr_rtld_lock_release;
207 li.thread_set_flag = _thr_rtld_set_flag;
208 li.thread_clr_flag = _thr_rtld_clr_flag;
211 /* mask signals, also force to resolve __sys_sigprocmask PLT */
212 _thr_signal_block(curthread);
213 _rtld_thread_init(&li);
214 _thr_signal_unblock(curthread);
220 struct pthread *curthread;
222 curthread = _get_curthread();
223 _thr_signal_block(curthread);
224 _rtld_thread_init(NULL);
225 _thr_signal_unblock(curthread);