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