xref: /openbmc/linux/tools/testing/selftests/bpf/test_btf.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
13f4d4c74SMartin KaFai Lau /* SPDX-License-Identifier: GPL-2.0 */
23f4d4c74SMartin KaFai Lau /* Copyright (c) 2019 Facebook */
33f4d4c74SMartin KaFai Lau 
43f4d4c74SMartin KaFai Lau #ifndef _TEST_BTF_H
53f4d4c74SMartin KaFai Lau #define _TEST_BTF_H
63f4d4c74SMartin KaFai Lau 
7*7a42008cSEduard Zingerman #define BTF_END_RAW 0xdeadbeef
8*7a42008cSEduard Zingerman 
93f4d4c74SMartin KaFai Lau #define BTF_INFO_ENC(kind, kind_flag, vlen)			\
103f4d4c74SMartin KaFai Lau 	((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))
113f4d4c74SMartin KaFai Lau 
123f4d4c74SMartin KaFai Lau #define BTF_TYPE_ENC(name, info, size_or_type)	\
133f4d4c74SMartin KaFai Lau 	(name), (info), (size_or_type)
143f4d4c74SMartin KaFai Lau 
153f4d4c74SMartin KaFai Lau #define BTF_INT_ENC(encoding, bits_offset, nr_bits)	\
163f4d4c74SMartin KaFai Lau 	((encoding) << 24 | (bits_offset) << 16 | (nr_bits))
173f4d4c74SMartin KaFai Lau #define BTF_TYPE_INT_ENC(name, encoding, bits_offset, bits, sz)	\
183f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), sz),	\
193f4d4c74SMartin KaFai Lau 	BTF_INT_ENC(encoding, bits_offset, bits)
203f4d4c74SMartin KaFai Lau 
213f4d4c74SMartin KaFai Lau #define BTF_FWD_ENC(name, kind_flag) \
223f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FWD, kind_flag, 0), 0)
233f4d4c74SMartin KaFai Lau 
243f4d4c74SMartin KaFai Lau #define BTF_ARRAY_ENC(type, index_type, nr_elems)	\
253f4d4c74SMartin KaFai Lau 	(type), (index_type), (nr_elems)
263f4d4c74SMartin KaFai Lau #define BTF_TYPE_ARRAY_ENC(type, index_type, nr_elems) \
273f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ARRAY, 0, 0), 0), \
283f4d4c74SMartin KaFai Lau 	BTF_ARRAY_ENC(type, index_type, nr_elems)
293f4d4c74SMartin KaFai Lau 
303f4d4c74SMartin KaFai Lau #define BTF_STRUCT_ENC(name, nr_elems, sz)	\
313f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, nr_elems), sz)
323f4d4c74SMartin KaFai Lau 
333f4d4c74SMartin KaFai Lau #define BTF_UNION_ENC(name, nr_elems, sz)	\
343f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_UNION, 0, nr_elems), sz)
353f4d4c74SMartin KaFai Lau 
363f4d4c74SMartin KaFai Lau #define BTF_VAR_ENC(name, type, linkage)	\
373f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), type), (linkage)
383f4d4c74SMartin KaFai Lau #define BTF_VAR_SECINFO_ENC(type, offset, size)	\
393f4d4c74SMartin KaFai Lau 	(type), (offset), (size)
403f4d4c74SMartin KaFai Lau 
413f4d4c74SMartin KaFai Lau #define BTF_MEMBER_ENC(name, type, bits_offset)	\
423f4d4c74SMartin KaFai Lau 	(name), (type), (bits_offset)
433f4d4c74SMartin KaFai Lau #define BTF_ENUM_ENC(name, val) (name), (val)
443b532518SYonghong Song #define BTF_ENUM64_ENC(name, val_lo32, val_hi32) (name), (val_lo32), (val_hi32)
453f4d4c74SMartin KaFai Lau #define BTF_MEMBER_OFFSET(bitfield_size, bits_offset) \
463f4d4c74SMartin KaFai Lau 	((bitfield_size) << 24 | (bits_offset))
473f4d4c74SMartin KaFai Lau 
483f4d4c74SMartin KaFai Lau #define BTF_TYPEDEF_ENC(name, type) \
493f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), type)
503f4d4c74SMartin KaFai Lau 
513f4d4c74SMartin KaFai Lau #define BTF_PTR_ENC(type) \
523f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), type)
533f4d4c74SMartin KaFai Lau 
543f4d4c74SMartin KaFai Lau #define BTF_CONST_ENC(type) \
553f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), type)
563f4d4c74SMartin KaFai Lau 
573f4d4c74SMartin KaFai Lau #define BTF_VOLATILE_ENC(type) \
583f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_VOLATILE, 0, 0), type)
593f4d4c74SMartin KaFai Lau 
603f4d4c74SMartin KaFai Lau #define BTF_RESTRICT_ENC(type) \
613f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_RESTRICT, 0, 0), type)
623f4d4c74SMartin KaFai Lau 
633f4d4c74SMartin KaFai Lau #define BTF_FUNC_PROTO_ENC(ret_type, nargs) \
643f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, nargs), ret_type)
653f4d4c74SMartin KaFai Lau 
663f4d4c74SMartin KaFai Lau #define BTF_FUNC_PROTO_ARG_ENC(name, type) \
673f4d4c74SMartin KaFai Lau 	(name), (type)
683f4d4c74SMartin KaFai Lau 
693f4d4c74SMartin KaFai Lau #define BTF_FUNC_ENC(name, func_proto) \
703f4d4c74SMartin KaFai Lau 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), func_proto)
713f4d4c74SMartin KaFai Lau 
727e72aad3SIlya Leoshkevich #define BTF_TYPE_FLOAT_ENC(name, sz) \
737e72aad3SIlya Leoshkevich 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz)
747e72aad3SIlya Leoshkevich 
75223f903eSYonghong Song #define BTF_DECL_TAG_ENC(value, type, component_idx)	\
76223f903eSYonghong Song 	BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_DECL_TAG, 0, 0), type), (component_idx)
7735baba7aSYonghong Song 
786aa5dabcSYonghong Song #define BTF_TYPE_TAG_ENC(value, type)	\
796aa5dabcSYonghong Song 	BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TYPE_TAG, 0, 0), type)
806aa5dabcSYonghong Song 
813f4d4c74SMartin KaFai Lau #endif /* _TEST_BTF_H */
82