1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 /* Copyright (c) 2019 Facebook */
3 
4 #ifndef __LIBBPF_LIBBPF_UTIL_H
5 #define __LIBBPF_LIBBPF_UTIL_H
6 
7 #include <stdbool.h>
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /* Use these barrier functions instead of smp_[rw]mb() when they are
14  * used in a libbpf header file. That way they can be built into the
15  * application that uses libbpf.
16  */
17 #if defined(__i386__) || defined(__x86_64__)
18 # define libbpf_smp_rmb() asm volatile("" : : : "memory")
19 # define libbpf_smp_wmb() asm volatile("" : : : "memory")
20 # define libbpf_smp_mb() \
21 	asm volatile("lock; addl $0,-4(%%rsp)" : : : "memory", "cc")
22 /* Hinders stores to be observed before older loads. */
23 # define libbpf_smp_rwmb() asm volatile("" : : : "memory")
24 #elif defined(__aarch64__)
25 # define libbpf_smp_rmb() asm volatile("dmb ishld" : : : "memory")
26 # define libbpf_smp_wmb() asm volatile("dmb ishst" : : : "memory")
27 # define libbpf_smp_mb() asm volatile("dmb ish" : : : "memory")
28 # define libbpf_smp_rwmb() libbpf_smp_mb()
29 #elif defined(__arm__)
30 /* These are only valid for armv7 and above */
31 # define libbpf_smp_rmb() asm volatile("dmb ish" : : : "memory")
32 # define libbpf_smp_wmb() asm volatile("dmb ishst" : : : "memory")
33 # define libbpf_smp_mb() asm volatile("dmb ish" : : : "memory")
34 # define libbpf_smp_rwmb() libbpf_smp_mb()
35 #else
36 /* Architecture missing native barrier functions. */
37 # define libbpf_smp_rmb() __sync_synchronize()
38 # define libbpf_smp_wmb() __sync_synchronize()
39 # define libbpf_smp_mb() __sync_synchronize()
40 # define libbpf_smp_rwmb() __sync_synchronize()
41 #endif
42 
43 #ifdef __cplusplus
44 } /* extern "C" */
45 #endif
46 
47 #endif
48