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; 6351c9fbb1SZi Shen Lim int epilogue_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 { 1168eee539dSXi Wang int to = ctx->offset[bpf_to]; 117e54bcde3SZi Shen Lim /* -1 to account for the Branch instruction */ 1188eee539dSXi Wang int from = ctx->offset[bpf_from] - 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 { 13351c9fbb1SZi Shen Lim int to = ctx->epilogue_offset; 13451c9fbb1SZi Shen Lim int from = ctx->idx; 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 20830d3d94cSZi Shen Lim /* JITs an eBPF instruction. 20930d3d94cSZi Shen Lim * Returns: 21030d3d94cSZi Shen Lim * 0 - successfully JITed an 8-byte eBPF instruction. 21130d3d94cSZi Shen Lim * >0 - successfully JITed a 16-byte eBPF instruction. 21230d3d94cSZi Shen Lim * <0 - failed to JIT. 21330d3d94cSZi 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) 292d63903bbSXi Wang goto emit_bswap_uxt; 293e54bcde3SZi Shen Lim #else /* !CONFIG_CPU_BIG_ENDIAN */ 294e54bcde3SZi Shen Lim if (BPF_SRC(code) == BPF_FROM_LE) 295d63903bbSXi Wang goto emit_bswap_uxt; 296e54bcde3SZi Shen Lim #endif 297e54bcde3SZi Shen Lim switch (imm) { 298e54bcde3SZi Shen Lim case 16: 299e54bcde3SZi Shen Lim emit(A64_REV16(is64, dst, dst), ctx); 300d63903bbSXi Wang /* zero-extend 16 bits into 64 bits */ 301d63903bbSXi Wang emit(A64_UXTH(is64, dst, dst), ctx); 302e54bcde3SZi Shen Lim break; 303e54bcde3SZi Shen Lim case 32: 304e54bcde3SZi Shen Lim emit(A64_REV32(is64, dst, dst), ctx); 305d63903bbSXi Wang /* upper 32 bits already cleared */ 306e54bcde3SZi Shen Lim break; 307e54bcde3SZi Shen Lim case 64: 308e54bcde3SZi Shen Lim emit(A64_REV64(dst, dst), ctx); 309e54bcde3SZi Shen Lim break; 310e54bcde3SZi Shen Lim } 311e54bcde3SZi Shen Lim break; 312d63903bbSXi Wang emit_bswap_uxt: 313d63903bbSXi Wang switch (imm) { 314d63903bbSXi Wang case 16: 315d63903bbSXi Wang /* zero-extend 16 bits into 64 bits */ 316d63903bbSXi Wang emit(A64_UXTH(is64, dst, dst), ctx); 317d63903bbSXi Wang break; 318d63903bbSXi Wang case 32: 319d63903bbSXi Wang /* zero-extend 32 bits into 64 bits */ 320d63903bbSXi Wang emit(A64_UXTW(is64, dst, dst), ctx); 321d63903bbSXi Wang break; 322d63903bbSXi Wang case 64: 323d63903bbSXi Wang /* nop */ 324d63903bbSXi Wang break; 325d63903bbSXi Wang } 326d63903bbSXi Wang break; 327e54bcde3SZi Shen Lim /* dst = imm */ 328e54bcde3SZi Shen Lim case BPF_ALU | BPF_MOV | BPF_K: 329e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_MOV | BPF_K: 330e54bcde3SZi Shen Lim emit_a64_mov_i(is64, dst, imm, ctx); 331e54bcde3SZi Shen Lim break; 332e54bcde3SZi Shen Lim /* dst = dst OP imm */ 333e54bcde3SZi Shen Lim case BPF_ALU | BPF_ADD | BPF_K: 334e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_ADD | 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_ADD(is64, dst, dst, tmp), ctx); 338e54bcde3SZi Shen Lim break; 339e54bcde3SZi Shen Lim case BPF_ALU | BPF_SUB | BPF_K: 340e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_SUB | 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_SUB(is64, dst, dst, tmp), ctx); 344e54bcde3SZi Shen Lim break; 345e54bcde3SZi Shen Lim case BPF_ALU | BPF_AND | BPF_K: 346e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_AND | 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_AND(is64, dst, dst, tmp), ctx); 350e54bcde3SZi Shen Lim break; 351e54bcde3SZi Shen Lim case BPF_ALU | BPF_OR | BPF_K: 352e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_OR | 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_ORR(is64, dst, dst, tmp), ctx); 356e54bcde3SZi Shen Lim break; 357e54bcde3SZi Shen Lim case BPF_ALU | BPF_XOR | BPF_K: 358e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_XOR | BPF_K: 359e54bcde3SZi Shen Lim ctx->tmp_used = 1; 360e54bcde3SZi Shen Lim emit_a64_mov_i(is64, tmp, imm, ctx); 361e54bcde3SZi Shen Lim emit(A64_EOR(is64, dst, dst, tmp), ctx); 362e54bcde3SZi Shen Lim break; 363e54bcde3SZi Shen Lim case BPF_ALU | BPF_MUL | BPF_K: 364e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_MUL | BPF_K: 365e54bcde3SZi Shen Lim ctx->tmp_used = 1; 366e54bcde3SZi Shen Lim emit_a64_mov_i(is64, tmp, imm, ctx); 367e54bcde3SZi Shen Lim emit(A64_MUL(is64, dst, dst, tmp), ctx); 368e54bcde3SZi Shen Lim break; 369e54bcde3SZi Shen Lim case BPF_ALU | BPF_DIV | BPF_K: 370e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_DIV | BPF_K: 371e54bcde3SZi Shen Lim ctx->tmp_used = 1; 372e54bcde3SZi Shen Lim emit_a64_mov_i(is64, tmp, imm, ctx); 373e54bcde3SZi Shen Lim emit(A64_UDIV(is64, dst, dst, tmp), ctx); 374e54bcde3SZi Shen Lim break; 375e54bcde3SZi Shen Lim case BPF_ALU | BPF_MOD | BPF_K: 376e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_MOD | BPF_K: 377e54bcde3SZi Shen Lim ctx->tmp_used = 1; 378e54bcde3SZi Shen Lim emit_a64_mov_i(is64, tmp2, imm, ctx); 379e54bcde3SZi Shen Lim emit(A64_UDIV(is64, tmp, dst, tmp2), ctx); 380e54bcde3SZi Shen Lim emit(A64_MUL(is64, tmp, tmp, tmp2), ctx); 381e54bcde3SZi Shen Lim emit(A64_SUB(is64, dst, dst, tmp), ctx); 382e54bcde3SZi Shen Lim break; 383e54bcde3SZi Shen Lim case BPF_ALU | BPF_LSH | BPF_K: 384e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_LSH | BPF_K: 385e54bcde3SZi Shen Lim emit(A64_LSL(is64, dst, dst, imm), ctx); 386e54bcde3SZi Shen Lim break; 387e54bcde3SZi Shen Lim case BPF_ALU | BPF_RSH | BPF_K: 388e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_RSH | BPF_K: 389e54bcde3SZi Shen Lim emit(A64_LSR(is64, dst, dst, imm), ctx); 390e54bcde3SZi Shen Lim break; 391e54bcde3SZi Shen Lim case BPF_ALU | BPF_ARSH | BPF_K: 392e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_ARSH | BPF_K: 393e54bcde3SZi Shen Lim emit(A64_ASR(is64, dst, dst, imm), ctx); 394e54bcde3SZi Shen Lim break; 395e54bcde3SZi Shen Lim 396e54bcde3SZi Shen Lim #define check_imm(bits, imm) do { \ 397e54bcde3SZi Shen Lim if ((((imm) > 0) && ((imm) >> (bits))) || \ 398e54bcde3SZi Shen Lim (((imm) < 0) && (~(imm) >> (bits)))) { \ 399e54bcde3SZi Shen Lim pr_info("[%2d] imm=%d(0x%x) out of range\n", \ 400e54bcde3SZi Shen Lim i, imm, imm); \ 401e54bcde3SZi Shen Lim return -EINVAL; \ 402e54bcde3SZi Shen Lim } \ 403e54bcde3SZi Shen Lim } while (0) 404e54bcde3SZi Shen Lim #define check_imm19(imm) check_imm(19, imm) 405e54bcde3SZi Shen Lim #define check_imm26(imm) check_imm(26, imm) 406e54bcde3SZi Shen Lim 407e54bcde3SZi Shen Lim /* JUMP off */ 408e54bcde3SZi Shen Lim case BPF_JMP | BPF_JA: 409e54bcde3SZi Shen Lim jmp_offset = bpf2a64_offset(i + off, i, ctx); 410e54bcde3SZi Shen Lim check_imm26(jmp_offset); 411e54bcde3SZi Shen Lim emit(A64_B(jmp_offset), ctx); 412e54bcde3SZi Shen Lim break; 413e54bcde3SZi Shen Lim /* IF (dst COND src) JUMP off */ 414e54bcde3SZi Shen Lim case BPF_JMP | BPF_JEQ | BPF_X: 415e54bcde3SZi Shen Lim case BPF_JMP | BPF_JGT | BPF_X: 416e54bcde3SZi Shen Lim case BPF_JMP | BPF_JGE | BPF_X: 417e54bcde3SZi Shen Lim case BPF_JMP | BPF_JNE | BPF_X: 418e54bcde3SZi Shen Lim case BPF_JMP | BPF_JSGT | BPF_X: 419e54bcde3SZi Shen Lim case BPF_JMP | BPF_JSGE | BPF_X: 420e54bcde3SZi Shen Lim emit(A64_CMP(1, dst, src), ctx); 421e54bcde3SZi Shen Lim emit_cond_jmp: 422e54bcde3SZi Shen Lim jmp_offset = bpf2a64_offset(i + off, i, ctx); 423e54bcde3SZi Shen Lim check_imm19(jmp_offset); 424e54bcde3SZi Shen Lim switch (BPF_OP(code)) { 425e54bcde3SZi Shen Lim case BPF_JEQ: 426e54bcde3SZi Shen Lim jmp_cond = A64_COND_EQ; 427e54bcde3SZi Shen Lim break; 428e54bcde3SZi Shen Lim case BPF_JGT: 429e54bcde3SZi Shen Lim jmp_cond = A64_COND_HI; 430e54bcde3SZi Shen Lim break; 431e54bcde3SZi Shen Lim case BPF_JGE: 432e54bcde3SZi Shen Lim jmp_cond = A64_COND_CS; 433e54bcde3SZi Shen Lim break; 434e54bcde3SZi Shen Lim case BPF_JNE: 435e54bcde3SZi Shen Lim jmp_cond = A64_COND_NE; 436e54bcde3SZi Shen Lim break; 437e54bcde3SZi Shen Lim case BPF_JSGT: 438e54bcde3SZi Shen Lim jmp_cond = A64_COND_GT; 439e54bcde3SZi Shen Lim break; 440e54bcde3SZi Shen Lim case BPF_JSGE: 441e54bcde3SZi Shen Lim jmp_cond = A64_COND_GE; 442e54bcde3SZi Shen Lim break; 443e54bcde3SZi Shen Lim default: 444e54bcde3SZi Shen Lim return -EFAULT; 445e54bcde3SZi Shen Lim } 446e54bcde3SZi Shen Lim emit(A64_B_(jmp_cond, jmp_offset), ctx); 447e54bcde3SZi Shen Lim break; 448e54bcde3SZi Shen Lim case BPF_JMP | BPF_JSET | BPF_X: 449e54bcde3SZi Shen Lim emit(A64_TST(1, dst, src), ctx); 450e54bcde3SZi Shen Lim goto emit_cond_jmp; 451e54bcde3SZi Shen Lim /* IF (dst COND imm) JUMP off */ 452e54bcde3SZi Shen Lim case BPF_JMP | BPF_JEQ | BPF_K: 453e54bcde3SZi Shen Lim case BPF_JMP | BPF_JGT | BPF_K: 454e54bcde3SZi Shen Lim case BPF_JMP | BPF_JGE | BPF_K: 455e54bcde3SZi Shen Lim case BPF_JMP | BPF_JNE | BPF_K: 456e54bcde3SZi Shen Lim case BPF_JMP | BPF_JSGT | BPF_K: 457e54bcde3SZi Shen Lim case BPF_JMP | BPF_JSGE | BPF_K: 458e54bcde3SZi Shen Lim ctx->tmp_used = 1; 459e54bcde3SZi Shen Lim emit_a64_mov_i(1, tmp, imm, ctx); 460e54bcde3SZi Shen Lim emit(A64_CMP(1, dst, tmp), ctx); 461e54bcde3SZi Shen Lim goto emit_cond_jmp; 462e54bcde3SZi Shen Lim case BPF_JMP | BPF_JSET | BPF_K: 463e54bcde3SZi Shen Lim ctx->tmp_used = 1; 464e54bcde3SZi Shen Lim emit_a64_mov_i(1, tmp, imm, ctx); 465e54bcde3SZi Shen Lim emit(A64_TST(1, dst, tmp), ctx); 466e54bcde3SZi Shen Lim goto emit_cond_jmp; 467e54bcde3SZi Shen Lim /* function call */ 468e54bcde3SZi Shen Lim case BPF_JMP | BPF_CALL: 469e54bcde3SZi Shen Lim { 470e54bcde3SZi Shen Lim const u8 r0 = bpf2a64[BPF_REG_0]; 471e54bcde3SZi Shen Lim const u64 func = (u64)__bpf_call_base + imm; 472e54bcde3SZi Shen Lim 473e54bcde3SZi Shen Lim ctx->tmp_used = 1; 474e54bcde3SZi Shen Lim emit_a64_mov_i64(tmp, func, ctx); 475e54bcde3SZi Shen Lim emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx); 476e54bcde3SZi Shen Lim emit(A64_MOV(1, A64_FP, A64_SP), ctx); 477e54bcde3SZi Shen Lim emit(A64_BLR(tmp), ctx); 478e54bcde3SZi Shen Lim emit(A64_MOV(1, r0, A64_R(0)), ctx); 479e54bcde3SZi Shen Lim emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx); 480e54bcde3SZi Shen Lim break; 481e54bcde3SZi Shen Lim } 482e54bcde3SZi Shen Lim /* function return */ 483e54bcde3SZi Shen Lim case BPF_JMP | BPF_EXIT: 48451c9fbb1SZi Shen Lim /* Optimization: when last instruction is EXIT, 48551c9fbb1SZi Shen Lim simply fallthrough to epilogue. */ 486e54bcde3SZi Shen Lim if (i == ctx->prog->len - 1) 487e54bcde3SZi Shen Lim break; 488e54bcde3SZi Shen Lim jmp_offset = epilogue_offset(ctx); 489e54bcde3SZi Shen Lim check_imm26(jmp_offset); 490e54bcde3SZi Shen Lim emit(A64_B(jmp_offset), ctx); 491e54bcde3SZi Shen Lim break; 492e54bcde3SZi Shen Lim 49330d3d94cSZi Shen Lim /* dst = imm64 */ 49430d3d94cSZi Shen Lim case BPF_LD | BPF_IMM | BPF_DW: 49530d3d94cSZi Shen Lim { 49630d3d94cSZi Shen Lim const struct bpf_insn insn1 = insn[1]; 49730d3d94cSZi Shen Lim u64 imm64; 49830d3d94cSZi Shen Lim 49930d3d94cSZi Shen Lim if (insn1.code != 0 || insn1.src_reg != 0 || 50030d3d94cSZi Shen Lim insn1.dst_reg != 0 || insn1.off != 0) { 50130d3d94cSZi Shen Lim /* Note: verifier in BPF core must catch invalid 50230d3d94cSZi Shen Lim * instructions. 50330d3d94cSZi Shen Lim */ 50430d3d94cSZi Shen Lim pr_err_once("Invalid BPF_LD_IMM64 instruction\n"); 50530d3d94cSZi Shen Lim return -EINVAL; 50630d3d94cSZi Shen Lim } 50730d3d94cSZi Shen Lim 5081e4df6b7SXi Wang imm64 = (u64)insn1.imm << 32 | (u32)imm; 50930d3d94cSZi Shen Lim emit_a64_mov_i64(dst, imm64, ctx); 51030d3d94cSZi Shen Lim 51130d3d94cSZi Shen Lim return 1; 51230d3d94cSZi Shen Lim } 51330d3d94cSZi Shen Lim 514e54bcde3SZi Shen Lim /* LDX: dst = *(size *)(src + off) */ 515e54bcde3SZi Shen Lim case BPF_LDX | BPF_MEM | BPF_W: 516e54bcde3SZi Shen Lim case BPF_LDX | BPF_MEM | BPF_H: 517e54bcde3SZi Shen Lim case BPF_LDX | BPF_MEM | BPF_B: 518e54bcde3SZi Shen Lim case BPF_LDX | BPF_MEM | BPF_DW: 519e54bcde3SZi Shen Lim ctx->tmp_used = 1; 520e54bcde3SZi Shen Lim emit_a64_mov_i(1, tmp, off, ctx); 521e54bcde3SZi Shen Lim switch (BPF_SIZE(code)) { 522e54bcde3SZi Shen Lim case BPF_W: 523e54bcde3SZi Shen Lim emit(A64_LDR32(dst, src, tmp), ctx); 524e54bcde3SZi Shen Lim break; 525e54bcde3SZi Shen Lim case BPF_H: 526e54bcde3SZi Shen Lim emit(A64_LDRH(dst, src, tmp), ctx); 527e54bcde3SZi Shen Lim break; 528e54bcde3SZi Shen Lim case BPF_B: 529e54bcde3SZi Shen Lim emit(A64_LDRB(dst, src, tmp), ctx); 530e54bcde3SZi Shen Lim break; 531e54bcde3SZi Shen Lim case BPF_DW: 532e54bcde3SZi Shen Lim emit(A64_LDR64(dst, src, tmp), ctx); 533e54bcde3SZi Shen Lim break; 534e54bcde3SZi Shen Lim } 535e54bcde3SZi Shen Lim break; 536e54bcde3SZi Shen Lim 537e54bcde3SZi Shen Lim /* ST: *(size *)(dst + off) = imm */ 538e54bcde3SZi Shen Lim case BPF_ST | BPF_MEM | BPF_W: 539e54bcde3SZi Shen Lim case BPF_ST | BPF_MEM | BPF_H: 540e54bcde3SZi Shen Lim case BPF_ST | BPF_MEM | BPF_B: 541e54bcde3SZi Shen Lim case BPF_ST | BPF_MEM | BPF_DW: 542e54bcde3SZi Shen Lim goto notyet; 543e54bcde3SZi Shen Lim 544e54bcde3SZi Shen Lim /* STX: *(size *)(dst + off) = src */ 545e54bcde3SZi Shen Lim case BPF_STX | BPF_MEM | BPF_W: 546e54bcde3SZi Shen Lim case BPF_STX | BPF_MEM | BPF_H: 547e54bcde3SZi Shen Lim case BPF_STX | BPF_MEM | BPF_B: 548e54bcde3SZi Shen Lim case BPF_STX | BPF_MEM | BPF_DW: 549e54bcde3SZi Shen Lim ctx->tmp_used = 1; 550e54bcde3SZi Shen Lim emit_a64_mov_i(1, tmp, off, ctx); 551e54bcde3SZi Shen Lim switch (BPF_SIZE(code)) { 552e54bcde3SZi Shen Lim case BPF_W: 553e54bcde3SZi Shen Lim emit(A64_STR32(src, dst, tmp), ctx); 554e54bcde3SZi Shen Lim break; 555e54bcde3SZi Shen Lim case BPF_H: 556e54bcde3SZi Shen Lim emit(A64_STRH(src, dst, tmp), ctx); 557e54bcde3SZi Shen Lim break; 558e54bcde3SZi Shen Lim case BPF_B: 559e54bcde3SZi Shen Lim emit(A64_STRB(src, dst, tmp), ctx); 560e54bcde3SZi Shen Lim break; 561e54bcde3SZi Shen Lim case BPF_DW: 562e54bcde3SZi Shen Lim emit(A64_STR64(src, dst, tmp), ctx); 563e54bcde3SZi Shen Lim break; 564e54bcde3SZi Shen Lim } 565e54bcde3SZi Shen Lim break; 566e54bcde3SZi Shen Lim /* STX XADD: lock *(u32 *)(dst + off) += src */ 567e54bcde3SZi Shen Lim case BPF_STX | BPF_XADD | BPF_W: 568e54bcde3SZi Shen Lim /* STX XADD: lock *(u64 *)(dst + off) += src */ 569e54bcde3SZi Shen Lim case BPF_STX | BPF_XADD | BPF_DW: 570e54bcde3SZi Shen Lim goto notyet; 571e54bcde3SZi Shen Lim 572e54bcde3SZi Shen Lim /* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + imm)) */ 573e54bcde3SZi Shen Lim case BPF_LD | BPF_ABS | BPF_W: 574e54bcde3SZi Shen Lim case BPF_LD | BPF_ABS | BPF_H: 575e54bcde3SZi Shen Lim case BPF_LD | BPF_ABS | BPF_B: 576e54bcde3SZi Shen Lim /* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + src + imm)) */ 577e54bcde3SZi Shen Lim case BPF_LD | BPF_IND | BPF_W: 578e54bcde3SZi Shen Lim case BPF_LD | BPF_IND | BPF_H: 579e54bcde3SZi Shen Lim case BPF_LD | BPF_IND | BPF_B: 580e54bcde3SZi Shen Lim { 581e54bcde3SZi Shen Lim const u8 r0 = bpf2a64[BPF_REG_0]; /* r0 = return value */ 582e54bcde3SZi Shen Lim const u8 r6 = bpf2a64[BPF_REG_6]; /* r6 = pointer to sk_buff */ 583e54bcde3SZi Shen Lim const u8 fp = bpf2a64[BPF_REG_FP]; 584e54bcde3SZi Shen Lim const u8 r1 = bpf2a64[BPF_REG_1]; /* r1: struct sk_buff *skb */ 585e54bcde3SZi Shen Lim const u8 r2 = bpf2a64[BPF_REG_2]; /* r2: int k */ 586e54bcde3SZi Shen Lim const u8 r3 = bpf2a64[BPF_REG_3]; /* r3: unsigned int size */ 587e54bcde3SZi Shen Lim const u8 r4 = bpf2a64[BPF_REG_4]; /* r4: void *buffer */ 588e54bcde3SZi Shen Lim const u8 r5 = bpf2a64[BPF_REG_5]; /* r5: void *(*func)(...) */ 589e54bcde3SZi Shen Lim int size; 590e54bcde3SZi Shen Lim 591e54bcde3SZi Shen Lim emit(A64_MOV(1, r1, r6), ctx); 592e54bcde3SZi Shen Lim emit_a64_mov_i(0, r2, imm, ctx); 593e54bcde3SZi Shen Lim if (BPF_MODE(code) == BPF_IND) 594e54bcde3SZi Shen Lim emit(A64_ADD(0, r2, r2, src), ctx); 595e54bcde3SZi Shen Lim switch (BPF_SIZE(code)) { 596e54bcde3SZi Shen Lim case BPF_W: 597e54bcde3SZi Shen Lim size = 4; 598e54bcde3SZi Shen Lim break; 599e54bcde3SZi Shen Lim case BPF_H: 600e54bcde3SZi Shen Lim size = 2; 601e54bcde3SZi Shen Lim break; 602e54bcde3SZi Shen Lim case BPF_B: 603e54bcde3SZi Shen Lim size = 1; 604e54bcde3SZi Shen Lim break; 605e54bcde3SZi Shen Lim default: 606e54bcde3SZi Shen Lim return -EINVAL; 607e54bcde3SZi Shen Lim } 608e54bcde3SZi Shen Lim emit_a64_mov_i64(r3, size, ctx); 609e54bcde3SZi Shen Lim emit(A64_ADD_I(1, r4, fp, MAX_BPF_STACK), ctx); 610e54bcde3SZi Shen Lim emit_a64_mov_i64(r5, (unsigned long)bpf_load_pointer, ctx); 611e54bcde3SZi Shen Lim emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx); 612e54bcde3SZi Shen Lim emit(A64_MOV(1, A64_FP, A64_SP), ctx); 613e54bcde3SZi Shen Lim emit(A64_BLR(r5), ctx); 614e54bcde3SZi Shen Lim emit(A64_MOV(1, r0, A64_R(0)), ctx); 615e54bcde3SZi Shen Lim emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx); 616e54bcde3SZi Shen Lim 617e54bcde3SZi Shen Lim jmp_offset = epilogue_offset(ctx); 618e54bcde3SZi Shen Lim check_imm19(jmp_offset); 619e54bcde3SZi Shen Lim emit(A64_CBZ(1, r0, jmp_offset), ctx); 620e54bcde3SZi Shen Lim emit(A64_MOV(1, r5, r0), ctx); 621e54bcde3SZi Shen Lim switch (BPF_SIZE(code)) { 622e54bcde3SZi Shen Lim case BPF_W: 623e54bcde3SZi Shen Lim emit(A64_LDR32(r0, r5, A64_ZR), ctx); 624e54bcde3SZi Shen Lim #ifndef CONFIG_CPU_BIG_ENDIAN 625e54bcde3SZi Shen Lim emit(A64_REV32(0, r0, r0), ctx); 626e54bcde3SZi Shen Lim #endif 627e54bcde3SZi Shen Lim break; 628e54bcde3SZi Shen Lim case BPF_H: 629e54bcde3SZi Shen Lim emit(A64_LDRH(r0, r5, A64_ZR), ctx); 630e54bcde3SZi Shen Lim #ifndef CONFIG_CPU_BIG_ENDIAN 631e54bcde3SZi Shen Lim emit(A64_REV16(0, r0, r0), ctx); 632e54bcde3SZi Shen Lim #endif 633e54bcde3SZi Shen Lim break; 634e54bcde3SZi Shen Lim case BPF_B: 635e54bcde3SZi Shen Lim emit(A64_LDRB(r0, r5, A64_ZR), ctx); 636e54bcde3SZi Shen Lim break; 637e54bcde3SZi Shen Lim } 638e54bcde3SZi Shen Lim break; 639e54bcde3SZi Shen Lim } 640e54bcde3SZi Shen Lim notyet: 641e54bcde3SZi Shen Lim pr_info_once("*** NOT YET: opcode %02x ***\n", code); 642e54bcde3SZi Shen Lim return -EFAULT; 643e54bcde3SZi Shen Lim 644e54bcde3SZi Shen Lim default: 645e54bcde3SZi Shen Lim pr_err_once("unknown opcode %02x\n", code); 646e54bcde3SZi Shen Lim return -EINVAL; 647e54bcde3SZi Shen Lim } 648e54bcde3SZi Shen Lim 649e54bcde3SZi Shen Lim return 0; 650e54bcde3SZi Shen Lim } 651e54bcde3SZi Shen Lim 652e54bcde3SZi Shen Lim static int build_body(struct jit_ctx *ctx) 653e54bcde3SZi Shen Lim { 654e54bcde3SZi Shen Lim const struct bpf_prog *prog = ctx->prog; 655e54bcde3SZi Shen Lim int i; 656e54bcde3SZi Shen Lim 657e54bcde3SZi Shen Lim for (i = 0; i < prog->len; i++) { 658e54bcde3SZi Shen Lim const struct bpf_insn *insn = &prog->insnsi[i]; 659e54bcde3SZi Shen Lim int ret; 660e54bcde3SZi Shen Lim 6618eee539dSXi Wang ret = build_insn(insn, ctx); 6628eee539dSXi Wang 663e54bcde3SZi Shen Lim if (ctx->image == NULL) 664e54bcde3SZi Shen Lim ctx->offset[i] = ctx->idx; 665e54bcde3SZi Shen Lim 66630d3d94cSZi Shen Lim if (ret > 0) { 66730d3d94cSZi Shen Lim i++; 66830d3d94cSZi Shen Lim continue; 66930d3d94cSZi Shen Lim } 670e54bcde3SZi Shen Lim if (ret) 671e54bcde3SZi Shen Lim return ret; 672e54bcde3SZi Shen Lim } 673e54bcde3SZi Shen Lim 674e54bcde3SZi Shen Lim return 0; 675e54bcde3SZi Shen Lim } 676e54bcde3SZi Shen Lim 677e54bcde3SZi Shen Lim static inline void bpf_flush_icache(void *start, void *end) 678e54bcde3SZi Shen Lim { 679e54bcde3SZi Shen Lim flush_icache_range((unsigned long)start, (unsigned long)end); 680e54bcde3SZi Shen Lim } 681e54bcde3SZi Shen Lim 682e54bcde3SZi Shen Lim void bpf_jit_compile(struct bpf_prog *prog) 683e54bcde3SZi Shen Lim { 684e54bcde3SZi Shen Lim /* Nothing to do here. We support Internal BPF. */ 685e54bcde3SZi Shen Lim } 686e54bcde3SZi Shen Lim 687e54bcde3SZi Shen Lim void bpf_int_jit_compile(struct bpf_prog *prog) 688e54bcde3SZi Shen Lim { 689b569c1c6SDaniel Borkmann struct bpf_binary_header *header; 690e54bcde3SZi Shen Lim struct jit_ctx ctx; 691e54bcde3SZi Shen Lim int image_size; 692b569c1c6SDaniel Borkmann u8 *image_ptr; 693e54bcde3SZi Shen Lim 694e54bcde3SZi Shen Lim if (!bpf_jit_enable) 695e54bcde3SZi Shen Lim return; 696e54bcde3SZi Shen Lim 697e54bcde3SZi Shen Lim if (!prog || !prog->len) 698e54bcde3SZi Shen Lim return; 699e54bcde3SZi Shen Lim 700e54bcde3SZi Shen Lim memset(&ctx, 0, sizeof(ctx)); 701e54bcde3SZi Shen Lim ctx.prog = prog; 702e54bcde3SZi Shen Lim 703e54bcde3SZi Shen Lim ctx.offset = kcalloc(prog->len, sizeof(int), GFP_KERNEL); 704e54bcde3SZi Shen Lim if (ctx.offset == NULL) 705e54bcde3SZi Shen Lim return; 706e54bcde3SZi Shen Lim 707e54bcde3SZi Shen Lim /* 1. Initial fake pass to compute ctx->idx. */ 708e54bcde3SZi Shen Lim 70951c9fbb1SZi Shen Lim /* Fake pass to fill in ctx->offset and ctx->tmp_used. */ 710e54bcde3SZi Shen Lim if (build_body(&ctx)) 711e54bcde3SZi Shen Lim goto out; 712e54bcde3SZi Shen Lim 713e54bcde3SZi Shen Lim build_prologue(&ctx); 71451c9fbb1SZi Shen Lim 71551c9fbb1SZi Shen Lim ctx.epilogue_offset = ctx.idx; 716e54bcde3SZi Shen Lim build_epilogue(&ctx); 717e54bcde3SZi Shen Lim 718e54bcde3SZi Shen Lim /* Now we know the actual image size. */ 719e54bcde3SZi Shen Lim image_size = sizeof(u32) * ctx.idx; 720b569c1c6SDaniel Borkmann header = bpf_jit_binary_alloc(image_size, &image_ptr, 721b569c1c6SDaniel Borkmann sizeof(u32), jit_fill_hole); 722b569c1c6SDaniel Borkmann if (header == NULL) 723e54bcde3SZi Shen Lim goto out; 724e54bcde3SZi Shen Lim 725e54bcde3SZi Shen Lim /* 2. Now, the actual pass. */ 726e54bcde3SZi Shen Lim 727b569c1c6SDaniel Borkmann ctx.image = (u32 *)image_ptr; 728e54bcde3SZi Shen Lim ctx.idx = 0; 729b569c1c6SDaniel Borkmann 730e54bcde3SZi Shen Lim build_prologue(&ctx); 731e54bcde3SZi Shen Lim 73260ef0494SDaniel Borkmann if (build_body(&ctx)) { 733b569c1c6SDaniel Borkmann bpf_jit_binary_free(header); 734e54bcde3SZi Shen Lim goto out; 73560ef0494SDaniel Borkmann } 736e54bcde3SZi Shen Lim 737e54bcde3SZi Shen Lim build_epilogue(&ctx); 738e54bcde3SZi Shen Lim 739e54bcde3SZi Shen Lim /* And we're done. */ 740e54bcde3SZi Shen Lim if (bpf_jit_enable > 1) 741e54bcde3SZi Shen Lim bpf_jit_dump(prog->len, image_size, 2, ctx.image); 742e54bcde3SZi Shen Lim 743e54bcde3SZi Shen Lim bpf_flush_icache(ctx.image, ctx.image + ctx.idx); 744b569c1c6SDaniel Borkmann 745b569c1c6SDaniel Borkmann set_memory_ro((unsigned long)header, header->pages); 746e54bcde3SZi Shen Lim prog->bpf_func = (void *)ctx.image; 747*a91263d5SDaniel Borkmann prog->jited = 1; 748e54bcde3SZi Shen Lim out: 749e54bcde3SZi Shen Lim kfree(ctx.offset); 750e54bcde3SZi Shen Lim } 751e54bcde3SZi Shen Lim 752e54bcde3SZi Shen Lim void bpf_jit_free(struct bpf_prog *prog) 753e54bcde3SZi Shen Lim { 754b569c1c6SDaniel Borkmann unsigned long addr = (unsigned long)prog->bpf_func & PAGE_MASK; 755b569c1c6SDaniel Borkmann struct bpf_binary_header *header = (void *)addr; 756e54bcde3SZi Shen Lim 757b569c1c6SDaniel Borkmann if (!prog->jited) 758b569c1c6SDaniel Borkmann goto free_filter; 759b569c1c6SDaniel Borkmann 760b569c1c6SDaniel Borkmann set_memory_rw(addr, header->pages); 761b569c1c6SDaniel Borkmann bpf_jit_binary_free(header); 762b569c1c6SDaniel Borkmann 763b569c1c6SDaniel Borkmann free_filter: 764b569c1c6SDaniel Borkmann bpf_prog_unlock_free(prog); 765e54bcde3SZi Shen Lim } 766