1b6b894f6SDavid Xu /* 2b6b894f6SDavid Xu * Copyright (c) 2006, David Xu <davidxu@freebsd.org> 3b6b894f6SDavid Xu * All rights reserved. 4b6b894f6SDavid Xu * 5b6b894f6SDavid Xu * Redistribution and use in source and binary forms, with or without 6b6b894f6SDavid Xu * modification, are permitted provided that the following conditions 7b6b894f6SDavid Xu * are met: 8b6b894f6SDavid Xu * 1. Redistributions of source code must retain the above copyright 9b6b894f6SDavid Xu * notice unmodified, this list of conditions, and the following 10b6b894f6SDavid Xu * disclaimer. 11b6b894f6SDavid Xu * 2. Redistributions in binary form must reproduce the above copyright 12b6b894f6SDavid Xu * notice, this list of conditions and the following disclaimer in the 13b6b894f6SDavid Xu * documentation and/or other materials provided with the distribution. 14b6b894f6SDavid Xu * 15b6b894f6SDavid Xu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16b6b894f6SDavid Xu * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17b6b894f6SDavid Xu * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18b6b894f6SDavid Xu * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19b6b894f6SDavid Xu * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20b6b894f6SDavid Xu * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21b6b894f6SDavid Xu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22b6b894f6SDavid Xu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23b6b894f6SDavid Xu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24b6b894f6SDavid Xu * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25b6b894f6SDavid Xu * 26b6b894f6SDavid Xu * $FreeBSD$ 27b6b894f6SDavid Xu * 28b6b894f6SDavid Xu */ 29b6b894f6SDavid Xu 30b6b894f6SDavid Xu /* 31b6b894f6SDavid Xu * A lockless rwlock for rtld. 32b6b894f6SDavid Xu */ 33b6b894f6SDavid Xu #include <sys/cdefs.h> 34b6b894f6SDavid Xu #include <stdlib.h> 35b6b894f6SDavid Xu 36b6b894f6SDavid Xu #include "rtld_lock.h" 37b6b894f6SDavid Xu #include "thr_private.h" 38b6b894f6SDavid Xu 39b6b894f6SDavid Xu #define CACHE_LINE_SIZE 64 40b6b894f6SDavid Xu #define WAFLAG 0x1 41b6b894f6SDavid Xu #define RC_INCR 0x2 42b6b894f6SDavid Xu 43b6b894f6SDavid Xu static int _thr_rtld_clr_flag(int); 44b6b894f6SDavid Xu static void *_thr_rtld_lock_create(void); 45b6b894f6SDavid Xu static void _thr_rtld_lock_destroy(void *); 46b6b894f6SDavid Xu static void _thr_rtld_lock_release(void *); 47b6b894f6SDavid Xu static void _thr_rtld_rlock_acquire(void *); 48b6b894f6SDavid Xu static int _thr_rtld_set_flag(int); 49b6b894f6SDavid Xu static void _thr_rtld_wlock_acquire(void *); 50b6b894f6SDavid Xu 51b6b894f6SDavid Xu struct rtld_lock { 52b6b894f6SDavid Xu volatile int lock; 53b6b894f6SDavid Xu volatile int rd_waiters; 54b6b894f6SDavid Xu volatile int wr_waiters; 55b6b894f6SDavid Xu volatile umtx_t rd_cv; 56b6b894f6SDavid Xu volatile umtx_t wr_cv; 57b6b894f6SDavid Xu void *base; 58b6b894f6SDavid Xu }; 59b6b894f6SDavid Xu 60b6b894f6SDavid Xu static void * 61b6b894f6SDavid Xu _thr_rtld_lock_create(void) 62b6b894f6SDavid Xu { 63b6b894f6SDavid Xu void *base; 64b6b894f6SDavid Xu char *p; 65b6b894f6SDavid Xu uintptr_t r; 66b6b894f6SDavid Xu struct rtld_lock *l; 67b6b894f6SDavid Xu 68b6b894f6SDavid Xu THR_ASSERT(sizeof(struct rtld_lock) <= CACHE_LINE_SIZE, 69b6b894f6SDavid Xu "rtld_lock too large"); 70b6b894f6SDavid Xu base = calloc(1, CACHE_LINE_SIZE); 71b6b894f6SDavid Xu p = (char *)base; 72b6b894f6SDavid Xu if ((uintptr_t)p % CACHE_LINE_SIZE != 0) { 73b6b894f6SDavid Xu free(base); 74b6b894f6SDavid Xu base = calloc(1, 2 * CACHE_LINE_SIZE); 75b6b894f6SDavid Xu p = (char *)base; 76b6b894f6SDavid Xu if ((r = (uintptr_t)p % CACHE_LINE_SIZE) != 0) 77b6b894f6SDavid Xu p += CACHE_LINE_SIZE - r; 78b6b894f6SDavid Xu } 79b6b894f6SDavid Xu l = (struct rtld_lock *)p; 80b6b894f6SDavid Xu l->base = base; 81b6b894f6SDavid Xu return (l); 82b6b894f6SDavid Xu } 83b6b894f6SDavid Xu 84b6b894f6SDavid Xu static void 85b6b894f6SDavid Xu _thr_rtld_lock_destroy(void *lock) 86b6b894f6SDavid Xu { 87b6b894f6SDavid Xu struct rtld_lock *l = (struct rtld_lock *)lock; 88b6b894f6SDavid Xu free(l->base); 89b6b894f6SDavid Xu } 90b6b894f6SDavid Xu 91b6b894f6SDavid Xu static void 92b6b894f6SDavid Xu _thr_rtld_rlock_acquire(void *lock) 93b6b894f6SDavid Xu { 94b6b894f6SDavid Xu struct pthread *curthread; 95b6b894f6SDavid Xu struct rtld_lock *l; 96b6b894f6SDavid Xu umtx_t v; 97b6b894f6SDavid Xu 98b6b894f6SDavid Xu curthread = _get_curthread(); 99b6b894f6SDavid Xu l = (struct rtld_lock *)lock; 100b6b894f6SDavid Xu 101b6b894f6SDavid Xu THR_CRITICAL_ENTER(curthread); 102b6b894f6SDavid Xu atomic_add_acq_int(&l->lock, RC_INCR); 103b6b894f6SDavid Xu if (!(l->lock & WAFLAG)) 104b6b894f6SDavid Xu return; 105b6b894f6SDavid Xu v = l->rd_cv; 106b6b894f6SDavid Xu atomic_add_int(&l->rd_waiters, 1); 107b6b894f6SDavid Xu while (l->lock & WAFLAG) { 108b6b894f6SDavid Xu _thr_umtx_wait(&l->rd_cv, v, NULL); 109b6b894f6SDavid Xu v = l->rd_cv; 110b6b894f6SDavid Xu } 111b6b894f6SDavid Xu atomic_add_int(&l->rd_waiters, -1); 112b6b894f6SDavid Xu } 113b6b894f6SDavid Xu 114b6b894f6SDavid Xu static void 115b6b894f6SDavid Xu _thr_rtld_wlock_acquire(void *lock) 116b6b894f6SDavid Xu { 117b6b894f6SDavid Xu struct pthread *curthread; 118b6b894f6SDavid Xu struct rtld_lock *l; 119b6b894f6SDavid Xu umtx_t v; 120b6b894f6SDavid Xu 121b6b894f6SDavid Xu curthread = _get_curthread(); 122b6b894f6SDavid Xu l = (struct rtld_lock *)lock; 123b6b894f6SDavid Xu 124b6b894f6SDavid Xu _thr_signal_block(curthread); 125b6b894f6SDavid Xu for (;;) { 126b6b894f6SDavid Xu if (atomic_cmpset_acq_int(&l->lock, 0, WAFLAG)) 127b6b894f6SDavid Xu return; 128b6b894f6SDavid Xu v = l->wr_cv; 129b6b894f6SDavid Xu atomic_add_int(&l->wr_waiters, 1); 130b6b894f6SDavid Xu while (l->lock != 0) { 131b6b894f6SDavid Xu _thr_umtx_wait(&l->wr_cv, v, NULL); 132b6b894f6SDavid Xu v = l->wr_cv; 133b6b894f6SDavid Xu } 134b6b894f6SDavid Xu atomic_add_int(&l->wr_waiters, -1); 135b6b894f6SDavid Xu } 136b6b894f6SDavid Xu } 137b6b894f6SDavid Xu 138b6b894f6SDavid Xu static void 139b6b894f6SDavid Xu _thr_rtld_lock_release(void *lock) 140b6b894f6SDavid Xu { 141b6b894f6SDavid Xu struct pthread *curthread; 142b6b894f6SDavid Xu struct rtld_lock *l; 143b6b894f6SDavid Xu 144b6b894f6SDavid Xu curthread = _get_curthread(); 145b6b894f6SDavid Xu l = (struct rtld_lock *)lock; 146b6b894f6SDavid Xu 147b6b894f6SDavid Xu if ((l->lock & WAFLAG) == 0) { 148b6b894f6SDavid Xu atomic_add_rel_int(&l->lock, -RC_INCR); 149f656ae70SDavid Xu if (l->lock == 0 && l->wr_waiters) { 150b6b894f6SDavid Xu atomic_add_long(&l->wr_cv, 1); 151b6b894f6SDavid Xu _thr_umtx_wake(&l->wr_cv, l->wr_waiters); 152b6b894f6SDavid Xu } 153b6b894f6SDavid Xu THR_CRITICAL_LEAVE(curthread); 154b6b894f6SDavid Xu } else { 155b6b894f6SDavid Xu atomic_add_rel_int(&l->lock, -WAFLAG); 156f656ae70SDavid Xu if (l->lock == 0 && l->wr_waiters) { 157b6b894f6SDavid Xu atomic_add_long(&l->wr_cv, 1); 158b6b894f6SDavid Xu _thr_umtx_wake(&l->wr_cv, l->wr_waiters); 159b6b894f6SDavid Xu } else if (l->rd_waiters) { 160b6b894f6SDavid Xu atomic_add_long(&l->rd_cv, 1); 161b6b894f6SDavid Xu _thr_umtx_wake(&l->rd_cv, l->rd_waiters); 162b6b894f6SDavid Xu } 163b6b894f6SDavid Xu _thr_signal_unblock(curthread); 164b6b894f6SDavid Xu } 165b6b894f6SDavid Xu } 166b6b894f6SDavid Xu 167b6b894f6SDavid Xu static int 16837a6356bSDavid Xu _thr_rtld_set_flag(int mask __unused) 169b6b894f6SDavid Xu { 170b6b894f6SDavid Xu /* 171b6b894f6SDavid Xu * The caller's code in rtld-elf is broken, it is not signal safe, 172b6b894f6SDavid Xu * just return zero to fool it. 173b6b894f6SDavid Xu */ 174b6b894f6SDavid Xu return (0); 175b6b894f6SDavid Xu } 176b6b894f6SDavid Xu 177b6b894f6SDavid Xu static int 17837a6356bSDavid Xu _thr_rtld_clr_flag(int mask __unused) 179b6b894f6SDavid Xu { 180b6b894f6SDavid Xu return (0); 181b6b894f6SDavid Xu } 182b6b894f6SDavid Xu 183b6b894f6SDavid Xu void 184b6b894f6SDavid Xu _thr_rtld_init(void) 185b6b894f6SDavid Xu { 186b6b894f6SDavid Xu struct RtldLockInfo li; 187b6b894f6SDavid Xu struct pthread *curthread; 188b6b894f6SDavid Xu umtx_t dummy; 189b6b894f6SDavid Xu 190b6b894f6SDavid Xu curthread = _get_curthread(); 191b6b894f6SDavid Xu 192b6b894f6SDavid Xu /* force to resolve _umtx_op PLT */ 193b6b894f6SDavid Xu _umtx_op((struct umtx *)&dummy, UMTX_OP_WAKE, 1, 0, 0); 194b6b894f6SDavid Xu 195b6b894f6SDavid Xu li.lock_create = _thr_rtld_lock_create; 196b6b894f6SDavid Xu li.lock_destroy = _thr_rtld_lock_destroy; 197b6b894f6SDavid Xu li.rlock_acquire = _thr_rtld_rlock_acquire; 198b6b894f6SDavid Xu li.wlock_acquire = _thr_rtld_wlock_acquire; 199b6b894f6SDavid Xu li.lock_release = _thr_rtld_lock_release; 200b6b894f6SDavid Xu li.thread_set_flag = _thr_rtld_set_flag; 201b6b894f6SDavid Xu li.thread_clr_flag = _thr_rtld_clr_flag; 202b6b894f6SDavid Xu li.at_fork = NULL; 203b6b894f6SDavid Xu 204b6b894f6SDavid Xu /* mask signals, also force to resolve __sys_sigprocmask PLT */ 205b6b894f6SDavid Xu _thr_signal_block(curthread); 206b6b894f6SDavid Xu _rtld_thread_init(&li); 207b6b894f6SDavid Xu _thr_signal_unblock(curthread); 208b6b894f6SDavid Xu } 209b6b894f6SDavid Xu 210b6b894f6SDavid Xu void 211b6b894f6SDavid Xu _thr_rtld_fini(void) 212b6b894f6SDavid Xu { 213b6b894f6SDavid Xu struct pthread *curthread; 214b6b894f6SDavid Xu 215b6b894f6SDavid Xu curthread = _get_curthread(); 216b6b894f6SDavid Xu _thr_signal_block(curthread); 217b6b894f6SDavid Xu _rtld_thread_init(NULL); 218b6b894f6SDavid Xu _thr_signal_unblock(curthread); 219b6b894f6SDavid Xu } 220