2 This file is part of PulseAudio.
4 Copyright 2009 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <pulse/xmalloc.h>
31 #include <pulsecore/semaphore.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/mutex.h>
34 #include <pulsecore/core-util.h>
42 pa_memtrap *next[2], *prev[2];
45 static pa_memtrap *memtraps[2] = { NULL, NULL };
46 static pa_atomic_t read_lock = PA_ATOMIC_INIT(0);
47 static pa_static_semaphore semaphore = PA_STATIC_SEMAPHORE_INIT;
48 static pa_static_mutex write_lock = PA_STATIC_MUTEX_INIT;
50 #define MSB (1U << (sizeof(unsigned)*8U-1))
51 #define WHICH(n) (!!((n) & MSB))
52 #define COUNTER(n) ((n) & ~MSB)
54 pa_bool_t pa_memtrap_is_good(pa_memtrap *m) {
57 return !pa_atomic_load(&m->bad);
60 static void sigsafe_error(const char *s) {
61 write(STDERR_FILENO, s, strlen(s));
64 static void signal_handler(int sig, siginfo_t* si, void *data) {
69 /* Increase the lock counter */
70 n = (unsigned) pa_atomic_inc(&read_lock);
72 /* The uppermost bit tells us which list to look at */
75 /* When n is 0 we have about 2^31 threads running that
76 * all got a sigbus at the same time, oh my! */
77 pa_assert(COUNTER(n)+1 > 0);
79 for (m = memtraps[j]; m; m = m->next[j])
80 if (si->si_addr >= m->start &&
81 (uint8_t*) si->si_addr < (uint8_t*) m->start + m->size)
87 pa_atomic_store(&m->bad, 1);
89 /* Remap anonymous memory into the bad segment */
90 if ((r = mmap(m->start, m->size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_FIXED|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
91 sigsafe_error("mmap() failed.\n");
95 pa_assert(r == m->start);
97 pa_atomic_dec(&read_lock);
99 /* Post the semaphore */
100 pa_semaphore_post(pa_static_semaphore_get(&semaphore, 0));
105 sigsafe_error("Failed to handle SIGBUS.\n");
106 pa_atomic_dec(&read_lock);
110 static void memtrap_swap(unsigned n) {
114 /* If the read counter is > 0 wait; if it is 0 try to swap the lists */
116 pa_semaphore_wait(pa_static_semaphore_get(&semaphore, 0));
117 else if (pa_atomic_cmpxchg(&read_lock, (int) n, (int) (n ^ MSB)))
120 n = (unsigned) pa_atomic_load(&read_lock);
124 static void memtrap_link(pa_memtrap *m, unsigned j) {
128 m->next[j] = memtraps[j];
132 static void memtrap_unlink(pa_memtrap *m, int j) {
136 m->next[j]->prev[j] = m->prev[j];
139 m->prev[j]->next[j] = m->next[j];
141 memtraps[j] = m->next[j];
144 pa_memtrap* pa_memtrap_add(const void *start, size_t size) {
145 pa_memtrap *m = NULL;
151 pa_assert(PA_PAGE_ALIGN_PTR(start) == start);
152 pa_assert(PA_PAGE_ALIGN(size) == size);
154 lock = pa_static_mutex_get(&write_lock, FALSE, FALSE);
157 n = (unsigned) pa_atomic_load(&read_lock);
160 m = pa_xnew(pa_memtrap, 1);
161 m->start = (void*) start;
163 pa_atomic_store(&m->bad, 0);
169 pa_mutex_unlock(lock);
174 void pa_memtrap_remove(pa_memtrap *m) {
180 lock = pa_static_mutex_get(&write_lock, FALSE, FALSE);
183 n = (unsigned) pa_atomic_load(&read_lock);
186 memtrap_unlink(m, !j);
188 memtrap_unlink(m, j);
192 pa_mutex_unlock(lock);
195 pa_memtrap *pa_memtrap_update(pa_memtrap *m, const void *start, size_t size) {
203 pa_assert(PA_PAGE_ALIGN_PTR(start) == start);
204 pa_assert(PA_PAGE_ALIGN(size) == size);
206 lock = pa_static_mutex_get(&write_lock, FALSE, FALSE);
209 if (m->start == start && m->size == size)
212 n = (unsigned) pa_atomic_load(&read_lock);
215 memtrap_unlink(m, !j);
217 memtrap_unlink(m, j);
219 m->start = (void*) start;
221 pa_atomic_store(&m->bad, 0);
223 n = (unsigned) pa_atomic_load(&read_lock);
231 pa_mutex_unlock(lock);
236 void pa_memtrap_install(void) {
239 /* Before we install the signal handler, make sure the semaphore
240 * is valid so that the initialization of the semaphore
241 * doesn't have to happen from the signal handler */
242 pa_static_semaphore_get(&semaphore, 0);
244 memset(&sa, 0, sizeof(sa));
245 sa.sa_sigaction = signal_handler;
246 sa.sa_flags = SA_RESTART|SA_SIGINFO;
248 pa_assert_se(sigaction(SIGBUS, &sa, NULL) == 0);