RFC: arm: eBPF JIT compiler
Subsystems (3)
PATCH 1/1 RFC: arm: eBPF JIT compiler
The JIT compiler emits ARM 32 bit instructions. Currently, It supports eBPF only. Classic BPF is supported because of the conversion by BPF core.
JIT is enabled with
echo 1 > /proc/sys/net/core/bpf_jit_enable
Constant Blinding can be enabled along with JIT using
echo 1 > /proc/sys/net/core/bpf_jit_enable echo 2 > /proc/sys/net/core/bpf_jit_harden
See Documentation/networking/filter.txt for more information. Tested on ARMv7 with CONFIG_FRAME_POINTER enabled.
Results:
1. Interpreter:
[ 93.551176] test_bpf: Summary: 314 PASSED, 0 FAILED, [0/306 JIT'ed]
2. JIT enabled:
[ 92.913931] test_bpf: Summary: 314 PASSED, 0 FAILED, [278/306 JIT'ed]
3. JIT + blinding enabled:
[ 109.414506] test_bpf: Summary: 314 PASSED, 0 FAILED, [278/306 JIT'ed]
Currently, following eBPF instructions are not JITed.
BPF_ALU64 | BPF_DIV | BPF_K BPF_ALU64 | BPF_DIV | BPF_X BPF_ALU64 | BPF_MOD | BPF_K BPF_ALU64 | BPF_MOD | BPF_X BPF_STX | BPF_XADD | BPF_W BPF_STX | BPF_XADD | BPF_DW BPF_JMP | BPF_CALL
--- arch/arm/net/bpf_jit_32.c | 2410 ++++++++++++++++++++++++++++++--------------- arch/arm/net/bpf_jit_32.h | 108 +- 2 files changed, 1716 insertions(+), 802 deletions(-)
arch/arm/net/bpf_jit_32.c +1633 -777
@@ -1,13 +1,16 @@/* -* Just-In-Time compiler for BPF filters on 32bit ARM +* Just-In-Time compiler for eBPF filters on 32bit ARM * * Copyright (c) 2011 Mircea Gherzan <[email protected]> +* Copyright (c) 2017 Shubham Bansal <[email protected]> * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; version 2 of the License. */ +#define pr_fmt (fmt) "bpf_jit: " fmt +#include <linux/bpf.h> #include <linux/bitops.h> #include <linux/compiler.h> #include <linux/errno.h>
@@ -23,44 +26,91 @@#include "bpf_jit_32.h" +int bpf_jit_enable__read_mostly ; + +#define STACK_OFFSET (k) (k) +#define TMP_REG_1 (MAX_BPF_JIT_REG + 0)/* TEMP Register 1 */ +#define TMP_REG_2 (MAX_BPF_JIT_REG + 1)/* TEMP Register 2 */ +#define TCALL_CNT (MAX_BPF_JIT_REG + 2)/* Tail Call Count */ + +/* Flags used for JIT optimization */ +#define SEEN_CALL (1 << 0) + +#define FLAG_IMM_OVERFLOW (1 << 0) +/* -* ABI: +* Map eBPF registers to ARM 32bit registers or stack scratch space. +* +* 1. First argument is passed using the arm 32bit registers and rest of the +* arguments are passed on stack scratch space. +* 2. First callee-saved aregument is mapped to arm 32 bit registers and rest +* arguments are mapped to scratch space on stack. +* 3. We need two 64 bit temp registers to do complex operations on eBPF +* registers. +* +* As the eBPF registers are all 64 bit registers and arm has only 32 bit +* registers, we have to map each eBPF registers with two arm 32 bit regs or +* scratch memory space and we have to build eBPF 64 bit register from those. * -* r0 scratch register -* r4 BPF register A -* r5 BPF register X -* r6 pointer to the skb -* r7 skb->data -* r8 skb_headlen(skb) */ +static const u8 bpf2a32[][2 ] = { +/* return value from in-kernel function, and exit value from eBPF */ + [BPF_REG_0 ] = {ARM_R1 ,ARM_R0 }, +/* arguments from eBPF program to in-kernel function */ + [BPF_REG_1 ] = {ARM_R3 ,ARM_R2 }, +/* Stored on stack scratch space */ + [BPF_REG_2 ] = {STACK_OFFSET (0 ),STACK_OFFSET (4 )}, + [BPF_REG_3 ] = {STACK_OFFSET (8 ),STACK_OFFSET (12 )}, + [BPF_REG_4 ] = {STACK_OFFSET (16 ),STACK_OFFSET (20 )}, + [BPF_REG_5 ] = {STACK_OFFSET (24 ),STACK_OFFSET (28 )}, +/* callee saved registers that in-kernel function will preserve */ + [BPF_REG_6 ] = {ARM_R5 ,ARM_R4 }, +/* Stored on stack scratch space */ + [BPF_REG_7 ] = {STACK_OFFSET (32 ),STACK_OFFSET (36 )}, + [BPF_REG_8 ] = {STACK_OFFSET (40 ),STACK_OFFSET (44 )}, + [BPF_REG_9 ] = {STACK_OFFSET (48 ),STACK_OFFSET (52 )}, +/* Read only Frame Pointer to access Stack */ + [BPF_REG_FP ] = {STACK_OFFSET (56 ),STACK_OFFSET (60 )}, +/* Temperory Register for internal BPF JIT, can be used +* for constant blindings and others. +*/ + [TMP_REG_1 ] = {ARM_R7 ,ARM_R6 }, + [TMP_REG_2 ] = {ARM_R10 ,ARM_R8 }, +/* Tail call count. Stored on stack scratch space. */ + [TCALL_CNT ] = {STACK_OFFSET (64 ),STACK_OFFSET (68 )}, +/* temporary register for blinding constants. +* Stored on stack scratch space. +*/ + [BPF_REG_AX ] = {STACK_OFFSET (72 ),STACK_OFFSET (76 )}, +}; -#define r_scratch ARM_R0 -/* r1-r3 are (also) used for the unaligned loads on the non-ARMv7 slowpath */ -#define r_off ARM_R1 -#define r_A ARM_R4 -#define r_X ARM_R5 -#define r_skb ARM_R6 -#define r_skb_data ARM_R7 -#define r_skb_hl ARM_R8 - -#define SCRATCH_SP_OFFSET 0 -#define SCRATCH_OFF (k) (SCRATCH_SP_OFFSET + 4 * (k)) - -#define SEEN_MEM ((1 << BPF_MEMWORDS) - 1) -#define SEEN_MEM_WORD (k) (1 << (k)) -#define SEEN_X (1 << BPF_MEMWORDS) -#define SEEN_CALL (1 << (BPF_MEMWORDS + 1)) -#define SEEN_SKB (1 << (BPF_MEMWORDS + 2)) -#define SEEN_DATA (1 << (BPF_MEMWORDS + 3)) +#define dst_lo dst[1] +#define dst_hi dst[0] +#define src_lo src[1] +#define src_hi src[0] -#define FLAG_NEED_X_RESET (1 << 0) -#define FLAG_IMM_OVERFLOW (1 << 1) +/* +* JIT Context: +* +* prog : bpf_prog +* idx : index of current last JITed instruction. +* prologue_bytes : bytes used in prologue. +* epilogue_offset : offset of epilogue starting. +* seen : bit mask used for JIT optimization. +* offsets : array of eBPF instruction offsets in +* JITed code. +* target : final JITed code. +* epilogue_bytes : no of bytes used in epilogue. +* imm_count : no of immediate counts used for global +* variables. +* imms : array of global variable addresses. +*/ struct jit_ctx { -const struct bpf_prog *skf; -unsigned idx; -unsigned prologue_bytes; -int ret0_fp_idx; +const struct bpf_prog *prog; +unsigned int idx; +unsigned int prologue_bytes; +unsigned int epilogue_offset;u32 seen;u32 flags;u32 *offsets;
@@ -72,68 +122,16 @@ struct jit_ctx {#endif }; -int bpf_jit_enable__read_mostly ; - -static inline int call_neg_helper (struct sk_buff *skb,int offset,void *ret, -unsigned int size) -{ -void *ptr =bpf_internal_load_pointer_neg_helper (skb, offset, size); - -if (!ptr) -return -EFAULT ; -memcpy (ret, ptr, size); -return 0 ; -} - -static u64 jit_get_skb_b (struct sk_buff *skb,int offset) -{ -u8 ret; -int err; - -if (offset <0 ) - err =call_neg_helper (skb, offset, &ret,1 ); -else - err =skb_copy_bits (skb, offset, &ret,1 ); - -return (u64 )err <<32 | ret; -} - -static u64 jit_get_skb_h (struct sk_buff *skb,int offset) -{ -u16 ret; -int err; - -if (offset <0 ) - err =call_neg_helper (skb, offset, &ret,2 ); -else - err =skb_copy_bits (skb, offset, &ret,2 ); - -return (u64 )err <<32 |ntohs (ret); -} - -static u64 jit_get_skb_w (struct sk_buff *skb,int offset) -{ -u32 ret; -int err; - -if (offset <0 ) - err =call_neg_helper (skb, offset, &ret,4 ); -else - err =skb_copy_bits (skb, offset, &ret,4 ); - -return (u64 )err <<32 |ntohl (ret); -} -/* * Wrappers which handle both OABI and EABI and assures Thumb2 interworking * (where the assembly routines like __aeabi_uidiv could cause problems). */ -static u32 jit_udiv (u32 dividend,u32 divisor) +static u32 jit_udiv32 (u32 dividend,u32 divisor) {return dividend / divisor; } -static u32 jit_mod (u32 dividend,u32 divisor) +static u32 jit_mod32 (u32 dividend,u32 divisor) {return dividend % divisor; }
@@ -157,36 +155,22 @@ static inline void emit(u32 inst, struct jit_ctx *ctx)_emit (ARM_COND_AL , inst, ctx); } -static u16 saved_regs (struct jit_ctx *ctx) +/* +* Checks if immediate value can be converted to imm12(12 bits) value. +*/ +static int16_t imm8m (u32 x) { -u16 ret =0 ; - -if ((ctx->skf->len >1 ) || - (ctx->skf->insns[0 ].code == (BPF_RET |BPF_A ))) - ret |=1 << r_A; - -#ifdef CONFIG_FRAME_POINTER - ret |= (1 <<ARM_FP ) | (1 <<ARM_IP ) | (1 <<ARM_LR ) | (1 <<ARM_PC ); -#else -if (ctx->seen &SEEN_CALL ) - ret |=1 <<ARM_LR ; -#endif -if (ctx->seen & (SEEN_DATA |SEEN_SKB )) - ret |=1 << r_skb; -if (ctx->seen &SEEN_DATA ) - ret |= (1 << r_skb_data) | (1 << r_skb_hl); -if (ctx->seen &SEEN_X ) - ret |=1 << r_X; - -return ret; -} +u32 rot; -static inline int mem_words_used (struct jit_ctx *ctx) -{ -/* yes, we do waste some stack space IF there are "holes" in the set" */ -return fls (ctx->seen &SEEN_MEM ); +for (rot =0 ; rot <16 ; rot++) +if ((x & ~ror32 (0xff ,2 * rot)) ==0 ) +return rol32 (x,2 * rot) | (rot <<8 ); +return -1 ; } +/* +* Initializes the JIT space with undefined instructions. +*/ static void jit_fill_hole (void *area,unsigned int size) {u32 *ptr;
@@ -195,88 +179,34 @@ static void jit_fill_hole(void *area, unsigned int size) *ptr++ =__opcode_to_mem_arm (ARM_INST_UDF ); } -static void build_prologue (struct jit_ctx *ctx) -{ -u16 reg_set =saved_regs (ctx); -u16 off; - -#ifdef CONFIG_FRAME_POINTER -emit (ARM_MOV_R (ARM_IP ,ARM_SP ), ctx); -emit (ARM_PUSH (reg_set), ctx); -emit (ARM_SUB_I (ARM_FP ,ARM_IP ,4 ), ctx); -#else -if (reg_set) -emit (ARM_PUSH (reg_set), ctx); -#endif +/* Stack must be multiples of 16 Bytes */ +#define STACK_ALIGN (sz) (((sz) + 15) & ~15) -if (ctx->seen & (SEEN_DATA |SEEN_SKB )) -emit (ARM_MOV_R (r_skb,ARM_R0 ), ctx); - -if (ctx->seen &SEEN_DATA ) { - off = offsetof(struct sk_buff , data); -emit (ARM_LDR_I (r_skb_data, r_skb, off), ctx); -/* headlen = len - data_len */ - off = offsetof(struct sk_buff , len); -emit (ARM_LDR_I (r_skb_hl, r_skb, off), ctx); - off = offsetof(struct sk_buff , data_len); -emit (ARM_LDR_I (r_scratch, r_skb, off), ctx); -emit (ARM_SUB_R (r_skb_hl, r_skb_hl, r_scratch), ctx); - } - -if (ctx->flags &FLAG_NEED_X_RESET ) -emit (ARM_MOV_I (r_X,0 ), ctx); - -/* do not leak kernel data to userspace */ -if (bpf_needs_clear_a (&ctx->skf->insns[0 ])) -emit (ARM_MOV_I (r_A,0 ), ctx); - -/* stack space for the BPF_MEM words */ -if (ctx->seen &SEEN_MEM ) -emit (ARM_SUB_I (ARM_SP ,ARM_SP ,mem_words_used (ctx) *4 ), ctx); -} - -static void build_epilogue (struct jit_ctx *ctx) -{ -u16 reg_set =saved_regs (ctx); - -if (ctx->seen &SEEN_MEM ) -emit (ARM_ADD_I (ARM_SP ,ARM_SP ,mem_words_used (ctx) *4 ), ctx); - - reg_set &= ~(1 <<ARM_LR ); - -#ifdef CONFIG_FRAME_POINTER -/* the first instruction of the prologue was: mov ip, sp */ - reg_set &= ~(1 <<ARM_IP ); - reg_set |= (1 <<ARM_SP ); -emit (ARM_LDM (ARM_SP , reg_set), ctx); -#else -if (reg_set) { -if (ctx->seen &SEEN_CALL ) - reg_set |=1 <<ARM_PC ; -emit (ARM_POP (reg_set), ctx); - } +/* Stack space for BPF_REG_2, BPF_REG_3, BPF_REG_4, +* BPF_REG_5, BPF_REG_7, BPF_REG_8, BPF_REG_9, +* BPF_REG_FP and Tail call counts. +*/ +#define SCRATCH_SIZE 80 -if (!(ctx->seen &SEEN_CALL )) -emit (ARM_BX (ARM_LR ), ctx); -#endif -} +/* total stack size used in JITed code */ +#define _STACK_SIZE \ + (MAX_BPF_STACK + \ + + SCRATCH_SIZE + \ + + 4 /* extra for skb_copy_bits buffer */) -static int16_t imm8m (u32 x) -{ -u32 rot; +#define STACK_SIZE STACK_ALIGN(_STACK_SIZE) -for (rot =0 ; rot <16 ; rot++) -if ((x & ~ror32 (0xff ,2 * rot)) ==0 ) -return rol32 (x,2 * rot) | (rot <<8 ); +/* Get the offset of eBPF REGISTERs stored on scratch space. */ +#define STACK_VAR (off) (STACK_SIZE-off-4) -return -1 ; -} +/* Offset of skb_copy_bits buffer */ +#define SKB_BUFFER STACK_VAR(SCRATCH_SIZE)#if __LINUX_ARM_ARCH__ <7 static u16 imm_offset (u32 k,struct jit_ctx *ctx) { -unsigned i =0 , offset; +unsigned int i =0 , offset;u16 imm;/* on the "fake" run we just count them (duplicates included) */
@@ -295,7 +225,7 @@ static u16 imm_offset(u32 k, struct jit_ctx *ctx) ctx->imms[i] = k;/* constants go just after the epilogue */ - offset = ctx->offsets[ctx->skf->len]; + offset = ctx->offsets[ctx->prog->len]; offset += ctx->prologue_bytes; offset += ctx->epilogue_bytes; offset += i *4 ;
@@ -319,10 +249,22 @@ static u16 imm_offset(u32 k, struct jit_ctx *ctx)#endif /* __LINUX_ARM_ARCH__ */ +static inline int bpf2a32_offset (int bpf_to,int bpf_from, +const struct jit_ctx *ctx) { +int to, from; + +if (ctx->target ==NULL ) +return 0 ; + to = ctx->offsets[bpf_to]; + from = ctx->offsets[bpf_from]; + +return to - from -1 ; +} +/* * Move an immediate that's not an imm8m to a core register. */ -static inline void emit_mov_i_no8m (int rd,u32 val,struct jit_ctx *ctx) +static inline void emit_mov_i_no8m (const u8 rd,u32 val,struct jit_ctx *ctx) {#if __LINUX_ARM_ARCH__ <7 emit (ARM_LDR_I (rd,ARM_PC ,imm_offset (val, ctx)), ctx);
@@ -333,7 +275,7 @@ static inline void emit_mov_i_no8m(int rd, u32 val, struct jit_ctx *ctx)#endif } -static inline void emit_mov_i (int rd,u32 val,struct jit_ctx *ctx) +static inline void emit_mov_i (const u8 rd,u32 val,struct jit_ctx *ctx) {int imm12 =imm8m (val);
@@ -343,676 +285,1559 @@ static inline void emit_mov_i(int rd, u32 val, struct jit_ctx *ctx)emit_mov_i_no8m (rd, val, ctx); } -#if __LINUX_ARM_ARCH__ <6 - -static void emit_load_be32 (u8 cond,u8 r_res,u8 r_addr,struct jit_ctx *ctx) +static inline void emit_blx_r (u8 tgt_reg,struct jit_ctx *ctx) { -_emit (cond,ARM_LDRB_I (ARM_R3 , r_addr,1 ), ctx); -_emit (cond,ARM_LDRB_I (ARM_R1 , r_addr,0 ), ctx); -_emit (cond,ARM_LDRB_I (ARM_R2 , r_addr,3 ), ctx); -_emit (cond,ARM_LSL_I (ARM_R3 ,ARM_R3 ,16 ), ctx); -_emit (cond,ARM_LDRB_I (ARM_R0 , r_addr,2 ), ctx); -_emit (cond,ARM_ORR_S (ARM_R3 ,ARM_R3 ,ARM_R1 ,SRTYPE_LSL ,24 ), ctx); -_emit (cond,ARM_ORR_R (ARM_R3 ,ARM_R3 ,ARM_R2 ), ctx); -_emit (cond,ARM_ORR_S (r_res,ARM_R3 ,ARM_R0 ,SRTYPE_LSL ,8 ), ctx); + ctx->seen |=SEEN_CALL ; +#if __LINUX_ARM_ARCH__ <5 +emit (ARM_MOV_R (ARM_LR ,ARM_PC ), ctx); + +if (elf_hwcap &HWCAP_THUMB ) +emit (ARM_BX (tgt_reg), ctx); +else +emit (ARM_MOV_R (ARM_PC , tgt_reg), ctx); +#else +emit (ARM_BLX_R (tgt_reg), ctx); +#endif } -static void emit_load_be16 (u8 cond,u8 r_res,u8 r_addr,struct jit_ctx *ctx) +static inline int epilogue_offset (const struct jit_ctx *ctx) { -_emit (cond,ARM_LDRB_I (ARM_R1 , r_addr,0 ), ctx); -_emit (cond,ARM_LDRB_I (ARM_R2 , r_addr,1 ), ctx); -_emit (cond,ARM_ORR_S (r_res,ARM_R2 ,ARM_R1 ,SRTYPE_LSL ,8 ), ctx); +int to, from; +/* No need for 1st dummy run */ +if (ctx->target ==NULL ) +return 0 ; + to = ctx->epilogue_offset; + from = ctx->idx; + +return to - from -2 ; } -static inline void emit_swap16 (u8 r_dst,u8 r_src,struct jit_ctx *ctx) +static inline void emit_udivmod (u8 rd,u8 rm,u8 rn,struct jit_ctx *ctx,u8 op) { -/* r_dst = (r_src << 8) | (r_src >> 8) */ -emit (ARM_LSL_I (ARM_R1 , r_src,8 ), ctx); -emit (ARM_ORR_S (r_dst,ARM_R1 , r_src,SRTYPE_LSR ,8 ), ctx); +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +s32 jmp_offset; + +/* checks if divisor is zero or not. If it is, then +* exit directly. +*/ +emit (ARM_CMP_I (rn,0 ), ctx); +_emit (ARM_COND_EQ ,ARM_MOV_I (ARM_R0 ,0 ), ctx); + jmp_offset =epilogue_offset (ctx); +_emit (ARM_COND_EQ ,ARM_B (jmp_offset), ctx); +#if __LINUX_ARM_ARCH__ ==7 +if (elf_hwcap &HWCAP_IDIVA ) { +if (op ==BPF_DIV ) +emit (ARM_UDIV (rd, rm, rn), ctx); +else { +emit (ARM_UDIV (ARM_IP , rm, rn), ctx); +emit (ARM_MLS (rd, rn,ARM_IP , rm), ctx); + } +return ; + } +#endif /* -* we need to mask out the bits set in r_dst[23:16] due to -* the first shift instruction. -* -* note that 0x8ff is the encoded immediate 0x00ff0000. +* For BPF_ALU | BPF_DIV | BPF_K instructions +* As ARM_R1 and ARM_R0 contains 1st argument of bpf +* function, we need to save it on caller side to save +* it from getting destroyed within callee. +* After the return from the callee, we restore ARM_R0 +* ARM_R1. */ -emit (ARM_BIC_I (r_dst, r_dst,0x8ff ), ctx); -} +if (rn !=ARM_R1 ) { +emit (ARM_MOV_R (tmp[0 ],ARM_R1 ), ctx); +emit (ARM_MOV_R (ARM_R1 , rn), ctx); + } +if (rm !=ARM_R0 ) { +emit (ARM_MOV_R (tmp[1 ],ARM_R0 ), ctx); +emit (ARM_MOV_R (ARM_R0 , rm), ctx); + } -#else /* ARMv6+ */ +/* Call appropriate function */ + ctx->seen |=SEEN_CALL ; +emit_mov_i (ARM_IP , op ==BPF_DIV ? + (u32 )jit_udiv32 : (u32 )jit_mod32, ctx); +emit_blx_r (ARM_IP , ctx); -static void emit_load_be32 (u8 cond,u8 r_res,u8 r_addr,struct jit_ctx *ctx) -{ -_emit (cond,ARM_LDR_I (r_res, r_addr,0 ), ctx); -#ifdef __LITTLE_ENDIAN -_emit (cond,ARM_REV (r_res, r_res), ctx); -#endif +/* Save return value */ +if (rd !=ARM_R0 ) +emit (ARM_MOV_R (rd,ARM_R0 ), ctx); + +/* Restore ARM_R0 and ARM_R1 */ +if (rn !=ARM_R1 ) +emit (ARM_MOV_R (ARM_R1 , tmp[0 ]), ctx); +if (rm !=ARM_R0 ) +emit (ARM_MOV_R (ARM_R0 , tmp[1 ]), ctx); } -static void emit_load_be16 (u8 cond,u8 r_res,u8 r_addr,struct jit_ctx *ctx) +/* Checks whether BPF register is on scratch stack space or not. */ +static inline bool is_on_stack (u8 bpf_reg) { -_emit (cond,ARM_LDRH_I (r_res, r_addr,0 ), ctx); -#ifdef __LITTLE_ENDIAN -_emit (cond,ARM_REV16 (r_res, r_res), ctx); -#endif +static u8 stack_regs[] = {BPF_REG_AX ,BPF_REG_3 ,BPF_REG_4 ,BPF_REG_5 , +BPF_REG_7 ,BPF_REG_8 ,BPF_REG_9 ,TCALL_CNT , +BPF_REG_2 ,BPF_REG_FP }; +int i, reg_len =sizeof (stack_regs); + +for (i =0 ; i < reg_len ; i++) { +if (bpf_reg == stack_regs[i]) +return true; + } +return false; } -static inline void emit_swap16 (u8 r_dst__maybe_unused , -u8 r_src__maybe_unused , -struct jit_ctx *ctx__maybe_unused ) +static inline void emit_a32_mov_i (const u8 dst,const u32 val, +bool dstk,struct jit_ctx *ctx) { -#ifdef __LITTLE_ENDIAN -emit (ARM_REV16 (r_dst, r_src), ctx); -#endif +const u8 *tmp = bpf2a32[TMP_REG_1 ]; + +if (dstk) { +emit_mov_i (tmp[1 ], val, ctx); +emit (ARM_STR_I (tmp[1 ],ARM_SP ,STACK_VAR (dst)), ctx); + }else { +emit_mov_i (dst, val, ctx); + } } -#endif /* __LINUX_ARM_ARCH__ < 6 */ +/* Sign extended move */ +static inline void emit_a32_mov_i64 (const bool is64,const u8 dst[], +const u32 val,bool dstk, +struct jit_ctx *ctx) { +u32 hi =0 ; +if (is64 && (val & (1 <<31 ))) + hi = (u32 )~0 ; +emit_a32_mov_i (dst_lo, val, dstk, ctx); +emit_a32_mov_i (dst_hi, hi, dstk, ctx); +} -/* Compute the immediate value for a PC-relative branch. */ -static inline u32 b_imm (unsigned tgt,struct jit_ctx *ctx) -{ -u32 imm; +static inline void emit_a32_add_r (const u8 dst,const u8 src, +const bool is64,const bool hi, +struct jit_ctx *ctx) { +/* 64 bit : +* adds dst_lo, dst_lo, src_lo +* adc dst_hi, dst_hi, src_hi +* 32 bit : +* add dst_lo, dst_lo, src_lo +*/ +if (!hi && is64) +emit (ARM_ADDS_R (dst, dst, src), ctx); +else if (hi && is64) +emit (ARM_ADC_R (dst, dst, src), ctx); +else +emit (ARM_ADD_R (dst, dst, src), ctx); +} -if (ctx->target ==NULL ) -return 0 ; -/* -* BPF allows only forward jumps and the offset of the target is -* still the one computed during the first pass. +static inline void emit_a32_sub_r (const u8 dst,const u8 src, +const bool is64,const bool hi, +struct jit_ctx *ctx) { +/* 64 bit : +* subs dst_lo, dst_lo, src_lo +* sbc dst_hi, dst_hi, src_hi +* 32 bit : +* sub dst_lo, dst_lo, src_lo */ - imm = ctx->offsets[tgt] + ctx->prologue_bytes - (ctx->idx *4 +8 ); +if (!hi && is64) +emit (ARM_SUBS_R (dst, dst, src), ctx); +else if (hi && is64) +emit (ARM_SBC_R (dst, dst, src), ctx); +else +emit (ARM_SUB_R (dst, dst, src), ctx); +} -return imm >>2 ; +static inline void emit_alu_r (const u8 dst,const u8 src,const bool is64, +const bool hi,const u8 op,struct jit_ctx *ctx){ +switch (BPF_OP (op)) { +/* dst = dst + src */ +case BPF_ADD : +emit_a32_add_r (dst, src, is64, hi, ctx); +break ; +/* dst = dst - src */ +case BPF_SUB : +emit_a32_sub_r (dst, src, is64, hi, ctx); +break ; +/* dst = dst | src */ +case BPF_OR : +emit (ARM_ORR_R (dst, dst, src), ctx); +break ; +/* dst = dst & src */ +case BPF_AND : +emit (ARM_AND_R (dst, dst, src), ctx); +break ; +/* dst = dst ^ src */ +case BPF_XOR : +emit (ARM_EOR_R (dst, dst, src), ctx); +break ; +/* dst = dst * src */ +case BPF_MUL : +emit (ARM_MUL (dst, dst, src), ctx); +break ; +/* dst = dst << src */ +case BPF_LSH : +emit (ARM_LSL_R (dst, dst, src), ctx); +break ; +/* dst = dst >> src */ +case BPF_RSH : +emit (ARM_LSR_R (dst, dst, src), ctx); +break ; +/* dst = dst >> src (signed)*/ +case BPF_ARSH : +emit (ARM_MOV_SR (dst, dst,SRTYPE_ASR , src), ctx); +break ; + } } -#define OP_IMM3 (op, r1, r2, imm_val, ctx) \ - do { \ - imm12 = imm8m(imm_val); \ - if (imm12 < 0) { \ - emit_mov_i_no8m(r_scratch, imm_val, ctx); \ - emit(op ## _R((r1), (r2), r_scratch), ctx); \ - } else { \ - emit(op ## _I((r1), (r2), imm12), ctx); \ - } \ - } while (0) - -static inline void emit_err_ret (u8 cond,struct jit_ctx *ctx) -{ -if (ctx->ret0_fp_idx >=0 ) { -_emit (cond,ARM_B (b_imm (ctx->ret0_fp_idx, ctx)), ctx); -/* NOP to keep the size constant between passes */ -emit (ARM_MOV_R (ARM_R0 ,ARM_R0 ), ctx); +/* ALU operation (32 bit) +* dst = dst (op) src +*/ +static inline void emit_a32_alu_r (const u8 dst,const u8 src, +bool dstk,bool sstk, +struct jit_ctx *ctx,const bool is64, +const bool hi,const u8 op) { +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +u8 rn = sstk ? tmp[1 ] : src; + +if (sstk) +emit (ARM_LDR_I (rn,ARM_SP ,STACK_VAR (src)), ctx); + +/* ALU operation */ +if (dstk) { +emit (ARM_LDR_I (tmp[0 ],ARM_SP ,STACK_VAR (dst)), ctx); +emit_alu_r (tmp[0 ], rn, is64, hi, op, ctx); +emit (ARM_STR_I (tmp[0 ],ARM_SP ,STACK_VAR (dst)), ctx); }else { -_emit (cond,ARM_MOV_I (ARM_R0 ,0 ), ctx); -_emit (cond,ARM_B (b_imm (ctx->skf->len, ctx)), ctx); +emit_alu_r (dst, rn, is64, hi, op, ctx); } } -static inline void emit_blx_r (u8 tgt_reg,struct jit_ctx *ctx) -{ -#if __LINUX_ARM_ARCH__ <5 -emit (ARM_MOV_R (ARM_LR ,ARM_PC ), ctx); +/* ALU operation (64 bit) */ +static inline void emit_a32_alu_r64 (const bool is64,const u8 dst[], +const u8 src[],bool dstk, +bool sstk,struct jit_ctx *ctx, +const u8 op) { +emit_a32_alu_r (dst_lo, src_lo, dstk, sstk, ctx, is64, false, op); +if (is64) +emit_a32_alu_r (dst_hi, src_hi, dstk, sstk, ctx, is64, true, op); +else +emit_a32_mov_i (dst_hi,0 , dstk, ctx); +} -if (elf_hwcap &HWCAP_THUMB ) -emit (ARM_BX (tgt_reg), ctx); +/* dst = imm (4 bytes)*/ +static inline void emit_a32_mov_r (const u8 dst,const u8 src, +bool dstk,bool sstk, +struct jit_ctx *ctx) { +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +u8 rt = sstk ? tmp[0 ] : src; + +if (sstk) +emit (ARM_LDR_I (tmp[0 ],ARM_SP ,STACK_VAR (src)), ctx); +if (dstk) +emit (ARM_STR_I (rt,ARM_SP ,STACK_VAR (dst)), ctx);else -emit (ARM_MOV_R (ARM_PC , tgt_reg), ctx); -#else -emit (ARM_BLX_R (tgt_reg), ctx); -#endif +emit (ARM_MOV_R (dst, rt), ctx); } -static inline void emit_udivmod (u8 rd,u8 rm,u8 rn,struct jit_ctx *ctx, -int bpf_op) -{ -#if __LINUX_ARM_ARCH__ ==7 -if (elf_hwcap &HWCAP_IDIVA ) { -if (bpf_op ==BPF_DIV ) -emit (ARM_UDIV (rd, rm, rn), ctx); -else { -emit (ARM_UDIV (ARM_R3 , rm, rn), ctx); -emit (ARM_MLS (rd, rn,ARM_R3 , rm), ctx); - } -return ; +/* dst = src */ +static inline void emit_a32_mov_r64 (const bool is64,const u8 dst[], +const u8 src[],bool dstk, +bool sstk,struct jit_ctx *ctx) { +emit_a32_mov_r (dst_lo, src_lo, dstk, sstk, ctx); +if (is64) { +/* complete 8 byte move */ +emit_a32_mov_r (dst_hi, src_hi, dstk, sstk, ctx); + }else { +/* Zero out high 4 bytes */ +emit_a32_mov_i (dst_hi,0 , dstk, ctx); } -#endif +} -/* -* For BPF_ALU | BPF_DIV | BPF_K instructions, rm is ARM_R4 -* (r_A) and rn is ARM_R0 (r_scratch) so load rn first into -* ARM_R1 to avoid accidentally overwriting ARM_R0 with rm -* before using it as a source for ARM_R1. -* -* For BPF_ALU | BPF_DIV | BPF_X rm is ARM_R4 (r_A) and rn is -* ARM_R5 (r_X) so there is no particular register overlap -* issues. -*/ -if (rn !=ARM_R1 ) -emit (ARM_MOV_R (ARM_R1 , rn), ctx); -if (rm !=ARM_R0 ) -emit (ARM_MOV_R (ARM_R0 , rm), ctx); +/* Shift operations */ +static inline void emit_a32_alu_i (const u8 dst,const u32 val,bool dstk, +struct jit_ctx *ctx,const u8 op) { +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +u8 rd = dstk ? tmp[0 ] : dst; + +if (dstk) +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst)), ctx); + +/* Do shift operation */ +switch (op) { +case BPF_LSH : +emit (ARM_LSL_I (rd, rd, val), ctx); +break ; +case BPF_RSH : +emit (ARM_LSR_I (rd, rd, val), ctx); +break ; +case BPF_NEG : +emit (ARM_RSB_I (rd, rd, val), ctx); +break ; + } + +if (dstk) +emit (ARM_STR_I (rd,ARM_SP ,STACK_VAR (dst)), ctx); +} + +/* dst = ~dst (64 bit) */ +static inline void emit_a32_neg64 (const u8 dst[],bool dstk, +struct jit_ctx *ctx){ +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +u8 rd = dstk ? tmp[1 ] : dst[1 ]; +u8 rm = dstk ? tmp[0 ] : dst[0 ]; + +/* Setup Operand */ +if (dstk) { +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_LDR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } + +/* Do Negate Operation */ +emit (ARM_RSBS_I (rd, rd,0 ), ctx); +emit (ARM_RSC_I (rm, rm,0 ), ctx); + +if (dstk) { +emit (ARM_STR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_STR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } +} + +/* dst = dst << src */ +static inline void emit_a32_lsh_r64 (const u8 dst[],const u8 src[],bool dstk, +bool sstk,struct jit_ctx *ctx) { +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; + +/* Setup Operands */ +u8 rt = sstk ? tmp2[1 ] : src_lo; +u8 rd = dstk ? tmp[1 ] : dst_lo; +u8 rm = dstk ? tmp[0 ] : dst_hi; + +if (sstk) +emit (ARM_LDR_I (rt,ARM_SP ,STACK_VAR (src_lo)), ctx); +if (dstk) { +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_LDR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } +/* Do LSH operation */ +emit (ARM_SUB_I (ARM_IP , rt,32 ), ctx); +emit (ARM_RSB_I (tmp2[0 ], rt,32 ), ctx); +/* As we are using ARM_LR */ ctx->seen |=SEEN_CALL ; -emit_mov_i (ARM_R3 , bpf_op ==BPF_DIV ? (u32 )jit_udiv : (u32 )jit_mod, - ctx); -emit_blx_r (ARM_R3 , ctx); +emit (ARM_MOV_SR (ARM_LR , rm,SRTYPE_ASL , rt), ctx); +emit (ARM_ORR_SR (ARM_LR ,ARM_LR , rd,SRTYPE_ASL ,ARM_IP ), ctx); +emit (ARM_ORR_SR (ARM_IP ,ARM_LR , rd,SRTYPE_LSR , tmp2[0 ]), ctx); +emit (ARM_MOV_SR (ARM_LR , rd,SRTYPE_ASL , rt), ctx); + +if (dstk) { +emit (ARM_STR_I (ARM_LR ,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_STR_I (ARM_IP ,ARM_SP ,STACK_VAR (dst_hi)), ctx); + }else { +emit (ARM_MOV_R (rd,ARM_LR ), ctx); +emit (ARM_MOV_R (rm,ARM_IP ), ctx); + } +} -if (rd !=ARM_R0 ) -emit (ARM_MOV_R (rd,ARM_R0 ), ctx); +/* dst = dst >> src (signed)*/ +static inline void emit_a32_arsh_r64 (const u8 dst[],const u8 src[],bool dstk, +bool sstk,struct jit_ctx *ctx) { +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; +/* Setup Operands */ +u8 rt = sstk ? tmp2[1 ] : src_lo; +u8 rd = dstk ? tmp[1 ] : dst_lo; +u8 rm = dstk ? tmp[0 ] : dst_hi; + +if (sstk) +emit (ARM_LDR_I (rt,ARM_SP ,STACK_VAR (src_lo)), ctx); +if (dstk) { +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_LDR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } + +/* Do the ARSH operation */ +emit (ARM_RSB_I (ARM_IP , rt,32 ), ctx); +emit (ARM_SUBS_I (tmp2[0 ], rt,32 ), ctx); +/* As we are using ARM_LR */ + ctx->seen |=SEEN_CALL ; +emit (ARM_MOV_SR (ARM_LR , rd,SRTYPE_LSR , rt), ctx); +emit (ARM_ORR_SR (ARM_LR ,ARM_LR , rm,SRTYPE_ASL ,ARM_IP ), ctx); +_emit (ARM_COND_MI ,ARM_B (0 ), ctx); +emit (ARM_ORR_SR (ARM_LR ,ARM_LR , rm,SRTYPE_ASR , tmp2[0 ]), ctx); +emit (ARM_MOV_SR (ARM_IP , rm,SRTYPE_ASR , rt), ctx); +if (dstk) { +emit (ARM_STR_I (ARM_LR ,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_STR_I (ARM_IP ,ARM_SP ,STACK_VAR (dst_hi)), ctx); + }else { +emit (ARM_MOV_R (rd,ARM_LR ), ctx); +emit (ARM_MOV_R (rm,ARM_IP ), ctx); + } +} + +/* dst = dst >> src */ +static inline void emit_a32_lsr_r64 (const u8 dst[],const u8 src[],bool dstk, +bool sstk,struct jit_ctx *ctx) { +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; +/* Setup Operands */ +u8 rt = sstk ? tmp2[1 ] : src_lo; +u8 rd = dstk ? tmp[1 ] : dst_lo; +u8 rm = dstk ? tmp[0 ] : dst_hi; + +if (sstk) +emit (ARM_LDR_I (rt,ARM_SP ,STACK_VAR (src_lo)), ctx); +if (dstk) { +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_LDR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } + +/* Do LSH operation */ +emit (ARM_RSB_I (ARM_IP , rt,32 ), ctx); +emit (ARM_SUBS_I (tmp2[0 ], rt,32 ), ctx); +/* As we are using ARM_LR */ + ctx->seen |=SEEN_CALL ; +emit (ARM_MOV_SR (ARM_LR , rd,SRTYPE_LSR , rt), ctx); +emit (ARM_ORR_SR (ARM_LR ,ARM_LR , rm,SRTYPE_ASL ,ARM_IP ), ctx); +emit (ARM_ORR_SR (ARM_LR ,ARM_LR , rm,SRTYPE_LSR , tmp2[0 ]), ctx); +emit (ARM_MOV_SR (ARM_IP , rm,SRTYPE_LSR , rt), ctx); +if (dstk) { +emit (ARM_STR_I (ARM_LR ,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_STR_I (ARM_IP ,ARM_SP ,STACK_VAR (dst_hi)), ctx); + }else { +emit (ARM_MOV_R (rd,ARM_LR ), ctx); +emit (ARM_MOV_R (rm,ARM_IP ), ctx); + } +} + +/* dst = dst << val */ +static inline void emit_a32_lsh_i64 (const u8 dst[],bool dstk, +const u32 val,struct jit_ctx *ctx){ +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; +/* Setup operands */ +u8 rd = dstk ? tmp[1 ] : dst_lo; +u8 rm = dstk ? tmp[0 ] : dst_hi; + +if (dstk) { +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_LDR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } + +/* Do LSH operation */ +if (val <32 ) { +emit (ARM_MOV_SI (tmp2[0 ], rm,SRTYPE_ASL , val), ctx); +emit (ARM_ORR_SI (rm, tmp2[0 ], rd,SRTYPE_LSR ,32 - val), ctx); +emit (ARM_MOV_SI (rd, rd,SRTYPE_ASL , val), ctx); + }else { +if (val ==32 ) +emit (ARM_MOV_R (rm, rd), ctx); +else +emit (ARM_MOV_SI (rm, rd,SRTYPE_ASL , val -32 ), ctx); +emit (ARM_EOR_R (rd, rd, rd), ctx); + } + +if (dstk) { +emit (ARM_STR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_STR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } +} + +/* dst = dst >> val */ +static inline void emit_a32_lsr_i64 (const u8 dst[],bool dstk, +const u32 val,struct jit_ctx *ctx) { +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; +/* Setup operands */ +u8 rd = dstk ? tmp[1 ] : dst_lo; +u8 rm = dstk ? tmp[0 ] : dst_hi; + +if (dstk) { +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_LDR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } + +/* Do LSR operation */ +if (val <32 ) { +emit (ARM_MOV_SI (tmp2[1 ], rd,SRTYPE_LSR , val), ctx); +emit (ARM_ORR_SI (rd, tmp2[1 ], rm,SRTYPE_ASL ,32 - val), ctx); +emit (ARM_MOV_SI (rm, rm,SRTYPE_LSR , val), ctx); + }else if (val ==32 ) { +emit (ARM_MOV_R (rd, rm), ctx); +emit (ARM_MOV_I (rm,0 ), ctx); + }else { +emit (ARM_MOV_SI (rd, rm,SRTYPE_LSR , val -32 ), ctx); +emit (ARM_MOV_I (rm,0 ), ctx); + } + +if (dstk) { +emit (ARM_STR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_STR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } +} + +/* dst = dst >> val (signed) */ +static inline void emit_a32_arsh_i64 (const u8 dst[],bool dstk, +const u32 val,struct jit_ctx *ctx){ +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; +/* Setup operands */ +u8 rd = dstk ? tmp[1 ] : dst_lo; +u8 rm = dstk ? tmp[0 ] : dst_hi; + +if (dstk) { +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_LDR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } + +/* Do ARSH operation */ +if (val <32 ) { +emit (ARM_MOV_SI (tmp2[1 ], rd,SRTYPE_LSR , val), ctx); +emit (ARM_ORR_SI (rd, tmp2[1 ], rm,SRTYPE_ASL ,32 - val), ctx); +emit (ARM_MOV_SI (rm, rm,SRTYPE_ASR , val), ctx); + }else if (val ==32 ) { +emit (ARM_MOV_R (rd, rm), ctx); +emit (ARM_MOV_SI (rm, rm,SRTYPE_ASR ,31 ), ctx); + }else { +emit (ARM_MOV_SI (rd, rm,SRTYPE_ASR , val -32 ), ctx); +emit (ARM_MOV_SI (rm, rm,SRTYPE_ASR ,31 ), ctx); + } + +if (dstk) { +emit (ARM_STR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_STR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } +} + +static inline void emit_a32_mul_r64 (const u8 dst[],const u8 src[],bool dstk, +bool sstk,struct jit_ctx *ctx) { +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; +/* Setup operands for multiplication */ +u8 rd = dstk ? tmp[1 ] : dst_lo; +u8 rm = dstk ? tmp[0 ] : dst_hi; +u8 rt = sstk ? tmp2[1 ] : src_lo; +u8 rn = sstk ? tmp2[0 ] : src_hi; + +if (dstk) { +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_LDR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } +if (sstk) { +emit (ARM_LDR_I (rt,ARM_SP ,STACK_VAR (src_lo)), ctx); +emit (ARM_LDR_I (rn,ARM_SP ,STACK_VAR (src_hi)), ctx); + } + +/* Do Multiplication */ +emit (ARM_MUL (ARM_IP , rd, rn), ctx); +emit (ARM_MUL (ARM_LR , rm, rt), ctx); +/* As we are using ARM_LR */ + ctx->seen |=SEEN_CALL ; +emit (ARM_ADD_R (ARM_LR ,ARM_IP ,ARM_LR ), ctx); + +emit (ARM_UMULL (ARM_IP , rm, rd, rt), ctx); +emit (ARM_ADD_R (rm,ARM_LR , rm), ctx); +if (dstk) { +emit (ARM_STR_I (ARM_IP ,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_STR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + }else { +emit (ARM_MOV_R (rd,ARM_IP ), ctx); + } } -static inline void update_on_xread (struct jit_ctx *ctx) +/* *(size *)(dst + off) = src */ +static inline void emit_str_r (const u8 dst,const u8 src,bool dstk, +const s32 off,struct jit_ctx *ctx,const u8 sz){ +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +u8 rd = dstk ? tmp[1 ] : dst; + +if (dstk) +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst)), ctx); +if (off) { +emit_a32_mov_i (tmp[0 ], off, false, ctx); +emit (ARM_ADD_R (tmp[0 ], rd, tmp[0 ]), ctx); + rd = tmp[0 ]; + } +switch (sz) { +case BPF_W : +/* Store a Word */ +emit (ARM_STR_I (src, rd,0 ), ctx); +break ; +case BPF_H : +/* Store a HalfWord */ +emit (ARM_STRH_I (src, rd,0 ), ctx); +break ; +case BPF_B : +/* Store a Byte */ +emit (ARM_STRB_I (src, rd,0 ), ctx); +break ; + } +} + +/* dst = *(size*)(src + off) */ +static inline void emit_ldx_r (const u8 dst,const u8 src,bool dstk, +const s32 off,struct jit_ctx *ctx,const u8 sz){ +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +u8 rd = dstk ? tmp[1 ] : dst; +u8 rm = src; + +if (off) { +emit_a32_mov_i (tmp[0 ], off, false, ctx); +emit (ARM_ADD_R (tmp[0 ], tmp[0 ], src), ctx); + rm = tmp[0 ]; + } +switch (sz) { +case BPF_W : +/* Load a Word */ +emit (ARM_LDR_I (rd, rm,0 ), ctx); +break ; +case BPF_H : +/* Load a HalfWord */ +emit (ARM_LDRH_I (rd, rm,0 ), ctx); +break ; +case BPF_B : +/* Load a Byte */ +emit (ARM_LDRB_I (rd, rm,0 ), ctx); +break ; + } +if (dstk) +emit (ARM_STR_I (rd,ARM_SP ,STACK_VAR (dst)), ctx); +} + +/* Arithmatic Operation */ +static inline void emit_ar_r (const u8 rd,const u8 rt,const u8 rm, +const u8 rn,struct jit_ctx *ctx,u8 op) { +switch (op) { +case BPF_JSET : + ctx->seen |=SEEN_CALL ; +emit (ARM_AND_R (ARM_IP , rt, rn), ctx); +emit (ARM_AND_R (ARM_LR , rd, rm), ctx); +emit (ARM_ORRS_R (ARM_IP ,ARM_LR ,ARM_IP ), ctx); +break ; +case BPF_JEQ : +case BPF_JNE : +case BPF_JGT : +case BPF_JGE : +emit (ARM_CMP_R (rd, rm), ctx); +_emit (ARM_COND_EQ ,ARM_CMP_R (rt, rn), ctx); +break ; +case BPF_JSGT : +emit (ARM_CMP_R (rn, rt), ctx); +emit (ARM_SBCS_R (ARM_IP , rm, rd), ctx); +break ; +case BPF_JSGE : +emit (ARM_CMP_R (rt, rn), ctx); +emit (ARM_SBCS_R (ARM_IP , rd, rm), ctx); +break ; + } +} + +static int out_offset =-1 ;/* initialized on the first pass of build_body() */ +static int emit_bpf_tail_call (struct jit_ctx *ctx) { -if (!(ctx->seen &SEEN_X )) - ctx->flags |=FLAG_NEED_X_RESET ; - ctx->seen |=SEEN_X ; +/* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */ +const u8 *r2 = bpf2a32[BPF_REG_2 ]; +const u8 *r3 = bpf2a32[BPF_REG_3 ]; +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; +const u8 *tcc = bpf2a32[TCALL_CNT ]; +const int idx0 = ctx->idx; +#define cur_offset (ctx->idx - idx0) +#define jmp_offset (out_offset - (cur_offset)) +u32 off, lo, hi; + +/* if (index >= array->map.max_entries) +* goto out; +*/ + off = offsetof(struct bpf_array, map.max_entries); +/* array->map.max_entries */ +emit_a32_mov_i (tmp[1 ], off, false, ctx); +emit (ARM_LDR_I (tmp2[1 ],ARM_SP ,STACK_VAR (r2[1 ])), ctx); +emit (ARM_LDR_R (tmp[1 ], tmp2[1 ], tmp[1 ]), ctx); +/* index (64 bit) */ +emit (ARM_LDR_I (tmp2[1 ],ARM_SP ,STACK_VAR (r3[1 ])), ctx); +/* index >= array->map.max_entries */ +emit (ARM_CMP_R (tmp2[1 ], tmp[1 ]), ctx); +_emit (ARM_COND_CS ,ARM_B (jmp_offset), ctx); + +/* if (tail_call_cnt > MAX_TAIL_CALL_CNT) +* goto out; +* tail_call_cnt++; +*/ + lo = (u32 )MAX_TAIL_CALL_CNT ; + hi = (u32)((u64 )MAX_TAIL_CALL_CNT >>32 ); +emit (ARM_LDR_I (tmp[1 ],ARM_SP ,STACK_VAR (tcc[1 ])), ctx); +emit (ARM_LDR_I (tmp[0 ],ARM_SP ,STACK_VAR (tcc[0 ])), ctx); +emit (ARM_CMP_I (tmp[0 ], hi), ctx); +_emit (ARM_COND_EQ ,ARM_CMP_I (tmp[1 ], lo), ctx); +_emit (ARM_COND_HI ,ARM_B (jmp_offset), ctx); +emit (ARM_ADDS_I (tmp[1 ], tmp[1 ],1 ), ctx); +emit (ARM_ADC_I (tmp[0 ], tmp[0 ],0 ), ctx); +emit (ARM_STR_I (tmp[1 ],ARM_SP ,STACK_VAR (tcc[1 ])), ctx); +emit (ARM_STR_I (tmp[0 ],ARM_SP ,STACK_VAR (tcc[0 ])), ctx); + +/* prog = array->ptrs[index] +* if (prog == NULL) +* goto out; +*/ + off = offsetof(struct bpf_array , ptrs); +emit_a32_mov_i (tmp[1 ], off, false, ctx); +emit (ARM_LDR_I (tmp2[1 ],ARM_SP ,STACK_VAR (r2[1 ])), ctx); +emit (ARM_LDR_R (tmp[1 ], tmp2[1 ], tmp[1 ]), ctx); +emit (ARM_LDR_I (tmp2[1 ],ARM_SP ,STACK_VAR (r3[1 ])), ctx); +emit (ARM_MOV_SI (tmp[0 ], tmp2[1 ],SRTYPE_ASL ,2 ), ctx); +emit (ARM_LDR_R (tmp[1 ], tmp[1 ], tmp[0 ]), ctx); +emit (ARM_CMP_I (tmp[1 ],0 ), ctx); +_emit (ARM_COND_EQ ,ARM_B (jmp_offset), ctx); + +/* goto *(prog->bpf_func + prologue_size); */ + off = offsetof(struct bpf_prog , bpf_func); +emit_a32_mov_i (tmp2[1 ], off, false, ctx); +emit (ARM_LDR_R (tmp[1 ], tmp[1 ], tmp2[1 ]), ctx); +emit (ARM_ADD_I (tmp[1 ], tmp[1 ], ctx->prologue_bytes), ctx); +emit (ARM_BX (tmp[1 ]), ctx); + +/* out: */ +if (out_offset ==-1 ) + out_offset = cur_offset; +if (cur_offset != out_offset) { +pr_err_once ("tail_call out_offset = %d, expected %d!\n" , + cur_offset, out_offset); +return -1 ; + } +return 0 ; +#undef cur_offset +#undef jmp_offset } -static int build_body (struct jit_ctx *ctx) +/* 0xabcd => 0xcdab */ +static inline void emit_rev16 (const u8 rd,const u8 rn,struct jit_ctx *ctx) { -void *load_func[] = {jit_get_skb_b, jit_get_skb_h, jit_get_skb_w}; -const struct bpf_prog *prog = ctx->skf; -const struct sock_filter *inst; -unsigned i, load_order, off, condt; -int imm12; -u32 k; +#if __LINUX_ARM_ARCH__ <6 +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; + +emit (ARM_AND_I (tmp2[1 ], rn,0xff ), ctx); +emit (ARM_MOV_S (tmp2[0 ], rn,SRTYPE_LSR ,8 ), ctx); +emit (ARM_AND_I (tmp2[0 ], tmp2[0 ],0xff ), ctx); +emit (ARM_ORR_SI (rd, tmp2[0 ], tmp2[1 ],SRTYPE_LSL ,8 ), ctx); +#else /* ARMv6+ */ +emit (ARM_REV16 (rd, rn), ctx); +#endif +} -for (i =0 ; i < prog->len; i++) { -u16 code; +/* 0xabcdefgh => 0xghefcdab */ +static inline void emit_rev32 (const u8 rd,const u8 rn,struct jit_ctx *ctx) +{ +#if __LINUX_ARM_ARCH__ <6 +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; + +emit (ARM_AND_I (tmp2[1 ], rn,0xff ), ctx); +emit (ARM_MOV_S (tmp2[0 ], rn,SRTYPE_LSR ,24 ), ctx); +emit (ARM_ORR_SI (ARM_IP , tmp2[0 ], tmp2[1 ],SRTYPE_LSL ,24 ), ctx); + +emit (ARM_MOV_I (tmp2[1 ], rn,0xff00 ), ctx); +emit (ARM_MOV_S (tmp2[0 ], rn,SRTYPE_LSR ,8 ), ctx); +emit (ARM_MOV_I (tmp2[0 ], tmp2[0 ],0xff00 ), ctx); +emit (ARM_ORR_SI (tmp2[0 ], tmp2[0 ], tmp2[1 ],SRTYPE_LSL ,8 ), ctx); +emit (ARM_ORR_R (rd,ARM_IP , tmp2[0 ]), ctx); +#else /* ARMv6+ */ +emit (ARM_REV (rd, rn), ctx); +#endif +} - inst = &(prog->insns[i]); -/* K as an immediate value operand */ - k = inst->k; - code =bpf_anc_helper (inst); +static void build_prologue (struct jit_ctx *ctx) +{ +const u8 r0 = bpf2a32[BPF_REG_0 ][1 ]; +const u8 r2 = bpf2a32[BPF_REG_1 ][1 ]; +const u8 r3 = bpf2a32[BPF_REG_1 ][0 ]; +const u8 r4 = bpf2a32[BPF_REG_6 ][1 ]; +const u8 r5 = bpf2a32[BPF_REG_6 ][0 ]; +const u8 r6 = bpf2a32[TMP_REG_1 ][1 ]; +const u8 r7 = bpf2a32[TMP_REG_1 ][0 ]; +const u8 r8 = bpf2a32[TMP_REG_2 ][1 ]; +const u8 r10 = bpf2a32[TMP_REG_2 ][0 ]; +const u8 fplo = bpf2a32[BPF_REG_FP ][1 ]; +const u8 fphi = bpf2a32[BPF_REG_FP ][0 ]; +const u8 sp =ARM_SP ; +const u8 *tcc = bpf2a32[TCALL_CNT ]; + +u16 reg_set =0 ; -/* compute offsets only in the fake pass */ -if (ctx->target ==NULL ) - ctx->offsets[i] = ctx->idx *4 ; +/* +* eBPF prog stack layout +* +* high +* original ARM_SP => +-----+ eBPF prologue +* |FP/LR| +* current ARM_FP => +-----+ +* | ... | callee saved registers +* eBPF fp register => +-----+ <= (BPF_FP) +* | ... | eBPF JIT scratch space +* | | eBPF prog stack +* +-----+ +* |RSVD | JIT scratchpad +* current A64_SP => +-----+ <= (BPF_FP - STACK_SIZE) +* | | +* | ... | Function call stack +* | | +* +-----+ +* low +*/ -switch (code) { -case BPF_LD |BPF_IMM : -emit_mov_i (r_A, k, ctx); +/* Save callee saved registers. */ + reg_set |= (1 <<r4) | (1 <<r5) | (1 <<r6) | (1 <<r7) | (1 <<r8) | (1 <<r10); +#ifdef CONFIG_FRAME_POINTER + reg_set |= (1 <<ARM_FP ) | (1 <<ARM_IP ) | (1 <<ARM_LR ) | (1 <<ARM_PC ); +emit (ARM_MOV_R (ARM_IP , sp), ctx); +emit (ARM_PUSH (reg_set), ctx); +emit (ARM_SUB_I (ARM_FP ,ARM_IP ,4 ), ctx); +#else +/* Check if call instruction exists in BPF body */ +if (ctx->seen &SEEN_CALL ) + reg_set |= (1 <<ARM_LR ); +emit (ARM_PUSH (reg_set), ctx); +#endif +/* Save frame pointer for later */ +emit (ARM_SUB_I (ARM_IP , sp,SCRATCH_SIZE ), ctx); + +/* Set up function call stack */ +emit (ARM_SUB_I (ARM_SP ,ARM_SP ,imm8m (STACK_SIZE )), ctx); + +/* Set up BPF prog stack base register */ +emit_a32_mov_r (fplo,ARM_IP , true, false, ctx); +emit_a32_mov_i (fphi,0 , true, ctx); + +/* mov r4, 0 */ +emit (ARM_MOV_I (r4,0 ), ctx); +/* MOV bpf_ctx pointer to BPF_R1 */ +emit (ARM_MOV_R (r3, r4), ctx); +emit (ARM_MOV_R (r2, r0), ctx); +/* Initialize Tail Count */ +emit (ARM_STR_I (r4,ARM_SP ,STACK_VAR (tcc[0 ])), ctx); +emit (ARM_STR_I (r4,ARM_SP ,STACK_VAR (tcc[1 ])), ctx); +/* end of prologue */ +} + +static void build_epilogue (struct jit_ctx *ctx) +{ +const u8 r4 = bpf2a32[BPF_REG_6 ][1 ]; +const u8 r5 = bpf2a32[BPF_REG_6 ][0 ]; +const u8 r6 = bpf2a32[TMP_REG_1 ][1 ]; +const u8 r7 = bpf2a32[TMP_REG_1 ][0 ]; +const u8 r8 = bpf2a32[TMP_REG_2 ][1 ]; +const u8 r10 = bpf2a32[TMP_REG_2 ][0 ]; +u16 reg_set =0 ; + +/* unwind function call stack */ +emit (ARM_ADD_I (ARM_SP ,ARM_SP ,imm8m (STACK_SIZE )), ctx); + +/* restore callee saved registers. */ + reg_set |= (1 <<r4) | (1 <<r5) | (1 <<r6) | (1 <<r7) | (1 <<r8) | (1 <<r10); +#ifdef CONFIG_FRAME_POINTER +/* the first instruction of the prologue was: mov ip, sp */ + reg_set |= (1 <<ARM_FP ) | (1 <<ARM_SP ) | (1 <<ARM_PC ); +emit (ARM_LDM (ARM_SP , reg_set), ctx); +#else +if (ctx->seen &SEEN_CALL ) + reg_set |= (1 <<ARM_PC ); +/* Restore callee saved registers. */ +emit (ARM_POP (reg_set), ctx); +/* Return back to the callee function */ +if (!(ctx->seen &SEEN_CALL )) +emit (ARM_BX (ARM_LR ), ctx); +#endif +} + +/* +* Convert an eBPF instruction to native instruction, i.e +* JITs an eBPF instruction. +* Returns : +* 0 - Successfully JITed an 8-byte eBPF instruction +* >0 - Successfully JITed a 16-byte eBPF instruction +* <0 - Failed to JIT. +*/ +static int build_insn (const struct bpf_insn *insn,struct jit_ctx *ctx) +{ +const u8 code = insn->code; +const u8 *dst = bpf2a32[insn->dst_reg]; +const u8 *src = bpf2a32[insn->src_reg]; +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; +const s16 off = insn->off; +const s32 imm = insn->imm; +const int i = insn - ctx->prog->insnsi; +const bool is64 =BPF_CLASS (code) ==BPF_ALU64 ; +const bool dstk =is_on_stack (insn->dst_reg); +const bool sstk =is_on_stack (insn->src_reg); +u8 rd, rt, rm, rn; +s32 jmp_offset; + +#define check_imm (bits, imm) do { \ + if ((((imm) > 0) && ((imm) >> (bits))) || \ + (((imm) < 0) && (~(imm) >> (bits)))) { \ + pr_info("[%2d] imm=%d(0x%x) out of range\n", \ + i, imm, imm); \ + return -EINVAL; \ + } \ +} while (0) +#define check_imm24 (imm) check_imm(24, imm) + +switch (code) { +/* ALU operations */ + +/* dst = src */ +case BPF_ALU |BPF_MOV |BPF_K : +case BPF_ALU |BPF_MOV |BPF_X : +case BPF_ALU64 |BPF_MOV |BPF_K : +case BPF_ALU64 |BPF_MOV |BPF_X : +switch (BPF_SRC (code)) { +case BPF_X : +emit_a32_mov_r64 (is64, dst, src, dstk, sstk, ctx);break ; -case BPF_LD |BPF_W |BPF_LEN : - ctx->seen |=SEEN_SKB ; - BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4); -emit (ARM_LDR_I (r_A, r_skb, - offsetof(struct sk_buff , len)), ctx); +case BPF_K : +/* Sign-extend immediate value to destination reg */ +emit_a32_mov_i64 (is64, dst, imm, dstk, ctx);break ; -case BPF_LD |BPF_MEM : -/* A = scratch[k] */ - ctx->seen |=SEEN_MEM_WORD (k); -emit (ARM_LDR_I (r_A,ARM_SP ,SCRATCH_OFF (k)), ctx); + } +break ; +/* dst = dst + src/imm */ +/* dst = dst - src/imm */ +/* dst = dst | src/imm */ +/* dst = dst & src/imm */ +/* dst = dst ^ src/imm */ +/* dst = dst * src/imm */ +/* dst = dst << src */ +/* dst = dst >> src */ +case BPF_ALU |BPF_ADD |BPF_K : +case BPF_ALU |BPF_ADD |BPF_X : +case BPF_ALU |BPF_SUB |BPF_K : +case BPF_ALU |BPF_SUB |BPF_X : +case BPF_ALU |BPF_OR |BPF_K : +case BPF_ALU |BPF_OR |BPF_X : +case BPF_ALU |BPF_AND |BPF_K : +case BPF_ALU |BPF_AND |BPF_X : +case BPF_ALU |BPF_XOR |BPF_K : +case BPF_ALU |BPF_XOR |BPF_X : +case BPF_ALU |BPF_MUL |BPF_K : +case BPF_ALU |BPF_MUL |BPF_X : +case BPF_ALU |BPF_LSH |BPF_X : +case BPF_ALU |BPF_RSH |BPF_X : +case BPF_ALU |BPF_ARSH |BPF_K : +case BPF_ALU |BPF_ARSH |BPF_X : +case BPF_ALU64 |BPF_ADD |BPF_K : +case BPF_ALU64 |BPF_ADD |BPF_X : +case BPF_ALU64 |BPF_SUB |BPF_K : +case BPF_ALU64 |BPF_SUB |BPF_X : +case BPF_ALU64 |BPF_OR |BPF_K : +case BPF_ALU64 |BPF_OR |BPF_X : +case BPF_ALU64 |BPF_AND |BPF_K : +case BPF_ALU64 |BPF_AND |BPF_X : +case BPF_ALU64 |BPF_XOR |BPF_K : +case BPF_ALU64 |BPF_XOR |BPF_X : +switch (BPF_SRC (code)) { +case BPF_X : +emit_a32_alu_r64 (is64, dst, src, dstk, sstk, + ctx,BPF_OP (code));break ; -case BPF_LD |BPF_W |BPF_ABS : - load_order =2 ; - goto load; -case BPF_LD |BPF_H |BPF_ABS : - load_order =1 ; - goto load; -case BPF_LD |BPF_B |BPF_ABS : - load_order =0 ; -load: -emit_mov_i (r_off, k, ctx); -load_common: - ctx->seen |=SEEN_DATA |SEEN_CALL ; - -if (load_order >0 ) { -emit (ARM_SUB_I (r_scratch, r_skb_hl, -1 << load_order), ctx); -emit (ARM_CMP_R (r_scratch, r_off), ctx); - condt =ARM_COND_GE ; - }else { -emit (ARM_CMP_R (r_skb_hl, r_off), ctx); - condt =ARM_COND_HI ; - } - -/* -* test for negative offset, only if we are -* currently scheduled to take the fast -* path. this will update the flags so that -* the slowpath instruction are ignored if the -* offset is negative. -* -* for loard_order == 0 the HI condition will -* make loads@offset 0 take the slow path too. +case BPF_K : +/* Move immediate value to the temporary register +* and then do the ALU operation on the temporary +* register as this will sign-extend the immediate +* value into temporary reg and then it would be +* safe to do the operation on it. */ -_emit (condt,ARM_CMP_I (r_off,0 ), ctx); - -_emit (condt,ARM_ADD_R (r_scratch, r_off, r_skb_data), - ctx); - -if (load_order ==0 ) -_emit (condt,ARM_LDRB_I (r_A, r_scratch,0 ), - ctx); -else if (load_order ==1 ) -emit_load_be16 (condt, r_A, r_scratch, ctx); -else if (load_order ==2 ) -emit_load_be32 (condt, r_A, r_scratch, ctx); - -_emit (condt,ARM_B (b_imm (i +1 , ctx)), ctx); - -/* the slowpath */ -emit_mov_i (ARM_R3 , (u32 )load_func[load_order], ctx); -emit (ARM_MOV_R (ARM_R0 , r_skb), ctx); -/* the offset is already in R1 */ -emit_blx_r (ARM_R3 , ctx); -/* check the result of skb_copy_bits */ -emit (ARM_CMP_I (ARM_R1 ,0 ), ctx); -emit_err_ret (ARM_COND_NE , ctx); -emit (ARM_MOV_R (r_A,ARM_R0 ), ctx); +emit_a32_mov_i64 (is64, tmp2, imm, false, ctx); +emit_a32_alu_r64 (is64, dst, tmp2, dstk, false, + ctx,BPF_OP (code));break ; -case BPF_LD |BPF_W |BPF_IND : - load_order =2 ; - goto load_ind; -case BPF_LD |BPF_H |BPF_IND : - load_order =1 ; - goto load_ind; -case BPF_LD |BPF_B |BPF_IND : - load_order =0 ; -load_ind: -update_on_xread (ctx); -OP_IMM3 (ARM_ADD , r_off, r_X, k, ctx); - goto load_common; -case BPF_LDX |BPF_IMM : - ctx->seen |=SEEN_X ; -emit_mov_i (r_X, k, ctx); + } +break ; +/* dst = dst / src(imm) */ +/* dst = dst % src(imm) */ +case BPF_ALU |BPF_DIV |BPF_K : +case BPF_ALU |BPF_DIV |BPF_X : +case BPF_ALU |BPF_MOD |BPF_K : +case BPF_ALU |BPF_MOD |BPF_X : + rt = src_lo; + rd = dstk ? tmp2[1 ] : dst_lo; +if (dstk) +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +switch (BPF_SRC (code)) { +case BPF_X : + rt = sstk ? tmp2[0 ] : rt; +if (sstk) +emit (ARM_LDR_I (rt,ARM_SP ,STACK_VAR (src_lo)), + ctx);break ; -case BPF_LDX |BPF_W |BPF_LEN : - ctx->seen |=SEEN_X |SEEN_SKB ; -emit (ARM_LDR_I (r_X, r_skb, - offsetof(struct sk_buff , len)), ctx); +case BPF_K : + rt = tmp2[0 ]; +emit_a32_mov_i (rt, imm, false, ctx);break ; -case BPF_LDX |BPF_MEM : - ctx->seen |=SEEN_X |SEEN_MEM_WORD (k); -emit (ARM_LDR_I (r_X,ARM_SP ,SCRATCH_OFF (k)), ctx); + } +emit_udivmod (rd, rd, rt, ctx,BPF_OP (code)); +if (dstk) +emit (ARM_STR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit_a32_mov_i (dst_hi,0 , dstk, ctx); +break ; +case BPF_ALU64 |BPF_DIV |BPF_K : +case BPF_ALU64 |BPF_DIV |BPF_X : +case BPF_ALU64 |BPF_MOD |BPF_K : +case BPF_ALU64 |BPF_MOD |BPF_X : + goto notyet; +/* dst = dst >> imm */ +/* dst = dst << imm */ +case BPF_ALU |BPF_RSH |BPF_K : +case BPF_ALU |BPF_LSH |BPF_K : +if (unlikely (imm >31 )) +return -EINVAL ; +if (imm) +emit_a32_alu_i (dst_lo, imm, dstk, ctx,BPF_OP (code)); +emit_a32_mov_i (dst_hi,0 , dstk, ctx); +break ; +/* dst = dst << imm */ +case BPF_ALU64 |BPF_LSH |BPF_K : +if (unlikely (imm >63 )) +return -EINVAL ; +emit_a32_lsh_i64 (dst, dstk, imm, ctx); +break ; +/* dst = dst >> imm */ +case BPF_ALU64 |BPF_RSH |BPF_K : +if (unlikely (imm >63 )) +return -EINVAL ; +emit_a32_lsr_i64 (dst, dstk, imm, ctx); +break ; +/* dst = dst << src */ +case BPF_ALU64 |BPF_LSH |BPF_X : +emit_a32_lsh_r64 (dst, src, dstk, sstk, ctx); +break ; +/* dst = dst >> src */ +case BPF_ALU64 |BPF_RSH |BPF_X : +emit_a32_lsr_r64 (dst, src, dstk, sstk, ctx); +break ; +/* dst = dst >> src (signed) */ +case BPF_ALU64 |BPF_ARSH |BPF_X : +emit_a32_arsh_r64 (dst, src, dstk, sstk, ctx); +break ; +/* dst = dst >> imm (signed) */ +case BPF_ALU64 |BPF_ARSH |BPF_K : +if (unlikely (imm >63 )) +return -EINVAL ; +emit_a32_arsh_i64 (dst, dstk, imm, ctx); +break ; +/* dst = ~dst */ +case BPF_ALU |BPF_NEG : +emit_a32_alu_i (dst_lo,0 , dstk, ctx,BPF_OP (code)); +emit_a32_mov_i (dst_hi,0 , dstk, ctx); +break ; +/* dst = ~dst (64 bit) */ +case BPF_ALU64 |BPF_NEG : +emit_a32_neg64 (dst, dstk, ctx); +break ; +/* dst = dst * src/imm */ +case BPF_ALU64 |BPF_MUL |BPF_X : +case BPF_ALU64 |BPF_MUL |BPF_K : +switch (BPF_SRC (code)) { +case BPF_X : +emit_a32_mul_r64 (dst, src, dstk, sstk, ctx);break ; -case BPF_LDX |BPF_B |BPF_MSH : -/* x = ((*(frame + k)) & 0xf) << 2; */ - ctx->seen |=SEEN_X |SEEN_DATA |SEEN_CALL ; -/* the interpreter should deal with the negative K */ -if ((int )k <0 ) -return -1 ; -/* offset in r1: we might have to take the slow path */ -emit_mov_i (r_off, k, ctx); -emit (ARM_CMP_R (r_skb_hl, r_off), ctx); - -/* load in r0: common with the slowpath */ -_emit (ARM_COND_HI ,ARM_LDRB_R (ARM_R0 , r_skb_data, -ARM_R1 ), ctx); -/* -* emit_mov_i() might generate one or two instructions, -* the same holds for emit_blx_r() +case BPF_K : +/* Move immediate value to the temporary register +* and then do the multiplication on it as this +* will sign-extend the immediate value into temp +* reg then it would be safe to do the operation +* on it. */ -_emit (ARM_COND_HI ,ARM_B (b_imm (i +1 , ctx) -2 ), ctx); - -emit (ARM_MOV_R (ARM_R0 , r_skb), ctx); -/* r_off is r1 */ -emit_mov_i (ARM_R3 , (u32 )jit_get_skb_b, ctx); -emit_blx_r (ARM_R3 , ctx); -/* check the return value of skb_copy_bits */ -emit (ARM_CMP_I (ARM_R1 ,0 ), ctx); -emit_err_ret (ARM_COND_NE , ctx); - -emit (ARM_AND_I (r_X,ARM_R0 ,0x00f ), ctx); -emit (ARM_LSL_I (r_X, r_X,2 ), ctx); -break ; -case BPF_ST : - ctx->seen |=SEEN_MEM_WORD (k); -emit (ARM_STR_I (r_A,ARM_SP ,SCRATCH_OFF (k)), ctx); -break ; -case BPF_STX : -update_on_xread (ctx); - ctx->seen |=SEEN_MEM_WORD (k); -emit (ARM_STR_I (r_X,ARM_SP ,SCRATCH_OFF (k)), ctx); -break ; -case BPF_ALU |BPF_ADD |BPF_K : -/* A += K */ -OP_IMM3 (ARM_ADD , r_A, r_A, k, ctx); -break ; -case BPF_ALU |BPF_ADD |BPF_X : -update_on_xread (ctx); -emit (ARM_ADD_R (r_A, r_A, r_X), ctx); -break ; -case BPF_ALU |BPF_SUB |BPF_K : -/* A -= K */ -OP_IMM3 (ARM_SUB , r_A, r_A, k, ctx); -break ; -case BPF_ALU |BPF_SUB |BPF_X : -update_on_xread (ctx); -emit (ARM_SUB_R (r_A, r_A, r_X), ctx); -break ; -case BPF_ALU |BPF_MUL |BPF_K : -/* A *= K */ -emit_mov_i (r_scratch, k, ctx); -emit (ARM_MUL (r_A, r_A, r_scratch), ctx); -break ; -case BPF_ALU |BPF_MUL |BPF_X : -update_on_xread (ctx); -emit (ARM_MUL (r_A, r_A, r_X), ctx); -break ; -case BPF_ALU |BPF_DIV |BPF_K : -if (k ==1 ) -break ; -emit_mov_i (r_scratch, k, ctx); -emit_udivmod (r_A, r_A, r_scratch, ctx,BPF_DIV ); -break ; -case BPF_ALU |BPF_DIV |BPF_X : -update_on_xread (ctx); -emit (ARM_CMP_I (r_X,0 ), ctx); -emit_err_ret (ARM_COND_EQ , ctx); -emit_udivmod (r_A, r_A, r_X, ctx,BPF_DIV ); -break ; -case BPF_ALU |BPF_MOD |BPF_K : -if (k ==1 ) { -emit_mov_i (r_A,0 , ctx); -break ; - } -emit_mov_i (r_scratch, k, ctx); -emit_udivmod (r_A, r_A, r_scratch, ctx,BPF_MOD ); +emit_a32_mov_i64 (is64, tmp2, imm, false, ctx); +emit_a32_mul_r64 (dst, tmp2, dstk, false, ctx);break ; -case BPF_ALU |BPF_MOD |BPF_X : -update_on_xread (ctx); -emit (ARM_CMP_I (r_X,0 ), ctx); -emit_err_ret (ARM_COND_EQ , ctx); -emit_udivmod (r_A, r_A, r_X, ctx,BPF_MOD ); -break ; -case BPF_ALU |BPF_OR |BPF_K : -/* A |= K */ -OP_IMM3 (ARM_ORR , r_A, r_A, k, ctx); + } +break ; +/* dst = htole(dst) */ +/* dst = htobe(dst) */ +case BPF_ALU |BPF_END |BPF_FROM_LE : +case BPF_ALU |BPF_END |BPF_FROM_BE : + rd = dstk ? tmp[0 ] : dst_hi; + rt = dstk ? tmp[1 ] : dst_lo; +if (dstk) { +emit (ARM_LDR_I (rt,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } +#ifdef CONFIG_CPU_BIG_ENDIAN +if (BPF_SRC (code) ==BPF_FROM_BE ) + goto emit_bswap_uxt; +#else /* !CONFIG_CPU_BIG_ENDIAN */ +if (BPF_SRC (code) ==BPF_FROM_LE ) + goto emit_bswap_uxt; +#endif +switch (imm) { +case 16 : +emit_rev16 (rt, rt, ctx); + goto emit_bswap_uxt; +case 32 : +emit_rev32 (rt, rt, ctx); + goto emit_bswap_uxt; +case 64 : +/* Because of the usage of ARM_LR */ + ctx->seen |=SEEN_CALL ; +emit_rev32 (ARM_LR , rt, ctx); +emit_rev32 (rt, rd, ctx); +emit (ARM_MOV_R (rd,ARM_LR ), ctx);break ; -case BPF_ALU |BPF_OR |BPF_X : -update_on_xread (ctx); -emit (ARM_ORR_R (r_A, r_A, r_X), ctx); + } + goto exit; +emit_bswap_uxt: +switch (imm) { +case 16 : +/* zero-extend 16 bits into 64 bits */ +#if __LINUX_ARM_ARCH__ <6 +emit_a32_mov_i (tmp2[1 ],0xffff , false, ctx); +emit (ARM_AND_R (rt, tmp2[1 ]), ctx); +#else /* ARMv6+ */ +emit (ARM_UXTH (rt, rt), ctx); +#endif +emit (ARM_EOR_R (rd, rd, rd), ctx);break ; -case BPF_ALU |BPF_XOR |BPF_K : -/* A ^= K; */ -OP_IMM3 (ARM_EOR , r_A, r_A, k, ctx); +case 32 : +/* zero-extend 32 bits into 64 bits */ +emit (ARM_EOR_R (rd, rd, rd), ctx);break ; -case BPF_ANC |SKF_AD_ALU_XOR_X : -case BPF_ALU |BPF_XOR |BPF_X : -/* A ^= X */ -update_on_xread (ctx); -emit (ARM_EOR_R (r_A, r_A, r_X), ctx); +case 64 : +/* nop */ break ; -case BPF_ALU |BPF_AND |BPF_K : -/* A &= K */ -OP_IMM3 (ARM_AND , r_A, r_A, k, ctx); + } +exit: +if (dstk) { +emit (ARM_STR_I (rt,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_STR_I (rd,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } +break ; +/* dst = imm64 */ +case BPF_LD |BPF_IMM |BPF_DW : + { +const struct bpf_insn insn1 = insn[1 ]; +u32 hi, lo = imm; + +if (insn1.code !=0 || insn1.src_reg !=0 || + insn1.dst_reg !=0 || insn1.off !=0 ) { +/* Note: verifier in BPF core must catch invalid +* instruction. +*/ +pr_err_once ("Invalid BPF_LD_IMM64 instruction\n" ); +return -EINVAL ; + } + hi = insn1.imm; +emit_a32_mov_i (dst_lo, lo, dstk, ctx); +emit_a32_mov_i (dst_hi, hi, dstk, ctx); + +return 1 ; + } +/* LDX: dst = *(size *)(src + off) */ +case BPF_LDX |BPF_MEM |BPF_W : +case BPF_LDX |BPF_MEM |BPF_H : +case BPF_LDX |BPF_MEM |BPF_B : +case BPF_LDX |BPF_MEM |BPF_DW : + rn = sstk ? tmp2[1 ] : src_lo; +if (sstk) +emit (ARM_LDR_I (rn,ARM_SP ,STACK_VAR (src_lo)), ctx); +switch (BPF_SIZE (code)) { +case BPF_W : +/* Load a Word */ +case BPF_H : +/* Load a Half-Word */ +case BPF_B : +/* Load a Byte */ +emit_ldx_r (dst_lo, rn, dstk, off, ctx,BPF_SIZE (code)); +emit_a32_mov_i (dst_hi,0 , dstk, ctx);break ; -case BPF_ALU |BPF_AND |BPF_X : -update_on_xread (ctx); -emit (ARM_AND_R (r_A, r_A, r_X), ctx); +case BPF_DW : +/* Load a double word */ +emit_ldx_r (dst_lo, rn, dstk, off, ctx,BPF_W ); +emit_ldx_r (dst_hi, rn, dstk, off+4 , ctx,BPF_W );break ; -case BPF_ALU |BPF_LSH |BPF_K : -if (unlikely (k >31 )) -return -1 ; -emit (ARM_LSL_I (r_A, r_A, k), ctx); + } +break ; +/* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + imm)) */ +case BPF_LD |BPF_ABS |BPF_W : +case BPF_LD |BPF_ABS |BPF_H : +case BPF_LD |BPF_ABS |BPF_B : +/* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + src + imm)) */ +case BPF_LD |BPF_IND |BPF_W : +case BPF_LD |BPF_IND |BPF_H : +case BPF_LD |BPF_IND |BPF_B : + { +const u8 r4 = bpf2a32[BPF_REG_6 ][1 ];/* r4 = ptr to sk_buff */ +const u8 r0 = bpf2a32[BPF_REG_0 ][1 ];/*r0: struct sk_buff *skb*/ +/* rtn value */ +const u8 r1 = bpf2a32[BPF_REG_0 ][0 ];/* r1: int k */ +const u8 r2 = bpf2a32[BPF_REG_1 ][1 ];/* r2: unsigned int size */ +const u8 r3 = bpf2a32[BPF_REG_1 ][0 ];/* r3: void *buffer */ +const u8 r6 = bpf2a32[TMP_REG_1 ][1 ];/* r6: void *(*func)(..) */ +int size; + +/* Setting up first argument */ +emit (ARM_MOV_R (r0, r4), ctx); + +/* Setting up second argument */ +emit_a32_mov_i (r1, imm, false, ctx); +if (BPF_MODE (code) ==BPF_IND ) +emit_a32_alu_r (r1, src_lo, false, sstk, ctx, + false, false,BPF_ADD ); + +/* Setting up third argument */ +switch (BPF_SIZE (code)) { +case BPF_W : + size =4 ;break ; -case BPF_ALU |BPF_LSH |BPF_X : -update_on_xread (ctx); -emit (ARM_LSL_R (r_A, r_A, r_X), ctx); +case BPF_H : + size =2 ;break ; -case BPF_ALU |BPF_RSH |BPF_K : -if (unlikely (k >31 )) -return -1 ; -if (k) -emit (ARM_LSR_I (r_A, r_A, k), ctx); +case BPF_B : + size =1 ;break ; -case BPF_ALU |BPF_RSH |BPF_X : -update_on_xread (ctx); -emit (ARM_LSR_R (r_A, r_A, r_X), ctx); +default : +return -EINVAL ; + } +emit_a32_mov_i (r2, size, false, ctx); + +/* Setting up fourth argument */ +emit (ARM_ADD_I (r3,ARM_SP ,imm8m (SKB_BUFFER )), ctx); + +/* Setting up function pointer to call */ +emit_a32_mov_i (r6, (unsigned int )bpf_load_pointer, false, ctx); +emit_blx_r (r6, ctx); + +emit (ARM_EOR_R (r1, r1, r1), ctx); +/* Check if return address is NULL or not. +* if NULL then jump to epilogue +* else continue to load the value from retn address +*/ +emit (ARM_CMP_I (r0,0 ), ctx); + jmp_offset =epilogue_offset (ctx); +check_imm24 (jmp_offset); +_emit (ARM_COND_EQ ,ARM_B (jmp_offset), ctx); + +/* Load value from the address */ +switch (BPF_SIZE (code)) { +case BPF_W : +emit (ARM_LDR_I (r0, r0,0 ), ctx); +#ifndef CONFIG_CPU_BIG_ENDIAN +emit_rev32 (r0, r0, ctx); +#endif break ; -case BPF_ALU |BPF_NEG : -/* A = -A */ -emit (ARM_RSB_I (r_A, r_A,0 ), ctx); +case BPF_H : +emit (ARM_LDRH_I (r0, r0,0 ), ctx); +#ifndef CONFIG_CPU_BIG_ENDIAN +emit_rev16 (r0, r0, ctx); +#endif break ; -case BPF_JMP |BPF_JA : -/* pc += K */ -emit (ARM_B (b_imm (i + k +1 , ctx)), ctx); +case BPF_B : +emit (ARM_LDRB_I (r0, r0,0 ), ctx); +/* No need to reverse */ break ; -case BPF_JMP |BPF_JEQ |BPF_K : -/* pc += (A == K) ? pc->jt : pc->jf */ - condt =ARM_COND_EQ ; - goto cmp_imm; -case BPF_JMP |BPF_JGT |BPF_K : -/* pc += (A > K) ? pc->jt : pc->jf */ - condt =ARM_COND_HI ; - goto cmp_imm; -case BPF_JMP |BPF_JGE |BPF_K : -/* pc += (A >= K) ? pc->jt : pc->jf */ - condt =ARM_COND_HS ; -cmp_imm: - imm12 =imm8m (k); -if (imm12 <0 ) { -emit_mov_i_no8m (r_scratch, k, ctx); -emit (ARM_CMP_R (r_A, r_scratch), ctx); - }else { -emit (ARM_CMP_I (r_A, imm12), ctx); - } -cond_jump: -if (inst->jt) -_emit (condt,ARM_B (b_imm (i + inst->jt +1 , - ctx)), ctx); -if (inst->jf) -_emit (condt ^1 ,ARM_B (b_imm (i + inst->jf +1 , - ctx)), ctx); + } +break ; + } +/* ST: *(size *)(dst + off) = imm */ +case BPF_ST |BPF_MEM |BPF_W : +case BPF_ST |BPF_MEM |BPF_H : +case BPF_ST |BPF_MEM |BPF_B : +case BPF_ST |BPF_MEM |BPF_DW : +switch (BPF_SIZE (code)) { +case BPF_DW : +/* Sign-extend immediate value into temp reg */ +emit_a32_mov_i64 (true, tmp2, imm, false, ctx); +emit_str_r (dst_lo, tmp2[1 ], dstk, off, ctx,BPF_W ); +emit_str_r (dst_lo, tmp2[0 ], dstk, off+4 , ctx,BPF_W );break ; -case BPF_JMP |BPF_JEQ |BPF_X : -/* pc += (A == X) ? pc->jt : pc->jf */ - condt =ARM_COND_EQ ; - goto cmp_x; -case BPF_JMP |BPF_JGT |BPF_X : -/* pc += (A > X) ? pc->jt : pc->jf */ - condt =ARM_COND_HI ; - goto cmp_x; -case BPF_JMP |BPF_JGE |BPF_X : -/* pc += (A >= X) ? pc->jt : pc->jf */ - condt =ARM_COND_CS ; -cmp_x: -update_on_xread (ctx); -emit (ARM_CMP_R (r_A, r_X), ctx); - goto cond_jump; -case BPF_JMP |BPF_JSET |BPF_K : -/* pc += (A & K) ? pc->jt : pc->jf */ - condt =ARM_COND_NE ; -/* not set iff all zeroes iff Z==1 iff EQ */ - - imm12 =imm8m (k); -if (imm12 <0 ) { -emit_mov_i_no8m (r_scratch, k, ctx); -emit (ARM_TST_R (r_A, r_scratch), ctx); - }else { -emit (ARM_TST_I (r_A, imm12), ctx); - } - goto cond_jump; -case BPF_JMP |BPF_JSET |BPF_X : -/* pc += (A & X) ? pc->jt : pc->jf */ -update_on_xread (ctx); - condt =ARM_COND_NE ; -emit (ARM_TST_R (r_A, r_X), ctx); - goto cond_jump; -case BPF_RET |BPF_A : -emit (ARM_MOV_R (ARM_R0 , r_A), ctx); - goto b_epilogue; -case BPF_RET |BPF_K : -if ((k ==0 ) && (ctx->ret0_fp_idx <0 )) - ctx->ret0_fp_idx = i; -emit_mov_i (ARM_R0 , k, ctx); -b_epilogue: -if (i != ctx->skf->len -1 ) -emit (ARM_B (b_imm (prog->len, ctx)), ctx); +case BPF_W : +case BPF_H : +case BPF_B : +emit_a32_mov_i (tmp2[1 ], imm, false, ctx); +emit_str_r (dst_lo, tmp2[1 ], dstk, off, ctx, +BPF_SIZE (code));break ; -case BPF_MISC |BPF_TAX : -/* X = A */ - ctx->seen |=SEEN_X ; -emit (ARM_MOV_R (r_X, r_A), ctx); + } +break ; +/* STX XADD: lock *(u32 *)(dst + off) += src */ +case BPF_STX |BPF_XADD |BPF_W : +/* STX XADD: lock *(u64 *)(dst + off) += src */ +case BPF_STX |BPF_XADD |BPF_DW : + goto notyet; +/* STX: *(size *)(dst + off) = src */ +case BPF_STX |BPF_MEM |BPF_W : +case BPF_STX |BPF_MEM |BPF_H : +case BPF_STX |BPF_MEM |BPF_B : +case BPF_STX |BPF_MEM |BPF_DW : + { +u8 sz =BPF_SIZE (code); + + rn = sstk ? tmp2[1 ] : src_lo; + rm = sstk ? tmp2[0 ] : src_hi; +if (!sstk) + goto do_store; +switch (BPF_SIZE (code)) { +case BPF_W : +emit (ARM_LDR_I (rn,ARM_SP ,STACK_VAR (src_lo)), ctx); + goto empty_hi; +case BPF_H : +emit (ARM_LDRH_I (rn,ARM_SP ,STACK_VAR (src_lo)), ctx); + goto empty_hi; +case BPF_B : +emit (ARM_LDRB_I (rn,ARM_SP ,STACK_VAR (src_lo)), ctx); + goto empty_hi; +empty_hi: +emit (ARM_EOR_R (rm, rm, rm), ctx); +case BPF_DW : +emit (ARM_LDR_I (rn,ARM_SP ,STACK_VAR (src_lo)), ctx); +emit (ARM_LDR_I (rm,ARM_SP ,STACK_VAR (src_hi)), ctx); + sz =BPF_W ;break ; -case BPF_MISC |BPF_TXA : -/* A = X */ -update_on_xread (ctx); -emit (ARM_MOV_R (r_A, r_X), ctx); + } + +do_store: +/* Clear higher word except for BPF_DW */ +if (BPF_SIZE (code) !=BPF_DW ) +emit (ARM_EOR_R (rm, rm, rm), ctx); + +/* Store the value */ +emit_str_r (dst_lo, rn, dstk, off, ctx, sz); +emit_str_r (dst_lo, rm, dstk, off+4 , ctx,BPF_W ); +break ; + } +/* PC += off if dst == src */ +/* PC += off if dst > src */ +/* PC += off if dst >= src */ +/* PC += off if dst != src */ +/* PC += off if dst > src (signed) */ +/* PC += off if dst >= src (signed) */ +/* PC += off if dst & src */ +case BPF_JMP |BPF_JEQ |BPF_X : +case BPF_JMP |BPF_JGT |BPF_X : +case BPF_JMP |BPF_JGE |BPF_X : +case BPF_JMP |BPF_JNE |BPF_X : +case BPF_JMP |BPF_JSGT |BPF_X : +case BPF_JMP |BPF_JSGE |BPF_X : +case BPF_JMP |BPF_JSET |BPF_X : +/* Setup source registers */ + rm = sstk ? tmp2[0 ] : src_hi; + rn = sstk ? tmp2[1 ] : src_lo; +if (sstk) { +emit (ARM_LDR_I (rn,ARM_SP ,STACK_VAR (src_lo)), ctx); +emit (ARM_LDR_I (rm,ARM_SP ,STACK_VAR (src_hi)), ctx); + } + goto go_jmp; +/* PC += off if dst == imm */ +/* PC += off if dst > imm */ +/* PC += off if dst >= imm */ +/* PC += off if dst != imm */ +/* PC += off if dst > imm (signed) */ +/* PC += off if dst >= imm (signed) */ +/* PC += off if dst & imm */ +case BPF_JMP |BPF_JEQ |BPF_K : +case BPF_JMP |BPF_JGT |BPF_K : +case BPF_JMP |BPF_JGE |BPF_K : +case BPF_JMP |BPF_JNE |BPF_K : +case BPF_JMP |BPF_JSGT |BPF_K : +case BPF_JMP |BPF_JSGE |BPF_K : +case BPF_JMP |BPF_JSET |BPF_K : +if (off ==0 )break ; -case BPF_ANC |SKF_AD_PROTOCOL : -/* A = ntohs(skb->protocol) */ - ctx->seen |=SEEN_SKB ; -BUILD_BUG_ON (FIELD_SIZEOF (struct sk_buff , - protocol) != 2); - off = offsetof(struct sk_buff , protocol); -emit (ARM_LDRH_I (r_scratch, r_skb, off), ctx); -emit_swap16 (r_A, r_scratch, ctx); + rm = tmp2[0 ]; + rn = tmp2[1 ]; +/* Sign-extend immediate value */ +emit_a32_mov_i64 (true, tmp2, imm, false, ctx); +go_jmp: +/* Setup destination register */ + rd = dstk ? tmp[0 ] : dst_hi; + rt = dstk ? tmp[1 ] : dst_lo; +if (dstk) { +emit (ARM_LDR_I (rt,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } + +/* Check for the condition */ +emit_ar_r (rd, rt, rm, rn, ctx,BPF_OP (code)); + +/* Setup JUMP instruction */ + jmp_offset =bpf2a32_offset (i+off, i, ctx); +switch (BPF_OP (code)) { +case BPF_JNE : +case BPF_JSET : +_emit (ARM_COND_NE ,ARM_B (jmp_offset), ctx);break ; -case BPF_ANC |SKF_AD_CPU : -/* r_scratch = current_thread_info() */ -OP_IMM3 (ARM_BIC , r_scratch,ARM_SP ,THREAD_SIZE -1 , ctx); -/* A = current_thread_info()->cpu */ - BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info, cpu) != 4); - off = offsetof(struct thread_info , cpu); -emit (ARM_LDR_I (r_A, r_scratch, off), ctx); +case BPF_JEQ : +_emit (ARM_COND_EQ ,ARM_B (jmp_offset), ctx);break ; -case BPF_ANC |SKF_AD_IFINDEX : -case BPF_ANC |SKF_AD_HATYPE : -/* A = skb->dev->ifindex */ -/* A = skb->dev->type */ - ctx->seen |=SEEN_SKB ; - off = offsetof(struct sk_buff , dev); -emit (ARM_LDR_I (r_scratch, r_skb, off), ctx); - -emit (ARM_CMP_I (r_scratch,0 ), ctx); -emit_err_ret (ARM_COND_EQ , ctx); - -BUILD_BUG_ON (FIELD_SIZEOF (struct net_device , - ifindex) != 4); -BUILD_BUG_ON (FIELD_SIZEOF (struct net_device , - type) != 2); - -if (code == (BPF_ANC |SKF_AD_IFINDEX )) { - off = offsetof(struct net_device , ifindex); -emit (ARM_LDR_I (r_A, r_scratch, off), ctx); - }else { -/* -* offset of field "type" in "struct -* net_device" is above what can be -* used in the ldrh rd, [rn, #imm] -* instruction, so load the offset in -* a register and use ldrh rd, [rn, rm] -*/ - off = offsetof(struct net_device , type); -emit_mov_i (ARM_R3 , off, ctx); -emit (ARM_LDRH_R (r_A, r_scratch,ARM_R3 ), ctx); - } +case BPF_JGT : +_emit (ARM_COND_HI ,ARM_B (jmp_offset), ctx);break ; -case BPF_ANC |SKF_AD_MARK : - ctx->seen |=SEEN_SKB ; - BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4); - off = offsetof(struct sk_buff , mark); -emit (ARM_LDR_I (r_A, r_skb, off), ctx); +case BPF_JGE : +_emit (ARM_COND_CS ,ARM_B (jmp_offset), ctx);break ; -case BPF_ANC |SKF_AD_RXHASH : - ctx->seen |=SEEN_SKB ; - BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4); - off = offsetof(struct sk_buff , hash); -emit (ARM_LDR_I (r_A, r_skb, off), ctx); +case BPF_JSGT : +_emit (ARM_COND_LT ,ARM_B (jmp_offset), ctx);break ; -case BPF_ANC |SKF_AD_VLAN_TAG : -case BPF_ANC |SKF_AD_VLAN_TAG_PRESENT : - ctx->seen |=SEEN_SKB ; - BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2); - off = offsetof(struct sk_buff , vlan_tci); -emit (ARM_LDRH_I (r_A, r_skb, off), ctx); -if (code == (BPF_ANC |SKF_AD_VLAN_TAG )) -OP_IMM3 (ARM_AND , r_A, r_A, ~VLAN_TAG_PRESENT , ctx); -else { -OP_IMM3 (ARM_LSR , r_A, r_A,12 , ctx); -OP_IMM3 (ARM_AND , r_A, r_A,0x1 , ctx); - } +case BPF_JSGE : +_emit (ARM_COND_GE ,ARM_B (jmp_offset), ctx);break ; -case BPF_ANC |SKF_AD_PKTTYPE : - ctx->seen |=SEEN_SKB ; -BUILD_BUG_ON (FIELD_SIZEOF (struct sk_buff , - __pkt_type_offset[0]) != 1); - off =PKT_TYPE_OFFSET (); -emit (ARM_LDRB_I (r_A, r_skb, off), ctx); -emit (ARM_AND_I (r_A, r_A,PKT_TYPE_MAX ), ctx); -#ifdef __BIG_ENDIAN_BITFIELD -emit (ARM_LSR_I (r_A, r_A,5 ), ctx); -#endif + } +break ; +/* JMP OFF */ +case BPF_JMP |BPF_JA : + { +if (off ==0 )break ; -case BPF_ANC |SKF_AD_QUEUE : - ctx->seen |=SEEN_SKB ; -BUILD_BUG_ON (FIELD_SIZEOF (struct sk_buff , - queue_mapping) != 2); -BUILD_BUG_ON (offsetof(struct sk_buff , - queue_mapping) >0xff ); - off = offsetof(struct sk_buff , queue_mapping); -emit (ARM_LDRH_I (r_A, r_skb, off), ctx); + jmp_offset =bpf2a32_offset (i+off, i, ctx); +check_imm24 (jmp_offset); +emit (ARM_B (jmp_offset), ctx); +break ; + } +/* tail call */ +case BPF_JMP |BPF_CALL |BPF_X : +if (emit_bpf_tail_call (ctx)) +return -EFAULT ; +break ; +/* function call */ +case BPF_JMP |BPF_CALL : + goto notyet; +/* function return */ +case BPF_JMP |BPF_EXIT : +/* Optimization: when last instruction is EXIT +* simply fallthrough to epilogue. +*/ +if (i == ctx->prog->len -1 )break ; -case BPF_ANC |SKF_AD_PAY_OFFSET : - ctx->seen |=SEEN_SKB |SEEN_CALL ; + jmp_offset =epilogue_offset (ctx); +check_imm24 (jmp_offset); +emit (ARM_B (jmp_offset), ctx); +break ; +notyet: +pr_info_once ("*** NOT YET: opcode %02x ***\n" , code); +return -EFAULT ; +default : +pr_err_once ("unknown opcode %02x\n" , code); +return -EINVAL ; + } -emit (ARM_MOV_R (ARM_R0 , r_skb), ctx); -emit_mov_i (ARM_R3 , (unsigned int )skb_get_poff, ctx); -emit_blx_r (ARM_R3 , ctx); -emit (ARM_MOV_R (r_A,ARM_R0 ), ctx); -break ; -case BPF_LDX |BPF_W |BPF_ABS : -/* -* load a 32bit word from struct seccomp_data. -* seccomp_check_filter() will already have checked -* that k is 32bit aligned and lies within the -* struct seccomp_data. -*/ - ctx->seen |=SEEN_SKB ; -emit (ARM_LDR_I (r_A, r_skb, k), ctx); -break ; -default : -return -1 ; +if (ctx->flags &FLAG_IMM_OVERFLOW ) +/* +* this instruction generated an overflow when +* trying to access the literal pool, so +* delegate this filter to the kernel interpreter. +*/ +return -1 ; +return 0 ; +} + +static int build_body (struct jit_ctx *ctx) +{ +const struct bpf_prog *prog = ctx->prog; +unsigned int i; + +for (i =0 ; i < prog->len; i++) { +const struct bpf_insn *insn = &(prog->insnsi[i]); +int ret; + +emit (ARM_MOV_R (ARM_IP ,ARM_PC ), ctx); + ret =build_insn (insn, ctx); + +/* It's used with loading the 64 bit immediate value. */ +if (ret >0 ) { + i++; +if (ctx->target ==NULL ) + ctx->offsets[i] = ctx->idx; +continue ; } -if (ctx->flags &FLAG_IMM_OVERFLOW ) -/* -* this instruction generated an overflow when -* trying to access the literal pool, so -* delegate this filter to the kernel interpreter. -*/ -return -1 ; +if (ctx->target ==NULL ) + ctx->offsets[i] = ctx->idx; + +/* If unsuccesfull, return with error code */ +if (ret) +return ret; } +return 0 ; +} -/* compute offsets only during the first pass */ -if (ctx->target ==NULL ) - ctx->offsets[i] = ctx->idx *4 ; +static int validate_code (struct jit_ctx *ctx) +{ +int i; + +for (i =0 ; i < ctx->idx; i++) { +u32 a32_insn =le32_to_cpu (ctx->target[i]); + +if (a32_insn ==ARM_INST_UDF ) +return -1 ; + }return 0 ; } +void bpf_jit_compile (struct bpf_prog *prog) +{ +/* Nothing to do here. We support Internal BPF. */ +} -void bpf_jit_compile (struct bpf_prog *fp) +struct bpf_prog *bpf_int_jit_compile (struct bpf_prog *prog) { +struct bpf_prog *tmp, *orig_prog = prog;struct bpf_binary_header *header; +bool tmp_blinded = false;struct jit_ctx ctx; -unsigned tmp_idx; -unsigned alloc_size; -u8 *target_ptr; +unsigned int tmp_idx; +unsigned int image_size; +u8 *image_ptr; +/* If BPF JIT was not enabled then we must fall back to +* the interpreter. +*/ if (!bpf_jit_enable) -return ; +return orig_prog; -memset (&ctx,0 ,sizeof (ctx)); - ctx.skf = fp; - ctx.ret0_fp_idx =-1 ; +/* If constant blinding was enabled and we failed during blinding +* then we must fall back to the interpreter. Otherwise, we save +* the new JITed code. +*/ + tmp =bpf_jit_blind_constants (prog); - ctx.offsets =kzalloc (4 * (ctx.skf->len +1 ),GFP_KERNEL ); -if (ctx.offsets ==NULL ) -return ; +if (IS_ERR (tmp)) +return orig_prog; +if (tmp != prog) { + tmp_blinded = true; + prog = tmp; + } + +memset (&ctx,0 ,sizeof (ctx)); + ctx.prog = prog; -/* fake pass to fill in the ctx->seen */ -if (unlikely (build_body (&ctx))) +/* Not able to allocate memory for offsets[] , then +* we must fall back to the interpreter +*/ + ctx.offsets =kcalloc (prog->len,sizeof (int ),GFP_KERNEL ); +if (ctx.offsets ==NULL ) { + prog = orig_prog; goto out; + } + +/* 1) fake pass to find in the length of the JITed code, +* to compute ctx->offsets and other context variables +* needed to compute final JITed code. +* Also, calculate random starting pointer/start of JITed code +* which is prefixed by random number of fault instructions. +* +* If the first pass fails then there is no chance of it +* being successful in the second pass, so just fall back +* to the interpreter. +*/ +if (build_body (&ctx)) { + prog = orig_prog; + goto out_off; + } tmp_idx = ctx.idx;build_prologue (&ctx); ctx.prologue_bytes = (ctx.idx - tmp_idx) *4 ; + ctx.epilogue_offset = ctx.idx; +#if __LINUX_ARM_ARCH__ <7 tmp_idx = ctx.idx;build_epilogue (&ctx);
@@ -1020,64 +1845,95 @@ void bpf_jit_compile(struct bpf_prog *fp) ctx.idx += ctx.imm_count;if (ctx.imm_count) { - ctx.imms =kzalloc (4 * ctx.imm_count,GFP_KERNEL ); -if (ctx.imms ==NULL ) - goto out; + ctx.imms =kcalloc (ctx.imm_count,sizeof (u32),GFP_KERNEL ); +if (ctx.imms ==NULL ) { + prog = orig_prog; + goto out_off; + } }#else -/* there's nothing after the epilogue on ARMv7 */ +/* there's nothing about the epilogue on ARMv7 */ build_epilogue (&ctx);#endif - alloc_size =4 * ctx.idx; - header =bpf_jit_binary_alloc (alloc_size, &target_ptr, -4 , jit_fill_hole); -if (header ==NULL ) - goto out; +/* Now we can get the actual image size of the JITed arm code. +* Currently, we are not considering the THUMB-2 instructions +* for jit, although it can decrease the size of the image. +* +* As each arm instruction is of length 32bit, we are translating +* number of JITed intructions into the size required to store these +* JITed code. +*/ + image_size =sizeof (u32) * ctx.idx; - ctx.target = (u32 *) target_ptr; +/* Now we know the size of the structure to make */ + header =bpf_jit_binary_alloc (image_size, &image_ptr, +sizeof (u32), jit_fill_hole); +/* Not able to allocate memory for the structure then +* we must fall back to the interpretation +*/ +if (header ==NULL ) { + prog = orig_prog; + goto out_imms; + } + +/* 2.) Actual pass to generate final JIT code */ + ctx.target = (u32 *) image_ptr; ctx.idx =0 ;build_prologue (&ctx); + +/* If building the body of the JITed code fails somehow, +* we fall back to the interpretation. +*/ if (build_body (&ctx) <0 ) { -#if __LINUX_ARM_ARCH__ <7 -if (ctx.imm_count) -kfree (ctx.imms); -#endif + image_ptr =NULL ;bpf_jit_binary_free (header); - goto out; + prog = orig_prog; + goto out_imms; }build_epilogue (&ctx); +/* 3.) Extra pass to validate JITed Code */ +if (validate_code (&ctx)) { + image_ptr =NULL ; +bpf_jit_binary_free (header); + prog = orig_prog; + goto out_imms; + }flush_icache_range ((u32 )header, (u32)(ctx.target + ctx.idx)); -#if __LINUX_ARM_ARCH__ <7 -if (ctx.imm_count) -kfree (ctx.imms); -#endif -if (bpf_jit_enable >1 )/* there are 2 passes here */ -bpf_jit_dump (fp->len, alloc_size,2 , ctx.target); +bpf_jit_dump (prog->len, image_size,2 , ctx.target);set_memory_ro ((unsigned long )header, header->pages); - fp->bpf_func = (void *)ctx.target; - fp->jited =1 ; -out: + prog->bpf_func = (void *)ctx.target; + prog->jited =1 ; +out_imms: +#if __LINUX_ARM_ARCH__ <7 +if (ctx.imm_count) +kfree (ctx.imms); +#endif +out_off:kfree (ctx.offsets); -return ; +out: +if (tmp_blinded) +bpf_jit_prog_release_other (prog, prog == orig_prog ? + tmp : orig_prog); +return prog; } -void bpf_jit_free (struct bpf_prog *fp) +void bpf_jit_free (struct bpf_prog *prog) { -unsigned long addr = (unsigned long )fp->bpf_func &PAGE_MASK ; +unsigned long addr = (unsigned long )prog->bpf_func &PAGE_MASK ;struct bpf_binary_header *header = (void *)addr; -if (!fp->jited) +if (!prog->jited) goto free_filter;set_memory_rw (addr, header->pages);bpf_jit_binary_free (header); free_filter: -bpf_prog_unlock_free (fp); +bpf_prog_unlock_free (prog); }
arch/arm/net/bpf_jit_32.h +83 -25
219 lines not on this page · raw message
Signature · 1 line
2.7.4
Russell King - ARM Linux
maintainerHi,
Quoted 8 lines · Shubham Bansal
The JIT compiler emits ARM 32 bit instructions. Currently, It supports eBPF only. Classic BPF is supported because of the conversion by BPF core.
JIT is enabled with
echo 1 >
/proc/sys/net/core/bpf_jit_enable
Quoted 7 lines
Constant Blinding can be enabled along with JIT using
echo 1 >
/proc/sys/net/core/bpf_jit_enableecho 2 >/proc/sys/net/core/bpf_jit_hardenSee
Documentation/networking/filter.txtfor more information. Tested on ARMv7 withCONFIG_FRAME_POINTERenabled.
What is this patch actually doing?
The commit message doesn't describe what changes this patch is doing, and it needs to. Presumably, it's adding support for eBPF?
If that's so, doesn't it need to change us from selecting HAVE_CBPF_JIT to selecting HAVE_EBPF_JIT ?
What about cases where the frame pointer is not enabled? If that's not supported then shouldn't the Kconfig be adjusted? What about users who are using cBPF without the frame pointer enabled?
I haven't been able to go through the patch analysing it fully, but what I have read doesn't give me any immediate concerns. Only one spelling mistake stood out.
Some things that I'm wondering though - what about endian issues? LDRD, for example, takes two registers, in ARM mode, these registers must be an even-odd pair. The even register is loaded from at the lowest address, the odd register from the higher address. This means when loading a BE value, the most significant word is in the even numbered register and the least significant word is in the odd numbered register. For LE values, the even numbered register contains the least significant word. Are we properly handling issues like this in all places?
Quoted 9 lines
arch/arm/net/bpf_jit_32.c +9
-0+static const u8 bpf2a32[][2 ] = { +/* return value from in-kernel function, and exit value from eBPF */ + [BPF_REG_0 ] = {ARM_R1 ,ARM_R0 }, +/* arguments from eBPF program to in-kernel function */ + [BPF_REG_1 ] = {ARM_R3 ,ARM_R2 }, +/* Stored on stack scratch space */ + [BPF_REG_2 ] = {STACK_OFFSET (0 ),STACK_OFFSET (4 )}, + [BPF_REG_3 ] = {STACK_OFFSET (8 ),STACK_OFFSET (12 )}, + [BPF_REG_4 ] = {STACK_OFFSET (16 ),STACK_OFFSET (20 )},
arch/arm/net/bpf_jit_32.c line 74
Quoted 10 lines
arch/arm/net/bpf_jit_32.c +10
-0+ [BPF_REG_5] = {STACK_OFFSET(24), STACK_OFFSET(28)}, + /* callee saved registers that in-kernel function will preserve */ + [BPF_REG_6] = {ARM_R5, ARM_R4}, + /* Stored on stack scratch space */ + [BPF_REG_7] = {STACK_OFFSET(32), STACK_OFFSET(36)}, + [BPF_REG_8] = {STACK_OFFSET(40), STACK_OFFSET(44)}, + [BPF_REG_9] = {STACK_OFFSET(48), STACK_OFFSET(52)}, + /* Read only Frame Pointer to access Stack */ + [BPF_REG_FP] = {STACK_OFFSET(56), STACK_OFFSET(60)}, +/* Temperory Register for internal BPF JIT, can be used
"Temporary"
Hi Russell,
Quoted 13 lines · Russell King - ARM Linux
Hi,
Quoted 10 lines · Shubham Bansal
The JIT compiler emits ARM 32 bit instructions. Currently, It supports eBPF only. Classic BPF is supported because of the conversion by BPF core.
JIT is enabled with
echo 1 >
/proc/sys/net/core/bpf_jit_enableConstant Blinding can be enabled along with JIT using
Quoted 7 lines
Quoted 5 lines
echo 1 >
/proc/sys/net/core/bpf_jit_enable
echo 2 >/proc/sys/net/core/bpf_jit_hardenSee
Documentation/networking/filter.txtfor more information. Tested on ARMv7 withCONFIG_FRAME_POINTERenabled.What is this patch actually doing?
This is the first implementation of eBPF JIT for ARM.
Quoted 3 lines
The commit message doesn't describe what changes this patch is doing, and it needs to. Presumably, it's adding support for eBPF?
Apologies. I thought commit message was good enough to understand. I will resend the patch if needed.
Although, Its an RFC. I am requesting the community for with testing and suggestions.
Quoted 3 lines
If that's so, doesn't it need to change us from selecting
HAVE_CBPF_JITto selectingHAVE_EBPF_JIT?
This patch is only for comments and testing. I thought, KConfig changes wouldn't be needed unless
the patch is accepted.
Quoted 4 lines
What about cases where the frame pointer is not enabled?If that's not supported then shouldn't the
Kconfigbe adjusted? What about users who are using cBPF without the frame pointer enabled?
I have tested it with and without the frame pointer as indicated in the results shown in the mail.
Quoted 6 lines
I haven't been able to go through the patch analysing it fully, but what I have read doesn't give me any immediate concerns. Only one spelling mistake stood out.
Quoted 9 lines
Some things that I'm wondering though - what about endian issues? LDRD, for example, takes two registers, in ARM mode, these registers must be an even-odd pair. The even register is loaded from at the lowest address, the odd register from the higher address. This means when loading a BE value, the most significant word is in the even numbered register and the least significant word is in the odd numbered register. For LE values, the even numbered register contains the least significant word. Are we properly handling issues like this in all places?
Actually I haven't tested it with BE but tested it LE as I wasn't able to run qemu to test it properly for BE.
I request anyone who has information on how to test it to help me with it.
Quoted 21 lines
Quoted 10 lines
arch/arm/net/bpf_jit_32.c +9
-0+static const u8 bpf2a32[][2 ] = { +/* return value from in-kernel function, and exit value from eBPF */ + [BPF_REG_0 ] = {ARM_R1 ,ARM_R0 }, +/* arguments from eBPF program to in-kernel function */ + [BPF_REG_1 ] = {ARM_R3 ,ARM_R2 }, +/* Stored on stack scratch space */ + [BPF_REG_2 ] = {STACK_OFFSET (0 ),STACK_OFFSET (4 )}, + [BPF_REG_3 ] = {STACK_OFFSET (8 ),STACK_OFFSET (12 )}, + [BPF_REG_4 ] = {STACK_OFFSET (16 ),STACK_OFFSET (20 )},arch/arm/net/bpf_jit_32.c line 74
Quoted 10 lines
arch/arm/net/bpf_jit_32.c +10
-0+ [BPF_REG_5] = {STACK_OFFSET(24), STACK_OFFSET(28)}, + /* callee saved registers that in-kernel function will preserve */ + [BPF_REG_6] = {ARM_R5, ARM_R4}, + /* Stored on stack scratch space */ + [BPF_REG_7] = {STACK_OFFSET(32), STACK_OFFSET(36)}, + [BPF_REG_8] = {STACK_OFFSET(40), STACK_OFFSET(44)}, + [BPF_REG_9] = {STACK_OFFSET(48), STACK_OFFSET(52)}, + /* Read only Frame Pointer to access Stack */ + [BPF_REG_FP] = {STACK_OFFSET(56), STACK_OFFSET(60)}, +/* Temperory Register for internal BPF JIT, can be used
Quoted 1 line
"Temporary"
I will correct that right away.
Thanks for the suggestions Russell.
-Shubham
Quoted 22 lines · Shubham Bansal
The JIT compiler emits ARM 32 bit instructions. Currently, It supports eBPF only. Classic BPF is supported because of the conversion by BPF core.
JIT is enabled with
echo 1 >
/proc/sys/net/core/bpf_jit_enableConstant Blinding can be enabled along with JIT using
echo 1 >
/proc/sys/net/core/bpf_jit_enableecho 2 >/proc/sys/net/core/bpf_jit_hardenSee
Documentation/networking/filter.txtfor more information. Tested on ARMv7 withCONFIG_FRAME_POINTERenabled.Results:
1. Interpreter:
[ 93.551176] test_bpf: Summary: 314 PASSED, 0 FAILED, [0/306 JIT'ed]
Quoted 7 lines
2. JIT enabled:
[ 92.913931] test_bpf: Summary: 314 PASSED, 0 FAILED, [278/306 JIT'ed]3. JIT + blinding enabled:
[ 109.414506] test_bpf: Summary: 314 PASSED, 0 FAILED, [278/306 JIT'ed]
Hi Shubham
Is there a HOWTO or a README somewhere about running the tests? I have a few ARMv5 platforms i can run tests on.
Thanks
Andrew
Quoted 3 lines · Shubham Bansal
The JIT compiler emits ARM 32 bit instructions. Currently, It supports eBPF only. Classic BPF is supported because of the conversion by BPF core.
make mvebu_v5_defconfig
make -s -j 16arch/arm/net/bpf_jit_32.c: In function emit_rev16:arch/arm/net/bpf_jit_32.c:1019:7: error: implicit declaration of function ARM_MOV_S [-Werror=implicit-function-declaration]
emit(ARM_MOV_S(tmp2[0], rn, SRTYPE_LSR, 8), ctx);
^~~~~~~~~
arch/arm/net/bpf_jit_32.c: In function emit_rev32:
arch/arm/net/bpf_jit_32.c:1037:36: error: macro "ARM_MOV_I" passed 3 arguments, but takes just 2
emit(ARM_MOV_I(tmp2[1], rn, 0xff00), ctx);
^
arch/arm/net/bpf_jit_32.c:1037:7: error: ARM_MOV_I undeclared (first use in this function)
emit(ARM_MOV_I(tmp2[1], rn, 0xff00), ctx);
^~~~~~~~~
arch/arm/net/bpf_jit_32.c:1037:7: note: each undeclared identifier is reported only once for each function it appears in
arch/arm/net/bpf_jit_32.c:1039:41: error: macro "ARM_MOV_I" passed 3 arguments, but takes just 2
emit(ARM_MOV_I(tmp2[0], tmp2[0], 0xff00), ctx);
^
arch/arm/net/bpf_jit_32.c: In function build_insn:
arch/arm/net/bpf_jit_32.c:1393:30: error: macro "ARM_AND_R" requires 3 arguments, but only 2 given
emit(ARM_AND_R(rt, tmp2[1]), ctx);
^
arch/arm/net/bpf_jit_32.c:1393:9: error: ARM_AND_R undeclared (first use in this function)
emit(ARM_AND_R(rt, tmp2[1]), ctx);
AndrewHi Andrew,
I think I forgot to compile it for ARMv5. Let me send the patch again in few minutes.
I appreciate the help.
Best,
Shubham Bansal
Quoted 31 lines · Andrew Lunn
Quoted 3 lines · Shubham Bansal
The JIT compiler emits ARM 32 bit instructions. Currently, It supports eBPF only. Classic BPF is supported because of the conversion by BPF core.
make
mvebu_v5_defconfigmake -s -j 16
arch/arm/net/bpf_jit_32.c: In functionemit_rev16:arch/arm/net/bpf_jit_32.c:1019:7: error: implicit declaration of functionARM_MOV_S[-Werror=implicit-function-declaration]emit(ARM_MOV_S(tmp2[0], rn, SRTYPE_LSR, 8), ctx); ^~~~~~~~~ arch/arm/net/bpf_jit_32.c: In function emit_rev32: arch/arm/net/bpf_jit_32.c:1037:36: error: macro "ARM_MOV_I" passed 3 arguments, but takes just 2 emit(ARM_MOV_I(tmp2[1], rn, 0xff00), ctx); ^ arch/arm/net/bpf_jit_32.c:1037:7: error: ARM_MOV_I undeclared (first use in this function) emit(ARM_MOV_I(tmp2[1], rn, 0xff00), ctx); ^~~~~~~~~ arch/arm/net/bpf_jit_32.c:1037:7: note: each undeclared identifier is reported only once for each function it appears in arch/arm/net/bpf_jit_32.c:1039:41: error: macro "ARM_MOV_I" passed 3 arguments, but takes just 2 emit(ARM_MOV_I(tmp2[0], tmp2[0], 0xff00), ctx); ^ arch/arm/net/bpf_jit_32.c: In function build_insn: arch/arm/net/bpf_jit_32.c:1393:30: error: macro "ARM_AND_R" requires 3 arguments, but only 2 given emit(ARM_AND_R(rt, tmp2[1]), ctx); ^ arch/arm/net/bpf_jit_32.c:1393:9: error: ARM_AND_R undeclared (first use in this function) emit(ARM_AND_R(rt, tmp2[1]), ctx); Andrew
Hi Andrew,
I am attaching the additional patch to make it work for ARMv5 as well. Let me know if you find any issues with this.
Please apply 0002-Editted-for-ARMv5-compilation.patch after applying the first patch.
Best,
Shubham Bansal
Quoted 23 lines · Andrew Lunn
Quoted 3 lines · Shubham Bansal
The JIT compiler emits ARM 32 bit instructions. Currently, It supports eBPF only. Classic BPF is supported because of the conversion by BPF core.
make
mvebu_v5_defconfigmake -s -j 16
arch/arm/net/bpf_jit_32.c: In functionemit_rev16:arch/arm/net/bpf_jit_32.c:1019:7: error: implicit declaration of functionARM_MOV_S[-Werror=implicit-function-declaration]emit(ARM_MOV_S(tmp2[0], rn, SRTYPE_LSR, 8), ctx); ^~~~~~~~~ arch/arm/net/bpf_jit_32.c: In function emit_rev32: arch/arm/net/bpf_jit_32.c:1037:36: error: macro "ARM_MOV_I" passed 3 arguments, but takes just 2 emit(ARM_MOV_I(tmp2[1], rn, 0xff00), ctx); ^ arch/arm/net/bpf_jit_32.c:1037:7: error: ARM_MOV_I undeclared (first use in this function) emit(ARM_MOV_I(tmp2[1], rn, 0xff00), ctx); ^~~~~~~~~ arch/arm/net/bpf_jit_32.c:1037:7: note: each undeclared identifier is reported only once for each function it appears in arch/arm/net/bpf_jit_32.c:1039:41: error: macro "ARM_MOV_I" passed 3 arguments, but takes just 2 emit(ARM_MOV_I(tmp2[0], tmp2[0], 0xff00), ctx); ^
Quoted 8 lines
arch/arm/net/bpf_jit_32.c: In functionbuild_insn:arch/arm/net/bpf_jit_32.c:1393:30: error: macro "ARM_AND_R" requires 3 arguments, but only 2 givenemit(ARM_AND_R(rt, tmp2[1]), ctx); ^ arch/arm/net/bpf_jit_32.c:1393:9: error: ARM_AND_R undeclared (first use in this function) emit(ARM_AND_R(rt, tmp2[1]), ctx); Andrew
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0002-Editted-for-ARMv5-compilation.patch
Type: application/octet-stream
Size: 2045 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20170524/646238cf/attachment.obj>
root at qnap:~# test_bpf: #0 TAX jited:0 241 466 241 PASStest_bpf: #1 TXA jited:1 51 117 51 PASStest_bpf: #2 ADD_SUB_MUL_K jited:0 167 PASStest_bpf: #3 DIV_MOD_KX jited:0 571 PASStest_bpf: #4 AND_OR_LSH_K jited:1 54 54 PASStest_bpf: #5 LD_IMM_0 jited:1 45 PASStest_bpf: #6 LD_IND jited:0 143 143 143 PASStest_bpf: #7 LD_ABS jited:0 120 133 120 PASStest_bpf: #8 LD_ABS_LL
Unable to handle kernel paging request at virtual address 9e096420 pgd = daa30000 [9e096420] *pgd=00000000 Internal error: Oops: 805 [#1] PREEMPT ARM Modules linked in: test_bpf(+) [last unloaded: test_bpf] CPU: 0 PID: 1886 Comm: modprobe Not tainted 4.12.0-rc2-00275-g17a0ff4ded06-dirty #1784 Hardware name: Marvell Kirkwood (Flattened Device Tree) task: df7d4ba0 task.stack: de738000 PC is at imm_offset+0xa4/0x108 LR is at 0x8 pc : [<c0015080>] lr : [<00000008>] psr: 60000013
sp : de739c6c ip : 0000002c fp : 00000003
r10: de739cf8 r9 : e1356000 r8 : 0000001c
r7 : de59c840 r6 : de59c840 r5 : bf2c2944 r4 : 00000000 r3 : dedd3adc r2 : dedd3adc r1 : de739cf8 r0 : ffe00000
Flags: nZCv IRQs on FIQs on Mode SVC_32 ISA ARM Segment none
Control: 0005397f Table: 1aa30000 DAC: 00000051 Process modprobe (pid: 1886, stack limit = 0xde738190)
Stack: (0xde739c6c to 0xde73a000)
9c60: 00000001 00000030 00000003 c05c5d5e c001a560
9c80: de739cf4 00000000 e1356038 00000000 c05c5d34 ffe00000 c05c5d42 00000000 9ca0: 00000000 0000001a e1356000 00000000 5e3b2a64 00000f04 00000000 e1356000 9cc0: e1356000 c078105c 00000000 000000f8 bf2c2000 00000000 bf1661c0 c001c64c 9ce0: de57f0c8 00000008 00000002 de739d54 00000eec bf2c2944 e1356000 0000001c 9d00: 0000002c 00000038 00000001 00000000 de59c9a0 bf2c2944 00040008 de59c840 9d20: e1356000 de739d50 de57f0c0 00000005 de59c9a0 0000ffff 00000001 c008fb98 9d40: e1356000 e1356000 de57f0c0 c04a8384 00000000 00000008 e1356000 de739dd8 9d60: 00000000 e1356000 00000028 de739dd4 bf165140 00000000 bf1661c0 c04a84d0 9d80: 00000005 e1354000 bf1651c8 00000000 e1354000 bf2af218 dedcfd20 ffffe000 9da0: df45ef80 bf1651c4 00000008 00000008 00000003 00000008 00000003 00000000 9dc0: bf1650a5 bf165144 79f5179d 0000004e 00000000 e1354000 00000005 bf1651c8 9de0: 00000000 ffffe000 bf2af000 00000000 de57f9e4 00000001 bf2acee0 c007aa88 9e00: 00000124 c0009738 ffffffff 00000000 00000001 dfbe78b0 c07c25ac 0080007f 9e20: dfbe78a4 00000000 00000000 0000001f 00000000 dffc3d60 de738000 df401f00 9e40: de56b080 c007d6e0 00016acf c007aa88 bf2acee0 00000001 de59c280 00000001 9e60: bf2acee0 c00a1548 00000001 de57f9c0 de739f54 00000001 de57f9c0 c007d6e8 9e80: bf2aceec 00007fff bf2acee0 c007afd4 de739f48 00000000 bf2acf28 00000000 9ea0: 00000000 bf2ad074 e1344928 c05c76a8 bf2aceec 00000000 de739f44 c00f6fa8 9ec0: 001879a0 00000000 c00f7684 00000000 00000000 00000000 00000000 00000000 9ee0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 9f00: 00000000 00000000 7fffffff 00000000 00000003 7f59f510 0000017b c000a544 9f20: de738000 00000000 00000000 c007de58 7fffffff 00000000 00000003 00000000 9f40: 00000000 e11bd000 001879a0 00000000 00000874 e11bd000 001879a0 e13441d0 9f60: e1343ff0 e1337960 00154550 001547c0 00000000 00000000 00000000 000014ec 9f80: 0000002f 00000030 0000001b 00000000 00000015 00000000 ca81c300 7f5b2810 9fa0: 00000000 c000a3a0 ca81c300 7f5b2810 00000003 7f59f510 00000000 7f59fd00 9fc0: ca81c300 7f5b2810 00000000 0000017b 7f59f510 00000000 7f5b21d0 00000000 9fe0: bee273a0 bee27390 7f593acc b6ed1130 60000010 00000003 00000000 00000000 [<c0015080>] (imm_offset) from [<c001a560>] (build_body+0x5164/0x70ec) [<c001a560>] (build_body) from [<c001c64c>] (bpf_int_jit_compile+0x160/0x314) [<c001c64c>] (bpf_int_jit_compile) from [<c008fb98>] (bpf_prog_select_runtime+0x14/0x124) [<c008fb98>] (bpf_prog_select_runtime) from [<c04a8384>] (bpf_prepare_filter+0x7b0/0x888) [<c04a8384>] (bpf_prepare_filter) from [<c04a84d0>] (bpf_prog_create+0x74/0xa4) [<c04a84d0>] (bpf_prog_create) from [<bf2af218>] (test_bpf_init+0x218/0x77c [test_bpf]) [<bf2af218>] (test_bpf_init [test_bpf]) from [<c0009738>] (do_one_initcall+0x40/0x178) [<c0009738>] (do_one_initcall) from [<c00a1548>] (do_init_module+0x58/0x1bc) [<c00a1548>] (do_init_module) from [<c007d6e8>] (load_module+0x1ae0/0x2050) [<c007d6e8>] (load_module) from [<c007de58>] (SyS_finit_module+0xa4/0xb8) [<c007de58>] (SyS_finit_module) from [<c000a3a0>] (ret_fast_syscall+0x0/0x38)
Looking at the .lst file:
/* constants go just after the epilogue */
offset = ctx->offsets[ctx->prog->len];
c0015054: e591c000 ldr ip, [r1]
c0015058: e5912018 ldr r2, [r1, #24]
offset += ctx->prologue_bytes;
offset += ctx->epilogue_bytes;
c001505c: e1d132b0 ldrh r3, [r1, #32]
offset = ctx->offsets[ctx->prog->len];
c0015060: e59ce008 ldr lr, [ip, #8]
offset += ctx->epilogue_bytes;
c0015064: e591c008 ldr ip, [r1, #8]
c0015068: 0591501c ldreq r5, [r1, #28]
offset = ctx->offsets[ctx->prog->len];
c001506c: e792210e ldr r2, [r2, lr, lsl #2]
offset += ctx->epilogue_bytes;
c0015070: e083300c add r3, r3, ip
c0015074: e0833002 add r3, r3, r2
offset += i * 4;c0015078: e0833004 add r3, r3, r4
ctx->target[offset / 4] = k; c001507c: e3c32003 bic r2, r3, #3 c0015080: e7850002 str r0, [r5, r2]
/* PC in ARM mode == address of the instruction + 8 */
imm = offset - (8 + ctx->idx * 4);
AndrewHi Andrew,
I am attaching the latest patch which should work on ARMv5 , ARMv6 and ARMv7.
Please test on your machines. Apologies for the last patch. This patch should work though.
Best,
Shubham Bansal
Quoted 91 lines · Andrew Lunn
root@qnap:~# test_bpf: #0 TAX jited:0 241 466 241 PASS test_bpf: #1 TXA jited:1 51 117 51 PASS test_bpf: #2 ADD_SUB_MUL_K jited:0 167 PASS test_bpf: #3 DIV_MOD_KX jited:0 571 PASS test_bpf: #4 AND_OR_LSH_K jited:1 54 54 PASS test_bpf: #5 LD_IMM_0 jited:1 45 PASS test_bpf: #6 LD_IND jited:0 143 143 143 PASS test_bpf: #7 LD_ABS jited:0 120 133 120 PASS test_bpf: #8 LD_ABS_LL Unable to handle kernel paging request at virtual address 9e096420 pgd = daa30000 [9e096420] *pgd=00000000 Internal error: Oops: 805 [#1] PREEMPT ARM Modules linked in: test_bpf(+) [last unloaded: test_bpf] CPU: 0 PID: 1886 Comm: modprobe Not tainted 4.12.0-rc2-00275-g17a0ff4ded06-dirty #1784 Hardware name: Marvell Kirkwood (Flattened Device Tree) task: df7d4ba0 task.stack: de738000 PC is at imm_offset+0xa4/0x108 LR is at 0x8 pc : [<c0015080>] lr : [<00000008>] psr: 60000013 sp : de739c6c ip : 0000002c fp : 00000003 r10: de739cf8 r9 : e1356000 r8 : 0000001c r7 : de59c840 r6 : de59c840 r5 : bf2c2944 r4 : 00000000 r3 : dedd3adc r2 : dedd3adc r1 : de739cf8 r0 : ffe00000 Flags: nZCv IRQs on FIQs on Mode SVC_32 ISA ARM Segment none Control: 0005397f Table: 1aa30000 DAC: 00000051 Process modprobe (pid: 1886, stack limit = 0xde738190) Stack: (0xde739c6c to 0xde73a000) 9c60: 00000001 00000030 00000003 c05c5d5e c001a560 9c80: de739cf4 00000000 e1356038 00000000 c05c5d34 ffe00000 c05c5d42 00000000 9ca0: 00000000 0000001a e1356000 00000000 5e3b2a64 00000f04 00000000 e1356000 9cc0: e1356000 c078105c 00000000 000000f8 bf2c2000 00000000 bf1661c0 c001c64c 9ce0: de57f0c8 00000008 00000002 de739d54 00000eec bf2c2944 e1356000 0000001c 9d00: 0000002c 00000038 00000001 00000000 de59c9a0 bf2c2944 00040008 de59c840 9d20: e1356000 de739d50 de57f0c0 00000005 de59c9a0 0000ffff 00000001 c008fb98 9d40: e1356000 e1356000 de57f0c0 c04a8384 00000000 00000008 e1356000 de739dd8 9d60: 00000000 e1356000 00000028 de739dd4 bf165140 00000000 bf1661c0 c04a84d0 9d80: 00000005 e1354000 bf1651c8 00000000 e1354000 bf2af218 dedcfd20 ffffe000 9da0: df45ef80 bf1651c4 00000008 00000008 00000003 00000008 00000003 00000000 9dc0: bf1650a5 bf165144 79f5179d 0000004e 00000000 e1354000 00000005 bf1651c8 9de0: 00000000 ffffe000 bf2af000 00000000 de57f9e4 00000001 bf2acee0 c007aa88 9e00: 00000124 c0009738 ffffffff 00000000 00000001 dfbe78b0 c07c25ac 0080007f 9e20: dfbe78a4 00000000 00000000 0000001f 00000000 dffc3d60 de738000 df401f00 9e40: de56b080 c007d6e0 00016acf c007aa88 bf2acee0 00000001 de59c280 00000001 9e60: bf2acee0 c00a1548 00000001 de57f9c0 de739f54 00000001 de57f9c0 c007d6e8 9e80: bf2aceec 00007fff bf2acee0 c007afd4 de739f48 00000000 bf2acf28 00000000 9ea0: 00000000 bf2ad074 e1344928 c05c76a8 bf2aceec 00000000 de739f44 c00f6fa8 9ec0: 001879a0 00000000 c00f7684 00000000 00000000 00000000 00000000 00000000 9ee0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 9f00: 00000000 00000000 7fffffff 00000000 00000003 7f59f510 0000017b c000a544 9f20: de738000 00000000 00000000 c007de58 7fffffff 00000000 00000003 00000000 9f40: 00000000 e11bd000 001879a0 00000000 00000874 e11bd000 001879a0 e13441d0 9f60: e1343ff0 e1337960 00154550 001547c0 00000000 00000000 00000000 000014ec 9f80: 0000002f 00000030 0000001b 00000000 00000015 00000000 ca81c300 7f5b2810 9fa0: 00000000 c000a3a0 ca81c300 7f5b2810 00000003 7f59f510 00000000 7f59fd00 9fc0: ca81c300 7f5b2810 00000000 0000017b 7f59f510 00000000 7f5b21d0 00000000 9fe0: bee273a0 bee27390 7f593acc b6ed1130 60000010 00000003 00000000 00000000 [<c0015080>] (imm_offset) from [<c001a560>] (build_body+0x5164/0x70ec) [<c001a560>] (build_body) from [<c001c64c>] (bpf_int_jit_compile+0x160/0x314) [<c001c64c>] (bpf_int_jit_compile) from [<c008fb98>] (bpf_prog_select_runtime+0x14/0x124) [<c008fb98>] (bpf_prog_select_runtime) from [<c04a8384>] (bpf_prepare_filter+0x7b0/0x888) [<c04a8384>] (bpf_prepare_filter) from [<c04a84d0>] (bpf_prog_create+0x74/0xa4) [<c04a84d0>] (bpf_prog_create) from [<bf2af218>] (test_bpf_init+0x218/0x77c [test_bpf]) [<bf2af218>] (test_bpf_init [test_bpf]) from [<c0009738>] (do_one_initcall+0x40/0x178) [<c0009738>] (do_one_initcall) from [<c00a1548>] (do_init_module+0x58/0x1bc) [<c00a1548>] (do_init_module) from [<c007d6e8>] (load_module+0x1ae0/0x2050) [<c007d6e8>] (load_module) from [<c007de58>] (SyS_finit_module+0xa4/0xb8) [<c007de58>] (SyS_finit_module) from [<c000a3a0>] (ret_fast_syscall+0x0/0x38)Looking at the .lst file:
/* constants go just after the epilogue */ offset = ctx->offsets[ctx->prog->len]; c0015054: e591c000 ldr ip, [r1] c0015058: e5912018 ldr r2, [r1, #24] offset += ctx->prologue_bytes; offset += ctx->epilogue_bytes; c001505c: e1d132b0 ldrh r3, [r1, #32] offset = ctx->offsets[ctx->prog->len]; c0015060: e59ce008 ldr lr, [ip, #8] offset += ctx->epilogue_bytes; c0015064: e591c008 ldr ip, [r1, #8] c0015068: 0591501c ldreq r5, [r1, #28] offset = ctx->offsets[ctx->prog->len]; c001506c: e792210e ldr r2, [r2, lr, lsl #2] offset += ctx->epilogue_bytes; c0015070: e083300c add r3, r3, ip c0015074: e0833002 add r3, r3, r2 offset += i * 4;
c0015078:e0833004add r3, r3, r4
Quoted 8 lines
ctx->target[offset / 4] = k; c001507c: e3c32003 bic r2, r3, #3 c0015080: e7850002 str r0, [r5, r2]/* PC in ARM mode == address of the instruction + 8 */ imm = offset - (8 + ctx->idx * 4); Andrew
From 408d690e090b4a2c35b03a7ccff5e4655d41fcee Mon Sep 17 00:00:00 2001
From: Shubham Bansal <[email protected]>
Date: Thu, 25 May 2017 01:54:34 +0530
Subject: [PATCH] Editted to make it work on ARMv7, ARMv6 and ARMv5.
--- arch/arm/net/bpf_jit_32.c | 2406 ++++++++++++++++++++++++++++++--------------- arch/arm/net/bpf_jit_32.h | 108 +- 2 files changed, 1712 insertions(+), 802 deletions(-)
arch/arm/net/bpf_jit_32.c +1629 -777
@@ -1,13 +1,15 @@/* -* Just-In-Time compiler for BPF filters on 32bit ARM +* Just-In-Time compiler for eBPF filters on 32bit ARM * * Copyright (c) 2011 Mircea Gherzan <[email protected]> +* Copyright (c) 2017 Shubham Bansal <[email protected]> * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; version 2 of the License. */ +#include <linux/bpf.h> #include <linux/bitops.h> #include <linux/compiler.h> #include <linux/errno.h>
@@ -23,44 +25,91 @@#include "bpf_jit_32.h" +int bpf_jit_enable__read_mostly ; + +#define STACK_OFFSET (k) (k) +#define TMP_REG_1 (MAX_BPF_JIT_REG + 0)/* TEMP Register 1 */ +#define TMP_REG_2 (MAX_BPF_JIT_REG + 1)/* TEMP Register 2 */ +#define TCALL_CNT (MAX_BPF_JIT_REG + 2)/* Tail Call Count */ + +/* Flags used for JIT optimization */ +#define SEEN_CALL (1 << 0) + +#define FLAG_IMM_OVERFLOW (1 << 0) +/* -* ABI: +* Map eBPF registers to ARM 32bit registers or stack scratch space. +* +* 1. First argument is passed using the arm 32bit registers and rest of the +* arguments are passed on stack scratch space. +* 2. First callee-saved aregument is mapped to arm 32 bit registers and rest +* arguments are mapped to scratch space on stack. +* 3. We need two 64 bit temp registers to do complex operations on eBPF +* registers. +* +* As the eBPF registers are all 64 bit registers and arm has only 32 bit +* registers, we have to map each eBPF registers with two arm 32 bit regs or +* scratch memory space and we have to build eBPF 64 bit register from those. * -* r0 scratch register -* r4 BPF register A -* r5 BPF register X -* r6 pointer to the skb -* r7 skb->data -* r8 skb_headlen(skb) */ +static const u8 bpf2a32[][2 ] = { +/* return value from in-kernel function, and exit value from eBPF */ + [BPF_REG_0 ] = {ARM_R1 ,ARM_R0 }, +/* arguments from eBPF program to in-kernel function */ + [BPF_REG_1 ] = {ARM_R3 ,ARM_R2 }, +/* Stored on stack scratch space */ + [BPF_REG_2 ] = {STACK_OFFSET (0 ),STACK_OFFSET (4 )}, + [BPF_REG_3 ] = {STACK_OFFSET (8 ),STACK_OFFSET (12 )}, + [BPF_REG_4 ] = {STACK_OFFSET (16 ),STACK_OFFSET (20 )}, + [BPF_REG_5 ] = {STACK_OFFSET (24 ),STACK_OFFSET (28 )}, +/* callee saved registers that in-kernel function will preserve */ + [BPF_REG_6 ] = {ARM_R5 ,ARM_R4 }, +/* Stored on stack scratch space */ + [BPF_REG_7 ] = {STACK_OFFSET (32 ),STACK_OFFSET (36 )}, + [BPF_REG_8 ] = {STACK_OFFSET (40 ),STACK_OFFSET (44 )}, + [BPF_REG_9 ] = {STACK_OFFSET (48 ),STACK_OFFSET (52 )}, +/* Read only Frame Pointer to access Stack */ + [BPF_REG_FP ] = {STACK_OFFSET (56 ),STACK_OFFSET (60 )}, +/* Temporary Register for internal BPF JIT, can be used +* for constant blindings and others. +*/ + [TMP_REG_1 ] = {ARM_R7 ,ARM_R6 }, + [TMP_REG_2 ] = {ARM_R10 ,ARM_R8 }, +/* Tail call count. Stored on stack scratch space. */ + [TCALL_CNT ] = {STACK_OFFSET (64 ),STACK_OFFSET (68 )}, +/* temporary register for blinding constants. +* Stored on stack scratch space. +*/ + [BPF_REG_AX ] = {STACK_OFFSET (72 ),STACK_OFFSET (76 )}, +}; -#define r_scratch ARM_R0 -/* r1-r3 are (also) used for the unaligned loads on the non-ARMv7 slowpath */ -#define r_off ARM_R1 -#define r_A ARM_R4 -#define r_X ARM_R5 -#define r_skb ARM_R6 -#define r_skb_data ARM_R7 -#define r_skb_hl ARM_R8 - -#define SCRATCH_SP_OFFSET 0 -#define SCRATCH_OFF (k) (SCRATCH_SP_OFFSET + 4 * (k)) - -#define SEEN_MEM ((1 << BPF_MEMWORDS) - 1) -#define SEEN_MEM_WORD (k) (1 << (k)) -#define SEEN_X (1 << BPF_MEMWORDS) -#define SEEN_CALL (1 << (BPF_MEMWORDS + 1)) -#define SEEN_SKB (1 << (BPF_MEMWORDS + 2)) -#define SEEN_DATA (1 << (BPF_MEMWORDS + 3)) +#define dst_lo dst[1] +#define dst_hi dst[0] +#define src_lo src[1] +#define src_hi src[0] -#define FLAG_NEED_X_RESET (1 << 0) -#define FLAG_IMM_OVERFLOW (1 << 1) +/* +* JIT Context: +* +* prog : bpf_prog +* idx : index of current last JITed instruction. +* prologue_bytes : bytes used in prologue. +* epilogue_offset : offset of epilogue starting. +* seen : bit mask used for JIT optimization. +* offsets : array of eBPF instruction offsets in +* JITed code. +* target : final JITed code. +* epilogue_bytes : no of bytes used in epilogue. +* imm_count : no of immediate counts used for global +* variables. +* imms : array of global variable addresses. +*/ struct jit_ctx { -const struct bpf_prog *skf; -unsigned idx; -unsigned prologue_bytes; -int ret0_fp_idx; +const struct bpf_prog *prog; +unsigned int idx; +unsigned int prologue_bytes; +unsigned int epilogue_offset;u32 seen;u32 flags;u32 *offsets;
@@ -72,68 +121,16 @@ struct jit_ctx {#endif }; -int bpf_jit_enable__read_mostly ; - -static inline int call_neg_helper (struct sk_buff *skb,int offset,void *ret, -unsigned int size) -{ -void *ptr =bpf_internal_load_pointer_neg_helper (skb, offset, size); - -if (!ptr) -return -EFAULT ; -memcpy (ret, ptr, size); -return 0 ; -} - -static u64 jit_get_skb_b (struct sk_buff *skb,int offset) -{ -u8 ret; -int err; - -if (offset <0 ) - err =call_neg_helper (skb, offset, &ret,1 ); -else - err =skb_copy_bits (skb, offset, &ret,1 ); - -return (u64 )err <<32 | ret; -} - -static u64 jit_get_skb_h (struct sk_buff *skb,int offset) -{ -u16 ret; -int err; - -if (offset <0 ) - err =call_neg_helper (skb, offset, &ret,2 ); -else - err =skb_copy_bits (skb, offset, &ret,2 ); - -return (u64 )err <<32 |ntohs (ret); -} - -static u64 jit_get_skb_w (struct sk_buff *skb,int offset) -{ -u32 ret; -int err; - -if (offset <0 ) - err =call_neg_helper (skb, offset, &ret,4 ); -else - err =skb_copy_bits (skb, offset, &ret,4 ); - -return (u64 )err <<32 |ntohl (ret); -} -/* * Wrappers which handle both OABI and EABI and assures Thumb2 interworking * (where the assembly routines like __aeabi_uidiv could cause problems). */ -static u32 jit_udiv (u32 dividend,u32 divisor) +static u32 jit_udiv32 (u32 dividend,u32 divisor) {return dividend / divisor; } -static u32 jit_mod (u32 dividend,u32 divisor) +static u32 jit_mod32 (u32 dividend,u32 divisor) {return dividend % divisor; }
@@ -157,36 +154,22 @@ static inline void emit(u32 inst, struct jit_ctx *ctx)_emit (ARM_COND_AL , inst, ctx); } -static u16 saved_regs (struct jit_ctx *ctx) +/* +* Checks if immediate value can be converted to imm12(12 bits) value. +*/ +static int16_t imm8m (u32 x) { -u16 ret =0 ; - -if ((ctx->skf->len >1 ) || - (ctx->skf->insns[0 ].code == (BPF_RET |BPF_A ))) - ret |=1 << r_A; - -#ifdef CONFIG_FRAME_POINTER - ret |= (1 <<ARM_FP ) | (1 <<ARM_IP ) | (1 <<ARM_LR ) | (1 <<ARM_PC ); -#else -if (ctx->seen &SEEN_CALL ) - ret |=1 <<ARM_LR ; -#endif -if (ctx->seen & (SEEN_DATA |SEEN_SKB )) - ret |=1 << r_skb; -if (ctx->seen &SEEN_DATA ) - ret |= (1 << r_skb_data) | (1 << r_skb_hl); -if (ctx->seen &SEEN_X ) - ret |=1 << r_X; - -return ret; -} +u32 rot; -static inline int mem_words_used (struct jit_ctx *ctx) -{ -/* yes, we do waste some stack space IF there are "holes" in the set" */ -return fls (ctx->seen &SEEN_MEM ); +for (rot =0 ; rot <16 ; rot++) +if ((x & ~ror32 (0xff ,2 * rot)) ==0 ) +return rol32 (x,2 * rot) | (rot <<8 ); +return -1 ; } +/* +* Initializes the JIT space with undefined instructions. +*/ static void jit_fill_hole (void *area,unsigned int size) {u32 *ptr;
@@ -195,88 +178,34 @@ static void jit_fill_hole(void *area, unsigned int size) *ptr++ =__opcode_to_mem_arm (ARM_INST_UDF ); } -static void build_prologue (struct jit_ctx *ctx) -{ -u16 reg_set =saved_regs (ctx); -u16 off; - -#ifdef CONFIG_FRAME_POINTER -emit (ARM_MOV_R (ARM_IP ,ARM_SP ), ctx); -emit (ARM_PUSH (reg_set), ctx); -emit (ARM_SUB_I (ARM_FP ,ARM_IP ,4 ), ctx); -#else -if (reg_set) -emit (ARM_PUSH (reg_set), ctx); -#endif +/* Stack must be multiples of 16 Bytes */ +#define STACK_ALIGN (sz) (((sz) + 15) & ~15) -if (ctx->seen & (SEEN_DATA |SEEN_SKB )) -emit (ARM_MOV_R (r_skb,ARM_R0 ), ctx); - -if (ctx->seen &SEEN_DATA ) { - off = offsetof(struct sk_buff , data); -emit (ARM_LDR_I (r_skb_data, r_skb, off), ctx); -/* headlen = len - data_len */ - off = offsetof(struct sk_buff , len); -emit (ARM_LDR_I (r_skb_hl, r_skb, off), ctx); - off = offsetof(struct sk_buff , data_len); -emit (ARM_LDR_I (r_scratch, r_skb, off), ctx); -emit (ARM_SUB_R (r_skb_hl, r_skb_hl, r_scratch), ctx); - } - -if (ctx->flags &FLAG_NEED_X_RESET ) -emit (ARM_MOV_I (r_X,0 ), ctx); - -/* do not leak kernel data to userspace */ -if (bpf_needs_clear_a (&ctx->skf->insns[0 ])) -emit (ARM_MOV_I (r_A,0 ), ctx); - -/* stack space for the BPF_MEM words */ -if (ctx->seen &SEEN_MEM ) -emit (ARM_SUB_I (ARM_SP ,ARM_SP ,mem_words_used (ctx) *4 ), ctx); -} - -static void build_epilogue (struct jit_ctx *ctx) -{ -u16 reg_set =saved_regs (ctx); - -if (ctx->seen &SEEN_MEM ) -emit (ARM_ADD_I (ARM_SP ,ARM_SP ,mem_words_used (ctx) *4 ), ctx); - - reg_set &= ~(1 <<ARM_LR ); - -#ifdef CONFIG_FRAME_POINTER -/* the first instruction of the prologue was: mov ip, sp */ - reg_set &= ~(1 <<ARM_IP ); - reg_set |= (1 <<ARM_SP ); -emit (ARM_LDM (ARM_SP , reg_set), ctx); -#else -if (reg_set) { -if (ctx->seen &SEEN_CALL ) - reg_set |=1 <<ARM_PC ; -emit (ARM_POP (reg_set), ctx); - } +/* Stack space for BPF_REG_2, BPF_REG_3, BPF_REG_4, +* BPF_REG_5, BPF_REG_7, BPF_REG_8, BPF_REG_9, +* BPF_REG_FP and Tail call counts. +*/ +#define SCRATCH_SIZE 80 -if (!(ctx->seen &SEEN_CALL )) -emit (ARM_BX (ARM_LR ), ctx); -#endif -} +/* total stack size used in JITed code */ +#define _STACK_SIZE \ + (MAX_BPF_STACK + \ + + SCRATCH_SIZE + \ + + 4 /* extra for skb_copy_bits buffer */) -static int16_t imm8m (u32 x) -{ -u32 rot; +#define STACK_SIZE STACK_ALIGN(_STACK_SIZE) -for (rot =0 ; rot <16 ; rot++) -if ((x & ~ror32 (0xff ,2 * rot)) ==0 ) -return rol32 (x,2 * rot) | (rot <<8 ); +/* Get the offset of eBPF REGISTERs stored on scratch space. */ +#define STACK_VAR (off) (STACK_SIZE-off-4) -return -1 ; -} +/* Offset of skb_copy_bits buffer */ +#define SKB_BUFFER STACK_VAR(SCRATCH_SIZE)#if __LINUX_ARM_ARCH__ <7 static u16 imm_offset (u32 k,struct jit_ctx *ctx) { -unsigned i =0 , offset; +unsigned int i =0 , offset;u16 imm;/* on the "fake" run we just count them (duplicates included) */
@@ -295,7 +224,7 @@ static u16 imm_offset(u32 k, struct jit_ctx *ctx) ctx->imms[i] = k;/* constants go just after the epilogue */ - offset = ctx->offsets[ctx->skf->len]; + offset = ctx->offsets[ctx->prog->len -1 ] *4 ; offset += ctx->prologue_bytes; offset += ctx->epilogue_bytes; offset += i *4 ;
@@ -319,10 +248,22 @@ static u16 imm_offset(u32 k, struct jit_ctx *ctx)#endif /* __LINUX_ARM_ARCH__ */ +static inline int bpf2a32_offset (int bpf_to,int bpf_from, +const struct jit_ctx *ctx) { +int to, from; + +if (ctx->target ==NULL ) +return 0 ; + to = ctx->offsets[bpf_to]; + from = ctx->offsets[bpf_from]; + +return to - from -1 ; +} +/* * Move an immediate that's not an imm8m to a core register. */ -static inline void emit_mov_i_no8m (int rd,u32 val,struct jit_ctx *ctx) +static inline void emit_mov_i_no8m (const u8 rd,u32 val,struct jit_ctx *ctx) {#if __LINUX_ARM_ARCH__ <7 emit (ARM_LDR_I (rd,ARM_PC ,imm_offset (val, ctx)), ctx);
@@ -333,7 +274,7 @@ static inline void emit_mov_i_no8m(int rd, u32 val, struct jit_ctx *ctx)#endif } -static inline void emit_mov_i (int rd,u32 val,struct jit_ctx *ctx) +static inline void emit_mov_i (const u8 rd,u32 val,struct jit_ctx *ctx) {int imm12 =imm8m (val);
@@ -343,676 +284,1555 @@ static inline void emit_mov_i(int rd, u32 val, struct jit_ctx *ctx)emit_mov_i_no8m (rd, val, ctx); } -#if __LINUX_ARM_ARCH__ <6 - -static void emit_load_be32 (u8 cond,u8 r_res,u8 r_addr,struct jit_ctx *ctx) +static inline void emit_blx_r (u8 tgt_reg,struct jit_ctx *ctx) { -_emit (cond,ARM_LDRB_I (ARM_R3 , r_addr,1 ), ctx); -_emit (cond,ARM_LDRB_I (ARM_R1 , r_addr,0 ), ctx); -_emit (cond,ARM_LDRB_I (ARM_R2 , r_addr,3 ), ctx); -_emit (cond,ARM_LSL_I (ARM_R3 ,ARM_R3 ,16 ), ctx); -_emit (cond,ARM_LDRB_I (ARM_R0 , r_addr,2 ), ctx); -_emit (cond,ARM_ORR_S (ARM_R3 ,ARM_R3 ,ARM_R1 ,SRTYPE_LSL ,24 ), ctx); -_emit (cond,ARM_ORR_R (ARM_R3 ,ARM_R3 ,ARM_R2 ), ctx); -_emit (cond,ARM_ORR_S (r_res,ARM_R3 ,ARM_R0 ,SRTYPE_LSL ,8 ), ctx); + ctx->seen |=SEEN_CALL ; +#if __LINUX_ARM_ARCH__ <5 +emit (ARM_MOV_R (ARM_LR ,ARM_PC ), ctx); + +if (elf_hwcap &HWCAP_THUMB ) +emit (ARM_BX (tgt_reg), ctx); +else +emit (ARM_MOV_R (ARM_PC , tgt_reg), ctx); +#else +emit (ARM_BLX_R (tgt_reg), ctx); +#endif } -static void emit_load_be16 (u8 cond,u8 r_res,u8 r_addr,struct jit_ctx *ctx) +static inline int epilogue_offset (const struct jit_ctx *ctx) { -_emit (cond,ARM_LDRB_I (ARM_R1 , r_addr,0 ), ctx); -_emit (cond,ARM_LDRB_I (ARM_R2 , r_addr,1 ), ctx); -_emit (cond,ARM_ORR_S (r_res,ARM_R2 ,ARM_R1 ,SRTYPE_LSL ,8 ), ctx); +int to, from; +/* No need for 1st dummy run */ +if (ctx->target ==NULL ) +return 0 ; + to = ctx->epilogue_offset; + from = ctx->idx; + +return to - from -2 ; } -static inline void emit_swap16 (u8 r_dst,u8 r_src,struct jit_ctx *ctx) +static inline void emit_udivmod (u8 rd,u8 rm,u8 rn,struct jit_ctx *ctx,u8 op) { -/* r_dst = (r_src << 8) | (r_src >> 8) */ -emit (ARM_LSL_I (ARM_R1 , r_src,8 ), ctx); -emit (ARM_ORR_S (r_dst,ARM_R1 , r_src,SRTYPE_LSR ,8 ), ctx); +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +s32 jmp_offset; + +/* checks if divisor is zero or not. If it is, then +* exit directly. +*/ +emit (ARM_CMP_I (rn,0 ), ctx); +_emit (ARM_COND_EQ ,ARM_MOV_I (ARM_R0 ,0 ), ctx); + jmp_offset =epilogue_offset (ctx); +_emit (ARM_COND_EQ ,ARM_B (jmp_offset), ctx); +#if __LINUX_ARM_ARCH__ ==7 +if (elf_hwcap &HWCAP_IDIVA ) { +if (op ==BPF_DIV ) +emit (ARM_UDIV (rd, rm, rn), ctx); +else { +emit (ARM_UDIV (ARM_IP , rm, rn), ctx); +emit (ARM_MLS (rd, rn,ARM_IP , rm), ctx); + } +return ; + } +#endif /* -* we need to mask out the bits set in r_dst[23:16] due to -* the first shift instruction. -* -* note that 0x8ff is the encoded immediate 0x00ff0000. +* For BPF_ALU | BPF_DIV | BPF_K instructions +* As ARM_R1 and ARM_R0 contains 1st argument of bpf +* function, we need to save it on caller side to save +* it from getting destroyed within callee. +* After the return from the callee, we restore ARM_R0 +* ARM_R1. */ -emit (ARM_BIC_I (r_dst, r_dst,0x8ff ), ctx); -} +if (rn !=ARM_R1 ) { +emit (ARM_MOV_R (tmp[0 ],ARM_R1 ), ctx); +emit (ARM_MOV_R (ARM_R1 , rn), ctx); + } +if (rm !=ARM_R0 ) { +emit (ARM_MOV_R (tmp[1 ],ARM_R0 ), ctx); +emit (ARM_MOV_R (ARM_R0 , rm), ctx); + } -#else /* ARMv6+ */ +/* Call appropriate function */ + ctx->seen |=SEEN_CALL ; +emit_mov_i (ARM_IP , op ==BPF_DIV ? + (u32 )jit_udiv32 : (u32 )jit_mod32, ctx); +emit_blx_r (ARM_IP , ctx); -static void emit_load_be32 (u8 cond,u8 r_res,u8 r_addr,struct jit_ctx *ctx) -{ -_emit (cond,ARM_LDR_I (r_res, r_addr,0 ), ctx); -#ifdef __LITTLE_ENDIAN -_emit (cond,ARM_REV (r_res, r_res), ctx); -#endif +/* Save return value */ +if (rd !=ARM_R0 ) +emit (ARM_MOV_R (rd,ARM_R0 ), ctx); + +/* Restore ARM_R0 and ARM_R1 */ +if (rn !=ARM_R1 ) +emit (ARM_MOV_R (ARM_R1 , tmp[0 ]), ctx); +if (rm !=ARM_R0 ) +emit (ARM_MOV_R (ARM_R0 , tmp[1 ]), ctx); } -static void emit_load_be16 (u8 cond,u8 r_res,u8 r_addr,struct jit_ctx *ctx) +/* Checks whether BPF register is on scratch stack space or not. */ +static inline bool is_on_stack (u8 bpf_reg) { -_emit (cond,ARM_LDRH_I (r_res, r_addr,0 ), ctx); -#ifdef __LITTLE_ENDIAN -_emit (cond,ARM_REV16 (r_res, r_res), ctx); -#endif +static u8 stack_regs[] = {BPF_REG_AX ,BPF_REG_3 ,BPF_REG_4 ,BPF_REG_5 , +BPF_REG_7 ,BPF_REG_8 ,BPF_REG_9 ,TCALL_CNT , +BPF_REG_2 ,BPF_REG_FP }; +int i, reg_len =sizeof (stack_regs); + +for (i =0 ; i < reg_len ; i++) { +if (bpf_reg == stack_regs[i]) +return true; + } +return false; } -static inline void emit_swap16 (u8 r_dst__maybe_unused , -u8 r_src__maybe_unused , -struct jit_ctx *ctx__maybe_unused ) +static inline void emit_a32_mov_i (const u8 dst,const u32 val, +bool dstk,struct jit_ctx *ctx) { -#ifdef __LITTLE_ENDIAN -emit (ARM_REV16 (r_dst, r_src), ctx); -#endif +const u8 *tmp = bpf2a32[TMP_REG_1 ]; + +if (dstk) { +emit_mov_i (tmp[1 ], val, ctx); +emit (ARM_STR_I (tmp[1 ],ARM_SP ,STACK_VAR (dst)), ctx); + }else { +emit_mov_i (dst, val, ctx); + } } -#endif /* __LINUX_ARM_ARCH__ < 6 */ +/* Sign extended move */ +static inline void emit_a32_mov_i64 (const bool is64,const u8 dst[], +const u32 val,bool dstk, +struct jit_ctx *ctx) { +u32 hi =0 ; +if (is64 && (val & (1 <<31 ))) + hi = (u32 )~0 ; +emit_a32_mov_i (dst_lo, val, dstk, ctx); +emit_a32_mov_i (dst_hi, hi, dstk, ctx); +} -/* Compute the immediate value for a PC-relative branch. */ -static inline u32 b_imm (unsigned tgt,struct jit_ctx *ctx) -{ -u32 imm; +static inline void emit_a32_add_r (const u8 dst,const u8 src, +const bool is64,const bool hi, +struct jit_ctx *ctx) { +/* 64 bit : +* adds dst_lo, dst_lo, src_lo +* adc dst_hi, dst_hi, src_hi +* 32 bit : +* add dst_lo, dst_lo, src_lo +*/ +if (!hi && is64) +emit (ARM_ADDS_R (dst, dst, src), ctx); +else if (hi && is64) +emit (ARM_ADC_R (dst, dst, src), ctx); +else +emit (ARM_ADD_R (dst, dst, src), ctx); +} -if (ctx->target ==NULL ) -return 0 ; -/* -* BPF allows only forward jumps and the offset of the target is -* still the one computed during the first pass. +static inline void emit_a32_sub_r (const u8 dst,const u8 src, +const bool is64,const bool hi, +struct jit_ctx *ctx) { +/* 64 bit : +* subs dst_lo, dst_lo, src_lo +* sbc dst_hi, dst_hi, src_hi +* 32 bit : +* sub dst_lo, dst_lo, src_lo */ - imm = ctx->offsets[tgt] + ctx->prologue_bytes - (ctx->idx *4 +8 ); +if (!hi && is64) +emit (ARM_SUBS_R (dst, dst, src), ctx); +else if (hi && is64) +emit (ARM_SBC_R (dst, dst, src), ctx); +else +emit (ARM_SUB_R (dst, dst, src), ctx); +} -return imm >>2 ; +static inline void emit_alu_r (const u8 dst,const u8 src,const bool is64, +const bool hi,const u8 op,struct jit_ctx *ctx){ +switch (BPF_OP (op)) { +/* dst = dst + src */ +case BPF_ADD : +emit_a32_add_r (dst, src, is64, hi, ctx); +break ; +/* dst = dst - src */ +case BPF_SUB : +emit_a32_sub_r (dst, src, is64, hi, ctx); +break ; +/* dst = dst | src */ +case BPF_OR : +emit (ARM_ORR_R (dst, dst, src), ctx); +break ; +/* dst = dst & src */ +case BPF_AND : +emit (ARM_AND_R (dst, dst, src), ctx); +break ; +/* dst = dst ^ src */ +case BPF_XOR : +emit (ARM_EOR_R (dst, dst, src), ctx); +break ; +/* dst = dst * src */ +case BPF_MUL : +emit (ARM_MUL (dst, dst, src), ctx); +break ; +/* dst = dst << src */ +case BPF_LSH : +emit (ARM_LSL_R (dst, dst, src), ctx); +break ; +/* dst = dst >> src */ +case BPF_RSH : +emit (ARM_LSR_R (dst, dst, src), ctx); +break ; +/* dst = dst >> src (signed)*/ +case BPF_ARSH : +emit (ARM_MOV_SR (dst, dst,SRTYPE_ASR , src), ctx); +break ; + } } -#define OP_IMM3 (op, r1, r2, imm_val, ctx) \ - do { \ - imm12 = imm8m(imm_val); \ - if (imm12 < 0) { \ - emit_mov_i_no8m(r_scratch, imm_val, ctx); \ - emit(op ## _R((r1), (r2), r_scratch), ctx); \ - } else { \ - emit(op ## _I((r1), (r2), imm12), ctx); \ - } \ - } while (0) - -static inline void emit_err_ret (u8 cond,struct jit_ctx *ctx) -{ -if (ctx->ret0_fp_idx >=0 ) { -_emit (cond,ARM_B (b_imm (ctx->ret0_fp_idx, ctx)), ctx); -/* NOP to keep the size constant between passes */ -emit (ARM_MOV_R (ARM_R0 ,ARM_R0 ), ctx); +/* ALU operation (32 bit) +* dst = dst (op) src +*/ +static inline void emit_a32_alu_r (const u8 dst,const u8 src, +bool dstk,bool sstk, +struct jit_ctx *ctx,const bool is64, +const bool hi,const u8 op) { +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +u8 rn = sstk ? tmp[1 ] : src; + +if (sstk) +emit (ARM_LDR_I (rn,ARM_SP ,STACK_VAR (src)), ctx); + +/* ALU operation */ +if (dstk) { +emit (ARM_LDR_I (tmp[0 ],ARM_SP ,STACK_VAR (dst)), ctx); +emit_alu_r (tmp[0 ], rn, is64, hi, op, ctx); +emit (ARM_STR_I (tmp[0 ],ARM_SP ,STACK_VAR (dst)), ctx); }else { -_emit (cond,ARM_MOV_I (ARM_R0 ,0 ), ctx); -_emit (cond,ARM_B (b_imm (ctx->skf->len, ctx)), ctx); +emit_alu_r (dst, rn, is64, hi, op, ctx); } } -static inline void emit_blx_r (u8 tgt_reg,struct jit_ctx *ctx) -{ -#if __LINUX_ARM_ARCH__ <5 -emit (ARM_MOV_R (ARM_LR ,ARM_PC ), ctx); +/* ALU operation (64 bit) */ +static inline void emit_a32_alu_r64 (const bool is64,const u8 dst[], +const u8 src[],bool dstk, +bool sstk,struct jit_ctx *ctx, +const u8 op) { +emit_a32_alu_r (dst_lo, src_lo, dstk, sstk, ctx, is64, false, op); +if (is64) +emit_a32_alu_r (dst_hi, src_hi, dstk, sstk, ctx, is64, true, op); +else +emit_a32_mov_i (dst_hi,0 , dstk, ctx); +} -if (elf_hwcap &HWCAP_THUMB ) -emit (ARM_BX (tgt_reg), ctx); +/* dst = imm (4 bytes)*/ +static inline void emit_a32_mov_r (const u8 dst,const u8 src, +bool dstk,bool sstk, +struct jit_ctx *ctx) { +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +u8 rt = sstk ? tmp[0 ] : src; + +if (sstk) +emit (ARM_LDR_I (tmp[0 ],ARM_SP ,STACK_VAR (src)), ctx); +if (dstk) +emit (ARM_STR_I (rt,ARM_SP ,STACK_VAR (dst)), ctx);else -emit (ARM_MOV_R (ARM_PC , tgt_reg), ctx); -#else -emit (ARM_BLX_R (tgt_reg), ctx); -#endif +emit (ARM_MOV_R (dst, rt), ctx); } -static inline void emit_udivmod (u8 rd,u8 rm,u8 rn,struct jit_ctx *ctx, -int bpf_op) -{ -#if __LINUX_ARM_ARCH__ ==7 -if (elf_hwcap &HWCAP_IDIVA ) { -if (bpf_op ==BPF_DIV ) -emit (ARM_UDIV (rd, rm, rn), ctx); -else { -emit (ARM_UDIV (ARM_R3 , rm, rn), ctx); -emit (ARM_MLS (rd, rn,ARM_R3 , rm), ctx); - } -return ; +/* dst = src */ +static inline void emit_a32_mov_r64 (const bool is64,const u8 dst[], +const u8 src[],bool dstk, +bool sstk,struct jit_ctx *ctx) { +emit_a32_mov_r (dst_lo, src_lo, dstk, sstk, ctx); +if (is64) { +/* complete 8 byte move */ +emit_a32_mov_r (dst_hi, src_hi, dstk, sstk, ctx); + }else { +/* Zero out high 4 bytes */ +emit_a32_mov_i (dst_hi,0 , dstk, ctx); } -#endif +} -/* -* For BPF_ALU | BPF_DIV | BPF_K instructions, rm is ARM_R4 -* (r_A) and rn is ARM_R0 (r_scratch) so load rn first into -* ARM_R1 to avoid accidentally overwriting ARM_R0 with rm -* before using it as a source for ARM_R1. -* -* For BPF_ALU | BPF_DIV | BPF_X rm is ARM_R4 (r_A) and rn is -* ARM_R5 (r_X) so there is no particular register overlap -* issues. -*/ -if (rn !=ARM_R1 ) -emit (ARM_MOV_R (ARM_R1 , rn), ctx); -if (rm !=ARM_R0 ) -emit (ARM_MOV_R (ARM_R0 , rm), ctx); +/* Shift operations */ +static inline void emit_a32_alu_i (const u8 dst,const u32 val,bool dstk, +struct jit_ctx *ctx,const u8 op) { +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +u8 rd = dstk ? tmp[0 ] : dst; + +if (dstk) +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst)), ctx); + +/* Do shift operation */ +switch (op) { +case BPF_LSH : +emit (ARM_LSL_I (rd, rd, val), ctx); +break ; +case BPF_RSH : +emit (ARM_LSR_I (rd, rd, val), ctx); +break ; +case BPF_NEG : +emit (ARM_RSB_I (rd, rd, val), ctx); +break ; + } + +if (dstk) +emit (ARM_STR_I (rd,ARM_SP ,STACK_VAR (dst)), ctx); +} + +/* dst = ~dst (64 bit) */ +static inline void emit_a32_neg64 (const u8 dst[],bool dstk, +struct jit_ctx *ctx){ +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +u8 rd = dstk ? tmp[1 ] : dst[1 ]; +u8 rm = dstk ? tmp[0 ] : dst[0 ]; + +/* Setup Operand */ +if (dstk) { +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_LDR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } + +/* Do Negate Operation */ +emit (ARM_RSBS_I (rd, rd,0 ), ctx); +emit (ARM_RSC_I (rm, rm,0 ), ctx); + +if (dstk) { +emit (ARM_STR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_STR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } +} +/* dst = dst << src */ +static inline void emit_a32_lsh_r64 (const u8 dst[],const u8 src[],bool dstk, +bool sstk,struct jit_ctx *ctx) { +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; + +/* Setup Operands */ +u8 rt = sstk ? tmp2[1 ] : src_lo; +u8 rd = dstk ? tmp[1 ] : dst_lo; +u8 rm = dstk ? tmp[0 ] : dst_hi; + +if (sstk) +emit (ARM_LDR_I (rt,ARM_SP ,STACK_VAR (src_lo)), ctx); +if (dstk) { +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_LDR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } + +/* Do LSH operation */ +emit (ARM_SUB_I (ARM_IP , rt,32 ), ctx); +emit (ARM_RSB_I (tmp2[0 ], rt,32 ), ctx); +/* As we are using ARM_LR */ ctx->seen |=SEEN_CALL ; -emit_mov_i (ARM_R3 , bpf_op ==BPF_DIV ? (u32 )jit_udiv : (u32 )jit_mod, - ctx); -emit_blx_r (ARM_R3 , ctx); +emit (ARM_MOV_SR (ARM_LR , rm,SRTYPE_ASL , rt), ctx); +emit (ARM_ORR_SR (ARM_LR ,ARM_LR , rd,SRTYPE_ASL ,ARM_IP ), ctx); +emit (ARM_ORR_SR (ARM_IP ,ARM_LR , rd,SRTYPE_LSR , tmp2[0 ]), ctx); +emit (ARM_MOV_SR (ARM_LR , rd,SRTYPE_ASL , rt), ctx); + +if (dstk) { +emit (ARM_STR_I (ARM_LR ,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_STR_I (ARM_IP ,ARM_SP ,STACK_VAR (dst_hi)), ctx); + }else { +emit (ARM_MOV_R (rd,ARM_LR ), ctx); +emit (ARM_MOV_R (rm,ARM_IP ), ctx); + } +} -if (rd !=ARM_R0 ) -emit (ARM_MOV_R (rd,ARM_R0 ), ctx); +/* dst = dst >> src (signed)*/ +static inline void emit_a32_arsh_r64 (const u8 dst[],const u8 src[],bool dstk, +bool sstk,struct jit_ctx *ctx) { +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; +/* Setup Operands */ +u8 rt = sstk ? tmp2[1 ] : src_lo; +u8 rd = dstk ? tmp[1 ] : dst_lo; +u8 rm = dstk ? tmp[0 ] : dst_hi; + +if (sstk) +emit (ARM_LDR_I (rt,ARM_SP ,STACK_VAR (src_lo)), ctx); +if (dstk) { +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_LDR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } + +/* Do the ARSH operation */ +emit (ARM_RSB_I (ARM_IP , rt,32 ), ctx); +emit (ARM_SUBS_I (tmp2[0 ], rt,32 ), ctx); +/* As we are using ARM_LR */ + ctx->seen |=SEEN_CALL ; +emit (ARM_MOV_SR (ARM_LR , rd,SRTYPE_LSR , rt), ctx); +emit (ARM_ORR_SR (ARM_LR ,ARM_LR , rm,SRTYPE_ASL ,ARM_IP ), ctx); +_emit (ARM_COND_MI ,ARM_B (0 ), ctx); +emit (ARM_ORR_SR (ARM_LR ,ARM_LR , rm,SRTYPE_ASR , tmp2[0 ]), ctx); +emit (ARM_MOV_SR (ARM_IP , rm,SRTYPE_ASR , rt), ctx); +if (dstk) { +emit (ARM_STR_I (ARM_LR ,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_STR_I (ARM_IP ,ARM_SP ,STACK_VAR (dst_hi)), ctx); + }else { +emit (ARM_MOV_R (rd,ARM_LR ), ctx); +emit (ARM_MOV_R (rm,ARM_IP ), ctx); + } } -static inline void update_on_xread (struct jit_ctx *ctx) +/* dst = dst >> src */ +static inline void emit_a32_lsr_r64 (const u8 dst[],const u8 src[],bool dstk, +bool sstk,struct jit_ctx *ctx) { +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; +/* Setup Operands */ +u8 rt = sstk ? tmp2[1 ] : src_lo; +u8 rd = dstk ? tmp[1 ] : dst_lo; +u8 rm = dstk ? tmp[0 ] : dst_hi; + +if (sstk) +emit (ARM_LDR_I (rt,ARM_SP ,STACK_VAR (src_lo)), ctx); +if (dstk) { +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_LDR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } + +/* Do LSH operation */ +emit (ARM_RSB_I (ARM_IP , rt,32 ), ctx); +emit (ARM_SUBS_I (tmp2[0 ], rt,32 ), ctx); +/* As we are using ARM_LR */ + ctx->seen |=SEEN_CALL ; +emit (ARM_MOV_SR (ARM_LR , rd,SRTYPE_LSR , rt), ctx); +emit (ARM_ORR_SR (ARM_LR ,ARM_LR , rm,SRTYPE_ASL ,ARM_IP ), ctx); +emit (ARM_ORR_SR (ARM_LR ,ARM_LR , rm,SRTYPE_LSR , tmp2[0 ]), ctx); +emit (ARM_MOV_SR (ARM_IP , rm,SRTYPE_LSR , rt), ctx); +if (dstk) { +emit (ARM_STR_I (ARM_LR ,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_STR_I (ARM_IP ,ARM_SP ,STACK_VAR (dst_hi)), ctx); + }else { +emit (ARM_MOV_R (rd,ARM_LR ), ctx); +emit (ARM_MOV_R (rm,ARM_IP ), ctx); + } +} + +/* dst = dst << val */ +static inline void emit_a32_lsh_i64 (const u8 dst[],bool dstk, +const u32 val,struct jit_ctx *ctx){ +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; +/* Setup operands */ +u8 rd = dstk ? tmp[1 ] : dst_lo; +u8 rm = dstk ? tmp[0 ] : dst_hi; + +if (dstk) { +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_LDR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } + +/* Do LSH operation */ +if (val <32 ) { +emit (ARM_MOV_SI (tmp2[0 ], rm,SRTYPE_ASL , val), ctx); +emit (ARM_ORR_SI (rm, tmp2[0 ], rd,SRTYPE_LSR ,32 - val), ctx); +emit (ARM_MOV_SI (rd, rd,SRTYPE_ASL , val), ctx); + }else { +if (val ==32 ) +emit (ARM_MOV_R (rm, rd), ctx); +else +emit (ARM_MOV_SI (rm, rd,SRTYPE_ASL , val -32 ), ctx); +emit (ARM_EOR_R (rd, rd, rd), ctx); + } + +if (dstk) { +emit (ARM_STR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_STR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } +} + +/* dst = dst >> val */ +static inline void emit_a32_lsr_i64 (const u8 dst[],bool dstk, +const u32 val,struct jit_ctx *ctx) { +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; +/* Setup operands */ +u8 rd = dstk ? tmp[1 ] : dst_lo; +u8 rm = dstk ? tmp[0 ] : dst_hi; + +if (dstk) { +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_LDR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } + +/* Do LSR operation */ +if (val <32 ) { +emit (ARM_MOV_SI (tmp2[1 ], rd,SRTYPE_LSR , val), ctx); +emit (ARM_ORR_SI (rd, tmp2[1 ], rm,SRTYPE_ASL ,32 - val), ctx); +emit (ARM_MOV_SI (rm, rm,SRTYPE_LSR , val), ctx); + }else if (val ==32 ) { +emit (ARM_MOV_R (rd, rm), ctx); +emit (ARM_MOV_I (rm,0 ), ctx); + }else { +emit (ARM_MOV_SI (rd, rm,SRTYPE_LSR , val -32 ), ctx); +emit (ARM_MOV_I (rm,0 ), ctx); + } + +if (dstk) { +emit (ARM_STR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_STR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } +} + +/* dst = dst >> val (signed) */ +static inline void emit_a32_arsh_i64 (const u8 dst[],bool dstk, +const u32 val,struct jit_ctx *ctx){ +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; +/* Setup operands */ +u8 rd = dstk ? tmp[1 ] : dst_lo; +u8 rm = dstk ? tmp[0 ] : dst_hi; + +if (dstk) { +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_LDR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } + +/* Do ARSH operation */ +if (val <32 ) { +emit (ARM_MOV_SI (tmp2[1 ], rd,SRTYPE_LSR , val), ctx); +emit (ARM_ORR_SI (rd, tmp2[1 ], rm,SRTYPE_ASL ,32 - val), ctx); +emit (ARM_MOV_SI (rm, rm,SRTYPE_ASR , val), ctx); + }else if (val ==32 ) { +emit (ARM_MOV_R (rd, rm), ctx); +emit (ARM_MOV_SI (rm, rm,SRTYPE_ASR ,31 ), ctx); + }else { +emit (ARM_MOV_SI (rd, rm,SRTYPE_ASR , val -32 ), ctx); +emit (ARM_MOV_SI (rm, rm,SRTYPE_ASR ,31 ), ctx); + } + +if (dstk) { +emit (ARM_STR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_STR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } +} + +static inline void emit_a32_mul_r64 (const u8 dst[],const u8 src[],bool dstk, +bool sstk,struct jit_ctx *ctx) { +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; +/* Setup operands for multiplication */ +u8 rd = dstk ? tmp[1 ] : dst_lo; +u8 rm = dstk ? tmp[0 ] : dst_hi; +u8 rt = sstk ? tmp2[1 ] : src_lo; +u8 rn = sstk ? tmp2[0 ] : src_hi; + +if (dstk) { +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_LDR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } +if (sstk) { +emit (ARM_LDR_I (rt,ARM_SP ,STACK_VAR (src_lo)), ctx); +emit (ARM_LDR_I (rn,ARM_SP ,STACK_VAR (src_hi)), ctx); + } + +/* Do Multiplication */ +emit (ARM_MUL (ARM_IP , rd, rn), ctx); +emit (ARM_MUL (ARM_LR , rm, rt), ctx); +/* As we are using ARM_LR */ + ctx->seen |=SEEN_CALL ; +emit (ARM_ADD_R (ARM_LR ,ARM_IP ,ARM_LR ), ctx); + +emit (ARM_UMULL (ARM_IP , rm, rd, rt), ctx); +emit (ARM_ADD_R (rm,ARM_LR , rm), ctx); +if (dstk) { +emit (ARM_STR_I (ARM_IP ,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_STR_I (rm,ARM_SP ,STACK_VAR (dst_hi)), ctx); + }else { +emit (ARM_MOV_R (rd,ARM_IP ), ctx); + } +} + +/* *(size *)(dst + off) = src */ +static inline void emit_str_r (const u8 dst,const u8 src,bool dstk, +const s32 off,struct jit_ctx *ctx,const u8 sz){ +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +u8 rd = dstk ? tmp[1 ] : dst; + +if (dstk) +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst)), ctx); +if (off) { +emit_a32_mov_i (tmp[0 ], off, false, ctx); +emit (ARM_ADD_R (tmp[0 ], rd, tmp[0 ]), ctx); + rd = tmp[0 ]; + } +switch (sz) { +case BPF_W : +/* Store a Word */ +emit (ARM_STR_I (src, rd,0 ), ctx); +break ; +case BPF_H : +/* Store a HalfWord */ +emit (ARM_STRH_I (src, rd,0 ), ctx); +break ; +case BPF_B : +/* Store a Byte */ +emit (ARM_STRB_I (src, rd,0 ), ctx); +break ; + } +} + +/* dst = *(size*)(src + off) */ +static inline void emit_ldx_r (const u8 dst,const u8 src,bool dstk, +const s32 off,struct jit_ctx *ctx,const u8 sz){ +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +u8 rd = dstk ? tmp[1 ] : dst; +u8 rm = src; + +if (off) { +emit_a32_mov_i (tmp[0 ], off, false, ctx); +emit (ARM_ADD_R (tmp[0 ], tmp[0 ], src), ctx); + rm = tmp[0 ]; + } +switch (sz) { +case BPF_W : +/* Load a Word */ +emit (ARM_LDR_I (rd, rm,0 ), ctx); +break ; +case BPF_H : +/* Load a HalfWord */ +emit (ARM_LDRH_I (rd, rm,0 ), ctx); +break ; +case BPF_B : +/* Load a Byte */ +emit (ARM_LDRB_I (rd, rm,0 ), ctx); +break ; + } +if (dstk) +emit (ARM_STR_I (rd,ARM_SP ,STACK_VAR (dst)), ctx); +} + +/* Arithmatic Operation */ +static inline void emit_ar_r (const u8 rd,const u8 rt,const u8 rm, +const u8 rn,struct jit_ctx *ctx,u8 op) { +switch (op) { +case BPF_JSET : + ctx->seen |=SEEN_CALL ; +emit (ARM_AND_R (ARM_IP , rt, rn), ctx); +emit (ARM_AND_R (ARM_LR , rd, rm), ctx); +emit (ARM_ORRS_R (ARM_IP ,ARM_LR ,ARM_IP ), ctx); +break ; +case BPF_JEQ : +case BPF_JNE : +case BPF_JGT : +case BPF_JGE : +emit (ARM_CMP_R (rd, rm), ctx); +_emit (ARM_COND_EQ ,ARM_CMP_R (rt, rn), ctx); +break ; +case BPF_JSGT : +emit (ARM_CMP_R (rn, rt), ctx); +emit (ARM_SBCS_R (ARM_IP , rm, rd), ctx); +break ; +case BPF_JSGE : +emit (ARM_CMP_R (rt, rn), ctx); +emit (ARM_SBCS_R (ARM_IP , rd, rm), ctx); +break ; + } +} + +static int out_offset =-1 ;/* initialized on the first pass of build_body() */ +static int emit_bpf_tail_call (struct jit_ctx *ctx) +{ + +/* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */ +const u8 *r2 = bpf2a32[BPF_REG_2 ]; +const u8 *r3 = bpf2a32[BPF_REG_3 ]; +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; +const u8 *tcc = bpf2a32[TCALL_CNT ]; +const int idx0 = ctx->idx; +#define cur_offset (ctx->idx - idx0) +#define jmp_offset (out_offset - (cur_offset)) +u32 off, lo, hi; + +/* if (index >= array->map.max_entries) +* goto out; +*/ + off = offsetof(struct bpf_array, map.max_entries); +/* array->map.max_entries */ +emit_a32_mov_i (tmp[1 ], off, false, ctx); +emit (ARM_LDR_I (tmp2[1 ],ARM_SP ,STACK_VAR (r2[1 ])), ctx); +emit (ARM_LDR_R (tmp[1 ], tmp2[1 ], tmp[1 ]), ctx); +/* index (64 bit) */ +emit (ARM_LDR_I (tmp2[1 ],ARM_SP ,STACK_VAR (r3[1 ])), ctx); +/* index >= array->map.max_entries */ +emit (ARM_CMP_R (tmp2[1 ], tmp[1 ]), ctx); +_emit (ARM_COND_CS ,ARM_B (jmp_offset), ctx); + +/* if (tail_call_cnt > MAX_TAIL_CALL_CNT) +* goto out; +* tail_call_cnt++; +*/ + lo = (u32 )MAX_TAIL_CALL_CNT ; + hi = (u32)((u64 )MAX_TAIL_CALL_CNT >>32 ); +emit (ARM_LDR_I (tmp[1 ],ARM_SP ,STACK_VAR (tcc[1 ])), ctx); +emit (ARM_LDR_I (tmp[0 ],ARM_SP ,STACK_VAR (tcc[0 ])), ctx); +emit (ARM_CMP_I (tmp[0 ], hi), ctx); +_emit (ARM_COND_EQ ,ARM_CMP_I (tmp[1 ], lo), ctx); +_emit (ARM_COND_HI ,ARM_B (jmp_offset), ctx); +emit (ARM_ADDS_I (tmp[1 ], tmp[1 ],1 ), ctx); +emit (ARM_ADC_I (tmp[0 ], tmp[0 ],0 ), ctx); +emit (ARM_STR_I (tmp[1 ],ARM_SP ,STACK_VAR (tcc[1 ])), ctx); +emit (ARM_STR_I (tmp[0 ],ARM_SP ,STACK_VAR (tcc[0 ])), ctx); + +/* prog = array->ptrs[index] +* if (prog == NULL) +* goto out; +*/ + off = offsetof(struct bpf_array , ptrs); +emit_a32_mov_i (tmp[1 ], off, false, ctx); +emit (ARM_LDR_I (tmp2[1 ],ARM_SP ,STACK_VAR (r2[1 ])), ctx); +emit (ARM_LDR_R (tmp[1 ], tmp2[1 ], tmp[1 ]), ctx); +emit (ARM_LDR_I (tmp2[1 ],ARM_SP ,STACK_VAR (r3[1 ])), ctx); +emit (ARM_MOV_SI (tmp[0 ], tmp2[1 ],SRTYPE_ASL ,2 ), ctx); +emit (ARM_LDR_R (tmp[1 ], tmp[1 ], tmp[0 ]), ctx); +emit (ARM_CMP_I (tmp[1 ],0 ), ctx); +_emit (ARM_COND_EQ ,ARM_B (jmp_offset), ctx); + +/* goto *(prog->bpf_func + prologue_size); */ + off = offsetof(struct bpf_prog , bpf_func); +emit_a32_mov_i (tmp2[1 ], off, false, ctx); +emit (ARM_LDR_R (tmp[1 ], tmp[1 ], tmp2[1 ]), ctx); +emit (ARM_ADD_I (tmp[1 ], tmp[1 ], ctx->prologue_bytes), ctx); +emit (ARM_BX (tmp[1 ]), ctx); + +/* out: */ +if (out_offset ==-1 ) + out_offset = cur_offset; +if (cur_offset != out_offset) { +pr_err_once ("tail_call out_offset = %d, expected %d!\n" , + cur_offset, out_offset); +return -1 ; + } +return 0 ; +#undef cur_offset +#undef jmp_offset +} + +/* 0xabcd => 0xcdab */ +static inline void emit_rev16 (const u8 rd,const u8 rn,struct jit_ctx *ctx) { -if (!(ctx->seen &SEEN_X )) - ctx->flags |=FLAG_NEED_X_RESET ; +#if __LINUX_ARM_ARCH__ <6 +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; + +emit (ARM_AND_I (tmp2[1 ], rn,0xff ), ctx); +emit (ARM_MOV_SI (tmp2[0 ], rn,SRTYPE_LSR ,8 ), ctx); +emit (ARM_AND_I (tmp2[0 ], tmp2[0 ],0xff ), ctx); +emit (ARM_ORR_SI (rd, tmp2[0 ], tmp2[1 ],SRTYPE_LSL ,8 ), ctx); +#else /* ARMv6+ */ +emit (ARM_REV16 (rd, rn), ctx); +#endif +} - ctx->seen |=SEEN_X ; +/* 0xabcdefgh => 0xghefcdab */ +static inline void emit_rev32 (const u8 rd,const u8 rn,struct jit_ctx *ctx) +{ +#if __LINUX_ARM_ARCH__ <6 +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; + +emit (ARM_AND_I (tmp2[1 ], rn,0xff ), ctx); +emit (ARM_MOV_SI (tmp2[0 ], rn,SRTYPE_LSR ,24 ), ctx); +emit (ARM_ORR_SI (ARM_IP , tmp2[0 ], tmp2[1 ],SRTYPE_LSL ,24 ), ctx); + +emit (ARM_MOV_SI (tmp2[1 ], rn,SRTYPE_LSR ,8 ), ctx); +emit (ARM_AND_I (tmp2[1 ], tmp2[1 ],0xff ), ctx); +emit (ARM_MOV_SI (tmp2[0 ], rn,SRTYPE_LSR ,16 ), ctx); +emit (ARM_AND_I (tmp2[0 ], tmp2[0 ],0xff ), ctx); +emit (ARM_MOV_SI (tmp2[0 ], tmp2[0 ],SRTYPE_LSL ,8 ), ctx); +emit (ARM_ORR_SI (tmp2[0 ], tmp2[0 ], tmp2[1 ],SRTYPE_LSL ,16 ), ctx); +emit (ARM_ORR_R (rd,ARM_IP , tmp2[0 ]), ctx); + +#else /* ARMv6+ */ +emit (ARM_REV (rd, rn), ctx); +#endif } -static int build_body (struct jit_ctx *ctx) +static void build_prologue (struct jit_ctx *ctx) { -void *load_func[] = {jit_get_skb_b, jit_get_skb_h, jit_get_skb_w}; -const struct bpf_prog *prog = ctx->skf; -const struct sock_filter *inst; -unsigned i, load_order, off, condt; -int imm12; -u32 k; +const u8 r0 = bpf2a32[BPF_REG_0 ][1 ]; +const u8 r2 = bpf2a32[BPF_REG_1 ][1 ]; +const u8 r3 = bpf2a32[BPF_REG_1 ][0 ]; +const u8 r4 = bpf2a32[BPF_REG_6 ][1 ]; +const u8 r5 = bpf2a32[BPF_REG_6 ][0 ]; +const u8 r6 = bpf2a32[TMP_REG_1 ][1 ]; +const u8 r7 = bpf2a32[TMP_REG_1 ][0 ]; +const u8 r8 = bpf2a32[TMP_REG_2 ][1 ]; +const u8 r10 = bpf2a32[TMP_REG_2 ][0 ]; +const u8 fplo = bpf2a32[BPF_REG_FP ][1 ]; +const u8 fphi = bpf2a32[BPF_REG_FP ][0 ]; +const u8 sp =ARM_SP ; +const u8 *tcc = bpf2a32[TCALL_CNT ]; + +u16 reg_set =0 ; -for (i =0 ; i < prog->len; i++) { -u16 code; +/* +* eBPF prog stack layout +* +* high +* original ARM_SP => +-----+ eBPF prologue +* |FP/LR| +* current ARM_FP => +-----+ +* | ... | callee saved registers +* eBPF fp register => +-----+ <= (BPF_FP) +* | ... | eBPF JIT scratch space +* | | eBPF prog stack +* +-----+ +* |RSVD | JIT scratchpad +* current A64_SP => +-----+ <= (BPF_FP - STACK_SIZE) +* | | +* | ... | Function call stack +* | | +* +-----+ +* low +*/ - inst = &(prog->insns[i]); -/* K as an immediate value operand */ - k = inst->k; - code =bpf_anc_helper (inst); +/* Save callee saved registers. */ + reg_set |= (1 <<r4) | (1 <<r5) | (1 <<r6) | (1 <<r7) | (1 <<r8) | (1 <<r10); +#ifdef CONFIG_FRAME_POINTER + reg_set |= (1 <<ARM_FP ) | (1 <<ARM_IP ) | (1 <<ARM_LR ) | (1 <<ARM_PC ); +emit (ARM_MOV_R (ARM_IP , sp), ctx); +emit (ARM_PUSH (reg_set), ctx); +emit (ARM_SUB_I (ARM_FP ,ARM_IP ,4 ), ctx); +#else +/* Check if call instruction exists in BPF body */ +if (ctx->seen &SEEN_CALL ) + reg_set |= (1 <<ARM_LR ); +emit (ARM_PUSH (reg_set), ctx); +#endif +/* Save frame pointer for later */ +emit (ARM_SUB_I (ARM_IP , sp,SCRATCH_SIZE ), ctx); + +/* Set up function call stack */ +emit (ARM_SUB_I (ARM_SP ,ARM_SP ,imm8m (STACK_SIZE )), ctx); + +/* Set up BPF prog stack base register */ +emit_a32_mov_r (fplo,ARM_IP , true, false, ctx); +emit_a32_mov_i (fphi,0 , true, ctx); + +/* mov r4, 0 */ +emit (ARM_MOV_I (r4,0 ), ctx); +/* MOV bpf_ctx pointer to BPF_R1 */ +emit (ARM_MOV_R (r3, r4), ctx); +emit (ARM_MOV_R (r2, r0), ctx); +/* Initialize Tail Count */ +emit (ARM_STR_I (r4,ARM_SP ,STACK_VAR (tcc[0 ])), ctx); +emit (ARM_STR_I (r4,ARM_SP ,STACK_VAR (tcc[1 ])), ctx); +/* end of prologue */ +} -/* compute offsets only in the fake pass */ -if (ctx->target ==NULL ) - ctx->offsets[i] = ctx->idx *4 ; +static void build_epilogue (struct jit_ctx *ctx) +{ +const u8 r4 = bpf2a32[BPF_REG_6 ][1 ]; +const u8 r5 = bpf2a32[BPF_REG_6 ][0 ]; +const u8 r6 = bpf2a32[TMP_REG_1 ][1 ]; +const u8 r7 = bpf2a32[TMP_REG_1 ][0 ]; +const u8 r8 = bpf2a32[TMP_REG_2 ][1 ]; +const u8 r10 = bpf2a32[TMP_REG_2 ][0 ]; +u16 reg_set =0 ; + +/* unwind function call stack */ +emit (ARM_ADD_I (ARM_SP ,ARM_SP ,imm8m (STACK_SIZE )), ctx); + +/* restore callee saved registers. */ + reg_set |= (1 <<r4) | (1 <<r5) | (1 <<r6) | (1 <<r7) | (1 <<r8) | (1 <<r10); +#ifdef CONFIG_FRAME_POINTER +/* the first instruction of the prologue was: mov ip, sp */ + reg_set |= (1 <<ARM_FP ) | (1 <<ARM_SP ) | (1 <<ARM_PC ); +emit (ARM_LDM (ARM_SP , reg_set), ctx); +#else +if (ctx->seen &SEEN_CALL ) + reg_set |= (1 <<ARM_PC ); +/* Restore callee saved registers. */ +emit (ARM_POP (reg_set), ctx); +/* Return back to the callee function */ +if (!(ctx->seen &SEEN_CALL )) +emit (ARM_BX (ARM_LR ), ctx); +#endif +} -switch (code) { -case BPF_LD |BPF_IMM : -emit_mov_i (r_A, k, ctx); +/* +* Convert an eBPF instruction to native instruction, i.e +* JITs an eBPF instruction. +* Returns : +* 0 - Successfully JITed an 8-byte eBPF instruction +* >0 - Successfully JITed a 16-byte eBPF instruction +* <0 - Failed to JIT. +*/ +static int build_insn (const struct bpf_insn *insn,struct jit_ctx *ctx) +{ +const u8 code = insn->code; +const u8 *dst = bpf2a32[insn->dst_reg]; +const u8 *src = bpf2a32[insn->src_reg]; +const u8 *tmp = bpf2a32[TMP_REG_1 ]; +const u8 *tmp2 = bpf2a32[TMP_REG_2 ]; +const s16 off = insn->off; +const s32 imm = insn->imm; +const int i = insn - ctx->prog->insnsi; +const bool is64 =BPF_CLASS (code) ==BPF_ALU64 ; +const bool dstk =is_on_stack (insn->dst_reg); +const bool sstk =is_on_stack (insn->src_reg); +u8 rd, rt, rm, rn; +s32 jmp_offset; + +#define check_imm (bits, imm) do { \ + if ((((imm) > 0) && ((imm) >> (bits))) || \ + (((imm) < 0) && (~(imm) >> (bits)))) { \ + pr_info("[%2d] imm=%d(0x%x) out of range\n", \ + i, imm, imm); \ + return -EINVAL; \ + } \ +} while (0) +#define check_imm24 (imm) check_imm(24, imm) + +switch (code) { +/* ALU operations */ + +/* dst = src */ +case BPF_ALU |BPF_MOV |BPF_K : +case BPF_ALU |BPF_MOV |BPF_X : +case BPF_ALU64 |BPF_MOV |BPF_K : +case BPF_ALU64 |BPF_MOV |BPF_X : +switch (BPF_SRC (code)) { +case BPF_X : +emit_a32_mov_r64 (is64, dst, src, dstk, sstk, ctx);break ; -case BPF_LD |BPF_W |BPF_LEN : - ctx->seen |=SEEN_SKB ; - BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4); -emit (ARM_LDR_I (r_A, r_skb, - offsetof(struct sk_buff , len)), ctx); +case BPF_K : +/* Sign-extend immediate value to destination reg */ +emit_a32_mov_i64 (is64, dst, imm, dstk, ctx);break ; -case BPF_LD |BPF_MEM : -/* A = scratch[k] */ - ctx->seen |=SEEN_MEM_WORD (k); -emit (ARM_LDR_I (r_A,ARM_SP ,SCRATCH_OFF (k)), ctx); + } +break ; +/* dst = dst + src/imm */ +/* dst = dst - src/imm */ +/* dst = dst | src/imm */ +/* dst = dst & src/imm */ +/* dst = dst ^ src/imm */ +/* dst = dst * src/imm */ +/* dst = dst << src */ +/* dst = dst >> src */ +case BPF_ALU |BPF_ADD |BPF_K : +case BPF_ALU |BPF_ADD |BPF_X : +case BPF_ALU |BPF_SUB |BPF_K : +case BPF_ALU |BPF_SUB |BPF_X : +case BPF_ALU |BPF_OR |BPF_K : +case BPF_ALU |BPF_OR |BPF_X : +case BPF_ALU |BPF_AND |BPF_K : +case BPF_ALU |BPF_AND |BPF_X : +case BPF_ALU |BPF_XOR |BPF_K : +case BPF_ALU |BPF_XOR |BPF_X : +case BPF_ALU |BPF_MUL |BPF_K : +case BPF_ALU |BPF_MUL |BPF_X : +case BPF_ALU |BPF_LSH |BPF_X : +case BPF_ALU |BPF_RSH |BPF_X : +case BPF_ALU |BPF_ARSH |BPF_K : +case BPF_ALU |BPF_ARSH |BPF_X : +case BPF_ALU64 |BPF_ADD |BPF_K : +case BPF_ALU64 |BPF_ADD |BPF_X : +case BPF_ALU64 |BPF_SUB |BPF_K : +case BPF_ALU64 |BPF_SUB |BPF_X : +case BPF_ALU64 |BPF_OR |BPF_K : +case BPF_ALU64 |BPF_OR |BPF_X : +case BPF_ALU64 |BPF_AND |BPF_K : +case BPF_ALU64 |BPF_AND |BPF_X : +case BPF_ALU64 |BPF_XOR |BPF_K : +case BPF_ALU64 |BPF_XOR |BPF_X : +switch (BPF_SRC (code)) { +case BPF_X : +emit_a32_alu_r64 (is64, dst, src, dstk, sstk, + ctx,BPF_OP (code));break ; -case BPF_LD |BPF_W |BPF_ABS : - load_order =2 ; - goto load; -case BPF_LD |BPF_H |BPF_ABS : - load_order =1 ; - goto load; -case BPF_LD |BPF_B |BPF_ABS : - load_order =0 ; -load: -emit_mov_i (r_off, k, ctx); -load_common: - ctx->seen |=SEEN_DATA |SEEN_CALL ; - -if (load_order >0 ) { -emit (ARM_SUB_I (r_scratch, r_skb_hl, -1 << load_order), ctx); -emit (ARM_CMP_R (r_scratch, r_off), ctx); - condt =ARM_COND_GE ; - }else { -emit (ARM_CMP_R (r_skb_hl, r_off), ctx); - condt =ARM_COND_HI ; - } - -/* -* test for negative offset, only if we are -* currently scheduled to take the fast -* path. this will update the flags so that -* the slowpath instruction are ignored if the -* offset is negative. -* -* for loard_order == 0 the HI condition will -* make loads at offset 0 take the slow path too. +case BPF_K : +/* Move immediate value to the temporary register +* and then do the ALU operation on the temporary +* register as this will sign-extend the immediate +* value into temporary reg and then it would be +* safe to do the operation on it. */ -_emit (condt,ARM_CMP_I (r_off,0 ), ctx); - -_emit (condt,ARM_ADD_R (r_scratch, r_off, r_skb_data), - ctx); - -if (load_order ==0 ) -_emit (condt,ARM_LDRB_I (r_A, r_scratch,0 ), - ctx); -else if (load_order ==1 ) -emit_load_be16 (condt, r_A, r_scratch, ctx); -else if (load_order ==2 ) -emit_load_be32 (condt, r_A, r_scratch, ctx); - -_emit (condt,ARM_B (b_imm (i +1 , ctx)), ctx); - -/* the slowpath */ -emit_mov_i (ARM_R3 , (u32 )load_func[load_order], ctx); -emit (ARM_MOV_R (ARM_R0 , r_skb), ctx); -/* the offset is already in R1 */ -emit_blx_r (ARM_R3 , ctx); -/* check the result of skb_copy_bits */ -emit (ARM_CMP_I (ARM_R1 ,0 ), ctx); -emit_err_ret (ARM_COND_NE , ctx); -emit (ARM_MOV_R (r_A,ARM_R0 ), ctx); +emit_a32_mov_i64 (is64, tmp2, imm, false, ctx); +emit_a32_alu_r64 (is64, dst, tmp2, dstk, false, + ctx,BPF_OP (code));break ; -case BPF_LD |BPF_W |BPF_IND : - load_order =2 ; - goto load_ind; -case BPF_LD |BPF_H |BPF_IND : - load_order =1 ; - goto load_ind; -case BPF_LD |BPF_B |BPF_IND : - load_order =0 ; -load_ind: -update_on_xread (ctx); -OP_IMM3 (ARM_ADD , r_off, r_X, k, ctx); - goto load_common; -case BPF_LDX |BPF_IMM : - ctx->seen |=SEEN_X ; -emit_mov_i (r_X, k, ctx); + } +break ; +/* dst = dst / src(imm) */ +/* dst = dst % src(imm) */ +case BPF_ALU |BPF_DIV |BPF_K : +case BPF_ALU |BPF_DIV |BPF_X : +case BPF_ALU |BPF_MOD |BPF_K : +case BPF_ALU |BPF_MOD |BPF_X : + rt = src_lo; + rd = dstk ? tmp2[1 ] : dst_lo; +if (dstk) +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +switch (BPF_SRC (code)) { +case BPF_X : + rt = sstk ? tmp2[0 ] : rt; +if (sstk) +emit (ARM_LDR_I (rt,ARM_SP ,STACK_VAR (src_lo)), + ctx);break ; -case BPF_LDX |BPF_W |BPF_LEN : - ctx->seen |=SEEN_X |SEEN_SKB ; -emit (ARM_LDR_I (r_X, r_skb, - offsetof(struct sk_buff , len)), ctx); +case BPF_K : + rt = tmp2[0 ]; +emit_a32_mov_i (rt, imm, false, ctx);break ; -case BPF_LDX |BPF_MEM : - ctx->seen |=SEEN_X |SEEN_MEM_WORD (k); -emit (ARM_LDR_I (r_X,ARM_SP ,SCRATCH_OFF (k)), ctx); + } +emit_udivmod (rd, rd, rt, ctx,BPF_OP (code)); +if (dstk) +emit (ARM_STR_I (rd,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit_a32_mov_i (dst_hi,0 , dstk, ctx); +break ; +case BPF_ALU64 |BPF_DIV |BPF_K : +case BPF_ALU64 |BPF_DIV |BPF_X : +case BPF_ALU64 |BPF_MOD |BPF_K : +case BPF_ALU64 |BPF_MOD |BPF_X : + goto notyet; +/* dst = dst >> imm */ +/* dst = dst << imm */ +case BPF_ALU |BPF_RSH |BPF_K : +case BPF_ALU |BPF_LSH |BPF_K : +if (unlikely (imm >31 )) +return -EINVAL ; +if (imm) +emit_a32_alu_i (dst_lo, imm, dstk, ctx,BPF_OP (code)); +emit_a32_mov_i (dst_hi,0 , dstk, ctx); +break ; +/* dst = dst << imm */ +case BPF_ALU64 |BPF_LSH |BPF_K : +if (unlikely (imm >63 )) +return -EINVAL ; +emit_a32_lsh_i64 (dst, dstk, imm, ctx); +break ; +/* dst = dst >> imm */ +case BPF_ALU64 |BPF_RSH |BPF_K : +if (unlikely (imm >63 )) +return -EINVAL ; +emit_a32_lsr_i64 (dst, dstk, imm, ctx); +break ; +/* dst = dst << src */ +case BPF_ALU64 |BPF_LSH |BPF_X : +emit_a32_lsh_r64 (dst, src, dstk, sstk, ctx); +break ; +/* dst = dst >> src */ +case BPF_ALU64 |BPF_RSH |BPF_X : +emit_a32_lsr_r64 (dst, src, dstk, sstk, ctx); +break ; +/* dst = dst >> src (signed) */ +case BPF_ALU64 |BPF_ARSH |BPF_X : +emit_a32_arsh_r64 (dst, src, dstk, sstk, ctx); +break ; +/* dst = dst >> imm (signed) */ +case BPF_ALU64 |BPF_ARSH |BPF_K : +if (unlikely (imm >63 )) +return -EINVAL ; +emit_a32_arsh_i64 (dst, dstk, imm, ctx); +break ; +/* dst = ~dst */ +case BPF_ALU |BPF_NEG : +emit_a32_alu_i (dst_lo,0 , dstk, ctx,BPF_OP (code)); +emit_a32_mov_i (dst_hi,0 , dstk, ctx); +break ; +/* dst = ~dst (64 bit) */ +case BPF_ALU64 |BPF_NEG : +emit_a32_neg64 (dst, dstk, ctx); +break ; +/* dst = dst * src/imm */ +case BPF_ALU64 |BPF_MUL |BPF_X : +case BPF_ALU64 |BPF_MUL |BPF_K : +switch (BPF_SRC (code)) { +case BPF_X : +emit_a32_mul_r64 (dst, src, dstk, sstk, ctx);break ; -case BPF_LDX |BPF_B |BPF_MSH : -/* x = ((*(frame + k)) & 0xf) << 2; */ - ctx->seen |=SEEN_X |SEEN_DATA |SEEN_CALL ; -/* the interpreter should deal with the negative K */ -if ((int )k <0 ) -return -1 ; -/* offset in r1: we might have to take the slow path */ -emit_mov_i (r_off, k, ctx); -emit (ARM_CMP_R (r_skb_hl, r_off), ctx); - -/* load in r0: common with the slowpath */ -_emit (ARM_COND_HI ,ARM_LDRB_R (ARM_R0 , r_skb_data, -ARM_R1 ), ctx); -/* -* emit_mov_i() might generate one or two instructions, -* the same holds for emit_blx_r() +case BPF_K : +/* Move immediate value to the temporary register +* and then do the multiplication on it as this +* will sign-extend the immediate value into temp +* reg then it would be safe to do the operation +* on it. */ -_emit (ARM_COND_HI ,ARM_B (b_imm (i +1 , ctx) -2 ), ctx); - -emit (ARM_MOV_R (ARM_R0 , r_skb), ctx); -/* r_off is r1 */ -emit_mov_i (ARM_R3 , (u32 )jit_get_skb_b, ctx); -emit_blx_r (ARM_R3 , ctx); -/* check the return value of skb_copy_bits */ -emit (ARM_CMP_I (ARM_R1 ,0 ), ctx); -emit_err_ret (ARM_COND_NE , ctx); - -emit (ARM_AND_I (r_X,ARM_R0 ,0x00f ), ctx); -emit (ARM_LSL_I (r_X, r_X,2 ), ctx); -break ; -case BPF_ST : - ctx->seen |=SEEN_MEM_WORD (k); -emit (ARM_STR_I (r_A,ARM_SP ,SCRATCH_OFF (k)), ctx); -break ; -case BPF_STX : -update_on_xread (ctx); - ctx->seen |=SEEN_MEM_WORD (k); -emit (ARM_STR_I (r_X,ARM_SP ,SCRATCH_OFF (k)), ctx); -break ; -case BPF_ALU |BPF_ADD |BPF_K : -/* A += K */ -OP_IMM3 (ARM_ADD , r_A, r_A, k, ctx); -break ; -case BPF_ALU |BPF_ADD |BPF_X : -update_on_xread (ctx); -emit (ARM_ADD_R (r_A, r_A, r_X), ctx); -break ; -case BPF_ALU |BPF_SUB |BPF_K : -/* A -= K */ -OP_IMM3 (ARM_SUB , r_A, r_A, k, ctx); -break ; -case BPF_ALU |BPF_SUB |BPF_X : -update_on_xread (ctx); -emit (ARM_SUB_R (r_A, r_A, r_X), ctx); -break ; -case BPF_ALU |BPF_MUL |BPF_K : -/* A *= K */ -emit_mov_i (r_scratch, k, ctx); -emit (ARM_MUL (r_A, r_A, r_scratch), ctx); -break ; -case BPF_ALU |BPF_MUL |BPF_X : -update_on_xread (ctx); -emit (ARM_MUL (r_A, r_A, r_X), ctx); -break ; -case BPF_ALU |BPF_DIV |BPF_K : -if (k ==1 ) -break ; -emit_mov_i (r_scratch, k, ctx); -emit_udivmod (r_A, r_A, r_scratch, ctx,BPF_DIV ); +emit_a32_mov_i64 (is64, tmp2, imm, false, ctx); +emit_a32_mul_r64 (dst, tmp2, dstk, false, ctx);break ; -case BPF_ALU |BPF_DIV |BPF_X : -update_on_xread (ctx); -emit (ARM_CMP_I (r_X,0 ), ctx); -emit_err_ret (ARM_COND_EQ , ctx); -emit_udivmod (r_A, r_A, r_X, ctx,BPF_DIV ); -break ; -case BPF_ALU |BPF_MOD |BPF_K : -if (k ==1 ) { -emit_mov_i (r_A,0 , ctx); -break ; - } -emit_mov_i (r_scratch, k, ctx); -emit_udivmod (r_A, r_A, r_scratch, ctx,BPF_MOD ); -break ; -case BPF_ALU |BPF_MOD |BPF_X : -update_on_xread (ctx); -emit (ARM_CMP_I (r_X,0 ), ctx); -emit_err_ret (ARM_COND_EQ , ctx); -emit_udivmod (r_A, r_A, r_X, ctx,BPF_MOD ); -break ; -case BPF_ALU |BPF_OR |BPF_K : -/* A |= K */ -OP_IMM3 (ARM_ORR , r_A, r_A, k, ctx); + } +break ; +/* dst = htole(dst) */ +/* dst = htobe(dst) */ +case BPF_ALU |BPF_END |BPF_FROM_LE : +case BPF_ALU |BPF_END |BPF_FROM_BE : + rd = dstk ? tmp[0 ] : dst_hi; + rt = dstk ? tmp[1 ] : dst_lo; +if (dstk) { +emit (ARM_LDR_I (rt,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } +if (BPF_SRC (code) ==BPF_FROM_LE ) + goto emit_bswap_uxt; +switch (imm) { +case 16 : +emit_rev16 (rt, rt, ctx); + goto emit_bswap_uxt; +case 32 : +emit_rev32 (rt, rt, ctx); + goto emit_bswap_uxt; +case 64 : +/* Because of the usage of ARM_LR */ + ctx->seen |=SEEN_CALL ; +emit_rev32 (ARM_LR , rt, ctx); +emit_rev32 (rt, rd, ctx); +emit (ARM_MOV_R (rd,ARM_LR ), ctx);break ; -case BPF_ALU |BPF_OR |BPF_X : -update_on_xread (ctx); -emit (ARM_ORR_R (r_A, r_A, r_X), ctx); + } + goto exit; +emit_bswap_uxt: +switch (imm) { +case 16 : +/* zero-extend 16 bits into 64 bits */ +#if __LINUX_ARM_ARCH__ <6 +emit_a32_mov_i (tmp2[1 ],0xffff , false, ctx); +emit (ARM_AND_R (rt, rt, tmp2[1 ]), ctx); +#else /* ARMv6+ */ +emit (ARM_UXTH (rt, rt), ctx); +#endif +emit (ARM_EOR_R (rd, rd, rd), ctx);break ; -case BPF_ALU |BPF_XOR |BPF_K : -/* A ^= K; */ -OP_IMM3 (ARM_EOR , r_A, r_A, k, ctx); +case 32 : +/* zero-extend 32 bits into 64 bits */ +emit (ARM_EOR_R (rd, rd, rd), ctx);break ; -case BPF_ANC |SKF_AD_ALU_XOR_X : -case BPF_ALU |BPF_XOR |BPF_X : -/* A ^= X */ -update_on_xread (ctx); -emit (ARM_EOR_R (r_A, r_A, r_X), ctx); +case 64 : +/* nop */ break ; -case BPF_ALU |BPF_AND |BPF_K : -/* A &= K */ -OP_IMM3 (ARM_AND , r_A, r_A, k, ctx); + } +exit: +if (dstk) { +emit (ARM_STR_I (rt,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_STR_I (rd,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } +break ; +/* dst = imm64 */ +case BPF_LD |BPF_IMM |BPF_DW : + { +const struct bpf_insn insn1 = insn[1 ]; +u32 hi, lo = imm; + +if (insn1.code !=0 || insn1.src_reg !=0 || + insn1.dst_reg !=0 || insn1.off !=0 ) { +/* Note: verifier in BPF core must catch invalid +* instruction. +*/ +pr_err_once ("Invalid BPF_LD_IMM64 instruction\n" ); +return -EINVAL ; + } + hi = insn1.imm; +emit_a32_mov_i (dst_lo, lo, dstk, ctx); +emit_a32_mov_i (dst_hi, hi, dstk, ctx); + +return 1 ; + } +/* LDX: dst = *(size *)(src + off) */ +case BPF_LDX |BPF_MEM |BPF_W : +case BPF_LDX |BPF_MEM |BPF_H : +case BPF_LDX |BPF_MEM |BPF_B : +case BPF_LDX |BPF_MEM |BPF_DW : + rn = sstk ? tmp2[1 ] : src_lo; +if (sstk) +emit (ARM_LDR_I (rn,ARM_SP ,STACK_VAR (src_lo)), ctx); +switch (BPF_SIZE (code)) { +case BPF_W : +/* Load a Word */ +case BPF_H : +/* Load a Half-Word */ +case BPF_B : +/* Load a Byte */ +emit_ldx_r (dst_lo, rn, dstk, off, ctx,BPF_SIZE (code)); +emit_a32_mov_i (dst_hi,0 , dstk, ctx);break ; -case BPF_ALU |BPF_AND |BPF_X : -update_on_xread (ctx); -emit (ARM_AND_R (r_A, r_A, r_X), ctx); +case BPF_DW : +/* Load a double word */ +emit_ldx_r (dst_lo, rn, dstk, off, ctx,BPF_W ); +emit_ldx_r (dst_hi, rn, dstk, off+4 , ctx,BPF_W );break ; -case BPF_ALU |BPF_LSH |BPF_K : -if (unlikely (k >31 )) -return -1 ; -emit (ARM_LSL_I (r_A, r_A, k), ctx); + } +break ; +/* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + imm)) */ +case BPF_LD |BPF_ABS |BPF_W : +case BPF_LD |BPF_ABS |BPF_H : +case BPF_LD |BPF_ABS |BPF_B : +/* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + src + imm)) */ +case BPF_LD |BPF_IND |BPF_W : +case BPF_LD |BPF_IND |BPF_H : +case BPF_LD |BPF_IND |BPF_B : + { +const u8 r4 = bpf2a32[BPF_REG_6 ][1 ];/* r4 = ptr to sk_buff */ +const u8 r0 = bpf2a32[BPF_REG_0 ][1 ];/*r0: struct sk_buff *skb*/ +/* rtn value */ +const u8 r1 = bpf2a32[BPF_REG_0 ][0 ];/* r1: int k */ +const u8 r2 = bpf2a32[BPF_REG_1 ][1 ];/* r2: unsigned int size */ +const u8 r3 = bpf2a32[BPF_REG_1 ][0 ];/* r3: void *buffer */ +const u8 r6 = bpf2a32[TMP_REG_1 ][1 ];/* r6: void *(*func)(..) */ +int size; + +/* Setting up first argument */ +emit (ARM_MOV_R (r0, r4), ctx); + +/* Setting up second argument */ +emit_a32_mov_i (r1, imm, false, ctx); +if (BPF_MODE (code) ==BPF_IND ) +emit_a32_alu_r (r1, src_lo, false, sstk, ctx, + false, false,BPF_ADD ); + +/* Setting up third argument */ +switch (BPF_SIZE (code)) { +case BPF_W : + size =4 ;break ; -case BPF_ALU |BPF_LSH |BPF_X : -update_on_xread (ctx); -emit (ARM_LSL_R (r_A, r_A, r_X), ctx); +case BPF_H : + size =2 ;break ; -case BPF_ALU |BPF_RSH |BPF_K : -if (unlikely (k >31 )) -return -1 ; -if (k) -emit (ARM_LSR_I (r_A, r_A, k), ctx); +case BPF_B : + size =1 ;break ; -case BPF_ALU |BPF_RSH |BPF_X : -update_on_xread (ctx); -emit (ARM_LSR_R (r_A, r_A, r_X), ctx); +default : +return -EINVAL ; + } +emit_a32_mov_i (r2, size, false, ctx); + +/* Setting up fourth argument */ +emit (ARM_ADD_I (r3,ARM_SP ,imm8m (SKB_BUFFER )), ctx); + +/* Setting up function pointer to call */ +emit_a32_mov_i (r6, (unsigned int )bpf_load_pointer, false, ctx); +emit_blx_r (r6, ctx); + +emit (ARM_EOR_R (r1, r1, r1), ctx); +/* Check if return address is NULL or not. +* if NULL then jump to epilogue +* else continue to load the value from retn address +*/ +emit (ARM_CMP_I (r0,0 ), ctx); + jmp_offset =epilogue_offset (ctx); +check_imm24 (jmp_offset); +_emit (ARM_COND_EQ ,ARM_B (jmp_offset), ctx); + +/* Load value from the address */ +switch (BPF_SIZE (code)) { +case BPF_W : +emit (ARM_LDR_I (r0, r0,0 ), ctx); +emit_rev32 (r0, r0, ctx);break ; -case BPF_ALU |BPF_NEG : -/* A = -A */ -emit (ARM_RSB_I (r_A, r_A,0 ), ctx); +case BPF_H : +emit (ARM_LDRH_I (r0, r0,0 ), ctx); +emit_rev16 (r0, r0, ctx);break ; -case BPF_JMP |BPF_JA : -/* pc += K */ -emit (ARM_B (b_imm (i + k +1 , ctx)), ctx); +case BPF_B : +emit (ARM_LDRB_I (r0, r0,0 ), ctx); +/* No need to reverse */ break ; -case BPF_JMP |BPF_JEQ |BPF_K : -/* pc += (A == K) ? pc->jt : pc->jf */ - condt =ARM_COND_EQ ; - goto cmp_imm; -case BPF_JMP |BPF_JGT |BPF_K : -/* pc += (A > K) ? pc->jt : pc->jf */ - condt =ARM_COND_HI ; - goto cmp_imm; -case BPF_JMP |BPF_JGE |BPF_K : -/* pc += (A >= K) ? pc->jt : pc->jf */ - condt =ARM_COND_HS ; -cmp_imm: - imm12 =imm8m (k); -if (imm12 <0 ) { -emit_mov_i_no8m (r_scratch, k, ctx); -emit (ARM_CMP_R (r_A, r_scratch), ctx); - }else { -emit (ARM_CMP_I (r_A, imm12), ctx); - } -cond_jump: -if (inst->jt) -_emit (condt,ARM_B (b_imm (i + inst->jt +1 , - ctx)), ctx); -if (inst->jf) -_emit (condt ^1 ,ARM_B (b_imm (i + inst->jf +1 , - ctx)), ctx); + } +break ; + } +/* ST: *(size *)(dst + off) = imm */ +case BPF_ST |BPF_MEM |BPF_W : +case BPF_ST |BPF_MEM |BPF_H : +case BPF_ST |BPF_MEM |BPF_B : +case BPF_ST |BPF_MEM |BPF_DW : +switch (BPF_SIZE (code)) { +case BPF_DW : +/* Sign-extend immediate value into temp reg */ +emit_a32_mov_i64 (true, tmp2, imm, false, ctx); +emit_str_r (dst_lo, tmp2[1 ], dstk, off, ctx,BPF_W ); +emit_str_r (dst_lo, tmp2[0 ], dstk, off+4 , ctx,BPF_W );break ; -case BPF_JMP |BPF_JEQ |BPF_X : -/* pc += (A == X) ? pc->jt : pc->jf */ - condt =ARM_COND_EQ ; - goto cmp_x; -case BPF_JMP |BPF_JGT |BPF_X : -/* pc += (A > X) ? pc->jt : pc->jf */ - condt =ARM_COND_HI ; - goto cmp_x; -case BPF_JMP |BPF_JGE |BPF_X : -/* pc += (A >= X) ? pc->jt : pc->jf */ - condt =ARM_COND_CS ; -cmp_x: -update_on_xread (ctx); -emit (ARM_CMP_R (r_A, r_X), ctx); - goto cond_jump; -case BPF_JMP |BPF_JSET |BPF_K : -/* pc += (A & K) ? pc->jt : pc->jf */ - condt =ARM_COND_NE ; -/* not set iff all zeroes iff Z==1 iff EQ */ - - imm12 =imm8m (k); -if (imm12 <0 ) { -emit_mov_i_no8m (r_scratch, k, ctx); -emit (ARM_TST_R (r_A, r_scratch), ctx); - }else { -emit (ARM_TST_I (r_A, imm12), ctx); - } - goto cond_jump; -case BPF_JMP |BPF_JSET |BPF_X : -/* pc += (A & X) ? pc->jt : pc->jf */ -update_on_xread (ctx); - condt =ARM_COND_NE ; -emit (ARM_TST_R (r_A, r_X), ctx); - goto cond_jump; -case BPF_RET |BPF_A : -emit (ARM_MOV_R (ARM_R0 , r_A), ctx); - goto b_epilogue; -case BPF_RET |BPF_K : -if ((k ==0 ) && (ctx->ret0_fp_idx <0 )) - ctx->ret0_fp_idx = i; -emit_mov_i (ARM_R0 , k, ctx); -b_epilogue: -if (i != ctx->skf->len -1 ) -emit (ARM_B (b_imm (prog->len, ctx)), ctx); +case BPF_W : +case BPF_H : +case BPF_B : +emit_a32_mov_i (tmp2[1 ], imm, false, ctx); +emit_str_r (dst_lo, tmp2[1 ], dstk, off, ctx, +BPF_SIZE (code));break ; -case BPF_MISC |BPF_TAX : -/* X = A */ - ctx->seen |=SEEN_X ; -emit (ARM_MOV_R (r_X, r_A), ctx); + } +break ; +/* STX XADD: lock *(u32 *)(dst + off) += src */ +case BPF_STX |BPF_XADD |BPF_W : +/* STX XADD: lock *(u64 *)(dst + off) += src */ +case BPF_STX |BPF_XADD |BPF_DW : + goto notyet; +/* STX: *(size *)(dst + off) = src */ +case BPF_STX |BPF_MEM |BPF_W : +case BPF_STX |BPF_MEM |BPF_H : +case BPF_STX |BPF_MEM |BPF_B : +case BPF_STX |BPF_MEM |BPF_DW : + { +u8 sz =BPF_SIZE (code); + + rn = sstk ? tmp2[1 ] : src_lo; + rm = sstk ? tmp2[0 ] : src_hi; +if (!sstk) + goto do_store; +switch (BPF_SIZE (code)) { +case BPF_W : +emit (ARM_LDR_I (rn,ARM_SP ,STACK_VAR (src_lo)), ctx); + goto empty_hi; +case BPF_H : +emit (ARM_LDRH_I (rn,ARM_SP ,STACK_VAR (src_lo)), ctx); + goto empty_hi; +case BPF_B : +emit (ARM_LDRB_I (rn,ARM_SP ,STACK_VAR (src_lo)), ctx); + goto empty_hi; +empty_hi: +emit (ARM_EOR_R (rm, rm, rm), ctx); +case BPF_DW : +emit (ARM_LDR_I (rn,ARM_SP ,STACK_VAR (src_lo)), ctx); +emit (ARM_LDR_I (rm,ARM_SP ,STACK_VAR (src_hi)), ctx); + sz =BPF_W ;break ; -case BPF_MISC |BPF_TXA : -/* A = X */ -update_on_xread (ctx); -emit (ARM_MOV_R (r_A, r_X), ctx); + } + +do_store: +/* Clear higher word except for BPF_DW */ +if (BPF_SIZE (code) !=BPF_DW ) +emit (ARM_EOR_R (rm, rm, rm), ctx); + +/* Store the value */ +emit_str_r (dst_lo, rn, dstk, off, ctx, sz); +emit_str_r (dst_lo, rm, dstk, off+4 , ctx,BPF_W ); +break ; + } +/* PC += off if dst == src */ +/* PC += off if dst > src */ +/* PC += off if dst >= src */ +/* PC += off if dst != src */ +/* PC += off if dst > src (signed) */ +/* PC += off if dst >= src (signed) */ +/* PC += off if dst & src */ +case BPF_JMP |BPF_JEQ |BPF_X : +case BPF_JMP |BPF_JGT |BPF_X : +case BPF_JMP |BPF_JGE |BPF_X : +case BPF_JMP |BPF_JNE |BPF_X : +case BPF_JMP |BPF_JSGT |BPF_X : +case BPF_JMP |BPF_JSGE |BPF_X : +case BPF_JMP |BPF_JSET |BPF_X : +/* Setup source registers */ + rm = sstk ? tmp2[0 ] : src_hi; + rn = sstk ? tmp2[1 ] : src_lo; +if (sstk) { +emit (ARM_LDR_I (rn,ARM_SP ,STACK_VAR (src_lo)), ctx); +emit (ARM_LDR_I (rm,ARM_SP ,STACK_VAR (src_hi)), ctx); + } + goto go_jmp; +/* PC += off if dst == imm */ +/* PC += off if dst > imm */ +/* PC += off if dst >= imm */ +/* PC += off if dst != imm */ +/* PC += off if dst > imm (signed) */ +/* PC += off if dst >= imm (signed) */ +/* PC += off if dst & imm */ +case BPF_JMP |BPF_JEQ |BPF_K : +case BPF_JMP |BPF_JGT |BPF_K : +case BPF_JMP |BPF_JGE |BPF_K : +case BPF_JMP |BPF_JNE |BPF_K : +case BPF_JMP |BPF_JSGT |BPF_K : +case BPF_JMP |BPF_JSGE |BPF_K : +case BPF_JMP |BPF_JSET |BPF_K : +if (off ==0 )break ; -case BPF_ANC |SKF_AD_PROTOCOL : -/* A = ntohs(skb->protocol) */ - ctx->seen |=SEEN_SKB ; -BUILD_BUG_ON (FIELD_SIZEOF (struct sk_buff , - protocol) != 2); - off = offsetof(struct sk_buff , protocol); -emit (ARM_LDRH_I (r_scratch, r_skb, off), ctx); -emit_swap16 (r_A, r_scratch, ctx); + rm = tmp2[0 ]; + rn = tmp2[1 ]; +/* Sign-extend immediate value */ +emit_a32_mov_i64 (true, tmp2, imm, false, ctx); +go_jmp: +/* Setup destination register */ + rd = dstk ? tmp[0 ] : dst_hi; + rt = dstk ? tmp[1 ] : dst_lo; +if (dstk) { +emit (ARM_LDR_I (rt,ARM_SP ,STACK_VAR (dst_lo)), ctx); +emit (ARM_LDR_I (rd,ARM_SP ,STACK_VAR (dst_hi)), ctx); + } + +/* Check for the condition */ +emit_ar_r (rd, rt, rm, rn, ctx,BPF_OP (code)); + +/* Setup JUMP instruction */ + jmp_offset =bpf2a32_offset (i+off, i, ctx); +switch (BPF_OP (code)) { +case BPF_JNE : +case BPF_JSET : +_emit (ARM_COND_NE ,ARM_B (jmp_offset), ctx);break ; -case BPF_ANC |SKF_AD_CPU : -/* r_scratch = current_thread_info() */ -OP_IMM3 (ARM_BIC , r_scratch,ARM_SP ,THREAD_SIZE -1 , ctx); -/* A = current_thread_info()->cpu */ - BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info, cpu) != 4); - off = offsetof(struct thread_info , cpu); -emit (ARM_LDR_I (r_A, r_scratch, off), ctx); +case BPF_JEQ : +_emit (ARM_COND_EQ ,ARM_B (jmp_offset), ctx);break ; -case BPF_ANC |SKF_AD_IFINDEX : -case BPF_ANC |SKF_AD_HATYPE : -/* A = skb->dev->ifindex */ -/* A = skb->dev->type */ - ctx->seen |=SEEN_SKB ; - off = offsetof(struct sk_buff , dev); -emit (ARM_LDR_I (r_scratch, r_skb, off), ctx); - -emit (ARM_CMP_I (r_scratch,0 ), ctx); -emit_err_ret (ARM_COND_EQ , ctx); - -BUILD_BUG_ON (FIELD_SIZEOF (struct net_device , - ifindex) != 4); -BUILD_BUG_ON (FIELD_SIZEOF (struct net_device , - type) != 2); - -if (code == (BPF_ANC |SKF_AD_IFINDEX )) { - off = offsetof(struct net_device , ifindex); -emit (ARM_LDR_I (r_A, r_scratch, off), ctx); - }else { -/* -* offset of field "type" in "struct -* net_device" is above what can be -* used in the ldrh rd, [rn, #imm] -* instruction, so load the offset in -* a register and use ldrh rd, [rn, rm] -*/ - off = offsetof(struct net_device , type); -emit_mov_i (ARM_R3 , off, ctx); -emit (ARM_LDRH_R (r_A, r_scratch,ARM_R3 ), ctx); - } +case BPF_JGT : +_emit (ARM_COND_HI ,ARM_B (jmp_offset), ctx);break ; -case BPF_ANC |SKF_AD_MARK : - ctx->seen |=SEEN_SKB ; - BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4); - off = offsetof(struct sk_buff , mark); -emit (ARM_LDR_I (r_A, r_skb, off), ctx); +case BPF_JGE : +_emit (ARM_COND_CS ,ARM_B (jmp_offset), ctx);break ; -case BPF_ANC |SKF_AD_RXHASH : - ctx->seen |=SEEN_SKB ; - BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4); - off = offsetof(struct sk_buff , hash); -emit (ARM_LDR_I (r_A, r_skb, off), ctx); +case BPF_JSGT : +_emit (ARM_COND_LT ,ARM_B (jmp_offset), ctx);break ; -case BPF_ANC |SKF_AD_VLAN_TAG : -case BPF_ANC |SKF_AD_VLAN_TAG_PRESENT : - ctx->seen |=SEEN_SKB ; - BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2); - off = offsetof(struct sk_buff , vlan_tci); -emit (ARM_LDRH_I (r_A, r_skb, off), ctx); -if (code == (BPF_ANC |SKF_AD_VLAN_TAG )) -OP_IMM3 (ARM_AND , r_A, r_A, ~VLAN_TAG_PRESENT , ctx); -else { -OP_IMM3 (ARM_LSR , r_A, r_A,12 , ctx); -OP_IMM3 (ARM_AND , r_A, r_A,0x1 , ctx); - } +case BPF_JSGE : +_emit (ARM_COND_GE ,ARM_B (jmp_offset), ctx);break ; -case BPF_ANC |SKF_AD_PKTTYPE : - ctx->seen |=SEEN_SKB ; -BUILD_BUG_ON (FIELD_SIZEOF (struct sk_buff , - __pkt_type_offset[0]) != 1); - off =PKT_TYPE_OFFSET (); -emit (ARM_LDRB_I (r_A, r_skb, off), ctx); -emit (ARM_AND_I (r_A, r_A,PKT_TYPE_MAX ), ctx); -#ifdef __BIG_ENDIAN_BITFIELD -emit (ARM_LSR_I (r_A, r_A,5 ), ctx); -#endif + } +break ; +/* JMP OFF */ +case BPF_JMP |BPF_JA : + { +if (off ==0 )break ; -case BPF_ANC |SKF_AD_QUEUE : - ctx->seen |=SEEN_SKB ; -BUILD_BUG_ON (FIELD_SIZEOF (struct sk_buff , - queue_mapping) != 2); -BUILD_BUG_ON (offsetof(struct sk_buff , - queue_mapping) >0xff ); - off = offsetof(struct sk_buff , queue_mapping); -emit (ARM_LDRH_I (r_A, r_skb, off), ctx); + jmp_offset =bpf2a32_offset (i+off, i, ctx); +check_imm24 (jmp_offset); +emit (ARM_B (jmp_offset), ctx); +break ; + } +/* tail call */ +case BPF_JMP |BPF_CALL |BPF_X : +if (emit_bpf_tail_call (ctx)) +return -EFAULT ; +break ; +/* function call */ +case BPF_JMP |BPF_CALL : + goto notyet; +/* function return */ +case BPF_JMP |BPF_EXIT : +/* Optimization: when last instruction is EXIT +* simply fallthrough to epilogue. +*/ +if (i == ctx->prog->len -1 )break ; -case BPF_ANC |SKF_AD_PAY_OFFSET : - ctx->seen |=SEEN_SKB |SEEN_CALL ; + jmp_offset =epilogue_offset (ctx); +check_imm24 (jmp_offset); +emit (ARM_B (jmp_offset), ctx); +break ; +notyet: +pr_info_once ("*** NOT YET: opcode %02x ***\n" , code); +return -EFAULT ; +default : +pr_err_once ("unknown opcode %02x\n" , code); +return -EINVAL ; + } -emit (ARM_MOV_R (ARM_R0 , r_skb), ctx); -emit_mov_i (ARM_R3 , (unsigned int )skb_get_poff, ctx); -emit_blx_r (ARM_R3 , ctx); -emit (ARM_MOV_R (r_A,ARM_R0 ), ctx); -break ; -case BPF_LDX |BPF_W |BPF_ABS : -/* -* load a 32bit word from struct seccomp_data. -* seccomp_check_filter() will already have checked -* that k is 32bit aligned and lies within the -* struct seccomp_data. -*/ - ctx->seen |=SEEN_SKB ; -emit (ARM_LDR_I (r_A, r_skb, k), ctx); -break ; -default : -return -1 ; +if (ctx->flags &FLAG_IMM_OVERFLOW ) +/* +* this instruction generated an overflow when +* trying to access the literal pool, so +* delegate this filter to the kernel interpreter. +*/ +return -1 ; +return 0 ; +} + +static int build_body (struct jit_ctx *ctx) +{ +const struct bpf_prog *prog = ctx->prog; +unsigned int i; + +for (i =0 ; i < prog->len; i++) { +const struct bpf_insn *insn = &(prog->insnsi[i]); +int ret; + +emit (ARM_MOV_R (ARM_IP ,ARM_PC ), ctx); + ret =build_insn (insn, ctx); + +/* It's used with loading the 64 bit immediate value. */ +if (ret >0 ) { + i++; +if (ctx->target ==NULL ) + ctx->offsets[i] = ctx->idx; +continue ; } -if (ctx->flags &FLAG_IMM_OVERFLOW ) -/* -* this instruction generated an overflow when -* trying to access the literal pool, so -* delegate this filter to the kernel interpreter. -*/ -return -1 ; +if (ctx->target ==NULL ) + ctx->offsets[i] = ctx->idx; + +/* If unsuccesfull, return with error code */ +if (ret) +return ret; } +pr_info ("ARM version : %d\n" ,__LINUX_ARM_ARCH__); +return 0 ; +} -/* compute offsets only during the first pass */ -if (ctx->target ==NULL ) - ctx->offsets[i] = ctx->idx *4 ; +static int validate_code (struct jit_ctx *ctx) +{ +int i; + +for (i =0 ; i < ctx->idx; i++) { +u32 a32_insn =le32_to_cpu (ctx->target[i]); + +if (a32_insn ==ARM_INST_UDF ) +return -1 ; + }return 0 ; } +void bpf_jit_compile (struct bpf_prog *prog) +{ +/* Nothing to do here. We support Internal BPF. */ +} -void bpf_jit_compile (struct bpf_prog *fp) +struct bpf_prog *bpf_int_jit_compile (struct bpf_prog *prog) { +#ifdef CONFIG_CPU_LITTLE_ENDIAN +struct bpf_prog *tmp, *orig_prog = prog;struct bpf_binary_header *header; +bool tmp_blinded = false;struct jit_ctx ctx; -unsigned tmp_idx; -unsigned alloc_size; -u8 *target_ptr; +unsigned int tmp_idx; +unsigned int image_size; +u8 *image_ptr; +/* If BPF JIT was not enabled then we must fall back to +* the interpreter. +*/ if (!bpf_jit_enable) -return ; +return orig_prog; -memset (&ctx,0 ,sizeof (ctx)); - ctx.skf = fp; - ctx.ret0_fp_idx =-1 ; +/* If constant blinding was enabled and we failed during blinding +* then we must fall back to the interpreter. Otherwise, we save +* the new JITed code. +*/ + tmp =bpf_jit_blind_constants (prog); - ctx.offsets =kzalloc (4 * (ctx.skf->len +1 ),GFP_KERNEL ); -if (ctx.offsets ==NULL ) -return ; +if (IS_ERR (tmp)) +return orig_prog; +if (tmp != prog) { + tmp_blinded = true; + prog = tmp; + } + +memset (&ctx,0 ,sizeof (ctx)); + ctx.prog = prog; -/* fake pass to fill in the ctx->seen */ -if (unlikely (build_body (&ctx))) +/* Not able to allocate memory for offsets[] , then +* we must fall back to the interpreter +*/ + ctx.offsets =kcalloc (prog->len,sizeof (int ),GFP_KERNEL ); +if (ctx.offsets ==NULL ) { + prog = orig_prog; goto out; + } + +/* 1) fake pass to find in the length of the JITed code, +* to compute ctx->offsets and other context variables +* needed to compute final JITed code. +* Also, calculate random starting pointer/start of JITed code +* which is prefixed by random number of fault instructions. +* +* If the first pass fails then there is no chance of it +* being successful in the second pass, so just fall back +* to the interpreter. +*/ +if (build_body (&ctx)) { + prog = orig_prog; + goto out_off; + } tmp_idx = ctx.idx;build_prologue (&ctx); ctx.prologue_bytes = (ctx.idx - tmp_idx) *4 ; + ctx.epilogue_offset = ctx.idx; +#if __LINUX_ARM_ARCH__ <7 tmp_idx = ctx.idx;build_epilogue (&ctx);
@@ -1020,64 +1840,96 @@ void bpf_jit_compile(struct bpf_prog *fp) ctx.idx += ctx.imm_count;if (ctx.imm_count) { - ctx.imms =kzalloc (4 * ctx.imm_count,GFP_KERNEL ); -if (ctx.imms ==NULL ) - goto out; + ctx.imms =kcalloc (ctx.imm_count,sizeof (u32),GFP_KERNEL ); +if (ctx.imms ==NULL ) { + prog = orig_prog; + goto out_off; + } }#else -/* there's nothing after the epilogue on ARMv7 */ +/* there's nothing about the epilogue on ARMv7 */ build_epilogue (&ctx);#endif - alloc_size =4 * ctx.idx; - header =bpf_jit_binary_alloc (alloc_size, &target_ptr, -4 , jit_fill_hole); -if (header ==NULL ) - goto out; +/* Now we can get the actual image size of the JITed arm code. +* Currently, we are not considering the THUMB-2 instructions +* for jit, although it can decrease the size of the image. +* +* As each arm instruction is of length 32bit, we are translating +* number of JITed intructions into the size required to store these +* JITed code. +*/ + image_size =sizeof (u32) * ctx.idx; - ctx.target = (u32 *) target_ptr; +/* Now we know the size of the structure to make */ + header =bpf_jit_binary_alloc (image_size, &image_ptr, +sizeof (u32), jit_fill_hole); +/* Not able to allocate memory for the structure then +* we must fall back to the interpretation +*/ +if (header ==NULL ) { + prog = orig_prog; + goto out_imms; + } + +/* 2.) Actual pass to generate final JIT code */ + ctx.target = (u32 *) image_ptr; ctx.idx =0 ;build_prologue (&ctx); + +/* If building the body of the JITed code fails somehow, +* we fall back to the interpretation. +*/ if (build_body (&ctx) <0 ) { -#if __LINUX_ARM_ARCH__ <7 -if (ctx.imm_count) -kfree (ctx.imms); -#endif + image_ptr =NULL ;bpf_jit_binary_free (header); - goto out; + prog = orig_prog; + goto out_imms; }build_epilogue (&ctx); +/* 3.) Extra pass to validate JITed Code */ +if (validate_code (&ctx)) { + image_ptr =NULL ; +bpf_jit_binary_free (header); + prog = orig_prog; + goto out_imms; + }flush_icache_range ((u32 )header, (u32)(ctx.target + ctx.idx)); -#if __LINUX_ARM_ARCH__ <7 -if (ctx.imm_count) -kfree (ctx.imms); -#endif -if (bpf_jit_enable >1 )/* there are 2 passes here */ -bpf_jit_dump (fp->len, alloc_size,2 , ctx.target); +bpf_jit_dump (prog->len, image_size,2 , ctx.target);set_memory_ro ((unsigned long )header, header->pages); - fp->bpf_func = (void *)ctx.target; - fp->jited =1 ; -out: + prog->bpf_func = (void *)ctx.target; + prog->jited =1 ; +out_imms: +#if __LINUX_ARM_ARCH__ <7 +if (ctx.imm_count) +kfree (ctx.imms); +#endif +out_off:kfree (ctx.offsets); -return ; +out: +if (tmp_blinded) +bpf_jit_prog_release_other (prog, prog == orig_prog ? + tmp : orig_prog); +#endif /* CONFIG_CPU_LITTLE_ENDIAN */ +return prog; } -void bpf_jit_free (struct bpf_prog *fp) +void bpf_jit_free (struct bpf_prog *prog) { -unsigned long addr = (unsigned long )fp->bpf_func &PAGE_MASK ; +unsigned long addr = (unsigned long )prog->bpf_func &PAGE_MASK ;struct bpf_binary_header *header = (void *)addr; -if (!fp->jited) +if (!prog->jited) goto free_filter;set_memory_rw (addr, header->pages);bpf_jit_binary_free (header); free_filter: -bpf_prog_unlock_free (fp); +bpf_prog_unlock_free (prog); }
arch/arm/net/bpf_jit_32.h +83 -25
219 lines not on this page · raw message
Signature · 1 line
2.7.4
Hi Andrew,
Did you get the chance to test the patch?
Best,
Shubham Bansal
Quoted 111 lines · Shubham Bansal
Hi Andrew,
I am attaching the latest patch which should work on ARMv5 , ARMv6 and ARMv7.
Please test on your machines. Apologies for the last patch. This patch should work though.
Best,
Shubham BansalQuoted 99 lines · Andrew Lunn
root at qnap:~#
test_bpf: #0 TAX jited:0 241 466 241 PASStest_bpf: #1 TXA jited:1 51 117 51 PASStest_bpf: #2ADD_SUB_MUL_Kjited:0 167 PASStest_bpf: #3DIV_MOD_KXjited:0 571 PASStest_bpf: #4AND_OR_LSH_Kjited:1 54 54 PASStest_bpf: #5LD_IMM_0jited:1 45 PASStest_bpf: #6LD_INDjited:0 143 143 143 PASStest_bpf: #7LD_ABSjited:0 120 133 120 PASStest_bpf: #8LD_ABS_LLUnable to handle kernel paging request at virtual address 9e096420 pgd = daa30000 [9e096420] *pgd=00000000 Internal error: Oops: 805 [#1] PREEMPT ARM Modules linked in: test_bpf(+) [last unloaded: test_bpf] CPU: 0 PID: 1886 Comm: modprobe Not tainted 4.12.0-rc2-00275-g17a0ff4ded06-dirty #1784 Hardware name: Marvell Kirkwood (Flattened Device Tree) task: df7d4ba0 task.stack: de738000 PC is at imm_offset+0xa4/0x108 LR is at 0x8 pc : [<c0015080>] lr : [<00000008>] psr: 60000013sp :
de739c6cip :0000002cfp : 00000003
r10:de739cf8r9 :e1356000r8 :0000001c
r7 :de59c840r6 :de59c840r5 :bf2c2944r4 : 00000000 r3 :dedd3adcr2 :dedd3adcr1 :de739cf8r0 :ffe00000
Flags: nZCv IRQs on FIQs on ModeSVC_32ISA ARM Segment none
Control:0005397fTable:1aa30000DAC: 00000051 Process modprobe (pid: 1886, stack limit =0xde738190)Stack: (0xde739c6c to 0xde73a000)9c60: 00000001 00000030 00000003
c05c5d5ec001a560
9c80:de739cf400000000e135603800000000c05c5d34ffe00000c05c5d4200000000 9ca0: 000000000000001ae1356000000000005e3b2a6400000f0400000000e13560009cc0:e1356000c078105c00000000000000f8bf2c200000000000bf1661c0c001c64c9ce0:de57f0c800000008 00000002de739d5400000eecbf2c2944e13560000000001c9d00:0000002c00000038 00000001 00000000de59c9a0bf2c294400040008de59c8409d20:e1356000de739d50de57f0c000000005de59c9a00000ffff00000001c008fb989d40:e1356000e1356000de57f0c0c04a838400000000 00000008e1356000de739dd89d60: 00000000e135600000000028de739dd4bf16514000000000bf1661c0c04a84d09d80: 00000005e1354000bf1651c800000000e1354000bf2af218dedcfd20ffffe0009da0:df45ef80bf1651c400000008 00000008 00000003 00000008 00000003 00000000 9dc0:bf1650a5bf16514479f5179d0000004e00000000e135400000000005bf1651c89de0: 00000000ffffe000bf2af00000000000de57f9e400000001bf2acee0c007aa889e00: 00000124c0009738ffffffff 00000000 00000001dfbe78b0c07c25ac0080007f9e20:dfbe78a400000000 000000000000001f00000000dffc3d60de738000df401f009e40:de56b080c007d6e000016acfc007aa88bf2acee000000001de59c28000000001 9e60:bf2acee0c00a154800000001de57f9c0de739f5400000001de57f9c0c007d6e89e80:bf2aceec00007fffbf2acee0c007afd4de739f4800000000bf2acf2800000000 9ea0: 00000000bf2ad074e1344928c05c76a8bf2aceec00000000de739f44c00f6fa89ec0:001879a000000000c00f768400000000 00000000 00000000 00000000 00000000 9ee0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 9f00: 00000000 000000007fffffff00000000 000000037f59f5100000017bc000a5449f20:de73800000000000 00000000c007de587fffffff00000000 00000003 00000000 9f40: 00000000e11bd000001879a000000000 00000874e11bd000001879a0e13441d09f60:e1343ff0e133796000154550001547c000000000 00000000 00000000000014ec9f80:0000002f000000300000001b00000000 00000015 00000000ca81c3007f5b28109fa0: 00000000c000a3a0ca81c3007f5b2810000000037f59f510000000007f59fd009fc0:ca81c3007f5b2810000000000000017b7f59f510000000007f5b21d000000000 9fe0:bee273a0bee273907f593accb6ed113060000010 00000003 00000000 00000000 [<c0015080>] (imm_offset) from [<c001a560>] (build_body+0x5164/0x70ec) [<c001a560>] (build_body) from [<c001c64c>] (bpf_int_jit_compile+0x160/0x314) [<c001c64c>] (bpf_int_jit_compile) from [<c008fb98>] (bpf_prog_select_runtime+0x14/0x124) [<c008fb98>] (bpf_prog_select_runtime) from [<c04a8384>] (bpf_prepare_filter+0x7b0/0x888) [<c04a8384>] (bpf_prepare_filter) from [<c04a84d0>] (bpf_prog_create+0x74/0xa4) [<c04a84d0>] (bpf_prog_create) from [<bf2af218>] (test_bpf_init+0x218/0x77c[test_bpf]) [<bf2af218>] (test_bpf_init[test_bpf]) from [<c0009738>] (do_one_initcall+0x40/0x178) [<c0009738>] (do_one_initcall) from [<c00a1548>] (do_init_module+0x58/0x1bc) [<c00a1548>] (do_init_module) from [<c007d6e8>] (load_module+0x1ae0/0x2050) [<c007d6e8>] (load_module) from [<c007de58>] (SyS_finit_module+0xa4/0xb8) [<c007de58>] (SyS_finit_module) from [<c000a3a0>] (ret_fast_syscall+0x0/0x38)Looking at the .lst file:
/* constants go just after the epilogue */ offset = ctx->offsets[ctx->prog->len]; c0015054: e591c000 ldr ip, [r1] c0015058: e5912018 ldr r2, [r1, #24] offset += ctx->prologue_bytes; offset += ctx->epilogue_bytes; c001505c: e1d132b0 ldrh r3, [r1, #32] offset = ctx->offsets[ctx->prog->len]; c0015060: e59ce008 ldr lr, [ip, #8] offset += ctx->epilogue_bytes; c0015064: e591c008 ldr ip, [r1, #8] c0015068: 0591501c ldreq r5, [r1, #28] offset = ctx->offsets[ctx->prog->len]; c001506c: e792210e ldr r2, [r2, lr, lsl #2] offset += ctx->epilogue_bytes; c0015070: e083300c add r3, r3, ip c0015074: e0833002 add r3, r3, r2 offset += i * 4;
c0015078:e0833004add r3, r3, r4ctx->target[offset / 4] = k; c001507c: e3c32003 bic r2, r3, #3 c0015080: e7850002 str r0, [r5, r2]/* PC in ARM mode == address of the instruction + 8 */ imm = offset - (8 + ctx->idx * 4); Andrew
Quoted 6 lines · Shubham Bansal
Hi Andrew,
I am attaching the latest patch which should work on ARMv5 , ARMv6 and ARMv7.
Please test on your machines. Apologies for the last patch. This patch should work though.
andrew at shuttle:~/linux$ make -s -j 16arch/arm/net/bpf_jit_32.c:1755:12: warning: ???validate_code??? defined but not used [-Wunused-function]
static int validate_code(struct jit_ctx *ctx)
^~~~~~~~~~~~~
arch/arm/net/bpf_jit_32.c:1724:12: warning: ???build_body??? defined but not used [-Wunused-function]
static int build_body(struct jit_ctx *ctx)
^~~~~~~~~~
arch/arm/net/bpf_jit_32.c:1122:13: warning: ???build_epilogue??? defined but not used [-Wunused-function]
static void build_epilogue(struct jit_ctx *ctx)
^~~~~~~~~~~~~~
arch/arm/net/bpf_jit_32.c:1049:13: warning: ???build_prologue??? defined but not used [-Wunused-function]
static void build_prologue(struct jit_ctx *ctx)
^~~~~~~~~~~~~~
arch/arm/net/bpf_jit_32.c:174:13: warning: ???jit_fill_hole??? defined but not used [-Wunused-function]
static void jit_fill_hole(void *area, unsigned int size)
^~~~~~~~~~~~~I _think_ this is because CONFIG_CPU_LITTLE_ENDIAN is not set. But it is a little endian machine/configuration. ARM does not seem to use CONFIG_CPU_LITTLE_ENDIAN, it just has CPU_BIG_ENDIAN.
Andrew
Quoted 25 lines · Andrew Lunn
Quoted 6 lines · Shubham Bansal
Hi Andrew,
I am attaching the latest patch which should work on ARMv5 , ARMv6 and ARMv7.
Please test on your machines. Apologies for the last patch. This patch should work though.
andrew at shuttle:~/linux$ make -s -j 16
arch/arm/net/bpf_jit_32.c:1755:12: warning: ???validate_code??? defined but not used [-Wunused-function]
static intvalidate_code(struct jit_ctx *ctx)^~~~~~~~~~~~~ arch/arm/net/bpf_jit_32.c:1724:12: warning: ???build_body??? defined but not used [-Wunused-function] static int build_body(struct jit_ctx *ctx) ^~~~~~~~~~ arch/arm/net/bpf_jit_32.c:1122:13: warning: ???build_epilogue??? defined but not used [-Wunused-function] static void build_epilogue(struct jit_ctx *ctx) ^~~~~~~~~~~~~~ arch/arm/net/bpf_jit_32.c:1049:13: warning: ???build_prologue??? defined but not used [-Wunused-function] static void build_prologue(struct jit_ctx *ctx) ^~~~~~~~~~~~~~ arch/arm/net/bpf_jit_32.c:174:13: warning: ???jit_fill_hole??? defined but not used [-Wunused-function] static void jit_fill_hole(void *area, unsigned int size) ^~~~~~~~~~~~~
Quoted 3 lines
I _think_ this is because
CONFIG_CPU_LITTLE_ENDIANis not set. But it is a little endian machine/configuration. ARM does not seem to useCONFIG_CPU_LITTLE_ENDIAN, it just hasCPU_BIG_ENDIAN.
After removing the #ifdef:
test_bpf: Summary: 314 PASSED, 0 FAILED, [272/306 JIT'ed]
Each test prints:
ARM version : 5
ARM version : 5
So the code needs some cleanup, but seems to basically work.
Andrew
Quoted 1 line · Shubham Bansal
Actually I intentionally put that print statement their.
Hi Shubham
Please don't top post.
Quoted 1 line
Can you tell me what ifdef you removed ?
#ifdef CONFIG_CPU_LITTLE_ENDIAN
Since i said, ARM does not seem to use it.
Andrew
Hi Andrew,
My Apologies.
I will remove the #ifdef CONFIG_CPU_LITTLE_ENDIAN. Do you have the ARMv6 machine you can test it on?
-Shubham
Quoted 6 lines · Shubham Bansal
Hi Andrew,
My Apologies.
I will remove the #ifdef
CONFIG_CPU_LITTLE_ENDIAN. Do you have the ARMv6 machine you can test it on?
Sorry, i don't have a v6.
Maybe try kzm with qemu-system-qemu?
Andrew
Hi Andrew,
Quoted 11 lines · Andrew Lunn
Quoted 6 lines · Shubham Bansal
Hi Andrew,
My Apologies.
I will remove the #ifdef
CONFIG_CPU_LITTLE_ENDIAN. Do you have the ARMv6 machine you can test it on?Sorry, i don't have a v6.
Maybe try kzm with qemu-system-qemu?
I am afraid I not so familiar with qemu, otherwise I would have tried to test the code on ARMv5,6,7 myself.
If you can help me with that it would be great, Otherwise I think I have to wait for someone to test it for ARMv6. Although, I am pretty sure it would work on that as it worked on ARMv5.
Quoted 2 lines
Andrew
-Shubham
Quoted 13 lines · Shubham Bansal
Hi Andrew,
Quoted 10 lines · Andrew Lunn
Quoted 6 lines · Shubham Bansal
Hi Andrew,
My Apologies.
I will remove the #ifdef
CONFIG_CPU_LITTLE_ENDIAN. Do you have the ARMv6 machine you can test it on?Sorry, i don't have a v6.
Quoted 7 lines
Quoted 1 line
Maybe try kzm with qemu-system-qemu?
I am afraid I not so familiar with qemu, otherwise I would have tried to test the code on ARMv5,6,7 myself.
If you can help me with that it would be great, Otherwise I think I have to wait for someone to test it for ARMv6. Although, I am pretty sure it would work on that as it worked on ARMv5.
Using QEMU is far less complex than the patch you wrote, the tricky point me be to find an ARMv6 root filesystem, unless you build the module into the kernel and let it run its tests.
Signature · 1 line
Florian
Hi Florian,
It would be great if you could help me with it.
Currently, what I am doing is.
root at arm$ ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- make -j4
Then I am using Busybox to build the root filesystem
root at arm$ qemu-system-arm -M vexpress-a9 -dtb ./linux/arch/arm/boot/dts/vexpress-v2p-ca9.dtb -kernel ./linux/arch/arm/boot/zImage -append "root=/dev/mmcblk0 console=ttyAMA0" -sd ./a9rootfs.ext3 --nographic
Which machine I should use for ARMv6 in QEMU. Which dtb file I should use for ? All these questions I have. If anybody knows please let me know.
Best,
Shubham Bansal
Quoted 27 lines · Florian Fainelli
Quoted 13 lines · Shubham Bansal
Hi Andrew,
Quoted 10 lines · Andrew Lunn
Quoted 6 lines · Shubham Bansal
Hi Andrew,
My Apologies.
I will remove the #ifdef
CONFIG_CPU_LITTLE_ENDIAN. Do you have the ARMv6 machine you can test it on?Sorry, i don't have a v6.
Quoted 7 lines
Quoted 1 line
Maybe try kzm with qemu-system-qemu?
I am afraid I not so familiar with qemu, otherwise I would have tried to test the code on ARMv5,6,7 myself.
If you can help me with that it would be great, Otherwise I think I have to wait for someone to test it for ARMv6. Although, I am pretty sure it would work on that as it worked on ARMv5.Using QEMU is far less complex than the patch you wrote, the tricky point me be to find an ARMv6 root filesystem, unless you build the module into the kernel and let it run its tests.
--
Florian
For what it's worth, I did an comparison of the numbers Shubham posted in another thread for the JIT, comparing the eBPF interpreter with his new JIT. The post is here:
https://www.spinics.net/lists/netdev/msg436402.html
-Kees
Signature · 2 lines
Kees Cook Pixel Security
Quoted 10 lines · Shubham Bansal
Hi Florian,
It would be great if you could help me with it.
Currently, what I am doing is.
root at arm$ ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- make -j4
Then I am using Busybox to build the root filesystem
Quoted 8 lines
root at arm$ qemu-system-arm -M vexpress-a9 -dtb ./linux/arch/arm/boot/dts/vexpress-v2p-ca9.dtb -kernel ./linux/arch/arm/boot/zImage -append "root=/dev/mmcblk0 console=ttyAMA0" -sd ./a9rootfs.ext3 --nographic
Which machine I should use for ARMv6 in QEMU. Which dtb file I should use for ? All these questions I have. If anybody knows please let me know.
You should use realview-eb-mpcore for instance (ARM11MPCORE is ARMv6).
Quoted 34 lines
Best,
Shubham BansalQuoted 27 lines · Florian Fainelli
Quoted 13 lines · Shubham Bansal
Hi Andrew,
Quoted 10 lines · Andrew Lunn
Quoted 6 lines · Shubham Bansal
Hi Andrew,
My Apologies.
I will remove the #ifdef
CONFIG_CPU_LITTLE_ENDIAN. Do you have the ARMv6 machine you can test it on?Sorry, i don't have a v6.
Quoted 7 lines
Quoted 1 line
Maybe try kzm with qemu-system-qemu?
I am afraid I not so familiar with qemu, otherwise I would have tried to test the code on ARMv5,6,7 myself.
If you can help me with that it would be great, Otherwise I think I have to wait for someone to test it for ARMv6. Although, I am pretty sure it would work on that as it worked on ARMv5.Using QEMU is far less complex than the patch you wrote, the tricky point me be to find an ARMv6 root filesystem, unless you build the module into the kernel and let it run its tests.
--
Florian
Signature · 1 line
Florian
Russell King - ARM Linux
maintainerQuoted 3 lines · Florian Fainelli
Using QEMU is far less complex than the patch you wrote, the tricky point me be to find an ARMv6 root filesystem, unless you build the module into the kernel and let it run its tests.
I beg to differ - when I tried qemu during the last year, it was basically completely non-functional for ARM, and I gave up with it. I think what you get depends on your distro.
Signature · 3 lines
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/ FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up according to speedtest.net.