xref: /linux/tools/include/uapi/linux/btf.h (revision 33c5a3278bdbef42fed02f1f4602a0367b954d03)
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /* Copyright (c) 2018 Facebook */
3 #ifndef _UAPI__LINUX_BTF_H__
4 #define _UAPI__LINUX_BTF_H__
5 
6 #include <linux/types.h>
7 
8 #define BTF_MAGIC	0xeB9F
9 #define BTF_VERSION	1
10 
11 /*
12  * BTF layout section consists of a struct btf_layout for each known
13  * kind at BTF encoding time.
14  */
15 struct btf_layout {
16 	__u8 info_sz;		/* size of singular element after btf_type */
17 	__u8 elem_sz;		/* size of each of btf_vlen(t) elements */
18 	__u16 flags;		/* currently unused */
19 };
20 
21 struct btf_header {
22 	__u16	magic;
23 	__u8	version;
24 	__u8	flags;
25 	__u32	hdr_len;
26 
27 	/* All offsets are in bytes relative to the end of this header */
28 	__u32	type_off;	/* offset of type section	*/
29 	__u32	type_len;	/* length of type section	*/
30 	__u32	str_off;	/* offset of string section	*/
31 	__u32	str_len;	/* length of string section	*/
32 	__u32	layout_off;	/* offset of layout section	*/
33 	__u32	layout_len;	/* length of layout section	*/
34 };
35 
36 enum btf_max {
37 	/* Max possible kind */
38 	BTF_MAX_KIND =		0x0000007f,
39 	/* Max # of type identifier */
40 	BTF_MAX_TYPE =		0x000fffff,
41 	/* Max offset into the string section */
42 	BTF_MAX_NAME_OFFSET =	0x00ffffff,
43 	/* Max # of struct/union/enum members or func args */
44 	BTF_MAX_VLEN =		0x00ffffff,
45 };
46 
47 struct btf_type {
48 	__u32 name_off;
49 	/* "info" bits arrangement
50 	 * bits  0-23: vlen (e.g. # of struct's members)
51 	 * bits 24-30: kind (e.g. int, ptr, array...etc)
52 	 * bit     31: kind_flag, currently used by
53 	 *             struct, union, enum, fwd, enum64,
54 	 *             decl_tag and type_tag
55 	 */
56 	__u32 info;
57 	/* "size" is used by INT, ENUM, STRUCT, UNION, DATASEC, ENUM64
58 	 * and LOC_PARAM. "size" tells the size of the type it is describing.
59 	 *
60 	 * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
61 	 * FUNC, FUNC_PROTO, VAR, DECL_TAG and TYPE_TAG.
62 	 * "type" is a type_id referring to another type.
63 	 */
64 	union {
65 		__u32 size;
66 		__u32 type;
67 	};
68 };
69 
70 #define BTF_INFO_KIND(info)	(((info) >> 24) & 0x7f)
71 #define BTF_INFO_VLEN(info)	((info) & 0xffffff)
72 #define BTF_INFO_KFLAG(info)	((info) >> 31)
73 
74 enum {
75 	BTF_KIND_UNKN		= 0,	/* Unknown	*/
76 	BTF_KIND_INT		= 1,	/* Integer	*/
77 	BTF_KIND_PTR		= 2,	/* Pointer	*/
78 	BTF_KIND_ARRAY		= 3,	/* Array	*/
79 	BTF_KIND_STRUCT		= 4,	/* Struct	*/
80 	BTF_KIND_UNION		= 5,	/* Union	*/
81 	BTF_KIND_ENUM		= 6,	/* Enumeration up to 32-bit values */
82 	BTF_KIND_FWD		= 7,	/* Forward	*/
83 	BTF_KIND_TYPEDEF	= 8,	/* Typedef	*/
84 	BTF_KIND_VOLATILE	= 9,	/* Volatile	*/
85 	BTF_KIND_CONST		= 10,	/* Const	*/
86 	BTF_KIND_RESTRICT	= 11,	/* Restrict	*/
87 	BTF_KIND_FUNC		= 12,	/* Function	*/
88 	BTF_KIND_FUNC_PROTO	= 13,	/* Function Proto	*/
89 	BTF_KIND_VAR		= 14,	/* Variable	*/
90 	BTF_KIND_DATASEC	= 15,	/* Section	*/
91 	BTF_KIND_FLOAT		= 16,	/* Floating point	*/
92 	BTF_KIND_DECL_TAG	= 17,	/* Decl Tag */
93 	BTF_KIND_TYPE_TAG	= 18,	/* Type Tag */
94 	BTF_KIND_ENUM64		= 19,	/* Enumeration up to 64-bit values */
95 	BTF_KIND_LOC_PARAM	= 20,	/* Location parameter information */
96 	BTF_KIND_LOC_PROTO	= 21,	/* Location prototype for site */
97 	BTF_KIND_LOCSEC		= 22,	/* Location section */
98 	NR_BTF_KINDS,
99 	BTF_KIND_MAX		= NR_BTF_KINDS - 1,
100 };
101 
102 /* For some specific BTF_KIND, "struct btf_type" is immediately
103  * followed by extra data.
104  */
105 
106 /* BTF_KIND_INT is followed by a u32 and the following
107  * is the 32 bits arrangement:
108  */
109 #define BTF_INT_ENCODING(VAL)	(((VAL) & 0x0f000000) >> 24)
110 #define BTF_INT_OFFSET(VAL)	(((VAL) & 0x00ff0000) >> 16)
111 #define BTF_INT_BITS(VAL)	((VAL)  & 0x000000ff)
112 
113 /* Attributes stored in the BTF_INT_ENCODING */
114 #define BTF_INT_SIGNED	(1 << 0)
115 #define BTF_INT_CHAR	(1 << 1)
116 #define BTF_INT_BOOL	(1 << 2)
117 
118 /* BTF_KIND_ENUM is followed by multiple "struct btf_enum".
119  * The exact number of btf_enum is stored in the vlen (of the
120  * info in "struct btf_type").
121  */
122 struct btf_enum {
123 	__u32	name_off;
124 	__s32	val;
125 };
126 
127 /* BTF_KIND_ARRAY is followed by one "struct btf_array" */
128 struct btf_array {
129 	__u32	type;
130 	__u32	index_type;
131 	__u32	nelems;
132 };
133 
134 /* BTF_KIND_STRUCT and BTF_KIND_UNION are followed
135  * by multiple "struct btf_member".  The exact number
136  * of btf_member is stored in the vlen (of the info in
137  * "struct btf_type").
138  */
139 struct btf_member {
140 	__u32	name_off;
141 	__u32	type;
142 	/* If the type info kind_flag is set, the btf_member offset
143 	 * contains both member bitfield size and bit offset. The
144 	 * bitfield size is set for bitfield members. If the type
145 	 * info kind_flag is not set, the offset contains only bit
146 	 * offset.
147 	 */
148 	__u32	offset;
149 };
150 
151 /* If the struct/union type info kind_flag is set, the
152  * following two macros are used to access bitfield_size
153  * and bit_offset from btf_member.offset.
154  */
155 #define BTF_MEMBER_BITFIELD_SIZE(val)	((val) >> 24)
156 #define BTF_MEMBER_BIT_OFFSET(val)	((val) & 0xffffff)
157 
158 /* BTF_KIND_FUNC_PROTO is followed by multiple "struct btf_param".
159  * The exact number of btf_param is stored in the vlen (of the
160  * info in "struct btf_type").
161  */
162 struct btf_param {
163 	__u32	name_off;
164 	__u32	type;
165 };
166 
167 enum {
168 	BTF_VAR_STATIC = 0,
169 	BTF_VAR_GLOBAL_ALLOCATED = 1,
170 	BTF_VAR_GLOBAL_EXTERN = 2,
171 };
172 
173 enum btf_func_linkage {
174 	BTF_FUNC_STATIC = 0,
175 	BTF_FUNC_GLOBAL = 1,
176 	BTF_FUNC_EXTERN = 2,
177 };
178 
179 /* BTF_KIND_VAR is followed by a single "struct btf_var" to describe
180  * additional information related to the variable such as its linkage.
181  */
182 struct btf_var {
183 	__u32	linkage;
184 };
185 
186 /* BTF_KIND_DATASEC is followed by multiple "struct btf_var_secinfo"
187  * to describe all BTF_KIND_VAR types it contains along with it's
188  * in-section offset as well as size.
189  */
190 struct btf_var_secinfo {
191 	__u32	type;
192 	__u32	offset;
193 	__u32	size;
194 };
195 
196 /* BTF_KIND_DECL_TAG is followed by a single "struct btf_decl_tag" to describe
197  * additional information related to the tag applied location.
198  * If component_idx == -1, the tag is applied to a struct, union,
199  * variable or function. Otherwise, it is applied to a struct/union
200  * member or a func argument, and component_idx indicates which member
201  * or argument (0 ... vlen-1).
202  */
203 struct btf_decl_tag {
204        __s32   component_idx;
205 };
206 
207 /* BTF_KIND_ENUM64 is followed by multiple "struct btf_enum64".
208  * The exact number of btf_enum64 is stored in the vlen (of the
209  * info in "struct btf_type").
210  */
211 struct btf_enum64 {
212 	__u32	name_off;
213 	__u32	val_lo32;
214 	__u32	val_hi32;
215 };
216 
217 /*
218  * BTF_KIND_LOC_PARAM is followed by a single "struct btf_loc_param"
219  * that contains flags specifying the contents of the vlen-specified
220  * number of 4-byte values that follow.
221  */
222 struct btf_loc_param {
223 	__u32 flags;
224 	__u32 values[];
225 };
226 
227 /*
228  * The combination of size, vlen and flags gives us the means to interpret
229  * the following vlen-specified set of 4-byte values:
230  *
231  * - a BTF_LOC_PARAM_CONST is a constant value; combination
232  *   of size, vlen and _SIGNED flag determines it. If the value requires
233  *   64 bits it is stored in {lo,hi} order.
234  * - a BTF_LOC_PARAM_ADDR|BTF_LOC_PARAM_CONST is an address that should be
235  *   normalized with respect to kernel/module base address.
236  * - a BTF_LOC_PARAM_REG with vlen 1 is a simple register number;
237  *   with vlen 2 it is a multi-register parameter.  Register numbers are
238  *   numbers derived from DW_OP_reg values, i.e. 0 is DW_OP_reg0; since
239  *   DW_OP_fbreg has its own special DW_OP_value and DW_OP_regx can refer
240  *   to an arbitrary register number, we reserve BTF_LOC_PARAM_FBREG for
241  *   the frame base register to avoid collisions.
242  * - a _REG | OFFSET describes an address without dereferencing it.
243  * - a _REG | DEREF with vlen 1 dereferences the value in the register
244  *   number specified.
245  * - a REG | DEREF | OFFSET with vlen > 1 specifies the register number
246  *   in the first 4-byte value and the offset in the remainder.
247  *   In the case of REG and OFFSET combinations, the OFFSET has the width
248  *   of the value words while the type size describes the represented parameter,
249  *   so for example a REG | DEREF | OFFSET with size 8 and vlen 2 would be
250  *   an 8-byte register dereference with signed 4-byte offset, since the vlen 2
251  *   values consist of a register value and the signed value.
252  */
253 enum btf_loc_param_flags {
254 	BTF_LOC_PARAM_SIGNED		=	0x1,
255 	BTF_LOC_PARAM_CONST		=	0x2,
256 	BTF_LOC_PARAM_ADDR		=	0x4,
257 	BTF_LOC_PARAM_REG		=	0x8,
258 	BTF_LOC_PARAM_DEREF		=	0x10,
259 	BTF_LOC_PARAM_OFFSET		=	0x20,
260 };
261 
262 enum {
263 	BTF_LOC_PARAM_FBREG		=	0xffffffff
264 };
265 
266 /*
267  * BTF_KIND_LOC_PROTO specifies location prototypes; i.e. how locations relate
268  * to parameters; a struct btf_type of BTF_KIND_LOC_PROTO is followed by a
269  * vlen-specified number of __u32 BTF type ids which specify the associated
270  * BTF_KIND_LOC_PARAM for each function parameter associated with the
271  * location.  The type should either be 0 (no location info) or point at
272  * a BTF_KIND_LOC_PARAM.
273  */
274 
275 /*
276  * BTF_KIND_LOCSEC consists of vlen-specified number of "struct btf_loc"
277  * containing location site-specific information for a specific ELF section;
278  * for example locations in ".text" are in a LOCSEC named "inline.text".
279  *
280  * - function (func)
281  * - location prototype type id (loc_proto)
282  * - address offset (offset) relative to the runtime base address of the
283  *   ELF section associated with the LOCSEC
284  */
285 
286 struct btf_loc {
287 	__u32 func;
288 	__u32 loc_proto;
289 	__u32 offset;
290 };
291 
292 #endif /* _UAPI__LINUX_BTF_H__ */
293