blob: 9f80b762fe3699e0fabadf3294a05fa229c9d72f [file] [log] [blame]
Alexei Starovoitove6ddac02016-11-20 02:25:00 +00001//===- BPFDisassembler.cpp - Disassembler for BPF ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is part of the BPF Disassembler.
11//
12//===----------------------------------------------------------------------===//
13
Alexei Starovoitove6ddac02016-11-20 02:25:00 +000014#include "MCTargetDesc/BPFMCTargetDesc.h"
Eugene Zelenko4282c402017-01-06 23:06:25 +000015#include "llvm/ADT/ArrayRef.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/MC/MCAsmInfo.h"
17#include "llvm/MC/MCContext.h"
Alexei Starovoitove6ddac02016-11-20 02:25:00 +000018#include "llvm/MC/MCDisassembler/MCDisassembler.h"
19#include "llvm/MC/MCFixedLenDisassembler.h"
20#include "llvm/MC/MCInst.h"
Eugene Zelenko4282c402017-01-06 23:06:25 +000021#include "llvm/Support/MathExtras.h"
Alexei Starovoitove6ddac02016-11-20 02:25:00 +000022#include "llvm/Support/TargetRegistry.h"
Eugene Zelenko4282c402017-01-06 23:06:25 +000023#include <cstdint>
Alexei Starovoitove6ddac02016-11-20 02:25:00 +000024
25using namespace llvm;
26
27#define DEBUG_TYPE "bpf-disassembler"
28
29typedef MCDisassembler::DecodeStatus DecodeStatus;
30
31namespace {
32
33/// A disassembler class for BPF.
34class BPFDisassembler : public MCDisassembler {
35public:
Yonghong Songae961bb2018-02-23 23:49:31 +000036 enum BPF_CLASS {
37 BPF_LD = 0x0,
38 BPF_LDX = 0x1,
39 BPF_ST = 0x2,
40 BPF_STX = 0x3,
41 BPF_ALU = 0x4,
42 BPF_JMP = 0x5,
43 BPF_RES = 0x6,
44 BPF_ALU64 = 0x7
45 };
46
47 enum BPF_SIZE {
48 BPF_W = 0x0,
49 BPF_H = 0x1,
50 BPF_B = 0x2,
51 BPF_DW = 0x3
52 };
53
54 enum BPF_MODE {
55 BPF_IMM = 0x0,
56 BPF_ABS = 0x1,
57 BPF_IND = 0x2,
58 BPF_MEM = 0x3,
59 BPF_LEN = 0x4,
60 BPF_MSH = 0x5,
61 BPF_XADD = 0x6
62 };
63
Alexei Starovoitove6ddac02016-11-20 02:25:00 +000064 BPFDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
65 : MCDisassembler(STI, Ctx) {}
Eugene Zelenko4282c402017-01-06 23:06:25 +000066 ~BPFDisassembler() override = default;
Alexei Starovoitove6ddac02016-11-20 02:25:00 +000067
68 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
69 ArrayRef<uint8_t> Bytes, uint64_t Address,
70 raw_ostream &VStream,
71 raw_ostream &CStream) const override;
Yonghong Songae961bb2018-02-23 23:49:31 +000072
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; };
Alexei Starovoitove6ddac02016-11-20 02:25:00 +000076};
Eugene Zelenko4282c402017-01-06 23:06:25 +000077
78} // end anonymous namespace
Alexei Starovoitove6ddac02016-11-20 02:25:00 +000079
80static MCDisassembler *createBPFDisassembler(const Target &T,
81 const MCSubtargetInfo &STI,
82 MCContext &Ctx) {
83 return new BPFDisassembler(STI, Ctx);
84}
85
86
87extern "C" void LLVMInitializeBPFDisassembler() {
88 // Register the disassembler.
89 TargetRegistry::RegisterMCDisassembler(getTheBPFTarget(),
90 createBPFDisassembler);
91 TargetRegistry::RegisterMCDisassembler(getTheBPFleTarget(),
92 createBPFDisassembler);
93 TargetRegistry::RegisterMCDisassembler(getTheBPFbeTarget(),
94 createBPFDisassembler);
95}
96
97static 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
101static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, unsigned RegNo,
102 uint64_t /*Address*/,
103 const void * /*Decoder*/) {
104 if (RegNo > 11)
105 return MCDisassembler::Fail;
106
107 unsigned Reg = GPRDecoderTable[RegNo];
108 Inst.addOperand(MCOperand::createReg(Reg));
109 return MCDisassembler::Success;
110}
111
Yonghong Songd2e0d1f2017-09-22 04:36:36 +0000112static 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
116static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst, unsigned RegNo,
117 uint64_t /*Address*/,
118 const void * /*Decoder*/) {
119 if (RegNo > 11)
120 return MCDisassembler::Fail;
121
122 unsigned Reg = GPR32DecoderTable[RegNo];
123 Inst.addOperand(MCOperand::createReg(Reg));
124 return MCDisassembler::Success;
125}
126
Alexei Starovoitove6ddac02016-11-20 02:25:00 +0000127static DecodeStatus decodeMemoryOpValue(MCInst &Inst, unsigned Insn,
128 uint64_t Address, const void *Decoder) {
129 unsigned Register = (Insn >> 16) & 0xf;
130 Inst.addOperand(MCOperand::createReg(GPRDecoderTable[Register]));
131 unsigned Offset = (Insn & 0xffff);
132 Inst.addOperand(MCOperand::createImm(SignExtend32<16>(Offset)));
133
134 return MCDisassembler::Success;
135}
136
137#include "BPFGenDisassemblerTables.inc"
Alexei Starovoitove6ddac02016-11-20 02:25:00 +0000138static DecodeStatus readInstruction64(ArrayRef<uint8_t> Bytes, uint64_t Address,
Alexei Starovoitovf7bd5eb2017-04-28 16:51:01 +0000139 uint64_t &Size, uint64_t &Insn,
140 bool IsLittleEndian) {
Alexei Starovoitove6ddac02016-11-20 02:25:00 +0000141 uint64_t Lo, Hi;
142
143 if (Bytes.size() < 8) {
144 Size = 0;
145 return MCDisassembler::Fail;
146 }
147
148 Size = 8;
Alexei Starovoitovf7bd5eb2017-04-28 16:51:01 +0000149 if (IsLittleEndian) {
150 Hi = (Bytes[0] << 24) | (Bytes[1] << 16) | (Bytes[2] << 0) | (Bytes[3] << 8);
151 Lo = (Bytes[4] << 0) | (Bytes[5] << 8) | (Bytes[6] << 16) | (Bytes[7] << 24);
152 } else {
153 Hi = (Bytes[0] << 24) | ((Bytes[1] & 0x0F) << 20) | ((Bytes[1] & 0xF0) << 12) |
154 (Bytes[2] << 8) | (Bytes[3] << 0);
155 Lo = (Bytes[4] << 24) | (Bytes[5] << 16) | (Bytes[6] << 8) | (Bytes[7] << 0);
156 }
Alexei Starovoitove6ddac02016-11-20 02:25:00 +0000157 Insn = Make_64(Hi, Lo);
158
159 return MCDisassembler::Success;
160}
161
162DecodeStatus BPFDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
163 ArrayRef<uint8_t> Bytes,
164 uint64_t Address,
165 raw_ostream &VStream,
166 raw_ostream &CStream) const {
Alexei Starovoitovf7bd5eb2017-04-28 16:51:01 +0000167 bool IsLittleEndian = getContext().getAsmInfo()->isLittleEndian();
168 uint64_t Insn, Hi;
Alexei Starovoitove6ddac02016-11-20 02:25:00 +0000169 DecodeStatus Result;
170
Alexei Starovoitovf7bd5eb2017-04-28 16:51:01 +0000171 Result = readInstruction64(Bytes, Address, Size, Insn, IsLittleEndian);
Alexei Starovoitove6ddac02016-11-20 02:25:00 +0000172 if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
173
Yonghong Songae961bb2018-02-23 23:49:31 +0000174 uint8_t InstClass = getInstClass(Insn);
175 if ((InstClass == BPF_LDX || InstClass == BPF_STX) &&
176 getInstSize(Insn) != BPF_DW &&
177 getInstMode(Insn) == BPF_MEM &&
178 STI.getFeatureBits()[BPF::ALU32])
179 Result = decodeInstruction(DecoderTableBPFALU3264, Instr, Insn, Address,
180 this, STI);
181 else
182 Result = decodeInstruction(DecoderTableBPF64, Instr, Insn, Address, this,
183 STI);
184
Alexei Starovoitove6ddac02016-11-20 02:25:00 +0000185 if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
186
187 switch (Instr.getOpcode()) {
Yonghong Songef29a842017-09-28 22:47:34 +0000188 case BPF::LD_imm64:
189 case BPF::LD_pseudo: {
Alexei Starovoitove6ddac02016-11-20 02:25:00 +0000190 if (Bytes.size() < 16) {
191 Size = 0;
192 return MCDisassembler::Fail;
193 }
194 Size = 16;
Alexei Starovoitovf7bd5eb2017-04-28 16:51:01 +0000195 if (IsLittleEndian)
196 Hi = (Bytes[12] << 0) | (Bytes[13] << 8) | (Bytes[14] << 16) | (Bytes[15] << 24);
197 else
198 Hi = (Bytes[12] << 24) | (Bytes[13] << 16) | (Bytes[14] << 8) | (Bytes[15] << 0);
Alexei Starovoitove6ddac02016-11-20 02:25:00 +0000199 auto& Op = Instr.getOperand(1);
200 Op.setImm(Make_64(Hi, Op.getImm()));
201 break;
202 }
203 case BPF::LD_ABS_B:
204 case BPF::LD_ABS_H:
205 case BPF::LD_ABS_W:
206 case BPF::LD_IND_B:
207 case BPF::LD_IND_H:
208 case BPF::LD_IND_W: {
209 auto Op = Instr.getOperand(0);
210 Instr.clear();
211 Instr.addOperand(MCOperand::createReg(BPF::R6));
212 Instr.addOperand(Op);
213 break;
214 }
215 }
216
217 return Result;
218}
219
220typedef DecodeStatus (*DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address,
221 const void *Decoder);