xref: /openbmc/qemu/target/arm/cpu.c (revision f15f7273ea55472d5904c53566c82369d81214c1)
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"
345a534314SPeter 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"
51d780d056SPhilippe Mathieu-Daudé #include "target/arm/cpu-qom.h"
52f4f318b4SPhilippe Mathieu-Daudé #include "target/arm/gtimer.h"
53fcf5ef2aSThomas Huth 
arm_cpu_set_pc(CPUState * cs,vaddr value)54fcf5ef2aSThomas Huth static void arm_cpu_set_pc(CPUState *cs, vaddr value)
55fcf5ef2aSThomas Huth {
56fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(cs);
5742f6ed91SJulia Suvorova     CPUARMState *env = &cpu->env;
58fcf5ef2aSThomas Huth 
5942f6ed91SJulia Suvorova     if (is_a64(env)) {
6042f6ed91SJulia Suvorova         env->pc = value;
61063bbd80SRichard Henderson         env->thumb = false;
6242f6ed91SJulia Suvorova     } else {
6342f6ed91SJulia Suvorova         env->regs[15] = value & ~1;
6442f6ed91SJulia Suvorova         env->thumb = value & 1;
6542f6ed91SJulia Suvorova     }
6642f6ed91SJulia Suvorova }
6742f6ed91SJulia Suvorova 
arm_cpu_get_pc(CPUState * cs)68e4fdf9dfSRichard Henderson static vaddr arm_cpu_get_pc(CPUState *cs)
69e4fdf9dfSRichard Henderson {
70e4fdf9dfSRichard Henderson     ARMCPU *cpu = ARM_CPU(cs);
71e4fdf9dfSRichard Henderson     CPUARMState *env = &cpu->env;
72e4fdf9dfSRichard Henderson 
73e4fdf9dfSRichard Henderson     if (is_a64(env)) {
74e4fdf9dfSRichard Henderson         return env->pc;
75e4fdf9dfSRichard Henderson     } else {
76e4fdf9dfSRichard Henderson         return env->regs[15];
77e4fdf9dfSRichard Henderson     }
78e4fdf9dfSRichard Henderson }
79e4fdf9dfSRichard Henderson 
80ec62595bSEduardo Habkost #ifdef CONFIG_TCG
arm_cpu_synchronize_from_tb(CPUState * cs,const TranslationBlock * tb)8178271684SClaudio Fontana void arm_cpu_synchronize_from_tb(CPUState *cs,
8204a37d4cSRichard Henderson                                  const TranslationBlock *tb)
8342f6ed91SJulia Suvorova {
8403a648c4SAnton Johansson     /* The program counter is always up to date with CF_PCREL. */
8503a648c4SAnton Johansson     if (!(tb_cflags(tb) & CF_PCREL)) {
86b77af26eSRichard Henderson         CPUARMState *env = cpu_env(cs);
8742f6ed91SJulia Suvorova         /*
8842f6ed91SJulia Suvorova          * It's OK to look at env for the current mode here, because it's
8942f6ed91SJulia Suvorova          * never possible for an AArch64 TB to chain to an AArch32 TB.
9042f6ed91SJulia Suvorova          */
9142f6ed91SJulia Suvorova         if (is_a64(env)) {
92f51a1dd7SAnton Johansson             env->pc = tb->pc;
9342f6ed91SJulia Suvorova         } else {
94f51a1dd7SAnton Johansson             env->regs[15] = tb->pc;
9542f6ed91SJulia Suvorova         }
96fcf5ef2aSThomas Huth     }
97abb80995SRichard Henderson }
9856c6c98dSRichard Henderson 
arm_restore_state_to_opc(CPUState * cs,const TranslationBlock * tb,const uint64_t * data)99475e56b6SEvgeny Ermakov void arm_restore_state_to_opc(CPUState *cs,
10056c6c98dSRichard Henderson                               const TranslationBlock *tb,
10156c6c98dSRichard Henderson                               const uint64_t *data)
10256c6c98dSRichard Henderson {
103b77af26eSRichard Henderson     CPUARMState *env = cpu_env(cs);
10456c6c98dSRichard Henderson 
10556c6c98dSRichard Henderson     if (is_a64(env)) {
10603a648c4SAnton Johansson         if (tb_cflags(tb) & CF_PCREL) {
10756c6c98dSRichard Henderson             env->pc = (env->pc & TARGET_PAGE_MASK) | data[0];
10856c6c98dSRichard Henderson         } else {
10956c6c98dSRichard Henderson             env->pc = data[0];
11056c6c98dSRichard Henderson         }
11156c6c98dSRichard Henderson         env->condexec_bits = 0;
11256c6c98dSRichard Henderson         env->exception.syndrome = data[2] << ARM_INSN_START_WORD2_SHIFT;
11356c6c98dSRichard Henderson     } else {
11403a648c4SAnton Johansson         if (tb_cflags(tb) & CF_PCREL) {
11556c6c98dSRichard Henderson             env->regs[15] = (env->regs[15] & TARGET_PAGE_MASK) | data[0];
11656c6c98dSRichard Henderson         } else {
11756c6c98dSRichard Henderson             env->regs[15] = data[0];
11856c6c98dSRichard Henderson         }
11956c6c98dSRichard Henderson         env->condexec_bits = data[1];
12056c6c98dSRichard Henderson         env->exception.syndrome = data[2] << ARM_INSN_START_WORD2_SHIFT;
12156c6c98dSRichard Henderson     }
12256c6c98dSRichard Henderson }
123ec62595bSEduardo Habkost #endif /* CONFIG_TCG */
124fcf5ef2aSThomas Huth 
125b36a32eaSJinjie Ruan /*
126b36a32eaSJinjie Ruan  * With SCTLR_ELx.NMI == 0, IRQ with Superpriority is masked identically with
127b36a32eaSJinjie Ruan  * IRQ without Superpriority. Moreover, if the GIC is configured so that
128b36a32eaSJinjie Ruan  * FEAT_GICv3_NMI is only set if FEAT_NMI is set, then we won't ever see
129b36a32eaSJinjie Ruan  * CPU_INTERRUPT_*NMI anyway. So we might as well accept NMI here
130b36a32eaSJinjie Ruan  * unconditionally.
131b36a32eaSJinjie Ruan  */
arm_cpu_has_work(CPUState * cs)132fcf5ef2aSThomas Huth static bool arm_cpu_has_work(CPUState *cs)
133fcf5ef2aSThomas Huth {
134fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(cs);
135fcf5ef2aSThomas Huth 
136062ba099SAlex Bennée     return (cpu->power_state != PSCI_OFF)
137fcf5ef2aSThomas Huth         && cs->interrupt_request &
138fcf5ef2aSThomas Huth         (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD
139b36a32eaSJinjie Ruan          | CPU_INTERRUPT_NMI | CPU_INTERRUPT_VINMI | CPU_INTERRUPT_VFNMI
1403c29632fSRichard Henderson          | CPU_INTERRUPT_VFIQ | CPU_INTERRUPT_VIRQ | CPU_INTERRUPT_VSERR
141fcf5ef2aSThomas Huth          | CPU_INTERRUPT_EXITTB);
142fcf5ef2aSThomas Huth }
143fcf5ef2aSThomas Huth 
arm_cpu_mmu_index(CPUState * cs,bool ifetch)144628421c7SRichard Henderson static int arm_cpu_mmu_index(CPUState *cs, bool ifetch)
145628421c7SRichard Henderson {
146628421c7SRichard Henderson     return arm_env_mmu_index(cpu_env(cs));
147628421c7SRichard Henderson }
148628421c7SRichard Henderson 
arm_register_pre_el_change_hook(ARMCPU * cpu,ARMELChangeHookFn * hook,void * opaque)149b5c53d1bSAaron Lindsay void arm_register_pre_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook,
150b5c53d1bSAaron Lindsay                                  void *opaque)
151b5c53d1bSAaron Lindsay {
152b5c53d1bSAaron Lindsay     ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1);
153b5c53d1bSAaron Lindsay 
154b5c53d1bSAaron Lindsay     entry->hook = hook;
155b5c53d1bSAaron Lindsay     entry->opaque = opaque;
156b5c53d1bSAaron Lindsay 
157b5c53d1bSAaron Lindsay     QLIST_INSERT_HEAD(&cpu->pre_el_change_hooks, entry, node);
158b5c53d1bSAaron Lindsay }
159b5c53d1bSAaron Lindsay 
arm_register_el_change_hook(ARMCPU * cpu,ARMELChangeHookFn * hook,void * opaque)16008267487SAaron Lindsay void arm_register_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook,
161fcf5ef2aSThomas Huth                                  void *opaque)
162fcf5ef2aSThomas Huth {
16308267487SAaron Lindsay     ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1);
16408267487SAaron Lindsay 
16508267487SAaron Lindsay     entry->hook = hook;
16608267487SAaron Lindsay     entry->opaque = opaque;
16708267487SAaron Lindsay 
16808267487SAaron Lindsay     QLIST_INSERT_HEAD(&cpu->el_change_hooks, entry, node);
169fcf5ef2aSThomas Huth }
170fcf5ef2aSThomas Huth 
171*d1ff9967SPeter Maydell /*
172*d1ff9967SPeter Maydell  * Set the float_status behaviour to match the Arm defaults:
173*d1ff9967SPeter Maydell  *  * tininess-before-rounding
174*d1ff9967SPeter Maydell  *  * 2-input NaN propagation prefers SNaN over QNaN, and then
175*d1ff9967SPeter Maydell  *    operand A over operand B (see FPProcessNaNs() pseudocode)
176*d1ff9967SPeter Maydell  */
arm_set_default_fp_behaviours(float_status * s)177*d1ff9967SPeter Maydell static void arm_set_default_fp_behaviours(float_status *s)
178*d1ff9967SPeter Maydell {
179*d1ff9967SPeter Maydell     set_float_detect_tininess(float_tininess_before_rounding, s);
180*d1ff9967SPeter Maydell     set_float_2nan_prop_rule(float_2nan_prop_s_ab, s);
181*d1ff9967SPeter Maydell }
182*d1ff9967SPeter Maydell 
cp_reg_reset(gpointer key,gpointer value,gpointer opaque)183fcf5ef2aSThomas Huth static void cp_reg_reset(gpointer key, gpointer value, gpointer opaque)
184fcf5ef2aSThomas Huth {
185fcf5ef2aSThomas Huth     /* Reset a single ARMCPRegInfo register */
186fcf5ef2aSThomas Huth     ARMCPRegInfo *ri = value;
187fcf5ef2aSThomas Huth     ARMCPU *cpu = opaque;
188fcf5ef2aSThomas Huth 
18987c3f0f2SRichard Henderson     if (ri->type & (ARM_CP_SPECIAL_MASK | ARM_CP_ALIAS)) {
190fcf5ef2aSThomas Huth         return;
191fcf5ef2aSThomas Huth     }
192fcf5ef2aSThomas Huth 
193fcf5ef2aSThomas Huth     if (ri->resetfn) {
194fcf5ef2aSThomas Huth         ri->resetfn(&cpu->env, ri);
195fcf5ef2aSThomas Huth         return;
196fcf5ef2aSThomas Huth     }
197fcf5ef2aSThomas Huth 
198fcf5ef2aSThomas Huth     /* A zero offset is never possible as it would be regs[0]
199fcf5ef2aSThomas Huth      * so we use it to indicate that reset is being handled elsewhere.
200fcf5ef2aSThomas Huth      * This is basically only used for fields in non-core coprocessors
201fcf5ef2aSThomas Huth      * (like the pxa2xx ones).
202fcf5ef2aSThomas Huth      */
203fcf5ef2aSThomas Huth     if (!ri->fieldoffset) {
204fcf5ef2aSThomas Huth         return;
205fcf5ef2aSThomas Huth     }
206fcf5ef2aSThomas Huth 
207fcf5ef2aSThomas Huth     if (cpreg_field_is_64bit(ri)) {
208fcf5ef2aSThomas Huth         CPREG_FIELD64(&cpu->env, ri) = ri->resetvalue;
209fcf5ef2aSThomas Huth     } else {
210fcf5ef2aSThomas Huth         CPREG_FIELD32(&cpu->env, ri) = ri->resetvalue;
211fcf5ef2aSThomas Huth     }
212fcf5ef2aSThomas Huth }
213fcf5ef2aSThomas Huth 
cp_reg_check_reset(gpointer key,gpointer value,gpointer opaque)214fcf5ef2aSThomas Huth static void cp_reg_check_reset(gpointer key, gpointer value,  gpointer opaque)
215fcf5ef2aSThomas Huth {
216fcf5ef2aSThomas Huth     /* Purely an assertion check: we've already done reset once,
217fcf5ef2aSThomas Huth      * so now check that running the reset for the cpreg doesn't
218fcf5ef2aSThomas Huth      * change its value. This traps bugs where two different cpregs
219fcf5ef2aSThomas Huth      * both try to reset the same state field but to different values.
220fcf5ef2aSThomas Huth      */
221fcf5ef2aSThomas Huth     ARMCPRegInfo *ri = value;
222fcf5ef2aSThomas Huth     ARMCPU *cpu = opaque;
223fcf5ef2aSThomas Huth     uint64_t oldvalue, newvalue;
224fcf5ef2aSThomas Huth 
22587c3f0f2SRichard Henderson     if (ri->type & (ARM_CP_SPECIAL_MASK | ARM_CP_ALIAS | ARM_CP_NO_RAW)) {
226fcf5ef2aSThomas Huth         return;
227fcf5ef2aSThomas Huth     }
228fcf5ef2aSThomas Huth 
229fcf5ef2aSThomas Huth     oldvalue = read_raw_cp_reg(&cpu->env, ri);
230fcf5ef2aSThomas Huth     cp_reg_reset(key, value, opaque);
231fcf5ef2aSThomas Huth     newvalue = read_raw_cp_reg(&cpu->env, ri);
232fcf5ef2aSThomas Huth     assert(oldvalue == newvalue);
233fcf5ef2aSThomas Huth }
234fcf5ef2aSThomas Huth 
arm_cpu_reset_hold(Object * obj,ResetType type)235ad80e367SPeter Maydell static void arm_cpu_reset_hold(Object *obj, ResetType type)
236fcf5ef2aSThomas Huth {
237348802b5SPhilippe Mathieu-Daudé     CPUState *cs = CPU(obj);
238348802b5SPhilippe Mathieu-Daudé     ARMCPU *cpu = ARM_CPU(cs);
239348802b5SPhilippe Mathieu-Daudé     ARMCPUClass *acc = ARM_CPU_GET_CLASS(obj);
240fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
241fcf5ef2aSThomas Huth 
2429130cadeSPeter Maydell     if (acc->parent_phases.hold) {
243ad80e367SPeter Maydell         acc->parent_phases.hold(obj, type);
2449130cadeSPeter Maydell     }
245fcf5ef2aSThomas Huth 
2461f5c00cfSAlex Bennée     memset(env, 0, offsetof(CPUARMState, end_reset_fields));
2471f5c00cfSAlex Bennée 
248fcf5ef2aSThomas Huth     g_hash_table_foreach(cpu->cp_regs, cp_reg_reset, cpu);
249fcf5ef2aSThomas Huth     g_hash_table_foreach(cpu->cp_regs, cp_reg_check_reset, cpu);
250fcf5ef2aSThomas Huth 
251fcf5ef2aSThomas Huth     env->vfp.xregs[ARM_VFP_FPSID] = cpu->reset_fpsid;
25247576b94SRichard Henderson     env->vfp.xregs[ARM_VFP_MVFR0] = cpu->isar.mvfr0;
25347576b94SRichard Henderson     env->vfp.xregs[ARM_VFP_MVFR1] = cpu->isar.mvfr1;
25447576b94SRichard Henderson     env->vfp.xregs[ARM_VFP_MVFR2] = cpu->isar.mvfr2;
255fcf5ef2aSThomas Huth 
256348802b5SPhilippe Mathieu-Daudé     cpu->power_state = cs->start_powered_off ? PSCI_OFF : PSCI_ON;
257fcf5ef2aSThomas Huth 
258fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
259fcf5ef2aSThomas Huth         env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
260fcf5ef2aSThomas Huth     }
261fcf5ef2aSThomas Huth 
262fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
263fcf5ef2aSThomas Huth         /* 64 bit CPUs always start in 64 bit mode */
26453221552SRichard Henderson         env->aarch64 = true;
265fcf5ef2aSThomas Huth #if defined(CONFIG_USER_ONLY)
266fcf5ef2aSThomas Huth         env->pstate = PSTATE_MODE_EL0t;
267fcf5ef2aSThomas Huth         /* Userspace expects access to DC ZVA, CTL_EL0 and the cache ops */
268fcf5ef2aSThomas Huth         env->cp15.sctlr_el[1] |= SCTLR_UCT | SCTLR_UCI | SCTLR_DZE;
269276c6e81SRichard Henderson         /* Enable all PAC keys.  */
270276c6e81SRichard Henderson         env->cp15.sctlr_el[1] |= (SCTLR_EnIA | SCTLR_EnIB |
271276c6e81SRichard Henderson                                   SCTLR_EnDA | SCTLR_EnDB);
272cda86e2bSRichard Henderson         /* Trap on btype=3 for PACIxSP. */
273cda86e2bSRichard Henderson         env->cp15.sctlr_el[1] |= SCTLR_BT0;
274d03396a8SRichard Henderson         /* Trap on implementation defined registers. */
275d03396a8SRichard Henderson         if (cpu_isar_feature(aa64_tidcp1, cpu)) {
276d03396a8SRichard Henderson             env->cp15.sctlr_el[1] |= SCTLR_TIDCP;
277d03396a8SRichard Henderson         }
278fcf5ef2aSThomas Huth         /* and to the FP/Neon instructions */
279fab8ad39SRichard Henderson         env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
280fab8ad39SRichard Henderson                                          CPACR_EL1, FPEN, 3);
28146303535SRichard Henderson         /* and to the SVE instructions, with default vector length */
28246303535SRichard Henderson         if (cpu_isar_feature(aa64_sve, cpu)) {
283fab8ad39SRichard Henderson             env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
284fab8ad39SRichard Henderson                                              CPACR_EL1, ZEN, 3);
28587252bdeSRichard Henderson             env->vfp.zcr_el[1] = cpu->sve_default_vq - 1;
2867b6a2198SAlex Bennée         }
28778011586SRichard Henderson         /* and for SME instructions, with default vector length, and TPIDR2 */
28878011586SRichard Henderson         if (cpu_isar_feature(aa64_sme, cpu)) {
28978011586SRichard Henderson             env->cp15.sctlr_el[1] |= SCTLR_EnTP2;
29078011586SRichard Henderson             env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
29178011586SRichard Henderson                                              CPACR_EL1, SMEN, 3);
29278011586SRichard Henderson             env->vfp.smcr_el[1] = cpu->sme_default_vq - 1;
29378011586SRichard Henderson             if (cpu_isar_feature(aa64_sme_fa64, cpu)) {
29478011586SRichard Henderson                 env->vfp.smcr_el[1] = FIELD_DP64(env->vfp.smcr_el[1],
29578011586SRichard Henderson                                                  SMCR, FA64, 1);
29678011586SRichard Henderson             }
29778011586SRichard Henderson         }
298f6a148feSRichard Henderson         /*
299691f1ffdSRichard Henderson          * Enable 48-bit address space (TODO: take reserved_va into account).
30016c84978SRichard Henderson          * Enable TBI0 but not TBI1.
30116c84978SRichard Henderson          * Note that this must match useronly_clean_ptr.
302f6a148feSRichard Henderson          */
303cb4a0a34SPeter Maydell         env->cp15.tcr_el[1] = 5 | (1ULL << 37);
304e3232864SRichard Henderson 
305e3232864SRichard Henderson         /* Enable MTE */
306e3232864SRichard Henderson         if (cpu_isar_feature(aa64_mte, cpu)) {
307e3232864SRichard Henderson             /* Enable tag access, but leave TCF0 as No Effect (0). */
308e3232864SRichard Henderson             env->cp15.sctlr_el[1] |= SCTLR_ATA0;
309e3232864SRichard Henderson             /*
310e3232864SRichard Henderson              * Exclude all tags, so that tag 0 is always used.
311e3232864SRichard Henderson              * This corresponds to Linux current->thread.gcr_incl = 0.
312e3232864SRichard Henderson              *
313e3232864SRichard Henderson              * Set RRND, so that helper_irg() will generate a seed later.
314e3232864SRichard Henderson              * Here in cpu_reset(), the crypto subsystem has not yet been
315e3232864SRichard Henderson              * initialized.
316e3232864SRichard Henderson              */
317e3232864SRichard Henderson             env->cp15.gcr_el1 = 0x1ffff;
318e3232864SRichard Henderson         }
3197cb1e618SRichard Henderson         /*
3207cb1e618SRichard Henderson          * Disable access to SCXTNUM_EL0 from CSV2_1p2.
3217cb1e618SRichard Henderson          * This is not yet exposed from the Linux kernel in any way.
3227cb1e618SRichard Henderson          */
3237cb1e618SRichard Henderson         env->cp15.sctlr_el[1] |= SCTLR_TSCXT;
324f9ac7788SZhuojia Shen         /* Disable access to Debug Communication Channel (DCC). */
325f9ac7788SZhuojia Shen         env->cp15.mdscr_el1 |= 1 << 12;
326a6b2c5a0SPeter Maydell         /* Enable FEAT_MOPS */
327a6b2c5a0SPeter Maydell         env->cp15.sctlr_el[1] |= SCTLR_MSCEN;
328fcf5ef2aSThomas Huth #else
329fcf5ef2aSThomas Huth         /* Reset into the highest available EL */
330fcf5ef2aSThomas Huth         if (arm_feature(env, ARM_FEATURE_EL3)) {
331fcf5ef2aSThomas Huth             env->pstate = PSTATE_MODE_EL3h;
332fcf5ef2aSThomas Huth         } else if (arm_feature(env, ARM_FEATURE_EL2)) {
333fcf5ef2aSThomas Huth             env->pstate = PSTATE_MODE_EL2h;
334fcf5ef2aSThomas Huth         } else {
335fcf5ef2aSThomas Huth             env->pstate = PSTATE_MODE_EL1h;
336fcf5ef2aSThomas Huth         }
3374a7319b7SEdgar E. Iglesias 
3384a7319b7SEdgar E. Iglesias         /* Sample rvbar at reset.  */
3394a7319b7SEdgar E. Iglesias         env->cp15.rvbar = cpu->rvbar_prop;
3404a7319b7SEdgar E. Iglesias         env->pc = env->cp15.rvbar;
341fcf5ef2aSThomas Huth #endif
342fcf5ef2aSThomas Huth     } else {
343fcf5ef2aSThomas Huth #if defined(CONFIG_USER_ONLY)
344fcf5ef2aSThomas Huth         /* Userspace expects access to cp10 and cp11 for FP/Neon */
345fab8ad39SRichard Henderson         env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
346fab8ad39SRichard Henderson                                          CPACR, CP10, 3);
347fab8ad39SRichard Henderson         env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
348fab8ad39SRichard Henderson                                          CPACR, CP11, 3);
349fcf5ef2aSThomas Huth #endif
350910e4f24STobias Röhmel         if (arm_feature(env, ARM_FEATURE_V8)) {
351910e4f24STobias Röhmel             env->cp15.rvbar = cpu->rvbar_prop;
352910e4f24STobias Röhmel             env->regs[15] = cpu->rvbar_prop;
353910e4f24STobias Röhmel         }
354fcf5ef2aSThomas Huth     }
355fcf5ef2aSThomas Huth 
356fcf5ef2aSThomas Huth #if defined(CONFIG_USER_ONLY)
357fcf5ef2aSThomas Huth     env->uncached_cpsr = ARM_CPU_MODE_USR;
358fcf5ef2aSThomas Huth     /* For user mode we must enable access to coprocessors */
359fcf5ef2aSThomas Huth     env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
360fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
361fcf5ef2aSThomas Huth         env->cp15.c15_cpar = 3;
362fcf5ef2aSThomas Huth     } else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
363fcf5ef2aSThomas Huth         env->cp15.c15_cpar = 1;
364fcf5ef2aSThomas Huth     }
365fcf5ef2aSThomas Huth #else
366060a65dfSPeter Maydell 
367060a65dfSPeter Maydell     /*
368060a65dfSPeter Maydell      * If the highest available EL is EL2, AArch32 will start in Hyp
369060a65dfSPeter Maydell      * mode; otherwise it starts in SVC. Note that if we start in
370060a65dfSPeter Maydell      * AArch64 then these values in the uncached_cpsr will be ignored.
371060a65dfSPeter Maydell      */
372060a65dfSPeter Maydell     if (arm_feature(env, ARM_FEATURE_EL2) &&
373060a65dfSPeter Maydell         !arm_feature(env, ARM_FEATURE_EL3)) {
374060a65dfSPeter Maydell         env->uncached_cpsr = ARM_CPU_MODE_HYP;
375060a65dfSPeter Maydell     } else {
376fcf5ef2aSThomas Huth         env->uncached_cpsr = ARM_CPU_MODE_SVC;
377060a65dfSPeter Maydell     }
378fcf5ef2aSThomas Huth     env->daif = PSTATE_D | PSTATE_A | PSTATE_I | PSTATE_F;
3791426f244SPeter Maydell 
3801426f244SPeter Maydell     /* AArch32 has a hard highvec setting of 0xFFFF0000.  If we are currently
3811426f244SPeter Maydell      * executing as AArch32 then check if highvecs are enabled and
3821426f244SPeter Maydell      * adjust the PC accordingly.
3831426f244SPeter Maydell      */
3841426f244SPeter Maydell     if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
3851426f244SPeter Maydell         env->regs[15] = 0xFFFF0000;
3861426f244SPeter Maydell     }
3871426f244SPeter Maydell 
3881426f244SPeter Maydell     env->vfp.xregs[ARM_VFP_FPEXC] = 0;
389b62ceeafSPeter Maydell #endif
390dc7abe4dSMichael Davidsaver 
391531c60a9SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M)) {
392b62ceeafSPeter Maydell #ifndef CONFIG_USER_ONLY
393fcf5ef2aSThomas Huth         uint32_t initial_msp; /* Loaded from 0x0 */
394fcf5ef2aSThomas Huth         uint32_t initial_pc; /* Loaded from 0x4 */
395fcf5ef2aSThomas Huth         uint8_t *rom;
39638e2a77cSPeter Maydell         uint32_t vecbase;
397b62ceeafSPeter Maydell #endif
398fcf5ef2aSThomas Huth 
3998128c8e8SPeter Maydell         if (cpu_isar_feature(aa32_lob, cpu)) {
4008128c8e8SPeter Maydell             /*
4018128c8e8SPeter Maydell              * LTPSIZE is constant 4 if MVE not implemented, and resets
4028128c8e8SPeter Maydell              * to an UNKNOWN value if MVE is implemented. We choose to
4038128c8e8SPeter Maydell              * always reset to 4.
4048128c8e8SPeter Maydell              */
4058128c8e8SPeter Maydell             env->v7m.ltpsize = 4;
40699c7834fSPeter Maydell             /* The LTPSIZE field in FPDSCR is constant and reads as 4. */
40799c7834fSPeter Maydell             env->v7m.fpdscr[M_REG_NS] = 4 << FPCR_LTPSIZE_SHIFT;
40899c7834fSPeter Maydell             env->v7m.fpdscr[M_REG_S] = 4 << FPCR_LTPSIZE_SHIFT;
4098128c8e8SPeter Maydell         }
4108128c8e8SPeter Maydell 
4111e577cc7SPeter Maydell         if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
4121e577cc7SPeter Maydell             env->v7m.secure = true;
4133b2e9344SPeter Maydell         } else {
4143b2e9344SPeter Maydell             /* This bit resets to 0 if security is supported, but 1 if
4153b2e9344SPeter Maydell              * it is not. The bit is not present in v7M, but we set it
4163b2e9344SPeter Maydell              * here so we can avoid having to make checks on it conditional
4173b2e9344SPeter Maydell              * on ARM_FEATURE_V8 (we don't let the guest see the bit).
4183b2e9344SPeter Maydell              */
4193b2e9344SPeter Maydell             env->v7m.aircr = R_V7M_AIRCR_BFHFNMINS_MASK;
42002ac2f7fSPeter Maydell             /*
42102ac2f7fSPeter Maydell              * Set NSACR to indicate "NS access permitted to everything";
42202ac2f7fSPeter Maydell              * this avoids having to have all the tests of it being
42302ac2f7fSPeter Maydell              * conditional on ARM_FEATURE_M_SECURITY. Note also that from
42402ac2f7fSPeter Maydell              * v8.1M the guest-visible value of NSACR in a CPU without the
42502ac2f7fSPeter Maydell              * Security Extension is 0xcff.
42602ac2f7fSPeter Maydell              */
42702ac2f7fSPeter Maydell             env->v7m.nsacr = 0xcff;
4281e577cc7SPeter Maydell         }
4291e577cc7SPeter Maydell 
4309d40cd8aSPeter Maydell         /* In v7M the reset value of this bit is IMPDEF, but ARM recommends
4312c4da50dSPeter Maydell          * that it resets to 1, so QEMU always does that rather than making
4329d40cd8aSPeter Maydell          * it dependent on CPU model. In v8M it is RES1.
4332c4da50dSPeter Maydell          */
4349d40cd8aSPeter Maydell         env->v7m.ccr[M_REG_NS] = R_V7M_CCR_STKALIGN_MASK;
4359d40cd8aSPeter Maydell         env->v7m.ccr[M_REG_S] = R_V7M_CCR_STKALIGN_MASK;
4369d40cd8aSPeter Maydell         if (arm_feature(env, ARM_FEATURE_V8)) {
4379d40cd8aSPeter Maydell             /* in v8M the NONBASETHRDENA bit [0] is RES1 */
4389d40cd8aSPeter Maydell             env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_NONBASETHRDENA_MASK;
4399d40cd8aSPeter Maydell             env->v7m.ccr[M_REG_S] |= R_V7M_CCR_NONBASETHRDENA_MASK;
4409d40cd8aSPeter Maydell         }
44122ab3460SJulia Suvorova         if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
44222ab3460SJulia Suvorova             env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_UNALIGN_TRP_MASK;
44322ab3460SJulia Suvorova             env->v7m.ccr[M_REG_S] |= R_V7M_CCR_UNALIGN_TRP_MASK;
44422ab3460SJulia Suvorova         }
4452c4da50dSPeter Maydell 
4467fbc6a40SRichard Henderson         if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
447d33abe82SPeter Maydell             env->v7m.fpccr[M_REG_NS] = R_V7M_FPCCR_ASPEN_MASK;
448d33abe82SPeter Maydell             env->v7m.fpccr[M_REG_S] = R_V7M_FPCCR_ASPEN_MASK |
449d33abe82SPeter Maydell                 R_V7M_FPCCR_LSPEN_MASK | R_V7M_FPCCR_S_MASK;
450d33abe82SPeter Maydell         }
451b62ceeafSPeter Maydell 
452b62ceeafSPeter Maydell #ifndef CONFIG_USER_ONLY
453056f43dfSPeter Maydell         /* Unlike A/R profile, M profile defines the reset LR value */
454056f43dfSPeter Maydell         env->regs[14] = 0xffffffff;
455056f43dfSPeter Maydell 
45638e2a77cSPeter Maydell         env->v7m.vecbase[M_REG_S] = cpu->init_svtor & 0xffffff80;
4577cda2149SPeter Maydell         env->v7m.vecbase[M_REG_NS] = cpu->init_nsvtor & 0xffffff80;
45838e2a77cSPeter Maydell 
45938e2a77cSPeter Maydell         /* Load the initial SP and PC from offset 0 and 4 in the vector table */
46038e2a77cSPeter Maydell         vecbase = env->v7m.vecbase[env->v7m.secure];
461348802b5SPhilippe Mathieu-Daudé         rom = rom_ptr_for_as(cs->as, vecbase, 8);
462fcf5ef2aSThomas Huth         if (rom) {
463fcf5ef2aSThomas Huth             /* Address zero is covered by ROM which hasn't yet been
464fcf5ef2aSThomas Huth              * copied into physical memory.
465fcf5ef2aSThomas Huth              */
466fcf5ef2aSThomas Huth             initial_msp = ldl_p(rom);
467fcf5ef2aSThomas Huth             initial_pc = ldl_p(rom + 4);
468fcf5ef2aSThomas Huth         } else {
469fcf5ef2aSThomas Huth             /* Address zero not covered by a ROM blob, or the ROM blob
470fcf5ef2aSThomas Huth              * is in non-modifiable memory and this is a second reset after
471fcf5ef2aSThomas Huth              * it got copied into memory. In the latter case, rom_ptr
472fcf5ef2aSThomas Huth              * will return a NULL pointer and we should use ldl_phys instead.
473fcf5ef2aSThomas Huth              */
474348802b5SPhilippe Mathieu-Daudé             initial_msp = ldl_phys(cs->as, vecbase);
475348802b5SPhilippe Mathieu-Daudé             initial_pc = ldl_phys(cs->as, vecbase + 4);
476fcf5ef2aSThomas Huth         }
477fcf5ef2aSThomas Huth 
4788cc2246cSPeter Maydell         qemu_log_mask(CPU_LOG_INT,
4798cc2246cSPeter Maydell                       "Loaded reset SP 0x%x PC 0x%x from vector table\n",
4808cc2246cSPeter Maydell                       initial_msp, initial_pc);
4818cc2246cSPeter Maydell 
482fcf5ef2aSThomas Huth         env->regs[13] = initial_msp & 0xFFFFFFFC;
483fcf5ef2aSThomas Huth         env->regs[15] = initial_pc & ~1;
484fcf5ef2aSThomas Huth         env->thumb = initial_pc & 1;
485b62ceeafSPeter Maydell #else
486b62ceeafSPeter Maydell         /*
487b62ceeafSPeter Maydell          * For user mode we run non-secure and with access to the FPU.
488b62ceeafSPeter Maydell          * The FPU context is active (ie does not need further setup)
489b62ceeafSPeter Maydell          * and is owned by non-secure.
490b62ceeafSPeter Maydell          */
491b62ceeafSPeter Maydell         env->v7m.secure = false;
492b62ceeafSPeter Maydell         env->v7m.nsacr = 0xcff;
493b62ceeafSPeter Maydell         env->v7m.cpacr[M_REG_NS] = 0xf0ffff;
494b62ceeafSPeter Maydell         env->v7m.fpccr[M_REG_S] &=
495b62ceeafSPeter Maydell             ~(R_V7M_FPCCR_LSPEN_MASK | R_V7M_FPCCR_S_MASK);
496b62ceeafSPeter Maydell         env->v7m.control[M_REG_S] |= R_V7M_CONTROL_FPCA_MASK;
497b62ceeafSPeter Maydell #endif
498fcf5ef2aSThomas Huth     }
499fcf5ef2aSThomas Huth 
500dc3c4c14SPeter Maydell     /* M profile requires that reset clears the exclusive monitor;
501dc3c4c14SPeter Maydell      * A profile does not, but clearing it makes more sense than having it
502dc3c4c14SPeter Maydell      * set with an exclusive access on address zero.
503dc3c4c14SPeter Maydell      */
504dc3c4c14SPeter Maydell     arm_clear_exclusive(env);
505dc3c4c14SPeter Maydell 
5060e1a46bbSPeter Maydell     if (arm_feature(env, ARM_FEATURE_PMSA)) {
50769ceea64SPeter Maydell         if (cpu->pmsav7_dregion > 0) {
5080e1a46bbSPeter Maydell             if (arm_feature(env, ARM_FEATURE_V8)) {
50962c58ee0SPeter Maydell                 memset(env->pmsav8.rbar[M_REG_NS], 0,
51062c58ee0SPeter Maydell                        sizeof(*env->pmsav8.rbar[M_REG_NS])
51162c58ee0SPeter Maydell                        * cpu->pmsav7_dregion);
51262c58ee0SPeter Maydell                 memset(env->pmsav8.rlar[M_REG_NS], 0,
51362c58ee0SPeter Maydell                        sizeof(*env->pmsav8.rlar[M_REG_NS])
51462c58ee0SPeter Maydell                        * cpu->pmsav7_dregion);
51562c58ee0SPeter Maydell                 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
51662c58ee0SPeter Maydell                     memset(env->pmsav8.rbar[M_REG_S], 0,
51762c58ee0SPeter Maydell                            sizeof(*env->pmsav8.rbar[M_REG_S])
51862c58ee0SPeter Maydell                            * cpu->pmsav7_dregion);
51962c58ee0SPeter Maydell                     memset(env->pmsav8.rlar[M_REG_S], 0,
52062c58ee0SPeter Maydell                            sizeof(*env->pmsav8.rlar[M_REG_S])
52162c58ee0SPeter Maydell                            * cpu->pmsav7_dregion);
52262c58ee0SPeter Maydell                 }
5230e1a46bbSPeter Maydell             } else if (arm_feature(env, ARM_FEATURE_V7)) {
52469ceea64SPeter Maydell                 memset(env->pmsav7.drbar, 0,
52569ceea64SPeter Maydell                        sizeof(*env->pmsav7.drbar) * cpu->pmsav7_dregion);
52669ceea64SPeter Maydell                 memset(env->pmsav7.drsr, 0,
52769ceea64SPeter Maydell                        sizeof(*env->pmsav7.drsr) * cpu->pmsav7_dregion);
52869ceea64SPeter Maydell                 memset(env->pmsav7.dracr, 0,
52969ceea64SPeter Maydell                        sizeof(*env->pmsav7.dracr) * cpu->pmsav7_dregion);
53069ceea64SPeter Maydell             }
5310e1a46bbSPeter Maydell         }
532761c4642STobias Röhmel 
533761c4642STobias Röhmel         if (cpu->pmsav8r_hdregion > 0) {
534761c4642STobias Röhmel             memset(env->pmsav8.hprbar, 0,
535761c4642STobias Röhmel                    sizeof(*env->pmsav8.hprbar) * cpu->pmsav8r_hdregion);
536761c4642STobias Röhmel             memset(env->pmsav8.hprlar, 0,
537761c4642STobias Röhmel                    sizeof(*env->pmsav8.hprlar) * cpu->pmsav8r_hdregion);
538761c4642STobias Röhmel         }
539761c4642STobias Röhmel 
5401bc04a88SPeter Maydell         env->pmsav7.rnr[M_REG_NS] = 0;
5411bc04a88SPeter Maydell         env->pmsav7.rnr[M_REG_S] = 0;
5424125e6feSPeter Maydell         env->pmsav8.mair0[M_REG_NS] = 0;
5434125e6feSPeter Maydell         env->pmsav8.mair0[M_REG_S] = 0;
5444125e6feSPeter Maydell         env->pmsav8.mair1[M_REG_NS] = 0;
5454125e6feSPeter Maydell         env->pmsav8.mair1[M_REG_S] = 0;
54669ceea64SPeter Maydell     }
54769ceea64SPeter Maydell 
5489901c576SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
5499901c576SPeter Maydell         if (cpu->sau_sregion > 0) {
5509901c576SPeter Maydell             memset(env->sau.rbar, 0, sizeof(*env->sau.rbar) * cpu->sau_sregion);
5519901c576SPeter Maydell             memset(env->sau.rlar, 0, sizeof(*env->sau.rlar) * cpu->sau_sregion);
5529901c576SPeter Maydell         }
5539901c576SPeter Maydell         env->sau.rnr = 0;
5549901c576SPeter Maydell         /* SAU_CTRL reset value is IMPDEF; we choose 0, which is what
5559901c576SPeter Maydell          * the Cortex-M33 does.
5569901c576SPeter Maydell          */
5579901c576SPeter Maydell         env->sau.ctrl = 0;
5589901c576SPeter Maydell     }
5599901c576SPeter Maydell 
560fcf5ef2aSThomas Huth     set_flush_to_zero(1, &env->vfp.standard_fp_status);
561fcf5ef2aSThomas Huth     set_flush_inputs_to_zero(1, &env->vfp.standard_fp_status);
562fcf5ef2aSThomas Huth     set_default_nan_mode(1, &env->vfp.standard_fp_status);
563aaae563bSPeter Maydell     set_default_nan_mode(1, &env->vfp.standard_fp_status_f16);
564*d1ff9967SPeter Maydell     arm_set_default_fp_behaviours(&env->vfp.fp_status);
565*d1ff9967SPeter Maydell     arm_set_default_fp_behaviours(&env->vfp.standard_fp_status);
566*d1ff9967SPeter Maydell     arm_set_default_fp_behaviours(&env->vfp.fp_status_f16);
567*d1ff9967SPeter Maydell     arm_set_default_fp_behaviours(&env->vfp.standard_fp_status_f16);
568*d1ff9967SPeter Maydell 
569fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
570fcf5ef2aSThomas Huth     if (kvm_enabled()) {
571fcf5ef2aSThomas Huth         kvm_arm_reset_vcpu(cpu);
572fcf5ef2aSThomas Huth     }
573fcf5ef2aSThomas Huth #endif
574fcf5ef2aSThomas Huth 
575fa05d1abSFabiano Rosas     if (tcg_enabled()) {
576fcf5ef2aSThomas Huth         hw_breakpoint_update_all(cpu);
577fcf5ef2aSThomas Huth         hw_watchpoint_update_all(cpu);
5782b77ad4dSFabiano Rosas 
579a8a79c7aSRichard Henderson         arm_rebuild_hflags(env);
580fcf5ef2aSThomas Huth     }
5812b77ad4dSFabiano Rosas }
582fcf5ef2aSThomas Huth 
arm_emulate_firmware_reset(CPUState * cpustate,int target_el)5833a45f4f5SPeter Maydell void arm_emulate_firmware_reset(CPUState *cpustate, int target_el)
5843a45f4f5SPeter Maydell {
5853a45f4f5SPeter Maydell     ARMCPU *cpu = ARM_CPU(cpustate);
5863a45f4f5SPeter Maydell     CPUARMState *env = &cpu->env;
5873a45f4f5SPeter Maydell     bool have_el3 = arm_feature(env, ARM_FEATURE_EL3);
5883a45f4f5SPeter Maydell     bool have_el2 = arm_feature(env, ARM_FEATURE_EL2);
5893a45f4f5SPeter Maydell 
5903a45f4f5SPeter Maydell     /*
5913a45f4f5SPeter Maydell      * Check we have the EL we're aiming for. If that is the
5923a45f4f5SPeter Maydell      * highest implemented EL, then cpu_reset has already done
5933a45f4f5SPeter Maydell      * all the work.
5943a45f4f5SPeter Maydell      */
5953a45f4f5SPeter Maydell     switch (target_el) {
5963a45f4f5SPeter Maydell     case 3:
5973a45f4f5SPeter Maydell         assert(have_el3);
5983a45f4f5SPeter Maydell         return;
5993a45f4f5SPeter Maydell     case 2:
6003a45f4f5SPeter Maydell         assert(have_el2);
6013a45f4f5SPeter Maydell         if (!have_el3) {
6023a45f4f5SPeter Maydell             return;
6033a45f4f5SPeter Maydell         }
6043a45f4f5SPeter Maydell         break;
6053a45f4f5SPeter Maydell     case 1:
6063a45f4f5SPeter Maydell         if (!have_el3 && !have_el2) {
6073a45f4f5SPeter Maydell             return;
6083a45f4f5SPeter Maydell         }
6093a45f4f5SPeter Maydell         break;
6103a45f4f5SPeter Maydell     default:
6113a45f4f5SPeter Maydell         g_assert_not_reached();
6123a45f4f5SPeter Maydell     }
6133a45f4f5SPeter Maydell 
6143a45f4f5SPeter Maydell     if (have_el3) {
6153a45f4f5SPeter Maydell         /*
6163a45f4f5SPeter Maydell          * Set the EL3 state so code can run at EL2. This should match
6173a45f4f5SPeter Maydell          * the requirements set by Linux in its booting spec.
6183a45f4f5SPeter Maydell          */
6193a45f4f5SPeter Maydell         if (env->aarch64) {
6203a45f4f5SPeter Maydell             env->cp15.scr_el3 |= SCR_RW;
6213a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_pauth, cpu)) {
6223a45f4f5SPeter Maydell                 env->cp15.scr_el3 |= SCR_API | SCR_APK;
6233a45f4f5SPeter Maydell             }
6243a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_mte, cpu)) {
6253a45f4f5SPeter Maydell                 env->cp15.scr_el3 |= SCR_ATA;
6263a45f4f5SPeter Maydell             }
6273a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_sve, cpu)) {
6283a45f4f5SPeter Maydell                 env->cp15.cptr_el[3] |= R_CPTR_EL3_EZ_MASK;
6293a45f4f5SPeter Maydell                 env->vfp.zcr_el[3] = 0xf;
6303a45f4f5SPeter Maydell             }
6313a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_sme, cpu)) {
6323a45f4f5SPeter Maydell                 env->cp15.cptr_el[3] |= R_CPTR_EL3_ESM_MASK;
6333a45f4f5SPeter Maydell                 env->cp15.scr_el3 |= SCR_ENTP2;
6343a45f4f5SPeter Maydell                 env->vfp.smcr_el[3] = 0xf;
6353a45f4f5SPeter Maydell             }
6363a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_hcx, cpu)) {
6373a45f4f5SPeter Maydell                 env->cp15.scr_el3 |= SCR_HXEN;
6383a45f4f5SPeter Maydell             }
6393a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_fgt, cpu)) {
6403a45f4f5SPeter Maydell                 env->cp15.scr_el3 |= SCR_FGTEN;
6413a45f4f5SPeter Maydell             }
6423a45f4f5SPeter Maydell         }
6433a45f4f5SPeter Maydell 
6443a45f4f5SPeter Maydell         if (target_el == 2) {
6453a45f4f5SPeter Maydell             /* If the guest is at EL2 then Linux expects the HVC insn to work */
6463a45f4f5SPeter Maydell             env->cp15.scr_el3 |= SCR_HCE;
6473a45f4f5SPeter Maydell         }
6483a45f4f5SPeter Maydell 
6493a45f4f5SPeter Maydell         /* Put CPU into non-secure state */
6503a45f4f5SPeter Maydell         env->cp15.scr_el3 |= SCR_NS;
6513a45f4f5SPeter Maydell         /* Set NSACR.{CP11,CP10} so NS can access the FPU */
6523a45f4f5SPeter Maydell         env->cp15.nsacr |= 3 << 10;
6533a45f4f5SPeter Maydell     }
6543a45f4f5SPeter Maydell 
6553a45f4f5SPeter Maydell     if (have_el2 && target_el < 2) {
6563a45f4f5SPeter Maydell         /* Set EL2 state so code can run at EL1. */
6573a45f4f5SPeter Maydell         if (env->aarch64) {
6583a45f4f5SPeter Maydell             env->cp15.hcr_el2 |= HCR_RW;
6593a45f4f5SPeter Maydell         }
6603a45f4f5SPeter Maydell     }
6613a45f4f5SPeter Maydell 
6623a45f4f5SPeter Maydell     /* Set the CPU to the desired state */
6633a45f4f5SPeter Maydell     if (env->aarch64) {
6643a45f4f5SPeter Maydell         env->pstate = aarch64_pstate_mode(target_el, true);
6653a45f4f5SPeter Maydell     } else {
6663a45f4f5SPeter Maydell         static const uint32_t mode_for_el[] = {
6673a45f4f5SPeter Maydell             0,
6683a45f4f5SPeter Maydell             ARM_CPU_MODE_SVC,
6693a45f4f5SPeter Maydell             ARM_CPU_MODE_HYP,
6703a45f4f5SPeter Maydell             ARM_CPU_MODE_SVC,
6713a45f4f5SPeter Maydell         };
6723a45f4f5SPeter Maydell 
6733a45f4f5SPeter Maydell         cpsr_write(env, mode_for_el[target_el], CPSR_M, CPSRWriteRaw);
6743a45f4f5SPeter Maydell     }
6753a45f4f5SPeter Maydell }
6763a45f4f5SPeter Maydell 
6773a45f4f5SPeter Maydell 
6789e406eeaSPhilippe Mathieu-Daudé #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
679083afd18SPhilippe Mathieu-Daudé 
arm_excp_unmasked(CPUState * cs,unsigned int excp_idx,unsigned int target_el,unsigned int cur_el,bool secure,uint64_t hcr_el2)680310cedf3SRichard Henderson static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx,
681be879556SRichard Henderson                                      unsigned int target_el,
682be879556SRichard Henderson                                      unsigned int cur_el, bool secure,
683be879556SRichard Henderson                                      uint64_t hcr_el2)
684310cedf3SRichard Henderson {
685b77af26eSRichard Henderson     CPUARMState *env = cpu_env(cs);
686310cedf3SRichard Henderson     bool pstate_unmasked;
68716e07f78SRichard Henderson     bool unmasked = false;
688b36a32eaSJinjie Ruan     bool allIntMask = false;
689310cedf3SRichard Henderson 
690310cedf3SRichard Henderson     /*
691310cedf3SRichard Henderson      * Don't take exceptions if they target a lower EL.
692310cedf3SRichard Henderson      * This check should catch any exceptions that would not be taken
693310cedf3SRichard Henderson      * but left pending.
694310cedf3SRichard Henderson      */
695310cedf3SRichard Henderson     if (cur_el > target_el) {
696310cedf3SRichard Henderson         return false;
697310cedf3SRichard Henderson     }
698310cedf3SRichard Henderson 
699b36a32eaSJinjie Ruan     if (cpu_isar_feature(aa64_nmi, env_archcpu(env)) &&
700b36a32eaSJinjie Ruan         env->cp15.sctlr_el[target_el] & SCTLR_NMI && cur_el == target_el) {
701b36a32eaSJinjie Ruan         allIntMask = env->pstate & PSTATE_ALLINT ||
702b36a32eaSJinjie Ruan                      ((env->cp15.sctlr_el[target_el] & SCTLR_SPINTMASK) &&
703b36a32eaSJinjie Ruan                       (env->pstate & PSTATE_SP));
704b36a32eaSJinjie Ruan     }
705b36a32eaSJinjie Ruan 
706310cedf3SRichard Henderson     switch (excp_idx) {
707b36a32eaSJinjie Ruan     case EXCP_NMI:
708b36a32eaSJinjie Ruan         pstate_unmasked = !allIntMask;
709b36a32eaSJinjie Ruan         break;
710b36a32eaSJinjie Ruan 
711b36a32eaSJinjie Ruan     case EXCP_VINMI:
712b36a32eaSJinjie Ruan         if (!(hcr_el2 & HCR_IMO) || (hcr_el2 & HCR_TGE)) {
713b36a32eaSJinjie Ruan             /* VINMIs are only taken when hypervized.  */
714b36a32eaSJinjie Ruan             return false;
715b36a32eaSJinjie Ruan         }
716b36a32eaSJinjie Ruan         return !allIntMask;
717b36a32eaSJinjie Ruan     case EXCP_VFNMI:
718b36a32eaSJinjie Ruan         if (!(hcr_el2 & HCR_FMO) || (hcr_el2 & HCR_TGE)) {
719b36a32eaSJinjie Ruan             /* VFNMIs are only taken when hypervized.  */
720b36a32eaSJinjie Ruan             return false;
721b36a32eaSJinjie Ruan         }
722b36a32eaSJinjie Ruan         return !allIntMask;
723310cedf3SRichard Henderson     case EXCP_FIQ:
724b36a32eaSJinjie Ruan         pstate_unmasked = (!(env->daif & PSTATE_F)) && (!allIntMask);
725310cedf3SRichard Henderson         break;
726310cedf3SRichard Henderson 
727310cedf3SRichard Henderson     case EXCP_IRQ:
728b36a32eaSJinjie Ruan         pstate_unmasked = (!(env->daif & PSTATE_I)) && (!allIntMask);
729310cedf3SRichard Henderson         break;
730310cedf3SRichard Henderson 
731310cedf3SRichard Henderson     case EXCP_VFIQ:
732cc974d5cSRémi Denis-Courmont         if (!(hcr_el2 & HCR_FMO) || (hcr_el2 & HCR_TGE)) {
733cc974d5cSRémi Denis-Courmont             /* VFIQs are only taken when hypervized.  */
734310cedf3SRichard Henderson             return false;
735310cedf3SRichard Henderson         }
736b36a32eaSJinjie Ruan         return !(env->daif & PSTATE_F) && (!allIntMask);
737310cedf3SRichard Henderson     case EXCP_VIRQ:
738cc974d5cSRémi Denis-Courmont         if (!(hcr_el2 & HCR_IMO) || (hcr_el2 & HCR_TGE)) {
739cc974d5cSRémi Denis-Courmont             /* VIRQs are only taken when hypervized.  */
740310cedf3SRichard Henderson             return false;
741310cedf3SRichard Henderson         }
742b36a32eaSJinjie Ruan         return !(env->daif & PSTATE_I) && (!allIntMask);
7433c29632fSRichard Henderson     case EXCP_VSERR:
7443c29632fSRichard Henderson         if (!(hcr_el2 & HCR_AMO) || (hcr_el2 & HCR_TGE)) {
7453c29632fSRichard Henderson             /* VIRQs are only taken when hypervized.  */
7463c29632fSRichard Henderson             return false;
7473c29632fSRichard Henderson         }
7483c29632fSRichard Henderson         return !(env->daif & PSTATE_A);
749310cedf3SRichard Henderson     default:
750310cedf3SRichard Henderson         g_assert_not_reached();
751310cedf3SRichard Henderson     }
752310cedf3SRichard Henderson 
753310cedf3SRichard Henderson     /*
754310cedf3SRichard Henderson      * Use the target EL, current execution state and SCR/HCR settings to
755310cedf3SRichard Henderson      * determine whether the corresponding CPSR bit is used to mask the
756310cedf3SRichard Henderson      * interrupt.
757310cedf3SRichard Henderson      */
758310cedf3SRichard Henderson     if ((target_el > cur_el) && (target_el != 1)) {
759310cedf3SRichard Henderson         /* Exceptions targeting a higher EL may not be maskable */
760310cedf3SRichard Henderson         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
761c939a7c7SAke Koomsin             switch (target_el) {
762c939a7c7SAke Koomsin             case 2:
763310cedf3SRichard Henderson                 /*
764c939a7c7SAke Koomsin                  * According to ARM DDI 0487H.a, an interrupt can be masked
765c939a7c7SAke Koomsin                  * when HCR_E2H and HCR_TGE are both set regardless of the
766c939a7c7SAke Koomsin                  * current Security state. Note that we need to revisit this
767c939a7c7SAke Koomsin                  * part again once we need to support NMI.
768310cedf3SRichard Henderson                  */
769c939a7c7SAke Koomsin                 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
77016e07f78SRichard Henderson                         unmasked = true;
771310cedf3SRichard Henderson                 }
772c939a7c7SAke Koomsin                 break;
773c939a7c7SAke Koomsin             case 3:
774c939a7c7SAke Koomsin                 /* Interrupt cannot be masked when the target EL is 3 */
775c939a7c7SAke Koomsin                 unmasked = true;
776c939a7c7SAke Koomsin                 break;
777c939a7c7SAke Koomsin             default:
778c939a7c7SAke Koomsin                 g_assert_not_reached();
779c939a7c7SAke Koomsin             }
780310cedf3SRichard Henderson         } else {
781310cedf3SRichard Henderson             /*
782310cedf3SRichard Henderson              * The old 32-bit-only environment has a more complicated
783310cedf3SRichard Henderson              * masking setup. HCR and SCR bits not only affect interrupt
784310cedf3SRichard Henderson              * routing but also change the behaviour of masking.
785310cedf3SRichard Henderson              */
786310cedf3SRichard Henderson             bool hcr, scr;
787310cedf3SRichard Henderson 
788310cedf3SRichard Henderson             switch (excp_idx) {
789310cedf3SRichard Henderson             case EXCP_FIQ:
790310cedf3SRichard Henderson                 /*
791310cedf3SRichard Henderson                  * If FIQs are routed to EL3 or EL2 then there are cases where
792310cedf3SRichard Henderson                  * we override the CPSR.F in determining if the exception is
793310cedf3SRichard Henderson                  * masked or not. If neither of these are set then we fall back
794310cedf3SRichard Henderson                  * to the CPSR.F setting otherwise we further assess the state
795310cedf3SRichard Henderson                  * below.
796310cedf3SRichard Henderson                  */
797310cedf3SRichard Henderson                 hcr = hcr_el2 & HCR_FMO;
798310cedf3SRichard Henderson                 scr = (env->cp15.scr_el3 & SCR_FIQ);
799310cedf3SRichard Henderson 
800310cedf3SRichard Henderson                 /*
801310cedf3SRichard Henderson                  * When EL3 is 32-bit, the SCR.FW bit controls whether the
802310cedf3SRichard Henderson                  * CPSR.F bit masks FIQ interrupts when taken in non-secure
803310cedf3SRichard Henderson                  * state. If SCR.FW is set then FIQs can be masked by CPSR.F
804310cedf3SRichard Henderson                  * when non-secure but only when FIQs are only routed to EL3.
805310cedf3SRichard Henderson                  */
806310cedf3SRichard Henderson                 scr = scr && !((env->cp15.scr_el3 & SCR_FW) && !hcr);
807310cedf3SRichard Henderson                 break;
808310cedf3SRichard Henderson             case EXCP_IRQ:
809310cedf3SRichard Henderson                 /*
810310cedf3SRichard Henderson                  * When EL3 execution state is 32-bit, if HCR.IMO is set then
811310cedf3SRichard Henderson                  * we may override the CPSR.I masking when in non-secure state.
812310cedf3SRichard Henderson                  * The SCR.IRQ setting has already been taken into consideration
813310cedf3SRichard Henderson                  * when setting the target EL, so it does not have a further
814310cedf3SRichard Henderson                  * affect here.
815310cedf3SRichard Henderson                  */
816310cedf3SRichard Henderson                 hcr = hcr_el2 & HCR_IMO;
817310cedf3SRichard Henderson                 scr = false;
818310cedf3SRichard Henderson                 break;
819310cedf3SRichard Henderson             default:
820310cedf3SRichard Henderson                 g_assert_not_reached();
821310cedf3SRichard Henderson             }
822310cedf3SRichard Henderson 
823310cedf3SRichard Henderson             if ((scr || hcr) && !secure) {
82416e07f78SRichard Henderson                 unmasked = true;
825310cedf3SRichard Henderson             }
826310cedf3SRichard Henderson         }
827310cedf3SRichard Henderson     }
828310cedf3SRichard Henderson 
829310cedf3SRichard Henderson     /*
830673d8215SMichael Tokarev      * The PSTATE bits only mask the interrupt if we have not overridden the
831310cedf3SRichard Henderson      * ability above.
832310cedf3SRichard Henderson      */
833310cedf3SRichard Henderson     return unmasked || pstate_unmasked;
834310cedf3SRichard Henderson }
835310cedf3SRichard Henderson 
arm_cpu_exec_interrupt(CPUState * cs,int interrupt_request)836083afd18SPhilippe Mathieu-Daudé static bool arm_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
837fcf5ef2aSThomas Huth {
838fcf5ef2aSThomas Huth     CPUClass *cc = CPU_GET_CLASS(cs);
839b77af26eSRichard Henderson     CPUARMState *env = cpu_env(cs);
840fcf5ef2aSThomas Huth     uint32_t cur_el = arm_current_el(env);
841fcf5ef2aSThomas Huth     bool secure = arm_is_secure(env);
842be879556SRichard Henderson     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
843fcf5ef2aSThomas Huth     uint32_t target_el;
844fcf5ef2aSThomas Huth     uint32_t excp_idx;
845d63d0ec5SRichard Henderson 
846d63d0ec5SRichard Henderson     /* The prioritization of interrupts is IMPLEMENTATION DEFINED. */
847fcf5ef2aSThomas Huth 
848b36a32eaSJinjie Ruan     if (cpu_isar_feature(aa64_nmi, env_archcpu(env)) &&
849b36a32eaSJinjie Ruan         (arm_sctlr(env, cur_el) & SCTLR_NMI)) {
850b36a32eaSJinjie Ruan         if (interrupt_request & CPU_INTERRUPT_NMI) {
851b36a32eaSJinjie Ruan             excp_idx = EXCP_NMI;
852b36a32eaSJinjie Ruan             target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
853b36a32eaSJinjie Ruan             if (arm_excp_unmasked(cs, excp_idx, target_el,
854b36a32eaSJinjie Ruan                                   cur_el, secure, hcr_el2)) {
855b36a32eaSJinjie Ruan                 goto found;
856b36a32eaSJinjie Ruan             }
857b36a32eaSJinjie Ruan         }
858b36a32eaSJinjie Ruan         if (interrupt_request & CPU_INTERRUPT_VINMI) {
859b36a32eaSJinjie Ruan             excp_idx = EXCP_VINMI;
860b36a32eaSJinjie Ruan             target_el = 1;
861b36a32eaSJinjie Ruan             if (arm_excp_unmasked(cs, excp_idx, target_el,
862b36a32eaSJinjie Ruan                                   cur_el, secure, hcr_el2)) {
863b36a32eaSJinjie Ruan                 goto found;
864b36a32eaSJinjie Ruan             }
865b36a32eaSJinjie Ruan         }
866b36a32eaSJinjie Ruan         if (interrupt_request & CPU_INTERRUPT_VFNMI) {
867b36a32eaSJinjie Ruan             excp_idx = EXCP_VFNMI;
868b36a32eaSJinjie Ruan             target_el = 1;
869b36a32eaSJinjie Ruan             if (arm_excp_unmasked(cs, excp_idx, target_el,
870b36a32eaSJinjie Ruan                                   cur_el, secure, hcr_el2)) {
871b36a32eaSJinjie Ruan                 goto found;
872b36a32eaSJinjie Ruan             }
873b36a32eaSJinjie Ruan         }
874b36a32eaSJinjie Ruan     } else {
875b36a32eaSJinjie Ruan         /*
876b36a32eaSJinjie Ruan          * NMI disabled: interrupts with superpriority are handled
877b36a32eaSJinjie Ruan          * as if they didn't have it
878b36a32eaSJinjie Ruan          */
879b36a32eaSJinjie Ruan         if (interrupt_request & CPU_INTERRUPT_NMI) {
880b36a32eaSJinjie Ruan             interrupt_request |= CPU_INTERRUPT_HARD;
881b36a32eaSJinjie Ruan         }
882b36a32eaSJinjie Ruan         if (interrupt_request & CPU_INTERRUPT_VINMI) {
883b36a32eaSJinjie Ruan             interrupt_request |= CPU_INTERRUPT_VIRQ;
884b36a32eaSJinjie Ruan         }
885b36a32eaSJinjie Ruan         if (interrupt_request & CPU_INTERRUPT_VFNMI) {
886b36a32eaSJinjie Ruan             interrupt_request |= CPU_INTERRUPT_VFIQ;
887b36a32eaSJinjie Ruan         }
888b36a32eaSJinjie Ruan     }
889b36a32eaSJinjie Ruan 
890fcf5ef2aSThomas Huth     if (interrupt_request & CPU_INTERRUPT_FIQ) {
891fcf5ef2aSThomas Huth         excp_idx = EXCP_FIQ;
892fcf5ef2aSThomas Huth         target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
893be879556SRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
894be879556SRichard Henderson                               cur_el, secure, hcr_el2)) {
895d63d0ec5SRichard Henderson             goto found;
896fcf5ef2aSThomas Huth         }
897fcf5ef2aSThomas Huth     }
898fcf5ef2aSThomas Huth     if (interrupt_request & CPU_INTERRUPT_HARD) {
899fcf5ef2aSThomas Huth         excp_idx = EXCP_IRQ;
900fcf5ef2aSThomas Huth         target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
901be879556SRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
902be879556SRichard Henderson                               cur_el, secure, hcr_el2)) {
903d63d0ec5SRichard Henderson             goto found;
904fcf5ef2aSThomas Huth         }
905fcf5ef2aSThomas Huth     }
906fcf5ef2aSThomas Huth     if (interrupt_request & CPU_INTERRUPT_VIRQ) {
907fcf5ef2aSThomas Huth         excp_idx = EXCP_VIRQ;
908fcf5ef2aSThomas Huth         target_el = 1;
909be879556SRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
910be879556SRichard Henderson                               cur_el, secure, hcr_el2)) {
911d63d0ec5SRichard Henderson             goto found;
912fcf5ef2aSThomas Huth         }
913fcf5ef2aSThomas Huth     }
914fcf5ef2aSThomas Huth     if (interrupt_request & CPU_INTERRUPT_VFIQ) {
915fcf5ef2aSThomas Huth         excp_idx = EXCP_VFIQ;
916fcf5ef2aSThomas Huth         target_el = 1;
917be879556SRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
918be879556SRichard Henderson                               cur_el, secure, hcr_el2)) {
919d63d0ec5SRichard Henderson             goto found;
920d63d0ec5SRichard Henderson         }
921d63d0ec5SRichard Henderson     }
9223c29632fSRichard Henderson     if (interrupt_request & CPU_INTERRUPT_VSERR) {
9233c29632fSRichard Henderson         excp_idx = EXCP_VSERR;
9243c29632fSRichard Henderson         target_el = 1;
9253c29632fSRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
9263c29632fSRichard Henderson                               cur_el, secure, hcr_el2)) {
9273c29632fSRichard Henderson             /* Taking a virtual abort clears HCR_EL2.VSE */
9283c29632fSRichard Henderson             env->cp15.hcr_el2 &= ~HCR_VSE;
9293c29632fSRichard Henderson             cpu_reset_interrupt(cs, CPU_INTERRUPT_VSERR);
9303c29632fSRichard Henderson             goto found;
9313c29632fSRichard Henderson         }
9323c29632fSRichard Henderson     }
933d63d0ec5SRichard Henderson     return false;
934d63d0ec5SRichard Henderson 
935d63d0ec5SRichard Henderson  found:
936fcf5ef2aSThomas Huth     cs->exception_index = excp_idx;
937fcf5ef2aSThomas Huth     env->exception.target_el = target_el;
93878271684SClaudio Fontana     cc->tcg_ops->do_interrupt(cs);
939d63d0ec5SRichard Henderson     return true;
940fcf5ef2aSThomas Huth }
9419e406eeaSPhilippe Mathieu-Daudé 
9429e406eeaSPhilippe Mathieu-Daudé #endif /* CONFIG_TCG && !CONFIG_USER_ONLY */
943fcf5ef2aSThomas Huth 
arm_cpu_update_virq(ARMCPU * cpu)94489430fc6SPeter Maydell void arm_cpu_update_virq(ARMCPU *cpu)
94589430fc6SPeter Maydell {
94689430fc6SPeter Maydell     /*
94789430fc6SPeter Maydell      * Update the interrupt level for VIRQ, which is the logical OR of
94889430fc6SPeter Maydell      * the HCR_EL2.VI bit and the input line level from the GIC.
94989430fc6SPeter Maydell      */
95089430fc6SPeter Maydell     CPUARMState *env = &cpu->env;
95189430fc6SPeter Maydell     CPUState *cs = CPU(cpu);
95289430fc6SPeter Maydell 
953b36a32eaSJinjie Ruan     bool new_state = ((arm_hcr_el2_eff(env) & HCR_VI) &&
954b36a32eaSJinjie Ruan         !(arm_hcrx_el2_eff(env) & HCRX_VINMI)) ||
95589430fc6SPeter Maydell         (env->irq_line_state & CPU_INTERRUPT_VIRQ);
95689430fc6SPeter Maydell 
95789430fc6SPeter Maydell     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VIRQ) != 0)) {
95889430fc6SPeter Maydell         if (new_state) {
95989430fc6SPeter Maydell             cpu_interrupt(cs, CPU_INTERRUPT_VIRQ);
96089430fc6SPeter Maydell         } else {
96189430fc6SPeter Maydell             cpu_reset_interrupt(cs, CPU_INTERRUPT_VIRQ);
96289430fc6SPeter Maydell         }
96389430fc6SPeter Maydell     }
96489430fc6SPeter Maydell }
96589430fc6SPeter Maydell 
arm_cpu_update_vfiq(ARMCPU * cpu)96689430fc6SPeter Maydell void arm_cpu_update_vfiq(ARMCPU *cpu)
96789430fc6SPeter Maydell {
96889430fc6SPeter Maydell     /*
96989430fc6SPeter Maydell      * Update the interrupt level for VFIQ, which is the logical OR of
97089430fc6SPeter Maydell      * the HCR_EL2.VF bit and the input line level from the GIC.
97189430fc6SPeter Maydell      */
97289430fc6SPeter Maydell     CPUARMState *env = &cpu->env;
97389430fc6SPeter Maydell     CPUState *cs = CPU(cpu);
97489430fc6SPeter Maydell 
975b36a32eaSJinjie Ruan     bool new_state = ((arm_hcr_el2_eff(env) & HCR_VF) &&
976b36a32eaSJinjie Ruan         !(arm_hcrx_el2_eff(env) & HCRX_VFNMI)) ||
97789430fc6SPeter Maydell         (env->irq_line_state & CPU_INTERRUPT_VFIQ);
97889430fc6SPeter Maydell 
97989430fc6SPeter Maydell     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VFIQ) != 0)) {
98089430fc6SPeter Maydell         if (new_state) {
98189430fc6SPeter Maydell             cpu_interrupt(cs, CPU_INTERRUPT_VFIQ);
98289430fc6SPeter Maydell         } else {
98389430fc6SPeter Maydell             cpu_reset_interrupt(cs, CPU_INTERRUPT_VFIQ);
98489430fc6SPeter Maydell         }
98589430fc6SPeter Maydell     }
98689430fc6SPeter Maydell }
98789430fc6SPeter Maydell 
arm_cpu_update_vinmi(ARMCPU * cpu)988b36a32eaSJinjie Ruan void arm_cpu_update_vinmi(ARMCPU *cpu)
989b36a32eaSJinjie Ruan {
990b36a32eaSJinjie Ruan     /*
991b36a32eaSJinjie Ruan      * Update the interrupt level for VINMI, which is the logical OR of
992b36a32eaSJinjie Ruan      * the HCRX_EL2.VINMI bit and the input line level from the GIC.
993b36a32eaSJinjie Ruan      */
994b36a32eaSJinjie Ruan     CPUARMState *env = &cpu->env;
995b36a32eaSJinjie Ruan     CPUState *cs = CPU(cpu);
996b36a32eaSJinjie Ruan 
997b36a32eaSJinjie Ruan     bool new_state = ((arm_hcr_el2_eff(env) & HCR_VI) &&
998b36a32eaSJinjie Ruan                       (arm_hcrx_el2_eff(env) & HCRX_VINMI)) ||
999b36a32eaSJinjie Ruan         (env->irq_line_state & CPU_INTERRUPT_VINMI);
1000b36a32eaSJinjie Ruan 
1001b36a32eaSJinjie Ruan     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VINMI) != 0)) {
1002b36a32eaSJinjie Ruan         if (new_state) {
1003b36a32eaSJinjie Ruan             cpu_interrupt(cs, CPU_INTERRUPT_VINMI);
1004b36a32eaSJinjie Ruan         } else {
1005b36a32eaSJinjie Ruan             cpu_reset_interrupt(cs, CPU_INTERRUPT_VINMI);
1006b36a32eaSJinjie Ruan         }
1007b36a32eaSJinjie Ruan     }
1008b36a32eaSJinjie Ruan }
1009b36a32eaSJinjie Ruan 
arm_cpu_update_vfnmi(ARMCPU * cpu)1010b36a32eaSJinjie Ruan void arm_cpu_update_vfnmi(ARMCPU *cpu)
1011b36a32eaSJinjie Ruan {
1012b36a32eaSJinjie Ruan     /*
1013b36a32eaSJinjie Ruan      * Update the interrupt level for VFNMI, which is the HCRX_EL2.VFNMI bit.
1014b36a32eaSJinjie Ruan      */
1015b36a32eaSJinjie Ruan     CPUARMState *env = &cpu->env;
1016b36a32eaSJinjie Ruan     CPUState *cs = CPU(cpu);
1017b36a32eaSJinjie Ruan 
1018b36a32eaSJinjie Ruan     bool new_state = (arm_hcr_el2_eff(env) & HCR_VF) &&
1019b36a32eaSJinjie Ruan                       (arm_hcrx_el2_eff(env) & HCRX_VFNMI);
1020b36a32eaSJinjie Ruan 
1021b36a32eaSJinjie Ruan     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VFNMI) != 0)) {
1022b36a32eaSJinjie Ruan         if (new_state) {
1023b36a32eaSJinjie Ruan             cpu_interrupt(cs, CPU_INTERRUPT_VFNMI);
1024b36a32eaSJinjie Ruan         } else {
1025b36a32eaSJinjie Ruan             cpu_reset_interrupt(cs, CPU_INTERRUPT_VFNMI);
1026b36a32eaSJinjie Ruan         }
1027b36a32eaSJinjie Ruan     }
1028b36a32eaSJinjie Ruan }
1029b36a32eaSJinjie Ruan 
arm_cpu_update_vserr(ARMCPU * cpu)10303c29632fSRichard Henderson void arm_cpu_update_vserr(ARMCPU *cpu)
10313c29632fSRichard Henderson {
10323c29632fSRichard Henderson     /*
10333c29632fSRichard Henderson      * Update the interrupt level for VSERR, which is the HCR_EL2.VSE bit.
10343c29632fSRichard Henderson      */
10353c29632fSRichard Henderson     CPUARMState *env = &cpu->env;
10363c29632fSRichard Henderson     CPUState *cs = CPU(cpu);
10373c29632fSRichard Henderson 
10383c29632fSRichard Henderson     bool new_state = env->cp15.hcr_el2 & HCR_VSE;
10393c29632fSRichard Henderson 
10403c29632fSRichard Henderson     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VSERR) != 0)) {
10413c29632fSRichard Henderson         if (new_state) {
10423c29632fSRichard Henderson             cpu_interrupt(cs, CPU_INTERRUPT_VSERR);
10433c29632fSRichard Henderson         } else {
10443c29632fSRichard Henderson             cpu_reset_interrupt(cs, CPU_INTERRUPT_VSERR);
10453c29632fSRichard Henderson         }
10463c29632fSRichard Henderson     }
10473c29632fSRichard Henderson }
10483c29632fSRichard Henderson 
1049fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
arm_cpu_set_irq(void * opaque,int irq,int level)1050fcf5ef2aSThomas Huth static void arm_cpu_set_irq(void *opaque, int irq, int level)
1051fcf5ef2aSThomas Huth {
1052fcf5ef2aSThomas Huth     ARMCPU *cpu = opaque;
1053fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
1054fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
1055fcf5ef2aSThomas Huth     static const int mask[] = {
1056fcf5ef2aSThomas Huth         [ARM_CPU_IRQ] = CPU_INTERRUPT_HARD,
1057fcf5ef2aSThomas Huth         [ARM_CPU_FIQ] = CPU_INTERRUPT_FIQ,
1058fcf5ef2aSThomas Huth         [ARM_CPU_VIRQ] = CPU_INTERRUPT_VIRQ,
1059b36a32eaSJinjie Ruan         [ARM_CPU_VFIQ] = CPU_INTERRUPT_VFIQ,
1060b36a32eaSJinjie Ruan         [ARM_CPU_NMI] = CPU_INTERRUPT_NMI,
1061b36a32eaSJinjie Ruan         [ARM_CPU_VINMI] = CPU_INTERRUPT_VINMI,
1062fcf5ef2aSThomas Huth     };
1063fcf5ef2aSThomas Huth 
10649acd2d33SPeter Maydell     if (!arm_feature(env, ARM_FEATURE_EL2) &&
10659acd2d33SPeter Maydell         (irq == ARM_CPU_VIRQ || irq == ARM_CPU_VFIQ)) {
10669acd2d33SPeter Maydell         /*
10679acd2d33SPeter Maydell          * The GIC might tell us about VIRQ and VFIQ state, but if we don't
10689acd2d33SPeter Maydell          * have EL2 support we don't care. (Unless the guest is doing something
10699acd2d33SPeter Maydell          * silly this will only be calls saying "level is still 0".)
10709acd2d33SPeter Maydell          */
10719acd2d33SPeter Maydell         return;
10729acd2d33SPeter Maydell     }
10739acd2d33SPeter Maydell 
1074ed89f078SPeter Maydell     if (level) {
1075ed89f078SPeter Maydell         env->irq_line_state |= mask[irq];
1076ed89f078SPeter Maydell     } else {
1077ed89f078SPeter Maydell         env->irq_line_state &= ~mask[irq];
1078ed89f078SPeter Maydell     }
1079ed89f078SPeter Maydell 
1080fcf5ef2aSThomas Huth     switch (irq) {
1081fcf5ef2aSThomas Huth     case ARM_CPU_VIRQ:
108289430fc6SPeter Maydell         arm_cpu_update_virq(cpu);
108389430fc6SPeter Maydell         break;
1084fcf5ef2aSThomas Huth     case ARM_CPU_VFIQ:
108589430fc6SPeter Maydell         arm_cpu_update_vfiq(cpu);
108689430fc6SPeter Maydell         break;
1087b36a32eaSJinjie Ruan     case ARM_CPU_VINMI:
1088b36a32eaSJinjie Ruan         arm_cpu_update_vinmi(cpu);
1089b36a32eaSJinjie Ruan         break;
1090fcf5ef2aSThomas Huth     case ARM_CPU_IRQ:
1091fcf5ef2aSThomas Huth     case ARM_CPU_FIQ:
1092b36a32eaSJinjie Ruan     case ARM_CPU_NMI:
1093fcf5ef2aSThomas Huth         if (level) {
1094fcf5ef2aSThomas Huth             cpu_interrupt(cs, mask[irq]);
1095fcf5ef2aSThomas Huth         } else {
1096fcf5ef2aSThomas Huth             cpu_reset_interrupt(cs, mask[irq]);
1097fcf5ef2aSThomas Huth         }
1098fcf5ef2aSThomas Huth         break;
1099fcf5ef2aSThomas Huth     default:
1100fcf5ef2aSThomas Huth         g_assert_not_reached();
1101fcf5ef2aSThomas Huth     }
1102fcf5ef2aSThomas Huth }
1103fcf5ef2aSThomas Huth 
arm_cpu_kvm_set_irq(void * opaque,int irq,int level)1104fcf5ef2aSThomas Huth static void arm_cpu_kvm_set_irq(void *opaque, int irq, int level)
1105fcf5ef2aSThomas Huth {
1106fcf5ef2aSThomas Huth #ifdef CONFIG_KVM
1107fcf5ef2aSThomas Huth     ARMCPU *cpu = opaque;
1108ed89f078SPeter Maydell     CPUARMState *env = &cpu->env;
1109fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
1110ed89f078SPeter Maydell     uint32_t linestate_bit;
1111f6530926SEric Auger     int irq_id;
1112fcf5ef2aSThomas Huth 
1113fcf5ef2aSThomas Huth     switch (irq) {
1114fcf5ef2aSThomas Huth     case ARM_CPU_IRQ:
1115f6530926SEric Auger         irq_id = KVM_ARM_IRQ_CPU_IRQ;
1116ed89f078SPeter Maydell         linestate_bit = CPU_INTERRUPT_HARD;
1117fcf5ef2aSThomas Huth         break;
1118fcf5ef2aSThomas Huth     case ARM_CPU_FIQ:
1119f6530926SEric Auger         irq_id = KVM_ARM_IRQ_CPU_FIQ;
1120ed89f078SPeter Maydell         linestate_bit = CPU_INTERRUPT_FIQ;
1121fcf5ef2aSThomas Huth         break;
1122fcf5ef2aSThomas Huth     default:
1123fcf5ef2aSThomas Huth         g_assert_not_reached();
1124fcf5ef2aSThomas Huth     }
1125ed89f078SPeter Maydell 
1126ed89f078SPeter Maydell     if (level) {
1127ed89f078SPeter Maydell         env->irq_line_state |= linestate_bit;
1128ed89f078SPeter Maydell     } else {
1129ed89f078SPeter Maydell         env->irq_line_state &= ~linestate_bit;
1130ed89f078SPeter Maydell     }
1131f6530926SEric Auger     kvm_arm_set_irq(cs->cpu_index, KVM_ARM_IRQ_TYPE_CPU, irq_id, !!level);
1132fcf5ef2aSThomas Huth #endif
1133fcf5ef2aSThomas Huth }
1134fcf5ef2aSThomas Huth 
arm_cpu_virtio_is_big_endian(CPUState * cs)1135fcf5ef2aSThomas Huth static bool arm_cpu_virtio_is_big_endian(CPUState *cs)
1136fcf5ef2aSThomas Huth {
1137fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(cs);
1138fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
1139fcf5ef2aSThomas Huth 
1140fcf5ef2aSThomas Huth     cpu_synchronize_state(cs);
1141fcf5ef2aSThomas Huth     return arm_cpu_data_is_big_endian(env);
1142fcf5ef2aSThomas Huth }
1143fcf5ef2aSThomas Huth 
1144a96edb68SPeter Maydell #ifdef CONFIG_TCG
arm_cpu_exec_halt(CPUState * cs)1145fcee3707SPeter Maydell bool arm_cpu_exec_halt(CPUState *cs)
1146a96edb68SPeter Maydell {
1147a96edb68SPeter Maydell     bool leave_halt = cpu_has_work(cs);
1148a96edb68SPeter Maydell 
1149a96edb68SPeter Maydell     if (leave_halt) {
1150a96edb68SPeter Maydell         /* We're about to come out of WFI/WFE: disable the WFxT timer */
1151a96edb68SPeter Maydell         ARMCPU *cpu = ARM_CPU(cs);
1152a96edb68SPeter Maydell         if (cpu->wfxt_timer) {
1153a96edb68SPeter Maydell             timer_del(cpu->wfxt_timer);
1154a96edb68SPeter Maydell         }
1155a96edb68SPeter Maydell     }
1156a96edb68SPeter Maydell     return leave_halt;
1157a96edb68SPeter Maydell }
1158a96edb68SPeter Maydell #endif
1159a96edb68SPeter Maydell 
arm_wfxt_timer_cb(void * opaque)1160a96edb68SPeter Maydell static void arm_wfxt_timer_cb(void *opaque)
1161a96edb68SPeter Maydell {
1162a96edb68SPeter Maydell     ARMCPU *cpu = opaque;
1163a96edb68SPeter Maydell     CPUState *cs = CPU(cpu);
1164a96edb68SPeter Maydell 
1165a96edb68SPeter Maydell     /*
1166a96edb68SPeter Maydell      * We expect the CPU to be halted; this will cause arm_cpu_is_work()
1167a96edb68SPeter Maydell      * to return true (so we will come out of halt even with no other
1168a96edb68SPeter Maydell      * pending interrupt), and the TCG accelerator's cpu_exec_interrupt()
1169a96edb68SPeter Maydell      * function auto-clears the CPU_INTERRUPT_EXITTB flag for us.
1170a96edb68SPeter Maydell      */
1171a96edb68SPeter Maydell     cpu_interrupt(cs, CPU_INTERRUPT_EXITTB);
1172a96edb68SPeter Maydell }
1173fcf5ef2aSThomas Huth #endif
1174fcf5ef2aSThomas Huth 
arm_disas_set_info(CPUState * cpu,disassemble_info * info)1175fcf5ef2aSThomas Huth static void arm_disas_set_info(CPUState *cpu, disassemble_info *info)
1176fcf5ef2aSThomas Huth {
1177fcf5ef2aSThomas Huth     ARMCPU *ac = ARM_CPU(cpu);
1178fcf5ef2aSThomas Huth     CPUARMState *env = &ac->env;
11797bcdbf51SRichard Henderson     bool sctlr_b;
1180fcf5ef2aSThomas Huth 
1181fcf5ef2aSThomas Huth     if (is_a64(env)) {
1182110f6c70SRichard Henderson         info->cap_arch = CS_ARCH_ARM64;
118315fa1a0aSRichard Henderson         info->cap_insn_unit = 4;
118415fa1a0aSRichard Henderson         info->cap_insn_split = 4;
1185110f6c70SRichard Henderson     } else {
1186110f6c70SRichard Henderson         int cap_mode;
1187110f6c70SRichard Henderson         if (env->thumb) {
118815fa1a0aSRichard Henderson             info->cap_insn_unit = 2;
118915fa1a0aSRichard Henderson             info->cap_insn_split = 4;
1190110f6c70SRichard Henderson             cap_mode = CS_MODE_THUMB;
1191fcf5ef2aSThomas Huth         } else {
119215fa1a0aSRichard Henderson             info->cap_insn_unit = 4;
119315fa1a0aSRichard Henderson             info->cap_insn_split = 4;
1194110f6c70SRichard Henderson             cap_mode = CS_MODE_ARM;
1195fcf5ef2aSThomas Huth         }
1196110f6c70SRichard Henderson         if (arm_feature(env, ARM_FEATURE_V8)) {
1197110f6c70SRichard Henderson             cap_mode |= CS_MODE_V8;
1198110f6c70SRichard Henderson         }
1199110f6c70SRichard Henderson         if (arm_feature(env, ARM_FEATURE_M)) {
1200110f6c70SRichard Henderson             cap_mode |= CS_MODE_MCLASS;
1201110f6c70SRichard Henderson         }
1202110f6c70SRichard Henderson         info->cap_arch = CS_ARCH_ARM;
1203110f6c70SRichard Henderson         info->cap_mode = cap_mode;
1204fcf5ef2aSThomas Huth     }
12057bcdbf51SRichard Henderson 
12067bcdbf51SRichard Henderson     sctlr_b = arm_sctlr_b(env);
12077bcdbf51SRichard Henderson     if (bswap_code(sctlr_b)) {
1208ee3eb3a7SMarc-André Lureau #if TARGET_BIG_ENDIAN
1209fcf5ef2aSThomas Huth         info->endian = BFD_ENDIAN_LITTLE;
1210fcf5ef2aSThomas Huth #else
1211fcf5ef2aSThomas Huth         info->endian = BFD_ENDIAN_BIG;
1212fcf5ef2aSThomas Huth #endif
1213fcf5ef2aSThomas Huth     }
1214f7478a92SJulian Brown     info->flags &= ~INSN_ARM_BE32;
12157bcdbf51SRichard Henderson #ifndef CONFIG_USER_ONLY
12167bcdbf51SRichard Henderson     if (sctlr_b) {
1217f7478a92SJulian Brown         info->flags |= INSN_ARM_BE32;
1218f7478a92SJulian Brown     }
12197bcdbf51SRichard Henderson #endif
1220fcf5ef2aSThomas Huth }
1221fcf5ef2aSThomas Huth 
122286480615SPhilippe Mathieu-Daudé #ifdef TARGET_AARCH64
122386480615SPhilippe Mathieu-Daudé 
aarch64_cpu_dump_state(CPUState * cs,FILE * f,int flags)122486480615SPhilippe Mathieu-Daudé static void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags)
122586480615SPhilippe Mathieu-Daudé {
122686480615SPhilippe Mathieu-Daudé     ARMCPU *cpu = ARM_CPU(cs);
122786480615SPhilippe Mathieu-Daudé     CPUARMState *env = &cpu->env;
122886480615SPhilippe Mathieu-Daudé     uint32_t psr = pstate_read(env);
1229a9d84070SRichard Henderson     int i, j;
123086480615SPhilippe Mathieu-Daudé     int el = arm_current_el(env);
1231bde0e60bSPeter Maydell     uint64_t hcr = arm_hcr_el2_eff(env);
123286480615SPhilippe Mathieu-Daudé     const char *ns_status;
12337a867dd5SRichard Henderson     bool sve;
123486480615SPhilippe Mathieu-Daudé 
123586480615SPhilippe Mathieu-Daudé     qemu_fprintf(f, " PC=%016" PRIx64 " ", env->pc);
123686480615SPhilippe Mathieu-Daudé     for (i = 0; i < 32; i++) {
123786480615SPhilippe Mathieu-Daudé         if (i == 31) {
123886480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, " SP=%016" PRIx64 "\n", env->xregs[i]);
123986480615SPhilippe Mathieu-Daudé         } else {
124086480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "X%02d=%016" PRIx64 "%s", i, env->xregs[i],
124186480615SPhilippe Mathieu-Daudé                          (i + 2) % 3 ? " " : "\n");
124286480615SPhilippe Mathieu-Daudé         }
124386480615SPhilippe Mathieu-Daudé     }
124486480615SPhilippe Mathieu-Daudé 
124586480615SPhilippe Mathieu-Daudé     if (arm_feature(env, ARM_FEATURE_EL3) && el != 3) {
124686480615SPhilippe Mathieu-Daudé         ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
124786480615SPhilippe Mathieu-Daudé     } else {
124886480615SPhilippe Mathieu-Daudé         ns_status = "";
124986480615SPhilippe Mathieu-Daudé     }
125086480615SPhilippe Mathieu-Daudé     qemu_fprintf(f, "PSTATE=%08x %c%c%c%c %sEL%d%c",
125186480615SPhilippe Mathieu-Daudé                  psr,
125286480615SPhilippe Mathieu-Daudé                  psr & PSTATE_N ? 'N' : '-',
125386480615SPhilippe Mathieu-Daudé                  psr & PSTATE_Z ? 'Z' : '-',
125486480615SPhilippe Mathieu-Daudé                  psr & PSTATE_C ? 'C' : '-',
125586480615SPhilippe Mathieu-Daudé                  psr & PSTATE_V ? 'V' : '-',
125686480615SPhilippe Mathieu-Daudé                  ns_status,
125786480615SPhilippe Mathieu-Daudé                  el,
125886480615SPhilippe Mathieu-Daudé                  psr & PSTATE_SP ? 'h' : 't');
125986480615SPhilippe Mathieu-Daudé 
12607a867dd5SRichard Henderson     if (cpu_isar_feature(aa64_sme, cpu)) {
12617a867dd5SRichard Henderson         qemu_fprintf(f, "  SVCR=%08" PRIx64 " %c%c",
12627a867dd5SRichard Henderson                      env->svcr,
12637a867dd5SRichard Henderson                      (FIELD_EX64(env->svcr, SVCR, ZA) ? 'Z' : '-'),
12647a867dd5SRichard Henderson                      (FIELD_EX64(env->svcr, SVCR, SM) ? 'S' : '-'));
12657a867dd5SRichard Henderson     }
126686480615SPhilippe Mathieu-Daudé     if (cpu_isar_feature(aa64_bti, cpu)) {
126786480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "  BTYPE=%d", (psr & PSTATE_BTYPE) >> 10);
126886480615SPhilippe Mathieu-Daudé     }
1269bde0e60bSPeter Maydell     qemu_fprintf(f, "%s%s%s",
1270bde0e60bSPeter Maydell                  (hcr & HCR_NV) ? " NV" : "",
1271bde0e60bSPeter Maydell                  (hcr & HCR_NV1) ? " NV1" : "",
1272bde0e60bSPeter Maydell                  (hcr & HCR_NV2) ? " NV2" : "");
127386480615SPhilippe Mathieu-Daudé     if (!(flags & CPU_DUMP_FPU)) {
127486480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "\n");
127586480615SPhilippe Mathieu-Daudé         return;
127686480615SPhilippe Mathieu-Daudé     }
127786480615SPhilippe Mathieu-Daudé     if (fp_exception_el(env, el) != 0) {
127886480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "    FPU disabled\n");
127986480615SPhilippe Mathieu-Daudé         return;
128086480615SPhilippe Mathieu-Daudé     }
128186480615SPhilippe Mathieu-Daudé     qemu_fprintf(f, "     FPCR=%08x FPSR=%08x\n",
128286480615SPhilippe Mathieu-Daudé                  vfp_get_fpcr(env), vfp_get_fpsr(env));
128386480615SPhilippe Mathieu-Daudé 
12847a867dd5SRichard Henderson     if (cpu_isar_feature(aa64_sme, cpu) && FIELD_EX64(env->svcr, SVCR, SM)) {
12857a867dd5SRichard Henderson         sve = sme_exception_el(env, el) == 0;
12867a867dd5SRichard Henderson     } else if (cpu_isar_feature(aa64_sve, cpu)) {
12877a867dd5SRichard Henderson         sve = sve_exception_el(env, el) == 0;
12887a867dd5SRichard Henderson     } else {
12897a867dd5SRichard Henderson         sve = false;
12907a867dd5SRichard Henderson     }
12917a867dd5SRichard Henderson 
12927a867dd5SRichard Henderson     if (sve) {
1293a9d84070SRichard Henderson         int zcr_len = sve_vqm1_for_el(env, el);
129486480615SPhilippe Mathieu-Daudé 
129586480615SPhilippe Mathieu-Daudé         for (i = 0; i <= FFR_PRED_NUM; i++) {
129686480615SPhilippe Mathieu-Daudé             bool eol;
129786480615SPhilippe Mathieu-Daudé             if (i == FFR_PRED_NUM) {
129886480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "FFR=");
129986480615SPhilippe Mathieu-Daudé                 /* It's last, so end the line.  */
130086480615SPhilippe Mathieu-Daudé                 eol = true;
130186480615SPhilippe Mathieu-Daudé             } else {
130286480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "P%02d=", i);
130386480615SPhilippe Mathieu-Daudé                 switch (zcr_len) {
130486480615SPhilippe Mathieu-Daudé                 case 0:
130586480615SPhilippe Mathieu-Daudé                     eol = i % 8 == 7;
130686480615SPhilippe Mathieu-Daudé                     break;
130786480615SPhilippe Mathieu-Daudé                 case 1:
130886480615SPhilippe Mathieu-Daudé                     eol = i % 6 == 5;
130986480615SPhilippe Mathieu-Daudé                     break;
131086480615SPhilippe Mathieu-Daudé                 case 2:
131186480615SPhilippe Mathieu-Daudé                 case 3:
131286480615SPhilippe Mathieu-Daudé                     eol = i % 3 == 2;
131386480615SPhilippe Mathieu-Daudé                     break;
131486480615SPhilippe Mathieu-Daudé                 default:
131586480615SPhilippe Mathieu-Daudé                     /* More than one quadword per predicate.  */
131686480615SPhilippe Mathieu-Daudé                     eol = true;
131786480615SPhilippe Mathieu-Daudé                     break;
131886480615SPhilippe Mathieu-Daudé                 }
131986480615SPhilippe Mathieu-Daudé             }
132086480615SPhilippe Mathieu-Daudé             for (j = zcr_len / 4; j >= 0; j--) {
132186480615SPhilippe Mathieu-Daudé                 int digits;
132286480615SPhilippe Mathieu-Daudé                 if (j * 4 + 4 <= zcr_len + 1) {
132386480615SPhilippe Mathieu-Daudé                     digits = 16;
132486480615SPhilippe Mathieu-Daudé                 } else {
132586480615SPhilippe Mathieu-Daudé                     digits = (zcr_len % 4 + 1) * 4;
132686480615SPhilippe Mathieu-Daudé                 }
132786480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "%0*" PRIx64 "%s", digits,
132886480615SPhilippe Mathieu-Daudé                              env->vfp.pregs[i].p[j],
132986480615SPhilippe Mathieu-Daudé                              j ? ":" : eol ? "\n" : " ");
133086480615SPhilippe Mathieu-Daudé             }
133186480615SPhilippe Mathieu-Daudé         }
133286480615SPhilippe Mathieu-Daudé 
133386480615SPhilippe Mathieu-Daudé         if (zcr_len == 0) {
1334a9d84070SRichard Henderson             /*
1335a9d84070SRichard Henderson              * With vl=16, there are only 37 columns per register,
1336a9d84070SRichard Henderson              * so output two registers per line.
1337a9d84070SRichard Henderson              */
1338a9d84070SRichard Henderson             for (i = 0; i < 32; i++) {
133986480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "Z%02d=%016" PRIx64 ":%016" PRIx64 "%s",
134086480615SPhilippe Mathieu-Daudé                              i, env->vfp.zregs[i].d[1],
134186480615SPhilippe Mathieu-Daudé                              env->vfp.zregs[i].d[0], i & 1 ? "\n" : " ");
1342a9d84070SRichard Henderson             }
134386480615SPhilippe Mathieu-Daudé         } else {
1344a9d84070SRichard Henderson             for (i = 0; i < 32; i++) {
1345a9d84070SRichard Henderson                 qemu_fprintf(f, "Z%02d=", i);
134686480615SPhilippe Mathieu-Daudé                 for (j = zcr_len; j >= 0; j--) {
134786480615SPhilippe Mathieu-Daudé                     qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%s",
134886480615SPhilippe Mathieu-Daudé                                  env->vfp.zregs[i].d[j * 2 + 1],
1349a9d84070SRichard Henderson                                  env->vfp.zregs[i].d[j * 2 + 0],
1350a9d84070SRichard Henderson                                  j ? ":" : "\n");
135186480615SPhilippe Mathieu-Daudé                 }
135286480615SPhilippe Mathieu-Daudé             }
135386480615SPhilippe Mathieu-Daudé         }
135486480615SPhilippe Mathieu-Daudé     } else {
135586480615SPhilippe Mathieu-Daudé         for (i = 0; i < 32; i++) {
135686480615SPhilippe Mathieu-Daudé             uint64_t *q = aa64_vfp_qreg(env, i);
135786480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "Q%02d=%016" PRIx64 ":%016" PRIx64 "%s",
135886480615SPhilippe Mathieu-Daudé                          i, q[1], q[0], (i & 1 ? "\n" : " "));
135986480615SPhilippe Mathieu-Daudé         }
136086480615SPhilippe Mathieu-Daudé     }
1361270bea47SRichard Henderson 
1362270bea47SRichard Henderson     if (cpu_isar_feature(aa64_sme, cpu) &&
1363270bea47SRichard Henderson         FIELD_EX64(env->svcr, SVCR, ZA) &&
1364270bea47SRichard Henderson         sme_exception_el(env, el) == 0) {
1365270bea47SRichard Henderson         int zcr_len = sve_vqm1_for_el_sm(env, el, true);
1366270bea47SRichard Henderson         int svl = (zcr_len + 1) * 16;
1367270bea47SRichard Henderson         int svl_lg10 = svl < 100 ? 2 : 3;
1368270bea47SRichard Henderson 
1369270bea47SRichard Henderson         for (i = 0; i < svl; i++) {
1370270bea47SRichard Henderson             qemu_fprintf(f, "ZA[%0*d]=", svl_lg10, i);
1371270bea47SRichard Henderson             for (j = zcr_len; j >= 0; --j) {
1372270bea47SRichard Henderson                 qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%c",
1373270bea47SRichard Henderson                              env->zarray[i].d[2 * j + 1],
1374270bea47SRichard Henderson                              env->zarray[i].d[2 * j],
1375270bea47SRichard Henderson                              j ? ':' : '\n');
1376270bea47SRichard Henderson             }
1377270bea47SRichard Henderson         }
1378270bea47SRichard Henderson     }
137986480615SPhilippe Mathieu-Daudé }
138086480615SPhilippe Mathieu-Daudé 
138186480615SPhilippe Mathieu-Daudé #else
138286480615SPhilippe Mathieu-Daudé 
aarch64_cpu_dump_state(CPUState * cs,FILE * f,int flags)138386480615SPhilippe Mathieu-Daudé static inline void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags)
138486480615SPhilippe Mathieu-Daudé {
138586480615SPhilippe Mathieu-Daudé     g_assert_not_reached();
138686480615SPhilippe Mathieu-Daudé }
138786480615SPhilippe Mathieu-Daudé 
138886480615SPhilippe Mathieu-Daudé #endif
138986480615SPhilippe Mathieu-Daudé 
arm_cpu_dump_state(CPUState * cs,FILE * f,int flags)139086480615SPhilippe Mathieu-Daudé static void arm_cpu_dump_state(CPUState *cs, FILE *f, int flags)
139186480615SPhilippe Mathieu-Daudé {
139286480615SPhilippe Mathieu-Daudé     ARMCPU *cpu = ARM_CPU(cs);
139386480615SPhilippe Mathieu-Daudé     CPUARMState *env = &cpu->env;
139486480615SPhilippe Mathieu-Daudé     int i;
139586480615SPhilippe Mathieu-Daudé 
139686480615SPhilippe Mathieu-Daudé     if (is_a64(env)) {
139786480615SPhilippe Mathieu-Daudé         aarch64_cpu_dump_state(cs, f, flags);
139886480615SPhilippe Mathieu-Daudé         return;
139986480615SPhilippe Mathieu-Daudé     }
140086480615SPhilippe Mathieu-Daudé 
140186480615SPhilippe Mathieu-Daudé     for (i = 0; i < 16; i++) {
140286480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "R%02d=%08x", i, env->regs[i]);
140386480615SPhilippe Mathieu-Daudé         if ((i % 4) == 3) {
140486480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "\n");
140586480615SPhilippe Mathieu-Daudé         } else {
140686480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, " ");
140786480615SPhilippe Mathieu-Daudé         }
140886480615SPhilippe Mathieu-Daudé     }
140986480615SPhilippe Mathieu-Daudé 
141086480615SPhilippe Mathieu-Daudé     if (arm_feature(env, ARM_FEATURE_M)) {
141186480615SPhilippe Mathieu-Daudé         uint32_t xpsr = xpsr_read(env);
141286480615SPhilippe Mathieu-Daudé         const char *mode;
141386480615SPhilippe Mathieu-Daudé         const char *ns_status = "";
141486480615SPhilippe Mathieu-Daudé 
141586480615SPhilippe Mathieu-Daudé         if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
141686480615SPhilippe Mathieu-Daudé             ns_status = env->v7m.secure ? "S " : "NS ";
141786480615SPhilippe Mathieu-Daudé         }
141886480615SPhilippe Mathieu-Daudé 
141986480615SPhilippe Mathieu-Daudé         if (xpsr & XPSR_EXCP) {
142086480615SPhilippe Mathieu-Daudé             mode = "handler";
142186480615SPhilippe Mathieu-Daudé         } else {
142286480615SPhilippe Mathieu-Daudé             if (env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_NPRIV_MASK) {
142386480615SPhilippe Mathieu-Daudé                 mode = "unpriv-thread";
142486480615SPhilippe Mathieu-Daudé             } else {
142586480615SPhilippe Mathieu-Daudé                 mode = "priv-thread";
142686480615SPhilippe Mathieu-Daudé             }
142786480615SPhilippe Mathieu-Daudé         }
142886480615SPhilippe Mathieu-Daudé 
142986480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "XPSR=%08x %c%c%c%c %c %s%s\n",
143086480615SPhilippe Mathieu-Daudé                      xpsr,
143186480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_N ? 'N' : '-',
143286480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_Z ? 'Z' : '-',
143386480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_C ? 'C' : '-',
143486480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_V ? 'V' : '-',
143586480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_T ? 'T' : 'A',
143686480615SPhilippe Mathieu-Daudé                      ns_status,
143786480615SPhilippe Mathieu-Daudé                      mode);
143886480615SPhilippe Mathieu-Daudé     } else {
143986480615SPhilippe Mathieu-Daudé         uint32_t psr = cpsr_read(env);
144086480615SPhilippe Mathieu-Daudé         const char *ns_status = "";
144186480615SPhilippe Mathieu-Daudé 
144286480615SPhilippe Mathieu-Daudé         if (arm_feature(env, ARM_FEATURE_EL3) &&
144386480615SPhilippe Mathieu-Daudé             (psr & CPSR_M) != ARM_CPU_MODE_MON) {
144486480615SPhilippe Mathieu-Daudé             ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
144586480615SPhilippe Mathieu-Daudé         }
144686480615SPhilippe Mathieu-Daudé 
144786480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "PSR=%08x %c%c%c%c %c %s%s%d\n",
144886480615SPhilippe Mathieu-Daudé                      psr,
144986480615SPhilippe Mathieu-Daudé                      psr & CPSR_N ? 'N' : '-',
145086480615SPhilippe Mathieu-Daudé                      psr & CPSR_Z ? 'Z' : '-',
145186480615SPhilippe Mathieu-Daudé                      psr & CPSR_C ? 'C' : '-',
145286480615SPhilippe Mathieu-Daudé                      psr & CPSR_V ? 'V' : '-',
145386480615SPhilippe Mathieu-Daudé                      psr & CPSR_T ? 'T' : 'A',
145486480615SPhilippe Mathieu-Daudé                      ns_status,
145586480615SPhilippe Mathieu-Daudé                      aarch32_mode_name(psr), (psr & 0x10) ? 32 : 26);
145686480615SPhilippe Mathieu-Daudé     }
145786480615SPhilippe Mathieu-Daudé 
145886480615SPhilippe Mathieu-Daudé     if (flags & CPU_DUMP_FPU) {
145986480615SPhilippe Mathieu-Daudé         int numvfpregs = 0;
1460a6627f5fSRichard Henderson         if (cpu_isar_feature(aa32_simd_r32, cpu)) {
1461a6627f5fSRichard Henderson             numvfpregs = 32;
14627fbc6a40SRichard Henderson         } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
1463a6627f5fSRichard Henderson             numvfpregs = 16;
146486480615SPhilippe Mathieu-Daudé         }
146586480615SPhilippe Mathieu-Daudé         for (i = 0; i < numvfpregs; i++) {
146686480615SPhilippe Mathieu-Daudé             uint64_t v = *aa32_vfp_dreg(env, i);
146786480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "s%02d=%08x s%02d=%08x d%02d=%016" PRIx64 "\n",
146886480615SPhilippe Mathieu-Daudé                          i * 2, (uint32_t)v,
146986480615SPhilippe Mathieu-Daudé                          i * 2 + 1, (uint32_t)(v >> 32),
147086480615SPhilippe Mathieu-Daudé                          i, v);
147186480615SPhilippe Mathieu-Daudé         }
147286480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "FPSCR: %08x\n", vfp_get_fpscr(env));
1473aa291908SPeter Maydell         if (cpu_isar_feature(aa32_mve, cpu)) {
1474aa291908SPeter Maydell             qemu_fprintf(f, "VPR: %08x\n", env->v7m.vpr);
1475aa291908SPeter Maydell         }
147686480615SPhilippe Mathieu-Daudé     }
147786480615SPhilippe Mathieu-Daudé }
147886480615SPhilippe Mathieu-Daudé 
arm_build_mp_affinity(int idx,uint8_t clustersz)1479750245edSRichard Henderson uint64_t arm_build_mp_affinity(int idx, uint8_t clustersz)
148046de5913SIgor Mammedov {
148146de5913SIgor Mammedov     uint32_t Aff1 = idx / clustersz;
148246de5913SIgor Mammedov     uint32_t Aff0 = idx % clustersz;
148346de5913SIgor Mammedov     return (Aff1 << ARM_AFF1_SHIFT) | Aff0;
148446de5913SIgor Mammedov }
148546de5913SIgor Mammedov 
arm_cpu_mp_affinity(ARMCPU * cpu)1486e2d8cf9bSPhilippe Mathieu-Daudé uint64_t arm_cpu_mp_affinity(ARMCPU *cpu)
1487e2d8cf9bSPhilippe Mathieu-Daudé {
1488e2d8cf9bSPhilippe Mathieu-Daudé     return cpu->mp_affinity;
1489e2d8cf9bSPhilippe Mathieu-Daudé }
1490e2d8cf9bSPhilippe Mathieu-Daudé 
arm_cpu_initfn(Object * obj)1491fcf5ef2aSThomas Huth static void arm_cpu_initfn(Object *obj)
1492fcf5ef2aSThomas Huth {
1493fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(obj);
1494fcf5ef2aSThomas Huth 
14955860362dSRichard Henderson     cpu->cp_regs = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1496c27f5d3aSRichard Henderson                                          NULL, g_free);
1497fcf5ef2aSThomas Huth 
1498b5c53d1bSAaron Lindsay     QLIST_INIT(&cpu->pre_el_change_hooks);
149908267487SAaron Lindsay     QLIST_INIT(&cpu->el_change_hooks);
150008267487SAaron Lindsay 
1501b3d52804SRichard Henderson #ifdef CONFIG_USER_ONLY
1502b3d52804SRichard Henderson # ifdef TARGET_AARCH64
1503b3d52804SRichard Henderson     /*
1504e74c0976SRichard Henderson      * The linux kernel defaults to 512-bit for SVE, and 256-bit for SME.
1505e74c0976SRichard Henderson      * These values were chosen to fit within the default signal frame.
1506e74c0976SRichard Henderson      * See documentation for /proc/sys/abi/{sve,sme}_default_vector_length,
1507e74c0976SRichard Henderson      * and our corresponding cpu property.
1508b3d52804SRichard Henderson      */
1509b3d52804SRichard Henderson     cpu->sve_default_vq = 4;
1510e74c0976SRichard Henderson     cpu->sme_default_vq = 2;
1511b3d52804SRichard Henderson # endif
1512b3d52804SRichard Henderson #else
1513fcf5ef2aSThomas Huth     /* Our inbound IRQ and FIQ lines */
1514fcf5ef2aSThomas Huth     if (kvm_enabled()) {
1515b36a32eaSJinjie Ruan         /*
1516b36a32eaSJinjie Ruan          * VIRQ, VFIQ, NMI, VINMI are unused with KVM but we add
1517b36a32eaSJinjie Ruan          * them to maintain the same interface as non-KVM CPUs.
1518fcf5ef2aSThomas Huth          */
1519b36a32eaSJinjie Ruan         qdev_init_gpio_in(DEVICE(cpu), arm_cpu_kvm_set_irq, 6);
1520fcf5ef2aSThomas Huth     } else {
1521b36a32eaSJinjie Ruan         qdev_init_gpio_in(DEVICE(cpu), arm_cpu_set_irq, 6);
1522fcf5ef2aSThomas Huth     }
1523fcf5ef2aSThomas Huth 
1524fcf5ef2aSThomas Huth     qdev_init_gpio_out(DEVICE(cpu), cpu->gt_timer_outputs,
1525fcf5ef2aSThomas Huth                        ARRAY_SIZE(cpu->gt_timer_outputs));
1526aa1b3111SPeter Maydell 
1527aa1b3111SPeter Maydell     qdev_init_gpio_out_named(DEVICE(cpu), &cpu->gicv3_maintenance_interrupt,
1528aa1b3111SPeter Maydell                              "gicv3-maintenance-interrupt", 1);
152907f48730SAndrew Jones     qdev_init_gpio_out_named(DEVICE(cpu), &cpu->pmu_interrupt,
153007f48730SAndrew Jones                              "pmu-interrupt", 1);
1531fcf5ef2aSThomas Huth #endif
1532fcf5ef2aSThomas Huth 
1533fcf5ef2aSThomas Huth     /* DTB consumers generally don't in fact care what the 'compatible'
1534fcf5ef2aSThomas Huth      * string is, so always provide some string and trust that a hypothetical
1535fcf5ef2aSThomas Huth      * picky DTB consumer will also provide a helpful error message.
1536fcf5ef2aSThomas Huth      */
1537fcf5ef2aSThomas Huth     cpu->dtb_compatible = "qemu,unknown";
15380dc71c70SAkihiko Odaki     cpu->psci_version = QEMU_PSCI_VERSION_0_1; /* By default assume PSCI v0.1 */
1539fcf5ef2aSThomas Huth     cpu->kvm_target = QEMU_KVM_ARM_TARGET_NONE;
1540fcf5ef2aSThomas Huth 
15412c9c0bf9SAlexander Graf     if (tcg_enabled() || hvf_enabled()) {
15420dc71c70SAkihiko Odaki         /* TCG and HVF implement PSCI 1.1 */
15430dc71c70SAkihiko Odaki         cpu->psci_version = QEMU_PSCI_VERSION_1_1;
1544fcf5ef2aSThomas Huth     }
1545fcf5ef2aSThomas Huth }
1546fcf5ef2aSThomas Huth 
1547bd8e9ddfSPeter Maydell /*
1548bd8e9ddfSPeter Maydell  * 0 means "unset, use the default value". That default might vary depending
1549bd8e9ddfSPeter Maydell  * on the CPU type, and is set in the realize fn.
1550bd8e9ddfSPeter Maydell  */
155196eec6b2SAndrew Jeffery static Property arm_cpu_gt_cntfrq_property =
1552bd8e9ddfSPeter Maydell             DEFINE_PROP_UINT64("cntfrq", ARMCPU, gt_cntfrq_hz, 0);
155396eec6b2SAndrew Jeffery 
1554fcf5ef2aSThomas Huth static Property arm_cpu_reset_cbar_property =
1555fcf5ef2aSThomas Huth             DEFINE_PROP_UINT64("reset-cbar", ARMCPU, reset_cbar, 0);
1556fcf5ef2aSThomas Huth 
1557fcf5ef2aSThomas Huth static Property arm_cpu_reset_hivecs_property =
1558fcf5ef2aSThomas Huth             DEFINE_PROP_BOOL("reset-hivecs", ARMCPU, reset_hivecs, false);
1559fcf5ef2aSThomas Huth 
156045ca3a14SRichard Henderson #ifndef CONFIG_USER_ONLY
1561c25bd18aSPeter Maydell static Property arm_cpu_has_el2_property =
1562c25bd18aSPeter Maydell             DEFINE_PROP_BOOL("has_el2", ARMCPU, has_el2, true);
1563c25bd18aSPeter Maydell 
1564fcf5ef2aSThomas Huth static Property arm_cpu_has_el3_property =
1565fcf5ef2aSThomas Huth             DEFINE_PROP_BOOL("has_el3", ARMCPU, has_el3, true);
156645ca3a14SRichard Henderson #endif
1567fcf5ef2aSThomas Huth 
15683a062d57SJulian Brown static Property arm_cpu_cfgend_property =
15693a062d57SJulian Brown             DEFINE_PROP_BOOL("cfgend", ARMCPU, cfgend, false);
15703a062d57SJulian Brown 
157197a28b0eSPeter Maydell static Property arm_cpu_has_vfp_property =
157297a28b0eSPeter Maydell             DEFINE_PROP_BOOL("vfp", ARMCPU, has_vfp, true);
157397a28b0eSPeter Maydell 
157442bea956SCédric Le Goater static Property arm_cpu_has_vfp_d32_property =
157542bea956SCédric Le Goater             DEFINE_PROP_BOOL("vfp-d32", ARMCPU, has_vfp_d32, true);
157642bea956SCédric Le Goater 
157797a28b0eSPeter Maydell static Property arm_cpu_has_neon_property =
157897a28b0eSPeter Maydell             DEFINE_PROP_BOOL("neon", ARMCPU, has_neon, true);
157997a28b0eSPeter Maydell 
1580ea90db0aSPeter Maydell static Property arm_cpu_has_dsp_property =
1581ea90db0aSPeter Maydell             DEFINE_PROP_BOOL("dsp", ARMCPU, has_dsp, true);
1582ea90db0aSPeter Maydell 
1583fcf5ef2aSThomas Huth static Property arm_cpu_has_mpu_property =
1584fcf5ef2aSThomas Huth             DEFINE_PROP_BOOL("has-mpu", ARMCPU, has_mpu, true);
1585fcf5ef2aSThomas Huth 
15868d92e26bSPeter Maydell /* This is like DEFINE_PROP_UINT32 but it doesn't set the default value,
15878d92e26bSPeter Maydell  * because the CPU initfn will have already set cpu->pmsav7_dregion to
15888d92e26bSPeter Maydell  * the right value for that particular CPU type, and we don't want
15898d92e26bSPeter Maydell  * to override that with an incorrect constant value.
15908d92e26bSPeter Maydell  */
1591fcf5ef2aSThomas Huth static Property arm_cpu_pmsav7_dregion_property =
15928d92e26bSPeter Maydell             DEFINE_PROP_UNSIGNED_NODEFAULT("pmsav7-dregion", ARMCPU,
15938d92e26bSPeter Maydell                                            pmsav7_dregion,
15948d92e26bSPeter Maydell                                            qdev_prop_uint32, uint32_t);
1595fcf5ef2aSThomas Huth 
arm_get_pmu(Object * obj,Error ** errp)1596ae502508SAndrew Jones static bool arm_get_pmu(Object *obj, Error **errp)
1597ae502508SAndrew Jones {
1598ae502508SAndrew Jones     ARMCPU *cpu = ARM_CPU(obj);
1599ae502508SAndrew Jones 
1600ae502508SAndrew Jones     return cpu->has_pmu;
1601ae502508SAndrew Jones }
1602ae502508SAndrew Jones 
arm_set_pmu(Object * obj,bool value,Error ** errp)1603ae502508SAndrew Jones static void arm_set_pmu(Object *obj, bool value, Error **errp)
1604ae502508SAndrew Jones {
1605ae502508SAndrew Jones     ARMCPU *cpu = ARM_CPU(obj);
1606ae502508SAndrew Jones 
1607ae502508SAndrew Jones     if (value) {
16087d20e681SPhilippe Mathieu-Daudé         if (kvm_enabled() && !kvm_arm_pmu_supported()) {
1609ae502508SAndrew Jones             error_setg(errp, "'pmu' feature not supported by KVM on this host");
1610ae502508SAndrew Jones             return;
1611ae502508SAndrew Jones         }
1612ae502508SAndrew Jones         set_feature(&cpu->env, ARM_FEATURE_PMU);
1613ae502508SAndrew Jones     } else {
1614ae502508SAndrew Jones         unset_feature(&cpu->env, ARM_FEATURE_PMU);
1615ae502508SAndrew Jones     }
1616ae502508SAndrew Jones     cpu->has_pmu = value;
1617ae502508SAndrew Jones }
1618ae502508SAndrew Jones 
gt_cntfrq_period_ns(ARMCPU * cpu)16197def8754SAndrew Jeffery unsigned int gt_cntfrq_period_ns(ARMCPU *cpu)
16207def8754SAndrew Jeffery {
162196eec6b2SAndrew Jeffery     /*
162296eec6b2SAndrew Jeffery      * The exact approach to calculating guest ticks is:
162396eec6b2SAndrew Jeffery      *
162496eec6b2SAndrew Jeffery      *     muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), cpu->gt_cntfrq_hz,
162596eec6b2SAndrew Jeffery      *              NANOSECONDS_PER_SECOND);
162696eec6b2SAndrew Jeffery      *
162796eec6b2SAndrew Jeffery      * We don't do that. Rather we intentionally use integer division
162896eec6b2SAndrew Jeffery      * truncation below and in the caller for the conversion of host monotonic
162996eec6b2SAndrew Jeffery      * time to guest ticks to provide the exact inverse for the semantics of
163096eec6b2SAndrew Jeffery      * the QEMUTimer scale factor. QEMUTimer's scale facter is an integer, so
163196eec6b2SAndrew Jeffery      * it loses precision when representing frequencies where
163296eec6b2SAndrew Jeffery      * `(NANOSECONDS_PER_SECOND % cpu->gt_cntfrq) > 0` holds. Failing to
163396eec6b2SAndrew Jeffery      * provide an exact inverse leads to scheduling timers with negative
163496eec6b2SAndrew Jeffery      * periods, which in turn leads to sticky behaviour in the guest.
163596eec6b2SAndrew Jeffery      *
163696eec6b2SAndrew Jeffery      * Finally, CNTFRQ is effectively capped at 1GHz to ensure our scale factor
163796eec6b2SAndrew Jeffery      * cannot become zero.
163896eec6b2SAndrew Jeffery      */
16397def8754SAndrew Jeffery     return NANOSECONDS_PER_SECOND > cpu->gt_cntfrq_hz ?
16407def8754SAndrew Jeffery       NANOSECONDS_PER_SECOND / cpu->gt_cntfrq_hz : 1;
16417def8754SAndrew Jeffery }
16427def8754SAndrew Jeffery 
arm_cpu_propagate_feature_implications(ARMCPU * cpu)1643b8f7959fSPeter Maydell static void arm_cpu_propagate_feature_implications(ARMCPU *cpu)
1644b8f7959fSPeter Maydell {
1645b8f7959fSPeter Maydell     CPUARMState *env = &cpu->env;
1646b8f7959fSPeter Maydell     bool no_aa32 = false;
1647b8f7959fSPeter Maydell 
1648b8f7959fSPeter Maydell     /*
1649b8f7959fSPeter Maydell      * Some features automatically imply others: set the feature
1650b8f7959fSPeter Maydell      * bits explicitly for these cases.
1651b8f7959fSPeter Maydell      */
1652b8f7959fSPeter Maydell 
1653b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_M)) {
1654b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_PMSA);
1655b8f7959fSPeter Maydell     }
1656b8f7959fSPeter Maydell 
1657b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V8)) {
1658b8f7959fSPeter Maydell         if (arm_feature(env, ARM_FEATURE_M)) {
1659b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_V7);
1660b8f7959fSPeter Maydell         } else {
1661b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_V7VE);
1662b8f7959fSPeter Maydell         }
1663b8f7959fSPeter Maydell     }
1664b8f7959fSPeter Maydell 
1665b8f7959fSPeter Maydell     /*
1666b8f7959fSPeter Maydell      * There exist AArch64 cpus without AArch32 support.  When KVM
1667b8f7959fSPeter Maydell      * queries ID_ISAR0_EL1 on such a host, the value is UNKNOWN.
1668b8f7959fSPeter Maydell      * Similarly, we cannot check ID_AA64PFR0 without AArch64 support.
1669b8f7959fSPeter Maydell      * As a general principle, we also do not make ID register
1670b8f7959fSPeter Maydell      * consistency checks anywhere unless using TCG, because only
1671b8f7959fSPeter Maydell      * for TCG would a consistency-check failure be a QEMU bug.
1672b8f7959fSPeter Maydell      */
1673b8f7959fSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
1674b8f7959fSPeter Maydell         no_aa32 = !cpu_isar_feature(aa64_aa32, cpu);
1675b8f7959fSPeter Maydell     }
1676b8f7959fSPeter Maydell 
1677b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V7VE)) {
1678b8f7959fSPeter Maydell         /*
1679b8f7959fSPeter Maydell          * v7 Virtualization Extensions. In real hardware this implies
1680b8f7959fSPeter Maydell          * EL2 and also the presence of the Security Extensions.
1681b8f7959fSPeter Maydell          * For QEMU, for backwards-compatibility we implement some
1682b8f7959fSPeter Maydell          * CPUs or CPU configs which have no actual EL2 or EL3 but do
1683b8f7959fSPeter Maydell          * include the various other features that V7VE implies.
1684b8f7959fSPeter Maydell          * Presence of EL2 itself is ARM_FEATURE_EL2, and of the
1685b8f7959fSPeter Maydell          * Security Extensions is ARM_FEATURE_EL3.
1686b8f7959fSPeter Maydell          */
1687b8f7959fSPeter Maydell         assert(!tcg_enabled() || no_aa32 ||
1688b8f7959fSPeter Maydell                cpu_isar_feature(aa32_arm_div, cpu));
1689b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_LPAE);
1690b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V7);
1691b8f7959fSPeter Maydell     }
1692b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V7)) {
1693b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_VAPA);
1694b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_THUMB2);
1695b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_MPIDR);
1696b8f7959fSPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
1697b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_V6K);
1698b8f7959fSPeter Maydell         } else {
1699b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_V6);
1700b8f7959fSPeter Maydell         }
1701b8f7959fSPeter Maydell 
1702b8f7959fSPeter Maydell         /*
1703b8f7959fSPeter Maydell          * Always define VBAR for V7 CPUs even if it doesn't exist in
1704b8f7959fSPeter Maydell          * non-EL3 configs. This is needed by some legacy boards.
1705b8f7959fSPeter Maydell          */
1706b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_VBAR);
1707b8f7959fSPeter Maydell     }
1708b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V6K)) {
1709b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V6);
1710b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_MVFR);
1711b8f7959fSPeter Maydell     }
1712b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V6)) {
1713b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V5);
1714b8f7959fSPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
1715b8f7959fSPeter Maydell             assert(!tcg_enabled() || no_aa32 ||
1716b8f7959fSPeter Maydell                    cpu_isar_feature(aa32_jazelle, cpu));
1717b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_AUXCR);
1718b8f7959fSPeter Maydell         }
1719b8f7959fSPeter Maydell     }
1720b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V5)) {
1721b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V4T);
1722b8f7959fSPeter Maydell     }
1723b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_LPAE)) {
1724b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V7MP);
1725b8f7959fSPeter Maydell     }
1726b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
1727b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_CBAR);
1728b8f7959fSPeter Maydell     }
1729b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_THUMB2) &&
1730b8f7959fSPeter Maydell         !arm_feature(env, ARM_FEATURE_M)) {
1731b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_THUMB_DSP);
1732b8f7959fSPeter Maydell     }
1733b8f7959fSPeter Maydell }
1734b8f7959fSPeter Maydell 
arm_cpu_post_init(Object * obj)173551e5ef45SMarc-André Lureau void arm_cpu_post_init(Object *obj)
1736fcf5ef2aSThomas Huth {
1737fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(obj);
1738fcf5ef2aSThomas Huth 
1739b8f7959fSPeter Maydell     /*
1740b8f7959fSPeter Maydell      * Some features imply others. Figure this out now, because we
1741b8f7959fSPeter Maydell      * are going to look at the feature bits in deciding which
1742b8f7959fSPeter Maydell      * properties to add.
1743790a1150SPeter Maydell      */
1744b8f7959fSPeter Maydell     arm_cpu_propagate_feature_implications(cpu);
1745790a1150SPeter Maydell 
1746fcf5ef2aSThomas Huth     if (arm_feature(&cpu->env, ARM_FEATURE_CBAR) ||
1747fcf5ef2aSThomas Huth         arm_feature(&cpu->env, ARM_FEATURE_CBAR_RO)) {
174894d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_cbar_property);
1749fcf5ef2aSThomas Huth     }
1750fcf5ef2aSThomas Huth 
1751fcf5ef2aSThomas Huth     if (!arm_feature(&cpu->env, ARM_FEATURE_M)) {
175294d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_hivecs_property);
1753fcf5ef2aSThomas Huth     }
1754fcf5ef2aSThomas Huth 
1755910e4f24STobias Röhmel     if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
17564a7319b7SEdgar E. Iglesias         object_property_add_uint64_ptr(obj, "rvbar",
17574a7319b7SEdgar E. Iglesias                                        &cpu->rvbar_prop,
17584a7319b7SEdgar E. Iglesias                                        OBJ_PROP_FLAG_READWRITE);
1759fcf5ef2aSThomas Huth     }
1760fcf5ef2aSThomas Huth 
176145ca3a14SRichard Henderson #ifndef CONFIG_USER_ONLY
1762fcf5ef2aSThomas Huth     if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
1763fcf5ef2aSThomas Huth         /* Add the has_el3 state CPU property only if EL3 is allowed.  This will
1764fcf5ef2aSThomas Huth          * prevent "has_el3" from existing on CPUs which cannot support EL3.
1765fcf5ef2aSThomas Huth          */
176694d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el3_property);
1767fcf5ef2aSThomas Huth 
1768fcf5ef2aSThomas Huth         object_property_add_link(obj, "secure-memory",
1769fcf5ef2aSThomas Huth                                  TYPE_MEMORY_REGION,
1770fcf5ef2aSThomas Huth                                  (Object **)&cpu->secure_memory,
1771fcf5ef2aSThomas Huth                                  qdev_prop_allow_set_link_before_realize,
1772d2623129SMarkus Armbruster                                  OBJ_PROP_LINK_STRONG);
1773fcf5ef2aSThomas Huth     }
1774fcf5ef2aSThomas Huth 
1775c25bd18aSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_EL2)) {
177694d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el2_property);
1777c25bd18aSPeter Maydell     }
177845ca3a14SRichard Henderson #endif
1779c25bd18aSPeter Maydell 
1780fcf5ef2aSThomas Huth     if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) {
1781ae502508SAndrew Jones         cpu->has_pmu = true;
1782d2623129SMarkus Armbruster         object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu);
1783fcf5ef2aSThomas Huth     }
1784fcf5ef2aSThomas Huth 
178597a28b0eSPeter Maydell     /*
178697a28b0eSPeter Maydell      * Allow user to turn off VFP and Neon support, but only for TCG --
178797a28b0eSPeter Maydell      * KVM does not currently allow us to lie to the guest about its
178897a28b0eSPeter Maydell      * ID/feature registers, so the guest always sees what the host has.
178997a28b0eSPeter Maydell      */
17904315f7c6SRichard Henderson     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
17914315f7c6SRichard Henderson         if (cpu_isar_feature(aa64_fp_simd, cpu)) {
179297a28b0eSPeter Maydell             cpu->has_vfp = true;
179342bea956SCédric Le Goater             cpu->has_vfp_d32 = true;
17944315f7c6SRichard Henderson             if (tcg_enabled() || qtest_enabled()) {
17954315f7c6SRichard Henderson                 qdev_property_add_static(DEVICE(obj),
17964315f7c6SRichard Henderson                                          &arm_cpu_has_vfp_property);
17974315f7c6SRichard Henderson             }
17984315f7c6SRichard Henderson         }
17994315f7c6SRichard Henderson     } else if (cpu_isar_feature(aa32_vfp, cpu)) {
18004315f7c6SRichard Henderson         cpu->has_vfp = true;
1801185e3fdfSPeter Maydell         if (tcg_enabled() || qtest_enabled()) {
1802185e3fdfSPeter Maydell             qdev_property_add_static(DEVICE(obj),
1803185e3fdfSPeter Maydell                                      &arm_cpu_has_vfp_property);
1804185e3fdfSPeter Maydell         }
18054315f7c6SRichard Henderson         if (cpu_isar_feature(aa32_simd_r32, cpu)) {
18064315f7c6SRichard Henderson             cpu->has_vfp_d32 = true;
180742bea956SCédric Le Goater             /*
180842bea956SCédric Le Goater              * The permitted values of the SIMDReg bits [3:0] on
180942bea956SCédric Le Goater              * Armv8-A are either 0b0000 and 0b0010. On such CPUs,
181042bea956SCédric Le Goater              * make sure that has_vfp_d32 can not be set to false.
181142bea956SCédric Le Goater              */
18124315f7c6SRichard Henderson             if ((tcg_enabled() || qtest_enabled())
18134315f7c6SRichard Henderson                 && !(arm_feature(&cpu->env, ARM_FEATURE_V8)
18144315f7c6SRichard Henderson                      && !arm_feature(&cpu->env, ARM_FEATURE_M))) {
181542bea956SCédric Le Goater                 qdev_property_add_static(DEVICE(obj),
181642bea956SCédric Le Goater                                          &arm_cpu_has_vfp_d32_property);
181742bea956SCédric Le Goater             }
181842bea956SCédric Le Goater         }
181942bea956SCédric Le Goater     }
182042bea956SCédric Le Goater 
182197a28b0eSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_NEON)) {
182297a28b0eSPeter Maydell         cpu->has_neon = true;
182397a28b0eSPeter Maydell         if (!kvm_enabled()) {
182494d912d1SMarc-André Lureau             qdev_property_add_static(DEVICE(obj), &arm_cpu_has_neon_property);
182597a28b0eSPeter Maydell         }
182697a28b0eSPeter Maydell     }
182797a28b0eSPeter Maydell 
1828ea90db0aSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_M) &&
1829ea90db0aSPeter Maydell         arm_feature(&cpu->env, ARM_FEATURE_THUMB_DSP)) {
183094d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_dsp_property);
1831ea90db0aSPeter Maydell     }
1832ea90db0aSPeter Maydell 
1833452a0955SPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_PMSA)) {
183494d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_mpu_property);
1835fcf5ef2aSThomas Huth         if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
1836fcf5ef2aSThomas Huth             qdev_property_add_static(DEVICE(obj),
183794d912d1SMarc-André Lureau                                      &arm_cpu_pmsav7_dregion_property);
1838fcf5ef2aSThomas Huth         }
1839fcf5ef2aSThomas Huth     }
1840fcf5ef2aSThomas Huth 
1841181962fdSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_M_SECURITY)) {
1842181962fdSPeter Maydell         object_property_add_link(obj, "idau", TYPE_IDAU_INTERFACE, &cpu->idau,
1843181962fdSPeter Maydell                                  qdev_prop_allow_set_link_before_realize,
1844d2623129SMarkus Armbruster                                  OBJ_PROP_LINK_STRONG);
1845f9f62e4cSPeter Maydell         /*
1846f9f62e4cSPeter Maydell          * M profile: initial value of the Secure VTOR. We can't just use
1847f9f62e4cSPeter Maydell          * a simple DEFINE_PROP_UINT32 for this because we want to permit
1848f9f62e4cSPeter Maydell          * the property to be set after realize.
1849f9f62e4cSPeter Maydell          */
185064a7b8deSFelipe Franciosi         object_property_add_uint32_ptr(obj, "init-svtor",
185164a7b8deSFelipe Franciosi                                        &cpu->init_svtor,
1852d2623129SMarkus Armbruster                                        OBJ_PROP_FLAG_READWRITE);
1853181962fdSPeter Maydell     }
18547cda2149SPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_M)) {
18557cda2149SPeter Maydell         /*
18567cda2149SPeter Maydell          * Initial value of the NS VTOR (for cores without the Security
18577cda2149SPeter Maydell          * extension, this is the only VTOR)
18587cda2149SPeter Maydell          */
18597cda2149SPeter Maydell         object_property_add_uint32_ptr(obj, "init-nsvtor",
18607cda2149SPeter Maydell                                        &cpu->init_nsvtor,
18617cda2149SPeter Maydell                                        OBJ_PROP_FLAG_READWRITE);
18627cda2149SPeter Maydell     }
1863181962fdSPeter Maydell 
1864bddd892eSPeter Maydell     /* Not DEFINE_PROP_UINT32: we want this to be settable after realize */
1865bddd892eSPeter Maydell     object_property_add_uint32_ptr(obj, "psci-conduit",
1866bddd892eSPeter Maydell                                    &cpu->psci_conduit,
1867bddd892eSPeter Maydell                                    OBJ_PROP_FLAG_READWRITE);
1868bddd892eSPeter Maydell 
186994d912d1SMarc-André Lureau     qdev_property_add_static(DEVICE(obj), &arm_cpu_cfgend_property);
187096eec6b2SAndrew Jeffery 
187196eec6b2SAndrew Jeffery     if (arm_feature(&cpu->env, ARM_FEATURE_GENERIC_TIMER)) {
187294d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(cpu), &arm_cpu_gt_cntfrq_property);
187396eec6b2SAndrew Jeffery     }
18749e6f8d8aSfangying 
18759e6f8d8aSfangying     if (kvm_enabled()) {
1876cac675b5SPhilippe Mathieu-Daudé         kvm_arm_add_vcpu_properties(cpu);
18779e6f8d8aSfangying     }
18788bce44a2SRichard Henderson 
18798bce44a2SRichard Henderson #ifndef CONFIG_USER_ONLY
18808bce44a2SRichard Henderson     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) &&
18818bce44a2SRichard Henderson         cpu_isar_feature(aa64_mte, cpu)) {
18828bce44a2SRichard Henderson         object_property_add_link(obj, "tag-memory",
18838bce44a2SRichard Henderson                                  TYPE_MEMORY_REGION,
18848bce44a2SRichard Henderson                                  (Object **)&cpu->tag_memory,
18858bce44a2SRichard Henderson                                  qdev_prop_allow_set_link_before_realize,
18868bce44a2SRichard Henderson                                  OBJ_PROP_LINK_STRONG);
18878bce44a2SRichard Henderson 
18888bce44a2SRichard Henderson         if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
18898bce44a2SRichard Henderson             object_property_add_link(obj, "secure-tag-memory",
18908bce44a2SRichard Henderson                                      TYPE_MEMORY_REGION,
18918bce44a2SRichard Henderson                                      (Object **)&cpu->secure_tag_memory,
18928bce44a2SRichard Henderson                                      qdev_prop_allow_set_link_before_realize,
18938bce44a2SRichard Henderson                                      OBJ_PROP_LINK_STRONG);
18948bce44a2SRichard Henderson         }
18958bce44a2SRichard Henderson     }
18968bce44a2SRichard Henderson #endif
1897fcf5ef2aSThomas Huth }
1898fcf5ef2aSThomas Huth 
arm_cpu_finalizefn(Object * obj)1899fcf5ef2aSThomas Huth static void arm_cpu_finalizefn(Object *obj)
1900fcf5ef2aSThomas Huth {
1901fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(obj);
190208267487SAaron Lindsay     ARMELChangeHook *hook, *next;
190308267487SAaron Lindsay 
1904fcf5ef2aSThomas Huth     g_hash_table_destroy(cpu->cp_regs);
190508267487SAaron Lindsay 
1906b5c53d1bSAaron Lindsay     QLIST_FOREACH_SAFE(hook, &cpu->pre_el_change_hooks, node, next) {
1907b5c53d1bSAaron Lindsay         QLIST_REMOVE(hook, node);
1908b5c53d1bSAaron Lindsay         g_free(hook);
1909b5c53d1bSAaron Lindsay     }
191008267487SAaron Lindsay     QLIST_FOREACH_SAFE(hook, &cpu->el_change_hooks, node, next) {
191108267487SAaron Lindsay         QLIST_REMOVE(hook, node);
191208267487SAaron Lindsay         g_free(hook);
191308267487SAaron Lindsay     }
19144e7beb0cSAaron Lindsay OS #ifndef CONFIG_USER_ONLY
19154e7beb0cSAaron Lindsay OS     if (cpu->pmu_timer) {
19164e7beb0cSAaron Lindsay OS         timer_free(cpu->pmu_timer);
19174e7beb0cSAaron Lindsay OS     }
1918a96edb68SPeter Maydell     if (cpu->wfxt_timer) {
1919a96edb68SPeter Maydell         timer_free(cpu->wfxt_timer);
1920a96edb68SPeter Maydell     }
19214e7beb0cSAaron Lindsay OS #endif
1922fcf5ef2aSThomas Huth }
1923fcf5ef2aSThomas Huth 
arm_cpu_finalize_features(ARMCPU * cpu,Error ** errp)19240df9142dSAndrew Jones void arm_cpu_finalize_features(ARMCPU *cpu, Error **errp)
19250df9142dSAndrew Jones {
19260df9142dSAndrew Jones     Error *local_err = NULL;
19270df9142dSAndrew Jones 
192807301161SRichard Henderson #ifdef TARGET_AARCH64
19290df9142dSAndrew Jones     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
19300df9142dSAndrew Jones         arm_cpu_sve_finalize(cpu, &local_err);
19310df9142dSAndrew Jones         if (local_err != NULL) {
19320df9142dSAndrew Jones             error_propagate(errp, local_err);
19330df9142dSAndrew Jones             return;
19340df9142dSAndrew Jones         }
1935eb94284dSRichard Henderson 
1936f7767ca3SPeter Maydell         /*
1937f7767ca3SPeter Maydell          * FEAT_SME is not architecturally dependent on FEAT_SVE (unless
1938f7767ca3SPeter Maydell          * FEAT_SME_FA64 is present). However our implementation currently
1939f7767ca3SPeter Maydell          * assumes it, so if the user asked for sve=off then turn off SME also.
1940f7767ca3SPeter Maydell          * (KVM doesn't currently support SME at all.)
1941f7767ca3SPeter Maydell          */
1942f7767ca3SPeter Maydell         if (cpu_isar_feature(aa64_sme, cpu) && !cpu_isar_feature(aa64_sve, cpu)) {
1943f7767ca3SPeter Maydell             object_property_set_bool(OBJECT(cpu), "sme", false, &error_abort);
1944f7767ca3SPeter Maydell         }
1945f7767ca3SPeter Maydell 
1946e74c0976SRichard Henderson         arm_cpu_sme_finalize(cpu, &local_err);
1947e74c0976SRichard Henderson         if (local_err != NULL) {
1948e74c0976SRichard Henderson             error_propagate(errp, local_err);
1949e74c0976SRichard Henderson             return;
1950e74c0976SRichard Henderson         }
1951e74c0976SRichard Henderson 
1952eb94284dSRichard Henderson         arm_cpu_pauth_finalize(cpu, &local_err);
1953eb94284dSRichard Henderson         if (local_err != NULL) {
1954eb94284dSRichard Henderson             error_propagate(errp, local_err);
1955eb94284dSRichard Henderson             return;
1956eb94284dSRichard Henderson         }
195769b2265dSRichard Henderson 
195869b2265dSRichard Henderson         arm_cpu_lpa2_finalize(cpu, &local_err);
195969b2265dSRichard Henderson         if (local_err != NULL) {
196069b2265dSRichard Henderson             error_propagate(errp, local_err);
196169b2265dSRichard Henderson             return;
196269b2265dSRichard Henderson         }
1963eb94284dSRichard Henderson     }
196407301161SRichard Henderson #endif
196568970d1eSAndrew Jones 
196668970d1eSAndrew Jones     if (kvm_enabled()) {
196768970d1eSAndrew Jones         kvm_arm_steal_time_finalize(cpu, &local_err);
196868970d1eSAndrew Jones         if (local_err != NULL) {
196968970d1eSAndrew Jones             error_propagate(errp, local_err);
197068970d1eSAndrew Jones             return;
197168970d1eSAndrew Jones         }
197268970d1eSAndrew Jones     }
19730df9142dSAndrew Jones }
19740df9142dSAndrew Jones 
arm_cpu_realizefn(DeviceState * dev,Error ** errp)1975fcf5ef2aSThomas Huth static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
1976fcf5ef2aSThomas Huth {
1977fcf5ef2aSThomas Huth     CPUState *cs = CPU(dev);
1978fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(dev);
1979fcf5ef2aSThomas Huth     ARMCPUClass *acc = ARM_CPU_GET_CLASS(dev);
1980fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
1981fcf5ef2aSThomas Huth     Error *local_err = NULL;
1982fcf5ef2aSThomas Huth 
1983b94b8c60SPhilippe Mathieu-Daudé #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
1984e607ea39SAnton Johansson     /* Use pc-relative instructions in system-mode */
1985b254c342SPhilippe Mathieu-Daudé     tcg_cflags_set(cs, CF_PCREL);
1986e607ea39SAnton Johansson #endif
1987e607ea39SAnton Johansson 
1988c4487d76SPeter Maydell     /* If we needed to query the host kernel for the CPU features
1989c4487d76SPeter Maydell      * then it's possible that might have failed in the initfn, but
1990c4487d76SPeter Maydell      * this is the first point where we can report it.
1991c4487d76SPeter Maydell      */
1992c4487d76SPeter Maydell     if (cpu->host_cpu_probe_failed) {
1993585df85eSPeter Maydell         if (!kvm_enabled() && !hvf_enabled()) {
1994585df85eSPeter Maydell             error_setg(errp, "The 'host' CPU type can only be used with KVM or HVF");
1995c4487d76SPeter Maydell         } else {
1996c4487d76SPeter Maydell             error_setg(errp, "Failed to retrieve host CPU features");
1997c4487d76SPeter Maydell         }
1998c4487d76SPeter Maydell         return;
1999c4487d76SPeter Maydell     }
2000c4487d76SPeter Maydell 
2001bd8e9ddfSPeter Maydell     if (!cpu->gt_cntfrq_hz) {
2002bd8e9ddfSPeter Maydell         /*
2003f037f5b4SPeter Maydell          * 0 means "the board didn't set a value, use the default". (We also
2004f037f5b4SPeter Maydell          * get here for the CONFIG_USER_ONLY case.)
2005f037f5b4SPeter Maydell          * ARMv8.6 and later CPUs architecturally must use a 1GHz timer; before
2006f037f5b4SPeter Maydell          * that it was an IMPDEF choice, and QEMU initially picked 62.5MHz,
2007f037f5b4SPeter Maydell          * which gives a 16ns tick period.
2008f037f5b4SPeter Maydell          *
2009f037f5b4SPeter Maydell          * We will use the back-compat value:
2010f037f5b4SPeter Maydell          *  - for QEMU CPU types added before we standardized on 1GHz
2011f037f5b4SPeter Maydell          *  - for versioned machine types with a version of 9.0 or earlier
2012bd8e9ddfSPeter Maydell          */
2013f037f5b4SPeter Maydell         if (arm_feature(env, ARM_FEATURE_BACKCOMPAT_CNTFRQ) ||
2014f037f5b4SPeter Maydell             cpu->backcompat_cntfrq) {
2015f037f5b4SPeter Maydell             cpu->gt_cntfrq_hz = GTIMER_BACKCOMPAT_HZ;
2016f037f5b4SPeter Maydell         } else {
2017bd8e9ddfSPeter Maydell             cpu->gt_cntfrq_hz = GTIMER_DEFAULT_HZ;
2018bd8e9ddfSPeter Maydell         }
2019f037f5b4SPeter Maydell     }
2020bd8e9ddfSPeter Maydell 
202195f87565SPeter Maydell #ifndef CONFIG_USER_ONLY
202295f87565SPeter Maydell     /* The NVIC and M-profile CPU are two halves of a single piece of
202395f87565SPeter Maydell      * hardware; trying to use one without the other is a command line
202495f87565SPeter Maydell      * error and will result in segfaults if not caught here.
202595f87565SPeter Maydell      */
202695f87565SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M)) {
202795f87565SPeter Maydell         if (!env->nvic) {
202895f87565SPeter Maydell             error_setg(errp, "This board cannot be used with Cortex-M CPUs");
202995f87565SPeter Maydell             return;
203095f87565SPeter Maydell         }
203195f87565SPeter Maydell     } else {
203295f87565SPeter Maydell         if (env->nvic) {
203395f87565SPeter Maydell             error_setg(errp, "This board can only be used with Cortex-M CPUs");
203495f87565SPeter Maydell             return;
203595f87565SPeter Maydell         }
203695f87565SPeter Maydell     }
2037397cd31fSPeter Maydell 
2038045e5064SAlexander Graf     if (!tcg_enabled() && !qtest_enabled()) {
203949e7f191SPeter Maydell         /*
2040045e5064SAlexander Graf          * We assume that no accelerator except TCG (and the "not really an
2041045e5064SAlexander Graf          * accelerator" qtest) can handle these features, because Arm hardware
2042045e5064SAlexander Graf          * virtualization can't virtualize them.
2043045e5064SAlexander Graf          *
204449e7f191SPeter Maydell          * Catch all the cases which might cause us to create more than one
204549e7f191SPeter Maydell          * address space for the CPU (otherwise we will assert() later in
204649e7f191SPeter Maydell          * cpu_address_space_init()).
204749e7f191SPeter Maydell          */
204849e7f191SPeter Maydell         if (arm_feature(env, ARM_FEATURE_M)) {
204949e7f191SPeter Maydell             error_setg(errp,
2050045e5064SAlexander Graf                        "Cannot enable %s when using an M-profile guest CPU",
2051045e5064SAlexander Graf                        current_accel_name());
205249e7f191SPeter Maydell             return;
205349e7f191SPeter Maydell         }
205449e7f191SPeter Maydell         if (cpu->has_el3) {
205549e7f191SPeter Maydell             error_setg(errp,
2056045e5064SAlexander Graf                        "Cannot enable %s when guest CPU has EL3 enabled",
2057045e5064SAlexander Graf                        current_accel_name());
205849e7f191SPeter Maydell             return;
205949e7f191SPeter Maydell         }
206049e7f191SPeter Maydell         if (cpu->tag_memory) {
206149e7f191SPeter Maydell             error_setg(errp,
2062d009607dSPeter Maydell                        "Cannot enable %s when guest CPUs has MTE enabled",
2063045e5064SAlexander Graf                        current_accel_name());
206449e7f191SPeter Maydell             return;
206549e7f191SPeter Maydell         }
206649e7f191SPeter Maydell     }
206749e7f191SPeter Maydell 
206896eec6b2SAndrew Jeffery     {
2069bd8e9ddfSPeter Maydell         uint64_t scale = gt_cntfrq_period_ns(cpu);
207096eec6b2SAndrew Jeffery 
207196eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_PHYS] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
2072397cd31fSPeter Maydell                                                arm_gt_ptimer_cb, cpu);
207396eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_VIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
2074397cd31fSPeter Maydell                                                arm_gt_vtimer_cb, cpu);
207596eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_HYP] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
2076397cd31fSPeter Maydell                                               arm_gt_htimer_cb, cpu);
207796eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_SEC] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
2078397cd31fSPeter Maydell                                               arm_gt_stimer_cb, cpu);
20798c94b071SRichard Henderson         cpu->gt_timer[GTIMER_HYPVIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
20808c94b071SRichard Henderson                                                   arm_gt_hvtimer_cb, cpu);
208196eec6b2SAndrew Jeffery     }
208295f87565SPeter Maydell #endif
208395f87565SPeter Maydell 
2084fcf5ef2aSThomas Huth     cpu_exec_realizefn(cs, &local_err);
2085fcf5ef2aSThomas Huth     if (local_err != NULL) {
2086fcf5ef2aSThomas Huth         error_propagate(errp, local_err);
2087fcf5ef2aSThomas Huth         return;
2088fcf5ef2aSThomas Huth     }
2089fcf5ef2aSThomas Huth 
20900df9142dSAndrew Jones     arm_cpu_finalize_features(cpu, &local_err);
20910df9142dSAndrew Jones     if (local_err != NULL) {
20920df9142dSAndrew Jones         error_propagate(errp, local_err);
20930df9142dSAndrew Jones         return;
20940df9142dSAndrew Jones     }
20950df9142dSAndrew Jones 
20969719f125SJohn Högberg #ifdef CONFIG_USER_ONLY
20979719f125SJohn Högberg     /*
20989719f125SJohn Högberg      * User mode relies on IC IVAU instructions to catch modification of
20999719f125SJohn Högberg      * dual-mapped code.
21009719f125SJohn Högberg      *
21019719f125SJohn Högberg      * Clear CTR_EL0.DIC to ensure that software that honors these flags uses
21029719f125SJohn Högberg      * IC IVAU even if the emulated processor does not normally require it.
21039719f125SJohn Högberg      */
21049719f125SJohn Högberg     cpu->ctr = FIELD_DP64(cpu->ctr, CTR_EL0, DIC, 0);
21059719f125SJohn Högberg #endif
21069719f125SJohn Högberg 
210797a28b0eSPeter Maydell     if (arm_feature(env, ARM_FEATURE_AARCH64) &&
210897a28b0eSPeter Maydell         cpu->has_vfp != cpu->has_neon) {
210997a28b0eSPeter Maydell         /*
211097a28b0eSPeter Maydell          * This is an architectural requirement for AArch64; AArch32 is
211197a28b0eSPeter Maydell          * more flexible and permits VFP-no-Neon and Neon-no-VFP.
211297a28b0eSPeter Maydell          */
211397a28b0eSPeter Maydell         error_setg(errp,
211497a28b0eSPeter Maydell                    "AArch64 CPUs must have both VFP and Neon or neither");
211597a28b0eSPeter Maydell         return;
211697a28b0eSPeter Maydell     }
211797a28b0eSPeter Maydell 
211842bea956SCédric Le Goater     if (cpu->has_vfp_d32 != cpu->has_neon) {
211942bea956SCédric Le Goater         error_setg(errp, "ARM CPUs must have both VFP-D32 and Neon or neither");
212042bea956SCédric Le Goater         return;
212142bea956SCédric Le Goater     }
212242bea956SCédric Le Goater 
212342bea956SCédric Le Goater    if (!cpu->has_vfp_d32) {
212442bea956SCédric Le Goater         uint32_t u;
212542bea956SCédric Le Goater 
212642bea956SCédric Le Goater         u = cpu->isar.mvfr0;
212742bea956SCédric Le Goater         u = FIELD_DP32(u, MVFR0, SIMDREG, 1); /* 16 registers */
212842bea956SCédric Le Goater         cpu->isar.mvfr0 = u;
212942bea956SCédric Le Goater     }
213042bea956SCédric Le Goater 
213197a28b0eSPeter Maydell     if (!cpu->has_vfp) {
213297a28b0eSPeter Maydell         uint64_t t;
213397a28b0eSPeter Maydell         uint32_t u;
213497a28b0eSPeter Maydell 
213597a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar1;
213697a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR1, JSCVT, 0);
213797a28b0eSPeter Maydell         cpu->isar.id_aa64isar1 = t;
213897a28b0eSPeter Maydell 
213997a28b0eSPeter Maydell         t = cpu->isar.id_aa64pfr0;
214097a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64PFR0, FP, 0xf);
214197a28b0eSPeter Maydell         cpu->isar.id_aa64pfr0 = t;
214297a28b0eSPeter Maydell 
214397a28b0eSPeter Maydell         u = cpu->isar.id_isar6;
214497a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR6, JSCVT, 0);
21453c93dfa4SRichard Henderson         u = FIELD_DP32(u, ID_ISAR6, BF16, 0);
214697a28b0eSPeter Maydell         cpu->isar.id_isar6 = u;
214797a28b0eSPeter Maydell 
214897a28b0eSPeter Maydell         u = cpu->isar.mvfr0;
214997a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPSP, 0);
215097a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPDP, 0);
215197a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPDIVIDE, 0);
215297a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPSQRT, 0);
215397a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPROUND, 0);
2154532a3af5SPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
2155532a3af5SPeter Maydell             u = FIELD_DP32(u, MVFR0, FPTRAP, 0);
2156532a3af5SPeter Maydell             u = FIELD_DP32(u, MVFR0, FPSHVEC, 0);
2157532a3af5SPeter Maydell         }
215897a28b0eSPeter Maydell         cpu->isar.mvfr0 = u;
215997a28b0eSPeter Maydell 
216097a28b0eSPeter Maydell         u = cpu->isar.mvfr1;
216197a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR1, FPFTZ, 0);
216297a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR1, FPDNAN, 0);
216397a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR1, FPHP, 0);
2164532a3af5SPeter Maydell         if (arm_feature(env, ARM_FEATURE_M)) {
2165532a3af5SPeter Maydell             u = FIELD_DP32(u, MVFR1, FP16, 0);
2166532a3af5SPeter Maydell         }
216797a28b0eSPeter Maydell         cpu->isar.mvfr1 = u;
216897a28b0eSPeter Maydell 
216997a28b0eSPeter Maydell         u = cpu->isar.mvfr2;
217097a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR2, FPMISC, 0);
217197a28b0eSPeter Maydell         cpu->isar.mvfr2 = u;
217297a28b0eSPeter Maydell     }
217397a28b0eSPeter Maydell 
217497a28b0eSPeter Maydell     if (!cpu->has_neon) {
217597a28b0eSPeter Maydell         uint64_t t;
217697a28b0eSPeter Maydell         uint32_t u;
217797a28b0eSPeter Maydell 
217897a28b0eSPeter Maydell         unset_feature(env, ARM_FEATURE_NEON);
217997a28b0eSPeter Maydell 
218097a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar0;
2181eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, AES, 0);
2182eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SHA1, 0);
2183eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SHA2, 0);
2184eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SHA3, 0);
2185eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SM3, 0);
2186eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SM4, 0);
218797a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR0, DP, 0);
218897a28b0eSPeter Maydell         cpu->isar.id_aa64isar0 = t;
218997a28b0eSPeter Maydell 
219097a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar1;
219197a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR1, FCMA, 0);
21923c93dfa4SRichard Henderson         t = FIELD_DP64(t, ID_AA64ISAR1, BF16, 0);
2193f8680aaaSRichard Henderson         t = FIELD_DP64(t, ID_AA64ISAR1, I8MM, 0);
219497a28b0eSPeter Maydell         cpu->isar.id_aa64isar1 = t;
219597a28b0eSPeter Maydell 
219697a28b0eSPeter Maydell         t = cpu->isar.id_aa64pfr0;
219797a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64PFR0, ADVSIMD, 0xf);
219897a28b0eSPeter Maydell         cpu->isar.id_aa64pfr0 = t;
219997a28b0eSPeter Maydell 
220097a28b0eSPeter Maydell         u = cpu->isar.id_isar5;
2201eb851c11SDamien Hedde         u = FIELD_DP32(u, ID_ISAR5, AES, 0);
2202eb851c11SDamien Hedde         u = FIELD_DP32(u, ID_ISAR5, SHA1, 0);
2203eb851c11SDamien Hedde         u = FIELD_DP32(u, ID_ISAR5, SHA2, 0);
220497a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR5, RDM, 0);
220597a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR5, VCMA, 0);
220697a28b0eSPeter Maydell         cpu->isar.id_isar5 = u;
220797a28b0eSPeter Maydell 
220897a28b0eSPeter Maydell         u = cpu->isar.id_isar6;
220997a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR6, DP, 0);
221097a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR6, FHM, 0);
22113c93dfa4SRichard Henderson         u = FIELD_DP32(u, ID_ISAR6, BF16, 0);
2212f8680aaaSRichard Henderson         u = FIELD_DP32(u, ID_ISAR6, I8MM, 0);
221397a28b0eSPeter Maydell         cpu->isar.id_isar6 = u;
221497a28b0eSPeter Maydell 
2215532a3af5SPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
221697a28b0eSPeter Maydell             u = cpu->isar.mvfr1;
221797a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDLS, 0);
221897a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDINT, 0);
221997a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDSP, 0);
222097a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDHP, 0);
222197a28b0eSPeter Maydell             cpu->isar.mvfr1 = u;
222297a28b0eSPeter Maydell 
222397a28b0eSPeter Maydell             u = cpu->isar.mvfr2;
222497a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR2, SIMDMISC, 0);
222597a28b0eSPeter Maydell             cpu->isar.mvfr2 = u;
222697a28b0eSPeter Maydell         }
2227532a3af5SPeter Maydell     }
222897a28b0eSPeter Maydell 
222997a28b0eSPeter Maydell     if (!cpu->has_neon && !cpu->has_vfp) {
223097a28b0eSPeter Maydell         uint64_t t;
223197a28b0eSPeter Maydell         uint32_t u;
223297a28b0eSPeter Maydell 
223397a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar0;
223497a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR0, FHM, 0);
223597a28b0eSPeter Maydell         cpu->isar.id_aa64isar0 = t;
223697a28b0eSPeter Maydell 
223797a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar1;
223897a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR1, FRINTTS, 0);
223997a28b0eSPeter Maydell         cpu->isar.id_aa64isar1 = t;
224097a28b0eSPeter Maydell 
224197a28b0eSPeter Maydell         u = cpu->isar.mvfr0;
224297a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, SIMDREG, 0);
224397a28b0eSPeter Maydell         cpu->isar.mvfr0 = u;
2244c52881bbSRichard Henderson 
2245c52881bbSRichard Henderson         /* Despite the name, this field covers both VFP and Neon */
2246c52881bbSRichard Henderson         u = cpu->isar.mvfr1;
2247c52881bbSRichard Henderson         u = FIELD_DP32(u, MVFR1, SIMDFMAC, 0);
2248c52881bbSRichard Henderson         cpu->isar.mvfr1 = u;
224997a28b0eSPeter Maydell     }
225097a28b0eSPeter Maydell 
2251ea90db0aSPeter Maydell     if (arm_feature(env, ARM_FEATURE_M) && !cpu->has_dsp) {
2252ea90db0aSPeter Maydell         uint32_t u;
2253ea90db0aSPeter Maydell 
2254ea90db0aSPeter Maydell         unset_feature(env, ARM_FEATURE_THUMB_DSP);
2255ea90db0aSPeter Maydell 
2256ea90db0aSPeter Maydell         u = cpu->isar.id_isar1;
2257ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR1, EXTEND, 1);
2258ea90db0aSPeter Maydell         cpu->isar.id_isar1 = u;
2259ea90db0aSPeter Maydell 
2260ea90db0aSPeter Maydell         u = cpu->isar.id_isar2;
2261ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR2, MULTU, 1);
2262ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR2, MULTS, 1);
2263ea90db0aSPeter Maydell         cpu->isar.id_isar2 = u;
2264ea90db0aSPeter Maydell 
2265ea90db0aSPeter Maydell         u = cpu->isar.id_isar3;
2266ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR3, SIMD, 1);
2267ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR3, SATURATE, 0);
2268ea90db0aSPeter Maydell         cpu->isar.id_isar3 = u;
2269ea90db0aSPeter Maydell     }
2270ea90db0aSPeter Maydell 
2271fcf5ef2aSThomas Huth 
2272ea7ac69dSPeter Maydell     /*
2273ea7ac69dSPeter Maydell      * We rely on no XScale CPU having VFP so we can use the same bits in the
2274ea7ac69dSPeter Maydell      * TB flags field for VECSTRIDE and XSCALE_CPAR.
2275ea7ac69dSPeter Maydell      */
2276ee1004bbSPhilippe Mathieu-Daudé     assert(arm_feature(env, ARM_FEATURE_AARCH64) ||
22777d63183fSRichard Henderson            !cpu_isar_feature(aa32_vfp_simd, cpu) ||
22787d63183fSRichard Henderson            !arm_feature(env, ARM_FEATURE_XSCALE));
2279ea7ac69dSPeter Maydell 
2280a575230fSRichard Henderson #ifndef CONFIG_USER_ONLY
2281a575230fSRichard Henderson     {
2282a575230fSRichard Henderson         int pagebits;
2283fcf5ef2aSThomas Huth         if (arm_feature(env, ARM_FEATURE_V7) &&
2284fcf5ef2aSThomas Huth             !arm_feature(env, ARM_FEATURE_M) &&
2285452a0955SPeter Maydell             !arm_feature(env, ARM_FEATURE_PMSA)) {
2286a575230fSRichard Henderson             /*
2287a575230fSRichard Henderson              * v7VMSA drops support for the old ARMv5 tiny pages,
2288a575230fSRichard Henderson              * so we can use 4K pages.
2289fcf5ef2aSThomas Huth              */
2290fcf5ef2aSThomas Huth             pagebits = 12;
2291fcf5ef2aSThomas Huth         } else {
2292a575230fSRichard Henderson             /*
2293a575230fSRichard Henderson              * For CPUs which might have tiny 1K pages, or which have an
2294fcf5ef2aSThomas Huth              * MPU and might have small region sizes, stick with 1K pages.
2295fcf5ef2aSThomas Huth              */
2296fcf5ef2aSThomas Huth             pagebits = 10;
2297fcf5ef2aSThomas Huth         }
2298fcf5ef2aSThomas Huth         if (!set_preferred_target_page_bits(pagebits)) {
2299a575230fSRichard Henderson             /*
2300a575230fSRichard Henderson              * This can only ever happen for hotplugging a CPU, or if
2301fcf5ef2aSThomas Huth              * the board code incorrectly creates a CPU which it has
2302fcf5ef2aSThomas Huth              * promised via minimum_page_size that it will not.
2303fcf5ef2aSThomas Huth              */
2304a575230fSRichard Henderson             error_setg(errp, "This CPU requires a smaller page size "
2305a575230fSRichard Henderson                        "than the system is using");
2306fcf5ef2aSThomas Huth             return;
2307fcf5ef2aSThomas Huth         }
2308a575230fSRichard Henderson     }
2309a575230fSRichard Henderson #endif
2310fcf5ef2aSThomas Huth 
2311fcf5ef2aSThomas Huth     /* This cpu-id-to-MPIDR affinity is used only for TCG; KVM will override it.
2312fcf5ef2aSThomas Huth      * We don't support setting cluster ID ([16..23]) (known as Aff2
2313fcf5ef2aSThomas Huth      * in later ARM ARM versions), or any of the higher affinity level fields,
2314fcf5ef2aSThomas Huth      * so these bits always RAZ.
2315fcf5ef2aSThomas Huth      */
2316fcf5ef2aSThomas Huth     if (cpu->mp_affinity == ARM64_AFFINITY_INVALID) {
2317750245edSRichard Henderson         cpu->mp_affinity = arm_build_mp_affinity(cs->cpu_index,
231846de5913SIgor Mammedov                                                  ARM_DEFAULT_CPUS_PER_CLUSTER);
2319fcf5ef2aSThomas Huth     }
2320fcf5ef2aSThomas Huth 
2321fcf5ef2aSThomas Huth     if (cpu->reset_hivecs) {
2322fcf5ef2aSThomas Huth             cpu->reset_sctlr |= (1 << 13);
2323fcf5ef2aSThomas Huth     }
2324fcf5ef2aSThomas Huth 
23253a062d57SJulian Brown     if (cpu->cfgend) {
2326ee1004bbSPhilippe Mathieu-Daudé         if (arm_feature(env, ARM_FEATURE_V7)) {
23273a062d57SJulian Brown             cpu->reset_sctlr |= SCTLR_EE;
23283a062d57SJulian Brown         } else {
23293a062d57SJulian Brown             cpu->reset_sctlr |= SCTLR_B;
23303a062d57SJulian Brown         }
23313a062d57SJulian Brown     }
23323a062d57SJulian Brown 
233340188188SPeter Maydell     if (!arm_feature(env, ARM_FEATURE_M) && !cpu->has_el3) {
2334fcf5ef2aSThomas Huth         /* If the has_el3 CPU property is disabled then we need to disable the
2335fcf5ef2aSThomas Huth          * feature.
2336fcf5ef2aSThomas Huth          */
2337fcf5ef2aSThomas Huth         unset_feature(env, ARM_FEATURE_EL3);
2338fcf5ef2aSThomas Huth 
2339b13c91c0SRichard Henderson         /*
2340b13c91c0SRichard Henderson          * Disable the security extension feature bits in the processor
2341b13c91c0SRichard Henderson          * feature registers as well.
2342fcf5ef2aSThomas Huth          */
2343b13c91c0SRichard Henderson         cpu->isar.id_pfr1 = FIELD_DP32(cpu->isar.id_pfr1, ID_PFR1, SECURITY, 0);
2344033a4f15SRichard Henderson         cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, COPSDBG, 0);
2345b13c91c0SRichard Henderson         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2346b13c91c0SRichard Henderson                                            ID_AA64PFR0, EL3, 0);
2347b9f335c2SRichard Henderson 
2348b9f335c2SRichard Henderson         /* Disable the realm management extension, which requires EL3. */
2349b9f335c2SRichard Henderson         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2350b9f335c2SRichard Henderson                                            ID_AA64PFR0, RME, 0);
2351fcf5ef2aSThomas Huth     }
2352fcf5ef2aSThomas Huth 
2353c25bd18aSPeter Maydell     if (!cpu->has_el2) {
2354c25bd18aSPeter Maydell         unset_feature(env, ARM_FEATURE_EL2);
2355c25bd18aSPeter Maydell     }
2356c25bd18aSPeter Maydell 
2357d6f02ce3SWei Huang     if (!cpu->has_pmu) {
2358fcf5ef2aSThomas Huth         unset_feature(env, ARM_FEATURE_PMU);
235957a4a11bSAaron Lindsay     }
236057a4a11bSAaron Lindsay     if (arm_feature(env, ARM_FEATURE_PMU)) {
2361bf8d0969SAaron Lindsay OS         pmu_init(cpu);
236257a4a11bSAaron Lindsay 
236357a4a11bSAaron Lindsay         if (!kvm_enabled()) {
2364033614c4SAaron Lindsay             arm_register_pre_el_change_hook(cpu, &pmu_pre_el_change, 0);
2365033614c4SAaron Lindsay             arm_register_el_change_hook(cpu, &pmu_post_el_change, 0);
2366fcf5ef2aSThomas Huth         }
23674e7beb0cSAaron Lindsay OS 
23684e7beb0cSAaron Lindsay OS #ifndef CONFIG_USER_ONLY
23694e7beb0cSAaron Lindsay OS         cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, arm_pmu_timer_cb,
23704e7beb0cSAaron Lindsay OS                 cpu);
23714e7beb0cSAaron Lindsay OS #endif
237257a4a11bSAaron Lindsay     } else {
23732a609df8SPeter Maydell         cpu->isar.id_aa64dfr0 =
23742a609df8SPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMUVER, 0);
2375a6179538SPeter Maydell         cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, PERFMON, 0);
237657a4a11bSAaron Lindsay         cpu->pmceid0 = 0;
237757a4a11bSAaron Lindsay         cpu->pmceid1 = 0;
237857a4a11bSAaron Lindsay     }
2379fcf5ef2aSThomas Huth 
2380fcf5ef2aSThomas Huth     if (!arm_feature(env, ARM_FEATURE_EL2)) {
2381b13c91c0SRichard Henderson         /*
2382b13c91c0SRichard Henderson          * Disable the hypervisor feature bits in the processor feature
2383b13c91c0SRichard Henderson          * registers if we don't have EL2.
2384fcf5ef2aSThomas Huth          */
2385b13c91c0SRichard Henderson         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2386b13c91c0SRichard Henderson                                            ID_AA64PFR0, EL2, 0);
2387b13c91c0SRichard Henderson         cpu->isar.id_pfr1 = FIELD_DP32(cpu->isar.id_pfr1,
2388b13c91c0SRichard Henderson                                        ID_PFR1, VIRTUALIZATION, 0);
2389fcf5ef2aSThomas Huth     }
2390fcf5ef2aSThomas Huth 
23917134cb07SRichard Henderson     if (cpu_isar_feature(aa64_mte, cpu)) {
23927134cb07SRichard Henderson         /*
23937134cb07SRichard Henderson          * The architectural range of GM blocksize is 2-6, however qemu
23947134cb07SRichard Henderson          * doesn't support blocksize of 2 (see HELPER(ldgm)).
23957134cb07SRichard Henderson          */
23967134cb07SRichard Henderson         if (tcg_enabled()) {
23977134cb07SRichard Henderson             assert(cpu->gm_blocksize >= 3 && cpu->gm_blocksize <= 6);
23987134cb07SRichard Henderson         }
23997134cb07SRichard Henderson 
24006f4e1405SRichard Henderson #ifndef CONFIG_USER_ONLY
24016f4e1405SRichard Henderson         /*
2402918d0de0SCornelia Huck          * If we run with TCG and do not have tag-memory provided by
2403918d0de0SCornelia Huck          * the machine, then reduce MTE support to instructions enabled at EL0.
2404cd305b5fSRichard Henderson          * This matches Cortex-A710 BROADCASTMTE input being LOW.
24056f4e1405SRichard Henderson          */
2406918d0de0SCornelia Huck         if (tcg_enabled() && cpu->tag_memory == NULL) {
24076f4e1405SRichard Henderson             cpu->isar.id_aa64pfr1 =
2408cd305b5fSRichard Henderson                 FIELD_DP64(cpu->isar.id_aa64pfr1, ID_AA64PFR1, MTE, 1);
24096f4e1405SRichard Henderson         }
2410918d0de0SCornelia Huck 
2411918d0de0SCornelia Huck         /*
2412918d0de0SCornelia Huck          * If MTE is supported by the host, however it should not be
2413918d0de0SCornelia Huck          * enabled on the guest (i.e mte=off), clear guest's MTE bits."
2414918d0de0SCornelia Huck          */
2415918d0de0SCornelia Huck         if (kvm_enabled() && !cpu->kvm_mte) {
2416918d0de0SCornelia Huck                 FIELD_DP64(cpu->isar.id_aa64pfr1, ID_AA64PFR1, MTE, 0);
2417918d0de0SCornelia Huck         }
24186f4e1405SRichard Henderson #endif
24197134cb07SRichard Henderson     }
24206f4e1405SRichard Henderson 
2421a96edb68SPeter Maydell #ifndef CONFIG_USER_ONLY
2422a96edb68SPeter Maydell     if (tcg_enabled() && cpu_isar_feature(aa64_wfxt, cpu)) {
2423a96edb68SPeter Maydell         cpu->wfxt_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
2424a96edb68SPeter Maydell                                        arm_wfxt_timer_cb, cpu);
2425a96edb68SPeter Maydell     }
2426a96edb68SPeter Maydell #endif
2427a96edb68SPeter Maydell 
24282daf518dSPeter Maydell     if (tcg_enabled()) {
24292daf518dSPeter Maydell         /*
24307d8c283eSPeter Maydell          * Don't report some architectural features in the ID registers
24317d8c283eSPeter Maydell          * where TCG does not yet implement it (not even a minimal
24327d8c283eSPeter Maydell          * stub version). This avoids guests falling over when they
24337d8c283eSPeter Maydell          * try to access the non-existent system registers for them.
24342daf518dSPeter Maydell          */
24357d8c283eSPeter Maydell         /* FEAT_SPE (Statistical Profiling Extension) */
24362daf518dSPeter Maydell         cpu->isar.id_aa64dfr0 =
24372daf518dSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMSVER, 0);
24383d5f45ecSRichard Henderson         /* FEAT_TRBE (Trace Buffer Extension) */
24393d5f45ecSRichard Henderson         cpu->isar.id_aa64dfr0 =
24403d5f45ecSRichard Henderson             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEBUFFER, 0);
24417d8c283eSPeter Maydell         /* FEAT_TRF (Self-hosted Trace Extension) */
24427d8c283eSPeter Maydell         cpu->isar.id_aa64dfr0 =
24437d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEFILT, 0);
24447d8c283eSPeter Maydell         cpu->isar.id_dfr0 =
24457d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, TRACEFILT, 0);
24467d8c283eSPeter Maydell         /* Trace Macrocell system register access */
24477d8c283eSPeter Maydell         cpu->isar.id_aa64dfr0 =
24487d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEVER, 0);
24497d8c283eSPeter Maydell         cpu->isar.id_dfr0 =
24507d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, COPTRC, 0);
24517d8c283eSPeter Maydell         /* Memory mapped trace */
24527d8c283eSPeter Maydell         cpu->isar.id_dfr0 =
24537d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, MMAPTRC, 0);
24547d8c283eSPeter Maydell         /* FEAT_AMU (Activity Monitors Extension) */
24557d8c283eSPeter Maydell         cpu->isar.id_aa64pfr0 =
24567d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64pfr0, ID_AA64PFR0, AMU, 0);
24577d8c283eSPeter Maydell         cpu->isar.id_pfr0 =
24587d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_pfr0, ID_PFR0, AMU, 0);
24597d8c283eSPeter Maydell         /* FEAT_MPAM (Memory Partitioning and Monitoring Extension) */
24607d8c283eSPeter Maydell         cpu->isar.id_aa64pfr0 =
24617d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64pfr0, ID_AA64PFR0, MPAM, 0);
24622daf518dSPeter Maydell     }
24632daf518dSPeter Maydell 
2464f50cd314SPeter Maydell     /* MPU can be configured out of a PMSA CPU either by setting has-mpu
2465f50cd314SPeter Maydell      * to false or by setting pmsav7-dregion to 0.
2466f50cd314SPeter Maydell      */
2467761c4642STobias Röhmel     if (!cpu->has_mpu || cpu->pmsav7_dregion == 0) {
2468f50cd314SPeter Maydell         cpu->has_mpu = false;
2469761c4642STobias Röhmel         cpu->pmsav7_dregion = 0;
2470761c4642STobias Röhmel         cpu->pmsav8r_hdregion = 0;
2471fcf5ef2aSThomas Huth     }
2472fcf5ef2aSThomas Huth 
2473452a0955SPeter Maydell     if (arm_feature(env, ARM_FEATURE_PMSA) &&
2474fcf5ef2aSThomas Huth         arm_feature(env, ARM_FEATURE_V7)) {
2475fcf5ef2aSThomas Huth         uint32_t nr = cpu->pmsav7_dregion;
2476fcf5ef2aSThomas Huth 
2477fcf5ef2aSThomas Huth         if (nr > 0xff) {
2478fcf5ef2aSThomas Huth             error_setg(errp, "PMSAv7 MPU #regions invalid %" PRIu32, nr);
2479fcf5ef2aSThomas Huth             return;
2480fcf5ef2aSThomas Huth         }
2481fcf5ef2aSThomas Huth 
2482fcf5ef2aSThomas Huth         if (nr) {
24830e1a46bbSPeter Maydell             if (arm_feature(env, ARM_FEATURE_V8)) {
24840e1a46bbSPeter Maydell                 /* PMSAv8 */
248562c58ee0SPeter Maydell                 env->pmsav8.rbar[M_REG_NS] = g_new0(uint32_t, nr);
248662c58ee0SPeter Maydell                 env->pmsav8.rlar[M_REG_NS] = g_new0(uint32_t, nr);
248762c58ee0SPeter Maydell                 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
248862c58ee0SPeter Maydell                     env->pmsav8.rbar[M_REG_S] = g_new0(uint32_t, nr);
248962c58ee0SPeter Maydell                     env->pmsav8.rlar[M_REG_S] = g_new0(uint32_t, nr);
249062c58ee0SPeter Maydell                 }
24910e1a46bbSPeter Maydell             } else {
2492fcf5ef2aSThomas Huth                 env->pmsav7.drbar = g_new0(uint32_t, nr);
2493fcf5ef2aSThomas Huth                 env->pmsav7.drsr = g_new0(uint32_t, nr);
2494fcf5ef2aSThomas Huth                 env->pmsav7.dracr = g_new0(uint32_t, nr);
2495fcf5ef2aSThomas Huth             }
2496fcf5ef2aSThomas Huth         }
2497761c4642STobias Röhmel 
2498761c4642STobias Röhmel         if (cpu->pmsav8r_hdregion > 0xff) {
2499761c4642STobias Röhmel             error_setg(errp, "PMSAv8 MPU EL2 #regions invalid %" PRIu32,
2500761c4642STobias Röhmel                               cpu->pmsav8r_hdregion);
2501761c4642STobias Röhmel             return;
2502761c4642STobias Röhmel         }
2503761c4642STobias Röhmel 
2504761c4642STobias Röhmel         if (cpu->pmsav8r_hdregion) {
2505761c4642STobias Röhmel             env->pmsav8.hprbar = g_new0(uint32_t,
2506761c4642STobias Röhmel                                         cpu->pmsav8r_hdregion);
2507761c4642STobias Röhmel             env->pmsav8.hprlar = g_new0(uint32_t,
2508761c4642STobias Röhmel                                         cpu->pmsav8r_hdregion);
2509761c4642STobias Röhmel         }
25100e1a46bbSPeter Maydell     }
2511fcf5ef2aSThomas Huth 
25129901c576SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
25139901c576SPeter Maydell         uint32_t nr = cpu->sau_sregion;
25149901c576SPeter Maydell 
25159901c576SPeter Maydell         if (nr > 0xff) {
25169901c576SPeter Maydell             error_setg(errp, "v8M SAU #regions invalid %" PRIu32, nr);
25179901c576SPeter Maydell             return;
25189901c576SPeter Maydell         }
25199901c576SPeter Maydell 
25209901c576SPeter Maydell         if (nr) {
25219901c576SPeter Maydell             env->sau.rbar = g_new0(uint32_t, nr);
25229901c576SPeter Maydell             env->sau.rlar = g_new0(uint32_t, nr);
25239901c576SPeter Maydell         }
25249901c576SPeter Maydell     }
25259901c576SPeter Maydell 
252691db4642SCédric Le Goater     if (arm_feature(env, ARM_FEATURE_EL3)) {
252791db4642SCédric Le Goater         set_feature(env, ARM_FEATURE_VBAR);
252891db4642SCédric Le Goater     }
252991db4642SCédric Le Goater 
2530f6fc36deSJean-Philippe Brucker #ifndef CONFIG_USER_ONLY
2531f6fc36deSJean-Philippe Brucker     if (tcg_enabled() && cpu_isar_feature(aa64_rme, cpu)) {
2532f6fc36deSJean-Philippe Brucker         arm_register_el_change_hook(cpu, &gt_rme_post_el_change, 0);
2533f6fc36deSJean-Philippe Brucker     }
2534f6fc36deSJean-Philippe Brucker #endif
2535f6fc36deSJean-Philippe Brucker 
2536fcf5ef2aSThomas Huth     register_cp_regs_for_features(cpu);
2537fcf5ef2aSThomas Huth     arm_cpu_register_gdb_regs_for_features(cpu);
2538f81198ceSGustavo Romero     arm_cpu_register_gdb_commands(cpu);
2539fcf5ef2aSThomas Huth 
2540fcf5ef2aSThomas Huth     init_cpreg_list(cpu);
2541fcf5ef2aSThomas Huth 
2542fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
2543cc7d44c2SLike Xu     MachineState *ms = MACHINE(qdev_get_machine());
2544cc7d44c2SLike Xu     unsigned int smp_cpus = ms->smp.cpus;
25458bce44a2SRichard Henderson     bool has_secure = cpu->has_el3 || arm_feature(env, ARM_FEATURE_M_SECURITY);
2546cc7d44c2SLike Xu 
25478bce44a2SRichard Henderson     /*
25488bce44a2SRichard Henderson      * We must set cs->num_ases to the final value before
25498bce44a2SRichard Henderson      * the first call to cpu_address_space_init.
25508bce44a2SRichard Henderson      */
25518bce44a2SRichard Henderson     if (cpu->tag_memory != NULL) {
25528bce44a2SRichard Henderson         cs->num_ases = 3 + has_secure;
25538bce44a2SRichard Henderson     } else {
25548bce44a2SRichard Henderson         cs->num_ases = 1 + has_secure;
25558bce44a2SRichard Henderson     }
25561d2091bcSPeter Maydell 
25578bce44a2SRichard Henderson     if (has_secure) {
2558fcf5ef2aSThomas Huth         if (!cpu->secure_memory) {
2559fcf5ef2aSThomas Huth             cpu->secure_memory = cs->memory;
2560fcf5ef2aSThomas Huth         }
256180ceb07aSPeter Xu         cpu_address_space_init(cs, ARMASIdx_S, "cpu-secure-memory",
256280ceb07aSPeter Xu                                cpu->secure_memory);
2563fcf5ef2aSThomas Huth     }
25648bce44a2SRichard Henderson 
25658bce44a2SRichard Henderson     if (cpu->tag_memory != NULL) {
25668bce44a2SRichard Henderson         cpu_address_space_init(cs, ARMASIdx_TagNS, "cpu-tag-memory",
25678bce44a2SRichard Henderson                                cpu->tag_memory);
25688bce44a2SRichard Henderson         if (has_secure) {
25698bce44a2SRichard Henderson             cpu_address_space_init(cs, ARMASIdx_TagS, "cpu-tag-memory",
25708bce44a2SRichard Henderson                                    cpu->secure_tag_memory);
25718bce44a2SRichard Henderson         }
25728bce44a2SRichard Henderson     }
25738bce44a2SRichard Henderson 
257480ceb07aSPeter Xu     cpu_address_space_init(cs, ARMASIdx_NS, "cpu-memory", cs->memory);
2575f9a69711SAlistair Francis 
2576f9a69711SAlistair Francis     /* No core_count specified, default to smp_cpus. */
2577f9a69711SAlistair Francis     if (cpu->core_count == -1) {
2578f9a69711SAlistair Francis         cpu->core_count = smp_cpus;
2579f9a69711SAlistair Francis     }
2580fcf5ef2aSThomas Huth #endif
2581fcf5ef2aSThomas Huth 
2582a4157b80SRichard Henderson     if (tcg_enabled()) {
2583a4157b80SRichard Henderson         int dcz_blocklen = 4 << cpu->dcz_blocksize;
2584a4157b80SRichard Henderson 
2585a4157b80SRichard Henderson         /*
2586a4157b80SRichard Henderson          * We only support DCZ blocklen that fits on one page.
2587a4157b80SRichard Henderson          *
2588a4157b80SRichard Henderson          * Architectually this is always true.  However TARGET_PAGE_SIZE
2589a4157b80SRichard Henderson          * is variable and, for compatibility with -machine virt-2.7,
2590a4157b80SRichard Henderson          * is only 1KiB, as an artifact of legacy ARMv5 subpage support.
2591a4157b80SRichard Henderson          * But even then, while the largest architectural DCZ blocklen
2592a4157b80SRichard Henderson          * is 2KiB, no cpu actually uses such a large blocklen.
2593a4157b80SRichard Henderson          */
2594a4157b80SRichard Henderson         assert(dcz_blocklen <= TARGET_PAGE_SIZE);
2595a4157b80SRichard Henderson 
2596a4157b80SRichard Henderson         /*
2597a4157b80SRichard Henderson          * We only support DCZ blocksize >= 2*TAG_GRANULE, which is to say
2598a4157b80SRichard Henderson          * both nibbles of each byte storing tag data may be written at once.
2599a4157b80SRichard Henderson          * Since TAG_GRANULE is 16, this means that blocklen must be >= 32.
2600a4157b80SRichard Henderson          */
2601a4157b80SRichard Henderson         if (cpu_isar_feature(aa64_mte, cpu)) {
2602a4157b80SRichard Henderson             assert(dcz_blocklen >= 2 * TAG_GRANULE);
2603a4157b80SRichard Henderson         }
2604a4157b80SRichard Henderson     }
2605a4157b80SRichard Henderson 
2606fcf5ef2aSThomas Huth     qemu_init_vcpu(cs);
2607fcf5ef2aSThomas Huth     cpu_reset(cs);
2608fcf5ef2aSThomas Huth 
2609fcf5ef2aSThomas Huth     acc->parent_realize(dev, errp);
2610fcf5ef2aSThomas Huth }
2611fcf5ef2aSThomas Huth 
arm_cpu_class_by_name(const char * cpu_model)2612fcf5ef2aSThomas Huth static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
2613fcf5ef2aSThomas Huth {
2614fcf5ef2aSThomas Huth     ObjectClass *oc;
2615fcf5ef2aSThomas Huth     char *typename;
2616fcf5ef2aSThomas Huth     char **cpuname;
2617a0032cc5SPeter Maydell     const char *cpunamestr;
2618fcf5ef2aSThomas Huth 
2619fcf5ef2aSThomas Huth     cpuname = g_strsplit(cpu_model, ",", 1);
2620a0032cc5SPeter Maydell     cpunamestr = cpuname[0];
2621a0032cc5SPeter Maydell #ifdef CONFIG_USER_ONLY
2622a0032cc5SPeter Maydell     /* For backwards compatibility usermode emulation allows "-cpu any",
2623a0032cc5SPeter Maydell      * which has the same semantics as "-cpu max".
2624a0032cc5SPeter Maydell      */
2625a0032cc5SPeter Maydell     if (!strcmp(cpunamestr, "any")) {
2626a0032cc5SPeter Maydell         cpunamestr = "max";
2627a0032cc5SPeter Maydell     }
2628a0032cc5SPeter Maydell #endif
2629a0032cc5SPeter Maydell     typename = g_strdup_printf(ARM_CPU_TYPE_NAME("%s"), cpunamestr);
2630fcf5ef2aSThomas Huth     oc = object_class_by_name(typename);
2631fcf5ef2aSThomas Huth     g_strfreev(cpuname);
2632fcf5ef2aSThomas Huth     g_free(typename);
2633d5be19f5SPhilippe Mathieu-Daudé 
2634fcf5ef2aSThomas Huth     return oc;
2635fcf5ef2aSThomas Huth }
2636fcf5ef2aSThomas Huth 
2637fcf5ef2aSThomas Huth static Property arm_cpu_properties[] = {
2638e544f800SPhilippe Mathieu-Daudé     DEFINE_PROP_UINT64("midr", ARMCPU, midr, 0),
2639fcf5ef2aSThomas Huth     DEFINE_PROP_UINT64("mp-affinity", ARMCPU,
2640fcf5ef2aSThomas Huth                         mp_affinity, ARM64_AFFINITY_INVALID),
264115f8b142SIgor Mammedov     DEFINE_PROP_INT32("node-id", ARMCPU, node_id, CPU_UNSET_NUMA_NODE_ID),
2642f9a69711SAlistair Francis     DEFINE_PROP_INT32("core-count", ARMCPU, core_count, -1),
2643f037f5b4SPeter Maydell     /* True to default to the backward-compat old CNTFRQ rather than 1Ghz */
2644f037f5b4SPeter Maydell     DEFINE_PROP_BOOL("backcompat-cntfrq", ARMCPU, backcompat_cntfrq, false),
2645fcf5ef2aSThomas Huth     DEFINE_PROP_END_OF_LIST()
2646fcf5ef2aSThomas Huth };
2647fcf5ef2aSThomas Huth 
arm_gdb_arch_name(CPUState * cs)2648a6506838SAkihiko Odaki static const gchar *arm_gdb_arch_name(CPUState *cs)
2649fcf5ef2aSThomas Huth {
2650fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(cs);
2651fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
2652fcf5ef2aSThomas Huth 
2653fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
2654a6506838SAkihiko Odaki         return "iwmmxt";
2655fcf5ef2aSThomas Huth     }
2656a6506838SAkihiko Odaki     return "arm";
2657fcf5ef2aSThomas Huth }
2658fcf5ef2aSThomas Huth 
26598b80bd28SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY
26608b80bd28SPhilippe Mathieu-Daudé #include "hw/core/sysemu-cpu-ops.h"
26618b80bd28SPhilippe Mathieu-Daudé 
26628b80bd28SPhilippe Mathieu-Daudé static const struct SysemuCPUOps arm_sysemu_ops = {
266308928c6dSPhilippe Mathieu-Daudé     .get_phys_page_attrs_debug = arm_cpu_get_phys_page_attrs_debug,
2664faf39e82SPhilippe Mathieu-Daudé     .asidx_from_attrs = arm_asidx_from_attrs,
2665715e3c1aSPhilippe Mathieu-Daudé     .write_elf32_note = arm_cpu_write_elf32_note,
2666715e3c1aSPhilippe Mathieu-Daudé     .write_elf64_note = arm_cpu_write_elf64_note,
2667da383e02SPhilippe Mathieu-Daudé     .virtio_is_big_endian = arm_cpu_virtio_is_big_endian,
2668feece4d0SPhilippe Mathieu-Daudé     .legacy_vmsd = &vmstate_arm_cpu,
26698b80bd28SPhilippe Mathieu-Daudé };
26708b80bd28SPhilippe Mathieu-Daudé #endif
26718b80bd28SPhilippe Mathieu-Daudé 
267278271684SClaudio Fontana #ifdef CONFIG_TCG
26731764ad70SRichard Henderson static const TCGCPUOps arm_tcg_ops = {
267478271684SClaudio Fontana     .initialize = arm_translate_init,
267578271684SClaudio Fontana     .synchronize_from_tb = arm_cpu_synchronize_from_tb,
267678271684SClaudio Fontana     .debug_excp_handler = arm_debug_excp_handler,
267756c6c98dSRichard Henderson     .restore_state_to_opc = arm_restore_state_to_opc,
267878271684SClaudio Fontana 
26799b12b6b4SRichard Henderson #ifdef CONFIG_USER_ONLY
26809b12b6b4SRichard Henderson     .record_sigsegv = arm_cpu_record_sigsegv,
268139a099caSRichard Henderson     .record_sigbus = arm_cpu_record_sigbus,
26829b12b6b4SRichard Henderson #else
26831ba3cb88SRichard Henderson     .tlb_fill_align = arm_cpu_tlb_fill_align,
2684083afd18SPhilippe Mathieu-Daudé     .cpu_exec_interrupt = arm_cpu_exec_interrupt,
2685a96edb68SPeter Maydell     .cpu_exec_halt = arm_cpu_exec_halt,
268678271684SClaudio Fontana     .do_interrupt = arm_cpu_do_interrupt,
268778271684SClaudio Fontana     .do_transaction_failed = arm_cpu_do_transaction_failed,
268878271684SClaudio Fontana     .do_unaligned_access = arm_cpu_do_unaligned_access,
268978271684SClaudio Fontana     .adjust_watchpoint_address = arm_adjust_watchpoint_address,
269078271684SClaudio Fontana     .debug_check_watchpoint = arm_debug_check_watchpoint,
2691b00d86bcSRichard Henderson     .debug_check_breakpoint = arm_debug_check_breakpoint,
269278271684SClaudio Fontana #endif /* !CONFIG_USER_ONLY */
269378271684SClaudio Fontana };
269478271684SClaudio Fontana #endif /* CONFIG_TCG */
269578271684SClaudio Fontana 
arm_cpu_class_init(ObjectClass * oc,void * data)2696fcf5ef2aSThomas Huth static void arm_cpu_class_init(ObjectClass *oc, void *data)
2697fcf5ef2aSThomas Huth {
2698fcf5ef2aSThomas Huth     ARMCPUClass *acc = ARM_CPU_CLASS(oc);
2699fcf5ef2aSThomas Huth     CPUClass *cc = CPU_CLASS(acc);
2700fcf5ef2aSThomas Huth     DeviceClass *dc = DEVICE_CLASS(oc);
27019130cadeSPeter Maydell     ResettableClass *rc = RESETTABLE_CLASS(oc);
2702fcf5ef2aSThomas Huth 
2703bf853881SPhilippe Mathieu-Daudé     device_class_set_parent_realize(dc, arm_cpu_realizefn,
2704bf853881SPhilippe Mathieu-Daudé                                     &acc->parent_realize);
2705fcf5ef2aSThomas Huth 
27064f67d30bSMarc-André Lureau     device_class_set_props(dc, arm_cpu_properties);
27079130cadeSPeter Maydell 
27089130cadeSPeter Maydell     resettable_class_set_parent_phases(rc, NULL, arm_cpu_reset_hold, NULL,
27099130cadeSPeter Maydell                                        &acc->parent_phases);
2710fcf5ef2aSThomas Huth 
2711fcf5ef2aSThomas Huth     cc->class_by_name = arm_cpu_class_by_name;
2712fcf5ef2aSThomas Huth     cc->has_work = arm_cpu_has_work;
2713628421c7SRichard Henderson     cc->mmu_index = arm_cpu_mmu_index;
2714fcf5ef2aSThomas Huth     cc->dump_state = arm_cpu_dump_state;
2715fcf5ef2aSThomas Huth     cc->set_pc = arm_cpu_set_pc;
2716e4fdf9dfSRichard Henderson     cc->get_pc = arm_cpu_get_pc;
2717fcf5ef2aSThomas Huth     cc->gdb_read_register = arm_cpu_gdb_read_register;
2718fcf5ef2aSThomas Huth     cc->gdb_write_register = arm_cpu_gdb_write_register;
27197350d553SRichard Henderson #ifndef CONFIG_USER_ONLY
27208b80bd28SPhilippe Mathieu-Daudé     cc->sysemu_ops = &arm_sysemu_ops;
2721fcf5ef2aSThomas Huth #endif
2722fcf5ef2aSThomas Huth     cc->gdb_arch_name = arm_gdb_arch_name;
2723fcf5ef2aSThomas Huth     cc->gdb_stop_before_watchpoint = true;
2724fcf5ef2aSThomas Huth     cc->disas_set_info = arm_disas_set_info;
272578271684SClaudio Fontana 
272674d7fc7fSRichard Henderson #ifdef CONFIG_TCG
272778271684SClaudio Fontana     cc->tcg_ops = &arm_tcg_ops;
2728cbc183d2SClaudio Fontana #endif /* CONFIG_TCG */
2729fcf5ef2aSThomas Huth }
2730fcf5ef2aSThomas Huth 
arm_cpu_instance_init(Object * obj)273151e5ef45SMarc-André Lureau static void arm_cpu_instance_init(Object *obj)
273251e5ef45SMarc-André Lureau {
273351e5ef45SMarc-André Lureau     ARMCPUClass *acc = ARM_CPU_GET_CLASS(obj);
273451e5ef45SMarc-André Lureau 
273551e5ef45SMarc-André Lureau     acc->info->initfn(obj);
273651e5ef45SMarc-André Lureau     arm_cpu_post_init(obj);
273751e5ef45SMarc-André Lureau }
273851e5ef45SMarc-André Lureau 
cpu_register_class_init(ObjectClass * oc,void * data)273951e5ef45SMarc-André Lureau static void cpu_register_class_init(ObjectClass *oc, void *data)
274051e5ef45SMarc-André Lureau {
274151e5ef45SMarc-André Lureau     ARMCPUClass *acc = ARM_CPU_CLASS(oc);
274248de6462SAkihiko Odaki     CPUClass *cc = CPU_CLASS(acc);
274351e5ef45SMarc-André Lureau 
274451e5ef45SMarc-André Lureau     acc->info = data;
274548de6462SAkihiko Odaki     cc->gdb_core_xml_file = "arm-core.xml";
274651e5ef45SMarc-André Lureau }
274751e5ef45SMarc-André Lureau 
arm_cpu_register(const ARMCPUInfo * info)274837bcf244SThomas Huth void arm_cpu_register(const ARMCPUInfo *info)
2749fcf5ef2aSThomas Huth {
2750fcf5ef2aSThomas Huth     TypeInfo type_info = {
2751fcf5ef2aSThomas Huth         .parent = TYPE_ARM_CPU,
275251e5ef45SMarc-André Lureau         .instance_init = arm_cpu_instance_init,
275351e5ef45SMarc-André Lureau         .class_init = info->class_init ?: cpu_register_class_init,
275451e5ef45SMarc-André Lureau         .class_data = (void *)info,
2755fcf5ef2aSThomas Huth     };
2756fcf5ef2aSThomas Huth 
2757fcf5ef2aSThomas Huth     type_info.name = g_strdup_printf("%s-" TYPE_ARM_CPU, info->name);
2758fcf5ef2aSThomas Huth     type_register(&type_info);
2759fcf5ef2aSThomas Huth     g_free((void *)type_info.name);
2760fcf5ef2aSThomas Huth }
2761fcf5ef2aSThomas Huth 
2762fcf5ef2aSThomas Huth static const TypeInfo arm_cpu_type_info = {
2763fcf5ef2aSThomas Huth     .name = TYPE_ARM_CPU,
2764fcf5ef2aSThomas Huth     .parent = TYPE_CPU,
2765fcf5ef2aSThomas Huth     .instance_size = sizeof(ARMCPU),
2766d03087bdSRichard Henderson     .instance_align = __alignof__(ARMCPU),
2767fcf5ef2aSThomas Huth     .instance_init = arm_cpu_initfn,
2768fcf5ef2aSThomas Huth     .instance_finalize = arm_cpu_finalizefn,
2769fcf5ef2aSThomas Huth     .abstract = true,
2770fcf5ef2aSThomas Huth     .class_size = sizeof(ARMCPUClass),
2771fcf5ef2aSThomas Huth     .class_init = arm_cpu_class_init,
2772fcf5ef2aSThomas Huth };
2773fcf5ef2aSThomas Huth 
arm_cpu_register_types(void)2774fcf5ef2aSThomas Huth static void arm_cpu_register_types(void)
2775fcf5ef2aSThomas Huth {
2776fcf5ef2aSThomas Huth     type_register_static(&arm_cpu_type_info);
2777fcf5ef2aSThomas Huth }
2778fcf5ef2aSThomas Huth 
2779fcf5ef2aSThomas Huth type_init(arm_cpu_register_types)
2780