Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 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 | |
Magnus Karlsson | b7e3a28 | 2019-04-16 14:58:11 +0200 | [diff] [blame] | 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") |
Magnus Karlsson | 2c5935f | 2019-04-16 14:58:12 +0200 | [diff] [blame] | 22 | /* Hinders stores to be observed before older loads. */ |
| 23 | # define libbpf_smp_rwmb() asm volatile("" : : : "memory") |
Magnus Karlsson | b7e3a28 | 2019-04-16 14:58:11 +0200 | [diff] [blame] | 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") |
Magnus Karlsson | 2c5935f | 2019-04-16 14:58:12 +0200 | [diff] [blame] | 28 | # define libbpf_smp_rwmb() libbpf_smp_mb() |
Magnus Karlsson | b7e3a28 | 2019-04-16 14:58:11 +0200 | [diff] [blame] | 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") |
Magnus Karlsson | 2c5935f | 2019-04-16 14:58:12 +0200 | [diff] [blame] | 34 | # define libbpf_smp_rwmb() libbpf_smp_mb() |
Magnus Karlsson | b7e3a28 | 2019-04-16 14:58:11 +0200 | [diff] [blame] | 35 | #else |
Magnus Karlsson | 79b1b30 | 2019-04-18 09:21:10 +0200 | [diff] [blame] | 36 | /* Architecture missing native barrier functions. */ |
Magnus Karlsson | b7e3a28 | 2019-04-16 14:58:11 +0200 | [diff] [blame] | 37 | # define libbpf_smp_rmb() __sync_synchronize() |
| 38 | # define libbpf_smp_wmb() __sync_synchronize() |
| 39 | # define libbpf_smp_mb() __sync_synchronize() |
Magnus Karlsson | 2c5935f | 2019-04-16 14:58:12 +0200 | [diff] [blame] | 40 | # define libbpf_smp_rwmb() __sync_synchronize() |
Magnus Karlsson | b7e3a28 | 2019-04-16 14:58:11 +0200 | [diff] [blame] | 41 | #endif |
| 42 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 43 | #ifdef __cplusplus |
| 44 | } /* extern "C" */ |
| 45 | #endif |
| 46 | |
| 47 | #endif |