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 #include "exec/target_page.h" 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 #ifndef CONFIG_USER_ONLY 565 /* 566 * Direct calls 567 * - jal x1; 568 * - jal x5; 569 * - c.jal. 570 * - cm.jalt. 571 * 572 * Direct jumps 573 * - jal x0; 574 * - c.j; 575 * - cm.jt. 576 * 577 * Other direct jumps 578 * - jal rd where rd != x1 and rd != x5 and rd != x0; 579 */ 580 static void gen_ctr_jal(DisasContext *ctx, int rd, target_ulong imm) 581 { 582 TCGv dest = tcg_temp_new(); 583 TCGv src = tcg_temp_new(); 584 TCGv type; 585 586 /* 587 * If rd is x1 or x5 link registers, treat this as direct call otherwise 588 * its a direct jump. 589 */ 590 if (rd == 1 || rd == 5) { 591 type = tcg_constant_tl(CTRDATA_TYPE_DIRECT_CALL); 592 } else if (rd == 0) { 593 type = tcg_constant_tl(CTRDATA_TYPE_DIRECT_JUMP); 594 } else { 595 type = tcg_constant_tl(CTRDATA_TYPE_OTHER_DIRECT_JUMP); 596 } 597 598 gen_pc_plus_diff(dest, ctx, imm); 599 gen_pc_plus_diff(src, ctx, 0); 600 gen_helper_ctr_add_entry(tcg_env, src, dest, type); 601 } 602 #endif 603 604 static void gen_jal(DisasContext *ctx, int rd, target_ulong imm) 605 { 606 TCGv succ_pc = dest_gpr(ctx, rd); 607 608 /* check misaligned: */ 609 if (!riscv_cpu_allow_16bit_insn(ctx->cfg_ptr, 610 ctx->priv_ver, 611 ctx->misa_ext)) { 612 if ((imm & 0x3) != 0) { 613 TCGv target_pc = tcg_temp_new(); 614 gen_pc_plus_diff(target_pc, ctx, imm); 615 gen_exception_inst_addr_mis(ctx, target_pc); 616 return; 617 } 618 } 619 620 #ifndef CONFIG_USER_ONLY 621 if (ctx->cfg_ptr->ext_smctr || ctx->cfg_ptr->ext_ssctr) { 622 gen_ctr_jal(ctx, rd, imm); 623 } 624 #endif 625 626 gen_pc_plus_diff(succ_pc, ctx, ctx->cur_insn_len); 627 gen_set_gpr(ctx, rd, succ_pc); 628 629 gen_goto_tb(ctx, 0, imm); /* must use this for safety */ 630 ctx->base.is_jmp = DISAS_NORETURN; 631 } 632 633 /* Compute a canonical address from a register plus offset. */ 634 static TCGv get_address(DisasContext *ctx, int rs1, int imm) 635 { 636 TCGv addr = tcg_temp_new(); 637 TCGv src1 = get_gpr(ctx, rs1, EXT_NONE); 638 639 tcg_gen_addi_tl(addr, src1, imm); 640 if (ctx->addr_signed) { 641 tcg_gen_sextract_tl(addr, addr, 0, ctx->addr_xl); 642 } else { 643 tcg_gen_extract_tl(addr, addr, 0, ctx->addr_xl); 644 } 645 646 return addr; 647 } 648 649 /* Compute a canonical address from a register plus reg offset. */ 650 static TCGv get_address_indexed(DisasContext *ctx, int rs1, TCGv offs) 651 { 652 TCGv addr = tcg_temp_new(); 653 TCGv src1 = get_gpr(ctx, rs1, EXT_NONE); 654 655 tcg_gen_add_tl(addr, src1, offs); 656 if (ctx->addr_signed) { 657 tcg_gen_sextract_tl(addr, addr, 0, ctx->addr_xl); 658 } else { 659 tcg_gen_extract_tl(addr, addr, 0, ctx->addr_xl); 660 } 661 662 return addr; 663 } 664 665 #ifndef CONFIG_USER_ONLY 666 /* 667 * We will have already diagnosed disabled state, 668 * and need to turn initial/clean into dirty. 669 */ 670 static void mark_fs_dirty(DisasContext *ctx) 671 { 672 TCGv tmp; 673 674 if (!has_ext(ctx, RVF)) { 675 return; 676 } 677 678 if (ctx->mstatus_fs != EXT_STATUS_DIRTY) { 679 /* Remember the state change for the rest of the TB. */ 680 ctx->mstatus_fs = EXT_STATUS_DIRTY; 681 682 tmp = tcg_temp_new(); 683 tcg_gen_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); 684 tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS); 685 tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); 686 687 if (ctx->virt_enabled) { 688 tcg_gen_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs)); 689 tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS); 690 tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs)); 691 } 692 } 693 } 694 #else 695 static inline void mark_fs_dirty(DisasContext *ctx) { } 696 #endif 697 698 #ifndef CONFIG_USER_ONLY 699 /* 700 * We will have already diagnosed disabled state, 701 * and need to turn initial/clean into dirty. 702 */ 703 static void mark_vs_dirty(DisasContext *ctx) 704 { 705 TCGv tmp; 706 707 if (ctx->mstatus_vs != EXT_STATUS_DIRTY) { 708 /* Remember the state change for the rest of the TB. */ 709 ctx->mstatus_vs = EXT_STATUS_DIRTY; 710 711 tmp = tcg_temp_new(); 712 tcg_gen_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); 713 tcg_gen_ori_tl(tmp, tmp, MSTATUS_VS); 714 tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); 715 716 if (ctx->virt_enabled) { 717 tcg_gen_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs)); 718 tcg_gen_ori_tl(tmp, tmp, MSTATUS_VS); 719 tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs)); 720 } 721 } 722 } 723 #else 724 static inline void mark_vs_dirty(DisasContext *ctx) { } 725 #endif 726 727 static void finalize_rvv_inst(DisasContext *ctx) 728 { 729 mark_vs_dirty(ctx); 730 ctx->vstart_eq_zero = true; 731 } 732 733 static void gen_set_rm(DisasContext *ctx, int rm) 734 { 735 if (ctx->frm == rm) { 736 return; 737 } 738 ctx->frm = rm; 739 740 if (rm == RISCV_FRM_DYN) { 741 /* The helper will return only if frm valid. */ 742 ctx->frm_valid = true; 743 } 744 745 /* The helper may raise ILLEGAL_INSN -- record binv for unwind. */ 746 decode_save_opc(ctx, 0); 747 gen_helper_set_rounding_mode(tcg_env, tcg_constant_i32(rm)); 748 } 749 750 static void gen_set_rm_chkfrm(DisasContext *ctx, int rm) 751 { 752 if (ctx->frm == rm && ctx->frm_valid) { 753 return; 754 } 755 ctx->frm = rm; 756 ctx->frm_valid = true; 757 758 /* The helper may raise ILLEGAL_INSN -- record binv for unwind. */ 759 decode_save_opc(ctx, 0); 760 gen_helper_set_rounding_mode_chkfrm(tcg_env, tcg_constant_i32(rm)); 761 } 762 763 static int ex_plus_1(DisasContext *ctx, int nf) 764 { 765 return nf + 1; 766 } 767 768 #define EX_SH(amount) \ 769 static int ex_shift_##amount(DisasContext *ctx, int imm) \ 770 { \ 771 return imm << amount; \ 772 } 773 EX_SH(1) 774 EX_SH(2) 775 EX_SH(3) 776 EX_SH(4) 777 EX_SH(12) 778 779 #define REQUIRE_EXT(ctx, ext) do { \ 780 if (!has_ext(ctx, ext)) { \ 781 return false; \ 782 } \ 783 } while (0) 784 785 #define REQUIRE_32BIT(ctx) do { \ 786 if (get_xl(ctx) != MXL_RV32) { \ 787 return false; \ 788 } \ 789 } while (0) 790 791 #define REQUIRE_64BIT(ctx) do { \ 792 if (get_xl(ctx) != MXL_RV64) { \ 793 return false; \ 794 } \ 795 } while (0) 796 797 #define REQUIRE_128BIT(ctx) do { \ 798 if (get_xl(ctx) != MXL_RV128) { \ 799 return false; \ 800 } \ 801 } while (0) 802 803 #define REQUIRE_64_OR_128BIT(ctx) do { \ 804 if (get_xl(ctx) == MXL_RV32) { \ 805 return false; \ 806 } \ 807 } while (0) 808 809 #define REQUIRE_EITHER_EXT(ctx, A, B) do { \ 810 if (!ctx->cfg_ptr->ext_##A && \ 811 !ctx->cfg_ptr->ext_##B) { \ 812 return false; \ 813 } \ 814 } while (0) 815 816 static int ex_rvc_register(DisasContext *ctx, int reg) 817 { 818 return 8 + reg; 819 } 820 821 static int ex_sreg_register(DisasContext *ctx, int reg) 822 { 823 return reg < 2 ? reg + 8 : reg + 16; 824 } 825 826 static int ex_rvc_shiftli(DisasContext *ctx, int imm) 827 { 828 /* For RV128 a shamt of 0 means a shift by 64. */ 829 if (get_ol(ctx) == MXL_RV128) { 830 imm = imm ? imm : 64; 831 } 832 return imm; 833 } 834 835 static int ex_rvc_shiftri(DisasContext *ctx, int imm) 836 { 837 /* 838 * For RV128 a shamt of 0 means a shift by 64, furthermore, for right 839 * shifts, the shamt is sign-extended. 840 */ 841 if (get_ol(ctx) == MXL_RV128) { 842 imm = imm | (imm & 32) << 1; 843 imm = imm ? imm : 64; 844 } 845 return imm; 846 } 847 848 /* Include the auto-generated decoder for 32 bit insn */ 849 #include "decode-insn32.c.inc" 850 851 static bool gen_logic_imm_fn(DisasContext *ctx, arg_i *a, 852 void (*func)(TCGv, TCGv, target_long)) 853 { 854 TCGv dest = dest_gpr(ctx, a->rd); 855 TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); 856 857 func(dest, src1, a->imm); 858 859 if (get_xl(ctx) == MXL_RV128) { 860 TCGv src1h = get_gprh(ctx, a->rs1); 861 TCGv desth = dest_gprh(ctx, a->rd); 862 863 func(desth, src1h, -(a->imm < 0)); 864 gen_set_gpr128(ctx, a->rd, dest, desth); 865 } else { 866 gen_set_gpr(ctx, a->rd, dest); 867 } 868 869 return true; 870 } 871 872 static bool gen_logic(DisasContext *ctx, arg_r *a, 873 void (*func)(TCGv, TCGv, TCGv)) 874 { 875 TCGv dest = dest_gpr(ctx, a->rd); 876 TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); 877 TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); 878 879 func(dest, src1, src2); 880 881 if (get_xl(ctx) == MXL_RV128) { 882 TCGv src1h = get_gprh(ctx, a->rs1); 883 TCGv src2h = get_gprh(ctx, a->rs2); 884 TCGv desth = dest_gprh(ctx, a->rd); 885 886 func(desth, src1h, src2h); 887 gen_set_gpr128(ctx, a->rd, dest, desth); 888 } else { 889 gen_set_gpr(ctx, a->rd, dest); 890 } 891 892 return true; 893 } 894 895 static bool gen_arith_imm_fn(DisasContext *ctx, arg_i *a, DisasExtend ext, 896 void (*func)(TCGv, TCGv, target_long), 897 void (*f128)(TCGv, TCGv, TCGv, TCGv, target_long)) 898 { 899 TCGv dest = dest_gpr(ctx, a->rd); 900 TCGv src1 = get_gpr(ctx, a->rs1, ext); 901 902 if (get_ol(ctx) < MXL_RV128) { 903 func(dest, src1, a->imm); 904 gen_set_gpr(ctx, a->rd, dest); 905 } else { 906 if (f128 == NULL) { 907 return false; 908 } 909 910 TCGv src1h = get_gprh(ctx, a->rs1); 911 TCGv desth = dest_gprh(ctx, a->rd); 912 913 f128(dest, desth, src1, src1h, a->imm); 914 gen_set_gpr128(ctx, a->rd, dest, desth); 915 } 916 return true; 917 } 918 919 static bool gen_arith_imm_tl(DisasContext *ctx, arg_i *a, DisasExtend ext, 920 void (*func)(TCGv, TCGv, TCGv), 921 void (*f128)(TCGv, TCGv, TCGv, TCGv, TCGv, TCGv)) 922 { 923 TCGv dest = dest_gpr(ctx, a->rd); 924 TCGv src1 = get_gpr(ctx, a->rs1, ext); 925 TCGv src2 = tcg_constant_tl(a->imm); 926 927 if (get_ol(ctx) < MXL_RV128) { 928 func(dest, src1, src2); 929 gen_set_gpr(ctx, a->rd, dest); 930 } else { 931 if (f128 == NULL) { 932 return false; 933 } 934 935 TCGv src1h = get_gprh(ctx, a->rs1); 936 TCGv src2h = tcg_constant_tl(-(a->imm < 0)); 937 TCGv desth = dest_gprh(ctx, a->rd); 938 939 f128(dest, desth, src1, src1h, src2, src2h); 940 gen_set_gpr128(ctx, a->rd, dest, desth); 941 } 942 return true; 943 } 944 945 static bool gen_arith(DisasContext *ctx, arg_r *a, DisasExtend ext, 946 void (*func)(TCGv, TCGv, TCGv), 947 void (*f128)(TCGv, TCGv, TCGv, TCGv, TCGv, TCGv)) 948 { 949 TCGv dest = dest_gpr(ctx, a->rd); 950 TCGv src1 = get_gpr(ctx, a->rs1, ext); 951 TCGv src2 = get_gpr(ctx, a->rs2, ext); 952 953 if (get_ol(ctx) < MXL_RV128) { 954 func(dest, src1, src2); 955 gen_set_gpr(ctx, a->rd, dest); 956 } else { 957 if (f128 == NULL) { 958 return false; 959 } 960 961 TCGv src1h = get_gprh(ctx, a->rs1); 962 TCGv src2h = get_gprh(ctx, a->rs2); 963 TCGv desth = dest_gprh(ctx, a->rd); 964 965 f128(dest, desth, src1, src1h, src2, src2h); 966 gen_set_gpr128(ctx, a->rd, dest, desth); 967 } 968 return true; 969 } 970 971 static bool gen_arith_per_ol(DisasContext *ctx, arg_r *a, DisasExtend ext, 972 void (*f_tl)(TCGv, TCGv, TCGv), 973 void (*f_32)(TCGv, TCGv, TCGv), 974 void (*f_128)(TCGv, TCGv, TCGv, TCGv, TCGv, TCGv)) 975 { 976 int olen = get_olen(ctx); 977 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_arith(ctx, a, ext, f_tl, f_128); 986 } 987 988 static bool gen_shift_imm_fn(DisasContext *ctx, arg_shift *a, DisasExtend ext, 989 void (*func)(TCGv, TCGv, target_long), 990 void (*f128)(TCGv, TCGv, TCGv, TCGv, target_long)) 991 { 992 TCGv dest, src1; 993 int max_len = get_olen(ctx); 994 995 if (a->shamt >= max_len) { 996 return false; 997 } 998 999 dest = dest_gpr(ctx, a->rd); 1000 src1 = get_gpr(ctx, a->rs1, ext); 1001 1002 if (max_len < 128) { 1003 func(dest, src1, a->shamt); 1004 gen_set_gpr(ctx, a->rd, dest); 1005 } else { 1006 TCGv src1h = get_gprh(ctx, a->rs1); 1007 TCGv desth = dest_gprh(ctx, a->rd); 1008 1009 if (f128 == NULL) { 1010 return false; 1011 } 1012 f128(dest, desth, src1, src1h, a->shamt); 1013 gen_set_gpr128(ctx, a->rd, dest, desth); 1014 } 1015 return true; 1016 } 1017 1018 static bool gen_shift_imm_fn_per_ol(DisasContext *ctx, arg_shift *a, 1019 DisasExtend ext, 1020 void (*f_tl)(TCGv, TCGv, target_long), 1021 void (*f_32)(TCGv, TCGv, target_long), 1022 void (*f_128)(TCGv, TCGv, TCGv, TCGv, 1023 target_long)) 1024 { 1025 int olen = get_olen(ctx); 1026 if (olen != TARGET_LONG_BITS) { 1027 if (olen == 32) { 1028 f_tl = f_32; 1029 } else if (olen != 128) { 1030 g_assert_not_reached(); 1031 } 1032 } 1033 return gen_shift_imm_fn(ctx, a, ext, f_tl, f_128); 1034 } 1035 1036 static bool gen_shift_imm_tl(DisasContext *ctx, arg_shift *a, DisasExtend ext, 1037 void (*func)(TCGv, TCGv, TCGv)) 1038 { 1039 TCGv dest, src1, src2; 1040 int max_len = get_olen(ctx); 1041 1042 if (a->shamt >= max_len) { 1043 return false; 1044 } 1045 1046 dest = dest_gpr(ctx, a->rd); 1047 src1 = get_gpr(ctx, a->rs1, ext); 1048 src2 = tcg_constant_tl(a->shamt); 1049 1050 func(dest, src1, src2); 1051 1052 gen_set_gpr(ctx, a->rd, dest); 1053 return true; 1054 } 1055 1056 static bool gen_shift(DisasContext *ctx, arg_r *a, DisasExtend ext, 1057 void (*func)(TCGv, TCGv, TCGv), 1058 void (*f128)(TCGv, TCGv, TCGv, TCGv, TCGv)) 1059 { 1060 TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); 1061 TCGv ext2 = tcg_temp_new(); 1062 int max_len = get_olen(ctx); 1063 1064 tcg_gen_andi_tl(ext2, src2, max_len - 1); 1065 1066 TCGv dest = dest_gpr(ctx, a->rd); 1067 TCGv src1 = get_gpr(ctx, a->rs1, ext); 1068 1069 if (max_len < 128) { 1070 func(dest, src1, ext2); 1071 gen_set_gpr(ctx, a->rd, dest); 1072 } else { 1073 TCGv src1h = get_gprh(ctx, a->rs1); 1074 TCGv desth = dest_gprh(ctx, a->rd); 1075 1076 if (f128 == NULL) { 1077 return false; 1078 } 1079 f128(dest, desth, src1, src1h, ext2); 1080 gen_set_gpr128(ctx, a->rd, dest, desth); 1081 } 1082 return true; 1083 } 1084 1085 static bool gen_shift_per_ol(DisasContext *ctx, arg_r *a, DisasExtend ext, 1086 void (*f_tl)(TCGv, TCGv, TCGv), 1087 void (*f_32)(TCGv, TCGv, TCGv), 1088 void (*f_128)(TCGv, TCGv, TCGv, TCGv, TCGv)) 1089 { 1090 int olen = get_olen(ctx); 1091 if (olen != TARGET_LONG_BITS) { 1092 if (olen == 32) { 1093 f_tl = f_32; 1094 } else if (olen != 128) { 1095 g_assert_not_reached(); 1096 } 1097 } 1098 return gen_shift(ctx, a, ext, f_tl, f_128); 1099 } 1100 1101 static bool gen_unary(DisasContext *ctx, arg_r2 *a, DisasExtend ext, 1102 void (*func)(TCGv, TCGv)) 1103 { 1104 TCGv dest = dest_gpr(ctx, a->rd); 1105 TCGv src1 = get_gpr(ctx, a->rs1, ext); 1106 1107 func(dest, src1); 1108 1109 gen_set_gpr(ctx, a->rd, dest); 1110 return true; 1111 } 1112 1113 static bool gen_unary_per_ol(DisasContext *ctx, arg_r2 *a, DisasExtend ext, 1114 void (*f_tl)(TCGv, TCGv), 1115 void (*f_32)(TCGv, TCGv)) 1116 { 1117 int olen = get_olen(ctx); 1118 1119 if (olen != TARGET_LONG_BITS) { 1120 if (olen == 32) { 1121 f_tl = f_32; 1122 } else { 1123 g_assert_not_reached(); 1124 } 1125 } 1126 return gen_unary(ctx, a, ext, f_tl); 1127 } 1128 1129 static bool gen_amo(DisasContext *ctx, arg_atomic *a, 1130 void(*func)(TCGv, TCGv, TCGv, TCGArg, MemOp), 1131 MemOp mop) 1132 { 1133 TCGv dest = dest_gpr(ctx, a->rd); 1134 TCGv src1, src2 = get_gpr(ctx, a->rs2, EXT_NONE); 1135 MemOp size = mop & MO_SIZE; 1136 1137 if (ctx->cfg_ptr->ext_zama16b && size >= MO_32) { 1138 mop |= MO_ATOM_WITHIN16; 1139 } else { 1140 mop |= MO_ALIGN; 1141 } 1142 1143 decode_save_opc(ctx, RISCV_UW2_ALWAYS_STORE_AMO); 1144 src1 = get_address(ctx, a->rs1, 0); 1145 func(dest, src1, src2, ctx->mem_idx, mop); 1146 1147 gen_set_gpr(ctx, a->rd, dest); 1148 return true; 1149 } 1150 1151 static bool gen_cmpxchg(DisasContext *ctx, arg_atomic *a, MemOp mop) 1152 { 1153 TCGv dest = get_gpr(ctx, a->rd, EXT_NONE); 1154 TCGv src1 = get_address(ctx, a->rs1, 0); 1155 TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); 1156 1157 decode_save_opc(ctx, RISCV_UW2_ALWAYS_STORE_AMO); 1158 tcg_gen_atomic_cmpxchg_tl(dest, src1, dest, src2, ctx->mem_idx, mop); 1159 1160 gen_set_gpr(ctx, a->rd, dest); 1161 return true; 1162 } 1163 1164 static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc) 1165 { 1166 DisasContext *ctx = container_of(dcbase, DisasContext, base); 1167 CPUState *cpu = ctx->cs; 1168 CPURISCVState *env = cpu_env(cpu); 1169 1170 return translator_ldl(env, &ctx->base, pc); 1171 } 1172 1173 #define SS_MMU_INDEX(ctx) (ctx->mem_idx | MMU_IDX_SS_WRITE) 1174 1175 /* Include insn module translation function */ 1176 #include "insn_trans/trans_rvi.c.inc" 1177 #include "insn_trans/trans_rvm.c.inc" 1178 #include "insn_trans/trans_rva.c.inc" 1179 #include "insn_trans/trans_rvf.c.inc" 1180 #include "insn_trans/trans_rvd.c.inc" 1181 #include "insn_trans/trans_rvh.c.inc" 1182 #include "insn_trans/trans_rvv.c.inc" 1183 #include "insn_trans/trans_rvb.c.inc" 1184 #include "insn_trans/trans_rvzicond.c.inc" 1185 #include "insn_trans/trans_rvzacas.c.inc" 1186 #include "insn_trans/trans_rvzabha.c.inc" 1187 #include "insn_trans/trans_rvzawrs.c.inc" 1188 #include "insn_trans/trans_rvzicbo.c.inc" 1189 #include "insn_trans/trans_rvzimop.c.inc" 1190 #include "insn_trans/trans_rvzfa.c.inc" 1191 #include "insn_trans/trans_rvzfh.c.inc" 1192 #include "insn_trans/trans_rvk.c.inc" 1193 #include "insn_trans/trans_rvvk.c.inc" 1194 #include "insn_trans/trans_privileged.c.inc" 1195 #include "insn_trans/trans_svinval.c.inc" 1196 #include "insn_trans/trans_rvbf16.c.inc" 1197 #include "decode-xthead.c.inc" 1198 #include "insn_trans/trans_xthead.c.inc" 1199 #include "insn_trans/trans_xventanacondops.c.inc" 1200 1201 /* Include the auto-generated decoder for 16 bit insn */ 1202 #include "decode-insn16.c.inc" 1203 #include "insn_trans/trans_rvzce.c.inc" 1204 #include "insn_trans/trans_rvzcmop.c.inc" 1205 #include "insn_trans/trans_rvzicfiss.c.inc" 1206 1207 /* Include decoders for factored-out extensions */ 1208 #include "decode-XVentanaCondOps.c.inc" 1209 1210 /* The specification allows for longer insns, but not supported by qemu. */ 1211 #define MAX_INSN_LEN 4 1212 1213 static inline int insn_len(uint16_t first_word) 1214 { 1215 return (first_word & 3) == 3 ? 4 : 2; 1216 } 1217 1218 const RISCVDecoder decoder_table[] = { 1219 { always_true_p, decode_insn32 }, 1220 { has_xthead_p, decode_xthead}, 1221 { has_XVentanaCondOps_p, decode_XVentanaCodeOps}, 1222 }; 1223 1224 const size_t decoder_table_size = ARRAY_SIZE(decoder_table); 1225 1226 static void decode_opc(CPURISCVState *env, DisasContext *ctx, uint16_t opcode) 1227 { 1228 ctx->virt_inst_excp = false; 1229 ctx->cur_insn_len = insn_len(opcode); 1230 /* Check for compressed insn */ 1231 if (ctx->cur_insn_len == 2) { 1232 ctx->opcode = opcode; 1233 /* 1234 * The Zca extension is added as way to refer to instructions in the C 1235 * extension that do not include the floating-point loads and stores 1236 */ 1237 if ((has_ext(ctx, RVC) || ctx->cfg_ptr->ext_zca) && 1238 decode_insn16(ctx, opcode)) { 1239 return; 1240 } 1241 } else { 1242 uint32_t opcode32 = opcode; 1243 opcode32 = deposit32(opcode32, 16, 16, 1244 translator_lduw(env, &ctx->base, 1245 ctx->base.pc_next + 2)); 1246 ctx->opcode = opcode32; 1247 1248 for (guint i = 0; i < ctx->decoders->len; ++i) { 1249 riscv_cpu_decode_fn func = g_ptr_array_index(ctx->decoders, i); 1250 if (func(ctx, opcode32)) { 1251 return; 1252 } 1253 } 1254 } 1255 1256 gen_exception_illegal(ctx); 1257 } 1258 1259 static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs) 1260 { 1261 DisasContext *ctx = container_of(dcbase, DisasContext, base); 1262 CPURISCVState *env = cpu_env(cs); 1263 RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cs); 1264 RISCVCPU *cpu = RISCV_CPU(cs); 1265 uint32_t tb_flags = ctx->base.tb->flags; 1266 1267 ctx->pc_save = ctx->base.pc_first; 1268 ctx->priv = FIELD_EX32(tb_flags, TB_FLAGS, PRIV); 1269 ctx->mem_idx = FIELD_EX32(tb_flags, TB_FLAGS, MEM_IDX); 1270 ctx->mstatus_fs = FIELD_EX32(tb_flags, TB_FLAGS, FS); 1271 ctx->mstatus_vs = FIELD_EX32(tb_flags, TB_FLAGS, VS); 1272 ctx->priv_ver = env->priv_ver; 1273 ctx->virt_enabled = FIELD_EX32(tb_flags, TB_FLAGS, VIRT_ENABLED); 1274 ctx->misa_ext = env->misa_ext; 1275 ctx->frm = -1; /* unknown rounding mode */ 1276 ctx->cfg_ptr = &(cpu->cfg); 1277 ctx->vill = FIELD_EX32(tb_flags, TB_FLAGS, VILL); 1278 ctx->sew = FIELD_EX32(tb_flags, TB_FLAGS, SEW); 1279 ctx->lmul = sextract32(FIELD_EX32(tb_flags, TB_FLAGS, LMUL), 0, 3); 1280 ctx->vta = FIELD_EX32(tb_flags, TB_FLAGS, VTA) && cpu->cfg.rvv_ta_all_1s; 1281 ctx->vma = FIELD_EX32(tb_flags, TB_FLAGS, VMA) && cpu->cfg.rvv_ma_all_1s; 1282 ctx->cfg_vta_all_1s = cpu->cfg.rvv_ta_all_1s; 1283 ctx->vstart_eq_zero = FIELD_EX32(tb_flags, TB_FLAGS, VSTART_EQ_ZERO); 1284 ctx->vl_eq_vlmax = FIELD_EX32(tb_flags, TB_FLAGS, VL_EQ_VLMAX); 1285 ctx->misa_mxl_max = mcc->misa_mxl_max; 1286 ctx->xl = FIELD_EX32(tb_flags, TB_FLAGS, XL); 1287 ctx->address_xl = FIELD_EX32(tb_flags, TB_FLAGS, AXL); 1288 ctx->cs = cs; 1289 if (get_xl(ctx) == MXL_RV32) { 1290 ctx->addr_xl = 32; 1291 ctx->addr_signed = false; 1292 } else { 1293 int pm_pmm = FIELD_EX32(tb_flags, TB_FLAGS, PM_PMM); 1294 ctx->addr_xl = 64 - riscv_pm_get_pmlen(pm_pmm); 1295 ctx->addr_signed = FIELD_EX32(tb_flags, TB_FLAGS, PM_SIGNEXTEND); 1296 } 1297 ctx->ztso = cpu->cfg.ext_ztso; 1298 ctx->itrigger = FIELD_EX32(tb_flags, TB_FLAGS, ITRIGGER); 1299 ctx->bcfi_enabled = FIELD_EX32(tb_flags, TB_FLAGS, BCFI_ENABLED); 1300 ctx->fcfi_lp_expected = FIELD_EX32(tb_flags, TB_FLAGS, FCFI_LP_EXPECTED); 1301 ctx->fcfi_enabled = FIELD_EX32(tb_flags, TB_FLAGS, FCFI_ENABLED); 1302 ctx->zero = tcg_constant_tl(0); 1303 ctx->virt_inst_excp = false; 1304 ctx->decoders = cpu->decoders; 1305 } 1306 1307 static void riscv_tr_tb_start(DisasContextBase *db, CPUState *cpu) 1308 { 1309 } 1310 1311 static void riscv_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu) 1312 { 1313 DisasContext *ctx = container_of(dcbase, DisasContext, base); 1314 target_ulong pc_next = ctx->base.pc_next; 1315 1316 if (tb_cflags(dcbase->tb) & CF_PCREL) { 1317 pc_next &= ~TARGET_PAGE_MASK; 1318 } 1319 1320 tcg_gen_insn_start(pc_next, 0, 0); 1321 ctx->insn_start_updated = false; 1322 } 1323 1324 static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu) 1325 { 1326 DisasContext *ctx = container_of(dcbase, DisasContext, base); 1327 CPURISCVState *env = cpu_env(cpu); 1328 uint16_t opcode16 = translator_lduw(env, &ctx->base, ctx->base.pc_next); 1329 1330 ctx->ol = ctx->xl; 1331 decode_opc(env, ctx, opcode16); 1332 ctx->base.pc_next += ctx->cur_insn_len; 1333 1334 /* 1335 * If 'fcfi_lp_expected' is still true after processing the instruction, 1336 * then we did not see an 'lpad' instruction, and must raise an exception. 1337 * Insert code to raise the exception at the start of the insn; any other 1338 * code the insn may have emitted will be deleted as dead code following 1339 * the noreturn exception 1340 */ 1341 if (ctx->fcfi_lp_expected) { 1342 /* Emit after insn_start, i.e. before the op following insn_start. */ 1343 tcg_ctx->emit_before_op = QTAILQ_NEXT(ctx->base.insn_start, link); 1344 tcg_gen_st_tl(tcg_constant_tl(RISCV_EXCP_SW_CHECK_FCFI_TVAL), 1345 tcg_env, offsetof(CPURISCVState, sw_check_code)); 1346 gen_helper_raise_exception(tcg_env, 1347 tcg_constant_i32(RISCV_EXCP_SW_CHECK)); 1348 tcg_ctx->emit_before_op = NULL; 1349 ctx->base.is_jmp = DISAS_NORETURN; 1350 } 1351 1352 /* Only the first insn within a TB is allowed to cross a page boundary. */ 1353 if (ctx->base.is_jmp == DISAS_NEXT) { 1354 if (ctx->itrigger || !translator_is_same_page(&ctx->base, ctx->base.pc_next)) { 1355 ctx->base.is_jmp = DISAS_TOO_MANY; 1356 } else { 1357 unsigned page_ofs = ctx->base.pc_next & ~TARGET_PAGE_MASK; 1358 1359 if (page_ofs > TARGET_PAGE_SIZE - MAX_INSN_LEN) { 1360 uint16_t next_insn = 1361 translator_lduw(env, &ctx->base, ctx->base.pc_next); 1362 int len = insn_len(next_insn); 1363 1364 if (!translator_is_same_page(&ctx->base, ctx->base.pc_next + len - 1)) { 1365 ctx->base.is_jmp = DISAS_TOO_MANY; 1366 } 1367 } 1368 } 1369 } 1370 } 1371 1372 static void riscv_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu) 1373 { 1374 DisasContext *ctx = container_of(dcbase, DisasContext, base); 1375 1376 switch (ctx->base.is_jmp) { 1377 case DISAS_TOO_MANY: 1378 gen_goto_tb(ctx, 0, 0); 1379 break; 1380 case DISAS_NORETURN: 1381 break; 1382 default: 1383 g_assert_not_reached(); 1384 } 1385 } 1386 1387 static const TranslatorOps riscv_tr_ops = { 1388 .init_disas_context = riscv_tr_init_disas_context, 1389 .tb_start = riscv_tr_tb_start, 1390 .insn_start = riscv_tr_insn_start, 1391 .translate_insn = riscv_tr_translate_insn, 1392 .tb_stop = riscv_tr_tb_stop, 1393 }; 1394 1395 void riscv_translate_code(CPUState *cs, TranslationBlock *tb, 1396 int *max_insns, vaddr pc, void *host_pc) 1397 { 1398 DisasContext ctx; 1399 1400 translator_loop(cs, tb, max_insns, pc, host_pc, &riscv_tr_ops, &ctx.base); 1401 } 1402 1403 void riscv_translate_init(void) 1404 { 1405 int i; 1406 1407 /* 1408 * cpu_gpr[0] is a placeholder for the zero register. Do not use it. 1409 * Use the gen_set_gpr and get_gpr helper functions when accessing regs, 1410 * unless you specifically block reads/writes to reg 0. 1411 */ 1412 cpu_gpr[0] = NULL; 1413 cpu_gprh[0] = NULL; 1414 1415 for (i = 1; i < 32; i++) { 1416 cpu_gpr[i] = tcg_global_mem_new(tcg_env, 1417 offsetof(CPURISCVState, gpr[i]), riscv_int_regnames[i]); 1418 cpu_gprh[i] = tcg_global_mem_new(tcg_env, 1419 offsetof(CPURISCVState, gprh[i]), riscv_int_regnamesh[i]); 1420 } 1421 1422 for (i = 0; i < 32; i++) { 1423 cpu_fpr[i] = tcg_global_mem_new_i64(tcg_env, 1424 offsetof(CPURISCVState, fpr[i]), riscv_fpr_regnames[i]); 1425 } 1426 1427 cpu_pc = tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, pc), "pc"); 1428 cpu_vl = tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, vl), "vl"); 1429 cpu_vstart = tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, vstart), 1430 "vstart"); 1431 load_res = tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, load_res), 1432 "load_res"); 1433 load_val = tcg_global_mem_new(tcg_env, offsetof(CPURISCVState, load_val), 1434 "load_val"); 1435 } 1436