mirror of
https://github.com/yuzu-emu/unicorn.git
synced 2024-12-31 23:15:44 +00:00
target/riscv: add vector configure instruction
vsetvl and vsetvli are two configure instructions for vl, vtype. TB flags should update after configure instructions. The (ill, lmul, sew ) of vtype and the bit of (VSTART == 0 && VL == VLMAX) will be placed within tb_flags. Backports 2b7168fc43fb270fb89e1dddc17ef54714712f3a from qemu
This commit is contained in:
parent
0554e79ad1
commit
9db3b70869
|
@ -6068,6 +6068,7 @@ riscv_symbols = (
|
|||
'helper_tlb_flush',
|
||||
'helper_set_rounding_mode',
|
||||
'helper_sret',
|
||||
'helper_vsetvl',
|
||||
'pmp_hart_has_privs',
|
||||
'pmpaddr_csr_read',
|
||||
'pmpaddr_csr_write',
|
||||
|
|
|
@ -3522,6 +3522,7 @@
|
|||
#define helper_tlb_flush helper_tlb_flush_riscv32
|
||||
#define helper_set_rounding_mode helper_set_rounding_mode_riscv32
|
||||
#define helper_sret helper_sret_riscv32
|
||||
#define helper_vsetvl helper_vsetvl_riscv32
|
||||
#define pmp_hart_has_privs pmp_hart_has_privs_riscv32
|
||||
#define pmpaddr_csr_read pmpaddr_csr_read_riscv32
|
||||
#define pmpaddr_csr_write pmpaddr_csr_write_riscv32
|
||||
|
|
|
@ -3522,6 +3522,7 @@
|
|||
#define helper_tlb_flush helper_tlb_flush_riscv64
|
||||
#define helper_set_rounding_mode helper_set_rounding_mode_riscv64
|
||||
#define helper_sret helper_sret_riscv64
|
||||
#define helper_vsetvl helper_vsetvl_riscv64
|
||||
#define pmp_hart_has_privs pmp_hart_has_privs_riscv64
|
||||
#define pmpaddr_csr_read pmpaddr_csr_read_riscv64
|
||||
#define pmpaddr_csr_write pmpaddr_csr_write_riscv64
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
obj-y += translate.o op_helper.o cpu_helper.o cpu.o csr.o fpu_helper.o pmp.o
|
||||
obj-y += translate.o op_helper.o cpu_helper.o cpu.o csr.o fpu_helper.o vector_helper.o pmp.o
|
||||
obj-y += unicorn.o
|
||||
|
||||
DECODETREE = $(SRC_PATH)/scripts/decodetree.py
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
|
||||
#include "qemu-common.h"
|
||||
#include "qom/cpu.h"
|
||||
#include "hw/registerfields.h"
|
||||
#include "exec/cpu-defs.h"
|
||||
#include "fpu/softfloat-types.h"
|
||||
|
||||
|
@ -98,6 +99,12 @@ typedef struct CPURISCVState CPURISCVState;
|
|||
|
||||
#define RV_VLEN_MAX 512
|
||||
|
||||
FIELD(VTYPE, VLMUL, 0, 2)
|
||||
FIELD(VTYPE, VSEW, 2, 3)
|
||||
FIELD(VTYPE, VEDIV, 5, 2)
|
||||
FIELD(VTYPE, RESERVED, 7, sizeof(target_ulong) * 8 - 9)
|
||||
FIELD(VTYPE, VILL, sizeof(target_ulong) * 8 - 2, 1)
|
||||
|
||||
struct CPURISCVState {
|
||||
target_ulong gpr[32];
|
||||
uint64_t fpr[32]; /* assume both F and D extensions */
|
||||
|
@ -351,19 +358,62 @@ void riscv_cpu_set_fflags(CPURISCVState *env, target_ulong);
|
|||
#define TB_FLAGS_MMU_MASK 3
|
||||
#define TB_FLAGS_MSTATUS_FS MSTATUS_FS
|
||||
|
||||
static inline void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
|
||||
target_ulong *cs_base, uint32_t *flags)
|
||||
typedef CPURISCVState CPUArchState;
|
||||
typedef RISCVCPU ArchCPU;
|
||||
#include "exec/cpu-all.h"
|
||||
|
||||
FIELD(TB_FLAGS, VL_EQ_VLMAX, 2, 1)
|
||||
FIELD(TB_FLAGS, LMUL, 3, 2)
|
||||
FIELD(TB_FLAGS, SEW, 5, 3)
|
||||
FIELD(TB_FLAGS, VILL, 8, 1)
|
||||
|
||||
/*
|
||||
* A simplification for VLMAX
|
||||
* = (1 << LMUL) * VLEN / (8 * (1 << SEW))
|
||||
* = (VLEN << LMUL) / (8 << SEW)
|
||||
* = (VLEN << LMUL) >> (SEW + 3)
|
||||
* = VLEN >> (SEW + 3 - LMUL)
|
||||
*/
|
||||
static inline uint32_t vext_get_vlmax(RISCVCPU *cpu, target_ulong vtype)
|
||||
{
|
||||
uint8_t sew, lmul;
|
||||
|
||||
sew = FIELD_EX64(vtype, VTYPE, VSEW);
|
||||
lmul = FIELD_EX64(vtype, VTYPE, VLMUL);
|
||||
return cpu->cfg.vlen >> (sew + 3 - lmul);
|
||||
}
|
||||
|
||||
static inline void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
|
||||
target_ulong *cs_base, uint32_t *pflags)
|
||||
{
|
||||
uint32_t flags = 0;
|
||||
|
||||
*pc = env->pc;
|
||||
*cs_base = 0;
|
||||
|
||||
if (riscv_has_ext(env, RVV)) {
|
||||
uint32_t vlmax = vext_get_vlmax(env_archcpu(env), env->vtype);
|
||||
bool vl_eq_vlmax = (env->vstart == 0) && (vlmax == env->vl);
|
||||
flags = FIELD_DP32(flags, TB_FLAGS, VILL,
|
||||
FIELD_EX64(env->vtype, VTYPE, VILL));
|
||||
flags = FIELD_DP32(flags, TB_FLAGS, SEW,
|
||||
FIELD_EX64(env->vtype, VTYPE, VSEW));
|
||||
flags = FIELD_DP32(flags, TB_FLAGS, LMUL,
|
||||
FIELD_EX64(env->vtype, VTYPE, VLMUL));
|
||||
flags = FIELD_DP32(flags, TB_FLAGS, VL_EQ_VLMAX, vl_eq_vlmax);
|
||||
} else {
|
||||
flags = FIELD_DP32(flags, TB_FLAGS, VILL, 1);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_USER_ONLY
|
||||
*flags = TB_FLAGS_MSTATUS_FS;
|
||||
flags |= TB_FLAGS_MSTATUS_FS;
|
||||
#else
|
||||
*flags = cpu_mmu_index(env, 0);
|
||||
flags |= cpu_mmu_index(env, 0);
|
||||
if (riscv_cpu_fp_enabled(env)) {
|
||||
*flags |= env->mstatus & MSTATUS_FS;
|
||||
flags |= env->mstatus & MSTATUS_FS;
|
||||
}
|
||||
#endif
|
||||
*pflags = flags;
|
||||
}
|
||||
|
||||
int riscv_csrrw(CPURISCVState *env, int csrno, target_ulong *ret_value,
|
||||
|
@ -402,11 +452,6 @@ typedef struct {
|
|||
void riscv_get_csr_ops(int csrno, riscv_csr_operations *ops);
|
||||
void riscv_set_csr_ops(int csrno, riscv_csr_operations *ops);
|
||||
|
||||
typedef CPURISCVState CPUArchState;
|
||||
typedef RISCVCPU ArchCPU;
|
||||
|
||||
#include "exec/cpu-all.h"
|
||||
|
||||
// Unicorn-specific
|
||||
void riscv_cpu_register_types(void *opaque);
|
||||
|
||||
|
|
|
@ -84,3 +84,6 @@ DEF_HELPER_1(tlb_flush, void, env)
|
|||
#ifndef CONFIG_USER_ONLY
|
||||
DEF_HELPER_1(hyp_tlb_flush, void, env)
|
||||
#endif
|
||||
|
||||
/* Vector functions */
|
||||
DEF_HELPER_3(vsetvl, tl, env, tl, tl)
|
||||
|
|
|
@ -62,6 +62,7 @@
|
|||
@r_rm ....... ..... ..... ... ..... ....... %rs2 %rs1 %rm %rd
|
||||
@r2_rm ....... ..... ..... ... ..... ....... %rs1 %rm %rd
|
||||
@r2 ....... ..... ..... ... ..... ....... %rs1 %rd
|
||||
@r2_zimm . zimm:11 ..... ... ..... ....... %rs1 %rd
|
||||
|
||||
@hfence_gvma ....... ..... ..... ... ..... ....... %rs2 %rs1
|
||||
@hfence_vvma ....... ..... ..... ... ..... ....... %rs2 %rs1
|
||||
|
@ -209,3 +210,7 @@ fcvt_d_wu 1101001 00001 ..... ... ..... 1010011 @r2_rm
|
|||
# *** RV32H Base Instruction Set ***
|
||||
hfence_gvma 0110001 ..... ..... 000 00000 1110011 @hfence_gvma
|
||||
hfence_vvma 0010001 ..... ..... 000 00000 1110011 @hfence_vvma
|
||||
|
||||
# *** RV32V Extension ***
|
||||
vsetvli 0 ........... ..... 111 ..... 1010111 @r2_zimm
|
||||
vsetvl 1000000 ..... ..... 111 ..... 1010111 @r
|
||||
|
|
81
qemu/target/riscv/insn_trans/trans_rvv.inc.c
Normal file
81
qemu/target/riscv/insn_trans/trans_rvv.inc.c
Normal file
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* RISC-V translation routines for the RVV Standard Extension.
|
||||
*
|
||||
* Copyright (c) 2020 T-Head Semiconductor Co., Ltd. All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms and conditions of the GNU General Public License,
|
||||
* version 2 or later, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope 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/>.
|
||||
*/
|
||||
|
||||
static bool trans_vsetvl(DisasContext *ctx, arg_vsetvl *a)
|
||||
{
|
||||
TCGContext *tcg_ctx = ctx->uc->tcg_ctx;
|
||||
TCGv s1, s2, dst;
|
||||
|
||||
if (!has_ext(ctx, RVV)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
s2 = tcg_temp_new(tcg_ctx);
|
||||
dst = tcg_temp_new(tcg_ctx);
|
||||
|
||||
/* Using x0 as the rs1 register specifier, encodes an infinite AVL */
|
||||
if (a->rs1 == 0) {
|
||||
/* As the mask is at least one bit, RV_VLEN_MAX is >= VLMAX */
|
||||
s1 = tcg_const_tl(tcg_ctx, RV_VLEN_MAX);
|
||||
} else {
|
||||
s1 = tcg_temp_new(tcg_ctx);
|
||||
gen_get_gpr(ctx, s1, a->rs1);
|
||||
}
|
||||
gen_get_gpr(ctx, s2, a->rs2);
|
||||
gen_helper_vsetvl(tcg_ctx, dst, tcg_ctx->cpu_env, s1, s2);
|
||||
gen_set_gpr(ctx, a->rd, dst);
|
||||
tcg_gen_movi_tl(tcg_ctx, tcg_ctx->cpu_pc_risc, ctx->pc_succ_insn);
|
||||
lookup_and_goto_ptr(ctx);
|
||||
ctx->base.is_jmp = DISAS_NORETURN;
|
||||
|
||||
tcg_temp_free(tcg_ctx, s1);
|
||||
tcg_temp_free(tcg_ctx, s2);
|
||||
tcg_temp_free(tcg_ctx, dst);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool trans_vsetvli(DisasContext *ctx, arg_vsetvli *a)
|
||||
{
|
||||
TCGContext *tcg_ctx = ctx->uc->tcg_ctx;
|
||||
TCGv s1, s2, dst;
|
||||
|
||||
if (!has_ext(ctx, RVV)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
s2 = tcg_const_tl(tcg_ctx, a->zimm);
|
||||
dst = tcg_temp_new(tcg_ctx);
|
||||
|
||||
/* Using x0 as the rs1 register specifier, encodes an infinite AVL */
|
||||
if (a->rs1 == 0) {
|
||||
/* As the mask is at least one bit, RV_VLEN_MAX is >= VLMAX */
|
||||
s1 = tcg_const_tl(tcg_ctx, RV_VLEN_MAX);
|
||||
} else {
|
||||
s1 = tcg_temp_new(tcg_ctx);
|
||||
gen_get_gpr(ctx, s1, a->rs1);
|
||||
}
|
||||
gen_helper_vsetvl(tcg_ctx, dst, tcg_ctx->cpu_env, s1, s2);
|
||||
gen_set_gpr(ctx, a->rd, dst);
|
||||
gen_goto_tb(ctx, 0, ctx->pc_succ_insn);
|
||||
ctx->base.is_jmp = DISAS_NORETURN;
|
||||
|
||||
tcg_temp_free(tcg_ctx, s1);
|
||||
tcg_temp_free(tcg_ctx, s2);
|
||||
tcg_temp_free(tcg_ctx, dst);
|
||||
return true;
|
||||
}
|
|
@ -54,6 +54,12 @@ typedef struct DisasContext {
|
|||
to reset this known value. */
|
||||
int frm;
|
||||
bool ext_ifencei;
|
||||
/* vector extension */
|
||||
bool vill;
|
||||
uint8_t lmul;
|
||||
uint8_t sew;
|
||||
uint16_t vlen;
|
||||
bool vl_eq_vlmax;
|
||||
|
||||
// Unicorn engine
|
||||
struct uc_struct *uc;
|
||||
|
@ -743,6 +749,7 @@ static bool gen_shift(DisasContext *ctx, arg_r *a,
|
|||
#include "insn_trans/trans_rvf.inc.c"
|
||||
#include "insn_trans/trans_rvd.inc.c"
|
||||
#include "insn_trans/trans_rvh.inc.c"
|
||||
#include "insn_trans/trans_rvv.inc.c"
|
||||
#include "insn_trans/trans_privileged.inc.c"
|
||||
|
||||
/* Include the auto-generated decoder for 16 bit insn */
|
||||
|
@ -774,11 +781,12 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
|
|||
DisasContext *ctx = container_of(dcbase, DisasContext, base);
|
||||
CPURISCVState *env = cs->env_ptr;
|
||||
RISCVCPU *cpu = RISCV_CPU(cs->uc, cs);
|
||||
uint32_t tb_flags = ctx->base.tb->flags;
|
||||
|
||||
ctx->uc = cs->uc;
|
||||
ctx->pc_succ_insn = ctx->base.pc_first;
|
||||
ctx->mem_idx = ctx->base.tb->flags & TB_FLAGS_MMU_MASK;
|
||||
ctx->mstatus_fs = ctx->base.tb->flags & TB_FLAGS_MSTATUS_FS;
|
||||
ctx->mem_idx = tb_flags & TB_FLAGS_MMU_MASK;
|
||||
ctx->mstatus_fs = tb_flags & TB_FLAGS_MSTATUS_FS;
|
||||
ctx->priv_ver = env->priv_ver;
|
||||
#if !defined(CONFIG_USER_ONLY)
|
||||
if (riscv_has_ext(env, RVH)) {
|
||||
|
@ -802,6 +810,11 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
|
|||
ctx->misa = env->misa;
|
||||
ctx->frm = -1; /* unknown rounding mode */
|
||||
ctx->ext_ifencei = cpu->cfg.ext_ifencei;
|
||||
ctx->vlen = cpu->cfg.vlen;
|
||||
ctx->vill = FIELD_EX32(tb_flags, TB_FLAGS, VILL);
|
||||
ctx->sew = FIELD_EX32(tb_flags, TB_FLAGS, SEW);
|
||||
ctx->lmul = FIELD_EX32(tb_flags, TB_FLAGS, LMUL);
|
||||
ctx->vl_eq_vlmax = FIELD_EX32(tb_flags, TB_FLAGS, VL_EQ_VLMAX);
|
||||
}
|
||||
|
||||
static void riscv_tr_tb_start(DisasContextBase *db, CPUState *cpu)
|
||||
|
|
53
qemu/target/riscv/vector_helper.c
Normal file
53
qemu/target/riscv/vector_helper.c
Normal file
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* RISC-V Vector Extension Helpers for QEMU.
|
||||
*
|
||||
* Copyright (c) 2020 T-Head Semiconductor Co., Ltd. All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms and conditions of the GNU General Public License,
|
||||
* version 2 or later, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope 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/>.
|
||||
*/
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "cpu.h"
|
||||
#include "exec/exec-all.h"
|
||||
#include "exec/helper-proto.h"
|
||||
#include <math.h>
|
||||
|
||||
target_ulong HELPER(vsetvl)(CPURISCVState *env, target_ulong s1,
|
||||
target_ulong s2)
|
||||
{
|
||||
int vlmax, vl;
|
||||
RISCVCPU *cpu = env_archcpu(env);
|
||||
uint16_t sew = 8 << FIELD_EX64(s2, VTYPE, VSEW);
|
||||
uint8_t ediv = FIELD_EX64(s2, VTYPE, VEDIV);
|
||||
bool vill = FIELD_EX64(s2, VTYPE, VILL);
|
||||
target_ulong reserved = FIELD_EX64(s2, VTYPE, RESERVED);
|
||||
|
||||
if ((sew > cpu->cfg.elen) || vill || (ediv != 0) || (reserved != 0)) {
|
||||
/* only set vill bit. */
|
||||
env->vtype = FIELD_DP64(0, VTYPE, VILL, 1);
|
||||
env->vl = 0;
|
||||
env->vstart = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
vlmax = vext_get_vlmax(cpu, s2);
|
||||
if (s1 <= vlmax) {
|
||||
vl = s1;
|
||||
} else {
|
||||
vl = vlmax;
|
||||
}
|
||||
env->vl = vl;
|
||||
env->vtype = s2;
|
||||
env->vstart = 0;
|
||||
return vl;
|
||||
}
|
Loading…
Reference in a new issue