hc
2023-11-06 15ade055295d13f95d49e3d99b09f3bbfb4a43e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- This file documents the GNU Assembler "as".
 
Copyright (C) 1991-2021 Free Software Foundation, Inc.
 
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3
or any later version published by the Free Software Foundation;
with no Invariant Sections, with no Front-Cover Texts, and with no
Back-Cover Texts.  A copy of the license is included in the
section entitled "GNU Free Documentation License".
 -->
<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
<head>
<title>Using as: BPF Opcodes</title>
 
<meta name="description" content="Using as: BPF Opcodes">
<meta name="keywords" content="Using as: BPF Opcodes">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="AS-Index.html#AS-Index" rel="index" title="AS Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="BPF_002dDependent.html#BPF_002dDependent" rel="up" title="BPF-Dependent">
<link href="CR16_002dDependent.html#CR16_002dDependent" rel="next" title="CR16-Dependent">
<link href="BPF-Directives.html#BPF-Directives" rel="previous" title="BPF Directives">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
 
 
</head>
 
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="BPF-Opcodes"></a>
<div class="header">
<p>
Previous: <a href="BPF-Directives.html#BPF-Directives" accesskey="p" rel="previous">BPF Directives</a>, Up: <a href="BPF_002dDependent.html#BPF_002dDependent" accesskey="u" rel="up">BPF-Dependent</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="AS-Index.html#AS-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Opcodes-5"></a>
<h4 class="subsection">9.7.4 Opcodes</h4>
 
<a name="index-BPF-opcodes"></a>
<a name="index-opcodes-for-BPF"></a>
<p>In the instruction descriptions below the following field descriptors
are used:
</p>
<dl compact="compact">
<dt><code>%d</code></dt>
<dd><p>Destination general-purpose register whose role is to be destination
of an operation.
</p></dd>
<dt><code>%s</code></dt>
<dd><p>Source general-purpose register whose role is to be the source of an
operation.
</p></dd>
<dt><code>disp16</code></dt>
<dd><p>16-bit signed PC-relative offset, measured in number of 64-bit words,
minus one.
</p></dd>
<dt><code>disp32</code></dt>
<dd><p>32-bit signed PC-relative offset, measured in number of 64-bit words,
minus one.
</p></dd>
<dt><code>offset16</code></dt>
<dd><p>Signed 16-bit immediate.
</p></dd>
<dt><code>imm32</code></dt>
<dd><p>Signed 32-bit immediate.
</p></dd>
<dt><code>imm64</code></dt>
<dd><p>Signed 64-bit immediate.
</p></dd>
</dl>
 
<a name="Arithmetic-instructions"></a>
<h4 class="subsubsection">9.7.4.1 Arithmetic instructions</h4>
 
<p>The destination register in these instructions act like an
accumulator.
</p>
<dl compact="compact">
<dt><code>add %d, (%s|imm32)</code></dt>
<dd><p>64-bit arithmetic addition.
</p></dd>
<dt><code>sub %d, (%s|imm32)</code></dt>
<dd><p>64-bit arithmetic subtraction.
</p></dd>
<dt><code>mul %d, (%s|imm32)</code></dt>
<dd><p>64-bit arithmetic multiplication.
</p></dd>
<dt><code>div %d, (%s|imm32)</code></dt>
<dd><p>64-bit arithmetic integer division.
</p></dd>
<dt><code>mod %d, (%s|imm32)</code></dt>
<dd><p>64-bit integer remainder.
</p></dd>
<dt><code>and %d, (%s|imm32)</code></dt>
<dd><p>64-bit bit-wise &ldquo;and&rdquo; operation.
</p></dd>
<dt><code>or %d, (%s|imm32)</code></dt>
<dd><p>64-bit bit-wise &ldquo;or&rdquo; operation.
</p></dd>
<dt><code>xor %d, (%s|imm32)</code></dt>
<dd><p>64-bit bit-wise exclusive-or operation.
</p></dd>
<dt><code>lsh %d, (%s|imm32)</code></dt>
<dd><p>64-bit left shift, by <code>%s</code> or <code>imm32</code> bits.
</p></dd>
<dt><code>rsh %d, (%s|imm32)</code></dt>
<dd><p>64-bit right logical shift, by <code>%s</code> or <code>imm32</code> bits.
</p></dd>
<dt><code>arsh %d, (%s|imm32)</code></dt>
<dd><p>64-bit right arithmetic shift, by <code>%s</code> or <code>imm32</code> bits.
</p></dd>
<dt><code>neg %d</code></dt>
<dd><p>64-bit arithmetic negation.
</p></dd>
<dt><code>mov %d, (%s|imm32)</code></dt>
<dd><p>Move the 64-bit value of <code>%s</code> in <code>%d</code>, or load <code>imm32</code>
in <code>%d</code>.
</p></dd>
</dl>
 
<a name="g_t32_002dbit-arithmetic-instructions"></a>
<h4 class="subsubsection">9.7.4.2 32-bit arithmetic instructions</h4>
 
