Section 2.8

Opcode

Full name: Operation code.

Definition

An opcode, short for operation code, is the part of a machine instruction that says which operation to perform, such as add, load, jump or call. The remaining parts of the instruction, called operands, say what the operation is performed on. In the instruction set eBPF §2.1 (extended Berkeley Packet Filter) the opcode is the first byte of every instruction, so it can take 256 different values. A decoder, the code that reads an instruction, looks at the opcode first, because the opcode determines how every other field is read.

The idea

A processor, or a virtual machine §2.3 (a computer defined in software), receives numbers, not words. A register is one of the machine’s numbered storage locations. The instruction “put 42 into register 0” contains three things: the action, which is to put a constant into a register; the place, register 0; and the constant, 42. The opcode is the number that stands for the action. The 42 is data for the action. It is not the action.

People do not write opcodes. They write assembly, in which the instruction above reads r0 = 42, or uses a short name, called a mnemonic, such as MOV for move. An assembler turns that text into the opcode and the operand fields. A disassembler turns the bytes back into text. One operation therefore has three faces: a mnemonic for people, an opcode in the bytes, and a behavior in the machine.

Think of the opcode as a row number in the machine’s table of operations. With one byte there are 256 rows, and not every row is filled. What the machine does when a program names an empty row is a safety question.

How it works

RFC 9669, the specification of the eBPF instruction set, fixes the instruction encoding §2.7, which is the layout of every instruction in bits. It splits the opcode byte into smaller fields.

A decoder handles one instruction in four steps.

  1. Read the opcode byte.

  2. Look it up. If the runtime, the software that executes the program, implements no instruction with this opcode, stop and refuse.

  3. Read the other fields in the way this opcode prescribes, and check that the fields it does not use are zero.

  4. Hand the operands to the piece of code that performs the operation, which is called the handler.

An interpreter §2.13, the engine that carries out instructions one at a time, often does step 2 with a dispatch table §2.39: an array §14.5 with one entry for each of the 256 opcode values, where each entry leads to a handler.

The lookup in step 2 can fail in three ways. An unknown opcode is a value to which the specification assigns no instruction. A reserved encoding is one that the specification explicitly sets aside: it names the bits and forbids programs to use them. An unsupported opcode is defined by the specification and not implemented by this runtime. RFC 9669 allows that case: it sorts the instructions into named conformance groups, requires every runtime to implement the group base32, and makes the other groups optional.

Worked example

The 8 bytes b7 00 00 00 2a 00 00 00 are one eBPF instruction, written in hexadecimal for a little-endian §4.21 machine, which stores the lowest byte of a number first.

The instruction means r0 = 42. If R0 held 9 before, it holds 42 afterwards.

Keep the last 7 bytes and change only the opcode.

One byte changed each time, and the meaning of the other seven bytes changed with it.

Stated precisely

In the arithmetic and jump classes the opcode byte is opcode=16×code+8×source+class,\mathrm{opcode} = 16 \times \mathrm{code} + 8 \times \mathrm{source} + \mathrm{class}, where code\mathrm{code} is a number from 0 to 15, source\mathrm{source} is 0 or 1, and class\mathrm{class} is 4, 5, 6 or 7. In the load and store classes it is opcode=32×mode+8×size+class,\mathrm{opcode} = 32 \times \mathrm{mode} + 8 \times \mathrm{size} + \mathrm{class}, where mode\mathrm{mode} is a number from 0 to 7, size\mathrm{size} is a number from 0 to 3, and class\mathrm{class} is 0, 1, 2 or 3. For example, the opcode 0x18, which loads a 64-bit constant, is 32×0+8×3+0=2432 \times 0 + 8 \times 3 + 0 = 24.

