Skip to content

Commit

Permalink
Add RISCV64 support
Browse files Browse the repository at this point in the history
Code taken from https://github.com/petrpavlu/valgrind-riscv64
Some minor adjustments have been made because that code is based on
upstream libvex.
  • Loading branch information
petrpavlu authored and rhelmot committed May 13, 2023
1 parent e75200b commit 02ae513
Show file tree
Hide file tree
Showing 13 changed files with 9,849 additions and 12 deletions.
36 changes: 36 additions & 0 deletions auxprogs/genoffsets.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "../pub/libvex_guest_mips32.h"
#include "../pub/libvex_guest_mips64.h"
#include "../pub/libvex_guest_tilegx.h"
#include "../pub/libvex_guest_riscv64.h"

#undef guest_LR

Expand Down Expand Up @@ -975,6 +976,41 @@ int main(int argc, char **argv)
GENOFFSET(TILEGX,tilegx,ex_context_0);
GENOFFSET(TILEGX,tilegx,ex_context_1);
GENOFFSET(TILEGX,tilegx,COND);

// riscv64
GENOFFSET(RISCV64,riscv64,x0);
GENOFFSET(RISCV64,riscv64,x1);
GENOFFSET(RISCV64,riscv64,x2);
GENOFFSET(RISCV64,riscv64,x3);
GENOFFSET(RISCV64,riscv64,x4);
GENOFFSET(RISCV64,riscv64,x5);
GENOFFSET(RISCV64,riscv64,x6);
GENOFFSET(RISCV64,riscv64,x7);
GENOFFSET(RISCV64,riscv64,x9);
GENOFFSET(RISCV64,riscv64,x9);
GENOFFSET(RISCV64,riscv64,x10);
GENOFFSET(RISCV64,riscv64,x11);
GENOFFSET(RISCV64,riscv64,x12);
GENOFFSET(RISCV64,riscv64,x13);
GENOFFSET(RISCV64,riscv64,x14);
GENOFFSET(RISCV64,riscv64,x15);
GENOFFSET(RISCV64,riscv64,x16);
GENOFFSET(RISCV64,riscv64,x17);
GENOFFSET(RISCV64,riscv64,x18);
GENOFFSET(RISCV64,riscv64,x19);
GENOFFSET(RISCV64,riscv64,x20);
GENOFFSET(RISCV64,riscv64,x21);
GENOFFSET(RISCV64,riscv64,x22);
GENOFFSET(RISCV64,riscv64,x23);
GENOFFSET(RISCV64,riscv64,x24);
GENOFFSET(RISCV64,riscv64,x25);
GENOFFSET(RISCV64,riscv64,x26);
GENOFFSET(RISCV64,riscv64,x27);
GENOFFSET(RISCV64,riscv64,x28);
GENOFFSET(RISCV64,riscv64,x29);
GENOFFSET(RISCV64,riscv64,x30);
GENOFFSET(RISCV64,riscv64,x31);
GENOFFSET(RISCV64,riscv64,pc);
}

/*--------------------------------------------------------------------*/
Expand Down
7 changes: 7 additions & 0 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ PUB_HEADERS = \
pub/libvex_guest_arm.h \
pub/libvex_guest_ppc32.h \
pub/libvex_guest_ppc64.h \
pub/libvex_guest_riscv64.h \
pub/libvex_guest_s390x.h \
pub/libvex_s390x_common.h \
pub/libvex_guest_mips32.h
Expand All @@ -29,6 +30,7 @@ PRIV_HEADERS = \
priv/host_amd64_defs.h \
priv/host_arm_defs.h \
priv/host_ppc_defs.h \
priv/host_riscv64_defs.h \
priv/host_s390_defs.h \
priv/host_mips_defs.h \
priv/host_generic_maddf.h \
Expand All @@ -44,6 +46,7 @@ PRIV_HEADERS = \
priv/guest_amd64_defs.h \
priv/guest_arm_defs.h \
priv/guest_ppc_defs.h \
priv/guest_riscv64_defs.h \
priv/guest_mips_defs.h \
priv/s390_disasm.h \
priv/s390_defs.h \
Expand All @@ -63,13 +66,15 @@ NORMAL_OBJS = \
priv/host_arm_defs.o \
priv/host_arm64_defs.o \
priv/host_ppc_defs.o \
priv/host_riscv64_defs.o \
priv/host_s390_defs.o \
priv/host_mips_defs.o \
priv/host_x86_isel.o \
priv/host_amd64_isel.o \
priv/host_arm_isel.o \
priv/host_arm64_isel.o \
priv/host_ppc_isel.o \
priv/host_riscv64_isel.o \
priv/host_s390_isel.o \
priv/host_mips_isel.o \
priv/host_generic_maddf.o \
Expand All @@ -86,13 +91,15 @@ NORMAL_OBJS = \
priv/guest_arm_helpers.o \
priv/guest_arm64_helpers.o \
priv/guest_ppc_helpers.o \
priv/guest_riscv64_helpers.o \
priv/guest_s390_helpers.o \
priv/guest_mips_helpers.o \
priv/guest_x86_toIR.o \
priv/guest_amd64_toIR.o \
priv/guest_arm_toIR.o \
priv/guest_arm64_toIR.o \
priv/guest_ppc_toIR.o \
priv/guest_riscv64_toIR.o \
priv/guest_s390_toIR.o \
priv/guest_mips_toIR.o

