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 | |
| 13 | extern void libbpf_print(enum libbpf_print_level level, |
| 14 | const char *format, ...) |
| 15 | __attribute__((format(printf, 2, 3))); |
| 16 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 17 | #define __pr(level, fmt, ...) \ |
| 18 | do { \ |
| 19 | libbpf_print(level, "libbpf: " fmt, ##__VA_ARGS__); \ |
| 20 | } while (0) |
| 21 | |
| 22 | #define pr_warning(fmt, ...) __pr(LIBBPF_WARN, fmt, ##__VA_ARGS__) |
| 23 | #define pr_info(fmt, ...) __pr(LIBBPF_INFO, fmt, ##__VA_ARGS__) |
| 24 | #define pr_debug(fmt, ...) __pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__) |
| 25 | |
Magnus Karlsson | b7e3a28 | 2019-04-16 14:58:11 +0200 | [diff] [blame] | 26 | /* Use these barrier functions instead of smp_[rw]mb() when they are |
| 27 | * used in a libbpf header file. That way they can be built into the |
| 28 | * application that uses libbpf. |
| 29 | */ |
| 30 | #if defined(__i386__) || defined(__x86_64__) |
| 31 | # define libbpf_smp_rmb() asm volatile("" : : : "memory") |
| 32 | # define libbpf_smp_wmb() asm volatile("" : : : "memory") |
| 33 | # define libbpf_smp_mb() \ |
| 34 | asm volatile("lock; addl $0,-4(%%rsp)" : : : "memory", "cc") |
Magnus Karlsson | 2c5935f | 2019-04-16 14:58:12 +0200 | [diff] [blame] | 35 | /* Hinders stores to be observed before older loads. */ |
| 36 | # define libbpf_smp_rwmb() asm volatile("" : : : "memory") |
Magnus Karlsson | b7e3a28 | 2019-04-16 14:58:11 +0200 | [diff] [blame] | 37 | #elif defined(__aarch64__) |
| 38 | # define libbpf_smp_rmb() asm volatile("dmb ishld" : : : "memory") |
| 39 | # define libbpf_smp_wmb() asm volatile("dmb ishst" : : : "memory") |
| 40 | # define libbpf_smp_mb() asm volatile("dmb ish" : : : "memory") |
Magnus Karlsson | 2c5935f | 2019-04-16 14:58:12 +0200 | [diff] [blame] | 41 | # define libbpf_smp_rwmb() libbpf_smp_mb() |
Magnus Karlsson | b7e3a28 | 2019-04-16 14:58:11 +0200 | [diff] [blame] | 42 | #elif defined(__arm__) |
| 43 | /* These are only valid for armv7 and above */ |
| 44 | # define libbpf_smp_rmb() asm volatile("dmb ish" : : : "memory") |
| 45 | # define libbpf_smp_wmb() asm volatile("dmb ishst" : : : "memory") |
| 46 | # define libbpf_smp_mb() asm volatile("dmb ish" : : : "memory") |
Magnus Karlsson | 2c5935f | 2019-04-16 14:58:12 +0200 | [diff] [blame] | 47 | # define libbpf_smp_rwmb() libbpf_smp_mb() |
Magnus Karlsson | b7e3a28 | 2019-04-16 14:58:11 +0200 | [diff] [blame] | 48 | #else |
Magnus Karlsson | 79b1b30 | 2019-04-18 09:21:10 +0200 | [diff] [blame] | 49 | /* Architecture missing native barrier functions. */ |
Magnus Karlsson | b7e3a28 | 2019-04-16 14:58:11 +0200 | [diff] [blame] | 50 | # define libbpf_smp_rmb() __sync_synchronize() |
| 51 | # define libbpf_smp_wmb() __sync_synchronize() |
| 52 | # define libbpf_smp_mb() __sync_synchronize() |
Magnus Karlsson | 2c5935f | 2019-04-16 14:58:12 +0200 | [diff] [blame] | 53 | # define libbpf_smp_rwmb() __sync_synchronize() |
Magnus Karlsson | b7e3a28 | 2019-04-16 14:58:11 +0200 | [diff] [blame] | 54 | #endif |
| 55 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 56 | #ifdef __cplusplus |
| 57 | } /* extern "C" */ |
| 58 | #endif |
| 59 | |
| 60 | #endif |