Fix: call_rcu: teardown default call_rcu worker on application exit
[urcu.git] / src / compat_arch.c
CommitLineData
d45599f4 1/*
0b1e236d 2 * compat_arch.c
d45599f4 3 *
0b1e236d 4 * Userspace RCU library - architecture compatibility checks
d45599f4 5 *
6982d6d7 6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
d45599f4
MD
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
0b1e236d
MJ
23#include <urcu/arch.h>
24
101389e4 25#ifdef URCU_ARCH_X86_NO_CAS
0b1e236d 26
d45599f4
MD
27#include <stdio.h>
28#include <pthread.h>
29#include <signal.h>
01477510 30#include <urcu/assert.h>
a2e7bf9c 31#include <urcu/uatomic.h>
d45599f4 32
976720e4
MD
33/*
34 * Using attribute "weak" for __rcu_cas_avail and
35 * __urcu_x86_compat_mutex. Those are globally visible by the entire
36 * program, even though many shared objects may have their own version.
37 * The first version that gets loaded will be used by the entire
38 * program (executable and all shared objects).
39 */
40
d45599f4
MD
41/*
42 * It does not really matter if the constructor is called before using
02be5561 43 * the library, as long as the caller checks if __rcu_cas_avail < 0 and calls
f99c6e92 44 * compat_arch_init() explicitly if needed.
d45599f4 45 */
02be5561 46int __attribute__((constructor)) __rcu_cas_init(void);
d45599f4 47
d45599f4
MD
48/*
49 * -1: unknown
50 * 1: available
51 * 0: unavailable
52 */
976720e4 53__attribute__((weak))
02be5561 54int __rcu_cas_avail = -1;
d45599f4 55
976720e4
MD
56__attribute__((weak))
57pthread_mutex_t __urcu_x86_compat_mutex = PTHREAD_MUTEX_INITIALIZER;
bf9de1b7 58
d45599f4 59/*
bf9de1b7
MD
60 * get_eflags/set_eflags/compare_and_swap_is_available imported from glibc
61 * 2.3.5. linuxthreads/sysdeps/i386/pt-machine.h.
d45599f4
MD
62 */
63
bf9de1b7 64static int get_eflags (void)
d45599f4
MD
65{
66 int res;
67 __asm__ __volatile__ ("pushfl; popl %0" : "=r" (res) : );
68 return res;
69}
70
bf9de1b7 71static void set_eflags (int newflags)
d45599f4
MD
72{
73 __asm__ __volatile__ ("pushl %0; popfl" : : "r" (newflags) : "cc");
74}
75
bf9de1b7 76static int compare_and_swap_is_available (void)
d45599f4
MD
77{
78 int oldflags = get_eflags ();
79 int changed;
80 /* Flip AC bit in EFLAGS. */
81 set_eflags (oldflags ^ 0x40000);
82 /* See if bit changed. */
83 changed = (get_eflags () ^ oldflags) & 0x40000;
84 /* Restore EFLAGS. */
85 set_eflags (oldflags);
86 /* If the AC flag did not change, it's a 386 and it lacks cmpxchg.
87 Otherwise, it's a 486 or above and it has cmpxchg. */
88 return changed != 0;
89}
90
bf9de1b7 91static void mutex_lock_signal_save(pthread_mutex_t *mutex, sigset_t *oldmask)
d45599f4 92{
bf9de1b7 93 sigset_t newmask;
d45599f4
MD
94 int ret;
95
96 /* Disable signals */
6ed4b2e6 97 ret = sigfillset(&newmask);
01477510 98 urcu_posix_assert(!ret);
6ed4b2e6 99 ret = pthread_sigmask(SIG_BLOCK, &newmask, oldmask);
01477510 100 urcu_posix_assert(!ret);
976720e4 101 ret = pthread_mutex_lock(&__urcu_x86_compat_mutex);
01477510 102 urcu_posix_assert(!ret);
bf9de1b7
MD
103}
104
105static void mutex_lock_signal_restore(pthread_mutex_t *mutex, sigset_t *oldmask)
106{
107 int ret;
108
976720e4 109 ret = pthread_mutex_unlock(&__urcu_x86_compat_mutex);
01477510 110 urcu_posix_assert(!ret);
bf9de1b7 111 ret = pthread_sigmask(SIG_SETMASK, oldmask, NULL);
01477510 112 urcu_posix_assert(!ret);
bf9de1b7
MD
113}
114
115unsigned long _compat_uatomic_set(void *addr, unsigned long _new, int len)
116{
117 sigset_t mask;
118 unsigned long result;
119
976720e4 120 mutex_lock_signal_save(&__urcu_x86_compat_mutex, &mask);
bf9de1b7
MD
121 switch (len) {
122 case 1:
123 *(unsigned char *)addr = (unsigned char)_new;
124 result = *(unsigned char *)addr;
125 break;
126 case 2:
127 *(unsigned short *)addr = (unsigned short)_new;
128 result = *(unsigned short *)addr;
129 break;
130 case 4:
131 *(unsigned int *)addr = (unsigned int)_new;
132 result = *(unsigned int *)addr;
133 break;
134 default:
135 /*
136 * generate an illegal instruction. Cannot catch this with
137 * linker tricks when optimizations are disabled.
138 */
c94627b5 139 result = 0;
bf9de1b7
MD
140 __asm__ __volatile__("ud2");
141 }
976720e4 142 mutex_lock_signal_restore(&__urcu_x86_compat_mutex, &mask);
c94627b5 143 return result;
bf9de1b7 144}
d45599f4 145
bf9de1b7
MD
146unsigned long _compat_uatomic_xchg(void *addr, unsigned long _new, int len)
147{
148 sigset_t mask;
149 unsigned long retval;
150
976720e4 151 mutex_lock_signal_save(&__urcu_x86_compat_mutex, &mask);
bf9de1b7
MD
152 switch (len) {
153 case 1:
154 retval = *(unsigned char *)addr;
155 *(unsigned char *)addr = (unsigned char)_new;
156 break;
157 case 2:
158 retval = *(unsigned short *)addr;
159 *(unsigned short *)addr = (unsigned short)_new;
160 break;
161 case 4:
162 retval = *(unsigned int *)addr;
163 *(unsigned int *)addr = (unsigned int)_new;
164 break;
165 default:
166 /*
167 * generate an illegal instruction. Cannot catch this with
168 * linker tricks when optimizations are disabled.
169 */
8c43fe72 170 retval = 0; /* silence gcc warnings */
bf9de1b7
MD
171 __asm__ __volatile__("ud2");
172 }
976720e4 173 mutex_lock_signal_restore(&__urcu_x86_compat_mutex, &mask);
bf9de1b7
MD
174 return retval;
175}
176
177unsigned long _compat_uatomic_cmpxchg(void *addr, unsigned long old,
178 unsigned long _new, int len)
179{
180 unsigned long retval;
181 sigset_t mask;
182
976720e4 183 mutex_lock_signal_save(&__urcu_x86_compat_mutex, &mask);
d45599f4
MD
184 switch (len) {
185 case 1:
186 {
187 unsigned char result = *(unsigned char *)addr;
bf9de1b7 188 if (result == (unsigned char)old)
d45599f4 189 *(unsigned char *)addr = (unsigned char)_new;
bf9de1b7
MD
190 retval = result;
191 break;
d45599f4
MD
192 }
193 case 2:
194 {
195 unsigned short result = *(unsigned short *)addr;
bf9de1b7 196 if (result == (unsigned short)old)
d45599f4 197 *(unsigned short *)addr = (unsigned short)_new;
bf9de1b7
MD
198 retval = result;
199 break;
d45599f4
MD
200 }
201 case 4:
202 {
203 unsigned int result = *(unsigned int *)addr;
bf9de1b7 204 if (result == (unsigned int)old)
d45599f4 205 *(unsigned int *)addr = (unsigned int)_new;
bf9de1b7
MD
206 retval = result;
207 break;
d45599f4 208 }
bf9de1b7
MD
209 default:
210 /*
211 * generate an illegal instruction. Cannot catch this with
212 * linker tricks when optimizations are disabled.
213 */
8c43fe72 214 retval = 0; /* silence gcc warnings */
bf9de1b7 215 __asm__ __volatile__("ud2");
d45599f4 216 }
976720e4 217 mutex_lock_signal_restore(&__urcu_x86_compat_mutex, &mask);
bf9de1b7
MD
218 return retval;
219}
d45599f4 220
985b35b1
PB
221void _compat_uatomic_or(void *addr, unsigned long v, int len)
222{
223 sigset_t mask;
224
976720e4 225 mutex_lock_signal_save(&__urcu_x86_compat_mutex, &mask);
985b35b1
PB
226 switch (len) {
227 case 1:
228 *(unsigned char *)addr |= (unsigned char)v;
229 break;
230 case 2:
231 *(unsigned short *)addr |= (unsigned short)v;
232 break;
233 case 4:
234 *(unsigned int *)addr |= (unsigned int)v;
235 break;
236 default:
237 /*
238 * generate an illegal instruction. Cannot catch this with
bf33aaea
PB
239 * linker tricks when optimizations are disabled.
240 */
241 __asm__ __volatile__("ud2");
242 }
976720e4 243 mutex_lock_signal_restore(&__urcu_x86_compat_mutex, &mask);
bf33aaea
PB
244}
245
246void _compat_uatomic_and(void *addr, unsigned long v, int len)
247{
248 sigset_t mask;
249
976720e4 250 mutex_lock_signal_save(&__urcu_x86_compat_mutex, &mask);
bf33aaea
PB
251 switch (len) {
252 case 1:
253 *(unsigned char *)addr &= (unsigned char)v;
254 break;
255 case 2:
256 *(unsigned short *)addr &= (unsigned short)v;
257 break;
258 case 4:
259 *(unsigned int *)addr &= (unsigned int)v;
260 break;
261 default:
262 /*
263 * generate an illegal instruction. Cannot catch this with
985b35b1
PB
264 * linker tricks when optimizations are disabled.
265 */
266 __asm__ __volatile__("ud2");
267 }
976720e4 268 mutex_lock_signal_restore(&__urcu_x86_compat_mutex, &mask);
985b35b1
PB
269}
270
bf9de1b7
MD
271unsigned long _compat_uatomic_add_return(void *addr, unsigned long v, int len)
272{
273 sigset_t mask;
274 unsigned long result;
275
976720e4 276 mutex_lock_signal_save(&__urcu_x86_compat_mutex, &mask);
bf9de1b7
MD
277 switch (len) {
278 case 1:
279 *(unsigned char *)addr += (unsigned char)v;
280 result = *(unsigned char *)addr;
281 break;
282 case 2:
283 *(unsigned short *)addr += (unsigned short)v;
284 result = *(unsigned short *)addr;
285 break;
286 case 4:
287 *(unsigned int *)addr += (unsigned int)v;
288 result = *(unsigned int *)addr;
289 break;
290 default:
291 /*
292 * generate an illegal instruction. Cannot catch this with
293 * linker tricks when optimizations are disabled.
294 */
8c43fe72 295 result = 0; /* silence gcc warnings */
bf9de1b7
MD
296 __asm__ __volatile__("ud2");
297 }
976720e4 298 mutex_lock_signal_restore(&__urcu_x86_compat_mutex, &mask);
bf9de1b7 299 return result;
d45599f4
MD
300}
301
02be5561 302int __rcu_cas_init(void)
d45599f4 303{
02be5561
MD
304 if (__rcu_cas_avail < 0)
305 __rcu_cas_avail = compare_and_swap_is_available();
306 return __rcu_cas_avail;
d45599f4 307}
0b1e236d 308#endif
This page took 0.05574 seconds and 4 git commands to generate.