1e54bcde3SZi Shen Lim /* 2e54bcde3SZi Shen Lim * BPF JIT compiler for ARM64 3e54bcde3SZi Shen Lim * 442ff712bSZi Shen Lim * Copyright (C) 2014-2016 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 21ddb55992SZi Shen Lim #include <linux/bpf.h> 22e54bcde3SZi Shen Lim #include <linux/filter.h> 23e54bcde3SZi Shen Lim #include <linux/printk.h> 24e54bcde3SZi Shen Lim #include <linux/skbuff.h> 25e54bcde3SZi Shen Lim #include <linux/slab.h> 26b569c1c6SDaniel Borkmann 27e54bcde3SZi Shen Lim #include <asm/byteorder.h> 28e54bcde3SZi Shen Lim #include <asm/cacheflush.h> 29b569c1c6SDaniel Borkmann #include <asm/debug-monitors.h> 30d4bbc30bSLaura Abbott #include <asm/set_memory.h> 31e54bcde3SZi Shen Lim 32e54bcde3SZi Shen Lim #include "bpf_jit.h" 33e54bcde3SZi Shen Lim 34e54bcde3SZi Shen Lim int bpf_jit_enable __read_mostly; 35e54bcde3SZi Shen Lim 3626eb042eSDaniel Borkmann #define TMP_REG_1 (MAX_BPF_JIT_REG + 0) 3726eb042eSDaniel Borkmann #define TMP_REG_2 (MAX_BPF_JIT_REG + 1) 38ddb55992SZi Shen Lim #define TCALL_CNT (MAX_BPF_JIT_REG + 2) 397005cadeSDaniel Borkmann #define TMP_REG_3 (MAX_BPF_JIT_REG + 3) 40e54bcde3SZi Shen Lim 41e54bcde3SZi Shen Lim /* Map BPF registers to A64 registers */ 42e54bcde3SZi Shen Lim static const int bpf2a64[] = { 43e54bcde3SZi Shen Lim /* return value from in-kernel function, and exit value from eBPF */ 44e54bcde3SZi Shen Lim [BPF_REG_0] = A64_R(7), 45e54bcde3SZi Shen Lim /* arguments from eBPF program to in-kernel function */ 46e54bcde3SZi Shen Lim [BPF_REG_1] = A64_R(0), 47e54bcde3SZi Shen Lim [BPF_REG_2] = A64_R(1), 48e54bcde3SZi Shen Lim [BPF_REG_3] = A64_R(2), 49e54bcde3SZi Shen Lim [BPF_REG_4] = A64_R(3), 50e54bcde3SZi Shen Lim [BPF_REG_5] = A64_R(4), 51e54bcde3SZi Shen Lim /* callee saved registers that in-kernel function will preserve */ 52e54bcde3SZi Shen Lim [BPF_REG_6] = A64_R(19), 53e54bcde3SZi Shen Lim [BPF_REG_7] = A64_R(20), 54e54bcde3SZi Shen Lim [BPF_REG_8] = A64_R(21), 55e54bcde3SZi Shen Lim [BPF_REG_9] = A64_R(22), 56e54bcde3SZi Shen Lim /* read-only frame pointer to access stack */ 57ec0738dbSYang Shi [BPF_REG_FP] = A64_R(25), 584c1cd4fdSYang Shi /* temporary registers for internal BPF JIT */ 594c1cd4fdSYang Shi [TMP_REG_1] = A64_R(10), 604c1cd4fdSYang Shi [TMP_REG_2] = A64_R(11), 617005cadeSDaniel Borkmann [TMP_REG_3] = A64_R(12), 62ddb55992SZi Shen Lim /* tail_call_cnt */ 63ddb55992SZi Shen Lim [TCALL_CNT] = A64_R(26), 6426eb042eSDaniel Borkmann /* temporary register for blinding constants */ 6526eb042eSDaniel Borkmann [BPF_REG_AX] = A64_R(9), 66e54bcde3SZi Shen Lim }; 67e54bcde3SZi Shen Lim 68e54bcde3SZi Shen Lim struct jit_ctx { 69e54bcde3SZi Shen Lim const struct bpf_prog *prog; 70e54bcde3SZi Shen Lim int idx; 7151c9fbb1SZi Shen Lim int epilogue_offset; 72e54bcde3SZi Shen Lim int *offset; 73425e1ed7SLuc Van Oostenryck __le32 *image; 74f1c9eed7SDaniel Borkmann u32 stack_size; 75e54bcde3SZi Shen Lim }; 76e54bcde3SZi Shen Lim 77e54bcde3SZi Shen Lim static inline void emit(const u32 insn, struct jit_ctx *ctx) 78e54bcde3SZi Shen Lim { 79e54bcde3SZi Shen Lim if (ctx->image != NULL) 80e54bcde3SZi Shen Lim ctx->image[ctx->idx] = cpu_to_le32(insn); 81e54bcde3SZi Shen Lim 82e54bcde3SZi Shen Lim ctx->idx++; 83e54bcde3SZi Shen Lim } 84e54bcde3SZi Shen Lim 85e54bcde3SZi Shen Lim static inline void emit_a64_mov_i64(const int reg, const u64 val, 86e54bcde3SZi Shen Lim struct jit_ctx *ctx) 87e54bcde3SZi Shen Lim { 88e54bcde3SZi Shen Lim u64 tmp = val; 89e54bcde3SZi Shen Lim int shift = 0; 90e54bcde3SZi Shen Lim 91e54bcde3SZi Shen Lim emit(A64_MOVZ(1, reg, tmp & 0xffff, shift), ctx); 92e54bcde3SZi Shen Lim tmp >>= 16; 93e54bcde3SZi Shen Lim shift += 16; 94e54bcde3SZi Shen Lim while (tmp) { 95e54bcde3SZi Shen Lim if (tmp & 0xffff) 96e54bcde3SZi Shen Lim emit(A64_MOVK(1, reg, tmp & 0xffff, shift), ctx); 97e54bcde3SZi Shen Lim tmp >>= 16; 98e54bcde3SZi Shen Lim shift += 16; 99e54bcde3SZi Shen Lim } 100e54bcde3SZi Shen Lim } 101e54bcde3SZi Shen Lim 102*db496944SAlexei Starovoitov static inline void emit_addr_mov_i64(const int reg, const u64 val, 103*db496944SAlexei Starovoitov struct jit_ctx *ctx) 104*db496944SAlexei Starovoitov { 105*db496944SAlexei Starovoitov u64 tmp = val; 106*db496944SAlexei Starovoitov int shift = 0; 107*db496944SAlexei Starovoitov 108*db496944SAlexei Starovoitov emit(A64_MOVZ(1, reg, tmp & 0xffff, shift), ctx); 109*db496944SAlexei Starovoitov for (;shift < 48;) { 110*db496944SAlexei Starovoitov tmp >>= 16; 111*db496944SAlexei Starovoitov shift += 16; 112*db496944SAlexei Starovoitov emit(A64_MOVK(1, reg, tmp & 0xffff, shift), ctx); 113*db496944SAlexei Starovoitov } 114*db496944SAlexei Starovoitov } 115*db496944SAlexei Starovoitov 116e54bcde3SZi Shen Lim static inline void emit_a64_mov_i(const int is64, const int reg, 117e54bcde3SZi Shen Lim const s32 val, struct jit_ctx *ctx) 118e54bcde3SZi Shen Lim { 119e54bcde3SZi Shen Lim u16 hi = val >> 16; 120e54bcde3SZi Shen Lim u16 lo = val & 0xffff; 121e54bcde3SZi Shen Lim 122e54bcde3SZi Shen Lim if (hi & 0x8000) { 123e54bcde3SZi Shen Lim if (hi == 0xffff) { 124e54bcde3SZi Shen Lim emit(A64_MOVN(is64, reg, (u16)~lo, 0), ctx); 125e54bcde3SZi Shen Lim } else { 126e54bcde3SZi Shen Lim emit(A64_MOVN(is64, reg, (u16)~hi, 16), ctx); 127e54bcde3SZi Shen Lim emit(A64_MOVK(is64, reg, lo, 0), ctx); 128e54bcde3SZi Shen Lim } 129e54bcde3SZi Shen Lim } else { 130e54bcde3SZi Shen Lim emit(A64_MOVZ(is64, reg, lo, 0), ctx); 131e54bcde3SZi Shen Lim if (hi) 132e54bcde3SZi Shen Lim emit(A64_MOVK(is64, reg, hi, 16), ctx); 133e54bcde3SZi Shen Lim } 134e54bcde3SZi Shen Lim } 135e54bcde3SZi Shen Lim 136e54bcde3SZi Shen Lim static inline int bpf2a64_offset(int bpf_to, int bpf_from, 137e54bcde3SZi Shen Lim const struct jit_ctx *ctx) 138e54bcde3SZi Shen Lim { 1398eee539dSXi Wang int to = ctx->offset[bpf_to]; 140e54bcde3SZi Shen Lim /* -1 to account for the Branch instruction */ 1418eee539dSXi Wang int from = ctx->offset[bpf_from] - 1; 142e54bcde3SZi Shen Lim 143e54bcde3SZi Shen Lim return to - from; 144e54bcde3SZi Shen Lim } 145e54bcde3SZi Shen Lim 146b569c1c6SDaniel Borkmann static void jit_fill_hole(void *area, unsigned int size) 147b569c1c6SDaniel Borkmann { 148425e1ed7SLuc Van Oostenryck __le32 *ptr; 149b569c1c6SDaniel Borkmann /* We are guaranteed to have aligned memory. */ 150b569c1c6SDaniel Borkmann for (ptr = area; size >= sizeof(u32); size -= sizeof(u32)) 151b569c1c6SDaniel Borkmann *ptr++ = cpu_to_le32(AARCH64_BREAK_FAULT); 152b569c1c6SDaniel Borkmann } 153b569c1c6SDaniel Borkmann 154e54bcde3SZi Shen Lim static inline int epilogue_offset(const struct jit_ctx *ctx) 155e54bcde3SZi Shen Lim { 15651c9fbb1SZi Shen Lim int to = ctx->epilogue_offset; 15751c9fbb1SZi Shen Lim int from = ctx->idx; 158e54bcde3SZi Shen Lim 159e54bcde3SZi Shen Lim return to - from; 160e54bcde3SZi Shen Lim } 161e54bcde3SZi Shen Lim 162e54bcde3SZi Shen Lim /* Stack must be multiples of 16B */ 163e54bcde3SZi Shen Lim #define STACK_ALIGN(sz) (((sz) + 15) & ~15) 164e54bcde3SZi Shen Lim 165ddb55992SZi Shen Lim #define PROLOGUE_OFFSET 8 166ddb55992SZi Shen Lim 167ddb55992SZi Shen Lim static int build_prologue(struct jit_ctx *ctx) 168e54bcde3SZi Shen Lim { 169f1c9eed7SDaniel Borkmann const struct bpf_prog *prog = ctx->prog; 170e54bcde3SZi Shen Lim const u8 r6 = bpf2a64[BPF_REG_6]; 171e54bcde3SZi Shen Lim const u8 r7 = bpf2a64[BPF_REG_7]; 172e54bcde3SZi Shen Lim const u8 r8 = bpf2a64[BPF_REG_8]; 173e54bcde3SZi Shen Lim const u8 r9 = bpf2a64[BPF_REG_9]; 174e54bcde3SZi Shen Lim const u8 fp = bpf2a64[BPF_REG_FP]; 175ddb55992SZi Shen Lim const u8 tcc = bpf2a64[TCALL_CNT]; 176ddb55992SZi Shen Lim const int idx0 = ctx->idx; 177ddb55992SZi Shen Lim int cur_offset; 178e54bcde3SZi Shen Lim 179ec0738dbSYang Shi /* 180ec0738dbSYang Shi * BPF prog stack layout 181ec0738dbSYang Shi * 182ec0738dbSYang Shi * high 183ec0738dbSYang Shi * original A64_SP => 0:+-----+ BPF prologue 184ec0738dbSYang Shi * |FP/LR| 185ec0738dbSYang Shi * current A64_FP => -16:+-----+ 186ec0738dbSYang Shi * | ... | callee saved registers 1874c1cd4fdSYang Shi * BPF fp register => -64:+-----+ <= (BPF_FP) 188ec0738dbSYang Shi * | | 189ec0738dbSYang Shi * | ... | BPF prog stack 190ec0738dbSYang Shi * | | 191f1c9eed7SDaniel Borkmann * +-----+ <= (BPF_FP - prog->aux->stack_depth) 192f4b16fceSZi Shen Lim * |RSVD | JIT scratchpad 193f1c9eed7SDaniel Borkmann * current A64_SP => +-----+ <= (BPF_FP - ctx->stack_size) 194ec0738dbSYang Shi * | | 195ec0738dbSYang Shi * | ... | Function call stack 196ec0738dbSYang Shi * | | 197ec0738dbSYang Shi * +-----+ 198ec0738dbSYang Shi * low 199ec0738dbSYang Shi * 200ec0738dbSYang Shi */ 201ec0738dbSYang Shi 202ec0738dbSYang Shi /* Save FP and LR registers to stay align with ARM64 AAPCS */ 203ec0738dbSYang Shi emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx); 204ec0738dbSYang Shi emit(A64_MOV(1, A64_FP, A64_SP), ctx); 205ec0738dbSYang Shi 206ddb55992SZi Shen Lim /* Save callee-saved registers */ 207e54bcde3SZi Shen Lim emit(A64_PUSH(r6, r7, A64_SP), ctx); 208e54bcde3SZi Shen Lim emit(A64_PUSH(r8, r9, A64_SP), ctx); 209ddb55992SZi Shen Lim emit(A64_PUSH(fp, tcc, A64_SP), ctx); 210e54bcde3SZi Shen Lim 211ddb55992SZi Shen Lim /* Set up BPF prog stack base register */ 212e54bcde3SZi Shen Lim emit(A64_MOV(1, fp, A64_SP), ctx); 213e54bcde3SZi Shen Lim 214ddb55992SZi Shen Lim /* Initialize tail_call_cnt */ 215ddb55992SZi Shen Lim emit(A64_MOVZ(1, tcc, 0, 0), ctx); 216ddb55992SZi Shen Lim 217f1c9eed7SDaniel Borkmann /* 4 byte extra for skb_copy_bits buffer */ 218f1c9eed7SDaniel Borkmann ctx->stack_size = prog->aux->stack_depth + 4; 219f1c9eed7SDaniel Borkmann ctx->stack_size = STACK_ALIGN(ctx->stack_size); 220f1c9eed7SDaniel Borkmann 221ec0738dbSYang Shi /* Set up function call stack */ 222f1c9eed7SDaniel Borkmann emit(A64_SUB_I(1, A64_SP, A64_SP, ctx->stack_size), ctx); 223ddb55992SZi Shen Lim 224ddb55992SZi Shen Lim cur_offset = ctx->idx - idx0; 225ddb55992SZi Shen Lim if (cur_offset != PROLOGUE_OFFSET) { 226ddb55992SZi Shen Lim pr_err_once("PROLOGUE_OFFSET = %d, expected %d!\n", 227ddb55992SZi Shen Lim cur_offset, PROLOGUE_OFFSET); 228ddb55992SZi Shen Lim return -1; 229ddb55992SZi Shen Lim } 230ddb55992SZi Shen Lim return 0; 231ddb55992SZi Shen Lim } 232ddb55992SZi Shen Lim 233ddb55992SZi Shen Lim static int out_offset = -1; /* initialized on the first pass of build_body() */ 234ddb55992SZi Shen Lim static int emit_bpf_tail_call(struct jit_ctx *ctx) 235ddb55992SZi Shen Lim { 236ddb55992SZi Shen Lim /* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */ 237ddb55992SZi Shen Lim const u8 r2 = bpf2a64[BPF_REG_2]; 238ddb55992SZi Shen Lim const u8 r3 = bpf2a64[BPF_REG_3]; 239ddb55992SZi Shen Lim 240ddb55992SZi Shen Lim const u8 tmp = bpf2a64[TMP_REG_1]; 241ddb55992SZi Shen Lim const u8 prg = bpf2a64[TMP_REG_2]; 242ddb55992SZi Shen Lim const u8 tcc = bpf2a64[TCALL_CNT]; 243ddb55992SZi Shen Lim const int idx0 = ctx->idx; 244ddb55992SZi Shen Lim #define cur_offset (ctx->idx - idx0) 245ddb55992SZi Shen Lim #define jmp_offset (out_offset - (cur_offset)) 246ddb55992SZi Shen Lim size_t off; 247ddb55992SZi Shen Lim 248ddb55992SZi Shen Lim /* if (index >= array->map.max_entries) 249ddb55992SZi Shen Lim * goto out; 250ddb55992SZi Shen Lim */ 251ddb55992SZi Shen Lim off = offsetof(struct bpf_array, map.max_entries); 252ddb55992SZi Shen Lim emit_a64_mov_i64(tmp, off, ctx); 253ddb55992SZi Shen Lim emit(A64_LDR32(tmp, r2, tmp), ctx); 254ddb55992SZi Shen Lim emit(A64_CMP(0, r3, tmp), ctx); 255ddb55992SZi Shen Lim emit(A64_B_(A64_COND_GE, jmp_offset), ctx); 256ddb55992SZi Shen Lim 257ddb55992SZi Shen Lim /* if (tail_call_cnt > MAX_TAIL_CALL_CNT) 258ddb55992SZi Shen Lim * goto out; 259ddb55992SZi Shen Lim * tail_call_cnt++; 260ddb55992SZi Shen Lim */ 261ddb55992SZi Shen Lim emit_a64_mov_i64(tmp, MAX_TAIL_CALL_CNT, ctx); 262ddb55992SZi Shen Lim emit(A64_CMP(1, tcc, tmp), ctx); 263ddb55992SZi Shen Lim emit(A64_B_(A64_COND_GT, jmp_offset), ctx); 264ddb55992SZi Shen Lim emit(A64_ADD_I(1, tcc, tcc, 1), ctx); 265ddb55992SZi Shen Lim 266ddb55992SZi Shen Lim /* prog = array->ptrs[index]; 267ddb55992SZi Shen Lim * if (prog == NULL) 268ddb55992SZi Shen Lim * goto out; 269ddb55992SZi Shen Lim */ 270ddb55992SZi Shen Lim off = offsetof(struct bpf_array, ptrs); 271ddb55992SZi Shen Lim emit_a64_mov_i64(tmp, off, ctx); 272d8b54110SDaniel Borkmann emit(A64_ADD(1, tmp, r2, tmp), ctx); 273d8b54110SDaniel Borkmann emit(A64_LSL(1, prg, r3, 3), ctx); 274d8b54110SDaniel Borkmann emit(A64_LDR64(prg, tmp, prg), ctx); 275ddb55992SZi Shen Lim emit(A64_CBZ(1, prg, jmp_offset), ctx); 276ddb55992SZi Shen Lim 277ddb55992SZi Shen Lim /* goto *(prog->bpf_func + prologue_size); */ 278ddb55992SZi Shen Lim off = offsetof(struct bpf_prog, bpf_func); 279ddb55992SZi Shen Lim emit_a64_mov_i64(tmp, off, ctx); 280ddb55992SZi Shen Lim emit(A64_LDR64(tmp, prg, tmp), ctx); 281ddb55992SZi Shen Lim emit(A64_ADD_I(1, tmp, tmp, sizeof(u32) * PROLOGUE_OFFSET), ctx); 282ddb55992SZi Shen Lim emit(A64_BR(tmp), ctx); 283ddb55992SZi Shen Lim 284ddb55992SZi Shen Lim /* out: */ 285ddb55992SZi Shen Lim if (out_offset == -1) 286ddb55992SZi Shen Lim out_offset = cur_offset; 287ddb55992SZi Shen Lim if (cur_offset != out_offset) { 288ddb55992SZi Shen Lim pr_err_once("tail_call out_offset = %d, expected %d!\n", 289ddb55992SZi Shen Lim cur_offset, out_offset); 290ddb55992SZi Shen Lim return -1; 291ddb55992SZi Shen Lim } 292ddb55992SZi Shen Lim return 0; 293ddb55992SZi Shen Lim #undef cur_offset 294ddb55992SZi Shen Lim #undef jmp_offset 295e54bcde3SZi Shen Lim } 296e54bcde3SZi Shen Lim 297e54bcde3SZi Shen Lim static void build_epilogue(struct jit_ctx *ctx) 298e54bcde3SZi Shen Lim { 299e54bcde3SZi Shen Lim const u8 r0 = bpf2a64[BPF_REG_0]; 300e54bcde3SZi Shen Lim const u8 r6 = bpf2a64[BPF_REG_6]; 301e54bcde3SZi Shen Lim const u8 r7 = bpf2a64[BPF_REG_7]; 302e54bcde3SZi Shen Lim const u8 r8 = bpf2a64[BPF_REG_8]; 303e54bcde3SZi Shen Lim const u8 r9 = bpf2a64[BPF_REG_9]; 304e54bcde3SZi Shen Lim const u8 fp = bpf2a64[BPF_REG_FP]; 305e54bcde3SZi Shen Lim 306e54bcde3SZi Shen Lim /* We're done with BPF stack */ 307f1c9eed7SDaniel Borkmann emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx); 308e54bcde3SZi Shen Lim 309ec0738dbSYang Shi /* Restore fs (x25) and x26 */ 310ec0738dbSYang Shi emit(A64_POP(fp, A64_R(26), A64_SP), ctx); 311ec0738dbSYang Shi 312e54bcde3SZi Shen Lim /* Restore callee-saved register */ 313e54bcde3SZi Shen Lim emit(A64_POP(r8, r9, A64_SP), ctx); 314e54bcde3SZi Shen Lim emit(A64_POP(r6, r7, A64_SP), ctx); 315e54bcde3SZi Shen Lim 316ec0738dbSYang Shi /* Restore FP/LR registers */ 317ec0738dbSYang Shi emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx); 318e54bcde3SZi Shen Lim 319e54bcde3SZi Shen Lim /* Set return value */ 320e54bcde3SZi Shen Lim emit(A64_MOV(1, A64_R(0), r0), ctx); 321e54bcde3SZi Shen Lim 322e54bcde3SZi Shen Lim emit(A64_RET(A64_LR), ctx); 323e54bcde3SZi Shen Lim } 324e54bcde3SZi Shen Lim 32530d3d94cSZi Shen Lim /* JITs an eBPF instruction. 32630d3d94cSZi Shen Lim * Returns: 32730d3d94cSZi Shen Lim * 0 - successfully JITed an 8-byte eBPF instruction. 32830d3d94cSZi Shen Lim * >0 - successfully JITed a 16-byte eBPF instruction. 32930d3d94cSZi Shen Lim * <0 - failed to JIT. 33030d3d94cSZi Shen Lim */ 331e54bcde3SZi Shen Lim static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx) 332e54bcde3SZi Shen Lim { 333e54bcde3SZi Shen Lim const u8 code = insn->code; 334e54bcde3SZi Shen Lim const u8 dst = bpf2a64[insn->dst_reg]; 335e54bcde3SZi Shen Lim const u8 src = bpf2a64[insn->src_reg]; 336e54bcde3SZi Shen Lim const u8 tmp = bpf2a64[TMP_REG_1]; 337e54bcde3SZi Shen Lim const u8 tmp2 = bpf2a64[TMP_REG_2]; 3387005cadeSDaniel Borkmann const u8 tmp3 = bpf2a64[TMP_REG_3]; 339e54bcde3SZi Shen Lim const s16 off = insn->off; 340e54bcde3SZi Shen Lim const s32 imm = insn->imm; 341e54bcde3SZi Shen Lim const int i = insn - ctx->prog->insnsi; 342e54bcde3SZi Shen Lim const bool is64 = BPF_CLASS(code) == BPF_ALU64; 34385f68fe8SDaniel Borkmann const bool isdw = BPF_SIZE(code) == BPF_DW; 344e54bcde3SZi Shen Lim u8 jmp_cond; 345e54bcde3SZi Shen Lim s32 jmp_offset; 346e54bcde3SZi Shen Lim 347251599e1SZi Shen Lim #define check_imm(bits, imm) do { \ 348251599e1SZi Shen Lim if ((((imm) > 0) && ((imm) >> (bits))) || \ 349251599e1SZi Shen Lim (((imm) < 0) && (~(imm) >> (bits)))) { \ 350251599e1SZi Shen Lim pr_info("[%2d] imm=%d(0x%x) out of range\n", \ 351251599e1SZi Shen Lim i, imm, imm); \ 352251599e1SZi Shen Lim return -EINVAL; \ 353251599e1SZi Shen Lim } \ 354251599e1SZi Shen Lim } while (0) 355251599e1SZi Shen Lim #define check_imm19(imm) check_imm(19, imm) 356251599e1SZi Shen Lim #define check_imm26(imm) check_imm(26, imm) 357251599e1SZi Shen Lim 358e54bcde3SZi Shen Lim switch (code) { 359e54bcde3SZi Shen Lim /* dst = src */ 360e54bcde3SZi Shen Lim case BPF_ALU | BPF_MOV | BPF_X: 361e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_MOV | BPF_X: 362e54bcde3SZi Shen Lim emit(A64_MOV(is64, dst, src), ctx); 363e54bcde3SZi Shen Lim break; 364e54bcde3SZi Shen Lim /* dst = dst OP src */ 365e54bcde3SZi Shen Lim case BPF_ALU | BPF_ADD | BPF_X: 366e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_ADD | BPF_X: 367e54bcde3SZi Shen Lim emit(A64_ADD(is64, dst, dst, src), ctx); 368e54bcde3SZi Shen Lim break; 369e54bcde3SZi Shen Lim case BPF_ALU | BPF_SUB | BPF_X: 370e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_SUB | BPF_X: 371e54bcde3SZi Shen Lim emit(A64_SUB(is64, dst, dst, src), ctx); 372e54bcde3SZi Shen Lim break; 373e54bcde3SZi Shen Lim case BPF_ALU | BPF_AND | BPF_X: 374e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_AND | BPF_X: 375e54bcde3SZi Shen Lim emit(A64_AND(is64, dst, dst, src), ctx); 376e54bcde3SZi Shen Lim break; 377e54bcde3SZi Shen Lim case BPF_ALU | BPF_OR | BPF_X: 378e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_OR | BPF_X: 379e54bcde3SZi Shen Lim emit(A64_ORR(is64, dst, dst, src), ctx); 380e54bcde3SZi Shen Lim break; 381e54bcde3SZi Shen Lim case BPF_ALU | BPF_XOR | BPF_X: 382e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_XOR | BPF_X: 383e54bcde3SZi Shen Lim emit(A64_EOR(is64, dst, dst, src), ctx); 384e54bcde3SZi Shen Lim break; 385e54bcde3SZi Shen Lim case BPF_ALU | BPF_MUL | BPF_X: 386e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_MUL | BPF_X: 387e54bcde3SZi Shen Lim emit(A64_MUL(is64, dst, dst, src), ctx); 388e54bcde3SZi Shen Lim break; 389e54bcde3SZi Shen Lim case BPF_ALU | BPF_DIV | BPF_X: 390e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_DIV | BPF_X: 391e54bcde3SZi Shen Lim case BPF_ALU | BPF_MOD | BPF_X: 392e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_MOD | BPF_X: 393251599e1SZi Shen Lim { 394251599e1SZi Shen Lim const u8 r0 = bpf2a64[BPF_REG_0]; 395251599e1SZi Shen Lim 396251599e1SZi Shen Lim /* if (src == 0) return 0 */ 397251599e1SZi Shen Lim jmp_offset = 3; /* skip ahead to else path */ 398251599e1SZi Shen Lim check_imm19(jmp_offset); 399251599e1SZi Shen Lim emit(A64_CBNZ(is64, src, jmp_offset), ctx); 400251599e1SZi Shen Lim emit(A64_MOVZ(1, r0, 0, 0), ctx); 401251599e1SZi Shen Lim jmp_offset = epilogue_offset(ctx); 402251599e1SZi Shen Lim check_imm26(jmp_offset); 403251599e1SZi Shen Lim emit(A64_B(jmp_offset), ctx); 404251599e1SZi Shen Lim /* else */ 40514e589ffSZi Shen Lim switch (BPF_OP(code)) { 40614e589ffSZi Shen Lim case BPF_DIV: 407e54bcde3SZi Shen Lim emit(A64_UDIV(is64, dst, dst, src), ctx); 408e54bcde3SZi Shen Lim break; 40914e589ffSZi Shen Lim case BPF_MOD: 410e54bcde3SZi Shen Lim emit(A64_UDIV(is64, tmp, dst, src), ctx); 411e54bcde3SZi Shen Lim emit(A64_MUL(is64, tmp, tmp, src), ctx); 412e54bcde3SZi Shen Lim emit(A64_SUB(is64, dst, dst, tmp), ctx); 413e54bcde3SZi Shen Lim break; 41414e589ffSZi Shen Lim } 41514e589ffSZi Shen Lim break; 41614e589ffSZi Shen Lim } 417d65a634aSZi Shen Lim case BPF_ALU | BPF_LSH | BPF_X: 418d65a634aSZi Shen Lim case BPF_ALU64 | BPF_LSH | BPF_X: 419d65a634aSZi Shen Lim emit(A64_LSLV(is64, dst, dst, src), ctx); 420d65a634aSZi Shen Lim break; 421d65a634aSZi Shen Lim case BPF_ALU | BPF_RSH | BPF_X: 422d65a634aSZi Shen Lim case BPF_ALU64 | BPF_RSH | BPF_X: 423d65a634aSZi Shen Lim emit(A64_LSRV(is64, dst, dst, src), ctx); 424d65a634aSZi Shen Lim break; 425d65a634aSZi Shen Lim case BPF_ALU | BPF_ARSH | BPF_X: 426d65a634aSZi Shen Lim case BPF_ALU64 | BPF_ARSH | BPF_X: 427d65a634aSZi Shen Lim emit(A64_ASRV(is64, dst, dst, src), ctx); 428d65a634aSZi Shen Lim break; 429e54bcde3SZi Shen Lim /* dst = -dst */ 430e54bcde3SZi Shen Lim case BPF_ALU | BPF_NEG: 431e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_NEG: 432e54bcde3SZi Shen Lim emit(A64_NEG(is64, dst, dst), ctx); 433e54bcde3SZi Shen Lim break; 434e54bcde3SZi Shen Lim /* dst = BSWAP##imm(dst) */ 435e54bcde3SZi Shen Lim case BPF_ALU | BPF_END | BPF_FROM_LE: 436e54bcde3SZi Shen Lim case BPF_ALU | BPF_END | BPF_FROM_BE: 437e54bcde3SZi Shen Lim #ifdef CONFIG_CPU_BIG_ENDIAN 438e54bcde3SZi Shen Lim if (BPF_SRC(code) == BPF_FROM_BE) 439d63903bbSXi Wang goto emit_bswap_uxt; 440e54bcde3SZi Shen Lim #else /* !CONFIG_CPU_BIG_ENDIAN */ 441e54bcde3SZi Shen Lim if (BPF_SRC(code) == BPF_FROM_LE) 442d63903bbSXi Wang goto emit_bswap_uxt; 443e54bcde3SZi Shen Lim #endif 444e54bcde3SZi Shen Lim switch (imm) { 445e54bcde3SZi Shen Lim case 16: 446e54bcde3SZi Shen Lim emit(A64_REV16(is64, dst, dst), ctx); 447d63903bbSXi Wang /* zero-extend 16 bits into 64 bits */ 448d63903bbSXi Wang emit(A64_UXTH(is64, dst, dst), ctx); 449e54bcde3SZi Shen Lim break; 450e54bcde3SZi Shen Lim case 32: 451e54bcde3SZi Shen Lim emit(A64_REV32(is64, dst, dst), ctx); 452d63903bbSXi Wang /* upper 32 bits already cleared */ 453e54bcde3SZi Shen Lim break; 454e54bcde3SZi Shen Lim case 64: 455e54bcde3SZi Shen Lim emit(A64_REV64(dst, dst), ctx); 456e54bcde3SZi Shen Lim break; 457e54bcde3SZi Shen Lim } 458e54bcde3SZi Shen Lim break; 459d63903bbSXi Wang emit_bswap_uxt: 460d63903bbSXi Wang switch (imm) { 461d63903bbSXi Wang case 16: 462d63903bbSXi Wang /* zero-extend 16 bits into 64 bits */ 463d63903bbSXi Wang emit(A64_UXTH(is64, dst, dst), ctx); 464d63903bbSXi Wang break; 465d63903bbSXi Wang case 32: 466d63903bbSXi Wang /* zero-extend 32 bits into 64 bits */ 467d63903bbSXi Wang emit(A64_UXTW(is64, dst, dst), ctx); 468d63903bbSXi Wang break; 469d63903bbSXi Wang case 64: 470d63903bbSXi Wang /* nop */ 471d63903bbSXi Wang break; 472d63903bbSXi Wang } 473d63903bbSXi Wang break; 474e54bcde3SZi Shen Lim /* dst = imm */ 475e54bcde3SZi Shen Lim case BPF_ALU | BPF_MOV | BPF_K: 476e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_MOV | BPF_K: 477e54bcde3SZi Shen Lim emit_a64_mov_i(is64, dst, imm, ctx); 478e54bcde3SZi Shen Lim break; 479e54bcde3SZi Shen Lim /* dst = dst OP imm */ 480e54bcde3SZi Shen Lim case BPF_ALU | BPF_ADD | BPF_K: 481e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_ADD | BPF_K: 482e54bcde3SZi Shen Lim emit_a64_mov_i(is64, tmp, imm, ctx); 483e54bcde3SZi Shen Lim emit(A64_ADD(is64, dst, dst, tmp), ctx); 484e54bcde3SZi Shen Lim break; 485e54bcde3SZi Shen Lim case BPF_ALU | BPF_SUB | BPF_K: 486e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_SUB | BPF_K: 487e54bcde3SZi Shen Lim emit_a64_mov_i(is64, tmp, imm, ctx); 488e54bcde3SZi Shen Lim emit(A64_SUB(is64, dst, dst, tmp), ctx); 489e54bcde3SZi Shen Lim break; 490e54bcde3SZi Shen Lim case BPF_ALU | BPF_AND | BPF_K: 491e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_AND | BPF_K: 492e54bcde3SZi Shen Lim emit_a64_mov_i(is64, tmp, imm, ctx); 493e54bcde3SZi Shen Lim emit(A64_AND(is64, dst, dst, tmp), ctx); 494e54bcde3SZi Shen Lim break; 495e54bcde3SZi Shen Lim case BPF_ALU | BPF_OR | BPF_K: 496e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_OR | BPF_K: 497e54bcde3SZi Shen Lim emit_a64_mov_i(is64, tmp, imm, ctx); 498e54bcde3SZi Shen Lim emit(A64_ORR(is64, dst, dst, tmp), ctx); 499e54bcde3SZi Shen Lim break; 500e54bcde3SZi Shen Lim case BPF_ALU | BPF_XOR | BPF_K: 501e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_XOR | BPF_K: 502e54bcde3SZi Shen Lim emit_a64_mov_i(is64, tmp, imm, ctx); 503e54bcde3SZi Shen Lim emit(A64_EOR(is64, dst, dst, tmp), ctx); 504e54bcde3SZi Shen Lim break; 505e54bcde3SZi Shen Lim case BPF_ALU | BPF_MUL | BPF_K: 506e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_MUL | BPF_K: 507e54bcde3SZi Shen Lim emit_a64_mov_i(is64, tmp, imm, ctx); 508e54bcde3SZi Shen Lim emit(A64_MUL(is64, dst, dst, tmp), ctx); 509e54bcde3SZi Shen Lim break; 510e54bcde3SZi Shen Lim case BPF_ALU | BPF_DIV | BPF_K: 511e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_DIV | BPF_K: 512e54bcde3SZi Shen Lim emit_a64_mov_i(is64, tmp, imm, ctx); 513e54bcde3SZi Shen Lim emit(A64_UDIV(is64, dst, dst, tmp), ctx); 514e54bcde3SZi Shen Lim break; 515e54bcde3SZi Shen Lim case BPF_ALU | BPF_MOD | BPF_K: 516e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_MOD | BPF_K: 517e54bcde3SZi Shen Lim emit_a64_mov_i(is64, tmp2, imm, ctx); 518e54bcde3SZi Shen Lim emit(A64_UDIV(is64, tmp, dst, tmp2), ctx); 519e54bcde3SZi Shen Lim emit(A64_MUL(is64, tmp, tmp, tmp2), ctx); 520e54bcde3SZi Shen Lim emit(A64_SUB(is64, dst, dst, tmp), ctx); 521e54bcde3SZi Shen Lim break; 522e54bcde3SZi Shen Lim case BPF_ALU | BPF_LSH | BPF_K: 523e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_LSH | BPF_K: 524e54bcde3SZi Shen Lim emit(A64_LSL(is64, dst, dst, imm), ctx); 525e54bcde3SZi Shen Lim break; 526e54bcde3SZi Shen Lim case BPF_ALU | BPF_RSH | BPF_K: 527e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_RSH | BPF_K: 528e54bcde3SZi Shen Lim emit(A64_LSR(is64, dst, dst, imm), ctx); 529e54bcde3SZi Shen Lim break; 530e54bcde3SZi Shen Lim case BPF_ALU | BPF_ARSH | BPF_K: 531e54bcde3SZi Shen Lim case BPF_ALU64 | BPF_ARSH | BPF_K: 532e54bcde3SZi Shen Lim emit(A64_ASR(is64, dst, dst, imm), ctx); 533e54bcde3SZi Shen Lim break; 534e54bcde3SZi Shen Lim 535e54bcde3SZi Shen Lim /* JUMP off */ 536e54bcde3SZi Shen Lim case BPF_JMP | BPF_JA: 537e54bcde3SZi Shen Lim jmp_offset = bpf2a64_offset(i + off, i, ctx); 538e54bcde3SZi Shen Lim check_imm26(jmp_offset); 539e54bcde3SZi Shen Lim emit(A64_B(jmp_offset), ctx); 540e54bcde3SZi Shen Lim break; 541e54bcde3SZi Shen Lim /* IF (dst COND src) JUMP off */ 542e54bcde3SZi Shen Lim case BPF_JMP | BPF_JEQ | BPF_X: 543e54bcde3SZi Shen Lim case BPF_JMP | BPF_JGT | BPF_X: 544c362b2f3SDaniel Borkmann case BPF_JMP | BPF_JLT | BPF_X: 545e54bcde3SZi Shen Lim case BPF_JMP | BPF_JGE | BPF_X: 546c362b2f3SDaniel Borkmann case BPF_JMP | BPF_JLE | BPF_X: 547e54bcde3SZi Shen Lim case BPF_JMP | BPF_JNE | BPF_X: 548e54bcde3SZi Shen Lim case BPF_JMP | BPF_JSGT | BPF_X: 549c362b2f3SDaniel Borkmann case BPF_JMP | BPF_JSLT | BPF_X: 550e54bcde3SZi Shen Lim case BPF_JMP | BPF_JSGE | BPF_X: 551c362b2f3SDaniel Borkmann case BPF_JMP | BPF_JSLE | BPF_X: 552e54bcde3SZi Shen Lim emit(A64_CMP(1, dst, src), ctx); 553e54bcde3SZi Shen Lim emit_cond_jmp: 554e54bcde3SZi Shen Lim jmp_offset = bpf2a64_offset(i + off, i, ctx); 555e54bcde3SZi Shen Lim check_imm19(jmp_offset); 556e54bcde3SZi Shen Lim switch (BPF_OP(code)) { 557e54bcde3SZi Shen Lim case BPF_JEQ: 558e54bcde3SZi Shen Lim jmp_cond = A64_COND_EQ; 559e54bcde3SZi Shen Lim break; 560e54bcde3SZi Shen Lim case BPF_JGT: 561e54bcde3SZi Shen Lim jmp_cond = A64_COND_HI; 562e54bcde3SZi Shen Lim break; 563c362b2f3SDaniel Borkmann case BPF_JLT: 564c362b2f3SDaniel Borkmann jmp_cond = A64_COND_CC; 565c362b2f3SDaniel Borkmann break; 566e54bcde3SZi Shen Lim case BPF_JGE: 567e54bcde3SZi Shen Lim jmp_cond = A64_COND_CS; 568e54bcde3SZi Shen Lim break; 569c362b2f3SDaniel Borkmann case BPF_JLE: 570c362b2f3SDaniel Borkmann jmp_cond = A64_COND_LS; 571c362b2f3SDaniel Borkmann break; 57298397fc5SZi Shen Lim case BPF_JSET: 573e54bcde3SZi Shen Lim case BPF_JNE: 574e54bcde3SZi Shen Lim jmp_cond = A64_COND_NE; 575e54bcde3SZi Shen Lim break; 576e54bcde3SZi Shen Lim case BPF_JSGT: 577e54bcde3SZi Shen Lim jmp_cond = A64_COND_GT; 578e54bcde3SZi Shen Lim break; 579c362b2f3SDaniel Borkmann case BPF_JSLT: 580c362b2f3SDaniel Borkmann jmp_cond = A64_COND_LT; 581c362b2f3SDaniel Borkmann break; 582e54bcde3SZi Shen Lim case BPF_JSGE: 583e54bcde3SZi Shen Lim jmp_cond = A64_COND_GE; 584e54bcde3SZi Shen Lim break; 585c362b2f3SDaniel Borkmann case BPF_JSLE: 586c362b2f3SDaniel Borkmann jmp_cond = A64_COND_LE; 587c362b2f3SDaniel Borkmann break; 588e54bcde3SZi Shen Lim default: 589e54bcde3SZi Shen Lim return -EFAULT; 590e54bcde3SZi Shen Lim } 591e54bcde3SZi Shen Lim emit(A64_B_(jmp_cond, jmp_offset), ctx); 592e54bcde3SZi Shen Lim break; 593e54bcde3SZi Shen Lim case BPF_JMP | BPF_JSET | BPF_X: 594e54bcde3SZi Shen Lim emit(A64_TST(1, dst, src), ctx); 595e54bcde3SZi Shen Lim goto emit_cond_jmp; 596e54bcde3SZi Shen Lim /* IF (dst COND imm) JUMP off */ 597e54bcde3SZi Shen Lim case BPF_JMP | BPF_JEQ | BPF_K: 598e54bcde3SZi Shen Lim case BPF_JMP | BPF_JGT | BPF_K: 599c362b2f3SDaniel Borkmann case BPF_JMP | BPF_JLT | BPF_K: 600e54bcde3SZi Shen Lim case BPF_JMP | BPF_JGE | BPF_K: 601c362b2f3SDaniel Borkmann case BPF_JMP | BPF_JLE | BPF_K: 602e54bcde3SZi Shen Lim case BPF_JMP | BPF_JNE | BPF_K: 603e54bcde3SZi Shen Lim case BPF_JMP | BPF_JSGT | BPF_K: 604c362b2f3SDaniel Borkmann case BPF_JMP | BPF_JSLT | BPF_K: 605e54bcde3SZi Shen Lim case BPF_JMP | BPF_JSGE | BPF_K: 606c362b2f3SDaniel Borkmann case BPF_JMP | BPF_JSLE | BPF_K: 607e54bcde3SZi Shen Lim emit_a64_mov_i(1, tmp, imm, ctx); 608e54bcde3SZi Shen Lim emit(A64_CMP(1, dst, tmp), ctx); 609e54bcde3SZi Shen Lim goto emit_cond_jmp; 610e54bcde3SZi Shen Lim case BPF_JMP | BPF_JSET | BPF_K: 611e54bcde3SZi Shen Lim emit_a64_mov_i(1, tmp, imm, ctx); 612e54bcde3SZi Shen Lim emit(A64_TST(1, dst, tmp), ctx); 613e54bcde3SZi Shen Lim goto emit_cond_jmp; 614e54bcde3SZi Shen Lim /* function call */ 615e54bcde3SZi Shen Lim case BPF_JMP | BPF_CALL: 616e54bcde3SZi Shen Lim { 617e54bcde3SZi Shen Lim const u8 r0 = bpf2a64[BPF_REG_0]; 618e54bcde3SZi Shen Lim const u64 func = (u64)__bpf_call_base + imm; 619e54bcde3SZi Shen Lim 620*db496944SAlexei Starovoitov if (ctx->prog->is_func) 621*db496944SAlexei Starovoitov emit_addr_mov_i64(tmp, func, ctx); 622*db496944SAlexei Starovoitov else 623e54bcde3SZi Shen Lim emit_a64_mov_i64(tmp, func, ctx); 624e54bcde3SZi Shen Lim emit(A64_BLR(tmp), ctx); 625e54bcde3SZi Shen Lim emit(A64_MOV(1, r0, A64_R(0)), ctx); 626e54bcde3SZi Shen Lim break; 627e54bcde3SZi Shen Lim } 628ddb55992SZi Shen Lim /* tail call */ 62971189fa9SAlexei Starovoitov case BPF_JMP | BPF_TAIL_CALL: 630ddb55992SZi Shen Lim if (emit_bpf_tail_call(ctx)) 631ddb55992SZi Shen Lim return -EFAULT; 632ddb55992SZi Shen Lim break; 633e54bcde3SZi Shen Lim /* function return */ 634e54bcde3SZi Shen Lim case BPF_JMP | BPF_EXIT: 63551c9fbb1SZi Shen Lim /* Optimization: when last instruction is EXIT, 63651c9fbb1SZi Shen Lim simply fallthrough to epilogue. */ 637e54bcde3SZi Shen Lim if (i == ctx->prog->len - 1) 638e54bcde3SZi Shen Lim break; 639e54bcde3SZi Shen Lim jmp_offset = epilogue_offset(ctx); 640e54bcde3SZi Shen Lim check_imm26(jmp_offset); 641e54bcde3SZi Shen Lim emit(A64_B(jmp_offset), ctx); 642e54bcde3SZi Shen Lim break; 643e54bcde3SZi Shen Lim 64430d3d94cSZi Shen Lim /* dst = imm64 */ 64530d3d94cSZi Shen Lim case BPF_LD | BPF_IMM | BPF_DW: 64630d3d94cSZi Shen Lim { 64730d3d94cSZi Shen Lim const struct bpf_insn insn1 = insn[1]; 64830d3d94cSZi Shen Lim u64 imm64; 64930d3d94cSZi Shen Lim 6501e4df6b7SXi Wang imm64 = (u64)insn1.imm << 32 | (u32)imm; 65130d3d94cSZi Shen Lim emit_a64_mov_i64(dst, imm64, ctx); 65230d3d94cSZi Shen Lim 65330d3d94cSZi Shen Lim return 1; 65430d3d94cSZi Shen Lim } 65530d3d94cSZi Shen Lim 656e54bcde3SZi Shen Lim /* LDX: dst = *(size *)(src + off) */ 657e54bcde3SZi Shen Lim case BPF_LDX | BPF_MEM | BPF_W: 658e54bcde3SZi Shen Lim case BPF_LDX | BPF_MEM | BPF_H: 659e54bcde3SZi Shen Lim case BPF_LDX | BPF_MEM | BPF_B: 660e54bcde3SZi Shen Lim case BPF_LDX | BPF_MEM | BPF_DW: 661e54bcde3SZi Shen Lim emit_a64_mov_i(1, tmp, off, ctx); 662e54bcde3SZi Shen Lim switch (BPF_SIZE(code)) { 663e54bcde3SZi Shen Lim case BPF_W: 664e54bcde3SZi Shen Lim emit(A64_LDR32(dst, src, tmp), ctx); 665e54bcde3SZi Shen Lim break; 666e54bcde3SZi Shen Lim case BPF_H: 667e54bcde3SZi Shen Lim emit(A64_LDRH(dst, src, tmp), ctx); 668e54bcde3SZi Shen Lim break; 669e54bcde3SZi Shen Lim case BPF_B: 670e54bcde3SZi Shen Lim emit(A64_LDRB(dst, src, tmp), ctx); 671e54bcde3SZi Shen Lim break; 672e54bcde3SZi Shen Lim case BPF_DW: 673e54bcde3SZi Shen Lim emit(A64_LDR64(dst, src, tmp), ctx); 674e54bcde3SZi Shen Lim break; 675e54bcde3SZi Shen Lim } 676e54bcde3SZi Shen Lim break; 677e54bcde3SZi Shen Lim 678e54bcde3SZi Shen Lim /* ST: *(size *)(dst + off) = imm */ 679e54bcde3SZi Shen Lim case BPF_ST | BPF_MEM | BPF_W: 680e54bcde3SZi Shen Lim case BPF_ST | BPF_MEM | BPF_H: 681e54bcde3SZi Shen Lim case BPF_ST | BPF_MEM | BPF_B: 682e54bcde3SZi Shen Lim case BPF_ST | BPF_MEM | BPF_DW: 683df849ba3SYang Shi /* Load imm to a register then store it */ 684df849ba3SYang Shi emit_a64_mov_i(1, tmp2, off, ctx); 685df849ba3SYang Shi emit_a64_mov_i(1, tmp, imm, ctx); 686df849ba3SYang Shi switch (BPF_SIZE(code)) { 687df849ba3SYang Shi case BPF_W: 688df849ba3SYang Shi emit(A64_STR32(tmp, dst, tmp2), ctx); 689df849ba3SYang Shi break; 690df849ba3SYang Shi case BPF_H: 691df849ba3SYang Shi emit(A64_STRH(tmp, dst, tmp2), ctx); 692df849ba3SYang Shi break; 693df849ba3SYang Shi case BPF_B: 694df849ba3SYang Shi emit(A64_STRB(tmp, dst, tmp2), ctx); 695df849ba3SYang Shi break; 696df849ba3SYang Shi case BPF_DW: 697df849ba3SYang Shi emit(A64_STR64(tmp, dst, tmp2), ctx); 698df849ba3SYang Shi break; 699df849ba3SYang Shi } 700df849ba3SYang Shi break; 701e54bcde3SZi Shen Lim 702e54bcde3SZi Shen Lim /* STX: *(size *)(dst + off) = src */ 703e54bcde3SZi Shen Lim case BPF_STX | BPF_MEM | BPF_W: 704e54bcde3SZi Shen Lim case BPF_STX | BPF_MEM | BPF_H: 705e54bcde3SZi Shen Lim case BPF_STX | BPF_MEM | BPF_B: 706e54bcde3SZi Shen Lim case BPF_STX | BPF_MEM | BPF_DW: 707e54bcde3SZi Shen Lim emit_a64_mov_i(1, tmp, off, ctx); 708e54bcde3SZi Shen Lim switch (BPF_SIZE(code)) { 709e54bcde3SZi Shen Lim case BPF_W: 710e54bcde3SZi Shen Lim emit(A64_STR32(src, dst, tmp), ctx); 711e54bcde3SZi Shen Lim break; 712e54bcde3SZi Shen Lim case BPF_H: 713e54bcde3SZi Shen Lim emit(A64_STRH(src, dst, tmp), ctx); 714e54bcde3SZi Shen Lim break; 715e54bcde3SZi Shen Lim case BPF_B: 716e54bcde3SZi Shen Lim emit(A64_STRB(src, dst, tmp), ctx); 717e54bcde3SZi Shen Lim break; 718e54bcde3SZi Shen Lim case BPF_DW: 719e54bcde3SZi Shen Lim emit(A64_STR64(src, dst, tmp), ctx); 720e54bcde3SZi Shen Lim break; 721e54bcde3SZi Shen Lim } 722e54bcde3SZi Shen Lim break; 723e54bcde3SZi Shen Lim /* STX XADD: lock *(u32 *)(dst + off) += src */ 724e54bcde3SZi Shen Lim case BPF_STX | BPF_XADD | BPF_W: 725e54bcde3SZi Shen Lim /* STX XADD: lock *(u64 *)(dst + off) += src */ 726e54bcde3SZi Shen Lim case BPF_STX | BPF_XADD | BPF_DW: 72785f68fe8SDaniel Borkmann emit_a64_mov_i(1, tmp, off, ctx); 72885f68fe8SDaniel Borkmann emit(A64_ADD(1, tmp, tmp, dst), ctx); 72985f68fe8SDaniel Borkmann emit(A64_PRFM(tmp, PST, L1, STRM), ctx); 73085f68fe8SDaniel Borkmann emit(A64_LDXR(isdw, tmp2, tmp), ctx); 73185f68fe8SDaniel Borkmann emit(A64_ADD(isdw, tmp2, tmp2, src), ctx); 7327005cadeSDaniel Borkmann emit(A64_STXR(isdw, tmp2, tmp, tmp3), ctx); 73385f68fe8SDaniel Borkmann jmp_offset = -3; 73485f68fe8SDaniel Borkmann check_imm19(jmp_offset); 7357005cadeSDaniel Borkmann emit(A64_CBNZ(0, tmp3, jmp_offset), ctx); 73685f68fe8SDaniel Borkmann break; 737e54bcde3SZi Shen Lim 738e54bcde3SZi Shen Lim /* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + imm)) */ 739e54bcde3SZi Shen Lim case BPF_LD | BPF_ABS | BPF_W: 740e54bcde3SZi Shen Lim case BPF_LD | BPF_ABS | BPF_H: 741e54bcde3SZi Shen Lim case BPF_LD | BPF_ABS | BPF_B: 742e54bcde3SZi Shen Lim /* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + src + imm)) */ 743e54bcde3SZi Shen Lim case BPF_LD | BPF_IND | BPF_W: 744e54bcde3SZi Shen Lim case BPF_LD | BPF_IND | BPF_H: 745e54bcde3SZi Shen Lim case BPF_LD | BPF_IND | BPF_B: 746e54bcde3SZi Shen Lim { 747e54bcde3SZi Shen Lim const u8 r0 = bpf2a64[BPF_REG_0]; /* r0 = return value */ 748e54bcde3SZi Shen Lim const u8 r6 = bpf2a64[BPF_REG_6]; /* r6 = pointer to sk_buff */ 749e54bcde3SZi Shen Lim const u8 fp = bpf2a64[BPF_REG_FP]; 750e54bcde3SZi Shen Lim const u8 r1 = bpf2a64[BPF_REG_1]; /* r1: struct sk_buff *skb */ 751e54bcde3SZi Shen Lim const u8 r2 = bpf2a64[BPF_REG_2]; /* r2: int k */ 752e54bcde3SZi Shen Lim const u8 r3 = bpf2a64[BPF_REG_3]; /* r3: unsigned int size */ 753e54bcde3SZi Shen Lim const u8 r4 = bpf2a64[BPF_REG_4]; /* r4: void *buffer */ 754e54bcde3SZi Shen Lim const u8 r5 = bpf2a64[BPF_REG_5]; /* r5: void *(*func)(...) */ 755e54bcde3SZi Shen Lim int size; 756e54bcde3SZi Shen Lim 757e54bcde3SZi Shen Lim emit(A64_MOV(1, r1, r6), ctx); 758e54bcde3SZi Shen Lim emit_a64_mov_i(0, r2, imm, ctx); 759e54bcde3SZi Shen Lim if (BPF_MODE(code) == BPF_IND) 760e54bcde3SZi Shen Lim emit(A64_ADD(0, r2, r2, src), ctx); 761e54bcde3SZi Shen Lim switch (BPF_SIZE(code)) { 762e54bcde3SZi Shen Lim case BPF_W: 763e54bcde3SZi Shen Lim size = 4; 764e54bcde3SZi Shen Lim break; 765e54bcde3SZi Shen Lim case BPF_H: 766e54bcde3SZi Shen Lim size = 2; 767e54bcde3SZi Shen Lim break; 768e54bcde3SZi Shen Lim case BPF_B: 769e54bcde3SZi Shen Lim size = 1; 770e54bcde3SZi Shen Lim break; 771e54bcde3SZi Shen Lim default: 772e54bcde3SZi Shen Lim return -EINVAL; 773e54bcde3SZi Shen Lim } 774e54bcde3SZi Shen Lim emit_a64_mov_i64(r3, size, ctx); 775f1c9eed7SDaniel Borkmann emit(A64_SUB_I(1, r4, fp, ctx->stack_size), ctx); 776e54bcde3SZi Shen Lim emit_a64_mov_i64(r5, (unsigned long)bpf_load_pointer, ctx); 777e54bcde3SZi Shen Lim emit(A64_BLR(r5), ctx); 778e54bcde3SZi Shen Lim emit(A64_MOV(1, r0, A64_R(0)), ctx); 779e54bcde3SZi Shen Lim 780e54bcde3SZi Shen Lim jmp_offset = epilogue_offset(ctx); 781e54bcde3SZi Shen Lim check_imm19(jmp_offset); 782e54bcde3SZi Shen Lim emit(A64_CBZ(1, r0, jmp_offset), ctx); 783e54bcde3SZi Shen Lim emit(A64_MOV(1, r5, r0), ctx); 784e54bcde3SZi Shen Lim switch (BPF_SIZE(code)) { 785e54bcde3SZi Shen Lim case BPF_W: 786e54bcde3SZi Shen Lim emit(A64_LDR32(r0, r5, A64_ZR), ctx); 787e54bcde3SZi Shen Lim #ifndef CONFIG_CPU_BIG_ENDIAN 788e54bcde3SZi Shen Lim emit(A64_REV32(0, r0, r0), ctx); 789e54bcde3SZi Shen Lim #endif 790e54bcde3SZi Shen Lim break; 791e54bcde3SZi Shen Lim case BPF_H: 792e54bcde3SZi Shen Lim emit(A64_LDRH(r0, r5, A64_ZR), ctx); 793e54bcde3SZi Shen Lim #ifndef CONFIG_CPU_BIG_ENDIAN 794e54bcde3SZi Shen Lim emit(A64_REV16(0, r0, r0), ctx); 795e54bcde3SZi Shen Lim #endif 796e54bcde3SZi Shen Lim break; 797e54bcde3SZi Shen Lim case BPF_B: 798e54bcde3SZi Shen Lim emit(A64_LDRB(r0, r5, A64_ZR), ctx); 799e54bcde3SZi Shen Lim break; 800e54bcde3SZi Shen Lim } 801e54bcde3SZi Shen Lim break; 802e54bcde3SZi Shen Lim } 803e54bcde3SZi Shen Lim default: 804e54bcde3SZi Shen Lim pr_err_once("unknown opcode %02x\n", code); 805e54bcde3SZi Shen Lim return -EINVAL; 806e54bcde3SZi Shen Lim } 807e54bcde3SZi Shen Lim 808e54bcde3SZi Shen Lim return 0; 809e54bcde3SZi Shen Lim } 810e54bcde3SZi Shen Lim 811e54bcde3SZi Shen Lim static int build_body(struct jit_ctx *ctx) 812e54bcde3SZi Shen Lim { 813e54bcde3SZi Shen Lim const struct bpf_prog *prog = ctx->prog; 814e54bcde3SZi Shen Lim int i; 815e54bcde3SZi Shen Lim 816e54bcde3SZi Shen Lim for (i = 0; i < prog->len; i++) { 817e54bcde3SZi Shen Lim const struct bpf_insn *insn = &prog->insnsi[i]; 818e54bcde3SZi Shen Lim int ret; 819e54bcde3SZi Shen Lim 8208eee539dSXi Wang ret = build_insn(insn, ctx); 82130d3d94cSZi Shen Lim if (ret > 0) { 82230d3d94cSZi Shen Lim i++; 823ddc665a4SDaniel Borkmann if (ctx->image == NULL) 824ddc665a4SDaniel Borkmann ctx->offset[i] = ctx->idx; 82530d3d94cSZi Shen Lim continue; 82630d3d94cSZi Shen Lim } 827ddc665a4SDaniel Borkmann if (ctx->image == NULL) 828ddc665a4SDaniel Borkmann ctx->offset[i] = ctx->idx; 829e54bcde3SZi Shen Lim if (ret) 830e54bcde3SZi Shen Lim return ret; 831e54bcde3SZi Shen Lim } 832e54bcde3SZi Shen Lim 833e54bcde3SZi Shen Lim return 0; 834e54bcde3SZi Shen Lim } 835e54bcde3SZi Shen Lim 83642ff712bSZi Shen Lim static int validate_code(struct jit_ctx *ctx) 83742ff712bSZi Shen Lim { 83842ff712bSZi Shen Lim int i; 83942ff712bSZi Shen Lim 84042ff712bSZi Shen Lim for (i = 0; i < ctx->idx; i++) { 84142ff712bSZi Shen Lim u32 a64_insn = le32_to_cpu(ctx->image[i]); 84242ff712bSZi Shen Lim 84342ff712bSZi Shen Lim if (a64_insn == AARCH64_BREAK_FAULT) 84442ff712bSZi Shen Lim return -1; 84542ff712bSZi Shen Lim } 84642ff712bSZi Shen Lim 84742ff712bSZi Shen Lim return 0; 84842ff712bSZi Shen Lim } 84942ff712bSZi Shen Lim 850e54bcde3SZi Shen Lim static inline void bpf_flush_icache(void *start, void *end) 851e54bcde3SZi Shen Lim { 852e54bcde3SZi Shen Lim flush_icache_range((unsigned long)start, (unsigned long)end); 853e54bcde3SZi Shen Lim } 854e54bcde3SZi Shen Lim 855*db496944SAlexei Starovoitov struct arm64_jit_data { 856*db496944SAlexei Starovoitov struct bpf_binary_header *header; 857*db496944SAlexei Starovoitov u8 *image; 858*db496944SAlexei Starovoitov struct jit_ctx ctx; 859*db496944SAlexei Starovoitov }; 860*db496944SAlexei Starovoitov 861d1c55ab5SDaniel Borkmann struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) 862e54bcde3SZi Shen Lim { 86326eb042eSDaniel Borkmann struct bpf_prog *tmp, *orig_prog = prog; 864b569c1c6SDaniel Borkmann struct bpf_binary_header *header; 865*db496944SAlexei Starovoitov struct arm64_jit_data *jit_data; 86626eb042eSDaniel Borkmann bool tmp_blinded = false; 867*db496944SAlexei Starovoitov bool extra_pass = false; 868e54bcde3SZi Shen Lim struct jit_ctx ctx; 869e54bcde3SZi Shen Lim int image_size; 870b569c1c6SDaniel Borkmann u8 *image_ptr; 871e54bcde3SZi Shen Lim 87260b58afcSAlexei Starovoitov if (!prog->jit_requested) 87326eb042eSDaniel Borkmann return orig_prog; 87426eb042eSDaniel Borkmann 87526eb042eSDaniel Borkmann tmp = bpf_jit_blind_constants(prog); 87626eb042eSDaniel Borkmann /* If blinding was requested and we failed during blinding, 87726eb042eSDaniel Borkmann * we must fall back to the interpreter. 87826eb042eSDaniel Borkmann */ 87926eb042eSDaniel Borkmann if (IS_ERR(tmp)) 88026eb042eSDaniel Borkmann return orig_prog; 88126eb042eSDaniel Borkmann if (tmp != prog) { 88226eb042eSDaniel Borkmann tmp_blinded = true; 88326eb042eSDaniel Borkmann prog = tmp; 88426eb042eSDaniel Borkmann } 885e54bcde3SZi Shen Lim 886*db496944SAlexei Starovoitov jit_data = prog->aux->jit_data; 887*db496944SAlexei Starovoitov if (!jit_data) { 888*db496944SAlexei Starovoitov jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL); 889*db496944SAlexei Starovoitov if (!jit_data) { 890*db496944SAlexei Starovoitov prog = orig_prog; 891*db496944SAlexei Starovoitov goto out; 892*db496944SAlexei Starovoitov } 893*db496944SAlexei Starovoitov prog->aux->jit_data = jit_data; 894*db496944SAlexei Starovoitov } 895*db496944SAlexei Starovoitov if (jit_data->ctx.offset) { 896*db496944SAlexei Starovoitov ctx = jit_data->ctx; 897*db496944SAlexei Starovoitov image_ptr = jit_data->image; 898*db496944SAlexei Starovoitov header = jit_data->header; 899*db496944SAlexei Starovoitov extra_pass = true; 900*db496944SAlexei Starovoitov goto skip_init_ctx; 901*db496944SAlexei Starovoitov } 902e54bcde3SZi Shen Lim memset(&ctx, 0, sizeof(ctx)); 903e54bcde3SZi Shen Lim ctx.prog = prog; 904e54bcde3SZi Shen Lim 905e54bcde3SZi Shen Lim ctx.offset = kcalloc(prog->len, sizeof(int), GFP_KERNEL); 90626eb042eSDaniel Borkmann if (ctx.offset == NULL) { 90726eb042eSDaniel Borkmann prog = orig_prog; 908*db496944SAlexei Starovoitov goto out_off; 90926eb042eSDaniel Borkmann } 910e54bcde3SZi Shen Lim 911e54bcde3SZi Shen Lim /* 1. Initial fake pass to compute ctx->idx. */ 912e54bcde3SZi Shen Lim 9134c1cd4fdSYang Shi /* Fake pass to fill in ctx->offset. */ 91426eb042eSDaniel Borkmann if (build_body(&ctx)) { 91526eb042eSDaniel Borkmann prog = orig_prog; 91626eb042eSDaniel Borkmann goto out_off; 91726eb042eSDaniel Borkmann } 918e54bcde3SZi Shen Lim 919ddb55992SZi Shen Lim if (build_prologue(&ctx)) { 920ddb55992SZi Shen Lim prog = orig_prog; 921ddb55992SZi Shen Lim goto out_off; 922ddb55992SZi Shen Lim } 92351c9fbb1SZi Shen Lim 92451c9fbb1SZi Shen Lim ctx.epilogue_offset = ctx.idx; 925e54bcde3SZi Shen Lim build_epilogue(&ctx); 926e54bcde3SZi Shen Lim 927e54bcde3SZi Shen Lim /* Now we know the actual image size. */ 928e54bcde3SZi Shen Lim image_size = sizeof(u32) * ctx.idx; 929b569c1c6SDaniel Borkmann header = bpf_jit_binary_alloc(image_size, &image_ptr, 930b569c1c6SDaniel Borkmann sizeof(u32), jit_fill_hole); 93126eb042eSDaniel Borkmann if (header == NULL) { 93226eb042eSDaniel Borkmann prog = orig_prog; 93326eb042eSDaniel Borkmann goto out_off; 93426eb042eSDaniel Borkmann } 935e54bcde3SZi Shen Lim 936e54bcde3SZi Shen Lim /* 2. Now, the actual pass. */ 937e54bcde3SZi Shen Lim 938425e1ed7SLuc Van Oostenryck ctx.image = (__le32 *)image_ptr; 939*db496944SAlexei Starovoitov skip_init_ctx: 940e54bcde3SZi Shen Lim ctx.idx = 0; 941b569c1c6SDaniel Borkmann 942e54bcde3SZi Shen Lim build_prologue(&ctx); 943e54bcde3SZi Shen Lim 94460ef0494SDaniel Borkmann if (build_body(&ctx)) { 945b569c1c6SDaniel Borkmann bpf_jit_binary_free(header); 94626eb042eSDaniel Borkmann prog = orig_prog; 94726eb042eSDaniel Borkmann goto out_off; 94860ef0494SDaniel Borkmann } 949e54bcde3SZi Shen Lim 950e54bcde3SZi Shen Lim build_epilogue(&ctx); 951e54bcde3SZi Shen Lim 95242ff712bSZi Shen Lim /* 3. Extra pass to validate JITed code. */ 95342ff712bSZi Shen Lim if (validate_code(&ctx)) { 95442ff712bSZi Shen Lim bpf_jit_binary_free(header); 95526eb042eSDaniel Borkmann prog = orig_prog; 95626eb042eSDaniel Borkmann goto out_off; 95742ff712bSZi Shen Lim } 95842ff712bSZi Shen Lim 959e54bcde3SZi Shen Lim /* And we're done. */ 960e54bcde3SZi Shen Lim if (bpf_jit_enable > 1) 961e54bcde3SZi Shen Lim bpf_jit_dump(prog->len, image_size, 2, ctx.image); 962e54bcde3SZi Shen Lim 963c3d4c682SDaniel Borkmann bpf_flush_icache(header, ctx.image + ctx.idx); 964b569c1c6SDaniel Borkmann 965*db496944SAlexei Starovoitov if (!prog->is_func || extra_pass) { 966*db496944SAlexei Starovoitov if (extra_pass && ctx.idx != jit_data->ctx.idx) { 967*db496944SAlexei Starovoitov pr_err_once("multi-func JIT bug %d != %d\n", 968*db496944SAlexei Starovoitov ctx.idx, jit_data->ctx.idx); 969*db496944SAlexei Starovoitov bpf_jit_binary_free(header); 970*db496944SAlexei Starovoitov prog->bpf_func = NULL; 971*db496944SAlexei Starovoitov prog->jited = 0; 972*db496944SAlexei Starovoitov goto out_off; 973*db496944SAlexei Starovoitov } 9749d876e79SDaniel Borkmann bpf_jit_binary_lock_ro(header); 975*db496944SAlexei Starovoitov } else { 976*db496944SAlexei Starovoitov jit_data->ctx = ctx; 977*db496944SAlexei Starovoitov jit_data->image = image_ptr; 978*db496944SAlexei Starovoitov jit_data->header = header; 979*db496944SAlexei Starovoitov } 980e54bcde3SZi Shen Lim prog->bpf_func = (void *)ctx.image; 981a91263d5SDaniel Borkmann prog->jited = 1; 982783d28ddSMartin KaFai Lau prog->jited_len = image_size; 98326eb042eSDaniel Borkmann 984*db496944SAlexei Starovoitov if (!prog->is_func || extra_pass) { 98526eb042eSDaniel Borkmann out_off: 986e54bcde3SZi Shen Lim kfree(ctx.offset); 987*db496944SAlexei Starovoitov kfree(jit_data); 988*db496944SAlexei Starovoitov prog->aux->jit_data = NULL; 989*db496944SAlexei Starovoitov } 99026eb042eSDaniel Borkmann out: 99126eb042eSDaniel Borkmann if (tmp_blinded) 99226eb042eSDaniel Borkmann bpf_jit_prog_release_other(prog, prog == orig_prog ? 99326eb042eSDaniel Borkmann tmp : orig_prog); 994d1c55ab5SDaniel Borkmann return prog; 995e54bcde3SZi Shen Lim } 996