2525 * with our redzone usage.
2626 *
2727 * [ prev sp ] <-------------
28- * [ nv gpr save area ] 5 *8 |
28+ * [ nv gpr save area ] 6 *8 |
2929 * [ tail_call_cnt ] 8 |
30- * [ local_tmp_var ] 16 |
30+ * [ local_tmp_var ] 24 |
3131 * fp (r31) --> [ ebpf stack space ] upto 512 |
3232 * [ frame header ] 32/112 |
3333 * sp (r1) ---> [ stack pointer ] --------------
3434 */
3535
3636/* for gpr non volatile registers BPG_REG_6 to 10 */
37- #define BPF_PPC_STACK_SAVE (5 *8)
37+ #define BPF_PPC_STACK_SAVE (6 *8)
3838/* for bpf JIT code internal usage */
39- #define BPF_PPC_STACK_LOCALS 24
39+ #define BPF_PPC_STACK_LOCALS 32
4040/* stack frame excluding BPF stack, ensure this is quadword aligned */
4141#define BPF_PPC_STACKFRAME (STACK_FRAME_MIN_SIZE + \
4242 BPF_PPC_STACK_LOCALS + BPF_PPC_STACK_SAVE)
4343
4444/* BPF register usage */
4545#define TMP_REG_1 (MAX_BPF_JIT_REG + 0)
4646#define TMP_REG_2 (MAX_BPF_JIT_REG + 1)
47+ #define ARENA_VM_START (MAX_BPF_JIT_REG + 2)
4748
4849/* BPF to ppc register mappings */
4950void bpf_jit_init_reg_mapping (struct codegen_context * ctx )
@@ -67,10 +68,12 @@ void bpf_jit_init_reg_mapping(struct codegen_context *ctx)
6768 ctx -> b2p [BPF_REG_AX ] = _R12 ;
6869 ctx -> b2p [TMP_REG_1 ] = _R9 ;
6970 ctx -> b2p [TMP_REG_2 ] = _R10 ;
71+ /* non volatile register for kern_vm_start address */
72+ ctx -> b2p [ARENA_VM_START ] = _R26 ;
7073}
7174
72- /* PPC NVR range -- update this if we ever use NVRs below r27 */
73- #define BPF_PPC_NVR_MIN _R27
75+ /* PPC NVR range -- update this if we ever use NVRs below r26 */
76+ #define BPF_PPC_NVR_MIN _R26
7477
7578static inline bool bpf_has_stack_frame (struct codegen_context * ctx )
7679{
@@ -89,22 +92,22 @@ static inline bool bpf_has_stack_frame(struct codegen_context *ctx)
8992 * [ prev sp ] <-------------
9093 * [ ... ] |
9194 * sp (r1) ---> [ stack pointer ] --------------
92- * [ nv gpr save area ] 5 *8
95+ * [ nv gpr save area ] 6 *8
9396 * [ tail_call_cnt ] 8
94- * [ local_tmp_var ] 16
97+ * [ local_tmp_var ] 24
9598 * [ unused red zone ] 224
9699 */
97100static int bpf_jit_stack_local (struct codegen_context * ctx )
98101{
99102 if (bpf_has_stack_frame (ctx ))
100103 return STACK_FRAME_MIN_SIZE + ctx -> stack_size ;
101104 else
102- return - (BPF_PPC_STACK_SAVE + 24 );
105+ return - (BPF_PPC_STACK_SAVE + 32 );
103106}
104107
105108static int bpf_jit_stack_tailcallcnt (struct codegen_context * ctx )
106109{
107- return bpf_jit_stack_local (ctx ) + 16 ;
110+ return bpf_jit_stack_local (ctx ) + 24 ;
108111}
109112
110113static int bpf_jit_stack_offsetof (struct codegen_context * ctx , int reg )
@@ -170,10 +173,17 @@ void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx)
170173 if (bpf_is_seen_register (ctx , bpf_to_ppc (i )))
171174 EMIT (PPC_RAW_STD (bpf_to_ppc (i ), _R1 , bpf_jit_stack_offsetof (ctx , bpf_to_ppc (i ))));
172175
176+ if (ctx -> arena_vm_start )
177+ EMIT (PPC_RAW_STD (bpf_to_ppc (ARENA_VM_START ), _R1 ,
178+ bpf_jit_stack_offsetof (ctx , bpf_to_ppc (ARENA_VM_START ))));
179+
173180 /* Setup frame pointer to point to the bpf stack area */
174181 if (bpf_is_seen_register (ctx , bpf_to_ppc (BPF_REG_FP )))
175182 EMIT (PPC_RAW_ADDI (bpf_to_ppc (BPF_REG_FP ), _R1 ,
176183 STACK_FRAME_MIN_SIZE + ctx -> stack_size ));
184+
185+ if (ctx -> arena_vm_start )
186+ PPC_LI64 (bpf_to_ppc (ARENA_VM_START ), ctx -> arena_vm_start );
177187}
178188
179189static void bpf_jit_emit_common_epilogue (u32 * image , struct codegen_context * ctx )
@@ -185,6 +195,10 @@ static void bpf_jit_emit_common_epilogue(u32 *image, struct codegen_context *ctx
185195 if (bpf_is_seen_register (ctx , bpf_to_ppc (i )))
186196 EMIT (PPC_RAW_LD (bpf_to_ppc (i ), _R1 , bpf_jit_stack_offsetof (ctx , bpf_to_ppc (i ))));
187197
198+ if (ctx -> arena_vm_start )
199+ EMIT (PPC_RAW_LD (bpf_to_ppc (ARENA_VM_START ), _R1 ,
200+ bpf_jit_stack_offsetof (ctx , bpf_to_ppc (ARENA_VM_START ))));
201+
188202 /* Tear down our stack frame */
189203 if (bpf_has_stack_frame (ctx )) {
190204 EMIT (PPC_RAW_ADDI (_R1 , _R1 , BPF_PPC_STACKFRAME + ctx -> stack_size ));
@@ -396,11 +410,11 @@ void bpf_stf_barrier(void);
396410asm (
397411" .global bpf_stf_barrier ;"
398412" bpf_stf_barrier: ;"
399- " std 21,-64 (1) ;"
400- " std 22,-56 (1) ;"
413+ " std 21,-80 (1) ;"
414+ " std 22,-72 (1) ;"
401415" sync ;"
402- " ld 21,-64 (1) ;"
403- " ld 22,-56 (1) ;"
416+ " ld 21,-80 (1) ;"
417+ " ld 22,-72 (1) ;"
404418" ori 31,31,0 ;"
405419" .rept 14 ;"
406420" b 1f ;"
@@ -409,6 +423,36 @@ asm (
409423" blr ;"
410424);
411425
426+ static int bpf_jit_emit_probe_mem_store (struct codegen_context * ctx , u32 src_reg , s16 off ,
427+ u32 code , u32 * image )
428+ {
429+ u32 tmp1_reg = bpf_to_ppc (TMP_REG_1 );
430+ u32 tmp2_reg = bpf_to_ppc (TMP_REG_2 );
431+
432+ switch (BPF_SIZE (code )) {
433+ case BPF_B :
434+ EMIT (PPC_RAW_STB (src_reg , tmp1_reg , off ));
435+ break ;
436+ case BPF_H :
437+ EMIT (PPC_RAW_STH (src_reg , tmp1_reg , off ));
438+ break ;
439+ case BPF_W :
440+ EMIT (PPC_RAW_STW (src_reg , tmp1_reg , off ));
441+ break ;
442+ case BPF_DW :
443+ if (off % 4 ) {
444+ EMIT (PPC_RAW_LI (tmp2_reg , off ));
445+ EMIT (PPC_RAW_STDX (src_reg , tmp1_reg , tmp2_reg ));
446+ } else {
447+ EMIT (PPC_RAW_STD (src_reg , tmp1_reg , off ));
448+ }
449+ break ;
450+ default :
451+ return - EINVAL ;
452+ }
453+ return 0 ;
454+ }
455+
412456static int emit_atomic_ld_st (const struct bpf_insn insn , struct codegen_context * ctx , u32 * image )
413457{
414458 u32 code = insn .code ;
@@ -960,6 +1004,50 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
9601004 }
9611005 break ;
9621006
1007+ case BPF_STX | BPF_PROBE_MEM32 | BPF_B :
1008+ case BPF_STX | BPF_PROBE_MEM32 | BPF_H :
1009+ case BPF_STX | BPF_PROBE_MEM32 | BPF_W :
1010+ case BPF_STX | BPF_PROBE_MEM32 | BPF_DW :
1011+
1012+ EMIT (PPC_RAW_ADD (tmp1_reg , dst_reg , bpf_to_ppc (ARENA_VM_START )));
1013+
1014+ ret = bpf_jit_emit_probe_mem_store (ctx , src_reg , off , code , image );
1015+ if (ret )
1016+ return ret ;
1017+
1018+ ret = bpf_add_extable_entry (fp , image , fimage , pass , ctx ,
1019+ ctx -> idx - 1 , 4 , -1 , code );
1020+ if (ret )
1021+ return ret ;
1022+
1023+ break ;
1024+
1025+ case BPF_ST | BPF_PROBE_MEM32 | BPF_B :
1026+ case BPF_ST | BPF_PROBE_MEM32 | BPF_H :
1027+ case BPF_ST | BPF_PROBE_MEM32 | BPF_W :
1028+ case BPF_ST | BPF_PROBE_MEM32 | BPF_DW :
1029+
1030+ EMIT (PPC_RAW_ADD (tmp1_reg , dst_reg , bpf_to_ppc (ARENA_VM_START )));
1031+
1032+ if (BPF_SIZE (code ) == BPF_W || BPF_SIZE (code ) == BPF_DW ) {
1033+ PPC_LI32 (tmp2_reg , imm );
1034+ src_reg = tmp2_reg ;
1035+ } else {
1036+ EMIT (PPC_RAW_LI (tmp2_reg , imm ));
1037+ src_reg = tmp2_reg ;
1038+ }
1039+
1040+ ret = bpf_jit_emit_probe_mem_store (ctx , src_reg , off , code , image );
1041+ if (ret )
1042+ return ret ;
1043+
1044+ ret = bpf_add_extable_entry (fp , image , fimage , pass , ctx ,
1045+ ctx -> idx - 1 , 4 , -1 , code );
1046+ if (ret )
1047+ return ret ;
1048+
1049+ break ;
1050+
9631051 /*
9641052 * BPF_STX ATOMIC (atomic ops)
9651053 */
@@ -1112,9 +1200,10 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
11121200 * Check if 'off' is word aligned for BPF_DW, because
11131201 * we might generate two instructions.
11141202 */
1115- if ((BPF_SIZE (code ) == BPF_DW ||
1116- (BPF_SIZE (code ) == BPF_B && BPF_MODE (code ) == BPF_PROBE_MEMSX )) &&
1117- (off & 3 ))
1203+ if ((BPF_SIZE (code ) == BPF_DW && (off & 3 )) ||
1204+ (BPF_SIZE (code ) == BPF_B &&
1205+ BPF_MODE (code ) == BPF_PROBE_MEMSX ) ||
1206+ (BPF_SIZE (code ) == BPF_B && BPF_MODE (code ) == BPF_MEMSX ))
11181207 PPC_JMP ((ctx -> idx + 3 ) * 4 );
11191208 else
11201209 PPC_JMP ((ctx -> idx + 2 ) * 4 );
@@ -1160,12 +1249,49 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
11601249
11611250 if (BPF_MODE (code ) == BPF_PROBE_MEM ) {
11621251 ret = bpf_add_extable_entry (fp , image , fimage , pass , ctx ,
1163- ctx -> idx - 1 , 4 , dst_reg );
1252+ ctx -> idx - 1 , 4 , dst_reg , code );
11641253 if (ret )
11651254 return ret ;
11661255 }
11671256 break ;
11681257
1258+ /* dst = *(u64 *)(ul) (src + ARENA_VM_START + off) */
1259+ case BPF_LDX | BPF_PROBE_MEM32 | BPF_B :
1260+ case BPF_LDX | BPF_PROBE_MEM32 | BPF_H :
1261+ case BPF_LDX | BPF_PROBE_MEM32 | BPF_W :
1262+ case BPF_LDX | BPF_PROBE_MEM32 | BPF_DW :
1263+
1264+ EMIT (PPC_RAW_ADD (tmp1_reg , src_reg , bpf_to_ppc (ARENA_VM_START )));
1265+
1266+ switch (size ) {
1267+ case BPF_B :
1268+ EMIT (PPC_RAW_LBZ (dst_reg , tmp1_reg , off ));
1269+ break ;
1270+ case BPF_H :
1271+ EMIT (PPC_RAW_LHZ (dst_reg , tmp1_reg , off ));
1272+ break ;
1273+ case BPF_W :
1274+ EMIT (PPC_RAW_LWZ (dst_reg , tmp1_reg , off ));
1275+ break ;
1276+ case BPF_DW :
1277+ if (off % 4 ) {
1278+ EMIT (PPC_RAW_LI (tmp2_reg , off ));
1279+ EMIT (PPC_RAW_LDX (dst_reg , tmp1_reg , tmp2_reg ));
1280+ } else {
1281+ EMIT (PPC_RAW_LD (dst_reg , tmp1_reg , off ));
1282+ }
1283+ break ;
1284+ }
1285+
1286+ if (size != BPF_DW && insn_is_zext (& insn [i + 1 ]))
1287+ addrs [++ i ] = ctx -> idx * 4 ;
1288+
1289+ ret = bpf_add_extable_entry (fp , image , fimage , pass , ctx ,
1290+ ctx -> idx - 1 , 4 , dst_reg , code );
1291+ if (ret )
1292+ return ret ;
1293+ break ;
1294+
11691295 /*
11701296 * Doubleword load
11711297 * 16 byte instruction that uses two 'struct bpf_insn'
0 commit comments