]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/doc/c-bpf.texi
gas: add support for eBPF
[thirdparty/binutils-gdb.git] / gas / doc / c-bpf.texi
CommitLineData
f8861f5d
JM
1@c Copyright (C) 2019 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:: Options
19* BPF Syntax:: Syntax
20* BPF Opcodes:: Opcodes
21@end menu
22
23@node BPF Options
24@section Options
25@cindex BPF options (none)
26@cindex options for BPF (none)
27
28@c man begin OPTIONS
29@table @gcctabopt
30
31@cindex @option{-EB} command-line option, BPF
32@item -EB
33This option specifies that the assembler should emit big-endian eBPF.
34
35@cindex @option{-EL} command-line option, BPF
36@item -EL
37This option specifies that the assembler should emit little-endian
38eBPF.
39@end table
40
41Note that if no endianness option is specified in the command line,
42the host endianness is used.
43@c man end
44
45@node BPF Syntax
46@section Syntax
47@menu
48* BPF-Chars:: Special Characters
49* BPF-Regs:: Register Names
50* BPF-Pseudo-Maps:: Pseudo map fds
51@end menu
52
53@node BPF-Chars
54@subsection Special Characters
55
56@cindex line comment character, BPF
57@cindex BPF line comment character
58The presence of a @samp{;} on a line indicates the start of a comment
59that extends to the end of the current line. If a @samp{#} appears as
60the first character of a line, the whole line is treated as a comment.
61
62@cindex statement separator, BPF
63Statements and assembly directives are separated by newlines.
64
65@node BPF-Regs
66@subsection Register Names
67
68@cindex BPF register names
69@cindex register names, BPF
70The eBPF processor provides ten general-purpose 64-bit registers,
71which are read-write, and a read-only frame pointer register:
72
73@table @samp
74@item %r0 .. %r9
75General-purpose registers.
76@item %r10
77Frame pointer register.
78@end table
79
80Some registers have additional names, to reflect their role in the
81eBPF ABI:
82
83@table @samp
84@item %a
85This is @samp{%r0}.
86@item %ctx
87This is @samp{%r6}.
88@item %fp
89This is @samp{%r10}.
90@end table
91
92@node BPF-Pseudo-Maps
93@subsection Pseudo Maps
94
95@cindex pseudo map fd, BPF
96The @samp{LDDW} instruction can take a literal pseudo map file
97descriptor as its second argument. This uses the syntax
98@samp{%map_fd(N)} where @samp{N} is a signed number.
99
100For example, to load the address of the pseudo map with file
101descriptor @samp{2} in register @samp{r1} we would do:
102
103@smallexample
104 lddw %r1, %map_fd(2)
105@end smallexample
106
107@node BPF Opcodes
108@section Opcodes
109
110@cindex BPF opcodes
111@cindex opcodes for BPF
112In the instruction descriptions below the following field descriptors
113are used:
114
115@table @code
116@item %d
117Destination general-purpose register whose role is to be destination
118of an operation.
119@item %s
120Source general-purpose register whose role is to be the source of an
121operation.
122@item disp16
12316-bit signed PC-relative offset, measured in number of 64-bit words,
124minus one.
125@item disp32
12632-bit signed PC-relative offset, measured in number of 64-bit words,
127minus one.
128@item offset16
129Signed 16-bit immediate.
130@item imm32
131Signed 32-bit immediate.
132@item imm64
133Signed 64-bit immediate.
134@end table
135
136@subsubsection Arithmetic instructions
137
138The destination register in these instructions act like an
139accumulator.
140
141@table @code
142@item add %d, (%s|imm32)
14364-bit arithmetic addition.
144@item sub %d, (%s|imm32)
14564-bit arithmetic subtraction.
146@item mul %d, (%s|imm32)
14764-bit arithmetic multiplication.
148@item div %d, (%s|imm32)
14964-bit arithmetic integer division.
150@item mod %d, (%s|imm32)
15164-bit integer remainder.
152@item and %d, (%s|imm32)
15364-bit bit-wise ``and'' operation.
154@item or %d, (%s|imm32)
15564-bit bit-wise ``or'' operation.
156@item xor %d, (%s|imm32)
15764-bit bit-wise exclusive-or operation.
158@item lsh %d, (%s|imm32)
15964-bit left shift, by @code{%s} or @code{imm32} bits.
160@item rsh %d, (%s|imm32)
16164-bit right logical shift, by @code{%s} or @code{imm32} bits.
162@item arsh %d, (%s|imm32)
16364-bit right arithmetic shift, by @code{%s} or @code{imm32} bits.
164@item neg %d
16564-bit arithmetic negation.
166@item mov %d, (%s|imm32)
167Move the 64-bit value of @code{%s} in @code{%d}, or load @code{imm32}
168in @code{%d}.
169@end table
170
171@subsubsection 32-bit arithmetic instructions
172
173The destination register in these instructions act as an accumulator.
174
175@table @code
176@item add32 %d, (%s|imm32)
17732-bit arithmetic addition.
178@item sub32 %d, (%s|imm32)
17932-bit arithmetic subtraction.
180@item mul32 %d, (%s|imm32)
18132-bit arithmetic multiplication.
182@item div32 %d, (%s|imm32)
18332-bit arithmetic integer division.
184@item mod32 %d, (%s|imm32)
18532-bit integer remainder.
186@item and32 %d, (%s|imm32)
18732-bit bit-wise ``and'' operation.
188@item or32 %d, (%s|imm32)
18932-bit bit-wise ``or'' operation.
190@item xor32 %d, (%s|imm32)
19132-bit bit-wise exclusive-or operation.
192@item lsh32 %d, (%s|imm32)
19332-bit left shift, by @code{%s} or @code{imm32} bits.
194@item rsh32 %d, (%s|imm32)
19532-bit right logical shift, by @code{%s} or @code{imm32} bits.
196@item arsh32 %d, (%s|imm32)
19732-bit right arithmetic shift, by @code{%s} or @code{imm32} bits.
198@item neg32 %d
19932-bit arithmetic negation.
200@item mov32 %d, (%s|imm32)
201Move the 32-bit value of @code{%s} in @code{%d}, or load @code{imm32}
202in @code{%d}.
203@end table
204
205@subsubsection Endianness conversion instructions
206
207@table @code
208@item endle %d, (8|16|32)
209Convert the 8-bit, 16-bit or 32-bit value in @code{%d} to
210little-endian.
211@item endbe %d, (8|16|32)
212Convert the 8-bit, 16-bit or 32-bit value in @code{%d} to big-endian.
213@end table
214
215@subsubsection 64-bit load and pseudo maps
216
217@table @code
218@item lddw %d, imm64
219Load the given signed 64-bit immediate, or pseudo map descriptor, to
220the destination register @code{%d}.
221@item lddw %d, %map_fd(N)
222Load the address of the given pseudo map fd @emph{N} to the
223destination register @code{%d}.
224@end table
225
226@subsubsection Load instructions for socket filters
227
228The following instructions are intended to be used in socket filters,
229and are therefore not general-purpose: they make assumptions on the
230contents of several registers. See the file
231@file{Documentation/networking/filter.txt} in the Linux kernel source
232tree for more information.
233
234Absolute loads:
235
236@table @code
237@item ldabsdw %d, %s, imm32
238Absolute 64-bit load.
239@item ldabsw %d, %s, imm32
240Absolute 32-bit load.
241@item ldabsh %d, %s, imm32
242Absolute 16-bit load.
243@item ldabsb %d, %s, imm32
244Absolute 8-bit load.
245@end table
246
247Indirect loads:
248
249@table @code
250@item ldinddw %d, %s, imm32
251Indirect 64-bit load.
252@item ldindw %d, %s, imm32
253Indirect 32-bit load.
254@item ldindh %d, %s, imm32
255Indirect 16-bit load.
256@item ldindb %d, %s, imm32
257Indirect 8-bit load.
258@end table
259
260@subsubsection Generic load/store instructions
261
262General-purpose load and store instructions are provided for several
263word sizes.
264
265Load to register instructions:
266
267@table @code
268@item ldxdw %d, [%s+offset16]
269Generic 64-bit load.
270@item ldxw %d, [%s+offset16]
271Generic 32-bit load.
272@item ldxh %d, [%s+offset16]
273Generic 16-bit load.
274@item ldxb %d, [%s+offset16]
275Generic 8-bit load.
276@end table
277
278Store from register instructions:
279
280@table @code
281@item stxdw [%d+offset16], %s
282Generic 64-bit store.
283@item stxw [%d+offset16], %s
284Generic 32-bit store.
285@item stxh [%d+offset16], %s
286Generic 16-bit store.
287@item stxb [%d+offset16], %s
288Generic 8-bit store.
289@end table
290
291Store from immediates instructions:
292
293@table @code
294@item stddw [%d+offset16], imm32
295Store immediate as 64-bit.
296@item stdw [%d+offset16], imm32
297Store immediate as 32-bit.
298@item stdh [%d+offset16], imm32
299Store immediate as 16-bit.
300@item stdb [%d+offset16], imm32
301Store immediate as 8-bit.
302@end table
303
304@subsubsection Jump instructions
305
306eBPF provides the following compare-and-jump instructions, which
307compare the values of the two given registers, or the values of a
308register and an immediate, and perform a branch in case the comparison
309holds true.
310
311@table @code
312@item ja %d,(%s|imm32),disp16
313Jump-always.
314@item jeq %d,(%s|imm32),disp16
315Jump if equal.
316@item jgt %d,(%s|imm32),disp16
317Jump if greater.
318@item jge %d,(%s|imm32),disp16
319Jump if greater or equal.
320@item jlt %d,(%s|imm32),disp16
321Jump if lesser.
322@item jle %d,(%s|imm32),disp16
323Jump if lesser or equal.
324@item jset %d,(%s|imm32),disp16
325Jump if signed equal.
326@item jne %d,(%s|imm32),disp16
327Jump if not equal.
328@item jsgt %d,(%s|imm32),disp16
329Jump if signed greater.
330@item jsge %d,(%s|imm32),disp16
331Jump if signed greater or equal.
332@item jslt %d,(%s|imm32),disp16
333Jump if signed lesser.
334@item jsle %d,(%s|imm32),disp16
335Jump if signed lesser or equal.
336@end table
337
338A call instruction is provided in order to perform calls to other eBPF
339functions, or to external kernel helpers:
340
341@table @code
342@item call (disp32|imm32)
343Jump and link to the offset @emph{disp32}, or to the kernel helper
344function identified by @emph{imm32}.
345@end table
346
347Finally:
348
349@table @code
350@item exit
351Terminate the eBPF program.
352@end table
353
354@subsubsection Atomic instructions
355
356Atomic exchange-and-add instructions are provided in two flavors: one
357for swapping 64-bit quantities and another for 32-bit quantities.
358
359@table @code
360@item xadddw [%d+offset16],%s
361Exchange-and-add a 64-bit value at the specified location.
362@item xaddw [%d+offset16],%s
363Exchange-and-add a 32-bit value at the specified location.
364@end table