xref: /openbmc/qemu/target/arm/cpu.c (revision a96edb687e76a44b554b7975d9deda522c2c4302)
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 
1135*a96edb68SPeter Maydell #ifdef CONFIG_TCG
1136*a96edb68SPeter Maydell static bool arm_cpu_exec_halt(CPUState *cs)
1137*a96edb68SPeter Maydell {
1138*a96edb68SPeter Maydell     bool leave_halt = cpu_has_work(cs);
1139*a96edb68SPeter Maydell 
1140*a96edb68SPeter Maydell     if (leave_halt) {
1141*a96edb68SPeter Maydell         /* We're about to come out of WFI/WFE: disable the WFxT timer */
1142*a96edb68SPeter Maydell         ARMCPU *cpu = ARM_CPU(cs);
1143*a96edb68SPeter Maydell         if (cpu->wfxt_timer) {
1144*a96edb68SPeter Maydell             timer_del(cpu->wfxt_timer);
1145*a96edb68SPeter Maydell         }
1146*a96edb68SPeter Maydell     }
1147*a96edb68SPeter Maydell     return leave_halt;
1148*a96edb68SPeter Maydell }
1149*a96edb68SPeter Maydell #endif
1150*a96edb68SPeter Maydell 
1151*a96edb68SPeter Maydell static void arm_wfxt_timer_cb(void *opaque)
1152*a96edb68SPeter Maydell {
1153*a96edb68SPeter Maydell     ARMCPU *cpu = opaque;
1154*a96edb68SPeter Maydell     CPUState *cs = CPU(cpu);
1155*a96edb68SPeter Maydell 
1156*a96edb68SPeter Maydell     /*
1157*a96edb68SPeter Maydell      * We expect the CPU to be halted; this will cause arm_cpu_is_work()
1158*a96edb68SPeter Maydell      * to return true (so we will come out of halt even with no other
1159*a96edb68SPeter Maydell      * pending interrupt), and the TCG accelerator's cpu_exec_interrupt()
1160*a96edb68SPeter Maydell      * function auto-clears the CPU_INTERRUPT_EXITTB flag for us.
1161*a96edb68SPeter Maydell      */
1162*a96edb68SPeter Maydell     cpu_interrupt(cs, CPU_INTERRUPT_EXITTB);
1163*a96edb68SPeter Maydell }
1164fcf5ef2aSThomas Huth #endif
1165fcf5ef2aSThomas Huth 
1166fcf5ef2aSThomas Huth static void arm_disas_set_info(CPUState *cpu, disassemble_info *info)
1167fcf5ef2aSThomas Huth {
1168fcf5ef2aSThomas Huth     ARMCPU *ac = ARM_CPU(cpu);
1169fcf5ef2aSThomas Huth     CPUARMState *env = &ac->env;
11707bcdbf51SRichard Henderson     bool sctlr_b;
1171fcf5ef2aSThomas Huth 
1172fcf5ef2aSThomas Huth     if (is_a64(env)) {
1173110f6c70SRichard Henderson         info->cap_arch = CS_ARCH_ARM64;
117415fa1a0aSRichard Henderson         info->cap_insn_unit = 4;
117515fa1a0aSRichard Henderson         info->cap_insn_split = 4;
1176110f6c70SRichard Henderson     } else {
1177110f6c70SRichard Henderson         int cap_mode;
1178110f6c70SRichard Henderson         if (env->thumb) {
117915fa1a0aSRichard Henderson             info->cap_insn_unit = 2;
118015fa1a0aSRichard Henderson             info->cap_insn_split = 4;
1181110f6c70SRichard Henderson             cap_mode = CS_MODE_THUMB;
1182fcf5ef2aSThomas Huth         } else {
118315fa1a0aSRichard Henderson             info->cap_insn_unit = 4;
118415fa1a0aSRichard Henderson             info->cap_insn_split = 4;
1185110f6c70SRichard Henderson             cap_mode = CS_MODE_ARM;
1186fcf5ef2aSThomas Huth         }
1187110f6c70SRichard Henderson         if (arm_feature(env, ARM_FEATURE_V8)) {
1188110f6c70SRichard Henderson             cap_mode |= CS_MODE_V8;
1189110f6c70SRichard Henderson         }
1190110f6c70SRichard Henderson         if (arm_feature(env, ARM_FEATURE_M)) {
1191110f6c70SRichard Henderson             cap_mode |= CS_MODE_MCLASS;
1192110f6c70SRichard Henderson         }
1193110f6c70SRichard Henderson         info->cap_arch = CS_ARCH_ARM;
1194110f6c70SRichard Henderson         info->cap_mode = cap_mode;
1195fcf5ef2aSThomas Huth     }
11967bcdbf51SRichard Henderson 
11977bcdbf51SRichard Henderson     sctlr_b = arm_sctlr_b(env);
11987bcdbf51SRichard Henderson     if (bswap_code(sctlr_b)) {
1199ee3eb3a7SMarc-André Lureau #if TARGET_BIG_ENDIAN
1200fcf5ef2aSThomas Huth         info->endian = BFD_ENDIAN_LITTLE;
1201fcf5ef2aSThomas Huth #else
1202fcf5ef2aSThomas Huth         info->endian = BFD_ENDIAN_BIG;
1203fcf5ef2aSThomas Huth #endif
1204fcf5ef2aSThomas Huth     }
1205f7478a92SJulian Brown     info->flags &= ~INSN_ARM_BE32;
12067bcdbf51SRichard Henderson #ifndef CONFIG_USER_ONLY
12077bcdbf51SRichard Henderson     if (sctlr_b) {
1208f7478a92SJulian Brown         info->flags |= INSN_ARM_BE32;
1209f7478a92SJulian Brown     }
12107bcdbf51SRichard Henderson #endif
1211fcf5ef2aSThomas Huth }
1212fcf5ef2aSThomas Huth 
121386480615SPhilippe Mathieu-Daudé #ifdef TARGET_AARCH64
121486480615SPhilippe Mathieu-Daudé 
121586480615SPhilippe Mathieu-Daudé static void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags)
121686480615SPhilippe Mathieu-Daudé {
121786480615SPhilippe Mathieu-Daudé     ARMCPU *cpu = ARM_CPU(cs);
121886480615SPhilippe Mathieu-Daudé     CPUARMState *env = &cpu->env;
121986480615SPhilippe Mathieu-Daudé     uint32_t psr = pstate_read(env);
1220a9d84070SRichard Henderson     int i, j;
122186480615SPhilippe Mathieu-Daudé     int el = arm_current_el(env);
1222bde0e60bSPeter Maydell     uint64_t hcr = arm_hcr_el2_eff(env);
122386480615SPhilippe Mathieu-Daudé     const char *ns_status;
12247a867dd5SRichard Henderson     bool sve;
122586480615SPhilippe Mathieu-Daudé 
122686480615SPhilippe Mathieu-Daudé     qemu_fprintf(f, " PC=%016" PRIx64 " ", env->pc);
122786480615SPhilippe Mathieu-Daudé     for (i = 0; i < 32; i++) {
122886480615SPhilippe Mathieu-Daudé         if (i == 31) {
122986480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, " SP=%016" PRIx64 "\n", env->xregs[i]);
123086480615SPhilippe Mathieu-Daudé         } else {
123186480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "X%02d=%016" PRIx64 "%s", i, env->xregs[i],
123286480615SPhilippe Mathieu-Daudé                          (i + 2) % 3 ? " " : "\n");
123386480615SPhilippe Mathieu-Daudé         }
123486480615SPhilippe Mathieu-Daudé     }
123586480615SPhilippe Mathieu-Daudé 
123686480615SPhilippe Mathieu-Daudé     if (arm_feature(env, ARM_FEATURE_EL3) && el != 3) {
123786480615SPhilippe Mathieu-Daudé         ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
123886480615SPhilippe Mathieu-Daudé     } else {
123986480615SPhilippe Mathieu-Daudé         ns_status = "";
124086480615SPhilippe Mathieu-Daudé     }
124186480615SPhilippe Mathieu-Daudé     qemu_fprintf(f, "PSTATE=%08x %c%c%c%c %sEL%d%c",
124286480615SPhilippe Mathieu-Daudé                  psr,
124386480615SPhilippe Mathieu-Daudé                  psr & PSTATE_N ? 'N' : '-',
124486480615SPhilippe Mathieu-Daudé                  psr & PSTATE_Z ? 'Z' : '-',
124586480615SPhilippe Mathieu-Daudé                  psr & PSTATE_C ? 'C' : '-',
124686480615SPhilippe Mathieu-Daudé                  psr & PSTATE_V ? 'V' : '-',
124786480615SPhilippe Mathieu-Daudé                  ns_status,
124886480615SPhilippe Mathieu-Daudé                  el,
124986480615SPhilippe Mathieu-Daudé                  psr & PSTATE_SP ? 'h' : 't');
125086480615SPhilippe Mathieu-Daudé 
12517a867dd5SRichard Henderson     if (cpu_isar_feature(aa64_sme, cpu)) {
12527a867dd5SRichard Henderson         qemu_fprintf(f, "  SVCR=%08" PRIx64 " %c%c",
12537a867dd5SRichard Henderson                      env->svcr,
12547a867dd5SRichard Henderson                      (FIELD_EX64(env->svcr, SVCR, ZA) ? 'Z' : '-'),
12557a867dd5SRichard Henderson                      (FIELD_EX64(env->svcr, SVCR, SM) ? 'S' : '-'));
12567a867dd5SRichard Henderson     }
125786480615SPhilippe Mathieu-Daudé     if (cpu_isar_feature(aa64_bti, cpu)) {
125886480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "  BTYPE=%d", (psr & PSTATE_BTYPE) >> 10);
125986480615SPhilippe Mathieu-Daudé     }
1260bde0e60bSPeter Maydell     qemu_fprintf(f, "%s%s%s",
1261bde0e60bSPeter Maydell                  (hcr & HCR_NV) ? " NV" : "",
1262bde0e60bSPeter Maydell                  (hcr & HCR_NV1) ? " NV1" : "",
1263bde0e60bSPeter Maydell                  (hcr & HCR_NV2) ? " NV2" : "");
126486480615SPhilippe Mathieu-Daudé     if (!(flags & CPU_DUMP_FPU)) {
126586480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "\n");
126686480615SPhilippe Mathieu-Daudé         return;
126786480615SPhilippe Mathieu-Daudé     }
126886480615SPhilippe Mathieu-Daudé     if (fp_exception_el(env, el) != 0) {
126986480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "    FPU disabled\n");
127086480615SPhilippe Mathieu-Daudé         return;
127186480615SPhilippe Mathieu-Daudé     }
127286480615SPhilippe Mathieu-Daudé     qemu_fprintf(f, "     FPCR=%08x FPSR=%08x\n",
127386480615SPhilippe Mathieu-Daudé                  vfp_get_fpcr(env), vfp_get_fpsr(env));
127486480615SPhilippe Mathieu-Daudé 
12757a867dd5SRichard Henderson     if (cpu_isar_feature(aa64_sme, cpu) && FIELD_EX64(env->svcr, SVCR, SM)) {
12767a867dd5SRichard Henderson         sve = sme_exception_el(env, el) == 0;
12777a867dd5SRichard Henderson     } else if (cpu_isar_feature(aa64_sve, cpu)) {
12787a867dd5SRichard Henderson         sve = sve_exception_el(env, el) == 0;
12797a867dd5SRichard Henderson     } else {
12807a867dd5SRichard Henderson         sve = false;
12817a867dd5SRichard Henderson     }
12827a867dd5SRichard Henderson 
12837a867dd5SRichard Henderson     if (sve) {
1284a9d84070SRichard Henderson         int zcr_len = sve_vqm1_for_el(env, el);
128586480615SPhilippe Mathieu-Daudé 
128686480615SPhilippe Mathieu-Daudé         for (i = 0; i <= FFR_PRED_NUM; i++) {
128786480615SPhilippe Mathieu-Daudé             bool eol;
128886480615SPhilippe Mathieu-Daudé             if (i == FFR_PRED_NUM) {
128986480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "FFR=");
129086480615SPhilippe Mathieu-Daudé                 /* It's last, so end the line.  */
129186480615SPhilippe Mathieu-Daudé                 eol = true;
129286480615SPhilippe Mathieu-Daudé             } else {
129386480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "P%02d=", i);
129486480615SPhilippe Mathieu-Daudé                 switch (zcr_len) {
129586480615SPhilippe Mathieu-Daudé                 case 0:
129686480615SPhilippe Mathieu-Daudé                     eol = i % 8 == 7;
129786480615SPhilippe Mathieu-Daudé                     break;
129886480615SPhilippe Mathieu-Daudé                 case 1:
129986480615SPhilippe Mathieu-Daudé                     eol = i % 6 == 5;
130086480615SPhilippe Mathieu-Daudé                     break;
130186480615SPhilippe Mathieu-Daudé                 case 2:
130286480615SPhilippe Mathieu-Daudé                 case 3:
130386480615SPhilippe Mathieu-Daudé                     eol = i % 3 == 2;
130486480615SPhilippe Mathieu-Daudé                     break;
130586480615SPhilippe Mathieu-Daudé                 default:
130686480615SPhilippe Mathieu-Daudé                     /* More than one quadword per predicate.  */
130786480615SPhilippe Mathieu-Daudé                     eol = true;
130886480615SPhilippe Mathieu-Daudé                     break;
130986480615SPhilippe Mathieu-Daudé                 }
131086480615SPhilippe Mathieu-Daudé             }
131186480615SPhilippe Mathieu-Daudé             for (j = zcr_len / 4; j >= 0; j--) {
131286480615SPhilippe Mathieu-Daudé                 int digits;
131386480615SPhilippe Mathieu-Daudé                 if (j * 4 + 4 <= zcr_len + 1) {
131486480615SPhilippe Mathieu-Daudé                     digits = 16;
131586480615SPhilippe Mathieu-Daudé                 } else {
131686480615SPhilippe Mathieu-Daudé                     digits = (zcr_len % 4 + 1) * 4;
131786480615SPhilippe Mathieu-Daudé                 }
131886480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "%0*" PRIx64 "%s", digits,
131986480615SPhilippe Mathieu-Daudé                              env->vfp.pregs[i].p[j],
132086480615SPhilippe Mathieu-Daudé                              j ? ":" : eol ? "\n" : " ");
132186480615SPhilippe Mathieu-Daudé             }
132286480615SPhilippe Mathieu-Daudé         }
132386480615SPhilippe Mathieu-Daudé 
132486480615SPhilippe Mathieu-Daudé         if (zcr_len == 0) {
1325a9d84070SRichard Henderson             /*
1326a9d84070SRichard Henderson              * With vl=16, there are only 37 columns per register,
1327a9d84070SRichard Henderson              * so output two registers per line.
1328a9d84070SRichard Henderson              */
1329a9d84070SRichard Henderson             for (i = 0; i < 32; i++) {
133086480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "Z%02d=%016" PRIx64 ":%016" PRIx64 "%s",
133186480615SPhilippe Mathieu-Daudé                              i, env->vfp.zregs[i].d[1],
133286480615SPhilippe Mathieu-Daudé                              env->vfp.zregs[i].d[0], i & 1 ? "\n" : " ");
1333a9d84070SRichard Henderson             }
133486480615SPhilippe Mathieu-Daudé         } else {
1335a9d84070SRichard Henderson             for (i = 0; i < 32; i++) {
1336a9d84070SRichard Henderson                 qemu_fprintf(f, "Z%02d=", i);
133786480615SPhilippe Mathieu-Daudé                 for (j = zcr_len; j >= 0; j--) {
133886480615SPhilippe Mathieu-Daudé                     qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%s",
133986480615SPhilippe Mathieu-Daudé                                  env->vfp.zregs[i].d[j * 2 + 1],
1340a9d84070SRichard Henderson                                  env->vfp.zregs[i].d[j * 2 + 0],
1341a9d84070SRichard Henderson                                  j ? ":" : "\n");
134286480615SPhilippe Mathieu-Daudé                 }
134386480615SPhilippe Mathieu-Daudé             }
134486480615SPhilippe Mathieu-Daudé         }
134586480615SPhilippe Mathieu-Daudé     } else {
134686480615SPhilippe Mathieu-Daudé         for (i = 0; i < 32; i++) {
134786480615SPhilippe Mathieu-Daudé             uint64_t *q = aa64_vfp_qreg(env, i);
134886480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "Q%02d=%016" PRIx64 ":%016" PRIx64 "%s",
134986480615SPhilippe Mathieu-Daudé                          i, q[1], q[0], (i & 1 ? "\n" : " "));
135086480615SPhilippe Mathieu-Daudé         }
135186480615SPhilippe Mathieu-Daudé     }
1352270bea47SRichard Henderson 
1353270bea47SRichard Henderson     if (cpu_isar_feature(aa64_sme, cpu) &&
1354270bea47SRichard Henderson         FIELD_EX64(env->svcr, SVCR, ZA) &&
1355270bea47SRichard Henderson         sme_exception_el(env, el) == 0) {
1356270bea47SRichard Henderson         int zcr_len = sve_vqm1_for_el_sm(env, el, true);
1357270bea47SRichard Henderson         int svl = (zcr_len + 1) * 16;
1358270bea47SRichard Henderson         int svl_lg10 = svl < 100 ? 2 : 3;
1359270bea47SRichard Henderson 
1360270bea47SRichard Henderson         for (i = 0; i < svl; i++) {
1361270bea47SRichard Henderson             qemu_fprintf(f, "ZA[%0*d]=", svl_lg10, i);
1362270bea47SRichard Henderson             for (j = zcr_len; j >= 0; --j) {
1363270bea47SRichard Henderson                 qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%c",
1364270bea47SRichard Henderson                              env->zarray[i].d[2 * j + 1],
1365270bea47SRichard Henderson                              env->zarray[i].d[2 * j],
1366270bea47SRichard Henderson                              j ? ':' : '\n');
1367270bea47SRichard Henderson             }
1368270bea47SRichard Henderson         }
1369270bea47SRichard Henderson     }
137086480615SPhilippe Mathieu-Daudé }
137186480615SPhilippe Mathieu-Daudé 
137286480615SPhilippe Mathieu-Daudé #else
137386480615SPhilippe Mathieu-Daudé 
137486480615SPhilippe Mathieu-Daudé static inline void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags)
137586480615SPhilippe Mathieu-Daudé {
137686480615SPhilippe Mathieu-Daudé     g_assert_not_reached();
137786480615SPhilippe Mathieu-Daudé }
137886480615SPhilippe Mathieu-Daudé 
137986480615SPhilippe Mathieu-Daudé #endif
138086480615SPhilippe Mathieu-Daudé 
138186480615SPhilippe Mathieu-Daudé static void arm_cpu_dump_state(CPUState *cs, FILE *f, int flags)
138286480615SPhilippe Mathieu-Daudé {
138386480615SPhilippe Mathieu-Daudé     ARMCPU *cpu = ARM_CPU(cs);
138486480615SPhilippe Mathieu-Daudé     CPUARMState *env = &cpu->env;
138586480615SPhilippe Mathieu-Daudé     int i;
138686480615SPhilippe Mathieu-Daudé 
138786480615SPhilippe Mathieu-Daudé     if (is_a64(env)) {
138886480615SPhilippe Mathieu-Daudé         aarch64_cpu_dump_state(cs, f, flags);
138986480615SPhilippe Mathieu-Daudé         return;
139086480615SPhilippe Mathieu-Daudé     }
139186480615SPhilippe Mathieu-Daudé 
139286480615SPhilippe Mathieu-Daudé     for (i = 0; i < 16; i++) {
139386480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "R%02d=%08x", i, env->regs[i]);
139486480615SPhilippe Mathieu-Daudé         if ((i % 4) == 3) {
139586480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "\n");
139686480615SPhilippe Mathieu-Daudé         } else {
139786480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, " ");
139886480615SPhilippe Mathieu-Daudé         }
139986480615SPhilippe Mathieu-Daudé     }
140086480615SPhilippe Mathieu-Daudé 
140186480615SPhilippe Mathieu-Daudé     if (arm_feature(env, ARM_FEATURE_M)) {
140286480615SPhilippe Mathieu-Daudé         uint32_t xpsr = xpsr_read(env);
140386480615SPhilippe Mathieu-Daudé         const char *mode;
140486480615SPhilippe Mathieu-Daudé         const char *ns_status = "";
140586480615SPhilippe Mathieu-Daudé 
140686480615SPhilippe Mathieu-Daudé         if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
140786480615SPhilippe Mathieu-Daudé             ns_status = env->v7m.secure ? "S " : "NS ";
140886480615SPhilippe Mathieu-Daudé         }
140986480615SPhilippe Mathieu-Daudé 
141086480615SPhilippe Mathieu-Daudé         if (xpsr & XPSR_EXCP) {
141186480615SPhilippe Mathieu-Daudé             mode = "handler";
141286480615SPhilippe Mathieu-Daudé         } else {
141386480615SPhilippe Mathieu-Daudé             if (env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_NPRIV_MASK) {
141486480615SPhilippe Mathieu-Daudé                 mode = "unpriv-thread";
141586480615SPhilippe Mathieu-Daudé             } else {
141686480615SPhilippe Mathieu-Daudé                 mode = "priv-thread";
141786480615SPhilippe Mathieu-Daudé             }
141886480615SPhilippe Mathieu-Daudé         }
141986480615SPhilippe Mathieu-Daudé 
142086480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "XPSR=%08x %c%c%c%c %c %s%s\n",
142186480615SPhilippe Mathieu-Daudé                      xpsr,
142286480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_N ? 'N' : '-',
142386480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_Z ? 'Z' : '-',
142486480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_C ? 'C' : '-',
142586480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_V ? 'V' : '-',
142686480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_T ? 'T' : 'A',
142786480615SPhilippe Mathieu-Daudé                      ns_status,
142886480615SPhilippe Mathieu-Daudé                      mode);
142986480615SPhilippe Mathieu-Daudé     } else {
143086480615SPhilippe Mathieu-Daudé         uint32_t psr = cpsr_read(env);
143186480615SPhilippe Mathieu-Daudé         const char *ns_status = "";
143286480615SPhilippe Mathieu-Daudé 
143386480615SPhilippe Mathieu-Daudé         if (arm_feature(env, ARM_FEATURE_EL3) &&
143486480615SPhilippe Mathieu-Daudé             (psr & CPSR_M) != ARM_CPU_MODE_MON) {
143586480615SPhilippe Mathieu-Daudé             ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
143686480615SPhilippe Mathieu-Daudé         }
143786480615SPhilippe Mathieu-Daudé 
143886480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "PSR=%08x %c%c%c%c %c %s%s%d\n",
143986480615SPhilippe Mathieu-Daudé                      psr,
144086480615SPhilippe Mathieu-Daudé                      psr & CPSR_N ? 'N' : '-',
144186480615SPhilippe Mathieu-Daudé                      psr & CPSR_Z ? 'Z' : '-',
144286480615SPhilippe Mathieu-Daudé                      psr & CPSR_C ? 'C' : '-',
144386480615SPhilippe Mathieu-Daudé                      psr & CPSR_V ? 'V' : '-',
144486480615SPhilippe Mathieu-Daudé                      psr & CPSR_T ? 'T' : 'A',
144586480615SPhilippe Mathieu-Daudé                      ns_status,
144686480615SPhilippe Mathieu-Daudé                      aarch32_mode_name(psr), (psr & 0x10) ? 32 : 26);
144786480615SPhilippe Mathieu-Daudé     }
144886480615SPhilippe Mathieu-Daudé 
144986480615SPhilippe Mathieu-Daudé     if (flags & CPU_DUMP_FPU) {
145086480615SPhilippe Mathieu-Daudé         int numvfpregs = 0;
1451a6627f5fSRichard Henderson         if (cpu_isar_feature(aa32_simd_r32, cpu)) {
1452a6627f5fSRichard Henderson             numvfpregs = 32;
14537fbc6a40SRichard Henderson         } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
1454a6627f5fSRichard Henderson             numvfpregs = 16;
145586480615SPhilippe Mathieu-Daudé         }
145686480615SPhilippe Mathieu-Daudé         for (i = 0; i < numvfpregs; i++) {
145786480615SPhilippe Mathieu-Daudé             uint64_t v = *aa32_vfp_dreg(env, i);
145886480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "s%02d=%08x s%02d=%08x d%02d=%016" PRIx64 "\n",
145986480615SPhilippe Mathieu-Daudé                          i * 2, (uint32_t)v,
146086480615SPhilippe Mathieu-Daudé                          i * 2 + 1, (uint32_t)(v >> 32),
146186480615SPhilippe Mathieu-Daudé                          i, v);
146286480615SPhilippe Mathieu-Daudé         }
146386480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "FPSCR: %08x\n", vfp_get_fpscr(env));
1464aa291908SPeter Maydell         if (cpu_isar_feature(aa32_mve, cpu)) {
1465aa291908SPeter Maydell             qemu_fprintf(f, "VPR: %08x\n", env->v7m.vpr);
1466aa291908SPeter Maydell         }
146786480615SPhilippe Mathieu-Daudé     }
146886480615SPhilippe Mathieu-Daudé }
146986480615SPhilippe Mathieu-Daudé 
1470750245edSRichard Henderson uint64_t arm_build_mp_affinity(int idx, uint8_t clustersz)
147146de5913SIgor Mammedov {
147246de5913SIgor Mammedov     uint32_t Aff1 = idx / clustersz;
147346de5913SIgor Mammedov     uint32_t Aff0 = idx % clustersz;
147446de5913SIgor Mammedov     return (Aff1 << ARM_AFF1_SHIFT) | Aff0;
147546de5913SIgor Mammedov }
147646de5913SIgor Mammedov 
1477e2d8cf9bSPhilippe Mathieu-Daudé uint64_t arm_cpu_mp_affinity(ARMCPU *cpu)
1478e2d8cf9bSPhilippe Mathieu-Daudé {
1479e2d8cf9bSPhilippe Mathieu-Daudé     return cpu->mp_affinity;
1480e2d8cf9bSPhilippe Mathieu-Daudé }
1481e2d8cf9bSPhilippe Mathieu-Daudé 
1482fcf5ef2aSThomas Huth static void arm_cpu_initfn(Object *obj)
1483fcf5ef2aSThomas Huth {
1484fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(obj);
1485fcf5ef2aSThomas Huth 
14865860362dSRichard Henderson     cpu->cp_regs = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1487c27f5d3aSRichard Henderson                                          NULL, g_free);
1488fcf5ef2aSThomas Huth 
1489b5c53d1bSAaron Lindsay     QLIST_INIT(&cpu->pre_el_change_hooks);
149008267487SAaron Lindsay     QLIST_INIT(&cpu->el_change_hooks);
149108267487SAaron Lindsay 
1492b3d52804SRichard Henderson #ifdef CONFIG_USER_ONLY
1493b3d52804SRichard Henderson # ifdef TARGET_AARCH64
1494b3d52804SRichard Henderson     /*
1495e74c0976SRichard Henderson      * The linux kernel defaults to 512-bit for SVE, and 256-bit for SME.
1496e74c0976SRichard Henderson      * These values were chosen to fit within the default signal frame.
1497e74c0976SRichard Henderson      * See documentation for /proc/sys/abi/{sve,sme}_default_vector_length,
1498e74c0976SRichard Henderson      * and our corresponding cpu property.
1499b3d52804SRichard Henderson      */
1500b3d52804SRichard Henderson     cpu->sve_default_vq = 4;
1501e74c0976SRichard Henderson     cpu->sme_default_vq = 2;
1502b3d52804SRichard Henderson # endif
1503b3d52804SRichard Henderson #else
1504fcf5ef2aSThomas Huth     /* Our inbound IRQ and FIQ lines */
1505fcf5ef2aSThomas Huth     if (kvm_enabled()) {
1506b36a32eaSJinjie Ruan         /*
1507b36a32eaSJinjie Ruan          * VIRQ, VFIQ, NMI, VINMI are unused with KVM but we add
1508b36a32eaSJinjie Ruan          * them to maintain the same interface as non-KVM CPUs.
1509fcf5ef2aSThomas Huth          */
1510b36a32eaSJinjie Ruan         qdev_init_gpio_in(DEVICE(cpu), arm_cpu_kvm_set_irq, 6);
1511fcf5ef2aSThomas Huth     } else {
1512b36a32eaSJinjie Ruan         qdev_init_gpio_in(DEVICE(cpu), arm_cpu_set_irq, 6);
1513fcf5ef2aSThomas Huth     }
1514fcf5ef2aSThomas Huth 
1515fcf5ef2aSThomas Huth     qdev_init_gpio_out(DEVICE(cpu), cpu->gt_timer_outputs,
1516fcf5ef2aSThomas Huth                        ARRAY_SIZE(cpu->gt_timer_outputs));
1517aa1b3111SPeter Maydell 
1518aa1b3111SPeter Maydell     qdev_init_gpio_out_named(DEVICE(cpu), &cpu->gicv3_maintenance_interrupt,
1519aa1b3111SPeter Maydell                              "gicv3-maintenance-interrupt", 1);
152007f48730SAndrew Jones     qdev_init_gpio_out_named(DEVICE(cpu), &cpu->pmu_interrupt,
152107f48730SAndrew Jones                              "pmu-interrupt", 1);
1522fcf5ef2aSThomas Huth #endif
1523fcf5ef2aSThomas Huth 
1524fcf5ef2aSThomas Huth     /* DTB consumers generally don't in fact care what the 'compatible'
1525fcf5ef2aSThomas Huth      * string is, so always provide some string and trust that a hypothetical
1526fcf5ef2aSThomas Huth      * picky DTB consumer will also provide a helpful error message.
1527fcf5ef2aSThomas Huth      */
1528fcf5ef2aSThomas Huth     cpu->dtb_compatible = "qemu,unknown";
15290dc71c70SAkihiko Odaki     cpu->psci_version = QEMU_PSCI_VERSION_0_1; /* By default assume PSCI v0.1 */
1530fcf5ef2aSThomas Huth     cpu->kvm_target = QEMU_KVM_ARM_TARGET_NONE;
1531fcf5ef2aSThomas Huth 
15322c9c0bf9SAlexander Graf     if (tcg_enabled() || hvf_enabled()) {
15330dc71c70SAkihiko Odaki         /* TCG and HVF implement PSCI 1.1 */
15340dc71c70SAkihiko Odaki         cpu->psci_version = QEMU_PSCI_VERSION_1_1;
1535fcf5ef2aSThomas Huth     }
1536fcf5ef2aSThomas Huth }
1537fcf5ef2aSThomas Huth 
1538bd8e9ddfSPeter Maydell /*
1539bd8e9ddfSPeter Maydell  * 0 means "unset, use the default value". That default might vary depending
1540bd8e9ddfSPeter Maydell  * on the CPU type, and is set in the realize fn.
1541bd8e9ddfSPeter Maydell  */
154296eec6b2SAndrew Jeffery static Property arm_cpu_gt_cntfrq_property =
1543bd8e9ddfSPeter Maydell             DEFINE_PROP_UINT64("cntfrq", ARMCPU, gt_cntfrq_hz, 0);
154496eec6b2SAndrew Jeffery 
1545fcf5ef2aSThomas Huth static Property arm_cpu_reset_cbar_property =
1546fcf5ef2aSThomas Huth             DEFINE_PROP_UINT64("reset-cbar", ARMCPU, reset_cbar, 0);
1547fcf5ef2aSThomas Huth 
1548fcf5ef2aSThomas Huth static Property arm_cpu_reset_hivecs_property =
1549fcf5ef2aSThomas Huth             DEFINE_PROP_BOOL("reset-hivecs", ARMCPU, reset_hivecs, false);
1550fcf5ef2aSThomas Huth 
155145ca3a14SRichard Henderson #ifndef CONFIG_USER_ONLY
1552c25bd18aSPeter Maydell static Property arm_cpu_has_el2_property =
1553c25bd18aSPeter Maydell             DEFINE_PROP_BOOL("has_el2", ARMCPU, has_el2, true);
1554c25bd18aSPeter Maydell 
1555fcf5ef2aSThomas Huth static Property arm_cpu_has_el3_property =
1556fcf5ef2aSThomas Huth             DEFINE_PROP_BOOL("has_el3", ARMCPU, has_el3, true);
155745ca3a14SRichard Henderson #endif
1558fcf5ef2aSThomas Huth 
15593a062d57SJulian Brown static Property arm_cpu_cfgend_property =
15603a062d57SJulian Brown             DEFINE_PROP_BOOL("cfgend", ARMCPU, cfgend, false);
15613a062d57SJulian Brown 
156297a28b0eSPeter Maydell static Property arm_cpu_has_vfp_property =
156397a28b0eSPeter Maydell             DEFINE_PROP_BOOL("vfp", ARMCPU, has_vfp, true);
156497a28b0eSPeter Maydell 
156542bea956SCédric Le Goater static Property arm_cpu_has_vfp_d32_property =
156642bea956SCédric Le Goater             DEFINE_PROP_BOOL("vfp-d32", ARMCPU, has_vfp_d32, true);
156742bea956SCédric Le Goater 
156897a28b0eSPeter Maydell static Property arm_cpu_has_neon_property =
156997a28b0eSPeter Maydell             DEFINE_PROP_BOOL("neon", ARMCPU, has_neon, true);
157097a28b0eSPeter Maydell 
1571ea90db0aSPeter Maydell static Property arm_cpu_has_dsp_property =
1572ea90db0aSPeter Maydell             DEFINE_PROP_BOOL("dsp", ARMCPU, has_dsp, true);
1573ea90db0aSPeter Maydell 
1574fcf5ef2aSThomas Huth static Property arm_cpu_has_mpu_property =
1575fcf5ef2aSThomas Huth             DEFINE_PROP_BOOL("has-mpu", ARMCPU, has_mpu, true);
1576fcf5ef2aSThomas Huth 
15778d92e26bSPeter Maydell /* This is like DEFINE_PROP_UINT32 but it doesn't set the default value,
15788d92e26bSPeter Maydell  * because the CPU initfn will have already set cpu->pmsav7_dregion to
15798d92e26bSPeter Maydell  * the right value for that particular CPU type, and we don't want
15808d92e26bSPeter Maydell  * to override that with an incorrect constant value.
15818d92e26bSPeter Maydell  */
1582fcf5ef2aSThomas Huth static Property arm_cpu_pmsav7_dregion_property =
15838d92e26bSPeter Maydell             DEFINE_PROP_UNSIGNED_NODEFAULT("pmsav7-dregion", ARMCPU,
15848d92e26bSPeter Maydell                                            pmsav7_dregion,
15858d92e26bSPeter Maydell                                            qdev_prop_uint32, uint32_t);
1586fcf5ef2aSThomas Huth 
1587ae502508SAndrew Jones static bool arm_get_pmu(Object *obj, Error **errp)
1588ae502508SAndrew Jones {
1589ae502508SAndrew Jones     ARMCPU *cpu = ARM_CPU(obj);
1590ae502508SAndrew Jones 
1591ae502508SAndrew Jones     return cpu->has_pmu;
1592ae502508SAndrew Jones }
1593ae502508SAndrew Jones 
1594ae502508SAndrew Jones static void arm_set_pmu(Object *obj, bool value, Error **errp)
1595ae502508SAndrew Jones {
1596ae502508SAndrew Jones     ARMCPU *cpu = ARM_CPU(obj);
1597ae502508SAndrew Jones 
1598ae502508SAndrew Jones     if (value) {
15997d20e681SPhilippe Mathieu-Daudé         if (kvm_enabled() && !kvm_arm_pmu_supported()) {
1600ae502508SAndrew Jones             error_setg(errp, "'pmu' feature not supported by KVM on this host");
1601ae502508SAndrew Jones             return;
1602ae502508SAndrew Jones         }
1603ae502508SAndrew Jones         set_feature(&cpu->env, ARM_FEATURE_PMU);
1604ae502508SAndrew Jones     } else {
1605ae502508SAndrew Jones         unset_feature(&cpu->env, ARM_FEATURE_PMU);
1606ae502508SAndrew Jones     }
1607ae502508SAndrew Jones     cpu->has_pmu = value;
1608ae502508SAndrew Jones }
1609ae502508SAndrew Jones 
16107def8754SAndrew Jeffery unsigned int gt_cntfrq_period_ns(ARMCPU *cpu)
16117def8754SAndrew Jeffery {
161296eec6b2SAndrew Jeffery     /*
161396eec6b2SAndrew Jeffery      * The exact approach to calculating guest ticks is:
161496eec6b2SAndrew Jeffery      *
161596eec6b2SAndrew Jeffery      *     muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), cpu->gt_cntfrq_hz,
161696eec6b2SAndrew Jeffery      *              NANOSECONDS_PER_SECOND);
161796eec6b2SAndrew Jeffery      *
161896eec6b2SAndrew Jeffery      * We don't do that. Rather we intentionally use integer division
161996eec6b2SAndrew Jeffery      * truncation below and in the caller for the conversion of host monotonic
162096eec6b2SAndrew Jeffery      * time to guest ticks to provide the exact inverse for the semantics of
162196eec6b2SAndrew Jeffery      * the QEMUTimer scale factor. QEMUTimer's scale facter is an integer, so
162296eec6b2SAndrew Jeffery      * it loses precision when representing frequencies where
162396eec6b2SAndrew Jeffery      * `(NANOSECONDS_PER_SECOND % cpu->gt_cntfrq) > 0` holds. Failing to
162496eec6b2SAndrew Jeffery      * provide an exact inverse leads to scheduling timers with negative
162596eec6b2SAndrew Jeffery      * periods, which in turn leads to sticky behaviour in the guest.
162696eec6b2SAndrew Jeffery      *
162796eec6b2SAndrew Jeffery      * Finally, CNTFRQ is effectively capped at 1GHz to ensure our scale factor
162896eec6b2SAndrew Jeffery      * cannot become zero.
162996eec6b2SAndrew Jeffery      */
16307def8754SAndrew Jeffery     return NANOSECONDS_PER_SECOND > cpu->gt_cntfrq_hz ?
16317def8754SAndrew Jeffery       NANOSECONDS_PER_SECOND / cpu->gt_cntfrq_hz : 1;
16327def8754SAndrew Jeffery }
16337def8754SAndrew Jeffery 
1634b8f7959fSPeter Maydell static void arm_cpu_propagate_feature_implications(ARMCPU *cpu)
1635b8f7959fSPeter Maydell {
1636b8f7959fSPeter Maydell     CPUARMState *env = &cpu->env;
1637b8f7959fSPeter Maydell     bool no_aa32 = false;
1638b8f7959fSPeter Maydell 
1639b8f7959fSPeter Maydell     /*
1640b8f7959fSPeter Maydell      * Some features automatically imply others: set the feature
1641b8f7959fSPeter Maydell      * bits explicitly for these cases.
1642b8f7959fSPeter Maydell      */
1643b8f7959fSPeter Maydell 
1644b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_M)) {
1645b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_PMSA);
1646b8f7959fSPeter Maydell     }
1647b8f7959fSPeter Maydell 
1648b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V8)) {
1649b8f7959fSPeter Maydell         if (arm_feature(env, ARM_FEATURE_M)) {
1650b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_V7);
1651b8f7959fSPeter Maydell         } else {
1652b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_V7VE);
1653b8f7959fSPeter Maydell         }
1654b8f7959fSPeter Maydell     }
1655b8f7959fSPeter Maydell 
1656b8f7959fSPeter Maydell     /*
1657b8f7959fSPeter Maydell      * There exist AArch64 cpus without AArch32 support.  When KVM
1658b8f7959fSPeter Maydell      * queries ID_ISAR0_EL1 on such a host, the value is UNKNOWN.
1659b8f7959fSPeter Maydell      * Similarly, we cannot check ID_AA64PFR0 without AArch64 support.
1660b8f7959fSPeter Maydell      * As a general principle, we also do not make ID register
1661b8f7959fSPeter Maydell      * consistency checks anywhere unless using TCG, because only
1662b8f7959fSPeter Maydell      * for TCG would a consistency-check failure be a QEMU bug.
1663b8f7959fSPeter Maydell      */
1664b8f7959fSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
1665b8f7959fSPeter Maydell         no_aa32 = !cpu_isar_feature(aa64_aa32, cpu);
1666b8f7959fSPeter Maydell     }
1667b8f7959fSPeter Maydell 
1668b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V7VE)) {
1669b8f7959fSPeter Maydell         /*
1670b8f7959fSPeter Maydell          * v7 Virtualization Extensions. In real hardware this implies
1671b8f7959fSPeter Maydell          * EL2 and also the presence of the Security Extensions.
1672b8f7959fSPeter Maydell          * For QEMU, for backwards-compatibility we implement some
1673b8f7959fSPeter Maydell          * CPUs or CPU configs which have no actual EL2 or EL3 but do
1674b8f7959fSPeter Maydell          * include the various other features that V7VE implies.
1675b8f7959fSPeter Maydell          * Presence of EL2 itself is ARM_FEATURE_EL2, and of the
1676b8f7959fSPeter Maydell          * Security Extensions is ARM_FEATURE_EL3.
1677b8f7959fSPeter Maydell          */
1678b8f7959fSPeter Maydell         assert(!tcg_enabled() || no_aa32 ||
1679b8f7959fSPeter Maydell                cpu_isar_feature(aa32_arm_div, cpu));
1680b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_LPAE);
1681b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V7);
1682b8f7959fSPeter Maydell     }
1683b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V7)) {
1684b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_VAPA);
1685b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_THUMB2);
1686b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_MPIDR);
1687b8f7959fSPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
1688b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_V6K);
1689b8f7959fSPeter Maydell         } else {
1690b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_V6);
1691b8f7959fSPeter Maydell         }
1692b8f7959fSPeter Maydell 
1693b8f7959fSPeter Maydell         /*
1694b8f7959fSPeter Maydell          * Always define VBAR for V7 CPUs even if it doesn't exist in
1695b8f7959fSPeter Maydell          * non-EL3 configs. This is needed by some legacy boards.
1696b8f7959fSPeter Maydell          */
1697b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_VBAR);
1698b8f7959fSPeter Maydell     }
1699b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V6K)) {
1700b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V6);
1701b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_MVFR);
1702b8f7959fSPeter Maydell     }
1703b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V6)) {
1704b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V5);
1705b8f7959fSPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
1706b8f7959fSPeter Maydell             assert(!tcg_enabled() || no_aa32 ||
1707b8f7959fSPeter Maydell                    cpu_isar_feature(aa32_jazelle, cpu));
1708b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_AUXCR);
1709b8f7959fSPeter Maydell         }
1710b8f7959fSPeter Maydell     }
1711b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V5)) {
1712b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V4T);
1713b8f7959fSPeter Maydell     }
1714b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_LPAE)) {
1715b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V7MP);
1716b8f7959fSPeter Maydell     }
1717b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
1718b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_CBAR);
1719b8f7959fSPeter Maydell     }
1720b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_THUMB2) &&
1721b8f7959fSPeter Maydell         !arm_feature(env, ARM_FEATURE_M)) {
1722b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_THUMB_DSP);
1723b8f7959fSPeter Maydell     }
1724b8f7959fSPeter Maydell }
1725b8f7959fSPeter Maydell 
172651e5ef45SMarc-André Lureau void arm_cpu_post_init(Object *obj)
1727fcf5ef2aSThomas Huth {
1728fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(obj);
1729fcf5ef2aSThomas Huth 
1730b8f7959fSPeter Maydell     /*
1731b8f7959fSPeter Maydell      * Some features imply others. Figure this out now, because we
1732b8f7959fSPeter Maydell      * are going to look at the feature bits in deciding which
1733b8f7959fSPeter Maydell      * properties to add.
1734790a1150SPeter Maydell      */
1735b8f7959fSPeter Maydell     arm_cpu_propagate_feature_implications(cpu);
1736790a1150SPeter Maydell 
1737fcf5ef2aSThomas Huth     if (arm_feature(&cpu->env, ARM_FEATURE_CBAR) ||
1738fcf5ef2aSThomas Huth         arm_feature(&cpu->env, ARM_FEATURE_CBAR_RO)) {
173994d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_cbar_property);
1740fcf5ef2aSThomas Huth     }
1741fcf5ef2aSThomas Huth 
1742fcf5ef2aSThomas Huth     if (!arm_feature(&cpu->env, ARM_FEATURE_M)) {
174394d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_hivecs_property);
1744fcf5ef2aSThomas Huth     }
1745fcf5ef2aSThomas Huth 
1746910e4f24STobias Röhmel     if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
17474a7319b7SEdgar E. Iglesias         object_property_add_uint64_ptr(obj, "rvbar",
17484a7319b7SEdgar E. Iglesias                                        &cpu->rvbar_prop,
17494a7319b7SEdgar E. Iglesias                                        OBJ_PROP_FLAG_READWRITE);
1750fcf5ef2aSThomas Huth     }
1751fcf5ef2aSThomas Huth 
175245ca3a14SRichard Henderson #ifndef CONFIG_USER_ONLY
1753fcf5ef2aSThomas Huth     if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
1754fcf5ef2aSThomas Huth         /* Add the has_el3 state CPU property only if EL3 is allowed.  This will
1755fcf5ef2aSThomas Huth          * prevent "has_el3" from existing on CPUs which cannot support EL3.
1756fcf5ef2aSThomas Huth          */
175794d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el3_property);
1758fcf5ef2aSThomas Huth 
1759fcf5ef2aSThomas Huth         object_property_add_link(obj, "secure-memory",
1760fcf5ef2aSThomas Huth                                  TYPE_MEMORY_REGION,
1761fcf5ef2aSThomas Huth                                  (Object **)&cpu->secure_memory,
1762fcf5ef2aSThomas Huth                                  qdev_prop_allow_set_link_before_realize,
1763d2623129SMarkus Armbruster                                  OBJ_PROP_LINK_STRONG);
1764fcf5ef2aSThomas Huth     }
1765fcf5ef2aSThomas Huth 
1766c25bd18aSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_EL2)) {
176794d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el2_property);
1768c25bd18aSPeter Maydell     }
176945ca3a14SRichard Henderson #endif
1770c25bd18aSPeter Maydell 
1771fcf5ef2aSThomas Huth     if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) {
1772ae502508SAndrew Jones         cpu->has_pmu = true;
1773d2623129SMarkus Armbruster         object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu);
1774fcf5ef2aSThomas Huth     }
1775fcf5ef2aSThomas Huth 
177697a28b0eSPeter Maydell     /*
177797a28b0eSPeter Maydell      * Allow user to turn off VFP and Neon support, but only for TCG --
177897a28b0eSPeter Maydell      * KVM does not currently allow us to lie to the guest about its
177997a28b0eSPeter Maydell      * ID/feature registers, so the guest always sees what the host has.
178097a28b0eSPeter Maydell      */
17814315f7c6SRichard Henderson     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
17824315f7c6SRichard Henderson         if (cpu_isar_feature(aa64_fp_simd, cpu)) {
178397a28b0eSPeter Maydell             cpu->has_vfp = true;
178442bea956SCédric Le Goater             cpu->has_vfp_d32 = true;
17854315f7c6SRichard Henderson             if (tcg_enabled() || qtest_enabled()) {
17864315f7c6SRichard Henderson                 qdev_property_add_static(DEVICE(obj),
17874315f7c6SRichard Henderson                                          &arm_cpu_has_vfp_property);
17884315f7c6SRichard Henderson             }
17894315f7c6SRichard Henderson         }
17904315f7c6SRichard Henderson     } else if (cpu_isar_feature(aa32_vfp, cpu)) {
17914315f7c6SRichard Henderson         cpu->has_vfp = true;
1792185e3fdfSPeter Maydell         if (tcg_enabled() || qtest_enabled()) {
1793185e3fdfSPeter Maydell             qdev_property_add_static(DEVICE(obj),
1794185e3fdfSPeter Maydell                                      &arm_cpu_has_vfp_property);
1795185e3fdfSPeter Maydell         }
17964315f7c6SRichard Henderson         if (cpu_isar_feature(aa32_simd_r32, cpu)) {
17974315f7c6SRichard Henderson             cpu->has_vfp_d32 = true;
179842bea956SCédric Le Goater             /*
179942bea956SCédric Le Goater              * The permitted values of the SIMDReg bits [3:0] on
180042bea956SCédric Le Goater              * Armv8-A are either 0b0000 and 0b0010. On such CPUs,
180142bea956SCédric Le Goater              * make sure that has_vfp_d32 can not be set to false.
180242bea956SCédric Le Goater              */
18034315f7c6SRichard Henderson             if ((tcg_enabled() || qtest_enabled())
18044315f7c6SRichard Henderson                 && !(arm_feature(&cpu->env, ARM_FEATURE_V8)
18054315f7c6SRichard Henderson                      && !arm_feature(&cpu->env, ARM_FEATURE_M))) {
180642bea956SCédric Le Goater                 qdev_property_add_static(DEVICE(obj),
180742bea956SCédric Le Goater                                          &arm_cpu_has_vfp_d32_property);
180842bea956SCédric Le Goater             }
180942bea956SCédric Le Goater         }
181042bea956SCédric Le Goater     }
181142bea956SCédric Le Goater 
181297a28b0eSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_NEON)) {
181397a28b0eSPeter Maydell         cpu->has_neon = true;
181497a28b0eSPeter Maydell         if (!kvm_enabled()) {
181594d912d1SMarc-André Lureau             qdev_property_add_static(DEVICE(obj), &arm_cpu_has_neon_property);
181697a28b0eSPeter Maydell         }
181797a28b0eSPeter Maydell     }
181897a28b0eSPeter Maydell 
1819ea90db0aSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_M) &&
1820ea90db0aSPeter Maydell         arm_feature(&cpu->env, ARM_FEATURE_THUMB_DSP)) {
182194d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_dsp_property);
1822ea90db0aSPeter Maydell     }
1823ea90db0aSPeter Maydell 
1824452a0955SPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_PMSA)) {
182594d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_mpu_property);
1826fcf5ef2aSThomas Huth         if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
1827fcf5ef2aSThomas Huth             qdev_property_add_static(DEVICE(obj),
182894d912d1SMarc-André Lureau                                      &arm_cpu_pmsav7_dregion_property);
1829fcf5ef2aSThomas Huth         }
1830fcf5ef2aSThomas Huth     }
1831fcf5ef2aSThomas Huth 
1832181962fdSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_M_SECURITY)) {
1833181962fdSPeter Maydell         object_property_add_link(obj, "idau", TYPE_IDAU_INTERFACE, &cpu->idau,
1834181962fdSPeter Maydell                                  qdev_prop_allow_set_link_before_realize,
1835d2623129SMarkus Armbruster                                  OBJ_PROP_LINK_STRONG);
1836f9f62e4cSPeter Maydell         /*
1837f9f62e4cSPeter Maydell          * M profile: initial value of the Secure VTOR. We can't just use
1838f9f62e4cSPeter Maydell          * a simple DEFINE_PROP_UINT32 for this because we want to permit
1839f9f62e4cSPeter Maydell          * the property to be set after realize.
1840f9f62e4cSPeter Maydell          */
184164a7b8deSFelipe Franciosi         object_property_add_uint32_ptr(obj, "init-svtor",
184264a7b8deSFelipe Franciosi                                        &cpu->init_svtor,
1843d2623129SMarkus Armbruster                                        OBJ_PROP_FLAG_READWRITE);
1844181962fdSPeter Maydell     }
18457cda2149SPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_M)) {
18467cda2149SPeter Maydell         /*
18477cda2149SPeter Maydell          * Initial value of the NS VTOR (for cores without the Security
18487cda2149SPeter Maydell          * extension, this is the only VTOR)
18497cda2149SPeter Maydell          */
18507cda2149SPeter Maydell         object_property_add_uint32_ptr(obj, "init-nsvtor",
18517cda2149SPeter Maydell                                        &cpu->init_nsvtor,
18527cda2149SPeter Maydell                                        OBJ_PROP_FLAG_READWRITE);
18537cda2149SPeter Maydell     }
1854181962fdSPeter Maydell 
1855bddd892eSPeter Maydell     /* Not DEFINE_PROP_UINT32: we want this to be settable after realize */
1856bddd892eSPeter Maydell     object_property_add_uint32_ptr(obj, "psci-conduit",
1857bddd892eSPeter Maydell                                    &cpu->psci_conduit,
1858bddd892eSPeter Maydell                                    OBJ_PROP_FLAG_READWRITE);
1859bddd892eSPeter Maydell 
186094d912d1SMarc-André Lureau     qdev_property_add_static(DEVICE(obj), &arm_cpu_cfgend_property);
186196eec6b2SAndrew Jeffery 
186296eec6b2SAndrew Jeffery     if (arm_feature(&cpu->env, ARM_FEATURE_GENERIC_TIMER)) {
186394d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(cpu), &arm_cpu_gt_cntfrq_property);
186496eec6b2SAndrew Jeffery     }
18659e6f8d8aSfangying 
18669e6f8d8aSfangying     if (kvm_enabled()) {
1867cac675b5SPhilippe Mathieu-Daudé         kvm_arm_add_vcpu_properties(cpu);
18689e6f8d8aSfangying     }
18698bce44a2SRichard Henderson 
18708bce44a2SRichard Henderson #ifndef CONFIG_USER_ONLY
18718bce44a2SRichard Henderson     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) &&
18728bce44a2SRichard Henderson         cpu_isar_feature(aa64_mte, cpu)) {
18738bce44a2SRichard Henderson         object_property_add_link(obj, "tag-memory",
18748bce44a2SRichard Henderson                                  TYPE_MEMORY_REGION,
18758bce44a2SRichard Henderson                                  (Object **)&cpu->tag_memory,
18768bce44a2SRichard Henderson                                  qdev_prop_allow_set_link_before_realize,
18778bce44a2SRichard Henderson                                  OBJ_PROP_LINK_STRONG);
18788bce44a2SRichard Henderson 
18798bce44a2SRichard Henderson         if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
18808bce44a2SRichard Henderson             object_property_add_link(obj, "secure-tag-memory",
18818bce44a2SRichard Henderson                                      TYPE_MEMORY_REGION,
18828bce44a2SRichard Henderson                                      (Object **)&cpu->secure_tag_memory,
18838bce44a2SRichard Henderson                                      qdev_prop_allow_set_link_before_realize,
18848bce44a2SRichard Henderson                                      OBJ_PROP_LINK_STRONG);
18858bce44a2SRichard Henderson         }
18868bce44a2SRichard Henderson     }
18878bce44a2SRichard Henderson #endif
1888fcf5ef2aSThomas Huth }
1889fcf5ef2aSThomas Huth 
1890fcf5ef2aSThomas Huth static void arm_cpu_finalizefn(Object *obj)
1891fcf5ef2aSThomas Huth {
1892fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(obj);
189308267487SAaron Lindsay     ARMELChangeHook *hook, *next;
189408267487SAaron Lindsay 
1895fcf5ef2aSThomas Huth     g_hash_table_destroy(cpu->cp_regs);
189608267487SAaron Lindsay 
1897b5c53d1bSAaron Lindsay     QLIST_FOREACH_SAFE(hook, &cpu->pre_el_change_hooks, node, next) {
1898b5c53d1bSAaron Lindsay         QLIST_REMOVE(hook, node);
1899b5c53d1bSAaron Lindsay         g_free(hook);
1900b5c53d1bSAaron Lindsay     }
190108267487SAaron Lindsay     QLIST_FOREACH_SAFE(hook, &cpu->el_change_hooks, node, next) {
190208267487SAaron Lindsay         QLIST_REMOVE(hook, node);
190308267487SAaron Lindsay         g_free(hook);
190408267487SAaron Lindsay     }
19054e7beb0cSAaron Lindsay OS #ifndef CONFIG_USER_ONLY
19064e7beb0cSAaron Lindsay OS     if (cpu->pmu_timer) {
19074e7beb0cSAaron Lindsay OS         timer_free(cpu->pmu_timer);
19084e7beb0cSAaron Lindsay OS     }
1909*a96edb68SPeter Maydell     if (cpu->wfxt_timer) {
1910*a96edb68SPeter Maydell         timer_free(cpu->wfxt_timer);
1911*a96edb68SPeter Maydell     }
19124e7beb0cSAaron Lindsay OS #endif
1913fcf5ef2aSThomas Huth }
1914fcf5ef2aSThomas Huth 
19150df9142dSAndrew Jones void arm_cpu_finalize_features(ARMCPU *cpu, Error **errp)
19160df9142dSAndrew Jones {
19170df9142dSAndrew Jones     Error *local_err = NULL;
19180df9142dSAndrew Jones 
191907301161SRichard Henderson #ifdef TARGET_AARCH64
19200df9142dSAndrew Jones     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
19210df9142dSAndrew Jones         arm_cpu_sve_finalize(cpu, &local_err);
19220df9142dSAndrew Jones         if (local_err != NULL) {
19230df9142dSAndrew Jones             error_propagate(errp, local_err);
19240df9142dSAndrew Jones             return;
19250df9142dSAndrew Jones         }
1926eb94284dSRichard Henderson 
1927f7767ca3SPeter Maydell         /*
1928f7767ca3SPeter Maydell          * FEAT_SME is not architecturally dependent on FEAT_SVE (unless
1929f7767ca3SPeter Maydell          * FEAT_SME_FA64 is present). However our implementation currently
1930f7767ca3SPeter Maydell          * assumes it, so if the user asked for sve=off then turn off SME also.
1931f7767ca3SPeter Maydell          * (KVM doesn't currently support SME at all.)
1932f7767ca3SPeter Maydell          */
1933f7767ca3SPeter Maydell         if (cpu_isar_feature(aa64_sme, cpu) && !cpu_isar_feature(aa64_sve, cpu)) {
1934f7767ca3SPeter Maydell             object_property_set_bool(OBJECT(cpu), "sme", false, &error_abort);
1935f7767ca3SPeter Maydell         }
1936f7767ca3SPeter Maydell 
1937e74c0976SRichard Henderson         arm_cpu_sme_finalize(cpu, &local_err);
1938e74c0976SRichard Henderson         if (local_err != NULL) {
1939e74c0976SRichard Henderson             error_propagate(errp, local_err);
1940e74c0976SRichard Henderson             return;
1941e74c0976SRichard Henderson         }
1942e74c0976SRichard Henderson 
1943eb94284dSRichard Henderson         arm_cpu_pauth_finalize(cpu, &local_err);
1944eb94284dSRichard Henderson         if (local_err != NULL) {
1945eb94284dSRichard Henderson             error_propagate(errp, local_err);
1946eb94284dSRichard Henderson             return;
1947eb94284dSRichard Henderson         }
194869b2265dSRichard Henderson 
194969b2265dSRichard Henderson         arm_cpu_lpa2_finalize(cpu, &local_err);
195069b2265dSRichard Henderson         if (local_err != NULL) {
195169b2265dSRichard Henderson             error_propagate(errp, local_err);
195269b2265dSRichard Henderson             return;
195369b2265dSRichard Henderson         }
1954eb94284dSRichard Henderson     }
195507301161SRichard Henderson #endif
195668970d1eSAndrew Jones 
195768970d1eSAndrew Jones     if (kvm_enabled()) {
195868970d1eSAndrew Jones         kvm_arm_steal_time_finalize(cpu, &local_err);
195968970d1eSAndrew Jones         if (local_err != NULL) {
196068970d1eSAndrew Jones             error_propagate(errp, local_err);
196168970d1eSAndrew Jones             return;
196268970d1eSAndrew Jones         }
196368970d1eSAndrew Jones     }
19640df9142dSAndrew Jones }
19650df9142dSAndrew Jones 
1966fcf5ef2aSThomas Huth static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
1967fcf5ef2aSThomas Huth {
1968fcf5ef2aSThomas Huth     CPUState *cs = CPU(dev);
1969fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(dev);
1970fcf5ef2aSThomas Huth     ARMCPUClass *acc = ARM_CPU_GET_CLASS(dev);
1971fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
1972fcf5ef2aSThomas Huth     Error *local_err = NULL;
1973fcf5ef2aSThomas Huth 
1974b94b8c60SPhilippe Mathieu-Daudé #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
1975e607ea39SAnton Johansson     /* Use pc-relative instructions in system-mode */
1976b254c342SPhilippe Mathieu-Daudé     tcg_cflags_set(cs, CF_PCREL);
1977e607ea39SAnton Johansson #endif
1978e607ea39SAnton Johansson 
1979c4487d76SPeter Maydell     /* If we needed to query the host kernel for the CPU features
1980c4487d76SPeter Maydell      * then it's possible that might have failed in the initfn, but
1981c4487d76SPeter Maydell      * this is the first point where we can report it.
1982c4487d76SPeter Maydell      */
1983c4487d76SPeter Maydell     if (cpu->host_cpu_probe_failed) {
1984585df85eSPeter Maydell         if (!kvm_enabled() && !hvf_enabled()) {
1985585df85eSPeter Maydell             error_setg(errp, "The 'host' CPU type can only be used with KVM or HVF");
1986c4487d76SPeter Maydell         } else {
1987c4487d76SPeter Maydell             error_setg(errp, "Failed to retrieve host CPU features");
1988c4487d76SPeter Maydell         }
1989c4487d76SPeter Maydell         return;
1990c4487d76SPeter Maydell     }
1991c4487d76SPeter Maydell 
1992bd8e9ddfSPeter Maydell     if (!cpu->gt_cntfrq_hz) {
1993bd8e9ddfSPeter Maydell         /*
1994f037f5b4SPeter Maydell          * 0 means "the board didn't set a value, use the default". (We also
1995f037f5b4SPeter Maydell          * get here for the CONFIG_USER_ONLY case.)
1996f037f5b4SPeter Maydell          * ARMv8.6 and later CPUs architecturally must use a 1GHz timer; before
1997f037f5b4SPeter Maydell          * that it was an IMPDEF choice, and QEMU initially picked 62.5MHz,
1998f037f5b4SPeter Maydell          * which gives a 16ns tick period.
1999f037f5b4SPeter Maydell          *
2000f037f5b4SPeter Maydell          * We will use the back-compat value:
2001f037f5b4SPeter Maydell          *  - for QEMU CPU types added before we standardized on 1GHz
2002f037f5b4SPeter Maydell          *  - for versioned machine types with a version of 9.0 or earlier
2003bd8e9ddfSPeter Maydell          */
2004f037f5b4SPeter Maydell         if (arm_feature(env, ARM_FEATURE_BACKCOMPAT_CNTFRQ) ||
2005f037f5b4SPeter Maydell             cpu->backcompat_cntfrq) {
2006f037f5b4SPeter Maydell             cpu->gt_cntfrq_hz = GTIMER_BACKCOMPAT_HZ;
2007f037f5b4SPeter Maydell         } else {
2008bd8e9ddfSPeter Maydell             cpu->gt_cntfrq_hz = GTIMER_DEFAULT_HZ;
2009bd8e9ddfSPeter Maydell         }
2010f037f5b4SPeter Maydell     }
2011bd8e9ddfSPeter Maydell 
201295f87565SPeter Maydell #ifndef CONFIG_USER_ONLY
201395f87565SPeter Maydell     /* The NVIC and M-profile CPU are two halves of a single piece of
201495f87565SPeter Maydell      * hardware; trying to use one without the other is a command line
201595f87565SPeter Maydell      * error and will result in segfaults if not caught here.
201695f87565SPeter Maydell      */
201795f87565SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M)) {
201895f87565SPeter Maydell         if (!env->nvic) {
201995f87565SPeter Maydell             error_setg(errp, "This board cannot be used with Cortex-M CPUs");
202095f87565SPeter Maydell             return;
202195f87565SPeter Maydell         }
202295f87565SPeter Maydell     } else {
202395f87565SPeter Maydell         if (env->nvic) {
202495f87565SPeter Maydell             error_setg(errp, "This board can only be used with Cortex-M CPUs");
202595f87565SPeter Maydell             return;
202695f87565SPeter Maydell         }
202795f87565SPeter Maydell     }
2028397cd31fSPeter Maydell 
2029045e5064SAlexander Graf     if (!tcg_enabled() && !qtest_enabled()) {
203049e7f191SPeter Maydell         /*
2031045e5064SAlexander Graf          * We assume that no accelerator except TCG (and the "not really an
2032045e5064SAlexander Graf          * accelerator" qtest) can handle these features, because Arm hardware
2033045e5064SAlexander Graf          * virtualization can't virtualize them.
2034045e5064SAlexander Graf          *
203549e7f191SPeter Maydell          * Catch all the cases which might cause us to create more than one
203649e7f191SPeter Maydell          * address space for the CPU (otherwise we will assert() later in
203749e7f191SPeter Maydell          * cpu_address_space_init()).
203849e7f191SPeter Maydell          */
203949e7f191SPeter Maydell         if (arm_feature(env, ARM_FEATURE_M)) {
204049e7f191SPeter Maydell             error_setg(errp,
2041045e5064SAlexander Graf                        "Cannot enable %s when using an M-profile guest CPU",
2042045e5064SAlexander Graf                        current_accel_name());
204349e7f191SPeter Maydell             return;
204449e7f191SPeter Maydell         }
204549e7f191SPeter Maydell         if (cpu->has_el3) {
204649e7f191SPeter Maydell             error_setg(errp,
2047045e5064SAlexander Graf                        "Cannot enable %s when guest CPU has EL3 enabled",
2048045e5064SAlexander Graf                        current_accel_name());
204949e7f191SPeter Maydell             return;
205049e7f191SPeter Maydell         }
205149e7f191SPeter Maydell         if (cpu->tag_memory) {
205249e7f191SPeter Maydell             error_setg(errp,
2053d009607dSPeter Maydell                        "Cannot enable %s when guest CPUs has MTE enabled",
2054045e5064SAlexander Graf                        current_accel_name());
205549e7f191SPeter Maydell             return;
205649e7f191SPeter Maydell         }
205749e7f191SPeter Maydell     }
205849e7f191SPeter Maydell 
205996eec6b2SAndrew Jeffery     {
2060bd8e9ddfSPeter Maydell         uint64_t scale = gt_cntfrq_period_ns(cpu);
206196eec6b2SAndrew Jeffery 
206296eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_PHYS] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
2063397cd31fSPeter Maydell                                                arm_gt_ptimer_cb, cpu);
206496eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_VIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
2065397cd31fSPeter Maydell                                                arm_gt_vtimer_cb, cpu);
206696eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_HYP] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
2067397cd31fSPeter Maydell                                               arm_gt_htimer_cb, cpu);
206896eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_SEC] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
2069397cd31fSPeter Maydell                                               arm_gt_stimer_cb, cpu);
20708c94b071SRichard Henderson         cpu->gt_timer[GTIMER_HYPVIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
20718c94b071SRichard Henderson                                                   arm_gt_hvtimer_cb, cpu);
207296eec6b2SAndrew Jeffery     }
207395f87565SPeter Maydell #endif
207495f87565SPeter Maydell 
2075fcf5ef2aSThomas Huth     cpu_exec_realizefn(cs, &local_err);
2076fcf5ef2aSThomas Huth     if (local_err != NULL) {
2077fcf5ef2aSThomas Huth         error_propagate(errp, local_err);
2078fcf5ef2aSThomas Huth         return;
2079fcf5ef2aSThomas Huth     }
2080fcf5ef2aSThomas Huth 
20810df9142dSAndrew Jones     arm_cpu_finalize_features(cpu, &local_err);
20820df9142dSAndrew Jones     if (local_err != NULL) {
20830df9142dSAndrew Jones         error_propagate(errp, local_err);
20840df9142dSAndrew Jones         return;
20850df9142dSAndrew Jones     }
20860df9142dSAndrew Jones 
20879719f125SJohn Högberg #ifdef CONFIG_USER_ONLY
20889719f125SJohn Högberg     /*
20899719f125SJohn Högberg      * User mode relies on IC IVAU instructions to catch modification of
20909719f125SJohn Högberg      * dual-mapped code.
20919719f125SJohn Högberg      *
20929719f125SJohn Högberg      * Clear CTR_EL0.DIC to ensure that software that honors these flags uses
20939719f125SJohn Högberg      * IC IVAU even if the emulated processor does not normally require it.
20949719f125SJohn Högberg      */
20959719f125SJohn Högberg     cpu->ctr = FIELD_DP64(cpu->ctr, CTR_EL0, DIC, 0);
20969719f125SJohn Högberg #endif
20979719f125SJohn Högberg 
209897a28b0eSPeter Maydell     if (arm_feature(env, ARM_FEATURE_AARCH64) &&
209997a28b0eSPeter Maydell         cpu->has_vfp != cpu->has_neon) {
210097a28b0eSPeter Maydell         /*
210197a28b0eSPeter Maydell          * This is an architectural requirement for AArch64; AArch32 is
210297a28b0eSPeter Maydell          * more flexible and permits VFP-no-Neon and Neon-no-VFP.
210397a28b0eSPeter Maydell          */
210497a28b0eSPeter Maydell         error_setg(errp,
210597a28b0eSPeter Maydell                    "AArch64 CPUs must have both VFP and Neon or neither");
210697a28b0eSPeter Maydell         return;
210797a28b0eSPeter Maydell     }
210897a28b0eSPeter Maydell 
210942bea956SCédric Le Goater     if (cpu->has_vfp_d32 != cpu->has_neon) {
211042bea956SCédric Le Goater         error_setg(errp, "ARM CPUs must have both VFP-D32 and Neon or neither");
211142bea956SCédric Le Goater         return;
211242bea956SCédric Le Goater     }
211342bea956SCédric Le Goater 
211442bea956SCédric Le Goater    if (!cpu->has_vfp_d32) {
211542bea956SCédric Le Goater         uint32_t u;
211642bea956SCédric Le Goater 
211742bea956SCédric Le Goater         u = cpu->isar.mvfr0;
211842bea956SCédric Le Goater         u = FIELD_DP32(u, MVFR0, SIMDREG, 1); /* 16 registers */
211942bea956SCédric Le Goater         cpu->isar.mvfr0 = u;
212042bea956SCédric Le Goater     }
212142bea956SCédric Le Goater 
212297a28b0eSPeter Maydell     if (!cpu->has_vfp) {
212397a28b0eSPeter Maydell         uint64_t t;
212497a28b0eSPeter Maydell         uint32_t u;
212597a28b0eSPeter Maydell 
212697a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar1;
212797a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR1, JSCVT, 0);
212897a28b0eSPeter Maydell         cpu->isar.id_aa64isar1 = t;
212997a28b0eSPeter Maydell 
213097a28b0eSPeter Maydell         t = cpu->isar.id_aa64pfr0;
213197a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64PFR0, FP, 0xf);
213297a28b0eSPeter Maydell         cpu->isar.id_aa64pfr0 = t;
213397a28b0eSPeter Maydell 
213497a28b0eSPeter Maydell         u = cpu->isar.id_isar6;
213597a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR6, JSCVT, 0);
21363c93dfa4SRichard Henderson         u = FIELD_DP32(u, ID_ISAR6, BF16, 0);
213797a28b0eSPeter Maydell         cpu->isar.id_isar6 = u;
213897a28b0eSPeter Maydell 
213997a28b0eSPeter Maydell         u = cpu->isar.mvfr0;
214097a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPSP, 0);
214197a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPDP, 0);
214297a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPDIVIDE, 0);
214397a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPSQRT, 0);
214497a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPROUND, 0);
2145532a3af5SPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
2146532a3af5SPeter Maydell             u = FIELD_DP32(u, MVFR0, FPTRAP, 0);
2147532a3af5SPeter Maydell             u = FIELD_DP32(u, MVFR0, FPSHVEC, 0);
2148532a3af5SPeter Maydell         }
214997a28b0eSPeter Maydell         cpu->isar.mvfr0 = u;
215097a28b0eSPeter Maydell 
215197a28b0eSPeter Maydell         u = cpu->isar.mvfr1;
215297a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR1, FPFTZ, 0);
215397a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR1, FPDNAN, 0);
215497a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR1, FPHP, 0);
2155532a3af5SPeter Maydell         if (arm_feature(env, ARM_FEATURE_M)) {
2156532a3af5SPeter Maydell             u = FIELD_DP32(u, MVFR1, FP16, 0);
2157532a3af5SPeter Maydell         }
215897a28b0eSPeter Maydell         cpu->isar.mvfr1 = u;
215997a28b0eSPeter Maydell 
216097a28b0eSPeter Maydell         u = cpu->isar.mvfr2;
216197a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR2, FPMISC, 0);
216297a28b0eSPeter Maydell         cpu->isar.mvfr2 = u;
216397a28b0eSPeter Maydell     }
216497a28b0eSPeter Maydell 
216597a28b0eSPeter Maydell     if (!cpu->has_neon) {
216697a28b0eSPeter Maydell         uint64_t t;
216797a28b0eSPeter Maydell         uint32_t u;
216897a28b0eSPeter Maydell 
216997a28b0eSPeter Maydell         unset_feature(env, ARM_FEATURE_NEON);
217097a28b0eSPeter Maydell 
217197a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar0;
2172eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, AES, 0);
2173eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SHA1, 0);
2174eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SHA2, 0);
2175eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SHA3, 0);
2176eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SM3, 0);
2177eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SM4, 0);
217897a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR0, DP, 0);
217997a28b0eSPeter Maydell         cpu->isar.id_aa64isar0 = t;
218097a28b0eSPeter Maydell 
218197a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar1;
218297a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR1, FCMA, 0);
21833c93dfa4SRichard Henderson         t = FIELD_DP64(t, ID_AA64ISAR1, BF16, 0);
2184f8680aaaSRichard Henderson         t = FIELD_DP64(t, ID_AA64ISAR1, I8MM, 0);
218597a28b0eSPeter Maydell         cpu->isar.id_aa64isar1 = t;
218697a28b0eSPeter Maydell 
218797a28b0eSPeter Maydell         t = cpu->isar.id_aa64pfr0;
218897a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64PFR0, ADVSIMD, 0xf);
218997a28b0eSPeter Maydell         cpu->isar.id_aa64pfr0 = t;
219097a28b0eSPeter Maydell 
219197a28b0eSPeter Maydell         u = cpu->isar.id_isar5;
2192eb851c11SDamien Hedde         u = FIELD_DP32(u, ID_ISAR5, AES, 0);
2193eb851c11SDamien Hedde         u = FIELD_DP32(u, ID_ISAR5, SHA1, 0);
2194eb851c11SDamien Hedde         u = FIELD_DP32(u, ID_ISAR5, SHA2, 0);
219597a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR5, RDM, 0);
219697a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR5, VCMA, 0);
219797a28b0eSPeter Maydell         cpu->isar.id_isar5 = u;
219897a28b0eSPeter Maydell 
219997a28b0eSPeter Maydell         u = cpu->isar.id_isar6;
220097a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR6, DP, 0);
220197a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR6, FHM, 0);
22023c93dfa4SRichard Henderson         u = FIELD_DP32(u, ID_ISAR6, BF16, 0);
2203f8680aaaSRichard Henderson         u = FIELD_DP32(u, ID_ISAR6, I8MM, 0);
220497a28b0eSPeter Maydell         cpu->isar.id_isar6 = u;
220597a28b0eSPeter Maydell 
2206532a3af5SPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
220797a28b0eSPeter Maydell             u = cpu->isar.mvfr1;
220897a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDLS, 0);
220997a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDINT, 0);
221097a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDSP, 0);
221197a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDHP, 0);
221297a28b0eSPeter Maydell             cpu->isar.mvfr1 = u;
221397a28b0eSPeter Maydell 
221497a28b0eSPeter Maydell             u = cpu->isar.mvfr2;
221597a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR2, SIMDMISC, 0);
221697a28b0eSPeter Maydell             cpu->isar.mvfr2 = u;
221797a28b0eSPeter Maydell         }
2218532a3af5SPeter Maydell     }
221997a28b0eSPeter Maydell 
222097a28b0eSPeter Maydell     if (!cpu->has_neon && !cpu->has_vfp) {
222197a28b0eSPeter Maydell         uint64_t t;
222297a28b0eSPeter Maydell         uint32_t u;
222397a28b0eSPeter Maydell 
222497a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar0;
222597a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR0, FHM, 0);
222697a28b0eSPeter Maydell         cpu->isar.id_aa64isar0 = t;
222797a28b0eSPeter Maydell 
222897a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar1;
222997a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR1, FRINTTS, 0);
223097a28b0eSPeter Maydell         cpu->isar.id_aa64isar1 = t;
223197a28b0eSPeter Maydell 
223297a28b0eSPeter Maydell         u = cpu->isar.mvfr0;
223397a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, SIMDREG, 0);
223497a28b0eSPeter Maydell         cpu->isar.mvfr0 = u;
2235c52881bbSRichard Henderson 
2236c52881bbSRichard Henderson         /* Despite the name, this field covers both VFP and Neon */
2237c52881bbSRichard Henderson         u = cpu->isar.mvfr1;
2238c52881bbSRichard Henderson         u = FIELD_DP32(u, MVFR1, SIMDFMAC, 0);
2239c52881bbSRichard Henderson         cpu->isar.mvfr1 = u;
224097a28b0eSPeter Maydell     }
224197a28b0eSPeter Maydell 
2242ea90db0aSPeter Maydell     if (arm_feature(env, ARM_FEATURE_M) && !cpu->has_dsp) {
2243ea90db0aSPeter Maydell         uint32_t u;
2244ea90db0aSPeter Maydell 
2245ea90db0aSPeter Maydell         unset_feature(env, ARM_FEATURE_THUMB_DSP);
2246ea90db0aSPeter Maydell 
2247ea90db0aSPeter Maydell         u = cpu->isar.id_isar1;
2248ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR1, EXTEND, 1);
2249ea90db0aSPeter Maydell         cpu->isar.id_isar1 = u;
2250ea90db0aSPeter Maydell 
2251ea90db0aSPeter Maydell         u = cpu->isar.id_isar2;
2252ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR2, MULTU, 1);
2253ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR2, MULTS, 1);
2254ea90db0aSPeter Maydell         cpu->isar.id_isar2 = u;
2255ea90db0aSPeter Maydell 
2256ea90db0aSPeter Maydell         u = cpu->isar.id_isar3;
2257ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR3, SIMD, 1);
2258ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR3, SATURATE, 0);
2259ea90db0aSPeter Maydell         cpu->isar.id_isar3 = u;
2260ea90db0aSPeter Maydell     }
2261ea90db0aSPeter Maydell 
2262fcf5ef2aSThomas Huth 
2263ea7ac69dSPeter Maydell     /*
2264ea7ac69dSPeter Maydell      * We rely on no XScale CPU having VFP so we can use the same bits in the
2265ea7ac69dSPeter Maydell      * TB flags field for VECSTRIDE and XSCALE_CPAR.
2266ea7ac69dSPeter Maydell      */
2267ee1004bbSPhilippe Mathieu-Daudé     assert(arm_feature(env, ARM_FEATURE_AARCH64) ||
22687d63183fSRichard Henderson            !cpu_isar_feature(aa32_vfp_simd, cpu) ||
22697d63183fSRichard Henderson            !arm_feature(env, ARM_FEATURE_XSCALE));
2270ea7ac69dSPeter Maydell 
2271a575230fSRichard Henderson #ifndef CONFIG_USER_ONLY
2272a575230fSRichard Henderson     {
2273a575230fSRichard Henderson         int pagebits;
2274fcf5ef2aSThomas Huth         if (arm_feature(env, ARM_FEATURE_V7) &&
2275fcf5ef2aSThomas Huth             !arm_feature(env, ARM_FEATURE_M) &&
2276452a0955SPeter Maydell             !arm_feature(env, ARM_FEATURE_PMSA)) {
2277a575230fSRichard Henderson             /*
2278a575230fSRichard Henderson              * v7VMSA drops support for the old ARMv5 tiny pages,
2279a575230fSRichard Henderson              * so we can use 4K pages.
2280fcf5ef2aSThomas Huth              */
2281fcf5ef2aSThomas Huth             pagebits = 12;
2282fcf5ef2aSThomas Huth         } else {
2283a575230fSRichard Henderson             /*
2284a575230fSRichard Henderson              * For CPUs which might have tiny 1K pages, or which have an
2285fcf5ef2aSThomas Huth              * MPU and might have small region sizes, stick with 1K pages.
2286fcf5ef2aSThomas Huth              */
2287fcf5ef2aSThomas Huth             pagebits = 10;
2288fcf5ef2aSThomas Huth         }
2289fcf5ef2aSThomas Huth         if (!set_preferred_target_page_bits(pagebits)) {
2290a575230fSRichard Henderson             /*
2291a575230fSRichard Henderson              * This can only ever happen for hotplugging a CPU, or if
2292fcf5ef2aSThomas Huth              * the board code incorrectly creates a CPU which it has
2293fcf5ef2aSThomas Huth              * promised via minimum_page_size that it will not.
2294fcf5ef2aSThomas Huth              */
2295a575230fSRichard Henderson             error_setg(errp, "This CPU requires a smaller page size "
2296a575230fSRichard Henderson                        "than the system is using");
2297fcf5ef2aSThomas Huth             return;
2298fcf5ef2aSThomas Huth         }
2299a575230fSRichard Henderson     }
2300a575230fSRichard Henderson #endif
2301fcf5ef2aSThomas Huth 
2302fcf5ef2aSThomas Huth     /* This cpu-id-to-MPIDR affinity is used only for TCG; KVM will override it.
2303fcf5ef2aSThomas Huth      * We don't support setting cluster ID ([16..23]) (known as Aff2
2304fcf5ef2aSThomas Huth      * in later ARM ARM versions), or any of the higher affinity level fields,
2305fcf5ef2aSThomas Huth      * so these bits always RAZ.
2306fcf5ef2aSThomas Huth      */
2307fcf5ef2aSThomas Huth     if (cpu->mp_affinity == ARM64_AFFINITY_INVALID) {
2308750245edSRichard Henderson         cpu->mp_affinity = arm_build_mp_affinity(cs->cpu_index,
230946de5913SIgor Mammedov                                                  ARM_DEFAULT_CPUS_PER_CLUSTER);
2310fcf5ef2aSThomas Huth     }
2311fcf5ef2aSThomas Huth 
2312fcf5ef2aSThomas Huth     if (cpu->reset_hivecs) {
2313fcf5ef2aSThomas Huth             cpu->reset_sctlr |= (1 << 13);
2314fcf5ef2aSThomas Huth     }
2315fcf5ef2aSThomas Huth 
23163a062d57SJulian Brown     if (cpu->cfgend) {
2317ee1004bbSPhilippe Mathieu-Daudé         if (arm_feature(env, ARM_FEATURE_V7)) {
23183a062d57SJulian Brown             cpu->reset_sctlr |= SCTLR_EE;
23193a062d57SJulian Brown         } else {
23203a062d57SJulian Brown             cpu->reset_sctlr |= SCTLR_B;
23213a062d57SJulian Brown         }
23223a062d57SJulian Brown     }
23233a062d57SJulian Brown 
232440188188SPeter Maydell     if (!arm_feature(env, ARM_FEATURE_M) && !cpu->has_el3) {
2325fcf5ef2aSThomas Huth         /* If the has_el3 CPU property is disabled then we need to disable the
2326fcf5ef2aSThomas Huth          * feature.
2327fcf5ef2aSThomas Huth          */
2328fcf5ef2aSThomas Huth         unset_feature(env, ARM_FEATURE_EL3);
2329fcf5ef2aSThomas Huth 
2330b13c91c0SRichard Henderson         /*
2331b13c91c0SRichard Henderson          * Disable the security extension feature bits in the processor
2332b13c91c0SRichard Henderson          * feature registers as well.
2333fcf5ef2aSThomas Huth          */
2334b13c91c0SRichard Henderson         cpu->isar.id_pfr1 = FIELD_DP32(cpu->isar.id_pfr1, ID_PFR1, SECURITY, 0);
2335033a4f15SRichard Henderson         cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, COPSDBG, 0);
2336b13c91c0SRichard Henderson         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2337b13c91c0SRichard Henderson                                            ID_AA64PFR0, EL3, 0);
2338b9f335c2SRichard Henderson 
2339b9f335c2SRichard Henderson         /* Disable the realm management extension, which requires EL3. */
2340b9f335c2SRichard Henderson         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2341b9f335c2SRichard Henderson                                            ID_AA64PFR0, RME, 0);
2342fcf5ef2aSThomas Huth     }
2343fcf5ef2aSThomas Huth 
2344c25bd18aSPeter Maydell     if (!cpu->has_el2) {
2345c25bd18aSPeter Maydell         unset_feature(env, ARM_FEATURE_EL2);
2346c25bd18aSPeter Maydell     }
2347c25bd18aSPeter Maydell 
2348d6f02ce3SWei Huang     if (!cpu->has_pmu) {
2349fcf5ef2aSThomas Huth         unset_feature(env, ARM_FEATURE_PMU);
235057a4a11bSAaron Lindsay     }
235157a4a11bSAaron Lindsay     if (arm_feature(env, ARM_FEATURE_PMU)) {
2352bf8d0969SAaron Lindsay OS         pmu_init(cpu);
235357a4a11bSAaron Lindsay 
235457a4a11bSAaron Lindsay         if (!kvm_enabled()) {
2355033614c4SAaron Lindsay             arm_register_pre_el_change_hook(cpu, &pmu_pre_el_change, 0);
2356033614c4SAaron Lindsay             arm_register_el_change_hook(cpu, &pmu_post_el_change, 0);
2357fcf5ef2aSThomas Huth         }
23584e7beb0cSAaron Lindsay OS 
23594e7beb0cSAaron Lindsay OS #ifndef CONFIG_USER_ONLY
23604e7beb0cSAaron Lindsay OS         cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, arm_pmu_timer_cb,
23614e7beb0cSAaron Lindsay OS                 cpu);
23624e7beb0cSAaron Lindsay OS #endif
236357a4a11bSAaron Lindsay     } else {
23642a609df8SPeter Maydell         cpu->isar.id_aa64dfr0 =
23652a609df8SPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMUVER, 0);
2366a6179538SPeter Maydell         cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, PERFMON, 0);
236757a4a11bSAaron Lindsay         cpu->pmceid0 = 0;
236857a4a11bSAaron Lindsay         cpu->pmceid1 = 0;
236957a4a11bSAaron Lindsay     }
2370fcf5ef2aSThomas Huth 
2371fcf5ef2aSThomas Huth     if (!arm_feature(env, ARM_FEATURE_EL2)) {
2372b13c91c0SRichard Henderson         /*
2373b13c91c0SRichard Henderson          * Disable the hypervisor feature bits in the processor feature
2374b13c91c0SRichard Henderson          * registers if we don't have EL2.
2375fcf5ef2aSThomas Huth          */
2376b13c91c0SRichard Henderson         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2377b13c91c0SRichard Henderson                                            ID_AA64PFR0, EL2, 0);
2378b13c91c0SRichard Henderson         cpu->isar.id_pfr1 = FIELD_DP32(cpu->isar.id_pfr1,
2379b13c91c0SRichard Henderson                                        ID_PFR1, VIRTUALIZATION, 0);
2380fcf5ef2aSThomas Huth     }
2381fcf5ef2aSThomas Huth 
23827134cb07SRichard Henderson     if (cpu_isar_feature(aa64_mte, cpu)) {
23837134cb07SRichard Henderson         /*
23847134cb07SRichard Henderson          * The architectural range of GM blocksize is 2-6, however qemu
23857134cb07SRichard Henderson          * doesn't support blocksize of 2 (see HELPER(ldgm)).
23867134cb07SRichard Henderson          */
23877134cb07SRichard Henderson         if (tcg_enabled()) {
23887134cb07SRichard Henderson             assert(cpu->gm_blocksize >= 3 && cpu->gm_blocksize <= 6);
23897134cb07SRichard Henderson         }
23907134cb07SRichard Henderson 
23916f4e1405SRichard Henderson #ifndef CONFIG_USER_ONLY
23926f4e1405SRichard Henderson         /*
2393cd305b5fSRichard Henderson          * If we do not have tag-memory provided by the machine,
2394cd305b5fSRichard Henderson          * reduce MTE support to instructions enabled at EL0.
2395cd305b5fSRichard Henderson          * This matches Cortex-A710 BROADCASTMTE input being LOW.
23966f4e1405SRichard Henderson          */
23977134cb07SRichard Henderson         if (cpu->tag_memory == NULL) {
23986f4e1405SRichard Henderson             cpu->isar.id_aa64pfr1 =
2399cd305b5fSRichard Henderson                 FIELD_DP64(cpu->isar.id_aa64pfr1, ID_AA64PFR1, MTE, 1);
24006f4e1405SRichard Henderson         }
24016f4e1405SRichard Henderson #endif
24027134cb07SRichard Henderson     }
24036f4e1405SRichard Henderson 
2404*a96edb68SPeter Maydell #ifndef CONFIG_USER_ONLY
2405*a96edb68SPeter Maydell     if (tcg_enabled() && cpu_isar_feature(aa64_wfxt, cpu)) {
2406*a96edb68SPeter Maydell         cpu->wfxt_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
2407*a96edb68SPeter Maydell                                        arm_wfxt_timer_cb, cpu);
2408*a96edb68SPeter Maydell     }
2409*a96edb68SPeter Maydell #endif
2410*a96edb68SPeter Maydell 
24112daf518dSPeter Maydell     if (tcg_enabled()) {
24122daf518dSPeter Maydell         /*
24137d8c283eSPeter Maydell          * Don't report some architectural features in the ID registers
24147d8c283eSPeter Maydell          * where TCG does not yet implement it (not even a minimal
24157d8c283eSPeter Maydell          * stub version). This avoids guests falling over when they
24167d8c283eSPeter Maydell          * try to access the non-existent system registers for them.
24172daf518dSPeter Maydell          */
24187d8c283eSPeter Maydell         /* FEAT_SPE (Statistical Profiling Extension) */
24192daf518dSPeter Maydell         cpu->isar.id_aa64dfr0 =
24202daf518dSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMSVER, 0);
24213d5f45ecSRichard Henderson         /* FEAT_TRBE (Trace Buffer Extension) */
24223d5f45ecSRichard Henderson         cpu->isar.id_aa64dfr0 =
24233d5f45ecSRichard Henderson             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEBUFFER, 0);
24247d8c283eSPeter Maydell         /* FEAT_TRF (Self-hosted Trace Extension) */
24257d8c283eSPeter Maydell         cpu->isar.id_aa64dfr0 =
24267d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEFILT, 0);
24277d8c283eSPeter Maydell         cpu->isar.id_dfr0 =
24287d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, TRACEFILT, 0);
24297d8c283eSPeter Maydell         /* Trace Macrocell system register access */
24307d8c283eSPeter Maydell         cpu->isar.id_aa64dfr0 =
24317d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEVER, 0);
24327d8c283eSPeter Maydell         cpu->isar.id_dfr0 =
24337d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, COPTRC, 0);
24347d8c283eSPeter Maydell         /* Memory mapped trace */
24357d8c283eSPeter Maydell         cpu->isar.id_dfr0 =
24367d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, MMAPTRC, 0);
24377d8c283eSPeter Maydell         /* FEAT_AMU (Activity Monitors Extension) */
24387d8c283eSPeter Maydell         cpu->isar.id_aa64pfr0 =
24397d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64pfr0, ID_AA64PFR0, AMU, 0);
24407d8c283eSPeter Maydell         cpu->isar.id_pfr0 =
24417d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_pfr0, ID_PFR0, AMU, 0);
24427d8c283eSPeter Maydell         /* FEAT_MPAM (Memory Partitioning and Monitoring Extension) */
24437d8c283eSPeter Maydell         cpu->isar.id_aa64pfr0 =
24447d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64pfr0, ID_AA64PFR0, MPAM, 0);
24452daf518dSPeter Maydell     }
24462daf518dSPeter Maydell 
2447f50cd314SPeter Maydell     /* MPU can be configured out of a PMSA CPU either by setting has-mpu
2448f50cd314SPeter Maydell      * to false or by setting pmsav7-dregion to 0.
2449f50cd314SPeter Maydell      */
2450761c4642STobias Röhmel     if (!cpu->has_mpu || cpu->pmsav7_dregion == 0) {
2451f50cd314SPeter Maydell         cpu->has_mpu = false;
2452761c4642STobias Röhmel         cpu->pmsav7_dregion = 0;
2453761c4642STobias Röhmel         cpu->pmsav8r_hdregion = 0;
2454fcf5ef2aSThomas Huth     }
2455fcf5ef2aSThomas Huth 
2456452a0955SPeter Maydell     if (arm_feature(env, ARM_FEATURE_PMSA) &&
2457fcf5ef2aSThomas Huth         arm_feature(env, ARM_FEATURE_V7)) {
2458fcf5ef2aSThomas Huth         uint32_t nr = cpu->pmsav7_dregion;
2459fcf5ef2aSThomas Huth 
2460fcf5ef2aSThomas Huth         if (nr > 0xff) {
2461fcf5ef2aSThomas Huth             error_setg(errp, "PMSAv7 MPU #regions invalid %" PRIu32, nr);
2462fcf5ef2aSThomas Huth             return;
2463fcf5ef2aSThomas Huth         }
2464fcf5ef2aSThomas Huth 
2465fcf5ef2aSThomas Huth         if (nr) {
24660e1a46bbSPeter Maydell             if (arm_feature(env, ARM_FEATURE_V8)) {
24670e1a46bbSPeter Maydell                 /* PMSAv8 */
246862c58ee0SPeter Maydell                 env->pmsav8.rbar[M_REG_NS] = g_new0(uint32_t, nr);
246962c58ee0SPeter Maydell                 env->pmsav8.rlar[M_REG_NS] = g_new0(uint32_t, nr);
247062c58ee0SPeter Maydell                 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
247162c58ee0SPeter Maydell                     env->pmsav8.rbar[M_REG_S] = g_new0(uint32_t, nr);
247262c58ee0SPeter Maydell                     env->pmsav8.rlar[M_REG_S] = g_new0(uint32_t, nr);
247362c58ee0SPeter Maydell                 }
24740e1a46bbSPeter Maydell             } else {
2475fcf5ef2aSThomas Huth                 env->pmsav7.drbar = g_new0(uint32_t, nr);
2476fcf5ef2aSThomas Huth                 env->pmsav7.drsr = g_new0(uint32_t, nr);
2477fcf5ef2aSThomas Huth                 env->pmsav7.dracr = g_new0(uint32_t, nr);
2478fcf5ef2aSThomas Huth             }
2479fcf5ef2aSThomas Huth         }
2480761c4642STobias Röhmel 
2481761c4642STobias Röhmel         if (cpu->pmsav8r_hdregion > 0xff) {
2482761c4642STobias Röhmel             error_setg(errp, "PMSAv8 MPU EL2 #regions invalid %" PRIu32,
2483761c4642STobias Röhmel                               cpu->pmsav8r_hdregion);
2484761c4642STobias Röhmel             return;
2485761c4642STobias Röhmel         }
2486761c4642STobias Röhmel 
2487761c4642STobias Röhmel         if (cpu->pmsav8r_hdregion) {
2488761c4642STobias Röhmel             env->pmsav8.hprbar = g_new0(uint32_t,
2489761c4642STobias Röhmel                                         cpu->pmsav8r_hdregion);
2490761c4642STobias Röhmel             env->pmsav8.hprlar = g_new0(uint32_t,
2491761c4642STobias Röhmel                                         cpu->pmsav8r_hdregion);
2492761c4642STobias Röhmel         }
24930e1a46bbSPeter Maydell     }
2494fcf5ef2aSThomas Huth 
24959901c576SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
24969901c576SPeter Maydell         uint32_t nr = cpu->sau_sregion;
24979901c576SPeter Maydell 
24989901c576SPeter Maydell         if (nr > 0xff) {
24999901c576SPeter Maydell             error_setg(errp, "v8M SAU #regions invalid %" PRIu32, nr);
25009901c576SPeter Maydell             return;
25019901c576SPeter Maydell         }
25029901c576SPeter Maydell 
25039901c576SPeter Maydell         if (nr) {
25049901c576SPeter Maydell             env->sau.rbar = g_new0(uint32_t, nr);
25059901c576SPeter Maydell             env->sau.rlar = g_new0(uint32_t, nr);
25069901c576SPeter Maydell         }
25079901c576SPeter Maydell     }
25089901c576SPeter Maydell 
250991db4642SCédric Le Goater     if (arm_feature(env, ARM_FEATURE_EL3)) {
251091db4642SCédric Le Goater         set_feature(env, ARM_FEATURE_VBAR);
251191db4642SCédric Le Goater     }
251291db4642SCédric Le Goater 
2513f6fc36deSJean-Philippe Brucker #ifndef CONFIG_USER_ONLY
2514f6fc36deSJean-Philippe Brucker     if (tcg_enabled() && cpu_isar_feature(aa64_rme, cpu)) {
2515f6fc36deSJean-Philippe Brucker         arm_register_el_change_hook(cpu, &gt_rme_post_el_change, 0);
2516f6fc36deSJean-Philippe Brucker     }
2517f6fc36deSJean-Philippe Brucker #endif
2518f6fc36deSJean-Philippe Brucker 
2519fcf5ef2aSThomas Huth     register_cp_regs_for_features(cpu);
2520fcf5ef2aSThomas Huth     arm_cpu_register_gdb_regs_for_features(cpu);
2521fcf5ef2aSThomas Huth 
2522fcf5ef2aSThomas Huth     init_cpreg_list(cpu);
2523fcf5ef2aSThomas Huth 
2524fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
2525cc7d44c2SLike Xu     MachineState *ms = MACHINE(qdev_get_machine());
2526cc7d44c2SLike Xu     unsigned int smp_cpus = ms->smp.cpus;
25278bce44a2SRichard Henderson     bool has_secure = cpu->has_el3 || arm_feature(env, ARM_FEATURE_M_SECURITY);
2528cc7d44c2SLike Xu 
25298bce44a2SRichard Henderson     /*
25308bce44a2SRichard Henderson      * We must set cs->num_ases to the final value before
25318bce44a2SRichard Henderson      * the first call to cpu_address_space_init.
25328bce44a2SRichard Henderson      */
25338bce44a2SRichard Henderson     if (cpu->tag_memory != NULL) {
25348bce44a2SRichard Henderson         cs->num_ases = 3 + has_secure;
25358bce44a2SRichard Henderson     } else {
25368bce44a2SRichard Henderson         cs->num_ases = 1 + has_secure;
25378bce44a2SRichard Henderson     }
25381d2091bcSPeter Maydell 
25398bce44a2SRichard Henderson     if (has_secure) {
2540fcf5ef2aSThomas Huth         if (!cpu->secure_memory) {
2541fcf5ef2aSThomas Huth             cpu->secure_memory = cs->memory;
2542fcf5ef2aSThomas Huth         }
254380ceb07aSPeter Xu         cpu_address_space_init(cs, ARMASIdx_S, "cpu-secure-memory",
254480ceb07aSPeter Xu                                cpu->secure_memory);
2545fcf5ef2aSThomas Huth     }
25468bce44a2SRichard Henderson 
25478bce44a2SRichard Henderson     if (cpu->tag_memory != NULL) {
25488bce44a2SRichard Henderson         cpu_address_space_init(cs, ARMASIdx_TagNS, "cpu-tag-memory",
25498bce44a2SRichard Henderson                                cpu->tag_memory);
25508bce44a2SRichard Henderson         if (has_secure) {
25518bce44a2SRichard Henderson             cpu_address_space_init(cs, ARMASIdx_TagS, "cpu-tag-memory",
25528bce44a2SRichard Henderson                                    cpu->secure_tag_memory);
25538bce44a2SRichard Henderson         }
25548bce44a2SRichard Henderson     }
25558bce44a2SRichard Henderson 
255680ceb07aSPeter Xu     cpu_address_space_init(cs, ARMASIdx_NS, "cpu-memory", cs->memory);
2557f9a69711SAlistair Francis 
2558f9a69711SAlistair Francis     /* No core_count specified, default to smp_cpus. */
2559f9a69711SAlistair Francis     if (cpu->core_count == -1) {
2560f9a69711SAlistair Francis         cpu->core_count = smp_cpus;
2561f9a69711SAlistair Francis     }
2562fcf5ef2aSThomas Huth #endif
2563fcf5ef2aSThomas Huth 
2564a4157b80SRichard Henderson     if (tcg_enabled()) {
2565a4157b80SRichard Henderson         int dcz_blocklen = 4 << cpu->dcz_blocksize;
2566a4157b80SRichard Henderson 
2567a4157b80SRichard Henderson         /*
2568a4157b80SRichard Henderson          * We only support DCZ blocklen that fits on one page.
2569a4157b80SRichard Henderson          *
2570a4157b80SRichard Henderson          * Architectually this is always true.  However TARGET_PAGE_SIZE
2571a4157b80SRichard Henderson          * is variable and, for compatibility with -machine virt-2.7,
2572a4157b80SRichard Henderson          * is only 1KiB, as an artifact of legacy ARMv5 subpage support.
2573a4157b80SRichard Henderson          * But even then, while the largest architectural DCZ blocklen
2574a4157b80SRichard Henderson          * is 2KiB, no cpu actually uses such a large blocklen.
2575a4157b80SRichard Henderson          */
2576a4157b80SRichard Henderson         assert(dcz_blocklen <= TARGET_PAGE_SIZE);
2577a4157b80SRichard Henderson 
2578a4157b80SRichard Henderson         /*
2579a4157b80SRichard Henderson          * We only support DCZ blocksize >= 2*TAG_GRANULE, which is to say
2580a4157b80SRichard Henderson          * both nibbles of each byte storing tag data may be written at once.
2581a4157b80SRichard Henderson          * Since TAG_GRANULE is 16, this means that blocklen must be >= 32.
2582a4157b80SRichard Henderson          */
2583a4157b80SRichard Henderson         if (cpu_isar_feature(aa64_mte, cpu)) {
2584a4157b80SRichard Henderson             assert(dcz_blocklen >= 2 * TAG_GRANULE);
2585a4157b80SRichard Henderson         }
2586a4157b80SRichard Henderson     }
2587a4157b80SRichard Henderson 
2588fcf5ef2aSThomas Huth     qemu_init_vcpu(cs);
2589fcf5ef2aSThomas Huth     cpu_reset(cs);
2590fcf5ef2aSThomas Huth 
2591fcf5ef2aSThomas Huth     acc->parent_realize(dev, errp);
2592fcf5ef2aSThomas Huth }
2593fcf5ef2aSThomas Huth 
2594fcf5ef2aSThomas Huth static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
2595fcf5ef2aSThomas Huth {
2596fcf5ef2aSThomas Huth     ObjectClass *oc;
2597fcf5ef2aSThomas Huth     char *typename;
2598fcf5ef2aSThomas Huth     char **cpuname;
2599a0032cc5SPeter Maydell     const char *cpunamestr;
2600fcf5ef2aSThomas Huth 
2601fcf5ef2aSThomas Huth     cpuname = g_strsplit(cpu_model, ",", 1);
2602a0032cc5SPeter Maydell     cpunamestr = cpuname[0];
2603a0032cc5SPeter Maydell #ifdef CONFIG_USER_ONLY
2604a0032cc5SPeter Maydell     /* For backwards compatibility usermode emulation allows "-cpu any",
2605a0032cc5SPeter Maydell      * which has the same semantics as "-cpu max".
2606a0032cc5SPeter Maydell      */
2607a0032cc5SPeter Maydell     if (!strcmp(cpunamestr, "any")) {
2608a0032cc5SPeter Maydell         cpunamestr = "max";
2609a0032cc5SPeter Maydell     }
2610a0032cc5SPeter Maydell #endif
2611a0032cc5SPeter Maydell     typename = g_strdup_printf(ARM_CPU_TYPE_NAME("%s"), cpunamestr);
2612fcf5ef2aSThomas Huth     oc = object_class_by_name(typename);
2613fcf5ef2aSThomas Huth     g_strfreev(cpuname);
2614fcf5ef2aSThomas Huth     g_free(typename);
2615d5be19f5SPhilippe Mathieu-Daudé 
2616fcf5ef2aSThomas Huth     return oc;
2617fcf5ef2aSThomas Huth }
2618fcf5ef2aSThomas Huth 
2619fcf5ef2aSThomas Huth static Property arm_cpu_properties[] = {
2620e544f800SPhilippe Mathieu-Daudé     DEFINE_PROP_UINT64("midr", ARMCPU, midr, 0),
2621fcf5ef2aSThomas Huth     DEFINE_PROP_UINT64("mp-affinity", ARMCPU,
2622fcf5ef2aSThomas Huth                         mp_affinity, ARM64_AFFINITY_INVALID),
262315f8b142SIgor Mammedov     DEFINE_PROP_INT32("node-id", ARMCPU, node_id, CPU_UNSET_NUMA_NODE_ID),
2624f9a69711SAlistair Francis     DEFINE_PROP_INT32("core-count", ARMCPU, core_count, -1),
2625f037f5b4SPeter Maydell     /* True to default to the backward-compat old CNTFRQ rather than 1Ghz */
2626f037f5b4SPeter Maydell     DEFINE_PROP_BOOL("backcompat-cntfrq", ARMCPU, backcompat_cntfrq, false),
2627fcf5ef2aSThomas Huth     DEFINE_PROP_END_OF_LIST()
2628fcf5ef2aSThomas Huth };
2629fcf5ef2aSThomas Huth 
2630a6506838SAkihiko Odaki static const gchar *arm_gdb_arch_name(CPUState *cs)
2631fcf5ef2aSThomas Huth {
2632fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(cs);
2633fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
2634fcf5ef2aSThomas Huth 
2635fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
2636a6506838SAkihiko Odaki         return "iwmmxt";
2637fcf5ef2aSThomas Huth     }
2638a6506838SAkihiko Odaki     return "arm";
2639fcf5ef2aSThomas Huth }
2640fcf5ef2aSThomas Huth 
26418b80bd28SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY
26428b80bd28SPhilippe Mathieu-Daudé #include "hw/core/sysemu-cpu-ops.h"
26438b80bd28SPhilippe Mathieu-Daudé 
26448b80bd28SPhilippe Mathieu-Daudé static const struct SysemuCPUOps arm_sysemu_ops = {
264508928c6dSPhilippe Mathieu-Daudé     .get_phys_page_attrs_debug = arm_cpu_get_phys_page_attrs_debug,
2646faf39e82SPhilippe Mathieu-Daudé     .asidx_from_attrs = arm_asidx_from_attrs,
2647715e3c1aSPhilippe Mathieu-Daudé     .write_elf32_note = arm_cpu_write_elf32_note,
2648715e3c1aSPhilippe Mathieu-Daudé     .write_elf64_note = arm_cpu_write_elf64_note,
2649da383e02SPhilippe Mathieu-Daudé     .virtio_is_big_endian = arm_cpu_virtio_is_big_endian,
2650feece4d0SPhilippe Mathieu-Daudé     .legacy_vmsd = &vmstate_arm_cpu,
26518b80bd28SPhilippe Mathieu-Daudé };
26528b80bd28SPhilippe Mathieu-Daudé #endif
26538b80bd28SPhilippe Mathieu-Daudé 
265478271684SClaudio Fontana #ifdef CONFIG_TCG
26551764ad70SRichard Henderson static const TCGCPUOps arm_tcg_ops = {
265678271684SClaudio Fontana     .initialize = arm_translate_init,
265778271684SClaudio Fontana     .synchronize_from_tb = arm_cpu_synchronize_from_tb,
265878271684SClaudio Fontana     .debug_excp_handler = arm_debug_excp_handler,
265956c6c98dSRichard Henderson     .restore_state_to_opc = arm_restore_state_to_opc,
266078271684SClaudio Fontana 
26619b12b6b4SRichard Henderson #ifdef CONFIG_USER_ONLY
26629b12b6b4SRichard Henderson     .record_sigsegv = arm_cpu_record_sigsegv,
266339a099caSRichard Henderson     .record_sigbus = arm_cpu_record_sigbus,
26649b12b6b4SRichard Henderson #else
26659b12b6b4SRichard Henderson     .tlb_fill = arm_cpu_tlb_fill,
2666083afd18SPhilippe Mathieu-Daudé     .cpu_exec_interrupt = arm_cpu_exec_interrupt,
2667*a96edb68SPeter Maydell     .cpu_exec_halt = arm_cpu_exec_halt,
266878271684SClaudio Fontana     .do_interrupt = arm_cpu_do_interrupt,
266978271684SClaudio Fontana     .do_transaction_failed = arm_cpu_do_transaction_failed,
267078271684SClaudio Fontana     .do_unaligned_access = arm_cpu_do_unaligned_access,
267178271684SClaudio Fontana     .adjust_watchpoint_address = arm_adjust_watchpoint_address,
267278271684SClaudio Fontana     .debug_check_watchpoint = arm_debug_check_watchpoint,
2673b00d86bcSRichard Henderson     .debug_check_breakpoint = arm_debug_check_breakpoint,
267478271684SClaudio Fontana #endif /* !CONFIG_USER_ONLY */
267578271684SClaudio Fontana };
267678271684SClaudio Fontana #endif /* CONFIG_TCG */
267778271684SClaudio Fontana 
2678fcf5ef2aSThomas Huth static void arm_cpu_class_init(ObjectClass *oc, void *data)
2679fcf5ef2aSThomas Huth {
2680fcf5ef2aSThomas Huth     ARMCPUClass *acc = ARM_CPU_CLASS(oc);
2681fcf5ef2aSThomas Huth     CPUClass *cc = CPU_CLASS(acc);
2682fcf5ef2aSThomas Huth     DeviceClass *dc = DEVICE_CLASS(oc);
26839130cadeSPeter Maydell     ResettableClass *rc = RESETTABLE_CLASS(oc);
2684fcf5ef2aSThomas Huth 
2685bf853881SPhilippe Mathieu-Daudé     device_class_set_parent_realize(dc, arm_cpu_realizefn,
2686bf853881SPhilippe Mathieu-Daudé                                     &acc->parent_realize);
2687fcf5ef2aSThomas Huth 
26884f67d30bSMarc-André Lureau     device_class_set_props(dc, arm_cpu_properties);
26899130cadeSPeter Maydell 
26909130cadeSPeter Maydell     resettable_class_set_parent_phases(rc, NULL, arm_cpu_reset_hold, NULL,
26919130cadeSPeter Maydell                                        &acc->parent_phases);
2692fcf5ef2aSThomas Huth 
2693fcf5ef2aSThomas Huth     cc->class_by_name = arm_cpu_class_by_name;
2694fcf5ef2aSThomas Huth     cc->has_work = arm_cpu_has_work;
2695628421c7SRichard Henderson     cc->mmu_index = arm_cpu_mmu_index;
2696fcf5ef2aSThomas Huth     cc->dump_state = arm_cpu_dump_state;
2697fcf5ef2aSThomas Huth     cc->set_pc = arm_cpu_set_pc;
2698e4fdf9dfSRichard Henderson     cc->get_pc = arm_cpu_get_pc;
2699fcf5ef2aSThomas Huth     cc->gdb_read_register = arm_cpu_gdb_read_register;
2700fcf5ef2aSThomas Huth     cc->gdb_write_register = arm_cpu_gdb_write_register;
27017350d553SRichard Henderson #ifndef CONFIG_USER_ONLY
27028b80bd28SPhilippe Mathieu-Daudé     cc->sysemu_ops = &arm_sysemu_ops;
2703fcf5ef2aSThomas Huth #endif
2704fcf5ef2aSThomas Huth     cc->gdb_arch_name = arm_gdb_arch_name;
2705fcf5ef2aSThomas Huth     cc->gdb_stop_before_watchpoint = true;
2706fcf5ef2aSThomas Huth     cc->disas_set_info = arm_disas_set_info;
270778271684SClaudio Fontana 
270874d7fc7fSRichard Henderson #ifdef CONFIG_TCG
270978271684SClaudio Fontana     cc->tcg_ops = &arm_tcg_ops;
2710cbc183d2SClaudio Fontana #endif /* CONFIG_TCG */
2711fcf5ef2aSThomas Huth }
2712fcf5ef2aSThomas Huth 
271351e5ef45SMarc-André Lureau static void arm_cpu_instance_init(Object *obj)
271451e5ef45SMarc-André Lureau {
271551e5ef45SMarc-André Lureau     ARMCPUClass *acc = ARM_CPU_GET_CLASS(obj);
271651e5ef45SMarc-André Lureau 
271751e5ef45SMarc-André Lureau     acc->info->initfn(obj);
271851e5ef45SMarc-André Lureau     arm_cpu_post_init(obj);
271951e5ef45SMarc-André Lureau }
272051e5ef45SMarc-André Lureau 
272151e5ef45SMarc-André Lureau static void cpu_register_class_init(ObjectClass *oc, void *data)
272251e5ef45SMarc-André Lureau {
272351e5ef45SMarc-André Lureau     ARMCPUClass *acc = ARM_CPU_CLASS(oc);
272448de6462SAkihiko Odaki     CPUClass *cc = CPU_CLASS(acc);
272551e5ef45SMarc-André Lureau 
272651e5ef45SMarc-André Lureau     acc->info = data;
272748de6462SAkihiko Odaki     cc->gdb_core_xml_file = "arm-core.xml";
272851e5ef45SMarc-André Lureau }
272951e5ef45SMarc-André Lureau 
273037bcf244SThomas Huth void arm_cpu_register(const ARMCPUInfo *info)
2731fcf5ef2aSThomas Huth {
2732fcf5ef2aSThomas Huth     TypeInfo type_info = {
2733fcf5ef2aSThomas Huth         .parent = TYPE_ARM_CPU,
273451e5ef45SMarc-André Lureau         .instance_init = arm_cpu_instance_init,
273551e5ef45SMarc-André Lureau         .class_init = info->class_init ?: cpu_register_class_init,
273651e5ef45SMarc-André Lureau         .class_data = (void *)info,
2737fcf5ef2aSThomas Huth     };
2738fcf5ef2aSThomas Huth 
2739fcf5ef2aSThomas Huth     type_info.name = g_strdup_printf("%s-" TYPE_ARM_CPU, info->name);
2740fcf5ef2aSThomas Huth     type_register(&type_info);
2741fcf5ef2aSThomas Huth     g_free((void *)type_info.name);
2742fcf5ef2aSThomas Huth }
2743fcf5ef2aSThomas Huth 
2744fcf5ef2aSThomas Huth static const TypeInfo arm_cpu_type_info = {
2745fcf5ef2aSThomas Huth     .name = TYPE_ARM_CPU,
2746fcf5ef2aSThomas Huth     .parent = TYPE_CPU,
2747fcf5ef2aSThomas Huth     .instance_size = sizeof(ARMCPU),
2748d03087bdSRichard Henderson     .instance_align = __alignof__(ARMCPU),
2749fcf5ef2aSThomas Huth     .instance_init = arm_cpu_initfn,
2750fcf5ef2aSThomas Huth     .instance_finalize = arm_cpu_finalizefn,
2751fcf5ef2aSThomas Huth     .abstract = true,
2752fcf5ef2aSThomas Huth     .class_size = sizeof(ARMCPUClass),
2753fcf5ef2aSThomas Huth     .class_init = arm_cpu_class_init,
2754fcf5ef2aSThomas Huth };
2755fcf5ef2aSThomas Huth 
2756fcf5ef2aSThomas Huth static void arm_cpu_register_types(void)
2757fcf5ef2aSThomas Huth {
2758fcf5ef2aSThomas Huth     type_register_static(&arm_cpu_type_info);
2759fcf5ef2aSThomas Huth }
2760fcf5ef2aSThomas Huth 
2761fcf5ef2aSThomas Huth type_init(arm_cpu_register_types)
2762