xref: /netbsd-src/external/gpl3/binutils/dist/gas/doc/c-bpf.texi (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
1@c Copyright (C) 2019-2024 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::                 BPF specific command-line options.
19* BPF Special Characters::      Comments and statements.
20* BPF Registers::               Register names.
21* BPF Directives::		Machine directives.
22* BPF Instructions::            Machine instructions.
23@end menu
24
25@node BPF Options
26@section BPF Options
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
42@cindex @option{-mdialect} command-line options, BPF
43@item -mdialect=@var{dialect}
44This option specifies the assembly language dialect to recognize while
45assembling.  The assembler supports @option{normal} and
46@option{pseudoc}.
47
48@cindex @option{-misa-spec} command-line options, BPF
49@item -misa-spec=@var{spec}
50This option specifies the version of the BPF instruction set to use
51when assembling.  The BPF ISA versions supported are @option{v1} @option{v2}, @option{v3} and @option{v4}.
52
53The value @option{xbpf} can be specified to recognize extra
54instructions that are used by GCC for testing purposes.  But beware
55this is not valid BPF.
56
57@cindex @option{-mno-relax} command-line options, BPF
58@item -mno-relax
59This option tells the assembler to not relax instructions.
60@end table
61
62Note that if no endianness option is specified in the command line,
63the host endianness is used.
64@c man end
65
66@node BPF Special Characters
67@section BPF Special Characters
68
69@cindex line comment character, BPF
70@cindex BPF line comment character
71The presence of a @samp{#} or @samp{//} anywhere on a line indicates
72the start of a comment that extends to the end of the line.
73
74@cindex block comments, BPF
75@cindex BPF block comments
76The presence of the @samp{/*} sequence indicates the beginning of a
77block (multi-line) comment, whose contents span until the next
78@samp{*/} sequence.  It is not possible to nest block comments.
79
80@cindex statement separator, BPF
81Statements and assembly directives are separated by newlines and
82@samp{;} characters.
83
84@node BPF Registers
85@section BPF Registers
86
87@cindex BPF register names
88@cindex register names, BPF
89The eBPF processor provides ten general-purpose 64-bit registers,
90which are read-write, and a read-only frame pointer register:
91
92@noindent
93In normal syntax:
94
95@table @samp
96@item %r0 .. %r9
97General-purpose registers.
98@item %r10
99@itemx %fp
100Read-only frame pointer register.
101@end table
102
103All BPF registers are 64-bit long.  However, in the Pseudo-C syntax
104registers can be referred using different names, which actually
105reflect the kind of instruction they appear on:
106
107@noindent
108In pseudoc syntax:
109
110@table @samp
111@item r0..r9
112General-purpose register in an instruction that operates on its value
113as if it was a 64-bit value.
114@item w0..w9
115General-purpose register in an instruction that operates on its value
116as if it was a 32-bit value.
117@item r10
118Read-only frame pointer register.
119@end table
120
121@noindent
122Note that in the Pseudo-C syntax register names are not preceded by
123@code{%} characters.  A consequence of that is that in contexts like
124instruction operands, where both register names and expressions
125involving symbols are expected, there is no way to disambiguate
126between them.  In order to keep things simple, this assembler does not
127allow to refer to symbols whose names collide with register names in
128instruction operands.
129
130@node BPF Directives
131@section BPF Directives
132
133@cindex machine directives, BPF
134
135The BPF version of @code{@value{AS}} supports the following additional
136machine directives:
137
138@table @code
139@cindex @code{half} directive, BPF
140@item .word
141The @code{.half} directive produces a 16 bit value.
142
143@cindex @code{word} directive, BPF
144@item .word
145The @code{.word} directive produces a 32 bit value.
146
147@cindex @code{dword} directive, BPF
148@item .dword
149The @code{.dword} directive produces a 64 bit value.
150@end table
151
152@node BPF Instructions
153@section BPF Instructions
154
155@cindex BPF opcodes
156@cindex opcodes for BPF
157In the instruction descriptions below the following field descriptors
158are used:
159
160@table @code
161@item rd
162Destination general-purpose register whose role is to be the
163destination of an operation.
164@item rs
165Source general-purpose register whose role is to be the source of an
166operation.
167@item disp16
16816-bit signed PC-relative offset, measured in number of 64-bit words,
169minus one.
170@item disp32
17132-bit signed PC-relative offset, measured in number of 64-bit words,
172minus one.
173@item offset16
174Signed 16-bit immediate representing an offset in bytes.
175@item disp16
176Signed 16-bit immediate representing a displacement to a target,
177measured in number of 64-bit words @emph{minus one}.
178@item disp32
179Signed 32-bit immediate representing a displacement to a target,
180measured in number of 64-bit words @emph{minus one}.
181@item imm32
182Signed 32-bit immediate.
183@item imm64
184Signed 64-bit immediate.
185@end table
186
187@noindent
188Note that the assembler allows to express the value for an immediate
189using any numerical literal whose two's complement encoding fits in
190the immediate field.  For example, @code{-2}, @code{0xfffffffe} and
191@code{4294967294} all denote the same encoded 32-bit immediate, whose
192value may be then interpreted by different instructions as either as a
193negative or a positive number.
194
195@subsection Arithmetic instructions
196
197The destination register in these instructions act like an
198accumulator.
199
200Note that in pseudoc syntax these instructions should use @code{r}
201registers.
202
203@table @code
204@item add rd, rs
205@itemx add rd, imm32
206@itemx rd += rs
207@itemx rd += imm32
20864-bit arithmetic addition.
209
210@item sub rd, rs
211@itemx sub rd, rs
212@itemx rd -= rs
213@itemx rd -= imm32
21464-bit arithmetic subtraction.
215
216@item mul rd, rs
217@itemx mul rd, imm32
218@itemx rd *= rs
219@itemx rd *= imm32
22064-bit arithmetic multiplication.
221
222@item div rd, rs
223@itemx div rd, imm32
224@itemx rd /= rs
225@itemx rd /= imm32
22664-bit arithmetic integer division.
227
228@item mod rd, rs
229@itemx mod rd, imm32
230@itemx rd %= rs
231@itemx rd %= imm32
23264-bit integer remainder.
233
234@item and rd, rs
235@itemx and rd, imm32
236@itemx rd &= rs
237@itemx rd &= imm32
23864-bit bit-wise ``and'' operation.
239
240@item or rd, rs
241@itemx or rd, imm32
242@itemx rd |= rs
243@itemx rd |= imm32
24464-bit bit-wise ``or'' operation.
245
246@item xor rd, imm32
247@itemx xor rd, rs
248@itemx rd ^= rs
249@itemx rd ^= imm32
25064-bit bit-wise exclusive-or operation.
251
252@item lsh rd, rs
253@itemx ldh rd, imm32
254@itemx rd <<= rs
255@itemx rd <<= imm32
25664-bit left shift, by @code{rs} or @code{imm32} bits.
257
258@item rsh %d, %s
259@itemx rsh rd, imm32
260@itemx rd >>= rs
261@itemx rd >>= imm32
26264-bit right logical shift, by @code{rs} or @code{imm32} bits.
263
264@item arsh rd, rs
265@itemx arsh rd, imm32
266@itemx rd s>>= rs
267@itemx rd s>>= imm32
26864-bit right arithmetic shift, by @code{rs} or @code{imm32} bits.
269
270@item neg rd
271@itemx rd = - rd
27264-bit arithmetic negation.
273
274@item mov rd, rs
275@itemx mov rd, imm32
276@itemx rd = rs
277@itemx rd = imm32
278Move the 64-bit value of @code{rs} in @code{rd}, or load @code{imm32}
279in @code{rd}.
280
281@item movs rd, rs, 8
282@itemx rd = (s8) rs
283Move the sign-extended 8-bit value in @code{rs} to @code{rd}.
284
285@item movs rd, rs, 16
286@itemx rd = (s16) rs
287Move the sign-extended 16-bit value in @code{rs} to @code{rd}.
288
289@item movs rd, rs, 32
290@itemx rd = (s32) rs
291Move the sign-extended 32-bit value in @code{rs} to @code{rd}.
292@end table
293
294@subsection 32-bit arithmetic instructions
295
296The destination register in these instructions act as an accumulator.
297
298Note that in pseudoc syntax these instructions should use @code{w}
299registers.  It is not allowed to mix @code{w} and @code{r} registers
300in the same instruction.
301
302@table @code
303@item add32 rd, rs
304@itemx add32 rd, imm32
305@itemx rd += rs
306@itemx rd += imm32
30732-bit arithmetic addition.
308
309@item sub32 rd, rs
310@itemx sub32 rd, imm32
311@itemx rd -= rs
312@itemx rd += imm32
31332-bit arithmetic subtraction.
314
315@item mul32 rd, rs
316@itemx mul32 rd, imm32
317@itemx rd *= rs
318@itemx rd *= imm32
31932-bit arithmetic multiplication.
320
321@item div32 rd, rs
322@itemx div32 rd, imm32
323@itemx rd /= rs
324@itemx rd /= imm32
32532-bit arithmetic integer division.
326
327@item mod32 rd, rs
328@itemx mod32 rd, imm32
329@itemx rd %= rs
330@itemx rd %= imm32
33132-bit integer remainder.
332
333@item and32 rd, rs
334@itemx and32 rd, imm32
335@itemx rd &= rs
336@itemx rd &= imm32
33732-bit bit-wise ``and'' operation.
338
339@item or32 rd, rs
340@itemx or32 rd, imm32
341@itemx rd |= rs
342@itemx rd |= imm32
34332-bit bit-wise ``or'' operation.
344
345@item xor32 rd, rs
346@itemx xor32 rd, imm32
347@itemx rd ^= rs
348@itemx rd ^= imm32
34932-bit bit-wise exclusive-or operation.
350
351@item lsh32 rd, rs
352@itemx lsh32 rd, imm32
353@itemx rd <<= rs
354@itemx rd <<= imm32
35532-bit left shift, by @code{rs} or @code{imm32} bits.
356
357@item rsh32 rd, rs
358@itemx rsh32 rd, imm32
359@itemx rd >>= rs
360@itemx rd >>= imm32
36132-bit right logical shift, by @code{rs} or @code{imm32} bits.
362
363@item arsh32 rd, rs
364@itemx arsh32 rd, imm32
365@itemx rd s>>= rs
366@itemx rd s>>= imm32
36732-bit right arithmetic shift, by @code{rs} or @code{imm32} bits.
368
369@item neg32 rd
370@itemx rd = - rd
37132-bit arithmetic negation.
372
373@item mov32 rd, rs
374@itemx mov32 rd, imm32
375@itemx rd = rs
376@itemx rd = imm32
377Move the 32-bit value of @code{rs} in @code{rd}, or load @code{imm32}
378in @code{rd}.
379
380@item mov32s rd, rs, 8
381@itemx rd = (s8) rs
382Move the sign-extended 8-bit value in @code{rs} to @code{rd}.
383
384@item mov32s rd, rs, 16
385@itemx rd = (s16) rs
386Move the sign-extended 16-bit value in @code{rs} to @code{rd}.
387
388@item mov32s rd, rs, 32
389@itemx rd = (s32) rs
390Move the sign-extended 32-bit value in @code{rs} to @code{rd}.
391@end table
392
393@subsection Endianness conversion instructions
394
395@table @code
396@item endle rd, 16
397@itemx endle rd, 32
398@itemx endle rd, 64
399@itemx rd = le16 rd
400@itemx rd = le32 rd
401@itemx rd = le64 rd
402Convert the 16-bit, 32-bit or 64-bit value in @code{rd} to
403little-endian and store it back in @code{rd}.
404@item endbe %d, 16
405@itemx endbe %d, 32
406@itemx endbe %d, 64
407@itemx rd = be16 rd
408@itemx rd = be32 rd
409@itemx rd = be64 rd
410Convert the 16-bit, 32-bit or 64-bit value in @code{rd} to big-endian
411and store it back in @code{rd}.
412@end table
413
414@subsection Byte swap instructions
415
416@table @code
417@item bswap rd, 16
418@itemx rd = bswap16 rd
419Swap the least-significant 16-bit word in @code{rd} with the
420most-significant 16-bit word.
421
422@item bswap rd, 32
423@itemx rd = bswap32 rd
424Swap the least-significant 32-bit word in @code{rd} with the
425most-significant 32-bit word.
426
427@item bswap rd, 64
428@itemx rd = bswap64 rd
429Swap the least-significant 64-bit word in @code{rd} with the
430most-significant 64-bit word.
431@end table
432
433
434@subsection 64-bit load and pseudo maps
435
436@table @code
437@item lddw rd, imm64
438@itemx rd = imm64 ll
439Load the given signed 64-bit immediate to the destination register
440@code{rd}.
441@end table
442
443@subsection Load instructions for socket filters
444
445The following instructions are intended to be used in socket filters,
446and are therefore not general-purpose: they make assumptions on the
447contents of several registers.  See the file
448@file{Documentation/networking/filter.txt} in the Linux kernel source
449tree for more information.
450
451Absolute loads:
452
453@table @code
454@item ldabsdw imm32
455@itemx r0 = *(u64 *) skb[imm32]
456Absolute 64-bit load.
457
458@item ldabsw imm32
459@itemx r0 = *(u32 *) skb[imm32]
460Absolute 32-bit load.
461
462@item ldabsh imm32
463@itemx r0 = *(u16 *) skb[imm32]
464Absolute 16-bit load.
465
466@item ldabsb imm32
467@itemx r0 = *(u8 *) skb[imm32]
468Absolute 8-bit load.
469@end table
470
471Indirect loads:
472
473@table @code
474@item ldinddw rs, imm32
475@itemx r0 = *(u64 *) skb[rs + imm32]
476Indirect 64-bit load.
477
478@item ldindw rs, imm32
479@itemx r0 = *(u32 *) skb[rs + imm32]
480Indirect 32-bit load.
481
482@item ldindh rs, imm32
483@itemx r0 = *(u16 *) skb[rs + imm32]
484Indirect 16-bit load.
485
486@item ldindb %s, imm32
487@itemx r0 = *(u8 *) skb[rs + imm32]
488Indirect 8-bit load.
489@end table
490
491@subsection Generic load/store instructions
492
493General-purpose load and store instructions are provided for several
494word sizes.
495
496Load to register instructions:
497
498@table @code
499@item ldxdw rd, [rs + offset16]
500@itemx rd = *(u64 *) (rs + offset16)
501Generic 64-bit load.
502
503@item ldxw rd, [rs + offset16]
504@itemx rd = *(u32 *) (rs + offset16)
505Generic 32-bit load.
506
507@item ldxh rd, [rs + offset16]
508@itemx rd = *(u16 *) (rs + offset16)
509Generic 16-bit load.
510
511@item ldxb rd, [rs + offset16]
512@itemx rd = *(u8 *) (rs + offset16)
513Generic 8-bit load.
514@end table
515
516Signed load to register instructions:
517
518@table @code
519@item ldxsdw rd, [rs + offset16]
520@itemx rd = *(s64 *) (rs + offset16)
521Generic 64-bit signed load.
522
523@item ldxsw rd, [rs + offset16]
524@itemx rd = *(s32 *) (rs + offset16)
525Generic 32-bit signed load.
526
527@item ldxsh rd, [rs + offset16]
528@itemx rd = *(s16 *) (rs + offset16)
529Generic 16-bit signed load.
530
531@item ldxsb rd, [rs + offset16]
532@itemx rd = *(s8 *) (rs + offset16)
533Generic 8-bit signed load.
534@end table
535
536Store from register instructions:
537
538@table @code
539@item stxdw [rd + offset16], %s
540@itemx *(u64 *) (rd + offset16)
541Generic 64-bit store.
542
543@item stxw [rd + offset16], %s
544@itemx *(u32 *) (rd + offset16)
545Generic 32-bit store.
546
547@item stxh [rd + offset16], %s
548@itemx *(u16 *) (rd + offset16)
549Generic 16-bit store.
550
551@item stxb [rd + offset16], %s
552@itemx *(u8 *) (rd + offset16)
553Generic 8-bit store.
554@end table
555
556Store from immediates instructions:
557
558@table @code
559@item stdw [rd + offset16], imm32
560@itemx *(u64 *) (rd + offset16) = imm32
561Store immediate as 64-bit.
562
563@item stw [rd + offset16], imm32
564@itemx *(u32 *) (rd + offset16) = imm32
565Store immediate as 32-bit.
566
567@item sth [rd + offset16], imm32
568@itemx *(u16 *) (rd + offset16) = imm32
569Store immediate as 16-bit.
570
571@item stb [rd + offset16], imm32
572@itemx *(u8 *) (rd + offset16) = imm32
573Store immediate as 8-bit.
574@end table
575
576@subsection Jump instructions
577
578eBPF provides the following compare-and-jump instructions, which
579compare the values of the two given registers, or the values of a
580register and an immediate, and perform a branch in case the comparison
581holds true.
582
583@table @code
584@item ja disp16
585@itemx goto disp16
586Jump-always.
587
588@item jal disp32
589@itemx gotol disp32
590Jump-always, long range.
591
592@item jeq rd, rs, disp16
593@itemx jeq rd, imm32, disp16
594@itemx if rd == rs goto disp16
595@itemx if rd == imm32 goto disp16
596Jump if equal, unsigned.
597
598@item jgt rd, rs, disp16
599@itemx jgt rd, imm32, disp16
600@itemx if rd > rs goto disp16
601@itemx if rd > imm32 goto disp16
602Jump if greater, unsigned.
603
604@item jge rd, rs, disp16
605@itemx jge rd, imm32, disp16
606@itemx if rd >= rs goto disp16
607@itemx if rd >= imm32 goto disp16
608Jump if greater or equal.
609
610@item jlt rd, rs, disp16
611@itemx jlt rd, imm32, disp16
612@itemx if rd < rs goto disp16
613@itemx if rd < imm32 goto disp16
614Jump if lesser.
615
616@item jle rd , rs, disp16
617@itemx jle rd, imm32, disp16
618@itemx if rd <= rs goto disp16
619@itemx if rd <= imm32 goto disp16
620Jump if lesser or equal.
621
622@item jset rd, rs, disp16
623@itemx jset rd, imm32, disp16
624@itemx if rd & rs goto disp16
625@itemx if rd & imm32 goto disp16
626Jump if signed equal.
627
628@item jne rd, rs, disp16
629@itemx jne rd, imm32, disp16
630@itemx if rd != rs goto disp16
631@itemx if rd != imm32 goto disp16
632Jump if not equal.
633
634@item jsgt rd, rs, disp16
635@itemx jsgt rd, imm32, disp16
636@itemx if rd s> rs goto disp16
637@itemx if rd s> imm32 goto disp16
638Jump if signed greater.
639
640@item jsge rd, rs, disp16
641@itemx jsge rd, imm32, disp16
642@itemx if rd s>= rd goto disp16
643@itemx if rd s>= imm32 goto disp16
644Jump if signed greater or equal.
645
646@item jslt rd, rs, disp16
647@itemx jslt rd, imm32, disp16
648@itemx if rd s< rs goto disp16
649@itemx if rd s< imm32 goto disp16
650Jump if signed lesser.
651
652@item jsle rd, rs, disp16
653@itemx jsle rd, imm32, disp16
654@itemx if rd s<= rs goto disp16
655@itemx if rd s<= imm32 goto disp16
656Jump if signed lesser or equal.
657@end table
658
659A call instruction is provided in order to perform calls to other eBPF
660functions, or to external kernel helpers:
661
662@table @code
663@item call disp32
664@item call imm32
665Jump and link to the offset @emph{disp32}, or to the kernel helper
666function identified by @emph{imm32}.
667@end table
668
669Finally:
670
671@table @code
672@item exit
673Terminate the eBPF program.
674@end table
675
676@subsection 32-bit jump instructions
677
678eBPF provides the following compare-and-jump instructions, which
679compare the 32-bit values of the two given registers, or the values of
680a register and an immediate, and perform a branch in case the
681comparison holds true.
682
683These instructions are only available in BPF v3 or later.
684
685@table @code
686@item jeq32 rd, rs, disp16
687@itemx jeq32 rd, imm32, disp16
688@itemx if rd == rs goto disp16
689@itemx if rd == imm32 goto disp16
690Jump if equal, unsigned.
691
692@item jgt32 rd, rs, disp16
693@itemx jgt32 rd, imm32, disp16
694@itemx if rd > rs goto disp16
695@itemx if rd > imm32 goto disp16
696Jump if greater, unsigned.
697
698@item jge32 rd, rs, disp16
699@itemx jge32 rd, imm32, disp16
700@itemx if rd >= rs goto disp16
701@itemx if rd >= imm32 goto disp16
702Jump if greater or equal.
703
704@item jlt32 rd, rs, disp16
705@itemx jlt32 rd, imm32, disp16
706@itemx if rd < rs goto disp16
707@itemx if rd < imm32 goto disp16
708Jump if lesser.
709
710@item jle32 rd , rs, disp16
711@itemx jle32 rd, imm32, disp16
712@itemx if rd <= rs goto disp16
713@itemx if rd <= imm32 goto disp16
714Jump if lesser or equal.
715
716@item jset32 rd, rs, disp16
717@itemx jset32 rd, imm32, disp16
718@itemx if rd & rs goto disp16
719@itemx if rd & imm32 goto disp16
720Jump if signed equal.
721
722@item jne32 rd, rs, disp16
723@itemx jne32 rd, imm32, disp16
724@itemx if rd != rs goto disp16
725@itemx if rd != imm32 goto disp16
726Jump if not equal.
727
728@item jsgt32 rd, rs, disp16
729@itemx jsgt32 rd, imm32, disp16
730@itemx if rd s> rs goto disp16
731@itemx if rd s> imm32 goto disp16
732Jump if signed greater.
733
734@item jsge32 rd, rs, disp16
735@itemx jsge32 rd, imm32, disp16
736@itemx if rd s>= rd goto disp16
737@itemx if rd s>= imm32 goto disp16
738Jump if signed greater or equal.
739
740@item jslt32 rd, rs, disp16
741@itemx jslt32 rd, imm32, disp16
742@itemx if rd s< rs goto disp16
743@itemx if rd s< imm32 goto disp16
744Jump if signed lesser.
745
746@item jsle32 rd, rs, disp16
747@itemx jsle32 rd, imm32, disp16
748@itemx if rd s<= rs goto disp16
749@itemx if rd s<= imm32 goto disp16
750Jump if signed lesser or equal.
751@end table
752
753@subsection Atomic instructions
754
755Atomic exchange instructions are provided in two flavors: one for
756compare-and-swap, one for unconditional exchange.
757
758@table @code
759@item acmp [rd + offset16], rs
760@itemx r0 = cmpxchg_64 (rd + offset16, r0, rs)
761Atomic compare-and-swap.  Compares value in @code{r0} to value
762addressed by @code{rd + offset16}.  On match, the value addressed by
763@code{rd + offset16} is replaced with the value in @code{rs}.
764Regardless, the value that was at @code{rd + offset16} is
765zero-extended and loaded into @code{r0}.
766
767@item axchg [rd + offset16], rs
768@itemx rs = xchg_64 (rd + offset16, rs)
769Atomic exchange.  Atomically exchanges the value in @code{rs} with
770the value addressed by @code{rd + offset16}.
771@end table
772
773@noindent
774The following instructions provide atomic arithmetic operations.
775
776@table @code
777@item aadd [rd + offset16], rs
778@itemx lock *(u64 *)(rd + offset16) = rs
779Atomic add instruction.
780
781@item aor [rd + offset16], rs
782@itemx lock *(u64 *) (rd + offset16) |= rs
783Atomic or instruction.
784
785@item aand [rd + offset16], rs
786@itemx lock *(u64 *) (rd + offset16) &= rs
787Atomic and instruction.
788
789@item axor [rd + offset16], rs
790@itemx lock *(u64 *) (rd + offset16) ^= rs
791Atomic xor instruction.
792@end table
793
794@noindent
795The following variants perform fetching before the atomic operation.
796
797@table @code
798@item afadd [rd + offset16], rs
799@itemx rs = atomic_fetch_add ((u64 *)(rd + offset16), rs)
800Atomic fetch-and-add instruction.
801
802@item afor [rd + offset16], rs
803@itemx rs = atomic_fetch_or ((u64 *)(rd + offset16), rs)
804Atomic fetch-and-or instruction.
805
806@item afand [rd + offset16], rs
807@itemx rs = atomic_fetch_and ((u64 *)(rd + offset16), rs)
808Atomic fetch-and-and instruction.
809
810@item afxor [rd + offset16], rs
811@itemx rs = atomic_fetch_xor ((u64 *)(rd + offset16), rs)
812Atomic fetch-and-or instruction.
813@end table
814
815The above instructions were introduced in the V3 of the BPF
816instruction set.  The following instruction is supported for backwards
817compatibility:
818
819@table @code
820@item xadddw [rd + offset16], rs
821Alias to @code{aadd}.
822@end table
823
824@subsection 32-bit atomic instructions
825
82632-bit atomic exchange instructions are provided in two flavors: one
827for compare-and-swap, one for unconditional exchange.
828
829@table @code
830@item acmp32 [rd + offset16], rs
831@itemx w0 = cmpxchg32_32 (rd + offset16, w0, ws)
832Atomic compare-and-swap.  Compares value in @code{w0} to value
833addressed by @code{rd + offset16}.  On match, the value addressed by
834@code{rd + offset16} is replaced with the value in @code{ws}.
835Regardless, the value that was at @code{rd + offset16} is
836zero-extended and loaded into @code{w0}.
837
838@item axchg [rd + offset16], rs
839@itemx ws = xchg32_32 (rd + offset16, ws)
840Atomic exchange.  Atomically exchanges the value in @code{ws} with
841the value addressed by @code{rd + offset16}.
842@end table
843
844@noindent
845The following instructions provide 32-bit atomic arithmetic operations.
846
847@table @code
848@item aadd32 [rd + offset16], rs
849@itemx lock *(u32 *)(rd + offset16) = rs
850Atomic add instruction.
851
852@item aor32 [rd + offset16], rs
853@itemx lock *(u32 *) (rd + offset16) |= rs
854Atomic or instruction.
855
856@item aand32 [rd + offset16], rs
857@itemx lock *(u32 *) (rd + offset16) &= rs
858Atomic and instruction.
859
860@item axor32 [rd + offset16], rs
861@itemx lock *(u32 *) (rd + offset16) ^= rs
862Atomic xor instruction
863@end table
864
865@noindent
866The following variants perform fetching before the atomic operation.
867
868@table @code
869@item afadd32 [dr + offset16], rs
870@itemx ws = atomic_fetch_add ((u32 *)(rd + offset16), ws)
871Atomic fetch-and-add instruction.
872
873@item afor32 [dr + offset16], rs
874@itemx ws = atomic_fetch_or ((u32 *)(rd + offset16), ws)
875Atomic fetch-and-or instruction.
876
877@item afand32 [dr + offset16], rs
878@itemx ws = atomic_fetch_and ((u32 *)(rd + offset16), ws)
879Atomic fetch-and-and instruction.
880
881@item afxor32 [dr + offset16], rs
882@itemx ws = atomic_fetch_xor ((u32 *)(rd + offset16), ws)
883Atomic fetch-and-or instruction
884@end table
885
886The above instructions were introduced in the V3 of the BPF
887instruction set.  The following instruction is supported for backwards
888compatibility:
889
890@table @code
891@item xaddw [rd + offset16], rs
892Alias to @code{aadd32}.
893@end table
894