arm64: eBPF JIT compiler
Subsystems (6)
v3 cover arm64: eBPF JIT compiler
This series implements eBPF JIT compiler for arm64. See [14/14] for change log.
Patches [1-13/14] implement code generation functions. Patch [14/14] implements the actual eBPF JIT compiler.
Zi Shen Lim (14):
arm64: introduce aarch64_insn_gen_comp_branch_imm()
arm64: introduce aarch64_insn_gen_branch_reg()
arm64: introduce aarch64_insn_gen_cond_branch_imm()
arm64: introduce aarch64_insn_gen_load_store_reg()
arm64: introduce aarch64_insn_gen_load_store_pair()
arm64: introduce aarch64_insn_gen_add_sub_imm()
arm64: introduce aarch64_insn_gen_bitfield()
arm64: introduce aarch64_insn_gen_movewide()
arm64: introduce aarch64_insn_gen_add_sub_shifted_reg()
arm64: introduce aarch64_insn_gen_data1()
arm64: introduce aarch64_insn_gen_data2()
arm64: introduce aarch64_insn_gen_data3()
arm64: introduce aarch64_insn_gen_logical_shifted_reg()
arm64: eBPF JIT compiler
Documentation/networking/filter.txt | 2 +- arch/arm64/Kconfig | 1 + arch/arm64/Makefile | 1 + arch/arm64/include/asm/insn.h | 249 +++++++++++++ arch/arm64/kernel/insn.c | 644 +++++++++++++++++++++++++++++++++- arch/arm64/net/Makefile | 4 + arch/arm64/net/bpf_jit.h | 169 +++++++++ arch/arm64/net/bpf_jit_comp.c | 677 ++++++++++++++++++++++++++++++++++++ 8 files changed, 1739 insertions(+), 8 deletions(-) create mode 100644 arch/arm64/net/Makefile create mode 100644 arch/arm64/net/bpf_jit.h create mode 100644 arch/arm64/net/bpf_jit_comp.c
Signature · 1 line
1.9.1
RFC PATCH v3 1/14 arm64: introduce aarch64_insn_gen_comp_branch_imm()
Introduce function to generate compare & branch (immediate) instructions.
--- arch/arm64/include/asm/insn.h | 57 ++++++++++++++++++++++++++++ arch/arm64/kernel/insn.c | 86 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 138 insertions(+), 5 deletions(-)
arch/arm64/include/asm/insn.h +57 -0
@@ -2,6 +2,8 @@* Copyright (C) 2013 Huawei Ltd. * Author: Jiang Liu <[email protected]> * +* Copyright (C) 2014 Zi Shen Lim <[email protected]> +* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation.
@@ -67,9 +69,58 @@ enum aarch64_insn_imm_type {AARCH64_INSN_IMM_MAX }; +enum aarch64_insn_register_type { +AARCH64_INSN_REGTYPE_RT , +}; + +enum aarch64_insn_register { +AARCH64_INSN_REG_0 =0 , +AARCH64_INSN_REG_1 =1 , +AARCH64_INSN_REG_2 =2 , +AARCH64_INSN_REG_3 =3 , +AARCH64_INSN_REG_4 =4 , +AARCH64_INSN_REG_5 =5 , +AARCH64_INSN_REG_6 =6 , +AARCH64_INSN_REG_7 =7 , +AARCH64_INSN_REG_8 =8 , +AARCH64_INSN_REG_9 =9 , +AARCH64_INSN_REG_10 =10 , +AARCH64_INSN_REG_11 =11 , +AARCH64_INSN_REG_12 =12 , +AARCH64_INSN_REG_13 =13 , +AARCH64_INSN_REG_14 =14 , +AARCH64_INSN_REG_15 =15 , +AARCH64_INSN_REG_16 =16 , +AARCH64_INSN_REG_17 =17 , +AARCH64_INSN_REG_18 =18 , +AARCH64_INSN_REG_19 =19 , +AARCH64_INSN_REG_20 =20 , +AARCH64_INSN_REG_21 =21 , +AARCH64_INSN_REG_22 =22 , +AARCH64_INSN_REG_23 =23 , +AARCH64_INSN_REG_24 =24 , +AARCH64_INSN_REG_25 =25 , +AARCH64_INSN_REG_26 =26 , +AARCH64_INSN_REG_27 =27 , +AARCH64_INSN_REG_28 =28 , +AARCH64_INSN_REG_29 =29 , +AARCH64_INSN_REG_FP =29 ,/* Frame pointer */ +AARCH64_INSN_REG_30 =30 , +AARCH64_INSN_REG_LR =30 ,/* Link register */ +AARCH64_INSN_REG_ZR =31 ,/* Zero: as source register */ +AARCH64_INSN_REG_SP =31 /* Stack pointer: as load/store base reg */ +}; + +enum aarch64_insn_variant { +AARCH64_INSN_VARIANT_32BIT , +AARCH64_INSN_VARIANT_64BIT +}; +enum aarch64_insn_branch_type {AARCH64_INSN_BRANCH_NOLINK ,AARCH64_INSN_BRANCH_LINK , +AARCH64_INSN_BRANCH_COMP_ZERO , +AARCH64_INSN_BRANCH_COMP_NONZERO , };#define __AARCH64_INSN_FUNCS (abbr, mask, val) \
@@ -80,6 +131,8 @@ static __always_inline u32 aarch64_insn_get_##abbr##_value(void) \ __AARCH64_INSN_FUNCS(b, 0xFC000000, 0x14000000) __AARCH64_INSN_FUNCS(bl, 0xFC000000, 0x94000000) +__AARCH64_INSN_FUNCS(cbz, 0xFE000000, 0x34000000) +__AARCH64_INSN_FUNCS(cbnz, 0xFE000000, 0x35000000) __AARCH64_INSN_FUNCS(svc, 0xFFE0001F, 0xD4000001) __AARCH64_INSN_FUNCS(hvc, 0xFFE0001F, 0xD4000002)__AARCH64_INSN_FUNCS (smc,0xFFE0001F ,0xD4000003 )
@@ -97,6 +150,10 @@ u32 aarch64_insn_encode_immediate(enum aarch64_insn_imm_type type, u32 insn, u64 imm);u32 aarch64_insn_gen_branch_imm (unsigned long pc,unsigned long addr,enum aarch64_insn_branch_type type); +u32 aarch64_insn_gen_comp_branch_imm (unsigned long pc,unsigned long addr, +enum aarch64_insn_register reg, +enum aarch64_insn_variant variant, +enum aarch64_insn_branch_type type);u32 aarch64_insn_gen_hint (enum aarch64_insn_hint_op op);u32 aarch64_insn_gen_nop (void );
arch/arm64/kernel/insn.c +81 -5
116 lines not on this page · raw message
Signature · 1 line
1.9.1
Re: [PATCH RFCv3 01/14] arm64: introduce aarch64_insn_gen_comp_branch_imm()
Quoted 56 lines · Zi Shen Lim
Introduce function to generate compare & branch (immediate) instructions.
Signed-off-by Zi Shen Lim <[email protected]>--- arch/arm64/include/asm/insn.h | 57 ++++++++++++++++++++++++++++ arch/arm64/kernel/insn.c | 86 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 138 insertions(+), 5 deletions(-)arch/arm64/include/asm/insn.h +32
-0@@ -2,6 +2,8 @@* Copyright (C) 2013 Huawei Ltd. * Author: Jiang Liu <[email protected]> * +* Copyright (C) 2014 Zi Shen Lim <[email protected]> +* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. @@ -67,9 +69,58 @@ enum aarch64_insn_imm_type {AARCH64_INSN_IMM_MAX }; +enum aarch64_insn_register_type { +AARCH64_INSN_REGTYPE_RT , +}; + +enum aarch64_insn_register { +AARCH64_INSN_REG_0 =0 , +AARCH64_INSN_REG_1 =1 , +AARCH64_INSN_REG_2 =2 , +AARCH64_INSN_REG_3 =3 , +AARCH64_INSN_REG_4 =4 , +AARCH64_INSN_REG_5 =5 , +AARCH64_INSN_REG_6 =6 , +AARCH64_INSN_REG_7 =7 , +AARCH64_INSN_REG_8 =8 , +AARCH64_INSN_REG_9 =9 , +AARCH64_INSN_REG_10 =10 , +AARCH64_INSN_REG_11 =11 , +AARCH64_INSN_REG_12 =12 , +AARCH64_INSN_REG_13 =13 , +AARCH64_INSN_REG_14 =14 , +AARCH64_INSN_REG_15 =15 , +AARCH64_INSN_REG_16 =16 , +AARCH64_INSN_REG_17 =17 , +AARCH64_INSN_REG_18 =18 , +AARCH64_INSN_REG_19 =19 , +AARCH64_INSN_REG_20 =20 , +AARCH64_INSN_REG_21 =21 , +AARCH64_INSN_REG_22 =22 , +AARCH64_INSN_REG_23 =23 , +AARCH64_INSN_REG_24 =24 ,
arch/arm64/include/asm/insn.h enum aarch64_insn_imm_type { · line 111
Quoted 10 lines
arch/arm64/include/asm/insn.h +10
-0+AARCH64_INSN_REG_25 =25 , +AARCH64_INSN_REG_26 =26 , +AARCH64_INSN_REG_27 =27 , +AARCH64_INSN_REG_28 =28 , +AARCH64_INSN_REG_29 =29 , +AARCH64_INSN_REG_FP =29 ,/* Frame pointer */ +AARCH64_INSN_REG_30 =30 , +AARCH64_INSN_REG_LR =30 ,/* Link register */ +AARCH64_INSN_REG_ZR =31 ,/* Zero: as source register */ +AARCH64_INSN_REG_SP =31 /* Stack pointer: as load/store base reg */
Can you just #define AARCH64_INSN_REG(x) instead, then have some magic values like ARM64_REG_LR which are defined as the appropriate numbers?
Quoted 135 lines
arch/arm64/include/asm/insn.h +15
-0+}; + +enum aarch64_insn_variant { +AARCH64_INSN_VARIANT_32BIT , +AARCH64_INSN_VARIANT_64BIT +}; +enum aarch64_insn_branch_type {AARCH64_INSN_BRANCH_NOLINK ,AARCH64_INSN_BRANCH_LINK , +AARCH64_INSN_BRANCH_COMP_ZERO , +AARCH64_INSN_BRANCH_COMP_NONZERO , };#define __AARCH64_INSN_FUNCS (abbr, mask, val) \@@ -80,6 +131,8 @@ static __always_inline u32 aarch64_insn_get_##abbr##_value(void) \ __AARCH64_INSN_FUNCS(b, 0xFC000000, 0x14000000) __AARCH64_INSN_FUNCS(bl, 0xFC000000, 0x94000000) +__AARCH64_INSN_FUNCS(cbz, 0xFE000000, 0x34000000) +__AARCH64_INSN_FUNCS(cbnz, 0xFE000000, 0x35000000) __AARCH64_INSN_FUNCS(svc, 0xFFE0001F, 0xD4000001) __AARCH64_INSN_FUNCS(hvc, 0xFFE0001F, 0xD4000002)__AARCH64_INSN_FUNCS (smc,0xFFE0001F ,0xD4000003 )@@ -97,6 +150,10 @@ u32 aarch64_insn_encode_immediate(enum aarch64_insn_imm_type type, u32 insn, u64 imm);u32 aarch64_insn_gen_branch_imm (unsigned long pc,unsigned long addr,enum aarch64_insn_branch_type type); +u32 aarch64_insn_gen_comp_branch_imm (unsigned long pc,unsigned long addr, +enum aarch64_insn_register reg, +enum aarch64_insn_variant variant, +enum aarch64_insn_branch_type type);u32 aarch64_insn_gen_hint (enum aarch64_insn_hint_op op);u32 aarch64_insn_gen_nop (void );arch/arm64/kernel/insn.c +60
-5@@ -2,6 +2,8 @@* Copyright (C) 2013 Huawei Ltd. * Author: Jiang Liu <[email protected]> * +* Copyright (C) 2014 Zi Shen Lim <[email protected]> +* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. @@ -264,10 +266,36 @@ u32 __kprobes aarch64_insn_encode_immediate(enum aarch64_insn_imm_type type,return insn; } -u32 __kprobes aarch64_insn_gen_branch_imm (unsigned long pc,unsigned long addr, -enum aarch64_insn_branch_type type) +static u32 aarch64_insn_encode_register (enum aarch64_insn_register_type type, +u32 insn, +enum aarch64_insn_register reg) +{ +int shift; + +if (reg <AARCH64_INSN_REG_0 || reg >AARCH64_INSN_REG_SP ) { +pr_err ("%s: unknown register encoding %d\n" , __func__, reg); +return 0 ; + } + +switch (type) { +case AARCH64_INSN_REGTYPE_RT : + shift =0 ; +break ; +default : +pr_err ("%s: unknown register type encoding %d\n" , __func__, + type); +return 0 ; + } + + insn &= ~(GENMASK (4 ,0 ) << shift); + insn |= reg << shift; + +return insn; +} + +static inline long branch_imm_common (unsigned long pc,unsigned long addr, +long range) { -u32 insn;long offset;/* @@ -276,13 +304,24 @@ u32 __kprobes aarch64_insn_gen_branch_imm(unsigned long pc, unsigned long addr,*/ BUG_ON ((pc &0x3 ) || (addr &0x3 )); + offset = ((long )addr - (long )pc); +BUG_ON (offset < -range || offset >= range); + +return offset; +} + +u32 __kprobes aarch64_insn_gen_branch_imm (unsigned long pc,unsigned long addr, +enum aarch64_insn_branch_type type) +{ +u32 insn; +long offset; +/* * B/BL support [-128M, 128M) offset * ARM64 virtual address arrangement guarantees all kernel and module * texts are within +/-128M. */ - offset = ((long )addr - (long )pc); -BUG_ON (offset < -SZ_128M || offset >=SZ_128M ); + offset =branch_imm_common (pc, addr,SZ_128M );if (type ==AARCH64_INSN_BRANCH_LINK ) insn =aarch64_insn_get_bl_value ();@@ -293,6 +332,43 @@ u32 __kprobes aarch64_insn_gen_branch_imm(unsigned long pc, unsigned long addr, offset >> 2); } +u32 aarch64_insn_gen_comp_branch_imm (unsigned long pc,unsigned long addr, +enum aarch64_insn_register reg, +enum aarch64_insn_variant variant, +enum aarch64_insn_branch_type type) +{ +u32 insn; +long offset; + + offset =branch_imm_common (pc, addr,SZ_1M ); + +switch (type) { +case AARCH64_INSN_BRANCH_COMP_ZERO : + insn =aarch64_insn_get_cbz_value (); +break ; +case AARCH64_INSN_BRANCH_COMP_NONZERO : + insn =aarch64_insn_get_cbnz_value ();
arch/arm64/kernel/insn.c u32 __kprobes aarch64_insn_gen_branch_imm(unsigned long pc, unsigned long addr, · line 360
Quoted 10 lines
arch/arm64/kernel/insn.c +10
-0+break ; +default : +BUG_ON (1 ); + } + +switch (variant) { +case AARCH64_INSN_VARIANT_32BIT : +break ; +case AARCH64_INSN_VARIANT_64BIT : + insn |=BIT (31 );
FWIW, that bit (31) is referred to as the `SF' bit in the instruction encodings (for Sixty-Four). You could have a #define for that to help people match up the bitfield, if you like.
arch/arm64/kernel/insn.c u32 __kprobes aarch64_insn_gen_branch_imm(unsigned long pc, unsigned long addr, · line 353
Quoted 3 lines
arch/arm64/kernel/insn.c +3
-0+break ; +default : +BUG_ON (1 );
Is a BUG_ON justifiable here? Is there not a nicer way to fail?
Will
Re: [PATCH RFCv3 01/14] arm64: introduce aarch64_insn_gen_comp_branch_imm()
Quoted 1 line · Will Deacon
On Tue, Jul 15, 2014 at 07:24:59AM +0100, Zi Shen Lim wrote:
[...]
Quoted 31 lines
Quoted 31 lines
arch/arm64/include/asm/insn.h +31
-0+enum aarch64_insn_register { +AARCH64_INSN_REG_0 =0 , +AARCH64_INSN_REG_1 =1 , +AARCH64_INSN_REG_2 =2 , +AARCH64_INSN_REG_3 =3 , +AARCH64_INSN_REG_4 =4 , +AARCH64_INSN_REG_5 =5 , +AARCH64_INSN_REG_6 =6 , +AARCH64_INSN_REG_7 =7 , +AARCH64_INSN_REG_8 =8 , +AARCH64_INSN_REG_9 =9 , +AARCH64_INSN_REG_10 =10 , +AARCH64_INSN_REG_11 =11 , +AARCH64_INSN_REG_12 =12 , +AARCH64_INSN_REG_13 =13 , +AARCH64_INSN_REG_14 =14 , +AARCH64_INSN_REG_15 =15 , +AARCH64_INSN_REG_16 =16 , +AARCH64_INSN_REG_17 =17 , +AARCH64_INSN_REG_18 =18 , +AARCH64_INSN_REG_19 =19 , +AARCH64_INSN_REG_20 =20 , +AARCH64_INSN_REG_21 =21 , +AARCH64_INSN_REG_22 =22 , +AARCH64_INSN_REG_23 =23 , +AARCH64_INSN_REG_24 =24 , +AARCH64_INSN_REG_25 =25 , +AARCH64_INSN_REG_26 =26 , +AARCH64_INSN_REG_27 =27 , +AARCH64_INSN_REG_28 =28 , +AARCH64_INSN_REG_29 =29 ,
Quoted 8 lines
arch/arm64/include/asm/insn.h enum aarch64_insn_imm_type { · line 111
Quoted 5 lines
arch/arm64/include/asm/insn.h +5
-0+AARCH64_INSN_REG_FP =29 ,/* Frame pointer */ +AARCH64_INSN_REG_30 =30 , +AARCH64_INSN_REG_LR =30 ,/* Link register */ +AARCH64_INSN_REG_ZR =31 ,/* Zero: as source register */ +AARCH64_INSN_REG_SP =31 /* Stack pointer: as load/store base reg */ Can you just #define
AARCH64_INSN_REG(x)instead, then have some magic values likeARM64_REG_LRwhich are defined as the appropriate numbers?
I actually had something like what you mentioned in the beginning, but decided to go with the above - thinking that it's clearer to present the complete set of valid register definitions.
The #define can still be added for convenience, though I think it's also a potential source of errors - it's much easier to typo something like AARCH64_INSN_REG(32) and not get caught.
[...]
Quoted 9 lines
arch/arm64/kernel/insn.c u32 __kprobes aarch64_insn_gen_branch_imm(unsigned long pc, unsigned long addr, · line 360
Quoted 5 lines
arch/arm64/kernel/insn.c +5
-0+switch (variant) { +case AARCH64_INSN_VARIANT_32BIT : +break ; +case AARCH64_INSN_VARIANT_64BIT : + insn |=BIT (31 );FWIW, that bit (31) is referred to as the `SF' bit in the instruction encodings (for Sixty-Four). You could have a #define for that to help people match up the bitfield, if you like.
Something like this?
#define AARCH64_INSN_SF_BIT BIT(31) ... case AARCH64_INSN_VARIANT_64BIT: insn |= AARCH64_INSN_SF_BIT;
In the case of bitfield instruction, there's also an "N" bit. So something like this?
#define AARCH64_INSN_N_BIT BIT(22) ... case AARCH64_INSN_VARIANT_64BIT: insn |= AARCH64_INSN_SF_BIT | AARCH64_INSN_N_BIT;
Quoted 6 lines
arch/arm64/kernel/insn.c u32 __kprobes aarch64_insn_gen_branch_imm(unsigned long pc, unsigned long addr, · line 353
Quoted 4 lines
arch/arm64/kernel/insn.c +3
-0+break ; +default : +BUG_ON (1 );Is a
BUG_ONjustifiable here? Is there not a nicer way to fail?
In general, it'd be nice if we returned something like -EINVAL and have all callers handle failures. Today all code gen functions return the u32 instruction and there's no error handling by callers. I think following the precedence (aarch64_insn_gen_branch_imm()) of failing with BUG_ON is a reasonable tradeoff.
In this case here, when we hit the default (failure) case, that means there's a serious error of attempting to use an unsupported variant. I think we're better off failing hard here than trying to arbitrarily "fallback" on a default choice.
One potential option instead of switch (variant) is:
if (variant == AARCH64_INSN_VARIANT_64BIT) /* do something */ else /* do something else */
which would be quite reasonable to do as we only have VARIANT_{32,64}BIT today.
However, consider the case where we add VARIANT_128BIT or other flavors in the future. The if/else option (basically defaulting to VARIANT_32BIT) would then make much less sense.
Quoted 2 lines
Will
Re: [PATCH RFCv3 01/14] arm64: introduce aarch64_insn_gen_comp_branch_imm()
Quoted 43 lines · Zi Shen Lim
Quoted 1 line · Will Deacon
On Tue, Jul 15, 2014 at 07:24:59AM +0100, Zi Shen Lim wrote:
[...]
Quoted 31 lines
Quoted 31 lines
arch/arm64/include/asm/insn.h +31
-0+enum aarch64_insn_register { +AARCH64_INSN_REG_0 =0 , +AARCH64_INSN_REG_1 =1 , +AARCH64_INSN_REG_2 =2 , +AARCH64_INSN_REG_3 =3 , +AARCH64_INSN_REG_4 =4 , +AARCH64_INSN_REG_5 =5 , +AARCH64_INSN_REG_6 =6 , +AARCH64_INSN_REG_7 =7 , +AARCH64_INSN_REG_8 =8 , +AARCH64_INSN_REG_9 =9 , +AARCH64_INSN_REG_10 =10 , +AARCH64_INSN_REG_11 =11 , +AARCH64_INSN_REG_12 =12 , +AARCH64_INSN_REG_13 =13 , +AARCH64_INSN_REG_14 =14 , +AARCH64_INSN_REG_15 =15 , +AARCH64_INSN_REG_16 =16 , +AARCH64_INSN_REG_17 =17 , +AARCH64_INSN_REG_18 =18 , +AARCH64_INSN_REG_19 =19 , +AARCH64_INSN_REG_20 =20 , +AARCH64_INSN_REG_21 =21 , +AARCH64_INSN_REG_22 =22 , +AARCH64_INSN_REG_23 =23 , +AARCH64_INSN_REG_24 =24 , +AARCH64_INSN_REG_25 =25 , +AARCH64_INSN_REG_26 =26 , +AARCH64_INSN_REG_27 =27 , +AARCH64_INSN_REG_28 =28 , +AARCH64_INSN_REG_29 =29 ,Quoted 8 lines
arch/arm64/include/asm/insn.h enum aarch64_insn_imm_type { · line 111
Quoted 5 lines
arch/arm64/include/asm/insn.h +5
-0+AARCH64_INSN_REG_FP =29 ,/* Frame pointer */ +AARCH64_INSN_REG_30 =30 , +AARCH64_INSN_REG_LR =30 ,/* Link register */ +AARCH64_INSN_REG_ZR =31 ,/* Zero: as source register */ +AARCH64_INSN_REG_SP =31 /* Stack pointer: as load/store base reg */ Can you just #define
AARCH64_INSN_REG(x)instead, then have some magic values likeARM64_REG_LRwhich are defined as the appropriate numbers?
Quoted 7 lines
I actually had something like what you mentioned in the beginning, but decided to go with the above - thinking that it's clearer to present the complete set of valid register definitions.
The #define can still be added for convenience, though I think it's also a potential source of errors - it's much easier to typo something like
AARCH64_INSN_REG(32)and not get caught.
Fair enough, that's a good enough reason to leave it like it is.
Quoted 23 lines
[...]
Quoted 9 lines
arch/arm64/kernel/insn.c u32 __kprobes aarch64_insn_gen_branch_imm(unsigned long pc, unsigned long addr, · line 360
Quoted 5 lines
arch/arm64/kernel/insn.c +5
-0+switch (variant) { +case AARCH64_INSN_VARIANT_32BIT : +break ; +case AARCH64_INSN_VARIANT_64BIT : + insn |=BIT (31 );FWIW, that bit (31) is referred to as the `SF' bit in the instruction encodings (for Sixty-Four). You could have a #define for that to help people match up the bitfield, if you like.
Something like this?
#define AARCH64_INSN_SF_BIT BIT(31) ... case AARCH64_INSN_VARIANT_64BIT: insn |= AARCH64_INSN_SF_BIT;In the case of bitfield instruction, there's also an "N" bit. So something like this?
Quoted 6 lines
#define AARCH64_INSN_N_BIT BIT(22) ... case AARCH64_INSN_VARIANT_64BIT: insn |= AARCH64_INSN_SF_BIT | AARCH64_INSN_N_BIT;
Looks good.
Quoted 7 lines
Quoted 6 lines
arch/arm64/kernel/insn.c u32 __kprobes aarch64_insn_gen_branch_imm(unsigned long pc, unsigned long addr, · line 353
Quoted 4 lines
arch/arm64/kernel/insn.c +3
-0+break ; +default : +BUG_ON (1 );Is a
BUG_ONjustifiable here? Is there not a nicer way to fail?
Quoted 5 lines
In general, it'd be nice if we returned something like
-EINVALand have all callers handle failures. Today all code gen functions return the u32 instruction and there's no error handling by callers. I think following the precedence (aarch64_insn_gen_branch_imm()) of failing withBUG_ONis a reasonable tradeoff.
Well, I don't necessarily agree with that BUG_ON, either :)
I take it eBPF doesn't have a `trap' instruction or similar? Otherwise, we could generate that and avoid having to propagate errors directly to the caller.
Quoted 4 lines
In this case here, when we hit the default (failure) case, that means there's a serious error of attempting to use an unsupported variant. I think we're better off failing hard here than trying to arbitrarily "fallback" on a default choice.
It might be a serious error for BPF, but a BUG_ON brings down the entire machine, which I think is unfortunate.
Quoted 8 lines
One potential option instead of switch (variant) is:
if (variant == AARCH64_INSN_VARIANT_64BIT) /* do something */ else /* do something else */
Quoted 6 lines
which would be quite reasonable to do as we only have VARIANT_{32,64}BIT today.
However, consider the case where we add
VARIANT_128BITor other flavors in the future. The if/else option (basically defaulting toVARIANT_32BIT) would then make much less sense.
I don't think we need to worry about hypothetical extensions to the instruction set at this stage.
Will
Re: [PATCH RFCv3 01/14] arm64: introduce aarch64_insn_gen_comp_branch_imm()
Quoted 15 lines · Will Deacon
Quoted 8 lines · Zi Shen Lim
Quoted 2 lines
Is a
BUG_ONjustifiable here? Is there not a nicer way to fail?In general, it'd be nice if we returned something like
-EINVALand have all callers handle failures. Today all code gen functions return the u32 instruction and there's no error handling by callers. I think following the precedence (aarch64_insn_gen_branch_imm()) of failing withBUG_ONis a reasonable tradeoff.Well, I don't necessarily agree with that
BUG_ON, either :)
I take it eBPF doesn't have a `trap' instruction or similar? Otherwise, we could generate that and avoid having to propagate errors directly to the caller.
Quoted 7 lines
Quoted 4 lines
In this case here, when we hit the default (failure) case, that means there's a serious error of attempting to use an unsupported variant. I think we're better off failing hard here than trying to arbitrarily "fallback" on a default choice.
It might be a serious error for BPF, but a
BUG_ONbrings down the entire machine, which I think is unfortunate.
There is some misunderstanding here. Here BUG_ON will trigger only on actual bug in JIT implementation, it cannot be triggered by user. eBPF program is verified before it reaches JIT, so all instructions are valid and input to JIT is proper. Two instruction are not yet implemented in this JIT and they trigger pr_.._once().
So I don't see any issue with this usage of BUG_ON
imo living with silent bugs in JIT is more dangerous.
For the same reason there is no 'trap' instruction in eBPF. Static verifier checks that program is valid. If there was a 'trap' insn the program would be rejected. Like programs with 'div by zero' are rejected. There is normal 'bpf_exit' insn to return from the program.
Re: [PATCH RFCv3 01/14] arm64: introduce aarch64_insn_gen_comp_branch_imm()
Quoted 32 lines · Alexei Starovoitov
Quoted 15 lines · Will Deacon
Quoted 8 lines · Zi Shen Lim
Quoted 2 lines
Is a
BUG_ONjustifiable here? Is there not a nicer way to fail?In general, it'd be nice if we returned something like
-EINVALand have all callers handle failures. Today all code gen functions return the u32 instruction and there's no error handling by callers. I think following the precedence (aarch64_insn_gen_branch_imm()) of failing withBUG_ONis a reasonable tradeoff.Well, I don't necessarily agree with that
BUG_ON, either :)
I take it eBPF doesn't have a `trap' instruction or similar? Otherwise, we could generate that and avoid having to propagate errors directly to the caller.Quoted 7 lines
Quoted 4 lines
In this case here, when we hit the default (failure) case, that means there's a serious error of attempting to use an unsupported variant. I think we're better off failing hard here than trying to arbitrarily "fallback" on a default choice.
It might be a serious error for BPF, but a
BUG_ONbrings down the entire machine, which I think is unfortunate.There is some misunderstanding here. Here
BUG_ONwill trigger only on actual bug in JIT implementation, it cannot be triggered by user. eBPF program is verified before it reaches JIT, so all instructions are valid and input to JIT is proper. Two instruction are not yet implemented in this JIT and they trigger pr_.._once().
So I don't see any issue with this usage ofBUG_ON
imo living with silent bugs in JIT is more dangerous.
Quoted 5 lines
For the same reason there is no 'trap' instruction in eBPF. Static verifier checks that program is valid. If there was a 'trap' insn the program would be rejected. Like programs with 'div by zero' are rejected. There is normal '
bpf_exit' insn to return from the program.
Ok, so assuming that BPF doesn't have any issues, I take your point. However, we could very easily re-use these functions for things like SMP alternatives and kprobes, where simply failing the instruction generation might be acceptable.
It just feels like a bit hammer to me, when the machine is probably happily scheduling user tasks, responding to interrupts, writing data to disk etc.
Will
Re: [PATCH RFCv3 01/14] arm64: introduce aarch64_insn_gen_comp_branch_imm()
(resending this email in case the first one got caught in your spam filter. sorry.)
Quoted 39 lines · Will Deacon
Quoted 32 lines · Alexei Starovoitov
Quoted 15 lines · Will Deacon
Quoted 8 lines · Zi Shen Lim
Quoted 2 lines
Is a
BUG_ONjustifiable here? Is there not a nicer way to fail?In general, it'd be nice if we returned something like
-EINVALand have all callers handle failures. Today all code gen functions return the u32 instruction and there's no error handling by callers. I think following the precedence (aarch64_insn_gen_branch_imm()) of failing withBUG_ONis a reasonable tradeoff.Well, I don't necessarily agree with that
BUG_ON, either :)
I take it eBPF doesn't have a `trap' instruction or similar? Otherwise, we could generate that and avoid having to propagate errors directly to the caller.Quoted 7 lines
Quoted 4 lines
In this case here, when we hit the default (failure) case, that means there's a serious error of attempting to use an unsupported variant. I think we're better off failing hard here than trying to arbitrarily "fallback" on a default choice.
It might be a serious error for BPF, but a
BUG_ONbrings down the entire machine, which I think is unfortunate.There is some misunderstanding here. Here
BUG_ONwill trigger only on actual bug in JIT implementation, it cannot be triggered by user. eBPF program is verified before it reaches JIT, so all instructions are valid and input to JIT is proper. Two instruction are not yet implemented in this JIT and they trigger pr_.._once().
So I don't see any issue with this usage ofBUG_ON
imo living with silent bugs in JIT is more dangerous.Quoted 5 lines
For the same reason there is no 'trap' instruction in eBPF. Static verifier checks that program is valid. If there was a 'trap' insn the program would be rejected. Like programs with 'div by zero' are rejected. There is normal '
bpf_exit' insn to return from the program.
Quoted 7 lines
Ok, so assuming that BPF doesn't have any issues, I take your point. However, we could very easily re-use these functions for things like SMP alternatives and kprobes, where simply failing the instruction generation might be acceptable.
It just feels like a bit hammer to me, when the machine is probably happily scheduling user tasks, responding to interrupts, writing data to disk etc.
Yes I agree with you Will, it'd be truly unfortunate if we inadvertently allow the entire system to be brought down.
Alexei accurately pointed out that if we ever hit such a case, it'd be a bug in the BPF JIT implementation (or bug in other in-kernel implementations).
Our BPF JIT implementation actually handles this, making sure that input to the codegen function is valid, or gracefully fail by not JITing and falling back on the core BPF interpreter. This way our JIT will not trigger the BUG_ON.
IMO, other future users of these codegen functions should do the same.
An alternative would be to throw away all the BUG_ON and have callers check for and handle error conditions. I think this is actually more dangerous as callers who don't handle the error conditions properly may end up causing system crash later with subtle (and quite possibly hard to debug) bugs.
Quoted 2 lines
Will
RFC PATCH v3 2/14 arm64: introduce aarch64_insn_gen_branch_reg()
In reply to Zi Shen LimIntroduce function to generate unconditional branch (register) instructions.
--- arch/arm64/include/asm/insn.h | 7 +++++++ arch/arm64/kernel/insn.c | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-)
arch/arm64/include/asm/insn.h +7 -0
@@ -71,6 +71,7 @@ enum aarch64_insn_imm_type {enum aarch64_insn_register_type {AARCH64_INSN_REGTYPE_RT , +AARCH64_INSN_REGTYPE_RN , };enum aarch64_insn_register {
@@ -119,6 +120,7 @@ enum aarch64_insn_variant {enum aarch64_insn_branch_type {AARCH64_INSN_BRANCH_NOLINK ,AARCH64_INSN_BRANCH_LINK , +AARCH64_INSN_BRANCH_RETURN ,AARCH64_INSN_BRANCH_COMP_ZERO ,AARCH64_INSN_BRANCH_COMP_NONZERO , };
@@ -138,6 +140,9 @@ __AARCH64_INSN_FUNCS(hvc, 0xFFE0001F, 0xD4000002) __AARCH64_INSN_FUNCS(smc, 0xFFE0001F, 0xD4000003) __AARCH64_INSN_FUNCS(brk, 0xFFE0001F, 0xD4200000) __AARCH64_INSN_FUNCS(hint, 0xFFFFF01F, 0xD503201F) +__AARCH64_INSN_FUNCS(br, 0xFFFFFC1F, 0xD61F0000) +__AARCH64_INSN_FUNCS(blr, 0xFFFFFC1F, 0xD63F0000) +__AARCH64_INSN_FUNCS(ret, 0xFFFFFC1F, 0xD65F0000)#undef __AARCH64_INSN_FUNCS
@@ -156,6 +161,8 @@ u32 aarch64_insn_gen_comp_branch_imm(unsigned long pc, unsigned long addr, enum aarch64_insn_branch_type type);u32 aarch64_insn_gen_hint (enum aarch64_insn_hint_op op);u32 aarch64_insn_gen_nop (void ); +u32 aarch64_insn_gen_branch_reg (enum aarch64_insn_register reg, +enum aarch64_insn_branch_type type);bool aarch64_insn_hotpatch_safe (u32 old_insn,u32 new_insn);
arch/arm64/kernel/insn.c +33 -2
@@ -281,6 +281,9 @@ static u32 aarch64_insn_encode_register(enum aarch64_insn_register_type type,case AARCH64_INSN_REGTYPE_RT : shift =0 ;break ; +case AARCH64_INSN_REGTYPE_RN : + shift =5 ; +break ;default :pr_err ("%s: unknown register type encoding %d\n" , __func__, type);
@@ -323,10 +326,16 @@ u32 __kprobes aarch64_insn_gen_branch_imm(unsigned long pc, unsigned long addr,*/ offset =branch_imm_common (pc, addr,SZ_128M ); -if (type ==AARCH64_INSN_BRANCH_LINK ) +switch (type) { +case AARCH64_INSN_BRANCH_LINK : insn =aarch64_insn_get_bl_value (); -else +break ; +case AARCH64_INSN_BRANCH_NOLINK : insn =aarch64_insn_get_b_value (); +break ; +default : +BUG_ON (1 ); + }return aarch64_insn_encode_immediate (AARCH64_INSN_IMM_26 , insn, offset >>2 );
@@ -378,3 +387,25 @@ u32 __kprobes aarch64_insn_gen_nop(void) {return aarch64_insn_gen_hint (AARCH64_INSN_HINT_NOP ); } + +u32 aarch64_insn_gen_branch_reg (enum aarch64_insn_register reg, +enum aarch64_insn_branch_type type) +{ +u32 insn; + +switch (type) { +case AARCH64_INSN_BRANCH_NOLINK : + insn =aarch64_insn_get_br_value (); +break ; +case AARCH64_INSN_BRANCH_LINK : + insn =aarch64_insn_get_blr_value (); +break ; +case AARCH64_INSN_BRANCH_RETURN : + insn =aarch64_insn_get_ret_value (); +break ; +default : +BUG_ON (1 ); + } + +return aarch64_insn_encode_register (AARCH64_INSN_REGTYPE_RN , insn, reg); +}
Signature · 1 line
1.9.1
RFC PATCH v3 3/14 arm64: introduce aarch64_insn_gen_cond_branch_imm()
In reply to Zi Shen LimIntroduce function to generate conditional branch (immediate) instructions.
--- arch/arm64/include/asm/insn.h | 21 +++++++++++++++++++++ arch/arm64/kernel/insn.c | 17 +++++++++++++++++ 2 files changed, 38 insertions(+)
arch/arm64/include/asm/insn.h +21 -0
@@ -117,6 +117,24 @@ enum aarch64_insn_variant {AARCH64_INSN_VARIANT_64BIT }; +enum aarch64_insn_condition { +AARCH64_INSN_COND_EQ =0x0 ,/* == */ +AARCH64_INSN_COND_NE =0x1 ,/* != */ +AARCH64_INSN_COND_CS =0x2 ,/* unsigned >= */ +AARCH64_INSN_COND_CC =0x3 ,/* unsigned < */ +AARCH64_INSN_COND_MI =0x4 ,/* < 0 */ +AARCH64_INSN_COND_PL =0x5 ,/* >= 0 */ +AARCH64_INSN_COND_VS =0x6 ,/* overflow */ +AARCH64_INSN_COND_VC =0x7 ,/* no overflow */ +AARCH64_INSN_COND_HI =0x8 ,/* unsigned > */ +AARCH64_INSN_COND_LS =0x9 ,/* unsigned <= */ +AARCH64_INSN_COND_GE =0xa ,/* signed >= */ +AARCH64_INSN_COND_LT =0xb ,/* signed < */ +AARCH64_INSN_COND_GT =0xc ,/* signed > */ +AARCH64_INSN_COND_LE =0xd ,/* signed <= */ +AARCH64_INSN_COND_AL =0xe ,/* always */ +}; +enum aarch64_insn_branch_type {AARCH64_INSN_BRANCH_NOLINK ,AARCH64_INSN_BRANCH_LINK ,
@@ -135,6 +153,7 @@ __AARCH64_INSN_FUNCS(b, 0xFC000000, 0x14000000) __AARCH64_INSN_FUNCS(bl, 0xFC000000, 0x94000000) __AARCH64_INSN_FUNCS(cbz, 0xFE000000, 0x34000000) __AARCH64_INSN_FUNCS(cbnz, 0xFE000000, 0x35000000) +__AARCH64_INSN_FUNCS(bcond, 0xFF000010, 0x54000000) __AARCH64_INSN_FUNCS(svc, 0xFFE0001F, 0xD4000001) __AARCH64_INSN_FUNCS(hvc, 0xFFE0001F, 0xD4000002)__AARCH64_INSN_FUNCS (smc,0xFFE0001F ,0xD4000003 )
@@ -159,6 +178,8 @@ u32 aarch64_insn_gen_comp_branch_imm(unsigned long pc, unsigned long addr,enum aarch64_insn_register reg, enum aarch64_insn_variant variant, enum aarch64_insn_branch_type type); +u32 aarch64_insn_gen_cond_branch_imm (unsigned long pc,unsigned long addr, +enum aarch64_insn_condition cond);u32 aarch64_insn_gen_hint (enum aarch64_insn_hint_op op);u32 aarch64_insn_gen_nop (void ); u32 aarch64_insn_gen_branch_reg(enum aarch64_insn_register reg,
arch/arm64/kernel/insn.c +17 -0
@@ -378,6 +378,23 @@ u32 aarch64_insn_gen_comp_branch_imm(unsigned long pc, unsigned long addr, offset >> 2); } +u32 aarch64_insn_gen_cond_branch_imm (unsigned long pc,unsigned long addr, +enum aarch64_insn_condition cond) +{ +u32 insn; +long offset; + + offset =branch_imm_common (pc, addr,SZ_1M ); + + insn =aarch64_insn_get_bcond_value (); + +BUG_ON (cond <AARCH64_INSN_COND_EQ || cond >AARCH64_INSN_COND_AL ); + insn |= cond; + +return aarch64_insn_encode_immediate (AARCH64_INSN_IMM_19 , insn, + offset >>2 ); +} +u32 __kprobes aarch64_insn_gen_hint (enum aarch64_insn_hint_op op) {return aarch64_insn_get_hint_value () | op;
Signature · 1 line
1.9.1
RFC PATCH v3 4/14 arm64: introduce aarch64_insn_gen_load_store_reg()
In reply to Zi Shen LimIntroduce function to generate load/store (register offset) instructions.
--- arch/arm64/include/asm/insn.h | 20 ++++++++++++++ arch/arm64/kernel/insn.c | 62 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+)
arch/arm64/include/asm/insn.h +20 -0
@@ -72,6 +72,7 @@ enum aarch64_insn_imm_type {enum aarch64_insn_register_type {AARCH64_INSN_REGTYPE_RT ,AARCH64_INSN_REGTYPE_RN , +AARCH64_INSN_REGTYPE_RM , };enum aarch64_insn_register {
@@ -143,12 +144,26 @@ enum aarch64_insn_branch_type { AARCH64_INSN_BRANCH_COMP_NONZERO, }; +enum aarch64_insn_size_type { +AARCH64_INSN_SIZE_8 , +AARCH64_INSN_SIZE_16 , +AARCH64_INSN_SIZE_32 , +AARCH64_INSN_SIZE_64 , +}; + +enum aarch64_insn_ldst_type { +AARCH64_INSN_LDST_LOAD_REG_OFFSET , +AARCH64_INSN_LDST_STORE_REG_OFFSET , +}; +#define __AARCH64_INSN_FUNCS (abbr, mask, val) \ static __always_inline bool aarch64_insn_is_##abbr(u32 code) \ { return (code & (mask)) == (val); } \ static __always_inline u32 aarch64_insn_get_##abbr##_value(void) \ { return (val); } +__AARCH64_INSN_FUNCS(str_reg, 0x3FE0EC00, 0x38206800) +__AARCH64_INSN_FUNCS(ldr_reg, 0x3FE0EC00, 0x38606800) __AARCH64_INSN_FUNCS(b, 0xFC000000, 0x14000000) __AARCH64_INSN_FUNCS(bl, 0xFC000000, 0x94000000)__AARCH64_INSN_FUNCS (cbz,0xFE000000 ,0x34000000 )
@@ -184,6 +199,11 @@ u32 aarch64_insn_gen_hint(enum aarch64_insn_hint_op op);u32 aarch64_insn_gen_nop (void );u32 aarch64_insn_gen_branch_reg (enum aarch64_insn_register reg,enum aarch64_insn_branch_type type); +u32 aarch64_insn_gen_load_store_reg (enum aarch64_insn_register reg, +enum aarch64_insn_register base, +enum aarch64_insn_register offset, +enum aarch64_insn_size_type size, +enum aarch64_insn_ldst_type type);bool aarch64_insn_hotpatch_safe (u32 old_insn,u32 new_insn);
arch/arm64/kernel/insn.c +62 -0
77 lines not on this page · raw message
Signature · 1 line
1.9.1
RFC PATCH v3 5/14 arm64: introduce aarch64_insn_gen_load_store_pair()
In reply to Zi Shen LimIntroduce function to generate load/store pair instructions.
--- arch/arm64/include/asm/insn.h | 16 +++++++++++ arch/arm64/kernel/insn.c | 65 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+)
arch/arm64/include/asm/insn.h +16 -0
@@ -66,12 +66,14 @@ enum aarch64_insn_imm_type {AARCH64_INSN_IMM_14 ,AARCH64_INSN_IMM_12 ,AARCH64_INSN_IMM_9 , +AARCH64_INSN_IMM_7 , AARCH64_INSN_IMM_MAX };enum aarch64_insn_register_type {AARCH64_INSN_REGTYPE_RT ,AARCH64_INSN_REGTYPE_RN , +AARCH64_INSN_REGTYPE_RT2 ,AARCH64_INSN_REGTYPE_RM , };
@@ -154,6 +156,10 @@ enum aarch64_insn_size_type {enum aarch64_insn_ldst_type {AARCH64_INSN_LDST_LOAD_REG_OFFSET ,AARCH64_INSN_LDST_STORE_REG_OFFSET , +AARCH64_INSN_LDST_LOAD_PAIR_PRE_INDEX , +AARCH64_INSN_LDST_STORE_PAIR_PRE_INDEX , +AARCH64_INSN_LDST_LOAD_PAIR_POST_INDEX , +AARCH64_INSN_LDST_STORE_PAIR_POST_INDEX , };#define __AARCH64_INSN_FUNCS (abbr, mask, val) \
@@ -164,6 +170,10 @@ static __always_inline u32 aarch64_insn_get_##abbr##_value(void) \ __AARCH64_INSN_FUNCS(str_reg, 0x3FE0EC00, 0x38206800) __AARCH64_INSN_FUNCS(ldr_reg, 0x3FE0EC00, 0x38606800) +__AARCH64_INSN_FUNCS(stp_post, 0x7FC00000, 0x28800000) +__AARCH64_INSN_FUNCS(ldp_post, 0x7FC00000, 0x28C00000) +__AARCH64_INSN_FUNCS(stp_pre, 0x7FC00000, 0x29800000) +__AARCH64_INSN_FUNCS(ldp_pre, 0x7FC00000, 0x29C00000) __AARCH64_INSN_FUNCS(b, 0xFC000000, 0x14000000) __AARCH64_INSN_FUNCS(bl, 0xFC000000, 0x94000000)__AARCH64_INSN_FUNCS (cbz,0xFE000000 ,0x34000000 )
@@ -204,6 +214,12 @@ u32 aarch64_insn_gen_load_store_reg(enum aarch64_insn_register reg,enum aarch64_insn_register offset, enum aarch64_insn_size_type size, enum aarch64_insn_ldst_type type); +u32 aarch64_insn_gen_load_store_pair (enum aarch64_insn_register reg1, +enum aarch64_insn_register reg2, +enum aarch64_insn_register base, +int offset, +enum aarch64_insn_variant variant, +enum aarch64_insn_ldst_type type);bool aarch64_insn_hotpatch_safe (u32 old_insn,u32 new_insn);
arch/arm64/kernel/insn.c +65 -0
80 lines not on this page · raw message
Signature · 1 line
1.9.1
RFC PATCH v3 6/14 arm64: introduce aarch64_insn_gen_add_sub_imm()
In reply to Zi Shen LimIntroduce function to generate add/subtract (immediate) instructions.
--- arch/arm64/include/asm/insn.h | 16 ++++++++++++++++ arch/arm64/kernel/insn.c | 44 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+)
arch/arm64/include/asm/insn.h +16 -0
@@ -75,6 +75,7 @@ enum aarch64_insn_register_type {AARCH64_INSN_REGTYPE_RN ,AARCH64_INSN_REGTYPE_RT2 ,AARCH64_INSN_REGTYPE_RM , + AARCH64_INSN_REGTYPE_RD, }; enum aarch64_insn_register {
@@ -162,6 +163,13 @@ enum aarch64_insn_ldst_type { AARCH64_INSN_LDST_STORE_PAIR_POST_INDEX, }; +enum aarch64_insn_adsb_type { +AARCH64_INSN_ADSB_ADD , +AARCH64_INSN_ADSB_SUB , +AARCH64_INSN_ADSB_ADD_SETFLAGS , +AARCH64_INSN_ADSB_SUB_SETFLAGS +}; +#define __AARCH64_INSN_FUNCS (abbr, mask, val) \ static __always_inline bool aarch64_insn_is_##abbr(u32 code) \ { return (code & (mask)) == (val); } \
@@ -174,6 +182,10 @@ __AARCH64_INSN_FUNCS(stp_post, 0x7FC00000, 0x28800000) __AARCH64_INSN_FUNCS(ldp_post, 0x7FC00000, 0x28C00000) __AARCH64_INSN_FUNCS(stp_pre, 0x7FC00000, 0x29800000) __AARCH64_INSN_FUNCS(ldp_pre, 0x7FC00000, 0x29C00000) +__AARCH64_INSN_FUNCS(add_imm, 0x7F000000, 0x11000000) +__AARCH64_INSN_FUNCS(adds_imm, 0x7F000000, 0x31000000) +__AARCH64_INSN_FUNCS(sub_imm, 0x7F000000, 0x51000000) +__AARCH64_INSN_FUNCS(subs_imm, 0x7F000000, 0x71000000) __AARCH64_INSN_FUNCS(b, 0xFC000000, 0x14000000) __AARCH64_INSN_FUNCS(bl, 0xFC000000, 0x94000000)__AARCH64_INSN_FUNCS (cbz,0xFE000000 ,0x34000000 )
@@ -220,6 +232,10 @@ u32 aarch64_insn_gen_load_store_pair(enum aarch64_insn_register reg1,int offset, enum aarch64_insn_variant variant, enum aarch64_insn_ldst_type type); +u32 aarch64_insn_gen_add_sub_imm (enum aarch64_insn_register dst, +enum aarch64_insn_register src, +int imm,enum aarch64_insn_variant variant, +enum aarch64_insn_adsb_type type);bool aarch64_insn_hotpatch_safe (u32 old_insn,u32 new_insn);
arch/arm64/kernel/insn.c +44 -0
53 lines not on this page · raw message
Signature · 1 line
1.9.1
RFC PATCH v3 7/14 arm64: introduce aarch64_insn_gen_bitfield()
In reply to Zi Shen LimIntroduce function to generate bitfield instructions.
--- arch/arm64/include/asm/insn.h | 16 +++++++++++++ arch/arm64/kernel/insn.c | 56 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+)
arch/arm64/include/asm/insn.h +16 -0
@@ -67,6 +67,8 @@ enum aarch64_insn_imm_type {AARCH64_INSN_IMM_12 ,AARCH64_INSN_IMM_9 ,AARCH64_INSN_IMM_7 , +AARCH64_INSN_IMM_S , +AARCH64_INSN_IMM_R , AARCH64_INSN_IMM_MAX };
@@ -170,6 +172,12 @@ enum aarch64_insn_adsb_type { AARCH64_INSN_ADSB_SUB_SETFLAGS }; +enum aarch64_insn_bitfield_type { +AARCH64_INSN_BITFIELD_MOVE , +AARCH64_INSN_BITFIELD_MOVE_UNSIGNED , +AARCH64_INSN_BITFIELD_MOVE_SIGNED +}; +#define __AARCH64_INSN_FUNCS (abbr, mask, val) \ static __always_inline bool aarch64_insn_is_##abbr(u32 code) \ { return (code & (mask)) == (val); } \
@@ -186,6 +194,9 @@ __AARCH64_INSN_FUNCS(add_imm, 0x7F000000, 0x11000000) __AARCH64_INSN_FUNCS(adds_imm, 0x7F000000, 0x31000000) __AARCH64_INSN_FUNCS(sub_imm, 0x7F000000, 0x51000000) __AARCH64_INSN_FUNCS(subs_imm, 0x7F000000, 0x71000000) +__AARCH64_INSN_FUNCS(sbfm, 0x7F800000, 0x13000000) +__AARCH64_INSN_FUNCS(bfm, 0x7F800000, 0x33000000) +__AARCH64_INSN_FUNCS(ubfm, 0x7F800000, 0x53000000) __AARCH64_INSN_FUNCS(b, 0xFC000000, 0x14000000) __AARCH64_INSN_FUNCS(bl, 0xFC000000, 0x94000000)__AARCH64_INSN_FUNCS (cbz,0xFE000000 ,0x34000000 )
@@ -236,6 +247,11 @@ u32 aarch64_insn_gen_add_sub_imm(enum aarch64_insn_register dst,enum aarch64_insn_register src, int imm, enum aarch64_insn_variant variant, enum aarch64_insn_adsb_type type); +u32 aarch64_insn_gen_bitfield (enum aarch64_insn_register dst, +enum aarch64_insn_register src, +int immr,int imms, +enum aarch64_insn_variant variant, +enum aarch64_insn_bitfield_type type);bool aarch64_insn_hotpatch_safe (u32 old_insn,u32 new_insn);
arch/arm64/kernel/insn.c +56 -0
65 lines not on this page · raw message
Signature · 1 line
1.9.1
RFC PATCH v3 8/14 arm64: introduce aarch64_insn_gen_movewide()
In reply to Zi Shen LimIntroduce function to generate move wide (immediate) instructions.
--- arch/arm64/include/asm/insn.h | 13 +++++++++++++ arch/arm64/kernel/insn.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+)
arch/arm64/include/asm/insn.h +13 -0
@@ -172,6 +172,12 @@ enum aarch64_insn_adsb_type {AARCH64_INSN_ADSB_SUB_SETFLAGS }; +enum aarch64_insn_movewide_type { +AARCH64_INSN_MOVEWIDE_ZERO , +AARCH64_INSN_MOVEWIDE_KEEP , +AARCH64_INSN_MOVEWIDE_INVERSE +}; +enum aarch64_insn_bitfield_type {AARCH64_INSN_BITFIELD_MOVE ,AARCH64_INSN_BITFIELD_MOVE_UNSIGNED ,
@@ -194,9 +200,12 @@ __AARCH64_INSN_FUNCS(add_imm, 0x7F000000, 0x11000000) __AARCH64_INSN_FUNCS(adds_imm, 0x7F000000, 0x31000000) __AARCH64_INSN_FUNCS(sub_imm, 0x7F000000, 0x51000000) __AARCH64_INSN_FUNCS(subs_imm, 0x7F000000, 0x71000000) +__AARCH64_INSN_FUNCS(movn, 0x7F800000, 0x12800000) __AARCH64_INSN_FUNCS(sbfm, 0x7F800000, 0x13000000) __AARCH64_INSN_FUNCS(bfm, 0x7F800000, 0x33000000) +__AARCH64_INSN_FUNCS(movz, 0x7F800000, 0x52800000) __AARCH64_INSN_FUNCS(ubfm, 0x7F800000, 0x53000000) +__AARCH64_INSN_FUNCS(movk, 0x7F800000, 0x72800000) __AARCH64_INSN_FUNCS(b, 0xFC000000, 0x14000000) __AARCH64_INSN_FUNCS(bl, 0xFC000000, 0x94000000)__AARCH64_INSN_FUNCS (cbz,0xFE000000 ,0x34000000 )
@@ -252,6 +261,10 @@ u32 aarch64_insn_gen_bitfield(enum aarch64_insn_register dst, int immr, int imms, enum aarch64_insn_variant variant, enum aarch64_insn_bitfield_type type); +u32 aarch64_insn_gen_movewide (enum aarch64_insn_register dst, +int imm,int shift, +enum aarch64_insn_variant variant, +enum aarch64_insn_movewide_type type);bool aarch64_insn_hotpatch_safe (u32 old_insn,u32 new_insn);
arch/arm64/kernel/insn.c +43 -0
46 lines not on this page · raw message
Signature · 1 line
1.9.1
Re: [PATCH RFCv3 08/14] arm64: introduce aarch64_insn_gen_movewide()
Quoted 1 line · Zi Shen Lim
Introduce function to generate move wide (immediate) instructions.
[...]
Quoted 12 lines
arch/arm64/kernel/insn.c +12
-0+u32 aarch64_insn_gen_movewide (enum aarch64_insn_register dst, +int imm,int shift, +enum aarch64_insn_variant variant, +enum aarch64_insn_movewide_type type) +{ +u32 insn; + +switch (type) { +case AARCH64_INSN_MOVEWIDE_ZERO : + insn =aarch64_insn_get_movz_value (); +break ; +case AARCH64_INSN_MOVEWIDE_KEEP :
arch/arm64/kernel/insn.c u32 aarch64_insn_gen_bitfield(enum aarch64_insn_register dst, · line 678
Quoted 10 lines
arch/arm64/kernel/insn.c +10
-0+ insn =aarch64_insn_get_movk_value (); +break ; +case AARCH64_INSN_MOVEWIDE_INVERSE : + insn =aarch64_insn_get_movn_value (); +break ; +default : +BUG_ON (1 ); + } + +BUG_ON (imm <0 || imm >65535 );
Do this check with masking instead?
arch/arm64/kernel/insn.c u32 aarch64_insn_gen_bitfield(enum aarch64_insn_register dst, · line 687
Quoted 9 lines
arch/arm64/kernel/insn.c +9
-0+ +switch (variant) { +case AARCH64_INSN_VARIANT_32BIT : +BUG_ON (shift !=0 && shift !=16 ); +break ; +case AARCH64_INSN_VARIANT_64BIT : + insn |=BIT (31 ); +BUG_ON (shift !=0 && shift !=16 && shift !=32 && + shift !=48 );
Would be neater as a nested switch, perhaps? If you reorder the outer-switch, you could probably fall-through too and combine the shift checks.
Will
RE: [PATCH RFCv3 08/14] arm64: introduce aarch64_insn_gen_movewide()
From: Will Deacon
...
Quoted 3 lines
arch/arm64/kernel/insn.c u32 aarch64_insn_gen_bitfield(enum aarch64_insn_register dst, · line 678
Quoted 1 line
arch/arm64/kernel/insn.c +1
-0+BUG_ON (imm <0 || imm >65535 );Do this check with masking instead?
The compiler will convert that to a single unsigned comparison.
...
Quoted 2 lines
arch/arm64/kernel/insn.c u32 aarch64_insn_gen_bitfield(enum aarch64_insn_register dst, · line 687
Quoted 2 lines
arch/arm64/kernel/insn.c +2
-0+BUG_ON (shift !=0 && shift !=16 && shift !=32 && + shift !=48 );
OTOH I don't think it will convert that to:
BUG_ON(shift & ~48); David
Re: [PATCH RFCv3 08/14] arm64: introduce aarch64_insn_gen_movewide()
In reply to Will DeaconQuoted 21 lines · Will Deacon
Quoted 1 line · Zi Shen Lim
Introduce function to generate move wide (immediate) instructions.
[...]
Quoted 16 lines
arch/arm64/kernel/insn.c +16
-0+u32 aarch64_insn_gen_movewide (enum aarch64_insn_register dst, +int imm,int shift, +enum aarch64_insn_variant variant, +enum aarch64_insn_movewide_type type) +{ +u32 insn; + +switch (type) { +case AARCH64_INSN_MOVEWIDE_ZERO : + insn =aarch64_insn_get_movz_value (); +break ; +case AARCH64_INSN_MOVEWIDE_KEEP : + insn =aarch64_insn_get_movk_value (); +break ; +case AARCH64_INSN_MOVEWIDE_INVERSE : + insn =aarch64_insn_get_movn_value ();
Quoted 8 lines
arch/arm64/kernel/insn.c u32 aarch64_insn_gen_bitfield(enum aarch64_insn_register dst, · line 678
Quoted 6 lines
arch/arm64/kernel/insn.c +6
-0+break ; +default : +BUG_ON (1 ); + } + +BUG_ON (imm <0 || imm >65535 );Do this check with masking instead?
Ok, if you prefer, I can change it to:
BUG_ON(imm & ~GENMASK(15, 0));
Quoted 14 lines
arch/arm64/kernel/insn.c u32 aarch64_insn_gen_bitfield(enum aarch64_insn_register dst, · line 687
Quoted 10 lines
arch/arm64/kernel/insn.c +9
-0+ +switch (variant) { +case AARCH64_INSN_VARIANT_32BIT : +BUG_ON (shift !=0 && shift !=16 ); +break ; +case AARCH64_INSN_VARIANT_64BIT : + insn |=BIT (31 ); +BUG_ON (shift !=0 && shift !=16 && shift !=32 && + shift !=48 );Would be neater as a nested switch, perhaps? If you reorder the outer-switch, you could probably fall-through too and combine the shift checks.
Not sure I picture what you had in mind... I couldn't come up with a neater version with the properties you described.
The alternative I had was using masks instead of integer values, but one could argue that while neater, it could also be harder to read:
switch (variant) {
case AARCH64_INSN_VARIANT_32BIT:
BUG_ON(shift & ~BIT(4));
break;
case AARCH64_INSN_VARIANT_64BIT:
insn |= BIT(31);
BUG_ON(shift & ~GENMASK(5, 4));
...Quoted 2 lines
Will
Re: [PATCH RFCv3 08/14] arm64: introduce aarch64_insn_gen_movewide()
Quoted 31 lines · Zi Shen Lim
Quoted 21 lines · Will Deacon
Quoted 1 line · Zi Shen Lim
Introduce function to generate move wide (immediate) instructions.
[...]
Quoted 16 lines
arch/arm64/kernel/insn.c +16
-0+u32 aarch64_insn_gen_movewide (enum aarch64_insn_register dst, +int imm,int shift, +enum aarch64_insn_variant variant, +enum aarch64_insn_movewide_type type) +{ +u32 insn; + +switch (type) { +case AARCH64_INSN_MOVEWIDE_ZERO : + insn =aarch64_insn_get_movz_value (); +break ; +case AARCH64_INSN_MOVEWIDE_KEEP : + insn =aarch64_insn_get_movk_value (); +break ; +case AARCH64_INSN_MOVEWIDE_INVERSE : + insn =aarch64_insn_get_movn_value ();Quoted 8 lines
arch/arm64/kernel/insn.c u32 aarch64_insn_gen_bitfield(enum aarch64_insn_register dst, · line 678
Quoted 6 lines
arch/arm64/kernel/insn.c +6
-0+break ; +default : +BUG_ON (1 ); + } + +BUG_ON (imm <0 || imm >65535 );Do this check with masking instead?
Quoted 3 lines
Ok, if you prefer, I can change it to:
BUG_ON(imm & ~GENMASK(15, 0));
Sure, that or use a named constant for the upper-bound (SZ_64K - 1).
Quoted 19 lines
Quoted 4 lines
Quoted 4 lines
arch/arm64/kernel/insn.c +4
-0+switch (variant) { +case AARCH64_INSN_VARIANT_32BIT : +BUG_ON (shift !=0 && shift !=16 ); +break ;Quoted 8 lines
arch/arm64/kernel/insn.c u32 aarch64_insn_gen_bitfield(enum aarch64_insn_register dst, · line 687
Quoted 4 lines
arch/arm64/kernel/insn.c +4
-0+case AARCH64_INSN_VARIANT_64BIT : + insn |=BIT (31 ); +BUG_ON (shift !=0 && shift !=16 && shift !=32 && + shift !=48 );Would be neater as a nested switch, perhaps? If you reorder the outer-switch, you could probably fall-through too and combine the shift checks.
Not sure I picture what you had in mind... I couldn't come up with a neater version with the properties you described.
The alternative I had was using masks instead of integer values, but one could argue that while neater, it could also be harder to read:
Quoted 8 lines
switch (variant) {
case AARCH64_INSN_VARIANT_32BIT:
BUG_ON(shift & ~BIT(4));
break;
case AARCH64_INSN_VARIANT_64BIT:
insn |= BIT(31);
BUG_ON(shift & ~GENMASK(5, 4));
...I was thinking of using nested switches, but that doesn't fall out like I hoped. How about:
switch (variant) {
case AARCH64_INSN_VARIANT_64BIT:
BUG_ON(shift != 32 && shift != 48);
case AARCH64_INSN_VARIANT_32BIT:
BUG_ON(shift != 0 && shift != 16);
};?
Will
RE: [PATCH RFCv3 08/14] arm64: introduce aarch64_insn_gen_movewide()
From: Will Deacon
...
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
arch/arm64/kernel/insn.c u32 aarch64_insn_gen_bitfield(enum aarch64_insn_register dst, · line 678
Quoted 1 line
arch/arm64/kernel/insn.c +1
-0+BUG_ON (imm <0 || imm >65535 );Do this check with masking instead?
Ok, if you prefer, I can change it to:
BUG_ON(imm & ~GENMASK(15, 0));
Gah - then anyone reading the code has to look up another define. There isn't a prize for the most complicated method of defining a constant that can never change.
Quoted 1 line
Sure, that or use a named constant for the upper-bound (
SZ_64K- 1).
There is nothing wrong with the original code.
Maybe use 0xffff for those people (are there any) that don't know their powers of two.
These are strong constants, they aren't going to be changed to any other value, and it is unlikely that anyone will want to search for their uses.
I presume that SZ_64K is defined somewhere to 0x10000u. But IMHO using it (instead of the literal) doesn't make code any more readable.
David
Re: [PATCH RFCv3 08/14] arm64: introduce aarch64_insn_gen_movewide()
In reply to Will Deacon(resending this email in case the first one got caught in your spam filter. sorry.)
Quoted 4 lines · Will Deacon
Quoted 3 lines · Zi Shen Lim
Quoted 2 lines · Will Deacon
Quoted 1 line · Zi Shen Lim
Introduce function to generate move wide (immediate) instructions.
[...]
Quoted 28 lines
Quoted 19 lines
Quoted 9 lines
arch/arm64/kernel/insn.c u32 aarch64_insn_gen_bitfield(enum aarch64_insn_register dst, · line 687
Quoted 8 lines
arch/arm64/kernel/insn.c +8
-0+switch (variant) { +case AARCH64_INSN_VARIANT_32BIT : +BUG_ON (shift !=0 && shift !=16 ); +break ; +case AARCH64_INSN_VARIANT_64BIT : + insn |=BIT (31 ); +BUG_ON (shift !=0 && shift !=16 && shift !=32 && + shift !=48 );Quoted 3 lines
Would be neater as a nested switch, perhaps? If you reorder the outer-switch, you could probably fall-through too and combine the shift checks.
Not sure I picture what you had in mind... I couldn't come up with a neater version with the properties you described.
The alternative I had was using masks instead of integer values, but one could argue that while neater, it could also be harder to read:
Quoted 8 lines
switch (variant) { case AARCH64_INSN_VARIANT_32BIT: BUG_ON(shift & ~BIT(4)); break; case AARCH64_INSN_VARIANT_64BIT: insn |= BIT(31); BUG_ON(shift & ~GENMASK(5, 4)); ...
Quoted 6 lines
I was thinking of using nested switches, but that doesn't fall out like I hoped. How about:
switch (variant) { case AARCH64_INSN_VARIANT_64BIT: BUG_ON(shift != 32 && shift != 48);
Sorry this won't work. For example, on the valid case of shift==0, we'll barf right here - no fallthrough.
Shall we just leave the code as is? :)
Quoted 7 lines
case AARCH64_INSN_VARIANT_32BIT: BUG_ON(shift != 0 && shift != 16); };?
Will
Re: [PATCH RFCv3 08/14] arm64: introduce aarch64_insn_gen_movewide()
Quoted 40 lines · Z Lim
(resending this email in case the first one got caught in your spam filter. sorry.)
Quoted 4 lines · Will Deacon
Quoted 3 lines · Zi Shen Lim
Quoted 2 lines · Will Deacon
Quoted 1 line · Zi Shen Lim
Introduce function to generate move wide (immediate) instructions.
[...]
Quoted 31 lines
Quoted 19 lines
Quoted 9 lines
arch/arm64/kernel/insn.c u32 aarch64_insn_gen_bitfield(enum aarch64_insn_register dst, · line 687
Quoted 8 lines
arch/arm64/kernel/insn.c +8
-0+switch (variant) { +case AARCH64_INSN_VARIANT_32BIT : +BUG_ON (shift !=0 && shift !=16 ); +break ; +case AARCH64_INSN_VARIANT_64BIT : + insn |=BIT (31 ); +BUG_ON (shift !=0 && shift !=16 && shift !=32 && + shift !=48 );Quoted 3 lines
Would be neater as a nested switch, perhaps? If you reorder the outer-switch, you could probably fall-through too and combine the shift checks.
Not sure I picture what you had in mind... I couldn't come up with a neater version with the properties you described.
The alternative I had was using masks instead of integer values, but one could argue that while neater, it could also be harder to read:
Quoted 8 lines
switch (variant) { case AARCH64_INSN_VARIANT_32BIT: BUG_ON(shift & ~BIT(4)); break; case AARCH64_INSN_VARIANT_64BIT: insn |= BIT(31); BUG_ON(shift & ~GENMASK(5, 4)); ...I was thinking of using nested switches, but that doesn't fall out like I hoped. How about:
Quoted 8 lines
Quoted 3 lines
switch (variant) { case AARCH64_INSN_VARIANT_64BIT: BUG_ON(shift != 32 && shift != 48);Sorry this won't work. For example, on the valid case of shift==0, we'll barf right here - no fallthrough.
Shall we just leave the code as is? :)
Yeah, I'm an idiot ;)
Cheers,
Will
RFC PATCH v3 9/14 arm64: introduce aarch64_insn_gen_add_sub_shifted_reg()
In reply to Zi Shen LimIntroduce function to generate add/subtract (shifted register) instructions.
--- arch/arm64/include/asm/insn.h | 11 ++++++++++ arch/arm64/kernel/insn.c | 49 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+)
arch/arm64/include/asm/insn.h +11 -0
@@ -67,6 +67,7 @@ enum aarch64_insn_imm_type {AARCH64_INSN_IMM_12 ,AARCH64_INSN_IMM_9 ,AARCH64_INSN_IMM_7 , +AARCH64_INSN_IMM_6 ,AARCH64_INSN_IMM_S ,AARCH64_INSN_IMM_R ,AARCH64_INSN_IMM_MAX
@@ -206,6 +207,10 @@ __AARCH64_INSN_FUNCS(bfm, 0x7F800000, 0x33000000) __AARCH64_INSN_FUNCS(movz, 0x7F800000, 0x52800000) __AARCH64_INSN_FUNCS(ubfm, 0x7F800000, 0x53000000) __AARCH64_INSN_FUNCS(movk, 0x7F800000, 0x72800000) +__AARCH64_INSN_FUNCS(add, 0x7F200000, 0x0B000000) +__AARCH64_INSN_FUNCS(adds, 0x7F200000, 0x2B000000) +__AARCH64_INSN_FUNCS(sub, 0x7F200000, 0x4B000000) +__AARCH64_INSN_FUNCS(subs, 0x7F200000, 0x6B000000) __AARCH64_INSN_FUNCS(b, 0xFC000000, 0x14000000) __AARCH64_INSN_FUNCS(bl, 0xFC000000, 0x94000000)__AARCH64_INSN_FUNCS (cbz,0xFE000000 ,0x34000000 )
@@ -265,6 +270,12 @@ u32 aarch64_insn_gen_movewide(enum aarch64_insn_register dst, int imm, int shift, enum aarch64_insn_variant variant, enum aarch64_insn_movewide_type type); +u32 aarch64_insn_gen_add_sub_shifted_reg (enum aarch64_insn_register dst, +enum aarch64_insn_register src, +enum aarch64_insn_register reg, +int shift, +enum aarch64_insn_variant variant, +enum aarch64_insn_adsb_type type);bool aarch64_insn_hotpatch_safe (u32 old_insn,u32 new_insn);
arch/arm64/kernel/insn.c +49 -0
58 lines not on this page · raw message
Signature · 1 line
1.9.1
RFC PATCH v3 10/14 arm64: introduce aarch64_insn_gen_data1()
In reply to Zi Shen LimIntroduce function to generate data-processing (1 source) instructions.
--- arch/arm64/include/asm/insn.h | 13 +++++++++++++ arch/arm64/kernel/insn.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+)
arch/arm64/include/asm/insn.h +13 -0
@@ -185,6 +185,12 @@ enum aarch64_insn_bitfield_type { AARCH64_INSN_BITFIELD_MOVE_SIGNED }; +enum aarch64_insn_data1_type { +AARCH64_INSN_DATA1_REVERSE_16 , +AARCH64_INSN_DATA1_REVERSE_32 , +AARCH64_INSN_DATA1_REVERSE_64 , +}; +#define __AARCH64_INSN_FUNCS (abbr, mask, val) \ static __always_inline bool aarch64_insn_is_##abbr(u32 code) \ { return (code & (mask)) == (val); } \
@@ -211,6 +217,9 @@ __AARCH64_INSN_FUNCS(add, 0x7F200000, 0x0B000000) __AARCH64_INSN_FUNCS(adds, 0x7F200000, 0x2B000000) __AARCH64_INSN_FUNCS(sub, 0x7F200000, 0x4B000000) __AARCH64_INSN_FUNCS(subs, 0x7F200000, 0x6B000000) +__AARCH64_INSN_FUNCS(rev16, 0x7FFFFC00, 0x5AC00400) +__AARCH64_INSN_FUNCS(rev32, 0x7FFFFC00, 0x5AC00800) +__AARCH64_INSN_FUNCS(rev64, 0x7FFFFC00, 0x5AC00C00) __AARCH64_INSN_FUNCS(b, 0xFC000000, 0x14000000) __AARCH64_INSN_FUNCS(bl, 0xFC000000, 0x94000000)__AARCH64_INSN_FUNCS (cbz,0xFE000000 ,0x34000000 )
@@ -276,6 +285,10 @@ u32 aarch64_insn_gen_add_sub_shifted_reg(enum aarch64_insn_register dst,int shift, enum aarch64_insn_variant variant, enum aarch64_insn_adsb_type type); +u32 aarch64_insn_gen_data1 (enum aarch64_insn_register dst, +enum aarch64_insn_register src, +enum aarch64_insn_variant variant, +enum aarch64_insn_data1_type type);bool aarch64_insn_hotpatch_safe (u32 old_insn,u32 new_insn);
arch/arm64/kernel/insn.c +37 -0
@@ -745,3 +745,40 @@ u32 aarch64_insn_gen_add_sub_shifted_reg(enum aarch64_insn_register dst,return aarch64_insn_encode_immediate (AARCH64_INSN_IMM_6 , insn, shift); } + +u32 aarch64_insn_gen_data1 (enum aarch64_insn_register dst, +enum aarch64_insn_register src, +enum aarch64_insn_variant variant, +enum aarch64_insn_data1_type type) +{ +u32 insn; + +switch (type) { +case AARCH64_INSN_DATA1_REVERSE_16 : + insn =aarch64_insn_get_rev16_value (); +break ; +case AARCH64_INSN_DATA1_REVERSE_32 : + insn =aarch64_insn_get_rev32_value (); +break ; +case AARCH64_INSN_DATA1_REVERSE_64 : +BUG_ON (variant !=AARCH64_INSN_VARIANT_64BIT ); + insn =aarch64_insn_get_rev64_value (); +break ; +default : +BUG_ON (1 ); + } + +switch (variant) { +case AARCH64_INSN_VARIANT_32BIT : +break ; +case AARCH64_INSN_VARIANT_64BIT : + insn |=BIT (31 ); +break ; +default : +BUG_ON (1 ); + } + + insn =aarch64_insn_encode_register (AARCH64_INSN_REGTYPE_RD , insn, dst); + +return aarch64_insn_encode_register (AARCH64_INSN_REGTYPE_RN , insn, src); +}
Signature · 1 line
1.9.1
RFC PATCH v3 11/14 arm64: introduce aarch64_insn_gen_data2()
In reply to Zi Shen LimIntroduce function to generate data-processing (2 source) instructions.
--- arch/arm64/include/asm/insn.h | 20 ++++++++++++++++++ arch/arm64/kernel/insn.c | 48 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+)
arch/arm64/include/asm/insn.h +20 -0
@@ -191,6 +191,15 @@ enum aarch64_insn_data1_type { AARCH64_INSN_DATA1_REVERSE_64, }; +enum aarch64_insn_data2_type { +AARCH64_INSN_DATA2_UDIV , +AARCH64_INSN_DATA2_SDIV , +AARCH64_INSN_DATA2_LSLV , +AARCH64_INSN_DATA2_LSRV , +AARCH64_INSN_DATA2_ASRV , +AARCH64_INSN_DATA2_RORV , +}; +#define __AARCH64_INSN_FUNCS (abbr, mask, val) \ static __always_inline bool aarch64_insn_is_##abbr(u32 code) \ { return (code & (mask)) == (val); } \
@@ -217,6 +226,12 @@ __AARCH64_INSN_FUNCS(add, 0x7F200000, 0x0B000000) __AARCH64_INSN_FUNCS(adds, 0x7F200000, 0x2B000000) __AARCH64_INSN_FUNCS(sub, 0x7F200000, 0x4B000000) __AARCH64_INSN_FUNCS(subs, 0x7F200000, 0x6B000000) +__AARCH64_INSN_FUNCS(udiv, 0x7FE0FC00, 0x1AC00800) +__AARCH64_INSN_FUNCS(sdiv, 0x7FE0FC00, 0x1AC00C00) +__AARCH64_INSN_FUNCS(lslv, 0x7FE0FC00, 0x1AC02000) +__AARCH64_INSN_FUNCS(lsrv, 0x7FE0FC00, 0x1AC02400) +__AARCH64_INSN_FUNCS(asrv, 0x7FE0FC00, 0x1AC02800) +__AARCH64_INSN_FUNCS(rorv, 0x7FE0FC00, 0x1AC02C00) __AARCH64_INSN_FUNCS(rev16, 0x7FFFFC00, 0x5AC00400) __AARCH64_INSN_FUNCS(rev32, 0x7FFFFC00, 0x5AC00800)__AARCH64_INSN_FUNCS (rev64,0x7FFFFC00 ,0x5AC00C00 )
@@ -289,6 +304,11 @@ u32 aarch64_insn_gen_data1(enum aarch64_insn_register dst,enum aarch64_insn_register src, enum aarch64_insn_variant variant, enum aarch64_insn_data1_type type); +u32 aarch64_insn_gen_data2 (enum aarch64_insn_register dst, +enum aarch64_insn_register src, +enum aarch64_insn_register reg, +enum aarch64_insn_variant variant, +enum aarch64_insn_data2_type type);bool aarch64_insn_hotpatch_safe (u32 old_insn,u32 new_insn);
arch/arm64/kernel/insn.c +48 -0
51 lines not on this page · raw message
Signature · 1 line
1.9.1
RFC PATCH v3 12/14 arm64: introduce aarch64_insn_gen_data3()
In reply to Zi Shen LimIntroduce function to generate data-processing (3 source) instructions.
--- arch/arm64/include/asm/insn.h | 14 ++++++++++++++ arch/arm64/kernel/insn.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+)
arch/arm64/include/asm/insn.h +14 -0
@@ -79,6 +79,7 @@ enum aarch64_insn_register_type {AARCH64_INSN_REGTYPE_RT2 ,AARCH64_INSN_REGTYPE_RM ,AARCH64_INSN_REGTYPE_RD , + AARCH64_INSN_REGTYPE_RA, }; enum aarch64_insn_register {
@@ -200,6 +201,11 @@ enum aarch64_insn_data2_type { AARCH64_INSN_DATA2_RORV, }; +enum aarch64_insn_data3_type { +AARCH64_INSN_DATA3_MADD , +AARCH64_INSN_DATA3_MSUB , +}; +#define __AARCH64_INSN_FUNCS (abbr, mask, val) \ static __always_inline bool aarch64_insn_is_##abbr(u32 code) \ { return (code & (mask)) == (val); } \
@@ -226,6 +232,8 @@ __AARCH64_INSN_FUNCS(add, 0x7F200000, 0x0B000000) __AARCH64_INSN_FUNCS(adds, 0x7F200000, 0x2B000000) __AARCH64_INSN_FUNCS(sub, 0x7F200000, 0x4B000000) __AARCH64_INSN_FUNCS(subs, 0x7F200000, 0x6B000000) +__AARCH64_INSN_FUNCS(madd, 0x7FE08000, 0x1B000000) +__AARCH64_INSN_FUNCS(msub, 0x7FE08000, 0x1B008000) __AARCH64_INSN_FUNCS(udiv, 0x7FE0FC00, 0x1AC00800) __AARCH64_INSN_FUNCS(sdiv, 0x7FE0FC00, 0x1AC00C00)__AARCH64_INSN_FUNCS (lslv,0x7FE0FC00 ,0x1AC02000 )
@@ -309,6 +317,12 @@ u32 aarch64_insn_gen_data2(enum aarch64_insn_register dst,enum aarch64_insn_register reg, enum aarch64_insn_variant variant, enum aarch64_insn_data2_type type); +u32 aarch64_insn_gen_data3 (enum aarch64_insn_register dst, +enum aarch64_insn_register src, +enum aarch64_insn_register reg1, +enum aarch64_insn_register reg2, +enum aarch64_insn_variant variant, +enum aarch64_insn_data3_type type);bool aarch64_insn_hotpatch_safe (u32 old_insn,u32 new_insn);
arch/arm64/kernel/insn.c +42 -0
51 lines not on this page · raw message
Signature · 1 line
1.9.1
RFC PATCH v3 13/14 arm64: introduce aarch64_insn_gen_logical_shifted_reg()
In reply to Zi Shen LimIntroduce function to generate logical (shifted register) instructions.
--- arch/arm64/include/asm/insn.h | 25 ++++++++++++++++++ arch/arm64/kernel/insn.c | 60 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+)
arch/arm64/include/asm/insn.h +25 -0
@@ -206,6 +206,17 @@ enum aarch64_insn_data3_type { AARCH64_INSN_DATA3_MSUB, }; +enum aarch64_insn_logic_type { +AARCH64_INSN_LOGIC_AND , +AARCH64_INSN_LOGIC_BIC , +AARCH64_INSN_LOGIC_ORR , +AARCH64_INSN_LOGIC_ORN , +AARCH64_INSN_LOGIC_EOR , +AARCH64_INSN_LOGIC_EON , +AARCH64_INSN_LOGIC_AND_SETFLAGS , +AARCH64_INSN_LOGIC_BIC_SETFLAGS +}; +#define __AARCH64_INSN_FUNCS (abbr, mask, val) \ static __always_inline bool aarch64_insn_is_##abbr(u32 code) \ { return (code & (mask)) == (val); } \
@@ -243,6 +254,14 @@ __AARCH64_INSN_FUNCS(rorv, 0x7FE0FC00, 0x1AC02C00) __AARCH64_INSN_FUNCS(rev16, 0x7FFFFC00, 0x5AC00400) __AARCH64_INSN_FUNCS(rev32, 0x7FFFFC00, 0x5AC00800) __AARCH64_INSN_FUNCS(rev64, 0x7FFFFC00, 0x5AC00C00) +__AARCH64_INSN_FUNCS(and, 0x7F200000, 0x0A000000) +__AARCH64_INSN_FUNCS(bic, 0x7F200000, 0x0A200000) +__AARCH64_INSN_FUNCS(orr, 0x7F200000, 0x2A000000) +__AARCH64_INSN_FUNCS(orn, 0x7F200000, 0x2A200000) +__AARCH64_INSN_FUNCS(eor, 0x7F200000, 0x4A000000) +__AARCH64_INSN_FUNCS(eon, 0x7F200000, 0x4A200000) +__AARCH64_INSN_FUNCS(ands, 0x7F200000, 0x6A000000) +__AARCH64_INSN_FUNCS(bics, 0x7F200000, 0x6A200000) __AARCH64_INSN_FUNCS(b, 0xFC000000, 0x14000000) __AARCH64_INSN_FUNCS(bl, 0xFC000000, 0x94000000)__AARCH64_INSN_FUNCS (cbz,0xFE000000 ,0x34000000 )
@@ -323,6 +342,12 @@ u32 aarch64_insn_gen_data3(enum aarch64_insn_register dst,enum aarch64_insn_register reg2, enum aarch64_insn_variant variant, enum aarch64_insn_data3_type type); +u32 aarch64_insn_gen_logical_shifted_reg (enum aarch64_insn_register dst, +enum aarch64_insn_register src, +enum aarch64_insn_register reg, +int shift, +enum aarch64_insn_variant variant, +enum aarch64_insn_logic_type type);bool aarch64_insn_hotpatch_safe (u32 old_insn,u32 new_insn);
arch/arm64/kernel/insn.c +60 -0
63 lines not on this page · raw message
Signature · 1 line
1.9.1
RFC PATCH v3 14/14 arm64: eBPF JIT compiler
In reply to Zi Shen LimThe JIT compiler emits A64 instructions. It supports eBPF only. Legacy BPF is supported thanks to conversion by BPF core.
JIT is enabled in the same way as for other architectures:
echo 1 > /proc/sys/net/core/bpf_jit_enable
Or for additional compiler output:
echo 2 > /proc/sys/net/core/bpf_jit_enable
See Documentation/networking/filter.txt for more information.
The implementation passes all 57 tests in lib/test_bpf.c on ARMv8 Foundation Model :)
---RFCv2->RFCv3:
- clarify 16B stack alignment requirement - I missed one reference
- fixed a couple checks for immediate bits
- make bpf_jit.h checkpatch clean
- remove stale DW case in LD_IND and LD_ABS (good catch by Alexei)
- add Alexei's Acked-by
- rebase onto net-next
Also, per discussion with Will, consolidated bpf_jit.h into arch/arm64/.../insn.{c,h}:
- instruction encoding stuff moved into arch/arm64/kernel/insn.c
- bpf_jit.h uses arch/arm64/include/asm/insn.h
RFCv1->RFCv2:
Addressed review comments from Alexei:
- use core-$(CONFIG_NET)
- use GENMASK
- lower-case function names in header file
- drop LD_ABS+DW and LD_IND+DW, which do not exist in eBPF yet
- use pr_xxx_once() to prevent spamming logs
- clarify 16B stack alignment requirement
- drop usage of EMIT macro which was saving just one argument, turns out having additional argument wasn't too much of an eyesore
Also, per discussion with Alexei, and additional suggestion from Daniel:
- moved load_pointer() from net/core/filter.c into filter.h as bpf_load_pointer()
which is done as a separate preparatory patch. [1]
[1] http://patchwork.ozlabs.org/patch/366906/
NOTES:
* The preparatory patch [1] has been merged into net-next 9f12fbe603f7 ("net: filter: move load_pointer() into filter.h").
* This patch applies on top of net-next @ 0854a7f13206 ("Merge branch 'amd811e-cleanups'")
* bpf_jit_comp.c and bpf_jit.h is checkpatch clean.
* The following sparse warning is not applicable:
warning: symbol 'bpf_jit_enable' was not declared. Should it be static?
FUTURE WORK:
1. Implement remaining classes of eBPF instructions: ST|MEM, STX|XADD which currently do not have corresponding test cases in test_bpf.
2. Further compiler optimization, such as optimization for small immediates.
--- Documentation/networking/filter.txt | 2 +- arch/arm64/Kconfig | 1 + arch/arm64/Makefile | 1 + arch/arm64/net/Makefile | 4 + arch/arm64/net/bpf_jit.h | 169 +++++++++ arch/arm64/net/bpf_jit_comp.c | 677 ++++++++++++++++++++++++++++++++++++ 6 files changed, 853 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/net/Makefile create mode 100644 arch/arm64/net/bpf_jit.h create mode 100644 arch/arm64/net/bpf_jit_comp.c
Documentation/networking/filter.txt +1 -1
@@ -462,7 +462,7 @@ JIT compiler ------------ The Linux kernel has a built-in BPF JIT compiler for x86_64, SPARC, PowerPC, -ARM and s390 and can be enabled through CONFIG_BPF_JIT. The JIT compiler is +ARM, ARM64 and s390 and can be enabled through CONFIG_BPF_JIT. The JIT compiler is transparently invoked for each attached filter from user space or for internal kernel users if it has been previously enabled by root:
arch/arm64/Kconfig +1 -0
@@ -32,6 +32,7 @@ config ARM64 select HAVE_ARCH_KGDB select HAVE_ARCH_TRACEHOOK select HAVE_C_RECORDMCOUNT + select HAVE_BPF_JIT select HAVE_DEBUG_BUGVERBOSE select HAVE_DEBUG_KMEMLEAK select HAVE_DMA_API_DEBUG
arch/arm64/Makefile +1 -0
@@ -43,6 +43,7 @@ TEXT_OFFSET := 0x00080000 export TEXT_OFFSET GZFLAGS core-y += arch/arm64/kernel/ arch/arm64/mm/ +core-$(CONFIG_NET) += arch/arm64/net/ core-$(CONFIG_KVM) += arch/arm64/kvm/ core-$(CONFIG_XEN) += arch/arm64/xen/ core-$(CONFIG_CRYPTO) += arch/arm64/crypto/
arch/arm64/net/Makefile new file +4 -0
@@ -0,0 +1,4 @@ +# +# ARM64 networking code +# +obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o
arch/arm64/net/bpf_jit.h new file +169 -0
169 lines not on this page · raw message
arch/arm64/net/bpf_jit_comp.c new file +677 -0
677 lines not on this page · raw message
Signature · 1 line
1.9.1
Quoted 4 lines · Zi Shen Lim
This series implements eBPF JIT compiler for arm64. See [14/14] for change log.
Patches [1-13/14] implement code generation functions.
Nice work, I'll take a look. Thanks!
Will
Quoted 7 lines · Will Deacon
Quoted 4 lines · Zi Shen Lim
This series implements eBPF JIT compiler for arm64. See [14/14] for change log.
Patches [1-13/14] implement code generation functions.
Nice work, I'll take a look. Thanks!
... and it all looks pretty damn good to me. I gave you a few in-line comments but, rather than repeat myself for each patch, my main two gripes were:
- The use of BUG_ON all over the place
- Explicit limit checks which could be done with masks
If you address those, this stuff looks pretty much ready to go from my perspective. Do you have a branch somewhere I can play with please?
Cheers,
Will
Quoted 10 lines · Will Deacon
Quoted 7 lines · Will Deacon
Quoted 4 lines · Zi Shen Lim
This series implements eBPF JIT compiler for arm64. See [14/14] for change log.
Patches [1-13/14] implement code generation functions.
Nice work, I'll take a look. Thanks!
... and it all looks pretty damn good to me. I gave you a few in-line
Thanks :)
Quoted 4 lines
comments but, rather than repeat myself for each patch, my main two gripes were:
- The use of
BUG_ONall over the place
I responded to your comment about BUG_ON in [1/14].
Quoted 2 lines
- Explicit limit checks which could be done with masks
I responded to your comment about this in [8/14]. I can go ahead and make changes throughout if you prefer.
Quoted 3 lines
If you address those, this stuff looks pretty much ready to go from my perspective. Do you have a branch somewhere I can play with please?
Just pushed it to:
https://github.com/zlim/linux.git arm64/bpf
which is the same as this RFCv3 series, i.e. I haven't rebased it on latest net-next.
Thanks so much for your time and attention!
Let me know how it goes for you.
I can post this series (dropping RFC) once we finalized the above.
Cheers,
z