Skip to contentSkip to conversation

bpf, docs: Add docs on extended 64-bit immediate instructions

PATCH Superseded Dave Thaler 2 participants 3 messages Mar 25, 2023 – Mar 26, 2023

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 bpf, docs: Add docs on extended 64-bit immediate instructions

From: Dave Thaler <[email protected]>

Add docs on extended 64-bit immediate instructions, including six instructions previously undocumented. Include a brief description of map objects, and variables, as used by those instructions.

---
V1 -> V2: rebased on top of latest master

V2 -> V3: addressed comments from Alexei

---
 Documentation/bpf/instruction-set.rst | 56 +++++++++++++++++++++++----
 Documentation/bpf/linux-notes.rst     | 13 +++++++
 2 files changed, 61 insertions(+), 8 deletions(-)
Documentation/bpf/instruction-set.rst +48 -8
index db8789e6969..2c8347d63e7 100644
@@ -385,14 +385,54 @@ and loaded back to ``R0``.
 -----------------------------
 
 Instructions with the ``BPF_IMM`` 'mode' modifier use the wide instruction
-encoding for an extra imm64 value.
-
-There is currently only one such instruction.
-
-``BPF_LD | BPF_DW | BPF_IMM`` means::
-
-  dst = imm64
-
+encoding defined in `Instruction encoding`_, and use the 'src' field of the
+basic instruction to hold an opcode subtype.
+
+The following instructions are defined, and use additional concepts defined below:
+
+=========================  ======  ===  =========================================  ===========  ==============
+opcode construction        opcode  src  pseudocode                                 imm type     dst type
+=========================  ======  ===  =========================================  ===========  ==============
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x0  dst = imm64                                integer      integer
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x1  dst = map_by_fd(imm)                       map fd       map
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x2  dst = map_val(map_by_fd(imm)) + next_imm   map fd       data pointer
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x3  dst = var_addr(imm)                        variable id  data pointer
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x4  dst = code_addr(imm)                       integer      code pointer
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x5  dst = map_by_idx(imm)                      map index    map
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x6  dst = map_val(map_by_idx(imm)) + next_imm  map index    data pointer
+=========================  ======  ===  =========================================  ===========  ==============
+
+where
+
+* map_by_fd(imm) means to convert a 32-bit POSIX file descriptor into an address of a map object (see `Map objects`_)
+* map_by_idx(imm) means to convert a 32-bit index into an address of a map object
+* map_val(map) gets the address of the first value in a given map object
+* var_addr(imm) gets the address of a platform variable (see `Platform Variables`_) with a given id
+* code_addr(imm) gets the address of the instruction at a specified relative offset in number of (64-bit) instructions
+* the 'imm type' can be used by disassemblers for display
+* the 'dst type' can be used for verification and JIT compilation purposes
+
+Map objects
+~~~~~~~~~~~
+
+Maps are shared memory regions accessible by eBPF programs on some platforms, where we use the term "map object"
+to refer to an object containing the data and metadata (e.g., size) about the memory region.
+A map can have various semantics as defined in a separate document, and may or may not have a single
+contiguous memory region, but the 'map_val(map)' is currently only defined for maps that do have a single
+contiguous memory region.
+
+Each map object can have a POSIX file descriptor (fd) if supported by the platform,
+where 'map_by_fd(imm)' means to get the map with the specified file descriptor.
+Each BPF program can also be defined to use a set of maps associated with the program
+at load time, and 'map_by_idx(imm)' means to get the map with the given index in the set
+associated with the BPF program containing the instruction.
+
+Platform Variables
+~~~~~~~~~~~~~~~~~~
+
+Platform variables are memory regions, identified by integer ids, exposed by the runtime and accessible by BPF programs on
+some platforms.  The 'var_addr(imm)' operation means to get the address of the memory region
+identified by the given id.
 
 Legacy BPF Packet access instructions
 -------------------------------------
Documentation/bpf/linux-notes.rst +13 -0
index 956b0c86699..2d161467105 100644
@@ -12,6 +12,19 @@ Byte swap instructions
 
 ``BPF_FROM_LE`` and ``BPF_FROM_BE`` exist as aliases for ``BPF_TO_LE`` and ``BPF_TO_BE`` respectively.
 
