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 36aea2f7b8SMartin KaFai Lau * bits 24-27: kind (e.g. int, ptr, array...etc) 379d5f9f70SYonghong Song * bits 28-30: unused 389d5f9f70SYonghong Song * bit 31: kind_flag, currently used by 399d5f9f70SYonghong Song * struct, union and fwd 4069b693f0SMartin KaFai Lau */ 4169b693f0SMartin KaFai Lau __u32 info; 42f063c889SDaniel Borkmann /* "size" is used by INT, ENUM, STRUCT, UNION and DATASEC. 4369b693f0SMartin KaFai Lau * "size" tells the size of the type it is describing. 4469b693f0SMartin KaFai Lau * 452667a262SMartin KaFai Lau * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, 46f063c889SDaniel Borkmann * FUNC, FUNC_PROTO and VAR. 4769b693f0SMartin KaFai Lau * "type" is a type_id referring to another type. 4869b693f0SMartin KaFai Lau */ 4969b693f0SMartin KaFai Lau union { 5069b693f0SMartin KaFai Lau __u32 size; 5169b693f0SMartin KaFai Lau __u32 type; 5269b693f0SMartin KaFai Lau }; 5369b693f0SMartin KaFai Lau }; 5469b693f0SMartin KaFai Lau 55aea2f7b8SMartin KaFai Lau #define BTF_INFO_KIND(info) (((info) >> 24) & 0x0f) 5669b693f0SMartin KaFai Lau #define BTF_INFO_VLEN(info) ((info) & 0xffff) 579d5f9f70SYonghong Song #define BTF_INFO_KFLAG(info) ((info) >> 31) 5869b693f0SMartin KaFai Lau 5969b693f0SMartin KaFai Lau #define BTF_KIND_UNKN 0 /* Unknown */ 6069b693f0SMartin KaFai Lau #define BTF_KIND_INT 1 /* Integer */ 6169b693f0SMartin KaFai Lau #define BTF_KIND_PTR 2 /* Pointer */ 6269b693f0SMartin KaFai Lau #define BTF_KIND_ARRAY 3 /* Array */ 6369b693f0SMartin KaFai Lau #define BTF_KIND_STRUCT 4 /* Struct */ 6469b693f0SMartin KaFai Lau #define BTF_KIND_UNION 5 /* Union */ 6569b693f0SMartin KaFai Lau #define BTF_KIND_ENUM 6 /* Enumeration */ 6669b693f0SMartin KaFai Lau #define BTF_KIND_FWD 7 /* Forward */ 6769b693f0SMartin KaFai Lau #define BTF_KIND_TYPEDEF 8 /* Typedef */ 6869b693f0SMartin KaFai Lau #define BTF_KIND_VOLATILE 9 /* Volatile */ 6969b693f0SMartin KaFai Lau #define BTF_KIND_CONST 10 /* Const */ 7069b693f0SMartin KaFai Lau #define BTF_KIND_RESTRICT 11 /* Restrict */ 712667a262SMartin KaFai Lau #define BTF_KIND_FUNC 12 /* Function */ 722667a262SMartin KaFai Lau #define BTF_KIND_FUNC_PROTO 13 /* Function Proto */ 73f063c889SDaniel Borkmann #define BTF_KIND_VAR 14 /* Variable */ 74f063c889SDaniel Borkmann #define BTF_KIND_DATASEC 15 /* Section */ 75f063c889SDaniel Borkmann #define BTF_KIND_MAX BTF_KIND_DATASEC 76f063c889SDaniel Borkmann #define NR_BTF_KINDS (BTF_KIND_MAX + 1) 7769b693f0SMartin KaFai Lau 7869b693f0SMartin KaFai Lau /* For some specific BTF_KIND, "struct btf_type" is immediately 7969b693f0SMartin KaFai Lau * followed by extra data. 8069b693f0SMartin KaFai Lau */ 8169b693f0SMartin KaFai Lau 8269b693f0SMartin KaFai Lau /* BTF_KIND_INT is followed by a u32 and the following 8369b693f0SMartin KaFai Lau * is the 32 bits arrangement: 8469b693f0SMartin KaFai Lau */ 85aea2f7b8SMartin KaFai Lau #define BTF_INT_ENCODING(VAL) (((VAL) & 0x0f000000) >> 24) 86948dc8c9SGary Lin #define BTF_INT_OFFSET(VAL) (((VAL) & 0x00ff0000) >> 16) 8736fc3c8cSMartin KaFai Lau #define BTF_INT_BITS(VAL) ((VAL) & 0x000000ff) 8869b693f0SMartin KaFai Lau 8969b693f0SMartin KaFai Lau /* Attributes stored in the BTF_INT_ENCODING */ 90aea2f7b8SMartin KaFai Lau #define BTF_INT_SIGNED (1 << 0) 91aea2f7b8SMartin KaFai Lau #define BTF_INT_CHAR (1 << 1) 92aea2f7b8SMartin KaFai Lau #define BTF_INT_BOOL (1 << 2) 9369b693f0SMartin KaFai Lau 9469b693f0SMartin KaFai Lau /* BTF_KIND_ENUM is followed by multiple "struct btf_enum". 9569b693f0SMartin KaFai Lau * The exact number of btf_enum is stored in the vlen (of the 9669b693f0SMartin KaFai Lau * info in "struct btf_type"). 9769b693f0SMartin KaFai Lau */ 9869b693f0SMartin KaFai Lau struct btf_enum { 99fbcf93ebSMartin KaFai Lau __u32 name_off; 10069b693f0SMartin KaFai Lau __s32 val; 10169b693f0SMartin KaFai Lau }; 10269b693f0SMartin KaFai Lau 10369b693f0SMartin KaFai Lau /* BTF_KIND_ARRAY is followed by one "struct btf_array" */ 10469b693f0SMartin KaFai Lau struct btf_array { 10569b693f0SMartin KaFai Lau __u32 type; 10669b693f0SMartin KaFai Lau __u32 index_type; 10769b693f0SMartin KaFai Lau __u32 nelems; 10869b693f0SMartin KaFai Lau }; 10969b693f0SMartin KaFai Lau 11069b693f0SMartin KaFai Lau /* BTF_KIND_STRUCT and BTF_KIND_UNION are followed 11169b693f0SMartin KaFai Lau * by multiple "struct btf_member". The exact number 11269b693f0SMartin KaFai Lau * of btf_member is stored in the vlen (of the info in 11369b693f0SMartin KaFai Lau * "struct btf_type"). 11469b693f0SMartin KaFai Lau */ 11569b693f0SMartin KaFai Lau struct btf_member { 116fbcf93ebSMartin KaFai Lau __u32 name_off; 11769b693f0SMartin KaFai Lau __u32 type; 1189d5f9f70SYonghong Song /* If the type info kind_flag is set, the btf_member offset 1199d5f9f70SYonghong Song * contains both member bitfield size and bit offset. The 1209d5f9f70SYonghong Song * bitfield size is set for bitfield members. If the type 1219d5f9f70SYonghong Song * info kind_flag is not set, the offset contains only bit 1229d5f9f70SYonghong Song * offset. 1239d5f9f70SYonghong Song */ 1249d5f9f70SYonghong Song __u32 offset; 12569b693f0SMartin KaFai Lau }; 12669b693f0SMartin KaFai Lau 1279d5f9f70SYonghong Song /* If the struct/union type info kind_flag is set, the 1289d5f9f70SYonghong Song * following two macros are used to access bitfield_size 1299d5f9f70SYonghong Song * and bit_offset from btf_member.offset. 1309d5f9f70SYonghong Song */ 1319d5f9f70SYonghong Song #define BTF_MEMBER_BITFIELD_SIZE(val) ((val) >> 24) 1329d5f9f70SYonghong Song #define BTF_MEMBER_BIT_OFFSET(val) ((val) & 0xffffff) 1339d5f9f70SYonghong Song 1342667a262SMartin KaFai Lau /* BTF_KIND_FUNC_PROTO is followed by multiple "struct btf_param". 1352667a262SMartin KaFai Lau * The exact number of btf_param is stored in the vlen (of the 1362667a262SMartin KaFai Lau * info in "struct btf_type"). 1372667a262SMartin KaFai Lau */ 1382667a262SMartin KaFai Lau struct btf_param { 1392667a262SMartin KaFai Lau __u32 name_off; 1402667a262SMartin KaFai Lau __u32 type; 1412667a262SMartin KaFai Lau }; 1422667a262SMartin KaFai Lau 143f063c889SDaniel Borkmann enum { 144f063c889SDaniel Borkmann BTF_VAR_STATIC = 0, 145*166750bcSAndrii Nakryiko BTF_VAR_GLOBAL_ALLOCATED = 1, 146*166750bcSAndrii Nakryiko BTF_VAR_GLOBAL_EXTERN = 2, 147f063c889SDaniel Borkmann }; 148f063c889SDaniel Borkmann 149f063c889SDaniel Borkmann /* BTF_KIND_VAR is followed by a single "struct btf_var" to describe 150f063c889SDaniel Borkmann * additional information related to the variable such as its linkage. 151f063c889SDaniel Borkmann */ 152f063c889SDaniel Borkmann struct btf_var { 153f063c889SDaniel Borkmann __u32 linkage; 154f063c889SDaniel Borkmann }; 155f063c889SDaniel Borkmann 156f063c889SDaniel Borkmann /* BTF_KIND_DATASEC is followed by multiple "struct btf_var_secinfo" 157f063c889SDaniel Borkmann * to describe all BTF_KIND_VAR types it contains along with it's 158f063c889SDaniel Borkmann * in-section offset as well as size. 159f063c889SDaniel Borkmann */ 160f063c889SDaniel Borkmann struct btf_var_secinfo { 161f063c889SDaniel Borkmann __u32 type; 162f063c889SDaniel Borkmann __u32 offset; 163f063c889SDaniel Borkmann __u32 size; 164f063c889SDaniel Borkmann }; 165f063c889SDaniel Borkmann 16669b693f0SMartin KaFai Lau #endif /* _UAPI__LINUX_BTF_H__ */ 167