@@ -197,12 +197,11 @@ struct jit_context {
197
197
#define BPF_MAX_INSN_SIZE 128
198
198
#define BPF_INSN_SAFETY 64
199
199
200
- #define STACKSIZE \
201
- (MAX_BPF_STACK + \
202
- 32 /* space for rbx, r13, r14, r15 */ + \
200
+ #define AUX_STACK_SPACE \
201
+ (32 /* space for rbx, r13, r14, r15 */ + \
203
202
8 /* space for skb_copy_bits() buffer */ )
204
203
205
- #define PROLOGUE_SIZE 48
204
+ #define PROLOGUE_SIZE 37
206
205
207
206
/* emit x64 prologue code for BPF program and check it's size.
208
207
* bpf_tail_call helper will skip it while jumping into another program
@@ -215,13 +214,16 @@ static void emit_prologue(u8 **pprog)
215
214
EMIT1 (0x55 ); /* push rbp */
216
215
EMIT3 (0x48 , 0x89 , 0xE5 ); /* mov rbp,rsp */
217
216
218
- /* sub rsp, STACKSIZE */
219
- EMIT3_off32 (0x48 , 0x81 , 0xEC , STACKSIZE );
217
+ /* sub rsp, MAX_BPF_STACK + AUX_STACK_SPACE */
218
+ EMIT3_off32 (0x48 , 0x81 , 0xEC , MAX_BPF_STACK + AUX_STACK_SPACE );
219
+
220
+ /* sub rbp, AUX_STACK_SPACE */
221
+ EMIT4 (0x48 , 0x83 , 0xED , AUX_STACK_SPACE );
220
222
221
223
/* all classic BPF filters use R6(rbx) save it */
222
224
223
- /* mov qword ptr [rbp-X ],rbx */
224
- EMIT3_off32 (0x48 , 0x89 , 0x9D , - STACKSIZE );
225
+ /* mov qword ptr [rbp+0 ],rbx */
226
+ EMIT4 (0x48 , 0x89 , 0x5D , 0 );
225
227
226
228
/* bpf_convert_filter() maps classic BPF register X to R7 and uses R8
227
229
* as temporary, so all tcpdump filters need to spill/fill R7(r13) and
@@ -231,12 +233,12 @@ static void emit_prologue(u8 **pprog)
231
233
* than synthetic ones. Therefore not worth adding complexity.
232
234
*/
233
235
234
- /* mov qword ptr [rbp-X ],r13 */
235
- EMIT3_off32 (0x4C , 0x89 , 0xAD , - STACKSIZE + 8 );
236
- /* mov qword ptr [rbp-X ],r14 */
237
- EMIT3_off32 (0x4C , 0x89 , 0xB5 , - STACKSIZE + 16 );
238
- /* mov qword ptr [rbp-X ],r15 */
239
- EMIT3_off32 (0x4C , 0x89 , 0xBD , - STACKSIZE + 24 );
236
+ /* mov qword ptr [rbp+8 ],r13 */
237
+ EMIT4 (0x4C , 0x89 , 0x6D , 8 );
238
+ /* mov qword ptr [rbp+16 ],r14 */
239
+ EMIT4 (0x4C , 0x89 , 0x75 , 16 );
240
+ /* mov qword ptr [rbp+24 ],r15 */
241
+ EMIT4 (0x4C , 0x89 , 0x7D , 24 );
240
242
241
243
/* Clear the tail call counter (tail_call_cnt): for eBPF tail calls
242
244
* we need to reset the counter to 0. It's done in two instructions,
@@ -246,8 +248,8 @@ static void emit_prologue(u8 **pprog)
246
248
247
249
/* xor eax, eax */
248
250
EMIT2 (0x31 , 0xc0 );
249
- /* mov qword ptr [rbp-X ], rax */
250
- EMIT3_off32 (0x48 , 0x89 , 0x85 , - STACKSIZE + 32 );
251
+ /* mov qword ptr [rbp+32 ], rax */
252
+ EMIT4 (0x48 , 0x89 , 0x45 , 32 );
251
253
252
254
BUILD_BUG_ON (cnt != PROLOGUE_SIZE );
253
255
* pprog = prog ;
@@ -289,13 +291,13 @@ static void emit_bpf_tail_call(u8 **pprog)
289
291
/* if (tail_call_cnt > MAX_TAIL_CALL_CNT)
290
292
* goto out;
291
293
*/
292
- EMIT2_off32 (0x8B , 0x85 , - STACKSIZE + 36 ); /* mov eax, dword ptr [rbp - 516 ] */
294
+ EMIT2_off32 (0x8B , 0x85 , 36 ); /* mov eax, dword ptr [rbp + 36 ] */
293
295
EMIT3 (0x83 , 0xF8 , MAX_TAIL_CALL_CNT ); /* cmp eax, MAX_TAIL_CALL_CNT */
294
296
#define OFFSET2 36
295
297
EMIT2 (X86_JA , OFFSET2 ); /* ja out */
296
298
label2 = cnt ;
297
299
EMIT3 (0x83 , 0xC0 , 0x01 ); /* add eax, 1 */
298
- EMIT2_off32 (0x89 , 0x85 , - STACKSIZE + 36 ); /* mov dword ptr [rbp - 516 ], eax */
300
+ EMIT2_off32 (0x89 , 0x85 , 36 ); /* mov dword ptr [rbp + 36 ], eax */
299
301
300
302
/* prog = array->ptrs[index]; */
301
303
EMIT4_off32 (0x48 , 0x8D , 0x84 , 0xD6 , /* lea rax, [rsi + rdx * 8 + offsetof(...)] */
@@ -1036,15 +1038,17 @@ xadd: if (is_imm8(insn->off))
1036
1038
seen_exit = true;
1037
1039
/* update cleanup_addr */
1038
1040
ctx -> cleanup_addr = proglen ;
1039
- /* mov rbx, qword ptr [rbp-X] */
1040
- EMIT3_off32 (0x48 , 0x8B , 0x9D , - STACKSIZE );
1041
- /* mov r13, qword ptr [rbp-X] */
1042
- EMIT3_off32 (0x4C , 0x8B , 0xAD , - STACKSIZE + 8 );
1043
- /* mov r14, qword ptr [rbp-X] */
1044
- EMIT3_off32 (0x4C , 0x8B , 0xB5 , - STACKSIZE + 16 );
1045
- /* mov r15, qword ptr [rbp-X] */
1046
- EMIT3_off32 (0x4C , 0x8B , 0xBD , - STACKSIZE + 24 );
1047
-
1041
+ /* mov rbx, qword ptr [rbp+0] */
1042
+ EMIT4 (0x48 , 0x8B , 0x5D , 0 );
1043
+ /* mov r13, qword ptr [rbp+8] */
1044
+ EMIT4 (0x4C , 0x8B , 0x6D , 8 );
1045
+ /* mov r14, qword ptr [rbp+16] */
1046
+ EMIT4 (0x4C , 0x8B , 0x75 , 16 );
1047
+ /* mov r15, qword ptr [rbp+24] */
1048
+ EMIT4 (0x4C , 0x8B , 0x7D , 24 );
1049
+
1050
+ /* add rbp, AUX_STACK_SPACE */
1051
+ EMIT4 (0x48 , 0x83 , 0xC5 , AUX_STACK_SPACE );
1048
1052
EMIT1 (0xC9 ); /* leave */
1049
1053
EMIT1 (0xC3 ); /* ret */
1050
1054
break ;
0 commit comments