Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i#3544 RV64: Added an encoder and some fixes and improvments to the decoder #6095

Merged
merged 36 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
b0284f1
RV64: Added skeleton code for encoder
ksco May 29, 2023
8e3e355
RV64: Fixed disassemble
ksco May 29, 2023
160a962
RV64: Optimize disassembled immediates format
ksco May 29, 2023
126170c
RV64: Added an encoder
ksco May 30, 2023
d9d162a
Clang format
ksco May 30, 2023
4e2d8f6
RV64: Fixed sizeof(opnd_t)
ksco May 31, 2023
754110a
RV64: Supported instr target for branch instr
ksco May 31, 2023
be310dd
i#3544 RV64: Fixed codec issue
ksco Jun 1, 2023
2f2d615
Added DR_OPND_IMM_PRINT_DECIMAL
ksco Jun 7, 2023
97b0d15
Clang format
ksco Jun 7, 2023
2fe186b
Clang format
ksco Jun 7, 2023
6ecff17
Merge branch 'master' into encoder
ksco Jun 14, 2023
da65eff
Start working on tests
ksco Jun 14, 2023
8de5d59
Added a simple case
ksco Jun 15, 2023
fc35f0e
Always use init_array for RISC-V
ksco Jun 15, 2023
330c996
A simple test works
ksco Jun 16, 2023
6d8b730
Dynamic linking test works
ksco Jun 16, 2023
57e48a0
Added integer load/store tests
ksco Jun 16, 2023
eab93f1
Added float load/store tests
ksco Jun 17, 2023
4d15ec1
Added atomic tests
ksco Jun 17, 2023
e143945
Added fcvt tests
ksco Jun 17, 2023
5a9d242
Added fmv tests
ksco Jun 17, 2023
ab114b2
Added float arith tests
ksco Jun 17, 2023
f8f264d
Added more tests
ksco Jun 17, 2023
7c922ce
Fixed branch tests
ksco Jun 17, 2023
4b854fa
Open RISCV64 tests
ksco Jun 17, 2023
4d6ff79
Setup QEMU_LD_PREFIX
ksco Jun 17, 2023
0939932
Added DR_DISASM_RISCV for syntax control
ksco Jun 21, 2023
4dc5fb5
Minor fixes
ksco Jun 21, 2023
f6aacbe
Use opnd_add_flags instead
ksco Jun 21, 2023
b127d66
Added comments
ksco Jun 21, 2023
9b7072f
Minor fixes
ksco Jun 21, 2023
746285c
Removed trailling spaces
ksco Jun 21, 2023
2d92a74
Fixed a typo
ksco Jun 21, 2023
38927e8
Fixed a typo
ksco Jun 22, 2023
7c30881
Merge branch 'master' into encoder
ksco Jun 22, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci-riscv64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ jobs:
DYNAMORIO_CROSS_RISCV64_LINUX_ONLY: yes
CI_TRIGGER: ${{ github.event_name }}
CI_BRANCH: ${{ github.ref }}
QEMU_LD_PREFIX: /usr/riscv64-linux-gnu/

- name: Send failure mail to dynamorio-devs
if: failure() && github.ref == 'refs/heads/master'
Expand Down
5 changes: 3 additions & 2 deletions api/docs/bt.dox
Original file line number Diff line number Diff line change
Expand Up @@ -731,8 +731,9 @@ disassemble_from_copy(), and disassemble_to_buffer().

The style of disassembly can be controlled through the
\ref op_syntax_intel "-syntax_intel" (for Intel-style disassembly),
\ref op_syntax_att "-syntax_att" (for AT&T-style disassembly), and
\ref op_syntax_arm "-syntax_arm" (for ARM-style disassembly) runtime
\ref op_syntax_att "-syntax_att" (for AT&T-style disassembly),
\ref op_syntax_arm "-syntax_arm" (for ARM-style disassembly), and
\ref op_syntax_riscv "-syntax_riscv" (for RISC-V-style disassembly) runtime
options, or the disassemble_set_syntax() function. The default disassembly
style is DynamoRIO's custom style, which lists all operands (both implicit
and explicit). The sources are listed first, followed by "->", and then
Expand Down
5 changes: 5 additions & 0 deletions api/docs/deployment.dox
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,11 @@ they want to shrink memory usage
ARM assembler syntax rather than the default show-implicit-operands
syntax. This can also be set using disassemble_set_syntax().

- \b -syntax_riscv: \anchor op_syntax_riscv
This option causes DynamoRIO to output all disassembly using
RISC-V assembler syntax rather than the default show-implicit-operands
syntax. This can also be set using disassemble_set_syntax().

