162306a36Sopenharmony_ci/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
262306a36Sopenharmony_ci/* Copyright (c) 2021 Facebook */
362306a36Sopenharmony_ci#ifndef __BPF_GEN_INTERNAL_H
462306a36Sopenharmony_ci#define __BPF_GEN_INTERNAL_H
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include "bpf.h"
762306a36Sopenharmony_ci
862306a36Sopenharmony_cistruct ksym_relo_desc {
962306a36Sopenharmony_ci	const char *name;
1062306a36Sopenharmony_ci	int kind;
1162306a36Sopenharmony_ci	int insn_idx;
1262306a36Sopenharmony_ci	bool is_weak;
1362306a36Sopenharmony_ci	bool is_typeless;
1462306a36Sopenharmony_ci	bool is_ld64;
1562306a36Sopenharmony_ci};
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_cistruct ksym_desc {
1862306a36Sopenharmony_ci	const char *name;
1962306a36Sopenharmony_ci	int ref;
2062306a36Sopenharmony_ci	int kind;
2162306a36Sopenharmony_ci	union {
2262306a36Sopenharmony_ci		/* used for kfunc */
2362306a36Sopenharmony_ci		int off;
2462306a36Sopenharmony_ci		/* used for typeless ksym */
2562306a36Sopenharmony_ci		bool typeless;
2662306a36Sopenharmony_ci	};
2762306a36Sopenharmony_ci	int insn;
2862306a36Sopenharmony_ci	bool is_ld64;
2962306a36Sopenharmony_ci};
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_cistruct bpf_gen {
3262306a36Sopenharmony_ci	struct gen_loader_opts *opts;
3362306a36Sopenharmony_ci	void *data_start;
3462306a36Sopenharmony_ci	void *data_cur;
3562306a36Sopenharmony_ci	void *insn_start;
3662306a36Sopenharmony_ci	void *insn_cur;
3762306a36Sopenharmony_ci	ssize_t cleanup_label;
3862306a36Sopenharmony_ci	__u32 nr_progs;
3962306a36Sopenharmony_ci	__u32 nr_maps;
4062306a36Sopenharmony_ci	int log_level;
4162306a36Sopenharmony_ci	int error;
4262306a36Sopenharmony_ci	struct ksym_relo_desc *relos;
4362306a36Sopenharmony_ci	int relo_cnt;
4462306a36Sopenharmony_ci	struct bpf_core_relo *core_relos;
4562306a36Sopenharmony_ci	int core_relo_cnt;
4662306a36Sopenharmony_ci	char attach_target[128];
4762306a36Sopenharmony_ci	int attach_kind;
4862306a36Sopenharmony_ci	struct ksym_desc *ksyms;
4962306a36Sopenharmony_ci	__u32 nr_ksyms;
5062306a36Sopenharmony_ci	int fd_array;
5162306a36Sopenharmony_ci	int nr_fd_array;
5262306a36Sopenharmony_ci};
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_civoid bpf_gen__init(struct bpf_gen *gen, int log_level, int nr_progs, int nr_maps);
5562306a36Sopenharmony_ciint bpf_gen__finish(struct bpf_gen *gen, int nr_progs, int nr_maps);
5662306a36Sopenharmony_civoid bpf_gen__free(struct bpf_gen *gen);
5762306a36Sopenharmony_civoid bpf_gen__load_btf(struct bpf_gen *gen, const void *raw_data, __u32 raw_size);
5862306a36Sopenharmony_civoid bpf_gen__map_create(struct bpf_gen *gen,
5962306a36Sopenharmony_ci			 enum bpf_map_type map_type, const char *map_name,
6062306a36Sopenharmony_ci			 __u32 key_size, __u32 value_size, __u32 max_entries,
6162306a36Sopenharmony_ci			 struct bpf_map_create_opts *map_attr, int map_idx);
6262306a36Sopenharmony_civoid bpf_gen__prog_load(struct bpf_gen *gen,
6362306a36Sopenharmony_ci			enum bpf_prog_type prog_type, const char *prog_name,
6462306a36Sopenharmony_ci			const char *license, struct bpf_insn *insns, size_t insn_cnt,
6562306a36Sopenharmony_ci			struct bpf_prog_load_opts *load_attr, int prog_idx);
6662306a36Sopenharmony_civoid bpf_gen__map_update_elem(struct bpf_gen *gen, int map_idx, void *value, __u32 value_size);
6762306a36Sopenharmony_civoid bpf_gen__map_freeze(struct bpf_gen *gen, int map_idx);
6862306a36Sopenharmony_civoid bpf_gen__record_attach_target(struct bpf_gen *gen, const char *name, enum bpf_attach_type type);
6962306a36Sopenharmony_civoid bpf_gen__record_extern(struct bpf_gen *gen, const char *name, bool is_weak,
7062306a36Sopenharmony_ci			    bool is_typeless, bool is_ld64, int kind, int insn_idx);
7162306a36Sopenharmony_civoid bpf_gen__record_relo_core(struct bpf_gen *gen, const struct bpf_core_relo *core_relo);
7262306a36Sopenharmony_civoid bpf_gen__populate_outer_map(struct bpf_gen *gen, int outer_map_idx, int key, int inner_map_idx);
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci#endif
75