1 | //===- BPFDisassembler.cpp - Disassembler for BPF ---------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file is part of the BPF Disassembler. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "MCTargetDesc/BPFMCTargetDesc.h" |
14 | #include "TargetInfo/BPFTargetInfo.h" |
15 | #include "llvm/ADT/ArrayRef.h" |
16 | #include "llvm/MC/MCAsmInfo.h" |
17 | #include "llvm/MC/MCContext.h" |
18 | #include "llvm/MC/MCDecoderOps.h" |
19 | #include "llvm/MC/MCDisassembler/MCDisassembler.h" |
20 | #include "llvm/MC/MCInst.h" |
21 | #include "llvm/MC/TargetRegistry.h" |
22 | #include "llvm/Support/Compiler.h" |
23 | #include "llvm/Support/MathExtras.h" |
24 | #include "llvm/TargetParser/SubtargetFeature.h" |
25 | #include <cstdint> |
26 | |
27 | using namespace llvm; |
28 | |
29 | #define DEBUG_TYPE "bpf-disassembler" |
30 | |
31 | typedef MCDisassembler::DecodeStatus DecodeStatus; |
32 | |
33 | namespace { |
34 | |
35 | /// A disassembler class for BPF. |
36 | class BPFDisassembler : public MCDisassembler { |
37 | public: |
38 | enum BPF_CLASS { |
39 | BPF_LD = 0x0, |
40 | BPF_LDX = 0x1, |
41 | BPF_ST = 0x2, |
42 | BPF_STX = 0x3, |
43 | BPF_ALU = 0x4, |
44 | BPF_JMP = 0x5, |
45 | BPF_JMP32 = 0x6, |
46 | BPF_ALU64 = 0x7 |
47 | }; |
48 | |
49 | enum BPF_SIZE { |
50 | BPF_W = 0x0, |
51 | BPF_H = 0x1, |
52 | BPF_B = 0x2, |
53 | BPF_DW = 0x3 |
54 | }; |
55 | |
56 | enum BPF_MODE { |
57 | BPF_IMM = 0x0, |
58 | BPF_ABS = 0x1, |
59 | BPF_IND = 0x2, |
60 | BPF_MEM = 0x3, |
61 | BPF_MEMSX = 0x4, |
62 | BPF_ATOMIC = 0x6 |
63 | }; |
64 | |
65 | BPFDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx) |
66 | : MCDisassembler(STI, Ctx) {} |
67 | ~BPFDisassembler() override = default; |
68 | |
69 | DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, |
70 | ArrayRef<uint8_t> Bytes, uint64_t Address, |
71 | raw_ostream &CStream) const override; |
72 | |
73 | uint8_t getInstClass(uint64_t Inst) const { return (Inst >> 56) & 0x7; }; |
74 | uint8_t getInstSize(uint64_t Inst) const { return (Inst >> 59) & 0x3; }; |
75 | uint8_t getInstMode(uint64_t Inst) const { return (Inst >> 61) & 0x7; }; |
76 | }; |
77 | |
78 | } // end anonymous namespace |
79 | |
80 | static MCDisassembler *createBPFDisassembler(const Target &T, |
81 | const MCSubtargetInfo &STI, |
82 | MCContext &Ctx) { |
83 | return new BPFDisassembler(STI, Ctx); |
84 | } |
85 | |
86 | extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void |
87 | LLVMInitializeBPFDisassembler() { |
88 | // Register the disassembler. |
89 | TargetRegistry::RegisterMCDisassembler(T&: getTheBPFTarget(), |
90 | Fn: createBPFDisassembler); |
91 | TargetRegistry::RegisterMCDisassembler(T&: getTheBPFleTarget(), |
92 | Fn: createBPFDisassembler); |
93 | TargetRegistry::RegisterMCDisassembler(T&: getTheBPFbeTarget(), |
94 | Fn: createBPFDisassembler); |
95 | } |
96 | |
97 | static const unsigned GPRDecoderTable[] = { |
98 | BPF::R0, BPF::R1, BPF::R2, BPF::R3, BPF::R4, BPF::R5, |
99 | BPF::R6, BPF::R7, BPF::R8, BPF::R9, BPF::R10, BPF::R11}; |
100 | |
101 | static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, unsigned RegNo, |
102 | uint64_t /*Address*/, |
103 | const MCDisassembler * /*Decoder*/) { |
104 | if (RegNo > 11) |
105 | return MCDisassembler::Fail; |
106 | |
107 | unsigned Reg = GPRDecoderTable[RegNo]; |
108 | Inst.addOperand(Op: MCOperand::createReg(Reg)); |
109 | return MCDisassembler::Success; |
110 | } |
111 | |
112 | static const unsigned GPR32DecoderTable[] = { |
113 | BPF::W0, BPF::W1, BPF::W2, BPF::W3, BPF::W4, BPF::W5, |
114 | BPF::W6, BPF::W7, BPF::W8, BPF::W9, BPF::W10, BPF::W11}; |
115 | |
116 | static DecodeStatus |
117 | DecodeGPR32RegisterClass(MCInst &Inst, unsigned RegNo, uint64_t /*Address*/, |
118 | const MCDisassembler * /*Decoder*/) { |
119 | if (RegNo > 11) |
120 | return MCDisassembler::Fail; |
121 | |
122 | unsigned Reg = GPR32DecoderTable[RegNo]; |
123 | Inst.addOperand(Op: MCOperand::createReg(Reg)); |
124 | return MCDisassembler::Success; |
125 | } |
126 | |
127 | static DecodeStatus decodeMemoryOpValue(MCInst &Inst, unsigned Insn, |
128 | uint64_t Address, |
129 | const MCDisassembler *Decoder) { |
130 | unsigned Register = (Insn >> 16) & 0xf; |
131 | if (Register > 11) |
132 | return MCDisassembler::Fail; |
133 | |
134 | Inst.addOperand(Op: MCOperand::createReg(Reg: GPRDecoderTable[Register])); |
135 | unsigned Offset = (Insn & 0xffff); |
136 | Inst.addOperand(Op: MCOperand::createImm(Val: SignExtend32<16>(X: Offset))); |
137 | |
138 | return MCDisassembler::Success; |
139 | } |
140 | |
141 | #include "BPFGenDisassemblerTables.inc" |
142 | static DecodeStatus readInstruction64(ArrayRef<uint8_t> Bytes, uint64_t Address, |
143 | uint64_t &Size, uint64_t &Insn, |
144 | bool IsLittleEndian) { |
145 | uint64_t Lo, Hi; |
146 | |
147 | if (Bytes.size() < 8) { |
148 | Size = 0; |
149 | return MCDisassembler::Fail; |
150 | } |
151 | |
152 | Size = 8; |
153 | if (IsLittleEndian) { |
154 | Hi = (Bytes[0] << 24) | (Bytes[1] << 16) | (Bytes[2] << 0) | (Bytes[3] << 8); |
155 | Lo = (Bytes[4] << 0) | (Bytes[5] << 8) | (Bytes[6] << 16) | (Bytes[7] << 24); |
156 | } else { |
157 | Hi = (Bytes[0] << 24) | ((Bytes[1] & 0x0F) << 20) | ((Bytes[1] & 0xF0) << 12) | |
158 | (Bytes[2] << 8) | (Bytes[3] << 0); |
159 | Lo = (Bytes[4] << 24) | (Bytes[5] << 16) | (Bytes[6] << 8) | (Bytes[7] << 0); |
160 | } |
161 | Insn = Make_64(High: Hi, Low: Lo); |
162 | |
163 | return MCDisassembler::Success; |
164 | } |
165 | |
166 | DecodeStatus BPFDisassembler::getInstruction(MCInst &Instr, uint64_t &Size, |
167 | ArrayRef<uint8_t> Bytes, |
168 | uint64_t Address, |
169 | raw_ostream &CStream) const { |
170 | bool IsLittleEndian = getContext().getAsmInfo()->isLittleEndian(); |
171 | uint64_t Insn, Hi; |
172 | DecodeStatus Result; |
173 | |
174 | Result = readInstruction64(Bytes, Address, Size, Insn, IsLittleEndian); |
175 | if (Result == MCDisassembler::Fail) return MCDisassembler::Fail; |
176 | |
177 | uint8_t InstClass = getInstClass(Inst: Insn); |
178 | uint8_t InstMode = getInstMode(Inst: Insn); |
179 | if ((InstClass == BPF_LDX || InstClass == BPF_STX) && |
180 | getInstSize(Inst: Insn) != BPF_DW && |
181 | (InstMode == BPF_MEM || InstMode == BPF_ATOMIC) && |
182 | STI.hasFeature(Feature: BPF::ALU32)) |
183 | Result = decodeInstruction(DecodeTable: DecoderTableBPFALU3264, MI&: Instr, insn: Insn, Address, |
184 | DisAsm: this, STI); |
185 | else |
186 | Result = decodeInstruction(DecodeTable: DecoderTableBPF64, MI&: Instr, insn: Insn, Address, DisAsm: this, |
187 | STI); |
188 | |
189 | if (Result == MCDisassembler::Fail) return MCDisassembler::Fail; |
190 | |
191 | switch (Instr.getOpcode()) { |
192 | case BPF::LD_imm64: |
193 | case BPF::LD_pseudo: { |
194 | if (Bytes.size() < 16) { |
195 | Size = 0; |
196 | return MCDisassembler::Fail; |
197 | } |
198 | Size = 16; |
199 | if (IsLittleEndian) |
200 | Hi = (Bytes[12] << 0) | (Bytes[13] << 8) | (Bytes[14] << 16) | (Bytes[15] << 24); |
201 | else |
202 | Hi = (Bytes[12] << 24) | (Bytes[13] << 16) | (Bytes[14] << 8) | (Bytes[15] << 0); |
203 | auto& Op = Instr.getOperand(i: 1); |
204 | Op.setImm(Make_64(High: Hi, Low: Op.getImm())); |
205 | break; |
206 | } |
207 | case BPF::LD_ABS_B: |
208 | case BPF::LD_ABS_H: |
209 | case BPF::LD_ABS_W: |
210 | case BPF::LD_IND_B: |
211 | case BPF::LD_IND_H: |
212 | case BPF::LD_IND_W: { |
213 | auto Op = Instr.getOperand(i: 0); |
214 | Instr.clear(); |
215 | Instr.addOperand(Op: MCOperand::createReg(Reg: BPF::R6)); |
216 | Instr.addOperand(Op); |
217 | break; |
218 | } |
219 | } |
220 | |
221 | return Result; |
222 | } |
223 | |
224 | typedef DecodeStatus (*DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address, |
225 | const MCDisassembler *Decoder); |
226 | |