xref: /openbmc/linux/tools/testing/selftests/bpf/test_btf.h (revision 3f4d4c74101d60b88d23289bd4f5f6126c7235fc)
1*3f4d4c74SMartin KaFai Lau /* SPDX-License-Identifier: GPL-2.0 */
2*3f4d4c74SMartin KaFai Lau /* Copyright (c) 2019 Facebook */
3*3f4d4c74SMartin KaFai Lau 
4*3f4d4c74SMartin KaFai Lau #ifndef _TEST_BTF_H
5*3f4d4c74SMartin KaFai Lau #define _TEST_BTF_H
6*3f4d4c74SMartin KaFai Lau 
7*3f4d4c74SMartin KaFai Lau #define BTF_INFO_ENC(kind, kind_flag, vlen)			\
8*3f4d4c74SMartin KaFai Lau 	((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))
9*3f4d4c74SMartin KaFai Lau 
10*3f4d4c74SMartin KaFai Lau #define BTF_TYPE_ENC(name, info, size_or_type)	\
11*3f4d4c74SMartin KaFai Lau 	(name), (info), (size_or_type)
12*3f4d4c74SMartin KaFai Lau 
13*3f4d4c74SMartin KaFai Lau #define BTF_INT_ENC(encoding, bits_offset, nr_bits)	\
14*3f4d4c74SMartin KaFai Lau 	((encoding) << 24 | (bits_offset) << 16 | (nr_bits))
15*3f4d4c74SMartin KaFai Lau #define BTF_TYPE_INT_ENC(name, encoding, bits_offset, bits, sz)	\
16*3f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), sz),	\
17*3f4d4c74SMartin KaFai Lau 	BTF_INT_ENC(encoding, bits_offset, bits)
18*3f4d4c74SMartin KaFai Lau 
19*3f4d4c74SMartin KaFai Lau #define BTF_FWD_ENC(name, kind_flag) \
20*3f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FWD, kind_flag, 0), 0)
21*3f4d4c74SMartin KaFai Lau 
22*3f4d4c74SMartin KaFai Lau #define BTF_ARRAY_ENC(type, index_type, nr_elems)	\
23*3f4d4c74SMartin KaFai Lau 	(type), (index_type), (nr_elems)
24*3f4d4c74SMartin KaFai Lau #define BTF_TYPE_ARRAY_ENC(type, index_type, nr_elems) \
25*3f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ARRAY, 0, 0), 0), \
26*3f4d4c74SMartin KaFai Lau 	BTF_ARRAY_ENC(type, index_type, nr_elems)
27*3f4d4c74SMartin KaFai Lau 
28*3f4d4c74SMartin KaFai Lau #define BTF_STRUCT_ENC(name, nr_elems, sz)	\
29*3f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, nr_elems), sz)
30*3f4d4c74SMartin KaFai Lau 
31*3f4d4c74SMartin KaFai Lau #define BTF_UNION_ENC(name, nr_elems, sz)	\
32*3f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_UNION, 0, nr_elems), sz)
33*3f4d4c74SMartin KaFai Lau 
34*3f4d4c74SMartin KaFai Lau #define BTF_VAR_ENC(name, type, linkage)	\
35*3f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), type), (linkage)
36*3f4d4c74SMartin KaFai Lau #define BTF_VAR_SECINFO_ENC(type, offset, size)	\
37*3f4d4c74SMartin KaFai Lau 	(type), (offset), (size)
38*3f4d4c74SMartin KaFai Lau 
39*3f4d4c74SMartin KaFai Lau #define BTF_MEMBER_ENC(name, type, bits_offset)	\
40*3f4d4c74SMartin KaFai Lau 	(name), (type), (bits_offset)
41*3f4d4c74SMartin KaFai Lau #define BTF_ENUM_ENC(name, val) (name), (val)
42*3f4d4c74SMartin KaFai Lau #define BTF_MEMBER_OFFSET(bitfield_size, bits_offset) \
43*3f4d4c74SMartin KaFai Lau 	((bitfield_size) << 24 | (bits_offset))
44*3f4d4c74SMartin KaFai Lau 
45*3f4d4c74SMartin KaFai Lau #define BTF_TYPEDEF_ENC(name, type) \
46*3f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), type)
47*3f4d4c74SMartin KaFai Lau 
48*3f4d4c74SMartin KaFai Lau #define BTF_PTR_ENC(type) \
49*3f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), type)
50*3f4d4c74SMartin KaFai Lau 
51*3f4d4c74SMartin KaFai Lau #define BTF_CONST_ENC(type) \
52*3f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), type)
53*3f4d4c74SMartin KaFai Lau 
54*3f4d4c74SMartin KaFai Lau #define BTF_VOLATILE_ENC(type) \
55*3f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_VOLATILE, 0, 0), type)
56*3f4d4c74SMartin KaFai Lau 
57*3f4d4c74SMartin KaFai Lau #define BTF_RESTRICT_ENC(type) \
58*3f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_RESTRICT, 0, 0), type)
59*3f4d4c74SMartin KaFai Lau 
60*3f4d4c74SMartin KaFai Lau #define BTF_FUNC_PROTO_ENC(ret_type, nargs) \
61*3f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, nargs), ret_type)
62*3f4d4c74SMartin KaFai Lau 
63*3f4d4c74SMartin KaFai Lau #define BTF_FUNC_PROTO_ARG_ENC(name, type) \
64*3f4d4c74SMartin KaFai Lau 	(name), (type)
65*3f4d4c74SMartin KaFai Lau 
66*3f4d4c74SMartin KaFai Lau #define BTF_FUNC_ENC(name, func_proto) \
67*3f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), func_proto)
68*3f4d4c74SMartin KaFai Lau 
69*3f4d4c74SMartin KaFai Lau #endif /* _TEST_BTF_H */
70