blob: 5c1ea99b480fa7164766454be9871b7d247d0d40 [file] [log] [blame]
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07001/* 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>
Martin KaFai Lau38207292019-10-24 17:18:11 -07008#include <uapi/linux/btf.h>
Martin KaFai Laueb3f5952018-04-18 15:55:58 -07009
Martin KaFai Lau85d33df2020-01-08 16:35:05 -080010#define BTF_TYPE_EMIT(type) ((void)(type *)0)
11
Martin KaFai Laueb3f5952018-04-18 15:55:58 -070012struct btf;
Yonghong Songffa0c1c2018-12-15 22:13:52 -080013struct btf_member;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -070014struct btf_type;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -070015union bpf_attr;
Martin KaFai Laueb3f5952018-04-18 15:55:58 -070016
Martin KaFai Lau60197cf2018-04-18 15:56:02 -070017extern const struct file_operations btf_fops;
18
Martin KaFai Lauf56a6532018-04-18 15:56:01 -070019void btf_put(struct btf *btf);
20int btf_new_fd(const union bpf_attr *attr);
21struct btf *btf_get_by_fd(int fd);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -070022int btf_get_info_by_fd(const struct btf *btf,
23 const union bpf_attr *attr,
24 union bpf_attr __user *uattr);
Martin KaFai Laueb3f5952018-04-18 15:55:58 -070025/* 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);
Martin KaFai Laub00b8da2018-04-18 15:56:00 -070049void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
50 struct seq_file *m);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -070051int btf_get_fd_by_id(u32 id);
52u32 btf_id(const struct btf *btf);
Yonghong Songffa0c1c2018-12-15 22:13:52 -080053bool 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);
Alexei Starovoitovd83525c2019-01-31 15:40:04 -080056int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
Daniel Borkmann2824ecb2019-04-09 23:20:10 +020057bool btf_type_is_void(const struct btf_type *t);
Martin KaFai Lau27ae79972020-01-08 16:35:03 -080058s32 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);
Martin KaFai Lau85d33df2020-01-08 16:35:05 -080065const struct btf_type *
66btf_resolve_size(const struct btf *btf, const struct btf_type *type,
67 u32 *type_size, const struct btf_type **elem_type,
68 u32 *total_nelems);
Martin KaFai Lau27ae79972020-01-08 16:35:03 -080069
70#define for_each_member(i, struct_type, member) \
71 for (i = 0, member = btf_type_member(struct_type); \
72 i < btf_type_vlen(struct_type); \
73 i++, member++)
Yonghong Songf6161a82018-11-20 14:08:20 -080074
Martin KaFai Lau38207292019-10-24 17:18:11 -070075static inline bool btf_type_is_ptr(const struct btf_type *t)
76{
77 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
78}
79
80static inline bool btf_type_is_int(const struct btf_type *t)
81{
82 return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
83}
84
85static inline bool btf_type_is_enum(const struct btf_type *t)
86{
87 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
88}
89
90static inline bool btf_type_is_typedef(const struct btf_type *t)
91{
92 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
93}
94
95static inline bool btf_type_is_func(const struct btf_type *t)
96{
97 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
98}
99
100static inline bool btf_type_is_func_proto(const struct btf_type *t)
101{
102 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
103}
104
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800105static inline u16 btf_type_vlen(const struct btf_type *t)
106{
107 return BTF_INFO_VLEN(t->info);
108}
109
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -0800110static inline u16 btf_func_linkage(const struct btf_type *t)
111{
112 return BTF_INFO_VLEN(t->info);
113}
114
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800115static inline bool btf_type_kflag(const struct btf_type *t)
116{
117 return BTF_INFO_KFLAG(t->info);
118}
119
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800120static inline u32 btf_member_bit_offset(const struct btf_type *struct_type,
121 const struct btf_member *member)
122{
123 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
124 : member->offset;
125}
126
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800127static inline u32 btf_member_bitfield_size(const struct btf_type *struct_type,
128 const struct btf_member *member)
129{
130 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
131 : 0;
132}
133
134static inline const struct btf_member *btf_type_member(const struct btf_type *t)
135{
136 return (const struct btf_member *)(t + 1);
137}
138
Yonghong Songf6161a82018-11-20 14:08:20 -0800139#ifdef CONFIG_BPF_SYSCALL
Yonghong Song838e9692018-11-19 15:29:11 -0800140const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
141const char *btf_name_by_offset(const struct btf *btf, u32 offset);
Alexei Starovoitov8580ac92019-10-15 20:24:57 -0700142struct btf *btf_parse_vmlinux(void);
Alexei Starovoitov5b92a282019-11-14 10:57:17 -0800143struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
Yonghong Songf6161a82018-11-20 14:08:20 -0800144#else
145static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
146 u32 type_id)
147{
148 return NULL;
149}
150static inline const char *btf_name_by_offset(const struct btf *btf,
151 u32 offset)
152{
153 return NULL;
154}
155#endif
Martin KaFai Laueb3f5952018-04-18 15:55:58 -0700156
157#endif