blob: f3e5cd8c1e6899ca8546441ab3053d1e40de1cbf [file] [log] [blame]
Eric Dumazet0a148422011-04-20 09:27:32 +00001/* bpf_jit_comp.c : BPF JIT compiler
2 *
Eric Dumazet3b589082013-01-30 17:51:44 -08003 * Copyright (C) 2011-2013 Eric Dumazet (eric.dumazet@gmail.com)
Alexei Starovoitov62258272014-05-13 19:50:46 -07004 * Internal BPF Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Eric Dumazet0a148422011-04-20 09:27:32 +00005 *
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 */
Eric Dumazet0a148422011-04-20 09:27:32 +000011#include <linux/netdevice.h>
12#include <linux/filter.h>
Eric Dumazet855ddb52012-10-27 02:26:22 +000013#include <linux/if_vlan.h>
Daniel Borkmann738cbe72014-09-08 08:04:47 +020014#include <asm/cacheflush.h>
Laura Abbottd1163652017-05-08 15:58:11 -070015#include <asm/set_memory.h>
Alexei Starovoitovb52f00e2015-05-19 16:59:04 -070016#include <linux/bpf.h>
Eric Dumazet0a148422011-04-20 09:27:32 +000017
Eric Dumazet0a148422011-04-20 09:27:32 +000018/*
19 * assembly code in arch/x86/net/bpf_jit.S
20 */
Alexei Starovoitov62258272014-05-13 19:50:46 -070021extern u8 sk_load_word[], sk_load_half[], sk_load_byte[];
Jan Seifferta998d432012-03-30 05:24:05 +000022extern u8 sk_load_word_positive_offset[], sk_load_half_positive_offset[];
Alexei Starovoitov62258272014-05-13 19:50:46 -070023extern u8 sk_load_byte_positive_offset[];
Jan Seifferta998d432012-03-30 05:24:05 +000024extern u8 sk_load_word_negative_offset[], sk_load_half_negative_offset[];
Alexei Starovoitov62258272014-05-13 19:50:46 -070025extern u8 sk_load_byte_negative_offset[];
Eric Dumazet0a148422011-04-20 09:27:32 +000026
Joe Perches5cccc702014-12-04 17:01:24 -080027static u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len)
Eric Dumazet0a148422011-04-20 09:27:32 +000028{
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
Alexei Starovoitovb52f00e2015-05-19 16:59:04 -070040#define EMIT(bytes, len) \
41 do { prog = emit_code(prog, bytes, len); cnt += len; } while (0)
Eric Dumazet0a148422011-04-20 09:27:32 +000042
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)
Alexei Starovoitov62258272014-05-13 19:50:46 -070047#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)
Eric Dumazet0a148422011-04-20 09:27:32 +000055
Joe Perches5cccc702014-12-04 17:01:24 -080056static bool is_imm8(int value)
Eric Dumazet0a148422011-04-20 09:27:32 +000057{
58 return value <= 127 && value >= -128;
59}
60
Joe Perches5cccc702014-12-04 17:01:24 -080061static bool is_simm32(s64 value)
Eric Dumazet0a148422011-04-20 09:27:32 +000062{
Daniel Borkmann6fe8b9c2018-02-24 01:07:59 +010063 return value == (s64)(s32)value;
64}
65
66static bool is_uimm32(u64 value)
67{
68 return value == (u64)(u32)value;
Eric Dumazet0a148422011-04-20 09:27:32 +000069}
70
Alexei Starovoitove430f342014-06-06 14:46:06 -070071/* 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)); \
Alexei Starovoitov62258272014-05-13 19:50:46 -070075 } 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}
Eric Dumazet0a148422011-04-20 09:27:32 +000090
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
Daniel Borkmann52afc512017-08-10 01:39:56 +0200100#define X86_JL 0x7C
Alexei Starovoitov62258272014-05-13 19:50:46 -0700101#define X86_JGE 0x7D
Daniel Borkmann52afc512017-08-10 01:39:56 +0200102#define X86_JLE 0x7E
Alexei Starovoitov62258272014-05-13 19:50:46 -0700103#define X86_JG 0x7F
Eric Dumazet0a148422011-04-20 09:27:32 +0000104
Joe Perches5cccc702014-12-04 17:01:24 -0800105static void bpf_flush_icache(void *start, void *end)
Eric Dumazet0a148422011-04-20 09:27:32 +0000106{
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
Jan Seifferta998d432012-03-30 05:24:05 +0000115#define CHOOSE_LOAD_FUNC(K, func) \
116 ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative_offset : func) : func##_positive_offset)
Eric Dumazet0a148422011-04-20 09:27:32 +0000117
Alexei Starovoitov62258272014-05-13 19:50:46 -0700118/* pick a register outside of BPF range for JIT internal work */
Daniel Borkmann959a7572016-05-13 19:08:33 +0200119#define AUX_REG (MAX_BPF_JIT_REG + 1)
Alexei Starovoitov62258272014-05-13 19:50:46 -0700120
Daniel Borkmann959a7572016-05-13 19:08:33 +0200121/* 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)
Alexei Starovoitov62258272014-05-13 19:50:46 -0700129 */
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 */
Daniel Borkmann959a7572016-05-13 19:08:33 +0200142 [BPF_REG_AX] = 2, /* r10 temp register */
Alexei Starovoitov62258272014-05-13 19:50:46 -0700143 [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 */
Joe Perches5cccc702014-12-04 17:01:24 -0800150static bool is_ereg(u32 reg)
Alexei Starovoitov62258272014-05-13 19:50:46 -0700151{
Joe Perchesd1481342014-12-04 15:00:48 -0800152 return (1 << reg) & (BIT(BPF_REG_5) |
153 BIT(AUX_REG) |
154 BIT(BPF_REG_7) |
155 BIT(BPF_REG_8) |
Daniel Borkmann959a7572016-05-13 19:08:33 +0200156 BIT(BPF_REG_9) |
157 BIT(BPF_REG_AX));
Alexei Starovoitov62258272014-05-13 19:50:46 -0700158}
159
Daniel Borkmannde0a4442018-01-20 01:24:35 +0100160static bool is_axreg(u32 reg)
161{
162 return reg == BPF_REG_0;
163}
164
Alexei Starovoitov62258272014-05-13 19:50:46 -0700165/* add modifiers if 'reg' maps to x64 registers r8..r15 */
Joe Perches5cccc702014-12-04 17:01:24 -0800166static u8 add_1mod(u8 byte, u32 reg)
Alexei Starovoitov62258272014-05-13 19:50:46 -0700167{
168 if (is_ereg(reg))
169 byte |= 1;
170 return byte;
171}
172
Joe Perches5cccc702014-12-04 17:01:24 -0800173static u8 add_2mod(u8 byte, u32 r1, u32 r2)
Alexei Starovoitov62258272014-05-13 19:50:46 -0700174{
175 if (is_ereg(r1))
176 byte |= 1;
177 if (is_ereg(r2))
178 byte |= 4;
179 return byte;
180}
181
Alexei Starovoitove430f342014-06-06 14:46:06 -0700182/* encode 'dst_reg' register into x64 opcode 'byte' */
Joe Perches5cccc702014-12-04 17:01:24 -0800183static u8 add_1reg(u8 byte, u32 dst_reg)
Alexei Starovoitov62258272014-05-13 19:50:46 -0700184{
Alexei Starovoitove430f342014-06-06 14:46:06 -0700185 return byte + reg2hex[dst_reg];
Alexei Starovoitov62258272014-05-13 19:50:46 -0700186}
187
Alexei Starovoitove430f342014-06-06 14:46:06 -0700188/* encode 'dst_reg' and 'src_reg' registers into x64 opcode 'byte' */
Joe Perches5cccc702014-12-04 17:01:24 -0800189static u8 add_2reg(u8 byte, u32 dst_reg, u32 src_reg)
Alexei Starovoitov62258272014-05-13 19:50:46 -0700190{
Alexei Starovoitove430f342014-06-06 14:46:06 -0700191 return byte + reg2hex[dst_reg] + (reg2hex[src_reg] << 3);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700192}
193
Daniel Borkmann738cbe72014-09-08 08:04:47 +0200194static void jit_fill_hole(void *area, unsigned int size)
195{
196 /* fill whole space with int3 instructions */
197 memset(area, 0xcc, size);
198}
199
Alexei Starovoitovf3c2af72014-05-13 19:50:45 -0700200struct jit_context {
Alexei Starovoitov769e0de2014-11-29 14:46:13 -0800201 int cleanup_addr; /* epilogue code offset */
Alexei Starovoitov62258272014-05-13 19:50:46 -0700202 bool seen_ld_abs;
Daniel Borkmann959a7572016-05-13 19:08:33 +0200203 bool seen_ax_reg;
Alexei Starovoitovf3c2af72014-05-13 19:50:45 -0700204};
205
Alexei Starovoitove0ee9c12014-10-10 20:30:23 -0700206/* maximum number of bytes emitted while JITing one eBPF insn */
207#define BPF_MAX_INSN_SIZE 128
208#define BPF_INSN_SAFETY 64
209
Alexei Starovoitov177366b2017-05-30 13:31:34 -0700210#define AUX_STACK_SPACE \
211 (32 /* space for rbx, r13, r14, r15 */ + \
Alexei Starovoitovb52f00e2015-05-19 16:59:04 -0700212 8 /* space for skb_copy_bits() buffer */)
213
Alexei Starovoitov177366b2017-05-30 13:31:34 -0700214#define PROLOGUE_SIZE 37
Alexei Starovoitovb52f00e2015-05-19 16:59:04 -0700215
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 */
Alexei Starovoitov2960ae42017-05-30 13:31:35 -0700219static void emit_prologue(u8 **pprog, u32 stack_depth)
Eric Dumazet0a148422011-04-20 09:27:32 +0000220{
Alexei Starovoitovb52f00e2015-05-19 16:59:04 -0700221 u8 *prog = *pprog;
222 int cnt = 0;
Eric Dumazet0a148422011-04-20 09:27:32 +0000223
Alexei Starovoitov62258272014-05-13 19:50:46 -0700224 EMIT1(0x55); /* push rbp */
225 EMIT3(0x48, 0x89, 0xE5); /* mov rbp,rsp */
Eric Dumazet0a148422011-04-20 09:27:32 +0000226
Alexei Starovoitov2960ae42017-05-30 13:31:35 -0700227 /* sub rsp, rounded_stack_depth + AUX_STACK_SPACE */
228 EMIT3_off32(0x48, 0x81, 0xEC,
229 round_up(stack_depth, 8) + AUX_STACK_SPACE);
Alexei Starovoitov177366b2017-05-30 13:31:34 -0700230
231 /* sub rbp, AUX_STACK_SPACE */
232 EMIT4(0x48, 0x83, 0xED, AUX_STACK_SPACE);
Eric Dumazet0a148422011-04-20 09:27:32 +0000233
Alexei Starovoitov62258272014-05-13 19:50:46 -0700234 /* all classic BPF filters use R6(rbx) save it */
Eric Dumazet0a148422011-04-20 09:27:32 +0000235
Alexei Starovoitov177366b2017-05-30 13:31:34 -0700236 /* mov qword ptr [rbp+0],rbx */
237 EMIT4(0x48, 0x89, 0x5D, 0);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700238
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700239 /* bpf_convert_filter() maps classic BPF register X to R7 and uses R8
Alexei Starovoitov62258272014-05-13 19:50:46 -0700240 * 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
Alexei Starovoitov177366b2017-05-30 13:31:34 -0700247 /* 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);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700253
Daniel Borkmann8b614ae2015-12-17 23:51:54 +0100254 /* 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 */
Alexei Starovoitov62258272014-05-13 19:50:46 -0700259
Daniel Borkmann8b614ae2015-12-17 23:51:54 +0100260 /* xor eax, eax */
261 EMIT2(0x31, 0xc0);
Alexei Starovoitov177366b2017-05-30 13:31:34 -0700262 /* mov qword ptr [rbp+32], rax */
263 EMIT4(0x48, 0x89, 0x45, 32);
Alexei Starovoitovb52f00e2015-05-19 16:59:04 -0700264
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;
Wang Nan2a36f0b2015-08-06 07:02:33 +0000275 * prog = array->ptrs[index];
Alexei Starovoitovb52f00e2015-05-19 16:59:04 -0700276 * 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 */
Alexei Starovoitov90caccd2017-10-03 15:37:20 -0700295 EMIT2(0x89, 0xD2); /* mov edx, edx */
296 EMIT3(0x39, 0x56, /* cmp dword ptr [rsi + 16], edx */
Alexei Starovoitovb52f00e2015-05-19 16:59:04 -0700297 offsetof(struct bpf_array, map.max_entries));
Eric Dumazet84ccac62017-08-31 04:53:42 -0700298#define OFFSET1 43 /* number of bytes to jump */
Alexei Starovoitovb52f00e2015-05-19 16:59:04 -0700299 EMIT2(X86_JBE, OFFSET1); /* jbe out */
300 label1 = cnt;
301
302 /* if (tail_call_cnt > MAX_TAIL_CALL_CNT)
303 * goto out;
304 */
Alexei Starovoitov177366b2017-05-30 13:31:34 -0700305 EMIT2_off32(0x8B, 0x85, 36); /* mov eax, dword ptr [rbp + 36] */
Alexei Starovoitovb52f00e2015-05-19 16:59:04 -0700306 EMIT3(0x83, 0xF8, MAX_TAIL_CALL_CNT); /* cmp eax, MAX_TAIL_CALL_CNT */
Eric Dumazet84ccac62017-08-31 04:53:42 -0700307#define OFFSET2 32
Alexei Starovoitovb52f00e2015-05-19 16:59:04 -0700308 EMIT2(X86_JA, OFFSET2); /* ja out */
309 label2 = cnt;
310 EMIT3(0x83, 0xC0, 0x01); /* add eax, 1 */
Alexei Starovoitov177366b2017-05-30 13:31:34 -0700311 EMIT2_off32(0x89, 0x85, 36); /* mov dword ptr [rbp + 36], eax */
Alexei Starovoitovb52f00e2015-05-19 16:59:04 -0700312
Wang Nan2a36f0b2015-08-06 07:02:33 +0000313 /* prog = array->ptrs[index]; */
Eric Dumazet84ccac62017-08-31 04:53:42 -0700314 EMIT4_off32(0x48, 0x8B, 0x84, 0xD6, /* mov rax, [rsi + rdx * 8 + offsetof(...)] */
Wang Nan2a36f0b2015-08-06 07:02:33 +0000315 offsetof(struct bpf_array, ptrs));
Alexei Starovoitovb52f00e2015-05-19 16:59:04 -0700316
317 /* if (prog == NULL)
318 * goto out;
319 */
Eric Dumazet84ccac62017-08-31 04:53:42 -0700320 EMIT3(0x48, 0x85, 0xC0); /* test rax,rax */
Alexei Starovoitovb52f00e2015-05-19 16:59:04 -0700321#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
Alexei Starovoitov4e10df92015-07-20 20:34:18 -0700343
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
Daniel Borkmann6fe8b9c2018-02-24 01:07:59 +0100363static 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
Alexei Starovoitovb52f00e2015-05-19 16:59:04 -0700425static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
426 int oldproglen, struct jit_context *ctx)
427{
428 struct bpf_insn *insn = bpf_prog->insnsi;
429 int insn_cnt = bpf_prog->len;
430 bool seen_ld_abs = ctx->seen_ld_abs | (oldproglen == 0);
Daniel Borkmann959a7572016-05-13 19:08:33 +0200431 bool seen_ax_reg = ctx->seen_ax_reg | (oldproglen == 0);
Alexei Starovoitovb52f00e2015-05-19 16:59:04 -0700432 bool seen_exit = false;
433 u8 temp[BPF_MAX_INSN_SIZE + BPF_INSN_SAFETY];
434 int i, cnt = 0;
435 int proglen = 0;
436 u8 *prog = temp;
437
Alexei Starovoitov2960ae42017-05-30 13:31:35 -0700438 emit_prologue(&prog, bpf_prog->aux->stack_depth);
Alexei Starovoitovb52f00e2015-05-19 16:59:04 -0700439
Alexei Starovoitov4e10df92015-07-20 20:34:18 -0700440 if (seen_ld_abs)
441 emit_load_skb_data_hlen(&prog);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700442
443 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitove430f342014-06-06 14:46:06 -0700444 const s32 imm32 = insn->imm;
445 u32 dst_reg = insn->dst_reg;
446 u32 src_reg = insn->src_reg;
Daniel Borkmann6fe8b9c2018-02-24 01:07:59 +0100447 u8 b2 = 0, b3 = 0;
Alexei Starovoitov62258272014-05-13 19:50:46 -0700448 s64 jmp_offset;
449 u8 jmp_cond;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -0700450 bool reload_skb_data;
Alexei Starovoitov62258272014-05-13 19:50:46 -0700451 int ilen;
452 u8 *func;
453
Daniel Borkmann959a7572016-05-13 19:08:33 +0200454 if (dst_reg == BPF_REG_AX || src_reg == BPF_REG_AX)
455 ctx->seen_ax_reg = seen_ax_reg = true;
456
Alexei Starovoitov62258272014-05-13 19:50:46 -0700457 switch (insn->code) {
458 /* ALU */
459 case BPF_ALU | BPF_ADD | BPF_X:
460 case BPF_ALU | BPF_SUB | BPF_X:
461 case BPF_ALU | BPF_AND | BPF_X:
462 case BPF_ALU | BPF_OR | BPF_X:
463 case BPF_ALU | BPF_XOR | BPF_X:
464 case BPF_ALU64 | BPF_ADD | BPF_X:
465 case BPF_ALU64 | BPF_SUB | BPF_X:
466 case BPF_ALU64 | BPF_AND | BPF_X:
467 case BPF_ALU64 | BPF_OR | BPF_X:
468 case BPF_ALU64 | BPF_XOR | BPF_X:
469 switch (BPF_OP(insn->code)) {
470 case BPF_ADD: b2 = 0x01; break;
471 case BPF_SUB: b2 = 0x29; break;
472 case BPF_AND: b2 = 0x21; break;
473 case BPF_OR: b2 = 0x09; break;
474 case BPF_XOR: b2 = 0x31; break;
Eric Dumazet0a148422011-04-20 09:27:32 +0000475 }
Alexei Starovoitov62258272014-05-13 19:50:46 -0700476 if (BPF_CLASS(insn->code) == BPF_ALU64)
Alexei Starovoitove430f342014-06-06 14:46:06 -0700477 EMIT1(add_2mod(0x48, dst_reg, src_reg));
478 else if (is_ereg(dst_reg) || is_ereg(src_reg))
479 EMIT1(add_2mod(0x40, dst_reg, src_reg));
480 EMIT2(b2, add_2reg(0xC0, dst_reg, src_reg));
Eric Dumazet0a148422011-04-20 09:27:32 +0000481 break;
Eric Dumazet0a148422011-04-20 09:27:32 +0000482
Alexei Starovoitove430f342014-06-06 14:46:06 -0700483 /* mov dst, src */
Alexei Starovoitov62258272014-05-13 19:50:46 -0700484 case BPF_ALU64 | BPF_MOV | BPF_X:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700485 EMIT_mov(dst_reg, src_reg);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700486 break;
Eric Dumazet0a148422011-04-20 09:27:32 +0000487
Alexei Starovoitove430f342014-06-06 14:46:06 -0700488 /* mov32 dst, src */
Alexei Starovoitov62258272014-05-13 19:50:46 -0700489 case BPF_ALU | BPF_MOV | BPF_X:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700490 if (is_ereg(dst_reg) || is_ereg(src_reg))
491 EMIT1(add_2mod(0x40, dst_reg, src_reg));
492 EMIT2(0x89, add_2reg(0xC0, dst_reg, src_reg));
Alexei Starovoitov62258272014-05-13 19:50:46 -0700493 break;
Eric Dumazet3b589082013-01-30 17:51:44 -0800494
Alexei Starovoitove430f342014-06-06 14:46:06 -0700495 /* neg dst */
Alexei Starovoitov62258272014-05-13 19:50:46 -0700496 case BPF_ALU | BPF_NEG:
497 case BPF_ALU64 | BPF_NEG:
498 if (BPF_CLASS(insn->code) == BPF_ALU64)
Alexei Starovoitove430f342014-06-06 14:46:06 -0700499 EMIT1(add_1mod(0x48, dst_reg));
500 else if (is_ereg(dst_reg))
501 EMIT1(add_1mod(0x40, dst_reg));
502 EMIT2(0xF7, add_1reg(0xD8, dst_reg));
Alexei Starovoitov62258272014-05-13 19:50:46 -0700503 break;
504
505 case BPF_ALU | BPF_ADD | BPF_K:
506 case BPF_ALU | BPF_SUB | BPF_K:
507 case BPF_ALU | BPF_AND | BPF_K:
508 case BPF_ALU | BPF_OR | BPF_K:
509 case BPF_ALU | BPF_XOR | BPF_K:
510 case BPF_ALU64 | BPF_ADD | BPF_K:
511 case BPF_ALU64 | BPF_SUB | BPF_K:
512 case BPF_ALU64 | BPF_AND | BPF_K:
513 case BPF_ALU64 | BPF_OR | BPF_K:
514 case BPF_ALU64 | BPF_XOR | BPF_K:
515 if (BPF_CLASS(insn->code) == BPF_ALU64)
Alexei Starovoitove430f342014-06-06 14:46:06 -0700516 EMIT1(add_1mod(0x48, dst_reg));
517 else if (is_ereg(dst_reg))
518 EMIT1(add_1mod(0x40, dst_reg));
Alexei Starovoitov62258272014-05-13 19:50:46 -0700519
Daniel Borkmannde0a4442018-01-20 01:24:35 +0100520 /* b3 holds 'normal' opcode, b2 short form only valid
521 * in case dst is eax/rax.
522 */
Alexei Starovoitov62258272014-05-13 19:50:46 -0700523 switch (BPF_OP(insn->code)) {
Daniel Borkmannde0a4442018-01-20 01:24:35 +0100524 case BPF_ADD:
525 b3 = 0xC0;
526 b2 = 0x05;
527 break;
528 case BPF_SUB:
529 b3 = 0xE8;
530 b2 = 0x2D;
531 break;
532 case BPF_AND:
533 b3 = 0xE0;
534 b2 = 0x25;
535 break;
536 case BPF_OR:
537 b3 = 0xC8;
538 b2 = 0x0D;
539 break;
540 case BPF_XOR:
541 b3 = 0xF0;
542 b2 = 0x35;
543 break;
Alexei Starovoitov62258272014-05-13 19:50:46 -0700544 }
545
Alexei Starovoitove430f342014-06-06 14:46:06 -0700546 if (is_imm8(imm32))
547 EMIT3(0x83, add_1reg(b3, dst_reg), imm32);
Daniel Borkmannde0a4442018-01-20 01:24:35 +0100548 else if (is_axreg(dst_reg))
549 EMIT1_off32(b2, imm32);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700550 else
Alexei Starovoitove430f342014-06-06 14:46:06 -0700551 EMIT2_off32(0x81, add_1reg(b3, dst_reg), imm32);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700552 break;
553
554 case BPF_ALU64 | BPF_MOV | BPF_K:
Alexei Starovoitov62258272014-05-13 19:50:46 -0700555 case BPF_ALU | BPF_MOV | BPF_K:
Daniel Borkmann6fe8b9c2018-02-24 01:07:59 +0100556 emit_mov_imm32(&prog, BPF_CLASS(insn->code) == BPF_ALU64,
557 dst_reg, imm32);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700558 break;
559
Alexei Starovoitov02ab6952014-09-04 22:17:17 -0700560 case BPF_LD | BPF_IMM | BPF_DW:
Daniel Borkmann6fe8b9c2018-02-24 01:07:59 +0100561 emit_mov_imm64(&prog, dst_reg, insn[1].imm, insn[0].imm);
Alexei Starovoitov02ab6952014-09-04 22:17:17 -0700562 insn++;
563 i++;
564 break;
565
Alexei Starovoitove430f342014-06-06 14:46:06 -0700566 /* dst %= src, dst /= src, dst %= imm32, dst /= imm32 */
Alexei Starovoitov62258272014-05-13 19:50:46 -0700567 case BPF_ALU | BPF_MOD | BPF_X:
568 case BPF_ALU | BPF_DIV | BPF_X:
569 case BPF_ALU | BPF_MOD | BPF_K:
570 case BPF_ALU | BPF_DIV | BPF_K:
571 case BPF_ALU64 | BPF_MOD | BPF_X:
572 case BPF_ALU64 | BPF_DIV | BPF_X:
573 case BPF_ALU64 | BPF_MOD | BPF_K:
574 case BPF_ALU64 | BPF_DIV | BPF_K:
575 EMIT1(0x50); /* push rax */
576 EMIT1(0x52); /* push rdx */
577
578 if (BPF_SRC(insn->code) == BPF_X)
Alexei Starovoitove430f342014-06-06 14:46:06 -0700579 /* mov r11, src_reg */
580 EMIT_mov(AUX_REG, src_reg);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700581 else
Alexei Starovoitove430f342014-06-06 14:46:06 -0700582 /* mov r11, imm32 */
583 EMIT3_off32(0x49, 0xC7, 0xC3, imm32);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700584
Alexei Starovoitove430f342014-06-06 14:46:06 -0700585 /* mov rax, dst_reg */
586 EMIT_mov(BPF_REG_0, dst_reg);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700587
588 /* xor edx, edx
589 * equivalent to 'xor rdx, rdx', but one byte less
590 */
591 EMIT2(0x31, 0xd2);
592
Alexei Starovoitov62258272014-05-13 19:50:46 -0700593 if (BPF_CLASS(insn->code) == BPF_ALU64)
594 /* div r11 */
595 EMIT3(0x49, 0xF7, 0xF3);
596 else
597 /* div r11d */
598 EMIT3(0x41, 0xF7, 0xF3);
599
600 if (BPF_OP(insn->code) == BPF_MOD)
601 /* mov r11, rdx */
602 EMIT3(0x49, 0x89, 0xD3);
603 else
604 /* mov r11, rax */
605 EMIT3(0x49, 0x89, 0xC3);
606
607 EMIT1(0x5A); /* pop rdx */
608 EMIT1(0x58); /* pop rax */
609
Alexei Starovoitove430f342014-06-06 14:46:06 -0700610 /* mov dst_reg, r11 */
611 EMIT_mov(dst_reg, AUX_REG);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700612 break;
613
614 case BPF_ALU | BPF_MUL | BPF_K:
615 case BPF_ALU | BPF_MUL | BPF_X:
616 case BPF_ALU64 | BPF_MUL | BPF_K:
617 case BPF_ALU64 | BPF_MUL | BPF_X:
618 EMIT1(0x50); /* push rax */
619 EMIT1(0x52); /* push rdx */
620
Alexei Starovoitove430f342014-06-06 14:46:06 -0700621 /* mov r11, dst_reg */
622 EMIT_mov(AUX_REG, dst_reg);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700623
624 if (BPF_SRC(insn->code) == BPF_X)
Alexei Starovoitove430f342014-06-06 14:46:06 -0700625 /* mov rax, src_reg */
626 EMIT_mov(BPF_REG_0, src_reg);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700627 else
Alexei Starovoitove430f342014-06-06 14:46:06 -0700628 /* mov rax, imm32 */
Daniel Borkmann6fe8b9c2018-02-24 01:07:59 +0100629 emit_mov_imm32(&prog, true,
630 BPF_REG_0, imm32);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700631
632 if (BPF_CLASS(insn->code) == BPF_ALU64)
633 EMIT1(add_1mod(0x48, AUX_REG));
634 else if (is_ereg(AUX_REG))
635 EMIT1(add_1mod(0x40, AUX_REG));
636 /* mul(q) r11 */
637 EMIT2(0xF7, add_1reg(0xE0, AUX_REG));
638
639 /* mov r11, rax */
640 EMIT_mov(AUX_REG, BPF_REG_0);
641
642 EMIT1(0x5A); /* pop rdx */
643 EMIT1(0x58); /* pop rax */
644
Alexei Starovoitove430f342014-06-06 14:46:06 -0700645 /* mov dst_reg, r11 */
646 EMIT_mov(dst_reg, AUX_REG);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700647 break;
648
649 /* shifts */
650 case BPF_ALU | BPF_LSH | BPF_K:
651 case BPF_ALU | BPF_RSH | BPF_K:
652 case BPF_ALU | BPF_ARSH | BPF_K:
653 case BPF_ALU64 | BPF_LSH | BPF_K:
654 case BPF_ALU64 | BPF_RSH | BPF_K:
655 case BPF_ALU64 | BPF_ARSH | BPF_K:
656 if (BPF_CLASS(insn->code) == BPF_ALU64)
Alexei Starovoitove430f342014-06-06 14:46:06 -0700657 EMIT1(add_1mod(0x48, dst_reg));
658 else if (is_ereg(dst_reg))
659 EMIT1(add_1mod(0x40, dst_reg));
Alexei Starovoitov62258272014-05-13 19:50:46 -0700660
661 switch (BPF_OP(insn->code)) {
662 case BPF_LSH: b3 = 0xE0; break;
663 case BPF_RSH: b3 = 0xE8; break;
664 case BPF_ARSH: b3 = 0xF8; break;
665 }
Daniel Borkmann88e69a12018-02-24 01:07:58 +0100666
667 if (imm32 == 1)
668 EMIT2(0xD1, add_1reg(b3, dst_reg));
669 else
670 EMIT3(0xC1, add_1reg(b3, dst_reg), imm32);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700671 break;
672
Alexei Starovoitov72b603e2014-08-25 12:27:02 -0700673 case BPF_ALU | BPF_LSH | BPF_X:
674 case BPF_ALU | BPF_RSH | BPF_X:
675 case BPF_ALU | BPF_ARSH | BPF_X:
676 case BPF_ALU64 | BPF_LSH | BPF_X:
677 case BPF_ALU64 | BPF_RSH | BPF_X:
678 case BPF_ALU64 | BPF_ARSH | BPF_X:
679
680 /* check for bad case when dst_reg == rcx */
681 if (dst_reg == BPF_REG_4) {
682 /* mov r11, dst_reg */
683 EMIT_mov(AUX_REG, dst_reg);
684 dst_reg = AUX_REG;
685 }
686
687 if (src_reg != BPF_REG_4) { /* common case */
688 EMIT1(0x51); /* push rcx */
689
690 /* mov rcx, src_reg */
691 EMIT_mov(BPF_REG_4, src_reg);
692 }
693
694 /* shl %rax, %cl | shr %rax, %cl | sar %rax, %cl */
695 if (BPF_CLASS(insn->code) == BPF_ALU64)
696 EMIT1(add_1mod(0x48, dst_reg));
697 else if (is_ereg(dst_reg))
698 EMIT1(add_1mod(0x40, dst_reg));
699
700 switch (BPF_OP(insn->code)) {
701 case BPF_LSH: b3 = 0xE0; break;
702 case BPF_RSH: b3 = 0xE8; break;
703 case BPF_ARSH: b3 = 0xF8; break;
704 }
705 EMIT2(0xD3, add_1reg(b3, dst_reg));
706
707 if (src_reg != BPF_REG_4)
708 EMIT1(0x59); /* pop rcx */
709
710 if (insn->dst_reg == BPF_REG_4)
711 /* mov dst_reg, r11 */
712 EMIT_mov(insn->dst_reg, AUX_REG);
713 break;
714
Alexei Starovoitov62258272014-05-13 19:50:46 -0700715 case BPF_ALU | BPF_END | BPF_FROM_BE:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700716 switch (imm32) {
Alexei Starovoitov62258272014-05-13 19:50:46 -0700717 case 16:
718 /* emit 'ror %ax, 8' to swap lower 2 bytes */
719 EMIT1(0x66);
Alexei Starovoitove430f342014-06-06 14:46:06 -0700720 if (is_ereg(dst_reg))
Alexei Starovoitov62258272014-05-13 19:50:46 -0700721 EMIT1(0x41);
Alexei Starovoitove430f342014-06-06 14:46:06 -0700722 EMIT3(0xC1, add_1reg(0xC8, dst_reg), 8);
Alexei Starovoitov343f8452015-05-11 23:25:16 -0700723
724 /* emit 'movzwl eax, ax' */
725 if (is_ereg(dst_reg))
726 EMIT3(0x45, 0x0F, 0xB7);
727 else
728 EMIT2(0x0F, 0xB7);
729 EMIT1(add_2reg(0xC0, dst_reg, dst_reg));
Eric Dumazet0a148422011-04-20 09:27:32 +0000730 break;
Alexei Starovoitov62258272014-05-13 19:50:46 -0700731 case 32:
732 /* emit 'bswap eax' to swap lower 4 bytes */
Alexei Starovoitove430f342014-06-06 14:46:06 -0700733 if (is_ereg(dst_reg))
Alexei Starovoitov62258272014-05-13 19:50:46 -0700734 EMIT2(0x41, 0x0F);
735 else
736 EMIT1(0x0F);
Alexei Starovoitove430f342014-06-06 14:46:06 -0700737 EMIT1(add_1reg(0xC8, dst_reg));
Eric Dumazet0a148422011-04-20 09:27:32 +0000738 break;
Alexei Starovoitov62258272014-05-13 19:50:46 -0700739 case 64:
740 /* emit 'bswap rax' to swap 8 bytes */
Alexei Starovoitove430f342014-06-06 14:46:06 -0700741 EMIT3(add_1mod(0x48, dst_reg), 0x0F,
742 add_1reg(0xC8, dst_reg));
Alexei Starovoitov62258272014-05-13 19:50:46 -0700743 break;
744 }
745 break;
746
747 case BPF_ALU | BPF_END | BPF_FROM_LE:
Alexei Starovoitov343f8452015-05-11 23:25:16 -0700748 switch (imm32) {
749 case 16:
750 /* emit 'movzwl eax, ax' to zero extend 16-bit
751 * into 64 bit
752 */
753 if (is_ereg(dst_reg))
754 EMIT3(0x45, 0x0F, 0xB7);
755 else
756 EMIT2(0x0F, 0xB7);
757 EMIT1(add_2reg(0xC0, dst_reg, dst_reg));
758 break;
759 case 32:
760 /* emit 'mov eax, eax' to clear upper 32-bits */
761 if (is_ereg(dst_reg))
762 EMIT1(0x45);
763 EMIT2(0x89, add_2reg(0xC0, dst_reg, dst_reg));
764 break;
765 case 64:
766 /* nop */
767 break;
768 }
Alexei Starovoitov62258272014-05-13 19:50:46 -0700769 break;
770
Alexei Starovoitove430f342014-06-06 14:46:06 -0700771 /* ST: *(u8*)(dst_reg + off) = imm */
Alexei Starovoitov62258272014-05-13 19:50:46 -0700772 case BPF_ST | BPF_MEM | BPF_B:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700773 if (is_ereg(dst_reg))
Alexei Starovoitov62258272014-05-13 19:50:46 -0700774 EMIT2(0x41, 0xC6);
775 else
776 EMIT1(0xC6);
777 goto st;
778 case BPF_ST | BPF_MEM | BPF_H:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700779 if (is_ereg(dst_reg))
Alexei Starovoitov62258272014-05-13 19:50:46 -0700780 EMIT3(0x66, 0x41, 0xC7);
781 else
782 EMIT2(0x66, 0xC7);
783 goto st;
784 case BPF_ST | BPF_MEM | BPF_W:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700785 if (is_ereg(dst_reg))
Alexei Starovoitov62258272014-05-13 19:50:46 -0700786 EMIT2(0x41, 0xC7);
787 else
788 EMIT1(0xC7);
789 goto st;
790 case BPF_ST | BPF_MEM | BPF_DW:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700791 EMIT2(add_1mod(0x48, dst_reg), 0xC7);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700792
793st: if (is_imm8(insn->off))
Alexei Starovoitove430f342014-06-06 14:46:06 -0700794 EMIT2(add_1reg(0x40, dst_reg), insn->off);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700795 else
Alexei Starovoitove430f342014-06-06 14:46:06 -0700796 EMIT1_off32(add_1reg(0x80, dst_reg), insn->off);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700797
Alexei Starovoitove430f342014-06-06 14:46:06 -0700798 EMIT(imm32, bpf_size_to_x86_bytes(BPF_SIZE(insn->code)));
Alexei Starovoitov62258272014-05-13 19:50:46 -0700799 break;
800
Alexei Starovoitove430f342014-06-06 14:46:06 -0700801 /* STX: *(u8*)(dst_reg + off) = src_reg */
Alexei Starovoitov62258272014-05-13 19:50:46 -0700802 case BPF_STX | BPF_MEM | BPF_B:
803 /* emit 'mov byte ptr [rax + off], al' */
Alexei Starovoitove430f342014-06-06 14:46:06 -0700804 if (is_ereg(dst_reg) || is_ereg(src_reg) ||
Alexei Starovoitov62258272014-05-13 19:50:46 -0700805 /* have to add extra byte for x86 SIL, DIL regs */
Alexei Starovoitove430f342014-06-06 14:46:06 -0700806 src_reg == BPF_REG_1 || src_reg == BPF_REG_2)
807 EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x88);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700808 else
809 EMIT1(0x88);
810 goto stx;
811 case BPF_STX | BPF_MEM | BPF_H:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700812 if (is_ereg(dst_reg) || is_ereg(src_reg))
813 EMIT3(0x66, add_2mod(0x40, dst_reg, src_reg), 0x89);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700814 else
815 EMIT2(0x66, 0x89);
816 goto stx;
817 case BPF_STX | BPF_MEM | BPF_W:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700818 if (is_ereg(dst_reg) || is_ereg(src_reg))
819 EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x89);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700820 else
821 EMIT1(0x89);
822 goto stx;
823 case BPF_STX | BPF_MEM | BPF_DW:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700824 EMIT2(add_2mod(0x48, dst_reg, src_reg), 0x89);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700825stx: if (is_imm8(insn->off))
Alexei Starovoitove430f342014-06-06 14:46:06 -0700826 EMIT2(add_2reg(0x40, dst_reg, src_reg), insn->off);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700827 else
Alexei Starovoitove430f342014-06-06 14:46:06 -0700828 EMIT1_off32(add_2reg(0x80, dst_reg, src_reg),
Alexei Starovoitov62258272014-05-13 19:50:46 -0700829 insn->off);
830 break;
831
Alexei Starovoitove430f342014-06-06 14:46:06 -0700832 /* LDX: dst_reg = *(u8*)(src_reg + off) */
Alexei Starovoitov62258272014-05-13 19:50:46 -0700833 case BPF_LDX | BPF_MEM | BPF_B:
834 /* emit 'movzx rax, byte ptr [rax + off]' */
Alexei Starovoitove430f342014-06-06 14:46:06 -0700835 EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB6);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700836 goto ldx;
837 case BPF_LDX | BPF_MEM | BPF_H:
838 /* emit 'movzx rax, word ptr [rax + off]' */
Alexei Starovoitove430f342014-06-06 14:46:06 -0700839 EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB7);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700840 goto ldx;
841 case BPF_LDX | BPF_MEM | BPF_W:
842 /* emit 'mov eax, dword ptr [rax+0x14]' */
Alexei Starovoitove430f342014-06-06 14:46:06 -0700843 if (is_ereg(dst_reg) || is_ereg(src_reg))
844 EMIT2(add_2mod(0x40, src_reg, dst_reg), 0x8B);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700845 else
846 EMIT1(0x8B);
847 goto ldx;
848 case BPF_LDX | BPF_MEM | BPF_DW:
849 /* emit 'mov rax, qword ptr [rax+0x14]' */
Alexei Starovoitove430f342014-06-06 14:46:06 -0700850 EMIT2(add_2mod(0x48, src_reg, dst_reg), 0x8B);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700851ldx: /* if insn->off == 0 we can save one extra byte, but
852 * special case of x86 r13 which always needs an offset
853 * is not worth the hassle
854 */
855 if (is_imm8(insn->off))
Alexei Starovoitove430f342014-06-06 14:46:06 -0700856 EMIT2(add_2reg(0x40, src_reg, dst_reg), insn->off);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700857 else
Alexei Starovoitove430f342014-06-06 14:46:06 -0700858 EMIT1_off32(add_2reg(0x80, src_reg, dst_reg),
Alexei Starovoitov62258272014-05-13 19:50:46 -0700859 insn->off);
860 break;
861
Alexei Starovoitove430f342014-06-06 14:46:06 -0700862 /* STX XADD: lock *(u32*)(dst_reg + off) += src_reg */
Alexei Starovoitov62258272014-05-13 19:50:46 -0700863 case BPF_STX | BPF_XADD | BPF_W:
864 /* emit 'lock add dword ptr [rax + off], eax' */
Alexei Starovoitove430f342014-06-06 14:46:06 -0700865 if (is_ereg(dst_reg) || is_ereg(src_reg))
866 EMIT3(0xF0, add_2mod(0x40, dst_reg, src_reg), 0x01);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700867 else
868 EMIT2(0xF0, 0x01);
869 goto xadd;
870 case BPF_STX | BPF_XADD | BPF_DW:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700871 EMIT3(0xF0, add_2mod(0x48, dst_reg, src_reg), 0x01);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700872xadd: if (is_imm8(insn->off))
Alexei Starovoitove430f342014-06-06 14:46:06 -0700873 EMIT2(add_2reg(0x40, dst_reg, src_reg), insn->off);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700874 else
Alexei Starovoitove430f342014-06-06 14:46:06 -0700875 EMIT1_off32(add_2reg(0x80, dst_reg, src_reg),
Alexei Starovoitov62258272014-05-13 19:50:46 -0700876 insn->off);
877 break;
878
879 /* call */
880 case BPF_JMP | BPF_CALL:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700881 func = (u8 *) __bpf_call_base + imm32;
Alexei Starovoitov62258272014-05-13 19:50:46 -0700882 jmp_offset = func - (image + addrs[i]);
Alexei Starovoitove0ee9c12014-10-10 20:30:23 -0700883 if (seen_ld_abs) {
Martin KaFai Lau17bedab2016-12-07 15:53:11 -0800884 reload_skb_data = bpf_helper_changes_pkt_data(func);
Alexei Starovoitov4e10df92015-07-20 20:34:18 -0700885 if (reload_skb_data) {
886 EMIT1(0x57); /* push %rdi */
887 jmp_offset += 22; /* pop, mov, sub, mov */
888 } else {
889 EMIT2(0x41, 0x52); /* push %r10 */
890 EMIT2(0x41, 0x51); /* push %r9 */
891 /* need to adjust jmp offset, since
892 * pop %r9, pop %r10 take 4 bytes after call insn
893 */
894 jmp_offset += 4;
895 }
Alexei Starovoitov62258272014-05-13 19:50:46 -0700896 }
Alexei Starovoitove430f342014-06-06 14:46:06 -0700897 if (!imm32 || !is_simm32(jmp_offset)) {
Alexei Starovoitov62258272014-05-13 19:50:46 -0700898 pr_err("unsupported bpf func %d addr %p image %p\n",
Alexei Starovoitove430f342014-06-06 14:46:06 -0700899 imm32, func, image);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700900 return -EINVAL;
901 }
902 EMIT1_off32(0xE8, jmp_offset);
Alexei Starovoitove0ee9c12014-10-10 20:30:23 -0700903 if (seen_ld_abs) {
Alexei Starovoitov4e10df92015-07-20 20:34:18 -0700904 if (reload_skb_data) {
905 EMIT1(0x5F); /* pop %rdi */
906 emit_load_skb_data_hlen(&prog);
907 } else {
908 EMIT2(0x41, 0x59); /* pop %r9 */
909 EMIT2(0x41, 0x5A); /* pop %r10 */
910 }
Alexei Starovoitov62258272014-05-13 19:50:46 -0700911 }
912 break;
913
Alexei Starovoitov71189fa2017-05-30 13:31:27 -0700914 case BPF_JMP | BPF_TAIL_CALL:
Alexei Starovoitovb52f00e2015-05-19 16:59:04 -0700915 emit_bpf_tail_call(&prog);
916 break;
917
Alexei Starovoitov62258272014-05-13 19:50:46 -0700918 /* cond jump */
919 case BPF_JMP | BPF_JEQ | BPF_X:
920 case BPF_JMP | BPF_JNE | BPF_X:
921 case BPF_JMP | BPF_JGT | BPF_X:
Daniel Borkmann52afc512017-08-10 01:39:56 +0200922 case BPF_JMP | BPF_JLT | BPF_X:
Alexei Starovoitov62258272014-05-13 19:50:46 -0700923 case BPF_JMP | BPF_JGE | BPF_X:
Daniel Borkmann52afc512017-08-10 01:39:56 +0200924 case BPF_JMP | BPF_JLE | BPF_X:
Alexei Starovoitov62258272014-05-13 19:50:46 -0700925 case BPF_JMP | BPF_JSGT | BPF_X:
Daniel Borkmann52afc512017-08-10 01:39:56 +0200926 case BPF_JMP | BPF_JSLT | BPF_X:
Alexei Starovoitov62258272014-05-13 19:50:46 -0700927 case BPF_JMP | BPF_JSGE | BPF_X:
Daniel Borkmann52afc512017-08-10 01:39:56 +0200928 case BPF_JMP | BPF_JSLE | BPF_X:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700929 /* cmp dst_reg, src_reg */
930 EMIT3(add_2mod(0x48, dst_reg, src_reg), 0x39,
931 add_2reg(0xC0, dst_reg, src_reg));
Alexei Starovoitov62258272014-05-13 19:50:46 -0700932 goto emit_cond_jmp;
933
934 case BPF_JMP | BPF_JSET | BPF_X:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700935 /* test dst_reg, src_reg */
936 EMIT3(add_2mod(0x48, dst_reg, src_reg), 0x85,
937 add_2reg(0xC0, dst_reg, src_reg));
Alexei Starovoitov62258272014-05-13 19:50:46 -0700938 goto emit_cond_jmp;
939
940 case BPF_JMP | BPF_JSET | BPF_K:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700941 /* test dst_reg, imm32 */
942 EMIT1(add_1mod(0x48, dst_reg));
943 EMIT2_off32(0xF7, add_1reg(0xC0, dst_reg), imm32);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700944 goto emit_cond_jmp;
945
946 case BPF_JMP | BPF_JEQ | BPF_K:
947 case BPF_JMP | BPF_JNE | BPF_K:
948 case BPF_JMP | BPF_JGT | BPF_K:
Daniel Borkmann52afc512017-08-10 01:39:56 +0200949 case BPF_JMP | BPF_JLT | BPF_K:
Alexei Starovoitov62258272014-05-13 19:50:46 -0700950 case BPF_JMP | BPF_JGE | BPF_K:
Daniel Borkmann52afc512017-08-10 01:39:56 +0200951 case BPF_JMP | BPF_JLE | BPF_K:
Alexei Starovoitov62258272014-05-13 19:50:46 -0700952 case BPF_JMP | BPF_JSGT | BPF_K:
Daniel Borkmann52afc512017-08-10 01:39:56 +0200953 case BPF_JMP | BPF_JSLT | BPF_K:
Alexei Starovoitov62258272014-05-13 19:50:46 -0700954 case BPF_JMP | BPF_JSGE | BPF_K:
Daniel Borkmann52afc512017-08-10 01:39:56 +0200955 case BPF_JMP | BPF_JSLE | BPF_K:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700956 /* cmp dst_reg, imm8/32 */
957 EMIT1(add_1mod(0x48, dst_reg));
Alexei Starovoitov62258272014-05-13 19:50:46 -0700958
Alexei Starovoitove430f342014-06-06 14:46:06 -0700959 if (is_imm8(imm32))
960 EMIT3(0x83, add_1reg(0xF8, dst_reg), imm32);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700961 else
Alexei Starovoitove430f342014-06-06 14:46:06 -0700962 EMIT2_off32(0x81, add_1reg(0xF8, dst_reg), imm32);
Alexei Starovoitov62258272014-05-13 19:50:46 -0700963
964emit_cond_jmp: /* convert BPF opcode to x86 */
965 switch (BPF_OP(insn->code)) {
966 case BPF_JEQ:
967 jmp_cond = X86_JE;
968 break;
969 case BPF_JSET:
970 case BPF_JNE:
971 jmp_cond = X86_JNE;
972 break;
973 case BPF_JGT:
974 /* GT is unsigned '>', JA in x86 */
975 jmp_cond = X86_JA;
976 break;
Daniel Borkmann52afc512017-08-10 01:39:56 +0200977 case BPF_JLT:
978 /* LT is unsigned '<', JB in x86 */
979 jmp_cond = X86_JB;
980 break;
Alexei Starovoitov62258272014-05-13 19:50:46 -0700981 case BPF_JGE:
982 /* GE is unsigned '>=', JAE in x86 */
983 jmp_cond = X86_JAE;
984 break;
Daniel Borkmann52afc512017-08-10 01:39:56 +0200985 case BPF_JLE:
986 /* LE is unsigned '<=', JBE in x86 */
987 jmp_cond = X86_JBE;
988 break;
Alexei Starovoitov62258272014-05-13 19:50:46 -0700989 case BPF_JSGT:
990 /* signed '>', GT in x86 */
991 jmp_cond = X86_JG;
992 break;
Daniel Borkmann52afc512017-08-10 01:39:56 +0200993 case BPF_JSLT:
994 /* signed '<', LT in x86 */
995 jmp_cond = X86_JL;
996 break;
Alexei Starovoitov62258272014-05-13 19:50:46 -0700997 case BPF_JSGE:
998 /* signed '>=', GE in x86 */
999 jmp_cond = X86_JGE;
1000 break;
Daniel Borkmann52afc512017-08-10 01:39:56 +02001001 case BPF_JSLE:
1002 /* signed '<=', LE in x86 */
1003 jmp_cond = X86_JLE;
1004 break;
Alexei Starovoitov62258272014-05-13 19:50:46 -07001005 default: /* to silence gcc warning */
1006 return -EFAULT;
1007 }
1008 jmp_offset = addrs[i + insn->off] - addrs[i];
1009 if (is_imm8(jmp_offset)) {
1010 EMIT2(jmp_cond, jmp_offset);
1011 } else if (is_simm32(jmp_offset)) {
1012 EMIT2_off32(0x0F, jmp_cond + 0x10, jmp_offset);
1013 } else {
1014 pr_err("cond_jmp gen bug %llx\n", jmp_offset);
1015 return -EFAULT;
1016 }
1017
1018 break;
1019
1020 case BPF_JMP | BPF_JA:
1021 jmp_offset = addrs[i + insn->off] - addrs[i];
1022 if (!jmp_offset)
1023 /* optimize out nop jumps */
1024 break;
1025emit_jmp:
1026 if (is_imm8(jmp_offset)) {
1027 EMIT2(0xEB, jmp_offset);
1028 } else if (is_simm32(jmp_offset)) {
1029 EMIT1_off32(0xE9, jmp_offset);
1030 } else {
1031 pr_err("jmp gen bug %llx\n", jmp_offset);
1032 return -EFAULT;
1033 }
1034 break;
1035
1036 case BPF_LD | BPF_IND | BPF_W:
1037 func = sk_load_word;
1038 goto common_load;
1039 case BPF_LD | BPF_ABS | BPF_W:
Alexei Starovoitove430f342014-06-06 14:46:06 -07001040 func = CHOOSE_LOAD_FUNC(imm32, sk_load_word);
Alexei Starovoitove0ee9c12014-10-10 20:30:23 -07001041common_load:
1042 ctx->seen_ld_abs = seen_ld_abs = true;
Alexei Starovoitov62258272014-05-13 19:50:46 -07001043 jmp_offset = func - (image + addrs[i]);
1044 if (!func || !is_simm32(jmp_offset)) {
1045 pr_err("unsupported bpf func %d addr %p image %p\n",
Alexei Starovoitove430f342014-06-06 14:46:06 -07001046 imm32, func, image);
Alexei Starovoitov62258272014-05-13 19:50:46 -07001047 return -EINVAL;
1048 }
1049 if (BPF_MODE(insn->code) == BPF_ABS) {
1050 /* mov %esi, imm32 */
Alexei Starovoitove430f342014-06-06 14:46:06 -07001051 EMIT1_off32(0xBE, imm32);
Alexei Starovoitov62258272014-05-13 19:50:46 -07001052 } else {
Alexei Starovoitove430f342014-06-06 14:46:06 -07001053 /* mov %rsi, src_reg */
1054 EMIT_mov(BPF_REG_2, src_reg);
1055 if (imm32) {
1056 if (is_imm8(imm32))
Alexei Starovoitov62258272014-05-13 19:50:46 -07001057 /* add %esi, imm8 */
Alexei Starovoitove430f342014-06-06 14:46:06 -07001058 EMIT3(0x83, 0xC6, imm32);
Eric Dumazet0a148422011-04-20 09:27:32 +00001059 else
Alexei Starovoitov62258272014-05-13 19:50:46 -07001060 /* add %esi, imm32 */
Alexei Starovoitove430f342014-06-06 14:46:06 -07001061 EMIT2_off32(0x81, 0xC6, imm32);
Eric Dumazet0a148422011-04-20 09:27:32 +00001062 }
Alexei Starovoitov62258272014-05-13 19:50:46 -07001063 }
1064 /* skb pointer is in R6 (%rbx), it will be copied into
1065 * %rdi if skb_copy_bits() call is necessary.
1066 * sk_load_* helpers also use %r10 and %r9d.
1067 * See bpf_jit.S
1068 */
Daniel Borkmann959a7572016-05-13 19:08:33 +02001069 if (seen_ax_reg)
1070 /* r10 = skb->data, mov %r10, off32(%rbx) */
1071 EMIT3_off32(0x4c, 0x8b, 0x93,
1072 offsetof(struct sk_buff, data));
Alexei Starovoitov62258272014-05-13 19:50:46 -07001073 EMIT1_off32(0xE8, jmp_offset); /* call */
1074 break;
1075
1076 case BPF_LD | BPF_IND | BPF_H:
1077 func = sk_load_half;
1078 goto common_load;
1079 case BPF_LD | BPF_ABS | BPF_H:
Alexei Starovoitove430f342014-06-06 14:46:06 -07001080 func = CHOOSE_LOAD_FUNC(imm32, sk_load_half);
Alexei Starovoitov62258272014-05-13 19:50:46 -07001081 goto common_load;
1082 case BPF_LD | BPF_IND | BPF_B:
1083 func = sk_load_byte;
1084 goto common_load;
1085 case BPF_LD | BPF_ABS | BPF_B:
Alexei Starovoitove430f342014-06-06 14:46:06 -07001086 func = CHOOSE_LOAD_FUNC(imm32, sk_load_byte);
Alexei Starovoitov62258272014-05-13 19:50:46 -07001087 goto common_load;
1088
1089 case BPF_JMP | BPF_EXIT:
Alexei Starovoitov769e0de2014-11-29 14:46:13 -08001090 if (seen_exit) {
Alexei Starovoitov62258272014-05-13 19:50:46 -07001091 jmp_offset = ctx->cleanup_addr - addrs[i];
1092 goto emit_jmp;
1093 }
Alexei Starovoitov769e0de2014-11-29 14:46:13 -08001094 seen_exit = true;
Alexei Starovoitov62258272014-05-13 19:50:46 -07001095 /* update cleanup_addr */
1096 ctx->cleanup_addr = proglen;
Alexei Starovoitov177366b2017-05-30 13:31:34 -07001097 /* mov rbx, qword ptr [rbp+0] */
1098 EMIT4(0x48, 0x8B, 0x5D, 0);
1099 /* mov r13, qword ptr [rbp+8] */
1100 EMIT4(0x4C, 0x8B, 0x6D, 8);
1101 /* mov r14, qword ptr [rbp+16] */
1102 EMIT4(0x4C, 0x8B, 0x75, 16);
1103 /* mov r15, qword ptr [rbp+24] */
1104 EMIT4(0x4C, 0x8B, 0x7D, 24);
Alexei Starovoitov62258272014-05-13 19:50:46 -07001105
Alexei Starovoitov177366b2017-05-30 13:31:34 -07001106 /* add rbp, AUX_STACK_SPACE */
1107 EMIT4(0x48, 0x83, 0xC5, AUX_STACK_SPACE);
Alexei Starovoitov62258272014-05-13 19:50:46 -07001108 EMIT1(0xC9); /* leave */
1109 EMIT1(0xC3); /* ret */
1110 break;
1111
Alexei Starovoitovf3c2af72014-05-13 19:50:45 -07001112 default:
Alexei Starovoitov62258272014-05-13 19:50:46 -07001113 /* By design x64 JIT should support all BPF instructions
1114 * This error will be seen if new instruction was added
1115 * to interpreter, but not to JIT
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001116 * or if there is junk in bpf_prog
Alexei Starovoitov62258272014-05-13 19:50:46 -07001117 */
1118 pr_err("bpf_jit: unknown opcode %02x\n", insn->code);
Alexei Starovoitovf3c2af72014-05-13 19:50:45 -07001119 return -EINVAL;
Eric Dumazet0a148422011-04-20 09:27:32 +00001120 }
Alexei Starovoitov62258272014-05-13 19:50:46 -07001121
Alexei Starovoitovf3c2af72014-05-13 19:50:45 -07001122 ilen = prog - temp;
Alexei Starovoitove0ee9c12014-10-10 20:30:23 -07001123 if (ilen > BPF_MAX_INSN_SIZE) {
Daniel Borkmann93831912017-02-16 22:24:49 +01001124 pr_err("bpf_jit: fatal insn size error\n");
Alexei Starovoitove0ee9c12014-10-10 20:30:23 -07001125 return -EFAULT;
1126 }
1127
Alexei Starovoitovf3c2af72014-05-13 19:50:45 -07001128 if (image) {
1129 if (unlikely(proglen + ilen > oldproglen)) {
Daniel Borkmann93831912017-02-16 22:24:49 +01001130 pr_err("bpf_jit: fatal error\n");
Alexei Starovoitovf3c2af72014-05-13 19:50:45 -07001131 return -EFAULT;
1132 }
1133 memcpy(image + proglen, temp, ilen);
1134 }
1135 proglen += ilen;
1136 addrs[i] = proglen;
1137 prog = temp;
1138 }
Alexei Starovoitovf3c2af72014-05-13 19:50:45 -07001139 return proglen;
1140}
1141
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001142struct x64_jit_data {
1143 struct bpf_binary_header *header;
1144 int *addrs;
1145 u8 *image;
1146 int proglen;
1147 struct jit_context ctx;
1148};
1149
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001150struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
Alexei Starovoitov62258272014-05-13 19:50:46 -07001151{
Alexei Starovoitovf3c2af72014-05-13 19:50:45 -07001152 struct bpf_binary_header *header = NULL;
Daniel Borkmann959a7572016-05-13 19:08:33 +02001153 struct bpf_prog *tmp, *orig_prog = prog;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001154 struct x64_jit_data *jit_data;
Alexei Starovoitovf3c2af72014-05-13 19:50:45 -07001155 int proglen, oldproglen = 0;
1156 struct jit_context ctx = {};
Daniel Borkmann959a7572016-05-13 19:08:33 +02001157 bool tmp_blinded = false;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001158 bool extra_pass = false;
Alexei Starovoitovf3c2af72014-05-13 19:50:45 -07001159 u8 *image = NULL;
1160 int *addrs;
1161 int pass;
1162 int i;
1163
Alexei Starovoitov60b58afc2017-12-14 17:55:14 -08001164 if (!prog->jit_requested)
Daniel Borkmann959a7572016-05-13 19:08:33 +02001165 return orig_prog;
1166
1167 tmp = bpf_jit_blind_constants(prog);
1168 /* If blinding was requested and we failed during blinding,
1169 * we must fall back to the interpreter.
1170 */
1171 if (IS_ERR(tmp))
1172 return orig_prog;
1173 if (tmp != prog) {
1174 tmp_blinded = true;
1175 prog = tmp;
1176 }
Alexei Starovoitovf3c2af72014-05-13 19:50:45 -07001177
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001178 jit_data = prog->aux->jit_data;
1179 if (!jit_data) {
1180 jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
1181 if (!jit_data) {
1182 prog = orig_prog;
1183 goto out;
1184 }
1185 prog->aux->jit_data = jit_data;
1186 }
1187 addrs = jit_data->addrs;
1188 if (addrs) {
1189 ctx = jit_data->ctx;
1190 oldproglen = jit_data->proglen;
1191 image = jit_data->image;
1192 header = jit_data->header;
1193 extra_pass = true;
1194 goto skip_init_addrs;
1195 }
Alexei Starovoitovf3c2af72014-05-13 19:50:45 -07001196 addrs = kmalloc(prog->len * sizeof(*addrs), GFP_KERNEL);
Daniel Borkmann959a7572016-05-13 19:08:33 +02001197 if (!addrs) {
1198 prog = orig_prog;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001199 goto out_addrs;
Daniel Borkmann959a7572016-05-13 19:08:33 +02001200 }
Alexei Starovoitovf3c2af72014-05-13 19:50:45 -07001201
1202 /* Before first pass, make a rough estimation of addrs[]
1203 * each bpf instruction is translated to less than 64 bytes
1204 */
1205 for (proglen = 0, i = 0; i < prog->len; i++) {
1206 proglen += 64;
1207 addrs[i] = proglen;
1208 }
1209 ctx.cleanup_addr = proglen;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001210skip_init_addrs:
Alexei Starovoitovf3c2af72014-05-13 19:50:45 -07001211
Alexei Starovoitov3f7352b2015-05-22 15:42:55 -07001212 /* JITed image shrinks with every pass and the loop iterates
1213 * until the image stops shrinking. Very large bpf programs
1214 * may converge on the last pass. In such case do one more
1215 * pass to emit the final image
1216 */
1217 for (pass = 0; pass < 10 || image; pass++) {
Alexei Starovoitovf3c2af72014-05-13 19:50:45 -07001218 proglen = do_jit(prog, addrs, image, oldproglen, &ctx);
1219 if (proglen <= 0) {
1220 image = NULL;
1221 if (header)
Daniel Borkmann738cbe72014-09-08 08:04:47 +02001222 bpf_jit_binary_free(header);
Daniel Borkmann959a7572016-05-13 19:08:33 +02001223 prog = orig_prog;
1224 goto out_addrs;
Alexei Starovoitovf3c2af72014-05-13 19:50:45 -07001225 }
Eric Dumazet0a148422011-04-20 09:27:32 +00001226 if (image) {
Alexei Starovoitove0ee9c12014-10-10 20:30:23 -07001227 if (proglen != oldproglen) {
Alexei Starovoitovf3c2af72014-05-13 19:50:45 -07001228 pr_err("bpf_jit: proglen=%d != oldproglen=%d\n",
1229 proglen, oldproglen);
Daniel Borkmann959a7572016-05-13 19:08:33 +02001230 prog = orig_prog;
1231 goto out_addrs;
Alexei Starovoitove0ee9c12014-10-10 20:30:23 -07001232 }
Eric Dumazet0a148422011-04-20 09:27:32 +00001233 break;
1234 }
1235 if (proglen == oldproglen) {
Daniel Borkmann738cbe72014-09-08 08:04:47 +02001236 header = bpf_jit_binary_alloc(proglen, &image,
1237 1, jit_fill_hole);
Daniel Borkmann959a7572016-05-13 19:08:33 +02001238 if (!header) {
1239 prog = orig_prog;
1240 goto out_addrs;
1241 }
Eric Dumazet0a148422011-04-20 09:27:32 +00001242 }
1243 oldproglen = proglen;
1244 }
Daniel Borkmann79617802013-03-21 22:22:03 +01001245
Eric Dumazet0a148422011-04-20 09:27:32 +00001246 if (bpf_jit_enable > 1)
Daniel Borkmann485d6512015-07-30 12:42:48 +02001247 bpf_jit_dump(prog->len, proglen, pass + 1, image);
Eric Dumazet0a148422011-04-20 09:27:32 +00001248
1249 if (image) {
Eric Dumazet314beb92013-05-17 16:37:03 +00001250 bpf_flush_icache(header, image + proglen);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001251 if (!prog->is_func || extra_pass) {
1252 bpf_jit_binary_lock_ro(header);
1253 } else {
1254 jit_data->addrs = addrs;
1255 jit_data->ctx = ctx;
1256 jit_data->proglen = proglen;
1257 jit_data->image = image;
1258 jit_data->header = header;
1259 }
Alexei Starovoitovf3c2af72014-05-13 19:50:45 -07001260 prog->bpf_func = (void *)image;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001261 prog->jited = 1;
Martin KaFai Lau783d28dd12017-06-05 12:15:51 -07001262 prog->jited_len = proglen;
Daniel Borkmann9d5ecb02017-01-07 00:26:33 +01001263 } else {
1264 prog = orig_prog;
Eric Dumazet0a148422011-04-20 09:27:32 +00001265 }
Daniel Borkmann959a7572016-05-13 19:08:33 +02001266
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001267 if (!prog->is_func || extra_pass) {
Daniel Borkmann959a7572016-05-13 19:08:33 +02001268out_addrs:
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001269 kfree(addrs);
1270 kfree(jit_data);
1271 prog->aux->jit_data = NULL;
1272 }
Daniel Borkmann959a7572016-05-13 19:08:33 +02001273out:
1274 if (tmp_blinded)
1275 bpf_jit_prog_release_other(prog, prog == orig_prog ?
1276 tmp : orig_prog);
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001277 return prog;
Eric Dumazet0a148422011-04-20 09:27:32 +00001278}