]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - lib/libthr/thread/thr_rtld.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / lib / libthr / thread / thr_rtld.c
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 <sys/mman.h>
35 #include <link.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "rtld_lock.h"
40 #include "thr_private.h"
41
42 #undef errno
43 extern int errno;
44
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 *);
52
53 struct rtld_lock {
54         struct  urwlock lock;
55         char            _pad[CACHE_LINE_SIZE - sizeof(struct urwlock)];
56 };
57
58 static struct rtld_lock lock_place[MAX_RTLD_LOCKS] __aligned(CACHE_LINE_SIZE);
59 static int busy_places;
60
61 static void *
62 _thr_rtld_lock_create(void)
63 {
64         int locki;
65         struct rtld_lock *l;
66         static const char fail[] = "_thr_rtld_lock_create failed\n";
67
68         for (locki = 0; locki < MAX_RTLD_LOCKS; locki++) {
69                 if ((busy_places & (1 << locki)) == 0)
70                         break;
71         }
72         if (locki == MAX_RTLD_LOCKS) {
73                 write(2, fail, sizeof(fail) - 1);
74                 return (NULL);
75         }
76         busy_places |= (1 << locki);
77
78         l = &lock_place[locki];
79         l->lock.rw_flags = URWLOCK_PREFER_READER;
80         return (l);
81 }
82
83 static void
84 _thr_rtld_lock_destroy(void *lock)
85 {
86         int locki;
87         size_t i;
88
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);
93 }
94
95 #define SAVE_ERRNO()    {                       \
96         if (curthread != _thr_initial)          \
97                 errsave = curthread->error;     \
98         else                                    \
99                 errsave = errno;                \
100 }
101
102 #define RESTORE_ERRNO() {                       \
103         if (curthread != _thr_initial)          \
104                 curthread->error = errsave;     \
105         else                                    \
106                 errno = errsave;                \
107 }
108
109 static void
110 _thr_rtld_rlock_acquire(void *lock)
111 {
112         struct pthread          *curthread;
113         struct rtld_lock        *l;
114         int                     errsave;
115
116         curthread = _get_curthread();
117         SAVE_ERRNO();
118         l = (struct rtld_lock *)lock;
119
120         THR_CRITICAL_ENTER(curthread);
121         while (_thr_rwlock_rdlock(&l->lock, 0, NULL) != 0)
122                 ;
123         curthread->rdlock_count++;
124         RESTORE_ERRNO();
125 }
126
127 static void
128 _thr_rtld_wlock_acquire(void *lock)
129 {
130         struct pthread          *curthread;
131         struct rtld_lock        *l;
132         int                     errsave;
133
134         curthread = _get_curthread();
135         SAVE_ERRNO();
136         l = (struct rtld_lock *)lock;
137
138         THR_CRITICAL_ENTER(curthread);
139         while (_thr_rwlock_wrlock(&l->lock, NULL) != 0)
140                 ;
141         RESTORE_ERRNO();
142 }
143
144 static void
145 _thr_rtld_lock_release(void *lock)
146 {
147         struct pthread          *curthread;
148         struct rtld_lock        *l;
149         int32_t                 state;
150         int                     errsave;
151
152         curthread = _get_curthread();
153         SAVE_ERRNO();
154         l = (struct rtld_lock *)lock;
155         
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);
161         }
162         RESTORE_ERRNO();
163 }
164
165 static int
166 _thr_rtld_set_flag(int mask __unused)
167 {
168         /*
169          * The caller's code in rtld-elf is broken, it is not signal safe,
170          * just return zero to fool it.
171          */
172         return (0);
173 }
174
175 static int
176 _thr_rtld_clr_flag(int mask __unused)
177 {
178         return (0);
179 }
180
181 void
182 _thr_rtld_init(void)
183 {
184         struct RtldLockInfo     li;
185         struct pthread          *curthread;
186         long dummy = -1;
187
188         curthread = _get_curthread();
189
190         /* force to resolve _umtx_op PLT */
191         _umtx_op_err((struct umtx *)&dummy, UMTX_OP_WAKE, 1, 0, 0);
192         
193         /* force to resolve errno() PLT */
194         __error();
195
196         /* force to resolve memcpy PLT */
197         memcpy(&dummy, &dummy, sizeof(dummy));
198
199         mprotect(NULL, 0, 0);
200         _rtld_get_stack_prot();
201
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;
209         li.at_fork = NULL;
210         
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);
215 }
216
217 void
218 _thr_rtld_fini(void)
219 {
220         struct pthread  *curthread;
221
222         curthread = _get_curthread();
223         _thr_signal_block(curthread);
224         _rtld_thread_init(NULL);
225         _thr_signal_unblock(curthread);
226 }