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