2 * RISC-V emulation for qemu: main translation routines.
4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2 or later, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
22 #include "tcg/tcg-op.h"
23 #include "exec/exec-all.h"
24 #include "exec/helper-proto.h"
25 #include "exec/helper-gen.h"
27 #include "exec/translator.h"
29 #include "semihosting/semihost.h"
31 #include "internals.h"
33 #define HELPER_H "helper.h"
34 #include "exec/helper-info.c.inc"
37 #include "tcg/tcg-cpu.h"
39 /* global register indices */
40 static TCGv cpu_gpr
[32], cpu_gprh
[32], cpu_pc
, cpu_vl
, cpu_vstart
;
41 static TCGv_i64 cpu_fpr
[32]; /* assume F and D extensions */
44 /* globals for PM CSRs */
49 * If an operation is being performed on less than TARGET_LONG_BITS,
50 * it may require the inputs to be sign- or zero-extended; which will
51 * depend on the exact operation being performed.
59 typedef struct DisasContext
{
60 DisasContextBase base
;
61 target_ulong cur_insn_len
;
63 target_ulong priv_ver
;
64 RISCVMXL misa_mxl_max
;
69 RISCVExtStatus mstatus_fs
;
70 RISCVExtStatus mstatus_vs
;
74 * Remember the rounding mode encoded in the previous fp instruction,
75 * which we have already installed into env->fp_status. Or -1 for
76 * no previous fp instruction. Note that we exit the TB when writing
77 * to any system register, which includes CSR_FRM, so we do not have
78 * to reset this known value.
84 const RISCVCPUConfig
*cfg_ptr
;
85 /* vector extension */
88 * Encode LMUL to lmul as follows:
108 /* PointerMasking extension */
109 bool pm_mask_enabled
;
110 bool pm_base_enabled
;
113 /* Use icount trigger for native debug */
115 /* FRM is known to contain a valid value. */
117 bool insn_start_updated
;
118 const GPtrArray
*decoders
;
121 static inline bool has_ext(DisasContext
*ctx
, uint32_t ext
)
123 return ctx
->misa_ext
& ext
;
126 #ifdef TARGET_RISCV32
127 #define get_xl(ctx) MXL_RV32
128 #elif defined(CONFIG_USER_ONLY)
129 #define get_xl(ctx) MXL_RV64
131 #define get_xl(ctx) ((ctx)->xl)
134 #ifdef TARGET_RISCV32
135 #define get_address_xl(ctx) MXL_RV32
136 #elif defined(CONFIG_USER_ONLY)
137 #define get_address_xl(ctx) MXL_RV64
139 #define get_address_xl(ctx) ((ctx)->address_xl)
142 /* The word size for this machine mode. */
143 static inline int __attribute__((unused
)) get_xlen(DisasContext
*ctx
)
145 return 16 << get_xl(ctx
);
148 /* The operation length, as opposed to the xlen. */
149 #ifdef TARGET_RISCV32
150 #define get_ol(ctx) MXL_RV32
152 #define get_ol(ctx) ((ctx)->ol)
155 static inline int get_olen(DisasContext
*ctx
)
157 return 16 << get_ol(ctx
);
160 /* The maximum register length */
161 #ifdef TARGET_RISCV32
162 #define get_xl_max(ctx) MXL_RV32
164 #define get_xl_max(ctx) ((ctx)->misa_mxl_max)
168 * RISC-V requires NaN-boxing of narrower width floating point values.
169 * This applies when a 32-bit value is assigned to a 64-bit FP register.
170 * For consistency and simplicity, we nanbox results even when the RVD
171 * extension is not present.
173 static void gen_nanbox_s(TCGv_i64 out
, TCGv_i64 in
)
175 tcg_gen_ori_i64(out
, in
, MAKE_64BIT_MASK(32, 32));
178 static void gen_nanbox_h(TCGv_i64 out
, TCGv_i64 in
)
180 tcg_gen_ori_i64(out
, in
, MAKE_64BIT_MASK(16, 48));
184 * A narrow n-bit operation, where n < FLEN, checks that input operands
185 * are correctly Nan-boxed, i.e., all upper FLEN - n bits are 1.
186 * If so, the least-significant bits of the input are used, otherwise the
187 * input value is treated as an n-bit canonical NaN (v2.2 section 9.2).
189 * Here, the result is always nan-boxed, even the canonical nan.
191 static void gen_check_nanbox_h(TCGv_i64 out
, TCGv_i64 in
)
193 TCGv_i64 t_max
= tcg_constant_i64(0xffffffffffff0000ull
);
194 TCGv_i64 t_nan
= tcg_constant_i64(0xffffffffffff7e00ull
);
196 tcg_gen_movcond_i64(TCG_COND_GEU
, out
, in
, t_max
, in
, t_nan
);
199 static void gen_check_nanbox_s(TCGv_i64 out
, TCGv_i64 in
)
201 TCGv_i64 t_max
= tcg_constant_i64(0xffffffff00000000ull
);
202 TCGv_i64 t_nan
= tcg_constant_i64(0xffffffff7fc00000ull
);
204 tcg_gen_movcond_i64(TCG_COND_GEU
, out
, in
, t_max
, in
, t_nan
);
207 static void decode_save_opc(DisasContext
*ctx
)
209 assert(!ctx
->insn_start_updated
);
210 ctx
->insn_start_updated
= true;
211 tcg_set_insn_start_param(ctx
->base
.insn_start
, 1, ctx
->opcode
);
214 static void gen_pc_plus_diff(TCGv target
, DisasContext
*ctx
,
217 target_ulong dest
= ctx
->base
.pc_next
+ diff
;
219 assert(ctx
->pc_save
!= -1);
220 if (tb_cflags(ctx
->base
.tb
) & CF_PCREL
) {
221 tcg_gen_addi_tl(target
, cpu_pc
, dest
- ctx
->pc_save
);
222 if (get_xl(ctx
) == MXL_RV32
) {
223 tcg_gen_ext32s_tl(target
, target
);
226 if (get_xl(ctx
) == MXL_RV32
) {
227 dest
= (int32_t)dest
;
229 tcg_gen_movi_tl(target
, dest
);
233 static void gen_update_pc(DisasContext
*ctx
, target_long diff
)
235 gen_pc_plus_diff(cpu_pc
, ctx
, diff
);
236 ctx
->pc_save
= ctx
->base
.pc_next
+ diff
;
239 static void generate_exception(DisasContext
*ctx
, int excp
)
241 gen_update_pc(ctx
, 0);
242 gen_helper_raise_exception(tcg_env
, tcg_constant_i32(excp
));
243 ctx
->base
.is_jmp
= DISAS_NORETURN
;
246 static void gen_exception_illegal(DisasContext
*ctx
)
248 tcg_gen_st_i32(tcg_constant_i32(ctx
->opcode
), tcg_env
,
249 offsetof(CPURISCVState
, bins
));
250 if (ctx
->virt_inst_excp
) {
251 generate_exception(ctx
, RISCV_EXCP_VIRT_INSTRUCTION_FAULT
);
253 generate_exception(ctx
, RISCV_EXCP_ILLEGAL_INST
);
257 static void gen_exception_inst_addr_mis(DisasContext
*ctx
, TCGv target
)
259 tcg_gen_st_tl(target
, tcg_env
, offsetof(CPURISCVState
, badaddr
));
260 generate_exception(ctx
, RISCV_EXCP_INST_ADDR_MIS
);
263 static void lookup_and_goto_ptr(DisasContext
*ctx
)
265 #ifndef CONFIG_USER_ONLY
267 gen_helper_itrigger_match(tcg_env
);
270 tcg_gen_lookup_and_goto_ptr();
273 static void exit_tb(DisasContext
*ctx
)
275 #ifndef CONFIG_USER_ONLY
277 gen_helper_itrigger_match(tcg_env
);
280 tcg_gen_exit_tb(NULL
, 0);
283 static void gen_goto_tb(DisasContext
*ctx
, int n
, target_long diff
)
285 target_ulong dest
= ctx
->base
.pc_next
+ diff
;
288 * Under itrigger, instruction executes one by one like singlestep,
289 * direct block chain benefits will be small.
291 if (translator_use_goto_tb(&ctx
->base
, dest
) && !ctx
->itrigger
) {
293 * For pcrel, the pc must always be up-to-date on entry to
294 * the linked TB, so that it can use simple additions for all
295 * further adjustments. For !pcrel, the linked TB is compiled
296 * to know its full virtual address, so we can delay the
297 * update to pc to the unlinked path. A long chain of links
298 * can thus avoid many updates to the PC.
300 if (tb_cflags(ctx
->base
.tb
) & CF_PCREL
) {
301 gen_update_pc(ctx
, diff
);
305 gen_update_pc(ctx
, diff
);
307 tcg_gen_exit_tb(ctx
->base
.tb
, n
);
309 gen_update_pc(ctx
, diff
);
310 lookup_and_goto_ptr(ctx
);
315 * Wrappers for getting reg values.
317 * The $zero register does not have cpu_gpr[0] allocated -- we supply the
318 * constant zero as a source, and an uninitialized sink as destination.
320 * Further, we may provide an extension for word operations.
322 static TCGv
get_gpr(DisasContext
*ctx
, int reg_num
, DisasExtend ext
)
330 switch (get_ol(ctx
)) {
337 tcg_gen_ext32s_tl(t
, cpu_gpr
[reg_num
]);
341 tcg_gen_ext32u_tl(t
, cpu_gpr
[reg_num
]);
344 g_assert_not_reached();
351 g_assert_not_reached();
353 return cpu_gpr
[reg_num
];
356 static TCGv
get_gprh(DisasContext
*ctx
, int reg_num
)
358 assert(get_xl(ctx
) == MXL_RV128
);
362 return cpu_gprh
[reg_num
];
365 static TCGv
dest_gpr(DisasContext
*ctx
, int reg_num
)
367 if (reg_num
== 0 || get_olen(ctx
) < TARGET_LONG_BITS
) {
368 return tcg_temp_new();
370 return cpu_gpr
[reg_num
];
373 static TCGv
dest_gprh(DisasContext
*ctx
, int reg_num
)
376 return tcg_temp_new();
378 return cpu_gprh
[reg_num
];
381 static void gen_set_gpr(DisasContext
*ctx
, int reg_num
, TCGv t
)
384 switch (get_ol(ctx
)) {
386 tcg_gen_ext32s_tl(cpu_gpr
[reg_num
], t
);
390 tcg_gen_mov_tl(cpu_gpr
[reg_num
], t
);
393 g_assert_not_reached();
396 if (get_xl_max(ctx
) == MXL_RV128
) {
397 tcg_gen_sari_tl(cpu_gprh
[reg_num
], cpu_gpr
[reg_num
], 63);
402 static void gen_set_gpri(DisasContext
*ctx
, int reg_num
, target_long imm
)
405 switch (get_ol(ctx
)) {
407 tcg_gen_movi_tl(cpu_gpr
[reg_num
], (int32_t)imm
);
411 tcg_gen_movi_tl(cpu_gpr
[reg_num
], imm
);
414 g_assert_not_reached();
417 if (get_xl_max(ctx
) == MXL_RV128
) {
418 tcg_gen_movi_tl(cpu_gprh
[reg_num
], -(imm
< 0));
423 static void gen_set_gpr128(DisasContext
*ctx
, int reg_num
, TCGv rl
, TCGv rh
)
425 assert(get_ol(ctx
) == MXL_RV128
);
427 tcg_gen_mov_tl(cpu_gpr
[reg_num
], rl
);
428 tcg_gen_mov_tl(cpu_gprh
[reg_num
], rh
);
432 static TCGv_i64
get_fpr_hs(DisasContext
*ctx
, int reg_num
)
434 if (!ctx
->cfg_ptr
->ext_zfinx
) {
435 return cpu_fpr
[reg_num
];
439 return tcg_constant_i64(0);
441 switch (get_xl(ctx
)) {
443 #ifdef TARGET_RISCV32
445 TCGv_i64 t
= tcg_temp_new_i64();
446 tcg_gen_ext_i32_i64(t
, cpu_gpr
[reg_num
]);
452 return cpu_gpr
[reg_num
];
455 g_assert_not_reached();
459 static TCGv_i64
get_fpr_d(DisasContext
*ctx
, int reg_num
)
461 if (!ctx
->cfg_ptr
->ext_zfinx
) {
462 return cpu_fpr
[reg_num
];
466 return tcg_constant_i64(0);
468 switch (get_xl(ctx
)) {
471 TCGv_i64 t
= tcg_temp_new_i64();
472 tcg_gen_concat_tl_i64(t
, cpu_gpr
[reg_num
], cpu_gpr
[reg_num
+ 1]);
475 #ifdef TARGET_RISCV64
477 return cpu_gpr
[reg_num
];
480 g_assert_not_reached();
484 static TCGv_i64
dest_fpr(DisasContext
*ctx
, int reg_num
)
486 if (!ctx
->cfg_ptr
->ext_zfinx
) {
487 return cpu_fpr
[reg_num
];
491 return tcg_temp_new_i64();
494 switch (get_xl(ctx
)) {
496 return tcg_temp_new_i64();
497 #ifdef TARGET_RISCV64
499 return cpu_gpr
[reg_num
];
502 g_assert_not_reached();
506 /* assume it is nanboxing (for normal) or sign-extended (for zfinx) */
507 static void gen_set_fpr_hs(DisasContext
*ctx
, int reg_num
, TCGv_i64 t
)
509 if (!ctx
->cfg_ptr
->ext_zfinx
) {
510 tcg_gen_mov_i64(cpu_fpr
[reg_num
], t
);
514 switch (get_xl(ctx
)) {
516 #ifdef TARGET_RISCV32
517 tcg_gen_extrl_i64_i32(cpu_gpr
[reg_num
], t
);
522 tcg_gen_mov_i64(cpu_gpr
[reg_num
], t
);
526 g_assert_not_reached();
531 static void gen_set_fpr_d(DisasContext
*ctx
, int reg_num
, TCGv_i64 t
)
533 if (!ctx
->cfg_ptr
->ext_zfinx
) {
534 tcg_gen_mov_i64(cpu_fpr
[reg_num
], t
);
539 switch (get_xl(ctx
)) {
541 #ifdef TARGET_RISCV32
542 tcg_gen_extr_i64_i32(cpu_gpr
[reg_num
], cpu_gpr
[reg_num
+ 1], t
);
545 tcg_gen_ext32s_i64(cpu_gpr
[reg_num
], t
);
546 tcg_gen_sari_i64(cpu_gpr
[reg_num
+ 1], t
, 32);
549 tcg_gen_mov_i64(cpu_gpr
[reg_num
], t
);
553 g_assert_not_reached();
558 static void gen_jal(DisasContext
*ctx
, int rd
, target_ulong imm
)
560 TCGv succ_pc
= dest_gpr(ctx
, rd
);
562 /* check misaligned: */
563 if (!has_ext(ctx
, RVC
) && !ctx
->cfg_ptr
->ext_zca
) {
564 if ((imm
& 0x3) != 0) {
565 TCGv target_pc
= tcg_temp_new();
566 gen_pc_plus_diff(target_pc
, ctx
, imm
);
567 gen_exception_inst_addr_mis(ctx
, target_pc
);
572 gen_pc_plus_diff(succ_pc
, ctx
, ctx
->cur_insn_len
);
573 gen_set_gpr(ctx
, rd
, succ_pc
);
575 gen_goto_tb(ctx
, 0, imm
); /* must use this for safety */
576 ctx
->base
.is_jmp
= DISAS_NORETURN
;
579 /* Compute a canonical address from a register plus offset. */
580 static TCGv
get_address(DisasContext
*ctx
, int rs1
, int imm
)
582 TCGv addr
= tcg_temp_new();
583 TCGv src1
= get_gpr(ctx
, rs1
, EXT_NONE
);
585 tcg_gen_addi_tl(addr
, src1
, imm
);
586 if (ctx
->pm_mask_enabled
) {
587 tcg_gen_andc_tl(addr
, addr
, pm_mask
);
588 } else if (get_address_xl(ctx
) == MXL_RV32
) {
589 tcg_gen_ext32u_tl(addr
, addr
);
591 if (ctx
->pm_base_enabled
) {
592 tcg_gen_or_tl(addr
, addr
, pm_base
);
598 /* Compute a canonical address from a register plus reg offset. */
599 static TCGv
get_address_indexed(DisasContext
*ctx
, int rs1
, TCGv offs
)
601 TCGv addr
= tcg_temp_new();
602 TCGv src1
= get_gpr(ctx
, rs1
, EXT_NONE
);
604 tcg_gen_add_tl(addr
, src1
, offs
);
605 if (ctx
->pm_mask_enabled
) {
606 tcg_gen_andc_tl(addr
, addr
, pm_mask
);
607 } else if (get_xl(ctx
) == MXL_RV32
) {
608 tcg_gen_ext32u_tl(addr
, addr
);
610 if (ctx
->pm_base_enabled
) {
611 tcg_gen_or_tl(addr
, addr
, pm_base
);
616 #ifndef CONFIG_USER_ONLY
618 * We will have already diagnosed disabled state,
619 * and need to turn initial/clean into dirty.
621 static void mark_fs_dirty(DisasContext
*ctx
)
625 if (!has_ext(ctx
, RVF
)) {
629 if (ctx
->mstatus_fs
!= EXT_STATUS_DIRTY
) {
630 /* Remember the state change for the rest of the TB. */
631 ctx
->mstatus_fs
= EXT_STATUS_DIRTY
;
633 tmp
= tcg_temp_new();
634 tcg_gen_ld_tl(tmp
, tcg_env
, offsetof(CPURISCVState
, mstatus
));
635 tcg_gen_ori_tl(tmp
, tmp
, MSTATUS_FS
);
636 tcg_gen_st_tl(tmp
, tcg_env
, offsetof(CPURISCVState
, mstatus
));
638 if (ctx
->virt_enabled
) {
639 tcg_gen_ld_tl(tmp
, tcg_env
, offsetof(CPURISCVState
, mstatus_hs
));
640 tcg_gen_ori_tl(tmp
, tmp
, MSTATUS_FS
);
641 tcg_gen_st_tl(tmp
, tcg_env
, offsetof(CPURISCVState
, mstatus_hs
));
646 static inline void mark_fs_dirty(DisasContext
*ctx
) { }
649 #ifndef CONFIG_USER_ONLY
651 * We will have already diagnosed disabled state,
652 * and need to turn initial/clean into dirty.
654 static void mark_vs_dirty(DisasContext
*ctx
)
658 if (ctx
->mstatus_vs
!= EXT_STATUS_DIRTY
) {
659 /* Remember the state change for the rest of the TB. */
660 ctx
->mstatus_vs
= EXT_STATUS_DIRTY
;
662 tmp
= tcg_temp_new();
663 tcg_gen_ld_tl(tmp
, tcg_env
, offsetof(CPURISCVState
, mstatus
));
664 tcg_gen_ori_tl(tmp
, tmp
, MSTATUS_VS
);
665 tcg_gen_st_tl(tmp
, tcg_env
, offsetof(CPURISCVState
, mstatus
));
667 if (ctx
->virt_enabled
) {
668 tcg_gen_ld_tl(tmp
, tcg_env
, offsetof(CPURISCVState
, mstatus_hs
));
669 tcg_gen_ori_tl(tmp
, tmp
, MSTATUS_VS
);
670 tcg_gen_st_tl(tmp
, tcg_env
, offsetof(CPURISCVState
, mstatus_hs
));
675 static inline void mark_vs_dirty(DisasContext
*ctx
) { }
678 static void finalize_rvv_inst(DisasContext
*ctx
)
681 ctx
->vstart_eq_zero
= true;
684 static void gen_set_rm(DisasContext
*ctx
, int rm
)
686 if (ctx
->frm
== rm
) {
691 if (rm
== RISCV_FRM_DYN
) {
692 /* The helper will return only if frm valid. */
693 ctx
->frm_valid
= true;
696 /* The helper may raise ILLEGAL_INSN -- record binv for unwind. */
697 decode_save_opc(ctx
);
698 gen_helper_set_rounding_mode(tcg_env
, tcg_constant_i32(rm
));
701 static void gen_set_rm_chkfrm(DisasContext
*ctx
, int rm
)
703 if (ctx
->frm
== rm
&& ctx
->frm_valid
) {
707 ctx
->frm_valid
= true;
709 /* The helper may raise ILLEGAL_INSN -- record binv for unwind. */
710 decode_save_opc(ctx
);
711 gen_helper_set_rounding_mode_chkfrm(tcg_env
, tcg_constant_i32(rm
));
714 static int ex_plus_1(DisasContext
*ctx
, int nf
)
719 #define EX_SH(amount) \
720 static int ex_shift_##amount(DisasContext *ctx, int imm) \
722 return imm << amount; \
730 #define REQUIRE_EXT(ctx, ext) do { \
731 if (!has_ext(ctx, ext)) { \
736 #define REQUIRE_32BIT(ctx) do { \
737 if (get_xl(ctx) != MXL_RV32) { \
742 #define REQUIRE_64BIT(ctx) do { \
743 if (get_xl(ctx) != MXL_RV64) { \
748 #define REQUIRE_128BIT(ctx) do { \
749 if (get_xl(ctx) != MXL_RV128) { \
754 #define REQUIRE_64_OR_128BIT(ctx) do { \
755 if (get_xl(ctx) == MXL_RV32) { \
760 #define REQUIRE_EITHER_EXT(ctx, A, B) do { \
761 if (!ctx->cfg_ptr->ext_##A && \
762 !ctx->cfg_ptr->ext_##B) { \
767 static int ex_rvc_register(DisasContext
*ctx
, int reg
)
772 static int ex_sreg_register(DisasContext
*ctx
, int reg
)
774 return reg
< 2 ? reg
+ 8 : reg
+ 16;
777 static int ex_rvc_shiftli(DisasContext
*ctx
, int imm
)
779 /* For RV128 a shamt of 0 means a shift by 64. */
780 if (get_ol(ctx
) == MXL_RV128
) {
781 imm
= imm
? imm
: 64;
786 static int ex_rvc_shiftri(DisasContext
*ctx
, int imm
)
789 * For RV128 a shamt of 0 means a shift by 64, furthermore, for right
790 * shifts, the shamt is sign-extended.
792 if (get_ol(ctx
) == MXL_RV128
) {
793 imm
= imm
| (imm
& 32) << 1;
794 imm
= imm
? imm
: 64;
799 /* Include the auto-generated decoder for 32 bit insn */
800 #include "decode-insn32.c.inc"
802 static bool gen_logic_imm_fn(DisasContext
*ctx
, arg_i
*a
,
803 void (*func
)(TCGv
, TCGv
, target_long
))
805 TCGv dest
= dest_gpr(ctx
, a
->rd
);
806 TCGv src1
= get_gpr(ctx
, a
->rs1
, EXT_NONE
);
808 func(dest
, src1
, a
->imm
);
810 if (get_xl(ctx
) == MXL_RV128
) {
811 TCGv src1h
= get_gprh(ctx
, a
->rs1
);
812 TCGv desth
= dest_gprh(ctx
, a
->rd
);
814 func(desth
, src1h
, -(a
->imm
< 0));
815 gen_set_gpr128(ctx
, a
->rd
, dest
, desth
);
817 gen_set_gpr(ctx
, a
->rd
, dest
);
823 static bool gen_logic(DisasContext
*ctx
, arg_r
*a
,
824 void (*func
)(TCGv
, TCGv
, TCGv
))
826 TCGv dest
= dest_gpr(ctx
, a
->rd
);
827 TCGv src1
= get_gpr(ctx
, a
->rs1
, EXT_NONE
);
828 TCGv src2
= get_gpr(ctx
, a
->rs2
, EXT_NONE
);
830 func(dest
, src1
, src2
);
832 if (get_xl(ctx
) == MXL_RV128
) {
833 TCGv src1h
= get_gprh(ctx
, a
->rs1
);
834 TCGv src2h
= get_gprh(ctx
, a
->rs2
);
835 TCGv desth
= dest_gprh(ctx
, a
->rd
);
837 func(desth
, src1h
, src2h
);
838 gen_set_gpr128(ctx
, a
->rd
, dest
, desth
);
840 gen_set_gpr(ctx
, a
->rd
, dest
);
846 static bool gen_arith_imm_fn(DisasContext
*ctx
, arg_i
*a
, DisasExtend ext
,
847 void (*func
)(TCGv
, TCGv
, target_long
),
848 void (*f128
)(TCGv
, TCGv
, TCGv
, TCGv
, target_long
))
850 TCGv dest
= dest_gpr(ctx
, a
->rd
);
851 TCGv src1
= get_gpr(ctx
, a
->rs1
, ext
);
853 if (get_ol(ctx
) < MXL_RV128
) {
854 func(dest
, src1
, a
->imm
);
855 gen_set_gpr(ctx
, a
->rd
, dest
);
861 TCGv src1h
= get_gprh(ctx
, a
->rs1
);
862 TCGv desth
= dest_gprh(ctx
, a
->rd
);
864 f128(dest
, desth
, src1
, src1h
, a
->imm
);
865 gen_set_gpr128(ctx
, a
->rd
, dest
, desth
);
870 static bool gen_arith_imm_tl(DisasContext
*ctx
, arg_i
*a
, DisasExtend ext
,
871 void (*func
)(TCGv
, TCGv
, TCGv
),
872 void (*f128
)(TCGv
, TCGv
, TCGv
, TCGv
, TCGv
, TCGv
))
874 TCGv dest
= dest_gpr(ctx
, a
->rd
);
875 TCGv src1
= get_gpr(ctx
, a
->rs1
, ext
);
876 TCGv src2
= tcg_constant_tl(a
->imm
);
878 if (get_ol(ctx
) < MXL_RV128
) {
879 func(dest
, src1
, src2
);
880 gen_set_gpr(ctx
, a
->rd
, dest
);
886 TCGv src1h
= get_gprh(ctx
, a
->rs1
);
887 TCGv src2h
= tcg_constant_tl(-(a
->imm
< 0));
888 TCGv desth
= dest_gprh(ctx
, a
->rd
);
890 f128(dest
, desth
, src1
, src1h
, src2
, src2h
);
891 gen_set_gpr128(ctx
, a
->rd
, dest
, desth
);
896 static bool gen_arith(DisasContext
*ctx
, arg_r
*a
, DisasExtend ext
,
897 void (*func
)(TCGv
, TCGv
, TCGv
),
898 void (*f128
)(TCGv
, TCGv
, TCGv
, TCGv
, TCGv
, TCGv
))
900 TCGv dest
= dest_gpr(ctx
, a
->rd
);
901 TCGv src1
= get_gpr(ctx
, a
->rs1
, ext
);
902 TCGv src2
= get_gpr(ctx
, a
->rs2
, ext
);
904 if (get_ol(ctx
) < MXL_RV128
) {
905 func(dest
, src1
, src2
);
906 gen_set_gpr(ctx
, a
->rd
, dest
);
912 TCGv src1h
= get_gprh(ctx
, a
->rs1
);
913 TCGv src2h
= get_gprh(ctx
, a
->rs2
);
914 TCGv desth
= dest_gprh(ctx
, a
->rd
);
916 f128(dest
, desth
, src1
, src1h
, src2
, src2h
);
917 gen_set_gpr128(ctx
, a
->rd
, dest
, desth
);
922 static bool gen_arith_per_ol(DisasContext
*ctx
, arg_r
*a
, DisasExtend ext
,
923 void (*f_tl
)(TCGv
, TCGv
, TCGv
),
924 void (*f_32
)(TCGv
, TCGv
, TCGv
),
925 void (*f_128
)(TCGv
, TCGv
, TCGv
, TCGv
, TCGv
, TCGv
))
927 int olen
= get_olen(ctx
);
929 if (olen
!= TARGET_LONG_BITS
) {
932 } else if (olen
!= 128) {
933 g_assert_not_reached();
936 return gen_arith(ctx
, a
, ext
, f_tl
, f_128
);
939 static bool gen_shift_imm_fn(DisasContext
*ctx
, arg_shift
*a
, DisasExtend ext
,
940 void (*func
)(TCGv
, TCGv
, target_long
),
941 void (*f128
)(TCGv
, TCGv
, TCGv
, TCGv
, target_long
))
944 int max_len
= get_olen(ctx
);
946 if (a
->shamt
>= max_len
) {
950 dest
= dest_gpr(ctx
, a
->rd
);
951 src1
= get_gpr(ctx
, a
->rs1
, ext
);
954 func(dest
, src1
, a
->shamt
);
955 gen_set_gpr(ctx
, a
->rd
, dest
);
957 TCGv src1h
= get_gprh(ctx
, a
->rs1
);
958 TCGv desth
= dest_gprh(ctx
, a
->rd
);
963 f128(dest
, desth
, src1
, src1h
, a
->shamt
);
964 gen_set_gpr128(ctx
, a
->rd
, dest
, desth
);
969 static bool gen_shift_imm_fn_per_ol(DisasContext
*ctx
, arg_shift
*a
,
971 void (*f_tl
)(TCGv
, TCGv
, target_long
),
972 void (*f_32
)(TCGv
, TCGv
, target_long
),
973 void (*f_128
)(TCGv
, TCGv
, TCGv
, TCGv
,
976 int olen
= get_olen(ctx
);
977 if (olen
!= TARGET_LONG_BITS
) {
980 } else if (olen
!= 128) {
981 g_assert_not_reached();
984 return gen_shift_imm_fn(ctx
, a
, ext
, f_tl
, f_128
);
987 static bool gen_shift_imm_tl(DisasContext
*ctx
, arg_shift
*a
, DisasExtend ext
,
988 void (*func
)(TCGv
, TCGv
, TCGv
))
990 TCGv dest
, src1
, src2
;
991 int max_len
= get_olen(ctx
);
993 if (a
->shamt
>= max_len
) {
997 dest
= dest_gpr(ctx
, a
->rd
);
998 src1
= get_gpr(ctx
, a
->rs1
, ext
);
999 src2
= tcg_constant_tl(a
->shamt
);
1001 func(dest
, src1
, src2
);
1003 gen_set_gpr(ctx
, a
->rd
, dest
);
1007 static bool gen_shift(DisasContext
*ctx
, arg_r
*a
, DisasExtend ext
,
1008 void (*func
)(TCGv
, TCGv
, TCGv
),
1009 void (*f128
)(TCGv
, TCGv
, TCGv
, TCGv
, TCGv
))
1011 TCGv src2
= get_gpr(ctx
, a
->rs2
, EXT_NONE
);
1012 TCGv ext2
= tcg_temp_new();
1013 int max_len
= get_olen(ctx
);
1015 tcg_gen_andi_tl(ext2
, src2
, max_len
- 1);
1017 TCGv dest
= dest_gpr(ctx
, a
->rd
);
1018 TCGv src1
= get_gpr(ctx
, a
->rs1
, ext
);
1020 if (max_len
< 128) {
1021 func(dest
, src1
, ext2
);
1022 gen_set_gpr(ctx
, a
->rd
, dest
);
1024 TCGv src1h
= get_gprh(ctx
, a
->rs1
);
1025 TCGv desth
= dest_gprh(ctx
, a
->rd
);
1030 f128(dest
, desth
, src1
, src1h
, ext2
);
1031 gen_set_gpr128(ctx
, a
->rd
, dest
, desth
);
1036 static bool gen_shift_per_ol(DisasContext
*ctx
, arg_r
*a
, DisasExtend ext
,
1037 void (*f_tl
)(TCGv
, TCGv
, TCGv
),
1038 void (*f_32
)(TCGv
, TCGv
, TCGv
),
1039 void (*f_128
)(TCGv
, TCGv
, TCGv
, TCGv
, TCGv
))
1041 int olen
= get_olen(ctx
);
1042 if (olen
!= TARGET_LONG_BITS
) {
1045 } else if (olen
!= 128) {
1046 g_assert_not_reached();
1049 return gen_shift(ctx
, a
, ext
, f_tl
, f_128
);
1052 static bool gen_unary(DisasContext
*ctx
, arg_r2
*a
, DisasExtend ext
,
1053 void (*func
)(TCGv
, TCGv
))
1055 TCGv dest
= dest_gpr(ctx
, a
->rd
);
1056 TCGv src1
= get_gpr(ctx
, a
->rs1
, ext
);
1060 gen_set_gpr(ctx
, a
->rd
, dest
);
1064 static bool gen_unary_per_ol(DisasContext
*ctx
, arg_r2
*a
, DisasExtend ext
,
1065 void (*f_tl
)(TCGv
, TCGv
),
1066 void (*f_32
)(TCGv
, TCGv
))
1068 int olen
= get_olen(ctx
);
1070 if (olen
!= TARGET_LONG_BITS
) {
1074 g_assert_not_reached();
1077 return gen_unary(ctx
, a
, ext
, f_tl
);
1080 static bool gen_amo(DisasContext
*ctx
, arg_atomic
*a
,
1081 void(*func
)(TCGv
, TCGv
, TCGv
, TCGArg
, MemOp
),
1084 TCGv dest
= dest_gpr(ctx
, a
->rd
);
1085 TCGv src1
, src2
= get_gpr(ctx
, a
->rs2
, EXT_NONE
);
1086 MemOp size
= mop
& MO_SIZE
;
1088 if (ctx
->cfg_ptr
->ext_zama16b
&& size
>= MO_32
) {
1089 mop
|= MO_ATOM_WITHIN16
;
1094 decode_save_opc(ctx
);
1095 src1
= get_address(ctx
, a
->rs1
, 0);
1096 func(dest
, src1
, src2
, ctx
->mem_idx
, mop
);
1098 gen_set_gpr(ctx
, a
->rd
, dest
);
1102 static bool gen_cmpxchg(DisasContext
*ctx
, arg_atomic
*a
, MemOp mop
)
1104 TCGv dest
= get_gpr(ctx
, a
->rd
, EXT_NONE
);
1105 TCGv src1
= get_address(ctx
, a
->rs1
, 0);
1106 TCGv src2
= get_gpr(ctx
, a
->rs2
, EXT_NONE
);
1108 decode_save_opc(ctx
);
1109 tcg_gen_atomic_cmpxchg_tl(dest
, src1
, dest
, src2
, ctx
->mem_idx
, mop
);
1111 gen_set_gpr(ctx
, a
->rd
, dest
);
1115 static uint32_t opcode_at(DisasContextBase
*dcbase
, target_ulong pc
)
1117 DisasContext
*ctx
= container_of(dcbase
, DisasContext
, base
);
1118 CPUState
*cpu
= ctx
->cs
;
1119 CPURISCVState
*env
= cpu_env(cpu
);
1121 return translator_ldl(env
, &ctx
->base
, pc
);
1124 /* Include insn module translation function */
1125 #include "insn_trans/trans_rvi.c.inc"
1126 #include "insn_trans/trans_rvm.c.inc"
1127 #include "insn_trans/trans_rva.c.inc"
1128 #include "insn_trans/trans_rvf.c.inc"
1129 #include "insn_trans/trans_rvd.c.inc"
1130 #include "insn_trans/trans_rvh.c.inc"
1131 #include "insn_trans/trans_rvv.c.inc"
1132 #include "insn_trans/trans_rvb.c.inc"
1133 #include "insn_trans/trans_rvzicond.c.inc"
1134 #include "insn_trans/trans_rvzacas.c.inc"
1135 #include "insn_trans/trans_rvzabha.c.inc"
1136 #include "insn_trans/trans_rvzawrs.c.inc"
1137 #include "insn_trans/trans_rvzicbo.c.inc"
1138 #include "insn_trans/trans_rvzimop.c.inc"
1139 #include "insn_trans/trans_rvzfa.c.inc"
1140 #include "insn_trans/trans_rvzfh.c.inc"
1141 #include "insn_trans/trans_rvk.c.inc"
1142 #include "insn_trans/trans_rvvk.c.inc"
1143 #include "insn_trans/trans_privileged.c.inc"
1144 #include "insn_trans/trans_svinval.c.inc"
1145 #include "insn_trans/trans_rvbf16.c.inc"
1146 #include "decode-xthead.c.inc"
1147 #include "insn_trans/trans_xthead.c.inc"
1148 #include "insn_trans/trans_xventanacondops.c.inc"
1150 /* Include the auto-generated decoder for 16 bit insn */
1151 #include "decode-insn16.c.inc"
1152 #include "insn_trans/trans_rvzce.c.inc"
1153 #include "insn_trans/trans_rvzcmop.c.inc"
1155 /* Include decoders for factored-out extensions */
1156 #include "decode-XVentanaCondOps.c.inc"
1158 /* The specification allows for longer insns, but not supported by qemu. */
1159 #define MAX_INSN_LEN 4
1161 static inline int insn_len(uint16_t first_word
)
1163 return (first_word
& 3) == 3 ? 4 : 2;
1166 const RISCVDecoder decoder_table
[] = {
1167 { always_true_p
, decode_insn32
},
1168 { has_xthead_p
, decode_xthead
},
1169 { has_XVentanaCondOps_p
, decode_XVentanaCodeOps
},
1172 const size_t decoder_table_size
= ARRAY_SIZE(decoder_table
);
1174 static void decode_opc(CPURISCVState
*env
, DisasContext
*ctx
, uint16_t opcode
)
1176 ctx
->virt_inst_excp
= false;
1177 ctx
->cur_insn_len
= insn_len(opcode
);
1178 /* Check for compressed insn */
1179 if (ctx
->cur_insn_len
== 2) {
1180 ctx
->opcode
= opcode
;
1182 * The Zca extension is added as way to refer to instructions in the C
1183 * extension that do not include the floating-point loads and stores
1185 if ((has_ext(ctx
, RVC
) || ctx
->cfg_ptr
->ext_zca
) &&
1186 decode_insn16(ctx
, opcode
)) {
1190 uint32_t opcode32
= opcode
;
1191 opcode32
= deposit32(opcode32
, 16, 16,
1192 translator_lduw(env
, &ctx
->base
,
1193 ctx
->base
.pc_next
+ 2));
1194 ctx
->opcode
= opcode32
;
1196 for (guint i
= 0; i
< ctx
->decoders
->len
; ++i
) {
1197 riscv_cpu_decode_fn func
= g_ptr_array_index(ctx
->decoders
, i
);
1198 if (func(ctx
, opcode32
)) {
1204 gen_exception_illegal(ctx
);
1207 static void riscv_tr_init_disas_context(DisasContextBase
*dcbase
, CPUState
*cs
)
1209 DisasContext
*ctx
= container_of(dcbase
, DisasContext
, base
);
1210 CPURISCVState
*env
= cpu_env(cs
);
1211 RISCVCPUClass
*mcc
= RISCV_CPU_GET_CLASS(cs
);
1212 RISCVCPU
*cpu
= RISCV_CPU(cs
);
1213 uint32_t tb_flags
= ctx
->base
.tb
->flags
;
1215 ctx
->pc_save
= ctx
->base
.pc_first
;
1216 ctx
->priv
= FIELD_EX32(tb_flags
, TB_FLAGS
, PRIV
);
1217 ctx
->mem_idx
= FIELD_EX32(tb_flags
, TB_FLAGS
, MEM_IDX
);
1218 ctx
->mstatus_fs
= FIELD_EX32(tb_flags
, TB_FLAGS
, FS
);
1219 ctx
->mstatus_vs
= FIELD_EX32(tb_flags
, TB_FLAGS
, VS
);
1220 ctx
->priv_ver
= env
->priv_ver
;
1221 ctx
->virt_enabled
= FIELD_EX32(tb_flags
, TB_FLAGS
, VIRT_ENABLED
);
1222 ctx
->misa_ext
= env
->misa_ext
;
1223 ctx
->frm
= -1; /* unknown rounding mode */
1224 ctx
->cfg_ptr
= &(cpu
->cfg
);
1225 ctx
->vill
= FIELD_EX32(tb_flags
, TB_FLAGS
, VILL
);
1226 ctx
->sew
= FIELD_EX32(tb_flags
, TB_FLAGS
, SEW
);
1227 ctx
->lmul
= sextract32(FIELD_EX32(tb_flags
, TB_FLAGS
, LMUL
), 0, 3);
1228 ctx
->vta
= FIELD_EX32(tb_flags
, TB_FLAGS
, VTA
) && cpu
->cfg
.rvv_ta_all_1s
;
1229 ctx
->vma
= FIELD_EX32(tb_flags
, TB_FLAGS
, VMA
) && cpu
->cfg
.rvv_ma_all_1s
;
1230 ctx
->cfg_vta_all_1s
= cpu
->cfg
.rvv_ta_all_1s
;
1231 ctx
->vstart_eq_zero
= FIELD_EX32(tb_flags
, TB_FLAGS
, VSTART_EQ_ZERO
);
1232 ctx
->vl_eq_vlmax
= FIELD_EX32(tb_flags
, TB_FLAGS
, VL_EQ_VLMAX
);
1233 ctx
->misa_mxl_max
= mcc
->misa_mxl_max
;
1234 ctx
->xl
= FIELD_EX32(tb_flags
, TB_FLAGS
, XL
);
1235 ctx
->address_xl
= FIELD_EX32(tb_flags
, TB_FLAGS
, AXL
);
1237 ctx
->pm_mask_enabled
= FIELD_EX32(tb_flags
, TB_FLAGS
, PM_MASK_ENABLED
);
1238 ctx
->pm_base_enabled
= FIELD_EX32(tb_flags
, TB_FLAGS
, PM_BASE_ENABLED
);
1239 ctx
->ztso
= cpu
->cfg
.ext_ztso
;
1240 ctx
->itrigger
= FIELD_EX32(tb_flags
, TB_FLAGS
, ITRIGGER
);
1241 ctx
->zero
= tcg_constant_tl(0);
1242 ctx
->virt_inst_excp
= false;
1243 ctx
->decoders
= cpu
->decoders
;
1246 static void riscv_tr_tb_start(DisasContextBase
*db
, CPUState
*cpu
)
1250 static void riscv_tr_insn_start(DisasContextBase
*dcbase
, CPUState
*cpu
)
1252 DisasContext
*ctx
= container_of(dcbase
, DisasContext
, base
);
1253 target_ulong pc_next
= ctx
->base
.pc_next
;
1255 if (tb_cflags(dcbase
->tb
) & CF_PCREL
) {
1256 pc_next
&= ~TARGET_PAGE_MASK
;
1259 tcg_gen_insn_start(pc_next
, 0);
1260 ctx
->insn_start_updated
= false;
1263 static void riscv_tr_translate_insn(DisasContextBase
*dcbase
, CPUState
*cpu
)
1265 DisasContext
*ctx
= container_of(dcbase
, DisasContext
, base
);
1266 CPURISCVState
*env
= cpu_env(cpu
);
1267 uint16_t opcode16
= translator_lduw(env
, &ctx
->base
, ctx
->base
.pc_next
);
1270 decode_opc(env
, ctx
, opcode16
);
1271 ctx
->base
.pc_next
+= ctx
->cur_insn_len
;
1273 /* Only the first insn within a TB is allowed to cross a page boundary. */
1274 if (ctx
->base
.is_jmp
== DISAS_NEXT
) {
1275 if (ctx
->itrigger
|| !is_same_page(&ctx
->base
, ctx
->base
.pc_next
)) {
1276 ctx
->base
.is_jmp
= DISAS_TOO_MANY
;
1278 unsigned page_ofs
= ctx
->base
.pc_next
& ~TARGET_PAGE_MASK
;
1280 if (page_ofs
> TARGET_PAGE_SIZE
- MAX_INSN_LEN
) {
1281 uint16_t next_insn
=
1282 translator_lduw(env
, &ctx
->base
, ctx
->base
.pc_next
);
1283 int len
= insn_len(next_insn
);
1285 if (!is_same_page(&ctx
->base
, ctx
->base
.pc_next
+ len
- 1)) {
1286 ctx
->base
.is_jmp
= DISAS_TOO_MANY
;
1293 static void riscv_tr_tb_stop(DisasContextBase
*dcbase
, CPUState
*cpu
)
1295 DisasContext
*ctx
= container_of(dcbase
, DisasContext
, base
);
1297 switch (ctx
->base
.is_jmp
) {
1298 case DISAS_TOO_MANY
:
1299 gen_goto_tb(ctx
, 0, 0);
1301 case DISAS_NORETURN
:
1304 g_assert_not_reached();
1308 static const TranslatorOps riscv_tr_ops
= {
1309 .init_disas_context
= riscv_tr_init_disas_context
,
1310 .tb_start
= riscv_tr_tb_start
,
1311 .insn_start
= riscv_tr_insn_start
,
1312 .translate_insn
= riscv_tr_translate_insn
,
1313 .tb_stop
= riscv_tr_tb_stop
,
1316 void gen_intermediate_code(CPUState
*cs
, TranslationBlock
*tb
, int *max_insns
,
1317 vaddr pc
, void *host_pc
)
1321 translator_loop(cs
, tb
, max_insns
, pc
, host_pc
, &riscv_tr_ops
, &ctx
.base
);
1324 void riscv_translate_init(void)
1329 * cpu_gpr[0] is a placeholder for the zero register. Do not use it.
1330 * Use the gen_set_gpr and get_gpr helper functions when accessing regs,
1331 * unless you specifically block reads/writes to reg 0.
1336 for (i
= 1; i
< 32; i
++) {
1337 cpu_gpr
[i
] = tcg_global_mem_new(tcg_env
,
1338 offsetof(CPURISCVState
, gpr
[i
]), riscv_int_regnames
[i
]);
1339 cpu_gprh
[i
] = tcg_global_mem_new(tcg_env
,
1340 offsetof(CPURISCVState
, gprh
[i
]), riscv_int_regnamesh
[i
]);
1343 for (i
= 0; i
< 32; i
++) {
1344 cpu_fpr
[i
] = tcg_global_mem_new_i64(tcg_env
,
1345 offsetof(CPURISCVState
, fpr
[i
]), riscv_fpr_regnames
[i
]);
1348 cpu_pc
= tcg_global_mem_new(tcg_env
, offsetof(CPURISCVState
, pc
), "pc");
1349 cpu_vl
= tcg_global_mem_new(tcg_env
, offsetof(CPURISCVState
, vl
), "vl");
1350 cpu_vstart
= tcg_global_mem_new(tcg_env
, offsetof(CPURISCVState
, vstart
),
1352 load_res
= tcg_global_mem_new(tcg_env
, offsetof(CPURISCVState
, load_res
),
1354 load_val
= tcg_global_mem_new(tcg_env
, offsetof(CPURISCVState
, load_val
),
1356 /* Assign PM CSRs to tcg globals */
1357 pm_mask
= tcg_global_mem_new(tcg_env
, offsetof(CPURISCVState
, cur_pmmask
),
1359 pm_base
= tcg_global_mem_new(tcg_env
, offsetof(CPURISCVState
, cur_pmbase
),