instruction-set.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. .. contents::
  2. .. sectnum::
  3. ========================================
  4. eBPF Instruction Set Specification, v1.0
  5. ========================================
  6. This document specifies version 1.0 of the eBPF instruction set.
  7. Registers and calling convention
  8. ================================
  9. eBPF has 10 general purpose registers and a read-only frame pointer register,
  10. all of which are 64-bits wide.
  11. The eBPF calling convention is defined as:
  12. * R0: return value from function calls, and exit value for eBPF programs
  13. * R1 - R5: arguments for function calls
  14. * R6 - R9: callee saved registers that function calls will preserve
  15. * R10: read-only frame pointer to access stack
  16. R0 - R5 are scratch registers and eBPF programs needs to spill/fill them if
  17. necessary across calls.
  18. Instruction encoding
  19. ====================
  20. eBPF has two instruction encodings:
  21. * the basic instruction encoding, which uses 64 bits to encode an instruction
  22. * the wide instruction encoding, which appends a second 64-bit immediate value
  23. (imm64) after the basic instruction for a total of 128 bits.
  24. The basic instruction encoding looks as follows:
  25. ============= ======= =============== ==================== ============
  26. 32 bits (MSB) 16 bits 4 bits 4 bits 8 bits (LSB)
  27. ============= ======= =============== ==================== ============
  28. immediate offset source register destination register opcode
  29. ============= ======= =============== ==================== ============
  30. Note that most instructions do not use all of the fields.
  31. Unused fields shall be cleared to zero.
  32. Instruction classes
  33. -------------------
  34. The three LSB bits of the 'opcode' field store the instruction class:
  35. ========= ===== =============================== ===================================
  36. class value description reference
  37. ========= ===== =============================== ===================================
  38. BPF_LD 0x00 non-standard load operations `Load and store instructions`_
  39. BPF_LDX 0x01 load into register operations `Load and store instructions`_
  40. BPF_ST 0x02 store from immediate operations `Load and store instructions`_
  41. BPF_STX 0x03 store from register operations `Load and store instructions`_
  42. BPF_ALU 0x04 32-bit arithmetic operations `Arithmetic and jump instructions`_
  43. BPF_JMP 0x05 64-bit jump operations `Arithmetic and jump instructions`_
  44. BPF_JMP32 0x06 32-bit jump operations `Arithmetic and jump instructions`_
  45. BPF_ALU64 0x07 64-bit arithmetic operations `Arithmetic and jump instructions`_
  46. ========= ===== =============================== ===================================
  47. Arithmetic and jump instructions
  48. ================================
  49. For arithmetic and jump instructions (``BPF_ALU``, ``BPF_ALU64``, ``BPF_JMP`` and
  50. ``BPF_JMP32``), the 8-bit 'opcode' field is divided into three parts:
  51. ============== ====== =================
  52. 4 bits (MSB) 1 bit 3 bits (LSB)
  53. ============== ====== =================
  54. operation code source instruction class
  55. ============== ====== =================
  56. The 4th bit encodes the source operand:
  57. ====== ===== ========================================
  58. source value description
  59. ====== ===== ========================================
  60. BPF_K 0x00 use 32-bit immediate as source operand
  61. BPF_X 0x08 use 'src_reg' register as source operand
  62. ====== ===== ========================================
  63. The four MSB bits store the operation code.
  64. Arithmetic instructions
  65. -----------------------
  66. ``BPF_ALU`` uses 32-bit wide operands while ``BPF_ALU64`` uses 64-bit wide operands for
  67. otherwise identical operations.
  68. The 'code' field encodes the operation as below:
  69. ======== ===== ==========================================================
  70. code value description
  71. ======== ===== ==========================================================
  72. BPF_ADD 0x00 dst += src
  73. BPF_SUB 0x10 dst -= src
  74. BPF_MUL 0x20 dst \*= src
  75. BPF_DIV 0x30 dst = (src != 0) ? (dst / src) : 0
  76. BPF_OR 0x40 dst \|= src
  77. BPF_AND 0x50 dst &= src
  78. BPF_LSH 0x60 dst <<= src
  79. BPF_RSH 0x70 dst >>= src
  80. BPF_NEG 0x80 dst = ~src
  81. BPF_MOD 0x90 dst = (src != 0) ? (dst % src) : dst
  82. BPF_XOR 0xa0 dst ^= src
  83. BPF_MOV 0xb0 dst = src
  84. BPF_ARSH 0xc0 sign extending shift right
  85. BPF_END 0xd0 byte swap operations (see `Byte swap instructions`_ below)
  86. ======== ===== ==========================================================
  87. Underflow and overflow are allowed during arithmetic operations, meaning
  88. the 64-bit or 32-bit value will wrap. If eBPF program execution would
  89. result in division by zero, the destination register is instead set to zero.
  90. If execution would result in modulo by zero, for ``BPF_ALU64`` the value of
  91. the destination register is unchanged whereas for ``BPF_ALU`` the upper
  92. 32 bits of the destination register are zeroed.
  93. ``BPF_ADD | BPF_X | BPF_ALU`` means::
  94. dst_reg = (u32) dst_reg + (u32) src_reg;
  95. ``BPF_ADD | BPF_X | BPF_ALU64`` means::
  96. dst_reg = dst_reg + src_reg
  97. ``BPF_XOR | BPF_K | BPF_ALU`` means::
  98. src_reg = (u32) src_reg ^ (u32) imm32
  99. ``BPF_XOR | BPF_K | BPF_ALU64`` means::
  100. src_reg = src_reg ^ imm32
  101. Also note that the division and modulo operations are unsigned. Thus, for
  102. ``BPF_ALU``, 'imm' is first interpreted as an unsigned 32-bit value, whereas
  103. for ``BPF_ALU64``, 'imm' is first sign extended to 64 bits and the result
  104. interpreted as an unsigned 64-bit value. There are no instructions for
  105. signed division or modulo.
  106. Byte swap instructions
  107. ~~~~~~~~~~~~~~~~~~~~~~
  108. The byte swap instructions use an instruction class of ``BPF_ALU`` and a 4-bit
  109. 'code' field of ``BPF_END``.
  110. The byte swap instructions operate on the destination register
  111. only and do not use a separate source register or immediate value.
  112. The 1-bit source operand field in the opcode is used to select what byte
  113. order the operation convert from or to:
  114. ========= ===== =================================================
  115. source value description
  116. ========= ===== =================================================
  117. BPF_TO_LE 0x00 convert between host byte order and little endian
  118. BPF_TO_BE 0x08 convert between host byte order and big endian
  119. ========= ===== =================================================
  120. The 'imm' field encodes the width of the swap operations. The following widths
  121. are supported: 16, 32 and 64.
  122. Examples:
  123. ``BPF_ALU | BPF_TO_LE | BPF_END`` with imm = 16 means::
  124. dst_reg = htole16(dst_reg)
  125. ``BPF_ALU | BPF_TO_BE | BPF_END`` with imm = 64 means::
  126. dst_reg = htobe64(dst_reg)
  127. Jump instructions
  128. -----------------
  129. ``BPF_JMP32`` uses 32-bit wide operands while ``BPF_JMP`` uses 64-bit wide operands for
  130. otherwise identical operations.
  131. The 'code' field encodes the operation as below:
  132. ======== ===== ========================= ============
  133. code value description notes
  134. ======== ===== ========================= ============
  135. BPF_JA 0x00 PC += off BPF_JMP only
  136. BPF_JEQ 0x10 PC += off if dst == src
  137. BPF_JGT 0x20 PC += off if dst > src unsigned
  138. BPF_JGE 0x30 PC += off if dst >= src unsigned
  139. BPF_JSET 0x40 PC += off if dst & src
  140. BPF_JNE 0x50 PC += off if dst != src
  141. BPF_JSGT 0x60 PC += off if dst > src signed
  142. BPF_JSGE 0x70 PC += off if dst >= src signed
  143. BPF_CALL 0x80 function call
  144. BPF_EXIT 0x90 function / program return BPF_JMP only
  145. BPF_JLT 0xa0 PC += off if dst < src unsigned
  146. BPF_JLE 0xb0 PC += off if dst <= src unsigned
  147. BPF_JSLT 0xc0 PC += off if dst < src signed
  148. BPF_JSLE 0xd0 PC += off if dst <= src signed
  149. ======== ===== ========================= ============
  150. The eBPF program needs to store the return value into register R0 before doing a
  151. BPF_EXIT.
  152. Load and store instructions
  153. ===========================
  154. For load and store instructions (``BPF_LD``, ``BPF_LDX``, ``BPF_ST``, and ``BPF_STX``), the
  155. 8-bit 'opcode' field is divided as:
  156. ============ ====== =================
  157. 3 bits (MSB) 2 bits 3 bits (LSB)
  158. ============ ====== =================
  159. mode size instruction class
  160. ============ ====== =================
  161. The mode modifier is one of:
  162. ============= ===== ==================================== =============
  163. mode modifier value description reference
  164. ============= ===== ==================================== =============
  165. BPF_IMM 0x00 64-bit immediate instructions `64-bit immediate instructions`_
  166. BPF_ABS 0x20 legacy BPF packet access (absolute) `Legacy BPF Packet access instructions`_
  167. BPF_IND 0x40 legacy BPF packet access (indirect) `Legacy BPF Packet access instructions`_
  168. BPF_MEM 0x60 regular load and store operations `Regular load and store operations`_
  169. BPF_ATOMIC 0xc0 atomic operations `Atomic operations`_
  170. ============= ===== ==================================== =============
  171. The size modifier is one of:
  172. ============= ===== =====================
  173. size modifier value description
  174. ============= ===== =====================
  175. BPF_W 0x00 word (4 bytes)
  176. BPF_H 0x08 half word (2 bytes)
  177. BPF_B 0x10 byte
  178. BPF_DW 0x18 double word (8 bytes)
  179. ============= ===== =====================
  180. Regular load and store operations
  181. ---------------------------------
  182. The ``BPF_MEM`` mode modifier is used to encode regular load and store
  183. instructions that transfer data between a register and memory.
  184. ``BPF_MEM | <size> | BPF_STX`` means::
  185. *(size *) (dst_reg + off) = src_reg
  186. ``BPF_MEM | <size> | BPF_ST`` means::
  187. *(size *) (dst_reg + off) = imm32
  188. ``BPF_MEM | <size> | BPF_LDX`` means::
  189. dst_reg = *(size *) (src_reg + off)
  190. Where size is one of: ``BPF_B``, ``BPF_H``, ``BPF_W``, or ``BPF_DW``.
  191. Atomic operations
  192. -----------------
  193. Atomic operations are operations that operate on memory and can not be
  194. interrupted or corrupted by other access to the same memory region
  195. by other eBPF programs or means outside of this specification.
  196. All atomic operations supported by eBPF are encoded as store operations
  197. that use the ``BPF_ATOMIC`` mode modifier as follows:
  198. * ``BPF_ATOMIC | BPF_W | BPF_STX`` for 32-bit operations
  199. * ``BPF_ATOMIC | BPF_DW | BPF_STX`` for 64-bit operations
  200. * 8-bit and 16-bit wide atomic operations are not supported.
  201. The 'imm' field is used to encode the actual atomic operation.
  202. Simple atomic operation use a subset of the values defined to encode
  203. arithmetic operations in the 'imm' field to encode the atomic operation:
  204. ======== ===== ===========
  205. imm value description
  206. ======== ===== ===========
  207. BPF_ADD 0x00 atomic add
  208. BPF_OR 0x40 atomic or
  209. BPF_AND 0x50 atomic and
  210. BPF_XOR 0xa0 atomic xor
  211. ======== ===== ===========
  212. ``BPF_ATOMIC | BPF_W | BPF_STX`` with 'imm' = BPF_ADD means::
  213. *(u32 *)(dst_reg + off16) += src_reg
  214. ``BPF_ATOMIC | BPF_DW | BPF_STX`` with 'imm' = BPF ADD means::
  215. *(u64 *)(dst_reg + off16) += src_reg
  216. In addition to the simple atomic operations, there also is a modifier and
  217. two complex atomic operations:
  218. =========== ================ ===========================
  219. imm value description
  220. =========== ================ ===========================
  221. BPF_FETCH 0x01 modifier: return old value
  222. BPF_XCHG 0xe0 | BPF_FETCH atomic exchange
  223. BPF_CMPXCHG 0xf0 | BPF_FETCH atomic compare and exchange
  224. =========== ================ ===========================
  225. The ``BPF_FETCH`` modifier is optional for simple atomic operations, and
  226. always set for the complex atomic operations. If the ``BPF_FETCH`` flag
  227. is set, then the operation also overwrites ``src_reg`` with the value that
  228. was in memory before it was modified.
  229. The ``BPF_XCHG`` operation atomically exchanges ``src_reg`` with the value
  230. addressed by ``dst_reg + off``.
  231. The ``BPF_CMPXCHG`` operation atomically compares the value addressed by
  232. ``dst_reg + off`` with ``R0``. If they match, the value addressed by
  233. ``dst_reg + off`` is replaced with ``src_reg``. In either case, the
  234. value that was at ``dst_reg + off`` before the operation is zero-extended
  235. and loaded back to ``R0``.
  236. 64-bit immediate instructions
  237. -----------------------------
  238. Instructions with the ``BPF_IMM`` 'mode' modifier use the wide instruction
  239. encoding for an extra imm64 value.
  240. There is currently only one such instruction.
  241. ``BPF_LD | BPF_DW | BPF_IMM`` means::
  242. dst_reg = imm64
  243. Legacy BPF Packet access instructions
  244. -------------------------------------
  245. eBPF previously introduced special instructions for access to packet data that were
  246. carried over from classic BPF. However, these instructions are
  247. deprecated and should no longer be used.