xref: /openbmc/qemu/target/arm/cpu.c (revision 3d5f45ec895b278bfc442a436eebefab598f416a)
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"
34fcf5ef2aSThomas Huth #include "exec/exec-all.h"
35fcf5ef2aSThomas Huth #include "hw/qdev-properties.h"
36fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
37fcf5ef2aSThomas Huth #include "hw/loader.h"
38cc7d44c2SLike Xu #include "hw/boards.h"
39165876f2SPhilippe Mathieu-Daudé #ifdef CONFIG_TCG
408f4e07c9SPhilippe Mathieu-Daudé #include "hw/intc/armv7m_nvic.h"
41165876f2SPhilippe Mathieu-Daudé #endif /* CONFIG_TCG */
42165876f2SPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */
4314a48c1dSMarkus Armbruster #include "sysemu/tcg.h"
44045e5064SAlexander Graf #include "sysemu/qtest.h"
45b3946626SVincent Palatin #include "sysemu/hw_accel.h"
46fcf5ef2aSThomas Huth #include "kvm_arm.h"
47110f6c70SRichard Henderson #include "disas/capstone.h"
4824f91e81SAlex Bennée #include "fpu/softfloat.h"
49cf7c6d10SRichard Henderson #include "cpregs.h"
50fcf5ef2aSThomas Huth 
51fcf5ef2aSThomas Huth static void arm_cpu_set_pc(CPUState *cs, vaddr value)
52fcf5ef2aSThomas Huth {
53fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(cs);
5442f6ed91SJulia Suvorova     CPUARMState *env = &cpu->env;
55fcf5ef2aSThomas Huth 
5642f6ed91SJulia Suvorova     if (is_a64(env)) {
5742f6ed91SJulia Suvorova         env->pc = value;
58063bbd80SRichard Henderson         env->thumb = false;
5942f6ed91SJulia Suvorova     } else {
6042f6ed91SJulia Suvorova         env->regs[15] = value & ~1;
6142f6ed91SJulia Suvorova         env->thumb = value & 1;
6242f6ed91SJulia Suvorova     }
6342f6ed91SJulia Suvorova }
6442f6ed91SJulia Suvorova 
65e4fdf9dfSRichard Henderson static vaddr arm_cpu_get_pc(CPUState *cs)
66e4fdf9dfSRichard Henderson {
67e4fdf9dfSRichard Henderson     ARMCPU *cpu = ARM_CPU(cs);
68e4fdf9dfSRichard Henderson     CPUARMState *env = &cpu->env;
69e4fdf9dfSRichard Henderson 
70e4fdf9dfSRichard Henderson     if (is_a64(env)) {
71e4fdf9dfSRichard Henderson         return env->pc;
72e4fdf9dfSRichard Henderson     } else {
73e4fdf9dfSRichard Henderson         return env->regs[15];
74e4fdf9dfSRichard Henderson     }
75e4fdf9dfSRichard Henderson }
76e4fdf9dfSRichard Henderson 
77ec62595bSEduardo Habkost #ifdef CONFIG_TCG
7878271684SClaudio Fontana void arm_cpu_synchronize_from_tb(CPUState *cs,
7904a37d4cSRichard Henderson                                  const TranslationBlock *tb)
8042f6ed91SJulia Suvorova {
8103a648c4SAnton Johansson     /* The program counter is always up to date with CF_PCREL. */
8203a648c4SAnton Johansson     if (!(tb_cflags(tb) & CF_PCREL)) {
83abb80995SRichard Henderson         CPUARMState *env = cs->env_ptr;
8442f6ed91SJulia Suvorova         /*
8542f6ed91SJulia Suvorova          * It's OK to look at env for the current mode here, because it's
8642f6ed91SJulia Suvorova          * never possible for an AArch64 TB to chain to an AArch32 TB.
8742f6ed91SJulia Suvorova          */
8842f6ed91SJulia Suvorova         if (is_a64(env)) {
89f51a1dd7SAnton Johansson             env->pc = tb->pc;
9042f6ed91SJulia Suvorova         } else {
91f51a1dd7SAnton Johansson             env->regs[15] = tb->pc;
9242f6ed91SJulia Suvorova         }
93fcf5ef2aSThomas Huth     }
94abb80995SRichard Henderson }
9556c6c98dSRichard Henderson 
96475e56b6SEvgeny Ermakov void arm_restore_state_to_opc(CPUState *cs,
9756c6c98dSRichard Henderson                               const TranslationBlock *tb,
9856c6c98dSRichard Henderson                               const uint64_t *data)
9956c6c98dSRichard Henderson {
10056c6c98dSRichard Henderson     CPUARMState *env = cs->env_ptr;
10156c6c98dSRichard Henderson 
10256c6c98dSRichard Henderson     if (is_a64(env)) {
10303a648c4SAnton Johansson         if (tb_cflags(tb) & CF_PCREL) {
10456c6c98dSRichard Henderson             env->pc = (env->pc & TARGET_PAGE_MASK) | data[0];
10556c6c98dSRichard Henderson         } else {
10656c6c98dSRichard Henderson             env->pc = data[0];
10756c6c98dSRichard Henderson         }
10856c6c98dSRichard Henderson         env->condexec_bits = 0;
10956c6c98dSRichard Henderson         env->exception.syndrome = data[2] << ARM_INSN_START_WORD2_SHIFT;
11056c6c98dSRichard Henderson     } else {
11103a648c4SAnton Johansson         if (tb_cflags(tb) & CF_PCREL) {
11256c6c98dSRichard Henderson             env->regs[15] = (env->regs[15] & TARGET_PAGE_MASK) | data[0];
11356c6c98dSRichard Henderson         } else {
11456c6c98dSRichard Henderson             env->regs[15] = data[0];
11556c6c98dSRichard Henderson         }
11656c6c98dSRichard Henderson         env->condexec_bits = data[1];
11756c6c98dSRichard Henderson         env->exception.syndrome = data[2] << ARM_INSN_START_WORD2_SHIFT;
11856c6c98dSRichard Henderson     }
11956c6c98dSRichard Henderson }
120ec62595bSEduardo Habkost #endif /* CONFIG_TCG */
121fcf5ef2aSThomas Huth 
122fcf5ef2aSThomas Huth static bool arm_cpu_has_work(CPUState *cs)
123fcf5ef2aSThomas Huth {
124fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(cs);
125fcf5ef2aSThomas Huth 
126062ba099SAlex Bennée     return (cpu->power_state != PSCI_OFF)
127fcf5ef2aSThomas Huth         && cs->interrupt_request &
128fcf5ef2aSThomas Huth         (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD
1293c29632fSRichard Henderson          | CPU_INTERRUPT_VFIQ | CPU_INTERRUPT_VIRQ | CPU_INTERRUPT_VSERR
130fcf5ef2aSThomas Huth          | CPU_INTERRUPT_EXITTB);
131fcf5ef2aSThomas Huth }
132fcf5ef2aSThomas Huth 
133b5c53d1bSAaron Lindsay void arm_register_pre_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook,
134b5c53d1bSAaron Lindsay                                  void *opaque)
135b5c53d1bSAaron Lindsay {
136b5c53d1bSAaron Lindsay     ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1);
137b5c53d1bSAaron Lindsay 
138b5c53d1bSAaron Lindsay     entry->hook = hook;
139b5c53d1bSAaron Lindsay     entry->opaque = opaque;
140b5c53d1bSAaron Lindsay 
141b5c53d1bSAaron Lindsay     QLIST_INSERT_HEAD(&cpu->pre_el_change_hooks, entry, node);
142b5c53d1bSAaron Lindsay }
143b5c53d1bSAaron Lindsay 
14408267487SAaron Lindsay void arm_register_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook,
145fcf5ef2aSThomas Huth                                  void *opaque)
146fcf5ef2aSThomas Huth {
14708267487SAaron Lindsay     ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1);
14808267487SAaron Lindsay 
14908267487SAaron Lindsay     entry->hook = hook;
15008267487SAaron Lindsay     entry->opaque = opaque;
15108267487SAaron Lindsay 
15208267487SAaron Lindsay     QLIST_INSERT_HEAD(&cpu->el_change_hooks, entry, node);
153fcf5ef2aSThomas Huth }
154fcf5ef2aSThomas Huth 
155fcf5ef2aSThomas Huth static void cp_reg_reset(gpointer key, gpointer value, gpointer opaque)
156fcf5ef2aSThomas Huth {
157fcf5ef2aSThomas Huth     /* Reset a single ARMCPRegInfo register */
158fcf5ef2aSThomas Huth     ARMCPRegInfo *ri = value;
159fcf5ef2aSThomas Huth     ARMCPU *cpu = opaque;
160fcf5ef2aSThomas Huth 
16187c3f0f2SRichard Henderson     if (ri->type & (ARM_CP_SPECIAL_MASK | ARM_CP_ALIAS)) {
162fcf5ef2aSThomas Huth         return;
163fcf5ef2aSThomas Huth     }
164fcf5ef2aSThomas Huth 
165fcf5ef2aSThomas Huth     if (ri->resetfn) {
166fcf5ef2aSThomas Huth         ri->resetfn(&cpu->env, ri);
167fcf5ef2aSThomas Huth         return;
168fcf5ef2aSThomas Huth     }
169fcf5ef2aSThomas Huth 
170fcf5ef2aSThomas Huth     /* A zero offset is never possible as it would be regs[0]
171fcf5ef2aSThomas Huth      * so we use it to indicate that reset is being handled elsewhere.
172fcf5ef2aSThomas Huth      * This is basically only used for fields in non-core coprocessors
173fcf5ef2aSThomas Huth      * (like the pxa2xx ones).
174fcf5ef2aSThomas Huth      */
175fcf5ef2aSThomas Huth     if (!ri->fieldoffset) {
176fcf5ef2aSThomas Huth         return;
177fcf5ef2aSThomas Huth     }
178fcf5ef2aSThomas Huth 
179fcf5ef2aSThomas Huth     if (cpreg_field_is_64bit(ri)) {
180fcf5ef2aSThomas Huth         CPREG_FIELD64(&cpu->env, ri) = ri->resetvalue;
181fcf5ef2aSThomas Huth     } else {
182fcf5ef2aSThomas Huth         CPREG_FIELD32(&cpu->env, ri) = ri->resetvalue;
183fcf5ef2aSThomas Huth     }
184fcf5ef2aSThomas Huth }
185fcf5ef2aSThomas Huth 
186fcf5ef2aSThomas Huth static void cp_reg_check_reset(gpointer key, gpointer value,  gpointer opaque)
187fcf5ef2aSThomas Huth {
188fcf5ef2aSThomas Huth     /* Purely an assertion check: we've already done reset once,
189fcf5ef2aSThomas Huth      * so now check that running the reset for the cpreg doesn't
190fcf5ef2aSThomas Huth      * change its value. This traps bugs where two different cpregs
191fcf5ef2aSThomas Huth      * both try to reset the same state field but to different values.
192fcf5ef2aSThomas Huth      */
193fcf5ef2aSThomas Huth     ARMCPRegInfo *ri = value;
194fcf5ef2aSThomas Huth     ARMCPU *cpu = opaque;
195fcf5ef2aSThomas Huth     uint64_t oldvalue, newvalue;
196fcf5ef2aSThomas Huth 
19787c3f0f2SRichard Henderson     if (ri->type & (ARM_CP_SPECIAL_MASK | ARM_CP_ALIAS | ARM_CP_NO_RAW)) {
198fcf5ef2aSThomas Huth         return;
199fcf5ef2aSThomas Huth     }
200fcf5ef2aSThomas Huth 
201fcf5ef2aSThomas Huth     oldvalue = read_raw_cp_reg(&cpu->env, ri);
202fcf5ef2aSThomas Huth     cp_reg_reset(key, value, opaque);
203fcf5ef2aSThomas Huth     newvalue = read_raw_cp_reg(&cpu->env, ri);
204fcf5ef2aSThomas Huth     assert(oldvalue == newvalue);
205fcf5ef2aSThomas Huth }
206fcf5ef2aSThomas Huth 
2079130cadeSPeter Maydell static void arm_cpu_reset_hold(Object *obj)
208fcf5ef2aSThomas Huth {
2099130cadeSPeter Maydell     CPUState *s = CPU(obj);
210fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(s);
211fcf5ef2aSThomas Huth     ARMCPUClass *acc = ARM_CPU_GET_CLASS(cpu);
212fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
213fcf5ef2aSThomas Huth 
2149130cadeSPeter Maydell     if (acc->parent_phases.hold) {
2159130cadeSPeter Maydell         acc->parent_phases.hold(obj);
2169130cadeSPeter Maydell     }
217fcf5ef2aSThomas Huth 
2181f5c00cfSAlex Bennée     memset(env, 0, offsetof(CPUARMState, end_reset_fields));
2191f5c00cfSAlex Bennée 
220fcf5ef2aSThomas Huth     g_hash_table_foreach(cpu->cp_regs, cp_reg_reset, cpu);
221fcf5ef2aSThomas Huth     g_hash_table_foreach(cpu->cp_regs, cp_reg_check_reset, cpu);
222fcf5ef2aSThomas Huth 
223fcf5ef2aSThomas Huth     env->vfp.xregs[ARM_VFP_FPSID] = cpu->reset_fpsid;
22447576b94SRichard Henderson     env->vfp.xregs[ARM_VFP_MVFR0] = cpu->isar.mvfr0;
22547576b94SRichard Henderson     env->vfp.xregs[ARM_VFP_MVFR1] = cpu->isar.mvfr1;
22647576b94SRichard Henderson     env->vfp.xregs[ARM_VFP_MVFR2] = cpu->isar.mvfr2;
227fcf5ef2aSThomas Huth 
228c1b70158SThiago Jung Bauermann     cpu->power_state = s->start_powered_off ? PSCI_OFF : PSCI_ON;
229fcf5ef2aSThomas Huth 
230fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
231fcf5ef2aSThomas Huth         env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
232fcf5ef2aSThomas Huth     }
233fcf5ef2aSThomas Huth 
234fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
235fcf5ef2aSThomas Huth         /* 64 bit CPUs always start in 64 bit mode */
23653221552SRichard Henderson         env->aarch64 = true;
237fcf5ef2aSThomas Huth #if defined(CONFIG_USER_ONLY)
238fcf5ef2aSThomas Huth         env->pstate = PSTATE_MODE_EL0t;
239fcf5ef2aSThomas Huth         /* Userspace expects access to DC ZVA, CTL_EL0 and the cache ops */
240fcf5ef2aSThomas Huth         env->cp15.sctlr_el[1] |= SCTLR_UCT | SCTLR_UCI | SCTLR_DZE;
241276c6e81SRichard Henderson         /* Enable all PAC keys.  */
242276c6e81SRichard Henderson         env->cp15.sctlr_el[1] |= (SCTLR_EnIA | SCTLR_EnIB |
243276c6e81SRichard Henderson                                   SCTLR_EnDA | SCTLR_EnDB);
244cda86e2bSRichard Henderson         /* Trap on btype=3 for PACIxSP. */
245cda86e2bSRichard Henderson         env->cp15.sctlr_el[1] |= SCTLR_BT0;
246fcf5ef2aSThomas Huth         /* and to the FP/Neon instructions */
247fab8ad39SRichard Henderson         env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
248fab8ad39SRichard Henderson                                          CPACR_EL1, FPEN, 3);
24946303535SRichard Henderson         /* and to the SVE instructions, with default vector length */
25046303535SRichard Henderson         if (cpu_isar_feature(aa64_sve, cpu)) {
251fab8ad39SRichard Henderson             env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
252fab8ad39SRichard Henderson                                              CPACR_EL1, ZEN, 3);
25387252bdeSRichard Henderson             env->vfp.zcr_el[1] = cpu->sve_default_vq - 1;
2547b6a2198SAlex Bennée         }
25578011586SRichard Henderson         /* and for SME instructions, with default vector length, and TPIDR2 */
25678011586SRichard Henderson         if (cpu_isar_feature(aa64_sme, cpu)) {
25778011586SRichard Henderson             env->cp15.sctlr_el[1] |= SCTLR_EnTP2;
25878011586SRichard Henderson             env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
25978011586SRichard Henderson                                              CPACR_EL1, SMEN, 3);
26078011586SRichard Henderson             env->vfp.smcr_el[1] = cpu->sme_default_vq - 1;
26178011586SRichard Henderson             if (cpu_isar_feature(aa64_sme_fa64, cpu)) {
26278011586SRichard Henderson                 env->vfp.smcr_el[1] = FIELD_DP64(env->vfp.smcr_el[1],
26378011586SRichard Henderson                                                  SMCR, FA64, 1);
26478011586SRichard Henderson             }
26578011586SRichard Henderson         }
266f6a148feSRichard Henderson         /*
267691f1ffdSRichard Henderson          * Enable 48-bit address space (TODO: take reserved_va into account).
26816c84978SRichard Henderson          * Enable TBI0 but not TBI1.
26916c84978SRichard Henderson          * Note that this must match useronly_clean_ptr.
270f6a148feSRichard Henderson          */
271cb4a0a34SPeter Maydell         env->cp15.tcr_el[1] = 5 | (1ULL << 37);
272e3232864SRichard Henderson 
273e3232864SRichard Henderson         /* Enable MTE */
274e3232864SRichard Henderson         if (cpu_isar_feature(aa64_mte, cpu)) {
275e3232864SRichard Henderson             /* Enable tag access, but leave TCF0 as No Effect (0). */
276e3232864SRichard Henderson             env->cp15.sctlr_el[1] |= SCTLR_ATA0;
277e3232864SRichard Henderson             /*
278e3232864SRichard Henderson              * Exclude all tags, so that tag 0 is always used.
279e3232864SRichard Henderson              * This corresponds to Linux current->thread.gcr_incl = 0.
280e3232864SRichard Henderson              *
281e3232864SRichard Henderson              * Set RRND, so that helper_irg() will generate a seed later.
282e3232864SRichard Henderson              * Here in cpu_reset(), the crypto subsystem has not yet been
283e3232864SRichard Henderson              * initialized.
284e3232864SRichard Henderson              */
285e3232864SRichard Henderson             env->cp15.gcr_el1 = 0x1ffff;
286e3232864SRichard Henderson         }
2877cb1e618SRichard Henderson         /*
2887cb1e618SRichard Henderson          * Disable access to SCXTNUM_EL0 from CSV2_1p2.
2897cb1e618SRichard Henderson          * This is not yet exposed from the Linux kernel in any way.
2907cb1e618SRichard Henderson          */
2917cb1e618SRichard Henderson         env->cp15.sctlr_el[1] |= SCTLR_TSCXT;
292f9ac7788SZhuojia Shen         /* Disable access to Debug Communication Channel (DCC). */
293f9ac7788SZhuojia Shen         env->cp15.mdscr_el1 |= 1 << 12;
294fcf5ef2aSThomas Huth #else
295fcf5ef2aSThomas Huth         /* Reset into the highest available EL */
296fcf5ef2aSThomas Huth         if (arm_feature(env, ARM_FEATURE_EL3)) {
297fcf5ef2aSThomas Huth             env->pstate = PSTATE_MODE_EL3h;
298fcf5ef2aSThomas Huth         } else if (arm_feature(env, ARM_FEATURE_EL2)) {
299fcf5ef2aSThomas Huth             env->pstate = PSTATE_MODE_EL2h;
300fcf5ef2aSThomas Huth         } else {
301fcf5ef2aSThomas Huth             env->pstate = PSTATE_MODE_EL1h;
302fcf5ef2aSThomas Huth         }
3034a7319b7SEdgar E. Iglesias 
3044a7319b7SEdgar E. Iglesias         /* Sample rvbar at reset.  */
3054a7319b7SEdgar E. Iglesias         env->cp15.rvbar = cpu->rvbar_prop;
3064a7319b7SEdgar E. Iglesias         env->pc = env->cp15.rvbar;
307fcf5ef2aSThomas Huth #endif
308fcf5ef2aSThomas Huth     } else {
309fcf5ef2aSThomas Huth #if defined(CONFIG_USER_ONLY)
310fcf5ef2aSThomas Huth         /* Userspace expects access to cp10 and cp11 for FP/Neon */
311fab8ad39SRichard Henderson         env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
312fab8ad39SRichard Henderson                                          CPACR, CP10, 3);
313fab8ad39SRichard Henderson         env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
314fab8ad39SRichard Henderson                                          CPACR, CP11, 3);
315fcf5ef2aSThomas Huth #endif
316910e4f24STobias Röhmel         if (arm_feature(env, ARM_FEATURE_V8)) {
317910e4f24STobias Röhmel             env->cp15.rvbar = cpu->rvbar_prop;
318910e4f24STobias Röhmel             env->regs[15] = cpu->rvbar_prop;
319910e4f24STobias Röhmel         }
320fcf5ef2aSThomas Huth     }
321fcf5ef2aSThomas Huth 
322fcf5ef2aSThomas Huth #if defined(CONFIG_USER_ONLY)
323fcf5ef2aSThomas Huth     env->uncached_cpsr = ARM_CPU_MODE_USR;
324fcf5ef2aSThomas Huth     /* For user mode we must enable access to coprocessors */
325fcf5ef2aSThomas Huth     env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
326fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
327fcf5ef2aSThomas Huth         env->cp15.c15_cpar = 3;
328fcf5ef2aSThomas Huth     } else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
329fcf5ef2aSThomas Huth         env->cp15.c15_cpar = 1;
330fcf5ef2aSThomas Huth     }
331fcf5ef2aSThomas Huth #else
332060a65dfSPeter Maydell 
333060a65dfSPeter Maydell     /*
334060a65dfSPeter Maydell      * If the highest available EL is EL2, AArch32 will start in Hyp
335060a65dfSPeter Maydell      * mode; otherwise it starts in SVC. Note that if we start in
336060a65dfSPeter Maydell      * AArch64 then these values in the uncached_cpsr will be ignored.
337060a65dfSPeter Maydell      */
338060a65dfSPeter Maydell     if (arm_feature(env, ARM_FEATURE_EL2) &&
339060a65dfSPeter Maydell         !arm_feature(env, ARM_FEATURE_EL3)) {
340060a65dfSPeter Maydell         env->uncached_cpsr = ARM_CPU_MODE_HYP;
341060a65dfSPeter Maydell     } else {
342fcf5ef2aSThomas Huth         env->uncached_cpsr = ARM_CPU_MODE_SVC;
343060a65dfSPeter Maydell     }
344fcf5ef2aSThomas Huth     env->daif = PSTATE_D | PSTATE_A | PSTATE_I | PSTATE_F;
3451426f244SPeter Maydell 
3461426f244SPeter Maydell     /* AArch32 has a hard highvec setting of 0xFFFF0000.  If we are currently
3471426f244SPeter Maydell      * executing as AArch32 then check if highvecs are enabled and
3481426f244SPeter Maydell      * adjust the PC accordingly.
3491426f244SPeter Maydell      */
3501426f244SPeter Maydell     if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
3511426f244SPeter Maydell         env->regs[15] = 0xFFFF0000;
3521426f244SPeter Maydell     }
3531426f244SPeter Maydell 
3541426f244SPeter Maydell     env->vfp.xregs[ARM_VFP_FPEXC] = 0;
355b62ceeafSPeter Maydell #endif
356dc7abe4dSMichael Davidsaver 
357531c60a9SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M)) {
358b62ceeafSPeter Maydell #ifndef CONFIG_USER_ONLY
359fcf5ef2aSThomas Huth         uint32_t initial_msp; /* Loaded from 0x0 */
360fcf5ef2aSThomas Huth         uint32_t initial_pc; /* Loaded from 0x4 */
361fcf5ef2aSThomas Huth         uint8_t *rom;
36238e2a77cSPeter Maydell         uint32_t vecbase;
363b62ceeafSPeter Maydell #endif
364fcf5ef2aSThomas Huth 
3658128c8e8SPeter Maydell         if (cpu_isar_feature(aa32_lob, cpu)) {
3668128c8e8SPeter Maydell             /*
3678128c8e8SPeter Maydell              * LTPSIZE is constant 4 if MVE not implemented, and resets
3688128c8e8SPeter Maydell              * to an UNKNOWN value if MVE is implemented. We choose to
3698128c8e8SPeter Maydell              * always reset to 4.
3708128c8e8SPeter Maydell              */
3718128c8e8SPeter Maydell             env->v7m.ltpsize = 4;
37299c7834fSPeter Maydell             /* The LTPSIZE field in FPDSCR is constant and reads as 4. */
37399c7834fSPeter Maydell             env->v7m.fpdscr[M_REG_NS] = 4 << FPCR_LTPSIZE_SHIFT;
37499c7834fSPeter Maydell             env->v7m.fpdscr[M_REG_S] = 4 << FPCR_LTPSIZE_SHIFT;
3758128c8e8SPeter Maydell         }
3768128c8e8SPeter Maydell 
3771e577cc7SPeter Maydell         if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
3781e577cc7SPeter Maydell             env->v7m.secure = true;
3793b2e9344SPeter Maydell         } else {
3803b2e9344SPeter Maydell             /* This bit resets to 0 if security is supported, but 1 if
3813b2e9344SPeter Maydell              * it is not. The bit is not present in v7M, but we set it
3823b2e9344SPeter Maydell              * here so we can avoid having to make checks on it conditional
3833b2e9344SPeter Maydell              * on ARM_FEATURE_V8 (we don't let the guest see the bit).
3843b2e9344SPeter Maydell              */
3853b2e9344SPeter Maydell             env->v7m.aircr = R_V7M_AIRCR_BFHFNMINS_MASK;
38602ac2f7fSPeter Maydell             /*
38702ac2f7fSPeter Maydell              * Set NSACR to indicate "NS access permitted to everything";
38802ac2f7fSPeter Maydell              * this avoids having to have all the tests of it being
38902ac2f7fSPeter Maydell              * conditional on ARM_FEATURE_M_SECURITY. Note also that from
39002ac2f7fSPeter Maydell              * v8.1M the guest-visible value of NSACR in a CPU without the
39102ac2f7fSPeter Maydell              * Security Extension is 0xcff.
39202ac2f7fSPeter Maydell              */
39302ac2f7fSPeter Maydell             env->v7m.nsacr = 0xcff;
3941e577cc7SPeter Maydell         }
3951e577cc7SPeter Maydell 
3969d40cd8aSPeter Maydell         /* In v7M the reset value of this bit is IMPDEF, but ARM recommends
3972c4da50dSPeter Maydell          * that it resets to 1, so QEMU always does that rather than making
3989d40cd8aSPeter Maydell          * it dependent on CPU model. In v8M it is RES1.
3992c4da50dSPeter Maydell          */
4009d40cd8aSPeter Maydell         env->v7m.ccr[M_REG_NS] = R_V7M_CCR_STKALIGN_MASK;
4019d40cd8aSPeter Maydell         env->v7m.ccr[M_REG_S] = R_V7M_CCR_STKALIGN_MASK;
4029d40cd8aSPeter Maydell         if (arm_feature(env, ARM_FEATURE_V8)) {
4039d40cd8aSPeter Maydell             /* in v8M the NONBASETHRDENA bit [0] is RES1 */
4049d40cd8aSPeter Maydell             env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_NONBASETHRDENA_MASK;
4059d40cd8aSPeter Maydell             env->v7m.ccr[M_REG_S] |= R_V7M_CCR_NONBASETHRDENA_MASK;
4069d40cd8aSPeter Maydell         }
40722ab3460SJulia Suvorova         if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
40822ab3460SJulia Suvorova             env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_UNALIGN_TRP_MASK;
40922ab3460SJulia Suvorova             env->v7m.ccr[M_REG_S] |= R_V7M_CCR_UNALIGN_TRP_MASK;
41022ab3460SJulia Suvorova         }
4112c4da50dSPeter Maydell 
4127fbc6a40SRichard Henderson         if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
413d33abe82SPeter Maydell             env->v7m.fpccr[M_REG_NS] = R_V7M_FPCCR_ASPEN_MASK;
414d33abe82SPeter Maydell             env->v7m.fpccr[M_REG_S] = R_V7M_FPCCR_ASPEN_MASK |
415d33abe82SPeter Maydell                 R_V7M_FPCCR_LSPEN_MASK | R_V7M_FPCCR_S_MASK;
416d33abe82SPeter Maydell         }
417b62ceeafSPeter Maydell 
418b62ceeafSPeter Maydell #ifndef CONFIG_USER_ONLY
419056f43dfSPeter Maydell         /* Unlike A/R profile, M profile defines the reset LR value */
420056f43dfSPeter Maydell         env->regs[14] = 0xffffffff;
421056f43dfSPeter Maydell 
42238e2a77cSPeter Maydell         env->v7m.vecbase[M_REG_S] = cpu->init_svtor & 0xffffff80;
4237cda2149SPeter Maydell         env->v7m.vecbase[M_REG_NS] = cpu->init_nsvtor & 0xffffff80;
42438e2a77cSPeter Maydell 
42538e2a77cSPeter Maydell         /* Load the initial SP and PC from offset 0 and 4 in the vector table */
42638e2a77cSPeter Maydell         vecbase = env->v7m.vecbase[env->v7m.secure];
42775ce72b7SPeter Maydell         rom = rom_ptr_for_as(s->as, vecbase, 8);
428fcf5ef2aSThomas Huth         if (rom) {
429fcf5ef2aSThomas Huth             /* Address zero is covered by ROM which hasn't yet been
430fcf5ef2aSThomas Huth              * copied into physical memory.
431fcf5ef2aSThomas Huth              */
432fcf5ef2aSThomas Huth             initial_msp = ldl_p(rom);
433fcf5ef2aSThomas Huth             initial_pc = ldl_p(rom + 4);
434fcf5ef2aSThomas Huth         } else {
435fcf5ef2aSThomas Huth             /* Address zero not covered by a ROM blob, or the ROM blob
436fcf5ef2aSThomas Huth              * is in non-modifiable memory and this is a second reset after
437fcf5ef2aSThomas Huth              * it got copied into memory. In the latter case, rom_ptr
438fcf5ef2aSThomas Huth              * will return a NULL pointer and we should use ldl_phys instead.
439fcf5ef2aSThomas Huth              */
44038e2a77cSPeter Maydell             initial_msp = ldl_phys(s->as, vecbase);
44138e2a77cSPeter Maydell             initial_pc = ldl_phys(s->as, vecbase + 4);
442fcf5ef2aSThomas Huth         }
443fcf5ef2aSThomas Huth 
4448cc2246cSPeter Maydell         qemu_log_mask(CPU_LOG_INT,
4458cc2246cSPeter Maydell                       "Loaded reset SP 0x%x PC 0x%x from vector table\n",
4468cc2246cSPeter Maydell                       initial_msp, initial_pc);
4478cc2246cSPeter Maydell 
448fcf5ef2aSThomas Huth         env->regs[13] = initial_msp & 0xFFFFFFFC;
449fcf5ef2aSThomas Huth         env->regs[15] = initial_pc & ~1;
450fcf5ef2aSThomas Huth         env->thumb = initial_pc & 1;
451b62ceeafSPeter Maydell #else
452b62ceeafSPeter Maydell         /*
453b62ceeafSPeter Maydell          * For user mode we run non-secure and with access to the FPU.
454b62ceeafSPeter Maydell          * The FPU context is active (ie does not need further setup)
455b62ceeafSPeter Maydell          * and is owned by non-secure.
456b62ceeafSPeter Maydell          */
457b62ceeafSPeter Maydell         env->v7m.secure = false;
458b62ceeafSPeter Maydell         env->v7m.nsacr = 0xcff;
459b62ceeafSPeter Maydell         env->v7m.cpacr[M_REG_NS] = 0xf0ffff;
460b62ceeafSPeter Maydell         env->v7m.fpccr[M_REG_S] &=
461b62ceeafSPeter Maydell             ~(R_V7M_FPCCR_LSPEN_MASK | R_V7M_FPCCR_S_MASK);
462b62ceeafSPeter Maydell         env->v7m.control[M_REG_S] |= R_V7M_CONTROL_FPCA_MASK;
463b62ceeafSPeter Maydell #endif
464fcf5ef2aSThomas Huth     }
465fcf5ef2aSThomas Huth 
466dc3c4c14SPeter Maydell     /* M profile requires that reset clears the exclusive monitor;
467dc3c4c14SPeter Maydell      * A profile does not, but clearing it makes more sense than having it
468dc3c4c14SPeter Maydell      * set with an exclusive access on address zero.
469dc3c4c14SPeter Maydell      */
470dc3c4c14SPeter Maydell     arm_clear_exclusive(env);
471dc3c4c14SPeter Maydell 
4720e1a46bbSPeter Maydell     if (arm_feature(env, ARM_FEATURE_PMSA)) {
47369ceea64SPeter Maydell         if (cpu->pmsav7_dregion > 0) {
4740e1a46bbSPeter Maydell             if (arm_feature(env, ARM_FEATURE_V8)) {
47562c58ee0SPeter Maydell                 memset(env->pmsav8.rbar[M_REG_NS], 0,
47662c58ee0SPeter Maydell                        sizeof(*env->pmsav8.rbar[M_REG_NS])
47762c58ee0SPeter Maydell                        * cpu->pmsav7_dregion);
47862c58ee0SPeter Maydell                 memset(env->pmsav8.rlar[M_REG_NS], 0,
47962c58ee0SPeter Maydell                        sizeof(*env->pmsav8.rlar[M_REG_NS])
48062c58ee0SPeter Maydell                        * cpu->pmsav7_dregion);
48162c58ee0SPeter Maydell                 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
48262c58ee0SPeter Maydell                     memset(env->pmsav8.rbar[M_REG_S], 0,
48362c58ee0SPeter Maydell                            sizeof(*env->pmsav8.rbar[M_REG_S])
48462c58ee0SPeter Maydell                            * cpu->pmsav7_dregion);
48562c58ee0SPeter Maydell                     memset(env->pmsav8.rlar[M_REG_S], 0,
48662c58ee0SPeter Maydell                            sizeof(*env->pmsav8.rlar[M_REG_S])
48762c58ee0SPeter Maydell                            * cpu->pmsav7_dregion);
48862c58ee0SPeter Maydell                 }
4890e1a46bbSPeter Maydell             } else if (arm_feature(env, ARM_FEATURE_V7)) {
49069ceea64SPeter Maydell                 memset(env->pmsav7.drbar, 0,
49169ceea64SPeter Maydell                        sizeof(*env->pmsav7.drbar) * cpu->pmsav7_dregion);
49269ceea64SPeter Maydell                 memset(env->pmsav7.drsr, 0,
49369ceea64SPeter Maydell                        sizeof(*env->pmsav7.drsr) * cpu->pmsav7_dregion);
49469ceea64SPeter Maydell                 memset(env->pmsav7.dracr, 0,
49569ceea64SPeter Maydell                        sizeof(*env->pmsav7.dracr) * cpu->pmsav7_dregion);
49669ceea64SPeter Maydell             }
4970e1a46bbSPeter Maydell         }
498761c4642STobias Röhmel 
499761c4642STobias Röhmel         if (cpu->pmsav8r_hdregion > 0) {
500761c4642STobias Röhmel             memset(env->pmsav8.hprbar, 0,
501761c4642STobias Röhmel                    sizeof(*env->pmsav8.hprbar) * cpu->pmsav8r_hdregion);
502761c4642STobias Röhmel             memset(env->pmsav8.hprlar, 0,
503761c4642STobias Röhmel                    sizeof(*env->pmsav8.hprlar) * cpu->pmsav8r_hdregion);
504761c4642STobias Röhmel         }
505761c4642STobias Röhmel 
5061bc04a88SPeter Maydell         env->pmsav7.rnr[M_REG_NS] = 0;
5071bc04a88SPeter Maydell         env->pmsav7.rnr[M_REG_S] = 0;
5084125e6feSPeter Maydell         env->pmsav8.mair0[M_REG_NS] = 0;
5094125e6feSPeter Maydell         env->pmsav8.mair0[M_REG_S] = 0;
5104125e6feSPeter Maydell         env->pmsav8.mair1[M_REG_NS] = 0;
5114125e6feSPeter Maydell         env->pmsav8.mair1[M_REG_S] = 0;
51269ceea64SPeter Maydell     }
51369ceea64SPeter Maydell 
5149901c576SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
5159901c576SPeter Maydell         if (cpu->sau_sregion > 0) {
5169901c576SPeter Maydell             memset(env->sau.rbar, 0, sizeof(*env->sau.rbar) * cpu->sau_sregion);
5179901c576SPeter Maydell             memset(env->sau.rlar, 0, sizeof(*env->sau.rlar) * cpu->sau_sregion);
5189901c576SPeter Maydell         }
5199901c576SPeter Maydell         env->sau.rnr = 0;
5209901c576SPeter Maydell         /* SAU_CTRL reset value is IMPDEF; we choose 0, which is what
5219901c576SPeter Maydell          * the Cortex-M33 does.
5229901c576SPeter Maydell          */
5239901c576SPeter Maydell         env->sau.ctrl = 0;
5249901c576SPeter Maydell     }
5259901c576SPeter Maydell 
526fcf5ef2aSThomas Huth     set_flush_to_zero(1, &env->vfp.standard_fp_status);
527fcf5ef2aSThomas Huth     set_flush_inputs_to_zero(1, &env->vfp.standard_fp_status);
528fcf5ef2aSThomas Huth     set_default_nan_mode(1, &env->vfp.standard_fp_status);
529aaae563bSPeter Maydell     set_default_nan_mode(1, &env->vfp.standard_fp_status_f16);
530fcf5ef2aSThomas Huth     set_float_detect_tininess(float_tininess_before_rounding,
531fcf5ef2aSThomas Huth                               &env->vfp.fp_status);
532fcf5ef2aSThomas Huth     set_float_detect_tininess(float_tininess_before_rounding,
533fcf5ef2aSThomas Huth                               &env->vfp.standard_fp_status);
534bcc531f0SPeter Maydell     set_float_detect_tininess(float_tininess_before_rounding,
535bcc531f0SPeter Maydell                               &env->vfp.fp_status_f16);
536aaae563bSPeter Maydell     set_float_detect_tininess(float_tininess_before_rounding,
537aaae563bSPeter Maydell                               &env->vfp.standard_fp_status_f16);
538fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
539fcf5ef2aSThomas Huth     if (kvm_enabled()) {
540fcf5ef2aSThomas Huth         kvm_arm_reset_vcpu(cpu);
541fcf5ef2aSThomas Huth     }
542fcf5ef2aSThomas Huth #endif
543fcf5ef2aSThomas Huth 
544fa05d1abSFabiano Rosas     if (tcg_enabled()) {
545fcf5ef2aSThomas Huth         hw_breakpoint_update_all(cpu);
546fcf5ef2aSThomas Huth         hw_watchpoint_update_all(cpu);
5472b77ad4dSFabiano Rosas 
548a8a79c7aSRichard Henderson         arm_rebuild_hflags(env);
549fcf5ef2aSThomas Huth     }
5502b77ad4dSFabiano Rosas }
551fcf5ef2aSThomas Huth 
5529e406eeaSPhilippe Mathieu-Daudé #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
553083afd18SPhilippe Mathieu-Daudé 
554310cedf3SRichard Henderson static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx,
555be879556SRichard Henderson                                      unsigned int target_el,
556be879556SRichard Henderson                                      unsigned int cur_el, bool secure,
557be879556SRichard Henderson                                      uint64_t hcr_el2)
558310cedf3SRichard Henderson {
559310cedf3SRichard Henderson     CPUARMState *env = cs->env_ptr;
560310cedf3SRichard Henderson     bool pstate_unmasked;
56116e07f78SRichard Henderson     bool unmasked = false;
562310cedf3SRichard Henderson 
563310cedf3SRichard Henderson     /*
564310cedf3SRichard Henderson      * Don't take exceptions if they target a lower EL.
565310cedf3SRichard Henderson      * This check should catch any exceptions that would not be taken
566310cedf3SRichard Henderson      * but left pending.
567310cedf3SRichard Henderson      */
568310cedf3SRichard Henderson     if (cur_el > target_el) {
569310cedf3SRichard Henderson         return false;
570310cedf3SRichard Henderson     }
571310cedf3SRichard Henderson 
572310cedf3SRichard Henderson     switch (excp_idx) {
573310cedf3SRichard Henderson     case EXCP_FIQ:
574310cedf3SRichard Henderson         pstate_unmasked = !(env->daif & PSTATE_F);
575310cedf3SRichard Henderson         break;
576310cedf3SRichard Henderson 
577310cedf3SRichard Henderson     case EXCP_IRQ:
578310cedf3SRichard Henderson         pstate_unmasked = !(env->daif & PSTATE_I);
579310cedf3SRichard Henderson         break;
580310cedf3SRichard Henderson 
581310cedf3SRichard Henderson     case EXCP_VFIQ:
582cc974d5cSRémi Denis-Courmont         if (!(hcr_el2 & HCR_FMO) || (hcr_el2 & HCR_TGE)) {
583cc974d5cSRémi Denis-Courmont             /* VFIQs are only taken when hypervized.  */
584310cedf3SRichard Henderson             return false;
585310cedf3SRichard Henderson         }
586310cedf3SRichard Henderson         return !(env->daif & PSTATE_F);
587310cedf3SRichard Henderson     case EXCP_VIRQ:
588cc974d5cSRémi Denis-Courmont         if (!(hcr_el2 & HCR_IMO) || (hcr_el2 & HCR_TGE)) {
589cc974d5cSRémi Denis-Courmont             /* VIRQs are only taken when hypervized.  */
590310cedf3SRichard Henderson             return false;
591310cedf3SRichard Henderson         }
592310cedf3SRichard Henderson         return !(env->daif & PSTATE_I);
5933c29632fSRichard Henderson     case EXCP_VSERR:
5943c29632fSRichard Henderson         if (!(hcr_el2 & HCR_AMO) || (hcr_el2 & HCR_TGE)) {
5953c29632fSRichard Henderson             /* VIRQs are only taken when hypervized.  */
5963c29632fSRichard Henderson             return false;
5973c29632fSRichard Henderson         }
5983c29632fSRichard Henderson         return !(env->daif & PSTATE_A);
599310cedf3SRichard Henderson     default:
600310cedf3SRichard Henderson         g_assert_not_reached();
601310cedf3SRichard Henderson     }
602310cedf3SRichard Henderson 
603310cedf3SRichard Henderson     /*
604310cedf3SRichard Henderson      * Use the target EL, current execution state and SCR/HCR settings to
605310cedf3SRichard Henderson      * determine whether the corresponding CPSR bit is used to mask the
606310cedf3SRichard Henderson      * interrupt.
607310cedf3SRichard Henderson      */
608310cedf3SRichard Henderson     if ((target_el > cur_el) && (target_el != 1)) {
609310cedf3SRichard Henderson         /* Exceptions targeting a higher EL may not be maskable */
610310cedf3SRichard Henderson         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
611c939a7c7SAke Koomsin             switch (target_el) {
612c939a7c7SAke Koomsin             case 2:
613310cedf3SRichard Henderson                 /*
614c939a7c7SAke Koomsin                  * According to ARM DDI 0487H.a, an interrupt can be masked
615c939a7c7SAke Koomsin                  * when HCR_E2H and HCR_TGE are both set regardless of the
616c939a7c7SAke Koomsin                  * current Security state. Note that we need to revisit this
617c939a7c7SAke Koomsin                  * part again once we need to support NMI.
618310cedf3SRichard Henderson                  */
619c939a7c7SAke Koomsin                 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
62016e07f78SRichard Henderson                         unmasked = true;
621310cedf3SRichard Henderson                 }
622c939a7c7SAke Koomsin                 break;
623c939a7c7SAke Koomsin             case 3:
624c939a7c7SAke Koomsin                 /* Interrupt cannot be masked when the target EL is 3 */
625c939a7c7SAke Koomsin                 unmasked = true;
626c939a7c7SAke Koomsin                 break;
627c939a7c7SAke Koomsin             default:
628c939a7c7SAke Koomsin                 g_assert_not_reached();
629c939a7c7SAke Koomsin             }
630310cedf3SRichard Henderson         } else {
631310cedf3SRichard Henderson             /*
632310cedf3SRichard Henderson              * The old 32-bit-only environment has a more complicated
633310cedf3SRichard Henderson              * masking setup. HCR and SCR bits not only affect interrupt
634310cedf3SRichard Henderson              * routing but also change the behaviour of masking.
635310cedf3SRichard Henderson              */
636310cedf3SRichard Henderson             bool hcr, scr;
637310cedf3SRichard Henderson 
638310cedf3SRichard Henderson             switch (excp_idx) {
639310cedf3SRichard Henderson             case EXCP_FIQ:
640310cedf3SRichard Henderson                 /*
641310cedf3SRichard Henderson                  * If FIQs are routed to EL3 or EL2 then there are cases where
642310cedf3SRichard Henderson                  * we override the CPSR.F in determining if the exception is
643310cedf3SRichard Henderson                  * masked or not. If neither of these are set then we fall back
644310cedf3SRichard Henderson                  * to the CPSR.F setting otherwise we further assess the state
645310cedf3SRichard Henderson                  * below.
646310cedf3SRichard Henderson                  */
647310cedf3SRichard Henderson                 hcr = hcr_el2 & HCR_FMO;
648310cedf3SRichard Henderson                 scr = (env->cp15.scr_el3 & SCR_FIQ);
649310cedf3SRichard Henderson 
650310cedf3SRichard Henderson                 /*
651310cedf3SRichard Henderson                  * When EL3 is 32-bit, the SCR.FW bit controls whether the
652310cedf3SRichard Henderson                  * CPSR.F bit masks FIQ interrupts when taken in non-secure
653310cedf3SRichard Henderson                  * state. If SCR.FW is set then FIQs can be masked by CPSR.F
654310cedf3SRichard Henderson                  * when non-secure but only when FIQs are only routed to EL3.
655310cedf3SRichard Henderson                  */
656310cedf3SRichard Henderson                 scr = scr && !((env->cp15.scr_el3 & SCR_FW) && !hcr);
657310cedf3SRichard Henderson                 break;
658310cedf3SRichard Henderson             case EXCP_IRQ:
659310cedf3SRichard Henderson                 /*
660310cedf3SRichard Henderson                  * When EL3 execution state is 32-bit, if HCR.IMO is set then
661310cedf3SRichard Henderson                  * we may override the CPSR.I masking when in non-secure state.
662310cedf3SRichard Henderson                  * The SCR.IRQ setting has already been taken into consideration
663310cedf3SRichard Henderson                  * when setting the target EL, so it does not have a further
664310cedf3SRichard Henderson                  * affect here.
665310cedf3SRichard Henderson                  */
666310cedf3SRichard Henderson                 hcr = hcr_el2 & HCR_IMO;
667310cedf3SRichard Henderson                 scr = false;
668310cedf3SRichard Henderson                 break;
669310cedf3SRichard Henderson             default:
670310cedf3SRichard Henderson                 g_assert_not_reached();
671310cedf3SRichard Henderson             }
672310cedf3SRichard Henderson 
673310cedf3SRichard Henderson             if ((scr || hcr) && !secure) {
67416e07f78SRichard Henderson                 unmasked = true;
675310cedf3SRichard Henderson             }
676310cedf3SRichard Henderson         }
677310cedf3SRichard Henderson     }
678310cedf3SRichard Henderson 
679310cedf3SRichard Henderson     /*
680673d8215SMichael Tokarev      * The PSTATE bits only mask the interrupt if we have not overridden the
681310cedf3SRichard Henderson      * ability above.
682310cedf3SRichard Henderson      */
683310cedf3SRichard Henderson     return unmasked || pstate_unmasked;
684310cedf3SRichard Henderson }
685310cedf3SRichard Henderson 
686083afd18SPhilippe Mathieu-Daudé static bool arm_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
687fcf5ef2aSThomas Huth {
688fcf5ef2aSThomas Huth     CPUClass *cc = CPU_GET_CLASS(cs);
689fcf5ef2aSThomas Huth     CPUARMState *env = cs->env_ptr;
690fcf5ef2aSThomas Huth     uint32_t cur_el = arm_current_el(env);
691fcf5ef2aSThomas Huth     bool secure = arm_is_secure(env);
692be879556SRichard Henderson     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
693fcf5ef2aSThomas Huth     uint32_t target_el;
694fcf5ef2aSThomas Huth     uint32_t excp_idx;
695d63d0ec5SRichard Henderson 
696d63d0ec5SRichard Henderson     /* The prioritization of interrupts is IMPLEMENTATION DEFINED. */
697fcf5ef2aSThomas Huth 
698fcf5ef2aSThomas Huth     if (interrupt_request & CPU_INTERRUPT_FIQ) {
699fcf5ef2aSThomas Huth         excp_idx = EXCP_FIQ;
700fcf5ef2aSThomas Huth         target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
701be879556SRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
702be879556SRichard Henderson                               cur_el, secure, hcr_el2)) {
703d63d0ec5SRichard Henderson             goto found;
704fcf5ef2aSThomas Huth         }
705fcf5ef2aSThomas Huth     }
706fcf5ef2aSThomas Huth     if (interrupt_request & CPU_INTERRUPT_HARD) {
707fcf5ef2aSThomas Huth         excp_idx = EXCP_IRQ;
708fcf5ef2aSThomas Huth         target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
709be879556SRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
710be879556SRichard Henderson                               cur_el, secure, hcr_el2)) {
711d63d0ec5SRichard Henderson             goto found;
712fcf5ef2aSThomas Huth         }
713fcf5ef2aSThomas Huth     }
714fcf5ef2aSThomas Huth     if (interrupt_request & CPU_INTERRUPT_VIRQ) {
715fcf5ef2aSThomas Huth         excp_idx = EXCP_VIRQ;
716fcf5ef2aSThomas Huth         target_el = 1;
717be879556SRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
718be879556SRichard Henderson                               cur_el, secure, hcr_el2)) {
719d63d0ec5SRichard Henderson             goto found;
720fcf5ef2aSThomas Huth         }
721fcf5ef2aSThomas Huth     }
722fcf5ef2aSThomas Huth     if (interrupt_request & CPU_INTERRUPT_VFIQ) {
723fcf5ef2aSThomas Huth         excp_idx = EXCP_VFIQ;
724fcf5ef2aSThomas Huth         target_el = 1;
725be879556SRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
726be879556SRichard Henderson                               cur_el, secure, hcr_el2)) {
727d63d0ec5SRichard Henderson             goto found;
728d63d0ec5SRichard Henderson         }
729d63d0ec5SRichard Henderson     }
7303c29632fSRichard Henderson     if (interrupt_request & CPU_INTERRUPT_VSERR) {
7313c29632fSRichard Henderson         excp_idx = EXCP_VSERR;
7323c29632fSRichard Henderson         target_el = 1;
7333c29632fSRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
7343c29632fSRichard Henderson                               cur_el, secure, hcr_el2)) {
7353c29632fSRichard Henderson             /* Taking a virtual abort clears HCR_EL2.VSE */
7363c29632fSRichard Henderson             env->cp15.hcr_el2 &= ~HCR_VSE;
7373c29632fSRichard Henderson             cpu_reset_interrupt(cs, CPU_INTERRUPT_VSERR);
7383c29632fSRichard Henderson             goto found;
7393c29632fSRichard Henderson         }
7403c29632fSRichard Henderson     }
741d63d0ec5SRichard Henderson     return false;
742d63d0ec5SRichard Henderson 
743d63d0ec5SRichard Henderson  found:
744fcf5ef2aSThomas Huth     cs->exception_index = excp_idx;
745fcf5ef2aSThomas Huth     env->exception.target_el = target_el;
74678271684SClaudio Fontana     cc->tcg_ops->do_interrupt(cs);
747d63d0ec5SRichard Henderson     return true;
748fcf5ef2aSThomas Huth }
7499e406eeaSPhilippe Mathieu-Daudé 
7509e406eeaSPhilippe Mathieu-Daudé #endif /* CONFIG_TCG && !CONFIG_USER_ONLY */
751fcf5ef2aSThomas Huth 
75289430fc6SPeter Maydell void arm_cpu_update_virq(ARMCPU *cpu)
75389430fc6SPeter Maydell {
75489430fc6SPeter Maydell     /*
75589430fc6SPeter Maydell      * Update the interrupt level for VIRQ, which is the logical OR of
75689430fc6SPeter Maydell      * the HCR_EL2.VI bit and the input line level from the GIC.
75789430fc6SPeter Maydell      */
75889430fc6SPeter Maydell     CPUARMState *env = &cpu->env;
75989430fc6SPeter Maydell     CPUState *cs = CPU(cpu);
76089430fc6SPeter Maydell 
76189430fc6SPeter Maydell     bool new_state = (env->cp15.hcr_el2 & HCR_VI) ||
76289430fc6SPeter Maydell         (env->irq_line_state & CPU_INTERRUPT_VIRQ);
76389430fc6SPeter Maydell 
76489430fc6SPeter Maydell     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VIRQ) != 0)) {
76589430fc6SPeter Maydell         if (new_state) {
76689430fc6SPeter Maydell             cpu_interrupt(cs, CPU_INTERRUPT_VIRQ);
76789430fc6SPeter Maydell         } else {
76889430fc6SPeter Maydell             cpu_reset_interrupt(cs, CPU_INTERRUPT_VIRQ);
76989430fc6SPeter Maydell         }
77089430fc6SPeter Maydell     }
77189430fc6SPeter Maydell }
77289430fc6SPeter Maydell 
77389430fc6SPeter Maydell void arm_cpu_update_vfiq(ARMCPU *cpu)
77489430fc6SPeter Maydell {
77589430fc6SPeter Maydell     /*
77689430fc6SPeter Maydell      * Update the interrupt level for VFIQ, which is the logical OR of
77789430fc6SPeter Maydell      * the HCR_EL2.VF bit and the input line level from the GIC.
77889430fc6SPeter Maydell      */
77989430fc6SPeter Maydell     CPUARMState *env = &cpu->env;
78089430fc6SPeter Maydell     CPUState *cs = CPU(cpu);
78189430fc6SPeter Maydell 
78289430fc6SPeter Maydell     bool new_state = (env->cp15.hcr_el2 & HCR_VF) ||
78389430fc6SPeter Maydell         (env->irq_line_state & CPU_INTERRUPT_VFIQ);
78489430fc6SPeter Maydell 
78589430fc6SPeter Maydell     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VFIQ) != 0)) {
78689430fc6SPeter Maydell         if (new_state) {
78789430fc6SPeter Maydell             cpu_interrupt(cs, CPU_INTERRUPT_VFIQ);
78889430fc6SPeter Maydell         } else {
78989430fc6SPeter Maydell             cpu_reset_interrupt(cs, CPU_INTERRUPT_VFIQ);
79089430fc6SPeter Maydell         }
79189430fc6SPeter Maydell     }
79289430fc6SPeter Maydell }
79389430fc6SPeter Maydell 
7943c29632fSRichard Henderson void arm_cpu_update_vserr(ARMCPU *cpu)
7953c29632fSRichard Henderson {
7963c29632fSRichard Henderson     /*
7973c29632fSRichard Henderson      * Update the interrupt level for VSERR, which is the HCR_EL2.VSE bit.
7983c29632fSRichard Henderson      */
7993c29632fSRichard Henderson     CPUARMState *env = &cpu->env;
8003c29632fSRichard Henderson     CPUState *cs = CPU(cpu);
8013c29632fSRichard Henderson 
8023c29632fSRichard Henderson     bool new_state = env->cp15.hcr_el2 & HCR_VSE;
8033c29632fSRichard Henderson 
8043c29632fSRichard Henderson     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VSERR) != 0)) {
8053c29632fSRichard Henderson         if (new_state) {
8063c29632fSRichard Henderson             cpu_interrupt(cs, CPU_INTERRUPT_VSERR);
8073c29632fSRichard Henderson         } else {
8083c29632fSRichard Henderson             cpu_reset_interrupt(cs, CPU_INTERRUPT_VSERR);
8093c29632fSRichard Henderson         }
8103c29632fSRichard Henderson     }
8113c29632fSRichard Henderson }
8123c29632fSRichard Henderson 
813fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
814fcf5ef2aSThomas Huth static void arm_cpu_set_irq(void *opaque, int irq, int level)
815fcf5ef2aSThomas Huth {
816fcf5ef2aSThomas Huth     ARMCPU *cpu = opaque;
817fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
818fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
819fcf5ef2aSThomas Huth     static const int mask[] = {
820fcf5ef2aSThomas Huth         [ARM_CPU_IRQ] = CPU_INTERRUPT_HARD,
821fcf5ef2aSThomas Huth         [ARM_CPU_FIQ] = CPU_INTERRUPT_FIQ,
822fcf5ef2aSThomas Huth         [ARM_CPU_VIRQ] = CPU_INTERRUPT_VIRQ,
823fcf5ef2aSThomas Huth         [ARM_CPU_VFIQ] = CPU_INTERRUPT_VFIQ
824fcf5ef2aSThomas Huth     };
825fcf5ef2aSThomas Huth 
8269acd2d33SPeter Maydell     if (!arm_feature(env, ARM_FEATURE_EL2) &&
8279acd2d33SPeter Maydell         (irq == ARM_CPU_VIRQ || irq == ARM_CPU_VFIQ)) {
8289acd2d33SPeter Maydell         /*
8299acd2d33SPeter Maydell          * The GIC might tell us about VIRQ and VFIQ state, but if we don't
8309acd2d33SPeter Maydell          * have EL2 support we don't care. (Unless the guest is doing something
8319acd2d33SPeter Maydell          * silly this will only be calls saying "level is still 0".)
8329acd2d33SPeter Maydell          */
8339acd2d33SPeter Maydell         return;
8349acd2d33SPeter Maydell     }
8359acd2d33SPeter Maydell 
836ed89f078SPeter Maydell     if (level) {
837ed89f078SPeter Maydell         env->irq_line_state |= mask[irq];
838ed89f078SPeter Maydell     } else {
839ed89f078SPeter Maydell         env->irq_line_state &= ~mask[irq];
840ed89f078SPeter Maydell     }
841ed89f078SPeter Maydell 
842fcf5ef2aSThomas Huth     switch (irq) {
843fcf5ef2aSThomas Huth     case ARM_CPU_VIRQ:
84489430fc6SPeter Maydell         arm_cpu_update_virq(cpu);
84589430fc6SPeter Maydell         break;
846fcf5ef2aSThomas Huth     case ARM_CPU_VFIQ:
84789430fc6SPeter Maydell         arm_cpu_update_vfiq(cpu);
84889430fc6SPeter Maydell         break;
849fcf5ef2aSThomas Huth     case ARM_CPU_IRQ:
850fcf5ef2aSThomas Huth     case ARM_CPU_FIQ:
851fcf5ef2aSThomas Huth         if (level) {
852fcf5ef2aSThomas Huth             cpu_interrupt(cs, mask[irq]);
853fcf5ef2aSThomas Huth         } else {
854fcf5ef2aSThomas Huth             cpu_reset_interrupt(cs, mask[irq]);
855fcf5ef2aSThomas Huth         }
856fcf5ef2aSThomas Huth         break;
857fcf5ef2aSThomas Huth     default:
858fcf5ef2aSThomas Huth         g_assert_not_reached();
859fcf5ef2aSThomas Huth     }
860fcf5ef2aSThomas Huth }
861fcf5ef2aSThomas Huth 
862fcf5ef2aSThomas Huth static void arm_cpu_kvm_set_irq(void *opaque, int irq, int level)
863fcf5ef2aSThomas Huth {
864fcf5ef2aSThomas Huth #ifdef CONFIG_KVM
865fcf5ef2aSThomas Huth     ARMCPU *cpu = opaque;
866ed89f078SPeter Maydell     CPUARMState *env = &cpu->env;
867fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
868ed89f078SPeter Maydell     uint32_t linestate_bit;
869f6530926SEric Auger     int irq_id;
870fcf5ef2aSThomas Huth 
871fcf5ef2aSThomas Huth     switch (irq) {
872fcf5ef2aSThomas Huth     case ARM_CPU_IRQ:
873f6530926SEric Auger         irq_id = KVM_ARM_IRQ_CPU_IRQ;
874ed89f078SPeter Maydell         linestate_bit = CPU_INTERRUPT_HARD;
875fcf5ef2aSThomas Huth         break;
876fcf5ef2aSThomas Huth     case ARM_CPU_FIQ:
877f6530926SEric Auger         irq_id = KVM_ARM_IRQ_CPU_FIQ;
878ed89f078SPeter Maydell         linestate_bit = CPU_INTERRUPT_FIQ;
879fcf5ef2aSThomas Huth         break;
880fcf5ef2aSThomas Huth     default:
881fcf5ef2aSThomas Huth         g_assert_not_reached();
882fcf5ef2aSThomas Huth     }
883ed89f078SPeter Maydell 
884ed89f078SPeter Maydell     if (level) {
885ed89f078SPeter Maydell         env->irq_line_state |= linestate_bit;
886ed89f078SPeter Maydell     } else {
887ed89f078SPeter Maydell         env->irq_line_state &= ~linestate_bit;
888ed89f078SPeter Maydell     }
889f6530926SEric Auger     kvm_arm_set_irq(cs->cpu_index, KVM_ARM_IRQ_TYPE_CPU, irq_id, !!level);
890fcf5ef2aSThomas Huth #endif
891fcf5ef2aSThomas Huth }
892fcf5ef2aSThomas Huth 
893fcf5ef2aSThomas Huth static bool arm_cpu_virtio_is_big_endian(CPUState *cs)
894fcf5ef2aSThomas Huth {
895fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(cs);
896fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
897fcf5ef2aSThomas Huth 
898fcf5ef2aSThomas Huth     cpu_synchronize_state(cs);
899fcf5ef2aSThomas Huth     return arm_cpu_data_is_big_endian(env);
900fcf5ef2aSThomas Huth }
901fcf5ef2aSThomas Huth 
902fcf5ef2aSThomas Huth #endif
903fcf5ef2aSThomas Huth 
904fcf5ef2aSThomas Huth static void arm_disas_set_info(CPUState *cpu, disassemble_info *info)
905fcf5ef2aSThomas Huth {
906fcf5ef2aSThomas Huth     ARMCPU *ac = ARM_CPU(cpu);
907fcf5ef2aSThomas Huth     CPUARMState *env = &ac->env;
9087bcdbf51SRichard Henderson     bool sctlr_b;
909fcf5ef2aSThomas Huth 
910fcf5ef2aSThomas Huth     if (is_a64(env)) {
911110f6c70SRichard Henderson         info->cap_arch = CS_ARCH_ARM64;
91215fa1a0aSRichard Henderson         info->cap_insn_unit = 4;
91315fa1a0aSRichard Henderson         info->cap_insn_split = 4;
914110f6c70SRichard Henderson     } else {
915110f6c70SRichard Henderson         int cap_mode;
916110f6c70SRichard Henderson         if (env->thumb) {
91715fa1a0aSRichard Henderson             info->cap_insn_unit = 2;
91815fa1a0aSRichard Henderson             info->cap_insn_split = 4;
919110f6c70SRichard Henderson             cap_mode = CS_MODE_THUMB;
920fcf5ef2aSThomas Huth         } else {
92115fa1a0aSRichard Henderson             info->cap_insn_unit = 4;
92215fa1a0aSRichard Henderson             info->cap_insn_split = 4;
923110f6c70SRichard Henderson             cap_mode = CS_MODE_ARM;
924fcf5ef2aSThomas Huth         }
925110f6c70SRichard Henderson         if (arm_feature(env, ARM_FEATURE_V8)) {
926110f6c70SRichard Henderson             cap_mode |= CS_MODE_V8;
927110f6c70SRichard Henderson         }
928110f6c70SRichard Henderson         if (arm_feature(env, ARM_FEATURE_M)) {
929110f6c70SRichard Henderson             cap_mode |= CS_MODE_MCLASS;
930110f6c70SRichard Henderson         }
931110f6c70SRichard Henderson         info->cap_arch = CS_ARCH_ARM;
932110f6c70SRichard Henderson         info->cap_mode = cap_mode;
933fcf5ef2aSThomas Huth     }
9347bcdbf51SRichard Henderson 
9357bcdbf51SRichard Henderson     sctlr_b = arm_sctlr_b(env);
9367bcdbf51SRichard Henderson     if (bswap_code(sctlr_b)) {
937ee3eb3a7SMarc-André Lureau #if TARGET_BIG_ENDIAN
938fcf5ef2aSThomas Huth         info->endian = BFD_ENDIAN_LITTLE;
939fcf5ef2aSThomas Huth #else
940fcf5ef2aSThomas Huth         info->endian = BFD_ENDIAN_BIG;
941fcf5ef2aSThomas Huth #endif
942fcf5ef2aSThomas Huth     }
943f7478a92SJulian Brown     info->flags &= ~INSN_ARM_BE32;
9447bcdbf51SRichard Henderson #ifndef CONFIG_USER_ONLY
9457bcdbf51SRichard Henderson     if (sctlr_b) {
946f7478a92SJulian Brown         info->flags |= INSN_ARM_BE32;
947f7478a92SJulian Brown     }
9487bcdbf51SRichard Henderson #endif
949fcf5ef2aSThomas Huth }
950fcf5ef2aSThomas Huth 
95186480615SPhilippe Mathieu-Daudé #ifdef TARGET_AARCH64
95286480615SPhilippe Mathieu-Daudé 
95386480615SPhilippe Mathieu-Daudé static void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags)
95486480615SPhilippe Mathieu-Daudé {
95586480615SPhilippe Mathieu-Daudé     ARMCPU *cpu = ARM_CPU(cs);
95686480615SPhilippe Mathieu-Daudé     CPUARMState *env = &cpu->env;
95786480615SPhilippe Mathieu-Daudé     uint32_t psr = pstate_read(env);
958a9d84070SRichard Henderson     int i, j;
95986480615SPhilippe Mathieu-Daudé     int el = arm_current_el(env);
96086480615SPhilippe Mathieu-Daudé     const char *ns_status;
9617a867dd5SRichard Henderson     bool sve;
96286480615SPhilippe Mathieu-Daudé 
96386480615SPhilippe Mathieu-Daudé     qemu_fprintf(f, " PC=%016" PRIx64 " ", env->pc);
96486480615SPhilippe Mathieu-Daudé     for (i = 0; i < 32; i++) {
96586480615SPhilippe Mathieu-Daudé         if (i == 31) {
96686480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, " SP=%016" PRIx64 "\n", env->xregs[i]);
96786480615SPhilippe Mathieu-Daudé         } else {
96886480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "X%02d=%016" PRIx64 "%s", i, env->xregs[i],
96986480615SPhilippe Mathieu-Daudé                          (i + 2) % 3 ? " " : "\n");
97086480615SPhilippe Mathieu-Daudé         }
97186480615SPhilippe Mathieu-Daudé     }
97286480615SPhilippe Mathieu-Daudé 
97386480615SPhilippe Mathieu-Daudé     if (arm_feature(env, ARM_FEATURE_EL3) && el != 3) {
97486480615SPhilippe Mathieu-Daudé         ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
97586480615SPhilippe Mathieu-Daudé     } else {
97686480615SPhilippe Mathieu-Daudé         ns_status = "";
97786480615SPhilippe Mathieu-Daudé     }
97886480615SPhilippe Mathieu-Daudé     qemu_fprintf(f, "PSTATE=%08x %c%c%c%c %sEL%d%c",
97986480615SPhilippe Mathieu-Daudé                  psr,
98086480615SPhilippe Mathieu-Daudé                  psr & PSTATE_N ? 'N' : '-',
98186480615SPhilippe Mathieu-Daudé                  psr & PSTATE_Z ? 'Z' : '-',
98286480615SPhilippe Mathieu-Daudé                  psr & PSTATE_C ? 'C' : '-',
98386480615SPhilippe Mathieu-Daudé                  psr & PSTATE_V ? 'V' : '-',
98486480615SPhilippe Mathieu-Daudé                  ns_status,
98586480615SPhilippe Mathieu-Daudé                  el,
98686480615SPhilippe Mathieu-Daudé                  psr & PSTATE_SP ? 'h' : 't');
98786480615SPhilippe Mathieu-Daudé 
9887a867dd5SRichard Henderson     if (cpu_isar_feature(aa64_sme, cpu)) {
9897a867dd5SRichard Henderson         qemu_fprintf(f, "  SVCR=%08" PRIx64 " %c%c",
9907a867dd5SRichard Henderson                      env->svcr,
9917a867dd5SRichard Henderson                      (FIELD_EX64(env->svcr, SVCR, ZA) ? 'Z' : '-'),
9927a867dd5SRichard Henderson                      (FIELD_EX64(env->svcr, SVCR, SM) ? 'S' : '-'));
9937a867dd5SRichard Henderson     }
99486480615SPhilippe Mathieu-Daudé     if (cpu_isar_feature(aa64_bti, cpu)) {
99586480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "  BTYPE=%d", (psr & PSTATE_BTYPE) >> 10);
99686480615SPhilippe Mathieu-Daudé     }
99786480615SPhilippe Mathieu-Daudé     if (!(flags & CPU_DUMP_FPU)) {
99886480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "\n");
99986480615SPhilippe Mathieu-Daudé         return;
100086480615SPhilippe Mathieu-Daudé     }
100186480615SPhilippe Mathieu-Daudé     if (fp_exception_el(env, el) != 0) {
100286480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "    FPU disabled\n");
100386480615SPhilippe Mathieu-Daudé         return;
100486480615SPhilippe Mathieu-Daudé     }
100586480615SPhilippe Mathieu-Daudé     qemu_fprintf(f, "     FPCR=%08x FPSR=%08x\n",
100686480615SPhilippe Mathieu-Daudé                  vfp_get_fpcr(env), vfp_get_fpsr(env));
100786480615SPhilippe Mathieu-Daudé 
10087a867dd5SRichard Henderson     if (cpu_isar_feature(aa64_sme, cpu) && FIELD_EX64(env->svcr, SVCR, SM)) {
10097a867dd5SRichard Henderson         sve = sme_exception_el(env, el) == 0;
10107a867dd5SRichard Henderson     } else if (cpu_isar_feature(aa64_sve, cpu)) {
10117a867dd5SRichard Henderson         sve = sve_exception_el(env, el) == 0;
10127a867dd5SRichard Henderson     } else {
10137a867dd5SRichard Henderson         sve = false;
10147a867dd5SRichard Henderson     }
10157a867dd5SRichard Henderson 
10167a867dd5SRichard Henderson     if (sve) {
1017a9d84070SRichard Henderson         int zcr_len = sve_vqm1_for_el(env, el);
101886480615SPhilippe Mathieu-Daudé 
101986480615SPhilippe Mathieu-Daudé         for (i = 0; i <= FFR_PRED_NUM; i++) {
102086480615SPhilippe Mathieu-Daudé             bool eol;
102186480615SPhilippe Mathieu-Daudé             if (i == FFR_PRED_NUM) {
102286480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "FFR=");
102386480615SPhilippe Mathieu-Daudé                 /* It's last, so end the line.  */
102486480615SPhilippe Mathieu-Daudé                 eol = true;
102586480615SPhilippe Mathieu-Daudé             } else {
102686480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "P%02d=", i);
102786480615SPhilippe Mathieu-Daudé                 switch (zcr_len) {
102886480615SPhilippe Mathieu-Daudé                 case 0:
102986480615SPhilippe Mathieu-Daudé                     eol = i % 8 == 7;
103086480615SPhilippe Mathieu-Daudé                     break;
103186480615SPhilippe Mathieu-Daudé                 case 1:
103286480615SPhilippe Mathieu-Daudé                     eol = i % 6 == 5;
103386480615SPhilippe Mathieu-Daudé                     break;
103486480615SPhilippe Mathieu-Daudé                 case 2:
103586480615SPhilippe Mathieu-Daudé                 case 3:
103686480615SPhilippe Mathieu-Daudé                     eol = i % 3 == 2;
103786480615SPhilippe Mathieu-Daudé                     break;
103886480615SPhilippe Mathieu-Daudé                 default:
103986480615SPhilippe Mathieu-Daudé                     /* More than one quadword per predicate.  */
104086480615SPhilippe Mathieu-Daudé                     eol = true;
104186480615SPhilippe Mathieu-Daudé                     break;
104286480615SPhilippe Mathieu-Daudé                 }
104386480615SPhilippe Mathieu-Daudé             }
104486480615SPhilippe Mathieu-Daudé             for (j = zcr_len / 4; j >= 0; j--) {
104586480615SPhilippe Mathieu-Daudé                 int digits;
104686480615SPhilippe Mathieu-Daudé                 if (j * 4 + 4 <= zcr_len + 1) {
104786480615SPhilippe Mathieu-Daudé                     digits = 16;
104886480615SPhilippe Mathieu-Daudé                 } else {
104986480615SPhilippe Mathieu-Daudé                     digits = (zcr_len % 4 + 1) * 4;
105086480615SPhilippe Mathieu-Daudé                 }
105186480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "%0*" PRIx64 "%s", digits,
105286480615SPhilippe Mathieu-Daudé                              env->vfp.pregs[i].p[j],
105386480615SPhilippe Mathieu-Daudé                              j ? ":" : eol ? "\n" : " ");
105486480615SPhilippe Mathieu-Daudé             }
105586480615SPhilippe Mathieu-Daudé         }
105686480615SPhilippe Mathieu-Daudé 
105786480615SPhilippe Mathieu-Daudé         if (zcr_len == 0) {
1058a9d84070SRichard Henderson             /*
1059a9d84070SRichard Henderson              * With vl=16, there are only 37 columns per register,
1060a9d84070SRichard Henderson              * so output two registers per line.
1061a9d84070SRichard Henderson              */
1062a9d84070SRichard Henderson             for (i = 0; i < 32; i++) {
106386480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "Z%02d=%016" PRIx64 ":%016" PRIx64 "%s",
106486480615SPhilippe Mathieu-Daudé                              i, env->vfp.zregs[i].d[1],
106586480615SPhilippe Mathieu-Daudé                              env->vfp.zregs[i].d[0], i & 1 ? "\n" : " ");
1066a9d84070SRichard Henderson             }
106786480615SPhilippe Mathieu-Daudé         } else {
1068a9d84070SRichard Henderson             for (i = 0; i < 32; i++) {
1069a9d84070SRichard Henderson                 qemu_fprintf(f, "Z%02d=", i);
107086480615SPhilippe Mathieu-Daudé                 for (j = zcr_len; j >= 0; j--) {
107186480615SPhilippe Mathieu-Daudé                     qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%s",
107286480615SPhilippe Mathieu-Daudé                                  env->vfp.zregs[i].d[j * 2 + 1],
1073a9d84070SRichard Henderson                                  env->vfp.zregs[i].d[j * 2 + 0],
1074a9d84070SRichard Henderson                                  j ? ":" : "\n");
107586480615SPhilippe Mathieu-Daudé                 }
107686480615SPhilippe Mathieu-Daudé             }
107786480615SPhilippe Mathieu-Daudé         }
107886480615SPhilippe Mathieu-Daudé     } else {
107986480615SPhilippe Mathieu-Daudé         for (i = 0; i < 32; i++) {
108086480615SPhilippe Mathieu-Daudé             uint64_t *q = aa64_vfp_qreg(env, i);
108186480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "Q%02d=%016" PRIx64 ":%016" PRIx64 "%s",
108286480615SPhilippe Mathieu-Daudé                          i, q[1], q[0], (i & 1 ? "\n" : " "));
108386480615SPhilippe Mathieu-Daudé         }
108486480615SPhilippe Mathieu-Daudé     }
1085270bea47SRichard Henderson 
1086270bea47SRichard Henderson     if (cpu_isar_feature(aa64_sme, cpu) &&
1087270bea47SRichard Henderson         FIELD_EX64(env->svcr, SVCR, ZA) &&
1088270bea47SRichard Henderson         sme_exception_el(env, el) == 0) {
1089270bea47SRichard Henderson         int zcr_len = sve_vqm1_for_el_sm(env, el, true);
1090270bea47SRichard Henderson         int svl = (zcr_len + 1) * 16;
1091270bea47SRichard Henderson         int svl_lg10 = svl < 100 ? 2 : 3;
1092270bea47SRichard Henderson 
1093270bea47SRichard Henderson         for (i = 0; i < svl; i++) {
1094270bea47SRichard Henderson             qemu_fprintf(f, "ZA[%0*d]=", svl_lg10, i);
1095270bea47SRichard Henderson             for (j = zcr_len; j >= 0; --j) {
1096270bea47SRichard Henderson                 qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%c",
1097270bea47SRichard Henderson                              env->zarray[i].d[2 * j + 1],
1098270bea47SRichard Henderson                              env->zarray[i].d[2 * j],
1099270bea47SRichard Henderson                              j ? ':' : '\n');
1100270bea47SRichard Henderson             }
1101270bea47SRichard Henderson         }
1102270bea47SRichard Henderson     }
110386480615SPhilippe Mathieu-Daudé }
110486480615SPhilippe Mathieu-Daudé 
110586480615SPhilippe Mathieu-Daudé #else
110686480615SPhilippe Mathieu-Daudé 
110786480615SPhilippe Mathieu-Daudé static inline void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags)
110886480615SPhilippe Mathieu-Daudé {
110986480615SPhilippe Mathieu-Daudé     g_assert_not_reached();
111086480615SPhilippe Mathieu-Daudé }
111186480615SPhilippe Mathieu-Daudé 
111286480615SPhilippe Mathieu-Daudé #endif
111386480615SPhilippe Mathieu-Daudé 
111486480615SPhilippe Mathieu-Daudé static void arm_cpu_dump_state(CPUState *cs, FILE *f, int flags)
111586480615SPhilippe Mathieu-Daudé {
111686480615SPhilippe Mathieu-Daudé     ARMCPU *cpu = ARM_CPU(cs);
111786480615SPhilippe Mathieu-Daudé     CPUARMState *env = &cpu->env;
111886480615SPhilippe Mathieu-Daudé     int i;
111986480615SPhilippe Mathieu-Daudé 
112086480615SPhilippe Mathieu-Daudé     if (is_a64(env)) {
112186480615SPhilippe Mathieu-Daudé         aarch64_cpu_dump_state(cs, f, flags);
112286480615SPhilippe Mathieu-Daudé         return;
112386480615SPhilippe Mathieu-Daudé     }
112486480615SPhilippe Mathieu-Daudé 
112586480615SPhilippe Mathieu-Daudé     for (i = 0; i < 16; i++) {
112686480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "R%02d=%08x", i, env->regs[i]);
112786480615SPhilippe Mathieu-Daudé         if ((i % 4) == 3) {
112886480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "\n");
112986480615SPhilippe Mathieu-Daudé         } else {
113086480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, " ");
113186480615SPhilippe Mathieu-Daudé         }
113286480615SPhilippe Mathieu-Daudé     }
113386480615SPhilippe Mathieu-Daudé 
113486480615SPhilippe Mathieu-Daudé     if (arm_feature(env, ARM_FEATURE_M)) {
113586480615SPhilippe Mathieu-Daudé         uint32_t xpsr = xpsr_read(env);
113686480615SPhilippe Mathieu-Daudé         const char *mode;
113786480615SPhilippe Mathieu-Daudé         const char *ns_status = "";
113886480615SPhilippe Mathieu-Daudé 
113986480615SPhilippe Mathieu-Daudé         if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
114086480615SPhilippe Mathieu-Daudé             ns_status = env->v7m.secure ? "S " : "NS ";
114186480615SPhilippe Mathieu-Daudé         }
114286480615SPhilippe Mathieu-Daudé 
114386480615SPhilippe Mathieu-Daudé         if (xpsr & XPSR_EXCP) {
114486480615SPhilippe Mathieu-Daudé             mode = "handler";
114586480615SPhilippe Mathieu-Daudé         } else {
114686480615SPhilippe Mathieu-Daudé             if (env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_NPRIV_MASK) {
114786480615SPhilippe Mathieu-Daudé                 mode = "unpriv-thread";
114886480615SPhilippe Mathieu-Daudé             } else {
114986480615SPhilippe Mathieu-Daudé                 mode = "priv-thread";
115086480615SPhilippe Mathieu-Daudé             }
115186480615SPhilippe Mathieu-Daudé         }
115286480615SPhilippe Mathieu-Daudé 
115386480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "XPSR=%08x %c%c%c%c %c %s%s\n",
115486480615SPhilippe Mathieu-Daudé                      xpsr,
115586480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_N ? 'N' : '-',
115686480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_Z ? 'Z' : '-',
115786480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_C ? 'C' : '-',
115886480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_V ? 'V' : '-',
115986480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_T ? 'T' : 'A',
116086480615SPhilippe Mathieu-Daudé                      ns_status,
116186480615SPhilippe Mathieu-Daudé                      mode);
116286480615SPhilippe Mathieu-Daudé     } else {
116386480615SPhilippe Mathieu-Daudé         uint32_t psr = cpsr_read(env);
116486480615SPhilippe Mathieu-Daudé         const char *ns_status = "";
116586480615SPhilippe Mathieu-Daudé 
116686480615SPhilippe Mathieu-Daudé         if (arm_feature(env, ARM_FEATURE_EL3) &&
116786480615SPhilippe Mathieu-Daudé             (psr & CPSR_M) != ARM_CPU_MODE_MON) {
116886480615SPhilippe Mathieu-Daudé             ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
116986480615SPhilippe Mathieu-Daudé         }
117086480615SPhilippe Mathieu-Daudé 
117186480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "PSR=%08x %c%c%c%c %c %s%s%d\n",
117286480615SPhilippe Mathieu-Daudé                      psr,
117386480615SPhilippe Mathieu-Daudé                      psr & CPSR_N ? 'N' : '-',
117486480615SPhilippe Mathieu-Daudé                      psr & CPSR_Z ? 'Z' : '-',
117586480615SPhilippe Mathieu-Daudé                      psr & CPSR_C ? 'C' : '-',
117686480615SPhilippe Mathieu-Daudé                      psr & CPSR_V ? 'V' : '-',
117786480615SPhilippe Mathieu-Daudé                      psr & CPSR_T ? 'T' : 'A',
117886480615SPhilippe Mathieu-Daudé                      ns_status,
117986480615SPhilippe Mathieu-Daudé                      aarch32_mode_name(psr), (psr & 0x10) ? 32 : 26);
118086480615SPhilippe Mathieu-Daudé     }
118186480615SPhilippe Mathieu-Daudé 
118286480615SPhilippe Mathieu-Daudé     if (flags & CPU_DUMP_FPU) {
118386480615SPhilippe Mathieu-Daudé         int numvfpregs = 0;
1184a6627f5fSRichard Henderson         if (cpu_isar_feature(aa32_simd_r32, cpu)) {
1185a6627f5fSRichard Henderson             numvfpregs = 32;
11867fbc6a40SRichard Henderson         } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
1187a6627f5fSRichard Henderson             numvfpregs = 16;
118886480615SPhilippe Mathieu-Daudé         }
118986480615SPhilippe Mathieu-Daudé         for (i = 0; i < numvfpregs; i++) {
119086480615SPhilippe Mathieu-Daudé             uint64_t v = *aa32_vfp_dreg(env, i);
119186480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "s%02d=%08x s%02d=%08x d%02d=%016" PRIx64 "\n",
119286480615SPhilippe Mathieu-Daudé                          i * 2, (uint32_t)v,
119386480615SPhilippe Mathieu-Daudé                          i * 2 + 1, (uint32_t)(v >> 32),
119486480615SPhilippe Mathieu-Daudé                          i, v);
119586480615SPhilippe Mathieu-Daudé         }
119686480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "FPSCR: %08x\n", vfp_get_fpscr(env));
1197aa291908SPeter Maydell         if (cpu_isar_feature(aa32_mve, cpu)) {
1198aa291908SPeter Maydell             qemu_fprintf(f, "VPR: %08x\n", env->v7m.vpr);
1199aa291908SPeter Maydell         }
120086480615SPhilippe Mathieu-Daudé     }
120186480615SPhilippe Mathieu-Daudé }
120286480615SPhilippe Mathieu-Daudé 
120346de5913SIgor Mammedov uint64_t arm_cpu_mp_affinity(int idx, uint8_t clustersz)
120446de5913SIgor Mammedov {
120546de5913SIgor Mammedov     uint32_t Aff1 = idx / clustersz;
120646de5913SIgor Mammedov     uint32_t Aff0 = idx % clustersz;
120746de5913SIgor Mammedov     return (Aff1 << ARM_AFF1_SHIFT) | Aff0;
120846de5913SIgor Mammedov }
120946de5913SIgor Mammedov 
1210fcf5ef2aSThomas Huth static void arm_cpu_initfn(Object *obj)
1211fcf5ef2aSThomas Huth {
1212fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(obj);
1213fcf5ef2aSThomas Huth 
12147506ed90SRichard Henderson     cpu_set_cpustate_pointers(cpu);
12155860362dSRichard Henderson     cpu->cp_regs = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1216c27f5d3aSRichard Henderson                                          NULL, g_free);
1217fcf5ef2aSThomas Huth 
1218b5c53d1bSAaron Lindsay     QLIST_INIT(&cpu->pre_el_change_hooks);
121908267487SAaron Lindsay     QLIST_INIT(&cpu->el_change_hooks);
122008267487SAaron Lindsay 
1221b3d52804SRichard Henderson #ifdef CONFIG_USER_ONLY
1222b3d52804SRichard Henderson # ifdef TARGET_AARCH64
1223b3d52804SRichard Henderson     /*
1224e74c0976SRichard Henderson      * The linux kernel defaults to 512-bit for SVE, and 256-bit for SME.
1225e74c0976SRichard Henderson      * These values were chosen to fit within the default signal frame.
1226e74c0976SRichard Henderson      * See documentation for /proc/sys/abi/{sve,sme}_default_vector_length,
1227e74c0976SRichard Henderson      * and our corresponding cpu property.
1228b3d52804SRichard Henderson      */
1229b3d52804SRichard Henderson     cpu->sve_default_vq = 4;
1230e74c0976SRichard Henderson     cpu->sme_default_vq = 2;
1231b3d52804SRichard Henderson # endif
1232b3d52804SRichard Henderson #else
1233fcf5ef2aSThomas Huth     /* Our inbound IRQ and FIQ lines */
1234fcf5ef2aSThomas Huth     if (kvm_enabled()) {
1235fcf5ef2aSThomas Huth         /* VIRQ and VFIQ are unused with KVM but we add them to maintain
1236fcf5ef2aSThomas Huth          * the same interface as non-KVM CPUs.
1237fcf5ef2aSThomas Huth          */
1238fcf5ef2aSThomas Huth         qdev_init_gpio_in(DEVICE(cpu), arm_cpu_kvm_set_irq, 4);
1239fcf5ef2aSThomas Huth     } else {
1240fcf5ef2aSThomas Huth         qdev_init_gpio_in(DEVICE(cpu), arm_cpu_set_irq, 4);
1241fcf5ef2aSThomas Huth     }
1242fcf5ef2aSThomas Huth 
1243fcf5ef2aSThomas Huth     qdev_init_gpio_out(DEVICE(cpu), cpu->gt_timer_outputs,
1244fcf5ef2aSThomas Huth                        ARRAY_SIZE(cpu->gt_timer_outputs));
1245aa1b3111SPeter Maydell 
1246aa1b3111SPeter Maydell     qdev_init_gpio_out_named(DEVICE(cpu), &cpu->gicv3_maintenance_interrupt,
1247aa1b3111SPeter Maydell                              "gicv3-maintenance-interrupt", 1);
124807f48730SAndrew Jones     qdev_init_gpio_out_named(DEVICE(cpu), &cpu->pmu_interrupt,
124907f48730SAndrew Jones                              "pmu-interrupt", 1);
1250fcf5ef2aSThomas Huth #endif
1251fcf5ef2aSThomas Huth 
1252fcf5ef2aSThomas Huth     /* DTB consumers generally don't in fact care what the 'compatible'
1253fcf5ef2aSThomas Huth      * string is, so always provide some string and trust that a hypothetical
1254fcf5ef2aSThomas Huth      * picky DTB consumer will also provide a helpful error message.
1255fcf5ef2aSThomas Huth      */
1256fcf5ef2aSThomas Huth     cpu->dtb_compatible = "qemu,unknown";
12570dc71c70SAkihiko Odaki     cpu->psci_version = QEMU_PSCI_VERSION_0_1; /* By default assume PSCI v0.1 */
1258fcf5ef2aSThomas Huth     cpu->kvm_target = QEMU_KVM_ARM_TARGET_NONE;
1259fcf5ef2aSThomas Huth 
12602c9c0bf9SAlexander Graf     if (tcg_enabled() || hvf_enabled()) {
12610dc71c70SAkihiko Odaki         /* TCG and HVF implement PSCI 1.1 */
12620dc71c70SAkihiko Odaki         cpu->psci_version = QEMU_PSCI_VERSION_1_1;
1263fcf5ef2aSThomas Huth     }
1264fcf5ef2aSThomas Huth }
1265fcf5ef2aSThomas Huth 
126696eec6b2SAndrew Jeffery static Property arm_cpu_gt_cntfrq_property =
126796eec6b2SAndrew Jeffery             DEFINE_PROP_UINT64("cntfrq", ARMCPU, gt_cntfrq_hz,
126896eec6b2SAndrew Jeffery                                NANOSECONDS_PER_SECOND / GTIMER_SCALE);
126996eec6b2SAndrew Jeffery 
1270fcf5ef2aSThomas Huth static Property arm_cpu_reset_cbar_property =
1271fcf5ef2aSThomas Huth             DEFINE_PROP_UINT64("reset-cbar", ARMCPU, reset_cbar, 0);
1272fcf5ef2aSThomas Huth 
1273fcf5ef2aSThomas Huth static Property arm_cpu_reset_hivecs_property =
1274fcf5ef2aSThomas Huth             DEFINE_PROP_BOOL("reset-hivecs", ARMCPU, reset_hivecs, false);
1275fcf5ef2aSThomas Huth 
127645ca3a14SRichard Henderson #ifndef CONFIG_USER_ONLY
1277c25bd18aSPeter Maydell static Property arm_cpu_has_el2_property =
1278c25bd18aSPeter Maydell             DEFINE_PROP_BOOL("has_el2", ARMCPU, has_el2, true);
1279c25bd18aSPeter Maydell 
1280fcf5ef2aSThomas Huth static Property arm_cpu_has_el3_property =
1281fcf5ef2aSThomas Huth             DEFINE_PROP_BOOL("has_el3", ARMCPU, has_el3, true);
128245ca3a14SRichard Henderson #endif
1283fcf5ef2aSThomas Huth 
12843a062d57SJulian Brown static Property arm_cpu_cfgend_property =
12853a062d57SJulian Brown             DEFINE_PROP_BOOL("cfgend", ARMCPU, cfgend, false);
12863a062d57SJulian Brown 
128797a28b0eSPeter Maydell static Property arm_cpu_has_vfp_property =
128897a28b0eSPeter Maydell             DEFINE_PROP_BOOL("vfp", ARMCPU, has_vfp, true);
128997a28b0eSPeter Maydell 
129042bea956SCédric Le Goater static Property arm_cpu_has_vfp_d32_property =
129142bea956SCédric Le Goater             DEFINE_PROP_BOOL("vfp-d32", ARMCPU, has_vfp_d32, true);
129242bea956SCédric Le Goater 
129397a28b0eSPeter Maydell static Property arm_cpu_has_neon_property =
129497a28b0eSPeter Maydell             DEFINE_PROP_BOOL("neon", ARMCPU, has_neon, true);
129597a28b0eSPeter Maydell 
1296ea90db0aSPeter Maydell static Property arm_cpu_has_dsp_property =
1297ea90db0aSPeter Maydell             DEFINE_PROP_BOOL("dsp", ARMCPU, has_dsp, true);
1298ea90db0aSPeter Maydell 
1299fcf5ef2aSThomas Huth static Property arm_cpu_has_mpu_property =
1300fcf5ef2aSThomas Huth             DEFINE_PROP_BOOL("has-mpu", ARMCPU, has_mpu, true);
1301fcf5ef2aSThomas Huth 
13028d92e26bSPeter Maydell /* This is like DEFINE_PROP_UINT32 but it doesn't set the default value,
13038d92e26bSPeter Maydell  * because the CPU initfn will have already set cpu->pmsav7_dregion to
13048d92e26bSPeter Maydell  * the right value for that particular CPU type, and we don't want
13058d92e26bSPeter Maydell  * to override that with an incorrect constant value.
13068d92e26bSPeter Maydell  */
1307fcf5ef2aSThomas Huth static Property arm_cpu_pmsav7_dregion_property =
13088d92e26bSPeter Maydell             DEFINE_PROP_UNSIGNED_NODEFAULT("pmsav7-dregion", ARMCPU,
13098d92e26bSPeter Maydell                                            pmsav7_dregion,
13108d92e26bSPeter Maydell                                            qdev_prop_uint32, uint32_t);
1311fcf5ef2aSThomas Huth 
1312ae502508SAndrew Jones static bool arm_get_pmu(Object *obj, Error **errp)
1313ae502508SAndrew Jones {
1314ae502508SAndrew Jones     ARMCPU *cpu = ARM_CPU(obj);
1315ae502508SAndrew Jones 
1316ae502508SAndrew Jones     return cpu->has_pmu;
1317ae502508SAndrew Jones }
1318ae502508SAndrew Jones 
1319ae502508SAndrew Jones static void arm_set_pmu(Object *obj, bool value, Error **errp)
1320ae502508SAndrew Jones {
1321ae502508SAndrew Jones     ARMCPU *cpu = ARM_CPU(obj);
1322ae502508SAndrew Jones 
1323ae502508SAndrew Jones     if (value) {
13247d20e681SPhilippe Mathieu-Daudé         if (kvm_enabled() && !kvm_arm_pmu_supported()) {
1325ae502508SAndrew Jones             error_setg(errp, "'pmu' feature not supported by KVM on this host");
1326ae502508SAndrew Jones             return;
1327ae502508SAndrew Jones         }
1328ae502508SAndrew Jones         set_feature(&cpu->env, ARM_FEATURE_PMU);
1329ae502508SAndrew Jones     } else {
1330ae502508SAndrew Jones         unset_feature(&cpu->env, ARM_FEATURE_PMU);
1331ae502508SAndrew Jones     }
1332ae502508SAndrew Jones     cpu->has_pmu = value;
1333ae502508SAndrew Jones }
1334ae502508SAndrew Jones 
13357def8754SAndrew Jeffery unsigned int gt_cntfrq_period_ns(ARMCPU *cpu)
13367def8754SAndrew Jeffery {
133796eec6b2SAndrew Jeffery     /*
133896eec6b2SAndrew Jeffery      * The exact approach to calculating guest ticks is:
133996eec6b2SAndrew Jeffery      *
134096eec6b2SAndrew Jeffery      *     muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), cpu->gt_cntfrq_hz,
134196eec6b2SAndrew Jeffery      *              NANOSECONDS_PER_SECOND);
134296eec6b2SAndrew Jeffery      *
134396eec6b2SAndrew Jeffery      * We don't do that. Rather we intentionally use integer division
134496eec6b2SAndrew Jeffery      * truncation below and in the caller for the conversion of host monotonic
134596eec6b2SAndrew Jeffery      * time to guest ticks to provide the exact inverse for the semantics of
134696eec6b2SAndrew Jeffery      * the QEMUTimer scale factor. QEMUTimer's scale facter is an integer, so
134796eec6b2SAndrew Jeffery      * it loses precision when representing frequencies where
134896eec6b2SAndrew Jeffery      * `(NANOSECONDS_PER_SECOND % cpu->gt_cntfrq) > 0` holds. Failing to
134996eec6b2SAndrew Jeffery      * provide an exact inverse leads to scheduling timers with negative
135096eec6b2SAndrew Jeffery      * periods, which in turn leads to sticky behaviour in the guest.
135196eec6b2SAndrew Jeffery      *
135296eec6b2SAndrew Jeffery      * Finally, CNTFRQ is effectively capped at 1GHz to ensure our scale factor
135396eec6b2SAndrew Jeffery      * cannot become zero.
135496eec6b2SAndrew Jeffery      */
13557def8754SAndrew Jeffery     return NANOSECONDS_PER_SECOND > cpu->gt_cntfrq_hz ?
13567def8754SAndrew Jeffery       NANOSECONDS_PER_SECOND / cpu->gt_cntfrq_hz : 1;
13577def8754SAndrew Jeffery }
13587def8754SAndrew Jeffery 
135951e5ef45SMarc-André Lureau void arm_cpu_post_init(Object *obj)
1360fcf5ef2aSThomas Huth {
1361fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(obj);
1362fcf5ef2aSThomas Huth 
1363790a1150SPeter Maydell     /* M profile implies PMSA. We have to do this here rather than
1364790a1150SPeter Maydell      * in realize with the other feature-implication checks because
1365790a1150SPeter Maydell      * we look at the PMSA bit to see if we should add some properties.
1366790a1150SPeter Maydell      */
1367790a1150SPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_M)) {
1368790a1150SPeter Maydell         set_feature(&cpu->env, ARM_FEATURE_PMSA);
1369790a1150SPeter Maydell     }
1370790a1150SPeter Maydell 
1371fcf5ef2aSThomas Huth     if (arm_feature(&cpu->env, ARM_FEATURE_CBAR) ||
1372fcf5ef2aSThomas Huth         arm_feature(&cpu->env, ARM_FEATURE_CBAR_RO)) {
137394d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_cbar_property);
1374fcf5ef2aSThomas Huth     }
1375fcf5ef2aSThomas Huth 
1376fcf5ef2aSThomas Huth     if (!arm_feature(&cpu->env, ARM_FEATURE_M)) {
137794d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_hivecs_property);
1378fcf5ef2aSThomas Huth     }
1379fcf5ef2aSThomas Huth 
1380910e4f24STobias Röhmel     if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
13814a7319b7SEdgar E. Iglesias         object_property_add_uint64_ptr(obj, "rvbar",
13824a7319b7SEdgar E. Iglesias                                        &cpu->rvbar_prop,
13834a7319b7SEdgar E. Iglesias                                        OBJ_PROP_FLAG_READWRITE);
1384fcf5ef2aSThomas Huth     }
1385fcf5ef2aSThomas Huth 
138645ca3a14SRichard Henderson #ifndef CONFIG_USER_ONLY
1387fcf5ef2aSThomas Huth     if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
1388fcf5ef2aSThomas Huth         /* Add the has_el3 state CPU property only if EL3 is allowed.  This will
1389fcf5ef2aSThomas Huth          * prevent "has_el3" from existing on CPUs which cannot support EL3.
1390fcf5ef2aSThomas Huth          */
139194d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el3_property);
1392fcf5ef2aSThomas Huth 
1393fcf5ef2aSThomas Huth         object_property_add_link(obj, "secure-memory",
1394fcf5ef2aSThomas Huth                                  TYPE_MEMORY_REGION,
1395fcf5ef2aSThomas Huth                                  (Object **)&cpu->secure_memory,
1396fcf5ef2aSThomas Huth                                  qdev_prop_allow_set_link_before_realize,
1397d2623129SMarkus Armbruster                                  OBJ_PROP_LINK_STRONG);
1398fcf5ef2aSThomas Huth     }
1399fcf5ef2aSThomas Huth 
1400c25bd18aSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_EL2)) {
140194d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el2_property);
1402c25bd18aSPeter Maydell     }
140345ca3a14SRichard Henderson #endif
1404c25bd18aSPeter Maydell 
1405fcf5ef2aSThomas Huth     if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) {
1406ae502508SAndrew Jones         cpu->has_pmu = true;
1407d2623129SMarkus Armbruster         object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu);
1408fcf5ef2aSThomas Huth     }
1409fcf5ef2aSThomas Huth 
141097a28b0eSPeter Maydell     /*
141197a28b0eSPeter Maydell      * Allow user to turn off VFP and Neon support, but only for TCG --
141297a28b0eSPeter Maydell      * KVM does not currently allow us to lie to the guest about its
141397a28b0eSPeter Maydell      * ID/feature registers, so the guest always sees what the host has.
141497a28b0eSPeter Maydell      */
14154315f7c6SRichard Henderson     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
14164315f7c6SRichard Henderson         if (cpu_isar_feature(aa64_fp_simd, cpu)) {
141797a28b0eSPeter Maydell             cpu->has_vfp = true;
141842bea956SCédric Le Goater             cpu->has_vfp_d32 = true;
14194315f7c6SRichard Henderson             if (tcg_enabled() || qtest_enabled()) {
14204315f7c6SRichard Henderson                 qdev_property_add_static(DEVICE(obj),
14214315f7c6SRichard Henderson                                          &arm_cpu_has_vfp_property);
14224315f7c6SRichard Henderson             }
14234315f7c6SRichard Henderson         }
14244315f7c6SRichard Henderson     } else if (cpu_isar_feature(aa32_vfp, cpu)) {
14254315f7c6SRichard Henderson         cpu->has_vfp = true;
14264315f7c6SRichard Henderson         if (cpu_isar_feature(aa32_simd_r32, cpu)) {
14274315f7c6SRichard Henderson             cpu->has_vfp_d32 = true;
142842bea956SCédric Le Goater             /*
142942bea956SCédric Le Goater              * The permitted values of the SIMDReg bits [3:0] on
143042bea956SCédric Le Goater              * Armv8-A are either 0b0000 and 0b0010. On such CPUs,
143142bea956SCédric Le Goater              * make sure that has_vfp_d32 can not be set to false.
143242bea956SCédric Le Goater              */
14334315f7c6SRichard Henderson             if ((tcg_enabled() || qtest_enabled())
14344315f7c6SRichard Henderson                 && !(arm_feature(&cpu->env, ARM_FEATURE_V8)
14354315f7c6SRichard Henderson                      && !arm_feature(&cpu->env, ARM_FEATURE_M))) {
143642bea956SCédric Le Goater                 qdev_property_add_static(DEVICE(obj),
143742bea956SCédric Le Goater                                          &arm_cpu_has_vfp_d32_property);
143842bea956SCédric Le Goater             }
143942bea956SCédric Le Goater         }
144042bea956SCédric Le Goater     }
144142bea956SCédric Le Goater 
144297a28b0eSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_NEON)) {
144397a28b0eSPeter Maydell         cpu->has_neon = true;
144497a28b0eSPeter Maydell         if (!kvm_enabled()) {
144594d912d1SMarc-André Lureau             qdev_property_add_static(DEVICE(obj), &arm_cpu_has_neon_property);
144697a28b0eSPeter Maydell         }
144797a28b0eSPeter Maydell     }
144897a28b0eSPeter Maydell 
1449ea90db0aSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_M) &&
1450ea90db0aSPeter Maydell         arm_feature(&cpu->env, ARM_FEATURE_THUMB_DSP)) {
145194d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_dsp_property);
1452ea90db0aSPeter Maydell     }
1453ea90db0aSPeter Maydell 
1454452a0955SPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_PMSA)) {
145594d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_mpu_property);
1456fcf5ef2aSThomas Huth         if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
1457fcf5ef2aSThomas Huth             qdev_property_add_static(DEVICE(obj),
145894d912d1SMarc-André Lureau                                      &arm_cpu_pmsav7_dregion_property);
1459fcf5ef2aSThomas Huth         }
1460fcf5ef2aSThomas Huth     }
1461fcf5ef2aSThomas Huth 
1462181962fdSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_M_SECURITY)) {
1463181962fdSPeter Maydell         object_property_add_link(obj, "idau", TYPE_IDAU_INTERFACE, &cpu->idau,
1464181962fdSPeter Maydell                                  qdev_prop_allow_set_link_before_realize,
1465d2623129SMarkus Armbruster                                  OBJ_PROP_LINK_STRONG);
1466f9f62e4cSPeter Maydell         /*
1467f9f62e4cSPeter Maydell          * M profile: initial value of the Secure VTOR. We can't just use
1468f9f62e4cSPeter Maydell          * a simple DEFINE_PROP_UINT32 for this because we want to permit
1469f9f62e4cSPeter Maydell          * the property to be set after realize.
1470f9f62e4cSPeter Maydell          */
147164a7b8deSFelipe Franciosi         object_property_add_uint32_ptr(obj, "init-svtor",
147264a7b8deSFelipe Franciosi                                        &cpu->init_svtor,
1473d2623129SMarkus Armbruster                                        OBJ_PROP_FLAG_READWRITE);
1474181962fdSPeter Maydell     }
14757cda2149SPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_M)) {
14767cda2149SPeter Maydell         /*
14777cda2149SPeter Maydell          * Initial value of the NS VTOR (for cores without the Security
14787cda2149SPeter Maydell          * extension, this is the only VTOR)
14797cda2149SPeter Maydell          */
14807cda2149SPeter Maydell         object_property_add_uint32_ptr(obj, "init-nsvtor",
14817cda2149SPeter Maydell                                        &cpu->init_nsvtor,
14827cda2149SPeter Maydell                                        OBJ_PROP_FLAG_READWRITE);
14837cda2149SPeter Maydell     }
1484181962fdSPeter Maydell 
1485bddd892eSPeter Maydell     /* Not DEFINE_PROP_UINT32: we want this to be settable after realize */
1486bddd892eSPeter Maydell     object_property_add_uint32_ptr(obj, "psci-conduit",
1487bddd892eSPeter Maydell                                    &cpu->psci_conduit,
1488bddd892eSPeter Maydell                                    OBJ_PROP_FLAG_READWRITE);
1489bddd892eSPeter Maydell 
149094d912d1SMarc-André Lureau     qdev_property_add_static(DEVICE(obj), &arm_cpu_cfgend_property);
149196eec6b2SAndrew Jeffery 
149296eec6b2SAndrew Jeffery     if (arm_feature(&cpu->env, ARM_FEATURE_GENERIC_TIMER)) {
149394d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(cpu), &arm_cpu_gt_cntfrq_property);
149496eec6b2SAndrew Jeffery     }
14959e6f8d8aSfangying 
14969e6f8d8aSfangying     if (kvm_enabled()) {
14979e6f8d8aSfangying         kvm_arm_add_vcpu_properties(obj);
14989e6f8d8aSfangying     }
14998bce44a2SRichard Henderson 
15008bce44a2SRichard Henderson #ifndef CONFIG_USER_ONLY
15018bce44a2SRichard Henderson     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) &&
15028bce44a2SRichard Henderson         cpu_isar_feature(aa64_mte, cpu)) {
15038bce44a2SRichard Henderson         object_property_add_link(obj, "tag-memory",
15048bce44a2SRichard Henderson                                  TYPE_MEMORY_REGION,
15058bce44a2SRichard Henderson                                  (Object **)&cpu->tag_memory,
15068bce44a2SRichard Henderson                                  qdev_prop_allow_set_link_before_realize,
15078bce44a2SRichard Henderson                                  OBJ_PROP_LINK_STRONG);
15088bce44a2SRichard Henderson 
15098bce44a2SRichard Henderson         if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
15108bce44a2SRichard Henderson             object_property_add_link(obj, "secure-tag-memory",
15118bce44a2SRichard Henderson                                      TYPE_MEMORY_REGION,
15128bce44a2SRichard Henderson                                      (Object **)&cpu->secure_tag_memory,
15138bce44a2SRichard Henderson                                      qdev_prop_allow_set_link_before_realize,
15148bce44a2SRichard Henderson                                      OBJ_PROP_LINK_STRONG);
15158bce44a2SRichard Henderson         }
15168bce44a2SRichard Henderson     }
15178bce44a2SRichard Henderson #endif
1518fcf5ef2aSThomas Huth }
1519fcf5ef2aSThomas Huth 
1520fcf5ef2aSThomas Huth static void arm_cpu_finalizefn(Object *obj)
1521fcf5ef2aSThomas Huth {
1522fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(obj);
152308267487SAaron Lindsay     ARMELChangeHook *hook, *next;
152408267487SAaron Lindsay 
1525fcf5ef2aSThomas Huth     g_hash_table_destroy(cpu->cp_regs);
152608267487SAaron Lindsay 
1527b5c53d1bSAaron Lindsay     QLIST_FOREACH_SAFE(hook, &cpu->pre_el_change_hooks, node, next) {
1528b5c53d1bSAaron Lindsay         QLIST_REMOVE(hook, node);
1529b5c53d1bSAaron Lindsay         g_free(hook);
1530b5c53d1bSAaron Lindsay     }
153108267487SAaron Lindsay     QLIST_FOREACH_SAFE(hook, &cpu->el_change_hooks, node, next) {
153208267487SAaron Lindsay         QLIST_REMOVE(hook, node);
153308267487SAaron Lindsay         g_free(hook);
153408267487SAaron Lindsay     }
15354e7beb0cSAaron Lindsay OS #ifndef CONFIG_USER_ONLY
15364e7beb0cSAaron Lindsay OS     if (cpu->pmu_timer) {
15374e7beb0cSAaron Lindsay OS         timer_free(cpu->pmu_timer);
15384e7beb0cSAaron Lindsay OS     }
15394e7beb0cSAaron Lindsay OS #endif
1540fcf5ef2aSThomas Huth }
1541fcf5ef2aSThomas Huth 
15420df9142dSAndrew Jones void arm_cpu_finalize_features(ARMCPU *cpu, Error **errp)
15430df9142dSAndrew Jones {
15440df9142dSAndrew Jones     Error *local_err = NULL;
15450df9142dSAndrew Jones 
154607301161SRichard Henderson #ifdef TARGET_AARCH64
15470df9142dSAndrew Jones     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
15480df9142dSAndrew Jones         arm_cpu_sve_finalize(cpu, &local_err);
15490df9142dSAndrew Jones         if (local_err != NULL) {
15500df9142dSAndrew Jones             error_propagate(errp, local_err);
15510df9142dSAndrew Jones             return;
15520df9142dSAndrew Jones         }
1553eb94284dSRichard Henderson 
1554e74c0976SRichard Henderson         arm_cpu_sme_finalize(cpu, &local_err);
1555e74c0976SRichard Henderson         if (local_err != NULL) {
1556e74c0976SRichard Henderson             error_propagate(errp, local_err);
1557e74c0976SRichard Henderson             return;
1558e74c0976SRichard Henderson         }
1559e74c0976SRichard Henderson 
1560eb94284dSRichard Henderson         arm_cpu_pauth_finalize(cpu, &local_err);
1561eb94284dSRichard Henderson         if (local_err != NULL) {
1562eb94284dSRichard Henderson             error_propagate(errp, local_err);
1563eb94284dSRichard Henderson             return;
1564eb94284dSRichard Henderson         }
156569b2265dSRichard Henderson 
156669b2265dSRichard Henderson         arm_cpu_lpa2_finalize(cpu, &local_err);
156769b2265dSRichard Henderson         if (local_err != NULL) {
156869b2265dSRichard Henderson             error_propagate(errp, local_err);
156969b2265dSRichard Henderson             return;
157069b2265dSRichard Henderson         }
1571eb94284dSRichard Henderson     }
157207301161SRichard Henderson #endif
157368970d1eSAndrew Jones 
157468970d1eSAndrew Jones     if (kvm_enabled()) {
157568970d1eSAndrew Jones         kvm_arm_steal_time_finalize(cpu, &local_err);
157668970d1eSAndrew Jones         if (local_err != NULL) {
157768970d1eSAndrew Jones             error_propagate(errp, local_err);
157868970d1eSAndrew Jones             return;
157968970d1eSAndrew Jones         }
158068970d1eSAndrew Jones     }
15810df9142dSAndrew Jones }
15820df9142dSAndrew Jones 
1583fcf5ef2aSThomas Huth static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
1584fcf5ef2aSThomas Huth {
1585fcf5ef2aSThomas Huth     CPUState *cs = CPU(dev);
1586fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(dev);
1587fcf5ef2aSThomas Huth     ARMCPUClass *acc = ARM_CPU_GET_CLASS(dev);
1588fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
1589fcf5ef2aSThomas Huth     int pagebits;
1590fcf5ef2aSThomas Huth     Error *local_err = NULL;
15910f8d06f1SRichard Henderson     bool no_aa32 = false;
1592fcf5ef2aSThomas Huth 
1593e607ea39SAnton Johansson     /* Use pc-relative instructions in system-mode */
1594e607ea39SAnton Johansson #ifndef CONFIG_USER_ONLY
1595e607ea39SAnton Johansson     cs->tcg_cflags |= CF_PCREL;
1596e607ea39SAnton Johansson #endif
1597e607ea39SAnton Johansson 
1598c4487d76SPeter Maydell     /* If we needed to query the host kernel for the CPU features
1599c4487d76SPeter Maydell      * then it's possible that might have failed in the initfn, but
1600c4487d76SPeter Maydell      * this is the first point where we can report it.
1601c4487d76SPeter Maydell      */
1602c4487d76SPeter Maydell     if (cpu->host_cpu_probe_failed) {
1603585df85eSPeter Maydell         if (!kvm_enabled() && !hvf_enabled()) {
1604585df85eSPeter Maydell             error_setg(errp, "The 'host' CPU type can only be used with KVM or HVF");
1605c4487d76SPeter Maydell         } else {
1606c4487d76SPeter Maydell             error_setg(errp, "Failed to retrieve host CPU features");
1607c4487d76SPeter Maydell         }
1608c4487d76SPeter Maydell         return;
1609c4487d76SPeter Maydell     }
1610c4487d76SPeter Maydell 
161195f87565SPeter Maydell #ifndef CONFIG_USER_ONLY
161295f87565SPeter Maydell     /* The NVIC and M-profile CPU are two halves of a single piece of
161395f87565SPeter Maydell      * hardware; trying to use one without the other is a command line
161495f87565SPeter Maydell      * error and will result in segfaults if not caught here.
161595f87565SPeter Maydell      */
161695f87565SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M)) {
161795f87565SPeter Maydell         if (!env->nvic) {
161895f87565SPeter Maydell             error_setg(errp, "This board cannot be used with Cortex-M CPUs");
161995f87565SPeter Maydell             return;
162095f87565SPeter Maydell         }
162195f87565SPeter Maydell     } else {
162295f87565SPeter Maydell         if (env->nvic) {
162395f87565SPeter Maydell             error_setg(errp, "This board can only be used with Cortex-M CPUs");
162495f87565SPeter Maydell             return;
162595f87565SPeter Maydell         }
162695f87565SPeter Maydell     }
1627397cd31fSPeter Maydell 
1628045e5064SAlexander Graf     if (!tcg_enabled() && !qtest_enabled()) {
162949e7f191SPeter Maydell         /*
1630045e5064SAlexander Graf          * We assume that no accelerator except TCG (and the "not really an
1631045e5064SAlexander Graf          * accelerator" qtest) can handle these features, because Arm hardware
1632045e5064SAlexander Graf          * virtualization can't virtualize them.
1633045e5064SAlexander Graf          *
163449e7f191SPeter Maydell          * Catch all the cases which might cause us to create more than one
163549e7f191SPeter Maydell          * address space for the CPU (otherwise we will assert() later in
163649e7f191SPeter Maydell          * cpu_address_space_init()).
163749e7f191SPeter Maydell          */
163849e7f191SPeter Maydell         if (arm_feature(env, ARM_FEATURE_M)) {
163949e7f191SPeter Maydell             error_setg(errp,
1640045e5064SAlexander Graf                        "Cannot enable %s when using an M-profile guest CPU",
1641045e5064SAlexander Graf                        current_accel_name());
164249e7f191SPeter Maydell             return;
164349e7f191SPeter Maydell         }
164449e7f191SPeter Maydell         if (cpu->has_el3) {
164549e7f191SPeter Maydell             error_setg(errp,
1646045e5064SAlexander Graf                        "Cannot enable %s when guest CPU has EL3 enabled",
1647045e5064SAlexander Graf                        current_accel_name());
164849e7f191SPeter Maydell             return;
164949e7f191SPeter Maydell         }
165049e7f191SPeter Maydell         if (cpu->tag_memory) {
165149e7f191SPeter Maydell             error_setg(errp,
1652d009607dSPeter Maydell                        "Cannot enable %s when guest CPUs has MTE enabled",
1653045e5064SAlexander Graf                        current_accel_name());
165449e7f191SPeter Maydell             return;
165549e7f191SPeter Maydell         }
165649e7f191SPeter Maydell     }
165749e7f191SPeter Maydell 
165896eec6b2SAndrew Jeffery     {
165996eec6b2SAndrew Jeffery         uint64_t scale;
166096eec6b2SAndrew Jeffery 
166196eec6b2SAndrew Jeffery         if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
166296eec6b2SAndrew Jeffery             if (!cpu->gt_cntfrq_hz) {
166396eec6b2SAndrew Jeffery                 error_setg(errp, "Invalid CNTFRQ: %"PRId64"Hz",
166496eec6b2SAndrew Jeffery                            cpu->gt_cntfrq_hz);
166596eec6b2SAndrew Jeffery                 return;
166696eec6b2SAndrew Jeffery             }
166796eec6b2SAndrew Jeffery             scale = gt_cntfrq_period_ns(cpu);
166896eec6b2SAndrew Jeffery         } else {
166996eec6b2SAndrew Jeffery             scale = GTIMER_SCALE;
167096eec6b2SAndrew Jeffery         }
167196eec6b2SAndrew Jeffery 
167296eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_PHYS] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
1673397cd31fSPeter Maydell                                                arm_gt_ptimer_cb, cpu);
167496eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_VIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
1675397cd31fSPeter Maydell                                                arm_gt_vtimer_cb, cpu);
167696eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_HYP] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
1677397cd31fSPeter Maydell                                               arm_gt_htimer_cb, cpu);
167896eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_SEC] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
1679397cd31fSPeter Maydell                                               arm_gt_stimer_cb, cpu);
16808c94b071SRichard Henderson         cpu->gt_timer[GTIMER_HYPVIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
16818c94b071SRichard Henderson                                                   arm_gt_hvtimer_cb, cpu);
168296eec6b2SAndrew Jeffery     }
168395f87565SPeter Maydell #endif
168495f87565SPeter Maydell 
1685fcf5ef2aSThomas Huth     cpu_exec_realizefn(cs, &local_err);
1686fcf5ef2aSThomas Huth     if (local_err != NULL) {
1687fcf5ef2aSThomas Huth         error_propagate(errp, local_err);
1688fcf5ef2aSThomas Huth         return;
1689fcf5ef2aSThomas Huth     }
1690fcf5ef2aSThomas Huth 
16910df9142dSAndrew Jones     arm_cpu_finalize_features(cpu, &local_err);
16920df9142dSAndrew Jones     if (local_err != NULL) {
16930df9142dSAndrew Jones         error_propagate(errp, local_err);
16940df9142dSAndrew Jones         return;
16950df9142dSAndrew Jones     }
16960df9142dSAndrew Jones 
16979719f125SJohn Högberg #ifdef CONFIG_USER_ONLY
16989719f125SJohn Högberg     /*
16999719f125SJohn Högberg      * User mode relies on IC IVAU instructions to catch modification of
17009719f125SJohn Högberg      * dual-mapped code.
17019719f125SJohn Högberg      *
17029719f125SJohn Högberg      * Clear CTR_EL0.DIC to ensure that software that honors these flags uses
17039719f125SJohn Högberg      * IC IVAU even if the emulated processor does not normally require it.
17049719f125SJohn Högberg      */
17059719f125SJohn Högberg     cpu->ctr = FIELD_DP64(cpu->ctr, CTR_EL0, DIC, 0);
17069719f125SJohn Högberg #endif
17079719f125SJohn Högberg 
170897a28b0eSPeter Maydell     if (arm_feature(env, ARM_FEATURE_AARCH64) &&
170997a28b0eSPeter Maydell         cpu->has_vfp != cpu->has_neon) {
171097a28b0eSPeter Maydell         /*
171197a28b0eSPeter Maydell          * This is an architectural requirement for AArch64; AArch32 is
171297a28b0eSPeter Maydell          * more flexible and permits VFP-no-Neon and Neon-no-VFP.
171397a28b0eSPeter Maydell          */
171497a28b0eSPeter Maydell         error_setg(errp,
171597a28b0eSPeter Maydell                    "AArch64 CPUs must have both VFP and Neon or neither");
171697a28b0eSPeter Maydell         return;
171797a28b0eSPeter Maydell     }
171897a28b0eSPeter Maydell 
171942bea956SCédric Le Goater     if (cpu->has_vfp_d32 != cpu->has_neon) {
172042bea956SCédric Le Goater         error_setg(errp, "ARM CPUs must have both VFP-D32 and Neon or neither");
172142bea956SCédric Le Goater         return;
172242bea956SCédric Le Goater     }
172342bea956SCédric Le Goater 
172442bea956SCédric Le Goater    if (!cpu->has_vfp_d32) {
172542bea956SCédric Le Goater         uint32_t u;
172642bea956SCédric Le Goater 
172742bea956SCédric Le Goater         u = cpu->isar.mvfr0;
172842bea956SCédric Le Goater         u = FIELD_DP32(u, MVFR0, SIMDREG, 1); /* 16 registers */
172942bea956SCédric Le Goater         cpu->isar.mvfr0 = u;
173042bea956SCédric Le Goater     }
173142bea956SCédric Le Goater 
173297a28b0eSPeter Maydell     if (!cpu->has_vfp) {
173397a28b0eSPeter Maydell         uint64_t t;
173497a28b0eSPeter Maydell         uint32_t u;
173597a28b0eSPeter Maydell 
173697a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar1;
173797a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR1, JSCVT, 0);
173897a28b0eSPeter Maydell         cpu->isar.id_aa64isar1 = t;
173997a28b0eSPeter Maydell 
174097a28b0eSPeter Maydell         t = cpu->isar.id_aa64pfr0;
174197a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64PFR0, FP, 0xf);
174297a28b0eSPeter Maydell         cpu->isar.id_aa64pfr0 = t;
174397a28b0eSPeter Maydell 
174497a28b0eSPeter Maydell         u = cpu->isar.id_isar6;
174597a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR6, JSCVT, 0);
17463c93dfa4SRichard Henderson         u = FIELD_DP32(u, ID_ISAR6, BF16, 0);
174797a28b0eSPeter Maydell         cpu->isar.id_isar6 = u;
174897a28b0eSPeter Maydell 
174997a28b0eSPeter Maydell         u = cpu->isar.mvfr0;
175097a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPSP, 0);
175197a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPDP, 0);
175297a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPDIVIDE, 0);
175397a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPSQRT, 0);
175497a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPROUND, 0);
1755532a3af5SPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
1756532a3af5SPeter Maydell             u = FIELD_DP32(u, MVFR0, FPTRAP, 0);
1757532a3af5SPeter Maydell             u = FIELD_DP32(u, MVFR0, FPSHVEC, 0);
1758532a3af5SPeter Maydell         }
175997a28b0eSPeter Maydell         cpu->isar.mvfr0 = u;
176097a28b0eSPeter Maydell 
176197a28b0eSPeter Maydell         u = cpu->isar.mvfr1;
176297a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR1, FPFTZ, 0);
176397a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR1, FPDNAN, 0);
176497a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR1, FPHP, 0);
1765532a3af5SPeter Maydell         if (arm_feature(env, ARM_FEATURE_M)) {
1766532a3af5SPeter Maydell             u = FIELD_DP32(u, MVFR1, FP16, 0);
1767532a3af5SPeter Maydell         }
176897a28b0eSPeter Maydell         cpu->isar.mvfr1 = u;
176997a28b0eSPeter Maydell 
177097a28b0eSPeter Maydell         u = cpu->isar.mvfr2;
177197a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR2, FPMISC, 0);
177297a28b0eSPeter Maydell         cpu->isar.mvfr2 = u;
177397a28b0eSPeter Maydell     }
177497a28b0eSPeter Maydell 
177597a28b0eSPeter Maydell     if (!cpu->has_neon) {
177697a28b0eSPeter Maydell         uint64_t t;
177797a28b0eSPeter Maydell         uint32_t u;
177897a28b0eSPeter Maydell 
177997a28b0eSPeter Maydell         unset_feature(env, ARM_FEATURE_NEON);
178097a28b0eSPeter Maydell 
178197a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar0;
1782eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, AES, 0);
1783eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SHA1, 0);
1784eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SHA2, 0);
1785eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SHA3, 0);
1786eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SM3, 0);
1787eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SM4, 0);
178897a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR0, DP, 0);
178997a28b0eSPeter Maydell         cpu->isar.id_aa64isar0 = t;
179097a28b0eSPeter Maydell 
179197a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar1;
179297a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR1, FCMA, 0);
17933c93dfa4SRichard Henderson         t = FIELD_DP64(t, ID_AA64ISAR1, BF16, 0);
1794f8680aaaSRichard Henderson         t = FIELD_DP64(t, ID_AA64ISAR1, I8MM, 0);
179597a28b0eSPeter Maydell         cpu->isar.id_aa64isar1 = t;
179697a28b0eSPeter Maydell 
179797a28b0eSPeter Maydell         t = cpu->isar.id_aa64pfr0;
179897a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64PFR0, ADVSIMD, 0xf);
179997a28b0eSPeter Maydell         cpu->isar.id_aa64pfr0 = t;
180097a28b0eSPeter Maydell 
180197a28b0eSPeter Maydell         u = cpu->isar.id_isar5;
1802eb851c11SDamien Hedde         u = FIELD_DP32(u, ID_ISAR5, AES, 0);
1803eb851c11SDamien Hedde         u = FIELD_DP32(u, ID_ISAR5, SHA1, 0);
1804eb851c11SDamien Hedde         u = FIELD_DP32(u, ID_ISAR5, SHA2, 0);
180597a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR5, RDM, 0);
180697a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR5, VCMA, 0);
180797a28b0eSPeter Maydell         cpu->isar.id_isar5 = u;
180897a28b0eSPeter Maydell 
180997a28b0eSPeter Maydell         u = cpu->isar.id_isar6;
181097a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR6, DP, 0);
181197a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR6, FHM, 0);
18123c93dfa4SRichard Henderson         u = FIELD_DP32(u, ID_ISAR6, BF16, 0);
1813f8680aaaSRichard Henderson         u = FIELD_DP32(u, ID_ISAR6, I8MM, 0);
181497a28b0eSPeter Maydell         cpu->isar.id_isar6 = u;
181597a28b0eSPeter Maydell 
1816532a3af5SPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
181797a28b0eSPeter Maydell             u = cpu->isar.mvfr1;
181897a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDLS, 0);
181997a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDINT, 0);
182097a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDSP, 0);
182197a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDHP, 0);
182297a28b0eSPeter Maydell             cpu->isar.mvfr1 = u;
182397a28b0eSPeter Maydell 
182497a28b0eSPeter Maydell             u = cpu->isar.mvfr2;
182597a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR2, SIMDMISC, 0);
182697a28b0eSPeter Maydell             cpu->isar.mvfr2 = u;
182797a28b0eSPeter Maydell         }
1828532a3af5SPeter Maydell     }
182997a28b0eSPeter Maydell 
183097a28b0eSPeter Maydell     if (!cpu->has_neon && !cpu->has_vfp) {
183197a28b0eSPeter Maydell         uint64_t t;
183297a28b0eSPeter Maydell         uint32_t u;
183397a28b0eSPeter Maydell 
183497a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar0;
183597a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR0, FHM, 0);
183697a28b0eSPeter Maydell         cpu->isar.id_aa64isar0 = t;
183797a28b0eSPeter Maydell 
183897a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar1;
183997a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR1, FRINTTS, 0);
184097a28b0eSPeter Maydell         cpu->isar.id_aa64isar1 = t;
184197a28b0eSPeter Maydell 
184297a28b0eSPeter Maydell         u = cpu->isar.mvfr0;
184397a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, SIMDREG, 0);
184497a28b0eSPeter Maydell         cpu->isar.mvfr0 = u;
1845c52881bbSRichard Henderson 
1846c52881bbSRichard Henderson         /* Despite the name, this field covers both VFP and Neon */
1847c52881bbSRichard Henderson         u = cpu->isar.mvfr1;
1848c52881bbSRichard Henderson         u = FIELD_DP32(u, MVFR1, SIMDFMAC, 0);
1849c52881bbSRichard Henderson         cpu->isar.mvfr1 = u;
185097a28b0eSPeter Maydell     }
185197a28b0eSPeter Maydell 
1852ea90db0aSPeter Maydell     if (arm_feature(env, ARM_FEATURE_M) && !cpu->has_dsp) {
1853ea90db0aSPeter Maydell         uint32_t u;
1854ea90db0aSPeter Maydell 
1855ea90db0aSPeter Maydell         unset_feature(env, ARM_FEATURE_THUMB_DSP);
1856ea90db0aSPeter Maydell 
1857ea90db0aSPeter Maydell         u = cpu->isar.id_isar1;
1858ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR1, EXTEND, 1);
1859ea90db0aSPeter Maydell         cpu->isar.id_isar1 = u;
1860ea90db0aSPeter Maydell 
1861ea90db0aSPeter Maydell         u = cpu->isar.id_isar2;
1862ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR2, MULTU, 1);
1863ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR2, MULTS, 1);
1864ea90db0aSPeter Maydell         cpu->isar.id_isar2 = u;
1865ea90db0aSPeter Maydell 
1866ea90db0aSPeter Maydell         u = cpu->isar.id_isar3;
1867ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR3, SIMD, 1);
1868ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR3, SATURATE, 0);
1869ea90db0aSPeter Maydell         cpu->isar.id_isar3 = u;
1870ea90db0aSPeter Maydell     }
1871ea90db0aSPeter Maydell 
1872fcf5ef2aSThomas Huth     /* Some features automatically imply others: */
1873fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_V8)) {
18745256df88SRichard Henderson         if (arm_feature(env, ARM_FEATURE_M)) {
18755256df88SRichard Henderson             set_feature(env, ARM_FEATURE_V7);
18765256df88SRichard Henderson         } else {
18775110e683SAaron Lindsay             set_feature(env, ARM_FEATURE_V7VE);
18785110e683SAaron Lindsay         }
18795256df88SRichard Henderson     }
18800f8d06f1SRichard Henderson 
18810f8d06f1SRichard Henderson     /*
18820f8d06f1SRichard Henderson      * There exist AArch64 cpus without AArch32 support.  When KVM
18830f8d06f1SRichard Henderson      * queries ID_ISAR0_EL1 on such a host, the value is UNKNOWN.
18840f8d06f1SRichard Henderson      * Similarly, we cannot check ID_AA64PFR0 without AArch64 support.
18858f4821d7SPeter Maydell      * As a general principle, we also do not make ID register
18868f4821d7SPeter Maydell      * consistency checks anywhere unless using TCG, because only
18878f4821d7SPeter Maydell      * for TCG would a consistency-check failure be a QEMU bug.
18880f8d06f1SRichard Henderson      */
18890f8d06f1SRichard Henderson     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
18900f8d06f1SRichard Henderson         no_aa32 = !cpu_isar_feature(aa64_aa32, cpu);
18910f8d06f1SRichard Henderson     }
18920f8d06f1SRichard Henderson 
18935110e683SAaron Lindsay     if (arm_feature(env, ARM_FEATURE_V7VE)) {
18945110e683SAaron Lindsay         /* v7 Virtualization Extensions. In real hardware this implies
18955110e683SAaron Lindsay          * EL2 and also the presence of the Security Extensions.
18965110e683SAaron Lindsay          * For QEMU, for backwards-compatibility we implement some
18975110e683SAaron Lindsay          * CPUs or CPU configs which have no actual EL2 or EL3 but do
18985110e683SAaron Lindsay          * include the various other features that V7VE implies.
18995110e683SAaron Lindsay          * Presence of EL2 itself is ARM_FEATURE_EL2, and of the
19005110e683SAaron Lindsay          * Security Extensions is ARM_FEATURE_EL3.
19015110e683SAaron Lindsay          */
1902873b73c0SPeter Maydell         assert(!tcg_enabled() || no_aa32 ||
1903873b73c0SPeter Maydell                cpu_isar_feature(aa32_arm_div, cpu));
1904fcf5ef2aSThomas Huth         set_feature(env, ARM_FEATURE_LPAE);
19055110e683SAaron Lindsay         set_feature(env, ARM_FEATURE_V7);
1906fcf5ef2aSThomas Huth     }
1907fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_V7)) {
1908fcf5ef2aSThomas Huth         set_feature(env, ARM_FEATURE_VAPA);
1909fcf5ef2aSThomas Huth         set_feature(env, ARM_FEATURE_THUMB2);
1910fcf5ef2aSThomas Huth         set_feature(env, ARM_FEATURE_MPIDR);
1911fcf5ef2aSThomas Huth         if (!arm_feature(env, ARM_FEATURE_M)) {
1912fcf5ef2aSThomas Huth             set_feature(env, ARM_FEATURE_V6K);
1913fcf5ef2aSThomas Huth         } else {
1914fcf5ef2aSThomas Huth             set_feature(env, ARM_FEATURE_V6);
1915fcf5ef2aSThomas Huth         }
191691db4642SCédric Le Goater 
191791db4642SCédric Le Goater         /* Always define VBAR for V7 CPUs even if it doesn't exist in
191891db4642SCédric Le Goater          * non-EL3 configs. This is needed by some legacy boards.
191991db4642SCédric Le Goater          */
192091db4642SCédric Le Goater         set_feature(env, ARM_FEATURE_VBAR);
1921fcf5ef2aSThomas Huth     }
1922fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_V6K)) {
1923fcf5ef2aSThomas Huth         set_feature(env, ARM_FEATURE_V6);
1924fcf5ef2aSThomas Huth         set_feature(env, ARM_FEATURE_MVFR);
1925fcf5ef2aSThomas Huth     }
1926fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_V6)) {
1927fcf5ef2aSThomas Huth         set_feature(env, ARM_FEATURE_V5);
1928fcf5ef2aSThomas Huth         if (!arm_feature(env, ARM_FEATURE_M)) {
1929873b73c0SPeter Maydell             assert(!tcg_enabled() || no_aa32 ||
1930873b73c0SPeter Maydell                    cpu_isar_feature(aa32_jazelle, cpu));
1931fcf5ef2aSThomas Huth             set_feature(env, ARM_FEATURE_AUXCR);
1932fcf5ef2aSThomas Huth         }
1933fcf5ef2aSThomas Huth     }
1934fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_V5)) {
1935fcf5ef2aSThomas Huth         set_feature(env, ARM_FEATURE_V4T);
1936fcf5ef2aSThomas Huth     }
1937fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_LPAE)) {
1938fcf5ef2aSThomas Huth         set_feature(env, ARM_FEATURE_V7MP);
1939fcf5ef2aSThomas Huth     }
1940fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
1941fcf5ef2aSThomas Huth         set_feature(env, ARM_FEATURE_CBAR);
1942fcf5ef2aSThomas Huth     }
1943fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_THUMB2) &&
1944fcf5ef2aSThomas Huth         !arm_feature(env, ARM_FEATURE_M)) {
1945fcf5ef2aSThomas Huth         set_feature(env, ARM_FEATURE_THUMB_DSP);
1946fcf5ef2aSThomas Huth     }
1947fcf5ef2aSThomas Huth 
1948ea7ac69dSPeter Maydell     /*
1949ea7ac69dSPeter Maydell      * We rely on no XScale CPU having VFP so we can use the same bits in the
1950ea7ac69dSPeter Maydell      * TB flags field for VECSTRIDE and XSCALE_CPAR.
1951ea7ac69dSPeter Maydell      */
19527d63183fSRichard Henderson     assert(arm_feature(&cpu->env, ARM_FEATURE_AARCH64) ||
19537d63183fSRichard Henderson            !cpu_isar_feature(aa32_vfp_simd, cpu) ||
19547d63183fSRichard Henderson            !arm_feature(env, ARM_FEATURE_XSCALE));
1955ea7ac69dSPeter Maydell 
1956fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_V7) &&
1957fcf5ef2aSThomas Huth         !arm_feature(env, ARM_FEATURE_M) &&
1958452a0955SPeter Maydell         !arm_feature(env, ARM_FEATURE_PMSA)) {
1959fcf5ef2aSThomas Huth         /* v7VMSA drops support for the old ARMv5 tiny pages, so we
1960fcf5ef2aSThomas Huth          * can use 4K pages.
1961fcf5ef2aSThomas Huth          */
1962fcf5ef2aSThomas Huth         pagebits = 12;
1963fcf5ef2aSThomas Huth     } else {
1964fcf5ef2aSThomas Huth         /* For CPUs which might have tiny 1K pages, or which have an
1965fcf5ef2aSThomas Huth          * MPU and might have small region sizes, stick with 1K pages.
1966fcf5ef2aSThomas Huth          */
1967fcf5ef2aSThomas Huth         pagebits = 10;
1968fcf5ef2aSThomas Huth     }
1969fcf5ef2aSThomas Huth     if (!set_preferred_target_page_bits(pagebits)) {
1970fcf5ef2aSThomas Huth         /* This can only ever happen for hotplugging a CPU, or if
1971fcf5ef2aSThomas Huth          * the board code incorrectly creates a CPU which it has
1972fcf5ef2aSThomas Huth          * promised via minimum_page_size that it will not.
1973fcf5ef2aSThomas Huth          */
1974fcf5ef2aSThomas Huth         error_setg(errp, "This CPU requires a smaller page size than the "
1975fcf5ef2aSThomas Huth                    "system is using");
1976fcf5ef2aSThomas Huth         return;
1977fcf5ef2aSThomas Huth     }
1978fcf5ef2aSThomas Huth 
1979fcf5ef2aSThomas Huth     /* This cpu-id-to-MPIDR affinity is used only for TCG; KVM will override it.
1980fcf5ef2aSThomas Huth      * We don't support setting cluster ID ([16..23]) (known as Aff2
1981fcf5ef2aSThomas Huth      * in later ARM ARM versions), or any of the higher affinity level fields,
1982fcf5ef2aSThomas Huth      * so these bits always RAZ.
1983fcf5ef2aSThomas Huth      */
1984fcf5ef2aSThomas Huth     if (cpu->mp_affinity == ARM64_AFFINITY_INVALID) {
198546de5913SIgor Mammedov         cpu->mp_affinity = arm_cpu_mp_affinity(cs->cpu_index,
198646de5913SIgor Mammedov                                                ARM_DEFAULT_CPUS_PER_CLUSTER);
1987fcf5ef2aSThomas Huth     }
1988fcf5ef2aSThomas Huth 
1989fcf5ef2aSThomas Huth     if (cpu->reset_hivecs) {
1990fcf5ef2aSThomas Huth             cpu->reset_sctlr |= (1 << 13);
1991fcf5ef2aSThomas Huth     }
1992fcf5ef2aSThomas Huth 
19933a062d57SJulian Brown     if (cpu->cfgend) {
19943a062d57SJulian Brown         if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
19953a062d57SJulian Brown             cpu->reset_sctlr |= SCTLR_EE;
19963a062d57SJulian Brown         } else {
19973a062d57SJulian Brown             cpu->reset_sctlr |= SCTLR_B;
19983a062d57SJulian Brown         }
19993a062d57SJulian Brown     }
20003a062d57SJulian Brown 
200140188188SPeter Maydell     if (!arm_feature(env, ARM_FEATURE_M) && !cpu->has_el3) {
2002fcf5ef2aSThomas Huth         /* If the has_el3 CPU property is disabled then we need to disable the
2003fcf5ef2aSThomas Huth          * feature.
2004fcf5ef2aSThomas Huth          */
2005fcf5ef2aSThomas Huth         unset_feature(env, ARM_FEATURE_EL3);
2006fcf5ef2aSThomas Huth 
2007b13c91c0SRichard Henderson         /*
2008b13c91c0SRichard Henderson          * Disable the security extension feature bits in the processor
2009b13c91c0SRichard Henderson          * feature registers as well.
2010fcf5ef2aSThomas Huth          */
2011b13c91c0SRichard Henderson         cpu->isar.id_pfr1 = FIELD_DP32(cpu->isar.id_pfr1, ID_PFR1, SECURITY, 0);
2012033a4f15SRichard Henderson         cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, COPSDBG, 0);
2013b13c91c0SRichard Henderson         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2014b13c91c0SRichard Henderson                                            ID_AA64PFR0, EL3, 0);
2015b9f335c2SRichard Henderson 
2016b9f335c2SRichard Henderson         /* Disable the realm management extension, which requires EL3. */
2017b9f335c2SRichard Henderson         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2018b9f335c2SRichard Henderson                                            ID_AA64PFR0, RME, 0);
2019fcf5ef2aSThomas Huth     }
2020fcf5ef2aSThomas Huth 
2021c25bd18aSPeter Maydell     if (!cpu->has_el2) {
2022c25bd18aSPeter Maydell         unset_feature(env, ARM_FEATURE_EL2);
2023c25bd18aSPeter Maydell     }
2024c25bd18aSPeter Maydell 
2025d6f02ce3SWei Huang     if (!cpu->has_pmu) {
2026fcf5ef2aSThomas Huth         unset_feature(env, ARM_FEATURE_PMU);
202757a4a11bSAaron Lindsay     }
202857a4a11bSAaron Lindsay     if (arm_feature(env, ARM_FEATURE_PMU)) {
2029bf8d0969SAaron Lindsay OS         pmu_init(cpu);
203057a4a11bSAaron Lindsay 
203157a4a11bSAaron Lindsay         if (!kvm_enabled()) {
2032033614c4SAaron Lindsay             arm_register_pre_el_change_hook(cpu, &pmu_pre_el_change, 0);
2033033614c4SAaron Lindsay             arm_register_el_change_hook(cpu, &pmu_post_el_change, 0);
2034fcf5ef2aSThomas Huth         }
20354e7beb0cSAaron Lindsay OS 
20364e7beb0cSAaron Lindsay OS #ifndef CONFIG_USER_ONLY
20374e7beb0cSAaron Lindsay OS         cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, arm_pmu_timer_cb,
20384e7beb0cSAaron Lindsay OS                 cpu);
20394e7beb0cSAaron Lindsay OS #endif
204057a4a11bSAaron Lindsay     } else {
20412a609df8SPeter Maydell         cpu->isar.id_aa64dfr0 =
20422a609df8SPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMUVER, 0);
2043a6179538SPeter Maydell         cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, PERFMON, 0);
204457a4a11bSAaron Lindsay         cpu->pmceid0 = 0;
204557a4a11bSAaron Lindsay         cpu->pmceid1 = 0;
204657a4a11bSAaron Lindsay     }
2047fcf5ef2aSThomas Huth 
2048fcf5ef2aSThomas Huth     if (!arm_feature(env, ARM_FEATURE_EL2)) {
2049b13c91c0SRichard Henderson         /*
2050b13c91c0SRichard Henderson          * Disable the hypervisor feature bits in the processor feature
2051b13c91c0SRichard Henderson          * registers if we don't have EL2.
2052fcf5ef2aSThomas Huth          */
2053b13c91c0SRichard Henderson         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2054b13c91c0SRichard Henderson                                            ID_AA64PFR0, EL2, 0);
2055b13c91c0SRichard Henderson         cpu->isar.id_pfr1 = FIELD_DP32(cpu->isar.id_pfr1,
2056b13c91c0SRichard Henderson                                        ID_PFR1, VIRTUALIZATION, 0);
2057fcf5ef2aSThomas Huth     }
2058fcf5ef2aSThomas Huth 
20597134cb07SRichard Henderson     if (cpu_isar_feature(aa64_mte, cpu)) {
20607134cb07SRichard Henderson         /*
20617134cb07SRichard Henderson          * The architectural range of GM blocksize is 2-6, however qemu
20627134cb07SRichard Henderson          * doesn't support blocksize of 2 (see HELPER(ldgm)).
20637134cb07SRichard Henderson          */
20647134cb07SRichard Henderson         if (tcg_enabled()) {
20657134cb07SRichard Henderson             assert(cpu->gm_blocksize >= 3 && cpu->gm_blocksize <= 6);
20667134cb07SRichard Henderson         }
20677134cb07SRichard Henderson 
20686f4e1405SRichard Henderson #ifndef CONFIG_USER_ONLY
20696f4e1405SRichard Henderson         /*
2070cd305b5fSRichard Henderson          * If we do not have tag-memory provided by the machine,
2071cd305b5fSRichard Henderson          * reduce MTE support to instructions enabled at EL0.
2072cd305b5fSRichard Henderson          * This matches Cortex-A710 BROADCASTMTE input being LOW.
20736f4e1405SRichard Henderson          */
20747134cb07SRichard Henderson         if (cpu->tag_memory == NULL) {
20756f4e1405SRichard Henderson             cpu->isar.id_aa64pfr1 =
2076cd305b5fSRichard Henderson                 FIELD_DP64(cpu->isar.id_aa64pfr1, ID_AA64PFR1, MTE, 1);
20776f4e1405SRichard Henderson         }
20786f4e1405SRichard Henderson #endif
20797134cb07SRichard Henderson     }
20806f4e1405SRichard Henderson 
20812daf518dSPeter Maydell     if (tcg_enabled()) {
20822daf518dSPeter Maydell         /*
20837d8c283eSPeter Maydell          * Don't report some architectural features in the ID registers
20847d8c283eSPeter Maydell          * where TCG does not yet implement it (not even a minimal
20857d8c283eSPeter Maydell          * stub version). This avoids guests falling over when they
20867d8c283eSPeter Maydell          * try to access the non-existent system registers for them.
20872daf518dSPeter Maydell          */
20887d8c283eSPeter Maydell         /* FEAT_SPE (Statistical Profiling Extension) */
20892daf518dSPeter Maydell         cpu->isar.id_aa64dfr0 =
20902daf518dSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMSVER, 0);
2091*3d5f45ecSRichard Henderson         /* FEAT_TRBE (Trace Buffer Extension) */
2092*3d5f45ecSRichard Henderson         cpu->isar.id_aa64dfr0 =
2093*3d5f45ecSRichard Henderson             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEBUFFER, 0);
20947d8c283eSPeter Maydell         /* FEAT_TRF (Self-hosted Trace Extension) */
20957d8c283eSPeter Maydell         cpu->isar.id_aa64dfr0 =
20967d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEFILT, 0);
20977d8c283eSPeter Maydell         cpu->isar.id_dfr0 =
20987d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, TRACEFILT, 0);
20997d8c283eSPeter Maydell         /* Trace Macrocell system register access */
21007d8c283eSPeter Maydell         cpu->isar.id_aa64dfr0 =
21017d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEVER, 0);
21027d8c283eSPeter Maydell         cpu->isar.id_dfr0 =
21037d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, COPTRC, 0);
21047d8c283eSPeter Maydell         /* Memory mapped trace */
21057d8c283eSPeter Maydell         cpu->isar.id_dfr0 =
21067d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, MMAPTRC, 0);
21077d8c283eSPeter Maydell         /* FEAT_AMU (Activity Monitors Extension) */
21087d8c283eSPeter Maydell         cpu->isar.id_aa64pfr0 =
21097d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64pfr0, ID_AA64PFR0, AMU, 0);
21107d8c283eSPeter Maydell         cpu->isar.id_pfr0 =
21117d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_pfr0, ID_PFR0, AMU, 0);
21127d8c283eSPeter Maydell         /* FEAT_MPAM (Memory Partitioning and Monitoring Extension) */
21137d8c283eSPeter Maydell         cpu->isar.id_aa64pfr0 =
21147d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64pfr0, ID_AA64PFR0, MPAM, 0);
21157d8c283eSPeter Maydell         /* FEAT_NV (Nested Virtualization) */
21167d8c283eSPeter Maydell         cpu->isar.id_aa64mmfr2 =
21177d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64mmfr2, ID_AA64MMFR2, NV, 0);
21182daf518dSPeter Maydell     }
21192daf518dSPeter Maydell 
2120f50cd314SPeter Maydell     /* MPU can be configured out of a PMSA CPU either by setting has-mpu
2121f50cd314SPeter Maydell      * to false or by setting pmsav7-dregion to 0.
2122f50cd314SPeter Maydell      */
2123761c4642STobias Röhmel     if (!cpu->has_mpu || cpu->pmsav7_dregion == 0) {
2124f50cd314SPeter Maydell         cpu->has_mpu = false;
2125761c4642STobias Röhmel         cpu->pmsav7_dregion = 0;
2126761c4642STobias Röhmel         cpu->pmsav8r_hdregion = 0;
2127fcf5ef2aSThomas Huth     }
2128fcf5ef2aSThomas Huth 
2129452a0955SPeter Maydell     if (arm_feature(env, ARM_FEATURE_PMSA) &&
2130fcf5ef2aSThomas Huth         arm_feature(env, ARM_FEATURE_V7)) {
2131fcf5ef2aSThomas Huth         uint32_t nr = cpu->pmsav7_dregion;
2132fcf5ef2aSThomas Huth 
2133fcf5ef2aSThomas Huth         if (nr > 0xff) {
2134fcf5ef2aSThomas Huth             error_setg(errp, "PMSAv7 MPU #regions invalid %" PRIu32, nr);
2135fcf5ef2aSThomas Huth             return;
2136fcf5ef2aSThomas Huth         }
2137fcf5ef2aSThomas Huth 
2138fcf5ef2aSThomas Huth         if (nr) {
21390e1a46bbSPeter Maydell             if (arm_feature(env, ARM_FEATURE_V8)) {
21400e1a46bbSPeter Maydell                 /* PMSAv8 */
214162c58ee0SPeter Maydell                 env->pmsav8.rbar[M_REG_NS] = g_new0(uint32_t, nr);
214262c58ee0SPeter Maydell                 env->pmsav8.rlar[M_REG_NS] = g_new0(uint32_t, nr);
214362c58ee0SPeter Maydell                 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
214462c58ee0SPeter Maydell                     env->pmsav8.rbar[M_REG_S] = g_new0(uint32_t, nr);
214562c58ee0SPeter Maydell                     env->pmsav8.rlar[M_REG_S] = g_new0(uint32_t, nr);
214662c58ee0SPeter Maydell                 }
21470e1a46bbSPeter Maydell             } else {
2148fcf5ef2aSThomas Huth                 env->pmsav7.drbar = g_new0(uint32_t, nr);
2149fcf5ef2aSThomas Huth                 env->pmsav7.drsr = g_new0(uint32_t, nr);
2150fcf5ef2aSThomas Huth                 env->pmsav7.dracr = g_new0(uint32_t, nr);
2151fcf5ef2aSThomas Huth             }
2152fcf5ef2aSThomas Huth         }
2153761c4642STobias Röhmel 
2154761c4642STobias Röhmel         if (cpu->pmsav8r_hdregion > 0xff) {
2155761c4642STobias Röhmel             error_setg(errp, "PMSAv8 MPU EL2 #regions invalid %" PRIu32,
2156761c4642STobias Röhmel                               cpu->pmsav8r_hdregion);
2157761c4642STobias Röhmel             return;
2158761c4642STobias Röhmel         }
2159761c4642STobias Röhmel 
2160761c4642STobias Röhmel         if (cpu->pmsav8r_hdregion) {
2161761c4642STobias Röhmel             env->pmsav8.hprbar = g_new0(uint32_t,
2162761c4642STobias Röhmel                                         cpu->pmsav8r_hdregion);
2163761c4642STobias Röhmel             env->pmsav8.hprlar = g_new0(uint32_t,
2164761c4642STobias Röhmel                                         cpu->pmsav8r_hdregion);
2165761c4642STobias Röhmel         }
21660e1a46bbSPeter Maydell     }
2167fcf5ef2aSThomas Huth 
21689901c576SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
21699901c576SPeter Maydell         uint32_t nr = cpu->sau_sregion;
21709901c576SPeter Maydell 
21719901c576SPeter Maydell         if (nr > 0xff) {
21729901c576SPeter Maydell             error_setg(errp, "v8M SAU #regions invalid %" PRIu32, nr);
21739901c576SPeter Maydell             return;
21749901c576SPeter Maydell         }
21759901c576SPeter Maydell 
21769901c576SPeter Maydell         if (nr) {
21779901c576SPeter Maydell             env->sau.rbar = g_new0(uint32_t, nr);
21789901c576SPeter Maydell             env->sau.rlar = g_new0(uint32_t, nr);
21799901c576SPeter Maydell         }
21809901c576SPeter Maydell     }
21819901c576SPeter Maydell 
218291db4642SCédric Le Goater     if (arm_feature(env, ARM_FEATURE_EL3)) {
218391db4642SCédric Le Goater         set_feature(env, ARM_FEATURE_VBAR);
218491db4642SCédric Le Goater     }
218591db4642SCédric Le Goater 
2186f6fc36deSJean-Philippe Brucker #ifndef CONFIG_USER_ONLY
2187f6fc36deSJean-Philippe Brucker     if (tcg_enabled() && cpu_isar_feature(aa64_rme, cpu)) {
2188f6fc36deSJean-Philippe Brucker         arm_register_el_change_hook(cpu, &gt_rme_post_el_change, 0);
2189f6fc36deSJean-Philippe Brucker     }
2190f6fc36deSJean-Philippe Brucker #endif
2191f6fc36deSJean-Philippe Brucker 
2192fcf5ef2aSThomas Huth     register_cp_regs_for_features(cpu);
2193fcf5ef2aSThomas Huth     arm_cpu_register_gdb_regs_for_features(cpu);
2194fcf5ef2aSThomas Huth 
2195fcf5ef2aSThomas Huth     init_cpreg_list(cpu);
2196fcf5ef2aSThomas Huth 
2197fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
2198cc7d44c2SLike Xu     MachineState *ms = MACHINE(qdev_get_machine());
2199cc7d44c2SLike Xu     unsigned int smp_cpus = ms->smp.cpus;
22008bce44a2SRichard Henderson     bool has_secure = cpu->has_el3 || arm_feature(env, ARM_FEATURE_M_SECURITY);
2201cc7d44c2SLike Xu 
22028bce44a2SRichard Henderson     /*
22038bce44a2SRichard Henderson      * We must set cs->num_ases to the final value before
22048bce44a2SRichard Henderson      * the first call to cpu_address_space_init.
22058bce44a2SRichard Henderson      */
22068bce44a2SRichard Henderson     if (cpu->tag_memory != NULL) {
22078bce44a2SRichard Henderson         cs->num_ases = 3 + has_secure;
22088bce44a2SRichard Henderson     } else {
22098bce44a2SRichard Henderson         cs->num_ases = 1 + has_secure;
22108bce44a2SRichard Henderson     }
22111d2091bcSPeter Maydell 
22128bce44a2SRichard Henderson     if (has_secure) {
2213fcf5ef2aSThomas Huth         if (!cpu->secure_memory) {
2214fcf5ef2aSThomas Huth             cpu->secure_memory = cs->memory;
2215fcf5ef2aSThomas Huth         }
221680ceb07aSPeter Xu         cpu_address_space_init(cs, ARMASIdx_S, "cpu-secure-memory",
221780ceb07aSPeter Xu                                cpu->secure_memory);
2218fcf5ef2aSThomas Huth     }
22198bce44a2SRichard Henderson 
22208bce44a2SRichard Henderson     if (cpu->tag_memory != NULL) {
22218bce44a2SRichard Henderson         cpu_address_space_init(cs, ARMASIdx_TagNS, "cpu-tag-memory",
22228bce44a2SRichard Henderson                                cpu->tag_memory);
22238bce44a2SRichard Henderson         if (has_secure) {
22248bce44a2SRichard Henderson             cpu_address_space_init(cs, ARMASIdx_TagS, "cpu-tag-memory",
22258bce44a2SRichard Henderson                                    cpu->secure_tag_memory);
22268bce44a2SRichard Henderson         }
22278bce44a2SRichard Henderson     }
22288bce44a2SRichard Henderson 
222980ceb07aSPeter Xu     cpu_address_space_init(cs, ARMASIdx_NS, "cpu-memory", cs->memory);
2230f9a69711SAlistair Francis 
2231f9a69711SAlistair Francis     /* No core_count specified, default to smp_cpus. */
2232f9a69711SAlistair Francis     if (cpu->core_count == -1) {
2233f9a69711SAlistair Francis         cpu->core_count = smp_cpus;
2234f9a69711SAlistair Francis     }
2235fcf5ef2aSThomas Huth #endif
2236fcf5ef2aSThomas Huth 
2237a4157b80SRichard Henderson     if (tcg_enabled()) {
2238a4157b80SRichard Henderson         int dcz_blocklen = 4 << cpu->dcz_blocksize;
2239a4157b80SRichard Henderson 
2240a4157b80SRichard Henderson         /*
2241a4157b80SRichard Henderson          * We only support DCZ blocklen that fits on one page.
2242a4157b80SRichard Henderson          *
2243a4157b80SRichard Henderson          * Architectually this is always true.  However TARGET_PAGE_SIZE
2244a4157b80SRichard Henderson          * is variable and, for compatibility with -machine virt-2.7,
2245a4157b80SRichard Henderson          * is only 1KiB, as an artifact of legacy ARMv5 subpage support.
2246a4157b80SRichard Henderson          * But even then, while the largest architectural DCZ blocklen
2247a4157b80SRichard Henderson          * is 2KiB, no cpu actually uses such a large blocklen.
2248a4157b80SRichard Henderson          */
2249a4157b80SRichard Henderson         assert(dcz_blocklen <= TARGET_PAGE_SIZE);
2250a4157b80SRichard Henderson 
2251a4157b80SRichard Henderson         /*
2252a4157b80SRichard Henderson          * We only support DCZ blocksize >= 2*TAG_GRANULE, which is to say
2253a4157b80SRichard Henderson          * both nibbles of each byte storing tag data may be written at once.
2254a4157b80SRichard Henderson          * Since TAG_GRANULE is 16, this means that blocklen must be >= 32.
2255a4157b80SRichard Henderson          */
2256a4157b80SRichard Henderson         if (cpu_isar_feature(aa64_mte, cpu)) {
2257a4157b80SRichard Henderson             assert(dcz_blocklen >= 2 * TAG_GRANULE);
2258a4157b80SRichard Henderson         }
2259a4157b80SRichard Henderson     }
2260a4157b80SRichard Henderson 
2261fcf5ef2aSThomas Huth     qemu_init_vcpu(cs);
2262fcf5ef2aSThomas Huth     cpu_reset(cs);
2263fcf5ef2aSThomas Huth 
2264fcf5ef2aSThomas Huth     acc->parent_realize(dev, errp);
2265fcf5ef2aSThomas Huth }
2266fcf5ef2aSThomas Huth 
2267fcf5ef2aSThomas Huth static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
2268fcf5ef2aSThomas Huth {
2269fcf5ef2aSThomas Huth     ObjectClass *oc;
2270fcf5ef2aSThomas Huth     char *typename;
2271fcf5ef2aSThomas Huth     char **cpuname;
2272a0032cc5SPeter Maydell     const char *cpunamestr;
2273fcf5ef2aSThomas Huth 
2274fcf5ef2aSThomas Huth     cpuname = g_strsplit(cpu_model, ",", 1);
2275a0032cc5SPeter Maydell     cpunamestr = cpuname[0];
2276a0032cc5SPeter Maydell #ifdef CONFIG_USER_ONLY
2277a0032cc5SPeter Maydell     /* For backwards compatibility usermode emulation allows "-cpu any",
2278a0032cc5SPeter Maydell      * which has the same semantics as "-cpu max".
2279a0032cc5SPeter Maydell      */
2280a0032cc5SPeter Maydell     if (!strcmp(cpunamestr, "any")) {
2281a0032cc5SPeter Maydell         cpunamestr = "max";
2282a0032cc5SPeter Maydell     }
2283a0032cc5SPeter Maydell #endif
2284a0032cc5SPeter Maydell     typename = g_strdup_printf(ARM_CPU_TYPE_NAME("%s"), cpunamestr);
2285fcf5ef2aSThomas Huth     oc = object_class_by_name(typename);
2286fcf5ef2aSThomas Huth     g_strfreev(cpuname);
2287fcf5ef2aSThomas Huth     g_free(typename);
2288fcf5ef2aSThomas Huth     if (!oc || !object_class_dynamic_cast(oc, TYPE_ARM_CPU) ||
2289fcf5ef2aSThomas Huth         object_class_is_abstract(oc)) {
2290fcf5ef2aSThomas Huth         return NULL;
2291fcf5ef2aSThomas Huth     }
2292fcf5ef2aSThomas Huth     return oc;
2293fcf5ef2aSThomas Huth }
2294fcf5ef2aSThomas Huth 
2295fcf5ef2aSThomas Huth static Property arm_cpu_properties[] = {
2296e544f800SPhilippe Mathieu-Daudé     DEFINE_PROP_UINT64("midr", ARMCPU, midr, 0),
2297fcf5ef2aSThomas Huth     DEFINE_PROP_UINT64("mp-affinity", ARMCPU,
2298fcf5ef2aSThomas Huth                         mp_affinity, ARM64_AFFINITY_INVALID),
229915f8b142SIgor Mammedov     DEFINE_PROP_INT32("node-id", ARMCPU, node_id, CPU_UNSET_NUMA_NODE_ID),
2300f9a69711SAlistair Francis     DEFINE_PROP_INT32("core-count", ARMCPU, core_count, -1),
2301fcf5ef2aSThomas Huth     DEFINE_PROP_END_OF_LIST()
2302fcf5ef2aSThomas Huth };
2303fcf5ef2aSThomas Huth 
2304fcf5ef2aSThomas Huth static gchar *arm_gdb_arch_name(CPUState *cs)
2305fcf5ef2aSThomas Huth {
2306fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(cs);
2307fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
2308fcf5ef2aSThomas Huth 
2309fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
2310fcf5ef2aSThomas Huth         return g_strdup("iwmmxt");
2311fcf5ef2aSThomas Huth     }
2312fcf5ef2aSThomas Huth     return g_strdup("arm");
2313fcf5ef2aSThomas Huth }
2314fcf5ef2aSThomas Huth 
23158b80bd28SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY
23168b80bd28SPhilippe Mathieu-Daudé #include "hw/core/sysemu-cpu-ops.h"
23178b80bd28SPhilippe Mathieu-Daudé 
23188b80bd28SPhilippe Mathieu-Daudé static const struct SysemuCPUOps arm_sysemu_ops = {
231908928c6dSPhilippe Mathieu-Daudé     .get_phys_page_attrs_debug = arm_cpu_get_phys_page_attrs_debug,
2320faf39e82SPhilippe Mathieu-Daudé     .asidx_from_attrs = arm_asidx_from_attrs,
2321715e3c1aSPhilippe Mathieu-Daudé     .write_elf32_note = arm_cpu_write_elf32_note,
2322715e3c1aSPhilippe Mathieu-Daudé     .write_elf64_note = arm_cpu_write_elf64_note,
2323da383e02SPhilippe Mathieu-Daudé     .virtio_is_big_endian = arm_cpu_virtio_is_big_endian,
2324feece4d0SPhilippe Mathieu-Daudé     .legacy_vmsd = &vmstate_arm_cpu,
23258b80bd28SPhilippe Mathieu-Daudé };
23268b80bd28SPhilippe Mathieu-Daudé #endif
23278b80bd28SPhilippe Mathieu-Daudé 
232878271684SClaudio Fontana #ifdef CONFIG_TCG
232911906557SRichard Henderson static const struct TCGCPUOps arm_tcg_ops = {
233078271684SClaudio Fontana     .initialize = arm_translate_init,
233178271684SClaudio Fontana     .synchronize_from_tb = arm_cpu_synchronize_from_tb,
233278271684SClaudio Fontana     .debug_excp_handler = arm_debug_excp_handler,
233356c6c98dSRichard Henderson     .restore_state_to_opc = arm_restore_state_to_opc,
233478271684SClaudio Fontana 
23359b12b6b4SRichard Henderson #ifdef CONFIG_USER_ONLY
23369b12b6b4SRichard Henderson     .record_sigsegv = arm_cpu_record_sigsegv,
233739a099caSRichard Henderson     .record_sigbus = arm_cpu_record_sigbus,
23389b12b6b4SRichard Henderson #else
23399b12b6b4SRichard Henderson     .tlb_fill = arm_cpu_tlb_fill,
2340083afd18SPhilippe Mathieu-Daudé     .cpu_exec_interrupt = arm_cpu_exec_interrupt,
234178271684SClaudio Fontana     .do_interrupt = arm_cpu_do_interrupt,
234278271684SClaudio Fontana     .do_transaction_failed = arm_cpu_do_transaction_failed,
234378271684SClaudio Fontana     .do_unaligned_access = arm_cpu_do_unaligned_access,
234478271684SClaudio Fontana     .adjust_watchpoint_address = arm_adjust_watchpoint_address,
234578271684SClaudio Fontana     .debug_check_watchpoint = arm_debug_check_watchpoint,
2346b00d86bcSRichard Henderson     .debug_check_breakpoint = arm_debug_check_breakpoint,
234778271684SClaudio Fontana #endif /* !CONFIG_USER_ONLY */
234878271684SClaudio Fontana };
234978271684SClaudio Fontana #endif /* CONFIG_TCG */
235078271684SClaudio Fontana 
2351fcf5ef2aSThomas Huth static void arm_cpu_class_init(ObjectClass *oc, void *data)
2352fcf5ef2aSThomas Huth {
2353fcf5ef2aSThomas Huth     ARMCPUClass *acc = ARM_CPU_CLASS(oc);
2354fcf5ef2aSThomas Huth     CPUClass *cc = CPU_CLASS(acc);
2355fcf5ef2aSThomas Huth     DeviceClass *dc = DEVICE_CLASS(oc);
23569130cadeSPeter Maydell     ResettableClass *rc = RESETTABLE_CLASS(oc);
2357fcf5ef2aSThomas Huth 
2358bf853881SPhilippe Mathieu-Daudé     device_class_set_parent_realize(dc, arm_cpu_realizefn,
2359bf853881SPhilippe Mathieu-Daudé                                     &acc->parent_realize);
2360fcf5ef2aSThomas Huth 
23614f67d30bSMarc-André Lureau     device_class_set_props(dc, arm_cpu_properties);
23629130cadeSPeter Maydell 
23639130cadeSPeter Maydell     resettable_class_set_parent_phases(rc, NULL, arm_cpu_reset_hold, NULL,
23649130cadeSPeter Maydell                                        &acc->parent_phases);
2365fcf5ef2aSThomas Huth 
2366fcf5ef2aSThomas Huth     cc->class_by_name = arm_cpu_class_by_name;
2367fcf5ef2aSThomas Huth     cc->has_work = arm_cpu_has_work;
2368fcf5ef2aSThomas Huth     cc->dump_state = arm_cpu_dump_state;
2369fcf5ef2aSThomas Huth     cc->set_pc = arm_cpu_set_pc;
2370e4fdf9dfSRichard Henderson     cc->get_pc = arm_cpu_get_pc;
2371fcf5ef2aSThomas Huth     cc->gdb_read_register = arm_cpu_gdb_read_register;
2372fcf5ef2aSThomas Huth     cc->gdb_write_register = arm_cpu_gdb_write_register;
23737350d553SRichard Henderson #ifndef CONFIG_USER_ONLY
23748b80bd28SPhilippe Mathieu-Daudé     cc->sysemu_ops = &arm_sysemu_ops;
2375fcf5ef2aSThomas Huth #endif
2376fcf5ef2aSThomas Huth     cc->gdb_num_core_regs = 26;
2377fcf5ef2aSThomas Huth     cc->gdb_core_xml_file = "arm-core.xml";
2378fcf5ef2aSThomas Huth     cc->gdb_arch_name = arm_gdb_arch_name;
2379200bf5b7SAbdallah Bouassida     cc->gdb_get_dynamic_xml = arm_gdb_get_dynamic_xml;
2380fcf5ef2aSThomas Huth     cc->gdb_stop_before_watchpoint = true;
2381fcf5ef2aSThomas Huth     cc->disas_set_info = arm_disas_set_info;
238278271684SClaudio Fontana 
238374d7fc7fSRichard Henderson #ifdef CONFIG_TCG
238478271684SClaudio Fontana     cc->tcg_ops = &arm_tcg_ops;
2385cbc183d2SClaudio Fontana #endif /* CONFIG_TCG */
2386fcf5ef2aSThomas Huth }
2387fcf5ef2aSThomas Huth 
238851e5ef45SMarc-André Lureau static void arm_cpu_instance_init(Object *obj)
238951e5ef45SMarc-André Lureau {
239051e5ef45SMarc-André Lureau     ARMCPUClass *acc = ARM_CPU_GET_CLASS(obj);
239151e5ef45SMarc-André Lureau 
239251e5ef45SMarc-André Lureau     acc->info->initfn(obj);
239351e5ef45SMarc-André Lureau     arm_cpu_post_init(obj);
239451e5ef45SMarc-André Lureau }
239551e5ef45SMarc-André Lureau 
239651e5ef45SMarc-André Lureau static void cpu_register_class_init(ObjectClass *oc, void *data)
239751e5ef45SMarc-André Lureau {
239851e5ef45SMarc-André Lureau     ARMCPUClass *acc = ARM_CPU_CLASS(oc);
239951e5ef45SMarc-André Lureau 
240051e5ef45SMarc-André Lureau     acc->info = data;
240151e5ef45SMarc-André Lureau }
240251e5ef45SMarc-André Lureau 
240337bcf244SThomas Huth void arm_cpu_register(const ARMCPUInfo *info)
2404fcf5ef2aSThomas Huth {
2405fcf5ef2aSThomas Huth     TypeInfo type_info = {
2406fcf5ef2aSThomas Huth         .parent = TYPE_ARM_CPU,
2407fcf5ef2aSThomas Huth         .instance_size = sizeof(ARMCPU),
2408d03087bdSRichard Henderson         .instance_align = __alignof__(ARMCPU),
240951e5ef45SMarc-André Lureau         .instance_init = arm_cpu_instance_init,
2410fcf5ef2aSThomas Huth         .class_size = sizeof(ARMCPUClass),
241151e5ef45SMarc-André Lureau         .class_init = info->class_init ?: cpu_register_class_init,
241251e5ef45SMarc-André Lureau         .class_data = (void *)info,
2413fcf5ef2aSThomas Huth     };
2414fcf5ef2aSThomas Huth 
2415fcf5ef2aSThomas Huth     type_info.name = g_strdup_printf("%s-" TYPE_ARM_CPU, info->name);
2416fcf5ef2aSThomas Huth     type_register(&type_info);
2417fcf5ef2aSThomas Huth     g_free((void *)type_info.name);
2418fcf5ef2aSThomas Huth }
2419fcf5ef2aSThomas Huth 
2420fcf5ef2aSThomas Huth static const TypeInfo arm_cpu_type_info = {
2421fcf5ef2aSThomas Huth     .name = TYPE_ARM_CPU,
2422fcf5ef2aSThomas Huth     .parent = TYPE_CPU,
2423fcf5ef2aSThomas Huth     .instance_size = sizeof(ARMCPU),
2424d03087bdSRichard Henderson     .instance_align = __alignof__(ARMCPU),
2425fcf5ef2aSThomas Huth     .instance_init = arm_cpu_initfn,
2426fcf5ef2aSThomas Huth     .instance_finalize = arm_cpu_finalizefn,
2427fcf5ef2aSThomas Huth     .abstract = true,
2428fcf5ef2aSThomas Huth     .class_size = sizeof(ARMCPUClass),
2429fcf5ef2aSThomas Huth     .class_init = arm_cpu_class_init,
2430fcf5ef2aSThomas Huth };
2431fcf5ef2aSThomas Huth 
2432fcf5ef2aSThomas Huth static void arm_cpu_register_types(void)
2433fcf5ef2aSThomas Huth {
2434fcf5ef2aSThomas Huth     type_register_static(&arm_cpu_type_info);
2435fcf5ef2aSThomas Huth }
2436fcf5ef2aSThomas Huth 
2437fcf5ef2aSThomas Huth type_init(arm_cpu_register_types)
2438