2 * Copyright 2009-2015 Samy Al Bahra.
3 * Copyright 2011 Devon H. O'Dell <devon.odell@gmail.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #error Do not include this file directly, use ck_pr.h
37 #include <ck_stdint.h>
40 * The following represent supported atomic operations.
41 * These operations may be emulated.
45 /* Minimum requirements for the CK_PR interface are met. */
49 * Prevent speculative execution in busy-wait loops (P4 <=) or "predefined
52 CK_CC_INLINE static void
55 __asm__ __volatile__("pause" ::: "memory");
60 #define CK_PR_LOCK_PREFIX
61 #define CK_PR_FENCE(T, I) \
62 CK_CC_INLINE static void \
63 ck_pr_fence_strict_##T(void) \
65 __asm__ __volatile__("" ::: "memory"); \
69 #define CK_PR_LOCK_PREFIX "lock "
70 #define CK_PR_FENCE(T, I) \
71 CK_CC_INLINE static void \
72 ck_pr_fence_strict_##T(void) \
74 __asm__ __volatile__(I ::: "memory"); \
77 #endif /* CK_MD_UMP */
79 #if defined(CK_MD_SSE_DISABLE)
80 /* If SSE is disabled, then use atomic operations for serialization. */
81 #define CK_MD_X86_MFENCE "lock addl $0, (%%esp)"
82 #define CK_MD_X86_SFENCE CK_MD_X86_MFENCE
83 #define CK_MD_X86_LFENCE CK_MD_X86_MFENCE
85 #define CK_MD_X86_SFENCE "sfence"
86 #define CK_MD_X86_LFENCE "lfence"
87 #define CK_MD_X86_MFENCE "mfence"
88 #endif /* !CK_MD_SSE_DISABLE */
90 CK_PR_FENCE(atomic, "")
91 CK_PR_FENCE(atomic_store, "")
92 CK_PR_FENCE(atomic_load, "")
93 CK_PR_FENCE(store_atomic, "")
94 CK_PR_FENCE(load_atomic, "")
95 CK_PR_FENCE(load, CK_MD_X86_LFENCE)
96 CK_PR_FENCE(load_store, CK_MD_X86_MFENCE)
97 CK_PR_FENCE(store, CK_MD_X86_SFENCE)
98 CK_PR_FENCE(store_load, CK_MD_X86_MFENCE)
99 CK_PR_FENCE(memory, CK_MD_X86_MFENCE)
100 CK_PR_FENCE(release, CK_MD_X86_MFENCE)
101 CK_PR_FENCE(acquire, CK_MD_X86_MFENCE)
102 CK_PR_FENCE(acqrel, CK_MD_X86_MFENCE)
103 CK_PR_FENCE(lock, CK_MD_X86_MFENCE)
104 CK_PR_FENCE(unlock, CK_MD_X86_MFENCE)
109 * Atomic fetch-and-store operations.
111 #define CK_PR_FAS(S, M, T, C, I) \
112 CK_CC_INLINE static T \
113 ck_pr_fas_##S(M *target, T v) \
115 __asm__ __volatile__(I " %0, %1" \
116 : "+m" (*(C *)target), \
123 CK_PR_FAS(ptr, void, void *, char, "xchgl")
125 #define CK_PR_FAS_S(S, T, I) CK_PR_FAS(S, T, T, T, I)
127 CK_PR_FAS_S(char, char, "xchgb")
128 CK_PR_FAS_S(uint, unsigned int, "xchgl")
129 CK_PR_FAS_S(int, int, "xchgl")
130 CK_PR_FAS_S(32, uint32_t, "xchgl")
131 CK_PR_FAS_S(16, uint16_t, "xchgw")
132 CK_PR_FAS_S(8, uint8_t, "xchgb")
137 #define CK_PR_LOAD(S, M, T, C, I) \
138 CK_CC_INLINE static T \
139 ck_pr_md_load_##S(const M *target) \
142 __asm__ __volatile__(I " %1, %0" \
144 : "m" (*(const C *)target) \
149 CK_PR_LOAD(ptr, void, void *, char, "movl")
151 #define CK_PR_LOAD_S(S, T, I) CK_PR_LOAD(S, T, T, T, I)
153 CK_PR_LOAD_S(char, char, "movb")
154 CK_PR_LOAD_S(uint, unsigned int, "movl")
155 CK_PR_LOAD_S(int, int, "movl")
156 CK_PR_LOAD_S(32, uint32_t, "movl")
157 CK_PR_LOAD_S(16, uint16_t, "movw")
158 CK_PR_LOAD_S(8, uint8_t, "movb")
163 #define CK_PR_STORE(S, M, T, C, I) \
164 CK_CC_INLINE static void \
165 ck_pr_md_store_##S(M *target, T v) \
167 __asm__ __volatile__(I " %1, %0" \
168 : "=m" (*(C *)target) \
169 : CK_CC_IMM "q" (v) \
174 CK_PR_STORE(ptr, void, const void *, char, "movl")
176 #define CK_PR_STORE_S(S, T, I) CK_PR_STORE(S, T, T, T, I)
178 CK_PR_STORE_S(char, char, "movb")
179 CK_PR_STORE_S(uint, unsigned int, "movl")
180 CK_PR_STORE_S(int, int, "movl")
181 CK_PR_STORE_S(32, uint32_t, "movl")
182 CK_PR_STORE_S(16, uint16_t, "movw")
183 CK_PR_STORE_S(8, uint8_t, "movb")
189 * Atomic fetch-and-add operations.
191 #define CK_PR_FAA(S, M, T, C, I) \
192 CK_CC_INLINE static T \
193 ck_pr_faa_##S(M *target, T d) \
195 __asm__ __volatile__(CK_PR_LOCK_PREFIX I " %1, %0" \
196 : "+m" (*(C *)target), \
203 CK_PR_FAA(ptr, void, uintptr_t, char, "xaddl")
205 #define CK_PR_FAA_S(S, T, I) CK_PR_FAA(S, T, T, T, I)
207 CK_PR_FAA_S(char, char, "xaddb")
208 CK_PR_FAA_S(uint, unsigned int, "xaddl")
209 CK_PR_FAA_S(int, int, "xaddl")
210 CK_PR_FAA_S(32, uint32_t, "xaddl")
211 CK_PR_FAA_S(16, uint16_t, "xaddw")
212 CK_PR_FAA_S(8, uint8_t, "xaddb")
218 * Atomic store-only unary operations.
220 #define CK_PR_UNARY(K, S, T, C, I) \
221 CK_PR_UNARY_R(K, S, T, C, I) \
222 CK_PR_UNARY_V(K, S, T, C, I)
224 #define CK_PR_UNARY_R(K, S, T, C, I) \
225 CK_CC_INLINE static void \
226 ck_pr_##K##_##S(T *target) \
228 __asm__ __volatile__(CK_PR_LOCK_PREFIX I " %0" \
229 : "+m" (*(C *)target) \
235 #define CK_PR_UNARY_V(K, S, T, C, I) \
236 CK_CC_INLINE static void \
237 ck_pr_##K##_##S##_zero(T *target, bool *r) \
239 __asm__ __volatile__(CK_PR_LOCK_PREFIX I " %0; setz %1" \
240 : "+m" (*(C *)target), \
248 #define CK_PR_UNARY_S(K, S, T, I) CK_PR_UNARY(K, S, T, T, I)
250 #define CK_PR_GENERATE(K) \
251 CK_PR_UNARY(K, ptr, void, char, #K "l") \
252 CK_PR_UNARY_S(K, char, char, #K "b") \
253 CK_PR_UNARY_S(K, int, int, #K "l") \
254 CK_PR_UNARY_S(K, uint, unsigned int, #K "l") \
255 CK_PR_UNARY_S(K, 32, uint32_t, #K "l") \
256 CK_PR_UNARY_S(K, 16, uint16_t, #K "w") \
257 CK_PR_UNARY_S(K, 8, uint8_t, #K "b")
263 /* not does not affect condition flags. */
265 #define CK_PR_UNARY_V(a, b, c, d, e)
268 #undef CK_PR_GENERATE
275 * Atomic store-only binary operations.
277 #define CK_PR_BINARY(K, S, M, T, C, I) \
278 CK_CC_INLINE static void \
279 ck_pr_##K##_##S(M *target, T d) \
281 __asm__ __volatile__(CK_PR_LOCK_PREFIX I " %1, %0" \
282 : "+m" (*(C *)target) \
283 : CK_CC_IMM "q" (d) \
288 #define CK_PR_BINARY_S(K, S, T, I) CK_PR_BINARY(K, S, T, T, T, I)
290 #define CK_PR_GENERATE(K) \
291 CK_PR_BINARY(K, ptr, void, uintptr_t, char, #K "l") \
292 CK_PR_BINARY_S(K, char, char, #K "b") \
293 CK_PR_BINARY_S(K, int, int, #K "l") \
294 CK_PR_BINARY_S(K, uint, unsigned int, #K "l") \
295 CK_PR_BINARY_S(K, 32, uint32_t, #K "l") \
296 CK_PR_BINARY_S(K, 16, uint16_t, #K "w") \
297 CK_PR_BINARY_S(K, 8, uint8_t, #K "b")
305 #undef CK_PR_GENERATE
306 #undef CK_PR_BINARY_S
310 * Atomic compare and swap.
312 #define CK_PR_CAS(S, M, T, C, I) \
313 CK_CC_INLINE static bool \
314 ck_pr_cas_##S(M *target, T compare, T set) \
317 __asm__ __volatile__(CK_PR_LOCK_PREFIX I " %2, %0; setz %1" \
318 : "+m" (*(C *)target), \
326 CK_PR_CAS(ptr, void, void *, char, "cmpxchgl")
328 #define CK_PR_CAS_S(S, T, I) CK_PR_CAS(S, T, T, T, I)
330 CK_PR_CAS_S(char, char, "cmpxchgb")
331 CK_PR_CAS_S(int, int, "cmpxchgl")
332 CK_PR_CAS_S(uint, unsigned int, "cmpxchgl")
333 CK_PR_CAS_S(32, uint32_t, "cmpxchgl")
334 CK_PR_CAS_S(16, uint16_t, "cmpxchgw")
335 CK_PR_CAS_S(8, uint8_t, "cmpxchgb")
341 * Compare and swap, set *v to old value of target.
343 #define CK_PR_CAS_O(S, M, T, C, I, R) \
344 CK_CC_INLINE static bool \
345 ck_pr_cas_##S##_value(M *target, T compare, T set, M *v) \
348 __asm__ __volatile__(CK_PR_LOCK_PREFIX "cmpxchg" I " %3, %0;" \
349 "mov %% " R ", %2;" \
351 : "+m" (*(C *)target), \
360 CK_PR_CAS_O(ptr, void, void *, char, "l", "eax")
362 #define CK_PR_CAS_O_S(S, T, I, R) \
363 CK_PR_CAS_O(S, T, T, T, I, R)
365 CK_PR_CAS_O_S(char, char, "b", "al")
366 CK_PR_CAS_O_S(int, int, "l", "eax")
367 CK_PR_CAS_O_S(uint, unsigned int, "l", "eax")
368 CK_PR_CAS_O_S(32, uint32_t, "l", "eax")
369 CK_PR_CAS_O_S(16, uint16_t, "w", "ax")
370 CK_PR_CAS_O_S(8, uint8_t, "b", "al")
376 * Atomic bit test operations.
378 #define CK_PR_BT(K, S, T, P, C, I) \
379 CK_CC_INLINE static bool \
380 ck_pr_##K##_##S(T *target, unsigned int b) \
383 __asm__ __volatile__(CK_PR_LOCK_PREFIX I "; setc %1" \
384 : "+m" (*(C *)target), \
391 #define CK_PR_BT_S(K, S, T, I) CK_PR_BT(K, S, T, T, T, I)
393 #define CK_PR_GENERATE(K) \
394 CK_PR_BT(K, ptr, void, uint32_t, char, #K "l %2, %0") \
395 CK_PR_BT_S(K, uint, unsigned int, #K "l %2, %0") \
396 CK_PR_BT_S(K, int, int, #K "l %2, %0") \
397 CK_PR_BT_S(K, 32, uint32_t, #K "l %2, %0") \
398 CK_PR_BT_S(K, 16, uint16_t, #K "w %w2, %0")
404 #undef CK_PR_GENERATE
407 #endif /* CK_PR_X86_H */