Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 1 | //===- BPFDisassembler.cpp - Disassembler for BPF ---------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file is part of the BPF Disassembler. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 13 | #include "MCTargetDesc/BPFMCTargetDesc.h" |
Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/ArrayRef.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCAsmInfo.h" |
| 16 | #include "llvm/MC/MCContext.h" |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCDisassembler/MCDisassembler.h" |
| 18 | #include "llvm/MC/MCFixedLenDisassembler.h" |
| 19 | #include "llvm/MC/MCInst.h" |
Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MathExtras.h" |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 21 | #include "llvm/Support/TargetRegistry.h" |
Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 22 | #include <cstdint> |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | #define DEBUG_TYPE "bpf-disassembler" |
| 27 | |
| 28 | typedef MCDisassembler::DecodeStatus DecodeStatus; |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | /// A disassembler class for BPF. |
| 33 | class BPFDisassembler : public MCDisassembler { |
| 34 | public: |
Yonghong Song | ae961bb | 2018-02-23 23:49:31 +0000 | [diff] [blame] | 35 | enum BPF_CLASS { |
| 36 | BPF_LD = 0x0, |
| 37 | BPF_LDX = 0x1, |
| 38 | BPF_ST = 0x2, |
| 39 | BPF_STX = 0x3, |
| 40 | BPF_ALU = 0x4, |
| 41 | BPF_JMP = 0x5, |
| 42 | BPF_RES = 0x6, |
| 43 | BPF_ALU64 = 0x7 |
| 44 | }; |
| 45 | |
| 46 | enum BPF_SIZE { |
| 47 | BPF_W = 0x0, |
| 48 | BPF_H = 0x1, |
| 49 | BPF_B = 0x2, |
| 50 | BPF_DW = 0x3 |
| 51 | }; |
| 52 | |
| 53 | enum BPF_MODE { |
| 54 | BPF_IMM = 0x0, |
| 55 | BPF_ABS = 0x1, |
| 56 | BPF_IND = 0x2, |
| 57 | BPF_MEM = 0x3, |
| 58 | BPF_LEN = 0x4, |
| 59 | BPF_MSH = 0x5, |
| 60 | BPF_XADD = 0x6 |
| 61 | }; |
| 62 | |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 63 | BPFDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx) |
| 64 | : MCDisassembler(STI, Ctx) {} |
Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 65 | ~BPFDisassembler() override = default; |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 66 | |
| 67 | DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, |
| 68 | ArrayRef<uint8_t> Bytes, uint64_t Address, |
| 69 | raw_ostream &VStream, |
| 70 | raw_ostream &CStream) const override; |
Yonghong Song | ae961bb | 2018-02-23 23:49:31 +0000 | [diff] [blame] | 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; }; |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 75 | }; |
Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 76 | |
| 77 | } // end anonymous namespace |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 78 | |
| 79 | static MCDisassembler *createBPFDisassembler(const Target &T, |
| 80 | const MCSubtargetInfo &STI, |
| 81 | MCContext &Ctx) { |
| 82 | return new BPFDisassembler(STI, Ctx); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | extern "C" void LLVMInitializeBPFDisassembler() { |
| 87 | // Register the disassembler. |
| 88 | TargetRegistry::RegisterMCDisassembler(getTheBPFTarget(), |
| 89 | createBPFDisassembler); |
| 90 | TargetRegistry::RegisterMCDisassembler(getTheBPFleTarget(), |
| 91 | createBPFDisassembler); |
| 92 | TargetRegistry::RegisterMCDisassembler(getTheBPFbeTarget(), |
| 93 | createBPFDisassembler); |
| 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]; |
| 107 | Inst.addOperand(MCOperand::createReg(Reg)); |
| 108 | return MCDisassembler::Success; |
| 109 | } |
| 110 | |
Yonghong Song | d2e0d1f | 2017-09-22 04:36:36 +0000 | [diff] [blame] | 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]; |
| 122 | Inst.addOperand(MCOperand::createReg(Reg)); |
| 123 | return MCDisassembler::Success; |
| 124 | } |
| 125 | |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 126 | static DecodeStatus decodeMemoryOpValue(MCInst &Inst, unsigned Insn, |
| 127 | uint64_t Address, const void *Decoder) { |
| 128 | unsigned Register = (Insn >> 16) & 0xf; |
| 129 | Inst.addOperand(MCOperand::createReg(GPRDecoderTable[Register])); |
| 130 | unsigned Offset = (Insn & 0xffff); |
| 131 | Inst.addOperand(MCOperand::createImm(SignExtend32<16>(Offset))); |
| 132 | |
| 133 | return MCDisassembler::Success; |
| 134 | } |
| 135 | |
| 136 | #include "BPFGenDisassemblerTables.inc" |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 137 | static DecodeStatus readInstruction64(ArrayRef<uint8_t> Bytes, uint64_t Address, |
Alexei Starovoitov | f7bd5eb | 2017-04-28 16:51:01 +0000 | [diff] [blame] | 138 | uint64_t &Size, uint64_t &Insn, |
| 139 | bool IsLittleEndian) { |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 140 | uint64_t Lo, Hi; |
| 141 | |
| 142 | if (Bytes.size() < 8) { |
| 143 | Size = 0; |
| 144 | return MCDisassembler::Fail; |
| 145 | } |
| 146 | |
| 147 | Size = 8; |
Alexei Starovoitov | f7bd5eb | 2017-04-28 16:51:01 +0000 | [diff] [blame] | 148 | if (IsLittleEndian) { |
| 149 | Hi = (Bytes[0] << 24) | (Bytes[1] << 16) | (Bytes[2] << 0) | (Bytes[3] << 8); |
| 150 | Lo = (Bytes[4] << 0) | (Bytes[5] << 8) | (Bytes[6] << 16) | (Bytes[7] << 24); |
| 151 | } else { |
| 152 | Hi = (Bytes[0] << 24) | ((Bytes[1] & 0x0F) << 20) | ((Bytes[1] & 0xF0) << 12) | |
| 153 | (Bytes[2] << 8) | (Bytes[3] << 0); |
| 154 | Lo = (Bytes[4] << 24) | (Bytes[5] << 16) | (Bytes[6] << 8) | (Bytes[7] << 0); |
| 155 | } |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 156 | Insn = Make_64(Hi, Lo); |
| 157 | |
| 158 | return MCDisassembler::Success; |
| 159 | } |
| 160 | |
| 161 | DecodeStatus BPFDisassembler::getInstruction(MCInst &Instr, uint64_t &Size, |
| 162 | ArrayRef<uint8_t> Bytes, |
| 163 | uint64_t Address, |
| 164 | raw_ostream &VStream, |
| 165 | raw_ostream &CStream) const { |
Alexei Starovoitov | f7bd5eb | 2017-04-28 16:51:01 +0000 | [diff] [blame] | 166 | bool IsLittleEndian = getContext().getAsmInfo()->isLittleEndian(); |
| 167 | uint64_t Insn, Hi; |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 168 | DecodeStatus Result; |
| 169 | |
Alexei Starovoitov | f7bd5eb | 2017-04-28 16:51:01 +0000 | [diff] [blame] | 170 | Result = readInstruction64(Bytes, Address, Size, Insn, IsLittleEndian); |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 171 | if (Result == MCDisassembler::Fail) return MCDisassembler::Fail; |
| 172 | |
Yonghong Song | ae961bb | 2018-02-23 23:49:31 +0000 | [diff] [blame] | 173 | uint8_t InstClass = getInstClass(Insn); |
| 174 | if ((InstClass == BPF_LDX || InstClass == BPF_STX) && |
| 175 | getInstSize(Insn) != BPF_DW && |
| 176 | getInstMode(Insn) == BPF_MEM && |
| 177 | STI.getFeatureBits()[BPF::ALU32]) |
| 178 | Result = decodeInstruction(DecoderTableBPFALU3264, Instr, Insn, Address, |
| 179 | this, STI); |
| 180 | else |
| 181 | Result = decodeInstruction(DecoderTableBPF64, Instr, Insn, Address, this, |
| 182 | STI); |
| 183 | |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 184 | if (Result == MCDisassembler::Fail) return MCDisassembler::Fail; |
| 185 | |
| 186 | switch (Instr.getOpcode()) { |
Yonghong Song | ef29a84 | 2017-09-28 22:47:34 +0000 | [diff] [blame] | 187 | case BPF::LD_imm64: |
| 188 | case BPF::LD_pseudo: { |
Alexei Starovoitov | e6ddac0 | 2016-11-20 02:25:00 +0000 | [diff] [blame] | 189 | if (Bytes.size() < 16) { |
| 190 | Size = 0; |
| 191 | return MCDisassembler::Fail; |
| 192 | } |
| 193 | Size = 16; |
Alexei Starovoitov | f7bd5eb | 2017-04-28 16:51:01 +0000 | [diff] [blame] | 194 | if (IsLittleEndian) |
| 195 | Hi = (Bytes[12] << 0) | (Bytes[13] << 8) | (Bytes[14] << 16) | (Bytes[15] << 24); |
| 196 | else |
| 197 | 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] | 198 | auto& Op = Instr.getOperand(1); |
| 199 | Op.setImm(Make_64(Hi, Op.getImm())); |
| 200 | break; |
| 201 | } |
| 202 | case BPF::LD_ABS_B: |
| 203 | case BPF::LD_ABS_H: |
| 204 | case BPF::LD_ABS_W: |
| 205 | case BPF::LD_IND_B: |
| 206 | case BPF::LD_IND_H: |
| 207 | case BPF::LD_IND_W: { |
| 208 | auto Op = Instr.getOperand(0); |
| 209 | Instr.clear(); |
| 210 | Instr.addOperand(MCOperand::createReg(BPF::R6)); |
| 211 | Instr.addOperand(Op); |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return Result; |
| 217 | } |
| 218 | |
| 219 | typedef DecodeStatus (*DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address, |
| 220 | const void *Decoder); |