13bd86a84SMartin KaFai Lau /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 23bd86a84SMartin KaFai Lau /* Copyright (c) 2018 Facebook */ 33bd86a84SMartin KaFai Lau #ifndef _UAPI__LINUX_BTF_H__ 43bd86a84SMartin KaFai Lau #define _UAPI__LINUX_BTF_H__ 53bd86a84SMartin KaFai Lau 63bd86a84SMartin KaFai Lau #include <linux/types.h> 73bd86a84SMartin KaFai Lau 83bd86a84SMartin KaFai Lau #define BTF_MAGIC 0xeB9F 93bd86a84SMartin KaFai Lau #define BTF_VERSION 1 103bd86a84SMartin KaFai Lau 113bd86a84SMartin KaFai Lau struct btf_header { 123bd86a84SMartin KaFai Lau __u16 magic; 133bd86a84SMartin KaFai Lau __u8 version; 143bd86a84SMartin KaFai Lau __u8 flags; 15f03b15d3SMartin KaFai Lau __u32 hdr_len; 163bd86a84SMartin KaFai Lau 173bd86a84SMartin KaFai Lau /* All offsets are in bytes relative to the end of this header */ 183bd86a84SMartin KaFai Lau __u32 type_off; /* offset of type section */ 19f03b15d3SMartin KaFai Lau __u32 type_len; /* length of type section */ 203bd86a84SMartin KaFai Lau __u32 str_off; /* offset of string section */ 213bd86a84SMartin KaFai Lau __u32 str_len; /* length of string section */ 223bd86a84SMartin KaFai Lau }; 233bd86a84SMartin KaFai Lau 243bd86a84SMartin KaFai Lau /* Max # of type identifier */ 25166750bcSAndrii Nakryiko #define BTF_MAX_TYPE 0x000fffff 263bd86a84SMartin KaFai Lau /* Max offset into the string section */ 27166750bcSAndrii Nakryiko #define BTF_MAX_NAME_OFFSET 0x00ffffff 283bd86a84SMartin KaFai Lau /* Max # of struct/union/enum members or func args */ 293bd86a84SMartin KaFai Lau #define BTF_MAX_VLEN 0xffff 303bd86a84SMartin KaFai Lau 313bd86a84SMartin KaFai Lau struct btf_type { 32fbcf93ebSMartin KaFai Lau __u32 name_off; 333bd86a84SMartin KaFai Lau /* "info" bits arrangement 343bd86a84SMartin KaFai Lau * bits 0-15: vlen (e.g. # of struct's members) 353bd86a84SMartin KaFai Lau * bits 16-23: unused 3666df0fdbSHaiyue Wang * bits 24-28: kind (e.g. int, ptr, array...etc) 3766df0fdbSHaiyue Wang * bits 29-30: unused 38128b343dSYonghong Song * bit 31: kind_flag, currently used by 39*ea70faa1SIhor Solodrai * struct, union, enum, fwd, enum64, 40*ea70faa1SIhor Solodrai * decl_tag and type_tag 413bd86a84SMartin KaFai Lau */ 423bd86a84SMartin KaFai Lau __u32 info; 436089fb32SYonghong Song /* "size" is used by INT, ENUM, STRUCT, UNION, DATASEC and ENUM64. 443bd86a84SMartin KaFai Lau * "size" tells the size of the type it is describing. 453bd86a84SMartin KaFai Lau * 46781e775eSMartin KaFai Lau * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, 478c42d2faSYonghong Song * FUNC, FUNC_PROTO, VAR, DECL_TAG and TYPE_TAG. 483bd86a84SMartin KaFai Lau * "type" is a type_id referring to another type. 493bd86a84SMartin KaFai Lau */ 503bd86a84SMartin KaFai Lau union { 513bd86a84SMartin KaFai Lau __u32 size; 523bd86a84SMartin KaFai Lau __u32 type; 533bd86a84SMartin KaFai Lau }; 543bd86a84SMartin KaFai Lau }; 553bd86a84SMartin KaFai Lau 568fd88691SIlya Leoshkevich #define BTF_INFO_KIND(info) (((info) >> 24) & 0x1f) 573bd86a84SMartin KaFai Lau #define BTF_INFO_VLEN(info) ((info) & 0xffff) 58128b343dSYonghong Song #define BTF_INFO_KFLAG(info) ((info) >> 31) 593bd86a84SMartin 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 }; 853bd86a84SMartin KaFai Lau 863bd86a84SMartin KaFai Lau /* For some specific BTF_KIND, "struct btf_type" is immediately 873bd86a84SMartin KaFai Lau * followed by extra data. 883bd86a84SMartin KaFai Lau */ 893bd86a84SMartin KaFai Lau 903bd86a84SMartin KaFai Lau /* BTF_KIND_INT is followed by a u32 and the following 913bd86a84SMartin KaFai Lau * is the 32 bits arrangement: 923bd86a84SMartin KaFai Lau */ 93f03b15d3SMartin KaFai Lau #define BTF_INT_ENCODING(VAL) (((VAL) & 0x0f000000) >> 24) 942474c628SGary Lin #define BTF_INT_OFFSET(VAL) (((VAL) & 0x00ff0000) >> 16) 9564bb5684SMartin KaFai Lau #define BTF_INT_BITS(VAL) ((VAL) & 0x000000ff) 963bd86a84SMartin KaFai Lau 973bd86a84SMartin KaFai Lau /* Attributes stored in the BTF_INT_ENCODING */ 98f03b15d3SMartin KaFai Lau #define BTF_INT_SIGNED (1 << 0) 99f03b15d3SMartin KaFai Lau #define BTF_INT_CHAR (1 << 1) 100f03b15d3SMartin KaFai Lau #define BTF_INT_BOOL (1 << 2) 1013bd86a84SMartin KaFai Lau 1023bd86a84SMartin KaFai Lau /* BTF_KIND_ENUM is followed by multiple "struct btf_enum". 1033bd86a84SMartin KaFai Lau * The exact number of btf_enum is stored in the vlen (of the 1043bd86a84SMartin KaFai Lau * info in "struct btf_type"). 1053bd86a84SMartin KaFai Lau */ 1063bd86a84SMartin KaFai Lau struct btf_enum { 107fbcf93ebSMartin KaFai Lau __u32 name_off; 1083bd86a84SMartin KaFai Lau __s32 val; 1093bd86a84SMartin KaFai Lau }; 1103bd86a84SMartin KaFai Lau 1113bd86a84SMartin KaFai Lau /* BTF_KIND_ARRAY is followed by one "struct btf_array" */ 1123bd86a84SMartin KaFai Lau struct btf_array { 1133bd86a84SMartin KaFai Lau __u32 type; 1143bd86a84SMartin KaFai Lau __u32 index_type; 1153bd86a84SMartin KaFai Lau __u32 nelems; 1163bd86a84SMartin KaFai Lau }; 1173bd86a84SMartin KaFai Lau 1183bd86a84SMartin KaFai Lau /* BTF_KIND_STRUCT and BTF_KIND_UNION are followed 1193bd86a84SMartin KaFai Lau * by multiple "struct btf_member". The exact number 1203bd86a84SMartin KaFai Lau * of btf_member is stored in the vlen (of the info in 1213bd86a84SMartin KaFai Lau * "struct btf_type"). 1223bd86a84SMartin KaFai Lau */ 1233bd86a84SMartin KaFai Lau struct btf_member { 124fbcf93ebSMartin KaFai Lau __u32 name_off; 1253bd86a84SMartin KaFai Lau __u32 type; 126128b343dSYonghong Song /* If the type info kind_flag is set, the btf_member offset 127128b343dSYonghong Song * contains both member bitfield size and bit offset. The 128128b343dSYonghong Song * bitfield size is set for bitfield members. If the type 129128b343dSYonghong Song * info kind_flag is not set, the offset contains only bit 130128b343dSYonghong Song * offset. 131128b343dSYonghong Song */ 132128b343dSYonghong Song __u32 offset; 1333bd86a84SMartin KaFai Lau }; 1343bd86a84SMartin KaFai Lau 135128b343dSYonghong Song /* If the struct/union type info kind_flag is set, the 136128b343dSYonghong Song * following two macros are used to access bitfield_size 137128b343dSYonghong Song * and bit_offset from btf_member.offset. 138128b343dSYonghong Song */ 139128b343dSYonghong Song #define BTF_MEMBER_BITFIELD_SIZE(val) ((val) >> 24) 140128b343dSYonghong Song #define BTF_MEMBER_BIT_OFFSET(val) ((val) & 0xffffff) 141128b343dSYonghong Song 142781e775eSMartin KaFai Lau /* BTF_KIND_FUNC_PROTO is followed by multiple "struct btf_param". 143781e775eSMartin KaFai Lau * The exact number of btf_param is stored in the vlen (of the 144781e775eSMartin KaFai Lau * info in "struct btf_type"). 145781e775eSMartin KaFai Lau */ 146781e775eSMartin KaFai Lau struct btf_param { 147781e775eSMartin KaFai Lau __u32 name_off; 148781e775eSMartin KaFai Lau __u32 type; 149781e775eSMartin KaFai Lau }; 150781e775eSMartin KaFai Lau 151c83fef6bSDaniel Borkmann enum { 152c83fef6bSDaniel Borkmann BTF_VAR_STATIC = 0, 153166750bcSAndrii Nakryiko BTF_VAR_GLOBAL_ALLOCATED = 1, 154166750bcSAndrii Nakryiko BTF_VAR_GLOBAL_EXTERN = 2, 155c83fef6bSDaniel Borkmann }; 156c83fef6bSDaniel Borkmann 1572d3eb67fSAlexei Starovoitov enum btf_func_linkage { 1582d3eb67fSAlexei Starovoitov BTF_FUNC_STATIC = 0, 1592d3eb67fSAlexei Starovoitov BTF_FUNC_GLOBAL = 1, 1602d3eb67fSAlexei Starovoitov BTF_FUNC_EXTERN = 2, 1612d3eb67fSAlexei Starovoitov }; 1622d3eb67fSAlexei Starovoitov 163c83fef6bSDaniel Borkmann /* BTF_KIND_VAR is followed by a single "struct btf_var" to describe 164c83fef6bSDaniel Borkmann * additional information related to the variable such as its linkage. 165c83fef6bSDaniel Borkmann */ 166c83fef6bSDaniel Borkmann struct btf_var { 167c83fef6bSDaniel Borkmann __u32 linkage; 168c83fef6bSDaniel Borkmann }; 169c83fef6bSDaniel Borkmann 170c83fef6bSDaniel Borkmann /* BTF_KIND_DATASEC is followed by multiple "struct btf_var_secinfo" 171c83fef6bSDaniel Borkmann * to describe all BTF_KIND_VAR types it contains along with it's 172c83fef6bSDaniel Borkmann * in-section offset as well as size. 173c83fef6bSDaniel Borkmann */ 174c83fef6bSDaniel Borkmann struct btf_var_secinfo { 175c83fef6bSDaniel Borkmann __u32 type; 176c83fef6bSDaniel Borkmann __u32 offset; 177c83fef6bSDaniel Borkmann __u32 size; 178c83fef6bSDaniel Borkmann }; 179c83fef6bSDaniel 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 2013bd86a84SMartin KaFai Lau #endif /* _UAPI__LINUX_BTF_H__ */ 202