Skip to content

Commit 02f68ef

Browse files
committed
bpf: Add atomic compare-and-exchange instructions
This patch adds the two remaining BPF v3 atomic instructions: - BPF_INSN_ACMP{,32}: atomic compare-and-swap - BPF_INSN_AXCHG{,32}: atomic (non-conditional) exchange Tests and documentation are also updated. gas/ * doc/c-bpf.texi (BPF Instructions): Document atomic exchange and atomic compare-and-swap instructions. * testsuite/gas/bpf/atomic.s: Test ACMP, ACMP32, AXCHG, AXCGH32 instructions. * testsuite/gas/bpf/atomic.d: Likewise. * testsuite/gas/bpf/atomic-be.d: Likewise. * testsuite/gas/bpf/atomic-pseudoc.s: Likewise. * testsuite/gas/bpf/atomic-pseudoc.d: Likewise. * testsuite/gas/bpf/atomic-be-pseudoc.d: Likewise. include/ * opcode/bpf.h (BPF_IMM32_ACMP): Fix typo. (enum bpf_insn_id): New entries for BPF_INSN_ACMP{,32} and BPF_INSN_AXCHG{,32}. opcodes/ * bpf-opc.c (bpf_opcodes): Add entries for ACMP{,32} and AXCHG{,32} instructions.
1 parent 055a4c8 commit 02f68ef

File tree

9 files changed

+82
-5
lines changed

9 files changed

+82
-5
lines changed

gas/doc/c-bpf.texi

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -745,8 +745,26 @@ Jump if signed lesser or equal.
745745

746746
@subsection Atomic instructions
747747

748-
Atomic exchange-and-add instructions are provided in two flavors: one
749-
for swapping 64-bit quantities and another for 32-bit quantities.
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.
750768

751769
@table @code
752770
@item aadd [rd + offset16], rs
@@ -798,8 +816,26 @@ Alias to @code{aadd}.
798816

799817
@subsection 32-bit atomic instructions
800818

801-
Atomic exchange-and-add instructions are provided in two flavors: one
802-
for swapping 32-bit quantities and another for 32-bit quantities.
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.
803839

804840
@table @code
805841
@item aadd32 [rd + offset16], rs

gas/testsuite/gas/bpf/atomic-be-pseudoc.d

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ Disassembly of section .text:
2626
78: c3 12 1e ef 00 00 00 41 w2=atomic_fetch_or\(\(u32\*\)\(r1\+0x1eef\),w2\)
2727
80: db 12 1e ef 00 00 00 a1 r2=atomic_fetch_xor\(\(u64\*\)\(r1\+0x1eef\),r2\)
2828
88: c3 12 1e ef 00 00 00 a1 w2=atomic_fetch_xor\(\(u32\*\)\(r1\+0x1eef\),w2\)
29+
90: db 12 00 04 00 00 00 f1 r0=cmpxchg_64\(r1\+0x4,r0,r2\)
30+
98: c3 23 00 04 00 00 00 f1 w0=cmpxchg32_32\(r2\+0x4,w0,w3\)
31+
a0: db 12 00 08 00 00 00 e1 r2=xchg_64\(r1\+0x8,r2\)
32+
a8: c3 13 00 08 00 00 00 e1 w3=xchg32_32\(r1\+0x8,w3\)

gas/testsuite/gas/bpf/atomic-be.d

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ Disassembly of section .text:
2424
68: c3 12 1e ef 00 00 00 41 afor32 \[%r1\+0x1eef\],%r2
2525
70: db 12 1e ef 00 00 00 a1 afxor \[%r1\+0x1eef\],%r2
2626
78: c3 12 1e ef 00 00 00 a1 afxor32 \[%r1\+0x1eef\],%r2
27+
80: db 12 00 04 00 00 00 f1 acmp \[%r1\+0x4\],%r2
28+
88: c3 23 00 04 00 00 00 f1 acmp32 \[%r2\+0x4\],%r3
29+
90: db 12 00 08 00 00 00 e1 axchg \[%r1\+0x8\],%r2
30+
98: c3 13 00 08 00 00 00 e1 axchg32 \[%r1\+0x8\],%r3

gas/testsuite/gas/bpf/atomic-pseudoc.d

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ Disassembly of section .text:
2626
78: c3 21 ef 1e 41 00 00 00 w2=atomic_fetch_or\(\(u32\*\)\(r1\+0x1eef\),w2\)
2727
80: db 21 ef 1e a1 00 00 00 r2=atomic_fetch_xor\(\(u64\*\)\(r1\+0x1eef\),r2\)
2828
88: c3 21 ef 1e a1 00 00 00 w2=atomic_fetch_xor\(\(u32\*\)\(r1\+0x1eef\),w2\)
29+
90: db 21 04 00 f1 00 00 00 r0=cmpxchg_64\(r1\+0x4,r0,r2\)
30+
98: c3 32 04 00 f1 00 00 00 w0=cmpxchg32_32\(r2\+0x4,w0,w3\)
31+
a0: db 21 08 00 e1 00 00 00 r2=xchg_64\(r1\+0x8,r2\)
32+
a8: c3 31 08 00 e1 00 00 00 w3=xchg32_32\(r1\+0x8,w3\)

gas/testsuite/gas/bpf/atomic-pseudoc.s

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@
1818
w2 = atomic_fetch_or((u32*)(r1+0x1eef),w2)
1919
r2 = atomic_fetch_xor((u64*)(r1+0x1eef),r2)
2020
w2 = atomic_fetch_xor((u32*)(r1+0x1eef),w2)
21+
r0 = cmpxchg_64(r1+0x4,r0,r2)
22+
w0 = cmpxchg32_32(r2+0x4,w0,w3)
23+
r2 = xchg_64(r1+0x8,r2)
24+
w3 = xchg32_32(r1+0x8,w3)

