Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 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 |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 9 | #define BTF_VERSION 1 |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 10 | |
| 11 | struct btf_header { |
| 12 | __u16 magic; |
| 13 | __u8 version; |
| 14 | __u8 flags; |
Martin KaFai Lau | f80442a | 2018-05-22 14:57:18 -0700 | [diff] [blame] | 15 | __u32 hdr_len; |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 16 | |
| 17 | /* All offsets are in bytes relative to the end of this header */ |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 18 | __u32 type_off; /* offset of type section */ |
Martin KaFai Lau | f80442a | 2018-05-22 14:57:18 -0700 | [diff] [blame] | 19 | __u32 type_len; /* length of type section */ |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 20 | __u32 str_off; /* offset of string section */ |
| 21 | __u32 str_len; /* length of string section */ |
| 22 | }; |
| 23 | |
| 24 | /* Max # of type identifier */ |
Alexei Starovoitov | a0791f0 | 2019-09-17 10:45:38 -0700 | [diff] [blame] | 25 | #define BTF_MAX_TYPE 0x000fffff |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 26 | /* Max offset into the string section */ |
Alexei Starovoitov | a0791f0 | 2019-09-17 10:45:38 -0700 | [diff] [blame] | 27 | #define BTF_MAX_NAME_OFFSET 0x00ffffff |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 28 | /* Max # of struct/union/enum members or func args */ |
| 29 | #define BTF_MAX_VLEN 0xffff |
| 30 | |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 31 | struct btf_type { |
Martin KaFai Lau | fbcf93e | 2018-04-21 09:48:23 -0700 | [diff] [blame] | 32 | __u32 name_off; |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 33 | /* "info" bits arrangement |
| 34 | * bits 0-15: vlen (e.g. # of struct's members) |
| 35 | * bits 16-23: unused |
Martin KaFai Lau | aea2f7b8 | 2018-05-22 14:57:20 -0700 | [diff] [blame] | 36 | * bits 24-27: kind (e.g. int, ptr, array...etc) |
Yonghong Song | 9d5f9f7 | 2018-12-15 22:13:51 -0800 | [diff] [blame] | 37 | * bits 28-30: unused |
| 38 | * bit 31: kind_flag, currently used by |
| 39 | * struct, union and fwd |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 40 | */ |
| 41 | __u32 info; |
Daniel Borkmann | f063c88 | 2019-04-09 23:20:08 +0200 | [diff] [blame] | 42 | /* "size" is used by INT, ENUM, STRUCT, UNION and DATASEC. |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 43 | * "size" tells the size of the type it is describing. |
| 44 | * |
Martin KaFai Lau | 2667a26 | 2018-11-19 15:29:08 -0800 | [diff] [blame] | 45 | * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, |
Daniel Borkmann | f063c88 | 2019-04-09 23:20:08 +0200 | [diff] [blame] | 46 | * FUNC, FUNC_PROTO and VAR. |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 47 | * "type" is a type_id referring to another type. |
| 48 | */ |
| 49 | union { |
| 50 | __u32 size; |
| 51 | __u32 type; |
| 52 | }; |
| 53 | }; |
| 54 | |
Martin KaFai Lau | aea2f7b8 | 2018-05-22 14:57:20 -0700 | [diff] [blame] | 55 | #define BTF_INFO_KIND(info) (((info) >> 24) & 0x0f) |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 56 | #define BTF_INFO_VLEN(info) ((info) & 0xffff) |
Yonghong Song | 9d5f9f7 | 2018-12-15 22:13:51 -0800 | [diff] [blame] | 57 | #define BTF_INFO_KFLAG(info) ((info) >> 31) |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 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 */ |
Martin KaFai Lau | 2667a26 | 2018-11-19 15:29:08 -0800 | [diff] [blame] | 71 | #define BTF_KIND_FUNC 12 /* Function */ |
| 72 | #define BTF_KIND_FUNC_PROTO 13 /* Function Proto */ |
Daniel Borkmann | f063c88 | 2019-04-09 23:20:08 +0200 | [diff] [blame] | 73 | #define BTF_KIND_VAR 14 /* Variable */ |
| 74 | #define BTF_KIND_DATASEC 15 /* Section */ |
| 75 | #define BTF_KIND_MAX BTF_KIND_DATASEC |
| 76 | #define NR_BTF_KINDS (BTF_KIND_MAX + 1) |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 77 | |
| 78 | /* For some specific BTF_KIND, "struct btf_type" is immediately |
| 79 | * followed by extra data. |
| 80 | */ |
| 81 | |
| 82 | /* BTF_KIND_INT is followed by a u32 and the following |
| 83 | * is the 32 bits arrangement: |
| 84 | */ |
Martin KaFai Lau | aea2f7b8 | 2018-05-22 14:57:20 -0700 | [diff] [blame] | 85 | #define BTF_INT_ENCODING(VAL) (((VAL) & 0x0f000000) >> 24) |
Gary Lin | 948dc8c | 2019-05-13 17:45:48 +0800 | [diff] [blame] | 86 | #define BTF_INT_OFFSET(VAL) (((VAL) & 0x00ff0000) >> 16) |
Martin KaFai Lau | 36fc3c8 | 2018-07-19 22:14:31 -0700 | [diff] [blame] | 87 | #define BTF_INT_BITS(VAL) ((VAL) & 0x000000ff) |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 88 | |
| 89 | /* Attributes stored in the BTF_INT_ENCODING */ |
Martin KaFai Lau | aea2f7b8 | 2018-05-22 14:57:20 -0700 | [diff] [blame] | 90 | #define BTF_INT_SIGNED (1 << 0) |
| 91 | #define BTF_INT_CHAR (1 << 1) |
| 92 | #define BTF_INT_BOOL (1 << 2) |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 93 | |
| 94 | /* BTF_KIND_ENUM is followed by multiple "struct btf_enum". |
| 95 | * The exact number of btf_enum is stored in the vlen (of the |
| 96 | * info in "struct btf_type"). |
| 97 | */ |
| 98 | struct btf_enum { |
Martin KaFai Lau | fbcf93e | 2018-04-21 09:48:23 -0700 | [diff] [blame] | 99 | __u32 name_off; |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 100 | __s32 val; |
| 101 | }; |
| 102 | |
| 103 | /* BTF_KIND_ARRAY is followed by one "struct btf_array" */ |
| 104 | struct btf_array { |
| 105 | __u32 type; |
| 106 | __u32 index_type; |
| 107 | __u32 nelems; |
| 108 | }; |
| 109 | |
| 110 | /* BTF_KIND_STRUCT and BTF_KIND_UNION are followed |
| 111 | * by multiple "struct btf_member". The exact number |
| 112 | * of btf_member is stored in the vlen (of the info in |
| 113 | * "struct btf_type"). |
| 114 | */ |
| 115 | struct btf_member { |
Martin KaFai Lau | fbcf93e | 2018-04-21 09:48:23 -0700 | [diff] [blame] | 116 | __u32 name_off; |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 117 | __u32 type; |
Yonghong Song | 9d5f9f7 | 2018-12-15 22:13:51 -0800 | [diff] [blame] | 118 | /* If the type info kind_flag is set, the btf_member offset |
| 119 | * contains both member bitfield size and bit offset. The |
| 120 | * bitfield size is set for bitfield members. If the type |
| 121 | * info kind_flag is not set, the offset contains only bit |
| 122 | * offset. |
| 123 | */ |
| 124 | __u32 offset; |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
Yonghong Song | 9d5f9f7 | 2018-12-15 22:13:51 -0800 | [diff] [blame] | 127 | /* If the struct/union type info kind_flag is set, the |
| 128 | * following two macros are used to access bitfield_size |
| 129 | * and bit_offset from btf_member.offset. |
| 130 | */ |
| 131 | #define BTF_MEMBER_BITFIELD_SIZE(val) ((val) >> 24) |
| 132 | #define BTF_MEMBER_BIT_OFFSET(val) ((val) & 0xffffff) |
| 133 | |
Martin KaFai Lau | 2667a26 | 2018-11-19 15:29:08 -0800 | [diff] [blame] | 134 | /* BTF_KIND_FUNC_PROTO is followed by multiple "struct btf_param". |
| 135 | * The exact number of btf_param is stored in the vlen (of the |
| 136 | * info in "struct btf_type"). |
| 137 | */ |
| 138 | struct btf_param { |
| 139 | __u32 name_off; |
| 140 | __u32 type; |
| 141 | }; |
| 142 | |
Daniel Borkmann | f063c88 | 2019-04-09 23:20:08 +0200 | [diff] [blame] | 143 | enum { |
| 144 | BTF_VAR_STATIC = 0, |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 145 | BTF_VAR_GLOBAL_ALLOCATED = 1, |
| 146 | BTF_VAR_GLOBAL_EXTERN = 2, |
Daniel Borkmann | f063c88 | 2019-04-09 23:20:08 +0200 | [diff] [blame] | 147 | }; |
| 148 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 149 | enum btf_func_linkage { |
| 150 | BTF_FUNC_STATIC = 0, |
| 151 | BTF_FUNC_GLOBAL = 1, |
| 152 | BTF_FUNC_EXTERN = 2, |
| 153 | }; |
| 154 | |
Daniel Borkmann | f063c88 | 2019-04-09 23:20:08 +0200 | [diff] [blame] | 155 | /* BTF_KIND_VAR is followed by a single "struct btf_var" to describe |
| 156 | * additional information related to the variable such as its linkage. |
| 157 | */ |
| 158 | struct btf_var { |
| 159 | __u32 linkage; |
| 160 | }; |
| 161 | |
| 162 | /* BTF_KIND_DATASEC is followed by multiple "struct btf_var_secinfo" |
| 163 | * to describe all BTF_KIND_VAR types it contains along with it's |
| 164 | * in-section offset as well as size. |
| 165 | */ |
| 166 | struct btf_var_secinfo { |
| 167 | __u32 type; |
| 168 | __u32 offset; |
| 169 | __u32 size; |
| 170 | }; |
| 171 | |
Martin KaFai Lau | 69b693f | 2018-04-18 15:55:57 -0700 | [diff] [blame] | 172 | #endif /* _UAPI__LINUX_BTF_H__ */ |