1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * BPF JIT compiler for ARM64 4 * 5 * Copyright (C) 2014-2016 Zi Shen Lim <zlim.lnx@gmail.com> 6 */ 7 8 #define pr_fmt(fmt) "bpf_jit: " fmt 9 10 #include <linux/arm-smccc.h> 11 #include <linux/bitfield.h> 12 #include <linux/bpf.h> 13 #include <linux/cfi.h> 14 #include <linux/filter.h> 15 #include <linux/memory.h> 16 #include <linux/printk.h> 17 #include <linux/slab.h> 18 19 #include <asm/asm-extable.h> 20 #include <asm/byteorder.h> 21 #include <asm/cacheflush.h> 22 #include <asm/cpufeature.h> 23 #include <asm/debug-monitors.h> 24 #include <asm/insn.h> 25 #include <asm/text-patching.h> 26 #include <asm/set_memory.h> 27 28 #include "bpf_jit.h" 29 30 #define TMP_REG_1 (MAX_BPF_JIT_REG + 0) 31 #define TMP_REG_2 (MAX_BPF_JIT_REG + 1) 32 #define TCCNT_PTR (MAX_BPF_JIT_REG + 2) 33 #define TMP_REG_3 (MAX_BPF_JIT_REG + 3) 34 #define PRIVATE_SP (MAX_BPF_JIT_REG + 4) 35 #define ARENA_VM_START (MAX_BPF_JIT_REG + 5) 36 37 #define check_imm(bits, imm) do { \ 38 if ((((imm) > 0) && ((imm) >> (bits))) || \ 39 (((imm) < 0) && (~(imm) >> (bits)))) { \ 40 pr_info("[%2d] imm=%d(0x%x) out of range\n", \ 41 i, imm, imm); \ 42 return -EINVAL; \ 43 } \ 44 } while (0) 45 #define check_imm19(imm) check_imm(19, imm) 46 #define check_imm26(imm) check_imm(26, imm) 47 48 /* Map BPF registers to A64 registers */ 49 static const int bpf2a64[] = { 50 /* return value from in-kernel function, and exit value from eBPF */ 51 [BPF_REG_0] = A64_R(7), 52 /* arguments from eBPF program to in-kernel function */ 53 [BPF_REG_1] = A64_R(0), 54 [BPF_REG_2] = A64_R(1), 55 [BPF_REG_3] = A64_R(2), 56 [BPF_REG_4] = A64_R(3), 57 [BPF_REG_5] = A64_R(4), 58 /* callee saved registers that in-kernel function will preserve */ 59 [BPF_REG_6] = A64_R(19), 60 [BPF_REG_7] = A64_R(20), 61 [BPF_REG_8] = A64_R(21), 62 [BPF_REG_9] = A64_R(22), 63 /* read-only frame pointer to access stack */ 64 [BPF_REG_FP] = A64_R(25), 65 /* temporary registers for BPF JIT */ 66 [TMP_REG_1] = A64_R(10), 67 [TMP_REG_2] = A64_R(11), 68 [TMP_REG_3] = A64_R(12), 69 /* tail_call_cnt_ptr */ 70 [TCCNT_PTR] = A64_R(26), 71 /* temporary register for blinding constants */ 72 [BPF_REG_AX] = A64_R(9), 73 /* callee saved register for private stack pointer */ 74 [PRIVATE_SP] = A64_R(27), 75 /* callee saved register for kern_vm_start address */ 76 [ARENA_VM_START] = A64_R(28), 77 }; 78 79 struct jit_ctx { 80 const struct bpf_prog *prog; 81 int idx; 82 int epilogue_offset; 83 int *offset; 84 int exentry_idx; 85 int nr_used_callee_reg; 86 u8 used_callee_reg[8]; /* r6~r9, fp, arena_vm_start */ 87 __le32 *image; 88 __le32 *ro_image; 89 u32 stack_size; 90 u64 user_vm_start; 91 u64 arena_vm_start; 92 bool fp_used; 93 bool priv_sp_used; 94 bool write; 95 }; 96 97 struct bpf_plt { 98 u32 insn_ldr; /* load target */ 99 u32 insn_br; /* branch to target */ 100 u64 target; /* target value */ 101 }; 102 103 #define PLT_TARGET_SIZE sizeof_field(struct bpf_plt, target) 104 #define PLT_TARGET_OFFSET offsetof(struct bpf_plt, target) 105 106 /* Memory size/value to protect private stack overflow/underflow */ 107 #define PRIV_STACK_GUARD_SZ 16 108 #define PRIV_STACK_GUARD_VAL 0xEB9F12345678eb9fULL 109 110 static inline void emit(const u32 insn, struct jit_ctx *ctx) 111 { 112 if (ctx->image != NULL && ctx->write) 113 ctx->image[ctx->idx] = cpu_to_le32(insn); 114 115 ctx->idx++; 116 } 117 118 static inline void emit_u32_data(const u32 data, struct jit_ctx *ctx) 119 { 120 if (ctx->image != NULL && ctx->write) 121 ctx->image[ctx->idx] = data; 122 123 ctx->idx++; 124 } 125 126 static inline void emit_a64_mov_i(const int is64, const int reg, 127 const s32 val, struct jit_ctx *ctx) 128 { 129 u16 hi = val >> 16; 130 u16 lo = val & 0xffff; 131 132 if (hi & 0x8000) { 133 if (hi == 0xffff) { 134 emit(A64_MOVN(is64, reg, (u16)~lo, 0), ctx); 135 } else { 136 emit(A64_MOVN(is64, reg, (u16)~hi, 16), ctx); 137 if (lo != 0xffff) 138 emit(A64_MOVK(is64, reg, lo, 0), ctx); 139 } 140 } else { 141 emit(A64_MOVZ(is64, reg, lo, 0), ctx); 142 if (hi) 143 emit(A64_MOVK(is64, reg, hi, 16), ctx); 144 } 145 } 146 147 static int i64_i16_blocks(const u64 val, bool inverse) 148 { 149 return (((val >> 0) & 0xffff) != (inverse ? 0xffff : 0x0000)) + 150 (((val >> 16) & 0xffff) != (inverse ? 0xffff : 0x0000)) + 151 (((val >> 32) & 0xffff) != (inverse ? 0xffff : 0x0000)) + 152 (((val >> 48) & 0xffff) != (inverse ? 0xffff : 0x0000)); 153 } 154 155 static inline void emit_a64_mov_i64(const int reg, const u64 val, 156 struct jit_ctx *ctx) 157 { 158 u64 nrm_tmp = val, rev_tmp = ~val; 159 bool inverse; 160 int shift; 161 162 if (!(nrm_tmp >> 32)) 163 return emit_a64_mov_i(0, reg, (u32)val, ctx); 164 165 inverse = i64_i16_blocks(nrm_tmp, true) < i64_i16_blocks(nrm_tmp, false); 166 shift = max(round_down((inverse ? (fls64(rev_tmp) - 1) : 167 (fls64(nrm_tmp) - 1)), 16), 0); 168 if (inverse) 169 emit(A64_MOVN(1, reg, (rev_tmp >> shift) & 0xffff, shift), ctx); 170 else 171 emit(A64_MOVZ(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx); 172 shift -= 16; 173 while (shift >= 0) { 174 if (((nrm_tmp >> shift) & 0xffff) != (inverse ? 0xffff : 0x0000)) 175 emit(A64_MOVK(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx); 176 shift -= 16; 177 } 178 } 179 180 static inline void emit_bti(u32 insn, struct jit_ctx *ctx) 181 { 182 if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL)) 183 emit(insn, ctx); 184 } 185 186 static inline void emit_kcfi(u32 hash, struct jit_ctx *ctx) 187 { 188 if (IS_ENABLED(CONFIG_CFI_CLANG)) 189 emit_u32_data(hash, ctx); 190 } 191 192 /* 193 * Kernel addresses in the vmalloc space use at most 48 bits, and the 194 * remaining bits are guaranteed to be 0x1. So we can compose the address 195 * with a fixed length movn/movk/movk sequence. 196 */ 197 static inline void emit_addr_mov_i64(const int reg, const u64 val, 198 struct jit_ctx *ctx) 199 { 200 u64 tmp = val; 201 int shift = 0; 202 203 emit(A64_MOVN(1, reg, ~tmp & 0xffff, shift), ctx); 204 while (shift < 32) { 205 tmp >>= 16; 206 shift += 16; 207 emit(A64_MOVK(1, reg, tmp & 0xffff, shift), ctx); 208 } 209 } 210 211 static bool should_emit_indirect_call(long target, const struct jit_ctx *ctx) 212 { 213 long offset; 214 215 /* when ctx->ro_image is not allocated or the target is unknown, 216 * emit indirect call 217 */ 218 if (!ctx->ro_image || !target) 219 return true; 220 221 offset = target - (long)&ctx->ro_image[ctx->idx]; 222 return offset < -SZ_128M || offset >= SZ_128M; 223 } 224 225 static void emit_direct_call(u64 target, struct jit_ctx *ctx) 226 { 227 u32 insn; 228 unsigned long pc; 229 230 pc = (unsigned long)&ctx->ro_image[ctx->idx]; 231 insn = aarch64_insn_gen_branch_imm(pc, target, AARCH64_INSN_BRANCH_LINK); 232 emit(insn, ctx); 233 } 234 235 static void emit_indirect_call(u64 target, struct jit_ctx *ctx) 236 { 237 u8 tmp; 238 239 tmp = bpf2a64[TMP_REG_1]; 240 emit_addr_mov_i64(tmp, target, ctx); 241 emit(A64_BLR(tmp), ctx); 242 } 243 244 static void emit_call(u64 target, struct jit_ctx *ctx) 245 { 246 if (should_emit_indirect_call((long)target, ctx)) 247 emit_indirect_call(target, ctx); 248 else 249 emit_direct_call(target, ctx); 250 } 251 252 static inline int bpf2a64_offset(int bpf_insn, int off, 253 const struct jit_ctx *ctx) 254 { 255 /* BPF JMP offset is relative to the next instruction */ 256 bpf_insn++; 257 /* 258 * Whereas arm64 branch instructions encode the offset 259 * from the branch itself, so we must subtract 1 from the 260 * instruction offset. 261 */ 262 return ctx->offset[bpf_insn + off] - (ctx->offset[bpf_insn] - 1); 263 } 264 265 static void jit_fill_hole(void *area, unsigned int size) 266 { 267 __le32 *ptr; 268 /* We are guaranteed to have aligned memory. */ 269 for (ptr = area; size >= sizeof(u32); size -= sizeof(u32)) 270 *ptr++ = cpu_to_le32(AARCH64_BREAK_FAULT); 271 } 272 273 int bpf_arch_text_invalidate(void *dst, size_t len) 274 { 275 if (!aarch64_insn_set(dst, AARCH64_BREAK_FAULT, len)) 276 return -EINVAL; 277 278 return 0; 279 } 280 281 static inline int epilogue_offset(const struct jit_ctx *ctx) 282 { 283 int to = ctx->epilogue_offset; 284 int from = ctx->idx; 285 286 return to - from; 287 } 288 289 static bool is_addsub_imm(u32 imm) 290 { 291 /* Either imm12 or shifted imm12. */ 292 return !(imm & ~0xfff) || !(imm & ~0xfff000); 293 } 294 295 static inline void emit_a64_add_i(const bool is64, const int dst, const int src, 296 const int tmp, const s32 imm, struct jit_ctx *ctx) 297 { 298 if (is_addsub_imm(imm)) { 299 emit(A64_ADD_I(is64, dst, src, imm), ctx); 300 } else if (is_addsub_imm(-(u32)imm)) { 301 emit(A64_SUB_I(is64, dst, src, -imm), ctx); 302 } else { 303 emit_a64_mov_i(is64, tmp, imm, ctx); 304 emit(A64_ADD(is64, dst, src, tmp), ctx); 305 } 306 } 307 308 /* 309 * There are 3 types of AArch64 LDR/STR (immediate) instruction: 310 * Post-index, Pre-index, Unsigned offset. 311 * 312 * For BPF ldr/str, the "unsigned offset" type is sufficient. 313 * 314 * "Unsigned offset" type LDR(immediate) format: 315 * 316 * 3 2 1 0 317 * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 318 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 319 * |x x|1 1 1 0 0 1 0 1| imm12 | Rn | Rt | 320 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 321 * scale 322 * 323 * "Unsigned offset" type STR(immediate) format: 324 * 3 2 1 0 325 * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 326 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 327 * |x x|1 1 1 0 0 1 0 0| imm12 | Rn | Rt | 328 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 329 * scale 330 * 331 * The offset is calculated from imm12 and scale in the following way: 332 * 333 * offset = (u64)imm12 << scale 334 */ 335 static bool is_lsi_offset(int offset, int scale) 336 { 337 if (offset < 0) 338 return false; 339 340 if (offset > (0xFFF << scale)) 341 return false; 342 343 if (offset & ((1 << scale) - 1)) 344 return false; 345 346 return true; 347 } 348 349 /* generated main prog prologue: 350 * bti c // if CONFIG_ARM64_BTI_KERNEL 351 * mov x9, lr 352 * nop // POKE_OFFSET 353 * paciasp // if CONFIG_ARM64_PTR_AUTH_KERNEL 354 * stp x29, lr, [sp, #-16]! 355 * mov x29, sp 356 * stp xzr, x26, [sp, #-16]! 357 * mov x26, sp 358 * // PROLOGUE_OFFSET 359 * // save callee-saved registers 360 */ 361 static void prepare_bpf_tail_call_cnt(struct jit_ctx *ctx) 362 { 363 const bool is_main_prog = !bpf_is_subprog(ctx->prog); 364 const u8 ptr = bpf2a64[TCCNT_PTR]; 365 366 if (is_main_prog) { 367 /* Initialize tail_call_cnt. */ 368 emit(A64_PUSH(A64_ZR, ptr, A64_SP), ctx); 369 emit(A64_MOV(1, ptr, A64_SP), ctx); 370 } else 371 emit(A64_PUSH(ptr, ptr, A64_SP), ctx); 372 } 373 374 static void find_used_callee_regs(struct jit_ctx *ctx) 375 { 376 int i; 377 const struct bpf_prog *prog = ctx->prog; 378 const struct bpf_insn *insn = &prog->insnsi[0]; 379 int reg_used = 0; 380 381 for (i = 0; i < prog->len; i++, insn++) { 382 if (insn->dst_reg == BPF_REG_6 || insn->src_reg == BPF_REG_6) 383 reg_used |= 1; 384 385 if (insn->dst_reg == BPF_REG_7 || insn->src_reg == BPF_REG_7) 386 reg_used |= 2; 387 388 if (insn->dst_reg == BPF_REG_8 || insn->src_reg == BPF_REG_8) 389 reg_used |= 4; 390 391 if (insn->dst_reg == BPF_REG_9 || insn->src_reg == BPF_REG_9) 392 reg_used |= 8; 393 394 if (insn->dst_reg == BPF_REG_FP || insn->src_reg == BPF_REG_FP) { 395 ctx->fp_used = true; 396 reg_used |= 16; 397 } 398 } 399 400 i = 0; 401 if (reg_used & 1) 402 ctx->used_callee_reg[i++] = bpf2a64[BPF_REG_6]; 403 404 if (reg_used & 2) 405 ctx->used_callee_reg[i++] = bpf2a64[BPF_REG_7]; 406 407 if (reg_used & 4) 408 ctx->used_callee_reg[i++] = bpf2a64[BPF_REG_8]; 409 410 if (reg_used & 8) 411 ctx->used_callee_reg[i++] = bpf2a64[BPF_REG_9]; 412 413 if (reg_used & 16) { 414 ctx->used_callee_reg[i++] = bpf2a64[BPF_REG_FP]; 415 if (ctx->priv_sp_used) 416 ctx->used_callee_reg[i++] = bpf2a64[PRIVATE_SP]; 417 } 418 419 if (ctx->arena_vm_start) 420 ctx->used_callee_reg[i++] = bpf2a64[ARENA_VM_START]; 421 422 ctx->nr_used_callee_reg = i; 423 } 424 425 /* Save callee-saved registers */ 426 static void push_callee_regs(struct jit_ctx *ctx) 427 { 428 int reg1, reg2, i; 429 430 /* 431 * Program acting as exception boundary should save all ARM64 432 * Callee-saved registers as the exception callback needs to recover 433 * all ARM64 Callee-saved registers in its epilogue. 434 */ 435 if (ctx->prog->aux->exception_boundary) { 436 emit(A64_PUSH(A64_R(19), A64_R(20), A64_SP), ctx); 437 emit(A64_PUSH(A64_R(21), A64_R(22), A64_SP), ctx); 438 emit(A64_PUSH(A64_R(23), A64_R(24), A64_SP), ctx); 439 emit(A64_PUSH(A64_R(25), A64_R(26), A64_SP), ctx); 440 emit(A64_PUSH(A64_R(27), A64_R(28), A64_SP), ctx); 441 ctx->fp_used = true; 442 } else { 443 find_used_callee_regs(ctx); 444 for (i = 0; i + 1 < ctx->nr_used_callee_reg; i += 2) { 445 reg1 = ctx->used_callee_reg[i]; 446 reg2 = ctx->used_callee_reg[i + 1]; 447 emit(A64_PUSH(reg1, reg2, A64_SP), ctx); 448 } 449 if (i < ctx->nr_used_callee_reg) { 450 reg1 = ctx->used_callee_reg[i]; 451 /* keep SP 16-byte aligned */ 452 emit(A64_PUSH(reg1, A64_ZR, A64_SP), ctx); 453 } 454 } 455 } 456 457 /* Restore callee-saved registers */ 458 static void pop_callee_regs(struct jit_ctx *ctx) 459 { 460 struct bpf_prog_aux *aux = ctx->prog->aux; 461 int reg1, reg2, i; 462 463 /* 464 * Program acting as exception boundary pushes R23 and R24 in addition 465 * to BPF callee-saved registers. Exception callback uses the boundary 466 * program's stack frame, so recover these extra registers in the above 467 * two cases. 468 */ 469 if (aux->exception_boundary || aux->exception_cb) { 470 emit(A64_POP(A64_R(27), A64_R(28), A64_SP), ctx); 471 emit(A64_POP(A64_R(25), A64_R(26), A64_SP), ctx); 472 emit(A64_POP(A64_R(23), A64_R(24), A64_SP), ctx); 473 emit(A64_POP(A64_R(21), A64_R(22), A64_SP), ctx); 474 emit(A64_POP(A64_R(19), A64_R(20), A64_SP), ctx); 475 } else { 476 i = ctx->nr_used_callee_reg - 1; 477 if (ctx->nr_used_callee_reg % 2 != 0) { 478 reg1 = ctx->used_callee_reg[i]; 479 emit(A64_POP(reg1, A64_ZR, A64_SP), ctx); 480 i--; 481 } 482 while (i > 0) { 483 reg1 = ctx->used_callee_reg[i - 1]; 484 reg2 = ctx->used_callee_reg[i]; 485 emit(A64_POP(reg1, reg2, A64_SP), ctx); 486 i -= 2; 487 } 488 } 489 } 490 491 static void emit_percpu_ptr(const u8 dst_reg, void __percpu *ptr, 492 struct jit_ctx *ctx) 493 { 494 const u8 tmp = bpf2a64[TMP_REG_1]; 495 496 emit_a64_mov_i64(dst_reg, (__force const u64)ptr, ctx); 497 if (cpus_have_cap(ARM64_HAS_VIRT_HOST_EXTN)) 498 emit(A64_MRS_TPIDR_EL2(tmp), ctx); 499 else 500 emit(A64_MRS_TPIDR_EL1(tmp), ctx); 501 emit(A64_ADD(1, dst_reg, dst_reg, tmp), ctx); 502 } 503 504 #define BTI_INSNS (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL) ? 1 : 0) 505 #define PAC_INSNS (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) ? 1 : 0) 506 507 /* Offset of nop instruction in bpf prog entry to be poked */ 508 #define POKE_OFFSET (BTI_INSNS + 1) 509 510 /* Tail call offset to jump into */ 511 #define PROLOGUE_OFFSET (BTI_INSNS + 2 + PAC_INSNS + 4) 512 513 static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf) 514 { 515 const struct bpf_prog *prog = ctx->prog; 516 const bool is_main_prog = !bpf_is_subprog(prog); 517 const u8 fp = bpf2a64[BPF_REG_FP]; 518 const u8 arena_vm_base = bpf2a64[ARENA_VM_START]; 519 const u8 priv_sp = bpf2a64[PRIVATE_SP]; 520 void __percpu *priv_stack_ptr; 521 int cur_offset; 522 523 /* 524 * BPF prog stack layout 525 * 526 * high 527 * original A64_SP => 0:+-----+ BPF prologue 528 * |FP/LR| 529 * current A64_FP => -16:+-----+ 530 * | ... | callee saved registers 531 * BPF fp register => -64:+-----+ <= (BPF_FP) 532 * | | 533 * | ... | BPF prog stack 534 * | | 535 * +-----+ <= (BPF_FP - prog->aux->stack_depth) 536 * |RSVD | padding 537 * current A64_SP => +-----+ <= (BPF_FP - ctx->stack_size) 538 * | | 539 * | ... | Function call stack 540 * | | 541 * +-----+ 542 * low 543 * 544 */ 545 546 emit_kcfi(is_main_prog ? cfi_bpf_hash : cfi_bpf_subprog_hash, ctx); 547 const int idx0 = ctx->idx; 548 549 /* bpf function may be invoked by 3 instruction types: 550 * 1. bl, attached via freplace to bpf prog via short jump 551 * 2. br, attached via freplace to bpf prog via long jump 552 * 3. blr, working as a function pointer, used by emit_call. 553 * So BTI_JC should used here to support both br and blr. 554 */ 555 emit_bti(A64_BTI_JC, ctx); 556 557 emit(A64_MOV(1, A64_R(9), A64_LR), ctx); 558 emit(A64_NOP, ctx); 559 560 if (!prog->aux->exception_cb) { 561 /* Sign lr */ 562 if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL)) 563 emit(A64_PACIASP, ctx); 564 565 /* Save FP and LR registers to stay align with ARM64 AAPCS */ 566 emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx); 567 emit(A64_MOV(1, A64_FP, A64_SP), ctx); 568 569 prepare_bpf_tail_call_cnt(ctx); 570 571 if (!ebpf_from_cbpf && is_main_prog) { 572 cur_offset = ctx->idx - idx0; 573 if (cur_offset != PROLOGUE_OFFSET) { 574 pr_err_once("PROLOGUE_OFFSET = %d, expected %d!\n", 575 cur_offset, PROLOGUE_OFFSET); 576 return -1; 577 } 578 /* BTI landing pad for the tail call, done with a BR */ 579 emit_bti(A64_BTI_J, ctx); 580 } 581 push_callee_regs(ctx); 582 } else { 583 /* 584 * Exception callback receives FP of Main Program as third 585 * parameter 586 */ 587 emit(A64_MOV(1, A64_FP, A64_R(2)), ctx); 588 /* 589 * Main Program already pushed the frame record and the 590 * callee-saved registers. The exception callback will not push 591 * anything and re-use the main program's stack. 592 * 593 * 12 registers are on the stack 594 */ 595 emit(A64_SUB_I(1, A64_SP, A64_FP, 96), ctx); 596 } 597 598 /* Stack must be multiples of 16B */ 599 ctx->stack_size = round_up(prog->aux->stack_depth, 16); 600 601 if (ctx->fp_used) { 602 if (ctx->priv_sp_used) { 603 /* Set up private stack pointer */ 604 priv_stack_ptr = prog->aux->priv_stack_ptr + PRIV_STACK_GUARD_SZ; 605 emit_percpu_ptr(priv_sp, priv_stack_ptr, ctx); 606 emit(A64_ADD_I(1, fp, priv_sp, ctx->stack_size), ctx); 607 } else { 608 /* Set up BPF prog stack base register */ 609 emit(A64_MOV(1, fp, A64_SP), ctx); 610 } 611 } 612 613 /* Set up function call stack */ 614 if (ctx->stack_size && !ctx->priv_sp_used) 615 emit(A64_SUB_I(1, A64_SP, A64_SP, ctx->stack_size), ctx); 616 617 if (ctx->arena_vm_start) 618 emit_a64_mov_i64(arena_vm_base, ctx->arena_vm_start, ctx); 619 620 return 0; 621 } 622 623 static int emit_bpf_tail_call(struct jit_ctx *ctx) 624 { 625 /* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */ 626 const u8 r2 = bpf2a64[BPF_REG_2]; 627 const u8 r3 = bpf2a64[BPF_REG_3]; 628 629 const u8 tmp = bpf2a64[TMP_REG_1]; 630 const u8 prg = bpf2a64[TMP_REG_2]; 631 const u8 tcc = bpf2a64[TMP_REG_3]; 632 const u8 ptr = bpf2a64[TCCNT_PTR]; 633 size_t off; 634 __le32 *branch1 = NULL; 635 __le32 *branch2 = NULL; 636 __le32 *branch3 = NULL; 637 638 /* if (index >= array->map.max_entries) 639 * goto out; 640 */ 641 off = offsetof(struct bpf_array, map.max_entries); 642 emit_a64_mov_i64(tmp, off, ctx); 643 emit(A64_LDR32(tmp, r2, tmp), ctx); 644 emit(A64_MOV(0, r3, r3), ctx); 645 emit(A64_CMP(0, r3, tmp), ctx); 646 branch1 = ctx->image + ctx->idx; 647 emit(A64_NOP, ctx); 648 649 /* 650 * if ((*tail_call_cnt_ptr) >= MAX_TAIL_CALL_CNT) 651 * goto out; 652 */ 653 emit_a64_mov_i64(tmp, MAX_TAIL_CALL_CNT, ctx); 654 emit(A64_LDR64I(tcc, ptr, 0), ctx); 655 emit(A64_CMP(1, tcc, tmp), ctx); 656 branch2 = ctx->image + ctx->idx; 657 emit(A64_NOP, ctx); 658 659 /* (*tail_call_cnt_ptr)++; */ 660 emit(A64_ADD_I(1, tcc, tcc, 1), ctx); 661 662 /* prog = array->ptrs[index]; 663 * if (prog == NULL) 664 * goto out; 665 */ 666 off = offsetof(struct bpf_array, ptrs); 667 emit_a64_mov_i64(tmp, off, ctx); 668 emit(A64_ADD(1, tmp, r2, tmp), ctx); 669 emit(A64_LSL(1, prg, r3, 3), ctx); 670 emit(A64_LDR64(prg, tmp, prg), ctx); 671 branch3 = ctx->image + ctx->idx; 672 emit(A64_NOP, ctx); 673 674 /* Update tail_call_cnt if the slot is populated. */ 675 emit(A64_STR64I(tcc, ptr, 0), ctx); 676 677 /* restore SP */ 678 if (ctx->stack_size && !ctx->priv_sp_used) 679 emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx); 680 681 pop_callee_regs(ctx); 682 683 /* goto *(prog->bpf_func + prologue_offset); */ 684 off = offsetof(struct bpf_prog, bpf_func); 685 emit_a64_mov_i64(tmp, off, ctx); 686 emit(A64_LDR64(tmp, prg, tmp), ctx); 687 emit(A64_ADD_I(1, tmp, tmp, sizeof(u32) * PROLOGUE_OFFSET), ctx); 688 emit(A64_BR(tmp), ctx); 689 690 if (ctx->image) { 691 off = &ctx->image[ctx->idx] - branch1; 692 *branch1 = cpu_to_le32(A64_B_(A64_COND_CS, off)); 693 694 off = &ctx->image[ctx->idx] - branch2; 695 *branch2 = cpu_to_le32(A64_B_(A64_COND_CS, off)); 696 697 off = &ctx->image[ctx->idx] - branch3; 698 *branch3 = cpu_to_le32(A64_CBZ(1, prg, off)); 699 } 700 701 return 0; 702 } 703 704 static int emit_atomic_ld_st(const struct bpf_insn *insn, struct jit_ctx *ctx) 705 { 706 const s32 imm = insn->imm; 707 const s16 off = insn->off; 708 const u8 code = insn->code; 709 const bool arena = BPF_MODE(code) == BPF_PROBE_ATOMIC; 710 const u8 arena_vm_base = bpf2a64[ARENA_VM_START]; 711 const u8 dst = bpf2a64[insn->dst_reg]; 712 const u8 src = bpf2a64[insn->src_reg]; 713 const u8 tmp = bpf2a64[TMP_REG_1]; 714 u8 reg; 715 716 switch (imm) { 717 case BPF_LOAD_ACQ: 718 reg = src; 719 break; 720 case BPF_STORE_REL: 721 reg = dst; 722 break; 723 default: 724 pr_err_once("unknown atomic load/store op code %02x\n", imm); 725 return -EINVAL; 726 } 727 728 if (off) { 729 emit_a64_add_i(1, tmp, reg, tmp, off, ctx); 730 reg = tmp; 731 } 732 if (arena) { 733 emit(A64_ADD(1, tmp, reg, arena_vm_base), ctx); 734 reg = tmp; 735 } 736 737 switch (imm) { 738 case BPF_LOAD_ACQ: 739 switch (BPF_SIZE(code)) { 740 case BPF_B: 741 emit(A64_LDARB(dst, reg), ctx); 742 break; 743 case BPF_H: 744 emit(A64_LDARH(dst, reg), ctx); 745 break; 746 case BPF_W: 747 emit(A64_LDAR32(dst, reg), ctx); 748 break; 749 case BPF_DW: 750 emit(A64_LDAR64(dst, reg), ctx); 751 break; 752 } 753 break; 754 case BPF_STORE_REL: 755 switch (BPF_SIZE(code)) { 756 case BPF_B: 757 emit(A64_STLRB(src, reg), ctx); 758 break; 759 case BPF_H: 760 emit(A64_STLRH(src, reg), ctx); 761 break; 762 case BPF_W: 763 emit(A64_STLR32(src, reg), ctx); 764 break; 765 case BPF_DW: 766 emit(A64_STLR64(src, reg), ctx); 767 break; 768 } 769 break; 770 default: 771 pr_err_once("unexpected atomic load/store op code %02x\n", 772 imm); 773 return -EINVAL; 774 } 775 776 return 0; 777 } 778 779 #ifdef CONFIG_ARM64_LSE_ATOMICS 780 static int emit_lse_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx) 781 { 782 const u8 code = insn->code; 783 const u8 arena_vm_base = bpf2a64[ARENA_VM_START]; 784 const u8 dst = bpf2a64[insn->dst_reg]; 785 const u8 src = bpf2a64[insn->src_reg]; 786 const u8 tmp = bpf2a64[TMP_REG_1]; 787 const u8 tmp2 = bpf2a64[TMP_REG_2]; 788 const bool isdw = BPF_SIZE(code) == BPF_DW; 789 const bool arena = BPF_MODE(code) == BPF_PROBE_ATOMIC; 790 const s16 off = insn->off; 791 u8 reg = dst; 792 793 if (off) { 794 emit_a64_add_i(1, tmp, reg, tmp, off, ctx); 795 reg = tmp; 796 } 797 if (arena) { 798 emit(A64_ADD(1, tmp, reg, arena_vm_base), ctx); 799 reg = tmp; 800 } 801 802 switch (insn->imm) { 803 /* lock *(u32/u64 *)(dst_reg + off) <op>= src_reg */ 804 case BPF_ADD: 805 emit(A64_STADD(isdw, reg, src), ctx); 806 break; 807 case BPF_AND: 808 emit(A64_MVN(isdw, tmp2, src), ctx); 809 emit(A64_STCLR(isdw, reg, tmp2), ctx); 810 break; 811 case BPF_OR: 812 emit(A64_STSET(isdw, reg, src), ctx); 813 break; 814 case BPF_XOR: 815 emit(A64_STEOR(isdw, reg, src), ctx); 816 break; 817 /* src_reg = atomic_fetch_<op>(dst_reg + off, src_reg) */ 818 case BPF_ADD | BPF_FETCH: 819 emit(A64_LDADDAL(isdw, src, reg, src), ctx); 820 break; 821 case BPF_AND | BPF_FETCH: 822 emit(A64_MVN(isdw, tmp2, src), ctx); 823 emit(A64_LDCLRAL(isdw, src, reg, tmp2), ctx); 824 break; 825 case BPF_OR | BPF_FETCH: 826 emit(A64_LDSETAL(isdw, src, reg, src), ctx); 827 break; 828 case BPF_XOR | BPF_FETCH: 829 emit(A64_LDEORAL(isdw, src, reg, src), ctx); 830 break; 831 /* src_reg = atomic_xchg(dst_reg + off, src_reg); */ 832 case BPF_XCHG: 833 emit(A64_SWPAL(isdw, src, reg, src), ctx); 834 break; 835 /* r0 = atomic_cmpxchg(dst_reg + off, r0, src_reg); */ 836 case BPF_CMPXCHG: 837 emit(A64_CASAL(isdw, src, reg, bpf2a64[BPF_REG_0]), ctx); 838 break; 839 default: 840 pr_err_once("unknown atomic op code %02x\n", insn->imm); 841 return -EINVAL; 842 } 843 844 return 0; 845 } 846 #else 847 static inline int emit_lse_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx) 848 { 849 return -EINVAL; 850 } 851 #endif 852 853 static int emit_ll_sc_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx) 854 { 855 const u8 code = insn->code; 856 const u8 dst = bpf2a64[insn->dst_reg]; 857 const u8 src = bpf2a64[insn->src_reg]; 858 const u8 tmp = bpf2a64[TMP_REG_1]; 859 const u8 tmp2 = bpf2a64[TMP_REG_2]; 860 const u8 tmp3 = bpf2a64[TMP_REG_3]; 861 const int i = insn - ctx->prog->insnsi; 862 const s32 imm = insn->imm; 863 const s16 off = insn->off; 864 const bool isdw = BPF_SIZE(code) == BPF_DW; 865 u8 reg = dst; 866 s32 jmp_offset; 867 868 if (BPF_MODE(code) == BPF_PROBE_ATOMIC) { 869 /* ll_sc based atomics don't support unsafe pointers yet. */ 870 pr_err_once("unknown atomic opcode %02x\n", code); 871 return -EINVAL; 872 } 873 874 if (off) { 875 emit_a64_add_i(1, tmp, reg, tmp, off, ctx); 876 reg = tmp; 877 } 878 879 if (imm == BPF_ADD || imm == BPF_AND || 880 imm == BPF_OR || imm == BPF_XOR) { 881 /* lock *(u32/u64 *)(dst_reg + off) <op>= src_reg */ 882 emit(A64_LDXR(isdw, tmp2, reg), ctx); 883 if (imm == BPF_ADD) 884 emit(A64_ADD(isdw, tmp2, tmp2, src), ctx); 885 else if (imm == BPF_AND) 886 emit(A64_AND(isdw, tmp2, tmp2, src), ctx); 887 else if (imm == BPF_OR) 888 emit(A64_ORR(isdw, tmp2, tmp2, src), ctx); 889 else 890 emit(A64_EOR(isdw, tmp2, tmp2, src), ctx); 891 emit(A64_STXR(isdw, tmp2, reg, tmp3), ctx); 892 jmp_offset = -3; 893 check_imm19(jmp_offset); 894 emit(A64_CBNZ(0, tmp3, jmp_offset), ctx); 895 } else if (imm == (BPF_ADD | BPF_FETCH) || 896 imm == (BPF_AND | BPF_FETCH) || 897 imm == (BPF_OR | BPF_FETCH) || 898 imm == (BPF_XOR | BPF_FETCH)) { 899 /* src_reg = atomic_fetch_<op>(dst_reg + off, src_reg) */ 900 const u8 ax = bpf2a64[BPF_REG_AX]; 901 902 emit(A64_MOV(isdw, ax, src), ctx); 903 emit(A64_LDXR(isdw, src, reg), ctx); 904 if (imm == (BPF_ADD | BPF_FETCH)) 905 emit(A64_ADD(isdw, tmp2, src, ax), ctx); 906 else if (imm == (BPF_AND | BPF_FETCH)) 907 emit(A64_AND(isdw, tmp2, src, ax), ctx); 908 else if (imm == (BPF_OR | BPF_FETCH)) 909 emit(A64_ORR(isdw, tmp2, src, ax), ctx); 910 else 911 emit(A64_EOR(isdw, tmp2, src, ax), ctx); 912 emit(A64_STLXR(isdw, tmp2, reg, tmp3), ctx); 913 jmp_offset = -3; 914 check_imm19(jmp_offset); 915 emit(A64_CBNZ(0, tmp3, jmp_offset), ctx); 916 emit(A64_DMB_ISH, ctx); 917 } else if (imm == BPF_XCHG) { 918 /* src_reg = atomic_xchg(dst_reg + off, src_reg); */ 919 emit(A64_MOV(isdw, tmp2, src), ctx); 920 emit(A64_LDXR(isdw, src, reg), ctx); 921 emit(A64_STLXR(isdw, tmp2, reg, tmp3), ctx); 922 jmp_offset = -2; 923 check_imm19(jmp_offset); 924 emit(A64_CBNZ(0, tmp3, jmp_offset), ctx); 925 emit(A64_DMB_ISH, ctx); 926 } else if (imm == BPF_CMPXCHG) { 927 /* r0 = atomic_cmpxchg(dst_reg + off, r0, src_reg); */ 928 const u8 r0 = bpf2a64[BPF_REG_0]; 929 930 emit(A64_MOV(isdw, tmp2, r0), ctx); 931 emit(A64_LDXR(isdw, r0, reg), ctx); 932 emit(A64_EOR(isdw, tmp3, r0, tmp2), ctx); 933 jmp_offset = 4; 934 check_imm19(jmp_offset); 935 emit(A64_CBNZ(isdw, tmp3, jmp_offset), ctx); 936 emit(A64_STLXR(isdw, src, reg, tmp3), ctx); 937 jmp_offset = -4; 938 check_imm19(jmp_offset); 939 emit(A64_CBNZ(0, tmp3, jmp_offset), ctx); 940 emit(A64_DMB_ISH, ctx); 941 } else { 942 pr_err_once("unknown atomic op code %02x\n", imm); 943 return -EINVAL; 944 } 945 946 return 0; 947 } 948 949 void dummy_tramp(void); 950 951 asm ( 952 " .pushsection .text, \"ax\", @progbits\n" 953 " .global dummy_tramp\n" 954 " .type dummy_tramp, %function\n" 955 "dummy_tramp:" 956 #if IS_ENABLED(CONFIG_ARM64_BTI_KERNEL) 957 " bti j\n" /* dummy_tramp is called via "br x10" */ 958 #endif 959 " mov x10, x30\n" 960 " mov x30, x9\n" 961 " ret x10\n" 962 " .size dummy_tramp, .-dummy_tramp\n" 963 " .popsection\n" 964 ); 965 966 /* build a plt initialized like this: 967 * 968 * plt: 969 * ldr tmp, target 970 * br tmp 971 * target: 972 * .quad dummy_tramp 973 * 974 * when a long jump trampoline is attached, target is filled with the 975 * trampoline address, and when the trampoline is removed, target is 976 * restored to dummy_tramp address. 977 */ 978 static void build_plt(struct jit_ctx *ctx) 979 { 980 const u8 tmp = bpf2a64[TMP_REG_1]; 981 struct bpf_plt *plt = NULL; 982 983 /* make sure target is 64-bit aligned */ 984 if ((ctx->idx + PLT_TARGET_OFFSET / AARCH64_INSN_SIZE) % 2) 985 emit(A64_NOP, ctx); 986 987 plt = (struct bpf_plt *)(ctx->image + ctx->idx); 988 /* plt is called via bl, no BTI needed here */ 989 emit(A64_LDR64LIT(tmp, 2 * AARCH64_INSN_SIZE), ctx); 990 emit(A64_BR(tmp), ctx); 991 992 if (ctx->image) 993 plt->target = (u64)&dummy_tramp; 994 } 995 996 /* Clobbers BPF registers 1-4, aka x0-x3 */ 997 static void __maybe_unused build_bhb_mitigation(struct jit_ctx *ctx) 998 { 999 const u8 r1 = bpf2a64[BPF_REG_1]; /* aka x0 */ 1000 u8 k = get_spectre_bhb_loop_value(); 1001 1002 if (!IS_ENABLED(CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY) || 1003 cpu_mitigations_off() || __nospectre_bhb || 1004 arm64_get_spectre_v2_state() == SPECTRE_VULNERABLE) 1005 return; 1006 1007 if (capable(CAP_SYS_ADMIN)) 1008 return; 1009 1010 if (supports_clearbhb(SCOPE_SYSTEM)) { 1011 emit(aarch64_insn_gen_hint(AARCH64_INSN_HINT_CLEARBHB), ctx); 1012 return; 1013 } 1014 1015 if (k) { 1016 emit_a64_mov_i64(r1, k, ctx); 1017 emit(A64_B(1), ctx); 1018 emit(A64_SUBS_I(true, r1, r1, 1), ctx); 1019 emit(A64_B_(A64_COND_NE, -2), ctx); 1020 emit(aarch64_insn_gen_dsb(AARCH64_INSN_MB_ISH), ctx); 1021 emit(aarch64_insn_get_isb_value(), ctx); 1022 } 1023 1024 if (is_spectre_bhb_fw_mitigated()) { 1025 emit(A64_ORR_I(false, r1, AARCH64_INSN_REG_ZR, 1026 ARM_SMCCC_ARCH_WORKAROUND_3), ctx); 1027 switch (arm_smccc_1_1_get_conduit()) { 1028 case SMCCC_CONDUIT_HVC: 1029 emit(aarch64_insn_get_hvc_value(), ctx); 1030 break; 1031 case SMCCC_CONDUIT_SMC: 1032 emit(aarch64_insn_get_smc_value(), ctx); 1033 break; 1034 default: 1035 pr_err_once("Firmware mitigation enabled with unknown conduit\n"); 1036 } 1037 } 1038 } 1039 1040 static void build_epilogue(struct jit_ctx *ctx, bool was_classic) 1041 { 1042 const u8 r0 = bpf2a64[BPF_REG_0]; 1043 const u8 ptr = bpf2a64[TCCNT_PTR]; 1044 1045 /* We're done with BPF stack */ 1046 if (ctx->stack_size && !ctx->priv_sp_used) 1047 emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx); 1048 1049 pop_callee_regs(ctx); 1050 1051 emit(A64_POP(A64_ZR, ptr, A64_SP), ctx); 1052 1053 if (was_classic) 1054 build_bhb_mitigation(ctx); 1055 1056 /* Restore FP/LR registers */ 1057 emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx); 1058 1059 /* Move the return value from bpf:r0 (aka x7) to x0 */ 1060 emit(A64_MOV(1, A64_R(0), r0), ctx); 1061 1062 /* Authenticate lr */ 1063 if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL)) 1064 emit(A64_AUTIASP, ctx); 1065 1066 emit(A64_RET(A64_LR), ctx); 1067 } 1068 1069 /* 1070 * Metadata encoding for exception handling in JITed code. 1071 * 1072 * Format of `fixup` field in `struct exception_table_entry`: 1073 * 1074 * Bit layout of `fixup` (32-bit): 1075 * 1076 * +-----------+--------+-----------+-----------+----------+ 1077 * | 31-27 | 26-22 | 21 | 20-16 | 15-0 | 1078 * | | | | | | 1079 * | FIXUP_REG | Unused | ARENA_ACC | ARENA_REG | OFFSET | 1080 * +-----------+--------+-----------+-----------+----------+ 1081 * 1082 * - OFFSET (16 bits): Offset used to compute address for Load/Store instruction. 1083 * - ARENA_REG (5 bits): Register that is used to calculate the address for load/store when 1084 * accessing the arena region. 1085 * - ARENA_ACCESS (1 bit): This bit is set when the faulting instruction accessed the arena region. 1086 * - FIXUP_REG (5 bits): Destination register for the load instruction (cleared on fault) or set to 1087 * DONT_CLEAR if it is a store instruction. 1088 */ 1089 1090 #define BPF_FIXUP_OFFSET_MASK GENMASK(15, 0) 1091 #define BPF_FIXUP_ARENA_REG_MASK GENMASK(20, 16) 1092 #define BPF_ARENA_ACCESS BIT(21) 1093 #define BPF_FIXUP_REG_MASK GENMASK(31, 27) 1094 #define DONT_CLEAR 5 /* Unused ARM64 register from BPF's POV */ 1095 1096 bool ex_handler_bpf(const struct exception_table_entry *ex, 1097 struct pt_regs *regs) 1098 { 1099 int dst_reg = FIELD_GET(BPF_FIXUP_REG_MASK, ex->fixup); 1100 s16 off = FIELD_GET(BPF_FIXUP_OFFSET_MASK, ex->fixup); 1101 int arena_reg = FIELD_GET(BPF_FIXUP_ARENA_REG_MASK, ex->fixup); 1102 bool is_arena = !!(ex->fixup & BPF_ARENA_ACCESS); 1103 bool is_write = (dst_reg == DONT_CLEAR); 1104 unsigned long addr; 1105 1106 if (is_arena) { 1107 addr = regs->regs[arena_reg] + off; 1108 bpf_prog_report_arena_violation(is_write, addr, regs->pc); 1109 } 1110 1111 if (dst_reg != DONT_CLEAR) 1112 regs->regs[dst_reg] = 0; 1113 /* Skip the faulting instruction */ 1114 regs->pc += AARCH64_INSN_SIZE; 1115 1116 return true; 1117 } 1118 1119 /* For accesses to BTF pointers, add an entry to the exception table */ 1120 static int add_exception_handler(const struct bpf_insn *insn, 1121 struct jit_ctx *ctx, 1122 int dst_reg) 1123 { 1124 off_t ins_offset; 1125 s16 off = insn->off; 1126 bool is_arena; 1127 int arena_reg; 1128 unsigned long pc; 1129 struct exception_table_entry *ex; 1130 1131 if (!ctx->image) 1132 /* First pass */ 1133 return 0; 1134 1135 if (BPF_MODE(insn->code) != BPF_PROBE_MEM && 1136 BPF_MODE(insn->code) != BPF_PROBE_MEMSX && 1137 BPF_MODE(insn->code) != BPF_PROBE_MEM32 && 1138 BPF_MODE(insn->code) != BPF_PROBE_ATOMIC) 1139 return 0; 1140 1141 is_arena = (BPF_MODE(insn->code) == BPF_PROBE_MEM32) || 1142 (BPF_MODE(insn->code) == BPF_PROBE_ATOMIC); 1143 1144 if (!ctx->prog->aux->extable || 1145 WARN_ON_ONCE(ctx->exentry_idx >= ctx->prog->aux->num_exentries)) 1146 return -EINVAL; 1147 1148 ex = &ctx->prog->aux->extable[ctx->exentry_idx]; 1149 pc = (unsigned long)&ctx->ro_image[ctx->idx - 1]; 1150 1151 /* 1152 * This is the relative offset of the instruction that may fault from 1153 * the exception table itself. This will be written to the exception 1154 * table and if this instruction faults, the destination register will 1155 * be set to '0' and the execution will jump to the next instruction. 1156 */ 1157 ins_offset = pc - (long)&ex->insn; 1158 if (WARN_ON_ONCE(ins_offset >= 0 || ins_offset < INT_MIN)) 1159 return -ERANGE; 1160 1161 /* 1162 * The offsets above have been calculated using the RO buffer but we 1163 * need to use the R/W buffer for writes. 1164 * switch ex to rw buffer for writing. 1165 */ 1166 ex = (void *)ctx->image + ((void *)ex - (void *)ctx->ro_image); 1167 1168 ex->insn = ins_offset; 1169 1170 if (BPF_CLASS(insn->code) != BPF_LDX) 1171 dst_reg = DONT_CLEAR; 1172 1173 ex->fixup = FIELD_PREP(BPF_FIXUP_REG_MASK, dst_reg); 1174 1175 if (is_arena) { 1176 ex->fixup |= BPF_ARENA_ACCESS; 1177 /* 1178 * insn->src_reg/dst_reg holds the address in the arena region with upper 32-bits 1179 * being zero because of a preceding addr_space_cast(r<n>, 0x0, 0x1) instruction. 1180 * This address is adjusted with the addition of arena_vm_start (see the 1181 * implementation of BPF_PROBE_MEM32 and BPF_PROBE_ATOMIC) before being used for the 1182 * memory access. Pass the reg holding the unmodified 32-bit address to 1183 * ex_handler_bpf. 1184 */ 1185 if (BPF_CLASS(insn->code) == BPF_LDX) 1186 arena_reg = bpf2a64[insn->src_reg]; 1187 else 1188 arena_reg = bpf2a64[insn->dst_reg]; 1189 1190 ex->fixup |= FIELD_PREP(BPF_FIXUP_OFFSET_MASK, off) | 1191 FIELD_PREP(BPF_FIXUP_ARENA_REG_MASK, arena_reg); 1192 } 1193 1194 ex->type = EX_TYPE_BPF; 1195 1196 ctx->exentry_idx++; 1197 return 0; 1198 } 1199 1200 /* JITs an eBPF instruction. 1201 * Returns: 1202 * 0 - successfully JITed an 8-byte eBPF instruction. 1203 * >0 - successfully JITed a 16-byte eBPF instruction. 1204 * <0 - failed to JIT. 1205 */ 1206 static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, 1207 bool extra_pass) 1208 { 1209 const u8 code = insn->code; 1210 u8 dst = bpf2a64[insn->dst_reg]; 1211 u8 src = bpf2a64[insn->src_reg]; 1212 const u8 tmp = bpf2a64[TMP_REG_1]; 1213 const u8 tmp2 = bpf2a64[TMP_REG_2]; 1214 const u8 fp = bpf2a64[BPF_REG_FP]; 1215 const u8 arena_vm_base = bpf2a64[ARENA_VM_START]; 1216 const u8 priv_sp = bpf2a64[PRIVATE_SP]; 1217 const s16 off = insn->off; 1218 const s32 imm = insn->imm; 1219 const int i = insn - ctx->prog->insnsi; 1220 const bool is64 = BPF_CLASS(code) == BPF_ALU64 || 1221 BPF_CLASS(code) == BPF_JMP; 1222 u8 jmp_cond; 1223 s32 jmp_offset; 1224 u32 a64_insn; 1225 u8 src_adj; 1226 u8 dst_adj; 1227 int off_adj; 1228 int ret; 1229 bool sign_extend; 1230 1231 switch (code) { 1232 /* dst = src */ 1233 case BPF_ALU | BPF_MOV | BPF_X: 1234 case BPF_ALU64 | BPF_MOV | BPF_X: 1235 if (insn_is_cast_user(insn)) { 1236 emit(A64_MOV(0, tmp, src), ctx); // 32-bit mov clears the upper 32 bits 1237 emit_a64_mov_i(0, dst, ctx->user_vm_start >> 32, ctx); 1238 emit(A64_LSL(1, dst, dst, 32), ctx); 1239 emit(A64_CBZ(1, tmp, 2), ctx); 1240 emit(A64_ORR(1, tmp, dst, tmp), ctx); 1241 emit(A64_MOV(1, dst, tmp), ctx); 1242 break; 1243 } else if (insn_is_mov_percpu_addr(insn)) { 1244 if (dst != src) 1245 emit(A64_MOV(1, dst, src), ctx); 1246 if (cpus_have_cap(ARM64_HAS_VIRT_HOST_EXTN)) 1247 emit(A64_MRS_TPIDR_EL2(tmp), ctx); 1248 else 1249 emit(A64_MRS_TPIDR_EL1(tmp), ctx); 1250 emit(A64_ADD(1, dst, dst, tmp), ctx); 1251 break; 1252 } 1253 switch (insn->off) { 1254 case 0: 1255 emit(A64_MOV(is64, dst, src), ctx); 1256 break; 1257 case 8: 1258 emit(A64_SXTB(is64, dst, src), ctx); 1259 break; 1260 case 16: 1261 emit(A64_SXTH(is64, dst, src), ctx); 1262 break; 1263 case 32: 1264 emit(A64_SXTW(is64, dst, src), ctx); 1265 break; 1266 } 1267 break; 1268 /* dst = dst OP src */ 1269 case BPF_ALU | BPF_ADD | BPF_X: 1270 case BPF_ALU64 | BPF_ADD | BPF_X: 1271 emit(A64_ADD(is64, dst, dst, src), ctx); 1272 break; 1273 case BPF_ALU | BPF_SUB | BPF_X: 1274 case BPF_ALU64 | BPF_SUB | BPF_X: 1275 emit(A64_SUB(is64, dst, dst, src), ctx); 1276 break; 1277 case BPF_ALU | BPF_AND | BPF_X: 1278 case BPF_ALU64 | BPF_AND | BPF_X: 1279 emit(A64_AND(is64, dst, dst, src), ctx); 1280 break; 1281 case BPF_ALU | BPF_OR | BPF_X: 1282 case BPF_ALU64 | BPF_OR | BPF_X: 1283 emit(A64_ORR(is64, dst, dst, src), ctx); 1284 break; 1285 case BPF_ALU | BPF_XOR | BPF_X: 1286 case BPF_ALU64 | BPF_XOR | BPF_X: 1287 emit(A64_EOR(is64, dst, dst, src), ctx); 1288 break; 1289 case BPF_ALU | BPF_MUL | BPF_X: 1290 case BPF_ALU64 | BPF_MUL | BPF_X: 1291 emit(A64_MUL(is64, dst, dst, src), ctx); 1292 break; 1293 case BPF_ALU | BPF_DIV | BPF_X: 1294 case BPF_ALU64 | BPF_DIV | BPF_X: 1295 if (!off) 1296 emit(A64_UDIV(is64, dst, dst, src), ctx); 1297 else 1298 emit(A64_SDIV(is64, dst, dst, src), ctx); 1299 break; 1300 case BPF_ALU | BPF_MOD | BPF_X: 1301 case BPF_ALU64 | BPF_MOD | BPF_X: 1302 if (!off) 1303 emit(A64_UDIV(is64, tmp, dst, src), ctx); 1304 else 1305 emit(A64_SDIV(is64, tmp, dst, src), ctx); 1306 emit(A64_MSUB(is64, dst, dst, tmp, src), ctx); 1307 break; 1308 case BPF_ALU | BPF_LSH | BPF_X: 1309 case BPF_ALU64 | BPF_LSH | BPF_X: 1310 emit(A64_LSLV(is64, dst, dst, src), ctx); 1311 break; 1312 case BPF_ALU | BPF_RSH | BPF_X: 1313 case BPF_ALU64 | BPF_RSH | BPF_X: 1314 emit(A64_LSRV(is64, dst, dst, src), ctx); 1315 break; 1316 case BPF_ALU | BPF_ARSH | BPF_X: 1317 case BPF_ALU64 | BPF_ARSH | BPF_X: 1318 emit(A64_ASRV(is64, dst, dst, src), ctx); 1319 break; 1320 /* dst = -dst */ 1321 case BPF_ALU | BPF_NEG: 1322 case BPF_ALU64 | BPF_NEG: 1323 emit(A64_NEG(is64, dst, dst), ctx); 1324 break; 1325 /* dst = BSWAP##imm(dst) */ 1326 case BPF_ALU | BPF_END | BPF_FROM_LE: 1327 case BPF_ALU | BPF_END | BPF_FROM_BE: 1328 case BPF_ALU64 | BPF_END | BPF_FROM_LE: 1329 #ifdef CONFIG_CPU_BIG_ENDIAN 1330 if (BPF_CLASS(code) == BPF_ALU && BPF_SRC(code) == BPF_FROM_BE) 1331 goto emit_bswap_uxt; 1332 #else /* !CONFIG_CPU_BIG_ENDIAN */ 1333 if (BPF_CLASS(code) == BPF_ALU && BPF_SRC(code) == BPF_FROM_LE) 1334 goto emit_bswap_uxt; 1335 #endif 1336 switch (imm) { 1337 case 16: 1338 emit(A64_REV16(is64, dst, dst), ctx); 1339 /* zero-extend 16 bits into 64 bits */ 1340 emit(A64_UXTH(is64, dst, dst), ctx); 1341 break; 1342 case 32: 1343 emit(A64_REV32(0, dst, dst), ctx); 1344 /* upper 32 bits already cleared */ 1345 break; 1346 case 64: 1347 emit(A64_REV64(dst, dst), ctx); 1348 break; 1349 } 1350 break; 1351 emit_bswap_uxt: 1352 switch (imm) { 1353 case 16: 1354 /* zero-extend 16 bits into 64 bits */ 1355 emit(A64_UXTH(is64, dst, dst), ctx); 1356 break; 1357 case 32: 1358 /* zero-extend 32 bits into 64 bits */ 1359 emit(A64_UXTW(is64, dst, dst), ctx); 1360 break; 1361 case 64: 1362 /* nop */ 1363 break; 1364 } 1365 break; 1366 /* dst = imm */ 1367 case BPF_ALU | BPF_MOV | BPF_K: 1368 case BPF_ALU64 | BPF_MOV | BPF_K: 1369 emit_a64_mov_i(is64, dst, imm, ctx); 1370 break; 1371 /* dst = dst OP imm */ 1372 case BPF_ALU | BPF_ADD | BPF_K: 1373 case BPF_ALU64 | BPF_ADD | BPF_K: 1374 emit_a64_add_i(is64, dst, dst, tmp, imm, ctx); 1375 break; 1376 case BPF_ALU | BPF_SUB | BPF_K: 1377 case BPF_ALU64 | BPF_SUB | BPF_K: 1378 if (is_addsub_imm(imm)) { 1379 emit(A64_SUB_I(is64, dst, dst, imm), ctx); 1380 } else if (is_addsub_imm(-(u32)imm)) { 1381 emit(A64_ADD_I(is64, dst, dst, -imm), ctx); 1382 } else { 1383 emit_a64_mov_i(is64, tmp, imm, ctx); 1384 emit(A64_SUB(is64, dst, dst, tmp), ctx); 1385 } 1386 break; 1387 case BPF_ALU | BPF_AND | BPF_K: 1388 case BPF_ALU64 | BPF_AND | BPF_K: 1389 a64_insn = A64_AND_I(is64, dst, dst, imm); 1390 if (a64_insn != AARCH64_BREAK_FAULT) { 1391 emit(a64_insn, ctx); 1392 } else { 1393 emit_a64_mov_i(is64, tmp, imm, ctx); 1394 emit(A64_AND(is64, dst, dst, tmp), ctx); 1395 } 1396 break; 1397 case BPF_ALU | BPF_OR | BPF_K: 1398 case BPF_ALU64 | BPF_OR | BPF_K: 1399 a64_insn = A64_ORR_I(is64, dst, dst, imm); 1400 if (a64_insn != AARCH64_BREAK_FAULT) { 1401 emit(a64_insn, ctx); 1402 } else { 1403 emit_a64_mov_i(is64, tmp, imm, ctx); 1404 emit(A64_ORR(is64, dst, dst, tmp), ctx); 1405 } 1406 break; 1407 case BPF_ALU | BPF_XOR | BPF_K: 1408 case BPF_ALU64 | BPF_XOR | BPF_K: 1409 a64_insn = A64_EOR_I(is64, dst, dst, imm); 1410 if (a64_insn != AARCH64_BREAK_FAULT) { 1411 emit(a64_insn, ctx); 1412 } else { 1413 emit_a64_mov_i(is64, tmp, imm, ctx); 1414 emit(A64_EOR(is64, dst, dst, tmp), ctx); 1415 } 1416 break; 1417 case BPF_ALU | BPF_MUL | BPF_K: 1418 case BPF_ALU64 | BPF_MUL | BPF_K: 1419 emit_a64_mov_i(is64, tmp, imm, ctx); 1420 emit(A64_MUL(is64, dst, dst, tmp), ctx); 1421 break; 1422 case BPF_ALU | BPF_DIV | BPF_K: 1423 case BPF_ALU64 | BPF_DIV | BPF_K: 1424 emit_a64_mov_i(is64, tmp, imm, ctx); 1425 if (!off) 1426 emit(A64_UDIV(is64, dst, dst, tmp), ctx); 1427 else 1428 emit(A64_SDIV(is64, dst, dst, tmp), ctx); 1429 break; 1430 case BPF_ALU | BPF_MOD | BPF_K: 1431 case BPF_ALU64 | BPF_MOD | BPF_K: 1432 emit_a64_mov_i(is64, tmp2, imm, ctx); 1433 if (!off) 1434 emit(A64_UDIV(is64, tmp, dst, tmp2), ctx); 1435 else 1436 emit(A64_SDIV(is64, tmp, dst, tmp2), ctx); 1437 emit(A64_MSUB(is64, dst, dst, tmp, tmp2), ctx); 1438 break; 1439 case BPF_ALU | BPF_LSH | BPF_K: 1440 case BPF_ALU64 | BPF_LSH | BPF_K: 1441 emit(A64_LSL(is64, dst, dst, imm), ctx); 1442 break; 1443 case BPF_ALU | BPF_RSH | BPF_K: 1444 case BPF_ALU64 | BPF_RSH | BPF_K: 1445 emit(A64_LSR(is64, dst, dst, imm), ctx); 1446 break; 1447 case BPF_ALU | BPF_ARSH | BPF_K: 1448 case BPF_ALU64 | BPF_ARSH | BPF_K: 1449 emit(A64_ASR(is64, dst, dst, imm), ctx); 1450 break; 1451 1452 /* JUMP off */ 1453 case BPF_JMP | BPF_JA: 1454 case BPF_JMP32 | BPF_JA: 1455 if (BPF_CLASS(code) == BPF_JMP) 1456 jmp_offset = bpf2a64_offset(i, off, ctx); 1457 else 1458 jmp_offset = bpf2a64_offset(i, imm, ctx); 1459 check_imm26(jmp_offset); 1460 emit(A64_B(jmp_offset), ctx); 1461 break; 1462 /* IF (dst COND src) JUMP off */ 1463 case BPF_JMP | BPF_JEQ | BPF_X: 1464 case BPF_JMP | BPF_JGT | BPF_X: 1465 case BPF_JMP | BPF_JLT | BPF_X: 1466 case BPF_JMP | BPF_JGE | BPF_X: 1467 case BPF_JMP | BPF_JLE | BPF_X: 1468 case BPF_JMP | BPF_JNE | BPF_X: 1469 case BPF_JMP | BPF_JSGT | BPF_X: 1470 case BPF_JMP | BPF_JSLT | BPF_X: 1471 case BPF_JMP | BPF_JSGE | BPF_X: 1472 case BPF_JMP | BPF_JSLE | BPF_X: 1473 case BPF_JMP32 | BPF_JEQ | BPF_X: 1474 case BPF_JMP32 | BPF_JGT | BPF_X: 1475 case BPF_JMP32 | BPF_JLT | BPF_X: 1476 case BPF_JMP32 | BPF_JGE | BPF_X: 1477 case BPF_JMP32 | BPF_JLE | BPF_X: 1478 case BPF_JMP32 | BPF_JNE | BPF_X: 1479 case BPF_JMP32 | BPF_JSGT | BPF_X: 1480 case BPF_JMP32 | BPF_JSLT | BPF_X: 1481 case BPF_JMP32 | BPF_JSGE | BPF_X: 1482 case BPF_JMP32 | BPF_JSLE | BPF_X: 1483 emit(A64_CMP(is64, dst, src), ctx); 1484 emit_cond_jmp: 1485 jmp_offset = bpf2a64_offset(i, off, ctx); 1486 check_imm19(jmp_offset); 1487 switch (BPF_OP(code)) { 1488 case BPF_JEQ: 1489 jmp_cond = A64_COND_EQ; 1490 break; 1491 case BPF_JGT: 1492 jmp_cond = A64_COND_HI; 1493 break; 1494 case BPF_JLT: 1495 jmp_cond = A64_COND_CC; 1496 break; 1497 case BPF_JGE: 1498 jmp_cond = A64_COND_CS; 1499 break; 1500 case BPF_JLE: 1501 jmp_cond = A64_COND_LS; 1502 break; 1503 case BPF_JSET: 1504 case BPF_JNE: 1505 jmp_cond = A64_COND_NE; 1506 break; 1507 case BPF_JSGT: 1508 jmp_cond = A64_COND_GT; 1509 break; 1510 case BPF_JSLT: 1511 jmp_cond = A64_COND_LT; 1512 break; 1513 case BPF_JSGE: 1514 jmp_cond = A64_COND_GE; 1515 break; 1516 case BPF_JSLE: 1517 jmp_cond = A64_COND_LE; 1518 break; 1519 default: 1520 return -EFAULT; 1521 } 1522 emit(A64_B_(jmp_cond, jmp_offset), ctx); 1523 break; 1524 case BPF_JMP | BPF_JSET | BPF_X: 1525 case BPF_JMP32 | BPF_JSET | BPF_X: 1526 emit(A64_TST(is64, dst, src), ctx); 1527 goto emit_cond_jmp; 1528 /* IF (dst COND imm) JUMP off */ 1529 case BPF_JMP | BPF_JEQ | BPF_K: 1530 case BPF_JMP | BPF_JGT | BPF_K: 1531 case BPF_JMP | BPF_JLT | BPF_K: 1532 case BPF_JMP | BPF_JGE | BPF_K: 1533 case BPF_JMP | BPF_JLE | BPF_K: 1534 case BPF_JMP | BPF_JNE | BPF_K: 1535 case BPF_JMP | BPF_JSGT | BPF_K: 1536 case BPF_JMP | BPF_JSLT | BPF_K: 1537 case BPF_JMP | BPF_JSGE | BPF_K: 1538 case BPF_JMP | BPF_JSLE | BPF_K: 1539 case BPF_JMP32 | BPF_JEQ | BPF_K: 1540 case BPF_JMP32 | BPF_JGT | BPF_K: 1541 case BPF_JMP32 | BPF_JLT | BPF_K: 1542 case BPF_JMP32 | BPF_JGE | BPF_K: 1543 case BPF_JMP32 | BPF_JLE | BPF_K: 1544 case BPF_JMP32 | BPF_JNE | BPF_K: 1545 case BPF_JMP32 | BPF_JSGT | BPF_K: 1546 case BPF_JMP32 | BPF_JSLT | BPF_K: 1547 case BPF_JMP32 | BPF_JSGE | BPF_K: 1548 case BPF_JMP32 | BPF_JSLE | BPF_K: 1549 if (is_addsub_imm(imm)) { 1550 emit(A64_CMP_I(is64, dst, imm), ctx); 1551 } else if (is_addsub_imm(-(u32)imm)) { 1552 emit(A64_CMN_I(is64, dst, -imm), ctx); 1553 } else { 1554 emit_a64_mov_i(is64, tmp, imm, ctx); 1555 emit(A64_CMP(is64, dst, tmp), ctx); 1556 } 1557 goto emit_cond_jmp; 1558 case BPF_JMP | BPF_JSET | BPF_K: 1559 case BPF_JMP32 | BPF_JSET | BPF_K: 1560 a64_insn = A64_TST_I(is64, dst, imm); 1561 if (a64_insn != AARCH64_BREAK_FAULT) { 1562 emit(a64_insn, ctx); 1563 } else { 1564 emit_a64_mov_i(is64, tmp, imm, ctx); 1565 emit(A64_TST(is64, dst, tmp), ctx); 1566 } 1567 goto emit_cond_jmp; 1568 /* function call */ 1569 case BPF_JMP | BPF_CALL: 1570 { 1571 const u8 r0 = bpf2a64[BPF_REG_0]; 1572 bool func_addr_fixed; 1573 u64 func_addr; 1574 u32 cpu_offset; 1575 1576 /* Implement helper call to bpf_get_smp_processor_id() inline */ 1577 if (insn->src_reg == 0 && insn->imm == BPF_FUNC_get_smp_processor_id) { 1578 cpu_offset = offsetof(struct thread_info, cpu); 1579 1580 emit(A64_MRS_SP_EL0(tmp), ctx); 1581 if (is_lsi_offset(cpu_offset, 2)) { 1582 emit(A64_LDR32I(r0, tmp, cpu_offset), ctx); 1583 } else { 1584 emit_a64_mov_i(1, tmp2, cpu_offset, ctx); 1585 emit(A64_LDR32(r0, tmp, tmp2), ctx); 1586 } 1587 break; 1588 } 1589 1590 /* Implement helper call to bpf_get_current_task/_btf() inline */ 1591 if (insn->src_reg == 0 && (insn->imm == BPF_FUNC_get_current_task || 1592 insn->imm == BPF_FUNC_get_current_task_btf)) { 1593 emit(A64_MRS_SP_EL0(r0), ctx); 1594 break; 1595 } 1596 1597 ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass, 1598 &func_addr, &func_addr_fixed); 1599 if (ret < 0) 1600 return ret; 1601 emit_call(func_addr, ctx); 1602 /* 1603 * Call to arch_bpf_timed_may_goto() is emitted by the 1604 * verifier and called with custom calling convention with 1605 * first argument and return value in BPF_REG_AX (x9). 1606 */ 1607 if (func_addr != (u64)arch_bpf_timed_may_goto) 1608 emit(A64_MOV(1, r0, A64_R(0)), ctx); 1609 break; 1610 } 1611 /* tail call */ 1612 case BPF_JMP | BPF_TAIL_CALL: 1613 if (emit_bpf_tail_call(ctx)) 1614 return -EFAULT; 1615 break; 1616 /* function return */ 1617 case BPF_JMP | BPF_EXIT: 1618 /* Optimization: when last instruction is EXIT, 1619 simply fallthrough to epilogue. */ 1620 if (i == ctx->prog->len - 1) 1621 break; 1622 jmp_offset = epilogue_offset(ctx); 1623 check_imm26(jmp_offset); 1624 emit(A64_B(jmp_offset), ctx); 1625 break; 1626 1627 /* dst = imm64 */ 1628 case BPF_LD | BPF_IMM | BPF_DW: 1629 { 1630 const struct bpf_insn insn1 = insn[1]; 1631 u64 imm64; 1632 1633 imm64 = (u64)insn1.imm << 32 | (u32)imm; 1634 if (bpf_pseudo_func(insn)) 1635 emit_addr_mov_i64(dst, imm64, ctx); 1636 else 1637 emit_a64_mov_i64(dst, imm64, ctx); 1638 1639 return 1; 1640 } 1641 1642 /* LDX: dst = (u64)*(unsigned size *)(src + off) */ 1643 case BPF_LDX | BPF_MEM | BPF_W: 1644 case BPF_LDX | BPF_MEM | BPF_H: 1645 case BPF_LDX | BPF_MEM | BPF_B: 1646 case BPF_LDX | BPF_MEM | BPF_DW: 1647 case BPF_LDX | BPF_PROBE_MEM | BPF_DW: 1648 case BPF_LDX | BPF_PROBE_MEM | BPF_W: 1649 case BPF_LDX | BPF_PROBE_MEM | BPF_H: 1650 case BPF_LDX | BPF_PROBE_MEM | BPF_B: 1651 /* LDXS: dst_reg = (s64)*(signed size *)(src_reg + off) */ 1652 case BPF_LDX | BPF_MEMSX | BPF_B: 1653 case BPF_LDX | BPF_MEMSX | BPF_H: 1654 case BPF_LDX | BPF_MEMSX | BPF_W: 1655 case BPF_LDX | BPF_PROBE_MEMSX | BPF_B: 1656 case BPF_LDX | BPF_PROBE_MEMSX | BPF_H: 1657 case BPF_LDX | BPF_PROBE_MEMSX | BPF_W: 1658 case BPF_LDX | BPF_PROBE_MEM32 | BPF_B: 1659 case BPF_LDX | BPF_PROBE_MEM32 | BPF_H: 1660 case BPF_LDX | BPF_PROBE_MEM32 | BPF_W: 1661 case BPF_LDX | BPF_PROBE_MEM32 | BPF_DW: 1662 if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) { 1663 emit(A64_ADD(1, tmp2, src, arena_vm_base), ctx); 1664 src = tmp2; 1665 } 1666 if (src == fp) { 1667 src_adj = ctx->priv_sp_used ? priv_sp : A64_SP; 1668 off_adj = off + ctx->stack_size; 1669 } else { 1670 src_adj = src; 1671 off_adj = off; 1672 } 1673 sign_extend = (BPF_MODE(insn->code) == BPF_MEMSX || 1674 BPF_MODE(insn->code) == BPF_PROBE_MEMSX); 1675 switch (BPF_SIZE(code)) { 1676 case BPF_W: 1677 if (is_lsi_offset(off_adj, 2)) { 1678 if (sign_extend) 1679 emit(A64_LDRSWI(dst, src_adj, off_adj), ctx); 1680 else 1681 emit(A64_LDR32I(dst, src_adj, off_adj), ctx); 1682 } else { 1683 emit_a64_mov_i(1, tmp, off, ctx); 1684 if (sign_extend) 1685 emit(A64_LDRSW(dst, src, tmp), ctx); 1686 else 1687 emit(A64_LDR32(dst, src, tmp), ctx); 1688 } 1689 break; 1690 case BPF_H: 1691 if (is_lsi_offset(off_adj, 1)) { 1692 if (sign_extend) 1693 emit(A64_LDRSHI(dst, src_adj, off_adj), ctx); 1694 else 1695 emit(A64_LDRHI(dst, src_adj, off_adj), ctx); 1696 } else { 1697 emit_a64_mov_i(1, tmp, off, ctx); 1698 if (sign_extend) 1699 emit(A64_LDRSH(dst, src, tmp), ctx); 1700 else 1701 emit(A64_LDRH(dst, src, tmp), ctx); 1702 } 1703 break; 1704 case BPF_B: 1705 if (is_lsi_offset(off_adj, 0)) { 1706 if (sign_extend) 1707 emit(A64_LDRSBI(dst, src_adj, off_adj), ctx); 1708 else 1709 emit(A64_LDRBI(dst, src_adj, off_adj), ctx); 1710 } else { 1711 emit_a64_mov_i(1, tmp, off, ctx); 1712 if (sign_extend) 1713 emit(A64_LDRSB(dst, src, tmp), ctx); 1714 else 1715 emit(A64_LDRB(dst, src, tmp), ctx); 1716 } 1717 break; 1718 case BPF_DW: 1719 if (is_lsi_offset(off_adj, 3)) { 1720 emit(A64_LDR64I(dst, src_adj, off_adj), ctx); 1721 } else { 1722 emit_a64_mov_i(1, tmp, off, ctx); 1723 emit(A64_LDR64(dst, src, tmp), ctx); 1724 } 1725 break; 1726 } 1727 1728 ret = add_exception_handler(insn, ctx, dst); 1729 if (ret) 1730 return ret; 1731 break; 1732 1733 /* speculation barrier against v1 and v4 */ 1734 case BPF_ST | BPF_NOSPEC: 1735 if (alternative_has_cap_likely(ARM64_HAS_SB)) { 1736 emit(A64_SB, ctx); 1737 } else { 1738 emit(A64_DSB_NSH, ctx); 1739 emit(A64_ISB, ctx); 1740 } 1741 break; 1742 1743 /* ST: *(size *)(dst + off) = imm */ 1744 case BPF_ST | BPF_MEM | BPF_W: 1745 case BPF_ST | BPF_MEM | BPF_H: 1746 case BPF_ST | BPF_MEM | BPF_B: 1747 case BPF_ST | BPF_MEM | BPF_DW: 1748 case BPF_ST | BPF_PROBE_MEM32 | BPF_B: 1749 case BPF_ST | BPF_PROBE_MEM32 | BPF_H: 1750 case BPF_ST | BPF_PROBE_MEM32 | BPF_W: 1751 case BPF_ST | BPF_PROBE_MEM32 | BPF_DW: 1752 if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) { 1753 emit(A64_ADD(1, tmp2, dst, arena_vm_base), ctx); 1754 dst = tmp2; 1755 } 1756 if (dst == fp) { 1757 dst_adj = ctx->priv_sp_used ? priv_sp : A64_SP; 1758 off_adj = off + ctx->stack_size; 1759 } else { 1760 dst_adj = dst; 1761 off_adj = off; 1762 } 1763 /* Load imm to a register then store it */ 1764 emit_a64_mov_i(1, tmp, imm, ctx); 1765 switch (BPF_SIZE(code)) { 1766 case BPF_W: 1767 if (is_lsi_offset(off_adj, 2)) { 1768 emit(A64_STR32I(tmp, dst_adj, off_adj), ctx); 1769 } else { 1770 emit_a64_mov_i(1, tmp2, off, ctx); 1771 emit(A64_STR32(tmp, dst, tmp2), ctx); 1772 } 1773 break; 1774 case BPF_H: 1775 if (is_lsi_offset(off_adj, 1)) { 1776 emit(A64_STRHI(tmp, dst_adj, off_adj), ctx); 1777 } else { 1778 emit_a64_mov_i(1, tmp2, off, ctx); 1779 emit(A64_STRH(tmp, dst, tmp2), ctx); 1780 } 1781 break; 1782 case BPF_B: 1783 if (is_lsi_offset(off_adj, 0)) { 1784 emit(A64_STRBI(tmp, dst_adj, off_adj), ctx); 1785 } else { 1786 emit_a64_mov_i(1, tmp2, off, ctx); 1787 emit(A64_STRB(tmp, dst, tmp2), ctx); 1788 } 1789 break; 1790 case BPF_DW: 1791 if (is_lsi_offset(off_adj, 3)) { 1792 emit(A64_STR64I(tmp, dst_adj, off_adj), ctx); 1793 } else { 1794 emit_a64_mov_i(1, tmp2, off, ctx); 1795 emit(A64_STR64(tmp, dst, tmp2), ctx); 1796 } 1797 break; 1798 } 1799 1800 ret = add_exception_handler(insn, ctx, dst); 1801 if (ret) 1802 return ret; 1803 break; 1804 1805 /* STX: *(size *)(dst + off) = src */ 1806 case BPF_STX | BPF_MEM | BPF_W: 1807 case BPF_STX | BPF_MEM | BPF_H: 1808 case BPF_STX | BPF_MEM | BPF_B: 1809 case BPF_STX | BPF_MEM | BPF_DW: 1810 case BPF_STX | BPF_PROBE_MEM32 | BPF_B: 1811 case BPF_STX | BPF_PROBE_MEM32 | BPF_H: 1812 case BPF_STX | BPF_PROBE_MEM32 | BPF_W: 1813 case BPF_STX | BPF_PROBE_MEM32 | BPF_DW: 1814 if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) { 1815 emit(A64_ADD(1, tmp2, dst, arena_vm_base), ctx); 1816 dst = tmp2; 1817 } 1818 if (dst == fp) { 1819 dst_adj = ctx->priv_sp_used ? priv_sp : A64_SP; 1820 off_adj = off + ctx->stack_size; 1821 } else { 1822 dst_adj = dst; 1823 off_adj = off; 1824 } 1825 switch (BPF_SIZE(code)) { 1826 case BPF_W: 1827 if (is_lsi_offset(off_adj, 2)) { 1828 emit(A64_STR32I(src, dst_adj, off_adj), ctx); 1829 } else { 1830 emit_a64_mov_i(1, tmp, off, ctx); 1831 emit(A64_STR32(src, dst, tmp), ctx); 1832 } 1833 break; 1834 case BPF_H: 1835 if (is_lsi_offset(off_adj, 1)) { 1836 emit(A64_STRHI(src, dst_adj, off_adj), ctx); 1837 } else { 1838 emit_a64_mov_i(1, tmp, off, ctx); 1839 emit(A64_STRH(src, dst, tmp), ctx); 1840 } 1841 break; 1842 case BPF_B: 1843 if (is_lsi_offset(off_adj, 0)) { 1844 emit(A64_STRBI(src, dst_adj, off_adj), ctx); 1845 } else { 1846 emit_a64_mov_i(1, tmp, off, ctx); 1847 emit(A64_STRB(src, dst, tmp), ctx); 1848 } 1849 break; 1850 case BPF_DW: 1851 if (is_lsi_offset(off_adj, 3)) { 1852 emit(A64_STR64I(src, dst_adj, off_adj), ctx); 1853 } else { 1854 emit_a64_mov_i(1, tmp, off, ctx); 1855 emit(A64_STR64(src, dst, tmp), ctx); 1856 } 1857 break; 1858 } 1859 1860 ret = add_exception_handler(insn, ctx, dst); 1861 if (ret) 1862 return ret; 1863 break; 1864 1865 case BPF_STX | BPF_ATOMIC | BPF_B: 1866 case BPF_STX | BPF_ATOMIC | BPF_H: 1867 case BPF_STX | BPF_ATOMIC | BPF_W: 1868 case BPF_STX | BPF_ATOMIC | BPF_DW: 1869 case BPF_STX | BPF_PROBE_ATOMIC | BPF_B: 1870 case BPF_STX | BPF_PROBE_ATOMIC | BPF_H: 1871 case BPF_STX | BPF_PROBE_ATOMIC | BPF_W: 1872 case BPF_STX | BPF_PROBE_ATOMIC | BPF_DW: 1873 if (bpf_atomic_is_load_store(insn)) 1874 ret = emit_atomic_ld_st(insn, ctx); 1875 else if (cpus_have_cap(ARM64_HAS_LSE_ATOMICS)) 1876 ret = emit_lse_atomic(insn, ctx); 1877 else 1878 ret = emit_ll_sc_atomic(insn, ctx); 1879 if (ret) 1880 return ret; 1881 1882 ret = add_exception_handler(insn, ctx, dst); 1883 if (ret) 1884 return ret; 1885 break; 1886 1887 default: 1888 pr_err_once("unknown opcode %02x\n", code); 1889 return -EINVAL; 1890 } 1891 1892 return 0; 1893 } 1894 1895 static int build_body(struct jit_ctx *ctx, bool extra_pass) 1896 { 1897 const struct bpf_prog *prog = ctx->prog; 1898 int i; 1899 1900 /* 1901 * - offset[0] offset of the end of prologue, 1902 * start of the 1st instruction. 1903 * - offset[1] - offset of the end of 1st instruction, 1904 * start of the 2nd instruction 1905 * [....] 1906 * - offset[3] - offset of the end of 3rd instruction, 1907 * start of 4th instruction 1908 */ 1909 for (i = 0; i < prog->len; i++) { 1910 const struct bpf_insn *insn = &prog->insnsi[i]; 1911 int ret; 1912 1913 ctx->offset[i] = ctx->idx; 1914 ret = build_insn(insn, ctx, extra_pass); 1915 if (ret > 0) { 1916 i++; 1917 ctx->offset[i] = ctx->idx; 1918 continue; 1919 } 1920 if (ret) 1921 return ret; 1922 } 1923 /* 1924 * offset is allocated with prog->len + 1 so fill in 1925 * the last element with the offset after the last 1926 * instruction (end of program) 1927 */ 1928 ctx->offset[i] = ctx->idx; 1929 1930 return 0; 1931 } 1932 1933 static int validate_code(struct jit_ctx *ctx) 1934 { 1935 int i; 1936 1937 for (i = 0; i < ctx->idx; i++) { 1938 u32 a64_insn = le32_to_cpu(ctx->image[i]); 1939 1940 if (a64_insn == AARCH64_BREAK_FAULT) 1941 return -1; 1942 } 1943 return 0; 1944 } 1945 1946 static int validate_ctx(struct jit_ctx *ctx) 1947 { 1948 if (validate_code(ctx)) 1949 return -1; 1950 1951 if (WARN_ON_ONCE(ctx->exentry_idx != ctx->prog->aux->num_exentries)) 1952 return -1; 1953 1954 return 0; 1955 } 1956 1957 static inline void bpf_flush_icache(void *start, void *end) 1958 { 1959 flush_icache_range((unsigned long)start, (unsigned long)end); 1960 } 1961 1962 static void priv_stack_init_guard(void __percpu *priv_stack_ptr, int alloc_size) 1963 { 1964 int cpu, underflow_idx = (alloc_size - PRIV_STACK_GUARD_SZ) >> 3; 1965 u64 *stack_ptr; 1966 1967 for_each_possible_cpu(cpu) { 1968 stack_ptr = per_cpu_ptr(priv_stack_ptr, cpu); 1969 stack_ptr[0] = PRIV_STACK_GUARD_VAL; 1970 stack_ptr[1] = PRIV_STACK_GUARD_VAL; 1971 stack_ptr[underflow_idx] = PRIV_STACK_GUARD_VAL; 1972 stack_ptr[underflow_idx + 1] = PRIV_STACK_GUARD_VAL; 1973 } 1974 } 1975 1976 static void priv_stack_check_guard(void __percpu *priv_stack_ptr, int alloc_size, 1977 struct bpf_prog *prog) 1978 { 1979 int cpu, underflow_idx = (alloc_size - PRIV_STACK_GUARD_SZ) >> 3; 1980 u64 *stack_ptr; 1981 1982 for_each_possible_cpu(cpu) { 1983 stack_ptr = per_cpu_ptr(priv_stack_ptr, cpu); 1984 if (stack_ptr[0] != PRIV_STACK_GUARD_VAL || 1985 stack_ptr[1] != PRIV_STACK_GUARD_VAL || 1986 stack_ptr[underflow_idx] != PRIV_STACK_GUARD_VAL || 1987 stack_ptr[underflow_idx + 1] != PRIV_STACK_GUARD_VAL) { 1988 pr_err("BPF private stack overflow/underflow detected for prog %sx\n", 1989 bpf_jit_get_prog_name(prog)); 1990 break; 1991 } 1992 } 1993 } 1994 1995 struct arm64_jit_data { 1996 struct bpf_binary_header *header; 1997 u8 *ro_image; 1998 struct bpf_binary_header *ro_header; 1999 struct jit_ctx ctx; 2000 }; 2001 2002 struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) 2003 { 2004 int image_size, prog_size, extable_size, extable_align, extable_offset; 2005 struct bpf_prog *tmp, *orig_prog = prog; 2006 struct bpf_binary_header *header; 2007 struct bpf_binary_header *ro_header = NULL; 2008 struct arm64_jit_data *jit_data; 2009 void __percpu *priv_stack_ptr = NULL; 2010 bool was_classic = bpf_prog_was_classic(prog); 2011 int priv_stack_alloc_sz; 2012 bool tmp_blinded = false; 2013 bool extra_pass = false; 2014 struct jit_ctx ctx; 2015 u8 *image_ptr; 2016 u8 *ro_image_ptr; 2017 int body_idx; 2018 int exentry_idx; 2019 2020 if (!prog->jit_requested) 2021 return orig_prog; 2022 2023 tmp = bpf_jit_blind_constants(prog); 2024 /* If blinding was requested and we failed during blinding, 2025 * we must fall back to the interpreter. 2026 */ 2027 if (IS_ERR(tmp)) 2028 return orig_prog; 2029 if (tmp != prog) { 2030 tmp_blinded = true; 2031 prog = tmp; 2032 } 2033 2034 jit_data = prog->aux->jit_data; 2035 if (!jit_data) { 2036 jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL); 2037 if (!jit_data) { 2038 prog = orig_prog; 2039 goto out; 2040 } 2041 prog->aux->jit_data = jit_data; 2042 } 2043 priv_stack_ptr = prog->aux->priv_stack_ptr; 2044 if (!priv_stack_ptr && prog->aux->jits_use_priv_stack) { 2045 /* Allocate actual private stack size with verifier-calculated 2046 * stack size plus two memory guards to protect overflow and 2047 * underflow. 2048 */ 2049 priv_stack_alloc_sz = round_up(prog->aux->stack_depth, 16) + 2050 2 * PRIV_STACK_GUARD_SZ; 2051 priv_stack_ptr = __alloc_percpu_gfp(priv_stack_alloc_sz, 16, GFP_KERNEL); 2052 if (!priv_stack_ptr) { 2053 prog = orig_prog; 2054 goto out_priv_stack; 2055 } 2056 2057 priv_stack_init_guard(priv_stack_ptr, priv_stack_alloc_sz); 2058 prog->aux->priv_stack_ptr = priv_stack_ptr; 2059 } 2060 if (jit_data->ctx.offset) { 2061 ctx = jit_data->ctx; 2062 ro_image_ptr = jit_data->ro_image; 2063 ro_header = jit_data->ro_header; 2064 header = jit_data->header; 2065 image_ptr = (void *)header + ((void *)ro_image_ptr 2066 - (void *)ro_header); 2067 extra_pass = true; 2068 prog_size = sizeof(u32) * ctx.idx; 2069 goto skip_init_ctx; 2070 } 2071 memset(&ctx, 0, sizeof(ctx)); 2072 ctx.prog = prog; 2073 2074 ctx.offset = kvcalloc(prog->len + 1, sizeof(int), GFP_KERNEL); 2075 if (ctx.offset == NULL) { 2076 prog = orig_prog; 2077 goto out_off; 2078 } 2079 2080 ctx.user_vm_start = bpf_arena_get_user_vm_start(prog->aux->arena); 2081 ctx.arena_vm_start = bpf_arena_get_kern_vm_start(prog->aux->arena); 2082 2083 if (priv_stack_ptr) 2084 ctx.priv_sp_used = true; 2085 2086 /* Pass 1: Estimate the maximum image size. 2087 * 2088 * BPF line info needs ctx->offset[i] to be the offset of 2089 * instruction[i] in jited image, so build prologue first. 2090 */ 2091 if (build_prologue(&ctx, was_classic)) { 2092 prog = orig_prog; 2093 goto out_off; 2094 } 2095 2096 if (build_body(&ctx, extra_pass)) { 2097 prog = orig_prog; 2098 goto out_off; 2099 } 2100 2101 ctx.epilogue_offset = ctx.idx; 2102 build_epilogue(&ctx, was_classic); 2103 build_plt(&ctx); 2104 2105 extable_align = __alignof__(struct exception_table_entry); 2106 extable_size = prog->aux->num_exentries * 2107 sizeof(struct exception_table_entry); 2108 2109 /* Now we know the maximum image size. */ 2110 prog_size = sizeof(u32) * ctx.idx; 2111 /* also allocate space for plt target */ 2112 extable_offset = round_up(prog_size + PLT_TARGET_SIZE, extable_align); 2113 image_size = extable_offset + extable_size; 2114 ro_header = bpf_jit_binary_pack_alloc(image_size, &ro_image_ptr, 2115 sizeof(u32), &header, &image_ptr, 2116 jit_fill_hole); 2117 if (!ro_header) { 2118 prog = orig_prog; 2119 goto out_off; 2120 } 2121 2122 /* Pass 2: Determine jited position and result for each instruction */ 2123 2124 /* 2125 * Use the image(RW) for writing the JITed instructions. But also save 2126 * the ro_image(RX) for calculating the offsets in the image. The RW 2127 * image will be later copied to the RX image from where the program 2128 * will run. The bpf_jit_binary_pack_finalize() will do this copy in the 2129 * final step. 2130 */ 2131 ctx.image = (__le32 *)image_ptr; 2132 ctx.ro_image = (__le32 *)ro_image_ptr; 2133 if (extable_size) 2134 prog->aux->extable = (void *)ro_image_ptr + extable_offset; 2135 skip_init_ctx: 2136 ctx.idx = 0; 2137 ctx.exentry_idx = 0; 2138 ctx.write = true; 2139 2140 build_prologue(&ctx, was_classic); 2141 2142 /* Record exentry_idx and body_idx before first build_body */ 2143 exentry_idx = ctx.exentry_idx; 2144 body_idx = ctx.idx; 2145 /* Dont write body instructions to memory for now */ 2146 ctx.write = false; 2147 2148 if (build_body(&ctx, extra_pass)) { 2149 prog = orig_prog; 2150 goto out_free_hdr; 2151 } 2152 2153 ctx.epilogue_offset = ctx.idx; 2154 ctx.exentry_idx = exentry_idx; 2155 ctx.idx = body_idx; 2156 ctx.write = true; 2157 2158 /* Pass 3: Adjust jump offset and write final image */ 2159 if (build_body(&ctx, extra_pass) || 2160 WARN_ON_ONCE(ctx.idx != ctx.epilogue_offset)) { 2161 prog = orig_prog; 2162 goto out_free_hdr; 2163 } 2164 2165 build_epilogue(&ctx, was_classic); 2166 build_plt(&ctx); 2167 2168 /* Extra pass to validate JITed code. */ 2169 if (validate_ctx(&ctx)) { 2170 prog = orig_prog; 2171 goto out_free_hdr; 2172 } 2173 2174 /* update the real prog size */ 2175 prog_size = sizeof(u32) * ctx.idx; 2176 2177 /* And we're done. */ 2178 if (bpf_jit_enable > 1) 2179 bpf_jit_dump(prog->len, prog_size, 2, ctx.image); 2180 2181 if (!prog->is_func || extra_pass) { 2182 /* The jited image may shrink since the jited result for 2183 * BPF_CALL to subprog may be changed from indirect call 2184 * to direct call. 2185 */ 2186 if (extra_pass && ctx.idx > jit_data->ctx.idx) { 2187 pr_err_once("multi-func JIT bug %d > %d\n", 2188 ctx.idx, jit_data->ctx.idx); 2189 prog->bpf_func = NULL; 2190 prog->jited = 0; 2191 prog->jited_len = 0; 2192 goto out_free_hdr; 2193 } 2194 if (WARN_ON(bpf_jit_binary_pack_finalize(ro_header, header))) { 2195 /* ro_header has been freed */ 2196 ro_header = NULL; 2197 prog = orig_prog; 2198 goto out_off; 2199 } 2200 /* 2201 * The instructions have now been copied to the ROX region from 2202 * where they will execute. Now the data cache has to be cleaned to 2203 * the PoU and the I-cache has to be invalidated for the VAs. 2204 */ 2205 bpf_flush_icache(ro_header, ctx.ro_image + ctx.idx); 2206 } else { 2207 jit_data->ctx = ctx; 2208 jit_data->ro_image = ro_image_ptr; 2209 jit_data->header = header; 2210 jit_data->ro_header = ro_header; 2211 } 2212 2213 prog->bpf_func = (void *)ctx.ro_image + cfi_get_offset(); 2214 prog->jited = 1; 2215 prog->jited_len = prog_size - cfi_get_offset(); 2216 2217 if (!prog->is_func || extra_pass) { 2218 int i; 2219 2220 /* offset[prog->len] is the size of program */ 2221 for (i = 0; i <= prog->len; i++) 2222 ctx.offset[i] *= AARCH64_INSN_SIZE; 2223 bpf_prog_fill_jited_linfo(prog, ctx.offset + 1); 2224 out_off: 2225 if (!ro_header && priv_stack_ptr) { 2226 free_percpu(priv_stack_ptr); 2227 prog->aux->priv_stack_ptr = NULL; 2228 } 2229 kvfree(ctx.offset); 2230 out_priv_stack: 2231 kfree(jit_data); 2232 prog->aux->jit_data = NULL; 2233 } 2234 out: 2235 if (tmp_blinded) 2236 bpf_jit_prog_release_other(prog, prog == orig_prog ? 2237 tmp : orig_prog); 2238 return prog; 2239 2240 out_free_hdr: 2241 if (header) { 2242 bpf_arch_text_copy(&ro_header->size, &header->size, 2243 sizeof(header->size)); 2244 bpf_jit_binary_pack_free(ro_header, header); 2245 } 2246 goto out_off; 2247 } 2248 2249 bool bpf_jit_supports_private_stack(void) 2250 { 2251 return true; 2252 } 2253 2254 bool bpf_jit_supports_kfunc_call(void) 2255 { 2256 return true; 2257 } 2258 2259 void *bpf_arch_text_copy(void *dst, void *src, size_t len) 2260 { 2261 if (!aarch64_insn_copy(dst, src, len)) 2262 return ERR_PTR(-EINVAL); 2263 return dst; 2264 } 2265 2266 u64 bpf_jit_alloc_exec_limit(void) 2267 { 2268 return VMALLOC_END - VMALLOC_START; 2269 } 2270 2271 /* Indicate the JIT backend supports mixing bpf2bpf and tailcalls. */ 2272 bool bpf_jit_supports_subprog_tailcalls(void) 2273 { 2274 return true; 2275 } 2276 2277 static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_link *l, 2278 int bargs_off, int retval_off, int run_ctx_off, 2279 bool save_ret) 2280 { 2281 __le32 *branch; 2282 u64 enter_prog; 2283 u64 exit_prog; 2284 struct bpf_prog *p = l->link.prog; 2285 int cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie); 2286 2287 enter_prog = (u64)bpf_trampoline_enter(p); 2288 exit_prog = (u64)bpf_trampoline_exit(p); 2289 2290 if (l->cookie == 0) { 2291 /* if cookie is zero, one instruction is enough to store it */ 2292 emit(A64_STR64I(A64_ZR, A64_SP, run_ctx_off + cookie_off), ctx); 2293 } else { 2294 emit_a64_mov_i64(A64_R(10), l->cookie, ctx); 2295 emit(A64_STR64I(A64_R(10), A64_SP, run_ctx_off + cookie_off), 2296 ctx); 2297 } 2298 2299 /* save p to callee saved register x19 to avoid loading p with mov_i64 2300 * each time. 2301 */ 2302 emit_addr_mov_i64(A64_R(19), (const u64)p, ctx); 2303 2304 /* arg1: prog */ 2305 emit(A64_MOV(1, A64_R(0), A64_R(19)), ctx); 2306 /* arg2: &run_ctx */ 2307 emit(A64_ADD_I(1, A64_R(1), A64_SP, run_ctx_off), ctx); 2308 2309 emit_call(enter_prog, ctx); 2310 2311 /* save return value to callee saved register x20 */ 2312 emit(A64_MOV(1, A64_R(20), A64_R(0)), ctx); 2313 2314 /* if (__bpf_prog_enter(prog) == 0) 2315 * goto skip_exec_of_prog; 2316 */ 2317 branch = ctx->image + ctx->idx; 2318 emit(A64_NOP, ctx); 2319 2320 emit(A64_ADD_I(1, A64_R(0), A64_SP, bargs_off), ctx); 2321 if (!p->jited) 2322 emit_addr_mov_i64(A64_R(1), (const u64)p->insnsi, ctx); 2323 2324 emit_call((const u64)p->bpf_func, ctx); 2325 2326 if (save_ret) 2327 emit(A64_STR64I(A64_R(0), A64_SP, retval_off), ctx); 2328 2329 if (ctx->image) { 2330 int offset = &ctx->image[ctx->idx] - branch; 2331 *branch = cpu_to_le32(A64_CBZ(1, A64_R(0), offset)); 2332 } 2333 2334 /* arg1: prog */ 2335 emit(A64_MOV(1, A64_R(0), A64_R(19)), ctx); 2336 /* arg2: start time */ 2337 emit(A64_MOV(1, A64_R(1), A64_R(20)), ctx); 2338 /* arg3: &run_ctx */ 2339 emit(A64_ADD_I(1, A64_R(2), A64_SP, run_ctx_off), ctx); 2340 2341 emit_call(exit_prog, ctx); 2342 } 2343 2344 static void invoke_bpf_mod_ret(struct jit_ctx *ctx, struct bpf_tramp_links *tl, 2345 int bargs_off, int retval_off, int run_ctx_off, 2346 __le32 **branches) 2347 { 2348 int i; 2349 2350 /* The first fmod_ret program will receive a garbage return value. 2351 * Set this to 0 to avoid confusing the program. 2352 */ 2353 emit(A64_STR64I(A64_ZR, A64_SP, retval_off), ctx); 2354 for (i = 0; i < tl->nr_links; i++) { 2355 invoke_bpf_prog(ctx, tl->links[i], bargs_off, retval_off, 2356 run_ctx_off, true); 2357 /* if (*(u64 *)(sp + retval_off) != 0) 2358 * goto do_fexit; 2359 */ 2360 emit(A64_LDR64I(A64_R(10), A64_SP, retval_off), ctx); 2361 /* Save the location of branch, and generate a nop. 2362 * This nop will be replaced with a cbnz later. 2363 */ 2364 branches[i] = ctx->image + ctx->idx; 2365 emit(A64_NOP, ctx); 2366 } 2367 } 2368 2369 struct arg_aux { 2370 /* how many args are passed through registers, the rest of the args are 2371 * passed through stack 2372 */ 2373 int args_in_regs; 2374 /* how many registers are used to pass arguments */ 2375 int regs_for_args; 2376 /* how much stack is used for additional args passed to bpf program 2377 * that did not fit in original function registers 2378 */ 2379 int bstack_for_args; 2380 /* home much stack is used for additional args passed to the 2381 * original function when called from trampoline (this one needs 2382 * arguments to be properly aligned) 2383 */ 2384 int ostack_for_args; 2385 }; 2386 2387 static int calc_arg_aux(const struct btf_func_model *m, 2388 struct arg_aux *a) 2389 { 2390 int stack_slots, nregs, slots, i; 2391 2392 /* verifier ensures m->nr_args <= MAX_BPF_FUNC_ARGS */ 2393 for (i = 0, nregs = 0; i < m->nr_args; i++) { 2394 slots = (m->arg_size[i] + 7) / 8; 2395 if (nregs + slots <= 8) /* passed through register ? */ 2396 nregs += slots; 2397 else 2398 break; 2399 } 2400 2401 a->args_in_regs = i; 2402 a->regs_for_args = nregs; 2403 a->ostack_for_args = 0; 2404 a->bstack_for_args = 0; 2405 2406 /* the rest arguments are passed through stack */ 2407 for (; i < m->nr_args; i++) { 2408 stack_slots = (m->arg_size[i] + 7) / 8; 2409 a->bstack_for_args += stack_slots * 8; 2410 a->ostack_for_args = a->ostack_for_args + stack_slots * 8; 2411 } 2412 2413 return 0; 2414 } 2415 2416 static void clear_garbage(struct jit_ctx *ctx, int reg, int effective_bytes) 2417 { 2418 if (effective_bytes) { 2419 int garbage_bits = 64 - 8 * effective_bytes; 2420 #ifdef CONFIG_CPU_BIG_ENDIAN 2421 /* garbage bits are at the right end */ 2422 emit(A64_LSR(1, reg, reg, garbage_bits), ctx); 2423 emit(A64_LSL(1, reg, reg, garbage_bits), ctx); 2424 #else 2425 /* garbage bits are at the left end */ 2426 emit(A64_LSL(1, reg, reg, garbage_bits), ctx); 2427 emit(A64_LSR(1, reg, reg, garbage_bits), ctx); 2428 #endif 2429 } 2430 } 2431 2432 static void save_args(struct jit_ctx *ctx, int bargs_off, int oargs_off, 2433 const struct btf_func_model *m, 2434 const struct arg_aux *a, 2435 bool for_call_origin) 2436 { 2437 int i; 2438 int reg; 2439 int doff; 2440 int soff; 2441 int slots; 2442 u8 tmp = bpf2a64[TMP_REG_1]; 2443 2444 /* store arguments to the stack for the bpf program, or restore 2445 * arguments from stack for the original function 2446 */ 2447 for (reg = 0; reg < a->regs_for_args; reg++) { 2448 emit(for_call_origin ? 2449 A64_LDR64I(reg, A64_SP, bargs_off) : 2450 A64_STR64I(reg, A64_SP, bargs_off), 2451 ctx); 2452 bargs_off += 8; 2453 } 2454 2455 soff = 32; /* on stack arguments start from FP + 32 */ 2456 doff = (for_call_origin ? oargs_off : bargs_off); 2457 2458 /* save on stack arguments */ 2459 for (i = a->args_in_regs; i < m->nr_args; i++) { 2460 slots = (m->arg_size[i] + 7) / 8; 2461 /* verifier ensures arg_size <= 16, so slots equals 1 or 2 */ 2462 while (slots-- > 0) { 2463 emit(A64_LDR64I(tmp, A64_FP, soff), ctx); 2464 /* if there is unused space in the last slot, clear 2465 * the garbage contained in the space. 2466 */ 2467 if (slots == 0 && !for_call_origin) 2468 clear_garbage(ctx, tmp, m->arg_size[i] % 8); 2469 emit(A64_STR64I(tmp, A64_SP, doff), ctx); 2470 soff += 8; 2471 doff += 8; 2472 } 2473 } 2474 } 2475 2476 static void restore_args(struct jit_ctx *ctx, int bargs_off, int nregs) 2477 { 2478 int reg; 2479 2480 for (reg = 0; reg < nregs; reg++) { 2481 emit(A64_LDR64I(reg, A64_SP, bargs_off), ctx); 2482 bargs_off += 8; 2483 } 2484 } 2485 2486 static bool is_struct_ops_tramp(const struct bpf_tramp_links *fentry_links) 2487 { 2488 return fentry_links->nr_links == 1 && 2489 fentry_links->links[0]->link.type == BPF_LINK_TYPE_STRUCT_OPS; 2490 } 2491 2492 /* Based on the x86's implementation of arch_prepare_bpf_trampoline(). 2493 * 2494 * bpf prog and function entry before bpf trampoline hooked: 2495 * mov x9, lr 2496 * nop 2497 * 2498 * bpf prog and function entry after bpf trampoline hooked: 2499 * mov x9, lr 2500 * bl <bpf_trampoline or plt> 2501 * 2502 */ 2503 static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im, 2504 struct bpf_tramp_links *tlinks, void *func_addr, 2505 const struct btf_func_model *m, 2506 const struct arg_aux *a, 2507 u32 flags) 2508 { 2509 int i; 2510 int stack_size; 2511 int retaddr_off; 2512 int regs_off; 2513 int retval_off; 2514 int bargs_off; 2515 int nfuncargs_off; 2516 int ip_off; 2517 int run_ctx_off; 2518 int oargs_off; 2519 int nfuncargs; 2520 struct bpf_tramp_links *fentry = &tlinks[BPF_TRAMP_FENTRY]; 2521 struct bpf_tramp_links *fexit = &tlinks[BPF_TRAMP_FEXIT]; 2522 struct bpf_tramp_links *fmod_ret = &tlinks[BPF_TRAMP_MODIFY_RETURN]; 2523 bool save_ret; 2524 __le32 **branches = NULL; 2525 bool is_struct_ops = is_struct_ops_tramp(fentry); 2526 2527 /* trampoline stack layout: 2528 * [ parent ip ] 2529 * [ FP ] 2530 * SP + retaddr_off [ self ip ] 2531 * [ FP ] 2532 * 2533 * [ padding ] align SP to multiples of 16 2534 * 2535 * [ x20 ] callee saved reg x20 2536 * SP + regs_off [ x19 ] callee saved reg x19 2537 * 2538 * SP + retval_off [ return value ] BPF_TRAMP_F_CALL_ORIG or 2539 * BPF_TRAMP_F_RET_FENTRY_RET 2540 * [ arg reg N ] 2541 * [ ... ] 2542 * SP + bargs_off [ arg reg 1 ] for bpf 2543 * 2544 * SP + nfuncargs_off [ arg regs count ] 2545 * 2546 * SP + ip_off [ traced function ] BPF_TRAMP_F_IP_ARG flag 2547 * 2548 * SP + run_ctx_off [ bpf_tramp_run_ctx ] 2549 * 2550 * [ stack arg N ] 2551 * [ ... ] 2552 * SP + oargs_off [ stack arg 1 ] for original func 2553 */ 2554 2555 stack_size = 0; 2556 oargs_off = stack_size; 2557 if (flags & BPF_TRAMP_F_CALL_ORIG) 2558 stack_size += a->ostack_for_args; 2559 2560 run_ctx_off = stack_size; 2561 /* room for bpf_tramp_run_ctx */ 2562 stack_size += round_up(sizeof(struct bpf_tramp_run_ctx), 8); 2563 2564 ip_off = stack_size; 2565 /* room for IP address argument */ 2566 if (flags & BPF_TRAMP_F_IP_ARG) 2567 stack_size += 8; 2568 2569 nfuncargs_off = stack_size; 2570 /* room for args count */ 2571 stack_size += 8; 2572 2573 bargs_off = stack_size; 2574 /* room for args */ 2575 nfuncargs = a->regs_for_args + a->bstack_for_args / 8; 2576 stack_size += 8 * nfuncargs; 2577 2578 /* room for return value */ 2579 retval_off = stack_size; 2580 save_ret = flags & (BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_RET_FENTRY_RET); 2581 if (save_ret) 2582 stack_size += 8; 2583 2584 /* room for callee saved registers, currently x19 and x20 are used */ 2585 regs_off = stack_size; 2586 stack_size += 16; 2587 2588 /* round up to multiples of 16 to avoid SPAlignmentFault */ 2589 stack_size = round_up(stack_size, 16); 2590 2591 /* return address locates above FP */ 2592 retaddr_off = stack_size + 8; 2593 2594 if (flags & BPF_TRAMP_F_INDIRECT) { 2595 /* 2596 * Indirect call for bpf_struct_ops 2597 */ 2598 emit_kcfi(cfi_get_func_hash(func_addr), ctx); 2599 } 2600 /* bpf trampoline may be invoked by 3 instruction types: 2601 * 1. bl, attached to bpf prog or kernel function via short jump 2602 * 2. br, attached to bpf prog or kernel function via long jump 2603 * 3. blr, working as a function pointer, used by struct_ops. 2604 * So BTI_JC should used here to support both br and blr. 2605 */ 2606 emit_bti(A64_BTI_JC, ctx); 2607 2608 /* x9 is not set for struct_ops */ 2609 if (!is_struct_ops) { 2610 /* frame for parent function */ 2611 emit(A64_PUSH(A64_FP, A64_R(9), A64_SP), ctx); 2612 emit(A64_MOV(1, A64_FP, A64_SP), ctx); 2613 } 2614 2615 /* frame for patched function for tracing, or caller for struct_ops */ 2616 emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx); 2617 emit(A64_MOV(1, A64_FP, A64_SP), ctx); 2618 2619 /* allocate stack space */ 2620 emit(A64_SUB_I(1, A64_SP, A64_SP, stack_size), ctx); 2621 2622 if (flags & BPF_TRAMP_F_IP_ARG) { 2623 /* save ip address of the traced function */ 2624 emit_addr_mov_i64(A64_R(10), (const u64)func_addr, ctx); 2625 emit(A64_STR64I(A64_R(10), A64_SP, ip_off), ctx); 2626 } 2627 2628 /* save arg regs count*/ 2629 emit(A64_MOVZ(1, A64_R(10), nfuncargs, 0), ctx); 2630 emit(A64_STR64I(A64_R(10), A64_SP, nfuncargs_off), ctx); 2631 2632 /* save args for bpf */ 2633 save_args(ctx, bargs_off, oargs_off, m, a, false); 2634 2635 /* save callee saved registers */ 2636 emit(A64_STR64I(A64_R(19), A64_SP, regs_off), ctx); 2637 emit(A64_STR64I(A64_R(20), A64_SP, regs_off + 8), ctx); 2638 2639 if (flags & BPF_TRAMP_F_CALL_ORIG) { 2640 /* for the first pass, assume the worst case */ 2641 if (!ctx->image) 2642 ctx->idx += 4; 2643 else 2644 emit_a64_mov_i64(A64_R(0), (const u64)im, ctx); 2645 emit_call((const u64)__bpf_tramp_enter, ctx); 2646 } 2647 2648 for (i = 0; i < fentry->nr_links; i++) 2649 invoke_bpf_prog(ctx, fentry->links[i], bargs_off, 2650 retval_off, run_ctx_off, 2651 flags & BPF_TRAMP_F_RET_FENTRY_RET); 2652 2653 if (fmod_ret->nr_links) { 2654 branches = kcalloc(fmod_ret->nr_links, sizeof(__le32 *), 2655 GFP_KERNEL); 2656 if (!branches) 2657 return -ENOMEM; 2658 2659 invoke_bpf_mod_ret(ctx, fmod_ret, bargs_off, retval_off, 2660 run_ctx_off, branches); 2661 } 2662 2663 if (flags & BPF_TRAMP_F_CALL_ORIG) { 2664 /* save args for original func */ 2665 save_args(ctx, bargs_off, oargs_off, m, a, true); 2666 /* call original func */ 2667 emit(A64_LDR64I(A64_R(10), A64_SP, retaddr_off), ctx); 2668 emit(A64_ADR(A64_LR, AARCH64_INSN_SIZE * 2), ctx); 2669 emit(A64_RET(A64_R(10)), ctx); 2670 /* store return value */ 2671 emit(A64_STR64I(A64_R(0), A64_SP, retval_off), ctx); 2672 /* reserve a nop for bpf_tramp_image_put */ 2673 im->ip_after_call = ctx->ro_image + ctx->idx; 2674 emit(A64_NOP, ctx); 2675 } 2676 2677 /* update the branches saved in invoke_bpf_mod_ret with cbnz */ 2678 for (i = 0; i < fmod_ret->nr_links && ctx->image != NULL; i++) { 2679 int offset = &ctx->image[ctx->idx] - branches[i]; 2680 *branches[i] = cpu_to_le32(A64_CBNZ(1, A64_R(10), offset)); 2681 } 2682 2683 for (i = 0; i < fexit->nr_links; i++) 2684 invoke_bpf_prog(ctx, fexit->links[i], bargs_off, retval_off, 2685 run_ctx_off, false); 2686 2687 if (flags & BPF_TRAMP_F_CALL_ORIG) { 2688 im->ip_epilogue = ctx->ro_image + ctx->idx; 2689 /* for the first pass, assume the worst case */ 2690 if (!ctx->image) 2691 ctx->idx += 4; 2692 else 2693 emit_a64_mov_i64(A64_R(0), (const u64)im, ctx); 2694 emit_call((const u64)__bpf_tramp_exit, ctx); 2695 } 2696 2697 if (flags & BPF_TRAMP_F_RESTORE_REGS) 2698 restore_args(ctx, bargs_off, a->regs_for_args); 2699 2700 /* restore callee saved register x19 and x20 */ 2701 emit(A64_LDR64I(A64_R(19), A64_SP, regs_off), ctx); 2702 emit(A64_LDR64I(A64_R(20), A64_SP, regs_off + 8), ctx); 2703 2704 if (save_ret) 2705 emit(A64_LDR64I(A64_R(0), A64_SP, retval_off), ctx); 2706 2707 /* reset SP */ 2708 emit(A64_MOV(1, A64_SP, A64_FP), ctx); 2709 2710 if (is_struct_ops) { 2711 emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx); 2712 emit(A64_RET(A64_LR), ctx); 2713 } else { 2714 /* pop frames */ 2715 emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx); 2716 emit(A64_POP(A64_FP, A64_R(9), A64_SP), ctx); 2717 2718 if (flags & BPF_TRAMP_F_SKIP_FRAME) { 2719 /* skip patched function, return to parent */ 2720 emit(A64_MOV(1, A64_LR, A64_R(9)), ctx); 2721 emit(A64_RET(A64_R(9)), ctx); 2722 } else { 2723 /* return to patched function */ 2724 emit(A64_MOV(1, A64_R(10), A64_LR), ctx); 2725 emit(A64_MOV(1, A64_LR, A64_R(9)), ctx); 2726 emit(A64_RET(A64_R(10)), ctx); 2727 } 2728 } 2729 2730 kfree(branches); 2731 2732 return ctx->idx; 2733 } 2734 2735 int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags, 2736 struct bpf_tramp_links *tlinks, void *func_addr) 2737 { 2738 struct jit_ctx ctx = { 2739 .image = NULL, 2740 .idx = 0, 2741 }; 2742 struct bpf_tramp_image im; 2743 struct arg_aux aaux; 2744 int ret; 2745 2746 ret = calc_arg_aux(m, &aaux); 2747 if (ret < 0) 2748 return ret; 2749 2750 ret = prepare_trampoline(&ctx, &im, tlinks, func_addr, m, &aaux, flags); 2751 if (ret < 0) 2752 return ret; 2753 2754 return ret < 0 ? ret : ret * AARCH64_INSN_SIZE; 2755 } 2756 2757 void *arch_alloc_bpf_trampoline(unsigned int size) 2758 { 2759 return bpf_prog_pack_alloc(size, jit_fill_hole); 2760 } 2761 2762 void arch_free_bpf_trampoline(void *image, unsigned int size) 2763 { 2764 bpf_prog_pack_free(image, size); 2765 } 2766 2767 int arch_protect_bpf_trampoline(void *image, unsigned int size) 2768 { 2769 return 0; 2770 } 2771 2772 int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *ro_image, 2773 void *ro_image_end, const struct btf_func_model *m, 2774 u32 flags, struct bpf_tramp_links *tlinks, 2775 void *func_addr) 2776 { 2777 u32 size = ro_image_end - ro_image; 2778 struct arg_aux aaux; 2779 void *image, *tmp; 2780 int ret; 2781 2782 /* image doesn't need to be in module memory range, so we can 2783 * use kvmalloc. 2784 */ 2785 image = kvmalloc(size, GFP_KERNEL); 2786 if (!image) 2787 return -ENOMEM; 2788 2789 struct jit_ctx ctx = { 2790 .image = image, 2791 .ro_image = ro_image, 2792 .idx = 0, 2793 .write = true, 2794 }; 2795 2796 2797 jit_fill_hole(image, (unsigned int)(ro_image_end - ro_image)); 2798 ret = calc_arg_aux(m, &aaux); 2799 if (ret) 2800 goto out; 2801 ret = prepare_trampoline(&ctx, im, tlinks, func_addr, m, &aaux, flags); 2802 2803 if (ret > 0 && validate_code(&ctx) < 0) { 2804 ret = -EINVAL; 2805 goto out; 2806 } 2807 2808 if (ret > 0) 2809 ret *= AARCH64_INSN_SIZE; 2810 2811 tmp = bpf_arch_text_copy(ro_image, image, size); 2812 if (IS_ERR(tmp)) { 2813 ret = PTR_ERR(tmp); 2814 goto out; 2815 } 2816 2817 out: 2818 kvfree(image); 2819 return ret; 2820 } 2821 2822 static bool is_long_jump(void *ip, void *target) 2823 { 2824 long offset; 2825 2826 /* NULL target means this is a NOP */ 2827 if (!target) 2828 return false; 2829 2830 offset = (long)target - (long)ip; 2831 return offset < -SZ_128M || offset >= SZ_128M; 2832 } 2833 2834 static int gen_branch_or_nop(enum aarch64_insn_branch_type type, void *ip, 2835 void *addr, void *plt, u32 *insn) 2836 { 2837 void *target; 2838 2839 if (!addr) { 2840 *insn = aarch64_insn_gen_nop(); 2841 return 0; 2842 } 2843 2844 if (is_long_jump(ip, addr)) 2845 target = plt; 2846 else 2847 target = addr; 2848 2849 *insn = aarch64_insn_gen_branch_imm((unsigned long)ip, 2850 (unsigned long)target, 2851 type); 2852 2853 return *insn != AARCH64_BREAK_FAULT ? 0 : -EFAULT; 2854 } 2855 2856 /* Replace the branch instruction from @ip to @old_addr in a bpf prog or a bpf 2857 * trampoline with the branch instruction from @ip to @new_addr. If @old_addr 2858 * or @new_addr is NULL, the old or new instruction is NOP. 2859 * 2860 * When @ip is the bpf prog entry, a bpf trampoline is being attached or 2861 * detached. Since bpf trampoline and bpf prog are allocated separately with 2862 * vmalloc, the address distance may exceed 128MB, the maximum branch range. 2863 * So long jump should be handled. 2864 * 2865 * When a bpf prog is constructed, a plt pointing to empty trampoline 2866 * dummy_tramp is placed at the end: 2867 * 2868 * bpf_prog: 2869 * mov x9, lr 2870 * nop // patchsite 2871 * ... 2872 * ret 2873 * 2874 * plt: 2875 * ldr x10, target 2876 * br x10 2877 * target: 2878 * .quad dummy_tramp // plt target 2879 * 2880 * This is also the state when no trampoline is attached. 2881 * 2882 * When a short-jump bpf trampoline is attached, the patchsite is patched 2883 * to a bl instruction to the trampoline directly: 2884 * 2885 * bpf_prog: 2886 * mov x9, lr 2887 * bl <short-jump bpf trampoline address> // patchsite 2888 * ... 2889 * ret 2890 * 2891 * plt: 2892 * ldr x10, target 2893 * br x10 2894 * target: 2895 * .quad dummy_tramp // plt target 2896 * 2897 * When a long-jump bpf trampoline is attached, the plt target is filled with 2898 * the trampoline address and the patchsite is patched to a bl instruction to 2899 * the plt: 2900 * 2901 * bpf_prog: 2902 * mov x9, lr 2903 * bl plt // patchsite 2904 * ... 2905 * ret 2906 * 2907 * plt: 2908 * ldr x10, target 2909 * br x10 2910 * target: 2911 * .quad <long-jump bpf trampoline address> // plt target 2912 * 2913 * The dummy_tramp is used to prevent another CPU from jumping to unknown 2914 * locations during the patching process, making the patching process easier. 2915 */ 2916 int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type poke_type, 2917 void *old_addr, void *new_addr) 2918 { 2919 int ret; 2920 u32 old_insn; 2921 u32 new_insn; 2922 u32 replaced; 2923 struct bpf_plt *plt = NULL; 2924 unsigned long size = 0UL; 2925 unsigned long offset = ~0UL; 2926 enum aarch64_insn_branch_type branch_type; 2927 char namebuf[KSYM_NAME_LEN]; 2928 void *image = NULL; 2929 u64 plt_target = 0ULL; 2930 bool poking_bpf_entry; 2931 2932 if (!__bpf_address_lookup((unsigned long)ip, &size, &offset, namebuf)) 2933 /* Only poking bpf text is supported. Since kernel function 2934 * entry is set up by ftrace, we reply on ftrace to poke kernel 2935 * functions. 2936 */ 2937 return -ENOTSUPP; 2938 2939 image = ip - offset; 2940 /* zero offset means we're poking bpf prog entry */ 2941 poking_bpf_entry = (offset == 0UL); 2942 2943 /* bpf prog entry, find plt and the real patchsite */ 2944 if (poking_bpf_entry) { 2945 /* plt locates at the end of bpf prog */ 2946 plt = image + size - PLT_TARGET_OFFSET; 2947 2948 /* skip to the nop instruction in bpf prog entry: 2949 * bti c // if BTI enabled 2950 * mov x9, x30 2951 * nop 2952 */ 2953 ip = image + POKE_OFFSET * AARCH64_INSN_SIZE; 2954 } 2955 2956 /* long jump is only possible at bpf prog entry */ 2957 if (WARN_ON((is_long_jump(ip, new_addr) || is_long_jump(ip, old_addr)) && 2958 !poking_bpf_entry)) 2959 return -EINVAL; 2960 2961 if (poke_type == BPF_MOD_CALL) 2962 branch_type = AARCH64_INSN_BRANCH_LINK; 2963 else 2964 branch_type = AARCH64_INSN_BRANCH_NOLINK; 2965 2966 if (gen_branch_or_nop(branch_type, ip, old_addr, plt, &old_insn) < 0) 2967 return -EFAULT; 2968 2969 if (gen_branch_or_nop(branch_type, ip, new_addr, plt, &new_insn) < 0) 2970 return -EFAULT; 2971 2972 if (is_long_jump(ip, new_addr)) 2973 plt_target = (u64)new_addr; 2974 else if (is_long_jump(ip, old_addr)) 2975 /* if the old target is a long jump and the new target is not, 2976 * restore the plt target to dummy_tramp, so there is always a 2977 * legal and harmless address stored in plt target, and we'll 2978 * never jump from plt to an unknown place. 2979 */ 2980 plt_target = (u64)&dummy_tramp; 2981 2982 if (plt_target) { 2983 /* non-zero plt_target indicates we're patching a bpf prog, 2984 * which is read only. 2985 */ 2986 if (set_memory_rw(PAGE_MASK & ((uintptr_t)&plt->target), 1)) 2987 return -EFAULT; 2988 WRITE_ONCE(plt->target, plt_target); 2989 set_memory_ro(PAGE_MASK & ((uintptr_t)&plt->target), 1); 2990 /* since plt target points to either the new trampoline 2991 * or dummy_tramp, even if another CPU reads the old plt 2992 * target value before fetching the bl instruction to plt, 2993 * it will be brought back by dummy_tramp, so no barrier is 2994 * required here. 2995 */ 2996 } 2997 2998 /* if the old target and the new target are both long jumps, no 2999 * patching is required 3000 */ 3001 if (old_insn == new_insn) 3002 return 0; 3003 3004 mutex_lock(&text_mutex); 3005 if (aarch64_insn_read(ip, &replaced)) { 3006 ret = -EFAULT; 3007 goto out; 3008 } 3009 3010 if (replaced != old_insn) { 3011 ret = -EFAULT; 3012 goto out; 3013 } 3014 3015 /* We call aarch64_insn_patch_text_nosync() to replace instruction 3016 * atomically, so no other CPUs will fetch a half-new and half-old 3017 * instruction. But there is chance that another CPU executes the 3018 * old instruction after the patching operation finishes (e.g., 3019 * pipeline not flushed, or icache not synchronized yet). 3020 * 3021 * 1. when a new trampoline is attached, it is not a problem for 3022 * different CPUs to jump to different trampolines temporarily. 3023 * 3024 * 2. when an old trampoline is freed, we should wait for all other 3025 * CPUs to exit the trampoline and make sure the trampoline is no 3026 * longer reachable, since bpf_tramp_image_put() function already 3027 * uses percpu_ref and task-based rcu to do the sync, no need to call 3028 * the sync version here, see bpf_tramp_image_put() for details. 3029 */ 3030 ret = aarch64_insn_patch_text_nosync(ip, new_insn); 3031 out: 3032 mutex_unlock(&text_mutex); 3033 3034 return ret; 3035 } 3036 3037 bool bpf_jit_supports_ptr_xchg(void) 3038 { 3039 return true; 3040 } 3041 3042 bool bpf_jit_supports_exceptions(void) 3043 { 3044 /* We unwind through both kernel frames starting from within bpf_throw 3045 * call and BPF frames. Therefore we require FP unwinder to be enabled 3046 * to walk kernel frames and reach BPF frames in the stack trace. 3047 * ARM64 kernel is aways compiled with CONFIG_FRAME_POINTER=y 3048 */ 3049 return true; 3050 } 3051 3052 bool bpf_jit_supports_arena(void) 3053 { 3054 return true; 3055 } 3056 3057 bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena) 3058 { 3059 if (!in_arena) 3060 return true; 3061 switch (insn->code) { 3062 case BPF_STX | BPF_ATOMIC | BPF_W: 3063 case BPF_STX | BPF_ATOMIC | BPF_DW: 3064 if (!bpf_atomic_is_load_store(insn) && 3065 !cpus_have_cap(ARM64_HAS_LSE_ATOMICS)) 3066 return false; 3067 } 3068 return true; 3069 } 3070 3071 bool bpf_jit_supports_percpu_insn(void) 3072 { 3073 return true; 3074 } 3075 3076 bool bpf_jit_bypass_spec_v4(void) 3077 { 3078 /* In case of arm64, we rely on the firmware mitigation of Speculative 3079 * Store Bypass as controlled via the ssbd kernel parameter. Whenever 3080 * the mitigation is enabled, it works for all of the kernel code with 3081 * no need to provide any additional instructions. Therefore, skip 3082 * inserting nospec insns against Spectre v4. 3083 */ 3084 return true; 3085 } 3086 3087 bool bpf_jit_supports_timed_may_goto(void) 3088 { 3089 return true; 3090 } 3091 3092 bool bpf_jit_inlines_helper_call(s32 imm) 3093 { 3094 switch (imm) { 3095 case BPF_FUNC_get_smp_processor_id: 3096 case BPF_FUNC_get_current_task: 3097 case BPF_FUNC_get_current_task_btf: 3098 return true; 3099 default: 3100 return false; 3101 } 3102 } 3103 3104 void bpf_jit_free(struct bpf_prog *prog) 3105 { 3106 if (prog->jited) { 3107 struct arm64_jit_data *jit_data = prog->aux->jit_data; 3108 struct bpf_binary_header *hdr; 3109 void __percpu *priv_stack_ptr; 3110 int priv_stack_alloc_sz; 3111 3112 /* 3113 * If we fail the final pass of JIT (from jit_subprogs), 3114 * the program may not be finalized yet. Call finalize here 3115 * before freeing it. 3116 */ 3117 if (jit_data) { 3118 bpf_jit_binary_pack_finalize(jit_data->ro_header, jit_data->header); 3119 kfree(jit_data); 3120 } 3121 prog->bpf_func -= cfi_get_offset(); 3122 hdr = bpf_jit_binary_pack_hdr(prog); 3123 bpf_jit_binary_pack_free(hdr, NULL); 3124 priv_stack_ptr = prog->aux->priv_stack_ptr; 3125 if (priv_stack_ptr) { 3126 priv_stack_alloc_sz = round_up(prog->aux->stack_depth, 16) + 3127 2 * PRIV_STACK_GUARD_SZ; 3128 priv_stack_check_guard(priv_stack_ptr, priv_stack_alloc_sz, prog); 3129 free_percpu(prog->aux->priv_stack_ptr); 3130 } 3131 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(prog)); 3132 } 3133 3134 bpf_prog_unlock_free(prog); 3135 } 3136