blob: da94c4cb2e4db5094e9b3e3a91ae05f8b1d9bbf6 [file] [log] [blame]
Yonghong Song8461ef82019-02-01 16:14:14 -08001/* 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
10extern "C" {
11#endif
12
13extern void libbpf_print(enum libbpf_print_level level,
14 const char *format, ...)
15 __attribute__((format(printf, 2, 3)));
16
Yonghong Song8461ef82019-02-01 16:14:14 -080017#define __pr(level, fmt, ...) \
18do { \
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 Karlssonb7e3a282019-04-16 14:58:11 +020026/* 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 Karlsson2c5935f2019-04-16 14:58:12 +020035/* Hinders stores to be observed before older loads. */
36# define libbpf_smp_rwmb() asm volatile("" : : : "memory")
Magnus Karlssonb7e3a282019-04-16 14:58:11 +020037#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 Karlsson2c5935f2019-04-16 14:58:12 +020041# define libbpf_smp_rwmb() libbpf_smp_mb()
Magnus Karlssonb7e3a282019-04-16 14:58:11 +020042#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 Karlsson2c5935f2019-04-16 14:58:12 +020047# define libbpf_smp_rwmb() libbpf_smp_mb()
Magnus Karlssonb7e3a282019-04-16 14:58:11 +020048#else
Magnus Karlsson79b1b302019-04-18 09:21:10 +020049/* Architecture missing native barrier functions. */
Magnus Karlssonb7e3a282019-04-16 14:58:11 +020050# define libbpf_smp_rmb() __sync_synchronize()
51# define libbpf_smp_wmb() __sync_synchronize()
52# define libbpf_smp_mb() __sync_synchronize()
Magnus Karlsson2c5935f2019-04-16 14:58:12 +020053# define libbpf_smp_rwmb() __sync_synchronize()
Magnus Karlssonb7e3a282019-04-16 14:58:11 +020054#endif
55
Yonghong Song8461ef82019-02-01 16:14:14 -080056#ifdef __cplusplus
57} /* extern "C" */
58#endif
59
60#endif