^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef __TOOLS_LINUX_KERNEL_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define __TOOLS_LINUX_KERNEL_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/build_bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <endian.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <byteswap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #ifndef UINT_MAX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define UINT_MAX (~0U)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define PERF_ALIGN(x, a) __PERF_ALIGN_MASK(x, (typeof(x))(a)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define __PERF_ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #ifndef offsetof
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #ifndef container_of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * container_of - cast a member of a structure out to the containing structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * @ptr: the pointer to the member.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * @type: the type of the container struct this is embedded in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * @member: the name of the member within the struct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define container_of(ptr, type, member) ({ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) const typeof(((type *)0)->member) * __mptr = (ptr); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) (type *)((char *)__mptr - offsetof(type, member)); })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #ifndef max
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define max(x, y) ({ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) typeof(x) _max1 = (x); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) typeof(y) _max2 = (y); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) (void) (&_max1 == &_max2); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) _max1 > _max2 ? _max1 : _max2; })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #ifndef min
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define min(x, y) ({ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) typeof(x) _min1 = (x); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) typeof(y) _min2 = (y); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) (void) (&_min1 == &_min2); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) _min1 < _min2 ? _min1 : _min2; })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #ifndef roundup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define roundup(x, y) ( \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) const typeof(y) __y = y; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) (((x) + (__y - 1)) / __y) * __y; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #ifndef BUG_ON
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #ifdef NDEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define BUG_ON(cond) do { if (cond) {} } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #define BUG_ON(cond) assert(!(cond))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #define BUG() BUG_ON(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #if __BYTE_ORDER == __BIG_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #define cpu_to_le16 bswap_16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define cpu_to_le32 bswap_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define cpu_to_le64 bswap_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define le16_to_cpu bswap_16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define le32_to_cpu bswap_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define le64_to_cpu bswap_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #define cpu_to_be16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define cpu_to_be32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #define cpu_to_be64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #define be16_to_cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #define be32_to_cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #define be64_to_cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #define cpu_to_le16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) #define cpu_to_le32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) #define cpu_to_le64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #define le16_to_cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #define le32_to_cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #define le64_to_cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #define cpu_to_be16 bswap_16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #define cpu_to_be32 bswap_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #define cpu_to_be64 bswap_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) #define be16_to_cpu bswap_16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #define be32_to_cpu bswap_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #define be64_to_cpu bswap_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) int scnprintf(char * buf, size_t size, const char * fmt, ...);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) int scnprintf_pad(char * buf, size_t size, const char * fmt, ...);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * This looks more complex than it should be. But we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * get the type for the ~ right in round_down (it needs to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * as wide as the result!), and we want to evaluate the macro
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * arguments just once each.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) #define __round_mask(x, y) ((__typeof__(x))((y)-1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) #define round_down(x, y) ((x) & ~__round_mask(x, y))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) #define current_gfp_context(k) 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) #define synchronize_rcu()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) #endif