5
5
eBPF Instruction Set
6
6
====================
7
7
8
+ The eBPF instruction set consists of eleven 64 bit registers, a program counter,
9
+ and 512 bytes of stack space.
10
+
8
11
Registers and calling convention
9
12
================================
10
13
@@ -18,8 +21,23 @@ The eBPF calling convention is defined as:
18
21
* R6 - R9: callee saved registers that function calls will preserve
19
22
* R10: read-only frame pointer to access stack
20
23
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.
23
41
24
42
Instruction encoding
25
43
====================
@@ -29,8 +47,8 @@ An eBPF program is a sequence of 64-bit instructions.
29
47
eBPF has two instruction encodings:
30
48
31
49
* 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.
34
52
35
53
The basic instruction encoding is as follows:
36
54
44
62
integer immediate value
45
63
46
64
offset
47
- integer offset
65
+ signed integer offset used with pointer arithmetic
48
66
49
67
src
50
- source register number
68
+ source register number (0-10)
51
69
52
70
dst
53
- destination register number
71
+ destination register number (0-10)
54
72
55
73
opcode
56
74
operation to perform
@@ -222,7 +240,7 @@ Jump instructions
222
240
223
241
Instruction class ``BPF_JMP32 `` uses 32-bit wide operands while ``BPF_JMP `` uses 64-bit wide operands for
224
242
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 :
226
244
227
245
======== ===== ============================ ============
228
246
code value description notes
@@ -235,7 +253,7 @@ The 4-bit 'code' field encodes the operation as below:
235
253
BPF_JNE 0x50 PC += offset if dst != src
236
254
BPF_JSGT 0x60 PC += offset if dst > src signed
237
255
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 `_
239
257
BPF_EXIT 0x90 function / program return BPF_JMP only
240
258
BPF_JLT 0xa0 PC += offset if dst < src unsigned
241
259
BPF_JLE 0xb0 PC += offset if dst <= src unsigned
@@ -247,6 +265,17 @@ The eBPF verifier is responsible for verifying that the
247
265
eBPF program stores the return value into register R0 before doing a
248
266
``BPF_EXIT ``.
249
267
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)
250
279
251
280
Load and store instructions
252
281
===========================
0 commit comments