MFC: An off-by-one malloc size was corrupting the installer's memory,
[dragonfly.git] / libexec / rtld-elf / i386 / lockdflt.c
blob046ba6857d6eafc4d3d7e1debabf5388ba30ad41
1 /*-
2 * Copyright 1999, 2000 John D. Polstra.
3 * All rights reserved.
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.
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.
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 $
30 * Thread locking implementation for the dynamic linker.
32 * We use the "simple, non-scalable reader-preference lock" from:
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.
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.
45 #include <setjmp.h>
46 #include <signal.h>
47 #include <stdlib.h>
48 #include <time.h>
50 #include "debug.h"
51 #include "rtld.h"
53 #define CACHE_LINE_SIZE 32
55 #define WAFLAG 0x1 /* A writer holds the lock */
56 #define RC_INCR 0x2 /* Adjusts count of readers desiring lock */
58 typedef struct Struct_Lock {
59 volatile int lock;
60 void *base;
61 } Lock;
63 static sigset_t fullsigmask, oldsigmask;
65 static inline int
66 cmpxchgl(int old, int new, volatile int *m)
68 int result;
70 __asm __volatile ("lock; cmpxchgl %2, %0"
71 : "+m"(*m), "=a"(result)
72 : "r"(new), "1"(old)
73 : "cc");
75 return result;
78 static void *
79 lock_create(void *context)
81 void *base;
82 char *p;
83 uintptr_t r;
84 Lock *l;
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.
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;
102 l = (Lock *)p;
103 l->base = base;
104 l->lock = 0;
105 return l;
108 static void
109 lock_destroy(void *lock)
111 Lock *l = (Lock *)lock;
113 free(l->base);
117 * Reader/writer locks.
119 static void
120 rlock_acquire(void *lock)
122 Lock *l = (Lock *)lock;
124 atomic_add_int(&l->lock, RC_INCR);
125 while (l->lock & WAFLAG)
126 ; /* Spin */
129 static void
130 wlock_acquire(void *lock)
132 Lock *l = (Lock *)lock;
133 sigset_t tmp_oldsigmask;
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);
141 oldsigmask = tmp_oldsigmask;
144 static void
145 rlock_release(void *lock)
147 Lock *l = (Lock *)lock;
149 atomic_add_int(&l->lock, -RC_INCR);
152 static void
153 wlock_release(void *lock)
155 Lock *l = (Lock *)lock;
157 atomic_add_int(&l->lock, -WAFLAG);
158 sigprocmask(SIG_SETMASK, &oldsigmask, NULL);
161 void
162 lockdflt_init(LockInfo *li)
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;
173 * Construct a mask to block all signals except traps which might
174 * conceivably be generated within the dynamic linker itself.
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);