xref: /openbmc/qemu/target/riscv/translate.c (revision 191d1daf)
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 
34 /* global register indices */
35 static TCGv cpu_gpr[32], cpu_pc, cpu_vl;
36 static TCGv_i64 cpu_fpr[32]; /* assume F and D extensions */
37 static TCGv load_res;
38 static TCGv load_val;
39 
40 #include "exec/gen-icount.h"
41 
42 /*
43  * If an operation is being performed on less than TARGET_LONG_BITS,
44  * it may require the inputs to be sign- or zero-extended; which will
45  * depend on the exact operation being performed.
46  */
47 typedef enum {
48     EXT_NONE,
49     EXT_SIGN,
50     EXT_ZERO,
51 } DisasExtend;
52 
53 typedef struct DisasContext {
54     DisasContextBase base;
55     /* pc_succ_insn points to the instruction following base.pc_next */
56     target_ulong pc_succ_insn;
57     target_ulong priv_ver;
58     target_ulong misa;
59     uint32_t opcode;
60     uint32_t mstatus_fs;
61     uint32_t mem_idx;
62     /* Remember the rounding mode encoded in the previous fp instruction,
63        which we have already installed into env->fp_status.  Or -1 for
64        no previous fp instruction.  Note that we exit the TB when writing
65        to any system register, which includes CSR_FRM, so we do not have
66        to reset this known value.  */
67     int frm;
68     bool w;
69     bool virt_enabled;
70     bool ext_ifencei;
71     bool hlsx;
72     /* vector extension */
73     bool vill;
74     uint8_t lmul;
75     uint8_t sew;
76     uint16_t vlen;
77     uint16_t mlen;
78     bool vl_eq_vlmax;
79     uint8_t ntemp;
80     CPUState *cs;
81     TCGv zero;
82     /* Space for 3 operands plus 1 extra for address computation. */
83     TCGv temp[4];
84 } DisasContext;
85 
86 static inline bool has_ext(DisasContext *ctx, uint32_t ext)
87 {
88     return ctx->misa & ext;
89 }
90 
91 #ifdef TARGET_RISCV32
92 # define is_32bit(ctx)  true
93 #elif defined(CONFIG_USER_ONLY)
94 # define is_32bit(ctx)  false
95 #else
96 static inline bool is_32bit(DisasContext *ctx)
97 {
98     return (ctx->misa & RV32) == RV32;
99 }
100 #endif
101 
102 /*
103  * RISC-V requires NaN-boxing of narrower width floating point values.
104  * This applies when a 32-bit value is assigned to a 64-bit FP register.
105  * For consistency and simplicity, we nanbox results even when the RVD
106  * extension is not present.
107  */
108 static void gen_nanbox_s(TCGv_i64 out, TCGv_i64 in)
109 {
110     tcg_gen_ori_i64(out, in, MAKE_64BIT_MASK(32, 32));
111 }
112 
113 /*
114  * A narrow n-bit operation, where n < FLEN, checks that input operands
115  * are correctly Nan-boxed, i.e., all upper FLEN - n bits are 1.
116  * If so, the least-significant bits of the input are used, otherwise the
117  * input value is treated as an n-bit canonical NaN (v2.2 section 9.2).
118  *
119  * Here, the result is always nan-boxed, even the canonical nan.
120  */
121 static void gen_check_nanbox_s(TCGv_i64 out, TCGv_i64 in)
122 {
123     TCGv_i64 t_max = tcg_constant_i64(0xffffffff00000000ull);
124     TCGv_i64 t_nan = tcg_constant_i64(0xffffffff7fc00000ull);
125 
126     tcg_gen_movcond_i64(TCG_COND_GEU, out, in, t_max, in, t_nan);
127 }
128 
129 static void generate_exception(DisasContext *ctx, int excp)
130 {
131     tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next);
132     gen_helper_raise_exception(cpu_env, tcg_constant_i32(excp));
133     ctx->base.is_jmp = DISAS_NORETURN;
134 }
135 
136 static void generate_exception_mtval(DisasContext *ctx, int excp)
137 {
138     tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next);
139     tcg_gen_st_tl(cpu_pc, cpu_env, offsetof(CPURISCVState, badaddr));
140     gen_helper_raise_exception(cpu_env, tcg_constant_i32(excp));
141     ctx->base.is_jmp = DISAS_NORETURN;
142 }
143 
144 static void gen_exception_debug(void)
145 {
146     gen_helper_raise_exception(cpu_env, tcg_constant_i32(EXCP_DEBUG));
147 }
148 
149 /* Wrapper around tcg_gen_exit_tb that handles single stepping */
150 static void exit_tb(DisasContext *ctx)
151 {
152     if (ctx->base.singlestep_enabled) {
153         gen_exception_debug();
154     } else {
155         tcg_gen_exit_tb(NULL, 0);
156     }
157 }
158 
159 /* Wrapper around tcg_gen_lookup_and_goto_ptr that handles single stepping */
160 static void lookup_and_goto_ptr(DisasContext *ctx)
161 {
162     if (ctx->base.singlestep_enabled) {
163         gen_exception_debug();
164     } else {
165         tcg_gen_lookup_and_goto_ptr();
166     }
167 }
168 
169 static void gen_exception_illegal(DisasContext *ctx)
170 {
171     generate_exception(ctx, RISCV_EXCP_ILLEGAL_INST);
172 }
173 
174 static void gen_exception_inst_addr_mis(DisasContext *ctx)
175 {
176     generate_exception_mtval(ctx, RISCV_EXCP_INST_ADDR_MIS);
177 }
178 
179 static void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
180 {
181     if (translator_use_goto_tb(&ctx->base, dest)) {
182         tcg_gen_goto_tb(n);
183         tcg_gen_movi_tl(cpu_pc, dest);
184         tcg_gen_exit_tb(ctx->base.tb, n);
185     } else {
186         tcg_gen_movi_tl(cpu_pc, dest);
187         lookup_and_goto_ptr(ctx);
188     }
189 }
190 
191 /*
192  * Wrappers for getting reg values.
193  *
194  * The $zero register does not have cpu_gpr[0] allocated -- we supply the
195  * constant zero as a source, and an uninitialized sink as destination.
196  *
197  * Further, we may provide an extension for word operations.
198  */
199 static TCGv temp_new(DisasContext *ctx)
200 {
201     assert(ctx->ntemp < ARRAY_SIZE(ctx->temp));
202     return ctx->temp[ctx->ntemp++] = tcg_temp_new();
203 }
204 
205 static TCGv get_gpr(DisasContext *ctx, int reg_num, DisasExtend ext)
206 {
207     TCGv t;
208 
209     if (reg_num == 0) {
210         return ctx->zero;
211     }
212 
213     switch (ctx->w ? ext : EXT_NONE) {
214     case EXT_NONE:
215         return cpu_gpr[reg_num];
216     case EXT_SIGN:
217         t = temp_new(ctx);
218         tcg_gen_ext32s_tl(t, cpu_gpr[reg_num]);
219         return t;
220     case EXT_ZERO:
221         t = temp_new(ctx);
222         tcg_gen_ext32u_tl(t, cpu_gpr[reg_num]);
223         return t;
224     }
225     g_assert_not_reached();
226 }
227 
228 static void gen_get_gpr(DisasContext *ctx, TCGv t, int reg_num)
229 {
230     tcg_gen_mov_tl(t, get_gpr(ctx, reg_num, EXT_NONE));
231 }
232 
233 static TCGv dest_gpr(DisasContext *ctx, int reg_num)
234 {
235     if (reg_num == 0 || ctx->w) {
236         return temp_new(ctx);
237     }
238     return cpu_gpr[reg_num];
239 }
240 
241 static void gen_set_gpr(DisasContext *ctx, int reg_num, TCGv t)
242 {
243     if (reg_num != 0) {
244         if (ctx->w) {
245             tcg_gen_ext32s_tl(cpu_gpr[reg_num], t);
246         } else {
247             tcg_gen_mov_tl(cpu_gpr[reg_num], t);
248         }
249     }
250 }
251 
252 static void gen_mulhsu(TCGv ret, TCGv arg1, TCGv arg2)
253 {
254     TCGv rl = tcg_temp_new();
255     TCGv rh = tcg_temp_new();
256 
257     tcg_gen_mulu2_tl(rl, rh, arg1, arg2);
258     /* fix up for one negative */
259     tcg_gen_sari_tl(rl, arg1, TARGET_LONG_BITS - 1);
260     tcg_gen_and_tl(rl, rl, arg2);
261     tcg_gen_sub_tl(ret, rh, rl);
262 
263     tcg_temp_free(rl);
264     tcg_temp_free(rh);
265 }
266 
267 static void gen_div(TCGv ret, TCGv source1, TCGv source2)
268 {
269     TCGv temp1, temp2, zero, one, mone, min;
270 
271     temp1 = tcg_temp_new();
272     temp2 = tcg_temp_new();
273     zero = tcg_constant_tl(0);
274     one = tcg_constant_tl(1);
275     mone = tcg_constant_tl(-1);
276     min = tcg_constant_tl(1ull << (TARGET_LONG_BITS - 1));
277 
278     /*
279      * If overflow, set temp2 to 1, else source2.
280      * This produces the required result of min.
281      */
282     tcg_gen_setcond_tl(TCG_COND_EQ, temp1, source1, min);
283     tcg_gen_setcond_tl(TCG_COND_EQ, temp2, source2, mone);
284     tcg_gen_and_tl(temp1, temp1, temp2);
285     tcg_gen_movcond_tl(TCG_COND_NE, temp2, temp1, zero, one, source2);
286 
287     /*
288      * If div by zero, set temp1 to -1 and temp2 to 1 to
289      * produce the required result of -1.
290      */
291     tcg_gen_movcond_tl(TCG_COND_EQ, temp1, source2, zero, mone, source1);
292     tcg_gen_movcond_tl(TCG_COND_EQ, temp2, source2, zero, one, temp2);
293 
294     tcg_gen_div_tl(ret, temp1, temp2);
295 
296     tcg_temp_free(temp1);
297     tcg_temp_free(temp2);
298 }
299 
300 static void gen_divu(TCGv ret, TCGv source1, TCGv source2)
301 {
302     TCGv temp1, temp2, zero, one, max;
303 
304     temp1 = tcg_temp_new();
305     temp2 = tcg_temp_new();
306     zero = tcg_constant_tl(0);
307     one = tcg_constant_tl(1);
308     max = tcg_constant_tl(~0);
309 
310     /*
311      * If div by zero, set temp1 to max and temp2 to 1 to
312      * produce the required result of max.
313      */
314     tcg_gen_movcond_tl(TCG_COND_EQ, temp1, source2, zero, max, source1);
315     tcg_gen_movcond_tl(TCG_COND_EQ, temp2, source2, zero, one, source2);
316     tcg_gen_divu_tl(ret, temp1, temp2);
317 
318     tcg_temp_free(temp1);
319     tcg_temp_free(temp2);
320 }
321 
322 static void gen_rem(TCGv ret, TCGv source1, TCGv source2)
323 {
324     TCGv temp1, temp2, zero, one, mone, min;
325 
326     temp1 = tcg_temp_new();
327     temp2 = tcg_temp_new();
328     zero = tcg_constant_tl(0);
329     one = tcg_constant_tl(1);
330     mone = tcg_constant_tl(-1);
331     min = tcg_constant_tl(1ull << (TARGET_LONG_BITS - 1));
332 
333     /*
334      * If overflow, set temp1 to 0, else source1.
335      * This avoids a possible host trap, and produces the required result of 0.
336      */
337     tcg_gen_setcond_tl(TCG_COND_EQ, temp1, source1, min);
338     tcg_gen_setcond_tl(TCG_COND_EQ, temp2, source2, mone);
339     tcg_gen_and_tl(temp1, temp1, temp2);
340     tcg_gen_movcond_tl(TCG_COND_NE, temp1, temp1, zero, zero, source1);
341 
342     /*
343      * If div by zero, set temp2 to 1, else source2.
344      * This avoids a possible host trap, but produces an incorrect result.
345      */
346     tcg_gen_movcond_tl(TCG_COND_EQ, temp2, source2, zero, one, source2);
347 
348     tcg_gen_rem_tl(temp1, temp1, temp2);
349 
350     /* If div by zero, the required result is the original dividend. */
351     tcg_gen_movcond_tl(TCG_COND_EQ, ret, source2, zero, source1, temp1);
352 
353     tcg_temp_free(temp1);
354     tcg_temp_free(temp2);
355 }
356 
357 static void gen_remu(TCGv ret, TCGv source1, TCGv source2)
358 {
359     TCGv temp, zero, one;
360 
361     temp = tcg_temp_new();
362     zero = tcg_constant_tl(0);
363     one = tcg_constant_tl(1);
364 
365     /*
366      * If div by zero, set temp to 1, else source2.
367      * This avoids a possible host trap, but produces an incorrect result.
368      */
369     tcg_gen_movcond_tl(TCG_COND_EQ, temp, source2, zero, one, source2);
370 
371     tcg_gen_remu_tl(temp, source1, temp);
372 
373     /* If div by zero, the required result is the original dividend. */
374     tcg_gen_movcond_tl(TCG_COND_EQ, ret, source2, zero, source1, temp);
375 
376     tcg_temp_free(temp);
377 }
378 
379 static void gen_jal(DisasContext *ctx, int rd, target_ulong imm)
380 {
381     target_ulong next_pc;
382 
383     /* check misaligned: */
384     next_pc = ctx->base.pc_next + imm;
385     if (!has_ext(ctx, RVC)) {
386         if ((next_pc & 0x3) != 0) {
387             gen_exception_inst_addr_mis(ctx);
388             return;
389         }
390     }
391     if (rd != 0) {
392         tcg_gen_movi_tl(cpu_gpr[rd], ctx->pc_succ_insn);
393     }
394 
395     gen_goto_tb(ctx, 0, ctx->base.pc_next + imm); /* must use this for safety */
396     ctx->base.is_jmp = DISAS_NORETURN;
397 }
398 
399 #ifndef CONFIG_USER_ONLY
400 /* The states of mstatus_fs are:
401  * 0 = disabled, 1 = initial, 2 = clean, 3 = dirty
402  * We will have already diagnosed disabled state,
403  * and need to turn initial/clean into dirty.
404  */
405 static void mark_fs_dirty(DisasContext *ctx)
406 {
407     TCGv tmp;
408     target_ulong sd;
409 
410     if (ctx->mstatus_fs == MSTATUS_FS) {
411         return;
412     }
413     /* Remember the state change for the rest of the TB.  */
414     ctx->mstatus_fs = MSTATUS_FS;
415 
416     tmp = tcg_temp_new();
417     sd = is_32bit(ctx) ? MSTATUS32_SD : MSTATUS64_SD;
418 
419     tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
420     tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS | sd);
421     tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
422 
423     if (ctx->virt_enabled) {
424         tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
425         tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS | sd);
426         tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
427     }
428     tcg_temp_free(tmp);
429 }
430 #else
431 static inline void mark_fs_dirty(DisasContext *ctx) { }
432 #endif
433 
434 static void gen_set_rm(DisasContext *ctx, int rm)
435 {
436     if (ctx->frm == rm) {
437         return;
438     }
439     ctx->frm = rm;
440     gen_helper_set_rounding_mode(cpu_env, tcg_constant_i32(rm));
441 }
442 
443 static int ex_plus_1(DisasContext *ctx, int nf)
444 {
445     return nf + 1;
446 }
447 
448 #define EX_SH(amount) \
449     static int ex_shift_##amount(DisasContext *ctx, int imm) \
450     {                                         \
451         return imm << amount;                 \
452     }
453 EX_SH(1)
454 EX_SH(2)
455 EX_SH(3)
456 EX_SH(4)
457 EX_SH(12)
458 
459 #define REQUIRE_EXT(ctx, ext) do { \
460     if (!has_ext(ctx, ext)) {      \
461         return false;              \
462     }                              \
463 } while (0)
464 
465 #define REQUIRE_64BIT(ctx) do { \
466     if (is_32bit(ctx)) {        \
467         return false;           \
468     }                           \
469 } while (0)
470 
471 static int ex_rvc_register(DisasContext *ctx, int reg)
472 {
473     return 8 + reg;
474 }
475 
476 static int ex_rvc_shifti(DisasContext *ctx, int imm)
477 {
478     /* For RV128 a shamt of 0 means a shift by 64. */
479     return imm ? imm : 64;
480 }
481 
482 /* Include the auto-generated decoder for 32 bit insn */
483 #include "decode-insn32.c.inc"
484 
485 static bool gen_arith_imm_fn(DisasContext *ctx, arg_i *a, DisasExtend ext,
486                              void (*func)(TCGv, TCGv, target_long))
487 {
488     TCGv dest = dest_gpr(ctx, a->rd);
489     TCGv src1 = get_gpr(ctx, a->rs1, ext);
490 
491     func(dest, src1, a->imm);
492 
493     gen_set_gpr(ctx, a->rd, dest);
494     return true;
495 }
496 
497 static bool gen_arith_imm_tl(DisasContext *ctx, arg_i *a, DisasExtend ext,
498                              void (*func)(TCGv, TCGv, TCGv))
499 {
500     TCGv dest = dest_gpr(ctx, a->rd);
501     TCGv src1 = get_gpr(ctx, a->rs1, ext);
502     TCGv src2 = tcg_constant_tl(a->imm);
503 
504     func(dest, src1, src2);
505 
506     gen_set_gpr(ctx, a->rd, dest);
507     return true;
508 }
509 
510 static bool gen_arith_div_w(DisasContext *ctx, arg_r *a,
511                             void(*func)(TCGv, TCGv, TCGv))
512 {
513     TCGv source1, source2;
514     source1 = tcg_temp_new();
515     source2 = tcg_temp_new();
516 
517     gen_get_gpr(ctx, source1, a->rs1);
518     gen_get_gpr(ctx, source2, a->rs2);
519     tcg_gen_ext32s_tl(source1, source1);
520     tcg_gen_ext32s_tl(source2, source2);
521 
522     (*func)(source1, source1, source2);
523 
524     tcg_gen_ext32s_tl(source1, source1);
525     gen_set_gpr(ctx, a->rd, source1);
526     tcg_temp_free(source1);
527     tcg_temp_free(source2);
528     return true;
529 }
530 
531 static bool gen_arith_div_uw(DisasContext *ctx, arg_r *a,
532                             void(*func)(TCGv, TCGv, TCGv))
533 {
534     TCGv source1, source2;
535     source1 = tcg_temp_new();
536     source2 = tcg_temp_new();
537 
538     gen_get_gpr(ctx, source1, a->rs1);
539     gen_get_gpr(ctx, source2, a->rs2);
540     tcg_gen_ext32u_tl(source1, source1);
541     tcg_gen_ext32u_tl(source2, source2);
542 
543     (*func)(source1, source1, source2);
544 
545     tcg_gen_ext32s_tl(source1, source1);
546     gen_set_gpr(ctx, a->rd, source1);
547     tcg_temp_free(source1);
548     tcg_temp_free(source2);
549     return true;
550 }
551 
552 static void gen_pack(TCGv ret, TCGv arg1, TCGv arg2)
553 {
554     tcg_gen_deposit_tl(ret, arg1, arg2,
555                        TARGET_LONG_BITS / 2,
556                        TARGET_LONG_BITS / 2);
557 }
558 
559 static void gen_packu(TCGv ret, TCGv arg1, TCGv arg2)
560 {
561     TCGv t = tcg_temp_new();
562     tcg_gen_shri_tl(t, arg1, TARGET_LONG_BITS / 2);
563     tcg_gen_deposit_tl(ret, arg2, t, 0, TARGET_LONG_BITS / 2);
564     tcg_temp_free(t);
565 }
566 
567 static void gen_packh(TCGv ret, TCGv arg1, TCGv arg2)
568 {
569     TCGv t = tcg_temp_new();
570     tcg_gen_ext8u_tl(t, arg2);
571     tcg_gen_deposit_tl(ret, arg1, t, 8, TARGET_LONG_BITS - 8);
572     tcg_temp_free(t);
573 }
574 
575 static void gen_sbop_mask(TCGv ret, TCGv shamt)
576 {
577     tcg_gen_movi_tl(ret, 1);
578     tcg_gen_shl_tl(ret, ret, shamt);
579 }
580 
581 static void gen_bset(TCGv ret, TCGv arg1, TCGv shamt)
582 {
583     TCGv t = tcg_temp_new();
584 
585     gen_sbop_mask(t, shamt);
586     tcg_gen_or_tl(ret, arg1, t);
587 
588     tcg_temp_free(t);
589 }
590 
591 static void gen_bclr(TCGv ret, TCGv arg1, TCGv shamt)
592 {
593     TCGv t = tcg_temp_new();
594 
595     gen_sbop_mask(t, shamt);
596     tcg_gen_andc_tl(ret, arg1, t);
597 
598     tcg_temp_free(t);
599 }
600 
601 static void gen_binv(TCGv ret, TCGv arg1, TCGv shamt)
602 {
603     TCGv t = tcg_temp_new();
604 
605     gen_sbop_mask(t, shamt);
606     tcg_gen_xor_tl(ret, arg1, t);
607 
608     tcg_temp_free(t);
609 }
610 
611 static void gen_bext(TCGv ret, TCGv arg1, TCGv shamt)
612 {
613     tcg_gen_shr_tl(ret, arg1, shamt);
614     tcg_gen_andi_tl(ret, ret, 1);
615 }
616 
617 static void gen_slo(TCGv ret, TCGv arg1, TCGv arg2)
618 {
619     tcg_gen_not_tl(ret, arg1);
620     tcg_gen_shl_tl(ret, ret, arg2);
621     tcg_gen_not_tl(ret, ret);
622 }
623 
624 static void gen_sro(TCGv ret, TCGv arg1, TCGv arg2)
625 {
626     tcg_gen_not_tl(ret, arg1);
627     tcg_gen_shr_tl(ret, ret, arg2);
628     tcg_gen_not_tl(ret, ret);
629 }
630 
631 static bool gen_grevi(DisasContext *ctx, arg_grevi *a)
632 {
633     TCGv source1 = tcg_temp_new();
634     TCGv source2;
635 
636     gen_get_gpr(ctx, source1, a->rs1);
637 
638     if (a->shamt == (TARGET_LONG_BITS - 8)) {
639         /* rev8, byte swaps */
640         tcg_gen_bswap_tl(source1, source1);
641     } else {
642         source2 = tcg_temp_new();
643         tcg_gen_movi_tl(source2, a->shamt);
644         gen_helper_grev(source1, source1, source2);
645         tcg_temp_free(source2);
646     }
647 
648     gen_set_gpr(ctx, a->rd, source1);
649     tcg_temp_free(source1);
650     return true;
651 }
652 
653 #define GEN_SHADD(SHAMT)                                       \
654 static void gen_sh##SHAMT##add(TCGv ret, TCGv arg1, TCGv arg2) \
655 {                                                              \
656     TCGv t = tcg_temp_new();                                   \
657                                                                \
658     tcg_gen_shli_tl(t, arg1, SHAMT);                           \
659     tcg_gen_add_tl(ret, t, arg2);                              \
660                                                                \
661     tcg_temp_free(t);                                          \
662 }
663 
664 GEN_SHADD(1)
665 GEN_SHADD(2)
666 GEN_SHADD(3)
667 
668 static void gen_ctzw(TCGv ret, TCGv arg1)
669 {
670     tcg_gen_ori_tl(ret, arg1, (target_ulong)MAKE_64BIT_MASK(32, 32));
671     tcg_gen_ctzi_tl(ret, ret, 64);
672 }
673 
674 static void gen_clzw(TCGv ret, TCGv arg1)
675 {
676     tcg_gen_ext32u_tl(ret, arg1);
677     tcg_gen_clzi_tl(ret, ret, 64);
678     tcg_gen_subi_tl(ret, ret, 32);
679 }
680 
681 static void gen_cpopw(TCGv ret, TCGv arg1)
682 {
683     tcg_gen_ext32u_tl(arg1, arg1);
684     tcg_gen_ctpop_tl(ret, arg1);
685 }
686 
687 static void gen_packw(TCGv ret, TCGv arg1, TCGv arg2)
688 {
689     TCGv t = tcg_temp_new();
690     tcg_gen_ext16s_tl(t, arg2);
691     tcg_gen_deposit_tl(ret, arg1, t, 16, 48);
692     tcg_temp_free(t);
693 }
694 
695 static void gen_packuw(TCGv ret, TCGv arg1, TCGv arg2)
696 {
697     TCGv t = tcg_temp_new();
698     tcg_gen_shri_tl(t, arg1, 16);
699     tcg_gen_deposit_tl(ret, arg2, t, 0, 16);
700     tcg_gen_ext32s_tl(ret, ret);
701     tcg_temp_free(t);
702 }
703 
704 static void gen_rorw(TCGv ret, TCGv arg1, TCGv arg2)
705 {
706     TCGv_i32 t1 = tcg_temp_new_i32();
707     TCGv_i32 t2 = tcg_temp_new_i32();
708 
709     /* truncate to 32-bits */
710     tcg_gen_trunc_tl_i32(t1, arg1);
711     tcg_gen_trunc_tl_i32(t2, arg2);
712 
713     tcg_gen_rotr_i32(t1, t1, t2);
714 
715     /* sign-extend 64-bits */
716     tcg_gen_ext_i32_tl(ret, t1);
717 
718     tcg_temp_free_i32(t1);
719     tcg_temp_free_i32(t2);
720 }
721 
722 static void gen_rolw(TCGv ret, TCGv arg1, TCGv arg2)
723 {
724     TCGv_i32 t1 = tcg_temp_new_i32();
725     TCGv_i32 t2 = tcg_temp_new_i32();
726 
727     /* truncate to 32-bits */
728     tcg_gen_trunc_tl_i32(t1, arg1);
729     tcg_gen_trunc_tl_i32(t2, arg2);
730 
731     tcg_gen_rotl_i32(t1, t1, t2);
732 
733     /* sign-extend 64-bits */
734     tcg_gen_ext_i32_tl(ret, t1);
735 
736     tcg_temp_free_i32(t1);
737     tcg_temp_free_i32(t2);
738 }
739 
740 static void gen_grevw(TCGv ret, TCGv arg1, TCGv arg2)
741 {
742     tcg_gen_ext32u_tl(arg1, arg1);
743     gen_helper_grev(ret, arg1, arg2);
744 }
745 
746 static void gen_gorcw(TCGv ret, TCGv arg1, TCGv arg2)
747 {
748     tcg_gen_ext32u_tl(arg1, arg1);
749     gen_helper_gorcw(ret, arg1, arg2);
750 }
751 
752 #define GEN_SHADD_UW(SHAMT)                                       \
753 static void gen_sh##SHAMT##add_uw(TCGv ret, TCGv arg1, TCGv arg2) \
754 {                                                                 \
755     TCGv t = tcg_temp_new();                                      \
756                                                                   \
757     tcg_gen_ext32u_tl(t, arg1);                                   \
758                                                                   \
759     tcg_gen_shli_tl(t, t, SHAMT);                                 \
760     tcg_gen_add_tl(ret, t, arg2);                                 \
761                                                                   \
762     tcg_temp_free(t);                                             \
763 }
764 
765 GEN_SHADD_UW(1)
766 GEN_SHADD_UW(2)
767 GEN_SHADD_UW(3)
768 
769 static void gen_add_uw(TCGv ret, TCGv arg1, TCGv arg2)
770 {
771     tcg_gen_ext32u_tl(arg1, arg1);
772     tcg_gen_add_tl(ret, arg1, arg2);
773 }
774 
775 static bool gen_arith(DisasContext *ctx, arg_r *a, DisasExtend ext,
776                       void (*func)(TCGv, TCGv, TCGv))
777 {
778     TCGv dest = dest_gpr(ctx, a->rd);
779     TCGv src1 = get_gpr(ctx, a->rs1, ext);
780     TCGv src2 = get_gpr(ctx, a->rs2, ext);
781 
782     func(dest, src1, src2);
783 
784     gen_set_gpr(ctx, a->rd, dest);
785     return true;
786 }
787 
788 static bool gen_shift(DisasContext *ctx, arg_r *a,
789                         void(*func)(TCGv, TCGv, TCGv))
790 {
791     TCGv source1 = tcg_temp_new();
792     TCGv source2 = tcg_temp_new();
793 
794     gen_get_gpr(ctx, source1, a->rs1);
795     gen_get_gpr(ctx, source2, a->rs2);
796 
797     tcg_gen_andi_tl(source2, source2, TARGET_LONG_BITS - 1);
798     (*func)(source1, source1, source2);
799 
800     gen_set_gpr(ctx, a->rd, source1);
801     tcg_temp_free(source1);
802     tcg_temp_free(source2);
803     return true;
804 }
805 
806 static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)
807 {
808     DisasContext *ctx = container_of(dcbase, DisasContext, base);
809     CPUState *cpu = ctx->cs;
810     CPURISCVState *env = cpu->env_ptr;
811 
812     return cpu_ldl_code(env, pc);
813 }
814 
815 static bool gen_shifti(DisasContext *ctx, arg_shift *a,
816                        void(*func)(TCGv, TCGv, TCGv))
817 {
818     if (a->shamt >= TARGET_LONG_BITS) {
819         return false;
820     }
821 
822     TCGv source1 = tcg_temp_new();
823     TCGv source2 = tcg_temp_new();
824 
825     gen_get_gpr(ctx, source1, a->rs1);
826 
827     tcg_gen_movi_tl(source2, a->shamt);
828     (*func)(source1, source1, source2);
829 
830     gen_set_gpr(ctx, a->rd, source1);
831     tcg_temp_free(source1);
832     tcg_temp_free(source2);
833     return true;
834 }
835 
836 static bool gen_shiftw(DisasContext *ctx, arg_r *a,
837                        void(*func)(TCGv, TCGv, TCGv))
838 {
839     TCGv source1 = tcg_temp_new();
840     TCGv source2 = tcg_temp_new();
841 
842     gen_get_gpr(ctx, source1, a->rs1);
843     gen_get_gpr(ctx, source2, a->rs2);
844 
845     tcg_gen_andi_tl(source2, source2, 31);
846     (*func)(source1, source1, source2);
847     tcg_gen_ext32s_tl(source1, source1);
848 
849     gen_set_gpr(ctx, a->rd, source1);
850     tcg_temp_free(source1);
851     tcg_temp_free(source2);
852     return true;
853 }
854 
855 static bool gen_shiftiw(DisasContext *ctx, arg_shift *a,
856                         void(*func)(TCGv, TCGv, TCGv))
857 {
858     TCGv source1 = tcg_temp_new();
859     TCGv source2 = tcg_temp_new();
860 
861     gen_get_gpr(ctx, source1, a->rs1);
862     tcg_gen_movi_tl(source2, a->shamt);
863 
864     (*func)(source1, source1, source2);
865     tcg_gen_ext32s_tl(source1, source1);
866 
867     gen_set_gpr(ctx, a->rd, source1);
868     tcg_temp_free(source1);
869     tcg_temp_free(source2);
870     return true;
871 }
872 
873 static void gen_ctz(TCGv ret, TCGv arg1)
874 {
875     tcg_gen_ctzi_tl(ret, arg1, TARGET_LONG_BITS);
876 }
877 
878 static void gen_clz(TCGv ret, TCGv arg1)
879 {
880     tcg_gen_clzi_tl(ret, arg1, TARGET_LONG_BITS);
881 }
882 
883 static bool gen_unary(DisasContext *ctx, arg_r2 *a,
884                       void(*func)(TCGv, TCGv))
885 {
886     TCGv source = tcg_temp_new();
887 
888     gen_get_gpr(ctx, source, a->rs1);
889 
890     (*func)(source, source);
891 
892     gen_set_gpr(ctx, a->rd, source);
893     tcg_temp_free(source);
894     return true;
895 }
896 
897 /* Include insn module translation function */
898 #include "insn_trans/trans_rvi.c.inc"
899 #include "insn_trans/trans_rvm.c.inc"
900 #include "insn_trans/trans_rva.c.inc"
901 #include "insn_trans/trans_rvf.c.inc"
902 #include "insn_trans/trans_rvd.c.inc"
903 #include "insn_trans/trans_rvh.c.inc"
904 #include "insn_trans/trans_rvv.c.inc"
905 #include "insn_trans/trans_rvb.c.inc"
906 #include "insn_trans/trans_privileged.c.inc"
907 
908 /* Include the auto-generated decoder for 16 bit insn */
909 #include "decode-insn16.c.inc"
910 
911 static void decode_opc(CPURISCVState *env, DisasContext *ctx, uint16_t opcode)
912 {
913     /* check for compressed insn */
914     if (extract16(opcode, 0, 2) != 3) {
915         if (!has_ext(ctx, RVC)) {
916             gen_exception_illegal(ctx);
917         } else {
918             ctx->pc_succ_insn = ctx->base.pc_next + 2;
919             if (!decode_insn16(ctx, opcode)) {
920                 gen_exception_illegal(ctx);
921             }
922         }
923     } else {
924         uint32_t opcode32 = opcode;
925         opcode32 = deposit32(opcode32, 16, 16,
926                              translator_lduw(env, ctx->base.pc_next + 2));
927         ctx->pc_succ_insn = ctx->base.pc_next + 4;
928         if (!decode_insn32(ctx, opcode32)) {
929             gen_exception_illegal(ctx);
930         }
931     }
932 }
933 
934 static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
935 {
936     DisasContext *ctx = container_of(dcbase, DisasContext, base);
937     CPURISCVState *env = cs->env_ptr;
938     RISCVCPU *cpu = RISCV_CPU(cs);
939     uint32_t tb_flags = ctx->base.tb->flags;
940 
941     ctx->pc_succ_insn = ctx->base.pc_first;
942     ctx->mem_idx = tb_flags & TB_FLAGS_MMU_MASK;
943     ctx->mstatus_fs = tb_flags & TB_FLAGS_MSTATUS_FS;
944     ctx->priv_ver = env->priv_ver;
945 #if !defined(CONFIG_USER_ONLY)
946     if (riscv_has_ext(env, RVH)) {
947         ctx->virt_enabled = riscv_cpu_virt_enabled(env);
948     } else {
949         ctx->virt_enabled = false;
950     }
951 #else
952     ctx->virt_enabled = false;
953 #endif
954     ctx->misa = env->misa;
955     ctx->frm = -1;  /* unknown rounding mode */
956     ctx->ext_ifencei = cpu->cfg.ext_ifencei;
957     ctx->vlen = cpu->cfg.vlen;
958     ctx->hlsx = FIELD_EX32(tb_flags, TB_FLAGS, HLSX);
959     ctx->vill = FIELD_EX32(tb_flags, TB_FLAGS, VILL);
960     ctx->sew = FIELD_EX32(tb_flags, TB_FLAGS, SEW);
961     ctx->lmul = FIELD_EX32(tb_flags, TB_FLAGS, LMUL);
962     ctx->mlen = 1 << (ctx->sew  + 3 - ctx->lmul);
963     ctx->vl_eq_vlmax = FIELD_EX32(tb_flags, TB_FLAGS, VL_EQ_VLMAX);
964     ctx->cs = cs;
965     ctx->w = false;
966     ctx->ntemp = 0;
967     memset(ctx->temp, 0, sizeof(ctx->temp));
968 
969     ctx->zero = tcg_constant_tl(0);
970 }
971 
972 static void riscv_tr_tb_start(DisasContextBase *db, CPUState *cpu)
973 {
974 }
975 
976 static void riscv_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
977 {
978     DisasContext *ctx = container_of(dcbase, DisasContext, base);
979 
980     tcg_gen_insn_start(ctx->base.pc_next);
981 }
982 
983 static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
984 {
985     DisasContext *ctx = container_of(dcbase, DisasContext, base);
986     CPURISCVState *env = cpu->env_ptr;
987     uint16_t opcode16 = translator_lduw(env, ctx->base.pc_next);
988 
989     decode_opc(env, ctx, opcode16);
990     ctx->base.pc_next = ctx->pc_succ_insn;
991     ctx->w = false;
992 
993     for (int i = ctx->ntemp - 1; i >= 0; --i) {
994         tcg_temp_free(ctx->temp[i]);
995         ctx->temp[i] = NULL;
996     }
997     ctx->ntemp = 0;
998 
999     if (ctx->base.is_jmp == DISAS_NEXT) {
1000         target_ulong page_start;
1001 
1002         page_start = ctx->base.pc_first & TARGET_PAGE_MASK;
1003         if (ctx->base.pc_next - page_start >= TARGET_PAGE_SIZE) {
1004             ctx->base.is_jmp = DISAS_TOO_MANY;
1005         }
1006     }
1007 }
1008 
1009 static void riscv_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
1010 {
1011     DisasContext *ctx = container_of(dcbase, DisasContext, base);
1012 
1013     switch (ctx->base.is_jmp) {
1014     case DISAS_TOO_MANY:
1015         gen_goto_tb(ctx, 0, ctx->base.pc_next);
1016         break;
1017     case DISAS_NORETURN:
1018         break;
1019     default:
1020         g_assert_not_reached();
1021     }
1022 }
1023 
1024 static void riscv_tr_disas_log(const DisasContextBase *dcbase, CPUState *cpu)
1025 {
1026 #ifndef CONFIG_USER_ONLY
1027     RISCVCPU *rvcpu = RISCV_CPU(cpu);
1028     CPURISCVState *env = &rvcpu->env;
1029 #endif
1030 
1031     qemu_log("IN: %s\n", lookup_symbol(dcbase->pc_first));
1032 #ifndef CONFIG_USER_ONLY
1033     qemu_log("Priv: "TARGET_FMT_ld"; Virt: "TARGET_FMT_ld"\n", env->priv, env->virt);
1034 #endif
1035     log_target_disas(cpu, dcbase->pc_first, dcbase->tb->size);
1036 }
1037 
1038 static const TranslatorOps riscv_tr_ops = {
1039     .init_disas_context = riscv_tr_init_disas_context,
1040     .tb_start           = riscv_tr_tb_start,
1041     .insn_start         = riscv_tr_insn_start,
1042     .translate_insn     = riscv_tr_translate_insn,
1043     .tb_stop            = riscv_tr_tb_stop,
1044     .disas_log          = riscv_tr_disas_log,
1045 };
1046 
1047 void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
1048 {
1049     DisasContext ctx;
1050 
1051     translator_loop(&riscv_tr_ops, &ctx.base, cs, tb, max_insns);
1052 }
1053 
1054 void riscv_translate_init(void)
1055 {
1056     int i;
1057 
1058     /* cpu_gpr[0] is a placeholder for the zero register. Do not use it. */
1059     /* Use the gen_set_gpr and gen_get_gpr helper functions when accessing */
1060     /* registers, unless you specifically block reads/writes to reg 0 */
1061     cpu_gpr[0] = NULL;
1062 
1063     for (i = 1; i < 32; i++) {
1064         cpu_gpr[i] = tcg_global_mem_new(cpu_env,
1065             offsetof(CPURISCVState, gpr[i]), riscv_int_regnames[i]);
1066     }
1067 
1068     for (i = 0; i < 32; i++) {
1069         cpu_fpr[i] = tcg_global_mem_new_i64(cpu_env,
1070             offsetof(CPURISCVState, fpr[i]), riscv_fpr_regnames[i]);
1071     }
1072 
1073     cpu_pc = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, pc), "pc");
1074     cpu_vl = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, vl), "vl");
1075     load_res = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, load_res),
1076                              "load_res");
1077     load_val = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, load_val),
1078                              "load_val");
1079 }
1080