Initial import from FreeBSD RELENG_4:
[dragonfly.git] / libexec / rtld-elf / alpha / 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/alpha/lockdflt.c,v 1.5.2.3 2002/07/11 23:52:27 jdp Exp $
26  */
27
28 /*
29  * Thread locking implementation for the dynamic linker.
30  *
31  * We use the "simple, non-scalable reader-preference lock" from:
32  *
33  *   J. M. Mellor-Crummey and M. L. Scott. "Scalable Reader-Writer
34  *   Synchronization for Shared-Memory Multiprocessors." 3rd ACM Symp. on
35  *   Principles and Practice of Parallel Programming, April 1991.
36  *
37  * In this algorithm the lock is a single word.  Its low-order bit is
38  * set when a writer holds the lock.  The remaining high-order bits
39  * contain a count of readers desiring the lock.  The algorithm requires
40  * atomic "compare_and_store" and "add" operations, which we implement
41  * using assembly language sequences in "rtld_start.S".
42  */
43
44 #include <signal.h>
45 #include <stdlib.h>
46 #include <time.h>
47
48 #include "debug.h"
49 #include "rtld.h"
50
51 /*
52  * This value of CACHE_LINE_SIZE is conservative.  The actual size
53  * is 32 on the  21064, 21064A, 21066, 21066A, and 21164.  It is 64
54  * on the 21264.  Compaq recommends sequestering each lock in its own
55  * 128-byte block to allow for future implementations with larger
56  * cache lines.
57  */
58 #define CACHE_LINE_SIZE         128
59
60 #define WAFLAG          0x1     /* A writer holds the lock */
61 #define RC_INCR         0x2     /* Adjusts count of readers desiring lock */
62
63 typedef struct Struct_Lock {
64         volatile int lock;
65         void *base;
66 } Lock;
67
68 static sigset_t fullsigmask, oldsigmask;
69
70 static void *
71 lock_create(void *context)
72 {
73     void *base;
74     char *p;
75     uintptr_t r;
76     Lock *l;
77
78     /*
79      * Arrange for the lock to occupy its own cache line.  First, we
80      * optimistically allocate just a cache line, hoping that malloc
81      * will give us a well-aligned block of memory.  If that doesn't
82      * work, we allocate a larger block and take a well-aligned cache
83      * line from it.
84      */
85     base = xmalloc(CACHE_LINE_SIZE);
86     p = (char *)base;
87     if ((uintptr_t)p % CACHE_LINE_SIZE != 0) {
88         free(base);
89         base = xmalloc(2 * CACHE_LINE_SIZE);
90         p = (char *)base;
91         if ((r = (uintptr_t)p % CACHE_LINE_SIZE) != 0)
92             p += CACHE_LINE_SIZE - r;
93     }
94     l = (Lock *)p;
95     l->base = base;
96     l->lock = 0;
97     return l;
98 }
99
100 static void
101 lock_destroy(void *lock)
102 {
103     Lock *l = (Lock *)lock;
104
105     free(l->base);
106 }
107
108 static void
109 rlock_acquire(void *lock)
110 {
111     Lock *l = (Lock *)lock;
112
113     atomic_add_int(&l->lock, RC_INCR);
114     while (l->lock & WAFLAG)
115             ;   /* Spin */
116 }
117
118 static void
119 wlock_acquire(void *lock)
120 {
121     Lock *l = (Lock *)lock;
122     sigset_t tmp_oldsigmask;
123
124     for ( ; ; ) {
125         sigprocmask(SIG_BLOCK, &fullsigmask, &tmp_oldsigmask);
126         if (cmp0_and_store_int(&l->lock, WAFLAG) == 0)
127             break;
128         sigprocmask(SIG_SETMASK, &tmp_oldsigmask, NULL);
129     }
130     oldsigmask = tmp_oldsigmask;
131 }
132
133 static void
134 rlock_release(void *lock)
135 {
136     Lock *l = (Lock *)lock;
137
138     atomic_add_int(&l->lock, -RC_INCR);
139 }
140
141 static void
142 wlock_release(void *lock)
143 {
144     Lock *l = (Lock *)lock;
145
146     atomic_add_int(&l->lock, -WAFLAG);
147     sigprocmask(SIG_SETMASK, &oldsigmask, NULL);
148 }
149
150 void
151 lockdflt_init(LockInfo *li)
152 {
153     li->context = NULL;
154     li->lock_create = lock_create;
155     li->rlock_acquire = rlock_acquire;
156     li->wlock_acquire = wlock_acquire;
157     li->rlock_release = rlock_release;
158     li->wlock_release = wlock_release;
159     li->lock_destroy = lock_destroy;
160     li->context_destroy = NULL;
161     /*
162      * Construct a mask to block all signals except traps which might
163      * conceivably be generated within the dynamic linker itself.
164      */
165     sigfillset(&fullsigmask);
166     sigdelset(&fullsigmask, SIGILL);
167     sigdelset(&fullsigmask, SIGTRAP);
168     sigdelset(&fullsigmask, SIGABRT);
169     sigdelset(&fullsigmask, SIGEMT);
170     sigdelset(&fullsigmask, SIGFPE);
171     sigdelset(&fullsigmask, SIGBUS);
172     sigdelset(&fullsigmask, SIGSEGV);
173     sigdelset(&fullsigmask, SIGSYS);
174 }