xref: /openbmc/qemu/target/arm/cpu.c (revision 185e3fdf8d106cb2f7d234d5e6453939c66db2a9)
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 
136b5c53d1bSAaron Lindsay void arm_register_pre_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook,
137b5c53d1bSAaron Lindsay                                  void *opaque)
138b5c53d1bSAaron Lindsay {
139b5c53d1bSAaron Lindsay     ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1);
140b5c53d1bSAaron Lindsay 
141b5c53d1bSAaron Lindsay     entry->hook = hook;
142b5c53d1bSAaron Lindsay     entry->opaque = opaque;
143b5c53d1bSAaron Lindsay 
144b5c53d1bSAaron Lindsay     QLIST_INSERT_HEAD(&cpu->pre_el_change_hooks, entry, node);
145b5c53d1bSAaron Lindsay }
146b5c53d1bSAaron Lindsay 
14708267487SAaron Lindsay void arm_register_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook,
148fcf5ef2aSThomas Huth                                  void *opaque)
149fcf5ef2aSThomas Huth {
15008267487SAaron Lindsay     ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1);
15108267487SAaron Lindsay 
15208267487SAaron Lindsay     entry->hook = hook;
15308267487SAaron Lindsay     entry->opaque = opaque;
15408267487SAaron Lindsay 
15508267487SAaron Lindsay     QLIST_INSERT_HEAD(&cpu->el_change_hooks, entry, node);
156fcf5ef2aSThomas Huth }
157fcf5ef2aSThomas Huth 
158fcf5ef2aSThomas Huth static void cp_reg_reset(gpointer key, gpointer value, gpointer opaque)
159fcf5ef2aSThomas Huth {
160fcf5ef2aSThomas Huth     /* Reset a single ARMCPRegInfo register */
161fcf5ef2aSThomas Huth     ARMCPRegInfo *ri = value;
162fcf5ef2aSThomas Huth     ARMCPU *cpu = opaque;
163fcf5ef2aSThomas Huth 
16487c3f0f2SRichard Henderson     if (ri->type & (ARM_CP_SPECIAL_MASK | ARM_CP_ALIAS)) {
165fcf5ef2aSThomas Huth         return;
166fcf5ef2aSThomas Huth     }
167fcf5ef2aSThomas Huth 
168fcf5ef2aSThomas Huth     if (ri->resetfn) {
169fcf5ef2aSThomas Huth         ri->resetfn(&cpu->env, ri);
170fcf5ef2aSThomas Huth         return;
171fcf5ef2aSThomas Huth     }
172fcf5ef2aSThomas Huth 
173fcf5ef2aSThomas Huth     /* A zero offset is never possible as it would be regs[0]
174fcf5ef2aSThomas Huth      * so we use it to indicate that reset is being handled elsewhere.
175fcf5ef2aSThomas Huth      * This is basically only used for fields in non-core coprocessors
176fcf5ef2aSThomas Huth      * (like the pxa2xx ones).
177fcf5ef2aSThomas Huth      */
178fcf5ef2aSThomas Huth     if (!ri->fieldoffset) {
179fcf5ef2aSThomas Huth         return;
180fcf5ef2aSThomas Huth     }
181fcf5ef2aSThomas Huth 
182fcf5ef2aSThomas Huth     if (cpreg_field_is_64bit(ri)) {
183fcf5ef2aSThomas Huth         CPREG_FIELD64(&cpu->env, ri) = ri->resetvalue;
184fcf5ef2aSThomas Huth     } else {
185fcf5ef2aSThomas Huth         CPREG_FIELD32(&cpu->env, ri) = ri->resetvalue;
186fcf5ef2aSThomas Huth     }
187fcf5ef2aSThomas Huth }
188fcf5ef2aSThomas Huth 
189fcf5ef2aSThomas Huth static void cp_reg_check_reset(gpointer key, gpointer value,  gpointer opaque)
190fcf5ef2aSThomas Huth {
191fcf5ef2aSThomas Huth     /* Purely an assertion check: we've already done reset once,
192fcf5ef2aSThomas Huth      * so now check that running the reset for the cpreg doesn't
193fcf5ef2aSThomas Huth      * change its value. This traps bugs where two different cpregs
194fcf5ef2aSThomas Huth      * both try to reset the same state field but to different values.
195fcf5ef2aSThomas Huth      */
196fcf5ef2aSThomas Huth     ARMCPRegInfo *ri = value;
197fcf5ef2aSThomas Huth     ARMCPU *cpu = opaque;
198fcf5ef2aSThomas Huth     uint64_t oldvalue, newvalue;
199fcf5ef2aSThomas Huth 
20087c3f0f2SRichard Henderson     if (ri->type & (ARM_CP_SPECIAL_MASK | ARM_CP_ALIAS | ARM_CP_NO_RAW)) {
201fcf5ef2aSThomas Huth         return;
202fcf5ef2aSThomas Huth     }
203fcf5ef2aSThomas Huth 
204fcf5ef2aSThomas Huth     oldvalue = read_raw_cp_reg(&cpu->env, ri);
205fcf5ef2aSThomas Huth     cp_reg_reset(key, value, opaque);
206fcf5ef2aSThomas Huth     newvalue = read_raw_cp_reg(&cpu->env, ri);
207fcf5ef2aSThomas Huth     assert(oldvalue == newvalue);
208fcf5ef2aSThomas Huth }
209fcf5ef2aSThomas Huth 
2109130cadeSPeter Maydell static void arm_cpu_reset_hold(Object *obj)
211fcf5ef2aSThomas Huth {
2129130cadeSPeter Maydell     CPUState *s = CPU(obj);
213fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(s);
214fcf5ef2aSThomas Huth     ARMCPUClass *acc = ARM_CPU_GET_CLASS(cpu);
215fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
216fcf5ef2aSThomas Huth 
2179130cadeSPeter Maydell     if (acc->parent_phases.hold) {
2189130cadeSPeter Maydell         acc->parent_phases.hold(obj);
2199130cadeSPeter Maydell     }
220fcf5ef2aSThomas Huth 
2211f5c00cfSAlex Bennée     memset(env, 0, offsetof(CPUARMState, end_reset_fields));
2221f5c00cfSAlex Bennée 
223fcf5ef2aSThomas Huth     g_hash_table_foreach(cpu->cp_regs, cp_reg_reset, cpu);
224fcf5ef2aSThomas Huth     g_hash_table_foreach(cpu->cp_regs, cp_reg_check_reset, cpu);
225fcf5ef2aSThomas Huth 
226fcf5ef2aSThomas Huth     env->vfp.xregs[ARM_VFP_FPSID] = cpu->reset_fpsid;
22747576b94SRichard Henderson     env->vfp.xregs[ARM_VFP_MVFR0] = cpu->isar.mvfr0;
22847576b94SRichard Henderson     env->vfp.xregs[ARM_VFP_MVFR1] = cpu->isar.mvfr1;
22947576b94SRichard Henderson     env->vfp.xregs[ARM_VFP_MVFR2] = cpu->isar.mvfr2;
230fcf5ef2aSThomas Huth 
231c1b70158SThiago Jung Bauermann     cpu->power_state = s->start_powered_off ? PSCI_OFF : PSCI_ON;
232fcf5ef2aSThomas Huth 
233fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
234fcf5ef2aSThomas Huth         env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
235fcf5ef2aSThomas Huth     }
236fcf5ef2aSThomas Huth 
237fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
238fcf5ef2aSThomas Huth         /* 64 bit CPUs always start in 64 bit mode */
23953221552SRichard Henderson         env->aarch64 = true;
240fcf5ef2aSThomas Huth #if defined(CONFIG_USER_ONLY)
241fcf5ef2aSThomas Huth         env->pstate = PSTATE_MODE_EL0t;
242fcf5ef2aSThomas Huth         /* Userspace expects access to DC ZVA, CTL_EL0 and the cache ops */
243fcf5ef2aSThomas Huth         env->cp15.sctlr_el[1] |= SCTLR_UCT | SCTLR_UCI | SCTLR_DZE;
244276c6e81SRichard Henderson         /* Enable all PAC keys.  */
245276c6e81SRichard Henderson         env->cp15.sctlr_el[1] |= (SCTLR_EnIA | SCTLR_EnIB |
246276c6e81SRichard Henderson                                   SCTLR_EnDA | SCTLR_EnDB);
247cda86e2bSRichard Henderson         /* Trap on btype=3 for PACIxSP. */
248cda86e2bSRichard Henderson         env->cp15.sctlr_el[1] |= SCTLR_BT0;
249d03396a8SRichard Henderson         /* Trap on implementation defined registers. */
250d03396a8SRichard Henderson         if (cpu_isar_feature(aa64_tidcp1, cpu)) {
251d03396a8SRichard Henderson             env->cp15.sctlr_el[1] |= SCTLR_TIDCP;
252d03396a8SRichard Henderson         }
253fcf5ef2aSThomas Huth         /* and to the FP/Neon instructions */
254fab8ad39SRichard Henderson         env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
255fab8ad39SRichard Henderson                                          CPACR_EL1, FPEN, 3);
25646303535SRichard Henderson         /* and to the SVE instructions, with default vector length */
25746303535SRichard Henderson         if (cpu_isar_feature(aa64_sve, cpu)) {
258fab8ad39SRichard Henderson             env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
259fab8ad39SRichard Henderson                                              CPACR_EL1, ZEN, 3);
26087252bdeSRichard Henderson             env->vfp.zcr_el[1] = cpu->sve_default_vq - 1;
2617b6a2198SAlex Bennée         }
26278011586SRichard Henderson         /* and for SME instructions, with default vector length, and TPIDR2 */
26378011586SRichard Henderson         if (cpu_isar_feature(aa64_sme, cpu)) {
26478011586SRichard Henderson             env->cp15.sctlr_el[1] |= SCTLR_EnTP2;
26578011586SRichard Henderson             env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
26678011586SRichard Henderson                                              CPACR_EL1, SMEN, 3);
26778011586SRichard Henderson             env->vfp.smcr_el[1] = cpu->sme_default_vq - 1;
26878011586SRichard Henderson             if (cpu_isar_feature(aa64_sme_fa64, cpu)) {
26978011586SRichard Henderson                 env->vfp.smcr_el[1] = FIELD_DP64(env->vfp.smcr_el[1],
27078011586SRichard Henderson                                                  SMCR, FA64, 1);
27178011586SRichard Henderson             }
27278011586SRichard Henderson         }
273f6a148feSRichard Henderson         /*
274691f1ffdSRichard Henderson          * Enable 48-bit address space (TODO: take reserved_va into account).
27516c84978SRichard Henderson          * Enable TBI0 but not TBI1.
27616c84978SRichard Henderson          * Note that this must match useronly_clean_ptr.
277f6a148feSRichard Henderson          */
278cb4a0a34SPeter Maydell         env->cp15.tcr_el[1] = 5 | (1ULL << 37);
279e3232864SRichard Henderson 
280e3232864SRichard Henderson         /* Enable MTE */
281e3232864SRichard Henderson         if (cpu_isar_feature(aa64_mte, cpu)) {
282e3232864SRichard Henderson             /* Enable tag access, but leave TCF0 as No Effect (0). */
283e3232864SRichard Henderson             env->cp15.sctlr_el[1] |= SCTLR_ATA0;
284e3232864SRichard Henderson             /*
285e3232864SRichard Henderson              * Exclude all tags, so that tag 0 is always used.
286e3232864SRichard Henderson              * This corresponds to Linux current->thread.gcr_incl = 0.
287e3232864SRichard Henderson              *
288e3232864SRichard Henderson              * Set RRND, so that helper_irg() will generate a seed later.
289e3232864SRichard Henderson              * Here in cpu_reset(), the crypto subsystem has not yet been
290e3232864SRichard Henderson              * initialized.
291e3232864SRichard Henderson              */
292e3232864SRichard Henderson             env->cp15.gcr_el1 = 0x1ffff;
293e3232864SRichard Henderson         }
2947cb1e618SRichard Henderson         /*
2957cb1e618SRichard Henderson          * Disable access to SCXTNUM_EL0 from CSV2_1p2.
2967cb1e618SRichard Henderson          * This is not yet exposed from the Linux kernel in any way.
2977cb1e618SRichard Henderson          */
2987cb1e618SRichard Henderson         env->cp15.sctlr_el[1] |= SCTLR_TSCXT;
299f9ac7788SZhuojia Shen         /* Disable access to Debug Communication Channel (DCC). */
300f9ac7788SZhuojia Shen         env->cp15.mdscr_el1 |= 1 << 12;
301a6b2c5a0SPeter Maydell         /* Enable FEAT_MOPS */
302a6b2c5a0SPeter Maydell         env->cp15.sctlr_el[1] |= SCTLR_MSCEN;
303fcf5ef2aSThomas Huth #else
304fcf5ef2aSThomas Huth         /* Reset into the highest available EL */
305fcf5ef2aSThomas Huth         if (arm_feature(env, ARM_FEATURE_EL3)) {
306fcf5ef2aSThomas Huth             env->pstate = PSTATE_MODE_EL3h;
307fcf5ef2aSThomas Huth         } else if (arm_feature(env, ARM_FEATURE_EL2)) {
308fcf5ef2aSThomas Huth             env->pstate = PSTATE_MODE_EL2h;
309fcf5ef2aSThomas Huth         } else {
310fcf5ef2aSThomas Huth             env->pstate = PSTATE_MODE_EL1h;
311fcf5ef2aSThomas Huth         }
3124a7319b7SEdgar E. Iglesias 
3134a7319b7SEdgar E. Iglesias         /* Sample rvbar at reset.  */
3144a7319b7SEdgar E. Iglesias         env->cp15.rvbar = cpu->rvbar_prop;
3154a7319b7SEdgar E. Iglesias         env->pc = env->cp15.rvbar;
316fcf5ef2aSThomas Huth #endif
317fcf5ef2aSThomas Huth     } else {
318fcf5ef2aSThomas Huth #if defined(CONFIG_USER_ONLY)
319fcf5ef2aSThomas Huth         /* Userspace expects access to cp10 and cp11 for FP/Neon */
320fab8ad39SRichard Henderson         env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
321fab8ad39SRichard Henderson                                          CPACR, CP10, 3);
322fab8ad39SRichard Henderson         env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
323fab8ad39SRichard Henderson                                          CPACR, CP11, 3);
324fcf5ef2aSThomas Huth #endif
325910e4f24STobias Röhmel         if (arm_feature(env, ARM_FEATURE_V8)) {
326910e4f24STobias Röhmel             env->cp15.rvbar = cpu->rvbar_prop;
327910e4f24STobias Röhmel             env->regs[15] = cpu->rvbar_prop;
328910e4f24STobias Röhmel         }
329fcf5ef2aSThomas Huth     }
330fcf5ef2aSThomas Huth 
331fcf5ef2aSThomas Huth #if defined(CONFIG_USER_ONLY)
332fcf5ef2aSThomas Huth     env->uncached_cpsr = ARM_CPU_MODE_USR;
333fcf5ef2aSThomas Huth     /* For user mode we must enable access to coprocessors */
334fcf5ef2aSThomas Huth     env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
335fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
336fcf5ef2aSThomas Huth         env->cp15.c15_cpar = 3;
337fcf5ef2aSThomas Huth     } else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
338fcf5ef2aSThomas Huth         env->cp15.c15_cpar = 1;
339fcf5ef2aSThomas Huth     }
340fcf5ef2aSThomas Huth #else
341060a65dfSPeter Maydell 
342060a65dfSPeter Maydell     /*
343060a65dfSPeter Maydell      * If the highest available EL is EL2, AArch32 will start in Hyp
344060a65dfSPeter Maydell      * mode; otherwise it starts in SVC. Note that if we start in
345060a65dfSPeter Maydell      * AArch64 then these values in the uncached_cpsr will be ignored.
346060a65dfSPeter Maydell      */
347060a65dfSPeter Maydell     if (arm_feature(env, ARM_FEATURE_EL2) &&
348060a65dfSPeter Maydell         !arm_feature(env, ARM_FEATURE_EL3)) {
349060a65dfSPeter Maydell         env->uncached_cpsr = ARM_CPU_MODE_HYP;
350060a65dfSPeter Maydell     } else {
351fcf5ef2aSThomas Huth         env->uncached_cpsr = ARM_CPU_MODE_SVC;
352060a65dfSPeter Maydell     }
353fcf5ef2aSThomas Huth     env->daif = PSTATE_D | PSTATE_A | PSTATE_I | PSTATE_F;
3541426f244SPeter Maydell 
3551426f244SPeter Maydell     /* AArch32 has a hard highvec setting of 0xFFFF0000.  If we are currently
3561426f244SPeter Maydell      * executing as AArch32 then check if highvecs are enabled and
3571426f244SPeter Maydell      * adjust the PC accordingly.
3581426f244SPeter Maydell      */
3591426f244SPeter Maydell     if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
3601426f244SPeter Maydell         env->regs[15] = 0xFFFF0000;
3611426f244SPeter Maydell     }
3621426f244SPeter Maydell 
3631426f244SPeter Maydell     env->vfp.xregs[ARM_VFP_FPEXC] = 0;
364b62ceeafSPeter Maydell #endif
365dc7abe4dSMichael Davidsaver 
366531c60a9SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M)) {
367b62ceeafSPeter Maydell #ifndef CONFIG_USER_ONLY
368fcf5ef2aSThomas Huth         uint32_t initial_msp; /* Loaded from 0x0 */
369fcf5ef2aSThomas Huth         uint32_t initial_pc; /* Loaded from 0x4 */
370fcf5ef2aSThomas Huth         uint8_t *rom;
37138e2a77cSPeter Maydell         uint32_t vecbase;
372b62ceeafSPeter Maydell #endif
373fcf5ef2aSThomas Huth 
3748128c8e8SPeter Maydell         if (cpu_isar_feature(aa32_lob, cpu)) {
3758128c8e8SPeter Maydell             /*
3768128c8e8SPeter Maydell              * LTPSIZE is constant 4 if MVE not implemented, and resets
3778128c8e8SPeter Maydell              * to an UNKNOWN value if MVE is implemented. We choose to
3788128c8e8SPeter Maydell              * always reset to 4.
3798128c8e8SPeter Maydell              */
3808128c8e8SPeter Maydell             env->v7m.ltpsize = 4;
38199c7834fSPeter Maydell             /* The LTPSIZE field in FPDSCR is constant and reads as 4. */
38299c7834fSPeter Maydell             env->v7m.fpdscr[M_REG_NS] = 4 << FPCR_LTPSIZE_SHIFT;
38399c7834fSPeter Maydell             env->v7m.fpdscr[M_REG_S] = 4 << FPCR_LTPSIZE_SHIFT;
3848128c8e8SPeter Maydell         }
3858128c8e8SPeter Maydell 
3861e577cc7SPeter Maydell         if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
3871e577cc7SPeter Maydell             env->v7m.secure = true;
3883b2e9344SPeter Maydell         } else {
3893b2e9344SPeter Maydell             /* This bit resets to 0 if security is supported, but 1 if
3903b2e9344SPeter Maydell              * it is not. The bit is not present in v7M, but we set it
3913b2e9344SPeter Maydell              * here so we can avoid having to make checks on it conditional
3923b2e9344SPeter Maydell              * on ARM_FEATURE_V8 (we don't let the guest see the bit).
3933b2e9344SPeter Maydell              */
3943b2e9344SPeter Maydell             env->v7m.aircr = R_V7M_AIRCR_BFHFNMINS_MASK;
39502ac2f7fSPeter Maydell             /*
39602ac2f7fSPeter Maydell              * Set NSACR to indicate "NS access permitted to everything";
39702ac2f7fSPeter Maydell              * this avoids having to have all the tests of it being
39802ac2f7fSPeter Maydell              * conditional on ARM_FEATURE_M_SECURITY. Note also that from
39902ac2f7fSPeter Maydell              * v8.1M the guest-visible value of NSACR in a CPU without the
40002ac2f7fSPeter Maydell              * Security Extension is 0xcff.
40102ac2f7fSPeter Maydell              */
40202ac2f7fSPeter Maydell             env->v7m.nsacr = 0xcff;
4031e577cc7SPeter Maydell         }
4041e577cc7SPeter Maydell 
4059d40cd8aSPeter Maydell         /* In v7M the reset value of this bit is IMPDEF, but ARM recommends
4062c4da50dSPeter Maydell          * that it resets to 1, so QEMU always does that rather than making
4079d40cd8aSPeter Maydell          * it dependent on CPU model. In v8M it is RES1.
4082c4da50dSPeter Maydell          */
4099d40cd8aSPeter Maydell         env->v7m.ccr[M_REG_NS] = R_V7M_CCR_STKALIGN_MASK;
4109d40cd8aSPeter Maydell         env->v7m.ccr[M_REG_S] = R_V7M_CCR_STKALIGN_MASK;
4119d40cd8aSPeter Maydell         if (arm_feature(env, ARM_FEATURE_V8)) {
4129d40cd8aSPeter Maydell             /* in v8M the NONBASETHRDENA bit [0] is RES1 */
4139d40cd8aSPeter Maydell             env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_NONBASETHRDENA_MASK;
4149d40cd8aSPeter Maydell             env->v7m.ccr[M_REG_S] |= R_V7M_CCR_NONBASETHRDENA_MASK;
4159d40cd8aSPeter Maydell         }
41622ab3460SJulia Suvorova         if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
41722ab3460SJulia Suvorova             env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_UNALIGN_TRP_MASK;
41822ab3460SJulia Suvorova             env->v7m.ccr[M_REG_S] |= R_V7M_CCR_UNALIGN_TRP_MASK;
41922ab3460SJulia Suvorova         }
4202c4da50dSPeter Maydell 
4217fbc6a40SRichard Henderson         if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
422d33abe82SPeter Maydell             env->v7m.fpccr[M_REG_NS] = R_V7M_FPCCR_ASPEN_MASK;
423d33abe82SPeter Maydell             env->v7m.fpccr[M_REG_S] = R_V7M_FPCCR_ASPEN_MASK |
424d33abe82SPeter Maydell                 R_V7M_FPCCR_LSPEN_MASK | R_V7M_FPCCR_S_MASK;
425d33abe82SPeter Maydell         }
426b62ceeafSPeter Maydell 
427b62ceeafSPeter Maydell #ifndef CONFIG_USER_ONLY
428056f43dfSPeter Maydell         /* Unlike A/R profile, M profile defines the reset LR value */
429056f43dfSPeter Maydell         env->regs[14] = 0xffffffff;
430056f43dfSPeter Maydell 
43138e2a77cSPeter Maydell         env->v7m.vecbase[M_REG_S] = cpu->init_svtor & 0xffffff80;
4327cda2149SPeter Maydell         env->v7m.vecbase[M_REG_NS] = cpu->init_nsvtor & 0xffffff80;
43338e2a77cSPeter Maydell 
43438e2a77cSPeter Maydell         /* Load the initial SP and PC from offset 0 and 4 in the vector table */
43538e2a77cSPeter Maydell         vecbase = env->v7m.vecbase[env->v7m.secure];
43675ce72b7SPeter Maydell         rom = rom_ptr_for_as(s->as, vecbase, 8);
437fcf5ef2aSThomas Huth         if (rom) {
438fcf5ef2aSThomas Huth             /* Address zero is covered by ROM which hasn't yet been
439fcf5ef2aSThomas Huth              * copied into physical memory.
440fcf5ef2aSThomas Huth              */
441fcf5ef2aSThomas Huth             initial_msp = ldl_p(rom);
442fcf5ef2aSThomas Huth             initial_pc = ldl_p(rom + 4);
443fcf5ef2aSThomas Huth         } else {
444fcf5ef2aSThomas Huth             /* Address zero not covered by a ROM blob, or the ROM blob
445fcf5ef2aSThomas Huth              * is in non-modifiable memory and this is a second reset after
446fcf5ef2aSThomas Huth              * it got copied into memory. In the latter case, rom_ptr
447fcf5ef2aSThomas Huth              * will return a NULL pointer and we should use ldl_phys instead.
448fcf5ef2aSThomas Huth              */
44938e2a77cSPeter Maydell             initial_msp = ldl_phys(s->as, vecbase);
45038e2a77cSPeter Maydell             initial_pc = ldl_phys(s->as, vecbase + 4);
451fcf5ef2aSThomas Huth         }
452fcf5ef2aSThomas Huth 
4538cc2246cSPeter Maydell         qemu_log_mask(CPU_LOG_INT,
4548cc2246cSPeter Maydell                       "Loaded reset SP 0x%x PC 0x%x from vector table\n",
4558cc2246cSPeter Maydell                       initial_msp, initial_pc);
4568cc2246cSPeter Maydell 
457fcf5ef2aSThomas Huth         env->regs[13] = initial_msp & 0xFFFFFFFC;
458fcf5ef2aSThomas Huth         env->regs[15] = initial_pc & ~1;
459fcf5ef2aSThomas Huth         env->thumb = initial_pc & 1;
460b62ceeafSPeter Maydell #else
461b62ceeafSPeter Maydell         /*
462b62ceeafSPeter Maydell          * For user mode we run non-secure and with access to the FPU.
463b62ceeafSPeter Maydell          * The FPU context is active (ie does not need further setup)
464b62ceeafSPeter Maydell          * and is owned by non-secure.
465b62ceeafSPeter Maydell          */
466b62ceeafSPeter Maydell         env->v7m.secure = false;
467b62ceeafSPeter Maydell         env->v7m.nsacr = 0xcff;
468b62ceeafSPeter Maydell         env->v7m.cpacr[M_REG_NS] = 0xf0ffff;
469b62ceeafSPeter Maydell         env->v7m.fpccr[M_REG_S] &=
470b62ceeafSPeter Maydell             ~(R_V7M_FPCCR_LSPEN_MASK | R_V7M_FPCCR_S_MASK);
471b62ceeafSPeter Maydell         env->v7m.control[M_REG_S] |= R_V7M_CONTROL_FPCA_MASK;
472b62ceeafSPeter Maydell #endif
473fcf5ef2aSThomas Huth     }
474fcf5ef2aSThomas Huth 
475dc3c4c14SPeter Maydell     /* M profile requires that reset clears the exclusive monitor;
476dc3c4c14SPeter Maydell      * A profile does not, but clearing it makes more sense than having it
477dc3c4c14SPeter Maydell      * set with an exclusive access on address zero.
478dc3c4c14SPeter Maydell      */
479dc3c4c14SPeter Maydell     arm_clear_exclusive(env);
480dc3c4c14SPeter Maydell 
4810e1a46bbSPeter Maydell     if (arm_feature(env, ARM_FEATURE_PMSA)) {
48269ceea64SPeter Maydell         if (cpu->pmsav7_dregion > 0) {
4830e1a46bbSPeter Maydell             if (arm_feature(env, ARM_FEATURE_V8)) {
48462c58ee0SPeter Maydell                 memset(env->pmsav8.rbar[M_REG_NS], 0,
48562c58ee0SPeter Maydell                        sizeof(*env->pmsav8.rbar[M_REG_NS])
48662c58ee0SPeter Maydell                        * cpu->pmsav7_dregion);
48762c58ee0SPeter Maydell                 memset(env->pmsav8.rlar[M_REG_NS], 0,
48862c58ee0SPeter Maydell                        sizeof(*env->pmsav8.rlar[M_REG_NS])
48962c58ee0SPeter Maydell                        * cpu->pmsav7_dregion);
49062c58ee0SPeter Maydell                 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
49162c58ee0SPeter Maydell                     memset(env->pmsav8.rbar[M_REG_S], 0,
49262c58ee0SPeter Maydell                            sizeof(*env->pmsav8.rbar[M_REG_S])
49362c58ee0SPeter Maydell                            * cpu->pmsav7_dregion);
49462c58ee0SPeter Maydell                     memset(env->pmsav8.rlar[M_REG_S], 0,
49562c58ee0SPeter Maydell                            sizeof(*env->pmsav8.rlar[M_REG_S])
49662c58ee0SPeter Maydell                            * cpu->pmsav7_dregion);
49762c58ee0SPeter Maydell                 }
4980e1a46bbSPeter Maydell             } else if (arm_feature(env, ARM_FEATURE_V7)) {
49969ceea64SPeter Maydell                 memset(env->pmsav7.drbar, 0,
50069ceea64SPeter Maydell                        sizeof(*env->pmsav7.drbar) * cpu->pmsav7_dregion);
50169ceea64SPeter Maydell                 memset(env->pmsav7.drsr, 0,
50269ceea64SPeter Maydell                        sizeof(*env->pmsav7.drsr) * cpu->pmsav7_dregion);
50369ceea64SPeter Maydell                 memset(env->pmsav7.dracr, 0,
50469ceea64SPeter Maydell                        sizeof(*env->pmsav7.dracr) * cpu->pmsav7_dregion);
50569ceea64SPeter Maydell             }
5060e1a46bbSPeter Maydell         }
507761c4642STobias Röhmel 
508761c4642STobias Röhmel         if (cpu->pmsav8r_hdregion > 0) {
509761c4642STobias Röhmel             memset(env->pmsav8.hprbar, 0,
510761c4642STobias Röhmel                    sizeof(*env->pmsav8.hprbar) * cpu->pmsav8r_hdregion);
511761c4642STobias Röhmel             memset(env->pmsav8.hprlar, 0,
512761c4642STobias Röhmel                    sizeof(*env->pmsav8.hprlar) * cpu->pmsav8r_hdregion);
513761c4642STobias Röhmel         }
514761c4642STobias Röhmel 
5151bc04a88SPeter Maydell         env->pmsav7.rnr[M_REG_NS] = 0;
5161bc04a88SPeter Maydell         env->pmsav7.rnr[M_REG_S] = 0;
5174125e6feSPeter Maydell         env->pmsav8.mair0[M_REG_NS] = 0;
5184125e6feSPeter Maydell         env->pmsav8.mair0[M_REG_S] = 0;
5194125e6feSPeter Maydell         env->pmsav8.mair1[M_REG_NS] = 0;
5204125e6feSPeter Maydell         env->pmsav8.mair1[M_REG_S] = 0;
52169ceea64SPeter Maydell     }
52269ceea64SPeter Maydell 
5239901c576SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
5249901c576SPeter Maydell         if (cpu->sau_sregion > 0) {
5259901c576SPeter Maydell             memset(env->sau.rbar, 0, sizeof(*env->sau.rbar) * cpu->sau_sregion);
5269901c576SPeter Maydell             memset(env->sau.rlar, 0, sizeof(*env->sau.rlar) * cpu->sau_sregion);
5279901c576SPeter Maydell         }
5289901c576SPeter Maydell         env->sau.rnr = 0;
5299901c576SPeter Maydell         /* SAU_CTRL reset value is IMPDEF; we choose 0, which is what
5309901c576SPeter Maydell          * the Cortex-M33 does.
5319901c576SPeter Maydell          */
5329901c576SPeter Maydell         env->sau.ctrl = 0;
5339901c576SPeter Maydell     }
5349901c576SPeter Maydell 
535fcf5ef2aSThomas Huth     set_flush_to_zero(1, &env->vfp.standard_fp_status);
536fcf5ef2aSThomas Huth     set_flush_inputs_to_zero(1, &env->vfp.standard_fp_status);
537fcf5ef2aSThomas Huth     set_default_nan_mode(1, &env->vfp.standard_fp_status);
538aaae563bSPeter Maydell     set_default_nan_mode(1, &env->vfp.standard_fp_status_f16);
539fcf5ef2aSThomas Huth     set_float_detect_tininess(float_tininess_before_rounding,
540fcf5ef2aSThomas Huth                               &env->vfp.fp_status);
541fcf5ef2aSThomas Huth     set_float_detect_tininess(float_tininess_before_rounding,
542fcf5ef2aSThomas Huth                               &env->vfp.standard_fp_status);
543bcc531f0SPeter Maydell     set_float_detect_tininess(float_tininess_before_rounding,
544bcc531f0SPeter Maydell                               &env->vfp.fp_status_f16);
545aaae563bSPeter Maydell     set_float_detect_tininess(float_tininess_before_rounding,
546aaae563bSPeter Maydell                               &env->vfp.standard_fp_status_f16);
547fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
548fcf5ef2aSThomas Huth     if (kvm_enabled()) {
549fcf5ef2aSThomas Huth         kvm_arm_reset_vcpu(cpu);
550fcf5ef2aSThomas Huth     }
551fcf5ef2aSThomas Huth #endif
552fcf5ef2aSThomas Huth 
553fa05d1abSFabiano Rosas     if (tcg_enabled()) {
554fcf5ef2aSThomas Huth         hw_breakpoint_update_all(cpu);
555fcf5ef2aSThomas Huth         hw_watchpoint_update_all(cpu);
5562b77ad4dSFabiano Rosas 
557a8a79c7aSRichard Henderson         arm_rebuild_hflags(env);
558fcf5ef2aSThomas Huth     }
5592b77ad4dSFabiano Rosas }
560fcf5ef2aSThomas Huth 
5613a45f4f5SPeter Maydell void arm_emulate_firmware_reset(CPUState *cpustate, int target_el)
5623a45f4f5SPeter Maydell {
5633a45f4f5SPeter Maydell     ARMCPU *cpu = ARM_CPU(cpustate);
5643a45f4f5SPeter Maydell     CPUARMState *env = &cpu->env;
5653a45f4f5SPeter Maydell     bool have_el3 = arm_feature(env, ARM_FEATURE_EL3);
5663a45f4f5SPeter Maydell     bool have_el2 = arm_feature(env, ARM_FEATURE_EL2);
5673a45f4f5SPeter Maydell 
5683a45f4f5SPeter Maydell     /*
5693a45f4f5SPeter Maydell      * Check we have the EL we're aiming for. If that is the
5703a45f4f5SPeter Maydell      * highest implemented EL, then cpu_reset has already done
5713a45f4f5SPeter Maydell      * all the work.
5723a45f4f5SPeter Maydell      */
5733a45f4f5SPeter Maydell     switch (target_el) {
5743a45f4f5SPeter Maydell     case 3:
5753a45f4f5SPeter Maydell         assert(have_el3);
5763a45f4f5SPeter Maydell         return;
5773a45f4f5SPeter Maydell     case 2:
5783a45f4f5SPeter Maydell         assert(have_el2);
5793a45f4f5SPeter Maydell         if (!have_el3) {
5803a45f4f5SPeter Maydell             return;
5813a45f4f5SPeter Maydell         }
5823a45f4f5SPeter Maydell         break;
5833a45f4f5SPeter Maydell     case 1:
5843a45f4f5SPeter Maydell         if (!have_el3 && !have_el2) {
5853a45f4f5SPeter Maydell             return;
5863a45f4f5SPeter Maydell         }
5873a45f4f5SPeter Maydell         break;
5883a45f4f5SPeter Maydell     default:
5893a45f4f5SPeter Maydell         g_assert_not_reached();
5903a45f4f5SPeter Maydell     }
5913a45f4f5SPeter Maydell 
5923a45f4f5SPeter Maydell     if (have_el3) {
5933a45f4f5SPeter Maydell         /*
5943a45f4f5SPeter Maydell          * Set the EL3 state so code can run at EL2. This should match
5953a45f4f5SPeter Maydell          * the requirements set by Linux in its booting spec.
5963a45f4f5SPeter Maydell          */
5973a45f4f5SPeter Maydell         if (env->aarch64) {
5983a45f4f5SPeter Maydell             env->cp15.scr_el3 |= SCR_RW;
5993a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_pauth, cpu)) {
6003a45f4f5SPeter Maydell                 env->cp15.scr_el3 |= SCR_API | SCR_APK;
6013a45f4f5SPeter Maydell             }
6023a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_mte, cpu)) {
6033a45f4f5SPeter Maydell                 env->cp15.scr_el3 |= SCR_ATA;
6043a45f4f5SPeter Maydell             }
6053a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_sve, cpu)) {
6063a45f4f5SPeter Maydell                 env->cp15.cptr_el[3] |= R_CPTR_EL3_EZ_MASK;
6073a45f4f5SPeter Maydell                 env->vfp.zcr_el[3] = 0xf;
6083a45f4f5SPeter Maydell             }
6093a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_sme, cpu)) {
6103a45f4f5SPeter Maydell                 env->cp15.cptr_el[3] |= R_CPTR_EL3_ESM_MASK;
6113a45f4f5SPeter Maydell                 env->cp15.scr_el3 |= SCR_ENTP2;
6123a45f4f5SPeter Maydell                 env->vfp.smcr_el[3] = 0xf;
6133a45f4f5SPeter Maydell             }
6143a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_hcx, cpu)) {
6153a45f4f5SPeter Maydell                 env->cp15.scr_el3 |= SCR_HXEN;
6163a45f4f5SPeter Maydell             }
6173a45f4f5SPeter Maydell             if (cpu_isar_feature(aa64_fgt, cpu)) {
6183a45f4f5SPeter Maydell                 env->cp15.scr_el3 |= SCR_FGTEN;
6193a45f4f5SPeter Maydell             }
6203a45f4f5SPeter Maydell         }
6213a45f4f5SPeter Maydell 
6223a45f4f5SPeter Maydell         if (target_el == 2) {
6233a45f4f5SPeter Maydell             /* If the guest is at EL2 then Linux expects the HVC insn to work */
6243a45f4f5SPeter Maydell             env->cp15.scr_el3 |= SCR_HCE;
6253a45f4f5SPeter Maydell         }
6263a45f4f5SPeter Maydell 
6273a45f4f5SPeter Maydell         /* Put CPU into non-secure state */
6283a45f4f5SPeter Maydell         env->cp15.scr_el3 |= SCR_NS;
6293a45f4f5SPeter Maydell         /* Set NSACR.{CP11,CP10} so NS can access the FPU */
6303a45f4f5SPeter Maydell         env->cp15.nsacr |= 3 << 10;
6313a45f4f5SPeter Maydell     }
6323a45f4f5SPeter Maydell 
6333a45f4f5SPeter Maydell     if (have_el2 && target_el < 2) {
6343a45f4f5SPeter Maydell         /* Set EL2 state so code can run at EL1. */
6353a45f4f5SPeter Maydell         if (env->aarch64) {
6363a45f4f5SPeter Maydell             env->cp15.hcr_el2 |= HCR_RW;
6373a45f4f5SPeter Maydell         }
6383a45f4f5SPeter Maydell     }
6393a45f4f5SPeter Maydell 
6403a45f4f5SPeter Maydell     /* Set the CPU to the desired state */
6413a45f4f5SPeter Maydell     if (env->aarch64) {
6423a45f4f5SPeter Maydell         env->pstate = aarch64_pstate_mode(target_el, true);
6433a45f4f5SPeter Maydell     } else {
6443a45f4f5SPeter Maydell         static const uint32_t mode_for_el[] = {
6453a45f4f5SPeter Maydell             0,
6463a45f4f5SPeter Maydell             ARM_CPU_MODE_SVC,
6473a45f4f5SPeter Maydell             ARM_CPU_MODE_HYP,
6483a45f4f5SPeter Maydell             ARM_CPU_MODE_SVC,
6493a45f4f5SPeter Maydell         };
6503a45f4f5SPeter Maydell 
6513a45f4f5SPeter Maydell         cpsr_write(env, mode_for_el[target_el], CPSR_M, CPSRWriteRaw);
6523a45f4f5SPeter Maydell     }
6533a45f4f5SPeter Maydell }
6543a45f4f5SPeter Maydell 
6553a45f4f5SPeter Maydell 
6569e406eeaSPhilippe Mathieu-Daudé #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
657083afd18SPhilippe Mathieu-Daudé 
658310cedf3SRichard Henderson static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx,
659be879556SRichard Henderson                                      unsigned int target_el,
660be879556SRichard Henderson                                      unsigned int cur_el, bool secure,
661be879556SRichard Henderson                                      uint64_t hcr_el2)
662310cedf3SRichard Henderson {
663b77af26eSRichard Henderson     CPUARMState *env = cpu_env(cs);
664310cedf3SRichard Henderson     bool pstate_unmasked;
66516e07f78SRichard Henderson     bool unmasked = false;
666310cedf3SRichard Henderson 
667310cedf3SRichard Henderson     /*
668310cedf3SRichard Henderson      * Don't take exceptions if they target a lower EL.
669310cedf3SRichard Henderson      * This check should catch any exceptions that would not be taken
670310cedf3SRichard Henderson      * but left pending.
671310cedf3SRichard Henderson      */
672310cedf3SRichard Henderson     if (cur_el > target_el) {
673310cedf3SRichard Henderson         return false;
674310cedf3SRichard Henderson     }
675310cedf3SRichard Henderson 
676310cedf3SRichard Henderson     switch (excp_idx) {
677310cedf3SRichard Henderson     case EXCP_FIQ:
678310cedf3SRichard Henderson         pstate_unmasked = !(env->daif & PSTATE_F);
679310cedf3SRichard Henderson         break;
680310cedf3SRichard Henderson 
681310cedf3SRichard Henderson     case EXCP_IRQ:
682310cedf3SRichard Henderson         pstate_unmasked = !(env->daif & PSTATE_I);
683310cedf3SRichard Henderson         break;
684310cedf3SRichard Henderson 
685310cedf3SRichard Henderson     case EXCP_VFIQ:
686cc974d5cSRémi Denis-Courmont         if (!(hcr_el2 & HCR_FMO) || (hcr_el2 & HCR_TGE)) {
687cc974d5cSRémi Denis-Courmont             /* VFIQs are only taken when hypervized.  */
688310cedf3SRichard Henderson             return false;
689310cedf3SRichard Henderson         }
690310cedf3SRichard Henderson         return !(env->daif & PSTATE_F);
691310cedf3SRichard Henderson     case EXCP_VIRQ:
692cc974d5cSRémi Denis-Courmont         if (!(hcr_el2 & HCR_IMO) || (hcr_el2 & HCR_TGE)) {
693cc974d5cSRémi Denis-Courmont             /* VIRQs are only taken when hypervized.  */
694310cedf3SRichard Henderson             return false;
695310cedf3SRichard Henderson         }
696310cedf3SRichard Henderson         return !(env->daif & PSTATE_I);
6973c29632fSRichard Henderson     case EXCP_VSERR:
6983c29632fSRichard Henderson         if (!(hcr_el2 & HCR_AMO) || (hcr_el2 & HCR_TGE)) {
6993c29632fSRichard Henderson             /* VIRQs are only taken when hypervized.  */
7003c29632fSRichard Henderson             return false;
7013c29632fSRichard Henderson         }
7023c29632fSRichard Henderson         return !(env->daif & PSTATE_A);
703310cedf3SRichard Henderson     default:
704310cedf3SRichard Henderson         g_assert_not_reached();
705310cedf3SRichard Henderson     }
706310cedf3SRichard Henderson 
707310cedf3SRichard Henderson     /*
708310cedf3SRichard Henderson      * Use the target EL, current execution state and SCR/HCR settings to
709310cedf3SRichard Henderson      * determine whether the corresponding CPSR bit is used to mask the
710310cedf3SRichard Henderson      * interrupt.
711310cedf3SRichard Henderson      */
712310cedf3SRichard Henderson     if ((target_el > cur_el) && (target_el != 1)) {
713310cedf3SRichard Henderson         /* Exceptions targeting a higher EL may not be maskable */
714310cedf3SRichard Henderson         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
715c939a7c7SAke Koomsin             switch (target_el) {
716c939a7c7SAke Koomsin             case 2:
717310cedf3SRichard Henderson                 /*
718c939a7c7SAke Koomsin                  * According to ARM DDI 0487H.a, an interrupt can be masked
719c939a7c7SAke Koomsin                  * when HCR_E2H and HCR_TGE are both set regardless of the
720c939a7c7SAke Koomsin                  * current Security state. Note that we need to revisit this
721c939a7c7SAke Koomsin                  * part again once we need to support NMI.
722310cedf3SRichard Henderson                  */
723c939a7c7SAke Koomsin                 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
72416e07f78SRichard Henderson                         unmasked = true;
725310cedf3SRichard Henderson                 }
726c939a7c7SAke Koomsin                 break;
727c939a7c7SAke Koomsin             case 3:
728c939a7c7SAke Koomsin                 /* Interrupt cannot be masked when the target EL is 3 */
729c939a7c7SAke Koomsin                 unmasked = true;
730c939a7c7SAke Koomsin                 break;
731c939a7c7SAke Koomsin             default:
732c939a7c7SAke Koomsin                 g_assert_not_reached();
733c939a7c7SAke Koomsin             }
734310cedf3SRichard Henderson         } else {
735310cedf3SRichard Henderson             /*
736310cedf3SRichard Henderson              * The old 32-bit-only environment has a more complicated
737310cedf3SRichard Henderson              * masking setup. HCR and SCR bits not only affect interrupt
738310cedf3SRichard Henderson              * routing but also change the behaviour of masking.
739310cedf3SRichard Henderson              */
740310cedf3SRichard Henderson             bool hcr, scr;
741310cedf3SRichard Henderson 
742310cedf3SRichard Henderson             switch (excp_idx) {
743310cedf3SRichard Henderson             case EXCP_FIQ:
744310cedf3SRichard Henderson                 /*
745310cedf3SRichard Henderson                  * If FIQs are routed to EL3 or EL2 then there are cases where
746310cedf3SRichard Henderson                  * we override the CPSR.F in determining if the exception is
747310cedf3SRichard Henderson                  * masked or not. If neither of these are set then we fall back
748310cedf3SRichard Henderson                  * to the CPSR.F setting otherwise we further assess the state
749310cedf3SRichard Henderson                  * below.
750310cedf3SRichard Henderson                  */
751310cedf3SRichard Henderson                 hcr = hcr_el2 & HCR_FMO;
752310cedf3SRichard Henderson                 scr = (env->cp15.scr_el3 & SCR_FIQ);
753310cedf3SRichard Henderson 
754310cedf3SRichard Henderson                 /*
755310cedf3SRichard Henderson                  * When EL3 is 32-bit, the SCR.FW bit controls whether the
756310cedf3SRichard Henderson                  * CPSR.F bit masks FIQ interrupts when taken in non-secure
757310cedf3SRichard Henderson                  * state. If SCR.FW is set then FIQs can be masked by CPSR.F
758310cedf3SRichard Henderson                  * when non-secure but only when FIQs are only routed to EL3.
759310cedf3SRichard Henderson                  */
760310cedf3SRichard Henderson                 scr = scr && !((env->cp15.scr_el3 & SCR_FW) && !hcr);
761310cedf3SRichard Henderson                 break;
762310cedf3SRichard Henderson             case EXCP_IRQ:
763310cedf3SRichard Henderson                 /*
764310cedf3SRichard Henderson                  * When EL3 execution state is 32-bit, if HCR.IMO is set then
765310cedf3SRichard Henderson                  * we may override the CPSR.I masking when in non-secure state.
766310cedf3SRichard Henderson                  * The SCR.IRQ setting has already been taken into consideration
767310cedf3SRichard Henderson                  * when setting the target EL, so it does not have a further
768310cedf3SRichard Henderson                  * affect here.
769310cedf3SRichard Henderson                  */
770310cedf3SRichard Henderson                 hcr = hcr_el2 & HCR_IMO;
771310cedf3SRichard Henderson                 scr = false;
772310cedf3SRichard Henderson                 break;
773310cedf3SRichard Henderson             default:
774310cedf3SRichard Henderson                 g_assert_not_reached();
775310cedf3SRichard Henderson             }
776310cedf3SRichard Henderson 
777310cedf3SRichard Henderson             if ((scr || hcr) && !secure) {
77816e07f78SRichard Henderson                 unmasked = true;
779310cedf3SRichard Henderson             }
780310cedf3SRichard Henderson         }
781310cedf3SRichard Henderson     }
782310cedf3SRichard Henderson 
783310cedf3SRichard Henderson     /*
784673d8215SMichael Tokarev      * The PSTATE bits only mask the interrupt if we have not overridden the
785310cedf3SRichard Henderson      * ability above.
786310cedf3SRichard Henderson      */
787310cedf3SRichard Henderson     return unmasked || pstate_unmasked;
788310cedf3SRichard Henderson }
789310cedf3SRichard Henderson 
790083afd18SPhilippe Mathieu-Daudé static bool arm_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
791fcf5ef2aSThomas Huth {
792fcf5ef2aSThomas Huth     CPUClass *cc = CPU_GET_CLASS(cs);
793b77af26eSRichard Henderson     CPUARMState *env = cpu_env(cs);
794fcf5ef2aSThomas Huth     uint32_t cur_el = arm_current_el(env);
795fcf5ef2aSThomas Huth     bool secure = arm_is_secure(env);
796be879556SRichard Henderson     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
797fcf5ef2aSThomas Huth     uint32_t target_el;
798fcf5ef2aSThomas Huth     uint32_t excp_idx;
799d63d0ec5SRichard Henderson 
800d63d0ec5SRichard Henderson     /* The prioritization of interrupts is IMPLEMENTATION DEFINED. */
801fcf5ef2aSThomas Huth 
802fcf5ef2aSThomas Huth     if (interrupt_request & CPU_INTERRUPT_FIQ) {
803fcf5ef2aSThomas Huth         excp_idx = EXCP_FIQ;
804fcf5ef2aSThomas Huth         target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
805be879556SRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
806be879556SRichard Henderson                               cur_el, secure, hcr_el2)) {
807d63d0ec5SRichard Henderson             goto found;
808fcf5ef2aSThomas Huth         }
809fcf5ef2aSThomas Huth     }
810fcf5ef2aSThomas Huth     if (interrupt_request & CPU_INTERRUPT_HARD) {
811fcf5ef2aSThomas Huth         excp_idx = EXCP_IRQ;
812fcf5ef2aSThomas Huth         target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
813be879556SRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
814be879556SRichard Henderson                               cur_el, secure, hcr_el2)) {
815d63d0ec5SRichard Henderson             goto found;
816fcf5ef2aSThomas Huth         }
817fcf5ef2aSThomas Huth     }
818fcf5ef2aSThomas Huth     if (interrupt_request & CPU_INTERRUPT_VIRQ) {
819fcf5ef2aSThomas Huth         excp_idx = EXCP_VIRQ;
820fcf5ef2aSThomas Huth         target_el = 1;
821be879556SRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
822be879556SRichard Henderson                               cur_el, secure, hcr_el2)) {
823d63d0ec5SRichard Henderson             goto found;
824fcf5ef2aSThomas Huth         }
825fcf5ef2aSThomas Huth     }
826fcf5ef2aSThomas Huth     if (interrupt_request & CPU_INTERRUPT_VFIQ) {
827fcf5ef2aSThomas Huth         excp_idx = EXCP_VFIQ;
828fcf5ef2aSThomas Huth         target_el = 1;
829be879556SRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
830be879556SRichard Henderson                               cur_el, secure, hcr_el2)) {
831d63d0ec5SRichard Henderson             goto found;
832d63d0ec5SRichard Henderson         }
833d63d0ec5SRichard Henderson     }
8343c29632fSRichard Henderson     if (interrupt_request & CPU_INTERRUPT_VSERR) {
8353c29632fSRichard Henderson         excp_idx = EXCP_VSERR;
8363c29632fSRichard Henderson         target_el = 1;
8373c29632fSRichard Henderson         if (arm_excp_unmasked(cs, excp_idx, target_el,
8383c29632fSRichard Henderson                               cur_el, secure, hcr_el2)) {
8393c29632fSRichard Henderson             /* Taking a virtual abort clears HCR_EL2.VSE */
8403c29632fSRichard Henderson             env->cp15.hcr_el2 &= ~HCR_VSE;
8413c29632fSRichard Henderson             cpu_reset_interrupt(cs, CPU_INTERRUPT_VSERR);
8423c29632fSRichard Henderson             goto found;
8433c29632fSRichard Henderson         }
8443c29632fSRichard Henderson     }
845d63d0ec5SRichard Henderson     return false;
846d63d0ec5SRichard Henderson 
847d63d0ec5SRichard Henderson  found:
848fcf5ef2aSThomas Huth     cs->exception_index = excp_idx;
849fcf5ef2aSThomas Huth     env->exception.target_el = target_el;
85078271684SClaudio Fontana     cc->tcg_ops->do_interrupt(cs);
851d63d0ec5SRichard Henderson     return true;
852fcf5ef2aSThomas Huth }
8539e406eeaSPhilippe Mathieu-Daudé 
8549e406eeaSPhilippe Mathieu-Daudé #endif /* CONFIG_TCG && !CONFIG_USER_ONLY */
855fcf5ef2aSThomas Huth 
85689430fc6SPeter Maydell void arm_cpu_update_virq(ARMCPU *cpu)
85789430fc6SPeter Maydell {
85889430fc6SPeter Maydell     /*
85989430fc6SPeter Maydell      * Update the interrupt level for VIRQ, which is the logical OR of
86089430fc6SPeter Maydell      * the HCR_EL2.VI bit and the input line level from the GIC.
86189430fc6SPeter Maydell      */
86289430fc6SPeter Maydell     CPUARMState *env = &cpu->env;
86389430fc6SPeter Maydell     CPUState *cs = CPU(cpu);
86489430fc6SPeter Maydell 
86589430fc6SPeter Maydell     bool new_state = (env->cp15.hcr_el2 & HCR_VI) ||
86689430fc6SPeter Maydell         (env->irq_line_state & CPU_INTERRUPT_VIRQ);
86789430fc6SPeter Maydell 
86889430fc6SPeter Maydell     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VIRQ) != 0)) {
86989430fc6SPeter Maydell         if (new_state) {
87089430fc6SPeter Maydell             cpu_interrupt(cs, CPU_INTERRUPT_VIRQ);
87189430fc6SPeter Maydell         } else {
87289430fc6SPeter Maydell             cpu_reset_interrupt(cs, CPU_INTERRUPT_VIRQ);
87389430fc6SPeter Maydell         }
87489430fc6SPeter Maydell     }
87589430fc6SPeter Maydell }
87689430fc6SPeter Maydell 
87789430fc6SPeter Maydell void arm_cpu_update_vfiq(ARMCPU *cpu)
87889430fc6SPeter Maydell {
87989430fc6SPeter Maydell     /*
88089430fc6SPeter Maydell      * Update the interrupt level for VFIQ, which is the logical OR of
88189430fc6SPeter Maydell      * the HCR_EL2.VF bit and the input line level from the GIC.
88289430fc6SPeter Maydell      */
88389430fc6SPeter Maydell     CPUARMState *env = &cpu->env;
88489430fc6SPeter Maydell     CPUState *cs = CPU(cpu);
88589430fc6SPeter Maydell 
88689430fc6SPeter Maydell     bool new_state = (env->cp15.hcr_el2 & HCR_VF) ||
88789430fc6SPeter Maydell         (env->irq_line_state & CPU_INTERRUPT_VFIQ);
88889430fc6SPeter Maydell 
88989430fc6SPeter Maydell     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VFIQ) != 0)) {
89089430fc6SPeter Maydell         if (new_state) {
89189430fc6SPeter Maydell             cpu_interrupt(cs, CPU_INTERRUPT_VFIQ);
89289430fc6SPeter Maydell         } else {
89389430fc6SPeter Maydell             cpu_reset_interrupt(cs, CPU_INTERRUPT_VFIQ);
89489430fc6SPeter Maydell         }
89589430fc6SPeter Maydell     }
89689430fc6SPeter Maydell }
89789430fc6SPeter Maydell 
8983c29632fSRichard Henderson void arm_cpu_update_vserr(ARMCPU *cpu)
8993c29632fSRichard Henderson {
9003c29632fSRichard Henderson     /*
9013c29632fSRichard Henderson      * Update the interrupt level for VSERR, which is the HCR_EL2.VSE bit.
9023c29632fSRichard Henderson      */
9033c29632fSRichard Henderson     CPUARMState *env = &cpu->env;
9043c29632fSRichard Henderson     CPUState *cs = CPU(cpu);
9053c29632fSRichard Henderson 
9063c29632fSRichard Henderson     bool new_state = env->cp15.hcr_el2 & HCR_VSE;
9073c29632fSRichard Henderson 
9083c29632fSRichard Henderson     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VSERR) != 0)) {
9093c29632fSRichard Henderson         if (new_state) {
9103c29632fSRichard Henderson             cpu_interrupt(cs, CPU_INTERRUPT_VSERR);
9113c29632fSRichard Henderson         } else {
9123c29632fSRichard Henderson             cpu_reset_interrupt(cs, CPU_INTERRUPT_VSERR);
9133c29632fSRichard Henderson         }
9143c29632fSRichard Henderson     }
9153c29632fSRichard Henderson }
9163c29632fSRichard Henderson 
917fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
918fcf5ef2aSThomas Huth static void arm_cpu_set_irq(void *opaque, int irq, int level)
919fcf5ef2aSThomas Huth {
920fcf5ef2aSThomas Huth     ARMCPU *cpu = opaque;
921fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
922fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
923fcf5ef2aSThomas Huth     static const int mask[] = {
924fcf5ef2aSThomas Huth         [ARM_CPU_IRQ] = CPU_INTERRUPT_HARD,
925fcf5ef2aSThomas Huth         [ARM_CPU_FIQ] = CPU_INTERRUPT_FIQ,
926fcf5ef2aSThomas Huth         [ARM_CPU_VIRQ] = CPU_INTERRUPT_VIRQ,
927fcf5ef2aSThomas Huth         [ARM_CPU_VFIQ] = CPU_INTERRUPT_VFIQ
928fcf5ef2aSThomas Huth     };
929fcf5ef2aSThomas Huth 
9309acd2d33SPeter Maydell     if (!arm_feature(env, ARM_FEATURE_EL2) &&
9319acd2d33SPeter Maydell         (irq == ARM_CPU_VIRQ || irq == ARM_CPU_VFIQ)) {
9329acd2d33SPeter Maydell         /*
9339acd2d33SPeter Maydell          * The GIC might tell us about VIRQ and VFIQ state, but if we don't
9349acd2d33SPeter Maydell          * have EL2 support we don't care. (Unless the guest is doing something
9359acd2d33SPeter Maydell          * silly this will only be calls saying "level is still 0".)
9369acd2d33SPeter Maydell          */
9379acd2d33SPeter Maydell         return;
9389acd2d33SPeter Maydell     }
9399acd2d33SPeter Maydell 
940ed89f078SPeter Maydell     if (level) {
941ed89f078SPeter Maydell         env->irq_line_state |= mask[irq];
942ed89f078SPeter Maydell     } else {
943ed89f078SPeter Maydell         env->irq_line_state &= ~mask[irq];
944ed89f078SPeter Maydell     }
945ed89f078SPeter Maydell 
946fcf5ef2aSThomas Huth     switch (irq) {
947fcf5ef2aSThomas Huth     case ARM_CPU_VIRQ:
94889430fc6SPeter Maydell         arm_cpu_update_virq(cpu);
94989430fc6SPeter Maydell         break;
950fcf5ef2aSThomas Huth     case ARM_CPU_VFIQ:
95189430fc6SPeter Maydell         arm_cpu_update_vfiq(cpu);
95289430fc6SPeter Maydell         break;
953fcf5ef2aSThomas Huth     case ARM_CPU_IRQ:
954fcf5ef2aSThomas Huth     case ARM_CPU_FIQ:
955fcf5ef2aSThomas Huth         if (level) {
956fcf5ef2aSThomas Huth             cpu_interrupt(cs, mask[irq]);
957fcf5ef2aSThomas Huth         } else {
958fcf5ef2aSThomas Huth             cpu_reset_interrupt(cs, mask[irq]);
959fcf5ef2aSThomas Huth         }
960fcf5ef2aSThomas Huth         break;
961fcf5ef2aSThomas Huth     default:
962fcf5ef2aSThomas Huth         g_assert_not_reached();
963fcf5ef2aSThomas Huth     }
964fcf5ef2aSThomas Huth }
965fcf5ef2aSThomas Huth 
966fcf5ef2aSThomas Huth static void arm_cpu_kvm_set_irq(void *opaque, int irq, int level)
967fcf5ef2aSThomas Huth {
968fcf5ef2aSThomas Huth #ifdef CONFIG_KVM
969fcf5ef2aSThomas Huth     ARMCPU *cpu = opaque;
970ed89f078SPeter Maydell     CPUARMState *env = &cpu->env;
971fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
972ed89f078SPeter Maydell     uint32_t linestate_bit;
973f6530926SEric Auger     int irq_id;
974fcf5ef2aSThomas Huth 
975fcf5ef2aSThomas Huth     switch (irq) {
976fcf5ef2aSThomas Huth     case ARM_CPU_IRQ:
977f6530926SEric Auger         irq_id = KVM_ARM_IRQ_CPU_IRQ;
978ed89f078SPeter Maydell         linestate_bit = CPU_INTERRUPT_HARD;
979fcf5ef2aSThomas Huth         break;
980fcf5ef2aSThomas Huth     case ARM_CPU_FIQ:
981f6530926SEric Auger         irq_id = KVM_ARM_IRQ_CPU_FIQ;
982ed89f078SPeter Maydell         linestate_bit = CPU_INTERRUPT_FIQ;
983fcf5ef2aSThomas Huth         break;
984fcf5ef2aSThomas Huth     default:
985fcf5ef2aSThomas Huth         g_assert_not_reached();
986fcf5ef2aSThomas Huth     }
987ed89f078SPeter Maydell 
988ed89f078SPeter Maydell     if (level) {
989ed89f078SPeter Maydell         env->irq_line_state |= linestate_bit;
990ed89f078SPeter Maydell     } else {
991ed89f078SPeter Maydell         env->irq_line_state &= ~linestate_bit;
992ed89f078SPeter Maydell     }
993f6530926SEric Auger     kvm_arm_set_irq(cs->cpu_index, KVM_ARM_IRQ_TYPE_CPU, irq_id, !!level);
994fcf5ef2aSThomas Huth #endif
995fcf5ef2aSThomas Huth }
996fcf5ef2aSThomas Huth 
997fcf5ef2aSThomas Huth static bool arm_cpu_virtio_is_big_endian(CPUState *cs)
998fcf5ef2aSThomas Huth {
999fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(cs);
1000fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
1001fcf5ef2aSThomas Huth 
1002fcf5ef2aSThomas Huth     cpu_synchronize_state(cs);
1003fcf5ef2aSThomas Huth     return arm_cpu_data_is_big_endian(env);
1004fcf5ef2aSThomas Huth }
1005fcf5ef2aSThomas Huth 
1006fcf5ef2aSThomas Huth #endif
1007fcf5ef2aSThomas Huth 
1008fcf5ef2aSThomas Huth static void arm_disas_set_info(CPUState *cpu, disassemble_info *info)
1009fcf5ef2aSThomas Huth {
1010fcf5ef2aSThomas Huth     ARMCPU *ac = ARM_CPU(cpu);
1011fcf5ef2aSThomas Huth     CPUARMState *env = &ac->env;
10127bcdbf51SRichard Henderson     bool sctlr_b;
1013fcf5ef2aSThomas Huth 
1014fcf5ef2aSThomas Huth     if (is_a64(env)) {
1015110f6c70SRichard Henderson         info->cap_arch = CS_ARCH_ARM64;
101615fa1a0aSRichard Henderson         info->cap_insn_unit = 4;
101715fa1a0aSRichard Henderson         info->cap_insn_split = 4;
1018110f6c70SRichard Henderson     } else {
1019110f6c70SRichard Henderson         int cap_mode;
1020110f6c70SRichard Henderson         if (env->thumb) {
102115fa1a0aSRichard Henderson             info->cap_insn_unit = 2;
102215fa1a0aSRichard Henderson             info->cap_insn_split = 4;
1023110f6c70SRichard Henderson             cap_mode = CS_MODE_THUMB;
1024fcf5ef2aSThomas Huth         } else {
102515fa1a0aSRichard Henderson             info->cap_insn_unit = 4;
102615fa1a0aSRichard Henderson             info->cap_insn_split = 4;
1027110f6c70SRichard Henderson             cap_mode = CS_MODE_ARM;
1028fcf5ef2aSThomas Huth         }
1029110f6c70SRichard Henderson         if (arm_feature(env, ARM_FEATURE_V8)) {
1030110f6c70SRichard Henderson             cap_mode |= CS_MODE_V8;
1031110f6c70SRichard Henderson         }
1032110f6c70SRichard Henderson         if (arm_feature(env, ARM_FEATURE_M)) {
1033110f6c70SRichard Henderson             cap_mode |= CS_MODE_MCLASS;
1034110f6c70SRichard Henderson         }
1035110f6c70SRichard Henderson         info->cap_arch = CS_ARCH_ARM;
1036110f6c70SRichard Henderson         info->cap_mode = cap_mode;
1037fcf5ef2aSThomas Huth     }
10387bcdbf51SRichard Henderson 
10397bcdbf51SRichard Henderson     sctlr_b = arm_sctlr_b(env);
10407bcdbf51SRichard Henderson     if (bswap_code(sctlr_b)) {
1041ee3eb3a7SMarc-André Lureau #if TARGET_BIG_ENDIAN
1042fcf5ef2aSThomas Huth         info->endian = BFD_ENDIAN_LITTLE;
1043fcf5ef2aSThomas Huth #else
1044fcf5ef2aSThomas Huth         info->endian = BFD_ENDIAN_BIG;
1045fcf5ef2aSThomas Huth #endif
1046fcf5ef2aSThomas Huth     }
1047f7478a92SJulian Brown     info->flags &= ~INSN_ARM_BE32;
10487bcdbf51SRichard Henderson #ifndef CONFIG_USER_ONLY
10497bcdbf51SRichard Henderson     if (sctlr_b) {
1050f7478a92SJulian Brown         info->flags |= INSN_ARM_BE32;
1051f7478a92SJulian Brown     }
10527bcdbf51SRichard Henderson #endif
1053fcf5ef2aSThomas Huth }
1054fcf5ef2aSThomas Huth 
105586480615SPhilippe Mathieu-Daudé #ifdef TARGET_AARCH64
105686480615SPhilippe Mathieu-Daudé 
105786480615SPhilippe Mathieu-Daudé static void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags)
105886480615SPhilippe Mathieu-Daudé {
105986480615SPhilippe Mathieu-Daudé     ARMCPU *cpu = ARM_CPU(cs);
106086480615SPhilippe Mathieu-Daudé     CPUARMState *env = &cpu->env;
106186480615SPhilippe Mathieu-Daudé     uint32_t psr = pstate_read(env);
1062a9d84070SRichard Henderson     int i, j;
106386480615SPhilippe Mathieu-Daudé     int el = arm_current_el(env);
1064bde0e60bSPeter Maydell     uint64_t hcr = arm_hcr_el2_eff(env);
106586480615SPhilippe Mathieu-Daudé     const char *ns_status;
10667a867dd5SRichard Henderson     bool sve;
106786480615SPhilippe Mathieu-Daudé 
106886480615SPhilippe Mathieu-Daudé     qemu_fprintf(f, " PC=%016" PRIx64 " ", env->pc);
106986480615SPhilippe Mathieu-Daudé     for (i = 0; i < 32; i++) {
107086480615SPhilippe Mathieu-Daudé         if (i == 31) {
107186480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, " SP=%016" PRIx64 "\n", env->xregs[i]);
107286480615SPhilippe Mathieu-Daudé         } else {
107386480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "X%02d=%016" PRIx64 "%s", i, env->xregs[i],
107486480615SPhilippe Mathieu-Daudé                          (i + 2) % 3 ? " " : "\n");
107586480615SPhilippe Mathieu-Daudé         }
107686480615SPhilippe Mathieu-Daudé     }
107786480615SPhilippe Mathieu-Daudé 
107886480615SPhilippe Mathieu-Daudé     if (arm_feature(env, ARM_FEATURE_EL3) && el != 3) {
107986480615SPhilippe Mathieu-Daudé         ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
108086480615SPhilippe Mathieu-Daudé     } else {
108186480615SPhilippe Mathieu-Daudé         ns_status = "";
108286480615SPhilippe Mathieu-Daudé     }
108386480615SPhilippe Mathieu-Daudé     qemu_fprintf(f, "PSTATE=%08x %c%c%c%c %sEL%d%c",
108486480615SPhilippe Mathieu-Daudé                  psr,
108586480615SPhilippe Mathieu-Daudé                  psr & PSTATE_N ? 'N' : '-',
108686480615SPhilippe Mathieu-Daudé                  psr & PSTATE_Z ? 'Z' : '-',
108786480615SPhilippe Mathieu-Daudé                  psr & PSTATE_C ? 'C' : '-',
108886480615SPhilippe Mathieu-Daudé                  psr & PSTATE_V ? 'V' : '-',
108986480615SPhilippe Mathieu-Daudé                  ns_status,
109086480615SPhilippe Mathieu-Daudé                  el,
109186480615SPhilippe Mathieu-Daudé                  psr & PSTATE_SP ? 'h' : 't');
109286480615SPhilippe Mathieu-Daudé 
10937a867dd5SRichard Henderson     if (cpu_isar_feature(aa64_sme, cpu)) {
10947a867dd5SRichard Henderson         qemu_fprintf(f, "  SVCR=%08" PRIx64 " %c%c",
10957a867dd5SRichard Henderson                      env->svcr,
10967a867dd5SRichard Henderson                      (FIELD_EX64(env->svcr, SVCR, ZA) ? 'Z' : '-'),
10977a867dd5SRichard Henderson                      (FIELD_EX64(env->svcr, SVCR, SM) ? 'S' : '-'));
10987a867dd5SRichard Henderson     }
109986480615SPhilippe Mathieu-Daudé     if (cpu_isar_feature(aa64_bti, cpu)) {
110086480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "  BTYPE=%d", (psr & PSTATE_BTYPE) >> 10);
110186480615SPhilippe Mathieu-Daudé     }
1102bde0e60bSPeter Maydell     qemu_fprintf(f, "%s%s%s",
1103bde0e60bSPeter Maydell                  (hcr & HCR_NV) ? " NV" : "",
1104bde0e60bSPeter Maydell                  (hcr & HCR_NV1) ? " NV1" : "",
1105bde0e60bSPeter Maydell                  (hcr & HCR_NV2) ? " NV2" : "");
110686480615SPhilippe Mathieu-Daudé     if (!(flags & CPU_DUMP_FPU)) {
110786480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "\n");
110886480615SPhilippe Mathieu-Daudé         return;
110986480615SPhilippe Mathieu-Daudé     }
111086480615SPhilippe Mathieu-Daudé     if (fp_exception_el(env, el) != 0) {
111186480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "    FPU disabled\n");
111286480615SPhilippe Mathieu-Daudé         return;
111386480615SPhilippe Mathieu-Daudé     }
111486480615SPhilippe Mathieu-Daudé     qemu_fprintf(f, "     FPCR=%08x FPSR=%08x\n",
111586480615SPhilippe Mathieu-Daudé                  vfp_get_fpcr(env), vfp_get_fpsr(env));
111686480615SPhilippe Mathieu-Daudé 
11177a867dd5SRichard Henderson     if (cpu_isar_feature(aa64_sme, cpu) && FIELD_EX64(env->svcr, SVCR, SM)) {
11187a867dd5SRichard Henderson         sve = sme_exception_el(env, el) == 0;
11197a867dd5SRichard Henderson     } else if (cpu_isar_feature(aa64_sve, cpu)) {
11207a867dd5SRichard Henderson         sve = sve_exception_el(env, el) == 0;
11217a867dd5SRichard Henderson     } else {
11227a867dd5SRichard Henderson         sve = false;
11237a867dd5SRichard Henderson     }
11247a867dd5SRichard Henderson 
11257a867dd5SRichard Henderson     if (sve) {
1126a9d84070SRichard Henderson         int zcr_len = sve_vqm1_for_el(env, el);
112786480615SPhilippe Mathieu-Daudé 
112886480615SPhilippe Mathieu-Daudé         for (i = 0; i <= FFR_PRED_NUM; i++) {
112986480615SPhilippe Mathieu-Daudé             bool eol;
113086480615SPhilippe Mathieu-Daudé             if (i == FFR_PRED_NUM) {
113186480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "FFR=");
113286480615SPhilippe Mathieu-Daudé                 /* It's last, so end the line.  */
113386480615SPhilippe Mathieu-Daudé                 eol = true;
113486480615SPhilippe Mathieu-Daudé             } else {
113586480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "P%02d=", i);
113686480615SPhilippe Mathieu-Daudé                 switch (zcr_len) {
113786480615SPhilippe Mathieu-Daudé                 case 0:
113886480615SPhilippe Mathieu-Daudé                     eol = i % 8 == 7;
113986480615SPhilippe Mathieu-Daudé                     break;
114086480615SPhilippe Mathieu-Daudé                 case 1:
114186480615SPhilippe Mathieu-Daudé                     eol = i % 6 == 5;
114286480615SPhilippe Mathieu-Daudé                     break;
114386480615SPhilippe Mathieu-Daudé                 case 2:
114486480615SPhilippe Mathieu-Daudé                 case 3:
114586480615SPhilippe Mathieu-Daudé                     eol = i % 3 == 2;
114686480615SPhilippe Mathieu-Daudé                     break;
114786480615SPhilippe Mathieu-Daudé                 default:
114886480615SPhilippe Mathieu-Daudé                     /* More than one quadword per predicate.  */
114986480615SPhilippe Mathieu-Daudé                     eol = true;
115086480615SPhilippe Mathieu-Daudé                     break;
115186480615SPhilippe Mathieu-Daudé                 }
115286480615SPhilippe Mathieu-Daudé             }
115386480615SPhilippe Mathieu-Daudé             for (j = zcr_len / 4; j >= 0; j--) {
115486480615SPhilippe Mathieu-Daudé                 int digits;
115586480615SPhilippe Mathieu-Daudé                 if (j * 4 + 4 <= zcr_len + 1) {
115686480615SPhilippe Mathieu-Daudé                     digits = 16;
115786480615SPhilippe Mathieu-Daudé                 } else {
115886480615SPhilippe Mathieu-Daudé                     digits = (zcr_len % 4 + 1) * 4;
115986480615SPhilippe Mathieu-Daudé                 }
116086480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "%0*" PRIx64 "%s", digits,
116186480615SPhilippe Mathieu-Daudé                              env->vfp.pregs[i].p[j],
116286480615SPhilippe Mathieu-Daudé                              j ? ":" : eol ? "\n" : " ");
116386480615SPhilippe Mathieu-Daudé             }
116486480615SPhilippe Mathieu-Daudé         }
116586480615SPhilippe Mathieu-Daudé 
116686480615SPhilippe Mathieu-Daudé         if (zcr_len == 0) {
1167a9d84070SRichard Henderson             /*
1168a9d84070SRichard Henderson              * With vl=16, there are only 37 columns per register,
1169a9d84070SRichard Henderson              * so output two registers per line.
1170a9d84070SRichard Henderson              */
1171a9d84070SRichard Henderson             for (i = 0; i < 32; i++) {
117286480615SPhilippe Mathieu-Daudé                 qemu_fprintf(f, "Z%02d=%016" PRIx64 ":%016" PRIx64 "%s",
117386480615SPhilippe Mathieu-Daudé                              i, env->vfp.zregs[i].d[1],
117486480615SPhilippe Mathieu-Daudé                              env->vfp.zregs[i].d[0], i & 1 ? "\n" : " ");
1175a9d84070SRichard Henderson             }
117686480615SPhilippe Mathieu-Daudé         } else {
1177a9d84070SRichard Henderson             for (i = 0; i < 32; i++) {
1178a9d84070SRichard Henderson                 qemu_fprintf(f, "Z%02d=", i);
117986480615SPhilippe Mathieu-Daudé                 for (j = zcr_len; j >= 0; j--) {
118086480615SPhilippe Mathieu-Daudé                     qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%s",
118186480615SPhilippe Mathieu-Daudé                                  env->vfp.zregs[i].d[j * 2 + 1],
1182a9d84070SRichard Henderson                                  env->vfp.zregs[i].d[j * 2 + 0],
1183a9d84070SRichard Henderson                                  j ? ":" : "\n");
118486480615SPhilippe Mathieu-Daudé                 }
118586480615SPhilippe Mathieu-Daudé             }
118686480615SPhilippe Mathieu-Daudé         }
118786480615SPhilippe Mathieu-Daudé     } else {
118886480615SPhilippe Mathieu-Daudé         for (i = 0; i < 32; i++) {
118986480615SPhilippe Mathieu-Daudé             uint64_t *q = aa64_vfp_qreg(env, i);
119086480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "Q%02d=%016" PRIx64 ":%016" PRIx64 "%s",
119186480615SPhilippe Mathieu-Daudé                          i, q[1], q[0], (i & 1 ? "\n" : " "));
119286480615SPhilippe Mathieu-Daudé         }
119386480615SPhilippe Mathieu-Daudé     }
1194270bea47SRichard Henderson 
1195270bea47SRichard Henderson     if (cpu_isar_feature(aa64_sme, cpu) &&
1196270bea47SRichard Henderson         FIELD_EX64(env->svcr, SVCR, ZA) &&
1197270bea47SRichard Henderson         sme_exception_el(env, el) == 0) {
1198270bea47SRichard Henderson         int zcr_len = sve_vqm1_for_el_sm(env, el, true);
1199270bea47SRichard Henderson         int svl = (zcr_len + 1) * 16;
1200270bea47SRichard Henderson         int svl_lg10 = svl < 100 ? 2 : 3;
1201270bea47SRichard Henderson 
1202270bea47SRichard Henderson         for (i = 0; i < svl; i++) {
1203270bea47SRichard Henderson             qemu_fprintf(f, "ZA[%0*d]=", svl_lg10, i);
1204270bea47SRichard Henderson             for (j = zcr_len; j >= 0; --j) {
1205270bea47SRichard Henderson                 qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%c",
1206270bea47SRichard Henderson                              env->zarray[i].d[2 * j + 1],
1207270bea47SRichard Henderson                              env->zarray[i].d[2 * j],
1208270bea47SRichard Henderson                              j ? ':' : '\n');
1209270bea47SRichard Henderson             }
1210270bea47SRichard Henderson         }
1211270bea47SRichard Henderson     }
121286480615SPhilippe Mathieu-Daudé }
121386480615SPhilippe Mathieu-Daudé 
121486480615SPhilippe Mathieu-Daudé #else
121586480615SPhilippe Mathieu-Daudé 
121686480615SPhilippe Mathieu-Daudé static inline void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags)
121786480615SPhilippe Mathieu-Daudé {
121886480615SPhilippe Mathieu-Daudé     g_assert_not_reached();
121986480615SPhilippe Mathieu-Daudé }
122086480615SPhilippe Mathieu-Daudé 
122186480615SPhilippe Mathieu-Daudé #endif
122286480615SPhilippe Mathieu-Daudé 
122386480615SPhilippe Mathieu-Daudé static void arm_cpu_dump_state(CPUState *cs, FILE *f, int flags)
122486480615SPhilippe Mathieu-Daudé {
122586480615SPhilippe Mathieu-Daudé     ARMCPU *cpu = ARM_CPU(cs);
122686480615SPhilippe Mathieu-Daudé     CPUARMState *env = &cpu->env;
122786480615SPhilippe Mathieu-Daudé     int i;
122886480615SPhilippe Mathieu-Daudé 
122986480615SPhilippe Mathieu-Daudé     if (is_a64(env)) {
123086480615SPhilippe Mathieu-Daudé         aarch64_cpu_dump_state(cs, f, flags);
123186480615SPhilippe Mathieu-Daudé         return;
123286480615SPhilippe Mathieu-Daudé     }
123386480615SPhilippe Mathieu-Daudé 
123486480615SPhilippe Mathieu-Daudé     for (i = 0; i < 16; i++) {
123586480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "R%02d=%08x", i, env->regs[i]);
123686480615SPhilippe Mathieu-Daudé         if ((i % 4) == 3) {
123786480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "\n");
123886480615SPhilippe Mathieu-Daudé         } else {
123986480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, " ");
124086480615SPhilippe Mathieu-Daudé         }
124186480615SPhilippe Mathieu-Daudé     }
124286480615SPhilippe Mathieu-Daudé 
124386480615SPhilippe Mathieu-Daudé     if (arm_feature(env, ARM_FEATURE_M)) {
124486480615SPhilippe Mathieu-Daudé         uint32_t xpsr = xpsr_read(env);
124586480615SPhilippe Mathieu-Daudé         const char *mode;
124686480615SPhilippe Mathieu-Daudé         const char *ns_status = "";
124786480615SPhilippe Mathieu-Daudé 
124886480615SPhilippe Mathieu-Daudé         if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
124986480615SPhilippe Mathieu-Daudé             ns_status = env->v7m.secure ? "S " : "NS ";
125086480615SPhilippe Mathieu-Daudé         }
125186480615SPhilippe Mathieu-Daudé 
125286480615SPhilippe Mathieu-Daudé         if (xpsr & XPSR_EXCP) {
125386480615SPhilippe Mathieu-Daudé             mode = "handler";
125486480615SPhilippe Mathieu-Daudé         } else {
125586480615SPhilippe Mathieu-Daudé             if (env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_NPRIV_MASK) {
125686480615SPhilippe Mathieu-Daudé                 mode = "unpriv-thread";
125786480615SPhilippe Mathieu-Daudé             } else {
125886480615SPhilippe Mathieu-Daudé                 mode = "priv-thread";
125986480615SPhilippe Mathieu-Daudé             }
126086480615SPhilippe Mathieu-Daudé         }
126186480615SPhilippe Mathieu-Daudé 
126286480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "XPSR=%08x %c%c%c%c %c %s%s\n",
126386480615SPhilippe Mathieu-Daudé                      xpsr,
126486480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_N ? 'N' : '-',
126586480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_Z ? 'Z' : '-',
126686480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_C ? 'C' : '-',
126786480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_V ? 'V' : '-',
126886480615SPhilippe Mathieu-Daudé                      xpsr & XPSR_T ? 'T' : 'A',
126986480615SPhilippe Mathieu-Daudé                      ns_status,
127086480615SPhilippe Mathieu-Daudé                      mode);
127186480615SPhilippe Mathieu-Daudé     } else {
127286480615SPhilippe Mathieu-Daudé         uint32_t psr = cpsr_read(env);
127386480615SPhilippe Mathieu-Daudé         const char *ns_status = "";
127486480615SPhilippe Mathieu-Daudé 
127586480615SPhilippe Mathieu-Daudé         if (arm_feature(env, ARM_FEATURE_EL3) &&
127686480615SPhilippe Mathieu-Daudé             (psr & CPSR_M) != ARM_CPU_MODE_MON) {
127786480615SPhilippe Mathieu-Daudé             ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
127886480615SPhilippe Mathieu-Daudé         }
127986480615SPhilippe Mathieu-Daudé 
128086480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "PSR=%08x %c%c%c%c %c %s%s%d\n",
128186480615SPhilippe Mathieu-Daudé                      psr,
128286480615SPhilippe Mathieu-Daudé                      psr & CPSR_N ? 'N' : '-',
128386480615SPhilippe Mathieu-Daudé                      psr & CPSR_Z ? 'Z' : '-',
128486480615SPhilippe Mathieu-Daudé                      psr & CPSR_C ? 'C' : '-',
128586480615SPhilippe Mathieu-Daudé                      psr & CPSR_V ? 'V' : '-',
128686480615SPhilippe Mathieu-Daudé                      psr & CPSR_T ? 'T' : 'A',
128786480615SPhilippe Mathieu-Daudé                      ns_status,
128886480615SPhilippe Mathieu-Daudé                      aarch32_mode_name(psr), (psr & 0x10) ? 32 : 26);
128986480615SPhilippe Mathieu-Daudé     }
129086480615SPhilippe Mathieu-Daudé 
129186480615SPhilippe Mathieu-Daudé     if (flags & CPU_DUMP_FPU) {
129286480615SPhilippe Mathieu-Daudé         int numvfpregs = 0;
1293a6627f5fSRichard Henderson         if (cpu_isar_feature(aa32_simd_r32, cpu)) {
1294a6627f5fSRichard Henderson             numvfpregs = 32;
12957fbc6a40SRichard Henderson         } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
1296a6627f5fSRichard Henderson             numvfpregs = 16;
129786480615SPhilippe Mathieu-Daudé         }
129886480615SPhilippe Mathieu-Daudé         for (i = 0; i < numvfpregs; i++) {
129986480615SPhilippe Mathieu-Daudé             uint64_t v = *aa32_vfp_dreg(env, i);
130086480615SPhilippe Mathieu-Daudé             qemu_fprintf(f, "s%02d=%08x s%02d=%08x d%02d=%016" PRIx64 "\n",
130186480615SPhilippe Mathieu-Daudé                          i * 2, (uint32_t)v,
130286480615SPhilippe Mathieu-Daudé                          i * 2 + 1, (uint32_t)(v >> 32),
130386480615SPhilippe Mathieu-Daudé                          i, v);
130486480615SPhilippe Mathieu-Daudé         }
130586480615SPhilippe Mathieu-Daudé         qemu_fprintf(f, "FPSCR: %08x\n", vfp_get_fpscr(env));
1306aa291908SPeter Maydell         if (cpu_isar_feature(aa32_mve, cpu)) {
1307aa291908SPeter Maydell             qemu_fprintf(f, "VPR: %08x\n", env->v7m.vpr);
1308aa291908SPeter Maydell         }
130986480615SPhilippe Mathieu-Daudé     }
131086480615SPhilippe Mathieu-Daudé }
131186480615SPhilippe Mathieu-Daudé 
1312750245edSRichard Henderson uint64_t arm_build_mp_affinity(int idx, uint8_t clustersz)
131346de5913SIgor Mammedov {
131446de5913SIgor Mammedov     uint32_t Aff1 = idx / clustersz;
131546de5913SIgor Mammedov     uint32_t Aff0 = idx % clustersz;
131646de5913SIgor Mammedov     return (Aff1 << ARM_AFF1_SHIFT) | Aff0;
131746de5913SIgor Mammedov }
131846de5913SIgor Mammedov 
1319e2d8cf9bSPhilippe Mathieu-Daudé uint64_t arm_cpu_mp_affinity(ARMCPU *cpu)
1320e2d8cf9bSPhilippe Mathieu-Daudé {
1321e2d8cf9bSPhilippe Mathieu-Daudé     return cpu->mp_affinity;
1322e2d8cf9bSPhilippe Mathieu-Daudé }
1323e2d8cf9bSPhilippe Mathieu-Daudé 
1324fcf5ef2aSThomas Huth static void arm_cpu_initfn(Object *obj)
1325fcf5ef2aSThomas Huth {
1326fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(obj);
1327fcf5ef2aSThomas Huth 
13285860362dSRichard Henderson     cpu->cp_regs = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1329c27f5d3aSRichard Henderson                                          NULL, g_free);
1330fcf5ef2aSThomas Huth 
1331b5c53d1bSAaron Lindsay     QLIST_INIT(&cpu->pre_el_change_hooks);
133208267487SAaron Lindsay     QLIST_INIT(&cpu->el_change_hooks);
133308267487SAaron Lindsay 
1334b3d52804SRichard Henderson #ifdef CONFIG_USER_ONLY
1335b3d52804SRichard Henderson # ifdef TARGET_AARCH64
1336b3d52804SRichard Henderson     /*
1337e74c0976SRichard Henderson      * The linux kernel defaults to 512-bit for SVE, and 256-bit for SME.
1338e74c0976SRichard Henderson      * These values were chosen to fit within the default signal frame.
1339e74c0976SRichard Henderson      * See documentation for /proc/sys/abi/{sve,sme}_default_vector_length,
1340e74c0976SRichard Henderson      * and our corresponding cpu property.
1341b3d52804SRichard Henderson      */
1342b3d52804SRichard Henderson     cpu->sve_default_vq = 4;
1343e74c0976SRichard Henderson     cpu->sme_default_vq = 2;
1344b3d52804SRichard Henderson # endif
1345b3d52804SRichard Henderson #else
1346fcf5ef2aSThomas Huth     /* Our inbound IRQ and FIQ lines */
1347fcf5ef2aSThomas Huth     if (kvm_enabled()) {
1348fcf5ef2aSThomas Huth         /* VIRQ and VFIQ are unused with KVM but we add them to maintain
1349fcf5ef2aSThomas Huth          * the same interface as non-KVM CPUs.
1350fcf5ef2aSThomas Huth          */
1351fcf5ef2aSThomas Huth         qdev_init_gpio_in(DEVICE(cpu), arm_cpu_kvm_set_irq, 4);
1352fcf5ef2aSThomas Huth     } else {
1353fcf5ef2aSThomas Huth         qdev_init_gpio_in(DEVICE(cpu), arm_cpu_set_irq, 4);
1354fcf5ef2aSThomas Huth     }
1355fcf5ef2aSThomas Huth 
1356fcf5ef2aSThomas Huth     qdev_init_gpio_out(DEVICE(cpu), cpu->gt_timer_outputs,
1357fcf5ef2aSThomas Huth                        ARRAY_SIZE(cpu->gt_timer_outputs));
1358aa1b3111SPeter Maydell 
1359aa1b3111SPeter Maydell     qdev_init_gpio_out_named(DEVICE(cpu), &cpu->gicv3_maintenance_interrupt,
1360aa1b3111SPeter Maydell                              "gicv3-maintenance-interrupt", 1);
136107f48730SAndrew Jones     qdev_init_gpio_out_named(DEVICE(cpu), &cpu->pmu_interrupt,
136207f48730SAndrew Jones                              "pmu-interrupt", 1);
1363fcf5ef2aSThomas Huth #endif
1364fcf5ef2aSThomas Huth 
1365fcf5ef2aSThomas Huth     /* DTB consumers generally don't in fact care what the 'compatible'
1366fcf5ef2aSThomas Huth      * string is, so always provide some string and trust that a hypothetical
1367fcf5ef2aSThomas Huth      * picky DTB consumer will also provide a helpful error message.
1368fcf5ef2aSThomas Huth      */
1369fcf5ef2aSThomas Huth     cpu->dtb_compatible = "qemu,unknown";
13700dc71c70SAkihiko Odaki     cpu->psci_version = QEMU_PSCI_VERSION_0_1; /* By default assume PSCI v0.1 */
1371fcf5ef2aSThomas Huth     cpu->kvm_target = QEMU_KVM_ARM_TARGET_NONE;
1372fcf5ef2aSThomas Huth 
13732c9c0bf9SAlexander Graf     if (tcg_enabled() || hvf_enabled()) {
13740dc71c70SAkihiko Odaki         /* TCG and HVF implement PSCI 1.1 */
13750dc71c70SAkihiko Odaki         cpu->psci_version = QEMU_PSCI_VERSION_1_1;
1376fcf5ef2aSThomas Huth     }
1377fcf5ef2aSThomas Huth }
1378fcf5ef2aSThomas Huth 
137996eec6b2SAndrew Jeffery static Property arm_cpu_gt_cntfrq_property =
138096eec6b2SAndrew Jeffery             DEFINE_PROP_UINT64("cntfrq", ARMCPU, gt_cntfrq_hz,
138196eec6b2SAndrew Jeffery                                NANOSECONDS_PER_SECOND / GTIMER_SCALE);
138296eec6b2SAndrew Jeffery 
1383fcf5ef2aSThomas Huth static Property arm_cpu_reset_cbar_property =
1384fcf5ef2aSThomas Huth             DEFINE_PROP_UINT64("reset-cbar", ARMCPU, reset_cbar, 0);
1385fcf5ef2aSThomas Huth 
1386fcf5ef2aSThomas Huth static Property arm_cpu_reset_hivecs_property =
1387fcf5ef2aSThomas Huth             DEFINE_PROP_BOOL("reset-hivecs", ARMCPU, reset_hivecs, false);
1388fcf5ef2aSThomas Huth 
138945ca3a14SRichard Henderson #ifndef CONFIG_USER_ONLY
1390c25bd18aSPeter Maydell static Property arm_cpu_has_el2_property =
1391c25bd18aSPeter Maydell             DEFINE_PROP_BOOL("has_el2", ARMCPU, has_el2, true);
1392c25bd18aSPeter Maydell 
1393fcf5ef2aSThomas Huth static Property arm_cpu_has_el3_property =
1394fcf5ef2aSThomas Huth             DEFINE_PROP_BOOL("has_el3", ARMCPU, has_el3, true);
139545ca3a14SRichard Henderson #endif
1396fcf5ef2aSThomas Huth 
13973a062d57SJulian Brown static Property arm_cpu_cfgend_property =
13983a062d57SJulian Brown             DEFINE_PROP_BOOL("cfgend", ARMCPU, cfgend, false);
13993a062d57SJulian Brown 
140097a28b0eSPeter Maydell static Property arm_cpu_has_vfp_property =
140197a28b0eSPeter Maydell             DEFINE_PROP_BOOL("vfp", ARMCPU, has_vfp, true);
140297a28b0eSPeter Maydell 
140342bea956SCédric Le Goater static Property arm_cpu_has_vfp_d32_property =
140442bea956SCédric Le Goater             DEFINE_PROP_BOOL("vfp-d32", ARMCPU, has_vfp_d32, true);
140542bea956SCédric Le Goater 
140697a28b0eSPeter Maydell static Property arm_cpu_has_neon_property =
140797a28b0eSPeter Maydell             DEFINE_PROP_BOOL("neon", ARMCPU, has_neon, true);
140897a28b0eSPeter Maydell 
1409ea90db0aSPeter Maydell static Property arm_cpu_has_dsp_property =
1410ea90db0aSPeter Maydell             DEFINE_PROP_BOOL("dsp", ARMCPU, has_dsp, true);
1411ea90db0aSPeter Maydell 
1412fcf5ef2aSThomas Huth static Property arm_cpu_has_mpu_property =
1413fcf5ef2aSThomas Huth             DEFINE_PROP_BOOL("has-mpu", ARMCPU, has_mpu, true);
1414fcf5ef2aSThomas Huth 
14158d92e26bSPeter Maydell /* This is like DEFINE_PROP_UINT32 but it doesn't set the default value,
14168d92e26bSPeter Maydell  * because the CPU initfn will have already set cpu->pmsav7_dregion to
14178d92e26bSPeter Maydell  * the right value for that particular CPU type, and we don't want
14188d92e26bSPeter Maydell  * to override that with an incorrect constant value.
14198d92e26bSPeter Maydell  */
1420fcf5ef2aSThomas Huth static Property arm_cpu_pmsav7_dregion_property =
14218d92e26bSPeter Maydell             DEFINE_PROP_UNSIGNED_NODEFAULT("pmsav7-dregion", ARMCPU,
14228d92e26bSPeter Maydell                                            pmsav7_dregion,
14238d92e26bSPeter Maydell                                            qdev_prop_uint32, uint32_t);
1424fcf5ef2aSThomas Huth 
1425ae502508SAndrew Jones static bool arm_get_pmu(Object *obj, Error **errp)
1426ae502508SAndrew Jones {
1427ae502508SAndrew Jones     ARMCPU *cpu = ARM_CPU(obj);
1428ae502508SAndrew Jones 
1429ae502508SAndrew Jones     return cpu->has_pmu;
1430ae502508SAndrew Jones }
1431ae502508SAndrew Jones 
1432ae502508SAndrew Jones static void arm_set_pmu(Object *obj, bool value, Error **errp)
1433ae502508SAndrew Jones {
1434ae502508SAndrew Jones     ARMCPU *cpu = ARM_CPU(obj);
1435ae502508SAndrew Jones 
1436ae502508SAndrew Jones     if (value) {
14377d20e681SPhilippe Mathieu-Daudé         if (kvm_enabled() && !kvm_arm_pmu_supported()) {
1438ae502508SAndrew Jones             error_setg(errp, "'pmu' feature not supported by KVM on this host");
1439ae502508SAndrew Jones             return;
1440ae502508SAndrew Jones         }
1441ae502508SAndrew Jones         set_feature(&cpu->env, ARM_FEATURE_PMU);
1442ae502508SAndrew Jones     } else {
1443ae502508SAndrew Jones         unset_feature(&cpu->env, ARM_FEATURE_PMU);
1444ae502508SAndrew Jones     }
1445ae502508SAndrew Jones     cpu->has_pmu = value;
1446ae502508SAndrew Jones }
1447ae502508SAndrew Jones 
14487def8754SAndrew Jeffery unsigned int gt_cntfrq_period_ns(ARMCPU *cpu)
14497def8754SAndrew Jeffery {
145096eec6b2SAndrew Jeffery     /*
145196eec6b2SAndrew Jeffery      * The exact approach to calculating guest ticks is:
145296eec6b2SAndrew Jeffery      *
145396eec6b2SAndrew Jeffery      *     muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), cpu->gt_cntfrq_hz,
145496eec6b2SAndrew Jeffery      *              NANOSECONDS_PER_SECOND);
145596eec6b2SAndrew Jeffery      *
145696eec6b2SAndrew Jeffery      * We don't do that. Rather we intentionally use integer division
145796eec6b2SAndrew Jeffery      * truncation below and in the caller for the conversion of host monotonic
145896eec6b2SAndrew Jeffery      * time to guest ticks to provide the exact inverse for the semantics of
145996eec6b2SAndrew Jeffery      * the QEMUTimer scale factor. QEMUTimer's scale facter is an integer, so
146096eec6b2SAndrew Jeffery      * it loses precision when representing frequencies where
146196eec6b2SAndrew Jeffery      * `(NANOSECONDS_PER_SECOND % cpu->gt_cntfrq) > 0` holds. Failing to
146296eec6b2SAndrew Jeffery      * provide an exact inverse leads to scheduling timers with negative
146396eec6b2SAndrew Jeffery      * periods, which in turn leads to sticky behaviour in the guest.
146496eec6b2SAndrew Jeffery      *
146596eec6b2SAndrew Jeffery      * Finally, CNTFRQ is effectively capped at 1GHz to ensure our scale factor
146696eec6b2SAndrew Jeffery      * cannot become zero.
146796eec6b2SAndrew Jeffery      */
14687def8754SAndrew Jeffery     return NANOSECONDS_PER_SECOND > cpu->gt_cntfrq_hz ?
14697def8754SAndrew Jeffery       NANOSECONDS_PER_SECOND / cpu->gt_cntfrq_hz : 1;
14707def8754SAndrew Jeffery }
14717def8754SAndrew Jeffery 
1472b8f7959fSPeter Maydell static void arm_cpu_propagate_feature_implications(ARMCPU *cpu)
1473b8f7959fSPeter Maydell {
1474b8f7959fSPeter Maydell     CPUARMState *env = &cpu->env;
1475b8f7959fSPeter Maydell     bool no_aa32 = false;
1476b8f7959fSPeter Maydell 
1477b8f7959fSPeter Maydell     /*
1478b8f7959fSPeter Maydell      * Some features automatically imply others: set the feature
1479b8f7959fSPeter Maydell      * bits explicitly for these cases.
1480b8f7959fSPeter Maydell      */
1481b8f7959fSPeter Maydell 
1482b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_M)) {
1483b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_PMSA);
1484b8f7959fSPeter Maydell     }
1485b8f7959fSPeter Maydell 
1486b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V8)) {
1487b8f7959fSPeter Maydell         if (arm_feature(env, ARM_FEATURE_M)) {
1488b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_V7);
1489b8f7959fSPeter Maydell         } else {
1490b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_V7VE);
1491b8f7959fSPeter Maydell         }
1492b8f7959fSPeter Maydell     }
1493b8f7959fSPeter Maydell 
1494b8f7959fSPeter Maydell     /*
1495b8f7959fSPeter Maydell      * There exist AArch64 cpus without AArch32 support.  When KVM
1496b8f7959fSPeter Maydell      * queries ID_ISAR0_EL1 on such a host, the value is UNKNOWN.
1497b8f7959fSPeter Maydell      * Similarly, we cannot check ID_AA64PFR0 without AArch64 support.
1498b8f7959fSPeter Maydell      * As a general principle, we also do not make ID register
1499b8f7959fSPeter Maydell      * consistency checks anywhere unless using TCG, because only
1500b8f7959fSPeter Maydell      * for TCG would a consistency-check failure be a QEMU bug.
1501b8f7959fSPeter Maydell      */
1502b8f7959fSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
1503b8f7959fSPeter Maydell         no_aa32 = !cpu_isar_feature(aa64_aa32, cpu);
1504b8f7959fSPeter Maydell     }
1505b8f7959fSPeter Maydell 
1506b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V7VE)) {
1507b8f7959fSPeter Maydell         /*
1508b8f7959fSPeter Maydell          * v7 Virtualization Extensions. In real hardware this implies
1509b8f7959fSPeter Maydell          * EL2 and also the presence of the Security Extensions.
1510b8f7959fSPeter Maydell          * For QEMU, for backwards-compatibility we implement some
1511b8f7959fSPeter Maydell          * CPUs or CPU configs which have no actual EL2 or EL3 but do
1512b8f7959fSPeter Maydell          * include the various other features that V7VE implies.
1513b8f7959fSPeter Maydell          * Presence of EL2 itself is ARM_FEATURE_EL2, and of the
1514b8f7959fSPeter Maydell          * Security Extensions is ARM_FEATURE_EL3.
1515b8f7959fSPeter Maydell          */
1516b8f7959fSPeter Maydell         assert(!tcg_enabled() || no_aa32 ||
1517b8f7959fSPeter Maydell                cpu_isar_feature(aa32_arm_div, cpu));
1518b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_LPAE);
1519b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V7);
1520b8f7959fSPeter Maydell     }
1521b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V7)) {
1522b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_VAPA);
1523b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_THUMB2);
1524b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_MPIDR);
1525b8f7959fSPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
1526b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_V6K);
1527b8f7959fSPeter Maydell         } else {
1528b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_V6);
1529b8f7959fSPeter Maydell         }
1530b8f7959fSPeter Maydell 
1531b8f7959fSPeter Maydell         /*
1532b8f7959fSPeter Maydell          * Always define VBAR for V7 CPUs even if it doesn't exist in
1533b8f7959fSPeter Maydell          * non-EL3 configs. This is needed by some legacy boards.
1534b8f7959fSPeter Maydell          */
1535b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_VBAR);
1536b8f7959fSPeter Maydell     }
1537b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V6K)) {
1538b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V6);
1539b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_MVFR);
1540b8f7959fSPeter Maydell     }
1541b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V6)) {
1542b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V5);
1543b8f7959fSPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
1544b8f7959fSPeter Maydell             assert(!tcg_enabled() || no_aa32 ||
1545b8f7959fSPeter Maydell                    cpu_isar_feature(aa32_jazelle, cpu));
1546b8f7959fSPeter Maydell             set_feature(env, ARM_FEATURE_AUXCR);
1547b8f7959fSPeter Maydell         }
1548b8f7959fSPeter Maydell     }
1549b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_V5)) {
1550b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V4T);
1551b8f7959fSPeter Maydell     }
1552b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_LPAE)) {
1553b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_V7MP);
1554b8f7959fSPeter Maydell     }
1555b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
1556b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_CBAR);
1557b8f7959fSPeter Maydell     }
1558b8f7959fSPeter Maydell     if (arm_feature(env, ARM_FEATURE_THUMB2) &&
1559b8f7959fSPeter Maydell         !arm_feature(env, ARM_FEATURE_M)) {
1560b8f7959fSPeter Maydell         set_feature(env, ARM_FEATURE_THUMB_DSP);
1561b8f7959fSPeter Maydell     }
1562b8f7959fSPeter Maydell }
1563b8f7959fSPeter Maydell 
156451e5ef45SMarc-André Lureau void arm_cpu_post_init(Object *obj)
1565fcf5ef2aSThomas Huth {
1566fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(obj);
1567fcf5ef2aSThomas Huth 
1568b8f7959fSPeter Maydell     /*
1569b8f7959fSPeter Maydell      * Some features imply others. Figure this out now, because we
1570b8f7959fSPeter Maydell      * are going to look at the feature bits in deciding which
1571b8f7959fSPeter Maydell      * properties to add.
1572790a1150SPeter Maydell      */
1573b8f7959fSPeter Maydell     arm_cpu_propagate_feature_implications(cpu);
1574790a1150SPeter Maydell 
1575fcf5ef2aSThomas Huth     if (arm_feature(&cpu->env, ARM_FEATURE_CBAR) ||
1576fcf5ef2aSThomas Huth         arm_feature(&cpu->env, ARM_FEATURE_CBAR_RO)) {
157794d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_cbar_property);
1578fcf5ef2aSThomas Huth     }
1579fcf5ef2aSThomas Huth 
1580fcf5ef2aSThomas Huth     if (!arm_feature(&cpu->env, ARM_FEATURE_M)) {
158194d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_hivecs_property);
1582fcf5ef2aSThomas Huth     }
1583fcf5ef2aSThomas Huth 
1584910e4f24STobias Röhmel     if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
15854a7319b7SEdgar E. Iglesias         object_property_add_uint64_ptr(obj, "rvbar",
15864a7319b7SEdgar E. Iglesias                                        &cpu->rvbar_prop,
15874a7319b7SEdgar E. Iglesias                                        OBJ_PROP_FLAG_READWRITE);
1588fcf5ef2aSThomas Huth     }
1589fcf5ef2aSThomas Huth 
159045ca3a14SRichard Henderson #ifndef CONFIG_USER_ONLY
1591fcf5ef2aSThomas Huth     if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
1592fcf5ef2aSThomas Huth         /* Add the has_el3 state CPU property only if EL3 is allowed.  This will
1593fcf5ef2aSThomas Huth          * prevent "has_el3" from existing on CPUs which cannot support EL3.
1594fcf5ef2aSThomas Huth          */
159594d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el3_property);
1596fcf5ef2aSThomas Huth 
1597fcf5ef2aSThomas Huth         object_property_add_link(obj, "secure-memory",
1598fcf5ef2aSThomas Huth                                  TYPE_MEMORY_REGION,
1599fcf5ef2aSThomas Huth                                  (Object **)&cpu->secure_memory,
1600fcf5ef2aSThomas Huth                                  qdev_prop_allow_set_link_before_realize,
1601d2623129SMarkus Armbruster                                  OBJ_PROP_LINK_STRONG);
1602fcf5ef2aSThomas Huth     }
1603fcf5ef2aSThomas Huth 
1604c25bd18aSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_EL2)) {
160594d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el2_property);
1606c25bd18aSPeter Maydell     }
160745ca3a14SRichard Henderson #endif
1608c25bd18aSPeter Maydell 
1609fcf5ef2aSThomas Huth     if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) {
1610ae502508SAndrew Jones         cpu->has_pmu = true;
1611d2623129SMarkus Armbruster         object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu);
1612fcf5ef2aSThomas Huth     }
1613fcf5ef2aSThomas Huth 
161497a28b0eSPeter Maydell     /*
161597a28b0eSPeter Maydell      * Allow user to turn off VFP and Neon support, but only for TCG --
161697a28b0eSPeter Maydell      * KVM does not currently allow us to lie to the guest about its
161797a28b0eSPeter Maydell      * ID/feature registers, so the guest always sees what the host has.
161897a28b0eSPeter Maydell      */
16194315f7c6SRichard Henderson     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
16204315f7c6SRichard Henderson         if (cpu_isar_feature(aa64_fp_simd, cpu)) {
162197a28b0eSPeter Maydell             cpu->has_vfp = true;
162242bea956SCédric Le Goater             cpu->has_vfp_d32 = true;
16234315f7c6SRichard Henderson             if (tcg_enabled() || qtest_enabled()) {
16244315f7c6SRichard Henderson                 qdev_property_add_static(DEVICE(obj),
16254315f7c6SRichard Henderson                                          &arm_cpu_has_vfp_property);
16264315f7c6SRichard Henderson             }
16274315f7c6SRichard Henderson         }
16284315f7c6SRichard Henderson     } else if (cpu_isar_feature(aa32_vfp, cpu)) {
16294315f7c6SRichard Henderson         cpu->has_vfp = true;
1630*185e3fdfSPeter Maydell         if (tcg_enabled() || qtest_enabled()) {
1631*185e3fdfSPeter Maydell             qdev_property_add_static(DEVICE(obj),
1632*185e3fdfSPeter Maydell                                      &arm_cpu_has_vfp_property);
1633*185e3fdfSPeter Maydell         }
16344315f7c6SRichard Henderson         if (cpu_isar_feature(aa32_simd_r32, cpu)) {
16354315f7c6SRichard Henderson             cpu->has_vfp_d32 = true;
163642bea956SCédric Le Goater             /*
163742bea956SCédric Le Goater              * The permitted values of the SIMDReg bits [3:0] on
163842bea956SCédric Le Goater              * Armv8-A are either 0b0000 and 0b0010. On such CPUs,
163942bea956SCédric Le Goater              * make sure that has_vfp_d32 can not be set to false.
164042bea956SCédric Le Goater              */
16414315f7c6SRichard Henderson             if ((tcg_enabled() || qtest_enabled())
16424315f7c6SRichard Henderson                 && !(arm_feature(&cpu->env, ARM_FEATURE_V8)
16434315f7c6SRichard Henderson                      && !arm_feature(&cpu->env, ARM_FEATURE_M))) {
164442bea956SCédric Le Goater                 qdev_property_add_static(DEVICE(obj),
164542bea956SCédric Le Goater                                          &arm_cpu_has_vfp_d32_property);
164642bea956SCédric Le Goater             }
164742bea956SCédric Le Goater         }
164842bea956SCédric Le Goater     }
164942bea956SCédric Le Goater 
165097a28b0eSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_NEON)) {
165197a28b0eSPeter Maydell         cpu->has_neon = true;
165297a28b0eSPeter Maydell         if (!kvm_enabled()) {
165394d912d1SMarc-André Lureau             qdev_property_add_static(DEVICE(obj), &arm_cpu_has_neon_property);
165497a28b0eSPeter Maydell         }
165597a28b0eSPeter Maydell     }
165697a28b0eSPeter Maydell 
1657ea90db0aSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_M) &&
1658ea90db0aSPeter Maydell         arm_feature(&cpu->env, ARM_FEATURE_THUMB_DSP)) {
165994d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_dsp_property);
1660ea90db0aSPeter Maydell     }
1661ea90db0aSPeter Maydell 
1662452a0955SPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_PMSA)) {
166394d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_mpu_property);
1664fcf5ef2aSThomas Huth         if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
1665fcf5ef2aSThomas Huth             qdev_property_add_static(DEVICE(obj),
166694d912d1SMarc-André Lureau                                      &arm_cpu_pmsav7_dregion_property);
1667fcf5ef2aSThomas Huth         }
1668fcf5ef2aSThomas Huth     }
1669fcf5ef2aSThomas Huth 
1670181962fdSPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_M_SECURITY)) {
1671181962fdSPeter Maydell         object_property_add_link(obj, "idau", TYPE_IDAU_INTERFACE, &cpu->idau,
1672181962fdSPeter Maydell                                  qdev_prop_allow_set_link_before_realize,
1673d2623129SMarkus Armbruster                                  OBJ_PROP_LINK_STRONG);
1674f9f62e4cSPeter Maydell         /*
1675f9f62e4cSPeter Maydell          * M profile: initial value of the Secure VTOR. We can't just use
1676f9f62e4cSPeter Maydell          * a simple DEFINE_PROP_UINT32 for this because we want to permit
1677f9f62e4cSPeter Maydell          * the property to be set after realize.
1678f9f62e4cSPeter Maydell          */
167964a7b8deSFelipe Franciosi         object_property_add_uint32_ptr(obj, "init-svtor",
168064a7b8deSFelipe Franciosi                                        &cpu->init_svtor,
1681d2623129SMarkus Armbruster                                        OBJ_PROP_FLAG_READWRITE);
1682181962fdSPeter Maydell     }
16837cda2149SPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_M)) {
16847cda2149SPeter Maydell         /*
16857cda2149SPeter Maydell          * Initial value of the NS VTOR (for cores without the Security
16867cda2149SPeter Maydell          * extension, this is the only VTOR)
16877cda2149SPeter Maydell          */
16887cda2149SPeter Maydell         object_property_add_uint32_ptr(obj, "init-nsvtor",
16897cda2149SPeter Maydell                                        &cpu->init_nsvtor,
16907cda2149SPeter Maydell                                        OBJ_PROP_FLAG_READWRITE);
16917cda2149SPeter Maydell     }
1692181962fdSPeter Maydell 
1693bddd892eSPeter Maydell     /* Not DEFINE_PROP_UINT32: we want this to be settable after realize */
1694bddd892eSPeter Maydell     object_property_add_uint32_ptr(obj, "psci-conduit",
1695bddd892eSPeter Maydell                                    &cpu->psci_conduit,
1696bddd892eSPeter Maydell                                    OBJ_PROP_FLAG_READWRITE);
1697bddd892eSPeter Maydell 
169894d912d1SMarc-André Lureau     qdev_property_add_static(DEVICE(obj), &arm_cpu_cfgend_property);
169996eec6b2SAndrew Jeffery 
170096eec6b2SAndrew Jeffery     if (arm_feature(&cpu->env, ARM_FEATURE_GENERIC_TIMER)) {
170194d912d1SMarc-André Lureau         qdev_property_add_static(DEVICE(cpu), &arm_cpu_gt_cntfrq_property);
170296eec6b2SAndrew Jeffery     }
17039e6f8d8aSfangying 
17049e6f8d8aSfangying     if (kvm_enabled()) {
1705cac675b5SPhilippe Mathieu-Daudé         kvm_arm_add_vcpu_properties(cpu);
17069e6f8d8aSfangying     }
17078bce44a2SRichard Henderson 
17088bce44a2SRichard Henderson #ifndef CONFIG_USER_ONLY
17098bce44a2SRichard Henderson     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) &&
17108bce44a2SRichard Henderson         cpu_isar_feature(aa64_mte, cpu)) {
17118bce44a2SRichard Henderson         object_property_add_link(obj, "tag-memory",
17128bce44a2SRichard Henderson                                  TYPE_MEMORY_REGION,
17138bce44a2SRichard Henderson                                  (Object **)&cpu->tag_memory,
17148bce44a2SRichard Henderson                                  qdev_prop_allow_set_link_before_realize,
17158bce44a2SRichard Henderson                                  OBJ_PROP_LINK_STRONG);
17168bce44a2SRichard Henderson 
17178bce44a2SRichard Henderson         if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
17188bce44a2SRichard Henderson             object_property_add_link(obj, "secure-tag-memory",
17198bce44a2SRichard Henderson                                      TYPE_MEMORY_REGION,
17208bce44a2SRichard Henderson                                      (Object **)&cpu->secure_tag_memory,
17218bce44a2SRichard Henderson                                      qdev_prop_allow_set_link_before_realize,
17228bce44a2SRichard Henderson                                      OBJ_PROP_LINK_STRONG);
17238bce44a2SRichard Henderson         }
17248bce44a2SRichard Henderson     }
17258bce44a2SRichard Henderson #endif
1726fcf5ef2aSThomas Huth }
1727fcf5ef2aSThomas Huth 
1728fcf5ef2aSThomas Huth static void arm_cpu_finalizefn(Object *obj)
1729fcf5ef2aSThomas Huth {
1730fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(obj);
173108267487SAaron Lindsay     ARMELChangeHook *hook, *next;
173208267487SAaron Lindsay 
1733fcf5ef2aSThomas Huth     g_hash_table_destroy(cpu->cp_regs);
173408267487SAaron Lindsay 
1735b5c53d1bSAaron Lindsay     QLIST_FOREACH_SAFE(hook, &cpu->pre_el_change_hooks, node, next) {
1736b5c53d1bSAaron Lindsay         QLIST_REMOVE(hook, node);
1737b5c53d1bSAaron Lindsay         g_free(hook);
1738b5c53d1bSAaron Lindsay     }
173908267487SAaron Lindsay     QLIST_FOREACH_SAFE(hook, &cpu->el_change_hooks, node, next) {
174008267487SAaron Lindsay         QLIST_REMOVE(hook, node);
174108267487SAaron Lindsay         g_free(hook);
174208267487SAaron Lindsay     }
17434e7beb0cSAaron Lindsay OS #ifndef CONFIG_USER_ONLY
17444e7beb0cSAaron Lindsay OS     if (cpu->pmu_timer) {
17454e7beb0cSAaron Lindsay OS         timer_free(cpu->pmu_timer);
17464e7beb0cSAaron Lindsay OS     }
17474e7beb0cSAaron Lindsay OS #endif
1748fcf5ef2aSThomas Huth }
1749fcf5ef2aSThomas Huth 
17500df9142dSAndrew Jones void arm_cpu_finalize_features(ARMCPU *cpu, Error **errp)
17510df9142dSAndrew Jones {
17520df9142dSAndrew Jones     Error *local_err = NULL;
17530df9142dSAndrew Jones 
175407301161SRichard Henderson #ifdef TARGET_AARCH64
17550df9142dSAndrew Jones     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
17560df9142dSAndrew Jones         arm_cpu_sve_finalize(cpu, &local_err);
17570df9142dSAndrew Jones         if (local_err != NULL) {
17580df9142dSAndrew Jones             error_propagate(errp, local_err);
17590df9142dSAndrew Jones             return;
17600df9142dSAndrew Jones         }
1761eb94284dSRichard Henderson 
1762f7767ca3SPeter Maydell         /*
1763f7767ca3SPeter Maydell          * FEAT_SME is not architecturally dependent on FEAT_SVE (unless
1764f7767ca3SPeter Maydell          * FEAT_SME_FA64 is present). However our implementation currently
1765f7767ca3SPeter Maydell          * assumes it, so if the user asked for sve=off then turn off SME also.
1766f7767ca3SPeter Maydell          * (KVM doesn't currently support SME at all.)
1767f7767ca3SPeter Maydell          */
1768f7767ca3SPeter Maydell         if (cpu_isar_feature(aa64_sme, cpu) && !cpu_isar_feature(aa64_sve, cpu)) {
1769f7767ca3SPeter Maydell             object_property_set_bool(OBJECT(cpu), "sme", false, &error_abort);
1770f7767ca3SPeter Maydell         }
1771f7767ca3SPeter Maydell 
1772e74c0976SRichard Henderson         arm_cpu_sme_finalize(cpu, &local_err);
1773e74c0976SRichard Henderson         if (local_err != NULL) {
1774e74c0976SRichard Henderson             error_propagate(errp, local_err);
1775e74c0976SRichard Henderson             return;
1776e74c0976SRichard Henderson         }
1777e74c0976SRichard Henderson 
1778eb94284dSRichard Henderson         arm_cpu_pauth_finalize(cpu, &local_err);
1779eb94284dSRichard Henderson         if (local_err != NULL) {
1780eb94284dSRichard Henderson             error_propagate(errp, local_err);
1781eb94284dSRichard Henderson             return;
1782eb94284dSRichard Henderson         }
178369b2265dSRichard Henderson 
178469b2265dSRichard Henderson         arm_cpu_lpa2_finalize(cpu, &local_err);
178569b2265dSRichard Henderson         if (local_err != NULL) {
178669b2265dSRichard Henderson             error_propagate(errp, local_err);
178769b2265dSRichard Henderson             return;
178869b2265dSRichard Henderson         }
1789eb94284dSRichard Henderson     }
179007301161SRichard Henderson #endif
179168970d1eSAndrew Jones 
179268970d1eSAndrew Jones     if (kvm_enabled()) {
179368970d1eSAndrew Jones         kvm_arm_steal_time_finalize(cpu, &local_err);
179468970d1eSAndrew Jones         if (local_err != NULL) {
179568970d1eSAndrew Jones             error_propagate(errp, local_err);
179668970d1eSAndrew Jones             return;
179768970d1eSAndrew Jones         }
179868970d1eSAndrew Jones     }
17990df9142dSAndrew Jones }
18000df9142dSAndrew Jones 
1801fcf5ef2aSThomas Huth static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
1802fcf5ef2aSThomas Huth {
1803fcf5ef2aSThomas Huth     CPUState *cs = CPU(dev);
1804fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(dev);
1805fcf5ef2aSThomas Huth     ARMCPUClass *acc = ARM_CPU_GET_CLASS(dev);
1806fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
1807fcf5ef2aSThomas Huth     int pagebits;
1808fcf5ef2aSThomas Huth     Error *local_err = NULL;
1809fcf5ef2aSThomas Huth 
1810b94b8c60SPhilippe Mathieu-Daudé #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
1811e607ea39SAnton Johansson     /* Use pc-relative instructions in system-mode */
1812e607ea39SAnton Johansson     cs->tcg_cflags |= CF_PCREL;
1813e607ea39SAnton Johansson #endif
1814e607ea39SAnton Johansson 
1815c4487d76SPeter Maydell     /* If we needed to query the host kernel for the CPU features
1816c4487d76SPeter Maydell      * then it's possible that might have failed in the initfn, but
1817c4487d76SPeter Maydell      * this is the first point where we can report it.
1818c4487d76SPeter Maydell      */
1819c4487d76SPeter Maydell     if (cpu->host_cpu_probe_failed) {
1820585df85eSPeter Maydell         if (!kvm_enabled() && !hvf_enabled()) {
1821585df85eSPeter Maydell             error_setg(errp, "The 'host' CPU type can only be used with KVM or HVF");
1822c4487d76SPeter Maydell         } else {
1823c4487d76SPeter Maydell             error_setg(errp, "Failed to retrieve host CPU features");
1824c4487d76SPeter Maydell         }
1825c4487d76SPeter Maydell         return;
1826c4487d76SPeter Maydell     }
1827c4487d76SPeter Maydell 
182895f87565SPeter Maydell #ifndef CONFIG_USER_ONLY
182995f87565SPeter Maydell     /* The NVIC and M-profile CPU are two halves of a single piece of
183095f87565SPeter Maydell      * hardware; trying to use one without the other is a command line
183195f87565SPeter Maydell      * error and will result in segfaults if not caught here.
183295f87565SPeter Maydell      */
183395f87565SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M)) {
183495f87565SPeter Maydell         if (!env->nvic) {
183595f87565SPeter Maydell             error_setg(errp, "This board cannot be used with Cortex-M CPUs");
183695f87565SPeter Maydell             return;
183795f87565SPeter Maydell         }
183895f87565SPeter Maydell     } else {
183995f87565SPeter Maydell         if (env->nvic) {
184095f87565SPeter Maydell             error_setg(errp, "This board can only be used with Cortex-M CPUs");
184195f87565SPeter Maydell             return;
184295f87565SPeter Maydell         }
184395f87565SPeter Maydell     }
1844397cd31fSPeter Maydell 
1845045e5064SAlexander Graf     if (!tcg_enabled() && !qtest_enabled()) {
184649e7f191SPeter Maydell         /*
1847045e5064SAlexander Graf          * We assume that no accelerator except TCG (and the "not really an
1848045e5064SAlexander Graf          * accelerator" qtest) can handle these features, because Arm hardware
1849045e5064SAlexander Graf          * virtualization can't virtualize them.
1850045e5064SAlexander Graf          *
185149e7f191SPeter Maydell          * Catch all the cases which might cause us to create more than one
185249e7f191SPeter Maydell          * address space for the CPU (otherwise we will assert() later in
185349e7f191SPeter Maydell          * cpu_address_space_init()).
185449e7f191SPeter Maydell          */
185549e7f191SPeter Maydell         if (arm_feature(env, ARM_FEATURE_M)) {
185649e7f191SPeter Maydell             error_setg(errp,
1857045e5064SAlexander Graf                        "Cannot enable %s when using an M-profile guest CPU",
1858045e5064SAlexander Graf                        current_accel_name());
185949e7f191SPeter Maydell             return;
186049e7f191SPeter Maydell         }
186149e7f191SPeter Maydell         if (cpu->has_el3) {
186249e7f191SPeter Maydell             error_setg(errp,
1863045e5064SAlexander Graf                        "Cannot enable %s when guest CPU has EL3 enabled",
1864045e5064SAlexander Graf                        current_accel_name());
186549e7f191SPeter Maydell             return;
186649e7f191SPeter Maydell         }
186749e7f191SPeter Maydell         if (cpu->tag_memory) {
186849e7f191SPeter Maydell             error_setg(errp,
1869d009607dSPeter Maydell                        "Cannot enable %s when guest CPUs has MTE enabled",
1870045e5064SAlexander Graf                        current_accel_name());
187149e7f191SPeter Maydell             return;
187249e7f191SPeter Maydell         }
187349e7f191SPeter Maydell     }
187449e7f191SPeter Maydell 
187596eec6b2SAndrew Jeffery     {
187696eec6b2SAndrew Jeffery         uint64_t scale;
187796eec6b2SAndrew Jeffery 
187896eec6b2SAndrew Jeffery         if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
187996eec6b2SAndrew Jeffery             if (!cpu->gt_cntfrq_hz) {
188096eec6b2SAndrew Jeffery                 error_setg(errp, "Invalid CNTFRQ: %"PRId64"Hz",
188196eec6b2SAndrew Jeffery                            cpu->gt_cntfrq_hz);
188296eec6b2SAndrew Jeffery                 return;
188396eec6b2SAndrew Jeffery             }
188496eec6b2SAndrew Jeffery             scale = gt_cntfrq_period_ns(cpu);
188596eec6b2SAndrew Jeffery         } else {
188696eec6b2SAndrew Jeffery             scale = GTIMER_SCALE;
188796eec6b2SAndrew Jeffery         }
188896eec6b2SAndrew Jeffery 
188996eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_PHYS] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
1890397cd31fSPeter Maydell                                                arm_gt_ptimer_cb, cpu);
189196eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_VIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
1892397cd31fSPeter Maydell                                                arm_gt_vtimer_cb, cpu);
189396eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_HYP] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
1894397cd31fSPeter Maydell                                               arm_gt_htimer_cb, cpu);
189596eec6b2SAndrew Jeffery         cpu->gt_timer[GTIMER_SEC] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
1896397cd31fSPeter Maydell                                               arm_gt_stimer_cb, cpu);
18978c94b071SRichard Henderson         cpu->gt_timer[GTIMER_HYPVIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
18988c94b071SRichard Henderson                                                   arm_gt_hvtimer_cb, cpu);
189996eec6b2SAndrew Jeffery     }
190095f87565SPeter Maydell #endif
190195f87565SPeter Maydell 
1902fcf5ef2aSThomas Huth     cpu_exec_realizefn(cs, &local_err);
1903fcf5ef2aSThomas Huth     if (local_err != NULL) {
1904fcf5ef2aSThomas Huth         error_propagate(errp, local_err);
1905fcf5ef2aSThomas Huth         return;
1906fcf5ef2aSThomas Huth     }
1907fcf5ef2aSThomas Huth 
19080df9142dSAndrew Jones     arm_cpu_finalize_features(cpu, &local_err);
19090df9142dSAndrew Jones     if (local_err != NULL) {
19100df9142dSAndrew Jones         error_propagate(errp, local_err);
19110df9142dSAndrew Jones         return;
19120df9142dSAndrew Jones     }
19130df9142dSAndrew Jones 
19149719f125SJohn Högberg #ifdef CONFIG_USER_ONLY
19159719f125SJohn Högberg     /*
19169719f125SJohn Högberg      * User mode relies on IC IVAU instructions to catch modification of
19179719f125SJohn Högberg      * dual-mapped code.
19189719f125SJohn Högberg      *
19199719f125SJohn Högberg      * Clear CTR_EL0.DIC to ensure that software that honors these flags uses
19209719f125SJohn Högberg      * IC IVAU even if the emulated processor does not normally require it.
19219719f125SJohn Högberg      */
19229719f125SJohn Högberg     cpu->ctr = FIELD_DP64(cpu->ctr, CTR_EL0, DIC, 0);
19239719f125SJohn Högberg #endif
19249719f125SJohn Högberg 
192597a28b0eSPeter Maydell     if (arm_feature(env, ARM_FEATURE_AARCH64) &&
192697a28b0eSPeter Maydell         cpu->has_vfp != cpu->has_neon) {
192797a28b0eSPeter Maydell         /*
192897a28b0eSPeter Maydell          * This is an architectural requirement for AArch64; AArch32 is
192997a28b0eSPeter Maydell          * more flexible and permits VFP-no-Neon and Neon-no-VFP.
193097a28b0eSPeter Maydell          */
193197a28b0eSPeter Maydell         error_setg(errp,
193297a28b0eSPeter Maydell                    "AArch64 CPUs must have both VFP and Neon or neither");
193397a28b0eSPeter Maydell         return;
193497a28b0eSPeter Maydell     }
193597a28b0eSPeter Maydell 
193642bea956SCédric Le Goater     if (cpu->has_vfp_d32 != cpu->has_neon) {
193742bea956SCédric Le Goater         error_setg(errp, "ARM CPUs must have both VFP-D32 and Neon or neither");
193842bea956SCédric Le Goater         return;
193942bea956SCédric Le Goater     }
194042bea956SCédric Le Goater 
194142bea956SCédric Le Goater    if (!cpu->has_vfp_d32) {
194242bea956SCédric Le Goater         uint32_t u;
194342bea956SCédric Le Goater 
194442bea956SCédric Le Goater         u = cpu->isar.mvfr0;
194542bea956SCédric Le Goater         u = FIELD_DP32(u, MVFR0, SIMDREG, 1); /* 16 registers */
194642bea956SCédric Le Goater         cpu->isar.mvfr0 = u;
194742bea956SCédric Le Goater     }
194842bea956SCédric Le Goater 
194997a28b0eSPeter Maydell     if (!cpu->has_vfp) {
195097a28b0eSPeter Maydell         uint64_t t;
195197a28b0eSPeter Maydell         uint32_t u;
195297a28b0eSPeter Maydell 
195397a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar1;
195497a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR1, JSCVT, 0);
195597a28b0eSPeter Maydell         cpu->isar.id_aa64isar1 = t;
195697a28b0eSPeter Maydell 
195797a28b0eSPeter Maydell         t = cpu->isar.id_aa64pfr0;
195897a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64PFR0, FP, 0xf);
195997a28b0eSPeter Maydell         cpu->isar.id_aa64pfr0 = t;
196097a28b0eSPeter Maydell 
196197a28b0eSPeter Maydell         u = cpu->isar.id_isar6;
196297a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR6, JSCVT, 0);
19633c93dfa4SRichard Henderson         u = FIELD_DP32(u, ID_ISAR6, BF16, 0);
196497a28b0eSPeter Maydell         cpu->isar.id_isar6 = u;
196597a28b0eSPeter Maydell 
196697a28b0eSPeter Maydell         u = cpu->isar.mvfr0;
196797a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPSP, 0);
196897a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPDP, 0);
196997a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPDIVIDE, 0);
197097a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPSQRT, 0);
197197a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, FPROUND, 0);
1972532a3af5SPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
1973532a3af5SPeter Maydell             u = FIELD_DP32(u, MVFR0, FPTRAP, 0);
1974532a3af5SPeter Maydell             u = FIELD_DP32(u, MVFR0, FPSHVEC, 0);
1975532a3af5SPeter Maydell         }
197697a28b0eSPeter Maydell         cpu->isar.mvfr0 = u;
197797a28b0eSPeter Maydell 
197897a28b0eSPeter Maydell         u = cpu->isar.mvfr1;
197997a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR1, FPFTZ, 0);
198097a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR1, FPDNAN, 0);
198197a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR1, FPHP, 0);
1982532a3af5SPeter Maydell         if (arm_feature(env, ARM_FEATURE_M)) {
1983532a3af5SPeter Maydell             u = FIELD_DP32(u, MVFR1, FP16, 0);
1984532a3af5SPeter Maydell         }
198597a28b0eSPeter Maydell         cpu->isar.mvfr1 = u;
198697a28b0eSPeter Maydell 
198797a28b0eSPeter Maydell         u = cpu->isar.mvfr2;
198897a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR2, FPMISC, 0);
198997a28b0eSPeter Maydell         cpu->isar.mvfr2 = u;
199097a28b0eSPeter Maydell     }
199197a28b0eSPeter Maydell 
199297a28b0eSPeter Maydell     if (!cpu->has_neon) {
199397a28b0eSPeter Maydell         uint64_t t;
199497a28b0eSPeter Maydell         uint32_t u;
199597a28b0eSPeter Maydell 
199697a28b0eSPeter Maydell         unset_feature(env, ARM_FEATURE_NEON);
199797a28b0eSPeter Maydell 
199897a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar0;
1999eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, AES, 0);
2000eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SHA1, 0);
2001eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SHA2, 0);
2002eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SHA3, 0);
2003eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SM3, 0);
2004eb851c11SDamien Hedde         t = FIELD_DP64(t, ID_AA64ISAR0, SM4, 0);
200597a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR0, DP, 0);
200697a28b0eSPeter Maydell         cpu->isar.id_aa64isar0 = t;
200797a28b0eSPeter Maydell 
200897a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar1;
200997a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR1, FCMA, 0);
20103c93dfa4SRichard Henderson         t = FIELD_DP64(t, ID_AA64ISAR1, BF16, 0);
2011f8680aaaSRichard Henderson         t = FIELD_DP64(t, ID_AA64ISAR1, I8MM, 0);
201297a28b0eSPeter Maydell         cpu->isar.id_aa64isar1 = t;
201397a28b0eSPeter Maydell 
201497a28b0eSPeter Maydell         t = cpu->isar.id_aa64pfr0;
201597a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64PFR0, ADVSIMD, 0xf);
201697a28b0eSPeter Maydell         cpu->isar.id_aa64pfr0 = t;
201797a28b0eSPeter Maydell 
201897a28b0eSPeter Maydell         u = cpu->isar.id_isar5;
2019eb851c11SDamien Hedde         u = FIELD_DP32(u, ID_ISAR5, AES, 0);
2020eb851c11SDamien Hedde         u = FIELD_DP32(u, ID_ISAR5, SHA1, 0);
2021eb851c11SDamien Hedde         u = FIELD_DP32(u, ID_ISAR5, SHA2, 0);
202297a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR5, RDM, 0);
202397a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR5, VCMA, 0);
202497a28b0eSPeter Maydell         cpu->isar.id_isar5 = u;
202597a28b0eSPeter Maydell 
202697a28b0eSPeter Maydell         u = cpu->isar.id_isar6;
202797a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR6, DP, 0);
202897a28b0eSPeter Maydell         u = FIELD_DP32(u, ID_ISAR6, FHM, 0);
20293c93dfa4SRichard Henderson         u = FIELD_DP32(u, ID_ISAR6, BF16, 0);
2030f8680aaaSRichard Henderson         u = FIELD_DP32(u, ID_ISAR6, I8MM, 0);
203197a28b0eSPeter Maydell         cpu->isar.id_isar6 = u;
203297a28b0eSPeter Maydell 
2033532a3af5SPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
203497a28b0eSPeter Maydell             u = cpu->isar.mvfr1;
203597a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDLS, 0);
203697a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDINT, 0);
203797a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDSP, 0);
203897a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR1, SIMDHP, 0);
203997a28b0eSPeter Maydell             cpu->isar.mvfr1 = u;
204097a28b0eSPeter Maydell 
204197a28b0eSPeter Maydell             u = cpu->isar.mvfr2;
204297a28b0eSPeter Maydell             u = FIELD_DP32(u, MVFR2, SIMDMISC, 0);
204397a28b0eSPeter Maydell             cpu->isar.mvfr2 = u;
204497a28b0eSPeter Maydell         }
2045532a3af5SPeter Maydell     }
204697a28b0eSPeter Maydell 
204797a28b0eSPeter Maydell     if (!cpu->has_neon && !cpu->has_vfp) {
204897a28b0eSPeter Maydell         uint64_t t;
204997a28b0eSPeter Maydell         uint32_t u;
205097a28b0eSPeter Maydell 
205197a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar0;
205297a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR0, FHM, 0);
205397a28b0eSPeter Maydell         cpu->isar.id_aa64isar0 = t;
205497a28b0eSPeter Maydell 
205597a28b0eSPeter Maydell         t = cpu->isar.id_aa64isar1;
205697a28b0eSPeter Maydell         t = FIELD_DP64(t, ID_AA64ISAR1, FRINTTS, 0);
205797a28b0eSPeter Maydell         cpu->isar.id_aa64isar1 = t;
205897a28b0eSPeter Maydell 
205997a28b0eSPeter Maydell         u = cpu->isar.mvfr0;
206097a28b0eSPeter Maydell         u = FIELD_DP32(u, MVFR0, SIMDREG, 0);
206197a28b0eSPeter Maydell         cpu->isar.mvfr0 = u;
2062c52881bbSRichard Henderson 
2063c52881bbSRichard Henderson         /* Despite the name, this field covers both VFP and Neon */
2064c52881bbSRichard Henderson         u = cpu->isar.mvfr1;
2065c52881bbSRichard Henderson         u = FIELD_DP32(u, MVFR1, SIMDFMAC, 0);
2066c52881bbSRichard Henderson         cpu->isar.mvfr1 = u;
206797a28b0eSPeter Maydell     }
206897a28b0eSPeter Maydell 
2069ea90db0aSPeter Maydell     if (arm_feature(env, ARM_FEATURE_M) && !cpu->has_dsp) {
2070ea90db0aSPeter Maydell         uint32_t u;
2071ea90db0aSPeter Maydell 
2072ea90db0aSPeter Maydell         unset_feature(env, ARM_FEATURE_THUMB_DSP);
2073ea90db0aSPeter Maydell 
2074ea90db0aSPeter Maydell         u = cpu->isar.id_isar1;
2075ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR1, EXTEND, 1);
2076ea90db0aSPeter Maydell         cpu->isar.id_isar1 = u;
2077ea90db0aSPeter Maydell 
2078ea90db0aSPeter Maydell         u = cpu->isar.id_isar2;
2079ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR2, MULTU, 1);
2080ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR2, MULTS, 1);
2081ea90db0aSPeter Maydell         cpu->isar.id_isar2 = u;
2082ea90db0aSPeter Maydell 
2083ea90db0aSPeter Maydell         u = cpu->isar.id_isar3;
2084ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR3, SIMD, 1);
2085ea90db0aSPeter Maydell         u = FIELD_DP32(u, ID_ISAR3, SATURATE, 0);
2086ea90db0aSPeter Maydell         cpu->isar.id_isar3 = u;
2087ea90db0aSPeter Maydell     }
2088ea90db0aSPeter Maydell 
2089fcf5ef2aSThomas Huth 
2090ea7ac69dSPeter Maydell     /*
2091ea7ac69dSPeter Maydell      * We rely on no XScale CPU having VFP so we can use the same bits in the
2092ea7ac69dSPeter Maydell      * TB flags field for VECSTRIDE and XSCALE_CPAR.
2093ea7ac69dSPeter Maydell      */
20947d63183fSRichard Henderson     assert(arm_feature(&cpu->env, ARM_FEATURE_AARCH64) ||
20957d63183fSRichard Henderson            !cpu_isar_feature(aa32_vfp_simd, cpu) ||
20967d63183fSRichard Henderson            !arm_feature(env, ARM_FEATURE_XSCALE));
2097ea7ac69dSPeter Maydell 
2098fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_V7) &&
2099fcf5ef2aSThomas Huth         !arm_feature(env, ARM_FEATURE_M) &&
2100452a0955SPeter Maydell         !arm_feature(env, ARM_FEATURE_PMSA)) {
2101fcf5ef2aSThomas Huth         /* v7VMSA drops support for the old ARMv5 tiny pages, so we
2102fcf5ef2aSThomas Huth          * can use 4K pages.
2103fcf5ef2aSThomas Huth          */
2104fcf5ef2aSThomas Huth         pagebits = 12;
2105fcf5ef2aSThomas Huth     } else {
2106fcf5ef2aSThomas Huth         /* For CPUs which might have tiny 1K pages, or which have an
2107fcf5ef2aSThomas Huth          * MPU and might have small region sizes, stick with 1K pages.
2108fcf5ef2aSThomas Huth          */
2109fcf5ef2aSThomas Huth         pagebits = 10;
2110fcf5ef2aSThomas Huth     }
2111fcf5ef2aSThomas Huth     if (!set_preferred_target_page_bits(pagebits)) {
2112fcf5ef2aSThomas Huth         /* This can only ever happen for hotplugging a CPU, or if
2113fcf5ef2aSThomas Huth          * the board code incorrectly creates a CPU which it has
2114fcf5ef2aSThomas Huth          * promised via minimum_page_size that it will not.
2115fcf5ef2aSThomas Huth          */
2116fcf5ef2aSThomas Huth         error_setg(errp, "This CPU requires a smaller page size than the "
2117fcf5ef2aSThomas Huth                    "system is using");
2118fcf5ef2aSThomas Huth         return;
2119fcf5ef2aSThomas Huth     }
2120fcf5ef2aSThomas Huth 
2121fcf5ef2aSThomas Huth     /* This cpu-id-to-MPIDR affinity is used only for TCG; KVM will override it.
2122fcf5ef2aSThomas Huth      * We don't support setting cluster ID ([16..23]) (known as Aff2
2123fcf5ef2aSThomas Huth      * in later ARM ARM versions), or any of the higher affinity level fields,
2124fcf5ef2aSThomas Huth      * so these bits always RAZ.
2125fcf5ef2aSThomas Huth      */
2126fcf5ef2aSThomas Huth     if (cpu->mp_affinity == ARM64_AFFINITY_INVALID) {
2127750245edSRichard Henderson         cpu->mp_affinity = arm_build_mp_affinity(cs->cpu_index,
212846de5913SIgor Mammedov                                                  ARM_DEFAULT_CPUS_PER_CLUSTER);
2129fcf5ef2aSThomas Huth     }
2130fcf5ef2aSThomas Huth 
2131fcf5ef2aSThomas Huth     if (cpu->reset_hivecs) {
2132fcf5ef2aSThomas Huth             cpu->reset_sctlr |= (1 << 13);
2133fcf5ef2aSThomas Huth     }
2134fcf5ef2aSThomas Huth 
21353a062d57SJulian Brown     if (cpu->cfgend) {
21363a062d57SJulian Brown         if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
21373a062d57SJulian Brown             cpu->reset_sctlr |= SCTLR_EE;
21383a062d57SJulian Brown         } else {
21393a062d57SJulian Brown             cpu->reset_sctlr |= SCTLR_B;
21403a062d57SJulian Brown         }
21413a062d57SJulian Brown     }
21423a062d57SJulian Brown 
214340188188SPeter Maydell     if (!arm_feature(env, ARM_FEATURE_M) && !cpu->has_el3) {
2144fcf5ef2aSThomas Huth         /* If the has_el3 CPU property is disabled then we need to disable the
2145fcf5ef2aSThomas Huth          * feature.
2146fcf5ef2aSThomas Huth          */
2147fcf5ef2aSThomas Huth         unset_feature(env, ARM_FEATURE_EL3);
2148fcf5ef2aSThomas Huth 
2149b13c91c0SRichard Henderson         /*
2150b13c91c0SRichard Henderson          * Disable the security extension feature bits in the processor
2151b13c91c0SRichard Henderson          * feature registers as well.
2152fcf5ef2aSThomas Huth          */
2153b13c91c0SRichard Henderson         cpu->isar.id_pfr1 = FIELD_DP32(cpu->isar.id_pfr1, ID_PFR1, SECURITY, 0);
2154033a4f15SRichard Henderson         cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, COPSDBG, 0);
2155b13c91c0SRichard Henderson         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2156b13c91c0SRichard Henderson                                            ID_AA64PFR0, EL3, 0);
2157b9f335c2SRichard Henderson 
2158b9f335c2SRichard Henderson         /* Disable the realm management extension, which requires EL3. */
2159b9f335c2SRichard Henderson         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2160b9f335c2SRichard Henderson                                            ID_AA64PFR0, RME, 0);
2161fcf5ef2aSThomas Huth     }
2162fcf5ef2aSThomas Huth 
2163c25bd18aSPeter Maydell     if (!cpu->has_el2) {
2164c25bd18aSPeter Maydell         unset_feature(env, ARM_FEATURE_EL2);
2165c25bd18aSPeter Maydell     }
2166c25bd18aSPeter Maydell 
2167d6f02ce3SWei Huang     if (!cpu->has_pmu) {
2168fcf5ef2aSThomas Huth         unset_feature(env, ARM_FEATURE_PMU);
216957a4a11bSAaron Lindsay     }
217057a4a11bSAaron Lindsay     if (arm_feature(env, ARM_FEATURE_PMU)) {
2171bf8d0969SAaron Lindsay OS         pmu_init(cpu);
217257a4a11bSAaron Lindsay 
217357a4a11bSAaron Lindsay         if (!kvm_enabled()) {
2174033614c4SAaron Lindsay             arm_register_pre_el_change_hook(cpu, &pmu_pre_el_change, 0);
2175033614c4SAaron Lindsay             arm_register_el_change_hook(cpu, &pmu_post_el_change, 0);
2176fcf5ef2aSThomas Huth         }
21774e7beb0cSAaron Lindsay OS 
21784e7beb0cSAaron Lindsay OS #ifndef CONFIG_USER_ONLY
21794e7beb0cSAaron Lindsay OS         cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, arm_pmu_timer_cb,
21804e7beb0cSAaron Lindsay OS                 cpu);
21814e7beb0cSAaron Lindsay OS #endif
218257a4a11bSAaron Lindsay     } else {
21832a609df8SPeter Maydell         cpu->isar.id_aa64dfr0 =
21842a609df8SPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMUVER, 0);
2185a6179538SPeter Maydell         cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, PERFMON, 0);
218657a4a11bSAaron Lindsay         cpu->pmceid0 = 0;
218757a4a11bSAaron Lindsay         cpu->pmceid1 = 0;
218857a4a11bSAaron Lindsay     }
2189fcf5ef2aSThomas Huth 
2190fcf5ef2aSThomas Huth     if (!arm_feature(env, ARM_FEATURE_EL2)) {
2191b13c91c0SRichard Henderson         /*
2192b13c91c0SRichard Henderson          * Disable the hypervisor feature bits in the processor feature
2193b13c91c0SRichard Henderson          * registers if we don't have EL2.
2194fcf5ef2aSThomas Huth          */
2195b13c91c0SRichard Henderson         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2196b13c91c0SRichard Henderson                                            ID_AA64PFR0, EL2, 0);
2197b13c91c0SRichard Henderson         cpu->isar.id_pfr1 = FIELD_DP32(cpu->isar.id_pfr1,
2198b13c91c0SRichard Henderson                                        ID_PFR1, VIRTUALIZATION, 0);
2199fcf5ef2aSThomas Huth     }
2200fcf5ef2aSThomas Huth 
22017134cb07SRichard Henderson     if (cpu_isar_feature(aa64_mte, cpu)) {
22027134cb07SRichard Henderson         /*
22037134cb07SRichard Henderson          * The architectural range of GM blocksize is 2-6, however qemu
22047134cb07SRichard Henderson          * doesn't support blocksize of 2 (see HELPER(ldgm)).
22057134cb07SRichard Henderson          */
22067134cb07SRichard Henderson         if (tcg_enabled()) {
22077134cb07SRichard Henderson             assert(cpu->gm_blocksize >= 3 && cpu->gm_blocksize <= 6);
22087134cb07SRichard Henderson         }
22097134cb07SRichard Henderson 
22106f4e1405SRichard Henderson #ifndef CONFIG_USER_ONLY
22116f4e1405SRichard Henderson         /*
2212cd305b5fSRichard Henderson          * If we do not have tag-memory provided by the machine,
2213cd305b5fSRichard Henderson          * reduce MTE support to instructions enabled at EL0.
2214cd305b5fSRichard Henderson          * This matches Cortex-A710 BROADCASTMTE input being LOW.
22156f4e1405SRichard Henderson          */
22167134cb07SRichard Henderson         if (cpu->tag_memory == NULL) {
22176f4e1405SRichard Henderson             cpu->isar.id_aa64pfr1 =
2218cd305b5fSRichard Henderson                 FIELD_DP64(cpu->isar.id_aa64pfr1, ID_AA64PFR1, MTE, 1);
22196f4e1405SRichard Henderson         }
22206f4e1405SRichard Henderson #endif
22217134cb07SRichard Henderson     }
22226f4e1405SRichard Henderson 
22232daf518dSPeter Maydell     if (tcg_enabled()) {
22242daf518dSPeter Maydell         /*
22257d8c283eSPeter Maydell          * Don't report some architectural features in the ID registers
22267d8c283eSPeter Maydell          * where TCG does not yet implement it (not even a minimal
22277d8c283eSPeter Maydell          * stub version). This avoids guests falling over when they
22287d8c283eSPeter Maydell          * try to access the non-existent system registers for them.
22292daf518dSPeter Maydell          */
22307d8c283eSPeter Maydell         /* FEAT_SPE (Statistical Profiling Extension) */
22312daf518dSPeter Maydell         cpu->isar.id_aa64dfr0 =
22322daf518dSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMSVER, 0);
22333d5f45ecSRichard Henderson         /* FEAT_TRBE (Trace Buffer Extension) */
22343d5f45ecSRichard Henderson         cpu->isar.id_aa64dfr0 =
22353d5f45ecSRichard Henderson             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEBUFFER, 0);
22367d8c283eSPeter Maydell         /* FEAT_TRF (Self-hosted Trace Extension) */
22377d8c283eSPeter Maydell         cpu->isar.id_aa64dfr0 =
22387d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEFILT, 0);
22397d8c283eSPeter Maydell         cpu->isar.id_dfr0 =
22407d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, TRACEFILT, 0);
22417d8c283eSPeter Maydell         /* Trace Macrocell system register access */
22427d8c283eSPeter Maydell         cpu->isar.id_aa64dfr0 =
22437d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEVER, 0);
22447d8c283eSPeter Maydell         cpu->isar.id_dfr0 =
22457d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, COPTRC, 0);
22467d8c283eSPeter Maydell         /* Memory mapped trace */
22477d8c283eSPeter Maydell         cpu->isar.id_dfr0 =
22487d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, MMAPTRC, 0);
22497d8c283eSPeter Maydell         /* FEAT_AMU (Activity Monitors Extension) */
22507d8c283eSPeter Maydell         cpu->isar.id_aa64pfr0 =
22517d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64pfr0, ID_AA64PFR0, AMU, 0);
22527d8c283eSPeter Maydell         cpu->isar.id_pfr0 =
22537d8c283eSPeter Maydell             FIELD_DP32(cpu->isar.id_pfr0, ID_PFR0, AMU, 0);
22547d8c283eSPeter Maydell         /* FEAT_MPAM (Memory Partitioning and Monitoring Extension) */
22557d8c283eSPeter Maydell         cpu->isar.id_aa64pfr0 =
22567d8c283eSPeter Maydell             FIELD_DP64(cpu->isar.id_aa64pfr0, ID_AA64PFR0, MPAM, 0);
22572daf518dSPeter Maydell     }
22582daf518dSPeter Maydell 
2259f50cd314SPeter Maydell     /* MPU can be configured out of a PMSA CPU either by setting has-mpu
2260f50cd314SPeter Maydell      * to false or by setting pmsav7-dregion to 0.
2261f50cd314SPeter Maydell      */
2262761c4642STobias Röhmel     if (!cpu->has_mpu || cpu->pmsav7_dregion == 0) {
2263f50cd314SPeter Maydell         cpu->has_mpu = false;
2264761c4642STobias Röhmel         cpu->pmsav7_dregion = 0;
2265761c4642STobias Röhmel         cpu->pmsav8r_hdregion = 0;
2266fcf5ef2aSThomas Huth     }
2267fcf5ef2aSThomas Huth 
2268452a0955SPeter Maydell     if (arm_feature(env, ARM_FEATURE_PMSA) &&
2269fcf5ef2aSThomas Huth         arm_feature(env, ARM_FEATURE_V7)) {
2270fcf5ef2aSThomas Huth         uint32_t nr = cpu->pmsav7_dregion;
2271fcf5ef2aSThomas Huth 
2272fcf5ef2aSThomas Huth         if (nr > 0xff) {
2273fcf5ef2aSThomas Huth             error_setg(errp, "PMSAv7 MPU #regions invalid %" PRIu32, nr);
2274fcf5ef2aSThomas Huth             return;
2275fcf5ef2aSThomas Huth         }
2276fcf5ef2aSThomas Huth 
2277fcf5ef2aSThomas Huth         if (nr) {
22780e1a46bbSPeter Maydell             if (arm_feature(env, ARM_FEATURE_V8)) {
22790e1a46bbSPeter Maydell                 /* PMSAv8 */
228062c58ee0SPeter Maydell                 env->pmsav8.rbar[M_REG_NS] = g_new0(uint32_t, nr);
228162c58ee0SPeter Maydell                 env->pmsav8.rlar[M_REG_NS] = g_new0(uint32_t, nr);
228262c58ee0SPeter Maydell                 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
228362c58ee0SPeter Maydell                     env->pmsav8.rbar[M_REG_S] = g_new0(uint32_t, nr);
228462c58ee0SPeter Maydell                     env->pmsav8.rlar[M_REG_S] = g_new0(uint32_t, nr);
228562c58ee0SPeter Maydell                 }
22860e1a46bbSPeter Maydell             } else {
2287fcf5ef2aSThomas Huth                 env->pmsav7.drbar = g_new0(uint32_t, nr);
2288fcf5ef2aSThomas Huth                 env->pmsav7.drsr = g_new0(uint32_t, nr);
2289fcf5ef2aSThomas Huth                 env->pmsav7.dracr = g_new0(uint32_t, nr);
2290fcf5ef2aSThomas Huth             }
2291fcf5ef2aSThomas Huth         }
2292761c4642STobias Röhmel 
2293761c4642STobias Röhmel         if (cpu->pmsav8r_hdregion > 0xff) {
2294761c4642STobias Röhmel             error_setg(errp, "PMSAv8 MPU EL2 #regions invalid %" PRIu32,
2295761c4642STobias Röhmel                               cpu->pmsav8r_hdregion);
2296761c4642STobias Röhmel             return;
2297761c4642STobias Röhmel         }
2298761c4642STobias Röhmel 
2299761c4642STobias Röhmel         if (cpu->pmsav8r_hdregion) {
2300761c4642STobias Röhmel             env->pmsav8.hprbar = g_new0(uint32_t,
2301761c4642STobias Röhmel                                         cpu->pmsav8r_hdregion);
2302761c4642STobias Röhmel             env->pmsav8.hprlar = g_new0(uint32_t,
2303761c4642STobias Röhmel                                         cpu->pmsav8r_hdregion);
2304761c4642STobias Röhmel         }
23050e1a46bbSPeter Maydell     }
2306fcf5ef2aSThomas Huth 
23079901c576SPeter Maydell     if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
23089901c576SPeter Maydell         uint32_t nr = cpu->sau_sregion;
23099901c576SPeter Maydell 
23109901c576SPeter Maydell         if (nr > 0xff) {
23119901c576SPeter Maydell             error_setg(errp, "v8M SAU #regions invalid %" PRIu32, nr);
23129901c576SPeter Maydell             return;
23139901c576SPeter Maydell         }
23149901c576SPeter Maydell 
23159901c576SPeter Maydell         if (nr) {
23169901c576SPeter Maydell             env->sau.rbar = g_new0(uint32_t, nr);
23179901c576SPeter Maydell             env->sau.rlar = g_new0(uint32_t, nr);
23189901c576SPeter Maydell         }
23199901c576SPeter Maydell     }
23209901c576SPeter Maydell 
232191db4642SCédric Le Goater     if (arm_feature(env, ARM_FEATURE_EL3)) {
232291db4642SCédric Le Goater         set_feature(env, ARM_FEATURE_VBAR);
232391db4642SCédric Le Goater     }
232491db4642SCédric Le Goater 
2325f6fc36deSJean-Philippe Brucker #ifndef CONFIG_USER_ONLY
2326f6fc36deSJean-Philippe Brucker     if (tcg_enabled() && cpu_isar_feature(aa64_rme, cpu)) {
2327f6fc36deSJean-Philippe Brucker         arm_register_el_change_hook(cpu, &gt_rme_post_el_change, 0);
2328f6fc36deSJean-Philippe Brucker     }
2329f6fc36deSJean-Philippe Brucker #endif
2330f6fc36deSJean-Philippe Brucker 
2331fcf5ef2aSThomas Huth     register_cp_regs_for_features(cpu);
2332fcf5ef2aSThomas Huth     arm_cpu_register_gdb_regs_for_features(cpu);
2333fcf5ef2aSThomas Huth 
2334fcf5ef2aSThomas Huth     init_cpreg_list(cpu);
2335fcf5ef2aSThomas Huth 
2336fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
2337cc7d44c2SLike Xu     MachineState *ms = MACHINE(qdev_get_machine());
2338cc7d44c2SLike Xu     unsigned int smp_cpus = ms->smp.cpus;
23398bce44a2SRichard Henderson     bool has_secure = cpu->has_el3 || arm_feature(env, ARM_FEATURE_M_SECURITY);
2340cc7d44c2SLike Xu 
23418bce44a2SRichard Henderson     /*
23428bce44a2SRichard Henderson      * We must set cs->num_ases to the final value before
23438bce44a2SRichard Henderson      * the first call to cpu_address_space_init.
23448bce44a2SRichard Henderson      */
23458bce44a2SRichard Henderson     if (cpu->tag_memory != NULL) {
23468bce44a2SRichard Henderson         cs->num_ases = 3 + has_secure;
23478bce44a2SRichard Henderson     } else {
23488bce44a2SRichard Henderson         cs->num_ases = 1 + has_secure;
23498bce44a2SRichard Henderson     }
23501d2091bcSPeter Maydell 
23518bce44a2SRichard Henderson     if (has_secure) {
2352fcf5ef2aSThomas Huth         if (!cpu->secure_memory) {
2353fcf5ef2aSThomas Huth             cpu->secure_memory = cs->memory;
2354fcf5ef2aSThomas Huth         }
235580ceb07aSPeter Xu         cpu_address_space_init(cs, ARMASIdx_S, "cpu-secure-memory",
235680ceb07aSPeter Xu                                cpu->secure_memory);
2357fcf5ef2aSThomas Huth     }
23588bce44a2SRichard Henderson 
23598bce44a2SRichard Henderson     if (cpu->tag_memory != NULL) {
23608bce44a2SRichard Henderson         cpu_address_space_init(cs, ARMASIdx_TagNS, "cpu-tag-memory",
23618bce44a2SRichard Henderson                                cpu->tag_memory);
23628bce44a2SRichard Henderson         if (has_secure) {
23638bce44a2SRichard Henderson             cpu_address_space_init(cs, ARMASIdx_TagS, "cpu-tag-memory",
23648bce44a2SRichard Henderson                                    cpu->secure_tag_memory);
23658bce44a2SRichard Henderson         }
23668bce44a2SRichard Henderson     }
23678bce44a2SRichard Henderson 
236880ceb07aSPeter Xu     cpu_address_space_init(cs, ARMASIdx_NS, "cpu-memory", cs->memory);
2369f9a69711SAlistair Francis 
2370f9a69711SAlistair Francis     /* No core_count specified, default to smp_cpus. */
2371f9a69711SAlistair Francis     if (cpu->core_count == -1) {
2372f9a69711SAlistair Francis         cpu->core_count = smp_cpus;
2373f9a69711SAlistair Francis     }
2374fcf5ef2aSThomas Huth #endif
2375fcf5ef2aSThomas Huth 
2376a4157b80SRichard Henderson     if (tcg_enabled()) {
2377a4157b80SRichard Henderson         int dcz_blocklen = 4 << cpu->dcz_blocksize;
2378a4157b80SRichard Henderson 
2379a4157b80SRichard Henderson         /*
2380a4157b80SRichard Henderson          * We only support DCZ blocklen that fits on one page.
2381a4157b80SRichard Henderson          *
2382a4157b80SRichard Henderson          * Architectually this is always true.  However TARGET_PAGE_SIZE
2383a4157b80SRichard Henderson          * is variable and, for compatibility with -machine virt-2.7,
2384a4157b80SRichard Henderson          * is only 1KiB, as an artifact of legacy ARMv5 subpage support.
2385a4157b80SRichard Henderson          * But even then, while the largest architectural DCZ blocklen
2386a4157b80SRichard Henderson          * is 2KiB, no cpu actually uses such a large blocklen.
2387a4157b80SRichard Henderson          */
2388a4157b80SRichard Henderson         assert(dcz_blocklen <= TARGET_PAGE_SIZE);
2389a4157b80SRichard Henderson 
2390a4157b80SRichard Henderson         /*
2391a4157b80SRichard Henderson          * We only support DCZ blocksize >= 2*TAG_GRANULE, which is to say
2392a4157b80SRichard Henderson          * both nibbles of each byte storing tag data may be written at once.
2393a4157b80SRichard Henderson          * Since TAG_GRANULE is 16, this means that blocklen must be >= 32.
2394a4157b80SRichard Henderson          */
2395a4157b80SRichard Henderson         if (cpu_isar_feature(aa64_mte, cpu)) {
2396a4157b80SRichard Henderson             assert(dcz_blocklen >= 2 * TAG_GRANULE);
2397a4157b80SRichard Henderson         }
2398a4157b80SRichard Henderson     }
2399a4157b80SRichard Henderson 
2400fcf5ef2aSThomas Huth     qemu_init_vcpu(cs);
2401fcf5ef2aSThomas Huth     cpu_reset(cs);
2402fcf5ef2aSThomas Huth 
2403fcf5ef2aSThomas Huth     acc->parent_realize(dev, errp);
2404fcf5ef2aSThomas Huth }
2405fcf5ef2aSThomas Huth 
2406fcf5ef2aSThomas Huth static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
2407fcf5ef2aSThomas Huth {
2408fcf5ef2aSThomas Huth     ObjectClass *oc;
2409fcf5ef2aSThomas Huth     char *typename;
2410fcf5ef2aSThomas Huth     char **cpuname;
2411a0032cc5SPeter Maydell     const char *cpunamestr;
2412fcf5ef2aSThomas Huth 
2413fcf5ef2aSThomas Huth     cpuname = g_strsplit(cpu_model, ",", 1);
2414a0032cc5SPeter Maydell     cpunamestr = cpuname[0];
2415a0032cc5SPeter Maydell #ifdef CONFIG_USER_ONLY
2416a0032cc5SPeter Maydell     /* For backwards compatibility usermode emulation allows "-cpu any",
2417a0032cc5SPeter Maydell      * which has the same semantics as "-cpu max".
2418a0032cc5SPeter Maydell      */
2419a0032cc5SPeter Maydell     if (!strcmp(cpunamestr, "any")) {
2420a0032cc5SPeter Maydell         cpunamestr = "max";
2421a0032cc5SPeter Maydell     }
2422a0032cc5SPeter Maydell #endif
2423a0032cc5SPeter Maydell     typename = g_strdup_printf(ARM_CPU_TYPE_NAME("%s"), cpunamestr);
2424fcf5ef2aSThomas Huth     oc = object_class_by_name(typename);
2425fcf5ef2aSThomas Huth     g_strfreev(cpuname);
2426fcf5ef2aSThomas Huth     g_free(typename);
2427d5be19f5SPhilippe Mathieu-Daudé 
2428fcf5ef2aSThomas Huth     return oc;
2429fcf5ef2aSThomas Huth }
2430fcf5ef2aSThomas Huth 
2431fcf5ef2aSThomas Huth static Property arm_cpu_properties[] = {
2432e544f800SPhilippe Mathieu-Daudé     DEFINE_PROP_UINT64("midr", ARMCPU, midr, 0),
2433fcf5ef2aSThomas Huth     DEFINE_PROP_UINT64("mp-affinity", ARMCPU,
2434fcf5ef2aSThomas Huth                         mp_affinity, ARM64_AFFINITY_INVALID),
243515f8b142SIgor Mammedov     DEFINE_PROP_INT32("node-id", ARMCPU, node_id, CPU_UNSET_NUMA_NODE_ID),
2436f9a69711SAlistair Francis     DEFINE_PROP_INT32("core-count", ARMCPU, core_count, -1),
2437fcf5ef2aSThomas Huth     DEFINE_PROP_END_OF_LIST()
2438fcf5ef2aSThomas Huth };
2439fcf5ef2aSThomas Huth 
2440a6506838SAkihiko Odaki static const gchar *arm_gdb_arch_name(CPUState *cs)
2441fcf5ef2aSThomas Huth {
2442fcf5ef2aSThomas Huth     ARMCPU *cpu = ARM_CPU(cs);
2443fcf5ef2aSThomas Huth     CPUARMState *env = &cpu->env;
2444fcf5ef2aSThomas Huth 
2445fcf5ef2aSThomas Huth     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
2446a6506838SAkihiko Odaki         return "iwmmxt";
2447fcf5ef2aSThomas Huth     }
2448a6506838SAkihiko Odaki     return "arm";
2449fcf5ef2aSThomas Huth }
2450fcf5ef2aSThomas Huth 
24518b80bd28SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY
24528b80bd28SPhilippe Mathieu-Daudé #include "hw/core/sysemu-cpu-ops.h"
24538b80bd28SPhilippe Mathieu-Daudé 
24548b80bd28SPhilippe Mathieu-Daudé static const struct SysemuCPUOps arm_sysemu_ops = {
245508928c6dSPhilippe Mathieu-Daudé     .get_phys_page_attrs_debug = arm_cpu_get_phys_page_attrs_debug,
2456faf39e82SPhilippe Mathieu-Daudé     .asidx_from_attrs = arm_asidx_from_attrs,
2457715e3c1aSPhilippe Mathieu-Daudé     .write_elf32_note = arm_cpu_write_elf32_note,
2458715e3c1aSPhilippe Mathieu-Daudé     .write_elf64_note = arm_cpu_write_elf64_note,
2459da383e02SPhilippe Mathieu-Daudé     .virtio_is_big_endian = arm_cpu_virtio_is_big_endian,
2460feece4d0SPhilippe Mathieu-Daudé     .legacy_vmsd = &vmstate_arm_cpu,
24618b80bd28SPhilippe Mathieu-Daudé };
24628b80bd28SPhilippe Mathieu-Daudé #endif
24638b80bd28SPhilippe Mathieu-Daudé 
246478271684SClaudio Fontana #ifdef CONFIG_TCG
24651764ad70SRichard Henderson static const TCGCPUOps arm_tcg_ops = {
246678271684SClaudio Fontana     .initialize = arm_translate_init,
246778271684SClaudio Fontana     .synchronize_from_tb = arm_cpu_synchronize_from_tb,
246878271684SClaudio Fontana     .debug_excp_handler = arm_debug_excp_handler,
246956c6c98dSRichard Henderson     .restore_state_to_opc = arm_restore_state_to_opc,
247078271684SClaudio Fontana 
24719b12b6b4SRichard Henderson #ifdef CONFIG_USER_ONLY
24729b12b6b4SRichard Henderson     .record_sigsegv = arm_cpu_record_sigsegv,
247339a099caSRichard Henderson     .record_sigbus = arm_cpu_record_sigbus,
24749b12b6b4SRichard Henderson #else
24759b12b6b4SRichard Henderson     .tlb_fill = arm_cpu_tlb_fill,
2476083afd18SPhilippe Mathieu-Daudé     .cpu_exec_interrupt = arm_cpu_exec_interrupt,
247778271684SClaudio Fontana     .do_interrupt = arm_cpu_do_interrupt,
247878271684SClaudio Fontana     .do_transaction_failed = arm_cpu_do_transaction_failed,
247978271684SClaudio Fontana     .do_unaligned_access = arm_cpu_do_unaligned_access,
248078271684SClaudio Fontana     .adjust_watchpoint_address = arm_adjust_watchpoint_address,
248178271684SClaudio Fontana     .debug_check_watchpoint = arm_debug_check_watchpoint,
2482b00d86bcSRichard Henderson     .debug_check_breakpoint = arm_debug_check_breakpoint,
248378271684SClaudio Fontana #endif /* !CONFIG_USER_ONLY */
248478271684SClaudio Fontana };
248578271684SClaudio Fontana #endif /* CONFIG_TCG */
248678271684SClaudio Fontana 
2487fcf5ef2aSThomas Huth static void arm_cpu_class_init(ObjectClass *oc, void *data)
2488fcf5ef2aSThomas Huth {
2489fcf5ef2aSThomas Huth     ARMCPUClass *acc = ARM_CPU_CLASS(oc);
2490fcf5ef2aSThomas Huth     CPUClass *cc = CPU_CLASS(acc);
2491fcf5ef2aSThomas Huth     DeviceClass *dc = DEVICE_CLASS(oc);
24929130cadeSPeter Maydell     ResettableClass *rc = RESETTABLE_CLASS(oc);
2493fcf5ef2aSThomas Huth 
2494bf853881SPhilippe Mathieu-Daudé     device_class_set_parent_realize(dc, arm_cpu_realizefn,
2495bf853881SPhilippe Mathieu-Daudé                                     &acc->parent_realize);
2496fcf5ef2aSThomas Huth 
24974f67d30bSMarc-André Lureau     device_class_set_props(dc, arm_cpu_properties);
24989130cadeSPeter Maydell 
24999130cadeSPeter Maydell     resettable_class_set_parent_phases(rc, NULL, arm_cpu_reset_hold, NULL,
25009130cadeSPeter Maydell                                        &acc->parent_phases);
2501fcf5ef2aSThomas Huth 
2502fcf5ef2aSThomas Huth     cc->class_by_name = arm_cpu_class_by_name;
2503fcf5ef2aSThomas Huth     cc->has_work = arm_cpu_has_work;
2504fcf5ef2aSThomas Huth     cc->dump_state = arm_cpu_dump_state;
2505fcf5ef2aSThomas Huth     cc->set_pc = arm_cpu_set_pc;
2506e4fdf9dfSRichard Henderson     cc->get_pc = arm_cpu_get_pc;
2507fcf5ef2aSThomas Huth     cc->gdb_read_register = arm_cpu_gdb_read_register;
2508fcf5ef2aSThomas Huth     cc->gdb_write_register = arm_cpu_gdb_write_register;
25097350d553SRichard Henderson #ifndef CONFIG_USER_ONLY
25108b80bd28SPhilippe Mathieu-Daudé     cc->sysemu_ops = &arm_sysemu_ops;
2511fcf5ef2aSThomas Huth #endif
2512fcf5ef2aSThomas Huth     cc->gdb_num_core_regs = 26;
2513fcf5ef2aSThomas Huth     cc->gdb_arch_name = arm_gdb_arch_name;
2514200bf5b7SAbdallah Bouassida     cc->gdb_get_dynamic_xml = arm_gdb_get_dynamic_xml;
2515fcf5ef2aSThomas Huth     cc->gdb_stop_before_watchpoint = true;
2516fcf5ef2aSThomas Huth     cc->disas_set_info = arm_disas_set_info;
251778271684SClaudio Fontana 
251874d7fc7fSRichard Henderson #ifdef CONFIG_TCG
251978271684SClaudio Fontana     cc->tcg_ops = &arm_tcg_ops;
2520cbc183d2SClaudio Fontana #endif /* CONFIG_TCG */
2521fcf5ef2aSThomas Huth }
2522fcf5ef2aSThomas Huth 
252351e5ef45SMarc-André Lureau static void arm_cpu_instance_init(Object *obj)
252451e5ef45SMarc-André Lureau {
252551e5ef45SMarc-André Lureau     ARMCPUClass *acc = ARM_CPU_GET_CLASS(obj);
252651e5ef45SMarc-André Lureau 
252751e5ef45SMarc-André Lureau     acc->info->initfn(obj);
252851e5ef45SMarc-André Lureau     arm_cpu_post_init(obj);
252951e5ef45SMarc-André Lureau }
253051e5ef45SMarc-André Lureau 
253151e5ef45SMarc-André Lureau static void cpu_register_class_init(ObjectClass *oc, void *data)
253251e5ef45SMarc-André Lureau {
253351e5ef45SMarc-André Lureau     ARMCPUClass *acc = ARM_CPU_CLASS(oc);
253448de6462SAkihiko Odaki     CPUClass *cc = CPU_CLASS(acc);
253551e5ef45SMarc-André Lureau 
253651e5ef45SMarc-André Lureau     acc->info = data;
253748de6462SAkihiko Odaki     cc->gdb_core_xml_file = "arm-core.xml";
253851e5ef45SMarc-André Lureau }
253951e5ef45SMarc-André Lureau 
254037bcf244SThomas Huth void arm_cpu_register(const ARMCPUInfo *info)
2541fcf5ef2aSThomas Huth {
2542fcf5ef2aSThomas Huth     TypeInfo type_info = {
2543fcf5ef2aSThomas Huth         .parent = TYPE_ARM_CPU,
254451e5ef45SMarc-André Lureau         .instance_init = arm_cpu_instance_init,
254551e5ef45SMarc-André Lureau         .class_init = info->class_init ?: cpu_register_class_init,
254651e5ef45SMarc-André Lureau         .class_data = (void *)info,
2547fcf5ef2aSThomas Huth     };
2548fcf5ef2aSThomas Huth 
2549fcf5ef2aSThomas Huth     type_info.name = g_strdup_printf("%s-" TYPE_ARM_CPU, info->name);
2550fcf5ef2aSThomas Huth     type_register(&type_info);
2551fcf5ef2aSThomas Huth     g_free((void *)type_info.name);
2552fcf5ef2aSThomas Huth }
2553fcf5ef2aSThomas Huth 
2554fcf5ef2aSThomas Huth static const TypeInfo arm_cpu_type_info = {
2555fcf5ef2aSThomas Huth     .name = TYPE_ARM_CPU,
2556fcf5ef2aSThomas Huth     .parent = TYPE_CPU,
2557fcf5ef2aSThomas Huth     .instance_size = sizeof(ARMCPU),
2558d03087bdSRichard Henderson     .instance_align = __alignof__(ARMCPU),
2559fcf5ef2aSThomas Huth     .instance_init = arm_cpu_initfn,
2560fcf5ef2aSThomas Huth     .instance_finalize = arm_cpu_finalizefn,
2561fcf5ef2aSThomas Huth     .abstract = true,
2562fcf5ef2aSThomas Huth     .class_size = sizeof(ARMCPUClass),
2563fcf5ef2aSThomas Huth     .class_init = arm_cpu_class_init,
2564fcf5ef2aSThomas Huth };
2565fcf5ef2aSThomas Huth 
2566fcf5ef2aSThomas Huth static void arm_cpu_register_types(void)
2567fcf5ef2aSThomas Huth {
2568fcf5ef2aSThomas Huth     type_register_static(&arm_cpu_type_info);
2569fcf5ef2aSThomas Huth }
2570fcf5ef2aSThomas Huth 
2571fcf5ef2aSThomas Huth type_init(arm_cpu_register_types)
2572