xref: /openbmc/qemu/target/arm/cpu.c (revision bd8e9ddf6f6915b8a67909a21e7f46f77a05767d)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  * QEMU ARM CPU
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  * Copyright (c) 2012 SUSE LINUX Products GmbH
5fcf5ef2aSThomas Huth  *
6fcf5ef2aSThomas Huth  * This program is free software; you can redistribute it and/or
7fcf5ef2aSThomas Huth  * modify it under the terms of the GNU General Public License
8fcf5ef2aSThomas Huth  * as published by the Free Software Foundation; either version 2
9fcf5ef2aSThomas Huth  * of the License, or (at your option) any later version.
10fcf5ef2aSThomas Huth  *
11fcf5ef2aSThomas Huth  * This program is distributed in the hope that it will be useful,
12fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14fcf5ef2aSThomas Huth  * GNU General Public License for more details.
15fcf5ef2aSThomas Huth  *
16fcf5ef2aSThomas Huth  * You should have received a copy of the GNU General Public License
17fcf5ef2aSThomas Huth  * along with this program; if not, see
18fcf5ef2aSThomas Huth  * <http://www.gnu.org/licenses/gpl-2.0.html>
19fcf5ef2aSThomas Huth  */
20fcf5ef2aSThomas Huth 
21fcf5ef2aSThomas Huth #include "qemu/osdep.h"
2286480615SPhilippe Mathieu-Daudé #include "qemu/qemu-print.h"
23b8012ecfSPhilippe Mathieu-Daudé #include "qemu/timer.h"
248cc2246cSPeter Maydell #include "qemu/log.h"
25ec5f7ca8SMarc-André Lureau #include "exec/page-vary.h"
26181962fdSPeter Maydell #include "target/arm/idau.h"
270b8fa32fSMarkus Armbruster #include "qemu/module.h"
28fcf5ef2aSThomas Huth #include "qapi/error.h"
29fcf5ef2aSThomas Huth #include "cpu.h"
3078271684SClaudio Fontana #ifdef CONFIG_TCG
3178271684SClaudio Fontana #include "hw/core/tcg-cpu-ops.h"
3278271684SClaudio Fontana #endif /* CONFIG_TCG */
33fcf5ef2aSThomas Huth #include "internals.h"
345a534314SPeter Maydell #include "cpu-features.h"
35fcf5ef2aSThomas Huth #include "exec/exec-all.h"
36fcf5ef2aSThomas Huth #include "hw/qdev-properties.h"
37fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
38fcf5ef2aSThomas Huth #include "hw/loader.h"
39cc7d44c2SLike Xu #include "hw/boards.h"
40165876f2SPhilippe Mathieu-Daudé #ifdef CONFIG_TCG
418f4e07c9SPhilippe Mathieu-Daudé #include "hw/intc/armv7m_nvic.h"
42165876f2SPhilippe Mathieu-Daudé #endif /* CONFIG_TCG */
43165876f2SPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */
4414a48c1dSMarkus Armbruster #include "sysemu/tcg.h"
45045e5064SAlexander Graf #include "sysemu/qtest.h"
46b3946626SVincent Palatin #include "sysemu/hw_accel.h"
47fcf5ef2aSThomas Huth #include "kvm_arm.h"
48110f6c70SRichard Henderson #include "disas/capstone.h"
4924f91e81SAlex Bennée #include "fpu/softfloat.h"
50cf7c6d10SRichard Henderson #include "cpregs.h"
51d780d056SPhilippe Mathieu-Daudé #include "target/arm/cpu-qom.h"
52f4f318b4SPhilippe Mathieu-Daudé #include "target/arm/gtimer.h"
53fcf5ef2aSThomas Huth 
54fcf5ef2aSThomas Huth static void arm_cpu_set_pc(CPUState *cs, vaddr value)
55fcf5ef2aSThomas Huth {
56fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(cs);
5742f6ed91SJulia Suvorova     CPUARMState *env = &cpu->env;
58fcf5ef2aSThomas Huth 
5942f6ed91SJulia Suvorova     if (is_a64(env)) {
6042f6ed91SJulia Suvorova         env->pc = value;
61063bbd80SRichard Henderson         env->thumb = false;
6242f6ed91SJulia Suvorova     } else {
6342f6ed91SJulia Suvorova         env->regs[15] = value & ~1;
6442f6ed91SJulia Suvorova         env->thumb = value & 1;
6542f6ed91SJulia Suvorova     }
6642f6ed91SJulia Suvorova }
6742f6ed91SJulia Suvorova 
68e4fdf9dfSRichard Henderson static vaddr arm_cpu_get_pc(CPUState *cs)
69e4fdf9dfSRichard Henderson {
70e4fdf9dfSRichard Henderson     ARMCPU *cpu = ARM_CPU(cs);
71e4fdf9dfSRichard Henderson     CPUARMState *env = &cpu->env;
72e4fdf9dfSRichard Henderson 
73e4fdf9dfSRichard Henderson     if (is_a64(env)) {
74e4fdf9dfSRichard Henderson         return env->pc;
75e4fdf9dfSRichard Henderson     } else {
76e4fdf9dfSRichard Henderson         return env->regs[15];
77e4fdf9dfSRichard Henderson     }
78e4fdf9dfSRichard Henderson }
79e4fdf9dfSRichard Henderson 
80ec62595bSEduardo Habkost #ifdef CONFIG_TCG
8178271684SClaudio Fontana void arm_cpu_synchronize_from_tb(CPUState *cs,
8204a37d4cSRichard Henderson                                  const TranslationBlock *tb)
8342f6ed91SJulia Suvorova {
8403a648c4SAnton Johansson     /* The program counter is always up to date with CF_PCREL. */
8503a648c4SAnton Johansson     if (!(tb_cflags(tb) & CF_PCREL)) {
86b77af26eSRichard Henderson         CPUARMState *env = cpu_env(cs);
8742f6ed91SJulia Suvorova         /*
8842f6ed91SJulia Suvorova          * It's OK to look at env for the current mode here, because it's
8942f6ed91SJulia Suvorova          * never possible for an AArch64 TB to chain to an AArch32 TB.
9042f6ed91SJulia Suvorova          */
9142f6ed91SJulia Suvorova         if (is_a64(env)) {
92f51a1dd7SAnton Johansson             env->pc = tb->pc;
9342f6ed91SJulia Suvorova         } else {
94f51a1dd7SAnton Johansson             env->regs[15] = tb->pc;
9542f6ed91SJulia Suvorova         }
96fcf5ef2aSThomas Huth     }
97abb80995SRichard Henderson }
9856c6c98dSRichard Henderson 
99475e56b6SEvgeny Ermakov void arm_restore_state_to_opc(CPUState *cs,
10056c6c98dSRichard Henderson                               const TranslationBlock *tb,
10156c6c98dSRichard Henderson                               const uint64_t *data)
10256c6c98dSRichard Henderson {
103b77af26eSRichard Henderson     CPUARMState *env = cpu_env(cs);
10456c6c98dSRichard Henderson 
10556c6c98dSRichard Henderson     if (is_a64(env)) {
10603a648c4SAnton Johansson         if (tb_cflags(tb) & CF_PCREL) {
10756c6c98dSRichard Henderson             env->pc = (env->pc & TARGET_PAGE_MASK) | data[0];
10856c6c98dSRichard Henderson         } else {
10956c6c98dSRichard Henderson             env->pc = data[0];
11056c6c98dSRichard Henderson         }
11156c6c98dSRichard Henderson         env->condexec_bits = 0;
11256c6c98dSRichard Henderson         env->exception.syndrome = data[2] << ARM_INSN_START_WORD2_SHIFT;
11356c6c98dSRichard Henderson     } else {
11403a648c4SAnton Johansson         if (tb_cflags(tb) & CF_PCREL) {
11556c6c98dSRichard Henderson             env->regs[15] = (env->regs[15] & TARGET_PAGE_MASK) | data[0];
11656c6c98dSRichard Henderson         } else {
11756c6c98dSRichard Henderson             env->regs[15] = data[0];
11856c6c98dSRichard Henderson         }
11956c6c98dSRichard Henderson         env->condexec_bits = data[1];
12056c6c98dSRichard Henderson         env->exception.syndrome = data[2] << ARM_INSN_START_WORD2_SHIFT;
12156c6c98dSRichard Henderson     }
12256c6c98dSRichard Henderson }
123ec62595bSEduardo Habkost #endif /* CONFIG_TCG */
124fcf5ef2aSThomas Huth 
125b36a32eaSJinjie Ruan /*
126b36a32eaSJinjie Ruan  * With SCTLR_ELx.NMI == 0, IRQ with Superpriority is masked identically with
127b36a32eaSJinjie Ruan  * IRQ without Superpriority. Moreover, if the GIC is configured so that
128b36a32eaSJinjie Ruan  * FEAT_GICv3_NMI is only set if FEAT_NMI is set, then we won't ever see
129b36a32eaSJinjie Ruan  * CPU_INTERRUPT_*NMI anyway. So we might as well accept NMI here
130b36a32eaSJinjie Ruan  * unconditionally.
131b36a32eaSJinjie Ruan  */
132fcf5ef2aSThomas Huth static bool arm_cpu_has_work(CPUState *cs)
133fcf5ef2aSThomas Huth {
134fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(cs);
135fcf5ef2aSThomas Huth 
136062ba099SAlex Bennée     return (cpu->power_state != PSCI_OFF)
137fcf5ef2aSThomas Huth         && cs->interrupt_request &
138fcf5ef2aSThomas Huth         (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD
139b36a32eaSJinjie Ruan          | CPU_INTERRUPT_NMI | CPU_INTERRUPT_VINMI | CPU_INTERRUPT_VFNMI
1403c29632fSRichard Henderson          | CPU_INTERRUPT_VFIQ | CPU_INTERRUPT_VIRQ | CPU_INTERRUPT_VSERR
141fcf5ef2aSThomas Huth          | CPU_INTERRUPT_EXITTB);
142fcf5ef2aSThomas Huth }
143fcf5ef2aSThomas Huth 
144628421c7SRichard Henderson static int arm_cpu_mmu_index(CPUState *cs, bool ifetch)
145628421c7SRichard Henderson {
146628421c7SRichard Henderson     return arm_env_mmu_index(cpu_env(cs));
147628421c7SRichard Henderson }
148628421c7SRichard Henderson 
149b5c53d1bSAaron Lindsay void arm_register_pre_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook,
150b5c53d1bSAaron Lindsay                                  void *opaque)
151b5c53d1bSAaron Lindsay {
152b5c53d1bSAaron Lindsay     ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1);
153b5c53d1bSAaron Lindsay 
154b5c53d1bSAaron Lindsay     entry->hook = hook;
155b5c53d1bSAaron Lindsay     entry->opaque = opaque;
156b5c53d1bSAaron Lindsay 
157b5c53d1bSAaron Lindsay     QLIST_INSERT_HEAD(&cpu->pre_el_change_hooks, entry, node);
158b5c53d1bSAaron Lindsay }
159b5c53d1bSAaron Lindsay 
16008267487SAaron Lindsay void arm_register_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook,
161fcf5ef2aSThomas Huth                                  void *opaque)
162fcf5ef2aSThomas Huth {
16308267487SAaron Lindsay     ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1);
16408267487SAaron Lindsay 
16508267487SAaron Lindsay     entry->hook = hook;
16608267487SAaron Lindsay     entry->opaque = opaque;
16708267487SAaron Lindsay 
16808267487SAaron Lindsay     QLIST_INSERT_HEAD(&cpu->el_change_hooks, entry, node);
169fcf5ef2aSThomas Huth }
170fcf5ef2aSThomas Huth 
171fcf5ef2aSThomas Huth static void cp_reg_reset(gpointer key, gpointer value, gpointer opaque)
172fcf5ef2aSThomas Huth {
173fcf5ef2aSThomas Huth     /* Reset a single ARMCPRegInfo register */
174fcf5ef2aSThomas Huth     ARMCPRegInfo *ri = value;
175fcf5ef2aSThomas Huth     ARMCPU *cpu = opaque;
176fcf5ef2aSThomas Huth 
17787c3f0f2SRichard Henderson     if (ri->type & (ARM_CP_SPECIAL_MASK | ARM_CP_ALIAS)) {
178fcf5ef2aSThomas Huth         return;
179fcf5ef2aSThomas Huth     }
180fcf5ef2aSThomas Huth 
181fcf5ef2aSThomas Huth     if (ri->resetfn) {
182fcf5ef2aSThomas Huth         ri->resetfn(&cpu->env, ri);
183fcf5ef2aSThomas Huth         return;
184fcf5ef2aSThomas Huth     }
185fcf5ef2aSThomas Huth 
186fcf5ef2aSThomas Huth     /* A zero offset is never possible as it would be regs[0]
187fcf5ef2aSThomas Huth      * so we use it to indicate that reset is being handled elsewhere.
188fcf5ef2aSThomas Huth      * This is basically only used for fields in non-core coprocessors
189fcf5ef2aSThomas Huth      * (like the pxa2xx ones).
190fcf5ef2aSThomas Huth      */
191fcf5ef2aSThomas Huth     if (!ri->fieldoffset) {
192fcf5ef2aSThomas Huth         return;
193fcf5ef2aSThomas Huth     }
194fcf5ef2aSThomas Huth 
195fcf5ef2aSThomas Huth     if (cpreg_field_is_64bit(ri)) {
196fcf5ef2aSThomas Huth         CPREG_FIELD64(&cpu->env, ri) = ri->resetvalue;
197fcf5ef2aSThomas Huth     } else {
198fcf5ef2aSThomas Huth         CPREG_FIELD32(&cpu->env, ri) = ri->resetvalue;
199fcf5ef2aSThomas Huth     }
200fcf5ef2aSThomas Huth }
201fcf5ef2aSThomas Huth 
202fcf5ef2aSThomas Huth static void cp_reg_check_reset(gpointer key, gpointer value,  gpointer opaque)
203fcf5ef2aSThomas Huth {
204fcf5ef2aSThomas Huth     /* Purely an assertion check: we've already done reset once,
205fcf5ef2aSThomas Huth      * so now check that running the reset for the cpreg doesn't
206fcf5ef2aSThomas Huth      * change its value. This traps bugs where two different cpregs
207fcf5ef2aSThomas Huth      * both try to reset the same state field but to different values.
208fcf5ef2aSThomas Huth      */
209fcf5ef2aSThomas Huth     ARMCPRegInfo *ri = value;
210fcf5ef2aSThomas Huth     ARMCPU *cpu = opaque;
211fcf5ef2aSThomas Huth     uint64_t oldvalue, newvalue;
212fcf5ef2aSThomas Huth 
21387c3f0f2SRichard Henderson     if (ri->type & (ARM_CP_SPECIAL_MASK | ARM_CP_ALIAS | ARM_CP_NO_RAW)) {
214fcf5ef2aSThomas Huth         return;
215fcf5ef2aSThomas Huth     }
216fcf5ef2aSThomas Huth 
217fcf5ef2aSThomas Huth     oldvalue = read_raw_cp_reg(&cpu->env, ri);
218fcf5ef2aSThomas Huth     cp_reg_reset(key, value, opaque);
219fcf5ef2aSThomas Huth     newvalue = read_raw_cp_reg(&cpu->env, ri);
220fcf5ef2aSThomas Huth     assert(oldvalue == newvalue);
221fcf5ef2aSThomas Huth }
222fcf5ef2aSThomas Huth 
223ad80e367SPeter Maydell static void arm_cpu_reset_hold(Object *obj, ResetType type)
224fcf5ef2aSThomas Huth {
225348802b5SPhilippe Mathieu-Daudé     CPUState *cs = CPU(obj);
226348802b5SPhilippe Mathieu-Daudé     ARMCPU *cpu = ARM_CPU(cs);
227348802b5SPhilippe Mathieu-Daudé     ARMCPUClass *acc = ARM_CPU_GET_CLASS(obj);
228fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
229fcf5ef2aSThomas Huth 
2309130cadeSPeter Maydell     if (acc->parent_phases.hold) {
231ad80e367SPeter Maydell         acc->parent_phases.hold(obj, type);
2329130cadeSPeter Maydell     }
233fcf5ef2aSThomas Huth 
2341f5c00cfSAlex Bennée     memset(env, 0, offsetof(CPUARMState, end_reset_fields));
2351f5c00cfSAlex Bennée 
236fcf5ef2aSThomas Huth     g_hash_table_foreach(cpu->cp_regs, cp_reg_reset, cpu);
237fcf5ef2aSThomas Huth     g_hash_table_foreach(cpu->cp_regs, cp_reg_check_reset, cpu);
238fcf5ef2aSThomas Huth 
239fcf5ef2aSThomas Huth     env->vfp.xregs[ARM_VFP_FPSID] = cpu->reset_fpsid;
24047576b94SRichard Henderson     env->vfp.xregs[ARM_VFP_MVFR0] = cpu->isar.mvfr0;
24147576b94SRichard Henderson     env->vfp.xregs[ARM_VFP_MVFR1] = cpu->isar.mvfr1;
24247576b94SRichard Henderson     env->vfp.xregs[ARM_VFP_MVFR2] = cpu->isar.mvfr2;
243fcf5ef2aSThomas Huth 
244348802b5SPhilippe Mathieu-Daudé     cpu->power_state = cs->start_powered_off ? PSCI_OFF : PSCI_ON;
245fcf5ef2aSThomas Huth 
246fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
247fcf5ef2aSThomas Huth         env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
248fcf5ef2aSThomas Huth     }
249fcf5ef2aSThomas Huth 
250fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
251fcf5ef2aSThomas Huth         /* 64 bit CPUs always start in 64 bit mode */
25253221552SRichard Henderson         env->aarch64 = true;
253fcf5ef2aSThomas Huth #if defined(CONFIG_USER_ONLY)
254fcf5ef2aSThomas Huth         env->pstate = PSTATE_MODE_EL0t;
255fcf5ef2aSThomas Huth         /* Userspace expects access to DC ZVA, CTL_EL0 and the cache ops */
256fcf5ef2aSThomas Huth         env->cp15.sctlr_el[1] |= SCTLR_UCT | SCTLR_UCI | SCTLR_DZE;
257276c6e81SRichard Henderson         /* Enable all PAC keys.  */
258276c6e81SRichard Henderson         env->cp15.sctlr_el[1] |= (SCTLR_EnIA | SCTLR_EnIB |
259276c6e81SRichard Henderson                                   SCTLR_EnDA | SCTLR_EnDB);
260cda86e2bSRichard Henderson         /* Trap on btype=3 for PACIxSP. */
261cda86e2bSRichard Henderson         env->cp15.sctlr_el[1] |= SCTLR_BT0;
262d03396a8SRichard Henderson         /* Trap on implementation defined registers. */
263d03396a8SRichard Henderson         if (cpu_isar_feature(aa64_tidcp1, cpu)) {
264d03396a8SRichard Henderson             env->cp15.sctlr_el[1] |= SCTLR_TIDCP;
265d03396a8SRichard Henderson         }
266fcf5ef2aSThomas Huth         /* and to the FP/Neon instructions */
267fab8ad39SRichard Henderson         env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
268fab8ad39SRichard Henderson                                          CPACR_EL1, FPEN, 3);
26946303535SRichard Henderson         /* and to the SVE instructions, with default vector length */
27046303535SRichard Henderson         if (cpu_isar_feature(aa64_sve, cpu)) {
271fab8ad39SRichard Henderson             env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
272fab8ad39SRichard Henderson                                              CPACR_EL1, ZEN, 3);
27387252bdeSRichard Henderson             env->vfp.zcr_el[1] = cpu->sve_default_vq - 1;
2747b6a2198SAlex Bennée         }
27578011586SRichard Henderson         /* and for SME instructions, with default vector length, and TPIDR2 */
27678011586SRichard Henderson         if (cpu_isar_feature(aa64_sme, cpu)) {
27778011586SRichard Henderson             env->cp15.sctlr_el[1] |= SCTLR_EnTP2;
27878011586SRichard Henderson             env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
27978011586SRichard Henderson                                              CPACR_EL1, SMEN, 3);
28078011586SRichard Henderson             env->vfp.smcr_el[1] = cpu->sme_default_vq - 1;
28178011586SRichard Henderson             if (cpu_isar_feature(aa64_sme_fa64, cpu)) {
28278011586SRichard Henderson                 env->vfp.smcr_el[1] = FIELD_DP64(env->vfp.smcr_el[1],
28378011586SRichard Henderson                                                  SMCR, FA64, 1);
28478011586SRichard Henderson             }
28578011586SRichard Henderson         }
286f6a148feSRichard Henderson         /*
287691f1ffdSRichard Henderson          * Enable 48-bit address space (TODO: take reserved_va into account).
28816c84978SRichard Henderson          * Enable TBI0 but not TBI1.
28916c84978SRichard Henderson          * Note that this must match useronly_clean_ptr.
290f6a148feSRichard Henderson          */
291cb4a0a34SPeter Maydell         env->cp15.tcr_el[1] = 5 | (1ULL << 37);
292e3232864SRichard Henderson 
293e3232864SRichard Henderson         /* Enable MTE */
294e3232864SRichard Henderson         if (cpu_isar_feature(aa64_mte, cpu)) {
295e3232864SRichard Henderson             /* Enable tag access, but leave TCF0 as No Effect (0). */
296e3232864SRichard Henderson             env->cp15.sctlr_el[1] |= SCTLR_ATA0;
297e3232864SRichard Henderson             /*
298e3232864SRichard Henderson              * Exclude all tags, so that tag 0 is always used.
299e3232864SRichard Henderson              * This corresponds to Linux current->thread.gcr_incl = 0.
300e3232864SRichard Henderson              *
301e3232864SRichard Henderson              * Set RRND, so that helper_irg() will generate a seed later.
302e3232864SRichard Henderson              * Here in cpu_reset(), the crypto subsystem has not yet been
303e3232864SRichard Henderson              * initialized.
304e3232864SRichard Henderson              */
305e3232864SRichard Henderson             env->cp15.gcr_el1 = 0x1ffff;
306e3232864SRichard Henderson         }
3077cb1e618SRichard Henderson         /*
3087cb1e618SRichard Henderson          * Disable access to SCXTNUM_EL0 from CSV2_1p2.
3097cb1e618SRichard Henderson          * This is not yet exposed from the Linux kernel in any way.
3107cb1e618SRichard Henderson          */
3117cb1e618SRichard Henderson         env->cp15.sctlr_el[1] |= SCTLR_TSCXT;
312f9ac7788SZhuojia Shen         /* Disable access to Debug Communication Channel (DCC). */
313f9ac7788SZhuojia Shen         env->cp15.mdscr_el1 |= 1 << 12;
314a6b2c5a0SPeter Maydell         /* Enable FEAT_MOPS */
315a6b2c5a0SPeter Maydell         env->cp15.sctlr_el[1] |= SCTLR_MSCEN;
316fcf5ef2aSThomas Huth #else
317fcf5ef2aSThomas Huth         /* Reset into the highest available EL */
318fcf5ef2aSThomas Huth         if (arm_feature(env, ARM_FEATURE_EL3)) {
319fcf5ef2aSThomas Huth             env->pstate = PSTATE_MODE_EL3h;
320fcf5ef2aSThomas Huth         } else if (arm_feature(env, ARM_FEATURE_EL2)) {
321fcf5ef2aSThomas Huth             env->pstate = PSTATE_MODE_EL2h;
322fcf5ef2aSThomas Huth         } else {
323fcf5ef2aSThomas Huth             env->pstate = PSTATE_MODE_EL1h;
324fcf5ef2aSThomas Huth         }
3254a7319b7SEdgar E. Iglesias 
3264a7319b7SEdgar E. Iglesias         /* Sample rvbar at reset.  */
3274a7319b7SEdgar E. Iglesias         env->cp15.rvbar = cpu->rvbar_prop;
3284a7319b7SEdgar E. Iglesias         env->pc = env->cp15.rvbar;
329fcf5ef2aSThomas Huth #endif
330fcf5ef2aSThomas Huth     } else {
331fcf5ef2aSThomas Huth #if defined(CONFIG_USER_ONLY)
332fcf5ef2aSThomas Huth         /* Userspace expects access to cp10 and cp11 for FP/Neon */
333fab8ad39SRichard Henderson         env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
334fab8ad39SRichard Henderson                                          CPACR, CP10, 3);
335fab8ad39SRichard Henderson         env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
336fab8ad39SRichard Henderson                                          CPACR, CP11, 3);
337fcf5ef2aSThomas Huth #endif
338910e4f24STobias Röhmel         if (arm_feature(env, ARM_FEATURE_V8)) {
339910e4f24STobias Röhmel             env->cp15.rvbar = cpu->rvbar_prop;
340910e4f24STobias Röhmel             env->regs[15] = cpu->rvbar_prop;
341910e4f24STobias Röhmel         }
342fcf5ef2aSThomas Huth     }
343fcf5ef2aSThomas Huth 
344fcf5ef2aSThomas Huth #if defined(CONFIG_USER_ONLY)
345fcf5ef2aSThomas Huth     env->uncached_cpsr = ARM_CPU_MODE_USR;
346fcf5ef2aSThomas Huth     /* For user mode we must enable access to coprocessors */
347fcf5ef2aSThomas Huth     env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
348fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
349fcf5ef2aSThomas Huth         env->cp15.c15_cpar = 3;
350fcf5ef2aSThomas Huth     } else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
351fcf5ef2aSThomas Huth         env->cp15.c15_cpar = 1;
352fcf5ef2aSThomas Huth     }
353fcf5ef2aSThomas Huth #else
354060a65dfSPeter Maydell 
355060a65dfSPeter Maydell     /*
356060a65dfSPeter Maydell      * If the highest available EL is EL2, AArch32 will start in Hyp
357060a65dfSPeter Maydell      * mode; otherwise it starts in SVC. Note that if we start in
358060a65dfSPeter Maydell      * AArch64 then these values in the uncached_cpsr will be ignored.
359060a65dfSPeter Maydell      */
360060a65dfSPeter Maydell     if (arm_feature(env, ARM_FEATURE_EL2) &&
361060a65dfSPeter Maydell         !arm_feature(env, ARM_FEATURE_EL3)) {
362060a65dfSPeter Maydell         env->uncached_cpsr = ARM_CPU_MODE_HYP;
363060a65dfSPeter Maydell     } else {
364fcf5ef2aSThomas Huth         env->uncached_cpsr = ARM_CPU_MODE_SVC;
365060a65dfSPeter Maydell     }
366fcf5ef2aSThomas Huth     env->daif = PSTATE_D | PSTATE_A | PSTATE_I | PSTATE_F;
3671426f244SPeter Maydell 
3681426f244SPeter Maydell     /* AArch32 has a hard highvec setting of 0xFFFF0000.  If we are currently
3691426f244SPeter Maydell      * executing as AArch32 then check if highvecs are enabled and
3701426f244SPeter Maydell      * adjust the PC accordingly.
3711426f244SPeter Maydell      */
3721426f244SPeter Maydell     if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
3731426f244SPeter Maydell         env->regs[15] = 0xFFFF0000;
3741426f244SPeter Maydell     }
3751426f244SPeter Maydell 
3761426f244SPeter Maydell     env->vfp.xregs[ARM_VFP_FPEXC] = 0;
377b62ceeafSPeter Maydell #endif
378dc7abe4dSMichael Davidsaver 
379531c60a9SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M)) {
380b62ceeafSPeter Maydell #ifndef CONFIG_USER_ONLY
381fcf5ef2aSThomas Huth         uint32_t initial_msp; /* Loaded from 0x0 */
382fcf5ef2aSThomas Huth         uint32_t initial_pc; /* Loaded from 0x4 */
383fcf5ef2aSThomas Huth         uint8_t *rom;
38438e2a77cSPeter Maydell         uint32_t vecbase;
385b62ceeafSPeter Maydell #endif
386fcf5ef2aSThomas Huth 
3878128c8e8SPeter Maydell         if (cpu_isar_feature(aa32_lob, cpu)) {
3888128c8e8SPeter Maydell             /*
3898128c8e8SPeter Maydell              * LTPSIZE is constant 4 if MVE not implemented, and resets
3908128c8e8SPeter Maydell              * to an UNKNOWN value if MVE is implemented. We choose to
3918128c8e8SPeter Maydell              * always reset to 4.
3928128c8e8SPeter Maydell              */
3938128c8e8SPeter Maydell             env->v7m.ltpsize = 4;
39499c7834fSPeter Maydell             /* The LTPSIZE field in FPDSCR is constant and reads as 4. */
39599c7834fSPeter Maydell             env->v7m.fpdscr[M_REG_NS] = 4 << FPCR_LTPSIZE_SHIFT;
39699c7834fSPeter Maydell             env->v7m.fpdscr[M_REG_S] = 4 << FPCR_LTPSIZE_SHIFT;
3978128c8e8SPeter Maydell         }
3988128c8e8SPeter Maydell 
3991e577cc7SPeter Maydell         if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
4001e577cc7SPeter Maydell             env->v7m.secure = true;
4013b2e9344SPeter Maydell         } else {
4023b2e9344SPeter Maydell             /* This bit resets to 0 if security is supported, but 1 if
4033b2e9344SPeter Maydell              * it is not. The bit is not present in v7M, but we set it
4043b2e9344SPeter Maydell              * here so we can avoid having to make checks on it conditional
4053b2e9344SPeter Maydell              * on ARM_FEATURE_V8 (we don't let the guest see the bit).
4063b2e9344SPeter Maydell              */
4073b2e9344SPeter Maydell             env->v7m.aircr = R_V7M_AIRCR_BFHFNMINS_MASK;
40802ac2f7fSPeter Maydell             /*
40902ac2f7fSPeter Maydell              * Set NSACR to indicate "NS access permitted to everything";
41002ac2f7fSPeter Maydell              * this avoids having to have all the tests of it being
41102ac2f7fSPeter Maydell              * conditional on ARM_FEATURE_M_SECURITY. Note also that from
41202ac2f7fSPeter Maydell              * v8.1M the guest-visible value of NSACR in a CPU without the
41302ac2f7fSPeter Maydell              * Security Extension is 0xcff.
41402ac2f7fSPeter Maydell              */
41502ac2f7fSPeter Maydell             env->v7m.nsacr = 0xcff;
4161e577cc7SPeter Maydell         }
4171e577cc7SPeter Maydell 
4189d40cd8aSPeter Maydell         /* In v7M the reset value of this bit is IMPDEF, but ARM recommends
4192c4da50dSPeter Maydell          * that it resets to 1, so QEMU always does that rather than making
4209d40cd8aSPeter Maydell          * it dependent on CPU model. In v8M it is RES1.
4212c4da50dSPeter Maydell          */
4229d40cd8aSPeter Maydell         env->v7m.ccr[M_REG_NS] = R_V7M_CCR_STKALIGN_MASK;
4239d40cd8aSPeter Maydell         env->v7m.ccr[M_REG_S] = R_V7M_CCR_STKALIGN_MASK;
4249d40cd8aSPeter Maydell         if (arm_feature(env, ARM_FEATURE_V8)) {
4259d40cd8aSPeter Maydell             /* in v8M the NONBASETHRDENA bit [0] is RES1 */
4269d40cd8aSPeter Maydell             env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_NONBASETHRDENA_MASK;
4279d40cd8aSPeter Maydell             env->v7m.ccr[M_REG_S] |= R_V7M_CCR_NONBASETHRDENA_MASK;
4289d40cd8aSPeter Maydell         }
42922ab3460SJulia Suvorova         if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
43022ab3460SJulia Suvorova             env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_UNALIGN_TRP_MASK;
43122ab3460SJulia Suvorova             env->v7m.ccr[M_REG_S] |= R_V7M_CCR_UNALIGN_TRP_MASK;
43222ab3460SJulia Suvorova         }
4332c4da50dSPeter Maydell 
4347fbc6a40SRichard Henderson         if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
435d33abe82SPeter Maydell             env->v7m.fpccr[M_REG_NS] = R_V7M_FPCCR_ASPEN_MASK;
436d33abe82SPeter Maydell             env->v7m.fpccr[M_REG_S] = R_V7M_FPCCR_ASPEN_MASK |
437d33abe82SPeter Maydell                 R_V7M_FPCCR_LSPEN_MASK | R_V7M_FPCCR_S_MASK;
438d33abe82SPeter Maydell         }
439b62ceeafSPeter Maydell 
440b62ceeafSPeter Maydell #ifndef CONFIG_USER_ONLY
441056f43dfSPeter Maydell         /* Unlike A/R profile, M profile defines the reset LR value */
442056f43dfSPeter Maydell         env->regs[14] = 0xffffffff;
443056f43dfSPeter Maydell 
44438e2a77cSPeter Maydell         env->v7m.vecbase[M_REG_S] = cpu->init_svtor & 0xffffff80;
4457cda2149SPeter Maydell         env->v7m.vecbase[M_REG_NS] = cpu->init_nsvtor & 0xffffff80;
44638e2a77cSPeter Maydell 
44738e2a77cSPeter Maydell         /* Load the initial SP and PC from offset 0 and 4 in the vector table */
44838e2a77cSPeter Maydell         vecbase = env->v7m.vecbase[env->v7m.secure];
449348802b5SPhilippe Mathieu-Daudé         rom = rom_ptr_for_as(cs->as, vecbase, 8);
450fcf5ef2aSThomas Huth         if (rom) {
451fcf5ef2aSThomas Huth             /* Address zero is covered by ROM which hasn't yet been
452fcf5ef2aSThomas Huth              * copied into physical memory.
453fcf5ef2aSThomas Huth              */
454fcf5ef2aSThomas Huth             initial_msp = ldl_p(rom);
455fcf5ef2aSThomas Huth             initial_pc = ldl_p(rom + 4);
456fcf5ef2aSThomas Huth         } else {
457fcf5ef2aSThomas Huth             /* Address zero not covered by a ROM blob, or the ROM blob
458fcf5ef2aSThomas Huth              * is in non-modifiable memory and this is a second reset after
459fcf5ef2aSThomas Huth              * it got copied into memory. In the latter case, rom_ptr
460fcf5ef2aSThomas Huth              * will return a NULL pointer and we should use ldl_phys instead.
461fcf5ef2aSThomas Huth              */
462348802b5SPhilippe Mathieu-Daudé             initial_msp = ldl_phys(cs->as, vecbase);
463348802b5SPhilippe Mathieu-Daudé             initial_pc = ldl_phys(cs->as, vecbase + 4);
464fcf5ef2aSThomas Huth         }
465fcf5ef2aSThomas Huth 
4668cc2246cSPeter Maydell         qemu_log_mask(CPU_LOG_INT,
4678cc2246cSPeter Maydell                       "Loaded reset SP 0x%x PC 0x%x from vector table\n",
4688cc2246cSPeter Maydell                       initial_msp, initial_pc);
4698cc2246cSPeter Maydell 
470fcf5ef2aSThomas Huth         env->regs[13] = initial_msp & 0xFFFFFFFC;
471fcf5ef2aSThomas Huth         env->regs[15] = initial_pc & ~1;
472fcf5ef2aSThomas Huth         env->thumb = initial_pc & 1;
473b62ceeafSPeter Maydell #else
474b62ceeafSPeter Maydell         /*
475b62ceeafSPeter Maydell          * For user mode we run non-secure and with access to the FPU.
476b62ceeafSPeter Maydell          * The FPU context is active (ie does not need further setup)
477b62ceeafSPeter Maydell          * and is owned by non-secure.
478b62ceeafSPeter Maydell          */
479b62ceeafSPeter Maydell         env->v7m.secure = false;
480b62ceeafSPeter Maydell         env->v7m.nsacr = 0xcff;
481b62ceeafSPeter Maydell         env->v7m.cpacr[M_REG_NS] = 0xf0ffff;
482b62ceeafSPeter Maydell         env->v7m.fpccr[M_REG_S] &=
483b62ceeafSPeter Maydell             ~(R_V7M_FPCCR_LSPEN_MASK | R_V7M_FPCCR_S_MASK);
484b62ceeafSPeter Maydell         env->v7m.control[M_REG_S] |= R_V7M_CONTROL_FPCA_MASK;
485b62ceeafSPeter Maydell #endif
486fcf5ef2aSThomas Huth     }
487fcf5ef2aSThomas Huth 
488dc3c4c14SPeter Maydell     /* M profile requires that reset clears the exclusive monitor;
489dc3c4c14SPeter Maydell      * A profile does not, but clearing it makes more sense than having it
490dc3c4c14SPeter Maydell      * set with an exclusive access on address zero.
491dc3c4c14SPeter Maydell      */
492dc3c4c14SPeter Maydell     arm_clear_exclusive(env);
493dc3c4c14SPeter Maydell 
4940e1a46bbSPeter Maydell     if (arm_feature(env, ARM_FEATURE_PMSA)) {
49569ceea64SPeter Maydell         if (cpu->pmsav7_dregion > 0) {
4960e1a46bbSPeter Maydell             if (arm_feature(env, ARM_FEATURE_V8)) {
49762c58ee0SPeter Maydell                 memset(env->pmsav8.rbar[M_REG_NS], 0,
49862c58ee0SPeter Maydell                        sizeof(*env->pmsav8.rbar[M_REG_NS])
49962c58ee0SPeter Maydell                        * cpu->pmsav7_dregion);
50062c58ee0SPeter Maydell                 memset(env->pmsav8.rlar[M_REG_NS], 0,
50162c58ee0SPeter Maydell                        sizeof(*env->pmsav8.rlar[M_REG_NS])
50262c58ee0SPeter Maydell                        * cpu->pmsav7_dregion);
50362c58ee0SPeter Maydell                 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
50462c58ee0SPeter Maydell                     memset(env->pmsav8.rbar[M_REG_S], 0,
50562c58ee0SPeter Maydell                            sizeof(*env->pmsav8.rbar[M_REG_S])
50662c58ee0SPeter Maydell                            * cpu->pmsav7_dregion);
50762c58ee0SPeter Maydell                     memset(env->pmsav8.rlar[M_REG_S], 0,
50862c58ee0SPeter Maydell                            sizeof(*env->pmsav8.rlar[M_REG_S])
50962c58ee0SPeter Maydell                            * cpu->pmsav7_dregion);
51062c58ee0SPeter Maydell                 }
5110e1a46bbSPeter Maydell             } else if (arm_feature(env, ARM_FEATURE_V7)) {
51269ceea64SPeter Maydell                 memset(env->pmsav7.drbar, 0,
51369ceea64SPeter Maydell                        sizeof(*env->pmsav7.drbar) * cpu->pmsav7_dregion);
51469ceea64SPeter Maydell                 memset(env->pmsav7.drsr, 0,
51569ceea64SPeter Maydell                        sizeof(*env->pmsav7.drsr) * cpu->pmsav7_dregion);
51669ceea64SPeter Maydell                 memset(env->pmsav7.dracr, 0,
51769ceea64SPeter Maydell                        sizeof(*env->pmsav7.dracr) * cpu->pmsav7_dregion);
51869ceea64SPeter Maydell             }
5190e1a46bbSPeter Maydell         }
520761c4642STobias Röhmel 
521761c4642STobias Röhmel         if (cpu->pmsav8r_hdregion > 0) {
522761c4642STobias Röhmel             memset(env->pmsav8.hprbar, 0,
523761c4642STobias Röhmel                    sizeof(*env->pmsav8.hprbar) * cpu->pmsav8r_hdregion);
524761c4642STobias Röhmel             memset(env->pmsav8.hprlar, 0,
525761c4642STobias Röhmel                    sizeof(*env->pmsav8.hprlar) * cpu->pmsav8r_hdregion);
526761c4642STobias Röhmel         }
527761c4642STobias Röhmel 
5281bc04a88SPeter Maydell         env->pmsav7.rnr[M_REG_NS] = 0;
5291bc04a88SPeter Maydell         env->pmsav7.rnr[M_REG_S] = 0;
5304125e6feSPeter Maydell         env->pmsav8.mair0[M_REG_NS] = 0;
5314125e6feSPeter Maydell         env->pmsav8.mair0[M_REG_S] = 0;
5324125e6feSPeter Maydell         env->pmsav8.mair1[M_REG_NS] = 0;
5334125e6feSPeter Maydell         env->pmsav8.mair1[M_REG_S] = 0;
53469ceea64SPeter Maydell     }
53569ceea64SPeter Maydell 
5369901c576SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
5379901c576SPeter Maydell         if (cpu->sau_sregion > 0) {
5389901c576SPeter Maydell             memset(env->sau.rbar, 0, sizeof(*env->sau.rbar) * cpu->sau_sregion);
5399901c576SPeter Maydell             memset(env->sau.rlar, 0, sizeof(*env->sau.rlar) * cpu->sau_sregion);
5409901c576SPeter Maydell         }
5419901c576SPeter Maydell         env->sau.rnr = 0;
5429901c576SPeter Maydell         /* SAU_CTRL reset value is IMPDEF; we choose 0, which is what
5439901c576SPeter Maydell          * the Cortex-M33 does.
5449901c576SPeter Maydell          */
5459901c576SPeter Maydell         env->sau.ctrl = 0;
5469901c576SPeter Maydell     }
5479901c576SPeter Maydell 
548fcf5ef2aSThomas Huth     set_flush_to_zero(1, &env->vfp.standard_fp_status);
549fcf5ef2aSThomas Huth     set_flush_inputs_to_zero(1, &env->vfp.standard_fp_status);
550fcf5ef2aSThomas Huth     set_default_nan_mode(1, &env->vfp.standard_fp_status);
551aaae563bSPeter Maydell     set_default_nan_mode(1, &env->vfp.standard_fp_status_f16);
552fcf5ef2aSThomas Huth     set_float_detect_tininess(float_tininess_before_rounding,
553fcf5ef2aSThomas Huth                               &env->vfp.fp_status);
554fcf5ef2aSThomas Huth     set_float_detect_tininess(float_tininess_before_rounding,
555fcf5ef2aSThomas Huth                               &env->vfp.standard_fp_status);
556bcc531f0SPeter Maydell     set_float_detect_tininess(float_tininess_before_rounding,
557bcc531f0SPeter Maydell                               &env->vfp.fp_status_f16);
558aaae563bSPeter Maydell     set_float_detect_tininess(float_tininess_before_rounding,
559aaae563bSPeter Maydell                               &env->vfp.standard_fp_status_f16);
560fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
561fcf5ef2aSThomas Huth     if (kvm_enabled()) {
562fcf5ef2aSThomas Huth         kvm_arm_reset_vcpu(cpu);
563fcf5ef2aSThomas Huth     }
564fcf5ef2aSThomas Huth #endif
565fcf5ef2aSThomas Huth 
566fa05d1abSFabiano Rosas     if (tcg_enabled()) {
567fcf5ef2aSThomas Huth         hw_breakpoint_update_all(cpu);
568fcf5ef2aSThomas Huth         hw_watchpoint_update_all(cpu);
5692b77ad4dSFabiano Rosas 
570a8a79c7aSRichard Henderson         arm_rebuild_hflags(env);
571fcf5ef2aSThomas Huth     }
5722b77ad4dSFabiano Rosas }
573fcf5ef2aSThomas Huth 
5743a45f4f5SPeter Maydell void arm_emulate_firmware_reset(CPUState *cpustate, int target_el)
5753a45f4f5SPeter Maydell {
5763a45f4f5SPeter Maydell     ARMCPU *cpu = ARM_CPU(cpustate);
5773a45f4f5SPeter Maydell     CPUARMState *env = &cpu->env;
5783a45f4f5SPeter Maydell     bool have_el3 = arm_feature(env, ARM_FEATURE_EL3);
5793a45f4f5SPeter Maydell     bool have_el2 = arm_feature(env, ARM_FEATURE_EL2);
5803a45f4f5SPeter Maydell 
5813a45f4f5SPeter Maydell     /*
5823a45f4f5SPeter Maydell      * Check we have the EL we're aiming for. If that is the
5833a45f4f5SPeter Maydell      * highest implemented EL, then cpu_reset has already done
5843a45f4f5SPeter Maydell      * all the work.
5853a45f4f5SPeter Maydell      */
5863a45f4f5SPeter Maydell     switch (target_el) {
5873a45f4f5SPeter Maydell     case 3:
5883a45f4f5SPeter Maydell         assert(have_el3);
5893a45f4f5SPeter Maydell         return;
5903a45f4f5SPeter Maydell     case 2:
5913a45f4f5SPeter Maydell         assert(have_el2);
5923a45f4f5SPeter Maydell         if (!have_el3) {
5933a45f4f5SPeter Maydell             return;
5943a45f4f5SPeter Maydell         }
5953a45f4f5SPeter Maydell         break;
5963a45f4f5SPeter Maydell     case 1:
5973a45f4f5SPeter Maydell         if (!have_el3 && !have_el2) {
5983a45f4f5SPeter Maydell             return;
5993a45f4f5SPeter Maydell         }
6003a45f4f5SPeter Maydell         break;
6013a45f4f5SPeter Maydell     default:
6023a45f4f5SPeter Maydell         g_assert_not_reached();
6033a45f4f5SPeter Maydell     }
6043a45f4f5SPeter Maydell 
6053a45f4f5SPeter Maydell     if (have_el3) {
6063a45f4f5SPeter Maydell         /*
6073a45f4f5SPeter Maydell          * Set the EL3 state so code can run at EL2. This should match
6083a45f4f5SPeter Maydell          * the requirements set by Linux in its booting spec.
6093a45f4f5SPeter Maydell          */
6103a45f4f5SPeter Maydell         if (env->aarch64) {
6113a45f4f5SPeter Maydell             env->cp15.scr_el3 |= SCR_RW;
6123a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_pauth, cpu)) {
6133a45f4f5SPeter Maydell                 env->cp15.scr_el3 |= SCR_API | SCR_APK;
6143a45f4f5SPeter Maydell             }
6153a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_mte, cpu)) {
6163a45f4f5SPeter Maydell                 env->cp15.scr_el3 |= SCR_ATA;
6173a45f4f5SPeter Maydell             }
6183a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_sve, cpu)) {
6193a45f4f5SPeter Maydell                 env->cp15.cptr_el[3] |= R_CPTR_EL3_EZ_MASK;
6203a45f4f5SPeter Maydell                 env->vfp.zcr_el[3] = 0xf;
6213a45f4f5SPeter Maydell             }
6223a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_sme, cpu)) {
6233a45f4f5SPeter Maydell                 env->cp15.cptr_el[3] |= R_CPTR_EL3_ESM_MASK;
6243a45f4f5SPeter Maydell                 env->cp15.scr_el3 |= SCR_ENTP2;
6253a45f4f5SPeter Maydell                 env->vfp.smcr_el[3] = 0xf;
6263a45f4f5SPeter Maydell             }
6273a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_hcx, cpu)) {
6283a45f4f5SPeter Maydell                 env->cp15.scr_el3 |= SCR_HXEN;
6293a45f4f5SPeter Maydell             }
6303a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_fgt, cpu)) {
6313a45f4f5SPeter Maydell                 env->cp15.scr_el3 |= SCR_FGTEN;
6323a45f4f5SPeter Maydell             }
6333a45f4f5SPeter Maydell         }
6343a45f4f5SPeter Maydell 
6353a45f4f5SPeter Maydell         if (target_el == 2) {
6363a45f4f5SPeter Maydell             /* If the guest is at EL2 then Linux expects the HVC insn to work */
6373a45f4f5SPeter Maydell             env->cp15.scr_el3 |= SCR_HCE;
6383a45f4f5SPeter Maydell         }
6393a45f4f5SPeter Maydell 
6403a45f4f5SPeter Maydell         /* Put CPU into non-secure state */
6413a45f4f5SPeter Maydell         env->cp15.scr_el3 |= SCR_NS;
6423a45f4f5SPeter Maydell         /* Set NSACR.{CP11,CP10} so NS can access the FPU */
6433a45f4f5SPeter Maydell         env->cp15.nsacr |= 3 << 10;
6443a45f4f5SPeter Maydell     }
6453a45f4f5SPeter Maydell 
6463a45f4f5SPeter Maydell     if (have_el2 && target_el < 2) {
6473a45f4f5SPeter Maydell         /* Set EL2 state so code can run at EL1. */
6483a45f4f5SPeter Maydell         if (env->aarch64) {
6493a45f4f5SPeter Maydell             env->cp15.hcr_el2 |= HCR_RW;
6503a45f4f5SPeter Maydell         }
6513a45f4f5SPeter Maydell     }
6523a45f4f5SPeter Maydell 
6533a45f4f5SPeter Maydell     /* Set the CPU to the desired state */
6543a45f4f5SPeter Maydell     if (env->aarch64) {
6553a45f4f5SPeter Maydell         env->pstate = aarch64_pstate_mode(target_el, true);
6563a45f4f5SPeter Maydell     } else {
6573a45f4f5SPeter Maydell         static const uint32_t mode_for_el[] = {
6583a45f4f5SPeter Maydell             0,
6593a45f4f5SPeter Maydell             ARM_CPU_MODE_SVC,
6603a45f4f5SPeter Maydell             ARM_CPU_MODE_HYP,
6613a45f4f5SPeter Maydell             ARM_CPU_MODE_SVC,
6623a45f4f5SPeter Maydell         };
6633a45f4f5SPeter Maydell 
6643a45f4f5SPeter Maydell         cpsr_write(env, mode_for_el[target_el], CPSR_M, CPSRWriteRaw);
6653a45f4f5SPeter Maydell     }
6663a45f4f5SPeter Maydell }
6673a45f4f5SPeter Maydell 
6683a45f4f5SPeter Maydell 
6699e406eeaSPhilippe Mathieu-Daudé #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
670083afd18SPhilippe Mathieu-Daudé 
671310cedf3SRichard Henderson static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx,
672be879556SRichard Henderson                                      unsigned int target_el,
673be879556SRichard Henderson                                      unsigned int cur_el, bool secure,
674be879556SRichard Henderson                                      uint64_t hcr_el2)
675310cedf3SRichard Henderson {
676b77af26eSRichard Henderson     CPUARMState *env = cpu_env(cs);
677310cedf3SRichard Henderson     bool pstate_unmasked;
67816e07f78SRichard Henderson     bool unmasked = false;
679b36a32eaSJinjie Ruan     bool allIntMask = false;
680310cedf3SRichard Henderson 
681310cedf3SRichard Henderson     /*
682310cedf3SRichard Henderson      * Don't take exceptions if they target a lower EL.
683310cedf3SRichard Henderson      * This check should catch any exceptions that would not be taken
684310cedf3SRichard Henderson      * but left pending.
685310cedf3SRichard Henderson      */
686310cedf3SRichard Henderson     if (cur_el > target_el) {
687310cedf3SRichard Henderson         return false;
688310cedf3SRichard Henderson     }
689310cedf3SRichard Henderson 
690b36a32eaSJinjie Ruan     if (cpu_isar_feature(aa64_nmi, env_archcpu(env)) &&
691b36a32eaSJinjie Ruan         env->cp15.sctlr_el[target_el] & SCTLR_NMI && cur_el == target_el) {
692b36a32eaSJinjie Ruan         allIntMask = env->pstate & PSTATE_ALLINT ||
693b36a32eaSJinjie Ruan                      ((env->cp15.sctlr_el[target_el] & SCTLR_SPINTMASK) &&
694b36a32eaSJinjie Ruan                       (env->pstate & PSTATE_SP));
695b36a32eaSJinjie Ruan     }
696b36a32eaSJinjie Ruan 
697310cedf3SRichard Henderson     switch (excp_idx) {
698b36a32eaSJinjie Ruan     case EXCP_NMI:
699b36a32eaSJinjie Ruan         pstate_unmasked = !allIntMask;
700b36a32eaSJinjie Ruan         break;
701b36a32eaSJinjie Ruan 
702b36a32eaSJinjie Ruan     case EXCP_VINMI:
703b36a32eaSJinjie Ruan         if (!(hcr_el2 & HCR_IMO) || (hcr_el2 & HCR_TGE)) {
704b36a32eaSJinjie Ruan             /* VINMIs are only taken when hypervized.  */
705b36a32eaSJinjie Ruan             return false;
706b36a32eaSJinjie Ruan         }
707b36a32eaSJinjie Ruan         return !allIntMask;
708b36a32eaSJinjie Ruan     case EXCP_VFNMI:
709b36a32eaSJinjie Ruan         if (!(hcr_el2 & HCR_FMO) || (hcr_el2 & HCR_TGE)) {
710b36a32eaSJinjie Ruan             /* VFNMIs are only taken when hypervized.  */
711b36a32eaSJinjie Ruan             return false;
712b36a32eaSJinjie Ruan         }
713b36a32eaSJinjie Ruan         return !allIntMask;
714310cedf3SRichard Henderson     case EXCP_FIQ:
715b36a32eaSJinjie Ruan         pstate_unmasked = (!(env->daif & PSTATE_F)) && (!allIntMask);
716310cedf3SRichard Henderson         break;
717310cedf3SRichard Henderson 
718310cedf3SRichard Henderson     case EXCP_IRQ:
719b36a32eaSJinjie Ruan         pstate_unmasked = (!(env->daif & PSTATE_I)) && (!allIntMask);
720310cedf3SRichard Henderson         break;
721310cedf3SRichard Henderson 
722310cedf3SRichard Henderson     case EXCP_VFIQ:
723cc974d5cSRémi Denis-Courmont         if (!(hcr_el2 & HCR_FMO) || (hcr_el2 & HCR_TGE)) {
724cc974d5cSRémi Denis-Courmont             /* VFIQs are only taken when hypervized.  */
725310cedf3SRichard Henderson             return false;
726310cedf3SRichard Henderson         }
727b36a32eaSJinjie Ruan         return !(env->daif & PSTATE_F) && (!allIntMask);
728310cedf3SRichard Henderson     case EXCP_VIRQ:
729cc974d5cSRémi Denis-Courmont         if (!(hcr_el2 & HCR_IMO) || (hcr_el2 & HCR_TGE)) {
730cc974d5cSRémi Denis-Courmont             /* VIRQs are only taken when hypervized.  */
731310cedf3SRichard Henderson             return false;
732310cedf3SRichard Henderson         }
733b36a32eaSJinjie Ruan         return !(env->daif & PSTATE_I) && (!allIntMask);
7343c29632fSRichard Henderson     case EXCP_VSERR:
7353c29632fSRichard Henderson         if (!(hcr_el2 & HCR_AMO) || (hcr_el2 & HCR_TGE)) {
7363c29632fSRichard Henderson             /* VIRQs are only taken when hypervized.  */
7373c29632fSRichard Henderson             return false;
7383c29632fSRichard Henderson         }
7393c29632fSRichard Henderson         return !(env->daif & PSTATE_A);
740310cedf3SRichard Henderson     default:
741310cedf3SRichard Henderson         g_assert_not_reached();
742310cedf3SRichard Henderson     }
743310cedf3SRichard Henderson 
744310cedf3SRichard Henderson     /*
745310cedf3SRichard Henderson      * Use the target EL, current execution state and SCR/HCR settings to
746310cedf3SRichard Henderson      * determine whether the corresponding CPSR bit is used to mask the
747310cedf3SRichard Henderson      * interrupt.
748310cedf3SRichard Henderson      */
749310cedf3SRichard Henderson     if ((target_el > cur_el) && (target_el != 1)) {
750310cedf3SRichard Henderson         /* Exceptions targeting a higher EL may not be maskable */
751310cedf3SRichard Henderson         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
752c939a7c7SAke Koomsin             switch (target_el) {
753c939a7c7SAke Koomsin             case 2:
754310cedf3SRichard Henderson                 /*
755c939a7c7SAke Koomsin                  * According to ARM DDI 0487H.a, an interrupt can be masked
756c939a7c7SAke Koomsin                  * when HCR_E2H and HCR_TGE are both set regardless of the
757c939a7c7SAke Koomsin                  * current Security state. Note that we need to revisit this
758c939a7c7SAke Koomsin                  * part again once we need to support NMI.
759310cedf3SRichard Henderson                  */
760c939a7c7SAke Koomsin                 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
76116e07f78SRichard Henderson                         unmasked = true;
762310cedf3SRichard Henderson                 }
763c939a7c7SAke Koomsin                 break;
764c939a7c7SAke Koomsin             case 3:
765c939a7c7SAke Koomsin                 /* Interrupt cannot be masked when the target EL is 3 */
766c939a7c7SAke Koomsin                 unmasked = true;
767c939a7c7SAke Koomsin                 break;
768c939a7c7SAke Koomsin             default:
769c939a7c7SAke Koomsin                 g_assert_not_reached();
770c939a7c7SAke Koomsin             }
771310cedf3SRichard Henderson         } else {
772310cedf3SRichard Henderson             /*
773310cedf3SRichard Henderson              * The old 32-bit-only environment has a more complicated
774310cedf3SRichard Henderson              * masking setup. HCR and SCR bits not only affect interrupt
775310cedf3SRichard Henderson              * routing but also change the behaviour of masking.
776310cedf3SRichard Henderson              */
777310cedf3SRichard Henderson             bool hcr, scr;
778310cedf3SRichard Henderson 
779310cedf3SRichard Henderson             switch (excp_idx) {
780310cedf3SRichard Henderson             case EXCP_FIQ:
781310cedf3SRichard Henderson                 /*
782310cedf3SRichard Henderson                  * If FIQs are routed to EL3 or EL2 then there are cases where
783310cedf3SRichard Henderson                  * we override the CPSR.F in determining if the exception is
784310cedf3SRichard Henderson                  * masked or not. If neither of these are set then we fall back
785310cedf3SRichard Henderson                  * to the CPSR.F setting otherwise we further assess the state
786310cedf3SRichard Henderson                  * below.
787310cedf3SRichard Henderson                  */
788310cedf3SRichard Henderson                 hcr = hcr_el2 & HCR_FMO;
789310cedf3SRichard Henderson                 scr = (env->cp15.scr_el3 & SCR_FIQ);
790310cedf3SRichard Henderson 
791310cedf3SRichard Henderson                 /*
792310cedf3SRichard Henderson                  * When EL3 is 32-bit, the SCR.FW bit controls whether the
793310cedf3SRichard Henderson                  * CPSR.F bit masks FIQ interrupts when taken in non-secure
794310cedf3SRichard Henderson                  * state. If SCR.FW is set then FIQs can be masked by CPSR.F
795310cedf3SRichard Henderson                  * when non-secure but only when FIQs are only routed to EL3.
796310cedf3SRichard Henderson                  */
797310cedf3SRichard Henderson                 scr = scr && !((env->cp15.scr_el3 & SCR_FW) && !hcr);
798310cedf3SRichard Henderson                 break;
799310cedf3SRichard Henderson             case EXCP_IRQ:
800310cedf3SRichard Henderson                 /*
801310cedf3SRichard Henderson                  * When EL3 execution state is 32-bit, if HCR.IMO is set then
802310cedf3SRichard Henderson                  * we may override the CPSR.I masking when in non-secure state.
803310cedf3SRichard Henderson                  * The SCR.IRQ setting has already been taken into consideration
804310cedf3SRichard Henderson                  * when setting the target EL, so it does not have a further
805310cedf3SRichard Henderson                  * affect here.
806310cedf3SRichard Henderson                  */
807310cedf3SRichard Henderson                 hcr = hcr_el2 & HCR_IMO;
808310cedf3SRichard Henderson                 scr = false;
809310cedf3SRichard Henderson                 break;
810310cedf3SRichard Henderson             default:
811310cedf3SRichard Henderson                 g_assert_not_reached();
812310cedf3SRichard Henderson             }
813310cedf3SRichard Henderson 
814310cedf3SRichard Henderson             if ((scr || hcr) && !secure) {
81516e07f78SRichard Henderson                 unmasked = true;
816310cedf3SRichard Henderson             }
817310cedf3SRichard Henderson         }
818310cedf3SRichard Henderson     }
819310cedf3SRichard Henderson 
820310cedf3SRichard Henderson     /*
821673d8215SMichael Tokarev      * The PSTATE bits only mask the interrupt if we have not overridden the
822310cedf3SRichard Henderson      * ability above.
823310cedf3SRichard Henderson      */
824310cedf3SRichard Henderson     return unmasked || pstate_unmasked;
825310cedf3SRichard Henderson }
826310cedf3SRichard Henderson 
827083afd18SPhilippe Mathieu-Daudé static bool arm_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
828fcf5ef2aSThomas Huth {
829fcf5ef2aSThomas Huth     CPUClass *cc = CPU_GET_CLASS(cs);
830b77af26eSRichard Henderson     CPUARMState *env = cpu_env(cs);
831fcf5ef2aSThomas Huth     uint32_t cur_el = arm_current_el(env);
832fcf5ef2aSThomas Huth     bool secure = arm_is_secure(env);
833be879556SRichard Henderson     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
834fcf5ef2aSThomas Huth     uint32_t target_el;
835fcf5ef2aSThomas Huth     uint32_t excp_idx;
836d63d0ec5SRichard Henderson 
837d63d0ec5SRichard Henderson     /* The prioritization of interrupts is IMPLEMENTATION DEFINED. */
838fcf5ef2aSThomas Huth 
839b36a32eaSJinjie Ruan     if (cpu_isar_feature(aa64_nmi, env_archcpu(env)) &&
840b36a32eaSJinjie Ruan         (arm_sctlr(env, cur_el) & SCTLR_NMI)) {
841b36a32eaSJinjie Ruan         if (interrupt_request & CPU_INTERRUPT_NMI) {
842b36a32eaSJinjie Ruan             excp_idx = EXCP_NMI;
843b36a32eaSJinjie Ruan             target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
844b36a32eaSJinjie Ruan             if (arm_excp_unmasked(cs, excp_idx, target_el,
845b36a32eaSJinjie Ruan                                   cur_el, secure, hcr_el2)) {
846b36a32eaSJinjie Ruan                 goto found;
847b36a32eaSJinjie Ruan             }
848b36a32eaSJinjie Ruan         }
849b36a32eaSJinjie Ruan         if (interrupt_request & CPU_INTERRUPT_VINMI) {
850b36a32eaSJinjie Ruan             excp_idx = EXCP_VINMI;
851b36a32eaSJinjie Ruan             target_el = 1;
852b36a32eaSJinjie Ruan             if (arm_excp_unmasked(cs, excp_idx, target_el,
853b36a32eaSJinjie Ruan                                   cur_el, secure, hcr_el2)) {
854b36a32eaSJinjie Ruan                 goto found;
855b36a32eaSJinjie Ruan             }
856b36a32eaSJinjie Ruan         }
857b36a32eaSJinjie Ruan         if (interrupt_request & CPU_INTERRUPT_VFNMI) {
858b36a32eaSJinjie Ruan             excp_idx = EXCP_VFNMI;
859b36a32eaSJinjie Ruan             target_el = 1;
860b36a32eaSJinjie Ruan             if (arm_excp_unmasked(cs, excp_idx, target_el,
861b36a32eaSJinjie Ruan                                   cur_el, secure, hcr_el2)) {
862b36a32eaSJinjie Ruan                 goto found;
863b36a32eaSJinjie Ruan             }
864b36a32eaSJinjie Ruan         }
865b36a32eaSJinjie Ruan     } else {
866b36a32eaSJinjie Ruan         /*
867b36a32eaSJinjie Ruan          * NMI disabled: interrupts with superpriority are handled
868b36a32eaSJinjie Ruan          * as if they didn't have it
869b36a32eaSJinjie Ruan          */
870b36a32eaSJinjie Ruan         if (interrupt_request & CPU_INTERRUPT_NMI) {
871b36a32eaSJinjie Ruan             interrupt_request |= CPU_INTERRUPT_HARD;
872b36a32eaSJinjie Ruan         }
873b36a32eaSJinjie Ruan         if (interrupt_request & CPU_INTERRUPT_VINMI) {
874b36a32eaSJinjie Ruan             interrupt_request |= CPU_INTERRUPT_VIRQ;
875b36a32eaSJinjie Ruan         }
876b36a32eaSJinjie Ruan         if (interrupt_request & CPU_INTERRUPT_VFNMI) {
877b36a32eaSJinjie Ruan             interrupt_request |= CPU_INTERRUPT_VFIQ;
878b36a32eaSJinjie Ruan         }
879b36a32eaSJinjie Ruan     }
880b36a32eaSJinjie Ruan 
881fcf5ef2aSThomas Huth     if (interrupt_request & CPU_INTERRUPT_FIQ) {
882fcf5ef2aSThomas Huth         excp_idx = EXCP_FIQ;
883fcf5ef2aSThomas Huth         target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
884be879556SRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
885be879556SRichard Henderson                               cur_el, secure, hcr_el2)) {
886d63d0ec5SRichard Henderson             goto found;
887fcf5ef2aSThomas Huth         }
888fcf5ef2aSThomas Huth     }
889fcf5ef2aSThomas Huth     if (interrupt_request & CPU_INTERRUPT_HARD) {
890fcf5ef2aSThomas Huth         excp_idx = EXCP_IRQ;
891fcf5ef2aSThomas Huth         target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
892be879556SRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
893be879556SRichard Henderson                               cur_el, secure, hcr_el2)) {
894d63d0ec5SRichard Henderson             goto found;
895fcf5ef2aSThomas Huth         }
896fcf5ef2aSThomas Huth     }
897fcf5ef2aSThomas Huth     if (interrupt_request & CPU_INTERRUPT_VIRQ) {
898fcf5ef2aSThomas Huth         excp_idx = EXCP_VIRQ;
899fcf5ef2aSThomas Huth         target_el = 1;
900be879556SRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
901be879556SRichard Henderson                               cur_el, secure, hcr_el2)) {
902d63d0ec5SRichard Henderson             goto found;
903fcf5ef2aSThomas Huth         }
904fcf5ef2aSThomas Huth     }
905fcf5ef2aSThomas Huth     if (interrupt_request & CPU_INTERRUPT_VFIQ) {
906fcf5ef2aSThomas Huth         excp_idx = EXCP_VFIQ;
907fcf5ef2aSThomas Huth         target_el = 1;
908be879556SRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
909be879556SRichard Henderson                               cur_el, secure, hcr_el2)) {
910d63d0ec5SRichard Henderson             goto found;
911d63d0ec5SRichard Henderson         }
912d63d0ec5SRichard Henderson     }
9133c29632fSRichard Henderson     if (interrupt_request & CPU_INTERRUPT_VSERR) {
9143c29632fSRichard Henderson         excp_idx = EXCP_VSERR;
9153c29632fSRichard Henderson         target_el = 1;
9163c29632fSRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
9173c29632fSRichard Henderson                               cur_el, secure, hcr_el2)) {
9183c29632fSRichard Henderson             /* Taking a virtual abort clears HCR_EL2.VSE */
9193c29632fSRichard Henderson             env->cp15.hcr_el2 &= ~HCR_VSE;
9203c29632fSRichard Henderson             cpu_reset_interrupt(cs, CPU_INTERRUPT_VSERR);
9213c29632fSRichard Henderson             goto found;
9223c29632fSRichard Henderson         }
9233c29632fSRichard Henderson     }
924d63d0ec5SRichard Henderson     return false;
925d63d0ec5SRichard Henderson 
926d63d0ec5SRichard Henderson  found:
927fcf5ef2aSThomas Huth     cs->exception_index = excp_idx;
928fcf5ef2aSThomas Huth     env->exception.target_el = target_el;
92978271684SClaudio Fontana     cc->tcg_ops->do_interrupt(cs);
930d63d0ec5SRichard Henderson     return true;
931fcf5ef2aSThomas Huth }
9329e406eeaSPhilippe Mathieu-Daudé 
9339e406eeaSPhilippe Mathieu-Daudé #endif /* CONFIG_TCG && !CONFIG_USER_ONLY */
934fcf5ef2aSThomas Huth 
93589430fc6SPeter Maydell void arm_cpu_update_virq(ARMCPU *cpu)
93689430fc6SPeter Maydell {
93789430fc6SPeter Maydell     /*
93889430fc6SPeter Maydell      * Update the interrupt level for VIRQ, which is the logical OR of
93989430fc6SPeter Maydell      * the HCR_EL2.VI bit and the input line level from the GIC.
94089430fc6SPeter Maydell      */
94189430fc6SPeter Maydell     CPUARMState *env = &cpu->env;
94289430fc6SPeter Maydell     CPUState *cs = CPU(cpu);
94389430fc6SPeter Maydell 
944b36a32eaSJinjie Ruan     bool new_state = ((arm_hcr_el2_eff(env) & HCR_VI) &&
945b36a32eaSJinjie Ruan         !(arm_hcrx_el2_eff(env) & HCRX_VINMI)) ||
94689430fc6SPeter Maydell         (env->irq_line_state & CPU_INTERRUPT_VIRQ);
94789430fc6SPeter Maydell 
94889430fc6SPeter Maydell     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VIRQ) != 0)) {
94989430fc6SPeter Maydell         if (new_state) {
95089430fc6SPeter Maydell             cpu_interrupt(cs, CPU_INTERRUPT_VIRQ);
95189430fc6SPeter Maydell         } else {
95289430fc6SPeter Maydell             cpu_reset_interrupt(cs, CPU_INTERRUPT_VIRQ);
95389430fc6SPeter Maydell         }
95489430fc6SPeter Maydell     }
95589430fc6SPeter Maydell }
95689430fc6SPeter Maydell 
95789430fc6SPeter Maydell void arm_cpu_update_vfiq(ARMCPU *cpu)
95889430fc6SPeter Maydell {
95989430fc6SPeter Maydell     /*
96089430fc6SPeter Maydell      * Update the interrupt level for VFIQ, which is the logical OR of
96189430fc6SPeter Maydell      * the HCR_EL2.VF bit and the input line level from the GIC.
96289430fc6SPeter Maydell      */
96389430fc6SPeter Maydell     CPUARMState *env = &cpu->env;
96489430fc6SPeter Maydell     CPUState *cs = CPU(cpu);
96589430fc6SPeter Maydell 
966b36a32eaSJinjie Ruan     bool new_state = ((arm_hcr_el2_eff(env) & HCR_VF) &&
967b36a32eaSJinjie Ruan         !(arm_hcrx_el2_eff(env) & HCRX_VFNMI)) ||
96889430fc6SPeter Maydell         (env->irq_line_state & CPU_INTERRUPT_VFIQ);
96989430fc6SPeter Maydell 
97089430fc6SPeter Maydell     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VFIQ) != 0)) {
97189430fc6SPeter Maydell         if (new_state) {
97289430fc6SPeter Maydell             cpu_interrupt(cs, CPU_INTERRUPT_VFIQ);
97389430fc6SPeter Maydell         } else {
97489430fc6SPeter Maydell             cpu_reset_interrupt(cs, CPU_INTERRUPT_VFIQ);
97589430fc6SPeter Maydell         }
97689430fc6SPeter Maydell     }
97789430fc6SPeter Maydell }
97889430fc6SPeter Maydell 
979b36a32eaSJinjie Ruan void arm_cpu_update_vinmi(ARMCPU *cpu)
980b36a32eaSJinjie Ruan {
981b36a32eaSJinjie Ruan     /*
982b36a32eaSJinjie Ruan      * Update the interrupt level for VINMI, which is the logical OR of
983b36a32eaSJinjie Ruan      * the HCRX_EL2.VINMI bit and the input line level from the GIC.
984b36a32eaSJinjie Ruan      */
985b36a32eaSJinjie Ruan     CPUARMState *env = &cpu->env;
986b36a32eaSJinjie Ruan     CPUState *cs = CPU(cpu);
987b36a32eaSJinjie Ruan 
988b36a32eaSJinjie Ruan     bool new_state = ((arm_hcr_el2_eff(env) & HCR_VI) &&
989b36a32eaSJinjie Ruan                       (arm_hcrx_el2_eff(env) & HCRX_VINMI)) ||
990b36a32eaSJinjie Ruan         (env->irq_line_state & CPU_INTERRUPT_VINMI);
991b36a32eaSJinjie Ruan 
992b36a32eaSJinjie Ruan     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VINMI) != 0)) {
993b36a32eaSJinjie Ruan         if (new_state) {
994b36a32eaSJinjie Ruan             cpu_interrupt(cs, CPU_INTERRUPT_VINMI);
995b36a32eaSJinjie Ruan         } else {
996b36a32eaSJinjie Ruan             cpu_reset_interrupt(cs, CPU_INTERRUPT_VINMI);
997b36a32eaSJinjie Ruan         }
998b36a32eaSJinjie Ruan     }
999b36a32eaSJinjie Ruan }
1000b36a32eaSJinjie Ruan 
1001b36a32eaSJinjie Ruan void arm_cpu_update_vfnmi(ARMCPU *cpu)
1002b36a32eaSJinjie Ruan {
1003b36a32eaSJinjie Ruan     /*
1004b36a32eaSJinjie Ruan      * Update the interrupt level for VFNMI, which is the HCRX_EL2.VFNMI bit.
1005b36a32eaSJinjie Ruan      */
1006b36a32eaSJinjie Ruan     CPUARMState *env = &cpu->env;
1007b36a32eaSJinjie Ruan     CPUState *cs = CPU(cpu);
1008b36a32eaSJinjie Ruan 
1009b36a32eaSJinjie Ruan     bool new_state = (arm_hcr_el2_eff(env) & HCR_VF) &&
1010b36a32eaSJinjie Ruan                       (arm_hcrx_el2_eff(env) & HCRX_VFNMI);
1011b36a32eaSJinjie Ruan 
1012b36a32eaSJinjie Ruan     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VFNMI) != 0)) {
1013b36a32eaSJinjie Ruan         if (new_state) {
1014b36a32eaSJinjie Ruan             cpu_interrupt(cs, CPU_INTERRUPT_VFNMI);
1015b36a32eaSJinjie Ruan         } else {
1016b36a32eaSJinjie Ruan             cpu_reset_interrupt(cs, CPU_INTERRUPT_VFNMI);
1017b36a32eaSJinjie Ruan         }
1018b36a32eaSJinjie Ruan     }
1019b36a32eaSJinjie Ruan }
1020b36a32eaSJinjie Ruan 
10213c29632fSRichard Henderson void arm_cpu_update_vserr(ARMCPU *cpu)
10223c29632fSRichard Henderson {
10233c29632fSRichard Henderson     /*
10243c29632fSRichard Henderson      * Update the interrupt level for VSERR, which is the HCR_EL2.VSE bit.
10253c29632fSRichard Henderson      */
10263c29632fSRichard Henderson     CPUARMState *env = &cpu->env;
10273c29632fSRichard Henderson     CPUState *cs = CPU(cpu);
10283c29632fSRichard Henderson 
10293c29632fSRichard Henderson     bool new_state = env->cp15.hcr_el2 & HCR_VSE;
10303c29632fSRichard Henderson 
10313c29632fSRichard Henderson     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VSERR) != 0)) {
10323c29632fSRichard Henderson         if (new_state) {
10333c29632fSRichard Henderson             cpu_interrupt(cs, CPU_INTERRUPT_VSERR);
10343c29632fSRichard Henderson         } else {
10353c29632fSRichard Henderson             cpu_reset_interrupt(cs, CPU_INTERRUPT_VSERR);
10363c29632fSRichard Henderson         }
10373c29632fSRichard Henderson     }
10383c29632fSRichard Henderson }
10393c29632fSRichard Henderson 
1040fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
1041fcf5ef2aSThomas Huth static void arm_cpu_set_irq(void *opaque, int irq, int level)
1042fcf5ef2aSThomas Huth {
1043fcf5ef2aSThomas Huth     ARMCPU *cpu = opaque;
1044fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
1045fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
1046fcf5ef2aSThomas Huth     static const int mask[] = {
1047fcf5ef2aSThomas Huth         [ARM_CPU_IRQ] = CPU_INTERRUPT_HARD,
1048fcf5ef2aSThomas Huth         [ARM_CPU_FIQ] = CPU_INTERRUPT_FIQ,
1049fcf5ef2aSThomas Huth         [ARM_CPU_VIRQ] = CPU_INTERRUPT_VIRQ,
1050b36a32eaSJinjie Ruan         [ARM_CPU_VFIQ] = CPU_INTERRUPT_VFIQ,
1051b36a32eaSJinjie Ruan         [ARM_CPU_NMI] = CPU_INTERRUPT_NMI,
1052b36a32eaSJinjie Ruan         [ARM_CPU_VINMI] = CPU_INTERRUPT_VINMI,
1053fcf5ef2aSThomas Huth     };
1054fcf5ef2aSThomas Huth 
10559acd2d33SPeter Maydell     if (!arm_feature(env, ARM_FEATURE_EL2) &&
10569acd2d33SPeter Maydell         (irq == ARM_CPU_VIRQ || irq == ARM_CPU_VFIQ)) {
10579acd2d33SPeter Maydell         /*
10589acd2d33SPeter Maydell          * The GIC might tell us about VIRQ and VFIQ state, but if we don't
10599acd2d33SPeter Maydell          * have EL2 support we don't care. (Unless the guest is doing something
10609acd2d33SPeter Maydell          * silly this will only be calls saying "level is still 0".)
10619acd2d33SPeter Maydell          */
10629acd2d33SPeter Maydell         return;
10639acd2d33SPeter Maydell     }
10649acd2d33SPeter Maydell 
1065ed89f078SPeter Maydell     if (level) {
1066ed89f078SPeter Maydell         env->irq_line_state |= mask[irq];
1067ed89f078SPeter Maydell     } else {
1068ed89f078SPeter Maydell         env->irq_line_state &= ~mask[irq];
1069ed89f078SPeter Maydell     }
1070ed89f078SPeter Maydell 
1071fcf5ef2aSThomas Huth     switch (irq) {
1072fcf5ef2aSThomas Huth     case ARM_CPU_VIRQ:
107389430fc6SPeter Maydell         arm_cpu_update_virq(cpu);
107489430fc6SPeter Maydell         break;
1075fcf5ef2aSThomas Huth     case ARM_CPU_VFIQ:
107689430fc6SPeter Maydell         arm_cpu_update_vfiq(cpu);
107789430fc6SPeter Maydell         break;
1078b36a32eaSJinjie Ruan     case ARM_CPU_VINMI:
1079b36a32eaSJinjie Ruan         arm_cpu_update_vinmi(cpu);
1080b36a32eaSJinjie Ruan         break;
1081fcf5ef2aSThomas Huth     case ARM_CPU_IRQ:
1082fcf5ef2aSThomas Huth     case ARM_CPU_FIQ:
1083b36a32eaSJinjie Ruan     case ARM_CPU_NMI:
1084fcf5ef2aSThomas Huth         if (level) {
1085fcf5ef2aSThomas Huth             cpu_interrupt(cs, mask[irq]);
1086fcf5ef2aSThomas Huth         } else {
1087fcf5ef2aSThomas Huth             cpu_reset_interrupt(cs, mask[irq]);
1088fcf5ef2aSThomas Huth         }
1089fcf5ef2aSThomas Huth         break;
1090fcf5ef2aSThomas Huth     default:
1091fcf5ef2aSThomas Huth         g_assert_not_reached();
1092fcf5ef2aSThomas Huth     }
1093fcf5ef2aSThomas Huth }
1094fcf5ef2aSThomas Huth 
1095fcf5ef2aSThomas Huth static void arm_cpu_kvm_set_irq(void *opaque, int irq, int level)
1096fcf5ef2aSThomas Huth {
1097fcf5ef2aSThomas Huth #ifdef CONFIG_KVM
1098fcf5ef2aSThomas Huth     ARMCPU *cpu = opaque;
1099ed89f078SPeter Maydell     CPUARMState *env = &cpu->env;
1100fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
1101ed89f078SPeter Maydell     uint32_t linestate_bit;
1102f6530926SEric Auger     int irq_id;
1103fcf5ef2aSThomas Huth 
1104fcf5ef2aSThomas Huth     switch (irq) {
1105fcf5ef2aSThomas Huth     case ARM_CPU_IRQ:
1106f6530926SEric Auger         irq_id = KVM_ARM_IRQ_CPU_IRQ;
1107ed89f078SPeter Maydell         linestate_bit = CPU_INTERRUPT_HARD;
1108fcf5ef2aSThomas Huth         break;
1109fcf5ef2aSThomas Huth     case ARM_CPU_FIQ:
1110f6530926SEric Auger         irq_id = KVM_ARM_IRQ_CPU_FIQ;
1111ed89f078SPeter Maydell         linestate_bit = CPU_INTERRUPT_FIQ;
1112fcf5ef2aSThomas Huth         break;
1113fcf5ef2aSThomas Huth     default:
1114fcf5ef2aSThomas Huth         g_assert_not_reached();
1115fcf5ef2aSThomas Huth     }
1116ed89f078SPeter Maydell 
1117ed89f078SPeter Maydell     if (level) {
1118ed89f078SPeter Maydell         env->irq_line_state |= linestate_bit;
1119ed89f078SPeter Maydell     } else {
1120ed89f078SPeter Maydell         env->irq_line_state &= ~linestate_bit;
1121ed89f078SPeter Maydell     }
1122f6530926SEric Auger     kvm_arm_set_irq(cs->cpu_index, KVM_ARM_IRQ_TYPE_CPU, irq_id, !!level);
1123fcf5ef2aSThomas Huth #endif
1124fcf5ef2aSThomas Huth }
1125fcf5ef2aSThomas Huth 
1126fcf5ef2aSThomas Huth static bool arm_cpu_virtio_is_big_endian(CPUState *cs)
1127fcf5ef2aSThomas Huth {
1128fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(cs);
1129fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
1130fcf5ef2aSThomas Huth 
1131fcf5ef2aSThomas Huth     cpu_synchronize_state(cs);
1132fcf5ef2aSThomas Huth     return arm_cpu_data_is_big_endian(env);
1133fcf5ef2aSThomas Huth }
1134fcf5ef2aSThomas Huth 
1135fcf5ef2aSThomas Huth #endif
1136fcf5ef2aSThomas Huth 
1137fcf5ef2aSThomas Huth static void arm_disas_set_info(CPUState *cpu, disassemble_info *info)
1138fcf5ef2aSThomas Huth {
1139fcf5ef2aSThomas Huth     ARMCPU *ac = ARM_CPU(cpu);
1140fcf5ef2aSThomas Huth     CPUARMState *env = &ac->env;
11417bcdbf51SRichard Henderson     bool sctlr_b;
1142fcf5ef2aSThomas Huth 
1143fcf5ef2aSThomas Huth     if (is_a64(env)) {
1144110f6c70SRichard Henderson         info->cap_arch = CS_ARCH_ARM64;
114515fa1a0aSRichard Henderson         info->cap_insn_unit = 4;
114615fa1a0aSRichard Henderson         info->cap_insn_split = 4;
1147110f6c70SRichard Henderson     } else {
1148110f6c70SRichard Henderson         int cap_mode;
1149110f6c70SRichard Henderson         if (env->thumb) {
115015fa1a0aSRichard Henderson             info->cap_insn_unit = 2;
115115fa1a0aSRichard Henderson             info->cap_insn_split = 4;
1152110f6c70SRichard Henderson             cap_mode = CS_MODE_THUMB;
1153fcf5ef2aSThomas Huth         } else {
115415fa1a0aSRichard Henderson             info->cap_insn_unit = 4;
115515fa1a0aSRichard Henderson             info->cap_insn_split = 4;
1156110f6c70SRichard Henderson             cap_mode = CS_MODE_ARM;
1157fcf5ef2aSThomas Huth         }
1158110f6c70SRichard Henderson         if (arm_feature(env, ARM_FEATURE_V8)) {
1159110f6c70SRichard Henderson             cap_mode |= CS_MODE_V8;
1160110f6c70SRichard Henderson         }
1161110f6c70SRichard Henderson         if (arm_feature(env, ARM_FEATURE_M)) {
1162110f6c70SRichard Henderson             cap_mode |= CS_MODE_MCLASS;
1163110f6c70SRichard Henderson         }
1164110f6c70SRichard Henderson         info->cap_arch = CS_ARCH_ARM;
1165110f6c70SRichard Henderson         info->cap_mode = cap_mode;
1166fcf5ef2aSThomas Huth     }
11677bcdbf51SRichard Henderson 
11687bcdbf51SRichard Henderson     sctlr_b = arm_sctlr_b(env);
11697bcdbf51SRichard Henderson     if (bswap_code(sctlr_b)) {
1170ee3eb3a7SMarc-André Lureau #if TARGET_BIG_ENDIAN
1171fcf5ef2aSThomas Huth         info->endian = BFD_ENDIAN_LITTLE;
1172fcf5ef2aSThomas Huth #else
1173fcf5ef2aSThomas Huth         info->endian = BFD_ENDIAN_BIG;
1174fcf5ef2aSThomas Huth #endif
1175fcf5ef2aSThomas Huth     }
1176f7478a92SJulian Brown     info->flags &= ~INSN_ARM_BE32;
11777bcdbf51SRichard Henderson #ifndef CONFIG_USER_ONLY
11787bcdbf51SRichard Henderson     if (sctlr_b) {
1179f7478a92SJulian Brown         info->flags |= INSN_ARM_BE32;
1180f7478a92SJulian Brown     }
11817bcdbf51SRichard Henderson #endif
1182fcf5ef2aSThomas Huth }
1183fcf5ef2aSThomas Huth 
118486480615SPhilippe Mathieu-Daudé #ifdef TARGET_AARCH64
118586480615SPhilippe Mathieu-Daudé 
118686480615SPhilippe Mathieu-Daudé static void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags)
118786480615SPhilippe Mathieu-Daudé {
118886480615SPhilippe Mathieu-Daudé     ARMCPU *cpu = ARM_CPU(cs);
118986480615SPhilippe Mathieu-Daudé     CPUARMState *env = &cpu->env;
119086480615SPhilippe Mathieu-Daudé     uint32_t psr = pstate_read(env);
1191a9d84070SRichard Henderson     int i, j;
119286480615SPhilippe Mathieu-Daudé     int el = arm_current_el(env);
1193bde0e60bSPeter Maydell     uint64_t hcr = arm_hcr_el2_eff(env);
119486480615SPhilippe Mathieu-Daudé     const char *ns_status;
11957a867dd5SRichard Henderson     bool sve;
119686480615SPhilippe Mathieu-Daudé 
119786480615SPhilippe Mathieu-Daudé     qemu_fprintf(f, " PC=%016" PRIx64 " ", env->pc);
119886480615SPhilippe Mathieu-Daudé     for (i = 0; i < 32; i++) {
119986480615SPhilippe Mathieu-Daudé         if (i == 31) {
120086480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, " SP=%016" PRIx64 "\n", env->xregs[i]);
120186480615SPhilippe Mathieu-Daudé         } else {
120286480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "X%02d=%016" PRIx64 "%s", i, env->xregs[i],
120386480615SPhilippe Mathieu-Daudé                          (i + 2) % 3 ? " " : "\n");
120486480615SPhilippe Mathieu-Daudé         }
120586480615SPhilippe Mathieu-Daudé     }
120686480615SPhilippe Mathieu-Daudé 
120786480615SPhilippe Mathieu-Daudé     if (arm_feature(env, ARM_FEATURE_EL3) && el != 3) {
120886480615SPhilippe Mathieu-Daudé         ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
120986480615SPhilippe Mathieu-Daudé     } else {
121086480615SPhilippe Mathieu-Daudé         ns_status = "";
121186480615SPhilippe Mathieu-Daudé     }
121286480615SPhilippe Mathieu-Daudé     qemu_fprintf(f, "PSTATE=%08x %c%c%c%c %sEL%d%c",
121386480615SPhilippe Mathieu-Daudé                  psr,
121486480615SPhilippe Mathieu-Daudé                  psr & PSTATE_N ? 'N' : '-',
121586480615SPhilippe Mathieu-Daudé                  psr & PSTATE_Z ? 'Z' : '-',
121686480615SPhilippe Mathieu-Daudé                  psr & PSTATE_C ? 'C' : '-',
121786480615SPhilippe Mathieu-Daudé                  psr & PSTATE_V ? 'V' : '-',
121886480615SPhilippe Mathieu-Daudé                  ns_status,
121986480615SPhilippe Mathieu-Daudé                  el,
122086480615SPhilippe Mathieu-Daudé                  psr & PSTATE_SP ? 'h' : 't');
122186480615SPhilippe Mathieu-Daudé 
12227a867dd5SRichard Henderson     if (cpu_isar_feature(aa64_sme, cpu)) {
12237a867dd5SRichard Henderson         qemu_fprintf(f, "  SVCR=%08" PRIx64 " %c%c",
12247a867dd5SRichard Henderson                      env->svcr,
12257a867dd5SRichard Henderson                      (FIELD_EX64(env->svcr, SVCR, ZA) ? 'Z' : '-'),
12267a867dd5SRichard Henderson                      (FIELD_EX64(env->svcr, SVCR, SM) ? 'S' : '-'));
12277a867dd5SRichard Henderson     }
122886480615SPhilippe Mathieu-Daudé     if (cpu_isar_feature(aa64_bti, cpu)) {
122986480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "  BTYPE=%d", (psr & PSTATE_BTYPE) >> 10);
123086480615SPhilippe Mathieu-Daudé     }
1231bde0e60bSPeter Maydell     qemu_fprintf(f, "%s%s%s",
1232bde0e60bSPeter Maydell                  (hcr & HCR_NV) ? " NV" : "",
1233bde0e60bSPeter Maydell                  (hcr & HCR_NV1) ? " NV1" : "",
1234bde0e60bSPeter Maydell                  (hcr & HCR_NV2) ? " NV2" : "");
123586480615SPhilippe Mathieu-Daudé     if (!(flags & CPU_DUMP_FPU)) {
123686480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "\n");
123786480615SPhilippe Mathieu-Daudé         return;
123886480615SPhilippe Mathieu-Daudé     }
123986480615SPhilippe Mathieu-Daudé     if (fp_exception_el(env, el) != 0) {
124086480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "    FPU disabled\n");
124186480615SPhilippe Mathieu-Daudé         return;
124286480615SPhilippe Mathieu-Daudé     }
124386480615SPhilippe Mathieu-Daudé     qemu_fprintf(f, "     FPCR=%08x FPSR=%08x\n",
124486480615SPhilippe Mathieu-Daudé                  vfp_get_fpcr(env), vfp_get_fpsr(env));
124586480615SPhilippe Mathieu-Daudé 
12467a867dd5SRichard Henderson     if (cpu_isar_feature(aa64_sme, cpu) && FIELD_EX64(env->svcr, SVCR, SM)) {
12477a867dd5SRichard Henderson         sve = sme_exception_el(env, el) == 0;
12487a867dd5SRichard Henderson     } else if (cpu_isar_feature(aa64_sve, cpu)) {
12497a867dd5SRichard Henderson         sve = sve_exception_el(env, el) == 0;
12507a867dd5SRichard Henderson     } else {
12517a867dd5SRichard Henderson         sve = false;
12527a867dd5SRichard Henderson     }
12537a867dd5SRichard Henderson 
12547a867dd5SRichard Henderson     if (sve) {
1255a9d84070SRichard Henderson         int zcr_len = sve_vqm1_for_el(env, el);
125686480615SPhilippe Mathieu-Daudé 
125786480615SPhilippe Mathieu-Daudé         for (i = 0; i <= FFR_PRED_NUM; i++) {
125886480615SPhilippe Mathieu-Daudé             bool eol;
125986480615SPhilippe Mathieu-Daudé             if (i == FFR_PRED_NUM) {
126086480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "FFR=");
126186480615SPhilippe Mathieu-Daudé                 /* It's last, so end the line.  */
126286480615SPhilippe Mathieu-Daudé                 eol = true;
126386480615SPhilippe Mathieu-Daudé             } else {
126486480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "P%02d=", i);
126586480615SPhilippe Mathieu-Daudé                 switch (zcr_len) {
126686480615SPhilippe Mathieu-Daudé                 case 0:
126786480615SPhilippe Mathieu-Daudé                     eol = i % 8 == 7;
126886480615SPhilippe Mathieu-Daudé                     break;
126986480615SPhilippe Mathieu-Daudé                 case 1:
127086480615SPhilippe Mathieu-Daudé                     eol = i % 6 == 5;
127186480615SPhilippe Mathieu-Daudé                     break;
127286480615SPhilippe Mathieu-Daudé                 case 2:
127386480615SPhilippe Mathieu-Daudé                 case 3:
127486480615SPhilippe Mathieu-Daudé                     eol = i % 3 == 2;
127586480615SPhilippe Mathieu-Daudé                     break;
127686480615SPhilippe Mathieu-Daudé                 default:
127786480615SPhilippe Mathieu-Daudé                     /* More than one quadword per predicate.  */
127886480615SPhilippe Mathieu-Daudé                     eol = true;
127986480615SPhilippe Mathieu-Daudé                     break;
128086480615SPhilippe Mathieu-Daudé                 }
128186480615SPhilippe Mathieu-Daudé             }
128286480615SPhilippe Mathieu-Daudé             for (j = zcr_len / 4; j >= 0; j--) {
128386480615SPhilippe Mathieu-Daudé                 int digits;
128486480615SPhilippe Mathieu-Daudé                 if (j * 4 + 4 <= zcr_len + 1) {
128586480615SPhilippe Mathieu-Daudé                     digits = 16;
128686480615SPhilippe Mathieu-Daudé                 } else {
128786480615SPhilippe Mathieu-Daudé                     digits = (zcr_len % 4 + 1) * 4;
128886480615SPhilippe Mathieu-Daudé                 }
128986480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "%0*" PRIx64 "%s", digits,
129086480615SPhilippe Mathieu-Daudé                              env->vfp.pregs[i].p[j],
129186480615SPhilippe Mathieu-Daudé                              j ? ":" : eol ? "\n" : " ");
129286480615SPhilippe Mathieu-Daudé             }
129386480615SPhilippe Mathieu-Daudé         }
129486480615SPhilippe Mathieu-Daudé 
129586480615SPhilippe Mathieu-Daudé         if (zcr_len == 0) {
1296a9d84070SRichard Henderson             /*
1297a9d84070SRichard Henderson              * With vl=16, there are only 37 columns per register,
1298a9d84070SRichard Henderson              * so output two registers per line.
1299a9d84070SRichard Henderson              */
1300a9d84070SRichard Henderson             for (i = 0; i < 32; i++) {
130186480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "Z%02d=%016" PRIx64 ":%016" PRIx64 "%s",
130286480615SPhilippe Mathieu-Daudé                              i, env->vfp.zregs[i].d[1],
130386480615SPhilippe Mathieu-Daudé                              env->vfp.zregs[i].d[0], i & 1 ? "\n" : " ");
1304a9d84070SRichard Henderson             }
130586480615SPhilippe Mathieu-Daudé         } else {
1306a9d84070SRichard Henderson             for (i = 0; i < 32; i++) {
1307a9d84070SRichard Henderson                 qemu_fprintf(f, "Z%02d=", i);
130886480615SPhilippe Mathieu-Daudé                 for (j = zcr_len; j >= 0; j--) {
130986480615SPhilippe Mathieu-Daudé                     qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%s",
131086480615SPhilippe Mathieu-Daudé                                  env->vfp.zregs[i].d[j * 2 + 1],
1311a9d84070SRichard Henderson                                  env->vfp.zregs[i].d[j * 2 + 0],
1312a9d84070SRichard Henderson                                  j ? ":" : "\n");
131386480615SPhilippe Mathieu-Daudé                 }
131486480615SPhilippe Mathieu-Daudé             }
131586480615SPhilippe Mathieu-Daudé         }
131686480615SPhilippe Mathieu-Daudé     } else {
131786480615SPhilippe Mathieu-Daudé         for (i = 0; i < 32; i++) {
131886480615SPhilippe Mathieu-Daudé             uint64_t *q = aa64_vfp_qreg(env, i);
131986480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "Q%02d=%016" PRIx64 ":%016" PRIx64 "%s",
132086480615SPhilippe Mathieu-Daudé                          i, q[1], q[0], (i & 1 ? "\n" : " "));
132186480615SPhilippe Mathieu-Daudé         }
132286480615SPhilippe Mathieu-Daudé     }
1323270bea47SRichard Henderson 
1324270bea47SRichard Henderson     if (cpu_isar_feature(aa64_sme, cpu) &&
1325270bea47SRichard Henderson         FIELD_EX64(env->svcr, SVCR, ZA) &&
1326270bea47SRichard Henderson         sme_exception_el(env, el) == 0) {
1327270bea47SRichard Henderson         int zcr_len = sve_vqm1_for_el_sm(env, el, true);
1328270bea47SRichard Henderson         int svl = (zcr_len + 1) * 16;
1329270bea47SRichard Henderson         int svl_lg10 = svl < 100 ? 2 : 3;
1330270bea47SRichard Henderson 
1331270bea47SRichard Henderson         for (i = 0; i < svl; i++) {
1332270bea47SRichard Henderson             qemu_fprintf(f, "ZA[%0*d]=", svl_lg10, i);
1333270bea47SRichard Henderson             for (j = zcr_len; j >= 0; --j) {
1334270bea47SRichard Henderson                 qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%c",
1335270bea47SRichard Henderson                              env->zarray[i].d[2 * j + 1],
1336270bea47SRichard Henderson                              env->zarray[i].d[2 * j],
1337270bea47SRichard Henderson                              j ? ':' : '\n');
1338270bea47SRichard Henderson             }
1339270bea47SRichard Henderson         }
1340270bea47SRichard Henderson     }
134186480615SPhilippe Mathieu-Daudé }
134286480615SPhilippe Mathieu-Daudé 
134386480615SPhilippe Mathieu-Daudé #else
134486480615SPhilippe Mathieu-Daudé 
134586480615SPhilippe Mathieu-Daudé static inline void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags)
134686480615SPhilippe Mathieu-Daudé {
134786480615SPhilippe Mathieu-Daudé     g_assert_not_reached();
134886480615SPhilippe Mathieu-Daudé }
134986480615SPhilippe Mathieu-Daudé 
135086480615SPhilippe Mathieu-Daudé #endif
135186480615SPhilippe Mathieu-Daudé 
135286480615SPhilippe Mathieu-Daudé static void arm_cpu_dump_state(CPUState *cs, FILE *f, int flags)
135386480615SPhilippe Mathieu-Daudé {
135486480615SPhilippe Mathieu-Daudé     ARMCPU *cpu = ARM_CPU(cs);
135586480615SPhilippe Mathieu-Daudé     CPUARMState *env = &cpu->env;
135686480615SPhilippe Mathieu-Daudé     int i;
135786480615SPhilippe Mathieu-Daudé 
135886480615SPhilippe Mathieu-Daudé     if (is_a64(env)) {
135986480615SPhilippe Mathieu-Daudé         aarch64_cpu_dump_state(cs, f, flags);
136086480615SPhilippe Mathieu-Daudé         return;
136186480615SPhilippe Mathieu-Daudé     }
136286480615SPhilippe Mathieu-Daudé 
136386480615SPhilippe Mathieu-Daudé     for (i = 0; i < 16; i++) {
136486480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "R%02d=%08x", i, env->regs[i]);
136586480615SPhilippe Mathieu-Daudé         if ((i % 4) == 3) {
136686480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "\n");
136786480615SPhilippe Mathieu-Daudé         } else {
136886480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, " ");
136986480615SPhilippe Mathieu-Daudé         }
137086480615SPhilippe Mathieu-Daudé     }
137186480615SPhilippe Mathieu-Daudé 
137286480615SPhilippe Mathieu-Daudé     if (arm_feature(env, ARM_FEATURE_M)) {
137386480615SPhilippe Mathieu-Daudé         uint32_t xpsr = xpsr_read(env);
137486480615SPhilippe Mathieu-Daudé         const char *mode;
137586480615SPhilippe Mathieu-Daudé         const char *ns_status = "";
137686480615SPhilippe Mathieu-Daudé 
137786480615SPhilippe Mathieu-Daudé         if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
137886480615SPhilippe Mathieu-Daudé             ns_status = env->v7m.secure ? "S " : "NS ";
137986480615SPhilippe Mathieu-Daudé         }
138086480615SPhilippe Mathieu-Daudé 
138186480615SPhilippe Mathieu-Daudé         if (xpsr & XPSR_EXCP) {
138286480615SPhilippe Mathieu-Daudé             mode = "handler";
138386480615SPhilippe Mathieu-Daudé         } else {
138486480615SPhilippe Mathieu-Daudé             if (env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_NPRIV_MASK) {
138586480615SPhilippe Mathieu-Daudé                 mode = "unpriv-thread";
138686480615SPhilippe Mathieu-Daudé             } else {
138786480615SPhilippe Mathieu-Daudé                 mode = "priv-thread";
138886480615SPhilippe Mathieu-Daudé             }
138986480615SPhilippe Mathieu-Daudé         }
139086480615SPhilippe Mathieu-Daudé 
139186480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "XPSR=%08x %c%c%c%c %c %s%s\n",
139286480615SPhilippe Mathieu-Daudé                      xpsr,
139386480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_N ? 'N' : '-',
139486480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_Z ? 'Z' : '-',
139586480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_C ? 'C' : '-',
139686480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_V ? 'V' : '-',
139786480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_T ? 'T' : 'A',
139886480615SPhilippe Mathieu-Daudé                      ns_status,
139986480615SPhilippe Mathieu-Daudé                      mode);
140086480615SPhilippe Mathieu-Daudé     } else {
140186480615SPhilippe Mathieu-Daudé         uint32_t psr = cpsr_read(env);
140286480615SPhilippe Mathieu-Daudé         const char *ns_status = "";
140386480615SPhilippe Mathieu-Daudé 
140486480615SPhilippe Mathieu-Daudé         if (arm_feature(env, ARM_FEATURE_EL3) &&
140586480615SPhilippe Mathieu-Daudé             (psr & CPSR_M) != ARM_CPU_MODE_MON) {
140686480615SPhilippe Mathieu-Daudé             ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
140786480615SPhilippe Mathieu-Daudé         }
140886480615SPhilippe Mathieu-Daudé 
140986480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "PSR=%08x %c%c%c%c %c %s%s%d\n",
141086480615SPhilippe Mathieu-Daudé                      psr,
141186480615SPhilippe Mathieu-Daudé                      psr & CPSR_N ? 'N' : '-',
141286480615SPhilippe Mathieu-Daudé                      psr & CPSR_Z ? 'Z' : '-',
141386480615SPhilippe Mathieu-Daudé                      psr & CPSR_C ? 'C' : '-',
141486480615SPhilippe Mathieu-Daudé                      psr & CPSR_V ? 'V' : '-',
141586480615SPhilippe Mathieu-Daudé                      psr & CPSR_T ? 'T' : 'A',
141686480615SPhilippe Mathieu-Daudé                      ns_status,
141786480615SPhilippe Mathieu-Daudé                      aarch32_mode_name(psr), (psr & 0x10) ? 32 : 26);
141886480615SPhilippe Mathieu-Daudé     }
141986480615SPhilippe Mathieu-Daudé 
142086480615SPhilippe Mathieu-Daudé     if (flags & CPU_DUMP_FPU) {
142186480615SPhilippe Mathieu-Daudé         int numvfpregs = 0;
1422a6627f5fSRichard Henderson         if (cpu_isar_feature(aa32_simd_r32, cpu)) {
1423a6627f5fSRichard Henderson             numvfpregs = 32;
14247fbc6a40SRichard Henderson         } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
1425a6627f5fSRichard Henderson             numvfpregs = 16;
142686480615SPhilippe Mathieu-Daudé         }
142786480615SPhilippe Mathieu-Daudé         for (i = 0; i < numvfpregs; i++) {
142886480615SPhilippe Mathieu-Daudé             uint64_t v = *aa32_vfp_dreg(env, i);
142986480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "s%02d=%08x s%02d=%08x d%02d=%016" PRIx64 "\n",
143086480615SPhilippe Mathieu-Daudé                          i * 2, (uint32_t)v,
143186480615SPhilippe Mathieu-Daudé                          i * 2 + 1, (uint32_t)(v >> 32),
143286480615SPhilippe Mathieu-Daudé                          i, v);
143386480615SPhilippe Mathieu-Daudé         }
143486480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "FPSCR: %08x\n", vfp_get_fpscr(env));
1435aa291908SPeter Maydell         if (cpu_isar_feature(aa32_mve, cpu)) {
1436aa291908SPeter Maydell             qemu_fprintf(f, "VPR: %08x\n", env->v7m.vpr);
1437aa291908SPeter Maydell         }
143886480615SPhilippe Mathieu-Daudé     }
143986480615SPhilippe Mathieu-Daudé }
144086480615SPhilippe Mathieu-Daudé 
1441750245edSRichard Henderson uint64_t arm_build_mp_affinity(int idx, uint8_t clustersz)
144246de5913SIgor Mammedov {
144346de5913SIgor Mammedov     uint32_t Aff1 = idx / clustersz;
144446de5913SIgor Mammedov     uint32_t Aff0 = idx % clustersz;
144546de5913SIgor Mammedov     return (Aff1 << ARM_AFF1_SHIFT) | Aff0;
144646de5913SIgor Mammedov }
144746de5913SIgor Mammedov 
1448e2d8cf9bSPhilippe Mathieu-Daudé uint64_t arm_cpu_mp_affinity(ARMCPU *cpu)
1449e2d8cf9bSPhilippe Mathieu-Daudé {
1450e2d8cf9bSPhilippe Mathieu-Daudé     return cpu->mp_affinity;
1451e2d8cf9bSPhilippe Mathieu-Daudé }
1452e2d8cf9bSPhilippe Mathieu-Daudé 
1453fcf5ef2aSThomas Huth static void arm_cpu_initfn(Object *obj)
1454fcf5ef2aSThomas Huth {
1455fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(obj);
1456fcf5ef2aSThomas Huth 
14575860362dSRichard Henderson     cpu->cp_regs = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1458c27f5d3aSRichard Henderson                                          NULL, g_free);
1459fcf5ef2aSThomas Huth 
1460b5c53d1bSAaron Lindsay     QLIST_INIT(&cpu->pre_el_change_hooks);
146108267487SAaron Lindsay     QLIST_INIT(&cpu->el_change_hooks);
146208267487SAaron Lindsay 
1463b3d52804SRichard Henderson #ifdef CONFIG_USER_ONLY
1464b3d52804SRichard Henderson # ifdef TARGET_AARCH64
1465b3d52804SRichard Henderson     /*
1466e74c0976SRichard Henderson      * The linux kernel defaults to 512-bit for SVE, and 256-bit for SME.
1467e74c0976SRichard Henderson      * These values were chosen to fit within the default signal frame.
1468e74c0976SRichard Henderson      * See documentation for /proc/sys/abi/{sve,sme}_default_vector_length,
1469e74c0976SRichard Henderson      * and our corresponding cpu property.
1470b3d52804SRichard Henderson      */
1471b3d52804SRichard Henderson     cpu->sve_default_vq = 4;
1472e74c0976SRichard Henderson     cpu->sme_default_vq = 2;
1473b3d52804SRichard Henderson # endif
1474b3d52804SRichard Henderson #else
1475fcf5ef2aSThomas Huth     /* Our inbound IRQ and FIQ lines */
1476fcf5ef2aSThomas Huth     if (kvm_enabled()) {
1477b36a32eaSJinjie Ruan         /*
1478b36a32eaSJinjie Ruan          * VIRQ, VFIQ, NMI, VINMI are unused with KVM but we add
1479b36a32eaSJinjie Ruan          * them to maintain the same interface as non-KVM CPUs.
1480fcf5ef2aSThomas Huth          */
1481b36a32eaSJinjie Ruan         qdev_init_gpio_in(DEVICE(cpu), arm_cpu_kvm_set_irq, 6);
1482fcf5ef2aSThomas Huth     } else {
1483b36a32eaSJinjie Ruan         qdev_init_gpio_in(DEVICE(cpu), arm_cpu_set_irq, 6);
1484fcf5ef2aSThomas Huth     }
1485fcf5ef2aSThomas Huth 
1486fcf5ef2aSThomas Huth     qdev_init_gpio_out(DEVICE(cpu), cpu->gt_timer_outputs,
1487fcf5ef2aSThomas Huth                        ARRAY_SIZE(cpu->gt_timer_outputs));
1488aa1b3111SPeter Maydell 
1489aa1b3111SPeter Maydell     qdev_init_gpio_out_named(DEVICE(cpu), &cpu->gicv3_maintenance_interrupt,
1490aa1b3111SPeter Maydell                              "gicv3-maintenance-interrupt", 1);
149107f48730SAndrew Jones     qdev_init_gpio_out_named(DEVICE(cpu), &cpu->pmu_interrupt,
149207f48730SAndrew Jones                              "pmu-interrupt", 1);
1493fcf5ef2aSThomas Huth #endif
1494fcf5ef2aSThomas Huth 
1495fcf5ef2aSThomas Huth     /* DTB consumers generally don't in fact care what the 'compatible'
1496fcf5ef2aSThomas Huth      * string is, so always provide some string and trust that a hypothetical
1497fcf5ef2aSThomas Huth      * picky DTB consumer will also provide a helpful error message.
1498fcf5ef2aSThomas Huth      */
1499fcf5ef2aSThomas Huth     cpu->dtb_compatible = "qemu,unknown";
15000dc71c70SAkihiko Odaki     cpu->psci_version = QEMU_PSCI_VERSION_0_1; /* By default assume PSCI v0.1 */
1501fcf5ef2aSThomas Huth     cpu->kvm_target = QEMU_KVM_ARM_TARGET_NONE;
1502fcf5ef2aSThomas Huth 
15032c9c0bf9SAlexander Graf     if (tcg_enabled() || hvf_enabled()) {
15040dc71c70SAkihiko Odaki         /* TCG and HVF implement PSCI 1.1 */
15050dc71c70SAkihiko Odaki         cpu->psci_version = QEMU_PSCI_VERSION_1_1;
1506fcf5ef2aSThomas Huth     }
1507fcf5ef2aSThomas Huth }
1508fcf5ef2aSThomas Huth 
1509*bd8e9ddfSPeter Maydell /*
1510*bd8e9ddfSPeter Maydell  * 0 means "unset, use the default value". That default might vary depending
1511*bd8e9ddfSPeter Maydell  * on the CPU type, and is set in the realize fn.
1512*bd8e9ddfSPeter Maydell  */
151396eec6b2SAndrew Jeffery static Property arm_cpu_gt_cntfrq_property =
1514*bd8e9ddfSPeter Maydell             DEFINE_PROP_UINT64("cntfrq", ARMCPU, gt_cntfrq_hz, 0);
151596eec6b2SAndrew Jeffery 
1516fcf5ef2aSThomas Huth static Property arm_cpu_reset_cbar_property =
1517fcf5ef2aSThomas Huth             DEFINE_PROP_UINT64("reset-cbar", ARMCPU, reset_cbar, 0);
1518fcf5ef2aSThomas Huth 
1519fcf5ef2aSThomas Huth static Property arm_cpu_reset_hivecs_property =
1520fcf5ef2aSThomas Huth             DEFINE_PROP_BOOL("reset-hivecs", ARMCPU, reset_hivecs, false);
1521fcf5ef2aSThomas Huth 
152245ca3a14SRichard Henderson #ifndef CONFIG_USER_ONLY
1523c25bd18aSPeter Maydell static Property arm_cpu_has_el2_property =
1524c25bd18aSPeter Maydell             DEFINE_PROP_BOOL("has_el2", ARMCPU, has_el2, true);
1525c25bd18aSPeter Maydell 
1526fcf5ef2aSThomas Huth static Property arm_cpu_has_el3_property =
1527fcf5ef2aSThomas Huth             DEFINE_PROP_BOOL("has_el3", ARMCPU, has_el3, true);
152845ca3a14SRichard Henderson #endif
1529fcf5ef2aSThomas Huth 
15303a062d57SJulian Brown static Property arm_cpu_cfgend_property =
15313a062d57SJulian Brown             DEFINE_PROP_BOOL("cfgend", ARMCPU, cfgend, false);
15323a062d57SJulian Brown 
153397a28b0eSPeter Maydell static Property arm_cpu_has_vfp_property =
153497a28b0eSPeter Maydell             DEFINE_PROP_BOOL("vfp", ARMCPU, has_vfp, true);
153597a28b0eSPeter Maydell 
153642bea956SCédric Le Goater static Property arm_cpu_has_vfp_d32_property =
153742bea956SCédric Le Goater             DEFINE_PROP_BOOL("vfp-d32", ARMCPU, has_vfp_d32, true);
153842bea956SCédric Le Goater 
153997a28b0eSPeter Maydell static Property arm_cpu_has_neon_property =
154097a28b0eSPeter Maydell             DEFINE_PROP_BOOL("neon", ARMCPU, has_neon, true);
154197a28b0eSPeter Maydell 
1542ea90db0aSPeter Maydell static Property arm_cpu_has_dsp_property =
1543ea90db0aSPeter Maydell             DEFINE_PROP_BOOL("dsp", ARMCPU, has_dsp, true);
1544ea90db0aSPeter Maydell 
1545fcf5ef2aSThomas Huth static Property arm_cpu_has_mpu_property =
1546fcf5ef2aSThomas Huth             DEFINE_PROP_BOOL("has-mpu", ARMCPU, has_mpu, true);
1547fcf5ef2aSThomas Huth 
15488d92e26bSPeter Maydell /* This is like DEFINE_PROP_UINT32 but it doesn't set the default value,
15498d92e26bSPeter Maydell  * because the CPU initfn will have already set cpu->pmsav7_dregion to
15508d92e26bSPeter Maydell  * the right value for that particular CPU type, and we don't want
15518d92e26bSPeter Maydell  * to override that with an incorrect constant value.
15528d92e26bSPeter Maydell  */
1553fcf5ef2aSThomas Huth static Property arm_cpu_pmsav7_dregion_property =
15548d92e26bSPeter Maydell             DEFINE_PROP_UNSIGNED_NODEFAULT("pmsav7-dregion", ARMCPU,
15558d92e26bSPeter Maydell                                            pmsav7_dregion,
15568d92e26bSPeter Maydell                                            qdev_prop_uint32, uint32_t);
1557fcf5ef2aSThomas Huth 
1558ae502508SAndrew Jones static bool arm_get_pmu(Object *obj, Error **errp)
1559ae502508SAndrew Jones {
1560ae502508SAndrew Jones     ARMCPU *cpu = ARM_CPU(obj);
1561ae502508SAndrew Jones 
1562ae502508SAndrew Jones     return cpu->has_pmu;
1563ae502508SAndrew Jones }
1564ae502508SAndrew Jones 
1565ae502508SAndrew Jones static void arm_set_pmu(Object *obj, bool value, Error **errp)
1566ae502508SAndrew Jones {
1567ae502508SAndrew Jones     ARMCPU *cpu = ARM_CPU(obj);
1568ae502508SAndrew Jones 
1569ae502508SAndrew Jones     if (value) {
15707d20e681SPhilippe Mathieu-Daudé         if (kvm_enabled() && !kvm_arm_pmu_supported()) {
1571ae502508SAndrew Jones             error_setg(errp, "'pmu' feature not supported by KVM on this host");
1572ae502508SAndrew Jones             return;
1573ae502508SAndrew Jones         }
1574ae502508SAndrew Jones         set_feature(&cpu->env, ARM_FEATURE_PMU);
1575ae502508SAndrew Jones     } else {
1576ae502508SAndrew Jones         unset_feature(&cpu->env, ARM_FEATURE_PMU);
1577ae502508SAndrew Jones     }
1578ae502508SAndrew Jones     cpu->has_pmu = value;
1579ae502508SAndrew Jones }
1580ae502508SAndrew Jones 
15817def8754SAndrew Jeffery unsigned int gt_cntfrq_period_ns(ARMCPU *cpu)
15827def8754SAndrew Jeffery {
158396eec6b2SAndrew Jeffery     /*
158496eec6b2SAndrew Jeffery      * The exact approach to calculating guest ticks is:
158596eec6b2SAndrew Jeffery      *
158696eec6b2SAndrew Jeffery      *     muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), cpu->gt_cntfrq_hz,
158796eec6b2SAndrew Jeffery      *              NANOSECONDS_PER_SECOND);
158896eec6b2SAndrew Jeffery      *
158996eec6b2SAndrew Jeffery      * We don't do that. Rather we intentionally use integer division
159096eec6b2SAndrew Jeffery      * truncation below and in the caller for the conversion of host monotonic
159196eec6b2SAndrew Jeffery      * time to guest ticks to provide the exact inverse for the semantics of
159296eec6b2SAndrew Jeffery      * the QEMUTimer scale factor. QEMUTimer's scale facter is an integer, so
159396eec6b2SAndrew Jeffery      * it loses precision when representing frequencies where
159496eec6b2SAndrew Jeffery      * `(NANOSECONDS_PER_SECOND % cpu->gt_cntfrq) > 0` holds. Failing to
159596eec6b2SAndrew Jeffery      * provide an exact inverse leads to scheduling timers with negative
159696eec6b2SAndrew Jeffery      * periods, which in turn leads to sticky behaviour in the guest.
159796eec6b2SAndrew Jeffery      *
159896eec6b2SAndrew Jeffery      * Finally, CNTFRQ is effectively capped at 1GHz to ensure our scale factor
159996eec6b2SAndrew Jeffery      * cannot become zero.
160096eec6b2SAndrew Jeffery      */
16017def8754SAndrew Jeffery     return NANOSECONDS_PER_SECOND > cpu->gt_cntfrq_hz ?
16027def8754SAndrew Jeffery       NANOSECONDS_PER_SECOND / cpu->gt_cntfrq_hz : 1;
16037def8754SAndrew Jeffery }
16047def8754SAndrew Jeffery 
1605b8f7959fSPeter Maydell static void arm_cpu_propagate_feature_implications(ARMCPU *cpu)
1606b8f7959fSPeter Maydell {
1607b8f7959fSPeter Maydell     CPUARMState *env = &cpu->env;
1608b8f7959fSPeter Maydell     bool no_aa32 = false;
1609b8f7959fSPeter Maydell 
1610b8f7959fSPeter Maydell     /*
1611b8f7959fSPeter Maydell      * Some features automatically imply others: set the feature
1612b8f7959fSPeter Maydell      * bits explicitly for these cases.
1613b8f7959fSPeter Maydell      */
1614b8f7959fSPeter Maydell 
1615b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_M)) {
1616b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_PMSA);
1617b8f7959fSPeter Maydell     }
1618b8f7959fSPeter Maydell 
1619b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V8)) {
1620b8f7959fSPeter Maydell         if (arm_feature(env, ARM_FEATURE_M)) {
1621b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_V7);
1622b8f7959fSPeter Maydell         } else {
1623b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_V7VE);
1624b8f7959fSPeter Maydell         }
1625b8f7959fSPeter Maydell     }
1626b8f7959fSPeter Maydell 
1627b8f7959fSPeter Maydell     /*
1628b8f7959fSPeter Maydell      * There exist AArch64 cpus without AArch32 support.  When KVM
1629b8f7959fSPeter Maydell      * queries ID_ISAR0_EL1 on such a host, the value is UNKNOWN.
1630b8f7959fSPeter Maydell      * Similarly, we cannot check ID_AA64PFR0 without AArch64 support.
1631b8f7959fSPeter Maydell      * As a general principle, we also do not make ID register
1632b8f7959fSPeter Maydell      * consistency checks anywhere unless using TCG, because only
1633b8f7959fSPeter Maydell      * for TCG would a consistency-check failure be a QEMU bug.
1634b8f7959fSPeter Maydell      */
1635b8f7959fSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
1636b8f7959fSPeter Maydell         no_aa32 = !cpu_isar_feature(aa64_aa32, cpu);
1637b8f7959fSPeter Maydell     }
1638b8f7959fSPeter Maydell 
1639b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V7VE)) {
1640b8f7959fSPeter Maydell         /*
1641b8f7959fSPeter Maydell          * v7 Virtualization Extensions. In real hardware this implies
1642b8f7959fSPeter Maydell          * EL2 and also the presence of the Security Extensions.
1643b8f7959fSPeter Maydell          * For QEMU, for backwards-compatibility we implement some
1644b8f7959fSPeter Maydell          * CPUs or CPU configs which have no actual EL2 or EL3 but do
1645b8f7959fSPeter Maydell          * include the various other features that V7VE implies.
1646b8f7959fSPeter Maydell          * Presence of EL2 itself is ARM_FEATURE_EL2, and of the
1647b8f7959fSPeter Maydell          * Security Extensions is ARM_FEATURE_EL3.
1648b8f7959fSPeter Maydell          */
1649b8f7959fSPeter Maydell         assert(!tcg_enabled() || no_aa32 ||
1650b8f7959fSPeter Maydell                cpu_isar_feature(aa32_arm_div, cpu));
1651b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_LPAE);
1652b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V7);
1653b8f7959fSPeter Maydell     }
1654b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V7)) {
1655b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_VAPA);
1656b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_THUMB2);
1657b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_MPIDR);
1658b8f7959fSPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
1659b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_V6K);
1660b8f7959fSPeter Maydell         } else {
1661b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_V6);
1662b8f7959fSPeter Maydell         }
1663b8f7959fSPeter Maydell 
1664b8f7959fSPeter Maydell         /*
1665b8f7959fSPeter Maydell          * Always define VBAR for V7 CPUs even if it doesn't exist in
1666b8f7959fSPeter Maydell          * non-EL3 configs. This is needed by some legacy boards.
1667b8f7959fSPeter Maydell          */
1668b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_VBAR);
1669b8f7959fSPeter Maydell     }
1670b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V6K)) {
1671b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V6);
1672b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_MVFR);
1673b8f7959fSPeter Maydell     }
1674b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V6)) {
1675b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V5);
1676b8f7959fSPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
1677b8f7959fSPeter Maydell             assert(!tcg_enabled() || no_aa32 ||
1678b8f7959fSPeter Maydell                    cpu_isar_feature(aa32_jazelle, cpu));
1679b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_AUXCR);
1680b8f7959fSPeter Maydell         }
1681b8f7959fSPeter Maydell     }
1682b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V5)) {
1683b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V4T);
1684b8f7959fSPeter Maydell     }
1685b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_LPAE)) {
1686b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V7MP);
1687b8f7959fSPeter Maydell     }
1688b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
1689b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_CBAR);
1690b8f7959fSPeter Maydell     }
1691b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_THUMB2) &&
1692b8f7959fSPeter Maydell         !arm_feature(env, ARM_FEATURE_M)) {
1693b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_THUMB_DSP);
1694b8f7959fSPeter Maydell     }
1695b8f7959fSPeter Maydell }
1696b8f7959fSPeter Maydell 
169751e5ef45SMarc-André Lureau void arm_cpu_post_init(Object *obj)
1698fcf5ef2aSThomas Huth {
1699fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(obj);
1700fcf5ef2aSThomas Huth 
1701b8f7959fSPeter Maydell     /*
1702b8f7959fSPeter Maydell      * Some features imply others. Figure this out now, because we
1703b8f7959fSPeter Maydell      * are going to look at the feature bits in deciding which
1704b8f7959fSPeter Maydell      * properties to add.
1705790a1150SPeter Maydell      */
1706b8f7959fSPeter Maydell     arm_cpu_propagate_feature_implications(cpu);
1707790a1150SPeter Maydell 
1708fcf5ef2aSThomas Huth     if (arm_feature(&cpu->env, ARM_FEATURE_CBAR) ||
1709fcf5ef2aSThomas Huth         arm_feature(&cpu->env, ARM_FEATURE_CBAR_RO)) {
171094d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_cbar_property);
1711fcf5ef2aSThomas Huth     }
1712fcf5ef2aSThomas Huth 
1713fcf5ef2aSThomas Huth     if (!arm_feature(&cpu->env, ARM_FEATURE_M)) {
171494d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_hivecs_property);
1715fcf5ef2aSThomas Huth     }
1716fcf5ef2aSThomas Huth 
1717910e4f24STobias Röhmel     if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
17184a7319b7SEdgar E. Iglesias         object_property_add_uint64_ptr(obj, "rvbar",
17194a7319b7SEdgar E. Iglesias                                        &cpu->rvbar_prop,
17204a7319b7SEdgar E. Iglesias                                        OBJ_PROP_FLAG_READWRITE);
1721fcf5ef2aSThomas Huth     }
1722fcf5ef2aSThomas Huth 
172345ca3a14SRichard Henderson #ifndef CONFIG_USER_ONLY
1724fcf5ef2aSThomas Huth     if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
1725fcf5ef2aSThomas Huth         /* Add the has_el3 state CPU property only if EL3 is allowed.  This will
1726fcf5ef2aSThomas Huth          * prevent "has_el3" from existing on CPUs which cannot support EL3.
1727fcf5ef2aSThomas Huth          */
172894d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el3_property);
1729fcf5ef2aSThomas Huth 
1730fcf5ef2aSThomas Huth         object_property_add_link(obj, "secure-memory",
1731fcf5ef2aSThomas Huth                                  TYPE_MEMORY_REGION,
1732fcf5ef2aSThomas Huth                                  (Object **)&cpu->secure_memory,
1733fcf5ef2aSThomas Huth                                  qdev_prop_allow_set_link_before_realize,
1734d2623129SMarkus Armbruster                                  OBJ_PROP_LINK_STRONG);
1735fcf5ef2aSThomas Huth     }
1736fcf5ef2aSThomas Huth 
1737c25bd18aSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_EL2)) {
173894d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el2_property);
1739c25bd18aSPeter Maydell     }
174045ca3a14SRichard Henderson #endif
1741c25bd18aSPeter Maydell 
1742fcf5ef2aSThomas Huth     if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) {
1743ae502508SAndrew Jones         cpu->has_pmu = true;
1744d2623129SMarkus Armbruster         object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu);
1745fcf5ef2aSThomas Huth     }
1746fcf5ef2aSThomas Huth 
174797a28b0eSPeter Maydell     /*
174897a28b0eSPeter Maydell      * Allow user to turn off VFP and Neon support, but only for TCG --
174997a28b0eSPeter Maydell      * KVM does not currently allow us to lie to the guest about its
175097a28b0eSPeter Maydell      * ID/feature registers, so the guest always sees what the host has.
175197a28b0eSPeter Maydell      */
17524315f7c6SRichard Henderson     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
17534315f7c6SRichard Henderson         if (cpu_isar_feature(aa64_fp_simd, cpu)) {
175497a28b0eSPeter Maydell             cpu->has_vfp = true;
175542bea956SCédric Le Goater             cpu->has_vfp_d32 = true;
17564315f7c6SRichard Henderson             if (tcg_enabled() || qtest_enabled()) {
17574315f7c6SRichard Henderson                 qdev_property_add_static(DEVICE(obj),
17584315f7c6SRichard Henderson                                          &arm_cpu_has_vfp_property);
17594315f7c6SRichard Henderson             }
17604315f7c6SRichard Henderson         }
17614315f7c6SRichard Henderson     } else if (cpu_isar_feature(aa32_vfp, cpu)) {
17624315f7c6SRichard Henderson         cpu->has_vfp = true;
1763185e3fdfSPeter Maydell         if (tcg_enabled() || qtest_enabled()) {
1764185e3fdfSPeter Maydell             qdev_property_add_static(DEVICE(obj),
1765185e3fdfSPeter Maydell                                      &arm_cpu_has_vfp_property);
1766185e3fdfSPeter Maydell         }
17674315f7c6SRichard Henderson         if (cpu_isar_feature(aa32_simd_r32, cpu)) {
17684315f7c6SRichard Henderson             cpu->has_vfp_d32 = true;
176942bea956SCédric Le Goater             /*
177042bea956SCédric Le Goater              * The permitted values of the SIMDReg bits [3:0] on
177142bea956SCédric Le Goater              * Armv8-A are either 0b0000 and 0b0010. On such CPUs,
177242bea956SCédric Le Goater              * make sure that has_vfp_d32 can not be set to false.
177342bea956SCédric Le Goater              */
17744315f7c6SRichard Henderson             if ((tcg_enabled() || qtest_enabled())
17754315f7c6SRichard Henderson                 && !(arm_feature(&cpu->env, ARM_FEATURE_V8)
17764315f7c6SRichard Henderson                      && !arm_feature(&cpu->env, ARM_FEATURE_M))) {
177742bea956SCédric Le Goater                 qdev_property_add_static(DEVICE(obj),
177842bea956SCédric Le Goater                                          &arm_cpu_has_vfp_d32_property);
177942bea956SCédric Le Goater             }
178042bea956SCédric Le Goater         }
178142bea956SCédric Le Goater     }
178242bea956SCédric Le Goater 
178397a28b0eSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_NEON)) {
178497a28b0eSPeter Maydell         cpu->has_neon = true;
178597a28b0eSPeter Maydell         if (!kvm_enabled()) {
178694d912d1SMarc-André Lureau             qdev_property_add_static(DEVICE(obj), &arm_cpu_has_neon_property);
178797a28b0eSPeter Maydell         }
178897a28b0eSPeter Maydell     }
178997a28b0eSPeter Maydell 
1790ea90db0aSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_M) &&
1791ea90db0aSPeter Maydell         arm_feature(&cpu->env, ARM_FEATURE_THUMB_DSP)) {
179294d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_dsp_property);
1793ea90db0aSPeter Maydell     }
1794ea90db0aSPeter Maydell 
1795452a0955SPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_PMSA)) {
179694d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_mpu_property);
1797fcf5ef2aSThomas Huth         if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
1798fcf5ef2aSThomas Huth             qdev_property_add_static(DEVICE(obj),
179994d912d1SMarc-André Lureau                                      &arm_cpu_pmsav7_dregion_property);
1800fcf5ef2aSThomas Huth         }
1801fcf5ef2aSThomas Huth     }
1802fcf5ef2aSThomas Huth 
1803181962fdSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_M_SECURITY)) {
1804181962fdSPeter Maydell         object_property_add_link(obj, "idau", TYPE_IDAU_INTERFACE, &cpu->idau,
1805181962fdSPeter Maydell                                  qdev_prop_allow_set_link_before_realize,
1806d2623129SMarkus Armbruster                                  OBJ_PROP_LINK_STRONG);
1807f9f62e4cSPeter Maydell         /*
1808f9f62e4cSPeter Maydell          * M profile: initial value of the Secure VTOR. We can't just use
1809f9f62e4cSPeter Maydell          * a simple DEFINE_PROP_UINT32 for this because we want to permit
1810f9f62e4cSPeter Maydell          * the property to be set after realize.
1811f9f62e4cSPeter Maydell          */
181264a7b8deSFelipe Franciosi         object_property_add_uint32_ptr(obj, "init-svtor",
181364a7b8deSFelipe Franciosi                                        &cpu->init_svtor,
1814d2623129SMarkus Armbruster                                        OBJ_PROP_FLAG_READWRITE);
1815181962fdSPeter Maydell     }
18167cda2149SPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_M)) {
18177cda2149SPeter Maydell         /*
18187cda2149SPeter Maydell          * Initial value of the NS VTOR (for cores without the Security
18197cda2149SPeter Maydell          * extension, this is the only VTOR)
18207cda2149SPeter Maydell          */
18217cda2149SPeter Maydell         object_property_add_uint32_ptr(obj, "init-nsvtor",
18227cda2149SPeter Maydell                                        &cpu->init_nsvtor,
18237cda2149SPeter Maydell                                        OBJ_PROP_FLAG_READWRITE);
18247cda2149SPeter Maydell     }
1825181962fdSPeter Maydell 
1826bddd892eSPeter Maydell     /* Not DEFINE_PROP_UINT32: we want this to be settable after realize */
1827bddd892eSPeter Maydell     object_property_add_uint32_ptr(obj, "psci-conduit",
1828bddd892eSPeter Maydell                                    &cpu->psci_conduit,
1829bddd892eSPeter Maydell                                    OBJ_PROP_FLAG_READWRITE);
1830bddd892eSPeter Maydell 
183194d912d1SMarc-André Lureau     qdev_property_add_static(DEVICE(obj), &arm_cpu_cfgend_property);
183296eec6b2SAndrew Jeffery 
183396eec6b2SAndrew Jeffery     if (arm_feature(&cpu->env, ARM_FEATURE_GENERIC_TIMER)) {
183494d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(cpu), &arm_cpu_gt_cntfrq_property);
183596eec6b2SAndrew Jeffery     }
18369e6f8d8aSfangying 
18379e6f8d8aSfangying     if (kvm_enabled()) {
1838cac675b5SPhilippe Mathieu-Daudé         kvm_arm_add_vcpu_properties(cpu);
18399e6f8d8aSfangying     }
18408bce44a2SRichard Henderson 
18418bce44a2SRichard Henderson #ifndef CONFIG_USER_ONLY
18428bce44a2SRichard Henderson     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) &&
18438bce44a2SRichard Henderson         cpu_isar_feature(aa64_mte, cpu)) {
18448bce44a2SRichard Henderson         object_property_add_link(obj, "tag-memory",
18458bce44a2SRichard Henderson                                  TYPE_MEMORY_REGION,
18468bce44a2SRichard Henderson                                  (Object **)&cpu->tag_memory,
18478bce44a2SRichard Henderson                                  qdev_prop_allow_set_link_before_realize,
18488bce44a2SRichard Henderson                                  OBJ_PROP_LINK_STRONG);
18498bce44a2SRichard Henderson 
18508bce44a2SRichard Henderson         if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
18518bce44a2SRichard Henderson             object_property_add_link(obj, "secure-tag-memory",
18528bce44a2SRichard Henderson                                      TYPE_MEMORY_REGION,
18538bce44a2SRichard Henderson                                      (Object **)&cpu->secure_tag_memory,
18548bce44a2SRichard Henderson                                      qdev_prop_allow_set_link_before_realize,
18558bce44a2SRichard Henderson                                      OBJ_PROP_LINK_STRONG);
18568bce44a2SRichard Henderson         }
18578bce44a2SRichard Henderson     }
18588bce44a2SRichard Henderson #endif
1859fcf5ef2aSThomas Huth }
1860fcf5ef2aSThomas Huth 
1861fcf5ef2aSThomas Huth static void arm_cpu_finalizefn(Object *obj)
1862fcf5ef2aSThomas Huth {
1863fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(obj);
186408267487SAaron Lindsay     ARMELChangeHook *hook, *next;
186508267487SAaron Lindsay 
1866fcf5ef2aSThomas Huth     g_hash_table_destroy(cpu->cp_regs);
186708267487SAaron Lindsay 
1868b5c53d1bSAaron Lindsay     QLIST_FOREACH_SAFE(hook, &cpu->pre_el_change_hooks, node, next) {
1869b5c53d1bSAaron Lindsay         QLIST_REMOVE(hook, node);
1870b5c53d1bSAaron Lindsay         g_free(hook);
1871b5c53d1bSAaron Lindsay     }
187208267487SAaron Lindsay     QLIST_FOREACH_SAFE(hook, &cpu->el_change_hooks, node, next) {
187308267487SAaron Lindsay         QLIST_REMOVE(hook, node);
187408267487SAaron Lindsay         g_free(hook);
187508267487SAaron Lindsay     }
18764e7beb0cSAaron Lindsay OS #ifndef CONFIG_USER_ONLY
18774e7beb0cSAaron Lindsay OS     if (cpu->pmu_timer) {
18784e7beb0cSAaron Lindsay OS         timer_free(cpu->pmu_timer);
18794e7beb0cSAaron Lindsay OS     }
18804e7beb0cSAaron Lindsay OS #endif
1881fcf5ef2aSThomas Huth }
1882fcf5ef2aSThomas Huth 
18830df9142dSAndrew Jones void arm_cpu_finalize_features(ARMCPU *cpu, Error **errp)
18840df9142dSAndrew Jones {
18850df9142dSAndrew Jones     Error *local_err = NULL;
18860df9142dSAndrew Jones 
188707301161SRichard Henderson #ifdef TARGET_AARCH64
18880df9142dSAndrew Jones     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
18890df9142dSAndrew Jones         arm_cpu_sve_finalize(cpu, &local_err);
18900df9142dSAndrew Jones         if (local_err != NULL) {
18910df9142dSAndrew Jones             error_propagate(errp, local_err);
18920df9142dSAndrew Jones             return;
18930df9142dSAndrew Jones         }
1894eb94284dSRichard Henderson 
1895f7767ca3SPeter Maydell         /*
1896f7767ca3SPeter Maydell          * FEAT_SME is not architecturally dependent on FEAT_SVE (unless
1897f7767ca3SPeter Maydell          * FEAT_SME_FA64 is present). However our implementation currently
1898f7767ca3SPeter Maydell          * assumes it, so if the user asked for sve=off then turn off SME also.
1899f7767ca3SPeter Maydell          * (KVM doesn't currently support SME at all.)
1900f7767ca3SPeter Maydell          */
1901f7767ca3SPeter Maydell         if (cpu_isar_feature(aa64_sme, cpu) && !cpu_isar_feature(aa64_sve, cpu)) {
1902f7767ca3SPeter Maydell             object_property_set_bool(OBJECT(cpu), "sme", false, &error_abort);
1903f7767ca3SPeter Maydell         }
1904f7767ca3SPeter Maydell 
1905e74c0976SRichard Henderson         arm_cpu_sme_finalize(cpu, &local_err);
1906e74c0976SRichard Henderson         if (local_err != NULL) {
1907e74c0976SRichard Henderson             error_propagate(errp, local_err);
1908e74c0976SRichard Henderson             return;
1909e74c0976SRichard Henderson         }
1910e74c0976SRichard Henderson 
1911eb94284dSRichard Henderson         arm_cpu_pauth_finalize(cpu, &local_err);
1912eb94284dSRichard Henderson         if (local_err != NULL) {
1913eb94284dSRichard Henderson             error_propagate(errp, local_err);
1914eb94284dSRichard Henderson             return;
1915eb94284dSRichard Henderson         }
191669b2265dSRichard Henderson 
191769b2265dSRichard Henderson         arm_cpu_lpa2_finalize(cpu, &local_err);
191869b2265dSRichard Henderson         if (local_err != NULL) {
191969b2265dSRichard Henderson             error_propagate(errp, local_err);
192069b2265dSRichard Henderson             return;
192169b2265dSRichard Henderson         }
1922eb94284dSRichard Henderson     }
192307301161SRichard Henderson #endif
192468970d1eSAndrew Jones 
192568970d1eSAndrew Jones     if (kvm_enabled()) {
192668970d1eSAndrew Jones         kvm_arm_steal_time_finalize(cpu, &local_err);
192768970d1eSAndrew Jones         if (local_err != NULL) {
192868970d1eSAndrew Jones             error_propagate(errp, local_err);
192968970d1eSAndrew Jones             return;
193068970d1eSAndrew Jones         }
193168970d1eSAndrew Jones     }
19320df9142dSAndrew Jones }
19330df9142dSAndrew Jones 
1934fcf5ef2aSThomas Huth static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
1935fcf5ef2aSThomas Huth {
1936fcf5ef2aSThomas Huth     CPUState *cs = CPU(dev);
1937fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(dev);
1938fcf5ef2aSThomas Huth     ARMCPUClass *acc = ARM_CPU_GET_CLASS(dev);
1939fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
1940fcf5ef2aSThomas Huth     Error *local_err = NULL;
1941fcf5ef2aSThomas Huth 
1942b94b8c60SPhilippe Mathieu-Daudé #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
1943e607ea39SAnton Johansson     /* Use pc-relative instructions in system-mode */
1944e607ea39SAnton Johansson     cs->tcg_cflags |= CF_PCREL;
1945e607ea39SAnton Johansson #endif
1946e607ea39SAnton Johansson 
1947c4487d76SPeter Maydell     /* If we needed to query the host kernel for the CPU features
1948c4487d76SPeter Maydell      * then it's possible that might have failed in the initfn, but
1949c4487d76SPeter Maydell      * this is the first point where we can report it.
1950c4487d76SPeter Maydell      */
1951c4487d76SPeter Maydell     if (cpu->host_cpu_probe_failed) {
1952585df85eSPeter Maydell         if (!kvm_enabled() && !hvf_enabled()) {
1953585df85eSPeter Maydell             error_setg(errp, "The 'host' CPU type can only be used with KVM or HVF");
1954c4487d76SPeter Maydell         } else {
1955c4487d76SPeter Maydell             error_setg(errp, "Failed to retrieve host CPU features");
1956c4487d76SPeter Maydell         }
1957c4487d76SPeter Maydell         return;
1958c4487d76SPeter Maydell     }
1959c4487d76SPeter Maydell 
1960*bd8e9ddfSPeter Maydell     if (!cpu->gt_cntfrq_hz) {
1961*bd8e9ddfSPeter Maydell         /*
1962*bd8e9ddfSPeter Maydell          * 0 means "the board didn't set a value, use the default".
1963*bd8e9ddfSPeter Maydell          * The default value of the generic timer frequency (as seen in
1964*bd8e9ddfSPeter Maydell          * CNTFRQ_EL0) is 62.5MHz, which corresponds to a period of 16ns.
1965*bd8e9ddfSPeter Maydell          * This is what you get (a) for a CONFIG_USER_ONLY CPU (b) if the
1966*bd8e9ddfSPeter Maydell          * board doesn't set it.
1967*bd8e9ddfSPeter Maydell          */
1968*bd8e9ddfSPeter Maydell         cpu->gt_cntfrq_hz = GTIMER_DEFAULT_HZ;
1969*bd8e9ddfSPeter Maydell     }
1970*bd8e9ddfSPeter Maydell 
197195f87565SPeter Maydell #ifndef CONFIG_USER_ONLY
197295f87565SPeter Maydell     /* The NVIC and M-profile CPU are two halves of a single piece of
197395f87565SPeter Maydell      * hardware; trying to use one without the other is a command line
197495f87565SPeter Maydell      * error and will result in segfaults if not caught here.
197595f87565SPeter Maydell      */
197695f87565SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M)) {
197795f87565SPeter Maydell         if (!env->nvic) {
197895f87565SPeter Maydell             error_setg(errp, "This board cannot be used with Cortex-M CPUs");
197995f87565SPeter Maydell             return;
198095f87565SPeter Maydell         }
198195f87565SPeter Maydell     } else {
198295f87565SPeter Maydell         if (env->nvic) {
198395f87565SPeter Maydell             error_setg(errp, "This board can only be used with Cortex-M CPUs");
198495f87565SPeter Maydell             return;
198595f87565SPeter Maydell         }
198695f87565SPeter Maydell     }
1987397cd31fSPeter Maydell 
1988045e5064SAlexander Graf     if (!tcg_enabled() && !qtest_enabled()) {
198949e7f191SPeter Maydell         /*
1990045e5064SAlexander Graf          * We assume that no accelerator except TCG (and the "not really an
1991045e5064SAlexander Graf          * accelerator" qtest) can handle these features, because Arm hardware
1992045e5064SAlexander Graf          * virtualization can't virtualize them.
1993045e5064SAlexander Graf          *
199449e7f191SPeter Maydell          * Catch all the cases which might cause us to create more than one
199549e7f191SPeter Maydell          * address space for the CPU (otherwise we will assert() later in
199649e7f191SPeter Maydell          * cpu_address_space_init()).
199749e7f191SPeter Maydell          */
199849e7f191SPeter Maydell         if (arm_feature(env, ARM_FEATURE_M)) {
199949e7f191SPeter Maydell             error_setg(errp,
2000045e5064SAlexander Graf                        "Cannot enable %s when using an M-profile guest CPU",
2001045e5064SAlexander Graf                        current_accel_name());
200249e7f191SPeter Maydell             return;
200349e7f191SPeter Maydell         }
200449e7f191SPeter Maydell         if (cpu->has_el3) {
200549e7f191SPeter Maydell             error_setg(errp,
2006045e5064SAlexander Graf                        "Cannot enable %s when guest CPU has EL3 enabled",
2007045e5064SAlexander Graf                        current_accel_name());
200849e7f191SPeter Maydell             return;
200949e7f191SPeter Maydell         }
201049e7f191SPeter Maydell         if (cpu->tag_memory) {
201149e7f191SPeter Maydell             error_setg(errp,
2012d009607dSPeter Maydell                        "Cannot enable %s when guest CPUs has MTE enabled",
2013045e5064SAlexander Graf                        current_accel_name());
201449e7f191SPeter Maydell             return;
201549e7f191SPeter Maydell         }
201649e7f191SPeter Maydell     }
201749e7f191SPeter Maydell 
201896eec6b2SAndrew Jeffery     {
2019*bd8e9ddfSPeter Maydell         uint64_t scale = gt_cntfrq_period_ns(cpu);
202096eec6b2SAndrew Jeffery 
202196eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_PHYS] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
2022397cd31fSPeter Maydell                                                arm_gt_ptimer_cb, cpu);
202396eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_VIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
2024397cd31fSPeter Maydell                                                arm_gt_vtimer_cb, cpu);
202596eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_HYP] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
2026397cd31fSPeter Maydell                                               arm_gt_htimer_cb, cpu);
202796eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_SEC] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
2028397cd31fSPeter Maydell                                               arm_gt_stimer_cb, cpu);
20298c94b071SRichard Henderson         cpu->gt_timer[GTIMER_HYPVIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
20308c94b071SRichard Henderson                                                   arm_gt_hvtimer_cb, cpu);
203196eec6b2SAndrew Jeffery     }
203295f87565SPeter Maydell #endif
203395f87565SPeter Maydell 
2034fcf5ef2aSThomas Huth     cpu_exec_realizefn(cs, &local_err);
2035fcf5ef2aSThomas Huth     if (local_err != NULL) {
2036fcf5ef2aSThomas Huth         error_propagate(errp, local_err);
2037fcf5ef2aSThomas Huth         return;
2038fcf5ef2aSThomas Huth     }
2039fcf5ef2aSThomas Huth 
20400df9142dSAndrew Jones     arm_cpu_finalize_features(cpu, &local_err);
20410df9142dSAndrew Jones     if (local_err != NULL) {
20420df9142dSAndrew Jones         error_propagate(errp, local_err);
20430df9142dSAndrew Jones         return;
20440df9142dSAndrew Jones     }
20450df9142dSAndrew Jones 
20469719f125SJohn Högberg #ifdef CONFIG_USER_ONLY
20479719f125SJohn Högberg     /*
20489719f125SJohn Högberg      * User mode relies on IC IVAU instructions to catch modification of
20499719f125SJohn Högberg      * dual-mapped code.
20509719f125SJohn Högberg      *
20519719f125SJohn Högberg      * Clear CTR_EL0.DIC to ensure that software that honors these flags uses
20529719f125SJohn Högberg      * IC IVAU even if the emulated processor does not normally require it.
20539719f125SJohn Högberg      */
20549719f125SJohn Högberg     cpu->ctr = FIELD_DP64(cpu->ctr, CTR_EL0, DIC, 0);
20559719f125SJohn Högberg #endif
20569719f125SJohn Högberg 
205797a28b0eSPeter Maydell     if (arm_feature(env, ARM_FEATURE_AARCH64) &&
205897a28b0eSPeter Maydell         cpu->has_vfp != cpu->has_neon) {
205997a28b0eSPeter Maydell         /*
206097a28b0eSPeter Maydell          * This is an architectural requirement for AArch64; AArch32 is
206197a28b0eSPeter Maydell          * more flexible and permits VFP-no-Neon and Neon-no-VFP.
206297a28b0eSPeter Maydell          */
206397a28b0eSPeter Maydell         error_setg(errp,
206497a28b0eSPeter Maydell                    "AArch64 CPUs must have both VFP and Neon or neither");
206597a28b0eSPeter Maydell         return;
206697a28b0eSPeter Maydell     }
206797a28b0eSPeter Maydell 
206842bea956SCédric Le Goater     if (cpu->has_vfp_d32 != cpu->has_neon) {
206942bea956SCédric Le Goater         error_setg(errp, "ARM CPUs must have both VFP-D32 and Neon or neither");
207042bea956SCédric Le Goater         return;
207142bea956SCédric Le Goater     }
207242bea956SCédric Le Goater 
207342bea956SCédric Le Goater    if (!cpu->has_vfp_d32) {
207442bea956SCédric Le Goater         uint32_t u;
207542bea956SCédric Le Goater 
207642bea956SCédric Le Goater         u = cpu->isar.mvfr0;
207742bea956SCédric Le Goater         u = FIELD_DP32(u, MVFR0, SIMDREG, 1); /* 16 registers */
207842bea956SCédric Le Goater         cpu->isar.mvfr0 = u;
207942bea956SCédric Le Goater     }
208042bea956SCédric Le Goater 
208197a28b0eSPeter Maydell     if (!cpu->has_vfp) {
208297a28b0eSPeter Maydell         uint64_t t;
208397a28b0eSPeter Maydell         uint32_t u;
208497a28b0eSPeter Maydell 
208597a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar1;
208697a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR1, JSCVT, 0);
208797a28b0eSPeter Maydell         cpu->isar.id_aa64isar1 = t;
208897a28b0eSPeter Maydell 
208997a28b0eSPeter Maydell         t = cpu->isar.id_aa64pfr0;
209097a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64PFR0, FP, 0xf);
209197a28b0eSPeter Maydell         cpu->isar.id_aa64pfr0 = t;
209297a28b0eSPeter Maydell 
209397a28b0eSPeter Maydell         u = cpu->isar.id_isar6;
209497a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR6, JSCVT, 0);
20953c93dfa4SRichard Henderson         u = FIELD_DP32(u, ID_ISAR6, BF16, 0);
209697a28b0eSPeter Maydell         cpu->isar.id_isar6 = u;
209797a28b0eSPeter Maydell 
209897a28b0eSPeter Maydell         u = cpu->isar.mvfr0;
209997a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPSP, 0);
210097a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPDP, 0);
210197a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPDIVIDE, 0);
210297a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPSQRT, 0);
210397a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPROUND, 0);
2104532a3af5SPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
2105532a3af5SPeter Maydell             u = FIELD_DP32(u, MVFR0, FPTRAP, 0);
2106532a3af5SPeter Maydell             u = FIELD_DP32(u, MVFR0, FPSHVEC, 0);
2107532a3af5SPeter Maydell         }
210897a28b0eSPeter Maydell         cpu->isar.mvfr0 = u;
210997a28b0eSPeter Maydell 
211097a28b0eSPeter Maydell         u = cpu->isar.mvfr1;
211197a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR1, FPFTZ, 0);
211297a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR1, FPDNAN, 0);
211397a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR1, FPHP, 0);
2114532a3af5SPeter Maydell         if (arm_feature(env, ARM_FEATURE_M)) {
2115532a3af5SPeter Maydell             u = FIELD_DP32(u, MVFR1, FP16, 0);
2116532a3af5SPeter Maydell         }
211797a28b0eSPeter Maydell         cpu->isar.mvfr1 = u;
211897a28b0eSPeter Maydell 
211997a28b0eSPeter Maydell         u = cpu->isar.mvfr2;
212097a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR2, FPMISC, 0);
212197a28b0eSPeter Maydell         cpu->isar.mvfr2 = u;
212297a28b0eSPeter Maydell     }
212397a28b0eSPeter Maydell 
212497a28b0eSPeter Maydell     if (!cpu->has_neon) {
212597a28b0eSPeter Maydell         uint64_t t;
212697a28b0eSPeter Maydell         uint32_t u;
212797a28b0eSPeter Maydell 
212897a28b0eSPeter Maydell         unset_feature(env, ARM_FEATURE_NEON);
212997a28b0eSPeter Maydell 
213097a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar0;
2131eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, AES, 0);
2132eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SHA1, 0);
2133eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SHA2, 0);
2134eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SHA3, 0);
2135eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SM3, 0);
2136eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SM4, 0);
213797a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR0, DP, 0);
213897a28b0eSPeter Maydell         cpu->isar.id_aa64isar0 = t;
213997a28b0eSPeter Maydell 
214097a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar1;
214197a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR1, FCMA, 0);
21423c93dfa4SRichard Henderson         t = FIELD_DP64(t, ID_AA64ISAR1, BF16, 0);
2143f8680aaaSRichard Henderson         t = FIELD_DP64(t, ID_AA64ISAR1, I8MM, 0);
214497a28b0eSPeter Maydell         cpu->isar.id_aa64isar1 = t;
214597a28b0eSPeter Maydell 
214697a28b0eSPeter Maydell         t = cpu->isar.id_aa64pfr0;
214797a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64PFR0, ADVSIMD, 0xf);
214897a28b0eSPeter Maydell         cpu->isar.id_aa64pfr0 = t;
214997a28b0eSPeter Maydell 
215097a28b0eSPeter Maydell         u = cpu->isar.id_isar5;
2151eb851c11SDamien Hedde         u = FIELD_DP32(u, ID_ISAR5, AES, 0);
2152eb851c11SDamien Hedde         u = FIELD_DP32(u, ID_ISAR5, SHA1, 0);
2153eb851c11SDamien Hedde         u = FIELD_DP32(u, ID_ISAR5, SHA2, 0);
215497a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR5, RDM, 0);
215597a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR5, VCMA, 0);
215697a28b0eSPeter Maydell         cpu->isar.id_isar5 = u;
215797a28b0eSPeter Maydell 
215897a28b0eSPeter Maydell         u = cpu->isar.id_isar6;
215997a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR6, DP, 0);
216097a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR6, FHM, 0);
21613c93dfa4SRichard Henderson         u = FIELD_DP32(u, ID_ISAR6, BF16, 0);
2162f8680aaaSRichard Henderson         u = FIELD_DP32(u, ID_ISAR6, I8MM, 0);
216397a28b0eSPeter Maydell         cpu->isar.id_isar6 = u;
216497a28b0eSPeter Maydell 
2165532a3af5SPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
216697a28b0eSPeter Maydell             u = cpu->isar.mvfr1;
216797a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDLS, 0);
216897a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDINT, 0);
216997a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDSP, 0);
217097a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDHP, 0);
217197a28b0eSPeter Maydell             cpu->isar.mvfr1 = u;
217297a28b0eSPeter Maydell 
217397a28b0eSPeter Maydell             u = cpu->isar.mvfr2;
217497a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR2, SIMDMISC, 0);
217597a28b0eSPeter Maydell             cpu->isar.mvfr2 = u;
217697a28b0eSPeter Maydell         }
2177532a3af5SPeter Maydell     }
217897a28b0eSPeter Maydell 
217997a28b0eSPeter Maydell     if (!cpu->has_neon && !cpu->has_vfp) {
218097a28b0eSPeter Maydell         uint64_t t;
218197a28b0eSPeter Maydell         uint32_t u;
218297a28b0eSPeter Maydell 
218397a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar0;
218497a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR0, FHM, 0);
218597a28b0eSPeter Maydell         cpu->isar.id_aa64isar0 = t;
218697a28b0eSPeter Maydell 
218797a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar1;
218897a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR1, FRINTTS, 0);
218997a28b0eSPeter Maydell         cpu->isar.id_aa64isar1 = t;
219097a28b0eSPeter Maydell 
219197a28b0eSPeter Maydell         u = cpu->isar.mvfr0;
219297a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, SIMDREG, 0);
219397a28b0eSPeter Maydell         cpu->isar.mvfr0 = u;
2194c52881bbSRichard Henderson 
2195c52881bbSRichard Henderson         /* Despite the name, this field covers both VFP and Neon */
2196c52881bbSRichard Henderson         u = cpu->isar.mvfr1;
2197c52881bbSRichard Henderson         u = FIELD_DP32(u, MVFR1, SIMDFMAC, 0);
2198c52881bbSRichard Henderson         cpu->isar.mvfr1 = u;
219997a28b0eSPeter Maydell     }
220097a28b0eSPeter Maydell 
2201ea90db0aSPeter Maydell     if (arm_feature(env, ARM_FEATURE_M) && !cpu->has_dsp) {
2202ea90db0aSPeter Maydell         uint32_t u;
2203ea90db0aSPeter Maydell 
2204ea90db0aSPeter Maydell         unset_feature(env, ARM_FEATURE_THUMB_DSP);
2205ea90db0aSPeter Maydell 
2206ea90db0aSPeter Maydell         u = cpu->isar.id_isar1;
2207ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR1, EXTEND, 1);
2208ea90db0aSPeter Maydell         cpu->isar.id_isar1 = u;
2209ea90db0aSPeter Maydell 
2210ea90db0aSPeter Maydell         u = cpu->isar.id_isar2;
2211ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR2, MULTU, 1);
2212ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR2, MULTS, 1);
2213ea90db0aSPeter Maydell         cpu->isar.id_isar2 = u;
2214ea90db0aSPeter Maydell 
2215ea90db0aSPeter Maydell         u = cpu->isar.id_isar3;
2216ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR3, SIMD, 1);
2217ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR3, SATURATE, 0);
2218ea90db0aSPeter Maydell         cpu->isar.id_isar3 = u;
2219ea90db0aSPeter Maydell     }
2220ea90db0aSPeter Maydell 
2221fcf5ef2aSThomas Huth 
2222ea7ac69dSPeter Maydell     /*
2223ea7ac69dSPeter Maydell      * We rely on no XScale CPU having VFP so we can use the same bits in the
2224ea7ac69dSPeter Maydell      * TB flags field for VECSTRIDE and XSCALE_CPAR.
2225ea7ac69dSPeter Maydell      */
2226ee1004bbSPhilippe Mathieu-Daudé     assert(arm_feature(env, ARM_FEATURE_AARCH64) ||
22277d63183fSRichard Henderson            !cpu_isar_feature(aa32_vfp_simd, cpu) ||
22287d63183fSRichard Henderson            !arm_feature(env, ARM_FEATURE_XSCALE));
2229ea7ac69dSPeter Maydell 
2230a575230fSRichard Henderson #ifndef CONFIG_USER_ONLY
2231a575230fSRichard Henderson     {
2232a575230fSRichard Henderson         int pagebits;
2233fcf5ef2aSThomas Huth         if (arm_feature(env, ARM_FEATURE_V7) &&
2234fcf5ef2aSThomas Huth             !arm_feature(env, ARM_FEATURE_M) &&
2235452a0955SPeter Maydell             !arm_feature(env, ARM_FEATURE_PMSA)) {
2236a575230fSRichard Henderson             /*
2237a575230fSRichard Henderson              * v7VMSA drops support for the old ARMv5 tiny pages,
2238a575230fSRichard Henderson              * so we can use 4K pages.
2239fcf5ef2aSThomas Huth              */
2240fcf5ef2aSThomas Huth             pagebits = 12;
2241fcf5ef2aSThomas Huth         } else {
2242a575230fSRichard Henderson             /*
2243a575230fSRichard Henderson              * For CPUs which might have tiny 1K pages, or which have an
2244fcf5ef2aSThomas Huth              * MPU and might have small region sizes, stick with 1K pages.
2245fcf5ef2aSThomas Huth              */
2246fcf5ef2aSThomas Huth             pagebits = 10;
2247fcf5ef2aSThomas Huth         }
2248fcf5ef2aSThomas Huth         if (!set_preferred_target_page_bits(pagebits)) {
2249a575230fSRichard Henderson             /*
2250a575230fSRichard Henderson              * This can only ever happen for hotplugging a CPU, or if
2251fcf5ef2aSThomas Huth              * the board code incorrectly creates a CPU which it has
2252fcf5ef2aSThomas Huth              * promised via minimum_page_size that it will not.
2253fcf5ef2aSThomas Huth              */
2254a575230fSRichard Henderson             error_setg(errp, "This CPU requires a smaller page size "
2255a575230fSRichard Henderson                        "than the system is using");
2256fcf5ef2aSThomas Huth             return;
2257fcf5ef2aSThomas Huth         }
2258a575230fSRichard Henderson     }
2259a575230fSRichard Henderson #endif
2260fcf5ef2aSThomas Huth 
2261fcf5ef2aSThomas Huth     /* This cpu-id-to-MPIDR affinity is used only for TCG; KVM will override it.
2262fcf5ef2aSThomas Huth      * We don't support setting cluster ID ([16..23]) (known as Aff2
2263fcf5ef2aSThomas Huth      * in later ARM ARM versions), or any of the higher affinity level fields,
2264fcf5ef2aSThomas Huth      * so these bits always RAZ.
2265fcf5ef2aSThomas Huth      */
2266fcf5ef2aSThomas Huth     if (cpu->mp_affinity == ARM64_AFFINITY_INVALID) {
2267750245edSRichard Henderson         cpu->mp_affinity = arm_build_mp_affinity(cs->cpu_index,
226846de5913SIgor Mammedov                                                  ARM_DEFAULT_CPUS_PER_CLUSTER);
2269fcf5ef2aSThomas Huth     }
2270fcf5ef2aSThomas Huth 
2271fcf5ef2aSThomas Huth     if (cpu->reset_hivecs) {
2272fcf5ef2aSThomas Huth             cpu->reset_sctlr |= (1 << 13);
2273fcf5ef2aSThomas Huth     }
2274fcf5ef2aSThomas Huth 
22753a062d57SJulian Brown     if (cpu->cfgend) {
2276ee1004bbSPhilippe Mathieu-Daudé         if (arm_feature(env, ARM_FEATURE_V7)) {
22773a062d57SJulian Brown             cpu->reset_sctlr |= SCTLR_EE;
22783a062d57SJulian Brown         } else {
22793a062d57SJulian Brown             cpu->reset_sctlr |= SCTLR_B;
22803a062d57SJulian Brown         }
22813a062d57SJulian Brown     }
22823a062d57SJulian Brown 
228340188188SPeter Maydell     if (!arm_feature(env, ARM_FEATURE_M) && !cpu->has_el3) {
2284fcf5ef2aSThomas Huth         /* If the has_el3 CPU property is disabled then we need to disable the
2285fcf5ef2aSThomas Huth          * feature.
2286fcf5ef2aSThomas Huth          */
2287fcf5ef2aSThomas Huth         unset_feature(env, ARM_FEATURE_EL3);
2288fcf5ef2aSThomas Huth 
2289b13c91c0SRichard Henderson         /*
2290b13c91c0SRichard Henderson          * Disable the security extension feature bits in the processor
2291b13c91c0SRichard Henderson          * feature registers as well.
2292fcf5ef2aSThomas Huth          */
2293b13c91c0SRichard Henderson         cpu->isar.id_pfr1 = FIELD_DP32(cpu->isar.id_pfr1, ID_PFR1, SECURITY, 0);
2294033a4f15SRichard Henderson         cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, COPSDBG, 0);
2295b13c91c0SRichard Henderson         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2296b13c91c0SRichard Henderson                                            ID_AA64PFR0, EL3, 0);
2297b9f335c2SRichard Henderson 
2298b9f335c2SRichard Henderson         /* Disable the realm management extension, which requires EL3. */
2299b9f335c2SRichard Henderson         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2300b9f335c2SRichard Henderson                                            ID_AA64PFR0, RME, 0);
2301fcf5ef2aSThomas Huth     }
2302fcf5ef2aSThomas Huth 
2303c25bd18aSPeter Maydell     if (!cpu->has_el2) {
2304c25bd18aSPeter Maydell         unset_feature(env, ARM_FEATURE_EL2);
2305c25bd18aSPeter Maydell     }
2306c25bd18aSPeter Maydell 
2307d6f02ce3SWei Huang     if (!cpu->has_pmu) {
2308fcf5ef2aSThomas Huth         unset_feature(env, ARM_FEATURE_PMU);
230957a4a11bSAaron Lindsay     }
231057a4a11bSAaron Lindsay     if (arm_feature(env, ARM_FEATURE_PMU)) {
2311bf8d0969SAaron Lindsay OS         pmu_init(cpu);
231257a4a11bSAaron Lindsay 
231357a4a11bSAaron Lindsay         if (!kvm_enabled()) {
2314033614c4SAaron Lindsay             arm_register_pre_el_change_hook(cpu, &pmu_pre_el_change, 0);
2315033614c4SAaron Lindsay             arm_register_el_change_hook(cpu, &pmu_post_el_change, 0);
2316fcf5ef2aSThomas Huth         }
23174e7beb0cSAaron Lindsay OS 
23184e7beb0cSAaron Lindsay OS #ifndef CONFIG_USER_ONLY
23194e7beb0cSAaron Lindsay OS         cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, arm_pmu_timer_cb,
23204e7beb0cSAaron Lindsay OS                 cpu);
23214e7beb0cSAaron Lindsay OS #endif
232257a4a11bSAaron Lindsay     } else {
23232a609df8SPeter Maydell         cpu->isar.id_aa64dfr0 =
23242a609df8SPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMUVER, 0);
2325a6179538SPeter Maydell         cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, PERFMON, 0);
232657a4a11bSAaron Lindsay         cpu->pmceid0 = 0;
232757a4a11bSAaron Lindsay         cpu->pmceid1 = 0;
232857a4a11bSAaron Lindsay     }
2329fcf5ef2aSThomas Huth 
2330fcf5ef2aSThomas Huth     if (!arm_feature(env, ARM_FEATURE_EL2)) {
2331b13c91c0SRichard Henderson         /*
2332b13c91c0SRichard Henderson          * Disable the hypervisor feature bits in the processor feature
2333b13c91c0SRichard Henderson          * registers if we don't have EL2.
2334fcf5ef2aSThomas Huth          */
2335b13c91c0SRichard Henderson         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2336b13c91c0SRichard Henderson                                            ID_AA64PFR0, EL2, 0);
2337b13c91c0SRichard Henderson         cpu->isar.id_pfr1 = FIELD_DP32(cpu->isar.id_pfr1,
2338b13c91c0SRichard Henderson                                        ID_PFR1, VIRTUALIZATION, 0);
2339fcf5ef2aSThomas Huth     }
2340fcf5ef2aSThomas Huth 
23417134cb07SRichard Henderson     if (cpu_isar_feature(aa64_mte, cpu)) {
23427134cb07SRichard Henderson         /*
23437134cb07SRichard Henderson          * The architectural range of GM blocksize is 2-6, however qemu
23447134cb07SRichard Henderson          * doesn't support blocksize of 2 (see HELPER(ldgm)).
23457134cb07SRichard Henderson          */
23467134cb07SRichard Henderson         if (tcg_enabled()) {
23477134cb07SRichard Henderson             assert(cpu->gm_blocksize >= 3 && cpu->gm_blocksize <= 6);
23487134cb07SRichard Henderson         }
23497134cb07SRichard Henderson 
23506f4e1405SRichard Henderson #ifndef CONFIG_USER_ONLY
23516f4e1405SRichard Henderson         /*
2352cd305b5fSRichard Henderson          * If we do not have tag-memory provided by the machine,
2353cd305b5fSRichard Henderson          * reduce MTE support to instructions enabled at EL0.
2354cd305b5fSRichard Henderson          * This matches Cortex-A710 BROADCASTMTE input being LOW.
23556f4e1405SRichard Henderson          */
23567134cb07SRichard Henderson         if (cpu->tag_memory == NULL) {
23576f4e1405SRichard Henderson             cpu->isar.id_aa64pfr1 =
2358cd305b5fSRichard Henderson                 FIELD_DP64(cpu->isar.id_aa64pfr1, ID_AA64PFR1, MTE, 1);
23596f4e1405SRichard Henderson         }
23606f4e1405SRichard Henderson #endif
23617134cb07SRichard Henderson     }
23626f4e1405SRichard Henderson 
23632daf518dSPeter Maydell     if (tcg_enabled()) {
23642daf518dSPeter Maydell         /*
23657d8c283eSPeter Maydell          * Don't report some architectural features in the ID registers
23667d8c283eSPeter Maydell          * where TCG does not yet implement it (not even a minimal
23677d8c283eSPeter Maydell          * stub version). This avoids guests falling over when they
23687d8c283eSPeter Maydell          * try to access the non-existent system registers for them.
23692daf518dSPeter Maydell          */
23707d8c283eSPeter Maydell         /* FEAT_SPE (Statistical Profiling Extension) */
23712daf518dSPeter Maydell         cpu->isar.id_aa64dfr0 =
23722daf518dSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMSVER, 0);
23733d5f45ecSRichard Henderson         /* FEAT_TRBE (Trace Buffer Extension) */
23743d5f45ecSRichard Henderson         cpu->isar.id_aa64dfr0 =
23753d5f45ecSRichard Henderson             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEBUFFER, 0);
23767d8c283eSPeter Maydell         /* FEAT_TRF (Self-hosted Trace Extension) */
23777d8c283eSPeter Maydell         cpu->isar.id_aa64dfr0 =
23787d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEFILT, 0);
23797d8c283eSPeter Maydell         cpu->isar.id_dfr0 =
23807d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, TRACEFILT, 0);
23817d8c283eSPeter Maydell         /* Trace Macrocell system register access */
23827d8c283eSPeter Maydell         cpu->isar.id_aa64dfr0 =
23837d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEVER, 0);
23847d8c283eSPeter Maydell         cpu->isar.id_dfr0 =
23857d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, COPTRC, 0);
23867d8c283eSPeter Maydell         /* Memory mapped trace */
23877d8c283eSPeter Maydell         cpu->isar.id_dfr0 =
23887d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, MMAPTRC, 0);
23897d8c283eSPeter Maydell         /* FEAT_AMU (Activity Monitors Extension) */
23907d8c283eSPeter Maydell         cpu->isar.id_aa64pfr0 =
23917d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64pfr0, ID_AA64PFR0, AMU, 0);
23927d8c283eSPeter Maydell         cpu->isar.id_pfr0 =
23937d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_pfr0, ID_PFR0, AMU, 0);
23947d8c283eSPeter Maydell         /* FEAT_MPAM (Memory Partitioning and Monitoring Extension) */
23957d8c283eSPeter Maydell         cpu->isar.id_aa64pfr0 =
23967d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64pfr0, ID_AA64PFR0, MPAM, 0);
23972daf518dSPeter Maydell     }
23982daf518dSPeter Maydell 
2399f50cd314SPeter Maydell     /* MPU can be configured out of a PMSA CPU either by setting has-mpu
2400f50cd314SPeter Maydell      * to false or by setting pmsav7-dregion to 0.
2401f50cd314SPeter Maydell      */
2402761c4642STobias Röhmel     if (!cpu->has_mpu || cpu->pmsav7_dregion == 0) {
2403f50cd314SPeter Maydell         cpu->has_mpu = false;
2404761c4642STobias Röhmel         cpu->pmsav7_dregion = 0;
2405761c4642STobias Röhmel         cpu->pmsav8r_hdregion = 0;
2406fcf5ef2aSThomas Huth     }
2407fcf5ef2aSThomas Huth 
2408452a0955SPeter Maydell     if (arm_feature(env, ARM_FEATURE_PMSA) &&
2409fcf5ef2aSThomas Huth         arm_feature(env, ARM_FEATURE_V7)) {
2410fcf5ef2aSThomas Huth         uint32_t nr = cpu->pmsav7_dregion;
2411fcf5ef2aSThomas Huth 
2412fcf5ef2aSThomas Huth         if (nr > 0xff) {
2413fcf5ef2aSThomas Huth             error_setg(errp, "PMSAv7 MPU #regions invalid %" PRIu32, nr);
2414fcf5ef2aSThomas Huth             return;
2415fcf5ef2aSThomas Huth         }
2416fcf5ef2aSThomas Huth 
2417fcf5ef2aSThomas Huth         if (nr) {
24180e1a46bbSPeter Maydell             if (arm_feature(env, ARM_FEATURE_V8)) {
24190e1a46bbSPeter Maydell                 /* PMSAv8 */
242062c58ee0SPeter Maydell                 env->pmsav8.rbar[M_REG_NS] = g_new0(uint32_t, nr);
242162c58ee0SPeter Maydell                 env->pmsav8.rlar[M_REG_NS] = g_new0(uint32_t, nr);
242262c58ee0SPeter Maydell                 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
242362c58ee0SPeter Maydell                     env->pmsav8.rbar[M_REG_S] = g_new0(uint32_t, nr);
242462c58ee0SPeter Maydell                     env->pmsav8.rlar[M_REG_S] = g_new0(uint32_t, nr);
242562c58ee0SPeter Maydell                 }
24260e1a46bbSPeter Maydell             } else {
2427fcf5ef2aSThomas Huth                 env->pmsav7.drbar = g_new0(uint32_t, nr);
2428fcf5ef2aSThomas Huth                 env->pmsav7.drsr = g_new0(uint32_t, nr);
2429fcf5ef2aSThomas Huth                 env->pmsav7.dracr = g_new0(uint32_t, nr);
2430fcf5ef2aSThomas Huth             }
2431fcf5ef2aSThomas Huth         }
2432761c4642STobias Röhmel 
2433761c4642STobias Röhmel         if (cpu->pmsav8r_hdregion > 0xff) {
2434761c4642STobias Röhmel             error_setg(errp, "PMSAv8 MPU EL2 #regions invalid %" PRIu32,
2435761c4642STobias Röhmel                               cpu->pmsav8r_hdregion);
2436761c4642STobias Röhmel             return;
2437761c4642STobias Röhmel         }
2438761c4642STobias Röhmel 
2439761c4642STobias Röhmel         if (cpu->pmsav8r_hdregion) {
2440761c4642STobias Röhmel             env->pmsav8.hprbar = g_new0(uint32_t,
2441761c4642STobias Röhmel                                         cpu->pmsav8r_hdregion);
2442761c4642STobias Röhmel             env->pmsav8.hprlar = g_new0(uint32_t,
2443761c4642STobias Röhmel                                         cpu->pmsav8r_hdregion);
2444761c4642STobias Röhmel         }
24450e1a46bbSPeter Maydell     }
2446fcf5ef2aSThomas Huth 
24479901c576SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
24489901c576SPeter Maydell         uint32_t nr = cpu->sau_sregion;
24499901c576SPeter Maydell 
24509901c576SPeter Maydell         if (nr > 0xff) {
24519901c576SPeter Maydell             error_setg(errp, "v8M SAU #regions invalid %" PRIu32, nr);
24529901c576SPeter Maydell             return;
24539901c576SPeter Maydell         }
24549901c576SPeter Maydell 
24559901c576SPeter Maydell         if (nr) {
24569901c576SPeter Maydell             env->sau.rbar = g_new0(uint32_t, nr);
24579901c576SPeter Maydell             env->sau.rlar = g_new0(uint32_t, nr);
24589901c576SPeter Maydell         }
24599901c576SPeter Maydell     }
24609901c576SPeter Maydell 
246191db4642SCédric Le Goater     if (arm_feature(env, ARM_FEATURE_EL3)) {
246291db4642SCédric Le Goater         set_feature(env, ARM_FEATURE_VBAR);
246391db4642SCédric Le Goater     }
246491db4642SCédric Le Goater 
2465f6fc36deSJean-Philippe Brucker #ifndef CONFIG_USER_ONLY
2466f6fc36deSJean-Philippe Brucker     if (tcg_enabled() && cpu_isar_feature(aa64_rme, cpu)) {
2467f6fc36deSJean-Philippe Brucker         arm_register_el_change_hook(cpu, &gt_rme_post_el_change, 0);
2468f6fc36deSJean-Philippe Brucker     }
2469f6fc36deSJean-Philippe Brucker #endif
2470f6fc36deSJean-Philippe Brucker 
2471fcf5ef2aSThomas Huth     register_cp_regs_for_features(cpu);
2472fcf5ef2aSThomas Huth     arm_cpu_register_gdb_regs_for_features(cpu);
2473fcf5ef2aSThomas Huth 
2474fcf5ef2aSThomas Huth     init_cpreg_list(cpu);
2475fcf5ef2aSThomas Huth 
2476fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
2477cc7d44c2SLike Xu     MachineState *ms = MACHINE(qdev_get_machine());
2478cc7d44c2SLike Xu     unsigned int smp_cpus = ms->smp.cpus;
24798bce44a2SRichard Henderson     bool has_secure = cpu->has_el3 || arm_feature(env, ARM_FEATURE_M_SECURITY);
2480cc7d44c2SLike Xu 
24818bce44a2SRichard Henderson     /*
24828bce44a2SRichard Henderson      * We must set cs->num_ases to the final value before
24838bce44a2SRichard Henderson      * the first call to cpu_address_space_init.
24848bce44a2SRichard Henderson      */
24858bce44a2SRichard Henderson     if (cpu->tag_memory != NULL) {
24868bce44a2SRichard Henderson         cs->num_ases = 3 + has_secure;
24878bce44a2SRichard Henderson     } else {
24888bce44a2SRichard Henderson         cs->num_ases = 1 + has_secure;
24898bce44a2SRichard Henderson     }
24901d2091bcSPeter Maydell 
24918bce44a2SRichard Henderson     if (has_secure) {
2492fcf5ef2aSThomas Huth         if (!cpu->secure_memory) {
2493fcf5ef2aSThomas Huth             cpu->secure_memory = cs->memory;
2494fcf5ef2aSThomas Huth         }
249580ceb07aSPeter Xu         cpu_address_space_init(cs, ARMASIdx_S, "cpu-secure-memory",
249680ceb07aSPeter Xu                                cpu->secure_memory);
2497fcf5ef2aSThomas Huth     }
24988bce44a2SRichard Henderson 
24998bce44a2SRichard Henderson     if (cpu->tag_memory != NULL) {
25008bce44a2SRichard Henderson         cpu_address_space_init(cs, ARMASIdx_TagNS, "cpu-tag-memory",
25018bce44a2SRichard Henderson                                cpu->tag_memory);
25028bce44a2SRichard Henderson         if (has_secure) {
25038bce44a2SRichard Henderson             cpu_address_space_init(cs, ARMASIdx_TagS, "cpu-tag-memory",
25048bce44a2SRichard Henderson                                    cpu->secure_tag_memory);
25058bce44a2SRichard Henderson         }
25068bce44a2SRichard Henderson     }
25078bce44a2SRichard Henderson 
250880ceb07aSPeter Xu     cpu_address_space_init(cs, ARMASIdx_NS, "cpu-memory", cs->memory);
2509f9a69711SAlistair Francis 
2510f9a69711SAlistair Francis     /* No core_count specified, default to smp_cpus. */
2511f9a69711SAlistair Francis     if (cpu->core_count == -1) {
2512f9a69711SAlistair Francis         cpu->core_count = smp_cpus;
2513f9a69711SAlistair Francis     }
2514fcf5ef2aSThomas Huth #endif
2515fcf5ef2aSThomas Huth 
2516a4157b80SRichard Henderson     if (tcg_enabled()) {
2517a4157b80SRichard Henderson         int dcz_blocklen = 4 << cpu->dcz_blocksize;
2518a4157b80SRichard Henderson 
2519a4157b80SRichard Henderson         /*
2520a4157b80SRichard Henderson          * We only support DCZ blocklen that fits on one page.
2521a4157b80SRichard Henderson          *
2522a4157b80SRichard Henderson          * Architectually this is always true.  However TARGET_PAGE_SIZE
2523a4157b80SRichard Henderson          * is variable and, for compatibility with -machine virt-2.7,
2524a4157b80SRichard Henderson          * is only 1KiB, as an artifact of legacy ARMv5 subpage support.
2525a4157b80SRichard Henderson          * But even then, while the largest architectural DCZ blocklen
2526a4157b80SRichard Henderson          * is 2KiB, no cpu actually uses such a large blocklen.
2527a4157b80SRichard Henderson          */
2528a4157b80SRichard Henderson         assert(dcz_blocklen <= TARGET_PAGE_SIZE);
2529a4157b80SRichard Henderson 
2530a4157b80SRichard Henderson         /*
2531a4157b80SRichard Henderson          * We only support DCZ blocksize >= 2*TAG_GRANULE, which is to say
2532a4157b80SRichard Henderson          * both nibbles of each byte storing tag data may be written at once.
2533a4157b80SRichard Henderson          * Since TAG_GRANULE is 16, this means that blocklen must be >= 32.
2534a4157b80SRichard Henderson          */
2535a4157b80SRichard Henderson         if (cpu_isar_feature(aa64_mte, cpu)) {
2536a4157b80SRichard Henderson             assert(dcz_blocklen >= 2 * TAG_GRANULE);
2537a4157b80SRichard Henderson         }
2538a4157b80SRichard Henderson     }
2539a4157b80SRichard Henderson 
2540fcf5ef2aSThomas Huth     qemu_init_vcpu(cs);
2541fcf5ef2aSThomas Huth     cpu_reset(cs);
2542fcf5ef2aSThomas Huth 
2543fcf5ef2aSThomas Huth     acc->parent_realize(dev, errp);
2544fcf5ef2aSThomas Huth }
2545fcf5ef2aSThomas Huth 
2546fcf5ef2aSThomas Huth static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
2547fcf5ef2aSThomas Huth {
2548fcf5ef2aSThomas Huth     ObjectClass *oc;
2549fcf5ef2aSThomas Huth     char *typename;
2550fcf5ef2aSThomas Huth     char **cpuname;
2551a0032cc5SPeter Maydell     const char *cpunamestr;
2552fcf5ef2aSThomas Huth 
2553fcf5ef2aSThomas Huth     cpuname = g_strsplit(cpu_model, ",", 1);
2554a0032cc5SPeter Maydell     cpunamestr = cpuname[0];
2555a0032cc5SPeter Maydell #ifdef CONFIG_USER_ONLY
2556a0032cc5SPeter Maydell     /* For backwards compatibility usermode emulation allows "-cpu any",
2557a0032cc5SPeter Maydell      * which has the same semantics as "-cpu max".
2558a0032cc5SPeter Maydell      */
2559a0032cc5SPeter Maydell     if (!strcmp(cpunamestr, "any")) {
2560a0032cc5SPeter Maydell         cpunamestr = "max";
2561a0032cc5SPeter Maydell     }
2562a0032cc5SPeter Maydell #endif
2563a0032cc5SPeter Maydell     typename = g_strdup_printf(ARM_CPU_TYPE_NAME("%s"), cpunamestr);
2564fcf5ef2aSThomas Huth     oc = object_class_by_name(typename);
2565fcf5ef2aSThomas Huth     g_strfreev(cpuname);
2566fcf5ef2aSThomas Huth     g_free(typename);
2567d5be19f5SPhilippe Mathieu-Daudé 
2568fcf5ef2aSThomas Huth     return oc;
2569fcf5ef2aSThomas Huth }
2570fcf5ef2aSThomas Huth 
2571fcf5ef2aSThomas Huth static Property arm_cpu_properties[] = {
2572e544f800SPhilippe Mathieu-Daudé     DEFINE_PROP_UINT64("midr", ARMCPU, midr, 0),
2573fcf5ef2aSThomas Huth     DEFINE_PROP_UINT64("mp-affinity", ARMCPU,
2574fcf5ef2aSThomas Huth                         mp_affinity, ARM64_AFFINITY_INVALID),
257515f8b142SIgor Mammedov     DEFINE_PROP_INT32("node-id", ARMCPU, node_id, CPU_UNSET_NUMA_NODE_ID),
2576f9a69711SAlistair Francis     DEFINE_PROP_INT32("core-count", ARMCPU, core_count, -1),
2577fcf5ef2aSThomas Huth     DEFINE_PROP_END_OF_LIST()
2578fcf5ef2aSThomas Huth };
2579fcf5ef2aSThomas Huth 
2580a6506838SAkihiko Odaki static const gchar *arm_gdb_arch_name(CPUState *cs)
2581fcf5ef2aSThomas Huth {
2582fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(cs);
2583fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
2584fcf5ef2aSThomas Huth 
2585fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
2586a6506838SAkihiko Odaki         return "iwmmxt";
2587fcf5ef2aSThomas Huth     }
2588a6506838SAkihiko Odaki     return "arm";
2589fcf5ef2aSThomas Huth }
2590fcf5ef2aSThomas Huth 
25918b80bd28SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY
25928b80bd28SPhilippe Mathieu-Daudé #include "hw/core/sysemu-cpu-ops.h"
25938b80bd28SPhilippe Mathieu-Daudé 
25948b80bd28SPhilippe Mathieu-Daudé static const struct SysemuCPUOps arm_sysemu_ops = {
259508928c6dSPhilippe Mathieu-Daudé     .get_phys_page_attrs_debug = arm_cpu_get_phys_page_attrs_debug,
2596faf39e82SPhilippe Mathieu-Daudé     .asidx_from_attrs = arm_asidx_from_attrs,
2597715e3c1aSPhilippe Mathieu-Daudé     .write_elf32_note = arm_cpu_write_elf32_note,
2598715e3c1aSPhilippe Mathieu-Daudé     .write_elf64_note = arm_cpu_write_elf64_note,
2599da383e02SPhilippe Mathieu-Daudé     .virtio_is_big_endian = arm_cpu_virtio_is_big_endian,
2600feece4d0SPhilippe Mathieu-Daudé     .legacy_vmsd = &vmstate_arm_cpu,
26018b80bd28SPhilippe Mathieu-Daudé };
26028b80bd28SPhilippe Mathieu-Daudé #endif
26038b80bd28SPhilippe Mathieu-Daudé 
260478271684SClaudio Fontana #ifdef CONFIG_TCG
26051764ad70SRichard Henderson static const TCGCPUOps arm_tcg_ops = {
260678271684SClaudio Fontana     .initialize = arm_translate_init,
260778271684SClaudio Fontana     .synchronize_from_tb = arm_cpu_synchronize_from_tb,
260878271684SClaudio Fontana     .debug_excp_handler = arm_debug_excp_handler,
260956c6c98dSRichard Henderson     .restore_state_to_opc = arm_restore_state_to_opc,
261078271684SClaudio Fontana 
26119b12b6b4SRichard Henderson #ifdef CONFIG_USER_ONLY
26129b12b6b4SRichard Henderson     .record_sigsegv = arm_cpu_record_sigsegv,
261339a099caSRichard Henderson     .record_sigbus = arm_cpu_record_sigbus,
26149b12b6b4SRichard Henderson #else
26159b12b6b4SRichard Henderson     .tlb_fill = arm_cpu_tlb_fill,
2616083afd18SPhilippe Mathieu-Daudé     .cpu_exec_interrupt = arm_cpu_exec_interrupt,
261778271684SClaudio Fontana     .do_interrupt = arm_cpu_do_interrupt,
261878271684SClaudio Fontana     .do_transaction_failed = arm_cpu_do_transaction_failed,
261978271684SClaudio Fontana     .do_unaligned_access = arm_cpu_do_unaligned_access,
262078271684SClaudio Fontana     .adjust_watchpoint_address = arm_adjust_watchpoint_address,
262178271684SClaudio Fontana     .debug_check_watchpoint = arm_debug_check_watchpoint,
2622b00d86bcSRichard Henderson     .debug_check_breakpoint = arm_debug_check_breakpoint,
262378271684SClaudio Fontana #endif /* !CONFIG_USER_ONLY */
262478271684SClaudio Fontana };
262578271684SClaudio Fontana #endif /* CONFIG_TCG */
262678271684SClaudio Fontana 
2627fcf5ef2aSThomas Huth static void arm_cpu_class_init(ObjectClass *oc, void *data)
2628fcf5ef2aSThomas Huth {
2629fcf5ef2aSThomas Huth     ARMCPUClass *acc = ARM_CPU_CLASS(oc);
2630fcf5ef2aSThomas Huth     CPUClass *cc = CPU_CLASS(acc);
2631fcf5ef2aSThomas Huth     DeviceClass *dc = DEVICE_CLASS(oc);
26329130cadeSPeter Maydell     ResettableClass *rc = RESETTABLE_CLASS(oc);
2633fcf5ef2aSThomas Huth 
2634bf853881SPhilippe Mathieu-Daudé     device_class_set_parent_realize(dc, arm_cpu_realizefn,
2635bf853881SPhilippe Mathieu-Daudé                                     &acc->parent_realize);
2636fcf5ef2aSThomas Huth 
26374f67d30bSMarc-André Lureau     device_class_set_props(dc, arm_cpu_properties);
26389130cadeSPeter Maydell 
26399130cadeSPeter Maydell     resettable_class_set_parent_phases(rc, NULL, arm_cpu_reset_hold, NULL,
26409130cadeSPeter Maydell                                        &acc->parent_phases);
2641fcf5ef2aSThomas Huth 
2642fcf5ef2aSThomas Huth     cc->class_by_name = arm_cpu_class_by_name;
2643fcf5ef2aSThomas Huth     cc->has_work = arm_cpu_has_work;
2644628421c7SRichard Henderson     cc->mmu_index = arm_cpu_mmu_index;
2645fcf5ef2aSThomas Huth     cc->dump_state = arm_cpu_dump_state;
2646fcf5ef2aSThomas Huth     cc->set_pc = arm_cpu_set_pc;
2647e4fdf9dfSRichard Henderson     cc->get_pc = arm_cpu_get_pc;
2648fcf5ef2aSThomas Huth     cc->gdb_read_register = arm_cpu_gdb_read_register;
2649fcf5ef2aSThomas Huth     cc->gdb_write_register = arm_cpu_gdb_write_register;
26507350d553SRichard Henderson #ifndef CONFIG_USER_ONLY
26518b80bd28SPhilippe Mathieu-Daudé     cc->sysemu_ops = &arm_sysemu_ops;
2652fcf5ef2aSThomas Huth #endif
2653fcf5ef2aSThomas Huth     cc->gdb_arch_name = arm_gdb_arch_name;
2654fcf5ef2aSThomas Huth     cc->gdb_stop_before_watchpoint = true;
2655fcf5ef2aSThomas Huth     cc->disas_set_info = arm_disas_set_info;
265678271684SClaudio Fontana 
265774d7fc7fSRichard Henderson #ifdef CONFIG_TCG
265878271684SClaudio Fontana     cc->tcg_ops = &arm_tcg_ops;
2659cbc183d2SClaudio Fontana #endif /* CONFIG_TCG */
2660fcf5ef2aSThomas Huth }
2661fcf5ef2aSThomas Huth 
266251e5ef45SMarc-André Lureau static void arm_cpu_instance_init(Object *obj)
266351e5ef45SMarc-André Lureau {
266451e5ef45SMarc-André Lureau     ARMCPUClass *acc = ARM_CPU_GET_CLASS(obj);
266551e5ef45SMarc-André Lureau 
266651e5ef45SMarc-André Lureau     acc->info->initfn(obj);
266751e5ef45SMarc-André Lureau     arm_cpu_post_init(obj);
266851e5ef45SMarc-André Lureau }
266951e5ef45SMarc-André Lureau 
267051e5ef45SMarc-André Lureau static void cpu_register_class_init(ObjectClass *oc, void *data)
267151e5ef45SMarc-André Lureau {
267251e5ef45SMarc-André Lureau     ARMCPUClass *acc = ARM_CPU_CLASS(oc);
267348de6462SAkihiko Odaki     CPUClass *cc = CPU_CLASS(acc);
267451e5ef45SMarc-André Lureau 
267551e5ef45SMarc-André Lureau     acc->info = data;
267648de6462SAkihiko Odaki     cc->gdb_core_xml_file = "arm-core.xml";
267751e5ef45SMarc-André Lureau }
267851e5ef45SMarc-André Lureau 
267937bcf244SThomas Huth void arm_cpu_register(const ARMCPUInfo *info)
2680fcf5ef2aSThomas Huth {
2681fcf5ef2aSThomas Huth     TypeInfo type_info = {
2682fcf5ef2aSThomas Huth         .parent = TYPE_ARM_CPU,
268351e5ef45SMarc-André Lureau         .instance_init = arm_cpu_instance_init,
268451e5ef45SMarc-André Lureau         .class_init = info->class_init ?: cpu_register_class_init,
268551e5ef45SMarc-André Lureau         .class_data = (void *)info,
2686fcf5ef2aSThomas Huth     };
2687fcf5ef2aSThomas Huth 
2688fcf5ef2aSThomas Huth     type_info.name = g_strdup_printf("%s-" TYPE_ARM_CPU, info->name);
2689fcf5ef2aSThomas Huth     type_register(&type_info);
2690fcf5ef2aSThomas Huth     g_free((void *)type_info.name);
2691fcf5ef2aSThomas Huth }
2692fcf5ef2aSThomas Huth 
2693fcf5ef2aSThomas Huth static const TypeInfo arm_cpu_type_info = {
2694fcf5ef2aSThomas Huth     .name = TYPE_ARM_CPU,
2695fcf5ef2aSThomas Huth     .parent = TYPE_CPU,
2696fcf5ef2aSThomas Huth     .instance_size = sizeof(ARMCPU),
2697d03087bdSRichard Henderson     .instance_align = __alignof__(ARMCPU),
2698fcf5ef2aSThomas Huth     .instance_init = arm_cpu_initfn,
2699fcf5ef2aSThomas Huth     .instance_finalize = arm_cpu_finalizefn,
2700fcf5ef2aSThomas Huth     .abstract = true,
2701fcf5ef2aSThomas Huth     .class_size = sizeof(ARMCPUClass),
2702fcf5ef2aSThomas Huth     .class_init = arm_cpu_class_init,
2703fcf5ef2aSThomas Huth };
2704fcf5ef2aSThomas Huth 
2705fcf5ef2aSThomas Huth static void arm_cpu_register_types(void)
2706fcf5ef2aSThomas Huth {
2707fcf5ef2aSThomas Huth     type_register_static(&arm_cpu_type_info);
2708fcf5ef2aSThomas Huth }
2709fcf5ef2aSThomas Huth 
2710fcf5ef2aSThomas Huth type_init(arm_cpu_register_types)
2711