]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - include/linux/btf.h
selftests/bpf: Ksyms_btf to test typed ksyms
[thirdparty/kernel/linux.git] / include / linux / btf.h
CommitLineData
eb3f595d
MKL
1/* SPDX-License-Identifier: GPL-2.0 */
2/* Copyright (c) 2018 Facebook */
3
4#ifndef _LINUX_BTF_H
5#define _LINUX_BTF_H 1
6
7#include <linux/types.h>
38207291 8#include <uapi/linux/btf.h>
c4d0bfb4 9#include <uapi/linux/bpf.h>
eb3f595d 10
85d33df3
MKL
11#define BTF_TYPE_EMIT(type) ((void)(type *)0)
12
eb3f595d 13struct btf;
ffa0c1cf 14struct btf_member;
eb3f595d 15struct btf_type;
f56a653c 16union bpf_attr;
31d0bc81 17struct btf_show;
eb3f595d 18
60197cfb
MKL
19extern const struct file_operations btf_fops;
20
f56a653c
MKL
21void btf_put(struct btf *btf);
22int btf_new_fd(const union bpf_attr *attr);
23struct btf *btf_get_by_fd(int fd);
60197cfb
MKL
24int btf_get_info_by_fd(const struct btf *btf,
25 const union bpf_attr *attr,
26 union bpf_attr __user *uattr);
eb3f595d
MKL
27/* Figure out the size of a type_id. If type_id is a modifier
28 * (e.g. const), it will be resolved to find out the type with size.
29 *
30 * For example:
31 * In describing "const void *", type_id is "const" and "const"
32 * refers to "void *". The return type will be "void *".
33 *
34 * If type_id is a simple "int", then return type will be "int".
35 *
36 * @btf: struct btf object
37 * @type_id: Find out the size of type_id. The type_id of the return
38 * type is set to *type_id.
39 * @ret_size: It can be NULL. If not NULL, the size of the return
40 * type is set to *ret_size.
41 * Return: The btf_type (resolved to another type with size info if needed).
42 * NULL is returned if type_id itself does not have size info
43 * (e.g. void) or it cannot be resolved to another type that
44 * has size info.
45 * *type_id and *ret_size will not be changed in the
46 * NULL return case.
47 */
48const struct btf_type *btf_type_id_size(const struct btf *btf,
49 u32 *type_id,
50 u32 *ret_size);
31d0bc81
AM
51
52/*
53 * Options to control show behaviour.
54 * - BTF_SHOW_COMPACT: no formatting around type information
55 * - BTF_SHOW_NONAME: no struct/union member names/types
56 * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values;
57 * equivalent to %px.
58 * - BTF_SHOW_ZERO: show zero-valued struct/union members; they
59 * are not displayed by default
60 * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read
61 * data before displaying it.
62 */
c4d0bfb4
AM
63#define BTF_SHOW_COMPACT BTF_F_COMPACT
64#define BTF_SHOW_NONAME BTF_F_NONAME
65#define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW
66#define BTF_SHOW_ZERO BTF_F_ZERO
31d0bc81
AM
67#define BTF_SHOW_UNSAFE (1ULL << 4)
68
b00b8dae
MKL
69void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
70 struct seq_file *m);
eb411377
AM
71int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
72 struct seq_file *m, u64 flags);
31d0bc81
AM
73
74/*
75 * Copy len bytes of string representation of obj of BTF type_id into buf.
76 *
77 * @btf: struct btf object
78 * @type_id: type id of type obj points to
79 * @obj: pointer to typed data
80 * @buf: buffer to write to
81 * @len: maximum length to write to buf
82 * @flags: show options (see above)
83 *
84 * Return: length that would have been/was copied as per snprintf, or
85 * negative error.
86 */
87int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
88 char *buf, int len, u64 flags);
89
78958fca
MKL
90int btf_get_fd_by_id(u32 id);
91u32 btf_id(const struct btf *btf);
ffa0c1cf
YS
92bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
93 const struct btf_member *m,
94 u32 expected_offset, u32 expected_size);
d83525ca 95int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
2824ecb7 96bool btf_type_is_void(const struct btf_type *t);
27ae7997
MKL
97s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
98const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
99 u32 id, u32 *res_id);
100const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
101 u32 id, u32 *res_id);
102const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
103 u32 id, u32 *res_id);
85d33df3
MKL
104const struct btf_type *
105btf_resolve_size(const struct btf *btf, const struct btf_type *type,
6298399b 106 u32 *type_size);
27ae7997
MKL
107
108#define for_each_member(i, struct_type, member) \
109 for (i = 0, member = btf_type_member(struct_type); \
110 i < btf_type_vlen(struct_type); \
111 i++, member++)
f6161a8f 112
38207291
MKL
113static inline bool btf_type_is_ptr(const struct btf_type *t)
114{
115 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
116}
117
118static inline bool btf_type_is_int(const struct btf_type *t)
119{
120 return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
121}
122
a9b59159
JF
123static inline bool btf_type_is_small_int(const struct btf_type *t)
124{
125 return btf_type_is_int(t) && t->size <= sizeof(u64);
126}
127
38207291
MKL
128static inline bool btf_type_is_enum(const struct btf_type *t)
129{
130 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
131}
132
133static inline bool btf_type_is_typedef(const struct btf_type *t)
134{
135 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
136}
137
138static inline bool btf_type_is_func(const struct btf_type *t)
139{
140 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
141}
142
143static inline bool btf_type_is_func_proto(const struct btf_type *t)
144{
145 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
146}
147
4976b718
HL
148static inline bool btf_type_is_var(const struct btf_type *t)
149{
150 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
151}
152
153/* union is only a special case of struct:
154 * all its offsetof(member) == 0
155 */
156static inline bool btf_type_is_struct(const struct btf_type *t)
157{
158 u8 kind = BTF_INFO_KIND(t->info);
159
160 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
161}
162
27ae7997
MKL
163static inline u16 btf_type_vlen(const struct btf_type *t)
164{
165 return BTF_INFO_VLEN(t->info);
166}
167
be8704ff
AS
168static inline u16 btf_func_linkage(const struct btf_type *t)
169{
170 return BTF_INFO_VLEN(t->info);
171}
172
27ae7997
MKL
173static inline bool btf_type_kflag(const struct btf_type *t)
174{
175 return BTF_INFO_KFLAG(t->info);
176}
177
85d33df3
MKL
178static inline u32 btf_member_bit_offset(const struct btf_type *struct_type,
179 const struct btf_member *member)
180{
181 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
182 : member->offset;
183}
184
27ae7997
MKL
185static inline u32 btf_member_bitfield_size(const struct btf_type *struct_type,
186 const struct btf_member *member)
187{
188 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
189 : 0;
190}
191
192static inline const struct btf_member *btf_type_member(const struct btf_type *t)
193{
194 return (const struct btf_member *)(t + 1);
195}
196
f6161a8f 197#ifdef CONFIG_BPF_SYSCALL
838e9690
YS
198const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
199const char *btf_name_by_offset(const struct btf *btf, u32 offset);
8580ac94 200struct btf *btf_parse_vmlinux(void);
5b92a28a 201struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
f6161a8f
YS
202#else
203static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
204 u32 type_id)
205{
206 return NULL;
207}
208static inline const char *btf_name_by_offset(const struct btf *btf,
209 u32 offset)
210{
211 return NULL;
212}
213#endif
eb3f595d
MKL
214
215#endif