<p>The destination register in these instructions act as an accumulator.
</p>
<dl compact="compact">
<dt><code>add32 %d, (%s|imm32)</code></dt>
<dd><p>32-bit arithmetic addition.
</p></dd>
<dt><code>sub32 %d, (%s|imm32)</code></dt>
<dd><p>32-bit arithmetic subtraction.
</p></dd>
<dt><code>mul32 %d, (%s|imm32)</code></dt>
<dd><p>32-bit arithmetic multiplication.
</p></dd>
<dt><code>div32 %d, (%s|imm32)</code></dt>
<dd><p>32-bit arithmetic integer division.
</p></dd>
<dt><code>mod32 %d, (%s|imm32)</code></dt>
<dd><p>32-bit integer remainder.
</p></dd>
<dt><code>and32 %d, (%s|imm32)</code></dt>
<dd><p>32-bit bit-wise &ldquo;and&rdquo; operation.
</p></dd>
<dt><code>or32 %d, (%s|imm32)</code></dt>
<dd><p>32-bit bit-wise &ldquo;or&rdquo; operation.
</p></dd>
<dt><code>xor32 %d, (%s|imm32)</code></dt>
<dd><p>32-bit bit-wise exclusive-or operation.
</p></dd>
<dt><code>lsh32 %d, (%s|imm32)</code></dt>
<dd><p>32-bit left shift, by <code>%s</code> or <code>imm32</code> bits.
</p></dd>
<dt><code>rsh32 %d, (%s|imm32)</code></dt>
<dd><p>32-bit right logical shift, by <code>%s</code> or <code>imm32</code> bits.
</p></dd>
<dt><code>arsh32 %d, (%s|imm32)</code></dt>
<dd><p>32-bit right arithmetic shift, by <code>%s</code> or <code>imm32</code> bits.
</p></dd>
<dt><code>neg32 %d</code></dt>
<dd><p>32-bit arithmetic negation.
</p></dd>
<dt><code>mov32 %d, (%s|imm32)</code></dt>
<dd><p>Move the 32-bit value of <code>%s</code> in <code>%d</code>, or load <code>imm32</code>
in <code>%d</code>.
</p></dd>
</dl>
 
<a name="Endianness-conversion-instructions"></a>
<h4 class="subsubsection">9.7.4.3 Endianness conversion instructions</h4>
 
<dl compact="compact">
<dt><code>endle %d, (8|16|32)</code></dt>
<dd><p>Convert the 8-bit, 16-bit or 32-bit value in <code>%d</code> to
little-endian.
</p></dd>
<dt><code>endbe %d, (8|16|32)</code></dt>
<dd><p>Convert the 8-bit, 16-bit or 32-bit value in <code>%d</code> to big-endian.
</p></dd>
</dl>
 
<a name="g_t64_002dbit-load-and-pseudo-maps"></a>
<h4 class="subsubsection">9.7.4.4 64-bit load and pseudo maps</h4>
 
<dl compact="compact">
<dt><code>lddw %d, imm64</code></dt>
<dd><p>Load the given signed 64-bit immediate, or pseudo map descriptor, to
the destination register <code>%d</code>.
</p></dd>
<dt><code>lddw %d, %map_fd(N)</code></dt>
<dd><p>Load the address of the given pseudo map fd <em>N</em> to the
destination register <code>%d</code>.
</p></dd>
</dl>
 
<a name="Load-instructions-for-socket-filters"></a>
<h4 class="subsubsection">9.7.4.5 Load instructions for socket filters</h4>
 
<p>The following instructions are intended to be used in socket filters,
and are therefore not general-purpose: they make assumptions on the
contents of several registers.  See the file
<samp>Documentation/networking/filter.txt</samp> in the Linux kernel source
tree for more information.
</p>
<p>Absolute loads:
</p>
<dl compact="compact">
<dt><code>ldabsdw imm32</code></dt>
<dd><p>Absolute 64-bit load.
</p></dd>
<dt><code>ldabsw imm32</code></dt>
<dd><p>Absolute 32-bit load.
</p></dd>
<dt><code>ldabsh imm32</code></dt>
<dd><p>Absolute 16-bit load.
</p></dd>
<dt><code>ldabsb imm32</code></dt>
<dd><p>Absolute 8-bit load.
</p></dd>
</dl>
 
<p>Indirect loads:
</p>
<dl compact="compact">
<dt><code>ldinddw %s, imm32</code></dt>
<dd><p>Indirect 64-bit load.
</p></dd>
<dt><code>ldindw %s, imm32</code></dt>
<dd><p>Indirect 32-bit load.
</p></dd>
<dt><code>ldindh %s, imm32</code></dt>
<dd><p>Indirect 16-bit load.
</p></dd>
<dt><code>ldindb %s, imm32</code></dt>
<dd><p>Indirect 8-bit load.
</p></dd>
</dl>
 
<a name="Generic-load_002fstore-instructions"></a>
<h4 class="subsubsection">9.7.4.6 Generic load/store instructions</h4>
 
