Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "disassembler_mips.h" |
| 18 | |
| 19 | #include <iostream> |
| 20 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 21 | #include "base/logging.h" |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 22 | #include "base/stringprintf.h" |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 23 | #include "thread.h" |
| 24 | |
| 25 | namespace art { |
| 26 | namespace mips { |
| 27 | |
| 28 | struct MipsInstruction { |
| 29 | uint32_t mask; |
| 30 | uint32_t value; |
| 31 | const char* name; |
| 32 | const char* args_fmt; |
| 33 | |
| 34 | bool Matches(uint32_t instruction) const { |
| 35 | return (instruction & mask) == value; |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | static const uint32_t kOpcodeShift = 26; |
| 40 | |
| 41 | static const uint32_t kCop1 = (17 << kOpcodeShift); |
| 42 | |
| 43 | static const uint32_t kITypeMask = (0x3f << kOpcodeShift); |
| 44 | static const uint32_t kJTypeMask = (0x3f << kOpcodeShift); |
| 45 | static const uint32_t kRTypeMask = ((0x3f << kOpcodeShift) | (0x3f)); |
| 46 | static const uint32_t kSpecial2Mask = (0x3f << kOpcodeShift); |
| 47 | static const uint32_t kFpMask = kRTypeMask; |
| 48 | |
| 49 | static const MipsInstruction gMipsInstructions[] = { |
| 50 | // "sll r0, r0, 0" is the canonical "nop", used in delay slots. |
| 51 | { 0xffffffff, 0, "nop", "" }, |
| 52 | |
| 53 | // R-type instructions. |
| 54 | { kRTypeMask, 0, "sll", "DTA", }, |
| 55 | // 0, 1, movci |
| 56 | { kRTypeMask, 2, "srl", "DTA", }, |
| 57 | { kRTypeMask, 3, "sra", "DTA", }, |
| 58 | { kRTypeMask, 4, "sllv", "DTS", }, |
| 59 | { kRTypeMask, 6, "srlv", "DTS", }, |
| 60 | { kRTypeMask, 7, "srav", "DTS", }, |
| 61 | { kRTypeMask, 8, "jr", "S", }, |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 62 | { kRTypeMask | (0x1f << 11), 9 | (31 << 11), "jalr", "S", }, // rd = 31 is implicit. |
| 63 | { kRTypeMask, 9, "jalr", "DS", }, // General case. |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 64 | { kRTypeMask | (0x1f << 6), 10, "movz", "DST", }, |
| 65 | { kRTypeMask | (0x1f << 6), 11, "movn", "DST", }, |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 66 | { kRTypeMask, 12, "syscall", "", }, // TODO: code |
| 67 | { kRTypeMask, 13, "break", "", }, // TODO: code |
| 68 | { kRTypeMask, 15, "sync", "", }, // TODO: type |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 69 | { kRTypeMask, 16, "mfhi", "D", }, |
| 70 | { kRTypeMask, 17, "mthi", "S", }, |
| 71 | { kRTypeMask, 18, "mflo", "D", }, |
| 72 | { kRTypeMask, 19, "mtlo", "S", }, |
| 73 | { kRTypeMask, 24, "mult", "ST", }, |
| 74 | { kRTypeMask, 25, "multu", "ST", }, |
| 75 | { kRTypeMask, 26, "div", "ST", }, |
| 76 | { kRTypeMask, 27, "divu", "ST", }, |
| 77 | { kRTypeMask, 32, "add", "DST", }, |
| 78 | { kRTypeMask, 33, "addu", "DST", }, |
| 79 | { kRTypeMask, 34, "sub", "DST", }, |
| 80 | { kRTypeMask, 35, "subu", "DST", }, |
| 81 | { kRTypeMask, 36, "and", "DST", }, |
| 82 | { kRTypeMask, 37, "or", "DST", }, |
| 83 | { kRTypeMask, 38, "xor", "DST", }, |
| 84 | { kRTypeMask, 39, "nor", "DST", }, |
| 85 | { kRTypeMask, 42, "slt", "DST", }, |
| 86 | { kRTypeMask, 43, "sltu", "DST", }, |
| 87 | // 0, 48, tge |
| 88 | // 0, 49, tgeu |
| 89 | // 0, 50, tlt |
| 90 | // 0, 51, tltu |
| 91 | // 0, 52, teq |
| 92 | // 0, 54, tne |
| 93 | |
| 94 | // SPECIAL2 |
| 95 | { kSpecial2Mask | 0x7ff, (28 << kOpcodeShift) | 2, "mul", "DST" }, |
| 96 | { kSpecial2Mask | 0x7ff, (28 << kOpcodeShift) | 32, "clz", "DS" }, |
| 97 | { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 0, "madd", "ST" }, |
| 98 | { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 1, "maddu", "ST" }, |
| 99 | { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 2, "mul", "DST" }, |
| 100 | { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 4, "msub", "ST" }, |
| 101 | { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 5, "msubu", "ST" }, |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 102 | { kSpecial2Mask | 0x3f, (28 << kOpcodeShift) | 0x3f, "sdbbp", "" }, // TODO: code |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 103 | |
| 104 | // J-type instructions. |
| 105 | { kJTypeMask, 2 << kOpcodeShift, "j", "L" }, |
| 106 | { kJTypeMask, 3 << kOpcodeShift, "jal", "L" }, |
| 107 | |
| 108 | // I-type instructions. |
| 109 | { kITypeMask, 4 << kOpcodeShift, "beq", "STB" }, |
| 110 | { kITypeMask, 5 << kOpcodeShift, "bne", "STB" }, |
| 111 | { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (1 << 16), "bgez", "SB" }, |
| 112 | { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (0 << 16), "bltz", "SB" }, |
| 113 | { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (2 << 16), "bltzl", "SB" }, |
| 114 | { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (16 << 16), "bltzal", "SB" }, |
| 115 | { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (18 << 16), "bltzall", "SB" }, |
| 116 | { kITypeMask | (0x1f << 16), 6 << kOpcodeShift | (0 << 16), "blez", "SB" }, |
| 117 | { kITypeMask | (0x1f << 16), 7 << kOpcodeShift | (0 << 16), "bgtz", "SB" }, |
| 118 | |
| 119 | { 0xffff0000, (4 << kOpcodeShift), "b", "B" }, |
| 120 | { 0xffff0000, (1 << kOpcodeShift) | (17 << 16), "bal", "B" }, |
| 121 | |
| 122 | { kITypeMask, 8 << kOpcodeShift, "addi", "TSi", }, |
| 123 | { kITypeMask, 9 << kOpcodeShift, "addiu", "TSi", }, |
| 124 | { kITypeMask, 10 << kOpcodeShift, "slti", "TSi", }, |
| 125 | { kITypeMask, 11 << kOpcodeShift, "sltiu", "TSi", }, |
| 126 | { kITypeMask, 12 << kOpcodeShift, "andi", "TSi", }, |
| 127 | { kITypeMask, 13 << kOpcodeShift, "ori", "TSi", }, |
| 128 | { kITypeMask, 14 << kOpcodeShift, "ori", "TSi", }, |
| 129 | { kITypeMask, 15 << kOpcodeShift, "lui", "TI", }, |
| 130 | |
Brian Carlstrom | d74e41b | 2013-03-24 23:47:01 -0700 | [diff] [blame] | 131 | { kITypeMask, 32u << kOpcodeShift, "lb", "TO", }, |
| 132 | { kITypeMask, 33u << kOpcodeShift, "lh", "TO", }, |
| 133 | { kITypeMask, 35u << kOpcodeShift, "lw", "TO", }, |
| 134 | { kITypeMask, 36u << kOpcodeShift, "lbu", "TO", }, |
| 135 | { kITypeMask, 37u << kOpcodeShift, "lhu", "TO", }, |
| 136 | { kITypeMask, 40u << kOpcodeShift, "sb", "TO", }, |
| 137 | { kITypeMask, 41u << kOpcodeShift, "sh", "TO", }, |
| 138 | { kITypeMask, 43u << kOpcodeShift, "sw", "TO", }, |
| 139 | { kITypeMask, 49u << kOpcodeShift, "lwc1", "tO", }, |
| 140 | { kITypeMask, 57u << kOpcodeShift, "swc1", "tO", }, |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 141 | |
| 142 | // Floating point. |
| 143 | { kFpMask, kCop1 | 0, "add", "fdst" }, |
| 144 | { kFpMask, kCop1 | 1, "sub", "fdst" }, |
| 145 | { kFpMask, kCop1 | 2, "mul", "fdst" }, |
| 146 | { kFpMask, kCop1 | 3, "div", "fdst" }, |
| 147 | { kFpMask | (0x1f << 16), kCop1 | 4, "sqrt", "fdst" }, |
| 148 | { kFpMask | (0x1f << 16), kCop1 | 5, "abs", "fds" }, |
| 149 | { kFpMask | (0x1f << 16), kCop1 | 6, "mov", "fds" }, |
| 150 | { kFpMask | (0x1f << 16), kCop1 | 7, "neg", "fds" }, |
| 151 | { kFpMask | (0x1f << 16), kCop1 | 8, "round.l", "fds" }, |
| 152 | { kFpMask | (0x1f << 16), kCop1 | 9, "trunc.l", "fds" }, |
| 153 | { kFpMask | (0x1f << 16), kCop1 | 10, "ceil.l", "fds" }, |
| 154 | { kFpMask | (0x1f << 16), kCop1 | 11, "floor.l", "fds" }, |
| 155 | { kFpMask | (0x1f << 16), kCop1 | 12, "round.w", "fds" }, |
| 156 | { kFpMask | (0x1f << 16), kCop1 | 13, "trunc.w", "fds" }, |
| 157 | { kFpMask | (0x1f << 16), kCop1 | 14, "ceil.w", "fds" }, |
| 158 | { kFpMask | (0x1f << 16), kCop1 | 15, "floor.w", "fds" }, |
| 159 | { kFpMask | (0x1f << 16), kCop1 | 32, "cvt.s", "fds" }, |
| 160 | { kFpMask | (0x1f << 16), kCop1 | 33, "cvt.d", "fds" }, |
| 161 | { kFpMask | (0x1f << 16), kCop1 | 36, "cvt.w", "fds" }, |
| 162 | { kFpMask | (0x1f << 16), kCop1 | 37, "cvt.l", "fds" }, |
| 163 | { kFpMask | (0x1f << 16), kCop1 | 38, "cvt.ps", "fds" }, |
| 164 | }; |
| 165 | |
| 166 | static uint32_t ReadU32(const uint8_t* ptr) { |
jeffhao | 4f8f04a | 2012-10-02 18:10:35 -0700 | [diff] [blame] | 167 | // We only support little-endian MIPS. |
| 168 | return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24); |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 171 | size_t DisassemblerMips::Dump(std::ostream& os, const uint8_t* instr_ptr) { |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 172 | uint32_t instruction = ReadU32(instr_ptr); |
| 173 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 174 | uint32_t rs = (instruction >> 21) & 0x1f; // I-type, R-type. |
| 175 | uint32_t rt = (instruction >> 16) & 0x1f; // I-type, R-type. |
| 176 | uint32_t rd = (instruction >> 11) & 0x1f; // R-type. |
| 177 | uint32_t sa = (instruction >> 6) & 0x1f; // R-type. |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 178 | |
| 179 | std::string opcode; |
| 180 | std::ostringstream args; |
| 181 | |
| 182 | // TODO: remove this! |
| 183 | uint32_t op = (instruction >> 26) & 0x3f; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 184 | uint32_t function = (instruction & 0x3f); // R-type. |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 185 | opcode = StringPrintf("op=%d fn=%d", op, function); |
| 186 | |
| 187 | for (size_t i = 0; i < arraysize(gMipsInstructions); ++i) { |
| 188 | if (gMipsInstructions[i].Matches(instruction)) { |
| 189 | opcode = gMipsInstructions[i].name; |
| 190 | for (const char* args_fmt = gMipsInstructions[i].args_fmt; *args_fmt; ++args_fmt) { |
| 191 | switch (*args_fmt) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 192 | case 'A': // sa (shift amount). |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 193 | args << sa; |
| 194 | break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 195 | case 'B': // Branch offset. |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 196 | { |
| 197 | int32_t offset = static_cast<int16_t>(instruction & 0xffff); |
| 198 | offset <<= 2; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 199 | offset += 4; // Delay slot. |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 200 | args << FormatInstructionPointer(instr_ptr + offset) |
| 201 | << StringPrintf(" ; %+d", offset); |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 202 | } |
| 203 | break; |
| 204 | case 'D': args << 'r' << rd; break; |
| 205 | case 'd': args << 'f' << rd; break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 206 | case 'f': // Floating point "fmt". |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 207 | { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 208 | size_t fmt = (instruction >> 21) & 0x7; // TODO: other fmts? |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 209 | switch (fmt) { |
| 210 | case 0: opcode += ".s"; break; |
| 211 | case 1: opcode += ".d"; break; |
| 212 | case 4: opcode += ".w"; break; |
| 213 | case 5: opcode += ".l"; break; |
| 214 | case 6: opcode += ".ps"; break; |
| 215 | default: opcode += ".?"; break; |
| 216 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 217 | continue; // No ", ". |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 218 | } |
| 219 | break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 220 | case 'I': // Upper 16-bit immediate. |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 221 | args << reinterpret_cast<void*>((instruction & 0xffff) << 16); |
| 222 | break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 223 | case 'i': // Sign-extended lower 16-bit immediate. |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 224 | args << static_cast<int16_t>(instruction & 0xffff); |
| 225 | break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 226 | case 'L': // Jump label. |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 227 | { |
| 228 | // TODO: is this right? |
| 229 | uint32_t instr_index = (instruction & 0x1ffffff); |
| 230 | uint32_t target = (instr_index << 2); |
| 231 | target |= (reinterpret_cast<uintptr_t>(instr_ptr + 4) & 0xf0000000); |
| 232 | args << reinterpret_cast<void*>(target); |
| 233 | } |
| 234 | break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 235 | case 'O': // +x(rs) |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 236 | { |
| 237 | int32_t offset = static_cast<int16_t>(instruction & 0xffff); |
| 238 | args << StringPrintf("%+d(r%d)", offset, rs); |
| 239 | if (rs == 17) { |
| 240 | args << " ; "; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 241 | Thread::DumpThreadOffset<4>(args, offset); |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | break; |
| 245 | case 'S': args << 'r' << rs; break; |
| 246 | case 's': args << 'f' << rs; break; |
| 247 | case 'T': args << 'r' << rt; break; |
| 248 | case 't': args << 'f' << rt; break; |
| 249 | } |
| 250 | if (*(args_fmt + 1)) { |
| 251 | args << ", "; |
| 252 | } |
| 253 | } |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 258 | os << FormatInstructionPointer(instr_ptr) |
| 259 | << StringPrintf(": %08x\t%-7s ", instruction, opcode.c_str()) |
| 260 | << args.str() << '\n'; |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 261 | return 4; |
| 262 | } |
| 263 | |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 264 | void DisassemblerMips::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) { |
| 265 | for (const uint8_t* cur = begin; cur < end; cur += 4) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 266 | Dump(os, cur); |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
| 270 | } // namespace mips |
| 271 | } // namespace art |