Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 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" |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 15 | #include "BPFSubtarget.h" |
| 16 | #include "MCTargetDesc/BPFMCTargetDesc.h" |
Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/ArrayRef.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCAsmInfo.h" |
| 19 | #include "llvm/MC/MCContext.h" |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCDisassembler/MCDisassembler.h" |
| 21 | #include "llvm/MC/MCFixedLenDisassembler.h" |
| 22 | #include "llvm/MC/MCInst.h" |
Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MathExtras.h" |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 24 | #include "llvm/Support/TargetRegistry.h" |
Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 25 | #include <cstdint> |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
| 29 | #define DEBUG_TYPE "bpf-disassembler" |
| 30 | |
| 31 | typedef MCDisassembler::DecodeStatus DecodeStatus; |
| 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) {} |
Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 40 | ~BPFDisassembler() override = default; |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 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 | }; |
Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 47 | |
| 48 | } // end anonymous namespace |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 49 | |
| 50 | static MCDisassembler *createBPFDisassembler(const Target &T, |
| 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. |
| 59 | TargetRegistry::RegisterMCDisassembler(getTheBPFTarget(), |
| 60 | createBPFDisassembler); |
| 61 | TargetRegistry::RegisterMCDisassembler(getTheBPFleTarget(), |
| 62 | createBPFDisassembler); |
| 63 | TargetRegistry::RegisterMCDisassembler(getTheBPFbeTarget(), |
| 64 | createBPFDisassembler); |
| 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]; |
| 78 | Inst.addOperand(MCOperand::createReg(Reg)); |
| 79 | return MCDisassembler::Success; |
| 80 | } |
| 81 | |
Yonghong Song | d2e0d1f | 2017-09-22 04:36:36 +0000 | [diff] [blame] | 82 | static const unsigned GPR32DecoderTable[] = { |
| 83 | BPF::W0, BPF::W1, BPF::W2, BPF::W3, BPF::W4, BPF::W5, |
| 84 | BPF::W6, BPF::W7, BPF::W8, BPF::W9, BPF::W10, BPF::W11}; |
| 85 | |
| 86 | static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst, unsigned RegNo, |
| 87 | uint64_t /*Address*/, |
| 88 | const void * /*Decoder*/) { |
| 89 | if (RegNo > 11) |
| 90 | return MCDisassembler::Fail; |
| 91 | |
| 92 | unsigned Reg = GPR32DecoderTable[RegNo]; |
| 93 | Inst.addOperand(MCOperand::createReg(Reg)); |
| 94 | return MCDisassembler::Success; |
| 95 | } |
| 96 | |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 97 | static DecodeStatus decodeMemoryOpValue(MCInst &Inst, unsigned Insn, |
| 98 | uint64_t Address, const void *Decoder) { |
| 99 | unsigned Register = (Insn >> 16) & 0xf; |
| 100 | Inst.addOperand(MCOperand::createReg(GPRDecoderTable[Register])); |
| 101 | unsigned Offset = (Insn & 0xffff); |
| 102 | Inst.addOperand(MCOperand::createImm(SignExtend32<16>(Offset))); |
| 103 | |
| 104 | return MCDisassembler::Success; |
| 105 | } |
| 106 | |
| 107 | #include "BPFGenDisassemblerTables.inc" |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 108 | static DecodeStatus readInstruction64(ArrayRef<uint8_t> Bytes, uint64_t Address, |
Alexei Starovoitov | f7bd5eb | 2017-04-28 16:51:01 +0000 | [diff] [blame] | 109 | uint64_t &Size, uint64_t &Insn, |
| 110 | bool IsLittleEndian) { |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 111 | uint64_t Lo, Hi; |
| 112 | |
| 113 | if (Bytes.size() < 8) { |
| 114 | Size = 0; |
| 115 | return MCDisassembler::Fail; |
| 116 | } |
| 117 | |
| 118 | Size = 8; |
Alexei Starovoitov | f7bd5eb | 2017-04-28 16:51:01 +0000 | [diff] [blame] | 119 | if (IsLittleEndian) { |
| 120 | Hi = (Bytes[0] << 24) | (Bytes[1] << 16) | (Bytes[2] << 0) | (Bytes[3] << 8); |
| 121 | Lo = (Bytes[4] << 0) | (Bytes[5] << 8) | (Bytes[6] << 16) | (Bytes[7] << 24); |
| 122 | } else { |
| 123 | Hi = (Bytes[0] << 24) | ((Bytes[1] & 0x0F) << 20) | ((Bytes[1] & 0xF0) << 12) | |
| 124 | (Bytes[2] << 8) | (Bytes[3] << 0); |
| 125 | Lo = (Bytes[4] << 24) | (Bytes[5] << 16) | (Bytes[6] << 8) | (Bytes[7] << 0); |
| 126 | } |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 127 | Insn = Make_64(Hi, Lo); |
| 128 | |
| 129 | return MCDisassembler::Success; |
| 130 | } |
| 131 | |
| 132 | DecodeStatus BPFDisassembler::getInstruction(MCInst &Instr, uint64_t &Size, |
| 133 | ArrayRef<uint8_t> Bytes, |
| 134 | uint64_t Address, |
| 135 | raw_ostream &VStream, |
| 136 | raw_ostream &CStream) const { |
Alexei Starovoitov | f7bd5eb | 2017-04-28 16:51:01 +0000 | [diff] [blame] | 137 | bool IsLittleEndian = getContext().getAsmInfo()->isLittleEndian(); |
| 138 | uint64_t Insn, Hi; |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 139 | DecodeStatus Result; |
| 140 | |
Alexei Starovoitov | f7bd5eb | 2017-04-28 16:51:01 +0000 | [diff] [blame] | 141 | Result = readInstruction64(Bytes, Address, Size, Insn, IsLittleEndian); |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 142 | if (Result == MCDisassembler::Fail) return MCDisassembler::Fail; |
| 143 | |
| 144 | Result = decodeInstruction(DecoderTableBPF64, Instr, Insn, |
| 145 | Address, this, STI); |
| 146 | if (Result == MCDisassembler::Fail) return MCDisassembler::Fail; |
| 147 | |
| 148 | switch (Instr.getOpcode()) { |
Yonghong Song | ef29a84 | 2017-09-28 22:47:34 +0000 | [diff] [blame^] | 149 | case BPF::LD_imm64: |
| 150 | case BPF::LD_pseudo: { |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 151 | if (Bytes.size() < 16) { |
| 152 | Size = 0; |
| 153 | return MCDisassembler::Fail; |
| 154 | } |
| 155 | Size = 16; |
Alexei Starovoitov | f7bd5eb | 2017-04-28 16:51:01 +0000 | [diff] [blame] | 156 | if (IsLittleEndian) |
| 157 | Hi = (Bytes[12] << 0) | (Bytes[13] << 8) | (Bytes[14] << 16) | (Bytes[15] << 24); |
| 158 | else |
| 159 | Hi = (Bytes[12] << 24) | (Bytes[13] << 16) | (Bytes[14] << 8) | (Bytes[15] << 0); |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 160 | auto& Op = Instr.getOperand(1); |
| 161 | Op.setImm(Make_64(Hi, Op.getImm())); |
| 162 | break; |
| 163 | } |
| 164 | case BPF::LD_ABS_B: |
| 165 | case BPF::LD_ABS_H: |
| 166 | case BPF::LD_ABS_W: |
| 167 | case BPF::LD_IND_B: |
| 168 | case BPF::LD_IND_H: |
| 169 | case BPF::LD_IND_W: { |
| 170 | auto Op = Instr.getOperand(0); |
| 171 | Instr.clear(); |
| 172 | Instr.addOperand(MCOperand::createReg(BPF::R6)); |
| 173 | Instr.addOperand(Op); |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return Result; |
| 179 | } |
| 180 | |
| 181 | typedef DecodeStatus (*DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address, |
| 182 | const void *Decoder); |