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