]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - arch/x86/net/bpf_jit_comp.c
bpf: change x86 JITed program stack layout
[mirror_ubuntu-hirsute-kernel.git] / arch / x86 / net / bpf_jit_comp.c
CommitLineData
0a14842f
ED
1/* bpf_jit_comp.c : BPF JIT compiler
2 *
3b58908a 3 * Copyright (C) 2011-2013 Eric Dumazet (eric.dumazet@gmail.com)
62258278 4 * Internal BPF Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
0a14842f
ED
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; version 2
9 * of the License.
10 */
0a14842f
ED
11#include <linux/netdevice.h>
12#include <linux/filter.h>
855ddb56 13#include <linux/if_vlan.h>
738cbe72 14#include <asm/cacheflush.h>
d1163651 15#include <asm/set_memory.h>
b52f00e6 16#include <linux/bpf.h>
0a14842f 17
0a14842f
ED
18int bpf_jit_enable __read_mostly;
19
20/*
21 * assembly code in arch/x86/net/bpf_jit.S
22 */
62258278 23extern u8 sk_load_word[], sk_load_half[], sk_load_byte[];
a998d434 24extern u8 sk_load_word_positive_offset[], sk_load_half_positive_offset[];
62258278 25extern u8 sk_load_byte_positive_offset[];
a998d434 26extern u8 sk_load_word_negative_offset[], sk_load_half_negative_offset[];
62258278 27extern u8 sk_load_byte_negative_offset[];
0a14842f 28
5cccc702 29static u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len)
0a14842f
ED
30{
31 if (len == 1)
32 *ptr = bytes;
33 else if (len == 2)
34 *(u16 *)ptr = bytes;
35 else {
36 *(u32 *)ptr = bytes;
37 barrier();
38 }
39 return ptr + len;
40}
41
b52f00e6
AS
42#define EMIT(bytes, len) \
43 do { prog = emit_code(prog, bytes, len); cnt += len; } while (0)
0a14842f
ED
44
45#define EMIT1(b1) EMIT(b1, 1)
46#define EMIT2(b1, b2) EMIT((b1) + ((b2) << 8), 2)
47#define EMIT3(b1, b2, b3) EMIT((b1) + ((b2) << 8) + ((b3) << 16), 3)
48#define EMIT4(b1, b2, b3, b4) EMIT((b1) + ((b2) << 8) + ((b3) << 16) + ((b4) << 24), 4)
62258278
AS
49#define EMIT1_off32(b1, off) \
50 do {EMIT1(b1); EMIT(off, 4); } while (0)
51#define EMIT2_off32(b1, b2, off) \
52 do {EMIT2(b1, b2); EMIT(off, 4); } while (0)
53#define EMIT3_off32(b1, b2, b3, off) \
54 do {EMIT3(b1, b2, b3); EMIT(off, 4); } while (0)
55#define EMIT4_off32(b1, b2, b3, b4, off) \
56 do {EMIT4(b1, b2, b3, b4); EMIT(off, 4); } while (0)
0a14842f 57
5cccc702 58static bool is_imm8(int value)
0a14842f
ED
59{
60 return value <= 127 && value >= -128;
61}
62
5cccc702 63static bool is_simm32(s64 value)
0a14842f 64{
62258278 65 return value == (s64) (s32) value;
0a14842f
ED
66}
67
e430f34e
AS
68/* mov dst, src */
69#define EMIT_mov(DST, SRC) \
70 do {if (DST != SRC) \
71 EMIT3(add_2mod(0x48, DST, SRC), 0x89, add_2reg(0xC0, DST, SRC)); \
62258278
AS
72 } while (0)
73
74static int bpf_size_to_x86_bytes(int bpf_size)
75{
76 if (bpf_size == BPF_W)
77 return 4;
78 else if (bpf_size == BPF_H)
79 return 2;
80 else if (bpf_size == BPF_B)
81 return 1;
82 else if (bpf_size == BPF_DW)
83 return 4; /* imm32 */
84 else
85 return 0;
86}
0a14842f
ED
87
88/* list of x86 cond jumps opcodes (. + s8)
89 * Add 0x10 (and an extra 0x0f) to generate far jumps (. + s32)
90 */
91#define X86_JB 0x72
92#define X86_JAE 0x73
93#define X86_JE 0x74
94#define X86_JNE 0x75
95#define X86_JBE 0x76
96#define X86_JA 0x77
62258278
AS
97#define X86_JGE 0x7D
98#define X86_JG 0x7F
0a14842f 99
5cccc702 100static void bpf_flush_icache(void *start, void *end)
0a14842f
ED
101{
102 mm_segment_t old_fs = get_fs();
103
104 set_fs(KERNEL_DS);
105 smp_wmb();
106 flush_icache_range((unsigned long)start, (unsigned long)end);
107 set_fs(old_fs);
108}
109
a998d434
JS
110#define CHOOSE_LOAD_FUNC(K, func) \
111 ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative_offset : func) : func##_positive_offset)
0a14842f 112
62258278 113/* pick a register outside of BPF range for JIT internal work */
959a7579 114#define AUX_REG (MAX_BPF_JIT_REG + 1)
62258278 115
959a7579
DB
116/* The following table maps BPF registers to x64 registers.
117 *
118 * x64 register r12 is unused, since if used as base address
119 * register in load/store instructions, it always needs an
120 * extra byte of encoding and is callee saved.
121 *
122 * r9 caches skb->len - skb->data_len
123 * r10 caches skb->data, and used for blinding (if enabled)
62258278
AS
124 */
125static const int reg2hex[] = {
126 [BPF_REG_0] = 0, /* rax */
127 [BPF_REG_1] = 7, /* rdi */
128 [BPF_REG_2] = 6, /* rsi */
129 [BPF_REG_3] = 2, /* rdx */
130 [BPF_REG_4] = 1, /* rcx */
131 [BPF_REG_5] = 0, /* r8 */
132 [BPF_REG_6] = 3, /* rbx callee saved */
133 [BPF_REG_7] = 5, /* r13 callee saved */
134 [BPF_REG_8] = 6, /* r14 callee saved */
135 [BPF_REG_9] = 7, /* r15 callee saved */
136 [BPF_REG_FP] = 5, /* rbp readonly */
959a7579 137 [BPF_REG_AX] = 2, /* r10 temp register */
62258278
AS
138 [AUX_REG] = 3, /* r11 temp register */
139};
140
141/* is_ereg() == true if BPF register 'reg' maps to x64 r8..r15
142 * which need extra byte of encoding.
143 * rax,rcx,...,rbp have simpler encoding
144 */
5cccc702 145static bool is_ereg(u32 reg)
62258278 146{
d148134b
JP
147 return (1 << reg) & (BIT(BPF_REG_5) |
148 BIT(AUX_REG) |
149 BIT(BPF_REG_7) |
150 BIT(BPF_REG_8) |
959a7579
DB
151 BIT(BPF_REG_9) |
152 BIT(BPF_REG_AX));
62258278
AS
153}
154
155/* add modifiers if 'reg' maps to x64 registers r8..r15 */
5cccc702 156static u8 add_1mod(u8 byte, u32 reg)
62258278
AS
157{
158 if (is_ereg(reg))
159 byte |= 1;
160 return byte;
161}
162
5cccc702 163static u8 add_2mod(u8 byte, u32 r1, u32 r2)
62258278
AS
164{
165 if (is_ereg(r1))
166 byte |= 1;
167 if (is_ereg(r2))
168 byte |= 4;
169 return byte;
170}
171
e430f34e 172/* encode 'dst_reg' register into x64 opcode 'byte' */
5cccc702 173static u8 add_1reg(u8 byte, u32 dst_reg)
62258278 174{
e430f34e 175 return byte + reg2hex[dst_reg];
62258278
AS
176}
177
e430f34e 178/* encode 'dst_reg' and 'src_reg' registers into x64 opcode 'byte' */
5cccc702 179static u8 add_2reg(u8 byte, u32 dst_reg, u32 src_reg)
62258278 180{
e430f34e 181 return byte + reg2hex[dst_reg] + (reg2hex[src_reg] << 3);
62258278
AS
182}
183
738cbe72
DB
184static void jit_fill_hole(void *area, unsigned int size)
185{
186 /* fill whole space with int3 instructions */
187 memset(area, 0xcc, size);
188}
189
f3c2af7b 190struct jit_context {
769e0de6 191 int cleanup_addr; /* epilogue code offset */
62258278 192 bool seen_ld_abs;
959a7579 193 bool seen_ax_reg;
f3c2af7b
AS
194};
195
e0ee9c12
AS
196/* maximum number of bytes emitted while JITing one eBPF insn */
197#define BPF_MAX_INSN_SIZE 128
198#define BPF_INSN_SAFETY 64
199
177366bf
AS
200#define AUX_STACK_SPACE \
201 (32 /* space for rbx, r13, r14, r15 */ + \
b52f00e6
AS
202 8 /* space for skb_copy_bits() buffer */)
203
177366bf 204#define PROLOGUE_SIZE 37
b52f00e6
AS
205
206/* emit x64 prologue code for BPF program and check it's size.
207 * bpf_tail_call helper will skip it while jumping into another program
208 */
209static void emit_prologue(u8 **pprog)
0a14842f 210{
b52f00e6
AS
211 u8 *prog = *pprog;
212 int cnt = 0;
0a14842f 213
62258278
AS
214 EMIT1(0x55); /* push rbp */
215 EMIT3(0x48, 0x89, 0xE5); /* mov rbp,rsp */
0a14842f 216
177366bf
AS
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);
62258278
AS
222
223 /* all classic BPF filters use R6(rbx) save it */
224
177366bf
AS
225 /* mov qword ptr [rbp+0],rbx */
226 EMIT4(0x48, 0x89, 0x5D, 0);
62258278 227
8fb575ca 228 /* bpf_convert_filter() maps classic BPF register X to R7 and uses R8
62258278
AS
229 * as temporary, so all tcpdump filters need to spill/fill R7(r13) and
230 * R8(r14). R9(r15) spill could be made conditional, but there is only
231 * one 'bpf_error' return path out of helper functions inside bpf_jit.S
232 * The overhead of extra spill is negligible for any filter other
233 * than synthetic ones. Therefore not worth adding complexity.
234 */
235
177366bf
AS
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);
62258278 242
8b614aeb
DB
243 /* Clear the tail call counter (tail_call_cnt): for eBPF tail calls
244 * we need to reset the counter to 0. It's done in two instructions,
245 * resetting rax register to 0 (xor on eax gets 0 extended), and
246 * moving it to the counter location.
247 */
62258278 248
8b614aeb
DB
249 /* xor eax, eax */
250 EMIT2(0x31, 0xc0);
177366bf
AS
251 /* mov qword ptr [rbp+32], rax */
252 EMIT4(0x48, 0x89, 0x45, 32);
b52f00e6
AS
253
254 BUILD_BUG_ON(cnt != PROLOGUE_SIZE);
255 *pprog = prog;
256}
257
258/* generate the following code:
259 * ... bpf_tail_call(void *ctx, struct bpf_array *array, u64 index) ...
260 * if (index >= array->map.max_entries)
261 * goto out;
262 * if (++tail_call_cnt > MAX_TAIL_CALL_CNT)
263 * goto out;
2a36f0b9 264 * prog = array->ptrs[index];
b52f00e6
AS
265 * if (prog == NULL)
266 * goto out;
267 * goto *(prog->bpf_func + prologue_size);
268 * out:
269 */
270static void emit_bpf_tail_call(u8 **pprog)
271{
272 u8 *prog = *pprog;
273 int label1, label2, label3;
274 int cnt = 0;
275
276 /* rdi - pointer to ctx
277 * rsi - pointer to bpf_array
278 * rdx - index in bpf_array
279 */
280
281 /* if (index >= array->map.max_entries)
282 * goto out;
283 */
284 EMIT4(0x48, 0x8B, 0x46, /* mov rax, qword ptr [rsi + 16] */
285 offsetof(struct bpf_array, map.max_entries));
286 EMIT3(0x48, 0x39, 0xD0); /* cmp rax, rdx */
2482abb9 287#define OFFSET1 47 /* number of bytes to jump */
b52f00e6
AS
288 EMIT2(X86_JBE, OFFSET1); /* jbe out */
289 label1 = cnt;
290
291 /* if (tail_call_cnt > MAX_TAIL_CALL_CNT)
292 * goto out;
293 */
177366bf 294 EMIT2_off32(0x8B, 0x85, 36); /* mov eax, dword ptr [rbp + 36] */
b52f00e6 295 EMIT3(0x83, 0xF8, MAX_TAIL_CALL_CNT); /* cmp eax, MAX_TAIL_CALL_CNT */
2482abb9 296#define OFFSET2 36
b52f00e6
AS
297 EMIT2(X86_JA, OFFSET2); /* ja out */
298 label2 = cnt;
299 EMIT3(0x83, 0xC0, 0x01); /* add eax, 1 */
177366bf 300 EMIT2_off32(0x89, 0x85, 36); /* mov dword ptr [rbp + 36], eax */
b52f00e6 301
2a36f0b9 302 /* prog = array->ptrs[index]; */
2482abb9 303 EMIT4_off32(0x48, 0x8D, 0x84, 0xD6, /* lea rax, [rsi + rdx * 8 + offsetof(...)] */
2a36f0b9 304 offsetof(struct bpf_array, ptrs));
b52f00e6
AS
305 EMIT3(0x48, 0x8B, 0x00); /* mov rax, qword ptr [rax] */
306
307 /* if (prog == NULL)
308 * goto out;
309 */
310 EMIT4(0x48, 0x83, 0xF8, 0x00); /* cmp rax, 0 */
311#define OFFSET3 10
312 EMIT2(X86_JE, OFFSET3); /* je out */
313 label3 = cnt;
314
315 /* goto *(prog->bpf_func + prologue_size); */
316 EMIT4(0x48, 0x8B, 0x40, /* mov rax, qword ptr [rax + 32] */
317 offsetof(struct bpf_prog, bpf_func));
318 EMIT4(0x48, 0x83, 0xC0, PROLOGUE_SIZE); /* add rax, prologue_size */
319
320 /* now we're ready to jump into next BPF program
321 * rdi == ctx (1st arg)
322 * rax == prog->bpf_func + prologue_size
323 */
324 EMIT2(0xFF, 0xE0); /* jmp rax */
325
326 /* out: */
327 BUILD_BUG_ON(cnt - label1 != OFFSET1);
328 BUILD_BUG_ON(cnt - label2 != OFFSET2);
329 BUILD_BUG_ON(cnt - label3 != OFFSET3);
330 *pprog = prog;
331}
332
4e10df9a
AS
333
334static void emit_load_skb_data_hlen(u8 **pprog)
335{
336 u8 *prog = *pprog;
337 int cnt = 0;
338
339 /* r9d = skb->len - skb->data_len (headlen)
340 * r10 = skb->data
341 */
342 /* mov %r9d, off32(%rdi) */
343 EMIT3_off32(0x44, 0x8b, 0x8f, offsetof(struct sk_buff, len));
344
345 /* sub %r9d, off32(%rdi) */
346 EMIT3_off32(0x44, 0x2b, 0x8f, offsetof(struct sk_buff, data_len));
347
348 /* mov %r10, off32(%rdi) */
349 EMIT3_off32(0x4c, 0x8b, 0x97, offsetof(struct sk_buff, data));
350 *pprog = prog;
351}
352
b52f00e6
AS
353static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
354 int oldproglen, struct jit_context *ctx)
355{
356 struct bpf_insn *insn = bpf_prog->insnsi;
357 int insn_cnt = bpf_prog->len;
358 bool seen_ld_abs = ctx->seen_ld_abs | (oldproglen == 0);
959a7579 359 bool seen_ax_reg = ctx->seen_ax_reg | (oldproglen == 0);
b52f00e6
AS
360 bool seen_exit = false;
361 u8 temp[BPF_MAX_INSN_SIZE + BPF_INSN_SAFETY];
362 int i, cnt = 0;
363 int proglen = 0;
364 u8 *prog = temp;
365
366 emit_prologue(&prog);
367
4e10df9a
AS
368 if (seen_ld_abs)
369 emit_load_skb_data_hlen(&prog);
62258278
AS
370
371 for (i = 0; i < insn_cnt; i++, insn++) {
e430f34e
AS
372 const s32 imm32 = insn->imm;
373 u32 dst_reg = insn->dst_reg;
374 u32 src_reg = insn->src_reg;
62258278
AS
375 u8 b1 = 0, b2 = 0, b3 = 0;
376 s64 jmp_offset;
377 u8 jmp_cond;
4e10df9a 378 bool reload_skb_data;
62258278
AS
379 int ilen;
380 u8 *func;
381
959a7579
DB
382 if (dst_reg == BPF_REG_AX || src_reg == BPF_REG_AX)
383 ctx->seen_ax_reg = seen_ax_reg = true;
384
62258278
AS
385 switch (insn->code) {
386 /* ALU */
387 case BPF_ALU | BPF_ADD | BPF_X:
388 case BPF_ALU | BPF_SUB | BPF_X:
389 case BPF_ALU | BPF_AND | BPF_X:
390 case BPF_ALU | BPF_OR | BPF_X:
391 case BPF_ALU | BPF_XOR | BPF_X:
392 case BPF_ALU64 | BPF_ADD | BPF_X:
393 case BPF_ALU64 | BPF_SUB | BPF_X:
394 case BPF_ALU64 | BPF_AND | BPF_X:
395 case BPF_ALU64 | BPF_OR | BPF_X:
396 case BPF_ALU64 | BPF_XOR | BPF_X:
397 switch (BPF_OP(insn->code)) {
398 case BPF_ADD: b2 = 0x01; break;
399 case BPF_SUB: b2 = 0x29; break;
400 case BPF_AND: b2 = 0x21; break;
401 case BPF_OR: b2 = 0x09; break;
402 case BPF_XOR: b2 = 0x31; break;
0a14842f 403 }
62258278 404 if (BPF_CLASS(insn->code) == BPF_ALU64)
e430f34e
AS
405 EMIT1(add_2mod(0x48, dst_reg, src_reg));
406 else if (is_ereg(dst_reg) || is_ereg(src_reg))
407 EMIT1(add_2mod(0x40, dst_reg, src_reg));
408 EMIT2(b2, add_2reg(0xC0, dst_reg, src_reg));
62258278 409 break;
0a14842f 410
e430f34e 411 /* mov dst, src */
62258278 412 case BPF_ALU64 | BPF_MOV | BPF_X:
e430f34e 413 EMIT_mov(dst_reg, src_reg);
0a14842f 414 break;
0a14842f 415
e430f34e 416 /* mov32 dst, src */
62258278 417 case BPF_ALU | BPF_MOV | BPF_X:
e430f34e
AS
418 if (is_ereg(dst_reg) || is_ereg(src_reg))
419 EMIT1(add_2mod(0x40, dst_reg, src_reg));
420 EMIT2(0x89, add_2reg(0xC0, dst_reg, src_reg));
62258278 421 break;
0a14842f 422
e430f34e 423 /* neg dst */
62258278
AS
424 case BPF_ALU | BPF_NEG:
425 case BPF_ALU64 | BPF_NEG:
426 if (BPF_CLASS(insn->code) == BPF_ALU64)
e430f34e
AS
427 EMIT1(add_1mod(0x48, dst_reg));
428 else if (is_ereg(dst_reg))
429 EMIT1(add_1mod(0x40, dst_reg));
430 EMIT2(0xF7, add_1reg(0xD8, dst_reg));
62258278
AS
431 break;
432
433 case BPF_ALU | BPF_ADD | BPF_K:
434 case BPF_ALU | BPF_SUB | BPF_K:
435 case BPF_ALU | BPF_AND | BPF_K:
436 case BPF_ALU | BPF_OR | BPF_K:
437 case BPF_ALU | BPF_XOR | BPF_K:
438 case BPF_ALU64 | BPF_ADD | BPF_K:
439 case BPF_ALU64 | BPF_SUB | BPF_K:
440 case BPF_ALU64 | BPF_AND | BPF_K:
441 case BPF_ALU64 | BPF_OR | BPF_K:
442 case BPF_ALU64 | BPF_XOR | BPF_K:
443 if (BPF_CLASS(insn->code) == BPF_ALU64)
e430f34e
AS
444 EMIT1(add_1mod(0x48, dst_reg));
445 else if (is_ereg(dst_reg))
446 EMIT1(add_1mod(0x40, dst_reg));
62258278
AS
447
448 switch (BPF_OP(insn->code)) {
449 case BPF_ADD: b3 = 0xC0; break;
450 case BPF_SUB: b3 = 0xE8; break;
451 case BPF_AND: b3 = 0xE0; break;
452 case BPF_OR: b3 = 0xC8; break;
453 case BPF_XOR: b3 = 0xF0; break;
454 }
455
e430f34e
AS
456 if (is_imm8(imm32))
457 EMIT3(0x83, add_1reg(b3, dst_reg), imm32);
62258278 458 else
e430f34e 459 EMIT2_off32(0x81, add_1reg(b3, dst_reg), imm32);
62258278
AS
460 break;
461
462 case BPF_ALU64 | BPF_MOV | BPF_K:
463 /* optimization: if imm32 is positive,
464 * use 'mov eax, imm32' (which zero-extends imm32)
465 * to save 2 bytes
466 */
e430f34e 467 if (imm32 < 0) {
62258278 468 /* 'mov rax, imm32' sign extends imm32 */
e430f34e 469 b1 = add_1mod(0x48, dst_reg);
62258278
AS
470 b2 = 0xC7;
471 b3 = 0xC0;
e430f34e 472 EMIT3_off32(b1, b2, add_1reg(b3, dst_reg), imm32);
0a14842f 473 break;
62258278
AS
474 }
475
476 case BPF_ALU | BPF_MOV | BPF_K:
606c88a8
DB
477 /* optimization: if imm32 is zero, use 'xor <dst>,<dst>'
478 * to save 3 bytes.
479 */
480 if (imm32 == 0) {
481 if (is_ereg(dst_reg))
482 EMIT1(add_2mod(0x40, dst_reg, dst_reg));
483 b2 = 0x31; /* xor */
484 b3 = 0xC0;
485 EMIT2(b2, add_2reg(b3, dst_reg, dst_reg));
486 break;
487 }
488
62258278 489 /* mov %eax, imm32 */
e430f34e
AS
490 if (is_ereg(dst_reg))
491 EMIT1(add_1mod(0x40, dst_reg));
492 EMIT1_off32(add_1reg(0xB8, dst_reg), imm32);
62258278
AS
493 break;
494
02ab695b 495 case BPF_LD | BPF_IMM | BPF_DW:
606c88a8
DB
496 /* optimization: if imm64 is zero, use 'xor <dst>,<dst>'
497 * to save 7 bytes.
498 */
499 if (insn[0].imm == 0 && insn[1].imm == 0) {
500 b1 = add_2mod(0x48, dst_reg, dst_reg);
501 b2 = 0x31; /* xor */
502 b3 = 0xC0;
503 EMIT3(b1, b2, add_2reg(b3, dst_reg, dst_reg));
504
505 insn++;
506 i++;
507 break;
508 }
509
02ab695b
AS
510 /* movabsq %rax, imm64 */
511 EMIT2(add_1mod(0x48, dst_reg), add_1reg(0xB8, dst_reg));
512 EMIT(insn[0].imm, 4);
513 EMIT(insn[1].imm, 4);
514
515 insn++;
516 i++;
517 break;
518
e430f34e 519 /* dst %= src, dst /= src, dst %= imm32, dst /= imm32 */
62258278
AS
520 case BPF_ALU | BPF_MOD | BPF_X:
521 case BPF_ALU | BPF_DIV | BPF_X:
522 case BPF_ALU | BPF_MOD | BPF_K:
523 case BPF_ALU | BPF_DIV | BPF_K:
524 case BPF_ALU64 | BPF_MOD | BPF_X:
525 case BPF_ALU64 | BPF_DIV | BPF_X:
526 case BPF_ALU64 | BPF_MOD | BPF_K:
527 case BPF_ALU64 | BPF_DIV | BPF_K:
528 EMIT1(0x50); /* push rax */
529 EMIT1(0x52); /* push rdx */
530
531 if (BPF_SRC(insn->code) == BPF_X)
e430f34e
AS
532 /* mov r11, src_reg */
533 EMIT_mov(AUX_REG, src_reg);
62258278 534 else
e430f34e
AS
535 /* mov r11, imm32 */
536 EMIT3_off32(0x49, 0xC7, 0xC3, imm32);
62258278 537
e430f34e
AS
538 /* mov rax, dst_reg */
539 EMIT_mov(BPF_REG_0, dst_reg);
62258278
AS
540
541 /* xor edx, edx
542 * equivalent to 'xor rdx, rdx', but one byte less
543 */
544 EMIT2(0x31, 0xd2);
545
546 if (BPF_SRC(insn->code) == BPF_X) {
e430f34e 547 /* if (src_reg == 0) return 0 */
62258278
AS
548
549 /* cmp r11, 0 */
550 EMIT4(0x49, 0x83, 0xFB, 0x00);
551
552 /* jne .+9 (skip over pop, pop, xor and jmp) */
553 EMIT2(X86_JNE, 1 + 1 + 2 + 5);
554 EMIT1(0x5A); /* pop rdx */
555 EMIT1(0x58); /* pop rax */
556 EMIT2(0x31, 0xc0); /* xor eax, eax */
557
558 /* jmp cleanup_addr
559 * addrs[i] - 11, because there are 11 bytes
560 * after this insn: div, mov, pop, pop, mov
561 */
562 jmp_offset = ctx->cleanup_addr - (addrs[i] - 11);
563 EMIT1_off32(0xE9, jmp_offset);
564 }
565
566 if (BPF_CLASS(insn->code) == BPF_ALU64)
567 /* div r11 */
568 EMIT3(0x49, 0xF7, 0xF3);
569 else
570 /* div r11d */
571 EMIT3(0x41, 0xF7, 0xF3);
572
573 if (BPF_OP(insn->code) == BPF_MOD)
574 /* mov r11, rdx */
575 EMIT3(0x49, 0x89, 0xD3);
576 else
577 /* mov r11, rax */
578 EMIT3(0x49, 0x89, 0xC3);
579
580 EMIT1(0x5A); /* pop rdx */
581 EMIT1(0x58); /* pop rax */
582
e430f34e
AS
583 /* mov dst_reg, r11 */
584 EMIT_mov(dst_reg, AUX_REG);
62258278
AS
585 break;
586
587 case BPF_ALU | BPF_MUL | BPF_K:
588 case BPF_ALU | BPF_MUL | BPF_X:
589 case BPF_ALU64 | BPF_MUL | BPF_K:
590 case BPF_ALU64 | BPF_MUL | BPF_X:
591 EMIT1(0x50); /* push rax */
592 EMIT1(0x52); /* push rdx */
593
e430f34e
AS
594 /* mov r11, dst_reg */
595 EMIT_mov(AUX_REG, dst_reg);
62258278
AS
596
597 if (BPF_SRC(insn->code) == BPF_X)
e430f34e
AS
598 /* mov rax, src_reg */
599 EMIT_mov(BPF_REG_0, src_reg);
62258278 600 else
e430f34e
AS
601 /* mov rax, imm32 */
602 EMIT3_off32(0x48, 0xC7, 0xC0, imm32);
62258278
AS
603
604 if (BPF_CLASS(insn->code) == BPF_ALU64)
605 EMIT1(add_1mod(0x48, AUX_REG));
606 else if (is_ereg(AUX_REG))
607 EMIT1(add_1mod(0x40, AUX_REG));
608 /* mul(q) r11 */
609 EMIT2(0xF7, add_1reg(0xE0, AUX_REG));
610
611 /* mov r11, rax */
612 EMIT_mov(AUX_REG, BPF_REG_0);
613
614 EMIT1(0x5A); /* pop rdx */
615 EMIT1(0x58); /* pop rax */
616
e430f34e
AS
617 /* mov dst_reg, r11 */
618 EMIT_mov(dst_reg, AUX_REG);
62258278
AS
619 break;
620
621 /* shifts */
622 case BPF_ALU | BPF_LSH | BPF_K:
623 case BPF_ALU | BPF_RSH | BPF_K:
624 case BPF_ALU | BPF_ARSH | BPF_K:
625 case BPF_ALU64 | BPF_LSH | BPF_K:
626 case BPF_ALU64 | BPF_RSH | BPF_K:
627 case BPF_ALU64 | BPF_ARSH | BPF_K:
628 if (BPF_CLASS(insn->code) == BPF_ALU64)
e430f34e
AS
629 EMIT1(add_1mod(0x48, dst_reg));
630 else if (is_ereg(dst_reg))
631 EMIT1(add_1mod(0x40, dst_reg));
62258278
AS
632
633 switch (BPF_OP(insn->code)) {
634 case BPF_LSH: b3 = 0xE0; break;
635 case BPF_RSH: b3 = 0xE8; break;
636 case BPF_ARSH: b3 = 0xF8; break;
637 }
e430f34e 638 EMIT3(0xC1, add_1reg(b3, dst_reg), imm32);
62258278
AS
639 break;
640
72b603ee
AS
641 case BPF_ALU | BPF_LSH | BPF_X:
642 case BPF_ALU | BPF_RSH | BPF_X:
643 case BPF_ALU | BPF_ARSH | BPF_X:
644 case BPF_ALU64 | BPF_LSH | BPF_X:
645 case BPF_ALU64 | BPF_RSH | BPF_X:
646 case BPF_ALU64 | BPF_ARSH | BPF_X:
647
648 /* check for bad case when dst_reg == rcx */
649 if (dst_reg == BPF_REG_4) {
650 /* mov r11, dst_reg */
651 EMIT_mov(AUX_REG, dst_reg);
652 dst_reg = AUX_REG;
653 }
654
655 if (src_reg != BPF_REG_4) { /* common case */
656 EMIT1(0x51); /* push rcx */
657
658 /* mov rcx, src_reg */
659 EMIT_mov(BPF_REG_4, src_reg);
660 }
661
662 /* shl %rax, %cl | shr %rax, %cl | sar %rax, %cl */
663 if (BPF_CLASS(insn->code) == BPF_ALU64)
664 EMIT1(add_1mod(0x48, dst_reg));
665 else if (is_ereg(dst_reg))
666 EMIT1(add_1mod(0x40, dst_reg));
667
668 switch (BPF_OP(insn->code)) {
669 case BPF_LSH: b3 = 0xE0; break;
670 case BPF_RSH: b3 = 0xE8; break;
671 case BPF_ARSH: b3 = 0xF8; break;
672 }
673 EMIT2(0xD3, add_1reg(b3, dst_reg));
674
675 if (src_reg != BPF_REG_4)
676 EMIT1(0x59); /* pop rcx */
677
678 if (insn->dst_reg == BPF_REG_4)
679 /* mov dst_reg, r11 */
680 EMIT_mov(insn->dst_reg, AUX_REG);
681 break;
682
62258278 683 case BPF_ALU | BPF_END | BPF_FROM_BE:
e430f34e 684 switch (imm32) {
62258278
AS
685 case 16:
686 /* emit 'ror %ax, 8' to swap lower 2 bytes */
687 EMIT1(0x66);
e430f34e 688 if (is_ereg(dst_reg))
62258278 689 EMIT1(0x41);
e430f34e 690 EMIT3(0xC1, add_1reg(0xC8, dst_reg), 8);
343f845b
AS
691
692 /* emit 'movzwl eax, ax' */
693 if (is_ereg(dst_reg))
694 EMIT3(0x45, 0x0F, 0xB7);
695 else
696 EMIT2(0x0F, 0xB7);
697 EMIT1(add_2reg(0xC0, dst_reg, dst_reg));
62258278
AS
698 break;
699 case 32:
700 /* emit 'bswap eax' to swap lower 4 bytes */
e430f34e 701 if (is_ereg(dst_reg))
62258278 702 EMIT2(0x41, 0x0F);
0a14842f 703 else
62258278 704 EMIT1(0x0F);
e430f34e 705 EMIT1(add_1reg(0xC8, dst_reg));
0a14842f 706 break;
62258278
AS
707 case 64:
708 /* emit 'bswap rax' to swap 8 bytes */
e430f34e
AS
709 EMIT3(add_1mod(0x48, dst_reg), 0x0F,
710 add_1reg(0xC8, dst_reg));
3b58908a
ED
711 break;
712 }
62258278
AS
713 break;
714
715 case BPF_ALU | BPF_END | BPF_FROM_LE:
343f845b
AS
716 switch (imm32) {
717 case 16:
718 /* emit 'movzwl eax, ax' to zero extend 16-bit
719 * into 64 bit
720 */
721 if (is_ereg(dst_reg))
722 EMIT3(0x45, 0x0F, 0xB7);
723 else
724 EMIT2(0x0F, 0xB7);
725 EMIT1(add_2reg(0xC0, dst_reg, dst_reg));
726 break;
727 case 32:
728 /* emit 'mov eax, eax' to clear upper 32-bits */
729 if (is_ereg(dst_reg))
730 EMIT1(0x45);
731 EMIT2(0x89, add_2reg(0xC0, dst_reg, dst_reg));
732 break;
733 case 64:
734 /* nop */
735 break;
736 }
62258278
AS
737 break;
738
e430f34e 739 /* ST: *(u8*)(dst_reg + off) = imm */
62258278 740 case BPF_ST | BPF_MEM | BPF_B:
e430f34e 741 if (is_ereg(dst_reg))
62258278
AS
742 EMIT2(0x41, 0xC6);
743 else
744 EMIT1(0xC6);
745 goto st;
746 case BPF_ST | BPF_MEM | BPF_H:
e430f34e 747 if (is_ereg(dst_reg))
62258278
AS
748 EMIT3(0x66, 0x41, 0xC7);
749 else
750 EMIT2(0x66, 0xC7);
751 goto st;
752 case BPF_ST | BPF_MEM | BPF_W:
e430f34e 753 if (is_ereg(dst_reg))
62258278
AS
754 EMIT2(0x41, 0xC7);
755 else
756 EMIT1(0xC7);
757 goto st;
758 case BPF_ST | BPF_MEM | BPF_DW:
e430f34e 759 EMIT2(add_1mod(0x48, dst_reg), 0xC7);
62258278
AS
760
761st: if (is_imm8(insn->off))
e430f34e 762 EMIT2(add_1reg(0x40, dst_reg), insn->off);
62258278 763 else
e430f34e 764 EMIT1_off32(add_1reg(0x80, dst_reg), insn->off);
62258278 765
e430f34e 766 EMIT(imm32, bpf_size_to_x86_bytes(BPF_SIZE(insn->code)));
62258278
AS
767 break;
768
e430f34e 769 /* STX: *(u8*)(dst_reg + off) = src_reg */
62258278
AS
770 case BPF_STX | BPF_MEM | BPF_B:
771 /* emit 'mov byte ptr [rax + off], al' */
e430f34e 772 if (is_ereg(dst_reg) || is_ereg(src_reg) ||
62258278 773 /* have to add extra byte for x86 SIL, DIL regs */
e430f34e
AS
774 src_reg == BPF_REG_1 || src_reg == BPF_REG_2)
775 EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x88);
62258278
AS
776 else
777 EMIT1(0x88);
778 goto stx;
779 case BPF_STX | BPF_MEM | BPF_H:
e430f34e
AS
780 if (is_ereg(dst_reg) || is_ereg(src_reg))
781 EMIT3(0x66, add_2mod(0x40, dst_reg, src_reg), 0x89);
62258278
AS
782 else
783 EMIT2(0x66, 0x89);
784 goto stx;
785 case BPF_STX | BPF_MEM | BPF_W:
e430f34e
AS
786 if (is_ereg(dst_reg) || is_ereg(src_reg))
787 EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x89);
62258278
AS
788 else
789 EMIT1(0x89);
790 goto stx;
791 case BPF_STX | BPF_MEM | BPF_DW:
e430f34e 792 EMIT2(add_2mod(0x48, dst_reg, src_reg), 0x89);
62258278 793stx: if (is_imm8(insn->off))
e430f34e 794 EMIT2(add_2reg(0x40, dst_reg, src_reg), insn->off);
62258278 795 else
e430f34e 796 EMIT1_off32(add_2reg(0x80, dst_reg, src_reg),
62258278
AS
797 insn->off);
798 break;
799
e430f34e 800 /* LDX: dst_reg = *(u8*)(src_reg + off) */
62258278
AS
801 case BPF_LDX | BPF_MEM | BPF_B:
802 /* emit 'movzx rax, byte ptr [rax + off]' */
e430f34e 803 EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB6);
62258278
AS
804 goto ldx;
805 case BPF_LDX | BPF_MEM | BPF_H:
806 /* emit 'movzx rax, word ptr [rax + off]' */
e430f34e 807 EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB7);
62258278
AS
808 goto ldx;
809 case BPF_LDX | BPF_MEM | BPF_W:
810 /* emit 'mov eax, dword ptr [rax+0x14]' */
e430f34e
AS
811 if (is_ereg(dst_reg) || is_ereg(src_reg))
812 EMIT2(add_2mod(0x40, src_reg, dst_reg), 0x8B);
62258278
AS
813 else
814 EMIT1(0x8B);
815 goto ldx;
816 case BPF_LDX | BPF_MEM | BPF_DW:
817 /* emit 'mov rax, qword ptr [rax+0x14]' */
e430f34e 818 EMIT2(add_2mod(0x48, src_reg, dst_reg), 0x8B);
62258278
AS
819ldx: /* if insn->off == 0 we can save one extra byte, but
820 * special case of x86 r13 which always needs an offset
821 * is not worth the hassle
822 */
823 if (is_imm8(insn->off))
e430f34e 824 EMIT2(add_2reg(0x40, src_reg, dst_reg), insn->off);
62258278 825 else
e430f34e 826 EMIT1_off32(add_2reg(0x80, src_reg, dst_reg),
62258278
AS
827 insn->off);
828 break;
829
e430f34e 830 /* STX XADD: lock *(u32*)(dst_reg + off) += src_reg */
62258278
AS
831 case BPF_STX | BPF_XADD | BPF_W:
832 /* emit 'lock add dword ptr [rax + off], eax' */
e430f34e
AS
833 if (is_ereg(dst_reg) || is_ereg(src_reg))
834 EMIT3(0xF0, add_2mod(0x40, dst_reg, src_reg), 0x01);
62258278
AS
835 else
836 EMIT2(0xF0, 0x01);
837 goto xadd;
838 case BPF_STX | BPF_XADD | BPF_DW:
e430f34e 839 EMIT3(0xF0, add_2mod(0x48, dst_reg, src_reg), 0x01);
62258278 840xadd: if (is_imm8(insn->off))
e430f34e 841 EMIT2(add_2reg(0x40, dst_reg, src_reg), insn->off);
62258278 842 else
e430f34e 843 EMIT1_off32(add_2reg(0x80, dst_reg, src_reg),
62258278
AS
844 insn->off);
845 break;
846
847 /* call */
848 case BPF_JMP | BPF_CALL:
e430f34e 849 func = (u8 *) __bpf_call_base + imm32;
62258278 850 jmp_offset = func - (image + addrs[i]);
e0ee9c12 851 if (seen_ld_abs) {
17bedab2 852 reload_skb_data = bpf_helper_changes_pkt_data(func);
4e10df9a
AS
853 if (reload_skb_data) {
854 EMIT1(0x57); /* push %rdi */
855 jmp_offset += 22; /* pop, mov, sub, mov */
856 } else {
857 EMIT2(0x41, 0x52); /* push %r10 */
858 EMIT2(0x41, 0x51); /* push %r9 */
859 /* need to adjust jmp offset, since
860 * pop %r9, pop %r10 take 4 bytes after call insn
861 */
862 jmp_offset += 4;
863 }
62258278 864 }
e430f34e 865 if (!imm32 || !is_simm32(jmp_offset)) {
62258278 866 pr_err("unsupported bpf func %d addr %p image %p\n",
e430f34e 867 imm32, func, image);
62258278
AS
868 return -EINVAL;
869 }
870 EMIT1_off32(0xE8, jmp_offset);
e0ee9c12 871 if (seen_ld_abs) {
4e10df9a
AS
872 if (reload_skb_data) {
873 EMIT1(0x5F); /* pop %rdi */
874 emit_load_skb_data_hlen(&prog);
875 } else {
876 EMIT2(0x41, 0x59); /* pop %r9 */
877 EMIT2(0x41, 0x5A); /* pop %r10 */
878 }
62258278
AS
879 }
880 break;
881
71189fa9 882 case BPF_JMP | BPF_TAIL_CALL:
b52f00e6
AS
883 emit_bpf_tail_call(&prog);
884 break;
885
62258278
AS
886 /* cond jump */
887 case BPF_JMP | BPF_JEQ | BPF_X:
888 case BPF_JMP | BPF_JNE | BPF_X:
889 case BPF_JMP | BPF_JGT | BPF_X:
890 case BPF_JMP | BPF_JGE | BPF_X:
891 case BPF_JMP | BPF_JSGT | BPF_X:
892 case BPF_JMP | BPF_JSGE | BPF_X:
e430f34e
AS
893 /* cmp dst_reg, src_reg */
894 EMIT3(add_2mod(0x48, dst_reg, src_reg), 0x39,
895 add_2reg(0xC0, dst_reg, src_reg));
62258278
AS
896 goto emit_cond_jmp;
897
898 case BPF_JMP | BPF_JSET | BPF_X:
e430f34e
AS
899 /* test dst_reg, src_reg */
900 EMIT3(add_2mod(0x48, dst_reg, src_reg), 0x85,
901 add_2reg(0xC0, dst_reg, src_reg));
62258278
AS
902 goto emit_cond_jmp;
903
904 case BPF_JMP | BPF_JSET | BPF_K:
e430f34e
AS
905 /* test dst_reg, imm32 */
906 EMIT1(add_1mod(0x48, dst_reg));
907 EMIT2_off32(0xF7, add_1reg(0xC0, dst_reg), imm32);
62258278
AS
908 goto emit_cond_jmp;
909
910 case BPF_JMP | BPF_JEQ | BPF_K:
911 case BPF_JMP | BPF_JNE | BPF_K:
912 case BPF_JMP | BPF_JGT | BPF_K:
913 case BPF_JMP | BPF_JGE | BPF_K:
914 case BPF_JMP | BPF_JSGT | BPF_K:
915 case BPF_JMP | BPF_JSGE | BPF_K:
e430f34e
AS
916 /* cmp dst_reg, imm8/32 */
917 EMIT1(add_1mod(0x48, dst_reg));
62258278 918
e430f34e
AS
919 if (is_imm8(imm32))
920 EMIT3(0x83, add_1reg(0xF8, dst_reg), imm32);
62258278 921 else
e430f34e 922 EMIT2_off32(0x81, add_1reg(0xF8, dst_reg), imm32);
62258278
AS
923
924emit_cond_jmp: /* convert BPF opcode to x86 */
925 switch (BPF_OP(insn->code)) {
926 case BPF_JEQ:
927 jmp_cond = X86_JE;
928 break;
929 case BPF_JSET:
930 case BPF_JNE:
931 jmp_cond = X86_JNE;
932 break;
933 case BPF_JGT:
934 /* GT is unsigned '>', JA in x86 */
935 jmp_cond = X86_JA;
936 break;
937 case BPF_JGE:
938 /* GE is unsigned '>=', JAE in x86 */
939 jmp_cond = X86_JAE;
940 break;
941 case BPF_JSGT:
942 /* signed '>', GT in x86 */
943 jmp_cond = X86_JG;
944 break;
945 case BPF_JSGE:
946 /* signed '>=', GE in x86 */
947 jmp_cond = X86_JGE;
948 break;
949 default: /* to silence gcc warning */
950 return -EFAULT;
951 }
952 jmp_offset = addrs[i + insn->off] - addrs[i];
953 if (is_imm8(jmp_offset)) {
954 EMIT2(jmp_cond, jmp_offset);
955 } else if (is_simm32(jmp_offset)) {
956 EMIT2_off32(0x0F, jmp_cond + 0x10, jmp_offset);
957 } else {
958 pr_err("cond_jmp gen bug %llx\n", jmp_offset);
959 return -EFAULT;
960 }
961
962 break;
0a14842f 963
62258278
AS
964 case BPF_JMP | BPF_JA:
965 jmp_offset = addrs[i + insn->off] - addrs[i];
966 if (!jmp_offset)
967 /* optimize out nop jumps */
968 break;
969emit_jmp:
970 if (is_imm8(jmp_offset)) {
971 EMIT2(0xEB, jmp_offset);
972 } else if (is_simm32(jmp_offset)) {
973 EMIT1_off32(0xE9, jmp_offset);
974 } else {
975 pr_err("jmp gen bug %llx\n", jmp_offset);
976 return -EFAULT;
977 }
978 break;
979
980 case BPF_LD | BPF_IND | BPF_W:
981 func = sk_load_word;
982 goto common_load;
983 case BPF_LD | BPF_ABS | BPF_W:
e430f34e 984 func = CHOOSE_LOAD_FUNC(imm32, sk_load_word);
e0ee9c12
AS
985common_load:
986 ctx->seen_ld_abs = seen_ld_abs = true;
62258278
AS
987 jmp_offset = func - (image + addrs[i]);
988 if (!func || !is_simm32(jmp_offset)) {
989 pr_err("unsupported bpf func %d addr %p image %p\n",
e430f34e 990 imm32, func, image);
62258278
AS
991 return -EINVAL;
992 }
993 if (BPF_MODE(insn->code) == BPF_ABS) {
994 /* mov %esi, imm32 */
e430f34e 995 EMIT1_off32(0xBE, imm32);
62258278 996 } else {
e430f34e
AS
997 /* mov %rsi, src_reg */
998 EMIT_mov(BPF_REG_2, src_reg);
999 if (imm32) {
1000 if (is_imm8(imm32))
62258278 1001 /* add %esi, imm8 */
e430f34e 1002 EMIT3(0x83, 0xC6, imm32);
0a14842f 1003 else
62258278 1004 /* add %esi, imm32 */
e430f34e 1005 EMIT2_off32(0x81, 0xC6, imm32);
0a14842f 1006 }
62258278
AS
1007 }
1008 /* skb pointer is in R6 (%rbx), it will be copied into
1009 * %rdi if skb_copy_bits() call is necessary.
1010 * sk_load_* helpers also use %r10 and %r9d.
1011 * See bpf_jit.S
1012 */
959a7579
DB
1013 if (seen_ax_reg)
1014 /* r10 = skb->data, mov %r10, off32(%rbx) */
1015 EMIT3_off32(0x4c, 0x8b, 0x93,
1016 offsetof(struct sk_buff, data));
62258278
AS
1017 EMIT1_off32(0xE8, jmp_offset); /* call */
1018 break;
1019
1020 case BPF_LD | BPF_IND | BPF_H:
1021 func = sk_load_half;
1022 goto common_load;
1023 case BPF_LD | BPF_ABS | BPF_H:
e430f34e 1024 func = CHOOSE_LOAD_FUNC(imm32, sk_load_half);
62258278
AS
1025 goto common_load;
1026 case BPF_LD | BPF_IND | BPF_B:
1027 func = sk_load_byte;
1028 goto common_load;
1029 case BPF_LD | BPF_ABS | BPF_B:
e430f34e 1030 func = CHOOSE_LOAD_FUNC(imm32, sk_load_byte);
62258278
AS
1031 goto common_load;
1032
1033 case BPF_JMP | BPF_EXIT:
769e0de6 1034 if (seen_exit) {
62258278
AS
1035 jmp_offset = ctx->cleanup_addr - addrs[i];
1036 goto emit_jmp;
1037 }
769e0de6 1038 seen_exit = true;
62258278
AS
1039 /* update cleanup_addr */
1040 ctx->cleanup_addr = proglen;
177366bf
AS
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);
62258278
AS
1052 EMIT1(0xC9); /* leave */
1053 EMIT1(0xC3); /* ret */
1054 break;
1055
f3c2af7b 1056 default:
62258278
AS
1057 /* By design x64 JIT should support all BPF instructions
1058 * This error will be seen if new instruction was added
1059 * to interpreter, but not to JIT
7ae457c1 1060 * or if there is junk in bpf_prog
62258278
AS
1061 */
1062 pr_err("bpf_jit: unknown opcode %02x\n", insn->code);
f3c2af7b
AS
1063 return -EINVAL;
1064 }
62258278 1065
f3c2af7b 1066 ilen = prog - temp;
e0ee9c12 1067 if (ilen > BPF_MAX_INSN_SIZE) {
9383191d 1068 pr_err("bpf_jit: fatal insn size error\n");
e0ee9c12
AS
1069 return -EFAULT;
1070 }
1071
f3c2af7b
AS
1072 if (image) {
1073 if (unlikely(proglen + ilen > oldproglen)) {
9383191d 1074 pr_err("bpf_jit: fatal error\n");
f3c2af7b 1075 return -EFAULT;
0a14842f 1076 }
f3c2af7b 1077 memcpy(image + proglen, temp, ilen);
0a14842f 1078 }
f3c2af7b
AS
1079 proglen += ilen;
1080 addrs[i] = proglen;
1081 prog = temp;
1082 }
f3c2af7b
AS
1083 return proglen;
1084}
1085
d1c55ab5 1086struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
f3c2af7b
AS
1087{
1088 struct bpf_binary_header *header = NULL;
959a7579 1089 struct bpf_prog *tmp, *orig_prog = prog;
f3c2af7b
AS
1090 int proglen, oldproglen = 0;
1091 struct jit_context ctx = {};
959a7579 1092 bool tmp_blinded = false;
f3c2af7b
AS
1093 u8 *image = NULL;
1094 int *addrs;
1095 int pass;
1096 int i;
1097
1098 if (!bpf_jit_enable)
959a7579
DB
1099 return orig_prog;
1100
1101 tmp = bpf_jit_blind_constants(prog);
1102 /* If blinding was requested and we failed during blinding,
1103 * we must fall back to the interpreter.
1104 */
1105 if (IS_ERR(tmp))
1106 return orig_prog;
1107 if (tmp != prog) {
1108 tmp_blinded = true;
1109 prog = tmp;
1110 }
0a14842f 1111
f3c2af7b 1112 addrs = kmalloc(prog->len * sizeof(*addrs), GFP_KERNEL);
959a7579
DB
1113 if (!addrs) {
1114 prog = orig_prog;
1115 goto out;
1116 }
f3c2af7b
AS
1117
1118 /* Before first pass, make a rough estimation of addrs[]
1119 * each bpf instruction is translated to less than 64 bytes
1120 */
1121 for (proglen = 0, i = 0; i < prog->len; i++) {
1122 proglen += 64;
1123 addrs[i] = proglen;
1124 }
1125 ctx.cleanup_addr = proglen;
f3c2af7b 1126
3f7352bf
AS
1127 /* JITed image shrinks with every pass and the loop iterates
1128 * until the image stops shrinking. Very large bpf programs
1129 * may converge on the last pass. In such case do one more
1130 * pass to emit the final image
1131 */
1132 for (pass = 0; pass < 10 || image; pass++) {
f3c2af7b
AS
1133 proglen = do_jit(prog, addrs, image, oldproglen, &ctx);
1134 if (proglen <= 0) {
1135 image = NULL;
1136 if (header)
738cbe72 1137 bpf_jit_binary_free(header);
959a7579
DB
1138 prog = orig_prog;
1139 goto out_addrs;
f3c2af7b 1140 }
0a14842f 1141 if (image) {
e0ee9c12 1142 if (proglen != oldproglen) {
f3c2af7b
AS
1143 pr_err("bpf_jit: proglen=%d != oldproglen=%d\n",
1144 proglen, oldproglen);
959a7579
DB
1145 prog = orig_prog;
1146 goto out_addrs;
e0ee9c12 1147 }
0a14842f
ED
1148 break;
1149 }
1150 if (proglen == oldproglen) {
738cbe72
DB
1151 header = bpf_jit_binary_alloc(proglen, &image,
1152 1, jit_fill_hole);
959a7579
DB
1153 if (!header) {
1154 prog = orig_prog;
1155 goto out_addrs;
1156 }
0a14842f
ED
1157 }
1158 oldproglen = proglen;
1159 }
79617801 1160
0a14842f 1161 if (bpf_jit_enable > 1)
485d6511 1162 bpf_jit_dump(prog->len, proglen, pass + 1, image);
0a14842f
ED
1163
1164 if (image) {
314beb9b 1165 bpf_flush_icache(header, image + proglen);
9d876e79 1166 bpf_jit_binary_lock_ro(header);
f3c2af7b 1167 prog->bpf_func = (void *)image;
a91263d5 1168 prog->jited = 1;
9d5ecb09
DB
1169 } else {
1170 prog = orig_prog;
0a14842f 1171 }
959a7579
DB
1172
1173out_addrs:
0a14842f 1174 kfree(addrs);
959a7579
DB
1175out:
1176 if (tmp_blinded)
1177 bpf_jit_prog_release_other(prog, prog == orig_prog ?
1178 tmp : orig_prog);
d1c55ab5 1179 return prog;
0a14842f 1180}