gas/testsuite/gas/bpf/atomic.d

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ Disassembly of section .text:
2424
68: c3 21 ef 1e 41 00 00 00 afor32 \[%r1\+0x1eef\],%r2
2525
70: db 21 ef 1e a1 00 00 00 afxor \[%r1\+0x1eef\],%r2
2626
78: c3 21 ef 1e a1 00 00 00 afxor32 \[%r1\+0x1eef\],%r2
27+
80: db 21 04 00 f1 00 00 00 acmp \[%r1\+0x4\],%r2
28+
88: c3 32 04 00 f1 00 00 00 acmp32 \[%r2\+0x4\],%r3
29+
90: db 21 08 00 e1 00 00 00 axchg \[%r1\+0x8\],%r2
30+
98: c3 31 08 00 e1 00 00 00 axchg32 \[%r1\+0x8\],%r3

gas/testsuite/gas/bpf/atomic.s

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@
1717
afor32 [%r1+0x1eef], %r2
1818
afxor [%r1+0x1eef], %r2
1919
afxor32 [%r1+0x1eef], %r2
20+
21+
acmp [%r1+4], %r2
22+
acmp32 [%r2+4], %r3
23+
axchg [%r1+8], %r2
24+
axchg32 [%r1+8], %r3

include/opcode/bpf.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ typedef uint64_t bpf_insn_word;
153153
#define BPF_IMM32_AFAND ((uint64_t)0x00000051)
154154
#define BPF_IMM32_AFXOR ((uint64_t)0x000000a1)
155155
#define BPF_IMM32_AXCHG ((uint64_t)0x000000e1)
156-
#define BPF_IMM32_ACMP ((uint64_t)b0x000000f1)
156+
#define BPF_IMM32_ACMP ((uint64_t)0x000000f1)
157157

158158
/* Unique identifiers for BPF instructions. */
159159

@@ -225,6 +225,10 @@ enum bpf_insn_id
225225
BPF_INSN_AADD32, BPF_INSN_AOR32, BPF_INSN_AAND32, BPF_INSN_AXOR32,
226226
/* Atomic instructions with fetching (32-bit.) */
227227
BPF_INSN_AFADD32, BPF_INSN_AFOR32, BPF_INSN_AFAND32, BPF_INSN_AFXOR32,
228+
/* Atomic compare-and-swap, atomic exchange. */
229+
BPF_INSN_ACMP, BPF_INSN_AXCHG,
230+
/* Atomic compare-and-swap, atomic exchange (32-bit). */
231+
BPF_INSN_ACMP32, BPF_INSN_AXCHG32,
228232
/* GNU simulator specific instruction. */
229233
BPF_INSN_BRKPT,
230234
};

opcodes/bpf-opc.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,18 @@ const struct bpf_opcode bpf_opcodes[] =
403403
{BPF_INSN_AFXOR32, "afxor32%W[ %dr %o16 ] , %sr", "%sw = atomic_fetch_xor ( ( u32 * ) ( %dr %o16 ) , %sw )",
404404
BPF_V3, BPF_CODE|BPF_IMM32, BPF_CLASS_STX|BPF_SIZE_W|BPF_MODE_ATOMIC|BPF_IMM32_AFXOR},
405405

406+
/* Atomic compare-and-swap, atomic exchange. */
407+
{BPF_INSN_ACMP, "acmp%W[ %dr %o16 ] , %sr", "r0 = cmpxchg_64 ( %dr %o16 , r0 , %sr )",
408+
BPF_V3, BPF_CODE|BPF_IMM32, BPF_CLASS_STX|BPF_SIZE_DW|BPF_MODE_ATOMIC|BPF_IMM32_ACMP},
409+
{BPF_INSN_AXCHG, "axchg%W[ %dr %o16 ] , %sr", "%sr = xchg_64 ( %dr %o16 , %sr )",
410+
BPF_V3, BPF_CODE|BPF_IMM32, BPF_CLASS_STX|BPF_SIZE_DW|BPF_MODE_ATOMIC|BPF_IMM32_AXCHG},
411+
412+
/* Atomic compare-and-swap, atomic exchange (32-bit). */
413+
{BPF_INSN_ACMP32, "acmp32%W[ %dr %o16 ], %sr", "w0 = cmpxchg32_32 ( %dr %o16 , w0 , %sw )",
414+
BPF_V3, BPF_CODE|BPF_IMM32, BPF_CLASS_STX|BPF_SIZE_W|BPF_MODE_ATOMIC|BPF_IMM32_ACMP},
415+
{BPF_INSN_AXCHG32, "axchg32%W[ %dr %o16 ], %sr", "%sw = xchg32_32 ( %dr %o16 , %sw )",
416+
BPF_V3, BPF_CODE|BPF_IMM32, BPF_CLASS_STX|BPF_SIZE_W|BPF_MODE_ATOMIC|BPF_IMM32_AXCHG},
417+
406418
/* Old versions of aadd and aadd32. */
407419
{BPF_INSN_AADD, "xadddw%W[ %dr %o16 ] , %sr", "* ( u64 * ) ( %dr %o16 ) += %sr",
408420
BPF_V1, BPF_CODE|BPF_IMM32, BPF_CLASS_STX|BPF_SIZE_DW|BPF_MODE_ATOMIC|BPF_IMM32_AADD},

0 commit comments

Comments
 (0)