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
;
36 static TCGv_i64 cpu_fpr
[32]; /* assume F and D extensions */
40 #include "exec/gen-icount.h"
42 typedef struct DisasContext
{
43 DisasContextBase base
;
44 /* pc_succ_insn points to the instruction following base.pc_next */
45 target_ulong pc_succ_insn
;
46 target_ulong priv_ver
;
51 /* Remember the rounding mode encoded in the previous fp instruction,
52 which we have already installed into env->fp_status. Or -1 for
53 no previous fp instruction. Note that we exit the TB when writing
54 to any system register, which includes CSR_FRM, so we do not have
55 to reset this known value. */
61 /* convert riscv funct3 to qemu memop for load/store */
62 static const int tcg_memop_lookup
[8] = {
75 #define CASE_OP_32_64(X) case X: case glue(X, W)
77 #define CASE_OP_32_64(X) case X
80 static inline bool has_ext(DisasContext
*ctx
, uint32_t ext
)
82 return ctx
->misa
& ext
;
85 static void generate_exception(DisasContext
*ctx
, int excp
)
87 tcg_gen_movi_tl(cpu_pc
, ctx
->base
.pc_next
);
88 TCGv_i32 helper_tmp
= tcg_const_i32(excp
);
89 gen_helper_raise_exception(cpu_env
, helper_tmp
);
90 tcg_temp_free_i32(helper_tmp
);
91 ctx
->base
.is_jmp
= DISAS_NORETURN
;
94 static void generate_exception_mbadaddr(DisasContext
*ctx
, int excp
)
96 tcg_gen_movi_tl(cpu_pc
, ctx
->base
.pc_next
);
97 tcg_gen_st_tl(cpu_pc
, cpu_env
, offsetof(CPURISCVState
, badaddr
));
98 TCGv_i32 helper_tmp
= tcg_const_i32(excp
);
99 gen_helper_raise_exception(cpu_env
, helper_tmp
);
100 tcg_temp_free_i32(helper_tmp
);
101 ctx
->base
.is_jmp
= DISAS_NORETURN
;
104 static void gen_exception_debug(void)
106 TCGv_i32 helper_tmp
= tcg_const_i32(EXCP_DEBUG
);
107 gen_helper_raise_exception(cpu_env
, helper_tmp
);
108 tcg_temp_free_i32(helper_tmp
);
111 /* Wrapper around tcg_gen_exit_tb that handles single stepping */
112 static void exit_tb(DisasContext
*ctx
)
114 if (ctx
->base
.singlestep_enabled
) {
115 gen_exception_debug();
117 tcg_gen_exit_tb(NULL
, 0);
121 /* Wrapper around tcg_gen_lookup_and_goto_ptr that handles single stepping */
122 static void lookup_and_goto_ptr(DisasContext
*ctx
)
124 if (ctx
->base
.singlestep_enabled
) {
125 gen_exception_debug();
127 tcg_gen_lookup_and_goto_ptr();
131 static void gen_exception_illegal(DisasContext
*ctx
)
133 generate_exception(ctx
, RISCV_EXCP_ILLEGAL_INST
);
136 static void gen_exception_inst_addr_mis(DisasContext
*ctx
)
138 generate_exception_mbadaddr(ctx
, RISCV_EXCP_INST_ADDR_MIS
);
141 static inline bool use_goto_tb(DisasContext
*ctx
, target_ulong dest
)
143 if (unlikely(ctx
->base
.singlestep_enabled
)) {
147 #ifndef CONFIG_USER_ONLY
148 return (ctx
->base
.tb
->pc
& TARGET_PAGE_MASK
) == (dest
& TARGET_PAGE_MASK
);
154 static void gen_goto_tb(DisasContext
*ctx
, int n
, target_ulong dest
)
156 if (use_goto_tb(ctx
, dest
)) {
157 /* chaining is only allowed when the jump is to the same page */
159 tcg_gen_movi_tl(cpu_pc
, dest
);
161 /* No need to check for single stepping here as use_goto_tb() will
162 * return false in case of single stepping.
164 tcg_gen_exit_tb(ctx
->base
.tb
, n
);
166 tcg_gen_movi_tl(cpu_pc
, dest
);
167 lookup_and_goto_ptr(ctx
);
171 /* Wrapper for getting reg values - need to check of reg is zero since
172 * cpu_gpr[0] is not actually allocated
174 static inline void gen_get_gpr(TCGv t
, int reg_num
)
177 tcg_gen_movi_tl(t
, 0);
179 tcg_gen_mov_tl(t
, cpu_gpr
[reg_num
]);
183 /* Wrapper for setting reg values - need to check of reg is zero since
184 * cpu_gpr[0] is not actually allocated. this is more for safety purposes,
185 * since we usually avoid calling the OP_TYPE_gen function if we see a write to
188 static inline void gen_set_gpr(int reg_num_dst
, TCGv t
)
190 if (reg_num_dst
!= 0) {
191 tcg_gen_mov_tl(cpu_gpr
[reg_num_dst
], t
);
195 static void gen_mulhsu(TCGv ret
, TCGv arg1
, TCGv arg2
)
197 TCGv rl
= tcg_temp_new();
198 TCGv rh
= tcg_temp_new();
200 tcg_gen_mulu2_tl(rl
, rh
, arg1
, arg2
);
201 /* fix up for one negative */
202 tcg_gen_sari_tl(rl
, arg1
, TARGET_LONG_BITS
- 1);
203 tcg_gen_and_tl(rl
, rl
, arg2
);
204 tcg_gen_sub_tl(ret
, rh
, rl
);
210 static void gen_div(TCGv ret
, TCGv source1
, TCGv source2
)
212 TCGv cond1
, cond2
, zeroreg
, resultopt1
;
214 * Handle by altering args to tcg_gen_div to produce req'd results:
215 * For overflow: want source1 in source1 and 1 in source2
216 * For div by zero: want -1 in source1 and 1 in source2 -> -1 result
218 cond1
= tcg_temp_new();
219 cond2
= tcg_temp_new();
220 zeroreg
= tcg_const_tl(0);
221 resultopt1
= tcg_temp_new();
223 tcg_gen_movi_tl(resultopt1
, (target_ulong
)-1);
224 tcg_gen_setcondi_tl(TCG_COND_EQ
, cond2
, source2
, (target_ulong
)(~0L));
225 tcg_gen_setcondi_tl(TCG_COND_EQ
, cond1
, source1
,
226 ((target_ulong
)1) << (TARGET_LONG_BITS
- 1));
227 tcg_gen_and_tl(cond1
, cond1
, cond2
); /* cond1 = overflow */
228 tcg_gen_setcondi_tl(TCG_COND_EQ
, cond2
, source2
, 0); /* cond2 = div 0 */
229 /* if div by zero, set source1 to -1, otherwise don't change */
230 tcg_gen_movcond_tl(TCG_COND_EQ
, source1
, cond2
, zeroreg
, source1
,
232 /* if overflow or div by zero, set source2 to 1, else don't change */
233 tcg_gen_or_tl(cond1
, cond1
, cond2
);
234 tcg_gen_movi_tl(resultopt1
, (target_ulong
)1);
235 tcg_gen_movcond_tl(TCG_COND_EQ
, source2
, cond1
, zeroreg
, source2
,
237 tcg_gen_div_tl(ret
, source1
, source2
);
239 tcg_temp_free(cond1
);
240 tcg_temp_free(cond2
);
241 tcg_temp_free(zeroreg
);
242 tcg_temp_free(resultopt1
);
245 static void gen_divu(TCGv ret
, TCGv source1
, TCGv source2
)
247 TCGv cond1
, zeroreg
, resultopt1
;
248 cond1
= tcg_temp_new();
250 zeroreg
= tcg_const_tl(0);
251 resultopt1
= tcg_temp_new();
253 tcg_gen_setcondi_tl(TCG_COND_EQ
, cond1
, source2
, 0);
254 tcg_gen_movi_tl(resultopt1
, (target_ulong
)-1);
255 tcg_gen_movcond_tl(TCG_COND_EQ
, source1
, cond1
, zeroreg
, source1
,
257 tcg_gen_movi_tl(resultopt1
, (target_ulong
)1);
258 tcg_gen_movcond_tl(TCG_COND_EQ
, source2
, cond1
, zeroreg
, source2
,
260 tcg_gen_divu_tl(ret
, source1
, source2
);
262 tcg_temp_free(cond1
);
263 tcg_temp_free(zeroreg
);
264 tcg_temp_free(resultopt1
);
267 static void gen_rem(TCGv ret
, TCGv source1
, TCGv source2
)
269 TCGv cond1
, cond2
, zeroreg
, resultopt1
;
271 cond1
= tcg_temp_new();
272 cond2
= tcg_temp_new();
273 zeroreg
= tcg_const_tl(0);
274 resultopt1
= tcg_temp_new();
276 tcg_gen_movi_tl(resultopt1
, 1L);
277 tcg_gen_setcondi_tl(TCG_COND_EQ
, cond2
, source2
, (target_ulong
)-1);
278 tcg_gen_setcondi_tl(TCG_COND_EQ
, cond1
, source1
,
279 (target_ulong
)1 << (TARGET_LONG_BITS
- 1));
280 tcg_gen_and_tl(cond2
, cond1
, cond2
); /* cond1 = overflow */
281 tcg_gen_setcondi_tl(TCG_COND_EQ
, cond1
, source2
, 0); /* cond2 = div 0 */
282 /* if overflow or div by zero, set source2 to 1, else don't change */
283 tcg_gen_or_tl(cond2
, cond1
, cond2
);
284 tcg_gen_movcond_tl(TCG_COND_EQ
, source2
, cond2
, zeroreg
, source2
,
286 tcg_gen_rem_tl(resultopt1
, source1
, source2
);
287 /* if div by zero, just return the original dividend */
288 tcg_gen_movcond_tl(TCG_COND_EQ
, ret
, cond1
, zeroreg
, resultopt1
,
291 tcg_temp_free(cond1
);
292 tcg_temp_free(cond2
);
293 tcg_temp_free(zeroreg
);
294 tcg_temp_free(resultopt1
);
297 static void gen_remu(TCGv ret
, TCGv source1
, TCGv source2
)
299 TCGv cond1
, zeroreg
, resultopt1
;
300 cond1
= tcg_temp_new();
301 zeroreg
= tcg_const_tl(0);
302 resultopt1
= tcg_temp_new();
304 tcg_gen_movi_tl(resultopt1
, (target_ulong
)1);
305 tcg_gen_setcondi_tl(TCG_COND_EQ
, cond1
, source2
, 0);
306 tcg_gen_movcond_tl(TCG_COND_EQ
, source2
, cond1
, zeroreg
, source2
,
308 tcg_gen_remu_tl(resultopt1
, source1
, source2
);
309 /* if div by zero, just return the original dividend */
310 tcg_gen_movcond_tl(TCG_COND_EQ
, ret
, cond1
, zeroreg
, resultopt1
,
313 tcg_temp_free(cond1
);
314 tcg_temp_free(zeroreg
);
315 tcg_temp_free(resultopt1
);
318 static void gen_jal(DisasContext
*ctx
, int rd
, target_ulong imm
)
320 target_ulong next_pc
;
322 /* check misaligned: */
323 next_pc
= ctx
->base
.pc_next
+ imm
;
324 if (!has_ext(ctx
, RVC
)) {
325 if ((next_pc
& 0x3) != 0) {
326 gen_exception_inst_addr_mis(ctx
);
331 tcg_gen_movi_tl(cpu_gpr
[rd
], ctx
->pc_succ_insn
);
334 gen_goto_tb(ctx
, 0, ctx
->base
.pc_next
+ imm
); /* must use this for safety */
335 ctx
->base
.is_jmp
= DISAS_NORETURN
;
338 #ifdef TARGET_RISCV64
339 static void gen_load_c(DisasContext
*ctx
, uint32_t opc
, int rd
, int rs1
,
342 TCGv t0
= tcg_temp_new();
343 TCGv t1
= tcg_temp_new();
344 gen_get_gpr(t0
, rs1
);
345 tcg_gen_addi_tl(t0
, t0
, imm
);
346 int memop
= tcg_memop_lookup
[(opc
>> 12) & 0x7];
349 gen_exception_illegal(ctx
);
353 tcg_gen_qemu_ld_tl(t1
, t0
, ctx
->mem_idx
, memop
);
359 static void gen_store_c(DisasContext
*ctx
, uint32_t opc
, int rs1
, int rs2
,
362 TCGv t0
= tcg_temp_new();
363 TCGv dat
= tcg_temp_new();
364 gen_get_gpr(t0
, rs1
);
365 tcg_gen_addi_tl(t0
, t0
, imm
);
366 gen_get_gpr(dat
, rs2
);
367 int memop
= tcg_memop_lookup
[(opc
>> 12) & 0x7];
370 gen_exception_illegal(ctx
);
374 tcg_gen_qemu_st_tl(dat
, t0
, ctx
->mem_idx
, memop
);
380 #ifndef CONFIG_USER_ONLY
381 /* The states of mstatus_fs are:
382 * 0 = disabled, 1 = initial, 2 = clean, 3 = dirty
383 * We will have already diagnosed disabled state,
384 * and need to turn initial/clean into dirty.
386 static void mark_fs_dirty(DisasContext
*ctx
)
389 if (ctx
->mstatus_fs
== MSTATUS_FS
) {
392 /* Remember the state change for the rest of the TB. */
393 ctx
->mstatus_fs
= MSTATUS_FS
;
395 tmp
= tcg_temp_new();
396 tcg_gen_ld_tl(tmp
, cpu_env
, offsetof(CPURISCVState
, mstatus
));
397 tcg_gen_ori_tl(tmp
, tmp
, MSTATUS_FS
);
398 tcg_gen_st_tl(tmp
, cpu_env
, offsetof(CPURISCVState
, mstatus
));
402 static inline void mark_fs_dirty(DisasContext
*ctx
) { }
405 #if !defined(TARGET_RISCV64)
406 static void gen_fp_load(DisasContext
*ctx
, uint32_t opc
, int rd
,
407 int rs1
, target_long imm
)
411 if (ctx
->mstatus_fs
== 0) {
412 gen_exception_illegal(ctx
);
417 gen_get_gpr(t0
, rs1
);
418 tcg_gen_addi_tl(t0
, t0
, imm
);
422 if (!has_ext(ctx
, RVF
)) {
425 tcg_gen_qemu_ld_i64(cpu_fpr
[rd
], t0
, ctx
->mem_idx
, MO_TEUL
);
426 /* RISC-V requires NaN-boxing of narrower width floating point values */
427 tcg_gen_ori_i64(cpu_fpr
[rd
], cpu_fpr
[rd
], 0xffffffff00000000ULL
);
430 if (!has_ext(ctx
, RVD
)) {
433 tcg_gen_qemu_ld_i64(cpu_fpr
[rd
], t0
, ctx
->mem_idx
, MO_TEQ
);
437 gen_exception_illegal(ctx
);
445 static void gen_fp_store(DisasContext
*ctx
, uint32_t opc
, int rs1
,
446 int rs2
, target_long imm
)
450 if (ctx
->mstatus_fs
== 0) {
451 gen_exception_illegal(ctx
);
456 gen_get_gpr(t0
, rs1
);
457 tcg_gen_addi_tl(t0
, t0
, imm
);
461 if (!has_ext(ctx
, RVF
)) {
464 tcg_gen_qemu_st_i64(cpu_fpr
[rs2
], t0
, ctx
->mem_idx
, MO_TEUL
);
467 if (!has_ext(ctx
, RVD
)) {
470 tcg_gen_qemu_st_i64(cpu_fpr
[rs2
], t0
, ctx
->mem_idx
, MO_TEQ
);
474 gen_exception_illegal(ctx
);
482 static void gen_set_rm(DisasContext
*ctx
, int rm
)
486 if (ctx
->frm
== rm
) {
490 t0
= tcg_const_i32(rm
);
491 gen_helper_set_rounding_mode(cpu_env
, t0
);
492 tcg_temp_free_i32(t0
);
495 static void decode_RV32_64C0(DisasContext
*ctx
)
497 uint8_t funct3
= extract32(ctx
->opcode
, 13, 3);
498 uint8_t rd_rs2
= GET_C_RS2S(ctx
->opcode
);
499 uint8_t rs1s
= GET_C_RS1S(ctx
->opcode
);
503 #if defined(TARGET_RISCV64)
504 /* C.LD(RV64/128) -> ld rd', offset[7:3](rs1')*/
505 gen_load_c(ctx
, OPC_RISC_LD
, rd_rs2
, rs1s
,
506 GET_C_LD_IMM(ctx
->opcode
));
508 /* C.FLW (RV32) -> flw rd', offset[6:2](rs1')*/
509 gen_fp_load(ctx
, OPC_RISC_FLW
, rd_rs2
, rs1s
,
510 GET_C_LW_IMM(ctx
->opcode
));
514 #if defined(TARGET_RISCV64)
515 /* C.SD (RV64/128) -> sd rs2', offset[7:3](rs1')*/
516 gen_store_c(ctx
, OPC_RISC_SD
, rs1s
, rd_rs2
,
517 GET_C_LD_IMM(ctx
->opcode
));
519 /* C.FSW (RV32) -> fsw rs2', offset[6:2](rs1')*/
520 gen_fp_store(ctx
, OPC_RISC_FSW
, rs1s
, rd_rs2
,
521 GET_C_LW_IMM(ctx
->opcode
));
527 static void decode_RV32_64C(DisasContext
*ctx
)
529 uint8_t op
= extract32(ctx
->opcode
, 0, 2);
533 decode_RV32_64C0(ctx
);
538 #define EX_SH(amount) \
539 static int ex_shift_##amount(DisasContext *ctx, int imm) \
541 return imm << amount; \
549 #define REQUIRE_EXT(ctx, ext) do { \
550 if (!has_ext(ctx, ext)) { \
555 static int ex_rvc_register(DisasContext
*ctx
, int reg
)
560 static int ex_rvc_shifti(DisasContext
*ctx
, int imm
)
562 /* For RV128 a shamt of 0 means a shift by 64. */
563 return imm
? imm
: 64;
566 /* Include the auto-generated decoder for 32 bit insn */
567 #include "decode_insn32.inc.c"
569 static bool gen_arith_imm_fn(DisasContext
*ctx
, arg_i
*a
,
570 void (*func
)(TCGv
, TCGv
, target_long
))
573 source1
= tcg_temp_new();
575 gen_get_gpr(source1
, a
->rs1
);
577 (*func
)(source1
, source1
, a
->imm
);
579 gen_set_gpr(a
->rd
, source1
);
580 tcg_temp_free(source1
);
584 static bool gen_arith_imm_tl(DisasContext
*ctx
, arg_i
*a
,
585 void (*func
)(TCGv
, TCGv
, TCGv
))
587 TCGv source1
, source2
;
588 source1
= tcg_temp_new();
589 source2
= tcg_temp_new();
591 gen_get_gpr(source1
, a
->rs1
);
592 tcg_gen_movi_tl(source2
, a
->imm
);
594 (*func
)(source1
, source1
, source2
);
596 gen_set_gpr(a
->rd
, source1
);
597 tcg_temp_free(source1
);
598 tcg_temp_free(source2
);
602 #ifdef TARGET_RISCV64
603 static void gen_addw(TCGv ret
, TCGv arg1
, TCGv arg2
)
605 tcg_gen_add_tl(ret
, arg1
, arg2
);
606 tcg_gen_ext32s_tl(ret
, ret
);
609 static void gen_subw(TCGv ret
, TCGv arg1
, TCGv arg2
)
611 tcg_gen_sub_tl(ret
, arg1
, arg2
);
612 tcg_gen_ext32s_tl(ret
, ret
);
615 static void gen_mulw(TCGv ret
, TCGv arg1
, TCGv arg2
)
617 tcg_gen_mul_tl(ret
, arg1
, arg2
);
618 tcg_gen_ext32s_tl(ret
, ret
);
621 static bool gen_arith_div_w(DisasContext
*ctx
, arg_r
*a
,
622 void(*func
)(TCGv
, TCGv
, TCGv
))
624 TCGv source1
, source2
;
625 source1
= tcg_temp_new();
626 source2
= tcg_temp_new();
628 gen_get_gpr(source1
, a
->rs1
);
629 gen_get_gpr(source2
, a
->rs2
);
630 tcg_gen_ext32s_tl(source1
, source1
);
631 tcg_gen_ext32s_tl(source2
, source2
);
633 (*func
)(source1
, source1
, source2
);
635 tcg_gen_ext32s_tl(source1
, source1
);
636 gen_set_gpr(a
->rd
, source1
);
637 tcg_temp_free(source1
);
638 tcg_temp_free(source2
);
642 static bool gen_arith_div_uw(DisasContext
*ctx
, arg_r
*a
,
643 void(*func
)(TCGv
, TCGv
, TCGv
))
645 TCGv source1
, source2
;
646 source1
= tcg_temp_new();
647 source2
= tcg_temp_new();
649 gen_get_gpr(source1
, a
->rs1
);
650 gen_get_gpr(source2
, a
->rs2
);
651 tcg_gen_ext32u_tl(source1
, source1
);
652 tcg_gen_ext32u_tl(source2
, source2
);
654 (*func
)(source1
, source1
, source2
);
656 tcg_gen_ext32s_tl(source1
, source1
);
657 gen_set_gpr(a
->rd
, source1
);
658 tcg_temp_free(source1
);
659 tcg_temp_free(source2
);
665 static bool gen_arith(DisasContext
*ctx
, arg_r
*a
,
666 void(*func
)(TCGv
, TCGv
, TCGv
))
668 TCGv source1
, source2
;
669 source1
= tcg_temp_new();
670 source2
= tcg_temp_new();
672 gen_get_gpr(source1
, a
->rs1
);
673 gen_get_gpr(source2
, a
->rs2
);
675 (*func
)(source1
, source1
, source2
);
677 gen_set_gpr(a
->rd
, source1
);
678 tcg_temp_free(source1
);
679 tcg_temp_free(source2
);
683 static bool gen_shift(DisasContext
*ctx
, arg_r
*a
,
684 void(*func
)(TCGv
, TCGv
, TCGv
))
686 TCGv source1
= tcg_temp_new();
687 TCGv source2
= tcg_temp_new();
689 gen_get_gpr(source1
, a
->rs1
);
690 gen_get_gpr(source2
, a
->rs2
);
692 tcg_gen_andi_tl(source2
, source2
, TARGET_LONG_BITS
- 1);
693 (*func
)(source1
, source1
, source2
);
695 gen_set_gpr(a
->rd
, source1
);
696 tcg_temp_free(source1
);
697 tcg_temp_free(source2
);
701 /* Include insn module translation function */
702 #include "insn_trans/trans_rvi.inc.c"
703 #include "insn_trans/trans_rvm.inc.c"
704 #include "insn_trans/trans_rva.inc.c"
705 #include "insn_trans/trans_rvf.inc.c"
706 #include "insn_trans/trans_rvd.inc.c"
707 #include "insn_trans/trans_privileged.inc.c"
709 /* Include the auto-generated decoder for 16 bit insn */
710 #include "decode_insn16.inc.c"
712 static void decode_opc(DisasContext
*ctx
)
714 /* check for compressed insn */
715 if (extract32(ctx
->opcode
, 0, 2) != 3) {
716 if (!has_ext(ctx
, RVC
)) {
717 gen_exception_illegal(ctx
);
719 ctx
->pc_succ_insn
= ctx
->base
.pc_next
+ 2;
720 if (!decode_insn16(ctx
, ctx
->opcode
)) {
721 /* fall back to old decoder */
722 decode_RV32_64C(ctx
);
726 ctx
->pc_succ_insn
= ctx
->base
.pc_next
+ 4;
727 if (!decode_insn32(ctx
, ctx
->opcode
)) {
728 gen_exception_illegal(ctx
);
733 static void riscv_tr_init_disas_context(DisasContextBase
*dcbase
, CPUState
*cs
)
735 DisasContext
*ctx
= container_of(dcbase
, DisasContext
, base
);
736 CPURISCVState
*env
= cs
->env_ptr
;
737 RISCVCPU
*cpu
= RISCV_CPU(cs
);
739 ctx
->pc_succ_insn
= ctx
->base
.pc_first
;
740 ctx
->mem_idx
= ctx
->base
.tb
->flags
& TB_FLAGS_MMU_MASK
;
741 ctx
->mstatus_fs
= ctx
->base
.tb
->flags
& TB_FLAGS_MSTATUS_FS
;
742 ctx
->priv_ver
= env
->priv_ver
;
743 ctx
->misa
= env
->misa
;
744 ctx
->frm
= -1; /* unknown rounding mode */
745 ctx
->ext_ifencei
= cpu
->cfg
.ext_ifencei
;
748 static void riscv_tr_tb_start(DisasContextBase
*db
, CPUState
*cpu
)
752 static void riscv_tr_insn_start(DisasContextBase
*dcbase
, CPUState
*cpu
)
754 DisasContext
*ctx
= container_of(dcbase
, DisasContext
, base
);
756 tcg_gen_insn_start(ctx
->base
.pc_next
);
759 static bool riscv_tr_breakpoint_check(DisasContextBase
*dcbase
, CPUState
*cpu
,
760 const CPUBreakpoint
*bp
)
762 DisasContext
*ctx
= container_of(dcbase
, DisasContext
, base
);
764 tcg_gen_movi_tl(cpu_pc
, ctx
->base
.pc_next
);
765 ctx
->base
.is_jmp
= DISAS_NORETURN
;
766 gen_exception_debug();
767 /* The address covered by the breakpoint must be included in
768 [tb->pc, tb->pc + tb->size) in order to for it to be
769 properly cleared -- thus we increment the PC here so that
770 the logic setting tb->size below does the right thing. */
771 ctx
->base
.pc_next
+= 4;
775 static void riscv_tr_translate_insn(DisasContextBase
*dcbase
, CPUState
*cpu
)
777 DisasContext
*ctx
= container_of(dcbase
, DisasContext
, base
);
778 CPURISCVState
*env
= cpu
->env_ptr
;
780 ctx
->opcode
= translator_ldl(env
, ctx
->base
.pc_next
);
782 ctx
->base
.pc_next
= ctx
->pc_succ_insn
;
784 if (ctx
->base
.is_jmp
== DISAS_NEXT
) {
785 target_ulong page_start
;
787 page_start
= ctx
->base
.pc_first
& TARGET_PAGE_MASK
;
788 if (ctx
->base
.pc_next
- page_start
>= TARGET_PAGE_SIZE
) {
789 ctx
->base
.is_jmp
= DISAS_TOO_MANY
;
794 static void riscv_tr_tb_stop(DisasContextBase
*dcbase
, CPUState
*cpu
)
796 DisasContext
*ctx
= container_of(dcbase
, DisasContext
, base
);
798 switch (ctx
->base
.is_jmp
) {
800 gen_goto_tb(ctx
, 0, ctx
->base
.pc_next
);
805 g_assert_not_reached();
809 static void riscv_tr_disas_log(const DisasContextBase
*dcbase
, CPUState
*cpu
)
811 qemu_log("IN: %s\n", lookup_symbol(dcbase
->pc_first
));
812 log_target_disas(cpu
, dcbase
->pc_first
, dcbase
->tb
->size
);
815 static const TranslatorOps riscv_tr_ops
= {
816 .init_disas_context
= riscv_tr_init_disas_context
,
817 .tb_start
= riscv_tr_tb_start
,
818 .insn_start
= riscv_tr_insn_start
,
819 .breakpoint_check
= riscv_tr_breakpoint_check
,
820 .translate_insn
= riscv_tr_translate_insn
,
821 .tb_stop
= riscv_tr_tb_stop
,
822 .disas_log
= riscv_tr_disas_log
,
825 void gen_intermediate_code(CPUState
*cs
, TranslationBlock
*tb
, int max_insns
)
829 translator_loop(&riscv_tr_ops
, &ctx
.base
, cs
, tb
, max_insns
);
832 void riscv_translate_init(void)
836 /* cpu_gpr[0] is a placeholder for the zero register. Do not use it. */
837 /* Use the gen_set_gpr and gen_get_gpr helper functions when accessing */
838 /* registers, unless you specifically block reads/writes to reg 0 */
841 for (i
= 1; i
< 32; i
++) {
842 cpu_gpr
[i
] = tcg_global_mem_new(cpu_env
,
843 offsetof(CPURISCVState
, gpr
[i
]), riscv_int_regnames
[i
]);
846 for (i
= 0; i
< 32; i
++) {
847 cpu_fpr
[i
] = tcg_global_mem_new_i64(cpu_env
,
848 offsetof(CPURISCVState
, fpr
[i
]), riscv_fpr_regnames
[i
]);
851 cpu_pc
= tcg_global_mem_new(cpu_env
, offsetof(CPURISCVState
, pc
), "pc");
852 load_res
= tcg_global_mem_new(cpu_env
, offsetof(CPURISCVState
, load_res
),
854 load_val
= tcg_global_mem_new(cpu_env
, offsetof(CPURISCVState
, load_val
),