1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
|
//===---EmulateInstructionLoongArch.cpp------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <cstdlib>
#include <optional>
#include "EmulateInstructionLoongArch.h"
#include "Plugins/Process/Utility/InstructionUtils.h"
#include "Plugins/Process/Utility/RegisterInfoPOSIX_loongarch64.h"
#include "Plugins/Process/Utility/lldb-loongarch-register-enums.h"
#include "lldb/Core/Address.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Interpreter/OptionValueArray.h"
#include "lldb/Interpreter/OptionValueDictionary.h"
#include "lldb/Symbol/UnwindPlan.h"
#include "lldb/Utility/ArchSpec.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/RegisterValue.h"
#include "lldb/Utility/Stream.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/MathExtras.h"
using namespace lldb;
using namespace lldb_private;
LLDB_PLUGIN_DEFINE_ADV(EmulateInstructionLoongArch, InstructionLoongArch)
namespace lldb_private {
EmulateInstructionLoongArch::Opcode *
EmulateInstructionLoongArch::GetOpcodeForInstruction(uint32_t inst) {
// TODO: Add the mask for other instruction.
static EmulateInstructionLoongArch::Opcode g_opcodes[] = {
{0xfc000000, 0x40000000, &EmulateInstructionLoongArch::EmulateBEQZ,
"beqz rj, offs21"},
{0xfc000000, 0x44000000, &EmulateInstructionLoongArch::EmulateBNEZ,
"bnez rj, offs21"},
{0xfc000300, 0x48000000, &EmulateInstructionLoongArch::EmulateBCEQZ,
"bceqz cj, offs21"},
{0xfc000300, 0x48000100, &EmulateInstructionLoongArch::EmulateBCNEZ,
"bcnez cj, offs21"},
{0xfc000000, 0x4c000000, &EmulateInstructionLoongArch::EmulateJIRL,
"jirl rd, rj, offs16"},
{0xfc000000, 0x50000000, &EmulateInstructionLoongArch::EmulateB,
" b offs26"},
{0xfc000000, 0x54000000, &EmulateInstructionLoongArch::EmulateBL,
"bl offs26"},
{0xfc000000, 0x58000000, &EmulateInstructionLoongArch::EmulateBEQ,
"beq rj, rd, offs16"},
{0xfc000000, 0x5c000000, &EmulateInstructionLoongArch::EmulateBNE,
"bne rj, rd, offs16"},
{0xfc000000, 0x60000000, &EmulateInstructionLoongArch::EmulateBLT,
"blt rj, rd, offs16"},
{0xfc000000, 0x64000000, &EmulateInstructionLoongArch::EmulateBGE,
"bge rj, rd, offs16"},
{0xfc000000, 0x68000000, &EmulateInstructionLoongArch::EmulateBLTU,
"bltu rj, rd, offs16"},
{0xfc000000, 0x6c000000, &EmulateInstructionLoongArch::EmulateBGEU,
"bgeu rj, rd, offs16"},
{0x00000000, 0x00000000, &EmulateInstructionLoongArch::EmulateNonJMP,
"NonJMP"}};
static const size_t num_loongarch_opcodes = std::size(g_opcodes);
for (size_t i = 0; i < num_loongarch_opcodes; ++i)
if ((g_opcodes[i].mask & inst) == g_opcodes[i].value)
return &g_opcodes[i];
return nullptr;
}
bool EmulateInstructionLoongArch::TestExecute(uint32_t inst) {
Opcode *opcode_data = GetOpcodeForInstruction(inst);
if (!opcode_data)
return false;
// Call the Emulate... function.
if (!(this->*opcode_data->callback)(inst))
return false;
return true;
}
bool EmulateInstructionLoongArch::EvaluateInstruction(uint32_t options) {
uint32_t inst_size = m_opcode.GetByteSize();
uint32_t inst = m_opcode.GetOpcode32();
bool increase_pc = options & eEmulateInstructionOptionAutoAdvancePC;
bool success = false;
Opcode *opcode_data = GetOpcodeForInstruction(inst);
if (!opcode_data)
return false;
lldb::addr_t old_pc = 0;
if (increase_pc) {
old_pc = ReadPC(&success);
if (!success)
return false;
}
// Call the Emulate... function.
if (!(this->*opcode_data->callback)(inst))
return false;
if (increase_pc) {
lldb::addr_t new_pc = ReadPC(&success);
if (!success)
return false;
if (new_pc == old_pc && !WritePC(old_pc + inst_size))
return false;
}
return true;
}
bool EmulateInstructionLoongArch::ReadInstruction() {
bool success = false;
m_addr = ReadPC(&success);
if (!success) {
m_addr = LLDB_INVALID_ADDRESS;
return false;
}
Context ctx;
ctx.type = eContextReadOpcode;
ctx.SetNoArgs();
uint32_t inst = (uint32_t)ReadMemoryUnsigned(ctx, m_addr, 4, 0, &success);
m_opcode.SetOpcode32(inst, GetByteOrder());
return true;
}
lldb::addr_t EmulateInstructionLoongArch::ReadPC(bool *success) {
return ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC,
LLDB_INVALID_ADDRESS, success);
}
bool EmulateInstructionLoongArch::WritePC(lldb::addr_t pc) {
EmulateInstruction::Context ctx;
ctx.type = eContextAdvancePC;
ctx.SetNoArgs();
return WriteRegisterUnsigned(ctx, eRegisterKindGeneric,
LLDB_REGNUM_GENERIC_PC, pc);
}
std::optional<RegisterInfo>
EmulateInstructionLoongArch::GetRegisterInfo(lldb::RegisterKind reg_kind,
uint32_t reg_index) {
if (reg_kind == eRegisterKindGeneric) {
switch (reg_index) {
case LLDB_REGNUM_GENERIC_PC:
reg_kind = eRegisterKindLLDB;
reg_index = gpr_pc_loongarch;
break;
case LLDB_REGNUM_GENERIC_SP:
reg_kind = eRegisterKindLLDB;
reg_index = gpr_sp_loongarch;
break;
case LLDB_REGNUM_GENERIC_FP:
reg_kind = eRegisterKindLLDB;
reg_index = gpr_fp_loongarch;
break;
case LLDB_REGNUM_GENERIC_RA:
reg_kind = eRegisterKindLLDB;
reg_index = gpr_ra_loongarch;
break;
// We may handle LLDB_REGNUM_GENERIC_ARGx when more instructions are
// supported.
default:
llvm_unreachable("unsupported register");
}
}
const RegisterInfo *array =
RegisterInfoPOSIX_loongarch64::GetRegisterInfoPtr(m_arch);
const uint32_t length =
RegisterInfoPOSIX_loongarch64::GetRegisterInfoCount(m_arch);
if (reg_index >= length || reg_kind != eRegisterKindLLDB)
return {};
return array[reg_index];
}
bool EmulateInstructionLoongArch::SetTargetTriple(const ArchSpec &arch) {
return SupportsThisArch(arch);
}
bool EmulateInstructionLoongArch::TestEmulation(
Stream &out_stream, ArchSpec &arch, OptionValueDictionary *test_data) {
return false;
}
void EmulateInstructionLoongArch::Initialize() {
PluginManager::RegisterPlugin(GetPluginNameStatic(),
GetPluginDescriptionStatic(), CreateInstance);
}
void EmulateInstructionLoongArch::Terminate() {
PluginManager::UnregisterPlugin(CreateInstance);
}
lldb_private::EmulateInstruction *
EmulateInstructionLoongArch::CreateInstance(const ArchSpec &arch,
InstructionType inst_type) {
if (EmulateInstructionLoongArch::SupportsThisInstructionType(inst_type) &&
SupportsThisArch(arch))
return new EmulateInstructionLoongArch(arch);
return nullptr;
}
bool EmulateInstructionLoongArch::SupportsThisArch(const ArchSpec &arch) {
return arch.GetTriple().isLoongArch();
}
bool EmulateInstructionLoongArch::EmulateBEQZ(uint32_t inst) {
return IsLoongArch64() ? EmulateBEQZ64(inst) : false;
}
bool EmulateInstructionLoongArch::EmulateBNEZ(uint32_t inst) {
return IsLoongArch64() ? EmulateBNEZ64(inst) : false;
}
bool EmulateInstructionLoongArch::EmulateBCEQZ(uint32_t inst) {
return IsLoongArch64() ? EmulateBCEQZ64(inst) : false;
}
bool EmulateInstructionLoongArch::EmulateBCNEZ(uint32_t inst) {
return IsLoongArch64() ? EmulateBCNEZ64(inst) : false;
}
bool EmulateInstructionLoongArch::EmulateJIRL(uint32_t inst) {
return IsLoongArch64() ? EmulateJIRL64(inst) : false;
}
bool EmulateInstructionLoongArch::EmulateB(uint32_t inst) {
return IsLoongArch64() ? EmulateB64(inst) : false;
}
bool EmulateInstructionLoongArch::EmulateBL(uint32_t inst) {
return IsLoongArch64() ? EmulateBL64(inst) : false;
}
bool EmulateInstructionLoongArch::EmulateBEQ(uint32_t inst) {
return IsLoongArch64() ? EmulateBEQ64(inst) : false;
}
bool EmulateInstructionLoongArch::EmulateBNE(uint32_t inst) {
return IsLoongArch64() ? EmulateBNE64(inst) : false;
}
bool EmulateInstructionLoongArch::EmulateBLT(uint32_t inst) {
return IsLoongArch64() ? EmulateBLT64(inst) : false;
}
bool EmulateInstructionLoongArch::EmulateBGE(uint32_t inst) {
return IsLoongArch64() ? EmulateBGE64(inst) : false;
}
bool EmulateInstructionLoongArch::EmulateBLTU(uint32_t inst) {
return IsLoongArch64() ? EmulateBLTU64(inst) : false;
}
bool EmulateInstructionLoongArch::EmulateBGEU(uint32_t inst) {
return IsLoongArch64() ? EmulateBGEU64(inst) : false;
}
bool EmulateInstructionLoongArch::EmulateNonJMP(uint32_t inst) { return false; }
// beqz rj, offs21
// if GR[rj] == 0:
// PC = PC + SignExtend({offs21, 2'b0}, GRLEN)
bool EmulateInstructionLoongArch::EmulateBEQZ64(uint32_t inst) {
bool success = false;
uint32_t rj = Bits32(inst, 9, 5);
uint64_t pc = ReadPC(&success);
if (!success)
return false;
uint32_t offs21 = Bits32(inst, 25, 10) + (Bits32(inst, 4, 0) << 16);
uint64_t rj_val = ReadRegisterUnsigned(eRegisterKindLLDB, rj, 0, &success);
if (!success)
return false;
if (rj_val == 0) {
uint64_t next_pc = pc + llvm::SignExtend64<23>(offs21 << 2);
return WritePC(next_pc);
} else
return WritePC(pc + 4);
}
// bnez rj, offs21
// if GR[rj] != 0:
// PC = PC + SignExtend({offs21, 2'b0}, GRLEN)
bool EmulateInstructionLoongArch::EmulateBNEZ64(uint32_t inst) {
bool success = false;
uint32_t rj = Bits32(inst, 9, 5);
uint64_t pc = ReadPC(&success);
if (!success)
return false;
uint32_t offs21 = Bits32(inst, 25, 10) + (Bits32(inst, 4, 0) << 16);
uint64_t rj_val = ReadRegisterUnsigned(eRegisterKindLLDB, rj, 0, &success);
if (!success)
return false;
if (rj_val != 0) {
uint64_t next_pc = pc + llvm::SignExtend64<23>(offs21 << 2);
return WritePC(next_pc);
} else
return WritePC(pc + 4);
}
// bceqz cj, offs21
// if CFR[cj] == 0:
// PC = PC + SignExtend({offs21, 2'b0}, GRLEN)
bool EmulateInstructionLoongArch::EmulateBCEQZ64(uint32_t inst) {
bool success = false;
uint32_t cj = Bits32(inst, 7, 5) + fpr_fcc0_loongarch;
uint64_t pc = ReadPC(&success);
if (!success)
return false;
uint32_t offs21 = Bits32(inst, 25, 10) + (Bits32(inst, 4, 0) << 16);
uint8_t cj_val =
(uint8_t)ReadRegisterUnsigned(eRegisterKindLLDB, cj, 0, &success);
if (!success)
return false;
if (cj_val == 0) {
uint64_t next_pc = pc + llvm::SignExtend64<23>(offs21 << 2);
return WritePC(next_pc);
} else
return WritePC(pc + 4);
return false;
}
// bcnez cj, offs21
// if CFR[cj] != 0:
// PC = PC + SignExtend({offs21, 2'b0}, GRLEN)
bool EmulateInstructionLoongArch::EmulateBCNEZ64(uint32_t inst) {
bool success = false;
uint32_t cj = Bits32(inst, 7, 5) + fpr_fcc0_loongarch;
uint64_t pc = ReadPC(&success);
if (!success)
return false;
uint32_t offs21 = Bits32(inst, 25, 10) + (Bits32(inst, 4, 0) << 16);
uint8_t cj_val =
(uint8_t)ReadRegisterUnsigned(eRegisterKindLLDB, cj, 0, &success);
if (!success)
return false;
if (cj_val != 0) {
uint64_t next_pc = pc + llvm::SignExtend64<23>(offs21 << 2);
return WritePC(next_pc);
} else
return WritePC(pc + 4);
return false;
}
// jirl rd, rj, offs16
// GR[rd] = PC + 4
// PC = GR[rj] + SignExtend({offs16, 2'b0}, GRLEN)
bool EmulateInstructionLoongArch::EmulateJIRL64(uint32_t inst) {
uint32_t rj = Bits32(inst, 9, 5);
uint32_t rd = Bits32(inst, 4, 0);
bool success = false;
uint64_t pc = ReadPC(&success);
if (!success)
return false;
EmulateInstruction::Context ctx;
if (!WriteRegisterUnsigned(ctx, eRegisterKindLLDB, rd, pc + 4))
return false;
uint64_t rj_val = ReadRegisterUnsigned(eRegisterKindLLDB, rj, 0, &success);
if (!success)
return false;
uint64_t next_pc = rj_val + llvm::SignExtend64<18>(Bits32(inst, 25, 10) << 2);
return WritePC(next_pc);
}
// b offs26
// PC = PC + SignExtend({offs26, 2' b0}, GRLEN)
bool EmulateInstructionLoongArch::EmulateB64(uint32_t inst) {
bool success = false;
uint64_t pc = ReadPC(&success);
if (!success)
return false;
uint32_t offs26 = Bits32(inst, 25, 10) + (Bits32(inst, 9, 0) << 16);
uint64_t next_pc = pc + llvm::SignExtend64<28>(offs26 << 2);
return WritePC(next_pc);
}
// bl offs26
// GR[1] = PC + 4
// PC = PC + SignExtend({offs26, 2'b0}, GRLEN)
bool EmulateInstructionLoongArch::EmulateBL64(uint32_t inst) {
bool success = false;
uint64_t pc = ReadPC(&success);
if (!success)
return false;
EmulateInstruction::Context ctx;
if (!WriteRegisterUnsigned(ctx, eRegisterKindLLDB, gpr_r1_loongarch, pc + 4))
return false;
uint32_t offs26 = Bits32(inst, 25, 10) + (Bits32(inst, 9, 0) << 16);
uint64_t next_pc = pc + llvm::SignExtend64<28>(offs26 << 2);
return WritePC(next_pc);
}
// beq rj, rd, offs16
// if GR[rj] == GR[rd]:
// PC = PC + SignExtend({offs16, 2'b0}, GRLEN)
bool EmulateInstructionLoongArch::EmulateBEQ64(uint32_t inst) {
bool success = false;
uint32_t rj = Bits32(inst, 9, 5);
uint32_t rd = Bits32(inst, 4, 0);
uint64_t pc = ReadPC(&success);
if (!success)
return false;
uint64_t rj_val = ReadRegisterUnsigned(eRegisterKindLLDB, rj, 0, &success);
if (!success)
return false;
uint64_t rd_val = ReadRegisterUnsigned(eRegisterKindLLDB, rd, 0, &success);
if (!success)
return false;
if (rj_val == rd_val) {
uint64_t next_pc = pc + llvm::SignExtend64<18>(Bits32(inst, 25, 10) << 2);
return WritePC(next_pc);
} else
return WritePC(pc + 4);
}
// bne rj, rd, offs16
// if GR[rj] != GR[rd]:
// PC = PC + SignExtend({offs16, 2'b0}, GRLEN)
bool EmulateInstructionLoongArch::EmulateBNE64(uint32_t inst) {
bool success = false;
uint32_t rj = Bits32(inst, 9, 5);
uint32_t rd = Bits32(inst, 4, 0);
uint64_t pc = ReadPC(&success);
if (!success)
return false;
uint64_t rj_val = ReadRegisterUnsigned(eRegisterKindLLDB, rj, 0, &success);
if (!success)
return false;
uint64_t rd_val = ReadRegisterUnsigned(eRegisterKindLLDB, rd, 0, &success);
if (!success)
return false;
if (rj_val != rd_val) {
uint64_t next_pc = pc + llvm::SignExtend64<18>(Bits32(inst, 25, 10) << 2);
return WritePC(next_pc);
} else
return WritePC(pc + 4);
}
// blt rj, rd, offs16
// if signed(GR[rj]) < signed(GR[rd]):
// PC = PC + SignExtend({offs16, 2'b0}, GRLEN)
bool EmulateInstructionLoongArch::EmulateBLT64(uint32_t inst) {
bool success = false;
uint32_t rj = Bits32(inst, 9, 5);
uint32_t rd = Bits32(inst, 4, 0);
uint64_t pc = ReadPC(&success);
if (!success)
return false;
int64_t rj_val =
(int64_t)ReadRegisterUnsigned(eRegisterKindLLDB, rj, 0, &success);
if (!success)
return false;
int64_t rd_val =
(int64_t)ReadRegisterUnsigned(eRegisterKindLLDB, rd, 0, &success);
if (!success)
return false;
if (rj_val < rd_val) {
uint64_t next_pc = pc + llvm::SignExtend64<18>(Bits32(inst, 25, 10) << 2);
return WritePC(next_pc);
} else
return WritePC(pc + 4);
}
// bge rj, rd, offs16
// if signed(GR[rj]) >= signed(GR[rd]):
// PC = PC + SignExtend({offs16, 2'b0}, GRLEN)
bool EmulateInstructionLoongArch::EmulateBGE64(uint32_t inst) {
bool success = false;
uint32_t rj = Bits32(inst, 9, 5);
uint32_t rd = Bits32(inst, 4, 0);
uint64_t pc = ReadPC(&success);
if (!success)
return false;
int64_t rj_val =
(int64_t)ReadRegisterUnsigned(eRegisterKindLLDB, rj, 0, &success);
if (!success)
return false;
int64_t rd_val =
(int64_t)ReadRegisterUnsigned(eRegisterKindLLDB, rd, 0, &success);
if (!success)
return false;
if (rj_val >= rd_val) {
uint64_t next_pc = pc + llvm::SignExtend64<18>(Bits32(inst, 25, 10) << 2);
return WritePC(next_pc);
} else
return WritePC(pc + 4);
}
// bltu rj, rd, offs16
// if unsigned(GR[rj]) < unsigned(GR[rd]):
// PC = PC + SignExtend({offs16, 2'b0}, GRLEN)
bool EmulateInstructionLoongArch::EmulateBLTU64(uint32_t inst) {
bool success = false;
uint32_t rj = Bits32(inst, 9, 5);
uint32_t rd = Bits32(inst, 4, 0);
uint64_t pc = ReadPC(&success);
if (!success)
return false;
uint64_t rj_val = ReadRegisterUnsigned(eRegisterKindLLDB, rj, 0, &success);
if (!success)
return false;
uint64_t rd_val = ReadRegisterUnsigned(eRegisterKindLLDB, rd, 0, &success);
if (!success)
return false;
if (rj_val < rd_val) {
uint64_t next_pc = pc + llvm::SignExtend64<18>(Bits32(inst, 25, 10) << 2);
return WritePC(next_pc);
} else
return WritePC(pc + 4);
}
// bgeu rj, rd, offs16
// if unsigned(GR[rj]) >= unsigned(GR[rd]):
// PC = PC + SignExtend({offs16, 2'b0}, GRLEN)
bool EmulateInstructionLoongArch::EmulateBGEU64(uint32_t inst) {
bool success = false;
uint32_t rj = Bits32(inst, 9, 5);
uint32_t rd = Bits32(inst, 4, 0);
uint64_t pc = ReadPC(&success);
if (!success)
return false;
uint64_t rj_val = ReadRegisterUnsigned(eRegisterKindLLDB, rj, 0, &success);
if (!success)
return false;
uint64_t rd_val = ReadRegisterUnsigned(eRegisterKindLLDB, rd, 0, &success);
if (!success)
return false;
if (rj_val >= rd_val) {
uint64_t next_pc = pc + llvm::SignExtend64<18>(Bits32(inst, 25, 10) << 2);
return WritePC(next_pc);
} else
return WritePC(pc + 4);
}
} // namespace lldb_private
|