Merge branch 'vendor/BZIP'
[dragonfly.git] / libexec / rtld-elf / i386 / lockdflt.c
1 /*-
2  * Copyright 1999, 2000 John D. Polstra.
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, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $FreeBSD: src/libexec/rtld-elf/i386/lockdflt.c,v 1.5.2.4 2002/07/11 23:52:32 jdp Exp $
26  * $DragonFly: src/libexec/rtld-elf/i386/lockdflt.c,v 1.3 2007/11/09 19:38:50 hasso Exp $
27  */
28
29 /*
30  * Thread locking implementation for the dynamic linker.
31  *
32  * We use the "simple, non-scalable reader-preference lock" from:
33  *
34  *   J. M. Mellor-Crummey and M. L. Scott. "Scalable Reader-Writer
35  *   Synchronization for Shared-Memory Multiprocessors." 3rd ACM Symp. on
36  *   Principles and Practice of Parallel Programming, April 1991.
37  *
38  * In this algorithm the lock is a single word.  Its low-order bit is
39  * set when a writer holds the lock.  The remaining high-order bits
40  * contain a count of readers desiring the lock.  The algorithm requires
41  * atomic "compare_and_store" and "add" operations.
42  *
43  */
44
45 #include <setjmp.h>
46 #include <signal.h>
47 #include <stdlib.h>
48 #include <time.h>
49
50 #include "debug.h"
51 #include "rtld.h"
52
53 #define CACHE_LINE_SIZE         32
54
55 #define WAFLAG          0x1     /* A writer holds the lock */
56 #define RC_INCR         0x2     /* Adjusts count of readers desiring lock */
57
58 typedef struct Struct_Lock {
59         volatile int lock;
60         void *base;
61 } Lock;
62
63 static sigset_t fullsigmask, oldsigmask;
64
65 static inline int
66 cmpxchgl(int old, int new, volatile int *m)
67 {
68         int result;
69
70         __asm __volatile ("lock; cmpxchgl %2, %0"
71             : "+m"(*m), "=a"(result)
72             : "r"(new), "1"(old)
73             : "cc");
74
75         return result;
76 }
77
78 static void *
79 lock_create(void *context)
80 {
81     void *base;
82     char *p;
83     uintptr_t r;
84     Lock *l;
85
86     /*
87      * Arrange for the lock to occupy its own cache line.  First, we
88      * optimistically allocate just a cache line, hoping that malloc
89      * will give us a well-aligned block of memory.  If that doesn't
90      * work, we allocate a larger block and take a well-aligned cache
91      * line from it.
92      */
93     base = xmalloc(CACHE_LINE_SIZE);
94     p = (char *)base;
95     if ((uintptr_t)p % CACHE_LINE_SIZE != 0) {
96         free(base);
97         base = xmalloc(2 * CACHE_LINE_SIZE);
98         p = (char *)base;
99         if ((r = (uintptr_t)p % CACHE_LINE_SIZE) != 0)
100             p += CACHE_LINE_SIZE - r;
101     }
102     l = (Lock *)p;
103     l->base = base;
104     l->lock = 0;
105     return l;
106 }
107
108 static void
109 lock_destroy(void *lock)
110 {
111     Lock *l = (Lock *)lock;
112
113     free(l->base);
114 }
115
116 /*
117  * Reader/writer locks.
118  */
119 static void
120 rlock_acquire(void *lock)
121 {
122     Lock *l = (Lock *)lock;
123
124     atomic_add_int(&l->lock, RC_INCR);
125     while (l->lock & WAFLAG)
126             ;   /* Spin */
127 }
128
129 static void
130 wlock_acquire(void *lock)
131 {
132     Lock *l = (Lock *)lock;
133     sigset_t tmp_oldsigmask;
134
135     for ( ; ; ) {
136         sigprocmask(SIG_BLOCK, &fullsigmask, &tmp_oldsigmask);
137         if (cmpxchgl(0, WAFLAG, &l->lock) == 0)
138             break;
139         sigprocmask(SIG_SETMASK, &tmp_oldsigmask, NULL);
140     }
141     oldsigmask = tmp_oldsigmask;
142 }
143
144 static void
145 rlock_release(void *lock)
146 {
147     Lock *l = (Lock *)lock;
148
149     atomic_add_int(&l->lock, -RC_INCR);
150 }
151
152 static void
153 wlock_release(void *lock)
154 {
155     Lock *l = (Lock *)lock;
156
157     atomic_add_int(&l->lock, -WAFLAG);
158     sigprocmask(SIG_SETMASK, &oldsigmask, NULL);
159 }
160
161 void
162 lockdflt_init(LockInfo *li)
163 {
164     li->context = NULL;
165     li->context_destroy = NULL;
166     li->lock_create = lock_create;
167     li->lock_destroy = lock_destroy;
168     li->rlock_acquire = rlock_acquire;
169     li->wlock_acquire = wlock_acquire;
170     li->rlock_release = rlock_release;
171     li->wlock_release = wlock_release;
172     /*
173      * Construct a mask to block all signals except traps which might
174      * conceivably be generated within the dynamic linker itself.
175      */
176     sigfillset(&fullsigmask);
177     sigdelset(&fullsigmask, SIGILL);
178     sigdelset(&fullsigmask, SIGTRAP);
179     sigdelset(&fullsigmask, SIGABRT);
180     sigdelset(&fullsigmask, SIGEMT);
181     sigdelset(&fullsigmask, SIGFPE);
182     sigdelset(&fullsigmask, SIGBUS);
183     sigdelset(&fullsigmask, SIGSEGV);
184     sigdelset(&fullsigmask, SIGSYS);
185 }