<p>General-purpose load and store instructions are provided for several
word sizes.
</p>
<p>Load to register instructions:
</p>
<dl compact="compact">
<dt><code>ldxdw %d, [%s+offset16]</code></dt>
<dd><p>Generic 64-bit load.
</p></dd>
<dt><code>ldxw %d, [%s+offset16]</code></dt>
<dd><p>Generic 32-bit load.
</p></dd>
<dt><code>ldxh %d, [%s+offset16]</code></dt>
<dd><p>Generic 16-bit load.
</p></dd>
<dt><code>ldxb %d, [%s+offset16]</code></dt>
<dd><p>Generic 8-bit load.
</p></dd>
</dl>
 
<p>Store from register instructions:
</p>
<dl compact="compact">
<dt><code>stxdw [%d+offset16], %s</code></dt>
<dd><p>Generic 64-bit store.
</p></dd>
<dt><code>stxw [%d+offset16], %s</code></dt>
<dd><p>Generic 32-bit store.
</p></dd>
<dt><code>stxh [%d+offset16], %s</code></dt>
<dd><p>Generic 16-bit store.
</p></dd>
<dt><code>stxb [%d+offset16], %s</code></dt>
<dd><p>Generic 8-bit store.
</p></dd>
</dl>
 
<p>Store from immediates instructions:
</p>
<dl compact="compact">
<dt><code>stddw [%d+offset16], imm32</code></dt>
<dd><p>Store immediate as 64-bit.
</p></dd>
<dt><code>stdw [%d+offset16], imm32</code></dt>
<dd><p>Store immediate as 32-bit.
</p></dd>
<dt><code>stdh [%d+offset16], imm32</code></dt>
<dd><p>Store immediate as 16-bit.
</p></dd>
<dt><code>stdb [%d+offset16], imm32</code></dt>
<dd><p>Store immediate as 8-bit.
</p></dd>
</dl>
 
<a name="Jump-instructions"></a>
<h4 class="subsubsection">9.7.4.7 Jump instructions</h4>
 
<p>eBPF provides the following compare-and-jump instructions, which
compare the values of the two given registers, or the values of a
register and an immediate, and perform a branch in case the comparison
holds true.
</p>
<dl compact="compact">
<dt><code>ja %d,(%s|imm32),disp16</code></dt>
<dd><p>Jump-always.
</p></dd>
<dt><code>jeq %d,(%s|imm32),disp16</code></dt>
<dd><p>Jump if equal.
</p></dd>
<dt><code>jgt %d,(%s|imm32),disp16</code></dt>
<dd><p>Jump if greater.
</p></dd>
<dt><code>jge %d,(%s|imm32),disp16</code></dt>
<dd><p>Jump if greater or equal.
</p></dd>
<dt><code>jlt %d,(%s|imm32),disp16</code></dt>
<dd><p>Jump if lesser.
</p></dd>
<dt><code>jle %d,(%s|imm32),disp16</code></dt>
<dd><p>Jump if lesser or equal.
</p></dd>
<dt><code>jset %d,(%s|imm32),disp16</code></dt>
<dd><p>Jump if signed equal.
</p></dd>
<dt><code>jne %d,(%s|imm32),disp16</code></dt>
<dd><p>Jump if not equal.
</p></dd>
<dt><code>jsgt %d,(%s|imm32),disp16</code></dt>
<dd><p>Jump if signed greater.
</p></dd>
<dt><code>jsge %d,(%s|imm32),disp16</code></dt>
<dd><p>Jump if signed greater or equal.
</p></dd>
<dt><code>jslt %d,(%s|imm32),disp16</code></dt>
<dd><p>Jump if signed lesser.
</p></dd>
<dt><code>jsle %d,(%s|imm32),disp16</code></dt>
<dd><p>Jump if signed lesser or equal.
</p></dd>
</dl>
 
<p>A call instruction is provided in order to perform calls to other eBPF
functions, or to external kernel helpers:
</p>
<dl compact="compact">
<dt><code>call (disp32|imm32)</code></dt>
<dd><p>Jump and link to the offset <em>disp32</em>, or to the kernel helper
function identified by <em>imm32</em>.
</p></dd>
</dl>
 
<p>Finally:
</p>
<dl compact="compact">
<dt><code>exit</code></dt>
<dd><p>Terminate the eBPF program.
</p></dd>
</dl>
 
<a name="Atomic-instructions"></a>
<h4 class="subsubsection">9.7.4.8 Atomic instructions</h4>
 
<p>Atomic exchange-and-add instructions are provided in two flavors: one
for swapping 64-bit quantities and another for 32-bit quantities.
</p>
<dl compact="compact">
<dt><code>xadddw [%d+offset16],%s</code></dt>
<dd><p>Exchange-and-add a 64-bit value at the specified location.
</p></dd>
<dt><code>xaddw [%d+offset16],%s</code></dt>
<dd><p>Exchange-and-add a 32-bit value at the specified location.
</p></dd>
</dl>
 
 
<hr>
<div class="header">
<p>
Previous: <a href="BPF-Directives.html#BPF-Directives" accesskey="p" rel="previous">BPF Directives</a>, Up: <a href="BPF_002dDependent.html#BPF_002dDependent" accesskey="u" rel="up">BPF-Dependent</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="AS-Index.html#AS-Index" title="Index" rel="index">Index</a>]</p>
</div>
 
 
 
</body>
</html>