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