Expand Down
136 changes: 136 additions & 0 deletions priv/guest_riscv64_defs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@

/*--------------------------------------------------------------------*/
/*--- begin guest_riscv64_defs.h ---*/
/*--------------------------------------------------------------------*/

/*
This file is part of Valgrind, a dynamic binary instrumentation
framework.
Copyright (C) 2020-2023 Petr Pavlu
[email protected]
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
The GNU General Public License is contained in the file COPYING.
Neither the names of the U.S. Department of Energy nor the
University of California nor the names of its contributors may be
used to endorse or promote products derived from this software
without prior written permission.
*/

/* Only to be used within the guest_riscv64_* files. */

#ifndef __VEX_GUEST_RISCV64_DEFS_H
#define __VEX_GUEST_RISCV64_DEFS_H

#include "libvex_basictypes.h"

#include "guest_generic_bb_to_IR.h"

/*------------------------------------------------------------*/
/*--- riscv64 to IR conversion ---*/
/*------------------------------------------------------------*/

/* Convert one riscv64 insn to IR. See the type DisOneInstrFn in
guest_generic_bb_to_IR.h. */
DisResult disInstr_RISCV64(IRSB* irbb,
const UChar* guest_code,
Long delta,
Addr guest_IP,
VexArch guest_arch,
const VexArchInfo* archinfo,
const VexAbiInfo* abiinfo,
VexEndness host_endness,
Bool sigill_diag);

/* Used by the optimiser to specialise calls to helpers. */
IRExpr* guest_riscv64_spechelper(const HChar* function_name,
IRExpr** args,
IRStmt** precedingStmts,
Int n_precedingStmts);

/* Describes to the optimiser which part of the guest state require precise
memory exceptions. This is logically part of the guest state description. */
Bool guest_riscv64_state_requires_precise_mem_exns(
Int minoff, Int maxoff, VexRegisterUpdates pxControl);

extern VexGuestLayout riscv64guest_layout;

/*------------------------------------------------------------*/
/*--- riscv64 guest helpers ---*/
/*------------------------------------------------------------*/

/* --- CLEAN HELPERS --- */

/* Calculate resulting flags of a specified floating-point operation. Returns
a 32-bit value where bits 4:0 contain the fflags in the RISC-V native
format (NV DZ OF UF NX) and remaining upper bits are zero. */
UInt riscv64g_calculate_fflags_fsqrt_s(Float a1, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fcvt_w_s(Float a1, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fcvt_wu_s(Float a1, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fcvt_s_w(UInt a1, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fcvt_s_wu(UInt a1, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fcvt_l_s(Float a1, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fcvt_lu_s(Float a1, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fcvt_s_l(ULong a1, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fcvt_s_lu(ULong a1, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fsqrt_d(Double a1, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fcvt_s_d(Double a1, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fcvt_w_d(Double a1, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fcvt_wu_d(Double a1, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fcvt_l_d(Double a1, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fcvt_lu_d(Double a1, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fcvt_d_l(ULong a1, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fcvt_d_lu(ULong a1, UInt rm_RISCV);

UInt riscv64g_calculate_fflags_fadd_s(Float a1, Float a2, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fmul_s(Float a1, Float a2, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fdiv_s(Float a1, Float a2, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fadd_d(Double a1, Double a2, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fmul_d(Double a1, Double a2, UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fdiv_d(Double a1, Double a2, UInt rm_RISCV);

UInt riscv64g_calculate_fflags_fmin_s(Float a1, Float a2);
UInt riscv64g_calculate_fflags_fmax_s(Float a1, Float a2);
UInt riscv64g_calculate_fflags_feq_s(Float a1, Float a2);
UInt riscv64g_calculate_fflags_flt_s(Float a1, Float a2);
UInt riscv64g_calculate_fflags_fle_s(Float a1, Float a2);
UInt riscv64g_calculate_fflags_fmin_d(Double a1, Double a2);
UInt riscv64g_calculate_fflags_fmax_d(Double a1, Double a2);
UInt riscv64g_calculate_fflags_feq_d(Double a1, Double a2);
UInt riscv64g_calculate_fflags_flt_d(Double a1, Double a2);
UInt riscv64g_calculate_fflags_fle_d(Double a1, Double a2);

UInt riscv64g_calculate_fflags_fmadd_s(Float a1,
Float a2,
Float a3,
UInt rm_RISCV);
UInt riscv64g_calculate_fflags_fmadd_d(Double a1,
Double a2,
Double a3,
UInt rm_RISCV);

/* Calculate floating-point class. Returns a 64-bit value where bits 9:0
contains the properties in the RISC-V FCLASS-instruction format and remaining
upper bits are zero. */
ULong riscv64g_calculate_fclass_s(Float a1);
ULong riscv64g_calculate_fclass_d(Double a1);

#endif /* ndef __VEX_GUEST_RISCV64_DEFS_H */

/*--------------------------------------------------------------------*/
/*--- end guest_riscv64_defs.h ---*/
/*--------------------------------------------------------------------*/
Loading

0 comments on commit 02ae513

Please sign in to comment.