1 /* 2 * QEMU RISC-V CPU 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 "qemu/qemu-print.h" 22 #include "qemu/ctype.h" 23 #include "qemu/log.h" 24 #include "cpu.h" 25 #include "pmu.h" 26 #include "internals.h" 27 #include "time_helper.h" 28 #include "exec/exec-all.h" 29 #include "qapi/error.h" 30 #include "qemu/error-report.h" 31 #include "hw/qdev-properties.h" 32 #include "migration/vmstate.h" 33 #include "fpu/softfloat-helpers.h" 34 #include "sysemu/kvm.h" 35 #include "kvm_riscv.h" 36 37 /* RISC-V CPU definitions */ 38 39 #define RISCV_CPU_MARCHID ((QEMU_VERSION_MAJOR << 16) | \ 40 (QEMU_VERSION_MINOR << 8) | \ 41 (QEMU_VERSION_MICRO)) 42 #define RISCV_CPU_MIMPID RISCV_CPU_MARCHID 43 44 static const char riscv_single_letter_exts[] = "IEMAFDQCPVH"; 45 46 struct isa_ext_data { 47 const char *name; 48 bool multi_letter; 49 int min_version; 50 int ext_enable_offset; 51 }; 52 53 #define ISA_EXT_DATA_ENTRY(_name, _m_letter, _min_ver, _prop) \ 54 {#_name, _m_letter, _min_ver, offsetof(struct RISCVCPUConfig, _prop)} 55 56 /** 57 * Here are the ordering rules of extension naming defined by RISC-V 58 * specification : 59 * 1. All extensions should be separated from other multi-letter extensions 60 * by an underscore. 61 * 2. The first letter following the 'Z' conventionally indicates the most 62 * closely related alphabetical extension category, IMAFDQLCBKJTPVH. 63 * If multiple 'Z' extensions are named, they should be ordered first 64 * by category, then alphabetically within a category. 65 * 3. Standard supervisor-level extensions (starts with 'S') should be 66 * listed after standard unprivileged extensions. If multiple 67 * supervisor-level extensions are listed, they should be ordered 68 * alphabetically. 69 * 4. Non-standard extensions (starts with 'X') must be listed after all 70 * standard extensions. They must be separated from other multi-letter 71 * extensions by an underscore. 72 */ 73 static const struct isa_ext_data isa_edata_arr[] = { 74 ISA_EXT_DATA_ENTRY(h, false, PRIV_VERSION_1_12_0, ext_h), 75 ISA_EXT_DATA_ENTRY(v, false, PRIV_VERSION_1_12_0, ext_v), 76 ISA_EXT_DATA_ENTRY(zicsr, true, PRIV_VERSION_1_10_0, ext_icsr), 77 ISA_EXT_DATA_ENTRY(zifencei, true, PRIV_VERSION_1_10_0, ext_ifencei), 78 ISA_EXT_DATA_ENTRY(zihintpause, true, PRIV_VERSION_1_10_0, ext_zihintpause), 79 ISA_EXT_DATA_ENTRY(zfh, true, PRIV_VERSION_1_12_0, ext_zfh), 80 ISA_EXT_DATA_ENTRY(zfhmin, true, PRIV_VERSION_1_12_0, ext_zfhmin), 81 ISA_EXT_DATA_ENTRY(zfinx, true, PRIV_VERSION_1_12_0, ext_zfinx), 82 ISA_EXT_DATA_ENTRY(zdinx, true, PRIV_VERSION_1_12_0, ext_zdinx), 83 ISA_EXT_DATA_ENTRY(zba, true, PRIV_VERSION_1_12_0, ext_zba), 84 ISA_EXT_DATA_ENTRY(zbb, true, PRIV_VERSION_1_12_0, ext_zbb), 85 ISA_EXT_DATA_ENTRY(zbc, true, PRIV_VERSION_1_12_0, ext_zbc), 86 ISA_EXT_DATA_ENTRY(zbkb, true, PRIV_VERSION_1_12_0, ext_zbkb), 87 ISA_EXT_DATA_ENTRY(zbkc, true, PRIV_VERSION_1_12_0, ext_zbkc), 88 ISA_EXT_DATA_ENTRY(zbkx, true, PRIV_VERSION_1_12_0, ext_zbkx), 89 ISA_EXT_DATA_ENTRY(zbs, true, PRIV_VERSION_1_12_0, ext_zbs), 90 ISA_EXT_DATA_ENTRY(zk, true, PRIV_VERSION_1_12_0, ext_zk), 91 ISA_EXT_DATA_ENTRY(zkn, true, PRIV_VERSION_1_12_0, ext_zkn), 92 ISA_EXT_DATA_ENTRY(zknd, true, PRIV_VERSION_1_12_0, ext_zknd), 93 ISA_EXT_DATA_ENTRY(zkne, true, PRIV_VERSION_1_12_0, ext_zkne), 94 ISA_EXT_DATA_ENTRY(zknh, true, PRIV_VERSION_1_12_0, ext_zknh), 95 ISA_EXT_DATA_ENTRY(zkr, true, PRIV_VERSION_1_12_0, ext_zkr), 96 ISA_EXT_DATA_ENTRY(zks, true, PRIV_VERSION_1_12_0, ext_zks), 97 ISA_EXT_DATA_ENTRY(zksed, true, PRIV_VERSION_1_12_0, ext_zksed), 98 ISA_EXT_DATA_ENTRY(zksh, true, PRIV_VERSION_1_12_0, ext_zksh), 99 ISA_EXT_DATA_ENTRY(zkt, true, PRIV_VERSION_1_12_0, ext_zkt), 100 ISA_EXT_DATA_ENTRY(zve32f, true, PRIV_VERSION_1_12_0, ext_zve32f), 101 ISA_EXT_DATA_ENTRY(zve64f, true, PRIV_VERSION_1_12_0, ext_zve64f), 102 ISA_EXT_DATA_ENTRY(zhinx, true, PRIV_VERSION_1_12_0, ext_zhinx), 103 ISA_EXT_DATA_ENTRY(zhinxmin, true, PRIV_VERSION_1_12_0, ext_zhinxmin), 104 ISA_EXT_DATA_ENTRY(smaia, true, PRIV_VERSION_1_12_0, ext_smaia), 105 ISA_EXT_DATA_ENTRY(ssaia, true, PRIV_VERSION_1_12_0, ext_ssaia), 106 ISA_EXT_DATA_ENTRY(sscofpmf, true, PRIV_VERSION_1_12_0, ext_sscofpmf), 107 ISA_EXT_DATA_ENTRY(sstc, true, PRIV_VERSION_1_12_0, ext_sstc), 108 ISA_EXT_DATA_ENTRY(svinval, true, PRIV_VERSION_1_12_0, ext_svinval), 109 ISA_EXT_DATA_ENTRY(svnapot, true, PRIV_VERSION_1_12_0, ext_svnapot), 110 ISA_EXT_DATA_ENTRY(svpbmt, true, PRIV_VERSION_1_12_0, ext_svpbmt), 111 ISA_EXT_DATA_ENTRY(xventanacondops, true, PRIV_VERSION_1_12_0, ext_XVentanaCondOps), 112 }; 113 114 static bool isa_ext_is_enabled(RISCVCPU *cpu, 115 const struct isa_ext_data *edata) 116 { 117 bool *ext_enabled = (void *)&cpu->cfg + edata->ext_enable_offset; 118 119 return *ext_enabled; 120 } 121 122 static void isa_ext_update_enabled(RISCVCPU *cpu, 123 const struct isa_ext_data *edata, bool en) 124 { 125 bool *ext_enabled = (void *)&cpu->cfg + edata->ext_enable_offset; 126 127 *ext_enabled = en; 128 } 129 130 const char * const riscv_int_regnames[] = { 131 "x0/zero", "x1/ra", "x2/sp", "x3/gp", "x4/tp", "x5/t0", "x6/t1", 132 "x7/t2", "x8/s0", "x9/s1", "x10/a0", "x11/a1", "x12/a2", "x13/a3", 133 "x14/a4", "x15/a5", "x16/a6", "x17/a7", "x18/s2", "x19/s3", "x20/s4", 134 "x21/s5", "x22/s6", "x23/s7", "x24/s8", "x25/s9", "x26/s10", "x27/s11", 135 "x28/t3", "x29/t4", "x30/t5", "x31/t6" 136 }; 137 138 const char * const riscv_int_regnamesh[] = { 139 "x0h/zeroh", "x1h/rah", "x2h/sph", "x3h/gph", "x4h/tph", "x5h/t0h", 140 "x6h/t1h", "x7h/t2h", "x8h/s0h", "x9h/s1h", "x10h/a0h", "x11h/a1h", 141 "x12h/a2h", "x13h/a3h", "x14h/a4h", "x15h/a5h", "x16h/a6h", "x17h/a7h", 142 "x18h/s2h", "x19h/s3h", "x20h/s4h", "x21h/s5h", "x22h/s6h", "x23h/s7h", 143 "x24h/s8h", "x25h/s9h", "x26h/s10h", "x27h/s11h", "x28h/t3h", "x29h/t4h", 144 "x30h/t5h", "x31h/t6h" 145 }; 146 147 const char * const riscv_fpr_regnames[] = { 148 "f0/ft0", "f1/ft1", "f2/ft2", "f3/ft3", "f4/ft4", "f5/ft5", 149 "f6/ft6", "f7/ft7", "f8/fs0", "f9/fs1", "f10/fa0", "f11/fa1", 150 "f12/fa2", "f13/fa3", "f14/fa4", "f15/fa5", "f16/fa6", "f17/fa7", 151 "f18/fs2", "f19/fs3", "f20/fs4", "f21/fs5", "f22/fs6", "f23/fs7", 152 "f24/fs8", "f25/fs9", "f26/fs10", "f27/fs11", "f28/ft8", "f29/ft9", 153 "f30/ft10", "f31/ft11" 154 }; 155 156 static const char * const riscv_excp_names[] = { 157 "misaligned_fetch", 158 "fault_fetch", 159 "illegal_instruction", 160 "breakpoint", 161 "misaligned_load", 162 "fault_load", 163 "misaligned_store", 164 "fault_store", 165 "user_ecall", 166 "supervisor_ecall", 167 "hypervisor_ecall", 168 "machine_ecall", 169 "exec_page_fault", 170 "load_page_fault", 171 "reserved", 172 "store_page_fault", 173 "reserved", 174 "reserved", 175 "reserved", 176 "reserved", 177 "guest_exec_page_fault", 178 "guest_load_page_fault", 179 "reserved", 180 "guest_store_page_fault", 181 }; 182 183 static const char * const riscv_intr_names[] = { 184 "u_software", 185 "s_software", 186 "vs_software", 187 "m_software", 188 "u_timer", 189 "s_timer", 190 "vs_timer", 191 "m_timer", 192 "u_external", 193 "s_external", 194 "vs_external", 195 "m_external", 196 "reserved", 197 "reserved", 198 "reserved", 199 "reserved" 200 }; 201 202 static void register_cpu_props(DeviceState *dev); 203 204 const char *riscv_cpu_get_trap_name(target_ulong cause, bool async) 205 { 206 if (async) { 207 return (cause < ARRAY_SIZE(riscv_intr_names)) ? 208 riscv_intr_names[cause] : "(unknown)"; 209 } else { 210 return (cause < ARRAY_SIZE(riscv_excp_names)) ? 211 riscv_excp_names[cause] : "(unknown)"; 212 } 213 } 214 215 static void set_misa(CPURISCVState *env, RISCVMXL mxl, uint32_t ext) 216 { 217 env->misa_mxl_max = env->misa_mxl = mxl; 218 env->misa_ext_mask = env->misa_ext = ext; 219 } 220 221 static void set_priv_version(CPURISCVState *env, int priv_ver) 222 { 223 env->priv_ver = priv_ver; 224 } 225 226 static void set_vext_version(CPURISCVState *env, int vext_ver) 227 { 228 env->vext_ver = vext_ver; 229 } 230 231 static void riscv_any_cpu_init(Object *obj) 232 { 233 CPURISCVState *env = &RISCV_CPU(obj)->env; 234 #if defined(TARGET_RISCV32) 235 set_misa(env, MXL_RV32, RVI | RVM | RVA | RVF | RVD | RVC | RVU); 236 #elif defined(TARGET_RISCV64) 237 set_misa(env, MXL_RV64, RVI | RVM | RVA | RVF | RVD | RVC | RVU); 238 #endif 239 set_priv_version(env, PRIV_VERSION_1_12_0); 240 register_cpu_props(DEVICE(obj)); 241 } 242 243 #if defined(TARGET_RISCV64) 244 static void rv64_base_cpu_init(Object *obj) 245 { 246 CPURISCVState *env = &RISCV_CPU(obj)->env; 247 /* We set this in the realise function */ 248 set_misa(env, MXL_RV64, 0); 249 register_cpu_props(DEVICE(obj)); 250 /* Set latest version of privileged specification */ 251 set_priv_version(env, PRIV_VERSION_1_12_0); 252 } 253 254 static void rv64_sifive_u_cpu_init(Object *obj) 255 { 256 CPURISCVState *env = &RISCV_CPU(obj)->env; 257 set_misa(env, MXL_RV64, RVI | RVM | RVA | RVF | RVD | RVC | RVS | RVU); 258 set_priv_version(env, PRIV_VERSION_1_10_0); 259 } 260 261 static void rv64_sifive_e_cpu_init(Object *obj) 262 { 263 CPURISCVState *env = &RISCV_CPU(obj)->env; 264 RISCVCPU *cpu = RISCV_CPU(obj); 265 266 set_misa(env, MXL_RV64, RVI | RVM | RVA | RVC | RVU); 267 set_priv_version(env, PRIV_VERSION_1_10_0); 268 cpu->cfg.mmu = false; 269 } 270 271 static void rv128_base_cpu_init(Object *obj) 272 { 273 if (qemu_tcg_mttcg_enabled()) { 274 /* Missing 128-bit aligned atomics */ 275 error_report("128-bit RISC-V currently does not work with Multi " 276 "Threaded TCG. Please use: -accel tcg,thread=single"); 277 exit(EXIT_FAILURE); 278 } 279 CPURISCVState *env = &RISCV_CPU(obj)->env; 280 /* We set this in the realise function */ 281 set_misa(env, MXL_RV128, 0); 282 register_cpu_props(DEVICE(obj)); 283 /* Set latest version of privileged specification */ 284 set_priv_version(env, PRIV_VERSION_1_12_0); 285 } 286 #else 287 static void rv32_base_cpu_init(Object *obj) 288 { 289 CPURISCVState *env = &RISCV_CPU(obj)->env; 290 /* We set this in the realise function */ 291 set_misa(env, MXL_RV32, 0); 292 register_cpu_props(DEVICE(obj)); 293 /* Set latest version of privileged specification */ 294 set_priv_version(env, PRIV_VERSION_1_12_0); 295 } 296 297 static void rv32_sifive_u_cpu_init(Object *obj) 298 { 299 CPURISCVState *env = &RISCV_CPU(obj)->env; 300 set_misa(env, MXL_RV32, RVI | RVM | RVA | RVF | RVD | RVC | RVS | RVU); 301 set_priv_version(env, PRIV_VERSION_1_10_0); 302 } 303 304 static void rv32_sifive_e_cpu_init(Object *obj) 305 { 306 CPURISCVState *env = &RISCV_CPU(obj)->env; 307 RISCVCPU *cpu = RISCV_CPU(obj); 308 309 set_misa(env, MXL_RV32, RVI | RVM | RVA | RVC | RVU); 310 set_priv_version(env, PRIV_VERSION_1_10_0); 311 cpu->cfg.mmu = false; 312 } 313 314 static void rv32_ibex_cpu_init(Object *obj) 315 { 316 CPURISCVState *env = &RISCV_CPU(obj)->env; 317 RISCVCPU *cpu = RISCV_CPU(obj); 318 319 set_misa(env, MXL_RV32, RVI | RVM | RVC | RVU); 320 set_priv_version(env, PRIV_VERSION_1_11_0); 321 cpu->cfg.mmu = false; 322 cpu->cfg.epmp = true; 323 } 324 325 static void rv32_imafcu_nommu_cpu_init(Object *obj) 326 { 327 CPURISCVState *env = &RISCV_CPU(obj)->env; 328 RISCVCPU *cpu = RISCV_CPU(obj); 329 330 set_misa(env, MXL_RV32, RVI | RVM | RVA | RVF | RVC | RVU); 331 set_priv_version(env, PRIV_VERSION_1_10_0); 332 cpu->cfg.mmu = false; 333 } 334 #endif 335 336 #if defined(CONFIG_KVM) 337 static void riscv_host_cpu_init(Object *obj) 338 { 339 CPURISCVState *env = &RISCV_CPU(obj)->env; 340 #if defined(TARGET_RISCV32) 341 set_misa(env, MXL_RV32, 0); 342 #elif defined(TARGET_RISCV64) 343 set_misa(env, MXL_RV64, 0); 344 #endif 345 register_cpu_props(DEVICE(obj)); 346 } 347 #endif 348 349 static ObjectClass *riscv_cpu_class_by_name(const char *cpu_model) 350 { 351 ObjectClass *oc; 352 char *typename; 353 char **cpuname; 354 355 cpuname = g_strsplit(cpu_model, ",", 1); 356 typename = g_strdup_printf(RISCV_CPU_TYPE_NAME("%s"), cpuname[0]); 357 oc = object_class_by_name(typename); 358 g_strfreev(cpuname); 359 g_free(typename); 360 if (!oc || !object_class_dynamic_cast(oc, TYPE_RISCV_CPU) || 361 object_class_is_abstract(oc)) { 362 return NULL; 363 } 364 return oc; 365 } 366 367 static void riscv_cpu_dump_state(CPUState *cs, FILE *f, int flags) 368 { 369 RISCVCPU *cpu = RISCV_CPU(cs); 370 CPURISCVState *env = &cpu->env; 371 int i; 372 373 #if !defined(CONFIG_USER_ONLY) 374 if (riscv_has_ext(env, RVH)) { 375 qemu_fprintf(f, " %s %d\n", "V = ", riscv_cpu_virt_enabled(env)); 376 } 377 #endif 378 qemu_fprintf(f, " %s " TARGET_FMT_lx "\n", "pc ", env->pc); 379 #ifndef CONFIG_USER_ONLY 380 { 381 static const int dump_csrs[] = { 382 CSR_MHARTID, 383 CSR_MSTATUS, 384 CSR_MSTATUSH, 385 /* 386 * CSR_SSTATUS is intentionally omitted here as its value 387 * can be figured out by looking at CSR_MSTATUS 388 */ 389 CSR_HSTATUS, 390 CSR_VSSTATUS, 391 CSR_MIP, 392 CSR_MIE, 393 CSR_MIDELEG, 394 CSR_HIDELEG, 395 CSR_MEDELEG, 396 CSR_HEDELEG, 397 CSR_MTVEC, 398 CSR_STVEC, 399 CSR_VSTVEC, 400 CSR_MEPC, 401 CSR_SEPC, 402 CSR_VSEPC, 403 CSR_MCAUSE, 404 CSR_SCAUSE, 405 CSR_VSCAUSE, 406 CSR_MTVAL, 407 CSR_STVAL, 408 CSR_HTVAL, 409 CSR_MTVAL2, 410 CSR_MSCRATCH, 411 CSR_SSCRATCH, 412 CSR_SATP, 413 CSR_MMTE, 414 CSR_UPMBASE, 415 CSR_UPMMASK, 416 CSR_SPMBASE, 417 CSR_SPMMASK, 418 CSR_MPMBASE, 419 CSR_MPMMASK, 420 }; 421 422 for (int i = 0; i < ARRAY_SIZE(dump_csrs); ++i) { 423 int csrno = dump_csrs[i]; 424 target_ulong val = 0; 425 RISCVException res = riscv_csrrw_debug(env, csrno, &val, 0, 0); 426 427 /* 428 * Rely on the smode, hmode, etc, predicates within csr.c 429 * to do the filtering of the registers that are present. 430 */ 431 if (res == RISCV_EXCP_NONE) { 432 qemu_fprintf(f, " %-8s " TARGET_FMT_lx "\n", 433 csr_ops[csrno].name, val); 434 } 435 } 436 } 437 #endif 438 439 for (i = 0; i < 32; i++) { 440 qemu_fprintf(f, " %-8s " TARGET_FMT_lx, 441 riscv_int_regnames[i], env->gpr[i]); 442 if ((i & 3) == 3) { 443 qemu_fprintf(f, "\n"); 444 } 445 } 446 if (flags & CPU_DUMP_FPU) { 447 for (i = 0; i < 32; i++) { 448 qemu_fprintf(f, " %-8s %016" PRIx64, 449 riscv_fpr_regnames[i], env->fpr[i]); 450 if ((i & 3) == 3) { 451 qemu_fprintf(f, "\n"); 452 } 453 } 454 } 455 } 456 457 static void riscv_cpu_set_pc(CPUState *cs, vaddr value) 458 { 459 RISCVCPU *cpu = RISCV_CPU(cs); 460 CPURISCVState *env = &cpu->env; 461 462 if (env->xl == MXL_RV32) { 463 env->pc = (int32_t)value; 464 } else { 465 env->pc = value; 466 } 467 } 468 469 static vaddr riscv_cpu_get_pc(CPUState *cs) 470 { 471 RISCVCPU *cpu = RISCV_CPU(cs); 472 CPURISCVState *env = &cpu->env; 473 474 /* Match cpu_get_tb_cpu_state. */ 475 if (env->xl == MXL_RV32) { 476 return env->pc & UINT32_MAX; 477 } 478 return env->pc; 479 } 480 481 static void riscv_cpu_synchronize_from_tb(CPUState *cs, 482 const TranslationBlock *tb) 483 { 484 RISCVCPU *cpu = RISCV_CPU(cs); 485 CPURISCVState *env = &cpu->env; 486 RISCVMXL xl = FIELD_EX32(tb->flags, TB_FLAGS, XL); 487 488 if (xl == MXL_RV32) { 489 env->pc = (int32_t)tb_pc(tb); 490 } else { 491 env->pc = tb_pc(tb); 492 } 493 } 494 495 static bool riscv_cpu_has_work(CPUState *cs) 496 { 497 #ifndef CONFIG_USER_ONLY 498 RISCVCPU *cpu = RISCV_CPU(cs); 499 CPURISCVState *env = &cpu->env; 500 /* 501 * Definition of the WFI instruction requires it to ignore the privilege 502 * mode and delegation registers, but respect individual enables 503 */ 504 return riscv_cpu_all_pending(env) != 0; 505 #else 506 return true; 507 #endif 508 } 509 510 static void riscv_restore_state_to_opc(CPUState *cs, 511 const TranslationBlock *tb, 512 const uint64_t *data) 513 { 514 RISCVCPU *cpu = RISCV_CPU(cs); 515 CPURISCVState *env = &cpu->env; 516 RISCVMXL xl = FIELD_EX32(tb->flags, TB_FLAGS, XL); 517 518 if (xl == MXL_RV32) { 519 env->pc = (int32_t)data[0]; 520 } else { 521 env->pc = data[0]; 522 } 523 env->bins = data[1]; 524 } 525 526 static void riscv_cpu_reset_hold(Object *obj) 527 { 528 #ifndef CONFIG_USER_ONLY 529 uint8_t iprio; 530 int i, irq, rdzero; 531 #endif 532 CPUState *cs = CPU(obj); 533 RISCVCPU *cpu = RISCV_CPU(cs); 534 RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cpu); 535 CPURISCVState *env = &cpu->env; 536 537 if (mcc->parent_phases.hold) { 538 mcc->parent_phases.hold(obj); 539 } 540 #ifndef CONFIG_USER_ONLY 541 env->misa_mxl = env->misa_mxl_max; 542 env->priv = PRV_M; 543 env->mstatus &= ~(MSTATUS_MIE | MSTATUS_MPRV); 544 if (env->misa_mxl > MXL_RV32) { 545 /* 546 * The reset status of SXL/UXL is undefined, but mstatus is WARL 547 * and we must ensure that the value after init is valid for read. 548 */ 549 env->mstatus = set_field(env->mstatus, MSTATUS64_SXL, env->misa_mxl); 550 env->mstatus = set_field(env->mstatus, MSTATUS64_UXL, env->misa_mxl); 551 if (riscv_has_ext(env, RVH)) { 552 env->vsstatus = set_field(env->vsstatus, 553 MSTATUS64_SXL, env->misa_mxl); 554 env->vsstatus = set_field(env->vsstatus, 555 MSTATUS64_UXL, env->misa_mxl); 556 env->mstatus_hs = set_field(env->mstatus_hs, 557 MSTATUS64_SXL, env->misa_mxl); 558 env->mstatus_hs = set_field(env->mstatus_hs, 559 MSTATUS64_UXL, env->misa_mxl); 560 } 561 } 562 env->mcause = 0; 563 env->miclaim = MIP_SGEIP; 564 env->pc = env->resetvec; 565 env->bins = 0; 566 env->two_stage_lookup = false; 567 568 /* Initialized default priorities of local interrupts. */ 569 for (i = 0; i < ARRAY_SIZE(env->miprio); i++) { 570 iprio = riscv_cpu_default_priority(i); 571 env->miprio[i] = (i == IRQ_M_EXT) ? 0 : iprio; 572 env->siprio[i] = (i == IRQ_S_EXT) ? 0 : iprio; 573 env->hviprio[i] = 0; 574 } 575 i = 0; 576 while (!riscv_cpu_hviprio_index2irq(i, &irq, &rdzero)) { 577 if (!rdzero) { 578 env->hviprio[irq] = env->miprio[irq]; 579 } 580 i++; 581 } 582 /* mmte is supposed to have pm.current hardwired to 1 */ 583 env->mmte |= (PM_EXT_INITIAL | MMTE_M_PM_CURRENT); 584 #endif 585 env->xl = riscv_cpu_mxl(env); 586 riscv_cpu_update_mask(env); 587 cs->exception_index = RISCV_EXCP_NONE; 588 env->load_res = -1; 589 set_default_nan_mode(1, &env->fp_status); 590 591 #ifndef CONFIG_USER_ONLY 592 if (riscv_feature(env, RISCV_FEATURE_DEBUG)) { 593 riscv_trigger_init(env); 594 } 595 596 if (kvm_enabled()) { 597 kvm_riscv_reset_vcpu(cpu); 598 } 599 #endif 600 } 601 602 static void riscv_cpu_disas_set_info(CPUState *s, disassemble_info *info) 603 { 604 RISCVCPU *cpu = RISCV_CPU(s); 605 606 switch (riscv_cpu_mxl(&cpu->env)) { 607 case MXL_RV32: 608 info->print_insn = print_insn_riscv32; 609 break; 610 case MXL_RV64: 611 info->print_insn = print_insn_riscv64; 612 break; 613 case MXL_RV128: 614 info->print_insn = print_insn_riscv128; 615 break; 616 default: 617 g_assert_not_reached(); 618 } 619 } 620 621 static void riscv_cpu_realize(DeviceState *dev, Error **errp) 622 { 623 CPUState *cs = CPU(dev); 624 RISCVCPU *cpu = RISCV_CPU(dev); 625 CPURISCVState *env = &cpu->env; 626 RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(dev); 627 CPUClass *cc = CPU_CLASS(mcc); 628 int i, priv_version = -1; 629 Error *local_err = NULL; 630 631 cpu_exec_realizefn(cs, &local_err); 632 if (local_err != NULL) { 633 error_propagate(errp, local_err); 634 return; 635 } 636 637 if (cpu->cfg.priv_spec) { 638 if (!g_strcmp0(cpu->cfg.priv_spec, "v1.12.0")) { 639 priv_version = PRIV_VERSION_1_12_0; 640 } else if (!g_strcmp0(cpu->cfg.priv_spec, "v1.11.0")) { 641 priv_version = PRIV_VERSION_1_11_0; 642 } else if (!g_strcmp0(cpu->cfg.priv_spec, "v1.10.0")) { 643 priv_version = PRIV_VERSION_1_10_0; 644 } else { 645 error_setg(errp, 646 "Unsupported privilege spec version '%s'", 647 cpu->cfg.priv_spec); 648 return; 649 } 650 } 651 652 if (priv_version >= PRIV_VERSION_1_10_0) { 653 set_priv_version(env, priv_version); 654 } 655 656 /* Force disable extensions if priv spec version does not match */ 657 for (i = 0; i < ARRAY_SIZE(isa_edata_arr); i++) { 658 if (isa_ext_is_enabled(cpu, &isa_edata_arr[i]) && 659 (env->priv_ver < isa_edata_arr[i].min_version)) { 660 isa_ext_update_enabled(cpu, &isa_edata_arr[i], false); 661 #ifndef CONFIG_USER_ONLY 662 warn_report("disabling %s extension for hart 0x%lx because " 663 "privilege spec version does not match", 664 isa_edata_arr[i].name, (unsigned long)env->mhartid); 665 #else 666 warn_report("disabling %s extension because " 667 "privilege spec version does not match", 668 isa_edata_arr[i].name); 669 #endif 670 } 671 } 672 673 if (cpu->cfg.mmu) { 674 riscv_set_feature(env, RISCV_FEATURE_MMU); 675 } 676 677 if (cpu->cfg.pmp) { 678 riscv_set_feature(env, RISCV_FEATURE_PMP); 679 680 /* 681 * Enhanced PMP should only be available 682 * on harts with PMP support 683 */ 684 if (cpu->cfg.epmp) { 685 riscv_set_feature(env, RISCV_FEATURE_EPMP); 686 } 687 } 688 689 if (cpu->cfg.debug) { 690 riscv_set_feature(env, RISCV_FEATURE_DEBUG); 691 } 692 693 694 #ifndef CONFIG_USER_ONLY 695 if (cpu->cfg.ext_sstc) { 696 riscv_timer_init(cpu); 697 } 698 #endif /* CONFIG_USER_ONLY */ 699 700 /* Validate that MISA_MXL is set properly. */ 701 switch (env->misa_mxl_max) { 702 #ifdef TARGET_RISCV64 703 case MXL_RV64: 704 case MXL_RV128: 705 cc->gdb_core_xml_file = "riscv-64bit-cpu.xml"; 706 break; 707 #endif 708 case MXL_RV32: 709 cc->gdb_core_xml_file = "riscv-32bit-cpu.xml"; 710 break; 711 default: 712 g_assert_not_reached(); 713 } 714 assert(env->misa_mxl_max == env->misa_mxl); 715 716 /* If only MISA_EXT is unset for misa, then set it from properties */ 717 if (env->misa_ext == 0) { 718 uint32_t ext = 0; 719 720 /* Do some ISA extension error checking */ 721 if (cpu->cfg.ext_g && !(cpu->cfg.ext_i && cpu->cfg.ext_m && 722 cpu->cfg.ext_a && cpu->cfg.ext_f && 723 cpu->cfg.ext_d && 724 cpu->cfg.ext_icsr && cpu->cfg.ext_ifencei)) { 725 warn_report("Setting G will also set IMAFD_Zicsr_Zifencei"); 726 cpu->cfg.ext_i = true; 727 cpu->cfg.ext_m = true; 728 cpu->cfg.ext_a = true; 729 cpu->cfg.ext_f = true; 730 cpu->cfg.ext_d = true; 731 cpu->cfg.ext_icsr = true; 732 cpu->cfg.ext_ifencei = true; 733 } 734 735 if (cpu->cfg.ext_i && cpu->cfg.ext_e) { 736 error_setg(errp, 737 "I and E extensions are incompatible"); 738 return; 739 } 740 741 if (!cpu->cfg.ext_i && !cpu->cfg.ext_e) { 742 error_setg(errp, 743 "Either I or E extension must be set"); 744 return; 745 } 746 747 if (cpu->cfg.ext_s && !cpu->cfg.ext_u) { 748 error_setg(errp, 749 "Setting S extension without U extension is illegal"); 750 return; 751 } 752 753 if (cpu->cfg.ext_h && !cpu->cfg.ext_i) { 754 error_setg(errp, 755 "H depends on an I base integer ISA with 32 x registers"); 756 return; 757 } 758 759 if (cpu->cfg.ext_h && !cpu->cfg.ext_s) { 760 error_setg(errp, "H extension implicitly requires S-mode"); 761 return; 762 } 763 764 if (cpu->cfg.ext_f && !cpu->cfg.ext_icsr) { 765 error_setg(errp, "F extension requires Zicsr"); 766 return; 767 } 768 769 if ((cpu->cfg.ext_zfh || cpu->cfg.ext_zfhmin) && !cpu->cfg.ext_f) { 770 error_setg(errp, "Zfh/Zfhmin extensions require F extension"); 771 return; 772 } 773 774 if (cpu->cfg.ext_d && !cpu->cfg.ext_f) { 775 error_setg(errp, "D extension requires F extension"); 776 return; 777 } 778 779 if (cpu->cfg.ext_v && !cpu->cfg.ext_d) { 780 error_setg(errp, "V extension requires D extension"); 781 return; 782 } 783 784 if ((cpu->cfg.ext_zve32f || cpu->cfg.ext_zve64f) && !cpu->cfg.ext_f) { 785 error_setg(errp, "Zve32f/Zve64f extensions require F extension"); 786 return; 787 } 788 789 /* Set the ISA extensions, checks should have happened above */ 790 if (cpu->cfg.ext_zdinx || cpu->cfg.ext_zhinx || 791 cpu->cfg.ext_zhinxmin) { 792 cpu->cfg.ext_zfinx = true; 793 } 794 795 if (cpu->cfg.ext_zfinx) { 796 if (!cpu->cfg.ext_icsr) { 797 error_setg(errp, "Zfinx extension requires Zicsr"); 798 return; 799 } 800 if (cpu->cfg.ext_f) { 801 error_setg(errp, 802 "Zfinx cannot be supported together with F extension"); 803 return; 804 } 805 } 806 807 if (cpu->cfg.ext_zk) { 808 cpu->cfg.ext_zkn = true; 809 cpu->cfg.ext_zkr = true; 810 cpu->cfg.ext_zkt = true; 811 } 812 813 if (cpu->cfg.ext_zkn) { 814 cpu->cfg.ext_zbkb = true; 815 cpu->cfg.ext_zbkc = true; 816 cpu->cfg.ext_zbkx = true; 817 cpu->cfg.ext_zkne = true; 818 cpu->cfg.ext_zknd = true; 819 cpu->cfg.ext_zknh = true; 820 } 821 822 if (cpu->cfg.ext_zks) { 823 cpu->cfg.ext_zbkb = true; 824 cpu->cfg.ext_zbkc = true; 825 cpu->cfg.ext_zbkx = true; 826 cpu->cfg.ext_zksed = true; 827 cpu->cfg.ext_zksh = true; 828 } 829 830 if (cpu->cfg.ext_i) { 831 ext |= RVI; 832 } 833 if (cpu->cfg.ext_e) { 834 ext |= RVE; 835 } 836 if (cpu->cfg.ext_m) { 837 ext |= RVM; 838 } 839 if (cpu->cfg.ext_a) { 840 ext |= RVA; 841 } 842 if (cpu->cfg.ext_f) { 843 ext |= RVF; 844 } 845 if (cpu->cfg.ext_d) { 846 ext |= RVD; 847 } 848 if (cpu->cfg.ext_c) { 849 ext |= RVC; 850 } 851 if (cpu->cfg.ext_s) { 852 ext |= RVS; 853 } 854 if (cpu->cfg.ext_u) { 855 ext |= RVU; 856 } 857 if (cpu->cfg.ext_h) { 858 ext |= RVH; 859 } 860 if (cpu->cfg.ext_v) { 861 int vext_version = VEXT_VERSION_1_00_0; 862 ext |= RVV; 863 if (!is_power_of_2(cpu->cfg.vlen)) { 864 error_setg(errp, 865 "Vector extension VLEN must be power of 2"); 866 return; 867 } 868 if (cpu->cfg.vlen > RV_VLEN_MAX || cpu->cfg.vlen < 128) { 869 error_setg(errp, 870 "Vector extension implementation only supports VLEN " 871 "in the range [128, %d]", RV_VLEN_MAX); 872 return; 873 } 874 if (!is_power_of_2(cpu->cfg.elen)) { 875 error_setg(errp, 876 "Vector extension ELEN must be power of 2"); 877 return; 878 } 879 if (cpu->cfg.elen > 64 || cpu->cfg.vlen < 8) { 880 error_setg(errp, 881 "Vector extension implementation only supports ELEN " 882 "in the range [8, 64]"); 883 return; 884 } 885 if (cpu->cfg.vext_spec) { 886 if (!g_strcmp0(cpu->cfg.vext_spec, "v1.0")) { 887 vext_version = VEXT_VERSION_1_00_0; 888 } else { 889 error_setg(errp, 890 "Unsupported vector spec version '%s'", 891 cpu->cfg.vext_spec); 892 return; 893 } 894 } else { 895 qemu_log("vector version is not specified, " 896 "use the default value v1.0\n"); 897 } 898 set_vext_version(env, vext_version); 899 } 900 if (cpu->cfg.ext_j) { 901 ext |= RVJ; 902 } 903 904 set_misa(env, env->misa_mxl, ext); 905 } 906 907 #ifndef CONFIG_USER_ONLY 908 if (cpu->cfg.pmu_num) { 909 if (!riscv_pmu_init(cpu, cpu->cfg.pmu_num) && cpu->cfg.ext_sscofpmf) { 910 cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, 911 riscv_pmu_timer_cb, cpu); 912 } 913 } 914 #endif 915 916 riscv_cpu_register_gdb_regs_for_features(cs); 917 918 qemu_init_vcpu(cs); 919 cpu_reset(cs); 920 921 mcc->parent_realize(dev, errp); 922 } 923 924 #ifndef CONFIG_USER_ONLY 925 static void riscv_cpu_set_irq(void *opaque, int irq, int level) 926 { 927 RISCVCPU *cpu = RISCV_CPU(opaque); 928 CPURISCVState *env = &cpu->env; 929 930 if (irq < IRQ_LOCAL_MAX) { 931 switch (irq) { 932 case IRQ_U_SOFT: 933 case IRQ_S_SOFT: 934 case IRQ_VS_SOFT: 935 case IRQ_M_SOFT: 936 case IRQ_U_TIMER: 937 case IRQ_S_TIMER: 938 case IRQ_VS_TIMER: 939 case IRQ_M_TIMER: 940 case IRQ_U_EXT: 941 case IRQ_VS_EXT: 942 case IRQ_M_EXT: 943 if (kvm_enabled()) { 944 kvm_riscv_set_irq(cpu, irq, level); 945 } else { 946 riscv_cpu_update_mip(cpu, 1 << irq, BOOL_TO_MASK(level)); 947 } 948 break; 949 case IRQ_S_EXT: 950 if (kvm_enabled()) { 951 kvm_riscv_set_irq(cpu, irq, level); 952 } else { 953 env->external_seip = level; 954 riscv_cpu_update_mip(cpu, 1 << irq, 955 BOOL_TO_MASK(level | env->software_seip)); 956 } 957 break; 958 default: 959 g_assert_not_reached(); 960 } 961 } else if (irq < (IRQ_LOCAL_MAX + IRQ_LOCAL_GUEST_MAX)) { 962 /* Require H-extension for handling guest local interrupts */ 963 if (!riscv_has_ext(env, RVH)) { 964 g_assert_not_reached(); 965 } 966 967 /* Compute bit position in HGEIP CSR */ 968 irq = irq - IRQ_LOCAL_MAX + 1; 969 if (env->geilen < irq) { 970 g_assert_not_reached(); 971 } 972 973 /* Update HGEIP CSR */ 974 env->hgeip &= ~((target_ulong)1 << irq); 975 if (level) { 976 env->hgeip |= (target_ulong)1 << irq; 977 } 978 979 /* Update mip.SGEIP bit */ 980 riscv_cpu_update_mip(cpu, MIP_SGEIP, 981 BOOL_TO_MASK(!!(env->hgeie & env->hgeip))); 982 } else { 983 g_assert_not_reached(); 984 } 985 } 986 #endif /* CONFIG_USER_ONLY */ 987 988 static void riscv_cpu_init(Object *obj) 989 { 990 RISCVCPU *cpu = RISCV_CPU(obj); 991 992 cpu->cfg.ext_ifencei = true; 993 cpu->cfg.ext_icsr = true; 994 cpu->cfg.mmu = true; 995 cpu->cfg.pmp = true; 996 997 cpu_set_cpustate_pointers(cpu); 998 999 #ifndef CONFIG_USER_ONLY 1000 qdev_init_gpio_in(DEVICE(cpu), riscv_cpu_set_irq, 1001 IRQ_LOCAL_MAX + IRQ_LOCAL_GUEST_MAX); 1002 #endif /* CONFIG_USER_ONLY */ 1003 } 1004 1005 static Property riscv_cpu_extensions[] = { 1006 /* Defaults for standard extensions */ 1007 DEFINE_PROP_BOOL("i", RISCVCPU, cfg.ext_i, true), 1008 DEFINE_PROP_BOOL("e", RISCVCPU, cfg.ext_e, false), 1009 DEFINE_PROP_BOOL("g", RISCVCPU, cfg.ext_g, false), 1010 DEFINE_PROP_BOOL("m", RISCVCPU, cfg.ext_m, true), 1011 DEFINE_PROP_BOOL("a", RISCVCPU, cfg.ext_a, true), 1012 DEFINE_PROP_BOOL("f", RISCVCPU, cfg.ext_f, true), 1013 DEFINE_PROP_BOOL("d", RISCVCPU, cfg.ext_d, true), 1014 DEFINE_PROP_BOOL("c", RISCVCPU, cfg.ext_c, true), 1015 DEFINE_PROP_BOOL("s", RISCVCPU, cfg.ext_s, true), 1016 DEFINE_PROP_BOOL("u", RISCVCPU, cfg.ext_u, true), 1017 DEFINE_PROP_BOOL("v", RISCVCPU, cfg.ext_v, false), 1018 DEFINE_PROP_BOOL("h", RISCVCPU, cfg.ext_h, true), 1019 DEFINE_PROP_UINT8("pmu-num", RISCVCPU, cfg.pmu_num, 16), 1020 DEFINE_PROP_BOOL("sscofpmf", RISCVCPU, cfg.ext_sscofpmf, false), 1021 DEFINE_PROP_BOOL("Zifencei", RISCVCPU, cfg.ext_ifencei, true), 1022 DEFINE_PROP_BOOL("Zicsr", RISCVCPU, cfg.ext_icsr, true), 1023 DEFINE_PROP_BOOL("Zihintpause", RISCVCPU, cfg.ext_zihintpause, true), 1024 DEFINE_PROP_BOOL("Zfh", RISCVCPU, cfg.ext_zfh, false), 1025 DEFINE_PROP_BOOL("Zfhmin", RISCVCPU, cfg.ext_zfhmin, false), 1026 DEFINE_PROP_BOOL("Zve32f", RISCVCPU, cfg.ext_zve32f, false), 1027 DEFINE_PROP_BOOL("Zve64f", RISCVCPU, cfg.ext_zve64f, false), 1028 DEFINE_PROP_BOOL("mmu", RISCVCPU, cfg.mmu, true), 1029 DEFINE_PROP_BOOL("pmp", RISCVCPU, cfg.pmp, true), 1030 DEFINE_PROP_BOOL("sstc", RISCVCPU, cfg.ext_sstc, true), 1031 1032 DEFINE_PROP_STRING("priv_spec", RISCVCPU, cfg.priv_spec), 1033 DEFINE_PROP_STRING("vext_spec", RISCVCPU, cfg.vext_spec), 1034 DEFINE_PROP_UINT16("vlen", RISCVCPU, cfg.vlen, 128), 1035 DEFINE_PROP_UINT16("elen", RISCVCPU, cfg.elen, 64), 1036 1037 DEFINE_PROP_BOOL("svinval", RISCVCPU, cfg.ext_svinval, false), 1038 DEFINE_PROP_BOOL("svnapot", RISCVCPU, cfg.ext_svnapot, false), 1039 DEFINE_PROP_BOOL("svpbmt", RISCVCPU, cfg.ext_svpbmt, false), 1040 1041 DEFINE_PROP_BOOL("zba", RISCVCPU, cfg.ext_zba, true), 1042 DEFINE_PROP_BOOL("zbb", RISCVCPU, cfg.ext_zbb, true), 1043 DEFINE_PROP_BOOL("zbc", RISCVCPU, cfg.ext_zbc, true), 1044 DEFINE_PROP_BOOL("zbkb", RISCVCPU, cfg.ext_zbkb, false), 1045 DEFINE_PROP_BOOL("zbkc", RISCVCPU, cfg.ext_zbkc, false), 1046 DEFINE_PROP_BOOL("zbkx", RISCVCPU, cfg.ext_zbkx, false), 1047 DEFINE_PROP_BOOL("zbs", RISCVCPU, cfg.ext_zbs, true), 1048 DEFINE_PROP_BOOL("zk", RISCVCPU, cfg.ext_zk, false), 1049 DEFINE_PROP_BOOL("zkn", RISCVCPU, cfg.ext_zkn, false), 1050 DEFINE_PROP_BOOL("zknd", RISCVCPU, cfg.ext_zknd, false), 1051 DEFINE_PROP_BOOL("zkne", RISCVCPU, cfg.ext_zkne, false), 1052 DEFINE_PROP_BOOL("zknh", RISCVCPU, cfg.ext_zknh, false), 1053 DEFINE_PROP_BOOL("zkr", RISCVCPU, cfg.ext_zkr, false), 1054 DEFINE_PROP_BOOL("zks", RISCVCPU, cfg.ext_zks, false), 1055 DEFINE_PROP_BOOL("zksed", RISCVCPU, cfg.ext_zksed, false), 1056 DEFINE_PROP_BOOL("zksh", RISCVCPU, cfg.ext_zksh, false), 1057 DEFINE_PROP_BOOL("zkt", RISCVCPU, cfg.ext_zkt, false), 1058 1059 DEFINE_PROP_BOOL("zdinx", RISCVCPU, cfg.ext_zdinx, false), 1060 DEFINE_PROP_BOOL("zfinx", RISCVCPU, cfg.ext_zfinx, false), 1061 DEFINE_PROP_BOOL("zhinx", RISCVCPU, cfg.ext_zhinx, false), 1062 DEFINE_PROP_BOOL("zhinxmin", RISCVCPU, cfg.ext_zhinxmin, false), 1063 1064 DEFINE_PROP_BOOL("zmmul", RISCVCPU, cfg.ext_zmmul, false), 1065 1066 /* Vendor-specific custom extensions */ 1067 DEFINE_PROP_BOOL("xventanacondops", RISCVCPU, cfg.ext_XVentanaCondOps, false), 1068 1069 /* These are experimental so mark with 'x-' */ 1070 DEFINE_PROP_BOOL("x-j", RISCVCPU, cfg.ext_j, false), 1071 /* ePMP 0.9.3 */ 1072 DEFINE_PROP_BOOL("x-epmp", RISCVCPU, cfg.epmp, false), 1073 DEFINE_PROP_BOOL("x-smaia", RISCVCPU, cfg.ext_smaia, false), 1074 DEFINE_PROP_BOOL("x-ssaia", RISCVCPU, cfg.ext_ssaia, false), 1075 1076 DEFINE_PROP_END_OF_LIST(), 1077 }; 1078 1079 static void register_cpu_props(DeviceState *dev) 1080 { 1081 Property *prop; 1082 1083 for (prop = riscv_cpu_extensions; prop && prop->name; prop++) { 1084 qdev_property_add_static(dev, prop); 1085 } 1086 } 1087 1088 static Property riscv_cpu_properties[] = { 1089 DEFINE_PROP_BOOL("debug", RISCVCPU, cfg.debug, true), 1090 1091 DEFINE_PROP_UINT32("mvendorid", RISCVCPU, cfg.mvendorid, 0), 1092 DEFINE_PROP_UINT64("marchid", RISCVCPU, cfg.marchid, RISCV_CPU_MARCHID), 1093 DEFINE_PROP_UINT64("mimpid", RISCVCPU, cfg.mimpid, RISCV_CPU_MIMPID), 1094 1095 #ifndef CONFIG_USER_ONLY 1096 DEFINE_PROP_UINT64("resetvec", RISCVCPU, env.resetvec, DEFAULT_RSTVEC), 1097 #endif 1098 1099 DEFINE_PROP_BOOL("short-isa-string", RISCVCPU, cfg.short_isa_string, false), 1100 1101 DEFINE_PROP_BOOL("rvv_ta_all_1s", RISCVCPU, cfg.rvv_ta_all_1s, false), 1102 DEFINE_PROP_BOOL("rvv_ma_all_1s", RISCVCPU, cfg.rvv_ma_all_1s, false), 1103 DEFINE_PROP_END_OF_LIST(), 1104 }; 1105 1106 static gchar *riscv_gdb_arch_name(CPUState *cs) 1107 { 1108 RISCVCPU *cpu = RISCV_CPU(cs); 1109 CPURISCVState *env = &cpu->env; 1110 1111 switch (riscv_cpu_mxl(env)) { 1112 case MXL_RV32: 1113 return g_strdup("riscv:rv32"); 1114 case MXL_RV64: 1115 case MXL_RV128: 1116 return g_strdup("riscv:rv64"); 1117 default: 1118 g_assert_not_reached(); 1119 } 1120 } 1121 1122 static const char *riscv_gdb_get_dynamic_xml(CPUState *cs, const char *xmlname) 1123 { 1124 RISCVCPU *cpu = RISCV_CPU(cs); 1125 1126 if (strcmp(xmlname, "riscv-csr.xml") == 0) { 1127 return cpu->dyn_csr_xml; 1128 } else if (strcmp(xmlname, "riscv-vector.xml") == 0) { 1129 return cpu->dyn_vreg_xml; 1130 } 1131 1132 return NULL; 1133 } 1134 1135 #ifndef CONFIG_USER_ONLY 1136 #include "hw/core/sysemu-cpu-ops.h" 1137 1138 static const struct SysemuCPUOps riscv_sysemu_ops = { 1139 .get_phys_page_debug = riscv_cpu_get_phys_page_debug, 1140 .write_elf64_note = riscv_cpu_write_elf64_note, 1141 .write_elf32_note = riscv_cpu_write_elf32_note, 1142 .legacy_vmsd = &vmstate_riscv_cpu, 1143 }; 1144 #endif 1145 1146 #include "hw/core/tcg-cpu-ops.h" 1147 1148 static const struct TCGCPUOps riscv_tcg_ops = { 1149 .initialize = riscv_translate_init, 1150 .synchronize_from_tb = riscv_cpu_synchronize_from_tb, 1151 .restore_state_to_opc = riscv_restore_state_to_opc, 1152 1153 #ifndef CONFIG_USER_ONLY 1154 .tlb_fill = riscv_cpu_tlb_fill, 1155 .cpu_exec_interrupt = riscv_cpu_exec_interrupt, 1156 .do_interrupt = riscv_cpu_do_interrupt, 1157 .do_transaction_failed = riscv_cpu_do_transaction_failed, 1158 .do_unaligned_access = riscv_cpu_do_unaligned_access, 1159 .debug_excp_handler = riscv_cpu_debug_excp_handler, 1160 .debug_check_breakpoint = riscv_cpu_debug_check_breakpoint, 1161 .debug_check_watchpoint = riscv_cpu_debug_check_watchpoint, 1162 #endif /* !CONFIG_USER_ONLY */ 1163 }; 1164 1165 static void riscv_cpu_class_init(ObjectClass *c, void *data) 1166 { 1167 RISCVCPUClass *mcc = RISCV_CPU_CLASS(c); 1168 CPUClass *cc = CPU_CLASS(c); 1169 DeviceClass *dc = DEVICE_CLASS(c); 1170 ResettableClass *rc = RESETTABLE_CLASS(c); 1171 1172 device_class_set_parent_realize(dc, riscv_cpu_realize, 1173 &mcc->parent_realize); 1174 1175 resettable_class_set_parent_phases(rc, NULL, riscv_cpu_reset_hold, NULL, 1176 &mcc->parent_phases); 1177 1178 cc->class_by_name = riscv_cpu_class_by_name; 1179 cc->has_work = riscv_cpu_has_work; 1180 cc->dump_state = riscv_cpu_dump_state; 1181 cc->set_pc = riscv_cpu_set_pc; 1182 cc->get_pc = riscv_cpu_get_pc; 1183 cc->gdb_read_register = riscv_cpu_gdb_read_register; 1184 cc->gdb_write_register = riscv_cpu_gdb_write_register; 1185 cc->gdb_num_core_regs = 33; 1186 cc->gdb_stop_before_watchpoint = true; 1187 cc->disas_set_info = riscv_cpu_disas_set_info; 1188 #ifndef CONFIG_USER_ONLY 1189 cc->sysemu_ops = &riscv_sysemu_ops; 1190 #endif 1191 cc->gdb_arch_name = riscv_gdb_arch_name; 1192 cc->gdb_get_dynamic_xml = riscv_gdb_get_dynamic_xml; 1193 cc->tcg_ops = &riscv_tcg_ops; 1194 1195 device_class_set_props(dc, riscv_cpu_properties); 1196 } 1197 1198 static void riscv_isa_string_ext(RISCVCPU *cpu, char **isa_str, int max_str_len) 1199 { 1200 char *old = *isa_str; 1201 char *new = *isa_str; 1202 int i; 1203 1204 for (i = 0; i < ARRAY_SIZE(isa_edata_arr); i++) { 1205 if (isa_edata_arr[i].multi_letter && 1206 isa_ext_is_enabled(cpu, &isa_edata_arr[i])) { 1207 new = g_strconcat(old, "_", isa_edata_arr[i].name, NULL); 1208 g_free(old); 1209 old = new; 1210 } 1211 } 1212 1213 *isa_str = new; 1214 } 1215 1216 char *riscv_isa_string(RISCVCPU *cpu) 1217 { 1218 int i; 1219 const size_t maxlen = sizeof("rv128") + sizeof(riscv_single_letter_exts); 1220 char *isa_str = g_new(char, maxlen); 1221 char *p = isa_str + snprintf(isa_str, maxlen, "rv%d", TARGET_LONG_BITS); 1222 for (i = 0; i < sizeof(riscv_single_letter_exts) - 1; i++) { 1223 if (cpu->env.misa_ext & RV(riscv_single_letter_exts[i])) { 1224 *p++ = qemu_tolower(riscv_single_letter_exts[i]); 1225 } 1226 } 1227 *p = '\0'; 1228 if (!cpu->cfg.short_isa_string) { 1229 riscv_isa_string_ext(cpu, &isa_str, maxlen); 1230 } 1231 return isa_str; 1232 } 1233 1234 static gint riscv_cpu_list_compare(gconstpointer a, gconstpointer b) 1235 { 1236 ObjectClass *class_a = (ObjectClass *)a; 1237 ObjectClass *class_b = (ObjectClass *)b; 1238 const char *name_a, *name_b; 1239 1240 name_a = object_class_get_name(class_a); 1241 name_b = object_class_get_name(class_b); 1242 return strcmp(name_a, name_b); 1243 } 1244 1245 static void riscv_cpu_list_entry(gpointer data, gpointer user_data) 1246 { 1247 const char *typename = object_class_get_name(OBJECT_CLASS(data)); 1248 int len = strlen(typename) - strlen(RISCV_CPU_TYPE_SUFFIX); 1249 1250 qemu_printf("%.*s\n", len, typename); 1251 } 1252 1253 void riscv_cpu_list(void) 1254 { 1255 GSList *list; 1256 1257 list = object_class_get_list(TYPE_RISCV_CPU, false); 1258 list = g_slist_sort(list, riscv_cpu_list_compare); 1259 g_slist_foreach(list, riscv_cpu_list_entry, NULL); 1260 g_slist_free(list); 1261 } 1262 1263 #define DEFINE_CPU(type_name, initfn) \ 1264 { \ 1265 .name = type_name, \ 1266 .parent = TYPE_RISCV_CPU, \ 1267 .instance_init = initfn \ 1268 } 1269 1270 static const TypeInfo riscv_cpu_type_infos[] = { 1271 { 1272 .name = TYPE_RISCV_CPU, 1273 .parent = TYPE_CPU, 1274 .instance_size = sizeof(RISCVCPU), 1275 .instance_align = __alignof__(RISCVCPU), 1276 .instance_init = riscv_cpu_init, 1277 .abstract = true, 1278 .class_size = sizeof(RISCVCPUClass), 1279 .class_init = riscv_cpu_class_init, 1280 }, 1281 DEFINE_CPU(TYPE_RISCV_CPU_ANY, riscv_any_cpu_init), 1282 #if defined(CONFIG_KVM) 1283 DEFINE_CPU(TYPE_RISCV_CPU_HOST, riscv_host_cpu_init), 1284 #endif 1285 #if defined(TARGET_RISCV32) 1286 DEFINE_CPU(TYPE_RISCV_CPU_BASE32, rv32_base_cpu_init), 1287 DEFINE_CPU(TYPE_RISCV_CPU_IBEX, rv32_ibex_cpu_init), 1288 DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E31, rv32_sifive_e_cpu_init), 1289 DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E34, rv32_imafcu_nommu_cpu_init), 1290 DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_U34, rv32_sifive_u_cpu_init), 1291 #elif defined(TARGET_RISCV64) 1292 DEFINE_CPU(TYPE_RISCV_CPU_BASE64, rv64_base_cpu_init), 1293 DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E51, rv64_sifive_e_cpu_init), 1294 DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_U54, rv64_sifive_u_cpu_init), 1295 DEFINE_CPU(TYPE_RISCV_CPU_SHAKTI_C, rv64_sifive_u_cpu_init), 1296 DEFINE_CPU(TYPE_RISCV_CPU_BASE128, rv128_base_cpu_init), 1297 #endif 1298 }; 1299 1300 DEFINE_TYPES(riscv_cpu_type_infos) 1301