LLVM  14.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"
20 #include "llvm/MC/MCInst.h"
23 #include <cstdint>
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "bpf-disassembler"
28 
30 
31 namespace {
32 
33 /// A disassembler class for BPF.
34 class BPFDisassembler : public MCDisassembler {
35 public:
36  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_JMP32 = 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_ATOMIC = 0x6
62  };
63 
64  BPFDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
65  : MCDisassembler(STI, Ctx) {}
66  ~BPFDisassembler() override = default;
67 
68  DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
70  raw_ostream &CStream) const override;
71 
72  uint8_t getInstClass(uint64_t Inst) const { return (Inst >> 56) & 0x7; };
73  uint8_t getInstSize(uint64_t Inst) const { return (Inst >> 59) & 0x3; };
74  uint8_t getInstMode(uint64_t Inst) const { return (Inst >> 61) & 0x7; };
75 };
76 
77 } // end anonymous namespace
78 
80  const MCSubtargetInfo &STI,
81  MCContext &Ctx) {
82  return new BPFDisassembler(STI, Ctx);
83 }
84 
85 
87  // Register the disassembler.
94 }
95 
96 static const unsigned GPRDecoderTable[] = {
97  BPF::R0, BPF::R1, BPF::R2, BPF::R3, BPF::R4, BPF::R5,
98  BPF::R6, BPF::R7, BPF::R8, BPF::R9, BPF::R10, BPF::R11};
99 
100 static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, unsigned RegNo,
101  uint64_t /*Address*/,
102  const void * /*Decoder*/) {
103  if (RegNo > 11)
104  return MCDisassembler::Fail;
105 
106  unsigned Reg = GPRDecoderTable[RegNo];
109 }
110 
111 static const unsigned GPR32DecoderTable[] = {
112  BPF::W0, BPF::W1, BPF::W2, BPF::W3, BPF::W4, BPF::W5,
113  BPF::W6, BPF::W7, BPF::W8, BPF::W9, BPF::W10, BPF::W11};
114 
115 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst, unsigned RegNo,
116  uint64_t /*Address*/,
117  const void * /*Decoder*/) {
118  if (RegNo > 11)
119  return MCDisassembler::Fail;
120 
121  unsigned Reg = GPR32DecoderTable[RegNo];
124 }
125 
126 static DecodeStatus decodeMemoryOpValue(MCInst &Inst, unsigned Insn,
127  uint64_t Address, const void *Decoder) {
128  unsigned Register = (Insn >> 16) & 0xf;
129  if (Register > 11)
130  return MCDisassembler::Fail;
131 
133  unsigned Offset = (Insn & 0xffff);
134  Inst.addOperand(MCOperand::createImm(SignExtend32<16>(Offset)));
135 
137 }
138 
139 #include "BPFGenDisassemblerTables.inc"
141  uint64_t &Size, uint64_t &Insn,
142  bool IsLittleEndian) {
143  uint64_t Lo, Hi;
144 
145  if (Bytes.size() < 8) {
146  Size = 0;
147  return MCDisassembler::Fail;
148  }
149 
150  Size = 8;
151  if (IsLittleEndian) {
152  Hi = (Bytes[0] << 24) | (Bytes[1] << 16) | (Bytes[2] << 0) | (Bytes[3] << 8);
153  Lo = (Bytes[4] << 0) | (Bytes[5] << 8) | (Bytes[6] << 16) | (Bytes[7] << 24);
154  } else {
155  Hi = (Bytes[0] << 24) | ((Bytes[1] & 0x0F) << 20) | ((Bytes[1] & 0xF0) << 12) |
156  (Bytes[2] << 8) | (Bytes[3] << 0);
157  Lo = (Bytes[4] << 24) | (Bytes[5] << 16) | (Bytes[6] << 8) | (Bytes[7] << 0);
158  }
159  Insn = Make_64(Hi, Lo);
160 
162 }
163 
164 DecodeStatus BPFDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
165  ArrayRef<uint8_t> Bytes,
166  uint64_t Address,
167  raw_ostream &CStream) const {
168  bool IsLittleEndian = getContext().getAsmInfo()->isLittleEndian();
169  uint64_t Insn, Hi;
171 
172  Result = readInstruction64(Bytes, Address, Size, Insn, IsLittleEndian);
173  if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
174 
175  uint8_t InstClass = getInstClass(Insn);
176  uint8_t InstMode = getInstMode(Insn);
177  if ((InstClass == BPF_LDX || InstClass == BPF_STX) &&
178  getInstSize(Insn) != BPF_DW &&
179  (InstMode == BPF_MEM || InstMode == BPF_ATOMIC) &&
180  STI.getFeatureBits()[BPF::ALU32])
181  Result = decodeInstruction(DecoderTableBPFALU3264, Instr, Insn, Address,
182  this, STI);
183  else
184  Result = decodeInstruction(DecoderTableBPF64, Instr, Insn, Address, this,
185  STI);
186 
187  if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
188 
189  switch (Instr.getOpcode()) {
190  case BPF::LD_imm64:
191  case BPF::LD_pseudo: {
192  if (Bytes.size() < 16) {
193  Size = 0;
194  return MCDisassembler::Fail;
195  }
196  Size = 16;
197  if (IsLittleEndian)
198  Hi = (Bytes[12] << 0) | (Bytes[13] << 8) | (Bytes[14] << 16) | (Bytes[15] << 24);
199  else
200  Hi = (Bytes[12] << 24) | (Bytes[13] << 16) | (Bytes[14] << 8) | (Bytes[15] << 0);
201  auto& Op = Instr.getOperand(1);
202  Op.setImm(Make_64(Hi, Op.getImm()));
203  break;
204  }
205  case BPF::LD_ABS_B:
206  case BPF::LD_ABS_H:
207  case BPF::LD_ABS_W:
208  case BPF::LD_IND_B:
209  case BPF::LD_IND_H:
210  case BPF::LD_IND_W: {
211  auto Op = Instr.getOperand(0);
212  Instr.clear();
214  Instr.addOperand(Op);
215  break;
216  }
217  }
218 
219  return Result;
220 }
221 
222 typedef DecodeStatus (*DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address,
223  const void *Decoder);
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
BPFMCTargetDesc.h
MCFixedLenDisassembler.h
llvm::getTheBPFleTarget
Target & getTheBPFleTarget()
Definition: BPFTargetInfo.cpp:14
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:103
MathExtras.h
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
Reg
unsigned Reg
Definition: MachineSink.cpp:1566
MCDisassembler.h
T
llvm::MCOperand::createImm
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:141
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:72
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:137
llvm::MipsISD::Lo
@ Lo
Definition: MipsISelLowering.h:79
R4
#define R4(n)
llvm::TargetRegistry::RegisterMCDisassembler
static void RegisterMCDisassembler(Target &T, Target::MCDisassemblerCtorTy Fn)
RegisterMCDisassembler - Register a MCDisassembler implementation for the given target.
Definition: TargetRegistry.h:916
DecodeStatus
MCDisassembler::DecodeStatus DecodeStatus
Definition: BPFDisassembler.cpp:29
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
BPFTargetInfo.h
llvm::MipsISD::Hi
@ Hi
Definition: MipsISelLowering.h:75
R2
#define R2(n)
decodeMemoryOpValue
static DecodeStatus decodeMemoryOpValue(MCInst &Inst, unsigned Insn, uint64_t Address, const void *Decoder)
Definition: BPFDisassembler.cpp:126
readInstruction64
static DecodeStatus readInstruction64(ArrayRef< uint8_t > Bytes, uint64_t Address, uint64_t &Size, uint64_t &Insn, bool IsLittleEndian)
Definition: BPFDisassembler.cpp:140
MCContext.h
llvm::ms_demangle::QualifierMangleMode::Result
@ Result
llvm::MCDisassembler::Success
@ Success
Definition: MCDisassembler.h:103
MCInst.h
GPR32DecoderTable
static const unsigned GPR32DecoderTable[]
Definition: BPFDisassembler.cpp:111
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::MCDisassembler::DecodeStatus
DecodeStatus
Ternary decode status.
Definition: MCDisassembler.h:100
DecodeGPRRegisterClass
static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t, const void *)
Definition: BPFDisassembler.cpp:100
llvm::MCInst::addOperand
void addOperand(const MCOperand Op)
Definition: MCInst.h:210
GPRDecoderTable
static const unsigned GPRDecoderTable[]
Definition: BPFDisassembler.cpp:96
llvm::Make_64
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:358
uint64_t
LLVM_EXTERNAL_VISIBILITY
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:132
llvm::MCDisassembler
Superclass for all disassemblers.
Definition: MCDisassembler.h:76
ArrayRef.h
llvm::HighlightColor::Address
@ Address
DecodeGPR32RegisterClass
static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst, unsigned RegNo, uint64_t, const void *)
Definition: BPFDisassembler.cpp:115
llvm::getTheBPFbeTarget
Target & getTheBPFbeTarget()
Definition: BPFTargetInfo.cpp:18
LLVMInitializeBPFDisassembler
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFDisassembler()
Definition: BPFDisassembler.cpp:86
R6
#define R6(n)
llvm::ArrayRef< uint8_t >
MCAsmInfo.h
llvm::MCOperand::createReg
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:134
llvm::MCDisassembler::Fail
@ Fail
Definition: MCDisassembler.h:101
llvm::Register
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:321
llvm::MCInst::getOpcode
unsigned getOpcode() const
Definition: MCInst.h:198
llvm::getTheBPFTarget
Target & getTheBPFTarget()
Definition: BPFTargetInfo.cpp:22
llvm::MCInst::getOperand
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:206
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
TargetRegistry.h
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:75
llvm::MCInst::clear
void clear()
Definition: MCInst.h:215
DecodeFunc
DecodeStatus(* DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address, const void *Decoder)
Definition: BPFDisassembler.cpp:222
createBPFDisassembler
static MCDisassembler * createBPFDisassembler(const Target &T, const MCSubtargetInfo &STI, MCContext &Ctx)
Definition: BPFDisassembler.cpp:79