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