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 struct btf_header {
12 	__u16	magic;
13 	__u8	version;
14 	__u8	flags;
15 	__u32	hdr_len;
16 
17 	/* All offsets are in bytes relative to the end of this header */
18 	__u32	type_off;	/* offset of type section	*/
19 	__u32	type_len;	/* length of type section	*/
20 	__u32	str_off;	/* offset of string section	*/
21 	__u32	str_len;	/* length of string section	*/
22 };
23 
24 /* Max # of type identifier */
25 #define BTF_MAX_TYPE	0x000fffff
26 /* Max offset into the string section */
27 #define BTF_MAX_NAME_OFFSET	0x00ffffff
28 /* Max # of struct/union/enum members or func args */
29 #define BTF_MAX_VLEN	0xffff
30 
31 struct btf_type {
32 	__u32 name_off;
33 	/* "info" bits arrangement
34 	 * bits  0-15: vlen (e.g. # of struct's members)
35 	 * bits 16-23: unused
36 	 * bits 24-28: kind (e.g. int, ptr, array...etc)
37 	 * bits 29-30: unused
38 	 * bit     31: kind_flag, currently used by
39 	 *             struct, union, enum, fwd, enum64,
40 	 *             decl_tag and type_tag
41 	 */
42 	__u32 info;
43 	/* "size" is used by INT, ENUM, STRUCT, UNION, DATASEC and ENUM64.
44 	 * "size" tells the size of the type it is describing.
45 	 *
46 	 * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
47 	 * FUNC, FUNC_PROTO, VAR, DECL_TAG and TYPE_TAG.
48 	 * "type" is a type_id referring to another type.
49 	 */
50 	union {
51 		__u32 size;
52 		__u32 type;
53 	};
54 };
55 
56 #define BTF_INFO_KIND(info)	(((info) >> 24) & 0x1f)
57 #define BTF_INFO_VLEN(info)	((info) & 0xffff)
58 #define BTF_INFO_KFLAG(info)	((info) >> 31)
59 
60 enum {
61 	BTF_KIND_UNKN		= 0,	/* Unknown	*/
62 	BTF_KIND_INT		= 1,	/* Integer	*/
63 	BTF_KIND_PTR		= 2,	/* Pointer	*/
64 	BTF_KIND_ARRAY		= 3,	/* Array	*/
65 	BTF_KIND_STRUCT		= 4,	/* Struct	*/
66 	BTF_KIND_UNION		= 5,	/* Union	*/
67 	BTF_KIND_ENUM		= 6,	/* Enumeration up to 32-bit values */
68 	BTF_KIND_FWD		= 7,	/* Forward	*/
69 	BTF_KIND_TYPEDEF	= 8,	/* Typedef	*/
70 	BTF_KIND_VOLATILE	= 9,	/* Volatile	*/
71 	BTF_KIND_CONST		= 10,	/* Const	*/
72 	BTF_KIND_RESTRICT	= 11,	/* Restrict	*/
73 	BTF_KIND_FUNC		= 12,	/* Function	*/
74 	BTF_KIND_FUNC_PROTO	= 13,	/* Function Proto	*/
75 	BTF_KIND_VAR		= 14,	/* Variable	*/
76 	BTF_KIND_DATASEC	= 15,	/* Section	*/
77 	BTF_KIND_FLOAT		= 16,	/* Floating point	*/
78 	BTF_KIND_DECL_TAG	= 17,	/* Decl Tag */
79 	BTF_KIND_TYPE_TAG	= 18,	/* Type Tag */
80 	BTF_KIND_ENUM64		= 19,	/* Enumeration up to 64-bit values */
81 
82 	NR_BTF_KINDS,
83 	BTF_KIND_MAX		= NR_BTF_KINDS - 1,
84 };
85 
86 /* For some specific BTF_KIND, "struct btf_type" is immediately
87  * followed by extra data.
88  */
89 
90 /* BTF_KIND_INT is followed by a u32 and the following
91  * is the 32 bits arrangement:
92  */
93 #define BTF_INT_ENCODING(VAL)	(((VAL) & 0x0f000000) >> 24)
94 #define BTF_INT_OFFSET(VAL)	(((VAL) & 0x00ff0000) >> 16)
95 #define BTF_INT_BITS(VAL)	((VAL)  & 0x000000ff)
96 
97 /* Attributes stored in the BTF_INT_ENCODING */
98 #define BTF_INT_SIGNED	(1 << 0)
99 #define BTF_INT_CHAR	(1 << 1)
100 #define BTF_INT_BOOL	(1 << 2)
101 
102 /* BTF_KIND_ENUM is followed by multiple "struct btf_enum".
103  * The exact number of btf_enum is stored in the vlen (of the
104  * info in "struct btf_type").
105  */
106 struct btf_enum {
107 	__u32	name_off;
108 	__s32	val;
109 };
110 
111 /* BTF_KIND_ARRAY is followed by one "struct btf_array" */
112 struct btf_array {
113 	__u32	type;
114 	__u32	index_type;
115 	__u32	nelems;
116 };
117 
118 /* BTF_KIND_STRUCT and BTF_KIND_UNION are followed
119  * by multiple "struct btf_member".  The exact number
120  * of btf_member is stored in the vlen (of the info in
121  * "struct btf_type").
122  */
123 struct btf_member {
124 	__u32	name_off;
125 	__u32	type;
126 	/* If the type info kind_flag is set, the btf_member offset
127 	 * contains both member bitfield size and bit offset. The
128 	 * bitfield size is set for bitfield members. If the type
129 	 * info kind_flag is not set, the offset contains only bit
130 	 * offset.
131 	 */
132 	__u32	offset;
133 };
134 
135 /* If the struct/union type info kind_flag is set, the
136  * following two macros are used to access bitfield_size
137  * and bit_offset from btf_member.offset.
138  */
139 #define BTF_MEMBER_BITFIELD_SIZE(val)	((val) >> 24)
140 #define BTF_MEMBER_BIT_OFFSET(val)	((val) & 0xffffff)
141 
142 /* BTF_KIND_FUNC_PROTO is followed by multiple "struct btf_param".
143  * The exact number of btf_param is stored in the vlen (of the
144  * info in "struct btf_type").
145  */
146 struct btf_param {
147 	__u32	name_off;
148 	__u32	type;
149 };
150 
151 enum {
152 	BTF_VAR_STATIC = 0,
153 	BTF_VAR_GLOBAL_ALLOCATED = 1,
154 	BTF_VAR_GLOBAL_EXTERN = 2,
155 };
156 
157 enum btf_func_linkage {
158 	BTF_FUNC_STATIC = 0,
159 	BTF_FUNC_GLOBAL = 1,
160 	BTF_FUNC_EXTERN = 2,
161 };
162 
163 /* BTF_KIND_VAR is followed by a single "struct btf_var" to describe
164  * additional information related to the variable such as its linkage.
165  */
166 struct btf_var {
167 	__u32	linkage;
168 };
169 
170 /* BTF_KIND_DATASEC is followed by multiple "struct btf_var_secinfo"
171  * to describe all BTF_KIND_VAR types it contains along with it's
172  * in-section offset as well as size.
173  */
174 struct btf_var_secinfo {
175 	__u32	type;
176 	__u32	offset;
177 	__u32	size;
178 };
179 
180 /* BTF_KIND_DECL_TAG is followed by a single "struct btf_decl_tag" to describe
181  * additional information related to the tag applied location.
182  * If component_idx == -1, the tag is applied to a struct, union,
183  * variable or function. Otherwise, it is applied to a struct/union
184  * member or a func argument, and component_idx indicates which member
185  * or argument (0 ... vlen-1).
186  */
187 struct btf_decl_tag {
188        __s32   component_idx;
189 };
190 
191 /* BTF_KIND_ENUM64 is followed by multiple "struct btf_enum64".
192  * The exact number of btf_enum64 is stored in the vlen (of the
193  * info in "struct btf_type").
194  */
195 struct btf_enum64 {
196 	__u32	name_off;
197 	__u32	val_lo32;
198 	__u32	val_hi32;
199 };
200 
201 #endif /* _UAPI__LINUX_BTF_H__ */
202