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: |
Yonghong Song | ae961bb | 2018-02-23 23:49:31 +0000 | [diff] [blame^] | 38 | enum BPF_CLASS { |
| 39 | BPF_LD = 0x0, |
| 40 | BPF_LDX = 0x1, |
| 41 | BPF_ST = 0x2, |
| 42 | BPF_STX = 0x3, |
| 43 | BPF_ALU = 0x4, |
| 44 | BPF_JMP = 0x5, |
| 45 | BPF_RES = 0x6, |
| 46 | BPF_ALU64 = 0x7 |
| 47 | }; |
| 48 | |
| 49 | enum BPF_SIZE { |
| 50 | BPF_W = 0x0, |
| 51 | BPF_H = 0x1, |
| 52 | BPF_B = 0x2, |
| 53 | BPF_DW = 0x3 |
| 54 | }; |
| 55 | |
| 56 | enum BPF_MODE { |
| 57 | BPF_IMM = 0x0, |
| 58 | BPF_ABS = 0x1, |
| 59 | BPF_IND = 0x2, |
| 60 | BPF_MEM = 0x3, |
| 61 | BPF_LEN = 0x4, |
| 62 | BPF_MSH = 0x5, |
| 63 | BPF_XADD = 0x6 |
| 64 | }; |
| 65 | |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 66 | BPFDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx) |
| 67 | : MCDisassembler(STI, Ctx) {} |
Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 68 | ~BPFDisassembler() override = default; |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 69 | |
| 70 | DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, |
| 71 | ArrayRef<uint8_t> Bytes, uint64_t Address, |
| 72 | raw_ostream &VStream, |
| 73 | raw_ostream &CStream) const override; |
Yonghong Song | ae961bb | 2018-02-23 23:49:31 +0000 | [diff] [blame^] | 74 | |
| 75 | uint8_t getInstClass(uint64_t Inst) const { return (Inst >> 56) & 0x7; }; |
| 76 | uint8_t getInstSize(uint64_t Inst) const { return (Inst >> 59) & 0x3; }; |
| 77 | uint8_t getInstMode(uint64_t Inst) const { return (Inst >> 61) & 0x7; }; |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 78 | }; |
Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 79 | |
| 80 | } // end anonymous namespace |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 81 | |
| 82 | static MCDisassembler *createBPFDisassembler(const Target &T, |
| 83 | const MCSubtargetInfo &STI, |
| 84 | MCContext &Ctx) { |
| 85 | return new BPFDisassembler(STI, Ctx); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | extern "C" void LLVMInitializeBPFDisassembler() { |
| 90 | // Register the disassembler. |
| 91 | TargetRegistry::RegisterMCDisassembler(getTheBPFTarget(), |
| 92 | createBPFDisassembler); |
| 93 | TargetRegistry::RegisterMCDisassembler(getTheBPFleTarget(), |
| 94 | createBPFDisassembler); |
| 95 | TargetRegistry::RegisterMCDisassembler(getTheBPFbeTarget(), |
| 96 | createBPFDisassembler); |
| 97 | } |
| 98 | |
| 99 | static const unsigned GPRDecoderTable[] = { |
| 100 | BPF::R0, BPF::R1, BPF::R2, BPF::R3, BPF::R4, BPF::R5, |
| 101 | BPF::R6, BPF::R7, BPF::R8, BPF::R9, BPF::R10, BPF::R11}; |
| 102 | |
| 103 | static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, unsigned RegNo, |
| 104 | uint64_t /*Address*/, |
| 105 | const void * /*Decoder*/) { |
| 106 | if (RegNo > 11) |
| 107 | return MCDisassembler::Fail; |
| 108 | |
| 109 | unsigned Reg = GPRDecoderTable[RegNo]; |
| 110 | Inst.addOperand(MCOperand::createReg(Reg)); |
| 111 | return MCDisassembler::Success; |
| 112 | } |
| 113 | |
Yonghong Song | d2e0d1f | 2017-09-22 04:36:36 +0000 | [diff] [blame] | 114 | static const unsigned GPR32DecoderTable[] = { |
| 115 | BPF::W0, BPF::W1, BPF::W2, BPF::W3, BPF::W4, BPF::W5, |
| 116 | BPF::W6, BPF::W7, BPF::W8, BPF::W9, BPF::W10, BPF::W11}; |
| 117 | |
| 118 | static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst, unsigned RegNo, |
| 119 | uint64_t /*Address*/, |
| 120 | const void * /*Decoder*/) { |
| 121 | if (RegNo > 11) |
| 122 | return MCDisassembler::Fail; |
| 123 | |
| 124 | unsigned Reg = GPR32DecoderTable[RegNo]; |
| 125 | Inst.addOperand(MCOperand::createReg(Reg)); |
| 126 | return MCDisassembler::Success; |
| 127 | } |
| 128 | |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 129 | static DecodeStatus decodeMemoryOpValue(MCInst &Inst, unsigned Insn, |
| 130 | uint64_t Address, const void *Decoder) { |
| 131 | unsigned Register = (Insn >> 16) & 0xf; |
| 132 | Inst.addOperand(MCOperand::createReg(GPRDecoderTable[Register])); |
| 133 | unsigned Offset = (Insn & 0xffff); |
| 134 | Inst.addOperand(MCOperand::createImm(SignExtend32<16>(Offset))); |
| 135 | |
| 136 | return MCDisassembler::Success; |
| 137 | } |
| 138 | |
| 139 | #include "BPFGenDisassemblerTables.inc" |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 140 | static DecodeStatus readInstruction64(ArrayRef<uint8_t> Bytes, uint64_t Address, |
Alexei Starovoitov | f7bd5eb | 2017-04-28 16:51:01 +0000 | [diff] [blame] | 141 | uint64_t &Size, uint64_t &Insn, |
| 142 | bool IsLittleEndian) { |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 143 | uint64_t Lo, Hi; |
| 144 | |
| 145 | if (Bytes.size() < 8) { |
| 146 | Size = 0; |
| 147 | return MCDisassembler::Fail; |
| 148 | } |
| 149 | |
| 150 | Size = 8; |
Alexei Starovoitov | f7bd5eb | 2017-04-28 16:51:01 +0000 | [diff] [blame] | 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 | } |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 159 | Insn = Make_64(Hi, Lo); |
| 160 | |
| 161 | return MCDisassembler::Success; |
| 162 | } |
| 163 | |
| 164 | DecodeStatus BPFDisassembler::getInstruction(MCInst &Instr, uint64_t &Size, |
| 165 | ArrayRef<uint8_t> Bytes, |
| 166 | uint64_t Address, |
| 167 | raw_ostream &VStream, |
| 168 | raw_ostream &CStream) const { |
Alexei Starovoitov | f7bd5eb | 2017-04-28 16:51:01 +0000 | [diff] [blame] | 169 | bool IsLittleEndian = getContext().getAsmInfo()->isLittleEndian(); |
| 170 | uint64_t Insn, Hi; |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 171 | DecodeStatus Result; |
| 172 | |
Alexei Starovoitov | f7bd5eb | 2017-04-28 16:51:01 +0000 | [diff] [blame] | 173 | Result = readInstruction64(Bytes, Address, Size, Insn, IsLittleEndian); |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 174 | if (Result == MCDisassembler::Fail) return MCDisassembler::Fail; |
| 175 | |
Yonghong Song | ae961bb | 2018-02-23 23:49:31 +0000 | [diff] [blame^] | 176 | uint8_t InstClass = getInstClass(Insn); |
| 177 | if ((InstClass == BPF_LDX || InstClass == BPF_STX) && |
| 178 | getInstSize(Insn) != BPF_DW && |
| 179 | getInstMode(Insn) == BPF_MEM && |
| 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 | |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 187 | if (Result == MCDisassembler::Fail) return MCDisassembler::Fail; |
| 188 | |
| 189 | switch (Instr.getOpcode()) { |
Yonghong Song | ef29a84 | 2017-09-28 22:47:34 +0000 | [diff] [blame] | 190 | case BPF::LD_imm64: |
| 191 | case BPF::LD_pseudo: { |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 192 | if (Bytes.size() < 16) { |
| 193 | Size = 0; |
| 194 | return MCDisassembler::Fail; |
| 195 | } |
| 196 | Size = 16; |
Alexei Starovoitov | f7bd5eb | 2017-04-28 16:51:01 +0000 | [diff] [blame] | 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); |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 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(); |
| 213 | Instr.addOperand(MCOperand::createReg(BPF::R6)); |
| 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); |