Skip to content

Commit

Permalink
Merge pull request #2 from inspireMeNow/test
Browse files Browse the repository at this point in the history
riscv: Add support for riscv64ilp32 (X32 ABI)
  • Loading branch information
pz9115 authored Apr 29, 2024
2 parents 20a1b34 + 463fa7c commit f946be9
Show file tree
Hide file tree
Showing 21 changed files with 76 additions and 18 deletions.
16 changes: 16 additions & 0 deletions configs/devices/riscv64ilp32-softmmu/default.mak
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Default configuration for riscv64-softmmu

# Uncomment the following lines to disable these optional devices:
#
#CONFIG_PCI_DEVICES=n
CONFIG_SEMIHOSTING=y
CONFIG_ARM_COMPATIBLE_SEMIHOSTING=y

# Boards:
#
CONFIG_SPIKE=y
CONFIG_SIFIVE_E=y
CONFIG_SIFIVE_U=y
CONFIG_RISCV_VIRT=y
CONFIG_MICROCHIP_PFSOC=y
CONFIG_SHAKTI_C=y
6 changes: 6 additions & 0 deletions configs/targets/riscv64ilp32-linux-user.mak
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
TARGET_ARCH=riscv64ilp32
TARGET_BASE_ARCH=riscv
TARGET_ABI_DIR=riscv
TARGET_XML_FILES= gdb-xml/riscv-64bit-cpu.xml gdb-xml/riscv-32bit-fpu.xml gdb-xml/riscv-64bit-fpu.xml gdb-xml/riscv-64bit-virtual.xml
CONFIG_SEMIHOSTING=y
CONFIG_ARM_COMPATIBLE_SEMIHOSTING=y
5 changes: 5 additions & 0 deletions configs/targets/riscv64ilp32-softmmu.mak
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
TARGET_ARCH=riscv64ilp32
TARGET_BASE_ARCH=riscv
TARGET_SUPPORTS_MTTCG=y
TARGET_XML_FILES= gdb-xml/riscv-64bit-cpu.xml gdb-xml/riscv-32bit-fpu.xml gdb-xml/riscv-64bit-fpu.xml gdb-xml/riscv-64bit-virtual.xml
TARGET_NEED_FDT=y
2 changes: 1 addition & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ case "$cpu" in
CPU_CFLAGS="-m64 -mlittle-endian"
;;

riscv32 | riscv64)
riscv32 | riscv64 | riscv64ilp32)
host_arch=riscv
linux_arch=riscv
;;
Expand Down
4 changes: 4 additions & 0 deletions hw/riscv/boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,15 @@ char *riscv_plic_hart_config_string(int hart_count)

target_ulong riscv_calc_kernel_start_addr(RISCVHartArrayState *harts,
target_ulong firmware_end_addr) {
#ifdef TARGET_RISCV64ILP32
return QEMU_ALIGN_UP(firmware_end_addr, 4 * MiB);
#else
if (riscv_is_32bit(harts)) {
return QEMU_ALIGN_UP(firmware_end_addr, 4 * MiB);
} else {
return QEMU_ALIGN_UP(firmware_end_addr, 2 * MiB);
}
#endif
}

