| 1 | //===-- RISCVDisassembler.cpp - Disassembler for RISCV --------------------===// |
| 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 | /* Capstone Disassembly Engine */ |
| 11 | /* RISC-V Backend By Rodrigo Cortes Porto <[email protected]> & |
| 12 | Shawn Chang <[email protected]>, HardenedLinux@2018 */ |
| 13 | |
| 14 | #ifdef CAPSTONE_HAS_RISCV |
| 15 | |
| 16 | #include <stdio.h> // DEBUG |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | |
| 20 | #include "../../cs_priv.h" |
| 21 | #include "../../utils.h" |
| 22 | |
| 23 | #include "../../MCInst.h" |
| 24 | #include "../../MCInstrDesc.h" |
| 25 | #include "../../MCFixedLenDisassembler.h" |
| 26 | #include "../../MCRegisterInfo.h" |
| 27 | #include "../../MCDisassembler.h" |
| 28 | #include "../../MathExtras.h" |
| 29 | #include "RISCVBaseInfo.h" |
| 30 | #include "RISCVDisassembler.h" |
| 31 | |
| 32 | |
| 33 | /* Need the feature infos define in |
| 34 | RISCVGenSubtargetInfo.inc. */ |
| 35 | #define GET_SUBTARGETINFO_ENUM |
| 36 | #include "RISCVGenSubtargetInfo.inc" |
| 37 | |
| 38 | /* When we specify the RISCV64 mode, It means It is RV64IMAFD. |
| 39 | Similar, RISCV32 means RV32IMAFD. |
| 40 | */ |
| 41 | static uint64_t getFeatureBits(int mode) |
| 42 | { |
| 43 | uint64_t ret = RISCV_FeatureStdExtM | RISCV_FeatureStdExtA | |
| 44 | RISCV_FeatureStdExtF | RISCV_FeatureStdExtD ; |
| 45 | |
| 46 | if (mode & CS_MODE_RISCV64) |
| 47 | ret |= RISCV_Feature64Bit; |
| 48 | if (mode & CS_MODE_RISCVC) |
| 49 | ret |= RISCV_FeatureStdExtC; |
| 50 | |
| 51 | return ret; |
| 52 | } |
| 53 | |
| 54 | #define GET_REGINFO_ENUM |
| 55 | #define GET_REGINFO_MC_DESC |
| 56 | #include "RISCVGenRegisterInfo.inc" |
| 57 | #define GET_INSTRINFO_ENUM |
| 58 | #include "RISCVGenInstrInfo.inc" |
| 59 | |
| 60 | static const unsigned GPRDecoderTable[] = { |
| 61 | RISCV_X0, RISCV_X1, RISCV_X2, RISCV_X3, |
| 62 | RISCV_X4, RISCV_X5, RISCV_X6, RISCV_X7, |
| 63 | RISCV_X8, RISCV_X9, RISCV_X10, RISCV_X11, |
| 64 | RISCV_X12, RISCV_X13, RISCV_X14, RISCV_X15, |
| 65 | RISCV_X16, RISCV_X17, RISCV_X18, RISCV_X19, |
| 66 | RISCV_X20, RISCV_X21, RISCV_X22, RISCV_X23, |
| 67 | RISCV_X24, RISCV_X25, RISCV_X26, RISCV_X27, |
| 68 | RISCV_X28, RISCV_X29, RISCV_X30, RISCV_X31 |
| 69 | }; |
| 70 | |
| 71 | static DecodeStatus DecodeGPRRegisterClass(MCInst *Inst, uint64_t RegNo, |
| 72 | uint64_t Address, const void *Decoder) |
| 73 | { |
| 74 | unsigned Reg = 0; |
| 75 | |
| 76 | if (RegNo >= ARR_SIZE(GPRDecoderTable)) |
| 77 | return MCDisassembler_Fail; |
| 78 | |
| 79 | // We must define our own mapping from RegNo to register identifier. |
| 80 | // Accessing index RegNo in the register class will work in the case that |
| 81 | // registers were added in ascending order, but not in general. |
| 82 | Reg = GPRDecoderTable[RegNo]; |
| 83 | //Inst.addOperand(MCOperand::createReg(Reg)); |
| 84 | MCOperand_CreateReg0(Inst, Reg); |
| 85 | return MCDisassembler_Success; |
| 86 | } |
| 87 | |
| 88 | static const unsigned FPR32DecoderTable[] = { |
| 89 | RISCV_F0_32, RISCV_F1_32, RISCV_F2_32, RISCV_F3_32, |
| 90 | RISCV_F4_32, RISCV_F5_32, RISCV_F6_32, RISCV_F7_32, |
| 91 | RISCV_F8_32, RISCV_F9_32, RISCV_F10_32, RISCV_F11_32, |
| 92 | RISCV_F12_32, RISCV_F13_32, RISCV_F14_32, RISCV_F15_32, |
| 93 | RISCV_F16_32, RISCV_F17_32, RISCV_F18_32, RISCV_F19_32, |
| 94 | RISCV_F20_32, RISCV_F21_32, RISCV_F22_32, RISCV_F23_32, |
| 95 | RISCV_F24_32, RISCV_F25_32, RISCV_F26_32, RISCV_F27_32, |
| 96 | RISCV_F28_32, RISCV_F29_32, RISCV_F30_32, RISCV_F31_32 |
| 97 | }; |
| 98 | |
| 99 | static DecodeStatus DecodeFPR32RegisterClass(MCInst *Inst, uint64_t RegNo, |
| 100 | uint64_t Address, const void *Decoder) |
| 101 | { |
| 102 | unsigned Reg = 0; |
| 103 | |
| 104 | if (RegNo >= ARR_SIZE(FPR32DecoderTable)) |
| 105 | return MCDisassembler_Fail; |
| 106 | |
| 107 | // We must define our own mapping from RegNo to register identifier. |
| 108 | // Accessing index RegNo in the register class will work in the case that |
| 109 | // registers were added in ascending order, but not in general. |
| 110 | Reg = FPR32DecoderTable[RegNo]; |
| 111 | MCOperand_CreateReg0(Inst, Reg); |
| 112 | return MCDisassembler_Success; |
| 113 | } |
| 114 | |
| 115 | static DecodeStatus DecodeFPR32CRegisterClass(MCInst *Inst, uint64_t RegNo, |
| 116 | uint64_t Address, |
| 117 | const void *Decoder) |
| 118 | { |
| 119 | unsigned Reg = 0; |
| 120 | |
| 121 | if (RegNo > 8) |
| 122 | return MCDisassembler_Fail; |
| 123 | Reg = FPR32DecoderTable[RegNo + 8]; |
| 124 | MCOperand_CreateReg0(Inst, Reg); |
| 125 | return MCDisassembler_Success; |
| 126 | } |
| 127 | |
| 128 | static const unsigned FPR64DecoderTable[] = { |
| 129 | RISCV_F0_64, RISCV_F1_64, RISCV_F2_64, RISCV_F3_64, |
| 130 | RISCV_F4_64, RISCV_F5_64, RISCV_F6_64, RISCV_F7_64, |
| 131 | RISCV_F8_64, RISCV_F9_64, RISCV_F10_64, RISCV_F11_64, |
| 132 | RISCV_F12_64, RISCV_F13_64, RISCV_F14_64, RISCV_F15_64, |
| 133 | RISCV_F16_64, RISCV_F17_64, RISCV_F18_64, RISCV_F19_64, |
| 134 | RISCV_F20_64, RISCV_F21_64, RISCV_F22_64, RISCV_F23_64, |
| 135 | RISCV_F24_64, RISCV_F25_64, RISCV_F26_64, RISCV_F27_64, |
| 136 | RISCV_F28_64, RISCV_F29_64, RISCV_F30_64, RISCV_F31_64 |
| 137 | }; |
| 138 | |
| 139 | static DecodeStatus DecodeFPR64RegisterClass(MCInst *Inst, uint64_t RegNo, |
| 140 | uint64_t Address, const void *Decoder) |
| 141 | { |
| 142 | unsigned Reg = 0; |
| 143 | |
| 144 | if (RegNo >= ARR_SIZE(FPR64DecoderTable)) |
| 145 | return MCDisassembler_Fail; |
| 146 | |
| 147 | // We must define our own mapping from RegNo to register identifier. |
| 148 | // Accessing index RegNo in the register class will work in the case that |
| 149 | // registers were added in ascending order, but not in general. |
| 150 | Reg = FPR64DecoderTable[RegNo]; |
| 151 | MCOperand_CreateReg0(Inst, Reg); |
| 152 | return MCDisassembler_Success; |
| 153 | } |
| 154 | |
| 155 | static DecodeStatus DecodeFPR64CRegisterClass(MCInst *Inst, uint64_t RegNo, |
| 156 | uint64_t Address, |
| 157 | const void *Decoder) |
| 158 | { |
| 159 | unsigned Reg = 0; |
| 160 | |
| 161 | if (RegNo > 8) |
| 162 | return MCDisassembler_Fail; |
| 163 | Reg = FPR64DecoderTable[RegNo + 8]; |
| 164 | MCOperand_CreateReg0(Inst, Reg); |
| 165 | return MCDisassembler_Success; |
| 166 | } |
| 167 | |
| 168 | static DecodeStatus DecodeGPRNoX0RegisterClass(MCInst *Inst, uint64_t RegNo, |
| 169 | uint64_t Address, |
| 170 | const void *Decoder) |
| 171 | { |
| 172 | if (RegNo == 0) |
| 173 | return MCDisassembler_Fail; |
| 174 | return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder); |
| 175 | } |
| 176 | |
| 177 | static DecodeStatus DecodeGPRNoX0X2RegisterClass(MCInst *Inst, uint64_t RegNo, |
| 178 | uint64_t Address, |
| 179 | const void *Decoder) |
| 180 | { |
| 181 | if (RegNo == 2) |
| 182 | return MCDisassembler_Fail; |
| 183 | return DecodeGPRNoX0RegisterClass(Inst, RegNo, Address, Decoder); |
| 184 | } |
| 185 | |
| 186 | static DecodeStatus DecodeGPRCRegisterClass(MCInst *Inst, uint64_t RegNo, |
| 187 | uint64_t Address, |
| 188 | const void *Decoder) |
| 189 | { |
| 190 | unsigned Reg = 0; |
| 191 | |
| 192 | if (RegNo > 8) |
| 193 | return MCDisassembler_Fail; |
| 194 | |
| 195 | Reg = GPRDecoderTable[RegNo + 8]; |
| 196 | MCOperand_CreateReg0(Inst, Reg); |
| 197 | return MCDisassembler_Success; |
| 198 | } |
| 199 | |
| 200 | // Add implied SP operand for instructions *SP compressed instructions. The SP |
| 201 | // operand isn't explicitly encoded in the instruction. |
| 202 | static void addImplySP(MCInst *Inst, int64_t Address, const void *Decoder) |
| 203 | { |
| 204 | if (MCInst_getOpcode(Inst) == RISCV_C_LWSP || |
| 205 | MCInst_getOpcode(Inst) == RISCV_C_SWSP || |
| 206 | MCInst_getOpcode(Inst) == RISCV_C_LDSP || |
| 207 | MCInst_getOpcode(Inst) == RISCV_C_SDSP || |
| 208 | MCInst_getOpcode(Inst) == RISCV_C_FLWSP || |
| 209 | MCInst_getOpcode(Inst) == RISCV_C_FSWSP || |
| 210 | MCInst_getOpcode(Inst) == RISCV_C_FLDSP || |
| 211 | MCInst_getOpcode(Inst) == RISCV_C_FSDSP || |
| 212 | MCInst_getOpcode(Inst) == RISCV_C_ADDI4SPN) { |
| 213 | DecodeGPRRegisterClass(Inst, 2, Address, Decoder); |
| 214 | } |
| 215 | |
| 216 | if (MCInst_getOpcode(Inst) == RISCV_C_ADDI16SP) { |
| 217 | DecodeGPRRegisterClass(Inst, 2, Address, Decoder); |
| 218 | DecodeGPRRegisterClass(Inst, 2, Address, Decoder); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | static DecodeStatus decodeUImmOperand(MCInst *Inst, uint64_t Imm, |
| 223 | int64_t Address, const void *Decoder, |
| 224 | unsigned N) |
| 225 | { |
| 226 | //CS_ASSERT(isUInt<N>(Imm) && "Invalid immediate"); |
| 227 | addImplySP(Inst, Address, Decoder); |
| 228 | //Inst.addOperand(MCOperand::createImm(Imm)); |
| 229 | MCOperand_CreateImm0(Inst, Imm); |
| 230 | return MCDisassembler_Success; |
| 231 | } |
| 232 | |
| 233 | static DecodeStatus decodeUImmNonZeroOperand(MCInst *Inst, uint64_t Imm, |
| 234 | int64_t Address, |
| 235 | const void *Decoder, |
| 236 | unsigned N) |
| 237 | { |
| 238 | if (Imm == 0) |
| 239 | return MCDisassembler_Fail; |
| 240 | return decodeUImmOperand(Inst, Imm, Address, Decoder, N); |
| 241 | } |
| 242 | |
| 243 | static DecodeStatus decodeSImmOperand(MCInst *Inst, uint64_t Imm, |
| 244 | int64_t Address, const void *Decoder, |
| 245 | unsigned N) |
| 246 | { |
| 247 | //CS_ASSERT(isUInt<N>(Imm) && "Invalid immediate"); |
| 248 | addImplySP(Inst, Address, Decoder); |
| 249 | // Sign-extend the number in the bottom N bits of Imm |
| 250 | //Inst.addOperand(MCOperand::createImm(SignExtend64<N>(Imm))); |
| 251 | MCOperand_CreateImm0(Inst, SignExtend64(Imm, N)); |
| 252 | return MCDisassembler_Success; |
| 253 | } |
| 254 | |
| 255 | static DecodeStatus decodeSImmNonZeroOperand(MCInst *Inst, uint64_t Imm, |
| 256 | int64_t Address, |
| 257 | const void *Decoder, |
| 258 | unsigned N) |
| 259 | { |
| 260 | if (Imm == 0) |
| 261 | return MCDisassembler_Fail; |
| 262 | return decodeSImmOperand(Inst, Imm, Address, Decoder, N); |
| 263 | } |
| 264 | |
| 265 | static DecodeStatus decodeSImmOperandAndLsl1(MCInst *Inst, uint64_t Imm, |
| 266 | int64_t Address, |
| 267 | const void *Decoder, |
| 268 | unsigned N) |
| 269 | { |
| 270 | //CS_ASSERT(isUInt<N>(Imm) && "Invalid immediate"); |
| 271 | // Sign-extend the number in the bottom N bits of Imm after accounting for |
| 272 | // the fact that the N bit immediate is stored in N-1 bits (the LSB is |
| 273 | // always zero) |
| 274 | //Inst.addOperand(MCOperand::createImm(SignExtend64<N>(Imm << 1))); |
| 275 | MCOperand_CreateImm0(Inst, SignExtend64(Imm << 1, N)); |
| 276 | return MCDisassembler_Success; |
| 277 | } |
| 278 | |
| 279 | static DecodeStatus decodeCLUIImmOperand(MCInst *Inst, uint64_t Imm, |
| 280 | int64_t Address, |
| 281 | const void *Decoder) |
| 282 | { |
| 283 | //CS_ASSERT(isUInt<6>(Imm) && "Invalid immediate"); |
| 284 | if (Imm > 31) { |
| 285 | Imm = (SignExtend64(Imm, 6) & 0xfffff); |
| 286 | } |
| 287 | //Inst.addOperand(MCOperand::createImm(Imm)); |
| 288 | MCOperand_CreateImm0(Inst, Imm); |
| 289 | return MCDisassembler_Success; |
| 290 | } |
| 291 | |
| 292 | static DecodeStatus decodeFRMArg(MCInst *Inst, uint64_t Imm, |
| 293 | int64_t Address, |
| 294 | const void *Decoder) |
| 295 | { |
| 296 | //CS_ASSERT(isUInt<3>(Imm) && "Invalid immediate"); |
| 297 | if (!RISCVFPRndMode_isValidRoundingMode(Imm)) |
| 298 | return MCDisassembler_Fail; |
| 299 | |
| 300 | //Inst.addOperand(MCOperand::createImm(Imm)); |
| 301 | MCOperand_CreateImm0(Inst, Imm); |
| 302 | return MCDisassembler_Success; |
| 303 | } |
| 304 | |
| 305 | |
| 306 | #include "RISCVGenDisassemblerTables.inc" |
| 307 | |
| 308 | static void init_MI_insn_detail(MCInst *MI) |
| 309 | { |
| 310 | if (MI->flat_insn->detail) { |
| 311 | memset(MI->flat_insn->detail, 0, sizeof(cs_detail)); |
| 312 | } |
| 313 | |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | // mark the load/store instructions through the opcode. |
| 318 | static void markLSInsn(MCInst *MI, uint32_t in) |
| 319 | { |
| 320 | /* |
| 321 | I ld 0000011 = 0x03 |
| 322 | st 0100011 = 0x23 |
| 323 | F/D ld 0000111 = 0x07 |
| 324 | st 0100111 = 0x27 |
| 325 | */ |
| 326 | #define MASK_LS_INSN 0x0000007f |
| 327 | uint32_t opcode = in & MASK_LS_INSN; |
| 328 | if (0 == (opcode ^ 0x03) || 0 == (opcode ^ 0x07) || |
| 329 | 0 == (opcode ^ 0x23) || 0 == (opcode ^ 0x27)) |
| 330 | MI->flat_insn->detail->riscv.need_effective_addr = true; |
| 331 | #undef MASK_LS_INSN |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | static DecodeStatus RISCVDisassembler_getInstruction(int mode, MCInst *MI, |
| 336 | const uint8_t *code, size_t code_len, |
| 337 | uint16_t *Size, uint64_t Address, |
| 338 | MCRegisterInfo *MRI) |
| 339 | { |
| 340 | // TODO: This will need modification when supporting instruction set |
| 341 | // extensions with instructions > 32-bits (up to 176 bits wide). |
| 342 | uint32_t Inst = 0; |
| 343 | DecodeStatus Result; |
| 344 | |
| 345 | // It's a 32 bit instruction if bit 0 and 1 are 1. |
| 346 | if ((code[0] & 0x3) == 0x3) { |
| 347 | if (code_len < 4) { |
| 348 | *Size = 0; |
| 349 | return MCDisassembler_Fail; |
| 350 | } |
| 351 | |
| 352 | *Size = 4; |
| 353 | // Get the four bytes of the instruction. |
| 354 | //Encoded as little endian 32 bits. |
| 355 | Inst = code[0] | (code[1] << 8) | (code[2] << 16) | ((uint32_t)code[3] << 24); |
| 356 | init_MI_insn_detail(MI); |
| 357 | // Now we need mark what instruction need fix effective address output. |
| 358 | if (MI->csh->detail) |
| 359 | markLSInsn(MI, Inst); |
| 360 | Result = decodeInstruction(DecoderTable32, MI, Inst, Address, MRI, mode); |
| 361 | } else { |
| 362 | if (code_len < 2) { |
| 363 | *Size = 0; |
| 364 | return MCDisassembler_Fail; |
| 365 | } |
| 366 | |
| 367 | // If not b4bit. |
| 368 | if (! (getFeatureBits(mode) & ((uint64_t)RISCV_Feature64Bit))) { |
| 369 | // Trying RISCV32Only_16 table (16-bit Instruction) |
| 370 | Inst = code[0] | (code[1] << 8); |
| 371 | init_MI_insn_detail(MI); |
| 372 | Result = decodeInstruction(DecoderTableRISCV32Only_16, MI, Inst, Address, |
| 373 | MRI, mode); |
| 374 | if (Result != MCDisassembler_Fail) { |
| 375 | *Size = 2; |
| 376 | return Result; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | // Trying RISCV_C table (16-bit Instruction) |
| 381 | Inst = code[0] | (code[1] << 8); |
| 382 | init_MI_insn_detail(MI); |
| 383 | // Calling the auto-generated decoder function. |
| 384 | Result = decodeInstruction(DecoderTable16, MI, Inst, Address, MRI, mode); |
| 385 | *Size = 2; |
| 386 | } |
| 387 | |
| 388 | return Result; |
| 389 | } |
| 390 | |
| 391 | bool RISCV_getInstruction(csh ud, const uint8_t *code, size_t code_len, |
| 392 | MCInst *instr, uint16_t *size, uint64_t address, |
| 393 | void *info) |
| 394 | { |
| 395 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 396 | |
| 397 | return MCDisassembler_Success == |
| 398 | RISCVDisassembler_getInstruction(handle->mode, instr, |
| 399 | code, code_len, |
| 400 | size, address, |
| 401 | (MCRegisterInfo *)info); |
| 402 | |
| 403 | } |
| 404 | |
| 405 | void RISCV_init(MCRegisterInfo * MRI) |
| 406 | { |
| 407 | /* |
| 408 | InitMCRegisterInfo(RISCVRegDesc, 97, RA, PC, |
| 409 | RISCVMCRegisterClasses, 11, |
| 410 | RISCVRegUnitRoots, |
| 411 | 64, |
| 412 | RISCVRegDiffLists, |
| 413 | RISCVLaneMaskLists, |
| 414 | RISCVRegStrings, |
| 415 | RISCVRegClassStrings, |
| 416 | RISCVSubRegIdxLists, |
| 417 | 2, |
| 418 | RISCVSubRegIdxRanges, |
| 419 | RISCVRegEncodingTable); |
| 420 | */ |
| 421 | |
| 422 | MCRegisterInfo_InitMCRegisterInfo(MRI, RISCVRegDesc, 97, 0, 0, |
| 423 | RISCVMCRegisterClasses, 11, |
| 424 | 0, |
| 425 | 0, |
| 426 | RISCVRegDiffLists, |
| 427 | 0, |
| 428 | RISCVSubRegIdxLists, |
| 429 | 2, |
| 430 | 0); |
| 431 | } |
| 432 | |
| 433 | #endif |