github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/cmd/libsnap-confine-private/bpf/bpf-insn.h (about)

     1  /* SPDX-License-Identifier: GPL-2.0 */
     2  
     3  /* imported from the Linux kernel, commit
     4   * 77d34a4683b053108ecd466cc7c4193b45805528 (v5.13-11855-g77d34a4683b0) */
     5  
     6  #ifndef __BPF_INSN_H__
     7  #define __BPF_INSN_H__
     8  
     9  #include <linux/bpf.h>
    10  
    11  /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
    12  
    13  #define BPF_ALU64_REG(OP, DST, SRC)				\
    14  	((struct bpf_insn) {					\
    15  		.code  = BPF_ALU64 | BPF_OP(OP) | BPF_X,	\
    16  		.dst_reg = DST,					\
    17  		.src_reg = SRC,					\
    18  		.off   = 0,					\
    19  		.imm   = 0 })
    20  
    21  #define BPF_ALU32_REG(OP, DST, SRC)				\
    22  	((struct bpf_insn) {					\
    23  		.code  = BPF_ALU | BPF_OP(OP) | BPF_X,		\
    24  		.dst_reg = DST,					\
    25  		.src_reg = SRC,					\
    26  		.off   = 0,					\
    27  		.imm   = 0 })
    28  
    29  /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
    30  
    31  #define BPF_ALU64_IMM(OP, DST, IMM)				\
    32  	((struct bpf_insn) {					\
    33  		.code  = BPF_ALU64 | BPF_OP(OP) | BPF_K,	\
    34  		.dst_reg = DST,					\
    35  		.src_reg = 0,					\
    36  		.off   = 0,					\
    37  		.imm   = IMM })
    38  
    39  #define BPF_ALU32_IMM(OP, DST, IMM)				\
    40  	((struct bpf_insn) {					\
    41  		.code  = BPF_ALU | BPF_OP(OP) | BPF_K,		\
    42  		.dst_reg = DST,					\
    43  		.src_reg = 0,					\
    44  		.off   = 0,					\
    45  		.imm   = IMM })
    46  
    47  /* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */
    48  
    49  #define BPF_ENDIAN(TYPE, DST, LEN)				\
    50  	((struct bpf_insn) {					\
    51  		.code  = BPF_ALU | BPF_END | BPF_SRC(TYPE),	\
    52  		.dst_reg = DST,					\
    53  		.src_reg = 0,					\
    54  		.off   = 0,					\
    55  		.imm   = LEN })
    56  
    57  /* Short form of mov, dst_reg = src_reg */
    58  
    59  #define BPF_MOV64_REG(DST, SRC)					\
    60  	((struct bpf_insn) {					\
    61  		.code  = BPF_ALU64 | BPF_MOV | BPF_X,		\
    62  		.dst_reg = DST,					\
    63  		.src_reg = SRC,					\
    64  		.off   = 0,					\
    65  		.imm   = 0 })
    66  
    67  #define BPF_MOV32_REG(DST, SRC)					\
    68  	((struct bpf_insn) {					\
    69  		.code  = BPF_ALU | BPF_MOV | BPF_X,		\
    70  		.dst_reg = DST,					\
    71  		.src_reg = SRC,					\
    72  		.off   = 0,					\
    73  		.imm   = 0 })
    74  
    75  /* Short form of mov, dst_reg = imm32 */
    76  
    77  #define BPF_MOV64_IMM(DST, IMM)					\
    78  	((struct bpf_insn) {					\
    79  		.code  = BPF_ALU64 | BPF_MOV | BPF_K,		\
    80  		.dst_reg = DST,					\
    81  		.src_reg = 0,					\
    82  		.off   = 0,					\
    83  		.imm   = IMM })
    84  
    85  #define BPF_MOV32_IMM(DST, IMM)					\
    86  	((struct bpf_insn) {					\
    87  		.code  = BPF_ALU | BPF_MOV | BPF_K,		\
    88  		.dst_reg = DST,					\
    89  		.src_reg = 0,					\
    90  		.off   = 0,					\
    91  		.imm   = IMM })
    92  
    93  /* Special form of mov32, used for doing explicit zero extension on dst. */
    94  #define BPF_ZEXT_REG(DST)					\
    95  	((struct bpf_insn) {					\
    96  		.code  = BPF_ALU | BPF_MOV | BPF_X,		\
    97  		.dst_reg = DST,					\
    98  		.src_reg = DST,					\
    99  		.off   = 0,					\
   100  		.imm   = 1 })
   101  
   102  /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
   103  #define BPF_LD_IMM64(DST, IMM)					\
   104  	BPF_LD_IMM64_RAW(DST, 0, IMM)
   105  
   106  #define BPF_LD_IMM64_RAW(DST, SRC, IMM)				\
   107  	((struct bpf_insn) {					\
   108  		.code  = BPF_LD | BPF_DW | BPF_IMM,		\
   109  		.dst_reg = DST,					\
   110  		.src_reg = SRC,					\
   111  		.off   = 0,					\
   112  		.imm   = (__u32) (IMM) }),			\
   113  	((struct bpf_insn) {					\
   114  		.code  = 0, /* zero is reserved opcode */	\
   115  		.dst_reg = 0,					\
   116  		.src_reg = 0,					\
   117  		.off   = 0,					\
   118  		.imm   = ((__u64) (IMM)) >> 32 })
   119  
   120  /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
   121  #define BPF_LD_MAP_FD(DST, MAP_FD)				\
   122  	BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
   123  
   124  /* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */
   125  
   126  #define BPF_MOV64_RAW(TYPE, DST, SRC, IMM)			\
   127  	((struct bpf_insn) {					\
   128  		.code  = BPF_ALU64 | BPF_MOV | BPF_SRC(TYPE),	\
   129  		.dst_reg = DST,					\
   130  		.src_reg = SRC,					\
   131  		.off   = 0,					\
   132  		.imm   = IMM })
   133  
   134  #define BPF_MOV32_RAW(TYPE, DST, SRC, IMM)			\
   135  	((struct bpf_insn) {					\
   136  		.code  = BPF_ALU | BPF_MOV | BPF_SRC(TYPE),	\
   137  		.dst_reg = DST,					\
   138  		.src_reg = SRC,					\
   139  		.off   = 0,					\
   140  		.imm   = IMM })
   141  
   142  /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
   143  
   144  #define BPF_LD_ABS(SIZE, IMM)					\
   145  	((struct bpf_insn) {					\
   146  		.code  = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS,	\
   147  		.dst_reg = 0,					\
   148  		.src_reg = 0,					\
   149  		.off   = 0,					\
   150  		.imm   = IMM })
   151  
   152  /* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */
   153  
   154  #define BPF_LD_IND(SIZE, SRC, IMM)				\
   155  	((struct bpf_insn) {					\
   156  		.code  = BPF_LD | BPF_SIZE(SIZE) | BPF_IND,	\
   157  		.dst_reg = 0,					\
   158  		.src_reg = SRC,					\
   159  		.off   = 0,					\
   160  		.imm   = IMM })
   161  
   162  /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
   163  
   164  #define BPF_LDX_MEM(SIZE, DST, SRC, OFF)			\
   165  	((struct bpf_insn) {					\
   166  		.code  = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM,	\
   167  		.dst_reg = DST,					\
   168  		.src_reg = SRC,					\
   169  		.off   = OFF,					\
   170  		.imm   = 0 })
   171  
   172  /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
   173  
   174  #define BPF_STX_MEM(SIZE, DST, SRC, OFF)			\
   175  	((struct bpf_insn) {					\
   176  		.code  = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM,	\
   177  		.dst_reg = DST,					\
   178  		.src_reg = SRC,					\
   179  		.off   = OFF,					\
   180  		.imm   = 0 })
   181  
   182  /*
   183   * Atomic operations:
   184   *
   185   *   BPF_ADD                  *(uint *) (dst_reg + off16) += src_reg
   186   *   BPF_AND                  *(uint *) (dst_reg + off16) &= src_reg
   187   *   BPF_OR                   *(uint *) (dst_reg + off16) |= src_reg
   188   *   BPF_XOR                  *(uint *) (dst_reg + off16) ^= src_reg
   189   *   BPF_ADD | BPF_FETCH      src_reg = atomic_fetch_add(dst_reg + off16, src_reg);
   190   *   BPF_AND | BPF_FETCH      src_reg = atomic_fetch_and(dst_reg + off16, src_reg);
   191   *   BPF_OR | BPF_FETCH       src_reg = atomic_fetch_or(dst_reg + off16, src_reg);
   192   *   BPF_XOR | BPF_FETCH      src_reg = atomic_fetch_xor(dst_reg + off16, src_reg);
   193   *   BPF_XCHG                 src_reg = atomic_xchg(dst_reg + off16, src_reg)
   194   *   BPF_CMPXCHG              r0 = atomic_cmpxchg(dst_reg + off16, r0, src_reg)
   195   */
   196  
   197  #define BPF_ATOMIC_OP(SIZE, OP, DST, SRC, OFF)			\
   198  	((struct bpf_insn) {					\
   199  		.code  = BPF_STX | BPF_SIZE(SIZE) | BPF_ATOMIC,	\
   200  		.dst_reg = DST,					\
   201  		.src_reg = SRC,					\
   202  		.off   = OFF,					\
   203  		.imm   = OP })
   204  
   205  /* Legacy alias */
   206  #define BPF_STX_XADD(SIZE, DST, SRC, OFF) BPF_ATOMIC_OP(SIZE, BPF_ADD, DST, SRC, OFF)
   207  
   208  /* Memory store, *(uint *) (dst_reg + off16) = imm32 */
   209  
   210  #define BPF_ST_MEM(SIZE, DST, OFF, IMM)				\
   211  	((struct bpf_insn) {					\
   212  		.code  = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM,	\
   213  		.dst_reg = DST,					\
   214  		.src_reg = 0,					\
   215  		.off   = OFF,					\
   216  		.imm   = IMM })
   217  
   218  /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
   219  
   220  #define BPF_JMP_REG(OP, DST, SRC, OFF)				\
   221  	((struct bpf_insn) {					\
   222  		.code  = BPF_JMP | BPF_OP(OP) | BPF_X,		\
   223  		.dst_reg = DST,					\
   224  		.src_reg = SRC,					\
   225  		.off   = OFF,					\
   226  		.imm   = 0 })
   227  
   228  /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
   229  
   230  #define BPF_JMP_IMM(OP, DST, IMM, OFF)				\
   231  	((struct bpf_insn) {					\
   232  		.code  = BPF_JMP | BPF_OP(OP) | BPF_K,		\
   233  		.dst_reg = DST,					\
   234  		.src_reg = 0,					\
   235  		.off   = OFF,					\
   236  		.imm   = IMM })
   237  
   238  /* Like BPF_JMP_REG, but with 32-bit wide operands for comparison. */
   239  
   240  #define BPF_JMP32_REG(OP, DST, SRC, OFF)			\
   241  	((struct bpf_insn) {					\
   242  		.code  = BPF_JMP32 | BPF_OP(OP) | BPF_X,	\
   243  		.dst_reg = DST,					\
   244  		.src_reg = SRC,					\
   245  		.off   = OFF,					\
   246  		.imm   = 0 })
   247  
   248  /* Like BPF_JMP_IMM, but with 32-bit wide operands for comparison. */
   249  
   250  #define BPF_JMP32_IMM(OP, DST, IMM, OFF)			\
   251  	((struct bpf_insn) {					\
   252  		.code  = BPF_JMP32 | BPF_OP(OP) | BPF_K,	\
   253  		.dst_reg = DST,					\
   254  		.src_reg = 0,					\
   255  		.off   = OFF,					\
   256  		.imm   = IMM })
   257  
   258  /* Unconditional jumps, goto pc + off16 */
   259  
   260  #define BPF_JMP_A(OFF)						\
   261  	((struct bpf_insn) {					\
   262  		.code  = BPF_JMP | BPF_JA,			\
   263  		.dst_reg = 0,					\
   264  		.src_reg = 0,					\
   265  		.off   = OFF,					\
   266  		.imm   = 0 })
   267  
   268  /* Relative call */
   269  
   270  #define BPF_CALL_REL(TGT)					\
   271  	((struct bpf_insn) {					\
   272  		.code  = BPF_JMP | BPF_CALL,			\
   273  		.dst_reg = 0,					\
   274  		.src_reg = BPF_PSEUDO_CALL,			\
   275  		.off   = 0,					\
   276  		.imm   = TGT })
   277  
   278  /* Function call */
   279  
   280  #define BPF_CAST_CALL(x)					\
   281  		((u64 (*)(u64, u64, u64, u64, u64))(x))
   282  
   283  #define BPF_EMIT_CALL(FUNC)					\
   284  	((struct bpf_insn) {					\
   285  		.code  = BPF_JMP | BPF_CALL,			\
   286  		.dst_reg = 0,					\
   287  		.src_reg = 0,					\
   288  		.off   = 0,					\
   289  		.imm   = ((FUNC) - __bpf_call_base) })
   290  
   291  /* Raw code statement block */
   292  
   293  #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM)			\
   294  	((struct bpf_insn) {					\
   295  		.code  = CODE,					\
   296  		.dst_reg = DST,					\
   297  		.src_reg = SRC,					\
   298  		.off   = OFF,					\
   299  		.imm   = IMM })
   300  
   301  /* Program exit */
   302  
   303  #define BPF_EXIT_INSN()						\
   304  	((struct bpf_insn) {					\
   305  		.code  = BPF_JMP | BPF_EXIT,			\
   306  		.dst_reg = 0,					\
   307  		.src_reg = 0,					\
   308  		.off   = 0,					\
   309  		.imm   = 0 })
   310  
   311  #endif				/* __BPF_INSN_H__ */