169b693f0SMartin KaFai Lau /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 269b693f0SMartin KaFai Lau /* Copyright (c) 2018 Facebook */ 369b693f0SMartin KaFai Lau #ifndef _UAPI__LINUX_BTF_H__ 469b693f0SMartin KaFai Lau #define _UAPI__LINUX_BTF_H__ 569b693f0SMartin KaFai Lau 669b693f0SMartin KaFai Lau #include <linux/types.h> 769b693f0SMartin KaFai Lau 869b693f0SMartin KaFai Lau #define BTF_MAGIC 0xeB9F 969b693f0SMartin KaFai Lau #define BTF_VERSION 1 1069b693f0SMartin KaFai Lau 1169b693f0SMartin KaFai Lau struct btf_header { 1269b693f0SMartin KaFai Lau __u16 magic; 1369b693f0SMartin KaFai Lau __u8 version; 1469b693f0SMartin KaFai Lau __u8 flags; 15f80442a4SMartin KaFai Lau __u32 hdr_len; 1669b693f0SMartin KaFai Lau 1769b693f0SMartin KaFai Lau /* All offsets are in bytes relative to the end of this header */ 1869b693f0SMartin KaFai Lau __u32 type_off; /* offset of type section */ 19f80442a4SMartin KaFai Lau __u32 type_len; /* length of type section */ 2069b693f0SMartin KaFai Lau __u32 str_off; /* offset of string section */ 2169b693f0SMartin KaFai Lau __u32 str_len; /* length of string section */ 2269b693f0SMartin KaFai Lau }; 2369b693f0SMartin KaFai Lau 2469b693f0SMartin KaFai Lau /* Max # of type identifier */ 25a0791f0dSAlexei Starovoitov #define BTF_MAX_TYPE 0x000fffff 2669b693f0SMartin KaFai Lau /* Max offset into the string section */ 27a0791f0dSAlexei Starovoitov #define BTF_MAX_NAME_OFFSET 0x00ffffff 2869b693f0SMartin KaFai Lau /* Max # of struct/union/enum members or func args */ 2969b693f0SMartin KaFai Lau #define BTF_MAX_VLEN 0xffff 3069b693f0SMartin KaFai Lau 3169b693f0SMartin KaFai Lau struct btf_type { 32fbcf93ebSMartin KaFai Lau __u32 name_off; 3369b693f0SMartin KaFai Lau /* "info" bits arrangement 3469b693f0SMartin KaFai Lau * bits 0-15: vlen (e.g. # of struct's members) 3569b693f0SMartin KaFai Lau * bits 16-23: unused 3666df0fdbSHaiyue Wang * bits 24-28: kind (e.g. int, ptr, array...etc) 3766df0fdbSHaiyue Wang * bits 29-30: unused 389d5f9f70SYonghong Song * bit 31: kind_flag, currently used by 39*ea70faa1SIhor Solodrai * struct, union, enum, fwd, enum64, 40*ea70faa1SIhor Solodrai * decl_tag and type_tag 4169b693f0SMartin KaFai Lau */ 4269b693f0SMartin KaFai Lau __u32 info; 436089fb32SYonghong Song /* "size" is used by INT, ENUM, STRUCT, UNION, DATASEC and ENUM64. 4469b693f0SMartin KaFai Lau * "size" tells the size of the type it is describing. 4569b693f0SMartin KaFai Lau * 462667a262SMartin KaFai Lau * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, 478c42d2faSYonghong Song * FUNC, FUNC_PROTO, VAR, DECL_TAG and TYPE_TAG. 4869b693f0SMartin KaFai Lau * "type" is a type_id referring to another type. 4969b693f0SMartin KaFai Lau */ 5069b693f0SMartin KaFai Lau union { 5169b693f0SMartin KaFai Lau __u32 size; 5269b693f0SMartin KaFai Lau __u32 type; 5369b693f0SMartin KaFai Lau }; 5469b693f0SMartin KaFai Lau }; 5569b693f0SMartin KaFai Lau 568fd88691SIlya Leoshkevich #define BTF_INFO_KIND(info) (((info) >> 24) & 0x1f) 5769b693f0SMartin KaFai Lau #define BTF_INFO_VLEN(info) ((info) & 0xffff) 589d5f9f70SYonghong Song #define BTF_INFO_KFLAG(info) ((info) >> 31) 5969b693f0SMartin KaFai Lau 6041ced4cdSYonghong Song enum { 6141ced4cdSYonghong Song BTF_KIND_UNKN = 0, /* Unknown */ 6241ced4cdSYonghong Song BTF_KIND_INT = 1, /* Integer */ 6341ced4cdSYonghong Song BTF_KIND_PTR = 2, /* Pointer */ 6441ced4cdSYonghong Song BTF_KIND_ARRAY = 3, /* Array */ 6541ced4cdSYonghong Song BTF_KIND_STRUCT = 4, /* Struct */ 6641ced4cdSYonghong Song BTF_KIND_UNION = 5, /* Union */ 676089fb32SYonghong Song BTF_KIND_ENUM = 6, /* Enumeration up to 32-bit values */ 6841ced4cdSYonghong Song BTF_KIND_FWD = 7, /* Forward */ 6941ced4cdSYonghong Song BTF_KIND_TYPEDEF = 8, /* Typedef */ 7041ced4cdSYonghong Song BTF_KIND_VOLATILE = 9, /* Volatile */ 7141ced4cdSYonghong Song BTF_KIND_CONST = 10, /* Const */ 7241ced4cdSYonghong Song BTF_KIND_RESTRICT = 11, /* Restrict */ 7341ced4cdSYonghong Song BTF_KIND_FUNC = 12, /* Function */ 7441ced4cdSYonghong Song BTF_KIND_FUNC_PROTO = 13, /* Function Proto */ 7541ced4cdSYonghong Song BTF_KIND_VAR = 14, /* Variable */ 7641ced4cdSYonghong Song BTF_KIND_DATASEC = 15, /* Section */ 7741ced4cdSYonghong Song BTF_KIND_FLOAT = 16, /* Floating point */ 78223f903eSYonghong Song BTF_KIND_DECL_TAG = 17, /* Decl Tag */ 798c42d2faSYonghong Song BTF_KIND_TYPE_TAG = 18, /* Type Tag */ 806089fb32SYonghong Song BTF_KIND_ENUM64 = 19, /* Enumeration up to 64-bit values */ 8141ced4cdSYonghong Song 8241ced4cdSYonghong Song NR_BTF_KINDS, 8341ced4cdSYonghong Song BTF_KIND_MAX = NR_BTF_KINDS - 1, 8441ced4cdSYonghong Song }; 8569b693f0SMartin KaFai Lau 8669b693f0SMartin KaFai Lau /* For some specific BTF_KIND, "struct btf_type" is immediately 8769b693f0SMartin KaFai Lau * followed by extra data. 8869b693f0SMartin KaFai Lau */ 8969b693f0SMartin KaFai Lau 9069b693f0SMartin KaFai Lau /* BTF_KIND_INT is followed by a u32 and the following 9169b693f0SMartin KaFai Lau * is the 32 bits arrangement: 9269b693f0SMartin KaFai Lau */ 93aea2f7b8SMartin KaFai Lau #define BTF_INT_ENCODING(VAL) (((VAL) & 0x0f000000) >> 24) 94948dc8c9SGary Lin #define BTF_INT_OFFSET(VAL) (((VAL) & 0x00ff0000) >> 16) 9536fc3c8cSMartin KaFai Lau #define BTF_INT_BITS(VAL) ((VAL) & 0x000000ff) 9669b693f0SMartin KaFai Lau 9769b693f0SMartin KaFai Lau /* Attributes stored in the BTF_INT_ENCODING */ 98aea2f7b8SMartin KaFai Lau #define BTF_INT_SIGNED (1 << 0) 99aea2f7b8SMartin KaFai Lau #define BTF_INT_CHAR (1 << 1) 100aea2f7b8SMartin KaFai Lau #define BTF_INT_BOOL (1 << 2) 10169b693f0SMartin KaFai Lau 10269b693f0SMartin KaFai Lau /* BTF_KIND_ENUM is followed by multiple "struct btf_enum". 10369b693f0SMartin KaFai Lau * The exact number of btf_enum is stored in the vlen (of the 10469b693f0SMartin KaFai Lau * info in "struct btf_type"). 10569b693f0SMartin KaFai Lau */ 10669b693f0SMartin KaFai Lau struct btf_enum { 107fbcf93ebSMartin KaFai Lau __u32 name_off; 10869b693f0SMartin KaFai Lau __s32 val; 10969b693f0SMartin KaFai Lau }; 11069b693f0SMartin KaFai Lau 11169b693f0SMartin KaFai Lau /* BTF_KIND_ARRAY is followed by one "struct btf_array" */ 11269b693f0SMartin KaFai Lau struct btf_array { 11369b693f0SMartin KaFai Lau __u32 type; 11469b693f0SMartin KaFai Lau __u32 index_type; 11569b693f0SMartin KaFai Lau __u32 nelems; 11669b693f0SMartin KaFai Lau }; 11769b693f0SMartin KaFai Lau 11869b693f0SMartin KaFai Lau /* BTF_KIND_STRUCT and BTF_KIND_UNION are followed 11969b693f0SMartin KaFai Lau * by multiple "struct btf_member". The exact number 12069b693f0SMartin KaFai Lau * of btf_member is stored in the vlen (of the info in 12169b693f0SMartin KaFai Lau * "struct btf_type"). 12269b693f0SMartin KaFai Lau */ 12369b693f0SMartin KaFai Lau struct btf_member { 124fbcf93ebSMartin KaFai Lau __u32 name_off; 12569b693f0SMartin KaFai Lau __u32 type; 1269d5f9f70SYonghong Song /* If the type info kind_flag is set, the btf_member offset 1279d5f9f70SYonghong Song * contains both member bitfield size and bit offset. The 1289d5f9f70SYonghong Song * bitfield size is set for bitfield members. If the type 1299d5f9f70SYonghong Song * info kind_flag is not set, the offset contains only bit 1309d5f9f70SYonghong Song * offset. 1319d5f9f70SYonghong Song */ 1329d5f9f70SYonghong Song __u32 offset; 13369b693f0SMartin KaFai Lau }; 13469b693f0SMartin KaFai Lau 1359d5f9f70SYonghong Song /* If the struct/union type info kind_flag is set, the 1369d5f9f70SYonghong Song * following two macros are used to access bitfield_size 1379d5f9f70SYonghong Song * and bit_offset from btf_member.offset. 1389d5f9f70SYonghong Song */ 1399d5f9f70SYonghong Song #define BTF_MEMBER_BITFIELD_SIZE(val) ((val) >> 24) 1409d5f9f70SYonghong Song #define BTF_MEMBER_BIT_OFFSET(val) ((val) & 0xffffff) 1419d5f9f70SYonghong Song 1422667a262SMartin KaFai Lau /* BTF_KIND_FUNC_PROTO is followed by multiple "struct btf_param". 1432667a262SMartin KaFai Lau * The exact number of btf_param is stored in the vlen (of the 1442667a262SMartin KaFai Lau * info in "struct btf_type"). 1452667a262SMartin KaFai Lau */ 1462667a262SMartin KaFai Lau struct btf_param { 1472667a262SMartin KaFai Lau __u32 name_off; 1482667a262SMartin KaFai Lau __u32 type; 1492667a262SMartin KaFai Lau }; 1502667a262SMartin KaFai Lau 151f063c889SDaniel Borkmann enum { 152f063c889SDaniel Borkmann BTF_VAR_STATIC = 0, 153166750bcSAndrii Nakryiko BTF_VAR_GLOBAL_ALLOCATED = 1, 154166750bcSAndrii Nakryiko BTF_VAR_GLOBAL_EXTERN = 2, 155f063c889SDaniel Borkmann }; 156f063c889SDaniel Borkmann 15751c39bb1SAlexei Starovoitov enum btf_func_linkage { 15851c39bb1SAlexei Starovoitov BTF_FUNC_STATIC = 0, 15951c39bb1SAlexei Starovoitov BTF_FUNC_GLOBAL = 1, 16051c39bb1SAlexei Starovoitov BTF_FUNC_EXTERN = 2, 16151c39bb1SAlexei Starovoitov }; 16251c39bb1SAlexei Starovoitov 163f063c889SDaniel Borkmann /* BTF_KIND_VAR is followed by a single "struct btf_var" to describe 164f063c889SDaniel Borkmann * additional information related to the variable such as its linkage. 165f063c889SDaniel Borkmann */ 166f063c889SDaniel Borkmann struct btf_var { 167f063c889SDaniel Borkmann __u32 linkage; 168f063c889SDaniel Borkmann }; 169f063c889SDaniel Borkmann 170f063c889SDaniel Borkmann /* BTF_KIND_DATASEC is followed by multiple "struct btf_var_secinfo" 171f063c889SDaniel Borkmann * to describe all BTF_KIND_VAR types it contains along with it's 172f063c889SDaniel Borkmann * in-section offset as well as size. 173f063c889SDaniel Borkmann */ 174f063c889SDaniel Borkmann struct btf_var_secinfo { 175f063c889SDaniel Borkmann __u32 type; 176f063c889SDaniel Borkmann __u32 offset; 177f063c889SDaniel Borkmann __u32 size; 178f063c889SDaniel Borkmann }; 179f063c889SDaniel Borkmann 180223f903eSYonghong Song /* BTF_KIND_DECL_TAG is followed by a single "struct btf_decl_tag" to describe 181b5ea834dSYonghong Song * additional information related to the tag applied location. 182b5ea834dSYonghong Song * If component_idx == -1, the tag is applied to a struct, union, 183b5ea834dSYonghong Song * variable or function. Otherwise, it is applied to a struct/union 184b5ea834dSYonghong Song * member or a func argument, and component_idx indicates which member 185b5ea834dSYonghong Song * or argument (0 ... vlen-1). 186b5ea834dSYonghong Song */ 187223f903eSYonghong Song struct btf_decl_tag { 188b5ea834dSYonghong Song __s32 component_idx; 189b5ea834dSYonghong Song }; 190b5ea834dSYonghong Song 1916089fb32SYonghong Song /* BTF_KIND_ENUM64 is followed by multiple "struct btf_enum64". 1926089fb32SYonghong Song * The exact number of btf_enum64 is stored in the vlen (of the 1936089fb32SYonghong Song * info in "struct btf_type"). 1946089fb32SYonghong Song */ 1956089fb32SYonghong Song struct btf_enum64 { 1966089fb32SYonghong Song __u32 name_off; 1976089fb32SYonghong Song __u32 val_lo32; 1986089fb32SYonghong Song __u32 val_hi32; 1996089fb32SYonghong Song }; 2006089fb32SYonghong Song 20169b693f0SMartin KaFai Lau #endif /* _UAPI__LINUX_BTF_H__ */ 202