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