Skip to content

Commit 4556801

Browse files
committed
Incorporate additional information
Looked for points made by https://docs.cilium.io/en/stable/bpf/ that were not made in the ISA doc already Signed-off-by: Dave Thaler <[email protected]>
1 parent c767814 commit 4556801

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

isa/kernel.org/instruction-set.rst

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
eBPF Instruction Set
66
====================
77

8+
The eBPF instruction set consists of eleven 64 bit registers, a program counter,
9+
and 512 bytes of stack space.
10+
811
Registers and calling convention
912
================================
1013

@@ -18,8 +21,23 @@ The eBPF calling convention is defined as:
1821
* R6 - R9: callee saved registers that function calls will preserve
1922
* R10: read-only frame pointer to access stack
2023

21-
R0 - R5 are scratch registers and eBPF programs need to spill/fill them if
22-
necessary across calls.
24+
Registers R0 - R5 are scratch registers, meaning the BPF program needs to either
25+
spill them to the BPF stack or move them to callee saved registers if these
26+
arguments are to be reused across multiple function calls. Spilling means
27+
that the value in the register is moved to the BPF stack. The reverse operation
28+
of moving the variable from the BPF stack to the register is called filling.
29+
The reason for spilling/filling is due to the limited number of registers.
30+
31+
*Linux implementation note*: In the Linux kernel, the exit value for eBPF
32+
programs is passed as a 32 bit value.
33+
34+
Upon entering execution of an eBPF program, register R1 initially contains
35+
the context for the program. The context is the input argument for the program
36+
(similar to argc/argv pair for a typical C program). Since only one register
37+
is used for the context, the context is typically a structure containing all
38+
the inputs needed. The context is defined by the program type; for example,
39+
a networking program might have a context that includes network packet data
40+
and/or metadata.
2341

2442
Instruction encoding
2543
====================
@@ -29,8 +47,8 @@ An eBPF program is a sequence of 64-bit instructions.
2947
eBPF has two instruction encodings:
3048

3149
* the basic instruction encoding, which uses 64 bits to encode an instruction
32-
* the wide instruction encoding, which appends a second 64-bit immediate value
33-
(imm64) after the basic instruction for a total of 128 bits.
50+
* the wide instruction encoding, which appends a second 64-bit immediate (i.e.,
51+
constant) value after the basic instruction for a total of 128 bits.
3452

3553
The basic instruction encoding is as follows:
3654

@@ -44,13 +62,13 @@ imm
4462
integer immediate value
4563

4664
offset
47-
integer offset
65+
signed integer offset used with pointer arithmetic
4866

4967
src
50-
source register number
68+
source register number (0-10)
5169

5270
dst
53-
destination register number
71+
destination register number (0-10)
5472

5573
opcode
5674
operation to perform
@@ -222,7 +240,7 @@ Jump instructions
222240

223241
Instruction class ``BPF_JMP32`` uses 32-bit wide operands while ``BPF_JMP`` uses 64-bit wide operands for
224242
otherwise identical operations.
225-
The 4-bit 'code' field encodes the operation as below:
243+
The 4-bit 'code' field encodes the operation as below, where PC is the program counter:
226244

227245
======== ===== ============================ ============
228246
code value description notes
@@ -235,7 +253,7 @@ The 4-bit 'code' field encodes the operation as below:
235253
BPF_JNE 0x50 PC += offset if dst != src
236254
BPF_JSGT 0x60 PC += offset if dst > src signed
237255
BPF_JSGE 0x70 PC += offset if dst >= src signed
238-
BPF_CALL 0x80 function call
256+
BPF_CALL 0x80 call function imm see `Helper functions`_
239257
BPF_EXIT 0x90 function / program return BPF_JMP only
240258
BPF_JLT 0xa0 PC += offset if dst < src unsigned
241259
BPF_JLE 0xb0 PC += offset if dst <= src unsigned
@@ -247,6 +265,17 @@ The eBPF verifier is responsible for verifying that the
247265
eBPF program stores the return value into register R0 before doing a
248266
``BPF_EXIT``.
249267

268+
Helper functions
269+
~~~~~~~~~~~~~~~~
270+
Helper functions are a concept whereby BPF programs can call into
271+
set of function calls exposed by the eBPF runtime. Each helper
272+
function is identified by an integer used in a ``BPF_CALL`` instruction.
273+
The available helper functions may differ for each eBPF program type.
274+
275+
Each helper function is implemented with a commonly shared function
276+
signature defined as:
277+
278+
uint64_t function(uint64_t r1, uint64_t r2, uint64_t r3, uint64_t r4, uint64_t r5)
250279

251280
Load and store instructions
252281
===========================

0 commit comments

Comments
 (0)