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 "disas/disas.h"
24 #include "exec/cpu_ldst.h"
25 #include "exec/exec-all.h"
26 #include "exec/helper-proto.h"
27 #include "exec/helper-gen.h"
29 #include "exec/translator.h"
34 /* global register indices */
35 static TCGv cpu_gpr
[32], cpu_pc
, cpu_vl
;
36 static TCGv_i64 cpu_fpr
[32]; /* assume F and D extensions */
40 #include "exec/gen-icount.h"
43 * If an operation is being performed on less than TARGET_LONG_BITS,
44 * it may require the inputs to be sign- or zero-extended; which will
45 * depend on the exact operation being performed.
53 typedef struct DisasContext
{
54 DisasContextBase base
;
55 /* pc_succ_insn points to the instruction following base.pc_next */
56 target_ulong pc_succ_insn
;
57 target_ulong priv_ver
;
62 /* Remember the rounding mode encoded in the previous fp instruction,
63 which we have already installed into env->fp_status. Or -1 for
64 no previous fp instruction. Note that we exit the TB when writing
65 to any system register, which includes CSR_FRM, so we do not have
66 to reset this known value. */
72 /* vector extension */
82 /* Space for 3 operands plus 1 extra for address computation. */
86 static inline bool has_ext(DisasContext
*ctx
, uint32_t ext
)
88 return ctx
->misa
& ext
;
92 # define is_32bit(ctx) true
93 #elif defined(CONFIG_USER_ONLY)
94 # define is_32bit(ctx) false
96 static inline bool is_32bit(DisasContext
*ctx
)
98 return (ctx
->misa
& RV32
) == RV32
;
102 /* The word size for this operation. */
103 static inline int oper_len(DisasContext
*ctx
)
105 return ctx
->w
? 32 : TARGET_LONG_BITS
;
110 * RISC-V requires NaN-boxing of narrower width floating point values.
111 * This applies when a 32-bit value is assigned to a 64-bit FP register.
112 * For consistency and simplicity, we nanbox results even when the RVD
113 * extension is not present.
115 static void gen_nanbox_s(TCGv_i64 out
, TCGv_i64 in
)
117 tcg_gen_ori_i64(out
, in
, MAKE_64BIT_MASK(32, 32));
121 * A narrow n-bit operation, where n < FLEN, checks that input operands
122 * are correctly Nan-boxed, i.e., all upper FLEN - n bits are 1.
123 * If so, the least-significant bits of the input are used, otherwise the
124 * input value is treated as an n-bit canonical NaN (v2.2 section 9.2).
126 * Here, the result is always nan-boxed, even the canonical nan.
128 static void gen_check_nanbox_s(TCGv_i64 out
, TCGv_i64 in
)
130 TCGv_i64 t_max
= tcg_constant_i64(0xffffffff00000000ull
);
131 TCGv_i64 t_nan
= tcg_constant_i64(0xffffffff7fc00000ull
);
133 tcg_gen_movcond_i64(TCG_COND_GEU
, out
, in
, t_max
, in
, t_nan
);
136 static void generate_exception(DisasContext
*ctx
, int excp
)
138 tcg_gen_movi_tl(cpu_pc
, ctx
->base
.pc_next
);
139 gen_helper_raise_exception(cpu_env
, tcg_constant_i32(excp
));
140 ctx
->base
.is_jmp
= DISAS_NORETURN
;
143 static void generate_exception_mtval(DisasContext
*ctx
, int excp
)
145 tcg_gen_movi_tl(cpu_pc
, ctx
->base
.pc_next
);
146 tcg_gen_st_tl(cpu_pc
, cpu_env
, offsetof(CPURISCVState
, badaddr
));
147 gen_helper_raise_exception(cpu_env
, tcg_constant_i32(excp
));
148 ctx
->base
.is_jmp
= DISAS_NORETURN
;
151 static void gen_exception_debug(void)
153 gen_helper_raise_exception(cpu_env
, tcg_constant_i32(EXCP_DEBUG
));
156 /* Wrapper around tcg_gen_exit_tb that handles single stepping */
157 static void exit_tb(DisasContext
*ctx
)
159 if (ctx
->base
.singlestep_enabled
) {
160 gen_exception_debug();
162 tcg_gen_exit_tb(NULL
, 0);
166 /* Wrapper around tcg_gen_lookup_and_goto_ptr that handles single stepping */
167 static void lookup_and_goto_ptr(DisasContext
*ctx
)
169 if (ctx
->base
.singlestep_enabled
) {
170 gen_exception_debug();
172 tcg_gen_lookup_and_goto_ptr();
176 static void gen_exception_illegal(DisasContext
*ctx
)
178 generate_exception(ctx
, RISCV_EXCP_ILLEGAL_INST
);
181 static void gen_exception_inst_addr_mis(DisasContext
*ctx
)
183 generate_exception_mtval(ctx
, RISCV_EXCP_INST_ADDR_MIS
);
186 static void gen_goto_tb(DisasContext
*ctx
, int n
, target_ulong dest
)
188 if (translator_use_goto_tb(&ctx
->base
, dest
)) {
190 tcg_gen_movi_tl(cpu_pc
, dest
);
191 tcg_gen_exit_tb(ctx
->base
.tb
, n
);
193 tcg_gen_movi_tl(cpu_pc
, dest
);
194 lookup_and_goto_ptr(ctx
);
199 * Wrappers for getting reg values.
201 * The $zero register does not have cpu_gpr[0] allocated -- we supply the
202 * constant zero as a source, and an uninitialized sink as destination.
204 * Further, we may provide an extension for word operations.
206 static TCGv
temp_new(DisasContext
*ctx
)
208 assert(ctx
->ntemp
< ARRAY_SIZE(ctx
->temp
));
209 return ctx
->temp
[ctx
->ntemp
++] = tcg_temp_new();
212 static TCGv
get_gpr(DisasContext
*ctx
, int reg_num
, DisasExtend ext
)
220 switch (ctx
->w
? ext
: EXT_NONE
) {
222 return cpu_gpr
[reg_num
];
225 tcg_gen_ext32s_tl(t
, cpu_gpr
[reg_num
]);
229 tcg_gen_ext32u_tl(t
, cpu_gpr
[reg_num
]);
232 g_assert_not_reached();
235 static TCGv
dest_gpr(DisasContext
*ctx
, int reg_num
)
237 if (reg_num
== 0 || ctx
->w
) {
238 return temp_new(ctx
);
240 return cpu_gpr
[reg_num
];
243 static void gen_set_gpr(DisasContext
*ctx
, int reg_num
, TCGv t
)
247 tcg_gen_ext32s_tl(cpu_gpr
[reg_num
], t
);
249 tcg_gen_mov_tl(cpu_gpr
[reg_num
], t
);
254 static void gen_jal(DisasContext
*ctx
, int rd
, target_ulong imm
)
256 target_ulong next_pc
;
258 /* check misaligned: */
259 next_pc
= ctx
->base
.pc_next
+ imm
;
260 if (!has_ext(ctx
, RVC
)) {
261 if ((next_pc
& 0x3) != 0) {
262 gen_exception_inst_addr_mis(ctx
);
267 tcg_gen_movi_tl(cpu_gpr
[rd
], ctx
->pc_succ_insn
);
270 gen_goto_tb(ctx
, 0, ctx
->base
.pc_next
+ imm
); /* must use this for safety */
271 ctx
->base
.is_jmp
= DISAS_NORETURN
;
274 #ifndef CONFIG_USER_ONLY
275 /* The states of mstatus_fs are:
276 * 0 = disabled, 1 = initial, 2 = clean, 3 = dirty
277 * We will have already diagnosed disabled state,
278 * and need to turn initial/clean into dirty.
280 static void mark_fs_dirty(DisasContext
*ctx
)
285 if (ctx
->mstatus_fs
== MSTATUS_FS
) {
288 /* Remember the state change for the rest of the TB. */
289 ctx
->mstatus_fs
= MSTATUS_FS
;
291 tmp
= tcg_temp_new();
292 sd
= is_32bit(ctx
) ? MSTATUS32_SD
: MSTATUS64_SD
;
294 tcg_gen_ld_tl(tmp
, cpu_env
, offsetof(CPURISCVState
, mstatus
));
295 tcg_gen_ori_tl(tmp
, tmp
, MSTATUS_FS
| sd
);
296 tcg_gen_st_tl(tmp
, cpu_env
, offsetof(CPURISCVState
, mstatus
));
298 if (ctx
->virt_enabled
) {
299 tcg_gen_ld_tl(tmp
, cpu_env
, offsetof(CPURISCVState
, mstatus_hs
));
300 tcg_gen_ori_tl(tmp
, tmp
, MSTATUS_FS
| sd
);
301 tcg_gen_st_tl(tmp
, cpu_env
, offsetof(CPURISCVState
, mstatus_hs
));
306 static inline void mark_fs_dirty(DisasContext
*ctx
) { }
309 static void gen_set_rm(DisasContext
*ctx
, int rm
)
311 if (ctx
->frm
== rm
) {
315 gen_helper_set_rounding_mode(cpu_env
, tcg_constant_i32(rm
));
318 static int ex_plus_1(DisasContext
*ctx
, int nf
)
323 #define EX_SH(amount) \
324 static int ex_shift_##amount(DisasContext *ctx, int imm) \
326 return imm << amount; \
334 #define REQUIRE_EXT(ctx, ext) do { \
335 if (!has_ext(ctx, ext)) { \
340 #define REQUIRE_32BIT(ctx) do { \
341 if (!is_32bit(ctx)) { \
346 #define REQUIRE_64BIT(ctx) do { \
347 if (is_32bit(ctx)) { \
352 static int ex_rvc_register(DisasContext
*ctx
, int reg
)
357 static int ex_rvc_shifti(DisasContext
*ctx
, int imm
)
359 /* For RV128 a shamt of 0 means a shift by 64. */
360 return imm
? imm
: 64;
363 /* Include the auto-generated decoder for 32 bit insn */
364 #include "decode-insn32.c.inc"
366 static bool gen_arith_imm_fn(DisasContext
*ctx
, arg_i
*a
, DisasExtend ext
,
367 void (*func
)(TCGv
, TCGv
, target_long
))
369 TCGv dest
= dest_gpr(ctx
, a
->rd
);
370 TCGv src1
= get_gpr(ctx
, a
->rs1
, ext
);
372 func(dest
, src1
, a
->imm
);
374 gen_set_gpr(ctx
, a
->rd
, dest
);
378 static bool gen_arith_imm_tl(DisasContext
*ctx
, arg_i
*a
, DisasExtend ext
,
379 void (*func
)(TCGv
, TCGv
, TCGv
))
381 TCGv dest
= dest_gpr(ctx
, a
->rd
);
382 TCGv src1
= get_gpr(ctx
, a
->rs1
, ext
);
383 TCGv src2
= tcg_constant_tl(a
->imm
);
385 func(dest
, src1
, src2
);
387 gen_set_gpr(ctx
, a
->rd
, dest
);
391 static bool gen_arith(DisasContext
*ctx
, arg_r
*a
, DisasExtend ext
,
392 void (*func
)(TCGv
, TCGv
, TCGv
))
394 TCGv dest
= dest_gpr(ctx
, a
->rd
);
395 TCGv src1
= get_gpr(ctx
, a
->rs1
, ext
);
396 TCGv src2
= get_gpr(ctx
, a
->rs2
, ext
);
398 func(dest
, src1
, src2
);
400 gen_set_gpr(ctx
, a
->rd
, dest
);
404 static bool gen_shift_imm_fn(DisasContext
*ctx
, arg_shift
*a
, DisasExtend ext
,
405 void (*func
)(TCGv
, TCGv
, target_long
))
408 int max_len
= oper_len(ctx
);
410 if (a
->shamt
>= max_len
) {
414 dest
= dest_gpr(ctx
, a
->rd
);
415 src1
= get_gpr(ctx
, a
->rs1
, ext
);
417 func(dest
, src1
, a
->shamt
);
419 gen_set_gpr(ctx
, a
->rd
, dest
);
423 static bool gen_shift_imm_tl(DisasContext
*ctx
, arg_shift
*a
, DisasExtend ext
,
424 void (*func
)(TCGv
, TCGv
, TCGv
))
426 TCGv dest
, src1
, src2
;
427 int max_len
= oper_len(ctx
);
429 if (a
->shamt
>= max_len
) {
433 dest
= dest_gpr(ctx
, a
->rd
);
434 src1
= get_gpr(ctx
, a
->rs1
, ext
);
435 src2
= tcg_constant_tl(a
->shamt
);
437 func(dest
, src1
, src2
);
439 gen_set_gpr(ctx
, a
->rd
, dest
);
443 static bool gen_shift(DisasContext
*ctx
, arg_r
*a
, DisasExtend ext
,
444 void (*func
)(TCGv
, TCGv
, TCGv
))
446 TCGv dest
= dest_gpr(ctx
, a
->rd
);
447 TCGv src1
= get_gpr(ctx
, a
->rs1
, ext
);
448 TCGv src2
= get_gpr(ctx
, a
->rs2
, EXT_NONE
);
449 TCGv ext2
= tcg_temp_new();
451 tcg_gen_andi_tl(ext2
, src2
, oper_len(ctx
) - 1);
452 func(dest
, src1
, ext2
);
454 gen_set_gpr(ctx
, a
->rd
, dest
);
459 static bool gen_unary(DisasContext
*ctx
, arg_r2
*a
, DisasExtend ext
,
460 void (*func
)(TCGv
, TCGv
))
462 TCGv dest
= dest_gpr(ctx
, a
->rd
);
463 TCGv src1
= get_gpr(ctx
, a
->rs1
, ext
);
467 gen_set_gpr(ctx
, a
->rd
, dest
);
471 static uint32_t opcode_at(DisasContextBase
*dcbase
, target_ulong pc
)
473 DisasContext
*ctx
= container_of(dcbase
, DisasContext
, base
);
474 CPUState
*cpu
= ctx
->cs
;
475 CPURISCVState
*env
= cpu
->env_ptr
;
477 return cpu_ldl_code(env
, pc
);
480 /* Include insn module translation function */
481 #include "insn_trans/trans_rvi.c.inc"
482 #include "insn_trans/trans_rvm.c.inc"
483 #include "insn_trans/trans_rva.c.inc"
484 #include "insn_trans/trans_rvf.c.inc"
485 #include "insn_trans/trans_rvd.c.inc"
486 #include "insn_trans/trans_rvh.c.inc"
487 #include "insn_trans/trans_rvv.c.inc"
488 #include "insn_trans/trans_rvb.c.inc"
489 #include "insn_trans/trans_privileged.c.inc"
491 /* Include the auto-generated decoder for 16 bit insn */
492 #include "decode-insn16.c.inc"
494 static void decode_opc(CPURISCVState
*env
, DisasContext
*ctx
, uint16_t opcode
)
496 /* check for compressed insn */
497 if (extract16(opcode
, 0, 2) != 3) {
498 if (!has_ext(ctx
, RVC
)) {
499 gen_exception_illegal(ctx
);
501 ctx
->pc_succ_insn
= ctx
->base
.pc_next
+ 2;
502 if (!decode_insn16(ctx
, opcode
)) {
503 gen_exception_illegal(ctx
);
507 uint32_t opcode32
= opcode
;
508 opcode32
= deposit32(opcode32
, 16, 16,
509 translator_lduw(env
, &ctx
->base
,
510 ctx
->base
.pc_next
+ 2));
511 ctx
->pc_succ_insn
= ctx
->base
.pc_next
+ 4;
512 if (!decode_insn32(ctx
, opcode32
)) {
513 gen_exception_illegal(ctx
);
518 static void riscv_tr_init_disas_context(DisasContextBase
*dcbase
, CPUState
*cs
)
520 DisasContext
*ctx
= container_of(dcbase
, DisasContext
, base
);
521 CPURISCVState
*env
= cs
->env_ptr
;
522 RISCVCPU
*cpu
= RISCV_CPU(cs
);
523 uint32_t tb_flags
= ctx
->base
.tb
->flags
;
525 ctx
->pc_succ_insn
= ctx
->base
.pc_first
;
526 ctx
->mem_idx
= tb_flags
& TB_FLAGS_MMU_MASK
;
527 ctx
->mstatus_fs
= tb_flags
& TB_FLAGS_MSTATUS_FS
;
528 ctx
->priv_ver
= env
->priv_ver
;
529 #if !defined(CONFIG_USER_ONLY)
530 if (riscv_has_ext(env
, RVH
)) {
531 ctx
->virt_enabled
= riscv_cpu_virt_enabled(env
);
533 ctx
->virt_enabled
= false;
536 ctx
->virt_enabled
= false;
538 ctx
->misa
= env
->misa
;
539 ctx
->frm
= -1; /* unknown rounding mode */
540 ctx
->ext_ifencei
= cpu
->cfg
.ext_ifencei
;
541 ctx
->vlen
= cpu
->cfg
.vlen
;
542 ctx
->hlsx
= FIELD_EX32(tb_flags
, TB_FLAGS
, HLSX
);
543 ctx
->vill
= FIELD_EX32(tb_flags
, TB_FLAGS
, VILL
);
544 ctx
->sew
= FIELD_EX32(tb_flags
, TB_FLAGS
, SEW
);
545 ctx
->lmul
= FIELD_EX32(tb_flags
, TB_FLAGS
, LMUL
);
546 ctx
->mlen
= 1 << (ctx
->sew
+ 3 - ctx
->lmul
);
547 ctx
->vl_eq_vlmax
= FIELD_EX32(tb_flags
, TB_FLAGS
, VL_EQ_VLMAX
);
551 memset(ctx
->temp
, 0, sizeof(ctx
->temp
));
553 ctx
->zero
= tcg_constant_tl(0);
556 static void riscv_tr_tb_start(DisasContextBase
*db
, CPUState
*cpu
)
560 static void riscv_tr_insn_start(DisasContextBase
*dcbase
, CPUState
*cpu
)
562 DisasContext
*ctx
= container_of(dcbase
, DisasContext
, base
);
564 tcg_gen_insn_start(ctx
->base
.pc_next
);
567 static void riscv_tr_translate_insn(DisasContextBase
*dcbase
, CPUState
*cpu
)
569 DisasContext
*ctx
= container_of(dcbase
, DisasContext
, base
);
570 CPURISCVState
*env
= cpu
->env_ptr
;
571 uint16_t opcode16
= translator_lduw(env
, &ctx
->base
, ctx
->base
.pc_next
);
573 decode_opc(env
, ctx
, opcode16
);
574 ctx
->base
.pc_next
= ctx
->pc_succ_insn
;
577 for (int i
= ctx
->ntemp
- 1; i
>= 0; --i
) {
578 tcg_temp_free(ctx
->temp
[i
]);
583 if (ctx
->base
.is_jmp
== DISAS_NEXT
) {
584 target_ulong page_start
;
586 page_start
= ctx
->base
.pc_first
& TARGET_PAGE_MASK
;
587 if (ctx
->base
.pc_next
- page_start
>= TARGET_PAGE_SIZE
) {
588 ctx
->base
.is_jmp
= DISAS_TOO_MANY
;
593 static void riscv_tr_tb_stop(DisasContextBase
*dcbase
, CPUState
*cpu
)
595 DisasContext
*ctx
= container_of(dcbase
, DisasContext
, base
);
597 switch (ctx
->base
.is_jmp
) {
599 gen_goto_tb(ctx
, 0, ctx
->base
.pc_next
);
604 g_assert_not_reached();
608 static void riscv_tr_disas_log(const DisasContextBase
*dcbase
, CPUState
*cpu
)
610 #ifndef CONFIG_USER_ONLY
611 RISCVCPU
*rvcpu
= RISCV_CPU(cpu
);
612 CPURISCVState
*env
= &rvcpu
->env
;
615 qemu_log("IN: %s\n", lookup_symbol(dcbase
->pc_first
));
616 #ifndef CONFIG_USER_ONLY
617 qemu_log("Priv: "TARGET_FMT_ld
"; Virt: "TARGET_FMT_ld
"\n", env
->priv
, env
->virt
);
619 log_target_disas(cpu
, dcbase
->pc_first
, dcbase
->tb
->size
);
622 static const TranslatorOps riscv_tr_ops
= {
623 .init_disas_context
= riscv_tr_init_disas_context
,
624 .tb_start
= riscv_tr_tb_start
,
625 .insn_start
= riscv_tr_insn_start
,
626 .translate_insn
= riscv_tr_translate_insn
,
627 .tb_stop
= riscv_tr_tb_stop
,
628 .disas_log
= riscv_tr_disas_log
,
631 void gen_intermediate_code(CPUState
*cs
, TranslationBlock
*tb
, int max_insns
)
635 translator_loop(&riscv_tr_ops
, &ctx
.base
, cs
, tb
, max_insns
);
638 void riscv_translate_init(void)
643 * cpu_gpr[0] is a placeholder for the zero register. Do not use it.
644 * Use the gen_set_gpr and get_gpr helper functions when accessing regs,
645 * unless you specifically block reads/writes to reg 0.
649 for (i
= 1; i
< 32; i
++) {
650 cpu_gpr
[i
] = tcg_global_mem_new(cpu_env
,
651 offsetof(CPURISCVState
, gpr
[i
]), riscv_int_regnames
[i
]);
654 for (i
= 0; i
< 32; i
++) {
655 cpu_fpr
[i
] = tcg_global_mem_new_i64(cpu_env
,
656 offsetof(CPURISCVState
, fpr
[i
]), riscv_fpr_regnames
[i
]);
659 cpu_pc
= tcg_global_mem_new(cpu_env
, offsetof(CPURISCVState
, pc
), "pc");
660 cpu_vl
= tcg_global_mem_new(cpu_env
, offsetof(CPURISCVState
, vl
), "vl");
661 load_res
= tcg_global_mem_new(cpu_env
, offsetof(CPURISCVState
, load_res
),
663 load_val
= tcg_global_mem_new(cpu_env
, offsetof(CPURISCVState
, load_val
),