- \b -disasm_mask:
This option sets the disassembly style to the specified bitmask of
dr_disasm_flags_t values. This option overlaps with -syntax_intel,
Expand Down
9 changes: 5 additions & 4 deletions clients/drcachesim/common/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,15 +540,16 @@ droption_t<bytesize_t>
"and the references following the simulated ones are dropped.");

droption_t<std::string>
op_view_syntax(DROPTION_SCOPE_FRONTEND, "view_syntax", "att/arm/dr",
op_view_syntax(DROPTION_SCOPE_FRONTEND, "view_syntax", "att/arm/dr/riscv",
"Syntax to use for disassembly.",
"Specifies the syntax to use when viewing disassembled offline traces."
// TODO i#4382: Add aarch64 syntax support.
" The option can be set to one of \"att\" (AT&T style), \"intel\""
" (Intel style), \"dr\" (DynamoRIO's native style with all implicit"
" operands listed), and \"arm\" (32-bit ARM style). An invalid"
" specification falls back to the default, which is \"att\" for x86,"
" \"arm\" for ARM (32-bit), and \"dr\" for AArch64.");
" operands listed), \"arm\" (32-bit ARM style), and \"riscv\" (RISC-V "
"style). An invalid specification falls back to the default, which is "
"\"att\" for x86, \"arm\" for ARM (32-bit), \"dr\" for AArch64, "
"and \"riscv\" for RISC-V.");

