@@ -58,6 +58,35 @@ static inline bool bpf_has_stack_frame(struct codegen_context *ctx)
58
58
return ctx -> seen & SEEN_FUNC || bpf_is_seen_register (ctx , BPF_REG_FP );
59
59
}
60
60
61
+ /*
62
+ * When not setting up our own stackframe, the redzone usage is:
63
+ *
64
+ * [ prev sp ] <-------------
65
+ * [ ... ] |
66
+ * sp (r1) ---> [ stack pointer ] --------------
67
+ * [ nv gpr save area ] 8*8
68
+ * [ tail_call_cnt ] 8
69
+ * [ local_tmp_var ] 8
70
+ * [ unused red zone ] 208 bytes protected
71
+ */
72
+ static int bpf_jit_stack_local (struct codegen_context * ctx )
73
+ {
74
+ if (bpf_has_stack_frame (ctx ))
75
+ return STACK_FRAME_MIN_SIZE + MAX_BPF_STACK ;
76
+ else
77
+ return - (BPF_PPC_STACK_SAVE + 16 );
78
+ }
79
+
80
+ static int bpf_jit_stack_offsetof (struct codegen_context * ctx , int reg )
81
+ {
82
+ if (reg >= BPF_PPC_NVR_MIN && reg < 32 )
83
+ return (bpf_has_stack_frame (ctx ) ? BPF_PPC_STACKFRAME : 0 )
84
+ - (8 * (32 - reg ));
85
+
86
+ pr_err ("BPF JIT is asking about unknown registers" );
87
+ BUG ();
88
+ }
89
+
61
90
static void bpf_jit_emit_skb_loads (u32 * image , struct codegen_context * ctx )
62
91
{
63
92
/*
@@ -100,9 +129,8 @@ static void bpf_jit_emit_func_call(u32 *image, struct codegen_context *ctx, u64
100
129
static void bpf_jit_build_prologue (u32 * image , struct codegen_context * ctx )
101
130
{
102
131
int i ;
103
- bool new_stack_frame = bpf_has_stack_frame (ctx );
104
132
105
- if (new_stack_frame ) {
133
+ if (bpf_has_stack_frame ( ctx ) ) {
106
134
/*
107
135
* We need a stack frame, but we don't necessarily need to
108
136
* save/restore LR unless we call other functions
@@ -122,53 +150,48 @@ static void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx)
122
150
*/
123
151
for (i = BPF_REG_6 ; i <= BPF_REG_10 ; i ++ )
124
152
if (bpf_is_seen_register (ctx , i ))
125
- PPC_BPF_STL (b2p [i ], 1 ,
126
- (new_stack_frame ? BPF_PPC_STACKFRAME : 0 ) -
127
- (8 * (32 - b2p [i ])));
153
+ PPC_BPF_STL (b2p [i ], 1 , bpf_jit_stack_offsetof (ctx , b2p [i ]));
128
154
129
155
/*
130
156
* Save additional non-volatile regs if we cache skb
131
157
* Also, setup skb data
132
158
*/
133
159
if (ctx -> seen & SEEN_SKB ) {
134
160
PPC_BPF_STL (b2p [SKB_HLEN_REG ], 1 ,
135
- BPF_PPC_STACKFRAME - ( 8 * ( 32 - b2p [SKB_HLEN_REG ]) ));
161
+ bpf_jit_stack_offsetof ( ctx , b2p [SKB_HLEN_REG ]));
136
162
PPC_BPF_STL (b2p [SKB_DATA_REG ], 1 ,
137
- BPF_PPC_STACKFRAME - ( 8 * ( 32 - b2p [SKB_DATA_REG ]) ));
163
+ bpf_jit_stack_offsetof ( ctx , b2p [SKB_DATA_REG ]));
138
164
bpf_jit_emit_skb_loads (image , ctx );
139
165
}
140
166
141
167
/* Setup frame pointer to point to the bpf stack area */
142
168
if (bpf_is_seen_register (ctx , BPF_REG_FP ))
143
169
PPC_ADDI (b2p [BPF_REG_FP ], 1 ,
144
- BPF_PPC_STACKFRAME - BPF_PPC_STACK_SAVE );
170
+ STACK_FRAME_MIN_SIZE + MAX_BPF_STACK );
145
171
}
146
172
147
173
static void bpf_jit_build_epilogue (u32 * image , struct codegen_context * ctx )
148
174
{
149
175
int i ;
150
- bool new_stack_frame = bpf_has_stack_frame (ctx );
151
176
152
177
/* Move result to r3 */
153
178
PPC_MR (3 , b2p [BPF_REG_0 ]);
154
179
155
180
/* Restore NVRs */
156
181
for (i = BPF_REG_6 ; i <= BPF_REG_10 ; i ++ )
157
182
if (bpf_is_seen_register (ctx , i ))
158
- PPC_BPF_LL (b2p [i ], 1 ,
159
- (new_stack_frame ? BPF_PPC_STACKFRAME : 0 ) -
160
- (8 * (32 - b2p [i ])));
183
+ PPC_BPF_LL (b2p [i ], 1 , bpf_jit_stack_offsetof (ctx , b2p [i ]));
161
184
162
185
/* Restore non-volatile registers used for skb cache */
163
186
if (ctx -> seen & SEEN_SKB ) {
164
187
PPC_BPF_LL (b2p [SKB_HLEN_REG ], 1 ,
165
- BPF_PPC_STACKFRAME - ( 8 * ( 32 - b2p [SKB_HLEN_REG ]) ));
188
+ bpf_jit_stack_offsetof ( ctx , b2p [SKB_HLEN_REG ]));
166
189
PPC_BPF_LL (b2p [SKB_DATA_REG ], 1 ,
167
- BPF_PPC_STACKFRAME - ( 8 * ( 32 - b2p [SKB_DATA_REG ]) ));
190
+ bpf_jit_stack_offsetof ( ctx , b2p [SKB_DATA_REG ]));
168
191
}
169
192
170
193
/* Tear down our stack frame */
171
- if (new_stack_frame ) {
194
+ if (bpf_has_stack_frame ( ctx ) ) {
172
195
PPC_ADDI (1 , 1 , BPF_PPC_STACKFRAME );
173
196
if (ctx -> seen & SEEN_FUNC ) {
174
197
PPC_BPF_LL (0 , 1 , PPC_LR_STKOFF );
@@ -200,7 +223,6 @@ static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
200
223
u64 imm64 ;
201
224
u8 * func ;
202
225
u32 true_cond ;
203
- int stack_local_off ;
204
226
205
227
/*
206
228
* addrs[] maps a BPF bytecode address into a real offset from
@@ -219,9 +241,9 @@ static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
219
241
* optimization but everything else should work without
220
242
* any issues.
221
243
*/
222
- if (dst_reg >= 24 && dst_reg <= 31 )
244
+ if (dst_reg >= BPF_PPC_NVR_MIN && dst_reg < 32 )
223
245
bpf_set_seen_register (ctx , insn [i ].dst_reg );
224
- if (src_reg >= 24 && src_reg <= 31 )
246
+ if (src_reg >= BPF_PPC_NVR_MIN && src_reg < 32 )
225
247
bpf_set_seen_register (ctx , insn [i ].src_reg );
226
248
227
249
switch (code ) {
@@ -490,25 +512,12 @@ static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
490
512
* Way easier and faster(?) to store the value
491
513
* into stack and then use ldbrx
492
514
*
493
- * First, determine where in stack we can store
494
- * this:
495
- * - if we have allotted a stack frame, then we
496
- * will utilize the area set aside by
497
- * BPF_PPC_STACK_LOCALS
498
- * - else, we use the area beneath the NV GPR
499
- * save area
500
- *
501
515
* ctx->seen will be reliable in pass2, but
502
516
* the instructions generated will remain the
503
517
* same across all passes
504
518
*/
505
- if (bpf_has_stack_frame (ctx ))
506
- stack_local_off = STACK_FRAME_MIN_SIZE ;
507
- else
508
- stack_local_off = - (BPF_PPC_STACK_SAVE + 8 );
509
-
510
- PPC_STD (dst_reg , 1 , stack_local_off );
511
- PPC_ADDI (b2p [TMP_REG_1 ], 1 , stack_local_off );
519
+ PPC_STD (dst_reg , 1 , bpf_jit_stack_local (ctx ));
520
+ PPC_ADDI (b2p [TMP_REG_1 ], 1 , bpf_jit_stack_local (ctx ));
512
521
PPC_LDBRX (dst_reg , 0 , b2p [TMP_REG_1 ]);
513
522
break ;
514
523
}
@@ -668,7 +677,7 @@ static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
668
677
669
678
/* Save skb pointer if we need to re-cache skb data */
670
679
if (bpf_helper_changes_skb_data (func ))
671
- PPC_BPF_STL (3 , 1 , STACK_FRAME_MIN_SIZE );
680
+ PPC_BPF_STL (3 , 1 , bpf_jit_stack_local ( ctx ) );
672
681
673
682
bpf_jit_emit_func_call (image , ctx , (u64 )func );
674
683
@@ -678,7 +687,7 @@ static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
678
687
/* refresh skb cache */
679
688
if (bpf_helper_changes_skb_data (func )) {
680
689
/* reload skb pointer to r3 */
681
- PPC_BPF_LL (3 , 1 , STACK_FRAME_MIN_SIZE );
690
+ PPC_BPF_LL (3 , 1 , bpf_jit_stack_local ( ctx ) );
682
691
bpf_jit_emit_skb_loads (image , ctx );
683
692
}
684
693
break ;
0 commit comments