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 void gen_pack(TCGv ret, TCGv arg1, TCGv arg2) 511 { 512 tcg_gen_deposit_tl(ret, arg1, arg2, 513 TARGET_LONG_BITS / 2, 514 TARGET_LONG_BITS / 2); 515 } 516 517 static void gen_packu(TCGv ret, TCGv arg1, TCGv arg2) 518 { 519 TCGv t = tcg_temp_new(); 520 tcg_gen_shri_tl(t, arg1, TARGET_LONG_BITS / 2); 521 tcg_gen_deposit_tl(ret, arg2, t, 0, TARGET_LONG_BITS / 2); 522 tcg_temp_free(t); 523 } 524 525 static void gen_packh(TCGv ret, TCGv arg1, TCGv arg2) 526 { 527 TCGv t = tcg_temp_new(); 528 tcg_gen_ext8u_tl(t, arg2); 529 tcg_gen_deposit_tl(ret, arg1, t, 8, TARGET_LONG_BITS - 8); 530 tcg_temp_free(t); 531 } 532 533 static void gen_sbop_mask(TCGv ret, TCGv shamt) 534 { 535 tcg_gen_movi_tl(ret, 1); 536 tcg_gen_shl_tl(ret, ret, shamt); 537 } 538 539 static void gen_bset(TCGv ret, TCGv arg1, TCGv shamt) 540 { 541 TCGv t = tcg_temp_new(); 542 543 gen_sbop_mask(t, shamt); 544 tcg_gen_or_tl(ret, arg1, t); 545 546 tcg_temp_free(t); 547 } 548 549 static void gen_bclr(TCGv ret, TCGv arg1, TCGv shamt) 550 { 551 TCGv t = tcg_temp_new(); 552 553 gen_sbop_mask(t, shamt); 554 tcg_gen_andc_tl(ret, arg1, t); 555 556 tcg_temp_free(t); 557 } 558 559 static void gen_binv(TCGv ret, TCGv arg1, TCGv shamt) 560 { 561 TCGv t = tcg_temp_new(); 562 563 gen_sbop_mask(t, shamt); 564 tcg_gen_xor_tl(ret, arg1, t); 565 566 tcg_temp_free(t); 567 } 568 569 static void gen_bext(TCGv ret, TCGv arg1, TCGv shamt) 570 { 571 tcg_gen_shr_tl(ret, arg1, shamt); 572 tcg_gen_andi_tl(ret, ret, 1); 573 } 574 575 static void gen_slo(TCGv ret, TCGv arg1, TCGv arg2) 576 { 577 tcg_gen_not_tl(ret, arg1); 578 tcg_gen_shl_tl(ret, ret, arg2); 579 tcg_gen_not_tl(ret, ret); 580 } 581 582 static void gen_sro(TCGv ret, TCGv arg1, TCGv arg2) 583 { 584 tcg_gen_not_tl(ret, arg1); 585 tcg_gen_shr_tl(ret, ret, arg2); 586 tcg_gen_not_tl(ret, ret); 587 } 588 589 static bool gen_grevi(DisasContext *ctx, arg_grevi *a) 590 { 591 TCGv source1 = tcg_temp_new(); 592 TCGv source2; 593 594 gen_get_gpr(ctx, source1, a->rs1); 595 596 if (a->shamt == (TARGET_LONG_BITS - 8)) { 597 /* rev8, byte swaps */ 598 tcg_gen_bswap_tl(source1, source1); 599 } else { 600 source2 = tcg_temp_new(); 601 tcg_gen_movi_tl(source2, a->shamt); 602 gen_helper_grev(source1, source1, source2); 603 tcg_temp_free(source2); 604 } 605 606 gen_set_gpr(ctx, a->rd, source1); 607 tcg_temp_free(source1); 608 return true; 609 } 610 611 #define GEN_SHADD(SHAMT) \ 612 static void gen_sh##SHAMT##add(TCGv ret, TCGv arg1, TCGv arg2) \ 613 { \ 614 TCGv t = tcg_temp_new(); \ 615 \ 616 tcg_gen_shli_tl(t, arg1, SHAMT); \ 617 tcg_gen_add_tl(ret, t, arg2); \ 618 \ 619 tcg_temp_free(t); \ 620 } 621 622 GEN_SHADD(1) 623 GEN_SHADD(2) 624 GEN_SHADD(3) 625 626 static void gen_ctzw(TCGv ret, TCGv arg1) 627 { 628 tcg_gen_ori_tl(ret, arg1, (target_ulong)MAKE_64BIT_MASK(32, 32)); 629 tcg_gen_ctzi_tl(ret, ret, 64); 630 } 631 632 static void gen_clzw(TCGv ret, TCGv arg1) 633 { 634 tcg_gen_ext32u_tl(ret, arg1); 635 tcg_gen_clzi_tl(ret, ret, 64); 636 tcg_gen_subi_tl(ret, ret, 32); 637 } 638 639 static void gen_cpopw(TCGv ret, TCGv arg1) 640 { 641 tcg_gen_ext32u_tl(arg1, arg1); 642 tcg_gen_ctpop_tl(ret, arg1); 643 } 644 645 static void gen_packw(TCGv ret, TCGv arg1, TCGv arg2) 646 { 647 TCGv t = tcg_temp_new(); 648 tcg_gen_ext16s_tl(t, arg2); 649 tcg_gen_deposit_tl(ret, arg1, t, 16, 48); 650 tcg_temp_free(t); 651 } 652 653 static void gen_packuw(TCGv ret, TCGv arg1, TCGv arg2) 654 { 655 TCGv t = tcg_temp_new(); 656 tcg_gen_shri_tl(t, arg1, 16); 657 tcg_gen_deposit_tl(ret, arg2, t, 0, 16); 658 tcg_gen_ext32s_tl(ret, ret); 659 tcg_temp_free(t); 660 } 661 662 static void gen_rorw(TCGv ret, TCGv arg1, TCGv arg2) 663 { 664 TCGv_i32 t1 = tcg_temp_new_i32(); 665 TCGv_i32 t2 = tcg_temp_new_i32(); 666 667 /* truncate to 32-bits */ 668 tcg_gen_trunc_tl_i32(t1, arg1); 669 tcg_gen_trunc_tl_i32(t2, arg2); 670 671 tcg_gen_rotr_i32(t1, t1, t2); 672 673 /* sign-extend 64-bits */ 674 tcg_gen_ext_i32_tl(ret, t1); 675 676 tcg_temp_free_i32(t1); 677 tcg_temp_free_i32(t2); 678 } 679 680 static void gen_rolw(TCGv ret, TCGv arg1, TCGv arg2) 681 { 682 TCGv_i32 t1 = tcg_temp_new_i32(); 683 TCGv_i32 t2 = tcg_temp_new_i32(); 684 685 /* truncate to 32-bits */ 686 tcg_gen_trunc_tl_i32(t1, arg1); 687 tcg_gen_trunc_tl_i32(t2, arg2); 688 689 tcg_gen_rotl_i32(t1, t1, t2); 690 691 /* sign-extend 64-bits */ 692 tcg_gen_ext_i32_tl(ret, t1); 693 694 tcg_temp_free_i32(t1); 695 tcg_temp_free_i32(t2); 696 } 697 698 static void gen_grevw(TCGv ret, TCGv arg1, TCGv arg2) 699 { 700 tcg_gen_ext32u_tl(arg1, arg1); 701 gen_helper_grev(ret, arg1, arg2); 702 } 703 704 static void gen_gorcw(TCGv ret, TCGv arg1, TCGv arg2) 705 { 706 tcg_gen_ext32u_tl(arg1, arg1); 707 gen_helper_gorcw(ret, arg1, arg2); 708 } 709 710 #define GEN_SHADD_UW(SHAMT) \ 711 static void gen_sh##SHAMT##add_uw(TCGv ret, TCGv arg1, TCGv arg2) \ 712 { \ 713 TCGv t = tcg_temp_new(); \ 714 \ 715 tcg_gen_ext32u_tl(t, arg1); \ 716 \ 717 tcg_gen_shli_tl(t, t, SHAMT); \ 718 tcg_gen_add_tl(ret, t, arg2); \ 719 \ 720 tcg_temp_free(t); \ 721 } 722 723 GEN_SHADD_UW(1) 724 GEN_SHADD_UW(2) 725 GEN_SHADD_UW(3) 726 727 static void gen_add_uw(TCGv ret, TCGv arg1, TCGv arg2) 728 { 729 tcg_gen_ext32u_tl(arg1, arg1); 730 tcg_gen_add_tl(ret, arg1, arg2); 731 } 732 733 static bool gen_arith(DisasContext *ctx, arg_r *a, DisasExtend ext, 734 void (*func)(TCGv, TCGv, TCGv)) 735 { 736 TCGv dest = dest_gpr(ctx, a->rd); 737 TCGv src1 = get_gpr(ctx, a->rs1, ext); 738 TCGv src2 = get_gpr(ctx, a->rs2, ext); 739 740 func(dest, src1, src2); 741 742 gen_set_gpr(ctx, a->rd, dest); 743 return true; 744 } 745 746 static bool gen_shift(DisasContext *ctx, arg_r *a, 747 void(*func)(TCGv, TCGv, TCGv)) 748 { 749 TCGv source1 = tcg_temp_new(); 750 TCGv source2 = tcg_temp_new(); 751 752 gen_get_gpr(ctx, source1, a->rs1); 753 gen_get_gpr(ctx, source2, a->rs2); 754 755 tcg_gen_andi_tl(source2, source2, TARGET_LONG_BITS - 1); 756 (*func)(source1, source1, source2); 757 758 gen_set_gpr(ctx, a->rd, source1); 759 tcg_temp_free(source1); 760 tcg_temp_free(source2); 761 return true; 762 } 763 764 static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc) 765 { 766 DisasContext *ctx = container_of(dcbase, DisasContext, base); 767 CPUState *cpu = ctx->cs; 768 CPURISCVState *env = cpu->env_ptr; 769 770 return cpu_ldl_code(env, pc); 771 } 772 773 static bool gen_shifti(DisasContext *ctx, arg_shift *a, 774 void(*func)(TCGv, TCGv, TCGv)) 775 { 776 if (a->shamt >= TARGET_LONG_BITS) { 777 return false; 778 } 779 780 TCGv source1 = tcg_temp_new(); 781 TCGv source2 = tcg_temp_new(); 782 783 gen_get_gpr(ctx, source1, a->rs1); 784 785 tcg_gen_movi_tl(source2, a->shamt); 786 (*func)(source1, source1, source2); 787 788 gen_set_gpr(ctx, a->rd, source1); 789 tcg_temp_free(source1); 790 tcg_temp_free(source2); 791 return true; 792 } 793 794 static bool gen_shiftw(DisasContext *ctx, arg_r *a, 795 void(*func)(TCGv, TCGv, TCGv)) 796 { 797 TCGv source1 = tcg_temp_new(); 798 TCGv source2 = tcg_temp_new(); 799 800 gen_get_gpr(ctx, source1, a->rs1); 801 gen_get_gpr(ctx, source2, a->rs2); 802 803 tcg_gen_andi_tl(source2, source2, 31); 804 (*func)(source1, source1, source2); 805 tcg_gen_ext32s_tl(source1, source1); 806 807 gen_set_gpr(ctx, a->rd, source1); 808 tcg_temp_free(source1); 809 tcg_temp_free(source2); 810 return true; 811 } 812 813 static bool gen_shiftiw(DisasContext *ctx, arg_shift *a, 814 void(*func)(TCGv, TCGv, TCGv)) 815 { 816 TCGv source1 = tcg_temp_new(); 817 TCGv source2 = tcg_temp_new(); 818 819 gen_get_gpr(ctx, source1, a->rs1); 820 tcg_gen_movi_tl(source2, a->shamt); 821 822 (*func)(source1, source1, source2); 823 tcg_gen_ext32s_tl(source1, source1); 824 825 gen_set_gpr(ctx, a->rd, source1); 826 tcg_temp_free(source1); 827 tcg_temp_free(source2); 828 return true; 829 } 830 831 static void gen_ctz(TCGv ret, TCGv arg1) 832 { 833 tcg_gen_ctzi_tl(ret, arg1, TARGET_LONG_BITS); 834 } 835 836 static void gen_clz(TCGv ret, TCGv arg1) 837 { 838 tcg_gen_clzi_tl(ret, arg1, TARGET_LONG_BITS); 839 } 840 841 static bool gen_unary(DisasContext *ctx, arg_r2 *a, 842 void(*func)(TCGv, TCGv)) 843 { 844 TCGv source = tcg_temp_new(); 845 846 gen_get_gpr(ctx, source, a->rs1); 847 848 (*func)(source, source); 849 850 gen_set_gpr(ctx, a->rd, source); 851 tcg_temp_free(source); 852 return true; 853 } 854 855 /* Include insn module translation function */ 856 #include "insn_trans/trans_rvi.c.inc" 857 #include "insn_trans/trans_rvm.c.inc" 858 #include "insn_trans/trans_rva.c.inc" 859 #include "insn_trans/trans_rvf.c.inc" 860 #include "insn_trans/trans_rvd.c.inc" 861 #include "insn_trans/trans_rvh.c.inc" 862 #include "insn_trans/trans_rvv.c.inc" 863 #include "insn_trans/trans_rvb.c.inc" 864 #include "insn_trans/trans_privileged.c.inc" 865 866 /* Include the auto-generated decoder for 16 bit insn */ 867 #include "decode-insn16.c.inc" 868 869 static void decode_opc(CPURISCVState *env, DisasContext *ctx, uint16_t opcode) 870 { 871 /* check for compressed insn */ 872 if (extract16(opcode, 0, 2) != 3) { 873 if (!has_ext(ctx, RVC)) { 874 gen_exception_illegal(ctx); 875 } else { 876 ctx->pc_succ_insn = ctx->base.pc_next + 2; 877 if (!decode_insn16(ctx, opcode)) { 878 gen_exception_illegal(ctx); 879 } 880 } 881 } else { 882 uint32_t opcode32 = opcode; 883 opcode32 = deposit32(opcode32, 16, 16, 884 translator_lduw(env, ctx->base.pc_next + 2)); 885 ctx->pc_succ_insn = ctx->base.pc_next + 4; 886 if (!decode_insn32(ctx, opcode32)) { 887 gen_exception_illegal(ctx); 888 } 889 } 890 } 891 892 static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs) 893 { 894 DisasContext *ctx = container_of(dcbase, DisasContext, base); 895 CPURISCVState *env = cs->env_ptr; 896 RISCVCPU *cpu = RISCV_CPU(cs); 897 uint32_t tb_flags = ctx->base.tb->flags; 898 899 ctx->pc_succ_insn = ctx->base.pc_first; 900 ctx->mem_idx = tb_flags & TB_FLAGS_MMU_MASK; 901 ctx->mstatus_fs = tb_flags & TB_FLAGS_MSTATUS_FS; 902 ctx->priv_ver = env->priv_ver; 903 #if !defined(CONFIG_USER_ONLY) 904 if (riscv_has_ext(env, RVH)) { 905 ctx->virt_enabled = riscv_cpu_virt_enabled(env); 906 } else { 907 ctx->virt_enabled = false; 908 } 909 #else 910 ctx->virt_enabled = false; 911 #endif 912 ctx->misa = env->misa; 913 ctx->frm = -1; /* unknown rounding mode */ 914 ctx->ext_ifencei = cpu->cfg.ext_ifencei; 915 ctx->vlen = cpu->cfg.vlen; 916 ctx->hlsx = FIELD_EX32(tb_flags, TB_FLAGS, HLSX); 917 ctx->vill = FIELD_EX32(tb_flags, TB_FLAGS, VILL); 918 ctx->sew = FIELD_EX32(tb_flags, TB_FLAGS, SEW); 919 ctx->lmul = FIELD_EX32(tb_flags, TB_FLAGS, LMUL); 920 ctx->mlen = 1 << (ctx->sew + 3 - ctx->lmul); 921 ctx->vl_eq_vlmax = FIELD_EX32(tb_flags, TB_FLAGS, VL_EQ_VLMAX); 922 ctx->cs = cs; 923 ctx->w = false; 924 ctx->ntemp = 0; 925 memset(ctx->temp, 0, sizeof(ctx->temp)); 926 927 ctx->zero = tcg_constant_tl(0); 928 } 929 930 static void riscv_tr_tb_start(DisasContextBase *db, CPUState *cpu) 931 { 932 } 933 934 static void riscv_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu) 935 { 936 DisasContext *ctx = container_of(dcbase, DisasContext, base); 937 938 tcg_gen_insn_start(ctx->base.pc_next); 939 } 940 941 static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu) 942 { 943 DisasContext *ctx = container_of(dcbase, DisasContext, base); 944 CPURISCVState *env = cpu->env_ptr; 945 uint16_t opcode16 = translator_lduw(env, ctx->base.pc_next); 946 947 decode_opc(env, ctx, opcode16); 948 ctx->base.pc_next = ctx->pc_succ_insn; 949 ctx->w = false; 950 951 for (int i = ctx->ntemp - 1; i >= 0; --i) { 952 tcg_temp_free(ctx->temp[i]); 953 ctx->temp[i] = NULL; 954 } 955 ctx->ntemp = 0; 956 957 if (ctx->base.is_jmp == DISAS_NEXT) { 958 target_ulong page_start; 959 960 page_start = ctx->base.pc_first & TARGET_PAGE_MASK; 961 if (ctx->base.pc_next - page_start >= TARGET_PAGE_SIZE) { 962 ctx->base.is_jmp = DISAS_TOO_MANY; 963 } 964 } 965 } 966 967 static void riscv_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu) 968 { 969 DisasContext *ctx = container_of(dcbase, DisasContext, base); 970 971 switch (ctx->base.is_jmp) { 972 case DISAS_TOO_MANY: 973 gen_goto_tb(ctx, 0, ctx->base.pc_next); 974 break; 975 case DISAS_NORETURN: 976 break; 977 default: 978 g_assert_not_reached(); 979 } 980 } 981 982 static void riscv_tr_disas_log(const DisasContextBase *dcbase, CPUState *cpu) 983 { 984 #ifndef CONFIG_USER_ONLY 985 RISCVCPU *rvcpu = RISCV_CPU(cpu); 986 CPURISCVState *env = &rvcpu->env; 987 #endif 988 989 qemu_log("IN: %s\n", lookup_symbol(dcbase->pc_first)); 990 #ifndef CONFIG_USER_ONLY 991 qemu_log("Priv: "TARGET_FMT_ld"; Virt: "TARGET_FMT_ld"\n", env->priv, env->virt); 992 #endif 993 log_target_disas(cpu, dcbase->pc_first, dcbase->tb->size); 994 } 995 996 static const TranslatorOps riscv_tr_ops = { 997 .init_disas_context = riscv_tr_init_disas_context, 998 .tb_start = riscv_tr_tb_start, 999 .insn_start = riscv_tr_insn_start, 1000 .translate_insn = riscv_tr_translate_insn, 1001 .tb_stop = riscv_tr_tb_stop, 1002 .disas_log = riscv_tr_disas_log, 1003 }; 1004 1005 void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns) 1006 { 1007 DisasContext ctx; 1008 1009 translator_loop(&riscv_tr_ops, &ctx.base, cs, tb, max_insns); 1010 } 1011 1012 void riscv_translate_init(void) 1013 { 1014 int i; 1015 1016 /* cpu_gpr[0] is a placeholder for the zero register. Do not use it. */ 1017 /* Use the gen_set_gpr and gen_get_gpr helper functions when accessing */ 1018 /* registers, unless you specifically block reads/writes to reg 0 */ 1019 cpu_gpr[0] = NULL; 1020 1021 for (i = 1; i < 32; i++) { 1022 cpu_gpr[i] = tcg_global_mem_new(cpu_env, 1023 offsetof(CPURISCVState, gpr[i]), riscv_int_regnames[i]); 1024 } 1025 1026 for (i = 0; i < 32; i++) { 1027 cpu_fpr[i] = tcg_global_mem_new_i64(cpu_env, 1028 offsetof(CPURISCVState, fpr[i]), riscv_fpr_regnames[i]); 1029 } 1030 1031 cpu_pc = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, pc), "pc"); 1032 cpu_vl = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, vl), "vl"); 1033 load_res = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, load_res), 1034 "load_res"); 1035 load_val = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, load_val), 1036 "load_val"); 1037 } 1038