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