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