@@ -190,9 +190,7 @@ struct jit_context {
190
190
#define BPF_MAX_INSN_SIZE 128
191
191
#define BPF_INSN_SAFETY 64
192
192
193
- #define AUX_STACK_SPACE 40 /* Space for RBX, R13, R14, R15, tailcnt */
194
-
195
- #define PROLOGUE_SIZE 37
193
+ #define PROLOGUE_SIZE 20
196
194
197
195
/*
198
196
* Emit x86-64 prologue code for BPF program and check its size.
@@ -203,44 +201,19 @@ static void emit_prologue(u8 **pprog, u32 stack_depth, bool ebpf_from_cbpf)
203
201
u8 * prog = * pprog ;
204
202
int cnt = 0 ;
205
203
206
- /* push rbp */
207
- EMIT1 (0x55 );
208
-
209
- /* mov rbp,rsp */
210
- EMIT3 (0x48 , 0x89 , 0xE5 );
211
-
212
- /* sub rsp, rounded_stack_depth + AUX_STACK_SPACE */
213
- EMIT3_off32 (0x48 , 0x81 , 0xEC ,
214
- round_up (stack_depth , 8 ) + AUX_STACK_SPACE );
215
-
216
- /* sub rbp, AUX_STACK_SPACE */
217
- EMIT4 (0x48 , 0x83 , 0xED , AUX_STACK_SPACE );
218
-
219
- /* mov qword ptr [rbp+0],rbx */
220
- EMIT4 (0x48 , 0x89 , 0x5D , 0 );
221
- /* mov qword ptr [rbp+8],r13 */
222
- EMIT4 (0x4C , 0x89 , 0x6D , 8 );
223
- /* mov qword ptr [rbp+16],r14 */
224
- EMIT4 (0x4C , 0x89 , 0x75 , 16 );
225
- /* mov qword ptr [rbp+24],r15 */
226
- EMIT4 (0x4C , 0x89 , 0x7D , 24 );
227
-
204
+ EMIT1 (0x55 ); /* push rbp */
205
+ EMIT3 (0x48 , 0x89 , 0xE5 ); /* mov rbp, rsp */
206
+ /* sub rsp, rounded_stack_depth */
207
+ EMIT3_off32 (0x48 , 0x81 , 0xEC , round_up (stack_depth , 8 ));
208
+ EMIT1 (0x53 ); /* push rbx */
209
+ EMIT2 (0x41 , 0x55 ); /* push r13 */
210
+ EMIT2 (0x41 , 0x56 ); /* push r14 */
211
+ EMIT2 (0x41 , 0x57 ); /* push r15 */
228
212
if (!ebpf_from_cbpf ) {
229
- /*
230
- * Clear the tail call counter (tail_call_cnt): for eBPF tail
231
- * calls we need to reset the counter to 0. It's done in two
232
- * instructions, resetting RAX register to 0, and moving it
233
- * to the counter location.
234
- */
235
-
236
- /* xor eax, eax */
237
- EMIT2 (0x31 , 0xc0 );
238
- /* mov qword ptr [rbp+32], rax */
239
- EMIT4 (0x48 , 0x89 , 0x45 , 32 );
240
-
213
+ /* zero init tail_call_cnt */
214
+ EMIT2 (0x6a , 0x00 );
241
215
BUILD_BUG_ON (cnt != PROLOGUE_SIZE );
242
216
}
243
-
244
217
* pprog = prog ;
245
218
}
246
219
@@ -285,13 +258,13 @@ static void emit_bpf_tail_call(u8 **pprog)
285
258
* if (tail_call_cnt > MAX_TAIL_CALL_CNT)
286
259
* goto out;
287
260
*/
288
- EMIT2_off32 (0x8B , 0x85 , 36 ); /* mov eax, dword ptr [rbp + 36 ] */
261
+ EMIT2_off32 (0x8B , 0x85 , -36 - MAX_BPF_STACK ); /* mov eax, dword ptr [rbp - 548 ] */
289
262
EMIT3 (0x83 , 0xF8 , MAX_TAIL_CALL_CNT ); /* cmp eax, MAX_TAIL_CALL_CNT */
290
263
#define OFFSET2 (30 + RETPOLINE_RAX_BPF_JIT_SIZE)
291
264
EMIT2 (X86_JA , OFFSET2 ); /* ja out */
292
265
label2 = cnt ;
293
266
EMIT3 (0x83 , 0xC0 , 0x01 ); /* add eax, 1 */
294
- EMIT2_off32 (0x89 , 0x85 , 36 ); /* mov dword ptr [rbp + 36 ], eax */
267
+ EMIT2_off32 (0x89 , 0x85 , -36 - MAX_BPF_STACK ); /* mov dword ptr [rbp -548 ], eax */
295
268
296
269
/* prog = array->ptrs[index]; */
297
270
EMIT4_off32 (0x48 , 0x8B , 0x84 , 0xD6 , /* mov rax, [rsi + rdx * 8 + offsetof(...)] */
@@ -1040,19 +1013,14 @@ xadd: if (is_imm8(insn->off))
1040
1013
seen_exit = true;
1041
1014
/* Update cleanup_addr */
1042
1015
ctx -> cleanup_addr = proglen ;
1043
- /* mov rbx, qword ptr [rbp+0] */
1044
- EMIT4 (0x48 , 0x8B , 0x5D , 0 );
1045
- /* mov r13, qword ptr [rbp+8] */
1046
- EMIT4 (0x4C , 0x8B , 0x6D , 8 );
1047
- /* mov r14, qword ptr [rbp+16] */
1048
- EMIT4 (0x4C , 0x8B , 0x75 , 16 );
1049
- /* mov r15, qword ptr [rbp+24] */
1050
- EMIT4 (0x4C , 0x8B , 0x7D , 24 );
1051
-
1052
- /* add rbp, AUX_STACK_SPACE */
1053
- EMIT4 (0x48 , 0x83 , 0xC5 , AUX_STACK_SPACE );
1054
- EMIT1 (0xC9 ); /* leave */
1055
- EMIT1 (0xC3 ); /* ret */
1016
+ if (!bpf_prog_was_classic (bpf_prog ))
1017
+ EMIT1 (0x5B ); /* get rid of tail_call_cnt */
1018
+ EMIT2 (0x41 , 0x5F ); /* pop r15 */
1019
+ EMIT2 (0x41 , 0x5E ); /* pop r14 */
1020
+ EMIT2 (0x41 , 0x5D ); /* pop r13 */
1021
+ EMIT1 (0x5B ); /* pop rbx */
1022
+ EMIT1 (0xC9 ); /* leave */
1023
+ EMIT1 (0xC3 ); /* ret */
1056
1024
break ;
1057
1025
1058
1026
default :
0 commit comments