xref: /openbmc/qemu/target/riscv/translate.c (revision 134c3ffa)
1 /*
2  * RISC-V emulation for qemu: main translation routines.
3  *
4  * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
5  *
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.
9  *
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.
14  *
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/>.
17  */
18 
19 #include "qemu/osdep.h"
20 #include "qemu/log.h"
21 #include "cpu.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"
28 
29 #include "exec/translator.h"
30 #include "exec/log.h"
31 #include "semihosting/semihost.h"
32 
33 #include "instmap.h"
34 #include "internals.h"
35 
36 /* global register indices */
37 static TCGv cpu_gpr[32], cpu_gprh[32], cpu_pc, cpu_vl, cpu_vstart;
38 static TCGv_i64 cpu_fpr[32]; /* assume F and D extensions */
39 static TCGv load_res;
40 static TCGv load_val;
41 /* globals for PM CSRs */
42 static TCGv pm_mask;
43 static TCGv pm_base;
44 
45 #include "exec/gen-icount.h"
46 
47 /*
48  * If an operation is being performed on less than TARGET_LONG_BITS,
49  * it may require the inputs to be sign- or zero-extended; which will
50  * depend on the exact operation being performed.
51  */
52 typedef enum {
53     EXT_NONE,
54     EXT_SIGN,
55     EXT_ZERO,
56 } DisasExtend;
57 
58 typedef struct DisasContext {
59     DisasContextBase base;
60     /* pc_succ_insn points to the instruction following base.pc_next */
61     target_ulong pc_succ_insn;
62     target_ulong priv_ver;
63     RISCVMXL misa_mxl_max;
64     RISCVMXL xl;
65     uint32_t misa_ext;
66     uint32_t opcode;
67     uint32_t mstatus_fs;
68     uint32_t mstatus_vs;
69     uint32_t mstatus_hs_fs;
70     uint32_t mstatus_hs_vs;
71     uint32_t mem_idx;
72     /* Remember the rounding mode encoded in the previous fp instruction,
73        which we have already installed into env->fp_status.  Or -1 for
74        no previous fp instruction.  Note that we exit the TB when writing
75        to any system register, which includes CSR_FRM, so we do not have
76        to reset this known value.  */
77     int frm;
78     RISCVMXL ol;
79     bool virt_inst_excp;
80     bool virt_enabled;
81     const RISCVCPUConfig *cfg_ptr;
82     bool hlsx;
83     /* vector extension */
84     bool vill;
85     /*
86      * Encode LMUL to lmul as follows:
87      *     LMUL    vlmul    lmul
88      *      1       000       0
89      *      2       001       1
90      *      4       010       2
91      *      8       011       3
92      *      -       100       -
93      *     1/8      101      -3
94      *     1/4      110      -2
95      *     1/2      111      -1
96      */
97     int8_t lmul;
98     uint8_t sew;
99     uint8_t vta;
100     uint8_t vma;
101     bool cfg_vta_all_1s;
102     target_ulong vstart;
103     bool vl_eq_vlmax;
104     uint8_t ntemp;
105     CPUState *cs;
106     TCGv zero;
107     /* Space for 3 operands plus 1 extra for address computation. */
108     TCGv temp[4];
109     /* Space for 4 operands(1 dest and <=3 src) for float point computation */
110     TCGv_i64 ftemp[4];
111     uint8_t nftemp;
112     /* PointerMasking extension */
113     bool pm_mask_enabled;
114     bool pm_base_enabled;
115     /* Use icount trigger for native debug */
116     bool itrigger;
117     /* FRM is known to contain a valid value. */
118     bool frm_valid;
119     /* TCG of the current insn_start */
120     TCGOp *insn_start;
121 } DisasContext;
122 
123 static inline bool has_ext(DisasContext *ctx, uint32_t ext)
124 {
125     return ctx->misa_ext & ext;
126 }
127 
128 static bool always_true_p(DisasContext *ctx  __attribute__((__unused__)))
129 {
130     return true;
131 }
132 
133 static bool has_xthead_p(DisasContext *ctx  __attribute__((__unused__)))
134 {
135     return ctx->cfg_ptr->ext_xtheadcmo || ctx->cfg_ptr->ext_xtheadsync;
136 }
137 
138 #define MATERIALISE_EXT_PREDICATE(ext)  \
139     static bool has_ ## ext ## _p(DisasContext *ctx)    \
140     { \
141         return ctx->cfg_ptr->ext_ ## ext ; \
142     }
143 
144 MATERIALISE_EXT_PREDICATE(XVentanaCondOps);
145 
146 #ifdef TARGET_RISCV32
147 #define get_xl(ctx)    MXL_RV32
148 #elif defined(CONFIG_USER_ONLY)
149 #define get_xl(ctx)    MXL_RV64
150 #else
151 #define get_xl(ctx)    ((ctx)->xl)
152 #endif
153 
154 /* The word size for this machine mode. */
155 static inline int __attribute__((unused)) get_xlen(DisasContext *ctx)
156 {
157     return 16 << get_xl(ctx);
158 }
159 
160 /* The operation length, as opposed to the xlen. */
161 #ifdef TARGET_RISCV32
162 #define get_ol(ctx)    MXL_RV32
163 #else
164 #define get_ol(ctx)    ((ctx)->ol)
165 #endif
166 
167 static inline int get_olen(DisasContext *ctx)
168 {
169     return 16 << get_ol(ctx);
170 }
171 
172 /* The maximum register length */
173 #ifdef TARGET_RISCV32
174 #define get_xl_max(ctx)    MXL_RV32
175 #else
176 #define get_xl_max(ctx)    ((ctx)->misa_mxl_max)
177 #endif
178 
179 /*
180  * RISC-V requires NaN-boxing of narrower width floating point values.
181  * This applies when a 32-bit value is assigned to a 64-bit FP register.
182  * For consistency and simplicity, we nanbox results even when the RVD
183  * extension is not present.
184  */
185 static void gen_nanbox_s(TCGv_i64 out, TCGv_i64 in)
186 {
187     tcg_gen_ori_i64(out, in, MAKE_64BIT_MASK(32, 32));
188 }
189 
190 static void gen_nanbox_h(TCGv_i64 out, TCGv_i64 in)
191 {
192     tcg_gen_ori_i64(out, in, MAKE_64BIT_MASK(16, 48));
193 }
194 
195 /*
196  * A narrow n-bit operation, where n < FLEN, checks that input operands
197  * are correctly Nan-boxed, i.e., all upper FLEN - n bits are 1.
198  * If so, the least-significant bits of the input are used, otherwise the
199  * input value is treated as an n-bit canonical NaN (v2.2 section 9.2).
200  *
201  * Here, the result is always nan-boxed, even the canonical nan.
202  */
203 static void gen_check_nanbox_h(TCGv_i64 out, TCGv_i64 in)
204 {
205     TCGv_i64 t_max = tcg_const_i64(0xffffffffffff0000ull);
206     TCGv_i64 t_nan = tcg_const_i64(0xffffffffffff7e00ull);
207 
208     tcg_gen_movcond_i64(TCG_COND_GEU, out, in, t_max, in, t_nan);
209     tcg_temp_free_i64(t_max);
210     tcg_temp_free_i64(t_nan);
211 }
212 
213 static void gen_check_nanbox_s(TCGv_i64 out, TCGv_i64 in)
214 {
215     TCGv_i64 t_max = tcg_constant_i64(0xffffffff00000000ull);
216     TCGv_i64 t_nan = tcg_constant_i64(0xffffffff7fc00000ull);
217 
218     tcg_gen_movcond_i64(TCG_COND_GEU, out, in, t_max, in, t_nan);
219 }
220 
221 static void decode_save_opc(DisasContext *ctx)
222 {
223     assert(ctx->insn_start != NULL);
224     tcg_set_insn_start_param(ctx->insn_start, 1, ctx->opcode);
225     ctx->insn_start = NULL;
226 }
227 
228 static void gen_set_pc_imm(DisasContext *ctx, target_ulong dest)
229 {
230     if (get_xl(ctx) == MXL_RV32) {
231         dest = (int32_t)dest;
232     }
233     tcg_gen_movi_tl(cpu_pc, dest);
234 }
235 
236 static void gen_set_pc(DisasContext *ctx, TCGv dest)
237 {
238     if (get_xl(ctx) == MXL_RV32) {
239         tcg_gen_ext32s_tl(cpu_pc, dest);
240     } else {
241         tcg_gen_mov_tl(cpu_pc, dest);
242     }
243 }
244 
245 static void generate_exception(DisasContext *ctx, int excp)
246 {
247     gen_set_pc_imm(ctx, ctx->base.pc_next);
248     gen_helper_raise_exception(cpu_env, tcg_constant_i32(excp));
249     ctx->base.is_jmp = DISAS_NORETURN;
250 }
251 
252 static void gen_exception_illegal(DisasContext *ctx)
253 {
254     tcg_gen_st_i32(tcg_constant_i32(ctx->opcode), cpu_env,
255                    offsetof(CPURISCVState, bins));
256     if (ctx->virt_inst_excp) {
257         generate_exception(ctx, RISCV_EXCP_VIRT_INSTRUCTION_FAULT);
258     } else {
259         generate_exception(ctx, RISCV_EXCP_ILLEGAL_INST);
260     }
261 }
262 
263 static void gen_exception_inst_addr_mis(DisasContext *ctx)
264 {
265     tcg_gen_st_tl(cpu_pc, cpu_env, offsetof(CPURISCVState, badaddr));
266     generate_exception(ctx, RISCV_EXCP_INST_ADDR_MIS);
267 }
268 
269 static void lookup_and_goto_ptr(DisasContext *ctx)
270 {
271 #ifndef CONFIG_USER_ONLY
272     if (ctx->itrigger) {
273         gen_helper_itrigger_match(cpu_env);
274     }
275 #endif
276     tcg_gen_lookup_and_goto_ptr();
277 }
278 
279 static void exit_tb(DisasContext *ctx)
280 {
281 #ifndef CONFIG_USER_ONLY
282     if (ctx->itrigger) {
283         gen_helper_itrigger_match(cpu_env);
284     }
285 #endif
286     tcg_gen_exit_tb(NULL, 0);
287 }
288 
289 static void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
290 {
291      /*
292       * Under itrigger, instruction executes one by one like singlestep,
293       * direct block chain benefits will be small.
294       */
295     if (translator_use_goto_tb(&ctx->base, dest) && !ctx->itrigger) {
296         tcg_gen_goto_tb(n);
297         gen_set_pc_imm(ctx, dest);
298         tcg_gen_exit_tb(ctx->base.tb, n);
299     } else {
300         gen_set_pc_imm(ctx, dest);
301         lookup_and_goto_ptr(ctx);
302     }
303 }
304 
305 /*
306  * Wrappers for getting reg values.
307  *
308  * The $zero register does not have cpu_gpr[0] allocated -- we supply the
309  * constant zero as a source, and an uninitialized sink as destination.
310  *
311  * Further, we may provide an extension for word operations.
312  */
313 static TCGv temp_new(DisasContext *ctx)
314 {
315     assert(ctx->ntemp < ARRAY_SIZE(ctx->temp));
316     return ctx->temp[ctx->ntemp++] = tcg_temp_new();
317 }
318 
319 static TCGv get_gpr(DisasContext *ctx, int reg_num, DisasExtend ext)
320 {
321     TCGv t;
322 
323     if (reg_num == 0) {
324         return ctx->zero;
325     }
326 
327     switch (get_ol(ctx)) {
328     case MXL_RV32:
329         switch (ext) {
330         case EXT_NONE:
331             break;
332         case EXT_SIGN:
333             t = temp_new(ctx);
334             tcg_gen_ext32s_tl(t, cpu_gpr[reg_num]);
335             return t;
336         case EXT_ZERO:
337             t = temp_new(ctx);
338             tcg_gen_ext32u_tl(t, cpu_gpr[reg_num]);
339             return t;
340         default:
341             g_assert_not_reached();
342         }
343         break;
344     case MXL_RV64:
345     case MXL_RV128:
346         break;
347     default:
348         g_assert_not_reached();
349     }
350     return cpu_gpr[reg_num];
351 }
352 
353 static TCGv get_gprh(DisasContext *ctx, int reg_num)
354 {
355     assert(get_xl(ctx) == MXL_RV128);
356     if (reg_num == 0) {
357         return ctx->zero;
358     }
359     return cpu_gprh[reg_num];
360 }
361 
362 static TCGv dest_gpr(DisasContext *ctx, int reg_num)
363 {
364     if (reg_num == 0 || get_olen(ctx) < TARGET_LONG_BITS) {
365         return temp_new(ctx);
366     }
367     return cpu_gpr[reg_num];
368 }
369 
370 static TCGv dest_gprh(DisasContext *ctx, int reg_num)
371 {
372     if (reg_num == 0) {
373         return temp_new(ctx);
374     }
375     return cpu_gprh[reg_num];
376 }
377 
378 static void gen_set_gpr(DisasContext *ctx, int reg_num, TCGv t)
379 {
380     if (reg_num != 0) {
381         switch (get_ol(ctx)) {
382         case MXL_RV32:
383             tcg_gen_ext32s_tl(cpu_gpr[reg_num], t);
384             break;
385         case MXL_RV64:
386         case MXL_RV128:
387             tcg_gen_mov_tl(cpu_gpr[reg_num], t);
388             break;
389         default:
390             g_assert_not_reached();
391         }
392 
393         if (get_xl_max(ctx) == MXL_RV128) {
394             tcg_gen_sari_tl(cpu_gprh[reg_num], cpu_gpr[reg_num], 63);
395         }
396     }
397 }
398 
399 static void gen_set_gpri(DisasContext *ctx, int reg_num, target_long imm)
400 {
401     if (reg_num != 0) {
402         switch (get_ol(ctx)) {
403         case MXL_RV32:
404             tcg_gen_movi_tl(cpu_gpr[reg_num], (int32_t)imm);
405             break;
406         case MXL_RV64:
407         case MXL_RV128:
408             tcg_gen_movi_tl(cpu_gpr[reg_num], imm);
409             break;
410         default:
411             g_assert_not_reached();
412         }
413 
414         if (get_xl_max(ctx) == MXL_RV128) {
415             tcg_gen_movi_tl(cpu_gprh[reg_num], -(imm < 0));
416         }
417     }
418 }
419 
420 static void gen_set_gpr128(DisasContext *ctx, int reg_num, TCGv rl, TCGv rh)
421 {
422     assert(get_ol(ctx) == MXL_RV128);
423     if (reg_num != 0) {
424         tcg_gen_mov_tl(cpu_gpr[reg_num], rl);
425         tcg_gen_mov_tl(cpu_gprh[reg_num], rh);
426     }
427 }
428 
429 static TCGv_i64 ftemp_new(DisasContext *ctx)
430 {
431     assert(ctx->nftemp < ARRAY_SIZE(ctx->ftemp));
432     return ctx->ftemp[ctx->nftemp++] = tcg_temp_new_i64();
433 }
434 
435 static TCGv_i64 get_fpr_hs(DisasContext *ctx, int reg_num)
436 {
437     if (!ctx->cfg_ptr->ext_zfinx) {
438         return cpu_fpr[reg_num];
439     }
440 
441     if (reg_num == 0) {
442         return tcg_constant_i64(0);
443     }
444     switch (get_xl(ctx)) {
445     case MXL_RV32:
446 #ifdef TARGET_RISCV32
447     {
448         TCGv_i64 t = ftemp_new(ctx);
449         tcg_gen_ext_i32_i64(t, cpu_gpr[reg_num]);
450         return t;
451     }
452 #else
453     /* fall through */
454     case MXL_RV64:
455         return cpu_gpr[reg_num];
456 #endif
457     default:
458         g_assert_not_reached();
459     }
460 }
461 
462 static TCGv_i64 get_fpr_d(DisasContext *ctx, int reg_num)
463 {
464     if (!ctx->cfg_ptr->ext_zfinx) {
465         return cpu_fpr[reg_num];
466     }
467 
468     if (reg_num == 0) {
469         return tcg_constant_i64(0);
470     }
471     switch (get_xl(ctx)) {
472     case MXL_RV32:
473     {
474         TCGv_i64 t = ftemp_new(ctx);
475         tcg_gen_concat_tl_i64(t, cpu_gpr[reg_num], cpu_gpr[reg_num + 1]);
476         return t;
477     }
478 #ifdef TARGET_RISCV64
479     case MXL_RV64:
480         return cpu_gpr[reg_num];
481 #endif
482     default:
483         g_assert_not_reached();
484     }
485 }
486 
487 static TCGv_i64 dest_fpr(DisasContext *ctx, int reg_num)
488 {
489     if (!ctx->cfg_ptr->ext_zfinx) {
490         return cpu_fpr[reg_num];
491     }
492 
493     if (reg_num == 0) {
494         return ftemp_new(ctx);
495     }
496 
497     switch (get_xl(ctx)) {
498     case MXL_RV32:
499         return ftemp_new(ctx);
500 #ifdef TARGET_RISCV64
501     case MXL_RV64:
502         return cpu_gpr[reg_num];
503 #endif
504     default:
505         g_assert_not_reached();
506     }
507 }
508 
509 /* assume t is nanboxing (for normal) or sign-extended (for zfinx) */
510 static void gen_set_fpr_hs(DisasContext *ctx, int reg_num, TCGv_i64 t)
511 {
512     if (!ctx->cfg_ptr->ext_zfinx) {
513         tcg_gen_mov_i64(cpu_fpr[reg_num], t);
514         return;
515     }
516     if (reg_num != 0) {
517         switch (get_xl(ctx)) {
518         case MXL_RV32:
519 #ifdef TARGET_RISCV32
520             tcg_gen_extrl_i64_i32(cpu_gpr[reg_num], t);
521             break;
522 #else
523         /* fall through */
524         case MXL_RV64:
525             tcg_gen_mov_i64(cpu_gpr[reg_num], t);
526             break;
527 #endif
528         default:
529             g_assert_not_reached();
530         }
531     }
532 }
533 
534 static void gen_set_fpr_d(DisasContext *ctx, int reg_num, TCGv_i64 t)
535 {
536     if (!ctx->cfg_ptr->ext_zfinx) {
537         tcg_gen_mov_i64(cpu_fpr[reg_num], t);
538         return;
539     }
540 
541     if (reg_num != 0) {
542         switch (get_xl(ctx)) {
543         case MXL_RV32:
544 #ifdef TARGET_RISCV32
545             tcg_gen_extr_i64_i32(cpu_gpr[reg_num], cpu_gpr[reg_num + 1], t);
546             break;
547 #else
548             tcg_gen_ext32s_i64(cpu_gpr[reg_num], t);
549             tcg_gen_sari_i64(cpu_gpr[reg_num + 1], t, 32);
550             break;
551         case MXL_RV64:
552             tcg_gen_mov_i64(cpu_gpr[reg_num], t);
553             break;
554 #endif
555         default:
556             g_assert_not_reached();
557         }
558     }
559 }
560 
561 static void gen_jal(DisasContext *ctx, int rd, target_ulong imm)
562 {
563     target_ulong next_pc;
564 
565     /* check misaligned: */
566     next_pc = ctx->base.pc_next + imm;
567     if (!has_ext(ctx, RVC)) {
568         if ((next_pc & 0x3) != 0) {
569             gen_exception_inst_addr_mis(ctx);
570             return;
571         }
572     }
573 
574     gen_set_gpri(ctx, rd, ctx->pc_succ_insn);
575     gen_goto_tb(ctx, 0, ctx->base.pc_next + imm); /* must use this for safety */
576     ctx->base.is_jmp = DISAS_NORETURN;
577 }
578 
579 /* Compute a canonical address from a register plus offset. */
580 static TCGv get_address(DisasContext *ctx, int rs1, int imm)
581 {
582     TCGv addr = temp_new(ctx);
583     TCGv src1 = get_gpr(ctx, rs1, EXT_NONE);
584 
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_xl(ctx) == MXL_RV32) {
589         tcg_gen_ext32u_tl(addr, addr);
590     }
591     if (ctx->pm_base_enabled) {
592         tcg_gen_or_tl(addr, addr, pm_base);
593     }
594     return addr;
595 }
596 
597 #ifndef CONFIG_USER_ONLY
598 /* The states of mstatus_fs are:
599  * 0 = disabled, 1 = initial, 2 = clean, 3 = dirty
600  * We will have already diagnosed disabled state,
601  * and need to turn initial/clean into dirty.
602  */
603 static void mark_fs_dirty(DisasContext *ctx)
604 {
605     TCGv tmp;
606 
607     if (!has_ext(ctx, RVF)) {
608         return;
609     }
610 
611     if (ctx->mstatus_fs != MSTATUS_FS) {
612         /* Remember the state change for the rest of the TB. */
613         ctx->mstatus_fs = MSTATUS_FS;
614 
615         tmp = tcg_temp_new();
616         tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
617         tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS);
618         tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
619         tcg_temp_free(tmp);
620     }
621 
622     if (ctx->virt_enabled && ctx->mstatus_hs_fs != MSTATUS_FS) {
623         /* Remember the stage change for the rest of the TB. */
624         ctx->mstatus_hs_fs = MSTATUS_FS;
625 
626         tmp = tcg_temp_new();
627         tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
628         tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS);
629         tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
630         tcg_temp_free(tmp);
631     }
632 }
633 #else
634 static inline void mark_fs_dirty(DisasContext *ctx) { }
635 #endif
636 
637 #ifndef CONFIG_USER_ONLY
638 /* The states of mstatus_vs are:
639  * 0 = disabled, 1 = initial, 2 = clean, 3 = dirty
640  * We will have already diagnosed disabled state,
641  * and need to turn initial/clean into dirty.
642  */
643 static void mark_vs_dirty(DisasContext *ctx)
644 {
645     TCGv tmp;
646 
647     if (ctx->mstatus_vs != MSTATUS_VS) {
648         /* Remember the state change for the rest of the TB.  */
649         ctx->mstatus_vs = MSTATUS_VS;
650 
651         tmp = tcg_temp_new();
652         tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
653         tcg_gen_ori_tl(tmp, tmp, MSTATUS_VS);
654         tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
655         tcg_temp_free(tmp);
656     }
657 
658     if (ctx->virt_enabled && ctx->mstatus_hs_vs != MSTATUS_VS) {
659         /* Remember the stage change for the rest of the TB. */
660         ctx->mstatus_hs_vs = MSTATUS_VS;
661 
662         tmp = tcg_temp_new();
663         tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
664         tcg_gen_ori_tl(tmp, tmp, MSTATUS_VS);
665         tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
666         tcg_temp_free(tmp);
667     }
668 }
669 #else
670 static inline void mark_vs_dirty(DisasContext *ctx) { }
671 #endif
672 
673 static void gen_set_rm(DisasContext *ctx, int rm)
674 {
675     if (ctx->frm == rm) {
676         return;
677     }
678     ctx->frm = rm;
679 
680     if (rm == RISCV_FRM_DYN) {
681         /* The helper will return only if frm valid. */
682         ctx->frm_valid = true;
683     }
684 
685     /* The helper may raise ILLEGAL_INSN -- record binv for unwind. */
686     decode_save_opc(ctx);
687     gen_helper_set_rounding_mode(cpu_env, tcg_constant_i32(rm));
688 }
689 
690 static void gen_set_rm_chkfrm(DisasContext *ctx, int rm)
691 {
692     if (ctx->frm == rm && ctx->frm_valid) {
693         return;
694     }
695     ctx->frm = rm;
696     ctx->frm_valid = true;
697 
698     /* The helper may raise ILLEGAL_INSN -- record binv for unwind. */
699     decode_save_opc(ctx);
700     gen_helper_set_rounding_mode_chkfrm(cpu_env, tcg_constant_i32(rm));
701 }
702 
703 static int ex_plus_1(DisasContext *ctx, int nf)
704 {
705     return nf + 1;
706 }
707 
708 #define EX_SH(amount) \
709     static int ex_shift_##amount(DisasContext *ctx, int imm) \
710     {                                         \
711         return imm << amount;                 \
712     }
713 EX_SH(1)
714 EX_SH(2)
715 EX_SH(3)
716 EX_SH(4)
717 EX_SH(12)
718 
719 #define REQUIRE_EXT(ctx, ext) do { \
720     if (!has_ext(ctx, ext)) {      \
721         return false;              \
722     }                              \
723 } while (0)
724 
725 #define REQUIRE_32BIT(ctx) do {    \
726     if (get_xl(ctx) != MXL_RV32) { \
727         return false;              \
728     }                              \
729 } while (0)
730 
731 #define REQUIRE_64BIT(ctx) do {     \
732     if (get_xl(ctx) != MXL_RV64) {  \
733         return false;               \
734     }                               \
735 } while (0)
736 
737 #define REQUIRE_128BIT(ctx) do {    \
738     if (get_xl(ctx) != MXL_RV128) { \
739         return false;               \
740     }                               \
741 } while (0)
742 
743 #define REQUIRE_64_OR_128BIT(ctx) do { \
744     if (get_xl(ctx) == MXL_RV32) {     \
745         return false;                  \
746     }                                  \
747 } while (0)
748 
749 #define REQUIRE_EITHER_EXT(ctx, A, B) do {       \
750     if (!ctx->cfg_ptr->ext_##A &&      \
751         !ctx->cfg_ptr->ext_##B) {      \
752         return false;                            \
753     }                                            \
754 } while (0)
755 
756 static int ex_rvc_register(DisasContext *ctx, int reg)
757 {
758     return 8 + reg;
759 }
760 
761 static int ex_rvc_shiftli(DisasContext *ctx, int imm)
762 {
763     /* For RV128 a shamt of 0 means a shift by 64. */
764     if (get_ol(ctx) == MXL_RV128) {
765         imm = imm ? imm : 64;
766     }
767     return imm;
768 }
769 
770 static int ex_rvc_shiftri(DisasContext *ctx, int imm)
771 {
772     /*
773      * For RV128 a shamt of 0 means a shift by 64, furthermore, for right
774      * shifts, the shamt is sign-extended.
775      */
776     if (get_ol(ctx) == MXL_RV128) {
777         imm = imm | (imm & 32) << 1;
778         imm = imm ? imm : 64;
779     }
780     return imm;
781 }
782 
783 /* Include the auto-generated decoder for 32 bit insn */
784 #include "decode-insn32.c.inc"
785 
786 static bool gen_logic_imm_fn(DisasContext *ctx, arg_i *a,
787                              void (*func)(TCGv, TCGv, target_long))
788 {
789     TCGv dest = dest_gpr(ctx, a->rd);
790     TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE);
791 
792     func(dest, src1, a->imm);
793 
794     if (get_xl(ctx) == MXL_RV128) {
795         TCGv src1h = get_gprh(ctx, a->rs1);
796         TCGv desth = dest_gprh(ctx, a->rd);
797 
798         func(desth, src1h, -(a->imm < 0));
799         gen_set_gpr128(ctx, a->rd, dest, desth);
800     } else {
801         gen_set_gpr(ctx, a->rd, dest);
802     }
803 
804     return true;
805 }
806 
807 static bool gen_logic(DisasContext *ctx, arg_r *a,
808                       void (*func)(TCGv, TCGv, TCGv))
809 {
810     TCGv dest = dest_gpr(ctx, a->rd);
811     TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE);
812     TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE);
813 
814     func(dest, src1, src2);
815 
816     if (get_xl(ctx) == MXL_RV128) {
817         TCGv src1h = get_gprh(ctx, a->rs1);
818         TCGv src2h = get_gprh(ctx, a->rs2);
819         TCGv desth = dest_gprh(ctx, a->rd);
820 
821         func(desth, src1h, src2h);
822         gen_set_gpr128(ctx, a->rd, dest, desth);
823     } else {
824         gen_set_gpr(ctx, a->rd, dest);
825     }
826 
827     return true;
828 }
829 
830 static bool gen_arith_imm_fn(DisasContext *ctx, arg_i *a, DisasExtend ext,
831                              void (*func)(TCGv, TCGv, target_long),
832                              void (*f128)(TCGv, TCGv, TCGv, TCGv, target_long))
833 {
834     TCGv dest = dest_gpr(ctx, a->rd);
835     TCGv src1 = get_gpr(ctx, a->rs1, ext);
836 
837     if (get_ol(ctx) < MXL_RV128) {
838         func(dest, src1, a->imm);
839         gen_set_gpr(ctx, a->rd, dest);
840     } else {
841         if (f128 == NULL) {
842             return false;
843         }
844 
845         TCGv src1h = get_gprh(ctx, a->rs1);
846         TCGv desth = dest_gprh(ctx, a->rd);
847 
848         f128(dest, desth, src1, src1h, a->imm);
849         gen_set_gpr128(ctx, a->rd, dest, desth);
850     }
851     return true;
852 }
853 
854 static bool gen_arith_imm_tl(DisasContext *ctx, arg_i *a, DisasExtend ext,
855                              void (*func)(TCGv, TCGv, TCGv),
856                              void (*f128)(TCGv, TCGv, TCGv, TCGv, TCGv, TCGv))
857 {
858     TCGv dest = dest_gpr(ctx, a->rd);
859     TCGv src1 = get_gpr(ctx, a->rs1, ext);
860     TCGv src2 = tcg_constant_tl(a->imm);
861 
862     if (get_ol(ctx) < MXL_RV128) {
863         func(dest, src1, src2);
864         gen_set_gpr(ctx, a->rd, dest);
865     } else {
866         if (f128 == NULL) {
867             return false;
868         }
869 
870         TCGv src1h = get_gprh(ctx, a->rs1);
871         TCGv src2h = tcg_constant_tl(-(a->imm < 0));
872         TCGv desth = dest_gprh(ctx, a->rd);
873 
874         f128(dest, desth, src1, src1h, src2, src2h);
875         gen_set_gpr128(ctx, a->rd, dest, desth);
876     }
877     return true;
878 }
879 
880 static bool gen_arith(DisasContext *ctx, arg_r *a, DisasExtend ext,
881                       void (*func)(TCGv, TCGv, TCGv),
882                       void (*f128)(TCGv, TCGv, TCGv, TCGv, TCGv, TCGv))
883 {
884     TCGv dest = dest_gpr(ctx, a->rd);
885     TCGv src1 = get_gpr(ctx, a->rs1, ext);
886     TCGv src2 = get_gpr(ctx, a->rs2, ext);
887 
888     if (get_ol(ctx) < MXL_RV128) {
889         func(dest, src1, src2);
890         gen_set_gpr(ctx, a->rd, dest);
891     } else {
892         if (f128 == NULL) {
893             return false;
894         }
895 
896         TCGv src1h = get_gprh(ctx, a->rs1);
897         TCGv src2h = get_gprh(ctx, a->rs2);
898         TCGv desth = dest_gprh(ctx, a->rd);
899 
900         f128(dest, desth, src1, src1h, src2, src2h);
901         gen_set_gpr128(ctx, a->rd, dest, desth);
902     }
903     return true;
904 }
905 
906 static bool gen_arith_per_ol(DisasContext *ctx, arg_r *a, DisasExtend ext,
907                              void (*f_tl)(TCGv, TCGv, TCGv),
908                              void (*f_32)(TCGv, TCGv, TCGv),
909                              void (*f_128)(TCGv, TCGv, TCGv, TCGv, TCGv, TCGv))
910 {
911     int olen = get_olen(ctx);
912 
913     if (olen != TARGET_LONG_BITS) {
914         if (olen == 32) {
915             f_tl = f_32;
916         } else if (olen != 128) {
917             g_assert_not_reached();
918         }
919     }
920     return gen_arith(ctx, a, ext, f_tl, f_128);
921 }
922 
923 static bool gen_shift_imm_fn(DisasContext *ctx, arg_shift *a, DisasExtend ext,
924                              void (*func)(TCGv, TCGv, target_long),
925                              void (*f128)(TCGv, TCGv, TCGv, TCGv, target_long))
926 {
927     TCGv dest, src1;
928     int max_len = get_olen(ctx);
929 
930     if (a->shamt >= max_len) {
931         return false;
932     }
933 
934     dest = dest_gpr(ctx, a->rd);
935     src1 = get_gpr(ctx, a->rs1, ext);
936 
937     if (max_len < 128) {
938         func(dest, src1, a->shamt);
939         gen_set_gpr(ctx, a->rd, dest);
940     } else {
941         TCGv src1h = get_gprh(ctx, a->rs1);
942         TCGv desth = dest_gprh(ctx, a->rd);
943 
944         if (f128 == NULL) {
945             return false;
946         }
947         f128(dest, desth, src1, src1h, a->shamt);
948         gen_set_gpr128(ctx, a->rd, dest, desth);
949     }
950     return true;
951 }
952 
953 static bool gen_shift_imm_fn_per_ol(DisasContext *ctx, arg_shift *a,
954                                     DisasExtend ext,
955                                     void (*f_tl)(TCGv, TCGv, target_long),
956                                     void (*f_32)(TCGv, TCGv, target_long),
957                                     void (*f_128)(TCGv, TCGv, TCGv, TCGv,
958                                                   target_long))
959 {
960     int olen = get_olen(ctx);
961     if (olen != TARGET_LONG_BITS) {
962         if (olen == 32) {
963             f_tl = f_32;
964         } else if (olen != 128) {
965             g_assert_not_reached();
966         }
967     }
968     return gen_shift_imm_fn(ctx, a, ext, f_tl, f_128);
969 }
970 
971 static bool gen_shift_imm_tl(DisasContext *ctx, arg_shift *a, DisasExtend ext,
972                              void (*func)(TCGv, TCGv, TCGv))
973 {
974     TCGv dest, src1, src2;
975     int max_len = get_olen(ctx);
976 
977     if (a->shamt >= max_len) {
978         return false;
979     }
980 
981     dest = dest_gpr(ctx, a->rd);
982     src1 = get_gpr(ctx, a->rs1, ext);
983     src2 = tcg_constant_tl(a->shamt);
984 
985     func(dest, src1, src2);
986 
987     gen_set_gpr(ctx, a->rd, dest);
988     return true;
989 }
990 
991 static bool gen_shift(DisasContext *ctx, arg_r *a, DisasExtend ext,
992                       void (*func)(TCGv, TCGv, TCGv),
993                       void (*f128)(TCGv, TCGv, TCGv, TCGv, TCGv))
994 {
995     TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE);
996     TCGv ext2 = tcg_temp_new();
997     int max_len = get_olen(ctx);
998 
999     tcg_gen_andi_tl(ext2, src2, max_len - 1);
1000 
1001     TCGv dest = dest_gpr(ctx, a->rd);
1002     TCGv src1 = get_gpr(ctx, a->rs1, ext);
1003 
1004     if (max_len < 128) {
1005         func(dest, src1, ext2);
1006         gen_set_gpr(ctx, a->rd, dest);
1007     } else {
1008         TCGv src1h = get_gprh(ctx, a->rs1);
1009         TCGv desth = dest_gprh(ctx, a->rd);
1010 
1011         if (f128 == NULL) {
1012             return false;
1013         }
1014         f128(dest, desth, src1, src1h, ext2);
1015         gen_set_gpr128(ctx, a->rd, dest, desth);
1016     }
1017     tcg_temp_free(ext2);
1018     return true;
1019 }
1020 
1021 static bool gen_shift_per_ol(DisasContext *ctx, arg_r *a, DisasExtend ext,
1022                              void (*f_tl)(TCGv, TCGv, TCGv),
1023                              void (*f_32)(TCGv, TCGv, TCGv),
1024                              void (*f_128)(TCGv, TCGv, TCGv, TCGv, TCGv))
1025 {
1026     int olen = get_olen(ctx);
1027     if (olen != TARGET_LONG_BITS) {
1028         if (olen == 32) {
1029             f_tl = f_32;
1030         } else if (olen != 128) {
1031             g_assert_not_reached();
1032         }
1033     }
1034     return gen_shift(ctx, a, ext, f_tl, f_128);
1035 }
1036 
1037 static bool gen_unary(DisasContext *ctx, arg_r2 *a, DisasExtend ext,
1038                       void (*func)(TCGv, TCGv))
1039 {
1040     TCGv dest = dest_gpr(ctx, a->rd);
1041     TCGv src1 = get_gpr(ctx, a->rs1, ext);
1042 
1043     func(dest, src1);
1044 
1045     gen_set_gpr(ctx, a->rd, dest);
1046     return true;
1047 }
1048 
1049 static bool gen_unary_per_ol(DisasContext *ctx, arg_r2 *a, DisasExtend ext,
1050                              void (*f_tl)(TCGv, TCGv),
1051                              void (*f_32)(TCGv, TCGv))
1052 {
1053     int olen = get_olen(ctx);
1054 
1055     if (olen != TARGET_LONG_BITS) {
1056         if (olen == 32) {
1057             f_tl = f_32;
1058         } else {
1059             g_assert_not_reached();
1060         }
1061     }
1062     return gen_unary(ctx, a, ext, f_tl);
1063 }
1064 
1065 static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)
1066 {
1067     DisasContext *ctx = container_of(dcbase, DisasContext, base);
1068     CPUState *cpu = ctx->cs;
1069     CPURISCVState *env = cpu->env_ptr;
1070 
1071     return cpu_ldl_code(env, pc);
1072 }
1073 
1074 /* Include insn module translation function */
1075 #include "insn_trans/trans_rvi.c.inc"
1076 #include "insn_trans/trans_rvm.c.inc"
1077 #include "insn_trans/trans_rva.c.inc"
1078 #include "insn_trans/trans_rvf.c.inc"
1079 #include "insn_trans/trans_rvd.c.inc"
1080 #include "insn_trans/trans_rvh.c.inc"
1081 #include "insn_trans/trans_rvv.c.inc"
1082 #include "insn_trans/trans_rvb.c.inc"
1083 #include "insn_trans/trans_rvzawrs.c.inc"
1084 #include "insn_trans/trans_rvzfh.c.inc"
1085 #include "insn_trans/trans_rvk.c.inc"
1086 #include "insn_trans/trans_privileged.c.inc"
1087 #include "insn_trans/trans_svinval.c.inc"
1088 #include "decode-xthead.c.inc"
1089 #include "insn_trans/trans_xthead.c.inc"
1090 #include "insn_trans/trans_xventanacondops.c.inc"
1091 
1092 /* Include the auto-generated decoder for 16 bit insn */
1093 #include "decode-insn16.c.inc"
1094 /* Include decoders for factored-out extensions */
1095 #include "decode-XVentanaCondOps.c.inc"
1096 
1097 /* The specification allows for longer insns, but not supported by qemu. */
1098 #define MAX_INSN_LEN  4
1099 
1100 static inline int insn_len(uint16_t first_word)
1101 {
1102     return (first_word & 3) == 3 ? 4 : 2;
1103 }
1104 
1105 static void decode_opc(CPURISCVState *env, DisasContext *ctx, uint16_t opcode)
1106 {
1107     /*
1108      * A table with predicate (i.e., guard) functions and decoder functions
1109      * that are tested in-order until a decoder matches onto the opcode.
1110      */
1111     static const struct {
1112         bool (*guard_func)(DisasContext *);
1113         bool (*decode_func)(DisasContext *, uint32_t);
1114     } decoders[] = {
1115         { always_true_p,  decode_insn32 },
1116         { has_xthead_p, decode_xthead },
1117         { has_XVentanaCondOps_p,  decode_XVentanaCodeOps },
1118     };
1119 
1120     ctx->virt_inst_excp = false;
1121     /* Check for compressed insn */
1122     if (insn_len(opcode) == 2) {
1123         ctx->opcode = opcode;
1124         ctx->pc_succ_insn = ctx->base.pc_next + 2;
1125         if (has_ext(ctx, RVC) && decode_insn16(ctx, opcode)) {
1126             return;
1127         }
1128     } else {
1129         uint32_t opcode32 = opcode;
1130         opcode32 = deposit32(opcode32, 16, 16,
1131                              translator_lduw(env, &ctx->base,
1132                                              ctx->base.pc_next + 2));
1133         ctx->opcode = opcode32;
1134         ctx->pc_succ_insn = ctx->base.pc_next + 4;
1135 
1136         for (size_t i = 0; i < ARRAY_SIZE(decoders); ++i) {
1137             if (decoders[i].guard_func(ctx) &&
1138                 decoders[i].decode_func(ctx, opcode32)) {
1139                 return;
1140             }
1141         }
1142     }
1143 
1144     gen_exception_illegal(ctx);
1145 }
1146 
1147 static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
1148 {
1149     DisasContext *ctx = container_of(dcbase, DisasContext, base);
1150     CPURISCVState *env = cs->env_ptr;
1151     RISCVCPU *cpu = RISCV_CPU(cs);
1152     uint32_t tb_flags = ctx->base.tb->flags;
1153 
1154     ctx->pc_succ_insn = ctx->base.pc_first;
1155     ctx->mem_idx = FIELD_EX32(tb_flags, TB_FLAGS, MEM_IDX);
1156     ctx->mstatus_fs = tb_flags & TB_FLAGS_MSTATUS_FS;
1157     ctx->mstatus_vs = tb_flags & TB_FLAGS_MSTATUS_VS;
1158     ctx->priv_ver = env->priv_ver;
1159 #if !defined(CONFIG_USER_ONLY)
1160     if (riscv_has_ext(env, RVH)) {
1161         ctx->virt_enabled = riscv_cpu_virt_enabled(env);
1162     } else {
1163         ctx->virt_enabled = false;
1164     }
1165 #else
1166     ctx->virt_enabled = false;
1167 #endif
1168     ctx->misa_ext = env->misa_ext;
1169     ctx->frm = -1;  /* unknown rounding mode */
1170     ctx->cfg_ptr = &(cpu->cfg);
1171     ctx->mstatus_hs_fs = FIELD_EX32(tb_flags, TB_FLAGS, MSTATUS_HS_FS);
1172     ctx->mstatus_hs_vs = FIELD_EX32(tb_flags, TB_FLAGS, MSTATUS_HS_VS);
1173     ctx->hlsx = FIELD_EX32(tb_flags, TB_FLAGS, HLSX);
1174     ctx->vill = FIELD_EX32(tb_flags, TB_FLAGS, VILL);
1175     ctx->sew = FIELD_EX32(tb_flags, TB_FLAGS, SEW);
1176     ctx->lmul = sextract32(FIELD_EX32(tb_flags, TB_FLAGS, LMUL), 0, 3);
1177     ctx->vta = FIELD_EX32(tb_flags, TB_FLAGS, VTA) && cpu->cfg.rvv_ta_all_1s;
1178     ctx->vma = FIELD_EX32(tb_flags, TB_FLAGS, VMA) && cpu->cfg.rvv_ma_all_1s;
1179     ctx->cfg_vta_all_1s = cpu->cfg.rvv_ta_all_1s;
1180     ctx->vstart = env->vstart;
1181     ctx->vl_eq_vlmax = FIELD_EX32(tb_flags, TB_FLAGS, VL_EQ_VLMAX);
1182     ctx->misa_mxl_max = env->misa_mxl_max;
1183     ctx->xl = FIELD_EX32(tb_flags, TB_FLAGS, XL);
1184     ctx->cs = cs;
1185     ctx->ntemp = 0;
1186     memset(ctx->temp, 0, sizeof(ctx->temp));
1187     ctx->nftemp = 0;
1188     memset(ctx->ftemp, 0, sizeof(ctx->ftemp));
1189     ctx->pm_mask_enabled = FIELD_EX32(tb_flags, TB_FLAGS, PM_MASK_ENABLED);
1190     ctx->pm_base_enabled = FIELD_EX32(tb_flags, TB_FLAGS, PM_BASE_ENABLED);
1191     ctx->itrigger = FIELD_EX32(tb_flags, TB_FLAGS, ITRIGGER);
1192     ctx->zero = tcg_constant_tl(0);
1193 }
1194 
1195 static void riscv_tr_tb_start(DisasContextBase *db, CPUState *cpu)
1196 {
1197 }
1198 
1199 static void riscv_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
1200 {
1201     DisasContext *ctx = container_of(dcbase, DisasContext, base);
1202 
1203     tcg_gen_insn_start(ctx->base.pc_next, 0);
1204     ctx->insn_start = tcg_last_op();
1205 }
1206 
1207 static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
1208 {
1209     DisasContext *ctx = container_of(dcbase, DisasContext, base);
1210     CPURISCVState *env = cpu->env_ptr;
1211     uint16_t opcode16 = translator_lduw(env, &ctx->base, ctx->base.pc_next);
1212     int i;
1213 
1214     ctx->ol = ctx->xl;
1215     decode_opc(env, ctx, opcode16);
1216     ctx->base.pc_next = ctx->pc_succ_insn;
1217 
1218     for (i = ctx->ntemp - 1; i >= 0; --i) {
1219         tcg_temp_free(ctx->temp[i]);
1220         ctx->temp[i] = NULL;
1221     }
1222     ctx->ntemp = 0;
1223     for (i = ctx->nftemp - 1; i >= 0; --i) {
1224         tcg_temp_free_i64(ctx->ftemp[i]);
1225         ctx->ftemp[i] = NULL;
1226     }
1227     ctx->nftemp = 0;
1228 
1229     /* Only the first insn within a TB is allowed to cross a page boundary. */
1230     if (ctx->base.is_jmp == DISAS_NEXT) {
1231         if (ctx->itrigger || !is_same_page(&ctx->base, ctx->base.pc_next)) {
1232             ctx->base.is_jmp = DISAS_TOO_MANY;
1233         } else {
1234             unsigned page_ofs = ctx->base.pc_next & ~TARGET_PAGE_MASK;
1235 
1236             if (page_ofs > TARGET_PAGE_SIZE - MAX_INSN_LEN) {
1237                 uint16_t next_insn = cpu_lduw_code(env, ctx->base.pc_next);
1238                 int len = insn_len(next_insn);
1239 
1240                 if (!is_same_page(&ctx->base, ctx->base.pc_next + len)) {
1241                     ctx->base.is_jmp = DISAS_TOO_MANY;
1242                 }
1243             }
1244         }
1245     }
1246 }
1247 
1248 static void riscv_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
1249 {
1250     DisasContext *ctx = container_of(dcbase, DisasContext, base);
1251 
1252     switch (ctx->base.is_jmp) {
1253     case DISAS_TOO_MANY:
1254         gen_goto_tb(ctx, 0, ctx->base.pc_next);
1255         break;
1256     case DISAS_NORETURN:
1257         break;
1258     default:
1259         g_assert_not_reached();
1260     }
1261 }
1262 
1263 static void riscv_tr_disas_log(const DisasContextBase *dcbase,
1264                                CPUState *cpu, FILE *logfile)
1265 {
1266 #ifndef CONFIG_USER_ONLY
1267     RISCVCPU *rvcpu = RISCV_CPU(cpu);
1268     CPURISCVState *env = &rvcpu->env;
1269 #endif
1270 
1271     fprintf(logfile, "IN: %s\n", lookup_symbol(dcbase->pc_first));
1272 #ifndef CONFIG_USER_ONLY
1273     fprintf(logfile, "Priv: "TARGET_FMT_ld"; Virt: "TARGET_FMT_ld"\n",
1274             env->priv, env->virt);
1275 #endif
1276     target_disas(logfile, cpu, dcbase->pc_first, dcbase->tb->size);
1277 }
1278 
1279 static const TranslatorOps riscv_tr_ops = {
1280     .init_disas_context = riscv_tr_init_disas_context,
1281     .tb_start           = riscv_tr_tb_start,
1282     .insn_start         = riscv_tr_insn_start,
1283     .translate_insn     = riscv_tr_translate_insn,
1284     .tb_stop            = riscv_tr_tb_stop,
1285     .disas_log          = riscv_tr_disas_log,
1286 };
1287 
1288 void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns,
1289                            target_ulong pc, void *host_pc)
1290 {
1291     DisasContext ctx;
1292 
1293     translator_loop(cs, tb, max_insns, pc, host_pc, &riscv_tr_ops, &ctx.base);
1294 }
1295 
1296 void riscv_translate_init(void)
1297 {
1298     int i;
1299 
1300     /*
1301      * cpu_gpr[0] is a placeholder for the zero register. Do not use it.
1302      * Use the gen_set_gpr and get_gpr helper functions when accessing regs,
1303      * unless you specifically block reads/writes to reg 0.
1304      */
1305     cpu_gpr[0] = NULL;
1306     cpu_gprh[0] = NULL;
1307 
1308     for (i = 1; i < 32; i++) {
1309         cpu_gpr[i] = tcg_global_mem_new(cpu_env,
1310             offsetof(CPURISCVState, gpr[i]), riscv_int_regnames[i]);
1311         cpu_gprh[i] = tcg_global_mem_new(cpu_env,
1312             offsetof(CPURISCVState, gprh[i]), riscv_int_regnamesh[i]);
1313     }
1314 
1315     for (i = 0; i < 32; i++) {
1316         cpu_fpr[i] = tcg_global_mem_new_i64(cpu_env,
1317             offsetof(CPURISCVState, fpr[i]), riscv_fpr_regnames[i]);
1318     }
1319 
1320     cpu_pc = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, pc), "pc");
1321     cpu_vl = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, vl), "vl");
1322     cpu_vstart = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, vstart),
1323                             "vstart");
1324     load_res = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, load_res),
1325                              "load_res");
1326     load_val = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, load_val),
1327                              "load_val");
1328     /* Assign PM CSRs to tcg globals */
1329     pm_mask = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, cur_pmmask),
1330                                  "pmmask");
1331     pm_base = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, cur_pmbase),
1332                                  "pmbase");
1333 }
1334