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