uatomic: fix i386 support
[urcu.git] / compat_arch_x86.c
CommitLineData
d45599f4
MD
1/*
2 * compat_arch_x86.c
3 *
4 * Userspace RCU library - x86 compatibility checks
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
23#include <stdio.h>
24#include <pthread.h>
25#include <signal.h>
26#include <assert.h>
a2e7bf9c 27#include <urcu/uatomic.h>
d45599f4
MD
28
29/*
30 * It does not really matter if the constructor is called before using
02be5561 31 * the library, as long as the caller checks if __rcu_cas_avail < 0 and calls
d45599f4
MD
32 * compat_arch_init() explicitely if needed.
33 */
02be5561 34int __attribute__((constructor)) __rcu_cas_init(void);
d45599f4 35
d45599f4
MD
36/*
37 * -1: unknown
38 * 1: available
39 * 0: unavailable
40 */
02be5561 41int __rcu_cas_avail = -1;
d45599f4 42
bf9de1b7
MD
43static pthread_mutex_t compat_mutex = PTHREAD_MUTEX_INITIALIZER;
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 */
83 ret = sigemptyset(&newmask);
84 assert(!ret);
bf9de1b7 85 ret = pthread_sigmask(SIG_SETMASK, &newmask, oldmask);
d45599f4
MD
86 assert(!ret);
87 ret = pthread_mutex_lock(&compat_mutex);
88 assert(!ret);
bf9de1b7
MD
89}
90
91static void mutex_lock_signal_restore(pthread_mutex_t *mutex, sigset_t *oldmask)
92{
93 int ret;
94
95 ret = pthread_mutex_unlock(&compat_mutex);
96 assert(!ret);
97 ret = pthread_sigmask(SIG_SETMASK, oldmask, NULL);
98 assert(!ret);
99}
100
101unsigned long _compat_uatomic_set(void *addr, unsigned long _new, int len)
102{
103 sigset_t mask;
104 unsigned long result;
105
106 mutex_lock_signal_save(&compat_mutex, &mask);
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 */
125 __asm__ __volatile__("ud2");
126 }
127 mutex_lock_signal_restore(&compat_mutex, &mask);
128 return _new;
129}
d45599f4 130
bf9de1b7
MD
131unsigned long _compat_uatomic_xchg(void *addr, unsigned long _new, int len)
132{
133 sigset_t mask;
134 unsigned long retval;
135
136 mutex_lock_signal_save(&compat_mutex, &mask);
137 switch (len) {
138 case 1:
139 retval = *(unsigned char *)addr;
140 *(unsigned char *)addr = (unsigned char)_new;
141 break;
142 case 2:
143 retval = *(unsigned short *)addr;
144 *(unsigned short *)addr = (unsigned short)_new;
145 break;
146 case 4:
147 retval = *(unsigned int *)addr;
148 *(unsigned int *)addr = (unsigned int)_new;
149 break;
150 default:
151 /*
152 * generate an illegal instruction. Cannot catch this with
153 * linker tricks when optimizations are disabled.
154 */
8c43fe72 155 retval = 0; /* silence gcc warnings */
bf9de1b7
MD
156 __asm__ __volatile__("ud2");
157 }
158 mutex_lock_signal_restore(&compat_mutex, &mask);
159 return retval;
160}
161
162unsigned long _compat_uatomic_cmpxchg(void *addr, unsigned long old,
163 unsigned long _new, int len)
164{
165 unsigned long retval;
166 sigset_t mask;
167
168 mutex_lock_signal_save(&compat_mutex, &mask);
d45599f4
MD
169 switch (len) {
170 case 1:
171 {
172 unsigned char result = *(unsigned char *)addr;
bf9de1b7 173 if (result == (unsigned char)old)
d45599f4 174 *(unsigned char *)addr = (unsigned char)_new;
bf9de1b7
MD
175 retval = result;
176 break;
d45599f4
MD
177 }
178 case 2:
179 {
180 unsigned short result = *(unsigned short *)addr;
bf9de1b7 181 if (result == (unsigned short)old)
d45599f4 182 *(unsigned short *)addr = (unsigned short)_new;
bf9de1b7
MD
183 retval = result;
184 break;
d45599f4
MD
185 }
186 case 4:
187 {
188 unsigned int result = *(unsigned int *)addr;
bf9de1b7 189 if (result == (unsigned int)old)
d45599f4 190 *(unsigned int *)addr = (unsigned int)_new;
bf9de1b7
MD
191 retval = result;
192 break;
d45599f4 193 }
bf9de1b7
MD
194 default:
195 /*
196 * generate an illegal instruction. Cannot catch this with
197 * linker tricks when optimizations are disabled.
198 */
8c43fe72 199 retval = 0; /* silence gcc warnings */
bf9de1b7 200 __asm__ __volatile__("ud2");
d45599f4 201 }
bf9de1b7
MD
202 mutex_lock_signal_restore(&compat_mutex, &mask);
203 return retval;
204}
d45599f4 205
985b35b1
PB
206void _compat_uatomic_or(void *addr, unsigned long v, int len)
207{
208 sigset_t mask;
209
210 mutex_lock_signal_save(&compat_mutex, &mask);
211 switch (len) {
212 case 1:
213 *(unsigned char *)addr |= (unsigned char)v;
214 break;
215 case 2:
216 *(unsigned short *)addr |= (unsigned short)v;
217 break;
218 case 4:
219 *(unsigned int *)addr |= (unsigned int)v;
220 break;
221 default:
222 /*
223 * generate an illegal instruction. Cannot catch this with
bf33aaea
PB
224 * linker tricks when optimizations are disabled.
225 */
226 __asm__ __volatile__("ud2");
227 }
228 mutex_lock_signal_restore(&compat_mutex, &mask);
229}
230
231void _compat_uatomic_and(void *addr, unsigned long v, int len)
232{
233 sigset_t mask;
234
235 mutex_lock_signal_save(&compat_mutex, &mask);
236 switch (len) {
237 case 1:
238 *(unsigned char *)addr &= (unsigned char)v;
239 break;
240 case 2:
241 *(unsigned short *)addr &= (unsigned short)v;
242 break;
243 case 4:
244 *(unsigned int *)addr &= (unsigned int)v;
245 break;
246 default:
247 /*
248 * generate an illegal instruction. Cannot catch this with
985b35b1
PB
249 * linker tricks when optimizations are disabled.
250 */
251 __asm__ __volatile__("ud2");
252 }
253 mutex_lock_signal_restore(&compat_mutex, &mask);
254}
255
bf9de1b7
MD
256unsigned long _compat_uatomic_add_return(void *addr, unsigned long v, int len)
257{
258 sigset_t mask;
259 unsigned long result;
260
261 mutex_lock_signal_save(&compat_mutex, &mask);
262 switch (len) {
263 case 1:
264 *(unsigned char *)addr += (unsigned char)v;
265 result = *(unsigned char *)addr;
266 break;
267 case 2:
268 *(unsigned short *)addr += (unsigned short)v;
269 result = *(unsigned short *)addr;
270 break;
271 case 4:
272 *(unsigned int *)addr += (unsigned int)v;
273 result = *(unsigned int *)addr;
274 break;
275 default:
276 /*
277 * generate an illegal instruction. Cannot catch this with
278 * linker tricks when optimizations are disabled.
279 */
8c43fe72 280 result = 0; /* silence gcc warnings */
bf9de1b7
MD
281 __asm__ __volatile__("ud2");
282 }
283 mutex_lock_signal_restore(&compat_mutex, &mask);
284 return result;
d45599f4
MD
285}
286
02be5561 287int __rcu_cas_init(void)
d45599f4 288{
02be5561
MD
289 if (__rcu_cas_avail < 0)
290 __rcu_cas_avail = compare_and_swap_is_available();
291 return __rcu_cas_avail;
d45599f4 292}
This page took 0.034487 seconds and 4 git commands to generate.