2018-04-19 06:55:58 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
/* Copyright (c) 2018 Facebook */
|
|
|
|
|
|
|
|
#ifndef _LINUX_BTF_H
|
|
|
|
#define _LINUX_BTF_H 1
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
2024-06-11 20:26:44 +08:00
|
|
|
#include <uapi/linux/btf.h>
|
|
|
|
|
|
|
|
#define BTF_TYPE_EMIT(type) ((void)(type *)0)
|
2018-04-19 06:55:58 +08:00
|
|
|
|
|
|
|
struct btf;
|
2018-12-16 14:13:52 +08:00
|
|
|
struct btf_member;
|
2018-04-19 06:55:58 +08:00
|
|
|
struct btf_type;
|
2018-04-19 06:56:01 +08:00
|
|
|
union bpf_attr;
|
2018-04-19 06:55:58 +08:00
|
|
|
|
2018-04-19 06:56:02 +08:00
|
|
|
extern const struct file_operations btf_fops;
|
|
|
|
|
2024-06-11 20:26:44 +08:00
|
|
|
void btf_get(struct btf *btf);
|
2018-04-19 06:56:01 +08:00
|
|
|
void btf_put(struct btf *btf);
|
|
|
|
int btf_new_fd(const union bpf_attr *attr);
|
|
|
|
struct btf *btf_get_by_fd(int fd);
|
2018-04-19 06:56:02 +08:00
|
|
|
int btf_get_info_by_fd(const struct btf *btf,
|
|
|
|
const union bpf_attr *attr,
|
|
|
|
union bpf_attr __user *uattr);
|
2018-04-19 06:55:58 +08:00
|
|
|
/* Figure out the size of a type_id. If type_id is a modifier
|
|
|
|
* (e.g. const), it will be resolved to find out the type with size.
|
|
|
|
*
|
|
|
|
* For example:
|
|
|
|
* In describing "const void *", type_id is "const" and "const"
|
|
|
|
* refers to "void *". The return type will be "void *".
|
|
|
|
*
|
|
|
|
* If type_id is a simple "int", then return type will be "int".
|
|
|
|
*
|
|
|
|
* @btf: struct btf object
|
|
|
|
* @type_id: Find out the size of type_id. The type_id of the return
|
|
|
|
* type is set to *type_id.
|
|
|
|
* @ret_size: It can be NULL. If not NULL, the size of the return
|
|
|
|
* type is set to *ret_size.
|
|
|
|
* Return: The btf_type (resolved to another type with size info if needed).
|
|
|
|
* NULL is returned if type_id itself does not have size info
|
|
|
|
* (e.g. void) or it cannot be resolved to another type that
|
|
|
|
* has size info.
|
|
|
|
* *type_id and *ret_size will not be changed in the
|
|
|
|
* NULL return case.
|
|
|
|
*/
|
|
|
|
const struct btf_type *btf_type_id_size(const struct btf *btf,
|
|
|
|
u32 *type_id,
|
|
|
|
u32 *ret_size);
|
2018-04-19 06:56:00 +08:00
|
|
|
void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
|
|
|
|
struct seq_file *m);
|
2018-05-05 05:49:51 +08:00
|
|
|
int btf_get_fd_by_id(u32 id);
|
|
|
|
u32 btf_id(const struct btf *btf);
|
2018-12-16 14:13:52 +08:00
|
|
|
bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
|
|
|
|
const struct btf_member *m,
|
|
|
|
u32 expected_offset, u32 expected_size);
|
2019-02-01 07:40:04 +08:00
|
|
|
int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
|
2024-06-11 20:26:44 +08:00
|
|
|
int btf_find_timer(const struct btf *btf, const struct btf_type *t);
|
2019-04-10 05:20:10 +08:00
|
|
|
bool btf_type_is_void(const struct btf_type *t);
|
2024-06-11 20:26:44 +08:00
|
|
|
s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
|
|
|
|
const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
|
|
|
|
u32 id, u32 *res_id);
|
|
|
|
const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
|
|
|
|
u32 id, u32 *res_id);
|
|
|
|
const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
|
|
|
|
u32 id, u32 *res_id);
|
|
|
|
const struct btf_type *
|
|
|
|
btf_resolve_size(const struct btf *btf, const struct btf_type *type,
|
|
|
|
u32 *type_size, const struct btf_type **elem_type,
|
|
|
|
u32 *total_nelems);
|
|
|
|
|
|
|
|
#define for_each_member(i, struct_type, member) \
|
|
|
|
for (i = 0, member = btf_type_member(struct_type); \
|
|
|
|
i < btf_type_vlen(struct_type); \
|
|
|
|
i++, member++)
|
|
|
|
|
|
|
|
static inline bool btf_type_is_ptr(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_type_is_int(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_type_is_enum(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_type_is_typedef(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_type_is_func(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_type_is_func_proto(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_type_is_var(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* union is only a special case of struct:
|
|
|
|
* all its offsetof(member) == 0
|
|
|
|
*/
|
|
|
|
static inline bool btf_type_is_struct(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
u8 kind = BTF_INFO_KIND(t->info);
|
|
|
|
|
|
|
|
return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u16 btf_type_vlen(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return BTF_INFO_VLEN(t->info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u16 btf_func_linkage(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return BTF_INFO_VLEN(t->info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_type_kflag(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return BTF_INFO_KFLAG(t->info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 btf_member_bit_offset(const struct btf_type *struct_type,
|
|
|
|
const struct btf_member *member)
|
|
|
|
{
|
|
|
|
return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
|
|
|
|
: member->offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 btf_member_bitfield_size(const struct btf_type *struct_type,
|
|
|
|
const struct btf_member *member)
|
|
|
|
{
|
|
|
|
return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
|
|
|
|
: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline const struct btf_member *btf_type_member(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return (const struct btf_member *)(t + 1);
|
|
|
|
}
|
2018-11-21 06:08:20 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_BPF_SYSCALL
|
bpf: Introduce bpf_func_info
This patch added interface to load a program with the following
additional information:
. prog_btf_fd
. func_info, func_info_rec_size and func_info_cnt
where func_info will provide function range and type_id
corresponding to each function.
The func_info_rec_size is introduced in the UAPI to specify
struct bpf_func_info size passed from user space. This
intends to make bpf_func_info structure growable in the future.
If the kernel gets a different bpf_func_info size from userspace,
it will try to handle user request with part of bpf_func_info
it can understand. In this patch, kernel can understand
struct bpf_func_info {
__u32 insn_offset;
__u32 type_id;
};
If user passed a bpf func_info record size of 16 bytes, the
kernel can still handle part of records with the above definition.
If verifier agrees with function range provided by the user,
the bpf_prog ksym for each function will use the func name
provided in the type_id, which is supposed to provide better
encoding as it is not limited by 16 bytes program name
limitation and this is better for bpf program which contains
multiple subprograms.
The bpf_prog_info interface is also extended to
return btf_id, func_info, func_info_rec_size and func_info_cnt
to userspace, so userspace can print out the function prototype
for each xlated function. The insn_offset in the returned
func_info corresponds to the insn offset for xlated functions.
With other jit related fields in bpf_prog_info, userspace can also
print out function prototypes for each jited function.
Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-11-20 07:29:11 +08:00
|
|
|
const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
|
|
|
|
const char *btf_name_by_offset(const struct btf *btf, u32 offset);
|
2024-06-11 20:26:44 +08:00
|
|
|
struct btf *btf_parse_vmlinux(void);
|
|
|
|
struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
|
2018-11-21 06:08:20 +08:00
|
|
|
#else
|
|
|
|
static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
|
|
|
|
u32 type_id)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
static inline const char *btf_name_by_offset(const struct btf *btf,
|
|
|
|
u32 offset)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2018-04-19 06:55:58 +08:00
|
|
|
|
|
|
|
#endif
|