1 /* 2 * QEMU ARM CPU 3 * 4 * Copyright (c) 2012 SUSE LINUX Products GmbH 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, see 18 * <http://www.gnu.org/licenses/gpl-2.0.html> 19 */ 20 21 #include "qemu/osdep.h" 22 #include "qemu/qemu-print.h" 23 #include "qemu/timer.h" 24 #include "qemu/log.h" 25 #include "exec/page-vary.h" 26 #include "target/arm/idau.h" 27 #include "qemu/module.h" 28 #include "qapi/error.h" 29 #include "cpu.h" 30 #ifdef CONFIG_TCG 31 #include "exec/translation-block.h" 32 #include "hw/core/tcg-cpu-ops.h" 33 #endif /* CONFIG_TCG */ 34 #include "internals.h" 35 #include "cpu-features.h" 36 #include "exec/exec-all.h" 37 #include "hw/qdev-properties.h" 38 #if !defined(CONFIG_USER_ONLY) 39 #include "hw/loader.h" 40 #include "hw/boards.h" 41 #ifdef CONFIG_TCG 42 #include "hw/intc/armv7m_nvic.h" 43 #endif /* CONFIG_TCG */ 44 #endif /* !CONFIG_USER_ONLY */ 45 #include "system/tcg.h" 46 #include "system/qtest.h" 47 #include "system/hw_accel.h" 48 #include "kvm_arm.h" 49 #include "disas/capstone.h" 50 #include "fpu/softfloat.h" 51 #include "cpregs.h" 52 #include "target/arm/cpu-qom.h" 53 #include "target/arm/gtimer.h" 54 55 static void arm_cpu_set_pc(CPUState *cs, vaddr value) 56 { 57 ARMCPU *cpu = ARM_CPU(cs); 58 CPUARMState *env = &cpu->env; 59 60 if (is_a64(env)) { 61 env->pc = value; 62 env->thumb = false; 63 } else { 64 env->regs[15] = value & ~1; 65 env->thumb = value & 1; 66 } 67 } 68 69 static vaddr arm_cpu_get_pc(CPUState *cs) 70 { 71 ARMCPU *cpu = ARM_CPU(cs); 72 CPUARMState *env = &cpu->env; 73 74 if (is_a64(env)) { 75 return env->pc; 76 } else { 77 return env->regs[15]; 78 } 79 } 80 81 #ifdef CONFIG_TCG 82 void arm_cpu_synchronize_from_tb(CPUState *cs, 83 const TranslationBlock *tb) 84 { 85 /* The program counter is always up to date with CF_PCREL. */ 86 if (!(tb_cflags(tb) & CF_PCREL)) { 87 CPUARMState *env = cpu_env(cs); 88 /* 89 * It's OK to look at env for the current mode here, because it's 90 * never possible for an AArch64 TB to chain to an AArch32 TB. 91 */ 92 if (is_a64(env)) { 93 env->pc = tb->pc; 94 } else { 95 env->regs[15] = tb->pc; 96 } 97 } 98 } 99 100 void arm_restore_state_to_opc(CPUState *cs, 101 const TranslationBlock *tb, 102 const uint64_t *data) 103 { 104 CPUARMState *env = cpu_env(cs); 105 106 if (is_a64(env)) { 107 if (tb_cflags(tb) & CF_PCREL) { 108 env->pc = (env->pc & TARGET_PAGE_MASK) | data[0]; 109 } else { 110 env->pc = data[0]; 111 } 112 env->condexec_bits = 0; 113 env->exception.syndrome = data[2] << ARM_INSN_START_WORD2_SHIFT; 114 } else { 115 if (tb_cflags(tb) & CF_PCREL) { 116 env->regs[15] = (env->regs[15] & TARGET_PAGE_MASK) | data[0]; 117 } else { 118 env->regs[15] = data[0]; 119 } 120 env->condexec_bits = data[1]; 121 env->exception.syndrome = data[2] << ARM_INSN_START_WORD2_SHIFT; 122 } 123 } 124 #endif /* CONFIG_TCG */ 125 126 /* 127 * With SCTLR_ELx.NMI == 0, IRQ with Superpriority is masked identically with 128 * IRQ without Superpriority. Moreover, if the GIC is configured so that 129 * FEAT_GICv3_NMI is only set if FEAT_NMI is set, then we won't ever see 130 * CPU_INTERRUPT_*NMI anyway. So we might as well accept NMI here 131 * unconditionally. 132 */ 133 static bool arm_cpu_has_work(CPUState *cs) 134 { 135 ARMCPU *cpu = ARM_CPU(cs); 136 137 return (cpu->power_state != PSCI_OFF) 138 && cs->interrupt_request & 139 (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD 140 | CPU_INTERRUPT_NMI | CPU_INTERRUPT_VINMI | CPU_INTERRUPT_VFNMI 141 | CPU_INTERRUPT_VFIQ | CPU_INTERRUPT_VIRQ | CPU_INTERRUPT_VSERR 142 | CPU_INTERRUPT_EXITTB); 143 } 144 145 static int arm_cpu_mmu_index(CPUState *cs, bool ifetch) 146 { 147 return arm_env_mmu_index(cpu_env(cs)); 148 } 149 150 void arm_register_pre_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook, 151 void *opaque) 152 { 153 ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1); 154 155 entry->hook = hook; 156 entry->opaque = opaque; 157 158 QLIST_INSERT_HEAD(&cpu->pre_el_change_hooks, entry, node); 159 } 160 161 void arm_register_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook, 162 void *opaque) 163 { 164 ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1); 165 166 entry->hook = hook; 167 entry->opaque = opaque; 168 169 QLIST_INSERT_HEAD(&cpu->el_change_hooks, entry, node); 170 } 171 172 /* 173 * Set the float_status behaviour to match the Arm defaults: 174 * * tininess-before-rounding 175 * * 2-input NaN propagation prefers SNaN over QNaN, and then 176 * operand A over operand B (see FPProcessNaNs() pseudocode) 177 * * 3-input NaN propagation prefers SNaN over QNaN, and then 178 * operand C over A over B (see FPProcessNaNs3() pseudocode, 179 * but note that for QEMU muladd is a * b + c, whereas for 180 * the pseudocode function the arguments are in the order c, a, b. 181 * * 0 * Inf + NaN returns the default NaN if the input NaN is quiet, 182 * and the input NaN if it is signalling 183 * * Default NaN has sign bit clear, msb frac bit set 184 */ 185 static void arm_set_default_fp_behaviours(float_status *s) 186 { 187 set_float_detect_tininess(float_tininess_before_rounding, s); 188 set_float_2nan_prop_rule(float_2nan_prop_s_ab, s); 189 set_float_3nan_prop_rule(float_3nan_prop_s_cab, s); 190 set_float_infzeronan_rule(float_infzeronan_dnan_if_qnan, s); 191 set_float_default_nan_pattern(0b01000000, s); 192 } 193 194 static void cp_reg_reset(gpointer key, gpointer value, gpointer opaque) 195 { 196 /* Reset a single ARMCPRegInfo register */ 197 ARMCPRegInfo *ri = value; 198 ARMCPU *cpu = opaque; 199 200 if (ri->type & (ARM_CP_SPECIAL_MASK | ARM_CP_ALIAS)) { 201 return; 202 } 203 204 if (ri->resetfn) { 205 ri->resetfn(&cpu->env, ri); 206 return; 207 } 208 209 /* A zero offset is never possible as it would be regs[0] 210 * so we use it to indicate that reset is being handled elsewhere. 211 * This is basically only used for fields in non-core coprocessors 212 * (like the pxa2xx ones). 213 */ 214 if (!ri->fieldoffset) { 215 return; 216 } 217 218 if (cpreg_field_is_64bit(ri)) { 219 CPREG_FIELD64(&cpu->env, ri) = ri->resetvalue; 220 } else { 221 CPREG_FIELD32(&cpu->env, ri) = ri->resetvalue; 222 } 223 } 224 225 static void cp_reg_check_reset(gpointer key, gpointer value, gpointer opaque) 226 { 227 /* Purely an assertion check: we've already done reset once, 228 * so now check that running the reset for the cpreg doesn't 229 * change its value. This traps bugs where two different cpregs 230 * both try to reset the same state field but to different values. 231 */ 232 ARMCPRegInfo *ri = value; 233 ARMCPU *cpu = opaque; 234 uint64_t oldvalue, newvalue; 235 236 if (ri->type & (ARM_CP_SPECIAL_MASK | ARM_CP_ALIAS | ARM_CP_NO_RAW)) { 237 return; 238 } 239 240 oldvalue = read_raw_cp_reg(&cpu->env, ri); 241 cp_reg_reset(key, value, opaque); 242 newvalue = read_raw_cp_reg(&cpu->env, ri); 243 assert(oldvalue == newvalue); 244 } 245 246 static void arm_cpu_reset_hold(Object *obj, ResetType type) 247 { 248 CPUState *cs = CPU(obj); 249 ARMCPU *cpu = ARM_CPU(cs); 250 ARMCPUClass *acc = ARM_CPU_GET_CLASS(obj); 251 CPUARMState *env = &cpu->env; 252 253 if (acc->parent_phases.hold) { 254 acc->parent_phases.hold(obj, type); 255 } 256 257 memset(env, 0, offsetof(CPUARMState, end_reset_fields)); 258 259 g_hash_table_foreach(cpu->cp_regs, cp_reg_reset, cpu); 260 g_hash_table_foreach(cpu->cp_regs, cp_reg_check_reset, cpu); 261 262 env->vfp.xregs[ARM_VFP_FPSID] = cpu->reset_fpsid; 263 env->vfp.xregs[ARM_VFP_MVFR0] = cpu->isar.mvfr0; 264 env->vfp.xregs[ARM_VFP_MVFR1] = cpu->isar.mvfr1; 265 env->vfp.xregs[ARM_VFP_MVFR2] = cpu->isar.mvfr2; 266 267 cpu->power_state = cs->start_powered_off ? PSCI_OFF : PSCI_ON; 268 269 if (arm_feature(env, ARM_FEATURE_IWMMXT)) { 270 env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q'; 271 } 272 273 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 274 /* 64 bit CPUs always start in 64 bit mode */ 275 env->aarch64 = true; 276 #if defined(CONFIG_USER_ONLY) 277 env->pstate = PSTATE_MODE_EL0t; 278 /* Userspace expects access to DC ZVA, CTL_EL0 and the cache ops */ 279 env->cp15.sctlr_el[1] |= SCTLR_UCT | SCTLR_UCI | SCTLR_DZE; 280 /* Enable all PAC keys. */ 281 env->cp15.sctlr_el[1] |= (SCTLR_EnIA | SCTLR_EnIB | 282 SCTLR_EnDA | SCTLR_EnDB); 283 /* Trap on btype=3 for PACIxSP. */ 284 env->cp15.sctlr_el[1] |= SCTLR_BT0; 285 /* Trap on implementation defined registers. */ 286 if (cpu_isar_feature(aa64_tidcp1, cpu)) { 287 env->cp15.sctlr_el[1] |= SCTLR_TIDCP; 288 } 289 /* and to the FP/Neon instructions */ 290 env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1, 291 CPACR_EL1, FPEN, 3); 292 /* and to the SVE instructions, with default vector length */ 293 if (cpu_isar_feature(aa64_sve, cpu)) { 294 env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1, 295 CPACR_EL1, ZEN, 3); 296 env->vfp.zcr_el[1] = cpu->sve_default_vq - 1; 297 } 298 /* and for SME instructions, with default vector length, and TPIDR2 */ 299 if (cpu_isar_feature(aa64_sme, cpu)) { 300 env->cp15.sctlr_el[1] |= SCTLR_EnTP2; 301 env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1, 302 CPACR_EL1, SMEN, 3); 303 env->vfp.smcr_el[1] = cpu->sme_default_vq - 1; 304 if (cpu_isar_feature(aa64_sme_fa64, cpu)) { 305 env->vfp.smcr_el[1] = FIELD_DP64(env->vfp.smcr_el[1], 306 SMCR, FA64, 1); 307 } 308 } 309 /* 310 * Enable 48-bit address space (TODO: take reserved_va into account). 311 * Enable TBI0 but not TBI1. 312 * Note that this must match useronly_clean_ptr. 313 */ 314 env->cp15.tcr_el[1] = 5 | (1ULL << 37); 315 316 /* Enable MTE */ 317 if (cpu_isar_feature(aa64_mte, cpu)) { 318 /* Enable tag access, but leave TCF0 as No Effect (0). */ 319 env->cp15.sctlr_el[1] |= SCTLR_ATA0; 320 /* 321 * Exclude all tags, so that tag 0 is always used. 322 * This corresponds to Linux current->thread.gcr_incl = 0. 323 * 324 * Set RRND, so that helper_irg() will generate a seed later. 325 * Here in cpu_reset(), the crypto subsystem has not yet been 326 * initialized. 327 */ 328 env->cp15.gcr_el1 = 0x1ffff; 329 } 330 /* 331 * Disable access to SCXTNUM_EL0 from CSV2_1p2. 332 * This is not yet exposed from the Linux kernel in any way. 333 */ 334 env->cp15.sctlr_el[1] |= SCTLR_TSCXT; 335 /* Disable access to Debug Communication Channel (DCC). */ 336 env->cp15.mdscr_el1 |= 1 << 12; 337 /* Enable FEAT_MOPS */ 338 env->cp15.sctlr_el[1] |= SCTLR_MSCEN; 339 #else 340 /* Reset into the highest available EL */ 341 if (arm_feature(env, ARM_FEATURE_EL3)) { 342 env->pstate = PSTATE_MODE_EL3h; 343 } else if (arm_feature(env, ARM_FEATURE_EL2)) { 344 env->pstate = PSTATE_MODE_EL2h; 345 } else { 346 env->pstate = PSTATE_MODE_EL1h; 347 } 348 349 /* Sample rvbar at reset. */ 350 env->cp15.rvbar = cpu->rvbar_prop; 351 env->pc = env->cp15.rvbar; 352 #endif 353 } else { 354 #if defined(CONFIG_USER_ONLY) 355 /* Userspace expects access to cp10 and cp11 for FP/Neon */ 356 env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1, 357 CPACR, CP10, 3); 358 env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1, 359 CPACR, CP11, 3); 360 #endif 361 if (arm_feature(env, ARM_FEATURE_V8)) { 362 env->cp15.rvbar = cpu->rvbar_prop; 363 env->regs[15] = cpu->rvbar_prop; 364 } 365 } 366 367 #if defined(CONFIG_USER_ONLY) 368 env->uncached_cpsr = ARM_CPU_MODE_USR; 369 /* For user mode we must enable access to coprocessors */ 370 env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30; 371 if (arm_feature(env, ARM_FEATURE_IWMMXT)) { 372 env->cp15.c15_cpar = 3; 373 } else if (arm_feature(env, ARM_FEATURE_XSCALE)) { 374 env->cp15.c15_cpar = 1; 375 } 376 #else 377 378 /* 379 * If the highest available EL is EL2, AArch32 will start in Hyp 380 * mode; otherwise it starts in SVC. Note that if we start in 381 * AArch64 then these values in the uncached_cpsr will be ignored. 382 */ 383 if (arm_feature(env, ARM_FEATURE_EL2) && 384 !arm_feature(env, ARM_FEATURE_EL3)) { 385 env->uncached_cpsr = ARM_CPU_MODE_HYP; 386 } else { 387 env->uncached_cpsr = ARM_CPU_MODE_SVC; 388 } 389 env->daif = PSTATE_D | PSTATE_A | PSTATE_I | PSTATE_F; 390 391 /* AArch32 has a hard highvec setting of 0xFFFF0000. If we are currently 392 * executing as AArch32 then check if highvecs are enabled and 393 * adjust the PC accordingly. 394 */ 395 if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) { 396 env->regs[15] = 0xFFFF0000; 397 } 398 399 env->vfp.xregs[ARM_VFP_FPEXC] = 0; 400 #endif 401 402 if (arm_feature(env, ARM_FEATURE_M)) { 403 #ifndef CONFIG_USER_ONLY 404 uint32_t initial_msp; /* Loaded from 0x0 */ 405 uint32_t initial_pc; /* Loaded from 0x4 */ 406 uint8_t *rom; 407 uint32_t vecbase; 408 #endif 409 410 if (cpu_isar_feature(aa32_lob, cpu)) { 411 /* 412 * LTPSIZE is constant 4 if MVE not implemented, and resets 413 * to an UNKNOWN value if MVE is implemented. We choose to 414 * always reset to 4. 415 */ 416 env->v7m.ltpsize = 4; 417 /* The LTPSIZE field in FPDSCR is constant and reads as 4. */ 418 env->v7m.fpdscr[M_REG_NS] = 4 << FPCR_LTPSIZE_SHIFT; 419 env->v7m.fpdscr[M_REG_S] = 4 << FPCR_LTPSIZE_SHIFT; 420 } 421 422 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 423 env->v7m.secure = true; 424 } else { 425 /* This bit resets to 0 if security is supported, but 1 if 426 * it is not. The bit is not present in v7M, but we set it 427 * here so we can avoid having to make checks on it conditional 428 * on ARM_FEATURE_V8 (we don't let the guest see the bit). 429 */ 430 env->v7m.aircr = R_V7M_AIRCR_BFHFNMINS_MASK; 431 /* 432 * Set NSACR to indicate "NS access permitted to everything"; 433 * this avoids having to have all the tests of it being 434 * conditional on ARM_FEATURE_M_SECURITY. Note also that from 435 * v8.1M the guest-visible value of NSACR in a CPU without the 436 * Security Extension is 0xcff. 437 */ 438 env->v7m.nsacr = 0xcff; 439 } 440 441 /* In v7M the reset value of this bit is IMPDEF, but ARM recommends 442 * that it resets to 1, so QEMU always does that rather than making 443 * it dependent on CPU model. In v8M it is RES1. 444 */ 445 env->v7m.ccr[M_REG_NS] = R_V7M_CCR_STKALIGN_MASK; 446 env->v7m.ccr[M_REG_S] = R_V7M_CCR_STKALIGN_MASK; 447 if (arm_feature(env, ARM_FEATURE_V8)) { 448 /* in v8M the NONBASETHRDENA bit [0] is RES1 */ 449 env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_NONBASETHRDENA_MASK; 450 env->v7m.ccr[M_REG_S] |= R_V7M_CCR_NONBASETHRDENA_MASK; 451 } 452 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) { 453 env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_UNALIGN_TRP_MASK; 454 env->v7m.ccr[M_REG_S] |= R_V7M_CCR_UNALIGN_TRP_MASK; 455 } 456 457 if (cpu_isar_feature(aa32_vfp_simd, cpu)) { 458 env->v7m.fpccr[M_REG_NS] = R_V7M_FPCCR_ASPEN_MASK; 459 env->v7m.fpccr[M_REG_S] = R_V7M_FPCCR_ASPEN_MASK | 460 R_V7M_FPCCR_LSPEN_MASK | R_V7M_FPCCR_S_MASK; 461 } 462 463 #ifndef CONFIG_USER_ONLY 464 /* Unlike A/R profile, M profile defines the reset LR value */ 465 env->regs[14] = 0xffffffff; 466 467 env->v7m.vecbase[M_REG_S] = cpu->init_svtor & 0xffffff80; 468 env->v7m.vecbase[M_REG_NS] = cpu->init_nsvtor & 0xffffff80; 469 470 /* Load the initial SP and PC from offset 0 and 4 in the vector table */ 471 vecbase = env->v7m.vecbase[env->v7m.secure]; 472 rom = rom_ptr_for_as(cs->as, vecbase, 8); 473 if (rom) { 474 /* Address zero is covered by ROM which hasn't yet been 475 * copied into physical memory. 476 */ 477 initial_msp = ldl_p(rom); 478 initial_pc = ldl_p(rom + 4); 479 } else { 480 /* Address zero not covered by a ROM blob, or the ROM blob 481 * is in non-modifiable memory and this is a second reset after 482 * it got copied into memory. In the latter case, rom_ptr 483 * will return a NULL pointer and we should use ldl_phys instead. 484 */ 485 initial_msp = ldl_phys(cs->as, vecbase); 486 initial_pc = ldl_phys(cs->as, vecbase + 4); 487 } 488 489 qemu_log_mask(CPU_LOG_INT, 490 "Loaded reset SP 0x%x PC 0x%x from vector table\n", 491 initial_msp, initial_pc); 492 493 env->regs[13] = initial_msp & 0xFFFFFFFC; 494 env->regs[15] = initial_pc & ~1; 495 env->thumb = initial_pc & 1; 496 #else 497 /* 498 * For user mode we run non-secure and with access to the FPU. 499 * The FPU context is active (ie does not need further setup) 500 * and is owned by non-secure. 501 */ 502 env->v7m.secure = false; 503 env->v7m.nsacr = 0xcff; 504 env->v7m.cpacr[M_REG_NS] = 0xf0ffff; 505 env->v7m.fpccr[M_REG_S] &= 506 ~(R_V7M_FPCCR_LSPEN_MASK | R_V7M_FPCCR_S_MASK); 507 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_FPCA_MASK; 508 #endif 509 } 510 511 /* M profile requires that reset clears the exclusive monitor; 512 * A profile does not, but clearing it makes more sense than having it 513 * set with an exclusive access on address zero. 514 */ 515 arm_clear_exclusive(env); 516 517 if (arm_feature(env, ARM_FEATURE_PMSA)) { 518 if (cpu->pmsav7_dregion > 0) { 519 if (arm_feature(env, ARM_FEATURE_V8)) { 520 memset(env->pmsav8.rbar[M_REG_NS], 0, 521 sizeof(*env->pmsav8.rbar[M_REG_NS]) 522 * cpu->pmsav7_dregion); 523 memset(env->pmsav8.rlar[M_REG_NS], 0, 524 sizeof(*env->pmsav8.rlar[M_REG_NS]) 525 * cpu->pmsav7_dregion); 526 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 527 memset(env->pmsav8.rbar[M_REG_S], 0, 528 sizeof(*env->pmsav8.rbar[M_REG_S]) 529 * cpu->pmsav7_dregion); 530 memset(env->pmsav8.rlar[M_REG_S], 0, 531 sizeof(*env->pmsav8.rlar[M_REG_S]) 532 * cpu->pmsav7_dregion); 533 } 534 } else if (arm_feature(env, ARM_FEATURE_V7)) { 535 memset(env->pmsav7.drbar, 0, 536 sizeof(*env->pmsav7.drbar) * cpu->pmsav7_dregion); 537 memset(env->pmsav7.drsr, 0, 538 sizeof(*env->pmsav7.drsr) * cpu->pmsav7_dregion); 539 memset(env->pmsav7.dracr, 0, 540 sizeof(*env->pmsav7.dracr) * cpu->pmsav7_dregion); 541 } 542 } 543 544 if (cpu->pmsav8r_hdregion > 0) { 545 memset(env->pmsav8.hprbar, 0, 546 sizeof(*env->pmsav8.hprbar) * cpu->pmsav8r_hdregion); 547 memset(env->pmsav8.hprlar, 0, 548 sizeof(*env->pmsav8.hprlar) * cpu->pmsav8r_hdregion); 549 } 550 551 env->pmsav7.rnr[M_REG_NS] = 0; 552 env->pmsav7.rnr[M_REG_S] = 0; 553 env->pmsav8.mair0[M_REG_NS] = 0; 554 env->pmsav8.mair0[M_REG_S] = 0; 555 env->pmsav8.mair1[M_REG_NS] = 0; 556 env->pmsav8.mair1[M_REG_S] = 0; 557 } 558 559 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 560 if (cpu->sau_sregion > 0) { 561 memset(env->sau.rbar, 0, sizeof(*env->sau.rbar) * cpu->sau_sregion); 562 memset(env->sau.rlar, 0, sizeof(*env->sau.rlar) * cpu->sau_sregion); 563 } 564 env->sau.rnr = 0; 565 /* SAU_CTRL reset value is IMPDEF; we choose 0, which is what 566 * the Cortex-M33 does. 567 */ 568 env->sau.ctrl = 0; 569 } 570 571 set_flush_to_zero(1, &env->vfp.standard_fp_status); 572 set_flush_inputs_to_zero(1, &env->vfp.standard_fp_status); 573 set_default_nan_mode(1, &env->vfp.standard_fp_status); 574 set_default_nan_mode(1, &env->vfp.standard_fp_status_f16); 575 arm_set_default_fp_behaviours(&env->vfp.fp_status); 576 arm_set_default_fp_behaviours(&env->vfp.standard_fp_status); 577 arm_set_default_fp_behaviours(&env->vfp.fp_status_f16); 578 arm_set_default_fp_behaviours(&env->vfp.standard_fp_status_f16); 579 580 #ifndef CONFIG_USER_ONLY 581 if (kvm_enabled()) { 582 kvm_arm_reset_vcpu(cpu); 583 } 584 #endif 585 586 if (tcg_enabled()) { 587 hw_breakpoint_update_all(cpu); 588 hw_watchpoint_update_all(cpu); 589 590 arm_rebuild_hflags(env); 591 } 592 } 593 594 void arm_emulate_firmware_reset(CPUState *cpustate, int target_el) 595 { 596 ARMCPU *cpu = ARM_CPU(cpustate); 597 CPUARMState *env = &cpu->env; 598 bool have_el3 = arm_feature(env, ARM_FEATURE_EL3); 599 bool have_el2 = arm_feature(env, ARM_FEATURE_EL2); 600 601 /* 602 * Check we have the EL we're aiming for. If that is the 603 * highest implemented EL, then cpu_reset has already done 604 * all the work. 605 */ 606 switch (target_el) { 607 case 3: 608 assert(have_el3); 609 return; 610 case 2: 611 assert(have_el2); 612 if (!have_el3) { 613 return; 614 } 615 break; 616 case 1: 617 if (!have_el3 && !have_el2) { 618 return; 619 } 620 break; 621 default: 622 g_assert_not_reached(); 623 } 624 625 if (have_el3) { 626 /* 627 * Set the EL3 state so code can run at EL2. This should match 628 * the requirements set by Linux in its booting spec. 629 */ 630 if (env->aarch64) { 631 env->cp15.scr_el3 |= SCR_RW; 632 if (cpu_isar_feature(aa64_pauth, cpu)) { 633 env->cp15.scr_el3 |= SCR_API | SCR_APK; 634 } 635 if (cpu_isar_feature(aa64_mte, cpu)) { 636 env->cp15.scr_el3 |= SCR_ATA; 637 } 638 if (cpu_isar_feature(aa64_sve, cpu)) { 639 env->cp15.cptr_el[3] |= R_CPTR_EL3_EZ_MASK; 640 env->vfp.zcr_el[3] = 0xf; 641 } 642 if (cpu_isar_feature(aa64_sme, cpu)) { 643 env->cp15.cptr_el[3] |= R_CPTR_EL3_ESM_MASK; 644 env->cp15.scr_el3 |= SCR_ENTP2; 645 env->vfp.smcr_el[3] = 0xf; 646 } 647 if (cpu_isar_feature(aa64_hcx, cpu)) { 648 env->cp15.scr_el3 |= SCR_HXEN; 649 } 650 if (cpu_isar_feature(aa64_fgt, cpu)) { 651 env->cp15.scr_el3 |= SCR_FGTEN; 652 } 653 } 654 655 if (target_el == 2) { 656 /* If the guest is at EL2 then Linux expects the HVC insn to work */ 657 env->cp15.scr_el3 |= SCR_HCE; 658 } 659 660 /* Put CPU into non-secure state */ 661 env->cp15.scr_el3 |= SCR_NS; 662 /* Set NSACR.{CP11,CP10} so NS can access the FPU */ 663 env->cp15.nsacr |= 3 << 10; 664 } 665 666 if (have_el2 && target_el < 2) { 667 /* Set EL2 state so code can run at EL1. */ 668 if (env->aarch64) { 669 env->cp15.hcr_el2 |= HCR_RW; 670 } 671 } 672 673 /* Set the CPU to the desired state */ 674 if (env->aarch64) { 675 env->pstate = aarch64_pstate_mode(target_el, true); 676 } else { 677 static const uint32_t mode_for_el[] = { 678 0, 679 ARM_CPU_MODE_SVC, 680 ARM_CPU_MODE_HYP, 681 ARM_CPU_MODE_SVC, 682 }; 683 684 cpsr_write(env, mode_for_el[target_el], CPSR_M, CPSRWriteRaw); 685 } 686 } 687 688 689 #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY) 690 691 static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx, 692 unsigned int target_el, 693 unsigned int cur_el, bool secure, 694 uint64_t hcr_el2) 695 { 696 CPUARMState *env = cpu_env(cs); 697 bool pstate_unmasked; 698 bool unmasked = false; 699 bool allIntMask = false; 700 701 /* 702 * Don't take exceptions if they target a lower EL. 703 * This check should catch any exceptions that would not be taken 704 * but left pending. 705 */ 706 if (cur_el > target_el) { 707 return false; 708 } 709 710 if (cpu_isar_feature(aa64_nmi, env_archcpu(env)) && 711 env->cp15.sctlr_el[target_el] & SCTLR_NMI && cur_el == target_el) { 712 allIntMask = env->pstate & PSTATE_ALLINT || 713 ((env->cp15.sctlr_el[target_el] & SCTLR_SPINTMASK) && 714 (env->pstate & PSTATE_SP)); 715 } 716 717 switch (excp_idx) { 718 case EXCP_NMI: 719 pstate_unmasked = !allIntMask; 720 break; 721 722 case EXCP_VINMI: 723 if (!(hcr_el2 & HCR_IMO) || (hcr_el2 & HCR_TGE)) { 724 /* VINMIs are only taken when hypervized. */ 725 return false; 726 } 727 return !allIntMask; 728 case EXCP_VFNMI: 729 if (!(hcr_el2 & HCR_FMO) || (hcr_el2 & HCR_TGE)) { 730 /* VFNMIs are only taken when hypervized. */ 731 return false; 732 } 733 return !allIntMask; 734 case EXCP_FIQ: 735 pstate_unmasked = (!(env->daif & PSTATE_F)) && (!allIntMask); 736 break; 737 738 case EXCP_IRQ: 739 pstate_unmasked = (!(env->daif & PSTATE_I)) && (!allIntMask); 740 break; 741 742 case EXCP_VFIQ: 743 if (!(hcr_el2 & HCR_FMO) || (hcr_el2 & HCR_TGE)) { 744 /* VFIQs are only taken when hypervized. */ 745 return false; 746 } 747 return !(env->daif & PSTATE_F) && (!allIntMask); 748 case EXCP_VIRQ: 749 if (!(hcr_el2 & HCR_IMO) || (hcr_el2 & HCR_TGE)) { 750 /* VIRQs are only taken when hypervized. */ 751 return false; 752 } 753 return !(env->daif & PSTATE_I) && (!allIntMask); 754 case EXCP_VSERR: 755 if (!(hcr_el2 & HCR_AMO) || (hcr_el2 & HCR_TGE)) { 756 /* VIRQs are only taken when hypervized. */ 757 return false; 758 } 759 return !(env->daif & PSTATE_A); 760 default: 761 g_assert_not_reached(); 762 } 763 764 /* 765 * Use the target EL, current execution state and SCR/HCR settings to 766 * determine whether the corresponding CPSR bit is used to mask the 767 * interrupt. 768 */ 769 if ((target_el > cur_el) && (target_el != 1)) { 770 /* Exceptions targeting a higher EL may not be maskable */ 771 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 772 switch (target_el) { 773 case 2: 774 /* 775 * According to ARM DDI 0487H.a, an interrupt can be masked 776 * when HCR_E2H and HCR_TGE are both set regardless of the 777 * current Security state. Note that we need to revisit this 778 * part again once we need to support NMI. 779 */ 780 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) { 781 unmasked = true; 782 } 783 break; 784 case 3: 785 /* Interrupt cannot be masked when the target EL is 3 */ 786 unmasked = true; 787 break; 788 default: 789 g_assert_not_reached(); 790 } 791 } else { 792 /* 793 * The old 32-bit-only environment has a more complicated 794 * masking setup. HCR and SCR bits not only affect interrupt 795 * routing but also change the behaviour of masking. 796 */ 797 bool hcr, scr; 798 799 switch (excp_idx) { 800 case EXCP_FIQ: 801 /* 802 * If FIQs are routed to EL3 or EL2 then there are cases where 803 * we override the CPSR.F in determining if the exception is 804 * masked or not. If neither of these are set then we fall back 805 * to the CPSR.F setting otherwise we further assess the state 806 * below. 807 */ 808 hcr = hcr_el2 & HCR_FMO; 809 scr = (env->cp15.scr_el3 & SCR_FIQ); 810 811 /* 812 * When EL3 is 32-bit, the SCR.FW bit controls whether the 813 * CPSR.F bit masks FIQ interrupts when taken in non-secure 814 * state. If SCR.FW is set then FIQs can be masked by CPSR.F 815 * when non-secure but only when FIQs are only routed to EL3. 816 */ 817 scr = scr && !((env->cp15.scr_el3 & SCR_FW) && !hcr); 818 break; 819 case EXCP_IRQ: 820 /* 821 * When EL3 execution state is 32-bit, if HCR.IMO is set then 822 * we may override the CPSR.I masking when in non-secure state. 823 * The SCR.IRQ setting has already been taken into consideration 824 * when setting the target EL, so it does not have a further 825 * affect here. 826 */ 827 hcr = hcr_el2 & HCR_IMO; 828 scr = false; 829 break; 830 default: 831 g_assert_not_reached(); 832 } 833 834 if ((scr || hcr) && !secure) { 835 unmasked = true; 836 } 837 } 838 } 839 840 /* 841 * The PSTATE bits only mask the interrupt if we have not overridden the 842 * ability above. 843 */ 844 return unmasked || pstate_unmasked; 845 } 846 847 static bool arm_cpu_exec_interrupt(CPUState *cs, int interrupt_request) 848 { 849 CPUClass *cc = CPU_GET_CLASS(cs); 850 CPUARMState *env = cpu_env(cs); 851 uint32_t cur_el = arm_current_el(env); 852 bool secure = arm_is_secure(env); 853 uint64_t hcr_el2 = arm_hcr_el2_eff(env); 854 uint32_t target_el; 855 uint32_t excp_idx; 856 857 /* The prioritization of interrupts is IMPLEMENTATION DEFINED. */ 858 859 if (cpu_isar_feature(aa64_nmi, env_archcpu(env)) && 860 (arm_sctlr(env, cur_el) & SCTLR_NMI)) { 861 if (interrupt_request & CPU_INTERRUPT_NMI) { 862 excp_idx = EXCP_NMI; 863 target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure); 864 if (arm_excp_unmasked(cs, excp_idx, target_el, 865 cur_el, secure, hcr_el2)) { 866 goto found; 867 } 868 } 869 if (interrupt_request & CPU_INTERRUPT_VINMI) { 870 excp_idx = EXCP_VINMI; 871 target_el = 1; 872 if (arm_excp_unmasked(cs, excp_idx, target_el, 873 cur_el, secure, hcr_el2)) { 874 goto found; 875 } 876 } 877 if (interrupt_request & CPU_INTERRUPT_VFNMI) { 878 excp_idx = EXCP_VFNMI; 879 target_el = 1; 880 if (arm_excp_unmasked(cs, excp_idx, target_el, 881 cur_el, secure, hcr_el2)) { 882 goto found; 883 } 884 } 885 } else { 886 /* 887 * NMI disabled: interrupts with superpriority are handled 888 * as if they didn't have it 889 */ 890 if (interrupt_request & CPU_INTERRUPT_NMI) { 891 interrupt_request |= CPU_INTERRUPT_HARD; 892 } 893 if (interrupt_request & CPU_INTERRUPT_VINMI) { 894 interrupt_request |= CPU_INTERRUPT_VIRQ; 895 } 896 if (interrupt_request & CPU_INTERRUPT_VFNMI) { 897 interrupt_request |= CPU_INTERRUPT_VFIQ; 898 } 899 } 900 901 if (interrupt_request & CPU_INTERRUPT_FIQ) { 902 excp_idx = EXCP_FIQ; 903 target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure); 904 if (arm_excp_unmasked(cs, excp_idx, target_el, 905 cur_el, secure, hcr_el2)) { 906 goto found; 907 } 908 } 909 if (interrupt_request & CPU_INTERRUPT_HARD) { 910 excp_idx = EXCP_IRQ; 911 target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure); 912 if (arm_excp_unmasked(cs, excp_idx, target_el, 913 cur_el, secure, hcr_el2)) { 914 goto found; 915 } 916 } 917 if (interrupt_request & CPU_INTERRUPT_VIRQ) { 918 excp_idx = EXCP_VIRQ; 919 target_el = 1; 920 if (arm_excp_unmasked(cs, excp_idx, target_el, 921 cur_el, secure, hcr_el2)) { 922 goto found; 923 } 924 } 925 if (interrupt_request & CPU_INTERRUPT_VFIQ) { 926 excp_idx = EXCP_VFIQ; 927 target_el = 1; 928 if (arm_excp_unmasked(cs, excp_idx, target_el, 929 cur_el, secure, hcr_el2)) { 930 goto found; 931 } 932 } 933 if (interrupt_request & CPU_INTERRUPT_VSERR) { 934 excp_idx = EXCP_VSERR; 935 target_el = 1; 936 if (arm_excp_unmasked(cs, excp_idx, target_el, 937 cur_el, secure, hcr_el2)) { 938 /* Taking a virtual abort clears HCR_EL2.VSE */ 939 env->cp15.hcr_el2 &= ~HCR_VSE; 940 cpu_reset_interrupt(cs, CPU_INTERRUPT_VSERR); 941 goto found; 942 } 943 } 944 return false; 945 946 found: 947 cs->exception_index = excp_idx; 948 env->exception.target_el = target_el; 949 cc->tcg_ops->do_interrupt(cs); 950 return true; 951 } 952 953 #endif /* CONFIG_TCG && !CONFIG_USER_ONLY */ 954 955 void arm_cpu_update_virq(ARMCPU *cpu) 956 { 957 /* 958 * Update the interrupt level for VIRQ, which is the logical OR of 959 * the HCR_EL2.VI bit and the input line level from the GIC. 960 */ 961 CPUARMState *env = &cpu->env; 962 CPUState *cs = CPU(cpu); 963 964 bool new_state = ((arm_hcr_el2_eff(env) & HCR_VI) && 965 !(arm_hcrx_el2_eff(env) & HCRX_VINMI)) || 966 (env->irq_line_state & CPU_INTERRUPT_VIRQ); 967 968 if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VIRQ) != 0)) { 969 if (new_state) { 970 cpu_interrupt(cs, CPU_INTERRUPT_VIRQ); 971 } else { 972 cpu_reset_interrupt(cs, CPU_INTERRUPT_VIRQ); 973 } 974 } 975 } 976 977 void arm_cpu_update_vfiq(ARMCPU *cpu) 978 { 979 /* 980 * Update the interrupt level for VFIQ, which is the logical OR of 981 * the HCR_EL2.VF bit and the input line level from the GIC. 982 */ 983 CPUARMState *env = &cpu->env; 984 CPUState *cs = CPU(cpu); 985 986 bool new_state = ((arm_hcr_el2_eff(env) & HCR_VF) && 987 !(arm_hcrx_el2_eff(env) & HCRX_VFNMI)) || 988 (env->irq_line_state & CPU_INTERRUPT_VFIQ); 989 990 if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VFIQ) != 0)) { 991 if (new_state) { 992 cpu_interrupt(cs, CPU_INTERRUPT_VFIQ); 993 } else { 994 cpu_reset_interrupt(cs, CPU_INTERRUPT_VFIQ); 995 } 996 } 997 } 998 999 void arm_cpu_update_vinmi(ARMCPU *cpu) 1000 { 1001 /* 1002 * Update the interrupt level for VINMI, which is the logical OR of 1003 * the HCRX_EL2.VINMI bit and the input line level from the GIC. 1004 */ 1005 CPUARMState *env = &cpu->env; 1006 CPUState *cs = CPU(cpu); 1007 1008 bool new_state = ((arm_hcr_el2_eff(env) & HCR_VI) && 1009 (arm_hcrx_el2_eff(env) & HCRX_VINMI)) || 1010 (env->irq_line_state & CPU_INTERRUPT_VINMI); 1011 1012 if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VINMI) != 0)) { 1013 if (new_state) { 1014 cpu_interrupt(cs, CPU_INTERRUPT_VINMI); 1015 } else { 1016 cpu_reset_interrupt(cs, CPU_INTERRUPT_VINMI); 1017 } 1018 } 1019 } 1020 1021 void arm_cpu_update_vfnmi(ARMCPU *cpu) 1022 { 1023 /* 1024 * Update the interrupt level for VFNMI, which is the HCRX_EL2.VFNMI bit. 1025 */ 1026 CPUARMState *env = &cpu->env; 1027 CPUState *cs = CPU(cpu); 1028 1029 bool new_state = (arm_hcr_el2_eff(env) & HCR_VF) && 1030 (arm_hcrx_el2_eff(env) & HCRX_VFNMI); 1031 1032 if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VFNMI) != 0)) { 1033 if (new_state) { 1034 cpu_interrupt(cs, CPU_INTERRUPT_VFNMI); 1035 } else { 1036 cpu_reset_interrupt(cs, CPU_INTERRUPT_VFNMI); 1037 } 1038 } 1039 } 1040 1041 void arm_cpu_update_vserr(ARMCPU *cpu) 1042 { 1043 /* 1044 * Update the interrupt level for VSERR, which is the HCR_EL2.VSE bit. 1045 */ 1046 CPUARMState *env = &cpu->env; 1047 CPUState *cs = CPU(cpu); 1048 1049 bool new_state = env->cp15.hcr_el2 & HCR_VSE; 1050 1051 if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VSERR) != 0)) { 1052 if (new_state) { 1053 cpu_interrupt(cs, CPU_INTERRUPT_VSERR); 1054 } else { 1055 cpu_reset_interrupt(cs, CPU_INTERRUPT_VSERR); 1056 } 1057 } 1058 } 1059 1060 #ifndef CONFIG_USER_ONLY 1061 static void arm_cpu_set_irq(void *opaque, int irq, int level) 1062 { 1063 ARMCPU *cpu = opaque; 1064 CPUARMState *env = &cpu->env; 1065 CPUState *cs = CPU(cpu); 1066 static const int mask[] = { 1067 [ARM_CPU_IRQ] = CPU_INTERRUPT_HARD, 1068 [ARM_CPU_FIQ] = CPU_INTERRUPT_FIQ, 1069 [ARM_CPU_VIRQ] = CPU_INTERRUPT_VIRQ, 1070 [ARM_CPU_VFIQ] = CPU_INTERRUPT_VFIQ, 1071 [ARM_CPU_NMI] = CPU_INTERRUPT_NMI, 1072 [ARM_CPU_VINMI] = CPU_INTERRUPT_VINMI, 1073 }; 1074 1075 if (!arm_feature(env, ARM_FEATURE_EL2) && 1076 (irq == ARM_CPU_VIRQ || irq == ARM_CPU_VFIQ)) { 1077 /* 1078 * The GIC might tell us about VIRQ and VFIQ state, but if we don't 1079 * have EL2 support we don't care. (Unless the guest is doing something 1080 * silly this will only be calls saying "level is still 0".) 1081 */ 1082 return; 1083 } 1084 1085 if (level) { 1086 env->irq_line_state |= mask[irq]; 1087 } else { 1088 env->irq_line_state &= ~mask[irq]; 1089 } 1090 1091 switch (irq) { 1092 case ARM_CPU_VIRQ: 1093 arm_cpu_update_virq(cpu); 1094 break; 1095 case ARM_CPU_VFIQ: 1096 arm_cpu_update_vfiq(cpu); 1097 break; 1098 case ARM_CPU_VINMI: 1099 arm_cpu_update_vinmi(cpu); 1100 break; 1101 case ARM_CPU_IRQ: 1102 case ARM_CPU_FIQ: 1103 case ARM_CPU_NMI: 1104 if (level) { 1105 cpu_interrupt(cs, mask[irq]); 1106 } else { 1107 cpu_reset_interrupt(cs, mask[irq]); 1108 } 1109 break; 1110 default: 1111 g_assert_not_reached(); 1112 } 1113 } 1114 1115 static void arm_cpu_kvm_set_irq(void *opaque, int irq, int level) 1116 { 1117 #ifdef CONFIG_KVM 1118 ARMCPU *cpu = opaque; 1119 CPUARMState *env = &cpu->env; 1120 CPUState *cs = CPU(cpu); 1121 uint32_t linestate_bit; 1122 int irq_id; 1123 1124 switch (irq) { 1125 case ARM_CPU_IRQ: 1126 irq_id = KVM_ARM_IRQ_CPU_IRQ; 1127 linestate_bit = CPU_INTERRUPT_HARD; 1128 break; 1129 case ARM_CPU_FIQ: 1130 irq_id = KVM_ARM_IRQ_CPU_FIQ; 1131 linestate_bit = CPU_INTERRUPT_FIQ; 1132 break; 1133 default: 1134 g_assert_not_reached(); 1135 } 1136 1137 if (level) { 1138 env->irq_line_state |= linestate_bit; 1139 } else { 1140 env->irq_line_state &= ~linestate_bit; 1141 } 1142 kvm_arm_set_irq(cs->cpu_index, KVM_ARM_IRQ_TYPE_CPU, irq_id, !!level); 1143 #endif 1144 } 1145 1146 static bool arm_cpu_virtio_is_big_endian(CPUState *cs) 1147 { 1148 ARMCPU *cpu = ARM_CPU(cs); 1149 CPUARMState *env = &cpu->env; 1150 1151 cpu_synchronize_state(cs); 1152 return arm_cpu_data_is_big_endian(env); 1153 } 1154 1155 #ifdef CONFIG_TCG 1156 bool arm_cpu_exec_halt(CPUState *cs) 1157 { 1158 bool leave_halt = cpu_has_work(cs); 1159 1160 if (leave_halt) { 1161 /* We're about to come out of WFI/WFE: disable the WFxT timer */ 1162 ARMCPU *cpu = ARM_CPU(cs); 1163 if (cpu->wfxt_timer) { 1164 timer_del(cpu->wfxt_timer); 1165 } 1166 } 1167 return leave_halt; 1168 } 1169 #endif 1170 1171 static void arm_wfxt_timer_cb(void *opaque) 1172 { 1173 ARMCPU *cpu = opaque; 1174 CPUState *cs = CPU(cpu); 1175 1176 /* 1177 * We expect the CPU to be halted; this will cause arm_cpu_is_work() 1178 * to return true (so we will come out of halt even with no other 1179 * pending interrupt), and the TCG accelerator's cpu_exec_interrupt() 1180 * function auto-clears the CPU_INTERRUPT_EXITTB flag for us. 1181 */ 1182 cpu_interrupt(cs, CPU_INTERRUPT_EXITTB); 1183 } 1184 #endif 1185 1186 static void arm_disas_set_info(CPUState *cpu, disassemble_info *info) 1187 { 1188 ARMCPU *ac = ARM_CPU(cpu); 1189 CPUARMState *env = &ac->env; 1190 bool sctlr_b; 1191 1192 if (is_a64(env)) { 1193 info->cap_arch = CS_ARCH_ARM64; 1194 info->cap_insn_unit = 4; 1195 info->cap_insn_split = 4; 1196 } else { 1197 int cap_mode; 1198 if (env->thumb) { 1199 info->cap_insn_unit = 2; 1200 info->cap_insn_split = 4; 1201 cap_mode = CS_MODE_THUMB; 1202 } else { 1203 info->cap_insn_unit = 4; 1204 info->cap_insn_split = 4; 1205 cap_mode = CS_MODE_ARM; 1206 } 1207 if (arm_feature(env, ARM_FEATURE_V8)) { 1208 cap_mode |= CS_MODE_V8; 1209 } 1210 if (arm_feature(env, ARM_FEATURE_M)) { 1211 cap_mode |= CS_MODE_MCLASS; 1212 } 1213 info->cap_arch = CS_ARCH_ARM; 1214 info->cap_mode = cap_mode; 1215 } 1216 1217 sctlr_b = arm_sctlr_b(env); 1218 if (bswap_code(sctlr_b)) { 1219 #if TARGET_BIG_ENDIAN 1220 info->endian = BFD_ENDIAN_LITTLE; 1221 #else 1222 info->endian = BFD_ENDIAN_BIG; 1223 #endif 1224 } 1225 info->flags &= ~INSN_ARM_BE32; 1226 #ifndef CONFIG_USER_ONLY 1227 if (sctlr_b) { 1228 info->flags |= INSN_ARM_BE32; 1229 } 1230 #endif 1231 } 1232 1233 #ifdef TARGET_AARCH64 1234 1235 static void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags) 1236 { 1237 ARMCPU *cpu = ARM_CPU(cs); 1238 CPUARMState *env = &cpu->env; 1239 uint32_t psr = pstate_read(env); 1240 int i, j; 1241 int el = arm_current_el(env); 1242 uint64_t hcr = arm_hcr_el2_eff(env); 1243 const char *ns_status; 1244 bool sve; 1245 1246 qemu_fprintf(f, " PC=%016" PRIx64 " ", env->pc); 1247 for (i = 0; i < 32; i++) { 1248 if (i == 31) { 1249 qemu_fprintf(f, " SP=%016" PRIx64 "\n", env->xregs[i]); 1250 } else { 1251 qemu_fprintf(f, "X%02d=%016" PRIx64 "%s", i, env->xregs[i], 1252 (i + 2) % 3 ? " " : "\n"); 1253 } 1254 } 1255 1256 if (arm_feature(env, ARM_FEATURE_EL3) && el != 3) { 1257 ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S "; 1258 } else { 1259 ns_status = ""; 1260 } 1261 qemu_fprintf(f, "PSTATE=%08x %c%c%c%c %sEL%d%c", 1262 psr, 1263 psr & PSTATE_N ? 'N' : '-', 1264 psr & PSTATE_Z ? 'Z' : '-', 1265 psr & PSTATE_C ? 'C' : '-', 1266 psr & PSTATE_V ? 'V' : '-', 1267 ns_status, 1268 el, 1269 psr & PSTATE_SP ? 'h' : 't'); 1270 1271 if (cpu_isar_feature(aa64_sme, cpu)) { 1272 qemu_fprintf(f, " SVCR=%08" PRIx64 " %c%c", 1273 env->svcr, 1274 (FIELD_EX64(env->svcr, SVCR, ZA) ? 'Z' : '-'), 1275 (FIELD_EX64(env->svcr, SVCR, SM) ? 'S' : '-')); 1276 } 1277 if (cpu_isar_feature(aa64_bti, cpu)) { 1278 qemu_fprintf(f, " BTYPE=%d", (psr & PSTATE_BTYPE) >> 10); 1279 } 1280 qemu_fprintf(f, "%s%s%s", 1281 (hcr & HCR_NV) ? " NV" : "", 1282 (hcr & HCR_NV1) ? " NV1" : "", 1283 (hcr & HCR_NV2) ? " NV2" : ""); 1284 if (!(flags & CPU_DUMP_FPU)) { 1285 qemu_fprintf(f, "\n"); 1286 return; 1287 } 1288 if (fp_exception_el(env, el) != 0) { 1289 qemu_fprintf(f, " FPU disabled\n"); 1290 return; 1291 } 1292 qemu_fprintf(f, " FPCR=%08x FPSR=%08x\n", 1293 vfp_get_fpcr(env), vfp_get_fpsr(env)); 1294 1295 if (cpu_isar_feature(aa64_sme, cpu) && FIELD_EX64(env->svcr, SVCR, SM)) { 1296 sve = sme_exception_el(env, el) == 0; 1297 } else if (cpu_isar_feature(aa64_sve, cpu)) { 1298 sve = sve_exception_el(env, el) == 0; 1299 } else { 1300 sve = false; 1301 } 1302 1303 if (sve) { 1304 int zcr_len = sve_vqm1_for_el(env, el); 1305 1306 for (i = 0; i <= FFR_PRED_NUM; i++) { 1307 bool eol; 1308 if (i == FFR_PRED_NUM) { 1309 qemu_fprintf(f, "FFR="); 1310 /* It's last, so end the line. */ 1311 eol = true; 1312 } else { 1313 qemu_fprintf(f, "P%02d=", i); 1314 switch (zcr_len) { 1315 case 0: 1316 eol = i % 8 == 7; 1317 break; 1318 case 1: 1319 eol = i % 6 == 5; 1320 break; 1321 case 2: 1322 case 3: 1323 eol = i % 3 == 2; 1324 break; 1325 default: 1326 /* More than one quadword per predicate. */ 1327 eol = true; 1328 break; 1329 } 1330 } 1331 for (j = zcr_len / 4; j >= 0; j--) { 1332 int digits; 1333 if (j * 4 + 4 <= zcr_len + 1) { 1334 digits = 16; 1335 } else { 1336 digits = (zcr_len % 4 + 1) * 4; 1337 } 1338 qemu_fprintf(f, "%0*" PRIx64 "%s", digits, 1339 env->vfp.pregs[i].p[j], 1340 j ? ":" : eol ? "\n" : " "); 1341 } 1342 } 1343 1344 if (zcr_len == 0) { 1345 /* 1346 * With vl=16, there are only 37 columns per register, 1347 * so output two registers per line. 1348 */ 1349 for (i = 0; i < 32; i++) { 1350 qemu_fprintf(f, "Z%02d=%016" PRIx64 ":%016" PRIx64 "%s", 1351 i, env->vfp.zregs[i].d[1], 1352 env->vfp.zregs[i].d[0], i & 1 ? "\n" : " "); 1353 } 1354 } else { 1355 for (i = 0; i < 32; i++) { 1356 qemu_fprintf(f, "Z%02d=", i); 1357 for (j = zcr_len; j >= 0; j--) { 1358 qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%s", 1359 env->vfp.zregs[i].d[j * 2 + 1], 1360 env->vfp.zregs[i].d[j * 2 + 0], 1361 j ? ":" : "\n"); 1362 } 1363 } 1364 } 1365 } else { 1366 for (i = 0; i < 32; i++) { 1367 uint64_t *q = aa64_vfp_qreg(env, i); 1368 qemu_fprintf(f, "Q%02d=%016" PRIx64 ":%016" PRIx64 "%s", 1369 i, q[1], q[0], (i & 1 ? "\n" : " ")); 1370 } 1371 } 1372 1373 if (cpu_isar_feature(aa64_sme, cpu) && 1374 FIELD_EX64(env->svcr, SVCR, ZA) && 1375 sme_exception_el(env, el) == 0) { 1376 int zcr_len = sve_vqm1_for_el_sm(env, el, true); 1377 int svl = (zcr_len + 1) * 16; 1378 int svl_lg10 = svl < 100 ? 2 : 3; 1379 1380 for (i = 0; i < svl; i++) { 1381 qemu_fprintf(f, "ZA[%0*d]=", svl_lg10, i); 1382 for (j = zcr_len; j >= 0; --j) { 1383 qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%c", 1384 env->zarray[i].d[2 * j + 1], 1385 env->zarray[i].d[2 * j], 1386 j ? ':' : '\n'); 1387 } 1388 } 1389 } 1390 } 1391 1392 #else 1393 1394 static inline void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags) 1395 { 1396 g_assert_not_reached(); 1397 } 1398 1399 #endif 1400 1401 static void arm_cpu_dump_state(CPUState *cs, FILE *f, int flags) 1402 { 1403 ARMCPU *cpu = ARM_CPU(cs); 1404 CPUARMState *env = &cpu->env; 1405 int i; 1406 1407 if (is_a64(env)) { 1408 aarch64_cpu_dump_state(cs, f, flags); 1409 return; 1410 } 1411 1412 for (i = 0; i < 16; i++) { 1413 qemu_fprintf(f, "R%02d=%08x", i, env->regs[i]); 1414 if ((i % 4) == 3) { 1415 qemu_fprintf(f, "\n"); 1416 } else { 1417 qemu_fprintf(f, " "); 1418 } 1419 } 1420 1421 if (arm_feature(env, ARM_FEATURE_M)) { 1422 uint32_t xpsr = xpsr_read(env); 1423 const char *mode; 1424 const char *ns_status = ""; 1425 1426 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 1427 ns_status = env->v7m.secure ? "S " : "NS "; 1428 } 1429 1430 if (xpsr & XPSR_EXCP) { 1431 mode = "handler"; 1432 } else { 1433 if (env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_NPRIV_MASK) { 1434 mode = "unpriv-thread"; 1435 } else { 1436 mode = "priv-thread"; 1437 } 1438 } 1439 1440 qemu_fprintf(f, "XPSR=%08x %c%c%c%c %c %s%s\n", 1441 xpsr, 1442 xpsr & XPSR_N ? 'N' : '-', 1443 xpsr & XPSR_Z ? 'Z' : '-', 1444 xpsr & XPSR_C ? 'C' : '-', 1445 xpsr & XPSR_V ? 'V' : '-', 1446 xpsr & XPSR_T ? 'T' : 'A', 1447 ns_status, 1448 mode); 1449 } else { 1450 uint32_t psr = cpsr_read(env); 1451 const char *ns_status = ""; 1452 1453 if (arm_feature(env, ARM_FEATURE_EL3) && 1454 (psr & CPSR_M) != ARM_CPU_MODE_MON) { 1455 ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S "; 1456 } 1457 1458 qemu_fprintf(f, "PSR=%08x %c%c%c%c %c %s%s%d\n", 1459 psr, 1460 psr & CPSR_N ? 'N' : '-', 1461 psr & CPSR_Z ? 'Z' : '-', 1462 psr & CPSR_C ? 'C' : '-', 1463 psr & CPSR_V ? 'V' : '-', 1464 psr & CPSR_T ? 'T' : 'A', 1465 ns_status, 1466 aarch32_mode_name(psr), (psr & 0x10) ? 32 : 26); 1467 } 1468 1469 if (flags & CPU_DUMP_FPU) { 1470 int numvfpregs = 0; 1471 if (cpu_isar_feature(aa32_simd_r32, cpu)) { 1472 numvfpregs = 32; 1473 } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) { 1474 numvfpregs = 16; 1475 } 1476 for (i = 0; i < numvfpregs; i++) { 1477 uint64_t v = *aa32_vfp_dreg(env, i); 1478 qemu_fprintf(f, "s%02d=%08x s%02d=%08x d%02d=%016" PRIx64 "\n", 1479 i * 2, (uint32_t)v, 1480 i * 2 + 1, (uint32_t)(v >> 32), 1481 i, v); 1482 } 1483 qemu_fprintf(f, "FPSCR: %08x\n", vfp_get_fpscr(env)); 1484 if (cpu_isar_feature(aa32_mve, cpu)) { 1485 qemu_fprintf(f, "VPR: %08x\n", env->v7m.vpr); 1486 } 1487 } 1488 } 1489 1490 uint64_t arm_build_mp_affinity(int idx, uint8_t clustersz) 1491 { 1492 uint32_t Aff1 = idx / clustersz; 1493 uint32_t Aff0 = idx % clustersz; 1494 return (Aff1 << ARM_AFF1_SHIFT) | Aff0; 1495 } 1496 1497 uint64_t arm_cpu_mp_affinity(ARMCPU *cpu) 1498 { 1499 return cpu->mp_affinity; 1500 } 1501 1502 static void arm_cpu_initfn(Object *obj) 1503 { 1504 ARMCPU *cpu = ARM_CPU(obj); 1505 1506 cpu->cp_regs = g_hash_table_new_full(g_direct_hash, g_direct_equal, 1507 NULL, g_free); 1508 1509 QLIST_INIT(&cpu->pre_el_change_hooks); 1510 QLIST_INIT(&cpu->el_change_hooks); 1511 1512 #ifdef CONFIG_USER_ONLY 1513 # ifdef TARGET_AARCH64 1514 /* 1515 * The linux kernel defaults to 512-bit for SVE, and 256-bit for SME. 1516 * These values were chosen to fit within the default signal frame. 1517 * See documentation for /proc/sys/abi/{sve,sme}_default_vector_length, 1518 * and our corresponding cpu property. 1519 */ 1520 cpu->sve_default_vq = 4; 1521 cpu->sme_default_vq = 2; 1522 # endif 1523 #else 1524 /* Our inbound IRQ and FIQ lines */ 1525 if (kvm_enabled()) { 1526 /* 1527 * VIRQ, VFIQ, NMI, VINMI are unused with KVM but we add 1528 * them to maintain the same interface as non-KVM CPUs. 1529 */ 1530 qdev_init_gpio_in(DEVICE(cpu), arm_cpu_kvm_set_irq, 6); 1531 } else { 1532 qdev_init_gpio_in(DEVICE(cpu), arm_cpu_set_irq, 6); 1533 } 1534 1535 qdev_init_gpio_out(DEVICE(cpu), cpu->gt_timer_outputs, 1536 ARRAY_SIZE(cpu->gt_timer_outputs)); 1537 1538 qdev_init_gpio_out_named(DEVICE(cpu), &cpu->gicv3_maintenance_interrupt, 1539 "gicv3-maintenance-interrupt", 1); 1540 qdev_init_gpio_out_named(DEVICE(cpu), &cpu->pmu_interrupt, 1541 "pmu-interrupt", 1); 1542 #endif 1543 1544 /* DTB consumers generally don't in fact care what the 'compatible' 1545 * string is, so always provide some string and trust that a hypothetical 1546 * picky DTB consumer will also provide a helpful error message. 1547 */ 1548 cpu->dtb_compatible = "qemu,unknown"; 1549 cpu->psci_version = QEMU_PSCI_VERSION_0_1; /* By default assume PSCI v0.1 */ 1550 cpu->kvm_target = QEMU_KVM_ARM_TARGET_NONE; 1551 1552 if (tcg_enabled() || hvf_enabled()) { 1553 /* TCG and HVF implement PSCI 1.1 */ 1554 cpu->psci_version = QEMU_PSCI_VERSION_1_1; 1555 } 1556 } 1557 1558 /* 1559 * 0 means "unset, use the default value". That default might vary depending 1560 * on the CPU type, and is set in the realize fn. 1561 */ 1562 static const Property arm_cpu_gt_cntfrq_property = 1563 DEFINE_PROP_UINT64("cntfrq", ARMCPU, gt_cntfrq_hz, 0); 1564 1565 static const Property arm_cpu_reset_cbar_property = 1566 DEFINE_PROP_UINT64("reset-cbar", ARMCPU, reset_cbar, 0); 1567 1568 static const Property arm_cpu_reset_hivecs_property = 1569 DEFINE_PROP_BOOL("reset-hivecs", ARMCPU, reset_hivecs, false); 1570 1571 #ifndef CONFIG_USER_ONLY 1572 static const Property arm_cpu_has_el2_property = 1573 DEFINE_PROP_BOOL("has_el2", ARMCPU, has_el2, true); 1574 1575 static const Property arm_cpu_has_el3_property = 1576 DEFINE_PROP_BOOL("has_el3", ARMCPU, has_el3, true); 1577 #endif 1578 1579 static const Property arm_cpu_cfgend_property = 1580 DEFINE_PROP_BOOL("cfgend", ARMCPU, cfgend, false); 1581 1582 static const Property arm_cpu_has_vfp_property = 1583 DEFINE_PROP_BOOL("vfp", ARMCPU, has_vfp, true); 1584 1585 static const Property arm_cpu_has_vfp_d32_property = 1586 DEFINE_PROP_BOOL("vfp-d32", ARMCPU, has_vfp_d32, true); 1587 1588 static const Property arm_cpu_has_neon_property = 1589 DEFINE_PROP_BOOL("neon", ARMCPU, has_neon, true); 1590 1591 static const Property arm_cpu_has_dsp_property = 1592 DEFINE_PROP_BOOL("dsp", ARMCPU, has_dsp, true); 1593 1594 static const Property arm_cpu_has_mpu_property = 1595 DEFINE_PROP_BOOL("has-mpu", ARMCPU, has_mpu, true); 1596 1597 /* This is like DEFINE_PROP_UINT32 but it doesn't set the default value, 1598 * because the CPU initfn will have already set cpu->pmsav7_dregion to 1599 * the right value for that particular CPU type, and we don't want 1600 * to override that with an incorrect constant value. 1601 */ 1602 static const Property arm_cpu_pmsav7_dregion_property = 1603 DEFINE_PROP_UNSIGNED_NODEFAULT("pmsav7-dregion", ARMCPU, 1604 pmsav7_dregion, 1605 qdev_prop_uint32, uint32_t); 1606 1607 static bool arm_get_pmu(Object *obj, Error **errp) 1608 { 1609 ARMCPU *cpu = ARM_CPU(obj); 1610 1611 return cpu->has_pmu; 1612 } 1613 1614 static void arm_set_pmu(Object *obj, bool value, Error **errp) 1615 { 1616 ARMCPU *cpu = ARM_CPU(obj); 1617 1618 if (value) { 1619 if (kvm_enabled() && !kvm_arm_pmu_supported()) { 1620 error_setg(errp, "'pmu' feature not supported by KVM on this host"); 1621 return; 1622 } 1623 set_feature(&cpu->env, ARM_FEATURE_PMU); 1624 } else { 1625 unset_feature(&cpu->env, ARM_FEATURE_PMU); 1626 } 1627 cpu->has_pmu = value; 1628 } 1629 1630 unsigned int gt_cntfrq_period_ns(ARMCPU *cpu) 1631 { 1632 /* 1633 * The exact approach to calculating guest ticks is: 1634 * 1635 * muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), cpu->gt_cntfrq_hz, 1636 * NANOSECONDS_PER_SECOND); 1637 * 1638 * We don't do that. Rather we intentionally use integer division 1639 * truncation below and in the caller for the conversion of host monotonic 1640 * time to guest ticks to provide the exact inverse for the semantics of 1641 * the QEMUTimer scale factor. QEMUTimer's scale facter is an integer, so 1642 * it loses precision when representing frequencies where 1643 * `(NANOSECONDS_PER_SECOND % cpu->gt_cntfrq) > 0` holds. Failing to 1644 * provide an exact inverse leads to scheduling timers with negative 1645 * periods, which in turn leads to sticky behaviour in the guest. 1646 * 1647 * Finally, CNTFRQ is effectively capped at 1GHz to ensure our scale factor 1648 * cannot become zero. 1649 */ 1650 return NANOSECONDS_PER_SECOND > cpu->gt_cntfrq_hz ? 1651 NANOSECONDS_PER_SECOND / cpu->gt_cntfrq_hz : 1; 1652 } 1653 1654 static void arm_cpu_propagate_feature_implications(ARMCPU *cpu) 1655 { 1656 CPUARMState *env = &cpu->env; 1657 bool no_aa32 = false; 1658 1659 /* 1660 * Some features automatically imply others: set the feature 1661 * bits explicitly for these cases. 1662 */ 1663 1664 if (arm_feature(env, ARM_FEATURE_M)) { 1665 set_feature(env, ARM_FEATURE_PMSA); 1666 } 1667 1668 if (arm_feature(env, ARM_FEATURE_V8)) { 1669 if (arm_feature(env, ARM_FEATURE_M)) { 1670 set_feature(env, ARM_FEATURE_V7); 1671 } else { 1672 set_feature(env, ARM_FEATURE_V7VE); 1673 } 1674 } 1675 1676 /* 1677 * There exist AArch64 cpus without AArch32 support. When KVM 1678 * queries ID_ISAR0_EL1 on such a host, the value is UNKNOWN. 1679 * Similarly, we cannot check ID_AA64PFR0 without AArch64 support. 1680 * As a general principle, we also do not make ID register 1681 * consistency checks anywhere unless using TCG, because only 1682 * for TCG would a consistency-check failure be a QEMU bug. 1683 */ 1684 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { 1685 no_aa32 = !cpu_isar_feature(aa64_aa32, cpu); 1686 } 1687 1688 if (arm_feature(env, ARM_FEATURE_V7VE)) { 1689 /* 1690 * v7 Virtualization Extensions. In real hardware this implies 1691 * EL2 and also the presence of the Security Extensions. 1692 * For QEMU, for backwards-compatibility we implement some 1693 * CPUs or CPU configs which have no actual EL2 or EL3 but do 1694 * include the various other features that V7VE implies. 1695 * Presence of EL2 itself is ARM_FEATURE_EL2, and of the 1696 * Security Extensions is ARM_FEATURE_EL3. 1697 */ 1698 assert(!tcg_enabled() || no_aa32 || 1699 cpu_isar_feature(aa32_arm_div, cpu)); 1700 set_feature(env, ARM_FEATURE_LPAE); 1701 set_feature(env, ARM_FEATURE_V7); 1702 } 1703 if (arm_feature(env, ARM_FEATURE_V7)) { 1704 set_feature(env, ARM_FEATURE_VAPA); 1705 set_feature(env, ARM_FEATURE_THUMB2); 1706 set_feature(env, ARM_FEATURE_MPIDR); 1707 if (!arm_feature(env, ARM_FEATURE_M)) { 1708 set_feature(env, ARM_FEATURE_V6K); 1709 } else { 1710 set_feature(env, ARM_FEATURE_V6); 1711 } 1712 1713 /* 1714 * Always define VBAR for V7 CPUs even if it doesn't exist in 1715 * non-EL3 configs. This is needed by some legacy boards. 1716 */ 1717 set_feature(env, ARM_FEATURE_VBAR); 1718 } 1719 if (arm_feature(env, ARM_FEATURE_V6K)) { 1720 set_feature(env, ARM_FEATURE_V6); 1721 set_feature(env, ARM_FEATURE_MVFR); 1722 } 1723 if (arm_feature(env, ARM_FEATURE_V6)) { 1724 set_feature(env, ARM_FEATURE_V5); 1725 if (!arm_feature(env, ARM_FEATURE_M)) { 1726 assert(!tcg_enabled() || no_aa32 || 1727 cpu_isar_feature(aa32_jazelle, cpu)); 1728 set_feature(env, ARM_FEATURE_AUXCR); 1729 } 1730 } 1731 if (arm_feature(env, ARM_FEATURE_V5)) { 1732 set_feature(env, ARM_FEATURE_V4T); 1733 } 1734 if (arm_feature(env, ARM_FEATURE_LPAE)) { 1735 set_feature(env, ARM_FEATURE_V7MP); 1736 } 1737 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) { 1738 set_feature(env, ARM_FEATURE_CBAR); 1739 } 1740 if (arm_feature(env, ARM_FEATURE_THUMB2) && 1741 !arm_feature(env, ARM_FEATURE_M)) { 1742 set_feature(env, ARM_FEATURE_THUMB_DSP); 1743 } 1744 } 1745 1746 void arm_cpu_post_init(Object *obj) 1747 { 1748 ARMCPU *cpu = ARM_CPU(obj); 1749 1750 /* 1751 * Some features imply others. Figure this out now, because we 1752 * are going to look at the feature bits in deciding which 1753 * properties to add. 1754 */ 1755 arm_cpu_propagate_feature_implications(cpu); 1756 1757 if (arm_feature(&cpu->env, ARM_FEATURE_CBAR) || 1758 arm_feature(&cpu->env, ARM_FEATURE_CBAR_RO)) { 1759 qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_cbar_property); 1760 } 1761 1762 if (!arm_feature(&cpu->env, ARM_FEATURE_M)) { 1763 qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_hivecs_property); 1764 } 1765 1766 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 1767 object_property_add_uint64_ptr(obj, "rvbar", 1768 &cpu->rvbar_prop, 1769 OBJ_PROP_FLAG_READWRITE); 1770 } 1771 1772 #ifndef CONFIG_USER_ONLY 1773 if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) { 1774 /* Add the has_el3 state CPU property only if EL3 is allowed. This will 1775 * prevent "has_el3" from existing on CPUs which cannot support EL3. 1776 */ 1777 qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el3_property); 1778 1779 object_property_add_link(obj, "secure-memory", 1780 TYPE_MEMORY_REGION, 1781 (Object **)&cpu->secure_memory, 1782 qdev_prop_allow_set_link_before_realize, 1783 OBJ_PROP_LINK_STRONG); 1784 } 1785 1786 if (arm_feature(&cpu->env, ARM_FEATURE_EL2)) { 1787 qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el2_property); 1788 } 1789 #endif 1790 1791 if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) { 1792 cpu->has_pmu = true; 1793 object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu); 1794 } 1795 1796 /* 1797 * Allow user to turn off VFP and Neon support, but only for TCG -- 1798 * KVM does not currently allow us to lie to the guest about its 1799 * ID/feature registers, so the guest always sees what the host has. 1800 */ 1801 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { 1802 if (cpu_isar_feature(aa64_fp_simd, cpu)) { 1803 cpu->has_vfp = true; 1804 cpu->has_vfp_d32 = true; 1805 if (tcg_enabled() || qtest_enabled()) { 1806 qdev_property_add_static(DEVICE(obj), 1807 &arm_cpu_has_vfp_property); 1808 } 1809 } 1810 } else if (cpu_isar_feature(aa32_vfp, cpu)) { 1811 cpu->has_vfp = true; 1812 if (tcg_enabled() || qtest_enabled()) { 1813 qdev_property_add_static(DEVICE(obj), 1814 &arm_cpu_has_vfp_property); 1815 } 1816 if (cpu_isar_feature(aa32_simd_r32, cpu)) { 1817 cpu->has_vfp_d32 = true; 1818 /* 1819 * The permitted values of the SIMDReg bits [3:0] on 1820 * Armv8-A are either 0b0000 and 0b0010. On such CPUs, 1821 * make sure that has_vfp_d32 can not be set to false. 1822 */ 1823 if ((tcg_enabled() || qtest_enabled()) 1824 && !(arm_feature(&cpu->env, ARM_FEATURE_V8) 1825 && !arm_feature(&cpu->env, ARM_FEATURE_M))) { 1826 qdev_property_add_static(DEVICE(obj), 1827 &arm_cpu_has_vfp_d32_property); 1828 } 1829 } 1830 } 1831 1832 if (arm_feature(&cpu->env, ARM_FEATURE_NEON)) { 1833 cpu->has_neon = true; 1834 if (!kvm_enabled()) { 1835 qdev_property_add_static(DEVICE(obj), &arm_cpu_has_neon_property); 1836 } 1837 } 1838 1839 if (arm_feature(&cpu->env, ARM_FEATURE_M) && 1840 arm_feature(&cpu->env, ARM_FEATURE_THUMB_DSP)) { 1841 qdev_property_add_static(DEVICE(obj), &arm_cpu_has_dsp_property); 1842 } 1843 1844 if (arm_feature(&cpu->env, ARM_FEATURE_PMSA)) { 1845 qdev_property_add_static(DEVICE(obj), &arm_cpu_has_mpu_property); 1846 if (arm_feature(&cpu->env, ARM_FEATURE_V7)) { 1847 qdev_property_add_static(DEVICE(obj), 1848 &arm_cpu_pmsav7_dregion_property); 1849 } 1850 } 1851 1852 if (arm_feature(&cpu->env, ARM_FEATURE_M_SECURITY)) { 1853 object_property_add_link(obj, "idau", TYPE_IDAU_INTERFACE, &cpu->idau, 1854 qdev_prop_allow_set_link_before_realize, 1855 OBJ_PROP_LINK_STRONG); 1856 /* 1857 * M profile: initial value of the Secure VTOR. We can't just use 1858 * a simple DEFINE_PROP_UINT32 for this because we want to permit 1859 * the property to be set after realize. 1860 */ 1861 object_property_add_uint32_ptr(obj, "init-svtor", 1862 &cpu->init_svtor, 1863 OBJ_PROP_FLAG_READWRITE); 1864 } 1865 if (arm_feature(&cpu->env, ARM_FEATURE_M)) { 1866 /* 1867 * Initial value of the NS VTOR (for cores without the Security 1868 * extension, this is the only VTOR) 1869 */ 1870 object_property_add_uint32_ptr(obj, "init-nsvtor", 1871 &cpu->init_nsvtor, 1872 OBJ_PROP_FLAG_READWRITE); 1873 } 1874 1875 /* Not DEFINE_PROP_UINT32: we want this to be settable after realize */ 1876 object_property_add_uint32_ptr(obj, "psci-conduit", 1877 &cpu->psci_conduit, 1878 OBJ_PROP_FLAG_READWRITE); 1879 1880 qdev_property_add_static(DEVICE(obj), &arm_cpu_cfgend_property); 1881 1882 if (arm_feature(&cpu->env, ARM_FEATURE_GENERIC_TIMER)) { 1883 qdev_property_add_static(DEVICE(cpu), &arm_cpu_gt_cntfrq_property); 1884 } 1885 1886 if (kvm_enabled()) { 1887 kvm_arm_add_vcpu_properties(cpu); 1888 } 1889 1890 #ifndef CONFIG_USER_ONLY 1891 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && 1892 cpu_isar_feature(aa64_mte, cpu)) { 1893 object_property_add_link(obj, "tag-memory", 1894 TYPE_MEMORY_REGION, 1895 (Object **)&cpu->tag_memory, 1896 qdev_prop_allow_set_link_before_realize, 1897 OBJ_PROP_LINK_STRONG); 1898 1899 if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) { 1900 object_property_add_link(obj, "secure-tag-memory", 1901 TYPE_MEMORY_REGION, 1902 (Object **)&cpu->secure_tag_memory, 1903 qdev_prop_allow_set_link_before_realize, 1904 OBJ_PROP_LINK_STRONG); 1905 } 1906 } 1907 #endif 1908 } 1909 1910 static void arm_cpu_finalizefn(Object *obj) 1911 { 1912 ARMCPU *cpu = ARM_CPU(obj); 1913 ARMELChangeHook *hook, *next; 1914 1915 g_hash_table_destroy(cpu->cp_regs); 1916 1917 QLIST_FOREACH_SAFE(hook, &cpu->pre_el_change_hooks, node, next) { 1918 QLIST_REMOVE(hook, node); 1919 g_free(hook); 1920 } 1921 QLIST_FOREACH_SAFE(hook, &cpu->el_change_hooks, node, next) { 1922 QLIST_REMOVE(hook, node); 1923 g_free(hook); 1924 } 1925 #ifndef CONFIG_USER_ONLY 1926 if (cpu->pmu_timer) { 1927 timer_free(cpu->pmu_timer); 1928 } 1929 if (cpu->wfxt_timer) { 1930 timer_free(cpu->wfxt_timer); 1931 } 1932 #endif 1933 } 1934 1935 void arm_cpu_finalize_features(ARMCPU *cpu, Error **errp) 1936 { 1937 Error *local_err = NULL; 1938 1939 #ifdef TARGET_AARCH64 1940 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { 1941 arm_cpu_sve_finalize(cpu, &local_err); 1942 if (local_err != NULL) { 1943 error_propagate(errp, local_err); 1944 return; 1945 } 1946 1947 /* 1948 * FEAT_SME is not architecturally dependent on FEAT_SVE (unless 1949 * FEAT_SME_FA64 is present). However our implementation currently 1950 * assumes it, so if the user asked for sve=off then turn off SME also. 1951 * (KVM doesn't currently support SME at all.) 1952 */ 1953 if (cpu_isar_feature(aa64_sme, cpu) && !cpu_isar_feature(aa64_sve, cpu)) { 1954 object_property_set_bool(OBJECT(cpu), "sme", false, &error_abort); 1955 } 1956 1957 arm_cpu_sme_finalize(cpu, &local_err); 1958 if (local_err != NULL) { 1959 error_propagate(errp, local_err); 1960 return; 1961 } 1962 1963 arm_cpu_pauth_finalize(cpu, &local_err); 1964 if (local_err != NULL) { 1965 error_propagate(errp, local_err); 1966 return; 1967 } 1968 1969 arm_cpu_lpa2_finalize(cpu, &local_err); 1970 if (local_err != NULL) { 1971 error_propagate(errp, local_err); 1972 return; 1973 } 1974 } 1975 #endif 1976 1977 if (kvm_enabled()) { 1978 kvm_arm_steal_time_finalize(cpu, &local_err); 1979 if (local_err != NULL) { 1980 error_propagate(errp, local_err); 1981 return; 1982 } 1983 } 1984 } 1985 1986 static void arm_cpu_realizefn(DeviceState *dev, Error **errp) 1987 { 1988 CPUState *cs = CPU(dev); 1989 ARMCPU *cpu = ARM_CPU(dev); 1990 ARMCPUClass *acc = ARM_CPU_GET_CLASS(dev); 1991 CPUARMState *env = &cpu->env; 1992 Error *local_err = NULL; 1993 1994 #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY) 1995 /* Use pc-relative instructions in system-mode */ 1996 tcg_cflags_set(cs, CF_PCREL); 1997 #endif 1998 1999 /* If we needed to query the host kernel for the CPU features 2000 * then it's possible that might have failed in the initfn, but 2001 * this is the first point where we can report it. 2002 */ 2003 if (cpu->host_cpu_probe_failed) { 2004 if (!kvm_enabled() && !hvf_enabled()) { 2005 error_setg(errp, "The 'host' CPU type can only be used with KVM or HVF"); 2006 } else { 2007 error_setg(errp, "Failed to retrieve host CPU features"); 2008 } 2009 return; 2010 } 2011 2012 if (!cpu->gt_cntfrq_hz) { 2013 /* 2014 * 0 means "the board didn't set a value, use the default". (We also 2015 * get here for the CONFIG_USER_ONLY case.) 2016 * ARMv8.6 and later CPUs architecturally must use a 1GHz timer; before 2017 * that it was an IMPDEF choice, and QEMU initially picked 62.5MHz, 2018 * which gives a 16ns tick period. 2019 * 2020 * We will use the back-compat value: 2021 * - for QEMU CPU types added before we standardized on 1GHz 2022 * - for versioned machine types with a version of 9.0 or earlier 2023 */ 2024 if (arm_feature(env, ARM_FEATURE_BACKCOMPAT_CNTFRQ) || 2025 cpu->backcompat_cntfrq) { 2026 cpu->gt_cntfrq_hz = GTIMER_BACKCOMPAT_HZ; 2027 } else { 2028 cpu->gt_cntfrq_hz = GTIMER_DEFAULT_HZ; 2029 } 2030 } 2031 2032 #ifndef CONFIG_USER_ONLY 2033 /* The NVIC and M-profile CPU are two halves of a single piece of 2034 * hardware; trying to use one without the other is a command line 2035 * error and will result in segfaults if not caught here. 2036 */ 2037 if (arm_feature(env, ARM_FEATURE_M)) { 2038 if (!env->nvic) { 2039 error_setg(errp, "This board cannot be used with Cortex-M CPUs"); 2040 return; 2041 } 2042 } else { 2043 if (env->nvic) { 2044 error_setg(errp, "This board can only be used with Cortex-M CPUs"); 2045 return; 2046 } 2047 } 2048 2049 if (!tcg_enabled() && !qtest_enabled()) { 2050 /* 2051 * We assume that no accelerator except TCG (and the "not really an 2052 * accelerator" qtest) can handle these features, because Arm hardware 2053 * virtualization can't virtualize them. 2054 * 2055 * Catch all the cases which might cause us to create more than one 2056 * address space for the CPU (otherwise we will assert() later in 2057 * cpu_address_space_init()). 2058 */ 2059 if (arm_feature(env, ARM_FEATURE_M)) { 2060 error_setg(errp, 2061 "Cannot enable %s when using an M-profile guest CPU", 2062 current_accel_name()); 2063 return; 2064 } 2065 if (cpu->has_el3) { 2066 error_setg(errp, 2067 "Cannot enable %s when guest CPU has EL3 enabled", 2068 current_accel_name()); 2069 return; 2070 } 2071 if (cpu->tag_memory) { 2072 error_setg(errp, 2073 "Cannot enable %s when guest CPUs has MTE enabled", 2074 current_accel_name()); 2075 return; 2076 } 2077 } 2078 2079 { 2080 uint64_t scale = gt_cntfrq_period_ns(cpu); 2081 2082 cpu->gt_timer[GTIMER_PHYS] = timer_new(QEMU_CLOCK_VIRTUAL, scale, 2083 arm_gt_ptimer_cb, cpu); 2084 cpu->gt_timer[GTIMER_VIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale, 2085 arm_gt_vtimer_cb, cpu); 2086 cpu->gt_timer[GTIMER_HYP] = timer_new(QEMU_CLOCK_VIRTUAL, scale, 2087 arm_gt_htimer_cb, cpu); 2088 cpu->gt_timer[GTIMER_SEC] = timer_new(QEMU_CLOCK_VIRTUAL, scale, 2089 arm_gt_stimer_cb, cpu); 2090 cpu->gt_timer[GTIMER_HYPVIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale, 2091 arm_gt_hvtimer_cb, cpu); 2092 } 2093 #endif 2094 2095 cpu_exec_realizefn(cs, &local_err); 2096 if (local_err != NULL) { 2097 error_propagate(errp, local_err); 2098 return; 2099 } 2100 2101 arm_cpu_finalize_features(cpu, &local_err); 2102 if (local_err != NULL) { 2103 error_propagate(errp, local_err); 2104 return; 2105 } 2106 2107 #ifdef CONFIG_USER_ONLY 2108 /* 2109 * User mode relies on IC IVAU instructions to catch modification of 2110 * dual-mapped code. 2111 * 2112 * Clear CTR_EL0.DIC to ensure that software that honors these flags uses 2113 * IC IVAU even if the emulated processor does not normally require it. 2114 */ 2115 cpu->ctr = FIELD_DP64(cpu->ctr, CTR_EL0, DIC, 0); 2116 #endif 2117 2118 if (arm_feature(env, ARM_FEATURE_AARCH64) && 2119 cpu->has_vfp != cpu->has_neon) { 2120 /* 2121 * This is an architectural requirement for AArch64; AArch32 is 2122 * more flexible and permits VFP-no-Neon and Neon-no-VFP. 2123 */ 2124 error_setg(errp, 2125 "AArch64 CPUs must have both VFP and Neon or neither"); 2126 return; 2127 } 2128 2129 if (cpu->has_vfp_d32 != cpu->has_neon) { 2130 error_setg(errp, "ARM CPUs must have both VFP-D32 and Neon or neither"); 2131 return; 2132 } 2133 2134 if (!cpu->has_vfp_d32) { 2135 uint32_t u; 2136 2137 u = cpu->isar.mvfr0; 2138 u = FIELD_DP32(u, MVFR0, SIMDREG, 1); /* 16 registers */ 2139 cpu->isar.mvfr0 = u; 2140 } 2141 2142 if (!cpu->has_vfp) { 2143 uint64_t t; 2144 uint32_t u; 2145 2146 t = cpu->isar.id_aa64isar1; 2147 t = FIELD_DP64(t, ID_AA64ISAR1, JSCVT, 0); 2148 cpu->isar.id_aa64isar1 = t; 2149 2150 t = cpu->isar.id_aa64pfr0; 2151 t = FIELD_DP64(t, ID_AA64PFR0, FP, 0xf); 2152 cpu->isar.id_aa64pfr0 = t; 2153 2154 u = cpu->isar.id_isar6; 2155 u = FIELD_DP32(u, ID_ISAR6, JSCVT, 0); 2156 u = FIELD_DP32(u, ID_ISAR6, BF16, 0); 2157 cpu->isar.id_isar6 = u; 2158 2159 u = cpu->isar.mvfr0; 2160 u = FIELD_DP32(u, MVFR0, FPSP, 0); 2161 u = FIELD_DP32(u, MVFR0, FPDP, 0); 2162 u = FIELD_DP32(u, MVFR0, FPDIVIDE, 0); 2163 u = FIELD_DP32(u, MVFR0, FPSQRT, 0); 2164 u = FIELD_DP32(u, MVFR0, FPROUND, 0); 2165 if (!arm_feature(env, ARM_FEATURE_M)) { 2166 u = FIELD_DP32(u, MVFR0, FPTRAP, 0); 2167 u = FIELD_DP32(u, MVFR0, FPSHVEC, 0); 2168 } 2169 cpu->isar.mvfr0 = u; 2170 2171 u = cpu->isar.mvfr1; 2172 u = FIELD_DP32(u, MVFR1, FPFTZ, 0); 2173 u = FIELD_DP32(u, MVFR1, FPDNAN, 0); 2174 u = FIELD_DP32(u, MVFR1, FPHP, 0); 2175 if (arm_feature(env, ARM_FEATURE_M)) { 2176 u = FIELD_DP32(u, MVFR1, FP16, 0); 2177 } 2178 cpu->isar.mvfr1 = u; 2179 2180 u = cpu->isar.mvfr2; 2181 u = FIELD_DP32(u, MVFR2, FPMISC, 0); 2182 cpu->isar.mvfr2 = u; 2183 } 2184 2185 if (!cpu->has_neon) { 2186 uint64_t t; 2187 uint32_t u; 2188 2189 unset_feature(env, ARM_FEATURE_NEON); 2190 2191 t = cpu->isar.id_aa64isar0; 2192 t = FIELD_DP64(t, ID_AA64ISAR0, AES, 0); 2193 t = FIELD_DP64(t, ID_AA64ISAR0, SHA1, 0); 2194 t = FIELD_DP64(t, ID_AA64ISAR0, SHA2, 0); 2195 t = FIELD_DP64(t, ID_AA64ISAR0, SHA3, 0); 2196 t = FIELD_DP64(t, ID_AA64ISAR0, SM3, 0); 2197 t = FIELD_DP64(t, ID_AA64ISAR0, SM4, 0); 2198 t = FIELD_DP64(t, ID_AA64ISAR0, DP, 0); 2199 cpu->isar.id_aa64isar0 = t; 2200 2201 t = cpu->isar.id_aa64isar1; 2202 t = FIELD_DP64(t, ID_AA64ISAR1, FCMA, 0); 2203 t = FIELD_DP64(t, ID_AA64ISAR1, BF16, 0); 2204 t = FIELD_DP64(t, ID_AA64ISAR1, I8MM, 0); 2205 cpu->isar.id_aa64isar1 = t; 2206 2207 t = cpu->isar.id_aa64pfr0; 2208 t = FIELD_DP64(t, ID_AA64PFR0, ADVSIMD, 0xf); 2209 cpu->isar.id_aa64pfr0 = t; 2210 2211 u = cpu->isar.id_isar5; 2212 u = FIELD_DP32(u, ID_ISAR5, AES, 0); 2213 u = FIELD_DP32(u, ID_ISAR5, SHA1, 0); 2214 u = FIELD_DP32(u, ID_ISAR5, SHA2, 0); 2215 u = FIELD_DP32(u, ID_ISAR5, RDM, 0); 2216 u = FIELD_DP32(u, ID_ISAR5, VCMA, 0); 2217 cpu->isar.id_isar5 = u; 2218 2219 u = cpu->isar.id_isar6; 2220 u = FIELD_DP32(u, ID_ISAR6, DP, 0); 2221 u = FIELD_DP32(u, ID_ISAR6, FHM, 0); 2222 u = FIELD_DP32(u, ID_ISAR6, BF16, 0); 2223 u = FIELD_DP32(u, ID_ISAR6, I8MM, 0); 2224 cpu->isar.id_isar6 = u; 2225 2226 if (!arm_feature(env, ARM_FEATURE_M)) { 2227 u = cpu->isar.mvfr1; 2228 u = FIELD_DP32(u, MVFR1, SIMDLS, 0); 2229 u = FIELD_DP32(u, MVFR1, SIMDINT, 0); 2230 u = FIELD_DP32(u, MVFR1, SIMDSP, 0); 2231 u = FIELD_DP32(u, MVFR1, SIMDHP, 0); 2232 cpu->isar.mvfr1 = u; 2233 2234 u = cpu->isar.mvfr2; 2235 u = FIELD_DP32(u, MVFR2, SIMDMISC, 0); 2236 cpu->isar.mvfr2 = u; 2237 } 2238 } 2239 2240 if (!cpu->has_neon && !cpu->has_vfp) { 2241 uint64_t t; 2242 uint32_t u; 2243 2244 t = cpu->isar.id_aa64isar0; 2245 t = FIELD_DP64(t, ID_AA64ISAR0, FHM, 0); 2246 cpu->isar.id_aa64isar0 = t; 2247 2248 t = cpu->isar.id_aa64isar1; 2249 t = FIELD_DP64(t, ID_AA64ISAR1, FRINTTS, 0); 2250 cpu->isar.id_aa64isar1 = t; 2251 2252 u = cpu->isar.mvfr0; 2253 u = FIELD_DP32(u, MVFR0, SIMDREG, 0); 2254 cpu->isar.mvfr0 = u; 2255 2256 /* Despite the name, this field covers both VFP and Neon */ 2257 u = cpu->isar.mvfr1; 2258 u = FIELD_DP32(u, MVFR1, SIMDFMAC, 0); 2259 cpu->isar.mvfr1 = u; 2260 } 2261 2262 if (arm_feature(env, ARM_FEATURE_M) && !cpu->has_dsp) { 2263 uint32_t u; 2264 2265 unset_feature(env, ARM_FEATURE_THUMB_DSP); 2266 2267 u = cpu->isar.id_isar1; 2268 u = FIELD_DP32(u, ID_ISAR1, EXTEND, 1); 2269 cpu->isar.id_isar1 = u; 2270 2271 u = cpu->isar.id_isar2; 2272 u = FIELD_DP32(u, ID_ISAR2, MULTU, 1); 2273 u = FIELD_DP32(u, ID_ISAR2, MULTS, 1); 2274 cpu->isar.id_isar2 = u; 2275 2276 u = cpu->isar.id_isar3; 2277 u = FIELD_DP32(u, ID_ISAR3, SIMD, 1); 2278 u = FIELD_DP32(u, ID_ISAR3, SATURATE, 0); 2279 cpu->isar.id_isar3 = u; 2280 } 2281 2282 2283 /* 2284 * We rely on no XScale CPU having VFP so we can use the same bits in the 2285 * TB flags field for VECSTRIDE and XSCALE_CPAR. 2286 */ 2287 assert(arm_feature(env, ARM_FEATURE_AARCH64) || 2288 !cpu_isar_feature(aa32_vfp_simd, cpu) || 2289 !arm_feature(env, ARM_FEATURE_XSCALE)); 2290 2291 #ifndef CONFIG_USER_ONLY 2292 { 2293 int pagebits; 2294 if (arm_feature(env, ARM_FEATURE_V7) && 2295 !arm_feature(env, ARM_FEATURE_M) && 2296 !arm_feature(env, ARM_FEATURE_PMSA)) { 2297 /* 2298 * v7VMSA drops support for the old ARMv5 tiny pages, 2299 * so we can use 4K pages. 2300 */ 2301 pagebits = 12; 2302 } else { 2303 /* 2304 * For CPUs which might have tiny 1K pages, or which have an 2305 * MPU and might have small region sizes, stick with 1K pages. 2306 */ 2307 pagebits = 10; 2308 } 2309 if (!set_preferred_target_page_bits(pagebits)) { 2310 /* 2311 * This can only ever happen for hotplugging a CPU, or if 2312 * the board code incorrectly creates a CPU which it has 2313 * promised via minimum_page_size that it will not. 2314 */ 2315 error_setg(errp, "This CPU requires a smaller page size " 2316 "than the system is using"); 2317 return; 2318 } 2319 } 2320 #endif 2321 2322 /* This cpu-id-to-MPIDR affinity is used only for TCG; KVM will override it. 2323 * We don't support setting cluster ID ([16..23]) (known as Aff2 2324 * in later ARM ARM versions), or any of the higher affinity level fields, 2325 * so these bits always RAZ. 2326 */ 2327 if (cpu->mp_affinity == ARM64_AFFINITY_INVALID) { 2328 cpu->mp_affinity = arm_build_mp_affinity(cs->cpu_index, 2329 ARM_DEFAULT_CPUS_PER_CLUSTER); 2330 } 2331 2332 if (cpu->reset_hivecs) { 2333 cpu->reset_sctlr |= (1 << 13); 2334 } 2335 2336 if (cpu->cfgend) { 2337 if (arm_feature(env, ARM_FEATURE_V7)) { 2338 cpu->reset_sctlr |= SCTLR_EE; 2339 } else { 2340 cpu->reset_sctlr |= SCTLR_B; 2341 } 2342 } 2343 2344 if (!arm_feature(env, ARM_FEATURE_M) && !cpu->has_el3) { 2345 /* If the has_el3 CPU property is disabled then we need to disable the 2346 * feature. 2347 */ 2348 unset_feature(env, ARM_FEATURE_EL3); 2349 2350 /* 2351 * Disable the security extension feature bits in the processor 2352 * feature registers as well. 2353 */ 2354 cpu->isar.id_pfr1 = FIELD_DP32(cpu->isar.id_pfr1, ID_PFR1, SECURITY, 0); 2355 cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, COPSDBG, 0); 2356 cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0, 2357 ID_AA64PFR0, EL3, 0); 2358 2359 /* Disable the realm management extension, which requires EL3. */ 2360 cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0, 2361 ID_AA64PFR0, RME, 0); 2362 } 2363 2364 if (!cpu->has_el2) { 2365 unset_feature(env, ARM_FEATURE_EL2); 2366 } 2367 2368 if (!cpu->has_pmu) { 2369 unset_feature(env, ARM_FEATURE_PMU); 2370 } 2371 if (arm_feature(env, ARM_FEATURE_PMU)) { 2372 pmu_init(cpu); 2373 2374 if (!kvm_enabled()) { 2375 arm_register_pre_el_change_hook(cpu, &pmu_pre_el_change, 0); 2376 arm_register_el_change_hook(cpu, &pmu_post_el_change, 0); 2377 } 2378 2379 #ifndef CONFIG_USER_ONLY 2380 cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, arm_pmu_timer_cb, 2381 cpu); 2382 #endif 2383 } else { 2384 cpu->isar.id_aa64dfr0 = 2385 FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMUVER, 0); 2386 cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, PERFMON, 0); 2387 cpu->pmceid0 = 0; 2388 cpu->pmceid1 = 0; 2389 } 2390 2391 if (!arm_feature(env, ARM_FEATURE_EL2)) { 2392 /* 2393 * Disable the hypervisor feature bits in the processor feature 2394 * registers if we don't have EL2. 2395 */ 2396 cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0, 2397 ID_AA64PFR0, EL2, 0); 2398 cpu->isar.id_pfr1 = FIELD_DP32(cpu->isar.id_pfr1, 2399 ID_PFR1, VIRTUALIZATION, 0); 2400 } 2401 2402 if (cpu_isar_feature(aa64_mte, cpu)) { 2403 /* 2404 * The architectural range of GM blocksize is 2-6, however qemu 2405 * doesn't support blocksize of 2 (see HELPER(ldgm)). 2406 */ 2407 if (tcg_enabled()) { 2408 assert(cpu->gm_blocksize >= 3 && cpu->gm_blocksize <= 6); 2409 } 2410 2411 #ifndef CONFIG_USER_ONLY 2412 /* 2413 * If we run with TCG and do not have tag-memory provided by 2414 * the machine, then reduce MTE support to instructions enabled at EL0. 2415 * This matches Cortex-A710 BROADCASTMTE input being LOW. 2416 */ 2417 if (tcg_enabled() && cpu->tag_memory == NULL) { 2418 cpu->isar.id_aa64pfr1 = 2419 FIELD_DP64(cpu->isar.id_aa64pfr1, ID_AA64PFR1, MTE, 1); 2420 } 2421 2422 /* 2423 * If MTE is supported by the host, however it should not be 2424 * enabled on the guest (i.e mte=off), clear guest's MTE bits." 2425 */ 2426 if (kvm_enabled() && !cpu->kvm_mte) { 2427 FIELD_DP64(cpu->isar.id_aa64pfr1, ID_AA64PFR1, MTE, 0); 2428 } 2429 #endif 2430 } 2431 2432 #ifndef CONFIG_USER_ONLY 2433 if (tcg_enabled() && cpu_isar_feature(aa64_wfxt, cpu)) { 2434 cpu->wfxt_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, 2435 arm_wfxt_timer_cb, cpu); 2436 } 2437 #endif 2438 2439 if (tcg_enabled()) { 2440 /* 2441 * Don't report some architectural features in the ID registers 2442 * where TCG does not yet implement it (not even a minimal 2443 * stub version). This avoids guests falling over when they 2444 * try to access the non-existent system registers for them. 2445 */ 2446 /* FEAT_SPE (Statistical Profiling Extension) */ 2447 cpu->isar.id_aa64dfr0 = 2448 FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMSVER, 0); 2449 /* FEAT_TRBE (Trace Buffer Extension) */ 2450 cpu->isar.id_aa64dfr0 = 2451 FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEBUFFER, 0); 2452 /* FEAT_TRF (Self-hosted Trace Extension) */ 2453 cpu->isar.id_aa64dfr0 = 2454 FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEFILT, 0); 2455 cpu->isar.id_dfr0 = 2456 FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, TRACEFILT, 0); 2457 /* Trace Macrocell system register access */ 2458 cpu->isar.id_aa64dfr0 = 2459 FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEVER, 0); 2460 cpu->isar.id_dfr0 = 2461 FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, COPTRC, 0); 2462 /* Memory mapped trace */ 2463 cpu->isar.id_dfr0 = 2464 FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, MMAPTRC, 0); 2465 /* FEAT_AMU (Activity Monitors Extension) */ 2466 cpu->isar.id_aa64pfr0 = 2467 FIELD_DP64(cpu->isar.id_aa64pfr0, ID_AA64PFR0, AMU, 0); 2468 cpu->isar.id_pfr0 = 2469 FIELD_DP32(cpu->isar.id_pfr0, ID_PFR0, AMU, 0); 2470 /* FEAT_MPAM (Memory Partitioning and Monitoring Extension) */ 2471 cpu->isar.id_aa64pfr0 = 2472 FIELD_DP64(cpu->isar.id_aa64pfr0, ID_AA64PFR0, MPAM, 0); 2473 } 2474 2475 /* MPU can be configured out of a PMSA CPU either by setting has-mpu 2476 * to false or by setting pmsav7-dregion to 0. 2477 */ 2478 if (!cpu->has_mpu || cpu->pmsav7_dregion == 0) { 2479 cpu->has_mpu = false; 2480 cpu->pmsav7_dregion = 0; 2481 cpu->pmsav8r_hdregion = 0; 2482 } 2483 2484 if (arm_feature(env, ARM_FEATURE_PMSA) && 2485 arm_feature(env, ARM_FEATURE_V7)) { 2486 uint32_t nr = cpu->pmsav7_dregion; 2487 2488 if (nr > 0xff) { 2489 error_setg(errp, "PMSAv7 MPU #regions invalid %" PRIu32, nr); 2490 return; 2491 } 2492 2493 if (nr) { 2494 if (arm_feature(env, ARM_FEATURE_V8)) { 2495 /* PMSAv8 */ 2496 env->pmsav8.rbar[M_REG_NS] = g_new0(uint32_t, nr); 2497 env->pmsav8.rlar[M_REG_NS] = g_new0(uint32_t, nr); 2498 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 2499 env->pmsav8.rbar[M_REG_S] = g_new0(uint32_t, nr); 2500 env->pmsav8.rlar[M_REG_S] = g_new0(uint32_t, nr); 2501 } 2502 } else { 2503 env->pmsav7.drbar = g_new0(uint32_t, nr); 2504 env->pmsav7.drsr = g_new0(uint32_t, nr); 2505 env->pmsav7.dracr = g_new0(uint32_t, nr); 2506 } 2507 } 2508 2509 if (cpu->pmsav8r_hdregion > 0xff) { 2510 error_setg(errp, "PMSAv8 MPU EL2 #regions invalid %" PRIu32, 2511 cpu->pmsav8r_hdregion); 2512 return; 2513 } 2514 2515 if (cpu->pmsav8r_hdregion) { 2516 env->pmsav8.hprbar = g_new0(uint32_t, 2517 cpu->pmsav8r_hdregion); 2518 env->pmsav8.hprlar = g_new0(uint32_t, 2519 cpu->pmsav8r_hdregion); 2520 } 2521 } 2522 2523 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 2524 uint32_t nr = cpu->sau_sregion; 2525 2526 if (nr > 0xff) { 2527 error_setg(errp, "v8M SAU #regions invalid %" PRIu32, nr); 2528 return; 2529 } 2530 2531 if (nr) { 2532 env->sau.rbar = g_new0(uint32_t, nr); 2533 env->sau.rlar = g_new0(uint32_t, nr); 2534 } 2535 } 2536 2537 if (arm_feature(env, ARM_FEATURE_EL3)) { 2538 set_feature(env, ARM_FEATURE_VBAR); 2539 } 2540 2541 #ifndef CONFIG_USER_ONLY 2542 if (tcg_enabled() && cpu_isar_feature(aa64_rme, cpu)) { 2543 arm_register_el_change_hook(cpu, >_rme_post_el_change, 0); 2544 } 2545 #endif 2546 2547 register_cp_regs_for_features(cpu); 2548 arm_cpu_register_gdb_regs_for_features(cpu); 2549 arm_cpu_register_gdb_commands(cpu); 2550 2551 init_cpreg_list(cpu); 2552 2553 #ifndef CONFIG_USER_ONLY 2554 MachineState *ms = MACHINE(qdev_get_machine()); 2555 unsigned int smp_cpus = ms->smp.cpus; 2556 bool has_secure = cpu->has_el3 || arm_feature(env, ARM_FEATURE_M_SECURITY); 2557 2558 /* 2559 * We must set cs->num_ases to the final value before 2560 * the first call to cpu_address_space_init. 2561 */ 2562 if (cpu->tag_memory != NULL) { 2563 cs->num_ases = 3 + has_secure; 2564 } else { 2565 cs->num_ases = 1 + has_secure; 2566 } 2567 2568 if (has_secure) { 2569 if (!cpu->secure_memory) { 2570 cpu->secure_memory = cs->memory; 2571 } 2572 cpu_address_space_init(cs, ARMASIdx_S, "cpu-secure-memory", 2573 cpu->secure_memory); 2574 } 2575 2576 if (cpu->tag_memory != NULL) { 2577 cpu_address_space_init(cs, ARMASIdx_TagNS, "cpu-tag-memory", 2578 cpu->tag_memory); 2579 if (has_secure) { 2580 cpu_address_space_init(cs, ARMASIdx_TagS, "cpu-tag-memory", 2581 cpu->secure_tag_memory); 2582 } 2583 } 2584 2585 cpu_address_space_init(cs, ARMASIdx_NS, "cpu-memory", cs->memory); 2586 2587 /* No core_count specified, default to smp_cpus. */ 2588 if (cpu->core_count == -1) { 2589 cpu->core_count = smp_cpus; 2590 } 2591 #endif 2592 2593 if (tcg_enabled()) { 2594 int dcz_blocklen = 4 << cpu->dcz_blocksize; 2595 2596 /* 2597 * We only support DCZ blocklen that fits on one page. 2598 * 2599 * Architectually this is always true. However TARGET_PAGE_SIZE 2600 * is variable and, for compatibility with -machine virt-2.7, 2601 * is only 1KiB, as an artifact of legacy ARMv5 subpage support. 2602 * But even then, while the largest architectural DCZ blocklen 2603 * is 2KiB, no cpu actually uses such a large blocklen. 2604 */ 2605 assert(dcz_blocklen <= TARGET_PAGE_SIZE); 2606 2607 /* 2608 * We only support DCZ blocksize >= 2*TAG_GRANULE, which is to say 2609 * both nibbles of each byte storing tag data may be written at once. 2610 * Since TAG_GRANULE is 16, this means that blocklen must be >= 32. 2611 */ 2612 if (cpu_isar_feature(aa64_mte, cpu)) { 2613 assert(dcz_blocklen >= 2 * TAG_GRANULE); 2614 } 2615 } 2616 2617 qemu_init_vcpu(cs); 2618 cpu_reset(cs); 2619 2620 acc->parent_realize(dev, errp); 2621 } 2622 2623 static ObjectClass *arm_cpu_class_by_name(const char *cpu_model) 2624 { 2625 ObjectClass *oc; 2626 char *typename; 2627 char **cpuname; 2628 const char *cpunamestr; 2629 2630 cpuname = g_strsplit(cpu_model, ",", 1); 2631 cpunamestr = cpuname[0]; 2632 #ifdef CONFIG_USER_ONLY 2633 /* For backwards compatibility usermode emulation allows "-cpu any", 2634 * which has the same semantics as "-cpu max". 2635 */ 2636 if (!strcmp(cpunamestr, "any")) { 2637 cpunamestr = "max"; 2638 } 2639 #endif 2640 typename = g_strdup_printf(ARM_CPU_TYPE_NAME("%s"), cpunamestr); 2641 oc = object_class_by_name(typename); 2642 g_strfreev(cpuname); 2643 g_free(typename); 2644 2645 return oc; 2646 } 2647 2648 static const Property arm_cpu_properties[] = { 2649 DEFINE_PROP_UINT64("midr", ARMCPU, midr, 0), 2650 DEFINE_PROP_UINT64("mp-affinity", ARMCPU, 2651 mp_affinity, ARM64_AFFINITY_INVALID), 2652 DEFINE_PROP_INT32("node-id", ARMCPU, node_id, CPU_UNSET_NUMA_NODE_ID), 2653 DEFINE_PROP_INT32("core-count", ARMCPU, core_count, -1), 2654 /* True to default to the backward-compat old CNTFRQ rather than 1Ghz */ 2655 DEFINE_PROP_BOOL("backcompat-cntfrq", ARMCPU, backcompat_cntfrq, false), 2656 }; 2657 2658 static const gchar *arm_gdb_arch_name(CPUState *cs) 2659 { 2660 ARMCPU *cpu = ARM_CPU(cs); 2661 CPUARMState *env = &cpu->env; 2662 2663 if (arm_feature(env, ARM_FEATURE_IWMMXT)) { 2664 return "iwmmxt"; 2665 } 2666 return "arm"; 2667 } 2668 2669 #ifndef CONFIG_USER_ONLY 2670 #include "hw/core/sysemu-cpu-ops.h" 2671 2672 static const struct SysemuCPUOps arm_sysemu_ops = { 2673 .get_phys_page_attrs_debug = arm_cpu_get_phys_page_attrs_debug, 2674 .asidx_from_attrs = arm_asidx_from_attrs, 2675 .write_elf32_note = arm_cpu_write_elf32_note, 2676 .write_elf64_note = arm_cpu_write_elf64_note, 2677 .virtio_is_big_endian = arm_cpu_virtio_is_big_endian, 2678 .legacy_vmsd = &vmstate_arm_cpu, 2679 }; 2680 #endif 2681 2682 #ifdef CONFIG_TCG 2683 static const TCGCPUOps arm_tcg_ops = { 2684 .initialize = arm_translate_init, 2685 .translate_code = arm_translate_code, 2686 .synchronize_from_tb = arm_cpu_synchronize_from_tb, 2687 .debug_excp_handler = arm_debug_excp_handler, 2688 .restore_state_to_opc = arm_restore_state_to_opc, 2689 2690 #ifdef CONFIG_USER_ONLY 2691 .record_sigsegv = arm_cpu_record_sigsegv, 2692 .record_sigbus = arm_cpu_record_sigbus, 2693 #else 2694 .tlb_fill_align = arm_cpu_tlb_fill_align, 2695 .cpu_exec_interrupt = arm_cpu_exec_interrupt, 2696 .cpu_exec_halt = arm_cpu_exec_halt, 2697 .do_interrupt = arm_cpu_do_interrupt, 2698 .do_transaction_failed = arm_cpu_do_transaction_failed, 2699 .do_unaligned_access = arm_cpu_do_unaligned_access, 2700 .adjust_watchpoint_address = arm_adjust_watchpoint_address, 2701 .debug_check_watchpoint = arm_debug_check_watchpoint, 2702 .debug_check_breakpoint = arm_debug_check_breakpoint, 2703 #endif /* !CONFIG_USER_ONLY */ 2704 }; 2705 #endif /* CONFIG_TCG */ 2706 2707 static void arm_cpu_class_init(ObjectClass *oc, void *data) 2708 { 2709 ARMCPUClass *acc = ARM_CPU_CLASS(oc); 2710 CPUClass *cc = CPU_CLASS(acc); 2711 DeviceClass *dc = DEVICE_CLASS(oc); 2712 ResettableClass *rc = RESETTABLE_CLASS(oc); 2713 2714 device_class_set_parent_realize(dc, arm_cpu_realizefn, 2715 &acc->parent_realize); 2716 2717 device_class_set_props(dc, arm_cpu_properties); 2718 2719 resettable_class_set_parent_phases(rc, NULL, arm_cpu_reset_hold, NULL, 2720 &acc->parent_phases); 2721 2722 cc->class_by_name = arm_cpu_class_by_name; 2723 cc->has_work = arm_cpu_has_work; 2724 cc->mmu_index = arm_cpu_mmu_index; 2725 cc->dump_state = arm_cpu_dump_state; 2726 cc->set_pc = arm_cpu_set_pc; 2727 cc->get_pc = arm_cpu_get_pc; 2728 cc->gdb_read_register = arm_cpu_gdb_read_register; 2729 cc->gdb_write_register = arm_cpu_gdb_write_register; 2730 #ifndef CONFIG_USER_ONLY 2731 cc->sysemu_ops = &arm_sysemu_ops; 2732 #endif 2733 cc->gdb_arch_name = arm_gdb_arch_name; 2734 cc->gdb_stop_before_watchpoint = true; 2735 cc->disas_set_info = arm_disas_set_info; 2736 2737 #ifdef CONFIG_TCG 2738 cc->tcg_ops = &arm_tcg_ops; 2739 #endif /* CONFIG_TCG */ 2740 } 2741 2742 static void arm_cpu_instance_init(Object *obj) 2743 { 2744 ARMCPUClass *acc = ARM_CPU_GET_CLASS(obj); 2745 2746 acc->info->initfn(obj); 2747 arm_cpu_post_init(obj); 2748 } 2749 2750 static void cpu_register_class_init(ObjectClass *oc, void *data) 2751 { 2752 ARMCPUClass *acc = ARM_CPU_CLASS(oc); 2753 CPUClass *cc = CPU_CLASS(acc); 2754 2755 acc->info = data; 2756 cc->gdb_core_xml_file = "arm-core.xml"; 2757 } 2758 2759 void arm_cpu_register(const ARMCPUInfo *info) 2760 { 2761 TypeInfo type_info = { 2762 .parent = TYPE_ARM_CPU, 2763 .instance_init = arm_cpu_instance_init, 2764 .class_init = info->class_init ?: cpu_register_class_init, 2765 .class_data = (void *)info, 2766 }; 2767 2768 type_info.name = g_strdup_printf("%s-" TYPE_ARM_CPU, info->name); 2769 type_register_static(&type_info); 2770 g_free((void *)type_info.name); 2771 } 2772 2773 static const TypeInfo arm_cpu_type_info = { 2774 .name = TYPE_ARM_CPU, 2775 .parent = TYPE_CPU, 2776 .instance_size = sizeof(ARMCPU), 2777 .instance_align = __alignof__(ARMCPU), 2778 .instance_init = arm_cpu_initfn, 2779 .instance_finalize = arm_cpu_finalizefn, 2780 .abstract = true, 2781 .class_size = sizeof(ARMCPUClass), 2782 .class_init = arm_cpu_class_init, 2783 }; 2784 2785 static void arm_cpu_register_types(void) 2786 { 2787 type_register_static(&arm_cpu_type_info); 2788 } 2789 2790 type_init(arm_cpu_register_types) 2791