]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - include/linux/btf.h
bpf: Provide function to get vmlinux BTF information
[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>
eb3f595d 9
85d33df3
MKL
10#define BTF_TYPE_EMIT(type) ((void)(type *)0)
11
eb3f595d 12struct btf;
ffa0c1cf 13struct btf_member;
eb3f595d 14struct btf_type;
f56a653c 15union bpf_attr;
eb3f595d 16
60197cfb
MKL
17extern const struct file_operations btf_fops;
18
f56a653c
MKL
19void btf_put(struct btf *btf);
20int btf_new_fd(const union bpf_attr *attr);
21struct btf *btf_get_by_fd(int fd);
60197cfb
MKL
22int btf_get_info_by_fd(const struct btf *btf,
23 const union bpf_attr *attr,
24 union bpf_attr __user *uattr);
eb3f595d
MKL
25/* Figure out the size of a type_id. If type_id is a modifier
26 * (e.g. const), it will be resolved to find out the type with size.
27 *
28 * For example:
29 * In describing "const void *", type_id is "const" and "const"
30 * refers to "void *". The return type will be "void *".
31 *
32 * If type_id is a simple "int", then return type will be "int".
33 *
34 * @btf: struct btf object
35 * @type_id: Find out the size of type_id. The type_id of the return
36 * type is set to *type_id.
37 * @ret_size: It can be NULL. If not NULL, the size of the return
38 * type is set to *ret_size.
39 * Return: The btf_type (resolved to another type with size info if needed).
40 * NULL is returned if type_id itself does not have size info
41 * (e.g. void) or it cannot be resolved to another type that
42 * has size info.
43 * *type_id and *ret_size will not be changed in the
44 * NULL return case.
45 */
46const struct btf_type *btf_type_id_size(const struct btf *btf,
47 u32 *type_id,
48 u32 *ret_size);
b00b8dae
MKL
49void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
50 struct seq_file *m);
78958fca
MKL
51int btf_get_fd_by_id(u32 id);
52u32 btf_id(const struct btf *btf);
ffa0c1cf
YS
53bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
54 const struct btf_member *m,
55 u32 expected_offset, u32 expected_size);
d83525ca 56int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
2824ecb7 57bool btf_type_is_void(const struct btf_type *t);
27ae7997
MKL
58s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
59const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
60 u32 id, u32 *res_id);
61const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
62 u32 id, u32 *res_id);
63const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
64 u32 id, u32 *res_id);
85d33df3
MKL
65const struct btf_type *
66btf_resolve_size(const struct btf *btf, const struct btf_type *type,
6298399b 67 u32 *type_size);
27ae7997
MKL
68
69#define for_each_member(i, struct_type, member) \
70 for (i = 0, member = btf_type_member(struct_type); \
71 i < btf_type_vlen(struct_type); \
72 i++, member++)
f6161a8f 73
38207291
MKL
74static inline bool btf_type_is_ptr(const struct btf_type *t)
75{
76 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
77}
78
79static inline bool btf_type_is_int(const struct btf_type *t)
80{
81 return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
82}
83
a9b59159
JF
84static inline bool btf_type_is_small_int(const struct btf_type *t)
85{
86 return btf_type_is_int(t) && t->size <= sizeof(u64);
87}
88
38207291
MKL
89static inline bool btf_type_is_enum(const struct btf_type *t)
90{
91 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
92}
93
94static inline bool btf_type_is_typedef(const struct btf_type *t)
95{
96 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
97}
98
99static inline bool btf_type_is_func(const struct btf_type *t)
100{
101 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
102}
103
104static inline bool btf_type_is_func_proto(const struct btf_type *t)
105{
106 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
107}
108
27ae7997
MKL
109static inline u16 btf_type_vlen(const struct btf_type *t)
110{
111 return BTF_INFO_VLEN(t->info);
112}
113
be8704ff
AS
114static inline u16 btf_func_linkage(const struct btf_type *t)
115{
116 return BTF_INFO_VLEN(t->info);
117}
118
27ae7997
MKL
119static inline bool btf_type_kflag(const struct btf_type *t)
120{
121 return BTF_INFO_KFLAG(t->info);
122}
123
85d33df3
MKL
124static inline u32 btf_member_bit_offset(const struct btf_type *struct_type,
125 const struct btf_member *member)
126{
127 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
128 : member->offset;
129}
130
27ae7997
MKL
131static inline u32 btf_member_bitfield_size(const struct btf_type *struct_type,
132 const struct btf_member *member)
133{
134 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
135 : 0;
136}
137
138static inline const struct btf_member *btf_type_member(const struct btf_type *t)
139{
140 return (const struct btf_member *)(t + 1);
141}
142
f6161a8f 143#ifdef CONFIG_BPF_SYSCALL
838e9690
YS
144const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
145const char *btf_name_by_offset(const struct btf *btf, u32 offset);
8580ac94 146struct btf *btf_parse_vmlinux(void);
5b92a28a 147struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
f6161a8f
YS
148#else
149static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
150 u32 type_id)
151{
152 return NULL;
153}
154static inline const char *btf_name_by_offset(const struct btf *btf,
155 u32 offset)
156{
157 return NULL;
158}
159#endif
eb3f595d
MKL
160
161#endif