const char *riscv_default_firmware_name(RISCVHartArrayState *harts)
Expand Down
1 change: 1 addition & 0 deletions include/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,7 @@ typedef struct {
#define EF_RISCV_FLOAT_ABI_QUAD 0x0006
#define EF_RISCV_RVE 0x0008
#define EF_RISCV_TSO 0x0010
#define EF_RISCV_X32 0x0020

typedef struct elf32_rel {
Elf32_Addr r_offset;
Expand Down
2 changes: 1 addition & 1 deletion include/hw/riscv/sifive_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#if defined(TARGET_RISCV32)
#define SIFIVE_E_CPU TYPE_RISCV_CPU_SIFIVE_E31
#define SIFIVE_U_CPU TYPE_RISCV_CPU_SIFIVE_U34
#elif defined(TARGET_RISCV64)
#elif defined(TARGET_RISCV64) || defined(TARGET_RISCV64ILP32)
#define SIFIVE_E_CPU TYPE_RISCV_CPU_SIFIVE_E51
#define SIFIVE_U_CPU TYPE_RISCV_CPU_SIFIVE_U54
#endif
Expand Down
9 changes: 8 additions & 1 deletion linux-user/elfload.c
Original file line number Diff line number Diff line change
Expand Up @@ -1697,7 +1697,11 @@ static void elf_core_copy_regs(target_elf_gregset_t *regs,
#define ELF_START_MMAP 0x80000000
#define ELF_ARCH EM_RISCV

#ifdef TARGET_RISCV32
#ifndef ABI_X32_P
#define ABI_X32_P(e_flags) ((e_flags & EF_RISCV_X32) != 0)
#endif

#if defined(TARGET_RISCV32) || defined(TARGET_RISCV64ILP32)
#define ELF_CLASS ELFCLASS32
#else
#define ELF_CLASS ELFCLASS64
Expand Down Expand Up @@ -2058,6 +2062,9 @@ static bool elf_check_ident(struct elfhdr *ehdr)
&& ehdr->e_ident[EI_MAG1] == ELFMAG1
&& ehdr->e_ident[EI_MAG2] == ELFMAG2
&& ehdr->e_ident[EI_MAG3] == ELFMAG3
#ifdef TARGET_RISCV64ILP32
&& ABI_X32_P(ehdr->e_flags)
#endif
&& ehdr->e_ident[EI_CLASS] == ELF_CLASS
&& ehdr->e_ident[EI_DATA] == ELF_DATA
&& ehdr->e_ident[EI_VERSION] == EV_CURRENT);
Expand Down
3 changes: 3 additions & 0 deletions target/riscv/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ config RISCV32

config RISCV64
bool

config RISCV64ILP32
bool
4 changes: 4 additions & 0 deletions target/riscv/cpu-param.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
# define TARGET_LONG_BITS 64
# define TARGET_PHYS_ADDR_SPACE_BITS 56 /* 44-bit PPN */
# define TARGET_VIRT_ADDR_SPACE_BITS 48 /* sv48 */
#elif defined(TARGET_RISCV64ILP32)
# define TARGET_LONG_BITS 64
# define TARGET_PHYS_ADDR_SPACE_BITS 31
# define TARGET_VIRT_ADDR_SPACE_BITS 31
#elif defined(TARGET_RISCV32)
# define TARGET_LONG_BITS 32
# define TARGET_PHYS_ADDR_SPACE_BITS 34 /* 22-bit PPN */
Expand Down
2 changes: 1 addition & 1 deletion target/riscv/cpu-qom.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

#if defined(TARGET_RISCV32)
# define TYPE_RISCV_CPU_BASE TYPE_RISCV_CPU_BASE32
#elif defined(TARGET_RISCV64)
#elif defined(TARGET_RISCV64) || defined(TARGET_RISCV64ILP32)
# define TYPE_RISCV_CPU_BASE TYPE_RISCV_CPU_BASE64
#endif

Expand Down
10 changes: 5 additions & 5 deletions target/riscv/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ static void riscv_any_cpu_init(Object *obj)
CPURISCVState *env = &cpu->env;
#if defined(TARGET_RISCV32)
set_misa(env, MXL_RV32, RVI | RVM | RVA | RVF | RVD | RVC | RVU);
#elif defined(TARGET_RISCV64)
#elif defined(TARGET_RISCV64) || defined(TARGET_RISCV64ILP32)
set_misa(env, MXL_RV64, RVI | RVM | RVA | RVF | RVD | RVC | RVU);
#endif

Expand All @@ -386,7 +386,7 @@ static void riscv_any_cpu_init(Object *obj)
cpu->cfg.pmp = true;
}

#if defined(TARGET_RISCV64)
#if defined(TARGET_RISCV64) || defined(TARGET_RISCV64ILP32)
static void rv64_base_cpu_init(Object *obj)
{
CPURISCVState *env = &RISCV_CPU(obj)->env;
Expand Down Expand Up @@ -613,7 +613,7 @@ static void riscv_host_cpu_init(Object *obj)
CPURISCVState *env = &RISCV_CPU(obj)->env;
#if defined(TARGET_RISCV32)
set_misa(env, MXL_RV32, 0);
#elif defined(TARGET_RISCV64)
#elif defined(TARGET_RISCV64) || defined(TARGET_RISCV64ILP32)
set_misa(env, MXL_RV64, 0);
#endif
riscv_cpu_add_user_properties(obj);
Expand Down Expand Up @@ -1040,7 +1040,7 @@ static void riscv_cpu_validate_misa_mxl(RISCVCPU *cpu, Error **errp)

/* Validate that MISA_MXL is set properly. */
switch (env->misa_mxl_max) {
#ifdef TARGET_RISCV64
#if defined(TARGET_RISCV64) || defined(TARGET_RISCV64ILP32)
case MXL_RV64:
case MXL_RV128:
cc->gdb_core_xml_file = "riscv-64bit-cpu.xml";
Expand Down Expand Up @@ -2290,7 +2290,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E31, rv32_sifive_e_cpu_init),
DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E34, rv32_imafcu_nommu_cpu_init),
DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_U34, rv32_sifive_u_cpu_init),
#elif defined(TARGET_RISCV64)
#elif defined(TARGET_RISCV64) || defined(TARGET_RISCV64ILP32)
DEFINE_DYNAMIC_CPU(TYPE_RISCV_CPU_BASE64, rv64_base_cpu_init),
DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E51, rv64_sifive_e_cpu_init),
DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_U54, rv64_sifive_u_cpu_init),
Expand Down
8 changes: 8 additions & 0 deletions target/riscv/cpu_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ void cpu_get_tb_cpu_state(CPURISCVState *env, vaddr *pc,
RISCVExtStatus fs, vs;
uint32_t flags = 0;

#ifdef TARGET_RISCV64ILP32
*pc = env->pc & UINT32_MAX;
#else
*pc = env->xl == MXL_RV32 ? env->pc & UINT32_MAX : env->pc;
#endif
*cs_base = 0;

if (cpu->cfg.ext_zve32f) {
Expand Down Expand Up @@ -189,6 +193,10 @@ void riscv_cpu_update_mask(CPURISCVState *env)
env->cur_pmmask = mask;
env->cur_pmbase = base;
}

#ifdef TARGET_RISCV64ILP32
env->cur_pmmask = 0xffffffff00000000ULL;
#endif
}

