xref: /openbmc/linux/tools/include/uapi/linux/btf.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
13bd86a84SMartin KaFai Lau /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
23bd86a84SMartin KaFai Lau /* Copyright (c) 2018 Facebook */
33bd86a84SMartin KaFai Lau #ifndef _UAPI__LINUX_BTF_H__
43bd86a84SMartin KaFai Lau #define _UAPI__LINUX_BTF_H__
53bd86a84SMartin KaFai Lau 
63bd86a84SMartin KaFai Lau #include <linux/types.h>
73bd86a84SMartin KaFai Lau 
83bd86a84SMartin KaFai Lau #define BTF_MAGIC	0xeB9F
93bd86a84SMartin KaFai Lau #define BTF_VERSION	1
103bd86a84SMartin KaFai Lau 
113bd86a84SMartin KaFai Lau struct btf_header {
123bd86a84SMartin KaFai Lau 	__u16	magic;
133bd86a84SMartin KaFai Lau 	__u8	version;
143bd86a84SMartin KaFai Lau 	__u8	flags;
15f03b15d3SMartin KaFai Lau 	__u32	hdr_len;
163bd86a84SMartin KaFai Lau 
173bd86a84SMartin KaFai Lau 	/* All offsets are in bytes relative to the end of this header */
183bd86a84SMartin KaFai Lau 	__u32	type_off;	/* offset of type section	*/
19f03b15d3SMartin KaFai Lau 	__u32	type_len;	/* length of type section	*/
203bd86a84SMartin KaFai Lau 	__u32	str_off;	/* offset of string section	*/
213bd86a84SMartin KaFai Lau 	__u32	str_len;	/* length of string section	*/
223bd86a84SMartin KaFai Lau };
233bd86a84SMartin KaFai Lau 
243bd86a84SMartin KaFai Lau /* Max # of type identifier */
25166750bcSAndrii Nakryiko #define BTF_MAX_TYPE	0x000fffff
263bd86a84SMartin KaFai Lau /* Max offset into the string section */
27166750bcSAndrii Nakryiko #define BTF_MAX_NAME_OFFSET	0x00ffffff
283bd86a84SMartin KaFai Lau /* Max # of struct/union/enum members or func args */
293bd86a84SMartin KaFai Lau #define BTF_MAX_VLEN	0xffff
303bd86a84SMartin KaFai Lau 
313bd86a84SMartin KaFai Lau struct btf_type {
32fbcf93ebSMartin KaFai Lau 	__u32 name_off;
333bd86a84SMartin KaFai Lau 	/* "info" bits arrangement
343bd86a84SMartin KaFai Lau 	 * bits  0-15: vlen (e.g. # of struct's members)
353bd86a84SMartin KaFai Lau 	 * bits 16-23: unused
3666df0fdbSHaiyue Wang 	 * bits 24-28: kind (e.g. int, ptr, array...etc)
3766df0fdbSHaiyue Wang 	 * bits 29-30: unused
38128b343dSYonghong Song 	 * bit     31: kind_flag, currently used by
39*6089fb32SYonghong Song 	 *             struct, union, enum, fwd and enum64
403bd86a84SMartin KaFai Lau 	 */
413bd86a84SMartin KaFai Lau 	__u32 info;
42*6089fb32SYonghong Song 	/* "size" is used by INT, ENUM, STRUCT, UNION, DATASEC and ENUM64.
433bd86a84SMartin KaFai Lau 	 * "size" tells the size of the type it is describing.
443bd86a84SMartin KaFai Lau 	 *
45781e775eSMartin KaFai Lau 	 * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
468c42d2faSYonghong Song 	 * FUNC, FUNC_PROTO, VAR, DECL_TAG and TYPE_TAG.
473bd86a84SMartin KaFai Lau 	 * "type" is a type_id referring to another type.
483bd86a84SMartin KaFai Lau 	 */
493bd86a84SMartin KaFai Lau 	union {
503bd86a84SMartin KaFai Lau 		__u32 size;
513bd86a84SMartin KaFai Lau 		__u32 type;
523bd86a84SMartin KaFai Lau 	};
533bd86a84SMartin KaFai Lau };
543bd86a84SMartin KaFai Lau 
558fd88691SIlya Leoshkevich #define BTF_INFO_KIND(info)	(((info) >> 24) & 0x1f)
563bd86a84SMartin KaFai Lau #define BTF_INFO_VLEN(info)	((info) & 0xffff)
57128b343dSYonghong Song #define BTF_INFO_KFLAG(info)	((info) >> 31)
583bd86a84SMartin KaFai Lau 
5941ced4cdSYonghong Song enum {
6041ced4cdSYonghong Song 	BTF_KIND_UNKN		= 0,	/* Unknown	*/
6141ced4cdSYonghong Song 	BTF_KIND_INT		= 1,	/* Integer	*/
6241ced4cdSYonghong Song 	BTF_KIND_PTR		= 2,	/* Pointer	*/
6341ced4cdSYonghong Song 	BTF_KIND_ARRAY		= 3,	/* Array	*/
6441ced4cdSYonghong Song 	BTF_KIND_STRUCT		= 4,	/* Struct	*/
6541ced4cdSYonghong Song 	BTF_KIND_UNION		= 5,	/* Union	*/
66*6089fb32SYonghong Song 	BTF_KIND_ENUM		= 6,	/* Enumeration up to 32-bit values */
6741ced4cdSYonghong Song 	BTF_KIND_FWD		= 7,	/* Forward	*/
6841ced4cdSYonghong Song 	BTF_KIND_TYPEDEF	= 8,	/* Typedef	*/
6941ced4cdSYonghong Song 	BTF_KIND_VOLATILE	= 9,	/* Volatile	*/
7041ced4cdSYonghong Song 	BTF_KIND_CONST		= 10,	/* Const	*/
7141ced4cdSYonghong Song 	BTF_KIND_RESTRICT	= 11,	/* Restrict	*/
7241ced4cdSYonghong Song 	BTF_KIND_FUNC		= 12,	/* Function	*/
7341ced4cdSYonghong Song 	BTF_KIND_FUNC_PROTO	= 13,	/* Function Proto	*/
7441ced4cdSYonghong Song 	BTF_KIND_VAR		= 14,	/* Variable	*/
7541ced4cdSYonghong Song 	BTF_KIND_DATASEC	= 15,	/* Section	*/
7641ced4cdSYonghong Song 	BTF_KIND_FLOAT		= 16,	/* Floating point	*/
77223f903eSYonghong Song 	BTF_KIND_DECL_TAG	= 17,	/* Decl Tag */
788c42d2faSYonghong Song 	BTF_KIND_TYPE_TAG	= 18,	/* Type Tag */
79*6089fb32SYonghong Song 	BTF_KIND_ENUM64		= 19,	/* Enumeration up to 64-bit values */
8041ced4cdSYonghong Song 
8141ced4cdSYonghong Song 	NR_BTF_KINDS,
8241ced4cdSYonghong Song 	BTF_KIND_MAX		= NR_BTF_KINDS - 1,
8341ced4cdSYonghong Song };
843bd86a84SMartin KaFai Lau 
853bd86a84SMartin KaFai Lau /* For some specific BTF_KIND, "struct btf_type" is immediately
863bd86a84SMartin KaFai Lau  * followed by extra data.
873bd86a84SMartin KaFai Lau  */
883bd86a84SMartin KaFai Lau 
893bd86a84SMartin KaFai Lau /* BTF_KIND_INT is followed by a u32 and the following
903bd86a84SMartin KaFai Lau  * is the 32 bits arrangement:
913bd86a84SMartin KaFai Lau  */
92f03b15d3SMartin KaFai Lau #define BTF_INT_ENCODING(VAL)	(((VAL) & 0x0f000000) >> 24)
932474c628SGary Lin #define BTF_INT_OFFSET(VAL)	(((VAL) & 0x00ff0000) >> 16)
9464bb5684SMartin KaFai Lau #define BTF_INT_BITS(VAL)	((VAL)  & 0x000000ff)
953bd86a84SMartin KaFai Lau 
963bd86a84SMartin KaFai Lau /* Attributes stored in the BTF_INT_ENCODING */
97f03b15d3SMartin KaFai Lau #define BTF_INT_SIGNED	(1 << 0)
98f03b15d3SMartin KaFai Lau #define BTF_INT_CHAR	(1 << 1)
99f03b15d3SMartin KaFai Lau #define BTF_INT_BOOL	(1 << 2)
1003bd86a84SMartin KaFai Lau 
1013bd86a84SMartin KaFai Lau /* BTF_KIND_ENUM is followed by multiple "struct btf_enum".
1023bd86a84SMartin KaFai Lau  * The exact number of btf_enum is stored in the vlen (of the
1033bd86a84SMartin KaFai Lau  * info in "struct btf_type").
1043bd86a84SMartin KaFai Lau  */
1053bd86a84SMartin KaFai Lau struct btf_enum {
106fbcf93ebSMartin KaFai Lau 	__u32	name_off;
1073bd86a84SMartin KaFai Lau 	__s32	val;
1083bd86a84SMartin KaFai Lau };
1093bd86a84SMartin KaFai Lau 
1103bd86a84SMartin KaFai Lau /* BTF_KIND_ARRAY is followed by one "struct btf_array" */
1113bd86a84SMartin KaFai Lau struct btf_array {
1123bd86a84SMartin KaFai Lau 	__u32	type;
1133bd86a84SMartin KaFai Lau 	__u32	index_type;
1143bd86a84SMartin KaFai Lau 	__u32	nelems;
1153bd86a84SMartin KaFai Lau };
1163bd86a84SMartin KaFai Lau 
1173bd86a84SMartin KaFai Lau /* BTF_KIND_STRUCT and BTF_KIND_UNION are followed
1183bd86a84SMartin KaFai Lau  * by multiple "struct btf_member".  The exact number
1193bd86a84SMartin KaFai Lau  * of btf_member is stored in the vlen (of the info in
1203bd86a84SMartin KaFai Lau  * "struct btf_type").
1213bd86a84SMartin KaFai Lau  */
1223bd86a84SMartin KaFai Lau struct btf_member {
123fbcf93ebSMartin KaFai Lau 	__u32	name_off;
1243bd86a84SMartin KaFai Lau 	__u32	type;
125128b343dSYonghong Song 	/* If the type info kind_flag is set, the btf_member offset
126128b343dSYonghong Song 	 * contains both member bitfield size and bit offset. The
127128b343dSYonghong Song 	 * bitfield size is set for bitfield members. If the type
128128b343dSYonghong Song 	 * info kind_flag is not set, the offset contains only bit
129128b343dSYonghong Song 	 * offset.
130128b343dSYonghong Song 	 */
131128b343dSYonghong Song 	__u32	offset;
1323bd86a84SMartin KaFai Lau };
1333bd86a84SMartin KaFai Lau 
134128b343dSYonghong Song /* If the struct/union type info kind_flag is set, the
135128b343dSYonghong Song  * following two macros are used to access bitfield_size
136128b343dSYonghong Song  * and bit_offset from btf_member.offset.
137128b343dSYonghong Song  */
138128b343dSYonghong Song #define BTF_MEMBER_BITFIELD_SIZE(val)	((val) >> 24)
139128b343dSYonghong Song #define BTF_MEMBER_BIT_OFFSET(val)	((val) & 0xffffff)
140128b343dSYonghong Song 
141781e775eSMartin KaFai Lau /* BTF_KIND_FUNC_PROTO is followed by multiple "struct btf_param".
142781e775eSMartin KaFai Lau  * The exact number of btf_param is stored in the vlen (of the
143781e775eSMartin KaFai Lau  * info in "struct btf_type").
144781e775eSMartin KaFai Lau  */
145781e775eSMartin KaFai Lau struct btf_param {
146781e775eSMartin KaFai Lau 	__u32	name_off;
147781e775eSMartin KaFai Lau 	__u32	type;
148781e775eSMartin KaFai Lau };
149781e775eSMartin KaFai Lau 
150c83fef6bSDaniel Borkmann enum {
151c83fef6bSDaniel Borkmann 	BTF_VAR_STATIC = 0,
152166750bcSAndrii Nakryiko 	BTF_VAR_GLOBAL_ALLOCATED = 1,
153166750bcSAndrii Nakryiko 	BTF_VAR_GLOBAL_EXTERN = 2,
154c83fef6bSDaniel Borkmann };
155c83fef6bSDaniel Borkmann 
1562d3eb67fSAlexei Starovoitov enum btf_func_linkage {
1572d3eb67fSAlexei Starovoitov 	BTF_FUNC_STATIC = 0,
1582d3eb67fSAlexei Starovoitov 	BTF_FUNC_GLOBAL = 1,
1592d3eb67fSAlexei Starovoitov 	BTF_FUNC_EXTERN = 2,
1602d3eb67fSAlexei Starovoitov };
1612d3eb67fSAlexei Starovoitov 
162c83fef6bSDaniel Borkmann /* BTF_KIND_VAR is followed by a single "struct btf_var" to describe
163c83fef6bSDaniel Borkmann  * additional information related to the variable such as its linkage.
164c83fef6bSDaniel Borkmann  */
165c83fef6bSDaniel Borkmann struct btf_var {
166c83fef6bSDaniel Borkmann 	__u32	linkage;
167c83fef6bSDaniel Borkmann };
168c83fef6bSDaniel Borkmann 
169c83fef6bSDaniel Borkmann /* BTF_KIND_DATASEC is followed by multiple "struct btf_var_secinfo"
170c83fef6bSDaniel Borkmann  * to describe all BTF_KIND_VAR types it contains along with it's
171c83fef6bSDaniel Borkmann  * in-section offset as well as size.
172c83fef6bSDaniel Borkmann  */
173c83fef6bSDaniel Borkmann struct btf_var_secinfo {
174c83fef6bSDaniel Borkmann 	__u32	type;
175c83fef6bSDaniel Borkmann 	__u32	offset;
176c83fef6bSDaniel Borkmann 	__u32	size;
177c83fef6bSDaniel Borkmann };
178c83fef6bSDaniel Borkmann 
179223f903eSYonghong Song /* BTF_KIND_DECL_TAG is followed by a single "struct btf_decl_tag" to describe
180b5ea834dSYonghong Song  * additional information related to the tag applied location.
181b5ea834dSYonghong Song  * If component_idx == -1, the tag is applied to a struct, union,
182b5ea834dSYonghong Song  * variable or function. Otherwise, it is applied to a struct/union
183b5ea834dSYonghong Song  * member or a func argument, and component_idx indicates which member
184b5ea834dSYonghong Song  * or argument (0 ... vlen-1).
185b5ea834dSYonghong Song  */
186223f903eSYonghong Song struct btf_decl_tag {
187b5ea834dSYonghong Song        __s32   component_idx;
188b5ea834dSYonghong Song };
189b5ea834dSYonghong Song 
190*6089fb32SYonghong Song /* BTF_KIND_ENUM64 is followed by multiple "struct btf_enum64".
191*6089fb32SYonghong Song  * The exact number of btf_enum64 is stored in the vlen (of the
192*6089fb32SYonghong Song  * info in "struct btf_type").
193*6089fb32SYonghong Song  */
194*6089fb32SYonghong Song struct btf_enum64 {
195*6089fb32SYonghong Song 	__u32	name_off;
196*6089fb32SYonghong Song 	__u32	val_lo32;
197*6089fb32SYonghong Song 	__u32	val_hi32;
198*6089fb32SYonghong Song };
199*6089fb32SYonghong Song 
2003bd86a84SMartin KaFai Lau #endif /* _UAPI__LINUX_BTF_H__ */
201