2018-03-02 15:31:11 +03:00
|
|
|
/*
|
|
|
|
* RISC-V emulation for qemu: main translation routines.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
|
|
|
|
*
|
|
|
|
* 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 "qemu/log.h"
|
|
|
|
#include "cpu.h"
|
2020-01-01 14:23:00 +03:00
|
|
|
#include "tcg/tcg-op.h"
|
2018-03-02 15:31:11 +03:00
|
|
|
#include "exec/exec-all.h"
|
|
|
|
#include "exec/helper-proto.h"
|
|
|
|
#include "exec/helper-gen.h"
|
|
|
|
|
2018-02-14 02:27:54 +03:00
|
|
|
#include "exec/translator.h"
|
2018-03-02 15:31:11 +03:00
|
|
|
#include "exec/log.h"
|
2022-08-22 17:12:30 +03:00
|
|
|
#include "semihosting/semihost.h"
|
2018-03-02 15:31:11 +03:00
|
|
|
|
2021-12-10 10:56:49 +03:00
|
|
|
#include "internals.h"
|
2018-03-02 15:31:11 +03:00
|
|
|
|
2023-03-31 20:37:04 +03:00
|
|
|
#define HELPER_H "helper.h"
|
|
|
|
#include "exec/helper-info.c.inc"
|
|
|
|
#undef HELPER_H
|
|
|
|
|
2024-05-06 05:36:07 +03:00
|
|
|
#include "tcg/tcg-cpu.h"
|
|
|
|
|
2018-03-02 15:31:11 +03:00
|
|
|
/* global register indices */
|
2022-01-07 00:00:56 +03:00
|
|
|
static TCGv cpu_gpr[32], cpu_gprh[32], cpu_pc, cpu_vl, cpu_vstart;
|
2018-03-02 15:31:11 +03:00
|
|
|
static TCGv_i64 cpu_fpr[32]; /* assume F and D extensions */
|
|
|
|
static TCGv load_res;
|
|
|
|
static TCGv load_val;
|
2021-10-25 20:36:08 +03:00
|
|
|
/* globals for PM CSRs */
|
2022-01-20 15:20:39 +03:00
|
|
|
static TCGv pm_mask;
|
|
|
|
static TCGv pm_base;
|
2018-03-02 15:31:11 +03:00
|
|
|
|
2021-08-23 22:55:10 +03:00
|
|
|
/*
|
|
|
|
* If an operation is being performed on less than TARGET_LONG_BITS,
|
|
|
|
* it may require the inputs to be sign- or zero-extended; which will
|
|
|
|
* depend on the exact operation being performed.
|
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
EXT_NONE,
|
|
|
|
EXT_SIGN,
|
|
|
|
EXT_ZERO,
|
|
|
|
} DisasExtend;
|
|
|
|
|
2018-03-02 15:31:11 +03:00
|
|
|
typedef struct DisasContext {
|
2018-02-14 02:28:36 +03:00
|
|
|
DisasContextBase base;
|
2023-05-26 10:21:19 +03:00
|
|
|
target_ulong cur_insn_len;
|
2023-05-26 10:21:23 +03:00
|
|
|
target_ulong pc_save;
|
2019-01-15 02:58:32 +03:00
|
|
|
target_ulong priv_ver;
|
2022-01-07 00:00:59 +03:00
|
|
|
RISCVMXL misa_mxl_max;
|
2021-10-20 06:16:57 +03:00
|
|
|
RISCVMXL xl;
|
2023-06-14 06:25:46 +03:00
|
|
|
RISCVMXL address_xl;
|
2021-10-20 06:16:57 +03:00
|
|
|
uint32_t misa_ext;
|
2020-02-01 04:02:46 +03:00
|
|
|
uint32_t opcode;
|
2023-04-12 14:43:11 +03:00
|
|
|
RISCVExtStatus mstatus_fs;
|
|
|
|
RISCVExtStatus mstatus_vs;
|
2018-03-02 15:31:11 +03:00
|
|
|
uint32_t mem_idx;
|
2023-04-12 14:43:14 +03:00
|
|
|
uint32_t priv;
|
2023-04-05 11:58:12 +03:00
|
|
|
/*
|
|
|
|
* Remember the rounding mode encoded in the previous fp instruction,
|
|
|
|
* which we have already installed into env->fp_status. Or -1 for
|
|
|
|
* no previous fp instruction. Note that we exit the TB when writing
|
|
|
|
* to any system register, which includes CSR_FRM, so we do not have
|
|
|
|
* to reset this known value.
|
|
|
|
*/
|
2018-03-02 15:31:11 +03:00
|
|
|
int frm;
|
2021-10-20 06:17:03 +03:00
|
|
|
RISCVMXL ol;
|
2022-10-16 15:47:24 +03:00
|
|
|
bool virt_inst_excp;
|
2021-08-23 22:55:10 +03:00
|
|
|
bool virt_enabled;
|
2022-02-02 03:52:44 +03:00
|
|
|
const RISCVCPUConfig *cfg_ptr;
|
2020-07-01 18:24:52 +03:00
|
|
|
/* vector extension */
|
|
|
|
bool vill;
|
2021-12-10 10:55:59 +03:00
|
|
|
/*
|
|
|
|
* Encode LMUL to lmul as follows:
|
|
|
|
* LMUL vlmul lmul
|
|
|
|
* 1 000 0
|
|
|
|
* 2 001 1
|
|
|
|
* 4 010 2
|
|
|
|
* 8 011 3
|
|
|
|
* - 100 -
|
|
|
|
* 1/8 101 -3
|
|
|
|
* 1/4 110 -2
|
|
|
|
* 1/2 111 -1
|
|
|
|
*/
|
|
|
|
int8_t lmul;
|
2020-07-01 18:24:52 +03:00
|
|
|
uint8_t sew;
|
2022-06-06 09:16:16 +03:00
|
|
|
uint8_t vta;
|
2022-06-20 09:51:02 +03:00
|
|
|
uint8_t vma;
|
2022-06-06 09:16:16 +03:00
|
|
|
bool cfg_vta_all_1s;
|
2023-04-12 14:43:13 +03:00
|
|
|
bool vstart_eq_zero;
|
2020-07-01 18:24:52 +03:00
|
|
|
bool vl_eq_vlmax;
|
2021-01-09 01:42:52 +03:00
|
|
|
CPUState *cs;
|
2021-08-23 22:55:10 +03:00
|
|
|
TCGv zero;
|
2021-10-25 20:36:08 +03:00
|
|
|
/* PointerMasking extension */
|
2022-01-20 15:20:41 +03:00
|
|
|
bool pm_mask_enabled;
|
|
|
|
bool pm_base_enabled;
|
2024-02-07 15:22:54 +03:00
|
|
|
/* Ztso */
|
|
|
|
bool ztso;
|
2022-10-13 09:29:43 +03:00
|
|
|
/* Use icount trigger for native debug */
|
|
|
|
bool itrigger;
|
2023-01-15 19:06:56 +03:00
|
|
|
/* FRM is known to contain a valid value. */
|
|
|
|
bool frm_valid;
|
2024-04-07 00:12:14 +03:00
|
|
|
bool insn_start_updated;
|
2024-05-06 05:36:07 +03:00
|
|
|
const GPtrArray *decoders;
|
2018-03-02 15:31:11 +03:00
|
|
|
} DisasContext;
|
|
|
|
|
2019-01-15 02:58:42 +03:00
|
|
|
static inline bool has_ext(DisasContext *ctx, uint32_t ext)
|
|
|
|
{
|
2021-10-20 06:16:57 +03:00
|
|
|
return ctx->misa_ext & ext;
|
2020-07-24 03:28:02 +03:00
|
|
|
}
|
|
|
|
|
2021-04-24 06:33:18 +03:00
|
|
|
#ifdef TARGET_RISCV32
|
2021-10-20 06:17:02 +03:00
|
|
|
#define get_xl(ctx) MXL_RV32
|
2021-04-24 06:33:18 +03:00
|
|
|
#elif defined(CONFIG_USER_ONLY)
|
2021-10-20 06:17:02 +03:00
|
|
|
#define get_xl(ctx) MXL_RV64
|
2021-04-24 06:33:18 +03:00
|
|
|
#else
|
2021-10-20 06:17:02 +03:00
|
|
|
#define get_xl(ctx) ((ctx)->xl)
|
|
|
|
#endif
|
|
|
|
|
2023-06-14 06:25:46 +03:00
|
|
|
#ifdef TARGET_RISCV32
|
|
|
|
#define get_address_xl(ctx) MXL_RV32
|
|
|
|
#elif defined(CONFIG_USER_ONLY)
|
|
|
|
#define get_address_xl(ctx) MXL_RV64
|
|
|
|
#else
|
|
|
|
#define get_address_xl(ctx) ((ctx)->address_xl)
|
|
|
|
#endif
|
|
|
|
|
2021-10-20 06:17:02 +03:00
|
|
|
/* The word size for this machine mode. */
|
|
|
|
static inline int __attribute__((unused)) get_xlen(DisasContext *ctx)
|
2021-04-24 06:33:18 +03:00
|
|
|
{
|
2021-10-20 06:17:02 +03:00
|
|
|
return 16 << get_xl(ctx);
|
2021-04-24 06:33:18 +03:00
|
|
|
}
|
|
|
|
|
2021-10-20 06:17:03 +03:00
|
|
|
/* The operation length, as opposed to the xlen. */
|
|
|
|
#ifdef TARGET_RISCV32
|
|
|
|
#define get_ol(ctx) MXL_RV32
|
|
|
|
#else
|
|
|
|
#define get_ol(ctx) ((ctx)->ol)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline int get_olen(DisasContext *ctx)
|
2021-08-23 22:55:17 +03:00
|
|
|
{
|
2021-10-20 06:17:03 +03:00
|
|
|
return 16 << get_ol(ctx);
|
2021-08-23 22:55:17 +03:00
|
|
|
}
|
|
|
|
|
2022-01-07 00:00:59 +03:00
|
|
|
/* The maximum register length */
|
|
|
|
#ifdef TARGET_RISCV32
|
|
|
|
#define get_xl_max(ctx) MXL_RV32
|
|
|
|
#else
|
|
|
|
#define get_xl_max(ctx) ((ctx)->misa_mxl_max)
|
|
|
|
#endif
|
|
|
|
|
2020-07-24 03:28:02 +03:00
|
|
|
/*
|
|
|
|
* RISC-V requires NaN-boxing of narrower width floating point values.
|
|
|
|
* This applies when a 32-bit value is assigned to a 64-bit FP register.
|
|
|
|
* For consistency and simplicity, we nanbox results even when the RVD
|
|
|
|
* extension is not present.
|
|
|
|
*/
|
|
|
|
static void gen_nanbox_s(TCGv_i64 out, TCGv_i64 in)
|
|
|
|
{
|
|
|
|
tcg_gen_ori_i64(out, in, MAKE_64BIT_MASK(32, 32));
|
2020-07-24 03:28:05 +03:00
|
|
|
}
|
|
|
|
|
2021-12-10 10:43:20 +03:00
|
|
|
static void gen_nanbox_h(TCGv_i64 out, TCGv_i64 in)
|
|
|
|
{
|
|
|
|
tcg_gen_ori_i64(out, in, MAKE_64BIT_MASK(16, 48));
|
|
|
|
}
|
|
|
|
|
2020-07-24 03:28:05 +03:00
|
|
|
/*
|
|
|
|
* A narrow n-bit operation, where n < FLEN, checks that input operands
|
|
|
|
* are correctly Nan-boxed, i.e., all upper FLEN - n bits are 1.
|
|
|
|
* If so, the least-significant bits of the input are used, otherwise the
|
|
|
|
* input value is treated as an n-bit canonical NaN (v2.2 section 9.2).
|
|
|
|
*
|
|
|
|
* Here, the result is always nan-boxed, even the canonical nan.
|
|
|
|
*/
|
2021-12-10 10:43:22 +03:00
|
|
|
static void gen_check_nanbox_h(TCGv_i64 out, TCGv_i64 in)
|
|
|
|
{
|
2023-02-26 22:15:34 +03:00
|
|
|
TCGv_i64 t_max = tcg_constant_i64(0xffffffffffff0000ull);
|
|
|
|
TCGv_i64 t_nan = tcg_constant_i64(0xffffffffffff7e00ull);
|
2021-12-10 10:43:22 +03:00
|
|
|
|
|
|
|
tcg_gen_movcond_i64(TCG_COND_GEU, out, in, t_max, in, t_nan);
|
|
|
|
}
|
|
|
|
|
2020-07-24 03:28:05 +03:00
|
|
|
static void gen_check_nanbox_s(TCGv_i64 out, TCGv_i64 in)
|
|
|
|
{
|
2021-08-23 22:55:06 +03:00
|
|
|
TCGv_i64 t_max = tcg_constant_i64(0xffffffff00000000ull);
|
|
|
|
TCGv_i64 t_nan = tcg_constant_i64(0xffffffff7fc00000ull);
|
2020-07-24 03:28:05 +03:00
|
|
|
|
|
|
|
tcg_gen_movcond_i64(TCG_COND_GEU, out, in, t_max, in, t_nan);
|
2019-01-15 02:58:42 +03:00
|
|
|
}
|
|
|
|
|
2022-06-05 02:10:04 +03:00
|
|
|
static void decode_save_opc(DisasContext *ctx)
|
|
|
|
{
|
2024-04-07 00:12:14 +03:00
|
|
|
assert(!ctx->insn_start_updated);
|
|
|
|
ctx->insn_start_updated = true;
|
|
|
|
tcg_set_insn_start_param(ctx->base.insn_start, 1, ctx->opcode);
|
2022-06-05 02:10:04 +03:00
|
|
|
}
|
|
|
|
|
2023-05-26 10:21:18 +03:00
|
|
|
static void gen_pc_plus_diff(TCGv target, DisasContext *ctx,
|
2023-05-26 10:21:22 +03:00
|
|
|
target_long diff)
|
2022-01-20 15:20:31 +03:00
|
|
|
{
|
2023-05-26 10:21:22 +03:00
|
|
|
target_ulong dest = ctx->base.pc_next + diff;
|
|
|
|
|
2023-05-26 10:21:23 +03:00
|
|
|
assert(ctx->pc_save != -1);
|
|
|
|
if (tb_cflags(ctx->base.tb) & CF_PCREL) {
|
|
|
|
tcg_gen_addi_tl(target, cpu_pc, dest - ctx->pc_save);
|
|
|
|
if (get_xl(ctx) == MXL_RV32) {
|
|
|
|
tcg_gen_ext32s_tl(target, target);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (get_xl(ctx) == MXL_RV32) {
|
|
|
|
dest = (int32_t)dest;
|
|
|
|
}
|
|
|
|
tcg_gen_movi_tl(target, dest);
|
2022-01-20 15:20:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 10:21:21 +03:00
|
|
|
static void gen_update_pc(DisasContext *ctx, target_long diff)
|
2022-01-20 15:20:31 +03:00
|
|
|
{
|
2023-05-26 10:21:22 +03:00
|
|
|
gen_pc_plus_diff(cpu_pc, ctx, diff);
|
2023-05-26 10:21:23 +03:00
|
|
|
ctx->pc_save = ctx->base.pc_next + diff;
|
2022-01-20 15:20:31 +03:00
|
|
|
}
|
|
|
|
|
2018-03-02 15:31:11 +03:00
|
|
|
static void generate_exception(DisasContext *ctx, int excp)
|
|
|
|
{
|
2023-05-26 10:21:21 +03:00
|
|
|
gen_update_pc(ctx, 0);
|
2023-09-14 02:37:36 +03:00
|
|
|
gen_helper_raise_exception(tcg_env, tcg_constant_i32(excp));
|
2018-02-14 02:28:36 +03:00
|
|
|
ctx->base.is_jmp = DISAS_NORETURN;
|
2018-03-02 15:31:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gen_exception_illegal(DisasContext *ctx)
|
|
|
|
{
|
2023-09-14 02:37:36 +03:00
|
|
|
tcg_gen_st_i32(tcg_constant_i32(ctx->opcode), tcg_env,
|
2022-06-05 02:10:02 +03:00
|
|
|
offsetof(CPURISCVState, bins));
|
2022-10-16 15:47:24 +03:00
|
|
|
if (ctx->virt_inst_excp) {
|
|
|
|
generate_exception(ctx, RISCV_EXCP_VIRT_INSTRUCTION_FAULT);
|
|
|
|
} else {
|
|
|
|
generate_exception(ctx, RISCV_EXCP_ILLEGAL_INST);
|
|
|
|
}
|
2018-03-02 15:31:11 +03:00
|
|
|
}
|
|
|
|
|
2023-05-26 10:21:18 +03:00
|
|
|
static void gen_exception_inst_addr_mis(DisasContext *ctx, TCGv target)
|
2018-03-02 15:31:11 +03:00
|
|
|
{
|
2023-09-14 02:37:36 +03:00
|
|
|
tcg_gen_st_tl(target, tcg_env, offsetof(CPURISCVState, badaddr));
|
2022-06-05 02:10:03 +03:00
|
|
|
generate_exception(ctx, RISCV_EXCP_INST_ADDR_MIS);
|
2018-03-02 15:31:11 +03:00
|
|
|
}
|
|
|
|
|
2022-10-13 09:29:43 +03:00
|
|
|
static void lookup_and_goto_ptr(DisasContext *ctx)
|
|
|
|
{
|
|
|
|
#ifndef CONFIG_USER_ONLY
|
|
|
|
if (ctx->itrigger) {
|
2023-09-14 02:37:36 +03:00
|
|
|
gen_helper_itrigger_match(tcg_env);
|
2022-10-13 09:29:43 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
tcg_gen_lookup_and_goto_ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void exit_tb(DisasContext *ctx)
|
|
|
|
{
|
|
|
|
#ifndef CONFIG_USER_ONLY
|
|
|
|
if (ctx->itrigger) {
|
2023-09-14 02:37:36 +03:00
|
|
|
gen_helper_itrigger_match(tcg_env);
|
2022-10-13 09:29:43 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
tcg_gen_exit_tb(NULL, 0);
|
|
|
|
}
|
|
|
|
|
2023-05-26 10:21:20 +03:00
|
|
|
static void gen_goto_tb(DisasContext *ctx, int n, target_long diff)
|
2018-03-02 15:31:11 +03:00
|
|
|
{
|
2023-05-26 10:21:20 +03:00
|
|
|
target_ulong dest = ctx->base.pc_next + diff;
|
|
|
|
|
2022-10-13 09:29:43 +03:00
|
|
|
/*
|
|
|
|
* Under itrigger, instruction executes one by one like singlestep,
|
|
|
|
* direct block chain benefits will be small.
|
|
|
|
*/
|
|
|
|
if (translator_use_goto_tb(&ctx->base, dest) && !ctx->itrigger) {
|
2023-05-26 10:21:23 +03:00
|
|
|
/*
|
|
|
|
* For pcrel, the pc must always be up-to-date on entry to
|
|
|
|
* the linked TB, so that it can use simple additions for all
|
|
|
|
* further adjustments. For !pcrel, the linked TB is compiled
|
|
|
|
* to know its full virtual address, so we can delay the
|
|
|
|
* update to pc to the unlinked path. A long chain of links
|
|
|
|
* can thus avoid many updates to the PC.
|
|
|
|
*/
|
|
|
|
if (tb_cflags(ctx->base.tb) & CF_PCREL) {
|
|
|
|
gen_update_pc(ctx, diff);
|
|
|
|
tcg_gen_goto_tb(n);
|
|
|
|
} else {
|
|
|
|
tcg_gen_goto_tb(n);
|
|
|
|
gen_update_pc(ctx, diff);
|
|
|
|
}
|
2018-05-31 04:06:23 +03:00
|
|
|
tcg_gen_exit_tb(ctx->base.tb, n);
|
2018-03-02 15:31:11 +03:00
|
|
|
} else {
|
2023-05-26 10:21:21 +03:00
|
|
|
gen_update_pc(ctx, diff);
|
2022-10-13 09:29:43 +03:00
|
|
|
lookup_and_goto_ptr(ctx);
|
2018-03-02 15:31:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 22:55:10 +03:00
|
|
|
/*
|
|
|
|
* Wrappers for getting reg values.
|
|
|
|
*
|
|
|
|
* The $zero register does not have cpu_gpr[0] allocated -- we supply the
|
|
|
|
* constant zero as a source, and an uninitialized sink as destination.
|
|
|
|
*
|
|
|
|
* Further, we may provide an extension for word operations.
|
2018-03-02 15:31:11 +03:00
|
|
|
*/
|
2021-08-23 22:55:10 +03:00
|
|
|
static TCGv get_gpr(DisasContext *ctx, int reg_num, DisasExtend ext)
|
2018-03-02 15:31:11 +03:00
|
|
|
{
|
2021-08-23 22:55:10 +03:00
|
|
|
TCGv t;
|
|
|
|
|
2018-03-02 15:31:11 +03:00
|
|
|
if (reg_num == 0) {
|
2021-08-23 22:55:10 +03:00
|
|
|
return ctx->zero;
|
2018-03-02 15:31:11 +03:00
|
|
|
}
|
2021-08-23 22:55:10 +03:00
|
|
|
|
2021-10-20 06:17:03 +03:00
|
|
|
switch (get_ol(ctx)) {
|
|
|
|
case MXL_RV32:
|
|
|
|
switch (ext) {
|
|
|
|
case EXT_NONE:
|
|
|
|
break;
|
|
|
|
case EXT_SIGN:
|
2023-02-25 09:27:57 +03:00
|
|
|
t = tcg_temp_new();
|
2021-10-20 06:17:03 +03:00
|
|
|
tcg_gen_ext32s_tl(t, cpu_gpr[reg_num]);
|
|
|
|
return t;
|
|
|
|
case EXT_ZERO:
|
2023-02-25 09:27:57 +03:00
|
|
|
t = tcg_temp_new();
|
2021-10-20 06:17:03 +03:00
|
|
|
tcg_gen_ext32u_tl(t, cpu_gpr[reg_num]);
|
|
|
|
return t;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MXL_RV64:
|
2022-01-07 00:00:59 +03:00
|
|
|
case MXL_RV128:
|
2021-10-20 06:17:03 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
2021-08-23 22:55:10 +03:00
|
|
|
}
|
2021-10-20 06:17:03 +03:00
|
|
|
return cpu_gpr[reg_num];
|
2018-03-02 15:31:11 +03:00
|
|
|
}
|
|
|
|
|
2022-01-07 00:00:59 +03:00
|
|
|
static TCGv get_gprh(DisasContext *ctx, int reg_num)
|
|
|
|
{
|
|
|
|
assert(get_xl(ctx) == MXL_RV128);
|
|
|
|
if (reg_num == 0) {
|
|
|
|
return ctx->zero;
|
|
|
|
}
|
|
|
|
return cpu_gprh[reg_num];
|
|
|
|
}
|
|
|
|
|
2021-08-23 22:55:11 +03:00
|
|
|
static TCGv dest_gpr(DisasContext *ctx, int reg_num)
|
2021-08-23 22:55:10 +03:00
|
|
|
{
|
2021-10-20 06:17:03 +03:00
|
|
|
if (reg_num == 0 || get_olen(ctx) < TARGET_LONG_BITS) {
|
2023-02-25 09:27:57 +03:00
|
|
|
return tcg_temp_new();
|
2021-08-23 22:55:10 +03:00
|
|
|
}
|
|
|
|
return cpu_gpr[reg_num];
|
|
|
|
}
|
|
|
|
|
2022-01-07 00:00:59 +03:00
|
|
|
static TCGv dest_gprh(DisasContext *ctx, int reg_num)
|
|
|
|
{
|
|
|
|
if (reg_num == 0) {
|
2023-02-25 09:27:57 +03:00
|
|
|
return tcg_temp_new();
|
2022-01-07 00:00:59 +03:00
|
|
|
}
|
|
|
|
return cpu_gprh[reg_num];
|
|
|
|
}
|
|
|
|
|
2021-08-23 22:55:10 +03:00
|
|
|
static void gen_set_gpr(DisasContext *ctx, int reg_num, TCGv t)
|
2018-03-02 15:31:11 +03:00
|
|
|
{
|
2021-08-23 22:55:10 +03:00
|
|
|
if (reg_num != 0) {
|
2021-10-20 06:17:03 +03:00
|
|
|
switch (get_ol(ctx)) {
|
|
|
|
case MXL_RV32:
|
2021-08-23 22:55:10 +03:00
|
|
|
tcg_gen_ext32s_tl(cpu_gpr[reg_num], t);
|
2021-10-20 06:17:03 +03:00
|
|
|
break;
|
|
|
|
case MXL_RV64:
|
2022-01-07 00:00:59 +03:00
|
|
|
case MXL_RV128:
|
2021-08-23 22:55:10 +03:00
|
|
|
tcg_gen_mov_tl(cpu_gpr[reg_num], t);
|
2021-10-20 06:17:03 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
2021-08-23 22:55:10 +03:00
|
|
|
}
|
2022-01-07 00:00:59 +03:00
|
|
|
|
|
|
|
if (get_xl_max(ctx) == MXL_RV128) {
|
|
|
|
tcg_gen_sari_tl(cpu_gprh[reg_num], cpu_gpr[reg_num], 63);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 00:01:01 +03:00
|
|
|
static void gen_set_gpri(DisasContext *ctx, int reg_num, target_long imm)
|
|
|
|
{
|
|
|
|
if (reg_num != 0) {
|
|
|
|
switch (get_ol(ctx)) {
|
|
|
|
case MXL_RV32:
|
|
|
|
tcg_gen_movi_tl(cpu_gpr[reg_num], (int32_t)imm);
|
|
|
|
break;
|
|
|
|
case MXL_RV64:
|
|
|
|
case MXL_RV128:
|
|
|
|
tcg_gen_movi_tl(cpu_gpr[reg_num], imm);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_xl_max(ctx) == MXL_RV128) {
|
|
|
|
tcg_gen_movi_tl(cpu_gprh[reg_num], -(imm < 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 00:00:59 +03:00
|
|
|
static void gen_set_gpr128(DisasContext *ctx, int reg_num, TCGv rl, TCGv rh)
|
|
|
|
{
|
|
|
|
assert(get_ol(ctx) == MXL_RV128);
|
|
|
|
if (reg_num != 0) {
|
|
|
|
tcg_gen_mov_tl(cpu_gpr[reg_num], rl);
|
|
|
|
tcg_gen_mov_tl(cpu_gprh[reg_num], rh);
|
2018-03-02 15:31:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 07:39:17 +03:00
|
|
|
static TCGv_i64 get_fpr_hs(DisasContext *ctx, int reg_num)
|
|
|
|
{
|
|
|
|
if (!ctx->cfg_ptr->ext_zfinx) {
|
|
|
|
return cpu_fpr[reg_num];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reg_num == 0) {
|
|
|
|
return tcg_constant_i64(0);
|
|
|
|
}
|
|
|
|
switch (get_xl(ctx)) {
|
|
|
|
case MXL_RV32:
|
|
|
|
#ifdef TARGET_RISCV32
|
|
|
|
{
|
2023-02-25 09:25:02 +03:00
|
|
|
TCGv_i64 t = tcg_temp_new_i64();
|
2022-02-11 07:39:17 +03:00
|
|
|
tcg_gen_ext_i32_i64(t, cpu_gpr[reg_num]);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* fall through */
|
|
|
|
case MXL_RV64:
|
|
|
|
return cpu_gpr[reg_num];
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 07:39:18 +03:00
|
|
|
static TCGv_i64 get_fpr_d(DisasContext *ctx, int reg_num)
|
|
|
|
{
|
|
|
|
if (!ctx->cfg_ptr->ext_zfinx) {
|
|
|
|
return cpu_fpr[reg_num];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reg_num == 0) {
|
|
|
|
return tcg_constant_i64(0);
|
|
|
|
}
|
|
|
|
switch (get_xl(ctx)) {
|
|
|
|
case MXL_RV32:
|
|
|
|
{
|
2023-02-25 09:25:02 +03:00
|
|
|
TCGv_i64 t = tcg_temp_new_i64();
|
2022-02-11 07:39:18 +03:00
|
|
|
tcg_gen_concat_tl_i64(t, cpu_gpr[reg_num], cpu_gpr[reg_num + 1]);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
#ifdef TARGET_RISCV64
|
|
|
|
case MXL_RV64:
|
|
|
|
return cpu_gpr[reg_num];
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 07:39:17 +03:00
|
|
|
static TCGv_i64 dest_fpr(DisasContext *ctx, int reg_num)
|
|
|
|
{
|
|
|
|
if (!ctx->cfg_ptr->ext_zfinx) {
|
|
|
|
return cpu_fpr[reg_num];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reg_num == 0) {
|
2023-02-25 09:25:02 +03:00
|
|
|
return tcg_temp_new_i64();
|
2022-02-11 07:39:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (get_xl(ctx)) {
|
|
|
|
case MXL_RV32:
|
2023-02-25 09:25:02 +03:00
|
|
|
return tcg_temp_new_i64();
|
2022-02-11 07:39:17 +03:00
|
|
|
#ifdef TARGET_RISCV64
|
|
|
|
case MXL_RV64:
|
|
|
|
return cpu_gpr[reg_num];
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 11:58:12 +03:00
|
|
|
/* assume it is nanboxing (for normal) or sign-extended (for zfinx) */
|
2022-02-11 07:39:17 +03:00
|
|
|
static void gen_set_fpr_hs(DisasContext *ctx, int reg_num, TCGv_i64 t)
|
|
|
|
{
|
|
|
|
if (!ctx->cfg_ptr->ext_zfinx) {
|
|
|
|
tcg_gen_mov_i64(cpu_fpr[reg_num], t);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (reg_num != 0) {
|
|
|
|
switch (get_xl(ctx)) {
|
|
|
|
case MXL_RV32:
|
|
|
|
#ifdef TARGET_RISCV32
|
|
|
|
tcg_gen_extrl_i64_i32(cpu_gpr[reg_num], t);
|
|
|
|
break;
|
|
|
|
#else
|
|
|
|
/* fall through */
|
|
|
|
case MXL_RV64:
|
|
|
|
tcg_gen_mov_i64(cpu_gpr[reg_num], t);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 07:39:18 +03:00
|
|
|
static void gen_set_fpr_d(DisasContext *ctx, int reg_num, TCGv_i64 t)
|
|
|
|
{
|
|
|
|
if (!ctx->cfg_ptr->ext_zfinx) {
|
|
|
|
tcg_gen_mov_i64(cpu_fpr[reg_num], t);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reg_num != 0) {
|
|
|
|
switch (get_xl(ctx)) {
|
|
|
|
case MXL_RV32:
|
|
|
|
#ifdef TARGET_RISCV32
|
|
|
|
tcg_gen_extr_i64_i32(cpu_gpr[reg_num], cpu_gpr[reg_num + 1], t);
|
|
|
|
break;
|
|
|
|
#else
|
|
|
|
tcg_gen_ext32s_i64(cpu_gpr[reg_num], t);
|
|
|
|
tcg_gen_sari_i64(cpu_gpr[reg_num + 1], t, 32);
|
|
|
|
break;
|
|
|
|
case MXL_RV64:
|
|
|
|
tcg_gen_mov_i64(cpu_gpr[reg_num], t);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 02:58:42 +03:00
|
|
|
static void gen_jal(DisasContext *ctx, int rd, target_ulong imm)
|
2018-03-02 15:31:11 +03:00
|
|
|
{
|
2023-05-26 10:21:23 +03:00
|
|
|
TCGv succ_pc = dest_gpr(ctx, rd);
|
|
|
|
|
2018-03-02 15:31:11 +03:00
|
|
|
/* check misaligned: */
|
2023-05-17 16:57:09 +03:00
|
|
|
if (!has_ext(ctx, RVC) && !ctx->cfg_ptr->ext_zca) {
|
2023-05-26 10:21:22 +03:00
|
|
|
if ((imm & 0x3) != 0) {
|
2023-05-26 10:21:18 +03:00
|
|
|
TCGv target_pc = tcg_temp_new();
|
2023-05-26 10:21:22 +03:00
|
|
|
gen_pc_plus_diff(target_pc, ctx, imm);
|
2023-05-26 10:21:18 +03:00
|
|
|
gen_exception_inst_addr_mis(ctx, target_pc);
|
2018-03-02 15:31:11 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 10:21:23 +03:00
|
|
|
gen_pc_plus_diff(succ_pc, ctx, ctx->cur_insn_len);
|
|
|
|
gen_set_gpr(ctx, rd, succ_pc);
|
|
|
|
|
2023-05-26 10:21:20 +03:00
|
|
|
gen_goto_tb(ctx, 0, imm); /* must use this for safety */
|
2018-02-14 02:28:36 +03:00
|
|
|
ctx->base.is_jmp = DISAS_NORETURN;
|
2018-03-02 15:31:11 +03:00
|
|
|
}
|
|
|
|
|
2022-01-20 15:20:40 +03:00
|
|
|
/* Compute a canonical address from a register plus offset. */
|
|
|
|
static TCGv get_address(DisasContext *ctx, int rs1, int imm)
|
2021-10-25 20:36:07 +03:00
|
|
|
{
|
2023-02-25 09:27:57 +03:00
|
|
|
TCGv addr = tcg_temp_new();
|
2022-01-20 15:20:40 +03:00
|
|
|
TCGv src1 = get_gpr(ctx, rs1, EXT_NONE);
|
|
|
|
|
|
|
|
tcg_gen_addi_tl(addr, src1, imm);
|
2022-01-20 15:20:41 +03:00
|
|
|
if (ctx->pm_mask_enabled) {
|
2022-07-17 13:15:43 +03:00
|
|
|
tcg_gen_andc_tl(addr, addr, pm_mask);
|
2023-06-14 06:25:46 +03:00
|
|
|
} else if (get_address_xl(ctx) == MXL_RV32) {
|
2022-01-20 15:20:40 +03:00
|
|
|
tcg_gen_ext32u_tl(addr, addr);
|
2021-10-25 20:36:08 +03:00
|
|
|
}
|
2022-01-20 15:20:41 +03:00
|
|
|
if (ctx->pm_base_enabled) {
|
|
|
|
tcg_gen_or_tl(addr, addr, pm_base);
|
|
|
|
}
|
2023-06-14 06:25:46 +03:00
|
|
|
|
2022-01-20 15:20:40 +03:00
|
|
|
return addr;
|
2021-10-25 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
2023-01-31 23:20:08 +03:00
|
|
|
/* Compute a canonical address from a register plus reg offset. */
|
|
|
|
static TCGv get_address_indexed(DisasContext *ctx, int rs1, TCGv offs)
|
|
|
|
{
|
2023-02-25 09:27:57 +03:00
|
|
|
TCGv addr = tcg_temp_new();
|
2023-01-31 23:20:08 +03:00
|
|
|
TCGv src1 = get_gpr(ctx, rs1, EXT_NONE);
|
|
|
|
|
|
|
|
tcg_gen_add_tl(addr, src1, offs);
|
|
|
|
if (ctx->pm_mask_enabled) {
|
|
|
|
tcg_gen_andc_tl(addr, addr, pm_mask);
|
|
|
|
} else if (get_xl(ctx) == MXL_RV32) {
|
|
|
|
tcg_gen_ext32u_tl(addr, addr);
|
|
|
|
}
|
|
|
|
if (ctx->pm_base_enabled) {
|
|
|
|
tcg_gen_or_tl(addr, addr, pm_base);
|
|
|
|
}
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2019-01-15 02:57:59 +03:00
|
|
|
#ifndef CONFIG_USER_ONLY
|
2023-04-05 11:58:12 +03:00
|
|
|
/*
|
2019-01-15 02:57:59 +03:00
|
|
|
* We will have already diagnosed disabled state,
|
|
|
|
* and need to turn initial/clean into dirty.
|
|
|
|
*/
|
|
|
|
static void mark_fs_dirty(DisasContext *ctx)
|
|
|
|
{
|
|
|
|
TCGv tmp;
|
2021-04-24 06:33:18 +03:00
|
|
|
|
2022-02-11 07:39:16 +03:00
|
|
|
if (!has_ext(ctx, RVF)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:43:11 +03:00
|
|
|
if (ctx->mstatus_fs != EXT_STATUS_DIRTY) {
|
2021-09-21 05:02:33 +03:00
|
|
|
/* Remember the state change for the rest of the TB. */
|
2023-04-12 14:43:11 +03:00
|
|
|
ctx->mstatus_fs = EXT_STATUS_DIRTY;
|
2019-01-15 02:57:59 +03:00
|
|
|
|
2021-09-21 05:02:33 +03:00
|
|
|
tmp = tcg_temp_new();
|
2023-09-14 02:37:36 +03:00
|
|
|
tcg_gen_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus));
|
2021-10-20 06:17:09 +03:00
|
|
|
tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS);
|
2023-09-14 02:37:36 +03:00
|
|
|
tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus));
|
2020-02-01 04:02:46 +03:00
|
|
|
|
2023-04-12 14:43:12 +03:00
|
|
|
if (ctx->virt_enabled) {
|
2023-09-14 02:37:36 +03:00
|
|
|
tcg_gen_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs));
|
2023-04-12 14:43:12 +03:00
|
|
|
tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS);
|
2023-09-14 02:37:36 +03:00
|
|
|
tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs));
|
2023-04-12 14:43:12 +03:00
|
|
|
}
|
2020-02-01 04:02:46 +03:00
|
|
|
}
|
2019-01-15 02:57:59 +03:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline void mark_fs_dirty(DisasContext *ctx) { }
|
|
|
|
#endif
|
|
|
|
|
2021-12-10 10:55:53 +03:00
|
|
|
#ifndef CONFIG_USER_ONLY
|
2023-04-05 11:58:12 +03:00
|
|
|
/*
|
2021-12-10 10:55:53 +03:00
|
|
|
* We will have already diagnosed disabled state,
|
|
|
|
* and need to turn initial/clean into dirty.
|
|
|
|
*/
|
|
|
|
static void mark_vs_dirty(DisasContext *ctx)
|
|
|
|
{
|
|
|
|
TCGv tmp;
|
|
|
|
|
2023-04-12 14:43:11 +03:00
|
|
|
if (ctx->mstatus_vs != EXT_STATUS_DIRTY) {
|
2021-12-10 10:55:53 +03:00
|
|
|
/* Remember the state change for the rest of the TB. */
|
2023-04-12 14:43:11 +03:00
|
|
|
ctx->mstatus_vs = EXT_STATUS_DIRTY;
|
2021-12-10 10:55:53 +03:00
|
|
|
|
|
|
|
tmp = tcg_temp_new();
|
2023-09-14 02:37:36 +03:00
|
|
|
tcg_gen_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus));
|
2021-12-10 10:55:53 +03:00
|
|
|
tcg_gen_ori_tl(tmp, tmp, MSTATUS_VS);
|
2023-09-14 02:37:36 +03:00
|
|
|
tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus));
|
2021-12-10 10:55:53 +03:00
|
|
|
|
2023-04-12 14:43:12 +03:00
|
|
|
if (ctx->virt_enabled) {
|
2023-09-14 02:37:36 +03:00
|
|
|
tcg_gen_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs));
|
2023-04-12 14:43:12 +03:00
|
|
|
tcg_gen_ori_tl(tmp, tmp, MSTATUS_VS);
|
2023-09-14 02:37:36 +03:00
|
|
|
tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs));
|
2023-04-12 14:43:12 +03:00
|
|
|
}
|
2021-12-10 10:55:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline void mark_vs_dirty(DisasContext *ctx) { }
|
|
|
|
#endif
|
|
|
|
|
2024-03-14 20:57:03 +03:00
|
|
|
static void finalize_rvv_inst(DisasContext *ctx)
|
|
|
|
{
|
|
|
|
mark_vs_dirty(ctx);
|
|
|
|
ctx->vstart_eq_zero = true;
|
|
|
|
}
|
|
|
|
|
2018-03-02 15:31:11 +03:00
|
|
|
static void gen_set_rm(DisasContext *ctx, int rm)
|
|
|
|
{
|
|
|
|
if (ctx->frm == rm) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ctx->frm = rm;
|
2021-12-10 10:56:49 +03:00
|
|
|
|
2023-01-15 19:06:56 +03:00
|
|
|
if (rm == RISCV_FRM_DYN) {
|
|
|
|
/* The helper will return only if frm valid. */
|
|
|
|
ctx->frm_valid = true;
|
|
|
|
}
|
2021-12-10 10:56:49 +03:00
|
|
|
|
2022-06-05 02:10:04 +03:00
|
|
|
/* The helper may raise ILLEGAL_INSN -- record binv for unwind. */
|
|
|
|
decode_save_opc(ctx);
|
2023-09-14 02:37:36 +03:00
|
|
|
gen_helper_set_rounding_mode(tcg_env, tcg_constant_i32(rm));
|
2018-03-02 15:31:11 +03:00
|
|
|
}
|
|
|
|
|
2023-01-15 19:06:56 +03:00
|
|
|
static void gen_set_rm_chkfrm(DisasContext *ctx, int rm)
|
|
|
|
{
|
|
|
|
if (ctx->frm == rm && ctx->frm_valid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ctx->frm = rm;
|
|
|
|
ctx->frm_valid = true;
|
|
|
|
|
|
|
|
/* The helper may raise ILLEGAL_INSN -- record binv for unwind. */
|
|
|
|
decode_save_opc(ctx);
|
2023-09-14 02:37:36 +03:00
|
|
|
gen_helper_set_rounding_mode_chkfrm(tcg_env, tcg_constant_i32(rm));
|
2023-01-15 19:06:56 +03:00
|
|
|
}
|
|
|
|
|
2020-07-01 18:24:54 +03:00
|
|
|
static int ex_plus_1(DisasContext *ctx, int nf)
|
|
|
|
{
|
|
|
|
return nf + 1;
|
|
|
|
}
|
|
|
|
|
2019-02-13 18:53:41 +03:00
|
|
|
#define EX_SH(amount) \
|
2019-03-21 05:21:31 +03:00
|
|
|
static int ex_shift_##amount(DisasContext *ctx, int imm) \
|
2019-02-13 18:53:41 +03:00
|
|
|
{ \
|
|
|
|
return imm << amount; \
|
|
|
|
}
|
2019-02-13 18:53:42 +03:00
|
|
|
EX_SH(1)
|
2019-02-13 18:53:56 +03:00
|
|
|
EX_SH(2)
|
|
|
|
EX_SH(3)
|
2019-02-13 18:53:57 +03:00
|
|
|
EX_SH(4)
|
2019-02-13 18:53:41 +03:00
|
|
|
EX_SH(12)
|
|
|
|
|
2019-02-13 18:53:48 +03:00
|
|
|
#define REQUIRE_EXT(ctx, ext) do { \
|
|
|
|
if (!has_ext(ctx, ext)) { \
|
|
|
|
return false; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2021-09-11 17:00:12 +03:00
|
|
|
|
2021-10-20 06:17:02 +03:00
|
|
|
#define REQUIRE_32BIT(ctx) do { \
|
|
|
|
if (get_xl(ctx) != MXL_RV32) { \
|
|
|
|
return false; \
|
|
|
|
} \
|
2021-09-11 17:00:12 +03:00
|
|
|
} while (0)
|
2019-02-13 18:53:48 +03:00
|
|
|
|
2022-01-07 00:00:54 +03:00
|
|
|
#define REQUIRE_64BIT(ctx) do { \
|
|
|
|
if (get_xl(ctx) != MXL_RV64) { \
|
|
|
|
return false; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define REQUIRE_128BIT(ctx) do { \
|
|
|
|
if (get_xl(ctx) != MXL_RV128) { \
|
|
|
|
return false; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define REQUIRE_64_OR_128BIT(ctx) do { \
|
|
|
|
if (get_xl(ctx) == MXL_RV32) { \
|
|
|
|
return false; \
|
|
|
|
} \
|
2021-04-24 06:34:12 +03:00
|
|
|
} while (0)
|
|
|
|
|
2022-04-23 05:34:58 +03:00
|
|
|
#define REQUIRE_EITHER_EXT(ctx, A, B) do { \
|
2023-04-05 11:58:11 +03:00
|
|
|
if (!ctx->cfg_ptr->ext_##A && \
|
|
|
|
!ctx->cfg_ptr->ext_##B) { \
|
2022-04-23 05:34:58 +03:00
|
|
|
return false; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2019-03-21 05:21:31 +03:00
|
|
|
static int ex_rvc_register(DisasContext *ctx, int reg)
|
2019-02-13 18:53:56 +03:00
|
|
|
{
|
|
|
|
return 8 + reg;
|
|
|
|
}
|
|
|
|
|
2023-03-07 11:13:59 +03:00
|
|
|
static int ex_sreg_register(DisasContext *ctx, int reg)
|
|
|
|
{
|
|
|
|
return reg < 2 ? reg + 8 : reg + 16;
|
|
|
|
}
|
|
|
|
|
2022-07-10 14:04:51 +03:00
|
|
|
static int ex_rvc_shiftli(DisasContext *ctx, int imm)
|
2019-04-01 06:11:51 +03:00
|
|
|
{
|
|
|
|
/* For RV128 a shamt of 0 means a shift by 64. */
|
2022-07-10 14:04:51 +03:00
|
|
|
if (get_ol(ctx) == MXL_RV128) {
|
|
|
|
imm = imm ? imm : 64;
|
|
|
|
}
|
|
|
|
return imm;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ex_rvc_shiftri(DisasContext *ctx, int imm)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* For RV128 a shamt of 0 means a shift by 64, furthermore, for right
|
|
|
|
* shifts, the shamt is sign-extended.
|
|
|
|
*/
|
|
|
|
if (get_ol(ctx) == MXL_RV128) {
|
|
|
|
imm = imm | (imm & 32) << 1;
|
|
|
|
imm = imm ? imm : 64;
|
|
|
|
}
|
|
|
|
return imm;
|
2019-04-01 06:11:51 +03:00
|
|
|
}
|
|
|
|
|
2019-02-13 18:53:41 +03:00
|
|
|
/* Include the auto-generated decoder for 32 bit insn */
|
2020-08-07 13:10:23 +03:00
|
|
|
#include "decode-insn32.c.inc"
|
2019-02-13 18:54:03 +03:00
|
|
|
|
2022-01-07 00:00:55 +03:00
|
|
|
static bool gen_logic_imm_fn(DisasContext *ctx, arg_i *a,
|
|
|
|
void (*func)(TCGv, TCGv, target_long))
|
|
|
|
{
|
|
|
|
TCGv dest = dest_gpr(ctx, a->rd);
|
|
|
|
TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE);
|
|
|
|
|
|
|
|
func(dest, src1, a->imm);
|
|
|
|
|
2022-01-07 00:01:00 +03:00
|
|
|
if (get_xl(ctx) == MXL_RV128) {
|
|
|
|
TCGv src1h = get_gprh(ctx, a->rs1);
|
|
|
|
TCGv desth = dest_gprh(ctx, a->rd);
|
|
|
|
|
|
|
|
func(desth, src1h, -(a->imm < 0));
|
|
|
|
gen_set_gpr128(ctx, a->rd, dest, desth);
|
|
|
|
} else {
|
|
|
|
gen_set_gpr(ctx, a->rd, dest);
|
|
|
|
}
|
2022-01-07 00:00:55 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool gen_logic(DisasContext *ctx, arg_r *a,
|
|
|
|
void (*func)(TCGv, TCGv, TCGv))
|
|
|
|
{
|
|
|
|
TCGv dest = dest_gpr(ctx, a->rd);
|
|
|
|
TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE);
|
|
|
|
TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE);
|
|
|
|
|
|
|
|
func(dest, src1, src2);
|
|
|
|
|
2022-01-07 00:01:00 +03:00
|
|
|
if (get_xl(ctx) == MXL_RV128) {
|
|
|
|
TCGv src1h = get_gprh(ctx, a->rs1);
|
|
|
|
TCGv src2h = get_gprh(ctx, a->rs2);
|
|
|
|
TCGv desth = dest_gprh(ctx, a->rd);
|
|
|
|
|
|
|
|
func(desth, src1h, src2h);
|
|
|
|
gen_set_gpr128(ctx, a->rd, dest, desth);
|
|
|
|
} else {
|
|
|
|
gen_set_gpr(ctx, a->rd, dest);
|
|
|
|
}
|
2022-01-07 00:00:55 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-23 22:55:11 +03:00
|
|
|
static bool gen_arith_imm_fn(DisasContext *ctx, arg_i *a, DisasExtend ext,
|
2022-01-07 00:01:03 +03:00
|
|
|
void (*func)(TCGv, TCGv, target_long),
|
|
|
|
void (*f128)(TCGv, TCGv, TCGv, TCGv, target_long))
|
2019-04-01 06:11:54 +03:00
|
|
|
{
|
2021-08-23 22:55:11 +03:00
|
|
|
TCGv dest = dest_gpr(ctx, a->rd);
|
|
|
|
TCGv src1 = get_gpr(ctx, a->rs1, ext);
|
2019-04-01 06:11:54 +03:00
|
|
|
|
2022-01-07 00:01:03 +03:00
|
|
|
if (get_ol(ctx) < MXL_RV128) {
|
|
|
|
func(dest, src1, a->imm);
|
|
|
|
gen_set_gpr(ctx, a->rd, dest);
|
|
|
|
} else {
|
|
|
|
if (f128 == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
TCGv src1h = get_gprh(ctx, a->rs1);
|
|
|
|
TCGv desth = dest_gprh(ctx, a->rd);
|
2019-04-01 06:11:54 +03:00
|
|
|
|
2022-01-07 00:01:03 +03:00
|
|
|
f128(dest, desth, src1, src1h, a->imm);
|
|
|
|
gen_set_gpr128(ctx, a->rd, dest, desth);
|
|
|
|
}
|
2019-04-01 06:11:54 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-23 22:55:11 +03:00
|
|
|
static bool gen_arith_imm_tl(DisasContext *ctx, arg_i *a, DisasExtend ext,
|
2022-01-07 00:01:03 +03:00
|
|
|
void (*func)(TCGv, TCGv, TCGv),
|
|
|
|
void (*f128)(TCGv, TCGv, TCGv, TCGv, TCGv, TCGv))
|
2019-02-13 18:54:03 +03:00
|
|
|
{
|
2021-08-23 22:55:11 +03:00
|
|
|
TCGv dest = dest_gpr(ctx, a->rd);
|
|
|
|
TCGv src1 = get_gpr(ctx, a->rs1, ext);
|
|
|
|
TCGv src2 = tcg_constant_tl(a->imm);
|
2019-02-13 18:54:03 +03:00
|
|
|
|
2022-01-07 00:01:03 +03:00
|
|
|
if (get_ol(ctx) < MXL_RV128) {
|
|
|
|
func(dest, src1, src2);
|
|
|
|
gen_set_gpr(ctx, a->rd, dest);
|
|
|
|
} else {
|
|
|
|
if (f128 == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-13 18:54:03 +03:00
|
|
|
|
2022-01-07 00:01:03 +03:00
|
|
|
TCGv src1h = get_gprh(ctx, a->rs1);
|
|
|
|
TCGv src2h = tcg_constant_tl(-(a->imm < 0));
|
|
|
|
TCGv desth = dest_gprh(ctx, a->rd);
|
|
|
|
|
|
|
|
f128(dest, desth, src1, src1h, src2, src2h);
|
|
|
|
gen_set_gpr128(ctx, a->rd, dest, desth);
|
|
|
|
}
|
2019-02-13 18:54:03 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-23 22:55:11 +03:00
|
|
|
static bool gen_arith(DisasContext *ctx, arg_r *a, DisasExtend ext,
|
2022-01-07 00:01:03 +03:00
|
|
|
void (*func)(TCGv, TCGv, TCGv),
|
|
|
|
void (*f128)(TCGv, TCGv, TCGv, TCGv, TCGv, TCGv))
|
2019-02-13 18:54:04 +03:00
|
|
|
{
|
2021-08-23 22:55:11 +03:00
|
|
|
TCGv dest = dest_gpr(ctx, a->rd);
|
|
|
|
TCGv src1 = get_gpr(ctx, a->rs1, ext);
|
|
|
|
TCGv src2 = get_gpr(ctx, a->rs2, ext);
|
2019-02-13 18:54:04 +03:00
|
|
|
|
2022-01-07 00:01:03 +03:00
|
|
|
if (get_ol(ctx) < MXL_RV128) {
|
|
|
|
func(dest, src1, src2);
|
|
|
|
gen_set_gpr(ctx, a->rd, dest);
|
|
|
|
} else {
|
|
|
|
if (f128 == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
TCGv src1h = get_gprh(ctx, a->rs1);
|
|
|
|
TCGv src2h = get_gprh(ctx, a->rs2);
|
|
|
|
TCGv desth = dest_gprh(ctx, a->rd);
|
2019-02-13 18:54:04 +03:00
|
|
|
|
2022-01-07 00:01:03 +03:00
|
|
|
f128(dest, desth, src1, src1h, src2, src2h);
|
|
|
|
gen_set_gpr128(ctx, a->rd, dest, desth);
|
|
|
|
}
|
2019-02-13 18:54:04 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-20 06:17:04 +03:00
|
|
|
static bool gen_arith_per_ol(DisasContext *ctx, arg_r *a, DisasExtend ext,
|
|
|
|
void (*f_tl)(TCGv, TCGv, TCGv),
|
2022-01-07 00:01:03 +03:00
|
|
|
void (*f_32)(TCGv, TCGv, TCGv),
|
|
|
|
void (*f_128)(TCGv, TCGv, TCGv, TCGv, TCGv, TCGv))
|
2021-10-20 06:17:04 +03:00
|
|
|
{
|
|
|
|
int olen = get_olen(ctx);
|
|
|
|
|
|
|
|
if (olen != TARGET_LONG_BITS) {
|
|
|
|
if (olen == 32) {
|
|
|
|
f_tl = f_32;
|
2022-01-07 00:01:03 +03:00
|
|
|
} else if (olen != 128) {
|
2021-10-20 06:17:04 +03:00
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
}
|
2022-01-07 00:01:03 +03:00
|
|
|
return gen_arith(ctx, a, ext, f_tl, f_128);
|
2021-10-20 06:17:04 +03:00
|
|
|
}
|
|
|
|
|
2021-08-23 22:55:17 +03:00
|
|
|
static bool gen_shift_imm_fn(DisasContext *ctx, arg_shift *a, DisasExtend ext,
|
2022-01-07 00:01:02 +03:00
|
|
|
void (*func)(TCGv, TCGv, target_long),
|
|
|
|
void (*f128)(TCGv, TCGv, TCGv, TCGv, target_long))
|
2021-01-09 01:42:52 +03:00
|
|
|
{
|
2021-08-23 22:55:17 +03:00
|
|
|
TCGv dest, src1;
|
2021-10-20 06:17:03 +03:00
|
|
|
int max_len = get_olen(ctx);
|
2021-01-09 01:42:52 +03:00
|
|
|
|
2021-08-23 22:55:17 +03:00
|
|
|
if (a->shamt >= max_len) {
|
2021-05-05 19:06:09 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-23 22:55:17 +03:00
|
|
|
dest = dest_gpr(ctx, a->rd);
|
|
|
|
src1 = get_gpr(ctx, a->rs1, ext);
|
2021-05-05 19:06:09 +03:00
|
|
|
|
2022-01-07 00:01:02 +03:00
|
|
|
if (max_len < 128) {
|
|
|
|
func(dest, src1, a->shamt);
|
|
|
|
gen_set_gpr(ctx, a->rd, dest);
|
|
|
|
} else {
|
|
|
|
TCGv src1h = get_gprh(ctx, a->rs1);
|
|
|
|
TCGv desth = dest_gprh(ctx, a->rd);
|
2021-05-05 19:06:09 +03:00
|
|
|
|
2022-01-07 00:01:02 +03:00
|
|
|
if (f128 == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
f128(dest, desth, src1, src1h, a->shamt);
|
|
|
|
gen_set_gpr128(ctx, a->rd, dest, desth);
|
|
|
|
}
|
2021-05-05 19:06:09 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-20 06:17:07 +03:00
|
|
|
static bool gen_shift_imm_fn_per_ol(DisasContext *ctx, arg_shift *a,
|
|
|
|
DisasExtend ext,
|
|
|
|
void (*f_tl)(TCGv, TCGv, target_long),
|
2022-01-07 00:01:02 +03:00
|
|
|
void (*f_32)(TCGv, TCGv, target_long),
|
|
|
|
void (*f_128)(TCGv, TCGv, TCGv, TCGv,
|
|
|
|
target_long))
|
2021-10-20 06:17:07 +03:00
|
|
|
{
|
|
|
|
int olen = get_olen(ctx);
|
|
|
|
if (olen != TARGET_LONG_BITS) {
|
|
|
|
if (olen == 32) {
|
|
|
|
f_tl = f_32;
|
2022-01-07 00:01:02 +03:00
|
|
|
} else if (olen != 128) {
|
2021-10-20 06:17:07 +03:00
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
}
|
2022-01-07 00:01:02 +03:00
|
|
|
return gen_shift_imm_fn(ctx, a, ext, f_tl, f_128);
|
2021-10-20 06:17:07 +03:00
|
|
|
}
|
|
|
|
|
2021-08-23 22:55:17 +03:00
|
|
|
static bool gen_shift_imm_tl(DisasContext *ctx, arg_shift *a, DisasExtend ext,
|
|
|
|
void (*func)(TCGv, TCGv, TCGv))
|
2021-05-05 19:06:10 +03:00
|
|
|
{
|
2021-08-23 22:55:17 +03:00
|
|
|
TCGv dest, src1, src2;
|
2021-10-20 06:17:03 +03:00
|
|
|
int max_len = get_olen(ctx);
|
2021-08-23 22:55:17 +03:00
|
|
|
|
|
|
|
if (a->shamt >= max_len) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-05 19:06:10 +03:00
|
|
|
|
2021-08-23 22:55:17 +03:00
|
|
|
dest = dest_gpr(ctx, a->rd);
|
|
|
|
src1 = get_gpr(ctx, a->rs1, ext);
|
|
|
|
src2 = tcg_constant_tl(a->shamt);
|
2021-05-05 19:06:10 +03:00
|
|
|
|
2021-08-23 22:55:17 +03:00
|
|
|
func(dest, src1, src2);
|
2021-05-05 19:06:10 +03:00
|
|
|
|
2021-08-23 22:55:17 +03:00
|
|
|
gen_set_gpr(ctx, a->rd, dest);
|
2021-05-05 19:06:10 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-23 22:55:17 +03:00
|
|
|
static bool gen_shift(DisasContext *ctx, arg_r *a, DisasExtend ext,
|
2022-01-07 00:01:02 +03:00
|
|
|
void (*func)(TCGv, TCGv, TCGv),
|
|
|
|
void (*f128)(TCGv, TCGv, TCGv, TCGv, TCGv))
|
2021-05-05 19:06:09 +03:00
|
|
|
{
|
2021-08-23 22:55:17 +03:00
|
|
|
TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE);
|
|
|
|
TCGv ext2 = tcg_temp_new();
|
2022-01-07 00:01:02 +03:00
|
|
|
int max_len = get_olen(ctx);
|
2021-05-05 19:06:09 +03:00
|
|
|
|
2022-01-07 00:01:02 +03:00
|
|
|
tcg_gen_andi_tl(ext2, src2, max_len - 1);
|
2021-05-05 19:06:09 +03:00
|
|
|
|
2022-01-07 00:01:02 +03:00
|
|
|
TCGv dest = dest_gpr(ctx, a->rd);
|
|
|
|
TCGv src1 = get_gpr(ctx, a->rs1, ext);
|
|
|
|
|
|
|
|
if (max_len < 128) {
|
|
|
|
func(dest, src1, ext2);
|
|
|
|
gen_set_gpr(ctx, a->rd, dest);
|
|
|
|
} else {
|
|
|
|
TCGv src1h = get_gprh(ctx, a->rs1);
|
|
|
|
TCGv desth = dest_gprh(ctx, a->rd);
|
|
|
|
|
|
|
|
if (f128 == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
f128(dest, desth, src1, src1h, ext2);
|
|
|
|
gen_set_gpr128(ctx, a->rd, dest, desth);
|
|
|
|
}
|
2021-05-05 19:06:09 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-20 06:17:07 +03:00
|
|
|
static bool gen_shift_per_ol(DisasContext *ctx, arg_r *a, DisasExtend ext,
|
|
|
|
void (*f_tl)(TCGv, TCGv, TCGv),
|
2022-01-07 00:01:02 +03:00
|
|
|
void (*f_32)(TCGv, TCGv, TCGv),
|
|
|
|
void (*f_128)(TCGv, TCGv, TCGv, TCGv, TCGv))
|
2021-10-20 06:17:07 +03:00
|
|
|
{
|
|
|
|
int olen = get_olen(ctx);
|
|
|
|
if (olen != TARGET_LONG_BITS) {
|
|
|
|
if (olen == 32) {
|
|
|
|
f_tl = f_32;
|
2022-01-07 00:01:02 +03:00
|
|
|
} else if (olen != 128) {
|
2021-10-20 06:17:07 +03:00
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
}
|
2022-01-07 00:01:02 +03:00
|
|
|
return gen_shift(ctx, a, ext, f_tl, f_128);
|
2021-10-20 06:17:07 +03:00
|
|
|
}
|
|
|
|
|
2021-08-23 22:55:16 +03:00
|
|
|
static bool gen_unary(DisasContext *ctx, arg_r2 *a, DisasExtend ext,
|
|
|
|
void (*func)(TCGv, TCGv))
|
2021-05-05 19:06:03 +03:00
|
|
|
{
|
2021-08-23 22:55:16 +03:00
|
|
|
TCGv dest = dest_gpr(ctx, a->rd);
|
|
|
|
TCGv src1 = get_gpr(ctx, a->rs1, ext);
|
2021-05-05 19:06:03 +03:00
|
|
|
|
2021-08-23 22:55:16 +03:00
|
|
|
func(dest, src1);
|
2021-05-05 19:06:03 +03:00
|
|
|
|
2021-08-23 22:55:16 +03:00
|
|
|
gen_set_gpr(ctx, a->rd, dest);
|
2021-05-05 19:06:03 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-20 06:17:06 +03:00
|
|
|
static bool gen_unary_per_ol(DisasContext *ctx, arg_r2 *a, DisasExtend ext,
|
|
|
|
void (*f_tl)(TCGv, TCGv),
|
|
|
|
void (*f_32)(TCGv, TCGv))
|
|
|
|
{
|
|
|
|
int olen = get_olen(ctx);
|
|
|
|
|
|
|
|
if (olen != TARGET_LONG_BITS) {
|
|
|
|
if (olen == 32) {
|
|
|
|
f_tl = f_32;
|
|
|
|
} else {
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return gen_unary(ctx, a, ext, f_tl);
|
|
|
|
}
|
|
|
|
|
2024-07-09 14:36:47 +03:00
|
|
|
static bool gen_amo(DisasContext *ctx, arg_atomic *a,
|
|
|
|
void(*func)(TCGv, TCGv, TCGv, TCGArg, MemOp),
|
|
|
|
MemOp mop)
|
|
|
|
{
|
|
|
|
TCGv dest = dest_gpr(ctx, a->rd);
|
|
|
|
TCGv src1, src2 = get_gpr(ctx, a->rs2, EXT_NONE);
|
2024-07-09 14:36:48 +03:00
|
|
|
MemOp size = mop & MO_SIZE;
|
2024-07-09 14:36:47 +03:00
|
|
|
|
2024-07-09 14:36:48 +03:00
|
|
|
if (ctx->cfg_ptr->ext_zama16b && size >= MO_32) {
|
2024-07-09 14:36:47 +03:00
|
|
|
mop |= MO_ATOM_WITHIN16;
|
|
|
|
} else {
|
|
|
|
mop |= MO_ALIGN;
|
|
|
|
}
|
|
|
|
|
|
|
|
decode_save_opc(ctx);
|
|
|
|
src1 = get_address(ctx, a->rs1, 0);
|
|
|
|
func(dest, src1, src2, ctx->mem_idx, mop);
|
|
|
|
|
|
|
|
gen_set_gpr(ctx, a->rd, dest);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-07-09 14:36:49 +03:00
|
|
|
static bool gen_cmpxchg(DisasContext *ctx, arg_atomic *a, MemOp mop)
|
|
|
|
{
|
|
|
|
TCGv dest = get_gpr(ctx, a->rd, EXT_NONE);
|
|
|
|
TCGv src1 = get_address(ctx, a->rs1, 0);
|
|
|
|
TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE);
|
|
|
|
|
|
|
|
decode_save_opc(ctx);
|
|
|
|
tcg_gen_atomic_cmpxchg_tl(dest, src1, dest, src2, ctx->mem_idx, mop);
|
|
|
|
|
|
|
|
gen_set_gpr(ctx, a->rd, dest);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-23 22:55:17 +03:00
|
|
|
static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)
|
|
|
|
{
|
|
|
|
DisasContext *ctx = container_of(dcbase, DisasContext, base);
|
|
|
|
CPUState *cpu = ctx->cs;
|
2023-09-14 03:22:49 +03:00
|
|
|
CPURISCVState *env = cpu_env(cpu);
|
2021-08-23 22:55:17 +03:00
|
|
|
|
2024-04-05 12:22:27 +03:00
|
|
|
return translator_ldl(env, &ctx->base, pc);
|
2021-08-23 22:55:17 +03:00
|
|
|
}
|
|
|
|
|
2019-02-13 18:53:41 +03:00
|
|
|
/* Include insn module translation function */
|
2020-02-04 14:41:01 +03:00
|
|
|
#include "insn_trans/trans_rvi.c.inc"
|
|
|
|
#include "insn_trans/trans_rvm.c.inc"
|
|
|
|
#include "insn_trans/trans_rva.c.inc"
|
|
|
|
#include "insn_trans/trans_rvf.c.inc"
|
|
|
|
#include "insn_trans/trans_rvd.c.inc"
|
|
|
|
#include "insn_trans/trans_rvh.c.inc"
|
|
|
|
#include "insn_trans/trans_rvv.c.inc"
|
2021-05-05 19:06:03 +03:00
|
|
|
#include "insn_trans/trans_rvb.c.inc"
|
2023-02-21 12:10:09 +03:00
|
|
|
#include "insn_trans/trans_rvzicond.c.inc"
|
2023-12-07 18:32:30 +03:00
|
|
|
#include "insn_trans/trans_rvzacas.c.inc"
|
2024-07-09 14:36:48 +03:00
|
|
|
#include "insn_trans/trans_rvzabha.c.inc"
|
2022-10-05 17:49:48 +03:00
|
|
|
#include "insn_trans/trans_rvzawrs.c.inc"
|
2023-02-24 16:25:34 +03:00
|
|
|
#include "insn_trans/trans_rvzicbo.c.inc"
|
2024-07-09 14:36:42 +03:00
|
|
|
#include "insn_trans/trans_rvzimop.c.inc"
|
2023-07-10 10:12:43 +03:00
|
|
|
#include "insn_trans/trans_rvzfa.c.inc"
|
2021-12-10 10:43:20 +03:00
|
|
|
#include "insn_trans/trans_rvzfh.c.inc"
|
2022-04-23 05:35:02 +03:00
|
|
|
#include "insn_trans/trans_rvk.c.inc"
|
2023-07-11 19:59:03 +03:00
|
|
|
#include "insn_trans/trans_rvvk.c.inc"
|
2020-02-04 14:41:01 +03:00
|
|
|
#include "insn_trans/trans_privileged.c.inc"
|
2022-02-04 05:26:57 +03:00
|
|
|
#include "insn_trans/trans_svinval.c.inc"
|
2023-06-15 09:32:58 +03:00
|
|
|
#include "insn_trans/trans_rvbf16.c.inc"
|
2023-01-31 23:20:00 +03:00
|
|
|
#include "decode-xthead.c.inc"
|
|
|
|
#include "insn_trans/trans_xthead.c.inc"
|
2022-02-02 03:52:48 +03:00
|
|
|
#include "insn_trans/trans_xventanacondops.c.inc"
|
2019-02-13 18:53:41 +03:00
|
|
|
|
2019-08-09 18:24:57 +03:00
|
|
|
/* Include the auto-generated decoder for 16 bit insn */
|
2020-08-07 13:10:23 +03:00
|
|
|
#include "decode-insn16.c.inc"
|
2023-03-07 11:13:58 +03:00
|
|
|
#include "insn_trans/trans_rvzce.c.inc"
|
2024-07-09 14:36:44 +03:00
|
|
|
#include "insn_trans/trans_rvzcmop.c.inc"
|
2023-03-07 11:13:58 +03:00
|
|
|
|
2022-02-02 03:52:48 +03:00
|
|
|
/* Include decoders for factored-out extensions */
|
|
|
|
#include "decode-XVentanaCondOps.c.inc"
|
2019-02-13 18:53:56 +03:00
|
|
|
|
2022-08-18 22:07:28 +03:00
|
|
|
/* The specification allows for longer insns, but not supported by qemu. */
|
|
|
|
#define MAX_INSN_LEN 4
|
|
|
|
|
|
|
|
static inline int insn_len(uint16_t first_word)
|
|
|
|
{
|
|
|
|
return (first_word & 3) == 3 ? 4 : 2;
|
|
|
|
}
|
|
|
|
|
2024-05-06 05:36:07 +03:00
|
|
|
const RISCVDecoder decoder_table[] = {
|
|
|
|
{ always_true_p, decode_insn32 },
|
|
|
|
{ has_xthead_p, decode_xthead},
|
|
|
|
{ has_XVentanaCondOps_p, decode_XVentanaCodeOps},
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t decoder_table_size = ARRAY_SIZE(decoder_table);
|
|
|
|
|
2020-02-25 15:47:05 +03:00
|
|
|
static void decode_opc(CPURISCVState *env, DisasContext *ctx, uint16_t opcode)
|
2018-03-02 15:31:11 +03:00
|
|
|
{
|
2022-10-16 15:47:24 +03:00
|
|
|
ctx->virt_inst_excp = false;
|
2023-05-26 10:21:19 +03:00
|
|
|
ctx->cur_insn_len = insn_len(opcode);
|
2022-02-02 03:52:47 +03:00
|
|
|
/* Check for compressed insn */
|
2023-05-26 10:21:19 +03:00
|
|
|
if (ctx->cur_insn_len == 2) {
|
2022-12-03 20:57:44 +03:00
|
|
|
ctx->opcode = opcode;
|
2023-03-07 11:13:55 +03:00
|
|
|
/*
|
|
|
|
* The Zca extension is added as way to refer to instructions in the C
|
|
|
|
* extension that do not include the floating-point loads and stores
|
|
|
|
*/
|
2023-05-17 16:57:09 +03:00
|
|
|
if ((has_ext(ctx, RVC) || ctx->cfg_ptr->ext_zca) &&
|
|
|
|
decode_insn16(ctx, opcode)) {
|
2022-12-03 20:57:44 +03:00
|
|
|
return;
|
2018-03-02 15:31:11 +03:00
|
|
|
}
|
|
|
|
} else {
|
2020-02-25 15:47:05 +03:00
|
|
|
uint32_t opcode32 = opcode;
|
|
|
|
opcode32 = deposit32(opcode32, 16, 16,
|
2021-08-10 01:32:59 +03:00
|
|
|
translator_lduw(env, &ctx->base,
|
|
|
|
ctx->base.pc_next + 2));
|
2021-12-20 09:49:14 +03:00
|
|
|
ctx->opcode = opcode32;
|
2022-02-02 03:52:47 +03:00
|
|
|
|
2024-05-06 05:36:07 +03:00
|
|
|
for (guint i = 0; i < ctx->decoders->len; ++i) {
|
|
|
|
riscv_cpu_decode_fn func = g_ptr_array_index(ctx->decoders, i);
|
|
|
|
if (func(ctx, opcode32)) {
|
2022-02-02 03:52:47 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-02-13 18:53:41 +03:00
|
|
|
}
|
2018-03-02 15:31:11 +03:00
|
|
|
}
|
2022-02-02 03:52:47 +03:00
|
|
|
|
|
|
|
gen_exception_illegal(ctx);
|
2018-03-02 15:31:11 +03:00
|
|
|
}
|
|
|
|
|
2018-04-06 20:42:27 +03:00
|
|
|
static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
|
2018-03-02 15:31:11 +03:00
|
|
|
{
|
2018-04-06 20:42:27 +03:00
|
|
|
DisasContext *ctx = container_of(dcbase, DisasContext, base);
|
2023-09-14 03:22:49 +03:00
|
|
|
CPURISCVState *env = cpu_env(cs);
|
2024-02-03 13:11:09 +03:00
|
|
|
RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cs);
|
2019-06-24 11:59:05 +03:00
|
|
|
RISCVCPU *cpu = RISCV_CPU(cs);
|
2020-07-01 18:24:52 +03:00
|
|
|
uint32_t tb_flags = ctx->base.tb->flags;
|
2018-03-02 15:31:11 +03:00
|
|
|
|
2023-05-26 10:21:23 +03:00
|
|
|
ctx->pc_save = ctx->base.pc_first;
|
2023-04-12 14:43:14 +03:00
|
|
|
ctx->priv = FIELD_EX32(tb_flags, TB_FLAGS, PRIV);
|
2021-10-15 10:45:02 +03:00
|
|
|
ctx->mem_idx = FIELD_EX32(tb_flags, TB_FLAGS, MEM_IDX);
|
2023-04-12 14:43:11 +03:00
|
|
|
ctx->mstatus_fs = FIELD_EX32(tb_flags, TB_FLAGS, FS);
|
|
|
|
ctx->mstatus_vs = FIELD_EX32(tb_flags, TB_FLAGS, VS);
|
2019-01-15 02:58:32 +03:00
|
|
|
ctx->priv_ver = env->priv_ver;
|
2023-04-12 14:43:09 +03:00
|
|
|
ctx->virt_enabled = FIELD_EX32(tb_flags, TB_FLAGS, VIRT_ENABLED);
|
2021-10-20 06:16:57 +03:00
|
|
|
ctx->misa_ext = env->misa_ext;
|
2018-04-06 20:42:27 +03:00
|
|
|
ctx->frm = -1; /* unknown rounding mode */
|
2022-02-02 03:52:44 +03:00
|
|
|
ctx->cfg_ptr = &(cpu->cfg);
|
2020-07-01 18:24:52 +03:00
|
|
|
ctx->vill = FIELD_EX32(tb_flags, TB_FLAGS, VILL);
|
|
|
|
ctx->sew = FIELD_EX32(tb_flags, TB_FLAGS, SEW);
|
2021-12-10 10:55:59 +03:00
|
|
|
ctx->lmul = sextract32(FIELD_EX32(tb_flags, TB_FLAGS, LMUL), 0, 3);
|
2022-06-06 09:16:16 +03:00
|
|
|
ctx->vta = FIELD_EX32(tb_flags, TB_FLAGS, VTA) && cpu->cfg.rvv_ta_all_1s;
|
2022-06-20 09:51:02 +03:00
|
|
|
ctx->vma = FIELD_EX32(tb_flags, TB_FLAGS, VMA) && cpu->cfg.rvv_ma_all_1s;
|
2022-06-06 09:16:16 +03:00
|
|
|
ctx->cfg_vta_all_1s = cpu->cfg.rvv_ta_all_1s;
|
2023-04-12 14:43:13 +03:00
|
|
|
ctx->vstart_eq_zero = FIELD_EX32(tb_flags, TB_FLAGS, VSTART_EQ_ZERO);
|
2020-07-01 18:24:52 +03:00
|
|
|
ctx->vl_eq_vlmax = FIELD_EX32(tb_flags, TB_FLAGS, VL_EQ_VLMAX);
|
2024-02-03 13:11:09 +03:00
|
|
|
ctx->misa_mxl_max = mcc->misa_mxl_max;
|
2021-10-20 06:16:59 +03:00
|
|
|
ctx->xl = FIELD_EX32(tb_flags, TB_FLAGS, XL);
|
2023-06-14 06:25:46 +03:00
|
|
|
ctx->address_xl = FIELD_EX32(tb_flags, TB_FLAGS, AXL);
|
2021-01-09 01:42:52 +03:00
|
|
|
ctx->cs = cs;
|
2022-01-20 15:20:41 +03:00
|
|
|
ctx->pm_mask_enabled = FIELD_EX32(tb_flags, TB_FLAGS, PM_MASK_ENABLED);
|
|
|
|
ctx->pm_base_enabled = FIELD_EX32(tb_flags, TB_FLAGS, PM_BASE_ENABLED);
|
2024-02-07 15:22:54 +03:00
|
|
|
ctx->ztso = cpu->cfg.ext_ztso;
|
2022-10-13 09:29:43 +03:00
|
|
|
ctx->itrigger = FIELD_EX32(tb_flags, TB_FLAGS, ITRIGGER);
|
2021-08-23 22:55:10 +03:00
|
|
|
ctx->zero = tcg_constant_tl(0);
|
2023-01-27 22:17:58 +03:00
|
|
|
ctx->virt_inst_excp = false;
|
2024-05-06 05:36:07 +03:00
|
|
|
ctx->decoders = cpu->decoders;
|
2018-04-06 20:42:27 +03:00
|
|
|
}
|
2018-03-02 15:31:11 +03:00
|
|
|
|
2018-04-06 20:42:27 +03:00
|
|
|
static void riscv_tr_tb_start(DisasContextBase *db, CPUState *cpu)
|
|
|
|
{
|
|
|
|
}
|
2018-03-02 15:31:11 +03:00
|
|
|
|
2018-04-06 20:42:27 +03:00
|
|
|
static void riscv_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
|
|
|
|
{
|
|
|
|
DisasContext *ctx = container_of(dcbase, DisasContext, base);
|
2023-05-26 10:21:23 +03:00
|
|
|
target_ulong pc_next = ctx->base.pc_next;
|
|
|
|
|
|
|
|
if (tb_cflags(dcbase->tb) & CF_PCREL) {
|
|
|
|
pc_next &= ~TARGET_PAGE_MASK;
|
|
|
|
}
|
2018-04-06 20:42:27 +03:00
|
|
|
|
2023-05-26 10:21:23 +03:00
|
|
|
tcg_gen_insn_start(pc_next, 0);
|
2024-04-07 00:12:14 +03:00
|
|
|
ctx->insn_start_updated = false;
|
2018-04-06 20:42:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
|
|
|
|
{
|
|
|
|
DisasContext *ctx = container_of(dcbase, DisasContext, base);
|
2023-09-14 03:22:49 +03:00
|
|
|
CPURISCVState *env = cpu_env(cpu);
|
2021-08-10 01:32:59 +03:00
|
|
|
uint16_t opcode16 = translator_lduw(env, &ctx->base, ctx->base.pc_next);
|
2018-03-02 15:31:11 +03:00
|
|
|
|
2021-10-20 06:17:03 +03:00
|
|
|
ctx->ol = ctx->xl;
|
2020-02-25 15:47:05 +03:00
|
|
|
decode_opc(env, ctx, opcode16);
|
2023-05-26 10:21:24 +03:00
|
|
|
ctx->base.pc_next += ctx->cur_insn_len;
|
2021-08-23 22:55:10 +03:00
|
|
|
|
2022-08-19 04:00:30 +03:00
|
|
|
/* Only the first insn within a TB is allowed to cross a page boundary. */
|
2018-04-06 20:42:27 +03:00
|
|
|
if (ctx->base.is_jmp == DISAS_NEXT) {
|
2022-10-13 09:29:43 +03:00
|
|
|
if (ctx->itrigger || !is_same_page(&ctx->base, ctx->base.pc_next)) {
|
2018-04-06 20:42:27 +03:00
|
|
|
ctx->base.is_jmp = DISAS_TOO_MANY;
|
2022-08-19 04:00:30 +03:00
|
|
|
} else {
|
|
|
|
unsigned page_ofs = ctx->base.pc_next & ~TARGET_PAGE_MASK;
|
|
|
|
|
|
|
|
if (page_ofs > TARGET_PAGE_SIZE - MAX_INSN_LEN) {
|
2024-04-05 12:22:27 +03:00
|
|
|
uint16_t next_insn =
|
|
|
|
translator_lduw(env, &ctx->base, ctx->base.pc_next);
|
2022-08-19 04:00:30 +03:00
|
|
|
int len = insn_len(next_insn);
|
|
|
|
|
2023-02-20 10:27:32 +03:00
|
|
|
if (!is_same_page(&ctx->base, ctx->base.pc_next + len - 1)) {
|
2022-08-19 04:00:30 +03:00
|
|
|
ctx->base.is_jmp = DISAS_TOO_MANY;
|
|
|
|
}
|
|
|
|
}
|
2018-03-02 15:31:11 +03:00
|
|
|
}
|
|
|
|
}
|
2018-04-06 20:42:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void riscv_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
|
|
|
|
{
|
|
|
|
DisasContext *ctx = container_of(dcbase, DisasContext, base);
|
|
|
|
|
|
|
|
switch (ctx->base.is_jmp) {
|
2018-02-14 02:27:54 +03:00
|
|
|
case DISAS_TOO_MANY:
|
2023-05-26 10:21:20 +03:00
|
|
|
gen_goto_tb(ctx, 0, 0);
|
2018-03-02 15:31:11 +03:00
|
|
|
break;
|
2018-02-14 02:27:54 +03:00
|
|
|
case DISAS_NORETURN:
|
2018-03-02 15:31:11 +03:00
|
|
|
break;
|
2018-02-14 02:27:54 +03:00
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
2018-03-02 15:31:11 +03:00
|
|
|
}
|
2018-04-06 20:42:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static const TranslatorOps riscv_tr_ops = {
|
|
|
|
.init_disas_context = riscv_tr_init_disas_context,
|
|
|
|
.tb_start = riscv_tr_tb_start,
|
|
|
|
.insn_start = riscv_tr_insn_start,
|
|
|
|
.translate_insn = riscv_tr_translate_insn,
|
|
|
|
.tb_stop = riscv_tr_tb_stop,
|
|
|
|
};
|
|
|
|
|
2023-01-29 04:19:22 +03:00
|
|
|
void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int *max_insns,
|
2024-01-19 17:39:58 +03:00
|
|
|
vaddr pc, void *host_pc)
|
2018-04-06 20:42:27 +03:00
|
|
|
{
|
|
|
|
DisasContext ctx;
|
|
|
|
|
2022-08-11 23:48:03 +03:00
|
|
|
translator_loop(cs, tb, max_insns, pc, host_pc, &riscv_tr_ops, &ctx.base);
|
2018-03-02 15:31:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void riscv_translate_init(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2021-08-23 22:55:29 +03:00
|
|
|
/*
|
|
|
|
* cpu_gpr[0] is a placeholder for the zero register. Do not use it.
|
|
|
|
* Use the gen_set_gpr and get_gpr helper functions when accessing regs,
|
|
|
|
* unless you specifically block reads/writes to reg 0.
|
|
|
|
*/
|
2018-03-02 15:31:11 +03:00
|
|
|
cpu_gpr[0] = NULL;
|
2022-01-07 00:00:56 +03:00
|
|
|
cpu_gprh[0] = NULL;
|
2018-03-02 15:31:11 +03:00
|
|
|
|
|
|
|
for (i = 1; i < 32; i++) {
|
2023-09-14 02:37:36 +03:00
|
|
|
cpu_gpr[i] = tcg_global_mem_new(tcg_env,
|
2018-03-02 15:31:11 +03:00
|
|
|
offsetof(CPURISCVState, gpr[i]), riscv_int_regnames[i]);
|
2023-09-14 02:37:36 +03:00
|
|
|
cpu_gprh[i] = tcg_global_mem_new(tcg_env,
|
2022-01-07 00:00:56 +03:00
|
|
|
offsetof(CPURISCVState, gprh[i]), riscv_int_regnamesh[i]);
|
2018-03-02 15:31:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 32; i++) {
|
2023-09-14 02:37:36 +03:00
|
|
|
cpu_fpr[i] = tcg_global_mem_new_i64(tcg_env,
|
2018-03-02 15:31:11 +03:00
|
|
|
offsetof(CPURISCVState, fpr[i]), riscv_fpr_regnames[i]);
|
|
|
|
}
|
|
|
|
|
2023-09-14 02:37:36 +03:00
|
|
|
cpu_pc = tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, pc), "pc");
|
|
|
|
cpu_vl = tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, vl), "vl");
|
|
|
|
cpu_vstart = tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, vstart),
|
2021-12-10 10:56:52 +03:00
|
|
|
"vstart");
|
2023-09-14 02:37:36 +03:00
|
|
|
load_res = tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, load_res),
|
2018-03-02 15:31:11 +03:00
|
|
|
"load_res");
|
2023-09-14 02:37:36 +03:00
|
|
|
load_val = tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, load_val),
|
2018-03-02 15:31:11 +03:00
|
|
|
"load_val");
|
2021-10-25 20:36:08 +03:00
|
|
|
/* Assign PM CSRs to tcg globals */
|
2023-09-14 02:37:36 +03:00
|
|
|
pm_mask = tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, cur_pmmask),
|
2022-01-20 15:20:39 +03:00
|
|
|
"pmmask");
|
2023-09-14 02:37:36 +03:00
|
|
|
pm_base = tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, cur_pmbase),
|
2022-01-20 15:20:39 +03:00
|
|
|
"pmbase");
|
2018-03-02 15:31:11 +03:00
|
|
|
}
|