#ifndef CONFIG_USER_ONLY
Expand Down
2 changes: 1 addition & 1 deletion target/riscv/csr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,7 @@ static RISCVException read_misa(CPURISCVState *env, int csrno,
case MXL_RV32:
misa = (target_ulong)MXL_RV32 << 30;
break;
#ifdef TARGET_RISCV64
#if defined(TARGET_RISCV64) || defined(TARGET_RISCV64ILP32)
case MXL_RV64:
misa = (target_ulong)MXL_RV64 << 62;
break;
Expand Down
4 changes: 4 additions & 0 deletions target/riscv/gdbstub.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ int riscv_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
if (n < 32) {
tmp = env->gpr[n];
} else if (n == 32) {
#ifdef TARGET_RISCV64ILP32
tmp = env->pc & UINT32_MAX;
#else
tmp = env->pc;
#endif
} else {
return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions target/riscv/insn_trans/trans_rvd.c.inc
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ static bool trans_fmv_x_d(DisasContext *ctx, arg_fmv_x_d *a)
REQUIRE_FPU;
REQUIRE_EXT(ctx, RVD);

#ifdef TARGET_RISCV64
#if defined(TARGET_RISCV64) || defined(TARGET_RISCV64ILP32)
gen_set_gpr(ctx, a->rd, cpu_fpr[a->rs1]);
return true;
#else
Expand Down Expand Up @@ -573,7 +573,7 @@ static bool trans_fmv_d_x(DisasContext *ctx, arg_fmv_d_x *a)
REQUIRE_FPU;
REQUIRE_EXT(ctx, RVD);

#ifdef TARGET_RISCV64
#if defined(TARGET_RISCV64) || defined(TARGET_RISCV64ILP32)
tcg_gen_mov_tl(cpu_fpr[a->rd], get_gpr(ctx, a->rs1, EXT_NONE));
mark_fs_dirty(ctx);
return true;
Expand Down
2 changes: 1 addition & 1 deletion target/riscv/insn_trans/trans_rvf.c.inc
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ static bool trans_fmv_x_w(DisasContext *ctx, arg_fmv_x_w *a)

TCGv dest = dest_gpr(ctx, a->rd);
TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
#if defined(TARGET_RISCV64)
#if defined(TARGET_RISCV64) || defined(TARGET_RISCV64ILP32)
tcg_gen_ext32s_tl(dest, src1);
#else
tcg_gen_extrl_i64_i32(dest, src1);
Expand Down
2 changes: 1 addition & 1 deletion target/riscv/insn_trans/trans_rvzfh.c.inc
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ static bool trans_fmv_x_h(DisasContext *ctx, arg_fmv_x_h *a)

TCGv dest = dest_gpr(ctx, a->rd);

#if defined(TARGET_RISCV64)
#if defined(TARGET_RISCV64) || defined(TARGET_RISCV64ILP32)
/* 16 bits -> 64 bits */
tcg_gen_ext16s_tl(dest, cpu_fpr[a->rs1]);
#else
Expand Down
2 changes: 1 addition & 1 deletion target/riscv/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
/* Perform linear address sign extension */
static target_ulong addr_canonical(int va_bits, target_ulong addr)
{
#ifdef TARGET_RISCV64
#if defined(TARGET_RISCV64) || defined(TARGET_RISCV64ILP32)
if (addr & (1UL << (va_bits - 1))) {
addr |= (hwaddr)-(1L << va_bits);
}
Expand Down
4 changes: 2 additions & 2 deletions target/riscv/translate.c
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ static TCGv_i64 get_fpr_d(DisasContext *ctx, int reg_num)
tcg_gen_concat_tl_i64(t, cpu_gpr[reg_num], cpu_gpr[reg_num + 1]);
return t;
}
#ifdef TARGET_RISCV64
#if defined(TARGET_RISCV64) || defined(TARGET_RISCV64ILP32)
case MXL_RV64:
return cpu_gpr[reg_num];
#endif
Expand All @@ -493,7 +493,7 @@ static TCGv_i64 dest_fpr(DisasContext *ctx, int reg_num)
switch (get_xl(ctx)) {
case MXL_RV32:
return tcg_temp_new_i64();
#ifdef TARGET_RISCV64
#if defined(TARGET_RISCV64) || defined(TARGET_RISCV64ILP32)
case MXL_RV64:
return cpu_gpr[reg_num];
#endif
Expand Down
2 changes: 1 addition & 1 deletion target/riscv/vector_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ static inline int vext_elem_mask(void *v0, int index)
}

/* elements operations for load and store */
typedef void vext_ldst_elem_fn(CPURISCVState *env, target_ulong addr,
typedef void vext_ldst_elem_fn(CPURISCVState *env, abi_ptr addr,
uint32_t idx, void *vd, uintptr_t retaddr);

#define GEN_VEXT_LD_ELEM(NAME, ETYPE, H, LDSUF) \
Expand Down

0 comments on commit f946be9

Please sign in to comment.