Section 2.11

ALU32 versus ALU64

Full name: 32-bit arithmetic logic unit instructions (ALU32) versus 64-bit arithmetic logic unit instructions (ALU64).

Definition

ALU32 and ALU64 are the two classes of arithmetic and logic instructions in eBPF §2.1 (extended Berkeley Packet Filter), an instruction set with 64-bit registers. ALU stands for arithmetic logic unit, the part of a processor that computes. An ALU64 instruction computes with all 64 bits of its operands, the values it works on, and writes a 64-bit result. An ALU32 instruction, except a 64-bit byte swap, computes with the low 32 bits, writes the result into the low half of the destination register and sets the high half to zero.

The idea

Register R0 holds 4,294,967,295, in hexadecimal 0x00000000ffffffff, and the next instruction adds 1. The sum, 4,294,967,296, needs 33 bits. An ALU64 addition keeps it; an ALU32 addition keeps only the low 32 bits, all zeros, so R0 becomes 0 and a following test “is R0 zero?” takes the other branch. The width of a computation belongs to the instruction, not to the register.

Picture the register as a row of 64 switches in two halves. An ALU64 instruction works on the whole row. An ALU32 instruction works on the low half as if the high half did not exist, then turns the whole high half off. The 32-bit class is useful because C programs use 32-bit types heavily, because a 32-bit processor needs two instructions for a 64-bit addition, and because x86-64 and 64-bit Arm processors clear a register’s upper half whenever they write its 32-bit form, so an ALU32 instruction becomes one processor instruction there.

How it works

The opcode §2.8, the first byte of every eBPF instruction, ends in a 3-bit class field: class 4, which RFC 9669 calls ALU, is ALU32, and class 7, ALU64, is ALU64. The remaining bits choose the operation in the same way in both, so adding a constant is opcode 0x04 in ALU32 and 0x07 in ALU64, and adding a register is 0x0c and 0x0f. Assembly notation writes w0 += 1 for the ALU32 form, with w0 naming the low 32 bits of register R0, and r0 += 1 for the ALU64 form.

An ALU32 instruction works in three steps.

  1. Take the low 32 bits of the destination register and of the source operand, which is a second register or the 32-bit constant inside the instruction, called the immediate.

  2. Compute on 32 bits. A result that needs more bits wraps around: only its low 32 bits are kept. This is unsigned modular arithmetic §4.16 with modulus 2322^{32}.

  3. Write the result into the low half of the destination and set the high half to zero. Filling new high bits with zeros is called zero extension §4.19.

An ALU64 instruction computes on all 64 bits, modulo 2642^{64}. Its immediate is still 32 bits long, so the immediate is first sign-extended: its highest bit is copied into 32 new high bits, which keeps its signed value. The constant −1-1, stored as 0xffffffff, becomes 0xffffffffffffffff, which is −1-1 as a signed 64-bit number.

RFC 9669 adds rules for a few operations.

Worked example

Registers are shown as 16 hexadecimal digits, which is 64 bits. Case 1, a carry. R0 holds 0x00000000ffffffff, which is 232−1=4,294,967,2952^{32} - 1 = 4{,}294{,}967{,}295. The instruction w0 += 1, bytes 04 00 00 00 01 00 00 00, adds in the low half: 4,294,967,295+1=2324{,}294{,}967{,}295 + 1 = 2^{32}. The remainder after division by 2322^{32} is 0, so R0 becomes 0. The instruction r0 += 1, bytes 07 00 00 00 01 00 00 00, keeps the whole sum, because 232<2642^{32} < 2^{64}, so R0 becomes 0x0000000100000000. A following if r0 == 0 jump is taken only after the ALU32 addition.

Case 2, old high bits. R1 holds 0xffffffff00000005. w1 += 1 computes 5+1=65 + 1 = 6 in the low half and clears the high half, so R1 becomes 6. r1 += 1 gives 0xffffffff00000006. An interpreter §2.13 that forgot to clear the high half would also give 0xffffffff00000006 for w1 += 1, and a test that inspects only the low half, 6 either way, would not notice.

Case 3, a negative result. R2 holds 0. w2 -= 1 computes 0−10 - 1, which wraps to 232−12^{32} - 1 on 32 bits. Zero extension makes R2 equal to 0x00000000ffffffff, the 64-bit number 4,294,967,295, not −1-1. r2 -= 1 gives 0xffffffffffffffff, which is −1-1 as a signed 64-bit number. Added to a memory address, the ALU64 result moves the address back by 1 byte; the ALU32 result moves it forward by 4,294,967,295 bytes, almost 4 GiB.

Stated precisely

