]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/doc/c-bpf.texi
2.41 Release sources
[thirdparty/binutils-gdb.git] / gas / doc / c-bpf.texi
CommitLineData
d87bef3a 1@c Copyright (C) 2019-2023 Free Software Foundation, Inc.
f8861f5d
JM
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
675b9d61
NC
18* BPF Options:: Options
19* BPF Syntax:: Syntax
20* BPF Directives:: Machine Directives
21* BPF Opcodes:: Opcodes
22* BPF Pseudo-C Syntax:: Alternative Pseudo-C Assembly Syntax
f8861f5d
JM
23@end menu
24
25@node BPF Options
675b9d61 26@section Options
f8861f5d
JM
27@cindex BPF options (none)
28@cindex options for BPF (none)
29
30@c man begin OPTIONS
31@table @gcctabopt
32
33@cindex @option{-EB} command-line option, BPF
34@item -EB
35This option specifies that the assembler should emit big-endian eBPF.
36
37@cindex @option{-EL} command-line option, BPF
38@item -EL
39This option specifies that the assembler should emit little-endian
40eBPF.
41@end table
42
43Note that if no endianness option is specified in the command line,
44the host endianness is used.
45@c man end
46
675b9d61
NC
47@node BPF Syntax
48@section Syntax
49@menu
50* BPF-Chars:: Special Characters
51* BPF-Regs:: Register Names
52* BPF-Pseudo-Maps:: Pseudo map fds
53@end menu
54
55@node BPF-Chars
56@subsection Special Characters
f8861f5d
JM
57
58@cindex line comment character, BPF
59@cindex BPF line comment character
675b9d61
NC
60The presence of a @samp{;} on a line indicates the start of a comment
61that extends to the end of the current line. If a @samp{#} appears as
62the first character of a line, the whole line is treated as a comment.
f8861f5d
JM
63
64@cindex statement separator, BPF
65Statements and assembly directives are separated by newlines.
66
675b9d61
NC
67@node BPF-Regs
68@subsection Register Names
f8861f5d
JM
69
70@cindex BPF register names
71@cindex register names, BPF
72The eBPF processor provides ten general-purpose 64-bit registers,
73which are read-write, and a read-only frame pointer register:
74
75@table @samp
76@item %r0 .. %r9
77General-purpose registers.
78@item %r10
675b9d61 79Frame pointer register.
f8861f5d
JM
80@end table
81
675b9d61
NC
82Some registers have additional names, to reflect their role in the
83eBPF ABI:
f8861f5d
JM
84
85@table @samp
675b9d61
NC
86@item %a
87This is @samp{%r0}.
88@item %ctx
89This is @samp{%r6}.
90@item %fp
91This is @samp{%r10}.
f8861f5d
JM
92@end table
93
675b9d61
NC
94@node BPF-Pseudo-Maps
95@subsection Pseudo Maps
96
97@cindex pseudo map fd, BPF
98The @samp{LDDW} instruction can take a literal pseudo map file
99descriptor as its second argument. This uses the syntax
100@samp{%map_fd(N)} where @samp{N} is a signed number.
101
102For example, to load the address of the pseudo map with file
103descriptor @samp{2} in register @samp{r1} we would do:
104
105@smallexample
106 lddw %r1, %map_fd(2)
107@end smallexample
f8861f5d 108
e0b989a6 109@node BPF Directives
675b9d61 110@section Machine Directives
e0b989a6
JM
111
112@cindex machine directives, BPF
113
114The BPF version of @code{@value{AS}} supports the following additional
115machine directives:
116
117@table @code
118@cindex @code{half} directive, BPF
119@item .word
120The @code{.half} directive produces a 16 bit value.
121
122@cindex @code{word} directive, BPF
123@item .word
124The @code{.word} directive produces a 32 bit value.
125
126@cindex @code{dword} directive, BPF
127@item .dword
128The @code{.dword} directive produces a 64 bit value.
129@end table
130
675b9d61
NC
131@node BPF Opcodes
132@section Opcodes
f8861f5d
JM
133
134@cindex BPF opcodes
135@cindex opcodes for BPF
136In the instruction descriptions below the following field descriptors
137are used:
138
139@table @code
675b9d61
NC
140@item %d
141Destination general-purpose register whose role is to be destination
142of an operation.
143@item %s
f8861f5d
JM
144Source general-purpose register whose role is to be the source of an
145operation.
146@item disp16
14716-bit signed PC-relative offset, measured in number of 64-bit words,
148minus one.
149@item disp32
15032-bit signed PC-relative offset, measured in number of 64-bit words,
151minus one.
152@item offset16
675b9d61 153Signed 16-bit immediate.
f8861f5d
JM
154@item imm32
155Signed 32-bit immediate.
156@item imm64
157Signed 64-bit immediate.
158@end table
159
675b9d61 160@subsubsection Arithmetic instructions
f8861f5d
JM
161
162The destination register in these instructions act like an
163accumulator.
164
165@table @code
675b9d61 166@item add %d, (%s|imm32)
f8861f5d 16764-bit arithmetic addition.
675b9d61 168@item sub %d, (%s|imm32)
f8861f5d 16964-bit arithmetic subtraction.
675b9d61 170@item mul %d, (%s|imm32)
f8861f5d 17164-bit arithmetic multiplication.
675b9d61 172@item div %d, (%s|imm32)
f8861f5d 17364-bit arithmetic integer division.
675b9d61 174@item mod %d, (%s|imm32)
f8861f5d 17564-bit integer remainder.
675b9d61 176@item and %d, (%s|imm32)
f8861f5d 17764-bit bit-wise ``and'' operation.
675b9d61 178@item or %d, (%s|imm32)
f8861f5d 17964-bit bit-wise ``or'' operation.
675b9d61 180@item xor %d, (%s|imm32)
f8861f5d 18164-bit bit-wise exclusive-or operation.
675b9d61
NC
182@item lsh %d, (%s|imm32)
18364-bit left shift, by @code{%s} or @code{imm32} bits.
184@item rsh %d, (%s|imm32)
18564-bit right logical shift, by @code{%s} or @code{imm32} bits.
186@item arsh %d, (%s|imm32)
18764-bit right arithmetic shift, by @code{%s} or @code{imm32} bits.
188@item neg %d
f8861f5d 18964-bit arithmetic negation.
675b9d61
NC
190@item mov %d, (%s|imm32)
191Move the 64-bit value of @code{%s} in @code{%d}, or load @code{imm32}
192in @code{%d}.
f8861f5d
JM
193@end table
194
675b9d61 195@subsubsection 32-bit arithmetic instructions
f8861f5d
JM
196
197The destination register in these instructions act as an accumulator.
198
199@table @code
675b9d61 200@item add32 %d, (%s|imm32)
f8861f5d 20132-bit arithmetic addition.
675b9d61 202@item sub32 %d, (%s|imm32)
f8861f5d 20332-bit arithmetic subtraction.
675b9d61 204@item mul32 %d, (%s|imm32)
f8861f5d 20532-bit arithmetic multiplication.
675b9d61 206@item div32 %d, (%s|imm32)
f8861f5d 20732-bit arithmetic integer division.
675b9d61 208@item mod32 %d, (%s|imm32)
f8861f5d 20932-bit integer remainder.
675b9d61 210@item and32 %d, (%s|imm32)
f8861f5d 21132-bit bit-wise ``and'' operation.
675b9d61 212@item or32 %d, (%s|imm32)
f8861f5d 21332-bit bit-wise ``or'' operation.
675b9d61 214@item xor32 %d, (%s|imm32)
f8861f5d 21532-bit bit-wise exclusive-or operation.
675b9d61
NC
216@item lsh32 %d, (%s|imm32)
21732-bit left shift, by @code{%s} or @code{imm32} bits.
218@item rsh32 %d, (%s|imm32)
21932-bit right logical shift, by @code{%s} or @code{imm32} bits.
220@item arsh32 %d, (%s|imm32)
22132-bit right arithmetic shift, by @code{%s} or @code{imm32} bits.
222@item neg32 %d
f8861f5d 22332-bit arithmetic negation.
675b9d61
NC
224@item mov32 %d, (%s|imm32)
225Move the 32-bit value of @code{%s} in @code{%d}, or load @code{imm32}
226in @code{%d}.
f8861f5d
JM
227@end table
228
675b9d61 229@subsubsection Endianness conversion instructions
5cbe5492
JM
230
231@table @code
675b9d61
NC
232@item endle %d, (16|32|64)
233Convert the 16-bit, 32-bit or 64-bit value in @code{%d} to
234little-endian.
235@item endbe %d, (16|32|64)
236Convert the 16-bit, 32-bit or 64-bit value in @code{%d} to big-endian.
5cbe5492
JM
237@end table
238
675b9d61 239@subsubsection 64-bit load and pseudo maps
f8861f5d
JM
240
241@table @code
675b9d61
NC
242@item lddw %d, imm64
243Load the given signed 64-bit immediate, or pseudo map descriptor, to
244the destination register @code{%d}.
245@item lddw %d, %map_fd(N)
246Load the address of the given pseudo map fd @emph{N} to the
247destination register @code{%d}.
f8861f5d
JM
248@end table
249
675b9d61 250@subsubsection Load instructions for socket filters
f8861f5d
JM
251
252The following instructions are intended to be used in socket filters,
253and are therefore not general-purpose: they make assumptions on the
254contents of several registers. See the file
255@file{Documentation/networking/filter.txt} in the Linux kernel source
256tree for more information.
257
258Absolute loads:
259
260@table @code
3719fd55 261@item ldabsdw imm32
f8861f5d 262Absolute 64-bit load.
3719fd55 263@item ldabsw imm32
f8861f5d 264Absolute 32-bit load.
3719fd55 265@item ldabsh imm32
f8861f5d 266Absolute 16-bit load.
3719fd55 267@item ldabsb imm32
f8861f5d
JM
268Absolute 8-bit load.
269@end table
270
271Indirect loads:
272
273@table @code
675b9d61 274@item ldinddw %s, imm32
f8861f5d 275Indirect 64-bit load.
675b9d61 276@item ldindw %s, imm32
f8861f5d 277Indirect 32-bit load.
675b9d61 278@item ldindh %s, imm32
f8861f5d 279Indirect 16-bit load.
3719fd55 280@item ldindb %s, imm32
f8861f5d
JM
281Indirect 8-bit load.
282@end table
283
675b9d61 284@subsubsection Generic load/store instructions
f8861f5d
JM
285
286General-purpose load and store instructions are provided for several
287word sizes.
288
289Load to register instructions:
290
291@table @code
675b9d61 292@item ldxdw %d, [%s+offset16]
f8861f5d 293Generic 64-bit load.
675b9d61 294@item ldxw %d, [%s+offset16]
f8861f5d 295Generic 32-bit load.
675b9d61 296@item ldxh %d, [%s+offset16]
f8861f5d 297Generic 16-bit load.
675b9d61 298@item ldxb %d, [%s+offset16]
f8861f5d
JM
299Generic 8-bit load.
300@end table
301
302Store from register instructions:
303
304@table @code
675b9d61 305@item stxdw [%d+offset16], %s
f8861f5d 306Generic 64-bit store.
675b9d61 307@item stxw [%d+offset16], %s
f8861f5d 308Generic 32-bit store.
675b9d61 309@item stxh [%d+offset16], %s
f8861f5d 310Generic 16-bit store.
675b9d61 311@item stxb [%d+offset16], %s
f8861f5d
JM
312Generic 8-bit store.
313@end table
314
315Store from immediates instructions:
316
317@table @code
675b9d61 318@item stddw [%d+offset16], imm32
f8861f5d 319Store immediate as 64-bit.
675b9d61 320@item stdw [%d+offset16], imm32
f8861f5d 321Store immediate as 32-bit.
675b9d61 322@item stdh [%d+offset16], imm32
f8861f5d 323Store immediate as 16-bit.
675b9d61 324@item stdb [%d+offset16], imm32
f8861f5d
JM
325Store immediate as 8-bit.
326@end table
327
675b9d61 328@subsubsection Jump instructions
f8861f5d
JM
329
330eBPF provides the following compare-and-jump instructions, which
331compare the values of the two given registers, or the values of a
332register and an immediate, and perform a branch in case the comparison
333holds true.
334
335@table @code
675b9d61 336@item ja %d,(%s|imm32),disp16
f8861f5d 337Jump-always.
675b9d61 338@item jeq %d,(%s|imm32),disp16
dcdec68b 339Jump if equal, unsigned.
675b9d61 340@item jgt %d,(%s|imm32),disp16
dcdec68b 341Jump if greater, unsigned.
675b9d61 342@item jge %d,(%s|imm32),disp16
f8861f5d 343Jump if greater or equal.
675b9d61 344@item jlt %d,(%s|imm32),disp16
f8861f5d 345Jump if lesser.
675b9d61 346@item jle %d,(%s|imm32),disp16
f8861f5d 347Jump if lesser or equal.
675b9d61 348@item jset %d,(%s|imm32),disp16
f8861f5d 349Jump if signed equal.
675b9d61 350@item jne %d,(%s|imm32),disp16
f8861f5d 351Jump if not equal.
675b9d61 352@item jsgt %d,(%s|imm32),disp16
f8861f5d 353Jump if signed greater.
675b9d61 354@item jsge %d,(%s|imm32),disp16
f8861f5d 355Jump if signed greater or equal.
675b9d61 356@item jslt %d,(%s|imm32),disp16
f8861f5d 357Jump if signed lesser.
675b9d61 358@item jsle %d,(%s|imm32),disp16
f8861f5d
JM
359Jump if signed lesser or equal.
360@end table
361
362A call instruction is provided in order to perform calls to other eBPF
363functions, or to external kernel helpers:
364
365@table @code
675b9d61 366@item call (disp32|imm32)
f8861f5d
JM
367Jump and link to the offset @emph{disp32}, or to the kernel helper
368function identified by @emph{imm32}.
369@end table
370
371Finally:
372
373@table @code
374@item exit
375Terminate the eBPF program.
376@end table
377
675b9d61 378@subsubsection Atomic instructions
f8861f5d 379
675b9d61
NC
380Atomic exchange-and-add instructions are provided in two flavors: one
381for swapping 64-bit quantities and another for 32-bit quantities.
dcdec68b 382
d218e7fe 383@table @code
675b9d61
NC
384@item xadddw [%d+offset16],%s
385Exchange-and-add a 64-bit value at the specified location.
386@item xaddw [%d+offset16],%s
387Exchange-and-add a 32-bit value at the specified location.
388@end table
dcdec68b 389
675b9d61
NC
390@node BPF Pseudo-C Syntax
391@section BPF Pseudo-C Syntax
dcdec68b 392
675b9d61
NC
393This assembler supports another syntax to denote BPF instructions,
394which is an alternative to the normal looking syntax documented above.
395This alternatative syntax, which we call @dfn{pseudo-C syntax}, is
396supported by the LLVM/clang integrated assembler.
dcdec68b 397
675b9d61
NC
398This syntax is very unconventional, but we need to support it in order
399to support inline assembly in existing BPF programs.
dcdec68b 400
675b9d61
NC
401Note that the assembler is able to parse sources in which both
402syntaxes coexist: some instructions can use the usual assembly like
403syntax, whereas some other instructions in the same file can use the
404pseudo-C syntax.
dcdec68b 405
675b9d61 406@subsubsection Pseudo-C Register Names
d218e7fe 407
675b9d61
NC
408All BPF registers are 64-bit long. However, in the Pseudo-C syntax
409registers can be referred using different names, which actually
410reflect the kind of instruction they appear on:
02f68ef2 411
675b9d61
NC
412@table @samp
413@item r0..r9
414General-purpose register in an instruction that operates on its value
415as if it was a 64-bit value.
416@item w0..w9
417General-purpose register in an instruction that operates on its value
418as if it was a 32-bit value.
02f68ef2
DF
419@end table
420
421@noindent
675b9d61
NC
422Note that in the Pseudo-C syntax register names are not preceded by
423@code{%} characters.
d218e7fe 424
675b9d61 425@subsubsection Arithmetic instructions
d218e7fe 426
675b9d61
NC
427In all the instructions below, the operations are 64-bit or 32-bit
428depending on the names used to refer to the registers. For example
429@code{r3 += r2} will perform 64-bit addition, whereas @code{w3 += w2}
430will perform 32-bit addition. Mixing register prefixes is an error,
431for example @code{r3 += w2}.
d218e7fe 432
675b9d61
NC
433@table @code
434@item dst_reg += (imm32|src_reg)
435Arithmetic addition.
436@item dst_reg -= (imm32|src_reg)
437Arithmetic subtraction.
438@item dst_reg *= (imm32|src_reg)
439Arithmetic multiplication.
440@item dst_reg /= (imm32|src_reg)
441Arithmetic integer unsigned division.
442@item dst_reg %= (imm32|src_reg)
443Arithmetic integer unsigned remainder.
444@item dst_reg &= (imm32|src_reg)
445Bit-wise ``and'' operation.
446@item dst_reg |= (imm32|src_reg)
447Bit-wise ``or'' operation.
448@item dst_reg ^= (imm32|src_reg)
449Bit-wise exclusive-or operation.
450@item dst_reg <<= (imm32|src_reg)
451Left shift, by whatever specified number of bits.
452@item dst_reg >>= (imm32|src_reg)
453Right logical shift, by whatever specified number of bits.
454@item dst_reg s>>= (imm32|src_reg)
455Right arithmetic shift, by whatever specified number of bits.
456@item dst_reg = (imm32|src_reg)
457Move the value in @code{imm32} or @code{src_reg} in @code{dst_reg}.
458@item dst_reg = -dst_reg
459Arithmetic negation.
dcdec68b
JM
460@end table
461
675b9d61 462@subsubsection Endianness conversion instructions
dcdec68b
JM
463
464@table @code
675b9d61
NC
465@item dst_reg = le16 src_reg
466Convert the 16-bit value in @code{src_reg} to little-endian.
467@item dst_reg = le32 src_reg
468Convert the 32-bit value in @code{src_reg} to little-endian.
469@item dst_reg = le64 src_reg
470Convert the 64-bit value in @code{src_reg} to little-endian.
471@item dst_reg = be16 src_reg
472Convert the 16-bit value in @code{src_reg} to big-endian.
473@item dst_reg = be32 src_reg
474Convert the 32-bit value in @code{src_reg} to big-endian.
475@item dst_reg = be64 src_reg
476Convert the 64-bit value in @code{src_reg} to big-endian.
dcdec68b
JM
477@end table
478
675b9d61 479@subsubsection 64-bit load and pseudo maps
dcdec68b
JM
480
481@table @code
675b9d61
NC
482@item dst_reg = imm64 ll
483Load the given signed 64-bit immediate, or pseudo map descriptor, to
484the destination register @code{dst_reg}.
dcdec68b
JM
485@end table
486
675b9d61 487@subsubsection Load instructions for socket filters
02f68ef2
DF
488
489@table @code
675b9d61
NC
490@item r0 = *(u8 *)skb[imm32]
491Absolute 8-bit load.
492@item r0 = *(u16 *)skb[imm32]
493Absolute 16-bit load.
494@item r0 = *(u32 *)skb[imm32]
495Absolute 32-bit load.
496@item r0 = *(u64 *)skb[imm32]
497Absolute 64-bit load.
498@item r0 = *(u8 *)skb[src_reg + imm32]
499Indirect 8-bit load.
500@item r0 = *(u16 *)skb[src_reg + imm32]
501Indirect 16-bit load.
502@item r0 = *(u32 *)skb[src_reg + imm32]
503Indirect 32-bit load.
504@item r0 = *(u64 *)skb[src_reg + imm32]
505Indirect 64-bit load.
02f68ef2
DF
506@end table
507
675b9d61 508@subsubsection Generic load/store instructions
dcdec68b
JM
509
510@table @code
675b9d61
NC
511@item dst_reg = *(u8 *)(src_reg + offset16)
512Generic 8-bit load.
513@item dst_reg = *(u16 *)(src_reg + offset16)
514Generic 16-bit load.
515@item dst_reg = *(u32 *)(src_reg + offset16)
516Generic 32-bit load.
517@item dst_reg = *(u64 *)(src_reg + offset16)
518Generic 64-bit load.
519@c XXX stb
520@c NO PSEUDOC-SYNTAX
521@c XXX sth
522@c NO PSEUDOC-SYNTAX
523@c XXX stw
524@c NO PSEUDOC-SYNTAX
525@c XXX stdw
526@c NO PSEUDOC-SYNTAX
527@item *(u8 *)(dst_reg + offset16) = src_reg
528Generic 8-bit store.
529@item *(u16 *)(dst_reg + offset16) = src_reg
530Generic 16-bit store.
531@item *(u32 *)(dst_reg + offset16) = src_reg
532Generic 32-bit store.
533@item *(u64 *)(dst_reg + offset16) = src_reg
534Generic 64-bit store.
dcdec68b
JM
535@end table
536
675b9d61 537@subsubsection Jump instructions
dcdec68b
JM
538
539@table @code
675b9d61
NC
540@item goto disp16
541Jump-always.
542@item if dst_reg == (imm32|src_reg) goto disp16
543Jump if equal.
544@item if dst_reg & (imm32|src_reg) goto disp16
545Jump if signed equal.
546@item if dst_reg != (imm32|src_reg) goto disp16
547Jump if not equal.
548@item if dst_reg > (imm32|src_reg) goto disp16
549Jump if bigger, unsigned.
550@item if dst_reg < (imm32|src_reg) goto disp16
551Jump if smaller, unsigned.
552@item if dst_reg >= (imm32|src_reg) goto disp16
553Jump if bigger or equal, unsigned.
554@item if dst_reg <= (imm32|src_reg) goto disp16
555Jump if smaller or equal, unsigned.
556@item if dst_reg s> (imm32|src_reg) goto disp16
557Jump if bigger, signed.
558@item if dst_reg s< (imm32|src_reg) goto disp16
559Jump if smaller, signed.
560@item if dst_reg s>= (imm32|src_reg) goto disp16
561Jump if bigger or equal, signed.
562@item if dst_reg s<= (imm32|src_reg) goto disp16
563Jump if smaller or equal, signed.
564@item call imm32
565Jump and link.
566@item exit
567Terminate the eBPF program.
dcdec68b
JM
568@end table
569
675b9d61 570@subsubsection Atomic instructions
dcdec68b
JM
571
572@table @code
675b9d61
NC
573@item lock *(u64 *)(dst_reg + offset16) += src_reg
574Exchange-and-add a 64-bit value at the specified location.
575@item lock *(u32 *)(dst_reg + offset16) += src_reg
576Exchange-and-add a 32-bit value at the specified location.
dcdec68b 577@end table