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