RFC 9669 defines an instruction for 125 of the 256 opcode values. One further value, 0x00, is not an instruction: it marks the second half of a 16-byte instruction. For the other 256−126=130256 - 126 = 130 values RFC 9669 defines no instruction. Appendix A of RFC 9669, its table of instructions, shows only 123 of these 126 values. It leaves out the three load instructions of section 5.2, which read a small signed number from memory and widen it to 64 bits; their opcodes are 0x81, 0x89 and 0x91. A list of valid opcodes copied from Appendix A alone is therefore incomplete.

RFC 9669 does not define what a machine does when it is asked to execute an unknown or reserved encoding. Its section on security says that a verifier, the checker that examines a program before it runs, is responsible for making sure that programs “don’t use instructions with undefined behavior”. The safe contract for a runtime has two parts.

  1. At load time. Decode every instruction of the program. Refuse the whole program if any opcode is outside the set that this runtime implements, or if any field breaks the rules of its opcode.

  2. At run time. If execution nevertheless reaches an opcode without a handler, stop the program and report an error. Never skip the instruction and continue, and never treat it as some other instruction.

Limits and common mistakes

The opcode is not the whole identity of an instruction. Several instructions share one opcode and differ in another field. Opcode 0x18 has seven variants, selected by the source register field. Opcode 0x85, call, has three. A decoder that checks only the opcode accepts encodings that are not instructions.

A range test is not a membership test. The largest opcode in RFC 9669 is 0xde, and many smaller values, such as 0x01 and 0x08, are unknown. A test of the form “the opcode is at most 0xde” lets them through.

A fallback that does nothing is the classic mistake. If the interpreter’s branch for an unlisted opcode simply moves on to the next instruction, a malformed program runs with holes in it, and what it does no longer matches what any checker examined.

Opcode values belong to one instruction set. The same byte means something else, or nothing, in another instruction set.

Questions answered

What happens for unknown, reserved, or unsupported opcodes?

A correct runtime refuses them. It rejects the whole program when the program is loaded, and if such an opcode is reached during execution anyway, it stops the program with an error. It never skips the instruction and never guesses a meaning. The opcode is the part of an instruction that selects the operation, and a runtime is the software that executes a program. In eBPF (extended Berkeley Packet Filter) the opcode is one byte.

The three words name three cases. An unknown opcode has no instruction assigned by the specification: RFC 9669, the specification of the eBPF instruction set, defines no instruction for 130 of the 256 byte values. A reserved encoding is set aside by the specification and must not be used. An unsupported opcode is defined, but belongs to an optional group of instructions that this runtime does not implement. A runtime treats all three in the same way, because in all three cases it has no correct behavior to offer.

RFC 9669 itself does not say what happens. It leaves the matter to the runtime and expects a verifier, a checker that examines a program before it runs, to keep such instructions out. Real runtimes follow the two-part rule. The Linux verifier rejects a program that contains an opcode it does not know with the message unknown opcode. The Linux interpreter first fills all 256 entries of its dispatch table, the array that leads from an opcode to its handler, with an entry that treats the opcode as a fatal error, and then enters the instructions it implements. uBPF §2.16 (userspace eBPF), a second runtime, likewise rejects an unknown opcode when the program is loaded, and its interpreter ends the run with an error if it meets one.

Which part of r0 = 42 is the opcode?

No part of the text is the opcode itself. The text r0 = 42 is assembly notation: r0 and 42 are the two operands, the destination register R0 and the constant 42, and the sign = between a register and a constant stands for the operation “move a constant into a register”. The opcode is the number that encodes this operation. In the bytes that the instruction set eBPF uses for this instruction, b7 00 00 00 2a 00 00 00, the first byte, b7, is the opcode. The second byte names register 0, and the last four bytes hold 42. An assembler chooses the opcode from the form of the line, and a disassembler prints the line back from the bytes.

Related entries: Instruction encoding (§2.7), ALU32 versus ALU64 (§2.11), Dispatch table (§2.39), Undefined versus unsupported semantics (§3.35), Endianness (§4.21), Structural bytecode admission (§2.26), Wide immediate instruction (§2.10), Bit operations: truth tables, masks, shifts, and rotations (§4.50). Further reading: [1, 3, 160].