1 ;; Linux BPF CPU description -*- Scheme -*-
2 ;; Copyright (C) 2019 Free Software Foundation, Inc.
4 ;; Contributed by Oracle Inc.
6 ;; This file is part of the GNU Binutils and of GDB.
8 ;; This program is free software; you can redistribute it and/or
9 ;; modify it under the terms of the GNU General Public License as
10 ;; published by the Free Software Foundation; either version 3 of the
11 ;; License, or (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful, but
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ;; General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program; if not, write to the Free Software
20 ;; Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
23 ;; This file contains a CGEN CPU description for the Linux kernel eBPF
24 ;; instruction set. eBPF is documented in the linux kernel source
25 ;; tree. See linux/Documentation/networking/filter.txt, and also the
26 ;; sources in the networking subsystem, notably
27 ;; linux/net/core/filter.c.
29 (include "simplify.inc")
33 (comment "Linux kernel BPF")
35 ;; XXX explain the default-alignment setting is for the simulator.
36 ;; It is confusing that the simulator follows the emulated memory
37 ;; access conventions for fetching instructions by pieces...
38 (default-alignment unaligned)
44 ;; Logically, eBPF comforms a single instruction set featuring two
45 ;; kind of instructions: 64-bit instructions and 128-bit instructions.
47 ;; The 64-bit instructions have the form:
49 ;; code:8 regs:8 offset:16 imm:32
51 ;; Whereas the 128-bit instructions (at the moment there is only one
52 ;; of such instructions, lddw) have the form:
54 ;; code:8 regs:8 offset:16 imm:32 unused:32 imm:32
56 ;; In both formats `regs' is itself composed by two fields:
60 ;; The ISA is supposed to be orthogonal to endianness: the endianness
61 ;; of the instruction fields follow the endianness of the host running
62 ;; the eBPF program, and that's all. However, this is not entirely
63 ;; true. The definition of an eBPF code in the Linux kernel is:
66 ;; __u8 code; /* opcode */
67 ;; __u8 dst_reg:4; /* dest register */
68 ;; __u8 src_reg:4; /* source register */
69 ;; __s16 off; /* signed offset */
70 ;; __s32 imm; /* signed immediate constant */
73 ;; Since the ordering of fields in C bitmaps is defined by the
74 ;; implementation, the impact of endianness in the encoding of eBPF
75 ;; instructions is effectively defined by GCC. In particular, GCC
76 ;; places dst_reg before src_reg in little-endian code, and the other
77 ;; way around in big-endian code.
79 ;; So, in reality, eBPF comprises two instruction sets: one for
80 ;; little-endian with instructions like:
82 ;; code:8 src:4 dst:4 offset:16 imm:32 [unused:32 imm:32]
84 ;; and another for big-endian with instructions like:
86 ;; code:8 dst:4 src:4 offset:16 imm:32 [unused:32 imm:32]
88 ;; where `offset' and the immediate fields are encoded in
89 ;; little-endian and big-endian byte-order, respectively.
91 (define-pmacro (define-bpf-isa x-endian)
93 (name (.sym ebpf x-endian))
94 (comment "The eBPF instruction set")
95 ;; Default length to record in ifields. This is used in
96 ;; calculations involving bit numbers.
97 (default-insn-word-bitsize 64)
98 ;; Length of an unknown instruction. Used by disassembly and by the
99 ;; simulator's invalid insn handler.
100 (default-insn-bitsize 64)
101 ;; Number of bits of insn that can be initially fetched. This is
102 ;; the size of the smallest insn.
103 (base-insn-bitsize 64)))
108 (define-pmacro all-isas () (ISA ebpfle,ebpfbe))
110 ;;;; Hardware Hierarchy
123 (comment "Linux kernel eBPF virtual CPU")
129 (comment "Linux eBPF")
131 (isas ebpfle ebpfbe))
135 (comment "Linux eBPF default model")
137 (unit u-exec "execution unit" ()
143 () ; profile action (default)
146 ;;;; Hardware Elements
148 ;; eBPF programs can access 10 general-purpose registers which are
153 (comment "General Purpose Registers")
154 (attrs all-isas (MACH bpf))
155 (type register DI (16))
157 ;; XXX the frame pointer fp is read-only, so it should
158 ;; go in a different hardware.
159 (;; ABI names. Take priority when disassembling.
160 (r0 0) (r1 1) (r2 2) (r3 3) (r4 4) (r5 5) (r6 6)
161 (r7 7) (r8 8) (r9 9) (fp 10)
162 ;; Additional names recognized when assembling.
163 (r0 0) (r6 6) (r10 10))))
165 ;; The program counter. CGEN requires it, even if it is not visible
170 (comment "program counter")
171 (attrs PC PROFILE all-isas)
173 (get () (raw-reg h-pc))
174 (set (newval) (set (raw-reg h-pc) newval)))
176 ;; A 64-bit h-sint to be used by the imm64 operand below. XXX this
177 ;; shouldn't be needed, as h-sint is supposed to be able to hold
178 ;; 64-bit values. However, in practice CGEN limits h-sint to 32 bits
179 ;; in 32-bit hosts. To be fixed in CGEN.
181 (dnh h-sint64 "signed 64-bit integer" (all-isas) (immediate DI)
184 ;;;; The Instruction Sets
186 ;;; Fields and Opcodes
188 ;; Convenience macro to shorten the definition of the fields below.
189 (define-pmacro (dwf x-name x-comment x-attrs
190 x-word-offset x-word-length x-start x-length
192 "Define a field including its containing word."
196 (.splice attrs (.unsplice x-attrs))
197 (word-offset x-word-offset)
198 (word-length x-word-length)
203 ;; For arithmetic and jump instructions the 8-bit code field is
206 ;; op-code:4 op-src:1 op-class:3
208 (dwf f-op-code "eBPF opcode code" (all-isas) 0 8 7 4 UINT)
209 (dwf f-op-src "eBPF opcode source" (all-isas) 0 8 3 1 UINT)
210 (dwf f-op-class "eBPF opcode instruction class" (all-isas) 0 8 2 3 UINT)
212 (define-normal-insn-enum insn-op-code-alu "eBPF instruction codes"
213 (all-isas) OP_CODE_ f-op-code
214 (;; Codes for OP_CLASS_ALU and OP_CLASS_ALU64
215 (ADD #x0) (SUB #x1) (MUL #x2) (DIV #x3) (OR #x4) (AND #x5)
216 (LSH #x6) (RSH #x7) (NEG #x8) (MOD #x9) (XOR #xa) (MOV #xb)
218 ;; Codes for OP_CLASS_JMP
219 (JA #x0) (JEQ #x1) (JGT #x2) (JGE #x3) (JSET #x4)
220 (JNE #x5) (JSGT #x6) (JSGE #x7) (CALL #x8) (EXIT #x9)
221 (JLT #xa) (JLE #xb) (JSLT #xc) (JSLE #xd)))
223 (define-normal-insn-enum insn-op-src "eBPF instruction source"
224 (all-isas) OP_SRC_ f-op-src
225 ;; X => use `src' as source operand.
226 ;; K => use `imm32' as source operand.
229 (define-normal-insn-enum insn-op-class "eBPF instruction class"
230 (all-isas) OP_CLASS_ f-op-class
231 ((LD #b000) (LDX #b001) (ST #b010) (STX #b011)
232 (ALU #b100) (JMP #b101) (JMP32 #b110) (ALU64 #b111)))
234 ;; For load/store instructions, the 8-bit code field is subdivided in:
236 ;; op-mode:3 op-size:2 op-class:3
238 (dwf f-op-mode "eBPF opcode mode" (all-isas) 0 8 7 3 UINT)
239 (dwf f-op-size "eBPF opcode size" (all-isas) 0 8 4 2 UINT)
241 (define-normal-insn-enum insn-op-mode "eBPF load/store instruction modes"
242 (all-isas) OP_MODE_ f-op-mode
243 ((IMM #b000) (ABS #b001) (IND #b010) (MEM #b011)
244 ;; #b100 and #b101 are used in classic BPF only, reserved in eBPF.
247 (define-normal-insn-enum insn-op-size "eBPF load/store instruction sizes"
248 (all-isas) OP_SIZE_ f-op-size
249 ((W #b00) ;; Word: 4 byte
250 (H #b01) ;; Half-word: 2 byte
251 (B #b10) ;; Byte: 1 byte
252 (DW #b11))) ;; Double-word: 8 byte
254 ;; The fields for the source and destination registers are a bit
255 ;; tricky. Due to the bizarre nibble swap between little-endian and
256 ;; big-endian ISAs we need to keep different variants of the fields.
258 ;; Note that f-regs is used in the format spec of instructions that do
259 ;; NOT use registers, where endianness is irrelevant i.e. f-regs is a
260 ;; constant 0 opcode.
262 (dwf f-dstle "eBPF dst register field" ((ISA ebpfle)) 8 8 3 4 UINT)
263 (dwf f-srcle "eBPF source register field" ((ISA ebpfle)) 8 8 7 4 UINT)
265 (dwf f-dstbe "eBPF dst register field" ((ISA ebpfbe)) 8 8 7 4 UINT)
266 (dwf f-srcbe "eBPF source register field" ((ISA ebpfbe)) 8 8 3 4 UINT)
268 (dwf f-regs "eBPF registers field" (all-isas) 8 8 7 8 UINT)
270 ;; Finally, the fields for the immediates.
272 ;; The 16-bit offsets and 32-bit immediates do not present any special
273 ;; difficulty: we put them in their own instruction word so the
274 ;; byte-endianness will be properly applied.
276 (dwf f-offset16 "eBPF offset field" (all-isas) 16 16 15 16 INT)
277 (dwf f-imm32 "eBPF 32-bit immediate field" (all-isas) 32 32 31 32 INT)
279 ;; For the disjoint 64-bit signed immediate, however, we need to use a
282 (dwf f-imm64-a "eBPF 64-bit immediate a" (all-isas) 32 32 31 32 UINT)
283 (dwf f-imm64-b "eBPF 64-bit immediate b" (all-isas) 64 32 31 32 UINT)
284 (dwf f-imm64-c "eBPF 64-bit immediate c" (all-isas) 96 32 31 32 UINT)
288 (comment "eBPF 64-bit immediate field")
291 (subfields f-imm64-a f-imm64-b f-imm64-c)
293 (set (ifield f-imm64-b) (const 0))
294 (set (ifield f-imm64-c) (srl (ifield f-imm64) (const 32)))
295 (set (ifield f-imm64-a) (and (ifield f-imm64) (const #xffffffff)))))
296 (extract (sequence ()
297 (set (ifield f-imm64)
298 (or (sll UDI (zext UDI (ifield f-imm64-c)) (const 32))
299 (zext UDI (ifield f-imm64-a)))))))
303 ;; A couple of source and destination register operands are defined
304 ;; for each ISA: ebpfle and ebpfbe.
306 (dno dstle "destination register" ((ISA ebpfle)) h-gpr f-dstle)
307 (dno srcle "source register" ((ISA ebpfle)) h-gpr f-srcle)
309 (dno dstbe "destination register" ((ISA ebpfbe)) h-gpr f-dstbe)
310 (dno srcbe "source register" ((ISA ebpfbe)) h-gpr f-srcbe)
312 ;; Jump instructions have a 16-bit PC-relative address.
313 ;; CALL instructions have a 32-bit PC-relative address.
315 (dno disp16 "16-bit PC-relative address" (all-isas PCREL-ADDR) h-sint
317 (dno disp32 "32-bit PC-relative address" (all-isas PCREL-ADDR) h-sint
320 ;; Immediate operands in eBPF are signed, and we want the disassembler
321 ;; to print negative values in a sane way. Therefore we use the macro
322 ;; below to register a printer, which is itself defined as a C
323 ;; function in bpf.opc.
325 ;; define-normal-signed-immediate-operand
326 (define-pmacro (dnsio x-name x-comment x-attrs x-type x-index)
330 (.splice attrs (.unsplice x-attrs))
333 (handlers (print "immediate"))))
335 (dnsio imm32 "32-bit immediate" (all-isas) h-sint f-imm32)
336 (dnsio offset16 "16-bit offset" (all-isas) h-sint f-offset16)
338 ;; The 64-bit immediate cannot use the default
339 ;; cgen_parse_signed_integer, because it assumes operands are at much
340 ;; 32-bit wide. Use our own.
344 (comment "64-bit immediate")
348 (handlers (parse "imm64") (print "immediate")))
350 ;; The endle/endbe instructions take an operand to specify the word
351 ;; width in endianness conversions. We use both a parser and printer,
352 ;; which are defined as C functions in bpf.opc.
356 (comment "endianness size immediate: 16, 32 or 64")
360 (handlers (parse "endsize") (print "endsize")))
364 ;; For each opcode in insn-op-code-alu representing and integer
365 ;; arithmetic instruction (ADD, SUB, etc) we define a bunch of
366 ;; instruction variants:
368 ;; ADD[32]{i,r}le for the little-endian ISA
369 ;; ADD[32]{i,r}be for the big-endian ISA
371 ;; The `i' variants perform `dst OP imm32 -> dst' operations.
372 ;; The `r' variants perform `dst OP src -> dst' operations.
374 ;; The variants with 32 in their name are of ALU class. Otherwise
375 ;; they are ALU64 class.
377 (define-pmacro (define-alu-insn-un x-basename x-suffix x-op-class x-op-code
378 x-endian x-mode x-semop)
379 (dni (.sym x-basename x-suffix x-endian)
380 (.str x-basename x-suffix)
381 ((ISA (.sym ebpf x-endian)))
382 (.str x-basename x-suffix " $dst" x-endian)
383 (+ (f-imm32 0) (f-offset16 0) ((.sym f-src x-endian) 0) (.sym dst x-endian)
384 x-op-class OP_SRC_K x-op-code)
385 (set x-mode (.sym dst x-endian) (x-semop x-mode (.sym dst x-endian)))
388 (define-pmacro (define-alu-insn-bin x-basename x-suffix x-op-class x-op-code
389 x-endian x-mode x-semop)
391 ;; dst = dst OP immediate
392 (dni (.sym x-basename x-suffix "i" x-endian)
393 (.str x-basename x-suffix " immediate")
394 ((ISA (.sym ebpf x-endian)))
395 (.str x-basename x-suffix " $dst" x-endian ",$imm32")
396 (+ imm32 (f-offset16 0) ((.sym f-src x-endian) 0) (.sym dst x-endian)
397 x-op-class OP_SRC_K x-op-code)
398 (set x-mode (.sym dst x-endian) (x-semop x-mode (.sym dst x-endian) imm32))
401 (dni (.sym x-basename x-suffix "r" x-endian)
402 (.str x-basename x-suffix " register")
403 ((ISA (.sym ebpf x-endian)))
404 (.str x-basename x-suffix " $dst" x-endian ",$src" x-endian)
405 (+ (f-imm32 0) (f-offset16 0) (.sym src x-endian) (.sym dst x-endian)
406 x-op-class OP_SRC_X x-op-code)
407 (set x-mode (.sym dst x-endian)
408 (x-semop x-mode (.sym dst x-endian) (.sym src x-endian)))
411 (define-pmacro (define-alu-insn-mov x-basename x-suffix x-op-class x-op-code
414 (dni (.sym mov x-suffix "i" x-endian)
415 (.str mov x-suffix " immediate")
416 ((ISA (.sym ebpf x-endian)))
417 (.str x-basename x-suffix " $dst" x-endian ",$imm32")
418 (+ imm32 (f-offset16 0) ((.sym f-src x-endian) 0) (.sym dst x-endian)
419 x-op-class OP_SRC_K x-op-code)
420 (set x-mode (.sym dst x-endian) imm32)
422 (dni (.sym mov x-suffix "r" x-endian)
423 (.str mov x-suffix " register")
424 ((ISA (.sym ebpf x-endian)))
425 (.str x-basename x-suffix " $dst" x-endian ",$src" x-endian)
426 (+ (f-imm32 0) (f-offset16 0) (.sym src x-endian) (.sym dst x-endian)
427 x-op-class OP_SRC_X x-op-code)
428 (set x-mode (.sym dst x-endian) (.sym src x-endian))
432 ;; Unary ALU instructions (neg)
433 (define-pmacro (daiu x-basename x-op-code x-endian x-semop)
435 (define-alu-insn-un x-basename "" OP_CLASS_ALU64 x-op-code x-endian DI x-semop)
436 (define-alu-insn-un x-basename "32" OP_CLASS_ALU x-op-code x-endian USI x-semop)))
438 ;; Binary ALU instructions (all the others)
439 ;; For ALU32: DST = (u32) DST OP (u32) SRC is correct semantics
440 (define-pmacro (daib x-basename x-op-code x-endian x-semop)
442 (define-alu-insn-bin x-basename "" OP_CLASS_ALU64 x-op-code x-endian DI x-semop)
443 (define-alu-insn-bin x-basename "32" OP_CLASS_ALU x-op-code x-endian USI x-semop)))
445 ;; Move ALU instructions (mov)
446 (define-pmacro (daim x-basename x-op-code x-endian)
448 (define-alu-insn-mov x-basename "" OP_CLASS_ALU64 x-op-code x-endian DI)
449 (define-alu-insn-mov x-basename "32" OP_CLASS_ALU x-op-code x-endian USI)))
451 (define-pmacro (define-alu-instructions x-endian)
453 (daib add OP_CODE_ADD x-endian add)
454 (daib sub OP_CODE_SUB x-endian sub)
455 (daib mul OP_CODE_MUL x-endian mul)
456 (daib div OP_CODE_DIV x-endian div)
457 (daib or OP_CODE_OR x-endian or)
458 (daib and OP_CODE_AND x-endian and)
459 (daib lsh OP_CODE_LSH x-endian sll)
460 (daib rsh OP_CODE_RSH x-endian srl)
461 (daib mod OP_CODE_MOD x-endian mod)
462 (daib xor OP_CODE_XOR x-endian xor)
463 (daib arsh OP_CODE_ARSH x-endian sra)
464 (daiu neg OP_CODE_NEG x-endian neg)
465 (daim mov OP_CODE_MOV x-endian)))
467 (define-alu-instructions le)
468 (define-alu-instructions be)
470 ;;; Endianness conversion instructions
472 ;; The endianness conversion instructions come in several variants:
474 ;; END{le,be}le for the little-endian ISA
475 ;; END{le,be}be for the big-endian ISA
477 ;; Please do not be confused by the repeated `be' and `le' here. Each
478 ;; ISA has both endle and endbe instructions. It is the disposition
479 ;; of the source and destination register fields that change between
480 ;; ISAs, not the semantics of the instructions themselves (see section
481 ;; "The ISAs" above in this very file.)
483 (define-pmacro (define-endian-insn x-suffix x-op-src x-endian)
484 (dni (.sym "end" x-suffix x-endian)
485 (.str "end" x-suffix " register")
486 ((ISA (.sym ebpf x-endian)))
487 (.str "end" x-suffix " $dst" x-endian ",$endsize")
488 (+ (f-offset16 0) ((.sym f-src x-endian) 0) (.sym dst x-endian) endsize
489 OP_CLASS_ALU x-op-src OP_CODE_END)
490 (set (.sym dst x-endian)
491 (c-call DI "bpfbf_end" (.sym dst x-endian) endsize))
494 (define-endian-insn "le" OP_SRC_K le)
495 (define-endian-insn "be" OP_SRC_X le)
496 (define-endian-insn "le" OP_SRC_K be)
497 (define-endian-insn "be" OP_SRC_X be)
499 ;;; Load/Store instructions
501 ;; The lddw instruction takes a 64-bit immediate as an operand. Since
502 ;; this instruction also takes a `dst' operand, we need to define a
503 ;; variant for each ISA:
505 ;; LDDWle for the little-endian ISA
506 ;; LDDWbe for the big-endian ISA
508 (define-pmacro (define-lddw x-endian)
509 (dni (.sym lddw x-endian)
510 (.str "lddw" x-endian)
511 ((ISA (.sym ebpf x-endian)))
512 (.str "lddw $dst" x-endian ",$imm64")
513 (+ imm64 (f-offset16 0) ((.sym f-src x-endian) 0)
515 OP_CLASS_LD OP_SIZE_DW OP_MODE_IMM)
516 (set DI (.sym dst x-endian) imm64)
522 ;; The absolute load instructions are non-generic loads designed to be
523 ;; used in socket filters. They come in several variants:
527 (define-pmacro (dlabs x-suffix x-size x-smode)
528 (dni (.sym "ldabs" x-suffix)
529 (.str "ldabs" x-suffix)
531 (.str "ldabs" x-suffix " $imm32")
532 (+ imm32 (f-offset16 0) (f-regs 0)
533 OP_CLASS_LD OP_MODE_ABS (.sym OP_SIZE_ x-size))
535 (reg x-smode h-gpr 0)
540 (reg DI h-gpr 6) ;; Pointer to struct sk_buff
541 (const DI 0))) ;; XXX offsetof
542 ;; (struct sk_buff, data) XXX but the offset
543 ;; depends on CONFIG_* options, so this should
544 ;; be configured in the simulator and driven by
545 ;; command-line options. Handle with a c-call.
547 ;; XXX this clobbers R1-R5
555 ;; The indirect load instructions are non-generic loads designed to be
556 ;; used in socket filters. They come in several variants:
558 ;; LDIND{w,h,b,dw}le for the little-endian ISA
559 ;; LDIND[w,h,b,dw}be for the big-endian ISA
561 (define-pmacro (dlind x-suffix x-size x-endian x-smode)
562 (dni (.sym "ldind" x-suffix x-endian)
563 (.str "ldind" x-suffix)
564 ((ISA (.sym ebpf x-endian)))
565 (.str "ldind" x-suffix " $src" x-endian ",$imm32")
566 (+ imm32 (f-offset16 0) ((.sym f-dst x-endian) 0) (.sym src x-endian)
567 OP_CLASS_LD OP_MODE_IND (.sym OP_SIZE_ x-size))
569 (reg x-smode h-gpr 0)
574 (reg DI h-gpr 6) ;; Pointer to struct sk_buff
575 (const DI 0))) ;; XXX offsetof
576 ;; (struct sk_buff, data) XXX but the offset
577 ;; depends on CONFIG_* options, so this should
578 ;; be configured in the simulator and driven by
579 ;; command-line options. Handle with a c-call.
583 ;; XXX this clobbers R1-R5
586 (define-pmacro (define-ldind x-endian)
588 (dlind "w" W x-endian SI)
589 (dlind "h" H x-endian HI)
590 (dlind "b" B x-endian QI)
591 (dlind "dw" DW x-endian DI)))
596 ;; Generic load and store instructions are provided for several word
597 ;; sizes. They come in several variants:
599 ;; LDX{b,h,w,dw}le, STX{b,h,w,dw}le for the little-endian ISA
601 ;; LDX{b,h,w,dw}be, STX{b,h,w,dw}be for the big-endian ISA
603 ;; Loads operate on [$SRC+-OFFSET] -> $DST
604 ;; Stores operate on $SRC -> [$DST+-OFFSET]
606 (define-pmacro (dxli x-basename x-suffix x-size x-endian x-mode)
607 (dni (.sym x-basename x-suffix x-endian)
608 (.str x-basename x-suffix)
609 ((ISA (.sym ebpf x-endian)))
610 (.str x-basename x-suffix " $dst" x-endian ",[$src" x-endian "+$offset16]")
611 (+ (f-imm32 0) offset16 (.sym src x-endian) (.sym dst x-endian)
612 OP_CLASS_LDX (.sym OP_SIZE_ x-size) OP_MODE_MEM)
615 (mem x-mode (add DI (.sym src x-endian) (ext DI (trunc HI offset16)))))
618 (define-pmacro (dxsi x-basename x-suffix x-size x-endian x-mode)
619 (dni (.sym x-basename x-suffix x-endian)
620 (.str x-basename x-suffix)
621 ((ISA (.sym ebpf x-endian)))
622 (.str x-basename x-suffix " [$dst" x-endian "+$offset16],$src" x-endian)
623 (+ (f-imm32 0) offset16 (.sym src x-endian) (.sym dst x-endian)
624 OP_CLASS_STX (.sym OP_SIZE_ x-size) OP_MODE_MEM)
626 (mem x-mode (add DI (.sym dst x-endian) (ext DI (trunc HI offset16))))
627 (.sym src x-endian)) ;; XXX address is section-relative
630 (define-pmacro (define-ldstx-insns x-endian)
632 (dxli "ldx" "w" W x-endian SI)
633 (dxli "ldx" "h" H x-endian HI)
634 (dxli "ldx" "b" B x-endian QI)
635 (dxli "ldx" "dw" DW x-endian DI)
637 (dxsi "stx" "w" W x-endian SI)
638 (dxsi "stx" "h" H x-endian HI)
639 (dxsi "stx" "b" B x-endian QI)
640 (dxsi "stx" "dw" DW x-endian DI)))
642 (define-ldstx-insns le)
643 (define-ldstx-insns be)
645 ;; Generic store instructions of the form IMM32 -> [$DST+OFFSET] are
646 ;; provided in several variants:
648 ;; ST{b,h,w,dw}le for the little-endian ISA
649 ;; ST{b,h,w,dw}be for the big-endian ISA
651 (define-pmacro (dsti x-suffix x-size x-endian x-mode)
652 (dni (.sym "st" x-suffix x-endian)
654 ((ISA (.sym ebpf x-endian)))
655 (.str "st" x-suffix " [$dst" x-endian "+$offset16],$imm32")
656 (+ imm32 offset16 ((.sym f-src x-endian) 0) (.sym dst x-endian)
657 OP_CLASS_ST (.sym OP_SIZE_ x-size) OP_MODE_MEM)
659 (mem x-mode (add DI (.sym dst x-endian) offset16))
660 imm32) ;; XXX address is section-relative
663 (define-pmacro (define-st-insns x-endian)
665 (dsti "b" B x-endian QI)
666 (dsti "h" H x-endian HI)
667 (dsti "w" W x-endian SI)
668 (dsti "dw" DW x-endian DI)))
673 ;;; Jump instructions
675 ;; Compare-and-jump instructions, on the other hand, make use of
676 ;; registers. Therefore, we need to define several variants in both
679 ;; J{eq,gt,ge,lt,le,set,ne,sgt,sge,slt,sle}[32]{i,r}le for the
680 ;; little-endian ISA.
681 ;; J{eq,gt,ge,lt,le,set,ne.sgt,sge,slt,sle}[32]{i,r}be for the
684 (define-pmacro (define-cond-jump-insn x-cond x-suffix x-op-class x-op-code x-endian x-mode x-semop)
686 (dni (.sym j x-cond x-suffix i x-endian)
687 (.str j x-cond x-suffix " i")
688 ((ISA (.sym ebpf x-endian)))
689 (.str "j" x-cond x-suffix " $dst" x-endian ",$imm32,$disp16")
690 (+ imm32 disp16 ((.sym f-src x-endian) 0) (.sym dst x-endian)
691 x-op-class OP_SRC_K (.sym OP_CODE_ x-op-code))
692 (if VOID (x-semop x-mode (.sym dst x-endian) imm32)
694 (reg DI h-pc) (add DI (reg DI h-pc)
695 (mul DI (add HI disp16 1) 8))))
697 (dni (.sym j x-cond x-suffix r x-endian)
698 (.str j x-cond x-suffix " r")
699 ((ISA (.sym ebpf x-endian)))
700 (.str "j" x-cond x-suffix " $dst" x-endian ",$src" x-endian ",$disp16")
701 (+ (f-imm32 0) disp16 (.sym src x-endian) (.sym dst x-endian)
702 x-op-class OP_SRC_X (.sym OP_CODE_ x-op-code))
703 (if VOID (x-semop x-mode (.sym dst x-endian) (.sym src x-endian))
705 (reg DI h-pc) (add DI (reg DI h-pc)
706 (mul DI (add HI disp16 1) 8))))
709 (define-pmacro (dcji x-cond x-op-code x-endian x-semop)
711 (define-cond-jump-insn x-cond "" OP_CLASS_JMP x-op-code x-endian DI x-semop)
712 (define-cond-jump-insn x-cond "32" OP_CLASS_JMP32 x-op-code x-endian SI x-semop )))
714 (define-pmacro (define-condjump-insns x-endian)
716 (dcji "eq" JEQ x-endian eq)
717 (dcji "gt" JGT x-endian gtu)
718 (dcji "ge" JGE x-endian geu)
719 (dcji "lt" JLT x-endian ltu)
720 (dcji "le" JLE x-endian leu)
721 (dcji "set" JSET x-endian and)
722 (dcji "ne" JNE x-endian ne)
723 (dcji "sgt" JSGT x-endian gt)
724 (dcji "sge" JSGE x-endian ge)
725 (dcji "slt" JSLT x-endian lt)
726 (dcji "sle" JSLE x-endian le)))
728 (define-condjump-insns le)
729 (define-condjump-insns be)
731 ;; The `call' instruction doesn't make use of registers, but the
732 ;; semantic routine should have access to the src register in order to
733 ;; properly interpret the meaning of disp32. Therefore we need one
736 (define-pmacro (define-call-insn x-endian)
737 (dni (.sym call x-endian)
739 ((ISA (.sym ebpf x-endian)))
741 (+ disp32 (f-offset16 0) (f-regs 0)
742 OP_CLASS_JMP OP_SRC_K OP_CODE_CALL)
744 "bpfbf_call" disp32 (ifield (.sym f-src x-endian)))
747 (define-call-insn le)
748 (define-call-insn be)
750 ;; The jump-always and `exit' instructions dont make use of either
751 ;; source nor destination registers, so only one variant per
752 ;; instruction is defined.
754 (dni ja "ja" (all-isas) "ja $disp16"
755 (+ (f-imm32 0) disp16 (f-regs 0)
756 OP_CLASS_JMP OP_SRC_K OP_CODE_JA)
757 (set DI (reg DI h-pc) (add DI (reg DI h-pc)
758 (mul DI (add HI disp16 1) 8)))
761 (dni "exit" "exit" (all-isas) "exit"
762 (+ (f-imm32 0) (f-offset16 0) (f-regs 0)
763 OP_CLASS_JMP (f-op-src 0) OP_CODE_EXIT)
764 (c-call VOID "bpfbf_exit")
767 ;;; Atomic instructions
769 ;; The atomic exchange-and-add instructions come in two flavors: one
770 ;; for swapping 64-bit quantities and another for 32-bit quantities.
772 (define-pmacro (sem-exchange-and-add x-endian x-mode)
773 (sequence VOID ((x-mode tmp))
774 ;; XXX acquire lock in simulator... as a hardware element?
775 (set x-mode tmp (mem x-mode (add DI (.sym dst x-endian) offset16)))
777 (mem x-mode (add DI (.sym dst x-endian) offset16))
778 (add x-mode tmp (.sym src x-endian)))))
780 (define-pmacro (define-atomic-insns x-endian)
782 (dni (.str "xadddw" x-endian)
784 ((ISA (.sym ebpf x-endian)))
785 (.str "xadddw [$dst" x-endian "+$offset16],$src" x-endian)
786 (+ (f-imm32 0) (.sym src x-endian) (.sym dst x-endian)
787 offset16 OP_MODE_XADD OP_SIZE_DW OP_CLASS_STX)
788 (sem-exchange-and-add x-endian DI)
790 (dni (.str "xaddw" x-endian)
792 ((ISA (.sym ebpf x-endian)))
793 (.str "xaddw [$dst" x-endian "+$offset16],$src" x-endian)
794 (+ (f-imm32 0) (.sym src x-endian) (.sym dst x-endian)
795 offset16 OP_MODE_XADD OP_SIZE_W OP_CLASS_STX)
796 (sem-exchange-and-add x-endian SI)
799 (define-atomic-insns le)
800 (define-atomic-insns be)
802 ;;; Breakpoint instruction
804 ;; The brkpt instruction is used by the BPF simulator and it doesn't
805 ;; really belong to the eBPF instruction set.
807 (dni "brkpt" "brkpt" (all-isas) "brkpt"
808 (+ (f-imm32 0) (f-offset16 0) (f-regs 0)
809 OP_CLASS_ALU OP_SRC_X OP_CODE_NEG)
810 (c-call VOID "bpfbf_breakpoint")