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