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 pa_atomic_dec(&read_lock
);
109 static void memtrap_swap(unsigned n
) {
113 /* If the read counter is > 0 wait; if it is 0 try to swap the lists */
115 pa_semaphore_wait(pa_static_semaphore_get(&semaphore
, 0));
116 else if (pa_atomic_cmpxchg(&read_lock
, (int) n
, (int) (n
^ MSB
)))
119 n
= (unsigned) pa_atomic_load(&read_lock
);
123 static void memtrap_link(pa_memtrap
*m
, unsigned j
) {
127 m
->next
[j
] = memtraps
[j
];
131 static void memtrap_unlink(pa_memtrap
*m
, int j
) {
135 m
->next
[j
]->prev
[j
] = m
->prev
[j
];
138 m
->prev
[j
]->next
[j
] = m
->next
[j
];
140 memtraps
[j
] = m
->next
[j
];
143 pa_memtrap
* pa_memtrap_add(const void *start
, size_t size
) {
144 pa_memtrap
*m
= NULL
;
150 pa_assert(PA_PAGE_ALIGN_PTR(start
) == start
);
151 pa_assert(PA_PAGE_ALIGN(size
) == size
);
153 lock
= pa_static_mutex_get(&write_lock
, FALSE
, FALSE
);
159 /* Before we install the signal handler, make sure the
160 * semaphore is valid so that the initialization of the
161 * semaphore doesn't have to happen from the signal handler */
162 pa_static_semaphore_get(&semaphore
, 0);
164 memset(&sa
, 0, sizeof(sa
));
165 sa
.sa_sigaction
= signal_handler
;
166 sa
.sa_flags
= SA_RESTART
|SA_SIGINFO
;
168 pa_assert_se(sigaction(SIGBUS
, &sa
, NULL
) == 0);
171 n
= (unsigned) pa_atomic_load(&read_lock
);
174 m
= pa_xnew(pa_memtrap
, 1);
175 m
->start
= (void*) start
;
177 pa_atomic_store(&m
->bad
, 0);
183 pa_mutex_unlock(lock
);
188 void pa_memtrap_remove(pa_memtrap
*m
) {
194 lock
= pa_static_mutex_get(&write_lock
, FALSE
, FALSE
);
197 n
= (unsigned) pa_atomic_load(&read_lock
);
200 memtrap_unlink(m
, !j
);
202 memtrap_unlink(m
, j
);
209 memset(&sa
, 0, sizeof(sa
));
210 sa
.sa_handler
= SIG_DFL
;
211 pa_assert_se(sigaction(SIGBUS
, &sa
, NULL
) == 0);
214 pa_mutex_unlock(lock
);
217 pa_memtrap
*pa_memtrap_update(pa_memtrap
*m
, const void *start
, size_t size
) {
225 pa_assert(PA_PAGE_ALIGN_PTR(start
) == start
);
226 pa_assert(PA_PAGE_ALIGN(size
) == size
);
228 lock
= pa_static_mutex_get(&write_lock
, FALSE
, FALSE
);
231 if (m
->start
== start
&& m
->size
== size
)
234 n
= (unsigned) pa_atomic_load(&read_lock
);
237 memtrap_unlink(m
, !j
);
239 memtrap_unlink(m
, j
);
241 m
->start
= (void*) start
;
243 pa_atomic_store(&m
->bad
, 0);
245 n
= (unsigned) pa_atomic_load(&read_lock
);
253 pa_mutex_unlock(lock
);