blob: 59c779c5790c16de5e64fc8be397639ec6356fbd [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
Magnus Karlssonb7e3a282019-04-16 14:58:11 +020013/* 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 Karlsson2c5935f2019-04-16 14:58:12 +020022/* Hinders stores to be observed before older loads. */
23# define libbpf_smp_rwmb() asm volatile("" : : : "memory")
Magnus Karlssonb7e3a282019-04-16 14:58:11 +020024#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 Karlsson2c5935f2019-04-16 14:58:12 +020028# define libbpf_smp_rwmb() libbpf_smp_mb()
Magnus Karlssonb7e3a282019-04-16 14:58:11 +020029#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 Karlsson2c5935f2019-04-16 14:58:12 +020034# define libbpf_smp_rwmb() libbpf_smp_mb()
Magnus Karlssonb7e3a282019-04-16 14:58:11 +020035#else
Magnus Karlsson79b1b302019-04-18 09:21:10 +020036/* Architecture missing native barrier functions. */
Magnus Karlssonb7e3a282019-04-16 14:58:11 +020037# define libbpf_smp_rmb() __sync_synchronize()
38# define libbpf_smp_wmb() __sync_synchronize()
39# define libbpf_smp_mb() __sync_synchronize()
Magnus Karlsson2c5935f2019-04-16 14:58:12 +020040# define libbpf_smp_rwmb() __sync_synchronize()
Magnus Karlssonb7e3a282019-04-16 14:58:11 +020041#endif
42
Yonghong Song8461ef82019-02-01 16:14:14 -080043#ifdef __cplusplus
44} /* extern "C" */
45#endif
46
47#endif