xref: /openbmc/qemu/target/riscv/translate.c (revision cc13aa36)
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 
32 #include "instmap.h"
33 #include "internals.h"
34 
35 /* global register indices */
36 static TCGv cpu_gpr[32], cpu_pc, cpu_vl, cpu_vstart;
37 static TCGv_i64 cpu_fpr[32]; /* assume F and D extensions */
38 static TCGv load_res;
39 static TCGv load_val;
40 /* globals for PM CSRs */
41 static TCGv pm_mask[4];
42 static TCGv pm_base[4];
43 
44 #include "exec/gen-icount.h"
45 
46 /*
47  * If an operation is being performed on less than TARGET_LONG_BITS,
48  * it may require the inputs to be sign- or zero-extended; which will
49  * depend on the exact operation being performed.
50  */
51 typedef enum {
52     EXT_NONE,
53     EXT_SIGN,
54     EXT_ZERO,
55 } DisasExtend;
56 
57 typedef struct DisasContext {
58     DisasContextBase base;
59     /* pc_succ_insn points to the instruction following base.pc_next */
60     target_ulong pc_succ_insn;
61     target_ulong priv_ver;
62     RISCVMXL xl;
63     uint32_t misa_ext;
64     uint32_t opcode;
65     uint32_t mstatus_fs;
66     uint32_t mstatus_vs;
67     uint32_t mstatus_hs_fs;
68     uint32_t mstatus_hs_vs;
69     uint32_t mem_idx;
70     /* Remember the rounding mode encoded in the previous fp instruction,
71        which we have already installed into env->fp_status.  Or -1 for
72        no previous fp instruction.  Note that we exit the TB when writing
73        to any system register, which includes CSR_FRM, so we do not have
74        to reset this known value.  */
75     int frm;
76     RISCVMXL ol;
77     bool virt_enabled;
78     bool ext_ifencei;
79     bool ext_zfh;
80     bool ext_zfhmin;
81     bool hlsx;
82     /* vector extension */
83     bool vill;
84     /*
85      * Encode LMUL to lmul as follows:
86      *     LMUL    vlmul    lmul
87      *      1       000       0
88      *      2       001       1
89      *      4       010       2
90      *      8       011       3
91      *      -       100       -
92      *     1/8      101      -3
93      *     1/4      110      -2
94      *     1/2      111      -1
95      */
96     int8_t lmul;
97     uint8_t sew;
98     uint16_t vlen;
99     uint16_t elen;
100     target_ulong vstart;
101     bool vl_eq_vlmax;
102     uint8_t ntemp;
103     CPUState *cs;
104     TCGv zero;
105     /* Space for 3 operands plus 1 extra for address computation. */
106     TCGv temp[4];
107     /* PointerMasking extension */
108     bool pm_enabled;
109     TCGv pm_mask;
110     TCGv pm_base;
111 } DisasContext;
112 
113 static inline bool has_ext(DisasContext *ctx, uint32_t ext)
114 {
115     return ctx->misa_ext & ext;
116 }
117 
118 #ifdef TARGET_RISCV32
119 #define get_xl(ctx)    MXL_RV32
120 #elif defined(CONFIG_USER_ONLY)
121 #define get_xl(ctx)    MXL_RV64
122 #else
123 #define get_xl(ctx)    ((ctx)->xl)
124 #endif
125 
126 /* The word size for this machine mode. */
127 static inline int __attribute__((unused)) get_xlen(DisasContext *ctx)
128 {
129     return 16 << get_xl(ctx);
130 }
131 
132 /* The operation length, as opposed to the xlen. */
133 #ifdef TARGET_RISCV32
134 #define get_ol(ctx)    MXL_RV32
135 #else
136 #define get_ol(ctx)    ((ctx)->ol)
137 #endif
138 
139 static inline int get_olen(DisasContext *ctx)
140 {
141     return 16 << get_ol(ctx);
142 }
143 
144 /*
145  * RISC-V requires NaN-boxing of narrower width floating point values.
146  * This applies when a 32-bit value is assigned to a 64-bit FP register.
147  * For consistency and simplicity, we nanbox results even when the RVD
148  * extension is not present.
149  */
150 static void gen_nanbox_s(TCGv_i64 out, TCGv_i64 in)
151 {
152     tcg_gen_ori_i64(out, in, MAKE_64BIT_MASK(32, 32));
153 }
154 
155 static void gen_nanbox_h(TCGv_i64 out, TCGv_i64 in)
156 {
157     tcg_gen_ori_i64(out, in, MAKE_64BIT_MASK(16, 48));
158 }
159 
160 /*
161  * A narrow n-bit operation, where n < FLEN, checks that input operands
162  * are correctly Nan-boxed, i.e., all upper FLEN - n bits are 1.
163  * If so, the least-significant bits of the input are used, otherwise the
164  * input value is treated as an n-bit canonical NaN (v2.2 section 9.2).
165  *
166  * Here, the result is always nan-boxed, even the canonical nan.
167  */
168 static void gen_check_nanbox_h(TCGv_i64 out, TCGv_i64 in)
169 {
170     TCGv_i64 t_max = tcg_const_i64(0xffffffffffff0000ull);
171     TCGv_i64 t_nan = tcg_const_i64(0xffffffffffff7e00ull);
172 
173     tcg_gen_movcond_i64(TCG_COND_GEU, out, in, t_max, in, t_nan);
174     tcg_temp_free_i64(t_max);
175     tcg_temp_free_i64(t_nan);
176 }
177 
178 static void gen_check_nanbox_s(TCGv_i64 out, TCGv_i64 in)
179 {
180     TCGv_i64 t_max = tcg_constant_i64(0xffffffff00000000ull);
181     TCGv_i64 t_nan = tcg_constant_i64(0xffffffff7fc00000ull);
182 
183     tcg_gen_movcond_i64(TCG_COND_GEU, out, in, t_max, in, t_nan);
184 }
185 
186 static void generate_exception(DisasContext *ctx, int excp)
187 {
188     tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next);
189     gen_helper_raise_exception(cpu_env, tcg_constant_i32(excp));
190     ctx->base.is_jmp = DISAS_NORETURN;
191 }
192 
193 static void generate_exception_mtval(DisasContext *ctx, int excp)
194 {
195     tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next);
196     tcg_gen_st_tl(cpu_pc, cpu_env, offsetof(CPURISCVState, badaddr));
197     gen_helper_raise_exception(cpu_env, tcg_constant_i32(excp));
198     ctx->base.is_jmp = DISAS_NORETURN;
199 }
200 
201 static void gen_exception_illegal(DisasContext *ctx)
202 {
203     generate_exception(ctx, RISCV_EXCP_ILLEGAL_INST);
204 }
205 
206 static void gen_exception_inst_addr_mis(DisasContext *ctx)
207 {
208     generate_exception_mtval(ctx, RISCV_EXCP_INST_ADDR_MIS);
209 }
210 
211 static void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
212 {
213     if (translator_use_goto_tb(&ctx->base, dest)) {
214         tcg_gen_goto_tb(n);
215         tcg_gen_movi_tl(cpu_pc, dest);
216         tcg_gen_exit_tb(ctx->base.tb, n);
217     } else {
218         tcg_gen_movi_tl(cpu_pc, dest);
219         tcg_gen_lookup_and_goto_ptr();
220     }
221 }
222 
223 /*
224  * Wrappers for getting reg values.
225  *
226  * The $zero register does not have cpu_gpr[0] allocated -- we supply the
227  * constant zero as a source, and an uninitialized sink as destination.
228  *
229  * Further, we may provide an extension for word operations.
230  */
231 static TCGv temp_new(DisasContext *ctx)
232 {
233     assert(ctx->ntemp < ARRAY_SIZE(ctx->temp));
234     return ctx->temp[ctx->ntemp++] = tcg_temp_new();
235 }
236 
237 static TCGv get_gpr(DisasContext *ctx, int reg_num, DisasExtend ext)
238 {
239     TCGv t;
240 
241     if (reg_num == 0) {
242         return ctx->zero;
243     }
244 
245     switch (get_ol(ctx)) {
246     case MXL_RV32:
247         switch (ext) {
248         case EXT_NONE:
249             break;
250         case EXT_SIGN:
251             t = temp_new(ctx);
252             tcg_gen_ext32s_tl(t, cpu_gpr[reg_num]);
253             return t;
254         case EXT_ZERO:
255             t = temp_new(ctx);
256             tcg_gen_ext32u_tl(t, cpu_gpr[reg_num]);
257             return t;
258         default:
259             g_assert_not_reached();
260         }
261         break;
262     case MXL_RV64:
263         break;
264     default:
265         g_assert_not_reached();
266     }
267     return cpu_gpr[reg_num];
268 }
269 
270 static TCGv dest_gpr(DisasContext *ctx, int reg_num)
271 {
272     if (reg_num == 0 || get_olen(ctx) < TARGET_LONG_BITS) {
273         return temp_new(ctx);
274     }
275     return cpu_gpr[reg_num];
276 }
277 
278 static void gen_set_gpr(DisasContext *ctx, int reg_num, TCGv t)
279 {
280     if (reg_num != 0) {
281         switch (get_ol(ctx)) {
282         case MXL_RV32:
283             tcg_gen_ext32s_tl(cpu_gpr[reg_num], t);
284             break;
285         case MXL_RV64:
286             tcg_gen_mov_tl(cpu_gpr[reg_num], t);
287             break;
288         default:
289             g_assert_not_reached();
290         }
291     }
292 }
293 
294 static void gen_jal(DisasContext *ctx, int rd, target_ulong imm)
295 {
296     target_ulong next_pc;
297 
298     /* check misaligned: */
299     next_pc = ctx->base.pc_next + imm;
300     if (!has_ext(ctx, RVC)) {
301         if ((next_pc & 0x3) != 0) {
302             gen_exception_inst_addr_mis(ctx);
303             return;
304         }
305     }
306     if (rd != 0) {
307         tcg_gen_movi_tl(cpu_gpr[rd], ctx->pc_succ_insn);
308     }
309 
310     gen_goto_tb(ctx, 0, ctx->base.pc_next + imm); /* must use this for safety */
311     ctx->base.is_jmp = DISAS_NORETURN;
312 }
313 
314 /*
315  * Generates address adjustment for PointerMasking
316  */
317 static TCGv gen_pm_adjust_address(DisasContext *s, TCGv src)
318 {
319     TCGv temp;
320     if (!s->pm_enabled) {
321         /* Load unmodified address */
322         return src;
323     } else {
324         temp = temp_new(s);
325         tcg_gen_andc_tl(temp, src, s->pm_mask);
326         tcg_gen_or_tl(temp, temp, s->pm_base);
327         return temp;
328     }
329 }
330 
331 #ifndef CONFIG_USER_ONLY
332 /* The states of mstatus_fs are:
333  * 0 = disabled, 1 = initial, 2 = clean, 3 = dirty
334  * We will have already diagnosed disabled state,
335  * and need to turn initial/clean into dirty.
336  */
337 static void mark_fs_dirty(DisasContext *ctx)
338 {
339     TCGv tmp;
340 
341     if (ctx->mstatus_fs != MSTATUS_FS) {
342         /* Remember the state change for the rest of the TB. */
343         ctx->mstatus_fs = MSTATUS_FS;
344 
345         tmp = tcg_temp_new();
346         tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
347         tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS);
348         tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
349         tcg_temp_free(tmp);
350     }
351 
352     if (ctx->virt_enabled && ctx->mstatus_hs_fs != MSTATUS_FS) {
353         /* Remember the stage change for the rest of the TB. */
354         ctx->mstatus_hs_fs = MSTATUS_FS;
355 
356         tmp = tcg_temp_new();
357         tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
358         tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS);
359         tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
360         tcg_temp_free(tmp);
361     }
362 }
363 #else
364 static inline void mark_fs_dirty(DisasContext *ctx) { }
365 #endif
366 
367 #ifndef CONFIG_USER_ONLY
368 /* The states of mstatus_vs are:
369  * 0 = disabled, 1 = initial, 2 = clean, 3 = dirty
370  * We will have already diagnosed disabled state,
371  * and need to turn initial/clean into dirty.
372  */
373 static void mark_vs_dirty(DisasContext *ctx)
374 {
375     TCGv tmp;
376 
377     if (ctx->mstatus_vs != MSTATUS_VS) {
378         /* Remember the state change for the rest of the TB.  */
379         ctx->mstatus_vs = MSTATUS_VS;
380 
381         tmp = tcg_temp_new();
382         tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
383         tcg_gen_ori_tl(tmp, tmp, MSTATUS_VS);
384         tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
385         tcg_temp_free(tmp);
386     }
387 
388     if (ctx->virt_enabled && ctx->mstatus_hs_vs != MSTATUS_VS) {
389         /* Remember the stage change for the rest of the TB. */
390         ctx->mstatus_hs_vs = MSTATUS_VS;
391 
392         tmp = tcg_temp_new();
393         tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
394         tcg_gen_ori_tl(tmp, tmp, MSTATUS_VS);
395         tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
396         tcg_temp_free(tmp);
397     }
398 }
399 #else
400 static inline void mark_vs_dirty(DisasContext *ctx) { }
401 #endif
402 
403 static void gen_set_rm(DisasContext *ctx, int rm)
404 {
405     if (ctx->frm == rm) {
406         return;
407     }
408     ctx->frm = rm;
409 
410     if (rm == RISCV_FRM_ROD) {
411         gen_helper_set_rod_rounding_mode(cpu_env);
412         return;
413     }
414 
415     gen_helper_set_rounding_mode(cpu_env, tcg_constant_i32(rm));
416 }
417 
418 static int ex_plus_1(DisasContext *ctx, int nf)
419 {
420     return nf + 1;
421 }
422 
423 #define EX_SH(amount) \
424     static int ex_shift_##amount(DisasContext *ctx, int imm) \
425     {                                         \
426         return imm << amount;                 \
427     }
428 EX_SH(1)
429 EX_SH(2)
430 EX_SH(3)
431 EX_SH(4)
432 EX_SH(12)
433 
434 #define REQUIRE_EXT(ctx, ext) do { \
435     if (!has_ext(ctx, ext)) {      \
436         return false;              \
437     }                              \
438 } while (0)
439 
440 #define REQUIRE_32BIT(ctx) do {    \
441     if (get_xl(ctx) != MXL_RV32) { \
442         return false;              \
443     }                              \
444 } while (0)
445 
446 #define REQUIRE_64BIT(ctx) do {    \
447     if (get_xl(ctx) < MXL_RV64) {  \
448         return false;              \
449     }                              \
450 } while (0)
451 
452 static int ex_rvc_register(DisasContext *ctx, int reg)
453 {
454     return 8 + reg;
455 }
456 
457 static int ex_rvc_shifti(DisasContext *ctx, int imm)
458 {
459     /* For RV128 a shamt of 0 means a shift by 64. */
460     return imm ? imm : 64;
461 }
462 
463 /* Include the auto-generated decoder for 32 bit insn */
464 #include "decode-insn32.c.inc"
465 
466 static bool gen_arith_imm_fn(DisasContext *ctx, arg_i *a, DisasExtend ext,
467                              void (*func)(TCGv, TCGv, target_long))
468 {
469     TCGv dest = dest_gpr(ctx, a->rd);
470     TCGv src1 = get_gpr(ctx, a->rs1, ext);
471 
472     func(dest, src1, a->imm);
473 
474     gen_set_gpr(ctx, a->rd, dest);
475     return true;
476 }
477 
478 static bool gen_arith_imm_tl(DisasContext *ctx, arg_i *a, DisasExtend ext,
479                              void (*func)(TCGv, TCGv, TCGv))
480 {
481     TCGv dest = dest_gpr(ctx, a->rd);
482     TCGv src1 = get_gpr(ctx, a->rs1, ext);
483     TCGv src2 = tcg_constant_tl(a->imm);
484 
485     func(dest, src1, src2);
486 
487     gen_set_gpr(ctx, a->rd, dest);
488     return true;
489 }
490 
491 static bool gen_arith(DisasContext *ctx, arg_r *a, DisasExtend ext,
492                       void (*func)(TCGv, TCGv, TCGv))
493 {
494     TCGv dest = dest_gpr(ctx, a->rd);
495     TCGv src1 = get_gpr(ctx, a->rs1, ext);
496     TCGv src2 = get_gpr(ctx, a->rs2, ext);
497 
498     func(dest, src1, src2);
499 
500     gen_set_gpr(ctx, a->rd, dest);
501     return true;
502 }
503 
504 static bool gen_arith_per_ol(DisasContext *ctx, arg_r *a, DisasExtend ext,
505                              void (*f_tl)(TCGv, TCGv, TCGv),
506                              void (*f_32)(TCGv, TCGv, TCGv))
507 {
508     int olen = get_olen(ctx);
509 
510     if (olen != TARGET_LONG_BITS) {
511         if (olen == 32) {
512             f_tl = f_32;
513         } else {
514             g_assert_not_reached();
515         }
516     }
517     return gen_arith(ctx, a, ext, f_tl);
518 }
519 
520 static bool gen_shift_imm_fn(DisasContext *ctx, arg_shift *a, DisasExtend ext,
521                              void (*func)(TCGv, TCGv, target_long))
522 {
523     TCGv dest, src1;
524     int max_len = get_olen(ctx);
525 
526     if (a->shamt >= max_len) {
527         return false;
528     }
529 
530     dest = dest_gpr(ctx, a->rd);
531     src1 = get_gpr(ctx, a->rs1, ext);
532 
533     func(dest, src1, a->shamt);
534 
535     gen_set_gpr(ctx, a->rd, dest);
536     return true;
537 }
538 
539 static bool gen_shift_imm_fn_per_ol(DisasContext *ctx, arg_shift *a,
540                                     DisasExtend ext,
541                                     void (*f_tl)(TCGv, TCGv, target_long),
542                                     void (*f_32)(TCGv, TCGv, target_long))
543 {
544     int olen = get_olen(ctx);
545     if (olen != TARGET_LONG_BITS) {
546         if (olen == 32) {
547             f_tl = f_32;
548         } else {
549             g_assert_not_reached();
550         }
551     }
552     return gen_shift_imm_fn(ctx, a, ext, f_tl);
553 }
554 
555 static bool gen_shift_imm_tl(DisasContext *ctx, arg_shift *a, DisasExtend ext,
556                              void (*func)(TCGv, TCGv, TCGv))
557 {
558     TCGv dest, src1, src2;
559     int max_len = get_olen(ctx);
560 
561     if (a->shamt >= max_len) {
562         return false;
563     }
564 
565     dest = dest_gpr(ctx, a->rd);
566     src1 = get_gpr(ctx, a->rs1, ext);
567     src2 = tcg_constant_tl(a->shamt);
568 
569     func(dest, src1, src2);
570 
571     gen_set_gpr(ctx, a->rd, dest);
572     return true;
573 }
574 
575 static bool gen_shift(DisasContext *ctx, arg_r *a, DisasExtend ext,
576                       void (*func)(TCGv, TCGv, TCGv))
577 {
578     TCGv dest = dest_gpr(ctx, a->rd);
579     TCGv src1 = get_gpr(ctx, a->rs1, ext);
580     TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE);
581     TCGv ext2 = tcg_temp_new();
582 
583     tcg_gen_andi_tl(ext2, src2, get_olen(ctx) - 1);
584     func(dest, src1, ext2);
585 
586     gen_set_gpr(ctx, a->rd, dest);
587     tcg_temp_free(ext2);
588     return true;
589 }
590 
591 static bool gen_shift_per_ol(DisasContext *ctx, arg_r *a, DisasExtend ext,
592                              void (*f_tl)(TCGv, TCGv, TCGv),
593                              void (*f_32)(TCGv, TCGv, TCGv))
594 {
595     int olen = get_olen(ctx);
596     if (olen != TARGET_LONG_BITS) {
597         if (olen == 32) {
598             f_tl = f_32;
599         } else {
600             g_assert_not_reached();
601         }
602     }
603     return gen_shift(ctx, a, ext, f_tl);
604 }
605 
606 static bool gen_unary(DisasContext *ctx, arg_r2 *a, DisasExtend ext,
607                       void (*func)(TCGv, TCGv))
608 {
609     TCGv dest = dest_gpr(ctx, a->rd);
610     TCGv src1 = get_gpr(ctx, a->rs1, ext);
611 
612     func(dest, src1);
613 
614     gen_set_gpr(ctx, a->rd, dest);
615     return true;
616 }
617 
618 static bool gen_unary_per_ol(DisasContext *ctx, arg_r2 *a, DisasExtend ext,
619                              void (*f_tl)(TCGv, TCGv),
620                              void (*f_32)(TCGv, TCGv))
621 {
622     int olen = get_olen(ctx);
623 
624     if (olen != TARGET_LONG_BITS) {
625         if (olen == 32) {
626             f_tl = f_32;
627         } else {
628             g_assert_not_reached();
629         }
630     }
631     return gen_unary(ctx, a, ext, f_tl);
632 }
633 
634 static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)
635 {
636     DisasContext *ctx = container_of(dcbase, DisasContext, base);
637     CPUState *cpu = ctx->cs;
638     CPURISCVState *env = cpu->env_ptr;
639 
640     return cpu_ldl_code(env, pc);
641 }
642 
643 /* Include insn module translation function */
644 #include "insn_trans/trans_rvi.c.inc"
645 #include "insn_trans/trans_rvm.c.inc"
646 #include "insn_trans/trans_rva.c.inc"
647 #include "insn_trans/trans_rvf.c.inc"
648 #include "insn_trans/trans_rvd.c.inc"
649 #include "insn_trans/trans_rvh.c.inc"
650 #include "insn_trans/trans_rvv.c.inc"
651 #include "insn_trans/trans_rvb.c.inc"
652 #include "insn_trans/trans_rvzfh.c.inc"
653 #include "insn_trans/trans_privileged.c.inc"
654 
655 /* Include the auto-generated decoder for 16 bit insn */
656 #include "decode-insn16.c.inc"
657 
658 static void decode_opc(CPURISCVState *env, DisasContext *ctx, uint16_t opcode)
659 {
660     /* check for compressed insn */
661     if (extract16(opcode, 0, 2) != 3) {
662         if (!has_ext(ctx, RVC)) {
663             gen_exception_illegal(ctx);
664         } else {
665             ctx->pc_succ_insn = ctx->base.pc_next + 2;
666             if (!decode_insn16(ctx, opcode)) {
667                 gen_exception_illegal(ctx);
668             }
669         }
670     } else {
671         uint32_t opcode32 = opcode;
672         opcode32 = deposit32(opcode32, 16, 16,
673                              translator_lduw(env, &ctx->base,
674                                              ctx->base.pc_next + 2));
675         ctx->pc_succ_insn = ctx->base.pc_next + 4;
676         if (!decode_insn32(ctx, opcode32)) {
677             gen_exception_illegal(ctx);
678         }
679     }
680 }
681 
682 static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
683 {
684     DisasContext *ctx = container_of(dcbase, DisasContext, base);
685     CPURISCVState *env = cs->env_ptr;
686     RISCVCPU *cpu = RISCV_CPU(cs);
687     uint32_t tb_flags = ctx->base.tb->flags;
688 
689     ctx->pc_succ_insn = ctx->base.pc_first;
690     ctx->mem_idx = FIELD_EX32(tb_flags, TB_FLAGS, MEM_IDX);
691     ctx->mstatus_fs = tb_flags & TB_FLAGS_MSTATUS_FS;
692     ctx->mstatus_vs = tb_flags & TB_FLAGS_MSTATUS_VS;
693     ctx->priv_ver = env->priv_ver;
694 #if !defined(CONFIG_USER_ONLY)
695     if (riscv_has_ext(env, RVH)) {
696         ctx->virt_enabled = riscv_cpu_virt_enabled(env);
697     } else {
698         ctx->virt_enabled = false;
699     }
700 #else
701     ctx->virt_enabled = false;
702 #endif
703     ctx->misa_ext = env->misa_ext;
704     ctx->frm = -1;  /* unknown rounding mode */
705     ctx->ext_ifencei = cpu->cfg.ext_ifencei;
706     ctx->ext_zfh = cpu->cfg.ext_zfh;
707     ctx->ext_zfhmin = cpu->cfg.ext_zfhmin;
708     ctx->vlen = cpu->cfg.vlen;
709     ctx->elen = cpu->cfg.elen;
710     ctx->mstatus_hs_fs = FIELD_EX32(tb_flags, TB_FLAGS, MSTATUS_HS_FS);
711     ctx->mstatus_hs_vs = FIELD_EX32(tb_flags, TB_FLAGS, MSTATUS_HS_VS);
712     ctx->hlsx = FIELD_EX32(tb_flags, TB_FLAGS, HLSX);
713     ctx->vill = FIELD_EX32(tb_flags, TB_FLAGS, VILL);
714     ctx->sew = FIELD_EX32(tb_flags, TB_FLAGS, SEW);
715     ctx->lmul = sextract32(FIELD_EX32(tb_flags, TB_FLAGS, LMUL), 0, 3);
716     ctx->vstart = env->vstart;
717     ctx->vl_eq_vlmax = FIELD_EX32(tb_flags, TB_FLAGS, VL_EQ_VLMAX);
718     ctx->xl = FIELD_EX32(tb_flags, TB_FLAGS, XL);
719     ctx->cs = cs;
720     ctx->ntemp = 0;
721     memset(ctx->temp, 0, sizeof(ctx->temp));
722     ctx->pm_enabled = FIELD_EX32(tb_flags, TB_FLAGS, PM_ENABLED);
723     int priv = tb_flags & TB_FLAGS_PRIV_MMU_MASK;
724     ctx->pm_mask = pm_mask[priv];
725     ctx->pm_base = pm_base[priv];
726 
727     ctx->zero = tcg_constant_tl(0);
728 }
729 
730 static void riscv_tr_tb_start(DisasContextBase *db, CPUState *cpu)
731 {
732 }
733 
734 static void riscv_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
735 {
736     DisasContext *ctx = container_of(dcbase, DisasContext, base);
737 
738     tcg_gen_insn_start(ctx->base.pc_next);
739 }
740 
741 static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
742 {
743     DisasContext *ctx = container_of(dcbase, DisasContext, base);
744     CPURISCVState *env = cpu->env_ptr;
745     uint16_t opcode16 = translator_lduw(env, &ctx->base, ctx->base.pc_next);
746 
747     ctx->ol = ctx->xl;
748     decode_opc(env, ctx, opcode16);
749     ctx->base.pc_next = ctx->pc_succ_insn;
750 
751     for (int i = ctx->ntemp - 1; i >= 0; --i) {
752         tcg_temp_free(ctx->temp[i]);
753         ctx->temp[i] = NULL;
754     }
755     ctx->ntemp = 0;
756 
757     if (ctx->base.is_jmp == DISAS_NEXT) {
758         target_ulong page_start;
759 
760         page_start = ctx->base.pc_first & TARGET_PAGE_MASK;
761         if (ctx->base.pc_next - page_start >= TARGET_PAGE_SIZE) {
762             ctx->base.is_jmp = DISAS_TOO_MANY;
763         }
764     }
765 }
766 
767 static void riscv_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
768 {
769     DisasContext *ctx = container_of(dcbase, DisasContext, base);
770 
771     switch (ctx->base.is_jmp) {
772     case DISAS_TOO_MANY:
773         gen_goto_tb(ctx, 0, ctx->base.pc_next);
774         break;
775     case DISAS_NORETURN:
776         break;
777     default:
778         g_assert_not_reached();
779     }
780 }
781 
782 static void riscv_tr_disas_log(const DisasContextBase *dcbase, CPUState *cpu)
783 {
784 #ifndef CONFIG_USER_ONLY
785     RISCVCPU *rvcpu = RISCV_CPU(cpu);
786     CPURISCVState *env = &rvcpu->env;
787 #endif
788 
789     qemu_log("IN: %s\n", lookup_symbol(dcbase->pc_first));
790 #ifndef CONFIG_USER_ONLY
791     qemu_log("Priv: "TARGET_FMT_ld"; Virt: "TARGET_FMT_ld"\n", env->priv, env->virt);
792 #endif
793     log_target_disas(cpu, dcbase->pc_first, dcbase->tb->size);
794 }
795 
796 static const TranslatorOps riscv_tr_ops = {
797     .init_disas_context = riscv_tr_init_disas_context,
798     .tb_start           = riscv_tr_tb_start,
799     .insn_start         = riscv_tr_insn_start,
800     .translate_insn     = riscv_tr_translate_insn,
801     .tb_stop            = riscv_tr_tb_stop,
802     .disas_log          = riscv_tr_disas_log,
803 };
804 
805 void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
806 {
807     DisasContext ctx;
808 
809     translator_loop(&riscv_tr_ops, &ctx.base, cs, tb, max_insns);
810 }
811 
812 void riscv_translate_init(void)
813 {
814     int i;
815 
816     /*
817      * cpu_gpr[0] is a placeholder for the zero register. Do not use it.
818      * Use the gen_set_gpr and get_gpr helper functions when accessing regs,
819      * unless you specifically block reads/writes to reg 0.
820      */
821     cpu_gpr[0] = NULL;
822 
823     for (i = 1; i < 32; i++) {
824         cpu_gpr[i] = tcg_global_mem_new(cpu_env,
825             offsetof(CPURISCVState, gpr[i]), riscv_int_regnames[i]);
826     }
827 
828     for (i = 0; i < 32; i++) {
829         cpu_fpr[i] = tcg_global_mem_new_i64(cpu_env,
830             offsetof(CPURISCVState, fpr[i]), riscv_fpr_regnames[i]);
831     }
832 
833     cpu_pc = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, pc), "pc");
834     cpu_vl = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, vl), "vl");
835     cpu_vstart = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, vstart),
836                             "vstart");
837     load_res = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, load_res),
838                              "load_res");
839     load_val = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, load_val),
840                              "load_val");
841 #ifndef CONFIG_USER_ONLY
842     /* Assign PM CSRs to tcg globals */
843     pm_mask[PRV_U] =
844       tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, upmmask), "upmmask");
845     pm_base[PRV_U] =
846       tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, upmbase), "upmbase");
847     pm_mask[PRV_S] =
848       tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, spmmask), "spmmask");
849     pm_base[PRV_S] =
850       tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, spmbase), "spmbase");
851     pm_mask[PRV_M] =
852       tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, mpmmask), "mpmmask");
853     pm_base[PRV_M] =
854       tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, mpmbase), "mpmbase");
855 #endif
856 }
857