Section 2.6

eBPF register convention

Full name: Register convention of the extended Berkeley Packet Filter (eBPF).

Definition

The eBPF register convention is the agreed division of work among the eleven registers (numbered 64-bit storage locations) of a program for eBPF §2.1, an instruction set for small programs that run inside a host program. R0 carries the result of a function call, R1 to R5 carry its arguments, R6 to R9 keep their values across a call, and R10 holds the address of the program’s stack (its memory for temporary data) and is read-only. A Linux kernel document recommends this convention [303]; RFC 9669, the instruction set specification, omits it.

The idea

A program is in the middle of a computation and calls a helper function §2.19, which is a function of the host, the software that runs the program, for example to read some bytes. The program and the helper function work with the same eleven registers. Without an agreement made in advance, the program does not know where to put the arguments, where the answer will appear, or which of its values will still be there afterwards. An agreement of this kind is called a calling convention.

Two words are needed. The caller is the code that makes a call. The callee is the function that is called. The convention sorts the registers by who must protect their content. R6 to R9 are callee-saved: the callee must hand them back unchanged, so the caller can keep long-lived values in them. R0 to R5 are caller-saved, also called scratch registers: the callee may leave anything in them, so a caller that still needs one of those values must copy it to a safe place before the call.

Picture a service counter. R1 to R5 are the counter on which the request is handed over. R0 is the tray on which the reply comes back. R6 to R9 are the customer’s own pockets. Whatever was left on the counter may be gone afterwards. The picture stops being accurate at one point: nothing physical guards the pockets. R6 to R9 keep their values only because the callee is written to restore them.

How it works

The convention gives each register one role.

A call of a helper function under the convention has five steps.

  1. The caller moves every value it will need later into R6 to R9, or stores it in stack memory.

  2. The caller puts the arguments in R1 to R5.

  3. The caller executes the call instruction, which names the helper function by a number.

  4. The helper function runs as code of the host.

  5. Execution continues after the call instruction. R0 holds the result. R6 to R9 are as they were. R1 to R5 must be treated as lost.

The same roles frame the program as a whole. On Linux, the host places a pointer to the program’s input in R1 before the first instruction, and the program must place its result in R0 before its exit instruction.

The roles were chosen to make calls cheap. A JIT §2.14 (just-in-time) compiler translates an eBPF program into instructions of the real processor. The Linux documentation pairs each eBPF register with a register of a 64-bit x86 processor that has the same role in that processor’s own calling convention, so an eBPF call becomes a single call instruction of the processor.

On Linux the eBPF verifier §3.3, the checker that examines a program before it runs, enforces the caller’s side. After every call it marks R1 to R5 as unreadable and gives R0 the type of the helper function’s return value. It rejects a program that reads R1 after a call without writing it again, and it rejects a program that writes to R10.

Worked example

The following program is adapted from the Linux documentation. It calls two helper functions, here named foo and bar, passes the program’s input to each as the first argument, and returns the sum of their results. Suppose the input value is 500, foo returns 11 and bar returns 7. A dash marks a register whose content is unspecified.

step instruction R0 R1 R2 R6 R7
(start) – 500 – – –
1 r6 = r1 – 500 – 500 –
2 r2 = 2 – 500 2 500 –
3 call foo 11 – – 500 –
4 r7 = r0 11 – – 500 11
5 r1 = r6 11 500 – 500 11
6 r2 = 6 11 500 6 500 11
7 call bar 7 – – 500 11
8 r0 += r7 18 – – 500 11
9 exit 18 – – 500 11

Step 1 saves the input in R6, because after the call in step 3 the content of R1 is unspecified. Step 4 saves the first result in R7, because the call in step 7 overwrites R0. Step 5 rebuilds the first argument from R6. Step 8 computes 7+11=187 + 11 = 18, and the program returns 18. Without steps 1 and 4 the program would read registers whose content is unspecified.

The Linux documentation also gives a short wrong program: r1 = 1, call foo, r0 = r1, exit. It reads R1 after the call. The documentation says that after a call R1 to R5 “contain junk values and cannot be read”, and the verifier rejects the program. With R6 in place of R1 in both instructions the program is correct and returns 1.

Stated precisely

For each register, let its old value be the value it holds immediately before a call instruction. Under the convention, the following holds when execution continues after the call.

  1. R6, R7, R8 and R9 each hold their old value.

  2. R10 holds its old value.

  3. R0 holds the return value of the callee.

  4. The content of R1 to R5 is unspecified. The caller must not read any of them before writing it again.

The callee, which is a helper function or another function of the same program, receives its first argument in R1, its second in R2, and so on up to its fifth in R5. For the program as a whole, the result is the value of R0 at the exit instruction.

The guarantee rests on three assumptions. First, the runtime, meaning the software that executes the program, has adopted this convention. RFC 9669 does not require it: the convention is published in the Linux kernel documentation as a recommendation, and a runtime of another design must state which convention it follows. Second, the callee keeps its side. R6 to R9 survive only because the engine that executes the program, which is an interpreter §2.13 or the output of a JIT compiler, and the native code of the helper function preserve them. Third, the caller keeps its side, which is rule 4. Either a verifier enforces rule 4, or nothing does.

Limits and common mistakes

A preserved value is not a preserved meaning. Suppose R6 holds the address of an object, and the helper function frees or moves that object. After the call R6 holds the same number, and the number now points at the wrong memory. Whether a pointer, meaning a value used as an address, is still usable at the moment of use is the subject of temporal memory safety §2.29. On Linux, when a helper function can move the data of a network packet, the verifier afterwards no longer accepts any register that held a pointer into the packet as such a pointer, R6 to R9 included. The program must obtain the pointers again before it uses them.

A test cannot establish what the convention leaves unspecified. An interpreter, an engine that carries out the instructions one at a time in software, may happen to leave R1 to R5 untouched across a call. Under a JIT compiler the same registers are real processor registers, which the native code of the helper function is free to overwrite. A program that reads R1 after a call can pass on one engine and fail on another, and what it reads can be leftover data of the host, such as an address.

The convention says where values travel. It does not say what a helper function may do with them. Which memory a helper function may touch, and what its return value means, including values that signal an error, belong to the helper contract §2.21, the stated rules of each helper function.

Only five registers carry arguments. A caller with more data to pass puts the data in memory and passes its address.

Questions answered

Which register values remain valid after a helper call?

Under the eBPF register convention, R6, R7, R8 and R9 keep the values they had before the call, R10 is unchanged, and R0 holds the return value of the helper function. R1 to R5, which carried the arguments, are unspecified after the call: the program must write them again before it reads them. R0 to R10 are the eleven registers, the numbered storage locations, of a program for eBPF (extended Berkeley Packet Filter), an instruction set for small programs that run inside a host program, and a helper function is a function of that host which the program may call. On Linux the verifier, which checks a program before it runs, enforces this rule by marking R1 to R5 unreadable after every call.

For a pointer, valid has a second condition. A register among R6 to R9 keeps its number, but the number is a usable address only while the object it points to still exists at that place. If the helper function freed or moved the object, the unchanged register is no longer a valid pointer.

All of this holds only for a runtime that follows the convention. RFC 9669, the specification of the eBPF instruction set, does not prescribe it.

Related entries: Helper contract (§2.21), Temporal memory safety (§2.29), Helper function (§2.19), VM stack (§2.25), Guest-host ABI (§2.37), eBPF verifier (§3.3). Further reading: [1, 2, 160, 303].