LLVM  5.0.0
BPFDisassembler.cpp
Go to the documentation of this file.
1 //===- 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 
14 #include "BPF.h"
15 #include "BPFSubtarget.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCInst.h"
25 #include <cstdint>
26 
27 using namespace llvm;
28 
29 #define DEBUG_TYPE "bpf-disassembler"
30 
32 
33 namespace {
34 
35 /// A disassembler class for BPF.
36 class BPFDisassembler : public MCDisassembler {
37 public:
38  BPFDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
39  : MCDisassembler(STI, Ctx) {}
40  ~BPFDisassembler() override = default;
41 
42  DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
43  ArrayRef<uint8_t> Bytes, uint64_t Address,
44  raw_ostream &VStream,
45  raw_ostream &CStream) const override;
46 };
47 
48 } // end anonymous namespace
49 
51  const MCSubtargetInfo &STI,
52  MCContext &Ctx) {
53  return new BPFDisassembler(STI, Ctx);
54 }
55 
56 
57 extern "C" void LLVMInitializeBPFDisassembler() {
58  // Register the disassembler.
65 }
66 
67 static const unsigned GPRDecoderTable[] = {
68  BPF::R0, BPF::R1, BPF::R2, BPF::R3, BPF::R4, BPF::R5,
69  BPF::R6, BPF::R7, BPF::R8, BPF::R9, BPF::R10, BPF::R11};
70 
71 static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, unsigned RegNo,
72  uint64_t /*Address*/,
73  const void * /*Decoder*/) {
74  if (RegNo > 11)
75  return MCDisassembler::Fail;
76 
77  unsigned Reg = GPRDecoderTable[RegNo];
80 }
81 
82 static DecodeStatus decodeMemoryOpValue(MCInst &Inst, unsigned Insn,
83  uint64_t Address, const void *Decoder) {
84  unsigned Register = (Insn >> 16) & 0xf;
86  unsigned Offset = (Insn & 0xffff);
87  Inst.addOperand(MCOperand::createImm(SignExtend32<16>(Offset)));
88 
90 }
91 
92 #include "BPFGenDisassemblerTables.inc"
94  uint64_t &Size, uint64_t &Insn,
95  bool IsLittleEndian) {
96  uint64_t Lo, Hi;
97 
98  if (Bytes.size() < 8) {
99  Size = 0;
100  return MCDisassembler::Fail;
101  }
102 
103  Size = 8;
104  if (IsLittleEndian) {
105  Hi = (Bytes[0] << 24) | (Bytes[1] << 16) | (Bytes[2] << 0) | (Bytes[3] << 8);
106  Lo = (Bytes[4] << 0) | (Bytes[5] << 8) | (Bytes[6] << 16) | (Bytes[7] << 24);
107  } else {
108  Hi = (Bytes[0] << 24) | ((Bytes[1] & 0x0F) << 20) | ((Bytes[1] & 0xF0) << 12) |
109  (Bytes[2] << 8) | (Bytes[3] << 0);
110  Lo = (Bytes[4] << 24) | (Bytes[5] << 16) | (Bytes[6] << 8) | (Bytes[7] << 0);
111  }
112  Insn = Make_64(Hi, Lo);
113 
115 }
116 
117 DecodeStatus BPFDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
118  ArrayRef<uint8_t> Bytes,
119  uint64_t Address,
120  raw_ostream &VStream,
121  raw_ostream &CStream) const {
122  bool IsLittleEndian = getContext().getAsmInfo()->isLittleEndian();
123  uint64_t Insn, Hi;
124  DecodeStatus Result;
125 
126  Result = readInstruction64(Bytes, Address, Size, Insn, IsLittleEndian);
127  if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
128 
129  Result = decodeInstruction(DecoderTableBPF64, Instr, Insn,
130  Address, this, STI);
131  if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
132 
133  switch (Instr.getOpcode()) {
134  case BPF::LD_imm64: {
135  if (Bytes.size() < 16) {
136  Size = 0;
137  return MCDisassembler::Fail;
138  }
139  Size = 16;
140  if (IsLittleEndian)
141  Hi = (Bytes[12] << 0) | (Bytes[13] << 8) | (Bytes[14] << 16) | (Bytes[15] << 24);
142  else
143  Hi = (Bytes[12] << 24) | (Bytes[13] << 16) | (Bytes[14] << 8) | (Bytes[15] << 0);
144  auto& Op = Instr.getOperand(1);
145  Op.setImm(Make_64(Hi, Op.getImm()));
146  break;
147  }
148  case BPF::LD_ABS_B:
149  case BPF::LD_ABS_H:
150  case BPF::LD_ABS_W:
151  case BPF::LD_IND_B:
152  case BPF::LD_IND_H:
153  case BPF::LD_IND_W: {
154  auto Op = Instr.getOperand(0);
155  Instr.clear();
157  Instr.addOperand(Op);
158  break;
159  }
160  }
161 
162  return Result;
163 }
164 
165 typedef DecodeStatus (*DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address,
166  const void *Decoder);
#define R4(n)
void clear()
Definition: MCInst.h:182
DecodeStatus
Ternary decode status.
Superclass for all disassemblers.
static void RegisterMCDisassembler(Target &T, Target::MCDisassemblerCtorTy Fn)
RegisterMCDisassembler - Register a MCDisassembler implementation for the given target.
#define R2(n)
static DecodeStatus readInstruction64(ArrayRef< uint8_t > Bytes, uint64_t Address, uint64_t &Size, uint64_t &Insn, bool IsLittleEndian)
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:116
Reg
All possible values of the reg field in the ModR/M byte.
Context object for machine code objects.
Definition: MCContext.h:59
static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t, const void *)
int decodeInstruction(InternalInstruction *insn, byteReader_t reader, const void *readerArg, dlog_t logger, void *loggerArg, const void *miiArg, uint64_t startLoc, DisassemblerMode mode)
Decode one instruction and store the decoding results in a buffer provided by the consumer...
static const unsigned GPRDecoderTable[]
DecodeStatus(* DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address, const void *Decoder)
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
#define R6(n)
static MCDisassembler * createBPFDisassembler(const Target &T, const MCSubtargetInfo &STI, MCContext &Ctx)
Promote Memory to Register
Definition: Mem2Reg.cpp:101
void LLVMInitializeBPFDisassembler()
unsigned getOpcode() const
Definition: MCInst.h:168
Target - Wrapper for Target specific information.
static DecodeStatus decodeMemoryOpValue(MCInst &Inst, unsigned Insn, uint64_t Address, const void *Decoder)
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
MCSubtargetInfo - Generic base class for all target subtargets.
Target & getTheBPFleTarget()
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:286
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:177
Target & getTheBPFbeTarget()
MCDisassembler::DecodeStatus DecodeStatus
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:123
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:173
Target & getTheBPFTarget()