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