1
2
3
4
5
6
7
8
9
10
11
12#include <linux/netdevice.h>
13#include <linux/filter.h>
14#include <linux/if_vlan.h>
15#include <asm/cacheflush.h>
16#include <asm/set_memory.h>
17#include <asm/nospec-branch.h>
18#include <asm/asm-prototypes.h>
19#include <linux/bpf.h>
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51static u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len)
52{
53 if (len == 1)
54 *ptr = bytes;
55 else if (len == 2)
56 *(u16 *)ptr = bytes;
57 else {
58 *(u32 *)ptr = bytes;
59 barrier();
60 }
61 return ptr + len;
62}
63
64#define EMIT(bytes, len) \
65 do { prog = emit_code(prog, bytes, len); cnt += len; } while (0)
66
67#define EMIT1(b1) EMIT(b1, 1)
68#define EMIT2(b1, b2) EMIT((b1) + ((b2) << 8), 2)
69#define EMIT3(b1, b2, b3) EMIT((b1) + ((b2) << 8) + ((b3) << 16), 3)
70#define EMIT4(b1, b2, b3, b4) \
71 EMIT((b1) + ((b2) << 8) + ((b3) << 16) + ((b4) << 24), 4)
72
73#define EMIT1_off32(b1, off) \
74 do { EMIT1(b1); EMIT(off, 4); } while (0)
75#define EMIT2_off32(b1, b2, off) \
76 do { EMIT2(b1, b2); EMIT(off, 4); } while (0)
77#define EMIT3_off32(b1, b2, b3, off) \
78 do { EMIT3(b1, b2, b3); EMIT(off, 4); } while (0)
79#define EMIT4_off32(b1, b2, b3, b4, off) \
80 do { EMIT4(b1, b2, b3, b4); EMIT(off, 4); } while (0)
81
82#define jmp_label(label, jmp_insn_len) (label - cnt - jmp_insn_len)
83
84static bool is_imm8(int value)
85{
86 return value <= 127 && value >= -128;
87}
88
89static bool is_simm32(s64 value)
90{
91 return value == (s64) (s32) value;
92}
93
94#define STACK_OFFSET(k) (k)
95#define TCALL_CNT (MAX_BPF_JIT_REG + 0)
96
97#define IA32_EAX (0x0)
98#define IA32_EBX (0x3)
99#define IA32_ECX (0x1)
100#define IA32_EDX (0x2)
101#define IA32_ESI (0x6)
102#define IA32_EDI (0x7)
103#define IA32_EBP (0x5)
104#define IA32_ESP (0x4)
105
106
107
108
109
110#define IA32_JB 0x72
111#define IA32_JAE 0x73
112#define IA32_JE 0x74
113#define IA32_JNE 0x75
114#define IA32_JBE 0x76
115#define IA32_JA 0x77
116#define IA32_JL 0x7C
117#define IA32_JGE 0x7D
118#define IA32_JLE 0x7E
119#define IA32_JG 0x7F
120
121#define COND_JMP_OPCODE_INVALID (0xFF)
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138static const u8 bpf2ia32[][2] = {
139
140 [BPF_REG_0] = {STACK_OFFSET(0), STACK_OFFSET(4)},
141
142
143
144 [BPF_REG_1] = {STACK_OFFSET(8), STACK_OFFSET(12)},
145 [BPF_REG_2] = {STACK_OFFSET(16), STACK_OFFSET(20)},
146 [BPF_REG_3] = {STACK_OFFSET(24), STACK_OFFSET(28)},
147 [BPF_REG_4] = {STACK_OFFSET(32), STACK_OFFSET(36)},
148 [BPF_REG_5] = {STACK_OFFSET(40), STACK_OFFSET(44)},
149
150
151
152 [BPF_REG_6] = {STACK_OFFSET(48), STACK_OFFSET(52)},
153 [BPF_REG_7] = {STACK_OFFSET(56), STACK_OFFSET(60)},
154 [BPF_REG_8] = {STACK_OFFSET(64), STACK_OFFSET(68)},
155 [BPF_REG_9] = {STACK_OFFSET(72), STACK_OFFSET(76)},
156
157
158 [BPF_REG_FP] = {STACK_OFFSET(80), STACK_OFFSET(84)},
159
160
161 [BPF_REG_AX] = {IA32_ESI, IA32_EDI},
162
163
164 [TCALL_CNT] = {STACK_OFFSET(88), STACK_OFFSET(92)},
165};
166
167#define dst_lo dst[0]
168#define dst_hi dst[1]
169#define src_lo src[0]
170#define src_hi src[1]
171
172#define STACK_ALIGNMENT 8
173
174
175
176
177
178#define SCRATCH_SIZE 96
179
180
181#define _STACK_SIZE (stack_depth + SCRATCH_SIZE)
182
183#define STACK_SIZE ALIGN(_STACK_SIZE, STACK_ALIGNMENT)
184
185
186#define STACK_VAR(off) (off)
187
188
189static u8 add_1reg(u8 byte, u32 dst_reg)
190{
191 return byte + dst_reg;
192}
193
194
195static u8 add_2reg(u8 byte, u32 dst_reg, u32 src_reg)
196{
197 return byte + dst_reg + (src_reg << 3);
198}
199
200static void jit_fill_hole(void *area, unsigned int size)
201{
202
203 memset(area, 0xcc, size);
204}
205
206static inline void emit_ia32_mov_i(const u8 dst, const u32 val, bool dstk,
207 u8 **pprog)
208{
209 u8 *prog = *pprog;
210 int cnt = 0;
211
212 if (dstk) {
213 if (val == 0) {
214
215 EMIT2(0x33, add_2reg(0xC0, IA32_EAX, IA32_EAX));
216
217 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EAX),
218 STACK_VAR(dst));
219 } else {
220 EMIT3_off32(0xC7, add_1reg(0x40, IA32_EBP),
221 STACK_VAR(dst), val);
222 }
223 } else {
224 if (val == 0)
225 EMIT2(0x33, add_2reg(0xC0, dst, dst));
226 else
227 EMIT2_off32(0xC7, add_1reg(0xC0, dst),
228 val);
229 }
230 *pprog = prog;
231}
232
233
234static inline void emit_ia32_mov_r(const u8 dst, const u8 src, bool dstk,
235 bool sstk, u8 **pprog)
236{
237 u8 *prog = *pprog;
238 int cnt = 0;
239 u8 sreg = sstk ? IA32_EAX : src;
240
241 if (sstk)
242
243 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX), STACK_VAR(src));
244 if (dstk)
245
246 EMIT3(0x89, add_2reg(0x40, IA32_EBP, sreg), STACK_VAR(dst));
247 else
248
249 EMIT2(0x89, add_2reg(0xC0, dst, sreg));
250
251 *pprog = prog;
252}
253
254
255static inline void emit_ia32_mov_r64(const bool is64, const u8 dst[],
256 const u8 src[], bool dstk,
257 bool sstk, u8 **pprog,
258 const struct bpf_prog_aux *aux)
259{
260 emit_ia32_mov_r(dst_lo, src_lo, dstk, sstk, pprog);
261 if (is64)
262
263 emit_ia32_mov_r(dst_hi, src_hi, dstk, sstk, pprog);
264 else if (!aux->verifier_zext)
265
266 emit_ia32_mov_i(dst_hi, 0, dstk, pprog);
267}
268
269
270static inline void emit_ia32_mov_i64(const bool is64, const u8 dst[],
271 const u32 val, bool dstk, u8 **pprog)
272{
273 u32 hi = 0;
274
275 if (is64 && (val & (1<<31)))
276 hi = (u32)~0;
277 emit_ia32_mov_i(dst_lo, val, dstk, pprog);
278 emit_ia32_mov_i(dst_hi, hi, dstk, pprog);
279}
280
281
282
283
284
285static inline void emit_ia32_mul_r(const u8 dst, const u8 src, bool dstk,
286 bool sstk, u8 **pprog)
287{
288 u8 *prog = *pprog;
289 int cnt = 0;
290 u8 sreg = sstk ? IA32_ECX : src;
291
292 if (sstk)
293
294 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX), STACK_VAR(src));
295
296 if (dstk)
297
298 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX), STACK_VAR(dst));
299 else
300
301 EMIT2(0x8B, add_2reg(0xC0, dst, IA32_EAX));
302
303
304 EMIT2(0xF7, add_1reg(0xE0, sreg));
305
306 if (dstk)
307
308 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EAX),
309 STACK_VAR(dst));
310 else
311
312 EMIT2(0x89, add_2reg(0xC0, dst, IA32_EAX));
313
314 *pprog = prog;
315}
316
317static inline void emit_ia32_to_le_r64(const u8 dst[], s32 val,
318 bool dstk, u8 **pprog,
319 const struct bpf_prog_aux *aux)
320{
321 u8 *prog = *pprog;
322 int cnt = 0;
323 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
324 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
325
326 if (dstk && val != 64) {
327 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
328 STACK_VAR(dst_lo));
329 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
330 STACK_VAR(dst_hi));
331 }
332 switch (val) {
333 case 16:
334
335
336
337
338 EMIT2(0x0F, 0xB7);
339 EMIT1(add_2reg(0xC0, dreg_lo, dreg_lo));
340 if (!aux->verifier_zext)
341
342 EMIT2(0x33, add_2reg(0xC0, dreg_hi, dreg_hi));
343 break;
344 case 32:
345 if (!aux->verifier_zext)
346
347 EMIT2(0x33, add_2reg(0xC0, dreg_hi, dreg_hi));
348 break;
349 case 64:
350
351 break;
352 }
353
354 if (dstk && val != 64) {
355
356 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_lo),
357 STACK_VAR(dst_lo));
358
359 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_hi),
360 STACK_VAR(dst_hi));
361 }
362 *pprog = prog;
363}
364
365static inline void emit_ia32_to_be_r64(const u8 dst[], s32 val,
366 bool dstk, u8 **pprog,
367 const struct bpf_prog_aux *aux)
368{
369 u8 *prog = *pprog;
370 int cnt = 0;
371 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
372 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
373
374 if (dstk) {
375 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
376 STACK_VAR(dst_lo));
377 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
378 STACK_VAR(dst_hi));
379 }
380 switch (val) {
381 case 16:
382
383 EMIT1(0x66);
384 EMIT3(0xC1, add_1reg(0xC8, dreg_lo), 8);
385
386 EMIT2(0x0F, 0xB7);
387 EMIT1(add_2reg(0xC0, dreg_lo, dreg_lo));
388
389 if (!aux->verifier_zext)
390
391 EMIT2(0x33, add_2reg(0xC0, dreg_hi, dreg_hi));
392 break;
393 case 32:
394
395 EMIT1(0x0F);
396 EMIT1(add_1reg(0xC8, dreg_lo));
397
398 if (!aux->verifier_zext)
399
400 EMIT2(0x33, add_2reg(0xC0, dreg_hi, dreg_hi));
401 break;
402 case 64:
403
404 EMIT1(0x0F);
405 EMIT1(add_1reg(0xC8, dreg_lo));
406
407
408 EMIT1(0x0F);
409 EMIT1(add_1reg(0xC8, dreg_hi));
410
411
412 EMIT2(0x89, add_2reg(0xC0, IA32_ECX, dreg_hi));
413
414 EMIT2(0x89, add_2reg(0xC0, dreg_hi, dreg_lo));
415
416 EMIT2(0x89, add_2reg(0xC0, dreg_lo, IA32_ECX));
417
418 break;
419 }
420 if (dstk) {
421
422 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_lo),
423 STACK_VAR(dst_lo));
424
425 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_hi),
426 STACK_VAR(dst_hi));
427 }
428 *pprog = prog;
429}
430
431
432
433
434
435static inline void emit_ia32_div_mod_r(const u8 op, const u8 dst, const u8 src,
436 bool dstk, bool sstk, u8 **pprog)
437{
438 u8 *prog = *pprog;
439 int cnt = 0;
440
441 if (sstk)
442
443 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX),
444 STACK_VAR(src));
445 else if (src != IA32_ECX)
446
447 EMIT2(0x8B, add_2reg(0xC0, src, IA32_ECX));
448
449 if (dstk)
450
451 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
452 STACK_VAR(dst));
453 else
454
455 EMIT2(0x8B, add_2reg(0xC0, dst, IA32_EAX));
456
457
458 EMIT2(0x31, add_2reg(0xC0, IA32_EDX, IA32_EDX));
459
460 EMIT2(0xF7, add_1reg(0xF0, IA32_ECX));
461
462 if (op == BPF_MOD) {
463 if (dstk)
464 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EDX),
465 STACK_VAR(dst));
466 else
467 EMIT2(0x89, add_2reg(0xC0, dst, IA32_EDX));
468 } else {
469 if (dstk)
470 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EAX),
471 STACK_VAR(dst));
472 else
473 EMIT2(0x89, add_2reg(0xC0, dst, IA32_EAX));
474 }
475 *pprog = prog;
476}
477
478
479
480
481
482static inline void emit_ia32_shift_r(const u8 op, const u8 dst, const u8 src,
483 bool dstk, bool sstk, u8 **pprog)
484{
485 u8 *prog = *pprog;
486 int cnt = 0;
487 u8 dreg = dstk ? IA32_EAX : dst;
488 u8 b2;
489
490 if (dstk)
491
492 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX), STACK_VAR(dst));
493
494 if (sstk)
495
496 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX), STACK_VAR(src));
497 else if (src != IA32_ECX)
498
499 EMIT2(0x8B, add_2reg(0xC0, src, IA32_ECX));
500
501 switch (op) {
502 case BPF_LSH:
503 b2 = 0xE0; break;
504 case BPF_RSH:
505 b2 = 0xE8; break;
506 case BPF_ARSH:
507 b2 = 0xF8; break;
508 default:
509 return;
510 }
511 EMIT2(0xD3, add_1reg(b2, dreg));
512
513 if (dstk)
514
515 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg), STACK_VAR(dst));
516 *pprog = prog;
517}
518
519
520
521
522
523static inline void emit_ia32_alu_r(const bool is64, const bool hi, const u8 op,
524 const u8 dst, const u8 src, bool dstk,
525 bool sstk, u8 **pprog)
526{
527 u8 *prog = *pprog;
528 int cnt = 0;
529 u8 sreg = sstk ? IA32_EAX : src;
530 u8 dreg = dstk ? IA32_EDX : dst;
531
532 if (sstk)
533
534 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX), STACK_VAR(src));
535
536 if (dstk)
537
538 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX), STACK_VAR(dst));
539
540 switch (BPF_OP(op)) {
541
542 case BPF_ADD:
543 if (hi && is64)
544 EMIT2(0x11, add_2reg(0xC0, dreg, sreg));
545 else
546 EMIT2(0x01, add_2reg(0xC0, dreg, sreg));
547 break;
548
549 case BPF_SUB:
550 if (hi && is64)
551 EMIT2(0x19, add_2reg(0xC0, dreg, sreg));
552 else
553 EMIT2(0x29, add_2reg(0xC0, dreg, sreg));
554 break;
555
556 case BPF_OR:
557 EMIT2(0x09, add_2reg(0xC0, dreg, sreg));
558 break;
559
560 case BPF_AND:
561 EMIT2(0x21, add_2reg(0xC0, dreg, sreg));
562 break;
563
564 case BPF_XOR:
565 EMIT2(0x31, add_2reg(0xC0, dreg, sreg));
566 break;
567 }
568
569 if (dstk)
570
571 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg),
572 STACK_VAR(dst));
573 *pprog = prog;
574}
575
576
577static inline void emit_ia32_alu_r64(const bool is64, const u8 op,
578 const u8 dst[], const u8 src[],
579 bool dstk, bool sstk,
580 u8 **pprog, const struct bpf_prog_aux *aux)
581{
582 u8 *prog = *pprog;
583
584 emit_ia32_alu_r(is64, false, op, dst_lo, src_lo, dstk, sstk, &prog);
585 if (is64)
586 emit_ia32_alu_r(is64, true, op, dst_hi, src_hi, dstk, sstk,
587 &prog);
588 else if (!aux->verifier_zext)
589 emit_ia32_mov_i(dst_hi, 0, dstk, &prog);
590 *pprog = prog;
591}
592
593
594
595
596
597static inline void emit_ia32_alu_i(const bool is64, const bool hi, const u8 op,
598 const u8 dst, const s32 val, bool dstk,
599 u8 **pprog)
600{
601 u8 *prog = *pprog;
602 int cnt = 0;
603 u8 dreg = dstk ? IA32_EAX : dst;
604 u8 sreg = IA32_EDX;
605
606 if (dstk)
607
608 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX), STACK_VAR(dst));
609
610 if (!is_imm8(val))
611
612 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_EDX), val);
613
614 switch (op) {
615
616 case BPF_ADD:
617 if (hi && is64) {
618 if (is_imm8(val))
619 EMIT3(0x83, add_1reg(0xD0, dreg), val);
620 else
621 EMIT2(0x11, add_2reg(0xC0, dreg, sreg));
622 } else {
623 if (is_imm8(val))
624 EMIT3(0x83, add_1reg(0xC0, dreg), val);
625 else
626 EMIT2(0x01, add_2reg(0xC0, dreg, sreg));
627 }
628 break;
629
630 case BPF_SUB:
631 if (hi && is64) {
632 if (is_imm8(val))
633 EMIT3(0x83, add_1reg(0xD8, dreg), val);
634 else
635 EMIT2(0x19, add_2reg(0xC0, dreg, sreg));
636 } else {
637 if (is_imm8(val))
638 EMIT3(0x83, add_1reg(0xE8, dreg), val);
639 else
640 EMIT2(0x29, add_2reg(0xC0, dreg, sreg));
641 }
642 break;
643
644 case BPF_OR:
645 if (is_imm8(val))
646 EMIT3(0x83, add_1reg(0xC8, dreg), val);
647 else
648 EMIT2(0x09, add_2reg(0xC0, dreg, sreg));
649 break;
650
651 case BPF_AND:
652 if (is_imm8(val))
653 EMIT3(0x83, add_1reg(0xE0, dreg), val);
654 else
655 EMIT2(0x21, add_2reg(0xC0, dreg, sreg));
656 break;
657
658 case BPF_XOR:
659 if (is_imm8(val))
660 EMIT3(0x83, add_1reg(0xF0, dreg), val);
661 else
662 EMIT2(0x31, add_2reg(0xC0, dreg, sreg));
663 break;
664 case BPF_NEG:
665 EMIT2(0xF7, add_1reg(0xD8, dreg));
666 break;
667 }
668
669 if (dstk)
670
671 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg),
672 STACK_VAR(dst));
673 *pprog = prog;
674}
675
676
677static inline void emit_ia32_alu_i64(const bool is64, const u8 op,
678 const u8 dst[], const u32 val,
679 bool dstk, u8 **pprog,
680 const struct bpf_prog_aux *aux)
681{
682 u8 *prog = *pprog;
683 u32 hi = 0;
684
685 if (is64 && (val & (1<<31)))
686 hi = (u32)~0;
687
688 emit_ia32_alu_i(is64, false, op, dst_lo, val, dstk, &prog);
689 if (is64)
690 emit_ia32_alu_i(is64, true, op, dst_hi, hi, dstk, &prog);
691 else if (!aux->verifier_zext)
692 emit_ia32_mov_i(dst_hi, 0, dstk, &prog);
693
694 *pprog = prog;
695}
696
697
698static inline void emit_ia32_neg64(const u8 dst[], bool dstk, u8 **pprog)
699{
700 u8 *prog = *pprog;
701 int cnt = 0;
702 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
703 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
704
705 if (dstk) {
706 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
707 STACK_VAR(dst_lo));
708 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
709 STACK_VAR(dst_hi));
710 }
711
712
713 EMIT2(0xF7, add_1reg(0xD8, dreg_lo));
714
715 EMIT3(0x83, add_1reg(0xD0, dreg_hi), 0x00);
716
717 EMIT2(0xF7, add_1reg(0xD8, dreg_hi));
718
719 if (dstk) {
720
721 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_lo),
722 STACK_VAR(dst_lo));
723
724 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_hi),
725 STACK_VAR(dst_hi));
726 }
727 *pprog = prog;
728}
729
730
731static inline void emit_ia32_lsh_r64(const u8 dst[], const u8 src[],
732 bool dstk, bool sstk, u8 **pprog)
733{
734 u8 *prog = *pprog;
735 int cnt = 0;
736 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
737 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
738
739 if (dstk) {
740 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
741 STACK_VAR(dst_lo));
742 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
743 STACK_VAR(dst_hi));
744 }
745
746 if (sstk)
747
748 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX),
749 STACK_VAR(src_lo));
750 else
751
752 EMIT2(0x8B, add_2reg(0xC0, src_lo, IA32_ECX));
753
754
755 EMIT3(0x0F, 0xA5, add_2reg(0xC0, dreg_hi, dreg_lo));
756
757 EMIT2(0xD3, add_1reg(0xE0, dreg_lo));
758
759
760
761
762 EMIT3(0x83, add_1reg(0xF8, IA32_ECX), 32);
763
764 EMIT2(IA32_JB, 4);
765
766
767 EMIT2(0x89, add_2reg(0xC0, dreg_hi, dreg_lo));
768
769 EMIT2(0x33, add_2reg(0xC0, dreg_lo, dreg_lo));
770
771 if (dstk) {
772
773 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_lo),
774 STACK_VAR(dst_lo));
775
776 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_hi),
777 STACK_VAR(dst_hi));
778 }
779
780 *pprog = prog;
781}
782
783
784static inline void emit_ia32_arsh_r64(const u8 dst[], const u8 src[],
785 bool dstk, bool sstk, u8 **pprog)
786{
787 u8 *prog = *pprog;
788 int cnt = 0;
789 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
790 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
791
792 if (dstk) {
793 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
794 STACK_VAR(dst_lo));
795 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
796 STACK_VAR(dst_hi));
797 }
798
799 if (sstk)
800
801 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX),
802 STACK_VAR(src_lo));
803 else
804
805 EMIT2(0x8B, add_2reg(0xC0, src_lo, IA32_ECX));
806
807
808 EMIT3(0x0F, 0xAD, add_2reg(0xC0, dreg_lo, dreg_hi));
809
810 EMIT2(0xD3, add_1reg(0xF8, dreg_hi));
811
812
813
814
815 EMIT3(0x83, add_1reg(0xF8, IA32_ECX), 32);
816
817 EMIT2(IA32_JB, 5);
818
819
820 EMIT2(0x89, add_2reg(0xC0, dreg_lo, dreg_hi));
821
822 EMIT3(0xC1, add_1reg(0xF8, dreg_hi), 31);
823
824 if (dstk) {
825
826 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_lo),
827 STACK_VAR(dst_lo));
828
829 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_hi),
830 STACK_VAR(dst_hi));
831 }
832
833 *pprog = prog;
834}
835
836
837static inline void emit_ia32_rsh_r64(const u8 dst[], const u8 src[], bool dstk,
838 bool sstk, u8 **pprog)
839{
840 u8 *prog = *pprog;
841 int cnt = 0;
842 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
843 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
844
845 if (dstk) {
846 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
847 STACK_VAR(dst_lo));
848 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
849 STACK_VAR(dst_hi));
850 }
851
852 if (sstk)
853
854 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX),
855 STACK_VAR(src_lo));
856 else
857
858 EMIT2(0x8B, add_2reg(0xC0, src_lo, IA32_ECX));
859
860
861 EMIT3(0x0F, 0xAD, add_2reg(0xC0, dreg_lo, dreg_hi));
862
863 EMIT2(0xD3, add_1reg(0xE8, dreg_hi));
864
865
866
867
868 EMIT3(0x83, add_1reg(0xF8, IA32_ECX), 32);
869
870 EMIT2(IA32_JB, 4);
871
872
873 EMIT2(0x89, add_2reg(0xC0, dreg_lo, dreg_hi));
874
875 EMIT2(0x33, add_2reg(0xC0, dreg_hi, dreg_hi));
876
877 if (dstk) {
878
879 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_lo),
880 STACK_VAR(dst_lo));
881
882 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_hi),
883 STACK_VAR(dst_hi));
884 }
885
886 *pprog = prog;
887}
888
889
890static inline void emit_ia32_lsh_i64(const u8 dst[], const u32 val,
891 bool dstk, u8 **pprog)
892{
893 u8 *prog = *pprog;
894 int cnt = 0;
895 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
896 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
897
898 if (dstk) {
899 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
900 STACK_VAR(dst_lo));
901 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
902 STACK_VAR(dst_hi));
903 }
904
905 if (val < 32) {
906
907 EMIT4(0x0F, 0xA4, add_2reg(0xC0, dreg_hi, dreg_lo), val);
908
909 EMIT3(0xC1, add_1reg(0xE0, dreg_lo), val);
910 } else if (val >= 32 && val < 64) {
911 u32 value = val - 32;
912
913
914 EMIT3(0xC1, add_1reg(0xE0, dreg_lo), value);
915
916 EMIT2(0x89, add_2reg(0xC0, dreg_hi, dreg_lo));
917
918 EMIT2(0x33, add_2reg(0xC0, dreg_lo, dreg_lo));
919 } else {
920
921 EMIT2(0x33, add_2reg(0xC0, dreg_lo, dreg_lo));
922
923 EMIT2(0x33, add_2reg(0xC0, dreg_hi, dreg_hi));
924 }
925
926 if (dstk) {
927
928 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_lo),
929 STACK_VAR(dst_lo));
930
931 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_hi),
932 STACK_VAR(dst_hi));
933 }
934 *pprog = prog;
935}
936
937
938static inline void emit_ia32_rsh_i64(const u8 dst[], const u32 val,
939 bool dstk, u8 **pprog)
940{
941 u8 *prog = *pprog;
942 int cnt = 0;
943 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
944 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
945
946 if (dstk) {
947 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
948 STACK_VAR(dst_lo));
949 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
950 STACK_VAR(dst_hi));
951 }
952
953
954 if (val < 32) {
955
956 EMIT4(0x0F, 0xAC, add_2reg(0xC0, dreg_lo, dreg_hi), val);
957
958 EMIT3(0xC1, add_1reg(0xE8, dreg_hi), val);
959 } else if (val >= 32 && val < 64) {
960 u32 value = val - 32;
961
962
963 EMIT3(0xC1, add_1reg(0xE8, dreg_hi), value);
964
965 EMIT2(0x89, add_2reg(0xC0, dreg_lo, dreg_hi));
966
967 EMIT2(0x33, add_2reg(0xC0, dreg_hi, dreg_hi));
968 } else {
969
970 EMIT2(0x33, add_2reg(0xC0, dreg_lo, dreg_lo));
971
972 EMIT2(0x33, add_2reg(0xC0, dreg_hi, dreg_hi));
973 }
974
975 if (dstk) {
976
977 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_lo),
978 STACK_VAR(dst_lo));
979
980 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_hi),
981 STACK_VAR(dst_hi));
982 }
983 *pprog = prog;
984}
985
986
987static inline void emit_ia32_arsh_i64(const u8 dst[], const u32 val,
988 bool dstk, u8 **pprog)
989{
990 u8 *prog = *pprog;
991 int cnt = 0;
992 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
993 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
994
995 if (dstk) {
996 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
997 STACK_VAR(dst_lo));
998 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
999 STACK_VAR(dst_hi));
1000 }
1001
1002 if (val < 32) {
1003
1004 EMIT4(0x0F, 0xAC, add_2reg(0xC0, dreg_lo, dreg_hi), val);
1005
1006 EMIT3(0xC1, add_1reg(0xF8, dreg_hi), val);
1007 } else if (val >= 32 && val < 64) {
1008 u32 value = val - 32;
1009
1010
1011 EMIT3(0xC1, add_1reg(0xF8, dreg_hi), value);
1012
1013 EMIT2(0x89, add_2reg(0xC0, dreg_lo, dreg_hi));
1014
1015
1016 EMIT3(0xC1, add_1reg(0xF8, dreg_hi), 31);
1017 } else {
1018
1019 EMIT3(0xC1, add_1reg(0xF8, dreg_hi), 31);
1020
1021 EMIT2(0x89, add_2reg(0xC0, dreg_lo, dreg_hi));
1022 }
1023
1024 if (dstk) {
1025
1026 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_lo),
1027 STACK_VAR(dst_lo));
1028
1029 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_hi),
1030 STACK_VAR(dst_hi));
1031 }
1032 *pprog = prog;
1033}
1034
1035static inline void emit_ia32_mul_r64(const u8 dst[], const u8 src[], bool dstk,
1036 bool sstk, u8 **pprog)
1037{
1038 u8 *prog = *pprog;
1039 int cnt = 0;
1040
1041 if (dstk)
1042
1043 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
1044 STACK_VAR(dst_hi));
1045 else
1046
1047 EMIT2(0x8B, add_2reg(0xC0, dst_hi, IA32_EAX));
1048
1049 if (sstk)
1050
1051 EMIT3(0xF7, add_1reg(0x60, IA32_EBP), STACK_VAR(src_lo));
1052 else
1053
1054 EMIT2(0xF7, add_1reg(0xE0, src_lo));
1055
1056
1057 EMIT2(0x89, add_2reg(0xC0, IA32_ECX, IA32_EAX));
1058
1059 if (dstk)
1060
1061 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
1062 STACK_VAR(dst_lo));
1063 else
1064
1065 EMIT2(0x8B, add_2reg(0xC0, dst_lo, IA32_EAX));
1066
1067 if (sstk)
1068
1069 EMIT3(0xF7, add_1reg(0x60, IA32_EBP), STACK_VAR(src_hi));
1070 else
1071
1072 EMIT2(0xF7, add_1reg(0xE0, src_hi));
1073
1074
1075 EMIT2(0x01, add_2reg(0xC0, IA32_ECX, IA32_EAX));
1076
1077 if (dstk)
1078
1079 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
1080 STACK_VAR(dst_lo));
1081 else
1082
1083 EMIT2(0x8B, add_2reg(0xC0, dst_lo, IA32_EAX));
1084
1085 if (sstk)
1086
1087 EMIT3(0xF7, add_1reg(0x60, IA32_EBP), STACK_VAR(src_lo));
1088 else
1089
1090 EMIT2(0xF7, add_1reg(0xE0, src_lo));
1091
1092
1093 EMIT2(0x01, add_2reg(0xC0, IA32_ECX, IA32_EDX));
1094
1095 if (dstk) {
1096
1097 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EAX),
1098 STACK_VAR(dst_lo));
1099
1100 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_ECX),
1101 STACK_VAR(dst_hi));
1102 } else {
1103
1104 EMIT2(0x89, add_2reg(0xC0, dst_lo, IA32_EAX));
1105
1106 EMIT2(0x89, add_2reg(0xC0, dst_hi, IA32_ECX));
1107 }
1108
1109 *pprog = prog;
1110}
1111
1112static inline void emit_ia32_mul_i64(const u8 dst[], const u32 val,
1113 bool dstk, u8 **pprog)
1114{
1115 u8 *prog = *pprog;
1116 int cnt = 0;
1117 u32 hi;
1118
1119 hi = val & (1<<31) ? (u32)~0 : 0;
1120
1121 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_EAX), val);
1122 if (dstk)
1123
1124 EMIT3(0xF7, add_1reg(0x60, IA32_EBP), STACK_VAR(dst_hi));
1125 else
1126
1127 EMIT2(0xF7, add_1reg(0xE0, dst_hi));
1128
1129
1130 EMIT2(0x89, add_2reg(0xC0, IA32_ECX, IA32_EAX));
1131
1132
1133 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_EAX), hi);
1134 if (dstk)
1135
1136 EMIT3(0xF7, add_1reg(0x60, IA32_EBP), STACK_VAR(dst_lo));
1137 else
1138
1139 EMIT2(0xF7, add_1reg(0xE0, dst_lo));
1140
1141 EMIT2(0x01, add_2reg(0xC0, IA32_ECX, IA32_EAX));
1142
1143
1144 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_EAX), val);
1145 if (dstk)
1146
1147 EMIT3(0xF7, add_1reg(0x60, IA32_EBP), STACK_VAR(dst_lo));
1148 else
1149
1150 EMIT2(0xF7, add_1reg(0xE0, dst_lo));
1151
1152
1153 EMIT2(0x01, add_2reg(0xC0, IA32_ECX, IA32_EDX));
1154
1155 if (dstk) {
1156
1157 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EAX),
1158 STACK_VAR(dst_lo));
1159
1160 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_ECX),
1161 STACK_VAR(dst_hi));
1162 } else {
1163
1164 EMIT2(0x89, add_2reg(0xC0, dst_lo, IA32_EAX));
1165
1166 EMIT2(0x89, add_2reg(0xC0, dst_hi, IA32_ECX));
1167 }
1168
1169 *pprog = prog;
1170}
1171
1172static int bpf_size_to_x86_bytes(int bpf_size)
1173{
1174 if (bpf_size == BPF_W)
1175 return 4;
1176 else if (bpf_size == BPF_H)
1177 return 2;
1178 else if (bpf_size == BPF_B)
1179 return 1;
1180 else if (bpf_size == BPF_DW)
1181 return 4;
1182 else
1183 return 0;
1184}
1185
1186struct jit_context {
1187 int cleanup_addr;
1188};
1189
1190
1191#define BPF_MAX_INSN_SIZE 128
1192#define BPF_INSN_SAFETY 64
1193
1194#define PROLOGUE_SIZE 35
1195
1196
1197
1198
1199
1200static void emit_prologue(u8 **pprog, u32 stack_depth)
1201{
1202 u8 *prog = *pprog;
1203 int cnt = 0;
1204 const u8 *r1 = bpf2ia32[BPF_REG_1];
1205 const u8 fplo = bpf2ia32[BPF_REG_FP][0];
1206 const u8 fphi = bpf2ia32[BPF_REG_FP][1];
1207 const u8 *tcc = bpf2ia32[TCALL_CNT];
1208
1209
1210 EMIT1(0x55);
1211
1212 EMIT2(0x89, 0xE5);
1213
1214 EMIT1(0x57);
1215
1216 EMIT1(0x56);
1217
1218 EMIT1(0x53);
1219
1220
1221 EMIT2_off32(0x81, 0xEC, STACK_SIZE);
1222
1223 EMIT3(0x83, add_1reg(0xE8, IA32_EBP), SCRATCH_SIZE + 12);
1224
1225 EMIT2(0x31, add_2reg(0xC0, IA32_EBX, IA32_EBX));
1226
1227
1228 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EBP), STACK_VAR(fplo));
1229 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EBX), STACK_VAR(fphi));
1230
1231
1232
1233 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EAX), STACK_VAR(r1[0]));
1234 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EBX), STACK_VAR(r1[1]));
1235
1236
1237 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EBX), STACK_VAR(tcc[0]));
1238 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EBX), STACK_VAR(tcc[1]));
1239
1240 BUILD_BUG_ON(cnt != PROLOGUE_SIZE);
1241 *pprog = prog;
1242}
1243
1244
1245static void emit_epilogue(u8 **pprog, u32 stack_depth)
1246{
1247 u8 *prog = *pprog;
1248 const u8 *r0 = bpf2ia32[BPF_REG_0];
1249 int cnt = 0;
1250
1251
1252 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX), STACK_VAR(r0[0]));
1253
1254 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX), STACK_VAR(r0[1]));
1255
1256
1257 EMIT3(0x83, add_1reg(0xC0, IA32_EBP), SCRATCH_SIZE + 12);
1258
1259
1260 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EBX), -12);
1261
1262 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ESI), -8);
1263
1264 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDI), -4);
1265
1266 EMIT1(0xC9);
1267 EMIT1(0xC3);
1268 *pprog = prog;
1269}
1270
1271static int emit_jmp_edx(u8 **pprog, u8 *ip)
1272{
1273 u8 *prog = *pprog;
1274 int cnt = 0;
1275
1276#ifdef CONFIG_RETPOLINE
1277 EMIT1_off32(0xE9, (u8 *)__x86_indirect_thunk_edx - (ip + 5));
1278#else
1279 EMIT2(0xFF, 0xE2);
1280#endif
1281 *pprog = prog;
1282
1283 return cnt;
1284}
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299static void emit_bpf_tail_call(u8 **pprog, u8 *ip)
1300{
1301 u8 *prog = *pprog;
1302 int cnt = 0;
1303 const u8 *r1 = bpf2ia32[BPF_REG_1];
1304 const u8 *r2 = bpf2ia32[BPF_REG_2];
1305 const u8 *r3 = bpf2ia32[BPF_REG_3];
1306 const u8 *tcc = bpf2ia32[TCALL_CNT];
1307 u32 lo, hi;
1308 static int jmp_label1 = -1;
1309
1310
1311
1312
1313
1314
1315 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX), STACK_VAR(r2[0]));
1316
1317 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX), STACK_VAR(r3[0]));
1318
1319
1320 EMIT3(0x39, add_2reg(0x40, IA32_EAX, IA32_EDX),
1321 offsetof(struct bpf_array, map.max_entries));
1322
1323 EMIT2(IA32_JBE, jmp_label(jmp_label1, 2));
1324
1325
1326
1327
1328
1329 lo = (u32)MAX_TAIL_CALL_CNT;
1330 hi = (u32)((u64)MAX_TAIL_CALL_CNT >> 32);
1331 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX), STACK_VAR(tcc[0]));
1332 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EBX), STACK_VAR(tcc[1]));
1333
1334
1335 EMIT3(0x83, add_1reg(0xF8, IA32_EBX), hi);
1336 EMIT2(IA32_JNE, 3);
1337
1338 EMIT3(0x83, add_1reg(0xF8, IA32_ECX), lo);
1339
1340
1341 EMIT2(IA32_JAE, jmp_label(jmp_label1, 2));
1342
1343
1344 EMIT3(0x83, add_1reg(0xC0, IA32_ECX), 0x01);
1345
1346 EMIT3(0x83, add_1reg(0xD0, IA32_EBX), 0x00);
1347
1348
1349 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_ECX), STACK_VAR(tcc[0]));
1350
1351 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EBX), STACK_VAR(tcc[1]));
1352
1353
1354
1355 EMIT3_off32(0x8B, 0x94, 0x90, offsetof(struct bpf_array, ptrs));
1356
1357
1358
1359
1360
1361
1362 EMIT2(0x85, add_2reg(0xC0, IA32_EDX, IA32_EDX));
1363
1364 EMIT2(IA32_JE, jmp_label(jmp_label1, 2));
1365
1366
1367
1368 EMIT3(0x8B, add_2reg(0x40, IA32_EDX, IA32_EDX),
1369 offsetof(struct bpf_prog, bpf_func));
1370
1371 EMIT3(0x83, add_1reg(0xC0, IA32_EDX), PROLOGUE_SIZE);
1372
1373
1374 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX), STACK_VAR(r1[0]));
1375
1376
1377
1378
1379
1380
1381 cnt += emit_jmp_edx(&prog, ip + cnt);
1382
1383 if (jmp_label1 == -1)
1384 jmp_label1 = cnt;
1385
1386
1387 *pprog = prog;
1388}
1389
1390
1391static inline void emit_push_r64(const u8 src[], u8 **pprog)
1392{
1393 u8 *prog = *pprog;
1394 int cnt = 0;
1395
1396
1397 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX), STACK_VAR(src_hi));
1398
1399 EMIT1(0x51);
1400
1401
1402 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX), STACK_VAR(src_lo));
1403
1404 EMIT1(0x51);
1405
1406 *pprog = prog;
1407}
1408
1409static void emit_push_r32(const u8 src[], u8 **pprog)
1410{
1411 u8 *prog = *pprog;
1412 int cnt = 0;
1413
1414
1415 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX), STACK_VAR(src_lo));
1416
1417 EMIT1(0x51);
1418
1419 *pprog = prog;
1420}
1421
1422static u8 get_cond_jmp_opcode(const u8 op, bool is_cmp_lo)
1423{
1424 u8 jmp_cond;
1425
1426
1427 switch (op) {
1428 case BPF_JEQ:
1429 jmp_cond = IA32_JE;
1430 break;
1431 case BPF_JSET:
1432 case BPF_JNE:
1433 jmp_cond = IA32_JNE;
1434 break;
1435 case BPF_JGT:
1436
1437 jmp_cond = IA32_JA;
1438 break;
1439 case BPF_JLT:
1440
1441 jmp_cond = IA32_JB;
1442 break;
1443 case BPF_JGE:
1444
1445 jmp_cond = IA32_JAE;
1446 break;
1447 case BPF_JLE:
1448
1449 jmp_cond = IA32_JBE;
1450 break;
1451 case BPF_JSGT:
1452 if (!is_cmp_lo)
1453
1454 jmp_cond = IA32_JG;
1455 else
1456
1457 jmp_cond = IA32_JA;
1458 break;
1459 case BPF_JSLT:
1460 if (!is_cmp_lo)
1461
1462 jmp_cond = IA32_JL;
1463 else
1464
1465 jmp_cond = IA32_JB;
1466 break;
1467 case BPF_JSGE:
1468 if (!is_cmp_lo)
1469
1470 jmp_cond = IA32_JGE;
1471 else
1472
1473 jmp_cond = IA32_JAE;
1474 break;
1475 case BPF_JSLE:
1476 if (!is_cmp_lo)
1477
1478 jmp_cond = IA32_JLE;
1479 else
1480
1481 jmp_cond = IA32_JBE;
1482 break;
1483 default:
1484 jmp_cond = COND_JMP_OPCODE_INVALID;
1485 break;
1486 }
1487
1488 return jmp_cond;
1489}
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569static int emit_kfunc_call(const struct bpf_prog *bpf_prog, u8 *end_addr,
1570 const struct bpf_insn *insn, u8 **pprog)
1571{
1572 const u8 arg_regs[] = { IA32_EAX, IA32_EDX, IA32_ECX };
1573 int i, cnt = 0, first_stack_regno, last_stack_regno;
1574 int free_arg_regs = ARRAY_SIZE(arg_regs);
1575 const struct btf_func_model *fm;
1576 int bytes_in_stack = 0;
1577 const u8 *cur_arg_reg;
1578 u8 *prog = *pprog;
1579 s64 jmp_offset;
1580
1581 fm = bpf_jit_find_kfunc_model(bpf_prog, insn);
1582 if (!fm)
1583 return -EINVAL;
1584
1585 first_stack_regno = BPF_REG_1;
1586 for (i = 0; i < fm->nr_args; i++) {
1587 int regs_needed = fm->arg_size[i] > sizeof(u32) ? 2 : 1;
1588
1589 if (regs_needed > free_arg_regs)
1590 break;
1591
1592 free_arg_regs -= regs_needed;
1593 first_stack_regno++;
1594 }
1595
1596
1597 last_stack_regno = BPF_REG_0 + fm->nr_args;
1598 for (i = last_stack_regno; i >= first_stack_regno; i--) {
1599 if (fm->arg_size[i - 1] > sizeof(u32)) {
1600 emit_push_r64(bpf2ia32[i], &prog);
1601 bytes_in_stack += 8;
1602 } else {
1603 emit_push_r32(bpf2ia32[i], &prog);
1604 bytes_in_stack += 4;
1605 }
1606 }
1607
1608 cur_arg_reg = &arg_regs[0];
1609 for (i = BPF_REG_1; i < first_stack_regno; i++) {
1610
1611 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, *cur_arg_reg++),
1612 STACK_VAR(bpf2ia32[i][0]));
1613 if (fm->arg_size[i - 1] > sizeof(u32))
1614
1615 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, *cur_arg_reg++),
1616 STACK_VAR(bpf2ia32[i][1]));
1617 }
1618
1619 if (bytes_in_stack)
1620
1621 end_addr -= 3;
1622
1623
1624 if (fm->ret_size > sizeof(u32))
1625 end_addr -= 3;
1626
1627
1628 if (fm->ret_size)
1629 end_addr -= 3;
1630
1631 jmp_offset = (u8 *)__bpf_call_base + insn->imm - end_addr;
1632 if (!is_simm32(jmp_offset)) {
1633 pr_err("unsupported BPF kernel function jmp_offset:%lld\n",
1634 jmp_offset);
1635 return -EINVAL;
1636 }
1637
1638 EMIT1_off32(0xE8, jmp_offset);
1639
1640 if (fm->ret_size)
1641
1642 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EAX),
1643 STACK_VAR(bpf2ia32[BPF_REG_0][0]));
1644
1645 if (fm->ret_size > sizeof(u32))
1646
1647 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EDX),
1648 STACK_VAR(bpf2ia32[BPF_REG_0][1]));
1649
1650 if (bytes_in_stack)
1651
1652 EMIT3(0x83, add_1reg(0xC0, IA32_ESP), bytes_in_stack);
1653
1654 *pprog = prog;
1655
1656 return 0;
1657}
1658
1659static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
1660 int oldproglen, struct jit_context *ctx)
1661{
1662 struct bpf_insn *insn = bpf_prog->insnsi;
1663 int insn_cnt = bpf_prog->len;
1664 bool seen_exit = false;
1665 u8 temp[BPF_MAX_INSN_SIZE + BPF_INSN_SAFETY];
1666 int i, cnt = 0;
1667 int proglen = 0;
1668 u8 *prog = temp;
1669
1670 emit_prologue(&prog, bpf_prog->aux->stack_depth);
1671
1672 for (i = 0; i < insn_cnt; i++, insn++) {
1673 const s32 imm32 = insn->imm;
1674 const bool is64 = BPF_CLASS(insn->code) == BPF_ALU64;
1675 const bool dstk = insn->dst_reg != BPF_REG_AX;
1676 const bool sstk = insn->src_reg != BPF_REG_AX;
1677 const u8 code = insn->code;
1678 const u8 *dst = bpf2ia32[insn->dst_reg];
1679 const u8 *src = bpf2ia32[insn->src_reg];
1680 const u8 *r0 = bpf2ia32[BPF_REG_0];
1681 s64 jmp_offset;
1682 u8 jmp_cond;
1683 int ilen;
1684 u8 *func;
1685
1686 switch (code) {
1687
1688
1689 case BPF_ALU | BPF_MOV | BPF_K:
1690 case BPF_ALU | BPF_MOV | BPF_X:
1691 case BPF_ALU64 | BPF_MOV | BPF_K:
1692 case BPF_ALU64 | BPF_MOV | BPF_X:
1693 switch (BPF_SRC(code)) {
1694 case BPF_X:
1695 if (imm32 == 1) {
1696
1697 emit_ia32_mov_i(dst_hi, 0, dstk, &prog);
1698 break;
1699 }
1700 emit_ia32_mov_r64(is64, dst, src, dstk, sstk,
1701 &prog, bpf_prog->aux);
1702 break;
1703 case BPF_K:
1704
1705 emit_ia32_mov_i64(is64, dst, imm32,
1706 dstk, &prog);
1707 break;
1708 }
1709 break;
1710
1711
1712
1713
1714
1715
1716
1717
1718 case BPF_ALU | BPF_ADD | BPF_K:
1719 case BPF_ALU | BPF_ADD | BPF_X:
1720 case BPF_ALU | BPF_SUB | BPF_K:
1721 case BPF_ALU | BPF_SUB | BPF_X:
1722 case BPF_ALU | BPF_OR | BPF_K:
1723 case BPF_ALU | BPF_OR | BPF_X:
1724 case BPF_ALU | BPF_AND | BPF_K:
1725 case BPF_ALU | BPF_AND | BPF_X:
1726 case BPF_ALU | BPF_XOR | BPF_K:
1727 case BPF_ALU | BPF_XOR | BPF_X:
1728 case BPF_ALU64 | BPF_ADD | BPF_K:
1729 case BPF_ALU64 | BPF_ADD | BPF_X:
1730 case BPF_ALU64 | BPF_SUB | BPF_K:
1731 case BPF_ALU64 | BPF_SUB | BPF_X:
1732 case BPF_ALU64 | BPF_OR | BPF_K:
1733 case BPF_ALU64 | BPF_OR | BPF_X:
1734 case BPF_ALU64 | BPF_AND | BPF_K:
1735 case BPF_ALU64 | BPF_AND | BPF_X:
1736 case BPF_ALU64 | BPF_XOR | BPF_K:
1737 case BPF_ALU64 | BPF_XOR | BPF_X:
1738 switch (BPF_SRC(code)) {
1739 case BPF_X:
1740 emit_ia32_alu_r64(is64, BPF_OP(code), dst,
1741 src, dstk, sstk, &prog,
1742 bpf_prog->aux);
1743 break;
1744 case BPF_K:
1745 emit_ia32_alu_i64(is64, BPF_OP(code), dst,
1746 imm32, dstk, &prog,
1747 bpf_prog->aux);
1748 break;
1749 }
1750 break;
1751 case BPF_ALU | BPF_MUL | BPF_K:
1752 case BPF_ALU | BPF_MUL | BPF_X:
1753 switch (BPF_SRC(code)) {
1754 case BPF_X:
1755 emit_ia32_mul_r(dst_lo, src_lo, dstk,
1756 sstk, &prog);
1757 break;
1758 case BPF_K:
1759
1760 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_ECX),
1761 imm32);
1762 emit_ia32_mul_r(dst_lo, IA32_ECX, dstk,
1763 false, &prog);
1764 break;
1765 }
1766 if (!bpf_prog->aux->verifier_zext)
1767 emit_ia32_mov_i(dst_hi, 0, dstk, &prog);
1768 break;
1769 case BPF_ALU | BPF_LSH | BPF_X:
1770 case BPF_ALU | BPF_RSH | BPF_X:
1771 case BPF_ALU | BPF_ARSH | BPF_K:
1772 case BPF_ALU | BPF_ARSH | BPF_X:
1773 switch (BPF_SRC(code)) {
1774 case BPF_X:
1775 emit_ia32_shift_r(BPF_OP(code), dst_lo, src_lo,
1776 dstk, sstk, &prog);
1777 break;
1778 case BPF_K:
1779
1780 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_ECX),
1781 imm32);
1782 emit_ia32_shift_r(BPF_OP(code), dst_lo,
1783 IA32_ECX, dstk, false,
1784 &prog);
1785 break;
1786 }
1787 if (!bpf_prog->aux->verifier_zext)
1788 emit_ia32_mov_i(dst_hi, 0, dstk, &prog);
1789 break;
1790
1791
1792 case BPF_ALU | BPF_DIV | BPF_K:
1793 case BPF_ALU | BPF_DIV | BPF_X:
1794 case BPF_ALU | BPF_MOD | BPF_K:
1795 case BPF_ALU | BPF_MOD | BPF_X:
1796 switch (BPF_SRC(code)) {
1797 case BPF_X:
1798 emit_ia32_div_mod_r(BPF_OP(code), dst_lo,
1799 src_lo, dstk, sstk, &prog);
1800 break;
1801 case BPF_K:
1802
1803 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_ECX),
1804 imm32);
1805 emit_ia32_div_mod_r(BPF_OP(code), dst_lo,
1806 IA32_ECX, dstk, false,
1807 &prog);
1808 break;
1809 }
1810 if (!bpf_prog->aux->verifier_zext)
1811 emit_ia32_mov_i(dst_hi, 0, dstk, &prog);
1812 break;
1813 case BPF_ALU64 | BPF_DIV | BPF_K:
1814 case BPF_ALU64 | BPF_DIV | BPF_X:
1815 case BPF_ALU64 | BPF_MOD | BPF_K:
1816 case BPF_ALU64 | BPF_MOD | BPF_X:
1817 goto notyet;
1818
1819
1820 case BPF_ALU | BPF_RSH | BPF_K:
1821 case BPF_ALU | BPF_LSH | BPF_K:
1822 if (unlikely(imm32 > 31))
1823 return -EINVAL;
1824
1825 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_ECX), imm32);
1826 emit_ia32_shift_r(BPF_OP(code), dst_lo, IA32_ECX, dstk,
1827 false, &prog);
1828 if (!bpf_prog->aux->verifier_zext)
1829 emit_ia32_mov_i(dst_hi, 0, dstk, &prog);
1830 break;
1831
1832 case BPF_ALU64 | BPF_LSH | BPF_K:
1833 if (unlikely(imm32 > 63))
1834 return -EINVAL;
1835 emit_ia32_lsh_i64(dst, imm32, dstk, &prog);
1836 break;
1837
1838 case BPF_ALU64 | BPF_RSH | BPF_K:
1839 if (unlikely(imm32 > 63))
1840 return -EINVAL;
1841 emit_ia32_rsh_i64(dst, imm32, dstk, &prog);
1842 break;
1843
1844 case BPF_ALU64 | BPF_LSH | BPF_X:
1845 emit_ia32_lsh_r64(dst, src, dstk, sstk, &prog);
1846 break;
1847
1848 case BPF_ALU64 | BPF_RSH | BPF_X:
1849 emit_ia32_rsh_r64(dst, src, dstk, sstk, &prog);
1850 break;
1851
1852 case BPF_ALU64 | BPF_ARSH | BPF_X:
1853 emit_ia32_arsh_r64(dst, src, dstk, sstk, &prog);
1854 break;
1855
1856 case BPF_ALU64 | BPF_ARSH | BPF_K:
1857 if (unlikely(imm32 > 63))
1858 return -EINVAL;
1859 emit_ia32_arsh_i64(dst, imm32, dstk, &prog);
1860 break;
1861
1862 case BPF_ALU | BPF_NEG:
1863 emit_ia32_alu_i(is64, false, BPF_OP(code),
1864 dst_lo, 0, dstk, &prog);
1865 if (!bpf_prog->aux->verifier_zext)
1866 emit_ia32_mov_i(dst_hi, 0, dstk, &prog);
1867 break;
1868
1869 case BPF_ALU64 | BPF_NEG:
1870 emit_ia32_neg64(dst, dstk, &prog);
1871 break;
1872
1873 case BPF_ALU64 | BPF_MUL | BPF_X:
1874 case BPF_ALU64 | BPF_MUL | BPF_K:
1875 switch (BPF_SRC(code)) {
1876 case BPF_X:
1877 emit_ia32_mul_r64(dst, src, dstk, sstk, &prog);
1878 break;
1879 case BPF_K:
1880 emit_ia32_mul_i64(dst, imm32, dstk, &prog);
1881 break;
1882 }
1883 break;
1884
1885 case BPF_ALU | BPF_END | BPF_FROM_LE:
1886 emit_ia32_to_le_r64(dst, imm32, dstk, &prog,
1887 bpf_prog->aux);
1888 break;
1889
1890 case BPF_ALU | BPF_END | BPF_FROM_BE:
1891 emit_ia32_to_be_r64(dst, imm32, dstk, &prog,
1892 bpf_prog->aux);
1893 break;
1894
1895 case BPF_LD | BPF_IMM | BPF_DW: {
1896 s32 hi, lo = imm32;
1897
1898 hi = insn[1].imm;
1899 emit_ia32_mov_i(dst_lo, lo, dstk, &prog);
1900 emit_ia32_mov_i(dst_hi, hi, dstk, &prog);
1901 insn++;
1902 i++;
1903 break;
1904 }
1905
1906 case BPF_ST | BPF_NOSPEC:
1907 if (boot_cpu_has(X86_FEATURE_XMM2))
1908
1909 EMIT3(0x0F, 0xAE, 0xE8);
1910 break;
1911
1912 case BPF_ST | BPF_MEM | BPF_H:
1913 case BPF_ST | BPF_MEM | BPF_B:
1914 case BPF_ST | BPF_MEM | BPF_W:
1915 case BPF_ST | BPF_MEM | BPF_DW:
1916 if (dstk)
1917
1918 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
1919 STACK_VAR(dst_lo));
1920 else
1921
1922 EMIT2(0x8B, add_2reg(0xC0, dst_lo, IA32_EAX));
1923
1924 switch (BPF_SIZE(code)) {
1925 case BPF_B:
1926 EMIT(0xC6, 1); break;
1927 case BPF_H:
1928 EMIT2(0x66, 0xC7); break;
1929 case BPF_W:
1930 case BPF_DW:
1931 EMIT(0xC7, 1); break;
1932 }
1933
1934 if (is_imm8(insn->off))
1935 EMIT2(add_1reg(0x40, IA32_EAX), insn->off);
1936 else
1937 EMIT1_off32(add_1reg(0x80, IA32_EAX),
1938 insn->off);
1939 EMIT(imm32, bpf_size_to_x86_bytes(BPF_SIZE(code)));
1940
1941 if (BPF_SIZE(code) == BPF_DW) {
1942 u32 hi;
1943
1944 hi = imm32 & (1<<31) ? (u32)~0 : 0;
1945 EMIT2_off32(0xC7, add_1reg(0x80, IA32_EAX),
1946 insn->off + 4);
1947 EMIT(hi, 4);
1948 }
1949 break;
1950
1951
1952 case BPF_STX | BPF_MEM | BPF_B:
1953 case BPF_STX | BPF_MEM | BPF_H:
1954 case BPF_STX | BPF_MEM | BPF_W:
1955 case BPF_STX | BPF_MEM | BPF_DW:
1956 if (dstk)
1957
1958 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
1959 STACK_VAR(dst_lo));
1960 else
1961
1962 EMIT2(0x8B, add_2reg(0xC0, dst_lo, IA32_EAX));
1963
1964 if (sstk)
1965
1966 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
1967 STACK_VAR(src_lo));
1968 else
1969
1970 EMIT2(0x8B, add_2reg(0xC0, src_lo, IA32_EDX));
1971
1972 switch (BPF_SIZE(code)) {
1973 case BPF_B:
1974 EMIT(0x88, 1); break;
1975 case BPF_H:
1976 EMIT2(0x66, 0x89); break;
1977 case BPF_W:
1978 case BPF_DW:
1979 EMIT(0x89, 1); break;
1980 }
1981
1982 if (is_imm8(insn->off))
1983 EMIT2(add_2reg(0x40, IA32_EAX, IA32_EDX),
1984 insn->off);
1985 else
1986 EMIT1_off32(add_2reg(0x80, IA32_EAX, IA32_EDX),
1987 insn->off);
1988
1989 if (BPF_SIZE(code) == BPF_DW) {
1990 if (sstk)
1991
1992 EMIT3(0x8B, add_2reg(0x40, IA32_EBP,
1993 IA32_EDX),
1994 STACK_VAR(src_hi));
1995 else
1996
1997 EMIT2(0x8B, add_2reg(0xC0, src_hi,
1998 IA32_EDX));
1999 EMIT1(0x89);
2000 if (is_imm8(insn->off + 4)) {
2001 EMIT2(add_2reg(0x40, IA32_EAX,
2002 IA32_EDX),
2003 insn->off + 4);
2004 } else {
2005 EMIT1(add_2reg(0x80, IA32_EAX,
2006 IA32_EDX));
2007 EMIT(insn->off + 4, 4);
2008 }
2009 }
2010 break;
2011
2012
2013 case BPF_LDX | BPF_MEM | BPF_B:
2014 case BPF_LDX | BPF_MEM | BPF_H:
2015 case BPF_LDX | BPF_MEM | BPF_W:
2016 case BPF_LDX | BPF_MEM | BPF_DW:
2017 if (sstk)
2018
2019 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
2020 STACK_VAR(src_lo));
2021 else
2022
2023 EMIT2(0x8B, add_2reg(0xC0, src_lo, IA32_EAX));
2024
2025 switch (BPF_SIZE(code)) {
2026 case BPF_B:
2027 EMIT2(0x0F, 0xB6); break;
2028 case BPF_H:
2029 EMIT2(0x0F, 0xB7); break;
2030 case BPF_W:
2031 case BPF_DW:
2032 EMIT(0x8B, 1); break;
2033 }
2034
2035 if (is_imm8(insn->off))
2036 EMIT2(add_2reg(0x40, IA32_EAX, IA32_EDX),
2037 insn->off);
2038 else
2039 EMIT1_off32(add_2reg(0x80, IA32_EAX, IA32_EDX),
2040 insn->off);
2041
2042 if (dstk)
2043
2044 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EDX),
2045 STACK_VAR(dst_lo));
2046 else
2047
2048 EMIT2(0x89, add_2reg(0xC0, dst_lo, IA32_EDX));
2049 switch (BPF_SIZE(code)) {
2050 case BPF_B:
2051 case BPF_H:
2052 case BPF_W:
2053 if (bpf_prog->aux->verifier_zext)
2054 break;
2055 if (dstk) {
2056 EMIT3(0xC7, add_1reg(0x40, IA32_EBP),
2057 STACK_VAR(dst_hi));
2058 EMIT(0x0, 4);
2059 } else {
2060
2061 EMIT2(0x33,
2062 add_2reg(0xC0, dst_hi, dst_hi));
2063 }
2064 break;
2065 case BPF_DW:
2066 EMIT2_off32(0x8B,
2067 add_2reg(0x80, IA32_EAX, IA32_EDX),
2068 insn->off + 4);
2069 if (dstk)
2070 EMIT3(0x89,
2071 add_2reg(0x40, IA32_EBP,
2072 IA32_EDX),
2073 STACK_VAR(dst_hi));
2074 else
2075 EMIT2(0x89,
2076 add_2reg(0xC0, dst_hi, IA32_EDX));
2077 break;
2078 default:
2079 break;
2080 }
2081 break;
2082
2083 case BPF_JMP | BPF_CALL:
2084 {
2085 const u8 *r1 = bpf2ia32[BPF_REG_1];
2086 const u8 *r2 = bpf2ia32[BPF_REG_2];
2087 const u8 *r3 = bpf2ia32[BPF_REG_3];
2088 const u8 *r4 = bpf2ia32[BPF_REG_4];
2089 const u8 *r5 = bpf2ia32[BPF_REG_5];
2090
2091 if (insn->src_reg == BPF_PSEUDO_CALL)
2092 goto notyet;
2093
2094 if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) {
2095 int err;
2096
2097 err = emit_kfunc_call(bpf_prog,
2098 image + addrs[i],
2099 insn, &prog);
2100
2101 if (err)
2102 return err;
2103 break;
2104 }
2105
2106 func = (u8 *) __bpf_call_base + imm32;
2107 jmp_offset = func - (image + addrs[i]);
2108
2109 if (!imm32 || !is_simm32(jmp_offset)) {
2110 pr_err("unsupported BPF func %d addr %p image %p\n",
2111 imm32, func, image);
2112 return -EINVAL;
2113 }
2114
2115
2116 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
2117 STACK_VAR(r1[0]));
2118
2119 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
2120 STACK_VAR(r1[1]));
2121
2122 emit_push_r64(r5, &prog);
2123 emit_push_r64(r4, &prog);
2124 emit_push_r64(r3, &prog);
2125 emit_push_r64(r2, &prog);
2126
2127 EMIT1_off32(0xE8, jmp_offset + 9);
2128
2129
2130 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EAX),
2131 STACK_VAR(r0[0]));
2132
2133 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EDX),
2134 STACK_VAR(r0[1]));
2135
2136
2137 EMIT3(0x83, add_1reg(0xC0, IA32_ESP), 32);
2138 break;
2139 }
2140 case BPF_JMP | BPF_TAIL_CALL:
2141 emit_bpf_tail_call(&prog, image + addrs[i - 1]);
2142 break;
2143
2144
2145 case BPF_JMP | BPF_JEQ | BPF_X:
2146 case BPF_JMP | BPF_JNE | BPF_X:
2147 case BPF_JMP | BPF_JGT | BPF_X:
2148 case BPF_JMP | BPF_JLT | BPF_X:
2149 case BPF_JMP | BPF_JGE | BPF_X:
2150 case BPF_JMP | BPF_JLE | BPF_X:
2151 case BPF_JMP32 | BPF_JEQ | BPF_X:
2152 case BPF_JMP32 | BPF_JNE | BPF_X:
2153 case BPF_JMP32 | BPF_JGT | BPF_X:
2154 case BPF_JMP32 | BPF_JLT | BPF_X:
2155 case BPF_JMP32 | BPF_JGE | BPF_X:
2156 case BPF_JMP32 | BPF_JLE | BPF_X:
2157 case BPF_JMP32 | BPF_JSGT | BPF_X:
2158 case BPF_JMP32 | BPF_JSLE | BPF_X:
2159 case BPF_JMP32 | BPF_JSLT | BPF_X:
2160 case BPF_JMP32 | BPF_JSGE | BPF_X: {
2161 bool is_jmp64 = BPF_CLASS(insn->code) == BPF_JMP;
2162 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
2163 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
2164 u8 sreg_lo = sstk ? IA32_ECX : src_lo;
2165 u8 sreg_hi = sstk ? IA32_EBX : src_hi;
2166
2167 if (dstk) {
2168 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
2169 STACK_VAR(dst_lo));
2170 if (is_jmp64)
2171 EMIT3(0x8B,
2172 add_2reg(0x40, IA32_EBP,
2173 IA32_EDX),
2174 STACK_VAR(dst_hi));
2175 }
2176
2177 if (sstk) {
2178 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX),
2179 STACK_VAR(src_lo));
2180 if (is_jmp64)
2181 EMIT3(0x8B,
2182 add_2reg(0x40, IA32_EBP,
2183 IA32_EBX),
2184 STACK_VAR(src_hi));
2185 }
2186
2187 if (is_jmp64) {
2188
2189 EMIT2(0x39, add_2reg(0xC0, dreg_hi, sreg_hi));
2190 EMIT2(IA32_JNE, 2);
2191 }
2192
2193 EMIT2(0x39, add_2reg(0xC0, dreg_lo, sreg_lo));
2194 goto emit_cond_jmp;
2195 }
2196 case BPF_JMP | BPF_JSGT | BPF_X:
2197 case BPF_JMP | BPF_JSLE | BPF_X:
2198 case BPF_JMP | BPF_JSLT | BPF_X:
2199 case BPF_JMP | BPF_JSGE | BPF_X: {
2200 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
2201 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
2202 u8 sreg_lo = sstk ? IA32_ECX : src_lo;
2203 u8 sreg_hi = sstk ? IA32_EBX : src_hi;
2204
2205 if (dstk) {
2206 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
2207 STACK_VAR(dst_lo));
2208 EMIT3(0x8B,
2209 add_2reg(0x40, IA32_EBP,
2210 IA32_EDX),
2211 STACK_VAR(dst_hi));
2212 }
2213
2214 if (sstk) {
2215 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX),
2216 STACK_VAR(src_lo));
2217 EMIT3(0x8B,
2218 add_2reg(0x40, IA32_EBP,
2219 IA32_EBX),
2220 STACK_VAR(src_hi));
2221 }
2222
2223
2224 EMIT2(0x39, add_2reg(0xC0, dreg_hi, sreg_hi));
2225 EMIT2(IA32_JNE, 10);
2226
2227 EMIT2(0x39, add_2reg(0xC0, dreg_lo, sreg_lo));
2228 goto emit_cond_jmp_signed;
2229 }
2230 case BPF_JMP | BPF_JSET | BPF_X:
2231 case BPF_JMP32 | BPF_JSET | BPF_X: {
2232 bool is_jmp64 = BPF_CLASS(insn->code) == BPF_JMP;
2233 u8 dreg_lo = IA32_EAX;
2234 u8 dreg_hi = IA32_EDX;
2235 u8 sreg_lo = sstk ? IA32_ECX : src_lo;
2236 u8 sreg_hi = sstk ? IA32_EBX : src_hi;
2237
2238 if (dstk) {
2239 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
2240 STACK_VAR(dst_lo));
2241 if (is_jmp64)
2242 EMIT3(0x8B,
2243 add_2reg(0x40, IA32_EBP,
2244 IA32_EDX),
2245 STACK_VAR(dst_hi));
2246 } else {
2247
2248 EMIT2(0x89, add_2reg(0xC0, dreg_lo, dst_lo));
2249 if (is_jmp64)
2250
2251 EMIT2(0x89,
2252 add_2reg(0xC0, dreg_hi, dst_hi));
2253 }
2254
2255 if (sstk) {
2256 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX),
2257 STACK_VAR(src_lo));
2258 if (is_jmp64)
2259 EMIT3(0x8B,
2260 add_2reg(0x40, IA32_EBP,
2261 IA32_EBX),
2262 STACK_VAR(src_hi));
2263 }
2264
2265 EMIT2(0x23, add_2reg(0xC0, sreg_lo, dreg_lo));
2266 if (is_jmp64) {
2267
2268 EMIT2(0x23, add_2reg(0xC0, sreg_hi, dreg_hi));
2269
2270 EMIT2(0x09, add_2reg(0xC0, dreg_lo, dreg_hi));
2271 }
2272 goto emit_cond_jmp;
2273 }
2274 case BPF_JMP | BPF_JSET | BPF_K:
2275 case BPF_JMP32 | BPF_JSET | BPF_K: {
2276 bool is_jmp64 = BPF_CLASS(insn->code) == BPF_JMP;
2277 u8 dreg_lo = IA32_EAX;
2278 u8 dreg_hi = IA32_EDX;
2279 u8 sreg_lo = IA32_ECX;
2280 u8 sreg_hi = IA32_EBX;
2281 u32 hi;
2282
2283 if (dstk) {
2284 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
2285 STACK_VAR(dst_lo));
2286 if (is_jmp64)
2287 EMIT3(0x8B,
2288 add_2reg(0x40, IA32_EBP,
2289 IA32_EDX),
2290 STACK_VAR(dst_hi));
2291 } else {
2292
2293 EMIT2(0x89, add_2reg(0xC0, dreg_lo, dst_lo));
2294 if (is_jmp64)
2295
2296 EMIT2(0x89,
2297 add_2reg(0xC0, dreg_hi, dst_hi));
2298 }
2299
2300
2301 EMIT2_off32(0xC7, add_1reg(0xC0, sreg_lo), imm32);
2302
2303
2304 EMIT2(0x23, add_2reg(0xC0, sreg_lo, dreg_lo));
2305 if (is_jmp64) {
2306 hi = imm32 & (1 << 31) ? (u32)~0 : 0;
2307
2308 EMIT2_off32(0xC7, add_1reg(0xC0, sreg_hi), hi);
2309
2310 EMIT2(0x23, add_2reg(0xC0, sreg_hi, dreg_hi));
2311
2312 EMIT2(0x09, add_2reg(0xC0, dreg_lo, dreg_hi));
2313 }
2314 goto emit_cond_jmp;
2315 }
2316 case BPF_JMP | BPF_JEQ | BPF_K:
2317 case BPF_JMP | BPF_JNE | BPF_K:
2318 case BPF_JMP | BPF_JGT | BPF_K:
2319 case BPF_JMP | BPF_JLT | BPF_K:
2320 case BPF_JMP | BPF_JGE | BPF_K:
2321 case BPF_JMP | BPF_JLE | BPF_K:
2322 case BPF_JMP32 | BPF_JEQ | BPF_K:
2323 case BPF_JMP32 | BPF_JNE | BPF_K:
2324 case BPF_JMP32 | BPF_JGT | BPF_K:
2325 case BPF_JMP32 | BPF_JLT | BPF_K:
2326 case BPF_JMP32 | BPF_JGE | BPF_K:
2327 case BPF_JMP32 | BPF_JLE | BPF_K:
2328 case BPF_JMP32 | BPF_JSGT | BPF_K:
2329 case BPF_JMP32 | BPF_JSLE | BPF_K:
2330 case BPF_JMP32 | BPF_JSLT | BPF_K:
2331 case BPF_JMP32 | BPF_JSGE | BPF_K: {
2332 bool is_jmp64 = BPF_CLASS(insn->code) == BPF_JMP;
2333 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
2334 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
2335 u8 sreg_lo = IA32_ECX;
2336 u8 sreg_hi = IA32_EBX;
2337 u32 hi;
2338
2339 if (dstk) {
2340 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
2341 STACK_VAR(dst_lo));
2342 if (is_jmp64)
2343 EMIT3(0x8B,
2344 add_2reg(0x40, IA32_EBP,
2345 IA32_EDX),
2346 STACK_VAR(dst_hi));
2347 }
2348
2349
2350 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_ECX), imm32);
2351 if (is_jmp64) {
2352 hi = imm32 & (1 << 31) ? (u32)~0 : 0;
2353
2354 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_EBX), hi);
2355
2356 EMIT2(0x39, add_2reg(0xC0, dreg_hi, sreg_hi));
2357 EMIT2(IA32_JNE, 2);
2358 }
2359
2360 EMIT2(0x39, add_2reg(0xC0, dreg_lo, sreg_lo));
2361
2362emit_cond_jmp: jmp_cond = get_cond_jmp_opcode(BPF_OP(code), false);
2363 if (jmp_cond == COND_JMP_OPCODE_INVALID)
2364 return -EFAULT;
2365 jmp_offset = addrs[i + insn->off] - addrs[i];
2366 if (is_imm8(jmp_offset)) {
2367 EMIT2(jmp_cond, jmp_offset);
2368 } else if (is_simm32(jmp_offset)) {
2369 EMIT2_off32(0x0F, jmp_cond + 0x10, jmp_offset);
2370 } else {
2371 pr_err("cond_jmp gen bug %llx\n", jmp_offset);
2372 return -EFAULT;
2373 }
2374 break;
2375 }
2376 case BPF_JMP | BPF_JSGT | BPF_K:
2377 case BPF_JMP | BPF_JSLE | BPF_K:
2378 case BPF_JMP | BPF_JSLT | BPF_K:
2379 case BPF_JMP | BPF_JSGE | BPF_K: {
2380 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
2381 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
2382 u8 sreg_lo = IA32_ECX;
2383 u8 sreg_hi = IA32_EBX;
2384 u32 hi;
2385
2386 if (dstk) {
2387 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
2388 STACK_VAR(dst_lo));
2389 EMIT3(0x8B,
2390 add_2reg(0x40, IA32_EBP,
2391 IA32_EDX),
2392 STACK_VAR(dst_hi));
2393 }
2394
2395
2396 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_ECX), imm32);
2397 hi = imm32 & (1 << 31) ? (u32)~0 : 0;
2398
2399 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_EBX), hi);
2400
2401 EMIT2(0x39, add_2reg(0xC0, dreg_hi, sreg_hi));
2402 EMIT2(IA32_JNE, 10);
2403
2404 EMIT2(0x39, add_2reg(0xC0, dreg_lo, sreg_lo));
2405
2406
2407
2408
2409
2410emit_cond_jmp_signed:
2411 jmp_cond = get_cond_jmp_opcode(BPF_OP(code), true);
2412 if (jmp_cond == COND_JMP_OPCODE_INVALID)
2413 return -EFAULT;
2414 jmp_offset = addrs[i + insn->off] - addrs[i] + 8;
2415 if (is_simm32(jmp_offset)) {
2416 EMIT2_off32(0x0F, jmp_cond + 0x10, jmp_offset);
2417 } else {
2418 pr_err("cond_jmp gen bug %llx\n", jmp_offset);
2419 return -EFAULT;
2420 }
2421 EMIT2(0xEB, 6);
2422
2423
2424 jmp_cond = get_cond_jmp_opcode(BPF_OP(code), false);
2425 if (jmp_cond == COND_JMP_OPCODE_INVALID)
2426 return -EFAULT;
2427 jmp_offset = addrs[i + insn->off] - addrs[i];
2428 if (is_simm32(jmp_offset)) {
2429 EMIT2_off32(0x0F, jmp_cond + 0x10, jmp_offset);
2430 } else {
2431 pr_err("cond_jmp gen bug %llx\n", jmp_offset);
2432 return -EFAULT;
2433 }
2434 break;
2435 }
2436 case BPF_JMP | BPF_JA:
2437 if (insn->off == -1)
2438
2439
2440
2441
2442
2443
2444 jmp_offset = -2;
2445 else
2446 jmp_offset = addrs[i + insn->off] - addrs[i];
2447
2448 if (!jmp_offset)
2449
2450 break;
2451emit_jmp:
2452 if (is_imm8(jmp_offset)) {
2453 EMIT2(0xEB, jmp_offset);
2454 } else if (is_simm32(jmp_offset)) {
2455 EMIT1_off32(0xE9, jmp_offset);
2456 } else {
2457 pr_err("jmp gen bug %llx\n", jmp_offset);
2458 return -EFAULT;
2459 }
2460 break;
2461 case BPF_STX | BPF_ATOMIC | BPF_W:
2462 case BPF_STX | BPF_ATOMIC | BPF_DW:
2463 goto notyet;
2464 case BPF_JMP | BPF_EXIT:
2465 if (seen_exit) {
2466 jmp_offset = ctx->cleanup_addr - addrs[i];
2467 goto emit_jmp;
2468 }
2469 seen_exit = true;
2470
2471 ctx->cleanup_addr = proglen;
2472 emit_epilogue(&prog, bpf_prog->aux->stack_depth);
2473 break;
2474notyet:
2475 pr_info_once("*** NOT YET: opcode %02x ***\n", code);
2476 return -EFAULT;
2477 default:
2478
2479
2480
2481
2482
2483 pr_err("bpf_jit: unknown opcode %02x\n", code);
2484 return -EINVAL;
2485 }
2486
2487 ilen = prog - temp;
2488 if (ilen > BPF_MAX_INSN_SIZE) {
2489 pr_err("bpf_jit: fatal insn size error\n");
2490 return -EFAULT;
2491 }
2492
2493 if (image) {
2494
2495
2496
2497
2498
2499
2500
2501
2502 if (unlikely(proglen + ilen > oldproglen ||
2503 proglen + ilen != addrs[i])) {
2504 pr_err("bpf_jit: fatal error\n");
2505 return -EFAULT;
2506 }
2507 memcpy(image + proglen, temp, ilen);
2508 }
2509 proglen += ilen;
2510 addrs[i] = proglen;
2511 prog = temp;
2512 }
2513 return proglen;
2514}
2515
2516bool bpf_jit_needs_zext(void)
2517{
2518 return true;
2519}
2520
2521struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
2522{
2523 struct bpf_binary_header *header = NULL;
2524 struct bpf_prog *tmp, *orig_prog = prog;
2525 int proglen, oldproglen = 0;
2526 struct jit_context ctx = {};
2527 bool tmp_blinded = false;
2528 u8 *image = NULL;
2529 int *addrs;
2530 int pass;
2531 int i;
2532
2533 if (!prog->jit_requested)
2534 return orig_prog;
2535
2536 tmp = bpf_jit_blind_constants(prog);
2537
2538
2539
2540
2541 if (IS_ERR(tmp))
2542 return orig_prog;
2543 if (tmp != prog) {
2544 tmp_blinded = true;
2545 prog = tmp;
2546 }
2547
2548 addrs = kmalloc_array(prog->len, sizeof(*addrs), GFP_KERNEL);
2549 if (!addrs) {
2550 prog = orig_prog;
2551 goto out;
2552 }
2553
2554
2555
2556
2557
2558 for (proglen = 0, i = 0; i < prog->len; i++) {
2559 proglen += 64;
2560 addrs[i] = proglen;
2561 }
2562 ctx.cleanup_addr = proglen;
2563
2564
2565
2566
2567
2568
2569
2570 for (pass = 0; pass < 20 || image; pass++) {
2571 proglen = do_jit(prog, addrs, image, oldproglen, &ctx);
2572 if (proglen <= 0) {
2573out_image:
2574 image = NULL;
2575 if (header)
2576 bpf_jit_binary_free(header);
2577 prog = orig_prog;
2578 goto out_addrs;
2579 }
2580 if (image) {
2581 if (proglen != oldproglen) {
2582 pr_err("bpf_jit: proglen=%d != oldproglen=%d\n",
2583 proglen, oldproglen);
2584 goto out_image;
2585 }
2586 break;
2587 }
2588 if (proglen == oldproglen) {
2589 header = bpf_jit_binary_alloc(proglen, &image,
2590 1, jit_fill_hole);
2591 if (!header) {
2592 prog = orig_prog;
2593 goto out_addrs;
2594 }
2595 }
2596 oldproglen = proglen;
2597 cond_resched();
2598 }
2599
2600 if (bpf_jit_enable > 1)
2601 bpf_jit_dump(prog->len, proglen, pass + 1, image);
2602
2603 if (image) {
2604 bpf_jit_binary_lock_ro(header);
2605 prog->bpf_func = (void *)image;
2606 prog->jited = 1;
2607 prog->jited_len = proglen;
2608 } else {
2609 prog = orig_prog;
2610 }
2611
2612out_addrs:
2613 kfree(addrs);
2614out:
2615 if (tmp_blinded)
2616 bpf_jit_prog_release_other(prog, prog == orig_prog ?
2617 tmp : orig_prog);
2618 return prog;
2619}
2620
2621bool bpf_jit_supports_kfunc_call(void)
2622{
2623 return true;
2624}
2625