Bump version to 0.14.0-pre
[urcu.git] / src / compat_arch.c
1 /*
2 * compat_arch.c
3 *
4 * Userspace RCU library - architecture compatibility checks
5 *
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
23 #include <urcu/arch.h>
24
25 #ifdef URCU_ARCH_X86_NO_CAS
26
27 #include <stdio.h>
28 #include <pthread.h>
29 #include <signal.h>
30 #include <urcu/assert.h>
31 #include <urcu/uatomic.h>
32
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
41 /*
42 * It does not really matter if the constructor is called before using
43 * the library, as long as the caller checks if __rcu_cas_avail < 0 and calls
44 * compat_arch_init() explicitly if needed.
45 */
46 int __attribute__((constructor)) __rcu_cas_init(void);
47
48 /*
49 * -1: unknown
50 * 1: available
51 * 0: unavailable
52 */
53 __attribute__((weak))
54 int __rcu_cas_avail = -1;
55
56 __attribute__((weak))
57 pthread_mutex_t __urcu_x86_compat_mutex = PTHREAD_MUTEX_INITIALIZER;
58
59 /*
60 * get_eflags/set_eflags/compare_and_swap_is_available imported from glibc
61 * 2.3.5. linuxthreads/sysdeps/i386/pt-machine.h.
62 */
63
64 static int get_eflags (void)
65 {
66 int res;
67 __asm__ __volatile__ ("pushfl; popl %0" : "=r" (res) : );
68 return res;
69 }
70
71 static void set_eflags (int newflags)
72 {
73 __asm__ __volatile__ ("pushl %0; popfl" : : "r" (newflags) : "cc");
74 }
75
76 static int compare_and_swap_is_available (void)
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
91 static void mutex_lock_signal_save(pthread_mutex_t *mutex, sigset_t *oldmask)
92 {
93 sigset_t newmask;
94 int ret;
95
96 /* Disable signals */
97 ret = sigfillset(&newmask);
98 urcu_posix_assert(!ret);
99 ret = pthread_sigmask(SIG_BLOCK, &newmask, oldmask);
100 urcu_posix_assert(!ret);
101 ret = pthread_mutex_lock(&__urcu_x86_compat_mutex);
102 urcu_posix_assert(!ret);
103 }
104
105 static void mutex_lock_signal_restore(pthread_mutex_t *mutex, sigset_t *oldmask)
106 {
107 int ret;
108
109 ret = pthread_mutex_unlock(&__urcu_x86_compat_mutex);
110 urcu_posix_assert(!ret);
111 ret = pthread_sigmask(SIG_SETMASK, oldmask, NULL);
112 urcu_posix_assert(!ret);
113 }
114
115 unsigned long _compat_uatomic_set(void *addr, unsigned long _new, int len)
116 {
117 sigset_t mask;
118 unsigned long result;
119
120 mutex_lock_signal_save(&__urcu_x86_compat_mutex, &mask);
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 */
139 result = 0;
140 __asm__ __volatile__("ud2");
141 }
142 mutex_lock_signal_restore(&__urcu_x86_compat_mutex, &mask);
143 return result;
144 }
145
146 unsigned long _compat_uatomic_xchg(void *addr, unsigned long _new, int len)
147 {
148 sigset_t mask;
149 unsigned long retval;
150
151 mutex_lock_signal_save(&__urcu_x86_compat_mutex, &mask);
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 */
170 retval = 0; /* silence gcc warnings */
171 __asm__ __volatile__("ud2");
172 }
173 mutex_lock_signal_restore(&__urcu_x86_compat_mutex, &mask);
174 return retval;
175 }
176
177 unsigned 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
183 mutex_lock_signal_save(&__urcu_x86_compat_mutex, &mask);
184 switch (len) {
185 case 1:
186 {
187 unsigned char result = *(unsigned char *)addr;
188 if (result == (unsigned char)old)
189 *(unsigned char *)addr = (unsigned char)_new;
190 retval = result;
191 break;
192 }
193 case 2:
194 {
195 unsigned short result = *(unsigned short *)addr;
196 if (result == (unsigned short)old)
197 *(unsigned short *)addr = (unsigned short)_new;
198 retval = result;
199 break;
200 }
201 case 4:
202 {
203 unsigned int result = *(unsigned int *)addr;
204 if (result == (unsigned int)old)
205 *(unsigned int *)addr = (unsigned int)_new;
206 retval = result;
207 break;
208 }
209 default:
210 /*
211 * generate an illegal instruction. Cannot catch this with
212 * linker tricks when optimizations are disabled.
213 */
214 retval = 0; /* silence gcc warnings */
215 __asm__ __volatile__("ud2");
216 }
217 mutex_lock_signal_restore(&__urcu_x86_compat_mutex, &mask);
218 return retval;
219 }
220
221 void _compat_uatomic_or(void *addr, unsigned long v, int len)
222 {
223 sigset_t mask;
224
225 mutex_lock_signal_save(&__urcu_x86_compat_mutex, &mask);
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
239 * linker tricks when optimizations are disabled.
240 */
241 __asm__ __volatile__("ud2");
242 }
243 mutex_lock_signal_restore(&__urcu_x86_compat_mutex, &mask);
244 }
245
246 void _compat_uatomic_and(void *addr, unsigned long v, int len)
247 {
248 sigset_t mask;
249
250 mutex_lock_signal_save(&__urcu_x86_compat_mutex, &mask);
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
264 * linker tricks when optimizations are disabled.
265 */
266 __asm__ __volatile__("ud2");
267 }
268 mutex_lock_signal_restore(&__urcu_x86_compat_mutex, &mask);
269 }
270
271 unsigned long _compat_uatomic_add_return(void *addr, unsigned long v, int len)
272 {
273 sigset_t mask;
274 unsigned long result;
275
276 mutex_lock_signal_save(&__urcu_x86_compat_mutex, &mask);
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 */
295 result = 0; /* silence gcc warnings */
296 __asm__ __volatile__("ud2");
297 }
298 mutex_lock_signal_restore(&__urcu_x86_compat_mutex, &mask);
299 return result;
300 }
301
302 int __rcu_cas_init(void)
303 {
304 if (__rcu_cas_avail < 0)
305 __rcu_cas_avail = compare_and_swap_is_available();
306 return __rcu_cas_avail;
307 }
308 #endif
This page took 0.034685 seconds and 4 git commands to generate.