Merge tag 'pull-request-2024-06-12' of https://gitlab.com/thuth/qemu into staging
[qemu/kevin.git] / target / riscv / translate.c
blob0569224e53aaa47de48428b63da2635ec98506e9
1 /*
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
13 * more details.
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"
20 #include "qemu/log.h"
21 #include "cpu.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"
28 #include "exec/log.h"
29 #include "semihosting/semihost.h"
31 #include "internals.h"
33 #define HELPER_H "helper.h"
34 #include "exec/helper-info.c.inc"
35 #undef HELPER_H
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 */
42 static TCGv load_res;
43 static TCGv load_val;
44 /* globals for PM CSRs */
45 static TCGv pm_mask;
46 static TCGv pm_base;
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.
53 typedef enum {
54 EXT_NONE,
55 EXT_SIGN,
56 EXT_ZERO,
57 } DisasExtend;
59 typedef struct DisasContext {
60 DisasContextBase base;
61 target_ulong cur_insn_len;
62 target_ulong pc_save;
63 target_ulong priv_ver;
64 RISCVMXL misa_mxl_max;
65 RISCVMXL xl;
66 RISCVMXL address_xl;
67 uint32_t misa_ext;
68 uint32_t opcode;
69 RISCVExtStatus mstatus_fs;
70 RISCVExtStatus mstatus_vs;
71 uint32_t mem_idx;
72 uint32_t priv;
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.
80 int frm;
81 RISCVMXL ol;
82 bool virt_inst_excp;
83 bool virt_enabled;
84 const RISCVCPUConfig *cfg_ptr;
85 /* vector extension */
86 bool vill;
88 * Encode LMUL to lmul as follows:
89 * LMUL vlmul lmul
90 * 1 000 0
91 * 2 001 1
92 * 4 010 2
93 * 8 011 3
94 * - 100 -
95 * 1/8 101 -3
96 * 1/4 110 -2
97 * 1/2 111 -1
99 int8_t lmul;
100 uint8_t sew;
101 uint8_t vta;
102 uint8_t vma;
103 bool cfg_vta_all_1s;
104 bool vstart_eq_zero;
105 bool vl_eq_vlmax;
106 CPUState *cs;
107 TCGv zero;
108 /* PointerMasking extension */
109 bool pm_mask_enabled;
110 bool pm_base_enabled;
111 /* Ztso */
112 bool ztso;
113 /* Use icount trigger for native debug */
114 bool itrigger;
115 /* FRM is known to contain a valid value. */
116 bool frm_valid;
117 bool insn_start_updated;
118 const GPtrArray *decoders;
119 } DisasContext;
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
130 #else
131 #define get_xl(ctx) ((ctx)->xl)
132 #endif
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
138 #else
139 #define get_address_xl(ctx) ((ctx)->address_xl)
140 #endif
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
151 #else
152 #define get_ol(ctx) ((ctx)->ol)
153 #endif
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
163 #else
164 #define get_xl_max(ctx) ((ctx)->misa_mxl_max)
165 #endif
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,
215 target_long diff)
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);
225 } else {
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);
252 } else {
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
266 if (ctx->itrigger) {
267 gen_helper_itrigger_match(tcg_env);
269 #endif
270 tcg_gen_lookup_and_goto_ptr();
273 static void exit_tb(DisasContext *ctx)
275 #ifndef CONFIG_USER_ONLY
276 if (ctx->itrigger) {
277 gen_helper_itrigger_match(tcg_env);
279 #endif
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);
302 tcg_gen_goto_tb(n);
303 } else {
304 tcg_gen_goto_tb(n);
305 gen_update_pc(ctx, diff);
307 tcg_gen_exit_tb(ctx->base.tb, n);
308 } else {
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)
324 TCGv t;
326 if (reg_num == 0) {
327 return ctx->zero;
330 switch (get_ol(ctx)) {
331 case MXL_RV32:
332 switch (ext) {
333 case EXT_NONE:
334 break;
335 case EXT_SIGN:
336 t = tcg_temp_new();
337 tcg_gen_ext32s_tl(t, cpu_gpr[reg_num]);
338 return t;
339 case EXT_ZERO:
340 t = tcg_temp_new();
341 tcg_gen_ext32u_tl(t, cpu_gpr[reg_num]);
342 return t;
343 default:
344 g_assert_not_reached();
346 break;
347 case MXL_RV64:
348 case MXL_RV128:
349 break;
350 default:
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);
359 if (reg_num == 0) {
360 return ctx->zero;
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)
375 if (reg_num == 0) {
376 return tcg_temp_new();
378 return cpu_gprh[reg_num];
381 static void gen_set_gpr(DisasContext *ctx, int reg_num, TCGv t)
383 if (reg_num != 0) {
384 switch (get_ol(ctx)) {
385 case MXL_RV32:
386 tcg_gen_ext32s_tl(cpu_gpr[reg_num], t);
387 break;
388 case MXL_RV64:
389 case MXL_RV128:
390 tcg_gen_mov_tl(cpu_gpr[reg_num], t);
391 break;
392 default:
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)
404 if (reg_num != 0) {
405 switch (get_ol(ctx)) {
406 case MXL_RV32:
407 tcg_gen_movi_tl(cpu_gpr[reg_num], (int32_t)imm);
408 break;
409 case MXL_RV64:
410 case MXL_RV128:
411 tcg_gen_movi_tl(cpu_gpr[reg_num], imm);
412 break;
413 default:
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);
426 if (reg_num != 0) {
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];
438 if (reg_num == 0) {
439 return tcg_constant_i64(0);
441 switch (get_xl(ctx)) {
442 case MXL_RV32:
443 #ifdef TARGET_RISCV32
445 TCGv_i64 t = tcg_temp_new_i64();
446 tcg_gen_ext_i32_i64(t, cpu_gpr[reg_num]);
447 return t;
449 #else
450 /* fall through */
451 case MXL_RV64:
452 return cpu_gpr[reg_num];
453 #endif
454 default:
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];
465 if (reg_num == 0) {
466 return tcg_constant_i64(0);
468 switch (get_xl(ctx)) {
469 case MXL_RV32:
471 TCGv_i64 t = tcg_temp_new_i64();
472 tcg_gen_concat_tl_i64(t, cpu_gpr[reg_num], cpu_gpr[reg_num + 1]);
473 return t;
475 #ifdef TARGET_RISCV64
476 case MXL_RV64:
477 return cpu_gpr[reg_num];
478 #endif
479 default:
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];
490 if (reg_num == 0) {
491 return tcg_temp_new_i64();
494 switch (get_xl(ctx)) {
495 case MXL_RV32:
496 return tcg_temp_new_i64();
497 #ifdef TARGET_RISCV64
498 case MXL_RV64:
499 return cpu_gpr[reg_num];
500 #endif
501 default:
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);
511 return;
513 if (reg_num != 0) {
514 switch (get_xl(ctx)) {
515 case MXL_RV32:
516 #ifdef TARGET_RISCV32
517 tcg_gen_extrl_i64_i32(cpu_gpr[reg_num], t);
518 break;
519 #else
520 /* fall through */
521 case MXL_RV64:
522 tcg_gen_mov_i64(cpu_gpr[reg_num], t);
523 break;
524 #endif
525 default:
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);
535 return;
538 if (reg_num != 0) {
539 switch (get_xl(ctx)) {
540 case MXL_RV32:
541 #ifdef TARGET_RISCV32
542 tcg_gen_extr_i64_i32(cpu_gpr[reg_num], cpu_gpr[reg_num + 1], t);
543 break;
544 #else
545 tcg_gen_ext32s_i64(cpu_gpr[reg_num], t);
546 tcg_gen_sari_i64(cpu_gpr[reg_num + 1], t, 32);
547 break;
548 case MXL_RV64:
549 tcg_gen_mov_i64(cpu_gpr[reg_num], t);
550 break;
551 #endif
552 default:
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);
568 return;
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);
595 return addr;
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);
613 return addr;
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)
623 TCGv tmp;
625 if (!has_ext(ctx, RVF)) {
626 return;
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));
645 #else
646 static inline void mark_fs_dirty(DisasContext *ctx) { }
647 #endif
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)
656 TCGv tmp;
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));
674 #else
675 static inline void mark_vs_dirty(DisasContext *ctx) { }
676 #endif
678 static void finalize_rvv_inst(DisasContext *ctx)
680 mark_vs_dirty(ctx);
681 ctx->vstart_eq_zero = true;
684 static void gen_set_rm(DisasContext *ctx, int rm)
686 if (ctx->frm == rm) {
687 return;
689 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) {
704 return;
706 ctx->frm = rm;
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)
716 return nf + 1;
719 #define EX_SH(amount) \
720 static int ex_shift_##amount(DisasContext *ctx, int imm) \
722 return imm << amount; \
724 EX_SH(1)
725 EX_SH(2)
726 EX_SH(3)
727 EX_SH(4)
728 EX_SH(12)
730 #define REQUIRE_EXT(ctx, ext) do { \
731 if (!has_ext(ctx, ext)) { \
732 return false; \
734 } while (0)
736 #define REQUIRE_32BIT(ctx) do { \
737 if (get_xl(ctx) != MXL_RV32) { \
738 return false; \
740 } while (0)
742 #define REQUIRE_64BIT(ctx) do { \
743 if (get_xl(ctx) != MXL_RV64) { \
744 return false; \
746 } while (0)
748 #define REQUIRE_128BIT(ctx) do { \
749 if (get_xl(ctx) != MXL_RV128) { \
750 return false; \
752 } while (0)
754 #define REQUIRE_64_OR_128BIT(ctx) do { \
755 if (get_xl(ctx) == MXL_RV32) { \
756 return false; \
758 } while (0)
760 #define REQUIRE_EITHER_EXT(ctx, A, B) do { \
761 if (!ctx->cfg_ptr->ext_##A && \
762 !ctx->cfg_ptr->ext_##B) { \
763 return false; \
765 } while (0)
767 static int ex_rvc_register(DisasContext *ctx, int reg)
769 return 8 + 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;
783 return imm;
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;
796 return imm;
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);
816 } else {
817 gen_set_gpr(ctx, a->rd, dest);
820 return true;
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);
839 } else {
840 gen_set_gpr(ctx, a->rd, dest);
843 return true;
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);
856 } else {
857 if (f128 == NULL) {
858 return false;
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);
867 return true;
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);
881 } else {
882 if (f128 == NULL) {
883 return false;
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);
893 return true;
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);
907 } else {
908 if (f128 == NULL) {
909 return false;
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);
919 return true;
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) {
930 if (olen == 32) {
931 f_tl = f_32;
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))
943 TCGv dest, src1;
944 int max_len = get_olen(ctx);
946 if (a->shamt >= max_len) {
947 return false;
950 dest = dest_gpr(ctx, a->rd);
951 src1 = get_gpr(ctx, a->rs1, ext);
953 if (max_len < 128) {
954 func(dest, src1, a->shamt);
955 gen_set_gpr(ctx, a->rd, dest);
956 } else {
957 TCGv src1h = get_gprh(ctx, a->rs1);
958 TCGv desth = dest_gprh(ctx, a->rd);
960 if (f128 == NULL) {
961 return false;
963 f128(dest, desth, src1, src1h, a->shamt);
964 gen_set_gpr128(ctx, a->rd, dest, desth);
966 return true;
969 static bool gen_shift_imm_fn_per_ol(DisasContext *ctx, arg_shift *a,
970 DisasExtend ext,
971 void (*f_tl)(TCGv, TCGv, target_long),
972 void (*f_32)(TCGv, TCGv, target_long),
973 void (*f_128)(TCGv, TCGv, TCGv, TCGv,
974 target_long))
976 int olen = get_olen(ctx);
977 if (olen != TARGET_LONG_BITS) {
978 if (olen == 32) {
979 f_tl = f_32;
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) {
994 return false;
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);
1004 return true;
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);
1023 } else {
1024 TCGv src1h = get_gprh(ctx, a->rs1);
1025 TCGv desth = dest_gprh(ctx, a->rd);
1027 if (f128 == NULL) {
1028 return false;
1030 f128(dest, desth, src1, src1h, ext2);
1031 gen_set_gpr128(ctx, a->rd, dest, desth);
1033 return true;
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) {
1043 if (olen == 32) {
1044 f_tl = f_32;
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);
1058 func(dest, src1);
1060 gen_set_gpr(ctx, a->rd, dest);
1061 return true;
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) {
1071 if (olen == 32) {
1072 f_tl = f_32;
1073 } else {
1074 g_assert_not_reached();
1077 return gen_unary(ctx, a, ext, f_tl);
1080 static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)
1082 DisasContext *ctx = container_of(dcbase, DisasContext, base);
1083 CPUState *cpu = ctx->cs;
1084 CPURISCVState *env = cpu_env(cpu);
1086 return translator_ldl(env, &ctx->base, pc);
1089 /* Include insn module translation function */
1090 #include "insn_trans/trans_rvi.c.inc"
1091 #include "insn_trans/trans_rvm.c.inc"
1092 #include "insn_trans/trans_rva.c.inc"
1093 #include "insn_trans/trans_rvf.c.inc"
1094 #include "insn_trans/trans_rvd.c.inc"
1095 #include "insn_trans/trans_rvh.c.inc"
1096 #include "insn_trans/trans_rvv.c.inc"
1097 #include "insn_trans/trans_rvb.c.inc"
1098 #include "insn_trans/trans_rvzicond.c.inc"
1099 #include "insn_trans/trans_rvzacas.c.inc"
1100 #include "insn_trans/trans_rvzawrs.c.inc"
1101 #include "insn_trans/trans_rvzicbo.c.inc"
1102 #include "insn_trans/trans_rvzfa.c.inc"
1103 #include "insn_trans/trans_rvzfh.c.inc"
1104 #include "insn_trans/trans_rvk.c.inc"
1105 #include "insn_trans/trans_rvvk.c.inc"
1106 #include "insn_trans/trans_privileged.c.inc"
1107 #include "insn_trans/trans_svinval.c.inc"
1108 #include "insn_trans/trans_rvbf16.c.inc"
1109 #include "decode-xthead.c.inc"
1110 #include "insn_trans/trans_xthead.c.inc"
1111 #include "insn_trans/trans_xventanacondops.c.inc"
1113 /* Include the auto-generated decoder for 16 bit insn */
1114 #include "decode-insn16.c.inc"
1115 #include "insn_trans/trans_rvzce.c.inc"
1117 /* Include decoders for factored-out extensions */
1118 #include "decode-XVentanaCondOps.c.inc"
1120 /* The specification allows for longer insns, but not supported by qemu. */
1121 #define MAX_INSN_LEN 4
1123 static inline int insn_len(uint16_t first_word)
1125 return (first_word & 3) == 3 ? 4 : 2;
1128 const RISCVDecoder decoder_table[] = {
1129 { always_true_p, decode_insn32 },
1130 { has_xthead_p, decode_xthead},
1131 { has_XVentanaCondOps_p, decode_XVentanaCodeOps},
1134 const size_t decoder_table_size = ARRAY_SIZE(decoder_table);
1136 static void decode_opc(CPURISCVState *env, DisasContext *ctx, uint16_t opcode)
1138 ctx->virt_inst_excp = false;
1139 ctx->cur_insn_len = insn_len(opcode);
1140 /* Check for compressed insn */
1141 if (ctx->cur_insn_len == 2) {
1142 ctx->opcode = opcode;
1144 * The Zca extension is added as way to refer to instructions in the C
1145 * extension that do not include the floating-point loads and stores
1147 if ((has_ext(ctx, RVC) || ctx->cfg_ptr->ext_zca) &&
1148 decode_insn16(ctx, opcode)) {
1149 return;
1151 } else {
1152 uint32_t opcode32 = opcode;
1153 opcode32 = deposit32(opcode32, 16, 16,
1154 translator_lduw(env, &ctx->base,
1155 ctx->base.pc_next + 2));
1156 ctx->opcode = opcode32;
1158 for (guint i = 0; i < ctx->decoders->len; ++i) {
1159 riscv_cpu_decode_fn func = g_ptr_array_index(ctx->decoders, i);
1160 if (func(ctx, opcode32)) {
1161 return;
1166 gen_exception_illegal(ctx);
1169 static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
1171 DisasContext *ctx = container_of(dcbase, DisasContext, base);
1172 CPURISCVState *env = cpu_env(cs);
1173 RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cs);
1174 RISCVCPU *cpu = RISCV_CPU(cs);
1175 uint32_t tb_flags = ctx->base.tb->flags;
1177 ctx->pc_save = ctx->base.pc_first;
1178 ctx->priv = FIELD_EX32(tb_flags, TB_FLAGS, PRIV);
1179 ctx->mem_idx = FIELD_EX32(tb_flags, TB_FLAGS, MEM_IDX);
1180 ctx->mstatus_fs = FIELD_EX32(tb_flags, TB_FLAGS, FS);
1181 ctx->mstatus_vs = FIELD_EX32(tb_flags, TB_FLAGS, VS);
1182 ctx->priv_ver = env->priv_ver;
1183 ctx->virt_enabled = FIELD_EX32(tb_flags, TB_FLAGS, VIRT_ENABLED);
1184 ctx->misa_ext = env->misa_ext;
1185 ctx->frm = -1; /* unknown rounding mode */
1186 ctx->cfg_ptr = &(cpu->cfg);
1187 ctx->vill = FIELD_EX32(tb_flags, TB_FLAGS, VILL);
1188 ctx->sew = FIELD_EX32(tb_flags, TB_FLAGS, SEW);
1189 ctx->lmul = sextract32(FIELD_EX32(tb_flags, TB_FLAGS, LMUL), 0, 3);
1190 ctx->vta = FIELD_EX32(tb_flags, TB_FLAGS, VTA) && cpu->cfg.rvv_ta_all_1s;
1191 ctx->vma = FIELD_EX32(tb_flags, TB_FLAGS, VMA) && cpu->cfg.rvv_ma_all_1s;
1192 ctx->cfg_vta_all_1s = cpu->cfg.rvv_ta_all_1s;
1193 ctx->vstart_eq_zero = FIELD_EX32(tb_flags, TB_FLAGS, VSTART_EQ_ZERO);
1194 ctx->vl_eq_vlmax = FIELD_EX32(tb_flags, TB_FLAGS, VL_EQ_VLMAX);
1195 ctx->misa_mxl_max = mcc->misa_mxl_max;
1196 ctx->xl = FIELD_EX32(tb_flags, TB_FLAGS, XL);
1197 ctx->address_xl = FIELD_EX32(tb_flags, TB_FLAGS, AXL);
1198 ctx->cs = cs;
1199 ctx->pm_mask_enabled = FIELD_EX32(tb_flags, TB_FLAGS, PM_MASK_ENABLED);
1200 ctx->pm_base_enabled = FIELD_EX32(tb_flags, TB_FLAGS, PM_BASE_ENABLED);
1201 ctx->ztso = cpu->cfg.ext_ztso;
1202 ctx->itrigger = FIELD_EX32(tb_flags, TB_FLAGS, ITRIGGER);
1203 ctx->zero = tcg_constant_tl(0);
1204 ctx->virt_inst_excp = false;
1205 ctx->decoders = cpu->decoders;
1208 static void riscv_tr_tb_start(DisasContextBase *db, CPUState *cpu)
1212 static void riscv_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
1214 DisasContext *ctx = container_of(dcbase, DisasContext, base);
1215 target_ulong pc_next = ctx->base.pc_next;
1217 if (tb_cflags(dcbase->tb) & CF_PCREL) {
1218 pc_next &= ~TARGET_PAGE_MASK;
1221 tcg_gen_insn_start(pc_next, 0);
1222 ctx->insn_start_updated = false;
1225 static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
1227 DisasContext *ctx = container_of(dcbase, DisasContext, base);
1228 CPURISCVState *env = cpu_env(cpu);
1229 uint16_t opcode16 = translator_lduw(env, &ctx->base, ctx->base.pc_next);
1231 ctx->ol = ctx->xl;
1232 decode_opc(env, ctx, opcode16);
1233 ctx->base.pc_next += ctx->cur_insn_len;
1235 /* Only the first insn within a TB is allowed to cross a page boundary. */
1236 if (ctx->base.is_jmp == DISAS_NEXT) {
1237 if (ctx->itrigger || !is_same_page(&ctx->base, ctx->base.pc_next)) {
1238 ctx->base.is_jmp = DISAS_TOO_MANY;
1239 } else {
1240 unsigned page_ofs = ctx->base.pc_next & ~TARGET_PAGE_MASK;
1242 if (page_ofs > TARGET_PAGE_SIZE - MAX_INSN_LEN) {
1243 uint16_t next_insn =
1244 translator_lduw(env, &ctx->base, ctx->base.pc_next);
1245 int len = insn_len(next_insn);
1247 if (!is_same_page(&ctx->base, ctx->base.pc_next + len - 1)) {
1248 ctx->base.is_jmp = DISAS_TOO_MANY;
1255 static void riscv_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
1257 DisasContext *ctx = container_of(dcbase, DisasContext, base);
1259 switch (ctx->base.is_jmp) {
1260 case DISAS_TOO_MANY:
1261 gen_goto_tb(ctx, 0, 0);
1262 break;
1263 case DISAS_NORETURN:
1264 break;
1265 default:
1266 g_assert_not_reached();
1270 static const TranslatorOps riscv_tr_ops = {
1271 .init_disas_context = riscv_tr_init_disas_context,
1272 .tb_start = riscv_tr_tb_start,
1273 .insn_start = riscv_tr_insn_start,
1274 .translate_insn = riscv_tr_translate_insn,
1275 .tb_stop = riscv_tr_tb_stop,
1278 void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int *max_insns,
1279 vaddr pc, void *host_pc)
1281 DisasContext ctx;
1283 translator_loop(cs, tb, max_insns, pc, host_pc, &riscv_tr_ops, &ctx.base);
1286 void riscv_translate_init(void)
1288 int i;
1291 * cpu_gpr[0] is a placeholder for the zero register. Do not use it.
1292 * Use the gen_set_gpr and get_gpr helper functions when accessing regs,
1293 * unless you specifically block reads/writes to reg 0.
1295 cpu_gpr[0] = NULL;
1296 cpu_gprh[0] = NULL;
1298 for (i = 1; i < 32; i++) {
1299 cpu_gpr[i] = tcg_global_mem_new(tcg_env,
1300 offsetof(CPURISCVState, gpr[i]), riscv_int_regnames[i]);
1301 cpu_gprh[i] = tcg_global_mem_new(tcg_env,
1302 offsetof(CPURISCVState, gprh[i]), riscv_int_regnamesh[i]);
1305 for (i = 0; i < 32; i++) {
1306 cpu_fpr[i] = tcg_global_mem_new_i64(tcg_env,
1307 offsetof(CPURISCVState, fpr[i]), riscv_fpr_regnames[i]);
1310 cpu_pc = tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, pc), "pc");
1311 cpu_vl = tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, vl), "vl");
1312 cpu_vstart = tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, vstart),
1313 "vstart");
1314 load_res = tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, load_res),
1315 "load_res");
1316 load_val = tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, load_val),
1317 "load_val");
1318 /* Assign PM CSRs to tcg globals */
1319 pm_mask = tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, cur_pmmask),
1320 "pmmask");
1321 pm_base = tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, cur_pmbase),
1322 "pmbase");