+Map objects
+===========
+
+Linux only supports the 'map_val(map)' operation on array maps with a single element.
+
+Linux uses an fd_array to store maps associated with a BPF program. Thus,
+map_by_index(index) uses the fd at that index in the array.
+
+Variables
+=========
+
+Linux uses BTF ids to identify variables.
+
 Legacy BPF Packet access instructions
 =====================================
 
Signature · 1 line
2.33.4
#1 lore raw
Quoted 27 lines · Dave Thaler

From: Dave Thaler <[email protected]>

Add docs on extended 64-bit immediate instructions, including six instructions previously undocumented. Include a brief description of map objects, and variables, as used by those instructions.

---
V1 -> V2: rebased on top of latest master

V2 -> V3: addressed comments from Alexei

---
 Documentation/bpf/instruction-set.rst | 56 +++++++++++++++++++++++----
 Documentation/bpf/linux-notes.rst     | 13 +++++++
 2 files changed, 61 insertions(+), 8 deletions(-)
Documentation/bpf/instruction-set.rst +0 -2
index db8789e6969..2c8347d63e7 100644
@@ -385,14 +385,54 @@ and loaded back to ``R0``.
 -----------------------------
 
 Instructions with the ``BPF_IMM`` 'mode' modifier use the wide instruction
-encoding for an extra imm64 value.
-

Documentation/bpf/instruction-set.rst and loaded back to ``R0``. · line 391

Quoted 10 lines
Documentation/bpf/instruction-set.rst +4 -6
-There is currently only one such instruction.
-
-``BPF_LD | BPF_DW | BPF_IMM`` means::
-
-  dst = imm64
-
+encoding defined in `Instruction encoding`_, and use the 'src' field of the
+basic instruction to hold an opcode subtype.
+
+The following instructions are defined, and use additional concepts defined below:

nit: Perhaps this is a bit clearer? Wdyt?

The following opcode subtypes are defined for BPF_IMM | BPF_DQ | BPF_LD instructions, with new terms such as "map" defined further below:

Quoted 57 lines
Documentation/bpf/instruction-set.rst +44 -0
+
+=========================  ======  ===  =========================================  ===========  ==============
+opcode construction        opcode  src  pseudocode                                 imm type     dst type
+=========================  ======  ===  =========================================  ===========  ==============
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x0  dst = imm64                                integer      integer
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x1  dst = map_by_fd(imm)                       map fd       map
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x2  dst = map_val(map_by_fd(imm)) + next_imm   map fd       data pointer
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x3  dst = var_addr(imm)                        variable id  data pointer
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x4  dst = code_addr(imm)                       integer      code pointer
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x5  dst = map_by_idx(imm)                      map index    map
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x6  dst = map_val(map_by_idx(imm)) + next_imm  map index    data pointer
+=========================  ======  ===  =========================================  ===========  ==============
+
+where
+
+* map_by_fd(imm) means to convert a 32-bit POSIX file descriptor into an address of a map object (see `Map objects`_)
+* map_by_idx(imm) means to convert a 32-bit index into an address of a map object
+* map_val(map) gets the address of the first value in a given map object
+* var_addr(imm) gets the address of a platform variable (see `Platform Variables`_) with a given id
+* code_addr(imm) gets the address of the instruction at a specified relative offset in number of (64-bit) instructions
+* the 'imm type' can be used by disassemblers for display
+* the 'dst type' can be used for verification and JIT compilation purposes
+
+Map objects
+~~~~~~~~~~~
+
+Maps are shared memory regions accessible by eBPF programs on some platforms, where we use the term "map object"
+to refer to an object containing the data and metadata (e.g., size) about the memory region.
+A map can have various semantics as defined in a separate document, and may or may not have a single
+contiguous memory region, but the 'map_val(map)' is currently only defined for maps that do have a single
+contiguous memory region.
+
+Each map object can have a POSIX file descriptor (fd) if supported by the platform,
+where 'map_by_fd(imm)' means to get the map with the specified file descriptor.
+Each BPF program can also be defined to use a set of maps associated with the program
+at load time, and 'map_by_idx(imm)' means to get the map with the given index in the set
+associated with the BPF program containing the instruction.
+
+Platform Variables
+~~~~~~~~~~~~~~~~~~
+
+Platform variables are memory regions, identified by integer ids, exposed by the runtime and accessible by BPF programs on
+some platforms.  The 'var_addr(imm)' operation means to get the address of the memory region
+identified by the given id.
 
 Legacy BPF Packet access instructions
 -------------------------------------
