]> git.ipfire.org Git - people/ms/linux.git/blame - arch/x86/net/bpf_jit_comp.c
bpf: Move BPF_STX reserved field check into BPF_STX verifier code
[people/ms/linux.git] / arch / x86 / net / bpf_jit_comp.c
CommitLineData
b886d83c 1// SPDX-License-Identifier: GPL-2.0-only
a2c7a983
IM
2/*
3 * bpf_jit_comp.c: BPF JIT compiler
0a14842f 4 *
3b58908a 5 * Copyright (C) 2011-2013 Eric Dumazet (eric.dumazet@gmail.com)
62258278 6 * Internal BPF Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
0a14842f 7 */
0a14842f
ED
8#include <linux/netdevice.h>
9#include <linux/filter.h>
855ddb56 10#include <linux/if_vlan.h>
71d22d58 11#include <linux/bpf.h>
5964b200 12#include <linux/memory.h>
75ccbef6 13#include <linux/sort.h>
3dec541b 14#include <asm/extable.h>
d1163651 15#include <asm/set_memory.h>
a493a87f 16#include <asm/nospec-branch.h>
5964b200 17#include <asm/text-patching.h>
75ccbef6 18#include <asm/asm-prototypes.h>
0a14842f 19
5cccc702 20static u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len)
0a14842f
ED
21{
22 if (len == 1)
23 *ptr = bytes;
24 else if (len == 2)
25 *(u16 *)ptr = bytes;
26 else {
27 *(u32 *)ptr = bytes;
28 barrier();
29 }
30 return ptr + len;
31}
32
b52f00e6
AS
33#define EMIT(bytes, len) \
34 do { prog = emit_code(prog, bytes, len); cnt += len; } while (0)
0a14842f
ED
35
36#define EMIT1(b1) EMIT(b1, 1)
37#define EMIT2(b1, b2) EMIT((b1) + ((b2) << 8), 2)
38#define EMIT3(b1, b2, b3) EMIT((b1) + ((b2) << 8) + ((b3) << 16), 3)
39#define EMIT4(b1, b2, b3, b4) EMIT((b1) + ((b2) << 8) + ((b3) << 16) + ((b4) << 24), 4)
a2c7a983 40
62258278 41#define EMIT1_off32(b1, off) \
a2c7a983 42 do { EMIT1(b1); EMIT(off, 4); } while (0)
62258278 43#define EMIT2_off32(b1, b2, off) \
a2c7a983 44 do { EMIT2(b1, b2); EMIT(off, 4); } while (0)
62258278 45#define EMIT3_off32(b1, b2, b3, off) \
a2c7a983 46 do { EMIT3(b1, b2, b3); EMIT(off, 4); } while (0)
62258278 47#define EMIT4_off32(b1, b2, b3, b4, off) \
a2c7a983 48 do { EMIT4(b1, b2, b3, b4); EMIT(off, 4); } while (0)
0a14842f 49
5cccc702 50static bool is_imm8(int value)
0a14842f
ED
51{
52 return value <= 127 && value >= -128;
53}
54
5cccc702 55static bool is_simm32(s64 value)
0a14842f 56{
6fe8b9c1
DB
57 return value == (s64)(s32)value;
58}
59
60static bool is_uimm32(u64 value)
61{
62 return value == (u64)(u32)value;
0a14842f
ED
63}
64
e430f34e 65/* mov dst, src */
a2c7a983
IM
66#define EMIT_mov(DST, SRC) \
67 do { \
68 if (DST != SRC) \
69 EMIT3(add_2mod(0x48, DST, SRC), 0x89, add_2reg(0xC0, DST, SRC)); \
62258278
AS
70 } while (0)
71
72static int bpf_size_to_x86_bytes(int bpf_size)
73{
74 if (bpf_size == BPF_W)
75 return 4;
76 else if (bpf_size == BPF_H)
77 return 2;
78 else if (bpf_size == BPF_B)
79 return 1;
80 else if (bpf_size == BPF_DW)
81 return 4; /* imm32 */
82 else
83 return 0;
84}
0a14842f 85
a2c7a983
IM
86/*
87 * List of x86 cond jumps opcodes (. + s8)
0a14842f
ED
88 * Add 0x10 (and an extra 0x0f) to generate far jumps (. + s32)
89 */
90#define X86_JB 0x72
91#define X86_JAE 0x73
92#define X86_JE 0x74
93#define X86_JNE 0x75
94#define X86_JBE 0x76
95#define X86_JA 0x77
52afc51e 96#define X86_JL 0x7C
62258278 97#define X86_JGE 0x7D
52afc51e 98#define X86_JLE 0x7E
62258278 99#define X86_JG 0x7F
0a14842f 100
a2c7a983 101/* Pick a register outside of BPF range for JIT internal work */
959a7579 102#define AUX_REG (MAX_BPF_JIT_REG + 1)
fec56f58 103#define X86_REG_R9 (MAX_BPF_JIT_REG + 2)
62258278 104
a2c7a983
IM
105/*
106 * The following table maps BPF registers to x86-64 registers.
959a7579 107 *
a2c7a983 108 * x86-64 register R12 is unused, since if used as base address
959a7579
DB
109 * register in load/store instructions, it always needs an
110 * extra byte of encoding and is callee saved.
111 *
fec56f58
AS
112 * x86-64 register R9 is not used by BPF programs, but can be used by BPF
113 * trampoline. x86-64 register R10 is used for blinding (if enabled).
62258278
AS
114 */
115static const int reg2hex[] = {
a2c7a983
IM
116 [BPF_REG_0] = 0, /* RAX */
117 [BPF_REG_1] = 7, /* RDI */
118 [BPF_REG_2] = 6, /* RSI */
119 [BPF_REG_3] = 2, /* RDX */
120 [BPF_REG_4] = 1, /* RCX */
121 [BPF_REG_5] = 0, /* R8 */
122 [BPF_REG_6] = 3, /* RBX callee saved */
123 [BPF_REG_7] = 5, /* R13 callee saved */
124 [BPF_REG_8] = 6, /* R14 callee saved */
125 [BPF_REG_9] = 7, /* R15 callee saved */
126 [BPF_REG_FP] = 5, /* RBP readonly */
127 [BPF_REG_AX] = 2, /* R10 temp register */
128 [AUX_REG] = 3, /* R11 temp register */
fec56f58 129 [X86_REG_R9] = 1, /* R9 register, 6th function argument */
62258278
AS
130};
131
3dec541b
AS
132static const int reg2pt_regs[] = {
133 [BPF_REG_0] = offsetof(struct pt_regs, ax),
134 [BPF_REG_1] = offsetof(struct pt_regs, di),
135 [BPF_REG_2] = offsetof(struct pt_regs, si),
136 [BPF_REG_3] = offsetof(struct pt_regs, dx),
137 [BPF_REG_4] = offsetof(struct pt_regs, cx),
138 [BPF_REG_5] = offsetof(struct pt_regs, r8),
139 [BPF_REG_6] = offsetof(struct pt_regs, bx),
140 [BPF_REG_7] = offsetof(struct pt_regs, r13),
141 [BPF_REG_8] = offsetof(struct pt_regs, r14),
142 [BPF_REG_9] = offsetof(struct pt_regs, r15),
143};
144
a2c7a983
IM
145/*
146 * is_ereg() == true if BPF register 'reg' maps to x86-64 r8..r15
62258278
AS
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 156 BIT(BPF_REG_9) |
fec56f58 157 BIT(X86_REG_R9) |
959a7579 158 BIT(BPF_REG_AX));
62258278
AS
159}
160
aee194b1
LN
161/*
162 * is_ereg_8l() == true if BPF register 'reg' is mapped to access x86-64
163 * lower 8-bit registers dil,sil,bpl,spl,r8b..r15b, which need extra byte
164 * of encoding. al,cl,dl,bl have simpler encoding.
165 */
166static bool is_ereg_8l(u32 reg)
167{
168 return is_ereg(reg) ||
169 (1 << reg) & (BIT(BPF_REG_1) |
170 BIT(BPF_REG_2) |
171 BIT(BPF_REG_FP));
172}
173
de0a444d
DB
174static bool is_axreg(u32 reg)
175{
176 return reg == BPF_REG_0;
177}
178
a2c7a983 179/* Add modifiers if 'reg' maps to x86-64 registers R8..R15 */
5cccc702 180static u8 add_1mod(u8 byte, u32 reg)
62258278
AS
181{
182 if (is_ereg(reg))
183 byte |= 1;
184 return byte;
185}
186
5cccc702 187static u8 add_2mod(u8 byte, u32 r1, u32 r2)
62258278
AS
188{
189 if (is_ereg(r1))
190 byte |= 1;
191 if (is_ereg(r2))
192 byte |= 4;
193 return byte;
194}
195
a2c7a983 196/* Encode 'dst_reg' register into x86-64 opcode 'byte' */
5cccc702 197static u8 add_1reg(u8 byte, u32 dst_reg)
62258278 198{
e430f34e 199 return byte + reg2hex[dst_reg];
62258278
AS
200}
201
a2c7a983 202/* Encode 'dst_reg' and 'src_reg' registers into x86-64 opcode 'byte' */
5cccc702 203static u8 add_2reg(u8 byte, u32 dst_reg, u32 src_reg)
62258278 204{
e430f34e 205 return byte + reg2hex[dst_reg] + (reg2hex[src_reg] << 3);
62258278
AS
206}
207
e5f02cac
BJ
208/* Some 1-byte opcodes for binary ALU operations */
209static u8 simple_alu_opcodes[] = {
210 [BPF_ADD] = 0x01,
211 [BPF_SUB] = 0x29,
212 [BPF_AND] = 0x21,
213 [BPF_OR] = 0x09,
214 [BPF_XOR] = 0x31,
215 [BPF_LSH] = 0xE0,
216 [BPF_RSH] = 0xE8,
217 [BPF_ARSH] = 0xF8,
218};
219
738cbe72
DB
220static void jit_fill_hole(void *area, unsigned int size)
221{
a2c7a983 222 /* Fill whole space with INT3 instructions */
738cbe72
DB
223 memset(area, 0xcc, size);
224}
225
f3c2af7b 226struct jit_context {
a2c7a983 227 int cleanup_addr; /* Epilogue code offset */
f3c2af7b
AS
228};
229
a2c7a983 230/* Maximum number of bytes emitted while JITing one eBPF insn */
e0ee9c12
AS
231#define BPF_MAX_INSN_SIZE 128
232#define BPF_INSN_SAFETY 64
4b3da77b
DB
233
234/* Number of bytes emit_patch() needs to generate instructions */
235#define X86_PATCH_SIZE 5
ebf7d1f5
MF
236/* Number of bytes that will be skipped on tailcall */
237#define X86_TAIL_CALL_OFFSET 11
e0ee9c12 238
ebf7d1f5
MF
239static void push_callee_regs(u8 **pprog, bool *callee_regs_used)
240{
241 u8 *prog = *pprog;
242 int cnt = 0;
243
244 if (callee_regs_used[0])
245 EMIT1(0x53); /* push rbx */
246 if (callee_regs_used[1])
247 EMIT2(0x41, 0x55); /* push r13 */
248 if (callee_regs_used[2])
249 EMIT2(0x41, 0x56); /* push r14 */
250 if (callee_regs_used[3])
251 EMIT2(0x41, 0x57); /* push r15 */
252 *pprog = prog;
253}
254
255static void pop_callee_regs(u8 **pprog, bool *callee_regs_used)
256{
257 u8 *prog = *pprog;
258 int cnt = 0;
259
260 if (callee_regs_used[3])
261 EMIT2(0x41, 0x5F); /* pop r15 */
262 if (callee_regs_used[2])
263 EMIT2(0x41, 0x5E); /* pop r14 */
264 if (callee_regs_used[1])
265 EMIT2(0x41, 0x5D); /* pop r13 */
266 if (callee_regs_used[0])
267 EMIT1(0x5B); /* pop rbx */
268 *pprog = prog;
269}
b52f00e6 270
a2c7a983 271/*
ebf7d1f5
MF
272 * Emit x86-64 prologue code for BPF program.
273 * bpf_tail_call helper will skip the first X86_TAIL_CALL_OFFSET bytes
274 * while jumping to another program
b52f00e6 275 */
ebf7d1f5
MF
276static void emit_prologue(u8 **pprog, u32 stack_depth, bool ebpf_from_cbpf,
277 bool tail_call_reachable, bool is_subprog)
0a14842f 278{
b52f00e6 279 u8 *prog = *pprog;
4b3da77b 280 int cnt = X86_PATCH_SIZE;
0a14842f 281
9fd4a39d
AS
282 /* BPF trampoline can be made to work without these nops,
283 * but let's waste 5 bytes for now and optimize later
284 */
285 memcpy(prog, ideal_nops[NOP_ATOMIC5], cnt);
286 prog += cnt;
ebf7d1f5
MF
287 if (!ebpf_from_cbpf) {
288 if (tail_call_reachable && !is_subprog)
289 EMIT2(0x31, 0xC0); /* xor eax, eax */
290 else
291 EMIT2(0x66, 0x90); /* nop2 */
292 }
fe8d9571
AS
293 EMIT1(0x55); /* push rbp */
294 EMIT3(0x48, 0x89, 0xE5); /* mov rbp, rsp */
295 /* sub rsp, rounded_stack_depth */
4d0b8c0b
MF
296 if (stack_depth)
297 EMIT3_off32(0x48, 0x81, 0xEC, round_up(stack_depth, 8));
ebf7d1f5
MF
298 if (tail_call_reachable)
299 EMIT1(0x50); /* push rax */
b52f00e6
AS
300 *pprog = prog;
301}
302
428d5df1
DB
303static int emit_patch(u8 **pprog, void *func, void *ip, u8 opcode)
304{
305 u8 *prog = *pprog;
306 int cnt = 0;
307 s64 offset;
308
309 offset = func - (ip + X86_PATCH_SIZE);
310 if (!is_simm32(offset)) {
311 pr_err("Target call %p is out of range\n", func);
312 return -ERANGE;
313 }
314 EMIT1_off32(opcode, offset);
315 *pprog = prog;
316 return 0;
317}
318
319static int emit_call(u8 **pprog, void *func, void *ip)
320{
321 return emit_patch(pprog, func, ip, 0xE8);
322}
323
324static int emit_jump(u8 **pprog, void *func, void *ip)
325{
326 return emit_patch(pprog, func, ip, 0xE9);
327}
328
329static int __bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
330 void *old_addr, void *new_addr,
331 const bool text_live)
332{
428d5df1 333 const u8 *nop_insn = ideal_nops[NOP_ATOMIC5];
b553a6ec
DB
334 u8 old_insn[X86_PATCH_SIZE];
335 u8 new_insn[X86_PATCH_SIZE];
428d5df1
DB
336 u8 *prog;
337 int ret;
338
b553a6ec
DB
339 memcpy(old_insn, nop_insn, X86_PATCH_SIZE);
340 if (old_addr) {
341 prog = old_insn;
342 ret = t == BPF_MOD_CALL ?
343 emit_call(&prog, old_addr, ip) :
344 emit_jump(&prog, old_addr, ip);
345 if (ret)
346 return ret;
428d5df1
DB
347 }
348
b553a6ec
DB
349 memcpy(new_insn, nop_insn, X86_PATCH_SIZE);
350 if (new_addr) {
351 prog = new_insn;
352 ret = t == BPF_MOD_CALL ?
353 emit_call(&prog, new_addr, ip) :
354 emit_jump(&prog, new_addr, ip);
355 if (ret)
356 return ret;
428d5df1
DB
357 }
358
359 ret = -EBUSY;
360 mutex_lock(&text_mutex);
361 if (memcmp(ip, old_insn, X86_PATCH_SIZE))
362 goto out;
ebf7d1f5 363 ret = 1;
b553a6ec
DB
364 if (memcmp(ip, new_insn, X86_PATCH_SIZE)) {
365 if (text_live)
366 text_poke_bp(ip, new_insn, X86_PATCH_SIZE, NULL);
367 else
368 memcpy(ip, new_insn, X86_PATCH_SIZE);
ebf7d1f5 369 ret = 0;
b553a6ec 370 }
428d5df1
DB
371out:
372 mutex_unlock(&text_mutex);
373 return ret;
374}
375
376int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
377 void *old_addr, void *new_addr)
378{
379 if (!is_kernel_text((long)ip) &&
380 !is_bpf_text_address((long)ip))
381 /* BPF poking in modules is not supported */
382 return -EINVAL;
383
384 return __bpf_arch_text_poke(ip, t, old_addr, new_addr, true);
385}
386
ebf7d1f5
MF
387static int get_pop_bytes(bool *callee_regs_used)
388{
389 int bytes = 0;
390
391 if (callee_regs_used[3])
392 bytes += 2;
393 if (callee_regs_used[2])
394 bytes += 2;
395 if (callee_regs_used[1])
396 bytes += 2;
397 if (callee_regs_used[0])
398 bytes += 1;
399
400 return bytes;
401}
402
a2c7a983
IM
403/*
404 * Generate the following code:
405 *
b52f00e6
AS
406 * ... bpf_tail_call(void *ctx, struct bpf_array *array, u64 index) ...
407 * if (index >= array->map.max_entries)
408 * goto out;
409 * if (++tail_call_cnt > MAX_TAIL_CALL_CNT)
410 * goto out;
2a36f0b9 411 * prog = array->ptrs[index];
b52f00e6
AS
412 * if (prog == NULL)
413 * goto out;
414 * goto *(prog->bpf_func + prologue_size);
415 * out:
416 */
ebf7d1f5
MF
417static void emit_bpf_tail_call_indirect(u8 **pprog, bool *callee_regs_used,
418 u32 stack_depth)
b52f00e6 419{
ebf7d1f5 420 int tcc_off = -4 - round_up(stack_depth, 8);
b52f00e6 421 u8 *prog = *pprog;
ebf7d1f5 422 int pop_bytes = 0;
4d0b8c0b
MF
423 int off1 = 42;
424 int off2 = 31;
425 int off3 = 9;
b52f00e6
AS
426 int cnt = 0;
427
ebf7d1f5
MF
428 /* count the additional bytes used for popping callee regs from stack
429 * that need to be taken into account for each of the offsets that
430 * are used for bailing out of the tail call
431 */
432 pop_bytes = get_pop_bytes(callee_regs_used);
433 off1 += pop_bytes;
434 off2 += pop_bytes;
435 off3 += pop_bytes;
436
4d0b8c0b
MF
437 if (stack_depth) {
438 off1 += 7;
439 off2 += 7;
440 off3 += 7;
441 }
442
a2c7a983
IM
443 /*
444 * rdi - pointer to ctx
b52f00e6
AS
445 * rsi - pointer to bpf_array
446 * rdx - index in bpf_array
447 */
448
a2c7a983
IM
449 /*
450 * if (index >= array->map.max_entries)
451 * goto out;
b52f00e6 452 */
90caccdd
AS
453 EMIT2(0x89, 0xD2); /* mov edx, edx */
454 EMIT3(0x39, 0x56, /* cmp dword ptr [rsi + 16], edx */
b52f00e6 455 offsetof(struct bpf_array, map.max_entries));
ebf7d1f5 456#define OFFSET1 (off1 + RETPOLINE_RCX_BPF_JIT_SIZE) /* Number of bytes to jump */
b52f00e6 457 EMIT2(X86_JBE, OFFSET1); /* jbe out */
b52f00e6 458
a2c7a983
IM
459 /*
460 * if (tail_call_cnt > MAX_TAIL_CALL_CNT)
461 * goto out;
b52f00e6 462 */
ebf7d1f5 463 EMIT2_off32(0x8B, 0x85, tcc_off); /* mov eax, dword ptr [rbp - tcc_off] */
b52f00e6 464 EMIT3(0x83, 0xF8, MAX_TAIL_CALL_CNT); /* cmp eax, MAX_TAIL_CALL_CNT */
ebf7d1f5 465#define OFFSET2 (off2 + RETPOLINE_RCX_BPF_JIT_SIZE)
b52f00e6 466 EMIT2(X86_JA, OFFSET2); /* ja out */
b52f00e6 467 EMIT3(0x83, 0xC0, 0x01); /* add eax, 1 */
ebf7d1f5 468 EMIT2_off32(0x89, 0x85, tcc_off); /* mov dword ptr [rbp - tcc_off], eax */
b52f00e6 469
2a36f0b9 470 /* prog = array->ptrs[index]; */
0d4ddce3 471 EMIT4_off32(0x48, 0x8B, 0x8C, 0xD6, /* mov rcx, [rsi + rdx * 8 + offsetof(...)] */
2a36f0b9 472 offsetof(struct bpf_array, ptrs));
b52f00e6 473
a2c7a983
IM
474 /*
475 * if (prog == NULL)
476 * goto out;
b52f00e6 477 */
ebf7d1f5
MF
478 EMIT3(0x48, 0x85, 0xC9); /* test rcx,rcx */
479#define OFFSET3 (off3 + RETPOLINE_RCX_BPF_JIT_SIZE)
b52f00e6 480 EMIT2(X86_JE, OFFSET3); /* je out */
b52f00e6 481
ebf7d1f5
MF
482 *pprog = prog;
483 pop_callee_regs(pprog, callee_regs_used);
484 prog = *pprog;
485
486 EMIT1(0x58); /* pop rax */
4d0b8c0b
MF
487 if (stack_depth)
488 EMIT3_off32(0x48, 0x81, 0xC4, /* add rsp, sd */
489 round_up(stack_depth, 8));
ebf7d1f5
MF
490
491 /* goto *(prog->bpf_func + X86_TAIL_CALL_OFFSET); */
0d4ddce3 492 EMIT4(0x48, 0x8B, 0x49, /* mov rcx, qword ptr [rcx + 32] */
b52f00e6 493 offsetof(struct bpf_prog, bpf_func));
ebf7d1f5
MF
494 EMIT4(0x48, 0x83, 0xC1, /* add rcx, X86_TAIL_CALL_OFFSET */
495 X86_TAIL_CALL_OFFSET);
a2c7a983 496 /*
0d4ddce3 497 * Now we're ready to jump into next BPF program
b52f00e6 498 * rdi == ctx (1st arg)
ebf7d1f5 499 * rcx == prog->bpf_func + X86_TAIL_CALL_OFFSET
b52f00e6 500 */
0d4ddce3 501 RETPOLINE_RCX_BPF_JIT();
b52f00e6
AS
502
503 /* out: */
b52f00e6
AS
504 *pprog = prog;
505}
506
428d5df1 507static void emit_bpf_tail_call_direct(struct bpf_jit_poke_descriptor *poke,
ebf7d1f5
MF
508 u8 **pprog, int addr, u8 *image,
509 bool *callee_regs_used, u32 stack_depth)
428d5df1 510{
ebf7d1f5 511 int tcc_off = -4 - round_up(stack_depth, 8);
428d5df1 512 u8 *prog = *pprog;
ebf7d1f5 513 int pop_bytes = 0;
4d0b8c0b 514 int off1 = 20;
ebf7d1f5 515 int poke_off;
428d5df1
DB
516 int cnt = 0;
517
ebf7d1f5
MF
518 /* count the additional bytes used for popping callee regs to stack
519 * that need to be taken into account for jump offset that is used for
520 * bailing out from of the tail call when limit is reached
521 */
522 pop_bytes = get_pop_bytes(callee_regs_used);
523 off1 += pop_bytes;
524
525 /*
526 * total bytes for:
527 * - nop5/ jmpq $off
528 * - pop callee regs
4d0b8c0b 529 * - sub rsp, $val if depth > 0
ebf7d1f5
MF
530 * - pop rax
531 */
4d0b8c0b
MF
532 poke_off = X86_PATCH_SIZE + pop_bytes + 1;
533 if (stack_depth) {
534 poke_off += 7;
535 off1 += 7;
536 }
ebf7d1f5 537
428d5df1
DB
538 /*
539 * if (tail_call_cnt > MAX_TAIL_CALL_CNT)
540 * goto out;
541 */
ebf7d1f5 542 EMIT2_off32(0x8B, 0x85, tcc_off); /* mov eax, dword ptr [rbp - tcc_off] */
428d5df1 543 EMIT3(0x83, 0xF8, MAX_TAIL_CALL_CNT); /* cmp eax, MAX_TAIL_CALL_CNT */
ebf7d1f5 544 EMIT2(X86_JA, off1); /* ja out */
428d5df1 545 EMIT3(0x83, 0xC0, 0x01); /* add eax, 1 */
ebf7d1f5 546 EMIT2_off32(0x89, 0x85, tcc_off); /* mov dword ptr [rbp - tcc_off], eax */
428d5df1 547
ebf7d1f5
MF
548 poke->tailcall_bypass = image + (addr - poke_off - X86_PATCH_SIZE);
549 poke->adj_off = X86_TAIL_CALL_OFFSET;
cf71b174 550 poke->tailcall_target = image + (addr - X86_PATCH_SIZE);
ebf7d1f5
MF
551 poke->bypass_addr = (u8 *)poke->tailcall_target + X86_PATCH_SIZE;
552
553 emit_jump(&prog, (u8 *)poke->tailcall_target + X86_PATCH_SIZE,
554 poke->tailcall_bypass);
555
556 *pprog = prog;
557 pop_callee_regs(pprog, callee_regs_used);
558 prog = *pprog;
559 EMIT1(0x58); /* pop rax */
4d0b8c0b
MF
560 if (stack_depth)
561 EMIT3_off32(0x48, 0x81, 0xC4, round_up(stack_depth, 8));
428d5df1
DB
562
563 memcpy(prog, ideal_nops[NOP_ATOMIC5], X86_PATCH_SIZE);
564 prog += X86_PATCH_SIZE;
565 /* out: */
566
567 *pprog = prog;
568}
569
570static void bpf_tail_call_direct_fixup(struct bpf_prog *prog)
571{
428d5df1
DB
572 struct bpf_jit_poke_descriptor *poke;
573 struct bpf_array *array;
574 struct bpf_prog *target;
575 int i, ret;
576
577 for (i = 0; i < prog->aux->size_poke_tab; i++) {
578 poke = &prog->aux->poke_tab[i];
cf71b174 579 WARN_ON_ONCE(READ_ONCE(poke->tailcall_target_stable));
428d5df1
DB
580
581 if (poke->reason != BPF_POKE_REASON_TAIL_CALL)
582 continue;
583
584 array = container_of(poke->tail_call.map, struct bpf_array, map);
585 mutex_lock(&array->aux->poke_mutex);
586 target = array->ptrs[poke->tail_call.key];
587 if (target) {
588 /* Plain memcpy is used when image is not live yet
589 * and still not locked as read-only. Once poke
cf71b174
MF
590 * location is active (poke->tailcall_target_stable),
591 * any parallel bpf_arch_text_poke() might occur
592 * still on the read-write image until we finally
593 * locked it as read-only. Both modifications on
594 * the given image are under text_mutex to avoid
595 * interference.
428d5df1 596 */
cf71b174
MF
597 ret = __bpf_arch_text_poke(poke->tailcall_target,
598 BPF_MOD_JUMP, NULL,
428d5df1
DB
599 (u8 *)target->bpf_func +
600 poke->adj_off, false);
601 BUG_ON(ret < 0);
ebf7d1f5
MF
602 ret = __bpf_arch_text_poke(poke->tailcall_bypass,
603 BPF_MOD_JUMP,
604 (u8 *)poke->tailcall_target +
605 X86_PATCH_SIZE, NULL, false);
606 BUG_ON(ret < 0);
428d5df1 607 }
cf71b174 608 WRITE_ONCE(poke->tailcall_target_stable, true);
428d5df1
DB
609 mutex_unlock(&array->aux->poke_mutex);
610 }
611}
612
6fe8b9c1
DB
613static void emit_mov_imm32(u8 **pprog, bool sign_propagate,
614 u32 dst_reg, const u32 imm32)
615{
616 u8 *prog = *pprog;
617 u8 b1, b2, b3;
618 int cnt = 0;
619
a2c7a983
IM
620 /*
621 * Optimization: if imm32 is positive, use 'mov %eax, imm32'
6fe8b9c1
DB
622 * (which zero-extends imm32) to save 2 bytes.
623 */
624 if (sign_propagate && (s32)imm32 < 0) {
625 /* 'mov %rax, imm32' sign extends imm32 */
626 b1 = add_1mod(0x48, dst_reg);
627 b2 = 0xC7;
628 b3 = 0xC0;
629 EMIT3_off32(b1, b2, add_1reg(b3, dst_reg), imm32);
630 goto done;
631 }
632
a2c7a983
IM
633 /*
634 * Optimization: if imm32 is zero, use 'xor %eax, %eax'
6fe8b9c1
DB
635 * to save 3 bytes.
636 */
637 if (imm32 == 0) {
638 if (is_ereg(dst_reg))
639 EMIT1(add_2mod(0x40, dst_reg, dst_reg));
640 b2 = 0x31; /* xor */
641 b3 = 0xC0;
642 EMIT2(b2, add_2reg(b3, dst_reg, dst_reg));
643 goto done;
644 }
645
646 /* mov %eax, imm32 */
647 if (is_ereg(dst_reg))
648 EMIT1(add_1mod(0x40, dst_reg));
649 EMIT1_off32(add_1reg(0xB8, dst_reg), imm32);
650done:
651 *pprog = prog;
652}
653
654static void emit_mov_imm64(u8 **pprog, u32 dst_reg,
655 const u32 imm32_hi, const u32 imm32_lo)
656{
657 u8 *prog = *pprog;
658 int cnt = 0;
659
660 if (is_uimm32(((u64)imm32_hi << 32) | (u32)imm32_lo)) {
a2c7a983
IM
661 /*
662 * For emitting plain u32, where sign bit must not be
6fe8b9c1
DB
663 * propagated LLVM tends to load imm64 over mov32
664 * directly, so save couple of bytes by just doing
665 * 'mov %eax, imm32' instead.
666 */
667 emit_mov_imm32(&prog, false, dst_reg, imm32_lo);
668 } else {
669 /* movabsq %rax, imm64 */
670 EMIT2(add_1mod(0x48, dst_reg), add_1reg(0xB8, dst_reg));
671 EMIT(imm32_lo, 4);
672 EMIT(imm32_hi, 4);
673 }
674
675 *pprog = prog;
676}
677
4c38e2f3
DB
678static void emit_mov_reg(u8 **pprog, bool is64, u32 dst_reg, u32 src_reg)
679{
680 u8 *prog = *pprog;
681 int cnt = 0;
682
683 if (is64) {
684 /* mov dst, src */
685 EMIT_mov(dst_reg, src_reg);
686 } else {
687 /* mov32 dst, src */
688 if (is_ereg(dst_reg) || is_ereg(src_reg))
689 EMIT1(add_2mod(0x40, dst_reg, src_reg));
690 EMIT2(0x89, add_2reg(0xC0, dst_reg, src_reg));
691 }
692
693 *pprog = prog;
694}
695
11c11d07
BJ
696/* Emit the suffix (ModR/M etc) for addressing *(ptr_reg + off) and val_reg */
697static void emit_insn_suffix(u8 **pprog, u32 ptr_reg, u32 val_reg, int off)
698{
699 u8 *prog = *pprog;
700 int cnt = 0;
701
702 if (is_imm8(off)) {
703 /* 1-byte signed displacement.
704 *
705 * If off == 0 we could skip this and save one extra byte, but
706 * special case of x86 R13 which always needs an offset is not
707 * worth the hassle
708 */
709 EMIT2(add_2reg(0x40, ptr_reg, val_reg), off);
710 } else {
711 /* 4-byte signed displacement */
712 EMIT1_off32(add_2reg(0x80, ptr_reg, val_reg), off);
713 }
714 *pprog = prog;
715}
716
74007cfc
BJ
717/*
718 * Emit a REX byte if it will be necessary to address these registers
719 */
720static void maybe_emit_mod(u8 **pprog, u32 dst_reg, u32 src_reg, bool is64)
721{
722 u8 *prog = *pprog;
723 int cnt = 0;
724
725 if (is64)
726 EMIT1(add_2mod(0x48, dst_reg, src_reg));
727 else if (is_ereg(dst_reg) || is_ereg(src_reg))
728 EMIT1(add_2mod(0x40, dst_reg, src_reg));
729 *pprog = prog;
730}
731
3b2744e6
AS
732/* LDX: dst_reg = *(u8*)(src_reg + off) */
733static void emit_ldx(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, int off)
734{
735 u8 *prog = *pprog;
736 int cnt = 0;
737
738 switch (size) {
739 case BPF_B:
740 /* Emit 'movzx rax, byte ptr [rax + off]' */
741 EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB6);
742 break;
743 case BPF_H:
744 /* Emit 'movzx rax, word ptr [rax + off]' */
745 EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB7);
746 break;
747 case BPF_W:
748 /* Emit 'mov eax, dword ptr [rax+0x14]' */
749 if (is_ereg(dst_reg) || is_ereg(src_reg))
750 EMIT2(add_2mod(0x40, src_reg, dst_reg), 0x8B);
751 else
752 EMIT1(0x8B);
753 break;
754 case BPF_DW:
755 /* Emit 'mov rax, qword ptr [rax+0x14]' */
756 EMIT2(add_2mod(0x48, src_reg, dst_reg), 0x8B);
757 break;
758 }
11c11d07 759 emit_insn_suffix(&prog, src_reg, dst_reg, off);
3b2744e6
AS
760 *pprog = prog;
761}
762
763/* STX: *(u8*)(dst_reg + off) = src_reg */
764static void emit_stx(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, int off)
765{
766 u8 *prog = *pprog;
767 int cnt = 0;
768
769 switch (size) {
770 case BPF_B:
771 /* Emit 'mov byte ptr [rax + off], al' */
aee194b1
LN
772 if (is_ereg(dst_reg) || is_ereg_8l(src_reg))
773 /* Add extra byte for eregs or SIL,DIL,BPL in src_reg */
3b2744e6
AS
774 EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x88);
775 else
776 EMIT1(0x88);
777 break;
778 case BPF_H:
779 if (is_ereg(dst_reg) || is_ereg(src_reg))
780 EMIT3(0x66, add_2mod(0x40, dst_reg, src_reg), 0x89);
781 else
782 EMIT2(0x66, 0x89);
783 break;
784 case BPF_W:
785 if (is_ereg(dst_reg) || is_ereg(src_reg))
786 EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x89);
787 else
788 EMIT1(0x89);
789 break;
790 case BPF_DW:
791 EMIT2(add_2mod(0x48, dst_reg, src_reg), 0x89);
792 break;
793 }
11c11d07 794 emit_insn_suffix(&prog, dst_reg, src_reg, off);
3b2744e6
AS
795 *pprog = prog;
796}
797
91c960b0
BJ
798static int emit_atomic(u8 **pprog, u8 atomic_op,
799 u32 dst_reg, u32 src_reg, s16 off, u8 bpf_size)
800{
801 u8 *prog = *pprog;
802 int cnt = 0;
803
804 EMIT1(0xF0); /* lock prefix */
805
806 maybe_emit_mod(&prog, dst_reg, src_reg, bpf_size == BPF_DW);
807
808 /* emit opcode */
809 switch (atomic_op) {
810 case BPF_ADD:
811 /* lock *(u32/u64*)(dst_reg + off) <op>= src_reg */
812 EMIT1(simple_alu_opcodes[atomic_op]);
813 break;
814 default:
815 pr_err("bpf_jit: unknown atomic opcode %02x\n", atomic_op);
816 return -EFAULT;
817 }
818
819 emit_insn_suffix(&prog, dst_reg, src_reg, off);
820
821 *pprog = prog;
822 return 0;
823}
824
3dec541b
AS
825static bool ex_handler_bpf(const struct exception_table_entry *x,
826 struct pt_regs *regs, int trapnr,
827 unsigned long error_code, unsigned long fault_addr)
828{
829 u32 reg = x->fixup >> 8;
830
831 /* jump over faulting load and clear dest register */
832 *(unsigned long *)((void *)regs + reg) = 0;
833 regs->ip += x->fixup & 0xff;
834 return true;
835}
836
ebf7d1f5
MF
837static void detect_reg_usage(struct bpf_insn *insn, int insn_cnt,
838 bool *regs_used, bool *tail_call_seen)
839{
840 int i;
841
842 for (i = 1; i <= insn_cnt; i++, insn++) {
843 if (insn->code == (BPF_JMP | BPF_TAIL_CALL))
844 *tail_call_seen = true;
845 if (insn->dst_reg == BPF_REG_6 || insn->src_reg == BPF_REG_6)
846 regs_used[0] = true;
847 if (insn->dst_reg == BPF_REG_7 || insn->src_reg == BPF_REG_7)
848 regs_used[1] = true;
849 if (insn->dst_reg == BPF_REG_8 || insn->src_reg == BPF_REG_8)
850 regs_used[2] = true;
851 if (insn->dst_reg == BPF_REG_9 || insn->src_reg == BPF_REG_9)
852 regs_used[3] = true;
853 }
854}
855
b52f00e6
AS
856static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
857 int oldproglen, struct jit_context *ctx)
858{
ebf7d1f5 859 bool tail_call_reachable = bpf_prog->aux->tail_call_reachable;
b52f00e6 860 struct bpf_insn *insn = bpf_prog->insnsi;
ebf7d1f5 861 bool callee_regs_used[4] = {};
b52f00e6 862 int insn_cnt = bpf_prog->len;
ebf7d1f5 863 bool tail_call_seen = false;
b52f00e6
AS
864 bool seen_exit = false;
865 u8 temp[BPF_MAX_INSN_SIZE + BPF_INSN_SAFETY];
3dec541b 866 int i, cnt = 0, excnt = 0;
b52f00e6
AS
867 int proglen = 0;
868 u8 *prog = temp;
91c960b0 869 int err;
b52f00e6 870
ebf7d1f5
MF
871 detect_reg_usage(insn, insn_cnt, callee_regs_used,
872 &tail_call_seen);
873
874 /* tail call's presence in current prog implies it is reachable */
875 tail_call_reachable |= tail_call_seen;
876
08691752 877 emit_prologue(&prog, bpf_prog->aux->stack_depth,
ebf7d1f5
MF
878 bpf_prog_was_classic(bpf_prog), tail_call_reachable,
879 bpf_prog->aux->func_idx != 0);
880 push_callee_regs(&prog, callee_regs_used);
7c2e988f 881 addrs[0] = prog - temp;
b52f00e6 882
7c2e988f 883 for (i = 1; i <= insn_cnt; i++, insn++) {
e430f34e
AS
884 const s32 imm32 = insn->imm;
885 u32 dst_reg = insn->dst_reg;
886 u32 src_reg = insn->src_reg;
6fe8b9c1 887 u8 b2 = 0, b3 = 0;
62258278
AS
888 s64 jmp_offset;
889 u8 jmp_cond;
890 int ilen;
891 u8 *func;
892
893 switch (insn->code) {
894 /* ALU */
895 case BPF_ALU | BPF_ADD | BPF_X:
896 case BPF_ALU | BPF_SUB | BPF_X:
897 case BPF_ALU | BPF_AND | BPF_X:
898 case BPF_ALU | BPF_OR | BPF_X:
899 case BPF_ALU | BPF_XOR | BPF_X:
900 case BPF_ALU64 | BPF_ADD | BPF_X:
901 case BPF_ALU64 | BPF_SUB | BPF_X:
902 case BPF_ALU64 | BPF_AND | BPF_X:
903 case BPF_ALU64 | BPF_OR | BPF_X:
904 case BPF_ALU64 | BPF_XOR | BPF_X:
74007cfc
BJ
905 maybe_emit_mod(&prog, dst_reg, src_reg,
906 BPF_CLASS(insn->code) == BPF_ALU64);
e5f02cac 907 b2 = simple_alu_opcodes[BPF_OP(insn->code)];
e430f34e 908 EMIT2(b2, add_2reg(0xC0, dst_reg, src_reg));
62258278 909 break;
0a14842f 910
62258278 911 case BPF_ALU64 | BPF_MOV | BPF_X:
62258278 912 case BPF_ALU | BPF_MOV | BPF_X:
4c38e2f3
DB
913 emit_mov_reg(&prog,
914 BPF_CLASS(insn->code) == BPF_ALU64,
915 dst_reg, src_reg);
62258278 916 break;
0a14842f 917
e430f34e 918 /* neg dst */
62258278
AS
919 case BPF_ALU | BPF_NEG:
920 case BPF_ALU64 | BPF_NEG:
921 if (BPF_CLASS(insn->code) == BPF_ALU64)
e430f34e
AS
922 EMIT1(add_1mod(0x48, dst_reg));
923 else if (is_ereg(dst_reg))
924 EMIT1(add_1mod(0x40, dst_reg));
925 EMIT2(0xF7, add_1reg(0xD8, dst_reg));
62258278
AS
926 break;
927
928 case BPF_ALU | BPF_ADD | BPF_K:
929 case BPF_ALU | BPF_SUB | BPF_K:
930 case BPF_ALU | BPF_AND | BPF_K:
931 case BPF_ALU | BPF_OR | BPF_K:
932 case BPF_ALU | BPF_XOR | BPF_K:
933 case BPF_ALU64 | BPF_ADD | BPF_K:
934 case BPF_ALU64 | BPF_SUB | BPF_K:
935 case BPF_ALU64 | BPF_AND | BPF_K:
936 case BPF_ALU64 | BPF_OR | BPF_K:
937 case BPF_ALU64 | BPF_XOR | BPF_K:
938 if (BPF_CLASS(insn->code) == BPF_ALU64)
e430f34e
AS
939 EMIT1(add_1mod(0x48, dst_reg));
940 else if (is_ereg(dst_reg))
941 EMIT1(add_1mod(0x40, dst_reg));
62258278 942
a2c7a983
IM
943 /*
944 * b3 holds 'normal' opcode, b2 short form only valid
de0a444d
DB
945 * in case dst is eax/rax.
946 */
62258278 947 switch (BPF_OP(insn->code)) {
de0a444d
DB
948 case BPF_ADD:
949 b3 = 0xC0;
950 b2 = 0x05;
951 break;
952 case BPF_SUB:
953 b3 = 0xE8;
954 b2 = 0x2D;
955 break;
956 case BPF_AND:
957 b3 = 0xE0;
958 b2 = 0x25;
959 break;
960 case BPF_OR:
961 b3 = 0xC8;
962 b2 = 0x0D;
963 break;
964 case BPF_XOR:
965 b3 = 0xF0;
966 b2 = 0x35;
967 break;
62258278
AS
968 }
969
e430f34e
AS
970 if (is_imm8(imm32))
971 EMIT3(0x83, add_1reg(b3, dst_reg), imm32);
de0a444d
DB
972 else if (is_axreg(dst_reg))
973 EMIT1_off32(b2, imm32);
62258278 974 else
e430f34e 975 EMIT2_off32(0x81, add_1reg(b3, dst_reg), imm32);
62258278
AS
976 break;
977
978 case BPF_ALU64 | BPF_MOV | BPF_K:
62258278 979 case BPF_ALU | BPF_MOV | BPF_K:
6fe8b9c1
DB
980 emit_mov_imm32(&prog, BPF_CLASS(insn->code) == BPF_ALU64,
981 dst_reg, imm32);
62258278
AS
982 break;
983
02ab695b 984 case BPF_LD | BPF_IMM | BPF_DW:
6fe8b9c1 985 emit_mov_imm64(&prog, dst_reg, insn[1].imm, insn[0].imm);
02ab695b
AS
986 insn++;
987 i++;
988 break;
989
e430f34e 990 /* dst %= src, dst /= src, dst %= imm32, dst /= imm32 */
62258278
AS
991 case BPF_ALU | BPF_MOD | BPF_X:
992 case BPF_ALU | BPF_DIV | BPF_X:
993 case BPF_ALU | BPF_MOD | BPF_K:
994 case BPF_ALU | BPF_DIV | BPF_K:
995 case BPF_ALU64 | BPF_MOD | BPF_X:
996 case BPF_ALU64 | BPF_DIV | BPF_X:
997 case BPF_ALU64 | BPF_MOD | BPF_K:
998 case BPF_ALU64 | BPF_DIV | BPF_K:
999 EMIT1(0x50); /* push rax */
1000 EMIT1(0x52); /* push rdx */
1001
1002 if (BPF_SRC(insn->code) == BPF_X)
e430f34e
AS
1003 /* mov r11, src_reg */
1004 EMIT_mov(AUX_REG, src_reg);
62258278 1005 else
e430f34e
AS
1006 /* mov r11, imm32 */
1007 EMIT3_off32(0x49, 0xC7, 0xC3, imm32);
62258278 1008
e430f34e
AS
1009 /* mov rax, dst_reg */
1010 EMIT_mov(BPF_REG_0, dst_reg);
62258278 1011
a2c7a983
IM
1012 /*
1013 * xor edx, edx
62258278
AS
1014 * equivalent to 'xor rdx, rdx', but one byte less
1015 */
1016 EMIT2(0x31, 0xd2);
1017
62258278
AS
1018 if (BPF_CLASS(insn->code) == BPF_ALU64)
1019 /* div r11 */
1020 EMIT3(0x49, 0xF7, 0xF3);
1021 else
1022 /* div r11d */
1023 EMIT3(0x41, 0xF7, 0xF3);
1024
1025 if (BPF_OP(insn->code) == BPF_MOD)
1026 /* mov r11, rdx */
1027 EMIT3(0x49, 0x89, 0xD3);
1028 else
1029 /* mov r11, rax */
1030 EMIT3(0x49, 0x89, 0xC3);
1031
1032 EMIT1(0x5A); /* pop rdx */
1033 EMIT1(0x58); /* pop rax */
1034
e430f34e
AS
1035 /* mov dst_reg, r11 */
1036 EMIT_mov(dst_reg, AUX_REG);
62258278
AS
1037 break;
1038
1039 case BPF_ALU | BPF_MUL | BPF_K:
1040 case BPF_ALU | BPF_MUL | BPF_X:
1041 case BPF_ALU64 | BPF_MUL | BPF_K:
1042 case BPF_ALU64 | BPF_MUL | BPF_X:
4c38e2f3
DB
1043 {
1044 bool is64 = BPF_CLASS(insn->code) == BPF_ALU64;
1045
d806a0cf
DB
1046 if (dst_reg != BPF_REG_0)
1047 EMIT1(0x50); /* push rax */
1048 if (dst_reg != BPF_REG_3)
1049 EMIT1(0x52); /* push rdx */
62258278 1050
e430f34e
AS
1051 /* mov r11, dst_reg */
1052 EMIT_mov(AUX_REG, dst_reg);
62258278
AS
1053
1054 if (BPF_SRC(insn->code) == BPF_X)
4c38e2f3 1055 emit_mov_reg(&prog, is64, BPF_REG_0, src_reg);
62258278 1056 else
4c38e2f3 1057 emit_mov_imm32(&prog, is64, BPF_REG_0, imm32);
62258278 1058
4c38e2f3 1059 if (is64)
62258278
AS
1060 EMIT1(add_1mod(0x48, AUX_REG));
1061 else if (is_ereg(AUX_REG))
1062 EMIT1(add_1mod(0x40, AUX_REG));
1063 /* mul(q) r11 */
1064 EMIT2(0xF7, add_1reg(0xE0, AUX_REG));
1065
d806a0cf
DB
1066 if (dst_reg != BPF_REG_3)
1067 EMIT1(0x5A); /* pop rdx */
1068 if (dst_reg != BPF_REG_0) {
1069 /* mov dst_reg, rax */
1070 EMIT_mov(dst_reg, BPF_REG_0);
1071 EMIT1(0x58); /* pop rax */
1072 }
62258278 1073 break;
4c38e2f3 1074 }
a2c7a983 1075 /* Shifts */
62258278
AS
1076 case BPF_ALU | BPF_LSH | BPF_K:
1077 case BPF_ALU | BPF_RSH | BPF_K:
1078 case BPF_ALU | BPF_ARSH | BPF_K:
1079 case BPF_ALU64 | BPF_LSH | BPF_K:
1080 case BPF_ALU64 | BPF_RSH | BPF_K:
1081 case BPF_ALU64 | BPF_ARSH | BPF_K:
1082 if (BPF_CLASS(insn->code) == BPF_ALU64)
e430f34e
AS
1083 EMIT1(add_1mod(0x48, dst_reg));
1084 else if (is_ereg(dst_reg))
1085 EMIT1(add_1mod(0x40, dst_reg));
62258278 1086
e5f02cac 1087 b3 = simple_alu_opcodes[BPF_OP(insn->code)];
88e69a1f
DB
1088 if (imm32 == 1)
1089 EMIT2(0xD1, add_1reg(b3, dst_reg));
1090 else
1091 EMIT3(0xC1, add_1reg(b3, dst_reg), imm32);
62258278
AS
1092 break;
1093
72b603ee
AS
1094 case BPF_ALU | BPF_LSH | BPF_X:
1095 case BPF_ALU | BPF_RSH | BPF_X:
1096 case BPF_ALU | BPF_ARSH | BPF_X:
1097 case BPF_ALU64 | BPF_LSH | BPF_X:
1098 case BPF_ALU64 | BPF_RSH | BPF_X:
1099 case BPF_ALU64 | BPF_ARSH | BPF_X:
1100
a2c7a983 1101 /* Check for bad case when dst_reg == rcx */
72b603ee
AS
1102 if (dst_reg == BPF_REG_4) {
1103 /* mov r11, dst_reg */
1104 EMIT_mov(AUX_REG, dst_reg);
1105 dst_reg = AUX_REG;
1106 }
1107
1108 if (src_reg != BPF_REG_4) { /* common case */
1109 EMIT1(0x51); /* push rcx */
1110
1111 /* mov rcx, src_reg */
1112 EMIT_mov(BPF_REG_4, src_reg);
1113 }
1114
1115 /* shl %rax, %cl | shr %rax, %cl | sar %rax, %cl */
1116 if (BPF_CLASS(insn->code) == BPF_ALU64)
1117 EMIT1(add_1mod(0x48, dst_reg));
1118 else if (is_ereg(dst_reg))
1119 EMIT1(add_1mod(0x40, dst_reg));
1120
e5f02cac 1121 b3 = simple_alu_opcodes[BPF_OP(insn->code)];
72b603ee
AS
1122 EMIT2(0xD3, add_1reg(b3, dst_reg));
1123
1124 if (src_reg != BPF_REG_4)
1125 EMIT1(0x59); /* pop rcx */
1126
1127 if (insn->dst_reg == BPF_REG_4)
1128 /* mov dst_reg, r11 */
1129 EMIT_mov(insn->dst_reg, AUX_REG);
1130 break;
1131
62258278 1132 case BPF_ALU | BPF_END | BPF_FROM_BE:
e430f34e 1133 switch (imm32) {
62258278 1134 case 16:
a2c7a983 1135 /* Emit 'ror %ax, 8' to swap lower 2 bytes */
62258278 1136 EMIT1(0x66);
e430f34e 1137 if (is_ereg(dst_reg))
62258278 1138 EMIT1(0x41);
e430f34e 1139 EMIT3(0xC1, add_1reg(0xC8, dst_reg), 8);
343f845b 1140
a2c7a983 1141 /* Emit 'movzwl eax, ax' */
343f845b
AS
1142 if (is_ereg(dst_reg))
1143 EMIT3(0x45, 0x0F, 0xB7);
1144 else
1145 EMIT2(0x0F, 0xB7);
1146 EMIT1(add_2reg(0xC0, dst_reg, dst_reg));
62258278
AS
1147 break;
1148 case 32:
a2c7a983 1149 /* Emit 'bswap eax' to swap lower 4 bytes */
e430f34e 1150 if (is_ereg(dst_reg))
62258278 1151 EMIT2(0x41, 0x0F);
0a14842f 1152 else
62258278 1153 EMIT1(0x0F);
e430f34e 1154 EMIT1(add_1reg(0xC8, dst_reg));
0a14842f 1155 break;
62258278 1156 case 64:
a2c7a983 1157 /* Emit 'bswap rax' to swap 8 bytes */
e430f34e
AS
1158 EMIT3(add_1mod(0x48, dst_reg), 0x0F,
1159 add_1reg(0xC8, dst_reg));
3b58908a
ED
1160 break;
1161 }
62258278
AS
1162 break;
1163
1164 case BPF_ALU | BPF_END | BPF_FROM_LE:
343f845b
AS
1165 switch (imm32) {
1166 case 16:
a2c7a983
IM
1167 /*
1168 * Emit 'movzwl eax, ax' to zero extend 16-bit
343f845b
AS
1169 * into 64 bit
1170 */
1171 if (is_ereg(dst_reg))
1172 EMIT3(0x45, 0x0F, 0xB7);
1173 else
1174 EMIT2(0x0F, 0xB7);
1175 EMIT1(add_2reg(0xC0, dst_reg, dst_reg));
1176 break;
1177 case 32:
a2c7a983 1178 /* Emit 'mov eax, eax' to clear upper 32-bits */
343f845b
AS
1179 if (is_ereg(dst_reg))
1180 EMIT1(0x45);
1181 EMIT2(0x89, add_2reg(0xC0, dst_reg, dst_reg));
1182 break;
1183 case 64:
1184 /* nop */
1185 break;
1186 }
62258278
AS
1187 break;
1188
e430f34e 1189 /* ST: *(u8*)(dst_reg + off) = imm */
62258278 1190 case BPF_ST | BPF_MEM | BPF_B:
e430f34e 1191 if (is_ereg(dst_reg))
62258278
AS
1192 EMIT2(0x41, 0xC6);
1193 else
1194 EMIT1(0xC6);
1195 goto st;
1196 case BPF_ST | BPF_MEM | BPF_H:
e430f34e 1197 if (is_ereg(dst_reg))
62258278
AS
1198 EMIT3(0x66, 0x41, 0xC7);
1199 else
1200 EMIT2(0x66, 0xC7);
1201 goto st;
1202 case BPF_ST | BPF_MEM | BPF_W:
e430f34e 1203 if (is_ereg(dst_reg))
62258278
AS
1204 EMIT2(0x41, 0xC7);
1205 else
1206 EMIT1(0xC7);
1207 goto st;
1208 case BPF_ST | BPF_MEM | BPF_DW:
e430f34e 1209 EMIT2(add_1mod(0x48, dst_reg), 0xC7);
62258278
AS
1210
1211st: if (is_imm8(insn->off))
e430f34e 1212 EMIT2(add_1reg(0x40, dst_reg), insn->off);
62258278 1213 else
e430f34e 1214 EMIT1_off32(add_1reg(0x80, dst_reg), insn->off);
62258278 1215
e430f34e 1216 EMIT(imm32, bpf_size_to_x86_bytes(BPF_SIZE(insn->code)));
62258278
AS
1217 break;
1218
e430f34e 1219 /* STX: *(u8*)(dst_reg + off) = src_reg */
62258278 1220 case BPF_STX | BPF_MEM | BPF_B:
62258278 1221 case BPF_STX | BPF_MEM | BPF_H:
62258278 1222 case BPF_STX | BPF_MEM | BPF_W:
62258278 1223 case BPF_STX | BPF_MEM | BPF_DW:
3b2744e6 1224 emit_stx(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn->off);
62258278
AS
1225 break;
1226
e430f34e 1227 /* LDX: dst_reg = *(u8*)(src_reg + off) */
62258278 1228 case BPF_LDX | BPF_MEM | BPF_B:
3dec541b 1229 case BPF_LDX | BPF_PROBE_MEM | BPF_B:
62258278 1230 case BPF_LDX | BPF_MEM | BPF_H:
3dec541b 1231 case BPF_LDX | BPF_PROBE_MEM | BPF_H:
62258278 1232 case BPF_LDX | BPF_MEM | BPF_W:
3dec541b 1233 case BPF_LDX | BPF_PROBE_MEM | BPF_W:
62258278 1234 case BPF_LDX | BPF_MEM | BPF_DW:
3dec541b 1235 case BPF_LDX | BPF_PROBE_MEM | BPF_DW:
3b2744e6 1236 emit_ldx(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn->off);
3dec541b
AS
1237 if (BPF_MODE(insn->code) == BPF_PROBE_MEM) {
1238 struct exception_table_entry *ex;
1239 u8 *_insn = image + proglen;
1240 s64 delta;
1241
1242 if (!bpf_prog->aux->extable)
1243 break;
1244
1245 if (excnt >= bpf_prog->aux->num_exentries) {
1246 pr_err("ex gen bug\n");
1247 return -EFAULT;
1248 }
1249 ex = &bpf_prog->aux->extable[excnt++];
1250
1251 delta = _insn - (u8 *)&ex->insn;
1252 if (!is_simm32(delta)) {
1253 pr_err("extable->insn doesn't fit into 32-bit\n");
1254 return -EFAULT;
1255 }
1256 ex->insn = delta;
1257
1258 delta = (u8 *)ex_handler_bpf - (u8 *)&ex->handler;
1259 if (!is_simm32(delta)) {
1260 pr_err("extable->handler doesn't fit into 32-bit\n");
1261 return -EFAULT;
1262 }
1263 ex->handler = delta;
1264
1265 if (dst_reg > BPF_REG_9) {
1266 pr_err("verifier error\n");
1267 return -EFAULT;
1268 }
1269 /*
1270 * Compute size of x86 insn and its target dest x86 register.
1271 * ex_handler_bpf() will use lower 8 bits to adjust
1272 * pt_regs->ip to jump over this x86 instruction
1273 * and upper bits to figure out which pt_regs to zero out.
1274 * End result: x86 insn "mov rbx, qword ptr [rax+0x14]"
1275 * of 4 bytes will be ignored and rbx will be zero inited.
1276 */
1277 ex->fixup = (prog - temp) | (reg2pt_regs[dst_reg] << 8);
1278 }
62258278
AS
1279 break;
1280
91c960b0
BJ
1281 case BPF_STX | BPF_ATOMIC | BPF_W:
1282 case BPF_STX | BPF_ATOMIC | BPF_DW:
1283 err = emit_atomic(&prog, insn->imm, dst_reg, src_reg,
1284 insn->off, BPF_SIZE(insn->code));
1285 if (err)
1286 return err;
62258278
AS
1287 break;
1288
1289 /* call */
1290 case BPF_JMP | BPF_CALL:
e430f34e 1291 func = (u8 *) __bpf_call_base + imm32;
ebf7d1f5
MF
1292 if (tail_call_reachable) {
1293 EMIT3_off32(0x48, 0x8B, 0x85,
1294 -(bpf_prog->aux->stack_depth + 8));
1295 if (!imm32 || emit_call(&prog, func, image + addrs[i - 1] + 7))
1296 return -EINVAL;
1297 } else {
1298 if (!imm32 || emit_call(&prog, func, image + addrs[i - 1]))
1299 return -EINVAL;
1300 }
62258278
AS
1301 break;
1302
71189fa9 1303 case BPF_JMP | BPF_TAIL_CALL:
428d5df1
DB
1304 if (imm32)
1305 emit_bpf_tail_call_direct(&bpf_prog->aux->poke_tab[imm32 - 1],
ebf7d1f5
MF
1306 &prog, addrs[i], image,
1307 callee_regs_used,
1308 bpf_prog->aux->stack_depth);
428d5df1 1309 else
ebf7d1f5
MF
1310 emit_bpf_tail_call_indirect(&prog,
1311 callee_regs_used,
1312 bpf_prog->aux->stack_depth);
b52f00e6
AS
1313 break;
1314
62258278
AS
1315 /* cond jump */
1316 case BPF_JMP | BPF_JEQ | BPF_X:
1317 case BPF_JMP | BPF_JNE | BPF_X:
1318 case BPF_JMP | BPF_JGT | BPF_X:
52afc51e 1319 case BPF_JMP | BPF_JLT | BPF_X:
62258278 1320 case BPF_JMP | BPF_JGE | BPF_X:
52afc51e 1321 case BPF_JMP | BPF_JLE | BPF_X:
62258278 1322 case BPF_JMP | BPF_JSGT | BPF_X:
52afc51e 1323 case BPF_JMP | BPF_JSLT | BPF_X:
62258278 1324 case BPF_JMP | BPF_JSGE | BPF_X:
52afc51e 1325 case BPF_JMP | BPF_JSLE | BPF_X:
3f5d6525
JW
1326 case BPF_JMP32 | BPF_JEQ | BPF_X:
1327 case BPF_JMP32 | BPF_JNE | BPF_X:
1328 case BPF_JMP32 | BPF_JGT | BPF_X:
1329 case BPF_JMP32 | BPF_JLT | BPF_X:
1330 case BPF_JMP32 | BPF_JGE | BPF_X:
1331 case BPF_JMP32 | BPF_JLE | BPF_X:
1332 case BPF_JMP32 | BPF_JSGT | BPF_X:
1333 case BPF_JMP32 | BPF_JSLT | BPF_X:
1334 case BPF_JMP32 | BPF_JSGE | BPF_X:
1335 case BPF_JMP32 | BPF_JSLE | BPF_X:
e430f34e 1336 /* cmp dst_reg, src_reg */
74007cfc
BJ
1337 maybe_emit_mod(&prog, dst_reg, src_reg,
1338 BPF_CLASS(insn->code) == BPF_JMP);
3f5d6525 1339 EMIT2(0x39, add_2reg(0xC0, dst_reg, src_reg));
62258278
AS
1340 goto emit_cond_jmp;
1341
1342 case BPF_JMP | BPF_JSET | BPF_X:
3f5d6525 1343 case BPF_JMP32 | BPF_JSET | BPF_X:
e430f34e 1344 /* test dst_reg, src_reg */
74007cfc
BJ
1345 maybe_emit_mod(&prog, dst_reg, src_reg,
1346 BPF_CLASS(insn->code) == BPF_JMP);
3f5d6525 1347 EMIT2(0x85, add_2reg(0xC0, dst_reg, src_reg));
62258278
AS
1348 goto emit_cond_jmp;
1349
1350 case BPF_JMP | BPF_JSET | BPF_K:
3f5d6525 1351 case BPF_JMP32 | BPF_JSET | BPF_K:
e430f34e 1352 /* test dst_reg, imm32 */
3f5d6525
JW
1353 if (BPF_CLASS(insn->code) == BPF_JMP)
1354 EMIT1(add_1mod(0x48, dst_reg));
1355 else if (is_ereg(dst_reg))
1356 EMIT1(add_1mod(0x40, dst_reg));
e430f34e 1357 EMIT2_off32(0xF7, add_1reg(0xC0, dst_reg), imm32);
62258278
AS
1358 goto emit_cond_jmp;
1359
1360 case BPF_JMP | BPF_JEQ | BPF_K:
1361 case BPF_JMP | BPF_JNE | BPF_K:
1362 case BPF_JMP | BPF_JGT | BPF_K:
52afc51e 1363 case BPF_JMP | BPF_JLT | BPF_K:
62258278 1364 case BPF_JMP | BPF_JGE | BPF_K:
52afc51e 1365 case BPF_JMP | BPF_JLE | BPF_K:
62258278 1366 case BPF_JMP | BPF_JSGT | BPF_K:
52afc51e 1367 case BPF_JMP | BPF_JSLT | BPF_K:
62258278 1368 case BPF_JMP | BPF_JSGE | BPF_K:
52afc51e 1369 case BPF_JMP | BPF_JSLE | BPF_K:
3f5d6525
JW
1370 case BPF_JMP32 | BPF_JEQ | BPF_K:
1371 case BPF_JMP32 | BPF_JNE | BPF_K:
1372 case BPF_JMP32 | BPF_JGT | BPF_K:
1373 case BPF_JMP32 | BPF_JLT | BPF_K:
1374 case BPF_JMP32 | BPF_JGE | BPF_K:
1375 case BPF_JMP32 | BPF_JLE | BPF_K:
1376 case BPF_JMP32 | BPF_JSGT | BPF_K:
1377 case BPF_JMP32 | BPF_JSLT | BPF_K:
1378 case BPF_JMP32 | BPF_JSGE | BPF_K:
1379 case BPF_JMP32 | BPF_JSLE | BPF_K:
38f51c07
DB
1380 /* test dst_reg, dst_reg to save one extra byte */
1381 if (imm32 == 0) {
74007cfc
BJ
1382 maybe_emit_mod(&prog, dst_reg, dst_reg,
1383 BPF_CLASS(insn->code) == BPF_JMP);
38f51c07
DB
1384 EMIT2(0x85, add_2reg(0xC0, dst_reg, dst_reg));
1385 goto emit_cond_jmp;
1386 }
1387
e430f34e 1388 /* cmp dst_reg, imm8/32 */
3f5d6525
JW
1389 if (BPF_CLASS(insn->code) == BPF_JMP)
1390 EMIT1(add_1mod(0x48, dst_reg));
1391 else if (is_ereg(dst_reg))
1392 EMIT1(add_1mod(0x40, dst_reg));
62258278 1393
e430f34e
AS
1394 if (is_imm8(imm32))
1395 EMIT3(0x83, add_1reg(0xF8, dst_reg), imm32);
62258278 1396 else
e430f34e 1397 EMIT2_off32(0x81, add_1reg(0xF8, dst_reg), imm32);
62258278 1398
a2c7a983 1399emit_cond_jmp: /* Convert BPF opcode to x86 */
62258278
AS
1400 switch (BPF_OP(insn->code)) {
1401 case BPF_JEQ:
1402 jmp_cond = X86_JE;
1403 break;
1404 case BPF_JSET:
1405 case BPF_JNE:
1406 jmp_cond = X86_JNE;
1407 break;
1408 case BPF_JGT:
1409 /* GT is unsigned '>', JA in x86 */
1410 jmp_cond = X86_JA;
1411 break;
52afc51e
DB
1412 case BPF_JLT:
1413 /* LT is unsigned '<', JB in x86 */
1414 jmp_cond = X86_JB;
1415 break;
62258278
AS
1416 case BPF_JGE:
1417 /* GE is unsigned '>=', JAE in x86 */
1418 jmp_cond = X86_JAE;
1419 break;
52afc51e
DB
1420 case BPF_JLE:
1421 /* LE is unsigned '<=', JBE in x86 */
1422 jmp_cond = X86_JBE;
1423 break;
62258278 1424 case BPF_JSGT:
a2c7a983 1425 /* Signed '>', GT in x86 */
62258278
AS
1426 jmp_cond = X86_JG;
1427 break;
52afc51e 1428 case BPF_JSLT:
a2c7a983 1429 /* Signed '<', LT in x86 */
52afc51e
DB
1430 jmp_cond = X86_JL;
1431 break;
62258278 1432 case BPF_JSGE:
a2c7a983 1433 /* Signed '>=', GE in x86 */
62258278
AS
1434 jmp_cond = X86_JGE;
1435 break;
52afc51e 1436 case BPF_JSLE:
a2c7a983 1437 /* Signed '<=', LE in x86 */
52afc51e
DB
1438 jmp_cond = X86_JLE;
1439 break;
a2c7a983 1440 default: /* to silence GCC warning */
62258278
AS
1441 return -EFAULT;
1442 }
1443 jmp_offset = addrs[i + insn->off] - addrs[i];
1444 if (is_imm8(jmp_offset)) {
1445 EMIT2(jmp_cond, jmp_offset);
1446 } else if (is_simm32(jmp_offset)) {
1447 EMIT2_off32(0x0F, jmp_cond + 0x10, jmp_offset);
1448 } else {
1449 pr_err("cond_jmp gen bug %llx\n", jmp_offset);
1450 return -EFAULT;
1451 }
1452
1453 break;
0a14842f 1454
62258278 1455 case BPF_JMP | BPF_JA:
1612a981
GB
1456 if (insn->off == -1)
1457 /* -1 jmp instructions will always jump
1458 * backwards two bytes. Explicitly handling
1459 * this case avoids wasting too many passes
1460 * when there are long sequences of replaced
1461 * dead code.
1462 */
1463 jmp_offset = -2;
1464 else
1465 jmp_offset = addrs[i + insn->off] - addrs[i];
1466
62258278 1467 if (!jmp_offset)
a2c7a983 1468 /* Optimize out nop jumps */
62258278
AS
1469 break;
1470emit_jmp:
1471 if (is_imm8(jmp_offset)) {
1472 EMIT2(0xEB, jmp_offset);
1473 } else if (is_simm32(jmp_offset)) {
1474 EMIT1_off32(0xE9, jmp_offset);
1475 } else {
1476 pr_err("jmp gen bug %llx\n", jmp_offset);
1477 return -EFAULT;
1478 }
1479 break;
1480
62258278 1481 case BPF_JMP | BPF_EXIT:
769e0de6 1482 if (seen_exit) {
62258278
AS
1483 jmp_offset = ctx->cleanup_addr - addrs[i];
1484 goto emit_jmp;
1485 }
769e0de6 1486 seen_exit = true;
a2c7a983 1487 /* Update cleanup_addr */
62258278 1488 ctx->cleanup_addr = proglen;
ebf7d1f5 1489 pop_callee_regs(&prog, callee_regs_used);
fe8d9571
AS
1490 EMIT1(0xC9); /* leave */
1491 EMIT1(0xC3); /* ret */
62258278
AS
1492 break;
1493
f3c2af7b 1494 default:
a2c7a983
IM
1495 /*
1496 * By design x86-64 JIT should support all BPF instructions.
62258278 1497 * This error will be seen if new instruction was added
a2c7a983
IM
1498 * to the interpreter, but not to the JIT, or if there is
1499 * junk in bpf_prog.
62258278
AS
1500 */
1501 pr_err("bpf_jit: unknown opcode %02x\n", insn->code);
f3c2af7b
AS
1502 return -EINVAL;
1503 }
62258278 1504
f3c2af7b 1505 ilen = prog - temp;
e0ee9c12 1506 if (ilen > BPF_MAX_INSN_SIZE) {
9383191d 1507 pr_err("bpf_jit: fatal insn size error\n");
e0ee9c12
AS
1508 return -EFAULT;
1509 }
1510
f3c2af7b
AS
1511 if (image) {
1512 if (unlikely(proglen + ilen > oldproglen)) {
9383191d 1513 pr_err("bpf_jit: fatal error\n");
f3c2af7b 1514 return -EFAULT;
0a14842f 1515 }
f3c2af7b 1516 memcpy(image + proglen, temp, ilen);
0a14842f 1517 }
f3c2af7b
AS
1518 proglen += ilen;
1519 addrs[i] = proglen;
1520 prog = temp;
1521 }
3dec541b
AS
1522
1523 if (image && excnt != bpf_prog->aux->num_exentries) {
1524 pr_err("extable is not populated\n");
1525 return -EFAULT;
1526 }
f3c2af7b
AS
1527 return proglen;
1528}
1529
85d33df3 1530static void save_regs(const struct btf_func_model *m, u8 **prog, int nr_args,
fec56f58
AS
1531 int stack_size)
1532{
1533 int i;
1534 /* Store function arguments to stack.
1535 * For a function that accepts two pointers the sequence will be:
1536 * mov QWORD PTR [rbp-0x10],rdi
1537 * mov QWORD PTR [rbp-0x8],rsi
1538 */
1539 for (i = 0; i < min(nr_args, 6); i++)
1540 emit_stx(prog, bytes_to_bpf_size(m->arg_size[i]),
1541 BPF_REG_FP,
1542 i == 5 ? X86_REG_R9 : BPF_REG_1 + i,
1543 -(stack_size - i * 8));
1544}
1545
85d33df3 1546static void restore_regs(const struct btf_func_model *m, u8 **prog, int nr_args,
fec56f58
AS
1547 int stack_size)
1548{
1549 int i;
1550
1551 /* Restore function arguments from stack.
1552 * For a function that accepts two pointers the sequence will be:
1553 * EMIT4(0x48, 0x8B, 0x7D, 0xF0); mov rdi,QWORD PTR [rbp-0x10]
1554 * EMIT4(0x48, 0x8B, 0x75, 0xF8); mov rsi,QWORD PTR [rbp-0x8]
1555 */
1556 for (i = 0; i < min(nr_args, 6); i++)
1557 emit_ldx(prog, bytes_to_bpf_size(m->arg_size[i]),
1558 i == 5 ? X86_REG_R9 : BPF_REG_1 + i,
1559 BPF_REG_FP,
1560 -(stack_size - i * 8));
1561}
1562
7e639208 1563static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog,
ae240823 1564 struct bpf_prog *p, int stack_size, bool mod_ret)
7e639208
KS
1565{
1566 u8 *prog = *pprog;
1567 int cnt = 0;
1568
1e6c62a8
AS
1569 if (p->aux->sleepable) {
1570 if (emit_call(&prog, __bpf_prog_enter_sleepable, prog))
1571 return -EINVAL;
1572 } else {
1573 if (emit_call(&prog, __bpf_prog_enter, prog))
1574 return -EINVAL;
1575 /* remember prog start time returned by __bpf_prog_enter */
1576 emit_mov_reg(&prog, true, BPF_REG_6, BPF_REG_0);
1577 }
7e639208
KS
1578
1579 /* arg1: lea rdi, [rbp - stack_size] */
1580 EMIT4(0x48, 0x8D, 0x7D, -stack_size);
1581 /* arg2: progs[i]->insnsi for interpreter */
1582 if (!p->jited)
1583 emit_mov_imm64(&prog, BPF_REG_2,
1584 (long) p->insnsi >> 32,
1585 (u32) (long) p->insnsi);
1586 /* call JITed bpf program or interpreter */
1587 if (emit_call(&prog, p->bpf_func, prog))
1588 return -EINVAL;
1589
ae240823
KS
1590 /* BPF_TRAMP_MODIFY_RETURN trampolines can modify the return
1591 * of the previous call which is then passed on the stack to
1592 * the next BPF program.
1593 */
1594 if (mod_ret)
1595 emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -8);
1596
1e6c62a8
AS
1597 if (p->aux->sleepable) {
1598 if (emit_call(&prog, __bpf_prog_exit_sleepable, prog))
1599 return -EINVAL;
1600 } else {
1601 /* arg1: mov rdi, progs[i] */
1602 emit_mov_imm64(&prog, BPF_REG_1, (long) p >> 32,
1603 (u32) (long) p);
1604 /* arg2: mov rsi, rbx <- start time in nsec */
1605 emit_mov_reg(&prog, true, BPF_REG_2, BPF_REG_6);
1606 if (emit_call(&prog, __bpf_prog_exit, prog))
1607 return -EINVAL;
1608 }
7e639208
KS
1609
1610 *pprog = prog;
1611 return 0;
1612}
1613
1614static void emit_nops(u8 **pprog, unsigned int len)
1615{
1616 unsigned int i, noplen;
1617 u8 *prog = *pprog;
1618 int cnt = 0;
1619
1620 while (len > 0) {
1621 noplen = len;
1622
1623 if (noplen > ASM_NOP_MAX)
1624 noplen = ASM_NOP_MAX;
1625
1626 for (i = 0; i < noplen; i++)
1627 EMIT1(ideal_nops[noplen][i]);
1628 len -= noplen;
1629 }
1630
1631 *pprog = prog;
1632}
1633
1634static void emit_align(u8 **pprog, u32 align)
1635{
1636 u8 *target, *prog = *pprog;
1637
1638 target = PTR_ALIGN(prog, align);
1639 if (target != prog)
1640 emit_nops(&prog, target - prog);
1641
1642 *pprog = prog;
1643}
1644
1645static int emit_cond_near_jump(u8 **pprog, void *func, void *ip, u8 jmp_cond)
1646{
1647 u8 *prog = *pprog;
1648 int cnt = 0;
1649 s64 offset;
1650
1651 offset = func - (ip + 2 + 4);
1652 if (!is_simm32(offset)) {
1653 pr_err("Target %p is out of range\n", func);
1654 return -EINVAL;
1655 }
1656 EMIT2_off32(0x0F, jmp_cond + 0x10, offset);
1657 *pprog = prog;
1658 return 0;
1659}
1660
85d33df3 1661static int invoke_bpf(const struct btf_func_model *m, u8 **pprog,
88fd9e53 1662 struct bpf_tramp_progs *tp, int stack_size)
fec56f58 1663{
7e639208 1664 int i;
fec56f58 1665 u8 *prog = *pprog;
fec56f58 1666
88fd9e53 1667 for (i = 0; i < tp->nr_progs; i++) {
ae240823
KS
1668 if (invoke_bpf_prog(m, &prog, tp->progs[i], stack_size, false))
1669 return -EINVAL;
1670 }
1671 *pprog = prog;
1672 return 0;
1673}
1674
1675static int invoke_bpf_mod_ret(const struct btf_func_model *m, u8 **pprog,
1676 struct bpf_tramp_progs *tp, int stack_size,
1677 u8 **branches)
1678{
1679 u8 *prog = *pprog;
13fac1d8 1680 int i, cnt = 0;
ae240823
KS
1681
1682 /* The first fmod_ret program will receive a garbage return value.
1683 * Set this to 0 to avoid confusing the program.
1684 */
1685 emit_mov_imm32(&prog, false, BPF_REG_0, 0);
1686 emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -8);
1687 for (i = 0; i < tp->nr_progs; i++) {
1688 if (invoke_bpf_prog(m, &prog, tp->progs[i], stack_size, true))
fec56f58 1689 return -EINVAL;
ae240823 1690
13fac1d8
AS
1691 /* mod_ret prog stored return value into [rbp - 8]. Emit:
1692 * if (*(u64 *)(rbp - 8) != 0)
ae240823 1693 * goto do_fexit;
ae240823 1694 */
13fac1d8
AS
1695 /* cmp QWORD PTR [rbp - 0x8], 0x0 */
1696 EMIT4(0x48, 0x83, 0x7d, 0xf8); EMIT1(0x00);
ae240823
KS
1697
1698 /* Save the location of the branch and Generate 6 nops
1699 * (4 bytes for an offset and 2 bytes for the jump) These nops
1700 * are replaced with a conditional jump once do_fexit (i.e. the
1701 * start of the fexit invocation) is finalized.
1702 */
1703 branches[i] = prog;
1704 emit_nops(&prog, 4 + 2);
fec56f58 1705 }
ae240823 1706
fec56f58
AS
1707 *pprog = prog;
1708 return 0;
1709}
1710
1711/* Example:
1712 * __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev);
1713 * its 'struct btf_func_model' will be nr_args=2
1714 * The assembly code when eth_type_trans is executing after trampoline:
1715 *
1716 * push rbp
1717 * mov rbp, rsp
1718 * sub rsp, 16 // space for skb and dev
1719 * push rbx // temp regs to pass start time
1720 * mov qword ptr [rbp - 16], rdi // save skb pointer to stack
1721 * mov qword ptr [rbp - 8], rsi // save dev pointer to stack
1722 * call __bpf_prog_enter // rcu_read_lock and preempt_disable
1723 * mov rbx, rax // remember start time in bpf stats are enabled
1724 * lea rdi, [rbp - 16] // R1==ctx of bpf prog
1725 * call addr_of_jited_FENTRY_prog
1726 * movabsq rdi, 64bit_addr_of_struct_bpf_prog // unused if bpf stats are off
1727 * mov rsi, rbx // prog start time
1728 * call __bpf_prog_exit // rcu_read_unlock, preempt_enable and stats math
1729 * mov rdi, qword ptr [rbp - 16] // restore skb pointer from stack
1730 * mov rsi, qword ptr [rbp - 8] // restore dev pointer from stack
1731 * pop rbx
1732 * leave
1733 * ret
1734 *
1735 * eth_type_trans has 5 byte nop at the beginning. These 5 bytes will be
1736 * replaced with 'call generated_bpf_trampoline'. When it returns
1737 * eth_type_trans will continue executing with original skb and dev pointers.
1738 *
1739 * The assembly code when eth_type_trans is called from trampoline:
1740 *
1741 * push rbp
1742 * mov rbp, rsp
1743 * sub rsp, 24 // space for skb, dev, return value
1744 * push rbx // temp regs to pass start time
1745 * mov qword ptr [rbp - 24], rdi // save skb pointer to stack
1746 * mov qword ptr [rbp - 16], rsi // save dev pointer to stack
1747 * call __bpf_prog_enter // rcu_read_lock and preempt_disable
1748 * mov rbx, rax // remember start time if bpf stats are enabled
1749 * lea rdi, [rbp - 24] // R1==ctx of bpf prog
1750 * call addr_of_jited_FENTRY_prog // bpf prog can access skb and dev
1751 * movabsq rdi, 64bit_addr_of_struct_bpf_prog // unused if bpf stats are off
1752 * mov rsi, rbx // prog start time
1753 * call __bpf_prog_exit // rcu_read_unlock, preempt_enable and stats math
1754 * mov rdi, qword ptr [rbp - 24] // restore skb pointer from stack
1755 * mov rsi, qword ptr [rbp - 16] // restore dev pointer from stack
1756 * call eth_type_trans+5 // execute body of eth_type_trans
1757 * mov qword ptr [rbp - 8], rax // save return value
1758 * call __bpf_prog_enter // rcu_read_lock and preempt_disable
1759 * mov rbx, rax // remember start time in bpf stats are enabled
1760 * lea rdi, [rbp - 24] // R1==ctx of bpf prog
1761 * call addr_of_jited_FEXIT_prog // bpf prog can access skb, dev, return value
1762 * movabsq rdi, 64bit_addr_of_struct_bpf_prog // unused if bpf stats are off
1763 * mov rsi, rbx // prog start time
1764 * call __bpf_prog_exit // rcu_read_unlock, preempt_enable and stats math
1765 * mov rax, qword ptr [rbp - 8] // restore eth_type_trans's return value
1766 * pop rbx
1767 * leave
1768 * add rsp, 8 // skip eth_type_trans's frame
1769 * ret // return to its caller
1770 */
85d33df3
MKL
1771int arch_prepare_bpf_trampoline(void *image, void *image_end,
1772 const struct btf_func_model *m, u32 flags,
88fd9e53 1773 struct bpf_tramp_progs *tprogs,
fec56f58
AS
1774 void *orig_call)
1775{
ae240823 1776 int ret, i, cnt = 0, nr_args = m->nr_args;
fec56f58 1777 int stack_size = nr_args * 8;
88fd9e53
KS
1778 struct bpf_tramp_progs *fentry = &tprogs[BPF_TRAMP_FENTRY];
1779 struct bpf_tramp_progs *fexit = &tprogs[BPF_TRAMP_FEXIT];
ae240823
KS
1780 struct bpf_tramp_progs *fmod_ret = &tprogs[BPF_TRAMP_MODIFY_RETURN];
1781 u8 **branches = NULL;
fec56f58
AS
1782 u8 *prog;
1783
1784 /* x86-64 supports up to 6 arguments. 7+ can be added in the future */
1785 if (nr_args > 6)
1786 return -ENOTSUPP;
1787
1788 if ((flags & BPF_TRAMP_F_RESTORE_REGS) &&
1789 (flags & BPF_TRAMP_F_SKIP_FRAME))
1790 return -EINVAL;
1791
1792 if (flags & BPF_TRAMP_F_CALL_ORIG)
1793 stack_size += 8; /* room for return value of orig_call */
1794
1795 if (flags & BPF_TRAMP_F_SKIP_FRAME)
1796 /* skip patched call instruction and point orig_call to actual
1797 * body of the kernel function.
1798 */
4b3da77b 1799 orig_call += X86_PATCH_SIZE;
fec56f58
AS
1800
1801 prog = image;
1802
1803 EMIT1(0x55); /* push rbp */
1804 EMIT3(0x48, 0x89, 0xE5); /* mov rbp, rsp */
1805 EMIT4(0x48, 0x83, 0xEC, stack_size); /* sub rsp, stack_size */
1806 EMIT1(0x53); /* push rbx */
1807
1808 save_regs(m, &prog, nr_args, stack_size);
1809
88fd9e53
KS
1810 if (fentry->nr_progs)
1811 if (invoke_bpf(m, &prog, fentry, stack_size))
fec56f58
AS
1812 return -EINVAL;
1813
ae240823
KS
1814 if (fmod_ret->nr_progs) {
1815 branches = kcalloc(fmod_ret->nr_progs, sizeof(u8 *),
1816 GFP_KERNEL);
1817 if (!branches)
1818 return -ENOMEM;
1819
1820 if (invoke_bpf_mod_ret(m, &prog, fmod_ret, stack_size,
1821 branches)) {
1822 ret = -EINVAL;
1823 goto cleanup;
1824 }
1825 }
1826
fec56f58 1827 if (flags & BPF_TRAMP_F_CALL_ORIG) {
ae240823 1828 if (fentry->nr_progs || fmod_ret->nr_progs)
fec56f58
AS
1829 restore_regs(m, &prog, nr_args, stack_size);
1830
1831 /* call original function */
ae240823
KS
1832 if (emit_call(&prog, orig_call, prog)) {
1833 ret = -EINVAL;
1834 goto cleanup;
1835 }
fec56f58
AS
1836 /* remember return value in a stack for bpf prog to access */
1837 emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -8);
1838 }
1839
ae240823
KS
1840 if (fmod_ret->nr_progs) {
1841 /* From Intel 64 and IA-32 Architectures Optimization
1842 * Reference Manual, 3.4.1.4 Code Alignment, Assembly/Compiler
1843 * Coding Rule 11: All branch targets should be 16-byte
1844 * aligned.
1845 */
1846 emit_align(&prog, 16);
1847 /* Update the branches saved in invoke_bpf_mod_ret with the
1848 * aligned address of do_fexit.
1849 */
1850 for (i = 0; i < fmod_ret->nr_progs; i++)
1851 emit_cond_near_jump(&branches[i], prog, branches[i],
1852 X86_JNE);
1853 }
1854
88fd9e53 1855 if (fexit->nr_progs)
ae240823
KS
1856 if (invoke_bpf(m, &prog, fexit, stack_size)) {
1857 ret = -EINVAL;
1858 goto cleanup;
1859 }
fec56f58
AS
1860
1861 if (flags & BPF_TRAMP_F_RESTORE_REGS)
1862 restore_regs(m, &prog, nr_args, stack_size);
1863
ae240823
KS
1864 /* This needs to be done regardless. If there were fmod_ret programs,
1865 * the return value is only updated on the stack and still needs to be
1866 * restored to R0.
1867 */
fec56f58
AS
1868 if (flags & BPF_TRAMP_F_CALL_ORIG)
1869 /* restore original return value back into RAX */
1870 emit_ldx(&prog, BPF_DW, BPF_REG_0, BPF_REG_FP, -8);
1871
1872 EMIT1(0x5B); /* pop rbx */
1873 EMIT1(0xC9); /* leave */
1874 if (flags & BPF_TRAMP_F_SKIP_FRAME)
1875 /* skip our return address and return to parent */
1876 EMIT4(0x48, 0x83, 0xC4, 8); /* add rsp, 8 */
1877 EMIT1(0xC3); /* ret */
85d33df3 1878 /* Make sure the trampoline generation logic doesn't overflow */
ae240823
KS
1879 if (WARN_ON_ONCE(prog > (u8 *)image_end - BPF_INSN_SAFETY)) {
1880 ret = -EFAULT;
1881 goto cleanup;
1882 }
1883 ret = prog - (u8 *)image;
1884
1885cleanup:
1886 kfree(branches);
1887 return ret;
fec56f58
AS
1888}
1889
75ccbef6
BT
1890static int emit_fallback_jump(u8 **pprog)
1891{
1892 u8 *prog = *pprog;
1893 int err = 0;
1894
1895#ifdef CONFIG_RETPOLINE
1896 /* Note that this assumes the the compiler uses external
1897 * thunks for indirect calls. Both clang and GCC use the same
1898 * naming convention for external thunks.
1899 */
1900 err = emit_jump(&prog, __x86_indirect_thunk_rdx, prog);
1901#else
1902 int cnt = 0;
1903
1904 EMIT2(0xFF, 0xE2); /* jmp rdx */
1905#endif
1906 *pprog = prog;
1907 return err;
1908}
1909
1910static int emit_bpf_dispatcher(u8 **pprog, int a, int b, s64 *progs)
1911{
7e639208 1912 u8 *jg_reloc, *prog = *pprog;
75ccbef6 1913 int pivot, err, jg_bytes = 1, cnt = 0;
75ccbef6
BT
1914 s64 jg_offset;
1915
1916 if (a == b) {
1917 /* Leaf node of recursion, i.e. not a range of indices
1918 * anymore.
1919 */
1920 EMIT1(add_1mod(0x48, BPF_REG_3)); /* cmp rdx,func */
1921 if (!is_simm32(progs[a]))
1922 return -1;
1923 EMIT2_off32(0x81, add_1reg(0xF8, BPF_REG_3),
1924 progs[a]);
1925 err = emit_cond_near_jump(&prog, /* je func */
1926 (void *)progs[a], prog,
1927 X86_JE);
1928 if (err)
1929 return err;
1930
1931 err = emit_fallback_jump(&prog); /* jmp thunk/indirect */
1932 if (err)
1933 return err;
1934
1935 *pprog = prog;
1936 return 0;
1937 }
1938
1939 /* Not a leaf node, so we pivot, and recursively descend into
1940 * the lower and upper ranges.
1941 */
1942 pivot = (b - a) / 2;
1943 EMIT1(add_1mod(0x48, BPF_REG_3)); /* cmp rdx,func */
1944 if (!is_simm32(progs[a + pivot]))
1945 return -1;
1946 EMIT2_off32(0x81, add_1reg(0xF8, BPF_REG_3), progs[a + pivot]);
1947
1948 if (pivot > 2) { /* jg upper_part */
1949 /* Require near jump. */
1950 jg_bytes = 4;
1951 EMIT2_off32(0x0F, X86_JG + 0x10, 0);
1952 } else {
1953 EMIT2(X86_JG, 0);
1954 }
1955 jg_reloc = prog;
1956
1957 err = emit_bpf_dispatcher(&prog, a, a + pivot, /* emit lower_part */
1958 progs);
1959 if (err)
1960 return err;
1961
116eb788
BT
1962 /* From Intel 64 and IA-32 Architectures Optimization
1963 * Reference Manual, 3.4.1.4 Code Alignment, Assembly/Compiler
1964 * Coding Rule 11: All branch targets should be 16-byte
1965 * aligned.
1966 */
7e639208 1967 emit_align(&prog, 16);
75ccbef6
BT
1968 jg_offset = prog - jg_reloc;
1969 emit_code(jg_reloc - jg_bytes, jg_offset, jg_bytes);
1970
1971 err = emit_bpf_dispatcher(&prog, a + pivot + 1, /* emit upper_part */
1972 b, progs);
1973 if (err)
1974 return err;
1975
1976 *pprog = prog;
1977 return 0;
1978}
1979
1980static int cmp_ips(const void *a, const void *b)
1981{
1982 const s64 *ipa = a;
1983 const s64 *ipb = b;
1984
1985 if (*ipa > *ipb)
1986 return 1;
1987 if (*ipa < *ipb)
1988 return -1;
1989 return 0;
1990}
1991
1992int arch_prepare_bpf_dispatcher(void *image, s64 *funcs, int num_funcs)
1993{
1994 u8 *prog = image;
1995
1996 sort(funcs, num_funcs, sizeof(funcs[0]), cmp_ips, NULL);
1997 return emit_bpf_dispatcher(&prog, 0, num_funcs - 1, funcs);
1998}
1999
1c2a088a
AS
2000struct x64_jit_data {
2001 struct bpf_binary_header *header;
2002 int *addrs;
2003 u8 *image;
2004 int proglen;
2005 struct jit_context ctx;
2006};
2007
d1c55ab5 2008struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
f3c2af7b
AS
2009{
2010 struct bpf_binary_header *header = NULL;
959a7579 2011 struct bpf_prog *tmp, *orig_prog = prog;
1c2a088a 2012 struct x64_jit_data *jit_data;
f3c2af7b
AS
2013 int proglen, oldproglen = 0;
2014 struct jit_context ctx = {};
959a7579 2015 bool tmp_blinded = false;
1c2a088a 2016 bool extra_pass = false;
f3c2af7b
AS
2017 u8 *image = NULL;
2018 int *addrs;
2019 int pass;
2020 int i;
2021
60b58afc 2022 if (!prog->jit_requested)
959a7579
DB
2023 return orig_prog;
2024
2025 tmp = bpf_jit_blind_constants(prog);
a2c7a983
IM
2026 /*
2027 * If blinding was requested and we failed during blinding,
959a7579
DB
2028 * we must fall back to the interpreter.
2029 */
2030 if (IS_ERR(tmp))
2031 return orig_prog;
2032 if (tmp != prog) {
2033 tmp_blinded = true;
2034 prog = tmp;
2035 }
0a14842f 2036
1c2a088a
AS
2037 jit_data = prog->aux->jit_data;
2038 if (!jit_data) {
2039 jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
2040 if (!jit_data) {
2041 prog = orig_prog;
2042 goto out;
2043 }
2044 prog->aux->jit_data = jit_data;
2045 }
2046 addrs = jit_data->addrs;
2047 if (addrs) {
2048 ctx = jit_data->ctx;
2049 oldproglen = jit_data->proglen;
2050 image = jit_data->image;
2051 header = jit_data->header;
2052 extra_pass = true;
2053 goto skip_init_addrs;
2054 }
7c2e988f 2055 addrs = kmalloc_array(prog->len + 1, sizeof(*addrs), GFP_KERNEL);
959a7579
DB
2056 if (!addrs) {
2057 prog = orig_prog;
1c2a088a 2058 goto out_addrs;
959a7579 2059 }
f3c2af7b 2060
a2c7a983
IM
2061 /*
2062 * Before first pass, make a rough estimation of addrs[]
2063 * each BPF instruction is translated to less than 64 bytes
f3c2af7b 2064 */
7c2e988f 2065 for (proglen = 0, i = 0; i <= prog->len; i++) {
f3c2af7b
AS
2066 proglen += 64;
2067 addrs[i] = proglen;
2068 }
2069 ctx.cleanup_addr = proglen;
1c2a088a 2070skip_init_addrs:
f3c2af7b 2071
a2c7a983
IM
2072 /*
2073 * JITed image shrinks with every pass and the loop iterates
2074 * until the image stops shrinking. Very large BPF programs
3f7352bf 2075 * may converge on the last pass. In such case do one more
a2c7a983 2076 * pass to emit the final image.
3f7352bf 2077 */
6007b080 2078 for (pass = 0; pass < 20 || image; pass++) {
f3c2af7b
AS
2079 proglen = do_jit(prog, addrs, image, oldproglen, &ctx);
2080 if (proglen <= 0) {
3aab8884 2081out_image:
f3c2af7b
AS
2082 image = NULL;
2083 if (header)
738cbe72 2084 bpf_jit_binary_free(header);
959a7579
DB
2085 prog = orig_prog;
2086 goto out_addrs;
f3c2af7b 2087 }
0a14842f 2088 if (image) {
e0ee9c12 2089 if (proglen != oldproglen) {
f3c2af7b
AS
2090 pr_err("bpf_jit: proglen=%d != oldproglen=%d\n",
2091 proglen, oldproglen);
3aab8884 2092 goto out_image;
e0ee9c12 2093 }
0a14842f
ED
2094 break;
2095 }
2096 if (proglen == oldproglen) {
3dec541b
AS
2097 /*
2098 * The number of entries in extable is the number of BPF_LDX
2099 * insns that access kernel memory via "pointer to BTF type".
2100 * The verifier changed their opcode from LDX|MEM|size
2101 * to LDX|PROBE_MEM|size to make JITing easier.
2102 */
2103 u32 align = __alignof__(struct exception_table_entry);
2104 u32 extable_size = prog->aux->num_exentries *
2105 sizeof(struct exception_table_entry);
2106
2107 /* allocate module memory for x86 insns and extable */
2108 header = bpf_jit_binary_alloc(roundup(proglen, align) + extable_size,
2109 &image, align, jit_fill_hole);
959a7579
DB
2110 if (!header) {
2111 prog = orig_prog;
2112 goto out_addrs;
2113 }
3dec541b 2114 prog->aux->extable = (void *) image + roundup(proglen, align);
0a14842f
ED
2115 }
2116 oldproglen = proglen;
6007b080 2117 cond_resched();
0a14842f 2118 }
79617801 2119
0a14842f 2120 if (bpf_jit_enable > 1)
485d6511 2121 bpf_jit_dump(prog->len, proglen, pass + 1, image);
0a14842f
ED
2122
2123 if (image) {
1c2a088a 2124 if (!prog->is_func || extra_pass) {
428d5df1 2125 bpf_tail_call_direct_fixup(prog);
1c2a088a
AS
2126 bpf_jit_binary_lock_ro(header);
2127 } else {
2128 jit_data->addrs = addrs;
2129 jit_data->ctx = ctx;
2130 jit_data->proglen = proglen;
2131 jit_data->image = image;
2132 jit_data->header = header;
2133 }
f3c2af7b 2134 prog->bpf_func = (void *)image;
a91263d5 2135 prog->jited = 1;
783d28dd 2136 prog->jited_len = proglen;
9d5ecb09
DB
2137 } else {
2138 prog = orig_prog;
0a14842f 2139 }
959a7579 2140
39f56ca9 2141 if (!image || !prog->is_func || extra_pass) {
c454a46b 2142 if (image)
7c2e988f 2143 bpf_prog_fill_jited_linfo(prog, addrs + 1);
959a7579 2144out_addrs:
1c2a088a
AS
2145 kfree(addrs);
2146 kfree(jit_data);
2147 prog->aux->jit_data = NULL;
2148 }
959a7579
DB
2149out:
2150 if (tmp_blinded)
2151 bpf_jit_prog_release_other(prog, prog == orig_prog ?
2152 tmp : orig_prog);
d1c55ab5 2153 return prog;
0a14842f 2154}