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