1fcf5ef2aSThomas Huth /* 2fcf5ef2aSThomas Huth * QEMU ARM CPU 3fcf5ef2aSThomas Huth * 4fcf5ef2aSThomas Huth * Copyright (c) 2012 SUSE LINUX Products GmbH 5fcf5ef2aSThomas Huth * 6fcf5ef2aSThomas Huth * This program is free software; you can redistribute it and/or 7fcf5ef2aSThomas Huth * modify it under the terms of the GNU General Public License 8fcf5ef2aSThomas Huth * as published by the Free Software Foundation; either version 2 9fcf5ef2aSThomas Huth * of the License, or (at your option) any later version. 10fcf5ef2aSThomas Huth * 11fcf5ef2aSThomas Huth * This program is distributed in the hope that it will be useful, 12fcf5ef2aSThomas Huth * but WITHOUT ANY WARRANTY; without even the implied warranty of 13fcf5ef2aSThomas Huth * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14fcf5ef2aSThomas Huth * GNU General Public License for more details. 15fcf5ef2aSThomas Huth * 16fcf5ef2aSThomas Huth * You should have received a copy of the GNU General Public License 17fcf5ef2aSThomas Huth * along with this program; if not, see 18fcf5ef2aSThomas Huth * <http://www.gnu.org/licenses/gpl-2.0.html> 19fcf5ef2aSThomas Huth */ 20fcf5ef2aSThomas Huth 21fcf5ef2aSThomas Huth #include "qemu/osdep.h" 2286480615SPhilippe Mathieu-Daudé #include "qemu/qemu-print.h" 23b8012ecfSPhilippe Mathieu-Daudé #include "qemu/timer.h" 248cc2246cSPeter Maydell #include "qemu/log.h" 25ec5f7ca8SMarc-André Lureau #include "exec/page-vary.h" 26181962fdSPeter Maydell #include "target/arm/idau.h" 270b8fa32fSMarkus Armbruster #include "qemu/module.h" 28fcf5ef2aSThomas Huth #include "qapi/error.h" 29fcf5ef2aSThomas Huth #include "cpu.h" 3078271684SClaudio Fontana #ifdef CONFIG_TCG 3178271684SClaudio Fontana #include "hw/core/tcg-cpu-ops.h" 3278271684SClaudio Fontana #endif /* CONFIG_TCG */ 33fcf5ef2aSThomas Huth #include "internals.h" 34*5a534314SPeter Maydell #include "cpu-features.h" 35fcf5ef2aSThomas Huth #include "exec/exec-all.h" 36fcf5ef2aSThomas Huth #include "hw/qdev-properties.h" 37fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY) 38fcf5ef2aSThomas Huth #include "hw/loader.h" 39cc7d44c2SLike Xu #include "hw/boards.h" 40165876f2SPhilippe Mathieu-Daudé #ifdef CONFIG_TCG 418f4e07c9SPhilippe Mathieu-Daudé #include "hw/intc/armv7m_nvic.h" 42165876f2SPhilippe Mathieu-Daudé #endif /* CONFIG_TCG */ 43165876f2SPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */ 4414a48c1dSMarkus Armbruster #include "sysemu/tcg.h" 45045e5064SAlexander Graf #include "sysemu/qtest.h" 46b3946626SVincent Palatin #include "sysemu/hw_accel.h" 47fcf5ef2aSThomas Huth #include "kvm_arm.h" 48110f6c70SRichard Henderson #include "disas/capstone.h" 4924f91e81SAlex Bennée #include "fpu/softfloat.h" 50cf7c6d10SRichard Henderson #include "cpregs.h" 51fcf5ef2aSThomas Huth 52fcf5ef2aSThomas Huth static void arm_cpu_set_pc(CPUState *cs, vaddr value) 53fcf5ef2aSThomas Huth { 54fcf5ef2aSThomas Huth ARMCPU *cpu = ARM_CPU(cs); 5542f6ed91SJulia Suvorova CPUARMState *env = &cpu->env; 56fcf5ef2aSThomas Huth 5742f6ed91SJulia Suvorova if (is_a64(env)) { 5842f6ed91SJulia Suvorova env->pc = value; 59063bbd80SRichard Henderson env->thumb = false; 6042f6ed91SJulia Suvorova } else { 6142f6ed91SJulia Suvorova env->regs[15] = value & ~1; 6242f6ed91SJulia Suvorova env->thumb = value & 1; 6342f6ed91SJulia Suvorova } 6442f6ed91SJulia Suvorova } 6542f6ed91SJulia Suvorova 66e4fdf9dfSRichard Henderson static vaddr arm_cpu_get_pc(CPUState *cs) 67e4fdf9dfSRichard Henderson { 68e4fdf9dfSRichard Henderson ARMCPU *cpu = ARM_CPU(cs); 69e4fdf9dfSRichard Henderson CPUARMState *env = &cpu->env; 70e4fdf9dfSRichard Henderson 71e4fdf9dfSRichard Henderson if (is_a64(env)) { 72e4fdf9dfSRichard Henderson return env->pc; 73e4fdf9dfSRichard Henderson } else { 74e4fdf9dfSRichard Henderson return env->regs[15]; 75e4fdf9dfSRichard Henderson } 76e4fdf9dfSRichard Henderson } 77e4fdf9dfSRichard Henderson 78ec62595bSEduardo Habkost #ifdef CONFIG_TCG 7978271684SClaudio Fontana void arm_cpu_synchronize_from_tb(CPUState *cs, 8004a37d4cSRichard Henderson const TranslationBlock *tb) 8142f6ed91SJulia Suvorova { 8203a648c4SAnton Johansson /* The program counter is always up to date with CF_PCREL. */ 8303a648c4SAnton Johansson if (!(tb_cflags(tb) & CF_PCREL)) { 84b77af26eSRichard Henderson CPUARMState *env = cpu_env(cs); 8542f6ed91SJulia Suvorova /* 8642f6ed91SJulia Suvorova * It's OK to look at env for the current mode here, because it's 8742f6ed91SJulia Suvorova * never possible for an AArch64 TB to chain to an AArch32 TB. 8842f6ed91SJulia Suvorova */ 8942f6ed91SJulia Suvorova if (is_a64(env)) { 90f51a1dd7SAnton Johansson env->pc = tb->pc; 9142f6ed91SJulia Suvorova } else { 92f51a1dd7SAnton Johansson env->regs[15] = tb->pc; 9342f6ed91SJulia Suvorova } 94fcf5ef2aSThomas Huth } 95abb80995SRichard Henderson } 9656c6c98dSRichard Henderson 97475e56b6SEvgeny Ermakov void arm_restore_state_to_opc(CPUState *cs, 9856c6c98dSRichard Henderson const TranslationBlock *tb, 9956c6c98dSRichard Henderson const uint64_t *data) 10056c6c98dSRichard Henderson { 101b77af26eSRichard Henderson CPUARMState *env = cpu_env(cs); 10256c6c98dSRichard Henderson 10356c6c98dSRichard Henderson if (is_a64(env)) { 10403a648c4SAnton Johansson if (tb_cflags(tb) & CF_PCREL) { 10556c6c98dSRichard Henderson env->pc = (env->pc & TARGET_PAGE_MASK) | data[0]; 10656c6c98dSRichard Henderson } else { 10756c6c98dSRichard Henderson env->pc = data[0]; 10856c6c98dSRichard Henderson } 10956c6c98dSRichard Henderson env->condexec_bits = 0; 11056c6c98dSRichard Henderson env->exception.syndrome = data[2] << ARM_INSN_START_WORD2_SHIFT; 11156c6c98dSRichard Henderson } else { 11203a648c4SAnton Johansson if (tb_cflags(tb) & CF_PCREL) { 11356c6c98dSRichard Henderson env->regs[15] = (env->regs[15] & TARGET_PAGE_MASK) | data[0]; 11456c6c98dSRichard Henderson } else { 11556c6c98dSRichard Henderson env->regs[15] = data[0]; 11656c6c98dSRichard Henderson } 11756c6c98dSRichard Henderson env->condexec_bits = data[1]; 11856c6c98dSRichard Henderson env->exception.syndrome = data[2] << ARM_INSN_START_WORD2_SHIFT; 11956c6c98dSRichard Henderson } 12056c6c98dSRichard Henderson } 121ec62595bSEduardo Habkost #endif /* CONFIG_TCG */ 122fcf5ef2aSThomas Huth 123fcf5ef2aSThomas Huth static bool arm_cpu_has_work(CPUState *cs) 124fcf5ef2aSThomas Huth { 125fcf5ef2aSThomas Huth ARMCPU *cpu = ARM_CPU(cs); 126fcf5ef2aSThomas Huth 127062ba099SAlex Bennée return (cpu->power_state != PSCI_OFF) 128fcf5ef2aSThomas Huth && cs->interrupt_request & 129fcf5ef2aSThomas Huth (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD 1303c29632fSRichard Henderson | CPU_INTERRUPT_VFIQ | CPU_INTERRUPT_VIRQ | CPU_INTERRUPT_VSERR 131fcf5ef2aSThomas Huth | CPU_INTERRUPT_EXITTB); 132fcf5ef2aSThomas Huth } 133fcf5ef2aSThomas Huth 134b5c53d1bSAaron Lindsay void arm_register_pre_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook, 135b5c53d1bSAaron Lindsay void *opaque) 136b5c53d1bSAaron Lindsay { 137b5c53d1bSAaron Lindsay ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1); 138b5c53d1bSAaron Lindsay 139b5c53d1bSAaron Lindsay entry->hook = hook; 140b5c53d1bSAaron Lindsay entry->opaque = opaque; 141b5c53d1bSAaron Lindsay 142b5c53d1bSAaron Lindsay QLIST_INSERT_HEAD(&cpu->pre_el_change_hooks, entry, node); 143b5c53d1bSAaron Lindsay } 144b5c53d1bSAaron Lindsay 14508267487SAaron Lindsay void arm_register_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook, 146fcf5ef2aSThomas Huth void *opaque) 147fcf5ef2aSThomas Huth { 14808267487SAaron Lindsay ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1); 14908267487SAaron Lindsay 15008267487SAaron Lindsay entry->hook = hook; 15108267487SAaron Lindsay entry->opaque = opaque; 15208267487SAaron Lindsay 15308267487SAaron Lindsay QLIST_INSERT_HEAD(&cpu->el_change_hooks, entry, node); 154fcf5ef2aSThomas Huth } 155fcf5ef2aSThomas Huth 156fcf5ef2aSThomas Huth static void cp_reg_reset(gpointer key, gpointer value, gpointer opaque) 157fcf5ef2aSThomas Huth { 158fcf5ef2aSThomas Huth /* Reset a single ARMCPRegInfo register */ 159fcf5ef2aSThomas Huth ARMCPRegInfo *ri = value; 160fcf5ef2aSThomas Huth ARMCPU *cpu = opaque; 161fcf5ef2aSThomas Huth 16287c3f0f2SRichard Henderson if (ri->type & (ARM_CP_SPECIAL_MASK | ARM_CP_ALIAS)) { 163fcf5ef2aSThomas Huth return; 164fcf5ef2aSThomas Huth } 165fcf5ef2aSThomas Huth 166fcf5ef2aSThomas Huth if (ri->resetfn) { 167fcf5ef2aSThomas Huth ri->resetfn(&cpu->env, ri); 168fcf5ef2aSThomas Huth return; 169fcf5ef2aSThomas Huth } 170fcf5ef2aSThomas Huth 171fcf5ef2aSThomas Huth /* A zero offset is never possible as it would be regs[0] 172fcf5ef2aSThomas Huth * so we use it to indicate that reset is being handled elsewhere. 173fcf5ef2aSThomas Huth * This is basically only used for fields in non-core coprocessors 174fcf5ef2aSThomas Huth * (like the pxa2xx ones). 175fcf5ef2aSThomas Huth */ 176fcf5ef2aSThomas Huth if (!ri->fieldoffset) { 177fcf5ef2aSThomas Huth return; 178fcf5ef2aSThomas Huth } 179fcf5ef2aSThomas Huth 180fcf5ef2aSThomas Huth if (cpreg_field_is_64bit(ri)) { 181fcf5ef2aSThomas Huth CPREG_FIELD64(&cpu->env, ri) = ri->resetvalue; 182fcf5ef2aSThomas Huth } else { 183fcf5ef2aSThomas Huth CPREG_FIELD32(&cpu->env, ri) = ri->resetvalue; 184fcf5ef2aSThomas Huth } 185fcf5ef2aSThomas Huth } 186fcf5ef2aSThomas Huth 187fcf5ef2aSThomas Huth static void cp_reg_check_reset(gpointer key, gpointer value, gpointer opaque) 188fcf5ef2aSThomas Huth { 189fcf5ef2aSThomas Huth /* Purely an assertion check: we've already done reset once, 190fcf5ef2aSThomas Huth * so now check that running the reset for the cpreg doesn't 191fcf5ef2aSThomas Huth * change its value. This traps bugs where two different cpregs 192fcf5ef2aSThomas Huth * both try to reset the same state field but to different values. 193fcf5ef2aSThomas Huth */ 194fcf5ef2aSThomas Huth ARMCPRegInfo *ri = value; 195fcf5ef2aSThomas Huth ARMCPU *cpu = opaque; 196fcf5ef2aSThomas Huth uint64_t oldvalue, newvalue; 197fcf5ef2aSThomas Huth 19887c3f0f2SRichard Henderson if (ri->type & (ARM_CP_SPECIAL_MASK | ARM_CP_ALIAS | ARM_CP_NO_RAW)) { 199fcf5ef2aSThomas Huth return; 200fcf5ef2aSThomas Huth } 201fcf5ef2aSThomas Huth 202fcf5ef2aSThomas Huth oldvalue = read_raw_cp_reg(&cpu->env, ri); 203fcf5ef2aSThomas Huth cp_reg_reset(key, value, opaque); 204fcf5ef2aSThomas Huth newvalue = read_raw_cp_reg(&cpu->env, ri); 205fcf5ef2aSThomas Huth assert(oldvalue == newvalue); 206fcf5ef2aSThomas Huth } 207fcf5ef2aSThomas Huth 2089130cadeSPeter Maydell static void arm_cpu_reset_hold(Object *obj) 209fcf5ef2aSThomas Huth { 2109130cadeSPeter Maydell CPUState *s = CPU(obj); 211fcf5ef2aSThomas Huth ARMCPU *cpu = ARM_CPU(s); 212fcf5ef2aSThomas Huth ARMCPUClass *acc = ARM_CPU_GET_CLASS(cpu); 213fcf5ef2aSThomas Huth CPUARMState *env = &cpu->env; 214fcf5ef2aSThomas Huth 2159130cadeSPeter Maydell if (acc->parent_phases.hold) { 2169130cadeSPeter Maydell acc->parent_phases.hold(obj); 2179130cadeSPeter Maydell } 218fcf5ef2aSThomas Huth 2191f5c00cfSAlex Bennée memset(env, 0, offsetof(CPUARMState, end_reset_fields)); 2201f5c00cfSAlex Bennée 221fcf5ef2aSThomas Huth g_hash_table_foreach(cpu->cp_regs, cp_reg_reset, cpu); 222fcf5ef2aSThomas Huth g_hash_table_foreach(cpu->cp_regs, cp_reg_check_reset, cpu); 223fcf5ef2aSThomas Huth 224fcf5ef2aSThomas Huth env->vfp.xregs[ARM_VFP_FPSID] = cpu->reset_fpsid; 22547576b94SRichard Henderson env->vfp.xregs[ARM_VFP_MVFR0] = cpu->isar.mvfr0; 22647576b94SRichard Henderson env->vfp.xregs[ARM_VFP_MVFR1] = cpu->isar.mvfr1; 22747576b94SRichard Henderson env->vfp.xregs[ARM_VFP_MVFR2] = cpu->isar.mvfr2; 228fcf5ef2aSThomas Huth 229c1b70158SThiago Jung Bauermann cpu->power_state = s->start_powered_off ? PSCI_OFF : PSCI_ON; 230fcf5ef2aSThomas Huth 231fcf5ef2aSThomas Huth if (arm_feature(env, ARM_FEATURE_IWMMXT)) { 232fcf5ef2aSThomas Huth env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q'; 233fcf5ef2aSThomas Huth } 234fcf5ef2aSThomas Huth 235fcf5ef2aSThomas Huth if (arm_feature(env, ARM_FEATURE_AARCH64)) { 236fcf5ef2aSThomas Huth /* 64 bit CPUs always start in 64 bit mode */ 23753221552SRichard Henderson env->aarch64 = true; 238fcf5ef2aSThomas Huth #if defined(CONFIG_USER_ONLY) 239fcf5ef2aSThomas Huth env->pstate = PSTATE_MODE_EL0t; 240fcf5ef2aSThomas Huth /* Userspace expects access to DC ZVA, CTL_EL0 and the cache ops */ 241fcf5ef2aSThomas Huth env->cp15.sctlr_el[1] |= SCTLR_UCT | SCTLR_UCI | SCTLR_DZE; 242276c6e81SRichard Henderson /* Enable all PAC keys. */ 243276c6e81SRichard Henderson env->cp15.sctlr_el[1] |= (SCTLR_EnIA | SCTLR_EnIB | 244276c6e81SRichard Henderson SCTLR_EnDA | SCTLR_EnDB); 245cda86e2bSRichard Henderson /* Trap on btype=3 for PACIxSP. */ 246cda86e2bSRichard Henderson env->cp15.sctlr_el[1] |= SCTLR_BT0; 247d03396a8SRichard Henderson /* Trap on implementation defined registers. */ 248d03396a8SRichard Henderson if (cpu_isar_feature(aa64_tidcp1, cpu)) { 249d03396a8SRichard Henderson env->cp15.sctlr_el[1] |= SCTLR_TIDCP; 250d03396a8SRichard Henderson } 251fcf5ef2aSThomas Huth /* and to the FP/Neon instructions */ 252fab8ad39SRichard Henderson env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1, 253fab8ad39SRichard Henderson CPACR_EL1, FPEN, 3); 25446303535SRichard Henderson /* and to the SVE instructions, with default vector length */ 25546303535SRichard Henderson if (cpu_isar_feature(aa64_sve, cpu)) { 256fab8ad39SRichard Henderson env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1, 257fab8ad39SRichard Henderson CPACR_EL1, ZEN, 3); 25887252bdeSRichard Henderson env->vfp.zcr_el[1] = cpu->sve_default_vq - 1; 2597b6a2198SAlex Bennée } 26078011586SRichard Henderson /* and for SME instructions, with default vector length, and TPIDR2 */ 26178011586SRichard Henderson if (cpu_isar_feature(aa64_sme, cpu)) { 26278011586SRichard Henderson env->cp15.sctlr_el[1] |= SCTLR_EnTP2; 26378011586SRichard Henderson env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1, 26478011586SRichard Henderson CPACR_EL1, SMEN, 3); 26578011586SRichard Henderson env->vfp.smcr_el[1] = cpu->sme_default_vq - 1; 26678011586SRichard Henderson if (cpu_isar_feature(aa64_sme_fa64, cpu)) { 26778011586SRichard Henderson env->vfp.smcr_el[1] = FIELD_DP64(env->vfp.smcr_el[1], 26878011586SRichard Henderson SMCR, FA64, 1); 26978011586SRichard Henderson } 27078011586SRichard Henderson } 271f6a148feSRichard Henderson /* 272691f1ffdSRichard Henderson * Enable 48-bit address space (TODO: take reserved_va into account). 27316c84978SRichard Henderson * Enable TBI0 but not TBI1. 27416c84978SRichard Henderson * Note that this must match useronly_clean_ptr. 275f6a148feSRichard Henderson */ 276cb4a0a34SPeter Maydell env->cp15.tcr_el[1] = 5 | (1ULL << 37); 277e3232864SRichard Henderson 278e3232864SRichard Henderson /* Enable MTE */ 279e3232864SRichard Henderson if (cpu_isar_feature(aa64_mte, cpu)) { 280e3232864SRichard Henderson /* Enable tag access, but leave TCF0 as No Effect (0). */ 281e3232864SRichard Henderson env->cp15.sctlr_el[1] |= SCTLR_ATA0; 282e3232864SRichard Henderson /* 283e3232864SRichard Henderson * Exclude all tags, so that tag 0 is always used. 284e3232864SRichard Henderson * This corresponds to Linux current->thread.gcr_incl = 0. 285e3232864SRichard Henderson * 286e3232864SRichard Henderson * Set RRND, so that helper_irg() will generate a seed later. 287e3232864SRichard Henderson * Here in cpu_reset(), the crypto subsystem has not yet been 288e3232864SRichard Henderson * initialized. 289e3232864SRichard Henderson */ 290e3232864SRichard Henderson env->cp15.gcr_el1 = 0x1ffff; 291e3232864SRichard Henderson } 2927cb1e618SRichard Henderson /* 2937cb1e618SRichard Henderson * Disable access to SCXTNUM_EL0 from CSV2_1p2. 2947cb1e618SRichard Henderson * This is not yet exposed from the Linux kernel in any way. 2957cb1e618SRichard Henderson */ 2967cb1e618SRichard Henderson env->cp15.sctlr_el[1] |= SCTLR_TSCXT; 297f9ac7788SZhuojia Shen /* Disable access to Debug Communication Channel (DCC). */ 298f9ac7788SZhuojia Shen env->cp15.mdscr_el1 |= 1 << 12; 299fcf5ef2aSThomas Huth #else 300fcf5ef2aSThomas Huth /* Reset into the highest available EL */ 301fcf5ef2aSThomas Huth if (arm_feature(env, ARM_FEATURE_EL3)) { 302fcf5ef2aSThomas Huth env->pstate = PSTATE_MODE_EL3h; 303fcf5ef2aSThomas Huth } else if (arm_feature(env, ARM_FEATURE_EL2)) { 304fcf5ef2aSThomas Huth env->pstate = PSTATE_MODE_EL2h; 305fcf5ef2aSThomas Huth } else { 306fcf5ef2aSThomas Huth env->pstate = PSTATE_MODE_EL1h; 307fcf5ef2aSThomas Huth } 3084a7319b7SEdgar E. Iglesias 3094a7319b7SEdgar E. Iglesias /* Sample rvbar at reset. */ 3104a7319b7SEdgar E. Iglesias env->cp15.rvbar = cpu->rvbar_prop; 3114a7319b7SEdgar E. Iglesias env->pc = env->cp15.rvbar; 312fcf5ef2aSThomas Huth #endif 313fcf5ef2aSThomas Huth } else { 314fcf5ef2aSThomas Huth #if defined(CONFIG_USER_ONLY) 315fcf5ef2aSThomas Huth /* Userspace expects access to cp10 and cp11 for FP/Neon */ 316fab8ad39SRichard Henderson env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1, 317fab8ad39SRichard Henderson CPACR, CP10, 3); 318fab8ad39SRichard Henderson env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1, 319fab8ad39SRichard Henderson CPACR, CP11, 3); 320fcf5ef2aSThomas Huth #endif 321910e4f24STobias Röhmel if (arm_feature(env, ARM_FEATURE_V8)) { 322910e4f24STobias Röhmel env->cp15.rvbar = cpu->rvbar_prop; 323910e4f24STobias Röhmel env->regs[15] = cpu->rvbar_prop; 324910e4f24STobias Röhmel } 325fcf5ef2aSThomas Huth } 326fcf5ef2aSThomas Huth 327fcf5ef2aSThomas Huth #if defined(CONFIG_USER_ONLY) 328fcf5ef2aSThomas Huth env->uncached_cpsr = ARM_CPU_MODE_USR; 329fcf5ef2aSThomas Huth /* For user mode we must enable access to coprocessors */ 330fcf5ef2aSThomas Huth env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30; 331fcf5ef2aSThomas Huth if (arm_feature(env, ARM_FEATURE_IWMMXT)) { 332fcf5ef2aSThomas Huth env->cp15.c15_cpar = 3; 333fcf5ef2aSThomas Huth } else if (arm_feature(env, ARM_FEATURE_XSCALE)) { 334fcf5ef2aSThomas Huth env->cp15.c15_cpar = 1; 335fcf5ef2aSThomas Huth } 336fcf5ef2aSThomas Huth #else 337060a65dfSPeter Maydell 338060a65dfSPeter Maydell /* 339060a65dfSPeter Maydell * If the highest available EL is EL2, AArch32 will start in Hyp 340060a65dfSPeter Maydell * mode; otherwise it starts in SVC. Note that if we start in 341060a65dfSPeter Maydell * AArch64 then these values in the uncached_cpsr will be ignored. 342060a65dfSPeter Maydell */ 343060a65dfSPeter Maydell if (arm_feature(env, ARM_FEATURE_EL2) && 344060a65dfSPeter Maydell !arm_feature(env, ARM_FEATURE_EL3)) { 345060a65dfSPeter Maydell env->uncached_cpsr = ARM_CPU_MODE_HYP; 346060a65dfSPeter Maydell } else { 347fcf5ef2aSThomas Huth env->uncached_cpsr = ARM_CPU_MODE_SVC; 348060a65dfSPeter Maydell } 349fcf5ef2aSThomas Huth env->daif = PSTATE_D | PSTATE_A | PSTATE_I | PSTATE_F; 3501426f244SPeter Maydell 3511426f244SPeter Maydell /* AArch32 has a hard highvec setting of 0xFFFF0000. If we are currently 3521426f244SPeter Maydell * executing as AArch32 then check if highvecs are enabled and 3531426f244SPeter Maydell * adjust the PC accordingly. 3541426f244SPeter Maydell */ 3551426f244SPeter Maydell if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) { 3561426f244SPeter Maydell env->regs[15] = 0xFFFF0000; 3571426f244SPeter Maydell } 3581426f244SPeter Maydell 3591426f244SPeter Maydell env->vfp.xregs[ARM_VFP_FPEXC] = 0; 360b62ceeafSPeter Maydell #endif 361dc7abe4dSMichael Davidsaver 362531c60a9SPeter Maydell if (arm_feature(env, ARM_FEATURE_M)) { 363b62ceeafSPeter Maydell #ifndef CONFIG_USER_ONLY 364fcf5ef2aSThomas Huth uint32_t initial_msp; /* Loaded from 0x0 */ 365fcf5ef2aSThomas Huth uint32_t initial_pc; /* Loaded from 0x4 */ 366fcf5ef2aSThomas Huth uint8_t *rom; 36738e2a77cSPeter Maydell uint32_t vecbase; 368b62ceeafSPeter Maydell #endif 369fcf5ef2aSThomas Huth 3708128c8e8SPeter Maydell if (cpu_isar_feature(aa32_lob, cpu)) { 3718128c8e8SPeter Maydell /* 3728128c8e8SPeter Maydell * LTPSIZE is constant 4 if MVE not implemented, and resets 3738128c8e8SPeter Maydell * to an UNKNOWN value if MVE is implemented. We choose to 3748128c8e8SPeter Maydell * always reset to 4. 3758128c8e8SPeter Maydell */ 3768128c8e8SPeter Maydell env->v7m.ltpsize = 4; 37799c7834fSPeter Maydell /* The LTPSIZE field in FPDSCR is constant and reads as 4. */ 37899c7834fSPeter Maydell env->v7m.fpdscr[M_REG_NS] = 4 << FPCR_LTPSIZE_SHIFT; 37999c7834fSPeter Maydell env->v7m.fpdscr[M_REG_S] = 4 << FPCR_LTPSIZE_SHIFT; 3808128c8e8SPeter Maydell } 3818128c8e8SPeter Maydell 3821e577cc7SPeter Maydell if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 3831e577cc7SPeter Maydell env->v7m.secure = true; 3843b2e9344SPeter Maydell } else { 3853b2e9344SPeter Maydell /* This bit resets to 0 if security is supported, but 1 if 3863b2e9344SPeter Maydell * it is not. The bit is not present in v7M, but we set it 3873b2e9344SPeter Maydell * here so we can avoid having to make checks on it conditional 3883b2e9344SPeter Maydell * on ARM_FEATURE_V8 (we don't let the guest see the bit). 3893b2e9344SPeter Maydell */ 3903b2e9344SPeter Maydell env->v7m.aircr = R_V7M_AIRCR_BFHFNMINS_MASK; 39102ac2f7fSPeter Maydell /* 39202ac2f7fSPeter Maydell * Set NSACR to indicate "NS access permitted to everything"; 39302ac2f7fSPeter Maydell * this avoids having to have all the tests of it being 39402ac2f7fSPeter Maydell * conditional on ARM_FEATURE_M_SECURITY. Note also that from 39502ac2f7fSPeter Maydell * v8.1M the guest-visible value of NSACR in a CPU without the 39602ac2f7fSPeter Maydell * Security Extension is 0xcff. 39702ac2f7fSPeter Maydell */ 39802ac2f7fSPeter Maydell env->v7m.nsacr = 0xcff; 3991e577cc7SPeter Maydell } 4001e577cc7SPeter Maydell 4019d40cd8aSPeter Maydell /* In v7M the reset value of this bit is IMPDEF, but ARM recommends 4022c4da50dSPeter Maydell * that it resets to 1, so QEMU always does that rather than making 4039d40cd8aSPeter Maydell * it dependent on CPU model. In v8M it is RES1. 4042c4da50dSPeter Maydell */ 4059d40cd8aSPeter Maydell env->v7m.ccr[M_REG_NS] = R_V7M_CCR_STKALIGN_MASK; 4069d40cd8aSPeter Maydell env->v7m.ccr[M_REG_S] = R_V7M_CCR_STKALIGN_MASK; 4079d40cd8aSPeter Maydell if (arm_feature(env, ARM_FEATURE_V8)) { 4089d40cd8aSPeter Maydell /* in v8M the NONBASETHRDENA bit [0] is RES1 */ 4099d40cd8aSPeter Maydell env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_NONBASETHRDENA_MASK; 4109d40cd8aSPeter Maydell env->v7m.ccr[M_REG_S] |= R_V7M_CCR_NONBASETHRDENA_MASK; 4119d40cd8aSPeter Maydell } 41222ab3460SJulia Suvorova if (!arm_feature(env, ARM_FEATURE_M_MAIN)) { 41322ab3460SJulia Suvorova env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_UNALIGN_TRP_MASK; 41422ab3460SJulia Suvorova env->v7m.ccr[M_REG_S] |= R_V7M_CCR_UNALIGN_TRP_MASK; 41522ab3460SJulia Suvorova } 4162c4da50dSPeter Maydell 4177fbc6a40SRichard Henderson if (cpu_isar_feature(aa32_vfp_simd, cpu)) { 418d33abe82SPeter Maydell env->v7m.fpccr[M_REG_NS] = R_V7M_FPCCR_ASPEN_MASK; 419d33abe82SPeter Maydell env->v7m.fpccr[M_REG_S] = R_V7M_FPCCR_ASPEN_MASK | 420d33abe82SPeter Maydell R_V7M_FPCCR_LSPEN_MASK | R_V7M_FPCCR_S_MASK; 421d33abe82SPeter Maydell } 422b62ceeafSPeter Maydell 423b62ceeafSPeter Maydell #ifndef CONFIG_USER_ONLY 424056f43dfSPeter Maydell /* Unlike A/R profile, M profile defines the reset LR value */ 425056f43dfSPeter Maydell env->regs[14] = 0xffffffff; 426056f43dfSPeter Maydell 42738e2a77cSPeter Maydell env->v7m.vecbase[M_REG_S] = cpu->init_svtor & 0xffffff80; 4287cda2149SPeter Maydell env->v7m.vecbase[M_REG_NS] = cpu->init_nsvtor & 0xffffff80; 42938e2a77cSPeter Maydell 43038e2a77cSPeter Maydell /* Load the initial SP and PC from offset 0 and 4 in the vector table */ 43138e2a77cSPeter Maydell vecbase = env->v7m.vecbase[env->v7m.secure]; 43275ce72b7SPeter Maydell rom = rom_ptr_for_as(s->as, vecbase, 8); 433fcf5ef2aSThomas Huth if (rom) { 434fcf5ef2aSThomas Huth /* Address zero is covered by ROM which hasn't yet been 435fcf5ef2aSThomas Huth * copied into physical memory. 436fcf5ef2aSThomas Huth */ 437fcf5ef2aSThomas Huth initial_msp = ldl_p(rom); 438fcf5ef2aSThomas Huth initial_pc = ldl_p(rom + 4); 439fcf5ef2aSThomas Huth } else { 440fcf5ef2aSThomas Huth /* Address zero not covered by a ROM blob, or the ROM blob 441fcf5ef2aSThomas Huth * is in non-modifiable memory and this is a second reset after 442fcf5ef2aSThomas Huth * it got copied into memory. In the latter case, rom_ptr 443fcf5ef2aSThomas Huth * will return a NULL pointer and we should use ldl_phys instead. 444fcf5ef2aSThomas Huth */ 44538e2a77cSPeter Maydell initial_msp = ldl_phys(s->as, vecbase); 44638e2a77cSPeter Maydell initial_pc = ldl_phys(s->as, vecbase + 4); 447fcf5ef2aSThomas Huth } 448fcf5ef2aSThomas Huth 4498cc2246cSPeter Maydell qemu_log_mask(CPU_LOG_INT, 4508cc2246cSPeter Maydell "Loaded reset SP 0x%x PC 0x%x from vector table\n", 4518cc2246cSPeter Maydell initial_msp, initial_pc); 4528cc2246cSPeter Maydell 453fcf5ef2aSThomas Huth env->regs[13] = initial_msp & 0xFFFFFFFC; 454fcf5ef2aSThomas Huth env->regs[15] = initial_pc & ~1; 455fcf5ef2aSThomas Huth env->thumb = initial_pc & 1; 456b62ceeafSPeter Maydell #else 457b62ceeafSPeter Maydell /* 458b62ceeafSPeter Maydell * For user mode we run non-secure and with access to the FPU. 459b62ceeafSPeter Maydell * The FPU context is active (ie does not need further setup) 460b62ceeafSPeter Maydell * and is owned by non-secure. 461b62ceeafSPeter Maydell */ 462b62ceeafSPeter Maydell env->v7m.secure = false; 463b62ceeafSPeter Maydell env->v7m.nsacr = 0xcff; 464b62ceeafSPeter Maydell env->v7m.cpacr[M_REG_NS] = 0xf0ffff; 465b62ceeafSPeter Maydell env->v7m.fpccr[M_REG_S] &= 466b62ceeafSPeter Maydell ~(R_V7M_FPCCR_LSPEN_MASK | R_V7M_FPCCR_S_MASK); 467b62ceeafSPeter Maydell env->v7m.control[M_REG_S] |= R_V7M_CONTROL_FPCA_MASK; 468b62ceeafSPeter Maydell #endif 469fcf5ef2aSThomas Huth } 470fcf5ef2aSThomas Huth 471dc3c4c14SPeter Maydell /* M profile requires that reset clears the exclusive monitor; 472dc3c4c14SPeter Maydell * A profile does not, but clearing it makes more sense than having it 473dc3c4c14SPeter Maydell * set with an exclusive access on address zero. 474dc3c4c14SPeter Maydell */ 475dc3c4c14SPeter Maydell arm_clear_exclusive(env); 476dc3c4c14SPeter Maydell 4770e1a46bbSPeter Maydell if (arm_feature(env, ARM_FEATURE_PMSA)) { 47869ceea64SPeter Maydell if (cpu->pmsav7_dregion > 0) { 4790e1a46bbSPeter Maydell if (arm_feature(env, ARM_FEATURE_V8)) { 48062c58ee0SPeter Maydell memset(env->pmsav8.rbar[M_REG_NS], 0, 48162c58ee0SPeter Maydell sizeof(*env->pmsav8.rbar[M_REG_NS]) 48262c58ee0SPeter Maydell * cpu->pmsav7_dregion); 48362c58ee0SPeter Maydell memset(env->pmsav8.rlar[M_REG_NS], 0, 48462c58ee0SPeter Maydell sizeof(*env->pmsav8.rlar[M_REG_NS]) 48562c58ee0SPeter Maydell * cpu->pmsav7_dregion); 48662c58ee0SPeter Maydell if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 48762c58ee0SPeter Maydell memset(env->pmsav8.rbar[M_REG_S], 0, 48862c58ee0SPeter Maydell sizeof(*env->pmsav8.rbar[M_REG_S]) 48962c58ee0SPeter Maydell * cpu->pmsav7_dregion); 49062c58ee0SPeter Maydell memset(env->pmsav8.rlar[M_REG_S], 0, 49162c58ee0SPeter Maydell sizeof(*env->pmsav8.rlar[M_REG_S]) 49262c58ee0SPeter Maydell * cpu->pmsav7_dregion); 49362c58ee0SPeter Maydell } 4940e1a46bbSPeter Maydell } else if (arm_feature(env, ARM_FEATURE_V7)) { 49569ceea64SPeter Maydell memset(env->pmsav7.drbar, 0, 49669ceea64SPeter Maydell sizeof(*env->pmsav7.drbar) * cpu->pmsav7_dregion); 49769ceea64SPeter Maydell memset(env->pmsav7.drsr, 0, 49869ceea64SPeter Maydell sizeof(*env->pmsav7.drsr) * cpu->pmsav7_dregion); 49969ceea64SPeter Maydell memset(env->pmsav7.dracr, 0, 50069ceea64SPeter Maydell sizeof(*env->pmsav7.dracr) * cpu->pmsav7_dregion); 50169ceea64SPeter Maydell } 5020e1a46bbSPeter Maydell } 503761c4642STobias Röhmel 504761c4642STobias Röhmel if (cpu->pmsav8r_hdregion > 0) { 505761c4642STobias Röhmel memset(env->pmsav8.hprbar, 0, 506761c4642STobias Röhmel sizeof(*env->pmsav8.hprbar) * cpu->pmsav8r_hdregion); 507761c4642STobias Röhmel memset(env->pmsav8.hprlar, 0, 508761c4642STobias Röhmel sizeof(*env->pmsav8.hprlar) * cpu->pmsav8r_hdregion); 509761c4642STobias Röhmel } 510761c4642STobias Röhmel 5111bc04a88SPeter Maydell env->pmsav7.rnr[M_REG_NS] = 0; 5121bc04a88SPeter Maydell env->pmsav7.rnr[M_REG_S] = 0; 5134125e6feSPeter Maydell env->pmsav8.mair0[M_REG_NS] = 0; 5144125e6feSPeter Maydell env->pmsav8.mair0[M_REG_S] = 0; 5154125e6feSPeter Maydell env->pmsav8.mair1[M_REG_NS] = 0; 5164125e6feSPeter Maydell env->pmsav8.mair1[M_REG_S] = 0; 51769ceea64SPeter Maydell } 51869ceea64SPeter Maydell 5199901c576SPeter Maydell if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 5209901c576SPeter Maydell if (cpu->sau_sregion > 0) { 5219901c576SPeter Maydell memset(env->sau.rbar, 0, sizeof(*env->sau.rbar) * cpu->sau_sregion); 5229901c576SPeter Maydell memset(env->sau.rlar, 0, sizeof(*env->sau.rlar) * cpu->sau_sregion); 5239901c576SPeter Maydell } 5249901c576SPeter Maydell env->sau.rnr = 0; 5259901c576SPeter Maydell /* SAU_CTRL reset value is IMPDEF; we choose 0, which is what 5269901c576SPeter Maydell * the Cortex-M33 does. 5279901c576SPeter Maydell */ 5289901c576SPeter Maydell env->sau.ctrl = 0; 5299901c576SPeter Maydell } 5309901c576SPeter Maydell 531fcf5ef2aSThomas Huth set_flush_to_zero(1, &env->vfp.standard_fp_status); 532fcf5ef2aSThomas Huth set_flush_inputs_to_zero(1, &env->vfp.standard_fp_status); 533fcf5ef2aSThomas Huth set_default_nan_mode(1, &env->vfp.standard_fp_status); 534aaae563bSPeter Maydell set_default_nan_mode(1, &env->vfp.standard_fp_status_f16); 535fcf5ef2aSThomas Huth set_float_detect_tininess(float_tininess_before_rounding, 536fcf5ef2aSThomas Huth &env->vfp.fp_status); 537fcf5ef2aSThomas Huth set_float_detect_tininess(float_tininess_before_rounding, 538fcf5ef2aSThomas Huth &env->vfp.standard_fp_status); 539bcc531f0SPeter Maydell set_float_detect_tininess(float_tininess_before_rounding, 540bcc531f0SPeter Maydell &env->vfp.fp_status_f16); 541aaae563bSPeter Maydell set_float_detect_tininess(float_tininess_before_rounding, 542aaae563bSPeter Maydell &env->vfp.standard_fp_status_f16); 543fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY 544fcf5ef2aSThomas Huth if (kvm_enabled()) { 545fcf5ef2aSThomas Huth kvm_arm_reset_vcpu(cpu); 546fcf5ef2aSThomas Huth } 547fcf5ef2aSThomas Huth #endif 548fcf5ef2aSThomas Huth 549fa05d1abSFabiano Rosas if (tcg_enabled()) { 550fcf5ef2aSThomas Huth hw_breakpoint_update_all(cpu); 551fcf5ef2aSThomas Huth hw_watchpoint_update_all(cpu); 5522b77ad4dSFabiano Rosas 553a8a79c7aSRichard Henderson arm_rebuild_hflags(env); 554fcf5ef2aSThomas Huth } 5552b77ad4dSFabiano Rosas } 556fcf5ef2aSThomas Huth 5573a45f4f5SPeter Maydell void arm_emulate_firmware_reset(CPUState *cpustate, int target_el) 5583a45f4f5SPeter Maydell { 5593a45f4f5SPeter Maydell ARMCPU *cpu = ARM_CPU(cpustate); 5603a45f4f5SPeter Maydell CPUARMState *env = &cpu->env; 5613a45f4f5SPeter Maydell bool have_el3 = arm_feature(env, ARM_FEATURE_EL3); 5623a45f4f5SPeter Maydell bool have_el2 = arm_feature(env, ARM_FEATURE_EL2); 5633a45f4f5SPeter Maydell 5643a45f4f5SPeter Maydell /* 5653a45f4f5SPeter Maydell * Check we have the EL we're aiming for. If that is the 5663a45f4f5SPeter Maydell * highest implemented EL, then cpu_reset has already done 5673a45f4f5SPeter Maydell * all the work. 5683a45f4f5SPeter Maydell */ 5693a45f4f5SPeter Maydell switch (target_el) { 5703a45f4f5SPeter Maydell case 3: 5713a45f4f5SPeter Maydell assert(have_el3); 5723a45f4f5SPeter Maydell return; 5733a45f4f5SPeter Maydell case 2: 5743a45f4f5SPeter Maydell assert(have_el2); 5753a45f4f5SPeter Maydell if (!have_el3) { 5763a45f4f5SPeter Maydell return; 5773a45f4f5SPeter Maydell } 5783a45f4f5SPeter Maydell break; 5793a45f4f5SPeter Maydell case 1: 5803a45f4f5SPeter Maydell if (!have_el3 && !have_el2) { 5813a45f4f5SPeter Maydell return; 5823a45f4f5SPeter Maydell } 5833a45f4f5SPeter Maydell break; 5843a45f4f5SPeter Maydell default: 5853a45f4f5SPeter Maydell g_assert_not_reached(); 5863a45f4f5SPeter Maydell } 5873a45f4f5SPeter Maydell 5883a45f4f5SPeter Maydell if (have_el3) { 5893a45f4f5SPeter Maydell /* 5903a45f4f5SPeter Maydell * Set the EL3 state so code can run at EL2. This should match 5913a45f4f5SPeter Maydell * the requirements set by Linux in its booting spec. 5923a45f4f5SPeter Maydell */ 5933a45f4f5SPeter Maydell if (env->aarch64) { 5943a45f4f5SPeter Maydell env->cp15.scr_el3 |= SCR_RW; 5953a45f4f5SPeter Maydell if (cpu_isar_feature(aa64_pauth, cpu)) { 5963a45f4f5SPeter Maydell env->cp15.scr_el3 |= SCR_API | SCR_APK; 5973a45f4f5SPeter Maydell } 5983a45f4f5SPeter Maydell if (cpu_isar_feature(aa64_mte, cpu)) { 5993a45f4f5SPeter Maydell env->cp15.scr_el3 |= SCR_ATA; 6003a45f4f5SPeter Maydell } 6013a45f4f5SPeter Maydell if (cpu_isar_feature(aa64_sve, cpu)) { 6023a45f4f5SPeter Maydell env->cp15.cptr_el[3] |= R_CPTR_EL3_EZ_MASK; 6033a45f4f5SPeter Maydell env->vfp.zcr_el[3] = 0xf; 6043a45f4f5SPeter Maydell } 6053a45f4f5SPeter Maydell if (cpu_isar_feature(aa64_sme, cpu)) { 6063a45f4f5SPeter Maydell env->cp15.cptr_el[3] |= R_CPTR_EL3_ESM_MASK; 6073a45f4f5SPeter Maydell env->cp15.scr_el3 |= SCR_ENTP2; 6083a45f4f5SPeter Maydell env->vfp.smcr_el[3] = 0xf; 6093a45f4f5SPeter Maydell } 6103a45f4f5SPeter Maydell if (cpu_isar_feature(aa64_hcx, cpu)) { 6113a45f4f5SPeter Maydell env->cp15.scr_el3 |= SCR_HXEN; 6123a45f4f5SPeter Maydell } 6133a45f4f5SPeter Maydell if (cpu_isar_feature(aa64_fgt, cpu)) { 6143a45f4f5SPeter Maydell env->cp15.scr_el3 |= SCR_FGTEN; 6153a45f4f5SPeter Maydell } 6163a45f4f5SPeter Maydell } 6173a45f4f5SPeter Maydell 6183a45f4f5SPeter Maydell if (target_el == 2) { 6193a45f4f5SPeter Maydell /* If the guest is at EL2 then Linux expects the HVC insn to work */ 6203a45f4f5SPeter Maydell env->cp15.scr_el3 |= SCR_HCE; 6213a45f4f5SPeter Maydell } 6223a45f4f5SPeter Maydell 6233a45f4f5SPeter Maydell /* Put CPU into non-secure state */ 6243a45f4f5SPeter Maydell env->cp15.scr_el3 |= SCR_NS; 6253a45f4f5SPeter Maydell /* Set NSACR.{CP11,CP10} so NS can access the FPU */ 6263a45f4f5SPeter Maydell env->cp15.nsacr |= 3 << 10; 6273a45f4f5SPeter Maydell } 6283a45f4f5SPeter Maydell 6293a45f4f5SPeter Maydell if (have_el2 && target_el < 2) { 6303a45f4f5SPeter Maydell /* Set EL2 state so code can run at EL1. */ 6313a45f4f5SPeter Maydell if (env->aarch64) { 6323a45f4f5SPeter Maydell env->cp15.hcr_el2 |= HCR_RW; 6333a45f4f5SPeter Maydell } 6343a45f4f5SPeter Maydell } 6353a45f4f5SPeter Maydell 6363a45f4f5SPeter Maydell /* Set the CPU to the desired state */ 6373a45f4f5SPeter Maydell if (env->aarch64) { 6383a45f4f5SPeter Maydell env->pstate = aarch64_pstate_mode(target_el, true); 6393a45f4f5SPeter Maydell } else { 6403a45f4f5SPeter Maydell static const uint32_t mode_for_el[] = { 6413a45f4f5SPeter Maydell 0, 6423a45f4f5SPeter Maydell ARM_CPU_MODE_SVC, 6433a45f4f5SPeter Maydell ARM_CPU_MODE_HYP, 6443a45f4f5SPeter Maydell ARM_CPU_MODE_SVC, 6453a45f4f5SPeter Maydell }; 6463a45f4f5SPeter Maydell 6473a45f4f5SPeter Maydell cpsr_write(env, mode_for_el[target_el], CPSR_M, CPSRWriteRaw); 6483a45f4f5SPeter Maydell } 6493a45f4f5SPeter Maydell } 6503a45f4f5SPeter Maydell 6513a45f4f5SPeter Maydell 6529e406eeaSPhilippe Mathieu-Daudé #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY) 653083afd18SPhilippe Mathieu-Daudé 654310cedf3SRichard Henderson static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx, 655be879556SRichard Henderson unsigned int target_el, 656be879556SRichard Henderson unsigned int cur_el, bool secure, 657be879556SRichard Henderson uint64_t hcr_el2) 658310cedf3SRichard Henderson { 659b77af26eSRichard Henderson CPUARMState *env = cpu_env(cs); 660310cedf3SRichard Henderson bool pstate_unmasked; 66116e07f78SRichard Henderson bool unmasked = false; 662310cedf3SRichard Henderson 663310cedf3SRichard Henderson /* 664310cedf3SRichard Henderson * Don't take exceptions if they target a lower EL. 665310cedf3SRichard Henderson * This check should catch any exceptions that would not be taken 666310cedf3SRichard Henderson * but left pending. 667310cedf3SRichard Henderson */ 668310cedf3SRichard Henderson if (cur_el > target_el) { 669310cedf3SRichard Henderson return false; 670310cedf3SRichard Henderson } 671310cedf3SRichard Henderson 672310cedf3SRichard Henderson switch (excp_idx) { 673310cedf3SRichard Henderson case EXCP_FIQ: 674310cedf3SRichard Henderson pstate_unmasked = !(env->daif & PSTATE_F); 675310cedf3SRichard Henderson break; 676310cedf3SRichard Henderson 677310cedf3SRichard Henderson case EXCP_IRQ: 678310cedf3SRichard Henderson pstate_unmasked = !(env->daif & PSTATE_I); 679310cedf3SRichard Henderson break; 680310cedf3SRichard Henderson 681310cedf3SRichard Henderson case EXCP_VFIQ: 682cc974d5cSRémi Denis-Courmont if (!(hcr_el2 & HCR_FMO) || (hcr_el2 & HCR_TGE)) { 683cc974d5cSRémi Denis-Courmont /* VFIQs are only taken when hypervized. */ 684310cedf3SRichard Henderson return false; 685310cedf3SRichard Henderson } 686310cedf3SRichard Henderson return !(env->daif & PSTATE_F); 687310cedf3SRichard Henderson case EXCP_VIRQ: 688cc974d5cSRémi Denis-Courmont if (!(hcr_el2 & HCR_IMO) || (hcr_el2 & HCR_TGE)) { 689cc974d5cSRémi Denis-Courmont /* VIRQs are only taken when hypervized. */ 690310cedf3SRichard Henderson return false; 691310cedf3SRichard Henderson } 692310cedf3SRichard Henderson return !(env->daif & PSTATE_I); 6933c29632fSRichard Henderson case EXCP_VSERR: 6943c29632fSRichard Henderson if (!(hcr_el2 & HCR_AMO) || (hcr_el2 & HCR_TGE)) { 6953c29632fSRichard Henderson /* VIRQs are only taken when hypervized. */ 6963c29632fSRichard Henderson return false; 6973c29632fSRichard Henderson } 6983c29632fSRichard Henderson return !(env->daif & PSTATE_A); 699310cedf3SRichard Henderson default: 700310cedf3SRichard Henderson g_assert_not_reached(); 701310cedf3SRichard Henderson } 702310cedf3SRichard Henderson 703310cedf3SRichard Henderson /* 704310cedf3SRichard Henderson * Use the target EL, current execution state and SCR/HCR settings to 705310cedf3SRichard Henderson * determine whether the corresponding CPSR bit is used to mask the 706310cedf3SRichard Henderson * interrupt. 707310cedf3SRichard Henderson */ 708310cedf3SRichard Henderson if ((target_el > cur_el) && (target_el != 1)) { 709310cedf3SRichard Henderson /* Exceptions targeting a higher EL may not be maskable */ 710310cedf3SRichard Henderson if (arm_feature(env, ARM_FEATURE_AARCH64)) { 711c939a7c7SAke Koomsin switch (target_el) { 712c939a7c7SAke Koomsin case 2: 713310cedf3SRichard Henderson /* 714c939a7c7SAke Koomsin * According to ARM DDI 0487H.a, an interrupt can be masked 715c939a7c7SAke Koomsin * when HCR_E2H and HCR_TGE are both set regardless of the 716c939a7c7SAke Koomsin * current Security state. Note that we need to revisit this 717c939a7c7SAke Koomsin * part again once we need to support NMI. 718310cedf3SRichard Henderson */ 719c939a7c7SAke Koomsin if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) { 72016e07f78SRichard Henderson unmasked = true; 721310cedf3SRichard Henderson } 722c939a7c7SAke Koomsin break; 723c939a7c7SAke Koomsin case 3: 724c939a7c7SAke Koomsin /* Interrupt cannot be masked when the target EL is 3 */ 725c939a7c7SAke Koomsin unmasked = true; 726c939a7c7SAke Koomsin break; 727c939a7c7SAke Koomsin default: 728c939a7c7SAke Koomsin g_assert_not_reached(); 729c939a7c7SAke Koomsin } 730310cedf3SRichard Henderson } else { 731310cedf3SRichard Henderson /* 732310cedf3SRichard Henderson * The old 32-bit-only environment has a more complicated 733310cedf3SRichard Henderson * masking setup. HCR and SCR bits not only affect interrupt 734310cedf3SRichard Henderson * routing but also change the behaviour of masking. 735310cedf3SRichard Henderson */ 736310cedf3SRichard Henderson bool hcr, scr; 737310cedf3SRichard Henderson 738310cedf3SRichard Henderson switch (excp_idx) { 739310cedf3SRichard Henderson case EXCP_FIQ: 740310cedf3SRichard Henderson /* 741310cedf3SRichard Henderson * If FIQs are routed to EL3 or EL2 then there are cases where 742310cedf3SRichard Henderson * we override the CPSR.F in determining if the exception is 743310cedf3SRichard Henderson * masked or not. If neither of these are set then we fall back 744310cedf3SRichard Henderson * to the CPSR.F setting otherwise we further assess the state 745310cedf3SRichard Henderson * below. 746310cedf3SRichard Henderson */ 747310cedf3SRichard Henderson hcr = hcr_el2 & HCR_FMO; 748310cedf3SRichard Henderson scr = (env->cp15.scr_el3 & SCR_FIQ); 749310cedf3SRichard Henderson 750310cedf3SRichard Henderson /* 751310cedf3SRichard Henderson * When EL3 is 32-bit, the SCR.FW bit controls whether the 752310cedf3SRichard Henderson * CPSR.F bit masks FIQ interrupts when taken in non-secure 753310cedf3SRichard Henderson * state. If SCR.FW is set then FIQs can be masked by CPSR.F 754310cedf3SRichard Henderson * when non-secure but only when FIQs are only routed to EL3. 755310cedf3SRichard Henderson */ 756310cedf3SRichard Henderson scr = scr && !((env->cp15.scr_el3 & SCR_FW) && !hcr); 757310cedf3SRichard Henderson break; 758310cedf3SRichard Henderson case EXCP_IRQ: 759310cedf3SRichard Henderson /* 760310cedf3SRichard Henderson * When EL3 execution state is 32-bit, if HCR.IMO is set then 761310cedf3SRichard Henderson * we may override the CPSR.I masking when in non-secure state. 762310cedf3SRichard Henderson * The SCR.IRQ setting has already been taken into consideration 763310cedf3SRichard Henderson * when setting the target EL, so it does not have a further 764310cedf3SRichard Henderson * affect here. 765310cedf3SRichard Henderson */ 766310cedf3SRichard Henderson hcr = hcr_el2 & HCR_IMO; 767310cedf3SRichard Henderson scr = false; 768310cedf3SRichard Henderson break; 769310cedf3SRichard Henderson default: 770310cedf3SRichard Henderson g_assert_not_reached(); 771310cedf3SRichard Henderson } 772310cedf3SRichard Henderson 773310cedf3SRichard Henderson if ((scr || hcr) && !secure) { 77416e07f78SRichard Henderson unmasked = true; 775310cedf3SRichard Henderson } 776310cedf3SRichard Henderson } 777310cedf3SRichard Henderson } 778310cedf3SRichard Henderson 779310cedf3SRichard Henderson /* 780673d8215SMichael Tokarev * The PSTATE bits only mask the interrupt if we have not overridden the 781310cedf3SRichard Henderson * ability above. 782310cedf3SRichard Henderson */ 783310cedf3SRichard Henderson return unmasked || pstate_unmasked; 784310cedf3SRichard Henderson } 785310cedf3SRichard Henderson 786083afd18SPhilippe Mathieu-Daudé static bool arm_cpu_exec_interrupt(CPUState *cs, int interrupt_request) 787fcf5ef2aSThomas Huth { 788fcf5ef2aSThomas Huth CPUClass *cc = CPU_GET_CLASS(cs); 789b77af26eSRichard Henderson CPUARMState *env = cpu_env(cs); 790fcf5ef2aSThomas Huth uint32_t cur_el = arm_current_el(env); 791fcf5ef2aSThomas Huth bool secure = arm_is_secure(env); 792be879556SRichard Henderson uint64_t hcr_el2 = arm_hcr_el2_eff(env); 793fcf5ef2aSThomas Huth uint32_t target_el; 794fcf5ef2aSThomas Huth uint32_t excp_idx; 795d63d0ec5SRichard Henderson 796d63d0ec5SRichard Henderson /* The prioritization of interrupts is IMPLEMENTATION DEFINED. */ 797fcf5ef2aSThomas Huth 798fcf5ef2aSThomas Huth if (interrupt_request & CPU_INTERRUPT_FIQ) { 799fcf5ef2aSThomas Huth excp_idx = EXCP_FIQ; 800fcf5ef2aSThomas Huth target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure); 801be879556SRichard Henderson if (arm_excp_unmasked(cs, excp_idx, target_el, 802be879556SRichard Henderson cur_el, secure, hcr_el2)) { 803d63d0ec5SRichard Henderson goto found; 804fcf5ef2aSThomas Huth } 805fcf5ef2aSThomas Huth } 806fcf5ef2aSThomas Huth if (interrupt_request & CPU_INTERRUPT_HARD) { 807fcf5ef2aSThomas Huth excp_idx = EXCP_IRQ; 808fcf5ef2aSThomas Huth target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure); 809be879556SRichard Henderson if (arm_excp_unmasked(cs, excp_idx, target_el, 810be879556SRichard Henderson cur_el, secure, hcr_el2)) { 811d63d0ec5SRichard Henderson goto found; 812fcf5ef2aSThomas Huth } 813fcf5ef2aSThomas Huth } 814fcf5ef2aSThomas Huth if (interrupt_request & CPU_INTERRUPT_VIRQ) { 815fcf5ef2aSThomas Huth excp_idx = EXCP_VIRQ; 816fcf5ef2aSThomas Huth target_el = 1; 817be879556SRichard Henderson if (arm_excp_unmasked(cs, excp_idx, target_el, 818be879556SRichard Henderson cur_el, secure, hcr_el2)) { 819d63d0ec5SRichard Henderson goto found; 820fcf5ef2aSThomas Huth } 821fcf5ef2aSThomas Huth } 822fcf5ef2aSThomas Huth if (interrupt_request & CPU_INTERRUPT_VFIQ) { 823fcf5ef2aSThomas Huth excp_idx = EXCP_VFIQ; 824fcf5ef2aSThomas Huth target_el = 1; 825be879556SRichard Henderson if (arm_excp_unmasked(cs, excp_idx, target_el, 826be879556SRichard Henderson cur_el, secure, hcr_el2)) { 827d63d0ec5SRichard Henderson goto found; 828d63d0ec5SRichard Henderson } 829d63d0ec5SRichard Henderson } 8303c29632fSRichard Henderson if (interrupt_request & CPU_INTERRUPT_VSERR) { 8313c29632fSRichard Henderson excp_idx = EXCP_VSERR; 8323c29632fSRichard Henderson target_el = 1; 8333c29632fSRichard Henderson if (arm_excp_unmasked(cs, excp_idx, target_el, 8343c29632fSRichard Henderson cur_el, secure, hcr_el2)) { 8353c29632fSRichard Henderson /* Taking a virtual abort clears HCR_EL2.VSE */ 8363c29632fSRichard Henderson env->cp15.hcr_el2 &= ~HCR_VSE; 8373c29632fSRichard Henderson cpu_reset_interrupt(cs, CPU_INTERRUPT_VSERR); 8383c29632fSRichard Henderson goto found; 8393c29632fSRichard Henderson } 8403c29632fSRichard Henderson } 841d63d0ec5SRichard Henderson return false; 842d63d0ec5SRichard Henderson 843d63d0ec5SRichard Henderson found: 844fcf5ef2aSThomas Huth cs->exception_index = excp_idx; 845fcf5ef2aSThomas Huth env->exception.target_el = target_el; 84678271684SClaudio Fontana cc->tcg_ops->do_interrupt(cs); 847d63d0ec5SRichard Henderson return true; 848fcf5ef2aSThomas Huth } 8499e406eeaSPhilippe Mathieu-Daudé 8509e406eeaSPhilippe Mathieu-Daudé #endif /* CONFIG_TCG && !CONFIG_USER_ONLY */ 851fcf5ef2aSThomas Huth 85289430fc6SPeter Maydell void arm_cpu_update_virq(ARMCPU *cpu) 85389430fc6SPeter Maydell { 85489430fc6SPeter Maydell /* 85589430fc6SPeter Maydell * Update the interrupt level for VIRQ, which is the logical OR of 85689430fc6SPeter Maydell * the HCR_EL2.VI bit and the input line level from the GIC. 85789430fc6SPeter Maydell */ 85889430fc6SPeter Maydell CPUARMState *env = &cpu->env; 85989430fc6SPeter Maydell CPUState *cs = CPU(cpu); 86089430fc6SPeter Maydell 86189430fc6SPeter Maydell bool new_state = (env->cp15.hcr_el2 & HCR_VI) || 86289430fc6SPeter Maydell (env->irq_line_state & CPU_INTERRUPT_VIRQ); 86389430fc6SPeter Maydell 86489430fc6SPeter Maydell if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VIRQ) != 0)) { 86589430fc6SPeter Maydell if (new_state) { 86689430fc6SPeter Maydell cpu_interrupt(cs, CPU_INTERRUPT_VIRQ); 86789430fc6SPeter Maydell } else { 86889430fc6SPeter Maydell cpu_reset_interrupt(cs, CPU_INTERRUPT_VIRQ); 86989430fc6SPeter Maydell } 87089430fc6SPeter Maydell } 87189430fc6SPeter Maydell } 87289430fc6SPeter Maydell 87389430fc6SPeter Maydell void arm_cpu_update_vfiq(ARMCPU *cpu) 87489430fc6SPeter Maydell { 87589430fc6SPeter Maydell /* 87689430fc6SPeter Maydell * Update the interrupt level for VFIQ, which is the logical OR of 87789430fc6SPeter Maydell * the HCR_EL2.VF bit and the input line level from the GIC. 87889430fc6SPeter Maydell */ 87989430fc6SPeter Maydell CPUARMState *env = &cpu->env; 88089430fc6SPeter Maydell CPUState *cs = CPU(cpu); 88189430fc6SPeter Maydell 88289430fc6SPeter Maydell bool new_state = (env->cp15.hcr_el2 & HCR_VF) || 88389430fc6SPeter Maydell (env->irq_line_state & CPU_INTERRUPT_VFIQ); 88489430fc6SPeter Maydell 88589430fc6SPeter Maydell if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VFIQ) != 0)) { 88689430fc6SPeter Maydell if (new_state) { 88789430fc6SPeter Maydell cpu_interrupt(cs, CPU_INTERRUPT_VFIQ); 88889430fc6SPeter Maydell } else { 88989430fc6SPeter Maydell cpu_reset_interrupt(cs, CPU_INTERRUPT_VFIQ); 89089430fc6SPeter Maydell } 89189430fc6SPeter Maydell } 89289430fc6SPeter Maydell } 89389430fc6SPeter Maydell 8943c29632fSRichard Henderson void arm_cpu_update_vserr(ARMCPU *cpu) 8953c29632fSRichard Henderson { 8963c29632fSRichard Henderson /* 8973c29632fSRichard Henderson * Update the interrupt level for VSERR, which is the HCR_EL2.VSE bit. 8983c29632fSRichard Henderson */ 8993c29632fSRichard Henderson CPUARMState *env = &cpu->env; 9003c29632fSRichard Henderson CPUState *cs = CPU(cpu); 9013c29632fSRichard Henderson 9023c29632fSRichard Henderson bool new_state = env->cp15.hcr_el2 & HCR_VSE; 9033c29632fSRichard Henderson 9043c29632fSRichard Henderson if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VSERR) != 0)) { 9053c29632fSRichard Henderson if (new_state) { 9063c29632fSRichard Henderson cpu_interrupt(cs, CPU_INTERRUPT_VSERR); 9073c29632fSRichard Henderson } else { 9083c29632fSRichard Henderson cpu_reset_interrupt(cs, CPU_INTERRUPT_VSERR); 9093c29632fSRichard Henderson } 9103c29632fSRichard Henderson } 9113c29632fSRichard Henderson } 9123c29632fSRichard Henderson 913fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY 914fcf5ef2aSThomas Huth static void arm_cpu_set_irq(void *opaque, int irq, int level) 915fcf5ef2aSThomas Huth { 916fcf5ef2aSThomas Huth ARMCPU *cpu = opaque; 917fcf5ef2aSThomas Huth CPUARMState *env = &cpu->env; 918fcf5ef2aSThomas Huth CPUState *cs = CPU(cpu); 919fcf5ef2aSThomas Huth static const int mask[] = { 920fcf5ef2aSThomas Huth [ARM_CPU_IRQ] = CPU_INTERRUPT_HARD, 921fcf5ef2aSThomas Huth [ARM_CPU_FIQ] = CPU_INTERRUPT_FIQ, 922fcf5ef2aSThomas Huth [ARM_CPU_VIRQ] = CPU_INTERRUPT_VIRQ, 923fcf5ef2aSThomas Huth [ARM_CPU_VFIQ] = CPU_INTERRUPT_VFIQ 924fcf5ef2aSThomas Huth }; 925fcf5ef2aSThomas Huth 9269acd2d33SPeter Maydell if (!arm_feature(env, ARM_FEATURE_EL2) && 9279acd2d33SPeter Maydell (irq == ARM_CPU_VIRQ || irq == ARM_CPU_VFIQ)) { 9289acd2d33SPeter Maydell /* 9299acd2d33SPeter Maydell * The GIC might tell us about VIRQ and VFIQ state, but if we don't 9309acd2d33SPeter Maydell * have EL2 support we don't care. (Unless the guest is doing something 9319acd2d33SPeter Maydell * silly this will only be calls saying "level is still 0".) 9329acd2d33SPeter Maydell */ 9339acd2d33SPeter Maydell return; 9349acd2d33SPeter Maydell } 9359acd2d33SPeter Maydell 936ed89f078SPeter Maydell if (level) { 937ed89f078SPeter Maydell env->irq_line_state |= mask[irq]; 938ed89f078SPeter Maydell } else { 939ed89f078SPeter Maydell env->irq_line_state &= ~mask[irq]; 940ed89f078SPeter Maydell } 941ed89f078SPeter Maydell 942fcf5ef2aSThomas Huth switch (irq) { 943fcf5ef2aSThomas Huth case ARM_CPU_VIRQ: 94489430fc6SPeter Maydell arm_cpu_update_virq(cpu); 94589430fc6SPeter Maydell break; 946fcf5ef2aSThomas Huth case ARM_CPU_VFIQ: 94789430fc6SPeter Maydell arm_cpu_update_vfiq(cpu); 94889430fc6SPeter Maydell break; 949fcf5ef2aSThomas Huth case ARM_CPU_IRQ: 950fcf5ef2aSThomas Huth case ARM_CPU_FIQ: 951fcf5ef2aSThomas Huth if (level) { 952fcf5ef2aSThomas Huth cpu_interrupt(cs, mask[irq]); 953fcf5ef2aSThomas Huth } else { 954fcf5ef2aSThomas Huth cpu_reset_interrupt(cs, mask[irq]); 955fcf5ef2aSThomas Huth } 956fcf5ef2aSThomas Huth break; 957fcf5ef2aSThomas Huth default: 958fcf5ef2aSThomas Huth g_assert_not_reached(); 959fcf5ef2aSThomas Huth } 960fcf5ef2aSThomas Huth } 961fcf5ef2aSThomas Huth 962fcf5ef2aSThomas Huth static void arm_cpu_kvm_set_irq(void *opaque, int irq, int level) 963fcf5ef2aSThomas Huth { 964fcf5ef2aSThomas Huth #ifdef CONFIG_KVM 965fcf5ef2aSThomas Huth ARMCPU *cpu = opaque; 966ed89f078SPeter Maydell CPUARMState *env = &cpu->env; 967fcf5ef2aSThomas Huth CPUState *cs = CPU(cpu); 968ed89f078SPeter Maydell uint32_t linestate_bit; 969f6530926SEric Auger int irq_id; 970fcf5ef2aSThomas Huth 971fcf5ef2aSThomas Huth switch (irq) { 972fcf5ef2aSThomas Huth case ARM_CPU_IRQ: 973f6530926SEric Auger irq_id = KVM_ARM_IRQ_CPU_IRQ; 974ed89f078SPeter Maydell linestate_bit = CPU_INTERRUPT_HARD; 975fcf5ef2aSThomas Huth break; 976fcf5ef2aSThomas Huth case ARM_CPU_FIQ: 977f6530926SEric Auger irq_id = KVM_ARM_IRQ_CPU_FIQ; 978ed89f078SPeter Maydell linestate_bit = CPU_INTERRUPT_FIQ; 979fcf5ef2aSThomas Huth break; 980fcf5ef2aSThomas Huth default: 981fcf5ef2aSThomas Huth g_assert_not_reached(); 982fcf5ef2aSThomas Huth } 983ed89f078SPeter Maydell 984ed89f078SPeter Maydell if (level) { 985ed89f078SPeter Maydell env->irq_line_state |= linestate_bit; 986ed89f078SPeter Maydell } else { 987ed89f078SPeter Maydell env->irq_line_state &= ~linestate_bit; 988ed89f078SPeter Maydell } 989f6530926SEric Auger kvm_arm_set_irq(cs->cpu_index, KVM_ARM_IRQ_TYPE_CPU, irq_id, !!level); 990fcf5ef2aSThomas Huth #endif 991fcf5ef2aSThomas Huth } 992fcf5ef2aSThomas Huth 993fcf5ef2aSThomas Huth static bool arm_cpu_virtio_is_big_endian(CPUState *cs) 994fcf5ef2aSThomas Huth { 995fcf5ef2aSThomas Huth ARMCPU *cpu = ARM_CPU(cs); 996fcf5ef2aSThomas Huth CPUARMState *env = &cpu->env; 997fcf5ef2aSThomas Huth 998fcf5ef2aSThomas Huth cpu_synchronize_state(cs); 999fcf5ef2aSThomas Huth return arm_cpu_data_is_big_endian(env); 1000fcf5ef2aSThomas Huth } 1001fcf5ef2aSThomas Huth 1002fcf5ef2aSThomas Huth #endif 1003fcf5ef2aSThomas Huth 1004fcf5ef2aSThomas Huth static void arm_disas_set_info(CPUState *cpu, disassemble_info *info) 1005fcf5ef2aSThomas Huth { 1006fcf5ef2aSThomas Huth ARMCPU *ac = ARM_CPU(cpu); 1007fcf5ef2aSThomas Huth CPUARMState *env = &ac->env; 10087bcdbf51SRichard Henderson bool sctlr_b; 1009fcf5ef2aSThomas Huth 1010fcf5ef2aSThomas Huth if (is_a64(env)) { 1011110f6c70SRichard Henderson info->cap_arch = CS_ARCH_ARM64; 101215fa1a0aSRichard Henderson info->cap_insn_unit = 4; 101315fa1a0aSRichard Henderson info->cap_insn_split = 4; 1014110f6c70SRichard Henderson } else { 1015110f6c70SRichard Henderson int cap_mode; 1016110f6c70SRichard Henderson if (env->thumb) { 101715fa1a0aSRichard Henderson info->cap_insn_unit = 2; 101815fa1a0aSRichard Henderson info->cap_insn_split = 4; 1019110f6c70SRichard Henderson cap_mode = CS_MODE_THUMB; 1020fcf5ef2aSThomas Huth } else { 102115fa1a0aSRichard Henderson info->cap_insn_unit = 4; 102215fa1a0aSRichard Henderson info->cap_insn_split = 4; 1023110f6c70SRichard Henderson cap_mode = CS_MODE_ARM; 1024fcf5ef2aSThomas Huth } 1025110f6c70SRichard Henderson if (arm_feature(env, ARM_FEATURE_V8)) { 1026110f6c70SRichard Henderson cap_mode |= CS_MODE_V8; 1027110f6c70SRichard Henderson } 1028110f6c70SRichard Henderson if (arm_feature(env, ARM_FEATURE_M)) { 1029110f6c70SRichard Henderson cap_mode |= CS_MODE_MCLASS; 1030110f6c70SRichard Henderson } 1031110f6c70SRichard Henderson info->cap_arch = CS_ARCH_ARM; 1032110f6c70SRichard Henderson info->cap_mode = cap_mode; 1033fcf5ef2aSThomas Huth } 10347bcdbf51SRichard Henderson 10357bcdbf51SRichard Henderson sctlr_b = arm_sctlr_b(env); 10367bcdbf51SRichard Henderson if (bswap_code(sctlr_b)) { 1037ee3eb3a7SMarc-André Lureau #if TARGET_BIG_ENDIAN 1038fcf5ef2aSThomas Huth info->endian = BFD_ENDIAN_LITTLE; 1039fcf5ef2aSThomas Huth #else 1040fcf5ef2aSThomas Huth info->endian = BFD_ENDIAN_BIG; 1041fcf5ef2aSThomas Huth #endif 1042fcf5ef2aSThomas Huth } 1043f7478a92SJulian Brown info->flags &= ~INSN_ARM_BE32; 10447bcdbf51SRichard Henderson #ifndef CONFIG_USER_ONLY 10457bcdbf51SRichard Henderson if (sctlr_b) { 1046f7478a92SJulian Brown info->flags |= INSN_ARM_BE32; 1047f7478a92SJulian Brown } 10487bcdbf51SRichard Henderson #endif 1049fcf5ef2aSThomas Huth } 1050fcf5ef2aSThomas Huth 105186480615SPhilippe Mathieu-Daudé #ifdef TARGET_AARCH64 105286480615SPhilippe Mathieu-Daudé 105386480615SPhilippe Mathieu-Daudé static void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags) 105486480615SPhilippe Mathieu-Daudé { 105586480615SPhilippe Mathieu-Daudé ARMCPU *cpu = ARM_CPU(cs); 105686480615SPhilippe Mathieu-Daudé CPUARMState *env = &cpu->env; 105786480615SPhilippe Mathieu-Daudé uint32_t psr = pstate_read(env); 1058a9d84070SRichard Henderson int i, j; 105986480615SPhilippe Mathieu-Daudé int el = arm_current_el(env); 106086480615SPhilippe Mathieu-Daudé const char *ns_status; 10617a867dd5SRichard Henderson bool sve; 106286480615SPhilippe Mathieu-Daudé 106386480615SPhilippe Mathieu-Daudé qemu_fprintf(f, " PC=%016" PRIx64 " ", env->pc); 106486480615SPhilippe Mathieu-Daudé for (i = 0; i < 32; i++) { 106586480615SPhilippe Mathieu-Daudé if (i == 31) { 106686480615SPhilippe Mathieu-Daudé qemu_fprintf(f, " SP=%016" PRIx64 "\n", env->xregs[i]); 106786480615SPhilippe Mathieu-Daudé } else { 106886480615SPhilippe Mathieu-Daudé qemu_fprintf(f, "X%02d=%016" PRIx64 "%s", i, env->xregs[i], 106986480615SPhilippe Mathieu-Daudé (i + 2) % 3 ? " " : "\n"); 107086480615SPhilippe Mathieu-Daudé } 107186480615SPhilippe Mathieu-Daudé } 107286480615SPhilippe Mathieu-Daudé 107386480615SPhilippe Mathieu-Daudé if (arm_feature(env, ARM_FEATURE_EL3) && el != 3) { 107486480615SPhilippe Mathieu-Daudé ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S "; 107586480615SPhilippe Mathieu-Daudé } else { 107686480615SPhilippe Mathieu-Daudé ns_status = ""; 107786480615SPhilippe Mathieu-Daudé } 107886480615SPhilippe Mathieu-Daudé qemu_fprintf(f, "PSTATE=%08x %c%c%c%c %sEL%d%c", 107986480615SPhilippe Mathieu-Daudé psr, 108086480615SPhilippe Mathieu-Daudé psr & PSTATE_N ? 'N' : '-', 108186480615SPhilippe Mathieu-Daudé psr & PSTATE_Z ? 'Z' : '-', 108286480615SPhilippe Mathieu-Daudé psr & PSTATE_C ? 'C' : '-', 108386480615SPhilippe Mathieu-Daudé psr & PSTATE_V ? 'V' : '-', 108486480615SPhilippe Mathieu-Daudé ns_status, 108586480615SPhilippe Mathieu-Daudé el, 108686480615SPhilippe Mathieu-Daudé psr & PSTATE_SP ? 'h' : 't'); 108786480615SPhilippe Mathieu-Daudé 10887a867dd5SRichard Henderson if (cpu_isar_feature(aa64_sme, cpu)) { 10897a867dd5SRichard Henderson qemu_fprintf(f, " SVCR=%08" PRIx64 " %c%c", 10907a867dd5SRichard Henderson env->svcr, 10917a867dd5SRichard Henderson (FIELD_EX64(env->svcr, SVCR, ZA) ? 'Z' : '-'), 10927a867dd5SRichard Henderson (FIELD_EX64(env->svcr, SVCR, SM) ? 'S' : '-')); 10937a867dd5SRichard Henderson } 109486480615SPhilippe Mathieu-Daudé if (cpu_isar_feature(aa64_bti, cpu)) { 109586480615SPhilippe Mathieu-Daudé qemu_fprintf(f, " BTYPE=%d", (psr & PSTATE_BTYPE) >> 10); 109686480615SPhilippe Mathieu-Daudé } 109786480615SPhilippe Mathieu-Daudé if (!(flags & CPU_DUMP_FPU)) { 109886480615SPhilippe Mathieu-Daudé qemu_fprintf(f, "\n"); 109986480615SPhilippe Mathieu-Daudé return; 110086480615SPhilippe Mathieu-Daudé } 110186480615SPhilippe Mathieu-Daudé if (fp_exception_el(env, el) != 0) { 110286480615SPhilippe Mathieu-Daudé qemu_fprintf(f, " FPU disabled\n"); 110386480615SPhilippe Mathieu-Daudé return; 110486480615SPhilippe Mathieu-Daudé } 110586480615SPhilippe Mathieu-Daudé qemu_fprintf(f, " FPCR=%08x FPSR=%08x\n", 110686480615SPhilippe Mathieu-Daudé vfp_get_fpcr(env), vfp_get_fpsr(env)); 110786480615SPhilippe Mathieu-Daudé 11087a867dd5SRichard Henderson if (cpu_isar_feature(aa64_sme, cpu) && FIELD_EX64(env->svcr, SVCR, SM)) { 11097a867dd5SRichard Henderson sve = sme_exception_el(env, el) == 0; 11107a867dd5SRichard Henderson } else if (cpu_isar_feature(aa64_sve, cpu)) { 11117a867dd5SRichard Henderson sve = sve_exception_el(env, el) == 0; 11127a867dd5SRichard Henderson } else { 11137a867dd5SRichard Henderson sve = false; 11147a867dd5SRichard Henderson } 11157a867dd5SRichard Henderson 11167a867dd5SRichard Henderson if (sve) { 1117a9d84070SRichard Henderson int zcr_len = sve_vqm1_for_el(env, el); 111886480615SPhilippe Mathieu-Daudé 111986480615SPhilippe Mathieu-Daudé for (i = 0; i <= FFR_PRED_NUM; i++) { 112086480615SPhilippe Mathieu-Daudé bool eol; 112186480615SPhilippe Mathieu-Daudé if (i == FFR_PRED_NUM) { 112286480615SPhilippe Mathieu-Daudé qemu_fprintf(f, "FFR="); 112386480615SPhilippe Mathieu-Daudé /* It's last, so end the line. */ 112486480615SPhilippe Mathieu-Daudé eol = true; 112586480615SPhilippe Mathieu-Daudé } else { 112686480615SPhilippe Mathieu-Daudé qemu_fprintf(f, "P%02d=", i); 112786480615SPhilippe Mathieu-Daudé switch (zcr_len) { 112886480615SPhilippe Mathieu-Daudé case 0: 112986480615SPhilippe Mathieu-Daudé eol = i % 8 == 7; 113086480615SPhilippe Mathieu-Daudé break; 113186480615SPhilippe Mathieu-Daudé case 1: 113286480615SPhilippe Mathieu-Daudé eol = i % 6 == 5; 113386480615SPhilippe Mathieu-Daudé break; 113486480615SPhilippe Mathieu-Daudé case 2: 113586480615SPhilippe Mathieu-Daudé case 3: 113686480615SPhilippe Mathieu-Daudé eol = i % 3 == 2; 113786480615SPhilippe Mathieu-Daudé break; 113886480615SPhilippe Mathieu-Daudé default: 113986480615SPhilippe Mathieu-Daudé /* More than one quadword per predicate. */ 114086480615SPhilippe Mathieu-Daudé eol = true; 114186480615SPhilippe Mathieu-Daudé break; 114286480615SPhilippe Mathieu-Daudé } 114386480615SPhilippe Mathieu-Daudé } 114486480615SPhilippe Mathieu-Daudé for (j = zcr_len / 4; j >= 0; j--) { 114586480615SPhilippe Mathieu-Daudé int digits; 114686480615SPhilippe Mathieu-Daudé if (j * 4 + 4 <= zcr_len + 1) { 114786480615SPhilippe Mathieu-Daudé digits = 16; 114886480615SPhilippe Mathieu-Daudé } else { 114986480615SPhilippe Mathieu-Daudé digits = (zcr_len % 4 + 1) * 4; 115086480615SPhilippe Mathieu-Daudé } 115186480615SPhilippe Mathieu-Daudé qemu_fprintf(f, "%0*" PRIx64 "%s", digits, 115286480615SPhilippe Mathieu-Daudé env->vfp.pregs[i].p[j], 115386480615SPhilippe Mathieu-Daudé j ? ":" : eol ? "\n" : " "); 115486480615SPhilippe Mathieu-Daudé } 115586480615SPhilippe Mathieu-Daudé } 115686480615SPhilippe Mathieu-Daudé 115786480615SPhilippe Mathieu-Daudé if (zcr_len == 0) { 1158a9d84070SRichard Henderson /* 1159a9d84070SRichard Henderson * With vl=16, there are only 37 columns per register, 1160a9d84070SRichard Henderson * so output two registers per line. 1161a9d84070SRichard Henderson */ 1162a9d84070SRichard Henderson for (i = 0; i < 32; i++) { 116386480615SPhilippe Mathieu-Daudé qemu_fprintf(f, "Z%02d=%016" PRIx64 ":%016" PRIx64 "%s", 116486480615SPhilippe Mathieu-Daudé i, env->vfp.zregs[i].d[1], 116586480615SPhilippe Mathieu-Daudé env->vfp.zregs[i].d[0], i & 1 ? "\n" : " "); 1166a9d84070SRichard Henderson } 116786480615SPhilippe Mathieu-Daudé } else { 1168a9d84070SRichard Henderson for (i = 0; i < 32; i++) { 1169a9d84070SRichard Henderson qemu_fprintf(f, "Z%02d=", i); 117086480615SPhilippe Mathieu-Daudé for (j = zcr_len; j >= 0; j--) { 117186480615SPhilippe Mathieu-Daudé qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%s", 117286480615SPhilippe Mathieu-Daudé env->vfp.zregs[i].d[j * 2 + 1], 1173a9d84070SRichard Henderson env->vfp.zregs[i].d[j * 2 + 0], 1174a9d84070SRichard Henderson j ? ":" : "\n"); 117586480615SPhilippe Mathieu-Daudé } 117686480615SPhilippe Mathieu-Daudé } 117786480615SPhilippe Mathieu-Daudé } 117886480615SPhilippe Mathieu-Daudé } else { 117986480615SPhilippe Mathieu-Daudé for (i = 0; i < 32; i++) { 118086480615SPhilippe Mathieu-Daudé uint64_t *q = aa64_vfp_qreg(env, i); 118186480615SPhilippe Mathieu-Daudé qemu_fprintf(f, "Q%02d=%016" PRIx64 ":%016" PRIx64 "%s", 118286480615SPhilippe Mathieu-Daudé i, q[1], q[0], (i & 1 ? "\n" : " ")); 118386480615SPhilippe Mathieu-Daudé } 118486480615SPhilippe Mathieu-Daudé } 1185270bea47SRichard Henderson 1186270bea47SRichard Henderson if (cpu_isar_feature(aa64_sme, cpu) && 1187270bea47SRichard Henderson FIELD_EX64(env->svcr, SVCR, ZA) && 1188270bea47SRichard Henderson sme_exception_el(env, el) == 0) { 1189270bea47SRichard Henderson int zcr_len = sve_vqm1_for_el_sm(env, el, true); 1190270bea47SRichard Henderson int svl = (zcr_len + 1) * 16; 1191270bea47SRichard Henderson int svl_lg10 = svl < 100 ? 2 : 3; 1192270bea47SRichard Henderson 1193270bea47SRichard Henderson for (i = 0; i < svl; i++) { 1194270bea47SRichard Henderson qemu_fprintf(f, "ZA[%0*d]=", svl_lg10, i); 1195270bea47SRichard Henderson for (j = zcr_len; j >= 0; --j) { 1196270bea47SRichard Henderson qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%c", 1197270bea47SRichard Henderson env->zarray[i].d[2 * j + 1], 1198270bea47SRichard Henderson env->zarray[i].d[2 * j], 1199270bea47SRichard Henderson j ? ':' : '\n'); 1200270bea47SRichard Henderson } 1201270bea47SRichard Henderson } 1202270bea47SRichard Henderson } 120386480615SPhilippe Mathieu-Daudé } 120486480615SPhilippe Mathieu-Daudé 120586480615SPhilippe Mathieu-Daudé #else 120686480615SPhilippe Mathieu-Daudé 120786480615SPhilippe Mathieu-Daudé static inline void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags) 120886480615SPhilippe Mathieu-Daudé { 120986480615SPhilippe Mathieu-Daudé g_assert_not_reached(); 121086480615SPhilippe Mathieu-Daudé } 121186480615SPhilippe Mathieu-Daudé 121286480615SPhilippe Mathieu-Daudé #endif 121386480615SPhilippe Mathieu-Daudé 121486480615SPhilippe Mathieu-Daudé static void arm_cpu_dump_state(CPUState *cs, FILE *f, int flags) 121586480615SPhilippe Mathieu-Daudé { 121686480615SPhilippe Mathieu-Daudé ARMCPU *cpu = ARM_CPU(cs); 121786480615SPhilippe Mathieu-Daudé CPUARMState *env = &cpu->env; 121886480615SPhilippe Mathieu-Daudé int i; 121986480615SPhilippe Mathieu-Daudé 122086480615SPhilippe Mathieu-Daudé if (is_a64(env)) { 122186480615SPhilippe Mathieu-Daudé aarch64_cpu_dump_state(cs, f, flags); 122286480615SPhilippe Mathieu-Daudé return; 122386480615SPhilippe Mathieu-Daudé } 122486480615SPhilippe Mathieu-Daudé 122586480615SPhilippe Mathieu-Daudé for (i = 0; i < 16; i++) { 122686480615SPhilippe Mathieu-Daudé qemu_fprintf(f, "R%02d=%08x", i, env->regs[i]); 122786480615SPhilippe Mathieu-Daudé if ((i % 4) == 3) { 122886480615SPhilippe Mathieu-Daudé qemu_fprintf(f, "\n"); 122986480615SPhilippe Mathieu-Daudé } else { 123086480615SPhilippe Mathieu-Daudé qemu_fprintf(f, " "); 123186480615SPhilippe Mathieu-Daudé } 123286480615SPhilippe Mathieu-Daudé } 123386480615SPhilippe Mathieu-Daudé 123486480615SPhilippe Mathieu-Daudé if (arm_feature(env, ARM_FEATURE_M)) { 123586480615SPhilippe Mathieu-Daudé uint32_t xpsr = xpsr_read(env); 123686480615SPhilippe Mathieu-Daudé const char *mode; 123786480615SPhilippe Mathieu-Daudé const char *ns_status = ""; 123886480615SPhilippe Mathieu-Daudé 123986480615SPhilippe Mathieu-Daudé if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 124086480615SPhilippe Mathieu-Daudé ns_status = env->v7m.secure ? "S " : "NS "; 124186480615SPhilippe Mathieu-Daudé } 124286480615SPhilippe Mathieu-Daudé 124386480615SPhilippe Mathieu-Daudé if (xpsr & XPSR_EXCP) { 124486480615SPhilippe Mathieu-Daudé mode = "handler"; 124586480615SPhilippe Mathieu-Daudé } else { 124686480615SPhilippe Mathieu-Daudé if (env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_NPRIV_MASK) { 124786480615SPhilippe Mathieu-Daudé mode = "unpriv-thread"; 124886480615SPhilippe Mathieu-Daudé } else { 124986480615SPhilippe Mathieu-Daudé mode = "priv-thread"; 125086480615SPhilippe Mathieu-Daudé } 125186480615SPhilippe Mathieu-Daudé } 125286480615SPhilippe Mathieu-Daudé 125386480615SPhilippe Mathieu-Daudé qemu_fprintf(f, "XPSR=%08x %c%c%c%c %c %s%s\n", 125486480615SPhilippe Mathieu-Daudé xpsr, 125586480615SPhilippe Mathieu-Daudé xpsr & XPSR_N ? 'N' : '-', 125686480615SPhilippe Mathieu-Daudé xpsr & XPSR_Z ? 'Z' : '-', 125786480615SPhilippe Mathieu-Daudé xpsr & XPSR_C ? 'C' : '-', 125886480615SPhilippe Mathieu-Daudé xpsr & XPSR_V ? 'V' : '-', 125986480615SPhilippe Mathieu-Daudé xpsr & XPSR_T ? 'T' : 'A', 126086480615SPhilippe Mathieu-Daudé ns_status, 126186480615SPhilippe Mathieu-Daudé mode); 126286480615SPhilippe Mathieu-Daudé } else { 126386480615SPhilippe Mathieu-Daudé uint32_t psr = cpsr_read(env); 126486480615SPhilippe Mathieu-Daudé const char *ns_status = ""; 126586480615SPhilippe Mathieu-Daudé 126686480615SPhilippe Mathieu-Daudé if (arm_feature(env, ARM_FEATURE_EL3) && 126786480615SPhilippe Mathieu-Daudé (psr & CPSR_M) != ARM_CPU_MODE_MON) { 126886480615SPhilippe Mathieu-Daudé ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S "; 126986480615SPhilippe Mathieu-Daudé } 127086480615SPhilippe Mathieu-Daudé 127186480615SPhilippe Mathieu-Daudé qemu_fprintf(f, "PSR=%08x %c%c%c%c %c %s%s%d\n", 127286480615SPhilippe Mathieu-Daudé psr, 127386480615SPhilippe Mathieu-Daudé psr & CPSR_N ? 'N' : '-', 127486480615SPhilippe Mathieu-Daudé psr & CPSR_Z ? 'Z' : '-', 127586480615SPhilippe Mathieu-Daudé psr & CPSR_C ? 'C' : '-', 127686480615SPhilippe Mathieu-Daudé psr & CPSR_V ? 'V' : '-', 127786480615SPhilippe Mathieu-Daudé psr & CPSR_T ? 'T' : 'A', 127886480615SPhilippe Mathieu-Daudé ns_status, 127986480615SPhilippe Mathieu-Daudé aarch32_mode_name(psr), (psr & 0x10) ? 32 : 26); 128086480615SPhilippe Mathieu-Daudé } 128186480615SPhilippe Mathieu-Daudé 128286480615SPhilippe Mathieu-Daudé if (flags & CPU_DUMP_FPU) { 128386480615SPhilippe Mathieu-Daudé int numvfpregs = 0; 1284a6627f5fSRichard Henderson if (cpu_isar_feature(aa32_simd_r32, cpu)) { 1285a6627f5fSRichard Henderson numvfpregs = 32; 12867fbc6a40SRichard Henderson } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) { 1287a6627f5fSRichard Henderson numvfpregs = 16; 128886480615SPhilippe Mathieu-Daudé } 128986480615SPhilippe Mathieu-Daudé for (i = 0; i < numvfpregs; i++) { 129086480615SPhilippe Mathieu-Daudé uint64_t v = *aa32_vfp_dreg(env, i); 129186480615SPhilippe Mathieu-Daudé qemu_fprintf(f, "s%02d=%08x s%02d=%08x d%02d=%016" PRIx64 "\n", 129286480615SPhilippe Mathieu-Daudé i * 2, (uint32_t)v, 129386480615SPhilippe Mathieu-Daudé i * 2 + 1, (uint32_t)(v >> 32), 129486480615SPhilippe Mathieu-Daudé i, v); 129586480615SPhilippe Mathieu-Daudé } 129686480615SPhilippe Mathieu-Daudé qemu_fprintf(f, "FPSCR: %08x\n", vfp_get_fpscr(env)); 1297aa291908SPeter Maydell if (cpu_isar_feature(aa32_mve, cpu)) { 1298aa291908SPeter Maydell qemu_fprintf(f, "VPR: %08x\n", env->v7m.vpr); 1299aa291908SPeter Maydell } 130086480615SPhilippe Mathieu-Daudé } 130186480615SPhilippe Mathieu-Daudé } 130286480615SPhilippe Mathieu-Daudé 130346de5913SIgor Mammedov uint64_t arm_cpu_mp_affinity(int idx, uint8_t clustersz) 130446de5913SIgor Mammedov { 130546de5913SIgor Mammedov uint32_t Aff1 = idx / clustersz; 130646de5913SIgor Mammedov uint32_t Aff0 = idx % clustersz; 130746de5913SIgor Mammedov return (Aff1 << ARM_AFF1_SHIFT) | Aff0; 130846de5913SIgor Mammedov } 130946de5913SIgor Mammedov 1310fcf5ef2aSThomas Huth static void arm_cpu_initfn(Object *obj) 1311fcf5ef2aSThomas Huth { 1312fcf5ef2aSThomas Huth ARMCPU *cpu = ARM_CPU(obj); 1313fcf5ef2aSThomas Huth 13145860362dSRichard Henderson cpu->cp_regs = g_hash_table_new_full(g_direct_hash, g_direct_equal, 1315c27f5d3aSRichard Henderson NULL, g_free); 1316fcf5ef2aSThomas Huth 1317b5c53d1bSAaron Lindsay QLIST_INIT(&cpu->pre_el_change_hooks); 131808267487SAaron Lindsay QLIST_INIT(&cpu->el_change_hooks); 131908267487SAaron Lindsay 1320b3d52804SRichard Henderson #ifdef CONFIG_USER_ONLY 1321b3d52804SRichard Henderson # ifdef TARGET_AARCH64 1322b3d52804SRichard Henderson /* 1323e74c0976SRichard Henderson * The linux kernel defaults to 512-bit for SVE, and 256-bit for SME. 1324e74c0976SRichard Henderson * These values were chosen to fit within the default signal frame. 1325e74c0976SRichard Henderson * See documentation for /proc/sys/abi/{sve,sme}_default_vector_length, 1326e74c0976SRichard Henderson * and our corresponding cpu property. 1327b3d52804SRichard Henderson */ 1328b3d52804SRichard Henderson cpu->sve_default_vq = 4; 1329e74c0976SRichard Henderson cpu->sme_default_vq = 2; 1330b3d52804SRichard Henderson # endif 1331b3d52804SRichard Henderson #else 1332fcf5ef2aSThomas Huth /* Our inbound IRQ and FIQ lines */ 1333fcf5ef2aSThomas Huth if (kvm_enabled()) { 1334fcf5ef2aSThomas Huth /* VIRQ and VFIQ are unused with KVM but we add them to maintain 1335fcf5ef2aSThomas Huth * the same interface as non-KVM CPUs. 1336fcf5ef2aSThomas Huth */ 1337fcf5ef2aSThomas Huth qdev_init_gpio_in(DEVICE(cpu), arm_cpu_kvm_set_irq, 4); 1338fcf5ef2aSThomas Huth } else { 1339fcf5ef2aSThomas Huth qdev_init_gpio_in(DEVICE(cpu), arm_cpu_set_irq, 4); 1340fcf5ef2aSThomas Huth } 1341fcf5ef2aSThomas Huth 1342fcf5ef2aSThomas Huth qdev_init_gpio_out(DEVICE(cpu), cpu->gt_timer_outputs, 1343fcf5ef2aSThomas Huth ARRAY_SIZE(cpu->gt_timer_outputs)); 1344aa1b3111SPeter Maydell 1345aa1b3111SPeter Maydell qdev_init_gpio_out_named(DEVICE(cpu), &cpu->gicv3_maintenance_interrupt, 1346aa1b3111SPeter Maydell "gicv3-maintenance-interrupt", 1); 134707f48730SAndrew Jones qdev_init_gpio_out_named(DEVICE(cpu), &cpu->pmu_interrupt, 134807f48730SAndrew Jones "pmu-interrupt", 1); 1349fcf5ef2aSThomas Huth #endif 1350fcf5ef2aSThomas Huth 1351fcf5ef2aSThomas Huth /* DTB consumers generally don't in fact care what the 'compatible' 1352fcf5ef2aSThomas Huth * string is, so always provide some string and trust that a hypothetical 1353fcf5ef2aSThomas Huth * picky DTB consumer will also provide a helpful error message. 1354fcf5ef2aSThomas Huth */ 1355fcf5ef2aSThomas Huth cpu->dtb_compatible = "qemu,unknown"; 13560dc71c70SAkihiko Odaki cpu->psci_version = QEMU_PSCI_VERSION_0_1; /* By default assume PSCI v0.1 */ 1357fcf5ef2aSThomas Huth cpu->kvm_target = QEMU_KVM_ARM_TARGET_NONE; 1358fcf5ef2aSThomas Huth 13592c9c0bf9SAlexander Graf if (tcg_enabled() || hvf_enabled()) { 13600dc71c70SAkihiko Odaki /* TCG and HVF implement PSCI 1.1 */ 13610dc71c70SAkihiko Odaki cpu->psci_version = QEMU_PSCI_VERSION_1_1; 1362fcf5ef2aSThomas Huth } 1363fcf5ef2aSThomas Huth } 1364fcf5ef2aSThomas Huth 136596eec6b2SAndrew Jeffery static Property arm_cpu_gt_cntfrq_property = 136696eec6b2SAndrew Jeffery DEFINE_PROP_UINT64("cntfrq", ARMCPU, gt_cntfrq_hz, 136796eec6b2SAndrew Jeffery NANOSECONDS_PER_SECOND / GTIMER_SCALE); 136896eec6b2SAndrew Jeffery 1369fcf5ef2aSThomas Huth static Property arm_cpu_reset_cbar_property = 1370fcf5ef2aSThomas Huth DEFINE_PROP_UINT64("reset-cbar", ARMCPU, reset_cbar, 0); 1371fcf5ef2aSThomas Huth 1372fcf5ef2aSThomas Huth static Property arm_cpu_reset_hivecs_property = 1373fcf5ef2aSThomas Huth DEFINE_PROP_BOOL("reset-hivecs", ARMCPU, reset_hivecs, false); 1374fcf5ef2aSThomas Huth 137545ca3a14SRichard Henderson #ifndef CONFIG_USER_ONLY 1376c25bd18aSPeter Maydell static Property arm_cpu_has_el2_property = 1377c25bd18aSPeter Maydell DEFINE_PROP_BOOL("has_el2", ARMCPU, has_el2, true); 1378c25bd18aSPeter Maydell 1379fcf5ef2aSThomas Huth static Property arm_cpu_has_el3_property = 1380fcf5ef2aSThomas Huth DEFINE_PROP_BOOL("has_el3", ARMCPU, has_el3, true); 138145ca3a14SRichard Henderson #endif 1382fcf5ef2aSThomas Huth 13833a062d57SJulian Brown static Property arm_cpu_cfgend_property = 13843a062d57SJulian Brown DEFINE_PROP_BOOL("cfgend", ARMCPU, cfgend, false); 13853a062d57SJulian Brown 138697a28b0eSPeter Maydell static Property arm_cpu_has_vfp_property = 138797a28b0eSPeter Maydell DEFINE_PROP_BOOL("vfp", ARMCPU, has_vfp, true); 138897a28b0eSPeter Maydell 138942bea956SCédric Le Goater static Property arm_cpu_has_vfp_d32_property = 139042bea956SCédric Le Goater DEFINE_PROP_BOOL("vfp-d32", ARMCPU, has_vfp_d32, true); 139142bea956SCédric Le Goater 139297a28b0eSPeter Maydell static Property arm_cpu_has_neon_property = 139397a28b0eSPeter Maydell DEFINE_PROP_BOOL("neon", ARMCPU, has_neon, true); 139497a28b0eSPeter Maydell 1395ea90db0aSPeter Maydell static Property arm_cpu_has_dsp_property = 1396ea90db0aSPeter Maydell DEFINE_PROP_BOOL("dsp", ARMCPU, has_dsp, true); 1397ea90db0aSPeter Maydell 1398fcf5ef2aSThomas Huth static Property arm_cpu_has_mpu_property = 1399fcf5ef2aSThomas Huth DEFINE_PROP_BOOL("has-mpu", ARMCPU, has_mpu, true); 1400fcf5ef2aSThomas Huth 14018d92e26bSPeter Maydell /* This is like DEFINE_PROP_UINT32 but it doesn't set the default value, 14028d92e26bSPeter Maydell * because the CPU initfn will have already set cpu->pmsav7_dregion to 14038d92e26bSPeter Maydell * the right value for that particular CPU type, and we don't want 14048d92e26bSPeter Maydell * to override that with an incorrect constant value. 14058d92e26bSPeter Maydell */ 1406fcf5ef2aSThomas Huth static Property arm_cpu_pmsav7_dregion_property = 14078d92e26bSPeter Maydell DEFINE_PROP_UNSIGNED_NODEFAULT("pmsav7-dregion", ARMCPU, 14088d92e26bSPeter Maydell pmsav7_dregion, 14098d92e26bSPeter Maydell qdev_prop_uint32, uint32_t); 1410fcf5ef2aSThomas Huth 1411ae502508SAndrew Jones static bool arm_get_pmu(Object *obj, Error **errp) 1412ae502508SAndrew Jones { 1413ae502508SAndrew Jones ARMCPU *cpu = ARM_CPU(obj); 1414ae502508SAndrew Jones 1415ae502508SAndrew Jones return cpu->has_pmu; 1416ae502508SAndrew Jones } 1417ae502508SAndrew Jones 1418ae502508SAndrew Jones static void arm_set_pmu(Object *obj, bool value, Error **errp) 1419ae502508SAndrew Jones { 1420ae502508SAndrew Jones ARMCPU *cpu = ARM_CPU(obj); 1421ae502508SAndrew Jones 1422ae502508SAndrew Jones if (value) { 14237d20e681SPhilippe Mathieu-Daudé if (kvm_enabled() && !kvm_arm_pmu_supported()) { 1424ae502508SAndrew Jones error_setg(errp, "'pmu' feature not supported by KVM on this host"); 1425ae502508SAndrew Jones return; 1426ae502508SAndrew Jones } 1427ae502508SAndrew Jones set_feature(&cpu->env, ARM_FEATURE_PMU); 1428ae502508SAndrew Jones } else { 1429ae502508SAndrew Jones unset_feature(&cpu->env, ARM_FEATURE_PMU); 1430ae502508SAndrew Jones } 1431ae502508SAndrew Jones cpu->has_pmu = value; 1432ae502508SAndrew Jones } 1433ae502508SAndrew Jones 14347def8754SAndrew Jeffery unsigned int gt_cntfrq_period_ns(ARMCPU *cpu) 14357def8754SAndrew Jeffery { 143696eec6b2SAndrew Jeffery /* 143796eec6b2SAndrew Jeffery * The exact approach to calculating guest ticks is: 143896eec6b2SAndrew Jeffery * 143996eec6b2SAndrew Jeffery * muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), cpu->gt_cntfrq_hz, 144096eec6b2SAndrew Jeffery * NANOSECONDS_PER_SECOND); 144196eec6b2SAndrew Jeffery * 144296eec6b2SAndrew Jeffery * We don't do that. Rather we intentionally use integer division 144396eec6b2SAndrew Jeffery * truncation below and in the caller for the conversion of host monotonic 144496eec6b2SAndrew Jeffery * time to guest ticks to provide the exact inverse for the semantics of 144596eec6b2SAndrew Jeffery * the QEMUTimer scale factor. QEMUTimer's scale facter is an integer, so 144696eec6b2SAndrew Jeffery * it loses precision when representing frequencies where 144796eec6b2SAndrew Jeffery * `(NANOSECONDS_PER_SECOND % cpu->gt_cntfrq) > 0` holds. Failing to 144896eec6b2SAndrew Jeffery * provide an exact inverse leads to scheduling timers with negative 144996eec6b2SAndrew Jeffery * periods, which in turn leads to sticky behaviour in the guest. 145096eec6b2SAndrew Jeffery * 145196eec6b2SAndrew Jeffery * Finally, CNTFRQ is effectively capped at 1GHz to ensure our scale factor 145296eec6b2SAndrew Jeffery * cannot become zero. 145396eec6b2SAndrew Jeffery */ 14547def8754SAndrew Jeffery return NANOSECONDS_PER_SECOND > cpu->gt_cntfrq_hz ? 14557def8754SAndrew Jeffery NANOSECONDS_PER_SECOND / cpu->gt_cntfrq_hz : 1; 14567def8754SAndrew Jeffery } 14577def8754SAndrew Jeffery 1458b8f7959fSPeter Maydell static void arm_cpu_propagate_feature_implications(ARMCPU *cpu) 1459b8f7959fSPeter Maydell { 1460b8f7959fSPeter Maydell CPUARMState *env = &cpu->env; 1461b8f7959fSPeter Maydell bool no_aa32 = false; 1462b8f7959fSPeter Maydell 1463b8f7959fSPeter Maydell /* 1464b8f7959fSPeter Maydell * Some features automatically imply others: set the feature 1465b8f7959fSPeter Maydell * bits explicitly for these cases. 1466b8f7959fSPeter Maydell */ 1467b8f7959fSPeter Maydell 1468b8f7959fSPeter Maydell if (arm_feature(env, ARM_FEATURE_M)) { 1469b8f7959fSPeter Maydell set_feature(env, ARM_FEATURE_PMSA); 1470b8f7959fSPeter Maydell } 1471b8f7959fSPeter Maydell 1472b8f7959fSPeter Maydell if (arm_feature(env, ARM_FEATURE_V8)) { 1473b8f7959fSPeter Maydell if (arm_feature(env, ARM_FEATURE_M)) { 1474b8f7959fSPeter Maydell set_feature(env, ARM_FEATURE_V7); 1475b8f7959fSPeter Maydell } else { 1476b8f7959fSPeter Maydell set_feature(env, ARM_FEATURE_V7VE); 1477b8f7959fSPeter Maydell } 1478b8f7959fSPeter Maydell } 1479b8f7959fSPeter Maydell 1480b8f7959fSPeter Maydell /* 1481b8f7959fSPeter Maydell * There exist AArch64 cpus without AArch32 support. When KVM 1482b8f7959fSPeter Maydell * queries ID_ISAR0_EL1 on such a host, the value is UNKNOWN. 1483b8f7959fSPeter Maydell * Similarly, we cannot check ID_AA64PFR0 without AArch64 support. 1484b8f7959fSPeter Maydell * As a general principle, we also do not make ID register 1485b8f7959fSPeter Maydell * consistency checks anywhere unless using TCG, because only 1486b8f7959fSPeter Maydell * for TCG would a consistency-check failure be a QEMU bug. 1487b8f7959fSPeter Maydell */ 1488b8f7959fSPeter Maydell if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { 1489b8f7959fSPeter Maydell no_aa32 = !cpu_isar_feature(aa64_aa32, cpu); 1490b8f7959fSPeter Maydell } 1491b8f7959fSPeter Maydell 1492b8f7959fSPeter Maydell if (arm_feature(env, ARM_FEATURE_V7VE)) { 1493b8f7959fSPeter Maydell /* 1494b8f7959fSPeter Maydell * v7 Virtualization Extensions. In real hardware this implies 1495b8f7959fSPeter Maydell * EL2 and also the presence of the Security Extensions. 1496b8f7959fSPeter Maydell * For QEMU, for backwards-compatibility we implement some 1497b8f7959fSPeter Maydell * CPUs or CPU configs which have no actual EL2 or EL3 but do 1498b8f7959fSPeter Maydell * include the various other features that V7VE implies. 1499b8f7959fSPeter Maydell * Presence of EL2 itself is ARM_FEATURE_EL2, and of the 1500b8f7959fSPeter Maydell * Security Extensions is ARM_FEATURE_EL3. 1501b8f7959fSPeter Maydell */ 1502b8f7959fSPeter Maydell assert(!tcg_enabled() || no_aa32 || 1503b8f7959fSPeter Maydell cpu_isar_feature(aa32_arm_div, cpu)); 1504b8f7959fSPeter Maydell set_feature(env, ARM_FEATURE_LPAE); 1505b8f7959fSPeter Maydell set_feature(env, ARM_FEATURE_V7); 1506b8f7959fSPeter Maydell } 1507b8f7959fSPeter Maydell if (arm_feature(env, ARM_FEATURE_V7)) { 1508b8f7959fSPeter Maydell set_feature(env, ARM_FEATURE_VAPA); 1509b8f7959fSPeter Maydell set_feature(env, ARM_FEATURE_THUMB2); 1510b8f7959fSPeter Maydell set_feature(env, ARM_FEATURE_MPIDR); 1511b8f7959fSPeter Maydell if (!arm_feature(env, ARM_FEATURE_M)) { 1512b8f7959fSPeter Maydell set_feature(env, ARM_FEATURE_V6K); 1513b8f7959fSPeter Maydell } else { 1514b8f7959fSPeter Maydell set_feature(env, ARM_FEATURE_V6); 1515b8f7959fSPeter Maydell } 1516b8f7959fSPeter Maydell 1517b8f7959fSPeter Maydell /* 1518b8f7959fSPeter Maydell * Always define VBAR for V7 CPUs even if it doesn't exist in 1519b8f7959fSPeter Maydell * non-EL3 configs. This is needed by some legacy boards. 1520b8f7959fSPeter Maydell */ 1521b8f7959fSPeter Maydell set_feature(env, ARM_FEATURE_VBAR); 1522b8f7959fSPeter Maydell } 1523b8f7959fSPeter Maydell if (arm_feature(env, ARM_FEATURE_V6K)) { 1524b8f7959fSPeter Maydell set_feature(env, ARM_FEATURE_V6); 1525b8f7959fSPeter Maydell set_feature(env, ARM_FEATURE_MVFR); 1526b8f7959fSPeter Maydell } 1527b8f7959fSPeter Maydell if (arm_feature(env, ARM_FEATURE_V6)) { 1528b8f7959fSPeter Maydell set_feature(env, ARM_FEATURE_V5); 1529b8f7959fSPeter Maydell if (!arm_feature(env, ARM_FEATURE_M)) { 1530b8f7959fSPeter Maydell assert(!tcg_enabled() || no_aa32 || 1531b8f7959fSPeter Maydell cpu_isar_feature(aa32_jazelle, cpu)); 1532b8f7959fSPeter Maydell set_feature(env, ARM_FEATURE_AUXCR); 1533b8f7959fSPeter Maydell } 1534b8f7959fSPeter Maydell } 1535b8f7959fSPeter Maydell if (arm_feature(env, ARM_FEATURE_V5)) { 1536b8f7959fSPeter Maydell set_feature(env, ARM_FEATURE_V4T); 1537b8f7959fSPeter Maydell } 1538b8f7959fSPeter Maydell if (arm_feature(env, ARM_FEATURE_LPAE)) { 1539b8f7959fSPeter Maydell set_feature(env, ARM_FEATURE_V7MP); 1540b8f7959fSPeter Maydell } 1541b8f7959fSPeter Maydell if (arm_feature(env, ARM_FEATURE_CBAR_RO)) { 1542b8f7959fSPeter Maydell set_feature(env, ARM_FEATURE_CBAR); 1543b8f7959fSPeter Maydell } 1544b8f7959fSPeter Maydell if (arm_feature(env, ARM_FEATURE_THUMB2) && 1545b8f7959fSPeter Maydell !arm_feature(env, ARM_FEATURE_M)) { 1546b8f7959fSPeter Maydell set_feature(env, ARM_FEATURE_THUMB_DSP); 1547b8f7959fSPeter Maydell } 1548b8f7959fSPeter Maydell } 1549b8f7959fSPeter Maydell 155051e5ef45SMarc-André Lureau void arm_cpu_post_init(Object *obj) 1551fcf5ef2aSThomas Huth { 1552fcf5ef2aSThomas Huth ARMCPU *cpu = ARM_CPU(obj); 1553fcf5ef2aSThomas Huth 1554b8f7959fSPeter Maydell /* 1555b8f7959fSPeter Maydell * Some features imply others. Figure this out now, because we 1556b8f7959fSPeter Maydell * are going to look at the feature bits in deciding which 1557b8f7959fSPeter Maydell * properties to add. 1558790a1150SPeter Maydell */ 1559b8f7959fSPeter Maydell arm_cpu_propagate_feature_implications(cpu); 1560790a1150SPeter Maydell 1561fcf5ef2aSThomas Huth if (arm_feature(&cpu->env, ARM_FEATURE_CBAR) || 1562fcf5ef2aSThomas Huth arm_feature(&cpu->env, ARM_FEATURE_CBAR_RO)) { 156394d912d1SMarc-André Lureau qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_cbar_property); 1564fcf5ef2aSThomas Huth } 1565fcf5ef2aSThomas Huth 1566fcf5ef2aSThomas Huth if (!arm_feature(&cpu->env, ARM_FEATURE_M)) { 156794d912d1SMarc-André Lureau qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_hivecs_property); 1568fcf5ef2aSThomas Huth } 1569fcf5ef2aSThomas Huth 1570910e4f24STobias Röhmel if (arm_feature(&cpu->env, ARM_FEATURE_V8)) { 15714a7319b7SEdgar E. Iglesias object_property_add_uint64_ptr(obj, "rvbar", 15724a7319b7SEdgar E. Iglesias &cpu->rvbar_prop, 15734a7319b7SEdgar E. Iglesias OBJ_PROP_FLAG_READWRITE); 1574fcf5ef2aSThomas Huth } 1575fcf5ef2aSThomas Huth 157645ca3a14SRichard Henderson #ifndef CONFIG_USER_ONLY 1577fcf5ef2aSThomas Huth if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) { 1578fcf5ef2aSThomas Huth /* Add the has_el3 state CPU property only if EL3 is allowed. This will 1579fcf5ef2aSThomas Huth * prevent "has_el3" from existing on CPUs which cannot support EL3. 1580fcf5ef2aSThomas Huth */ 158194d912d1SMarc-André Lureau qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el3_property); 1582fcf5ef2aSThomas Huth 1583fcf5ef2aSThomas Huth object_property_add_link(obj, "secure-memory", 1584fcf5ef2aSThomas Huth TYPE_MEMORY_REGION, 1585fcf5ef2aSThomas Huth (Object **)&cpu->secure_memory, 1586fcf5ef2aSThomas Huth qdev_prop_allow_set_link_before_realize, 1587d2623129SMarkus Armbruster OBJ_PROP_LINK_STRONG); 1588fcf5ef2aSThomas Huth } 1589fcf5ef2aSThomas Huth 1590c25bd18aSPeter Maydell if (arm_feature(&cpu->env, ARM_FEATURE_EL2)) { 159194d912d1SMarc-André Lureau qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el2_property); 1592c25bd18aSPeter Maydell } 159345ca3a14SRichard Henderson #endif 1594c25bd18aSPeter Maydell 1595fcf5ef2aSThomas Huth if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) { 1596ae502508SAndrew Jones cpu->has_pmu = true; 1597d2623129SMarkus Armbruster object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu); 1598fcf5ef2aSThomas Huth } 1599fcf5ef2aSThomas Huth 160097a28b0eSPeter Maydell /* 160197a28b0eSPeter Maydell * Allow user to turn off VFP and Neon support, but only for TCG -- 160297a28b0eSPeter Maydell * KVM does not currently allow us to lie to the guest about its 160397a28b0eSPeter Maydell * ID/feature registers, so the guest always sees what the host has. 160497a28b0eSPeter Maydell */ 16054315f7c6SRichard Henderson if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { 16064315f7c6SRichard Henderson if (cpu_isar_feature(aa64_fp_simd, cpu)) { 160797a28b0eSPeter Maydell cpu->has_vfp = true; 160842bea956SCédric Le Goater cpu->has_vfp_d32 = true; 16094315f7c6SRichard Henderson if (tcg_enabled() || qtest_enabled()) { 16104315f7c6SRichard Henderson qdev_property_add_static(DEVICE(obj), 16114315f7c6SRichard Henderson &arm_cpu_has_vfp_property); 16124315f7c6SRichard Henderson } 16134315f7c6SRichard Henderson } 16144315f7c6SRichard Henderson } else if (cpu_isar_feature(aa32_vfp, cpu)) { 16154315f7c6SRichard Henderson cpu->has_vfp = true; 16164315f7c6SRichard Henderson if (cpu_isar_feature(aa32_simd_r32, cpu)) { 16174315f7c6SRichard Henderson cpu->has_vfp_d32 = true; 161842bea956SCédric Le Goater /* 161942bea956SCédric Le Goater * The permitted values of the SIMDReg bits [3:0] on 162042bea956SCédric Le Goater * Armv8-A are either 0b0000 and 0b0010. On such CPUs, 162142bea956SCédric Le Goater * make sure that has_vfp_d32 can not be set to false. 162242bea956SCédric Le Goater */ 16234315f7c6SRichard Henderson if ((tcg_enabled() || qtest_enabled()) 16244315f7c6SRichard Henderson && !(arm_feature(&cpu->env, ARM_FEATURE_V8) 16254315f7c6SRichard Henderson && !arm_feature(&cpu->env, ARM_FEATURE_M))) { 162642bea956SCédric Le Goater qdev_property_add_static(DEVICE(obj), 162742bea956SCédric Le Goater &arm_cpu_has_vfp_d32_property); 162842bea956SCédric Le Goater } 162942bea956SCédric Le Goater } 163042bea956SCédric Le Goater } 163142bea956SCédric Le Goater 163297a28b0eSPeter Maydell if (arm_feature(&cpu->env, ARM_FEATURE_NEON)) { 163397a28b0eSPeter Maydell cpu->has_neon = true; 163497a28b0eSPeter Maydell if (!kvm_enabled()) { 163594d912d1SMarc-André Lureau qdev_property_add_static(DEVICE(obj), &arm_cpu_has_neon_property); 163697a28b0eSPeter Maydell } 163797a28b0eSPeter Maydell } 163897a28b0eSPeter Maydell 1639ea90db0aSPeter Maydell if (arm_feature(&cpu->env, ARM_FEATURE_M) && 1640ea90db0aSPeter Maydell arm_feature(&cpu->env, ARM_FEATURE_THUMB_DSP)) { 164194d912d1SMarc-André Lureau qdev_property_add_static(DEVICE(obj), &arm_cpu_has_dsp_property); 1642ea90db0aSPeter Maydell } 1643ea90db0aSPeter Maydell 1644452a0955SPeter Maydell if (arm_feature(&cpu->env, ARM_FEATURE_PMSA)) { 164594d912d1SMarc-André Lureau qdev_property_add_static(DEVICE(obj), &arm_cpu_has_mpu_property); 1646fcf5ef2aSThomas Huth if (arm_feature(&cpu->env, ARM_FEATURE_V7)) { 1647fcf5ef2aSThomas Huth qdev_property_add_static(DEVICE(obj), 164894d912d1SMarc-André Lureau &arm_cpu_pmsav7_dregion_property); 1649fcf5ef2aSThomas Huth } 1650fcf5ef2aSThomas Huth } 1651fcf5ef2aSThomas Huth 1652181962fdSPeter Maydell if (arm_feature(&cpu->env, ARM_FEATURE_M_SECURITY)) { 1653181962fdSPeter Maydell object_property_add_link(obj, "idau", TYPE_IDAU_INTERFACE, &cpu->idau, 1654181962fdSPeter Maydell qdev_prop_allow_set_link_before_realize, 1655d2623129SMarkus Armbruster OBJ_PROP_LINK_STRONG); 1656f9f62e4cSPeter Maydell /* 1657f9f62e4cSPeter Maydell * M profile: initial value of the Secure VTOR. We can't just use 1658f9f62e4cSPeter Maydell * a simple DEFINE_PROP_UINT32 for this because we want to permit 1659f9f62e4cSPeter Maydell * the property to be set after realize. 1660f9f62e4cSPeter Maydell */ 166164a7b8deSFelipe Franciosi object_property_add_uint32_ptr(obj, "init-svtor", 166264a7b8deSFelipe Franciosi &cpu->init_svtor, 1663d2623129SMarkus Armbruster OBJ_PROP_FLAG_READWRITE); 1664181962fdSPeter Maydell } 16657cda2149SPeter Maydell if (arm_feature(&cpu->env, ARM_FEATURE_M)) { 16667cda2149SPeter Maydell /* 16677cda2149SPeter Maydell * Initial value of the NS VTOR (for cores without the Security 16687cda2149SPeter Maydell * extension, this is the only VTOR) 16697cda2149SPeter Maydell */ 16707cda2149SPeter Maydell object_property_add_uint32_ptr(obj, "init-nsvtor", 16717cda2149SPeter Maydell &cpu->init_nsvtor, 16727cda2149SPeter Maydell OBJ_PROP_FLAG_READWRITE); 16737cda2149SPeter Maydell } 1674181962fdSPeter Maydell 1675bddd892eSPeter Maydell /* Not DEFINE_PROP_UINT32: we want this to be settable after realize */ 1676bddd892eSPeter Maydell object_property_add_uint32_ptr(obj, "psci-conduit", 1677bddd892eSPeter Maydell &cpu->psci_conduit, 1678bddd892eSPeter Maydell OBJ_PROP_FLAG_READWRITE); 1679bddd892eSPeter Maydell 168094d912d1SMarc-André Lureau qdev_property_add_static(DEVICE(obj), &arm_cpu_cfgend_property); 168196eec6b2SAndrew Jeffery 168296eec6b2SAndrew Jeffery if (arm_feature(&cpu->env, ARM_FEATURE_GENERIC_TIMER)) { 168394d912d1SMarc-André Lureau qdev_property_add_static(DEVICE(cpu), &arm_cpu_gt_cntfrq_property); 168496eec6b2SAndrew Jeffery } 16859e6f8d8aSfangying 16869e6f8d8aSfangying if (kvm_enabled()) { 16879e6f8d8aSfangying kvm_arm_add_vcpu_properties(obj); 16889e6f8d8aSfangying } 16898bce44a2SRichard Henderson 16908bce44a2SRichard Henderson #ifndef CONFIG_USER_ONLY 16918bce44a2SRichard Henderson if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && 16928bce44a2SRichard Henderson cpu_isar_feature(aa64_mte, cpu)) { 16938bce44a2SRichard Henderson object_property_add_link(obj, "tag-memory", 16948bce44a2SRichard Henderson TYPE_MEMORY_REGION, 16958bce44a2SRichard Henderson (Object **)&cpu->tag_memory, 16968bce44a2SRichard Henderson qdev_prop_allow_set_link_before_realize, 16978bce44a2SRichard Henderson OBJ_PROP_LINK_STRONG); 16988bce44a2SRichard Henderson 16998bce44a2SRichard Henderson if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) { 17008bce44a2SRichard Henderson object_property_add_link(obj, "secure-tag-memory", 17018bce44a2SRichard Henderson TYPE_MEMORY_REGION, 17028bce44a2SRichard Henderson (Object **)&cpu->secure_tag_memory, 17038bce44a2SRichard Henderson qdev_prop_allow_set_link_before_realize, 17048bce44a2SRichard Henderson OBJ_PROP_LINK_STRONG); 17058bce44a2SRichard Henderson } 17068bce44a2SRichard Henderson } 17078bce44a2SRichard Henderson #endif 1708fcf5ef2aSThomas Huth } 1709fcf5ef2aSThomas Huth 1710fcf5ef2aSThomas Huth static void arm_cpu_finalizefn(Object *obj) 1711fcf5ef2aSThomas Huth { 1712fcf5ef2aSThomas Huth ARMCPU *cpu = ARM_CPU(obj); 171308267487SAaron Lindsay ARMELChangeHook *hook, *next; 171408267487SAaron Lindsay 1715fcf5ef2aSThomas Huth g_hash_table_destroy(cpu->cp_regs); 171608267487SAaron Lindsay 1717b5c53d1bSAaron Lindsay QLIST_FOREACH_SAFE(hook, &cpu->pre_el_change_hooks, node, next) { 1718b5c53d1bSAaron Lindsay QLIST_REMOVE(hook, node); 1719b5c53d1bSAaron Lindsay g_free(hook); 1720b5c53d1bSAaron Lindsay } 172108267487SAaron Lindsay QLIST_FOREACH_SAFE(hook, &cpu->el_change_hooks, node, next) { 172208267487SAaron Lindsay QLIST_REMOVE(hook, node); 172308267487SAaron Lindsay g_free(hook); 172408267487SAaron Lindsay } 17254e7beb0cSAaron Lindsay OS #ifndef CONFIG_USER_ONLY 17264e7beb0cSAaron Lindsay OS if (cpu->pmu_timer) { 17274e7beb0cSAaron Lindsay OS timer_free(cpu->pmu_timer); 17284e7beb0cSAaron Lindsay OS } 17294e7beb0cSAaron Lindsay OS #endif 1730fcf5ef2aSThomas Huth } 1731fcf5ef2aSThomas Huth 17320df9142dSAndrew Jones void arm_cpu_finalize_features(ARMCPU *cpu, Error **errp) 17330df9142dSAndrew Jones { 17340df9142dSAndrew Jones Error *local_err = NULL; 17350df9142dSAndrew Jones 173607301161SRichard Henderson #ifdef TARGET_AARCH64 17370df9142dSAndrew Jones if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { 17380df9142dSAndrew Jones arm_cpu_sve_finalize(cpu, &local_err); 17390df9142dSAndrew Jones if (local_err != NULL) { 17400df9142dSAndrew Jones error_propagate(errp, local_err); 17410df9142dSAndrew Jones return; 17420df9142dSAndrew Jones } 1743eb94284dSRichard Henderson 1744e74c0976SRichard Henderson arm_cpu_sme_finalize(cpu, &local_err); 1745e74c0976SRichard Henderson if (local_err != NULL) { 1746e74c0976SRichard Henderson error_propagate(errp, local_err); 1747e74c0976SRichard Henderson return; 1748e74c0976SRichard Henderson } 1749e74c0976SRichard Henderson 1750eb94284dSRichard Henderson arm_cpu_pauth_finalize(cpu, &local_err); 1751eb94284dSRichard Henderson if (local_err != NULL) { 1752eb94284dSRichard Henderson error_propagate(errp, local_err); 1753eb94284dSRichard Henderson return; 1754eb94284dSRichard Henderson } 175569b2265dSRichard Henderson 175669b2265dSRichard Henderson arm_cpu_lpa2_finalize(cpu, &local_err); 175769b2265dSRichard Henderson if (local_err != NULL) { 175869b2265dSRichard Henderson error_propagate(errp, local_err); 175969b2265dSRichard Henderson return; 176069b2265dSRichard Henderson } 1761eb94284dSRichard Henderson } 176207301161SRichard Henderson #endif 176368970d1eSAndrew Jones 176468970d1eSAndrew Jones if (kvm_enabled()) { 176568970d1eSAndrew Jones kvm_arm_steal_time_finalize(cpu, &local_err); 176668970d1eSAndrew Jones if (local_err != NULL) { 176768970d1eSAndrew Jones error_propagate(errp, local_err); 176868970d1eSAndrew Jones return; 176968970d1eSAndrew Jones } 177068970d1eSAndrew Jones } 17710df9142dSAndrew Jones } 17720df9142dSAndrew Jones 1773fcf5ef2aSThomas Huth static void arm_cpu_realizefn(DeviceState *dev, Error **errp) 1774fcf5ef2aSThomas Huth { 1775fcf5ef2aSThomas Huth CPUState *cs = CPU(dev); 1776fcf5ef2aSThomas Huth ARMCPU *cpu = ARM_CPU(dev); 1777fcf5ef2aSThomas Huth ARMCPUClass *acc = ARM_CPU_GET_CLASS(dev); 1778fcf5ef2aSThomas Huth CPUARMState *env = &cpu->env; 1779fcf5ef2aSThomas Huth int pagebits; 1780fcf5ef2aSThomas Huth Error *local_err = NULL; 1781fcf5ef2aSThomas Huth 1782e607ea39SAnton Johansson /* Use pc-relative instructions in system-mode */ 1783e607ea39SAnton Johansson #ifndef CONFIG_USER_ONLY 1784e607ea39SAnton Johansson cs->tcg_cflags |= CF_PCREL; 1785e607ea39SAnton Johansson #endif 1786e607ea39SAnton Johansson 1787c4487d76SPeter Maydell /* If we needed to query the host kernel for the CPU features 1788c4487d76SPeter Maydell * then it's possible that might have failed in the initfn, but 1789c4487d76SPeter Maydell * this is the first point where we can report it. 1790c4487d76SPeter Maydell */ 1791c4487d76SPeter Maydell if (cpu->host_cpu_probe_failed) { 1792585df85eSPeter Maydell if (!kvm_enabled() && !hvf_enabled()) { 1793585df85eSPeter Maydell error_setg(errp, "The 'host' CPU type can only be used with KVM or HVF"); 1794c4487d76SPeter Maydell } else { 1795c4487d76SPeter Maydell error_setg(errp, "Failed to retrieve host CPU features"); 1796c4487d76SPeter Maydell } 1797c4487d76SPeter Maydell return; 1798c4487d76SPeter Maydell } 1799c4487d76SPeter Maydell 180095f87565SPeter Maydell #ifndef CONFIG_USER_ONLY 180195f87565SPeter Maydell /* The NVIC and M-profile CPU are two halves of a single piece of 180295f87565SPeter Maydell * hardware; trying to use one without the other is a command line 180395f87565SPeter Maydell * error and will result in segfaults if not caught here. 180495f87565SPeter Maydell */ 180595f87565SPeter Maydell if (arm_feature(env, ARM_FEATURE_M)) { 180695f87565SPeter Maydell if (!env->nvic) { 180795f87565SPeter Maydell error_setg(errp, "This board cannot be used with Cortex-M CPUs"); 180895f87565SPeter Maydell return; 180995f87565SPeter Maydell } 181095f87565SPeter Maydell } else { 181195f87565SPeter Maydell if (env->nvic) { 181295f87565SPeter Maydell error_setg(errp, "This board can only be used with Cortex-M CPUs"); 181395f87565SPeter Maydell return; 181495f87565SPeter Maydell } 181595f87565SPeter Maydell } 1816397cd31fSPeter Maydell 1817045e5064SAlexander Graf if (!tcg_enabled() && !qtest_enabled()) { 181849e7f191SPeter Maydell /* 1819045e5064SAlexander Graf * We assume that no accelerator except TCG (and the "not really an 1820045e5064SAlexander Graf * accelerator" qtest) can handle these features, because Arm hardware 1821045e5064SAlexander Graf * virtualization can't virtualize them. 1822045e5064SAlexander Graf * 182349e7f191SPeter Maydell * Catch all the cases which might cause us to create more than one 182449e7f191SPeter Maydell * address space for the CPU (otherwise we will assert() later in 182549e7f191SPeter Maydell * cpu_address_space_init()). 182649e7f191SPeter Maydell */ 182749e7f191SPeter Maydell if (arm_feature(env, ARM_FEATURE_M)) { 182849e7f191SPeter Maydell error_setg(errp, 1829045e5064SAlexander Graf "Cannot enable %s when using an M-profile guest CPU", 1830045e5064SAlexander Graf current_accel_name()); 183149e7f191SPeter Maydell return; 183249e7f191SPeter Maydell } 183349e7f191SPeter Maydell if (cpu->has_el3) { 183449e7f191SPeter Maydell error_setg(errp, 1835045e5064SAlexander Graf "Cannot enable %s when guest CPU has EL3 enabled", 1836045e5064SAlexander Graf current_accel_name()); 183749e7f191SPeter Maydell return; 183849e7f191SPeter Maydell } 183949e7f191SPeter Maydell if (cpu->tag_memory) { 184049e7f191SPeter Maydell error_setg(errp, 1841d009607dSPeter Maydell "Cannot enable %s when guest CPUs has MTE enabled", 1842045e5064SAlexander Graf current_accel_name()); 184349e7f191SPeter Maydell return; 184449e7f191SPeter Maydell } 184549e7f191SPeter Maydell } 184649e7f191SPeter Maydell 184796eec6b2SAndrew Jeffery { 184896eec6b2SAndrew Jeffery uint64_t scale; 184996eec6b2SAndrew Jeffery 185096eec6b2SAndrew Jeffery if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) { 185196eec6b2SAndrew Jeffery if (!cpu->gt_cntfrq_hz) { 185296eec6b2SAndrew Jeffery error_setg(errp, "Invalid CNTFRQ: %"PRId64"Hz", 185396eec6b2SAndrew Jeffery cpu->gt_cntfrq_hz); 185496eec6b2SAndrew Jeffery return; 185596eec6b2SAndrew Jeffery } 185696eec6b2SAndrew Jeffery scale = gt_cntfrq_period_ns(cpu); 185796eec6b2SAndrew Jeffery } else { 185896eec6b2SAndrew Jeffery scale = GTIMER_SCALE; 185996eec6b2SAndrew Jeffery } 186096eec6b2SAndrew Jeffery 186196eec6b2SAndrew Jeffery cpu->gt_timer[GTIMER_PHYS] = timer_new(QEMU_CLOCK_VIRTUAL, scale, 1862397cd31fSPeter Maydell arm_gt_ptimer_cb, cpu); 186396eec6b2SAndrew Jeffery cpu->gt_timer[GTIMER_VIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale, 1864397cd31fSPeter Maydell arm_gt_vtimer_cb, cpu); 186596eec6b2SAndrew Jeffery cpu->gt_timer[GTIMER_HYP] = timer_new(QEMU_CLOCK_VIRTUAL, scale, 1866397cd31fSPeter Maydell arm_gt_htimer_cb, cpu); 186796eec6b2SAndrew Jeffery cpu->gt_timer[GTIMER_SEC] = timer_new(QEMU_CLOCK_VIRTUAL, scale, 1868397cd31fSPeter Maydell arm_gt_stimer_cb, cpu); 18698c94b071SRichard Henderson cpu->gt_timer[GTIMER_HYPVIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale, 18708c94b071SRichard Henderson arm_gt_hvtimer_cb, cpu); 187196eec6b2SAndrew Jeffery } 187295f87565SPeter Maydell #endif 187395f87565SPeter Maydell 1874fcf5ef2aSThomas Huth cpu_exec_realizefn(cs, &local_err); 1875fcf5ef2aSThomas Huth if (local_err != NULL) { 1876fcf5ef2aSThomas Huth error_propagate(errp, local_err); 1877fcf5ef2aSThomas Huth return; 1878fcf5ef2aSThomas Huth } 1879fcf5ef2aSThomas Huth 18800df9142dSAndrew Jones arm_cpu_finalize_features(cpu, &local_err); 18810df9142dSAndrew Jones if (local_err != NULL) { 18820df9142dSAndrew Jones error_propagate(errp, local_err); 18830df9142dSAndrew Jones return; 18840df9142dSAndrew Jones } 18850df9142dSAndrew Jones 18869719f125SJohn Högberg #ifdef CONFIG_USER_ONLY 18879719f125SJohn Högberg /* 18889719f125SJohn Högberg * User mode relies on IC IVAU instructions to catch modification of 18899719f125SJohn Högberg * dual-mapped code. 18909719f125SJohn Högberg * 18919719f125SJohn Högberg * Clear CTR_EL0.DIC to ensure that software that honors these flags uses 18929719f125SJohn Högberg * IC IVAU even if the emulated processor does not normally require it. 18939719f125SJohn Högberg */ 18949719f125SJohn Högberg cpu->ctr = FIELD_DP64(cpu->ctr, CTR_EL0, DIC, 0); 18959719f125SJohn Högberg #endif 18969719f125SJohn Högberg 189797a28b0eSPeter Maydell if (arm_feature(env, ARM_FEATURE_AARCH64) && 189897a28b0eSPeter Maydell cpu->has_vfp != cpu->has_neon) { 189997a28b0eSPeter Maydell /* 190097a28b0eSPeter Maydell * This is an architectural requirement for AArch64; AArch32 is 190197a28b0eSPeter Maydell * more flexible and permits VFP-no-Neon and Neon-no-VFP. 190297a28b0eSPeter Maydell */ 190397a28b0eSPeter Maydell error_setg(errp, 190497a28b0eSPeter Maydell "AArch64 CPUs must have both VFP and Neon or neither"); 190597a28b0eSPeter Maydell return; 190697a28b0eSPeter Maydell } 190797a28b0eSPeter Maydell 190842bea956SCédric Le Goater if (cpu->has_vfp_d32 != cpu->has_neon) { 190942bea956SCédric Le Goater error_setg(errp, "ARM CPUs must have both VFP-D32 and Neon or neither"); 191042bea956SCédric Le Goater return; 191142bea956SCédric Le Goater } 191242bea956SCédric Le Goater 191342bea956SCédric Le Goater if (!cpu->has_vfp_d32) { 191442bea956SCédric Le Goater uint32_t u; 191542bea956SCédric Le Goater 191642bea956SCédric Le Goater u = cpu->isar.mvfr0; 191742bea956SCédric Le Goater u = FIELD_DP32(u, MVFR0, SIMDREG, 1); /* 16 registers */ 191842bea956SCédric Le Goater cpu->isar.mvfr0 = u; 191942bea956SCédric Le Goater } 192042bea956SCédric Le Goater 192197a28b0eSPeter Maydell if (!cpu->has_vfp) { 192297a28b0eSPeter Maydell uint64_t t; 192397a28b0eSPeter Maydell uint32_t u; 192497a28b0eSPeter Maydell 192597a28b0eSPeter Maydell t = cpu->isar.id_aa64isar1; 192697a28b0eSPeter Maydell t = FIELD_DP64(t, ID_AA64ISAR1, JSCVT, 0); 192797a28b0eSPeter Maydell cpu->isar.id_aa64isar1 = t; 192897a28b0eSPeter Maydell 192997a28b0eSPeter Maydell t = cpu->isar.id_aa64pfr0; 193097a28b0eSPeter Maydell t = FIELD_DP64(t, ID_AA64PFR0, FP, 0xf); 193197a28b0eSPeter Maydell cpu->isar.id_aa64pfr0 = t; 193297a28b0eSPeter Maydell 193397a28b0eSPeter Maydell u = cpu->isar.id_isar6; 193497a28b0eSPeter Maydell u = FIELD_DP32(u, ID_ISAR6, JSCVT, 0); 19353c93dfa4SRichard Henderson u = FIELD_DP32(u, ID_ISAR6, BF16, 0); 193697a28b0eSPeter Maydell cpu->isar.id_isar6 = u; 193797a28b0eSPeter Maydell 193897a28b0eSPeter Maydell u = cpu->isar.mvfr0; 193997a28b0eSPeter Maydell u = FIELD_DP32(u, MVFR0, FPSP, 0); 194097a28b0eSPeter Maydell u = FIELD_DP32(u, MVFR0, FPDP, 0); 194197a28b0eSPeter Maydell u = FIELD_DP32(u, MVFR0, FPDIVIDE, 0); 194297a28b0eSPeter Maydell u = FIELD_DP32(u, MVFR0, FPSQRT, 0); 194397a28b0eSPeter Maydell u = FIELD_DP32(u, MVFR0, FPROUND, 0); 1944532a3af5SPeter Maydell if (!arm_feature(env, ARM_FEATURE_M)) { 1945532a3af5SPeter Maydell u = FIELD_DP32(u, MVFR0, FPTRAP, 0); 1946532a3af5SPeter Maydell u = FIELD_DP32(u, MVFR0, FPSHVEC, 0); 1947532a3af5SPeter Maydell } 194897a28b0eSPeter Maydell cpu->isar.mvfr0 = u; 194997a28b0eSPeter Maydell 195097a28b0eSPeter Maydell u = cpu->isar.mvfr1; 195197a28b0eSPeter Maydell u = FIELD_DP32(u, MVFR1, FPFTZ, 0); 195297a28b0eSPeter Maydell u = FIELD_DP32(u, MVFR1, FPDNAN, 0); 195397a28b0eSPeter Maydell u = FIELD_DP32(u, MVFR1, FPHP, 0); 1954532a3af5SPeter Maydell if (arm_feature(env, ARM_FEATURE_M)) { 1955532a3af5SPeter Maydell u = FIELD_DP32(u, MVFR1, FP16, 0); 1956532a3af5SPeter Maydell } 195797a28b0eSPeter Maydell cpu->isar.mvfr1 = u; 195897a28b0eSPeter Maydell 195997a28b0eSPeter Maydell u = cpu->isar.mvfr2; 196097a28b0eSPeter Maydell u = FIELD_DP32(u, MVFR2, FPMISC, 0); 196197a28b0eSPeter Maydell cpu->isar.mvfr2 = u; 196297a28b0eSPeter Maydell } 196397a28b0eSPeter Maydell 196497a28b0eSPeter Maydell if (!cpu->has_neon) { 196597a28b0eSPeter Maydell uint64_t t; 196697a28b0eSPeter Maydell uint32_t u; 196797a28b0eSPeter Maydell 196897a28b0eSPeter Maydell unset_feature(env, ARM_FEATURE_NEON); 196997a28b0eSPeter Maydell 197097a28b0eSPeter Maydell t = cpu->isar.id_aa64isar0; 1971eb851c11SDamien Hedde t = FIELD_DP64(t, ID_AA64ISAR0, AES, 0); 1972eb851c11SDamien Hedde t = FIELD_DP64(t, ID_AA64ISAR0, SHA1, 0); 1973eb851c11SDamien Hedde t = FIELD_DP64(t, ID_AA64ISAR0, SHA2, 0); 1974eb851c11SDamien Hedde t = FIELD_DP64(t, ID_AA64ISAR0, SHA3, 0); 1975eb851c11SDamien Hedde t = FIELD_DP64(t, ID_AA64ISAR0, SM3, 0); 1976eb851c11SDamien Hedde t = FIELD_DP64(t, ID_AA64ISAR0, SM4, 0); 197797a28b0eSPeter Maydell t = FIELD_DP64(t, ID_AA64ISAR0, DP, 0); 197897a28b0eSPeter Maydell cpu->isar.id_aa64isar0 = t; 197997a28b0eSPeter Maydell 198097a28b0eSPeter Maydell t = cpu->isar.id_aa64isar1; 198197a28b0eSPeter Maydell t = FIELD_DP64(t, ID_AA64ISAR1, FCMA, 0); 19823c93dfa4SRichard Henderson t = FIELD_DP64(t, ID_AA64ISAR1, BF16, 0); 1983f8680aaaSRichard Henderson t = FIELD_DP64(t, ID_AA64ISAR1, I8MM, 0); 198497a28b0eSPeter Maydell cpu->isar.id_aa64isar1 = t; 198597a28b0eSPeter Maydell 198697a28b0eSPeter Maydell t = cpu->isar.id_aa64pfr0; 198797a28b0eSPeter Maydell t = FIELD_DP64(t, ID_AA64PFR0, ADVSIMD, 0xf); 198897a28b0eSPeter Maydell cpu->isar.id_aa64pfr0 = t; 198997a28b0eSPeter Maydell 199097a28b0eSPeter Maydell u = cpu->isar.id_isar5; 1991eb851c11SDamien Hedde u = FIELD_DP32(u, ID_ISAR5, AES, 0); 1992eb851c11SDamien Hedde u = FIELD_DP32(u, ID_ISAR5, SHA1, 0); 1993eb851c11SDamien Hedde u = FIELD_DP32(u, ID_ISAR5, SHA2, 0); 199497a28b0eSPeter Maydell u = FIELD_DP32(u, ID_ISAR5, RDM, 0); 199597a28b0eSPeter Maydell u = FIELD_DP32(u, ID_ISAR5, VCMA, 0); 199697a28b0eSPeter Maydell cpu->isar.id_isar5 = u; 199797a28b0eSPeter Maydell 199897a28b0eSPeter Maydell u = cpu->isar.id_isar6; 199997a28b0eSPeter Maydell u = FIELD_DP32(u, ID_ISAR6, DP, 0); 200097a28b0eSPeter Maydell u = FIELD_DP32(u, ID_ISAR6, FHM, 0); 20013c93dfa4SRichard Henderson u = FIELD_DP32(u, ID_ISAR6, BF16, 0); 2002f8680aaaSRichard Henderson u = FIELD_DP32(u, ID_ISAR6, I8MM, 0); 200397a28b0eSPeter Maydell cpu->isar.id_isar6 = u; 200497a28b0eSPeter Maydell 2005532a3af5SPeter Maydell if (!arm_feature(env, ARM_FEATURE_M)) { 200697a28b0eSPeter Maydell u = cpu->isar.mvfr1; 200797a28b0eSPeter Maydell u = FIELD_DP32(u, MVFR1, SIMDLS, 0); 200897a28b0eSPeter Maydell u = FIELD_DP32(u, MVFR1, SIMDINT, 0); 200997a28b0eSPeter Maydell u = FIELD_DP32(u, MVFR1, SIMDSP, 0); 201097a28b0eSPeter Maydell u = FIELD_DP32(u, MVFR1, SIMDHP, 0); 201197a28b0eSPeter Maydell cpu->isar.mvfr1 = u; 201297a28b0eSPeter Maydell 201397a28b0eSPeter Maydell u = cpu->isar.mvfr2; 201497a28b0eSPeter Maydell u = FIELD_DP32(u, MVFR2, SIMDMISC, 0); 201597a28b0eSPeter Maydell cpu->isar.mvfr2 = u; 201697a28b0eSPeter Maydell } 2017532a3af5SPeter Maydell } 201897a28b0eSPeter Maydell 201997a28b0eSPeter Maydell if (!cpu->has_neon && !cpu->has_vfp) { 202097a28b0eSPeter Maydell uint64_t t; 202197a28b0eSPeter Maydell uint32_t u; 202297a28b0eSPeter Maydell 202397a28b0eSPeter Maydell t = cpu->isar.id_aa64isar0; 202497a28b0eSPeter Maydell t = FIELD_DP64(t, ID_AA64ISAR0, FHM, 0); 202597a28b0eSPeter Maydell cpu->isar.id_aa64isar0 = t; 202697a28b0eSPeter Maydell 202797a28b0eSPeter Maydell t = cpu->isar.id_aa64isar1; 202897a28b0eSPeter Maydell t = FIELD_DP64(t, ID_AA64ISAR1, FRINTTS, 0); 202997a28b0eSPeter Maydell cpu->isar.id_aa64isar1 = t; 203097a28b0eSPeter Maydell 203197a28b0eSPeter Maydell u = cpu->isar.mvfr0; 203297a28b0eSPeter Maydell u = FIELD_DP32(u, MVFR0, SIMDREG, 0); 203397a28b0eSPeter Maydell cpu->isar.mvfr0 = u; 2034c52881bbSRichard Henderson 2035c52881bbSRichard Henderson /* Despite the name, this field covers both VFP and Neon */ 2036c52881bbSRichard Henderson u = cpu->isar.mvfr1; 2037c52881bbSRichard Henderson u = FIELD_DP32(u, MVFR1, SIMDFMAC, 0); 2038c52881bbSRichard Henderson cpu->isar.mvfr1 = u; 203997a28b0eSPeter Maydell } 204097a28b0eSPeter Maydell 2041ea90db0aSPeter Maydell if (arm_feature(env, ARM_FEATURE_M) && !cpu->has_dsp) { 2042ea90db0aSPeter Maydell uint32_t u; 2043ea90db0aSPeter Maydell 2044ea90db0aSPeter Maydell unset_feature(env, ARM_FEATURE_THUMB_DSP); 2045ea90db0aSPeter Maydell 2046ea90db0aSPeter Maydell u = cpu->isar.id_isar1; 2047ea90db0aSPeter Maydell u = FIELD_DP32(u, ID_ISAR1, EXTEND, 1); 2048ea90db0aSPeter Maydell cpu->isar.id_isar1 = u; 2049ea90db0aSPeter Maydell 2050ea90db0aSPeter Maydell u = cpu->isar.id_isar2; 2051ea90db0aSPeter Maydell u = FIELD_DP32(u, ID_ISAR2, MULTU, 1); 2052ea90db0aSPeter Maydell u = FIELD_DP32(u, ID_ISAR2, MULTS, 1); 2053ea90db0aSPeter Maydell cpu->isar.id_isar2 = u; 2054ea90db0aSPeter Maydell 2055ea90db0aSPeter Maydell u = cpu->isar.id_isar3; 2056ea90db0aSPeter Maydell u = FIELD_DP32(u, ID_ISAR3, SIMD, 1); 2057ea90db0aSPeter Maydell u = FIELD_DP32(u, ID_ISAR3, SATURATE, 0); 2058ea90db0aSPeter Maydell cpu->isar.id_isar3 = u; 2059ea90db0aSPeter Maydell } 2060ea90db0aSPeter Maydell 2061fcf5ef2aSThomas Huth 2062ea7ac69dSPeter Maydell /* 2063ea7ac69dSPeter Maydell * We rely on no XScale CPU having VFP so we can use the same bits in the 2064ea7ac69dSPeter Maydell * TB flags field for VECSTRIDE and XSCALE_CPAR. 2065ea7ac69dSPeter Maydell */ 20667d63183fSRichard Henderson assert(arm_feature(&cpu->env, ARM_FEATURE_AARCH64) || 20677d63183fSRichard Henderson !cpu_isar_feature(aa32_vfp_simd, cpu) || 20687d63183fSRichard Henderson !arm_feature(env, ARM_FEATURE_XSCALE)); 2069ea7ac69dSPeter Maydell 2070fcf5ef2aSThomas Huth if (arm_feature(env, ARM_FEATURE_V7) && 2071fcf5ef2aSThomas Huth !arm_feature(env, ARM_FEATURE_M) && 2072452a0955SPeter Maydell !arm_feature(env, ARM_FEATURE_PMSA)) { 2073fcf5ef2aSThomas Huth /* v7VMSA drops support for the old ARMv5 tiny pages, so we 2074fcf5ef2aSThomas Huth * can use 4K pages. 2075fcf5ef2aSThomas Huth */ 2076fcf5ef2aSThomas Huth pagebits = 12; 2077fcf5ef2aSThomas Huth } else { 2078fcf5ef2aSThomas Huth /* For CPUs which might have tiny 1K pages, or which have an 2079fcf5ef2aSThomas Huth * MPU and might have small region sizes, stick with 1K pages. 2080fcf5ef2aSThomas Huth */ 2081fcf5ef2aSThomas Huth pagebits = 10; 2082fcf5ef2aSThomas Huth } 2083fcf5ef2aSThomas Huth if (!set_preferred_target_page_bits(pagebits)) { 2084fcf5ef2aSThomas Huth /* This can only ever happen for hotplugging a CPU, or if 2085fcf5ef2aSThomas Huth * the board code incorrectly creates a CPU which it has 2086fcf5ef2aSThomas Huth * promised via minimum_page_size that it will not. 2087fcf5ef2aSThomas Huth */ 2088fcf5ef2aSThomas Huth error_setg(errp, "This CPU requires a smaller page size than the " 2089fcf5ef2aSThomas Huth "system is using"); 2090fcf5ef2aSThomas Huth return; 2091fcf5ef2aSThomas Huth } 2092fcf5ef2aSThomas Huth 2093fcf5ef2aSThomas Huth /* This cpu-id-to-MPIDR affinity is used only for TCG; KVM will override it. 2094fcf5ef2aSThomas Huth * We don't support setting cluster ID ([16..23]) (known as Aff2 2095fcf5ef2aSThomas Huth * in later ARM ARM versions), or any of the higher affinity level fields, 2096fcf5ef2aSThomas Huth * so these bits always RAZ. 2097fcf5ef2aSThomas Huth */ 2098fcf5ef2aSThomas Huth if (cpu->mp_affinity == ARM64_AFFINITY_INVALID) { 209946de5913SIgor Mammedov cpu->mp_affinity = arm_cpu_mp_affinity(cs->cpu_index, 210046de5913SIgor Mammedov ARM_DEFAULT_CPUS_PER_CLUSTER); 2101fcf5ef2aSThomas Huth } 2102fcf5ef2aSThomas Huth 2103fcf5ef2aSThomas Huth if (cpu->reset_hivecs) { 2104fcf5ef2aSThomas Huth cpu->reset_sctlr |= (1 << 13); 2105fcf5ef2aSThomas Huth } 2106fcf5ef2aSThomas Huth 21073a062d57SJulian Brown if (cpu->cfgend) { 21083a062d57SJulian Brown if (arm_feature(&cpu->env, ARM_FEATURE_V7)) { 21093a062d57SJulian Brown cpu->reset_sctlr |= SCTLR_EE; 21103a062d57SJulian Brown } else { 21113a062d57SJulian Brown cpu->reset_sctlr |= SCTLR_B; 21123a062d57SJulian Brown } 21133a062d57SJulian Brown } 21143a062d57SJulian Brown 211540188188SPeter Maydell if (!arm_feature(env, ARM_FEATURE_M) && !cpu->has_el3) { 2116fcf5ef2aSThomas Huth /* If the has_el3 CPU property is disabled then we need to disable the 2117fcf5ef2aSThomas Huth * feature. 2118fcf5ef2aSThomas Huth */ 2119fcf5ef2aSThomas Huth unset_feature(env, ARM_FEATURE_EL3); 2120fcf5ef2aSThomas Huth 2121b13c91c0SRichard Henderson /* 2122b13c91c0SRichard Henderson * Disable the security extension feature bits in the processor 2123b13c91c0SRichard Henderson * feature registers as well. 2124fcf5ef2aSThomas Huth */ 2125b13c91c0SRichard Henderson cpu->isar.id_pfr1 = FIELD_DP32(cpu->isar.id_pfr1, ID_PFR1, SECURITY, 0); 2126033a4f15SRichard Henderson cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, COPSDBG, 0); 2127b13c91c0SRichard Henderson cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0, 2128b13c91c0SRichard Henderson ID_AA64PFR0, EL3, 0); 2129b9f335c2SRichard Henderson 2130b9f335c2SRichard Henderson /* Disable the realm management extension, which requires EL3. */ 2131b9f335c2SRichard Henderson cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0, 2132b9f335c2SRichard Henderson ID_AA64PFR0, RME, 0); 2133fcf5ef2aSThomas Huth } 2134fcf5ef2aSThomas Huth 2135c25bd18aSPeter Maydell if (!cpu->has_el2) { 2136c25bd18aSPeter Maydell unset_feature(env, ARM_FEATURE_EL2); 2137c25bd18aSPeter Maydell } 2138c25bd18aSPeter Maydell 2139d6f02ce3SWei Huang if (!cpu->has_pmu) { 2140fcf5ef2aSThomas Huth unset_feature(env, ARM_FEATURE_PMU); 214157a4a11bSAaron Lindsay } 214257a4a11bSAaron Lindsay if (arm_feature(env, ARM_FEATURE_PMU)) { 2143bf8d0969SAaron Lindsay OS pmu_init(cpu); 214457a4a11bSAaron Lindsay 214557a4a11bSAaron Lindsay if (!kvm_enabled()) { 2146033614c4SAaron Lindsay arm_register_pre_el_change_hook(cpu, &pmu_pre_el_change, 0); 2147033614c4SAaron Lindsay arm_register_el_change_hook(cpu, &pmu_post_el_change, 0); 2148fcf5ef2aSThomas Huth } 21494e7beb0cSAaron Lindsay OS 21504e7beb0cSAaron Lindsay OS #ifndef CONFIG_USER_ONLY 21514e7beb0cSAaron Lindsay OS cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, arm_pmu_timer_cb, 21524e7beb0cSAaron Lindsay OS cpu); 21534e7beb0cSAaron Lindsay OS #endif 215457a4a11bSAaron Lindsay } else { 21552a609df8SPeter Maydell cpu->isar.id_aa64dfr0 = 21562a609df8SPeter Maydell FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMUVER, 0); 2157a6179538SPeter Maydell cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, PERFMON, 0); 215857a4a11bSAaron Lindsay cpu->pmceid0 = 0; 215957a4a11bSAaron Lindsay cpu->pmceid1 = 0; 216057a4a11bSAaron Lindsay } 2161fcf5ef2aSThomas Huth 2162fcf5ef2aSThomas Huth if (!arm_feature(env, ARM_FEATURE_EL2)) { 2163b13c91c0SRichard Henderson /* 2164b13c91c0SRichard Henderson * Disable the hypervisor feature bits in the processor feature 2165b13c91c0SRichard Henderson * registers if we don't have EL2. 2166fcf5ef2aSThomas Huth */ 2167b13c91c0SRichard Henderson cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0, 2168b13c91c0SRichard Henderson ID_AA64PFR0, EL2, 0); 2169b13c91c0SRichard Henderson cpu->isar.id_pfr1 = FIELD_DP32(cpu->isar.id_pfr1, 2170b13c91c0SRichard Henderson ID_PFR1, VIRTUALIZATION, 0); 2171fcf5ef2aSThomas Huth } 2172fcf5ef2aSThomas Huth 21737134cb07SRichard Henderson if (cpu_isar_feature(aa64_mte, cpu)) { 21747134cb07SRichard Henderson /* 21757134cb07SRichard Henderson * The architectural range of GM blocksize is 2-6, however qemu 21767134cb07SRichard Henderson * doesn't support blocksize of 2 (see HELPER(ldgm)). 21777134cb07SRichard Henderson */ 21787134cb07SRichard Henderson if (tcg_enabled()) { 21797134cb07SRichard Henderson assert(cpu->gm_blocksize >= 3 && cpu->gm_blocksize <= 6); 21807134cb07SRichard Henderson } 21817134cb07SRichard Henderson 21826f4e1405SRichard Henderson #ifndef CONFIG_USER_ONLY 21836f4e1405SRichard Henderson /* 2184cd305b5fSRichard Henderson * If we do not have tag-memory provided by the machine, 2185cd305b5fSRichard Henderson * reduce MTE support to instructions enabled at EL0. 2186cd305b5fSRichard Henderson * This matches Cortex-A710 BROADCASTMTE input being LOW. 21876f4e1405SRichard Henderson */ 21887134cb07SRichard Henderson if (cpu->tag_memory == NULL) { 21896f4e1405SRichard Henderson cpu->isar.id_aa64pfr1 = 2190cd305b5fSRichard Henderson FIELD_DP64(cpu->isar.id_aa64pfr1, ID_AA64PFR1, MTE, 1); 21916f4e1405SRichard Henderson } 21926f4e1405SRichard Henderson #endif 21937134cb07SRichard Henderson } 21946f4e1405SRichard Henderson 21952daf518dSPeter Maydell if (tcg_enabled()) { 21962daf518dSPeter Maydell /* 21977d8c283eSPeter Maydell * Don't report some architectural features in the ID registers 21987d8c283eSPeter Maydell * where TCG does not yet implement it (not even a minimal 21997d8c283eSPeter Maydell * stub version). This avoids guests falling over when they 22007d8c283eSPeter Maydell * try to access the non-existent system registers for them. 22012daf518dSPeter Maydell */ 22027d8c283eSPeter Maydell /* FEAT_SPE (Statistical Profiling Extension) */ 22032daf518dSPeter Maydell cpu->isar.id_aa64dfr0 = 22042daf518dSPeter Maydell FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMSVER, 0); 22053d5f45ecSRichard Henderson /* FEAT_TRBE (Trace Buffer Extension) */ 22063d5f45ecSRichard Henderson cpu->isar.id_aa64dfr0 = 22073d5f45ecSRichard Henderson FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEBUFFER, 0); 22087d8c283eSPeter Maydell /* FEAT_TRF (Self-hosted Trace Extension) */ 22097d8c283eSPeter Maydell cpu->isar.id_aa64dfr0 = 22107d8c283eSPeter Maydell FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEFILT, 0); 22117d8c283eSPeter Maydell cpu->isar.id_dfr0 = 22127d8c283eSPeter Maydell FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, TRACEFILT, 0); 22137d8c283eSPeter Maydell /* Trace Macrocell system register access */ 22147d8c283eSPeter Maydell cpu->isar.id_aa64dfr0 = 22157d8c283eSPeter Maydell FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEVER, 0); 22167d8c283eSPeter Maydell cpu->isar.id_dfr0 = 22177d8c283eSPeter Maydell FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, COPTRC, 0); 22187d8c283eSPeter Maydell /* Memory mapped trace */ 22197d8c283eSPeter Maydell cpu->isar.id_dfr0 = 22207d8c283eSPeter Maydell FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, MMAPTRC, 0); 22217d8c283eSPeter Maydell /* FEAT_AMU (Activity Monitors Extension) */ 22227d8c283eSPeter Maydell cpu->isar.id_aa64pfr0 = 22237d8c283eSPeter Maydell FIELD_DP64(cpu->isar.id_aa64pfr0, ID_AA64PFR0, AMU, 0); 22247d8c283eSPeter Maydell cpu->isar.id_pfr0 = 22257d8c283eSPeter Maydell FIELD_DP32(cpu->isar.id_pfr0, ID_PFR0, AMU, 0); 22267d8c283eSPeter Maydell /* FEAT_MPAM (Memory Partitioning and Monitoring Extension) */ 22277d8c283eSPeter Maydell cpu->isar.id_aa64pfr0 = 22287d8c283eSPeter Maydell FIELD_DP64(cpu->isar.id_aa64pfr0, ID_AA64PFR0, MPAM, 0); 22297d8c283eSPeter Maydell /* FEAT_NV (Nested Virtualization) */ 22307d8c283eSPeter Maydell cpu->isar.id_aa64mmfr2 = 22317d8c283eSPeter Maydell FIELD_DP64(cpu->isar.id_aa64mmfr2, ID_AA64MMFR2, NV, 0); 22322daf518dSPeter Maydell } 22332daf518dSPeter Maydell 2234f50cd314SPeter Maydell /* MPU can be configured out of a PMSA CPU either by setting has-mpu 2235f50cd314SPeter Maydell * to false or by setting pmsav7-dregion to 0. 2236f50cd314SPeter Maydell */ 2237761c4642STobias Röhmel if (!cpu->has_mpu || cpu->pmsav7_dregion == 0) { 2238f50cd314SPeter Maydell cpu->has_mpu = false; 2239761c4642STobias Röhmel cpu->pmsav7_dregion = 0; 2240761c4642STobias Röhmel cpu->pmsav8r_hdregion = 0; 2241fcf5ef2aSThomas Huth } 2242fcf5ef2aSThomas Huth 2243452a0955SPeter Maydell if (arm_feature(env, ARM_FEATURE_PMSA) && 2244fcf5ef2aSThomas Huth arm_feature(env, ARM_FEATURE_V7)) { 2245fcf5ef2aSThomas Huth uint32_t nr = cpu->pmsav7_dregion; 2246fcf5ef2aSThomas Huth 2247fcf5ef2aSThomas Huth if (nr > 0xff) { 2248fcf5ef2aSThomas Huth error_setg(errp, "PMSAv7 MPU #regions invalid %" PRIu32, nr); 2249fcf5ef2aSThomas Huth return; 2250fcf5ef2aSThomas Huth } 2251fcf5ef2aSThomas Huth 2252fcf5ef2aSThomas Huth if (nr) { 22530e1a46bbSPeter Maydell if (arm_feature(env, ARM_FEATURE_V8)) { 22540e1a46bbSPeter Maydell /* PMSAv8 */ 225562c58ee0SPeter Maydell env->pmsav8.rbar[M_REG_NS] = g_new0(uint32_t, nr); 225662c58ee0SPeter Maydell env->pmsav8.rlar[M_REG_NS] = g_new0(uint32_t, nr); 225762c58ee0SPeter Maydell if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 225862c58ee0SPeter Maydell env->pmsav8.rbar[M_REG_S] = g_new0(uint32_t, nr); 225962c58ee0SPeter Maydell env->pmsav8.rlar[M_REG_S] = g_new0(uint32_t, nr); 226062c58ee0SPeter Maydell } 22610e1a46bbSPeter Maydell } else { 2262fcf5ef2aSThomas Huth env->pmsav7.drbar = g_new0(uint32_t, nr); 2263fcf5ef2aSThomas Huth env->pmsav7.drsr = g_new0(uint32_t, nr); 2264fcf5ef2aSThomas Huth env->pmsav7.dracr = g_new0(uint32_t, nr); 2265fcf5ef2aSThomas Huth } 2266fcf5ef2aSThomas Huth } 2267761c4642STobias Röhmel 2268761c4642STobias Röhmel if (cpu->pmsav8r_hdregion > 0xff) { 2269761c4642STobias Röhmel error_setg(errp, "PMSAv8 MPU EL2 #regions invalid %" PRIu32, 2270761c4642STobias Röhmel cpu->pmsav8r_hdregion); 2271761c4642STobias Röhmel return; 2272761c4642STobias Röhmel } 2273761c4642STobias Röhmel 2274761c4642STobias Röhmel if (cpu->pmsav8r_hdregion) { 2275761c4642STobias Röhmel env->pmsav8.hprbar = g_new0(uint32_t, 2276761c4642STobias Röhmel cpu->pmsav8r_hdregion); 2277761c4642STobias Röhmel env->pmsav8.hprlar = g_new0(uint32_t, 2278761c4642STobias Röhmel cpu->pmsav8r_hdregion); 2279761c4642STobias Röhmel } 22800e1a46bbSPeter Maydell } 2281fcf5ef2aSThomas Huth 22829901c576SPeter Maydell if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 22839901c576SPeter Maydell uint32_t nr = cpu->sau_sregion; 22849901c576SPeter Maydell 22859901c576SPeter Maydell if (nr > 0xff) { 22869901c576SPeter Maydell error_setg(errp, "v8M SAU #regions invalid %" PRIu32, nr); 22879901c576SPeter Maydell return; 22889901c576SPeter Maydell } 22899901c576SPeter Maydell 22909901c576SPeter Maydell if (nr) { 22919901c576SPeter Maydell env->sau.rbar = g_new0(uint32_t, nr); 22929901c576SPeter Maydell env->sau.rlar = g_new0(uint32_t, nr); 22939901c576SPeter Maydell } 22949901c576SPeter Maydell } 22959901c576SPeter Maydell 229691db4642SCédric Le Goater if (arm_feature(env, ARM_FEATURE_EL3)) { 229791db4642SCédric Le Goater set_feature(env, ARM_FEATURE_VBAR); 229891db4642SCédric Le Goater } 229991db4642SCédric Le Goater 2300f6fc36deSJean-Philippe Brucker #ifndef CONFIG_USER_ONLY 2301f6fc36deSJean-Philippe Brucker if (tcg_enabled() && cpu_isar_feature(aa64_rme, cpu)) { 2302f6fc36deSJean-Philippe Brucker arm_register_el_change_hook(cpu, >_rme_post_el_change, 0); 2303f6fc36deSJean-Philippe Brucker } 2304f6fc36deSJean-Philippe Brucker #endif 2305f6fc36deSJean-Philippe Brucker 2306fcf5ef2aSThomas Huth register_cp_regs_for_features(cpu); 2307fcf5ef2aSThomas Huth arm_cpu_register_gdb_regs_for_features(cpu); 2308fcf5ef2aSThomas Huth 2309fcf5ef2aSThomas Huth init_cpreg_list(cpu); 2310fcf5ef2aSThomas Huth 2311fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY 2312cc7d44c2SLike Xu MachineState *ms = MACHINE(qdev_get_machine()); 2313cc7d44c2SLike Xu unsigned int smp_cpus = ms->smp.cpus; 23148bce44a2SRichard Henderson bool has_secure = cpu->has_el3 || arm_feature(env, ARM_FEATURE_M_SECURITY); 2315cc7d44c2SLike Xu 23168bce44a2SRichard Henderson /* 23178bce44a2SRichard Henderson * We must set cs->num_ases to the final value before 23188bce44a2SRichard Henderson * the first call to cpu_address_space_init. 23198bce44a2SRichard Henderson */ 23208bce44a2SRichard Henderson if (cpu->tag_memory != NULL) { 23218bce44a2SRichard Henderson cs->num_ases = 3 + has_secure; 23228bce44a2SRichard Henderson } else { 23238bce44a2SRichard Henderson cs->num_ases = 1 + has_secure; 23248bce44a2SRichard Henderson } 23251d2091bcSPeter Maydell 23268bce44a2SRichard Henderson if (has_secure) { 2327fcf5ef2aSThomas Huth if (!cpu->secure_memory) { 2328fcf5ef2aSThomas Huth cpu->secure_memory = cs->memory; 2329fcf5ef2aSThomas Huth } 233080ceb07aSPeter Xu cpu_address_space_init(cs, ARMASIdx_S, "cpu-secure-memory", 233180ceb07aSPeter Xu cpu->secure_memory); 2332fcf5ef2aSThomas Huth } 23338bce44a2SRichard Henderson 23348bce44a2SRichard Henderson if (cpu->tag_memory != NULL) { 23358bce44a2SRichard Henderson cpu_address_space_init(cs, ARMASIdx_TagNS, "cpu-tag-memory", 23368bce44a2SRichard Henderson cpu->tag_memory); 23378bce44a2SRichard Henderson if (has_secure) { 23388bce44a2SRichard Henderson cpu_address_space_init(cs, ARMASIdx_TagS, "cpu-tag-memory", 23398bce44a2SRichard Henderson cpu->secure_tag_memory); 23408bce44a2SRichard Henderson } 23418bce44a2SRichard Henderson } 23428bce44a2SRichard Henderson 234380ceb07aSPeter Xu cpu_address_space_init(cs, ARMASIdx_NS, "cpu-memory", cs->memory); 2344f9a69711SAlistair Francis 2345f9a69711SAlistair Francis /* No core_count specified, default to smp_cpus. */ 2346f9a69711SAlistair Francis if (cpu->core_count == -1) { 2347f9a69711SAlistair Francis cpu->core_count = smp_cpus; 2348f9a69711SAlistair Francis } 2349fcf5ef2aSThomas Huth #endif 2350fcf5ef2aSThomas Huth 2351a4157b80SRichard Henderson if (tcg_enabled()) { 2352a4157b80SRichard Henderson int dcz_blocklen = 4 << cpu->dcz_blocksize; 2353a4157b80SRichard Henderson 2354a4157b80SRichard Henderson /* 2355a4157b80SRichard Henderson * We only support DCZ blocklen that fits on one page. 2356a4157b80SRichard Henderson * 2357a4157b80SRichard Henderson * Architectually this is always true. However TARGET_PAGE_SIZE 2358a4157b80SRichard Henderson * is variable and, for compatibility with -machine virt-2.7, 2359a4157b80SRichard Henderson * is only 1KiB, as an artifact of legacy ARMv5 subpage support. 2360a4157b80SRichard Henderson * But even then, while the largest architectural DCZ blocklen 2361a4157b80SRichard Henderson * is 2KiB, no cpu actually uses such a large blocklen. 2362a4157b80SRichard Henderson */ 2363a4157b80SRichard Henderson assert(dcz_blocklen <= TARGET_PAGE_SIZE); 2364a4157b80SRichard Henderson 2365a4157b80SRichard Henderson /* 2366a4157b80SRichard Henderson * We only support DCZ blocksize >= 2*TAG_GRANULE, which is to say 2367a4157b80SRichard Henderson * both nibbles of each byte storing tag data may be written at once. 2368a4157b80SRichard Henderson * Since TAG_GRANULE is 16, this means that blocklen must be >= 32. 2369a4157b80SRichard Henderson */ 2370a4157b80SRichard Henderson if (cpu_isar_feature(aa64_mte, cpu)) { 2371a4157b80SRichard Henderson assert(dcz_blocklen >= 2 * TAG_GRANULE); 2372a4157b80SRichard Henderson } 2373a4157b80SRichard Henderson } 2374a4157b80SRichard Henderson 2375fcf5ef2aSThomas Huth qemu_init_vcpu(cs); 2376fcf5ef2aSThomas Huth cpu_reset(cs); 2377fcf5ef2aSThomas Huth 2378fcf5ef2aSThomas Huth acc->parent_realize(dev, errp); 2379fcf5ef2aSThomas Huth } 2380fcf5ef2aSThomas Huth 2381fcf5ef2aSThomas Huth static ObjectClass *arm_cpu_class_by_name(const char *cpu_model) 2382fcf5ef2aSThomas Huth { 2383fcf5ef2aSThomas Huth ObjectClass *oc; 2384fcf5ef2aSThomas Huth char *typename; 2385fcf5ef2aSThomas Huth char **cpuname; 2386a0032cc5SPeter Maydell const char *cpunamestr; 2387fcf5ef2aSThomas Huth 2388fcf5ef2aSThomas Huth cpuname = g_strsplit(cpu_model, ",", 1); 2389a0032cc5SPeter Maydell cpunamestr = cpuname[0]; 2390a0032cc5SPeter Maydell #ifdef CONFIG_USER_ONLY 2391a0032cc5SPeter Maydell /* For backwards compatibility usermode emulation allows "-cpu any", 2392a0032cc5SPeter Maydell * which has the same semantics as "-cpu max". 2393a0032cc5SPeter Maydell */ 2394a0032cc5SPeter Maydell if (!strcmp(cpunamestr, "any")) { 2395a0032cc5SPeter Maydell cpunamestr = "max"; 2396a0032cc5SPeter Maydell } 2397a0032cc5SPeter Maydell #endif 2398a0032cc5SPeter Maydell typename = g_strdup_printf(ARM_CPU_TYPE_NAME("%s"), cpunamestr); 2399fcf5ef2aSThomas Huth oc = object_class_by_name(typename); 2400fcf5ef2aSThomas Huth g_strfreev(cpuname); 2401fcf5ef2aSThomas Huth g_free(typename); 2402fcf5ef2aSThomas Huth if (!oc || !object_class_dynamic_cast(oc, TYPE_ARM_CPU) || 2403fcf5ef2aSThomas Huth object_class_is_abstract(oc)) { 2404fcf5ef2aSThomas Huth return NULL; 2405fcf5ef2aSThomas Huth } 2406fcf5ef2aSThomas Huth return oc; 2407fcf5ef2aSThomas Huth } 2408fcf5ef2aSThomas Huth 2409fcf5ef2aSThomas Huth static Property arm_cpu_properties[] = { 2410e544f800SPhilippe Mathieu-Daudé DEFINE_PROP_UINT64("midr", ARMCPU, midr, 0), 2411fcf5ef2aSThomas Huth DEFINE_PROP_UINT64("mp-affinity", ARMCPU, 2412fcf5ef2aSThomas Huth mp_affinity, ARM64_AFFINITY_INVALID), 241315f8b142SIgor Mammedov DEFINE_PROP_INT32("node-id", ARMCPU, node_id, CPU_UNSET_NUMA_NODE_ID), 2414f9a69711SAlistair Francis DEFINE_PROP_INT32("core-count", ARMCPU, core_count, -1), 2415fcf5ef2aSThomas Huth DEFINE_PROP_END_OF_LIST() 2416fcf5ef2aSThomas Huth }; 2417fcf5ef2aSThomas Huth 2418a6506838SAkihiko Odaki static const gchar *arm_gdb_arch_name(CPUState *cs) 2419fcf5ef2aSThomas Huth { 2420fcf5ef2aSThomas Huth ARMCPU *cpu = ARM_CPU(cs); 2421fcf5ef2aSThomas Huth CPUARMState *env = &cpu->env; 2422fcf5ef2aSThomas Huth 2423fcf5ef2aSThomas Huth if (arm_feature(env, ARM_FEATURE_IWMMXT)) { 2424a6506838SAkihiko Odaki return "iwmmxt"; 2425fcf5ef2aSThomas Huth } 2426a6506838SAkihiko Odaki return "arm"; 2427fcf5ef2aSThomas Huth } 2428fcf5ef2aSThomas Huth 24298b80bd28SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY 24308b80bd28SPhilippe Mathieu-Daudé #include "hw/core/sysemu-cpu-ops.h" 24318b80bd28SPhilippe Mathieu-Daudé 24328b80bd28SPhilippe Mathieu-Daudé static const struct SysemuCPUOps arm_sysemu_ops = { 243308928c6dSPhilippe Mathieu-Daudé .get_phys_page_attrs_debug = arm_cpu_get_phys_page_attrs_debug, 2434faf39e82SPhilippe Mathieu-Daudé .asidx_from_attrs = arm_asidx_from_attrs, 2435715e3c1aSPhilippe Mathieu-Daudé .write_elf32_note = arm_cpu_write_elf32_note, 2436715e3c1aSPhilippe Mathieu-Daudé .write_elf64_note = arm_cpu_write_elf64_note, 2437da383e02SPhilippe Mathieu-Daudé .virtio_is_big_endian = arm_cpu_virtio_is_big_endian, 2438feece4d0SPhilippe Mathieu-Daudé .legacy_vmsd = &vmstate_arm_cpu, 24398b80bd28SPhilippe Mathieu-Daudé }; 24408b80bd28SPhilippe Mathieu-Daudé #endif 24418b80bd28SPhilippe Mathieu-Daudé 244278271684SClaudio Fontana #ifdef CONFIG_TCG 244311906557SRichard Henderson static const struct TCGCPUOps arm_tcg_ops = { 244478271684SClaudio Fontana .initialize = arm_translate_init, 244578271684SClaudio Fontana .synchronize_from_tb = arm_cpu_synchronize_from_tb, 244678271684SClaudio Fontana .debug_excp_handler = arm_debug_excp_handler, 244756c6c98dSRichard Henderson .restore_state_to_opc = arm_restore_state_to_opc, 244878271684SClaudio Fontana 24499b12b6b4SRichard Henderson #ifdef CONFIG_USER_ONLY 24509b12b6b4SRichard Henderson .record_sigsegv = arm_cpu_record_sigsegv, 245139a099caSRichard Henderson .record_sigbus = arm_cpu_record_sigbus, 24529b12b6b4SRichard Henderson #else 24539b12b6b4SRichard Henderson .tlb_fill = arm_cpu_tlb_fill, 2454083afd18SPhilippe Mathieu-Daudé .cpu_exec_interrupt = arm_cpu_exec_interrupt, 245578271684SClaudio Fontana .do_interrupt = arm_cpu_do_interrupt, 245678271684SClaudio Fontana .do_transaction_failed = arm_cpu_do_transaction_failed, 245778271684SClaudio Fontana .do_unaligned_access = arm_cpu_do_unaligned_access, 245878271684SClaudio Fontana .adjust_watchpoint_address = arm_adjust_watchpoint_address, 245978271684SClaudio Fontana .debug_check_watchpoint = arm_debug_check_watchpoint, 2460b00d86bcSRichard Henderson .debug_check_breakpoint = arm_debug_check_breakpoint, 246178271684SClaudio Fontana #endif /* !CONFIG_USER_ONLY */ 246278271684SClaudio Fontana }; 246378271684SClaudio Fontana #endif /* CONFIG_TCG */ 246478271684SClaudio Fontana 2465fcf5ef2aSThomas Huth static void arm_cpu_class_init(ObjectClass *oc, void *data) 2466fcf5ef2aSThomas Huth { 2467fcf5ef2aSThomas Huth ARMCPUClass *acc = ARM_CPU_CLASS(oc); 2468fcf5ef2aSThomas Huth CPUClass *cc = CPU_CLASS(acc); 2469fcf5ef2aSThomas Huth DeviceClass *dc = DEVICE_CLASS(oc); 24709130cadeSPeter Maydell ResettableClass *rc = RESETTABLE_CLASS(oc); 2471fcf5ef2aSThomas Huth 2472bf853881SPhilippe Mathieu-Daudé device_class_set_parent_realize(dc, arm_cpu_realizefn, 2473bf853881SPhilippe Mathieu-Daudé &acc->parent_realize); 2474fcf5ef2aSThomas Huth 24754f67d30bSMarc-André Lureau device_class_set_props(dc, arm_cpu_properties); 24769130cadeSPeter Maydell 24779130cadeSPeter Maydell resettable_class_set_parent_phases(rc, NULL, arm_cpu_reset_hold, NULL, 24789130cadeSPeter Maydell &acc->parent_phases); 2479fcf5ef2aSThomas Huth 2480fcf5ef2aSThomas Huth cc->class_by_name = arm_cpu_class_by_name; 2481fcf5ef2aSThomas Huth cc->has_work = arm_cpu_has_work; 2482fcf5ef2aSThomas Huth cc->dump_state = arm_cpu_dump_state; 2483fcf5ef2aSThomas Huth cc->set_pc = arm_cpu_set_pc; 2484e4fdf9dfSRichard Henderson cc->get_pc = arm_cpu_get_pc; 2485fcf5ef2aSThomas Huth cc->gdb_read_register = arm_cpu_gdb_read_register; 2486fcf5ef2aSThomas Huth cc->gdb_write_register = arm_cpu_gdb_write_register; 24877350d553SRichard Henderson #ifndef CONFIG_USER_ONLY 24888b80bd28SPhilippe Mathieu-Daudé cc->sysemu_ops = &arm_sysemu_ops; 2489fcf5ef2aSThomas Huth #endif 2490fcf5ef2aSThomas Huth cc->gdb_num_core_regs = 26; 2491fcf5ef2aSThomas Huth cc->gdb_arch_name = arm_gdb_arch_name; 2492200bf5b7SAbdallah Bouassida cc->gdb_get_dynamic_xml = arm_gdb_get_dynamic_xml; 2493fcf5ef2aSThomas Huth cc->gdb_stop_before_watchpoint = true; 2494fcf5ef2aSThomas Huth cc->disas_set_info = arm_disas_set_info; 249578271684SClaudio Fontana 249674d7fc7fSRichard Henderson #ifdef CONFIG_TCG 249778271684SClaudio Fontana cc->tcg_ops = &arm_tcg_ops; 2498cbc183d2SClaudio Fontana #endif /* CONFIG_TCG */ 2499fcf5ef2aSThomas Huth } 2500fcf5ef2aSThomas Huth 250151e5ef45SMarc-André Lureau static void arm_cpu_instance_init(Object *obj) 250251e5ef45SMarc-André Lureau { 250351e5ef45SMarc-André Lureau ARMCPUClass *acc = ARM_CPU_GET_CLASS(obj); 250451e5ef45SMarc-André Lureau 250551e5ef45SMarc-André Lureau acc->info->initfn(obj); 250651e5ef45SMarc-André Lureau arm_cpu_post_init(obj); 250751e5ef45SMarc-André Lureau } 250851e5ef45SMarc-André Lureau 250951e5ef45SMarc-André Lureau static void cpu_register_class_init(ObjectClass *oc, void *data) 251051e5ef45SMarc-André Lureau { 251151e5ef45SMarc-André Lureau ARMCPUClass *acc = ARM_CPU_CLASS(oc); 251248de6462SAkihiko Odaki CPUClass *cc = CPU_CLASS(acc); 251351e5ef45SMarc-André Lureau 251451e5ef45SMarc-André Lureau acc->info = data; 251548de6462SAkihiko Odaki cc->gdb_core_xml_file = "arm-core.xml"; 251651e5ef45SMarc-André Lureau } 251751e5ef45SMarc-André Lureau 251837bcf244SThomas Huth void arm_cpu_register(const ARMCPUInfo *info) 2519fcf5ef2aSThomas Huth { 2520fcf5ef2aSThomas Huth TypeInfo type_info = { 2521fcf5ef2aSThomas Huth .parent = TYPE_ARM_CPU, 252251e5ef45SMarc-André Lureau .instance_init = arm_cpu_instance_init, 252351e5ef45SMarc-André Lureau .class_init = info->class_init ?: cpu_register_class_init, 252451e5ef45SMarc-André Lureau .class_data = (void *)info, 2525fcf5ef2aSThomas Huth }; 2526fcf5ef2aSThomas Huth 2527fcf5ef2aSThomas Huth type_info.name = g_strdup_printf("%s-" TYPE_ARM_CPU, info->name); 2528fcf5ef2aSThomas Huth type_register(&type_info); 2529fcf5ef2aSThomas Huth g_free((void *)type_info.name); 2530fcf5ef2aSThomas Huth } 2531fcf5ef2aSThomas Huth 2532fcf5ef2aSThomas Huth static const TypeInfo arm_cpu_type_info = { 2533fcf5ef2aSThomas Huth .name = TYPE_ARM_CPU, 2534fcf5ef2aSThomas Huth .parent = TYPE_CPU, 2535fcf5ef2aSThomas Huth .instance_size = sizeof(ARMCPU), 2536d03087bdSRichard Henderson .instance_align = __alignof__(ARMCPU), 2537fcf5ef2aSThomas Huth .instance_init = arm_cpu_initfn, 2538fcf5ef2aSThomas Huth .instance_finalize = arm_cpu_finalizefn, 2539fcf5ef2aSThomas Huth .abstract = true, 2540fcf5ef2aSThomas Huth .class_size = sizeof(ARMCPUClass), 2541fcf5ef2aSThomas Huth .class_init = arm_cpu_class_init, 2542fcf5ef2aSThomas Huth }; 2543fcf5ef2aSThomas Huth 2544fcf5ef2aSThomas Huth static void arm_cpu_register_types(void) 2545fcf5ef2aSThomas Huth { 2546fcf5ef2aSThomas Huth type_register_static(&arm_cpu_type_info); 2547fcf5ef2aSThomas Huth } 2548fcf5ef2aSThomas Huth 2549fcf5ef2aSThomas Huth type_init(arm_cpu_register_types) 2550