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 601 for (int i = 0; i < gen->nr_ksyms; i++) { 602 if (!strcmp(gen->ksyms[i].name, relo->name)) { 603 gen->ksyms[i].ref++; 604 return &gen->ksyms[i]; 605 } 606 } 607 kdesc = libbpf_reallocarray(gen->ksyms, gen->nr_ksyms + 1, sizeof(*kdesc)); 608 if (!kdesc) { 609 gen->error = -ENOMEM; 610 return NULL; 611 } 612 gen->ksyms = kdesc; 613 kdesc = &gen->ksyms[gen->nr_ksyms++]; 614 kdesc->name = relo->name; 615 kdesc->kind = relo->kind; 616 kdesc->ref = 1; 617 kdesc->off = 0; 618 kdesc->insn = 0; 619 return kdesc; 620 } 621 622 /* Overwrites BPF_REG_{0, 1, 2, 3, 4, 7} 623 * Returns result in BPF_REG_7 624 */ 625 static void emit_bpf_find_by_name_kind(struct bpf_gen *gen, struct ksym_relo_desc *relo) 626 { 627 int name_off, len = strlen(relo->name) + 1; 628 629 name_off = add_data(gen, relo->name, len); 630 emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE, 631 0, 0, 0, name_off)); 632 emit(gen, BPF_MOV64_IMM(BPF_REG_2, len)); 633 emit(gen, BPF_MOV64_IMM(BPF_REG_3, relo->kind)); 634 emit(gen, BPF_MOV64_IMM(BPF_REG_4, 0)); 635 emit(gen, BPF_EMIT_CALL(BPF_FUNC_btf_find_by_name_kind)); 636 emit(gen, BPF_MOV64_REG(BPF_REG_7, BPF_REG_0)); 637 debug_ret(gen, "find_by_name_kind(%s,%d)", relo->name, relo->kind); 638 } 639 640 /* Overwrites BPF_REG_{0, 1, 2, 3, 4, 7} 641 * Returns result in BPF_REG_7 642 * Returns u64 symbol addr in BPF_REG_9 643 */ 644 static void emit_bpf_kallsyms_lookup_name(struct bpf_gen *gen, struct ksym_relo_desc *relo) 645 { 646 int name_off, len = strlen(relo->name) + 1, res_off; 647 648 name_off = add_data(gen, relo->name, len); 649 res_off = add_data(gen, NULL, 8); /* res is u64 */ 650 emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE, 651 0, 0, 0, name_off)); 652 emit(gen, BPF_MOV64_IMM(BPF_REG_2, len)); 653 emit(gen, BPF_MOV64_IMM(BPF_REG_3, 0)); 654 emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_4, BPF_PSEUDO_MAP_IDX_VALUE, 655 0, 0, 0, res_off)); 656 emit(gen, BPF_MOV64_REG(BPF_REG_7, BPF_REG_4)); 657 emit(gen, BPF_EMIT_CALL(BPF_FUNC_kallsyms_lookup_name)); 658 emit(gen, BPF_LDX_MEM(BPF_DW, BPF_REG_9, BPF_REG_7, 0)); 659 emit(gen, BPF_MOV64_REG(BPF_REG_7, BPF_REG_0)); 660 debug_ret(gen, "kallsyms_lookup_name(%s,%d)", relo->name, relo->kind); 661 } 662 663 /* Expects: 664 * BPF_REG_8 - pointer to instruction 665 * 666 * We need to reuse BTF fd for same symbol otherwise each relocation takes a new 667 * index, while kernel limits total kfunc BTFs to 256. For duplicate symbols, 668 * this would mean a new BTF fd index for each entry. By pairing symbol name 669 * with index, we get the insn->imm, insn->off pairing that kernel uses for 670 * kfunc_tab, which becomes the effective limit even though all of them may 671 * share same index in fd_array (such that kfunc_btf_tab has 1 element). 672 */ 673 static void emit_relo_kfunc_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo, int insn) 674 { 675 struct ksym_desc *kdesc; 676 int btf_fd_idx; 677 678 kdesc = get_ksym_desc(gen, relo); 679 if (!kdesc) 680 return; 681 /* try to copy from existing bpf_insn */ 682 if (kdesc->ref > 1) { 683 move_blob2blob(gen, insn + offsetof(struct bpf_insn, imm), 4, 684 kdesc->insn + offsetof(struct bpf_insn, imm)); 685 move_blob2blob(gen, insn + offsetof(struct bpf_insn, off), 2, 686 kdesc->insn + offsetof(struct bpf_insn, off)); 687 goto log; 688 } 689 /* remember insn offset, so we can copy BTF ID and FD later */ 690 kdesc->insn = insn; 691 emit_bpf_find_by_name_kind(gen, relo); 692 if (!relo->is_weak) 693 emit_check_err(gen); 694 /* get index in fd_array to store BTF FD at */ 695 btf_fd_idx = add_kfunc_btf_fd(gen); 696 if (btf_fd_idx > INT16_MAX) { 697 pr_warn("BTF fd off %d for kfunc %s exceeds INT16_MAX, cannot process relocation\n", 698 btf_fd_idx, relo->name); 699 gen->error = -E2BIG; 700 return; 701 } 702 kdesc->off = btf_fd_idx; 703 /* set a default value for imm */ 704 emit(gen, BPF_ST_MEM(BPF_W, BPF_REG_8, offsetof(struct bpf_insn, imm), 0)); 705 /* skip success case store if ret < 0 */ 706 emit(gen, BPF_JMP_IMM(BPF_JSLT, BPF_REG_7, 0, 1)); 707 /* store btf_id into insn[insn_idx].imm */ 708 emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_8, BPF_REG_7, offsetof(struct bpf_insn, imm))); 709 /* load fd_array slot pointer */ 710 emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_0, BPF_PSEUDO_MAP_IDX_VALUE, 711 0, 0, 0, blob_fd_array_off(gen, btf_fd_idx))); 712 /* skip store of BTF fd if ret < 0 */ 713 emit(gen, BPF_JMP_IMM(BPF_JSLT, BPF_REG_7, 0, 3)); 714 /* store BTF fd in slot */ 715 emit(gen, BPF_MOV64_REG(BPF_REG_9, BPF_REG_7)); 716 emit(gen, BPF_ALU64_IMM(BPF_RSH, BPF_REG_9, 32)); 717 emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_0, BPF_REG_9, 0)); 718 /* set a default value for off */ 719 emit(gen, BPF_ST_MEM(BPF_H, BPF_REG_8, offsetof(struct bpf_insn, off), 0)); 720 /* skip insn->off store if ret < 0 */ 721 emit(gen, BPF_JMP_IMM(BPF_JSLT, BPF_REG_7, 0, 2)); 722 /* skip if vmlinux BTF */ 723 emit(gen, BPF_JMP_IMM(BPF_JEQ, BPF_REG_9, 0, 1)); 724 /* store index into insn[insn_idx].off */ 725 emit(gen, BPF_ST_MEM(BPF_H, BPF_REG_8, offsetof(struct bpf_insn, off), btf_fd_idx)); 726 log: 727 if (!gen->log_level) 728 return; 729 emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_8, 730 offsetof(struct bpf_insn, imm))); 731 emit(gen, BPF_LDX_MEM(BPF_H, BPF_REG_9, BPF_REG_8, 732 offsetof(struct bpf_insn, off))); 733 debug_regs(gen, BPF_REG_7, BPF_REG_9, " func (%s:count=%d): imm: %%d, off: %%d", 734 relo->name, kdesc->ref); 735 emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_0, BPF_PSEUDO_MAP_IDX_VALUE, 736 0, 0, 0, blob_fd_array_off(gen, kdesc->off))); 737 emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_9, BPF_REG_0, 0)); 738 debug_regs(gen, BPF_REG_9, -1, " func (%s:count=%d): btf_fd", 739 relo->name, kdesc->ref); 740 } 741 742 static void emit_ksym_relo_log(struct bpf_gen *gen, struct ksym_relo_desc *relo, 743 int ref) 744 { 745 if (!gen->log_level) 746 return; 747 emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_8, 748 offsetof(struct bpf_insn, imm))); 749 emit(gen, BPF_LDX_MEM(BPF_H, BPF_REG_9, BPF_REG_8, sizeof(struct bpf_insn) + 750 offsetof(struct bpf_insn, imm))); 751 debug_regs(gen, BPF_REG_7, BPF_REG_9, " var t=%d w=%d (%s:count=%d): imm[0]: %%d, imm[1]: %%d", 752 relo->is_typeless, relo->is_weak, relo->name, ref); 753 emit(gen, BPF_LDX_MEM(BPF_B, BPF_REG_9, BPF_REG_8, offsetofend(struct bpf_insn, code))); 754 debug_regs(gen, BPF_REG_9, -1, " var t=%d w=%d (%s:count=%d): insn.reg", 755 relo->is_typeless, relo->is_weak, relo->name, ref); 756 } 757 758 /* Expects: 759 * BPF_REG_8 - pointer to instruction 760 */ 761 static void emit_relo_ksym_typeless(struct bpf_gen *gen, 762 struct ksym_relo_desc *relo, int insn) 763 { 764 struct ksym_desc *kdesc; 765 766 kdesc = get_ksym_desc(gen, relo); 767 if (!kdesc) 768 return; 769 /* try to copy from existing ldimm64 insn */ 770 if (kdesc->ref > 1) { 771 move_blob2blob(gen, insn + offsetof(struct bpf_insn, imm), 4, 772 kdesc->insn + offsetof(struct bpf_insn, imm)); 773 move_blob2blob(gen, insn + sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm), 4, 774 kdesc->insn + sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm)); 775 goto log; 776 } 777 /* remember insn offset, so we can copy ksym addr later */ 778 kdesc->insn = insn; 779 /* skip typeless ksym_desc in fd closing loop in cleanup_relos */ 780 kdesc->typeless = true; 781 emit_bpf_kallsyms_lookup_name(gen, relo); 782 emit(gen, BPF_JMP_IMM(BPF_JEQ, BPF_REG_7, -ENOENT, 1)); 783 emit_check_err(gen); 784 /* store lower half of addr into insn[insn_idx].imm */ 785 emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_8, BPF_REG_9, offsetof(struct bpf_insn, imm))); 786 /* store upper half of addr into insn[insn_idx + 1].imm */ 787 emit(gen, BPF_ALU64_IMM(BPF_RSH, BPF_REG_9, 32)); 788 emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_8, BPF_REG_9, 789 sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm))); 790 log: 791 emit_ksym_relo_log(gen, relo, kdesc->ref); 792 } 793 794 static __u32 src_reg_mask(void) 795 { 796 #if defined(__LITTLE_ENDIAN_BITFIELD) 797 return 0x0f; /* src_reg,dst_reg,... */ 798 #elif defined(__BIG_ENDIAN_BITFIELD) 799 return 0xf0; /* dst_reg,src_reg,... */ 800 #else 801 #error "Unsupported bit endianness, cannot proceed" 802 #endif 803 } 804 805 /* Expects: 806 * BPF_REG_8 - pointer to instruction 807 */ 808 static void emit_relo_ksym_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo, int insn) 809 { 810 struct ksym_desc *kdesc; 811 __u32 reg_mask; 812 813 kdesc = get_ksym_desc(gen, relo); 814 if (!kdesc) 815 return; 816 /* try to copy from existing ldimm64 insn */ 817 if (kdesc->ref > 1) { 818 move_blob2blob(gen, insn + offsetof(struct bpf_insn, imm), 4, 819 kdesc->insn + offsetof(struct bpf_insn, imm)); 820 move_blob2blob(gen, insn + sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm), 4, 821 kdesc->insn + sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm)); 822 emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_9, BPF_REG_8, offsetof(struct bpf_insn, imm))); 823 /* jump over src_reg adjustment if imm is not 0 */ 824 emit(gen, BPF_JMP_IMM(BPF_JNE, BPF_REG_9, 0, 3)); 825 goto clear_src_reg; 826 } 827 /* remember insn offset, so we can copy BTF ID and FD later */ 828 kdesc->insn = insn; 829 emit_bpf_find_by_name_kind(gen, relo); 830 if (!relo->is_weak) 831 emit_check_err(gen); 832 /* set default values as 0 */ 833 emit(gen, BPF_ST_MEM(BPF_W, BPF_REG_8, offsetof(struct bpf_insn, imm), 0)); 834 emit(gen, BPF_ST_MEM(BPF_W, BPF_REG_8, sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm), 0)); 835 /* skip success case stores if ret < 0 */ 836 emit(gen, BPF_JMP_IMM(BPF_JSLT, BPF_REG_7, 0, 4)); 837 /* store btf_id into insn[insn_idx].imm */ 838 emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_8, BPF_REG_7, offsetof(struct bpf_insn, imm))); 839 /* store btf_obj_fd into insn[insn_idx + 1].imm */ 840 emit(gen, BPF_ALU64_IMM(BPF_RSH, BPF_REG_7, 32)); 841 emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_8, BPF_REG_7, 842 sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm))); 843 emit(gen, BPF_JMP_IMM(BPF_JSGE, BPF_REG_7, 0, 3)); 844 clear_src_reg: 845 /* clear bpf_object__relocate_data's src_reg assignment, otherwise we get a verifier failure */ 846 reg_mask = src_reg_mask(); 847 emit(gen, BPF_LDX_MEM(BPF_B, BPF_REG_9, BPF_REG_8, offsetofend(struct bpf_insn, code))); 848 emit(gen, BPF_ALU32_IMM(BPF_AND, BPF_REG_9, reg_mask)); 849 emit(gen, BPF_STX_MEM(BPF_B, BPF_REG_8, BPF_REG_9, offsetofend(struct bpf_insn, code))); 850 851 emit_ksym_relo_log(gen, relo, kdesc->ref); 852 } 853 854 static void emit_relo(struct bpf_gen *gen, struct ksym_relo_desc *relo, int insns) 855 { 856 int insn; 857 858 pr_debug("gen: emit_relo (%d): %s at %d\n", relo->kind, relo->name, relo->insn_idx); 859 insn = insns + sizeof(struct bpf_insn) * relo->insn_idx; 860 emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_8, BPF_PSEUDO_MAP_IDX_VALUE, 0, 0, 0, insn)); 861 switch (relo->kind) { 862 case BTF_KIND_VAR: 863 if (relo->is_typeless) 864 emit_relo_ksym_typeless(gen, relo, insn); 865 else 866 emit_relo_ksym_btf(gen, relo, insn); 867 break; 868 case BTF_KIND_FUNC: 869 emit_relo_kfunc_btf(gen, relo, insn); 870 break; 871 default: 872 pr_warn("Unknown relocation kind '%d'\n", relo->kind); 873 gen->error = -EDOM; 874 return; 875 } 876 } 877 878 static void emit_relos(struct bpf_gen *gen, int insns) 879 { 880 int i; 881 882 for (i = 0; i < gen->relo_cnt; i++) 883 emit_relo(gen, gen->relos + i, insns); 884 } 885 886 static void cleanup_relos(struct bpf_gen *gen, int insns) 887 { 888 int i, insn; 889 890 for (i = 0; i < gen->nr_ksyms; i++) { 891 /* only close fds for typed ksyms and kfuncs */ 892 if (gen->ksyms[i].kind == BTF_KIND_VAR && !gen->ksyms[i].typeless) { 893 /* close fd recorded in insn[insn_idx + 1].imm */ 894 insn = gen->ksyms[i].insn; 895 insn += sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm); 896 emit_sys_close_blob(gen, insn); 897 } else if (gen->ksyms[i].kind == BTF_KIND_FUNC) { 898 emit_sys_close_blob(gen, blob_fd_array_off(gen, gen->ksyms[i].off)); 899 if (gen->ksyms[i].off < MAX_FD_ARRAY_SZ) 900 gen->nr_fd_array--; 901 } 902 } 903 if (gen->nr_ksyms) { 904 free(gen->ksyms); 905 gen->nr_ksyms = 0; 906 gen->ksyms = NULL; 907 } 908 if (gen->relo_cnt) { 909 free(gen->relos); 910 gen->relo_cnt = 0; 911 gen->relos = NULL; 912 } 913 } 914 915 void bpf_gen__prog_load(struct bpf_gen *gen, 916 struct bpf_prog_load_params *load_attr, int prog_idx) 917 { 918 int attr_size = offsetofend(union bpf_attr, fd_array); 919 int prog_load_attr, license, insns, func_info, line_info; 920 union bpf_attr attr; 921 922 memset(&attr, 0, attr_size); 923 pr_debug("gen: prog_load: type %d insns_cnt %zd\n", 924 load_attr->prog_type, load_attr->insn_cnt); 925 /* add license string to blob of bytes */ 926 license = add_data(gen, load_attr->license, strlen(load_attr->license) + 1); 927 /* add insns to blob of bytes */ 928 insns = add_data(gen, load_attr->insns, 929 load_attr->insn_cnt * sizeof(struct bpf_insn)); 930 931 attr.prog_type = load_attr->prog_type; 932 attr.expected_attach_type = load_attr->expected_attach_type; 933 attr.attach_btf_id = load_attr->attach_btf_id; 934 attr.prog_ifindex = load_attr->prog_ifindex; 935 attr.kern_version = 0; 936 attr.insn_cnt = (__u32)load_attr->insn_cnt; 937 attr.prog_flags = load_attr->prog_flags; 938 939 attr.func_info_rec_size = load_attr->func_info_rec_size; 940 attr.func_info_cnt = load_attr->func_info_cnt; 941 func_info = add_data(gen, load_attr->func_info, 942 attr.func_info_cnt * attr.func_info_rec_size); 943 944 attr.line_info_rec_size = load_attr->line_info_rec_size; 945 attr.line_info_cnt = load_attr->line_info_cnt; 946 line_info = add_data(gen, load_attr->line_info, 947 attr.line_info_cnt * attr.line_info_rec_size); 948 949 memcpy(attr.prog_name, load_attr->name, 950 min((unsigned)strlen(load_attr->name), BPF_OBJ_NAME_LEN - 1)); 951 prog_load_attr = add_data(gen, &attr, attr_size); 952 953 /* populate union bpf_attr with a pointer to license */ 954 emit_rel_store(gen, attr_field(prog_load_attr, license), license); 955 956 /* populate union bpf_attr with a pointer to instructions */ 957 emit_rel_store(gen, attr_field(prog_load_attr, insns), insns); 958 959 /* populate union bpf_attr with a pointer to func_info */ 960 emit_rel_store(gen, attr_field(prog_load_attr, func_info), func_info); 961 962 /* populate union bpf_attr with a pointer to line_info */ 963 emit_rel_store(gen, attr_field(prog_load_attr, line_info), line_info); 964 965 /* populate union bpf_attr fd_array with a pointer to data where map_fds are saved */ 966 emit_rel_store(gen, attr_field(prog_load_attr, fd_array), gen->fd_array); 967 968 /* populate union bpf_attr with user provided log details */ 969 move_ctx2blob(gen, attr_field(prog_load_attr, log_level), 4, 970 offsetof(struct bpf_loader_ctx, log_level), false); 971 move_ctx2blob(gen, attr_field(prog_load_attr, log_size), 4, 972 offsetof(struct bpf_loader_ctx, log_size), false); 973 move_ctx2blob(gen, attr_field(prog_load_attr, log_buf), 8, 974 offsetof(struct bpf_loader_ctx, log_buf), false); 975 /* populate union bpf_attr with btf_fd saved in the stack earlier */ 976 move_stack2blob(gen, attr_field(prog_load_attr, prog_btf_fd), 4, 977 stack_off(btf_fd)); 978 if (gen->attach_kind) { 979 emit_find_attach_target(gen); 980 /* populate union bpf_attr with btf_id and btf_obj_fd found by helper */ 981 emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_0, BPF_PSEUDO_MAP_IDX_VALUE, 982 0, 0, 0, prog_load_attr)); 983 emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_0, BPF_REG_7, 984 offsetof(union bpf_attr, attach_btf_id))); 985 emit(gen, BPF_ALU64_IMM(BPF_RSH, BPF_REG_7, 32)); 986 emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_0, BPF_REG_7, 987 offsetof(union bpf_attr, attach_btf_obj_fd))); 988 } 989 emit_relos(gen, insns); 990 /* emit PROG_LOAD command */ 991 emit_sys_bpf(gen, BPF_PROG_LOAD, prog_load_attr, attr_size); 992 debug_ret(gen, "prog_load %s insn_cnt %d", attr.prog_name, attr.insn_cnt); 993 /* successful or not, close btf module FDs used in extern ksyms and attach_btf_obj_fd */ 994 cleanup_relos(gen, insns); 995 if (gen->attach_kind) 996 emit_sys_close_blob(gen, 997 attr_field(prog_load_attr, attach_btf_obj_fd)); 998 emit_check_err(gen); 999 /* remember prog_fd in the stack, if successful */ 1000 emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_7, 1001 stack_off(prog_fd[gen->nr_progs]))); 1002 gen->nr_progs++; 1003 } 1004 1005 void bpf_gen__map_update_elem(struct bpf_gen *gen, int map_idx, void *pvalue, 1006 __u32 value_size) 1007 { 1008 int attr_size = offsetofend(union bpf_attr, flags); 1009 int map_update_attr, value, key; 1010 union bpf_attr attr; 1011 int zero = 0; 1012 1013 memset(&attr, 0, attr_size); 1014 pr_debug("gen: map_update_elem: idx %d\n", map_idx); 1015 1016 value = add_data(gen, pvalue, value_size); 1017 key = add_data(gen, &zero, sizeof(zero)); 1018 1019 /* if (map_desc[map_idx].initial_value) 1020 * copy_from_user(value, initial_value, value_size); 1021 */ 1022 emit(gen, BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_6, 1023 sizeof(struct bpf_loader_ctx) + 1024 sizeof(struct bpf_map_desc) * map_idx + 1025 offsetof(struct bpf_map_desc, initial_value))); 1026 emit(gen, BPF_JMP_IMM(BPF_JEQ, BPF_REG_3, 0, 4)); 1027 emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE, 1028 0, 0, 0, value)); 1029 emit(gen, BPF_MOV64_IMM(BPF_REG_2, value_size)); 1030 emit(gen, BPF_EMIT_CALL(BPF_FUNC_copy_from_user)); 1031 1032 map_update_attr = add_data(gen, &attr, attr_size); 1033 move_blob2blob(gen, attr_field(map_update_attr, map_fd), 4, 1034 blob_fd_array_off(gen, map_idx)); 1035 emit_rel_store(gen, attr_field(map_update_attr, key), key); 1036 emit_rel_store(gen, attr_field(map_update_attr, value), value); 1037 /* emit MAP_UPDATE_ELEM command */ 1038 emit_sys_bpf(gen, BPF_MAP_UPDATE_ELEM, map_update_attr, attr_size); 1039 debug_ret(gen, "update_elem idx %d value_size %d", map_idx, value_size); 1040 emit_check_err(gen); 1041 } 1042 1043 void bpf_gen__map_freeze(struct bpf_gen *gen, int map_idx) 1044 { 1045 int attr_size = offsetofend(union bpf_attr, map_fd); 1046 int map_freeze_attr; 1047 union bpf_attr attr; 1048 1049 memset(&attr, 0, attr_size); 1050 pr_debug("gen: map_freeze: idx %d\n", map_idx); 1051 map_freeze_attr = add_data(gen, &attr, attr_size); 1052 move_blob2blob(gen, attr_field(map_freeze_attr, map_fd), 4, 1053 blob_fd_array_off(gen, map_idx)); 1054 /* emit MAP_FREEZE command */ 1055 emit_sys_bpf(gen, BPF_MAP_FREEZE, map_freeze_attr, attr_size); 1056 debug_ret(gen, "map_freeze"); 1057 emit_check_err(gen); 1058 } 1059