32
32
#include <asm/set_memory.h>
33
33
#include <asm/text-patching.h>
34
34
#include <asm/unwind.h>
35
- #include "bpf_jit.h"
36
35
37
36
struct bpf_jit {
38
37
u32 seen ; /* Flags to remember seen eBPF instructions */
@@ -54,7 +53,7 @@ struct bpf_jit {
54
53
int prologue_plt ; /* Start of prologue hotpatch PLT */
55
54
int kern_arena ; /* Pool offset of kernel arena address */
56
55
u64 user_arena ; /* User arena address */
57
- u32 frame_off ; /* Offset of frame from %r15 */
56
+ u32 frame_off ; /* Offset of struct bpf_prog from %r15 */
58
57
};
59
58
60
59
#define SEEN_MEM BIT(0) /* use mem[] for temporary storage */
@@ -426,12 +425,26 @@ static void jit_fill_hole(void *area, unsigned int size)
426
425
memset (area , 0 , size );
427
426
}
428
427
428
+ /*
429
+ * Caller-allocated part of the frame.
430
+ * Thanks to packed stack, its otherwise unused initial part can be used for
431
+ * the BPF stack and for the next frame.
432
+ */
433
+ struct prog_frame {
434
+ u64 unused [8 ];
435
+ /* BPF stack starts here and grows towards 0 */
436
+ u32 tail_call_cnt ;
437
+ u32 pad ;
438
+ u64 r6 [10 ]; /* r6 - r15 */
439
+ u64 backchain ;
440
+ } __packed ;
441
+
429
442
/*
430
443
* Save registers from "rs" (register start) to "re" (register end) on stack
431
444
*/
432
445
static void save_regs (struct bpf_jit * jit , u32 rs , u32 re )
433
446
{
434
- u32 off = STK_OFF_R6 + (rs - 6 ) * 8 ;
447
+ u32 off = offsetof( struct prog_frame , r6 ) + (rs - 6 ) * 8 ;
435
448
436
449
if (rs == re )
437
450
/* stg %rs,off(%r15) */
@@ -446,7 +459,7 @@ static void save_regs(struct bpf_jit *jit, u32 rs, u32 re)
446
459
*/
447
460
static void restore_regs (struct bpf_jit * jit , u32 rs , u32 re )
448
461
{
449
- u32 off = jit -> frame_off + STK_OFF_R6 + (rs - 6 ) * 8 ;
462
+ u32 off = jit -> frame_off + offsetof( struct prog_frame , r6 ) + (rs - 6 ) * 8 ;
450
463
451
464
if (rs == re )
452
465
/* lg %rs,off(%r15) */
@@ -570,19 +583,22 @@ static void bpf_jit_plt(struct bpf_plt *plt, void *ret, void *target)
570
583
* Emit function prologue
571
584
*
572
585
* Save registers and create stack frame if necessary.
573
- * See stack frame layout description in "bpf_jit.h"!
586
+ * Stack frame layout is described by struct prog_frame.
574
587
*/
575
588
static void bpf_jit_prologue (struct bpf_jit * jit , struct bpf_prog * fp )
576
589
{
590
+ BUILD_BUG_ON (sizeof (struct prog_frame ) != STACK_FRAME_OVERHEAD );
591
+
577
592
/* No-op for hotpatching */
578
593
/* brcl 0,prologue_plt */
579
594
EMIT6_PCREL_RILC (0xc0040000 , 0 , jit -> prologue_plt );
580
595
jit -> prologue_plt_ret = jit -> prg ;
581
596
582
597
if (!bpf_is_subprog (fp )) {
583
598
/* Initialize the tail call counter in the main program. */
584
- /* xc STK_OFF_TCCNT(4,%r15),STK_OFF_TCCNT(%r15) */
585
- _EMIT6 (0xd703f000 | STK_OFF_TCCNT , 0xf000 | STK_OFF_TCCNT );
599
+ /* xc tail_call_cnt(4,%r15),tail_call_cnt(%r15) */
600
+ _EMIT6 (0xd703f000 | offsetof(struct prog_frame , tail_call_cnt ),
601
+ 0xf000 | offsetof(struct prog_frame , tail_call_cnt ));
586
602
} else {
587
603
/*
588
604
* Skip the tail call counter initialization in subprograms.
@@ -625,13 +641,15 @@ static void bpf_jit_prologue(struct bpf_jit *jit, struct bpf_prog *fp)
625
641
if (is_first_pass (jit ) || (jit -> seen & SEEN_STACK )) {
626
642
/* lgr %w1,%r15 (backchain) */
627
643
EMIT4 (0xb9040000 , REG_W1 , REG_15 );
628
- /* la %bfp,STK_160_UNUSED(%r15) (BPF frame pointer) */
629
- EMIT4_DISP (0x41000000 , BPF_REG_FP , REG_15 , STK_160_UNUSED );
644
+ /* la %bfp,unused_end(%r15) (BPF frame pointer) */
645
+ EMIT4_DISP (0x41000000 , BPF_REG_FP , REG_15 ,
646
+ offsetofend (struct prog_frame , unused ));
630
647
/* aghi %r15,-frame_off */
631
648
EMIT4_IMM (0xa70b0000 , REG_15 , - jit -> frame_off );
632
- /* stg %w1,152 (%r15) (backchain ) */
649
+ /* stg %w1,backchain (%r15) */
633
650
EMIT6_DISP_LH (0xe3000000 , 0x0024 , REG_W1 , REG_0 ,
634
- REG_15 , 152 );
651
+ REG_15 ,
652
+ offsetof(struct prog_frame , backchain ));
635
653
}
636
654
}
637
655
@@ -1774,9 +1792,10 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
1774
1792
* Note 2: We assume that the verifier does not let us call the
1775
1793
* main program, which clears the tail call counter on entry.
1776
1794
*/
1777
- /* mvc STK_OFF_TCCNT(4,%r15),frame_off+STK_OFF_TCCNT(%r15) */
1778
- _EMIT6 (0xd203f000 | STK_OFF_TCCNT ,
1779
- 0xf000 | (jit -> frame_off + STK_OFF_TCCNT ));
1795
+ /* mvc tail_call_cnt(4,%r15),frame_off+tail_call_cnt(%r15) */
1796
+ _EMIT6 (0xd203f000 | offsetof(struct prog_frame , tail_call_cnt ),
1797
+ 0xf000 | (jit -> frame_off +
1798
+ offsetof(struct prog_frame , tail_call_cnt )));
1780
1799
1781
1800
/* Sign-extend the kfunc arguments. */
1782
1801
if (insn -> src_reg == BPF_PSEUDO_KFUNC_CALL ) {
@@ -1827,7 +1846,8 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
1827
1846
* goto out;
1828
1847
*/
1829
1848
1830
- off = jit -> frame_off + STK_OFF_TCCNT ;
1849
+ off = jit -> frame_off +
1850
+ offsetof(struct prog_frame , tail_call_cnt );
1831
1851
/* lhi %w0,1 */
1832
1852
EMIT4_IMM (0xa7080000 , REG_W0 , 1 );
1833
1853
/* laal %w1,%w0,off(%r15) */
@@ -2160,7 +2180,9 @@ static int bpf_jit_prog(struct bpf_jit *jit, struct bpf_prog *fp,
2160
2180
jit -> prg = 0 ;
2161
2181
jit -> excnt = 0 ;
2162
2182
if (is_first_pass (jit ) || (jit -> seen & SEEN_STACK ))
2163
- jit -> frame_off = STK_OFF + round_up (fp -> aux -> stack_depth , 8 );
2183
+ jit -> frame_off = sizeof (struct prog_frame ) -
2184
+ offsetofend (struct prog_frame , unused ) +
2185
+ round_up (fp -> aux -> stack_depth , 8 );
2164
2186
else
2165
2187
jit -> frame_off = 0 ;
2166
2188
@@ -2642,9 +2664,10 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
2642
2664
/* stg %r1,backchain_off(%r15) */
2643
2665
EMIT6_DISP_LH (0xe3000000 , 0x0024 , REG_1 , REG_0 , REG_15 ,
2644
2666
tjit -> backchain_off );
2645
- /* mvc tccnt_off(4,%r15),stack_size+STK_OFF_TCCNT (%r15) */
2667
+ /* mvc tccnt_off(4,%r15),stack_size+tail_call_cnt (%r15) */
2646
2668
_EMIT6 (0xd203f000 | tjit -> tccnt_off ,
2647
- 0xf000 | (tjit -> stack_size + STK_OFF_TCCNT ));
2669
+ 0xf000 | (tjit -> stack_size +
2670
+ offsetof(struct prog_frame , tail_call_cnt )));
2648
2671
/* stmg %r2,%rN,fwd_reg_args_off(%r15) */
2649
2672
if (nr_reg_args )
2650
2673
EMIT6_DISP_LH (0xeb000000 , 0x0024 , REG_2 ,
@@ -2781,8 +2804,9 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
2781
2804
(nr_stack_args * sizeof (u64 ) - 1 ) << 16 |
2782
2805
tjit -> stack_args_off ,
2783
2806
0xf000 | tjit -> orig_stack_args_off );
2784
- /* mvc STK_OFF_TCCNT(4,%r15),tccnt_off(%r15) */
2785
- _EMIT6 (0xd203f000 | STK_OFF_TCCNT , 0xf000 | tjit -> tccnt_off );
2807
+ /* mvc tail_call_cnt(4,%r15),tccnt_off(%r15) */
2808
+ _EMIT6 (0xd203f000 | offsetof(struct prog_frame , tail_call_cnt ),
2809
+ 0xf000 | tjit -> tccnt_off );
2786
2810
/* lgr %r1,%r8 */
2787
2811
EMIT4 (0xb9040000 , REG_1 , REG_8 );
2788
2812
/* %r1() */
@@ -2839,8 +2863,9 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
2839
2863
if (flags & (BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_RET_FENTRY_RET ))
2840
2864
EMIT6_DISP_LH (0xe3000000 , 0x0004 , REG_2 , REG_0 , REG_15 ,
2841
2865
tjit -> retval_off );
2842
- /* mvc stack_size+STK_OFF_TCCNT(4,%r15),tccnt_off(%r15) */
2843
- _EMIT6 (0xd203f000 | (tjit -> stack_size + STK_OFF_TCCNT ),
2866
+ /* mvc stack_size+tail_call_cnt(4,%r15),tccnt_off(%r15) */
2867
+ _EMIT6 (0xd203f000 | (tjit -> stack_size +
2868
+ offsetof(struct prog_frame , tail_call_cnt )),
2844
2869
0xf000 | tjit -> tccnt_off );
2845
2870
/* aghi %r15,stack_size */
2846
2871
EMIT4_IMM (0xa70b0000 , REG_15 , tjit -> stack_size );
0 commit comments