bpf.cpu 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. ;; Linux BPF CPU description -*- Scheme -*-
  2. ;; Copyright (C) 2019 Free Software Foundation, Inc.
  3. ;;
  4. ;; Contributed by Oracle Inc.
  5. ;;
  6. ;; This file is part of the GNU Binutils and of GDB.
  7. ;;
  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.
  12. ;;
  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.
  17. ;;
  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
  21. ;; 02110-1301, USA.
  22. ;; This file contains a CGEN CPU description for the Linux kernel eBPF
  23. ;; instruction set. eBPF is documented in the linux kernel source
  24. ;; tree. See linux/Documentation/networking/filter.txt, and also the
  25. ;; sources in the networking subsystem, notably
  26. ;; linux/net/core/filter.c.
  27. (include "simplify.inc")
  28. (define-arch
  29. (name bpf)
  30. (comment "Linux kernel BPF")
  31. (insn-lsb0? #t)
  32. ;; XXX explain the default-alignment setting is for the simulator.
  33. ;; It is confusing that the simulator follows the emulated memory
  34. ;; access conventions for fetching instructions by pieces...
  35. (default-alignment unaligned)
  36. (machs bpf xbpf)
  37. (isas ebpfle ebpfbe xbpfle xbpfbe))
  38. ;;;; The ISAs
  39. ;; Logically, eBPF comforms a single instruction set featuring two
  40. ;; kind of instructions: 64-bit instructions and 128-bit instructions.
  41. ;;
  42. ;; The 64-bit instructions have the form:
  43. ;;
  44. ;; code:8 regs:8 offset:16 imm:32
  45. ;;
  46. ;; Whereas the 128-bit instructions (at the moment there is only one
  47. ;; of such instructions, lddw) have the form:
  48. ;;
  49. ;; code:8 regs:8 offset:16 imm:32 unused:32 imm:32
  50. ;;
  51. ;; In both formats `regs' is itself composed by two fields:
  52. ;;
  53. ;; dst:4 src:4
  54. ;;
  55. ;; The ISA is supposed to be orthogonal to endianness: the endianness
  56. ;; of the instruction fields follow the endianness of the host running
  57. ;; the eBPF program, and that's all. However, this is not entirely
  58. ;; true. The definition of an eBPF code in the Linux kernel is:
  59. ;;
  60. ;; struct bpf_insn {
  61. ;; __u8 code; /* opcode */
  62. ;; __u8 dst_reg:4; /* dest register */
  63. ;; __u8 src_reg:4; /* source register */
  64. ;; __s16 off; /* signed offset */
  65. ;; __s32 imm; /* signed immediate constant */
  66. ;; };
  67. ;;
  68. ;; Since the ordering of fields in C bitmaps is defined by the
  69. ;; implementation, the impact of endianness in the encoding of eBPF
  70. ;; instructions is effectively defined by GCC. In particular, GCC
  71. ;; places dst_reg before src_reg in little-endian code, and the other
  72. ;; way around in big-endian code.
  73. ;;
  74. ;; So, in reality, eBPF comprises two instruction sets: one for
  75. ;; little-endian with instructions like:
  76. ;;
  77. ;; code:8 src:4 dst:4 offset:16 imm:32 [unused:32 imm:32]
  78. ;;
  79. ;; and another for big-endian with instructions like:
  80. ;;
  81. ;; code:8 dst:4 src:4 offset:16 imm:32 [unused:32 imm:32]
  82. ;;
  83. ;; where `offset' and the immediate fields are encoded in
  84. ;; little-endian and big-endian byte-order, respectively.
  85. (define-pmacro (define-bpf-isa x-endian)
  86. (define-isa
  87. (name (.sym ebpf x-endian))
  88. (comment "The eBPF instruction set")
  89. ;; Default length to record in ifields. This is used in
  90. ;; calculations involving bit numbers.
  91. (default-insn-word-bitsize 64)
  92. ;; Length of an unknown instruction. Used by disassembly and by the
  93. ;; simulator's invalid insn handler.
  94. (default-insn-bitsize 64)
  95. ;; Number of bits of insn that can be initially fetched. This is
  96. ;; the size of the smallest insn.
  97. (base-insn-bitsize 64)))
  98. (define-bpf-isa le)
  99. (define-bpf-isa be)
  100. (define-pmacro (define-xbpf-isa x-endian)
  101. (define-isa
  102. (name (.sym xbpf x-endian))
  103. (comment "The xBPF instruction set")
  104. (default-insn-word-bitsize 64)
  105. (default-insn-bitsize 64)
  106. (base-insn-bitsize 64)))
  107. (define-xbpf-isa le)
  108. (define-xbpf-isa be)
  109. (define-pmacro all-isas () (ISA ebpfle,ebpfbe,xbpfle,xbpfbe))
  110. (define-pmacro xbpf-isas () (ISA xbpfle,xbpfbe))
  111. (define-pmacro (endian-isas x-endian)
  112. ((ISA (.sym ebpf x-endian) (.sym xbpf x-endian))))
  113. ;;;; Hardware Hierarchy
  114. ;;
  115. ;; bpf architecture
  116. ;; |
  117. ;; bpfbf cpu-family
  118. ;; / \
  119. ;; bpf xbpf machine
  120. ;; | |
  121. ;; bpf-def xbpf-def model
  122. (define-cpu
  123. (name bpfbf)
  124. (comment "Linux kernel eBPF virtual CPU")
  125. (insn-endian big)
  126. (word-bitsize 64))
  127. (define-mach
  128. (name bpf)
  129. (comment "Linux eBPF")
  130. (cpu bpfbf)
  131. (isas ebpfle ebpfbe))
  132. (define-model
  133. (name bpf-def)
  134. (comment "Linux eBPF default model")
  135. (mach bpf)
  136. (unit u-exec "execution unit" ()
  137. 1 ; issue
  138. 1 ; done
  139. () ; state
  140. () ; inputs
  141. () ; outputs
  142. () ; profile action (default)
  143. ))
  144. (define-mach
  145. (name xbpf)
  146. (comment "Experimental BPF")
  147. (cpu bpfbf)
  148. (isas ebpfle ebpfbe xbpfle xbpfbe))
  149. (define-model
  150. (name xbpf-def)
  151. (comment "xBPF default model")
  152. (mach xbpf)
  153. (unit u-exec "execution unit" ()
  154. 1 ; issue
  155. 1 ; done
  156. () ; state
  157. () ; inputs
  158. () ; outputs
  159. () ; profile action (default)
  160. ))
  161. ;;;; Hardware Elements
  162. ;; eBPF programs can access 10 general-purpose registers which are
  163. ;; 64-bit.
  164. (define-hardware
  165. (name h-gpr)
  166. (comment "General Purpose Registers")
  167. (attrs all-isas (MACH bpf xbpf))
  168. (type register DI (16))
  169. (indices keyword "%"
  170. ;; XXX the frame pointer fp is read-only, so it should
  171. ;; go in a different hardware.
  172. (;; ABI names. Take priority when disassembling.
  173. (r0 0) (r1 1) (r2 2) (r3 3) (r4 4) (r5 5) (r6 6)
  174. (r7 7) (r8 8) (r9 9) (fp 10)
  175. ;; Additional names recognized when assembling.
  176. (r0 0) (r6 6) (r10 10))))
  177. ;; The program counter. CGEN requires it, even if it is not visible
  178. ;; to eBPF programs.
  179. (define-hardware
  180. (name h-pc)
  181. (comment "program counter")
  182. (attrs PC PROFILE all-isas)
  183. (type pc UDI)
  184. (get () (raw-reg h-pc))
  185. (set (newval) (set (raw-reg h-pc) newval)))
  186. ;; A 64-bit h-sint to be used by the imm64 operand below. XXX this
  187. ;; shouldn't be needed, as h-sint is supposed to be able to hold
  188. ;; 64-bit values. However, in practice CGEN limits h-sint to 32 bits
  189. ;; in 32-bit hosts. To be fixed in CGEN.
  190. (dnh h-sint64 "signed 64-bit integer" (all-isas) (immediate DI)
  191. () () ())
  192. ;;;; The Instruction Sets
  193. ;;; Fields and Opcodes
  194. ;; Convenience macro to shorten the definition of the fields below.
  195. (define-pmacro (dwf x-name x-comment x-attrs
  196. x-word-offset x-word-length x-start x-length
  197. x-mode)
  198. "Define a field including its containing word."
  199. (define-ifield
  200. (name x-name)
  201. (comment x-comment)
  202. (.splice attrs (.unsplice x-attrs))
  203. (word-offset x-word-offset)
  204. (word-length x-word-length)
  205. (start x-start)
  206. (length x-length)
  207. (mode x-mode)))
  208. ;; For arithmetic and jump instructions the 8-bit code field is
  209. ;; subdivided in:
  210. ;;
  211. ;; op-code:4 op-src:1 op-class:3
  212. (dwf f-op-code "eBPF opcode code" (all-isas) 0 8 7 4 UINT)
  213. (dwf f-op-src "eBPF opcode source" (all-isas) 0 8 3 1 UINT)
  214. (dwf f-op-class "eBPF opcode instruction class" (all-isas) 0 8 2 3 UINT)
  215. (define-normal-insn-enum insn-op-code-alu "eBPF instruction codes"
  216. (all-isas) OP_CODE_ f-op-code
  217. (;; Codes for OP_CLASS_ALU and OP_CLASS_ALU64
  218. (ADD #x0) (SUB #x1) (MUL #x2) (DIV #x3) (OR #x4) (AND #x5)
  219. (LSH #x6) (RSH #x7) (NEG #x8) (MOD #x9) (XOR #xa) (MOV #xb)
  220. (ARSH #xc) (END #xd)
  221. ;; xBPF-only: signed div, signed mod
  222. (SDIV #xe) (SMOD #xf)
  223. ;; Codes for OP_CLASS_JMP
  224. (JA #x0) (JEQ #x1) (JGT #x2) (JGE #x3) (JSET #x4)
  225. (JNE #x5) (JSGT #x6) (JSGE #x7) (CALL #x8) (EXIT #x9)
  226. (JLT #xa) (JLE #xb) (JSLT #xc) (JSLE #xd)))
  227. (define-normal-insn-enum insn-op-src "eBPF instruction source"
  228. (all-isas) OP_SRC_ f-op-src
  229. ;; X => use `src' as source operand.
  230. ;; K => use `imm32' as source operand.
  231. ((K #b0) (X #b1)))
  232. (define-normal-insn-enum insn-op-class "eBPF instruction class"
  233. (all-isas) OP_CLASS_ f-op-class
  234. ((LD #b000) (LDX #b001) (ST #b010) (STX #b011)
  235. (ALU #b100) (JMP #b101) (JMP32 #b110) (ALU64 #b111)))
  236. ;; For load/store instructions, the 8-bit code field is subdivided in:
  237. ;;
  238. ;; op-mode:3 op-size:2 op-class:3
  239. (dwf f-op-mode "eBPF opcode mode" (all-isas) 0 8 7 3 UINT)
  240. (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.
  245. (XADD #b110)))
  246. (define-normal-insn-enum insn-op-size "eBPF load/store instruction sizes"
  247. (all-isas) OP_SIZE_ f-op-size
  248. ((W #b00) ;; Word: 4 byte
  249. (H #b01) ;; Half-word: 2 byte
  250. (B #b10) ;; Byte: 1 byte
  251. (DW #b11))) ;; Double-word: 8 byte
  252. ;; The fields for the source and destination registers are a bit
  253. ;; tricky. Due to the bizarre nibble swap between little-endian and
  254. ;; big-endian ISAs we need to keep different variants of the fields.
  255. ;;
  256. ;; Note that f-regs is used in the format spec of instructions that do
  257. ;; NOT use registers, where endianness is irrelevant i.e. f-regs is a
  258. ;; constant 0 opcode.
  259. (dwf f-dstle "eBPF dst register field" ((ISA ebpfle xbpfle)) 8 8 3 4 UINT)
  260. (dwf f-srcle "eBPF source register field" ((ISA ebpfle xbpfle)) 8 8 7 4 UINT)
  261. (dwf f-dstbe "eBPF dst register field" ((ISA ebpfbe xbpfbe)) 8 8 7 4 UINT)
  262. (dwf f-srcbe "eBPF source register field" ((ISA ebpfbe xbpfbe)) 8 8 3 4 UINT)
  263. (dwf f-regs "eBPF registers field" (all-isas) 8 8 7 8 UINT)
  264. ;; Finally, the fields for the immediates.
  265. ;;
  266. ;; The 16-bit offsets and 32-bit immediates do not present any special
  267. ;; difficulty: we put them in their own instruction word so the
  268. ;; byte-endianness will be properly applied.
  269. (dwf f-offset16 "eBPF offset field" (all-isas) 16 16 15 16 HI)
  270. (dwf f-imm32 "eBPF 32-bit immediate field" (all-isas) 32 32 31 32 INT)
  271. ;; For the disjoint 64-bit signed immediate, however, we need to use a
  272. ;; multi-ifield.
  273. (dwf f-imm64-a "eBPF 64-bit immediate a" (all-isas) 32 32 31 32 UINT)
  274. (dwf f-imm64-b "eBPF 64-bit immediate b" (all-isas) 64 32 31 32 UINT)
  275. (dwf f-imm64-c "eBPF 64-bit immediate c" (all-isas) 96 32 31 32 UINT)
  276. (define-multi-ifield
  277. (name f-imm64)
  278. (comment "eBPF 64-bit immediate field")
  279. (attrs all-isas)
  280. (mode DI)
  281. (subfields f-imm64-a f-imm64-b f-imm64-c)
  282. (insert (sequence ()
  283. (set (ifield f-imm64-b) (const 0))
  284. (set (ifield f-imm64-c) (srl (ifield f-imm64) (const 32)))
  285. (set (ifield f-imm64-a) (and (ifield f-imm64) (const #xffffffff)))))
  286. (extract (sequence ()
  287. (set (ifield f-imm64)
  288. (or (sll UDI (zext UDI (ifield f-imm64-c)) (const 32))
  289. (zext UDI (ifield f-imm64-a)))))))
  290. ;;; Operands
  291. ;; A couple of source and destination register operands are defined
  292. ;; for each ISA: ebpfle and ebpfbe.
  293. (dno dstle "destination register" ((ISA ebpfle xbpfle)) h-gpr f-dstle)
  294. (dno srcle "source register" ((ISA ebpfle xbpfle)) h-gpr f-srcle)
  295. (dno dstbe "destination register" ((ISA ebpfbe xbpfbe)) h-gpr f-dstbe)
  296. (dno srcbe "source register" ((ISA ebpfbe xbpfbe)) h-gpr f-srcbe)
  297. ;; Jump instructions have a 16-bit PC-relative address.
  298. ;; CALL instructions have a 32-bit PC-relative address.
  299. (dno disp16 "16-bit PC-relative address" (all-isas PCREL-ADDR) h-sint
  300. f-offset16)
  301. (dno disp32 "32-bit PC-relative address" (all-isas PCREL-ADDR) h-sint
  302. f-imm32)
  303. ;; Immediate operands in eBPF are signed, and we want the disassembler
  304. ;; to print negative values in a sane way. Therefore we use the macro
  305. ;; below to register a printer, which is itself defined as a C
  306. ;; function in bpf.opc.
  307. ;; define-normal-signed-immediate-operand
  308. (define-pmacro (dnsio x-name x-comment x-attrs x-type x-index)
  309. (define-operand
  310. (name x-name)
  311. (comment x-comment)
  312. (.splice attrs (.unsplice x-attrs))
  313. (type x-type)
  314. (index x-index)
  315. (handlers (print "immediate"))))
  316. (dnsio imm32 "32-bit immediate" (all-isas) h-sint f-imm32)
  317. (dnsio offset16 "16-bit offset" (all-isas) h-sint f-offset16)
  318. ;; The 64-bit immediate cannot use the default
  319. ;; cgen_parse_signed_integer, because it assumes operands are at much
  320. ;; 32-bit wide. Use our own.
  321. (define-operand
  322. (name imm64)
  323. (comment "64-bit immediate")
  324. (attrs all-isas)
  325. (type h-sint64)
  326. (index f-imm64)
  327. (handlers (parse "imm64") (print "immediate")))
  328. ;; The endle/endbe instructions take an operand to specify the word
  329. ;; width in endianness conversions. We use both a parser and printer,
  330. ;; which are defined as C functions in bpf.opc.
  331. (define-operand
  332. (name endsize)
  333. (comment "endianness size immediate: 16, 32 or 64")
  334. (attrs all-isas)
  335. (type h-uint)
  336. (index f-imm32)
  337. (handlers (parse "endsize") (print "endsize")))
  338. ;;; ALU instructions
  339. ;; For each opcode in insn-op-code-alu representing and integer
  340. ;; arithmetic instruction (ADD, SUB, etc) we define a bunch of
  341. ;; instruction variants:
  342. ;;
  343. ;; ADD[32]{i,r}le for the little-endian ISA
  344. ;; ADD[32]{i,r}be for the big-endian ISA
  345. ;;
  346. ;; The `i' variants perform `dst OP imm32 -> dst' operations.
  347. ;; The `r' variants perform `dst OP src -> dst' operations.
  348. ;;
  349. ;; The variants with 32 in their name are of ALU class. Otherwise
  350. ;; they are ALU64 class.
  351. (define-pmacro (define-alu-insn-un x-basename x-suffix x-op-class x-op-code
  352. x-endian x-mode x-semop)
  353. (dni (.sym x-basename x-suffix x-endian)
  354. (.str x-basename x-suffix)
  355. (endian-isas x-endian)
  356. (.str x-basename x-suffix " $dst" x-endian)
  357. (+ (f-imm32 0) (f-offset16 0) ((.sym f-src x-endian) 0) (.sym dst x-endian)
  358. x-op-class OP_SRC_K x-op-code)
  359. (set x-mode (.sym dst x-endian) (x-semop x-mode (.sym dst x-endian)))
  360. ()))
  361. (define-pmacro (define-alu-insn-bin x-basename x-suffix x-op-class x-op-code
  362. x-endian x-mode x-semop x-isas)
  363. (begin
  364. ;; dst = dst OP immediate
  365. (dni (.sym x-basename x-suffix "i" x-endian)
  366. (.str x-basename x-suffix " immediate")
  367. (.splice (.unsplice x-isas))
  368. (.str x-basename x-suffix " $dst" x-endian ",$imm32")
  369. (+ imm32 (f-offset16 0) ((.sym f-src x-endian) 0) (.sym dst x-endian)
  370. x-op-class OP_SRC_K x-op-code)
  371. (set x-mode (.sym dst x-endian) (x-semop x-mode (.sym dst x-endian) imm32))
  372. ())
  373. ;; dst = dst OP src
  374. (dni (.sym x-basename x-suffix "r" x-endian)
  375. (.str x-basename x-suffix " register")
  376. (.splice (.unsplice x-isas))
  377. (.str x-basename x-suffix " $dst" x-endian ",$src" x-endian)
  378. (+ (f-imm32 0) (f-offset16 0) (.sym src x-endian) (.sym dst x-endian)
  379. x-op-class OP_SRC_X x-op-code)
  380. (set x-mode (.sym dst x-endian)
  381. (x-semop x-mode (.sym dst x-endian) (.sym src x-endian)))
  382. ())))
  383. (define-pmacro (define-alu-insn-mov x-basename x-suffix x-op-class x-op-code
  384. x-endian x-mode)
  385. (begin
  386. (dni (.sym mov x-suffix "i" x-endian)
  387. (.str mov x-suffix " immediate")
  388. (endian-isas x-endian)
  389. (.str x-basename x-suffix " $dst" x-endian ",$imm32")
  390. (+ imm32 (f-offset16 0) ((.sym f-src x-endian) 0) (.sym dst x-endian)
  391. x-op-class OP_SRC_K x-op-code)
  392. (set x-mode (.sym dst x-endian) imm32)
  393. ())
  394. (dni (.sym mov x-suffix "r" x-endian)
  395. (.str mov x-suffix " register")
  396. (endian-isas x-endian)
  397. (.str x-basename x-suffix " $dst" x-endian ",$src" x-endian)
  398. (+ (f-imm32 0) (f-offset16 0) (.sym src x-endian) (.sym dst x-endian)
  399. x-op-class OP_SRC_X x-op-code)
  400. (set x-mode (.sym dst x-endian) (.sym src x-endian))
  401. ())))
  402. ;; Unary ALU instructions (neg)
  403. (define-pmacro (daiu x-basename x-op-code x-endian x-semop)
  404. (begin
  405. (define-alu-insn-un x-basename "" OP_CLASS_ALU64 x-op-code x-endian DI x-semop)
  406. (define-alu-insn-un x-basename "32" OP_CLASS_ALU x-op-code x-endian USI x-semop)))
  407. ;; Binary ALU instructions (all the others)
  408. ;; For ALU32: DST = (u32) DST OP (u32) SRC is correct semantics
  409. (define-pmacro (daib x-basename x-op-code x-endian x-semop x-isas)
  410. (begin
  411. (define-alu-insn-bin x-basename "" OP_CLASS_ALU64 x-op-code x-endian DI x-semop x-isas)
  412. (define-alu-insn-bin x-basename "32" OP_CLASS_ALU x-op-code x-endian USI x-semop x-isas)))
  413. ;; Move ALU instructions (mov)
  414. (define-pmacro (daim x-basename x-op-code x-endian)
  415. (begin
  416. (define-alu-insn-mov x-basename "" OP_CLASS_ALU64 x-op-code x-endian DI)
  417. (define-alu-insn-mov x-basename "32" OP_CLASS_ALU x-op-code x-endian USI)))
  418. (define-pmacro (define-alu-instructions x-endian)
  419. (begin
  420. (daib add OP_CODE_ADD x-endian add (endian-isas x-endian))
  421. (daib sub OP_CODE_SUB x-endian sub (endian-isas x-endian))
  422. (daib mul OP_CODE_MUL x-endian mul (endian-isas x-endian))
  423. (daib div OP_CODE_DIV x-endian udiv (endian-isas x-endian))
  424. (daib or OP_CODE_OR x-endian or (endian-isas x-endian))
  425. (daib and OP_CODE_AND x-endian and (endian-isas x-endian))
  426. (daib lsh OP_CODE_LSH x-endian sll (endian-isas x-endian))
  427. (daib rsh OP_CODE_RSH x-endian srl (endian-isas x-endian))
  428. (daib mod OP_CODE_MOD x-endian umod (endian-isas x-endian))
  429. (daib xor OP_CODE_XOR x-endian xor (endian-isas x-endian))
  430. (daib arsh OP_CODE_ARSH x-endian sra (endian-isas x-endian))
  431. (daib sdiv OP_CODE_SDIV x-endian div ((ISA (.sym xbpf x-endian))))
  432. (daib smod OP_CODE_SMOD x-endian mod ((ISA (.sym xbpf x-endian))))
  433. (daiu neg OP_CODE_NEG x-endian neg)
  434. (daim mov OP_CODE_MOV x-endian)))
  435. (define-alu-instructions le)
  436. (define-alu-instructions be)
  437. ;;; Endianness conversion instructions
  438. ;; The endianness conversion instructions come in several variants:
  439. ;;
  440. ;; END{le,be}le for the little-endian ISA
  441. ;; END{le,be}be for the big-endian ISA
  442. ;;
  443. ;; Please do not be confused by the repeated `be' and `le' here. Each
  444. ;; ISA has both endle and endbe instructions. It is the disposition
  445. ;; of the source and destination register fields that change between
  446. ;; ISAs, not the semantics of the instructions themselves (see section
  447. ;; "The ISAs" above in this very file.)
  448. (define-pmacro (define-endian-insn x-suffix x-op-src x-endian)
  449. (dni (.sym "end" x-suffix x-endian)
  450. (.str "end" x-suffix " register")
  451. (endian-isas x-endian)
  452. (.str "end" x-suffix " $dst" x-endian ",$endsize")
  453. (+ (f-offset16 0) ((.sym f-src x-endian) 0) (.sym dst x-endian) endsize
  454. OP_CLASS_ALU x-op-src OP_CODE_END)
  455. (set (.sym dst x-endian)
  456. (c-call DI (.str "bpfbf_end" x-suffix) (.sym dst x-endian) endsize))
  457. ()))
  458. (define-endian-insn "le" OP_SRC_K le)
  459. (define-endian-insn "be" OP_SRC_X le)
  460. (define-endian-insn "le" OP_SRC_K be)
  461. (define-endian-insn "be" OP_SRC_X be)
  462. ;;; Load/Store instructions
  463. ;; The lddw instruction takes a 64-bit immediate as an operand. Since
  464. ;; this instruction also takes a `dst' operand, we need to define a
  465. ;; variant for each ISA:
  466. ;;
  467. ;; LDDWle for the little-endian ISA
  468. ;; LDDWbe for the big-endian ISA
  469. (define-pmacro (define-lddw x-endian)
  470. (dni (.sym lddw x-endian)
  471. (.str "lddw" x-endian)
  472. (endian-isas x-endian)
  473. (.str "lddw $dst" x-endian ",$imm64")
  474. (+ imm64 (f-offset16 0) ((.sym f-src x-endian) 0)
  475. (.sym dst x-endian)
  476. OP_CLASS_LD OP_SIZE_DW OP_MODE_IMM)
  477. (set DI (.sym dst x-endian) imm64)
  478. ()))
  479. (define-lddw le)
  480. (define-lddw be)
  481. ;; The absolute load instructions are non-generic loads designed to be
  482. ;; used in socket filters. They come in several variants:
  483. ;;
  484. ;; LDABS{w,h,b,dw}
  485. (define-pmacro (dlabs x-suffix x-size x-smode)
  486. (dni (.sym "ldabs" x-suffix)
  487. (.str "ldabs" x-suffix)
  488. (all-isas)
  489. (.str "ldabs" x-suffix " $imm32")
  490. (+ imm32 (f-offset16 0) (f-regs 0)
  491. OP_CLASS_LD OP_MODE_ABS (.sym OP_SIZE_ x-size))
  492. (set x-smode
  493. (reg x-smode h-gpr 0)
  494. (mem x-smode
  495. (add DI
  496. (mem DI
  497. (add DI
  498. (reg DI h-gpr 6) ;; Pointer to struct sk_buff
  499. (c-call "bpfbf_skb_data_offset")))
  500. imm32)))
  501. ;; XXX this clobbers R1-R5
  502. ()))
  503. (dlabs "w" W SI)
  504. (dlabs "h" H HI)
  505. (dlabs "b" B QI)
  506. (dlabs "dw" DW DI)
  507. ;; The indirect load instructions are non-generic loads designed to be
  508. ;; used in socket filters. They come in several variants:
  509. ;;
  510. ;; LDIND{w,h,b,dw}le for the little-endian ISA
  511. ;; LDIND[w,h,b,dw}be for the big-endian ISA
  512. (define-pmacro (dlind x-suffix x-size x-endian x-smode)
  513. (dni (.sym "ldind" x-suffix x-endian)
  514. (.str "ldind" x-suffix)
  515. (endian-isas x-endian)
  516. (.str "ldind" x-suffix " $src" x-endian ",$imm32")
  517. (+ imm32 (f-offset16 0) ((.sym f-dst x-endian) 0) (.sym src x-endian)
  518. OP_CLASS_LD OP_MODE_IND (.sym OP_SIZE_ x-size))
  519. (set x-smode
  520. (reg x-smode h-gpr 0)
  521. (mem x-smode
  522. (add DI
  523. (mem DI
  524. (add DI
  525. (reg DI h-gpr 6) ;; Pointer to struct sk_buff
  526. (c-call "bpfbf_skb_data_offset")))
  527. (add DI
  528. (.sym src x-endian)
  529. imm32))))
  530. ;; XXX this clobbers R1-R5
  531. ()))
  532. (define-pmacro (define-ldind x-endian)
  533. (begin
  534. (dlind "w" W x-endian SI)
  535. (dlind "h" H x-endian HI)
  536. (dlind "b" B x-endian QI)
  537. (dlind "dw" DW x-endian DI)))
  538. (define-ldind le)
  539. (define-ldind be)
  540. ;; Generic load and store instructions are provided for several word
  541. ;; sizes. They come in several variants:
  542. ;;
  543. ;; LDX{b,h,w,dw}le, STX{b,h,w,dw}le for the little-endian ISA
  544. ;;
  545. ;; LDX{b,h,w,dw}be, STX{b,h,w,dw}be for the big-endian ISA
  546. ;;
  547. ;; Loads operate on [$SRC+-OFFSET] -> $DST
  548. ;; Stores operate on $SRC -> [$DST+-OFFSET]
  549. (define-pmacro (dxli x-basename x-suffix x-size x-endian x-mode)
  550. (dni (.sym x-basename x-suffix x-endian)
  551. (.str x-basename x-suffix)
  552. (endian-isas x-endian)
  553. (.str x-basename x-suffix " $dst" x-endian ",[$src" x-endian "+$offset16]")
  554. (+ (f-imm32 0) offset16 (.sym src x-endian) (.sym dst x-endian)
  555. OP_CLASS_LDX (.sym OP_SIZE_ x-size) OP_MODE_MEM)
  556. (set x-mode
  557. (.sym dst x-endian)
  558. (mem x-mode (add DI (.sym src x-endian) offset16)))
  559. ()))
  560. (define-pmacro (dxsi x-basename x-suffix x-size x-endian x-mode)
  561. (dni (.sym x-basename x-suffix x-endian)
  562. (.str x-basename x-suffix)
  563. (endian-isas x-endian)
  564. (.str x-basename x-suffix " [$dst" x-endian "+$offset16],$src" x-endian)
  565. (+ (f-imm32 0) offset16 (.sym src x-endian) (.sym dst x-endian)
  566. OP_CLASS_STX (.sym OP_SIZE_ x-size) OP_MODE_MEM)
  567. (set x-mode
  568. (mem x-mode (add DI (.sym dst x-endian) offset16))
  569. (.sym src x-endian)) ;; XXX address is section-relative
  570. ()))
  571. (define-pmacro (define-ldstx-insns x-endian)
  572. (begin
  573. (dxli "ldx" "w" W x-endian SI)
  574. (dxli "ldx" "h" H x-endian HI)
  575. (dxli "ldx" "b" B x-endian QI)
  576. (dxli "ldx" "dw" DW x-endian DI)
  577. (dxsi "stx" "w" W x-endian SI)
  578. (dxsi "stx" "h" H x-endian HI)
  579. (dxsi "stx" "b" B x-endian QI)
  580. (dxsi "stx" "dw" DW x-endian DI)))
  581. (define-ldstx-insns le)
  582. (define-ldstx-insns be)
  583. ;; Generic store instructions of the form IMM32 -> [$DST+OFFSET] are
  584. ;; provided in several variants:
  585. ;;
  586. ;; ST{b,h,w,dw}le for the little-endian ISA
  587. ;; ST{b,h,w,dw}be for the big-endian ISA
  588. (define-pmacro (dsti x-suffix x-size x-endian x-mode)
  589. (dni (.sym "st" x-suffix x-endian)
  590. (.str "st" x-suffix)
  591. (endian-isas x-endian)
  592. (.str "st" x-suffix " [$dst" x-endian "+$offset16],$imm32")
  593. (+ imm32 offset16 ((.sym f-src x-endian) 0) (.sym dst x-endian)
  594. OP_CLASS_ST (.sym OP_SIZE_ x-size) OP_MODE_MEM)
  595. (set x-mode
  596. (mem x-mode (add DI (.sym dst x-endian) offset16))
  597. imm32) ;; XXX address is section-relative
  598. ()))
  599. (define-pmacro (define-st-insns x-endian)
  600. (begin
  601. (dsti "b" B x-endian QI)
  602. (dsti "h" H x-endian HI)
  603. (dsti "w" W x-endian SI)
  604. (dsti "dw" DW x-endian DI)))
  605. (define-st-insns le)
  606. (define-st-insns be)
  607. ;;; Jump instructions
  608. ;; Compare-and-jump instructions, on the other hand, make use of
  609. ;; registers. Therefore, we need to define several variants in both
  610. ;; ISAs:
  611. ;;
  612. ;; J{eq,gt,ge,lt,le,set,ne,sgt,sge,slt,sle}[32]{i,r}le for the
  613. ;; little-endian ISA.
  614. ;; J{eq,gt,ge,lt,le,set,ne.sgt,sge,slt,sle}[32]{i,r}be for the
  615. ;; big-endian ISA.
  616. (define-pmacro (define-cond-jump-insn x-cond x-suffix x-op-class x-op-code x-endian x-mode x-semop)
  617. (begin
  618. (dni (.sym j x-cond x-suffix i x-endian)
  619. (.str j x-cond x-suffix " i")
  620. (endian-isas x-endian)
  621. (.str "j" x-cond x-suffix " $dst" x-endian ",$imm32,$disp16")
  622. (+ imm32 disp16 ((.sym f-src x-endian) 0) (.sym dst x-endian)
  623. x-op-class OP_SRC_K (.sym OP_CODE_ x-op-code))
  624. (if VOID (x-semop x-mode (.sym dst x-endian) imm32)
  625. (set DI
  626. (reg DI h-pc) (add DI (reg DI h-pc)
  627. (mul DI (add HI disp16 1) 8))))
  628. ())
  629. (dni (.sym j x-cond x-suffix r x-endian)
  630. (.str j x-cond x-suffix " r")
  631. (endian-isas x-endian)
  632. (.str "j" x-cond x-suffix " $dst" x-endian ",$src" x-endian ",$disp16")
  633. (+ (f-imm32 0) disp16 (.sym src x-endian) (.sym dst x-endian)
  634. x-op-class OP_SRC_X (.sym OP_CODE_ x-op-code))
  635. (if VOID (x-semop x-mode (.sym dst x-endian) (.sym src x-endian))
  636. (set DI
  637. (reg DI h-pc) (add DI (reg DI h-pc)
  638. (mul DI (add HI disp16 1) 8))))
  639. ())))
  640. (define-pmacro (dcji x-cond x-op-code x-endian x-semop)
  641. (begin
  642. (define-cond-jump-insn x-cond "" OP_CLASS_JMP x-op-code x-endian DI x-semop)
  643. (define-cond-jump-insn x-cond "32" OP_CLASS_JMP32 x-op-code x-endian SI x-semop )))
  644. (define-pmacro (define-condjump-insns x-endian)
  645. (begin
  646. (dcji "eq" JEQ x-endian eq)
  647. (dcji "gt" JGT x-endian gtu)
  648. (dcji "ge" JGE x-endian geu)
  649. (dcji "lt" JLT x-endian ltu)
  650. (dcji "le" JLE x-endian leu)
  651. (dcji "set" JSET x-endian and)
  652. (dcji "ne" JNE x-endian ne)
  653. (dcji "sgt" JSGT x-endian gt)
  654. (dcji "sge" JSGE x-endian ge)
  655. (dcji "slt" JSLT x-endian lt)
  656. (dcji "sle" JSLE x-endian le)))
  657. (define-condjump-insns le)
  658. (define-condjump-insns be)
  659. ;; The `call' instruction doesn't make use of registers, but the
  660. ;; semantic routine should have access to the src register in order to
  661. ;; properly interpret the meaning of disp32. Therefore we need one
  662. ;; version per ISA.
  663. (define-pmacro (define-call-insn x-endian)
  664. (dni (.sym call x-endian)
  665. "call"
  666. (endian-isas x-endian)
  667. "call $disp32"
  668. (+ disp32 (f-offset16 0) (f-regs 0)
  669. OP_CLASS_JMP OP_SRC_K OP_CODE_CALL)
  670. (c-call VOID
  671. "bpfbf_call" disp32 (ifield (.sym f-src x-endian)))
  672. ()))
  673. (define-call-insn le)
  674. (define-call-insn be)
  675. (define-pmacro (define-callr-insn x-endian)
  676. (dni (.sym callr x-endian)
  677. "callr"
  678. ((ISA (.sym xbpf x-endian)))
  679. (.str "call $dst" x-endian)
  680. (+ (f-imm32 0) (f-offset16 0) ((.sym f-src x-endian) 0) (.sym dst x-endian)
  681. OP_CLASS_JMP OP_SRC_X OP_CODE_CALL)
  682. (c-call VOID
  683. "bpfbf_callr" (ifield (.sym f-dst x-endian)))
  684. ()))
  685. (define-callr-insn le)
  686. (define-callr-insn be)
  687. ;; The jump-always and `exit' instructions dont make use of either
  688. ;; source nor destination registers, so only one variant per
  689. ;; instruction is defined.
  690. (dni ja "ja" (all-isas) "ja $disp16"
  691. (+ (f-imm32 0) disp16 (f-regs 0)
  692. OP_CLASS_JMP OP_SRC_K OP_CODE_JA)
  693. (set DI (reg DI h-pc) (add DI (reg DI h-pc)
  694. (mul DI (add HI disp16 1) 8)))
  695. ())
  696. (dni "exit" "exit" (all-isas) "exit"
  697. (+ (f-imm32 0) (f-offset16 0) (f-regs 0)
  698. OP_CLASS_JMP (f-op-src 0) OP_CODE_EXIT)
  699. (c-call VOID "bpfbf_exit")
  700. ())
  701. ;;; Atomic instructions
  702. ;; The atomic exchange-and-add instructions come in two flavors: one
  703. ;; for swapping 64-bit quantities and another for 32-bit quantities.
  704. (define-pmacro (sem-exchange-and-add x-endian x-mode)
  705. (sequence VOID ((x-mode tmp))
  706. ;; XXX acquire lock in simulator... as a hardware element?
  707. (set x-mode tmp (mem x-mode (add DI (.sym dst x-endian) offset16)))
  708. (set x-mode
  709. (mem x-mode (add DI (.sym dst x-endian) offset16))
  710. (add x-mode tmp (.sym src x-endian)))))
  711. (define-pmacro (define-atomic-insns x-endian)
  712. (begin
  713. (dni (.str "xadddw" x-endian)
  714. "xadddw"
  715. (endian-isas x-endian)
  716. (.str "xadddw [$dst" x-endian "+$offset16],$src" x-endian)
  717. (+ (f-imm32 0) (.sym src x-endian) (.sym dst x-endian)
  718. offset16 OP_MODE_XADD OP_SIZE_DW OP_CLASS_STX)
  719. (sem-exchange-and-add x-endian DI)
  720. ())
  721. (dni (.str "xaddw" x-endian)
  722. "xaddw"
  723. (endian-isas x-endian)
  724. (.str "xaddw [$dst" x-endian "+$offset16],$src" x-endian)
  725. (+ (f-imm32 0) (.sym src x-endian) (.sym dst x-endian)
  726. offset16 OP_MODE_XADD OP_SIZE_W OP_CLASS_STX)
  727. (sem-exchange-and-add x-endian SI)
  728. ())))
  729. (define-atomic-insns le)
  730. (define-atomic-insns be)
  731. ;;; Breakpoint instruction
  732. ;; The brkpt instruction is used by the BPF simulator and it doesn't
  733. ;; really belong to the eBPF instruction set.
  734. (dni "brkpt" "brkpt" (all-isas) "brkpt"
  735. (+ (f-imm32 0) (f-offset16 0) (f-regs 0)
  736. OP_CLASS_ALU OP_SRC_X OP_CODE_NEG)
  737. (c-call VOID "bpfbf_breakpoint")
  738. ())