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