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