@@ -167,25 +167,37 @@ static void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)
167
167
168
168
static void bpf_jit_emit_func_call (u32 * image , struct codegen_context * ctx , u64 func )
169
169
{
170
+ unsigned int i , ctx_idx = ctx -> idx ;
171
+
172
+ /* Load function address into r12 */
173
+ PPC_LI64 (12 , func );
174
+
175
+ /* For bpf-to-bpf function calls, the callee's address is unknown
176
+ * until the last extra pass. As seen above, we use PPC_LI64() to
177
+ * load the callee's address, but this may optimize the number of
178
+ * instructions required based on the nature of the address.
179
+ *
180
+ * Since we don't want the number of instructions emitted to change,
181
+ * we pad the optimized PPC_LI64() call with NOPs to guarantee that
182
+ * we always have a five-instruction sequence, which is the maximum
183
+ * that PPC_LI64() can emit.
184
+ */
185
+ for (i = ctx -> idx - ctx_idx ; i < 5 ; i ++ )
186
+ PPC_NOP ();
187
+
170
188
#ifdef PPC64_ELF_ABI_v1
171
- /* func points to the function descriptor */
172
- PPC_LI64 (b2p [TMP_REG_2 ], func );
173
- /* Load actual entry point from function descriptor */
174
- PPC_BPF_LL (b2p [TMP_REG_1 ], b2p [TMP_REG_2 ], 0 );
175
- /* ... and move it to LR */
176
- PPC_MTLR (b2p [TMP_REG_1 ]);
177
189
/*
178
190
* Load TOC from function descriptor at offset 8.
179
191
* We can clobber r2 since we get called through a
180
192
* function pointer (so caller will save/restore r2)
181
193
* and since we don't use a TOC ourself.
182
194
*/
183
- PPC_BPF_LL (2 , b2p [TMP_REG_2 ], 8 );
184
- #else
185
- /* We can clobber r12 */
186
- PPC_FUNC_ADDR (12 , func );
187
- PPC_MTLR (12 );
195
+ PPC_BPF_LL (2 , 12 , 8 );
196
+ /* Load actual entry point from function descriptor */
197
+ PPC_BPF_LL (12 , 12 , 0 );
188
198
#endif
199
+
200
+ PPC_MTLR (12 );
189
201
PPC_BLRL ();
190
202
}
191
203
@@ -256,7 +268,7 @@ static void bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32
256
268
/* Assemble the body code between the prologue & epilogue */
257
269
static int bpf_jit_build_body (struct bpf_prog * fp , u32 * image ,
258
270
struct codegen_context * ctx ,
259
- u32 * addrs )
271
+ u32 * addrs , bool extra_pass )
260
272
{
261
273
const struct bpf_insn * insn = fp -> insnsi ;
262
274
int flen = fp -> len ;
@@ -712,11 +724,25 @@ static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
712
724
break ;
713
725
714
726
/*
715
- * Call kernel helper
727
+ * Call kernel helper or bpf function
716
728
*/
717
729
case BPF_JMP | BPF_CALL :
718
730
ctx -> seen |= SEEN_FUNC ;
719
- func = (u8 * ) __bpf_call_base + imm ;
731
+
732
+ /* bpf function call */
733
+ if (insn [i ].src_reg == BPF_PSEUDO_CALL )
734
+ if (!extra_pass )
735
+ func = NULL ;
736
+ else if (fp -> aux -> func && off < fp -> aux -> func_cnt )
737
+ /* use the subprog id from the off
738
+ * field to lookup the callee address
739
+ */
740
+ func = (u8 * ) fp -> aux -> func [off ]-> bpf_func ;
741
+ else
742
+ return - EINVAL ;
743
+ /* kernel helper call */
744
+ else
745
+ func = (u8 * ) __bpf_call_base + imm ;
720
746
721
747
bpf_jit_emit_func_call (image , ctx , (u64 )func );
722
748
@@ -864,20 +890,30 @@ static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
864
890
return 0 ;
865
891
}
866
892
893
+ struct powerpc64_jit_data {
894
+ struct bpf_binary_header * header ;
895
+ u32 * addrs ;
896
+ u8 * image ;
897
+ u32 proglen ;
898
+ struct codegen_context ctx ;
899
+ };
900
+
867
901
struct bpf_prog * bpf_int_jit_compile (struct bpf_prog * fp )
868
902
{
869
903
u32 proglen ;
870
904
u32 alloclen ;
871
905
u8 * image = NULL ;
872
906
u32 * code_base ;
873
907
u32 * addrs ;
908
+ struct powerpc64_jit_data * jit_data ;
874
909
struct codegen_context cgctx ;
875
910
int pass ;
876
911
int flen ;
877
912
struct bpf_binary_header * bpf_hdr ;
878
913
struct bpf_prog * org_fp = fp ;
879
914
struct bpf_prog * tmp_fp ;
880
915
bool bpf_blinded = false;
916
+ bool extra_pass = false;
881
917
882
918
if (!fp -> jit_requested )
883
919
return org_fp ;
@@ -891,11 +927,32 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
891
927
fp = tmp_fp ;
892
928
}
893
929
930
+ jit_data = fp -> aux -> jit_data ;
931
+ if (!jit_data ) {
932
+ jit_data = kzalloc (sizeof (* jit_data ), GFP_KERNEL );
933
+ if (!jit_data ) {
934
+ fp = org_fp ;
935
+ goto out ;
936
+ }
937
+ fp -> aux -> jit_data = jit_data ;
938
+ }
939
+
894
940
flen = fp -> len ;
941
+ addrs = jit_data -> addrs ;
942
+ if (addrs ) {
943
+ cgctx = jit_data -> ctx ;
944
+ image = jit_data -> image ;
945
+ bpf_hdr = jit_data -> header ;
946
+ proglen = jit_data -> proglen ;
947
+ alloclen = proglen + FUNCTION_DESCR_SIZE ;
948
+ extra_pass = true;
949
+ goto skip_init_ctx ;
950
+ }
951
+
895
952
addrs = kzalloc ((flen + 1 ) * sizeof (* addrs ), GFP_KERNEL );
896
953
if (addrs == NULL ) {
897
954
fp = org_fp ;
898
- goto out ;
955
+ goto out_addrs ;
899
956
}
900
957
901
958
memset (& cgctx , 0 , sizeof (struct codegen_context ));
@@ -904,10 +961,10 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
904
961
cgctx .stack_size = round_up (fp -> aux -> stack_depth , 16 );
905
962
906
963
/* Scouting faux-generate pass 0 */
907
- if (bpf_jit_build_body (fp , 0 , & cgctx , addrs )) {
964
+ if (bpf_jit_build_body (fp , 0 , & cgctx , addrs , false )) {
908
965
/* We hit something illegal or unsupported. */
909
966
fp = org_fp ;
910
- goto out ;
967
+ goto out_addrs ;
911
968
}
912
969
913
970
/*
@@ -925,17 +982,18 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
925
982
bpf_jit_fill_ill_insns );
926
983
if (!bpf_hdr ) {
927
984
fp = org_fp ;
928
- goto out ;
985
+ goto out_addrs ;
929
986
}
930
987
988
+ skip_init_ctx :
931
989
code_base = (u32 * )(image + FUNCTION_DESCR_SIZE );
932
990
933
991
/* Code generation passes 1-2 */
934
992
for (pass = 1 ; pass < 3 ; pass ++ ) {
935
993
/* Now build the prologue, body code & epilogue for real. */
936
994
cgctx .idx = 0 ;
937
995
bpf_jit_build_prologue (code_base , & cgctx );
938
- bpf_jit_build_body (fp , code_base , & cgctx , addrs );
996
+ bpf_jit_build_body (fp , code_base , & cgctx , addrs , extra_pass );
939
997
bpf_jit_build_epilogue (code_base , & cgctx );
940
998
941
999
if (bpf_jit_enable > 1 )
@@ -961,10 +1019,20 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
961
1019
fp -> jited_len = alloclen ;
962
1020
963
1021
bpf_flush_icache (bpf_hdr , (u8 * )bpf_hdr + (bpf_hdr -> pages * PAGE_SIZE ));
1022
+ if (!fp -> is_func || extra_pass ) {
1023
+ out_addrs :
1024
+ kfree (addrs );
1025
+ kfree (jit_data );
1026
+ fp -> aux -> jit_data = NULL ;
1027
+ } else {
1028
+ jit_data -> addrs = addrs ;
1029
+ jit_data -> ctx = cgctx ;
1030
+ jit_data -> proglen = proglen ;
1031
+ jit_data -> image = image ;
1032
+ jit_data -> header = bpf_hdr ;
1033
+ }
964
1034
965
1035
out :
966
- kfree (addrs );
967
-
968
1036
if (bpf_blinded )
969
1037
bpf_jit_prog_release_other (fp , fp == org_fp ? tmp_fp : org_fp );
970
1038
0 commit comments