xref: /freebsd/lib/libthr/thread/thr_rtld.c (revision a3cf0ef5a295c885c895fabfd56470c0d1db322d)
1 /*
2  * Copyright (c) 2006, David Xu <davidxu@freebsd.org>
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 unmodified, this list of conditions, and the following
10  *    disclaimer.
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.
14  *
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.
25  *
26  * $FreeBSD$
27  *
28  */
29 
30  /*
31   * A lockless rwlock for rtld.
32   */
33 #include <sys/cdefs.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "rtld_lock.h"
38 #include "thr_private.h"
39 
40 #undef errno
41 extern int errno;
42 
43 static int	_thr_rtld_clr_flag(int);
44 static void	*_thr_rtld_lock_create(void);
45 static void	_thr_rtld_lock_destroy(void *);
46 static void	_thr_rtld_lock_release(void *);
47 static void	_thr_rtld_rlock_acquire(void *);
48 static int	_thr_rtld_set_flag(int);
49 static void	_thr_rtld_wlock_acquire(void *);
50 
51 struct rtld_lock {
52 	struct	urwlock	lock;
53 	char		_pad[CACHE_LINE_SIZE - sizeof(struct urwlock)];
54 };
55 
56 static struct rtld_lock lock_place[MAX_RTLD_LOCKS] __aligned(CACHE_LINE_SIZE);
57 static int busy_places;
58 
59 static void *
60 _thr_rtld_lock_create(void)
61 {
62 	int locki;
63 	struct rtld_lock *l;
64 	static const char fail[] = "_thr_rtld_lock_create failed\n";
65 
66 	for (locki = 0; locki < MAX_RTLD_LOCKS; locki++) {
67 		if ((busy_places & (1 << locki)) == 0)
68 			break;
69 	}
70 	if (locki == MAX_RTLD_LOCKS) {
71 		write(2, fail, sizeof(fail) - 1);
72 		return (NULL);
73 	}
74 	busy_places |= (1 << locki);
75 
76 	l = &lock_place[locki];
77 	l->lock.rw_flags = URWLOCK_PREFER_READER;
78 	return (l);
79 }
80 
81 static void
82 _thr_rtld_lock_destroy(void *lock)
83 {
84 	int locki;
85 	size_t i;
86 
87 	locki = (struct rtld_lock *)lock - &lock_place[0];
88 	for (i = 0; i < sizeof(struct rtld_lock); ++i)
89 		((char *)lock)[i] = 0;
90 	busy_places &= ~(1 << locki);
91 }
92 
93 #define SAVE_ERRNO()	{			\
94 	if (curthread != _thr_initial)		\
95 		errsave = curthread->error;	\
96 	else					\
97 		errsave = errno;		\
98 }
99 
100 #define RESTORE_ERRNO()	{ 			\
101 	if (curthread != _thr_initial)  	\
102 		curthread->error = errsave;	\
103 	else					\
104 		errno = errsave;		\
105 }
106 
107 static void
108 _thr_rtld_rlock_acquire(void *lock)
109 {
110 	struct pthread		*curthread;
111 	struct rtld_lock	*l;
112 	int			errsave;
113 
114 	curthread = _get_curthread();
115 	SAVE_ERRNO();
116 	l = (struct rtld_lock *)lock;
117 
118 	THR_CRITICAL_ENTER(curthread);
119 	while (_thr_rwlock_rdlock(&l->lock, 0, NULL) != 0)
120 		;
121 	curthread->rdlock_count++;
122 	RESTORE_ERRNO();
123 }
124 
125 static void
126 _thr_rtld_wlock_acquire(void *lock)
127 {
128 	struct pthread		*curthread;
129 	struct rtld_lock	*l;
130 	int			errsave;
131 
132 	curthread = _get_curthread();
133 	SAVE_ERRNO();
134 	l = (struct rtld_lock *)lock;
135 
136 	THR_CRITICAL_ENTER(curthread);
137 	while (_thr_rwlock_wrlock(&l->lock, NULL) != 0)
138 		;
139 	RESTORE_ERRNO();
140 }
141 
142 static void
143 _thr_rtld_lock_release(void *lock)
144 {
145 	struct pthread		*curthread;
146 	struct rtld_lock	*l;
147 	int32_t			state;
148 	int			errsave;
149 
150 	curthread = _get_curthread();
151 	SAVE_ERRNO();
152 	l = (struct rtld_lock *)lock;
153 
154 	state = l->lock.rw_state;
155 	if (_thr_rwlock_unlock(&l->lock) == 0) {
156 		if ((state & URWLOCK_WRITE_OWNER) == 0)
157 			curthread->rdlock_count--;
158 		THR_CRITICAL_LEAVE(curthread);
159 	}
160 	RESTORE_ERRNO();
161 }
162 
163 static int
164 _thr_rtld_set_flag(int mask __unused)
165 {
166 	/*
167 	 * The caller's code in rtld-elf is broken, it is not signal safe,
168 	 * just return zero to fool it.
169 	 */
170 	return (0);
171 }
172 
173 static int
174 _thr_rtld_clr_flag(int mask __unused)
175 {
176 	return (0);
177 }
178 
179 void
180 _thr_rtld_init(void)
181 {
182 	struct RtldLockInfo	li;
183 	struct pthread		*curthread;
184 	long dummy = -1;
185 
186 	curthread = _get_curthread();
187 
188 	/* force to resolve _umtx_op PLT */
189 	_umtx_op_err((struct umtx *)&dummy, UMTX_OP_WAKE, 1, 0, 0);
190 
191 	/* force to resolve errno() PLT */
192 	__error();
193 
194 	/* force to resolve memcpy PLT */
195 	memcpy(&dummy, &dummy, sizeof(dummy));
196 
197 	li.lock_create  = _thr_rtld_lock_create;
198 	li.lock_destroy = _thr_rtld_lock_destroy;
199 	li.rlock_acquire = _thr_rtld_rlock_acquire;
200 	li.wlock_acquire = _thr_rtld_wlock_acquire;
201 	li.lock_release  = _thr_rtld_lock_release;
202 	li.thread_set_flag = _thr_rtld_set_flag;
203 	li.thread_clr_flag = _thr_rtld_clr_flag;
204 	li.at_fork = NULL;
205 
206 	/* mask signals, also force to resolve __sys_sigprocmask PLT */
207 	_thr_signal_block(curthread);
208 	_rtld_thread_init(&li);
209 	_thr_signal_unblock(curthread);
210 }
211 
212 void
213 _thr_rtld_fini(void)
214 {
215 	struct pthread	*curthread;
216 
217 	curthread = _get_curthread();
218 	_thr_signal_block(curthread);
219 	_rtld_thread_init(NULL);
220 	_thr_signal_unblock(curthread);
221 }
222