droption_t<std::string>
op_config_file(DROPTION_SCOPE_FRONTEND, "config_file", "",
Expand Down
4 changes: 3 additions & 1 deletion clients/drcachesim/tests/view_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ class view_test_t : public view_t {
{
module_mapper_->get_loaded_modules();
dr_disasm_flags_t flags =
IF_X86_ELSE(DR_DISASM_ATT, IF_AARCH64_ELSE(DR_DISASM_DR, DR_DISASM_ARM));
IF_X86_ELSE(DR_DISASM_ATT,
IF_AARCH64_ELSE(DR_DISASM_DR,
IF_RISCV64_ELSE(DR_DISASM_RISCV, DR_DISASM_ARM)));
disassemble_set_syntax(flags);
return "";
}
Expand Down
7 changes: 5 additions & 2 deletions clients/drcachesim/tools/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,17 @@ view_t::initialize_stream(memtrace_stream_t *serial_stream)
std::string error = module_mapper_->get_last_error();
if (!error.empty())
return "Failed to load binaries: " + error;
dr_disasm_flags_t flags =
IF_X86_ELSE(DR_DISASM_ATT, IF_AARCH64_ELSE(DR_DISASM_DR, DR_DISASM_ARM));
dr_disasm_flags_t flags = IF_X86_ELSE(
DR_DISASM_ATT,
IF_AARCH64_ELSE(DR_DISASM_DR, IF_RISCV64_ELSE(DR_DISASM_RISCV, DR_DISASM_ARM)));
if (knob_syntax_ == "intel") {
flags = DR_DISASM_INTEL;
} else if (knob_syntax_ == "dr") {
flags = DR_DISASM_DR;
} else if (knob_syntax_ == "arm") {
flags = DR_DISASM_ARM;
} else if (knob_syntax_ == "riscv") {
flags = DR_DISASM_RISCV;
}
disassemble_set_syntax(flags);
return "";
Expand Down
6 changes: 5 additions & 1 deletion clients/drdisas/drdisas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ droption_t<bool> op_show_bytes(DROPTION_SCOPE_FRONTEND, "show_bytes", true,
"Display the instruction encoding bytes.",
"Display the instruction encoding bytes.");

#if defined(AARCH64) || defined(ARM)
#if defined(AARCH64) || defined(ARM) || defined(RISCV64)
# define MAX_INSTR_LENGTH 4
#else
# define MAX_INSTR_LENGTH 17
Expand Down Expand Up @@ -165,6 +165,10 @@ main(int argc, const char *argv[])
}
#endif

#ifdef RISCV64
disassemble_set_syntax(DR_DISASM_RISCV);
#endif

// Turn the arguments into a series of hex values.
std::vector<byte> bytes;
for (int i = last_index; i < argc; ++i) {
Expand Down
3 changes: 2 additions & 1 deletion core/drlibc/drlibc_module_elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ is_elf_so_header_common(app_pc base, size_t size, bool memory)
ASSERT_CURIOSITY(!memory ||
#ifdef X64
elf_header.e_machine == EM_X86_64 ||
elf_header.e_machine == EM_AARCH64
elf_header.e_machine == EM_AARCH64 ||
elf_header.e_machine == EM_RISCV
#else
elf_header.e_machine == EM_386 || elf_header.e_machine == EM_ARM
#endif
Expand Down
10 changes: 8 additions & 2 deletions core/ir/disassemble_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,21 @@ typedef enum {
* \p -syntax_arm. Implicit operands are not displayed.
*/
DR_DISASM_ARM = 0x10,
/**
* Requests RISC-V assembler syntax for disassembly. This set the same option that
* is controlled by the runtime option \p -syntax_riscv. Implicit oprands are not
* displayed.
*/
DR_DISASM_RISCV = 0x20,
} dr_disasm_flags_t;
/* TODO i#4382: Add DR_DISASM_AARCH64. */

DR_API
/**
* Sets the disassembly style and decoding options.
* The default is to use DR's custom syntax, unless one of the \ref op_syntax_intel
* "-syntax_intel", \ref op_syntax_att "-syntax_att", or \ref op_syntax_arm
* "-syntax_arm" runtime options is specified.
* "-syntax_intel", \ref op_syntax_att "-syntax_att", \ref op_syntax_arm
* "-syntax_arm", or \ref op_syntax_riscv "-syntax_riscv" runtime options is specified.
*/
void
disassemble_set_syntax(dr_disasm_flags_t flags);
Expand Down
14 changes: 11 additions & 3 deletions core/ir/disassemble_shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ disassemble_options_init(void)
if (DYNAMO_OPTION(syntax_arm)) {
flags |= DR_DISASM_ARM;
}
if (DYNAMO_OPTION(syntax_riscv)) {
flags |= DR_DISASM_RISCV;
}
/* This option is separate as it's not strictly a disasm style */
dynamo_options.decode_strict = TEST(DR_DISASM_STRICT_INVALID, flags);
if (DYNAMO_OPTION(decode_strict))
Expand All @@ -154,7 +157,8 @@ disassemble_set_syntax(dr_disasm_flags_t flags)
static inline bool
dsts_first(void)
{
return TESTANY(DR_DISASM_INTEL | DR_DISASM_ARM, DYNAMO_OPTION(disasm_mask));
return TESTANY(DR_DISASM_INTEL | DR_DISASM_ARM | DR_DISASM_RISCV,
DYNAMO_OPTION(disasm_mask));
}

static inline bool
Expand All @@ -170,7 +174,7 @@ internal_instr_disassemble(char *buf, size_t bufsz, size_t *sofar INOUT,
static inline const char *
immed_prefix(void)
{
return (TEST(DR_DISASM_INTEL, DYNAMO_OPTION(disasm_mask))
return (TEST(DR_DISASM_INTEL | DR_DISASM_RISCV, DYNAMO_OPTION(disasm_mask))
? ""
: (TEST(DR_DISASM_ARM, DYNAMO_OPTION(disasm_mask)) ? "#" : "$"));
}
Expand All @@ -180,7 +184,8 @@ reg_disassemble(char *buf, size_t bufsz, size_t *sofar INOUT, reg_id_t reg,
dr_opnd_flags_t flags, const char *prefix, const char *suffix)
{
print_to_buffer(buf, bufsz, sofar,
TESTANY(DR_DISASM_INTEL | DR_DISASM_ARM, DYNAMO_OPTION(disasm_mask))
TESTANY(DR_DISASM_INTEL | DR_DISASM_ARM | DR_DISASM_RISCV,
DYNAMO_OPTION(disasm_mask))
? "%s%s%s%s"
: "%s%s%%%s%s",
prefix, TEST(DR_OPND_NEGATED, flags) ? "-" : "", reg_names[reg],
Expand Down Expand Up @@ -401,6 +406,9 @@ opnd_base_disp_disassemble(char *buf, size_t bufsz, size_t *sofar INOUT, opnd_t
}
if (TEST(DR_DISASM_ARM, DYNAMO_OPTION(disasm_mask)))
print_to_buffer(buf, bufsz, sofar, "%d", disp);
else if (TEST(DR_DISASM_RISCV, DYNAMO_OPTION(disasm_mask)) &&
TEST(opnd_get_flags(opnd), DR_OPND_IMM_PRINT_DECIMAL))
print_to_buffer(buf, bufsz, sofar, "%d", disp);
else if ((unsigned)disp <= 0xff && !opnd_is_disp_force_full(opnd))
print_to_buffer(buf, bufsz, sofar, "0x%02x", disp);
else if ((unsigned)disp <= 0xffff IF_X86(&&opnd_is_disp_short_addr(opnd)))
Expand Down
2 changes: 1 addition & 1 deletion core/ir/encode_shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ instr_encode_arch(dcontext_t *dcontext, instr_t *instr, byte *copy_pc, byte *fin
bool *has_instr_opnds /*OUT OPTIONAL*/
_IF_DEBUG(bool assert_reachable));

#ifdef AARCH64
#if defined(AARCH64) || defined(RISCV64)
/* exported
*/
bool
Expand Down
4 changes: 2 additions & 2 deletions core/ir/instr_inline_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,12 @@ opnd_create_pc(app_pc pc)
.value.reg_and_element_size.reg)
# define opnd_get_reg OPND_GET_REG

# if defined(X86) || defined(RISCV64)
# if defined(X86)
# define OPND_GET_FLAGS(opnd) \
(CLIENT_ASSERT_( \
opnd_is_reg(opnd) || opnd_is_base_disp(opnd) || opnd_is_immed_int(opnd), \
"opnd_get_flags called on non-reg non-base-disp non-immed-int opnd") 0)
# elif defined(AARCHXX)
# elif defined(AARCHXX) || defined(RISCV64)
# define OPND_GET_FLAGS(opnd) \
(CLIENT_ASSERT_( \
opnd_is_reg(opnd) || opnd_is_base_disp(opnd) || \
Expand Down
14 changes: 10 additions & 4 deletions core/ir/opnd_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,6 @@ enum {
DR_REG_X29, /**< The x29(t4) register. */
DR_REG_X30, /**< The x30(t5) register. */
DR_REG_X31, /**< The x31(t6) register. */
DR_REG_PC, /**< The program counter. */
/* GPR aliases */
DR_REG_ZERO = DR_REG_X0, /**< The hard-wired zero (x0) register. */
DR_REG_RA = DR_REG_X1, /**< The return address (x1) register. */
Expand Down Expand Up @@ -1209,6 +1208,7 @@ enum {
DR_REG_T4 = DR_REG_X29, /**< The 5th temporary (x29) register. */
DR_REG_T5 = DR_REG_X30, /**< The 6th temporary (x30) register. */
DR_REG_T6 = DR_REG_X31, /**< The 7th temporary (x31) register. */
DR_REG_PC, /**< The program counter. */
/* Floating point registers */
DR_REG_F0, /**< The f0(ft0) floating-point register. */
DR_REG_F1, /**< The f1(ft1) floating-point register. */
Expand Down Expand Up @@ -1285,9 +1285,9 @@ enum {
DR_REG_LAST_ENUM = DR_REG_FCSR, /**< Last value of register enums. */

DR_REG_START_64 = DR_REG_X0, /**< Start of 64-bit general register enum values. */
ksco marked this conversation as resolved.
Show resolved Hide resolved
DR_REG_STOP_64 = DR_REG_X31, /**< End of 64-bit general register enum values. */
DR_REG_STOP_64 = DR_REG_F31, /**< End of 64-bit general register enum values. */
DR_REG_START_32 = DR_REG_X0, /**< Start of 32-bit general register enum values. */
DR_REG_STOP_32 = DR_REG_X31, /**< End of 32-bit general register enum values. */
DR_REG_STOP_32 = DR_REG_F31, /**< End of 32-bit general register enum values. */
DR_REG_START_GPR = DR_REG_X0, /**< Start of general register registers. */
DR_REG_STOP_GPR = DR_REG_X31, /**< End of general register registers. */
DR_REG_XSP = DR_REG_SP, /**< Platform-independent way to refer to stack pointer. */
Expand Down Expand Up @@ -1780,6 +1780,11 @@ typedef enum _dr_opnd_flags_t {
* SVE predicate constraint
*/
DR_OPND_IS_PREDICATE_CONSTRAINT = 0x800,

/**
* This is used by RISCV64 for immediates display format.
*/
DR_OPND_IMM_PRINT_DECIMAL = 0x1000,
} dr_opnd_flags_t;

#ifdef DR_FAST_IR
Expand Down Expand Up @@ -2171,7 +2176,8 @@ DR_API
* needs to be specified for an absolute address; otherwise, simply
* use the desired short registers for base and/or index).
*
* (The encoding optimization flags are all false when using opnd_create_base_disp()).
* (The encoding optimization flags are all false when using
* opnd_create_base_disp()).
*/
opnd_t
opnd_create_base_disp_ex(reg_id_t base_reg, reg_id_t index_reg, int scale, int disp,
Expand Down
Loading