1@c Copyright (C) 2019-2022 Free Software Foundation, Inc. 2@c This is part of the GAS manual. 3@c For copying conditions, see the file as.texinfo. 4 5@ifset GENERIC 6@page 7@node BPF-Dependent 8@chapter BPF Dependent Features 9@end ifset 10 11@ifclear GENERIC 12@node Machine Dependencies 13@chapter BPF Dependent Features 14@end ifclear 15 16@cindex BPF support 17@menu 18* BPF Options:: Options 19* BPF Syntax:: Syntax 20* BPF Directives:: Machine Directives 21* BPF Opcodes:: Opcodes 22@end menu 23 24@node BPF Options 25@section Options 26@cindex BPF options (none) 27@cindex options for BPF (none) 28 29@c man begin OPTIONS 30@table @gcctabopt 31 32@cindex @option{-EB} command-line option, BPF 33@item -EB 34This option specifies that the assembler should emit big-endian eBPF. 35 36@cindex @option{-EL} command-line option, BPF 37@item -EL 38This option specifies that the assembler should emit little-endian 39eBPF. 40@end table 41 42Note that if no endianness option is specified in the command line, 43the host endianness is used. 44@c man end 45 46@node BPF Syntax 47@section Syntax 48@menu 49* BPF-Chars:: Special Characters 50* BPF-Regs:: Register Names 51* BPF-Pseudo-Maps:: Pseudo map fds 52@end menu 53 54@node BPF-Chars 55@subsection Special Characters 56 57@cindex line comment character, BPF 58@cindex BPF line comment character 59The presence of a @samp{;} on a line indicates the start of a comment 60that extends to the end of the current line. If a @samp{#} appears as 61the first character of a line, the whole line is treated as a comment. 62 63@cindex statement separator, BPF 64Statements and assembly directives are separated by newlines. 65 66@node BPF-Regs 67@subsection Register Names 68 69@cindex BPF register names 70@cindex register names, BPF 71The eBPF processor provides ten general-purpose 64-bit registers, 72which are read-write, and a read-only frame pointer register: 73 74@table @samp 75@item %r0 .. %r9 76General-purpose registers. 77@item %r10 78Frame pointer register. 79@end table 80 81Some registers have additional names, to reflect their role in the 82eBPF ABI: 83 84@table @samp 85@item %a 86This is @samp{%r0}. 87@item %ctx 88This is @samp{%r6}. 89@item %fp 90This is @samp{%r10}. 91@end table 92 93@node BPF-Pseudo-Maps 94@subsection Pseudo Maps 95 96@cindex pseudo map fd, BPF 97The @samp{LDDW} instruction can take a literal pseudo map file 98descriptor as its second argument. This uses the syntax 99@samp{%map_fd(N)} where @samp{N} is a signed number. 100 101For example, to load the address of the pseudo map with file 102descriptor @samp{2} in register @samp{r1} we would do: 103 104@smallexample 105 lddw %r1, %map_fd(2) 106@end smallexample 107 108@node BPF Directives 109@section Machine Directives 110 111@cindex machine directives, BPF 112 113The BPF version of @code{@value{AS}} supports the following additional 114machine directives: 115 116@table @code 117@cindex @code{half} directive, BPF 118@item .word 119The @code{.half} directive produces a 16 bit value. 120 121@cindex @code{word} directive, BPF 122@item .word 123The @code{.word} directive produces a 32 bit value. 124 125@cindex @code{dword} directive, BPF 126@item .dword 127The @code{.dword} directive produces a 64 bit value. 128@end table 129 130@node BPF Opcodes 131@section Opcodes 132 133@cindex BPF opcodes 134@cindex opcodes for BPF 135In the instruction descriptions below the following field descriptors 136are used: 137 138@table @code 139@item %d 140Destination general-purpose register whose role is to be destination 141of an operation. 142@item %s 143Source general-purpose register whose role is to be the source of an 144operation. 145@item disp16 14616-bit signed PC-relative offset, measured in number of 64-bit words, 147minus one. 148@item disp32 14932-bit signed PC-relative offset, measured in number of 64-bit words, 150minus one. 151@item offset16 152Signed 16-bit immediate. 153@item imm32 154Signed 32-bit immediate. 155@item imm64 156Signed 64-bit immediate. 157@end table 158 159@subsubsection Arithmetic instructions 160 161The destination register in these instructions act like an 162accumulator. 163 164@table @code 165@item add %d, (%s|imm32) 16664-bit arithmetic addition. 167@item sub %d, (%s|imm32) 16864-bit arithmetic subtraction. 169@item mul %d, (%s|imm32) 17064-bit arithmetic multiplication. 171@item div %d, (%s|imm32) 17264-bit arithmetic integer division. 173@item mod %d, (%s|imm32) 17464-bit integer remainder. 175@item and %d, (%s|imm32) 17664-bit bit-wise ``and'' operation. 177@item or %d, (%s|imm32) 17864-bit bit-wise ``or'' operation. 179@item xor %d, (%s|imm32) 18064-bit bit-wise exclusive-or operation. 181@item lsh %d, (%s|imm32) 18264-bit left shift, by @code{%s} or @code{imm32} bits. 183@item rsh %d, (%s|imm32) 18464-bit right logical shift, by @code{%s} or @code{imm32} bits. 185@item arsh %d, (%s|imm32) 18664-bit right arithmetic shift, by @code{%s} or @code{imm32} bits. 187@item neg %d 18864-bit arithmetic negation. 189@item mov %d, (%s|imm32) 190Move the 64-bit value of @code{%s} in @code{%d}, or load @code{imm32} 191in @code{%d}. 192@end table 193 194@subsubsection 32-bit arithmetic instructions 195 196The destination register in these instructions act as an accumulator. 197 198@table @code 199@item add32 %d, (%s|imm32) 20032-bit arithmetic addition. 201@item sub32 %d, (%s|imm32) 20232-bit arithmetic subtraction. 203@item mul32 %d, (%s|imm32) 20432-bit arithmetic multiplication. 205@item div32 %d, (%s|imm32) 20632-bit arithmetic integer division. 207@item mod32 %d, (%s|imm32) 20832-bit integer remainder. 209@item and32 %d, (%s|imm32) 21032-bit bit-wise ``and'' operation. 211@item or32 %d, (%s|imm32) 21232-bit bit-wise ``or'' operation. 213@item xor32 %d, (%s|imm32) 21432-bit bit-wise exclusive-or operation. 215@item lsh32 %d, (%s|imm32) 21632-bit left shift, by @code{%s} or @code{imm32} bits. 217@item rsh32 %d, (%s|imm32) 21832-bit right logical shift, by @code{%s} or @code{imm32} bits. 219@item arsh32 %d, (%s|imm32) 22032-bit right arithmetic shift, by @code{%s} or @code{imm32} bits. 221@item neg32 %d 22232-bit arithmetic negation. 223@item mov32 %d, (%s|imm32) 224Move the 32-bit value of @code{%s} in @code{%d}, or load @code{imm32} 225in @code{%d}. 226@end table 227 228@subsubsection Endianness conversion instructions 229 230@table @code 231@item endle %d, (8|16|32) 232Convert the 8-bit, 16-bit or 32-bit value in @code{%d} to 233little-endian. 234@item endbe %d, (8|16|32) 235Convert the 8-bit, 16-bit or 32-bit value in @code{%d} to big-endian. 236@end table 237 238@subsubsection 64-bit load and pseudo maps 239 240@table @code 241@item lddw %d, imm64 242Load the given signed 64-bit immediate, or pseudo map descriptor, to 243the destination register @code{%d}. 244@item lddw %d, %map_fd(N) 245Load the address of the given pseudo map fd @emph{N} to the 246destination register @code{%d}. 247@end table 248 249@subsubsection Load instructions for socket filters 250 251The following instructions are intended to be used in socket filters, 252and are therefore not general-purpose: they make assumptions on the 253contents of several registers. See the file 254@file{Documentation/networking/filter.txt} in the Linux kernel source 255tree for more information. 256 257Absolute loads: 258 259@table @code 260@item ldabsdw imm32 261Absolute 64-bit load. 262@item ldabsw imm32 263Absolute 32-bit load. 264@item ldabsh imm32 265Absolute 16-bit load. 266@item ldabsb imm32 267Absolute 8-bit load. 268@end table 269 270Indirect loads: 271 272@table @code 273@item ldinddw %s, imm32 274Indirect 64-bit load. 275@item ldindw %s, imm32 276Indirect 32-bit load. 277@item ldindh %s, imm32 278Indirect 16-bit load. 279@item ldindb %s, imm32 280Indirect 8-bit load. 281@end table 282 283@subsubsection Generic load/store instructions 284 285General-purpose load and store instructions are provided for several 286word sizes. 287 288Load to register instructions: 289 290@table @code 291@item ldxdw %d, [%s+offset16] 292Generic 64-bit load. 293@item ldxw %d, [%s+offset16] 294Generic 32-bit load. 295@item ldxh %d, [%s+offset16] 296Generic 16-bit load. 297@item ldxb %d, [%s+offset16] 298Generic 8-bit load. 299@end table 300 301Store from register instructions: 302 303@table @code 304@item stxdw [%d+offset16], %s 305Generic 64-bit store. 306@item stxw [%d+offset16], %s 307Generic 32-bit store. 308@item stxh [%d+offset16], %s 309Generic 16-bit store. 310@item stxb [%d+offset16], %s 311Generic 8-bit store. 312@end table 313 314Store from immediates instructions: 315 316@table @code 317@item stddw [%d+offset16], imm32 318Store immediate as 64-bit. 319@item stdw [%d+offset16], imm32 320Store immediate as 32-bit. 321@item stdh [%d+offset16], imm32 322Store immediate as 16-bit. 323@item stdb [%d+offset16], imm32 324Store immediate as 8-bit. 325@end table 326 327@subsubsection Jump instructions 328 329eBPF provides the following compare-and-jump instructions, which 330compare the values of the two given registers, or the values of a 331register and an immediate, and perform a branch in case the comparison 332holds true. 333 334@table @code 335@item ja %d,(%s|imm32),disp16 336Jump-always. 337@item jeq %d,(%s|imm32),disp16 338Jump if equal. 339@item jgt %d,(%s|imm32),disp16 340Jump if greater. 341@item jge %d,(%s|imm32),disp16 342Jump if greater or equal. 343@item jlt %d,(%s|imm32),disp16 344Jump if lesser. 345@item jle %d,(%s|imm32),disp16 346Jump if lesser or equal. 347@item jset %d,(%s|imm32),disp16 348Jump if signed equal. 349@item jne %d,(%s|imm32),disp16 350Jump if not equal. 351@item jsgt %d,(%s|imm32),disp16 352Jump if signed greater. 353@item jsge %d,(%s|imm32),disp16 354Jump if signed greater or equal. 355@item jslt %d,(%s|imm32),disp16 356Jump if signed lesser. 357@item jsle %d,(%s|imm32),disp16 358Jump if signed lesser or equal. 359@end table 360 361A call instruction is provided in order to perform calls to other eBPF 362functions, or to external kernel helpers: 363 364@table @code 365@item call (disp32|imm32) 366Jump and link to the offset @emph{disp32}, or to the kernel helper 367function identified by @emph{imm32}. 368@end table 369 370Finally: 371 372@table @code 373@item exit 374Terminate the eBPF program. 375@end table 376 377@subsubsection Atomic instructions 378 379Atomic exchange-and-add instructions are provided in two flavors: one 380for swapping 64-bit quantities and another for 32-bit quantities. 381 382@table @code 383@item xadddw [%d+offset16],%s 384Exchange-and-add a 64-bit value at the specified location. 385@item xaddw [%d+offset16],%s 386Exchange-and-add a 32-bit value at the specified location. 387@end table 388