1e54bcde3SZi Shen Lim /* 2e54bcde3SZi Shen Lim * BPF JIT compiler for ARM64 3e54bcde3SZi Shen Lim * 4e54bcde3SZi Shen Lim * Copyright (C) 2014 Zi Shen Lim <zlim.lnx@gmail.com> 5e54bcde3SZi Shen Lim * 6e54bcde3SZi Shen Lim * This program is free software; you can redistribute it and/or modify 7e54bcde3SZi Shen Lim * it under the terms of the GNU General Public License version 2 as 8e54bcde3SZi Shen Lim * published by the Free Software Foundation. 9e54bcde3SZi Shen Lim * 10e54bcde3SZi Shen Lim * This program is distributed in the hope that it will be useful, 11e54bcde3SZi Shen Lim * but WITHOUT ANY WARRANTY; without even the implied warranty of 12e54bcde3SZi Shen Lim * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13e54bcde3SZi Shen Lim * GNU General Public License for more details. 14e54bcde3SZi Shen Lim * 15e54bcde3SZi Shen Lim * You should have received a copy of the GNU General Public License 16e54bcde3SZi Shen Lim * along with this program. If not, see <http://www.gnu.org/licenses/>. 17e54bcde3SZi Shen Lim */ 18e54bcde3SZi Shen Lim 19e54bcde3SZi Shen Lim #define pr_fmt(fmt) "bpf_jit: " fmt 20e54bcde3SZi Shen Lim 21e54bcde3SZi Shen Lim #include <linux/filter.h> 22e54bcde3SZi Shen Lim #include <linux/printk.h> 23e54bcde3SZi Shen Lim #include <linux/skbuff.h> 24e54bcde3SZi Shen Lim #include <linux/slab.h> 25b569c1c6SDaniel Borkmann 26e54bcde3SZi Shen Lim #include <asm/byteorder.h> 27e54bcde3SZi Shen Lim #include <asm/cacheflush.h> 28b569c1c6SDaniel Borkmann #include <asm/debug-monitors.h> 29e54bcde3SZi Shen Lim 30e54bcde3SZi Shen Lim #include "bpf_jit.h" 31e54bcde3SZi Shen Lim 32e54bcde3SZi Shen Lim int bpf_jit_enable __read_mostly; 33e54bcde3SZi Shen Lim 34e54bcde3SZi Shen Lim #define TMP_REG_1 (MAX_BPF_REG + 0) 35e54bcde3SZi Shen Lim #define TMP_REG_2 (MAX_BPF_REG + 1) 36e54bcde3SZi Shen Lim 37e54bcde3SZi Shen Lim /* Map BPF registers to A64 registers */ 38e54bcde3SZi Shen Lim static const int bpf2a64[] = { 39e54bcde3SZi Shen Lim /* return value from in-kernel function, and exit value from eBPF */ 40e54bcde3SZi Shen Lim [BPF_REG_0] = A64_R(7), 41e54bcde3SZi Shen Lim /* arguments from eBPF program to in-kernel function */ 42e54bcde3SZi Shen Lim [BPF_REG_1] = A64_R(0), 43e54bcde3SZi Shen Lim [BPF_REG_2] = A64_R(1), 44e54bcde3SZi Shen Lim [BPF_REG_3] = A64_R(2), 45e54bcde3SZi Shen Lim [BPF_REG_4] = A64_R(3), 46e54bcde3SZi Shen Lim [BPF_REG_5] = A64_R(4), 47e54bcde3SZi Shen Lim /* callee saved registers that in-kernel function will preserve */ 48e54bcde3SZi Shen Lim [BPF_REG_6] = A64_R(19), 49e54bcde3SZi Shen Lim [BPF_REG_7] = A64_R(20), 50e54bcde3SZi Shen Lim [BPF_REG_8] = A64_R(21), 51e54bcde3SZi Shen Lim [BPF_REG_9] = A64_R(22), 52e54bcde3SZi Shen Lim /* read-only frame pointer to access stack */ 53e54bcde3SZi Shen Lim [BPF_REG_FP] = A64_FP, 54e54bcde3SZi Shen Lim /* temporary register for internal BPF JIT */ 55e54bcde3SZi Shen Lim [TMP_REG_1] = A64_R(23), 56e54bcde3SZi Shen Lim [TMP_REG_2] = A64_R(24), 57e54bcde3SZi Shen Lim }; 58e54bcde3SZi Shen Lim 59e54bcde3SZi Shen Lim struct jit_ctx { 60e54bcde3SZi Shen Lim const struct bpf_prog *prog; 61e54bcde3SZi Shen Lim int idx; 62e54bcde3SZi Shen Lim int tmp_used; 63e54bcde3SZi Shen Lim int body_offset; 64e54bcde3SZi Shen Lim int *offset; 65e54bcde3SZi Shen Lim u32 *image; 66e54bcde3SZi Shen Lim }; 67e54bcde3SZi Shen Lim 68e54bcde3SZi Shen Lim static inline void emit(const u32 insn, struct jit_ctx *ctx) 69e54bcde3SZi Shen Lim { 70e54bcde3SZi Shen Lim if (ctx->image != NULL) 71e54bcde3SZi Shen Lim ctx->image[ctx->idx] = cpu_to_le32(insn); 72e54bcde3SZi Shen Lim 73e54bcde3SZi Shen Lim ctx->idx++; 74e54bcde3SZi Shen Lim } 75e54bcde3SZi Shen Lim 76e54bcde3SZi Shen Lim static inline void emit_a64_mov_i64(const int reg, const u64 val, 77e54bcde3SZi Shen Lim struct jit_ctx *ctx) 78e54bcde3SZi Shen Lim { 79e54bcde3SZi Shen Lim u64 tmp = val; 80e54bcde3SZi Shen Lim int shift = 0; 81e54bcde3SZi Shen Lim 82e54bcde3SZi Shen Lim emit(A64_MOVZ(1, reg, tmp & 0xffff, shift), ctx); 83e54bcde3SZi Shen Lim tmp >>= 16; 84e54bcde3SZi Shen Lim shift += 16; 85e54bcde3SZi Shen Lim while (tmp) { 86e54bcde3SZi Shen Lim if (tmp & 0xffff) 87e54bcde3SZi Shen Lim emit(A64_MOVK(1, reg, tmp & 0xffff, shift), ctx); 88e54bcde3SZi Shen Lim tmp >>= 16; 89e54bcde3SZi Shen Lim shift += 16; 90e54bcde3SZi Shen Lim } 91e54bcde3SZi Shen Lim } 92e54bcde3SZi Shen Lim 93e54bcde3SZi Shen Lim static inline void emit_a64_mov_i(const int is64, const int reg, 94e54bcde3SZi Shen Lim const s32 val, struct jit_ctx *ctx) 95e54bcde3SZi Shen Lim { 96e54bcde3SZi Shen Lim u16 hi = val >> 16; 97e54bcde3SZi Shen Lim u16 lo = val & 0xffff; 98e54bcde3SZi Shen Lim 99e54bcde3SZi Shen Lim if (hi & 0x8000) { 100e54bcde3SZi Shen Lim if (hi == 0xffff) { 101e54bcde3SZi Shen Lim emit(A64_MOVN(is64, reg, (u16)~lo, 0), ctx); 102e54bcde3SZi Shen Lim } else { 103e54bcde3SZi Shen Lim emit(A64_MOVN(is64, reg, (u16)~hi, 16), ctx); 104e54bcde3SZi Shen Lim emit(A64_MOVK(is64, reg, lo, 0), ctx); 105e54bcde3SZi Shen Lim } 106e54bcde3SZi Shen Lim } else { 107e54bcde3SZi Shen Lim emit(A64_MOVZ(is64, reg, lo, 0), ctx); 108e54bcde3SZi Shen Lim if (hi) 109e54bcde3SZi Shen Lim emit(A64_MOVK(is64, reg, hi, 16), ctx); 110e54bcde3SZi Shen Lim } 111e54bcde3SZi Shen Lim } 112e54bcde3SZi Shen Lim 113e54bcde3SZi Shen Lim static inline int bpf2a64_offset(int bpf_to, int bpf_from, 114e54bcde3SZi Shen Lim const struct jit_ctx *ctx) 115e54bcde3SZi Shen Lim { 116e54bcde3SZi Shen Lim int to = ctx->offset[bpf_to + 1]; 117e54bcde3SZi Shen Lim /* -1 to account for the Branch instruction */ 118e54bcde3SZi Shen Lim int from = ctx->offset[bpf_from + 1] - 1; 119e54bcde3SZi Shen Lim 120e54bcde3SZi Shen Lim return to - from; 121e54bcde3SZi Shen Lim } 122e54bcde3SZi Shen Lim 123b569c1c6SDaniel Borkmann static void jit_fill_hole(void *area, unsigned int size) 124b569c1c6SDaniel Borkmann { 125b569c1c6SDaniel Borkmann u32 *ptr; 126b569c1c6SDaniel Borkmann /* We are guaranteed to have aligned memory. */ 127b569c1c6SDaniel Borkmann for (ptr = area; size >= sizeof(u32); size -= sizeof(u32)) 128b569c1c6SDaniel Borkmann *ptr++ = cpu_to_le32(AARCH64_BREAK_FAULT); 129b569c1c6SDaniel Borkmann } 130b569c1c6SDaniel Borkmann 131e54bcde3SZi Shen Lim static inline int epilogue_offset(const struct jit_ctx *ctx) 132e54bcde3SZi Shen Lim { 133e54bcde3SZi Shen Lim int to = ctx->offset[ctx->prog->len - 1]; 134e54bcde3SZi Shen Lim int from = ctx->idx - ctx->body_offset; 135e54bcde3SZi Shen Lim 136e54bcde3SZi Shen Lim return to - from; 137e54bcde3SZi Shen Lim } 138e54bcde3SZi Shen Lim 139e54bcde3SZi Shen Lim /* Stack must be multiples of 16B */ 140e54bcde3SZi Shen Lim #define STACK_ALIGN(sz) (((sz) + 15) & ~15) 141e54bcde3SZi Shen Lim 142e54bcde3SZi Shen Lim static void build_prologue(struct jit_ctx *ctx) 143e54bcde3SZi Shen Lim { 144e54bcde3SZi Shen Lim const u8 r6 = bpf2a64[BPF_REG_6]; 145e54bcde3SZi Shen Lim const u8 r7 = bpf2a64[BPF_REG_7]; 146e54bcde3SZi Shen Lim const u8 r8 = bpf2a64[BPF_REG_8]; 147e54bcde3SZi Shen Lim const u8 r9 = bpf2a64[BPF_REG_9]; 148e54bcde3SZi Shen Lim const u8 fp = bpf2a64[BPF_REG_FP]; 149e54bcde3SZi Shen Lim const u8 ra = bpf2a64[BPF_REG_A]; 150e54bcde3SZi Shen Lim const u8 rx = bpf2a64[BPF_REG_X]; 151e54bcde3SZi Shen Lim const u8 tmp1 = bpf2a64[TMP_REG_1]; 152e54bcde3SZi Shen Lim const u8 tmp2 = bpf2a64[TMP_REG_2]; 153e54bcde3SZi Shen Lim int stack_size = MAX_BPF_STACK; 154e54bcde3SZi Shen Lim 155e54bcde3SZi Shen Lim stack_size += 4; /* extra for skb_copy_bits buffer */ 156e54bcde3SZi Shen Lim stack_size = STACK_ALIGN(stack_size); 157e54bcde3SZi Shen Lim 158e54bcde3SZi Shen Lim /* Save callee-saved register */ 159e54bcde3SZi Shen Lim emit(A64_PUSH(r6, r7, A64_SP), ctx); 160e54bcde3SZi Shen Lim emit(A64_PUSH(r8, r9, A64_SP), ctx); 161e54bcde3SZi Shen Lim if (ctx->tmp_used) 162e54bcde3SZi Shen Lim emit(A64_PUSH(tmp1, tmp2, A64_SP), ctx); 163e54bcde3SZi Shen Lim 164e54bcde3SZi Shen Lim /* Set up BPF stack */ 165e54bcde3SZi Shen Lim emit(A64_SUB_I(1, A64_SP, A64_SP, stack_size), ctx); 166e54bcde3SZi Shen Lim 167e54bcde3SZi Shen Lim /* Set up frame pointer */ 168e54bcde3SZi Shen Lim emit(A64_MOV(1, fp, A64_SP), ctx); 169e54bcde3SZi Shen Lim 170e54bcde3SZi Shen Lim /* Clear registers A and X */ 171e54bcde3SZi Shen Lim emit_a64_mov_i64(ra, 0, ctx); 172e54bcde3SZi Shen Lim emit_a64_mov_i64(rx, 0, ctx); 173e54bcde3SZi Shen Lim } 174e54bcde3SZi Shen Lim 175e54bcde3SZi Shen Lim static void build_epilogue(struct jit_ctx *ctx) 176e54bcde3SZi Shen Lim { 177e54bcde3SZi Shen Lim const u8 r0 = bpf2a64[BPF_REG_0]; 178e54bcde3SZi Shen Lim const u8 r6 = bpf2a64[BPF_REG_6]; 179e54bcde3SZi Shen Lim const u8 r7 = bpf2a64[BPF_REG_7]; 180e54bcde3SZi Shen Lim const u8 r8 = bpf2a64[BPF_REG_8]; 181e54bcde3SZi Shen Lim const u8 r9 = bpf2a64[BPF_REG_9]; 182e54bcde3SZi Shen Lim const u8 fp = bpf2a64[BPF_REG_FP]; 183e54bcde3SZi Shen Lim const u8 tmp1 = bpf2a64[TMP_REG_1]; 184e54bcde3SZi Shen Lim const u8 tmp2 = bpf2a64[TMP_REG_2]; 185e54bcde3SZi Shen Lim int stack_size = MAX_BPF_STACK; 186e54bcde3SZi Shen Lim 187e54bcde3SZi Shen Lim stack_size += 4; /* extra for skb_copy_bits buffer */ 188e54bcde3SZi Shen Lim stack_size = STACK_ALIGN(stack_size); 189e54bcde3SZi Shen Lim 190e54bcde3SZi Shen Lim /* We're done with BPF stack */ 191e54bcde3SZi Shen Lim emit(A64_ADD_I(1, A64_SP, A64_SP, stack_size), ctx); 192e54bcde3SZi Shen Lim 193e54bcde3SZi Shen Lim /* Restore callee-saved register */ 194e54bcde3SZi Shen Lim if (ctx->tmp_used) 195e54bcde3SZi Shen Lim emit(A64_POP(tmp1, tmp2, A64_SP), ctx); 196e54bcde3SZi Shen Lim emit(A64_POP(r8, r9, A64_SP), ctx); 197e54bcde3SZi Shen Lim emit(A64_POP(r6, r7, A64_SP), ctx); 198e54bcde3SZi Shen Lim 199e54bcde3SZi Shen Lim /* Restore frame pointer */ 200e54bcde3SZi Shen Lim emit(A64_MOV(1, fp, A64_SP), ctx); 201e54bcde3SZi Shen Lim 202e54bcde3SZi Shen Lim /* Set return value */ 203e54bcde3SZi Shen Lim emit(A64_MOV(1, A64_R(0), r0), ctx); 204e54bcde3SZi Shen Lim 205e54bcde3SZi Shen Lim emit(A64_RET(A64_LR), ctx); 206e54bcde3SZi Shen Lim } 207e54bcde3SZi Shen Lim 208*30d3d94cSZi Shen Lim /* JITs an eBPF instruction. 209*30d3d94cSZi Shen Lim * Returns: 210*30d3d94cSZi Shen Lim * 0 - successfully JITed an 8-byte eBPF instruction. 211*30d3d94cSZi Shen Lim * >0 - successfully JITed a 16-byte eBPF instruction. 212*30d3d94cSZi Shen Lim * <0 - failed to JIT. 213*30d3d94cSZi Shen Lim */ 214e54bcde3SZi Shen Lim static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx) 215e54bcde3SZi Shen Lim { 216e54bcde3SZi Shen Lim const u8 code = insn->code; 217e54bcde3SZi Shen Lim const u8 dst = bpf2a64[insn->dst_reg]; 218e54bcde3SZi Shen Lim const u8 src = bpf2a64[insn->src_reg]; 219e54bcde3SZi Shen Lim const u8 tmp = bpf2a64[TMP_REG_1]; 220e54bcde3SZi Shen Lim const u8 tmp2 = bpf2a64[TMP_REG_2]; 221e54bcde3SZi Shen Lim const s16 off = insn->off; 222e54bcde3SZi Shen Lim const s32 imm = insn->imm; 223e54bcde3SZi Shen Lim const int i = insn - ctx->prog->insnsi; 224e54bcde3SZi Shen Lim const bool is64 = BPF_CLASS(code) == BPF_ALU64; 225e54bcde3SZi Shen Lim u8 jmp_cond; 226e54bcde3SZi Shen Lim s32 jmp_offset; 227e54bcde3SZi Shen Lim 228e54bcde3SZi Shen Lim switch (code) { 229e54bcde3SZi Shen Lim /* dst = src */ 230e54bcde3SZi Shen Lim case BPF_ALU | BPF_MOV | BPF_X: 231e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_MOV | BPF_X: 232e54bcde3SZi Shen Lim emit(A64_MOV(is64, dst, src), ctx); 233e54bcde3SZi Shen Lim break; 234e54bcde3SZi Shen Lim /* dst = dst OP src */ 235e54bcde3SZi Shen Lim case BPF_ALU | BPF_ADD | BPF_X: 236e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_ADD | BPF_X: 237e54bcde3SZi Shen Lim emit(A64_ADD(is64, dst, dst, src), ctx); 238e54bcde3SZi Shen Lim break; 239e54bcde3SZi Shen Lim case BPF_ALU | BPF_SUB | BPF_X: 240e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_SUB | BPF_X: 241e54bcde3SZi Shen Lim emit(A64_SUB(is64, dst, dst, src), ctx); 242e54bcde3SZi Shen Lim break; 243e54bcde3SZi Shen Lim case BPF_ALU | BPF_AND | BPF_X: 244e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_AND | BPF_X: 245e54bcde3SZi Shen Lim emit(A64_AND(is64, dst, dst, src), ctx); 246e54bcde3SZi Shen Lim break; 247e54bcde3SZi Shen Lim case BPF_ALU | BPF_OR | BPF_X: 248e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_OR | BPF_X: 249e54bcde3SZi Shen Lim emit(A64_ORR(is64, dst, dst, src), ctx); 250e54bcde3SZi Shen Lim break; 251e54bcde3SZi Shen Lim case BPF_ALU | BPF_XOR | BPF_X: 252e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_XOR | BPF_X: 253e54bcde3SZi Shen Lim emit(A64_EOR(is64, dst, dst, src), ctx); 254e54bcde3SZi Shen Lim break; 255e54bcde3SZi Shen Lim case BPF_ALU | BPF_MUL | BPF_X: 256e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_MUL | BPF_X: 257e54bcde3SZi Shen Lim emit(A64_MUL(is64, dst, dst, src), ctx); 258e54bcde3SZi Shen Lim break; 259e54bcde3SZi Shen Lim case BPF_ALU | BPF_DIV | BPF_X: 260e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_DIV | BPF_X: 261e54bcde3SZi Shen Lim emit(A64_UDIV(is64, dst, dst, src), ctx); 262e54bcde3SZi Shen Lim break; 263e54bcde3SZi Shen Lim case BPF_ALU | BPF_MOD | BPF_X: 264e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_MOD | BPF_X: 265e54bcde3SZi Shen Lim ctx->tmp_used = 1; 266e54bcde3SZi Shen Lim emit(A64_UDIV(is64, tmp, dst, src), ctx); 267e54bcde3SZi Shen Lim emit(A64_MUL(is64, tmp, tmp, src), ctx); 268e54bcde3SZi Shen Lim emit(A64_SUB(is64, dst, dst, tmp), ctx); 269e54bcde3SZi Shen Lim break; 270d65a634aSZi Shen Lim case BPF_ALU | BPF_LSH | BPF_X: 271d65a634aSZi Shen Lim case BPF_ALU64 | BPF_LSH | BPF_X: 272d65a634aSZi Shen Lim emit(A64_LSLV(is64, dst, dst, src), ctx); 273d65a634aSZi Shen Lim break; 274d65a634aSZi Shen Lim case BPF_ALU | BPF_RSH | BPF_X: 275d65a634aSZi Shen Lim case BPF_ALU64 | BPF_RSH | BPF_X: 276d65a634aSZi Shen Lim emit(A64_LSRV(is64, dst, dst, src), ctx); 277d65a634aSZi Shen Lim break; 278d65a634aSZi Shen Lim case BPF_ALU | BPF_ARSH | BPF_X: 279d65a634aSZi Shen Lim case BPF_ALU64 | BPF_ARSH | BPF_X: 280d65a634aSZi Shen Lim emit(A64_ASRV(is64, dst, dst, src), ctx); 281d65a634aSZi Shen Lim break; 282e54bcde3SZi Shen Lim /* dst = -dst */ 283e54bcde3SZi Shen Lim case BPF_ALU | BPF_NEG: 284e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_NEG: 285e54bcde3SZi Shen Lim emit(A64_NEG(is64, dst, dst), ctx); 286e54bcde3SZi Shen Lim break; 287e54bcde3SZi Shen Lim /* dst = BSWAP##imm(dst) */ 288e54bcde3SZi Shen Lim case BPF_ALU | BPF_END | BPF_FROM_LE: 289e54bcde3SZi Shen Lim case BPF_ALU | BPF_END | BPF_FROM_BE: 290e54bcde3SZi Shen Lim #ifdef CONFIG_CPU_BIG_ENDIAN 291e54bcde3SZi Shen Lim if (BPF_SRC(code) == BPF_FROM_BE) 292e54bcde3SZi Shen Lim break; 293e54bcde3SZi Shen Lim #else /* !CONFIG_CPU_BIG_ENDIAN */ 294e54bcde3SZi Shen Lim if (BPF_SRC(code) == BPF_FROM_LE) 295e54bcde3SZi Shen Lim break; 296e54bcde3SZi Shen Lim #endif 297e54bcde3SZi Shen Lim switch (imm) { 298e54bcde3SZi Shen Lim case 16: 299e54bcde3SZi Shen Lim emit(A64_REV16(is64, dst, dst), ctx); 300e54bcde3SZi Shen Lim break; 301e54bcde3SZi Shen Lim case 32: 302e54bcde3SZi Shen Lim emit(A64_REV32(is64, dst, dst), ctx); 303e54bcde3SZi Shen Lim break; 304e54bcde3SZi Shen Lim case 64: 305e54bcde3SZi Shen Lim emit(A64_REV64(dst, dst), ctx); 306e54bcde3SZi Shen Lim break; 307e54bcde3SZi Shen Lim } 308e54bcde3SZi Shen Lim break; 309e54bcde3SZi Shen Lim /* dst = imm */ 310e54bcde3SZi Shen Lim case BPF_ALU | BPF_MOV | BPF_K: 311e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_MOV | BPF_K: 312e54bcde3SZi Shen Lim emit_a64_mov_i(is64, dst, imm, ctx); 313e54bcde3SZi Shen Lim break; 314e54bcde3SZi Shen Lim /* dst = dst OP imm */ 315e54bcde3SZi Shen Lim case BPF_ALU | BPF_ADD | BPF_K: 316e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_ADD | BPF_K: 317e54bcde3SZi Shen Lim ctx->tmp_used = 1; 318e54bcde3SZi Shen Lim emit_a64_mov_i(is64, tmp, imm, ctx); 319e54bcde3SZi Shen Lim emit(A64_ADD(is64, dst, dst, tmp), ctx); 320e54bcde3SZi Shen Lim break; 321e54bcde3SZi Shen Lim case BPF_ALU | BPF_SUB | BPF_K: 322e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_SUB | BPF_K: 323e54bcde3SZi Shen Lim ctx->tmp_used = 1; 324e54bcde3SZi Shen Lim emit_a64_mov_i(is64, tmp, imm, ctx); 325e54bcde3SZi Shen Lim emit(A64_SUB(is64, dst, dst, tmp), ctx); 326e54bcde3SZi Shen Lim break; 327e54bcde3SZi Shen Lim case BPF_ALU | BPF_AND | BPF_K: 328e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_AND | BPF_K: 329e54bcde3SZi Shen Lim ctx->tmp_used = 1; 330e54bcde3SZi Shen Lim emit_a64_mov_i(is64, tmp, imm, ctx); 331e54bcde3SZi Shen Lim emit(A64_AND(is64, dst, dst, tmp), ctx); 332e54bcde3SZi Shen Lim break; 333e54bcde3SZi Shen Lim case BPF_ALU | BPF_OR | BPF_K: 334e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_OR | BPF_K: 335e54bcde3SZi Shen Lim ctx->tmp_used = 1; 336e54bcde3SZi Shen Lim emit_a64_mov_i(is64, tmp, imm, ctx); 337e54bcde3SZi Shen Lim emit(A64_ORR(is64, dst, dst, tmp), ctx); 338e54bcde3SZi Shen Lim break; 339e54bcde3SZi Shen Lim case BPF_ALU | BPF_XOR | BPF_K: 340e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_XOR | BPF_K: 341e54bcde3SZi Shen Lim ctx->tmp_used = 1; 342e54bcde3SZi Shen Lim emit_a64_mov_i(is64, tmp, imm, ctx); 343e54bcde3SZi Shen Lim emit(A64_EOR(is64, dst, dst, tmp), ctx); 344e54bcde3SZi Shen Lim break; 345e54bcde3SZi Shen Lim case BPF_ALU | BPF_MUL | BPF_K: 346e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_MUL | BPF_K: 347e54bcde3SZi Shen Lim ctx->tmp_used = 1; 348e54bcde3SZi Shen Lim emit_a64_mov_i(is64, tmp, imm, ctx); 349e54bcde3SZi Shen Lim emit(A64_MUL(is64, dst, dst, tmp), ctx); 350e54bcde3SZi Shen Lim break; 351e54bcde3SZi Shen Lim case BPF_ALU | BPF_DIV | BPF_K: 352e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_DIV | BPF_K: 353e54bcde3SZi Shen Lim ctx->tmp_used = 1; 354e54bcde3SZi Shen Lim emit_a64_mov_i(is64, tmp, imm, ctx); 355e54bcde3SZi Shen Lim emit(A64_UDIV(is64, dst, dst, tmp), ctx); 356e54bcde3SZi Shen Lim break; 357e54bcde3SZi Shen Lim case BPF_ALU | BPF_MOD | BPF_K: 358e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_MOD | BPF_K: 359e54bcde3SZi Shen Lim ctx->tmp_used = 1; 360e54bcde3SZi Shen Lim emit_a64_mov_i(is64, tmp2, imm, ctx); 361e54bcde3SZi Shen Lim emit(A64_UDIV(is64, tmp, dst, tmp2), ctx); 362e54bcde3SZi Shen Lim emit(A64_MUL(is64, tmp, tmp, tmp2), ctx); 363e54bcde3SZi Shen Lim emit(A64_SUB(is64, dst, dst, tmp), ctx); 364e54bcde3SZi Shen Lim break; 365e54bcde3SZi Shen Lim case BPF_ALU | BPF_LSH | BPF_K: 366e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_LSH | BPF_K: 367e54bcde3SZi Shen Lim emit(A64_LSL(is64, dst, dst, imm), ctx); 368e54bcde3SZi Shen Lim break; 369e54bcde3SZi Shen Lim case BPF_ALU | BPF_RSH | BPF_K: 370e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_RSH | BPF_K: 371e54bcde3SZi Shen Lim emit(A64_LSR(is64, dst, dst, imm), ctx); 372e54bcde3SZi Shen Lim break; 373e54bcde3SZi Shen Lim case BPF_ALU | BPF_ARSH | BPF_K: 374e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_ARSH | BPF_K: 375e54bcde3SZi Shen Lim emit(A64_ASR(is64, dst, dst, imm), ctx); 376e54bcde3SZi Shen Lim break; 377e54bcde3SZi Shen Lim 378e54bcde3SZi Shen Lim #define check_imm(bits, imm) do { \ 379e54bcde3SZi Shen Lim if ((((imm) > 0) && ((imm) >> (bits))) || \ 380e54bcde3SZi Shen Lim (((imm) < 0) && (~(imm) >> (bits)))) { \ 381e54bcde3SZi Shen Lim pr_info("[%2d] imm=%d(0x%x) out of range\n", \ 382e54bcde3SZi Shen Lim i, imm, imm); \ 383e54bcde3SZi Shen Lim return -EINVAL; \ 384e54bcde3SZi Shen Lim } \ 385e54bcde3SZi Shen Lim } while (0) 386e54bcde3SZi Shen Lim #define check_imm19(imm) check_imm(19, imm) 387e54bcde3SZi Shen Lim #define check_imm26(imm) check_imm(26, imm) 388e54bcde3SZi Shen Lim 389e54bcde3SZi Shen Lim /* JUMP off */ 390e54bcde3SZi Shen Lim case BPF_JMP | BPF_JA: 391e54bcde3SZi Shen Lim jmp_offset = bpf2a64_offset(i + off, i, ctx); 392e54bcde3SZi Shen Lim check_imm26(jmp_offset); 393e54bcde3SZi Shen Lim emit(A64_B(jmp_offset), ctx); 394e54bcde3SZi Shen Lim break; 395e54bcde3SZi Shen Lim /* IF (dst COND src) JUMP off */ 396e54bcde3SZi Shen Lim case BPF_JMP | BPF_JEQ | BPF_X: 397e54bcde3SZi Shen Lim case BPF_JMP | BPF_JGT | BPF_X: 398e54bcde3SZi Shen Lim case BPF_JMP | BPF_JGE | BPF_X: 399e54bcde3SZi Shen Lim case BPF_JMP | BPF_JNE | BPF_X: 400e54bcde3SZi Shen Lim case BPF_JMP | BPF_JSGT | BPF_X: 401e54bcde3SZi Shen Lim case BPF_JMP | BPF_JSGE | BPF_X: 402e54bcde3SZi Shen Lim emit(A64_CMP(1, dst, src), ctx); 403e54bcde3SZi Shen Lim emit_cond_jmp: 404e54bcde3SZi Shen Lim jmp_offset = bpf2a64_offset(i + off, i, ctx); 405e54bcde3SZi Shen Lim check_imm19(jmp_offset); 406e54bcde3SZi Shen Lim switch (BPF_OP(code)) { 407e54bcde3SZi Shen Lim case BPF_JEQ: 408e54bcde3SZi Shen Lim jmp_cond = A64_COND_EQ; 409e54bcde3SZi Shen Lim break; 410e54bcde3SZi Shen Lim case BPF_JGT: 411e54bcde3SZi Shen Lim jmp_cond = A64_COND_HI; 412e54bcde3SZi Shen Lim break; 413e54bcde3SZi Shen Lim case BPF_JGE: 414e54bcde3SZi Shen Lim jmp_cond = A64_COND_CS; 415e54bcde3SZi Shen Lim break; 416e54bcde3SZi Shen Lim case BPF_JNE: 417e54bcde3SZi Shen Lim jmp_cond = A64_COND_NE; 418e54bcde3SZi Shen Lim break; 419e54bcde3SZi Shen Lim case BPF_JSGT: 420e54bcde3SZi Shen Lim jmp_cond = A64_COND_GT; 421e54bcde3SZi Shen Lim break; 422e54bcde3SZi Shen Lim case BPF_JSGE: 423e54bcde3SZi Shen Lim jmp_cond = A64_COND_GE; 424e54bcde3SZi Shen Lim break; 425e54bcde3SZi Shen Lim default: 426e54bcde3SZi Shen Lim return -EFAULT; 427e54bcde3SZi Shen Lim } 428e54bcde3SZi Shen Lim emit(A64_B_(jmp_cond, jmp_offset), ctx); 429e54bcde3SZi Shen Lim break; 430e54bcde3SZi Shen Lim case BPF_JMP | BPF_JSET | BPF_X: 431e54bcde3SZi Shen Lim emit(A64_TST(1, dst, src), ctx); 432e54bcde3SZi Shen Lim goto emit_cond_jmp; 433e54bcde3SZi Shen Lim /* IF (dst COND imm) JUMP off */ 434e54bcde3SZi Shen Lim case BPF_JMP | BPF_JEQ | BPF_K: 435e54bcde3SZi Shen Lim case BPF_JMP | BPF_JGT | BPF_K: 436e54bcde3SZi Shen Lim case BPF_JMP | BPF_JGE | BPF_K: 437e54bcde3SZi Shen Lim case BPF_JMP | BPF_JNE | BPF_K: 438e54bcde3SZi Shen Lim case BPF_JMP | BPF_JSGT | BPF_K: 439e54bcde3SZi Shen Lim case BPF_JMP | BPF_JSGE | BPF_K: 440e54bcde3SZi Shen Lim ctx->tmp_used = 1; 441e54bcde3SZi Shen Lim emit_a64_mov_i(1, tmp, imm, ctx); 442e54bcde3SZi Shen Lim emit(A64_CMP(1, dst, tmp), ctx); 443e54bcde3SZi Shen Lim goto emit_cond_jmp; 444e54bcde3SZi Shen Lim case BPF_JMP | BPF_JSET | BPF_K: 445e54bcde3SZi Shen Lim ctx->tmp_used = 1; 446e54bcde3SZi Shen Lim emit_a64_mov_i(1, tmp, imm, ctx); 447e54bcde3SZi Shen Lim emit(A64_TST(1, dst, tmp), ctx); 448e54bcde3SZi Shen Lim goto emit_cond_jmp; 449e54bcde3SZi Shen Lim /* function call */ 450e54bcde3SZi Shen Lim case BPF_JMP | BPF_CALL: 451e54bcde3SZi Shen Lim { 452e54bcde3SZi Shen Lim const u8 r0 = bpf2a64[BPF_REG_0]; 453e54bcde3SZi Shen Lim const u64 func = (u64)__bpf_call_base + imm; 454e54bcde3SZi Shen Lim 455e54bcde3SZi Shen Lim ctx->tmp_used = 1; 456e54bcde3SZi Shen Lim emit_a64_mov_i64(tmp, func, ctx); 457e54bcde3SZi Shen Lim emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx); 458e54bcde3SZi Shen Lim emit(A64_MOV(1, A64_FP, A64_SP), ctx); 459e54bcde3SZi Shen Lim emit(A64_BLR(tmp), ctx); 460e54bcde3SZi Shen Lim emit(A64_MOV(1, r0, A64_R(0)), ctx); 461e54bcde3SZi Shen Lim emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx); 462e54bcde3SZi Shen Lim break; 463e54bcde3SZi Shen Lim } 464e54bcde3SZi Shen Lim /* function return */ 465e54bcde3SZi Shen Lim case BPF_JMP | BPF_EXIT: 466e54bcde3SZi Shen Lim if (i == ctx->prog->len - 1) 467e54bcde3SZi Shen Lim break; 468e54bcde3SZi Shen Lim jmp_offset = epilogue_offset(ctx); 469e54bcde3SZi Shen Lim check_imm26(jmp_offset); 470e54bcde3SZi Shen Lim emit(A64_B(jmp_offset), ctx); 471e54bcde3SZi Shen Lim break; 472e54bcde3SZi Shen Lim 473*30d3d94cSZi Shen Lim /* dst = imm64 */ 474*30d3d94cSZi Shen Lim case BPF_LD | BPF_IMM | BPF_DW: 475*30d3d94cSZi Shen Lim { 476*30d3d94cSZi Shen Lim const struct bpf_insn insn1 = insn[1]; 477*30d3d94cSZi Shen Lim u64 imm64; 478*30d3d94cSZi Shen Lim 479*30d3d94cSZi Shen Lim if (insn1.code != 0 || insn1.src_reg != 0 || 480*30d3d94cSZi Shen Lim insn1.dst_reg != 0 || insn1.off != 0) { 481*30d3d94cSZi Shen Lim /* Note: verifier in BPF core must catch invalid 482*30d3d94cSZi Shen Lim * instructions. 483*30d3d94cSZi Shen Lim */ 484*30d3d94cSZi Shen Lim pr_err_once("Invalid BPF_LD_IMM64 instruction\n"); 485*30d3d94cSZi Shen Lim return -EINVAL; 486*30d3d94cSZi Shen Lim } 487*30d3d94cSZi Shen Lim 488*30d3d94cSZi Shen Lim imm64 = (u64)insn1.imm << 32 | imm; 489*30d3d94cSZi Shen Lim emit_a64_mov_i64(dst, imm64, ctx); 490*30d3d94cSZi Shen Lim 491*30d3d94cSZi Shen Lim return 1; 492*30d3d94cSZi Shen Lim } 493*30d3d94cSZi Shen Lim 494e54bcde3SZi Shen Lim /* LDX: dst = *(size *)(src + off) */ 495e54bcde3SZi Shen Lim case BPF_LDX | BPF_MEM | BPF_W: 496e54bcde3SZi Shen Lim case BPF_LDX | BPF_MEM | BPF_H: 497e54bcde3SZi Shen Lim case BPF_LDX | BPF_MEM | BPF_B: 498e54bcde3SZi Shen Lim case BPF_LDX | BPF_MEM | BPF_DW: 499e54bcde3SZi Shen Lim ctx->tmp_used = 1; 500e54bcde3SZi Shen Lim emit_a64_mov_i(1, tmp, off, ctx); 501e54bcde3SZi Shen Lim switch (BPF_SIZE(code)) { 502e54bcde3SZi Shen Lim case BPF_W: 503e54bcde3SZi Shen Lim emit(A64_LDR32(dst, src, tmp), ctx); 504e54bcde3SZi Shen Lim break; 505e54bcde3SZi Shen Lim case BPF_H: 506e54bcde3SZi Shen Lim emit(A64_LDRH(dst, src, tmp), ctx); 507e54bcde3SZi Shen Lim break; 508e54bcde3SZi Shen Lim case BPF_B: 509e54bcde3SZi Shen Lim emit(A64_LDRB(dst, src, tmp), ctx); 510e54bcde3SZi Shen Lim break; 511e54bcde3SZi Shen Lim case BPF_DW: 512e54bcde3SZi Shen Lim emit(A64_LDR64(dst, src, tmp), ctx); 513e54bcde3SZi Shen Lim break; 514e54bcde3SZi Shen Lim } 515e54bcde3SZi Shen Lim break; 516e54bcde3SZi Shen Lim 517e54bcde3SZi Shen Lim /* ST: *(size *)(dst + off) = imm */ 518e54bcde3SZi Shen Lim case BPF_ST | BPF_MEM | BPF_W: 519e54bcde3SZi Shen Lim case BPF_ST | BPF_MEM | BPF_H: 520e54bcde3SZi Shen Lim case BPF_ST | BPF_MEM | BPF_B: 521e54bcde3SZi Shen Lim case BPF_ST | BPF_MEM | BPF_DW: 522e54bcde3SZi Shen Lim goto notyet; 523e54bcde3SZi Shen Lim 524e54bcde3SZi Shen Lim /* STX: *(size *)(dst + off) = src */ 525e54bcde3SZi Shen Lim case BPF_STX | BPF_MEM | BPF_W: 526e54bcde3SZi Shen Lim case BPF_STX | BPF_MEM | BPF_H: 527e54bcde3SZi Shen Lim case BPF_STX | BPF_MEM | BPF_B: 528e54bcde3SZi Shen Lim case BPF_STX | BPF_MEM | BPF_DW: 529e54bcde3SZi Shen Lim ctx->tmp_used = 1; 530e54bcde3SZi Shen Lim emit_a64_mov_i(1, tmp, off, ctx); 531e54bcde3SZi Shen Lim switch (BPF_SIZE(code)) { 532e54bcde3SZi Shen Lim case BPF_W: 533e54bcde3SZi Shen Lim emit(A64_STR32(src, dst, tmp), ctx); 534e54bcde3SZi Shen Lim break; 535e54bcde3SZi Shen Lim case BPF_H: 536e54bcde3SZi Shen Lim emit(A64_STRH(src, dst, tmp), ctx); 537e54bcde3SZi Shen Lim break; 538e54bcde3SZi Shen Lim case BPF_B: 539e54bcde3SZi Shen Lim emit(A64_STRB(src, dst, tmp), ctx); 540e54bcde3SZi Shen Lim break; 541e54bcde3SZi Shen Lim case BPF_DW: 542e54bcde3SZi Shen Lim emit(A64_STR64(src, dst, tmp), ctx); 543e54bcde3SZi Shen Lim break; 544e54bcde3SZi Shen Lim } 545e54bcde3SZi Shen Lim break; 546e54bcde3SZi Shen Lim /* STX XADD: lock *(u32 *)(dst + off) += src */ 547e54bcde3SZi Shen Lim case BPF_STX | BPF_XADD | BPF_W: 548e54bcde3SZi Shen Lim /* STX XADD: lock *(u64 *)(dst + off) += src */ 549e54bcde3SZi Shen Lim case BPF_STX | BPF_XADD | BPF_DW: 550e54bcde3SZi Shen Lim goto notyet; 551e54bcde3SZi Shen Lim 552e54bcde3SZi Shen Lim /* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + imm)) */ 553e54bcde3SZi Shen Lim case BPF_LD | BPF_ABS | BPF_W: 554e54bcde3SZi Shen Lim case BPF_LD | BPF_ABS | BPF_H: 555e54bcde3SZi Shen Lim case BPF_LD | BPF_ABS | BPF_B: 556e54bcde3SZi Shen Lim /* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + src + imm)) */ 557e54bcde3SZi Shen Lim case BPF_LD | BPF_IND | BPF_W: 558e54bcde3SZi Shen Lim case BPF_LD | BPF_IND | BPF_H: 559e54bcde3SZi Shen Lim case BPF_LD | BPF_IND | BPF_B: 560e54bcde3SZi Shen Lim { 561e54bcde3SZi Shen Lim const u8 r0 = bpf2a64[BPF_REG_0]; /* r0 = return value */ 562e54bcde3SZi Shen Lim const u8 r6 = bpf2a64[BPF_REG_6]; /* r6 = pointer to sk_buff */ 563e54bcde3SZi Shen Lim const u8 fp = bpf2a64[BPF_REG_FP]; 564e54bcde3SZi Shen Lim const u8 r1 = bpf2a64[BPF_REG_1]; /* r1: struct sk_buff *skb */ 565e54bcde3SZi Shen Lim const u8 r2 = bpf2a64[BPF_REG_2]; /* r2: int k */ 566e54bcde3SZi Shen Lim const u8 r3 = bpf2a64[BPF_REG_3]; /* r3: unsigned int size */ 567e54bcde3SZi Shen Lim const u8 r4 = bpf2a64[BPF_REG_4]; /* r4: void *buffer */ 568e54bcde3SZi Shen Lim const u8 r5 = bpf2a64[BPF_REG_5]; /* r5: void *(*func)(...) */ 569e54bcde3SZi Shen Lim int size; 570e54bcde3SZi Shen Lim 571e54bcde3SZi Shen Lim emit(A64_MOV(1, r1, r6), ctx); 572e54bcde3SZi Shen Lim emit_a64_mov_i(0, r2, imm, ctx); 573e54bcde3SZi Shen Lim if (BPF_MODE(code) == BPF_IND) 574e54bcde3SZi Shen Lim emit(A64_ADD(0, r2, r2, src), ctx); 575e54bcde3SZi Shen Lim switch (BPF_SIZE(code)) { 576e54bcde3SZi Shen Lim case BPF_W: 577e54bcde3SZi Shen Lim size = 4; 578e54bcde3SZi Shen Lim break; 579e54bcde3SZi Shen Lim case BPF_H: 580e54bcde3SZi Shen Lim size = 2; 581e54bcde3SZi Shen Lim break; 582e54bcde3SZi Shen Lim case BPF_B: 583e54bcde3SZi Shen Lim size = 1; 584e54bcde3SZi Shen Lim break; 585e54bcde3SZi Shen Lim default: 586e54bcde3SZi Shen Lim return -EINVAL; 587e54bcde3SZi Shen Lim } 588e54bcde3SZi Shen Lim emit_a64_mov_i64(r3, size, ctx); 589e54bcde3SZi Shen Lim emit(A64_ADD_I(1, r4, fp, MAX_BPF_STACK), ctx); 590e54bcde3SZi Shen Lim emit_a64_mov_i64(r5, (unsigned long)bpf_load_pointer, ctx); 591e54bcde3SZi Shen Lim emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx); 592e54bcde3SZi Shen Lim emit(A64_MOV(1, A64_FP, A64_SP), ctx); 593e54bcde3SZi Shen Lim emit(A64_BLR(r5), ctx); 594e54bcde3SZi Shen Lim emit(A64_MOV(1, r0, A64_R(0)), ctx); 595e54bcde3SZi Shen Lim emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx); 596e54bcde3SZi Shen Lim 597e54bcde3SZi Shen Lim jmp_offset = epilogue_offset(ctx); 598e54bcde3SZi Shen Lim check_imm19(jmp_offset); 599e54bcde3SZi Shen Lim emit(A64_CBZ(1, r0, jmp_offset), ctx); 600e54bcde3SZi Shen Lim emit(A64_MOV(1, r5, r0), ctx); 601e54bcde3SZi Shen Lim switch (BPF_SIZE(code)) { 602e54bcde3SZi Shen Lim case BPF_W: 603e54bcde3SZi Shen Lim emit(A64_LDR32(r0, r5, A64_ZR), ctx); 604e54bcde3SZi Shen Lim #ifndef CONFIG_CPU_BIG_ENDIAN 605e54bcde3SZi Shen Lim emit(A64_REV32(0, r0, r0), ctx); 606e54bcde3SZi Shen Lim #endif 607e54bcde3SZi Shen Lim break; 608e54bcde3SZi Shen Lim case BPF_H: 609e54bcde3SZi Shen Lim emit(A64_LDRH(r0, r5, A64_ZR), ctx); 610e54bcde3SZi Shen Lim #ifndef CONFIG_CPU_BIG_ENDIAN 611e54bcde3SZi Shen Lim emit(A64_REV16(0, r0, r0), ctx); 612e54bcde3SZi Shen Lim #endif 613e54bcde3SZi Shen Lim break; 614e54bcde3SZi Shen Lim case BPF_B: 615e54bcde3SZi Shen Lim emit(A64_LDRB(r0, r5, A64_ZR), ctx); 616e54bcde3SZi Shen Lim break; 617e54bcde3SZi Shen Lim } 618e54bcde3SZi Shen Lim break; 619e54bcde3SZi Shen Lim } 620e54bcde3SZi Shen Lim notyet: 621e54bcde3SZi Shen Lim pr_info_once("*** NOT YET: opcode %02x ***\n", code); 622e54bcde3SZi Shen Lim return -EFAULT; 623e54bcde3SZi Shen Lim 624e54bcde3SZi Shen Lim default: 625e54bcde3SZi Shen Lim pr_err_once("unknown opcode %02x\n", code); 626e54bcde3SZi Shen Lim return -EINVAL; 627e54bcde3SZi Shen Lim } 628e54bcde3SZi Shen Lim 629e54bcde3SZi Shen Lim return 0; 630e54bcde3SZi Shen Lim } 631e54bcde3SZi Shen Lim 632e54bcde3SZi Shen Lim static int build_body(struct jit_ctx *ctx) 633e54bcde3SZi Shen Lim { 634e54bcde3SZi Shen Lim const struct bpf_prog *prog = ctx->prog; 635e54bcde3SZi Shen Lim int i; 636e54bcde3SZi Shen Lim 637e54bcde3SZi Shen Lim for (i = 0; i < prog->len; i++) { 638e54bcde3SZi Shen Lim const struct bpf_insn *insn = &prog->insnsi[i]; 639e54bcde3SZi Shen Lim int ret; 640e54bcde3SZi Shen Lim 641e54bcde3SZi Shen Lim if (ctx->image == NULL) 642e54bcde3SZi Shen Lim ctx->offset[i] = ctx->idx; 643e54bcde3SZi Shen Lim 644e54bcde3SZi Shen Lim ret = build_insn(insn, ctx); 645*30d3d94cSZi Shen Lim if (ret > 0) { 646*30d3d94cSZi Shen Lim i++; 647*30d3d94cSZi Shen Lim continue; 648*30d3d94cSZi Shen Lim } 649e54bcde3SZi Shen Lim if (ret) 650e54bcde3SZi Shen Lim return ret; 651e54bcde3SZi Shen Lim } 652e54bcde3SZi Shen Lim 653e54bcde3SZi Shen Lim return 0; 654e54bcde3SZi Shen Lim } 655e54bcde3SZi Shen Lim 656e54bcde3SZi Shen Lim static inline void bpf_flush_icache(void *start, void *end) 657e54bcde3SZi Shen Lim { 658e54bcde3SZi Shen Lim flush_icache_range((unsigned long)start, (unsigned long)end); 659e54bcde3SZi Shen Lim } 660e54bcde3SZi Shen Lim 661e54bcde3SZi Shen Lim void bpf_jit_compile(struct bpf_prog *prog) 662e54bcde3SZi Shen Lim { 663e54bcde3SZi Shen Lim /* Nothing to do here. We support Internal BPF. */ 664e54bcde3SZi Shen Lim } 665e54bcde3SZi Shen Lim 666e54bcde3SZi Shen Lim void bpf_int_jit_compile(struct bpf_prog *prog) 667e54bcde3SZi Shen Lim { 668b569c1c6SDaniel Borkmann struct bpf_binary_header *header; 669e54bcde3SZi Shen Lim struct jit_ctx ctx; 670e54bcde3SZi Shen Lim int image_size; 671b569c1c6SDaniel Borkmann u8 *image_ptr; 672e54bcde3SZi Shen Lim 673e54bcde3SZi Shen Lim if (!bpf_jit_enable) 674e54bcde3SZi Shen Lim return; 675e54bcde3SZi Shen Lim 676e54bcde3SZi Shen Lim if (!prog || !prog->len) 677e54bcde3SZi Shen Lim return; 678e54bcde3SZi Shen Lim 679e54bcde3SZi Shen Lim memset(&ctx, 0, sizeof(ctx)); 680e54bcde3SZi Shen Lim ctx.prog = prog; 681e54bcde3SZi Shen Lim 682e54bcde3SZi Shen Lim ctx.offset = kcalloc(prog->len, sizeof(int), GFP_KERNEL); 683e54bcde3SZi Shen Lim if (ctx.offset == NULL) 684e54bcde3SZi Shen Lim return; 685e54bcde3SZi Shen Lim 686e54bcde3SZi Shen Lim /* 1. Initial fake pass to compute ctx->idx. */ 687e54bcde3SZi Shen Lim 688e54bcde3SZi Shen Lim /* Fake pass to fill in ctx->offset. */ 689e54bcde3SZi Shen Lim if (build_body(&ctx)) 690e54bcde3SZi Shen Lim goto out; 691e54bcde3SZi Shen Lim 692e54bcde3SZi Shen Lim build_prologue(&ctx); 693e54bcde3SZi Shen Lim build_epilogue(&ctx); 694e54bcde3SZi Shen Lim 695e54bcde3SZi Shen Lim /* Now we know the actual image size. */ 696e54bcde3SZi Shen Lim image_size = sizeof(u32) * ctx.idx; 697b569c1c6SDaniel Borkmann header = bpf_jit_binary_alloc(image_size, &image_ptr, 698b569c1c6SDaniel Borkmann sizeof(u32), jit_fill_hole); 699b569c1c6SDaniel Borkmann if (header == NULL) 700e54bcde3SZi Shen Lim goto out; 701e54bcde3SZi Shen Lim 702e54bcde3SZi Shen Lim /* 2. Now, the actual pass. */ 703e54bcde3SZi Shen Lim 704b569c1c6SDaniel Borkmann ctx.image = (u32 *)image_ptr; 705e54bcde3SZi Shen Lim ctx.idx = 0; 706b569c1c6SDaniel Borkmann 707e54bcde3SZi Shen Lim build_prologue(&ctx); 708e54bcde3SZi Shen Lim 709e54bcde3SZi Shen Lim ctx.body_offset = ctx.idx; 71060ef0494SDaniel Borkmann if (build_body(&ctx)) { 711b569c1c6SDaniel Borkmann bpf_jit_binary_free(header); 712e54bcde3SZi Shen Lim goto out; 71360ef0494SDaniel Borkmann } 714e54bcde3SZi Shen Lim 715e54bcde3SZi Shen Lim build_epilogue(&ctx); 716e54bcde3SZi Shen Lim 717e54bcde3SZi Shen Lim /* And we're done. */ 718e54bcde3SZi Shen Lim if (bpf_jit_enable > 1) 719e54bcde3SZi Shen Lim bpf_jit_dump(prog->len, image_size, 2, ctx.image); 720e54bcde3SZi Shen Lim 721e54bcde3SZi Shen Lim bpf_flush_icache(ctx.image, ctx.image + ctx.idx); 722b569c1c6SDaniel Borkmann 723b569c1c6SDaniel Borkmann set_memory_ro((unsigned long)header, header->pages); 724e54bcde3SZi Shen Lim prog->bpf_func = (void *)ctx.image; 725e54bcde3SZi Shen Lim prog->jited = 1; 726e54bcde3SZi Shen Lim out: 727e54bcde3SZi Shen Lim kfree(ctx.offset); 728e54bcde3SZi Shen Lim } 729e54bcde3SZi Shen Lim 730e54bcde3SZi Shen Lim void bpf_jit_free(struct bpf_prog *prog) 731e54bcde3SZi Shen Lim { 732b569c1c6SDaniel Borkmann unsigned long addr = (unsigned long)prog->bpf_func & PAGE_MASK; 733b569c1c6SDaniel Borkmann struct bpf_binary_header *header = (void *)addr; 734e54bcde3SZi Shen Lim 735b569c1c6SDaniel Borkmann if (!prog->jited) 736b569c1c6SDaniel Borkmann goto free_filter; 737b569c1c6SDaniel Borkmann 738b569c1c6SDaniel Borkmann set_memory_rw(addr, header->pages); 739b569c1c6SDaniel Borkmann bpf_jit_binary_free(header); 740b569c1c6SDaniel Borkmann 741b569c1c6SDaniel Borkmann free_filter: 742b569c1c6SDaniel Borkmann bpf_prog_unlock_free(prog); 743e54bcde3SZi Shen Lim } 744