1*f7c14bbaSAndroid Build Coastguard Worker // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2*f7c14bbaSAndroid Build Coastguard Worker /* Copyright (c) 2021 Facebook */
3*f7c14bbaSAndroid Build Coastguard Worker #include <stdio.h>
4*f7c14bbaSAndroid Build Coastguard Worker #include <stdlib.h>
5*f7c14bbaSAndroid Build Coastguard Worker #include <string.h>
6*f7c14bbaSAndroid Build Coastguard Worker #include <errno.h>
7*f7c14bbaSAndroid Build Coastguard Worker #include <linux/filter.h>
8*f7c14bbaSAndroid Build Coastguard Worker #include <sys/param.h>
9*f7c14bbaSAndroid Build Coastguard Worker #include "btf.h"
10*f7c14bbaSAndroid Build Coastguard Worker #include "bpf.h"
11*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf.h"
12*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf_internal.h"
13*f7c14bbaSAndroid Build Coastguard Worker #include "hashmap.h"
14*f7c14bbaSAndroid Build Coastguard Worker #include "bpf_gen_internal.h"
15*f7c14bbaSAndroid Build Coastguard Worker #include "skel_internal.h"
16*f7c14bbaSAndroid Build Coastguard Worker #include <asm/byteorder.h>
17*f7c14bbaSAndroid Build Coastguard Worker
18*f7c14bbaSAndroid Build Coastguard Worker #define MAX_USED_MAPS 64
19*f7c14bbaSAndroid Build Coastguard Worker #define MAX_USED_PROGS 32
20*f7c14bbaSAndroid Build Coastguard Worker #define MAX_KFUNC_DESCS 256
21*f7c14bbaSAndroid Build Coastguard Worker #define MAX_FD_ARRAY_SZ (MAX_USED_MAPS + MAX_KFUNC_DESCS)
22*f7c14bbaSAndroid Build Coastguard Worker
23*f7c14bbaSAndroid Build Coastguard Worker /* The following structure describes the stack layout of the loader program.
24*f7c14bbaSAndroid Build Coastguard Worker * In addition R6 contains the pointer to context.
25*f7c14bbaSAndroid Build Coastguard Worker * R7 contains the result of the last sys_bpf command (typically error or FD).
26*f7c14bbaSAndroid Build Coastguard Worker * R9 contains the result of the last sys_close command.
27*f7c14bbaSAndroid Build Coastguard Worker *
28*f7c14bbaSAndroid Build Coastguard Worker * Naming convention:
29*f7c14bbaSAndroid Build Coastguard Worker * ctx - bpf program context
30*f7c14bbaSAndroid Build Coastguard Worker * stack - bpf program stack
31*f7c14bbaSAndroid Build Coastguard Worker * blob - bpf_attr-s, strings, insns, map data.
32*f7c14bbaSAndroid Build Coastguard Worker * All the bytes that loader prog will use for read/write.
33*f7c14bbaSAndroid Build Coastguard Worker */
34*f7c14bbaSAndroid Build Coastguard Worker struct loader_stack {
35*f7c14bbaSAndroid Build Coastguard Worker __u32 btf_fd;
36*f7c14bbaSAndroid Build Coastguard Worker __u32 inner_map_fd;
37*f7c14bbaSAndroid Build Coastguard Worker __u32 prog_fd[MAX_USED_PROGS];
38*f7c14bbaSAndroid Build Coastguard Worker };
39*f7c14bbaSAndroid Build Coastguard Worker
40*f7c14bbaSAndroid Build Coastguard Worker #define stack_off(field) \
41*f7c14bbaSAndroid Build Coastguard Worker (__s16)(-sizeof(struct loader_stack) + offsetof(struct loader_stack, field))
42*f7c14bbaSAndroid Build Coastguard Worker
43*f7c14bbaSAndroid Build Coastguard Worker #define attr_field(attr, field) (attr + offsetof(union bpf_attr, field))
44*f7c14bbaSAndroid Build Coastguard Worker
blob_fd_array_off(struct bpf_gen * gen,int index)45*f7c14bbaSAndroid Build Coastguard Worker static int blob_fd_array_off(struct bpf_gen *gen, int index)
46*f7c14bbaSAndroid Build Coastguard Worker {
47*f7c14bbaSAndroid Build Coastguard Worker return gen->fd_array + index * sizeof(int);
48*f7c14bbaSAndroid Build Coastguard Worker }
49*f7c14bbaSAndroid Build Coastguard Worker
realloc_insn_buf(struct bpf_gen * gen,__u32 size)50*f7c14bbaSAndroid Build Coastguard Worker static int realloc_insn_buf(struct bpf_gen *gen, __u32 size)
51*f7c14bbaSAndroid Build Coastguard Worker {
52*f7c14bbaSAndroid Build Coastguard Worker size_t off = gen->insn_cur - gen->insn_start;
53*f7c14bbaSAndroid Build Coastguard Worker void *insn_start;
54*f7c14bbaSAndroid Build Coastguard Worker
55*f7c14bbaSAndroid Build Coastguard Worker if (gen->error)
56*f7c14bbaSAndroid Build Coastguard Worker return gen->error;
57*f7c14bbaSAndroid Build Coastguard Worker if (size > INT32_MAX || off + size > INT32_MAX) {
58*f7c14bbaSAndroid Build Coastguard Worker gen->error = -ERANGE;
59*f7c14bbaSAndroid Build Coastguard Worker return -ERANGE;
60*f7c14bbaSAndroid Build Coastguard Worker }
61*f7c14bbaSAndroid Build Coastguard Worker insn_start = realloc(gen->insn_start, off + size);
62*f7c14bbaSAndroid Build Coastguard Worker if (!insn_start) {
63*f7c14bbaSAndroid Build Coastguard Worker gen->error = -ENOMEM;
64*f7c14bbaSAndroid Build Coastguard Worker free(gen->insn_start);
65*f7c14bbaSAndroid Build Coastguard Worker gen->insn_start = NULL;
66*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
67*f7c14bbaSAndroid Build Coastguard Worker }
68*f7c14bbaSAndroid Build Coastguard Worker gen->insn_start = insn_start;
69*f7c14bbaSAndroid Build Coastguard Worker gen->insn_cur = insn_start + off;
70*f7c14bbaSAndroid Build Coastguard Worker return 0;
71*f7c14bbaSAndroid Build Coastguard Worker }
72*f7c14bbaSAndroid Build Coastguard Worker
realloc_data_buf(struct bpf_gen * gen,__u32 size)73*f7c14bbaSAndroid Build Coastguard Worker static int realloc_data_buf(struct bpf_gen *gen, __u32 size)
74*f7c14bbaSAndroid Build Coastguard Worker {
75*f7c14bbaSAndroid Build Coastguard Worker size_t off = gen->data_cur - gen->data_start;
76*f7c14bbaSAndroid Build Coastguard Worker void *data_start;
77*f7c14bbaSAndroid Build Coastguard Worker
78*f7c14bbaSAndroid Build Coastguard Worker if (gen->error)
79*f7c14bbaSAndroid Build Coastguard Worker return gen->error;
80*f7c14bbaSAndroid Build Coastguard Worker if (size > INT32_MAX || off + size > INT32_MAX) {
81*f7c14bbaSAndroid Build Coastguard Worker gen->error = -ERANGE;
82*f7c14bbaSAndroid Build Coastguard Worker return -ERANGE;
83*f7c14bbaSAndroid Build Coastguard Worker }
84*f7c14bbaSAndroid Build Coastguard Worker data_start = realloc(gen->data_start, off + size);
85*f7c14bbaSAndroid Build Coastguard Worker if (!data_start) {
86*f7c14bbaSAndroid Build Coastguard Worker gen->error = -ENOMEM;
87*f7c14bbaSAndroid Build Coastguard Worker free(gen->data_start);
88*f7c14bbaSAndroid Build Coastguard Worker gen->data_start = NULL;
89*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
90*f7c14bbaSAndroid Build Coastguard Worker }
91*f7c14bbaSAndroid Build Coastguard Worker gen->data_start = data_start;
92*f7c14bbaSAndroid Build Coastguard Worker gen->data_cur = data_start + off;
93*f7c14bbaSAndroid Build Coastguard Worker return 0;
94*f7c14bbaSAndroid Build Coastguard Worker }
95*f7c14bbaSAndroid Build Coastguard Worker
emit(struct bpf_gen * gen,struct bpf_insn insn)96*f7c14bbaSAndroid Build Coastguard Worker static void emit(struct bpf_gen *gen, struct bpf_insn insn)
97*f7c14bbaSAndroid Build Coastguard Worker {
98*f7c14bbaSAndroid Build Coastguard Worker if (realloc_insn_buf(gen, sizeof(insn)))
99*f7c14bbaSAndroid Build Coastguard Worker return;
100*f7c14bbaSAndroid Build Coastguard Worker memcpy(gen->insn_cur, &insn, sizeof(insn));
101*f7c14bbaSAndroid Build Coastguard Worker gen->insn_cur += sizeof(insn);
102*f7c14bbaSAndroid Build Coastguard Worker }
103*f7c14bbaSAndroid Build Coastguard Worker
emit2(struct bpf_gen * gen,struct bpf_insn insn1,struct bpf_insn insn2)104*f7c14bbaSAndroid Build Coastguard Worker static void emit2(struct bpf_gen *gen, struct bpf_insn insn1, struct bpf_insn insn2)
105*f7c14bbaSAndroid Build Coastguard Worker {
106*f7c14bbaSAndroid Build Coastguard Worker emit(gen, insn1);
107*f7c14bbaSAndroid Build Coastguard Worker emit(gen, insn2);
108*f7c14bbaSAndroid Build Coastguard Worker }
109*f7c14bbaSAndroid Build Coastguard Worker
110*f7c14bbaSAndroid Build Coastguard Worker static int add_data(struct bpf_gen *gen, const void *data, __u32 size);
111*f7c14bbaSAndroid Build Coastguard Worker static void emit_sys_close_blob(struct bpf_gen *gen, int blob_off);
112*f7c14bbaSAndroid Build Coastguard Worker
bpf_gen__init(struct bpf_gen * gen,int log_level,int nr_progs,int nr_maps)113*f7c14bbaSAndroid Build Coastguard Worker void bpf_gen__init(struct bpf_gen *gen, int log_level, int nr_progs, int nr_maps)
114*f7c14bbaSAndroid Build Coastguard Worker {
115*f7c14bbaSAndroid Build Coastguard Worker size_t stack_sz = sizeof(struct loader_stack), nr_progs_sz;
116*f7c14bbaSAndroid Build Coastguard Worker int i;
117*f7c14bbaSAndroid Build Coastguard Worker
118*f7c14bbaSAndroid Build Coastguard Worker gen->fd_array = add_data(gen, NULL, MAX_FD_ARRAY_SZ * sizeof(int));
119*f7c14bbaSAndroid Build Coastguard Worker gen->log_level = log_level;
120*f7c14bbaSAndroid Build Coastguard Worker /* save ctx pointer into R6 */
121*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_REG(BPF_REG_6, BPF_REG_1));
122*f7c14bbaSAndroid Build Coastguard Worker
123*f7c14bbaSAndroid Build Coastguard Worker /* bzero stack */
124*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_REG(BPF_REG_1, BPF_REG_10));
125*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -stack_sz));
126*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_IMM(BPF_REG_2, stack_sz));
127*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_IMM(BPF_REG_3, 0));
128*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_EMIT_CALL(BPF_FUNC_probe_read_kernel));
129*f7c14bbaSAndroid Build Coastguard Worker
130*f7c14bbaSAndroid Build Coastguard Worker /* amount of stack actually used, only used to calculate iterations, not stack offset */
131*f7c14bbaSAndroid Build Coastguard Worker nr_progs_sz = offsetof(struct loader_stack, prog_fd[nr_progs]);
132*f7c14bbaSAndroid Build Coastguard Worker /* jump over cleanup code */
133*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_JMP_IMM(BPF_JA, 0, 0,
134*f7c14bbaSAndroid Build Coastguard Worker /* size of cleanup code below (including map fd cleanup) */
135*f7c14bbaSAndroid Build Coastguard Worker (nr_progs_sz / 4) * 3 + 2 +
136*f7c14bbaSAndroid Build Coastguard Worker /* 6 insns for emit_sys_close_blob,
137*f7c14bbaSAndroid Build Coastguard Worker * 6 insns for debug_regs in emit_sys_close_blob
138*f7c14bbaSAndroid Build Coastguard Worker */
139*f7c14bbaSAndroid Build Coastguard Worker nr_maps * (6 + (gen->log_level ? 6 : 0))));
140*f7c14bbaSAndroid Build Coastguard Worker
141*f7c14bbaSAndroid Build Coastguard Worker /* remember the label where all error branches will jump to */
142*f7c14bbaSAndroid Build Coastguard Worker gen->cleanup_label = gen->insn_cur - gen->insn_start;
143*f7c14bbaSAndroid Build Coastguard Worker /* emit cleanup code: close all temp FDs */
144*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < nr_progs_sz; i += 4) {
145*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_10, -stack_sz + i));
146*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_JMP_IMM(BPF_JSLE, BPF_REG_1, 0, 1));
147*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_EMIT_CALL(BPF_FUNC_sys_close));
148*f7c14bbaSAndroid Build Coastguard Worker }
149*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < nr_maps; i++)
150*f7c14bbaSAndroid Build Coastguard Worker emit_sys_close_blob(gen, blob_fd_array_off(gen, i));
151*f7c14bbaSAndroid Build Coastguard Worker /* R7 contains the error code from sys_bpf. Copy it into R0 and exit. */
152*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_REG(BPF_REG_0, BPF_REG_7));
153*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_EXIT_INSN());
154*f7c14bbaSAndroid Build Coastguard Worker }
155*f7c14bbaSAndroid Build Coastguard Worker
add_data(struct bpf_gen * gen,const void * data,__u32 size)156*f7c14bbaSAndroid Build Coastguard Worker static int add_data(struct bpf_gen *gen, const void *data, __u32 size)
157*f7c14bbaSAndroid Build Coastguard Worker {
158*f7c14bbaSAndroid Build Coastguard Worker __u32 size8 = roundup(size, 8);
159*f7c14bbaSAndroid Build Coastguard Worker __u64 zero = 0;
160*f7c14bbaSAndroid Build Coastguard Worker void *prev;
161*f7c14bbaSAndroid Build Coastguard Worker
162*f7c14bbaSAndroid Build Coastguard Worker if (realloc_data_buf(gen, size8))
163*f7c14bbaSAndroid Build Coastguard Worker return 0;
164*f7c14bbaSAndroid Build Coastguard Worker prev = gen->data_cur;
165*f7c14bbaSAndroid Build Coastguard Worker if (data) {
166*f7c14bbaSAndroid Build Coastguard Worker memcpy(gen->data_cur, data, size);
167*f7c14bbaSAndroid Build Coastguard Worker memcpy(gen->data_cur + size, &zero, size8 - size);
168*f7c14bbaSAndroid Build Coastguard Worker } else {
169*f7c14bbaSAndroid Build Coastguard Worker memset(gen->data_cur, 0, size8);
170*f7c14bbaSAndroid Build Coastguard Worker }
171*f7c14bbaSAndroid Build Coastguard Worker gen->data_cur += size8;
172*f7c14bbaSAndroid Build Coastguard Worker return prev - gen->data_start;
173*f7c14bbaSAndroid Build Coastguard Worker }
174*f7c14bbaSAndroid Build Coastguard Worker
175*f7c14bbaSAndroid Build Coastguard Worker /* Get index for map_fd/btf_fd slot in reserved fd_array, or in data relative
176*f7c14bbaSAndroid Build Coastguard Worker * to start of fd_array. Caller can decide if it is usable or not.
177*f7c14bbaSAndroid Build Coastguard Worker */
add_map_fd(struct bpf_gen * gen)178*f7c14bbaSAndroid Build Coastguard Worker static int add_map_fd(struct bpf_gen *gen)
179*f7c14bbaSAndroid Build Coastguard Worker {
180*f7c14bbaSAndroid Build Coastguard Worker if (gen->nr_maps == MAX_USED_MAPS) {
181*f7c14bbaSAndroid Build Coastguard Worker pr_warn("Total maps exceeds %d\n", MAX_USED_MAPS);
182*f7c14bbaSAndroid Build Coastguard Worker gen->error = -E2BIG;
183*f7c14bbaSAndroid Build Coastguard Worker return 0;
184*f7c14bbaSAndroid Build Coastguard Worker }
185*f7c14bbaSAndroid Build Coastguard Worker return gen->nr_maps++;
186*f7c14bbaSAndroid Build Coastguard Worker }
187*f7c14bbaSAndroid Build Coastguard Worker
add_kfunc_btf_fd(struct bpf_gen * gen)188*f7c14bbaSAndroid Build Coastguard Worker static int add_kfunc_btf_fd(struct bpf_gen *gen)
189*f7c14bbaSAndroid Build Coastguard Worker {
190*f7c14bbaSAndroid Build Coastguard Worker int cur;
191*f7c14bbaSAndroid Build Coastguard Worker
192*f7c14bbaSAndroid Build Coastguard Worker if (gen->nr_fd_array == MAX_KFUNC_DESCS) {
193*f7c14bbaSAndroid Build Coastguard Worker cur = add_data(gen, NULL, sizeof(int));
194*f7c14bbaSAndroid Build Coastguard Worker return (cur - gen->fd_array) / sizeof(int);
195*f7c14bbaSAndroid Build Coastguard Worker }
196*f7c14bbaSAndroid Build Coastguard Worker return MAX_USED_MAPS + gen->nr_fd_array++;
197*f7c14bbaSAndroid Build Coastguard Worker }
198*f7c14bbaSAndroid Build Coastguard Worker
insn_bytes_to_bpf_size(__u32 sz)199*f7c14bbaSAndroid Build Coastguard Worker static int insn_bytes_to_bpf_size(__u32 sz)
200*f7c14bbaSAndroid Build Coastguard Worker {
201*f7c14bbaSAndroid Build Coastguard Worker switch (sz) {
202*f7c14bbaSAndroid Build Coastguard Worker case 8: return BPF_DW;
203*f7c14bbaSAndroid Build Coastguard Worker case 4: return BPF_W;
204*f7c14bbaSAndroid Build Coastguard Worker case 2: return BPF_H;
205*f7c14bbaSAndroid Build Coastguard Worker case 1: return BPF_B;
206*f7c14bbaSAndroid Build Coastguard Worker default: return -1;
207*f7c14bbaSAndroid Build Coastguard Worker }
208*f7c14bbaSAndroid Build Coastguard Worker }
209*f7c14bbaSAndroid Build Coastguard Worker
210*f7c14bbaSAndroid Build Coastguard Worker /* *(u64 *)(blob + off) = (u64)(void *)(blob + data) */
emit_rel_store(struct bpf_gen * gen,int off,int data)211*f7c14bbaSAndroid Build Coastguard Worker static void emit_rel_store(struct bpf_gen *gen, int off, int data)
212*f7c14bbaSAndroid Build Coastguard Worker {
213*f7c14bbaSAndroid Build Coastguard Worker emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_0, BPF_PSEUDO_MAP_IDX_VALUE,
214*f7c14bbaSAndroid Build Coastguard Worker 0, 0, 0, data));
215*f7c14bbaSAndroid Build Coastguard Worker emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE,
216*f7c14bbaSAndroid Build Coastguard Worker 0, 0, 0, off));
217*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, 0));
218*f7c14bbaSAndroid Build Coastguard Worker }
219*f7c14bbaSAndroid Build Coastguard Worker
move_blob2blob(struct bpf_gen * gen,int off,int size,int blob_off)220*f7c14bbaSAndroid Build Coastguard Worker static void move_blob2blob(struct bpf_gen *gen, int off, int size, int blob_off)
221*f7c14bbaSAndroid Build Coastguard Worker {
222*f7c14bbaSAndroid Build Coastguard Worker emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_2, BPF_PSEUDO_MAP_IDX_VALUE,
223*f7c14bbaSAndroid Build Coastguard Worker 0, 0, 0, blob_off));
224*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_LDX_MEM(insn_bytes_to_bpf_size(size), BPF_REG_0, BPF_REG_2, 0));
225*f7c14bbaSAndroid Build Coastguard Worker emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE,
226*f7c14bbaSAndroid Build Coastguard Worker 0, 0, 0, off));
227*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_STX_MEM(insn_bytes_to_bpf_size(size), BPF_REG_1, BPF_REG_0, 0));
228*f7c14bbaSAndroid Build Coastguard Worker }
229*f7c14bbaSAndroid Build Coastguard Worker
move_blob2ctx(struct bpf_gen * gen,int ctx_off,int size,int blob_off)230*f7c14bbaSAndroid Build Coastguard Worker static void move_blob2ctx(struct bpf_gen *gen, int ctx_off, int size, int blob_off)
231*f7c14bbaSAndroid Build Coastguard Worker {
232*f7c14bbaSAndroid Build Coastguard Worker emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE,
233*f7c14bbaSAndroid Build Coastguard Worker 0, 0, 0, blob_off));
234*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_LDX_MEM(insn_bytes_to_bpf_size(size), BPF_REG_0, BPF_REG_1, 0));
235*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_STX_MEM(insn_bytes_to_bpf_size(size), BPF_REG_6, BPF_REG_0, ctx_off));
236*f7c14bbaSAndroid Build Coastguard Worker }
237*f7c14bbaSAndroid Build Coastguard Worker
move_ctx2blob(struct bpf_gen * gen,int off,int size,int ctx_off,bool check_non_zero)238*f7c14bbaSAndroid Build Coastguard Worker static void move_ctx2blob(struct bpf_gen *gen, int off, int size, int ctx_off,
239*f7c14bbaSAndroid Build Coastguard Worker bool check_non_zero)
240*f7c14bbaSAndroid Build Coastguard Worker {
241*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_LDX_MEM(insn_bytes_to_bpf_size(size), BPF_REG_0, BPF_REG_6, ctx_off));
242*f7c14bbaSAndroid Build Coastguard Worker if (check_non_zero)
243*f7c14bbaSAndroid Build Coastguard Worker /* If value in ctx is zero don't update the blob.
244*f7c14bbaSAndroid Build Coastguard Worker * For example: when ctx->map.max_entries == 0, keep default max_entries from bpf.c
245*f7c14bbaSAndroid Build Coastguard Worker */
246*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 3));
247*f7c14bbaSAndroid Build Coastguard Worker emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE,
248*f7c14bbaSAndroid Build Coastguard Worker 0, 0, 0, off));
249*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_STX_MEM(insn_bytes_to_bpf_size(size), BPF_REG_1, BPF_REG_0, 0));
250*f7c14bbaSAndroid Build Coastguard Worker }
251*f7c14bbaSAndroid Build Coastguard Worker
move_stack2blob(struct bpf_gen * gen,int off,int size,int stack_off)252*f7c14bbaSAndroid Build Coastguard Worker static void move_stack2blob(struct bpf_gen *gen, int off, int size, int stack_off)
253*f7c14bbaSAndroid Build Coastguard Worker {
254*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_LDX_MEM(insn_bytes_to_bpf_size(size), BPF_REG_0, BPF_REG_10, stack_off));
255*f7c14bbaSAndroid Build Coastguard Worker emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE,
256*f7c14bbaSAndroid Build Coastguard Worker 0, 0, 0, off));
257*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_STX_MEM(insn_bytes_to_bpf_size(size), BPF_REG_1, BPF_REG_0, 0));
258*f7c14bbaSAndroid Build Coastguard Worker }
259*f7c14bbaSAndroid Build Coastguard Worker
move_stack2ctx(struct bpf_gen * gen,int ctx_off,int size,int stack_off)260*f7c14bbaSAndroid Build Coastguard Worker static void move_stack2ctx(struct bpf_gen *gen, int ctx_off, int size, int stack_off)
261*f7c14bbaSAndroid Build Coastguard Worker {
262*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_LDX_MEM(insn_bytes_to_bpf_size(size), BPF_REG_0, BPF_REG_10, stack_off));
263*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_STX_MEM(insn_bytes_to_bpf_size(size), BPF_REG_6, BPF_REG_0, ctx_off));
264*f7c14bbaSAndroid Build Coastguard Worker }
265*f7c14bbaSAndroid Build Coastguard Worker
emit_sys_bpf(struct bpf_gen * gen,int cmd,int attr,int attr_size)266*f7c14bbaSAndroid Build Coastguard Worker static void emit_sys_bpf(struct bpf_gen *gen, int cmd, int attr, int attr_size)
267*f7c14bbaSAndroid Build Coastguard Worker {
268*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_IMM(BPF_REG_1, cmd));
269*f7c14bbaSAndroid Build Coastguard Worker emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_2, BPF_PSEUDO_MAP_IDX_VALUE,
270*f7c14bbaSAndroid Build Coastguard Worker 0, 0, 0, attr));
271*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_IMM(BPF_REG_3, attr_size));
272*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_EMIT_CALL(BPF_FUNC_sys_bpf));
273*f7c14bbaSAndroid Build Coastguard Worker /* remember the result in R7 */
274*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_REG(BPF_REG_7, BPF_REG_0));
275*f7c14bbaSAndroid Build Coastguard Worker }
276*f7c14bbaSAndroid Build Coastguard Worker
is_simm16(__s64 value)277*f7c14bbaSAndroid Build Coastguard Worker static bool is_simm16(__s64 value)
278*f7c14bbaSAndroid Build Coastguard Worker {
279*f7c14bbaSAndroid Build Coastguard Worker return value == (__s64)(__s16)value;
280*f7c14bbaSAndroid Build Coastguard Worker }
281*f7c14bbaSAndroid Build Coastguard Worker
emit_check_err(struct bpf_gen * gen)282*f7c14bbaSAndroid Build Coastguard Worker static void emit_check_err(struct bpf_gen *gen)
283*f7c14bbaSAndroid Build Coastguard Worker {
284*f7c14bbaSAndroid Build Coastguard Worker __s64 off = -(gen->insn_cur - gen->insn_start - gen->cleanup_label) / 8 - 1;
285*f7c14bbaSAndroid Build Coastguard Worker
286*f7c14bbaSAndroid Build Coastguard Worker /* R7 contains result of last sys_bpf command.
287*f7c14bbaSAndroid Build Coastguard Worker * if (R7 < 0) goto cleanup;
288*f7c14bbaSAndroid Build Coastguard Worker */
289*f7c14bbaSAndroid Build Coastguard Worker if (is_simm16(off)) {
290*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_JMP_IMM(BPF_JSLT, BPF_REG_7, 0, off));
291*f7c14bbaSAndroid Build Coastguard Worker } else {
292*f7c14bbaSAndroid Build Coastguard Worker gen->error = -ERANGE;
293*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_JMP_IMM(BPF_JA, 0, 0, -1));
294*f7c14bbaSAndroid Build Coastguard Worker }
295*f7c14bbaSAndroid Build Coastguard Worker }
296*f7c14bbaSAndroid Build Coastguard Worker
297*f7c14bbaSAndroid Build Coastguard Worker /* reg1 and reg2 should not be R1 - R5. They can be R0, R6 - R10 */
emit_debug(struct bpf_gen * gen,int reg1,int reg2,const char * fmt,va_list args)298*f7c14bbaSAndroid Build Coastguard Worker static void emit_debug(struct bpf_gen *gen, int reg1, int reg2,
299*f7c14bbaSAndroid Build Coastguard Worker const char *fmt, va_list args)
300*f7c14bbaSAndroid Build Coastguard Worker {
301*f7c14bbaSAndroid Build Coastguard Worker char buf[1024];
302*f7c14bbaSAndroid Build Coastguard Worker int addr, len, ret;
303*f7c14bbaSAndroid Build Coastguard Worker
304*f7c14bbaSAndroid Build Coastguard Worker if (!gen->log_level)
305*f7c14bbaSAndroid Build Coastguard Worker return;
306*f7c14bbaSAndroid Build Coastguard Worker ret = vsnprintf(buf, sizeof(buf), fmt, args);
307*f7c14bbaSAndroid Build Coastguard Worker if (ret < 1024 - 7 && reg1 >= 0 && reg2 < 0)
308*f7c14bbaSAndroid Build Coastguard Worker /* The special case to accommodate common debug_ret():
309*f7c14bbaSAndroid Build Coastguard Worker * to avoid specifying BPF_REG_7 and adding " r=%%d" to
310*f7c14bbaSAndroid Build Coastguard Worker * prints explicitly.
311*f7c14bbaSAndroid Build Coastguard Worker */
312*f7c14bbaSAndroid Build Coastguard Worker strcat(buf, " r=%d");
313*f7c14bbaSAndroid Build Coastguard Worker len = strlen(buf) + 1;
314*f7c14bbaSAndroid Build Coastguard Worker addr = add_data(gen, buf, len);
315*f7c14bbaSAndroid Build Coastguard Worker
316*f7c14bbaSAndroid Build Coastguard Worker emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE,
317*f7c14bbaSAndroid Build Coastguard Worker 0, 0, 0, addr));
318*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_IMM(BPF_REG_2, len));
319*f7c14bbaSAndroid Build Coastguard Worker if (reg1 >= 0)
320*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_REG(BPF_REG_3, reg1));
321*f7c14bbaSAndroid Build Coastguard Worker if (reg2 >= 0)
322*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_REG(BPF_REG_4, reg2));
323*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_EMIT_CALL(BPF_FUNC_trace_printk));
324*f7c14bbaSAndroid Build Coastguard Worker }
325*f7c14bbaSAndroid Build Coastguard Worker
debug_regs(struct bpf_gen * gen,int reg1,int reg2,const char * fmt,...)326*f7c14bbaSAndroid Build Coastguard Worker static void debug_regs(struct bpf_gen *gen, int reg1, int reg2, const char *fmt, ...)
327*f7c14bbaSAndroid Build Coastguard Worker {
328*f7c14bbaSAndroid Build Coastguard Worker va_list args;
329*f7c14bbaSAndroid Build Coastguard Worker
330*f7c14bbaSAndroid Build Coastguard Worker va_start(args, fmt);
331*f7c14bbaSAndroid Build Coastguard Worker emit_debug(gen, reg1, reg2, fmt, args);
332*f7c14bbaSAndroid Build Coastguard Worker va_end(args);
333*f7c14bbaSAndroid Build Coastguard Worker }
334*f7c14bbaSAndroid Build Coastguard Worker
debug_ret(struct bpf_gen * gen,const char * fmt,...)335*f7c14bbaSAndroid Build Coastguard Worker static void debug_ret(struct bpf_gen *gen, const char *fmt, ...)
336*f7c14bbaSAndroid Build Coastguard Worker {
337*f7c14bbaSAndroid Build Coastguard Worker va_list args;
338*f7c14bbaSAndroid Build Coastguard Worker
339*f7c14bbaSAndroid Build Coastguard Worker va_start(args, fmt);
340*f7c14bbaSAndroid Build Coastguard Worker emit_debug(gen, BPF_REG_7, -1, fmt, args);
341*f7c14bbaSAndroid Build Coastguard Worker va_end(args);
342*f7c14bbaSAndroid Build Coastguard Worker }
343*f7c14bbaSAndroid Build Coastguard Worker
__emit_sys_close(struct bpf_gen * gen)344*f7c14bbaSAndroid Build Coastguard Worker static void __emit_sys_close(struct bpf_gen *gen)
345*f7c14bbaSAndroid Build Coastguard Worker {
346*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_JMP_IMM(BPF_JSLE, BPF_REG_1, 0,
347*f7c14bbaSAndroid Build Coastguard Worker /* 2 is the number of the following insns
348*f7c14bbaSAndroid Build Coastguard Worker * * 6 is additional insns in debug_regs
349*f7c14bbaSAndroid Build Coastguard Worker */
350*f7c14bbaSAndroid Build Coastguard Worker 2 + (gen->log_level ? 6 : 0)));
351*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_REG(BPF_REG_9, BPF_REG_1));
352*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_EMIT_CALL(BPF_FUNC_sys_close));
353*f7c14bbaSAndroid Build Coastguard Worker debug_regs(gen, BPF_REG_9, BPF_REG_0, "close(%%d) = %%d");
354*f7c14bbaSAndroid Build Coastguard Worker }
355*f7c14bbaSAndroid Build Coastguard Worker
emit_sys_close_stack(struct bpf_gen * gen,int stack_off)356*f7c14bbaSAndroid Build Coastguard Worker static void emit_sys_close_stack(struct bpf_gen *gen, int stack_off)
357*f7c14bbaSAndroid Build Coastguard Worker {
358*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_10, stack_off));
359*f7c14bbaSAndroid Build Coastguard Worker __emit_sys_close(gen);
360*f7c14bbaSAndroid Build Coastguard Worker }
361*f7c14bbaSAndroid Build Coastguard Worker
emit_sys_close_blob(struct bpf_gen * gen,int blob_off)362*f7c14bbaSAndroid Build Coastguard Worker static void emit_sys_close_blob(struct bpf_gen *gen, int blob_off)
363*f7c14bbaSAndroid Build Coastguard Worker {
364*f7c14bbaSAndroid Build Coastguard Worker emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_0, BPF_PSEUDO_MAP_IDX_VALUE,
365*f7c14bbaSAndroid Build Coastguard Worker 0, 0, 0, blob_off));
366*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, 0));
367*f7c14bbaSAndroid Build Coastguard Worker __emit_sys_close(gen);
368*f7c14bbaSAndroid Build Coastguard Worker }
369*f7c14bbaSAndroid Build Coastguard Worker
bpf_gen__finish(struct bpf_gen * gen,int nr_progs,int nr_maps)370*f7c14bbaSAndroid Build Coastguard Worker int bpf_gen__finish(struct bpf_gen *gen, int nr_progs, int nr_maps)
371*f7c14bbaSAndroid Build Coastguard Worker {
372*f7c14bbaSAndroid Build Coastguard Worker int i;
373*f7c14bbaSAndroid Build Coastguard Worker
374*f7c14bbaSAndroid Build Coastguard Worker if (nr_progs < gen->nr_progs || nr_maps != gen->nr_maps) {
375*f7c14bbaSAndroid Build Coastguard Worker pr_warn("nr_progs %d/%d nr_maps %d/%d mismatch\n",
376*f7c14bbaSAndroid Build Coastguard Worker nr_progs, gen->nr_progs, nr_maps, gen->nr_maps);
377*f7c14bbaSAndroid Build Coastguard Worker gen->error = -EFAULT;
378*f7c14bbaSAndroid Build Coastguard Worker return gen->error;
379*f7c14bbaSAndroid Build Coastguard Worker }
380*f7c14bbaSAndroid Build Coastguard Worker emit_sys_close_stack(gen, stack_off(btf_fd));
381*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < gen->nr_progs; i++)
382*f7c14bbaSAndroid Build Coastguard Worker move_stack2ctx(gen,
383*f7c14bbaSAndroid Build Coastguard Worker sizeof(struct bpf_loader_ctx) +
384*f7c14bbaSAndroid Build Coastguard Worker sizeof(struct bpf_map_desc) * gen->nr_maps +
385*f7c14bbaSAndroid Build Coastguard Worker sizeof(struct bpf_prog_desc) * i +
386*f7c14bbaSAndroid Build Coastguard Worker offsetof(struct bpf_prog_desc, prog_fd), 4,
387*f7c14bbaSAndroid Build Coastguard Worker stack_off(prog_fd[i]));
388*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < gen->nr_maps; i++)
389*f7c14bbaSAndroid Build Coastguard Worker move_blob2ctx(gen,
390*f7c14bbaSAndroid Build Coastguard Worker sizeof(struct bpf_loader_ctx) +
391*f7c14bbaSAndroid Build Coastguard Worker sizeof(struct bpf_map_desc) * i +
392*f7c14bbaSAndroid Build Coastguard Worker offsetof(struct bpf_map_desc, map_fd), 4,
393*f7c14bbaSAndroid Build Coastguard Worker blob_fd_array_off(gen, i));
394*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_IMM(BPF_REG_0, 0));
395*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_EXIT_INSN());
396*f7c14bbaSAndroid Build Coastguard Worker pr_debug("gen: finish %d\n", gen->error);
397*f7c14bbaSAndroid Build Coastguard Worker if (!gen->error) {
398*f7c14bbaSAndroid Build Coastguard Worker struct gen_loader_opts *opts = gen->opts;
399*f7c14bbaSAndroid Build Coastguard Worker
400*f7c14bbaSAndroid Build Coastguard Worker opts->insns = gen->insn_start;
401*f7c14bbaSAndroid Build Coastguard Worker opts->insns_sz = gen->insn_cur - gen->insn_start;
402*f7c14bbaSAndroid Build Coastguard Worker opts->data = gen->data_start;
403*f7c14bbaSAndroid Build Coastguard Worker opts->data_sz = gen->data_cur - gen->data_start;
404*f7c14bbaSAndroid Build Coastguard Worker }
405*f7c14bbaSAndroid Build Coastguard Worker return gen->error;
406*f7c14bbaSAndroid Build Coastguard Worker }
407*f7c14bbaSAndroid Build Coastguard Worker
bpf_gen__free(struct bpf_gen * gen)408*f7c14bbaSAndroid Build Coastguard Worker void bpf_gen__free(struct bpf_gen *gen)
409*f7c14bbaSAndroid Build Coastguard Worker {
410*f7c14bbaSAndroid Build Coastguard Worker if (!gen)
411*f7c14bbaSAndroid Build Coastguard Worker return;
412*f7c14bbaSAndroid Build Coastguard Worker free(gen->data_start);
413*f7c14bbaSAndroid Build Coastguard Worker free(gen->insn_start);
414*f7c14bbaSAndroid Build Coastguard Worker free(gen);
415*f7c14bbaSAndroid Build Coastguard Worker }
416*f7c14bbaSAndroid Build Coastguard Worker
bpf_gen__load_btf(struct bpf_gen * gen,const void * btf_raw_data,__u32 btf_raw_size)417*f7c14bbaSAndroid Build Coastguard Worker void bpf_gen__load_btf(struct bpf_gen *gen, const void *btf_raw_data,
418*f7c14bbaSAndroid Build Coastguard Worker __u32 btf_raw_size)
419*f7c14bbaSAndroid Build Coastguard Worker {
420*f7c14bbaSAndroid Build Coastguard Worker int attr_size = offsetofend(union bpf_attr, btf_log_level);
421*f7c14bbaSAndroid Build Coastguard Worker int btf_data, btf_load_attr;
422*f7c14bbaSAndroid Build Coastguard Worker union bpf_attr attr;
423*f7c14bbaSAndroid Build Coastguard Worker
424*f7c14bbaSAndroid Build Coastguard Worker memset(&attr, 0, attr_size);
425*f7c14bbaSAndroid Build Coastguard Worker pr_debug("gen: load_btf: size %d\n", btf_raw_size);
426*f7c14bbaSAndroid Build Coastguard Worker btf_data = add_data(gen, btf_raw_data, btf_raw_size);
427*f7c14bbaSAndroid Build Coastguard Worker
428*f7c14bbaSAndroid Build Coastguard Worker attr.btf_size = btf_raw_size;
429*f7c14bbaSAndroid Build Coastguard Worker btf_load_attr = add_data(gen, &attr, attr_size);
430*f7c14bbaSAndroid Build Coastguard Worker
431*f7c14bbaSAndroid Build Coastguard Worker /* populate union bpf_attr with user provided log details */
432*f7c14bbaSAndroid Build Coastguard Worker move_ctx2blob(gen, attr_field(btf_load_attr, btf_log_level), 4,
433*f7c14bbaSAndroid Build Coastguard Worker offsetof(struct bpf_loader_ctx, log_level), false);
434*f7c14bbaSAndroid Build Coastguard Worker move_ctx2blob(gen, attr_field(btf_load_attr, btf_log_size), 4,
435*f7c14bbaSAndroid Build Coastguard Worker offsetof(struct bpf_loader_ctx, log_size), false);
436*f7c14bbaSAndroid Build Coastguard Worker move_ctx2blob(gen, attr_field(btf_load_attr, btf_log_buf), 8,
437*f7c14bbaSAndroid Build Coastguard Worker offsetof(struct bpf_loader_ctx, log_buf), false);
438*f7c14bbaSAndroid Build Coastguard Worker /* populate union bpf_attr with a pointer to the BTF data */
439*f7c14bbaSAndroid Build Coastguard Worker emit_rel_store(gen, attr_field(btf_load_attr, btf), btf_data);
440*f7c14bbaSAndroid Build Coastguard Worker /* emit BTF_LOAD command */
441*f7c14bbaSAndroid Build Coastguard Worker emit_sys_bpf(gen, BPF_BTF_LOAD, btf_load_attr, attr_size);
442*f7c14bbaSAndroid Build Coastguard Worker debug_ret(gen, "btf_load size %d", btf_raw_size);
443*f7c14bbaSAndroid Build Coastguard Worker emit_check_err(gen);
444*f7c14bbaSAndroid Build Coastguard Worker /* remember btf_fd in the stack, if successful */
445*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_7, stack_off(btf_fd)));
446*f7c14bbaSAndroid Build Coastguard Worker }
447*f7c14bbaSAndroid Build Coastguard Worker
bpf_gen__map_create(struct bpf_gen * gen,enum bpf_map_type map_type,const char * map_name,__u32 key_size,__u32 value_size,__u32 max_entries,struct bpf_map_create_opts * map_attr,int map_idx)448*f7c14bbaSAndroid Build Coastguard Worker void bpf_gen__map_create(struct bpf_gen *gen,
449*f7c14bbaSAndroid Build Coastguard Worker enum bpf_map_type map_type,
450*f7c14bbaSAndroid Build Coastguard Worker const char *map_name,
451*f7c14bbaSAndroid Build Coastguard Worker __u32 key_size, __u32 value_size, __u32 max_entries,
452*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map_create_opts *map_attr, int map_idx)
453*f7c14bbaSAndroid Build Coastguard Worker {
454*f7c14bbaSAndroid Build Coastguard Worker int attr_size = offsetofend(union bpf_attr, map_extra);
455*f7c14bbaSAndroid Build Coastguard Worker bool close_inner_map_fd = false;
456*f7c14bbaSAndroid Build Coastguard Worker int map_create_attr, idx;
457*f7c14bbaSAndroid Build Coastguard Worker union bpf_attr attr;
458*f7c14bbaSAndroid Build Coastguard Worker
459*f7c14bbaSAndroid Build Coastguard Worker memset(&attr, 0, attr_size);
460*f7c14bbaSAndroid Build Coastguard Worker attr.map_type = map_type;
461*f7c14bbaSAndroid Build Coastguard Worker attr.key_size = key_size;
462*f7c14bbaSAndroid Build Coastguard Worker attr.value_size = value_size;
463*f7c14bbaSAndroid Build Coastguard Worker attr.map_flags = map_attr->map_flags;
464*f7c14bbaSAndroid Build Coastguard Worker attr.map_extra = map_attr->map_extra;
465*f7c14bbaSAndroid Build Coastguard Worker if (map_name)
466*f7c14bbaSAndroid Build Coastguard Worker libbpf_strlcpy(attr.map_name, map_name, sizeof(attr.map_name));
467*f7c14bbaSAndroid Build Coastguard Worker attr.numa_node = map_attr->numa_node;
468*f7c14bbaSAndroid Build Coastguard Worker attr.map_ifindex = map_attr->map_ifindex;
469*f7c14bbaSAndroid Build Coastguard Worker attr.max_entries = max_entries;
470*f7c14bbaSAndroid Build Coastguard Worker attr.btf_key_type_id = map_attr->btf_key_type_id;
471*f7c14bbaSAndroid Build Coastguard Worker attr.btf_value_type_id = map_attr->btf_value_type_id;
472*f7c14bbaSAndroid Build Coastguard Worker
473*f7c14bbaSAndroid Build Coastguard Worker pr_debug("gen: map_create: %s idx %d type %d value_type_id %d\n",
474*f7c14bbaSAndroid Build Coastguard Worker attr.map_name, map_idx, map_type, attr.btf_value_type_id);
475*f7c14bbaSAndroid Build Coastguard Worker
476*f7c14bbaSAndroid Build Coastguard Worker map_create_attr = add_data(gen, &attr, attr_size);
477*f7c14bbaSAndroid Build Coastguard Worker if (attr.btf_value_type_id)
478*f7c14bbaSAndroid Build Coastguard Worker /* populate union bpf_attr with btf_fd saved in the stack earlier */
479*f7c14bbaSAndroid Build Coastguard Worker move_stack2blob(gen, attr_field(map_create_attr, btf_fd), 4,
480*f7c14bbaSAndroid Build Coastguard Worker stack_off(btf_fd));
481*f7c14bbaSAndroid Build Coastguard Worker switch (attr.map_type) {
482*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_ARRAY_OF_MAPS:
483*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_HASH_OF_MAPS:
484*f7c14bbaSAndroid Build Coastguard Worker move_stack2blob(gen, attr_field(map_create_attr, inner_map_fd), 4,
485*f7c14bbaSAndroid Build Coastguard Worker stack_off(inner_map_fd));
486*f7c14bbaSAndroid Build Coastguard Worker close_inner_map_fd = true;
487*f7c14bbaSAndroid Build Coastguard Worker break;
488*f7c14bbaSAndroid Build Coastguard Worker default:
489*f7c14bbaSAndroid Build Coastguard Worker break;
490*f7c14bbaSAndroid Build Coastguard Worker }
491*f7c14bbaSAndroid Build Coastguard Worker /* conditionally update max_entries */
492*f7c14bbaSAndroid Build Coastguard Worker if (map_idx >= 0)
493*f7c14bbaSAndroid Build Coastguard Worker move_ctx2blob(gen, attr_field(map_create_attr, max_entries), 4,
494*f7c14bbaSAndroid Build Coastguard Worker sizeof(struct bpf_loader_ctx) +
495*f7c14bbaSAndroid Build Coastguard Worker sizeof(struct bpf_map_desc) * map_idx +
496*f7c14bbaSAndroid Build Coastguard Worker offsetof(struct bpf_map_desc, max_entries),
497*f7c14bbaSAndroid Build Coastguard Worker true /* check that max_entries != 0 */);
498*f7c14bbaSAndroid Build Coastguard Worker /* emit MAP_CREATE command */
499*f7c14bbaSAndroid Build Coastguard Worker emit_sys_bpf(gen, BPF_MAP_CREATE, map_create_attr, attr_size);
500*f7c14bbaSAndroid Build Coastguard Worker debug_ret(gen, "map_create %s idx %d type %d value_size %d value_btf_id %d",
501*f7c14bbaSAndroid Build Coastguard Worker attr.map_name, map_idx, map_type, value_size,
502*f7c14bbaSAndroid Build Coastguard Worker attr.btf_value_type_id);
503*f7c14bbaSAndroid Build Coastguard Worker emit_check_err(gen);
504*f7c14bbaSAndroid Build Coastguard Worker /* remember map_fd in the stack, if successful */
505*f7c14bbaSAndroid Build Coastguard Worker if (map_idx < 0) {
506*f7c14bbaSAndroid Build Coastguard Worker /* This bpf_gen__map_create() function is called with map_idx >= 0
507*f7c14bbaSAndroid Build Coastguard Worker * for all maps that libbpf loading logic tracks.
508*f7c14bbaSAndroid Build Coastguard Worker * It's called with -1 to create an inner map.
509*f7c14bbaSAndroid Build Coastguard Worker */
510*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_7,
511*f7c14bbaSAndroid Build Coastguard Worker stack_off(inner_map_fd)));
512*f7c14bbaSAndroid Build Coastguard Worker } else if (map_idx != gen->nr_maps) {
513*f7c14bbaSAndroid Build Coastguard Worker gen->error = -EDOM; /* internal bug */
514*f7c14bbaSAndroid Build Coastguard Worker return;
515*f7c14bbaSAndroid Build Coastguard Worker } else {
516*f7c14bbaSAndroid Build Coastguard Worker /* add_map_fd does gen->nr_maps++ */
517*f7c14bbaSAndroid Build Coastguard Worker idx = add_map_fd(gen);
518*f7c14bbaSAndroid Build Coastguard Worker emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE,
519*f7c14bbaSAndroid Build Coastguard Worker 0, 0, 0, blob_fd_array_off(gen, idx)));
520*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_7, 0));
521*f7c14bbaSAndroid Build Coastguard Worker }
522*f7c14bbaSAndroid Build Coastguard Worker if (close_inner_map_fd)
523*f7c14bbaSAndroid Build Coastguard Worker emit_sys_close_stack(gen, stack_off(inner_map_fd));
524*f7c14bbaSAndroid Build Coastguard Worker }
525*f7c14bbaSAndroid Build Coastguard Worker
bpf_gen__record_attach_target(struct bpf_gen * gen,const char * attach_name,enum bpf_attach_type type)526*f7c14bbaSAndroid Build Coastguard Worker void bpf_gen__record_attach_target(struct bpf_gen *gen, const char *attach_name,
527*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type type)
528*f7c14bbaSAndroid Build Coastguard Worker {
529*f7c14bbaSAndroid Build Coastguard Worker const char *prefix;
530*f7c14bbaSAndroid Build Coastguard Worker int kind, ret;
531*f7c14bbaSAndroid Build Coastguard Worker
532*f7c14bbaSAndroid Build Coastguard Worker btf_get_kernel_prefix_kind(type, &prefix, &kind);
533*f7c14bbaSAndroid Build Coastguard Worker gen->attach_kind = kind;
534*f7c14bbaSAndroid Build Coastguard Worker ret = snprintf(gen->attach_target, sizeof(gen->attach_target), "%s%s",
535*f7c14bbaSAndroid Build Coastguard Worker prefix, attach_name);
536*f7c14bbaSAndroid Build Coastguard Worker if (ret >= sizeof(gen->attach_target))
537*f7c14bbaSAndroid Build Coastguard Worker gen->error = -ENOSPC;
538*f7c14bbaSAndroid Build Coastguard Worker }
539*f7c14bbaSAndroid Build Coastguard Worker
emit_find_attach_target(struct bpf_gen * gen)540*f7c14bbaSAndroid Build Coastguard Worker static void emit_find_attach_target(struct bpf_gen *gen)
541*f7c14bbaSAndroid Build Coastguard Worker {
542*f7c14bbaSAndroid Build Coastguard Worker int name, len = strlen(gen->attach_target) + 1;
543*f7c14bbaSAndroid Build Coastguard Worker
544*f7c14bbaSAndroid Build Coastguard Worker pr_debug("gen: find_attach_tgt %s %d\n", gen->attach_target, gen->attach_kind);
545*f7c14bbaSAndroid Build Coastguard Worker name = add_data(gen, gen->attach_target, len);
546*f7c14bbaSAndroid Build Coastguard Worker
547*f7c14bbaSAndroid Build Coastguard Worker emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE,
548*f7c14bbaSAndroid Build Coastguard Worker 0, 0, 0, name));
549*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_IMM(BPF_REG_2, len));
550*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_IMM(BPF_REG_3, gen->attach_kind));
551*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_IMM(BPF_REG_4, 0));
552*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_EMIT_CALL(BPF_FUNC_btf_find_by_name_kind));
553*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_REG(BPF_REG_7, BPF_REG_0));
554*f7c14bbaSAndroid Build Coastguard Worker debug_ret(gen, "find_by_name_kind(%s,%d)",
555*f7c14bbaSAndroid Build Coastguard Worker gen->attach_target, gen->attach_kind);
556*f7c14bbaSAndroid Build Coastguard Worker emit_check_err(gen);
557*f7c14bbaSAndroid Build Coastguard Worker /* if successful, btf_id is in lower 32-bit of R7 and
558*f7c14bbaSAndroid Build Coastguard Worker * btf_obj_fd is in upper 32-bit
559*f7c14bbaSAndroid Build Coastguard Worker */
560*f7c14bbaSAndroid Build Coastguard Worker }
561*f7c14bbaSAndroid Build Coastguard Worker
bpf_gen__record_extern(struct bpf_gen * gen,const char * name,bool is_weak,bool is_typeless,bool is_ld64,int kind,int insn_idx)562*f7c14bbaSAndroid Build Coastguard Worker void bpf_gen__record_extern(struct bpf_gen *gen, const char *name, bool is_weak,
563*f7c14bbaSAndroid Build Coastguard Worker bool is_typeless, bool is_ld64, int kind, int insn_idx)
564*f7c14bbaSAndroid Build Coastguard Worker {
565*f7c14bbaSAndroid Build Coastguard Worker struct ksym_relo_desc *relo;
566*f7c14bbaSAndroid Build Coastguard Worker
567*f7c14bbaSAndroid Build Coastguard Worker relo = libbpf_reallocarray(gen->relos, gen->relo_cnt + 1, sizeof(*relo));
568*f7c14bbaSAndroid Build Coastguard Worker if (!relo) {
569*f7c14bbaSAndroid Build Coastguard Worker gen->error = -ENOMEM;
570*f7c14bbaSAndroid Build Coastguard Worker return;
571*f7c14bbaSAndroid Build Coastguard Worker }
572*f7c14bbaSAndroid Build Coastguard Worker gen->relos = relo;
573*f7c14bbaSAndroid Build Coastguard Worker relo += gen->relo_cnt;
574*f7c14bbaSAndroid Build Coastguard Worker relo->name = name;
575*f7c14bbaSAndroid Build Coastguard Worker relo->is_weak = is_weak;
576*f7c14bbaSAndroid Build Coastguard Worker relo->is_typeless = is_typeless;
577*f7c14bbaSAndroid Build Coastguard Worker relo->is_ld64 = is_ld64;
578*f7c14bbaSAndroid Build Coastguard Worker relo->kind = kind;
579*f7c14bbaSAndroid Build Coastguard Worker relo->insn_idx = insn_idx;
580*f7c14bbaSAndroid Build Coastguard Worker gen->relo_cnt++;
581*f7c14bbaSAndroid Build Coastguard Worker }
582*f7c14bbaSAndroid Build Coastguard Worker
583*f7c14bbaSAndroid Build Coastguard Worker /* returns existing ksym_desc with ref incremented, or inserts a new one */
get_ksym_desc(struct bpf_gen * gen,struct ksym_relo_desc * relo)584*f7c14bbaSAndroid Build Coastguard Worker static struct ksym_desc *get_ksym_desc(struct bpf_gen *gen, struct ksym_relo_desc *relo)
585*f7c14bbaSAndroid Build Coastguard Worker {
586*f7c14bbaSAndroid Build Coastguard Worker struct ksym_desc *kdesc;
587*f7c14bbaSAndroid Build Coastguard Worker int i;
588*f7c14bbaSAndroid Build Coastguard Worker
589*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < gen->nr_ksyms; i++) {
590*f7c14bbaSAndroid Build Coastguard Worker kdesc = &gen->ksyms[i];
591*f7c14bbaSAndroid Build Coastguard Worker if (kdesc->kind == relo->kind && kdesc->is_ld64 == relo->is_ld64 &&
592*f7c14bbaSAndroid Build Coastguard Worker !strcmp(kdesc->name, relo->name)) {
593*f7c14bbaSAndroid Build Coastguard Worker kdesc->ref++;
594*f7c14bbaSAndroid Build Coastguard Worker return kdesc;
595*f7c14bbaSAndroid Build Coastguard Worker }
596*f7c14bbaSAndroid Build Coastguard Worker }
597*f7c14bbaSAndroid Build Coastguard Worker kdesc = libbpf_reallocarray(gen->ksyms, gen->nr_ksyms + 1, sizeof(*kdesc));
598*f7c14bbaSAndroid Build Coastguard Worker if (!kdesc) {
599*f7c14bbaSAndroid Build Coastguard Worker gen->error = -ENOMEM;
600*f7c14bbaSAndroid Build Coastguard Worker return NULL;
601*f7c14bbaSAndroid Build Coastguard Worker }
602*f7c14bbaSAndroid Build Coastguard Worker gen->ksyms = kdesc;
603*f7c14bbaSAndroid Build Coastguard Worker kdesc = &gen->ksyms[gen->nr_ksyms++];
604*f7c14bbaSAndroid Build Coastguard Worker kdesc->name = relo->name;
605*f7c14bbaSAndroid Build Coastguard Worker kdesc->kind = relo->kind;
606*f7c14bbaSAndroid Build Coastguard Worker kdesc->ref = 1;
607*f7c14bbaSAndroid Build Coastguard Worker kdesc->off = 0;
608*f7c14bbaSAndroid Build Coastguard Worker kdesc->insn = 0;
609*f7c14bbaSAndroid Build Coastguard Worker kdesc->is_ld64 = relo->is_ld64;
610*f7c14bbaSAndroid Build Coastguard Worker return kdesc;
611*f7c14bbaSAndroid Build Coastguard Worker }
612*f7c14bbaSAndroid Build Coastguard Worker
613*f7c14bbaSAndroid Build Coastguard Worker /* Overwrites BPF_REG_{0, 1, 2, 3, 4, 7}
614*f7c14bbaSAndroid Build Coastguard Worker * Returns result in BPF_REG_7
615*f7c14bbaSAndroid Build Coastguard Worker */
emit_bpf_find_by_name_kind(struct bpf_gen * gen,struct ksym_relo_desc * relo)616*f7c14bbaSAndroid Build Coastguard Worker static void emit_bpf_find_by_name_kind(struct bpf_gen *gen, struct ksym_relo_desc *relo)
617*f7c14bbaSAndroid Build Coastguard Worker {
618*f7c14bbaSAndroid Build Coastguard Worker int name_off, len = strlen(relo->name) + 1;
619*f7c14bbaSAndroid Build Coastguard Worker
620*f7c14bbaSAndroid Build Coastguard Worker name_off = add_data(gen, relo->name, len);
621*f7c14bbaSAndroid Build Coastguard Worker emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE,
622*f7c14bbaSAndroid Build Coastguard Worker 0, 0, 0, name_off));
623*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_IMM(BPF_REG_2, len));
624*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_IMM(BPF_REG_3, relo->kind));
625*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_IMM(BPF_REG_4, 0));
626*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_EMIT_CALL(BPF_FUNC_btf_find_by_name_kind));
627*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_REG(BPF_REG_7, BPF_REG_0));
628*f7c14bbaSAndroid Build Coastguard Worker debug_ret(gen, "find_by_name_kind(%s,%d)", relo->name, relo->kind);
629*f7c14bbaSAndroid Build Coastguard Worker }
630*f7c14bbaSAndroid Build Coastguard Worker
631*f7c14bbaSAndroid Build Coastguard Worker /* Overwrites BPF_REG_{0, 1, 2, 3, 4, 7}
632*f7c14bbaSAndroid Build Coastguard Worker * Returns result in BPF_REG_7
633*f7c14bbaSAndroid Build Coastguard Worker * Returns u64 symbol addr in BPF_REG_9
634*f7c14bbaSAndroid Build Coastguard Worker */
emit_bpf_kallsyms_lookup_name(struct bpf_gen * gen,struct ksym_relo_desc * relo)635*f7c14bbaSAndroid Build Coastguard Worker static void emit_bpf_kallsyms_lookup_name(struct bpf_gen *gen, struct ksym_relo_desc *relo)
636*f7c14bbaSAndroid Build Coastguard Worker {
637*f7c14bbaSAndroid Build Coastguard Worker int name_off, len = strlen(relo->name) + 1, res_off;
638*f7c14bbaSAndroid Build Coastguard Worker
639*f7c14bbaSAndroid Build Coastguard Worker name_off = add_data(gen, relo->name, len);
640*f7c14bbaSAndroid Build Coastguard Worker res_off = add_data(gen, NULL, 8); /* res is u64 */
641*f7c14bbaSAndroid Build Coastguard Worker emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE,
642*f7c14bbaSAndroid Build Coastguard Worker 0, 0, 0, name_off));
643*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_IMM(BPF_REG_2, len));
644*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_IMM(BPF_REG_3, 0));
645*f7c14bbaSAndroid Build Coastguard Worker emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_4, BPF_PSEUDO_MAP_IDX_VALUE,
646*f7c14bbaSAndroid Build Coastguard Worker 0, 0, 0, res_off));
647*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_REG(BPF_REG_7, BPF_REG_4));
648*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_EMIT_CALL(BPF_FUNC_kallsyms_lookup_name));
649*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_LDX_MEM(BPF_DW, BPF_REG_9, BPF_REG_7, 0));
650*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_REG(BPF_REG_7, BPF_REG_0));
651*f7c14bbaSAndroid Build Coastguard Worker debug_ret(gen, "kallsyms_lookup_name(%s,%d)", relo->name, relo->kind);
652*f7c14bbaSAndroid Build Coastguard Worker }
653*f7c14bbaSAndroid Build Coastguard Worker
654*f7c14bbaSAndroid Build Coastguard Worker /* Expects:
655*f7c14bbaSAndroid Build Coastguard Worker * BPF_REG_8 - pointer to instruction
656*f7c14bbaSAndroid Build Coastguard Worker *
657*f7c14bbaSAndroid Build Coastguard Worker * We need to reuse BTF fd for same symbol otherwise each relocation takes a new
658*f7c14bbaSAndroid Build Coastguard Worker * index, while kernel limits total kfunc BTFs to 256. For duplicate symbols,
659*f7c14bbaSAndroid Build Coastguard Worker * this would mean a new BTF fd index for each entry. By pairing symbol name
660*f7c14bbaSAndroid Build Coastguard Worker * with index, we get the insn->imm, insn->off pairing that kernel uses for
661*f7c14bbaSAndroid Build Coastguard Worker * kfunc_tab, which becomes the effective limit even though all of them may
662*f7c14bbaSAndroid Build Coastguard Worker * share same index in fd_array (such that kfunc_btf_tab has 1 element).
663*f7c14bbaSAndroid Build Coastguard Worker */
emit_relo_kfunc_btf(struct bpf_gen * gen,struct ksym_relo_desc * relo,int insn)664*f7c14bbaSAndroid Build Coastguard Worker static void emit_relo_kfunc_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo, int insn)
665*f7c14bbaSAndroid Build Coastguard Worker {
666*f7c14bbaSAndroid Build Coastguard Worker struct ksym_desc *kdesc;
667*f7c14bbaSAndroid Build Coastguard Worker int btf_fd_idx;
668*f7c14bbaSAndroid Build Coastguard Worker
669*f7c14bbaSAndroid Build Coastguard Worker kdesc = get_ksym_desc(gen, relo);
670*f7c14bbaSAndroid Build Coastguard Worker if (!kdesc)
671*f7c14bbaSAndroid Build Coastguard Worker return;
672*f7c14bbaSAndroid Build Coastguard Worker /* try to copy from existing bpf_insn */
673*f7c14bbaSAndroid Build Coastguard Worker if (kdesc->ref > 1) {
674*f7c14bbaSAndroid Build Coastguard Worker move_blob2blob(gen, insn + offsetof(struct bpf_insn, imm), 4,
675*f7c14bbaSAndroid Build Coastguard Worker kdesc->insn + offsetof(struct bpf_insn, imm));
676*f7c14bbaSAndroid Build Coastguard Worker move_blob2blob(gen, insn + offsetof(struct bpf_insn, off), 2,
677*f7c14bbaSAndroid Build Coastguard Worker kdesc->insn + offsetof(struct bpf_insn, off));
678*f7c14bbaSAndroid Build Coastguard Worker goto log;
679*f7c14bbaSAndroid Build Coastguard Worker }
680*f7c14bbaSAndroid Build Coastguard Worker /* remember insn offset, so we can copy BTF ID and FD later */
681*f7c14bbaSAndroid Build Coastguard Worker kdesc->insn = insn;
682*f7c14bbaSAndroid Build Coastguard Worker emit_bpf_find_by_name_kind(gen, relo);
683*f7c14bbaSAndroid Build Coastguard Worker if (!relo->is_weak)
684*f7c14bbaSAndroid Build Coastguard Worker emit_check_err(gen);
685*f7c14bbaSAndroid Build Coastguard Worker /* get index in fd_array to store BTF FD at */
686*f7c14bbaSAndroid Build Coastguard Worker btf_fd_idx = add_kfunc_btf_fd(gen);
687*f7c14bbaSAndroid Build Coastguard Worker if (btf_fd_idx > INT16_MAX) {
688*f7c14bbaSAndroid Build Coastguard Worker pr_warn("BTF fd off %d for kfunc %s exceeds INT16_MAX, cannot process relocation\n",
689*f7c14bbaSAndroid Build Coastguard Worker btf_fd_idx, relo->name);
690*f7c14bbaSAndroid Build Coastguard Worker gen->error = -E2BIG;
691*f7c14bbaSAndroid Build Coastguard Worker return;
692*f7c14bbaSAndroid Build Coastguard Worker }
693*f7c14bbaSAndroid Build Coastguard Worker kdesc->off = btf_fd_idx;
694*f7c14bbaSAndroid Build Coastguard Worker /* jump to success case */
695*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_JMP_IMM(BPF_JSGE, BPF_REG_7, 0, 3));
696*f7c14bbaSAndroid Build Coastguard Worker /* set value for imm, off as 0 */
697*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_ST_MEM(BPF_W, BPF_REG_8, offsetof(struct bpf_insn, imm), 0));
698*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_ST_MEM(BPF_H, BPF_REG_8, offsetof(struct bpf_insn, off), 0));
699*f7c14bbaSAndroid Build Coastguard Worker /* skip success case for ret < 0 */
700*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_JMP_IMM(BPF_JA, 0, 0, 10));
701*f7c14bbaSAndroid Build Coastguard Worker /* store btf_id into insn[insn_idx].imm */
702*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_8, BPF_REG_7, offsetof(struct bpf_insn, imm)));
703*f7c14bbaSAndroid Build Coastguard Worker /* obtain fd in BPF_REG_9 */
704*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_REG(BPF_REG_9, BPF_REG_7));
705*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_ALU64_IMM(BPF_RSH, BPF_REG_9, 32));
706*f7c14bbaSAndroid Build Coastguard Worker /* load fd_array slot pointer */
707*f7c14bbaSAndroid Build Coastguard Worker emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_0, BPF_PSEUDO_MAP_IDX_VALUE,
708*f7c14bbaSAndroid Build Coastguard Worker 0, 0, 0, blob_fd_array_off(gen, btf_fd_idx)));
709*f7c14bbaSAndroid Build Coastguard Worker /* store BTF fd in slot, 0 for vmlinux */
710*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_0, BPF_REG_9, 0));
711*f7c14bbaSAndroid Build Coastguard Worker /* jump to insn[insn_idx].off store if fd denotes module BTF */
712*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_JMP_IMM(BPF_JNE, BPF_REG_9, 0, 2));
713*f7c14bbaSAndroid Build Coastguard Worker /* set the default value for off */
714*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_ST_MEM(BPF_H, BPF_REG_8, offsetof(struct bpf_insn, off), 0));
715*f7c14bbaSAndroid Build Coastguard Worker /* skip BTF fd store for vmlinux BTF */
716*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_JMP_IMM(BPF_JA, 0, 0, 1));
717*f7c14bbaSAndroid Build Coastguard Worker /* store index into insn[insn_idx].off */
718*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_ST_MEM(BPF_H, BPF_REG_8, offsetof(struct bpf_insn, off), btf_fd_idx));
719*f7c14bbaSAndroid Build Coastguard Worker log:
720*f7c14bbaSAndroid Build Coastguard Worker if (!gen->log_level)
721*f7c14bbaSAndroid Build Coastguard Worker return;
722*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_8,
723*f7c14bbaSAndroid Build Coastguard Worker offsetof(struct bpf_insn, imm)));
724*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_LDX_MEM(BPF_H, BPF_REG_9, BPF_REG_8,
725*f7c14bbaSAndroid Build Coastguard Worker offsetof(struct bpf_insn, off)));
726*f7c14bbaSAndroid Build Coastguard Worker debug_regs(gen, BPF_REG_7, BPF_REG_9, " func (%s:count=%d): imm: %%d, off: %%d",
727*f7c14bbaSAndroid Build Coastguard Worker relo->name, kdesc->ref);
728*f7c14bbaSAndroid Build Coastguard Worker emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_0, BPF_PSEUDO_MAP_IDX_VALUE,
729*f7c14bbaSAndroid Build Coastguard Worker 0, 0, 0, blob_fd_array_off(gen, kdesc->off)));
730*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_9, BPF_REG_0, 0));
731*f7c14bbaSAndroid Build Coastguard Worker debug_regs(gen, BPF_REG_9, -1, " func (%s:count=%d): btf_fd",
732*f7c14bbaSAndroid Build Coastguard Worker relo->name, kdesc->ref);
733*f7c14bbaSAndroid Build Coastguard Worker }
734*f7c14bbaSAndroid Build Coastguard Worker
emit_ksym_relo_log(struct bpf_gen * gen,struct ksym_relo_desc * relo,int ref)735*f7c14bbaSAndroid Build Coastguard Worker static void emit_ksym_relo_log(struct bpf_gen *gen, struct ksym_relo_desc *relo,
736*f7c14bbaSAndroid Build Coastguard Worker int ref)
737*f7c14bbaSAndroid Build Coastguard Worker {
738*f7c14bbaSAndroid Build Coastguard Worker if (!gen->log_level)
739*f7c14bbaSAndroid Build Coastguard Worker return;
740*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_8,
741*f7c14bbaSAndroid Build Coastguard Worker offsetof(struct bpf_insn, imm)));
742*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_LDX_MEM(BPF_H, BPF_REG_9, BPF_REG_8, sizeof(struct bpf_insn) +
743*f7c14bbaSAndroid Build Coastguard Worker offsetof(struct bpf_insn, imm)));
744*f7c14bbaSAndroid Build Coastguard Worker debug_regs(gen, BPF_REG_7, BPF_REG_9, " var t=%d w=%d (%s:count=%d): imm[0]: %%d, imm[1]: %%d",
745*f7c14bbaSAndroid Build Coastguard Worker relo->is_typeless, relo->is_weak, relo->name, ref);
746*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_LDX_MEM(BPF_B, BPF_REG_9, BPF_REG_8, offsetofend(struct bpf_insn, code)));
747*f7c14bbaSAndroid Build Coastguard Worker debug_regs(gen, BPF_REG_9, -1, " var t=%d w=%d (%s:count=%d): insn.reg",
748*f7c14bbaSAndroid Build Coastguard Worker relo->is_typeless, relo->is_weak, relo->name, ref);
749*f7c14bbaSAndroid Build Coastguard Worker }
750*f7c14bbaSAndroid Build Coastguard Worker
751*f7c14bbaSAndroid Build Coastguard Worker /* Expects:
752*f7c14bbaSAndroid Build Coastguard Worker * BPF_REG_8 - pointer to instruction
753*f7c14bbaSAndroid Build Coastguard Worker */
emit_relo_ksym_typeless(struct bpf_gen * gen,struct ksym_relo_desc * relo,int insn)754*f7c14bbaSAndroid Build Coastguard Worker static void emit_relo_ksym_typeless(struct bpf_gen *gen,
755*f7c14bbaSAndroid Build Coastguard Worker struct ksym_relo_desc *relo, int insn)
756*f7c14bbaSAndroid Build Coastguard Worker {
757*f7c14bbaSAndroid Build Coastguard Worker struct ksym_desc *kdesc;
758*f7c14bbaSAndroid Build Coastguard Worker
759*f7c14bbaSAndroid Build Coastguard Worker kdesc = get_ksym_desc(gen, relo);
760*f7c14bbaSAndroid Build Coastguard Worker if (!kdesc)
761*f7c14bbaSAndroid Build Coastguard Worker return;
762*f7c14bbaSAndroid Build Coastguard Worker /* try to copy from existing ldimm64 insn */
763*f7c14bbaSAndroid Build Coastguard Worker if (kdesc->ref > 1) {
764*f7c14bbaSAndroid Build Coastguard Worker move_blob2blob(gen, insn + offsetof(struct bpf_insn, imm), 4,
765*f7c14bbaSAndroid Build Coastguard Worker kdesc->insn + offsetof(struct bpf_insn, imm));
766*f7c14bbaSAndroid Build Coastguard Worker move_blob2blob(gen, insn + sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm), 4,
767*f7c14bbaSAndroid Build Coastguard Worker kdesc->insn + sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm));
768*f7c14bbaSAndroid Build Coastguard Worker goto log;
769*f7c14bbaSAndroid Build Coastguard Worker }
770*f7c14bbaSAndroid Build Coastguard Worker /* remember insn offset, so we can copy ksym addr later */
771*f7c14bbaSAndroid Build Coastguard Worker kdesc->insn = insn;
772*f7c14bbaSAndroid Build Coastguard Worker /* skip typeless ksym_desc in fd closing loop in cleanup_relos */
773*f7c14bbaSAndroid Build Coastguard Worker kdesc->typeless = true;
774*f7c14bbaSAndroid Build Coastguard Worker emit_bpf_kallsyms_lookup_name(gen, relo);
775*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_JMP_IMM(BPF_JEQ, BPF_REG_7, -ENOENT, 1));
776*f7c14bbaSAndroid Build Coastguard Worker emit_check_err(gen);
777*f7c14bbaSAndroid Build Coastguard Worker /* store lower half of addr into insn[insn_idx].imm */
778*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_8, BPF_REG_9, offsetof(struct bpf_insn, imm)));
779*f7c14bbaSAndroid Build Coastguard Worker /* store upper half of addr into insn[insn_idx + 1].imm */
780*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_ALU64_IMM(BPF_RSH, BPF_REG_9, 32));
781*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_8, BPF_REG_9,
782*f7c14bbaSAndroid Build Coastguard Worker sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm)));
783*f7c14bbaSAndroid Build Coastguard Worker log:
784*f7c14bbaSAndroid Build Coastguard Worker emit_ksym_relo_log(gen, relo, kdesc->ref);
785*f7c14bbaSAndroid Build Coastguard Worker }
786*f7c14bbaSAndroid Build Coastguard Worker
src_reg_mask(void)787*f7c14bbaSAndroid Build Coastguard Worker static __u32 src_reg_mask(void)
788*f7c14bbaSAndroid Build Coastguard Worker {
789*f7c14bbaSAndroid Build Coastguard Worker #if defined(__LITTLE_ENDIAN_BITFIELD)
790*f7c14bbaSAndroid Build Coastguard Worker return 0x0f; /* src_reg,dst_reg,... */
791*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__BIG_ENDIAN_BITFIELD)
792*f7c14bbaSAndroid Build Coastguard Worker return 0xf0; /* dst_reg,src_reg,... */
793*f7c14bbaSAndroid Build Coastguard Worker #else
794*f7c14bbaSAndroid Build Coastguard Worker #error "Unsupported bit endianness, cannot proceed"
795*f7c14bbaSAndroid Build Coastguard Worker #endif
796*f7c14bbaSAndroid Build Coastguard Worker }
797*f7c14bbaSAndroid Build Coastguard Worker
798*f7c14bbaSAndroid Build Coastguard Worker /* Expects:
799*f7c14bbaSAndroid Build Coastguard Worker * BPF_REG_8 - pointer to instruction
800*f7c14bbaSAndroid Build Coastguard Worker */
emit_relo_ksym_btf(struct bpf_gen * gen,struct ksym_relo_desc * relo,int insn)801*f7c14bbaSAndroid Build Coastguard Worker static void emit_relo_ksym_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo, int insn)
802*f7c14bbaSAndroid Build Coastguard Worker {
803*f7c14bbaSAndroid Build Coastguard Worker struct ksym_desc *kdesc;
804*f7c14bbaSAndroid Build Coastguard Worker __u32 reg_mask;
805*f7c14bbaSAndroid Build Coastguard Worker
806*f7c14bbaSAndroid Build Coastguard Worker kdesc = get_ksym_desc(gen, relo);
807*f7c14bbaSAndroid Build Coastguard Worker if (!kdesc)
808*f7c14bbaSAndroid Build Coastguard Worker return;
809*f7c14bbaSAndroid Build Coastguard Worker /* try to copy from existing ldimm64 insn */
810*f7c14bbaSAndroid Build Coastguard Worker if (kdesc->ref > 1) {
811*f7c14bbaSAndroid Build Coastguard Worker move_blob2blob(gen, insn + sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm), 4,
812*f7c14bbaSAndroid Build Coastguard Worker kdesc->insn + sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm));
813*f7c14bbaSAndroid Build Coastguard Worker move_blob2blob(gen, insn + offsetof(struct bpf_insn, imm), 4,
814*f7c14bbaSAndroid Build Coastguard Worker kdesc->insn + offsetof(struct bpf_insn, imm));
815*f7c14bbaSAndroid Build Coastguard Worker /* jump over src_reg adjustment if imm (btf_id) is not 0, reuse BPF_REG_0 from move_blob2blob
816*f7c14bbaSAndroid Build Coastguard Worker * If btf_id is zero, clear BPF_PSEUDO_BTF_ID flag in src_reg of ld_imm64 insn
817*f7c14bbaSAndroid Build Coastguard Worker */
818*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 3));
819*f7c14bbaSAndroid Build Coastguard Worker goto clear_src_reg;
820*f7c14bbaSAndroid Build Coastguard Worker }
821*f7c14bbaSAndroid Build Coastguard Worker /* remember insn offset, so we can copy BTF ID and FD later */
822*f7c14bbaSAndroid Build Coastguard Worker kdesc->insn = insn;
823*f7c14bbaSAndroid Build Coastguard Worker emit_bpf_find_by_name_kind(gen, relo);
824*f7c14bbaSAndroid Build Coastguard Worker if (!relo->is_weak)
825*f7c14bbaSAndroid Build Coastguard Worker emit_check_err(gen);
826*f7c14bbaSAndroid Build Coastguard Worker /* jump to success case */
827*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_JMP_IMM(BPF_JSGE, BPF_REG_7, 0, 3));
828*f7c14bbaSAndroid Build Coastguard Worker /* set values for insn[insn_idx].imm, insn[insn_idx + 1].imm as 0 */
829*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_ST_MEM(BPF_W, BPF_REG_8, offsetof(struct bpf_insn, imm), 0));
830*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_ST_MEM(BPF_W, BPF_REG_8, sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm), 0));
831*f7c14bbaSAndroid Build Coastguard Worker /* skip success case for ret < 0 */
832*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_JMP_IMM(BPF_JA, 0, 0, 4));
833*f7c14bbaSAndroid Build Coastguard Worker /* store btf_id into insn[insn_idx].imm */
834*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_8, BPF_REG_7, offsetof(struct bpf_insn, imm)));
835*f7c14bbaSAndroid Build Coastguard Worker /* store btf_obj_fd into insn[insn_idx + 1].imm */
836*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_ALU64_IMM(BPF_RSH, BPF_REG_7, 32));
837*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_8, BPF_REG_7,
838*f7c14bbaSAndroid Build Coastguard Worker sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm)));
839*f7c14bbaSAndroid Build Coastguard Worker /* skip src_reg adjustment */
840*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_JMP_IMM(BPF_JA, 0, 0, 3));
841*f7c14bbaSAndroid Build Coastguard Worker clear_src_reg:
842*f7c14bbaSAndroid Build Coastguard Worker /* clear bpf_object__relocate_data's src_reg assignment, otherwise we get a verifier failure */
843*f7c14bbaSAndroid Build Coastguard Worker reg_mask = src_reg_mask();
844*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_LDX_MEM(BPF_B, BPF_REG_9, BPF_REG_8, offsetofend(struct bpf_insn, code)));
845*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_ALU32_IMM(BPF_AND, BPF_REG_9, reg_mask));
846*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_STX_MEM(BPF_B, BPF_REG_8, BPF_REG_9, offsetofend(struct bpf_insn, code)));
847*f7c14bbaSAndroid Build Coastguard Worker
848*f7c14bbaSAndroid Build Coastguard Worker emit_ksym_relo_log(gen, relo, kdesc->ref);
849*f7c14bbaSAndroid Build Coastguard Worker }
850*f7c14bbaSAndroid Build Coastguard Worker
bpf_gen__record_relo_core(struct bpf_gen * gen,const struct bpf_core_relo * core_relo)851*f7c14bbaSAndroid Build Coastguard Worker void bpf_gen__record_relo_core(struct bpf_gen *gen,
852*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_core_relo *core_relo)
853*f7c14bbaSAndroid Build Coastguard Worker {
854*f7c14bbaSAndroid Build Coastguard Worker struct bpf_core_relo *relos;
855*f7c14bbaSAndroid Build Coastguard Worker
856*f7c14bbaSAndroid Build Coastguard Worker relos = libbpf_reallocarray(gen->core_relos, gen->core_relo_cnt + 1, sizeof(*relos));
857*f7c14bbaSAndroid Build Coastguard Worker if (!relos) {
858*f7c14bbaSAndroid Build Coastguard Worker gen->error = -ENOMEM;
859*f7c14bbaSAndroid Build Coastguard Worker return;
860*f7c14bbaSAndroid Build Coastguard Worker }
861*f7c14bbaSAndroid Build Coastguard Worker gen->core_relos = relos;
862*f7c14bbaSAndroid Build Coastguard Worker relos += gen->core_relo_cnt;
863*f7c14bbaSAndroid Build Coastguard Worker memcpy(relos, core_relo, sizeof(*relos));
864*f7c14bbaSAndroid Build Coastguard Worker gen->core_relo_cnt++;
865*f7c14bbaSAndroid Build Coastguard Worker }
866*f7c14bbaSAndroid Build Coastguard Worker
emit_relo(struct bpf_gen * gen,struct ksym_relo_desc * relo,int insns)867*f7c14bbaSAndroid Build Coastguard Worker static void emit_relo(struct bpf_gen *gen, struct ksym_relo_desc *relo, int insns)
868*f7c14bbaSAndroid Build Coastguard Worker {
869*f7c14bbaSAndroid Build Coastguard Worker int insn;
870*f7c14bbaSAndroid Build Coastguard Worker
871*f7c14bbaSAndroid Build Coastguard Worker pr_debug("gen: emit_relo (%d): %s at %d %s\n",
872*f7c14bbaSAndroid Build Coastguard Worker relo->kind, relo->name, relo->insn_idx, relo->is_ld64 ? "ld64" : "call");
873*f7c14bbaSAndroid Build Coastguard Worker insn = insns + sizeof(struct bpf_insn) * relo->insn_idx;
874*f7c14bbaSAndroid Build Coastguard Worker emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_8, BPF_PSEUDO_MAP_IDX_VALUE, 0, 0, 0, insn));
875*f7c14bbaSAndroid Build Coastguard Worker if (relo->is_ld64) {
876*f7c14bbaSAndroid Build Coastguard Worker if (relo->is_typeless)
877*f7c14bbaSAndroid Build Coastguard Worker emit_relo_ksym_typeless(gen, relo, insn);
878*f7c14bbaSAndroid Build Coastguard Worker else
879*f7c14bbaSAndroid Build Coastguard Worker emit_relo_ksym_btf(gen, relo, insn);
880*f7c14bbaSAndroid Build Coastguard Worker } else {
881*f7c14bbaSAndroid Build Coastguard Worker emit_relo_kfunc_btf(gen, relo, insn);
882*f7c14bbaSAndroid Build Coastguard Worker }
883*f7c14bbaSAndroid Build Coastguard Worker }
884*f7c14bbaSAndroid Build Coastguard Worker
emit_relos(struct bpf_gen * gen,int insns)885*f7c14bbaSAndroid Build Coastguard Worker static void emit_relos(struct bpf_gen *gen, int insns)
886*f7c14bbaSAndroid Build Coastguard Worker {
887*f7c14bbaSAndroid Build Coastguard Worker int i;
888*f7c14bbaSAndroid Build Coastguard Worker
889*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < gen->relo_cnt; i++)
890*f7c14bbaSAndroid Build Coastguard Worker emit_relo(gen, gen->relos + i, insns);
891*f7c14bbaSAndroid Build Coastguard Worker }
892*f7c14bbaSAndroid Build Coastguard Worker
cleanup_core_relo(struct bpf_gen * gen)893*f7c14bbaSAndroid Build Coastguard Worker static void cleanup_core_relo(struct bpf_gen *gen)
894*f7c14bbaSAndroid Build Coastguard Worker {
895*f7c14bbaSAndroid Build Coastguard Worker if (!gen->core_relo_cnt)
896*f7c14bbaSAndroid Build Coastguard Worker return;
897*f7c14bbaSAndroid Build Coastguard Worker free(gen->core_relos);
898*f7c14bbaSAndroid Build Coastguard Worker gen->core_relo_cnt = 0;
899*f7c14bbaSAndroid Build Coastguard Worker gen->core_relos = NULL;
900*f7c14bbaSAndroid Build Coastguard Worker }
901*f7c14bbaSAndroid Build Coastguard Worker
cleanup_relos(struct bpf_gen * gen,int insns)902*f7c14bbaSAndroid Build Coastguard Worker static void cleanup_relos(struct bpf_gen *gen, int insns)
903*f7c14bbaSAndroid Build Coastguard Worker {
904*f7c14bbaSAndroid Build Coastguard Worker struct ksym_desc *kdesc;
905*f7c14bbaSAndroid Build Coastguard Worker int i, insn;
906*f7c14bbaSAndroid Build Coastguard Worker
907*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < gen->nr_ksyms; i++) {
908*f7c14bbaSAndroid Build Coastguard Worker kdesc = &gen->ksyms[i];
909*f7c14bbaSAndroid Build Coastguard Worker /* only close fds for typed ksyms and kfuncs */
910*f7c14bbaSAndroid Build Coastguard Worker if (kdesc->is_ld64 && !kdesc->typeless) {
911*f7c14bbaSAndroid Build Coastguard Worker /* close fd recorded in insn[insn_idx + 1].imm */
912*f7c14bbaSAndroid Build Coastguard Worker insn = kdesc->insn;
913*f7c14bbaSAndroid Build Coastguard Worker insn += sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm);
914*f7c14bbaSAndroid Build Coastguard Worker emit_sys_close_blob(gen, insn);
915*f7c14bbaSAndroid Build Coastguard Worker } else if (!kdesc->is_ld64) {
916*f7c14bbaSAndroid Build Coastguard Worker emit_sys_close_blob(gen, blob_fd_array_off(gen, kdesc->off));
917*f7c14bbaSAndroid Build Coastguard Worker if (kdesc->off < MAX_FD_ARRAY_SZ)
918*f7c14bbaSAndroid Build Coastguard Worker gen->nr_fd_array--;
919*f7c14bbaSAndroid Build Coastguard Worker }
920*f7c14bbaSAndroid Build Coastguard Worker }
921*f7c14bbaSAndroid Build Coastguard Worker if (gen->nr_ksyms) {
922*f7c14bbaSAndroid Build Coastguard Worker free(gen->ksyms);
923*f7c14bbaSAndroid Build Coastguard Worker gen->nr_ksyms = 0;
924*f7c14bbaSAndroid Build Coastguard Worker gen->ksyms = NULL;
925*f7c14bbaSAndroid Build Coastguard Worker }
926*f7c14bbaSAndroid Build Coastguard Worker if (gen->relo_cnt) {
927*f7c14bbaSAndroid Build Coastguard Worker free(gen->relos);
928*f7c14bbaSAndroid Build Coastguard Worker gen->relo_cnt = 0;
929*f7c14bbaSAndroid Build Coastguard Worker gen->relos = NULL;
930*f7c14bbaSAndroid Build Coastguard Worker }
931*f7c14bbaSAndroid Build Coastguard Worker cleanup_core_relo(gen);
932*f7c14bbaSAndroid Build Coastguard Worker }
933*f7c14bbaSAndroid Build Coastguard Worker
bpf_gen__prog_load(struct bpf_gen * gen,enum bpf_prog_type prog_type,const char * prog_name,const char * license,struct bpf_insn * insns,size_t insn_cnt,struct bpf_prog_load_opts * load_attr,int prog_idx)934*f7c14bbaSAndroid Build Coastguard Worker void bpf_gen__prog_load(struct bpf_gen *gen,
935*f7c14bbaSAndroid Build Coastguard Worker enum bpf_prog_type prog_type, const char *prog_name,
936*f7c14bbaSAndroid Build Coastguard Worker const char *license, struct bpf_insn *insns, size_t insn_cnt,
937*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_load_opts *load_attr, int prog_idx)
938*f7c14bbaSAndroid Build Coastguard Worker {
939*f7c14bbaSAndroid Build Coastguard Worker int prog_load_attr, license_off, insns_off, func_info, line_info, core_relos;
940*f7c14bbaSAndroid Build Coastguard Worker int attr_size = offsetofend(union bpf_attr, core_relo_rec_size);
941*f7c14bbaSAndroid Build Coastguard Worker union bpf_attr attr;
942*f7c14bbaSAndroid Build Coastguard Worker
943*f7c14bbaSAndroid Build Coastguard Worker memset(&attr, 0, attr_size);
944*f7c14bbaSAndroid Build Coastguard Worker pr_debug("gen: prog_load: type %d insns_cnt %zd progi_idx %d\n",
945*f7c14bbaSAndroid Build Coastguard Worker prog_type, insn_cnt, prog_idx);
946*f7c14bbaSAndroid Build Coastguard Worker /* add license string to blob of bytes */
947*f7c14bbaSAndroid Build Coastguard Worker license_off = add_data(gen, license, strlen(license) + 1);
948*f7c14bbaSAndroid Build Coastguard Worker /* add insns to blob of bytes */
949*f7c14bbaSAndroid Build Coastguard Worker insns_off = add_data(gen, insns, insn_cnt * sizeof(struct bpf_insn));
950*f7c14bbaSAndroid Build Coastguard Worker
951*f7c14bbaSAndroid Build Coastguard Worker attr.prog_type = prog_type;
952*f7c14bbaSAndroid Build Coastguard Worker attr.expected_attach_type = load_attr->expected_attach_type;
953*f7c14bbaSAndroid Build Coastguard Worker attr.attach_btf_id = load_attr->attach_btf_id;
954*f7c14bbaSAndroid Build Coastguard Worker attr.prog_ifindex = load_attr->prog_ifindex;
955*f7c14bbaSAndroid Build Coastguard Worker attr.kern_version = 0;
956*f7c14bbaSAndroid Build Coastguard Worker attr.insn_cnt = (__u32)insn_cnt;
957*f7c14bbaSAndroid Build Coastguard Worker attr.prog_flags = load_attr->prog_flags;
958*f7c14bbaSAndroid Build Coastguard Worker
959*f7c14bbaSAndroid Build Coastguard Worker attr.func_info_rec_size = load_attr->func_info_rec_size;
960*f7c14bbaSAndroid Build Coastguard Worker attr.func_info_cnt = load_attr->func_info_cnt;
961*f7c14bbaSAndroid Build Coastguard Worker func_info = add_data(gen, load_attr->func_info,
962*f7c14bbaSAndroid Build Coastguard Worker attr.func_info_cnt * attr.func_info_rec_size);
963*f7c14bbaSAndroid Build Coastguard Worker
964*f7c14bbaSAndroid Build Coastguard Worker attr.line_info_rec_size = load_attr->line_info_rec_size;
965*f7c14bbaSAndroid Build Coastguard Worker attr.line_info_cnt = load_attr->line_info_cnt;
966*f7c14bbaSAndroid Build Coastguard Worker line_info = add_data(gen, load_attr->line_info,
967*f7c14bbaSAndroid Build Coastguard Worker attr.line_info_cnt * attr.line_info_rec_size);
968*f7c14bbaSAndroid Build Coastguard Worker
969*f7c14bbaSAndroid Build Coastguard Worker attr.core_relo_rec_size = sizeof(struct bpf_core_relo);
970*f7c14bbaSAndroid Build Coastguard Worker attr.core_relo_cnt = gen->core_relo_cnt;
971*f7c14bbaSAndroid Build Coastguard Worker core_relos = add_data(gen, gen->core_relos,
972*f7c14bbaSAndroid Build Coastguard Worker attr.core_relo_cnt * attr.core_relo_rec_size);
973*f7c14bbaSAndroid Build Coastguard Worker
974*f7c14bbaSAndroid Build Coastguard Worker libbpf_strlcpy(attr.prog_name, prog_name, sizeof(attr.prog_name));
975*f7c14bbaSAndroid Build Coastguard Worker prog_load_attr = add_data(gen, &attr, attr_size);
976*f7c14bbaSAndroid Build Coastguard Worker
977*f7c14bbaSAndroid Build Coastguard Worker /* populate union bpf_attr with a pointer to license */
978*f7c14bbaSAndroid Build Coastguard Worker emit_rel_store(gen, attr_field(prog_load_attr, license), license_off);
979*f7c14bbaSAndroid Build Coastguard Worker
980*f7c14bbaSAndroid Build Coastguard Worker /* populate union bpf_attr with a pointer to instructions */
981*f7c14bbaSAndroid Build Coastguard Worker emit_rel_store(gen, attr_field(prog_load_attr, insns), insns_off);
982*f7c14bbaSAndroid Build Coastguard Worker
983*f7c14bbaSAndroid Build Coastguard Worker /* populate union bpf_attr with a pointer to func_info */
984*f7c14bbaSAndroid Build Coastguard Worker emit_rel_store(gen, attr_field(prog_load_attr, func_info), func_info);
985*f7c14bbaSAndroid Build Coastguard Worker
986*f7c14bbaSAndroid Build Coastguard Worker /* populate union bpf_attr with a pointer to line_info */
987*f7c14bbaSAndroid Build Coastguard Worker emit_rel_store(gen, attr_field(prog_load_attr, line_info), line_info);
988*f7c14bbaSAndroid Build Coastguard Worker
989*f7c14bbaSAndroid Build Coastguard Worker /* populate union bpf_attr with a pointer to core_relos */
990*f7c14bbaSAndroid Build Coastguard Worker emit_rel_store(gen, attr_field(prog_load_attr, core_relos), core_relos);
991*f7c14bbaSAndroid Build Coastguard Worker
992*f7c14bbaSAndroid Build Coastguard Worker /* populate union bpf_attr fd_array with a pointer to data where map_fds are saved */
993*f7c14bbaSAndroid Build Coastguard Worker emit_rel_store(gen, attr_field(prog_load_attr, fd_array), gen->fd_array);
994*f7c14bbaSAndroid Build Coastguard Worker
995*f7c14bbaSAndroid Build Coastguard Worker /* populate union bpf_attr with user provided log details */
996*f7c14bbaSAndroid Build Coastguard Worker move_ctx2blob(gen, attr_field(prog_load_attr, log_level), 4,
997*f7c14bbaSAndroid Build Coastguard Worker offsetof(struct bpf_loader_ctx, log_level), false);
998*f7c14bbaSAndroid Build Coastguard Worker move_ctx2blob(gen, attr_field(prog_load_attr, log_size), 4,
999*f7c14bbaSAndroid Build Coastguard Worker offsetof(struct bpf_loader_ctx, log_size), false);
1000*f7c14bbaSAndroid Build Coastguard Worker move_ctx2blob(gen, attr_field(prog_load_attr, log_buf), 8,
1001*f7c14bbaSAndroid Build Coastguard Worker offsetof(struct bpf_loader_ctx, log_buf), false);
1002*f7c14bbaSAndroid Build Coastguard Worker /* populate union bpf_attr with btf_fd saved in the stack earlier */
1003*f7c14bbaSAndroid Build Coastguard Worker move_stack2blob(gen, attr_field(prog_load_attr, prog_btf_fd), 4,
1004*f7c14bbaSAndroid Build Coastguard Worker stack_off(btf_fd));
1005*f7c14bbaSAndroid Build Coastguard Worker if (gen->attach_kind) {
1006*f7c14bbaSAndroid Build Coastguard Worker emit_find_attach_target(gen);
1007*f7c14bbaSAndroid Build Coastguard Worker /* populate union bpf_attr with btf_id and btf_obj_fd found by helper */
1008*f7c14bbaSAndroid Build Coastguard Worker emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_0, BPF_PSEUDO_MAP_IDX_VALUE,
1009*f7c14bbaSAndroid Build Coastguard Worker 0, 0, 0, prog_load_attr));
1010*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_0, BPF_REG_7,
1011*f7c14bbaSAndroid Build Coastguard Worker offsetof(union bpf_attr, attach_btf_id)));
1012*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_ALU64_IMM(BPF_RSH, BPF_REG_7, 32));
1013*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_0, BPF_REG_7,
1014*f7c14bbaSAndroid Build Coastguard Worker offsetof(union bpf_attr, attach_btf_obj_fd)));
1015*f7c14bbaSAndroid Build Coastguard Worker }
1016*f7c14bbaSAndroid Build Coastguard Worker emit_relos(gen, insns_off);
1017*f7c14bbaSAndroid Build Coastguard Worker /* emit PROG_LOAD command */
1018*f7c14bbaSAndroid Build Coastguard Worker emit_sys_bpf(gen, BPF_PROG_LOAD, prog_load_attr, attr_size);
1019*f7c14bbaSAndroid Build Coastguard Worker debug_ret(gen, "prog_load %s insn_cnt %d", attr.prog_name, attr.insn_cnt);
1020*f7c14bbaSAndroid Build Coastguard Worker /* successful or not, close btf module FDs used in extern ksyms and attach_btf_obj_fd */
1021*f7c14bbaSAndroid Build Coastguard Worker cleanup_relos(gen, insns_off);
1022*f7c14bbaSAndroid Build Coastguard Worker if (gen->attach_kind) {
1023*f7c14bbaSAndroid Build Coastguard Worker emit_sys_close_blob(gen,
1024*f7c14bbaSAndroid Build Coastguard Worker attr_field(prog_load_attr, attach_btf_obj_fd));
1025*f7c14bbaSAndroid Build Coastguard Worker gen->attach_kind = 0;
1026*f7c14bbaSAndroid Build Coastguard Worker }
1027*f7c14bbaSAndroid Build Coastguard Worker emit_check_err(gen);
1028*f7c14bbaSAndroid Build Coastguard Worker /* remember prog_fd in the stack, if successful */
1029*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_7,
1030*f7c14bbaSAndroid Build Coastguard Worker stack_off(prog_fd[gen->nr_progs])));
1031*f7c14bbaSAndroid Build Coastguard Worker gen->nr_progs++;
1032*f7c14bbaSAndroid Build Coastguard Worker }
1033*f7c14bbaSAndroid Build Coastguard Worker
bpf_gen__map_update_elem(struct bpf_gen * gen,int map_idx,void * pvalue,__u32 value_size)1034*f7c14bbaSAndroid Build Coastguard Worker void bpf_gen__map_update_elem(struct bpf_gen *gen, int map_idx, void *pvalue,
1035*f7c14bbaSAndroid Build Coastguard Worker __u32 value_size)
1036*f7c14bbaSAndroid Build Coastguard Worker {
1037*f7c14bbaSAndroid Build Coastguard Worker int attr_size = offsetofend(union bpf_attr, flags);
1038*f7c14bbaSAndroid Build Coastguard Worker int map_update_attr, value, key;
1039*f7c14bbaSAndroid Build Coastguard Worker union bpf_attr attr;
1040*f7c14bbaSAndroid Build Coastguard Worker int zero = 0;
1041*f7c14bbaSAndroid Build Coastguard Worker
1042*f7c14bbaSAndroid Build Coastguard Worker memset(&attr, 0, attr_size);
1043*f7c14bbaSAndroid Build Coastguard Worker pr_debug("gen: map_update_elem: idx %d\n", map_idx);
1044*f7c14bbaSAndroid Build Coastguard Worker
1045*f7c14bbaSAndroid Build Coastguard Worker value = add_data(gen, pvalue, value_size);
1046*f7c14bbaSAndroid Build Coastguard Worker key = add_data(gen, &zero, sizeof(zero));
1047*f7c14bbaSAndroid Build Coastguard Worker
1048*f7c14bbaSAndroid Build Coastguard Worker /* if (map_desc[map_idx].initial_value) {
1049*f7c14bbaSAndroid Build Coastguard Worker * if (ctx->flags & BPF_SKEL_KERNEL)
1050*f7c14bbaSAndroid Build Coastguard Worker * bpf_probe_read_kernel(value, value_size, initial_value);
1051*f7c14bbaSAndroid Build Coastguard Worker * else
1052*f7c14bbaSAndroid Build Coastguard Worker * bpf_copy_from_user(value, value_size, initial_value);
1053*f7c14bbaSAndroid Build Coastguard Worker * }
1054*f7c14bbaSAndroid Build Coastguard Worker */
1055*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_6,
1056*f7c14bbaSAndroid Build Coastguard Worker sizeof(struct bpf_loader_ctx) +
1057*f7c14bbaSAndroid Build Coastguard Worker sizeof(struct bpf_map_desc) * map_idx +
1058*f7c14bbaSAndroid Build Coastguard Worker offsetof(struct bpf_map_desc, initial_value)));
1059*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_JMP_IMM(BPF_JEQ, BPF_REG_3, 0, 8));
1060*f7c14bbaSAndroid Build Coastguard Worker emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE,
1061*f7c14bbaSAndroid Build Coastguard Worker 0, 0, 0, value));
1062*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_MOV64_IMM(BPF_REG_2, value_size));
1063*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_6,
1064*f7c14bbaSAndroid Build Coastguard Worker offsetof(struct bpf_loader_ctx, flags)));
1065*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_JMP_IMM(BPF_JSET, BPF_REG_0, BPF_SKEL_KERNEL, 2));
1066*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_EMIT_CALL(BPF_FUNC_copy_from_user));
1067*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_JMP_IMM(BPF_JA, 0, 0, 1));
1068*f7c14bbaSAndroid Build Coastguard Worker emit(gen, BPF_EMIT_CALL(BPF_FUNC_probe_read_kernel));
1069*f7c14bbaSAndroid Build Coastguard Worker
1070*f7c14bbaSAndroid Build Coastguard Worker map_update_attr = add_data(gen, &attr, attr_size);
1071*f7c14bbaSAndroid Build Coastguard Worker move_blob2blob(gen, attr_field(map_update_attr, map_fd), 4,
1072*f7c14bbaSAndroid Build Coastguard Worker blob_fd_array_off(gen, map_idx));
1073*f7c14bbaSAndroid Build Coastguard Worker emit_rel_store(gen, attr_field(map_update_attr, key), key);
1074*f7c14bbaSAndroid Build Coastguard Worker emit_rel_store(gen, attr_field(map_update_attr, value), value);
1075*f7c14bbaSAndroid Build Coastguard Worker /* emit MAP_UPDATE_ELEM command */
1076*f7c14bbaSAndroid Build Coastguard Worker emit_sys_bpf(gen, BPF_MAP_UPDATE_ELEM, map_update_attr, attr_size);
1077*f7c14bbaSAndroid Build Coastguard Worker debug_ret(gen, "update_elem idx %d value_size %d", map_idx, value_size);
1078*f7c14bbaSAndroid Build Coastguard Worker emit_check_err(gen);
1079*f7c14bbaSAndroid Build Coastguard Worker }
1080*f7c14bbaSAndroid Build Coastguard Worker
bpf_gen__populate_outer_map(struct bpf_gen * gen,int outer_map_idx,int slot,int inner_map_idx)1081*f7c14bbaSAndroid Build Coastguard Worker void bpf_gen__populate_outer_map(struct bpf_gen *gen, int outer_map_idx, int slot,
1082*f7c14bbaSAndroid Build Coastguard Worker int inner_map_idx)
1083*f7c14bbaSAndroid Build Coastguard Worker {
1084*f7c14bbaSAndroid Build Coastguard Worker int attr_size = offsetofend(union bpf_attr, flags);
1085*f7c14bbaSAndroid Build Coastguard Worker int map_update_attr, key;
1086*f7c14bbaSAndroid Build Coastguard Worker union bpf_attr attr;
1087*f7c14bbaSAndroid Build Coastguard Worker
1088*f7c14bbaSAndroid Build Coastguard Worker memset(&attr, 0, attr_size);
1089*f7c14bbaSAndroid Build Coastguard Worker pr_debug("gen: populate_outer_map: outer %d key %d inner %d\n",
1090*f7c14bbaSAndroid Build Coastguard Worker outer_map_idx, slot, inner_map_idx);
1091*f7c14bbaSAndroid Build Coastguard Worker
1092*f7c14bbaSAndroid Build Coastguard Worker key = add_data(gen, &slot, sizeof(slot));
1093*f7c14bbaSAndroid Build Coastguard Worker
1094*f7c14bbaSAndroid Build Coastguard Worker map_update_attr = add_data(gen, &attr, attr_size);
1095*f7c14bbaSAndroid Build Coastguard Worker move_blob2blob(gen, attr_field(map_update_attr, map_fd), 4,
1096*f7c14bbaSAndroid Build Coastguard Worker blob_fd_array_off(gen, outer_map_idx));
1097*f7c14bbaSAndroid Build Coastguard Worker emit_rel_store(gen, attr_field(map_update_attr, key), key);
1098*f7c14bbaSAndroid Build Coastguard Worker emit_rel_store(gen, attr_field(map_update_attr, value),
1099*f7c14bbaSAndroid Build Coastguard Worker blob_fd_array_off(gen, inner_map_idx));
1100*f7c14bbaSAndroid Build Coastguard Worker
1101*f7c14bbaSAndroid Build Coastguard Worker /* emit MAP_UPDATE_ELEM command */
1102*f7c14bbaSAndroid Build Coastguard Worker emit_sys_bpf(gen, BPF_MAP_UPDATE_ELEM, map_update_attr, attr_size);
1103*f7c14bbaSAndroid Build Coastguard Worker debug_ret(gen, "populate_outer_map outer %d key %d inner %d",
1104*f7c14bbaSAndroid Build Coastguard Worker outer_map_idx, slot, inner_map_idx);
1105*f7c14bbaSAndroid Build Coastguard Worker emit_check_err(gen);
1106*f7c14bbaSAndroid Build Coastguard Worker }
1107*f7c14bbaSAndroid Build Coastguard Worker
bpf_gen__map_freeze(struct bpf_gen * gen,int map_idx)1108*f7c14bbaSAndroid Build Coastguard Worker void bpf_gen__map_freeze(struct bpf_gen *gen, int map_idx)
1109*f7c14bbaSAndroid Build Coastguard Worker {
1110*f7c14bbaSAndroid Build Coastguard Worker int attr_size = offsetofend(union bpf_attr, map_fd);
1111*f7c14bbaSAndroid Build Coastguard Worker int map_freeze_attr;
1112*f7c14bbaSAndroid Build Coastguard Worker union bpf_attr attr;
1113*f7c14bbaSAndroid Build Coastguard Worker
1114*f7c14bbaSAndroid Build Coastguard Worker memset(&attr, 0, attr_size);
1115*f7c14bbaSAndroid Build Coastguard Worker pr_debug("gen: map_freeze: idx %d\n", map_idx);
1116*f7c14bbaSAndroid Build Coastguard Worker map_freeze_attr = add_data(gen, &attr, attr_size);
1117*f7c14bbaSAndroid Build Coastguard Worker move_blob2blob(gen, attr_field(map_freeze_attr, map_fd), 4,
1118*f7c14bbaSAndroid Build Coastguard Worker blob_fd_array_off(gen, map_idx));
1119*f7c14bbaSAndroid Build Coastguard Worker /* emit MAP_FREEZE command */
1120*f7c14bbaSAndroid Build Coastguard Worker emit_sys_bpf(gen, BPF_MAP_FREEZE, map_freeze_attr, attr_size);
1121*f7c14bbaSAndroid Build Coastguard Worker debug_ret(gen, "map_freeze");
1122*f7c14bbaSAndroid Build Coastguard Worker emit_check_err(gen);
1123*f7c14bbaSAndroid Build Coastguard Worker }
1124