Skip to content

Commit ee932bf

Browse files
Yonghong SongAlexei Starovoitov
authored andcommitted
docs/bpf: Improve documentation for cpu=v4 instructions
Improve documentation for cpu=v4 instructions based on David's suggestions. Cc: [email protected] Suggested-by: David Vernet <[email protected]> Acked-by: David Vernet <[email protected]> Signed-off-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent d1a0235 commit ee932bf

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

Documentation/bpf/standardization/instruction-set.rst

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ BPF_MOV 0xb0 0 dst = src
174174
BPF_MOVSX 0xb0 8/16/32 dst = (s8,s16,s32)src
175175
BPF_ARSH 0xc0 0 sign extending dst >>= (src & mask)
176176
BPF_END 0xd0 0 byte swap operations (see `Byte swap instructions`_ below)
177-
======== ===== ============ ==========================================================
177+
======== ===== ======= ==========================================================
178178

179179
Underflow and overflow are allowed during arithmetic operations, meaning
180180
the 64-bit or 32-bit value will wrap. If eBPF program execution would
@@ -201,43 +201,50 @@ where '(u32)' indicates that the upper 32 bits are zeroed.
201201

202202
dst = dst ^ imm32
203203

204-
Note that most instructions have instruction offset of 0. But three instructions
205-
(BPF_SDIV, BPF_SMOD, BPF_MOVSX) have non-zero offset.
204+
Note that most instructions have instruction offset of 0. Only three instructions
205+
(``BPF_SDIV``, ``BPF_SMOD``, ``BPF_MOVSX``) have a non-zero offset.
206206

207207
The devision and modulo operations support both unsigned and signed flavors.
208-
For unsigned operation (BPF_DIV and BPF_MOD), for ``BPF_ALU``, 'imm' is first
209-
interpreted as an unsigned 32-bit value, whereas for ``BPF_ALU64``, 'imm' is
210-
first sign extended to 64 bits and the result interpreted as an unsigned 64-bit
211-
value. For signed operation (BPF_SDIV and BPF_SMOD), for ``BPF_ALU``, 'imm' is
212-
interpreted as a signed value. For ``BPF_ALU64``, the 'imm' is sign extended
213-
from 32 to 64 and interpreted as a signed 64-bit value.
214208

215-
Instruction BPF_MOVSX does move operation with sign extension.
216-
``BPF_ALU | MOVSX`` sign extendes 8-bit and 16-bit into 32-bit and upper 32-bit are zeroed.
217-
``BPF_ALU64 | MOVSX`` sign extends 8-bit, 16-bit and 32-bit into 64-bit.
209+
For unsigned operations (``BPF_DIV`` and ``BPF_MOD``), for ``BPF_ALU``,
210+
'imm' is interpreted as a 32-bit unsigned value. For ``BPF_ALU64``,
211+
'imm' is first sign extended from 32 to 64 bits, and then interpreted as
212+
a 64-bit unsigned value.
213+
214+
For signed operations (``BPF_SDIV`` and ``BPF_SMOD``), for ``BPF_ALU``,
215+
'imm' is interpreted as a 32-bit signed value. For ``BPF_ALU64``, 'imm'
216+
is first sign extended from 32 to 64 bits, and then interpreted as a
217+
64-bit signed value.
218+
219+
The ``BPF_MOVSX`` instruction does a move operation with sign extension.
220+
``BPF_ALU | BPF_MOVSX`` sign extends 8-bit and 16-bit operands into 32
221+
bit operands, and zeroes the remaining upper 32 bits.
222+
``BPF_ALU64 | BPF_MOVSX`` sign extends 8-bit, 16-bit, and 32-bit
223+
operands into 64 bit operands.
218224

219225
Shift operations use a mask of 0x3F (63) for 64-bit operations and 0x1F (31)
220226
for 32-bit operations.
221227

222228
Byte swap instructions
223-
~~~~~~~~~~~~~~~~~~~~~~
229+
----------------------
224230

225231
The byte swap instructions use instruction classes of ``BPF_ALU`` and ``BPF_ALU64``
226232
and a 4-bit 'code' field of ``BPF_END``.
227233

228234
The byte swap instructions operate on the destination register
229235
only and do not use a separate source register or immediate value.
230236

231-
For ``BPF_ALU``, the 1-bit source operand field in the opcode is used to select what byte
232-
order the operation convert from or to. For ``BPF_ALU64``, the 1-bit source operand
233-
field in the opcode is not used and must be 0.
237+
For ``BPF_ALU``, the 1-bit source operand field in the opcode is used to
238+
select what byte order the operation converts from or to. For
239+
``BPF_ALU64``, the 1-bit source operand field in the opcode is reserved
240+
and must be set to 0.
234241

235242
========= ========= ===== =================================================
236243
class source value description
237244
========= ========= ===== =================================================
238245
BPF_ALU BPF_TO_LE 0x00 convert between host byte order and little endian
239246
BPF_ALU BPF_TO_BE 0x08 convert between host byte order and big endian
240-
BPF_ALU64 BPF_TO_LE 0x00 do byte swap unconditionally
247+
BPF_ALU64 Reserved 0x00 do byte swap unconditionally
241248
========= ========= ===== =================================================
242249

243250
The 'imm' field encodes the width of the swap operations. The following widths
@@ -305,9 +312,12 @@ where 's>=' indicates a signed '>=' comparison.
305312

306313
where 'imm' means the branch offset comes from insn 'imm' field.
307314

308-
Note there are two flavors of BPF_JA instrions. BPF_JMP class permits 16-bit jump offset while
309-
BPF_JMP32 permits 32-bit jump offset. A >16bit conditional jmp can be converted to a <16bit
310-
conditional jmp plus a 32-bit unconditional jump.
315+
Note that there are two flavors of ``BPF_JA`` instructions. The
316+
``BPF_JMP`` class permits a 16-bit jump offset specified by the 'offset'
317+
field, whereas the ``BPF_JMP32`` class permits a 32-bit jump offset
318+
specified by the 'imm' field. A > 16-bit conditional jump may be
319+
converted to a < 16-bit conditional jump plus a 32-bit unconditional
320+
jump.
311321

312322
Helper functions
313323
~~~~~~~~~~~~~~~~
@@ -385,7 +395,7 @@ instructions that transfer data between a register and memory.
385395
dst = *(unsigned size *) (src + offset)
386396

387397
Where size is one of: ``BPF_B``, ``BPF_H``, ``BPF_W``, or ``BPF_DW`` and
388-
'unsigned size' is one of u8, u16, u32 and u64.
398+
'unsigned size' is one of u8, u16, u32 or u64.
389399

390400
The ``BPF_MEMSX`` mode modifier is used to encode sign-extension load
391401
instructions that transfer data between a register and memory.
@@ -395,7 +405,7 @@ instructions that transfer data between a register and memory.
395405
dst = *(signed size *) (src + offset)
396406

397407
Where size is one of: ``BPF_B``, ``BPF_H`` or ``BPF_W``, and
398-
'signed size' is one of s8, s16 and s32.
408+
'signed size' is one of s8, s16 or s32.
399409

400410
Atomic operations
401411
-----------------

0 commit comments

Comments
 (0)