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> 258c9602d3SRussell King #include <asm/system_info.h> 26ddecdfceSMircea Gherzan 27ddecdfceSMircea Gherzan #include "bpf_jit_32.h" 28ddecdfceSMircea Gherzan 2970ec3a6cSRussell King /* 300005e55aSRussell King * eBPF prog stack layout: 3170ec3a6cSRussell King * 3270ec3a6cSRussell King * high 330005e55aSRussell King * original ARM_SP => +-----+ 340005e55aSRussell King * | | callee saved registers 350005e55aSRussell King * +-----+ <= (BPF_FP + SCRATCH_SIZE) 3670ec3a6cSRussell King * | ... | eBPF JIT scratch space 370005e55aSRussell King * eBPF fp register => +-----+ 380005e55aSRussell King * (BPF_FP) | ... | eBPF prog stack 3970ec3a6cSRussell King * +-----+ 4070ec3a6cSRussell King * |RSVD | JIT scratchpad 410005e55aSRussell King * current ARM_SP => +-----+ <= (BPF_FP - STACK_SIZE + SCRATCH_SIZE) 4270ec3a6cSRussell King * | | 4370ec3a6cSRussell King * | ... | Function call stack 4470ec3a6cSRussell King * | | 4570ec3a6cSRussell King * +-----+ 4670ec3a6cSRussell King * low 470005e55aSRussell King * 480005e55aSRussell King * The callee saved registers depends on whether frame pointers are enabled. 490005e55aSRussell King * With frame pointers (to be compliant with the ABI): 500005e55aSRussell King * 510005e55aSRussell King * high 52bef8968dSRussell King * original ARM_SP => +--------------+ \ 530005e55aSRussell King * | pc | | 54bef8968dSRussell King * current ARM_FP => +--------------+ } callee saved registers 55bef8968dSRussell King * |r4-r9,fp,ip,lr| | 56bef8968dSRussell King * +--------------+ / 570005e55aSRussell King * low 580005e55aSRussell King * 590005e55aSRussell King * Without frame pointers: 600005e55aSRussell King * 610005e55aSRussell King * high 62bef8968dSRussell King * original ARM_SP => +--------------+ 63bef8968dSRussell King * | r4-r9,fp,lr | callee saved registers 64bef8968dSRussell King * current ARM_FP => +--------------+ 650005e55aSRussell King * low 6602088d9bSRussell King * 6702088d9bSRussell King * When popping registers off the stack at the end of a BPF function, we 6802088d9bSRussell King * reference them via the current ARM_FP register. 6970ec3a6cSRussell King */ 7002088d9bSRussell King #define CALLEE_MASK (1 << ARM_R4 | 1 << ARM_R5 | 1 << ARM_R6 | \ 71bef8968dSRussell King 1 << ARM_R7 | 1 << ARM_R8 | 1 << ARM_R9 | \ 7202088d9bSRussell King 1 << ARM_FP) 7302088d9bSRussell King #define CALLEE_PUSH_MASK (CALLEE_MASK | 1 << ARM_LR) 7402088d9bSRussell King #define CALLEE_POP_MASK (CALLEE_MASK | 1 << ARM_PC) 7570ec3a6cSRussell King 76d449ceb1SRussell King enum { 77d449ceb1SRussell King /* Stack layout - these are offsets from (top of stack - 4) */ 78d449ceb1SRussell King BPF_R2_HI, 79d449ceb1SRussell King BPF_R2_LO, 80d449ceb1SRussell King BPF_R3_HI, 81d449ceb1SRussell King BPF_R3_LO, 82d449ceb1SRussell King BPF_R4_HI, 83d449ceb1SRussell King BPF_R4_LO, 84d449ceb1SRussell King BPF_R5_HI, 85d449ceb1SRussell King BPF_R5_LO, 86d449ceb1SRussell King BPF_R7_HI, 87d449ceb1SRussell King BPF_R7_LO, 88d449ceb1SRussell King BPF_R8_HI, 89d449ceb1SRussell King BPF_R8_LO, 90d449ceb1SRussell King BPF_R9_HI, 91d449ceb1SRussell King BPF_R9_LO, 92d449ceb1SRussell King BPF_FP_HI, 93d449ceb1SRussell King BPF_FP_LO, 94d449ceb1SRussell King BPF_TC_HI, 95d449ceb1SRussell King BPF_TC_LO, 96d449ceb1SRussell King BPF_AX_HI, 97d449ceb1SRussell King BPF_AX_LO, 98d449ceb1SRussell King /* Stack space for BPF_REG_2, BPF_REG_3, BPF_REG_4, 99d449ceb1SRussell King * BPF_REG_5, BPF_REG_7, BPF_REG_8, BPF_REG_9, 100d449ceb1SRussell King * BPF_REG_FP and Tail call counts. 101d449ceb1SRussell King */ 102d449ceb1SRussell King BPF_JIT_SCRATCH_REGS, 103d449ceb1SRussell King }; 104d449ceb1SRussell King 1051c35ba12SRussell King /* 1061c35ba12SRussell King * Negative "register" values indicate the register is stored on the stack 1071c35ba12SRussell King * and are the offset from the top of the eBPF JIT scratch space. 1081c35ba12SRussell King */ 1091c35ba12SRussell King #define STACK_OFFSET(k) (-4 - (k) * 4) 110d449ceb1SRussell King #define SCRATCH_SIZE (BPF_JIT_SCRATCH_REGS * 4) 111d449ceb1SRussell King 11296cced4eSRussell King #ifdef CONFIG_FRAME_POINTER 11396cced4eSRussell King #define EBPF_SCRATCH_TO_ARM_FP(x) ((x) - 4 * hweight16(CALLEE_PUSH_MASK) - 4) 11496cced4eSRussell King #else 11596cced4eSRussell King #define EBPF_SCRATCH_TO_ARM_FP(x) (x) 11696cced4eSRussell King #endif 11796cced4eSRussell King 11839c13c20SShubham Bansal #define TMP_REG_1 (MAX_BPF_JIT_REG + 0) /* TEMP Register 1 */ 11939c13c20SShubham Bansal #define TMP_REG_2 (MAX_BPF_JIT_REG + 1) /* TEMP Register 2 */ 12039c13c20SShubham Bansal #define TCALL_CNT (MAX_BPF_JIT_REG + 2) /* Tail Call Count */ 12139c13c20SShubham Bansal 12239c13c20SShubham Bansal #define FLAG_IMM_OVERFLOW (1 << 0) 12339c13c20SShubham Bansal 124ddecdfceSMircea Gherzan /* 12539c13c20SShubham Bansal * Map eBPF registers to ARM 32bit registers or stack scratch space. 126ddecdfceSMircea Gherzan * 12739c13c20SShubham Bansal * 1. First argument is passed using the arm 32bit registers and rest of the 12839c13c20SShubham Bansal * arguments are passed on stack scratch space. 1292b589a7eSWang YanQing * 2. First callee-saved argument is mapped to arm 32 bit registers and rest 13039c13c20SShubham Bansal * arguments are mapped to scratch space on stack. 13139c13c20SShubham Bansal * 3. We need two 64 bit temp registers to do complex operations on eBPF 13239c13c20SShubham Bansal * registers. 13339c13c20SShubham Bansal * 13439c13c20SShubham Bansal * As the eBPF registers are all 64 bit registers and arm has only 32 bit 13539c13c20SShubham Bansal * registers, we have to map each eBPF registers with two arm 32 bit regs or 13639c13c20SShubham Bansal * scratch memory space and we have to build eBPF 64 bit register from those. 13739c13c20SShubham Bansal * 13839c13c20SShubham Bansal */ 1391c35ba12SRussell King static const s8 bpf2a32[][2] = { 14039c13c20SShubham Bansal /* return value from in-kernel function, and exit value from eBPF */ 14139c13c20SShubham Bansal [BPF_REG_0] = {ARM_R1, ARM_R0}, 14239c13c20SShubham Bansal /* arguments from eBPF program to in-kernel function */ 14339c13c20SShubham Bansal [BPF_REG_1] = {ARM_R3, ARM_R2}, 14439c13c20SShubham Bansal /* Stored on stack scratch space */ 145d449ceb1SRussell King [BPF_REG_2] = {STACK_OFFSET(BPF_R2_HI), STACK_OFFSET(BPF_R2_LO)}, 146d449ceb1SRussell King [BPF_REG_3] = {STACK_OFFSET(BPF_R3_HI), STACK_OFFSET(BPF_R3_LO)}, 147d449ceb1SRussell King [BPF_REG_4] = {STACK_OFFSET(BPF_R4_HI), STACK_OFFSET(BPF_R4_LO)}, 148d449ceb1SRussell King [BPF_REG_5] = {STACK_OFFSET(BPF_R5_HI), STACK_OFFSET(BPF_R5_LO)}, 14939c13c20SShubham Bansal /* callee saved registers that in-kernel function will preserve */ 15039c13c20SShubham Bansal [BPF_REG_6] = {ARM_R5, ARM_R4}, 15139c13c20SShubham Bansal /* Stored on stack scratch space */ 152d449ceb1SRussell King [BPF_REG_7] = {STACK_OFFSET(BPF_R7_HI), STACK_OFFSET(BPF_R7_LO)}, 153d449ceb1SRussell King [BPF_REG_8] = {STACK_OFFSET(BPF_R8_HI), STACK_OFFSET(BPF_R8_LO)}, 154d449ceb1SRussell King [BPF_REG_9] = {STACK_OFFSET(BPF_R9_HI), STACK_OFFSET(BPF_R9_LO)}, 15539c13c20SShubham Bansal /* Read only Frame Pointer to access Stack */ 156d449ceb1SRussell King [BPF_REG_FP] = {STACK_OFFSET(BPF_FP_HI), STACK_OFFSET(BPF_FP_LO)}, 15739c13c20SShubham Bansal /* Temporary Register for internal BPF JIT, can be used 15839c13c20SShubham Bansal * for constant blindings and others. 15939c13c20SShubham Bansal */ 16039c13c20SShubham Bansal [TMP_REG_1] = {ARM_R7, ARM_R6}, 161bef8968dSRussell King [TMP_REG_2] = {ARM_R9, ARM_R8}, 16239c13c20SShubham Bansal /* Tail call count. Stored on stack scratch space. */ 163d449ceb1SRussell King [TCALL_CNT] = {STACK_OFFSET(BPF_TC_HI), STACK_OFFSET(BPF_TC_LO)}, 16439c13c20SShubham Bansal /* temporary register for blinding constants. 16539c13c20SShubham Bansal * Stored on stack scratch space. 16639c13c20SShubham Bansal */ 167d449ceb1SRussell King [BPF_REG_AX] = {STACK_OFFSET(BPF_AX_HI), STACK_OFFSET(BPF_AX_LO)}, 16839c13c20SShubham Bansal }; 16939c13c20SShubham Bansal 17039c13c20SShubham Bansal #define dst_lo dst[1] 17139c13c20SShubham Bansal #define dst_hi dst[0] 17239c13c20SShubham Bansal #define src_lo src[1] 17339c13c20SShubham Bansal #define src_hi src[0] 17439c13c20SShubham Bansal 17539c13c20SShubham Bansal /* 17639c13c20SShubham Bansal * JIT Context: 17739c13c20SShubham Bansal * 17839c13c20SShubham Bansal * prog : bpf_prog 17939c13c20SShubham Bansal * idx : index of current last JITed instruction. 18039c13c20SShubham Bansal * prologue_bytes : bytes used in prologue. 18139c13c20SShubham Bansal * epilogue_offset : offset of epilogue starting. 18239c13c20SShubham Bansal * offsets : array of eBPF instruction offsets in 18339c13c20SShubham Bansal * JITed code. 18439c13c20SShubham Bansal * target : final JITed code. 18539c13c20SShubham Bansal * epilogue_bytes : no of bytes used in epilogue. 18639c13c20SShubham Bansal * imm_count : no of immediate counts used for global 18739c13c20SShubham Bansal * variables. 18839c13c20SShubham Bansal * imms : array of global variable addresses. 189ddecdfceSMircea Gherzan */ 190ddecdfceSMircea Gherzan 191ddecdfceSMircea Gherzan struct jit_ctx { 19239c13c20SShubham Bansal const struct bpf_prog *prog; 19339c13c20SShubham Bansal unsigned int idx; 19439c13c20SShubham Bansal unsigned int prologue_bytes; 19539c13c20SShubham Bansal unsigned int epilogue_offset; 1968c9602d3SRussell King unsigned int cpu_architecture; 197ddecdfceSMircea Gherzan u32 flags; 198ddecdfceSMircea Gherzan u32 *offsets; 199ddecdfceSMircea Gherzan u32 *target; 20039c13c20SShubham Bansal u32 stack_size; 201ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7 202ddecdfceSMircea Gherzan u16 epilogue_bytes; 203ddecdfceSMircea Gherzan u16 imm_count; 204ddecdfceSMircea Gherzan u32 *imms; 205ddecdfceSMircea Gherzan #endif 206ddecdfceSMircea Gherzan }; 207ddecdfceSMircea Gherzan 208ddecdfceSMircea Gherzan /* 2094560cdffSNicolas Schichan * Wrappers which handle both OABI and EABI and assures Thumb2 interworking 210ddecdfceSMircea Gherzan * (where the assembly routines like __aeabi_uidiv could cause problems). 211ddecdfceSMircea Gherzan */ 21239c13c20SShubham Bansal static u32 jit_udiv32(u32 dividend, u32 divisor) 213ddecdfceSMircea Gherzan { 214ddecdfceSMircea Gherzan return dividend / divisor; 215ddecdfceSMircea Gherzan } 216ddecdfceSMircea Gherzan 21739c13c20SShubham Bansal static u32 jit_mod32(u32 dividend, u32 divisor) 2184560cdffSNicolas Schichan { 2194560cdffSNicolas Schichan return dividend % divisor; 2204560cdffSNicolas Schichan } 2214560cdffSNicolas Schichan 222ddecdfceSMircea Gherzan static inline void _emit(int cond, u32 inst, struct jit_ctx *ctx) 223ddecdfceSMircea Gherzan { 2243460743eSBen Dooks inst |= (cond << 28); 2253460743eSBen Dooks inst = __opcode_to_mem_arm(inst); 2263460743eSBen Dooks 227ddecdfceSMircea Gherzan if (ctx->target != NULL) 2283460743eSBen Dooks ctx->target[ctx->idx] = inst; 229ddecdfceSMircea Gherzan 230ddecdfceSMircea Gherzan ctx->idx++; 231ddecdfceSMircea Gherzan } 232ddecdfceSMircea Gherzan 233ddecdfceSMircea Gherzan /* 234ddecdfceSMircea Gherzan * Emit an instruction that will be executed unconditionally. 235ddecdfceSMircea Gherzan */ 236ddecdfceSMircea Gherzan static inline void emit(u32 inst, struct jit_ctx *ctx) 237ddecdfceSMircea Gherzan { 238ddecdfceSMircea Gherzan _emit(ARM_COND_AL, inst, ctx); 239ddecdfceSMircea Gherzan } 240ddecdfceSMircea Gherzan 24139c13c20SShubham Bansal /* 2421ca3b17bSRussell King * This is rather horrid, but necessary to convert an integer constant 2431ca3b17bSRussell King * to an immediate operand for the opcodes, and be able to detect at 2441ca3b17bSRussell King * build time whether the constant can't be converted (iow, usable in 2451ca3b17bSRussell King * BUILD_BUG_ON()). 2461ca3b17bSRussell King */ 2471ca3b17bSRussell King #define imm12val(v, s) (rol32(v, (s)) | (s) << 7) 2481ca3b17bSRussell King #define const_imm8m(x) \ 2491ca3b17bSRussell King ({ int r; \ 2501ca3b17bSRussell King u32 v = (x); \ 2511ca3b17bSRussell King if (!(v & ~0x000000ff)) \ 2521ca3b17bSRussell King r = imm12val(v, 0); \ 2531ca3b17bSRussell King else if (!(v & ~0xc000003f)) \ 2541ca3b17bSRussell King r = imm12val(v, 2); \ 2551ca3b17bSRussell King else if (!(v & ~0xf000000f)) \ 2561ca3b17bSRussell King r = imm12val(v, 4); \ 2571ca3b17bSRussell King else if (!(v & ~0xfc000003)) \ 2581ca3b17bSRussell King r = imm12val(v, 6); \ 2591ca3b17bSRussell King else if (!(v & ~0xff000000)) \ 2601ca3b17bSRussell King r = imm12val(v, 8); \ 2611ca3b17bSRussell King else if (!(v & ~0x3fc00000)) \ 2621ca3b17bSRussell King r = imm12val(v, 10); \ 2631ca3b17bSRussell King else if (!(v & ~0x0ff00000)) \ 2641ca3b17bSRussell King r = imm12val(v, 12); \ 2651ca3b17bSRussell King else if (!(v & ~0x03fc0000)) \ 2661ca3b17bSRussell King r = imm12val(v, 14); \ 2671ca3b17bSRussell King else if (!(v & ~0x00ff0000)) \ 2681ca3b17bSRussell King r = imm12val(v, 16); \ 2691ca3b17bSRussell King else if (!(v & ~0x003fc000)) \ 2701ca3b17bSRussell King r = imm12val(v, 18); \ 2711ca3b17bSRussell King else if (!(v & ~0x000ff000)) \ 2721ca3b17bSRussell King r = imm12val(v, 20); \ 2731ca3b17bSRussell King else if (!(v & ~0x0003fc00)) \ 2741ca3b17bSRussell King r = imm12val(v, 22); \ 2751ca3b17bSRussell King else if (!(v & ~0x0000ff00)) \ 2761ca3b17bSRussell King r = imm12val(v, 24); \ 2771ca3b17bSRussell King else if (!(v & ~0x00003fc0)) \ 2781ca3b17bSRussell King r = imm12val(v, 26); \ 2791ca3b17bSRussell King else if (!(v & ~0x00000ff0)) \ 2801ca3b17bSRussell King r = imm12val(v, 28); \ 2811ca3b17bSRussell King else if (!(v & ~0x000003fc)) \ 2821ca3b17bSRussell King r = imm12val(v, 30); \ 2831ca3b17bSRussell King else \ 2841ca3b17bSRussell King r = -1; \ 2851ca3b17bSRussell King r; }) 2861ca3b17bSRussell King 2871ca3b17bSRussell King /* 28839c13c20SShubham Bansal * Checks if immediate value can be converted to imm12(12 bits) value. 28939c13c20SShubham Bansal */ 2901ca3b17bSRussell King static int imm8m(u32 x) 291ddecdfceSMircea Gherzan { 29239c13c20SShubham Bansal u32 rot; 293ddecdfceSMircea Gherzan 29439c13c20SShubham Bansal for (rot = 0; rot < 16; rot++) 29539c13c20SShubham Bansal if ((x & ~ror32(0xff, 2 * rot)) == 0) 29639c13c20SShubham Bansal return rol32(x, 2 * rot) | (rot << 8); 29739c13c20SShubham Bansal return -1; 298ddecdfceSMircea Gherzan } 299ddecdfceSMircea Gherzan 3001ca3b17bSRussell King #define imm8m(x) (__builtin_constant_p(x) ? const_imm8m(x) : imm8m(x)) 3011ca3b17bSRussell King 302a8ef95a0SRussell King static u32 arm_bpf_ldst_imm12(u32 op, u8 rt, u8 rn, s16 imm12) 303a8ef95a0SRussell King { 304a8ef95a0SRussell King op |= rt << 12 | rn << 16; 305a8ef95a0SRussell King if (imm12 >= 0) 306a8ef95a0SRussell King op |= ARM_INST_LDST__U; 307a8ef95a0SRussell King else 308a8ef95a0SRussell King imm12 = -imm12; 309828e2b90SRussell King return op | (imm12 & ARM_INST_LDST__IMM12); 310a8ef95a0SRussell King } 311a8ef95a0SRussell King 312a8ef95a0SRussell King static u32 arm_bpf_ldst_imm8(u32 op, u8 rt, u8 rn, s16 imm8) 313a8ef95a0SRussell King { 314a8ef95a0SRussell King op |= rt << 12 | rn << 16; 315a8ef95a0SRussell King if (imm8 >= 0) 316a8ef95a0SRussell King op |= ARM_INST_LDST__U; 317a8ef95a0SRussell King else 318a8ef95a0SRussell King imm8 = -imm8; 319a8ef95a0SRussell King return op | (imm8 & 0xf0) << 4 | (imm8 & 0x0f); 320a8ef95a0SRussell King } 321a8ef95a0SRussell King 322a8ef95a0SRussell King #define ARM_LDR_I(rt, rn, off) arm_bpf_ldst_imm12(ARM_INST_LDR_I, rt, rn, off) 323a8ef95a0SRussell King #define ARM_LDRB_I(rt, rn, off) arm_bpf_ldst_imm12(ARM_INST_LDRB_I, rt, rn, off) 3248c9602d3SRussell King #define ARM_LDRD_I(rt, rn, off) arm_bpf_ldst_imm8(ARM_INST_LDRD_I, rt, rn, off) 325a8ef95a0SRussell King #define ARM_LDRH_I(rt, rn, off) arm_bpf_ldst_imm8(ARM_INST_LDRH_I, rt, rn, off) 326a8ef95a0SRussell King 327a8ef95a0SRussell King #define ARM_STR_I(rt, rn, off) arm_bpf_ldst_imm12(ARM_INST_STR_I, rt, rn, off) 328a8ef95a0SRussell King #define ARM_STRB_I(rt, rn, off) arm_bpf_ldst_imm12(ARM_INST_STRB_I, rt, rn, off) 3298c9602d3SRussell King #define ARM_STRD_I(rt, rn, off) arm_bpf_ldst_imm8(ARM_INST_STRD_I, rt, rn, off) 330a8ef95a0SRussell King #define ARM_STRH_I(rt, rn, off) arm_bpf_ldst_imm8(ARM_INST_STRH_I, rt, rn, off) 331a8ef95a0SRussell King 33239c13c20SShubham Bansal /* 33339c13c20SShubham Bansal * Initializes the JIT space with undefined instructions. 33439c13c20SShubham Bansal */ 33555309dd3SDaniel Borkmann static void jit_fill_hole(void *area, unsigned int size) 33655309dd3SDaniel Borkmann { 337e8b56d55SDaniel Borkmann u32 *ptr; 33855309dd3SDaniel Borkmann /* We are guaranteed to have aligned memory. */ 33955309dd3SDaniel Borkmann for (ptr = area; size >= sizeof(u32); size -= sizeof(u32)) 340e8b56d55SDaniel Borkmann *ptr++ = __opcode_to_mem_arm(ARM_INST_UDF); 34155309dd3SDaniel Borkmann } 34255309dd3SDaniel Borkmann 343d1220efdSRussell King #if defined(CONFIG_AEABI) && (__LINUX_ARM_ARCH__ >= 5) 344d1220efdSRussell King /* EABI requires the stack to be aligned to 64-bit boundaries */ 345d1220efdSRussell King #define STACK_ALIGNMENT 8 346d1220efdSRussell King #else 347d1220efdSRussell King /* Stack must be aligned to 32-bit boundaries */ 348d1220efdSRussell King #define STACK_ALIGNMENT 4 349d1220efdSRussell King #endif 350ddecdfceSMircea Gherzan 35139c13c20SShubham Bansal /* total stack size used in JITed code */ 35238ca9306SDaniel Borkmann #define _STACK_SIZE (ctx->prog->aux->stack_depth + SCRATCH_SIZE) 353d1220efdSRussell King #define STACK_SIZE ALIGN(_STACK_SIZE, STACK_ALIGNMENT) 354ddecdfceSMircea Gherzan 355ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7 356ddecdfceSMircea Gherzan 357ddecdfceSMircea Gherzan static u16 imm_offset(u32 k, struct jit_ctx *ctx) 358ddecdfceSMircea Gherzan { 35939c13c20SShubham Bansal unsigned int i = 0, offset; 360ddecdfceSMircea Gherzan u16 imm; 361ddecdfceSMircea Gherzan 362ddecdfceSMircea Gherzan /* on the "fake" run we just count them (duplicates included) */ 363ddecdfceSMircea Gherzan if (ctx->target == NULL) { 364ddecdfceSMircea Gherzan ctx->imm_count++; 365ddecdfceSMircea Gherzan return 0; 366ddecdfceSMircea Gherzan } 367ddecdfceSMircea Gherzan 368ddecdfceSMircea Gherzan while ((i < ctx->imm_count) && ctx->imms[i]) { 369ddecdfceSMircea Gherzan if (ctx->imms[i] == k) 370ddecdfceSMircea Gherzan break; 371ddecdfceSMircea Gherzan i++; 372ddecdfceSMircea Gherzan } 373ddecdfceSMircea Gherzan 374ddecdfceSMircea Gherzan if (ctx->imms[i] == 0) 375ddecdfceSMircea Gherzan ctx->imms[i] = k; 376ddecdfceSMircea Gherzan 377ddecdfceSMircea Gherzan /* constants go just after the epilogue */ 37839c13c20SShubham Bansal offset = ctx->offsets[ctx->prog->len - 1] * 4; 379ddecdfceSMircea Gherzan offset += ctx->prologue_bytes; 380ddecdfceSMircea Gherzan offset += ctx->epilogue_bytes; 381ddecdfceSMircea Gherzan offset += i * 4; 382ddecdfceSMircea Gherzan 383ddecdfceSMircea Gherzan ctx->target[offset / 4] = k; 384ddecdfceSMircea Gherzan 385ddecdfceSMircea Gherzan /* PC in ARM mode == address of the instruction + 8 */ 386ddecdfceSMircea Gherzan imm = offset - (8 + ctx->idx * 4); 387ddecdfceSMircea Gherzan 3880b59d880SNicolas Schichan if (imm & ~0xfff) { 3890b59d880SNicolas Schichan /* 3900b59d880SNicolas Schichan * literal pool is too far, signal it into flags. we 3910b59d880SNicolas Schichan * can only detect it on the second pass unfortunately. 3920b59d880SNicolas Schichan */ 3930b59d880SNicolas Schichan ctx->flags |= FLAG_IMM_OVERFLOW; 3940b59d880SNicolas Schichan return 0; 3950b59d880SNicolas Schichan } 3960b59d880SNicolas Schichan 397ddecdfceSMircea Gherzan return imm; 398ddecdfceSMircea Gherzan } 399ddecdfceSMircea Gherzan 400ddecdfceSMircea Gherzan #endif /* __LINUX_ARM_ARCH__ */ 401ddecdfceSMircea Gherzan 40239c13c20SShubham Bansal static inline int bpf2a32_offset(int bpf_to, int bpf_from, 40339c13c20SShubham Bansal const struct jit_ctx *ctx) { 40439c13c20SShubham Bansal int to, from; 40539c13c20SShubham Bansal 40639c13c20SShubham Bansal if (ctx->target == NULL) 40739c13c20SShubham Bansal return 0; 40839c13c20SShubham Bansal to = ctx->offsets[bpf_to]; 40939c13c20SShubham Bansal from = ctx->offsets[bpf_from]; 41039c13c20SShubham Bansal 41139c13c20SShubham Bansal return to - from - 1; 41239c13c20SShubham Bansal } 41339c13c20SShubham Bansal 414ddecdfceSMircea Gherzan /* 415ddecdfceSMircea Gherzan * Move an immediate that's not an imm8m to a core register. 416ddecdfceSMircea Gherzan */ 41739c13c20SShubham Bansal static inline void emit_mov_i_no8m(const u8 rd, u32 val, struct jit_ctx *ctx) 418ddecdfceSMircea Gherzan { 419ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7 420ddecdfceSMircea Gherzan emit(ARM_LDR_I(rd, ARM_PC, imm_offset(val, ctx)), ctx); 421ddecdfceSMircea Gherzan #else 422ddecdfceSMircea Gherzan emit(ARM_MOVW(rd, val & 0xffff), ctx); 423ddecdfceSMircea Gherzan if (val > 0xffff) 424ddecdfceSMircea Gherzan emit(ARM_MOVT(rd, val >> 16), ctx); 425ddecdfceSMircea Gherzan #endif 426ddecdfceSMircea Gherzan } 427ddecdfceSMircea Gherzan 42839c13c20SShubham Bansal static inline void emit_mov_i(const u8 rd, u32 val, struct jit_ctx *ctx) 429ddecdfceSMircea Gherzan { 430ddecdfceSMircea Gherzan int imm12 = imm8m(val); 431ddecdfceSMircea Gherzan 432ddecdfceSMircea Gherzan if (imm12 >= 0) 433ddecdfceSMircea Gherzan emit(ARM_MOV_I(rd, imm12), ctx); 434ddecdfceSMircea Gherzan else 435ddecdfceSMircea Gherzan emit_mov_i_no8m(rd, val, ctx); 436ddecdfceSMircea Gherzan } 437ddecdfceSMircea Gherzan 438e9062481SRussell King static void emit_bx_r(u8 tgt_reg, struct jit_ctx *ctx) 439ddecdfceSMircea Gherzan { 440ddecdfceSMircea Gherzan if (elf_hwcap & HWCAP_THUMB) 441ddecdfceSMircea Gherzan emit(ARM_BX(tgt_reg), ctx); 442ddecdfceSMircea Gherzan else 443ddecdfceSMircea Gherzan emit(ARM_MOV_R(ARM_PC, tgt_reg), ctx); 444e9062481SRussell King } 445e9062481SRussell King 446ddecdfceSMircea Gherzan static inline void emit_blx_r(u8 tgt_reg, struct jit_ctx *ctx) 447ddecdfceSMircea Gherzan { 448ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 5 449ddecdfceSMircea Gherzan emit(ARM_MOV_R(ARM_LR, ARM_PC), ctx); 450e9062481SRussell King emit_bx_r(tgt_reg, ctx); 451ddecdfceSMircea Gherzan #else 452ddecdfceSMircea Gherzan emit(ARM_BLX_R(tgt_reg), ctx); 453ddecdfceSMircea Gherzan #endif 454ddecdfceSMircea Gherzan } 455ddecdfceSMircea Gherzan 45639c13c20SShubham Bansal static inline int epilogue_offset(const struct jit_ctx *ctx) 457ddecdfceSMircea Gherzan { 45839c13c20SShubham Bansal int to, from; 45939c13c20SShubham Bansal /* No need for 1st dummy run */ 46039c13c20SShubham Bansal if (ctx->target == NULL) 46139c13c20SShubham Bansal return 0; 46239c13c20SShubham Bansal to = ctx->epilogue_offset; 46339c13c20SShubham Bansal from = ctx->idx; 46439c13c20SShubham Bansal 46539c13c20SShubham Bansal return to - from - 2; 46639c13c20SShubham Bansal } 46739c13c20SShubham Bansal 46839c13c20SShubham Bansal static inline void emit_udivmod(u8 rd, u8 rm, u8 rn, struct jit_ctx *ctx, u8 op) 46939c13c20SShubham Bansal { 4701c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 47139c13c20SShubham Bansal 472ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ == 7 473ddecdfceSMircea Gherzan if (elf_hwcap & HWCAP_IDIVA) { 47439c13c20SShubham Bansal if (op == BPF_DIV) 475ddecdfceSMircea Gherzan emit(ARM_UDIV(rd, rm, rn), ctx); 4764560cdffSNicolas Schichan else { 47739c13c20SShubham Bansal emit(ARM_UDIV(ARM_IP, rm, rn), ctx); 47839c13c20SShubham Bansal emit(ARM_MLS(rd, rn, ARM_IP, rm), ctx); 4794560cdffSNicolas Schichan } 480ddecdfceSMircea Gherzan return; 481ddecdfceSMircea Gherzan } 482ddecdfceSMircea Gherzan #endif 48319fc99d0SNicolas Schichan 48419fc99d0SNicolas Schichan /* 48539c13c20SShubham Bansal * For BPF_ALU | BPF_DIV | BPF_K instructions 48639c13c20SShubham Bansal * As ARM_R1 and ARM_R0 contains 1st argument of bpf 48739c13c20SShubham Bansal * function, we need to save it on caller side to save 48839c13c20SShubham Bansal * it from getting destroyed within callee. 48939c13c20SShubham Bansal * After the return from the callee, we restore ARM_R0 49039c13c20SShubham Bansal * ARM_R1. 49119fc99d0SNicolas Schichan */ 49239c13c20SShubham Bansal if (rn != ARM_R1) { 49339c13c20SShubham Bansal emit(ARM_MOV_R(tmp[0], ARM_R1), ctx); 494ddecdfceSMircea Gherzan emit(ARM_MOV_R(ARM_R1, rn), ctx); 49539c13c20SShubham Bansal } 49639c13c20SShubham Bansal if (rm != ARM_R0) { 49739c13c20SShubham Bansal emit(ARM_MOV_R(tmp[1], ARM_R0), ctx); 49819fc99d0SNicolas Schichan emit(ARM_MOV_R(ARM_R0, rm), ctx); 49939c13c20SShubham Bansal } 500ddecdfceSMircea Gherzan 50139c13c20SShubham Bansal /* Call appropriate function */ 50239c13c20SShubham Bansal emit_mov_i(ARM_IP, op == BPF_DIV ? 50339c13c20SShubham Bansal (u32)jit_udiv32 : (u32)jit_mod32, ctx); 50439c13c20SShubham Bansal emit_blx_r(ARM_IP, ctx); 505ddecdfceSMircea Gherzan 50639c13c20SShubham Bansal /* Save return value */ 507ddecdfceSMircea Gherzan if (rd != ARM_R0) 508ddecdfceSMircea Gherzan emit(ARM_MOV_R(rd, ARM_R0), ctx); 50939c13c20SShubham Bansal 51039c13c20SShubham Bansal /* Restore ARM_R0 and ARM_R1 */ 51139c13c20SShubham Bansal if (rn != ARM_R1) 51239c13c20SShubham Bansal emit(ARM_MOV_R(ARM_R1, tmp[0]), ctx); 51339c13c20SShubham Bansal if (rm != ARM_R0) 51439c13c20SShubham Bansal emit(ARM_MOV_R(ARM_R0, tmp[1]), ctx); 515ddecdfceSMircea Gherzan } 516ddecdfceSMircea Gherzan 51747b9c3bfSRussell King /* Is the translated BPF register on stack? */ 51847b9c3bfSRussell King static bool is_stacked(s8 reg) 519ddecdfceSMircea Gherzan { 52047b9c3bfSRussell King return reg < 0; 521ddecdfceSMircea Gherzan } 522ddecdfceSMircea Gherzan 5237a987025SRussell King /* If a BPF register is on the stack (stk is true), load it to the 5247a987025SRussell King * supplied temporary register and return the temporary register 5257a987025SRussell King * for subsequent operations, otherwise just use the CPU register. 5267a987025SRussell King */ 5277a987025SRussell King static s8 arm_bpf_get_reg32(s8 reg, s8 tmp, struct jit_ctx *ctx) 5287a987025SRussell King { 5297a987025SRussell King if (is_stacked(reg)) { 53096cced4eSRussell King emit(ARM_LDR_I(tmp, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(reg)), ctx); 5317a987025SRussell King reg = tmp; 5327a987025SRussell King } 5337a987025SRussell King return reg; 5347a987025SRussell King } 5357a987025SRussell King 536a6eccac5SRussell King static const s8 *arm_bpf_get_reg64(const s8 *reg, const s8 *tmp, 537a6eccac5SRussell King struct jit_ctx *ctx) 538a6eccac5SRussell King { 539a6eccac5SRussell King if (is_stacked(reg[1])) { 5408c9602d3SRussell King if (__LINUX_ARM_ARCH__ >= 6 || 5418c9602d3SRussell King ctx->cpu_architecture >= CPU_ARCH_ARMv5TE) { 5428c9602d3SRussell King emit(ARM_LDRD_I(tmp[1], ARM_FP, 5438c9602d3SRussell King EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx); 5448c9602d3SRussell King } else { 5458c9602d3SRussell King emit(ARM_LDR_I(tmp[1], ARM_FP, 5468c9602d3SRussell King EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx); 5478c9602d3SRussell King emit(ARM_LDR_I(tmp[0], ARM_FP, 5488c9602d3SRussell King EBPF_SCRATCH_TO_ARM_FP(reg[0])), ctx); 5498c9602d3SRussell King } 550a6eccac5SRussell King reg = tmp; 551a6eccac5SRussell King } 552a6eccac5SRussell King return reg; 553a6eccac5SRussell King } 554a6eccac5SRussell King 5557a987025SRussell King /* If a BPF register is on the stack (stk is true), save the register 5567a987025SRussell King * back to the stack. If the source register is not the same, then 5577a987025SRussell King * move it into the correct register. 5587a987025SRussell King */ 5597a987025SRussell King static void arm_bpf_put_reg32(s8 reg, s8 src, struct jit_ctx *ctx) 5607a987025SRussell King { 5617a987025SRussell King if (is_stacked(reg)) 56296cced4eSRussell King emit(ARM_STR_I(src, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(reg)), ctx); 5637a987025SRussell King else if (reg != src) 5647a987025SRussell King emit(ARM_MOV_R(reg, src), ctx); 5657a987025SRussell King } 5667a987025SRussell King 567a6eccac5SRussell King static void arm_bpf_put_reg64(const s8 *reg, const s8 *src, 568a6eccac5SRussell King struct jit_ctx *ctx) 569a6eccac5SRussell King { 570a6eccac5SRussell King if (is_stacked(reg[1])) { 5718c9602d3SRussell King if (__LINUX_ARM_ARCH__ >= 6 || 5728c9602d3SRussell King ctx->cpu_architecture >= CPU_ARCH_ARMv5TE) { 5738c9602d3SRussell King emit(ARM_STRD_I(src[1], ARM_FP, 5748c9602d3SRussell King EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx); 5758c9602d3SRussell King } else { 5768c9602d3SRussell King emit(ARM_STR_I(src[1], ARM_FP, 5778c9602d3SRussell King EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx); 5788c9602d3SRussell King emit(ARM_STR_I(src[0], ARM_FP, 5798c9602d3SRussell King EBPF_SCRATCH_TO_ARM_FP(reg[0])), ctx); 5808c9602d3SRussell King } 581a6eccac5SRussell King } else { 582a6eccac5SRussell King if (reg[1] != src[1]) 583a6eccac5SRussell King emit(ARM_MOV_R(reg[1], src[1]), ctx); 584a6eccac5SRussell King if (reg[0] != src[0]) 585a6eccac5SRussell King emit(ARM_MOV_R(reg[0], src[0]), ctx); 586a6eccac5SRussell King } 587a6eccac5SRussell King } 588a6eccac5SRussell King 5891c35ba12SRussell King static inline void emit_a32_mov_i(const s8 dst, const u32 val, 59047b9c3bfSRussell King struct jit_ctx *ctx) 591ddecdfceSMircea Gherzan { 5921c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 593ddecdfceSMircea Gherzan 59447b9c3bfSRussell King if (is_stacked(dst)) { 59539c13c20SShubham Bansal emit_mov_i(tmp[1], val, ctx); 5967a987025SRussell King arm_bpf_put_reg32(dst, tmp[1], ctx); 59739c13c20SShubham Bansal } else { 59839c13c20SShubham Bansal emit_mov_i(dst, val, ctx); 59939c13c20SShubham Bansal } 60039c13c20SShubham Bansal } 60134805931SDaniel Borkmann 602f9ff5018SRussell King static void emit_a32_mov_i64(const s8 dst[], u64 val, struct jit_ctx *ctx) 603f9ff5018SRussell King { 604f9ff5018SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 605f9ff5018SRussell King const s8 *rd = is_stacked(dst_lo) ? tmp : dst; 606f9ff5018SRussell King 607f9ff5018SRussell King emit_mov_i(rd[1], (u32)val, ctx); 608f9ff5018SRussell King emit_mov_i(rd[0], val >> 32, ctx); 609f9ff5018SRussell King 610f9ff5018SRussell King arm_bpf_put_reg64(dst, rd, ctx); 611f9ff5018SRussell King } 612f9ff5018SRussell King 61339c13c20SShubham Bansal /* Sign extended move */ 614f9ff5018SRussell King static inline void emit_a32_mov_se_i64(const bool is64, const s8 dst[], 61547b9c3bfSRussell King const u32 val, struct jit_ctx *ctx) { 616077513b8SRussell King u64 val64 = val; 617ddecdfceSMircea Gherzan 61839c13c20SShubham Bansal if (is64 && (val & (1<<31))) 619077513b8SRussell King val64 |= 0xffffffff00000000ULL; 620077513b8SRussell King emit_a32_mov_i64(dst, val64, ctx); 62139c13c20SShubham Bansal } 62239c13c20SShubham Bansal 62339c13c20SShubham Bansal static inline void emit_a32_add_r(const u8 dst, const u8 src, 62439c13c20SShubham Bansal const bool is64, const bool hi, 62539c13c20SShubham Bansal struct jit_ctx *ctx) { 62639c13c20SShubham Bansal /* 64 bit : 62739c13c20SShubham Bansal * adds dst_lo, dst_lo, src_lo 62839c13c20SShubham Bansal * adc dst_hi, dst_hi, src_hi 62939c13c20SShubham Bansal * 32 bit : 63039c13c20SShubham Bansal * add dst_lo, dst_lo, src_lo 63139c13c20SShubham Bansal */ 63239c13c20SShubham Bansal if (!hi && is64) 63339c13c20SShubham Bansal emit(ARM_ADDS_R(dst, dst, src), ctx); 63439c13c20SShubham Bansal else if (hi && is64) 63539c13c20SShubham Bansal emit(ARM_ADC_R(dst, dst, src), ctx); 63639c13c20SShubham Bansal else 63739c13c20SShubham Bansal emit(ARM_ADD_R(dst, dst, src), ctx); 63839c13c20SShubham Bansal } 63939c13c20SShubham Bansal 64039c13c20SShubham Bansal static inline void emit_a32_sub_r(const u8 dst, const u8 src, 64139c13c20SShubham Bansal const bool is64, const bool hi, 64239c13c20SShubham Bansal struct jit_ctx *ctx) { 64339c13c20SShubham Bansal /* 64 bit : 64439c13c20SShubham Bansal * subs dst_lo, dst_lo, src_lo 64539c13c20SShubham Bansal * sbc dst_hi, dst_hi, src_hi 64639c13c20SShubham Bansal * 32 bit : 64739c13c20SShubham Bansal * sub dst_lo, dst_lo, src_lo 64839c13c20SShubham Bansal */ 64939c13c20SShubham Bansal if (!hi && is64) 65039c13c20SShubham Bansal emit(ARM_SUBS_R(dst, dst, src), ctx); 65139c13c20SShubham Bansal else if (hi && is64) 65239c13c20SShubham Bansal emit(ARM_SBC_R(dst, dst, src), ctx); 65339c13c20SShubham Bansal else 65439c13c20SShubham Bansal emit(ARM_SUB_R(dst, dst, src), ctx); 65539c13c20SShubham Bansal } 65639c13c20SShubham Bansal 65739c13c20SShubham Bansal static inline void emit_alu_r(const u8 dst, const u8 src, const bool is64, 65839c13c20SShubham Bansal const bool hi, const u8 op, struct jit_ctx *ctx){ 65939c13c20SShubham Bansal switch (BPF_OP(op)) { 66039c13c20SShubham Bansal /* dst = dst + src */ 66139c13c20SShubham Bansal case BPF_ADD: 66239c13c20SShubham Bansal emit_a32_add_r(dst, src, is64, hi, ctx); 66339c13c20SShubham Bansal break; 66439c13c20SShubham Bansal /* dst = dst - src */ 66539c13c20SShubham Bansal case BPF_SUB: 66639c13c20SShubham Bansal emit_a32_sub_r(dst, src, is64, hi, ctx); 66739c13c20SShubham Bansal break; 66839c13c20SShubham Bansal /* dst = dst | src */ 66939c13c20SShubham Bansal case BPF_OR: 67039c13c20SShubham Bansal emit(ARM_ORR_R(dst, dst, src), ctx); 67139c13c20SShubham Bansal break; 67239c13c20SShubham Bansal /* dst = dst & src */ 67339c13c20SShubham Bansal case BPF_AND: 67439c13c20SShubham Bansal emit(ARM_AND_R(dst, dst, src), ctx); 67539c13c20SShubham Bansal break; 67639c13c20SShubham Bansal /* dst = dst ^ src */ 67739c13c20SShubham Bansal case BPF_XOR: 67839c13c20SShubham Bansal emit(ARM_EOR_R(dst, dst, src), ctx); 67939c13c20SShubham Bansal break; 68039c13c20SShubham Bansal /* dst = dst * src */ 68139c13c20SShubham Bansal case BPF_MUL: 68239c13c20SShubham Bansal emit(ARM_MUL(dst, dst, src), ctx); 68339c13c20SShubham Bansal break; 68439c13c20SShubham Bansal /* dst = dst << src */ 68539c13c20SShubham Bansal case BPF_LSH: 68639c13c20SShubham Bansal emit(ARM_LSL_R(dst, dst, src), ctx); 68739c13c20SShubham Bansal break; 68839c13c20SShubham Bansal /* dst = dst >> src */ 68939c13c20SShubham Bansal case BPF_RSH: 69039c13c20SShubham Bansal emit(ARM_LSR_R(dst, dst, src), ctx); 69139c13c20SShubham Bansal break; 69239c13c20SShubham Bansal /* dst = dst >> src (signed)*/ 69339c13c20SShubham Bansal case BPF_ARSH: 69439c13c20SShubham Bansal emit(ARM_MOV_SR(dst, dst, SRTYPE_ASR, src), ctx); 69539c13c20SShubham Bansal break; 69639c13c20SShubham Bansal } 69739c13c20SShubham Bansal } 69839c13c20SShubham Bansal 69939c13c20SShubham Bansal /* ALU operation (32 bit) 70039c13c20SShubham Bansal * dst = dst (op) src 70139c13c20SShubham Bansal */ 7021c35ba12SRussell King static inline void emit_a32_alu_r(const s8 dst, const s8 src, 70339c13c20SShubham Bansal struct jit_ctx *ctx, const bool is64, 70439c13c20SShubham Bansal const bool hi, const u8 op) { 7051c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 7067a987025SRussell King s8 rn, rd; 70739c13c20SShubham Bansal 7087a987025SRussell King rn = arm_bpf_get_reg32(src, tmp[1], ctx); 7097a987025SRussell King rd = arm_bpf_get_reg32(dst, tmp[0], ctx); 71039c13c20SShubham Bansal /* ALU operation */ 7117a987025SRussell King emit_alu_r(rd, rn, is64, hi, op, ctx); 7127a987025SRussell King arm_bpf_put_reg32(dst, rd, ctx); 71339c13c20SShubham Bansal } 71439c13c20SShubham Bansal 71539c13c20SShubham Bansal /* ALU operation (64 bit) */ 7161c35ba12SRussell King static inline void emit_a32_alu_r64(const bool is64, const s8 dst[], 71747b9c3bfSRussell King const s8 src[], struct jit_ctx *ctx, 71839c13c20SShubham Bansal const u8 op) { 71947b9c3bfSRussell King emit_a32_alu_r(dst_lo, src_lo, ctx, is64, false, op); 72039c13c20SShubham Bansal if (is64) 72147b9c3bfSRussell King emit_a32_alu_r(dst_hi, src_hi, ctx, is64, true, op); 72239c13c20SShubham Bansal else 72347b9c3bfSRussell King emit_a32_mov_i(dst_hi, 0, ctx); 72439c13c20SShubham Bansal } 72539c13c20SShubham Bansal 7267a987025SRussell King /* dst = src (4 bytes)*/ 7271c35ba12SRussell King static inline void emit_a32_mov_r(const s8 dst, const s8 src, 72839c13c20SShubham Bansal struct jit_ctx *ctx) { 7291c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 7307a987025SRussell King s8 rt; 73139c13c20SShubham Bansal 7327a987025SRussell King rt = arm_bpf_get_reg32(src, tmp[0], ctx); 7337a987025SRussell King arm_bpf_put_reg32(dst, rt, ctx); 73439c13c20SShubham Bansal } 73539c13c20SShubham Bansal 73639c13c20SShubham Bansal /* dst = src */ 7371c35ba12SRussell King static inline void emit_a32_mov_r64(const bool is64, const s8 dst[], 73847b9c3bfSRussell King const s8 src[], 73947b9c3bfSRussell King struct jit_ctx *ctx) { 7408c9602d3SRussell King if (!is64) { 74147b9c3bfSRussell King emit_a32_mov_r(dst_lo, src_lo, ctx); 74239c13c20SShubham Bansal /* Zero out high 4 bytes */ 74347b9c3bfSRussell King emit_a32_mov_i(dst_hi, 0, ctx); 7448c9602d3SRussell King } else if (__LINUX_ARM_ARCH__ < 6 && 7458c9602d3SRussell King ctx->cpu_architecture < CPU_ARCH_ARMv5TE) { 7468c9602d3SRussell King /* complete 8 byte move */ 7478c9602d3SRussell King emit_a32_mov_r(dst_lo, src_lo, ctx); 7488c9602d3SRussell King emit_a32_mov_r(dst_hi, src_hi, ctx); 7498c9602d3SRussell King } else if (is_stacked(src_lo) && is_stacked(dst_lo)) { 7508c9602d3SRussell King const u8 *tmp = bpf2a32[TMP_REG_1]; 7518c9602d3SRussell King 7528c9602d3SRussell King emit(ARM_LDRD_I(tmp[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(src_lo)), ctx); 7538c9602d3SRussell King emit(ARM_STRD_I(tmp[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(dst_lo)), ctx); 7548c9602d3SRussell King } else if (is_stacked(src_lo)) { 7558c9602d3SRussell King emit(ARM_LDRD_I(dst[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(src_lo)), ctx); 7568c9602d3SRussell King } else if (is_stacked(dst_lo)) { 7578c9602d3SRussell King emit(ARM_STRD_I(src[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(dst_lo)), ctx); 7588c9602d3SRussell King } else { 7598c9602d3SRussell King emit(ARM_MOV_R(dst[0], src[0]), ctx); 7608c9602d3SRussell King emit(ARM_MOV_R(dst[1], src[1]), ctx); 76139c13c20SShubham Bansal } 76239c13c20SShubham Bansal } 76339c13c20SShubham Bansal 76439c13c20SShubham Bansal /* Shift operations */ 76547b9c3bfSRussell King static inline void emit_a32_alu_i(const s8 dst, const u32 val, 76639c13c20SShubham Bansal struct jit_ctx *ctx, const u8 op) { 7671c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 7687a987025SRussell King s8 rd; 76939c13c20SShubham Bansal 7707a987025SRussell King rd = arm_bpf_get_reg32(dst, tmp[0], ctx); 77139c13c20SShubham Bansal 77239c13c20SShubham Bansal /* Do shift operation */ 77339c13c20SShubham Bansal switch (op) { 77439c13c20SShubham Bansal case BPF_LSH: 77539c13c20SShubham Bansal emit(ARM_LSL_I(rd, rd, val), ctx); 77639c13c20SShubham Bansal break; 77739c13c20SShubham Bansal case BPF_RSH: 77839c13c20SShubham Bansal emit(ARM_LSR_I(rd, rd, val), ctx); 77939c13c20SShubham Bansal break; 78039c13c20SShubham Bansal case BPF_NEG: 78139c13c20SShubham Bansal emit(ARM_RSB_I(rd, rd, val), ctx); 78239c13c20SShubham Bansal break; 78339c13c20SShubham Bansal } 78439c13c20SShubham Bansal 7857a987025SRussell King arm_bpf_put_reg32(dst, rd, ctx); 78639c13c20SShubham Bansal } 78739c13c20SShubham Bansal 78839c13c20SShubham Bansal /* dst = ~dst (64 bit) */ 78947b9c3bfSRussell King static inline void emit_a32_neg64(const s8 dst[], 79039c13c20SShubham Bansal struct jit_ctx *ctx){ 7911c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 792a6eccac5SRussell King const s8 *rd; 79339c13c20SShubham Bansal 79439c13c20SShubham Bansal /* Setup Operand */ 795a6eccac5SRussell King rd = arm_bpf_get_reg64(dst, tmp, ctx); 79639c13c20SShubham Bansal 79739c13c20SShubham Bansal /* Do Negate Operation */ 798a6eccac5SRussell King emit(ARM_RSBS_I(rd[1], rd[1], 0), ctx); 799a6eccac5SRussell King emit(ARM_RSC_I(rd[0], rd[0], 0), ctx); 80039c13c20SShubham Bansal 801a6eccac5SRussell King arm_bpf_put_reg64(dst, rd, ctx); 80239c13c20SShubham Bansal } 80339c13c20SShubham Bansal 80439c13c20SShubham Bansal /* dst = dst << src */ 80547b9c3bfSRussell King static inline void emit_a32_lsh_r64(const s8 dst[], const s8 src[], 80647b9c3bfSRussell King struct jit_ctx *ctx) { 8071c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 8081c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 809a6eccac5SRussell King const s8 *rd; 810a6eccac5SRussell King s8 rt; 81139c13c20SShubham Bansal 81239c13c20SShubham Bansal /* Setup Operands */ 8137a987025SRussell King rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx); 814a6eccac5SRussell King rd = arm_bpf_get_reg64(dst, tmp, ctx); 81539c13c20SShubham Bansal 81639c13c20SShubham Bansal /* Do LSH operation */ 81739c13c20SShubham Bansal emit(ARM_SUB_I(ARM_IP, rt, 32), ctx); 81839c13c20SShubham Bansal emit(ARM_RSB_I(tmp2[0], rt, 32), ctx); 819a6eccac5SRussell King emit(ARM_MOV_SR(ARM_LR, rd[0], SRTYPE_ASL, rt), ctx); 820a6eccac5SRussell King emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[1], SRTYPE_ASL, ARM_IP), ctx); 821a6eccac5SRussell King emit(ARM_ORR_SR(ARM_IP, ARM_LR, rd[1], SRTYPE_LSR, tmp2[0]), ctx); 822a6eccac5SRussell King emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_ASL, rt), ctx); 82339c13c20SShubham Bansal 8247a987025SRussell King arm_bpf_put_reg32(dst_lo, ARM_LR, ctx); 8257a987025SRussell King arm_bpf_put_reg32(dst_hi, ARM_IP, ctx); 82639c13c20SShubham Bansal } 82739c13c20SShubham Bansal 82839c13c20SShubham Bansal /* dst = dst >> src (signed)*/ 82947b9c3bfSRussell King static inline void emit_a32_arsh_r64(const s8 dst[], const s8 src[], 83047b9c3bfSRussell King struct jit_ctx *ctx) { 8311c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 8321c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 833a6eccac5SRussell King const s8 *rd; 834a6eccac5SRussell King s8 rt; 83539c13c20SShubham Bansal 8367a987025SRussell King /* Setup Operands */ 8377a987025SRussell King rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx); 838a6eccac5SRussell King rd = arm_bpf_get_reg64(dst, tmp, ctx); 83939c13c20SShubham Bansal 84039c13c20SShubham Bansal /* Do the ARSH operation */ 84139c13c20SShubham Bansal emit(ARM_RSB_I(ARM_IP, rt, 32), ctx); 84239c13c20SShubham Bansal emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx); 843a6eccac5SRussell King emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_LSR, rt), ctx); 844a6eccac5SRussell King emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASL, ARM_IP), ctx); 84539c13c20SShubham Bansal _emit(ARM_COND_MI, ARM_B(0), ctx); 846a6eccac5SRussell King emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASR, tmp2[0]), ctx); 847a6eccac5SRussell King emit(ARM_MOV_SR(ARM_IP, rd[0], SRTYPE_ASR, rt), ctx); 8487a987025SRussell King 8497a987025SRussell King arm_bpf_put_reg32(dst_lo, ARM_LR, ctx); 8507a987025SRussell King arm_bpf_put_reg32(dst_hi, ARM_IP, ctx); 85139c13c20SShubham Bansal } 85239c13c20SShubham Bansal 85339c13c20SShubham Bansal /* dst = dst >> src */ 85447b9c3bfSRussell King static inline void emit_a32_rsh_r64(const s8 dst[], const s8 src[], 85547b9c3bfSRussell King struct jit_ctx *ctx) { 8561c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 8571c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 858a6eccac5SRussell King const s8 *rd; 859a6eccac5SRussell King s8 rt; 86039c13c20SShubham Bansal 8617a987025SRussell King /* Setup Operands */ 8627a987025SRussell King rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx); 863a6eccac5SRussell King rd = arm_bpf_get_reg64(dst, tmp, ctx); 86439c13c20SShubham Bansal 86568565a1aSWang YanQing /* Do RSH operation */ 86639c13c20SShubham Bansal emit(ARM_RSB_I(ARM_IP, rt, 32), ctx); 86739c13c20SShubham Bansal emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx); 868a6eccac5SRussell King emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_LSR, rt), ctx); 869a6eccac5SRussell King emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASL, ARM_IP), ctx); 870a6eccac5SRussell King emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_LSR, tmp2[0]), ctx); 871a6eccac5SRussell King emit(ARM_MOV_SR(ARM_IP, rd[0], SRTYPE_LSR, rt), ctx); 8727a987025SRussell King 8737a987025SRussell King arm_bpf_put_reg32(dst_lo, ARM_LR, ctx); 8747a987025SRussell King arm_bpf_put_reg32(dst_hi, ARM_IP, ctx); 87539c13c20SShubham Bansal } 87639c13c20SShubham Bansal 87739c13c20SShubham Bansal /* dst = dst << val */ 87847b9c3bfSRussell King static inline void emit_a32_lsh_i64(const s8 dst[], 87939c13c20SShubham Bansal const u32 val, struct jit_ctx *ctx){ 8801c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 8811c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 882a6eccac5SRussell King const s8 *rd; 88339c13c20SShubham Bansal 8847a987025SRussell King /* Setup operands */ 885a6eccac5SRussell King rd = arm_bpf_get_reg64(dst, tmp, ctx); 88639c13c20SShubham Bansal 88739c13c20SShubham Bansal /* Do LSH operation */ 88839c13c20SShubham Bansal if (val < 32) { 889a6eccac5SRussell King emit(ARM_MOV_SI(tmp2[0], rd[0], SRTYPE_ASL, val), ctx); 890a6eccac5SRussell King emit(ARM_ORR_SI(rd[0], tmp2[0], rd[1], SRTYPE_LSR, 32 - val), ctx); 891a6eccac5SRussell King emit(ARM_MOV_SI(rd[1], rd[1], SRTYPE_ASL, val), ctx); 89239c13c20SShubham Bansal } else { 89339c13c20SShubham Bansal if (val == 32) 894a6eccac5SRussell King emit(ARM_MOV_R(rd[0], rd[1]), ctx); 89539c13c20SShubham Bansal else 896a6eccac5SRussell King emit(ARM_MOV_SI(rd[0], rd[1], SRTYPE_ASL, val - 32), ctx); 897a6eccac5SRussell King emit(ARM_EOR_R(rd[1], rd[1], rd[1]), ctx); 89839c13c20SShubham Bansal } 89939c13c20SShubham Bansal 900a6eccac5SRussell King arm_bpf_put_reg64(dst, rd, ctx); 90139c13c20SShubham Bansal } 90239c13c20SShubham Bansal 90339c13c20SShubham Bansal /* dst = dst >> val */ 90447b9c3bfSRussell King static inline void emit_a32_rsh_i64(const s8 dst[], 90539c13c20SShubham Bansal const u32 val, struct jit_ctx *ctx) { 9061c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 9071c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 908a6eccac5SRussell King const s8 *rd; 90939c13c20SShubham Bansal 9107a987025SRussell King /* Setup operands */ 911a6eccac5SRussell King rd = arm_bpf_get_reg64(dst, tmp, ctx); 91239c13c20SShubham Bansal 91339c13c20SShubham Bansal /* Do LSR operation */ 91439c13c20SShubham Bansal if (val < 32) { 915a6eccac5SRussell King emit(ARM_MOV_SI(tmp2[1], rd[1], SRTYPE_LSR, val), ctx); 916a6eccac5SRussell King emit(ARM_ORR_SI(rd[1], tmp2[1], rd[0], SRTYPE_ASL, 32 - val), ctx); 917a6eccac5SRussell King emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_LSR, val), ctx); 91839c13c20SShubham Bansal } else if (val == 32) { 919a6eccac5SRussell King emit(ARM_MOV_R(rd[1], rd[0]), ctx); 920a6eccac5SRussell King emit(ARM_MOV_I(rd[0], 0), ctx); 92139c13c20SShubham Bansal } else { 922a6eccac5SRussell King emit(ARM_MOV_SI(rd[1], rd[0], SRTYPE_LSR, val - 32), ctx); 923a6eccac5SRussell King emit(ARM_MOV_I(rd[0], 0), ctx); 92439c13c20SShubham Bansal } 92539c13c20SShubham Bansal 926a6eccac5SRussell King arm_bpf_put_reg64(dst, rd, ctx); 92739c13c20SShubham Bansal } 92839c13c20SShubham Bansal 92939c13c20SShubham Bansal /* dst = dst >> val (signed) */ 93047b9c3bfSRussell King static inline void emit_a32_arsh_i64(const s8 dst[], 93139c13c20SShubham Bansal const u32 val, struct jit_ctx *ctx){ 9321c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 9331c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 934a6eccac5SRussell King const s8 *rd; 93539c13c20SShubham Bansal 9367a987025SRussell King /* Setup operands */ 937a6eccac5SRussell King rd = arm_bpf_get_reg64(dst, tmp, ctx); 93839c13c20SShubham Bansal 93939c13c20SShubham Bansal /* Do ARSH operation */ 94039c13c20SShubham Bansal if (val < 32) { 941a6eccac5SRussell King emit(ARM_MOV_SI(tmp2[1], rd[1], SRTYPE_LSR, val), ctx); 942a6eccac5SRussell King emit(ARM_ORR_SI(rd[1], tmp2[1], rd[0], SRTYPE_ASL, 32 - val), ctx); 943a6eccac5SRussell King emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, val), ctx); 94439c13c20SShubham Bansal } else if (val == 32) { 945a6eccac5SRussell King emit(ARM_MOV_R(rd[1], rd[0]), ctx); 946a6eccac5SRussell King emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, 31), ctx); 94739c13c20SShubham Bansal } else { 948a6eccac5SRussell King emit(ARM_MOV_SI(rd[1], rd[0], SRTYPE_ASR, val - 32), ctx); 949a6eccac5SRussell King emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, 31), ctx); 95039c13c20SShubham Bansal } 95139c13c20SShubham Bansal 952a6eccac5SRussell King arm_bpf_put_reg64(dst, rd, ctx); 95339c13c20SShubham Bansal } 95439c13c20SShubham Bansal 95547b9c3bfSRussell King static inline void emit_a32_mul_r64(const s8 dst[], const s8 src[], 95647b9c3bfSRussell King struct jit_ctx *ctx) { 9571c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 9581c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 959a6eccac5SRussell King const s8 *rd, *rt; 96039c13c20SShubham Bansal 9617a987025SRussell King /* Setup operands for multiplication */ 962a6eccac5SRussell King rd = arm_bpf_get_reg64(dst, tmp, ctx); 963a6eccac5SRussell King rt = arm_bpf_get_reg64(src, tmp2, ctx); 96439c13c20SShubham Bansal 96539c13c20SShubham Bansal /* Do Multiplication */ 966a6eccac5SRussell King emit(ARM_MUL(ARM_IP, rd[1], rt[0]), ctx); 967a6eccac5SRussell King emit(ARM_MUL(ARM_LR, rd[0], rt[1]), ctx); 96839c13c20SShubham Bansal emit(ARM_ADD_R(ARM_LR, ARM_IP, ARM_LR), ctx); 96939c13c20SShubham Bansal 970a6eccac5SRussell King emit(ARM_UMULL(ARM_IP, rd[0], rd[1], rt[1]), ctx); 971a6eccac5SRussell King emit(ARM_ADD_R(rd[0], ARM_LR, rd[0]), ctx); 9727a987025SRussell King 9737a987025SRussell King arm_bpf_put_reg32(dst_lo, ARM_IP, ctx); 974a6eccac5SRussell King arm_bpf_put_reg32(dst_hi, rd[0], ctx); 97539c13c20SShubham Bansal } 97639c13c20SShubham Bansal 97739c13c20SShubham Bansal /* *(size *)(dst + off) = src */ 978*c5eae692SRussell King static inline void emit_str_r(const s8 dst, const s8 src[], 979*c5eae692SRussell King s32 off, struct jit_ctx *ctx, const u8 sz){ 9801c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 981*c5eae692SRussell King s32 off_max; 9827a987025SRussell King s8 rd; 98339c13c20SShubham Bansal 9847a987025SRussell King rd = arm_bpf_get_reg32(dst, tmp[1], ctx); 985*c5eae692SRussell King 986*c5eae692SRussell King if (sz == BPF_H) 987*c5eae692SRussell King off_max = 0xff; 988*c5eae692SRussell King else 989*c5eae692SRussell King off_max = 0xfff; 990*c5eae692SRussell King 991*c5eae692SRussell King if (off < 0 || off > off_max) { 99247b9c3bfSRussell King emit_a32_mov_i(tmp[0], off, ctx); 993*c5eae692SRussell King emit(ARM_ADD_R(tmp[0], tmp[0], rd), ctx); 99439c13c20SShubham Bansal rd = tmp[0]; 995*c5eae692SRussell King off = 0; 99639c13c20SShubham Bansal } 99739c13c20SShubham Bansal switch (sz) { 998*c5eae692SRussell King case BPF_B: 999*c5eae692SRussell King /* Store a Byte */ 1000*c5eae692SRussell King emit(ARM_STRB_I(src_lo, rd, off), ctx); 100139c13c20SShubham Bansal break; 100239c13c20SShubham Bansal case BPF_H: 100339c13c20SShubham Bansal /* Store a HalfWord */ 1004*c5eae692SRussell King emit(ARM_STRH_I(src_lo, rd, off), ctx); 100539c13c20SShubham Bansal break; 1006*c5eae692SRussell King case BPF_W: 1007*c5eae692SRussell King /* Store a Word */ 1008*c5eae692SRussell King emit(ARM_STR_I(src_lo, rd, off), ctx); 1009*c5eae692SRussell King break; 1010*c5eae692SRussell King case BPF_DW: 1011*c5eae692SRussell King /* Store a Double Word */ 1012*c5eae692SRussell King emit(ARM_STR_I(src_lo, rd, off), ctx); 1013*c5eae692SRussell King emit(ARM_STR_I(src_hi, rd, off + 4), ctx); 101439c13c20SShubham Bansal break; 101539c13c20SShubham Bansal } 101639c13c20SShubham Bansal } 101739c13c20SShubham Bansal 101839c13c20SShubham Bansal /* dst = *(size*)(src + off) */ 101947b9c3bfSRussell King static inline void emit_ldx_r(const s8 dst[], const s8 src, 1020ec19e02bSRussell King s32 off, struct jit_ctx *ctx, const u8 sz){ 10211c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 102247b9c3bfSRussell King const s8 *rd = is_stacked(dst_lo) ? tmp : dst; 10231c35ba12SRussell King s8 rm = src; 1024ec19e02bSRussell King s32 off_max; 102539c13c20SShubham Bansal 1026ec19e02bSRussell King if (sz == BPF_H) 1027ec19e02bSRussell King off_max = 0xff; 1028ec19e02bSRussell King else 1029ec19e02bSRussell King off_max = 0xfff; 1030ec19e02bSRussell King 1031ec19e02bSRussell King if (off < 0 || off > off_max) { 103247b9c3bfSRussell King emit_a32_mov_i(tmp[0], off, ctx); 103339c13c20SShubham Bansal emit(ARM_ADD_R(tmp[0], tmp[0], src), ctx); 103439c13c20SShubham Bansal rm = tmp[0]; 1035ec19e02bSRussell King off = 0; 1036ec19e02bSRussell King } else if (rd[1] == rm) { 1037ec19e02bSRussell King emit(ARM_MOV_R(tmp[0], rm), ctx); 1038ec19e02bSRussell King rm = tmp[0]; 103939c13c20SShubham Bansal } 104039c13c20SShubham Bansal switch (sz) { 1041ec19e02bSRussell King case BPF_B: 1042ec19e02bSRussell King /* Load a Byte */ 1043ec19e02bSRussell King emit(ARM_LDRB_I(rd[1], rm, off), ctx); 1044a6eccac5SRussell King emit_a32_mov_i(rd[0], 0, ctx); 104539c13c20SShubham Bansal break; 104639c13c20SShubham Bansal case BPF_H: 104739c13c20SShubham Bansal /* Load a HalfWord */ 1048ec19e02bSRussell King emit(ARM_LDRH_I(rd[1], rm, off), ctx); 1049a6eccac5SRussell King emit_a32_mov_i(rd[0], 0, ctx); 105039c13c20SShubham Bansal break; 1051ec19e02bSRussell King case BPF_W: 1052ec19e02bSRussell King /* Load a Word */ 1053ec19e02bSRussell King emit(ARM_LDR_I(rd[1], rm, off), ctx); 1054a6eccac5SRussell King emit_a32_mov_i(rd[0], 0, ctx); 1055ec19e02bSRussell King break; 1056ec19e02bSRussell King case BPF_DW: 1057ec19e02bSRussell King /* Load a Double Word */ 1058ec19e02bSRussell King emit(ARM_LDR_I(rd[1], rm, off), ctx); 1059ec19e02bSRussell King emit(ARM_LDR_I(rd[0], rm, off + 4), ctx); 106039c13c20SShubham Bansal break; 106139c13c20SShubham Bansal } 1062a6eccac5SRussell King arm_bpf_put_reg64(dst, rd, ctx); 106339c13c20SShubham Bansal } 106439c13c20SShubham Bansal 106539c13c20SShubham Bansal /* Arithmatic Operation */ 106639c13c20SShubham Bansal static inline void emit_ar_r(const u8 rd, const u8 rt, const u8 rm, 106739c13c20SShubham Bansal const u8 rn, struct jit_ctx *ctx, u8 op) { 106839c13c20SShubham Bansal switch (op) { 106939c13c20SShubham Bansal case BPF_JSET: 107039c13c20SShubham Bansal emit(ARM_AND_R(ARM_IP, rt, rn), ctx); 107139c13c20SShubham Bansal emit(ARM_AND_R(ARM_LR, rd, rm), ctx); 107239c13c20SShubham Bansal emit(ARM_ORRS_R(ARM_IP, ARM_LR, ARM_IP), ctx); 107339c13c20SShubham Bansal break; 107439c13c20SShubham Bansal case BPF_JEQ: 107539c13c20SShubham Bansal case BPF_JNE: 107639c13c20SShubham Bansal case BPF_JGT: 107739c13c20SShubham Bansal case BPF_JGE: 107839c13c20SShubham Bansal case BPF_JLE: 107939c13c20SShubham Bansal case BPF_JLT: 108039c13c20SShubham Bansal emit(ARM_CMP_R(rd, rm), ctx); 108139c13c20SShubham Bansal _emit(ARM_COND_EQ, ARM_CMP_R(rt, rn), ctx); 108239c13c20SShubham Bansal break; 108339c13c20SShubham Bansal case BPF_JSLE: 108439c13c20SShubham Bansal case BPF_JSGT: 108539c13c20SShubham Bansal emit(ARM_CMP_R(rn, rt), ctx); 108639c13c20SShubham Bansal emit(ARM_SBCS_R(ARM_IP, rm, rd), ctx); 108739c13c20SShubham Bansal break; 108839c13c20SShubham Bansal case BPF_JSLT: 108939c13c20SShubham Bansal case BPF_JSGE: 109039c13c20SShubham Bansal emit(ARM_CMP_R(rt, rn), ctx); 109139c13c20SShubham Bansal emit(ARM_SBCS_R(ARM_IP, rd, rm), ctx); 109239c13c20SShubham Bansal break; 109339c13c20SShubham Bansal } 109439c13c20SShubham Bansal } 109539c13c20SShubham Bansal 109639c13c20SShubham Bansal static int out_offset = -1; /* initialized on the first pass of build_body() */ 109739c13c20SShubham Bansal static int emit_bpf_tail_call(struct jit_ctx *ctx) 109839c13c20SShubham Bansal { 109939c13c20SShubham Bansal 110039c13c20SShubham Bansal /* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */ 11011c35ba12SRussell King const s8 *r2 = bpf2a32[BPF_REG_2]; 11021c35ba12SRussell King const s8 *r3 = bpf2a32[BPF_REG_3]; 11031c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 11041c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 11051c35ba12SRussell King const s8 *tcc = bpf2a32[TCALL_CNT]; 1106a6eccac5SRussell King const s8 *tc; 110739c13c20SShubham Bansal const int idx0 = ctx->idx; 110839c13c20SShubham Bansal #define cur_offset (ctx->idx - idx0) 1109f4483f2cSRussell King #define jmp_offset (out_offset - (cur_offset) - 2) 1110828e2b90SRussell King u32 lo, hi; 1111a6eccac5SRussell King s8 r_array, r_index; 1112828e2b90SRussell King int off; 111339c13c20SShubham Bansal 111439c13c20SShubham Bansal /* if (index >= array->map.max_entries) 111539c13c20SShubham Bansal * goto out; 111639c13c20SShubham Bansal */ 1117828e2b90SRussell King BUILD_BUG_ON(offsetof(struct bpf_array, map.max_entries) > 1118828e2b90SRussell King ARM_INST_LDST__IMM12); 111939c13c20SShubham Bansal off = offsetof(struct bpf_array, map.max_entries); 1120b5045229SRussell King r_array = arm_bpf_get_reg32(r2[1], tmp2[0], ctx); 1121091f0248SRussell King /* index is 32-bit for arrays */ 11227a987025SRussell King r_index = arm_bpf_get_reg32(r3[1], tmp2[1], ctx); 1123b5045229SRussell King /* array->map.max_entries */ 1124b5045229SRussell King emit(ARM_LDR_I(tmp[1], r_array, off), ctx); 112539c13c20SShubham Bansal /* index >= array->map.max_entries */ 11267a987025SRussell King emit(ARM_CMP_R(r_index, tmp[1]), ctx); 112739c13c20SShubham Bansal _emit(ARM_COND_CS, ARM_B(jmp_offset), ctx); 112839c13c20SShubham Bansal 1129b5045229SRussell King /* tmp2[0] = array, tmp2[1] = index */ 1130aaffd2f5SRussell King 113139c13c20SShubham Bansal /* if (tail_call_cnt > MAX_TAIL_CALL_CNT) 113239c13c20SShubham Bansal * goto out; 113339c13c20SShubham Bansal * tail_call_cnt++; 113439c13c20SShubham Bansal */ 113539c13c20SShubham Bansal lo = (u32)MAX_TAIL_CALL_CNT; 113639c13c20SShubham Bansal hi = (u32)((u64)MAX_TAIL_CALL_CNT >> 32); 1137a6eccac5SRussell King tc = arm_bpf_get_reg64(tcc, tmp, ctx); 1138a6eccac5SRussell King emit(ARM_CMP_I(tc[0], hi), ctx); 1139a6eccac5SRussell King _emit(ARM_COND_EQ, ARM_CMP_I(tc[1], lo), ctx); 114039c13c20SShubham Bansal _emit(ARM_COND_HI, ARM_B(jmp_offset), ctx); 1141a6eccac5SRussell King emit(ARM_ADDS_I(tc[1], tc[1], 1), ctx); 1142a6eccac5SRussell King emit(ARM_ADC_I(tc[0], tc[0], 0), ctx); 1143a6eccac5SRussell King arm_bpf_put_reg64(tcc, tmp, ctx); 114439c13c20SShubham Bansal 114539c13c20SShubham Bansal /* prog = array->ptrs[index] 114639c13c20SShubham Bansal * if (prog == NULL) 114739c13c20SShubham Bansal * goto out; 114839c13c20SShubham Bansal */ 1149828e2b90SRussell King BUILD_BUG_ON(imm8m(offsetof(struct bpf_array, ptrs)) < 0); 1150828e2b90SRussell King off = imm8m(offsetof(struct bpf_array, ptrs)); 1151828e2b90SRussell King emit(ARM_ADD_I(tmp[1], r_array, off), ctx); 11522b6958efSRussell King emit(ARM_LDR_R_SI(tmp[1], tmp[1], r_index, SRTYPE_ASL, 2), ctx); 115339c13c20SShubham Bansal emit(ARM_CMP_I(tmp[1], 0), ctx); 115439c13c20SShubham Bansal _emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx); 115539c13c20SShubham Bansal 115639c13c20SShubham Bansal /* goto *(prog->bpf_func + prologue_size); */ 1157828e2b90SRussell King BUILD_BUG_ON(offsetof(struct bpf_prog, bpf_func) > 1158828e2b90SRussell King ARM_INST_LDST__IMM12); 115939c13c20SShubham Bansal off = offsetof(struct bpf_prog, bpf_func); 1160828e2b90SRussell King emit(ARM_LDR_I(tmp[1], tmp[1], off), ctx); 116139c13c20SShubham Bansal emit(ARM_ADD_I(tmp[1], tmp[1], ctx->prologue_bytes), ctx); 1162e9062481SRussell King emit_bx_r(tmp[1], ctx); 116339c13c20SShubham Bansal 116439c13c20SShubham Bansal /* out: */ 116539c13c20SShubham Bansal if (out_offset == -1) 116639c13c20SShubham Bansal out_offset = cur_offset; 116739c13c20SShubham Bansal if (cur_offset != out_offset) { 116839c13c20SShubham Bansal pr_err_once("tail_call out_offset = %d, expected %d!\n", 116939c13c20SShubham Bansal cur_offset, out_offset); 117039c13c20SShubham Bansal return -1; 117139c13c20SShubham Bansal } 117239c13c20SShubham Bansal return 0; 117339c13c20SShubham Bansal #undef cur_offset 117439c13c20SShubham Bansal #undef jmp_offset 117539c13c20SShubham Bansal } 117639c13c20SShubham Bansal 117739c13c20SShubham Bansal /* 0xabcd => 0xcdab */ 117839c13c20SShubham Bansal static inline void emit_rev16(const u8 rd, const u8 rn, struct jit_ctx *ctx) 117939c13c20SShubham Bansal { 118039c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6 11811c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 118239c13c20SShubham Bansal 118339c13c20SShubham Bansal emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx); 118439c13c20SShubham Bansal emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 8), ctx); 118539c13c20SShubham Bansal emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx); 118639c13c20SShubham Bansal emit(ARM_ORR_SI(rd, tmp2[0], tmp2[1], SRTYPE_LSL, 8), ctx); 118739c13c20SShubham Bansal #else /* ARMv6+ */ 118839c13c20SShubham Bansal emit(ARM_REV16(rd, rn), ctx); 118939c13c20SShubham Bansal #endif 119039c13c20SShubham Bansal } 119139c13c20SShubham Bansal 119239c13c20SShubham Bansal /* 0xabcdefgh => 0xghefcdab */ 119339c13c20SShubham Bansal static inline void emit_rev32(const u8 rd, const u8 rn, struct jit_ctx *ctx) 119439c13c20SShubham Bansal { 119539c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6 11961c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 119739c13c20SShubham Bansal 119839c13c20SShubham Bansal emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx); 119939c13c20SShubham Bansal emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 24), ctx); 120039c13c20SShubham Bansal emit(ARM_ORR_SI(ARM_IP, tmp2[0], tmp2[1], SRTYPE_LSL, 24), ctx); 120139c13c20SShubham Bansal 120239c13c20SShubham Bansal emit(ARM_MOV_SI(tmp2[1], rn, SRTYPE_LSR, 8), ctx); 120339c13c20SShubham Bansal emit(ARM_AND_I(tmp2[1], tmp2[1], 0xff), ctx); 120439c13c20SShubham Bansal emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 16), ctx); 120539c13c20SShubham Bansal emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx); 120639c13c20SShubham Bansal emit(ARM_MOV_SI(tmp2[0], tmp2[0], SRTYPE_LSL, 8), ctx); 120739c13c20SShubham Bansal emit(ARM_ORR_SI(tmp2[0], tmp2[0], tmp2[1], SRTYPE_LSL, 16), ctx); 120839c13c20SShubham Bansal emit(ARM_ORR_R(rd, ARM_IP, tmp2[0]), ctx); 120939c13c20SShubham Bansal 121039c13c20SShubham Bansal #else /* ARMv6+ */ 121139c13c20SShubham Bansal emit(ARM_REV(rd, rn), ctx); 121239c13c20SShubham Bansal #endif 121339c13c20SShubham Bansal } 121439c13c20SShubham Bansal 121539c13c20SShubham Bansal // push the scratch stack register on top of the stack 121696cced4eSRussell King static inline void emit_push_r64(const s8 src[], struct jit_ctx *ctx) 121739c13c20SShubham Bansal { 12181c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 121996cced4eSRussell King const s8 *rt; 122039c13c20SShubham Bansal u16 reg_set = 0; 122139c13c20SShubham Bansal 122296cced4eSRussell King rt = arm_bpf_get_reg64(src, tmp2, ctx); 122339c13c20SShubham Bansal 122496cced4eSRussell King reg_set = (1 << rt[1]) | (1 << rt[0]); 122539c13c20SShubham Bansal emit(ARM_PUSH(reg_set), ctx); 122639c13c20SShubham Bansal } 122739c13c20SShubham Bansal 122839c13c20SShubham Bansal static void build_prologue(struct jit_ctx *ctx) 122939c13c20SShubham Bansal { 12301c35ba12SRussell King const s8 r0 = bpf2a32[BPF_REG_0][1]; 12311c35ba12SRussell King const s8 r2 = bpf2a32[BPF_REG_1][1]; 12321c35ba12SRussell King const s8 r3 = bpf2a32[BPF_REG_1][0]; 12331c35ba12SRussell King const s8 r4 = bpf2a32[BPF_REG_6][1]; 12341c35ba12SRussell King const s8 fplo = bpf2a32[BPF_REG_FP][1]; 12351c35ba12SRussell King const s8 fphi = bpf2a32[BPF_REG_FP][0]; 12361c35ba12SRussell King const s8 *tcc = bpf2a32[TCALL_CNT]; 123739c13c20SShubham Bansal 123839c13c20SShubham Bansal /* Save callee saved registers. */ 123939c13c20SShubham Bansal #ifdef CONFIG_FRAME_POINTER 124002088d9bSRussell King u16 reg_set = CALLEE_PUSH_MASK | 1 << ARM_IP | 1 << ARM_PC; 124102088d9bSRussell King emit(ARM_MOV_R(ARM_IP, ARM_SP), ctx); 124239c13c20SShubham Bansal emit(ARM_PUSH(reg_set), ctx); 124339c13c20SShubham Bansal emit(ARM_SUB_I(ARM_FP, ARM_IP, 4), ctx); 124439c13c20SShubham Bansal #else 124502088d9bSRussell King emit(ARM_PUSH(CALLEE_PUSH_MASK), ctx); 124602088d9bSRussell King emit(ARM_MOV_R(ARM_FP, ARM_SP), ctx); 124739c13c20SShubham Bansal #endif 124839c13c20SShubham Bansal /* Save frame pointer for later */ 124902088d9bSRussell King emit(ARM_SUB_I(ARM_IP, ARM_SP, SCRATCH_SIZE), ctx); 125039c13c20SShubham Bansal 125139c13c20SShubham Bansal ctx->stack_size = imm8m(STACK_SIZE); 125239c13c20SShubham Bansal 125339c13c20SShubham Bansal /* Set up function call stack */ 125439c13c20SShubham Bansal emit(ARM_SUB_I(ARM_SP, ARM_SP, ctx->stack_size), ctx); 125539c13c20SShubham Bansal 125639c13c20SShubham Bansal /* Set up BPF prog stack base register */ 125747b9c3bfSRussell King emit_a32_mov_r(fplo, ARM_IP, ctx); 125847b9c3bfSRussell King emit_a32_mov_i(fphi, 0, ctx); 125939c13c20SShubham Bansal 126039c13c20SShubham Bansal /* mov r4, 0 */ 126139c13c20SShubham Bansal emit(ARM_MOV_I(r4, 0), ctx); 126239c13c20SShubham Bansal 126339c13c20SShubham Bansal /* Move BPF_CTX to BPF_R1 */ 126439c13c20SShubham Bansal emit(ARM_MOV_R(r3, r4), ctx); 126539c13c20SShubham Bansal emit(ARM_MOV_R(r2, r0), ctx); 126639c13c20SShubham Bansal /* Initialize Tail Count */ 126796cced4eSRussell King emit(ARM_STR_I(r4, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(tcc[0])), ctx); 126896cced4eSRussell King emit(ARM_STR_I(r4, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(tcc[1])), ctx); 126939c13c20SShubham Bansal /* end of prologue */ 127039c13c20SShubham Bansal } 127139c13c20SShubham Bansal 127202088d9bSRussell King /* restore callee saved registers. */ 127339c13c20SShubham Bansal static void build_epilogue(struct jit_ctx *ctx) 127439c13c20SShubham Bansal { 127539c13c20SShubham Bansal #ifdef CONFIG_FRAME_POINTER 127602088d9bSRussell King /* When using frame pointers, some additional registers need to 127702088d9bSRussell King * be loaded. */ 127802088d9bSRussell King u16 reg_set = CALLEE_POP_MASK | 1 << ARM_SP; 127902088d9bSRussell King emit(ARM_SUB_I(ARM_SP, ARM_FP, hweight16(reg_set) * 4), ctx); 128039c13c20SShubham Bansal emit(ARM_LDM(ARM_SP, reg_set), ctx); 128139c13c20SShubham Bansal #else 128239c13c20SShubham Bansal /* Restore callee saved registers. */ 128302088d9bSRussell King emit(ARM_MOV_R(ARM_SP, ARM_FP), ctx); 128402088d9bSRussell King emit(ARM_POP(CALLEE_POP_MASK), ctx); 128539c13c20SShubham Bansal #endif 128639c13c20SShubham Bansal } 128739c13c20SShubham Bansal 128839c13c20SShubham Bansal /* 128939c13c20SShubham Bansal * Convert an eBPF instruction to native instruction, i.e 129039c13c20SShubham Bansal * JITs an eBPF instruction. 129139c13c20SShubham Bansal * Returns : 129239c13c20SShubham Bansal * 0 - Successfully JITed an 8-byte eBPF instruction 129339c13c20SShubham Bansal * >0 - Successfully JITed a 16-byte eBPF instruction 129439c13c20SShubham Bansal * <0 - Failed to JIT. 129539c13c20SShubham Bansal */ 129639c13c20SShubham Bansal static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx) 129739c13c20SShubham Bansal { 129839c13c20SShubham Bansal const u8 code = insn->code; 12991c35ba12SRussell King const s8 *dst = bpf2a32[insn->dst_reg]; 13001c35ba12SRussell King const s8 *src = bpf2a32[insn->src_reg]; 13011c35ba12SRussell King const s8 *tmp = bpf2a32[TMP_REG_1]; 13021c35ba12SRussell King const s8 *tmp2 = bpf2a32[TMP_REG_2]; 130339c13c20SShubham Bansal const s16 off = insn->off; 130439c13c20SShubham Bansal const s32 imm = insn->imm; 130539c13c20SShubham Bansal const int i = insn - ctx->prog->insnsi; 130639c13c20SShubham Bansal const bool is64 = BPF_CLASS(code) == BPF_ALU64; 1307a6eccac5SRussell King const s8 *rd, *rs; 1308a6eccac5SRussell King s8 rd_lo, rt, rm, rn; 130939c13c20SShubham Bansal s32 jmp_offset; 131039c13c20SShubham Bansal 131139c13c20SShubham Bansal #define check_imm(bits, imm) do { \ 13122b589a7eSWang YanQing if ((imm) >= (1 << ((bits) - 1)) || \ 13132b589a7eSWang YanQing (imm) < -(1 << ((bits) - 1))) { \ 131439c13c20SShubham Bansal pr_info("[%2d] imm=%d(0x%x) out of range\n", \ 131539c13c20SShubham Bansal i, imm, imm); \ 131639c13c20SShubham Bansal return -EINVAL; \ 131739c13c20SShubham Bansal } \ 131839c13c20SShubham Bansal } while (0) 131939c13c20SShubham Bansal #define check_imm24(imm) check_imm(24, imm) 1320ddecdfceSMircea Gherzan 132134805931SDaniel Borkmann switch (code) { 132239c13c20SShubham Bansal /* ALU operations */ 1323ddecdfceSMircea Gherzan 132439c13c20SShubham Bansal /* dst = src */ 132539c13c20SShubham Bansal case BPF_ALU | BPF_MOV | BPF_K: 132639c13c20SShubham Bansal case BPF_ALU | BPF_MOV | BPF_X: 132739c13c20SShubham Bansal case BPF_ALU64 | BPF_MOV | BPF_K: 132839c13c20SShubham Bansal case BPF_ALU64 | BPF_MOV | BPF_X: 132939c13c20SShubham Bansal switch (BPF_SRC(code)) { 133039c13c20SShubham Bansal case BPF_X: 133147b9c3bfSRussell King emit_a32_mov_r64(is64, dst, src, ctx); 133239c13c20SShubham Bansal break; 133339c13c20SShubham Bansal case BPF_K: 133439c13c20SShubham Bansal /* Sign-extend immediate value to destination reg */ 1335f9ff5018SRussell King emit_a32_mov_se_i64(is64, dst, imm, ctx); 133639c13c20SShubham Bansal break; 1337ddecdfceSMircea Gherzan } 1338ddecdfceSMircea Gherzan break; 133939c13c20SShubham Bansal /* dst = dst + src/imm */ 134039c13c20SShubham Bansal /* dst = dst - src/imm */ 134139c13c20SShubham Bansal /* dst = dst | src/imm */ 134239c13c20SShubham Bansal /* dst = dst & src/imm */ 134339c13c20SShubham Bansal /* dst = dst ^ src/imm */ 134439c13c20SShubham Bansal /* dst = dst * src/imm */ 134539c13c20SShubham Bansal /* dst = dst << src */ 134639c13c20SShubham Bansal /* dst = dst >> src */ 134734805931SDaniel Borkmann case BPF_ALU | BPF_ADD | BPF_K: 134834805931SDaniel Borkmann case BPF_ALU | BPF_ADD | BPF_X: 134934805931SDaniel Borkmann case BPF_ALU | BPF_SUB | BPF_K: 135034805931SDaniel Borkmann case BPF_ALU | BPF_SUB | BPF_X: 135134805931SDaniel Borkmann case BPF_ALU | BPF_OR | BPF_K: 135234805931SDaniel Borkmann case BPF_ALU | BPF_OR | BPF_X: 135334805931SDaniel Borkmann case BPF_ALU | BPF_AND | BPF_K: 135434805931SDaniel Borkmann case BPF_ALU | BPF_AND | BPF_X: 135539c13c20SShubham Bansal case BPF_ALU | BPF_XOR | BPF_K: 135639c13c20SShubham Bansal case BPF_ALU | BPF_XOR | BPF_X: 135739c13c20SShubham Bansal case BPF_ALU | BPF_MUL | BPF_K: 135839c13c20SShubham Bansal case BPF_ALU | BPF_MUL | BPF_X: 135934805931SDaniel Borkmann case BPF_ALU | BPF_LSH | BPF_X: 136034805931SDaniel Borkmann case BPF_ALU | BPF_RSH | BPF_X: 136139c13c20SShubham Bansal case BPF_ALU | BPF_ARSH | BPF_K: 136239c13c20SShubham Bansal case BPF_ALU | BPF_ARSH | BPF_X: 136339c13c20SShubham Bansal case BPF_ALU64 | BPF_ADD | BPF_K: 136439c13c20SShubham Bansal case BPF_ALU64 | BPF_ADD | BPF_X: 136539c13c20SShubham Bansal case BPF_ALU64 | BPF_SUB | BPF_K: 136639c13c20SShubham Bansal case BPF_ALU64 | BPF_SUB | BPF_X: 136739c13c20SShubham Bansal case BPF_ALU64 | BPF_OR | BPF_K: 136839c13c20SShubham Bansal case BPF_ALU64 | BPF_OR | BPF_X: 136939c13c20SShubham Bansal case BPF_ALU64 | BPF_AND | BPF_K: 137039c13c20SShubham Bansal case BPF_ALU64 | BPF_AND | BPF_X: 137139c13c20SShubham Bansal case BPF_ALU64 | BPF_XOR | BPF_K: 137239c13c20SShubham Bansal case BPF_ALU64 | BPF_XOR | BPF_X: 137339c13c20SShubham Bansal switch (BPF_SRC(code)) { 137439c13c20SShubham Bansal case BPF_X: 137547b9c3bfSRussell King emit_a32_alu_r64(is64, dst, src, ctx, BPF_OP(code)); 1376ddecdfceSMircea Gherzan break; 137739c13c20SShubham Bansal case BPF_K: 137839c13c20SShubham Bansal /* Move immediate value to the temporary register 137939c13c20SShubham Bansal * and then do the ALU operation on the temporary 138039c13c20SShubham Bansal * register as this will sign-extend the immediate 138139c13c20SShubham Bansal * value into temporary reg and then it would be 138239c13c20SShubham Bansal * safe to do the operation on it. 138339c13c20SShubham Bansal */ 1384f9ff5018SRussell King emit_a32_mov_se_i64(is64, tmp2, imm, ctx); 138547b9c3bfSRussell King emit_a32_alu_r64(is64, dst, tmp2, ctx, BPF_OP(code)); 138639c13c20SShubham Bansal break; 138739c13c20SShubham Bansal } 138839c13c20SShubham Bansal break; 138939c13c20SShubham Bansal /* dst = dst / src(imm) */ 139039c13c20SShubham Bansal /* dst = dst % src(imm) */ 139139c13c20SShubham Bansal case BPF_ALU | BPF_DIV | BPF_K: 139239c13c20SShubham Bansal case BPF_ALU | BPF_DIV | BPF_X: 139339c13c20SShubham Bansal case BPF_ALU | BPF_MOD | BPF_K: 139439c13c20SShubham Bansal case BPF_ALU | BPF_MOD | BPF_X: 1395a6eccac5SRussell King rd_lo = arm_bpf_get_reg32(dst_lo, tmp2[1], ctx); 139639c13c20SShubham Bansal switch (BPF_SRC(code)) { 139739c13c20SShubham Bansal case BPF_X: 13987a987025SRussell King rt = arm_bpf_get_reg32(src_lo, tmp2[0], ctx); 139939c13c20SShubham Bansal break; 140039c13c20SShubham Bansal case BPF_K: 140139c13c20SShubham Bansal rt = tmp2[0]; 140247b9c3bfSRussell King emit_a32_mov_i(rt, imm, ctx); 140347b9c3bfSRussell King break; 140447b9c3bfSRussell King default: 140547b9c3bfSRussell King rt = src_lo; 140639c13c20SShubham Bansal break; 140739c13c20SShubham Bansal } 1408a6eccac5SRussell King emit_udivmod(rd_lo, rd_lo, rt, ctx, BPF_OP(code)); 1409a6eccac5SRussell King arm_bpf_put_reg32(dst_lo, rd_lo, ctx); 141047b9c3bfSRussell King emit_a32_mov_i(dst_hi, 0, ctx); 141139c13c20SShubham Bansal break; 141239c13c20SShubham Bansal case BPF_ALU64 | BPF_DIV | BPF_K: 141339c13c20SShubham Bansal case BPF_ALU64 | BPF_DIV | BPF_X: 141439c13c20SShubham Bansal case BPF_ALU64 | BPF_MOD | BPF_K: 141539c13c20SShubham Bansal case BPF_ALU64 | BPF_MOD | BPF_X: 141639c13c20SShubham Bansal goto notyet; 141739c13c20SShubham Bansal /* dst = dst >> imm */ 141839c13c20SShubham Bansal /* dst = dst << imm */ 141939c13c20SShubham Bansal case BPF_ALU | BPF_RSH | BPF_K: 142039c13c20SShubham Bansal case BPF_ALU | BPF_LSH | BPF_K: 142139c13c20SShubham Bansal if (unlikely(imm > 31)) 142239c13c20SShubham Bansal return -EINVAL; 142339c13c20SShubham Bansal if (imm) 142447b9c3bfSRussell King emit_a32_alu_i(dst_lo, imm, ctx, BPF_OP(code)); 142547b9c3bfSRussell King emit_a32_mov_i(dst_hi, 0, ctx); 142639c13c20SShubham Bansal break; 142739c13c20SShubham Bansal /* dst = dst << imm */ 142839c13c20SShubham Bansal case BPF_ALU64 | BPF_LSH | BPF_K: 142939c13c20SShubham Bansal if (unlikely(imm > 63)) 143039c13c20SShubham Bansal return -EINVAL; 143147b9c3bfSRussell King emit_a32_lsh_i64(dst, imm, ctx); 143239c13c20SShubham Bansal break; 143339c13c20SShubham Bansal /* dst = dst >> imm */ 143439c13c20SShubham Bansal case BPF_ALU64 | BPF_RSH | BPF_K: 143539c13c20SShubham Bansal if (unlikely(imm > 63)) 143639c13c20SShubham Bansal return -EINVAL; 143747b9c3bfSRussell King emit_a32_rsh_i64(dst, imm, ctx); 143839c13c20SShubham Bansal break; 143939c13c20SShubham Bansal /* dst = dst << src */ 144039c13c20SShubham Bansal case BPF_ALU64 | BPF_LSH | BPF_X: 144147b9c3bfSRussell King emit_a32_lsh_r64(dst, src, ctx); 144239c13c20SShubham Bansal break; 144339c13c20SShubham Bansal /* dst = dst >> src */ 144439c13c20SShubham Bansal case BPF_ALU64 | BPF_RSH | BPF_X: 144547b9c3bfSRussell King emit_a32_rsh_r64(dst, src, ctx); 144639c13c20SShubham Bansal break; 144739c13c20SShubham Bansal /* dst = dst >> src (signed) */ 144839c13c20SShubham Bansal case BPF_ALU64 | BPF_ARSH | BPF_X: 144947b9c3bfSRussell King emit_a32_arsh_r64(dst, src, ctx); 145039c13c20SShubham Bansal break; 145139c13c20SShubham Bansal /* dst = dst >> imm (signed) */ 145239c13c20SShubham Bansal case BPF_ALU64 | BPF_ARSH | BPF_K: 145339c13c20SShubham Bansal if (unlikely(imm > 63)) 145439c13c20SShubham Bansal return -EINVAL; 145547b9c3bfSRussell King emit_a32_arsh_i64(dst, imm, ctx); 145639c13c20SShubham Bansal break; 145739c13c20SShubham Bansal /* dst = ~dst */ 145834805931SDaniel Borkmann case BPF_ALU | BPF_NEG: 145947b9c3bfSRussell King emit_a32_alu_i(dst_lo, 0, ctx, BPF_OP(code)); 146047b9c3bfSRussell King emit_a32_mov_i(dst_hi, 0, ctx); 1461ddecdfceSMircea Gherzan break; 146239c13c20SShubham Bansal /* dst = ~dst (64 bit) */ 146339c13c20SShubham Bansal case BPF_ALU64 | BPF_NEG: 146447b9c3bfSRussell King emit_a32_neg64(dst, ctx); 1465ddecdfceSMircea Gherzan break; 146639c13c20SShubham Bansal /* dst = dst * src/imm */ 146739c13c20SShubham Bansal case BPF_ALU64 | BPF_MUL | BPF_X: 146839c13c20SShubham Bansal case BPF_ALU64 | BPF_MUL | BPF_K: 146939c13c20SShubham Bansal switch (BPF_SRC(code)) { 147039c13c20SShubham Bansal case BPF_X: 147147b9c3bfSRussell King emit_a32_mul_r64(dst, src, ctx); 1472ddecdfceSMircea Gherzan break; 147339c13c20SShubham Bansal case BPF_K: 147439c13c20SShubham Bansal /* Move immediate value to the temporary register 147539c13c20SShubham Bansal * and then do the multiplication on it as this 147639c13c20SShubham Bansal * will sign-extend the immediate value into temp 147739c13c20SShubham Bansal * reg then it would be safe to do the operation 147839c13c20SShubham Bansal * on it. 14795bf705b4SNicolas Schichan */ 1480f9ff5018SRussell King emit_a32_mov_se_i64(is64, tmp2, imm, ctx); 148147b9c3bfSRussell King emit_a32_mul_r64(dst, tmp2, ctx); 148239c13c20SShubham Bansal break; 14835bf705b4SNicolas Schichan } 1484ddecdfceSMircea Gherzan break; 148539c13c20SShubham Bansal /* dst = htole(dst) */ 148639c13c20SShubham Bansal /* dst = htobe(dst) */ 148739c13c20SShubham Bansal case BPF_ALU | BPF_END | BPF_FROM_LE: 148839c13c20SShubham Bansal case BPF_ALU | BPF_END | BPF_FROM_BE: 1489a6eccac5SRussell King rd = arm_bpf_get_reg64(dst, tmp, ctx); 149039c13c20SShubham Bansal if (BPF_SRC(code) == BPF_FROM_LE) 149139c13c20SShubham Bansal goto emit_bswap_uxt; 149239c13c20SShubham Bansal switch (imm) { 149339c13c20SShubham Bansal case 16: 1494a6eccac5SRussell King emit_rev16(rd[1], rd[1], ctx); 149539c13c20SShubham Bansal goto emit_bswap_uxt; 149639c13c20SShubham Bansal case 32: 1497a6eccac5SRussell King emit_rev32(rd[1], rd[1], ctx); 149839c13c20SShubham Bansal goto emit_bswap_uxt; 149939c13c20SShubham Bansal case 64: 1500a6eccac5SRussell King emit_rev32(ARM_LR, rd[1], ctx); 1501a6eccac5SRussell King emit_rev32(rd[1], rd[0], ctx); 1502a6eccac5SRussell King emit(ARM_MOV_R(rd[0], ARM_LR), ctx); 1503bf0098f2SDaniel Borkmann break; 150439c13c20SShubham Bansal } 150539c13c20SShubham Bansal goto exit; 150639c13c20SShubham Bansal emit_bswap_uxt: 150739c13c20SShubham Bansal switch (imm) { 150839c13c20SShubham Bansal case 16: 150939c13c20SShubham Bansal /* zero-extend 16 bits into 64 bits */ 151039c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6 151147b9c3bfSRussell King emit_a32_mov_i(tmp2[1], 0xffff, ctx); 1512a6eccac5SRussell King emit(ARM_AND_R(rd[1], rd[1], tmp2[1]), ctx); 151339c13c20SShubham Bansal #else /* ARMv6+ */ 1514a6eccac5SRussell King emit(ARM_UXTH(rd[1], rd[1]), ctx); 15151447f93fSNicolas Schichan #endif 1516a6eccac5SRussell King emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx); 15171447f93fSNicolas Schichan break; 151839c13c20SShubham Bansal case 32: 151939c13c20SShubham Bansal /* zero-extend 32 bits into 64 bits */ 1520a6eccac5SRussell King emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx); 1521ddecdfceSMircea Gherzan break; 152239c13c20SShubham Bansal case 64: 152339c13c20SShubham Bansal /* nop */ 152439c13c20SShubham Bansal break; 152539c13c20SShubham Bansal } 152639c13c20SShubham Bansal exit: 1527a6eccac5SRussell King arm_bpf_put_reg64(dst, rd, ctx); 152839c13c20SShubham Bansal break; 152939c13c20SShubham Bansal /* dst = imm64 */ 153039c13c20SShubham Bansal case BPF_LD | BPF_IMM | BPF_DW: 153139c13c20SShubham Bansal { 1532f9ff5018SRussell King u64 val = (u32)imm | (u64)insn[1].imm << 32; 1533303249abSNicolas Schichan 1534f9ff5018SRussell King emit_a32_mov_i64(dst, val, ctx); 153539c13c20SShubham Bansal 153639c13c20SShubham Bansal return 1; 153739c13c20SShubham Bansal } 153839c13c20SShubham Bansal /* LDX: dst = *(size *)(src + off) */ 153939c13c20SShubham Bansal case BPF_LDX | BPF_MEM | BPF_W: 154039c13c20SShubham Bansal case BPF_LDX | BPF_MEM | BPF_H: 154139c13c20SShubham Bansal case BPF_LDX | BPF_MEM | BPF_B: 154239c13c20SShubham Bansal case BPF_LDX | BPF_MEM | BPF_DW: 15437a987025SRussell King rn = arm_bpf_get_reg32(src_lo, tmp2[1], ctx); 154447b9c3bfSRussell King emit_ldx_r(dst, rn, off, ctx, BPF_SIZE(code)); 154539c13c20SShubham Bansal break; 154639c13c20SShubham Bansal /* ST: *(size *)(dst + off) = imm */ 154739c13c20SShubham Bansal case BPF_ST | BPF_MEM | BPF_W: 154839c13c20SShubham Bansal case BPF_ST | BPF_MEM | BPF_H: 154939c13c20SShubham Bansal case BPF_ST | BPF_MEM | BPF_B: 155039c13c20SShubham Bansal case BPF_ST | BPF_MEM | BPF_DW: 155139c13c20SShubham Bansal switch (BPF_SIZE(code)) { 155239c13c20SShubham Bansal case BPF_DW: 155339c13c20SShubham Bansal /* Sign-extend immediate value into temp reg */ 1554f9ff5018SRussell King emit_a32_mov_se_i64(true, tmp2, imm, ctx); 155539c13c20SShubham Bansal break; 155639c13c20SShubham Bansal case BPF_W: 155739c13c20SShubham Bansal case BPF_H: 155839c13c20SShubham Bansal case BPF_B: 155947b9c3bfSRussell King emit_a32_mov_i(tmp2[1], imm, ctx); 156039c13c20SShubham Bansal break; 156139c13c20SShubham Bansal } 1562*c5eae692SRussell King emit_str_r(dst_lo, tmp2, off, ctx, BPF_SIZE(code)); 156339c13c20SShubham Bansal break; 156439c13c20SShubham Bansal /* STX XADD: lock *(u32 *)(dst + off) += src */ 156539c13c20SShubham Bansal case BPF_STX | BPF_XADD | BPF_W: 156639c13c20SShubham Bansal /* STX XADD: lock *(u64 *)(dst + off) += src */ 156739c13c20SShubham Bansal case BPF_STX | BPF_XADD | BPF_DW: 156839c13c20SShubham Bansal goto notyet; 156939c13c20SShubham Bansal /* STX: *(size *)(dst + off) = src */ 157039c13c20SShubham Bansal case BPF_STX | BPF_MEM | BPF_W: 157139c13c20SShubham Bansal case BPF_STX | BPF_MEM | BPF_H: 157239c13c20SShubham Bansal case BPF_STX | BPF_MEM | BPF_B: 157339c13c20SShubham Bansal case BPF_STX | BPF_MEM | BPF_DW: 1574a6eccac5SRussell King rs = arm_bpf_get_reg64(src, tmp2, ctx); 1575*c5eae692SRussell King emit_str_r(dst_lo, rs, off, ctx, BPF_SIZE(code)); 157639c13c20SShubham Bansal break; 157739c13c20SShubham Bansal /* PC += off if dst == src */ 157839c13c20SShubham Bansal /* PC += off if dst > src */ 157939c13c20SShubham Bansal /* PC += off if dst >= src */ 158039c13c20SShubham Bansal /* PC += off if dst < src */ 158139c13c20SShubham Bansal /* PC += off if dst <= src */ 158239c13c20SShubham Bansal /* PC += off if dst != src */ 158339c13c20SShubham Bansal /* PC += off if dst > src (signed) */ 158439c13c20SShubham Bansal /* PC += off if dst >= src (signed) */ 158539c13c20SShubham Bansal /* PC += off if dst < src (signed) */ 158639c13c20SShubham Bansal /* PC += off if dst <= src (signed) */ 158739c13c20SShubham Bansal /* PC += off if dst & src */ 158839c13c20SShubham Bansal case BPF_JMP | BPF_JEQ | BPF_X: 158939c13c20SShubham Bansal case BPF_JMP | BPF_JGT | BPF_X: 159039c13c20SShubham Bansal case BPF_JMP | BPF_JGE | BPF_X: 159139c13c20SShubham Bansal case BPF_JMP | BPF_JNE | BPF_X: 159239c13c20SShubham Bansal case BPF_JMP | BPF_JSGT | BPF_X: 159339c13c20SShubham Bansal case BPF_JMP | BPF_JSGE | BPF_X: 159439c13c20SShubham Bansal case BPF_JMP | BPF_JSET | BPF_X: 159539c13c20SShubham Bansal case BPF_JMP | BPF_JLE | BPF_X: 159639c13c20SShubham Bansal case BPF_JMP | BPF_JLT | BPF_X: 159739c13c20SShubham Bansal case BPF_JMP | BPF_JSLT | BPF_X: 159839c13c20SShubham Bansal case BPF_JMP | BPF_JSLE | BPF_X: 159939c13c20SShubham Bansal /* Setup source registers */ 16007a987025SRussell King rm = arm_bpf_get_reg32(src_hi, tmp2[0], ctx); 16017a987025SRussell King rn = arm_bpf_get_reg32(src_lo, tmp2[1], ctx); 160239c13c20SShubham Bansal goto go_jmp; 160339c13c20SShubham Bansal /* PC += off if dst == imm */ 160439c13c20SShubham Bansal /* PC += off if dst > imm */ 160539c13c20SShubham Bansal /* PC += off if dst >= imm */ 160639c13c20SShubham Bansal /* PC += off if dst < imm */ 160739c13c20SShubham Bansal /* PC += off if dst <= imm */ 160839c13c20SShubham Bansal /* PC += off if dst != imm */ 160939c13c20SShubham Bansal /* PC += off if dst > imm (signed) */ 161039c13c20SShubham Bansal /* PC += off if dst >= imm (signed) */ 161139c13c20SShubham Bansal /* PC += off if dst < imm (signed) */ 161239c13c20SShubham Bansal /* PC += off if dst <= imm (signed) */ 161339c13c20SShubham Bansal /* PC += off if dst & imm */ 161439c13c20SShubham Bansal case BPF_JMP | BPF_JEQ | BPF_K: 161539c13c20SShubham Bansal case BPF_JMP | BPF_JGT | BPF_K: 161639c13c20SShubham Bansal case BPF_JMP | BPF_JGE | BPF_K: 161739c13c20SShubham Bansal case BPF_JMP | BPF_JNE | BPF_K: 161839c13c20SShubham Bansal case BPF_JMP | BPF_JSGT | BPF_K: 161939c13c20SShubham Bansal case BPF_JMP | BPF_JSGE | BPF_K: 162039c13c20SShubham Bansal case BPF_JMP | BPF_JSET | BPF_K: 162139c13c20SShubham Bansal case BPF_JMP | BPF_JLT | BPF_K: 162239c13c20SShubham Bansal case BPF_JMP | BPF_JLE | BPF_K: 162339c13c20SShubham Bansal case BPF_JMP | BPF_JSLT | BPF_K: 162439c13c20SShubham Bansal case BPF_JMP | BPF_JSLE | BPF_K: 162539c13c20SShubham Bansal if (off == 0) 162639c13c20SShubham Bansal break; 162739c13c20SShubham Bansal rm = tmp2[0]; 162839c13c20SShubham Bansal rn = tmp2[1]; 162939c13c20SShubham Bansal /* Sign-extend immediate value */ 1630f9ff5018SRussell King emit_a32_mov_se_i64(true, tmp2, imm, ctx); 163139c13c20SShubham Bansal go_jmp: 163239c13c20SShubham Bansal /* Setup destination register */ 1633a6eccac5SRussell King rd = arm_bpf_get_reg64(dst, tmp, ctx); 163439c13c20SShubham Bansal 163539c13c20SShubham Bansal /* Check for the condition */ 1636a6eccac5SRussell King emit_ar_r(rd[0], rd[1], rm, rn, ctx, BPF_OP(code)); 163739c13c20SShubham Bansal 163839c13c20SShubham Bansal /* Setup JUMP instruction */ 163939c13c20SShubham Bansal jmp_offset = bpf2a32_offset(i+off, i, ctx); 164039c13c20SShubham Bansal switch (BPF_OP(code)) { 164139c13c20SShubham Bansal case BPF_JNE: 164239c13c20SShubham Bansal case BPF_JSET: 164339c13c20SShubham Bansal _emit(ARM_COND_NE, ARM_B(jmp_offset), ctx); 164439c13c20SShubham Bansal break; 164539c13c20SShubham Bansal case BPF_JEQ: 164639c13c20SShubham Bansal _emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx); 164739c13c20SShubham Bansal break; 164839c13c20SShubham Bansal case BPF_JGT: 164939c13c20SShubham Bansal _emit(ARM_COND_HI, ARM_B(jmp_offset), ctx); 165039c13c20SShubham Bansal break; 165139c13c20SShubham Bansal case BPF_JGE: 165239c13c20SShubham Bansal _emit(ARM_COND_CS, ARM_B(jmp_offset), ctx); 165339c13c20SShubham Bansal break; 165439c13c20SShubham Bansal case BPF_JSGT: 165539c13c20SShubham Bansal _emit(ARM_COND_LT, ARM_B(jmp_offset), ctx); 165639c13c20SShubham Bansal break; 165739c13c20SShubham Bansal case BPF_JSGE: 165839c13c20SShubham Bansal _emit(ARM_COND_GE, ARM_B(jmp_offset), ctx); 165939c13c20SShubham Bansal break; 166039c13c20SShubham Bansal case BPF_JLE: 166139c13c20SShubham Bansal _emit(ARM_COND_LS, ARM_B(jmp_offset), ctx); 166239c13c20SShubham Bansal break; 166339c13c20SShubham Bansal case BPF_JLT: 166439c13c20SShubham Bansal _emit(ARM_COND_CC, ARM_B(jmp_offset), ctx); 166539c13c20SShubham Bansal break; 166639c13c20SShubham Bansal case BPF_JSLT: 166739c13c20SShubham Bansal _emit(ARM_COND_LT, ARM_B(jmp_offset), ctx); 166839c13c20SShubham Bansal break; 166939c13c20SShubham Bansal case BPF_JSLE: 167039c13c20SShubham Bansal _emit(ARM_COND_GE, ARM_B(jmp_offset), ctx); 167139c13c20SShubham Bansal break; 167239c13c20SShubham Bansal } 167339c13c20SShubham Bansal break; 167439c13c20SShubham Bansal /* JMP OFF */ 167539c13c20SShubham Bansal case BPF_JMP | BPF_JA: 167639c13c20SShubham Bansal { 167739c13c20SShubham Bansal if (off == 0) 167839c13c20SShubham Bansal break; 167939c13c20SShubham Bansal jmp_offset = bpf2a32_offset(i+off, i, ctx); 168039c13c20SShubham Bansal check_imm24(jmp_offset); 168139c13c20SShubham Bansal emit(ARM_B(jmp_offset), ctx); 168239c13c20SShubham Bansal break; 168339c13c20SShubham Bansal } 168439c13c20SShubham Bansal /* tail call */ 168539c13c20SShubham Bansal case BPF_JMP | BPF_TAIL_CALL: 168639c13c20SShubham Bansal if (emit_bpf_tail_call(ctx)) 168739c13c20SShubham Bansal return -EFAULT; 168839c13c20SShubham Bansal break; 168939c13c20SShubham Bansal /* function call */ 169039c13c20SShubham Bansal case BPF_JMP | BPF_CALL: 169139c13c20SShubham Bansal { 16921c35ba12SRussell King const s8 *r0 = bpf2a32[BPF_REG_0]; 16931c35ba12SRussell King const s8 *r1 = bpf2a32[BPF_REG_1]; 16941c35ba12SRussell King const s8 *r2 = bpf2a32[BPF_REG_2]; 16951c35ba12SRussell King const s8 *r3 = bpf2a32[BPF_REG_3]; 16961c35ba12SRussell King const s8 *r4 = bpf2a32[BPF_REG_4]; 16971c35ba12SRussell King const s8 *r5 = bpf2a32[BPF_REG_5]; 169839c13c20SShubham Bansal const u32 func = (u32)__bpf_call_base + (u32)imm; 169939c13c20SShubham Bansal 170047b9c3bfSRussell King emit_a32_mov_r64(true, r0, r1, ctx); 170147b9c3bfSRussell King emit_a32_mov_r64(true, r1, r2, ctx); 170296cced4eSRussell King emit_push_r64(r5, ctx); 170396cced4eSRussell King emit_push_r64(r4, ctx); 170496cced4eSRussell King emit_push_r64(r3, ctx); 170539c13c20SShubham Bansal 170647b9c3bfSRussell King emit_a32_mov_i(tmp[1], func, ctx); 170739c13c20SShubham Bansal emit_blx_r(tmp[1], ctx); 170839c13c20SShubham Bansal 170939c13c20SShubham Bansal emit(ARM_ADD_I(ARM_SP, ARM_SP, imm8m(24)), ctx); // callee clean 171039c13c20SShubham Bansal break; 171139c13c20SShubham Bansal } 171239c13c20SShubham Bansal /* function return */ 171339c13c20SShubham Bansal case BPF_JMP | BPF_EXIT: 171439c13c20SShubham Bansal /* Optimization: when last instruction is EXIT 171539c13c20SShubham Bansal * simply fallthrough to epilogue. 171639c13c20SShubham Bansal */ 171739c13c20SShubham Bansal if (i == ctx->prog->len - 1) 171839c13c20SShubham Bansal break; 171939c13c20SShubham Bansal jmp_offset = epilogue_offset(ctx); 172039c13c20SShubham Bansal check_imm24(jmp_offset); 172139c13c20SShubham Bansal emit(ARM_B(jmp_offset), ctx); 172239c13c20SShubham Bansal break; 172339c13c20SShubham Bansal notyet: 172439c13c20SShubham Bansal pr_info_once("*** NOT YET: opcode %02x ***\n", code); 172539c13c20SShubham Bansal return -EFAULT; 172639c13c20SShubham Bansal default: 172739c13c20SShubham Bansal pr_err_once("unknown opcode %02x\n", code); 172839c13c20SShubham Bansal return -EINVAL; 1729ddecdfceSMircea Gherzan } 17300b59d880SNicolas Schichan 17310b59d880SNicolas Schichan if (ctx->flags & FLAG_IMM_OVERFLOW) 17320b59d880SNicolas Schichan /* 17330b59d880SNicolas Schichan * this instruction generated an overflow when 17340b59d880SNicolas Schichan * trying to access the literal pool, so 17350b59d880SNicolas Schichan * delegate this filter to the kernel interpreter. 17360b59d880SNicolas Schichan */ 17370b59d880SNicolas Schichan return -1; 173839c13c20SShubham Bansal return 0; 1739ddecdfceSMircea Gherzan } 1740ddecdfceSMircea Gherzan 174139c13c20SShubham Bansal static int build_body(struct jit_ctx *ctx) 174239c13c20SShubham Bansal { 174339c13c20SShubham Bansal const struct bpf_prog *prog = ctx->prog; 174439c13c20SShubham Bansal unsigned int i; 174539c13c20SShubham Bansal 174639c13c20SShubham Bansal for (i = 0; i < prog->len; i++) { 174739c13c20SShubham Bansal const struct bpf_insn *insn = &(prog->insnsi[i]); 174839c13c20SShubham Bansal int ret; 174939c13c20SShubham Bansal 175039c13c20SShubham Bansal ret = build_insn(insn, ctx); 175139c13c20SShubham Bansal 175239c13c20SShubham Bansal /* It's used with loading the 64 bit immediate value. */ 175339c13c20SShubham Bansal if (ret > 0) { 175439c13c20SShubham Bansal i++; 1755ddecdfceSMircea Gherzan if (ctx->target == NULL) 175639c13c20SShubham Bansal ctx->offsets[i] = ctx->idx; 175739c13c20SShubham Bansal continue; 175839c13c20SShubham Bansal } 175939c13c20SShubham Bansal 176039c13c20SShubham Bansal if (ctx->target == NULL) 176139c13c20SShubham Bansal ctx->offsets[i] = ctx->idx; 176239c13c20SShubham Bansal 176339c13c20SShubham Bansal /* If unsuccesfull, return with error code */ 176439c13c20SShubham Bansal if (ret) 176539c13c20SShubham Bansal return ret; 176639c13c20SShubham Bansal } 176739c13c20SShubham Bansal return 0; 176839c13c20SShubham Bansal } 176939c13c20SShubham Bansal 177039c13c20SShubham Bansal static int validate_code(struct jit_ctx *ctx) 177139c13c20SShubham Bansal { 177239c13c20SShubham Bansal int i; 177339c13c20SShubham Bansal 177439c13c20SShubham Bansal for (i = 0; i < ctx->idx; i++) { 177539c13c20SShubham Bansal if (ctx->target[i] == __opcode_to_mem_arm(ARM_INST_UDF)) 177639c13c20SShubham Bansal return -1; 177739c13c20SShubham Bansal } 1778ddecdfceSMircea Gherzan 1779ddecdfceSMircea Gherzan return 0; 1780ddecdfceSMircea Gherzan } 1781ddecdfceSMircea Gherzan 178239c13c20SShubham Bansal void bpf_jit_compile(struct bpf_prog *prog) 1783ddecdfceSMircea Gherzan { 178439c13c20SShubham Bansal /* Nothing to do here. We support Internal BPF. */ 178539c13c20SShubham Bansal } 1786ddecdfceSMircea Gherzan 178739c13c20SShubham Bansal struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) 178839c13c20SShubham Bansal { 178939c13c20SShubham Bansal struct bpf_prog *tmp, *orig_prog = prog; 179039c13c20SShubham Bansal struct bpf_binary_header *header; 179139c13c20SShubham Bansal bool tmp_blinded = false; 179239c13c20SShubham Bansal struct jit_ctx ctx; 179339c13c20SShubham Bansal unsigned int tmp_idx; 179439c13c20SShubham Bansal unsigned int image_size; 179539c13c20SShubham Bansal u8 *image_ptr; 179639c13c20SShubham Bansal 179739c13c20SShubham Bansal /* If BPF JIT was not enabled then we must fall back to 179839c13c20SShubham Bansal * the interpreter. 179939c13c20SShubham Bansal */ 180060b58afcSAlexei Starovoitov if (!prog->jit_requested) 180139c13c20SShubham Bansal return orig_prog; 180239c13c20SShubham Bansal 180339c13c20SShubham Bansal /* If constant blinding was enabled and we failed during blinding 180439c13c20SShubham Bansal * then we must fall back to the interpreter. Otherwise, we save 180539c13c20SShubham Bansal * the new JITed code. 180639c13c20SShubham Bansal */ 180739c13c20SShubham Bansal tmp = bpf_jit_blind_constants(prog); 180839c13c20SShubham Bansal 180939c13c20SShubham Bansal if (IS_ERR(tmp)) 181039c13c20SShubham Bansal return orig_prog; 181139c13c20SShubham Bansal if (tmp != prog) { 181239c13c20SShubham Bansal tmp_blinded = true; 181339c13c20SShubham Bansal prog = tmp; 181439c13c20SShubham Bansal } 1815ddecdfceSMircea Gherzan 1816ddecdfceSMircea Gherzan memset(&ctx, 0, sizeof(ctx)); 181739c13c20SShubham Bansal ctx.prog = prog; 18188c9602d3SRussell King ctx.cpu_architecture = cpu_architecture(); 1819ddecdfceSMircea Gherzan 182039c13c20SShubham Bansal /* Not able to allocate memory for offsets[] , then 182139c13c20SShubham Bansal * we must fall back to the interpreter 182239c13c20SShubham Bansal */ 182339c13c20SShubham Bansal ctx.offsets = kcalloc(prog->len, sizeof(int), GFP_KERNEL); 182439c13c20SShubham Bansal if (ctx.offsets == NULL) { 182539c13c20SShubham Bansal prog = orig_prog; 1826ddecdfceSMircea Gherzan goto out; 182739c13c20SShubham Bansal } 182839c13c20SShubham Bansal 182939c13c20SShubham Bansal /* 1) fake pass to find in the length of the JITed code, 183039c13c20SShubham Bansal * to compute ctx->offsets and other context variables 183139c13c20SShubham Bansal * needed to compute final JITed code. 183239c13c20SShubham Bansal * Also, calculate random starting pointer/start of JITed code 183339c13c20SShubham Bansal * which is prefixed by random number of fault instructions. 183439c13c20SShubham Bansal * 183539c13c20SShubham Bansal * If the first pass fails then there is no chance of it 183639c13c20SShubham Bansal * being successful in the second pass, so just fall back 183739c13c20SShubham Bansal * to the interpreter. 183839c13c20SShubham Bansal */ 183939c13c20SShubham Bansal if (build_body(&ctx)) { 184039c13c20SShubham Bansal prog = orig_prog; 184139c13c20SShubham Bansal goto out_off; 184239c13c20SShubham Bansal } 1843ddecdfceSMircea Gherzan 1844ddecdfceSMircea Gherzan tmp_idx = ctx.idx; 1845ddecdfceSMircea Gherzan build_prologue(&ctx); 1846ddecdfceSMircea Gherzan ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4; 1847ddecdfceSMircea Gherzan 184839c13c20SShubham Bansal ctx.epilogue_offset = ctx.idx; 184939c13c20SShubham Bansal 1850ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7 1851ddecdfceSMircea Gherzan tmp_idx = ctx.idx; 1852ddecdfceSMircea Gherzan build_epilogue(&ctx); 1853ddecdfceSMircea Gherzan ctx.epilogue_bytes = (ctx.idx - tmp_idx) * 4; 1854ddecdfceSMircea Gherzan 1855ddecdfceSMircea Gherzan ctx.idx += ctx.imm_count; 1856ddecdfceSMircea Gherzan if (ctx.imm_count) { 185739c13c20SShubham Bansal ctx.imms = kcalloc(ctx.imm_count, sizeof(u32), GFP_KERNEL); 185839c13c20SShubham Bansal if (ctx.imms == NULL) { 185939c13c20SShubham Bansal prog = orig_prog; 186039c13c20SShubham Bansal goto out_off; 186139c13c20SShubham Bansal } 1862ddecdfceSMircea Gherzan } 1863ddecdfceSMircea Gherzan #else 186439c13c20SShubham Bansal /* there's nothing about the epilogue on ARMv7 */ 1865ddecdfceSMircea Gherzan build_epilogue(&ctx); 1866ddecdfceSMircea Gherzan #endif 186739c13c20SShubham Bansal /* Now we can get the actual image size of the JITed arm code. 186839c13c20SShubham Bansal * Currently, we are not considering the THUMB-2 instructions 186939c13c20SShubham Bansal * for jit, although it can decrease the size of the image. 187039c13c20SShubham Bansal * 187139c13c20SShubham Bansal * As each arm instruction is of length 32bit, we are translating 187239c13c20SShubham Bansal * number of JITed intructions into the size required to store these 187339c13c20SShubham Bansal * JITed code. 187439c13c20SShubham Bansal */ 187539c13c20SShubham Bansal image_size = sizeof(u32) * ctx.idx; 1876ddecdfceSMircea Gherzan 187739c13c20SShubham Bansal /* Now we know the size of the structure to make */ 187839c13c20SShubham Bansal header = bpf_jit_binary_alloc(image_size, &image_ptr, 187939c13c20SShubham Bansal sizeof(u32), jit_fill_hole); 188039c13c20SShubham Bansal /* Not able to allocate memory for the structure then 188139c13c20SShubham Bansal * we must fall back to the interpretation 188239c13c20SShubham Bansal */ 188339c13c20SShubham Bansal if (header == NULL) { 188439c13c20SShubham Bansal prog = orig_prog; 188539c13c20SShubham Bansal goto out_imms; 188639c13c20SShubham Bansal } 188739c13c20SShubham Bansal 188839c13c20SShubham Bansal /* 2.) Actual pass to generate final JIT code */ 188939c13c20SShubham Bansal ctx.target = (u32 *) image_ptr; 1890ddecdfceSMircea Gherzan ctx.idx = 0; 189155309dd3SDaniel Borkmann 1892ddecdfceSMircea Gherzan build_prologue(&ctx); 189339c13c20SShubham Bansal 189439c13c20SShubham Bansal /* If building the body of the JITed code fails somehow, 189539c13c20SShubham Bansal * we fall back to the interpretation. 189639c13c20SShubham Bansal */ 18970b59d880SNicolas Schichan if (build_body(&ctx) < 0) { 189839c13c20SShubham Bansal image_ptr = NULL; 18990b59d880SNicolas Schichan bpf_jit_binary_free(header); 190039c13c20SShubham Bansal prog = orig_prog; 190139c13c20SShubham Bansal goto out_imms; 19020b59d880SNicolas Schichan } 1903ddecdfceSMircea Gherzan build_epilogue(&ctx); 1904ddecdfceSMircea Gherzan 190539c13c20SShubham Bansal /* 3.) Extra pass to validate JITed Code */ 190639c13c20SShubham Bansal if (validate_code(&ctx)) { 190739c13c20SShubham Bansal image_ptr = NULL; 190839c13c20SShubham Bansal bpf_jit_binary_free(header); 190939c13c20SShubham Bansal prog = orig_prog; 191039c13c20SShubham Bansal goto out_imms; 191139c13c20SShubham Bansal } 1912ebaef649SDaniel Borkmann flush_icache_range((u32)header, (u32)(ctx.target + ctx.idx)); 1913ddecdfceSMircea Gherzan 191439c13c20SShubham Bansal if (bpf_jit_enable > 1) 191539c13c20SShubham Bansal /* there are 2 passes here */ 191639c13c20SShubham Bansal bpf_jit_dump(prog->len, image_size, 2, ctx.target); 191739c13c20SShubham Bansal 191818d405afSDaniel Borkmann bpf_jit_binary_lock_ro(header); 191939c13c20SShubham Bansal prog->bpf_func = (void *)ctx.target; 192039c13c20SShubham Bansal prog->jited = 1; 192139c13c20SShubham Bansal prog->jited_len = image_size; 192239c13c20SShubham Bansal 192339c13c20SShubham Bansal out_imms: 1924ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7 1925ddecdfceSMircea Gherzan if (ctx.imm_count) 1926ddecdfceSMircea Gherzan kfree(ctx.imms); 1927ddecdfceSMircea Gherzan #endif 192839c13c20SShubham Bansal out_off: 1929ddecdfceSMircea Gherzan kfree(ctx.offsets); 193039c13c20SShubham Bansal out: 193139c13c20SShubham Bansal if (tmp_blinded) 193239c13c20SShubham Bansal bpf_jit_prog_release_other(prog, prog == orig_prog ? 193339c13c20SShubham Bansal tmp : orig_prog); 193439c13c20SShubham Bansal return prog; 1935ddecdfceSMircea Gherzan } 1936ddecdfceSMircea Gherzan 1937