1ddecdfceSMircea Gherzan /* 239c13c20SShubham Bansal * Just-In-Time compiler for eBPF filters on 32bit ARM 3ddecdfceSMircea Gherzan * 439c13c20SShubham Bansal * Copyright (c) 2017 Shubham Bansal <illusionist.neo@gmail.com> 5ddecdfceSMircea Gherzan * Copyright (c) 2011 Mircea Gherzan <mgherzan@gmail.com> 6ddecdfceSMircea Gherzan * 7ddecdfceSMircea Gherzan * This program is free software; you can redistribute it and/or modify it 8ddecdfceSMircea Gherzan * under the terms of the GNU General Public License as published by the 9ddecdfceSMircea Gherzan * Free Software Foundation; version 2 of the License. 10ddecdfceSMircea Gherzan */ 11ddecdfceSMircea Gherzan 1239c13c20SShubham Bansal #include <linux/bpf.h> 13ddecdfceSMircea Gherzan #include <linux/bitops.h> 14ddecdfceSMircea Gherzan #include <linux/compiler.h> 15ddecdfceSMircea Gherzan #include <linux/errno.h> 16ddecdfceSMircea Gherzan #include <linux/filter.h> 17ddecdfceSMircea Gherzan #include <linux/netdevice.h> 18ddecdfceSMircea Gherzan #include <linux/string.h> 19ddecdfceSMircea Gherzan #include <linux/slab.h> 20bf0098f2SDaniel Borkmann #include <linux/if_vlan.h> 21e8b56d55SDaniel Borkmann 22ddecdfceSMircea Gherzan #include <asm/cacheflush.h> 23ddecdfceSMircea Gherzan #include <asm/hwcap.h> 243460743eSBen Dooks #include <asm/opcodes.h> 25ddecdfceSMircea Gherzan 26ddecdfceSMircea Gherzan #include "bpf_jit_32.h" 27ddecdfceSMircea Gherzan 2870ec3a6cSRussell King /* 290005e55aSRussell King * eBPF prog stack layout: 3070ec3a6cSRussell King * 3170ec3a6cSRussell King * high 320005e55aSRussell King * original ARM_SP => +-----+ 330005e55aSRussell King * | | callee saved registers 340005e55aSRussell King * +-----+ <= (BPF_FP + SCRATCH_SIZE) 3570ec3a6cSRussell King * | ... | eBPF JIT scratch space 360005e55aSRussell King * eBPF fp register => +-----+ 370005e55aSRussell King * (BPF_FP) | ... | eBPF prog stack 3870ec3a6cSRussell King * +-----+ 3970ec3a6cSRussell King * |RSVD | JIT scratchpad 400005e55aSRussell King * current ARM_SP => +-----+ <= (BPF_FP - STACK_SIZE + SCRATCH_SIZE) 4170ec3a6cSRussell King * | | 4270ec3a6cSRussell King * | ... | Function call stack 4370ec3a6cSRussell King * | | 4470ec3a6cSRussell King * +-----+ 4570ec3a6cSRussell King * low 460005e55aSRussell King * 470005e55aSRussell King * The callee saved registers depends on whether frame pointers are enabled. 480005e55aSRussell King * With frame pointers (to be compliant with the ABI): 490005e55aSRussell King * 500005e55aSRussell King * high 510005e55aSRussell King * original ARM_SP => +------------------+ \ 520005e55aSRussell King * | pc | | 530005e55aSRussell King * current ARM_FP => +------------------+ } callee saved registers 540005e55aSRussell King * |r4-r8,r10,fp,ip,lr| | 550005e55aSRussell King * +------------------+ / 560005e55aSRussell King * low 570005e55aSRussell King * 580005e55aSRussell King * Without frame pointers: 590005e55aSRussell King * 600005e55aSRussell King * high 610005e55aSRussell King * original ARM_SP => +------------------+ 6202088d9bSRussell King * | r4-r8,r10,fp,lr | callee saved registers 6302088d9bSRussell King * current ARM_FP => +------------------+ 640005e55aSRussell King * low 6502088d9bSRussell King * 6602088d9bSRussell King * When popping registers off the stack at the end of a BPF function, we 6702088d9bSRussell King * reference them via the current ARM_FP register. 6870ec3a6cSRussell King */ 6902088d9bSRussell King #define CALLEE_MASK (1 << ARM_R4 | 1 << ARM_R5 | 1 << ARM_R6 | \ 7002088d9bSRussell King 1 << ARM_R7 | 1 << ARM_R8 | 1 << ARM_R10 | \ 7102088d9bSRussell King 1 << ARM_FP) 7202088d9bSRussell King #define CALLEE_PUSH_MASK (CALLEE_MASK | 1 << ARM_LR) 7302088d9bSRussell King #define CALLEE_POP_MASK (CALLEE_MASK | 1 << ARM_PC) 7470ec3a6cSRussell King 75d449ceb1SRussell King enum { 76d449ceb1SRussell King /* Stack layout - these are offsets from (top of stack - 4) */ 77d449ceb1SRussell King BPF_R2_HI, 78d449ceb1SRussell King BPF_R2_LO, 79d449ceb1SRussell King BPF_R3_HI, 80d449ceb1SRussell King BPF_R3_LO, 81d449ceb1SRussell King BPF_R4_HI, 82d449ceb1SRussell King BPF_R4_LO, 83d449ceb1SRussell King BPF_R5_HI, 84d449ceb1SRussell King BPF_R5_LO, 85d449ceb1SRussell King BPF_R7_HI, 86d449ceb1SRussell King BPF_R7_LO, 87d449ceb1SRussell King BPF_R8_HI, 88d449ceb1SRussell King BPF_R8_LO, 89d449ceb1SRussell King BPF_R9_HI, 90d449ceb1SRussell King BPF_R9_LO, 91d449ceb1SRussell King BPF_FP_HI, 92d449ceb1SRussell King BPF_FP_LO, 93d449ceb1SRussell King BPF_TC_HI, 94d449ceb1SRussell King BPF_TC_LO, 95d449ceb1SRussell King BPF_AX_HI, 96d449ceb1SRussell King BPF_AX_LO, 97d449ceb1SRussell King /* Stack space for BPF_REG_2, BPF_REG_3, BPF_REG_4, 98d449ceb1SRussell King * BPF_REG_5, BPF_REG_7, BPF_REG_8, BPF_REG_9, 99d449ceb1SRussell King * BPF_REG_FP and Tail call counts. 100d449ceb1SRussell King */ 101d449ceb1SRussell King BPF_JIT_SCRATCH_REGS, 102d449ceb1SRussell King }; 103d449ceb1SRussell King 1041c35ba12SRussell King /* 1051c35ba12SRussell King * Negative "register" values indicate the register is stored on the stack 1061c35ba12SRussell King * and are the offset from the top of the eBPF JIT scratch space. 1071c35ba12SRussell King */ 1081c35ba12SRussell King #define STACK_OFFSET(k) (-4 - (k) * 4) 109d449ceb1SRussell King #define SCRATCH_SIZE (BPF_JIT_SCRATCH_REGS * 4) 110d449ceb1SRussell King 11139c13c20SShubham Bansal #define TMP_REG_1 (MAX_BPF_JIT_REG + 0) /* TEMP Register 1 */ 11239c13c20SShubham Bansal #define TMP_REG_2 (MAX_BPF_JIT_REG + 1) /* TEMP Register 2 */ 11339c13c20SShubham Bansal #define TCALL_CNT (MAX_BPF_JIT_REG + 2) /* Tail Call Count */ 11439c13c20SShubham Bansal 11539c13c20SShubham Bansal #define FLAG_IMM_OVERFLOW (1 << 0) 11639c13c20SShubham Bansal 117ddecdfceSMircea Gherzan /* 11839c13c20SShubham Bansal * Map eBPF registers to ARM 32bit registers or stack scratch space. 119ddecdfceSMircea Gherzan * 12039c13c20SShubham Bansal * 1. First argument is passed using the arm 32bit registers and rest of the 12139c13c20SShubham Bansal * arguments are passed on stack scratch space. 1222b589a7eSWang YanQing * 2. First callee-saved argument is mapped to arm 32 bit registers and rest 12339c13c20SShubham Bansal * arguments are mapped to scratch space on stack. 12439c13c20SShubham Bansal * 3. We need two 64 bit temp registers to do complex operations on eBPF 12539c13c20SShubham Bansal * registers. 12639c13c20SShubham Bansal * 12739c13c20SShubham Bansal * As the eBPF registers are all 64 bit registers and arm has only 32 bit 12839c13c20SShubham Bansal * registers, we have to map each eBPF registers with two arm 32 bit regs or 12939c13c20SShubham Bansal * scratch memory space and we have to build eBPF 64 bit register from those. 13039c13c20SShubham Bansal * 13139c13c20SShubham Bansal */ 1321c35ba12SRussell King static const s8 bpf2a32[][2] = { 13339c13c20SShubham Bansal /* return value from in-kernel function, and exit value from eBPF */ 13439c13c20SShubham Bansal [BPF_REG_0] = {ARM_R1, ARM_R0}, 13539c13c20SShubham Bansal /* arguments from eBPF program to in-kernel function */ 13639c13c20SShubham Bansal [BPF_REG_1] = {ARM_R3, ARM_R2}, 13739c13c20SShubham Bansal /* Stored on stack scratch space */ 138d449ceb1SRussell King [BPF_REG_2] = {STACK_OFFSET(BPF_R2_HI), STACK_OFFSET(BPF_R2_LO)}, 139d449ceb1SRussell King [BPF_REG_3] = {STACK_OFFSET(BPF_R3_HI), STACK_OFFSET(BPF_R3_LO)}, 140d449ceb1SRussell King [BPF_REG_4] = {STACK_OFFSET(BPF_R4_HI), STACK_OFFSET(BPF_R4_LO)}, 141d449ceb1SRussell King [BPF_REG_5] = {STACK_OFFSET(BPF_R5_HI), STACK_OFFSET(BPF_R5_LO)}, 14239c13c20SShubham Bansal /* callee saved registers that in-kernel function will preserve */ 14339c13c20SShubham Bansal [BPF_REG_6] = {ARM_R5, ARM_R4}, 14439c13c20SShubham Bansal /* Stored on stack scratch space */ 145d449ceb1SRussell King [BPF_REG_7] = {STACK_OFFSET(BPF_R7_HI), STACK_OFFSET(BPF_R7_LO)}, 146d449ceb1SRussell King [BPF_REG_8] = {STACK_OFFSET(BPF_R8_HI), STACK_OFFSET(BPF_R8_LO)}, 147d449ceb1SRussell King [BPF_REG_9] = {STACK_OFFSET(BPF_R9_HI), STACK_OFFSET(BPF_R9_LO)}, 14839c13c20SShubham Bansal /* Read only Frame Pointer to access Stack */ 149d449ceb1SRussell King [BPF_REG_FP] = {STACK_OFFSET(BPF_FP_HI), STACK_OFFSET(BPF_FP_LO)}, 15039c13c20SShubham Bansal /* Temporary Register for internal BPF JIT, can be used 15139c13c20SShubham Bansal * for constant blindings and others. 15239c13c20SShubham Bansal */ 15339c13c20SShubham Bansal [TMP_REG_1] = {ARM_R7, ARM_R6}, 15439c13c20SShubham Bansal [TMP_REG_2] = {ARM_R10, ARM_R8}, 15539c13c20SShubham Bansal /* Tail call count. Stored on stack scratch space. */ 156d449ceb1SRussell King [TCALL_CNT] = {STACK_OFFSET(BPF_TC_HI), STACK_OFFSET(BPF_TC_LO)}, 15739c13c20SShubham Bansal /* temporary register for blinding constants. 15839c13c20SShubham Bansal * Stored on stack scratch space. 15939c13c20SShubham Bansal */ 160d449ceb1SRussell King [BPF_REG_AX] = {STACK_OFFSET(BPF_AX_HI), STACK_OFFSET(BPF_AX_LO)}, 16139c13c20SShubham Bansal }; 16239c13c20SShubham Bansal 16339c13c20SShubham Bansal #define dst_lo dst[1] 16439c13c20SShubham Bansal #define dst_hi dst[0] 16539c13c20SShubham Bansal #define src_lo src[1] 16639c13c20SShubham Bansal #define src_hi src[0] 16739c13c20SShubham Bansal 16839c13c20SShubham Bansal /* 16939c13c20SShubham Bansal * JIT Context: 17039c13c20SShubham Bansal * 17139c13c20SShubham Bansal * prog : bpf_prog 17239c13c20SShubham Bansal * idx : index of current last JITed instruction. 17339c13c20SShubham Bansal * prologue_bytes : bytes used in prologue. 17439c13c20SShubham Bansal * epilogue_offset : offset of epilogue starting. 17539c13c20SShubham Bansal * offsets : array of eBPF instruction offsets in 17639c13c20SShubham Bansal * JITed code. 17739c13c20SShubham Bansal * target : final JITed code. 17839c13c20SShubham Bansal * epilogue_bytes : no of bytes used in epilogue. 17939c13c20SShubham Bansal * imm_count : no of immediate counts used for global 18039c13c20SShubham Bansal * variables. 18139c13c20SShubham Bansal * imms : array of global variable addresses. 182ddecdfceSMircea Gherzan */ 183ddecdfceSMircea Gherzan 184ddecdfceSMircea Gherzan struct jit_ctx { 18539c13c20SShubham Bansal const struct bpf_prog *prog; 18639c13c20SShubham Bansal unsigned int idx; 18739c13c20SShubham Bansal unsigned int prologue_bytes; 18839c13c20SShubham Bansal unsigned int epilogue_offset; 189ddecdfceSMircea Gherzan u32 flags; 190ddecdfceSMircea Gherzan u32 *offsets; 191ddecdfceSMircea Gherzan u32 *target; 19239c13c20SShubham Bansal u32 stack_size; 193ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7 194ddecdfceSMircea Gherzan u16 epilogue_bytes; 195ddecdfceSMircea Gherzan u16 imm_count; 196ddecdfceSMircea Gherzan u32 *imms; 197ddecdfceSMircea Gherzan #endif 198ddecdfceSMircea Gherzan }; 199ddecdfceSMircea Gherzan 200ddecdfceSMircea Gherzan /* 2014560cdffSNicolas Schichan * Wrappers which handle both OABI and EABI and assures Thumb2 interworking 202ddecdfceSMircea Gherzan * (where the assembly routines like __aeabi_uidiv could cause problems). 203ddecdfceSMircea Gherzan */ 20439c13c20SShubham Bansal static u32 jit_udiv32(u32 dividend, u32 divisor) 205ddecdfceSMircea Gherzan { 206ddecdfceSMircea Gherzan return dividend / divisor; 207ddecdfceSMircea Gherzan } 208ddecdfceSMircea Gherzan 20939c13c20SShubham Bansal static u32 jit_mod32(u32 dividend, u32 divisor) 2104560cdffSNicolas Schichan { 2114560cdffSNicolas Schichan return dividend % divisor; 2124560cdffSNicolas Schichan } 2134560cdffSNicolas Schichan 214ddecdfceSMircea Gherzan static inline void _emit(int cond, u32 inst, struct jit_ctx *ctx) 215ddecdfceSMircea Gherzan { 2163460743eSBen Dooks inst |= (cond << 28); 2173460743eSBen Dooks inst = __opcode_to_mem_arm(inst); 2183460743eSBen Dooks 219ddecdfceSMircea Gherzan if (ctx->target != NULL) 2203460743eSBen Dooks ctx->target[ctx->idx] = inst; 221ddecdfceSMircea Gherzan 222ddecdfceSMircea Gherzan ctx->idx++; 223ddecdfceSMircea Gherzan } 224ddecdfceSMircea Gherzan 225ddecdfceSMircea Gherzan /* 226ddecdfceSMircea Gherzan * Emit an instruction that will be executed unconditionally. 227ddecdfceSMircea Gherzan */ 228ddecdfceSMircea Gherzan static inline void emit(u32 inst, struct jit_ctx *ctx) 229ddecdfceSMircea Gherzan { 230ddecdfceSMircea Gherzan _emit(ARM_COND_AL, inst, ctx); 231ddecdfceSMircea Gherzan } 232ddecdfceSMircea Gherzan 23339c13c20SShubham Bansal /* 23439c13c20SShubham Bansal * Checks if immediate value can be converted to imm12(12 bits) value. 23539c13c20SShubham Bansal */ 23639c13c20SShubham Bansal static int16_t imm8m(u32 x) 237ddecdfceSMircea Gherzan { 23839c13c20SShubham Bansal u32 rot; 239ddecdfceSMircea Gherzan 24039c13c20SShubham Bansal for (rot = 0; rot < 16; rot++) 24139c13c20SShubham Bansal if ((x & ~ror32(0xff, 2 * rot)) == 0) 24239c13c20SShubham Bansal return rol32(x, 2 * rot) | (rot << 8); 24339c13c20SShubham Bansal return -1; 244ddecdfceSMircea Gherzan } 245ddecdfceSMircea Gherzan 246a8ef95a0SRussell King static u32 arm_bpf_ldst_imm12(u32 op, u8 rt, u8 rn, s16 imm12) 247a8ef95a0SRussell King { 248a8ef95a0SRussell King op |= rt << 12 | rn << 16; 249a8ef95a0SRussell King if (imm12 >= 0) 250a8ef95a0SRussell King op |= ARM_INST_LDST__U; 251a8ef95a0SRussell King else 252a8ef95a0SRussell King imm12 = -imm12; 253a8ef95a0SRussell King return op | (imm12 & 0xfff); 254a8ef95a0SRussell King } 255a8ef95a0SRussell King 256a8ef95a0SRussell King static u32 arm_bpf_ldst_imm8(u32 op, u8 rt, u8 rn, s16 imm8) 257a8ef95a0SRussell King { 258a8ef95a0SRussell King op |= rt << 12 | rn << 16; 259a8ef95a0SRussell King if (imm8 >= 0) 260a8ef95a0SRussell King op |= ARM_INST_LDST__U; 261a8ef95a0SRussell King else 262a8ef95a0SRussell King imm8 = -imm8; 263a8ef95a0SRussell King return op | (imm8 & 0xf0) << 4 | (imm8 & 0x0f); 264a8ef95a0SRussell King } 265a8ef95a0SRussell King 266a8ef95a0SRussell King #define ARM_LDR_I(rt, rn, off) arm_bpf_ldst_imm12(ARM_INST_LDR_I, rt, rn, off) 267a8ef95a0SRussell King #define ARM_LDRB_I(rt, rn, off) arm_bpf_ldst_imm12(ARM_INST_LDRB_I, rt, rn, off) 268a8ef95a0SRussell King #define ARM_LDRH_I(rt, rn, off) arm_bpf_ldst_imm8(ARM_INST_LDRH_I, rt, rn, off) 269a8ef95a0SRussell King 270a8ef95a0SRussell King #define ARM_STR_I(rt, rn, off) arm_bpf_ldst_imm12(ARM_INST_STR_I, rt, rn, off) 271a8ef95a0SRussell King #define ARM_STRB_I(rt, rn, off) arm_bpf_ldst_imm12(ARM_INST_STRB_I, rt, rn, off) 272a8ef95a0SRussell King #define ARM_STRH_I(rt, rn, off) arm_bpf_ldst_imm8(ARM_INST_STRH_I, rt, rn, off) 273a8ef95a0SRussell King 27439c13c20SShubham Bansal /* 27539c13c20SShubham Bansal * Initializes the JIT space with undefined instructions. 27639c13c20SShubham Bansal */ 27755309dd3SDaniel Borkmann static void jit_fill_hole(void *area, unsigned int size) 27855309dd3SDaniel Borkmann { 279e8b56d55SDaniel Borkmann u32 *ptr; 28055309dd3SDaniel Borkmann /* We are guaranteed to have aligned memory. */ 28155309dd3SDaniel Borkmann for (ptr = area; size >= sizeof(u32); size -= sizeof(u32)) 282e8b56d55SDaniel Borkmann *ptr++ = __opcode_to_mem_arm(ARM_INST_UDF); 28355309dd3SDaniel Borkmann } 28455309dd3SDaniel Borkmann 285d1220efdSRussell King #if defined(CONFIG_AEABI) && (__LINUX_ARM_ARCH__ >= 5) 286d1220efdSRussell King /* EABI requires the stack to be aligned to 64-bit boundaries */ 287d1220efdSRussell King #define STACK_ALIGNMENT 8 288d1220efdSRussell King #else 289d1220efdSRussell King /* Stack must be aligned to 32-bit boundaries */ 290d1220efdSRussell King #define STACK_ALIGNMENT 4 291d1220efdSRussell King #endif 292ddecdfceSMircea Gherzan 29339c13c20SShubham Bansal /* total stack size used in JITed code */ 29438ca9306SDaniel Borkmann #define _STACK_SIZE (ctx->prog->aux->stack_depth + SCRATCH_SIZE) 295d1220efdSRussell King #define STACK_SIZE ALIGN(_STACK_SIZE, STACK_ALIGNMENT) 296ddecdfceSMircea Gherzan 29739c13c20SShubham Bansal /* Get the offset of eBPF REGISTERs stored on scratch space. */ 2981c35ba12SRussell King #define STACK_VAR(off) (STACK_SIZE + (off)) 299ddecdfceSMircea Gherzan 300ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7 301ddecdfceSMircea Gherzan 302ddecdfceSMircea Gherzan static u16 imm_offset(u32 k, struct jit_ctx *ctx) 303ddecdfceSMircea Gherzan { 30439c13c20SShubham Bansal unsigned int i = 0, offset; 305ddecdfceSMircea Gherzan u16 imm; 306ddecdfceSMircea Gherzan 307ddecdfceSMircea Gherzan /* on the "fake" run we just count them (duplicates included) */ 308ddecdfceSMircea Gherzan if (ctx->target == NULL) { 309ddecdfceSMircea Gherzan ctx->imm_count++; 310ddecdfceSMircea Gherzan return 0; 311ddecdfceSMircea Gherzan } 312ddecdfceSMircea Gherzan 313ddecdfceSMircea Gherzan while ((i < ctx->imm_count) && ctx->imms[i]) { 314ddecdfceSMircea Gherzan if (ctx->imms[i] == k) 315ddecdfceSMircea Gherzan break; 316ddecdfceSMircea Gherzan i++; 317ddecdfceSMircea Gherzan } 318ddecdfceSMircea Gherzan 319ddecdfceSMircea Gherzan if (ctx->imms[i] == 0) 320ddecdfceSMircea Gherzan ctx->imms[i] = k; 321ddecdfceSMircea Gherzan 322ddecdfceSMircea Gherzan /* constants go just after the epilogue */ 32339c13c20SShubham Bansal offset = ctx->offsets[ctx->prog->len - 1] * 4; 324ddecdfceSMircea Gherzan offset += ctx->prologue_bytes; 325ddecdfceSMircea Gherzan offset += ctx->epilogue_bytes; 326ddecdfceSMircea Gherzan offset += i * 4; 327ddecdfceSMircea Gherzan 328ddecdfceSMircea Gherzan ctx->target[offset / 4] = k; 329ddecdfceSMircea Gherzan 330ddecdfceSMircea Gherzan /* PC in ARM mode == address of the instruction + 8 */ 331ddecdfceSMircea Gherzan imm = offset - (8 + ctx->idx * 4); 332ddecdfceSMircea Gherzan 3330b59d880SNicolas Schichan if (imm & ~0xfff) { 3340b59d880SNicolas Schichan /* 3350b59d880SNicolas Schichan * literal pool is too far, signal it into flags. we 3360b59d880SNicolas Schichan * can only detect it on the second pass unfortunately. 3370b59d880SNicolas Schichan */ 3380b59d880SNicolas Schichan ctx->flags |= FLAG_IMM_OVERFLOW; 3390b59d880SNicolas Schichan return 0; 3400b59d880SNicolas Schichan } 3410b59d880SNicolas Schichan 342ddecdfceSMircea Gherzan return imm; 343ddecdfceSMircea Gherzan } 344ddecdfceSMircea Gherzan 345ddecdfceSMircea Gherzan #endif /* __LINUX_ARM_ARCH__ */ 346ddecdfceSMircea Gherzan 34739c13c20SShubham Bansal static inline int bpf2a32_offset(int bpf_to, int bpf_from, 34839c13c20SShubham Bansal const struct jit_ctx *ctx) { 34939c13c20SShubham Bansal int to, from; 35039c13c20SShubham Bansal 35139c13c20SShubham Bansal if (ctx->target == NULL) 35239c13c20SShubham Bansal return 0; 35339c13c20SShubham Bansal to = ctx->offsets[bpf_to]; 35439c13c20SShubham Bansal from = ctx->offsets[bpf_from]; 35539c13c20SShubham Bansal 35639c13c20SShubham Bansal return to - from - 1; 35739c13c20SShubham Bansal } 35839c13c20SShubham Bansal 359ddecdfceSMircea Gherzan /* 360ddecdfceSMircea Gherzan * Move an immediate that's not an imm8m to a core register. 361ddecdfceSMircea Gherzan */ 36239c13c20SShubham Bansal static inline void emit_mov_i_no8m(const u8 rd, u32 val, struct jit_ctx *ctx) 363ddecdfceSMircea Gherzan { 364ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7 365ddecdfceSMircea Gherzan emit(ARM_LDR_I(rd, ARM_PC, imm_offset(val, ctx)), ctx); 366ddecdfceSMircea Gherzan #else 367ddecdfceSMircea Gherzan emit(ARM_MOVW(rd, val & 0xffff), ctx); 368ddecdfceSMircea Gherzan if (val > 0xffff) 369ddecdfceSMircea Gherzan emit(ARM_MOVT(rd, val >> 16), ctx); 370ddecdfceSMircea Gherzan #endif 371ddecdfceSMircea Gherzan } 372ddecdfceSMircea Gherzan 37339c13c20SShubham Bansal static inline void emit_mov_i(const u8 rd, u32 val, struct jit_ctx *ctx) 374ddecdfceSMircea Gherzan { 375ddecdfceSMircea Gherzan int imm12 = imm8m(val); 376ddecdfceSMircea Gherzan 377ddecdfceSMircea Gherzan if (imm12 >= 0) 378ddecdfceSMircea Gherzan emit(ARM_MOV_I(rd, imm12), ctx); 379ddecdfceSMircea Gherzan else 380ddecdfceSMircea Gherzan emit_mov_i_no8m(rd, val, ctx); 381ddecdfceSMircea Gherzan } 382ddecdfceSMircea Gherzan 383e9062481SRussell King static void emit_bx_r(u8 tgt_reg, struct jit_ctx *ctx) 384ddecdfceSMircea Gherzan { 385ddecdfceSMircea Gherzan if (elf_hwcap & HWCAP_THUMB) 386ddecdfceSMircea Gherzan emit(ARM_BX(tgt_reg), ctx); 387ddecdfceSMircea Gherzan else 388ddecdfceSMircea Gherzan emit(ARM_MOV_R(ARM_PC, tgt_reg), ctx); 389e9062481SRussell King } 390e9062481SRussell King 391ddecdfceSMircea Gherzan static inline void emit_blx_r(u8 tgt_reg, struct jit_ctx *ctx) 392ddecdfceSMircea Gherzan { 393ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 5 394ddecdfceSMircea Gherzan emit(ARM_MOV_R(ARM_LR, ARM_PC), ctx); 395e9062481SRussell King emit_bx_r(tgt_reg, ctx); 396ddecdfceSMircea Gherzan #else 397ddecdfceSMircea Gherzan emit(ARM_BLX_R(tgt_reg), ctx); 398ddecdfceSMircea Gherzan #endif 399ddecdfceSMircea Gherzan } 400ddecdfceSMircea Gherzan 40139c13c20SShubham Bansal static inline int epilogue_offset(const struct jit_ctx *ctx) 402ddecdfceSMircea Gherzan { 40339c13c20SShubham Bansal int to, from; 40439c13c20SShubham Bansal /* No need for 1st dummy run */ 40539c13c20SShubham Bansal if (ctx->target == NULL) 40639c13c20SShubham Bansal return 0; 40739c13c20SShubham Bansal to = ctx->epilogue_offset; 40839c13c20SShubham Bansal from = ctx->idx; 40939c13c20SShubham Bansal 41039c13c20SShubham Bansal return to - from - 2; 41139c13c20SShubham Bansal } 41239c13c20SShubham Bansal 41339c13c20SShubham Bansal static inline void emit_udivmod(u8 rd, u8 rm, u8 rn, struct jit_ctx *ctx, u8 op) 41439c13c20SShubham Bansal { 4151c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 41639c13c20SShubham Bansal 417ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ == 7 418ddecdfceSMircea Gherzan if (elf_hwcap & HWCAP_IDIVA) { 41939c13c20SShubham Bansal if (op == BPF_DIV) 420ddecdfceSMircea Gherzan emit(ARM_UDIV(rd, rm, rn), ctx); 4214560cdffSNicolas Schichan else { 42239c13c20SShubham Bansal emit(ARM_UDIV(ARM_IP, rm, rn), ctx); 42339c13c20SShubham Bansal emit(ARM_MLS(rd, rn, ARM_IP, rm), ctx); 4244560cdffSNicolas Schichan } 425ddecdfceSMircea Gherzan return; 426ddecdfceSMircea Gherzan } 427ddecdfceSMircea Gherzan #endif 42819fc99d0SNicolas Schichan 42919fc99d0SNicolas Schichan /* 43039c13c20SShubham Bansal * For BPF_ALU | BPF_DIV | BPF_K instructions 43139c13c20SShubham Bansal * As ARM_R1 and ARM_R0 contains 1st argument of bpf 43239c13c20SShubham Bansal * function, we need to save it on caller side to save 43339c13c20SShubham Bansal * it from getting destroyed within callee. 43439c13c20SShubham Bansal * After the return from the callee, we restore ARM_R0 43539c13c20SShubham Bansal * ARM_R1. 43619fc99d0SNicolas Schichan */ 43739c13c20SShubham Bansal if (rn != ARM_R1) { 43839c13c20SShubham Bansal emit(ARM_MOV_R(tmp[0], ARM_R1), ctx); 439ddecdfceSMircea Gherzan emit(ARM_MOV_R(ARM_R1, rn), ctx); 44039c13c20SShubham Bansal } 44139c13c20SShubham Bansal if (rm != ARM_R0) { 44239c13c20SShubham Bansal emit(ARM_MOV_R(tmp[1], ARM_R0), ctx); 44319fc99d0SNicolas Schichan emit(ARM_MOV_R(ARM_R0, rm), ctx); 44439c13c20SShubham Bansal } 445ddecdfceSMircea Gherzan 44639c13c20SShubham Bansal /* Call appropriate function */ 44739c13c20SShubham Bansal emit_mov_i(ARM_IP, op == BPF_DIV ? 44839c13c20SShubham Bansal (u32)jit_udiv32 : (u32)jit_mod32, ctx); 44939c13c20SShubham Bansal emit_blx_r(ARM_IP, ctx); 450ddecdfceSMircea Gherzan 45139c13c20SShubham Bansal /* Save return value */ 452ddecdfceSMircea Gherzan if (rd != ARM_R0) 453ddecdfceSMircea Gherzan emit(ARM_MOV_R(rd, ARM_R0), ctx); 45439c13c20SShubham Bansal 45539c13c20SShubham Bansal /* Restore ARM_R0 and ARM_R1 */ 45639c13c20SShubham Bansal if (rn != ARM_R1) 45739c13c20SShubham Bansal emit(ARM_MOV_R(ARM_R1, tmp[0]), ctx); 45839c13c20SShubham Bansal if (rm != ARM_R0) 45939c13c20SShubham Bansal emit(ARM_MOV_R(ARM_R0, tmp[1]), ctx); 460ddecdfceSMircea Gherzan } 461ddecdfceSMircea Gherzan 46247b9c3bfSRussell King /* Is the translated BPF register on stack? */ 46347b9c3bfSRussell King static bool is_stacked(s8 reg) 464ddecdfceSMircea Gherzan { 46547b9c3bfSRussell King return reg < 0; 466ddecdfceSMircea Gherzan } 467ddecdfceSMircea Gherzan 4687a987025SRussell King /* If a BPF register is on the stack (stk is true), load it to the 4697a987025SRussell King * supplied temporary register and return the temporary register 4707a987025SRussell King * for subsequent operations, otherwise just use the CPU register. 4717a987025SRussell King */ 4727a987025SRussell King static s8 arm_bpf_get_reg32(s8 reg, s8 tmp, struct jit_ctx *ctx) 4737a987025SRussell King { 4747a987025SRussell King if (is_stacked(reg)) { 4757a987025SRussell King emit(ARM_LDR_I(tmp, ARM_SP, STACK_VAR(reg)), ctx); 4767a987025SRussell King reg = tmp; 4777a987025SRussell King } 4787a987025SRussell King return reg; 4797a987025SRussell King } 4807a987025SRussell King 481*a6eccac5SRussell King static const s8 *arm_bpf_get_reg64(const s8 *reg, const s8 *tmp, 482*a6eccac5SRussell King struct jit_ctx *ctx) 483*a6eccac5SRussell King { 484*a6eccac5SRussell King if (is_stacked(reg[1])) { 485*a6eccac5SRussell King emit(ARM_LDR_I(tmp[1], ARM_SP, STACK_VAR(reg[1])), ctx); 486*a6eccac5SRussell King emit(ARM_LDR_I(tmp[0], ARM_SP, STACK_VAR(reg[0])), ctx); 487*a6eccac5SRussell King reg = tmp; 488*a6eccac5SRussell King } 489*a6eccac5SRussell King return reg; 490*a6eccac5SRussell King } 491*a6eccac5SRussell King 4927a987025SRussell King /* If a BPF register is on the stack (stk is true), save the register 4937a987025SRussell King * back to the stack. If the source register is not the same, then 4947a987025SRussell King * move it into the correct register. 4957a987025SRussell King */ 4967a987025SRussell King static void arm_bpf_put_reg32(s8 reg, s8 src, struct jit_ctx *ctx) 4977a987025SRussell King { 4987a987025SRussell King if (is_stacked(reg)) 4997a987025SRussell King emit(ARM_STR_I(src, ARM_SP, STACK_VAR(reg)), ctx); 5007a987025SRussell King else if (reg != src) 5017a987025SRussell King emit(ARM_MOV_R(reg, src), ctx); 5027a987025SRussell King } 5037a987025SRussell King 504*a6eccac5SRussell King static void arm_bpf_put_reg64(const s8 *reg, const s8 *src, 505*a6eccac5SRussell King struct jit_ctx *ctx) 506*a6eccac5SRussell King { 507*a6eccac5SRussell King if (is_stacked(reg[1])) { 508*a6eccac5SRussell King emit(ARM_STR_I(src[1], ARM_SP, STACK_VAR(reg[1])), ctx); 509*a6eccac5SRussell King emit(ARM_STR_I(src[0], ARM_SP, STACK_VAR(reg[0])), ctx); 510*a6eccac5SRussell King } else { 511*a6eccac5SRussell King if (reg[1] != src[1]) 512*a6eccac5SRussell King emit(ARM_MOV_R(reg[1], src[1]), ctx); 513*a6eccac5SRussell King if (reg[0] != src[0]) 514*a6eccac5SRussell King emit(ARM_MOV_R(reg[0], src[0]), ctx); 515*a6eccac5SRussell King } 516*a6eccac5SRussell King } 517*a6eccac5SRussell King 5181c35ba12SRussell King static inline void emit_a32_mov_i(const s8 dst, const u32 val, 51947b9c3bfSRussell King struct jit_ctx *ctx) 520ddecdfceSMircea Gherzan { 5211c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 522ddecdfceSMircea Gherzan 52347b9c3bfSRussell King if (is_stacked(dst)) { 52439c13c20SShubham Bansal emit_mov_i(tmp[1], val, ctx); 5257a987025SRussell King arm_bpf_put_reg32(dst, tmp[1], ctx); 52639c13c20SShubham Bansal } else { 52739c13c20SShubham Bansal emit_mov_i(dst, val, ctx); 52839c13c20SShubham Bansal } 52939c13c20SShubham Bansal } 53034805931SDaniel Borkmann 53139c13c20SShubham Bansal /* Sign extended move */ 5321c35ba12SRussell King static inline void emit_a32_mov_i64(const bool is64, const s8 dst[], 53347b9c3bfSRussell King const u32 val, struct jit_ctx *ctx) { 53439c13c20SShubham Bansal u32 hi = 0; 535ddecdfceSMircea Gherzan 53639c13c20SShubham Bansal if (is64 && (val & (1<<31))) 53739c13c20SShubham Bansal hi = (u32)~0; 53847b9c3bfSRussell King emit_a32_mov_i(dst_lo, val, ctx); 53947b9c3bfSRussell King emit_a32_mov_i(dst_hi, hi, ctx); 54039c13c20SShubham Bansal } 54139c13c20SShubham Bansal 54239c13c20SShubham Bansal static inline void emit_a32_add_r(const u8 dst, const u8 src, 54339c13c20SShubham Bansal const bool is64, const bool hi, 54439c13c20SShubham Bansal struct jit_ctx *ctx) { 54539c13c20SShubham Bansal /* 64 bit : 54639c13c20SShubham Bansal * adds dst_lo, dst_lo, src_lo 54739c13c20SShubham Bansal * adc dst_hi, dst_hi, src_hi 54839c13c20SShubham Bansal * 32 bit : 54939c13c20SShubham Bansal * add dst_lo, dst_lo, src_lo 55039c13c20SShubham Bansal */ 55139c13c20SShubham Bansal if (!hi && is64) 55239c13c20SShubham Bansal emit(ARM_ADDS_R(dst, dst, src), ctx); 55339c13c20SShubham Bansal else if (hi && is64) 55439c13c20SShubham Bansal emit(ARM_ADC_R(dst, dst, src), ctx); 55539c13c20SShubham Bansal else 55639c13c20SShubham Bansal emit(ARM_ADD_R(dst, dst, src), ctx); 55739c13c20SShubham Bansal } 55839c13c20SShubham Bansal 55939c13c20SShubham Bansal static inline void emit_a32_sub_r(const u8 dst, const u8 src, 56039c13c20SShubham Bansal const bool is64, const bool hi, 56139c13c20SShubham Bansal struct jit_ctx *ctx) { 56239c13c20SShubham Bansal /* 64 bit : 56339c13c20SShubham Bansal * subs dst_lo, dst_lo, src_lo 56439c13c20SShubham Bansal * sbc dst_hi, dst_hi, src_hi 56539c13c20SShubham Bansal * 32 bit : 56639c13c20SShubham Bansal * sub dst_lo, dst_lo, src_lo 56739c13c20SShubham Bansal */ 56839c13c20SShubham Bansal if (!hi && is64) 56939c13c20SShubham Bansal emit(ARM_SUBS_R(dst, dst, src), ctx); 57039c13c20SShubham Bansal else if (hi && is64) 57139c13c20SShubham Bansal emit(ARM_SBC_R(dst, dst, src), ctx); 57239c13c20SShubham Bansal else 57339c13c20SShubham Bansal emit(ARM_SUB_R(dst, dst, src), ctx); 57439c13c20SShubham Bansal } 57539c13c20SShubham Bansal 57639c13c20SShubham Bansal static inline void emit_alu_r(const u8 dst, const u8 src, const bool is64, 57739c13c20SShubham Bansal const bool hi, const u8 op, struct jit_ctx *ctx){ 57839c13c20SShubham Bansal switch (BPF_OP(op)) { 57939c13c20SShubham Bansal /* dst = dst + src */ 58039c13c20SShubham Bansal case BPF_ADD: 58139c13c20SShubham Bansal emit_a32_add_r(dst, src, is64, hi, ctx); 58239c13c20SShubham Bansal break; 58339c13c20SShubham Bansal /* dst = dst - src */ 58439c13c20SShubham Bansal case BPF_SUB: 58539c13c20SShubham Bansal emit_a32_sub_r(dst, src, is64, hi, ctx); 58639c13c20SShubham Bansal break; 58739c13c20SShubham Bansal /* dst = dst | src */ 58839c13c20SShubham Bansal case BPF_OR: 58939c13c20SShubham Bansal emit(ARM_ORR_R(dst, dst, src), ctx); 59039c13c20SShubham Bansal break; 59139c13c20SShubham Bansal /* dst = dst & src */ 59239c13c20SShubham Bansal case BPF_AND: 59339c13c20SShubham Bansal emit(ARM_AND_R(dst, dst, src), ctx); 59439c13c20SShubham Bansal break; 59539c13c20SShubham Bansal /* dst = dst ^ src */ 59639c13c20SShubham Bansal case BPF_XOR: 59739c13c20SShubham Bansal emit(ARM_EOR_R(dst, dst, src), ctx); 59839c13c20SShubham Bansal break; 59939c13c20SShubham Bansal /* dst = dst * src */ 60039c13c20SShubham Bansal case BPF_MUL: 60139c13c20SShubham Bansal emit(ARM_MUL(dst, dst, src), ctx); 60239c13c20SShubham Bansal break; 60339c13c20SShubham Bansal /* dst = dst << src */ 60439c13c20SShubham Bansal case BPF_LSH: 60539c13c20SShubham Bansal emit(ARM_LSL_R(dst, dst, src), ctx); 60639c13c20SShubham Bansal break; 60739c13c20SShubham Bansal /* dst = dst >> src */ 60839c13c20SShubham Bansal case BPF_RSH: 60939c13c20SShubham Bansal emit(ARM_LSR_R(dst, dst, src), ctx); 61039c13c20SShubham Bansal break; 61139c13c20SShubham Bansal /* dst = dst >> src (signed)*/ 61239c13c20SShubham Bansal case BPF_ARSH: 61339c13c20SShubham Bansal emit(ARM_MOV_SR(dst, dst, SRTYPE_ASR, src), ctx); 61439c13c20SShubham Bansal break; 61539c13c20SShubham Bansal } 61639c13c20SShubham Bansal } 61739c13c20SShubham Bansal 61839c13c20SShubham Bansal /* ALU operation (32 bit) 61939c13c20SShubham Bansal * dst = dst (op) src 62039c13c20SShubham Bansal */ 6211c35ba12SRussell King static inline void emit_a32_alu_r(const s8 dst, const s8 src, 62239c13c20SShubham Bansal struct jit_ctx *ctx, const bool is64, 62339c13c20SShubham Bansal const bool hi, const u8 op) { 6241c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 6257a987025SRussell King s8 rn, rd; 62639c13c20SShubham Bansal 6277a987025SRussell King rn = arm_bpf_get_reg32(src, tmp[1], ctx); 6287a987025SRussell King rd = arm_bpf_get_reg32(dst, tmp[0], ctx); 62939c13c20SShubham Bansal /* ALU operation */ 6307a987025SRussell King emit_alu_r(rd, rn, is64, hi, op, ctx); 6317a987025SRussell King arm_bpf_put_reg32(dst, rd, ctx); 63239c13c20SShubham Bansal } 63339c13c20SShubham Bansal 63439c13c20SShubham Bansal /* ALU operation (64 bit) */ 6351c35ba12SRussell King static inline void emit_a32_alu_r64(const bool is64, const s8 dst[], 63647b9c3bfSRussell King const s8 src[], struct jit_ctx *ctx, 63739c13c20SShubham Bansal const u8 op) { 63847b9c3bfSRussell King emit_a32_alu_r(dst_lo, src_lo, ctx, is64, false, op); 63939c13c20SShubham Bansal if (is64) 64047b9c3bfSRussell King emit_a32_alu_r(dst_hi, src_hi, ctx, is64, true, op); 64139c13c20SShubham Bansal else 64247b9c3bfSRussell King emit_a32_mov_i(dst_hi, 0, ctx); 64339c13c20SShubham Bansal } 64439c13c20SShubham Bansal 6457a987025SRussell King /* dst = src (4 bytes)*/ 6461c35ba12SRussell King static inline void emit_a32_mov_r(const s8 dst, const s8 src, 64739c13c20SShubham Bansal struct jit_ctx *ctx) { 6481c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 6497a987025SRussell King s8 rt; 65039c13c20SShubham Bansal 6517a987025SRussell King rt = arm_bpf_get_reg32(src, tmp[0], ctx); 6527a987025SRussell King arm_bpf_put_reg32(dst, rt, ctx); 65339c13c20SShubham Bansal } 65439c13c20SShubham Bansal 65539c13c20SShubham Bansal /* dst = src */ 6561c35ba12SRussell King static inline void emit_a32_mov_r64(const bool is64, const s8 dst[], 65747b9c3bfSRussell King const s8 src[], 65847b9c3bfSRussell King struct jit_ctx *ctx) { 65947b9c3bfSRussell King emit_a32_mov_r(dst_lo, src_lo, ctx); 66039c13c20SShubham Bansal if (is64) { 66139c13c20SShubham Bansal /* complete 8 byte move */ 66247b9c3bfSRussell King emit_a32_mov_r(dst_hi, src_hi, ctx); 66339c13c20SShubham Bansal } else { 66439c13c20SShubham Bansal /* Zero out high 4 bytes */ 66547b9c3bfSRussell King emit_a32_mov_i(dst_hi, 0, ctx); 66639c13c20SShubham Bansal } 66739c13c20SShubham Bansal } 66839c13c20SShubham Bansal 66939c13c20SShubham Bansal /* Shift operations */ 67047b9c3bfSRussell King static inline void emit_a32_alu_i(const s8 dst, const u32 val, 67139c13c20SShubham Bansal struct jit_ctx *ctx, const u8 op) { 6721c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 6737a987025SRussell King s8 rd; 67439c13c20SShubham Bansal 6757a987025SRussell King rd = arm_bpf_get_reg32(dst, tmp[0], ctx); 67639c13c20SShubham Bansal 67739c13c20SShubham Bansal /* Do shift operation */ 67839c13c20SShubham Bansal switch (op) { 67939c13c20SShubham Bansal case BPF_LSH: 68039c13c20SShubham Bansal emit(ARM_LSL_I(rd, rd, val), ctx); 68139c13c20SShubham Bansal break; 68239c13c20SShubham Bansal case BPF_RSH: 68339c13c20SShubham Bansal emit(ARM_LSR_I(rd, rd, val), ctx); 68439c13c20SShubham Bansal break; 68539c13c20SShubham Bansal case BPF_NEG: 68639c13c20SShubham Bansal emit(ARM_RSB_I(rd, rd, val), ctx); 68739c13c20SShubham Bansal break; 68839c13c20SShubham Bansal } 68939c13c20SShubham Bansal 6907a987025SRussell King arm_bpf_put_reg32(dst, rd, ctx); 69139c13c20SShubham Bansal } 69239c13c20SShubham Bansal 69339c13c20SShubham Bansal /* dst = ~dst (64 bit) */ 69447b9c3bfSRussell King static inline void emit_a32_neg64(const s8 dst[], 69539c13c20SShubham Bansal struct jit_ctx *ctx){ 6961c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 697*a6eccac5SRussell King const s8 *rd; 69839c13c20SShubham Bansal 69939c13c20SShubham Bansal /* Setup Operand */ 700*a6eccac5SRussell King rd = arm_bpf_get_reg64(dst, tmp, ctx); 70139c13c20SShubham Bansal 70239c13c20SShubham Bansal /* Do Negate Operation */ 703*a6eccac5SRussell King emit(ARM_RSBS_I(rd[1], rd[1], 0), ctx); 704*a6eccac5SRussell King emit(ARM_RSC_I(rd[0], rd[0], 0), ctx); 70539c13c20SShubham Bansal 706*a6eccac5SRussell King arm_bpf_put_reg64(dst, rd, ctx); 70739c13c20SShubham Bansal } 70839c13c20SShubham Bansal 70939c13c20SShubham Bansal /* dst = dst << src */ 71047b9c3bfSRussell King static inline void emit_a32_lsh_r64(const s8 dst[], const s8 src[], 71147b9c3bfSRussell King struct jit_ctx *ctx) { 7121c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 7131c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 714*a6eccac5SRussell King const s8 *rd; 715*a6eccac5SRussell King s8 rt; 71639c13c20SShubham Bansal 71739c13c20SShubham Bansal /* Setup Operands */ 7187a987025SRussell King rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx); 719*a6eccac5SRussell King rd = arm_bpf_get_reg64(dst, tmp, ctx); 72039c13c20SShubham Bansal 72139c13c20SShubham Bansal /* Do LSH operation */ 72239c13c20SShubham Bansal emit(ARM_SUB_I(ARM_IP, rt, 32), ctx); 72339c13c20SShubham Bansal emit(ARM_RSB_I(tmp2[0], rt, 32), ctx); 724*a6eccac5SRussell King emit(ARM_MOV_SR(ARM_LR, rd[0], SRTYPE_ASL, rt), ctx); 725*a6eccac5SRussell King emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[1], SRTYPE_ASL, ARM_IP), ctx); 726*a6eccac5SRussell King emit(ARM_ORR_SR(ARM_IP, ARM_LR, rd[1], SRTYPE_LSR, tmp2[0]), ctx); 727*a6eccac5SRussell King emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_ASL, rt), ctx); 72839c13c20SShubham Bansal 7297a987025SRussell King arm_bpf_put_reg32(dst_lo, ARM_LR, ctx); 7307a987025SRussell King arm_bpf_put_reg32(dst_hi, ARM_IP, ctx); 73139c13c20SShubham Bansal } 73239c13c20SShubham Bansal 73339c13c20SShubham Bansal /* dst = dst >> src (signed)*/ 73447b9c3bfSRussell King static inline void emit_a32_arsh_r64(const s8 dst[], const s8 src[], 73547b9c3bfSRussell King struct jit_ctx *ctx) { 7361c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 7371c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 738*a6eccac5SRussell King const s8 *rd; 739*a6eccac5SRussell King s8 rt; 74039c13c20SShubham Bansal 7417a987025SRussell King /* Setup Operands */ 7427a987025SRussell King rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx); 743*a6eccac5SRussell King rd = arm_bpf_get_reg64(dst, tmp, ctx); 74439c13c20SShubham Bansal 74539c13c20SShubham Bansal /* Do the ARSH operation */ 74639c13c20SShubham Bansal emit(ARM_RSB_I(ARM_IP, rt, 32), ctx); 74739c13c20SShubham Bansal emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx); 748*a6eccac5SRussell King emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_LSR, rt), ctx); 749*a6eccac5SRussell King emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASL, ARM_IP), ctx); 75039c13c20SShubham Bansal _emit(ARM_COND_MI, ARM_B(0), ctx); 751*a6eccac5SRussell King emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASR, tmp2[0]), ctx); 752*a6eccac5SRussell King emit(ARM_MOV_SR(ARM_IP, rd[0], SRTYPE_ASR, rt), ctx); 7537a987025SRussell King 7547a987025SRussell King arm_bpf_put_reg32(dst_lo, ARM_LR, ctx); 7557a987025SRussell King arm_bpf_put_reg32(dst_hi, ARM_IP, ctx); 75639c13c20SShubham Bansal } 75739c13c20SShubham Bansal 75839c13c20SShubham Bansal /* dst = dst >> src */ 75947b9c3bfSRussell King static inline void emit_a32_rsh_r64(const s8 dst[], const s8 src[], 76047b9c3bfSRussell King struct jit_ctx *ctx) { 7611c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 7621c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 763*a6eccac5SRussell King const s8 *rd; 764*a6eccac5SRussell King s8 rt; 76539c13c20SShubham Bansal 7667a987025SRussell King /* Setup Operands */ 7677a987025SRussell King rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx); 768*a6eccac5SRussell King rd = arm_bpf_get_reg64(dst, tmp, ctx); 76939c13c20SShubham Bansal 77068565a1aSWang YanQing /* Do RSH operation */ 77139c13c20SShubham Bansal emit(ARM_RSB_I(ARM_IP, rt, 32), ctx); 77239c13c20SShubham Bansal emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx); 773*a6eccac5SRussell King emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_LSR, rt), ctx); 774*a6eccac5SRussell King emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASL, ARM_IP), ctx); 775*a6eccac5SRussell King emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_LSR, tmp2[0]), ctx); 776*a6eccac5SRussell King emit(ARM_MOV_SR(ARM_IP, rd[0], SRTYPE_LSR, rt), ctx); 7777a987025SRussell King 7787a987025SRussell King arm_bpf_put_reg32(dst_lo, ARM_LR, ctx); 7797a987025SRussell King arm_bpf_put_reg32(dst_hi, ARM_IP, ctx); 78039c13c20SShubham Bansal } 78139c13c20SShubham Bansal 78239c13c20SShubham Bansal /* dst = dst << val */ 78347b9c3bfSRussell King static inline void emit_a32_lsh_i64(const s8 dst[], 78439c13c20SShubham Bansal const u32 val, struct jit_ctx *ctx){ 7851c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 7861c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 787*a6eccac5SRussell King const s8 *rd; 78839c13c20SShubham Bansal 7897a987025SRussell King /* Setup operands */ 790*a6eccac5SRussell King rd = arm_bpf_get_reg64(dst, tmp, ctx); 79139c13c20SShubham Bansal 79239c13c20SShubham Bansal /* Do LSH operation */ 79339c13c20SShubham Bansal if (val < 32) { 794*a6eccac5SRussell King emit(ARM_MOV_SI(tmp2[0], rd[0], SRTYPE_ASL, val), ctx); 795*a6eccac5SRussell King emit(ARM_ORR_SI(rd[0], tmp2[0], rd[1], SRTYPE_LSR, 32 - val), ctx); 796*a6eccac5SRussell King emit(ARM_MOV_SI(rd[1], rd[1], SRTYPE_ASL, val), ctx); 79739c13c20SShubham Bansal } else { 79839c13c20SShubham Bansal if (val == 32) 799*a6eccac5SRussell King emit(ARM_MOV_R(rd[0], rd[1]), ctx); 80039c13c20SShubham Bansal else 801*a6eccac5SRussell King emit(ARM_MOV_SI(rd[0], rd[1], SRTYPE_ASL, val - 32), ctx); 802*a6eccac5SRussell King emit(ARM_EOR_R(rd[1], rd[1], rd[1]), ctx); 80339c13c20SShubham Bansal } 80439c13c20SShubham Bansal 805*a6eccac5SRussell King arm_bpf_put_reg64(dst, rd, ctx); 80639c13c20SShubham Bansal } 80739c13c20SShubham Bansal 80839c13c20SShubham Bansal /* dst = dst >> val */ 80947b9c3bfSRussell King static inline void emit_a32_rsh_i64(const s8 dst[], 81039c13c20SShubham Bansal const u32 val, struct jit_ctx *ctx) { 8111c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 8121c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 813*a6eccac5SRussell King const s8 *rd; 81439c13c20SShubham Bansal 8157a987025SRussell King /* Setup operands */ 816*a6eccac5SRussell King rd = arm_bpf_get_reg64(dst, tmp, ctx); 81739c13c20SShubham Bansal 81839c13c20SShubham Bansal /* Do LSR operation */ 81939c13c20SShubham Bansal if (val < 32) { 820*a6eccac5SRussell King emit(ARM_MOV_SI(tmp2[1], rd[1], SRTYPE_LSR, val), ctx); 821*a6eccac5SRussell King emit(ARM_ORR_SI(rd[1], tmp2[1], rd[0], SRTYPE_ASL, 32 - val), ctx); 822*a6eccac5SRussell King emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_LSR, val), ctx); 82339c13c20SShubham Bansal } else if (val == 32) { 824*a6eccac5SRussell King emit(ARM_MOV_R(rd[1], rd[0]), ctx); 825*a6eccac5SRussell King emit(ARM_MOV_I(rd[0], 0), ctx); 82639c13c20SShubham Bansal } else { 827*a6eccac5SRussell King emit(ARM_MOV_SI(rd[1], rd[0], SRTYPE_LSR, val - 32), ctx); 828*a6eccac5SRussell King emit(ARM_MOV_I(rd[0], 0), ctx); 82939c13c20SShubham Bansal } 83039c13c20SShubham Bansal 831*a6eccac5SRussell King arm_bpf_put_reg64(dst, rd, ctx); 83239c13c20SShubham Bansal } 83339c13c20SShubham Bansal 83439c13c20SShubham Bansal /* dst = dst >> val (signed) */ 83547b9c3bfSRussell King static inline void emit_a32_arsh_i64(const s8 dst[], 83639c13c20SShubham Bansal const u32 val, struct jit_ctx *ctx){ 8371c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 8381c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 839*a6eccac5SRussell King const s8 *rd; 84039c13c20SShubham Bansal 8417a987025SRussell King /* Setup operands */ 842*a6eccac5SRussell King rd = arm_bpf_get_reg64(dst, tmp, ctx); 84339c13c20SShubham Bansal 84439c13c20SShubham Bansal /* Do ARSH operation */ 84539c13c20SShubham Bansal if (val < 32) { 846*a6eccac5SRussell King emit(ARM_MOV_SI(tmp2[1], rd[1], SRTYPE_LSR, val), ctx); 847*a6eccac5SRussell King emit(ARM_ORR_SI(rd[1], tmp2[1], rd[0], SRTYPE_ASL, 32 - val), ctx); 848*a6eccac5SRussell King emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, val), ctx); 84939c13c20SShubham Bansal } else if (val == 32) { 850*a6eccac5SRussell King emit(ARM_MOV_R(rd[1], rd[0]), ctx); 851*a6eccac5SRussell King emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, 31), ctx); 85239c13c20SShubham Bansal } else { 853*a6eccac5SRussell King emit(ARM_MOV_SI(rd[1], rd[0], SRTYPE_ASR, val - 32), ctx); 854*a6eccac5SRussell King emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, 31), ctx); 85539c13c20SShubham Bansal } 85639c13c20SShubham Bansal 857*a6eccac5SRussell King arm_bpf_put_reg64(dst, rd, ctx); 85839c13c20SShubham Bansal } 85939c13c20SShubham Bansal 86047b9c3bfSRussell King static inline void emit_a32_mul_r64(const s8 dst[], const s8 src[], 86147b9c3bfSRussell King struct jit_ctx *ctx) { 8621c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 8631c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 864*a6eccac5SRussell King const s8 *rd, *rt; 86539c13c20SShubham Bansal 8667a987025SRussell King /* Setup operands for multiplication */ 867*a6eccac5SRussell King rd = arm_bpf_get_reg64(dst, tmp, ctx); 868*a6eccac5SRussell King rt = arm_bpf_get_reg64(src, tmp2, ctx); 86939c13c20SShubham Bansal 87039c13c20SShubham Bansal /* Do Multiplication */ 871*a6eccac5SRussell King emit(ARM_MUL(ARM_IP, rd[1], rt[0]), ctx); 872*a6eccac5SRussell King emit(ARM_MUL(ARM_LR, rd[0], rt[1]), ctx); 87339c13c20SShubham Bansal emit(ARM_ADD_R(ARM_LR, ARM_IP, ARM_LR), ctx); 87439c13c20SShubham Bansal 875*a6eccac5SRussell King emit(ARM_UMULL(ARM_IP, rd[0], rd[1], rt[1]), ctx); 876*a6eccac5SRussell King emit(ARM_ADD_R(rd[0], ARM_LR, rd[0]), ctx); 8777a987025SRussell King 8787a987025SRussell King arm_bpf_put_reg32(dst_lo, ARM_IP, ctx); 879*a6eccac5SRussell King arm_bpf_put_reg32(dst_hi, rd[0], ctx); 88039c13c20SShubham Bansal } 88139c13c20SShubham Bansal 88239c13c20SShubham Bansal /* *(size *)(dst + off) = src */ 88347b9c3bfSRussell King static inline void emit_str_r(const s8 dst, const s8 src, 88439c13c20SShubham Bansal const s32 off, struct jit_ctx *ctx, const u8 sz){ 8851c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 8867a987025SRussell King s8 rd; 88739c13c20SShubham Bansal 8887a987025SRussell King rd = arm_bpf_get_reg32(dst, tmp[1], ctx); 88939c13c20SShubham Bansal if (off) { 89047b9c3bfSRussell King emit_a32_mov_i(tmp[0], off, ctx); 89139c13c20SShubham Bansal emit(ARM_ADD_R(tmp[0], rd, tmp[0]), ctx); 89239c13c20SShubham Bansal rd = tmp[0]; 89339c13c20SShubham Bansal } 89439c13c20SShubham Bansal switch (sz) { 89539c13c20SShubham Bansal case BPF_W: 89639c13c20SShubham Bansal /* Store a Word */ 89739c13c20SShubham Bansal emit(ARM_STR_I(src, rd, 0), ctx); 89839c13c20SShubham Bansal break; 89939c13c20SShubham Bansal case BPF_H: 90039c13c20SShubham Bansal /* Store a HalfWord */ 90139c13c20SShubham Bansal emit(ARM_STRH_I(src, rd, 0), ctx); 90239c13c20SShubham Bansal break; 90339c13c20SShubham Bansal case BPF_B: 90439c13c20SShubham Bansal /* Store a Byte */ 90539c13c20SShubham Bansal emit(ARM_STRB_I(src, rd, 0), ctx); 90639c13c20SShubham Bansal break; 90739c13c20SShubham Bansal } 90839c13c20SShubham Bansal } 90939c13c20SShubham Bansal 91039c13c20SShubham Bansal /* dst = *(size*)(src + off) */ 91147b9c3bfSRussell King static inline void emit_ldx_r(const s8 dst[], const s8 src, 912ec19e02bSRussell King s32 off, struct jit_ctx *ctx, const u8 sz){ 9131c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 91447b9c3bfSRussell King const s8 *rd = is_stacked(dst_lo) ? tmp : dst; 9151c35ba12SRussell King s8 rm = src; 916ec19e02bSRussell King s32 off_max; 91739c13c20SShubham Bansal 918ec19e02bSRussell King if (sz == BPF_H) 919ec19e02bSRussell King off_max = 0xff; 920ec19e02bSRussell King else 921ec19e02bSRussell King off_max = 0xfff; 922ec19e02bSRussell King 923ec19e02bSRussell King if (off < 0 || off > off_max) { 92447b9c3bfSRussell King emit_a32_mov_i(tmp[0], off, ctx); 92539c13c20SShubham Bansal emit(ARM_ADD_R(tmp[0], tmp[0], src), ctx); 92639c13c20SShubham Bansal rm = tmp[0]; 927ec19e02bSRussell King off = 0; 928ec19e02bSRussell King } else if (rd[1] == rm) { 929ec19e02bSRussell King emit(ARM_MOV_R(tmp[0], rm), ctx); 930ec19e02bSRussell King rm = tmp[0]; 93139c13c20SShubham Bansal } 93239c13c20SShubham Bansal switch (sz) { 933ec19e02bSRussell King case BPF_B: 934ec19e02bSRussell King /* Load a Byte */ 935ec19e02bSRussell King emit(ARM_LDRB_I(rd[1], rm, off), ctx); 936*a6eccac5SRussell King emit_a32_mov_i(rd[0], 0, ctx); 93739c13c20SShubham Bansal break; 93839c13c20SShubham Bansal case BPF_H: 93939c13c20SShubham Bansal /* Load a HalfWord */ 940ec19e02bSRussell King emit(ARM_LDRH_I(rd[1], rm, off), ctx); 941*a6eccac5SRussell King emit_a32_mov_i(rd[0], 0, ctx); 94239c13c20SShubham Bansal break; 943ec19e02bSRussell King case BPF_W: 944ec19e02bSRussell King /* Load a Word */ 945ec19e02bSRussell King emit(ARM_LDR_I(rd[1], rm, off), ctx); 946*a6eccac5SRussell King emit_a32_mov_i(rd[0], 0, ctx); 947ec19e02bSRussell King break; 948ec19e02bSRussell King case BPF_DW: 949ec19e02bSRussell King /* Load a Double Word */ 950ec19e02bSRussell King emit(ARM_LDR_I(rd[1], rm, off), ctx); 951ec19e02bSRussell King emit(ARM_LDR_I(rd[0], rm, off + 4), ctx); 95239c13c20SShubham Bansal break; 95339c13c20SShubham Bansal } 954*a6eccac5SRussell King arm_bpf_put_reg64(dst, rd, ctx); 95539c13c20SShubham Bansal } 95639c13c20SShubham Bansal 95739c13c20SShubham Bansal /* Arithmatic Operation */ 95839c13c20SShubham Bansal static inline void emit_ar_r(const u8 rd, const u8 rt, const u8 rm, 95939c13c20SShubham Bansal const u8 rn, struct jit_ctx *ctx, u8 op) { 96039c13c20SShubham Bansal switch (op) { 96139c13c20SShubham Bansal case BPF_JSET: 96239c13c20SShubham Bansal emit(ARM_AND_R(ARM_IP, rt, rn), ctx); 96339c13c20SShubham Bansal emit(ARM_AND_R(ARM_LR, rd, rm), ctx); 96439c13c20SShubham Bansal emit(ARM_ORRS_R(ARM_IP, ARM_LR, ARM_IP), ctx); 96539c13c20SShubham Bansal break; 96639c13c20SShubham Bansal case BPF_JEQ: 96739c13c20SShubham Bansal case BPF_JNE: 96839c13c20SShubham Bansal case BPF_JGT: 96939c13c20SShubham Bansal case BPF_JGE: 97039c13c20SShubham Bansal case BPF_JLE: 97139c13c20SShubham Bansal case BPF_JLT: 97239c13c20SShubham Bansal emit(ARM_CMP_R(rd, rm), ctx); 97339c13c20SShubham Bansal _emit(ARM_COND_EQ, ARM_CMP_R(rt, rn), ctx); 97439c13c20SShubham Bansal break; 97539c13c20SShubham Bansal case BPF_JSLE: 97639c13c20SShubham Bansal case BPF_JSGT: 97739c13c20SShubham Bansal emit(ARM_CMP_R(rn, rt), ctx); 97839c13c20SShubham Bansal emit(ARM_SBCS_R(ARM_IP, rm, rd), ctx); 97939c13c20SShubham Bansal break; 98039c13c20SShubham Bansal case BPF_JSLT: 98139c13c20SShubham Bansal case BPF_JSGE: 98239c13c20SShubham Bansal emit(ARM_CMP_R(rt, rn), ctx); 98339c13c20SShubham Bansal emit(ARM_SBCS_R(ARM_IP, rd, rm), ctx); 98439c13c20SShubham Bansal break; 98539c13c20SShubham Bansal } 98639c13c20SShubham Bansal } 98739c13c20SShubham Bansal 98839c13c20SShubham Bansal static int out_offset = -1; /* initialized on the first pass of build_body() */ 98939c13c20SShubham Bansal static int emit_bpf_tail_call(struct jit_ctx *ctx) 99039c13c20SShubham Bansal { 99139c13c20SShubham Bansal 99239c13c20SShubham Bansal /* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */ 9931c35ba12SRussell King const s8 *r2 = bpf2a32[BPF_REG_2]; 9941c35ba12SRussell King const s8 *r3 = bpf2a32[BPF_REG_3]; 9951c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 9961c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 9971c35ba12SRussell King const s8 *tcc = bpf2a32[TCALL_CNT]; 998*a6eccac5SRussell King const s8 *tc; 99939c13c20SShubham Bansal const int idx0 = ctx->idx; 100039c13c20SShubham Bansal #define cur_offset (ctx->idx - idx0) 1001f4483f2cSRussell King #define jmp_offset (out_offset - (cur_offset) - 2) 100239c13c20SShubham Bansal u32 off, lo, hi; 1003*a6eccac5SRussell King s8 r_array, r_index; 100439c13c20SShubham Bansal 100539c13c20SShubham Bansal /* if (index >= array->map.max_entries) 100639c13c20SShubham Bansal * goto out; 100739c13c20SShubham Bansal */ 100839c13c20SShubham Bansal off = offsetof(struct bpf_array, map.max_entries); 100939c13c20SShubham Bansal /* array->map.max_entries */ 101047b9c3bfSRussell King emit_a32_mov_i(tmp[1], off, ctx); 10117a987025SRussell King r_array = arm_bpf_get_reg32(r2[1], tmp2[1], ctx); 10127a987025SRussell King emit(ARM_LDR_R(tmp[1], r_array, tmp[1]), ctx); 1013091f0248SRussell King /* index is 32-bit for arrays */ 10147a987025SRussell King r_index = arm_bpf_get_reg32(r3[1], tmp2[1], ctx); 101539c13c20SShubham Bansal /* index >= array->map.max_entries */ 10167a987025SRussell King emit(ARM_CMP_R(r_index, tmp[1]), ctx); 101739c13c20SShubham Bansal _emit(ARM_COND_CS, ARM_B(jmp_offset), ctx); 101839c13c20SShubham Bansal 101939c13c20SShubham Bansal /* if (tail_call_cnt > MAX_TAIL_CALL_CNT) 102039c13c20SShubham Bansal * goto out; 102139c13c20SShubham Bansal * tail_call_cnt++; 102239c13c20SShubham Bansal */ 102339c13c20SShubham Bansal lo = (u32)MAX_TAIL_CALL_CNT; 102439c13c20SShubham Bansal hi = (u32)((u64)MAX_TAIL_CALL_CNT >> 32); 1025*a6eccac5SRussell King tc = arm_bpf_get_reg64(tcc, tmp, ctx); 1026*a6eccac5SRussell King emit(ARM_CMP_I(tc[0], hi), ctx); 1027*a6eccac5SRussell King _emit(ARM_COND_EQ, ARM_CMP_I(tc[1], lo), ctx); 102839c13c20SShubham Bansal _emit(ARM_COND_HI, ARM_B(jmp_offset), ctx); 1029*a6eccac5SRussell King emit(ARM_ADDS_I(tc[1], tc[1], 1), ctx); 1030*a6eccac5SRussell King emit(ARM_ADC_I(tc[0], tc[0], 0), ctx); 1031*a6eccac5SRussell King arm_bpf_put_reg64(tcc, tmp, ctx); 103239c13c20SShubham Bansal 103339c13c20SShubham Bansal /* prog = array->ptrs[index] 103439c13c20SShubham Bansal * if (prog == NULL) 103539c13c20SShubham Bansal * goto out; 103639c13c20SShubham Bansal */ 103739c13c20SShubham Bansal off = offsetof(struct bpf_array, ptrs); 103847b9c3bfSRussell King emit_a32_mov_i(tmp[1], off, ctx); 10397a987025SRussell King r_array = arm_bpf_get_reg32(r2[1], tmp2[1], ctx); 10407a987025SRussell King emit(ARM_ADD_R(tmp[1], r_array, tmp[1]), ctx); 10417a987025SRussell King r_index = arm_bpf_get_reg32(r3[1], tmp2[1], ctx); 10427a987025SRussell King emit(ARM_MOV_SI(tmp[0], r_index, SRTYPE_ASL, 2), ctx); 104339c13c20SShubham Bansal emit(ARM_LDR_R(tmp[1], tmp[1], tmp[0]), ctx); 104439c13c20SShubham Bansal emit(ARM_CMP_I(tmp[1], 0), ctx); 104539c13c20SShubham Bansal _emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx); 104639c13c20SShubham Bansal 104739c13c20SShubham Bansal /* goto *(prog->bpf_func + prologue_size); */ 104839c13c20SShubham Bansal off = offsetof(struct bpf_prog, bpf_func); 104947b9c3bfSRussell King emit_a32_mov_i(tmp2[1], off, ctx); 105039c13c20SShubham Bansal emit(ARM_LDR_R(tmp[1], tmp[1], tmp2[1]), ctx); 105139c13c20SShubham Bansal emit(ARM_ADD_I(tmp[1], tmp[1], ctx->prologue_bytes), ctx); 1052e9062481SRussell King emit_bx_r(tmp[1], ctx); 105339c13c20SShubham Bansal 105439c13c20SShubham Bansal /* out: */ 105539c13c20SShubham Bansal if (out_offset == -1) 105639c13c20SShubham Bansal out_offset = cur_offset; 105739c13c20SShubham Bansal if (cur_offset != out_offset) { 105839c13c20SShubham Bansal pr_err_once("tail_call out_offset = %d, expected %d!\n", 105939c13c20SShubham Bansal cur_offset, out_offset); 106039c13c20SShubham Bansal return -1; 106139c13c20SShubham Bansal } 106239c13c20SShubham Bansal return 0; 106339c13c20SShubham Bansal #undef cur_offset 106439c13c20SShubham Bansal #undef jmp_offset 106539c13c20SShubham Bansal } 106639c13c20SShubham Bansal 106739c13c20SShubham Bansal /* 0xabcd => 0xcdab */ 106839c13c20SShubham Bansal static inline void emit_rev16(const u8 rd, const u8 rn, struct jit_ctx *ctx) 106939c13c20SShubham Bansal { 107039c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6 10711c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 107239c13c20SShubham Bansal 107339c13c20SShubham Bansal emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx); 107439c13c20SShubham Bansal emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 8), ctx); 107539c13c20SShubham Bansal emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx); 107639c13c20SShubham Bansal emit(ARM_ORR_SI(rd, tmp2[0], tmp2[1], SRTYPE_LSL, 8), ctx); 107739c13c20SShubham Bansal #else /* ARMv6+ */ 107839c13c20SShubham Bansal emit(ARM_REV16(rd, rn), ctx); 107939c13c20SShubham Bansal #endif 108039c13c20SShubham Bansal } 108139c13c20SShubham Bansal 108239c13c20SShubham Bansal /* 0xabcdefgh => 0xghefcdab */ 108339c13c20SShubham Bansal static inline void emit_rev32(const u8 rd, const u8 rn, struct jit_ctx *ctx) 108439c13c20SShubham Bansal { 108539c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6 10861c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 108739c13c20SShubham Bansal 108839c13c20SShubham Bansal emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx); 108939c13c20SShubham Bansal emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 24), ctx); 109039c13c20SShubham Bansal emit(ARM_ORR_SI(ARM_IP, tmp2[0], tmp2[1], SRTYPE_LSL, 24), ctx); 109139c13c20SShubham Bansal 109239c13c20SShubham Bansal emit(ARM_MOV_SI(tmp2[1], rn, SRTYPE_LSR, 8), ctx); 109339c13c20SShubham Bansal emit(ARM_AND_I(tmp2[1], tmp2[1], 0xff), ctx); 109439c13c20SShubham Bansal emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 16), ctx); 109539c13c20SShubham Bansal emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx); 109639c13c20SShubham Bansal emit(ARM_MOV_SI(tmp2[0], tmp2[0], SRTYPE_LSL, 8), ctx); 109739c13c20SShubham Bansal emit(ARM_ORR_SI(tmp2[0], tmp2[0], tmp2[1], SRTYPE_LSL, 16), ctx); 109839c13c20SShubham Bansal emit(ARM_ORR_R(rd, ARM_IP, tmp2[0]), ctx); 109939c13c20SShubham Bansal 110039c13c20SShubham Bansal #else /* ARMv6+ */ 110139c13c20SShubham Bansal emit(ARM_REV(rd, rn), ctx); 110239c13c20SShubham Bansal #endif 110339c13c20SShubham Bansal } 110439c13c20SShubham Bansal 110539c13c20SShubham Bansal // push the scratch stack register on top of the stack 11061c35ba12SRussell King static inline void emit_push_r64(const s8 src[], const u8 shift, 110739c13c20SShubham Bansal struct jit_ctx *ctx) 110839c13c20SShubham Bansal { 11091c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 111039c13c20SShubham Bansal u16 reg_set = 0; 111139c13c20SShubham Bansal 111239c13c20SShubham Bansal emit(ARM_LDR_I(tmp2[1], ARM_SP, STACK_VAR(src[1]+shift)), ctx); 111339c13c20SShubham Bansal emit(ARM_LDR_I(tmp2[0], ARM_SP, STACK_VAR(src[0]+shift)), ctx); 111439c13c20SShubham Bansal 111539c13c20SShubham Bansal reg_set = (1 << tmp2[1]) | (1 << tmp2[0]); 111639c13c20SShubham Bansal emit(ARM_PUSH(reg_set), ctx); 111739c13c20SShubham Bansal } 111839c13c20SShubham Bansal 111939c13c20SShubham Bansal static void build_prologue(struct jit_ctx *ctx) 112039c13c20SShubham Bansal { 11211c35ba12SRussell King const s8 r0 = bpf2a32[BPF_REG_0][1]; 11221c35ba12SRussell King const s8 r2 = bpf2a32[BPF_REG_1][1]; 11231c35ba12SRussell King const s8 r3 = bpf2a32[BPF_REG_1][0]; 11241c35ba12SRussell King const s8 r4 = bpf2a32[BPF_REG_6][1]; 11251c35ba12SRussell King const s8 fplo = bpf2a32[BPF_REG_FP][1]; 11261c35ba12SRussell King const s8 fphi = bpf2a32[BPF_REG_FP][0]; 11271c35ba12SRussell King const s8 *tcc = bpf2a32[TCALL_CNT]; 112839c13c20SShubham Bansal 112939c13c20SShubham Bansal /* Save callee saved registers. */ 113039c13c20SShubham Bansal #ifdef CONFIG_FRAME_POINTER 113102088d9bSRussell King u16 reg_set = CALLEE_PUSH_MASK | 1 << ARM_IP | 1 << ARM_PC; 113202088d9bSRussell King emit(ARM_MOV_R(ARM_IP, ARM_SP), ctx); 113339c13c20SShubham Bansal emit(ARM_PUSH(reg_set), ctx); 113439c13c20SShubham Bansal emit(ARM_SUB_I(ARM_FP, ARM_IP, 4), ctx); 113539c13c20SShubham Bansal #else 113602088d9bSRussell King emit(ARM_PUSH(CALLEE_PUSH_MASK), ctx); 113702088d9bSRussell King emit(ARM_MOV_R(ARM_FP, ARM_SP), ctx); 113839c13c20SShubham Bansal #endif 113939c13c20SShubham Bansal /* Save frame pointer for later */ 114002088d9bSRussell King emit(ARM_SUB_I(ARM_IP, ARM_SP, SCRATCH_SIZE), ctx); 114139c13c20SShubham Bansal 114239c13c20SShubham Bansal ctx->stack_size = imm8m(STACK_SIZE); 114339c13c20SShubham Bansal 114439c13c20SShubham Bansal /* Set up function call stack */ 114539c13c20SShubham Bansal emit(ARM_SUB_I(ARM_SP, ARM_SP, ctx->stack_size), ctx); 114639c13c20SShubham Bansal 114739c13c20SShubham Bansal /* Set up BPF prog stack base register */ 114847b9c3bfSRussell King emit_a32_mov_r(fplo, ARM_IP, ctx); 114947b9c3bfSRussell King emit_a32_mov_i(fphi, 0, ctx); 115039c13c20SShubham Bansal 115139c13c20SShubham Bansal /* mov r4, 0 */ 115239c13c20SShubham Bansal emit(ARM_MOV_I(r4, 0), ctx); 115339c13c20SShubham Bansal 115439c13c20SShubham Bansal /* Move BPF_CTX to BPF_R1 */ 115539c13c20SShubham Bansal emit(ARM_MOV_R(r3, r4), ctx); 115639c13c20SShubham Bansal emit(ARM_MOV_R(r2, r0), ctx); 115739c13c20SShubham Bansal /* Initialize Tail Count */ 115839c13c20SShubham Bansal emit(ARM_STR_I(r4, ARM_SP, STACK_VAR(tcc[0])), ctx); 115939c13c20SShubham Bansal emit(ARM_STR_I(r4, ARM_SP, STACK_VAR(tcc[1])), ctx); 116039c13c20SShubham Bansal /* end of prologue */ 116139c13c20SShubham Bansal } 116239c13c20SShubham Bansal 116302088d9bSRussell King /* restore callee saved registers. */ 116439c13c20SShubham Bansal static void build_epilogue(struct jit_ctx *ctx) 116539c13c20SShubham Bansal { 116639c13c20SShubham Bansal #ifdef CONFIG_FRAME_POINTER 116702088d9bSRussell King /* When using frame pointers, some additional registers need to 116802088d9bSRussell King * be loaded. */ 116902088d9bSRussell King u16 reg_set = CALLEE_POP_MASK | 1 << ARM_SP; 117002088d9bSRussell King emit(ARM_SUB_I(ARM_SP, ARM_FP, hweight16(reg_set) * 4), ctx); 117139c13c20SShubham Bansal emit(ARM_LDM(ARM_SP, reg_set), ctx); 117239c13c20SShubham Bansal #else 117339c13c20SShubham Bansal /* Restore callee saved registers. */ 117402088d9bSRussell King emit(ARM_MOV_R(ARM_SP, ARM_FP), ctx); 117502088d9bSRussell King emit(ARM_POP(CALLEE_POP_MASK), ctx); 117639c13c20SShubham Bansal #endif 117739c13c20SShubham Bansal } 117839c13c20SShubham Bansal 117939c13c20SShubham Bansal /* 118039c13c20SShubham Bansal * Convert an eBPF instruction to native instruction, i.e 118139c13c20SShubham Bansal * JITs an eBPF instruction. 118239c13c20SShubham Bansal * Returns : 118339c13c20SShubham Bansal * 0 - Successfully JITed an 8-byte eBPF instruction 118439c13c20SShubham Bansal * >0 - Successfully JITed a 16-byte eBPF instruction 118539c13c20SShubham Bansal * <0 - Failed to JIT. 118639c13c20SShubham Bansal */ 118739c13c20SShubham Bansal static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx) 118839c13c20SShubham Bansal { 118939c13c20SShubham Bansal const u8 code = insn->code; 11901c35ba12SRussell King const s8 *dst = bpf2a32[insn->dst_reg]; 11911c35ba12SRussell King const s8 *src = bpf2a32[insn->src_reg]; 11921c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 11931c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 119439c13c20SShubham Bansal const s16 off = insn->off; 119539c13c20SShubham Bansal const s32 imm = insn->imm; 119639c13c20SShubham Bansal const int i = insn - ctx->prog->insnsi; 119739c13c20SShubham Bansal const bool is64 = BPF_CLASS(code) == BPF_ALU64; 1198*a6eccac5SRussell King const s8 *rd, *rs; 1199*a6eccac5SRussell King s8 rd_lo, rt, rm, rn; 120039c13c20SShubham Bansal s32 jmp_offset; 120139c13c20SShubham Bansal 120239c13c20SShubham Bansal #define check_imm(bits, imm) do { \ 12032b589a7eSWang YanQing if ((imm) >= (1 << ((bits) - 1)) || \ 12042b589a7eSWang YanQing (imm) < -(1 << ((bits) - 1))) { \ 120539c13c20SShubham Bansal pr_info("[%2d] imm=%d(0x%x) out of range\n", \ 120639c13c20SShubham Bansal i, imm, imm); \ 120739c13c20SShubham Bansal return -EINVAL; \ 120839c13c20SShubham Bansal } \ 120939c13c20SShubham Bansal } while (0) 121039c13c20SShubham Bansal #define check_imm24(imm) check_imm(24, imm) 1211ddecdfceSMircea Gherzan 121234805931SDaniel Borkmann switch (code) { 121339c13c20SShubham Bansal /* ALU operations */ 1214ddecdfceSMircea Gherzan 121539c13c20SShubham Bansal /* dst = src */ 121639c13c20SShubham Bansal case BPF_ALU | BPF_MOV | BPF_K: 121739c13c20SShubham Bansal case BPF_ALU | BPF_MOV | BPF_X: 121839c13c20SShubham Bansal case BPF_ALU64 | BPF_MOV | BPF_K: 121939c13c20SShubham Bansal case BPF_ALU64 | BPF_MOV | BPF_X: 122039c13c20SShubham Bansal switch (BPF_SRC(code)) { 122139c13c20SShubham Bansal case BPF_X: 122247b9c3bfSRussell King emit_a32_mov_r64(is64, dst, src, ctx); 122339c13c20SShubham Bansal break; 122439c13c20SShubham Bansal case BPF_K: 122539c13c20SShubham Bansal /* Sign-extend immediate value to destination reg */ 122647b9c3bfSRussell King emit_a32_mov_i64(is64, dst, imm, ctx); 122739c13c20SShubham Bansal break; 1228ddecdfceSMircea Gherzan } 1229ddecdfceSMircea Gherzan break; 123039c13c20SShubham Bansal /* dst = dst + src/imm */ 123139c13c20SShubham Bansal /* dst = dst - src/imm */ 123239c13c20SShubham Bansal /* dst = dst | src/imm */ 123339c13c20SShubham Bansal /* dst = dst & src/imm */ 123439c13c20SShubham Bansal /* dst = dst ^ src/imm */ 123539c13c20SShubham Bansal /* dst = dst * src/imm */ 123639c13c20SShubham Bansal /* dst = dst << src */ 123739c13c20SShubham Bansal /* dst = dst >> src */ 123834805931SDaniel Borkmann case BPF_ALU | BPF_ADD | BPF_K: 123934805931SDaniel Borkmann case BPF_ALU | BPF_ADD | BPF_X: 124034805931SDaniel Borkmann case BPF_ALU | BPF_SUB | BPF_K: 124134805931SDaniel Borkmann case BPF_ALU | BPF_SUB | BPF_X: 124234805931SDaniel Borkmann case BPF_ALU | BPF_OR | BPF_K: 124334805931SDaniel Borkmann case BPF_ALU | BPF_OR | BPF_X: 124434805931SDaniel Borkmann case BPF_ALU | BPF_AND | BPF_K: 124534805931SDaniel Borkmann case BPF_ALU | BPF_AND | BPF_X: 124639c13c20SShubham Bansal case BPF_ALU | BPF_XOR | BPF_K: 124739c13c20SShubham Bansal case BPF_ALU | BPF_XOR | BPF_X: 124839c13c20SShubham Bansal case BPF_ALU | BPF_MUL | BPF_K: 124939c13c20SShubham Bansal case BPF_ALU | BPF_MUL | BPF_X: 125034805931SDaniel Borkmann case BPF_ALU | BPF_LSH | BPF_X: 125134805931SDaniel Borkmann case BPF_ALU | BPF_RSH | BPF_X: 125239c13c20SShubham Bansal case BPF_ALU | BPF_ARSH | BPF_K: 125339c13c20SShubham Bansal case BPF_ALU | BPF_ARSH | BPF_X: 125439c13c20SShubham Bansal case BPF_ALU64 | BPF_ADD | BPF_K: 125539c13c20SShubham Bansal case BPF_ALU64 | BPF_ADD | BPF_X: 125639c13c20SShubham Bansal case BPF_ALU64 | BPF_SUB | BPF_K: 125739c13c20SShubham Bansal case BPF_ALU64 | BPF_SUB | BPF_X: 125839c13c20SShubham Bansal case BPF_ALU64 | BPF_OR | BPF_K: 125939c13c20SShubham Bansal case BPF_ALU64 | BPF_OR | BPF_X: 126039c13c20SShubham Bansal case BPF_ALU64 | BPF_AND | BPF_K: 126139c13c20SShubham Bansal case BPF_ALU64 | BPF_AND | BPF_X: 126239c13c20SShubham Bansal case BPF_ALU64 | BPF_XOR | BPF_K: 126339c13c20SShubham Bansal case BPF_ALU64 | BPF_XOR | BPF_X: 126439c13c20SShubham Bansal switch (BPF_SRC(code)) { 126539c13c20SShubham Bansal case BPF_X: 126647b9c3bfSRussell King emit_a32_alu_r64(is64, dst, src, ctx, BPF_OP(code)); 1267ddecdfceSMircea Gherzan break; 126839c13c20SShubham Bansal case BPF_K: 126939c13c20SShubham Bansal /* Move immediate value to the temporary register 127039c13c20SShubham Bansal * and then do the ALU operation on the temporary 127139c13c20SShubham Bansal * register as this will sign-extend the immediate 127239c13c20SShubham Bansal * value into temporary reg and then it would be 127339c13c20SShubham Bansal * safe to do the operation on it. 127439c13c20SShubham Bansal */ 127547b9c3bfSRussell King emit_a32_mov_i64(is64, tmp2, imm, ctx); 127647b9c3bfSRussell King emit_a32_alu_r64(is64, dst, tmp2, ctx, BPF_OP(code)); 127739c13c20SShubham Bansal break; 127839c13c20SShubham Bansal } 127939c13c20SShubham Bansal break; 128039c13c20SShubham Bansal /* dst = dst / src(imm) */ 128139c13c20SShubham Bansal /* dst = dst % src(imm) */ 128239c13c20SShubham Bansal case BPF_ALU | BPF_DIV | BPF_K: 128339c13c20SShubham Bansal case BPF_ALU | BPF_DIV | BPF_X: 128439c13c20SShubham Bansal case BPF_ALU | BPF_MOD | BPF_K: 128539c13c20SShubham Bansal case BPF_ALU | BPF_MOD | BPF_X: 1286*a6eccac5SRussell King rd_lo = arm_bpf_get_reg32(dst_lo, tmp2[1], ctx); 128739c13c20SShubham Bansal switch (BPF_SRC(code)) { 128839c13c20SShubham Bansal case BPF_X: 12897a987025SRussell King rt = arm_bpf_get_reg32(src_lo, tmp2[0], ctx); 129039c13c20SShubham Bansal break; 129139c13c20SShubham Bansal case BPF_K: 129239c13c20SShubham Bansal rt = tmp2[0]; 129347b9c3bfSRussell King emit_a32_mov_i(rt, imm, ctx); 129447b9c3bfSRussell King break; 129547b9c3bfSRussell King default: 129647b9c3bfSRussell King rt = src_lo; 129739c13c20SShubham Bansal break; 129839c13c20SShubham Bansal } 1299*a6eccac5SRussell King emit_udivmod(rd_lo, rd_lo, rt, ctx, BPF_OP(code)); 1300*a6eccac5SRussell King arm_bpf_put_reg32(dst_lo, rd_lo, ctx); 130147b9c3bfSRussell King emit_a32_mov_i(dst_hi, 0, ctx); 130239c13c20SShubham Bansal break; 130339c13c20SShubham Bansal case BPF_ALU64 | BPF_DIV | BPF_K: 130439c13c20SShubham Bansal case BPF_ALU64 | BPF_DIV | BPF_X: 130539c13c20SShubham Bansal case BPF_ALU64 | BPF_MOD | BPF_K: 130639c13c20SShubham Bansal case BPF_ALU64 | BPF_MOD | BPF_X: 130739c13c20SShubham Bansal goto notyet; 130839c13c20SShubham Bansal /* dst = dst >> imm */ 130939c13c20SShubham Bansal /* dst = dst << imm */ 131039c13c20SShubham Bansal case BPF_ALU | BPF_RSH | BPF_K: 131139c13c20SShubham Bansal case BPF_ALU | BPF_LSH | BPF_K: 131239c13c20SShubham Bansal if (unlikely(imm > 31)) 131339c13c20SShubham Bansal return -EINVAL; 131439c13c20SShubham Bansal if (imm) 131547b9c3bfSRussell King emit_a32_alu_i(dst_lo, imm, ctx, BPF_OP(code)); 131647b9c3bfSRussell King emit_a32_mov_i(dst_hi, 0, ctx); 131739c13c20SShubham Bansal break; 131839c13c20SShubham Bansal /* dst = dst << imm */ 131939c13c20SShubham Bansal case BPF_ALU64 | BPF_LSH | BPF_K: 132039c13c20SShubham Bansal if (unlikely(imm > 63)) 132139c13c20SShubham Bansal return -EINVAL; 132247b9c3bfSRussell King emit_a32_lsh_i64(dst, imm, ctx); 132339c13c20SShubham Bansal break; 132439c13c20SShubham Bansal /* dst = dst >> imm */ 132539c13c20SShubham Bansal case BPF_ALU64 | BPF_RSH | BPF_K: 132639c13c20SShubham Bansal if (unlikely(imm > 63)) 132739c13c20SShubham Bansal return -EINVAL; 132847b9c3bfSRussell King emit_a32_rsh_i64(dst, imm, ctx); 132939c13c20SShubham Bansal break; 133039c13c20SShubham Bansal /* dst = dst << src */ 133139c13c20SShubham Bansal case BPF_ALU64 | BPF_LSH | BPF_X: 133247b9c3bfSRussell King emit_a32_lsh_r64(dst, src, ctx); 133339c13c20SShubham Bansal break; 133439c13c20SShubham Bansal /* dst = dst >> src */ 133539c13c20SShubham Bansal case BPF_ALU64 | BPF_RSH | BPF_X: 133647b9c3bfSRussell King emit_a32_rsh_r64(dst, src, ctx); 133739c13c20SShubham Bansal break; 133839c13c20SShubham Bansal /* dst = dst >> src (signed) */ 133939c13c20SShubham Bansal case BPF_ALU64 | BPF_ARSH | BPF_X: 134047b9c3bfSRussell King emit_a32_arsh_r64(dst, src, ctx); 134139c13c20SShubham Bansal break; 134239c13c20SShubham Bansal /* dst = dst >> imm (signed) */ 134339c13c20SShubham Bansal case BPF_ALU64 | BPF_ARSH | BPF_K: 134439c13c20SShubham Bansal if (unlikely(imm > 63)) 134539c13c20SShubham Bansal return -EINVAL; 134647b9c3bfSRussell King emit_a32_arsh_i64(dst, imm, ctx); 134739c13c20SShubham Bansal break; 134839c13c20SShubham Bansal /* dst = ~dst */ 134934805931SDaniel Borkmann case BPF_ALU | BPF_NEG: 135047b9c3bfSRussell King emit_a32_alu_i(dst_lo, 0, ctx, BPF_OP(code)); 135147b9c3bfSRussell King emit_a32_mov_i(dst_hi, 0, ctx); 1352ddecdfceSMircea Gherzan break; 135339c13c20SShubham Bansal /* dst = ~dst (64 bit) */ 135439c13c20SShubham Bansal case BPF_ALU64 | BPF_NEG: 135547b9c3bfSRussell King emit_a32_neg64(dst, ctx); 1356ddecdfceSMircea Gherzan break; 135739c13c20SShubham Bansal /* dst = dst * src/imm */ 135839c13c20SShubham Bansal case BPF_ALU64 | BPF_MUL | BPF_X: 135939c13c20SShubham Bansal case BPF_ALU64 | BPF_MUL | BPF_K: 136039c13c20SShubham Bansal switch (BPF_SRC(code)) { 136139c13c20SShubham Bansal case BPF_X: 136247b9c3bfSRussell King emit_a32_mul_r64(dst, src, ctx); 1363ddecdfceSMircea Gherzan break; 136439c13c20SShubham Bansal case BPF_K: 136539c13c20SShubham Bansal /* Move immediate value to the temporary register 136639c13c20SShubham Bansal * and then do the multiplication on it as this 136739c13c20SShubham Bansal * will sign-extend the immediate value into temp 136839c13c20SShubham Bansal * reg then it would be safe to do the operation 136939c13c20SShubham Bansal * on it. 13705bf705b4SNicolas Schichan */ 137147b9c3bfSRussell King emit_a32_mov_i64(is64, tmp2, imm, ctx); 137247b9c3bfSRussell King emit_a32_mul_r64(dst, tmp2, ctx); 137339c13c20SShubham Bansal break; 13745bf705b4SNicolas Schichan } 1375ddecdfceSMircea Gherzan break; 137639c13c20SShubham Bansal /* dst = htole(dst) */ 137739c13c20SShubham Bansal /* dst = htobe(dst) */ 137839c13c20SShubham Bansal case BPF_ALU | BPF_END | BPF_FROM_LE: 137939c13c20SShubham Bansal case BPF_ALU | BPF_END | BPF_FROM_BE: 1380*a6eccac5SRussell King rd = arm_bpf_get_reg64(dst, tmp, ctx); 138139c13c20SShubham Bansal if (BPF_SRC(code) == BPF_FROM_LE) 138239c13c20SShubham Bansal goto emit_bswap_uxt; 138339c13c20SShubham Bansal switch (imm) { 138439c13c20SShubham Bansal case 16: 1385*a6eccac5SRussell King emit_rev16(rd[1], rd[1], ctx); 138639c13c20SShubham Bansal goto emit_bswap_uxt; 138739c13c20SShubham Bansal case 32: 1388*a6eccac5SRussell King emit_rev32(rd[1], rd[1], ctx); 138939c13c20SShubham Bansal goto emit_bswap_uxt; 139039c13c20SShubham Bansal case 64: 1391*a6eccac5SRussell King emit_rev32(ARM_LR, rd[1], ctx); 1392*a6eccac5SRussell King emit_rev32(rd[1], rd[0], ctx); 1393*a6eccac5SRussell King emit(ARM_MOV_R(rd[0], ARM_LR), ctx); 1394bf0098f2SDaniel Borkmann break; 139539c13c20SShubham Bansal } 139639c13c20SShubham Bansal goto exit; 139739c13c20SShubham Bansal emit_bswap_uxt: 139839c13c20SShubham Bansal switch (imm) { 139939c13c20SShubham Bansal case 16: 140039c13c20SShubham Bansal /* zero-extend 16 bits into 64 bits */ 140139c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6 140247b9c3bfSRussell King emit_a32_mov_i(tmp2[1], 0xffff, ctx); 1403*a6eccac5SRussell King emit(ARM_AND_R(rd[1], rd[1], tmp2[1]), ctx); 140439c13c20SShubham Bansal #else /* ARMv6+ */ 1405*a6eccac5SRussell King emit(ARM_UXTH(rd[1], rd[1]), ctx); 14061447f93fSNicolas Schichan #endif 1407*a6eccac5SRussell King emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx); 14081447f93fSNicolas Schichan break; 140939c13c20SShubham Bansal case 32: 141039c13c20SShubham Bansal /* zero-extend 32 bits into 64 bits */ 1411*a6eccac5SRussell King emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx); 1412ddecdfceSMircea Gherzan break; 141339c13c20SShubham Bansal case 64: 141439c13c20SShubham Bansal /* nop */ 141539c13c20SShubham Bansal break; 141639c13c20SShubham Bansal } 141739c13c20SShubham Bansal exit: 1418*a6eccac5SRussell King arm_bpf_put_reg64(dst, rd, ctx); 141939c13c20SShubham Bansal break; 142039c13c20SShubham Bansal /* dst = imm64 */ 142139c13c20SShubham Bansal case BPF_LD | BPF_IMM | BPF_DW: 142239c13c20SShubham Bansal { 142339c13c20SShubham Bansal const struct bpf_insn insn1 = insn[1]; 142439c13c20SShubham Bansal u32 hi, lo = imm; 1425303249abSNicolas Schichan 142639c13c20SShubham Bansal hi = insn1.imm; 142747b9c3bfSRussell King emit_a32_mov_i(dst_lo, lo, ctx); 142847b9c3bfSRussell King emit_a32_mov_i(dst_hi, hi, ctx); 142939c13c20SShubham Bansal 143039c13c20SShubham Bansal return 1; 143139c13c20SShubham Bansal } 143239c13c20SShubham Bansal /* LDX: dst = *(size *)(src + off) */ 143339c13c20SShubham Bansal case BPF_LDX | BPF_MEM | BPF_W: 143439c13c20SShubham Bansal case BPF_LDX | BPF_MEM | BPF_H: 143539c13c20SShubham Bansal case BPF_LDX | BPF_MEM | BPF_B: 143639c13c20SShubham Bansal case BPF_LDX | BPF_MEM | BPF_DW: 14377a987025SRussell King rn = arm_bpf_get_reg32(src_lo, tmp2[1], ctx); 143847b9c3bfSRussell King emit_ldx_r(dst, rn, off, ctx, BPF_SIZE(code)); 143939c13c20SShubham Bansal break; 144039c13c20SShubham Bansal /* ST: *(size *)(dst + off) = imm */ 144139c13c20SShubham Bansal case BPF_ST | BPF_MEM | BPF_W: 144239c13c20SShubham Bansal case BPF_ST | BPF_MEM | BPF_H: 144339c13c20SShubham Bansal case BPF_ST | BPF_MEM | BPF_B: 144439c13c20SShubham Bansal case BPF_ST | BPF_MEM | BPF_DW: 144539c13c20SShubham Bansal switch (BPF_SIZE(code)) { 144639c13c20SShubham Bansal case BPF_DW: 144739c13c20SShubham Bansal /* Sign-extend immediate value into temp reg */ 144847b9c3bfSRussell King emit_a32_mov_i64(true, tmp2, imm, ctx); 144947b9c3bfSRussell King emit_str_r(dst_lo, tmp2[1], off, ctx, BPF_W); 145047b9c3bfSRussell King emit_str_r(dst_lo, tmp2[0], off+4, ctx, BPF_W); 145139c13c20SShubham Bansal break; 145239c13c20SShubham Bansal case BPF_W: 145339c13c20SShubham Bansal case BPF_H: 145439c13c20SShubham Bansal case BPF_B: 145547b9c3bfSRussell King emit_a32_mov_i(tmp2[1], imm, ctx); 145647b9c3bfSRussell King emit_str_r(dst_lo, tmp2[1], off, ctx, BPF_SIZE(code)); 145739c13c20SShubham Bansal break; 145839c13c20SShubham Bansal } 145939c13c20SShubham Bansal break; 146039c13c20SShubham Bansal /* STX XADD: lock *(u32 *)(dst + off) += src */ 146139c13c20SShubham Bansal case BPF_STX | BPF_XADD | BPF_W: 146239c13c20SShubham Bansal /* STX XADD: lock *(u64 *)(dst + off) += src */ 146339c13c20SShubham Bansal case BPF_STX | BPF_XADD | BPF_DW: 146439c13c20SShubham Bansal goto notyet; 146539c13c20SShubham Bansal /* STX: *(size *)(dst + off) = src */ 146639c13c20SShubham Bansal case BPF_STX | BPF_MEM | BPF_W: 146739c13c20SShubham Bansal case BPF_STX | BPF_MEM | BPF_H: 146839c13c20SShubham Bansal case BPF_STX | BPF_MEM | BPF_B: 146939c13c20SShubham Bansal case BPF_STX | BPF_MEM | BPF_DW: 147039c13c20SShubham Bansal { 147139c13c20SShubham Bansal u8 sz = BPF_SIZE(code); 147239c13c20SShubham Bansal 1473*a6eccac5SRussell King rs = arm_bpf_get_reg64(src, tmp2, ctx); 147439c13c20SShubham Bansal 147539c13c20SShubham Bansal /* Store the value */ 147639c13c20SShubham Bansal if (BPF_SIZE(code) == BPF_DW) { 1477*a6eccac5SRussell King emit_str_r(dst_lo, rs[1], off, ctx, BPF_W); 1478*a6eccac5SRussell King emit_str_r(dst_lo, rs[0], off+4, ctx, BPF_W); 147939c13c20SShubham Bansal } else { 1480*a6eccac5SRussell King emit_str_r(dst_lo, rs[1], off, ctx, sz); 148139c13c20SShubham Bansal } 148239c13c20SShubham Bansal break; 148339c13c20SShubham Bansal } 148439c13c20SShubham Bansal /* PC += off if dst == src */ 148539c13c20SShubham Bansal /* PC += off if dst > src */ 148639c13c20SShubham Bansal /* PC += off if dst >= src */ 148739c13c20SShubham Bansal /* PC += off if dst < src */ 148839c13c20SShubham Bansal /* PC += off if dst <= src */ 148939c13c20SShubham Bansal /* PC += off if dst != src */ 149039c13c20SShubham Bansal /* PC += off if dst > src (signed) */ 149139c13c20SShubham Bansal /* PC += off if dst >= src (signed) */ 149239c13c20SShubham Bansal /* PC += off if dst < src (signed) */ 149339c13c20SShubham Bansal /* PC += off if dst <= src (signed) */ 149439c13c20SShubham Bansal /* PC += off if dst & src */ 149539c13c20SShubham Bansal case BPF_JMP | BPF_JEQ | BPF_X: 149639c13c20SShubham Bansal case BPF_JMP | BPF_JGT | BPF_X: 149739c13c20SShubham Bansal case BPF_JMP | BPF_JGE | BPF_X: 149839c13c20SShubham Bansal case BPF_JMP | BPF_JNE | BPF_X: 149939c13c20SShubham Bansal case BPF_JMP | BPF_JSGT | BPF_X: 150039c13c20SShubham Bansal case BPF_JMP | BPF_JSGE | BPF_X: 150139c13c20SShubham Bansal case BPF_JMP | BPF_JSET | BPF_X: 150239c13c20SShubham Bansal case BPF_JMP | BPF_JLE | BPF_X: 150339c13c20SShubham Bansal case BPF_JMP | BPF_JLT | BPF_X: 150439c13c20SShubham Bansal case BPF_JMP | BPF_JSLT | BPF_X: 150539c13c20SShubham Bansal case BPF_JMP | BPF_JSLE | BPF_X: 150639c13c20SShubham Bansal /* Setup source registers */ 15077a987025SRussell King rm = arm_bpf_get_reg32(src_hi, tmp2[0], ctx); 15087a987025SRussell King rn = arm_bpf_get_reg32(src_lo, tmp2[1], ctx); 150939c13c20SShubham Bansal goto go_jmp; 151039c13c20SShubham Bansal /* PC += off if dst == imm */ 151139c13c20SShubham Bansal /* PC += off if dst > imm */ 151239c13c20SShubham Bansal /* PC += off if dst >= imm */ 151339c13c20SShubham Bansal /* PC += off if dst < imm */ 151439c13c20SShubham Bansal /* PC += off if dst <= imm */ 151539c13c20SShubham Bansal /* PC += off if dst != imm */ 151639c13c20SShubham Bansal /* PC += off if dst > imm (signed) */ 151739c13c20SShubham Bansal /* PC += off if dst >= imm (signed) */ 151839c13c20SShubham Bansal /* PC += off if dst < imm (signed) */ 151939c13c20SShubham Bansal /* PC += off if dst <= imm (signed) */ 152039c13c20SShubham Bansal /* PC += off if dst & imm */ 152139c13c20SShubham Bansal case BPF_JMP | BPF_JEQ | BPF_K: 152239c13c20SShubham Bansal case BPF_JMP | BPF_JGT | BPF_K: 152339c13c20SShubham Bansal case BPF_JMP | BPF_JGE | BPF_K: 152439c13c20SShubham Bansal case BPF_JMP | BPF_JNE | BPF_K: 152539c13c20SShubham Bansal case BPF_JMP | BPF_JSGT | BPF_K: 152639c13c20SShubham Bansal case BPF_JMP | BPF_JSGE | BPF_K: 152739c13c20SShubham Bansal case BPF_JMP | BPF_JSET | BPF_K: 152839c13c20SShubham Bansal case BPF_JMP | BPF_JLT | BPF_K: 152939c13c20SShubham Bansal case BPF_JMP | BPF_JLE | BPF_K: 153039c13c20SShubham Bansal case BPF_JMP | BPF_JSLT | BPF_K: 153139c13c20SShubham Bansal case BPF_JMP | BPF_JSLE | BPF_K: 153239c13c20SShubham Bansal if (off == 0) 153339c13c20SShubham Bansal break; 153439c13c20SShubham Bansal rm = tmp2[0]; 153539c13c20SShubham Bansal rn = tmp2[1]; 153639c13c20SShubham Bansal /* Sign-extend immediate value */ 153747b9c3bfSRussell King emit_a32_mov_i64(true, tmp2, imm, ctx); 153839c13c20SShubham Bansal go_jmp: 153939c13c20SShubham Bansal /* Setup destination register */ 1540*a6eccac5SRussell King rd = arm_bpf_get_reg64(dst, tmp, ctx); 154139c13c20SShubham Bansal 154239c13c20SShubham Bansal /* Check for the condition */ 1543*a6eccac5SRussell King emit_ar_r(rd[0], rd[1], rm, rn, ctx, BPF_OP(code)); 154439c13c20SShubham Bansal 154539c13c20SShubham Bansal /* Setup JUMP instruction */ 154639c13c20SShubham Bansal jmp_offset = bpf2a32_offset(i+off, i, ctx); 154739c13c20SShubham Bansal switch (BPF_OP(code)) { 154839c13c20SShubham Bansal case BPF_JNE: 154939c13c20SShubham Bansal case BPF_JSET: 155039c13c20SShubham Bansal _emit(ARM_COND_NE, ARM_B(jmp_offset), ctx); 155139c13c20SShubham Bansal break; 155239c13c20SShubham Bansal case BPF_JEQ: 155339c13c20SShubham Bansal _emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx); 155439c13c20SShubham Bansal break; 155539c13c20SShubham Bansal case BPF_JGT: 155639c13c20SShubham Bansal _emit(ARM_COND_HI, ARM_B(jmp_offset), ctx); 155739c13c20SShubham Bansal break; 155839c13c20SShubham Bansal case BPF_JGE: 155939c13c20SShubham Bansal _emit(ARM_COND_CS, ARM_B(jmp_offset), ctx); 156039c13c20SShubham Bansal break; 156139c13c20SShubham Bansal case BPF_JSGT: 156239c13c20SShubham Bansal _emit(ARM_COND_LT, ARM_B(jmp_offset), ctx); 156339c13c20SShubham Bansal break; 156439c13c20SShubham Bansal case BPF_JSGE: 156539c13c20SShubham Bansal _emit(ARM_COND_GE, ARM_B(jmp_offset), ctx); 156639c13c20SShubham Bansal break; 156739c13c20SShubham Bansal case BPF_JLE: 156839c13c20SShubham Bansal _emit(ARM_COND_LS, ARM_B(jmp_offset), ctx); 156939c13c20SShubham Bansal break; 157039c13c20SShubham Bansal case BPF_JLT: 157139c13c20SShubham Bansal _emit(ARM_COND_CC, ARM_B(jmp_offset), ctx); 157239c13c20SShubham Bansal break; 157339c13c20SShubham Bansal case BPF_JSLT: 157439c13c20SShubham Bansal _emit(ARM_COND_LT, ARM_B(jmp_offset), ctx); 157539c13c20SShubham Bansal break; 157639c13c20SShubham Bansal case BPF_JSLE: 157739c13c20SShubham Bansal _emit(ARM_COND_GE, ARM_B(jmp_offset), ctx); 157839c13c20SShubham Bansal break; 157939c13c20SShubham Bansal } 158039c13c20SShubham Bansal break; 158139c13c20SShubham Bansal /* JMP OFF */ 158239c13c20SShubham Bansal case BPF_JMP | BPF_JA: 158339c13c20SShubham Bansal { 158439c13c20SShubham Bansal if (off == 0) 158539c13c20SShubham Bansal break; 158639c13c20SShubham Bansal jmp_offset = bpf2a32_offset(i+off, i, ctx); 158739c13c20SShubham Bansal check_imm24(jmp_offset); 158839c13c20SShubham Bansal emit(ARM_B(jmp_offset), ctx); 158939c13c20SShubham Bansal break; 159039c13c20SShubham Bansal } 159139c13c20SShubham Bansal /* tail call */ 159239c13c20SShubham Bansal case BPF_JMP | BPF_TAIL_CALL: 159339c13c20SShubham Bansal if (emit_bpf_tail_call(ctx)) 159439c13c20SShubham Bansal return -EFAULT; 159539c13c20SShubham Bansal break; 159639c13c20SShubham Bansal /* function call */ 159739c13c20SShubham Bansal case BPF_JMP | BPF_CALL: 159839c13c20SShubham Bansal { 15991c35ba12SRussell King const s8 *r0 = bpf2a32[BPF_REG_0]; 16001c35ba12SRussell King const s8 *r1 = bpf2a32[BPF_REG_1]; 16011c35ba12SRussell King const s8 *r2 = bpf2a32[BPF_REG_2]; 16021c35ba12SRussell King const s8 *r3 = bpf2a32[BPF_REG_3]; 16031c35ba12SRussell King const s8 *r4 = bpf2a32[BPF_REG_4]; 16041c35ba12SRussell King const s8 *r5 = bpf2a32[BPF_REG_5]; 160539c13c20SShubham Bansal const u32 func = (u32)__bpf_call_base + (u32)imm; 160639c13c20SShubham Bansal 160747b9c3bfSRussell King emit_a32_mov_r64(true, r0, r1, ctx); 160847b9c3bfSRussell King emit_a32_mov_r64(true, r1, r2, ctx); 160939c13c20SShubham Bansal emit_push_r64(r5, 0, ctx); 161039c13c20SShubham Bansal emit_push_r64(r4, 8, ctx); 161139c13c20SShubham Bansal emit_push_r64(r3, 16, ctx); 161239c13c20SShubham Bansal 161347b9c3bfSRussell King emit_a32_mov_i(tmp[1], func, ctx); 161439c13c20SShubham Bansal emit_blx_r(tmp[1], ctx); 161539c13c20SShubham Bansal 161639c13c20SShubham Bansal emit(ARM_ADD_I(ARM_SP, ARM_SP, imm8m(24)), ctx); // callee clean 161739c13c20SShubham Bansal break; 161839c13c20SShubham Bansal } 161939c13c20SShubham Bansal /* function return */ 162039c13c20SShubham Bansal case BPF_JMP | BPF_EXIT: 162139c13c20SShubham Bansal /* Optimization: when last instruction is EXIT 162239c13c20SShubham Bansal * simply fallthrough to epilogue. 162339c13c20SShubham Bansal */ 162439c13c20SShubham Bansal if (i == ctx->prog->len - 1) 162539c13c20SShubham Bansal break; 162639c13c20SShubham Bansal jmp_offset = epilogue_offset(ctx); 162739c13c20SShubham Bansal check_imm24(jmp_offset); 162839c13c20SShubham Bansal emit(ARM_B(jmp_offset), ctx); 162939c13c20SShubham Bansal break; 163039c13c20SShubham Bansal notyet: 163139c13c20SShubham Bansal pr_info_once("*** NOT YET: opcode %02x ***\n", code); 163239c13c20SShubham Bansal return -EFAULT; 163339c13c20SShubham Bansal default: 163439c13c20SShubham Bansal pr_err_once("unknown opcode %02x\n", code); 163539c13c20SShubham Bansal return -EINVAL; 1636ddecdfceSMircea Gherzan } 16370b59d880SNicolas Schichan 16380b59d880SNicolas Schichan if (ctx->flags & FLAG_IMM_OVERFLOW) 16390b59d880SNicolas Schichan /* 16400b59d880SNicolas Schichan * this instruction generated an overflow when 16410b59d880SNicolas Schichan * trying to access the literal pool, so 16420b59d880SNicolas Schichan * delegate this filter to the kernel interpreter. 16430b59d880SNicolas Schichan */ 16440b59d880SNicolas Schichan return -1; 164539c13c20SShubham Bansal return 0; 1646ddecdfceSMircea Gherzan } 1647ddecdfceSMircea Gherzan 164839c13c20SShubham Bansal static int build_body(struct jit_ctx *ctx) 164939c13c20SShubham Bansal { 165039c13c20SShubham Bansal const struct bpf_prog *prog = ctx->prog; 165139c13c20SShubham Bansal unsigned int i; 165239c13c20SShubham Bansal 165339c13c20SShubham Bansal for (i = 0; i < prog->len; i++) { 165439c13c20SShubham Bansal const struct bpf_insn *insn = &(prog->insnsi[i]); 165539c13c20SShubham Bansal int ret; 165639c13c20SShubham Bansal 165739c13c20SShubham Bansal ret = build_insn(insn, ctx); 165839c13c20SShubham Bansal 165939c13c20SShubham Bansal /* It's used with loading the 64 bit immediate value. */ 166039c13c20SShubham Bansal if (ret > 0) { 166139c13c20SShubham Bansal i++; 1662ddecdfceSMircea Gherzan if (ctx->target == NULL) 166339c13c20SShubham Bansal ctx->offsets[i] = ctx->idx; 166439c13c20SShubham Bansal continue; 166539c13c20SShubham Bansal } 166639c13c20SShubham Bansal 166739c13c20SShubham Bansal if (ctx->target == NULL) 166839c13c20SShubham Bansal ctx->offsets[i] = ctx->idx; 166939c13c20SShubham Bansal 167039c13c20SShubham Bansal /* If unsuccesfull, return with error code */ 167139c13c20SShubham Bansal if (ret) 167239c13c20SShubham Bansal return ret; 167339c13c20SShubham Bansal } 167439c13c20SShubham Bansal return 0; 167539c13c20SShubham Bansal } 167639c13c20SShubham Bansal 167739c13c20SShubham Bansal static int validate_code(struct jit_ctx *ctx) 167839c13c20SShubham Bansal { 167939c13c20SShubham Bansal int i; 168039c13c20SShubham Bansal 168139c13c20SShubham Bansal for (i = 0; i < ctx->idx; i++) { 168239c13c20SShubham Bansal if (ctx->target[i] == __opcode_to_mem_arm(ARM_INST_UDF)) 168339c13c20SShubham Bansal return -1; 168439c13c20SShubham Bansal } 1685ddecdfceSMircea Gherzan 1686ddecdfceSMircea Gherzan return 0; 1687ddecdfceSMircea Gherzan } 1688ddecdfceSMircea Gherzan 168939c13c20SShubham Bansal void bpf_jit_compile(struct bpf_prog *prog) 1690ddecdfceSMircea Gherzan { 169139c13c20SShubham Bansal /* Nothing to do here. We support Internal BPF. */ 169239c13c20SShubham Bansal } 1693ddecdfceSMircea Gherzan 169439c13c20SShubham Bansal struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) 169539c13c20SShubham Bansal { 169639c13c20SShubham Bansal struct bpf_prog *tmp, *orig_prog = prog; 169739c13c20SShubham Bansal struct bpf_binary_header *header; 169839c13c20SShubham Bansal bool tmp_blinded = false; 169939c13c20SShubham Bansal struct jit_ctx ctx; 170039c13c20SShubham Bansal unsigned int tmp_idx; 170139c13c20SShubham Bansal unsigned int image_size; 170239c13c20SShubham Bansal u8 *image_ptr; 170339c13c20SShubham Bansal 170439c13c20SShubham Bansal /* If BPF JIT was not enabled then we must fall back to 170539c13c20SShubham Bansal * the interpreter. 170639c13c20SShubham Bansal */ 170760b58afcSAlexei Starovoitov if (!prog->jit_requested) 170839c13c20SShubham Bansal return orig_prog; 170939c13c20SShubham Bansal 171039c13c20SShubham Bansal /* If constant blinding was enabled and we failed during blinding 171139c13c20SShubham Bansal * then we must fall back to the interpreter. Otherwise, we save 171239c13c20SShubham Bansal * the new JITed code. 171339c13c20SShubham Bansal */ 171439c13c20SShubham Bansal tmp = bpf_jit_blind_constants(prog); 171539c13c20SShubham Bansal 171639c13c20SShubham Bansal if (IS_ERR(tmp)) 171739c13c20SShubham Bansal return orig_prog; 171839c13c20SShubham Bansal if (tmp != prog) { 171939c13c20SShubham Bansal tmp_blinded = true; 172039c13c20SShubham Bansal prog = tmp; 172139c13c20SShubham Bansal } 1722ddecdfceSMircea Gherzan 1723ddecdfceSMircea Gherzan memset(&ctx, 0, sizeof(ctx)); 172439c13c20SShubham Bansal ctx.prog = prog; 1725ddecdfceSMircea Gherzan 172639c13c20SShubham Bansal /* Not able to allocate memory for offsets[] , then 172739c13c20SShubham Bansal * we must fall back to the interpreter 172839c13c20SShubham Bansal */ 172939c13c20SShubham Bansal ctx.offsets = kcalloc(prog->len, sizeof(int), GFP_KERNEL); 173039c13c20SShubham Bansal if (ctx.offsets == NULL) { 173139c13c20SShubham Bansal prog = orig_prog; 1732ddecdfceSMircea Gherzan goto out; 173339c13c20SShubham Bansal } 173439c13c20SShubham Bansal 173539c13c20SShubham Bansal /* 1) fake pass to find in the length of the JITed code, 173639c13c20SShubham Bansal * to compute ctx->offsets and other context variables 173739c13c20SShubham Bansal * needed to compute final JITed code. 173839c13c20SShubham Bansal * Also, calculate random starting pointer/start of JITed code 173939c13c20SShubham Bansal * which is prefixed by random number of fault instructions. 174039c13c20SShubham Bansal * 174139c13c20SShubham Bansal * If the first pass fails then there is no chance of it 174239c13c20SShubham Bansal * being successful in the second pass, so just fall back 174339c13c20SShubham Bansal * to the interpreter. 174439c13c20SShubham Bansal */ 174539c13c20SShubham Bansal if (build_body(&ctx)) { 174639c13c20SShubham Bansal prog = orig_prog; 174739c13c20SShubham Bansal goto out_off; 174839c13c20SShubham Bansal } 1749ddecdfceSMircea Gherzan 1750ddecdfceSMircea Gherzan tmp_idx = ctx.idx; 1751ddecdfceSMircea Gherzan build_prologue(&ctx); 1752ddecdfceSMircea Gherzan ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4; 1753ddecdfceSMircea Gherzan 175439c13c20SShubham Bansal ctx.epilogue_offset = ctx.idx; 175539c13c20SShubham Bansal 1756ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7 1757ddecdfceSMircea Gherzan tmp_idx = ctx.idx; 1758ddecdfceSMircea Gherzan build_epilogue(&ctx); 1759ddecdfceSMircea Gherzan ctx.epilogue_bytes = (ctx.idx - tmp_idx) * 4; 1760ddecdfceSMircea Gherzan 1761ddecdfceSMircea Gherzan ctx.idx += ctx.imm_count; 1762ddecdfceSMircea Gherzan if (ctx.imm_count) { 176339c13c20SShubham Bansal ctx.imms = kcalloc(ctx.imm_count, sizeof(u32), GFP_KERNEL); 176439c13c20SShubham Bansal if (ctx.imms == NULL) { 176539c13c20SShubham Bansal prog = orig_prog; 176639c13c20SShubham Bansal goto out_off; 176739c13c20SShubham Bansal } 1768ddecdfceSMircea Gherzan } 1769ddecdfceSMircea Gherzan #else 177039c13c20SShubham Bansal /* there's nothing about the epilogue on ARMv7 */ 1771ddecdfceSMircea Gherzan build_epilogue(&ctx); 1772ddecdfceSMircea Gherzan #endif 177339c13c20SShubham Bansal /* Now we can get the actual image size of the JITed arm code. 177439c13c20SShubham Bansal * Currently, we are not considering the THUMB-2 instructions 177539c13c20SShubham Bansal * for jit, although it can decrease the size of the image. 177639c13c20SShubham Bansal * 177739c13c20SShubham Bansal * As each arm instruction is of length 32bit, we are translating 177839c13c20SShubham Bansal * number of JITed intructions into the size required to store these 177939c13c20SShubham Bansal * JITed code. 178039c13c20SShubham Bansal */ 178139c13c20SShubham Bansal image_size = sizeof(u32) * ctx.idx; 1782ddecdfceSMircea Gherzan 178339c13c20SShubham Bansal /* Now we know the size of the structure to make */ 178439c13c20SShubham Bansal header = bpf_jit_binary_alloc(image_size, &image_ptr, 178539c13c20SShubham Bansal sizeof(u32), jit_fill_hole); 178639c13c20SShubham Bansal /* Not able to allocate memory for the structure then 178739c13c20SShubham Bansal * we must fall back to the interpretation 178839c13c20SShubham Bansal */ 178939c13c20SShubham Bansal if (header == NULL) { 179039c13c20SShubham Bansal prog = orig_prog; 179139c13c20SShubham Bansal goto out_imms; 179239c13c20SShubham Bansal } 179339c13c20SShubham Bansal 179439c13c20SShubham Bansal /* 2.) Actual pass to generate final JIT code */ 179539c13c20SShubham Bansal ctx.target = (u32 *) image_ptr; 1796ddecdfceSMircea Gherzan ctx.idx = 0; 179755309dd3SDaniel Borkmann 1798ddecdfceSMircea Gherzan build_prologue(&ctx); 179939c13c20SShubham Bansal 180039c13c20SShubham Bansal /* If building the body of the JITed code fails somehow, 180139c13c20SShubham Bansal * we fall back to the interpretation. 180239c13c20SShubham Bansal */ 18030b59d880SNicolas Schichan if (build_body(&ctx) < 0) { 180439c13c20SShubham Bansal image_ptr = NULL; 18050b59d880SNicolas Schichan bpf_jit_binary_free(header); 180639c13c20SShubham Bansal prog = orig_prog; 180739c13c20SShubham Bansal goto out_imms; 18080b59d880SNicolas Schichan } 1809ddecdfceSMircea Gherzan build_epilogue(&ctx); 1810ddecdfceSMircea Gherzan 181139c13c20SShubham Bansal /* 3.) Extra pass to validate JITed Code */ 181239c13c20SShubham Bansal if (validate_code(&ctx)) { 181339c13c20SShubham Bansal image_ptr = NULL; 181439c13c20SShubham Bansal bpf_jit_binary_free(header); 181539c13c20SShubham Bansal prog = orig_prog; 181639c13c20SShubham Bansal goto out_imms; 181739c13c20SShubham Bansal } 1818ebaef649SDaniel Borkmann flush_icache_range((u32)header, (u32)(ctx.target + ctx.idx)); 1819ddecdfceSMircea Gherzan 182039c13c20SShubham Bansal if (bpf_jit_enable > 1) 182139c13c20SShubham Bansal /* there are 2 passes here */ 182239c13c20SShubham Bansal bpf_jit_dump(prog->len, image_size, 2, ctx.target); 182339c13c20SShubham Bansal 182418d405afSDaniel Borkmann bpf_jit_binary_lock_ro(header); 182539c13c20SShubham Bansal prog->bpf_func = (void *)ctx.target; 182639c13c20SShubham Bansal prog->jited = 1; 182739c13c20SShubham Bansal prog->jited_len = image_size; 182839c13c20SShubham Bansal 182939c13c20SShubham Bansal out_imms: 1830ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7 1831ddecdfceSMircea Gherzan if (ctx.imm_count) 1832ddecdfceSMircea Gherzan kfree(ctx.imms); 1833ddecdfceSMircea Gherzan #endif 183439c13c20SShubham Bansal out_off: 1835ddecdfceSMircea Gherzan kfree(ctx.offsets); 183639c13c20SShubham Bansal out: 183739c13c20SShubham Bansal if (tmp_blinded) 183839c13c20SShubham Bansal bpf_jit_prog_release_other(prog, prog == orig_prog ? 183939c13c20SShubham Bansal tmp : orig_prog); 184039c13c20SShubham Bansal return prog; 1841ddecdfceSMircea Gherzan } 1842ddecdfceSMircea Gherzan 1843