Documentation/bpf/linux-notes.rst +2 -0
index 956b0c86699..2d161467105 100644
@@ -12,6 +12,19 @@ Byte swap instructions
 
 ``BPF_FROM_LE`` and ``BPF_FROM_BE`` exist as aliases for ``BPF_TO_LE`` and ``BPF_TO_BE`` respectively.
 
+Map objects
+===========

Documentation/bpf/linux-notes.rst Byte swap instructions · line 26

Quoted 10 lines
Documentation/bpf/linux-notes.rst +10 -0
+
+Linux only supports the 'map_val(map)' operation on array maps with a single element.
+
+Linux uses an fd_array to store maps associated with a BPF program. Thus,
+map_by_index(index) uses the fd at that index in the array.
+
+Variables
+=========
+
+Linux uses BTF ids to identify variables.

Not quite sure exactly what this means. Linux uses BTF ids to identify _types_, right? This doesn't seem like something that needs to be specified as Linux specific either, even if it's not yet supported elsewhere. Certain legacy things such as Linux-specific helpers make sense, but not sure about BTF ids.

Quoted 11 lines
Documentation/bpf/linux-notes.rst +1 -0
+
 Legacy BPF Packet access instructions
 =====================================
 
Signature · 6 lines
2.33.4

-- 
Bpf mailing list
[email protected]
https://www.ietf.org/mailman/listinfo/bpf
#2 lore raw
Quoted 41 lines · David Vernet
Quoted 27 lines · Dave Thaler

From: Dave Thaler <[email protected]>

Add docs on extended 64-bit immediate instructions, including six instructions previously undocumented. Include a brief description of map objects, and variables, as used by those instructions.

---
V1 -> V2: rebased on top of latest master

V2 -> V3: addressed comments from Alexei

---
 Documentation/bpf/instruction-set.rst | 56 +++++++++++++++++++++++----
 Documentation/bpf/linux-notes.rst     | 13 +++++++
 2 files changed, 61 insertions(+), 8 deletions(-)
Documentation/bpf/instruction-set.rst +0 -2
index db8789e6969..2c8347d63e7 100644
@@ -385,14 +385,54 @@ and loaded back to ``R0``.
 -----------------------------
 
 Instructions with the ``BPF_IMM`` 'mode' modifier use the wide instruction
-encoding for an extra imm64 value.
-

Documentation/bpf/instruction-set.rst and loaded back to ``R0``. · line 391

Quoted 10 lines
Documentation/bpf/instruction-set.rst +4 -6
-There is currently only one such instruction.
-
-``BPF_LD | BPF_DW | BPF_IMM`` means::
-
-  dst = imm64
-
+encoding defined in `Instruction encoding`_, and use the 'src' field of the
+basic instruction to hold an opcode subtype.
+
+The following instructions are defined, and use additional concepts defined below:

nit: Perhaps this is a bit clearer? Wdyt?

Quoted 3 lines

The following opcode subtypes are defined for BPF_IMM | BPF_DQ | BPF_LD instructions, with new terms such as "map" defined further below:

Oops, meant to say BPF_IMM | BPF_DW | BPF_LD

