xref: /openbmc/qemu/target/arm/cpu.c (revision ee1004bba6eadeddf988a29716dc28849c0211c8)
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 
125fcf5ef2aSThomas Huth static bool arm_cpu_has_work(CPUState *cs)
126fcf5ef2aSThomas Huth {
127fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(cs);
128fcf5ef2aSThomas Huth 
129062ba099SAlex Bennée     return (cpu->power_state != PSCI_OFF)
130fcf5ef2aSThomas Huth         && cs->interrupt_request &
131fcf5ef2aSThomas Huth         (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD
1323c29632fSRichard Henderson          | CPU_INTERRUPT_VFIQ | CPU_INTERRUPT_VIRQ | CPU_INTERRUPT_VSERR
133fcf5ef2aSThomas Huth          | CPU_INTERRUPT_EXITTB);
134fcf5ef2aSThomas Huth }
135fcf5ef2aSThomas Huth 
136628421c7SRichard Henderson static int arm_cpu_mmu_index(CPUState *cs, bool ifetch)
137628421c7SRichard Henderson {
138628421c7SRichard Henderson     return arm_env_mmu_index(cpu_env(cs));
139628421c7SRichard Henderson }
140628421c7SRichard Henderson 
141b5c53d1bSAaron Lindsay void arm_register_pre_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook,
142b5c53d1bSAaron Lindsay                                  void *opaque)
143b5c53d1bSAaron Lindsay {
144b5c53d1bSAaron Lindsay     ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1);
145b5c53d1bSAaron Lindsay 
146b5c53d1bSAaron Lindsay     entry->hook = hook;
147b5c53d1bSAaron Lindsay     entry->opaque = opaque;
148b5c53d1bSAaron Lindsay 
149b5c53d1bSAaron Lindsay     QLIST_INSERT_HEAD(&cpu->pre_el_change_hooks, entry, node);
150b5c53d1bSAaron Lindsay }
151b5c53d1bSAaron Lindsay 
15208267487SAaron Lindsay void arm_register_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook,
153fcf5ef2aSThomas Huth                                  void *opaque)
154fcf5ef2aSThomas Huth {
15508267487SAaron Lindsay     ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1);
15608267487SAaron Lindsay 
15708267487SAaron Lindsay     entry->hook = hook;
15808267487SAaron Lindsay     entry->opaque = opaque;
15908267487SAaron Lindsay 
16008267487SAaron Lindsay     QLIST_INSERT_HEAD(&cpu->el_change_hooks, entry, node);
161fcf5ef2aSThomas Huth }
162fcf5ef2aSThomas Huth 
163fcf5ef2aSThomas Huth static void cp_reg_reset(gpointer key, gpointer value, gpointer opaque)
164fcf5ef2aSThomas Huth {
165fcf5ef2aSThomas Huth     /* Reset a single ARMCPRegInfo register */
166fcf5ef2aSThomas Huth     ARMCPRegInfo *ri = value;
167fcf5ef2aSThomas Huth     ARMCPU *cpu = opaque;
168fcf5ef2aSThomas Huth 
16987c3f0f2SRichard Henderson     if (ri->type & (ARM_CP_SPECIAL_MASK | ARM_CP_ALIAS)) {
170fcf5ef2aSThomas Huth         return;
171fcf5ef2aSThomas Huth     }
172fcf5ef2aSThomas Huth 
173fcf5ef2aSThomas Huth     if (ri->resetfn) {
174fcf5ef2aSThomas Huth         ri->resetfn(&cpu->env, ri);
175fcf5ef2aSThomas Huth         return;
176fcf5ef2aSThomas Huth     }
177fcf5ef2aSThomas Huth 
178fcf5ef2aSThomas Huth     /* A zero offset is never possible as it would be regs[0]
179fcf5ef2aSThomas Huth      * so we use it to indicate that reset is being handled elsewhere.
180fcf5ef2aSThomas Huth      * This is basically only used for fields in non-core coprocessors
181fcf5ef2aSThomas Huth      * (like the pxa2xx ones).
182fcf5ef2aSThomas Huth      */
183fcf5ef2aSThomas Huth     if (!ri->fieldoffset) {
184fcf5ef2aSThomas Huth         return;
185fcf5ef2aSThomas Huth     }
186fcf5ef2aSThomas Huth 
187fcf5ef2aSThomas Huth     if (cpreg_field_is_64bit(ri)) {
188fcf5ef2aSThomas Huth         CPREG_FIELD64(&cpu->env, ri) = ri->resetvalue;
189fcf5ef2aSThomas Huth     } else {
190fcf5ef2aSThomas Huth         CPREG_FIELD32(&cpu->env, ri) = ri->resetvalue;
191fcf5ef2aSThomas Huth     }
192fcf5ef2aSThomas Huth }
193fcf5ef2aSThomas Huth 
194fcf5ef2aSThomas Huth static void cp_reg_check_reset(gpointer key, gpointer value,  gpointer opaque)
195fcf5ef2aSThomas Huth {
196fcf5ef2aSThomas Huth     /* Purely an assertion check: we've already done reset once,
197fcf5ef2aSThomas Huth      * so now check that running the reset for the cpreg doesn't
198fcf5ef2aSThomas Huth      * change its value. This traps bugs where two different cpregs
199fcf5ef2aSThomas Huth      * both try to reset the same state field but to different values.
200fcf5ef2aSThomas Huth      */
201fcf5ef2aSThomas Huth     ARMCPRegInfo *ri = value;
202fcf5ef2aSThomas Huth     ARMCPU *cpu = opaque;
203fcf5ef2aSThomas Huth     uint64_t oldvalue, newvalue;
204fcf5ef2aSThomas Huth 
20587c3f0f2SRichard Henderson     if (ri->type & (ARM_CP_SPECIAL_MASK | ARM_CP_ALIAS | ARM_CP_NO_RAW)) {
206fcf5ef2aSThomas Huth         return;
207fcf5ef2aSThomas Huth     }
208fcf5ef2aSThomas Huth 
209fcf5ef2aSThomas Huth     oldvalue = read_raw_cp_reg(&cpu->env, ri);
210fcf5ef2aSThomas Huth     cp_reg_reset(key, value, opaque);
211fcf5ef2aSThomas Huth     newvalue = read_raw_cp_reg(&cpu->env, ri);
212fcf5ef2aSThomas Huth     assert(oldvalue == newvalue);
213fcf5ef2aSThomas Huth }
214fcf5ef2aSThomas Huth 
2159130cadeSPeter Maydell static void arm_cpu_reset_hold(Object *obj)
216fcf5ef2aSThomas Huth {
2179130cadeSPeter Maydell     CPUState *s = CPU(obj);
218fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(s);
219fcf5ef2aSThomas Huth     ARMCPUClass *acc = ARM_CPU_GET_CLASS(cpu);
220fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
221fcf5ef2aSThomas Huth 
2229130cadeSPeter Maydell     if (acc->parent_phases.hold) {
2239130cadeSPeter Maydell         acc->parent_phases.hold(obj);
2249130cadeSPeter Maydell     }
225fcf5ef2aSThomas Huth 
2261f5c00cfSAlex Bennée     memset(env, 0, offsetof(CPUARMState, end_reset_fields));
2271f5c00cfSAlex Bennée 
228fcf5ef2aSThomas Huth     g_hash_table_foreach(cpu->cp_regs, cp_reg_reset, cpu);
229fcf5ef2aSThomas Huth     g_hash_table_foreach(cpu->cp_regs, cp_reg_check_reset, cpu);
230fcf5ef2aSThomas Huth 
231fcf5ef2aSThomas Huth     env->vfp.xregs[ARM_VFP_FPSID] = cpu->reset_fpsid;
23247576b94SRichard Henderson     env->vfp.xregs[ARM_VFP_MVFR0] = cpu->isar.mvfr0;
23347576b94SRichard Henderson     env->vfp.xregs[ARM_VFP_MVFR1] = cpu->isar.mvfr1;
23447576b94SRichard Henderson     env->vfp.xregs[ARM_VFP_MVFR2] = cpu->isar.mvfr2;
235fcf5ef2aSThomas Huth 
236c1b70158SThiago Jung Bauermann     cpu->power_state = s->start_powered_off ? PSCI_OFF : PSCI_ON;
237fcf5ef2aSThomas Huth 
238fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
239fcf5ef2aSThomas Huth         env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
240fcf5ef2aSThomas Huth     }
241fcf5ef2aSThomas Huth 
242fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
243fcf5ef2aSThomas Huth         /* 64 bit CPUs always start in 64 bit mode */
24453221552SRichard Henderson         env->aarch64 = true;
245fcf5ef2aSThomas Huth #if defined(CONFIG_USER_ONLY)
246fcf5ef2aSThomas Huth         env->pstate = PSTATE_MODE_EL0t;
247fcf5ef2aSThomas Huth         /* Userspace expects access to DC ZVA, CTL_EL0 and the cache ops */
248fcf5ef2aSThomas Huth         env->cp15.sctlr_el[1] |= SCTLR_UCT | SCTLR_UCI | SCTLR_DZE;
249276c6e81SRichard Henderson         /* Enable all PAC keys.  */
250276c6e81SRichard Henderson         env->cp15.sctlr_el[1] |= (SCTLR_EnIA | SCTLR_EnIB |
251276c6e81SRichard Henderson                                   SCTLR_EnDA | SCTLR_EnDB);
252cda86e2bSRichard Henderson         /* Trap on btype=3 for PACIxSP. */
253cda86e2bSRichard Henderson         env->cp15.sctlr_el[1] |= SCTLR_BT0;
254d03396a8SRichard Henderson         /* Trap on implementation defined registers. */
255d03396a8SRichard Henderson         if (cpu_isar_feature(aa64_tidcp1, cpu)) {
256d03396a8SRichard Henderson             env->cp15.sctlr_el[1] |= SCTLR_TIDCP;
257d03396a8SRichard Henderson         }
258fcf5ef2aSThomas Huth         /* and to the FP/Neon instructions */
259fab8ad39SRichard Henderson         env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
260fab8ad39SRichard Henderson                                          CPACR_EL1, FPEN, 3);
26146303535SRichard Henderson         /* and to the SVE instructions, with default vector length */
26246303535SRichard Henderson         if (cpu_isar_feature(aa64_sve, cpu)) {
263fab8ad39SRichard Henderson             env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
264fab8ad39SRichard Henderson                                              CPACR_EL1, ZEN, 3);
26587252bdeSRichard Henderson             env->vfp.zcr_el[1] = cpu->sve_default_vq - 1;
2667b6a2198SAlex Bennée         }
26778011586SRichard Henderson         /* and for SME instructions, with default vector length, and TPIDR2 */
26878011586SRichard Henderson         if (cpu_isar_feature(aa64_sme, cpu)) {
26978011586SRichard Henderson             env->cp15.sctlr_el[1] |= SCTLR_EnTP2;
27078011586SRichard Henderson             env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
27178011586SRichard Henderson                                              CPACR_EL1, SMEN, 3);
27278011586SRichard Henderson             env->vfp.smcr_el[1] = cpu->sme_default_vq - 1;
27378011586SRichard Henderson             if (cpu_isar_feature(aa64_sme_fa64, cpu)) {
27478011586SRichard Henderson                 env->vfp.smcr_el[1] = FIELD_DP64(env->vfp.smcr_el[1],
27578011586SRichard Henderson                                                  SMCR, FA64, 1);
27678011586SRichard Henderson             }
27778011586SRichard Henderson         }
278f6a148feSRichard Henderson         /*
279691f1ffdSRichard Henderson          * Enable 48-bit address space (TODO: take reserved_va into account).
28016c84978SRichard Henderson          * Enable TBI0 but not TBI1.
28116c84978SRichard Henderson          * Note that this must match useronly_clean_ptr.
282f6a148feSRichard Henderson          */
283cb4a0a34SPeter Maydell         env->cp15.tcr_el[1] = 5 | (1ULL << 37);
284e3232864SRichard Henderson 
285e3232864SRichard Henderson         /* Enable MTE */
286e3232864SRichard Henderson         if (cpu_isar_feature(aa64_mte, cpu)) {
287e3232864SRichard Henderson             /* Enable tag access, but leave TCF0 as No Effect (0). */
288e3232864SRichard Henderson             env->cp15.sctlr_el[1] |= SCTLR_ATA0;
289e3232864SRichard Henderson             /*
290e3232864SRichard Henderson              * Exclude all tags, so that tag 0 is always used.
291e3232864SRichard Henderson              * This corresponds to Linux current->thread.gcr_incl = 0.
292e3232864SRichard Henderson              *
293e3232864SRichard Henderson              * Set RRND, so that helper_irg() will generate a seed later.
294e3232864SRichard Henderson              * Here in cpu_reset(), the crypto subsystem has not yet been
295e3232864SRichard Henderson              * initialized.
296e3232864SRichard Henderson              */
297e3232864SRichard Henderson             env->cp15.gcr_el1 = 0x1ffff;
298e3232864SRichard Henderson         }
2997cb1e618SRichard Henderson         /*
3007cb1e618SRichard Henderson          * Disable access to SCXTNUM_EL0 from CSV2_1p2.
3017cb1e618SRichard Henderson          * This is not yet exposed from the Linux kernel in any way.
3027cb1e618SRichard Henderson          */
3037cb1e618SRichard Henderson         env->cp15.sctlr_el[1] |= SCTLR_TSCXT;
304f9ac7788SZhuojia Shen         /* Disable access to Debug Communication Channel (DCC). */
305f9ac7788SZhuojia Shen         env->cp15.mdscr_el1 |= 1 << 12;
306a6b2c5a0SPeter Maydell         /* Enable FEAT_MOPS */
307a6b2c5a0SPeter Maydell         env->cp15.sctlr_el[1] |= SCTLR_MSCEN;
308fcf5ef2aSThomas Huth #else
309fcf5ef2aSThomas Huth         /* Reset into the highest available EL */
310fcf5ef2aSThomas Huth         if (arm_feature(env, ARM_FEATURE_EL3)) {
311fcf5ef2aSThomas Huth             env->pstate = PSTATE_MODE_EL3h;
312fcf5ef2aSThomas Huth         } else if (arm_feature(env, ARM_FEATURE_EL2)) {
313fcf5ef2aSThomas Huth             env->pstate = PSTATE_MODE_EL2h;
314fcf5ef2aSThomas Huth         } else {
315fcf5ef2aSThomas Huth             env->pstate = PSTATE_MODE_EL1h;
316fcf5ef2aSThomas Huth         }
3174a7319b7SEdgar E. Iglesias 
3184a7319b7SEdgar E. Iglesias         /* Sample rvbar at reset.  */
3194a7319b7SEdgar E. Iglesias         env->cp15.rvbar = cpu->rvbar_prop;
3204a7319b7SEdgar E. Iglesias         env->pc = env->cp15.rvbar;
321fcf5ef2aSThomas Huth #endif
322fcf5ef2aSThomas Huth     } else {
323fcf5ef2aSThomas Huth #if defined(CONFIG_USER_ONLY)
324fcf5ef2aSThomas Huth         /* Userspace expects access to cp10 and cp11 for FP/Neon */
325fab8ad39SRichard Henderson         env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
326fab8ad39SRichard Henderson                                          CPACR, CP10, 3);
327fab8ad39SRichard Henderson         env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
328fab8ad39SRichard Henderson                                          CPACR, CP11, 3);
329fcf5ef2aSThomas Huth #endif
330910e4f24STobias Röhmel         if (arm_feature(env, ARM_FEATURE_V8)) {
331910e4f24STobias Röhmel             env->cp15.rvbar = cpu->rvbar_prop;
332910e4f24STobias Röhmel             env->regs[15] = cpu->rvbar_prop;
333910e4f24STobias Röhmel         }
334fcf5ef2aSThomas Huth     }
335fcf5ef2aSThomas Huth 
336fcf5ef2aSThomas Huth #if defined(CONFIG_USER_ONLY)
337fcf5ef2aSThomas Huth     env->uncached_cpsr = ARM_CPU_MODE_USR;
338fcf5ef2aSThomas Huth     /* For user mode we must enable access to coprocessors */
339fcf5ef2aSThomas Huth     env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
340fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
341fcf5ef2aSThomas Huth         env->cp15.c15_cpar = 3;
342fcf5ef2aSThomas Huth     } else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
343fcf5ef2aSThomas Huth         env->cp15.c15_cpar = 1;
344fcf5ef2aSThomas Huth     }
345fcf5ef2aSThomas Huth #else
346060a65dfSPeter Maydell 
347060a65dfSPeter Maydell     /*
348060a65dfSPeter Maydell      * If the highest available EL is EL2, AArch32 will start in Hyp
349060a65dfSPeter Maydell      * mode; otherwise it starts in SVC. Note that if we start in
350060a65dfSPeter Maydell      * AArch64 then these values in the uncached_cpsr will be ignored.
351060a65dfSPeter Maydell      */
352060a65dfSPeter Maydell     if (arm_feature(env, ARM_FEATURE_EL2) &&
353060a65dfSPeter Maydell         !arm_feature(env, ARM_FEATURE_EL3)) {
354060a65dfSPeter Maydell         env->uncached_cpsr = ARM_CPU_MODE_HYP;
355060a65dfSPeter Maydell     } else {
356fcf5ef2aSThomas Huth         env->uncached_cpsr = ARM_CPU_MODE_SVC;
357060a65dfSPeter Maydell     }
358fcf5ef2aSThomas Huth     env->daif = PSTATE_D | PSTATE_A | PSTATE_I | PSTATE_F;
3591426f244SPeter Maydell 
3601426f244SPeter Maydell     /* AArch32 has a hard highvec setting of 0xFFFF0000.  If we are currently
3611426f244SPeter Maydell      * executing as AArch32 then check if highvecs are enabled and
3621426f244SPeter Maydell      * adjust the PC accordingly.
3631426f244SPeter Maydell      */
3641426f244SPeter Maydell     if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
3651426f244SPeter Maydell         env->regs[15] = 0xFFFF0000;
3661426f244SPeter Maydell     }
3671426f244SPeter Maydell 
3681426f244SPeter Maydell     env->vfp.xregs[ARM_VFP_FPEXC] = 0;
369b62ceeafSPeter Maydell #endif
370dc7abe4dSMichael Davidsaver 
371531c60a9SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M)) {
372b62ceeafSPeter Maydell #ifndef CONFIG_USER_ONLY
373fcf5ef2aSThomas Huth         uint32_t initial_msp; /* Loaded from 0x0 */
374fcf5ef2aSThomas Huth         uint32_t initial_pc; /* Loaded from 0x4 */
375fcf5ef2aSThomas Huth         uint8_t *rom;
37638e2a77cSPeter Maydell         uint32_t vecbase;
377b62ceeafSPeter Maydell #endif
378fcf5ef2aSThomas Huth 
3798128c8e8SPeter Maydell         if (cpu_isar_feature(aa32_lob, cpu)) {
3808128c8e8SPeter Maydell             /*
3818128c8e8SPeter Maydell              * LTPSIZE is constant 4 if MVE not implemented, and resets
3828128c8e8SPeter Maydell              * to an UNKNOWN value if MVE is implemented. We choose to
3838128c8e8SPeter Maydell              * always reset to 4.
3848128c8e8SPeter Maydell              */
3858128c8e8SPeter Maydell             env->v7m.ltpsize = 4;
38699c7834fSPeter Maydell             /* The LTPSIZE field in FPDSCR is constant and reads as 4. */
38799c7834fSPeter Maydell             env->v7m.fpdscr[M_REG_NS] = 4 << FPCR_LTPSIZE_SHIFT;
38899c7834fSPeter Maydell             env->v7m.fpdscr[M_REG_S] = 4 << FPCR_LTPSIZE_SHIFT;
3898128c8e8SPeter Maydell         }
3908128c8e8SPeter Maydell 
3911e577cc7SPeter Maydell         if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
3921e577cc7SPeter Maydell             env->v7m.secure = true;
3933b2e9344SPeter Maydell         } else {
3943b2e9344SPeter Maydell             /* This bit resets to 0 if security is supported, but 1 if
3953b2e9344SPeter Maydell              * it is not. The bit is not present in v7M, but we set it
3963b2e9344SPeter Maydell              * here so we can avoid having to make checks on it conditional
3973b2e9344SPeter Maydell              * on ARM_FEATURE_V8 (we don't let the guest see the bit).
3983b2e9344SPeter Maydell              */
3993b2e9344SPeter Maydell             env->v7m.aircr = R_V7M_AIRCR_BFHFNMINS_MASK;
40002ac2f7fSPeter Maydell             /*
40102ac2f7fSPeter Maydell              * Set NSACR to indicate "NS access permitted to everything";
40202ac2f7fSPeter Maydell              * this avoids having to have all the tests of it being
40302ac2f7fSPeter Maydell              * conditional on ARM_FEATURE_M_SECURITY. Note also that from
40402ac2f7fSPeter Maydell              * v8.1M the guest-visible value of NSACR in a CPU without the
40502ac2f7fSPeter Maydell              * Security Extension is 0xcff.
40602ac2f7fSPeter Maydell              */
40702ac2f7fSPeter Maydell             env->v7m.nsacr = 0xcff;
4081e577cc7SPeter Maydell         }
4091e577cc7SPeter Maydell 
4109d40cd8aSPeter Maydell         /* In v7M the reset value of this bit is IMPDEF, but ARM recommends
4112c4da50dSPeter Maydell          * that it resets to 1, so QEMU always does that rather than making
4129d40cd8aSPeter Maydell          * it dependent on CPU model. In v8M it is RES1.
4132c4da50dSPeter Maydell          */
4149d40cd8aSPeter Maydell         env->v7m.ccr[M_REG_NS] = R_V7M_CCR_STKALIGN_MASK;
4159d40cd8aSPeter Maydell         env->v7m.ccr[M_REG_S] = R_V7M_CCR_STKALIGN_MASK;
4169d40cd8aSPeter Maydell         if (arm_feature(env, ARM_FEATURE_V8)) {
4179d40cd8aSPeter Maydell             /* in v8M the NONBASETHRDENA bit [0] is RES1 */
4189d40cd8aSPeter Maydell             env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_NONBASETHRDENA_MASK;
4199d40cd8aSPeter Maydell             env->v7m.ccr[M_REG_S] |= R_V7M_CCR_NONBASETHRDENA_MASK;
4209d40cd8aSPeter Maydell         }
42122ab3460SJulia Suvorova         if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
42222ab3460SJulia Suvorova             env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_UNALIGN_TRP_MASK;
42322ab3460SJulia Suvorova             env->v7m.ccr[M_REG_S] |= R_V7M_CCR_UNALIGN_TRP_MASK;
42422ab3460SJulia Suvorova         }
4252c4da50dSPeter Maydell 
4267fbc6a40SRichard Henderson         if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
427d33abe82SPeter Maydell             env->v7m.fpccr[M_REG_NS] = R_V7M_FPCCR_ASPEN_MASK;
428d33abe82SPeter Maydell             env->v7m.fpccr[M_REG_S] = R_V7M_FPCCR_ASPEN_MASK |
429d33abe82SPeter Maydell                 R_V7M_FPCCR_LSPEN_MASK | R_V7M_FPCCR_S_MASK;
430d33abe82SPeter Maydell         }
431b62ceeafSPeter Maydell 
432b62ceeafSPeter Maydell #ifndef CONFIG_USER_ONLY
433056f43dfSPeter Maydell         /* Unlike A/R profile, M profile defines the reset LR value */
434056f43dfSPeter Maydell         env->regs[14] = 0xffffffff;
435056f43dfSPeter Maydell 
43638e2a77cSPeter Maydell         env->v7m.vecbase[M_REG_S] = cpu->init_svtor & 0xffffff80;
4377cda2149SPeter Maydell         env->v7m.vecbase[M_REG_NS] = cpu->init_nsvtor & 0xffffff80;
43838e2a77cSPeter Maydell 
43938e2a77cSPeter Maydell         /* Load the initial SP and PC from offset 0 and 4 in the vector table */
44038e2a77cSPeter Maydell         vecbase = env->v7m.vecbase[env->v7m.secure];
44175ce72b7SPeter Maydell         rom = rom_ptr_for_as(s->as, vecbase, 8);
442fcf5ef2aSThomas Huth         if (rom) {
443fcf5ef2aSThomas Huth             /* Address zero is covered by ROM which hasn't yet been
444fcf5ef2aSThomas Huth              * copied into physical memory.
445fcf5ef2aSThomas Huth              */
446fcf5ef2aSThomas Huth             initial_msp = ldl_p(rom);
447fcf5ef2aSThomas Huth             initial_pc = ldl_p(rom + 4);
448fcf5ef2aSThomas Huth         } else {
449fcf5ef2aSThomas Huth             /* Address zero not covered by a ROM blob, or the ROM blob
450fcf5ef2aSThomas Huth              * is in non-modifiable memory and this is a second reset after
451fcf5ef2aSThomas Huth              * it got copied into memory. In the latter case, rom_ptr
452fcf5ef2aSThomas Huth              * will return a NULL pointer and we should use ldl_phys instead.
453fcf5ef2aSThomas Huth              */
45438e2a77cSPeter Maydell             initial_msp = ldl_phys(s->as, vecbase);
45538e2a77cSPeter Maydell             initial_pc = ldl_phys(s->as, vecbase + 4);
456fcf5ef2aSThomas Huth         }
457fcf5ef2aSThomas Huth 
4588cc2246cSPeter Maydell         qemu_log_mask(CPU_LOG_INT,
4598cc2246cSPeter Maydell                       "Loaded reset SP 0x%x PC 0x%x from vector table\n",
4608cc2246cSPeter Maydell                       initial_msp, initial_pc);
4618cc2246cSPeter Maydell 
462fcf5ef2aSThomas Huth         env->regs[13] = initial_msp & 0xFFFFFFFC;
463fcf5ef2aSThomas Huth         env->regs[15] = initial_pc & ~1;
464fcf5ef2aSThomas Huth         env->thumb = initial_pc & 1;
465b62ceeafSPeter Maydell #else
466b62ceeafSPeter Maydell         /*
467b62ceeafSPeter Maydell          * For user mode we run non-secure and with access to the FPU.
468b62ceeafSPeter Maydell          * The FPU context is active (ie does not need further setup)
469b62ceeafSPeter Maydell          * and is owned by non-secure.
470b62ceeafSPeter Maydell          */
471b62ceeafSPeter Maydell         env->v7m.secure = false;
472b62ceeafSPeter Maydell         env->v7m.nsacr = 0xcff;
473b62ceeafSPeter Maydell         env->v7m.cpacr[M_REG_NS] = 0xf0ffff;
474b62ceeafSPeter Maydell         env->v7m.fpccr[M_REG_S] &=
475b62ceeafSPeter Maydell             ~(R_V7M_FPCCR_LSPEN_MASK | R_V7M_FPCCR_S_MASK);
476b62ceeafSPeter Maydell         env->v7m.control[M_REG_S] |= R_V7M_CONTROL_FPCA_MASK;
477b62ceeafSPeter Maydell #endif
478fcf5ef2aSThomas Huth     }
479fcf5ef2aSThomas Huth 
480dc3c4c14SPeter Maydell     /* M profile requires that reset clears the exclusive monitor;
481dc3c4c14SPeter Maydell      * A profile does not, but clearing it makes more sense than having it
482dc3c4c14SPeter Maydell      * set with an exclusive access on address zero.
483dc3c4c14SPeter Maydell      */
484dc3c4c14SPeter Maydell     arm_clear_exclusive(env);
485dc3c4c14SPeter Maydell 
4860e1a46bbSPeter Maydell     if (arm_feature(env, ARM_FEATURE_PMSA)) {
48769ceea64SPeter Maydell         if (cpu->pmsav7_dregion > 0) {
4880e1a46bbSPeter Maydell             if (arm_feature(env, ARM_FEATURE_V8)) {
48962c58ee0SPeter Maydell                 memset(env->pmsav8.rbar[M_REG_NS], 0,
49062c58ee0SPeter Maydell                        sizeof(*env->pmsav8.rbar[M_REG_NS])
49162c58ee0SPeter Maydell                        * cpu->pmsav7_dregion);
49262c58ee0SPeter Maydell                 memset(env->pmsav8.rlar[M_REG_NS], 0,
49362c58ee0SPeter Maydell                        sizeof(*env->pmsav8.rlar[M_REG_NS])
49462c58ee0SPeter Maydell                        * cpu->pmsav7_dregion);
49562c58ee0SPeter Maydell                 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
49662c58ee0SPeter Maydell                     memset(env->pmsav8.rbar[M_REG_S], 0,
49762c58ee0SPeter Maydell                            sizeof(*env->pmsav8.rbar[M_REG_S])
49862c58ee0SPeter Maydell                            * cpu->pmsav7_dregion);
49962c58ee0SPeter Maydell                     memset(env->pmsav8.rlar[M_REG_S], 0,
50062c58ee0SPeter Maydell                            sizeof(*env->pmsav8.rlar[M_REG_S])
50162c58ee0SPeter Maydell                            * cpu->pmsav7_dregion);
50262c58ee0SPeter Maydell                 }
5030e1a46bbSPeter Maydell             } else if (arm_feature(env, ARM_FEATURE_V7)) {
50469ceea64SPeter Maydell                 memset(env->pmsav7.drbar, 0,
50569ceea64SPeter Maydell                        sizeof(*env->pmsav7.drbar) * cpu->pmsav7_dregion);
50669ceea64SPeter Maydell                 memset(env->pmsav7.drsr, 0,
50769ceea64SPeter Maydell                        sizeof(*env->pmsav7.drsr) * cpu->pmsav7_dregion);
50869ceea64SPeter Maydell                 memset(env->pmsav7.dracr, 0,
50969ceea64SPeter Maydell                        sizeof(*env->pmsav7.dracr) * cpu->pmsav7_dregion);
51069ceea64SPeter Maydell             }
5110e1a46bbSPeter Maydell         }
512761c4642STobias Röhmel 
513761c4642STobias Röhmel         if (cpu->pmsav8r_hdregion > 0) {
514761c4642STobias Röhmel             memset(env->pmsav8.hprbar, 0,
515761c4642STobias Röhmel                    sizeof(*env->pmsav8.hprbar) * cpu->pmsav8r_hdregion);
516761c4642STobias Röhmel             memset(env->pmsav8.hprlar, 0,
517761c4642STobias Röhmel                    sizeof(*env->pmsav8.hprlar) * cpu->pmsav8r_hdregion);
518761c4642STobias Röhmel         }
519761c4642STobias Röhmel 
5201bc04a88SPeter Maydell         env->pmsav7.rnr[M_REG_NS] = 0;
5211bc04a88SPeter Maydell         env->pmsav7.rnr[M_REG_S] = 0;
5224125e6feSPeter Maydell         env->pmsav8.mair0[M_REG_NS] = 0;
5234125e6feSPeter Maydell         env->pmsav8.mair0[M_REG_S] = 0;
5244125e6feSPeter Maydell         env->pmsav8.mair1[M_REG_NS] = 0;
5254125e6feSPeter Maydell         env->pmsav8.mair1[M_REG_S] = 0;
52669ceea64SPeter Maydell     }
52769ceea64SPeter Maydell 
5289901c576SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
5299901c576SPeter Maydell         if (cpu->sau_sregion > 0) {
5309901c576SPeter Maydell             memset(env->sau.rbar, 0, sizeof(*env->sau.rbar) * cpu->sau_sregion);
5319901c576SPeter Maydell             memset(env->sau.rlar, 0, sizeof(*env->sau.rlar) * cpu->sau_sregion);
5329901c576SPeter Maydell         }
5339901c576SPeter Maydell         env->sau.rnr = 0;
5349901c576SPeter Maydell         /* SAU_CTRL reset value is IMPDEF; we choose 0, which is what
5359901c576SPeter Maydell          * the Cortex-M33 does.
5369901c576SPeter Maydell          */
5379901c576SPeter Maydell         env->sau.ctrl = 0;
5389901c576SPeter Maydell     }
5399901c576SPeter Maydell 
540fcf5ef2aSThomas Huth     set_flush_to_zero(1, &env->vfp.standard_fp_status);
541fcf5ef2aSThomas Huth     set_flush_inputs_to_zero(1, &env->vfp.standard_fp_status);
542fcf5ef2aSThomas Huth     set_default_nan_mode(1, &env->vfp.standard_fp_status);
543aaae563bSPeter Maydell     set_default_nan_mode(1, &env->vfp.standard_fp_status_f16);
544fcf5ef2aSThomas Huth     set_float_detect_tininess(float_tininess_before_rounding,
545fcf5ef2aSThomas Huth                               &env->vfp.fp_status);
546fcf5ef2aSThomas Huth     set_float_detect_tininess(float_tininess_before_rounding,
547fcf5ef2aSThomas Huth                               &env->vfp.standard_fp_status);
548bcc531f0SPeter Maydell     set_float_detect_tininess(float_tininess_before_rounding,
549bcc531f0SPeter Maydell                               &env->vfp.fp_status_f16);
550aaae563bSPeter Maydell     set_float_detect_tininess(float_tininess_before_rounding,
551aaae563bSPeter Maydell                               &env->vfp.standard_fp_status_f16);
552fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
553fcf5ef2aSThomas Huth     if (kvm_enabled()) {
554fcf5ef2aSThomas Huth         kvm_arm_reset_vcpu(cpu);
555fcf5ef2aSThomas Huth     }
556fcf5ef2aSThomas Huth #endif
557fcf5ef2aSThomas Huth 
558fa05d1abSFabiano Rosas     if (tcg_enabled()) {
559fcf5ef2aSThomas Huth         hw_breakpoint_update_all(cpu);
560fcf5ef2aSThomas Huth         hw_watchpoint_update_all(cpu);
5612b77ad4dSFabiano Rosas 
562a8a79c7aSRichard Henderson         arm_rebuild_hflags(env);
563fcf5ef2aSThomas Huth     }
5642b77ad4dSFabiano Rosas }
565fcf5ef2aSThomas Huth 
5663a45f4f5SPeter Maydell void arm_emulate_firmware_reset(CPUState *cpustate, int target_el)
5673a45f4f5SPeter Maydell {
5683a45f4f5SPeter Maydell     ARMCPU *cpu = ARM_CPU(cpustate);
5693a45f4f5SPeter Maydell     CPUARMState *env = &cpu->env;
5703a45f4f5SPeter Maydell     bool have_el3 = arm_feature(env, ARM_FEATURE_EL3);
5713a45f4f5SPeter Maydell     bool have_el2 = arm_feature(env, ARM_FEATURE_EL2);
5723a45f4f5SPeter Maydell 
5733a45f4f5SPeter Maydell     /*
5743a45f4f5SPeter Maydell      * Check we have the EL we're aiming for. If that is the
5753a45f4f5SPeter Maydell      * highest implemented EL, then cpu_reset has already done
5763a45f4f5SPeter Maydell      * all the work.
5773a45f4f5SPeter Maydell      */
5783a45f4f5SPeter Maydell     switch (target_el) {
5793a45f4f5SPeter Maydell     case 3:
5803a45f4f5SPeter Maydell         assert(have_el3);
5813a45f4f5SPeter Maydell         return;
5823a45f4f5SPeter Maydell     case 2:
5833a45f4f5SPeter Maydell         assert(have_el2);
5843a45f4f5SPeter Maydell         if (!have_el3) {
5853a45f4f5SPeter Maydell             return;
5863a45f4f5SPeter Maydell         }
5873a45f4f5SPeter Maydell         break;
5883a45f4f5SPeter Maydell     case 1:
5893a45f4f5SPeter Maydell         if (!have_el3 && !have_el2) {
5903a45f4f5SPeter Maydell             return;
5913a45f4f5SPeter Maydell         }
5923a45f4f5SPeter Maydell         break;
5933a45f4f5SPeter Maydell     default:
5943a45f4f5SPeter Maydell         g_assert_not_reached();
5953a45f4f5SPeter Maydell     }
5963a45f4f5SPeter Maydell 
5973a45f4f5SPeter Maydell     if (have_el3) {
5983a45f4f5SPeter Maydell         /*
5993a45f4f5SPeter Maydell          * Set the EL3 state so code can run at EL2. This should match
6003a45f4f5SPeter Maydell          * the requirements set by Linux in its booting spec.
6013a45f4f5SPeter Maydell          */
6023a45f4f5SPeter Maydell         if (env->aarch64) {
6033a45f4f5SPeter Maydell             env->cp15.scr_el3 |= SCR_RW;
6043a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_pauth, cpu)) {
6053a45f4f5SPeter Maydell                 env->cp15.scr_el3 |= SCR_API | SCR_APK;
6063a45f4f5SPeter Maydell             }
6073a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_mte, cpu)) {
6083a45f4f5SPeter Maydell                 env->cp15.scr_el3 |= SCR_ATA;
6093a45f4f5SPeter Maydell             }
6103a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_sve, cpu)) {
6113a45f4f5SPeter Maydell                 env->cp15.cptr_el[3] |= R_CPTR_EL3_EZ_MASK;
6123a45f4f5SPeter Maydell                 env->vfp.zcr_el[3] = 0xf;
6133a45f4f5SPeter Maydell             }
6143a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_sme, cpu)) {
6153a45f4f5SPeter Maydell                 env->cp15.cptr_el[3] |= R_CPTR_EL3_ESM_MASK;
6163a45f4f5SPeter Maydell                 env->cp15.scr_el3 |= SCR_ENTP2;
6173a45f4f5SPeter Maydell                 env->vfp.smcr_el[3] = 0xf;
6183a45f4f5SPeter Maydell             }
6193a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_hcx, cpu)) {
6203a45f4f5SPeter Maydell                 env->cp15.scr_el3 |= SCR_HXEN;
6213a45f4f5SPeter Maydell             }
6223a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_fgt, cpu)) {
6233a45f4f5SPeter Maydell                 env->cp15.scr_el3 |= SCR_FGTEN;
6243a45f4f5SPeter Maydell             }
6253a45f4f5SPeter Maydell         }
6263a45f4f5SPeter Maydell 
6273a45f4f5SPeter Maydell         if (target_el == 2) {
6283a45f4f5SPeter Maydell             /* If the guest is at EL2 then Linux expects the HVC insn to work */
6293a45f4f5SPeter Maydell             env->cp15.scr_el3 |= SCR_HCE;
6303a45f4f5SPeter Maydell         }
6313a45f4f5SPeter Maydell 
6323a45f4f5SPeter Maydell         /* Put CPU into non-secure state */
6333a45f4f5SPeter Maydell         env->cp15.scr_el3 |= SCR_NS;
6343a45f4f5SPeter Maydell         /* Set NSACR.{CP11,CP10} so NS can access the FPU */
6353a45f4f5SPeter Maydell         env->cp15.nsacr |= 3 << 10;
6363a45f4f5SPeter Maydell     }
6373a45f4f5SPeter Maydell 
6383a45f4f5SPeter Maydell     if (have_el2 && target_el < 2) {
6393a45f4f5SPeter Maydell         /* Set EL2 state so code can run at EL1. */
6403a45f4f5SPeter Maydell         if (env->aarch64) {
6413a45f4f5SPeter Maydell             env->cp15.hcr_el2 |= HCR_RW;
6423a45f4f5SPeter Maydell         }
6433a45f4f5SPeter Maydell     }
6443a45f4f5SPeter Maydell 
6453a45f4f5SPeter Maydell     /* Set the CPU to the desired state */
6463a45f4f5SPeter Maydell     if (env->aarch64) {
6473a45f4f5SPeter Maydell         env->pstate = aarch64_pstate_mode(target_el, true);
6483a45f4f5SPeter Maydell     } else {
6493a45f4f5SPeter Maydell         static const uint32_t mode_for_el[] = {
6503a45f4f5SPeter Maydell             0,
6513a45f4f5SPeter Maydell             ARM_CPU_MODE_SVC,
6523a45f4f5SPeter Maydell             ARM_CPU_MODE_HYP,
6533a45f4f5SPeter Maydell             ARM_CPU_MODE_SVC,
6543a45f4f5SPeter Maydell         };
6553a45f4f5SPeter Maydell 
6563a45f4f5SPeter Maydell         cpsr_write(env, mode_for_el[target_el], CPSR_M, CPSRWriteRaw);
6573a45f4f5SPeter Maydell     }
6583a45f4f5SPeter Maydell }
6593a45f4f5SPeter Maydell 
6603a45f4f5SPeter Maydell 
6619e406eeaSPhilippe Mathieu-Daudé #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
662083afd18SPhilippe Mathieu-Daudé 
663310cedf3SRichard Henderson static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx,
664be879556SRichard Henderson                                      unsigned int target_el,
665be879556SRichard Henderson                                      unsigned int cur_el, bool secure,
666be879556SRichard Henderson                                      uint64_t hcr_el2)
667310cedf3SRichard Henderson {
668b77af26eSRichard Henderson     CPUARMState *env = cpu_env(cs);
669310cedf3SRichard Henderson     bool pstate_unmasked;
67016e07f78SRichard Henderson     bool unmasked = false;
671310cedf3SRichard Henderson 
672310cedf3SRichard Henderson     /*
673310cedf3SRichard Henderson      * Don't take exceptions if they target a lower EL.
674310cedf3SRichard Henderson      * This check should catch any exceptions that would not be taken
675310cedf3SRichard Henderson      * but left pending.
676310cedf3SRichard Henderson      */
677310cedf3SRichard Henderson     if (cur_el > target_el) {
678310cedf3SRichard Henderson         return false;
679310cedf3SRichard Henderson     }
680310cedf3SRichard Henderson 
681310cedf3SRichard Henderson     switch (excp_idx) {
682310cedf3SRichard Henderson     case EXCP_FIQ:
683310cedf3SRichard Henderson         pstate_unmasked = !(env->daif & PSTATE_F);
684310cedf3SRichard Henderson         break;
685310cedf3SRichard Henderson 
686310cedf3SRichard Henderson     case EXCP_IRQ:
687310cedf3SRichard Henderson         pstate_unmasked = !(env->daif & PSTATE_I);
688310cedf3SRichard Henderson         break;
689310cedf3SRichard Henderson 
690310cedf3SRichard Henderson     case EXCP_VFIQ:
691cc974d5cSRémi Denis-Courmont         if (!(hcr_el2 & HCR_FMO) || (hcr_el2 & HCR_TGE)) {
692cc974d5cSRémi Denis-Courmont             /* VFIQs are only taken when hypervized.  */
693310cedf3SRichard Henderson             return false;
694310cedf3SRichard Henderson         }
695310cedf3SRichard Henderson         return !(env->daif & PSTATE_F);
696310cedf3SRichard Henderson     case EXCP_VIRQ:
697cc974d5cSRémi Denis-Courmont         if (!(hcr_el2 & HCR_IMO) || (hcr_el2 & HCR_TGE)) {
698cc974d5cSRémi Denis-Courmont             /* VIRQs are only taken when hypervized.  */
699310cedf3SRichard Henderson             return false;
700310cedf3SRichard Henderson         }
701310cedf3SRichard Henderson         return !(env->daif & PSTATE_I);
7023c29632fSRichard Henderson     case EXCP_VSERR:
7033c29632fSRichard Henderson         if (!(hcr_el2 & HCR_AMO) || (hcr_el2 & HCR_TGE)) {
7043c29632fSRichard Henderson             /* VIRQs are only taken when hypervized.  */
7053c29632fSRichard Henderson             return false;
7063c29632fSRichard Henderson         }
7073c29632fSRichard Henderson         return !(env->daif & PSTATE_A);
708310cedf3SRichard Henderson     default:
709310cedf3SRichard Henderson         g_assert_not_reached();
710310cedf3SRichard Henderson     }
711310cedf3SRichard Henderson 
712310cedf3SRichard Henderson     /*
713310cedf3SRichard Henderson      * Use the target EL, current execution state and SCR/HCR settings to
714310cedf3SRichard Henderson      * determine whether the corresponding CPSR bit is used to mask the
715310cedf3SRichard Henderson      * interrupt.
716310cedf3SRichard Henderson      */
717310cedf3SRichard Henderson     if ((target_el > cur_el) && (target_el != 1)) {
718310cedf3SRichard Henderson         /* Exceptions targeting a higher EL may not be maskable */
719310cedf3SRichard Henderson         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
720c939a7c7SAke Koomsin             switch (target_el) {
721c939a7c7SAke Koomsin             case 2:
722310cedf3SRichard Henderson                 /*
723c939a7c7SAke Koomsin                  * According to ARM DDI 0487H.a, an interrupt can be masked
724c939a7c7SAke Koomsin                  * when HCR_E2H and HCR_TGE are both set regardless of the
725c939a7c7SAke Koomsin                  * current Security state. Note that we need to revisit this
726c939a7c7SAke Koomsin                  * part again once we need to support NMI.
727310cedf3SRichard Henderson                  */
728c939a7c7SAke Koomsin                 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
72916e07f78SRichard Henderson                         unmasked = true;
730310cedf3SRichard Henderson                 }
731c939a7c7SAke Koomsin                 break;
732c939a7c7SAke Koomsin             case 3:
733c939a7c7SAke Koomsin                 /* Interrupt cannot be masked when the target EL is 3 */
734c939a7c7SAke Koomsin                 unmasked = true;
735c939a7c7SAke Koomsin                 break;
736c939a7c7SAke Koomsin             default:
737c939a7c7SAke Koomsin                 g_assert_not_reached();
738c939a7c7SAke Koomsin             }
739310cedf3SRichard Henderson         } else {
740310cedf3SRichard Henderson             /*
741310cedf3SRichard Henderson              * The old 32-bit-only environment has a more complicated
742310cedf3SRichard Henderson              * masking setup. HCR and SCR bits not only affect interrupt
743310cedf3SRichard Henderson              * routing but also change the behaviour of masking.
744310cedf3SRichard Henderson              */
745310cedf3SRichard Henderson             bool hcr, scr;
746310cedf3SRichard Henderson 
747310cedf3SRichard Henderson             switch (excp_idx) {
748310cedf3SRichard Henderson             case EXCP_FIQ:
749310cedf3SRichard Henderson                 /*
750310cedf3SRichard Henderson                  * If FIQs are routed to EL3 or EL2 then there are cases where
751310cedf3SRichard Henderson                  * we override the CPSR.F in determining if the exception is
752310cedf3SRichard Henderson                  * masked or not. If neither of these are set then we fall back
753310cedf3SRichard Henderson                  * to the CPSR.F setting otherwise we further assess the state
754310cedf3SRichard Henderson                  * below.
755310cedf3SRichard Henderson                  */
756310cedf3SRichard Henderson                 hcr = hcr_el2 & HCR_FMO;
757310cedf3SRichard Henderson                 scr = (env->cp15.scr_el3 & SCR_FIQ);
758310cedf3SRichard Henderson 
759310cedf3SRichard Henderson                 /*
760310cedf3SRichard Henderson                  * When EL3 is 32-bit, the SCR.FW bit controls whether the
761310cedf3SRichard Henderson                  * CPSR.F bit masks FIQ interrupts when taken in non-secure
762310cedf3SRichard Henderson                  * state. If SCR.FW is set then FIQs can be masked by CPSR.F
763310cedf3SRichard Henderson                  * when non-secure but only when FIQs are only routed to EL3.
764310cedf3SRichard Henderson                  */
765310cedf3SRichard Henderson                 scr = scr && !((env->cp15.scr_el3 & SCR_FW) && !hcr);
766310cedf3SRichard Henderson                 break;
767310cedf3SRichard Henderson             case EXCP_IRQ:
768310cedf3SRichard Henderson                 /*
769310cedf3SRichard Henderson                  * When EL3 execution state is 32-bit, if HCR.IMO is set then
770310cedf3SRichard Henderson                  * we may override the CPSR.I masking when in non-secure state.
771310cedf3SRichard Henderson                  * The SCR.IRQ setting has already been taken into consideration
772310cedf3SRichard Henderson                  * when setting the target EL, so it does not have a further
773310cedf3SRichard Henderson                  * affect here.
774310cedf3SRichard Henderson                  */
775310cedf3SRichard Henderson                 hcr = hcr_el2 & HCR_IMO;
776310cedf3SRichard Henderson                 scr = false;
777310cedf3SRichard Henderson                 break;
778310cedf3SRichard Henderson             default:
779310cedf3SRichard Henderson                 g_assert_not_reached();
780310cedf3SRichard Henderson             }
781310cedf3SRichard Henderson 
782310cedf3SRichard Henderson             if ((scr || hcr) && !secure) {
78316e07f78SRichard Henderson                 unmasked = true;
784310cedf3SRichard Henderson             }
785310cedf3SRichard Henderson         }
786310cedf3SRichard Henderson     }
787310cedf3SRichard Henderson 
788310cedf3SRichard Henderson     /*
789673d8215SMichael Tokarev      * The PSTATE bits only mask the interrupt if we have not overridden the
790310cedf3SRichard Henderson      * ability above.
791310cedf3SRichard Henderson      */
792310cedf3SRichard Henderson     return unmasked || pstate_unmasked;
793310cedf3SRichard Henderson }
794310cedf3SRichard Henderson 
795083afd18SPhilippe Mathieu-Daudé static bool arm_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
796fcf5ef2aSThomas Huth {
797fcf5ef2aSThomas Huth     CPUClass *cc = CPU_GET_CLASS(cs);
798b77af26eSRichard Henderson     CPUARMState *env = cpu_env(cs);
799fcf5ef2aSThomas Huth     uint32_t cur_el = arm_current_el(env);
800fcf5ef2aSThomas Huth     bool secure = arm_is_secure(env);
801be879556SRichard Henderson     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
802fcf5ef2aSThomas Huth     uint32_t target_el;
803fcf5ef2aSThomas Huth     uint32_t excp_idx;
804d63d0ec5SRichard Henderson 
805d63d0ec5SRichard Henderson     /* The prioritization of interrupts is IMPLEMENTATION DEFINED. */
806fcf5ef2aSThomas Huth 
807fcf5ef2aSThomas Huth     if (interrupt_request & CPU_INTERRUPT_FIQ) {
808fcf5ef2aSThomas Huth         excp_idx = EXCP_FIQ;
809fcf5ef2aSThomas Huth         target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
810be879556SRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
811be879556SRichard Henderson                               cur_el, secure, hcr_el2)) {
812d63d0ec5SRichard Henderson             goto found;
813fcf5ef2aSThomas Huth         }
814fcf5ef2aSThomas Huth     }
815fcf5ef2aSThomas Huth     if (interrupt_request & CPU_INTERRUPT_HARD) {
816fcf5ef2aSThomas Huth         excp_idx = EXCP_IRQ;
817fcf5ef2aSThomas Huth         target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
818be879556SRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
819be879556SRichard Henderson                               cur_el, secure, hcr_el2)) {
820d63d0ec5SRichard Henderson             goto found;
821fcf5ef2aSThomas Huth         }
822fcf5ef2aSThomas Huth     }
823fcf5ef2aSThomas Huth     if (interrupt_request & CPU_INTERRUPT_VIRQ) {
824fcf5ef2aSThomas Huth         excp_idx = EXCP_VIRQ;
825fcf5ef2aSThomas Huth         target_el = 1;
826be879556SRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
827be879556SRichard Henderson                               cur_el, secure, hcr_el2)) {
828d63d0ec5SRichard Henderson             goto found;
829fcf5ef2aSThomas Huth         }
830fcf5ef2aSThomas Huth     }
831fcf5ef2aSThomas Huth     if (interrupt_request & CPU_INTERRUPT_VFIQ) {
832fcf5ef2aSThomas Huth         excp_idx = EXCP_VFIQ;
833fcf5ef2aSThomas Huth         target_el = 1;
834be879556SRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
835be879556SRichard Henderson                               cur_el, secure, hcr_el2)) {
836d63d0ec5SRichard Henderson             goto found;
837d63d0ec5SRichard Henderson         }
838d63d0ec5SRichard Henderson     }
8393c29632fSRichard Henderson     if (interrupt_request & CPU_INTERRUPT_VSERR) {
8403c29632fSRichard Henderson         excp_idx = EXCP_VSERR;
8413c29632fSRichard Henderson         target_el = 1;
8423c29632fSRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
8433c29632fSRichard Henderson                               cur_el, secure, hcr_el2)) {
8443c29632fSRichard Henderson             /* Taking a virtual abort clears HCR_EL2.VSE */
8453c29632fSRichard Henderson             env->cp15.hcr_el2 &= ~HCR_VSE;
8463c29632fSRichard Henderson             cpu_reset_interrupt(cs, CPU_INTERRUPT_VSERR);
8473c29632fSRichard Henderson             goto found;
8483c29632fSRichard Henderson         }
8493c29632fSRichard Henderson     }
850d63d0ec5SRichard Henderson     return false;
851d63d0ec5SRichard Henderson 
852d63d0ec5SRichard Henderson  found:
853fcf5ef2aSThomas Huth     cs->exception_index = excp_idx;
854fcf5ef2aSThomas Huth     env->exception.target_el = target_el;
85578271684SClaudio Fontana     cc->tcg_ops->do_interrupt(cs);
856d63d0ec5SRichard Henderson     return true;
857fcf5ef2aSThomas Huth }
8589e406eeaSPhilippe Mathieu-Daudé 
8599e406eeaSPhilippe Mathieu-Daudé #endif /* CONFIG_TCG && !CONFIG_USER_ONLY */
860fcf5ef2aSThomas Huth 
86189430fc6SPeter Maydell void arm_cpu_update_virq(ARMCPU *cpu)
86289430fc6SPeter Maydell {
86389430fc6SPeter Maydell     /*
86489430fc6SPeter Maydell      * Update the interrupt level for VIRQ, which is the logical OR of
86589430fc6SPeter Maydell      * the HCR_EL2.VI bit and the input line level from the GIC.
86689430fc6SPeter Maydell      */
86789430fc6SPeter Maydell     CPUARMState *env = &cpu->env;
86889430fc6SPeter Maydell     CPUState *cs = CPU(cpu);
86989430fc6SPeter Maydell 
87089430fc6SPeter Maydell     bool new_state = (env->cp15.hcr_el2 & HCR_VI) ||
87189430fc6SPeter Maydell         (env->irq_line_state & CPU_INTERRUPT_VIRQ);
87289430fc6SPeter Maydell 
87389430fc6SPeter Maydell     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VIRQ) != 0)) {
87489430fc6SPeter Maydell         if (new_state) {
87589430fc6SPeter Maydell             cpu_interrupt(cs, CPU_INTERRUPT_VIRQ);
87689430fc6SPeter Maydell         } else {
87789430fc6SPeter Maydell             cpu_reset_interrupt(cs, CPU_INTERRUPT_VIRQ);
87889430fc6SPeter Maydell         }
87989430fc6SPeter Maydell     }
88089430fc6SPeter Maydell }
88189430fc6SPeter Maydell 
88289430fc6SPeter Maydell void arm_cpu_update_vfiq(ARMCPU *cpu)
88389430fc6SPeter Maydell {
88489430fc6SPeter Maydell     /*
88589430fc6SPeter Maydell      * Update the interrupt level for VFIQ, which is the logical OR of
88689430fc6SPeter Maydell      * the HCR_EL2.VF bit and the input line level from the GIC.
88789430fc6SPeter Maydell      */
88889430fc6SPeter Maydell     CPUARMState *env = &cpu->env;
88989430fc6SPeter Maydell     CPUState *cs = CPU(cpu);
89089430fc6SPeter Maydell 
89189430fc6SPeter Maydell     bool new_state = (env->cp15.hcr_el2 & HCR_VF) ||
89289430fc6SPeter Maydell         (env->irq_line_state & CPU_INTERRUPT_VFIQ);
89389430fc6SPeter Maydell 
89489430fc6SPeter Maydell     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VFIQ) != 0)) {
89589430fc6SPeter Maydell         if (new_state) {
89689430fc6SPeter Maydell             cpu_interrupt(cs, CPU_INTERRUPT_VFIQ);
89789430fc6SPeter Maydell         } else {
89889430fc6SPeter Maydell             cpu_reset_interrupt(cs, CPU_INTERRUPT_VFIQ);
89989430fc6SPeter Maydell         }
90089430fc6SPeter Maydell     }
90189430fc6SPeter Maydell }
90289430fc6SPeter Maydell 
9033c29632fSRichard Henderson void arm_cpu_update_vserr(ARMCPU *cpu)
9043c29632fSRichard Henderson {
9053c29632fSRichard Henderson     /*
9063c29632fSRichard Henderson      * Update the interrupt level for VSERR, which is the HCR_EL2.VSE bit.
9073c29632fSRichard Henderson      */
9083c29632fSRichard Henderson     CPUARMState *env = &cpu->env;
9093c29632fSRichard Henderson     CPUState *cs = CPU(cpu);
9103c29632fSRichard Henderson 
9113c29632fSRichard Henderson     bool new_state = env->cp15.hcr_el2 & HCR_VSE;
9123c29632fSRichard Henderson 
9133c29632fSRichard Henderson     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VSERR) != 0)) {
9143c29632fSRichard Henderson         if (new_state) {
9153c29632fSRichard Henderson             cpu_interrupt(cs, CPU_INTERRUPT_VSERR);
9163c29632fSRichard Henderson         } else {
9173c29632fSRichard Henderson             cpu_reset_interrupt(cs, CPU_INTERRUPT_VSERR);
9183c29632fSRichard Henderson         }
9193c29632fSRichard Henderson     }
9203c29632fSRichard Henderson }
9213c29632fSRichard Henderson 
922fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
923fcf5ef2aSThomas Huth static void arm_cpu_set_irq(void *opaque, int irq, int level)
924fcf5ef2aSThomas Huth {
925fcf5ef2aSThomas Huth     ARMCPU *cpu = opaque;
926fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
927fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
928fcf5ef2aSThomas Huth     static const int mask[] = {
929fcf5ef2aSThomas Huth         [ARM_CPU_IRQ] = CPU_INTERRUPT_HARD,
930fcf5ef2aSThomas Huth         [ARM_CPU_FIQ] = CPU_INTERRUPT_FIQ,
931fcf5ef2aSThomas Huth         [ARM_CPU_VIRQ] = CPU_INTERRUPT_VIRQ,
932fcf5ef2aSThomas Huth         [ARM_CPU_VFIQ] = CPU_INTERRUPT_VFIQ
933fcf5ef2aSThomas Huth     };
934fcf5ef2aSThomas Huth 
9359acd2d33SPeter Maydell     if (!arm_feature(env, ARM_FEATURE_EL2) &&
9369acd2d33SPeter Maydell         (irq == ARM_CPU_VIRQ || irq == ARM_CPU_VFIQ)) {
9379acd2d33SPeter Maydell         /*
9389acd2d33SPeter Maydell          * The GIC might tell us about VIRQ and VFIQ state, but if we don't
9399acd2d33SPeter Maydell          * have EL2 support we don't care. (Unless the guest is doing something
9409acd2d33SPeter Maydell          * silly this will only be calls saying "level is still 0".)
9419acd2d33SPeter Maydell          */
9429acd2d33SPeter Maydell         return;
9439acd2d33SPeter Maydell     }
9449acd2d33SPeter Maydell 
945ed89f078SPeter Maydell     if (level) {
946ed89f078SPeter Maydell         env->irq_line_state |= mask[irq];
947ed89f078SPeter Maydell     } else {
948ed89f078SPeter Maydell         env->irq_line_state &= ~mask[irq];
949ed89f078SPeter Maydell     }
950ed89f078SPeter Maydell 
951fcf5ef2aSThomas Huth     switch (irq) {
952fcf5ef2aSThomas Huth     case ARM_CPU_VIRQ:
95389430fc6SPeter Maydell         arm_cpu_update_virq(cpu);
95489430fc6SPeter Maydell         break;
955fcf5ef2aSThomas Huth     case ARM_CPU_VFIQ:
95689430fc6SPeter Maydell         arm_cpu_update_vfiq(cpu);
95789430fc6SPeter Maydell         break;
958fcf5ef2aSThomas Huth     case ARM_CPU_IRQ:
959fcf5ef2aSThomas Huth     case ARM_CPU_FIQ:
960fcf5ef2aSThomas Huth         if (level) {
961fcf5ef2aSThomas Huth             cpu_interrupt(cs, mask[irq]);
962fcf5ef2aSThomas Huth         } else {
963fcf5ef2aSThomas Huth             cpu_reset_interrupt(cs, mask[irq]);
964fcf5ef2aSThomas Huth         }
965fcf5ef2aSThomas Huth         break;
966fcf5ef2aSThomas Huth     default:
967fcf5ef2aSThomas Huth         g_assert_not_reached();
968fcf5ef2aSThomas Huth     }
969fcf5ef2aSThomas Huth }
970fcf5ef2aSThomas Huth 
971fcf5ef2aSThomas Huth static void arm_cpu_kvm_set_irq(void *opaque, int irq, int level)
972fcf5ef2aSThomas Huth {
973fcf5ef2aSThomas Huth #ifdef CONFIG_KVM
974fcf5ef2aSThomas Huth     ARMCPU *cpu = opaque;
975ed89f078SPeter Maydell     CPUARMState *env = &cpu->env;
976fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
977ed89f078SPeter Maydell     uint32_t linestate_bit;
978f6530926SEric Auger     int irq_id;
979fcf5ef2aSThomas Huth 
980fcf5ef2aSThomas Huth     switch (irq) {
981fcf5ef2aSThomas Huth     case ARM_CPU_IRQ:
982f6530926SEric Auger         irq_id = KVM_ARM_IRQ_CPU_IRQ;
983ed89f078SPeter Maydell         linestate_bit = CPU_INTERRUPT_HARD;
984fcf5ef2aSThomas Huth         break;
985fcf5ef2aSThomas Huth     case ARM_CPU_FIQ:
986f6530926SEric Auger         irq_id = KVM_ARM_IRQ_CPU_FIQ;
987ed89f078SPeter Maydell         linestate_bit = CPU_INTERRUPT_FIQ;
988fcf5ef2aSThomas Huth         break;
989fcf5ef2aSThomas Huth     default:
990fcf5ef2aSThomas Huth         g_assert_not_reached();
991fcf5ef2aSThomas Huth     }
992ed89f078SPeter Maydell 
993ed89f078SPeter Maydell     if (level) {
994ed89f078SPeter Maydell         env->irq_line_state |= linestate_bit;
995ed89f078SPeter Maydell     } else {
996ed89f078SPeter Maydell         env->irq_line_state &= ~linestate_bit;
997ed89f078SPeter Maydell     }
998f6530926SEric Auger     kvm_arm_set_irq(cs->cpu_index, KVM_ARM_IRQ_TYPE_CPU, irq_id, !!level);
999fcf5ef2aSThomas Huth #endif
1000fcf5ef2aSThomas Huth }
1001fcf5ef2aSThomas Huth 
1002fcf5ef2aSThomas Huth static bool arm_cpu_virtio_is_big_endian(CPUState *cs)
1003fcf5ef2aSThomas Huth {
1004fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(cs);
1005fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
1006fcf5ef2aSThomas Huth 
1007fcf5ef2aSThomas Huth     cpu_synchronize_state(cs);
1008fcf5ef2aSThomas Huth     return arm_cpu_data_is_big_endian(env);
1009fcf5ef2aSThomas Huth }
1010fcf5ef2aSThomas Huth 
1011fcf5ef2aSThomas Huth #endif
1012fcf5ef2aSThomas Huth 
1013fcf5ef2aSThomas Huth static void arm_disas_set_info(CPUState *cpu, disassemble_info *info)
1014fcf5ef2aSThomas Huth {
1015fcf5ef2aSThomas Huth     ARMCPU *ac = ARM_CPU(cpu);
1016fcf5ef2aSThomas Huth     CPUARMState *env = &ac->env;
10177bcdbf51SRichard Henderson     bool sctlr_b;
1018fcf5ef2aSThomas Huth 
1019fcf5ef2aSThomas Huth     if (is_a64(env)) {
1020110f6c70SRichard Henderson         info->cap_arch = CS_ARCH_ARM64;
102115fa1a0aSRichard Henderson         info->cap_insn_unit = 4;
102215fa1a0aSRichard Henderson         info->cap_insn_split = 4;
1023110f6c70SRichard Henderson     } else {
1024110f6c70SRichard Henderson         int cap_mode;
1025110f6c70SRichard Henderson         if (env->thumb) {
102615fa1a0aSRichard Henderson             info->cap_insn_unit = 2;
102715fa1a0aSRichard Henderson             info->cap_insn_split = 4;
1028110f6c70SRichard Henderson             cap_mode = CS_MODE_THUMB;
1029fcf5ef2aSThomas Huth         } else {
103015fa1a0aSRichard Henderson             info->cap_insn_unit = 4;
103115fa1a0aSRichard Henderson             info->cap_insn_split = 4;
1032110f6c70SRichard Henderson             cap_mode = CS_MODE_ARM;
1033fcf5ef2aSThomas Huth         }
1034110f6c70SRichard Henderson         if (arm_feature(env, ARM_FEATURE_V8)) {
1035110f6c70SRichard Henderson             cap_mode |= CS_MODE_V8;
1036110f6c70SRichard Henderson         }
1037110f6c70SRichard Henderson         if (arm_feature(env, ARM_FEATURE_M)) {
1038110f6c70SRichard Henderson             cap_mode |= CS_MODE_MCLASS;
1039110f6c70SRichard Henderson         }
1040110f6c70SRichard Henderson         info->cap_arch = CS_ARCH_ARM;
1041110f6c70SRichard Henderson         info->cap_mode = cap_mode;
1042fcf5ef2aSThomas Huth     }
10437bcdbf51SRichard Henderson 
10447bcdbf51SRichard Henderson     sctlr_b = arm_sctlr_b(env);
10457bcdbf51SRichard Henderson     if (bswap_code(sctlr_b)) {
1046ee3eb3a7SMarc-André Lureau #if TARGET_BIG_ENDIAN
1047fcf5ef2aSThomas Huth         info->endian = BFD_ENDIAN_LITTLE;
1048fcf5ef2aSThomas Huth #else
1049fcf5ef2aSThomas Huth         info->endian = BFD_ENDIAN_BIG;
1050fcf5ef2aSThomas Huth #endif
1051fcf5ef2aSThomas Huth     }
1052f7478a92SJulian Brown     info->flags &= ~INSN_ARM_BE32;
10537bcdbf51SRichard Henderson #ifndef CONFIG_USER_ONLY
10547bcdbf51SRichard Henderson     if (sctlr_b) {
1055f7478a92SJulian Brown         info->flags |= INSN_ARM_BE32;
1056f7478a92SJulian Brown     }
10577bcdbf51SRichard Henderson #endif
1058fcf5ef2aSThomas Huth }
1059fcf5ef2aSThomas Huth 
106086480615SPhilippe Mathieu-Daudé #ifdef TARGET_AARCH64
106186480615SPhilippe Mathieu-Daudé 
106286480615SPhilippe Mathieu-Daudé static void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags)
106386480615SPhilippe Mathieu-Daudé {
106486480615SPhilippe Mathieu-Daudé     ARMCPU *cpu = ARM_CPU(cs);
106586480615SPhilippe Mathieu-Daudé     CPUARMState *env = &cpu->env;
106686480615SPhilippe Mathieu-Daudé     uint32_t psr = pstate_read(env);
1067a9d84070SRichard Henderson     int i, j;
106886480615SPhilippe Mathieu-Daudé     int el = arm_current_el(env);
1069bde0e60bSPeter Maydell     uint64_t hcr = arm_hcr_el2_eff(env);
107086480615SPhilippe Mathieu-Daudé     const char *ns_status;
10717a867dd5SRichard Henderson     bool sve;
107286480615SPhilippe Mathieu-Daudé 
107386480615SPhilippe Mathieu-Daudé     qemu_fprintf(f, " PC=%016" PRIx64 " ", env->pc);
107486480615SPhilippe Mathieu-Daudé     for (i = 0; i < 32; i++) {
107586480615SPhilippe Mathieu-Daudé         if (i == 31) {
107686480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, " SP=%016" PRIx64 "\n", env->xregs[i]);
107786480615SPhilippe Mathieu-Daudé         } else {
107886480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "X%02d=%016" PRIx64 "%s", i, env->xregs[i],
107986480615SPhilippe Mathieu-Daudé                          (i + 2) % 3 ? " " : "\n");
108086480615SPhilippe Mathieu-Daudé         }
108186480615SPhilippe Mathieu-Daudé     }
108286480615SPhilippe Mathieu-Daudé 
108386480615SPhilippe Mathieu-Daudé     if (arm_feature(env, ARM_FEATURE_EL3) && el != 3) {
108486480615SPhilippe Mathieu-Daudé         ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
108586480615SPhilippe Mathieu-Daudé     } else {
108686480615SPhilippe Mathieu-Daudé         ns_status = "";
108786480615SPhilippe Mathieu-Daudé     }
108886480615SPhilippe Mathieu-Daudé     qemu_fprintf(f, "PSTATE=%08x %c%c%c%c %sEL%d%c",
108986480615SPhilippe Mathieu-Daudé                  psr,
109086480615SPhilippe Mathieu-Daudé                  psr & PSTATE_N ? 'N' : '-',
109186480615SPhilippe Mathieu-Daudé                  psr & PSTATE_Z ? 'Z' : '-',
109286480615SPhilippe Mathieu-Daudé                  psr & PSTATE_C ? 'C' : '-',
109386480615SPhilippe Mathieu-Daudé                  psr & PSTATE_V ? 'V' : '-',
109486480615SPhilippe Mathieu-Daudé                  ns_status,
109586480615SPhilippe Mathieu-Daudé                  el,
109686480615SPhilippe Mathieu-Daudé                  psr & PSTATE_SP ? 'h' : 't');
109786480615SPhilippe Mathieu-Daudé 
10987a867dd5SRichard Henderson     if (cpu_isar_feature(aa64_sme, cpu)) {
10997a867dd5SRichard Henderson         qemu_fprintf(f, "  SVCR=%08" PRIx64 " %c%c",
11007a867dd5SRichard Henderson                      env->svcr,
11017a867dd5SRichard Henderson                      (FIELD_EX64(env->svcr, SVCR, ZA) ? 'Z' : '-'),
11027a867dd5SRichard Henderson                      (FIELD_EX64(env->svcr, SVCR, SM) ? 'S' : '-'));
11037a867dd5SRichard Henderson     }
110486480615SPhilippe Mathieu-Daudé     if (cpu_isar_feature(aa64_bti, cpu)) {
110586480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "  BTYPE=%d", (psr & PSTATE_BTYPE) >> 10);
110686480615SPhilippe Mathieu-Daudé     }
1107bde0e60bSPeter Maydell     qemu_fprintf(f, "%s%s%s",
1108bde0e60bSPeter Maydell                  (hcr & HCR_NV) ? " NV" : "",
1109bde0e60bSPeter Maydell                  (hcr & HCR_NV1) ? " NV1" : "",
1110bde0e60bSPeter Maydell                  (hcr & HCR_NV2) ? " NV2" : "");
111186480615SPhilippe Mathieu-Daudé     if (!(flags & CPU_DUMP_FPU)) {
111286480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "\n");
111386480615SPhilippe Mathieu-Daudé         return;
111486480615SPhilippe Mathieu-Daudé     }
111586480615SPhilippe Mathieu-Daudé     if (fp_exception_el(env, el) != 0) {
111686480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "    FPU disabled\n");
111786480615SPhilippe Mathieu-Daudé         return;
111886480615SPhilippe Mathieu-Daudé     }
111986480615SPhilippe Mathieu-Daudé     qemu_fprintf(f, "     FPCR=%08x FPSR=%08x\n",
112086480615SPhilippe Mathieu-Daudé                  vfp_get_fpcr(env), vfp_get_fpsr(env));
112186480615SPhilippe Mathieu-Daudé 
11227a867dd5SRichard Henderson     if (cpu_isar_feature(aa64_sme, cpu) && FIELD_EX64(env->svcr, SVCR, SM)) {
11237a867dd5SRichard Henderson         sve = sme_exception_el(env, el) == 0;
11247a867dd5SRichard Henderson     } else if (cpu_isar_feature(aa64_sve, cpu)) {
11257a867dd5SRichard Henderson         sve = sve_exception_el(env, el) == 0;
11267a867dd5SRichard Henderson     } else {
11277a867dd5SRichard Henderson         sve = false;
11287a867dd5SRichard Henderson     }
11297a867dd5SRichard Henderson 
11307a867dd5SRichard Henderson     if (sve) {
1131a9d84070SRichard Henderson         int zcr_len = sve_vqm1_for_el(env, el);
113286480615SPhilippe Mathieu-Daudé 
113386480615SPhilippe Mathieu-Daudé         for (i = 0; i <= FFR_PRED_NUM; i++) {
113486480615SPhilippe Mathieu-Daudé             bool eol;
113586480615SPhilippe Mathieu-Daudé             if (i == FFR_PRED_NUM) {
113686480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "FFR=");
113786480615SPhilippe Mathieu-Daudé                 /* It's last, so end the line.  */
113886480615SPhilippe Mathieu-Daudé                 eol = true;
113986480615SPhilippe Mathieu-Daudé             } else {
114086480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "P%02d=", i);
114186480615SPhilippe Mathieu-Daudé                 switch (zcr_len) {
114286480615SPhilippe Mathieu-Daudé                 case 0:
114386480615SPhilippe Mathieu-Daudé                     eol = i % 8 == 7;
114486480615SPhilippe Mathieu-Daudé                     break;
114586480615SPhilippe Mathieu-Daudé                 case 1:
114686480615SPhilippe Mathieu-Daudé                     eol = i % 6 == 5;
114786480615SPhilippe Mathieu-Daudé                     break;
114886480615SPhilippe Mathieu-Daudé                 case 2:
114986480615SPhilippe Mathieu-Daudé                 case 3:
115086480615SPhilippe Mathieu-Daudé                     eol = i % 3 == 2;
115186480615SPhilippe Mathieu-Daudé                     break;
115286480615SPhilippe Mathieu-Daudé                 default:
115386480615SPhilippe Mathieu-Daudé                     /* More than one quadword per predicate.  */
115486480615SPhilippe Mathieu-Daudé                     eol = true;
115586480615SPhilippe Mathieu-Daudé                     break;
115686480615SPhilippe Mathieu-Daudé                 }
115786480615SPhilippe Mathieu-Daudé             }
115886480615SPhilippe Mathieu-Daudé             for (j = zcr_len / 4; j >= 0; j--) {
115986480615SPhilippe Mathieu-Daudé                 int digits;
116086480615SPhilippe Mathieu-Daudé                 if (j * 4 + 4 <= zcr_len + 1) {
116186480615SPhilippe Mathieu-Daudé                     digits = 16;
116286480615SPhilippe Mathieu-Daudé                 } else {
116386480615SPhilippe Mathieu-Daudé                     digits = (zcr_len % 4 + 1) * 4;
116486480615SPhilippe Mathieu-Daudé                 }
116586480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "%0*" PRIx64 "%s", digits,
116686480615SPhilippe Mathieu-Daudé                              env->vfp.pregs[i].p[j],
116786480615SPhilippe Mathieu-Daudé                              j ? ":" : eol ? "\n" : " ");
116886480615SPhilippe Mathieu-Daudé             }
116986480615SPhilippe Mathieu-Daudé         }
117086480615SPhilippe Mathieu-Daudé 
117186480615SPhilippe Mathieu-Daudé         if (zcr_len == 0) {
1172a9d84070SRichard Henderson             /*
1173a9d84070SRichard Henderson              * With vl=16, there are only 37 columns per register,
1174a9d84070SRichard Henderson              * so output two registers per line.
1175a9d84070SRichard Henderson              */
1176a9d84070SRichard Henderson             for (i = 0; i < 32; i++) {
117786480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "Z%02d=%016" PRIx64 ":%016" PRIx64 "%s",
117886480615SPhilippe Mathieu-Daudé                              i, env->vfp.zregs[i].d[1],
117986480615SPhilippe Mathieu-Daudé                              env->vfp.zregs[i].d[0], i & 1 ? "\n" : " ");
1180a9d84070SRichard Henderson             }
118186480615SPhilippe Mathieu-Daudé         } else {
1182a9d84070SRichard Henderson             for (i = 0; i < 32; i++) {
1183a9d84070SRichard Henderson                 qemu_fprintf(f, "Z%02d=", i);
118486480615SPhilippe Mathieu-Daudé                 for (j = zcr_len; j >= 0; j--) {
118586480615SPhilippe Mathieu-Daudé                     qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%s",
118686480615SPhilippe Mathieu-Daudé                                  env->vfp.zregs[i].d[j * 2 + 1],
1187a9d84070SRichard Henderson                                  env->vfp.zregs[i].d[j * 2 + 0],
1188a9d84070SRichard Henderson                                  j ? ":" : "\n");
118986480615SPhilippe Mathieu-Daudé                 }
119086480615SPhilippe Mathieu-Daudé             }
119186480615SPhilippe Mathieu-Daudé         }
119286480615SPhilippe Mathieu-Daudé     } else {
119386480615SPhilippe Mathieu-Daudé         for (i = 0; i < 32; i++) {
119486480615SPhilippe Mathieu-Daudé             uint64_t *q = aa64_vfp_qreg(env, i);
119586480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "Q%02d=%016" PRIx64 ":%016" PRIx64 "%s",
119686480615SPhilippe Mathieu-Daudé                          i, q[1], q[0], (i & 1 ? "\n" : " "));
119786480615SPhilippe Mathieu-Daudé         }
119886480615SPhilippe Mathieu-Daudé     }
1199270bea47SRichard Henderson 
1200270bea47SRichard Henderson     if (cpu_isar_feature(aa64_sme, cpu) &&
1201270bea47SRichard Henderson         FIELD_EX64(env->svcr, SVCR, ZA) &&
1202270bea47SRichard Henderson         sme_exception_el(env, el) == 0) {
1203270bea47SRichard Henderson         int zcr_len = sve_vqm1_for_el_sm(env, el, true);
1204270bea47SRichard Henderson         int svl = (zcr_len + 1) * 16;
1205270bea47SRichard Henderson         int svl_lg10 = svl < 100 ? 2 : 3;
1206270bea47SRichard Henderson 
1207270bea47SRichard Henderson         for (i = 0; i < svl; i++) {
1208270bea47SRichard Henderson             qemu_fprintf(f, "ZA[%0*d]=", svl_lg10, i);
1209270bea47SRichard Henderson             for (j = zcr_len; j >= 0; --j) {
1210270bea47SRichard Henderson                 qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%c",
1211270bea47SRichard Henderson                              env->zarray[i].d[2 * j + 1],
1212270bea47SRichard Henderson                              env->zarray[i].d[2 * j],
1213270bea47SRichard Henderson                              j ? ':' : '\n');
1214270bea47SRichard Henderson             }
1215270bea47SRichard Henderson         }
1216270bea47SRichard Henderson     }
121786480615SPhilippe Mathieu-Daudé }
121886480615SPhilippe Mathieu-Daudé 
121986480615SPhilippe Mathieu-Daudé #else
122086480615SPhilippe Mathieu-Daudé 
122186480615SPhilippe Mathieu-Daudé static inline void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags)
122286480615SPhilippe Mathieu-Daudé {
122386480615SPhilippe Mathieu-Daudé     g_assert_not_reached();
122486480615SPhilippe Mathieu-Daudé }
122586480615SPhilippe Mathieu-Daudé 
122686480615SPhilippe Mathieu-Daudé #endif
122786480615SPhilippe Mathieu-Daudé 
122886480615SPhilippe Mathieu-Daudé static void arm_cpu_dump_state(CPUState *cs, FILE *f, int flags)
122986480615SPhilippe Mathieu-Daudé {
123086480615SPhilippe Mathieu-Daudé     ARMCPU *cpu = ARM_CPU(cs);
123186480615SPhilippe Mathieu-Daudé     CPUARMState *env = &cpu->env;
123286480615SPhilippe Mathieu-Daudé     int i;
123386480615SPhilippe Mathieu-Daudé 
123486480615SPhilippe Mathieu-Daudé     if (is_a64(env)) {
123586480615SPhilippe Mathieu-Daudé         aarch64_cpu_dump_state(cs, f, flags);
123686480615SPhilippe Mathieu-Daudé         return;
123786480615SPhilippe Mathieu-Daudé     }
123886480615SPhilippe Mathieu-Daudé 
123986480615SPhilippe Mathieu-Daudé     for (i = 0; i < 16; i++) {
124086480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "R%02d=%08x", i, env->regs[i]);
124186480615SPhilippe Mathieu-Daudé         if ((i % 4) == 3) {
124286480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "\n");
124386480615SPhilippe Mathieu-Daudé         } else {
124486480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, " ");
124586480615SPhilippe Mathieu-Daudé         }
124686480615SPhilippe Mathieu-Daudé     }
124786480615SPhilippe Mathieu-Daudé 
124886480615SPhilippe Mathieu-Daudé     if (arm_feature(env, ARM_FEATURE_M)) {
124986480615SPhilippe Mathieu-Daudé         uint32_t xpsr = xpsr_read(env);
125086480615SPhilippe Mathieu-Daudé         const char *mode;
125186480615SPhilippe Mathieu-Daudé         const char *ns_status = "";
125286480615SPhilippe Mathieu-Daudé 
125386480615SPhilippe Mathieu-Daudé         if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
125486480615SPhilippe Mathieu-Daudé             ns_status = env->v7m.secure ? "S " : "NS ";
125586480615SPhilippe Mathieu-Daudé         }
125686480615SPhilippe Mathieu-Daudé 
125786480615SPhilippe Mathieu-Daudé         if (xpsr & XPSR_EXCP) {
125886480615SPhilippe Mathieu-Daudé             mode = "handler";
125986480615SPhilippe Mathieu-Daudé         } else {
126086480615SPhilippe Mathieu-Daudé             if (env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_NPRIV_MASK) {
126186480615SPhilippe Mathieu-Daudé                 mode = "unpriv-thread";
126286480615SPhilippe Mathieu-Daudé             } else {
126386480615SPhilippe Mathieu-Daudé                 mode = "priv-thread";
126486480615SPhilippe Mathieu-Daudé             }
126586480615SPhilippe Mathieu-Daudé         }
126686480615SPhilippe Mathieu-Daudé 
126786480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "XPSR=%08x %c%c%c%c %c %s%s\n",
126886480615SPhilippe Mathieu-Daudé                      xpsr,
126986480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_N ? 'N' : '-',
127086480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_Z ? 'Z' : '-',
127186480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_C ? 'C' : '-',
127286480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_V ? 'V' : '-',
127386480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_T ? 'T' : 'A',
127486480615SPhilippe Mathieu-Daudé                      ns_status,
127586480615SPhilippe Mathieu-Daudé                      mode);
127686480615SPhilippe Mathieu-Daudé     } else {
127786480615SPhilippe Mathieu-Daudé         uint32_t psr = cpsr_read(env);
127886480615SPhilippe Mathieu-Daudé         const char *ns_status = "";
127986480615SPhilippe Mathieu-Daudé 
128086480615SPhilippe Mathieu-Daudé         if (arm_feature(env, ARM_FEATURE_EL3) &&
128186480615SPhilippe Mathieu-Daudé             (psr & CPSR_M) != ARM_CPU_MODE_MON) {
128286480615SPhilippe Mathieu-Daudé             ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
128386480615SPhilippe Mathieu-Daudé         }
128486480615SPhilippe Mathieu-Daudé 
128586480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "PSR=%08x %c%c%c%c %c %s%s%d\n",
128686480615SPhilippe Mathieu-Daudé                      psr,
128786480615SPhilippe Mathieu-Daudé                      psr & CPSR_N ? 'N' : '-',
128886480615SPhilippe Mathieu-Daudé                      psr & CPSR_Z ? 'Z' : '-',
128986480615SPhilippe Mathieu-Daudé                      psr & CPSR_C ? 'C' : '-',
129086480615SPhilippe Mathieu-Daudé                      psr & CPSR_V ? 'V' : '-',
129186480615SPhilippe Mathieu-Daudé                      psr & CPSR_T ? 'T' : 'A',
129286480615SPhilippe Mathieu-Daudé                      ns_status,
129386480615SPhilippe Mathieu-Daudé                      aarch32_mode_name(psr), (psr & 0x10) ? 32 : 26);
129486480615SPhilippe Mathieu-Daudé     }
129586480615SPhilippe Mathieu-Daudé 
129686480615SPhilippe Mathieu-Daudé     if (flags & CPU_DUMP_FPU) {
129786480615SPhilippe Mathieu-Daudé         int numvfpregs = 0;
1298a6627f5fSRichard Henderson         if (cpu_isar_feature(aa32_simd_r32, cpu)) {
1299a6627f5fSRichard Henderson             numvfpregs = 32;
13007fbc6a40SRichard Henderson         } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
1301a6627f5fSRichard Henderson             numvfpregs = 16;
130286480615SPhilippe Mathieu-Daudé         }
130386480615SPhilippe Mathieu-Daudé         for (i = 0; i < numvfpregs; i++) {
130486480615SPhilippe Mathieu-Daudé             uint64_t v = *aa32_vfp_dreg(env, i);
130586480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "s%02d=%08x s%02d=%08x d%02d=%016" PRIx64 "\n",
130686480615SPhilippe Mathieu-Daudé                          i * 2, (uint32_t)v,
130786480615SPhilippe Mathieu-Daudé                          i * 2 + 1, (uint32_t)(v >> 32),
130886480615SPhilippe Mathieu-Daudé                          i, v);
130986480615SPhilippe Mathieu-Daudé         }
131086480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "FPSCR: %08x\n", vfp_get_fpscr(env));
1311aa291908SPeter Maydell         if (cpu_isar_feature(aa32_mve, cpu)) {
1312aa291908SPeter Maydell             qemu_fprintf(f, "VPR: %08x\n", env->v7m.vpr);
1313aa291908SPeter Maydell         }
131486480615SPhilippe Mathieu-Daudé     }
131586480615SPhilippe Mathieu-Daudé }
131686480615SPhilippe Mathieu-Daudé 
1317750245edSRichard Henderson uint64_t arm_build_mp_affinity(int idx, uint8_t clustersz)
131846de5913SIgor Mammedov {
131946de5913SIgor Mammedov     uint32_t Aff1 = idx / clustersz;
132046de5913SIgor Mammedov     uint32_t Aff0 = idx % clustersz;
132146de5913SIgor Mammedov     return (Aff1 << ARM_AFF1_SHIFT) | Aff0;
132246de5913SIgor Mammedov }
132346de5913SIgor Mammedov 
1324e2d8cf9bSPhilippe Mathieu-Daudé uint64_t arm_cpu_mp_affinity(ARMCPU *cpu)
1325e2d8cf9bSPhilippe Mathieu-Daudé {
1326e2d8cf9bSPhilippe Mathieu-Daudé     return cpu->mp_affinity;
1327e2d8cf9bSPhilippe Mathieu-Daudé }
1328e2d8cf9bSPhilippe Mathieu-Daudé 
1329fcf5ef2aSThomas Huth static void arm_cpu_initfn(Object *obj)
1330fcf5ef2aSThomas Huth {
1331fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(obj);
1332fcf5ef2aSThomas Huth 
13335860362dSRichard Henderson     cpu->cp_regs = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1334c27f5d3aSRichard Henderson                                          NULL, g_free);
1335fcf5ef2aSThomas Huth 
1336b5c53d1bSAaron Lindsay     QLIST_INIT(&cpu->pre_el_change_hooks);
133708267487SAaron Lindsay     QLIST_INIT(&cpu->el_change_hooks);
133808267487SAaron Lindsay 
1339b3d52804SRichard Henderson #ifdef CONFIG_USER_ONLY
1340b3d52804SRichard Henderson # ifdef TARGET_AARCH64
1341b3d52804SRichard Henderson     /*
1342e74c0976SRichard Henderson      * The linux kernel defaults to 512-bit for SVE, and 256-bit for SME.
1343e74c0976SRichard Henderson      * These values were chosen to fit within the default signal frame.
1344e74c0976SRichard Henderson      * See documentation for /proc/sys/abi/{sve,sme}_default_vector_length,
1345e74c0976SRichard Henderson      * and our corresponding cpu property.
1346b3d52804SRichard Henderson      */
1347b3d52804SRichard Henderson     cpu->sve_default_vq = 4;
1348e74c0976SRichard Henderson     cpu->sme_default_vq = 2;
1349b3d52804SRichard Henderson # endif
1350b3d52804SRichard Henderson #else
1351fcf5ef2aSThomas Huth     /* Our inbound IRQ and FIQ lines */
1352fcf5ef2aSThomas Huth     if (kvm_enabled()) {
1353fcf5ef2aSThomas Huth         /* VIRQ and VFIQ are unused with KVM but we add them to maintain
1354fcf5ef2aSThomas Huth          * the same interface as non-KVM CPUs.
1355fcf5ef2aSThomas Huth          */
1356fcf5ef2aSThomas Huth         qdev_init_gpio_in(DEVICE(cpu), arm_cpu_kvm_set_irq, 4);
1357fcf5ef2aSThomas Huth     } else {
1358fcf5ef2aSThomas Huth         qdev_init_gpio_in(DEVICE(cpu), arm_cpu_set_irq, 4);
1359fcf5ef2aSThomas Huth     }
1360fcf5ef2aSThomas Huth 
1361fcf5ef2aSThomas Huth     qdev_init_gpio_out(DEVICE(cpu), cpu->gt_timer_outputs,
1362fcf5ef2aSThomas Huth                        ARRAY_SIZE(cpu->gt_timer_outputs));
1363aa1b3111SPeter Maydell 
1364aa1b3111SPeter Maydell     qdev_init_gpio_out_named(DEVICE(cpu), &cpu->gicv3_maintenance_interrupt,
1365aa1b3111SPeter Maydell                              "gicv3-maintenance-interrupt", 1);
136607f48730SAndrew Jones     qdev_init_gpio_out_named(DEVICE(cpu), &cpu->pmu_interrupt,
136707f48730SAndrew Jones                              "pmu-interrupt", 1);
1368fcf5ef2aSThomas Huth #endif
1369fcf5ef2aSThomas Huth 
1370fcf5ef2aSThomas Huth     /* DTB consumers generally don't in fact care what the 'compatible'
1371fcf5ef2aSThomas Huth      * string is, so always provide some string and trust that a hypothetical
1372fcf5ef2aSThomas Huth      * picky DTB consumer will also provide a helpful error message.
1373fcf5ef2aSThomas Huth      */
1374fcf5ef2aSThomas Huth     cpu->dtb_compatible = "qemu,unknown";
13750dc71c70SAkihiko Odaki     cpu->psci_version = QEMU_PSCI_VERSION_0_1; /* By default assume PSCI v0.1 */
1376fcf5ef2aSThomas Huth     cpu->kvm_target = QEMU_KVM_ARM_TARGET_NONE;
1377fcf5ef2aSThomas Huth 
13782c9c0bf9SAlexander Graf     if (tcg_enabled() || hvf_enabled()) {
13790dc71c70SAkihiko Odaki         /* TCG and HVF implement PSCI 1.1 */
13800dc71c70SAkihiko Odaki         cpu->psci_version = QEMU_PSCI_VERSION_1_1;
1381fcf5ef2aSThomas Huth     }
1382fcf5ef2aSThomas Huth }
1383fcf5ef2aSThomas Huth 
138496eec6b2SAndrew Jeffery static Property arm_cpu_gt_cntfrq_property =
138596eec6b2SAndrew Jeffery             DEFINE_PROP_UINT64("cntfrq", ARMCPU, gt_cntfrq_hz,
138696eec6b2SAndrew Jeffery                                NANOSECONDS_PER_SECOND / GTIMER_SCALE);
138796eec6b2SAndrew Jeffery 
1388fcf5ef2aSThomas Huth static Property arm_cpu_reset_cbar_property =
1389fcf5ef2aSThomas Huth             DEFINE_PROP_UINT64("reset-cbar", ARMCPU, reset_cbar, 0);
1390fcf5ef2aSThomas Huth 
1391fcf5ef2aSThomas Huth static Property arm_cpu_reset_hivecs_property =
1392fcf5ef2aSThomas Huth             DEFINE_PROP_BOOL("reset-hivecs", ARMCPU, reset_hivecs, false);
1393fcf5ef2aSThomas Huth 
139445ca3a14SRichard Henderson #ifndef CONFIG_USER_ONLY
1395c25bd18aSPeter Maydell static Property arm_cpu_has_el2_property =
1396c25bd18aSPeter Maydell             DEFINE_PROP_BOOL("has_el2", ARMCPU, has_el2, true);
1397c25bd18aSPeter Maydell 
1398fcf5ef2aSThomas Huth static Property arm_cpu_has_el3_property =
1399fcf5ef2aSThomas Huth             DEFINE_PROP_BOOL("has_el3", ARMCPU, has_el3, true);
140045ca3a14SRichard Henderson #endif
1401fcf5ef2aSThomas Huth 
14023a062d57SJulian Brown static Property arm_cpu_cfgend_property =
14033a062d57SJulian Brown             DEFINE_PROP_BOOL("cfgend", ARMCPU, cfgend, false);
14043a062d57SJulian Brown 
140597a28b0eSPeter Maydell static Property arm_cpu_has_vfp_property =
140697a28b0eSPeter Maydell             DEFINE_PROP_BOOL("vfp", ARMCPU, has_vfp, true);
140797a28b0eSPeter Maydell 
140842bea956SCédric Le Goater static Property arm_cpu_has_vfp_d32_property =
140942bea956SCédric Le Goater             DEFINE_PROP_BOOL("vfp-d32", ARMCPU, has_vfp_d32, true);
141042bea956SCédric Le Goater 
141197a28b0eSPeter Maydell static Property arm_cpu_has_neon_property =
141297a28b0eSPeter Maydell             DEFINE_PROP_BOOL("neon", ARMCPU, has_neon, true);
141397a28b0eSPeter Maydell 
1414ea90db0aSPeter Maydell static Property arm_cpu_has_dsp_property =
1415ea90db0aSPeter Maydell             DEFINE_PROP_BOOL("dsp", ARMCPU, has_dsp, true);
1416ea90db0aSPeter Maydell 
1417fcf5ef2aSThomas Huth static Property arm_cpu_has_mpu_property =
1418fcf5ef2aSThomas Huth             DEFINE_PROP_BOOL("has-mpu", ARMCPU, has_mpu, true);
1419fcf5ef2aSThomas Huth 
14208d92e26bSPeter Maydell /* This is like DEFINE_PROP_UINT32 but it doesn't set the default value,
14218d92e26bSPeter Maydell  * because the CPU initfn will have already set cpu->pmsav7_dregion to
14228d92e26bSPeter Maydell  * the right value for that particular CPU type, and we don't want
14238d92e26bSPeter Maydell  * to override that with an incorrect constant value.
14248d92e26bSPeter Maydell  */
1425fcf5ef2aSThomas Huth static Property arm_cpu_pmsav7_dregion_property =
14268d92e26bSPeter Maydell             DEFINE_PROP_UNSIGNED_NODEFAULT("pmsav7-dregion", ARMCPU,
14278d92e26bSPeter Maydell                                            pmsav7_dregion,
14288d92e26bSPeter Maydell                                            qdev_prop_uint32, uint32_t);
1429fcf5ef2aSThomas Huth 
1430ae502508SAndrew Jones static bool arm_get_pmu(Object *obj, Error **errp)
1431ae502508SAndrew Jones {
1432ae502508SAndrew Jones     ARMCPU *cpu = ARM_CPU(obj);
1433ae502508SAndrew Jones 
1434ae502508SAndrew Jones     return cpu->has_pmu;
1435ae502508SAndrew Jones }
1436ae502508SAndrew Jones 
1437ae502508SAndrew Jones static void arm_set_pmu(Object *obj, bool value, Error **errp)
1438ae502508SAndrew Jones {
1439ae502508SAndrew Jones     ARMCPU *cpu = ARM_CPU(obj);
1440ae502508SAndrew Jones 
1441ae502508SAndrew Jones     if (value) {
14427d20e681SPhilippe Mathieu-Daudé         if (kvm_enabled() && !kvm_arm_pmu_supported()) {
1443ae502508SAndrew Jones             error_setg(errp, "'pmu' feature not supported by KVM on this host");
1444ae502508SAndrew Jones             return;
1445ae502508SAndrew Jones         }
1446ae502508SAndrew Jones         set_feature(&cpu->env, ARM_FEATURE_PMU);
1447ae502508SAndrew Jones     } else {
1448ae502508SAndrew Jones         unset_feature(&cpu->env, ARM_FEATURE_PMU);
1449ae502508SAndrew Jones     }
1450ae502508SAndrew Jones     cpu->has_pmu = value;
1451ae502508SAndrew Jones }
1452ae502508SAndrew Jones 
14537def8754SAndrew Jeffery unsigned int gt_cntfrq_period_ns(ARMCPU *cpu)
14547def8754SAndrew Jeffery {
145596eec6b2SAndrew Jeffery     /*
145696eec6b2SAndrew Jeffery      * The exact approach to calculating guest ticks is:
145796eec6b2SAndrew Jeffery      *
145896eec6b2SAndrew Jeffery      *     muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), cpu->gt_cntfrq_hz,
145996eec6b2SAndrew Jeffery      *              NANOSECONDS_PER_SECOND);
146096eec6b2SAndrew Jeffery      *
146196eec6b2SAndrew Jeffery      * We don't do that. Rather we intentionally use integer division
146296eec6b2SAndrew Jeffery      * truncation below and in the caller for the conversion of host monotonic
146396eec6b2SAndrew Jeffery      * time to guest ticks to provide the exact inverse for the semantics of
146496eec6b2SAndrew Jeffery      * the QEMUTimer scale factor. QEMUTimer's scale facter is an integer, so
146596eec6b2SAndrew Jeffery      * it loses precision when representing frequencies where
146696eec6b2SAndrew Jeffery      * `(NANOSECONDS_PER_SECOND % cpu->gt_cntfrq) > 0` holds. Failing to
146796eec6b2SAndrew Jeffery      * provide an exact inverse leads to scheduling timers with negative
146896eec6b2SAndrew Jeffery      * periods, which in turn leads to sticky behaviour in the guest.
146996eec6b2SAndrew Jeffery      *
147096eec6b2SAndrew Jeffery      * Finally, CNTFRQ is effectively capped at 1GHz to ensure our scale factor
147196eec6b2SAndrew Jeffery      * cannot become zero.
147296eec6b2SAndrew Jeffery      */
14737def8754SAndrew Jeffery     return NANOSECONDS_PER_SECOND > cpu->gt_cntfrq_hz ?
14747def8754SAndrew Jeffery       NANOSECONDS_PER_SECOND / cpu->gt_cntfrq_hz : 1;
14757def8754SAndrew Jeffery }
14767def8754SAndrew Jeffery 
1477b8f7959fSPeter Maydell static void arm_cpu_propagate_feature_implications(ARMCPU *cpu)
1478b8f7959fSPeter Maydell {
1479b8f7959fSPeter Maydell     CPUARMState *env = &cpu->env;
1480b8f7959fSPeter Maydell     bool no_aa32 = false;
1481b8f7959fSPeter Maydell 
1482b8f7959fSPeter Maydell     /*
1483b8f7959fSPeter Maydell      * Some features automatically imply others: set the feature
1484b8f7959fSPeter Maydell      * bits explicitly for these cases.
1485b8f7959fSPeter Maydell      */
1486b8f7959fSPeter Maydell 
1487b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_M)) {
1488b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_PMSA);
1489b8f7959fSPeter Maydell     }
1490b8f7959fSPeter Maydell 
1491b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V8)) {
1492b8f7959fSPeter Maydell         if (arm_feature(env, ARM_FEATURE_M)) {
1493b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_V7);
1494b8f7959fSPeter Maydell         } else {
1495b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_V7VE);
1496b8f7959fSPeter Maydell         }
1497b8f7959fSPeter Maydell     }
1498b8f7959fSPeter Maydell 
1499b8f7959fSPeter Maydell     /*
1500b8f7959fSPeter Maydell      * There exist AArch64 cpus without AArch32 support.  When KVM
1501b8f7959fSPeter Maydell      * queries ID_ISAR0_EL1 on such a host, the value is UNKNOWN.
1502b8f7959fSPeter Maydell      * Similarly, we cannot check ID_AA64PFR0 without AArch64 support.
1503b8f7959fSPeter Maydell      * As a general principle, we also do not make ID register
1504b8f7959fSPeter Maydell      * consistency checks anywhere unless using TCG, because only
1505b8f7959fSPeter Maydell      * for TCG would a consistency-check failure be a QEMU bug.
1506b8f7959fSPeter Maydell      */
1507b8f7959fSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
1508b8f7959fSPeter Maydell         no_aa32 = !cpu_isar_feature(aa64_aa32, cpu);
1509b8f7959fSPeter Maydell     }
1510b8f7959fSPeter Maydell 
1511b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V7VE)) {
1512b8f7959fSPeter Maydell         /*
1513b8f7959fSPeter Maydell          * v7 Virtualization Extensions. In real hardware this implies
1514b8f7959fSPeter Maydell          * EL2 and also the presence of the Security Extensions.
1515b8f7959fSPeter Maydell          * For QEMU, for backwards-compatibility we implement some
1516b8f7959fSPeter Maydell          * CPUs or CPU configs which have no actual EL2 or EL3 but do
1517b8f7959fSPeter Maydell          * include the various other features that V7VE implies.
1518b8f7959fSPeter Maydell          * Presence of EL2 itself is ARM_FEATURE_EL2, and of the
1519b8f7959fSPeter Maydell          * Security Extensions is ARM_FEATURE_EL3.
1520b8f7959fSPeter Maydell          */
1521b8f7959fSPeter Maydell         assert(!tcg_enabled() || no_aa32 ||
1522b8f7959fSPeter Maydell                cpu_isar_feature(aa32_arm_div, cpu));
1523b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_LPAE);
1524b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V7);
1525b8f7959fSPeter Maydell     }
1526b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V7)) {
1527b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_VAPA);
1528b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_THUMB2);
1529b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_MPIDR);
1530b8f7959fSPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
1531b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_V6K);
1532b8f7959fSPeter Maydell         } else {
1533b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_V6);
1534b8f7959fSPeter Maydell         }
1535b8f7959fSPeter Maydell 
1536b8f7959fSPeter Maydell         /*
1537b8f7959fSPeter Maydell          * Always define VBAR for V7 CPUs even if it doesn't exist in
1538b8f7959fSPeter Maydell          * non-EL3 configs. This is needed by some legacy boards.
1539b8f7959fSPeter Maydell          */
1540b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_VBAR);
1541b8f7959fSPeter Maydell     }
1542b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V6K)) {
1543b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V6);
1544b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_MVFR);
1545b8f7959fSPeter Maydell     }
1546b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V6)) {
1547b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V5);
1548b8f7959fSPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
1549b8f7959fSPeter Maydell             assert(!tcg_enabled() || no_aa32 ||
1550b8f7959fSPeter Maydell                    cpu_isar_feature(aa32_jazelle, cpu));
1551b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_AUXCR);
1552b8f7959fSPeter Maydell         }
1553b8f7959fSPeter Maydell     }
1554b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V5)) {
1555b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V4T);
1556b8f7959fSPeter Maydell     }
1557b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_LPAE)) {
1558b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V7MP);
1559b8f7959fSPeter Maydell     }
1560b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
1561b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_CBAR);
1562b8f7959fSPeter Maydell     }
1563b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_THUMB2) &&
1564b8f7959fSPeter Maydell         !arm_feature(env, ARM_FEATURE_M)) {
1565b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_THUMB_DSP);
1566b8f7959fSPeter Maydell     }
1567b8f7959fSPeter Maydell }
1568b8f7959fSPeter Maydell 
156951e5ef45SMarc-André Lureau void arm_cpu_post_init(Object *obj)
1570fcf5ef2aSThomas Huth {
1571fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(obj);
1572fcf5ef2aSThomas Huth 
1573b8f7959fSPeter Maydell     /*
1574b8f7959fSPeter Maydell      * Some features imply others. Figure this out now, because we
1575b8f7959fSPeter Maydell      * are going to look at the feature bits in deciding which
1576b8f7959fSPeter Maydell      * properties to add.
1577790a1150SPeter Maydell      */
1578b8f7959fSPeter Maydell     arm_cpu_propagate_feature_implications(cpu);
1579790a1150SPeter Maydell 
1580fcf5ef2aSThomas Huth     if (arm_feature(&cpu->env, ARM_FEATURE_CBAR) ||
1581fcf5ef2aSThomas Huth         arm_feature(&cpu->env, ARM_FEATURE_CBAR_RO)) {
158294d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_cbar_property);
1583fcf5ef2aSThomas Huth     }
1584fcf5ef2aSThomas Huth 
1585fcf5ef2aSThomas Huth     if (!arm_feature(&cpu->env, ARM_FEATURE_M)) {
158694d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_hivecs_property);
1587fcf5ef2aSThomas Huth     }
1588fcf5ef2aSThomas Huth 
1589910e4f24STobias Röhmel     if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
15904a7319b7SEdgar E. Iglesias         object_property_add_uint64_ptr(obj, "rvbar",
15914a7319b7SEdgar E. Iglesias                                        &cpu->rvbar_prop,
15924a7319b7SEdgar E. Iglesias                                        OBJ_PROP_FLAG_READWRITE);
1593fcf5ef2aSThomas Huth     }
1594fcf5ef2aSThomas Huth 
159545ca3a14SRichard Henderson #ifndef CONFIG_USER_ONLY
1596fcf5ef2aSThomas Huth     if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
1597fcf5ef2aSThomas Huth         /* Add the has_el3 state CPU property only if EL3 is allowed.  This will
1598fcf5ef2aSThomas Huth          * prevent "has_el3" from existing on CPUs which cannot support EL3.
1599fcf5ef2aSThomas Huth          */
160094d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el3_property);
1601fcf5ef2aSThomas Huth 
1602fcf5ef2aSThomas Huth         object_property_add_link(obj, "secure-memory",
1603fcf5ef2aSThomas Huth                                  TYPE_MEMORY_REGION,
1604fcf5ef2aSThomas Huth                                  (Object **)&cpu->secure_memory,
1605fcf5ef2aSThomas Huth                                  qdev_prop_allow_set_link_before_realize,
1606d2623129SMarkus Armbruster                                  OBJ_PROP_LINK_STRONG);
1607fcf5ef2aSThomas Huth     }
1608fcf5ef2aSThomas Huth 
1609c25bd18aSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_EL2)) {
161094d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el2_property);
1611c25bd18aSPeter Maydell     }
161245ca3a14SRichard Henderson #endif
1613c25bd18aSPeter Maydell 
1614fcf5ef2aSThomas Huth     if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) {
1615ae502508SAndrew Jones         cpu->has_pmu = true;
1616d2623129SMarkus Armbruster         object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu);
1617fcf5ef2aSThomas Huth     }
1618fcf5ef2aSThomas Huth 
161997a28b0eSPeter Maydell     /*
162097a28b0eSPeter Maydell      * Allow user to turn off VFP and Neon support, but only for TCG --
162197a28b0eSPeter Maydell      * KVM does not currently allow us to lie to the guest about its
162297a28b0eSPeter Maydell      * ID/feature registers, so the guest always sees what the host has.
162397a28b0eSPeter Maydell      */
16244315f7c6SRichard Henderson     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
16254315f7c6SRichard Henderson         if (cpu_isar_feature(aa64_fp_simd, cpu)) {
162697a28b0eSPeter Maydell             cpu->has_vfp = true;
162742bea956SCédric Le Goater             cpu->has_vfp_d32 = true;
16284315f7c6SRichard Henderson             if (tcg_enabled() || qtest_enabled()) {
16294315f7c6SRichard Henderson                 qdev_property_add_static(DEVICE(obj),
16304315f7c6SRichard Henderson                                          &arm_cpu_has_vfp_property);
16314315f7c6SRichard Henderson             }
16324315f7c6SRichard Henderson         }
16334315f7c6SRichard Henderson     } else if (cpu_isar_feature(aa32_vfp, cpu)) {
16344315f7c6SRichard Henderson         cpu->has_vfp = true;
1635185e3fdfSPeter Maydell         if (tcg_enabled() || qtest_enabled()) {
1636185e3fdfSPeter Maydell             qdev_property_add_static(DEVICE(obj),
1637185e3fdfSPeter Maydell                                      &arm_cpu_has_vfp_property);
1638185e3fdfSPeter Maydell         }
16394315f7c6SRichard Henderson         if (cpu_isar_feature(aa32_simd_r32, cpu)) {
16404315f7c6SRichard Henderson             cpu->has_vfp_d32 = true;
164142bea956SCédric Le Goater             /*
164242bea956SCédric Le Goater              * The permitted values of the SIMDReg bits [3:0] on
164342bea956SCédric Le Goater              * Armv8-A are either 0b0000 and 0b0010. On such CPUs,
164442bea956SCédric Le Goater              * make sure that has_vfp_d32 can not be set to false.
164542bea956SCédric Le Goater              */
16464315f7c6SRichard Henderson             if ((tcg_enabled() || qtest_enabled())
16474315f7c6SRichard Henderson                 && !(arm_feature(&cpu->env, ARM_FEATURE_V8)
16484315f7c6SRichard Henderson                      && !arm_feature(&cpu->env, ARM_FEATURE_M))) {
164942bea956SCédric Le Goater                 qdev_property_add_static(DEVICE(obj),
165042bea956SCédric Le Goater                                          &arm_cpu_has_vfp_d32_property);
165142bea956SCédric Le Goater             }
165242bea956SCédric Le Goater         }
165342bea956SCédric Le Goater     }
165442bea956SCédric Le Goater 
165597a28b0eSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_NEON)) {
165697a28b0eSPeter Maydell         cpu->has_neon = true;
165797a28b0eSPeter Maydell         if (!kvm_enabled()) {
165894d912d1SMarc-André Lureau             qdev_property_add_static(DEVICE(obj), &arm_cpu_has_neon_property);
165997a28b0eSPeter Maydell         }
166097a28b0eSPeter Maydell     }
166197a28b0eSPeter Maydell 
1662ea90db0aSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_M) &&
1663ea90db0aSPeter Maydell         arm_feature(&cpu->env, ARM_FEATURE_THUMB_DSP)) {
166494d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_dsp_property);
1665ea90db0aSPeter Maydell     }
1666ea90db0aSPeter Maydell 
1667452a0955SPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_PMSA)) {
166894d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_mpu_property);
1669fcf5ef2aSThomas Huth         if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
1670fcf5ef2aSThomas Huth             qdev_property_add_static(DEVICE(obj),
167194d912d1SMarc-André Lureau                                      &arm_cpu_pmsav7_dregion_property);
1672fcf5ef2aSThomas Huth         }
1673fcf5ef2aSThomas Huth     }
1674fcf5ef2aSThomas Huth 
1675181962fdSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_M_SECURITY)) {
1676181962fdSPeter Maydell         object_property_add_link(obj, "idau", TYPE_IDAU_INTERFACE, &cpu->idau,
1677181962fdSPeter Maydell                                  qdev_prop_allow_set_link_before_realize,
1678d2623129SMarkus Armbruster                                  OBJ_PROP_LINK_STRONG);
1679f9f62e4cSPeter Maydell         /*
1680f9f62e4cSPeter Maydell          * M profile: initial value of the Secure VTOR. We can't just use
1681f9f62e4cSPeter Maydell          * a simple DEFINE_PROP_UINT32 for this because we want to permit
1682f9f62e4cSPeter Maydell          * the property to be set after realize.
1683f9f62e4cSPeter Maydell          */
168464a7b8deSFelipe Franciosi         object_property_add_uint32_ptr(obj, "init-svtor",
168564a7b8deSFelipe Franciosi                                        &cpu->init_svtor,
1686d2623129SMarkus Armbruster                                        OBJ_PROP_FLAG_READWRITE);
1687181962fdSPeter Maydell     }
16887cda2149SPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_M)) {
16897cda2149SPeter Maydell         /*
16907cda2149SPeter Maydell          * Initial value of the NS VTOR (for cores without the Security
16917cda2149SPeter Maydell          * extension, this is the only VTOR)
16927cda2149SPeter Maydell          */
16937cda2149SPeter Maydell         object_property_add_uint32_ptr(obj, "init-nsvtor",
16947cda2149SPeter Maydell                                        &cpu->init_nsvtor,
16957cda2149SPeter Maydell                                        OBJ_PROP_FLAG_READWRITE);
16967cda2149SPeter Maydell     }
1697181962fdSPeter Maydell 
1698bddd892eSPeter Maydell     /* Not DEFINE_PROP_UINT32: we want this to be settable after realize */
1699bddd892eSPeter Maydell     object_property_add_uint32_ptr(obj, "psci-conduit",
1700bddd892eSPeter Maydell                                    &cpu->psci_conduit,
1701bddd892eSPeter Maydell                                    OBJ_PROP_FLAG_READWRITE);
1702bddd892eSPeter Maydell 
170394d912d1SMarc-André Lureau     qdev_property_add_static(DEVICE(obj), &arm_cpu_cfgend_property);
170496eec6b2SAndrew Jeffery 
170596eec6b2SAndrew Jeffery     if (arm_feature(&cpu->env, ARM_FEATURE_GENERIC_TIMER)) {
170694d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(cpu), &arm_cpu_gt_cntfrq_property);
170796eec6b2SAndrew Jeffery     }
17089e6f8d8aSfangying 
17099e6f8d8aSfangying     if (kvm_enabled()) {
1710cac675b5SPhilippe Mathieu-Daudé         kvm_arm_add_vcpu_properties(cpu);
17119e6f8d8aSfangying     }
17128bce44a2SRichard Henderson 
17138bce44a2SRichard Henderson #ifndef CONFIG_USER_ONLY
17148bce44a2SRichard Henderson     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) &&
17158bce44a2SRichard Henderson         cpu_isar_feature(aa64_mte, cpu)) {
17168bce44a2SRichard Henderson         object_property_add_link(obj, "tag-memory",
17178bce44a2SRichard Henderson                                  TYPE_MEMORY_REGION,
17188bce44a2SRichard Henderson                                  (Object **)&cpu->tag_memory,
17198bce44a2SRichard Henderson                                  qdev_prop_allow_set_link_before_realize,
17208bce44a2SRichard Henderson                                  OBJ_PROP_LINK_STRONG);
17218bce44a2SRichard Henderson 
17228bce44a2SRichard Henderson         if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
17238bce44a2SRichard Henderson             object_property_add_link(obj, "secure-tag-memory",
17248bce44a2SRichard Henderson                                      TYPE_MEMORY_REGION,
17258bce44a2SRichard Henderson                                      (Object **)&cpu->secure_tag_memory,
17268bce44a2SRichard Henderson                                      qdev_prop_allow_set_link_before_realize,
17278bce44a2SRichard Henderson                                      OBJ_PROP_LINK_STRONG);
17288bce44a2SRichard Henderson         }
17298bce44a2SRichard Henderson     }
17308bce44a2SRichard Henderson #endif
1731fcf5ef2aSThomas Huth }
1732fcf5ef2aSThomas Huth 
1733fcf5ef2aSThomas Huth static void arm_cpu_finalizefn(Object *obj)
1734fcf5ef2aSThomas Huth {
1735fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(obj);
173608267487SAaron Lindsay     ARMELChangeHook *hook, *next;
173708267487SAaron Lindsay 
1738fcf5ef2aSThomas Huth     g_hash_table_destroy(cpu->cp_regs);
173908267487SAaron Lindsay 
1740b5c53d1bSAaron Lindsay     QLIST_FOREACH_SAFE(hook, &cpu->pre_el_change_hooks, node, next) {
1741b5c53d1bSAaron Lindsay         QLIST_REMOVE(hook, node);
1742b5c53d1bSAaron Lindsay         g_free(hook);
1743b5c53d1bSAaron Lindsay     }
174408267487SAaron Lindsay     QLIST_FOREACH_SAFE(hook, &cpu->el_change_hooks, node, next) {
174508267487SAaron Lindsay         QLIST_REMOVE(hook, node);
174608267487SAaron Lindsay         g_free(hook);
174708267487SAaron Lindsay     }
17484e7beb0cSAaron Lindsay OS #ifndef CONFIG_USER_ONLY
17494e7beb0cSAaron Lindsay OS     if (cpu->pmu_timer) {
17504e7beb0cSAaron Lindsay OS         timer_free(cpu->pmu_timer);
17514e7beb0cSAaron Lindsay OS     }
17524e7beb0cSAaron Lindsay OS #endif
1753fcf5ef2aSThomas Huth }
1754fcf5ef2aSThomas Huth 
17550df9142dSAndrew Jones void arm_cpu_finalize_features(ARMCPU *cpu, Error **errp)
17560df9142dSAndrew Jones {
17570df9142dSAndrew Jones     Error *local_err = NULL;
17580df9142dSAndrew Jones 
175907301161SRichard Henderson #ifdef TARGET_AARCH64
17600df9142dSAndrew Jones     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
17610df9142dSAndrew Jones         arm_cpu_sve_finalize(cpu, &local_err);
17620df9142dSAndrew Jones         if (local_err != NULL) {
17630df9142dSAndrew Jones             error_propagate(errp, local_err);
17640df9142dSAndrew Jones             return;
17650df9142dSAndrew Jones         }
1766eb94284dSRichard Henderson 
1767f7767ca3SPeter Maydell         /*
1768f7767ca3SPeter Maydell          * FEAT_SME is not architecturally dependent on FEAT_SVE (unless
1769f7767ca3SPeter Maydell          * FEAT_SME_FA64 is present). However our implementation currently
1770f7767ca3SPeter Maydell          * assumes it, so if the user asked for sve=off then turn off SME also.
1771f7767ca3SPeter Maydell          * (KVM doesn't currently support SME at all.)
1772f7767ca3SPeter Maydell          */
1773f7767ca3SPeter Maydell         if (cpu_isar_feature(aa64_sme, cpu) && !cpu_isar_feature(aa64_sve, cpu)) {
1774f7767ca3SPeter Maydell             object_property_set_bool(OBJECT(cpu), "sme", false, &error_abort);
1775f7767ca3SPeter Maydell         }
1776f7767ca3SPeter Maydell 
1777e74c0976SRichard Henderson         arm_cpu_sme_finalize(cpu, &local_err);
1778e74c0976SRichard Henderson         if (local_err != NULL) {
1779e74c0976SRichard Henderson             error_propagate(errp, local_err);
1780e74c0976SRichard Henderson             return;
1781e74c0976SRichard Henderson         }
1782e74c0976SRichard Henderson 
1783eb94284dSRichard Henderson         arm_cpu_pauth_finalize(cpu, &local_err);
1784eb94284dSRichard Henderson         if (local_err != NULL) {
1785eb94284dSRichard Henderson             error_propagate(errp, local_err);
1786eb94284dSRichard Henderson             return;
1787eb94284dSRichard Henderson         }
178869b2265dSRichard Henderson 
178969b2265dSRichard Henderson         arm_cpu_lpa2_finalize(cpu, &local_err);
179069b2265dSRichard Henderson         if (local_err != NULL) {
179169b2265dSRichard Henderson             error_propagate(errp, local_err);
179269b2265dSRichard Henderson             return;
179369b2265dSRichard Henderson         }
1794eb94284dSRichard Henderson     }
179507301161SRichard Henderson #endif
179668970d1eSAndrew Jones 
179768970d1eSAndrew Jones     if (kvm_enabled()) {
179868970d1eSAndrew Jones         kvm_arm_steal_time_finalize(cpu, &local_err);
179968970d1eSAndrew Jones         if (local_err != NULL) {
180068970d1eSAndrew Jones             error_propagate(errp, local_err);
180168970d1eSAndrew Jones             return;
180268970d1eSAndrew Jones         }
180368970d1eSAndrew Jones     }
18040df9142dSAndrew Jones }
18050df9142dSAndrew Jones 
1806fcf5ef2aSThomas Huth static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
1807fcf5ef2aSThomas Huth {
1808fcf5ef2aSThomas Huth     CPUState *cs = CPU(dev);
1809fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(dev);
1810fcf5ef2aSThomas Huth     ARMCPUClass *acc = ARM_CPU_GET_CLASS(dev);
1811fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
1812fcf5ef2aSThomas Huth     Error *local_err = NULL;
1813fcf5ef2aSThomas Huth 
1814b94b8c60SPhilippe Mathieu-Daudé #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
1815e607ea39SAnton Johansson     /* Use pc-relative instructions in system-mode */
1816e607ea39SAnton Johansson     cs->tcg_cflags |= CF_PCREL;
1817e607ea39SAnton Johansson #endif
1818e607ea39SAnton Johansson 
1819c4487d76SPeter Maydell     /* If we needed to query the host kernel for the CPU features
1820c4487d76SPeter Maydell      * then it's possible that might have failed in the initfn, but
1821c4487d76SPeter Maydell      * this is the first point where we can report it.
1822c4487d76SPeter Maydell      */
1823c4487d76SPeter Maydell     if (cpu->host_cpu_probe_failed) {
1824585df85eSPeter Maydell         if (!kvm_enabled() && !hvf_enabled()) {
1825585df85eSPeter Maydell             error_setg(errp, "The 'host' CPU type can only be used with KVM or HVF");
1826c4487d76SPeter Maydell         } else {
1827c4487d76SPeter Maydell             error_setg(errp, "Failed to retrieve host CPU features");
1828c4487d76SPeter Maydell         }
1829c4487d76SPeter Maydell         return;
1830c4487d76SPeter Maydell     }
1831c4487d76SPeter Maydell 
183295f87565SPeter Maydell #ifndef CONFIG_USER_ONLY
183395f87565SPeter Maydell     /* The NVIC and M-profile CPU are two halves of a single piece of
183495f87565SPeter Maydell      * hardware; trying to use one without the other is a command line
183595f87565SPeter Maydell      * error and will result in segfaults if not caught here.
183695f87565SPeter Maydell      */
183795f87565SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M)) {
183895f87565SPeter Maydell         if (!env->nvic) {
183995f87565SPeter Maydell             error_setg(errp, "This board cannot be used with Cortex-M CPUs");
184095f87565SPeter Maydell             return;
184195f87565SPeter Maydell         }
184295f87565SPeter Maydell     } else {
184395f87565SPeter Maydell         if (env->nvic) {
184495f87565SPeter Maydell             error_setg(errp, "This board can only be used with Cortex-M CPUs");
184595f87565SPeter Maydell             return;
184695f87565SPeter Maydell         }
184795f87565SPeter Maydell     }
1848397cd31fSPeter Maydell 
1849045e5064SAlexander Graf     if (!tcg_enabled() && !qtest_enabled()) {
185049e7f191SPeter Maydell         /*
1851045e5064SAlexander Graf          * We assume that no accelerator except TCG (and the "not really an
1852045e5064SAlexander Graf          * accelerator" qtest) can handle these features, because Arm hardware
1853045e5064SAlexander Graf          * virtualization can't virtualize them.
1854045e5064SAlexander Graf          *
185549e7f191SPeter Maydell          * Catch all the cases which might cause us to create more than one
185649e7f191SPeter Maydell          * address space for the CPU (otherwise we will assert() later in
185749e7f191SPeter Maydell          * cpu_address_space_init()).
185849e7f191SPeter Maydell          */
185949e7f191SPeter Maydell         if (arm_feature(env, ARM_FEATURE_M)) {
186049e7f191SPeter Maydell             error_setg(errp,
1861045e5064SAlexander Graf                        "Cannot enable %s when using an M-profile guest CPU",
1862045e5064SAlexander Graf                        current_accel_name());
186349e7f191SPeter Maydell             return;
186449e7f191SPeter Maydell         }
186549e7f191SPeter Maydell         if (cpu->has_el3) {
186649e7f191SPeter Maydell             error_setg(errp,
1867045e5064SAlexander Graf                        "Cannot enable %s when guest CPU has EL3 enabled",
1868045e5064SAlexander Graf                        current_accel_name());
186949e7f191SPeter Maydell             return;
187049e7f191SPeter Maydell         }
187149e7f191SPeter Maydell         if (cpu->tag_memory) {
187249e7f191SPeter Maydell             error_setg(errp,
1873d009607dSPeter Maydell                        "Cannot enable %s when guest CPUs has MTE enabled",
1874045e5064SAlexander Graf                        current_accel_name());
187549e7f191SPeter Maydell             return;
187649e7f191SPeter Maydell         }
187749e7f191SPeter Maydell     }
187849e7f191SPeter Maydell 
187996eec6b2SAndrew Jeffery     {
188096eec6b2SAndrew Jeffery         uint64_t scale;
188196eec6b2SAndrew Jeffery 
188296eec6b2SAndrew Jeffery         if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
188396eec6b2SAndrew Jeffery             if (!cpu->gt_cntfrq_hz) {
188496eec6b2SAndrew Jeffery                 error_setg(errp, "Invalid CNTFRQ: %"PRId64"Hz",
188596eec6b2SAndrew Jeffery                            cpu->gt_cntfrq_hz);
188696eec6b2SAndrew Jeffery                 return;
188796eec6b2SAndrew Jeffery             }
188896eec6b2SAndrew Jeffery             scale = gt_cntfrq_period_ns(cpu);
188996eec6b2SAndrew Jeffery         } else {
189096eec6b2SAndrew Jeffery             scale = GTIMER_SCALE;
189196eec6b2SAndrew Jeffery         }
189296eec6b2SAndrew Jeffery 
189396eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_PHYS] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
1894397cd31fSPeter Maydell                                                arm_gt_ptimer_cb, cpu);
189596eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_VIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
1896397cd31fSPeter Maydell                                                arm_gt_vtimer_cb, cpu);
189796eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_HYP] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
1898397cd31fSPeter Maydell                                               arm_gt_htimer_cb, cpu);
189996eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_SEC] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
1900397cd31fSPeter Maydell                                               arm_gt_stimer_cb, cpu);
19018c94b071SRichard Henderson         cpu->gt_timer[GTIMER_HYPVIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
19028c94b071SRichard Henderson                                                   arm_gt_hvtimer_cb, cpu);
190396eec6b2SAndrew Jeffery     }
190495f87565SPeter Maydell #endif
190595f87565SPeter Maydell 
1906fcf5ef2aSThomas Huth     cpu_exec_realizefn(cs, &local_err);
1907fcf5ef2aSThomas Huth     if (local_err != NULL) {
1908fcf5ef2aSThomas Huth         error_propagate(errp, local_err);
1909fcf5ef2aSThomas Huth         return;
1910fcf5ef2aSThomas Huth     }
1911fcf5ef2aSThomas Huth 
19120df9142dSAndrew Jones     arm_cpu_finalize_features(cpu, &local_err);
19130df9142dSAndrew Jones     if (local_err != NULL) {
19140df9142dSAndrew Jones         error_propagate(errp, local_err);
19150df9142dSAndrew Jones         return;
19160df9142dSAndrew Jones     }
19170df9142dSAndrew Jones 
19189719f125SJohn Högberg #ifdef CONFIG_USER_ONLY
19199719f125SJohn Högberg     /*
19209719f125SJohn Högberg      * User mode relies on IC IVAU instructions to catch modification of
19219719f125SJohn Högberg      * dual-mapped code.
19229719f125SJohn Högberg      *
19239719f125SJohn Högberg      * Clear CTR_EL0.DIC to ensure that software that honors these flags uses
19249719f125SJohn Högberg      * IC IVAU even if the emulated processor does not normally require it.
19259719f125SJohn Högberg      */
19269719f125SJohn Högberg     cpu->ctr = FIELD_DP64(cpu->ctr, CTR_EL0, DIC, 0);
19279719f125SJohn Högberg #endif
19289719f125SJohn Högberg 
192997a28b0eSPeter Maydell     if (arm_feature(env, ARM_FEATURE_AARCH64) &&
193097a28b0eSPeter Maydell         cpu->has_vfp != cpu->has_neon) {
193197a28b0eSPeter Maydell         /*
193297a28b0eSPeter Maydell          * This is an architectural requirement for AArch64; AArch32 is
193397a28b0eSPeter Maydell          * more flexible and permits VFP-no-Neon and Neon-no-VFP.
193497a28b0eSPeter Maydell          */
193597a28b0eSPeter Maydell         error_setg(errp,
193697a28b0eSPeter Maydell                    "AArch64 CPUs must have both VFP and Neon or neither");
193797a28b0eSPeter Maydell         return;
193897a28b0eSPeter Maydell     }
193997a28b0eSPeter Maydell 
194042bea956SCédric Le Goater     if (cpu->has_vfp_d32 != cpu->has_neon) {
194142bea956SCédric Le Goater         error_setg(errp, "ARM CPUs must have both VFP-D32 and Neon or neither");
194242bea956SCédric Le Goater         return;
194342bea956SCédric Le Goater     }
194442bea956SCédric Le Goater 
194542bea956SCédric Le Goater    if (!cpu->has_vfp_d32) {
194642bea956SCédric Le Goater         uint32_t u;
194742bea956SCédric Le Goater 
194842bea956SCédric Le Goater         u = cpu->isar.mvfr0;
194942bea956SCédric Le Goater         u = FIELD_DP32(u, MVFR0, SIMDREG, 1); /* 16 registers */
195042bea956SCédric Le Goater         cpu->isar.mvfr0 = u;
195142bea956SCédric Le Goater     }
195242bea956SCédric Le Goater 
195397a28b0eSPeter Maydell     if (!cpu->has_vfp) {
195497a28b0eSPeter Maydell         uint64_t t;
195597a28b0eSPeter Maydell         uint32_t u;
195697a28b0eSPeter Maydell 
195797a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar1;
195897a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR1, JSCVT, 0);
195997a28b0eSPeter Maydell         cpu->isar.id_aa64isar1 = t;
196097a28b0eSPeter Maydell 
196197a28b0eSPeter Maydell         t = cpu->isar.id_aa64pfr0;
196297a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64PFR0, FP, 0xf);
196397a28b0eSPeter Maydell         cpu->isar.id_aa64pfr0 = t;
196497a28b0eSPeter Maydell 
196597a28b0eSPeter Maydell         u = cpu->isar.id_isar6;
196697a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR6, JSCVT, 0);
19673c93dfa4SRichard Henderson         u = FIELD_DP32(u, ID_ISAR6, BF16, 0);
196897a28b0eSPeter Maydell         cpu->isar.id_isar6 = u;
196997a28b0eSPeter Maydell 
197097a28b0eSPeter Maydell         u = cpu->isar.mvfr0;
197197a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPSP, 0);
197297a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPDP, 0);
197397a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPDIVIDE, 0);
197497a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPSQRT, 0);
197597a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPROUND, 0);
1976532a3af5SPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
1977532a3af5SPeter Maydell             u = FIELD_DP32(u, MVFR0, FPTRAP, 0);
1978532a3af5SPeter Maydell             u = FIELD_DP32(u, MVFR0, FPSHVEC, 0);
1979532a3af5SPeter Maydell         }
198097a28b0eSPeter Maydell         cpu->isar.mvfr0 = u;
198197a28b0eSPeter Maydell 
198297a28b0eSPeter Maydell         u = cpu->isar.mvfr1;
198397a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR1, FPFTZ, 0);
198497a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR1, FPDNAN, 0);
198597a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR1, FPHP, 0);
1986532a3af5SPeter Maydell         if (arm_feature(env, ARM_FEATURE_M)) {
1987532a3af5SPeter Maydell             u = FIELD_DP32(u, MVFR1, FP16, 0);
1988532a3af5SPeter Maydell         }
198997a28b0eSPeter Maydell         cpu->isar.mvfr1 = u;
199097a28b0eSPeter Maydell 
199197a28b0eSPeter Maydell         u = cpu->isar.mvfr2;
199297a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR2, FPMISC, 0);
199397a28b0eSPeter Maydell         cpu->isar.mvfr2 = u;
199497a28b0eSPeter Maydell     }
199597a28b0eSPeter Maydell 
199697a28b0eSPeter Maydell     if (!cpu->has_neon) {
199797a28b0eSPeter Maydell         uint64_t t;
199897a28b0eSPeter Maydell         uint32_t u;
199997a28b0eSPeter Maydell 
200097a28b0eSPeter Maydell         unset_feature(env, ARM_FEATURE_NEON);
200197a28b0eSPeter Maydell 
200297a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar0;
2003eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, AES, 0);
2004eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SHA1, 0);
2005eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SHA2, 0);
2006eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SHA3, 0);
2007eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SM3, 0);
2008eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SM4, 0);
200997a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR0, DP, 0);
201097a28b0eSPeter Maydell         cpu->isar.id_aa64isar0 = t;
201197a28b0eSPeter Maydell 
201297a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar1;
201397a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR1, FCMA, 0);
20143c93dfa4SRichard Henderson         t = FIELD_DP64(t, ID_AA64ISAR1, BF16, 0);
2015f8680aaaSRichard Henderson         t = FIELD_DP64(t, ID_AA64ISAR1, I8MM, 0);
201697a28b0eSPeter Maydell         cpu->isar.id_aa64isar1 = t;
201797a28b0eSPeter Maydell 
201897a28b0eSPeter Maydell         t = cpu->isar.id_aa64pfr0;
201997a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64PFR0, ADVSIMD, 0xf);
202097a28b0eSPeter Maydell         cpu->isar.id_aa64pfr0 = t;
202197a28b0eSPeter Maydell 
202297a28b0eSPeter Maydell         u = cpu->isar.id_isar5;
2023eb851c11SDamien Hedde         u = FIELD_DP32(u, ID_ISAR5, AES, 0);
2024eb851c11SDamien Hedde         u = FIELD_DP32(u, ID_ISAR5, SHA1, 0);
2025eb851c11SDamien Hedde         u = FIELD_DP32(u, ID_ISAR5, SHA2, 0);
202697a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR5, RDM, 0);
202797a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR5, VCMA, 0);
202897a28b0eSPeter Maydell         cpu->isar.id_isar5 = u;
202997a28b0eSPeter Maydell 
203097a28b0eSPeter Maydell         u = cpu->isar.id_isar6;
203197a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR6, DP, 0);
203297a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR6, FHM, 0);
20333c93dfa4SRichard Henderson         u = FIELD_DP32(u, ID_ISAR6, BF16, 0);
2034f8680aaaSRichard Henderson         u = FIELD_DP32(u, ID_ISAR6, I8MM, 0);
203597a28b0eSPeter Maydell         cpu->isar.id_isar6 = u;
203697a28b0eSPeter Maydell 
2037532a3af5SPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
203897a28b0eSPeter Maydell             u = cpu->isar.mvfr1;
203997a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDLS, 0);
204097a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDINT, 0);
204197a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDSP, 0);
204297a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDHP, 0);
204397a28b0eSPeter Maydell             cpu->isar.mvfr1 = u;
204497a28b0eSPeter Maydell 
204597a28b0eSPeter Maydell             u = cpu->isar.mvfr2;
204697a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR2, SIMDMISC, 0);
204797a28b0eSPeter Maydell             cpu->isar.mvfr2 = u;
204897a28b0eSPeter Maydell         }
2049532a3af5SPeter Maydell     }
205097a28b0eSPeter Maydell 
205197a28b0eSPeter Maydell     if (!cpu->has_neon && !cpu->has_vfp) {
205297a28b0eSPeter Maydell         uint64_t t;
205397a28b0eSPeter Maydell         uint32_t u;
205497a28b0eSPeter Maydell 
205597a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar0;
205697a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR0, FHM, 0);
205797a28b0eSPeter Maydell         cpu->isar.id_aa64isar0 = t;
205897a28b0eSPeter Maydell 
205997a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar1;
206097a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR1, FRINTTS, 0);
206197a28b0eSPeter Maydell         cpu->isar.id_aa64isar1 = t;
206297a28b0eSPeter Maydell 
206397a28b0eSPeter Maydell         u = cpu->isar.mvfr0;
206497a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, SIMDREG, 0);
206597a28b0eSPeter Maydell         cpu->isar.mvfr0 = u;
2066c52881bbSRichard Henderson 
2067c52881bbSRichard Henderson         /* Despite the name, this field covers both VFP and Neon */
2068c52881bbSRichard Henderson         u = cpu->isar.mvfr1;
2069c52881bbSRichard Henderson         u = FIELD_DP32(u, MVFR1, SIMDFMAC, 0);
2070c52881bbSRichard Henderson         cpu->isar.mvfr1 = u;
207197a28b0eSPeter Maydell     }
207297a28b0eSPeter Maydell 
2073ea90db0aSPeter Maydell     if (arm_feature(env, ARM_FEATURE_M) && !cpu->has_dsp) {
2074ea90db0aSPeter Maydell         uint32_t u;
2075ea90db0aSPeter Maydell 
2076ea90db0aSPeter Maydell         unset_feature(env, ARM_FEATURE_THUMB_DSP);
2077ea90db0aSPeter Maydell 
2078ea90db0aSPeter Maydell         u = cpu->isar.id_isar1;
2079ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR1, EXTEND, 1);
2080ea90db0aSPeter Maydell         cpu->isar.id_isar1 = u;
2081ea90db0aSPeter Maydell 
2082ea90db0aSPeter Maydell         u = cpu->isar.id_isar2;
2083ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR2, MULTU, 1);
2084ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR2, MULTS, 1);
2085ea90db0aSPeter Maydell         cpu->isar.id_isar2 = u;
2086ea90db0aSPeter Maydell 
2087ea90db0aSPeter Maydell         u = cpu->isar.id_isar3;
2088ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR3, SIMD, 1);
2089ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR3, SATURATE, 0);
2090ea90db0aSPeter Maydell         cpu->isar.id_isar3 = u;
2091ea90db0aSPeter Maydell     }
2092ea90db0aSPeter Maydell 
2093fcf5ef2aSThomas Huth 
2094ea7ac69dSPeter Maydell     /*
2095ea7ac69dSPeter Maydell      * We rely on no XScale CPU having VFP so we can use the same bits in the
2096ea7ac69dSPeter Maydell      * TB flags field for VECSTRIDE and XSCALE_CPAR.
2097ea7ac69dSPeter Maydell      */
2098*ee1004bbSPhilippe Mathieu-Daudé     assert(arm_feature(env, ARM_FEATURE_AARCH64) ||
20997d63183fSRichard Henderson            !cpu_isar_feature(aa32_vfp_simd, cpu) ||
21007d63183fSRichard Henderson            !arm_feature(env, ARM_FEATURE_XSCALE));
2101ea7ac69dSPeter Maydell 
2102a575230fSRichard Henderson #ifndef CONFIG_USER_ONLY
2103a575230fSRichard Henderson     {
2104a575230fSRichard Henderson         int pagebits;
2105fcf5ef2aSThomas Huth         if (arm_feature(env, ARM_FEATURE_V7) &&
2106fcf5ef2aSThomas Huth             !arm_feature(env, ARM_FEATURE_M) &&
2107452a0955SPeter Maydell             !arm_feature(env, ARM_FEATURE_PMSA)) {
2108a575230fSRichard Henderson             /*
2109a575230fSRichard Henderson              * v7VMSA drops support for the old ARMv5 tiny pages,
2110a575230fSRichard Henderson              * so we can use 4K pages.
2111fcf5ef2aSThomas Huth              */
2112fcf5ef2aSThomas Huth             pagebits = 12;
2113fcf5ef2aSThomas Huth         } else {
2114a575230fSRichard Henderson             /*
2115a575230fSRichard Henderson              * For CPUs which might have tiny 1K pages, or which have an
2116fcf5ef2aSThomas Huth              * MPU and might have small region sizes, stick with 1K pages.
2117fcf5ef2aSThomas Huth              */
2118fcf5ef2aSThomas Huth             pagebits = 10;
2119fcf5ef2aSThomas Huth         }
2120fcf5ef2aSThomas Huth         if (!set_preferred_target_page_bits(pagebits)) {
2121a575230fSRichard Henderson             /*
2122a575230fSRichard Henderson              * This can only ever happen for hotplugging a CPU, or if
2123fcf5ef2aSThomas Huth              * the board code incorrectly creates a CPU which it has
2124fcf5ef2aSThomas Huth              * promised via minimum_page_size that it will not.
2125fcf5ef2aSThomas Huth              */
2126a575230fSRichard Henderson             error_setg(errp, "This CPU requires a smaller page size "
2127a575230fSRichard Henderson                        "than the system is using");
2128fcf5ef2aSThomas Huth             return;
2129fcf5ef2aSThomas Huth         }
2130a575230fSRichard Henderson     }
2131a575230fSRichard Henderson #endif
2132fcf5ef2aSThomas Huth 
2133fcf5ef2aSThomas Huth     /* This cpu-id-to-MPIDR affinity is used only for TCG; KVM will override it.
2134fcf5ef2aSThomas Huth      * We don't support setting cluster ID ([16..23]) (known as Aff2
2135fcf5ef2aSThomas Huth      * in later ARM ARM versions), or any of the higher affinity level fields,
2136fcf5ef2aSThomas Huth      * so these bits always RAZ.
2137fcf5ef2aSThomas Huth      */
2138fcf5ef2aSThomas Huth     if (cpu->mp_affinity == ARM64_AFFINITY_INVALID) {
2139750245edSRichard Henderson         cpu->mp_affinity = arm_build_mp_affinity(cs->cpu_index,
214046de5913SIgor Mammedov                                                  ARM_DEFAULT_CPUS_PER_CLUSTER);
2141fcf5ef2aSThomas Huth     }
2142fcf5ef2aSThomas Huth 
2143fcf5ef2aSThomas Huth     if (cpu->reset_hivecs) {
2144fcf5ef2aSThomas Huth             cpu->reset_sctlr |= (1 << 13);
2145fcf5ef2aSThomas Huth     }
2146fcf5ef2aSThomas Huth 
21473a062d57SJulian Brown     if (cpu->cfgend) {
2148*ee1004bbSPhilippe Mathieu-Daudé         if (arm_feature(env, ARM_FEATURE_V7)) {
21493a062d57SJulian Brown             cpu->reset_sctlr |= SCTLR_EE;
21503a062d57SJulian Brown         } else {
21513a062d57SJulian Brown             cpu->reset_sctlr |= SCTLR_B;
21523a062d57SJulian Brown         }
21533a062d57SJulian Brown     }
21543a062d57SJulian Brown 
215540188188SPeter Maydell     if (!arm_feature(env, ARM_FEATURE_M) && !cpu->has_el3) {
2156fcf5ef2aSThomas Huth         /* If the has_el3 CPU property is disabled then we need to disable the
2157fcf5ef2aSThomas Huth          * feature.
2158fcf5ef2aSThomas Huth          */
2159fcf5ef2aSThomas Huth         unset_feature(env, ARM_FEATURE_EL3);
2160fcf5ef2aSThomas Huth 
2161b13c91c0SRichard Henderson         /*
2162b13c91c0SRichard Henderson          * Disable the security extension feature bits in the processor
2163b13c91c0SRichard Henderson          * feature registers as well.
2164fcf5ef2aSThomas Huth          */
2165b13c91c0SRichard Henderson         cpu->isar.id_pfr1 = FIELD_DP32(cpu->isar.id_pfr1, ID_PFR1, SECURITY, 0);
2166033a4f15SRichard Henderson         cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, COPSDBG, 0);
2167b13c91c0SRichard Henderson         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2168b13c91c0SRichard Henderson                                            ID_AA64PFR0, EL3, 0);
2169b9f335c2SRichard Henderson 
2170b9f335c2SRichard Henderson         /* Disable the realm management extension, which requires EL3. */
2171b9f335c2SRichard Henderson         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2172b9f335c2SRichard Henderson                                            ID_AA64PFR0, RME, 0);
2173fcf5ef2aSThomas Huth     }
2174fcf5ef2aSThomas Huth 
2175c25bd18aSPeter Maydell     if (!cpu->has_el2) {
2176c25bd18aSPeter Maydell         unset_feature(env, ARM_FEATURE_EL2);
2177c25bd18aSPeter Maydell     }
2178c25bd18aSPeter Maydell 
2179d6f02ce3SWei Huang     if (!cpu->has_pmu) {
2180fcf5ef2aSThomas Huth         unset_feature(env, ARM_FEATURE_PMU);
218157a4a11bSAaron Lindsay     }
218257a4a11bSAaron Lindsay     if (arm_feature(env, ARM_FEATURE_PMU)) {
2183bf8d0969SAaron Lindsay OS         pmu_init(cpu);
218457a4a11bSAaron Lindsay 
218557a4a11bSAaron Lindsay         if (!kvm_enabled()) {
2186033614c4SAaron Lindsay             arm_register_pre_el_change_hook(cpu, &pmu_pre_el_change, 0);
2187033614c4SAaron Lindsay             arm_register_el_change_hook(cpu, &pmu_post_el_change, 0);
2188fcf5ef2aSThomas Huth         }
21894e7beb0cSAaron Lindsay OS 
21904e7beb0cSAaron Lindsay OS #ifndef CONFIG_USER_ONLY
21914e7beb0cSAaron Lindsay OS         cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, arm_pmu_timer_cb,
21924e7beb0cSAaron Lindsay OS                 cpu);
21934e7beb0cSAaron Lindsay OS #endif
219457a4a11bSAaron Lindsay     } else {
21952a609df8SPeter Maydell         cpu->isar.id_aa64dfr0 =
21962a609df8SPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMUVER, 0);
2197a6179538SPeter Maydell         cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, PERFMON, 0);
219857a4a11bSAaron Lindsay         cpu->pmceid0 = 0;
219957a4a11bSAaron Lindsay         cpu->pmceid1 = 0;
220057a4a11bSAaron Lindsay     }
2201fcf5ef2aSThomas Huth 
2202fcf5ef2aSThomas Huth     if (!arm_feature(env, ARM_FEATURE_EL2)) {
2203b13c91c0SRichard Henderson         /*
2204b13c91c0SRichard Henderson          * Disable the hypervisor feature bits in the processor feature
2205b13c91c0SRichard Henderson          * registers if we don't have EL2.
2206fcf5ef2aSThomas Huth          */
2207b13c91c0SRichard Henderson         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2208b13c91c0SRichard Henderson                                            ID_AA64PFR0, EL2, 0);
2209b13c91c0SRichard Henderson         cpu->isar.id_pfr1 = FIELD_DP32(cpu->isar.id_pfr1,
2210b13c91c0SRichard Henderson                                        ID_PFR1, VIRTUALIZATION, 0);
2211fcf5ef2aSThomas Huth     }
2212fcf5ef2aSThomas Huth 
22137134cb07SRichard Henderson     if (cpu_isar_feature(aa64_mte, cpu)) {
22147134cb07SRichard Henderson         /*
22157134cb07SRichard Henderson          * The architectural range of GM blocksize is 2-6, however qemu
22167134cb07SRichard Henderson          * doesn't support blocksize of 2 (see HELPER(ldgm)).
22177134cb07SRichard Henderson          */
22187134cb07SRichard Henderson         if (tcg_enabled()) {
22197134cb07SRichard Henderson             assert(cpu->gm_blocksize >= 3 && cpu->gm_blocksize <= 6);
22207134cb07SRichard Henderson         }
22217134cb07SRichard Henderson 
22226f4e1405SRichard Henderson #ifndef CONFIG_USER_ONLY
22236f4e1405SRichard Henderson         /*
2224cd305b5fSRichard Henderson          * If we do not have tag-memory provided by the machine,
2225cd305b5fSRichard Henderson          * reduce MTE support to instructions enabled at EL0.
2226cd305b5fSRichard Henderson          * This matches Cortex-A710 BROADCASTMTE input being LOW.
22276f4e1405SRichard Henderson          */
22287134cb07SRichard Henderson         if (cpu->tag_memory == NULL) {
22296f4e1405SRichard Henderson             cpu->isar.id_aa64pfr1 =
2230cd305b5fSRichard Henderson                 FIELD_DP64(cpu->isar.id_aa64pfr1, ID_AA64PFR1, MTE, 1);
22316f4e1405SRichard Henderson         }
22326f4e1405SRichard Henderson #endif
22337134cb07SRichard Henderson     }
22346f4e1405SRichard Henderson 
22352daf518dSPeter Maydell     if (tcg_enabled()) {
22362daf518dSPeter Maydell         /*
22377d8c283eSPeter Maydell          * Don't report some architectural features in the ID registers
22387d8c283eSPeter Maydell          * where TCG does not yet implement it (not even a minimal
22397d8c283eSPeter Maydell          * stub version). This avoids guests falling over when they
22407d8c283eSPeter Maydell          * try to access the non-existent system registers for them.
22412daf518dSPeter Maydell          */
22427d8c283eSPeter Maydell         /* FEAT_SPE (Statistical Profiling Extension) */
22432daf518dSPeter Maydell         cpu->isar.id_aa64dfr0 =
22442daf518dSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMSVER, 0);
22453d5f45ecSRichard Henderson         /* FEAT_TRBE (Trace Buffer Extension) */
22463d5f45ecSRichard Henderson         cpu->isar.id_aa64dfr0 =
22473d5f45ecSRichard Henderson             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEBUFFER, 0);
22487d8c283eSPeter Maydell         /* FEAT_TRF (Self-hosted Trace Extension) */
22497d8c283eSPeter Maydell         cpu->isar.id_aa64dfr0 =
22507d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEFILT, 0);
22517d8c283eSPeter Maydell         cpu->isar.id_dfr0 =
22527d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, TRACEFILT, 0);
22537d8c283eSPeter Maydell         /* Trace Macrocell system register access */
22547d8c283eSPeter Maydell         cpu->isar.id_aa64dfr0 =
22557d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEVER, 0);
22567d8c283eSPeter Maydell         cpu->isar.id_dfr0 =
22577d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, COPTRC, 0);
22587d8c283eSPeter Maydell         /* Memory mapped trace */
22597d8c283eSPeter Maydell         cpu->isar.id_dfr0 =
22607d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, MMAPTRC, 0);
22617d8c283eSPeter Maydell         /* FEAT_AMU (Activity Monitors Extension) */
22627d8c283eSPeter Maydell         cpu->isar.id_aa64pfr0 =
22637d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64pfr0, ID_AA64PFR0, AMU, 0);
22647d8c283eSPeter Maydell         cpu->isar.id_pfr0 =
22657d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_pfr0, ID_PFR0, AMU, 0);
22667d8c283eSPeter Maydell         /* FEAT_MPAM (Memory Partitioning and Monitoring Extension) */
22677d8c283eSPeter Maydell         cpu->isar.id_aa64pfr0 =
22687d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64pfr0, ID_AA64PFR0, MPAM, 0);
22692daf518dSPeter Maydell     }
22702daf518dSPeter Maydell 
2271f50cd314SPeter Maydell     /* MPU can be configured out of a PMSA CPU either by setting has-mpu
2272f50cd314SPeter Maydell      * to false or by setting pmsav7-dregion to 0.
2273f50cd314SPeter Maydell      */
2274761c4642STobias Röhmel     if (!cpu->has_mpu || cpu->pmsav7_dregion == 0) {
2275f50cd314SPeter Maydell         cpu->has_mpu = false;
2276761c4642STobias Röhmel         cpu->pmsav7_dregion = 0;
2277761c4642STobias Röhmel         cpu->pmsav8r_hdregion = 0;
2278fcf5ef2aSThomas Huth     }
2279fcf5ef2aSThomas Huth 
2280452a0955SPeter Maydell     if (arm_feature(env, ARM_FEATURE_PMSA) &&
2281fcf5ef2aSThomas Huth         arm_feature(env, ARM_FEATURE_V7)) {
2282fcf5ef2aSThomas Huth         uint32_t nr = cpu->pmsav7_dregion;
2283fcf5ef2aSThomas Huth 
2284fcf5ef2aSThomas Huth         if (nr > 0xff) {
2285fcf5ef2aSThomas Huth             error_setg(errp, "PMSAv7 MPU #regions invalid %" PRIu32, nr);
2286fcf5ef2aSThomas Huth             return;
2287fcf5ef2aSThomas Huth         }
2288fcf5ef2aSThomas Huth 
2289fcf5ef2aSThomas Huth         if (nr) {
22900e1a46bbSPeter Maydell             if (arm_feature(env, ARM_FEATURE_V8)) {
22910e1a46bbSPeter Maydell                 /* PMSAv8 */
229262c58ee0SPeter Maydell                 env->pmsav8.rbar[M_REG_NS] = g_new0(uint32_t, nr);
229362c58ee0SPeter Maydell                 env->pmsav8.rlar[M_REG_NS] = g_new0(uint32_t, nr);
229462c58ee0SPeter Maydell                 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
229562c58ee0SPeter Maydell                     env->pmsav8.rbar[M_REG_S] = g_new0(uint32_t, nr);
229662c58ee0SPeter Maydell                     env->pmsav8.rlar[M_REG_S] = g_new0(uint32_t, nr);
229762c58ee0SPeter Maydell                 }
22980e1a46bbSPeter Maydell             } else {
2299fcf5ef2aSThomas Huth                 env->pmsav7.drbar = g_new0(uint32_t, nr);
2300fcf5ef2aSThomas Huth                 env->pmsav7.drsr = g_new0(uint32_t, nr);
2301fcf5ef2aSThomas Huth                 env->pmsav7.dracr = g_new0(uint32_t, nr);
2302fcf5ef2aSThomas Huth             }
2303fcf5ef2aSThomas Huth         }
2304761c4642STobias Röhmel 
2305761c4642STobias Röhmel         if (cpu->pmsav8r_hdregion > 0xff) {
2306761c4642STobias Röhmel             error_setg(errp, "PMSAv8 MPU EL2 #regions invalid %" PRIu32,
2307761c4642STobias Röhmel                               cpu->pmsav8r_hdregion);
2308761c4642STobias Röhmel             return;
2309761c4642STobias Röhmel         }
2310761c4642STobias Röhmel 
2311761c4642STobias Röhmel         if (cpu->pmsav8r_hdregion) {
2312761c4642STobias Röhmel             env->pmsav8.hprbar = g_new0(uint32_t,
2313761c4642STobias Röhmel                                         cpu->pmsav8r_hdregion);
2314761c4642STobias Röhmel             env->pmsav8.hprlar = g_new0(uint32_t,
2315761c4642STobias Röhmel                                         cpu->pmsav8r_hdregion);
2316761c4642STobias Röhmel         }
23170e1a46bbSPeter Maydell     }
2318fcf5ef2aSThomas Huth 
23199901c576SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
23209901c576SPeter Maydell         uint32_t nr = cpu->sau_sregion;
23219901c576SPeter Maydell 
23229901c576SPeter Maydell         if (nr > 0xff) {
23239901c576SPeter Maydell             error_setg(errp, "v8M SAU #regions invalid %" PRIu32, nr);
23249901c576SPeter Maydell             return;
23259901c576SPeter Maydell         }
23269901c576SPeter Maydell 
23279901c576SPeter Maydell         if (nr) {
23289901c576SPeter Maydell             env->sau.rbar = g_new0(uint32_t, nr);
23299901c576SPeter Maydell             env->sau.rlar = g_new0(uint32_t, nr);
23309901c576SPeter Maydell         }
23319901c576SPeter Maydell     }
23329901c576SPeter Maydell 
233391db4642SCédric Le Goater     if (arm_feature(env, ARM_FEATURE_EL3)) {
233491db4642SCédric Le Goater         set_feature(env, ARM_FEATURE_VBAR);
233591db4642SCédric Le Goater     }
233691db4642SCédric Le Goater 
2337f6fc36deSJean-Philippe Brucker #ifndef CONFIG_USER_ONLY
2338f6fc36deSJean-Philippe Brucker     if (tcg_enabled() && cpu_isar_feature(aa64_rme, cpu)) {
2339f6fc36deSJean-Philippe Brucker         arm_register_el_change_hook(cpu, &gt_rme_post_el_change, 0);
2340f6fc36deSJean-Philippe Brucker     }
2341f6fc36deSJean-Philippe Brucker #endif
2342f6fc36deSJean-Philippe Brucker 
2343fcf5ef2aSThomas Huth     register_cp_regs_for_features(cpu);
2344fcf5ef2aSThomas Huth     arm_cpu_register_gdb_regs_for_features(cpu);
2345fcf5ef2aSThomas Huth 
2346fcf5ef2aSThomas Huth     init_cpreg_list(cpu);
2347fcf5ef2aSThomas Huth 
2348fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
2349cc7d44c2SLike Xu     MachineState *ms = MACHINE(qdev_get_machine());
2350cc7d44c2SLike Xu     unsigned int smp_cpus = ms->smp.cpus;
23518bce44a2SRichard Henderson     bool has_secure = cpu->has_el3 || arm_feature(env, ARM_FEATURE_M_SECURITY);
2352cc7d44c2SLike Xu 
23538bce44a2SRichard Henderson     /*
23548bce44a2SRichard Henderson      * We must set cs->num_ases to the final value before
23558bce44a2SRichard Henderson      * the first call to cpu_address_space_init.
23568bce44a2SRichard Henderson      */
23578bce44a2SRichard Henderson     if (cpu->tag_memory != NULL) {
23588bce44a2SRichard Henderson         cs->num_ases = 3 + has_secure;
23598bce44a2SRichard Henderson     } else {
23608bce44a2SRichard Henderson         cs->num_ases = 1 + has_secure;
23618bce44a2SRichard Henderson     }
23621d2091bcSPeter Maydell 
23638bce44a2SRichard Henderson     if (has_secure) {
2364fcf5ef2aSThomas Huth         if (!cpu->secure_memory) {
2365fcf5ef2aSThomas Huth             cpu->secure_memory = cs->memory;
2366fcf5ef2aSThomas Huth         }
236780ceb07aSPeter Xu         cpu_address_space_init(cs, ARMASIdx_S, "cpu-secure-memory",
236880ceb07aSPeter Xu                                cpu->secure_memory);
2369fcf5ef2aSThomas Huth     }
23708bce44a2SRichard Henderson 
23718bce44a2SRichard Henderson     if (cpu->tag_memory != NULL) {
23728bce44a2SRichard Henderson         cpu_address_space_init(cs, ARMASIdx_TagNS, "cpu-tag-memory",
23738bce44a2SRichard Henderson                                cpu->tag_memory);
23748bce44a2SRichard Henderson         if (has_secure) {
23758bce44a2SRichard Henderson             cpu_address_space_init(cs, ARMASIdx_TagS, "cpu-tag-memory",
23768bce44a2SRichard Henderson                                    cpu->secure_tag_memory);
23778bce44a2SRichard Henderson         }
23788bce44a2SRichard Henderson     }
23798bce44a2SRichard Henderson 
238080ceb07aSPeter Xu     cpu_address_space_init(cs, ARMASIdx_NS, "cpu-memory", cs->memory);
2381f9a69711SAlistair Francis 
2382f9a69711SAlistair Francis     /* No core_count specified, default to smp_cpus. */
2383f9a69711SAlistair Francis     if (cpu->core_count == -1) {
2384f9a69711SAlistair Francis         cpu->core_count = smp_cpus;
2385f9a69711SAlistair Francis     }
2386fcf5ef2aSThomas Huth #endif
2387fcf5ef2aSThomas Huth 
2388a4157b80SRichard Henderson     if (tcg_enabled()) {
2389a4157b80SRichard Henderson         int dcz_blocklen = 4 << cpu->dcz_blocksize;
2390a4157b80SRichard Henderson 
2391a4157b80SRichard Henderson         /*
2392a4157b80SRichard Henderson          * We only support DCZ blocklen that fits on one page.
2393a4157b80SRichard Henderson          *
2394a4157b80SRichard Henderson          * Architectually this is always true.  However TARGET_PAGE_SIZE
2395a4157b80SRichard Henderson          * is variable and, for compatibility with -machine virt-2.7,
2396a4157b80SRichard Henderson          * is only 1KiB, as an artifact of legacy ARMv5 subpage support.
2397a4157b80SRichard Henderson          * But even then, while the largest architectural DCZ blocklen
2398a4157b80SRichard Henderson          * is 2KiB, no cpu actually uses such a large blocklen.
2399a4157b80SRichard Henderson          */
2400a4157b80SRichard Henderson         assert(dcz_blocklen <= TARGET_PAGE_SIZE);
2401a4157b80SRichard Henderson 
2402a4157b80SRichard Henderson         /*
2403a4157b80SRichard Henderson          * We only support DCZ blocksize >= 2*TAG_GRANULE, which is to say
2404a4157b80SRichard Henderson          * both nibbles of each byte storing tag data may be written at once.
2405a4157b80SRichard Henderson          * Since TAG_GRANULE is 16, this means that blocklen must be >= 32.
2406a4157b80SRichard Henderson          */
2407a4157b80SRichard Henderson         if (cpu_isar_feature(aa64_mte, cpu)) {
2408a4157b80SRichard Henderson             assert(dcz_blocklen >= 2 * TAG_GRANULE);
2409a4157b80SRichard Henderson         }
2410a4157b80SRichard Henderson     }
2411a4157b80SRichard Henderson 
2412fcf5ef2aSThomas Huth     qemu_init_vcpu(cs);
2413fcf5ef2aSThomas Huth     cpu_reset(cs);
2414fcf5ef2aSThomas Huth 
2415fcf5ef2aSThomas Huth     acc->parent_realize(dev, errp);
2416fcf5ef2aSThomas Huth }
2417fcf5ef2aSThomas Huth 
2418fcf5ef2aSThomas Huth static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
2419fcf5ef2aSThomas Huth {
2420fcf5ef2aSThomas Huth     ObjectClass *oc;
2421fcf5ef2aSThomas Huth     char *typename;
2422fcf5ef2aSThomas Huth     char **cpuname;
2423a0032cc5SPeter Maydell     const char *cpunamestr;
2424fcf5ef2aSThomas Huth 
2425fcf5ef2aSThomas Huth     cpuname = g_strsplit(cpu_model, ",", 1);
2426a0032cc5SPeter Maydell     cpunamestr = cpuname[0];
2427a0032cc5SPeter Maydell #ifdef CONFIG_USER_ONLY
2428a0032cc5SPeter Maydell     /* For backwards compatibility usermode emulation allows "-cpu any",
2429a0032cc5SPeter Maydell      * which has the same semantics as "-cpu max".
2430a0032cc5SPeter Maydell      */
2431a0032cc5SPeter Maydell     if (!strcmp(cpunamestr, "any")) {
2432a0032cc5SPeter Maydell         cpunamestr = "max";
2433a0032cc5SPeter Maydell     }
2434a0032cc5SPeter Maydell #endif
2435a0032cc5SPeter Maydell     typename = g_strdup_printf(ARM_CPU_TYPE_NAME("%s"), cpunamestr);
2436fcf5ef2aSThomas Huth     oc = object_class_by_name(typename);
2437fcf5ef2aSThomas Huth     g_strfreev(cpuname);
2438fcf5ef2aSThomas Huth     g_free(typename);
2439d5be19f5SPhilippe Mathieu-Daudé 
2440fcf5ef2aSThomas Huth     return oc;
2441fcf5ef2aSThomas Huth }
2442fcf5ef2aSThomas Huth 
2443fcf5ef2aSThomas Huth static Property arm_cpu_properties[] = {
2444e544f800SPhilippe Mathieu-Daudé     DEFINE_PROP_UINT64("midr", ARMCPU, midr, 0),
2445fcf5ef2aSThomas Huth     DEFINE_PROP_UINT64("mp-affinity", ARMCPU,
2446fcf5ef2aSThomas Huth                         mp_affinity, ARM64_AFFINITY_INVALID),
244715f8b142SIgor Mammedov     DEFINE_PROP_INT32("node-id", ARMCPU, node_id, CPU_UNSET_NUMA_NODE_ID),
2448f9a69711SAlistair Francis     DEFINE_PROP_INT32("core-count", ARMCPU, core_count, -1),
2449fcf5ef2aSThomas Huth     DEFINE_PROP_END_OF_LIST()
2450fcf5ef2aSThomas Huth };
2451fcf5ef2aSThomas Huth 
2452a6506838SAkihiko Odaki static const gchar *arm_gdb_arch_name(CPUState *cs)
2453fcf5ef2aSThomas Huth {
2454fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(cs);
2455fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
2456fcf5ef2aSThomas Huth 
2457fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
2458a6506838SAkihiko Odaki         return "iwmmxt";
2459fcf5ef2aSThomas Huth     }
2460a6506838SAkihiko Odaki     return "arm";
2461fcf5ef2aSThomas Huth }
2462fcf5ef2aSThomas Huth 
24638b80bd28SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY
24648b80bd28SPhilippe Mathieu-Daudé #include "hw/core/sysemu-cpu-ops.h"
24658b80bd28SPhilippe Mathieu-Daudé 
24668b80bd28SPhilippe Mathieu-Daudé static const struct SysemuCPUOps arm_sysemu_ops = {
246708928c6dSPhilippe Mathieu-Daudé     .get_phys_page_attrs_debug = arm_cpu_get_phys_page_attrs_debug,
2468faf39e82SPhilippe Mathieu-Daudé     .asidx_from_attrs = arm_asidx_from_attrs,
2469715e3c1aSPhilippe Mathieu-Daudé     .write_elf32_note = arm_cpu_write_elf32_note,
2470715e3c1aSPhilippe Mathieu-Daudé     .write_elf64_note = arm_cpu_write_elf64_note,
2471da383e02SPhilippe Mathieu-Daudé     .virtio_is_big_endian = arm_cpu_virtio_is_big_endian,
2472feece4d0SPhilippe Mathieu-Daudé     .legacy_vmsd = &vmstate_arm_cpu,
24738b80bd28SPhilippe Mathieu-Daudé };
24748b80bd28SPhilippe Mathieu-Daudé #endif
24758b80bd28SPhilippe Mathieu-Daudé 
247678271684SClaudio Fontana #ifdef CONFIG_TCG
24771764ad70SRichard Henderson static const TCGCPUOps arm_tcg_ops = {
247878271684SClaudio Fontana     .initialize = arm_translate_init,
247978271684SClaudio Fontana     .synchronize_from_tb = arm_cpu_synchronize_from_tb,
248078271684SClaudio Fontana     .debug_excp_handler = arm_debug_excp_handler,
248156c6c98dSRichard Henderson     .restore_state_to_opc = arm_restore_state_to_opc,
248278271684SClaudio Fontana 
24839b12b6b4SRichard Henderson #ifdef CONFIG_USER_ONLY
24849b12b6b4SRichard Henderson     .record_sigsegv = arm_cpu_record_sigsegv,
248539a099caSRichard Henderson     .record_sigbus = arm_cpu_record_sigbus,
24869b12b6b4SRichard Henderson #else
24879b12b6b4SRichard Henderson     .tlb_fill = arm_cpu_tlb_fill,
2488083afd18SPhilippe Mathieu-Daudé     .cpu_exec_interrupt = arm_cpu_exec_interrupt,
248978271684SClaudio Fontana     .do_interrupt = arm_cpu_do_interrupt,
249078271684SClaudio Fontana     .do_transaction_failed = arm_cpu_do_transaction_failed,
249178271684SClaudio Fontana     .do_unaligned_access = arm_cpu_do_unaligned_access,
249278271684SClaudio Fontana     .adjust_watchpoint_address = arm_adjust_watchpoint_address,
249378271684SClaudio Fontana     .debug_check_watchpoint = arm_debug_check_watchpoint,
2494b00d86bcSRichard Henderson     .debug_check_breakpoint = arm_debug_check_breakpoint,
249578271684SClaudio Fontana #endif /* !CONFIG_USER_ONLY */
249678271684SClaudio Fontana };
249778271684SClaudio Fontana #endif /* CONFIG_TCG */
249878271684SClaudio Fontana 
2499fcf5ef2aSThomas Huth static void arm_cpu_class_init(ObjectClass *oc, void *data)
2500fcf5ef2aSThomas Huth {
2501fcf5ef2aSThomas Huth     ARMCPUClass *acc = ARM_CPU_CLASS(oc);
2502fcf5ef2aSThomas Huth     CPUClass *cc = CPU_CLASS(acc);
2503fcf5ef2aSThomas Huth     DeviceClass *dc = DEVICE_CLASS(oc);
25049130cadeSPeter Maydell     ResettableClass *rc = RESETTABLE_CLASS(oc);
2505fcf5ef2aSThomas Huth 
2506bf853881SPhilippe Mathieu-Daudé     device_class_set_parent_realize(dc, arm_cpu_realizefn,
2507bf853881SPhilippe Mathieu-Daudé                                     &acc->parent_realize);
2508fcf5ef2aSThomas Huth 
25094f67d30bSMarc-André Lureau     device_class_set_props(dc, arm_cpu_properties);
25109130cadeSPeter Maydell 
25119130cadeSPeter Maydell     resettable_class_set_parent_phases(rc, NULL, arm_cpu_reset_hold, NULL,
25129130cadeSPeter Maydell                                        &acc->parent_phases);
2513fcf5ef2aSThomas Huth 
2514fcf5ef2aSThomas Huth     cc->class_by_name = arm_cpu_class_by_name;
2515fcf5ef2aSThomas Huth     cc->has_work = arm_cpu_has_work;
2516628421c7SRichard Henderson     cc->mmu_index = arm_cpu_mmu_index;
2517fcf5ef2aSThomas Huth     cc->dump_state = arm_cpu_dump_state;
2518fcf5ef2aSThomas Huth     cc->set_pc = arm_cpu_set_pc;
2519e4fdf9dfSRichard Henderson     cc->get_pc = arm_cpu_get_pc;
2520fcf5ef2aSThomas Huth     cc->gdb_read_register = arm_cpu_gdb_read_register;
2521fcf5ef2aSThomas Huth     cc->gdb_write_register = arm_cpu_gdb_write_register;
25227350d553SRichard Henderson #ifndef CONFIG_USER_ONLY
25238b80bd28SPhilippe Mathieu-Daudé     cc->sysemu_ops = &arm_sysemu_ops;
2524fcf5ef2aSThomas Huth #endif
2525fcf5ef2aSThomas Huth     cc->gdb_arch_name = arm_gdb_arch_name;
2526fcf5ef2aSThomas Huth     cc->gdb_stop_before_watchpoint = true;
2527fcf5ef2aSThomas Huth     cc->disas_set_info = arm_disas_set_info;
252878271684SClaudio Fontana 
252974d7fc7fSRichard Henderson #ifdef CONFIG_TCG
253078271684SClaudio Fontana     cc->tcg_ops = &arm_tcg_ops;
2531cbc183d2SClaudio Fontana #endif /* CONFIG_TCG */
2532fcf5ef2aSThomas Huth }
2533fcf5ef2aSThomas Huth 
253451e5ef45SMarc-André Lureau static void arm_cpu_instance_init(Object *obj)
253551e5ef45SMarc-André Lureau {
253651e5ef45SMarc-André Lureau     ARMCPUClass *acc = ARM_CPU_GET_CLASS(obj);
253751e5ef45SMarc-André Lureau 
253851e5ef45SMarc-André Lureau     acc->info->initfn(obj);
253951e5ef45SMarc-André Lureau     arm_cpu_post_init(obj);
254051e5ef45SMarc-André Lureau }
254151e5ef45SMarc-André Lureau 
254251e5ef45SMarc-André Lureau static void cpu_register_class_init(ObjectClass *oc, void *data)
254351e5ef45SMarc-André Lureau {
254451e5ef45SMarc-André Lureau     ARMCPUClass *acc = ARM_CPU_CLASS(oc);
254548de6462SAkihiko Odaki     CPUClass *cc = CPU_CLASS(acc);
254651e5ef45SMarc-André Lureau 
254751e5ef45SMarc-André Lureau     acc->info = data;
254848de6462SAkihiko Odaki     cc->gdb_core_xml_file = "arm-core.xml";
254951e5ef45SMarc-André Lureau }
255051e5ef45SMarc-André Lureau 
255137bcf244SThomas Huth void arm_cpu_register(const ARMCPUInfo *info)
2552fcf5ef2aSThomas Huth {
2553fcf5ef2aSThomas Huth     TypeInfo type_info = {
2554fcf5ef2aSThomas Huth         .parent = TYPE_ARM_CPU,
255551e5ef45SMarc-André Lureau         .instance_init = arm_cpu_instance_init,
255651e5ef45SMarc-André Lureau         .class_init = info->class_init ?: cpu_register_class_init,
255751e5ef45SMarc-André Lureau         .class_data = (void *)info,
2558fcf5ef2aSThomas Huth     };
2559fcf5ef2aSThomas Huth 
2560fcf5ef2aSThomas Huth     type_info.name = g_strdup_printf("%s-" TYPE_ARM_CPU, info->name);
2561fcf5ef2aSThomas Huth     type_register(&type_info);
2562fcf5ef2aSThomas Huth     g_free((void *)type_info.name);
2563fcf5ef2aSThomas Huth }
2564fcf5ef2aSThomas Huth 
2565fcf5ef2aSThomas Huth static const TypeInfo arm_cpu_type_info = {
2566fcf5ef2aSThomas Huth     .name = TYPE_ARM_CPU,
2567fcf5ef2aSThomas Huth     .parent = TYPE_CPU,
2568fcf5ef2aSThomas Huth     .instance_size = sizeof(ARMCPU),
2569d03087bdSRichard Henderson     .instance_align = __alignof__(ARMCPU),
2570fcf5ef2aSThomas Huth     .instance_init = arm_cpu_initfn,
2571fcf5ef2aSThomas Huth     .instance_finalize = arm_cpu_finalizefn,
2572fcf5ef2aSThomas Huth     .abstract = true,
2573fcf5ef2aSThomas Huth     .class_size = sizeof(ARMCPUClass),
2574fcf5ef2aSThomas Huth     .class_init = arm_cpu_class_init,
2575fcf5ef2aSThomas Huth };
2576fcf5ef2aSThomas Huth 
2577fcf5ef2aSThomas Huth static void arm_cpu_register_types(void)
2578fcf5ef2aSThomas Huth {
2579fcf5ef2aSThomas Huth     type_register_static(&arm_cpu_type_info);
2580fcf5ef2aSThomas Huth }
2581fcf5ef2aSThomas Huth 
2582fcf5ef2aSThomas Huth type_init(arm_cpu_register_types)
2583