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