Quoted 66 lines
Quoted 66 lines
Documentation/bpf/instruction-set.rst +44 -0
+
+=========================  ======  ===  =========================================  ===========  ==============
+opcode construction        opcode  src  pseudocode                                 imm type     dst type
+=========================  ======  ===  =========================================  ===========  ==============
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x0  dst = imm64                                integer      integer
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x1  dst = map_by_fd(imm)                       map fd       map
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x2  dst = map_val(map_by_fd(imm)) + next_imm   map fd       data pointer
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x3  dst = var_addr(imm)                        variable id  data pointer
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x4  dst = code_addr(imm)                       integer      code pointer
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x5  dst = map_by_idx(imm)                      map index    map
+BPF_IMM | BPF_DW | BPF_LD  0x18    0x6  dst = map_val(map_by_idx(imm)) + next_imm  map index    data pointer
+=========================  ======  ===  =========================================  ===========  ==============
+
+where
+
+* map_by_fd(imm) means to convert a 32-bit POSIX file descriptor into an address of a map object (see `Map objects`_)
+* map_by_idx(imm) means to convert a 32-bit index into an address of a map object
+* map_val(map) gets the address of the first value in a given map object
+* var_addr(imm) gets the address of a platform variable (see `Platform Variables`_) with a given id
+* code_addr(imm) gets the address of the instruction at a specified relative offset in number of (64-bit) instructions
+* the 'imm type' can be used by disassemblers for display
+* the 'dst type' can be used for verification and JIT compilation purposes
+
+Map objects
+~~~~~~~~~~~
+
+Maps are shared memory regions accessible by eBPF programs on some platforms, where we use the term "map object"
+to refer to an object containing the data and metadata (e.g., size) about the memory region.
+A map can have various semantics as defined in a separate document, and may or may not have a single
+contiguous memory region, but the 'map_val(map)' is currently only defined for maps that do have a single
+contiguous memory region.
+
+Each map object can have a POSIX file descriptor (fd) if supported by the platform,
+where 'map_by_fd(imm)' means to get the map with the specified file descriptor.
+Each BPF program can also be defined to use a set of maps associated with the program
+at load time, and 'map_by_idx(imm)' means to get the map with the given index in the set
+associated with the BPF program containing the instruction.
+
+Platform Variables
+~~~~~~~~~~~~~~~~~~
+
+Platform variables are memory regions, identified by integer ids, exposed by the runtime and accessible by BPF programs on
+some platforms.  The 'var_addr(imm)' operation means to get the address of the memory region
+identified by the given id.
 
 Legacy BPF Packet access instructions
 -------------------------------------
Documentation/bpf/linux-notes.rst +10 -0
index 956b0c86699..2d161467105 100644
@@ -12,6 +12,19 @@ Byte swap instructions
 
 ``BPF_FROM_LE`` and ``BPF_FROM_BE`` exist as aliases for ``BPF_TO_LE`` and ``BPF_TO_BE`` respectively.
 
+Map objects
+===========
+
+Linux only supports the 'map_val(map)' operation on array maps with a single element.
+
+Linux uses an fd_array to store maps associated with a BPF program. Thus,
+map_by_index(index) uses the fd at that index in the array.
+
+Variables
+=========
Quoted 8 lines

Documentation/bpf/linux-notes.rst Byte swap instructions · line 26

Quoted 2 lines
Documentation/bpf/linux-notes.rst +2 -0
+
+Linux uses BTF ids to identify variables.

Not quite sure exactly what this means. Linux uses BTF ids to identify _types_, right? This doesn't seem like something that needs to be specified as Linux specific either, even if it's not yet supported elsewhere. Certain legacy things such as Linux-specific helpers make sense, but not sure about BTF ids.

Ok, I read over this some more and I think I understand what the intention was here. You were specifying that for the following instruction encoding:

=========================  ======  ===  ===================  ===========  ============
opcode construction        opcode  src  pseudocode           imm type     dst type
=========================  ======  ===  ===================  ===========  ============
BPF_IMM | BPF_DW | BPF_LD  0x18    0x3  dst = var_addr(imm)  variable id  data pointer

imm is a BTF ID, and it is Linux-specific that var_addr(imm) happens to be mapping a BTF ID to an actual variable address that is stored in dst. Is that right? If so, I think this section needs to include a bit more context so that it makes sense on its own. What about something like this:

The following 64-bit immediate instruction specifies that a variable address, which corresponds to some integer stored in the immediate field, should be loaded:

=========================  ======  ===  =========================================  ===========  ==============
opcode construction        opcode  src  pseudocode                                 imm type     dst type
=========================  ======  ===  =========================================  ===========  ==============
BPF_IMM | BPF_DW | BPF_LD  0x18    0x3  dst = var_addr(imm)                        variable id  data pointer

On Linux, this integer is a BTF ID.

Quoted 11 lines
Quoted 11 lines
Documentation/bpf/linux-notes.rst +1 -0
+
 Legacy BPF Packet access instructions
 =====================================
 
Signature · 6 lines
2.33.4

-- 
Bpf mailing list
[email protected]
https://www.ietf.org/mailman/listinfo/bpf