10b57cec5SDimitry Andric //===- BPFDisassembler.cpp - Disassembler for BPF ---------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file is part of the BPF Disassembler.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "MCTargetDesc/BPFMCTargetDesc.h"
140b57cec5SDimitry Andric #include "TargetInfo/BPFTargetInfo.h"
150b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
160b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
170b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
1881ad6265SDimitry Andric #include "llvm/MC/MCDecoderOps.h"
190b57cec5SDimitry Andric #include "llvm/MC/MCDisassembler/MCDisassembler.h"
200b57cec5SDimitry Andric #include "llvm/MC/MCInst.h"
21349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
220b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
2306c3fb27SDimitry Andric #include "llvm/TargetParser/SubtargetFeature.h"
240b57cec5SDimitry Andric #include <cstdint>
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric using namespace llvm;
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric #define DEBUG_TYPE "bpf-disassembler"
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric typedef MCDisassembler::DecodeStatus DecodeStatus;
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric namespace {
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric /// A disassembler class for BPF.
350b57cec5SDimitry Andric class BPFDisassembler : public MCDisassembler {
360b57cec5SDimitry Andric public:
370b57cec5SDimitry Andric   enum BPF_CLASS {
380b57cec5SDimitry Andric     BPF_LD = 0x0,
390b57cec5SDimitry Andric     BPF_LDX = 0x1,
400b57cec5SDimitry Andric     BPF_ST = 0x2,
410b57cec5SDimitry Andric     BPF_STX = 0x3,
420b57cec5SDimitry Andric     BPF_ALU = 0x4,
430b57cec5SDimitry Andric     BPF_JMP = 0x5,
440b57cec5SDimitry Andric     BPF_JMP32 = 0x6,
450b57cec5SDimitry Andric     BPF_ALU64 = 0x7
460b57cec5SDimitry Andric   };
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric   enum BPF_SIZE {
490b57cec5SDimitry Andric     BPF_W = 0x0,
500b57cec5SDimitry Andric     BPF_H = 0x1,
510b57cec5SDimitry Andric     BPF_B = 0x2,
520b57cec5SDimitry Andric     BPF_DW = 0x3
530b57cec5SDimitry Andric   };
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   enum BPF_MODE {
560b57cec5SDimitry Andric     BPF_IMM = 0x0,
570b57cec5SDimitry Andric     BPF_ABS = 0x1,
580b57cec5SDimitry Andric     BPF_IND = 0x2,
590b57cec5SDimitry Andric     BPF_MEM = 0x3,
605f757f3fSDimitry Andric     BPF_MEMSX = 0x4,
61e8d8bef9SDimitry Andric     BPF_ATOMIC = 0x6
620b57cec5SDimitry Andric   };
630b57cec5SDimitry Andric 
BPFDisassembler(const MCSubtargetInfo & STI,MCContext & Ctx)640b57cec5SDimitry Andric   BPFDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
650b57cec5SDimitry Andric       : MCDisassembler(STI, Ctx) {}
660b57cec5SDimitry Andric   ~BPFDisassembler() override = default;
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric   DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
690b57cec5SDimitry Andric                               ArrayRef<uint8_t> Bytes, uint64_t Address,
700b57cec5SDimitry Andric                               raw_ostream &CStream) const override;
710b57cec5SDimitry Andric 
getInstClass(uint64_t Inst) const720b57cec5SDimitry Andric   uint8_t getInstClass(uint64_t Inst) const { return (Inst >> 56) & 0x7; };
getInstSize(uint64_t Inst) const730b57cec5SDimitry Andric   uint8_t getInstSize(uint64_t Inst) const { return (Inst >> 59) & 0x3; };
getInstMode(uint64_t Inst) const740b57cec5SDimitry Andric   uint8_t getInstMode(uint64_t Inst) const { return (Inst >> 61) & 0x7; };
750b57cec5SDimitry Andric };
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric } // end anonymous namespace
780b57cec5SDimitry Andric 
createBPFDisassembler(const Target & T,const MCSubtargetInfo & STI,MCContext & Ctx)790b57cec5SDimitry Andric static MCDisassembler *createBPFDisassembler(const Target &T,
800b57cec5SDimitry Andric                                              const MCSubtargetInfo &STI,
810b57cec5SDimitry Andric                                              MCContext &Ctx) {
820b57cec5SDimitry Andric   return new BPFDisassembler(STI, Ctx);
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric 
LLVMInitializeBPFDisassembler()86480093f4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFDisassembler() {
870b57cec5SDimitry Andric   // Register the disassembler.
880b57cec5SDimitry Andric   TargetRegistry::RegisterMCDisassembler(getTheBPFTarget(),
890b57cec5SDimitry Andric                                          createBPFDisassembler);
900b57cec5SDimitry Andric   TargetRegistry::RegisterMCDisassembler(getTheBPFleTarget(),
910b57cec5SDimitry Andric                                          createBPFDisassembler);
920b57cec5SDimitry Andric   TargetRegistry::RegisterMCDisassembler(getTheBPFbeTarget(),
930b57cec5SDimitry Andric                                          createBPFDisassembler);
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric static const unsigned GPRDecoderTable[] = {
970b57cec5SDimitry Andric     BPF::R0,  BPF::R1,  BPF::R2,  BPF::R3,  BPF::R4,  BPF::R5,
980b57cec5SDimitry Andric     BPF::R6,  BPF::R7,  BPF::R8,  BPF::R9,  BPF::R10, BPF::R11};
990b57cec5SDimitry Andric 
DecodeGPRRegisterClass(MCInst & Inst,unsigned RegNo,uint64_t,const MCDisassembler *)1000b57cec5SDimitry Andric static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, unsigned RegNo,
1010b57cec5SDimitry Andric                                            uint64_t /*Address*/,
10281ad6265SDimitry Andric                                            const MCDisassembler * /*Decoder*/) {
1030b57cec5SDimitry Andric   if (RegNo > 11)
1040b57cec5SDimitry Andric     return MCDisassembler::Fail;
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric   unsigned Reg = GPRDecoderTable[RegNo];
1070b57cec5SDimitry Andric   Inst.addOperand(MCOperand::createReg(Reg));
1080b57cec5SDimitry Andric   return MCDisassembler::Success;
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric static const unsigned GPR32DecoderTable[] = {
1120b57cec5SDimitry Andric     BPF::W0,  BPF::W1,  BPF::W2,  BPF::W3,  BPF::W4,  BPF::W5,
1130b57cec5SDimitry Andric     BPF::W6,  BPF::W7,  BPF::W8,  BPF::W9,  BPF::W10, BPF::W11};
1140b57cec5SDimitry Andric 
11581ad6265SDimitry Andric static DecodeStatus
DecodeGPR32RegisterClass(MCInst & Inst,unsigned RegNo,uint64_t,const MCDisassembler *)11681ad6265SDimitry Andric DecodeGPR32RegisterClass(MCInst &Inst, unsigned RegNo, uint64_t /*Address*/,
11781ad6265SDimitry Andric                          const MCDisassembler * /*Decoder*/) {
1180b57cec5SDimitry Andric   if (RegNo > 11)
1190b57cec5SDimitry Andric     return MCDisassembler::Fail;
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric   unsigned Reg = GPR32DecoderTable[RegNo];
1220b57cec5SDimitry Andric   Inst.addOperand(MCOperand::createReg(Reg));
1230b57cec5SDimitry Andric   return MCDisassembler::Success;
1240b57cec5SDimitry Andric }
1250b57cec5SDimitry Andric 
decodeMemoryOpValue(MCInst & Inst,unsigned Insn,uint64_t Address,const MCDisassembler * Decoder)1260b57cec5SDimitry Andric static DecodeStatus decodeMemoryOpValue(MCInst &Inst, unsigned Insn,
12781ad6265SDimitry Andric                                         uint64_t Address,
12881ad6265SDimitry Andric                                         const MCDisassembler *Decoder) {
1290b57cec5SDimitry Andric   unsigned Register = (Insn >> 16) & 0xf;
1305ffd83dbSDimitry Andric   if (Register > 11)
1315ffd83dbSDimitry Andric     return MCDisassembler::Fail;
1325ffd83dbSDimitry Andric 
1330b57cec5SDimitry Andric   Inst.addOperand(MCOperand::createReg(GPRDecoderTable[Register]));
1340b57cec5SDimitry Andric   unsigned Offset = (Insn & 0xffff);
1350b57cec5SDimitry Andric   Inst.addOperand(MCOperand::createImm(SignExtend32<16>(Offset)));
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric   return MCDisassembler::Success;
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric #include "BPFGenDisassemblerTables.inc"
readInstruction64(ArrayRef<uint8_t> Bytes,uint64_t Address,uint64_t & Size,uint64_t & Insn,bool IsLittleEndian)1410b57cec5SDimitry Andric static DecodeStatus readInstruction64(ArrayRef<uint8_t> Bytes, uint64_t Address,
1420b57cec5SDimitry Andric                                       uint64_t &Size, uint64_t &Insn,
1430b57cec5SDimitry Andric                                       bool IsLittleEndian) {
1440b57cec5SDimitry Andric   uint64_t Lo, Hi;
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric   if (Bytes.size() < 8) {
1470b57cec5SDimitry Andric     Size = 0;
1480b57cec5SDimitry Andric     return MCDisassembler::Fail;
1490b57cec5SDimitry Andric   }
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric   Size = 8;
1520b57cec5SDimitry Andric   if (IsLittleEndian) {
1530b57cec5SDimitry Andric     Hi = (Bytes[0] << 24) | (Bytes[1] << 16) | (Bytes[2] << 0) | (Bytes[3] << 8);
1540b57cec5SDimitry Andric     Lo = (Bytes[4] << 0) | (Bytes[5] << 8) | (Bytes[6] << 16) | (Bytes[7] << 24);
1550b57cec5SDimitry Andric   } else {
1560b57cec5SDimitry Andric     Hi = (Bytes[0] << 24) | ((Bytes[1] & 0x0F) << 20) | ((Bytes[1] & 0xF0) << 12) |
1570b57cec5SDimitry Andric          (Bytes[2] << 8) | (Bytes[3] << 0);
1580b57cec5SDimitry Andric     Lo = (Bytes[4] << 24) | (Bytes[5] << 16) | (Bytes[6] << 8) | (Bytes[7] << 0);
1590b57cec5SDimitry Andric   }
1600b57cec5SDimitry Andric   Insn = Make_64(Hi, Lo);
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   return MCDisassembler::Success;
1630b57cec5SDimitry Andric }
1640b57cec5SDimitry Andric 
getInstruction(MCInst & Instr,uint64_t & Size,ArrayRef<uint8_t> Bytes,uint64_t Address,raw_ostream & CStream) const1650b57cec5SDimitry Andric DecodeStatus BPFDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
1660b57cec5SDimitry Andric                                              ArrayRef<uint8_t> Bytes,
1670b57cec5SDimitry Andric                                              uint64_t Address,
1680b57cec5SDimitry Andric                                              raw_ostream &CStream) const {
1690b57cec5SDimitry Andric   bool IsLittleEndian = getContext().getAsmInfo()->isLittleEndian();
1700b57cec5SDimitry Andric   uint64_t Insn, Hi;
1710b57cec5SDimitry Andric   DecodeStatus Result;
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric   Result = readInstruction64(Bytes, Address, Size, Insn, IsLittleEndian);
1740b57cec5SDimitry Andric   if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric   uint8_t InstClass = getInstClass(Insn);
1770b57cec5SDimitry Andric   uint8_t InstMode = getInstMode(Insn);
1780b57cec5SDimitry Andric   if ((InstClass == BPF_LDX || InstClass == BPF_STX) &&
1790b57cec5SDimitry Andric       getInstSize(Insn) != BPF_DW &&
180e8d8bef9SDimitry Andric       (InstMode == BPF_MEM || InstMode == BPF_ATOMIC) &&
18106c3fb27SDimitry Andric       STI.hasFeature(BPF::ALU32))
1820b57cec5SDimitry Andric     Result = decodeInstruction(DecoderTableBPFALU3264, Instr, Insn, Address,
1830b57cec5SDimitry Andric                                this, STI);
1840b57cec5SDimitry Andric   else
1850b57cec5SDimitry Andric     Result = decodeInstruction(DecoderTableBPF64, Instr, Insn, Address, this,
1860b57cec5SDimitry Andric                                STI);
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric   if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric   switch (Instr.getOpcode()) {
1910b57cec5SDimitry Andric   case BPF::LD_imm64:
1920b57cec5SDimitry Andric   case BPF::LD_pseudo: {
1930b57cec5SDimitry Andric     if (Bytes.size() < 16) {
1940b57cec5SDimitry Andric       Size = 0;
1950b57cec5SDimitry Andric       return MCDisassembler::Fail;
1960b57cec5SDimitry Andric     }
1970b57cec5SDimitry Andric     Size = 16;
1980b57cec5SDimitry Andric     if (IsLittleEndian)
1990b57cec5SDimitry Andric       Hi = (Bytes[12] << 0) | (Bytes[13] << 8) | (Bytes[14] << 16) | (Bytes[15] << 24);
2000b57cec5SDimitry Andric     else
2010b57cec5SDimitry Andric       Hi = (Bytes[12] << 24) | (Bytes[13] << 16) | (Bytes[14] << 8) | (Bytes[15] << 0);
2020b57cec5SDimitry Andric     auto& Op = Instr.getOperand(1);
2030b57cec5SDimitry Andric     Op.setImm(Make_64(Hi, Op.getImm()));
2040b57cec5SDimitry Andric     break;
2050b57cec5SDimitry Andric   }
2060b57cec5SDimitry Andric   case BPF::LD_ABS_B:
2070b57cec5SDimitry Andric   case BPF::LD_ABS_H:
2080b57cec5SDimitry Andric   case BPF::LD_ABS_W:
2090b57cec5SDimitry Andric   case BPF::LD_IND_B:
2100b57cec5SDimitry Andric   case BPF::LD_IND_H:
2110b57cec5SDimitry Andric   case BPF::LD_IND_W: {
2120b57cec5SDimitry Andric     auto Op = Instr.getOperand(0);
2130b57cec5SDimitry Andric     Instr.clear();
2140b57cec5SDimitry Andric     Instr.addOperand(MCOperand::createReg(BPF::R6));
2150b57cec5SDimitry Andric     Instr.addOperand(Op);
2160b57cec5SDimitry Andric     break;
2170b57cec5SDimitry Andric   }
2180b57cec5SDimitry Andric   }
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric   return Result;
2210b57cec5SDimitry Andric }
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric typedef DecodeStatus (*DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address,
22481ad6265SDimitry Andric                                    const MCDisassembler *Decoder);
225