1 /* 2 * riscv TCG cpu class initialization 3 * 4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu 5 * Copyright (c) 2017-2018 SiFive, Inc. 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms and conditions of the GNU General Public License, 9 * version 2 or later, as published by the Free Software Foundation. 10 * 11 * This program is distributed in the hope it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 * more details. 15 * 16 * You should have received a copy of the GNU General Public License along with 17 * this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "qemu/osdep.h" 21 #include "exec/translation-block.h" 22 #include "tcg-cpu.h" 23 #include "cpu.h" 24 #include "exec/target_page.h" 25 #include "internals.h" 26 #include "pmu.h" 27 #include "time_helper.h" 28 #include "qapi/error.h" 29 #include "qapi/visitor.h" 30 #include "qemu/accel.h" 31 #include "qemu/error-report.h" 32 #include "qemu/log.h" 33 #include "accel/accel-cpu-target.h" 34 #include "accel/tcg/cpu-ops.h" 35 #include "tcg/tcg.h" 36 #ifndef CONFIG_USER_ONLY 37 #include "hw/boards.h" 38 #include "system/tcg.h" 39 #include "exec/icount.h" 40 #endif 41 42 /* Hash that stores user set extensions */ 43 static GHashTable *multi_ext_user_opts; 44 static GHashTable *misa_ext_user_opts; 45 46 static GHashTable *multi_ext_implied_rules; 47 static GHashTable *misa_ext_implied_rules; 48 49 static bool cpu_cfg_ext_is_user_set(uint32_t ext_offset) 50 { 51 return g_hash_table_contains(multi_ext_user_opts, 52 GUINT_TO_POINTER(ext_offset)); 53 } 54 55 static bool cpu_misa_ext_is_user_set(uint32_t misa_bit) 56 { 57 return g_hash_table_contains(misa_ext_user_opts, 58 GUINT_TO_POINTER(misa_bit)); 59 } 60 61 static void cpu_cfg_ext_add_user_opt(uint32_t ext_offset, bool value) 62 { 63 g_hash_table_insert(multi_ext_user_opts, GUINT_TO_POINTER(ext_offset), 64 (gpointer)value); 65 } 66 67 static void cpu_misa_ext_add_user_opt(uint32_t bit, bool value) 68 { 69 g_hash_table_insert(misa_ext_user_opts, GUINT_TO_POINTER(bit), 70 (gpointer)value); 71 } 72 73 static void riscv_cpu_write_misa_bit(RISCVCPU *cpu, uint32_t bit, 74 bool enabled) 75 { 76 CPURISCVState *env = &cpu->env; 77 78 if (enabled) { 79 env->misa_ext |= bit; 80 env->misa_ext_mask |= bit; 81 } else { 82 env->misa_ext &= ~bit; 83 env->misa_ext_mask &= ~bit; 84 } 85 } 86 87 static const char *cpu_priv_ver_to_str(int priv_ver) 88 { 89 const char *priv_spec_str = priv_spec_to_str(priv_ver); 90 91 g_assert(priv_spec_str); 92 93 return priv_spec_str; 94 } 95 96 static int riscv_cpu_mmu_index(CPUState *cs, bool ifetch) 97 { 98 return riscv_env_mmu_index(cpu_env(cs), ifetch); 99 } 100 101 static TCGTBCPUState riscv_get_tb_cpu_state(CPUState *cs) 102 { 103 CPURISCVState *env = cpu_env(cs); 104 RISCVCPU *cpu = env_archcpu(env); 105 RISCVExtStatus fs, vs; 106 uint32_t flags = 0; 107 bool pm_signext = riscv_cpu_virt_mem_enabled(env); 108 109 if (cpu->cfg.ext_zve32x) { 110 /* 111 * If env->vl equals to VLMAX, we can use generic vector operation 112 * expanders (GVEC) to accerlate the vector operations. 113 * However, as LMUL could be a fractional number. The maximum 114 * vector size can be operated might be less than 8 bytes, 115 * which is not supported by GVEC. So we set vl_eq_vlmax flag to true 116 * only when maxsz >= 8 bytes. 117 */ 118 119 /* lmul encoded as in DisasContext::lmul */ 120 int8_t lmul = sextract32(FIELD_EX64(env->vtype, VTYPE, VLMUL), 0, 3); 121 uint32_t vsew = FIELD_EX64(env->vtype, VTYPE, VSEW); 122 uint32_t vlmax = vext_get_vlmax(cpu->cfg.vlenb, vsew, lmul); 123 uint32_t maxsz = vlmax << vsew; 124 bool vl_eq_vlmax = (env->vstart == 0) && (vlmax == env->vl) && 125 (maxsz >= 8); 126 flags = FIELD_DP32(flags, TB_FLAGS, VILL, env->vill); 127 flags = FIELD_DP32(flags, TB_FLAGS, SEW, vsew); 128 flags = FIELD_DP32(flags, TB_FLAGS, LMUL, 129 FIELD_EX64(env->vtype, VTYPE, VLMUL)); 130 flags = FIELD_DP32(flags, TB_FLAGS, VL_EQ_VLMAX, vl_eq_vlmax); 131 flags = FIELD_DP32(flags, TB_FLAGS, VTA, 132 FIELD_EX64(env->vtype, VTYPE, VTA)); 133 flags = FIELD_DP32(flags, TB_FLAGS, VMA, 134 FIELD_EX64(env->vtype, VTYPE, VMA)); 135 flags = FIELD_DP32(flags, TB_FLAGS, VSTART_EQ_ZERO, env->vstart == 0); 136 } else { 137 flags = FIELD_DP32(flags, TB_FLAGS, VILL, 1); 138 } 139 140 if (cpu_get_fcfien(env)) { 141 /* 142 * For Forward CFI, only the expectation of a lpad at 143 * the start of the block is tracked via env->elp. env->elp 144 * is turned on during jalr translation. 145 */ 146 flags = FIELD_DP32(flags, TB_FLAGS, FCFI_LP_EXPECTED, env->elp); 147 flags = FIELD_DP32(flags, TB_FLAGS, FCFI_ENABLED, 1); 148 } 149 150 if (cpu_get_bcfien(env)) { 151 flags = FIELD_DP32(flags, TB_FLAGS, BCFI_ENABLED, 1); 152 } 153 154 #ifdef CONFIG_USER_ONLY 155 fs = EXT_STATUS_DIRTY; 156 vs = EXT_STATUS_DIRTY; 157 #else 158 flags = FIELD_DP32(flags, TB_FLAGS, PRIV, env->priv); 159 160 flags |= riscv_env_mmu_index(env, 0); 161 fs = get_field(env->mstatus, MSTATUS_FS); 162 vs = get_field(env->mstatus, MSTATUS_VS); 163 164 if (env->virt_enabled) { 165 flags = FIELD_DP32(flags, TB_FLAGS, VIRT_ENABLED, 1); 166 /* 167 * Merge DISABLED and !DIRTY states using MIN. 168 * We will set both fields when dirtying. 169 */ 170 fs = MIN(fs, get_field(env->mstatus_hs, MSTATUS_FS)); 171 vs = MIN(vs, get_field(env->mstatus_hs, MSTATUS_VS)); 172 } 173 174 /* With Zfinx, floating point is enabled/disabled by Smstateen. */ 175 if (!riscv_has_ext(env, RVF)) { 176 fs = (smstateen_acc_ok(env, 0, SMSTATEEN0_FCSR) == RISCV_EXCP_NONE) 177 ? EXT_STATUS_DIRTY : EXT_STATUS_DISABLED; 178 } 179 180 if (cpu->cfg.debug && !icount_enabled()) { 181 flags = FIELD_DP32(flags, TB_FLAGS, ITRIGGER, env->itrigger_enabled); 182 } 183 #endif 184 185 flags = FIELD_DP32(flags, TB_FLAGS, FS, fs); 186 flags = FIELD_DP32(flags, TB_FLAGS, VS, vs); 187 flags = FIELD_DP32(flags, TB_FLAGS, XL, env->xl); 188 flags = FIELD_DP32(flags, TB_FLAGS, AXL, cpu_address_xl(env)); 189 flags = FIELD_DP32(flags, TB_FLAGS, PM_PMM, riscv_pm_get_pmm(env)); 190 flags = FIELD_DP32(flags, TB_FLAGS, PM_SIGNEXTEND, pm_signext); 191 192 return (TCGTBCPUState){ 193 .pc = env->xl == MXL_RV32 ? env->pc & UINT32_MAX : env->pc, 194 .flags = flags 195 }; 196 } 197 198 static void riscv_cpu_synchronize_from_tb(CPUState *cs, 199 const TranslationBlock *tb) 200 { 201 if (!(tb_cflags(tb) & CF_PCREL)) { 202 RISCVCPU *cpu = RISCV_CPU(cs); 203 CPURISCVState *env = &cpu->env; 204 RISCVMXL xl = FIELD_EX32(tb->flags, TB_FLAGS, XL); 205 206 tcg_debug_assert(!tcg_cflags_has(cs, CF_PCREL)); 207 208 if (xl == MXL_RV32) { 209 env->pc = (int32_t) tb->pc; 210 } else { 211 env->pc = tb->pc; 212 } 213 } 214 } 215 216 static void riscv_restore_state_to_opc(CPUState *cs, 217 const TranslationBlock *tb, 218 const uint64_t *data) 219 { 220 RISCVCPU *cpu = RISCV_CPU(cs); 221 CPURISCVState *env = &cpu->env; 222 RISCVMXL xl = FIELD_EX32(tb->flags, TB_FLAGS, XL); 223 target_ulong pc; 224 225 if (tb_cflags(tb) & CF_PCREL) { 226 pc = (env->pc & TARGET_PAGE_MASK) | data[0]; 227 } else { 228 pc = data[0]; 229 } 230 231 if (xl == MXL_RV32) { 232 env->pc = (int32_t)pc; 233 } else { 234 env->pc = pc; 235 } 236 env->bins = data[1]; 237 env->excp_uw2 = data[2]; 238 } 239 240 #ifndef CONFIG_USER_ONLY 241 static vaddr riscv_pointer_wrap(CPUState *cs, int mmu_idx, 242 vaddr result, vaddr base) 243 { 244 CPURISCVState *env = cpu_env(cs); 245 uint32_t pm_len; 246 bool pm_signext; 247 248 if (cpu_address_xl(env) == MXL_RV32) { 249 return (uint32_t)result; 250 } 251 252 pm_len = riscv_pm_get_pmlen(riscv_pm_get_pmm(env)); 253 if (pm_len == 0) { 254 return result; 255 } 256 257 pm_signext = riscv_cpu_virt_mem_enabled(env); 258 if (pm_signext) { 259 return sextract64(result, 0, 64 - pm_len); 260 } 261 return extract64(result, 0, 64 - pm_len); 262 } 263 #endif 264 265 const TCGCPUOps riscv_tcg_ops = { 266 .mttcg_supported = true, 267 .guest_default_memory_order = 0, 268 269 .initialize = riscv_translate_init, 270 .translate_code = riscv_translate_code, 271 .get_tb_cpu_state = riscv_get_tb_cpu_state, 272 .synchronize_from_tb = riscv_cpu_synchronize_from_tb, 273 .restore_state_to_opc = riscv_restore_state_to_opc, 274 .mmu_index = riscv_cpu_mmu_index, 275 276 #ifndef CONFIG_USER_ONLY 277 .tlb_fill = riscv_cpu_tlb_fill, 278 .pointer_wrap = riscv_pointer_wrap, 279 .cpu_exec_interrupt = riscv_cpu_exec_interrupt, 280 .cpu_exec_halt = riscv_cpu_has_work, 281 .cpu_exec_reset = cpu_reset, 282 .do_interrupt = riscv_cpu_do_interrupt, 283 .do_transaction_failed = riscv_cpu_do_transaction_failed, 284 .do_unaligned_access = riscv_cpu_do_unaligned_access, 285 .debug_excp_handler = riscv_cpu_debug_excp_handler, 286 .debug_check_breakpoint = riscv_cpu_debug_check_breakpoint, 287 .debug_check_watchpoint = riscv_cpu_debug_check_watchpoint, 288 #endif /* !CONFIG_USER_ONLY */ 289 }; 290 291 static int cpu_cfg_ext_get_min_version(uint32_t ext_offset) 292 { 293 const RISCVIsaExtData *edata; 294 295 for (edata = isa_edata_arr; edata && edata->name; edata++) { 296 if (edata->ext_enable_offset != ext_offset) { 297 continue; 298 } 299 300 return edata->min_version; 301 } 302 303 g_assert_not_reached(); 304 } 305 306 static const char *cpu_cfg_ext_get_name(uint32_t ext_offset) 307 { 308 const RISCVCPUMultiExtConfig *feat; 309 const RISCVIsaExtData *edata; 310 311 for (edata = isa_edata_arr; edata->name != NULL; edata++) { 312 if (edata->ext_enable_offset == ext_offset) { 313 return edata->name; 314 } 315 } 316 317 for (feat = riscv_cpu_named_features; feat->name != NULL; feat++) { 318 if (feat->offset == ext_offset) { 319 return feat->name; 320 } 321 } 322 323 g_assert_not_reached(); 324 } 325 326 static bool cpu_cfg_offset_is_named_feat(uint32_t ext_offset) 327 { 328 const RISCVCPUMultiExtConfig *feat; 329 330 for (feat = riscv_cpu_named_features; feat->name != NULL; feat++) { 331 if (feat->offset == ext_offset) { 332 return true; 333 } 334 } 335 336 return false; 337 } 338 339 static void riscv_cpu_enable_named_feat(RISCVCPU *cpu, uint32_t feat_offset) 340 { 341 /* 342 * All other named features are already enabled 343 * in riscv_tcg_cpu_instance_init(). 344 */ 345 switch (feat_offset) { 346 case CPU_CFG_OFFSET(ext_zic64b): 347 cpu->cfg.cbom_blocksize = 64; 348 cpu->cfg.cbop_blocksize = 64; 349 cpu->cfg.cboz_blocksize = 64; 350 break; 351 case CPU_CFG_OFFSET(ext_sha): 352 if (!cpu_misa_ext_is_user_set(RVH)) { 353 riscv_cpu_write_misa_bit(cpu, RVH, true); 354 } 355 /* fallthrough */ 356 case CPU_CFG_OFFSET(ext_ssstateen): 357 cpu->cfg.ext_smstateen = true; 358 break; 359 } 360 } 361 362 static void cpu_bump_multi_ext_priv_ver(CPURISCVState *env, 363 uint32_t ext_offset) 364 { 365 int ext_priv_ver; 366 367 if (env->priv_ver == PRIV_VERSION_LATEST) { 368 return; 369 } 370 371 ext_priv_ver = cpu_cfg_ext_get_min_version(ext_offset); 372 373 if (env->priv_ver < ext_priv_ver) { 374 /* 375 * Note: the 'priv_spec' command line option, if present, 376 * will take precedence over this priv_ver bump. 377 */ 378 env->priv_ver = ext_priv_ver; 379 } 380 } 381 382 static void cpu_cfg_ext_auto_update(RISCVCPU *cpu, uint32_t ext_offset, 383 bool value) 384 { 385 CPURISCVState *env = &cpu->env; 386 bool prev_val = isa_ext_is_enabled(cpu, ext_offset); 387 int min_version; 388 389 if (prev_val == value) { 390 return; 391 } 392 393 if (cpu_cfg_ext_is_user_set(ext_offset)) { 394 return; 395 } 396 397 if (value && env->priv_ver != PRIV_VERSION_LATEST) { 398 /* Do not enable it if priv_ver is older than min_version */ 399 min_version = cpu_cfg_ext_get_min_version(ext_offset); 400 if (env->priv_ver < min_version) { 401 return; 402 } 403 } 404 405 isa_ext_update_enabled(cpu, ext_offset, value); 406 } 407 408 static void riscv_cpu_validate_misa_priv(CPURISCVState *env, Error **errp) 409 { 410 if (riscv_has_ext(env, RVH) && env->priv_ver < PRIV_VERSION_1_12_0) { 411 error_setg(errp, "H extension requires priv spec 1.12.0"); 412 return; 413 } 414 } 415 416 static void riscv_cpu_validate_v(CPURISCVState *env, RISCVCPUConfig *cfg, 417 Error **errp) 418 { 419 uint32_t vlen = cfg->vlenb << 3; 420 421 if (vlen > RV_VLEN_MAX || vlen < 128) { 422 error_setg(errp, 423 "Vector extension implementation only supports VLEN " 424 "in the range [128, %d]", RV_VLEN_MAX); 425 return; 426 } 427 428 if (cfg->elen > 64 || cfg->elen < 8) { 429 error_setg(errp, 430 "Vector extension implementation only supports ELEN " 431 "in the range [8, 64]"); 432 return; 433 } 434 } 435 436 static void riscv_cpu_disable_priv_spec_isa_exts(RISCVCPU *cpu) 437 { 438 CPURISCVState *env = &cpu->env; 439 const RISCVIsaExtData *edata; 440 441 /* Force disable extensions if priv spec version does not match */ 442 for (edata = isa_edata_arr; edata && edata->name; edata++) { 443 if (isa_ext_is_enabled(cpu, edata->ext_enable_offset) && 444 (env->priv_ver < edata->min_version)) { 445 /* 446 * These two extensions are always enabled as they were supported 447 * by QEMU before they were added as extensions in the ISA. 448 */ 449 if (!strcmp(edata->name, "zicntr") || 450 !strcmp(edata->name, "zihpm")) { 451 continue; 452 } 453 454 /* 455 * cpu.debug = true is marked as 'sdtrig', priv spec 1.12. 456 * Skip this warning since existing CPUs with older priv 457 * spec and debug = true will be impacted. 458 */ 459 if (!strcmp(edata->name, "sdtrig")) { 460 continue; 461 } 462 463 isa_ext_update_enabled(cpu, edata->ext_enable_offset, false); 464 465 /* 466 * Do not show user warnings for named features that users 467 * can't enable/disable in the command line. See commit 468 * 68c9e54bea for more info. 469 */ 470 if (cpu_cfg_offset_is_named_feat(edata->ext_enable_offset)) { 471 continue; 472 } 473 #ifndef CONFIG_USER_ONLY 474 warn_report("disabling %s extension for hart 0x" TARGET_FMT_lx 475 " because privilege spec version does not match", 476 edata->name, env->mhartid); 477 #else 478 warn_report("disabling %s extension because " 479 "privilege spec version does not match", 480 edata->name); 481 #endif 482 } 483 } 484 } 485 486 static void riscv_cpu_update_named_features(RISCVCPU *cpu) 487 { 488 if (cpu->env.priv_ver >= PRIV_VERSION_1_11_0) { 489 cpu->cfg.has_priv_1_11 = true; 490 } 491 492 if (cpu->env.priv_ver >= PRIV_VERSION_1_12_0) { 493 cpu->cfg.has_priv_1_12 = true; 494 } 495 496 if (cpu->env.priv_ver >= PRIV_VERSION_1_13_0) { 497 cpu->cfg.has_priv_1_13 = true; 498 } 499 500 cpu->cfg.ext_zic64b = cpu->cfg.cbom_blocksize == 64 && 501 cpu->cfg.cbop_blocksize == 64 && 502 cpu->cfg.cboz_blocksize == 64; 503 504 cpu->cfg.ext_ssstateen = cpu->cfg.ext_smstateen; 505 506 cpu->cfg.ext_sha = riscv_has_ext(&cpu->env, RVH) && 507 cpu->cfg.ext_ssstateen; 508 509 cpu->cfg.ext_ziccrse = cpu->cfg.has_priv_1_11; 510 } 511 512 static void riscv_cpu_validate_g(RISCVCPU *cpu) 513 { 514 const char *warn_msg = "RVG mandates disabled extension %s"; 515 uint32_t g_misa_bits[] = {RVI, RVM, RVA, RVF, RVD}; 516 bool send_warn = cpu_misa_ext_is_user_set(RVG); 517 518 for (int i = 0; i < ARRAY_SIZE(g_misa_bits); i++) { 519 uint32_t bit = g_misa_bits[i]; 520 521 if (riscv_has_ext(&cpu->env, bit)) { 522 continue; 523 } 524 525 if (!cpu_misa_ext_is_user_set(bit)) { 526 riscv_cpu_write_misa_bit(cpu, bit, true); 527 continue; 528 } 529 530 if (send_warn) { 531 warn_report(warn_msg, riscv_get_misa_ext_name(bit)); 532 } 533 } 534 535 if (!cpu->cfg.ext_zicsr) { 536 if (!cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_zicsr))) { 537 cpu->cfg.ext_zicsr = true; 538 } else if (send_warn) { 539 warn_report(warn_msg, "zicsr"); 540 } 541 } 542 543 if (!cpu->cfg.ext_zifencei) { 544 if (!cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_zifencei))) { 545 cpu->cfg.ext_zifencei = true; 546 } else if (send_warn) { 547 warn_report(warn_msg, "zifencei"); 548 } 549 } 550 } 551 552 static void riscv_cpu_validate_b(RISCVCPU *cpu) 553 { 554 const char *warn_msg = "RVB mandates disabled extension %s"; 555 556 if (!cpu->cfg.ext_zba) { 557 if (!cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_zba))) { 558 cpu->cfg.ext_zba = true; 559 } else { 560 warn_report(warn_msg, "zba"); 561 } 562 } 563 564 if (!cpu->cfg.ext_zbb) { 565 if (!cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_zbb))) { 566 cpu->cfg.ext_zbb = true; 567 } else { 568 warn_report(warn_msg, "zbb"); 569 } 570 } 571 572 if (!cpu->cfg.ext_zbs) { 573 if (!cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_zbs))) { 574 cpu->cfg.ext_zbs = true; 575 } else { 576 warn_report(warn_msg, "zbs"); 577 } 578 } 579 } 580 581 /* 582 * Check consistency between chosen extensions while setting 583 * cpu->cfg accordingly. 584 */ 585 void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp) 586 { 587 RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cpu); 588 CPURISCVState *env = &cpu->env; 589 Error *local_err = NULL; 590 591 if (riscv_has_ext(env, RVG)) { 592 riscv_cpu_validate_g(cpu); 593 } 594 595 if (riscv_has_ext(env, RVB)) { 596 riscv_cpu_validate_b(cpu); 597 } 598 599 if (riscv_has_ext(env, RVI) && riscv_has_ext(env, RVE)) { 600 error_setg(errp, 601 "I and E extensions are incompatible"); 602 return; 603 } 604 605 if (!riscv_has_ext(env, RVI) && !riscv_has_ext(env, RVE)) { 606 error_setg(errp, 607 "Either I or E extension must be set"); 608 return; 609 } 610 611 if (riscv_has_ext(env, RVS) && !riscv_has_ext(env, RVU)) { 612 error_setg(errp, 613 "Setting S extension without U extension is illegal"); 614 return; 615 } 616 617 if (riscv_has_ext(env, RVH) && !riscv_has_ext(env, RVI)) { 618 error_setg(errp, 619 "H depends on an I base integer ISA with 32 x registers"); 620 return; 621 } 622 623 if (riscv_has_ext(env, RVH) && !riscv_has_ext(env, RVS)) { 624 error_setg(errp, "H extension implicitly requires S-mode"); 625 return; 626 } 627 628 if (riscv_has_ext(env, RVF) && !cpu->cfg.ext_zicsr) { 629 error_setg(errp, "F extension requires Zicsr"); 630 return; 631 } 632 633 if ((cpu->cfg.ext_zacas) && !riscv_has_ext(env, RVA)) { 634 error_setg(errp, "Zacas extension requires A extension"); 635 return; 636 } 637 638 if ((cpu->cfg.ext_zawrs) && !riscv_has_ext(env, RVA)) { 639 error_setg(errp, "Zawrs extension requires A extension"); 640 return; 641 } 642 643 if (cpu->cfg.ext_zfa && !riscv_has_ext(env, RVF)) { 644 error_setg(errp, "Zfa extension requires F extension"); 645 return; 646 } 647 648 if (cpu->cfg.ext_zfhmin && !riscv_has_ext(env, RVF)) { 649 error_setg(errp, "Zfh/Zfhmin extensions require F extension"); 650 return; 651 } 652 653 if (cpu->cfg.ext_zfbfmin && !riscv_has_ext(env, RVF)) { 654 error_setg(errp, "Zfbfmin extension depends on F extension"); 655 return; 656 } 657 658 if (riscv_has_ext(env, RVD) && !riscv_has_ext(env, RVF)) { 659 error_setg(errp, "D extension requires F extension"); 660 return; 661 } 662 663 if (riscv_has_ext(env, RVV)) { 664 riscv_cpu_validate_v(env, &cpu->cfg, &local_err); 665 if (local_err != NULL) { 666 error_propagate(errp, local_err); 667 return; 668 } 669 } 670 671 /* The Zve64d extension depends on the Zve64f extension */ 672 if (cpu->cfg.ext_zve64d) { 673 if (!riscv_has_ext(env, RVD)) { 674 error_setg(errp, "Zve64d/V extensions require D extension"); 675 return; 676 } 677 } 678 679 /* The Zve32f extension depends on the Zve32x extension */ 680 if (cpu->cfg.ext_zve32f) { 681 if (!riscv_has_ext(env, RVF)) { 682 error_setg(errp, "Zve32f/Zve64f extensions require F extension"); 683 return; 684 } 685 } 686 687 if (cpu->cfg.ext_zvfhmin && !cpu->cfg.ext_zve32f) { 688 error_setg(errp, "Zvfh/Zvfhmin extensions require Zve32f extension"); 689 return; 690 } 691 692 if (cpu->cfg.ext_zvfh && !cpu->cfg.ext_zfhmin) { 693 error_setg(errp, "Zvfh extensions requires Zfhmin extension"); 694 return; 695 } 696 697 if (cpu->cfg.ext_zvfbfmin && !cpu->cfg.ext_zve32f) { 698 error_setg(errp, "Zvfbfmin extension depends on Zve32f extension"); 699 return; 700 } 701 702 if (cpu->cfg.ext_zvfbfwma && !cpu->cfg.ext_zvfbfmin) { 703 error_setg(errp, "Zvfbfwma extension depends on Zvfbfmin extension"); 704 return; 705 } 706 707 if ((cpu->cfg.ext_zdinx || cpu->cfg.ext_zhinxmin) && !cpu->cfg.ext_zfinx) { 708 error_setg(errp, "Zdinx/Zhinx/Zhinxmin extensions require Zfinx"); 709 return; 710 } 711 712 if (cpu->cfg.ext_zfinx) { 713 if (!cpu->cfg.ext_zicsr) { 714 error_setg(errp, "Zfinx extension requires Zicsr"); 715 return; 716 } 717 if (riscv_has_ext(env, RVF)) { 718 error_setg(errp, 719 "Zfinx cannot be supported together with F extension"); 720 return; 721 } 722 } 723 724 if (cpu->cfg.ext_zcmop && !cpu->cfg.ext_zca) { 725 error_setg(errp, "Zcmop extensions require Zca"); 726 return; 727 } 728 729 if (mcc->def->misa_mxl_max != MXL_RV32 && cpu->cfg.ext_zcf) { 730 error_setg(errp, "Zcf extension is only relevant to RV32"); 731 return; 732 } 733 734 if (!riscv_has_ext(env, RVF) && cpu->cfg.ext_zcf) { 735 error_setg(errp, "Zcf extension requires F extension"); 736 return; 737 } 738 739 if (!riscv_has_ext(env, RVD) && cpu->cfg.ext_zcd) { 740 error_setg(errp, "Zcd extension requires D extension"); 741 return; 742 } 743 744 if ((cpu->cfg.ext_zcf || cpu->cfg.ext_zcd || cpu->cfg.ext_zcb || 745 cpu->cfg.ext_zcmp || cpu->cfg.ext_zcmt) && !cpu->cfg.ext_zca) { 746 error_setg(errp, "Zcf/Zcd/Zcb/Zcmp/Zcmt extensions require Zca " 747 "extension"); 748 return; 749 } 750 751 if (cpu->cfg.ext_zcd && (cpu->cfg.ext_zcmp || cpu->cfg.ext_zcmt)) { 752 error_setg(errp, "Zcmp/Zcmt extensions are incompatible with " 753 "Zcd extension"); 754 return; 755 } 756 757 if (cpu->cfg.ext_zcmt && !cpu->cfg.ext_zicsr) { 758 error_setg(errp, "Zcmt extension requires Zicsr extension"); 759 return; 760 } 761 762 if ((cpu->cfg.ext_zvbb || cpu->cfg.ext_zvkb || cpu->cfg.ext_zvkg || 763 cpu->cfg.ext_zvkned || cpu->cfg.ext_zvknha || cpu->cfg.ext_zvksed || 764 cpu->cfg.ext_zvksh) && !cpu->cfg.ext_zve32x) { 765 error_setg(errp, 766 "Vector crypto extensions require V or Zve* extensions"); 767 return; 768 } 769 770 if ((cpu->cfg.ext_zvbc || cpu->cfg.ext_zvknhb) && !cpu->cfg.ext_zve64x) { 771 error_setg( 772 errp, 773 "Zvbc and Zvknhb extensions require V or Zve64x extensions"); 774 return; 775 } 776 777 if (cpu->cfg.ext_zicntr && !cpu->cfg.ext_zicsr) { 778 if (cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_zicntr))) { 779 error_setg(errp, "zicntr requires zicsr"); 780 return; 781 } 782 cpu->cfg.ext_zicntr = false; 783 } 784 785 if (cpu->cfg.ext_zihpm && !cpu->cfg.ext_zicsr) { 786 if (cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_zihpm))) { 787 error_setg(errp, "zihpm requires zicsr"); 788 return; 789 } 790 cpu->cfg.ext_zihpm = false; 791 } 792 793 if (cpu->cfg.ext_zicfiss) { 794 if (!cpu->cfg.ext_zicsr) { 795 error_setg(errp, "zicfiss extension requires zicsr extension"); 796 return; 797 } 798 if (!riscv_has_ext(env, RVA)) { 799 error_setg(errp, "zicfiss extension requires A extension"); 800 return; 801 } 802 if (!riscv_has_ext(env, RVS)) { 803 error_setg(errp, "zicfiss extension requires S"); 804 return; 805 } 806 if (!cpu->cfg.ext_zimop) { 807 error_setg(errp, "zicfiss extension requires zimop extension"); 808 return; 809 } 810 if (cpu->cfg.ext_zca && !cpu->cfg.ext_zcmop) { 811 error_setg(errp, "zicfiss with zca requires zcmop extension"); 812 return; 813 } 814 } 815 816 if (!cpu->cfg.ext_zihpm) { 817 cpu->cfg.pmu_mask = 0; 818 cpu->pmu_avail_ctrs = 0; 819 } 820 821 if (cpu->cfg.ext_zicfilp && !cpu->cfg.ext_zicsr) { 822 error_setg(errp, "zicfilp extension requires zicsr extension"); 823 return; 824 } 825 826 if (mcc->def->misa_mxl_max == MXL_RV32 && cpu->cfg.ext_svukte) { 827 error_setg(errp, "svukte is not supported for RV32"); 828 return; 829 } 830 831 if ((cpu->cfg.ext_smctr || cpu->cfg.ext_ssctr) && 832 (!riscv_has_ext(env, RVS) || !cpu->cfg.ext_sscsrind)) { 833 if (cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_smctr)) || 834 cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_ssctr))) { 835 error_setg(errp, "Smctr and Ssctr require S-mode and Sscsrind"); 836 return; 837 } 838 cpu->cfg.ext_smctr = false; 839 cpu->cfg.ext_ssctr = false; 840 } 841 842 /* 843 * Disable isa extensions based on priv spec after we 844 * validated and set everything we need. 845 */ 846 riscv_cpu_disable_priv_spec_isa_exts(cpu); 847 } 848 849 #ifndef CONFIG_USER_ONLY 850 static bool riscv_cpu_validate_profile_satp(RISCVCPU *cpu, 851 RISCVCPUProfile *profile, 852 bool send_warn) 853 { 854 int satp_max = cpu->cfg.max_satp_mode; 855 856 assert(satp_max >= 0); 857 if (profile->satp_mode > satp_max) { 858 if (send_warn) { 859 bool is_32bit = riscv_cpu_is_32bit(cpu); 860 const char *req_satp = satp_mode_str(profile->satp_mode, is_32bit); 861 const char *cur_satp = satp_mode_str(satp_max, is_32bit); 862 863 warn_report("Profile %s requires satp mode %s, " 864 "but satp mode %s was set", profile->name, 865 req_satp, cur_satp); 866 } 867 868 return false; 869 } 870 871 return true; 872 } 873 #endif 874 875 static void riscv_cpu_check_parent_profile(RISCVCPU *cpu, 876 RISCVCPUProfile *profile, 877 RISCVCPUProfile *parent) 878 { 879 if (!profile->present || !parent) { 880 return; 881 } 882 883 profile->present = parent->present; 884 } 885 886 static void riscv_cpu_validate_profile(RISCVCPU *cpu, 887 RISCVCPUProfile *profile) 888 { 889 CPURISCVState *env = &cpu->env; 890 const char *warn_msg = "Profile %s mandates disabled extension %s"; 891 bool send_warn = profile->user_set && profile->enabled; 892 bool profile_impl = true; 893 int i; 894 895 #ifndef CONFIG_USER_ONLY 896 if (profile->satp_mode != RISCV_PROFILE_ATTR_UNUSED) { 897 profile_impl = riscv_cpu_validate_profile_satp(cpu, profile, 898 send_warn); 899 } 900 #endif 901 902 if (profile->priv_spec != RISCV_PROFILE_ATTR_UNUSED && 903 profile->priv_spec > env->priv_ver) { 904 profile_impl = false; 905 906 if (send_warn) { 907 warn_report("Profile %s requires priv spec %s, " 908 "but priv ver %s was set", profile->name, 909 cpu_priv_ver_to_str(profile->priv_spec), 910 cpu_priv_ver_to_str(env->priv_ver)); 911 } 912 } 913 914 for (i = 0; misa_bits[i] != 0; i++) { 915 uint32_t bit = misa_bits[i]; 916 917 if (!(profile->misa_ext & bit)) { 918 continue; 919 } 920 921 if (!riscv_has_ext(&cpu->env, bit)) { 922 profile_impl = false; 923 924 if (send_warn) { 925 warn_report(warn_msg, profile->name, 926 riscv_get_misa_ext_name(bit)); 927 } 928 } 929 } 930 931 for (i = 0; profile->ext_offsets[i] != RISCV_PROFILE_EXT_LIST_END; i++) { 932 int ext_offset = profile->ext_offsets[i]; 933 934 if (!isa_ext_is_enabled(cpu, ext_offset)) { 935 profile_impl = false; 936 937 if (send_warn) { 938 warn_report(warn_msg, profile->name, 939 cpu_cfg_ext_get_name(ext_offset)); 940 } 941 } 942 } 943 944 profile->present = profile_impl; 945 946 riscv_cpu_check_parent_profile(cpu, profile, profile->u_parent); 947 riscv_cpu_check_parent_profile(cpu, profile, profile->s_parent); 948 } 949 950 static void riscv_cpu_validate_profiles(RISCVCPU *cpu) 951 { 952 for (int i = 0; riscv_profiles[i] != NULL; i++) { 953 riscv_cpu_validate_profile(cpu, riscv_profiles[i]); 954 } 955 } 956 957 static void riscv_cpu_init_implied_exts_rules(void) 958 { 959 RISCVCPUImpliedExtsRule *rule; 960 #ifndef CONFIG_USER_ONLY 961 MachineState *ms = MACHINE(qdev_get_machine()); 962 #endif 963 static bool initialized; 964 int i; 965 966 /* Implied rules only need to be initialized once. */ 967 if (initialized) { 968 return; 969 } 970 971 for (i = 0; (rule = riscv_misa_ext_implied_rules[i]); i++) { 972 #ifndef CONFIG_USER_ONLY 973 rule->enabled = bitmap_new(ms->smp.cpus); 974 #endif 975 g_hash_table_insert(misa_ext_implied_rules, 976 GUINT_TO_POINTER(rule->ext), (gpointer)rule); 977 } 978 979 for (i = 0; (rule = riscv_multi_ext_implied_rules[i]); i++) { 980 #ifndef CONFIG_USER_ONLY 981 rule->enabled = bitmap_new(ms->smp.cpus); 982 #endif 983 g_hash_table_insert(multi_ext_implied_rules, 984 GUINT_TO_POINTER(rule->ext), (gpointer)rule); 985 } 986 987 initialized = true; 988 } 989 990 static void cpu_enable_implied_rule(RISCVCPU *cpu, 991 RISCVCPUImpliedExtsRule *rule) 992 { 993 CPURISCVState *env = &cpu->env; 994 RISCVCPUImpliedExtsRule *ir; 995 bool enabled = false; 996 int i; 997 998 #ifndef CONFIG_USER_ONLY 999 enabled = test_bit(cpu->env.mhartid, rule->enabled); 1000 #endif 1001 1002 if (!enabled) { 1003 /* Enable the implied MISAs. */ 1004 if (rule->implied_misa_exts) { 1005 for (i = 0; misa_bits[i] != 0; i++) { 1006 if (rule->implied_misa_exts & misa_bits[i]) { 1007 /* 1008 * If the user disabled the misa_bit do not re-enable it 1009 * and do not apply any implied rules related to it. 1010 */ 1011 if (cpu_misa_ext_is_user_set(misa_bits[i]) && 1012 !(env->misa_ext & misa_bits[i])) { 1013 continue; 1014 } 1015 1016 riscv_cpu_set_misa_ext(env, env->misa_ext | misa_bits[i]); 1017 ir = g_hash_table_lookup(misa_ext_implied_rules, 1018 GUINT_TO_POINTER(misa_bits[i])); 1019 1020 if (ir) { 1021 cpu_enable_implied_rule(cpu, ir); 1022 } 1023 } 1024 } 1025 } 1026 1027 /* Enable the implied extensions. */ 1028 for (i = 0; 1029 rule->implied_multi_exts[i] != RISCV_IMPLIED_EXTS_RULE_END; i++) { 1030 cpu_cfg_ext_auto_update(cpu, rule->implied_multi_exts[i], true); 1031 1032 ir = g_hash_table_lookup(multi_ext_implied_rules, 1033 GUINT_TO_POINTER( 1034 rule->implied_multi_exts[i])); 1035 1036 if (ir) { 1037 cpu_enable_implied_rule(cpu, ir); 1038 } 1039 } 1040 1041 #ifndef CONFIG_USER_ONLY 1042 bitmap_set(rule->enabled, cpu->env.mhartid, 1); 1043 #endif 1044 } 1045 } 1046 1047 /* Zc extension has special implied rules that need to be handled separately. */ 1048 static void cpu_enable_zc_implied_rules(RISCVCPU *cpu) 1049 { 1050 RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cpu); 1051 CPURISCVState *env = &cpu->env; 1052 1053 if (cpu->cfg.ext_zce) { 1054 cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zca), true); 1055 cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zcb), true); 1056 cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zcmp), true); 1057 cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zcmt), true); 1058 1059 if (riscv_has_ext(env, RVF) && mcc->def->misa_mxl_max == MXL_RV32) { 1060 cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zcf), true); 1061 } 1062 } 1063 1064 /* Zca, Zcd and Zcf has a PRIV 1.12.0 restriction */ 1065 if (riscv_has_ext(env, RVC) && env->priv_ver >= PRIV_VERSION_1_12_0) { 1066 cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zca), true); 1067 1068 if (riscv_has_ext(env, RVF) && mcc->def->misa_mxl_max == MXL_RV32) { 1069 cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zcf), true); 1070 } 1071 1072 if (riscv_has_ext(env, RVD)) { 1073 cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zcd), true); 1074 } 1075 } 1076 } 1077 1078 static void riscv_cpu_enable_implied_rules(RISCVCPU *cpu) 1079 { 1080 RISCVCPUImpliedExtsRule *rule; 1081 int i; 1082 1083 /* Enable the implied extensions for Zc. */ 1084 cpu_enable_zc_implied_rules(cpu); 1085 1086 /* Enable the implied MISAs. */ 1087 for (i = 0; (rule = riscv_misa_ext_implied_rules[i]); i++) { 1088 if (riscv_has_ext(&cpu->env, rule->ext)) { 1089 cpu_enable_implied_rule(cpu, rule); 1090 } 1091 } 1092 1093 /* Enable the implied extensions. */ 1094 for (i = 0; (rule = riscv_multi_ext_implied_rules[i]); i++) { 1095 if (isa_ext_is_enabled(cpu, rule->ext)) { 1096 cpu_enable_implied_rule(cpu, rule); 1097 } 1098 } 1099 } 1100 1101 void riscv_tcg_cpu_finalize_features(RISCVCPU *cpu, Error **errp) 1102 { 1103 CPURISCVState *env = &cpu->env; 1104 Error *local_err = NULL; 1105 1106 riscv_cpu_init_implied_exts_rules(); 1107 riscv_cpu_enable_implied_rules(cpu); 1108 1109 riscv_cpu_validate_misa_priv(env, &local_err); 1110 if (local_err != NULL) { 1111 error_propagate(errp, local_err); 1112 return; 1113 } 1114 1115 riscv_cpu_update_named_features(cpu); 1116 riscv_cpu_validate_profiles(cpu); 1117 1118 if (cpu->cfg.ext_smepmp && !cpu->cfg.pmp) { 1119 /* 1120 * Enhanced PMP should only be available 1121 * on harts with PMP support 1122 */ 1123 error_setg(errp, "Invalid configuration: Smepmp requires PMP support"); 1124 return; 1125 } 1126 1127 riscv_cpu_validate_set_extensions(cpu, &local_err); 1128 if (local_err != NULL) { 1129 error_propagate(errp, local_err); 1130 return; 1131 } 1132 #ifndef CONFIG_USER_ONLY 1133 if (cpu->cfg.pmu_mask) { 1134 riscv_pmu_init(cpu, &local_err); 1135 if (local_err != NULL) { 1136 error_propagate(errp, local_err); 1137 return; 1138 } 1139 1140 if (cpu->cfg.ext_sscofpmf) { 1141 cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, 1142 riscv_pmu_timer_cb, cpu); 1143 } 1144 } 1145 #endif 1146 } 1147 1148 void riscv_tcg_cpu_finalize_dynamic_decoder(RISCVCPU *cpu) 1149 { 1150 GPtrArray *dynamic_decoders; 1151 dynamic_decoders = g_ptr_array_sized_new(decoder_table_size); 1152 for (size_t i = 0; i < decoder_table_size; ++i) { 1153 if (decoder_table[i].guard_func && 1154 decoder_table[i].guard_func(&cpu->cfg)) { 1155 g_ptr_array_add(dynamic_decoders, 1156 (gpointer)decoder_table[i].riscv_cpu_decode_fn); 1157 } 1158 } 1159 1160 cpu->decoders = dynamic_decoders; 1161 } 1162 1163 bool riscv_cpu_tcg_compatible(RISCVCPU *cpu) 1164 { 1165 return object_dynamic_cast(OBJECT(cpu), TYPE_RISCV_CPU_HOST) == NULL; 1166 } 1167 1168 static bool riscv_cpu_is_generic(Object *cpu_obj) 1169 { 1170 return object_dynamic_cast(cpu_obj, TYPE_RISCV_DYNAMIC_CPU) != NULL; 1171 } 1172 1173 static void riscv_cpu_set_profile(RISCVCPU *cpu, 1174 RISCVCPUProfile *profile, 1175 bool enabled) 1176 { 1177 int i, ext_offset; 1178 1179 if (profile->u_parent != NULL) { 1180 riscv_cpu_set_profile(cpu, profile->u_parent, enabled); 1181 } 1182 1183 if (profile->s_parent != NULL) { 1184 riscv_cpu_set_profile(cpu, profile->s_parent, enabled); 1185 } 1186 1187 profile->enabled = enabled; 1188 1189 if (profile->enabled) { 1190 cpu->env.priv_ver = profile->priv_spec; 1191 1192 #ifndef CONFIG_USER_ONLY 1193 if (profile->satp_mode != RISCV_PROFILE_ATTR_UNUSED) { 1194 object_property_set_bool(OBJECT(cpu), "mmu", true, NULL); 1195 const char *satp_prop = satp_mode_str(profile->satp_mode, 1196 riscv_cpu_is_32bit(cpu)); 1197 object_property_set_bool(OBJECT(cpu), satp_prop, true, NULL); 1198 } 1199 #endif 1200 } 1201 1202 for (i = 0; misa_bits[i] != 0; i++) { 1203 uint32_t bit = misa_bits[i]; 1204 1205 if (!(profile->misa_ext & bit)) { 1206 continue; 1207 } 1208 1209 if (bit == RVI && !profile->enabled) { 1210 /* 1211 * Disabling profiles will not disable the base 1212 * ISA RV64I. 1213 */ 1214 continue; 1215 } 1216 1217 cpu_misa_ext_add_user_opt(bit, profile->enabled); 1218 riscv_cpu_write_misa_bit(cpu, bit, profile->enabled); 1219 } 1220 1221 for (i = 0; profile->ext_offsets[i] != RISCV_PROFILE_EXT_LIST_END; i++) { 1222 ext_offset = profile->ext_offsets[i]; 1223 1224 if (profile->enabled) { 1225 if (cpu_cfg_offset_is_named_feat(ext_offset)) { 1226 riscv_cpu_enable_named_feat(cpu, ext_offset); 1227 } 1228 1229 cpu_bump_multi_ext_priv_ver(&cpu->env, ext_offset); 1230 } 1231 1232 cpu_cfg_ext_add_user_opt(ext_offset, profile->enabled); 1233 isa_ext_update_enabled(cpu, ext_offset, profile->enabled); 1234 } 1235 } 1236 1237 /* 1238 * We'll get here via the following path: 1239 * 1240 * riscv_cpu_realize() 1241 * -> cpu_exec_realizefn() 1242 * -> tcg_cpu_realize() (via accel_cpu_common_realize()) 1243 */ 1244 static bool riscv_tcg_cpu_realize(CPUState *cs, Error **errp) 1245 { 1246 RISCVCPU *cpu = RISCV_CPU(cs); 1247 1248 if (!riscv_cpu_tcg_compatible(cpu)) { 1249 g_autofree char *name = riscv_cpu_get_name(cpu); 1250 error_setg(errp, "'%s' CPU is not compatible with TCG acceleration", 1251 name); 1252 return false; 1253 } 1254 1255 #ifndef CONFIG_USER_ONLY 1256 RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cpu); 1257 1258 if (mcc->def->misa_mxl_max >= MXL_RV128 && qemu_tcg_mttcg_enabled()) { 1259 /* Missing 128-bit aligned atomics */ 1260 error_setg(errp, 1261 "128-bit RISC-V currently does not work with Multi " 1262 "Threaded TCG. Please use: -accel tcg,thread=single"); 1263 return false; 1264 } 1265 1266 CPURISCVState *env = &cpu->env; 1267 1268 tcg_cflags_set(CPU(cs), CF_PCREL); 1269 1270 if (cpu->cfg.ext_sstc) { 1271 riscv_timer_init(cpu); 1272 } 1273 1274 /* With H-Ext, VSSIP, VSTIP, VSEIP and SGEIP are hardwired to one. */ 1275 if (riscv_has_ext(env, RVH)) { 1276 env->mideleg = MIP_VSSIP | MIP_VSTIP | MIP_VSEIP | MIP_SGEIP; 1277 } 1278 #endif 1279 1280 return true; 1281 } 1282 1283 typedef struct RISCVCPUMisaExtConfig { 1284 target_ulong misa_bit; 1285 bool enabled; 1286 } RISCVCPUMisaExtConfig; 1287 1288 static void cpu_set_misa_ext_cfg(Object *obj, Visitor *v, const char *name, 1289 void *opaque, Error **errp) 1290 { 1291 const RISCVCPUMisaExtConfig *misa_ext_cfg = opaque; 1292 target_ulong misa_bit = misa_ext_cfg->misa_bit; 1293 RISCVCPU *cpu = RISCV_CPU(obj); 1294 CPURISCVState *env = &cpu->env; 1295 bool vendor_cpu = riscv_cpu_is_vendor(obj); 1296 bool prev_val, value; 1297 1298 if (!visit_type_bool(v, name, &value, errp)) { 1299 return; 1300 } 1301 1302 cpu_misa_ext_add_user_opt(misa_bit, value); 1303 1304 prev_val = env->misa_ext & misa_bit; 1305 1306 if (value == prev_val) { 1307 return; 1308 } 1309 1310 if (value) { 1311 if (vendor_cpu) { 1312 g_autofree char *cpuname = riscv_cpu_get_name(cpu); 1313 error_setg(errp, "'%s' CPU does not allow enabling extensions", 1314 cpuname); 1315 return; 1316 } 1317 1318 if (misa_bit == RVH && env->priv_ver < PRIV_VERSION_1_12_0) { 1319 /* 1320 * Note: the 'priv_spec' command line option, if present, 1321 * will take precedence over this priv_ver bump. 1322 */ 1323 env->priv_ver = PRIV_VERSION_1_12_0; 1324 } 1325 } 1326 1327 riscv_cpu_write_misa_bit(cpu, misa_bit, value); 1328 } 1329 1330 static void cpu_get_misa_ext_cfg(Object *obj, Visitor *v, const char *name, 1331 void *opaque, Error **errp) 1332 { 1333 const RISCVCPUMisaExtConfig *misa_ext_cfg = opaque; 1334 target_ulong misa_bit = misa_ext_cfg->misa_bit; 1335 RISCVCPU *cpu = RISCV_CPU(obj); 1336 CPURISCVState *env = &cpu->env; 1337 bool value; 1338 1339 value = env->misa_ext & misa_bit; 1340 1341 visit_type_bool(v, name, &value, errp); 1342 } 1343 1344 #define MISA_CFG(_bit, _enabled) \ 1345 {.misa_bit = _bit, .enabled = _enabled} 1346 1347 static const RISCVCPUMisaExtConfig misa_ext_cfgs[] = { 1348 MISA_CFG(RVA, true), 1349 MISA_CFG(RVC, true), 1350 MISA_CFG(RVD, true), 1351 MISA_CFG(RVF, true), 1352 MISA_CFG(RVI, true), 1353 MISA_CFG(RVE, false), 1354 MISA_CFG(RVM, true), 1355 MISA_CFG(RVS, true), 1356 MISA_CFG(RVU, true), 1357 MISA_CFG(RVH, true), 1358 MISA_CFG(RVV, false), 1359 MISA_CFG(RVG, false), 1360 MISA_CFG(RVB, false), 1361 }; 1362 1363 /* 1364 * We do not support user choice tracking for MISA 1365 * extensions yet because, so far, we do not silently 1366 * change MISA bits during realize() (RVG enables MISA 1367 * bits but the user is warned about it). 1368 */ 1369 static void riscv_cpu_add_misa_properties(Object *cpu_obj) 1370 { 1371 bool use_def_vals = riscv_cpu_is_generic(cpu_obj); 1372 int i; 1373 1374 for (i = 0; i < ARRAY_SIZE(misa_ext_cfgs); i++) { 1375 const RISCVCPUMisaExtConfig *misa_cfg = &misa_ext_cfgs[i]; 1376 int bit = misa_cfg->misa_bit; 1377 const char *name = riscv_get_misa_ext_name(bit); 1378 const char *desc = riscv_get_misa_ext_description(bit); 1379 1380 /* Check if KVM already created the property */ 1381 if (object_property_find(cpu_obj, name)) { 1382 continue; 1383 } 1384 1385 object_property_add(cpu_obj, name, "bool", 1386 cpu_get_misa_ext_cfg, 1387 cpu_set_misa_ext_cfg, 1388 NULL, (void *)misa_cfg); 1389 object_property_set_description(cpu_obj, name, desc); 1390 if (use_def_vals) { 1391 riscv_cpu_write_misa_bit(RISCV_CPU(cpu_obj), bit, 1392 misa_cfg->enabled); 1393 } 1394 } 1395 } 1396 1397 static void cpu_set_profile(Object *obj, Visitor *v, const char *name, 1398 void *opaque, Error **errp) 1399 { 1400 RISCVCPUProfile *profile = opaque; 1401 RISCVCPU *cpu = RISCV_CPU(obj); 1402 bool value; 1403 1404 if (riscv_cpu_is_vendor(obj)) { 1405 error_setg(errp, "Profile %s is not available for vendor CPUs", 1406 profile->name); 1407 return; 1408 } 1409 1410 if (cpu->env.misa_mxl != MXL_RV64) { 1411 error_setg(errp, "Profile %s only available for 64 bit CPUs", 1412 profile->name); 1413 return; 1414 } 1415 1416 if (!visit_type_bool(v, name, &value, errp)) { 1417 return; 1418 } 1419 1420 profile->user_set = true; 1421 1422 riscv_cpu_set_profile(cpu, profile, value); 1423 } 1424 1425 static void cpu_get_profile(Object *obj, Visitor *v, const char *name, 1426 void *opaque, Error **errp) 1427 { 1428 RISCVCPUProfile *profile = opaque; 1429 bool value = profile->enabled; 1430 1431 visit_type_bool(v, name, &value, errp); 1432 } 1433 1434 static void riscv_cpu_add_profiles(Object *cpu_obj) 1435 { 1436 for (int i = 0; riscv_profiles[i] != NULL; i++) { 1437 RISCVCPUProfile *profile = riscv_profiles[i]; 1438 1439 object_property_add(cpu_obj, profile->name, "bool", 1440 cpu_get_profile, cpu_set_profile, 1441 NULL, (void *)profile); 1442 1443 /* 1444 * CPUs might enable a profile right from the start. 1445 * Enable its mandatory extensions right away in this 1446 * case. 1447 */ 1448 if (profile->enabled) { 1449 riscv_cpu_set_profile(RISCV_CPU(cpu_obj), profile, true); 1450 } 1451 } 1452 } 1453 1454 static void cpu_set_multi_ext_cfg(Object *obj, Visitor *v, const char *name, 1455 void *opaque, Error **errp) 1456 { 1457 const RISCVCPUMultiExtConfig *multi_ext_cfg = opaque; 1458 RISCVCPU *cpu = RISCV_CPU(obj); 1459 bool vendor_cpu = riscv_cpu_is_vendor(obj); 1460 bool prev_val, value; 1461 1462 if (!visit_type_bool(v, name, &value, errp)) { 1463 return; 1464 } 1465 1466 cpu_cfg_ext_add_user_opt(multi_ext_cfg->offset, value); 1467 1468 prev_val = isa_ext_is_enabled(cpu, multi_ext_cfg->offset); 1469 1470 if (value == prev_val) { 1471 return; 1472 } 1473 1474 if (value && vendor_cpu) { 1475 g_autofree char *cpuname = riscv_cpu_get_name(cpu); 1476 error_setg(errp, "'%s' CPU does not allow enabling extensions", 1477 cpuname); 1478 return; 1479 } 1480 1481 if (value) { 1482 cpu_bump_multi_ext_priv_ver(&cpu->env, multi_ext_cfg->offset); 1483 } 1484 1485 isa_ext_update_enabled(cpu, multi_ext_cfg->offset, value); 1486 } 1487 1488 static void cpu_get_multi_ext_cfg(Object *obj, Visitor *v, const char *name, 1489 void *opaque, Error **errp) 1490 { 1491 const RISCVCPUMultiExtConfig *multi_ext_cfg = opaque; 1492 bool value = isa_ext_is_enabled(RISCV_CPU(obj), multi_ext_cfg->offset); 1493 1494 visit_type_bool(v, name, &value, errp); 1495 } 1496 1497 static void cpu_add_multi_ext_prop(Object *cpu_obj, 1498 const RISCVCPUMultiExtConfig *multi_cfg) 1499 { 1500 bool generic_cpu = riscv_cpu_is_generic(cpu_obj); 1501 1502 object_property_add(cpu_obj, multi_cfg->name, "bool", 1503 cpu_get_multi_ext_cfg, 1504 cpu_set_multi_ext_cfg, 1505 NULL, (void *)multi_cfg); 1506 1507 if (!generic_cpu) { 1508 return; 1509 } 1510 1511 /* 1512 * Set def val directly instead of using 1513 * object_property_set_bool() to save the set() 1514 * callback hash for user inputs. 1515 */ 1516 isa_ext_update_enabled(RISCV_CPU(cpu_obj), multi_cfg->offset, 1517 multi_cfg->enabled); 1518 } 1519 1520 static void riscv_cpu_add_multiext_prop_array(Object *obj, 1521 const RISCVCPUMultiExtConfig *array) 1522 { 1523 const RISCVCPUMultiExtConfig *prop; 1524 1525 g_assert(array); 1526 1527 for (prop = array; prop && prop->name; prop++) { 1528 cpu_add_multi_ext_prop(obj, prop); 1529 } 1530 } 1531 1532 /* 1533 * Add CPU properties with user-facing flags. 1534 * 1535 * This will overwrite existing env->misa_ext values with the 1536 * defaults set via riscv_cpu_add_misa_properties(). 1537 */ 1538 static void riscv_cpu_add_user_properties(Object *obj) 1539 { 1540 #ifndef CONFIG_USER_ONLY 1541 riscv_add_satp_mode_properties(obj); 1542 #endif 1543 1544 riscv_cpu_add_misa_properties(obj); 1545 1546 riscv_cpu_add_multiext_prop_array(obj, riscv_cpu_extensions); 1547 riscv_cpu_add_multiext_prop_array(obj, riscv_cpu_vendor_exts); 1548 riscv_cpu_add_multiext_prop_array(obj, riscv_cpu_experimental_exts); 1549 1550 riscv_cpu_add_profiles(obj); 1551 } 1552 1553 /* 1554 * The 'max' type CPU will have all possible ratified 1555 * non-vendor extensions enabled. 1556 */ 1557 static void riscv_init_max_cpu_extensions(Object *obj) 1558 { 1559 RISCVCPU *cpu = RISCV_CPU(obj); 1560 CPURISCVState *env = &cpu->env; 1561 const RISCVCPUMultiExtConfig *prop; 1562 1563 /* Enable RVG and RVV that are disabled by default */ 1564 riscv_cpu_set_misa_ext(env, env->misa_ext | RVB | RVG | RVV); 1565 1566 for (prop = riscv_cpu_extensions; prop && prop->name; prop++) { 1567 isa_ext_update_enabled(cpu, prop->offset, true); 1568 } 1569 1570 /* 1571 * Some extensions can't be added without backward compatibilty concerns. 1572 * Disable those, the user can still opt in to them on the command line. 1573 */ 1574 cpu->cfg.ext_svade = false; 1575 1576 /* set vector version */ 1577 env->vext_ver = VEXT_VERSION_1_00_0; 1578 1579 /* Zfinx is not compatible with F. Disable it */ 1580 isa_ext_update_enabled(cpu, CPU_CFG_OFFSET(ext_zfinx), false); 1581 isa_ext_update_enabled(cpu, CPU_CFG_OFFSET(ext_zdinx), false); 1582 isa_ext_update_enabled(cpu, CPU_CFG_OFFSET(ext_zhinx), false); 1583 isa_ext_update_enabled(cpu, CPU_CFG_OFFSET(ext_zhinxmin), false); 1584 1585 isa_ext_update_enabled(cpu, CPU_CFG_OFFSET(ext_zce), false); 1586 isa_ext_update_enabled(cpu, CPU_CFG_OFFSET(ext_zcmp), false); 1587 isa_ext_update_enabled(cpu, CPU_CFG_OFFSET(ext_zcmt), false); 1588 1589 if (env->misa_mxl != MXL_RV32) { 1590 isa_ext_update_enabled(cpu, CPU_CFG_OFFSET(ext_zcf), false); 1591 } 1592 1593 /* 1594 * TODO: ext_smrnmi requires OpenSBI changes that our current 1595 * image does not have. Disable it for now. 1596 */ 1597 if (cpu->cfg.ext_smrnmi) { 1598 isa_ext_update_enabled(cpu, CPU_CFG_OFFSET(ext_smrnmi), false); 1599 } 1600 1601 /* 1602 * TODO: ext_smdbltrp requires the firmware to clear MSTATUS.MDT on startup 1603 * to avoid generating a double trap. OpenSBI does not currently support it, 1604 * disable it for now. 1605 */ 1606 if (cpu->cfg.ext_smdbltrp) { 1607 isa_ext_update_enabled(cpu, CPU_CFG_OFFSET(ext_smdbltrp), false); 1608 } 1609 } 1610 1611 static bool riscv_cpu_has_max_extensions(Object *cpu_obj) 1612 { 1613 return object_dynamic_cast(cpu_obj, TYPE_RISCV_CPU_MAX) != NULL; 1614 } 1615 1616 static void riscv_tcg_cpu_instance_init(CPUState *cs) 1617 { 1618 RISCVCPU *cpu = RISCV_CPU(cs); 1619 Object *obj = OBJECT(cpu); 1620 1621 misa_ext_user_opts = g_hash_table_new(NULL, g_direct_equal); 1622 multi_ext_user_opts = g_hash_table_new(NULL, g_direct_equal); 1623 1624 if (!misa_ext_implied_rules) { 1625 misa_ext_implied_rules = g_hash_table_new(NULL, g_direct_equal); 1626 } 1627 1628 if (!multi_ext_implied_rules) { 1629 multi_ext_implied_rules = g_hash_table_new(NULL, g_direct_equal); 1630 } 1631 1632 riscv_cpu_add_user_properties(obj); 1633 1634 if (riscv_cpu_has_max_extensions(obj)) { 1635 riscv_init_max_cpu_extensions(obj); 1636 } 1637 } 1638 1639 static void riscv_tcg_cpu_accel_class_init(ObjectClass *oc, const void *data) 1640 { 1641 AccelCPUClass *acc = ACCEL_CPU_CLASS(oc); 1642 1643 acc->cpu_instance_init = riscv_tcg_cpu_instance_init; 1644 acc->cpu_target_realize = riscv_tcg_cpu_realize; 1645 } 1646 1647 static const TypeInfo riscv_tcg_cpu_accel_type_info = { 1648 .name = ACCEL_CPU_NAME("tcg"), 1649 1650 .parent = TYPE_ACCEL_CPU, 1651 .class_init = riscv_tcg_cpu_accel_class_init, 1652 .abstract = true, 1653 }; 1654 1655 static void riscv_tcg_cpu_accel_register_types(void) 1656 { 1657 type_register_static(&riscv_tcg_cpu_accel_type_info); 1658 } 1659 type_init(riscv_tcg_cpu_accel_register_types); 1660