Let xx be the value of the destination register before the instruction and yy the value of the source operand, each read as an unsigned 64-bit integer; an immediate is first sign-extended to 64 bits. Write amodma \bmod m for the remainder of aa after division by mm, a number from 0 to m−1m - 1, and lo(v)=vmod232\mathrm{lo}(v) = v \bmod 2^{32} for the low 32 bits of vv. For addition, the new value x′x' of the destination is ALU64: x′=(x+y)mod264,ALU32: x′=(lo(x)+lo(y))mod232.\text{ALU64: } x' = (x + y) \bmod 2^{64}, \qquad \text{ALU32: } x' = (\mathrm{lo}(x) + \mathrm{lo}(y)) \bmod 2^{32}. Subtraction, multiplication and the bitwise operations §4.50 AND, OR and XOR (exclusive or) follow the same pattern. Every ALU32 result, except that of a 64-bit byte swap, satisfies 0≤x′<2320 \le x' < 2^{32}, which says that its high 32 bits are zero.

For these six operations the ALU32 result is the low half of the ALU64 result, because the low 32 bits of a sum, difference, product or bitwise result depend only on the low 32 bits of the inputs; computing in 64 bits and keeping the low half is correct for them. For shifts, division and modulo it is wrong. With xx equal to 0x0000000100000000, a 64-bit right shift by 1 gives 0x0000000080000000, low half 0x80000000, but the ALU32 shift starts from lo(x)=0\mathrm{lo}(x) = 0 and gives 0. With x=1x = 1 and a left shift by 40, ALU32 shifts by 40mod32=840 \bmod 32 = 8 and gives 256, but the low half of 2402^{40} is 0.

These equations are the definitions in RFC 9669. An interpreter, or a JIT §2.14 (just-in-time) compiler, which translates the program into processor instructions just before it runs, is correct only if it produces the same 64 bits for every input.

Limits and common mistakes

In C, implement ALU32 with unsigned arithmetic, which wraps by definition, and convert each result to uint32_t, keeping its low 32 bits. Signed integer overflow §4.15 is undefined behavior §4.12 in C, so wrap-around in int32_t cannot be relied on, even when it seems to work. Declaring the operands uint32_t does not by itself make the arithmetic unsigned, because of integer promotion §4.18: before arithmetic, C converts a uint32_t operand to the signed type int whenever int can hold every uint32_t value. With the usual compilers for x86-64 and Arm processors, int has 32 bits, cannot hold every such value, and the arithmetic stays unsigned. With a 64-bit int, the operands become signed, and 𝟶𝚡𝚏𝚏𝚏𝚏𝚏𝚏𝚏𝚏×𝟶𝚡𝚏𝚏𝚏𝚏𝚏𝚏𝚏𝚏=264−233+1\texttt{0xffffffff} \times \texttt{0xffffffff} = 2^{64} - 2^{33} + 1 exceeds the largest int, 263−12^{63} - 1: a signed overflow. Writing 1u * a * b instead of a * b, where 1u is the constant 1 of type unsigned int, keeps the arithmetic unsigned on every machine.

A verifier, a checker that examines a program before it runs, relies on the zero high half. The Linux eBPF verifier §3.3 tracks bounds for the whole register and for its low 32 bits and records a zero high half after a 32-bit arithmetic result, so an interpreter or compiler that leaves old high bits runs a program the verifier never approved. The verifier itself must apply the rule correctly: a verifier that derives wrong bounds for 32-bit instructions can accept programs that reach memory out of bounds.

Jumps split the same way: class JMP compares all 64 bits, and class JMP32 only the low 32. ALU32 does not make a register narrower; a later ALU64 instruction reads all 64 bits, zeros included.

Questions answered

What happens to the upper register bits after a 32-bit operation?

After an ordinary 32-bit (ALU32) operation, the upper 32 bits of the 64-bit destination register are set to zero. This is the rule of eBPF (extended Berkeley Packet Filter), an instruction set with 64-bit registers and two classes of arithmetic logic unit (ALU) instructions, ALU32 and ALU64. An ALU32 instruction computes on the low 32 bits of its operands, writes the 32-bit result into the low half of the destination and sets the high half to zero, which is called zero extension. So 0x00000000ffffffff plus 1 gives 0, and 0xffffffff00000005 plus 1 gives 6.

The high half is set to zero even when the 32-bit result is negative as a signed number: 0−10 - 1 in ALU32 gives 0x00000000ffffffff, the 64-bit number 4,294,967,295, not the 64-bit −1-1. The ALU32 sign-extending move and ALU32 modulo by zero also leave a zero high half. The one exception in the ALU32 class is a 64-bit byte swap, an instruction that converts all 8 bytes of the register between byte orders; apart from it, the only arithmetic instructions that write all 64 bits from their own result are the ALU64 instructions.

Related entries: Unsigned modular arithmetic (§4.16), Interpreter semantic fidelity (§2.38), Signed versus unsigned comparison (§2.12), Truncation and extension (§4.19), Signed integer overflow (§4.15), Opcode (§2.8). Further reading: [1, 160].