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