2121#include <linux/bpf.h>
2222#include <linux/filter.h>
2323#include <linux/printk.h>
24- #include <linux/skbuff.h>
2524#include <linux/slab.h>
2625
2726#include <asm/byteorder.h>
@@ -80,23 +79,66 @@ static inline void emit(const u32 insn, struct jit_ctx *ctx)
8079 ctx -> idx ++ ;
8180}
8281
82+ static inline void emit_a64_mov_i (const int is64 , const int reg ,
83+ const s32 val , struct jit_ctx * ctx )
84+ {
85+ u16 hi = val >> 16 ;
86+ u16 lo = val & 0xffff ;
87+
88+ if (hi & 0x8000 ) {
89+ if (hi == 0xffff ) {
90+ emit (A64_MOVN (is64 , reg , (u16 )~lo , 0 ), ctx );
91+ } else {
92+ emit (A64_MOVN (is64 , reg , (u16 )~hi , 16 ), ctx );
93+ if (lo != 0xffff )
94+ emit (A64_MOVK (is64 , reg , lo , 0 ), ctx );
95+ }
96+ } else {
97+ emit (A64_MOVZ (is64 , reg , lo , 0 ), ctx );
98+ if (hi )
99+ emit (A64_MOVK (is64 , reg , hi , 16 ), ctx );
100+ }
101+ }
102+
103+ static int i64_i16_blocks (const u64 val , bool inverse )
104+ {
105+ return (((val >> 0 ) & 0xffff ) != (inverse ? 0xffff : 0x0000 )) +
106+ (((val >> 16 ) & 0xffff ) != (inverse ? 0xffff : 0x0000 )) +
107+ (((val >> 32 ) & 0xffff ) != (inverse ? 0xffff : 0x0000 )) +
108+ (((val >> 48 ) & 0xffff ) != (inverse ? 0xffff : 0x0000 ));
109+ }
110+
83111static inline void emit_a64_mov_i64 (const int reg , const u64 val ,
84112 struct jit_ctx * ctx )
85113{
86- u64 tmp = val ;
87- int shift = 0 ;
88-
89- emit (A64_MOVZ (1 , reg , tmp & 0xffff , shift ), ctx );
90- tmp >>= 16 ;
91- shift += 16 ;
92- while (tmp ) {
93- if (tmp & 0xffff )
94- emit (A64_MOVK (1 , reg , tmp & 0xffff , shift ), ctx );
95- tmp >>= 16 ;
96- shift += 16 ;
114+ u64 nrm_tmp = val , rev_tmp = ~val ;
115+ bool inverse ;
116+ int shift ;
117+
118+ if (!(nrm_tmp >> 32 ))
119+ return emit_a64_mov_i (0 , reg , (u32 )val , ctx );
120+
121+ inverse = i64_i16_blocks (nrm_tmp , true) < i64_i16_blocks (nrm_tmp , false);
122+ shift = max (round_down ((inverse ? (fls64 (rev_tmp ) - 1 ) :
123+ (fls64 (nrm_tmp ) - 1 )), 16 ), 0 );
124+ if (inverse )
125+ emit (A64_MOVN (1 , reg , (rev_tmp >> shift ) & 0xffff , shift ), ctx );
126+ else
127+ emit (A64_MOVZ (1 , reg , (nrm_tmp >> shift ) & 0xffff , shift ), ctx );
128+ shift -= 16 ;
129+ while (shift >= 0 ) {
130+ if (((nrm_tmp >> shift ) & 0xffff ) != (inverse ? 0xffff : 0x0000 ))
131+ emit (A64_MOVK (1 , reg , (nrm_tmp >> shift ) & 0xffff , shift ), ctx );
132+ shift -= 16 ;
97133 }
98134}
99135
136+ /*
137+ * This is an unoptimized 64 immediate emission used for BPF to BPF call
138+ * addresses. It will always do a full 64 bit decomposition as otherwise
139+ * more complexity in the last extra pass is required since we previously
140+ * reserved 4 instructions for the address.
141+ */
100142static inline void emit_addr_mov_i64 (const int reg , const u64 val ,
101143 struct jit_ctx * ctx )
102144{
@@ -111,26 +153,6 @@ static inline void emit_addr_mov_i64(const int reg, const u64 val,
111153 }
112154}
113155
114- static inline void emit_a64_mov_i (const int is64 , const int reg ,
115- const s32 val , struct jit_ctx * ctx )
116- {
117- u16 hi = val >> 16 ;
118- u16 lo = val & 0xffff ;
119-
120- if (hi & 0x8000 ) {
121- if (hi == 0xffff ) {
122- emit (A64_MOVN (is64 , reg , (u16 )~lo , 0 ), ctx );
123- } else {
124- emit (A64_MOVN (is64 , reg , (u16 )~hi , 16 ), ctx );
125- emit (A64_MOVK (is64 , reg , lo , 0 ), ctx );
126- }
127- } else {
128- emit (A64_MOVZ (is64 , reg , lo , 0 ), ctx );
129- if (hi )
130- emit (A64_MOVK (is64 , reg , hi , 16 ), ctx );
131- }
132- }
133-
134156static inline int bpf2a64_offset (int bpf_to , int bpf_from ,
135157 const struct jit_ctx * ctx )
136158{
@@ -163,7 +185,7 @@ static inline int epilogue_offset(const struct jit_ctx *ctx)
163185/* Tail call offset to jump into */
164186#define PROLOGUE_OFFSET 7
165187
166- static int build_prologue (struct jit_ctx * ctx )
188+ static int build_prologue (struct jit_ctx * ctx , bool ebpf_from_cbpf )
167189{
168190 const struct bpf_prog * prog = ctx -> prog ;
169191 const u8 r6 = bpf2a64 [BPF_REG_6 ];
@@ -188,7 +210,7 @@ static int build_prologue(struct jit_ctx *ctx)
188210 * | ... | BPF prog stack
189211 * | |
190212 * +-----+ <= (BPF_FP - prog->aux->stack_depth)
191- * |RSVD | JIT scratchpad
213+ * |RSVD | padding
192214 * current A64_SP => +-----+ <= (BPF_FP - ctx->stack_size)
193215 * | |
194216 * | ... | Function call stack
@@ -210,19 +232,19 @@ static int build_prologue(struct jit_ctx *ctx)
210232 /* Set up BPF prog stack base register */
211233 emit (A64_MOV (1 , fp , A64_SP ), ctx );
212234
213- /* Initialize tail_call_cnt */
214- emit (A64_MOVZ (1 , tcc , 0 , 0 ), ctx );
235+ if (!ebpf_from_cbpf ) {
236+ /* Initialize tail_call_cnt */
237+ emit (A64_MOVZ (1 , tcc , 0 , 0 ), ctx );
215238
216- cur_offset = ctx -> idx - idx0 ;
217- if (cur_offset != PROLOGUE_OFFSET ) {
218- pr_err_once ("PROLOGUE_OFFSET = %d, expected %d!\n" ,
219- cur_offset , PROLOGUE_OFFSET );
220- return -1 ;
239+ cur_offset = ctx -> idx - idx0 ;
240+ if (cur_offset != PROLOGUE_OFFSET ) {
241+ pr_err_once ("PROLOGUE_OFFSET = %d, expected %d!\n" ,
242+ cur_offset , PROLOGUE_OFFSET );
243+ return -1 ;
244+ }
221245 }
222246
223- /* 4 byte extra for skb_copy_bits buffer */
224- ctx -> stack_size = prog -> aux -> stack_depth + 4 ;
225- ctx -> stack_size = STACK_ALIGN (ctx -> stack_size );
247+ ctx -> stack_size = STACK_ALIGN (prog -> aux -> stack_depth );
226248
227249 /* Set up function call stack */
228250 emit (A64_SUB_I (1 , A64_SP , A64_SP , ctx -> stack_size ), ctx );
@@ -786,6 +808,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
786808 struct bpf_prog * tmp , * orig_prog = prog ;
787809 struct bpf_binary_header * header ;
788810 struct arm64_jit_data * jit_data ;
811+ bool was_classic = bpf_prog_was_classic (prog );
789812 bool tmp_blinded = false;
790813 bool extra_pass = false;
791814 struct jit_ctx ctx ;
@@ -840,7 +863,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
840863 goto out_off ;
841864 }
842865
843- if (build_prologue (& ctx )) {
866+ if (build_prologue (& ctx , was_classic )) {
844867 prog = orig_prog ;
845868 goto out_off ;
846869 }
@@ -863,7 +886,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
863886skip_init_ctx :
864887 ctx .idx = 0 ;
865888
866- build_prologue (& ctx );
889+ build_prologue (& ctx , was_classic );
867890
868891 if (build_body (& ctx )) {
869892 bpf_jit_binary_free (header );
0 commit comments