LLVM 22.0.0git
BPFDisassembler.cpp
Go to the documentation of this file.
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
15#include "llvm/ADT/ArrayRef.h"
16#include "llvm/MC/MCAsmInfo.h"
17#include "llvm/MC/MCContext.h"
18#include "llvm/MC/MCDecoder.h"
21#include "llvm/MC/MCInst.h"
26#include <cstdint>
27
28using namespace llvm;
29
30#define DEBUG_TYPE "bpf-disassembler"
31
33
34namespace {
35
36/// A disassembler class for BPF.
37class BPFDisassembler : public MCDisassembler {
38public:
39 enum BPF_CLASS {
40 BPF_LD = 0x0,
41 BPF_LDX = 0x1,
42 BPF_ST = 0x2,
43 BPF_STX = 0x3,
44 BPF_ALU = 0x4,
45 BPF_JMP = 0x5,
46 BPF_JMP32 = 0x6,
47 BPF_ALU64 = 0x7
48 };
49
50 enum BPF_SIZE {
51 BPF_W = 0x0,
52 BPF_H = 0x1,
53 BPF_B = 0x2,
54 BPF_DW = 0x3
55 };
56
57 enum BPF_MODE {
58 BPF_IMM = 0x0,
59 BPF_ABS = 0x1,
60 BPF_IND = 0x2,
61 BPF_MEM = 0x3,
62 BPF_MEMSX = 0x4,
63 BPF_ATOMIC = 0x6
64 };
65
66 BPFDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
67 : MCDisassembler(STI, Ctx) {}
68 ~BPFDisassembler() override = default;
69
72 raw_ostream &CStream) const override;
73
74 uint8_t getInstClass(uint64_t Inst) const { return (Inst >> 56) & 0x7; };
75 uint8_t getInstSize(uint64_t Inst) const { return (Inst >> 59) & 0x3; };
76 uint8_t getInstMode(uint64_t Inst) const { return (Inst >> 61) & 0x7; };
77};
78
79} // end anonymous namespace
80
82 const MCSubtargetInfo &STI,
83 MCContext &Ctx) {
84 return new BPFDisassembler(STI, Ctx);
85}
86
89 // Register the disassembler.
96}
97
98static const unsigned GPRDecoderTable[] = {
99 BPF::R0, BPF::R1, BPF::R2, BPF::R3, BPF::R4, BPF::R5,
100 BPF::R6, BPF::R7, BPF::R8, BPF::R9, BPF::R10, BPF::R11};
101
102static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, unsigned RegNo,
103 uint64_t /*Address*/,
104 const MCDisassembler * /*Decoder*/) {
105 if (RegNo > 11)
107
108 unsigned Reg = GPRDecoderTable[RegNo];
111}
112
113static const unsigned GPR32DecoderTable[] = {
114 BPF::W0, BPF::W1, BPF::W2, BPF::W3, BPF::W4, BPF::W5,
115 BPF::W6, BPF::W7, BPF::W8, BPF::W9, BPF::W10, BPF::W11};
116
117static DecodeStatus
118DecodeGPR32RegisterClass(MCInst &Inst, unsigned RegNo, uint64_t /*Address*/,
119 const MCDisassembler * /*Decoder*/) {
120 if (RegNo > 11)
122
123 unsigned Reg = GPR32DecoderTable[RegNo];
126}
127
128static DecodeStatus decodeMemoryOpValue(MCInst &Inst, unsigned Insn,
129 uint64_t Address,
130 const MCDisassembler *Decoder) {
131 unsigned Register = (Insn >> 16) & 0xf;
132 if (Register > 11)
134
136 unsigned Offset = (Insn & 0xffff);
137 Inst.addOperand(MCOperand::createImm(SignExtend32<16>(Offset)));
138
140}
141
142#include "BPFGenDisassemblerTables.inc"
144 uint64_t &Size, uint64_t &Insn,
145 bool IsLittleEndian) {
146 uint64_t Lo, Hi;
147
148 if (Bytes.size() < 8) {
149 Size = 0;
151 }
152
153 Size = 8;
154 if (IsLittleEndian) {
155 Hi = (Bytes[0] << 24) | (Bytes[1] << 16) | (Bytes[2] << 0) | (Bytes[3] << 8);
156 Lo = (Bytes[4] << 0) | (Bytes[5] << 8) | (Bytes[6] << 16) | (Bytes[7] << 24);
157 } else {
158 Hi = (Bytes[0] << 24) | ((Bytes[1] & 0x0F) << 20) | ((Bytes[1] & 0xF0) << 12) |
159 (Bytes[2] << 8) | (Bytes[3] << 0);
160 Lo = (Bytes[4] << 24) | (Bytes[5] << 16) | (Bytes[6] << 8) | (Bytes[7] << 0);
161 }
162 Insn = Make_64(Hi, Lo);
163
165}
166
167DecodeStatus BPFDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
168 ArrayRef<uint8_t> Bytes,
169 uint64_t Address,
170 raw_ostream &CStream) const {
171 bool IsLittleEndian = getContext().getAsmInfo()->isLittleEndian();
172 uint64_t Insn, Hi;
174
175 Result = readInstruction64(Bytes, Address, Size, Insn, IsLittleEndian);
176 if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
177
178 uint8_t InstClass = getInstClass(Insn);
179 uint8_t InstMode = getInstMode(Insn);
180 if ((InstClass == BPF_LDX || InstClass == BPF_STX) &&
181 getInstSize(Insn) != BPF_DW &&
182 (InstMode == BPF_MEM || InstMode == BPF_ATOMIC) &&
183 STI.hasFeature(BPF::ALU32))
184 Result = decodeInstruction(DecoderTableBPFALU3264, Instr, Insn, Address,
185 this, STI);
186 else
187 Result = decodeInstruction(DecoderTableBPF64, Instr, Insn, Address, this,
188 STI);
189
190 if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
191
192 switch (Instr.getOpcode()) {
193 case BPF::LD_imm64:
194 case BPF::LD_pseudo: {
195 if (Bytes.size() < 16) {
196 Size = 0;
198 }
199 Size = 16;
200 if (IsLittleEndian)
201 Hi = (Bytes[12] << 0) | (Bytes[13] << 8) | (Bytes[14] << 16) | (Bytes[15] << 24);
202 else
203 Hi = (Bytes[12] << 24) | (Bytes[13] << 16) | (Bytes[14] << 8) | (Bytes[15] << 0);
204 auto& Op = Instr.getOperand(1);
205 Op.setImm(Make_64(Hi, Op.getImm()));
206 break;
207 }
208 case BPF::LD_ABS_B:
209 case BPF::LD_ABS_H:
210 case BPF::LD_ABS_W:
211 case BPF::LD_IND_B:
212 case BPF::LD_IND_H:
213 case BPF::LD_IND_W: {
214 auto Op = Instr.getOperand(0);
215 Instr.clear();
216 Instr.addOperand(MCOperand::createReg(BPF::R6));
217 Instr.addOperand(Op);
218 break;
219 }
220 }
221
222 return Result;
223}
224
225typedef DecodeStatus (*DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address,
226 const MCDisassembler *Decoder);
MCDisassembler::DecodeStatus DecodeStatus
static DecodeStatus decodeMemoryOpValue(MCInst &Inst, unsigned Insn, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus readInstruction64(ArrayRef< uint8_t > Bytes, uint64_t Address, uint64_t &Size, uint64_t &Insn, bool IsLittleEndian)
DecodeStatus(* DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address, const MCDisassembler *Decoder)
static const unsigned GPR32DecoderTable[]
static const unsigned GPRDecoderTable[]
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFDisassembler()
static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst, unsigned RegNo, uint64_t, const MCDisassembler *)
static MCDisassembler * createBPFDisassembler(const Target &T, const MCSubtargetInfo &STI, MCContext &Ctx)
static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t, const MCDisassembler *)
#define LLVM_ABI
Definition: Compiler.h:213
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:132
uint64_t Size
IRTranslator LLVM IR MI
support::ulittle16_t & Lo
Definition: aarch32.cpp:205
support::ulittle16_t & Hi
Definition: aarch32.cpp:204
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:147
This class represents an Operation in the Expression.
Context object for machine code objects.
Definition: MCContext.h:83
Superclass for all disassemblers.
DecodeStatus
Ternary decode status.
virtual DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, ArrayRef< uint8_t > Bytes, uint64_t Address, raw_ostream &CStream) const =0
Returns the disassembly of a single instruction.
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:188
void addOperand(const MCOperand Op)
Definition: MCInst.h:215
static MCOperand createReg(MCRegister Reg)
Definition: MCInst.h:138
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:145
Generic base class for all target subtargets.
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
Target - Wrapper for Target specific information.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
NodeAddr< InstrNode * > Instr
Definition: RDFGraph.h:389
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
Target & getTheBPFleTarget()
Target & getTheBPFbeTarget()
Target & getTheBPFTarget()
constexpr uint64_t Make_64(uint32_t High, uint32_t Low)
Make a 64-bit integer from a high / low pair of 32-bit integers.
Definition: MathExtras.h:169
static void RegisterMCDisassembler(Target &T, Target::MCDisassemblerCtorTy Fn)
RegisterMCDisassembler - Register a MCDisassembler implementation for the given target.