]> Git Repo - qemu.git/blame - target/riscv/translate.c
target/riscv: Move gen_arith_imm() decoding into trans_* functions
[qemu.git] / target / riscv / translate.c
CommitLineData
55c2a12c
MC
1/*
2 * RISC-V emulation for qemu: main translation routines.
3 *
4 * Copyright (c) 2016-2017 Sagar Karandikar, [email protected]
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-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
b2e32021 29#include "exec/translator.h"
55c2a12c
MC
30#include "exec/log.h"
31
32#include "instmap.h"
33
34/* global register indices */
35static TCGv cpu_gpr[32], cpu_pc;
36static TCGv_i64 cpu_fpr[32]; /* assume F and D extensions */
37static TCGv load_res;
38static TCGv load_val;
39
40#include "exec/gen-icount.h"
41
42typedef struct DisasContext {
0114db1c
EC
43 DisasContextBase base;
44 /* pc_succ_insn points to the instruction following base.pc_next */
45 target_ulong pc_succ_insn;
d75377bf 46 target_ulong priv_ver;
55c2a12c 47 uint32_t opcode;
83a71719 48 uint32_t mstatus_fs;
db9f3fd6 49 uint32_t misa;
55c2a12c 50 uint32_t mem_idx;
55c2a12c
MC
51 /* Remember the rounding mode encoded in the previous fp instruction,
52 which we have already installed into env->fp_status. Or -1 for
53 no previous fp instruction. Note that we exit the TB when writing
54 to any system register, which includes CSR_FRM, so we do not have
55 to reset this known value. */
56 int frm;
57} DisasContext;
58
bce8a342 59#ifdef TARGET_RISCV64
55c2a12c
MC
60/* convert riscv funct3 to qemu memop for load/store */
61static const int tcg_memop_lookup[8] = {
62 [0 ... 7] = -1,
63 [0] = MO_SB,
64 [1] = MO_TESW,
65 [2] = MO_TESL,
66 [4] = MO_UB,
67 [5] = MO_TEUW,
68#ifdef TARGET_RISCV64
69 [3] = MO_TEQ,
70 [6] = MO_TEUL,
71#endif
72};
bce8a342 73#endif
55c2a12c
MC
74
75#ifdef TARGET_RISCV64
76#define CASE_OP_32_64(X) case X: case glue(X, W)
77#else
78#define CASE_OP_32_64(X) case X
79#endif
80
db9f3fd6
MC
81static inline bool has_ext(DisasContext *ctx, uint32_t ext)
82{
83 return ctx->misa & ext;
84}
85
55c2a12c
MC
86static void generate_exception(DisasContext *ctx, int excp)
87{
0114db1c 88 tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next);
55c2a12c
MC
89 TCGv_i32 helper_tmp = tcg_const_i32(excp);
90 gen_helper_raise_exception(cpu_env, helper_tmp);
91 tcg_temp_free_i32(helper_tmp);
0114db1c 92 ctx->base.is_jmp = DISAS_NORETURN;
55c2a12c
MC
93}
94
95static void generate_exception_mbadaddr(DisasContext *ctx, int excp)
96{
0114db1c 97 tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next);
55c2a12c
MC
98 tcg_gen_st_tl(cpu_pc, cpu_env, offsetof(CPURISCVState, badaddr));
99 TCGv_i32 helper_tmp = tcg_const_i32(excp);
100 gen_helper_raise_exception(cpu_env, helper_tmp);
101 tcg_temp_free_i32(helper_tmp);
0114db1c 102 ctx->base.is_jmp = DISAS_NORETURN;
55c2a12c
MC
103}
104
105static void gen_exception_debug(void)
106{
107 TCGv_i32 helper_tmp = tcg_const_i32(EXCP_DEBUG);
108 gen_helper_raise_exception(cpu_env, helper_tmp);
109 tcg_temp_free_i32(helper_tmp);
110}
111
112static void gen_exception_illegal(DisasContext *ctx)
113{
114 generate_exception(ctx, RISCV_EXCP_ILLEGAL_INST);
115}
116
117static void gen_exception_inst_addr_mis(DisasContext *ctx)
118{
119 generate_exception_mbadaddr(ctx, RISCV_EXCP_INST_ADDR_MIS);
120}
121
122static inline bool use_goto_tb(DisasContext *ctx, target_ulong dest)
123{
0114db1c 124 if (unlikely(ctx->base.singlestep_enabled)) {
55c2a12c
MC
125 return false;
126 }
127
128#ifndef CONFIG_USER_ONLY
0114db1c 129 return (ctx->base.tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
55c2a12c
MC
130#else
131 return true;
132#endif
133}
134
135static void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
136{
137 if (use_goto_tb(ctx, dest)) {
138 /* chaining is only allowed when the jump is to the same page */
139 tcg_gen_goto_tb(n);
140 tcg_gen_movi_tl(cpu_pc, dest);
07ea28b4 141 tcg_gen_exit_tb(ctx->base.tb, n);
55c2a12c
MC
142 } else {
143 tcg_gen_movi_tl(cpu_pc, dest);
0114db1c 144 if (ctx->base.singlestep_enabled) {
55c2a12c
MC
145 gen_exception_debug();
146 } else {
6dbebd55 147 tcg_gen_lookup_and_goto_ptr();
55c2a12c
MC
148 }
149 }
150}
151
152/* Wrapper for getting reg values - need to check of reg is zero since
153 * cpu_gpr[0] is not actually allocated
154 */
155static inline void gen_get_gpr(TCGv t, int reg_num)
156{
157 if (reg_num == 0) {
158 tcg_gen_movi_tl(t, 0);
159 } else {
160 tcg_gen_mov_tl(t, cpu_gpr[reg_num]);
161 }
162}
163
164/* Wrapper for setting reg values - need to check of reg is zero since
165 * cpu_gpr[0] is not actually allocated. this is more for safety purposes,
166 * since we usually avoid calling the OP_TYPE_gen function if we see a write to
167 * $zero
168 */
169static inline void gen_set_gpr(int reg_num_dst, TCGv t)
170{
171 if (reg_num_dst != 0) {
172 tcg_gen_mov_tl(cpu_gpr[reg_num_dst], t);
173 }
174}
175
176static void gen_mulhsu(TCGv ret, TCGv arg1, TCGv arg2)
177{
178 TCGv rl = tcg_temp_new();
179 TCGv rh = tcg_temp_new();
180
181 tcg_gen_mulu2_tl(rl, rh, arg1, arg2);
182 /* fix up for one negative */
183 tcg_gen_sari_tl(rl, arg1, TARGET_LONG_BITS - 1);
184 tcg_gen_and_tl(rl, rl, arg2);
185 tcg_gen_sub_tl(ret, rh, rl);
186
187 tcg_temp_free(rl);
188 tcg_temp_free(rh);
189}
190
55c2a12c
MC
191static void gen_arith(DisasContext *ctx, uint32_t opc, int rd, int rs1,
192 int rs2)
193{
194 TCGv source1, source2, cond1, cond2, zeroreg, resultopt1;
195 source1 = tcg_temp_new();
196 source2 = tcg_temp_new();
197 gen_get_gpr(source1, rs1);
198 gen_get_gpr(source2, rs2);
199
200 switch (opc) {
201 CASE_OP_32_64(OPC_RISC_ADD):
202 tcg_gen_add_tl(source1, source1, source2);
203 break;
204 CASE_OP_32_64(OPC_RISC_SUB):
205 tcg_gen_sub_tl(source1, source1, source2);
206 break;
207#if defined(TARGET_RISCV64)
208 case OPC_RISC_SLLW:
209 tcg_gen_andi_tl(source2, source2, 0x1F);
210 tcg_gen_shl_tl(source1, source1, source2);
211 break;
212#endif
213 case OPC_RISC_SLL:
214 tcg_gen_andi_tl(source2, source2, TARGET_LONG_BITS - 1);
215 tcg_gen_shl_tl(source1, source1, source2);
216 break;
217 case OPC_RISC_SLT:
218 tcg_gen_setcond_tl(TCG_COND_LT, source1, source1, source2);
219 break;
220 case OPC_RISC_SLTU:
221 tcg_gen_setcond_tl(TCG_COND_LTU, source1, source1, source2);
222 break;
223 case OPC_RISC_XOR:
224 tcg_gen_xor_tl(source1, source1, source2);
225 break;
226#if defined(TARGET_RISCV64)
227 case OPC_RISC_SRLW:
228 /* clear upper 32 */
229 tcg_gen_ext32u_tl(source1, source1);
230 tcg_gen_andi_tl(source2, source2, 0x1F);
231 tcg_gen_shr_tl(source1, source1, source2);
232 break;
233#endif
234 case OPC_RISC_SRL:
235 tcg_gen_andi_tl(source2, source2, TARGET_LONG_BITS - 1);
236 tcg_gen_shr_tl(source1, source1, source2);
237 break;
238#if defined(TARGET_RISCV64)
239 case OPC_RISC_SRAW:
240 /* first, trick to get it to act like working on 32 bits (get rid of
241 upper 32, sign extend to fill space) */
242 tcg_gen_ext32s_tl(source1, source1);
243 tcg_gen_andi_tl(source2, source2, 0x1F);
244 tcg_gen_sar_tl(source1, source1, source2);
245 break;
55c2a12c
MC
246#endif
247 case OPC_RISC_SRA:
248 tcg_gen_andi_tl(source2, source2, TARGET_LONG_BITS - 1);
249 tcg_gen_sar_tl(source1, source1, source2);
250 break;
251 case OPC_RISC_OR:
252 tcg_gen_or_tl(source1, source1, source2);
253 break;
254 case OPC_RISC_AND:
255 tcg_gen_and_tl(source1, source1, source2);
256 break;
257 CASE_OP_32_64(OPC_RISC_MUL):
d77c3401
MC
258 if (!has_ext(ctx, RVM)) {
259 goto do_illegal;
260 }
55c2a12c
MC
261 tcg_gen_mul_tl(source1, source1, source2);
262 break;
263 case OPC_RISC_MULH:
d77c3401
MC
264 if (!has_ext(ctx, RVM)) {
265 goto do_illegal;
266 }
55c2a12c
MC
267 tcg_gen_muls2_tl(source2, source1, source1, source2);
268 break;
269 case OPC_RISC_MULHSU:
d77c3401
MC
270 if (!has_ext(ctx, RVM)) {
271 goto do_illegal;
272 }
55c2a12c
MC
273 gen_mulhsu(source1, source1, source2);
274 break;
275 case OPC_RISC_MULHU:
d77c3401
MC
276 if (!has_ext(ctx, RVM)) {
277 goto do_illegal;
278 }
55c2a12c
MC
279 tcg_gen_mulu2_tl(source2, source1, source1, source2);
280 break;
281#if defined(TARGET_RISCV64)
282 case OPC_RISC_DIVW:
d77c3401
MC
283 if (!has_ext(ctx, RVM)) {
284 goto do_illegal;
285 }
55c2a12c
MC
286 tcg_gen_ext32s_tl(source1, source1);
287 tcg_gen_ext32s_tl(source2, source2);
288 /* fall through to DIV */
289#endif
290 case OPC_RISC_DIV:
d77c3401
MC
291 if (!has_ext(ctx, RVM)) {
292 goto do_illegal;
293 }
55c2a12c
MC
294 /* Handle by altering args to tcg_gen_div to produce req'd results:
295 * For overflow: want source1 in source1 and 1 in source2
296 * For div by zero: want -1 in source1 and 1 in source2 -> -1 result */
297 cond1 = tcg_temp_new();
298 cond2 = tcg_temp_new();
299 zeroreg = tcg_const_tl(0);
300 resultopt1 = tcg_temp_new();
301
302 tcg_gen_movi_tl(resultopt1, (target_ulong)-1);
303 tcg_gen_setcondi_tl(TCG_COND_EQ, cond2, source2, (target_ulong)(~0L));
304 tcg_gen_setcondi_tl(TCG_COND_EQ, cond1, source1,
305 ((target_ulong)1) << (TARGET_LONG_BITS - 1));
306 tcg_gen_and_tl(cond1, cond1, cond2); /* cond1 = overflow */
307 tcg_gen_setcondi_tl(TCG_COND_EQ, cond2, source2, 0); /* cond2 = div 0 */
308 /* if div by zero, set source1 to -1, otherwise don't change */
309 tcg_gen_movcond_tl(TCG_COND_EQ, source1, cond2, zeroreg, source1,
310 resultopt1);
311 /* if overflow or div by zero, set source2 to 1, else don't change */
312 tcg_gen_or_tl(cond1, cond1, cond2);
313 tcg_gen_movi_tl(resultopt1, (target_ulong)1);
314 tcg_gen_movcond_tl(TCG_COND_EQ, source2, cond1, zeroreg, source2,
315 resultopt1);
316 tcg_gen_div_tl(source1, source1, source2);
317
318 tcg_temp_free(cond1);
319 tcg_temp_free(cond2);
320 tcg_temp_free(zeroreg);
321 tcg_temp_free(resultopt1);
322 break;
323#if defined(TARGET_RISCV64)
324 case OPC_RISC_DIVUW:
d77c3401
MC
325 if (!has_ext(ctx, RVM)) {
326 goto do_illegal;
327 }
55c2a12c
MC
328 tcg_gen_ext32u_tl(source1, source1);
329 tcg_gen_ext32u_tl(source2, source2);
330 /* fall through to DIVU */
331#endif
332 case OPC_RISC_DIVU:
d77c3401
MC
333 if (!has_ext(ctx, RVM)) {
334 goto do_illegal;
335 }
55c2a12c
MC
336 cond1 = tcg_temp_new();
337 zeroreg = tcg_const_tl(0);
338 resultopt1 = tcg_temp_new();
339
340 tcg_gen_setcondi_tl(TCG_COND_EQ, cond1, source2, 0);
341 tcg_gen_movi_tl(resultopt1, (target_ulong)-1);
342 tcg_gen_movcond_tl(TCG_COND_EQ, source1, cond1, zeroreg, source1,
343 resultopt1);
344 tcg_gen_movi_tl(resultopt1, (target_ulong)1);
345 tcg_gen_movcond_tl(TCG_COND_EQ, source2, cond1, zeroreg, source2,
346 resultopt1);
347 tcg_gen_divu_tl(source1, source1, source2);
348
349 tcg_temp_free(cond1);
350 tcg_temp_free(zeroreg);
351 tcg_temp_free(resultopt1);
352 break;
353#if defined(TARGET_RISCV64)
354 case OPC_RISC_REMW:
d77c3401
MC
355 if (!has_ext(ctx, RVM)) {
356 goto do_illegal;
357 }
55c2a12c
MC
358 tcg_gen_ext32s_tl(source1, source1);
359 tcg_gen_ext32s_tl(source2, source2);
360 /* fall through to REM */
361#endif
362 case OPC_RISC_REM:
d77c3401
MC
363 if (!has_ext(ctx, RVM)) {
364 goto do_illegal;
365 }
55c2a12c
MC
366 cond1 = tcg_temp_new();
367 cond2 = tcg_temp_new();
368 zeroreg = tcg_const_tl(0);
369 resultopt1 = tcg_temp_new();
370
371 tcg_gen_movi_tl(resultopt1, 1L);
372 tcg_gen_setcondi_tl(TCG_COND_EQ, cond2, source2, (target_ulong)-1);
373 tcg_gen_setcondi_tl(TCG_COND_EQ, cond1, source1,
374 (target_ulong)1 << (TARGET_LONG_BITS - 1));
375 tcg_gen_and_tl(cond2, cond1, cond2); /* cond1 = overflow */
376 tcg_gen_setcondi_tl(TCG_COND_EQ, cond1, source2, 0); /* cond2 = div 0 */
377 /* if overflow or div by zero, set source2 to 1, else don't change */
378 tcg_gen_or_tl(cond2, cond1, cond2);
379 tcg_gen_movcond_tl(TCG_COND_EQ, source2, cond2, zeroreg, source2,
380 resultopt1);
381 tcg_gen_rem_tl(resultopt1, source1, source2);
382 /* if div by zero, just return the original dividend */
383 tcg_gen_movcond_tl(TCG_COND_EQ, source1, cond1, zeroreg, resultopt1,
384 source1);
385
386 tcg_temp_free(cond1);
387 tcg_temp_free(cond2);
388 tcg_temp_free(zeroreg);
389 tcg_temp_free(resultopt1);
390 break;
391#if defined(TARGET_RISCV64)
392 case OPC_RISC_REMUW:
d77c3401
MC
393 if (!has_ext(ctx, RVM)) {
394 goto do_illegal;
395 }
55c2a12c
MC
396 tcg_gen_ext32u_tl(source1, source1);
397 tcg_gen_ext32u_tl(source2, source2);
398 /* fall through to REMU */
399#endif
400 case OPC_RISC_REMU:
d77c3401
MC
401 if (!has_ext(ctx, RVM)) {
402 goto do_illegal;
403 }
55c2a12c
MC
404 cond1 = tcg_temp_new();
405 zeroreg = tcg_const_tl(0);
406 resultopt1 = tcg_temp_new();
407
408 tcg_gen_movi_tl(resultopt1, (target_ulong)1);
409 tcg_gen_setcondi_tl(TCG_COND_EQ, cond1, source2, 0);
410 tcg_gen_movcond_tl(TCG_COND_EQ, source2, cond1, zeroreg, source2,
411 resultopt1);
412 tcg_gen_remu_tl(resultopt1, source1, source2);
413 /* if div by zero, just return the original dividend */
414 tcg_gen_movcond_tl(TCG_COND_EQ, source1, cond1, zeroreg, resultopt1,
415 source1);
416
417 tcg_temp_free(cond1);
418 tcg_temp_free(zeroreg);
419 tcg_temp_free(resultopt1);
420 break;
d77c3401 421 do_illegal:
55c2a12c
MC
422 default:
423 gen_exception_illegal(ctx);
424 return;
425 }
426
427 if (opc & 0x8) { /* sign extend for W instructions */
428 tcg_gen_ext32s_tl(source1, source1);
429 }
430
431 gen_set_gpr(rd, source1);
432 tcg_temp_free(source1);
433 tcg_temp_free(source2);
434}
435
db9f3fd6 436static void gen_jal(DisasContext *ctx, int rd, target_ulong imm)
55c2a12c
MC
437{
438 target_ulong next_pc;
439
440 /* check misaligned: */
0114db1c 441 next_pc = ctx->base.pc_next + imm;
db9f3fd6 442 if (!has_ext(ctx, RVC)) {
55c2a12c
MC
443 if ((next_pc & 0x3) != 0) {
444 gen_exception_inst_addr_mis(ctx);
445 return;
446 }
447 }
448 if (rd != 0) {
0114db1c 449 tcg_gen_movi_tl(cpu_gpr[rd], ctx->pc_succ_insn);
55c2a12c
MC
450 }
451
0114db1c
EC
452 gen_goto_tb(ctx, 0, ctx->base.pc_next + imm); /* must use this for safety */
453 ctx->base.is_jmp = DISAS_NORETURN;
55c2a12c
MC
454}
455
98898b20
BK
456#ifdef TARGET_RISCV64
457static void gen_load_c(DisasContext *ctx, uint32_t opc, int rd, int rs1,
55c2a12c
MC
458 target_long imm)
459{
460 TCGv t0 = tcg_temp_new();
461 TCGv t1 = tcg_temp_new();
462 gen_get_gpr(t0, rs1);
463 tcg_gen_addi_tl(t0, t0, imm);
464 int memop = tcg_memop_lookup[(opc >> 12) & 0x7];
465
466 if (memop < 0) {
467 gen_exception_illegal(ctx);
468 return;
469 }
470
471 tcg_gen_qemu_ld_tl(t1, t0, ctx->mem_idx, memop);
472 gen_set_gpr(rd, t1);
473 tcg_temp_free(t0);
474 tcg_temp_free(t1);
475}
476
bce8a342 477static void gen_store_c(DisasContext *ctx, uint32_t opc, int rs1, int rs2,
55c2a12c
MC
478 target_long imm)
479{
480 TCGv t0 = tcg_temp_new();
481 TCGv dat = tcg_temp_new();
482 gen_get_gpr(t0, rs1);
483 tcg_gen_addi_tl(t0, t0, imm);
484 gen_get_gpr(dat, rs2);
485 int memop = tcg_memop_lookup[(opc >> 12) & 0x7];
486
487 if (memop < 0) {
488 gen_exception_illegal(ctx);
489 return;
490 }
491
492 tcg_gen_qemu_st_tl(dat, t0, ctx->mem_idx, memop);
493 tcg_temp_free(t0);
494 tcg_temp_free(dat);
495}
bce8a342 496#endif
55c2a12c 497
533b8f88
RH
498#ifndef CONFIG_USER_ONLY
499/* The states of mstatus_fs are:
500 * 0 = disabled, 1 = initial, 2 = clean, 3 = dirty
501 * We will have already diagnosed disabled state,
502 * and need to turn initial/clean into dirty.
503 */
504static void mark_fs_dirty(DisasContext *ctx)
505{
506 TCGv tmp;
507 if (ctx->mstatus_fs == MSTATUS_FS) {
508 return;
509 }
510 /* Remember the state change for the rest of the TB. */
511 ctx->mstatus_fs = MSTATUS_FS;
512
513 tmp = tcg_temp_new();
514 tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
515 tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS);
516 tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
517 tcg_temp_free(tmp);
518}
519#else
520static inline void mark_fs_dirty(DisasContext *ctx) { }
521#endif
522
97b0be81 523#if !defined(TARGET_RISCV64)
55c2a12c
MC
524static void gen_fp_load(DisasContext *ctx, uint32_t opc, int rd,
525 int rs1, target_long imm)
526{
527 TCGv t0;
528
83a71719 529 if (ctx->mstatus_fs == 0) {
55c2a12c
MC
530 gen_exception_illegal(ctx);
531 return;
532 }
533
534 t0 = tcg_temp_new();
535 gen_get_gpr(t0, rs1);
536 tcg_gen_addi_tl(t0, t0, imm);
537
538 switch (opc) {
539 case OPC_RISC_FLW:
d77c3401
MC
540 if (!has_ext(ctx, RVF)) {
541 goto do_illegal;
542 }
55c2a12c
MC
543 tcg_gen_qemu_ld_i64(cpu_fpr[rd], t0, ctx->mem_idx, MO_TEUL);
544 /* RISC-V requires NaN-boxing of narrower width floating point values */
545 tcg_gen_ori_i64(cpu_fpr[rd], cpu_fpr[rd], 0xffffffff00000000ULL);
546 break;
547 case OPC_RISC_FLD:
d77c3401
MC
548 if (!has_ext(ctx, RVD)) {
549 goto do_illegal;
550 }
55c2a12c
MC
551 tcg_gen_qemu_ld_i64(cpu_fpr[rd], t0, ctx->mem_idx, MO_TEQ);
552 break;
d77c3401 553 do_illegal:
55c2a12c
MC
554 default:
555 gen_exception_illegal(ctx);
556 break;
557 }
558 tcg_temp_free(t0);
533b8f88
RH
559
560 mark_fs_dirty(ctx);
55c2a12c
MC
561}
562
563static void gen_fp_store(DisasContext *ctx, uint32_t opc, int rs1,
564 int rs2, target_long imm)
565{
566 TCGv t0;
567
83a71719 568 if (ctx->mstatus_fs == 0) {
55c2a12c
MC
569 gen_exception_illegal(ctx);
570 return;
571 }
572
573 t0 = tcg_temp_new();
574 gen_get_gpr(t0, rs1);
575 tcg_gen_addi_tl(t0, t0, imm);
576
577 switch (opc) {
578 case OPC_RISC_FSW:
d77c3401
MC
579 if (!has_ext(ctx, RVF)) {
580 goto do_illegal;
581 }
55c2a12c
MC
582 tcg_gen_qemu_st_i64(cpu_fpr[rs2], t0, ctx->mem_idx, MO_TEUL);
583 break;
584 case OPC_RISC_FSD:
d77c3401
MC
585 if (!has_ext(ctx, RVD)) {
586 goto do_illegal;
587 }
55c2a12c
MC
588 tcg_gen_qemu_st_i64(cpu_fpr[rs2], t0, ctx->mem_idx, MO_TEQ);
589 break;
d77c3401 590 do_illegal:
55c2a12c
MC
591 default:
592 gen_exception_illegal(ctx);
593 break;
594 }
595
596 tcg_temp_free(t0);
597}
97b0be81 598#endif
55c2a12c 599
55c2a12c
MC
600static void gen_set_rm(DisasContext *ctx, int rm)
601{
602 TCGv_i32 t0;
603
604 if (ctx->frm == rm) {
605 return;
606 }
607 ctx->frm = rm;
608 t0 = tcg_const_i32(rm);
609 gen_helper_set_rounding_mode(cpu_env, t0);
610 tcg_temp_free_i32(t0);
611}
612
db9f3fd6
MC
613static void gen_system(DisasContext *ctx, uint32_t opc, int rd, int rs1,
614 int csr)
55c2a12c 615{
0114db1c 616 tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next);
55c2a12c 617
55c2a12c
MC
618 switch (opc) {
619 case OPC_RISC_ECALL:
620 switch (csr) {
621 case 0x0: /* ECALL */
622 /* always generates U-level ECALL, fixed in do_interrupt handler */
623 generate_exception(ctx, RISCV_EXCP_U_ECALL);
07ea28b4 624 tcg_gen_exit_tb(NULL, 0); /* no chaining */
0114db1c 625 ctx->base.is_jmp = DISAS_NORETURN;
55c2a12c
MC
626 break;
627 case 0x1: /* EBREAK */
628 generate_exception(ctx, RISCV_EXCP_BREAKPOINT);
07ea28b4 629 tcg_gen_exit_tb(NULL, 0); /* no chaining */
0114db1c 630 ctx->base.is_jmp = DISAS_NORETURN;
55c2a12c 631 break;
55c2a12c
MC
632 default:
633 gen_exception_illegal(ctx);
634 break;
635 }
636 break;
55c2a12c 637 }
55c2a12c
MC
638}
639
640static void decode_RV32_64C0(DisasContext *ctx)
641{
642 uint8_t funct3 = extract32(ctx->opcode, 13, 3);
643 uint8_t rd_rs2 = GET_C_RS2S(ctx->opcode);
644 uint8_t rs1s = GET_C_RS1S(ctx->opcode);
645
646 switch (funct3) {
55c2a12c
MC
647 case 3:
648#if defined(TARGET_RISCV64)
649 /* C.LD(RV64/128) -> ld rd', offset[7:3](rs1')*/
98898b20 650 gen_load_c(ctx, OPC_RISC_LD, rd_rs2, rs1s,
55c2a12c
MC
651 GET_C_LD_IMM(ctx->opcode));
652#else
653 /* C.FLW (RV32) -> flw rd', offset[6:2](rs1')*/
654 gen_fp_load(ctx, OPC_RISC_FLW, rd_rs2, rs1s,
655 GET_C_LW_IMM(ctx->opcode));
656#endif
657 break;
55c2a12c
MC
658 case 7:
659#if defined(TARGET_RISCV64)
660 /* C.SD (RV64/128) -> sd rs2', offset[7:3](rs1')*/
bce8a342 661 gen_store_c(ctx, OPC_RISC_SD, rs1s, rd_rs2,
55c2a12c
MC
662 GET_C_LD_IMM(ctx->opcode));
663#else
664 /* C.FSW (RV32) -> fsw rs2', offset[6:2](rs1')*/
665 gen_fp_store(ctx, OPC_RISC_FSW, rs1s, rd_rs2,
666 GET_C_LW_IMM(ctx->opcode));
667#endif
668 break;
669 }
670}
671
db9f3fd6 672static void decode_RV32_64C(DisasContext *ctx)
55c2a12c
MC
673{
674 uint8_t op = extract32(ctx->opcode, 0, 2);
675
676 switch (op) {
677 case 0:
678 decode_RV32_64C0(ctx);
679 break;
55c2a12c
MC
680 }
681}
682
2a53cff4
BK
683#define EX_SH(amount) \
684 static int ex_shift_##amount(int imm) \
685 { \
686 return imm << amount; \
687 }
3cca75a6 688EX_SH(1)
e98d9140
BK
689EX_SH(2)
690EX_SH(3)
07b001c6 691EX_SH(4)
2a53cff4
BK
692EX_SH(12)
693
d2e2c1e4
BK
694#define REQUIRE_EXT(ctx, ext) do { \
695 if (!has_ext(ctx, ext)) { \
696 return false; \
697 } \
698} while (0)
699
e98d9140
BK
700static int ex_rvc_register(int reg)
701{
702 return 8 + reg;
703}
704
2a53cff4
BK
705bool decode_insn32(DisasContext *ctx, uint32_t insn);
706/* Include the auto-generated decoder for 32 bit insn */
707#include "decode_insn32.inc.c"
7a50d3e2
BK
708
709static bool gen_arith_imm(DisasContext *ctx, arg_i *a,
710 void(*func)(TCGv, TCGv, TCGv))
711{
712 TCGv source1, source2;
713 source1 = tcg_temp_new();
714 source2 = tcg_temp_new();
715
716 gen_get_gpr(source1, a->rs1);
717 tcg_gen_movi_tl(source2, a->imm);
718
719 (*func)(source1, source1, source2);
720
721 gen_set_gpr(a->rd, source1);
722 tcg_temp_free(source1);
723 tcg_temp_free(source2);
724 return true;
725}
726
727#ifdef TARGET_RISCV64
728static void gen_addw(TCGv ret, TCGv arg1, TCGv arg2)
729{
730 tcg_gen_add_tl(ret, arg1, arg2);
731 tcg_gen_ext32s_tl(ret, ret);
732}
733#endif
734
2a53cff4
BK
735/* Include insn module translation function */
736#include "insn_trans/trans_rvi.inc.c"
d2e2c1e4 737#include "insn_trans/trans_rvm.inc.c"
3b77c289 738#include "insn_trans/trans_rva.inc.c"
6f0e74ff 739#include "insn_trans/trans_rvf.inc.c"
97f8b493 740#include "insn_trans/trans_rvd.inc.c"
4ba79c47 741#include "insn_trans/trans_privileged.inc.c"
2a53cff4 742
e98d9140
BK
743bool decode_insn16(DisasContext *ctx, uint16_t insn);
744/* auto-generated decoder*/
745#include "decode_insn16.inc.c"
746#include "insn_trans/trans_rvc.inc.c"
747
db9f3fd6 748static void decode_RV32_64G(DisasContext *ctx)
55c2a12c 749{
31fe4d35 750 int rs1, rd;
55c2a12c 751 uint32_t op;
55c2a12c
MC
752
753 /* We do not do misaligned address check here: the address should never be
754 * misaligned at this point. Instructions that set PC must do the check,
755 * since epc must be the address of the instruction that caused us to
756 * perform the misaligned instruction fetch */
757
758 op = MASK_OP_MAJOR(ctx->opcode);
759 rs1 = GET_RS1(ctx->opcode);
55c2a12c 760 rd = GET_RD(ctx->opcode);
55c2a12c
MC
761
762 switch (op) {
55c2a12c 763 case OPC_RISC_SYSTEM:
db9f3fd6 764 gen_system(ctx, MASK_OP_SYSTEM(ctx->opcode), rd, rs1,
55c2a12c
MC
765 (ctx->opcode & 0xFFF00000) >> 20);
766 break;
767 default:
768 gen_exception_illegal(ctx);
769 break;
770 }
771}
772
db9f3fd6 773static void decode_opc(DisasContext *ctx)
55c2a12c
MC
774{
775 /* check for compressed insn */
776 if (extract32(ctx->opcode, 0, 2) != 3) {
db9f3fd6 777 if (!has_ext(ctx, RVC)) {
55c2a12c
MC
778 gen_exception_illegal(ctx);
779 } else {
0114db1c 780 ctx->pc_succ_insn = ctx->base.pc_next + 2;
e98d9140
BK
781 if (!decode_insn16(ctx, ctx->opcode)) {
782 /* fall back to old decoder */
783 decode_RV32_64C(ctx);
784 }
55c2a12c
MC
785 }
786 } else {
0114db1c 787 ctx->pc_succ_insn = ctx->base.pc_next + 4;
2a53cff4
BK
788 if (!decode_insn32(ctx, ctx->opcode)) {
789 /* fallback to old decoder */
790 decode_RV32_64G(ctx);
791 }
55c2a12c
MC
792 }
793}
794
5b4f1d2d 795static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
55c2a12c 796{
5b4f1d2d 797 DisasContext *ctx = container_of(dcbase, DisasContext, base);
d75377bf 798 CPURISCVState *env = cs->env_ptr;
55c2a12c 799
5b4f1d2d 800 ctx->pc_succ_insn = ctx->base.pc_first;
5b4f1d2d 801 ctx->mem_idx = ctx->base.tb->flags & TB_FLAGS_MMU_MASK;
83a71719 802 ctx->mstatus_fs = ctx->base.tb->flags & TB_FLAGS_MSTATUS_FS;
d75377bf 803 ctx->priv_ver = env->priv_ver;
db9f3fd6 804 ctx->misa = env->misa;
5b4f1d2d
EC
805 ctx->frm = -1; /* unknown rounding mode */
806}
55c2a12c 807
5b4f1d2d
EC
808static void riscv_tr_tb_start(DisasContextBase *db, CPUState *cpu)
809{
810}
55c2a12c 811
5b4f1d2d
EC
812static void riscv_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
813{
814 DisasContext *ctx = container_of(dcbase, DisasContext, base);
815
816 tcg_gen_insn_start(ctx->base.pc_next);
817}
818
819static bool riscv_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,
820 const CPUBreakpoint *bp)
821{
822 DisasContext *ctx = container_of(dcbase, DisasContext, base);
823
824 tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next);
825 ctx->base.is_jmp = DISAS_NORETURN;
826 gen_exception_debug();
827 /* The address covered by the breakpoint must be included in
828 [tb->pc, tb->pc + tb->size) in order to for it to be
829 properly cleared -- thus we increment the PC here so that
830 the logic setting tb->size below does the right thing. */
831 ctx->base.pc_next += 4;
832 return true;
833}
834
5b4f1d2d
EC
835static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
836{
837 DisasContext *ctx = container_of(dcbase, DisasContext, base);
838 CPURISCVState *env = cpu->env_ptr;
55c2a12c 839
5b4f1d2d 840 ctx->opcode = cpu_ldl_code(env, ctx->base.pc_next);
db9f3fd6 841 decode_opc(ctx);
5b4f1d2d
EC
842 ctx->base.pc_next = ctx->pc_succ_insn;
843
844 if (ctx->base.is_jmp == DISAS_NEXT) {
845 target_ulong page_start;
846
847 page_start = ctx->base.pc_first & TARGET_PAGE_MASK;
848 if (ctx->base.pc_next - page_start >= TARGET_PAGE_SIZE) {
849 ctx->base.is_jmp = DISAS_TOO_MANY;
55c2a12c 850 }
55c2a12c 851 }
5b4f1d2d
EC
852}
853
854static void riscv_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
855{
856 DisasContext *ctx = container_of(dcbase, DisasContext, base);
857
858 switch (ctx->base.is_jmp) {
b2e32021 859 case DISAS_TOO_MANY:
ccf08e40 860 gen_goto_tb(ctx, 0, ctx->base.pc_next);
55c2a12c 861 break;
b2e32021 862 case DISAS_NORETURN:
55c2a12c 863 break;
b2e32021
EC
864 default:
865 g_assert_not_reached();
55c2a12c 866 }
5b4f1d2d
EC
867}
868
869static void riscv_tr_disas_log(const DisasContextBase *dcbase, CPUState *cpu)
870{
871 qemu_log("IN: %s\n", lookup_symbol(dcbase->pc_first));
872 log_target_disas(cpu, dcbase->pc_first, dcbase->tb->size);
873}
874
875static const TranslatorOps riscv_tr_ops = {
876 .init_disas_context = riscv_tr_init_disas_context,
877 .tb_start = riscv_tr_tb_start,
878 .insn_start = riscv_tr_insn_start,
879 .breakpoint_check = riscv_tr_breakpoint_check,
880 .translate_insn = riscv_tr_translate_insn,
881 .tb_stop = riscv_tr_tb_stop,
882 .disas_log = riscv_tr_disas_log,
883};
884
885void gen_intermediate_code(CPUState *cs, TranslationBlock *tb)
886{
887 DisasContext ctx;
888
889 translator_loop(&riscv_tr_ops, &ctx.base, cs, tb);
55c2a12c
MC
890}
891
892void riscv_translate_init(void)
893{
894 int i;
895
896 /* cpu_gpr[0] is a placeholder for the zero register. Do not use it. */
897 /* Use the gen_set_gpr and gen_get_gpr helper functions when accessing */
898 /* registers, unless you specifically block reads/writes to reg 0 */
899 cpu_gpr[0] = NULL;
900
901 for (i = 1; i < 32; i++) {
902 cpu_gpr[i] = tcg_global_mem_new(cpu_env,
903 offsetof(CPURISCVState, gpr[i]), riscv_int_regnames[i]);
904 }
905
906 for (i = 0; i < 32; i++) {
907 cpu_fpr[i] = tcg_global_mem_new_i64(cpu_env,
908 offsetof(CPURISCVState, fpr[i]), riscv_fpr_regnames[i]);
909 }
910
911 cpu_pc = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, pc), "pc");
912 load_res = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, load_res),
913 "load_res");
914 load_val = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, load_val),
915 "load_val");
916}
This page took 0.243972 seconds and 4 git commands to generate.