xref: /qemu/target/arm/cpu.c (revision 6b8f40c61bbfef1abe77eeb9c716ec642927c12c)
1 /*
2  * QEMU ARM CPU
3  *
4  * Copyright (c) 2012 SUSE LINUX Products GmbH
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see
18  * <http://www.gnu.org/licenses/gpl-2.0.html>
19  */
20 
21 #include "qemu/osdep.h"
22 #include "qemu/qemu-print.h"
23 #include "qemu/timer.h"
24 #include "qemu/log.h"
25 #include "exec/page-vary.h"
26 #include "target/arm/idau.h"
27 #include "qemu/module.h"
28 #include "qapi/error.h"
29 #include "cpu.h"
30 #ifdef CONFIG_TCG
31 #include "exec/translation-block.h"
32 #include "hw/core/tcg-cpu-ops.h"
33 #endif /* CONFIG_TCG */
34 #include "internals.h"
35 #include "cpu-features.h"
36 #include "exec/exec-all.h"
37 #include "hw/qdev-properties.h"
38 #if !defined(CONFIG_USER_ONLY)
39 #include "hw/loader.h"
40 #include "hw/boards.h"
41 #ifdef CONFIG_TCG
42 #include "hw/intc/armv7m_nvic.h"
43 #endif /* CONFIG_TCG */
44 #endif /* !CONFIG_USER_ONLY */
45 #include "system/tcg.h"
46 #include "system/qtest.h"
47 #include "system/hw_accel.h"
48 #include "kvm_arm.h"
49 #include "disas/capstone.h"
50 #include "fpu/softfloat.h"
51 #include "cpregs.h"
52 #include "target/arm/cpu-qom.h"
53 #include "target/arm/gtimer.h"
54 
55 static void arm_cpu_set_pc(CPUState *cs, vaddr value)
56 {
57     ARMCPU *cpu = ARM_CPU(cs);
58     CPUARMState *env = &cpu->env;
59 
60     if (is_a64(env)) {
61         env->pc = value;
62         env->thumb = false;
63     } else {
64         env->regs[15] = value & ~1;
65         env->thumb = value & 1;
66     }
67 }
68 
69 static vaddr arm_cpu_get_pc(CPUState *cs)
70 {
71     ARMCPU *cpu = ARM_CPU(cs);
72     CPUARMState *env = &cpu->env;
73 
74     if (is_a64(env)) {
75         return env->pc;
76     } else {
77         return env->regs[15];
78     }
79 }
80 
81 #ifdef CONFIG_TCG
82 void arm_cpu_synchronize_from_tb(CPUState *cs,
83                                  const TranslationBlock *tb)
84 {
85     /* The program counter is always up to date with CF_PCREL. */
86     if (!(tb_cflags(tb) & CF_PCREL)) {
87         CPUARMState *env = cpu_env(cs);
88         /*
89          * It's OK to look at env for the current mode here, because it's
90          * never possible for an AArch64 TB to chain to an AArch32 TB.
91          */
92         if (is_a64(env)) {
93             env->pc = tb->pc;
94         } else {
95             env->regs[15] = tb->pc;
96         }
97     }
98 }
99 
100 void arm_restore_state_to_opc(CPUState *cs,
101                               const TranslationBlock *tb,
102                               const uint64_t *data)
103 {
104     CPUARMState *env = cpu_env(cs);
105 
106     if (is_a64(env)) {
107         if (tb_cflags(tb) & CF_PCREL) {
108             env->pc = (env->pc & TARGET_PAGE_MASK) | data[0];
109         } else {
110             env->pc = data[0];
111         }
112         env->condexec_bits = 0;
113         env->exception.syndrome = data[2] << ARM_INSN_START_WORD2_SHIFT;
114     } else {
115         if (tb_cflags(tb) & CF_PCREL) {
116             env->regs[15] = (env->regs[15] & TARGET_PAGE_MASK) | data[0];
117         } else {
118             env->regs[15] = data[0];
119         }
120         env->condexec_bits = data[1];
121         env->exception.syndrome = data[2] << ARM_INSN_START_WORD2_SHIFT;
122     }
123 }
124 #endif /* CONFIG_TCG */
125 
126 /*
127  * With SCTLR_ELx.NMI == 0, IRQ with Superpriority is masked identically with
128  * IRQ without Superpriority. Moreover, if the GIC is configured so that
129  * FEAT_GICv3_NMI is only set if FEAT_NMI is set, then we won't ever see
130  * CPU_INTERRUPT_*NMI anyway. So we might as well accept NMI here
131  * unconditionally.
132  */
133 static bool arm_cpu_has_work(CPUState *cs)
134 {
135     ARMCPU *cpu = ARM_CPU(cs);
136 
137     return (cpu->power_state != PSCI_OFF)
138         && cs->interrupt_request &
139         (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD
140          | CPU_INTERRUPT_NMI | CPU_INTERRUPT_VINMI | CPU_INTERRUPT_VFNMI
141          | CPU_INTERRUPT_VFIQ | CPU_INTERRUPT_VIRQ | CPU_INTERRUPT_VSERR
142          | CPU_INTERRUPT_EXITTB);
143 }
144 
145 static int arm_cpu_mmu_index(CPUState *cs, bool ifetch)
146 {
147     return arm_env_mmu_index(cpu_env(cs));
148 }
149 
150 void arm_register_pre_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook,
151                                  void *opaque)
152 {
153     ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1);
154 
155     entry->hook = hook;
156     entry->opaque = opaque;
157 
158     QLIST_INSERT_HEAD(&cpu->pre_el_change_hooks, entry, node);
159 }
160 
161 void arm_register_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook,
162                                  void *opaque)
163 {
164     ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1);
165 
166     entry->hook = hook;
167     entry->opaque = opaque;
168 
169     QLIST_INSERT_HEAD(&cpu->el_change_hooks, entry, node);
170 }
171 
172 /*
173  * Set the float_status behaviour to match the Arm defaults:
174  *  * tininess-before-rounding
175  *  * 2-input NaN propagation prefers SNaN over QNaN, and then
176  *    operand A over operand B (see FPProcessNaNs() pseudocode)
177  *  * 3-input NaN propagation prefers SNaN over QNaN, and then
178  *    operand C over A over B (see FPProcessNaNs3() pseudocode,
179  *    but note that for QEMU muladd is a * b + c, whereas for
180  *    the pseudocode function the arguments are in the order c, a, b.
181  *  * 0 * Inf + NaN returns the default NaN if the input NaN is quiet,
182  *    and the input NaN if it is signalling
183  *  * Default NaN has sign bit clear, msb frac bit set
184  */
185 static void arm_set_default_fp_behaviours(float_status *s)
186 {
187     set_float_detect_tininess(float_tininess_before_rounding, s);
188     set_float_2nan_prop_rule(float_2nan_prop_s_ab, s);
189     set_float_3nan_prop_rule(float_3nan_prop_s_cab, s);
190     set_float_infzeronan_rule(float_infzeronan_dnan_if_qnan, s);
191     set_float_default_nan_pattern(0b01000000, s);
192 }
193 
194 static void cp_reg_reset(gpointer key, gpointer value, gpointer opaque)
195 {
196     /* Reset a single ARMCPRegInfo register */
197     ARMCPRegInfo *ri = value;
198     ARMCPU *cpu = opaque;
199 
200     if (ri->type & (ARM_CP_SPECIAL_MASK | ARM_CP_ALIAS)) {
201         return;
202     }
203 
204     if (ri->resetfn) {
205         ri->resetfn(&cpu->env, ri);
206         return;
207     }
208 
209     /* A zero offset is never possible as it would be regs[0]
210      * so we use it to indicate that reset is being handled elsewhere.
211      * This is basically only used for fields in non-core coprocessors
212      * (like the pxa2xx ones).
213      */
214     if (!ri->fieldoffset) {
215         return;
216     }
217 
218     if (cpreg_field_is_64bit(ri)) {
219         CPREG_FIELD64(&cpu->env, ri) = ri->resetvalue;
220     } else {
221         CPREG_FIELD32(&cpu->env, ri) = ri->resetvalue;
222     }
223 }
224 
225 static void cp_reg_check_reset(gpointer key, gpointer value,  gpointer opaque)
226 {
227     /* Purely an assertion check: we've already done reset once,
228      * so now check that running the reset for the cpreg doesn't
229      * change its value. This traps bugs where two different cpregs
230      * both try to reset the same state field but to different values.
231      */
232     ARMCPRegInfo *ri = value;
233     ARMCPU *cpu = opaque;
234     uint64_t oldvalue, newvalue;
235 
236     if (ri->type & (ARM_CP_SPECIAL_MASK | ARM_CP_ALIAS | ARM_CP_NO_RAW)) {
237         return;
238     }
239 
240     oldvalue = read_raw_cp_reg(&cpu->env, ri);
241     cp_reg_reset(key, value, opaque);
242     newvalue = read_raw_cp_reg(&cpu->env, ri);
243     assert(oldvalue == newvalue);
244 }
245 
246 static void arm_cpu_reset_hold(Object *obj, ResetType type)
247 {
248     CPUState *cs = CPU(obj);
249     ARMCPU *cpu = ARM_CPU(cs);
250     ARMCPUClass *acc = ARM_CPU_GET_CLASS(obj);
251     CPUARMState *env = &cpu->env;
252 
253     if (acc->parent_phases.hold) {
254         acc->parent_phases.hold(obj, type);
255     }
256 
257     memset(env, 0, offsetof(CPUARMState, end_reset_fields));
258 
259     g_hash_table_foreach(cpu->cp_regs, cp_reg_reset, cpu);
260     g_hash_table_foreach(cpu->cp_regs, cp_reg_check_reset, cpu);
261 
262     env->vfp.xregs[ARM_VFP_FPSID] = cpu->reset_fpsid;
263     env->vfp.xregs[ARM_VFP_MVFR0] = cpu->isar.mvfr0;
264     env->vfp.xregs[ARM_VFP_MVFR1] = cpu->isar.mvfr1;
265     env->vfp.xregs[ARM_VFP_MVFR2] = cpu->isar.mvfr2;
266 
267     cpu->power_state = cs->start_powered_off ? PSCI_OFF : PSCI_ON;
268 
269     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
270         env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
271     }
272 
273     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
274         /* 64 bit CPUs always start in 64 bit mode */
275         env->aarch64 = true;
276 #if defined(CONFIG_USER_ONLY)
277         env->pstate = PSTATE_MODE_EL0t;
278         /* Userspace expects access to DC ZVA, CTL_EL0 and the cache ops */
279         env->cp15.sctlr_el[1] |= SCTLR_UCT | SCTLR_UCI | SCTLR_DZE;
280         /* Enable all PAC keys.  */
281         env->cp15.sctlr_el[1] |= (SCTLR_EnIA | SCTLR_EnIB |
282                                   SCTLR_EnDA | SCTLR_EnDB);
283         /* Trap on btype=3 for PACIxSP. */
284         env->cp15.sctlr_el[1] |= SCTLR_BT0;
285         /* Trap on implementation defined registers. */
286         if (cpu_isar_feature(aa64_tidcp1, cpu)) {
287             env->cp15.sctlr_el[1] |= SCTLR_TIDCP;
288         }
289         /* and to the FP/Neon instructions */
290         env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
291                                          CPACR_EL1, FPEN, 3);
292         /* and to the SVE instructions, with default vector length */
293         if (cpu_isar_feature(aa64_sve, cpu)) {
294             env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
295                                              CPACR_EL1, ZEN, 3);
296             env->vfp.zcr_el[1] = cpu->sve_default_vq - 1;
297         }
298         /* and for SME instructions, with default vector length, and TPIDR2 */
299         if (cpu_isar_feature(aa64_sme, cpu)) {
300             env->cp15.sctlr_el[1] |= SCTLR_EnTP2;
301             env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
302                                              CPACR_EL1, SMEN, 3);
303             env->vfp.smcr_el[1] = cpu->sme_default_vq - 1;
304             if (cpu_isar_feature(aa64_sme_fa64, cpu)) {
305                 env->vfp.smcr_el[1] = FIELD_DP64(env->vfp.smcr_el[1],
306                                                  SMCR, FA64, 1);
307             }
308         }
309         /*
310          * Enable 48-bit address space (TODO: take reserved_va into account).
311          * Enable TBI0 but not TBI1.
312          * Note that this must match useronly_clean_ptr.
313          */
314         env->cp15.tcr_el[1] = 5 | (1ULL << 37);
315 
316         /* Enable MTE */
317         if (cpu_isar_feature(aa64_mte, cpu)) {
318             /* Enable tag access, but leave TCF0 as No Effect (0). */
319             env->cp15.sctlr_el[1] |= SCTLR_ATA0;
320             /*
321              * Exclude all tags, so that tag 0 is always used.
322              * This corresponds to Linux current->thread.gcr_incl = 0.
323              *
324              * Set RRND, so that helper_irg() will generate a seed later.
325              * Here in cpu_reset(), the crypto subsystem has not yet been
326              * initialized.
327              */
328             env->cp15.gcr_el1 = 0x1ffff;
329         }
330         /*
331          * Disable access to SCXTNUM_EL0 from CSV2_1p2.
332          * This is not yet exposed from the Linux kernel in any way.
333          */
334         env->cp15.sctlr_el[1] |= SCTLR_TSCXT;
335         /* Disable access to Debug Communication Channel (DCC). */
336         env->cp15.mdscr_el1 |= 1 << 12;
337         /* Enable FEAT_MOPS */
338         env->cp15.sctlr_el[1] |= SCTLR_MSCEN;
339 #else
340         /* Reset into the highest available EL */
341         if (arm_feature(env, ARM_FEATURE_EL3)) {
342             env->pstate = PSTATE_MODE_EL3h;
343         } else if (arm_feature(env, ARM_FEATURE_EL2)) {
344             env->pstate = PSTATE_MODE_EL2h;
345         } else {
346             env->pstate = PSTATE_MODE_EL1h;
347         }
348 
349         /* Sample rvbar at reset.  */
350         env->cp15.rvbar = cpu->rvbar_prop;
351         env->pc = env->cp15.rvbar;
352 #endif
353     } else {
354 #if defined(CONFIG_USER_ONLY)
355         /* Userspace expects access to cp10 and cp11 for FP/Neon */
356         env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
357                                          CPACR, CP10, 3);
358         env->cp15.cpacr_el1 = FIELD_DP64(env->cp15.cpacr_el1,
359                                          CPACR, CP11, 3);
360 #endif
361         if (arm_feature(env, ARM_FEATURE_V8)) {
362             env->cp15.rvbar = cpu->rvbar_prop;
363             env->regs[15] = cpu->rvbar_prop;
364         }
365     }
366 
367 #if defined(CONFIG_USER_ONLY)
368     env->uncached_cpsr = ARM_CPU_MODE_USR;
369     /* For user mode we must enable access to coprocessors */
370     env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
371     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
372         env->cp15.c15_cpar = 3;
373     } else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
374         env->cp15.c15_cpar = 1;
375     }
376 #else
377 
378     /*
379      * If the highest available EL is EL2, AArch32 will start in Hyp
380      * mode; otherwise it starts in SVC. Note that if we start in
381      * AArch64 then these values in the uncached_cpsr will be ignored.
382      */
383     if (arm_feature(env, ARM_FEATURE_EL2) &&
384         !arm_feature(env, ARM_FEATURE_EL3)) {
385         env->uncached_cpsr = ARM_CPU_MODE_HYP;
386     } else {
387         env->uncached_cpsr = ARM_CPU_MODE_SVC;
388     }
389     env->daif = PSTATE_D | PSTATE_A | PSTATE_I | PSTATE_F;
390 
391     /* AArch32 has a hard highvec setting of 0xFFFF0000.  If we are currently
392      * executing as AArch32 then check if highvecs are enabled and
393      * adjust the PC accordingly.
394      */
395     if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
396         env->regs[15] = 0xFFFF0000;
397     }
398 
399     env->vfp.xregs[ARM_VFP_FPEXC] = 0;
400 #endif
401 
402     if (arm_feature(env, ARM_FEATURE_M)) {
403 #ifndef CONFIG_USER_ONLY
404         uint32_t initial_msp; /* Loaded from 0x0 */
405         uint32_t initial_pc; /* Loaded from 0x4 */
406         uint8_t *rom;
407         uint32_t vecbase;
408 #endif
409 
410         if (cpu_isar_feature(aa32_lob, cpu)) {
411             /*
412              * LTPSIZE is constant 4 if MVE not implemented, and resets
413              * to an UNKNOWN value if MVE is implemented. We choose to
414              * always reset to 4.
415              */
416             env->v7m.ltpsize = 4;
417             /* The LTPSIZE field in FPDSCR is constant and reads as 4. */
418             env->v7m.fpdscr[M_REG_NS] = 4 << FPCR_LTPSIZE_SHIFT;
419             env->v7m.fpdscr[M_REG_S] = 4 << FPCR_LTPSIZE_SHIFT;
420         }
421 
422         if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
423             env->v7m.secure = true;
424         } else {
425             /* This bit resets to 0 if security is supported, but 1 if
426              * it is not. The bit is not present in v7M, but we set it
427              * here so we can avoid having to make checks on it conditional
428              * on ARM_FEATURE_V8 (we don't let the guest see the bit).
429              */
430             env->v7m.aircr = R_V7M_AIRCR_BFHFNMINS_MASK;
431             /*
432              * Set NSACR to indicate "NS access permitted to everything";
433              * this avoids having to have all the tests of it being
434              * conditional on ARM_FEATURE_M_SECURITY. Note also that from
435              * v8.1M the guest-visible value of NSACR in a CPU without the
436              * Security Extension is 0xcff.
437              */
438             env->v7m.nsacr = 0xcff;
439         }
440 
441         /* In v7M the reset value of this bit is IMPDEF, but ARM recommends
442          * that it resets to 1, so QEMU always does that rather than making
443          * it dependent on CPU model. In v8M it is RES1.
444          */
445         env->v7m.ccr[M_REG_NS] = R_V7M_CCR_STKALIGN_MASK;
446         env->v7m.ccr[M_REG_S] = R_V7M_CCR_STKALIGN_MASK;
447         if (arm_feature(env, ARM_FEATURE_V8)) {
448             /* in v8M the NONBASETHRDENA bit [0] is RES1 */
449             env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_NONBASETHRDENA_MASK;
450             env->v7m.ccr[M_REG_S] |= R_V7M_CCR_NONBASETHRDENA_MASK;
451         }
452         if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
453             env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_UNALIGN_TRP_MASK;
454             env->v7m.ccr[M_REG_S] |= R_V7M_CCR_UNALIGN_TRP_MASK;
455         }
456 
457         if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
458             env->v7m.fpccr[M_REG_NS] = R_V7M_FPCCR_ASPEN_MASK;
459             env->v7m.fpccr[M_REG_S] = R_V7M_FPCCR_ASPEN_MASK |
460                 R_V7M_FPCCR_LSPEN_MASK | R_V7M_FPCCR_S_MASK;
461         }
462 
463 #ifndef CONFIG_USER_ONLY
464         /* Unlike A/R profile, M profile defines the reset LR value */
465         env->regs[14] = 0xffffffff;
466 
467         env->v7m.vecbase[M_REG_S] = cpu->init_svtor & 0xffffff80;
468         env->v7m.vecbase[M_REG_NS] = cpu->init_nsvtor & 0xffffff80;
469 
470         /* Load the initial SP and PC from offset 0 and 4 in the vector table */
471         vecbase = env->v7m.vecbase[env->v7m.secure];
472         rom = rom_ptr_for_as(cs->as, vecbase, 8);
473         if (rom) {
474             /* Address zero is covered by ROM which hasn't yet been
475              * copied into physical memory.
476              */
477             initial_msp = ldl_p(rom);
478             initial_pc = ldl_p(rom + 4);
479         } else {
480             /* Address zero not covered by a ROM blob, or the ROM blob
481              * is in non-modifiable memory and this is a second reset after
482              * it got copied into memory. In the latter case, rom_ptr
483              * will return a NULL pointer and we should use ldl_phys instead.
484              */
485             initial_msp = ldl_phys(cs->as, vecbase);
486             initial_pc = ldl_phys(cs->as, vecbase + 4);
487         }
488 
489         qemu_log_mask(CPU_LOG_INT,
490                       "Loaded reset SP 0x%x PC 0x%x from vector table\n",
491                       initial_msp, initial_pc);
492 
493         env->regs[13] = initial_msp & 0xFFFFFFFC;
494         env->regs[15] = initial_pc & ~1;
495         env->thumb = initial_pc & 1;
496 #else
497         /*
498          * For user mode we run non-secure and with access to the FPU.
499          * The FPU context is active (ie does not need further setup)
500          * and is owned by non-secure.
501          */
502         env->v7m.secure = false;
503         env->v7m.nsacr = 0xcff;
504         env->v7m.cpacr[M_REG_NS] = 0xf0ffff;
505         env->v7m.fpccr[M_REG_S] &=
506             ~(R_V7M_FPCCR_LSPEN_MASK | R_V7M_FPCCR_S_MASK);
507         env->v7m.control[M_REG_S] |= R_V7M_CONTROL_FPCA_MASK;
508 #endif
509     }
510 
511     /* M profile requires that reset clears the exclusive monitor;
512      * A profile does not, but clearing it makes more sense than having it
513      * set with an exclusive access on address zero.
514      */
515     arm_clear_exclusive(env);
516 
517     if (arm_feature(env, ARM_FEATURE_PMSA)) {
518         if (cpu->pmsav7_dregion > 0) {
519             if (arm_feature(env, ARM_FEATURE_V8)) {
520                 memset(env->pmsav8.rbar[M_REG_NS], 0,
521                        sizeof(*env->pmsav8.rbar[M_REG_NS])
522                        * cpu->pmsav7_dregion);
523                 memset(env->pmsav8.rlar[M_REG_NS], 0,
524                        sizeof(*env->pmsav8.rlar[M_REG_NS])
525                        * cpu->pmsav7_dregion);
526                 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
527                     memset(env->pmsav8.rbar[M_REG_S], 0,
528                            sizeof(*env->pmsav8.rbar[M_REG_S])
529                            * cpu->pmsav7_dregion);
530                     memset(env->pmsav8.rlar[M_REG_S], 0,
531                            sizeof(*env->pmsav8.rlar[M_REG_S])
532                            * cpu->pmsav7_dregion);
533                 }
534             } else if (arm_feature(env, ARM_FEATURE_V7)) {
535                 memset(env->pmsav7.drbar, 0,
536                        sizeof(*env->pmsav7.drbar) * cpu->pmsav7_dregion);
537                 memset(env->pmsav7.drsr, 0,
538                        sizeof(*env->pmsav7.drsr) * cpu->pmsav7_dregion);
539                 memset(env->pmsav7.dracr, 0,
540                        sizeof(*env->pmsav7.dracr) * cpu->pmsav7_dregion);
541             }
542         }
543 
544         if (cpu->pmsav8r_hdregion > 0) {
545             memset(env->pmsav8.hprbar, 0,
546                    sizeof(*env->pmsav8.hprbar) * cpu->pmsav8r_hdregion);
547             memset(env->pmsav8.hprlar, 0,
548                    sizeof(*env->pmsav8.hprlar) * cpu->pmsav8r_hdregion);
549         }
550 
551         env->pmsav7.rnr[M_REG_NS] = 0;
552         env->pmsav7.rnr[M_REG_S] = 0;
553         env->pmsav8.mair0[M_REG_NS] = 0;
554         env->pmsav8.mair0[M_REG_S] = 0;
555         env->pmsav8.mair1[M_REG_NS] = 0;
556         env->pmsav8.mair1[M_REG_S] = 0;
557     }
558 
559     if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
560         if (cpu->sau_sregion > 0) {
561             memset(env->sau.rbar, 0, sizeof(*env->sau.rbar) * cpu->sau_sregion);
562             memset(env->sau.rlar, 0, sizeof(*env->sau.rlar) * cpu->sau_sregion);
563         }
564         env->sau.rnr = 0;
565         /* SAU_CTRL reset value is IMPDEF; we choose 0, which is what
566          * the Cortex-M33 does.
567          */
568         env->sau.ctrl = 0;
569     }
570 
571     set_flush_to_zero(1, &env->vfp.standard_fp_status);
572     set_flush_inputs_to_zero(1, &env->vfp.standard_fp_status);
573     set_default_nan_mode(1, &env->vfp.standard_fp_status);
574     set_default_nan_mode(1, &env->vfp.standard_fp_status_f16);
575     arm_set_default_fp_behaviours(&env->vfp.fp_status_a32);
576     arm_set_default_fp_behaviours(&env->vfp.fp_status_a64);
577     arm_set_default_fp_behaviours(&env->vfp.standard_fp_status);
578     arm_set_default_fp_behaviours(&env->vfp.fp_status_f16_a32);
579     arm_set_default_fp_behaviours(&env->vfp.fp_status_f16_a64);
580     arm_set_default_fp_behaviours(&env->vfp.standard_fp_status_f16);
581 
582 #ifndef CONFIG_USER_ONLY
583     if (kvm_enabled()) {
584         kvm_arm_reset_vcpu(cpu);
585     }
586 #endif
587 
588     if (tcg_enabled()) {
589         hw_breakpoint_update_all(cpu);
590         hw_watchpoint_update_all(cpu);
591 
592         arm_rebuild_hflags(env);
593     }
594 }
595 
596 void arm_emulate_firmware_reset(CPUState *cpustate, int target_el)
597 {
598     ARMCPU *cpu = ARM_CPU(cpustate);
599     CPUARMState *env = &cpu->env;
600     bool have_el3 = arm_feature(env, ARM_FEATURE_EL3);
601     bool have_el2 = arm_feature(env, ARM_FEATURE_EL2);
602 
603     /*
604      * Check we have the EL we're aiming for. If that is the
605      * highest implemented EL, then cpu_reset has already done
606      * all the work.
607      */
608     switch (target_el) {
609     case 3:
610         assert(have_el3);
611         return;
612     case 2:
613         assert(have_el2);
614         if (!have_el3) {
615             return;
616         }
617         break;
618     case 1:
619         if (!have_el3 && !have_el2) {
620             return;
621         }
622         break;
623     default:
624         g_assert_not_reached();
625     }
626 
627     if (have_el3) {
628         /*
629          * Set the EL3 state so code can run at EL2. This should match
630          * the requirements set by Linux in its booting spec.
631          */
632         if (env->aarch64) {
633             env->cp15.scr_el3 |= SCR_RW;
634             if (cpu_isar_feature(aa64_pauth, cpu)) {
635                 env->cp15.scr_el3 |= SCR_API | SCR_APK;
636             }
637             if (cpu_isar_feature(aa64_mte, cpu)) {
638                 env->cp15.scr_el3 |= SCR_ATA;
639             }
640             if (cpu_isar_feature(aa64_sve, cpu)) {
641                 env->cp15.cptr_el[3] |= R_CPTR_EL3_EZ_MASK;
642                 env->vfp.zcr_el[3] = 0xf;
643             }
644             if (cpu_isar_feature(aa64_sme, cpu)) {
645                 env->cp15.cptr_el[3] |= R_CPTR_EL3_ESM_MASK;
646                 env->cp15.scr_el3 |= SCR_ENTP2;
647                 env->vfp.smcr_el[3] = 0xf;
648             }
649             if (cpu_isar_feature(aa64_hcx, cpu)) {
650                 env->cp15.scr_el3 |= SCR_HXEN;
651             }
652             if (cpu_isar_feature(aa64_fgt, cpu)) {
653                 env->cp15.scr_el3 |= SCR_FGTEN;
654             }
655         }
656 
657         if (target_el == 2) {
658             /* If the guest is at EL2 then Linux expects the HVC insn to work */
659             env->cp15.scr_el3 |= SCR_HCE;
660         }
661 
662         /* Put CPU into non-secure state */
663         env->cp15.scr_el3 |= SCR_NS;
664         /* Set NSACR.{CP11,CP10} so NS can access the FPU */
665         env->cp15.nsacr |= 3 << 10;
666     }
667 
668     if (have_el2 && target_el < 2) {
669         /* Set EL2 state so code can run at EL1. */
670         if (env->aarch64) {
671             env->cp15.hcr_el2 |= HCR_RW;
672         }
673     }
674 
675     /* Set the CPU to the desired state */
676     if (env->aarch64) {
677         env->pstate = aarch64_pstate_mode(target_el, true);
678     } else {
679         static const uint32_t mode_for_el[] = {
680             0,
681             ARM_CPU_MODE_SVC,
682             ARM_CPU_MODE_HYP,
683             ARM_CPU_MODE_SVC,
684         };
685 
686         cpsr_write(env, mode_for_el[target_el], CPSR_M, CPSRWriteRaw);
687     }
688 }
689 
690 
691 #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
692 
693 static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx,
694                                      unsigned int target_el,
695                                      unsigned int cur_el, bool secure,
696                                      uint64_t hcr_el2)
697 {
698     CPUARMState *env = cpu_env(cs);
699     bool pstate_unmasked;
700     bool unmasked = false;
701     bool allIntMask = false;
702 
703     /*
704      * Don't take exceptions if they target a lower EL.
705      * This check should catch any exceptions that would not be taken
706      * but left pending.
707      */
708     if (cur_el > target_el) {
709         return false;
710     }
711 
712     if (cpu_isar_feature(aa64_nmi, env_archcpu(env)) &&
713         env->cp15.sctlr_el[target_el] & SCTLR_NMI && cur_el == target_el) {
714         allIntMask = env->pstate & PSTATE_ALLINT ||
715                      ((env->cp15.sctlr_el[target_el] & SCTLR_SPINTMASK) &&
716                       (env->pstate & PSTATE_SP));
717     }
718 
719     switch (excp_idx) {
720     case EXCP_NMI:
721         pstate_unmasked = !allIntMask;
722         break;
723 
724     case EXCP_VINMI:
725         if (!(hcr_el2 & HCR_IMO) || (hcr_el2 & HCR_TGE)) {
726             /* VINMIs are only taken when hypervized.  */
727             return false;
728         }
729         return !allIntMask;
730     case EXCP_VFNMI:
731         if (!(hcr_el2 & HCR_FMO) || (hcr_el2 & HCR_TGE)) {
732             /* VFNMIs are only taken when hypervized.  */
733             return false;
734         }
735         return !allIntMask;
736     case EXCP_FIQ:
737         pstate_unmasked = (!(env->daif & PSTATE_F)) && (!allIntMask);
738         break;
739 
740     case EXCP_IRQ:
741         pstate_unmasked = (!(env->daif & PSTATE_I)) && (!allIntMask);
742         break;
743 
744     case EXCP_VFIQ:
745         if (!(hcr_el2 & HCR_FMO) || (hcr_el2 & HCR_TGE)) {
746             /* VFIQs are only taken when hypervized.  */
747             return false;
748         }
749         return !(env->daif & PSTATE_F) && (!allIntMask);
750     case EXCP_VIRQ:
751         if (!(hcr_el2 & HCR_IMO) || (hcr_el2 & HCR_TGE)) {
752             /* VIRQs are only taken when hypervized.  */
753             return false;
754         }
755         return !(env->daif & PSTATE_I) && (!allIntMask);
756     case EXCP_VSERR:
757         if (!(hcr_el2 & HCR_AMO) || (hcr_el2 & HCR_TGE)) {
758             /* VIRQs are only taken when hypervized.  */
759             return false;
760         }
761         return !(env->daif & PSTATE_A);
762     default:
763         g_assert_not_reached();
764     }
765 
766     /*
767      * Use the target EL, current execution state and SCR/HCR settings to
768      * determine whether the corresponding CPSR bit is used to mask the
769      * interrupt.
770      */
771     if ((target_el > cur_el) && (target_el != 1)) {
772         /* Exceptions targeting a higher EL may not be maskable */
773         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
774             switch (target_el) {
775             case 2:
776                 /*
777                  * According to ARM DDI 0487H.a, an interrupt can be masked
778                  * when HCR_E2H and HCR_TGE are both set regardless of the
779                  * current Security state. Note that we need to revisit this
780                  * part again once we need to support NMI.
781                  */
782                 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
783                         unmasked = true;
784                 }
785                 break;
786             case 3:
787                 /* Interrupt cannot be masked when the target EL is 3 */
788                 unmasked = true;
789                 break;
790             default:
791                 g_assert_not_reached();
792             }
793         } else {
794             /*
795              * The old 32-bit-only environment has a more complicated
796              * masking setup. HCR and SCR bits not only affect interrupt
797              * routing but also change the behaviour of masking.
798              */
799             bool hcr, scr;
800 
801             switch (excp_idx) {
802             case EXCP_FIQ:
803                 /*
804                  * If FIQs are routed to EL3 or EL2 then there are cases where
805                  * we override the CPSR.F in determining if the exception is
806                  * masked or not. If neither of these are set then we fall back
807                  * to the CPSR.F setting otherwise we further assess the state
808                  * below.
809                  */
810                 hcr = hcr_el2 & HCR_FMO;
811                 scr = (env->cp15.scr_el3 & SCR_FIQ);
812 
813                 /*
814                  * When EL3 is 32-bit, the SCR.FW bit controls whether the
815                  * CPSR.F bit masks FIQ interrupts when taken in non-secure
816                  * state. If SCR.FW is set then FIQs can be masked by CPSR.F
817                  * when non-secure but only when FIQs are only routed to EL3.
818                  */
819                 scr = scr && !((env->cp15.scr_el3 & SCR_FW) && !hcr);
820                 break;
821             case EXCP_IRQ:
822                 /*
823                  * When EL3 execution state is 32-bit, if HCR.IMO is set then
824                  * we may override the CPSR.I masking when in non-secure state.
825                  * The SCR.IRQ setting has already been taken into consideration
826                  * when setting the target EL, so it does not have a further
827                  * affect here.
828                  */
829                 hcr = hcr_el2 & HCR_IMO;
830                 scr = false;
831                 break;
832             default:
833                 g_assert_not_reached();
834             }
835 
836             if ((scr || hcr) && !secure) {
837                 unmasked = true;
838             }
839         }
840     }
841 
842     /*
843      * The PSTATE bits only mask the interrupt if we have not overridden the
844      * ability above.
845      */
846     return unmasked || pstate_unmasked;
847 }
848 
849 static bool arm_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
850 {
851     CPUClass *cc = CPU_GET_CLASS(cs);
852     CPUARMState *env = cpu_env(cs);
853     uint32_t cur_el = arm_current_el(env);
854     bool secure = arm_is_secure(env);
855     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
856     uint32_t target_el;
857     uint32_t excp_idx;
858 
859     /* The prioritization of interrupts is IMPLEMENTATION DEFINED. */
860 
861     if (cpu_isar_feature(aa64_nmi, env_archcpu(env)) &&
862         (arm_sctlr(env, cur_el) & SCTLR_NMI)) {
863         if (interrupt_request & CPU_INTERRUPT_NMI) {
864             excp_idx = EXCP_NMI;
865             target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
866             if (arm_excp_unmasked(cs, excp_idx, target_el,
867                                   cur_el, secure, hcr_el2)) {
868                 goto found;
869             }
870         }
871         if (interrupt_request & CPU_INTERRUPT_VINMI) {
872             excp_idx = EXCP_VINMI;
873             target_el = 1;
874             if (arm_excp_unmasked(cs, excp_idx, target_el,
875                                   cur_el, secure, hcr_el2)) {
876                 goto found;
877             }
878         }
879         if (interrupt_request & CPU_INTERRUPT_VFNMI) {
880             excp_idx = EXCP_VFNMI;
881             target_el = 1;
882             if (arm_excp_unmasked(cs, excp_idx, target_el,
883                                   cur_el, secure, hcr_el2)) {
884                 goto found;
885             }
886         }
887     } else {
888         /*
889          * NMI disabled: interrupts with superpriority are handled
890          * as if they didn't have it
891          */
892         if (interrupt_request & CPU_INTERRUPT_NMI) {
893             interrupt_request |= CPU_INTERRUPT_HARD;
894         }
895         if (interrupt_request & CPU_INTERRUPT_VINMI) {
896             interrupt_request |= CPU_INTERRUPT_VIRQ;
897         }
898         if (interrupt_request & CPU_INTERRUPT_VFNMI) {
899             interrupt_request |= CPU_INTERRUPT_VFIQ;
900         }
901     }
902 
903     if (interrupt_request & CPU_INTERRUPT_FIQ) {
904         excp_idx = EXCP_FIQ;
905         target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
906         if (arm_excp_unmasked(cs, excp_idx, target_el,
907                               cur_el, secure, hcr_el2)) {
908             goto found;
909         }
910     }
911     if (interrupt_request & CPU_INTERRUPT_HARD) {
912         excp_idx = EXCP_IRQ;
913         target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
914         if (arm_excp_unmasked(cs, excp_idx, target_el,
915                               cur_el, secure, hcr_el2)) {
916             goto found;
917         }
918     }
919     if (interrupt_request & CPU_INTERRUPT_VIRQ) {
920         excp_idx = EXCP_VIRQ;
921         target_el = 1;
922         if (arm_excp_unmasked(cs, excp_idx, target_el,
923                               cur_el, secure, hcr_el2)) {
924             goto found;
925         }
926     }
927     if (interrupt_request & CPU_INTERRUPT_VFIQ) {
928         excp_idx = EXCP_VFIQ;
929         target_el = 1;
930         if (arm_excp_unmasked(cs, excp_idx, target_el,
931                               cur_el, secure, hcr_el2)) {
932             goto found;
933         }
934     }
935     if (interrupt_request & CPU_INTERRUPT_VSERR) {
936         excp_idx = EXCP_VSERR;
937         target_el = 1;
938         if (arm_excp_unmasked(cs, excp_idx, target_el,
939                               cur_el, secure, hcr_el2)) {
940             /* Taking a virtual abort clears HCR_EL2.VSE */
941             env->cp15.hcr_el2 &= ~HCR_VSE;
942             cpu_reset_interrupt(cs, CPU_INTERRUPT_VSERR);
943             goto found;
944         }
945     }
946     return false;
947 
948  found:
949     cs->exception_index = excp_idx;
950     env->exception.target_el = target_el;
951     cc->tcg_ops->do_interrupt(cs);
952     return true;
953 }
954 
955 #endif /* CONFIG_TCG && !CONFIG_USER_ONLY */
956 
957 void arm_cpu_update_virq(ARMCPU *cpu)
958 {
959     /*
960      * Update the interrupt level for VIRQ, which is the logical OR of
961      * the HCR_EL2.VI bit and the input line level from the GIC.
962      */
963     CPUARMState *env = &cpu->env;
964     CPUState *cs = CPU(cpu);
965 
966     bool new_state = ((arm_hcr_el2_eff(env) & HCR_VI) &&
967         !(arm_hcrx_el2_eff(env) & HCRX_VINMI)) ||
968         (env->irq_line_state & CPU_INTERRUPT_VIRQ);
969 
970     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VIRQ) != 0)) {
971         if (new_state) {
972             cpu_interrupt(cs, CPU_INTERRUPT_VIRQ);
973         } else {
974             cpu_reset_interrupt(cs, CPU_INTERRUPT_VIRQ);
975         }
976     }
977 }
978 
979 void arm_cpu_update_vfiq(ARMCPU *cpu)
980 {
981     /*
982      * Update the interrupt level for VFIQ, which is the logical OR of
983      * the HCR_EL2.VF bit and the input line level from the GIC.
984      */
985     CPUARMState *env = &cpu->env;
986     CPUState *cs = CPU(cpu);
987 
988     bool new_state = ((arm_hcr_el2_eff(env) & HCR_VF) &&
989         !(arm_hcrx_el2_eff(env) & HCRX_VFNMI)) ||
990         (env->irq_line_state & CPU_INTERRUPT_VFIQ);
991 
992     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VFIQ) != 0)) {
993         if (new_state) {
994             cpu_interrupt(cs, CPU_INTERRUPT_VFIQ);
995         } else {
996             cpu_reset_interrupt(cs, CPU_INTERRUPT_VFIQ);
997         }
998     }
999 }
1000 
1001 void arm_cpu_update_vinmi(ARMCPU *cpu)
1002 {
1003     /*
1004      * Update the interrupt level for VINMI, which is the logical OR of
1005      * the HCRX_EL2.VINMI bit and the input line level from the GIC.
1006      */
1007     CPUARMState *env = &cpu->env;
1008     CPUState *cs = CPU(cpu);
1009 
1010     bool new_state = ((arm_hcr_el2_eff(env) & HCR_VI) &&
1011                       (arm_hcrx_el2_eff(env) & HCRX_VINMI)) ||
1012         (env->irq_line_state & CPU_INTERRUPT_VINMI);
1013 
1014     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VINMI) != 0)) {
1015         if (new_state) {
1016             cpu_interrupt(cs, CPU_INTERRUPT_VINMI);
1017         } else {
1018             cpu_reset_interrupt(cs, CPU_INTERRUPT_VINMI);
1019         }
1020     }
1021 }
1022 
1023 void arm_cpu_update_vfnmi(ARMCPU *cpu)
1024 {
1025     /*
1026      * Update the interrupt level for VFNMI, which is the HCRX_EL2.VFNMI bit.
1027      */
1028     CPUARMState *env = &cpu->env;
1029     CPUState *cs = CPU(cpu);
1030 
1031     bool new_state = (arm_hcr_el2_eff(env) & HCR_VF) &&
1032                       (arm_hcrx_el2_eff(env) & HCRX_VFNMI);
1033 
1034     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VFNMI) != 0)) {
1035         if (new_state) {
1036             cpu_interrupt(cs, CPU_INTERRUPT_VFNMI);
1037         } else {
1038             cpu_reset_interrupt(cs, CPU_INTERRUPT_VFNMI);
1039         }
1040     }
1041 }
1042 
1043 void arm_cpu_update_vserr(ARMCPU *cpu)
1044 {
1045     /*
1046      * Update the interrupt level for VSERR, which is the HCR_EL2.VSE bit.
1047      */
1048     CPUARMState *env = &cpu->env;
1049     CPUState *cs = CPU(cpu);
1050 
1051     bool new_state = env->cp15.hcr_el2 & HCR_VSE;
1052 
1053     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VSERR) != 0)) {
1054         if (new_state) {
1055             cpu_interrupt(cs, CPU_INTERRUPT_VSERR);
1056         } else {
1057             cpu_reset_interrupt(cs, CPU_INTERRUPT_VSERR);
1058         }
1059     }
1060 }
1061 
1062 #ifndef CONFIG_USER_ONLY
1063 static void arm_cpu_set_irq(void *opaque, int irq, int level)
1064 {
1065     ARMCPU *cpu = opaque;
1066     CPUARMState *env = &cpu->env;
1067     CPUState *cs = CPU(cpu);
1068     static const int mask[] = {
1069         [ARM_CPU_IRQ] = CPU_INTERRUPT_HARD,
1070         [ARM_CPU_FIQ] = CPU_INTERRUPT_FIQ,
1071         [ARM_CPU_VIRQ] = CPU_INTERRUPT_VIRQ,
1072         [ARM_CPU_VFIQ] = CPU_INTERRUPT_VFIQ,
1073         [ARM_CPU_NMI] = CPU_INTERRUPT_NMI,
1074         [ARM_CPU_VINMI] = CPU_INTERRUPT_VINMI,
1075     };
1076 
1077     if (!arm_feature(env, ARM_FEATURE_EL2) &&
1078         (irq == ARM_CPU_VIRQ || irq == ARM_CPU_VFIQ)) {
1079         /*
1080          * The GIC might tell us about VIRQ and VFIQ state, but if we don't
1081          * have EL2 support we don't care. (Unless the guest is doing something
1082          * silly this will only be calls saying "level is still 0".)
1083          */
1084         return;
1085     }
1086 
1087     if (level) {
1088         env->irq_line_state |= mask[irq];
1089     } else {
1090         env->irq_line_state &= ~mask[irq];
1091     }
1092 
1093     switch (irq) {
1094     case ARM_CPU_VIRQ:
1095         arm_cpu_update_virq(cpu);
1096         break;
1097     case ARM_CPU_VFIQ:
1098         arm_cpu_update_vfiq(cpu);
1099         break;
1100     case ARM_CPU_VINMI:
1101         arm_cpu_update_vinmi(cpu);
1102         break;
1103     case ARM_CPU_IRQ:
1104     case ARM_CPU_FIQ:
1105     case ARM_CPU_NMI:
1106         if (level) {
1107             cpu_interrupt(cs, mask[irq]);
1108         } else {
1109             cpu_reset_interrupt(cs, mask[irq]);
1110         }
1111         break;
1112     default:
1113         g_assert_not_reached();
1114     }
1115 }
1116 
1117 static void arm_cpu_kvm_set_irq(void *opaque, int irq, int level)
1118 {
1119 #ifdef CONFIG_KVM
1120     ARMCPU *cpu = opaque;
1121     CPUARMState *env = &cpu->env;
1122     CPUState *cs = CPU(cpu);
1123     uint32_t linestate_bit;
1124     int irq_id;
1125 
1126     switch (irq) {
1127     case ARM_CPU_IRQ:
1128         irq_id = KVM_ARM_IRQ_CPU_IRQ;
1129         linestate_bit = CPU_INTERRUPT_HARD;
1130         break;
1131     case ARM_CPU_FIQ:
1132         irq_id = KVM_ARM_IRQ_CPU_FIQ;
1133         linestate_bit = CPU_INTERRUPT_FIQ;
1134         break;
1135     default:
1136         g_assert_not_reached();
1137     }
1138 
1139     if (level) {
1140         env->irq_line_state |= linestate_bit;
1141     } else {
1142         env->irq_line_state &= ~linestate_bit;
1143     }
1144     kvm_arm_set_irq(cs->cpu_index, KVM_ARM_IRQ_TYPE_CPU, irq_id, !!level);
1145 #endif
1146 }
1147 
1148 static bool arm_cpu_virtio_is_big_endian(CPUState *cs)
1149 {
1150     ARMCPU *cpu = ARM_CPU(cs);
1151     CPUARMState *env = &cpu->env;
1152 
1153     cpu_synchronize_state(cs);
1154     return arm_cpu_data_is_big_endian(env);
1155 }
1156 
1157 #ifdef CONFIG_TCG
1158 bool arm_cpu_exec_halt(CPUState *cs)
1159 {
1160     bool leave_halt = cpu_has_work(cs);
1161 
1162     if (leave_halt) {
1163         /* We're about to come out of WFI/WFE: disable the WFxT timer */
1164         ARMCPU *cpu = ARM_CPU(cs);
1165         if (cpu->wfxt_timer) {
1166             timer_del(cpu->wfxt_timer);
1167         }
1168     }
1169     return leave_halt;
1170 }
1171 #endif
1172 
1173 static void arm_wfxt_timer_cb(void *opaque)
1174 {
1175     ARMCPU *cpu = opaque;
1176     CPUState *cs = CPU(cpu);
1177 
1178     /*
1179      * We expect the CPU to be halted; this will cause arm_cpu_is_work()
1180      * to return true (so we will come out of halt even with no other
1181      * pending interrupt), and the TCG accelerator's cpu_exec_interrupt()
1182      * function auto-clears the CPU_INTERRUPT_EXITTB flag for us.
1183      */
1184     cpu_interrupt(cs, CPU_INTERRUPT_EXITTB);
1185 }
1186 #endif
1187 
1188 static void arm_disas_set_info(CPUState *cpu, disassemble_info *info)
1189 {
1190     ARMCPU *ac = ARM_CPU(cpu);
1191     CPUARMState *env = &ac->env;
1192     bool sctlr_b;
1193 
1194     if (is_a64(env)) {
1195         info->cap_arch = CS_ARCH_ARM64;
1196         info->cap_insn_unit = 4;
1197         info->cap_insn_split = 4;
1198     } else {
1199         int cap_mode;
1200         if (env->thumb) {
1201             info->cap_insn_unit = 2;
1202             info->cap_insn_split = 4;
1203             cap_mode = CS_MODE_THUMB;
1204         } else {
1205             info->cap_insn_unit = 4;
1206             info->cap_insn_split = 4;
1207             cap_mode = CS_MODE_ARM;
1208         }
1209         if (arm_feature(env, ARM_FEATURE_V8)) {
1210             cap_mode |= CS_MODE_V8;
1211         }
1212         if (arm_feature(env, ARM_FEATURE_M)) {
1213             cap_mode |= CS_MODE_MCLASS;
1214         }
1215         info->cap_arch = CS_ARCH_ARM;
1216         info->cap_mode = cap_mode;
1217     }
1218 
1219     sctlr_b = arm_sctlr_b(env);
1220     if (bswap_code(sctlr_b)) {
1221 #if TARGET_BIG_ENDIAN
1222         info->endian = BFD_ENDIAN_LITTLE;
1223 #else
1224         info->endian = BFD_ENDIAN_BIG;
1225 #endif
1226     }
1227     info->flags &= ~INSN_ARM_BE32;
1228 #ifndef CONFIG_USER_ONLY
1229     if (sctlr_b) {
1230         info->flags |= INSN_ARM_BE32;
1231     }
1232 #endif
1233 }
1234 
1235 #ifdef TARGET_AARCH64
1236 
1237 static void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags)
1238 {
1239     ARMCPU *cpu = ARM_CPU(cs);
1240     CPUARMState *env = &cpu->env;
1241     uint32_t psr = pstate_read(env);
1242     int i, j;
1243     int el = arm_current_el(env);
1244     uint64_t hcr = arm_hcr_el2_eff(env);
1245     const char *ns_status;
1246     bool sve;
1247 
1248     qemu_fprintf(f, " PC=%016" PRIx64 " ", env->pc);
1249     for (i = 0; i < 32; i++) {
1250         if (i == 31) {
1251             qemu_fprintf(f, " SP=%016" PRIx64 "\n", env->xregs[i]);
1252         } else {
1253             qemu_fprintf(f, "X%02d=%016" PRIx64 "%s", i, env->xregs[i],
1254                          (i + 2) % 3 ? " " : "\n");
1255         }
1256     }
1257 
1258     if (arm_feature(env, ARM_FEATURE_EL3) && el != 3) {
1259         ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
1260     } else {
1261         ns_status = "";
1262     }
1263     qemu_fprintf(f, "PSTATE=%08x %c%c%c%c %sEL%d%c",
1264                  psr,
1265                  psr & PSTATE_N ? 'N' : '-',
1266                  psr & PSTATE_Z ? 'Z' : '-',
1267                  psr & PSTATE_C ? 'C' : '-',
1268                  psr & PSTATE_V ? 'V' : '-',
1269                  ns_status,
1270                  el,
1271                  psr & PSTATE_SP ? 'h' : 't');
1272 
1273     if (cpu_isar_feature(aa64_sme, cpu)) {
1274         qemu_fprintf(f, "  SVCR=%08" PRIx64 " %c%c",
1275                      env->svcr,
1276                      (FIELD_EX64(env->svcr, SVCR, ZA) ? 'Z' : '-'),
1277                      (FIELD_EX64(env->svcr, SVCR, SM) ? 'S' : '-'));
1278     }
1279     if (cpu_isar_feature(aa64_bti, cpu)) {
1280         qemu_fprintf(f, "  BTYPE=%d", (psr & PSTATE_BTYPE) >> 10);
1281     }
1282     qemu_fprintf(f, "%s%s%s",
1283                  (hcr & HCR_NV) ? " NV" : "",
1284                  (hcr & HCR_NV1) ? " NV1" : "",
1285                  (hcr & HCR_NV2) ? " NV2" : "");
1286     if (!(flags & CPU_DUMP_FPU)) {
1287         qemu_fprintf(f, "\n");
1288         return;
1289     }
1290     if (fp_exception_el(env, el) != 0) {
1291         qemu_fprintf(f, "    FPU disabled\n");
1292         return;
1293     }
1294     qemu_fprintf(f, "     FPCR=%08x FPSR=%08x\n",
1295                  vfp_get_fpcr(env), vfp_get_fpsr(env));
1296 
1297     if (cpu_isar_feature(aa64_sme, cpu) && FIELD_EX64(env->svcr, SVCR, SM)) {
1298         sve = sme_exception_el(env, el) == 0;
1299     } else if (cpu_isar_feature(aa64_sve, cpu)) {
1300         sve = sve_exception_el(env, el) == 0;
1301     } else {
1302         sve = false;
1303     }
1304 
1305     if (sve) {
1306         int zcr_len = sve_vqm1_for_el(env, el);
1307 
1308         for (i = 0; i <= FFR_PRED_NUM; i++) {
1309             bool eol;
1310             if (i == FFR_PRED_NUM) {
1311                 qemu_fprintf(f, "FFR=");
1312                 /* It's last, so end the line.  */
1313                 eol = true;
1314             } else {
1315                 qemu_fprintf(f, "P%02d=", i);
1316                 switch (zcr_len) {
1317                 case 0:
1318                     eol = i % 8 == 7;
1319                     break;
1320                 case 1:
1321                     eol = i % 6 == 5;
1322                     break;
1323                 case 2:
1324                 case 3:
1325                     eol = i % 3 == 2;
1326                     break;
1327                 default:
1328                     /* More than one quadword per predicate.  */
1329                     eol = true;
1330                     break;
1331                 }
1332             }
1333             for (j = zcr_len / 4; j >= 0; j--) {
1334                 int digits;
1335                 if (j * 4 + 4 <= zcr_len + 1) {
1336                     digits = 16;
1337                 } else {
1338                     digits = (zcr_len % 4 + 1) * 4;
1339                 }
1340                 qemu_fprintf(f, "%0*" PRIx64 "%s", digits,
1341                              env->vfp.pregs[i].p[j],
1342                              j ? ":" : eol ? "\n" : " ");
1343             }
1344         }
1345 
1346         if (zcr_len == 0) {
1347             /*
1348              * With vl=16, there are only 37 columns per register,
1349              * so output two registers per line.
1350              */
1351             for (i = 0; i < 32; i++) {
1352                 qemu_fprintf(f, "Z%02d=%016" PRIx64 ":%016" PRIx64 "%s",
1353                              i, env->vfp.zregs[i].d[1],
1354                              env->vfp.zregs[i].d[0], i & 1 ? "\n" : " ");
1355             }
1356         } else {
1357             for (i = 0; i < 32; i++) {
1358                 qemu_fprintf(f, "Z%02d=", i);
1359                 for (j = zcr_len; j >= 0; j--) {
1360                     qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%s",
1361                                  env->vfp.zregs[i].d[j * 2 + 1],
1362                                  env->vfp.zregs[i].d[j * 2 + 0],
1363                                  j ? ":" : "\n");
1364                 }
1365             }
1366         }
1367     } else {
1368         for (i = 0; i < 32; i++) {
1369             uint64_t *q = aa64_vfp_qreg(env, i);
1370             qemu_fprintf(f, "Q%02d=%016" PRIx64 ":%016" PRIx64 "%s",
1371                          i, q[1], q[0], (i & 1 ? "\n" : " "));
1372         }
1373     }
1374 
1375     if (cpu_isar_feature(aa64_sme, cpu) &&
1376         FIELD_EX64(env->svcr, SVCR, ZA) &&
1377         sme_exception_el(env, el) == 0) {
1378         int zcr_len = sve_vqm1_for_el_sm(env, el, true);
1379         int svl = (zcr_len + 1) * 16;
1380         int svl_lg10 = svl < 100 ? 2 : 3;
1381 
1382         for (i = 0; i < svl; i++) {
1383             qemu_fprintf(f, "ZA[%0*d]=", svl_lg10, i);
1384             for (j = zcr_len; j >= 0; --j) {
1385                 qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%c",
1386                              env->zarray[i].d[2 * j + 1],
1387                              env->zarray[i].d[2 * j],
1388                              j ? ':' : '\n');
1389             }
1390         }
1391     }
1392 }
1393 
1394 #else
1395 
1396 static inline void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags)
1397 {
1398     g_assert_not_reached();
1399 }
1400 
1401 #endif
1402 
1403 static void arm_cpu_dump_state(CPUState *cs, FILE *f, int flags)
1404 {
1405     ARMCPU *cpu = ARM_CPU(cs);
1406     CPUARMState *env = &cpu->env;
1407     int i;
1408 
1409     if (is_a64(env)) {
1410         aarch64_cpu_dump_state(cs, f, flags);
1411         return;
1412     }
1413 
1414     for (i = 0; i < 16; i++) {
1415         qemu_fprintf(f, "R%02d=%08x", i, env->regs[i]);
1416         if ((i % 4) == 3) {
1417             qemu_fprintf(f, "\n");
1418         } else {
1419             qemu_fprintf(f, " ");
1420         }
1421     }
1422 
1423     if (arm_feature(env, ARM_FEATURE_M)) {
1424         uint32_t xpsr = xpsr_read(env);
1425         const char *mode;
1426         const char *ns_status = "";
1427 
1428         if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
1429             ns_status = env->v7m.secure ? "S " : "NS ";
1430         }
1431 
1432         if (xpsr & XPSR_EXCP) {
1433             mode = "handler";
1434         } else {
1435             if (env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_NPRIV_MASK) {
1436                 mode = "unpriv-thread";
1437             } else {
1438                 mode = "priv-thread";
1439             }
1440         }
1441 
1442         qemu_fprintf(f, "XPSR=%08x %c%c%c%c %c %s%s\n",
1443                      xpsr,
1444                      xpsr & XPSR_N ? 'N' : '-',
1445                      xpsr & XPSR_Z ? 'Z' : '-',
1446                      xpsr & XPSR_C ? 'C' : '-',
1447                      xpsr & XPSR_V ? 'V' : '-',
1448                      xpsr & XPSR_T ? 'T' : 'A',
1449                      ns_status,
1450                      mode);
1451     } else {
1452         uint32_t psr = cpsr_read(env);
1453         const char *ns_status = "";
1454 
1455         if (arm_feature(env, ARM_FEATURE_EL3) &&
1456             (psr & CPSR_M) != ARM_CPU_MODE_MON) {
1457             ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
1458         }
1459 
1460         qemu_fprintf(f, "PSR=%08x %c%c%c%c %c %s%s%d\n",
1461                      psr,
1462                      psr & CPSR_N ? 'N' : '-',
1463                      psr & CPSR_Z ? 'Z' : '-',
1464                      psr & CPSR_C ? 'C' : '-',
1465                      psr & CPSR_V ? 'V' : '-',
1466                      psr & CPSR_T ? 'T' : 'A',
1467                      ns_status,
1468                      aarch32_mode_name(psr), (psr & 0x10) ? 32 : 26);
1469     }
1470 
1471     if (flags & CPU_DUMP_FPU) {
1472         int numvfpregs = 0;
1473         if (cpu_isar_feature(aa32_simd_r32, cpu)) {
1474             numvfpregs = 32;
1475         } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
1476             numvfpregs = 16;
1477         }
1478         for (i = 0; i < numvfpregs; i++) {
1479             uint64_t v = *aa32_vfp_dreg(env, i);
1480             qemu_fprintf(f, "s%02d=%08x s%02d=%08x d%02d=%016" PRIx64 "\n",
1481                          i * 2, (uint32_t)v,
1482                          i * 2 + 1, (uint32_t)(v >> 32),
1483                          i, v);
1484         }
1485         qemu_fprintf(f, "FPSCR: %08x\n", vfp_get_fpscr(env));
1486         if (cpu_isar_feature(aa32_mve, cpu)) {
1487             qemu_fprintf(f, "VPR: %08x\n", env->v7m.vpr);
1488         }
1489     }
1490 }
1491 
1492 uint64_t arm_build_mp_affinity(int idx, uint8_t clustersz)
1493 {
1494     uint32_t Aff1 = idx / clustersz;
1495     uint32_t Aff0 = idx % clustersz;
1496     return (Aff1 << ARM_AFF1_SHIFT) | Aff0;
1497 }
1498 
1499 uint64_t arm_cpu_mp_affinity(ARMCPU *cpu)
1500 {
1501     return cpu->mp_affinity;
1502 }
1503 
1504 static void arm_cpu_initfn(Object *obj)
1505 {
1506     ARMCPU *cpu = ARM_CPU(obj);
1507 
1508     cpu->cp_regs = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1509                                          NULL, g_free);
1510 
1511     QLIST_INIT(&cpu->pre_el_change_hooks);
1512     QLIST_INIT(&cpu->el_change_hooks);
1513 
1514 #ifdef CONFIG_USER_ONLY
1515 # ifdef TARGET_AARCH64
1516     /*
1517      * The linux kernel defaults to 512-bit for SVE, and 256-bit for SME.
1518      * These values were chosen to fit within the default signal frame.
1519      * See documentation for /proc/sys/abi/{sve,sme}_default_vector_length,
1520      * and our corresponding cpu property.
1521      */
1522     cpu->sve_default_vq = 4;
1523     cpu->sme_default_vq = 2;
1524 # endif
1525 #else
1526     /* Our inbound IRQ and FIQ lines */
1527     if (kvm_enabled()) {
1528         /*
1529          * VIRQ, VFIQ, NMI, VINMI are unused with KVM but we add
1530          * them to maintain the same interface as non-KVM CPUs.
1531          */
1532         qdev_init_gpio_in(DEVICE(cpu), arm_cpu_kvm_set_irq, 6);
1533     } else {
1534         qdev_init_gpio_in(DEVICE(cpu), arm_cpu_set_irq, 6);
1535     }
1536 
1537     qdev_init_gpio_out(DEVICE(cpu), cpu->gt_timer_outputs,
1538                        ARRAY_SIZE(cpu->gt_timer_outputs));
1539 
1540     qdev_init_gpio_out_named(DEVICE(cpu), &cpu->gicv3_maintenance_interrupt,
1541                              "gicv3-maintenance-interrupt", 1);
1542     qdev_init_gpio_out_named(DEVICE(cpu), &cpu->pmu_interrupt,
1543                              "pmu-interrupt", 1);
1544 #endif
1545 
1546     /* DTB consumers generally don't in fact care what the 'compatible'
1547      * string is, so always provide some string and trust that a hypothetical
1548      * picky DTB consumer will also provide a helpful error message.
1549      */
1550     cpu->dtb_compatible = "qemu,unknown";
1551     cpu->psci_version = QEMU_PSCI_VERSION_0_1; /* By default assume PSCI v0.1 */
1552     cpu->kvm_target = QEMU_KVM_ARM_TARGET_NONE;
1553 
1554     if (tcg_enabled() || hvf_enabled()) {
1555         /* TCG and HVF implement PSCI 1.1 */
1556         cpu->psci_version = QEMU_PSCI_VERSION_1_1;
1557     }
1558 }
1559 
1560 /*
1561  * 0 means "unset, use the default value". That default might vary depending
1562  * on the CPU type, and is set in the realize fn.
1563  */
1564 static const Property arm_cpu_gt_cntfrq_property =
1565             DEFINE_PROP_UINT64("cntfrq", ARMCPU, gt_cntfrq_hz, 0);
1566 
1567 static const Property arm_cpu_reset_cbar_property =
1568             DEFINE_PROP_UINT64("reset-cbar", ARMCPU, reset_cbar, 0);
1569 
1570 static const Property arm_cpu_reset_hivecs_property =
1571             DEFINE_PROP_BOOL("reset-hivecs", ARMCPU, reset_hivecs, false);
1572 
1573 #ifndef CONFIG_USER_ONLY
1574 static const Property arm_cpu_has_el2_property =
1575             DEFINE_PROP_BOOL("has_el2", ARMCPU, has_el2, true);
1576 
1577 static const Property arm_cpu_has_el3_property =
1578             DEFINE_PROP_BOOL("has_el3", ARMCPU, has_el3, true);
1579 #endif
1580 
1581 static const Property arm_cpu_cfgend_property =
1582             DEFINE_PROP_BOOL("cfgend", ARMCPU, cfgend, false);
1583 
1584 static const Property arm_cpu_has_vfp_property =
1585             DEFINE_PROP_BOOL("vfp", ARMCPU, has_vfp, true);
1586 
1587 static const Property arm_cpu_has_vfp_d32_property =
1588             DEFINE_PROP_BOOL("vfp-d32", ARMCPU, has_vfp_d32, true);
1589 
1590 static const Property arm_cpu_has_neon_property =
1591             DEFINE_PROP_BOOL("neon", ARMCPU, has_neon, true);
1592 
1593 static const Property arm_cpu_has_dsp_property =
1594             DEFINE_PROP_BOOL("dsp", ARMCPU, has_dsp, true);
1595 
1596 static const Property arm_cpu_has_mpu_property =
1597             DEFINE_PROP_BOOL("has-mpu", ARMCPU, has_mpu, true);
1598 
1599 /* This is like DEFINE_PROP_UINT32 but it doesn't set the default value,
1600  * because the CPU initfn will have already set cpu->pmsav7_dregion to
1601  * the right value for that particular CPU type, and we don't want
1602  * to override that with an incorrect constant value.
1603  */
1604 static const Property arm_cpu_pmsav7_dregion_property =
1605             DEFINE_PROP_UNSIGNED_NODEFAULT("pmsav7-dregion", ARMCPU,
1606                                            pmsav7_dregion,
1607                                            qdev_prop_uint32, uint32_t);
1608 
1609 static bool arm_get_pmu(Object *obj, Error **errp)
1610 {
1611     ARMCPU *cpu = ARM_CPU(obj);
1612 
1613     return cpu->has_pmu;
1614 }
1615 
1616 static void arm_set_pmu(Object *obj, bool value, Error **errp)
1617 {
1618     ARMCPU *cpu = ARM_CPU(obj);
1619 
1620     if (value) {
1621         if (kvm_enabled() && !kvm_arm_pmu_supported()) {
1622             error_setg(errp, "'pmu' feature not supported by KVM on this host");
1623             return;
1624         }
1625         set_feature(&cpu->env, ARM_FEATURE_PMU);
1626     } else {
1627         unset_feature(&cpu->env, ARM_FEATURE_PMU);
1628     }
1629     cpu->has_pmu = value;
1630 }
1631 
1632 unsigned int gt_cntfrq_period_ns(ARMCPU *cpu)
1633 {
1634     /*
1635      * The exact approach to calculating guest ticks is:
1636      *
1637      *     muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), cpu->gt_cntfrq_hz,
1638      *              NANOSECONDS_PER_SECOND);
1639      *
1640      * We don't do that. Rather we intentionally use integer division
1641      * truncation below and in the caller for the conversion of host monotonic
1642      * time to guest ticks to provide the exact inverse for the semantics of
1643      * the QEMUTimer scale factor. QEMUTimer's scale facter is an integer, so
1644      * it loses precision when representing frequencies where
1645      * `(NANOSECONDS_PER_SECOND % cpu->gt_cntfrq) > 0` holds. Failing to
1646      * provide an exact inverse leads to scheduling timers with negative
1647      * periods, which in turn leads to sticky behaviour in the guest.
1648      *
1649      * Finally, CNTFRQ is effectively capped at 1GHz to ensure our scale factor
1650      * cannot become zero.
1651      */
1652     return NANOSECONDS_PER_SECOND > cpu->gt_cntfrq_hz ?
1653       NANOSECONDS_PER_SECOND / cpu->gt_cntfrq_hz : 1;
1654 }
1655 
1656 static void arm_cpu_propagate_feature_implications(ARMCPU *cpu)
1657 {
1658     CPUARMState *env = &cpu->env;
1659     bool no_aa32 = false;
1660 
1661     /*
1662      * Some features automatically imply others: set the feature
1663      * bits explicitly for these cases.
1664      */
1665 
1666     if (arm_feature(env, ARM_FEATURE_M)) {
1667         set_feature(env, ARM_FEATURE_PMSA);
1668     }
1669 
1670     if (arm_feature(env, ARM_FEATURE_V8)) {
1671         if (arm_feature(env, ARM_FEATURE_M)) {
1672             set_feature(env, ARM_FEATURE_V7);
1673         } else {
1674             set_feature(env, ARM_FEATURE_V7VE);
1675         }
1676     }
1677 
1678     /*
1679      * There exist AArch64 cpus without AArch32 support.  When KVM
1680      * queries ID_ISAR0_EL1 on such a host, the value is UNKNOWN.
1681      * Similarly, we cannot check ID_AA64PFR0 without AArch64 support.
1682      * As a general principle, we also do not make ID register
1683      * consistency checks anywhere unless using TCG, because only
1684      * for TCG would a consistency-check failure be a QEMU bug.
1685      */
1686     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
1687         no_aa32 = !cpu_isar_feature(aa64_aa32, cpu);
1688     }
1689 
1690     if (arm_feature(env, ARM_FEATURE_V7VE)) {
1691         /*
1692          * v7 Virtualization Extensions. In real hardware this implies
1693          * EL2 and also the presence of the Security Extensions.
1694          * For QEMU, for backwards-compatibility we implement some
1695          * CPUs or CPU configs which have no actual EL2 or EL3 but do
1696          * include the various other features that V7VE implies.
1697          * Presence of EL2 itself is ARM_FEATURE_EL2, and of the
1698          * Security Extensions is ARM_FEATURE_EL3.
1699          */
1700         assert(!tcg_enabled() || no_aa32 ||
1701                cpu_isar_feature(aa32_arm_div, cpu));
1702         set_feature(env, ARM_FEATURE_LPAE);
1703         set_feature(env, ARM_FEATURE_V7);
1704     }
1705     if (arm_feature(env, ARM_FEATURE_V7)) {
1706         set_feature(env, ARM_FEATURE_VAPA);
1707         set_feature(env, ARM_FEATURE_THUMB2);
1708         set_feature(env, ARM_FEATURE_MPIDR);
1709         if (!arm_feature(env, ARM_FEATURE_M)) {
1710             set_feature(env, ARM_FEATURE_V6K);
1711         } else {
1712             set_feature(env, ARM_FEATURE_V6);
1713         }
1714 
1715         /*
1716          * Always define VBAR for V7 CPUs even if it doesn't exist in
1717          * non-EL3 configs. This is needed by some legacy boards.
1718          */
1719         set_feature(env, ARM_FEATURE_VBAR);
1720     }
1721     if (arm_feature(env, ARM_FEATURE_V6K)) {
1722         set_feature(env, ARM_FEATURE_V6);
1723         set_feature(env, ARM_FEATURE_MVFR);
1724     }
1725     if (arm_feature(env, ARM_FEATURE_V6)) {
1726         set_feature(env, ARM_FEATURE_V5);
1727         if (!arm_feature(env, ARM_FEATURE_M)) {
1728             assert(!tcg_enabled() || no_aa32 ||
1729                    cpu_isar_feature(aa32_jazelle, cpu));
1730             set_feature(env, ARM_FEATURE_AUXCR);
1731         }
1732     }
1733     if (arm_feature(env, ARM_FEATURE_V5)) {
1734         set_feature(env, ARM_FEATURE_V4T);
1735     }
1736     if (arm_feature(env, ARM_FEATURE_LPAE)) {
1737         set_feature(env, ARM_FEATURE_V7MP);
1738     }
1739     if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
1740         set_feature(env, ARM_FEATURE_CBAR);
1741     }
1742     if (arm_feature(env, ARM_FEATURE_THUMB2) &&
1743         !arm_feature(env, ARM_FEATURE_M)) {
1744         set_feature(env, ARM_FEATURE_THUMB_DSP);
1745     }
1746 }
1747 
1748 void arm_cpu_post_init(Object *obj)
1749 {
1750     ARMCPU *cpu = ARM_CPU(obj);
1751 
1752     /*
1753      * Some features imply others. Figure this out now, because we
1754      * are going to look at the feature bits in deciding which
1755      * properties to add.
1756      */
1757     arm_cpu_propagate_feature_implications(cpu);
1758 
1759     if (arm_feature(&cpu->env, ARM_FEATURE_CBAR) ||
1760         arm_feature(&cpu->env, ARM_FEATURE_CBAR_RO)) {
1761         qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_cbar_property);
1762     }
1763 
1764     if (!arm_feature(&cpu->env, ARM_FEATURE_M)) {
1765         qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_hivecs_property);
1766     }
1767 
1768     if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1769         object_property_add_uint64_ptr(obj, "rvbar",
1770                                        &cpu->rvbar_prop,
1771                                        OBJ_PROP_FLAG_READWRITE);
1772     }
1773 
1774 #ifndef CONFIG_USER_ONLY
1775     if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
1776         /* Add the has_el3 state CPU property only if EL3 is allowed.  This will
1777          * prevent "has_el3" from existing on CPUs which cannot support EL3.
1778          */
1779         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el3_property);
1780 
1781         object_property_add_link(obj, "secure-memory",
1782                                  TYPE_MEMORY_REGION,
1783                                  (Object **)&cpu->secure_memory,
1784                                  qdev_prop_allow_set_link_before_realize,
1785                                  OBJ_PROP_LINK_STRONG);
1786     }
1787 
1788     if (arm_feature(&cpu->env, ARM_FEATURE_EL2)) {
1789         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el2_property);
1790     }
1791 #endif
1792 
1793     if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) {
1794         cpu->has_pmu = true;
1795         object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu);
1796     }
1797 
1798     /*
1799      * Allow user to turn off VFP and Neon support, but only for TCG --
1800      * KVM does not currently allow us to lie to the guest about its
1801      * ID/feature registers, so the guest always sees what the host has.
1802      */
1803     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
1804         if (cpu_isar_feature(aa64_fp_simd, cpu)) {
1805             cpu->has_vfp = true;
1806             cpu->has_vfp_d32 = true;
1807             if (tcg_enabled() || qtest_enabled()) {
1808                 qdev_property_add_static(DEVICE(obj),
1809                                          &arm_cpu_has_vfp_property);
1810             }
1811         }
1812     } else if (cpu_isar_feature(aa32_vfp, cpu)) {
1813         cpu->has_vfp = true;
1814         if (tcg_enabled() || qtest_enabled()) {
1815             qdev_property_add_static(DEVICE(obj),
1816                                      &arm_cpu_has_vfp_property);
1817         }
1818         if (cpu_isar_feature(aa32_simd_r32, cpu)) {
1819             cpu->has_vfp_d32 = true;
1820             /*
1821              * The permitted values of the SIMDReg bits [3:0] on
1822              * Armv8-A are either 0b0000 and 0b0010. On such CPUs,
1823              * make sure that has_vfp_d32 can not be set to false.
1824              */
1825             if ((tcg_enabled() || qtest_enabled())
1826                 && !(arm_feature(&cpu->env, ARM_FEATURE_V8)
1827                      && !arm_feature(&cpu->env, ARM_FEATURE_M))) {
1828                 qdev_property_add_static(DEVICE(obj),
1829                                          &arm_cpu_has_vfp_d32_property);
1830             }
1831         }
1832     }
1833 
1834     if (arm_feature(&cpu->env, ARM_FEATURE_NEON)) {
1835         cpu->has_neon = true;
1836         if (!kvm_enabled()) {
1837             qdev_property_add_static(DEVICE(obj), &arm_cpu_has_neon_property);
1838         }
1839     }
1840 
1841     if (arm_feature(&cpu->env, ARM_FEATURE_M) &&
1842         arm_feature(&cpu->env, ARM_FEATURE_THUMB_DSP)) {
1843         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_dsp_property);
1844     }
1845 
1846     if (arm_feature(&cpu->env, ARM_FEATURE_PMSA)) {
1847         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_mpu_property);
1848         if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
1849             qdev_property_add_static(DEVICE(obj),
1850                                      &arm_cpu_pmsav7_dregion_property);
1851         }
1852     }
1853 
1854     if (arm_feature(&cpu->env, ARM_FEATURE_M_SECURITY)) {
1855         object_property_add_link(obj, "idau", TYPE_IDAU_INTERFACE, &cpu->idau,
1856                                  qdev_prop_allow_set_link_before_realize,
1857                                  OBJ_PROP_LINK_STRONG);
1858         /*
1859          * M profile: initial value of the Secure VTOR. We can't just use
1860          * a simple DEFINE_PROP_UINT32 for this because we want to permit
1861          * the property to be set after realize.
1862          */
1863         object_property_add_uint32_ptr(obj, "init-svtor",
1864                                        &cpu->init_svtor,
1865                                        OBJ_PROP_FLAG_READWRITE);
1866     }
1867     if (arm_feature(&cpu->env, ARM_FEATURE_M)) {
1868         /*
1869          * Initial value of the NS VTOR (for cores without the Security
1870          * extension, this is the only VTOR)
1871          */
1872         object_property_add_uint32_ptr(obj, "init-nsvtor",
1873                                        &cpu->init_nsvtor,
1874                                        OBJ_PROP_FLAG_READWRITE);
1875     }
1876 
1877     /* Not DEFINE_PROP_UINT32: we want this to be settable after realize */
1878     object_property_add_uint32_ptr(obj, "psci-conduit",
1879                                    &cpu->psci_conduit,
1880                                    OBJ_PROP_FLAG_READWRITE);
1881 
1882     qdev_property_add_static(DEVICE(obj), &arm_cpu_cfgend_property);
1883 
1884     if (arm_feature(&cpu->env, ARM_FEATURE_GENERIC_TIMER)) {
1885         qdev_property_add_static(DEVICE(cpu), &arm_cpu_gt_cntfrq_property);
1886     }
1887 
1888     if (kvm_enabled()) {
1889         kvm_arm_add_vcpu_properties(cpu);
1890     }
1891 
1892 #ifndef CONFIG_USER_ONLY
1893     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) &&
1894         cpu_isar_feature(aa64_mte, cpu)) {
1895         object_property_add_link(obj, "tag-memory",
1896                                  TYPE_MEMORY_REGION,
1897                                  (Object **)&cpu->tag_memory,
1898                                  qdev_prop_allow_set_link_before_realize,
1899                                  OBJ_PROP_LINK_STRONG);
1900 
1901         if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
1902             object_property_add_link(obj, "secure-tag-memory",
1903                                      TYPE_MEMORY_REGION,
1904                                      (Object **)&cpu->secure_tag_memory,
1905                                      qdev_prop_allow_set_link_before_realize,
1906                                      OBJ_PROP_LINK_STRONG);
1907         }
1908     }
1909 #endif
1910 }
1911 
1912 static void arm_cpu_finalizefn(Object *obj)
1913 {
1914     ARMCPU *cpu = ARM_CPU(obj);
1915     ARMELChangeHook *hook, *next;
1916 
1917     g_hash_table_destroy(cpu->cp_regs);
1918 
1919     QLIST_FOREACH_SAFE(hook, &cpu->pre_el_change_hooks, node, next) {
1920         QLIST_REMOVE(hook, node);
1921         g_free(hook);
1922     }
1923     QLIST_FOREACH_SAFE(hook, &cpu->el_change_hooks, node, next) {
1924         QLIST_REMOVE(hook, node);
1925         g_free(hook);
1926     }
1927 #ifndef CONFIG_USER_ONLY
1928     if (cpu->pmu_timer) {
1929         timer_free(cpu->pmu_timer);
1930     }
1931     if (cpu->wfxt_timer) {
1932         timer_free(cpu->wfxt_timer);
1933     }
1934 #endif
1935 }
1936 
1937 void arm_cpu_finalize_features(ARMCPU *cpu, Error **errp)
1938 {
1939     Error *local_err = NULL;
1940 
1941 #ifdef TARGET_AARCH64
1942     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
1943         arm_cpu_sve_finalize(cpu, &local_err);
1944         if (local_err != NULL) {
1945             error_propagate(errp, local_err);
1946             return;
1947         }
1948 
1949         /*
1950          * FEAT_SME is not architecturally dependent on FEAT_SVE (unless
1951          * FEAT_SME_FA64 is present). However our implementation currently
1952          * assumes it, so if the user asked for sve=off then turn off SME also.
1953          * (KVM doesn't currently support SME at all.)
1954          */
1955         if (cpu_isar_feature(aa64_sme, cpu) && !cpu_isar_feature(aa64_sve, cpu)) {
1956             object_property_set_bool(OBJECT(cpu), "sme", false, &error_abort);
1957         }
1958 
1959         arm_cpu_sme_finalize(cpu, &local_err);
1960         if (local_err != NULL) {
1961             error_propagate(errp, local_err);
1962             return;
1963         }
1964 
1965         arm_cpu_pauth_finalize(cpu, &local_err);
1966         if (local_err != NULL) {
1967             error_propagate(errp, local_err);
1968             return;
1969         }
1970 
1971         arm_cpu_lpa2_finalize(cpu, &local_err);
1972         if (local_err != NULL) {
1973             error_propagate(errp, local_err);
1974             return;
1975         }
1976     }
1977 #endif
1978 
1979     if (kvm_enabled()) {
1980         kvm_arm_steal_time_finalize(cpu, &local_err);
1981         if (local_err != NULL) {
1982             error_propagate(errp, local_err);
1983             return;
1984         }
1985     }
1986 }
1987 
1988 static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
1989 {
1990     CPUState *cs = CPU(dev);
1991     ARMCPU *cpu = ARM_CPU(dev);
1992     ARMCPUClass *acc = ARM_CPU_GET_CLASS(dev);
1993     CPUARMState *env = &cpu->env;
1994     Error *local_err = NULL;
1995 
1996 #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
1997     /* Use pc-relative instructions in system-mode */
1998     tcg_cflags_set(cs, CF_PCREL);
1999 #endif
2000 
2001     /* If we needed to query the host kernel for the CPU features
2002      * then it's possible that might have failed in the initfn, but
2003      * this is the first point where we can report it.
2004      */
2005     if (cpu->host_cpu_probe_failed) {
2006         if (!kvm_enabled() && !hvf_enabled()) {
2007             error_setg(errp, "The 'host' CPU type can only be used with KVM or HVF");
2008         } else {
2009             error_setg(errp, "Failed to retrieve host CPU features");
2010         }
2011         return;
2012     }
2013 
2014     if (!cpu->gt_cntfrq_hz) {
2015         /*
2016          * 0 means "the board didn't set a value, use the default". (We also
2017          * get here for the CONFIG_USER_ONLY case.)
2018          * ARMv8.6 and later CPUs architecturally must use a 1GHz timer; before
2019          * that it was an IMPDEF choice, and QEMU initially picked 62.5MHz,
2020          * which gives a 16ns tick period.
2021          *
2022          * We will use the back-compat value:
2023          *  - for QEMU CPU types added before we standardized on 1GHz
2024          *  - for versioned machine types with a version of 9.0 or earlier
2025          */
2026         if (arm_feature(env, ARM_FEATURE_BACKCOMPAT_CNTFRQ) ||
2027             cpu->backcompat_cntfrq) {
2028             cpu->gt_cntfrq_hz = GTIMER_BACKCOMPAT_HZ;
2029         } else {
2030             cpu->gt_cntfrq_hz = GTIMER_DEFAULT_HZ;
2031         }
2032     }
2033 
2034 #ifndef CONFIG_USER_ONLY
2035     /* The NVIC and M-profile CPU are two halves of a single piece of
2036      * hardware; trying to use one without the other is a command line
2037      * error and will result in segfaults if not caught here.
2038      */
2039     if (arm_feature(env, ARM_FEATURE_M)) {
2040         if (!env->nvic) {
2041             error_setg(errp, "This board cannot be used with Cortex-M CPUs");
2042             return;
2043         }
2044     } else {
2045         if (env->nvic) {
2046             error_setg(errp, "This board can only be used with Cortex-M CPUs");
2047             return;
2048         }
2049     }
2050 
2051     if (!tcg_enabled() && !qtest_enabled()) {
2052         /*
2053          * We assume that no accelerator except TCG (and the "not really an
2054          * accelerator" qtest) can handle these features, because Arm hardware
2055          * virtualization can't virtualize them.
2056          *
2057          * Catch all the cases which might cause us to create more than one
2058          * address space for the CPU (otherwise we will assert() later in
2059          * cpu_address_space_init()).
2060          */
2061         if (arm_feature(env, ARM_FEATURE_M)) {
2062             error_setg(errp,
2063                        "Cannot enable %s when using an M-profile guest CPU",
2064                        current_accel_name());
2065             return;
2066         }
2067         if (cpu->has_el3) {
2068             error_setg(errp,
2069                        "Cannot enable %s when guest CPU has EL3 enabled",
2070                        current_accel_name());
2071             return;
2072         }
2073         if (cpu->tag_memory) {
2074             error_setg(errp,
2075                        "Cannot enable %s when guest CPUs has MTE enabled",
2076                        current_accel_name());
2077             return;
2078         }
2079     }
2080 
2081     {
2082         uint64_t scale = gt_cntfrq_period_ns(cpu);
2083 
2084         cpu->gt_timer[GTIMER_PHYS] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
2085                                                arm_gt_ptimer_cb, cpu);
2086         cpu->gt_timer[GTIMER_VIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
2087                                                arm_gt_vtimer_cb, cpu);
2088         cpu->gt_timer[GTIMER_HYP] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
2089                                               arm_gt_htimer_cb, cpu);
2090         cpu->gt_timer[GTIMER_SEC] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
2091                                               arm_gt_stimer_cb, cpu);
2092         cpu->gt_timer[GTIMER_HYPVIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
2093                                                   arm_gt_hvtimer_cb, cpu);
2094     }
2095 #endif
2096 
2097     cpu_exec_realizefn(cs, &local_err);
2098     if (local_err != NULL) {
2099         error_propagate(errp, local_err);
2100         return;
2101     }
2102 
2103     arm_cpu_finalize_features(cpu, &local_err);
2104     if (local_err != NULL) {
2105         error_propagate(errp, local_err);
2106         return;
2107     }
2108 
2109 #ifdef CONFIG_USER_ONLY
2110     /*
2111      * User mode relies on IC IVAU instructions to catch modification of
2112      * dual-mapped code.
2113      *
2114      * Clear CTR_EL0.DIC to ensure that software that honors these flags uses
2115      * IC IVAU even if the emulated processor does not normally require it.
2116      */
2117     cpu->ctr = FIELD_DP64(cpu->ctr, CTR_EL0, DIC, 0);
2118 #endif
2119 
2120     if (arm_feature(env, ARM_FEATURE_AARCH64) &&
2121         cpu->has_vfp != cpu->has_neon) {
2122         /*
2123          * This is an architectural requirement for AArch64; AArch32 is
2124          * more flexible and permits VFP-no-Neon and Neon-no-VFP.
2125          */
2126         error_setg(errp,
2127                    "AArch64 CPUs must have both VFP and Neon or neither");
2128         return;
2129     }
2130 
2131     if (cpu->has_vfp_d32 != cpu->has_neon) {
2132         error_setg(errp, "ARM CPUs must have both VFP-D32 and Neon or neither");
2133         return;
2134     }
2135 
2136    if (!cpu->has_vfp_d32) {
2137         uint32_t u;
2138 
2139         u = cpu->isar.mvfr0;
2140         u = FIELD_DP32(u, MVFR0, SIMDREG, 1); /* 16 registers */
2141         cpu->isar.mvfr0 = u;
2142     }
2143 
2144     if (!cpu->has_vfp) {
2145         uint64_t t;
2146         uint32_t u;
2147 
2148         t = cpu->isar.id_aa64isar1;
2149         t = FIELD_DP64(t, ID_AA64ISAR1, JSCVT, 0);
2150         cpu->isar.id_aa64isar1 = t;
2151 
2152         t = cpu->isar.id_aa64pfr0;
2153         t = FIELD_DP64(t, ID_AA64PFR0, FP, 0xf);
2154         cpu->isar.id_aa64pfr0 = t;
2155 
2156         u = cpu->isar.id_isar6;
2157         u = FIELD_DP32(u, ID_ISAR6, JSCVT, 0);
2158         u = FIELD_DP32(u, ID_ISAR6, BF16, 0);
2159         cpu->isar.id_isar6 = u;
2160 
2161         u = cpu->isar.mvfr0;
2162         u = FIELD_DP32(u, MVFR0, FPSP, 0);
2163         u = FIELD_DP32(u, MVFR0, FPDP, 0);
2164         u = FIELD_DP32(u, MVFR0, FPDIVIDE, 0);
2165         u = FIELD_DP32(u, MVFR0, FPSQRT, 0);
2166         u = FIELD_DP32(u, MVFR0, FPROUND, 0);
2167         if (!arm_feature(env, ARM_FEATURE_M)) {
2168             u = FIELD_DP32(u, MVFR0, FPTRAP, 0);
2169             u = FIELD_DP32(u, MVFR0, FPSHVEC, 0);
2170         }
2171         cpu->isar.mvfr0 = u;
2172 
2173         u = cpu->isar.mvfr1;
2174         u = FIELD_DP32(u, MVFR1, FPFTZ, 0);
2175         u = FIELD_DP32(u, MVFR1, FPDNAN, 0);
2176         u = FIELD_DP32(u, MVFR1, FPHP, 0);
2177         if (arm_feature(env, ARM_FEATURE_M)) {
2178             u = FIELD_DP32(u, MVFR1, FP16, 0);
2179         }
2180         cpu->isar.mvfr1 = u;
2181 
2182         u = cpu->isar.mvfr2;
2183         u = FIELD_DP32(u, MVFR2, FPMISC, 0);
2184         cpu->isar.mvfr2 = u;
2185     }
2186 
2187     if (!cpu->has_neon) {
2188         uint64_t t;
2189         uint32_t u;
2190 
2191         unset_feature(env, ARM_FEATURE_NEON);
2192 
2193         t = cpu->isar.id_aa64isar0;
2194         t = FIELD_DP64(t, ID_AA64ISAR0, AES, 0);
2195         t = FIELD_DP64(t, ID_AA64ISAR0, SHA1, 0);
2196         t = FIELD_DP64(t, ID_AA64ISAR0, SHA2, 0);
2197         t = FIELD_DP64(t, ID_AA64ISAR0, SHA3, 0);
2198         t = FIELD_DP64(t, ID_AA64ISAR0, SM3, 0);
2199         t = FIELD_DP64(t, ID_AA64ISAR0, SM4, 0);
2200         t = FIELD_DP64(t, ID_AA64ISAR0, DP, 0);
2201         cpu->isar.id_aa64isar0 = t;
2202 
2203         t = cpu->isar.id_aa64isar1;
2204         t = FIELD_DP64(t, ID_AA64ISAR1, FCMA, 0);
2205         t = FIELD_DP64(t, ID_AA64ISAR1, BF16, 0);
2206         t = FIELD_DP64(t, ID_AA64ISAR1, I8MM, 0);
2207         cpu->isar.id_aa64isar1 = t;
2208 
2209         t = cpu->isar.id_aa64pfr0;
2210         t = FIELD_DP64(t, ID_AA64PFR0, ADVSIMD, 0xf);
2211         cpu->isar.id_aa64pfr0 = t;
2212 
2213         u = cpu->isar.id_isar5;
2214         u = FIELD_DP32(u, ID_ISAR5, AES, 0);
2215         u = FIELD_DP32(u, ID_ISAR5, SHA1, 0);
2216         u = FIELD_DP32(u, ID_ISAR5, SHA2, 0);
2217         u = FIELD_DP32(u, ID_ISAR5, RDM, 0);
2218         u = FIELD_DP32(u, ID_ISAR5, VCMA, 0);
2219         cpu->isar.id_isar5 = u;
2220 
2221         u = cpu->isar.id_isar6;
2222         u = FIELD_DP32(u, ID_ISAR6, DP, 0);
2223         u = FIELD_DP32(u, ID_ISAR6, FHM, 0);
2224         u = FIELD_DP32(u, ID_ISAR6, BF16, 0);
2225         u = FIELD_DP32(u, ID_ISAR6, I8MM, 0);
2226         cpu->isar.id_isar6 = u;
2227 
2228         if (!arm_feature(env, ARM_FEATURE_M)) {
2229             u = cpu->isar.mvfr1;
2230             u = FIELD_DP32(u, MVFR1, SIMDLS, 0);
2231             u = FIELD_DP32(u, MVFR1, SIMDINT, 0);
2232             u = FIELD_DP32(u, MVFR1, SIMDSP, 0);
2233             u = FIELD_DP32(u, MVFR1, SIMDHP, 0);
2234             cpu->isar.mvfr1 = u;
2235 
2236             u = cpu->isar.mvfr2;
2237             u = FIELD_DP32(u, MVFR2, SIMDMISC, 0);
2238             cpu->isar.mvfr2 = u;
2239         }
2240     }
2241 
2242     if (!cpu->has_neon && !cpu->has_vfp) {
2243         uint64_t t;
2244         uint32_t u;
2245 
2246         t = cpu->isar.id_aa64isar0;
2247         t = FIELD_DP64(t, ID_AA64ISAR0, FHM, 0);
2248         cpu->isar.id_aa64isar0 = t;
2249 
2250         t = cpu->isar.id_aa64isar1;
2251         t = FIELD_DP64(t, ID_AA64ISAR1, FRINTTS, 0);
2252         cpu->isar.id_aa64isar1 = t;
2253 
2254         u = cpu->isar.mvfr0;
2255         u = FIELD_DP32(u, MVFR0, SIMDREG, 0);
2256         cpu->isar.mvfr0 = u;
2257 
2258         /* Despite the name, this field covers both VFP and Neon */
2259         u = cpu->isar.mvfr1;
2260         u = FIELD_DP32(u, MVFR1, SIMDFMAC, 0);
2261         cpu->isar.mvfr1 = u;
2262     }
2263 
2264     if (arm_feature(env, ARM_FEATURE_M) && !cpu->has_dsp) {
2265         uint32_t u;
2266 
2267         unset_feature(env, ARM_FEATURE_THUMB_DSP);
2268 
2269         u = cpu->isar.id_isar1;
2270         u = FIELD_DP32(u, ID_ISAR1, EXTEND, 1);
2271         cpu->isar.id_isar1 = u;
2272 
2273         u = cpu->isar.id_isar2;
2274         u = FIELD_DP32(u, ID_ISAR2, MULTU, 1);
2275         u = FIELD_DP32(u, ID_ISAR2, MULTS, 1);
2276         cpu->isar.id_isar2 = u;
2277 
2278         u = cpu->isar.id_isar3;
2279         u = FIELD_DP32(u, ID_ISAR3, SIMD, 1);
2280         u = FIELD_DP32(u, ID_ISAR3, SATURATE, 0);
2281         cpu->isar.id_isar3 = u;
2282     }
2283 
2284 
2285     /*
2286      * We rely on no XScale CPU having VFP so we can use the same bits in the
2287      * TB flags field for VECSTRIDE and XSCALE_CPAR.
2288      */
2289     assert(arm_feature(env, ARM_FEATURE_AARCH64) ||
2290            !cpu_isar_feature(aa32_vfp_simd, cpu) ||
2291            !arm_feature(env, ARM_FEATURE_XSCALE));
2292 
2293 #ifndef CONFIG_USER_ONLY
2294     {
2295         int pagebits;
2296         if (arm_feature(env, ARM_FEATURE_V7) &&
2297             !arm_feature(env, ARM_FEATURE_M) &&
2298             !arm_feature(env, ARM_FEATURE_PMSA)) {
2299             /*
2300              * v7VMSA drops support for the old ARMv5 tiny pages,
2301              * so we can use 4K pages.
2302              */
2303             pagebits = 12;
2304         } else {
2305             /*
2306              * For CPUs which might have tiny 1K pages, or which have an
2307              * MPU and might have small region sizes, stick with 1K pages.
2308              */
2309             pagebits = 10;
2310         }
2311         if (!set_preferred_target_page_bits(pagebits)) {
2312             /*
2313              * This can only ever happen for hotplugging a CPU, or if
2314              * the board code incorrectly creates a CPU which it has
2315              * promised via minimum_page_size that it will not.
2316              */
2317             error_setg(errp, "This CPU requires a smaller page size "
2318                        "than the system is using");
2319             return;
2320         }
2321     }
2322 #endif
2323 
2324     /* This cpu-id-to-MPIDR affinity is used only for TCG; KVM will override it.
2325      * We don't support setting cluster ID ([16..23]) (known as Aff2
2326      * in later ARM ARM versions), or any of the higher affinity level fields,
2327      * so these bits always RAZ.
2328      */
2329     if (cpu->mp_affinity == ARM64_AFFINITY_INVALID) {
2330         cpu->mp_affinity = arm_build_mp_affinity(cs->cpu_index,
2331                                                  ARM_DEFAULT_CPUS_PER_CLUSTER);
2332     }
2333 
2334     if (cpu->reset_hivecs) {
2335             cpu->reset_sctlr |= (1 << 13);
2336     }
2337 
2338     if (cpu->cfgend) {
2339         if (arm_feature(env, ARM_FEATURE_V7)) {
2340             cpu->reset_sctlr |= SCTLR_EE;
2341         } else {
2342             cpu->reset_sctlr |= SCTLR_B;
2343         }
2344     }
2345 
2346     if (!arm_feature(env, ARM_FEATURE_M) && !cpu->has_el3) {
2347         /* If the has_el3 CPU property is disabled then we need to disable the
2348          * feature.
2349          */
2350         unset_feature(env, ARM_FEATURE_EL3);
2351 
2352         /*
2353          * Disable the security extension feature bits in the processor
2354          * feature registers as well.
2355          */
2356         cpu->isar.id_pfr1 = FIELD_DP32(cpu->isar.id_pfr1, ID_PFR1, SECURITY, 0);
2357         cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, COPSDBG, 0);
2358         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2359                                            ID_AA64PFR0, EL3, 0);
2360 
2361         /* Disable the realm management extension, which requires EL3. */
2362         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2363                                            ID_AA64PFR0, RME, 0);
2364     }
2365 
2366     if (!cpu->has_el2) {
2367         unset_feature(env, ARM_FEATURE_EL2);
2368     }
2369 
2370     if (!cpu->has_pmu) {
2371         unset_feature(env, ARM_FEATURE_PMU);
2372     }
2373     if (arm_feature(env, ARM_FEATURE_PMU)) {
2374         pmu_init(cpu);
2375 
2376         if (!kvm_enabled()) {
2377             arm_register_pre_el_change_hook(cpu, &pmu_pre_el_change, 0);
2378             arm_register_el_change_hook(cpu, &pmu_post_el_change, 0);
2379         }
2380 
2381 #ifndef CONFIG_USER_ONLY
2382         cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, arm_pmu_timer_cb,
2383                 cpu);
2384 #endif
2385     } else {
2386         cpu->isar.id_aa64dfr0 =
2387             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMUVER, 0);
2388         cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, PERFMON, 0);
2389         cpu->pmceid0 = 0;
2390         cpu->pmceid1 = 0;
2391     }
2392 
2393     if (!arm_feature(env, ARM_FEATURE_EL2)) {
2394         /*
2395          * Disable the hypervisor feature bits in the processor feature
2396          * registers if we don't have EL2.
2397          */
2398         cpu->isar.id_aa64pfr0 = FIELD_DP64(cpu->isar.id_aa64pfr0,
2399                                            ID_AA64PFR0, EL2, 0);
2400         cpu->isar.id_pfr1 = FIELD_DP32(cpu->isar.id_pfr1,
2401                                        ID_PFR1, VIRTUALIZATION, 0);
2402     }
2403 
2404     if (cpu_isar_feature(aa64_mte, cpu)) {
2405         /*
2406          * The architectural range of GM blocksize is 2-6, however qemu
2407          * doesn't support blocksize of 2 (see HELPER(ldgm)).
2408          */
2409         if (tcg_enabled()) {
2410             assert(cpu->gm_blocksize >= 3 && cpu->gm_blocksize <= 6);
2411         }
2412 
2413 #ifndef CONFIG_USER_ONLY
2414         /*
2415          * If we run with TCG and do not have tag-memory provided by
2416          * the machine, then reduce MTE support to instructions enabled at EL0.
2417          * This matches Cortex-A710 BROADCASTMTE input being LOW.
2418          */
2419         if (tcg_enabled() && cpu->tag_memory == NULL) {
2420             cpu->isar.id_aa64pfr1 =
2421                 FIELD_DP64(cpu->isar.id_aa64pfr1, ID_AA64PFR1, MTE, 1);
2422         }
2423 
2424         /*
2425          * If MTE is supported by the host, however it should not be
2426          * enabled on the guest (i.e mte=off), clear guest's MTE bits."
2427          */
2428         if (kvm_enabled() && !cpu->kvm_mte) {
2429                 FIELD_DP64(cpu->isar.id_aa64pfr1, ID_AA64PFR1, MTE, 0);
2430         }
2431 #endif
2432     }
2433 
2434 #ifndef CONFIG_USER_ONLY
2435     if (tcg_enabled() && cpu_isar_feature(aa64_wfxt, cpu)) {
2436         cpu->wfxt_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
2437                                        arm_wfxt_timer_cb, cpu);
2438     }
2439 #endif
2440 
2441     if (tcg_enabled()) {
2442         /*
2443          * Don't report some architectural features in the ID registers
2444          * where TCG does not yet implement it (not even a minimal
2445          * stub version). This avoids guests falling over when they
2446          * try to access the non-existent system registers for them.
2447          */
2448         /* FEAT_SPE (Statistical Profiling Extension) */
2449         cpu->isar.id_aa64dfr0 =
2450             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMSVER, 0);
2451         /* FEAT_TRBE (Trace Buffer Extension) */
2452         cpu->isar.id_aa64dfr0 =
2453             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEBUFFER, 0);
2454         /* FEAT_TRF (Self-hosted Trace Extension) */
2455         cpu->isar.id_aa64dfr0 =
2456             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEFILT, 0);
2457         cpu->isar.id_dfr0 =
2458             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, TRACEFILT, 0);
2459         /* Trace Macrocell system register access */
2460         cpu->isar.id_aa64dfr0 =
2461             FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, TRACEVER, 0);
2462         cpu->isar.id_dfr0 =
2463             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, COPTRC, 0);
2464         /* Memory mapped trace */
2465         cpu->isar.id_dfr0 =
2466             FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, MMAPTRC, 0);
2467         /* FEAT_AMU (Activity Monitors Extension) */
2468         cpu->isar.id_aa64pfr0 =
2469             FIELD_DP64(cpu->isar.id_aa64pfr0, ID_AA64PFR0, AMU, 0);
2470         cpu->isar.id_pfr0 =
2471             FIELD_DP32(cpu->isar.id_pfr0, ID_PFR0, AMU, 0);
2472         /* FEAT_MPAM (Memory Partitioning and Monitoring Extension) */
2473         cpu->isar.id_aa64pfr0 =
2474             FIELD_DP64(cpu->isar.id_aa64pfr0, ID_AA64PFR0, MPAM, 0);
2475     }
2476 
2477     /* MPU can be configured out of a PMSA CPU either by setting has-mpu
2478      * to false or by setting pmsav7-dregion to 0.
2479      */
2480     if (!cpu->has_mpu || cpu->pmsav7_dregion == 0) {
2481         cpu->has_mpu = false;
2482         cpu->pmsav7_dregion = 0;
2483         cpu->pmsav8r_hdregion = 0;
2484     }
2485 
2486     if (arm_feature(env, ARM_FEATURE_PMSA) &&
2487         arm_feature(env, ARM_FEATURE_V7)) {
2488         uint32_t nr = cpu->pmsav7_dregion;
2489 
2490         if (nr > 0xff) {
2491             error_setg(errp, "PMSAv7 MPU #regions invalid %" PRIu32, nr);
2492             return;
2493         }
2494 
2495         if (nr) {
2496             if (arm_feature(env, ARM_FEATURE_V8)) {
2497                 /* PMSAv8 */
2498                 env->pmsav8.rbar[M_REG_NS] = g_new0(uint32_t, nr);
2499                 env->pmsav8.rlar[M_REG_NS] = g_new0(uint32_t, nr);
2500                 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
2501                     env->pmsav8.rbar[M_REG_S] = g_new0(uint32_t, nr);
2502                     env->pmsav8.rlar[M_REG_S] = g_new0(uint32_t, nr);
2503                 }
2504             } else {
2505                 env->pmsav7.drbar = g_new0(uint32_t, nr);
2506                 env->pmsav7.drsr = g_new0(uint32_t, nr);
2507                 env->pmsav7.dracr = g_new0(uint32_t, nr);
2508             }
2509         }
2510 
2511         if (cpu->pmsav8r_hdregion > 0xff) {
2512             error_setg(errp, "PMSAv8 MPU EL2 #regions invalid %" PRIu32,
2513                               cpu->pmsav8r_hdregion);
2514             return;
2515         }
2516 
2517         if (cpu->pmsav8r_hdregion) {
2518             env->pmsav8.hprbar = g_new0(uint32_t,
2519                                         cpu->pmsav8r_hdregion);
2520             env->pmsav8.hprlar = g_new0(uint32_t,
2521                                         cpu->pmsav8r_hdregion);
2522         }
2523     }
2524 
2525     if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
2526         uint32_t nr = cpu->sau_sregion;
2527 
2528         if (nr > 0xff) {
2529             error_setg(errp, "v8M SAU #regions invalid %" PRIu32, nr);
2530             return;
2531         }
2532 
2533         if (nr) {
2534             env->sau.rbar = g_new0(uint32_t, nr);
2535             env->sau.rlar = g_new0(uint32_t, nr);
2536         }
2537     }
2538 
2539     if (arm_feature(env, ARM_FEATURE_EL3)) {
2540         set_feature(env, ARM_FEATURE_VBAR);
2541     }
2542 
2543 #ifndef CONFIG_USER_ONLY
2544     if (tcg_enabled() && cpu_isar_feature(aa64_rme, cpu)) {
2545         arm_register_el_change_hook(cpu, &gt_rme_post_el_change, 0);
2546     }
2547 #endif
2548 
2549     register_cp_regs_for_features(cpu);
2550     arm_cpu_register_gdb_regs_for_features(cpu);
2551     arm_cpu_register_gdb_commands(cpu);
2552 
2553     init_cpreg_list(cpu);
2554 
2555 #ifndef CONFIG_USER_ONLY
2556     MachineState *ms = MACHINE(qdev_get_machine());
2557     unsigned int smp_cpus = ms->smp.cpus;
2558     bool has_secure = cpu->has_el3 || arm_feature(env, ARM_FEATURE_M_SECURITY);
2559 
2560     /*
2561      * We must set cs->num_ases to the final value before
2562      * the first call to cpu_address_space_init.
2563      */
2564     if (cpu->tag_memory != NULL) {
2565         cs->num_ases = 3 + has_secure;
2566     } else {
2567         cs->num_ases = 1 + has_secure;
2568     }
2569 
2570     if (has_secure) {
2571         if (!cpu->secure_memory) {
2572             cpu->secure_memory = cs->memory;
2573         }
2574         cpu_address_space_init(cs, ARMASIdx_S, "cpu-secure-memory",
2575                                cpu->secure_memory);
2576     }
2577 
2578     if (cpu->tag_memory != NULL) {
2579         cpu_address_space_init(cs, ARMASIdx_TagNS, "cpu-tag-memory",
2580                                cpu->tag_memory);
2581         if (has_secure) {
2582             cpu_address_space_init(cs, ARMASIdx_TagS, "cpu-tag-memory",
2583                                    cpu->secure_tag_memory);
2584         }
2585     }
2586 
2587     cpu_address_space_init(cs, ARMASIdx_NS, "cpu-memory", cs->memory);
2588 
2589     /* No core_count specified, default to smp_cpus. */
2590     if (cpu->core_count == -1) {
2591         cpu->core_count = smp_cpus;
2592     }
2593 #endif
2594 
2595     if (tcg_enabled()) {
2596         int dcz_blocklen = 4 << cpu->dcz_blocksize;
2597 
2598         /*
2599          * We only support DCZ blocklen that fits on one page.
2600          *
2601          * Architectually this is always true.  However TARGET_PAGE_SIZE
2602          * is variable and, for compatibility with -machine virt-2.7,
2603          * is only 1KiB, as an artifact of legacy ARMv5 subpage support.
2604          * But even then, while the largest architectural DCZ blocklen
2605          * is 2KiB, no cpu actually uses such a large blocklen.
2606          */
2607         assert(dcz_blocklen <= TARGET_PAGE_SIZE);
2608 
2609         /*
2610          * We only support DCZ blocksize >= 2*TAG_GRANULE, which is to say
2611          * both nibbles of each byte storing tag data may be written at once.
2612          * Since TAG_GRANULE is 16, this means that blocklen must be >= 32.
2613          */
2614         if (cpu_isar_feature(aa64_mte, cpu)) {
2615             assert(dcz_blocklen >= 2 * TAG_GRANULE);
2616         }
2617     }
2618 
2619     qemu_init_vcpu(cs);
2620     cpu_reset(cs);
2621 
2622     acc->parent_realize(dev, errp);
2623 }
2624 
2625 static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
2626 {
2627     ObjectClass *oc;
2628     char *typename;
2629     char **cpuname;
2630     const char *cpunamestr;
2631 
2632     cpuname = g_strsplit(cpu_model, ",", 1);
2633     cpunamestr = cpuname[0];
2634 #ifdef CONFIG_USER_ONLY
2635     /* For backwards compatibility usermode emulation allows "-cpu any",
2636      * which has the same semantics as "-cpu max".
2637      */
2638     if (!strcmp(cpunamestr, "any")) {
2639         cpunamestr = "max";
2640     }
2641 #endif
2642     typename = g_strdup_printf(ARM_CPU_TYPE_NAME("%s"), cpunamestr);
2643     oc = object_class_by_name(typename);
2644     g_strfreev(cpuname);
2645     g_free(typename);
2646 
2647     return oc;
2648 }
2649 
2650 static const Property arm_cpu_properties[] = {
2651     DEFINE_PROP_UINT64("midr", ARMCPU, midr, 0),
2652     DEFINE_PROP_UINT64("mp-affinity", ARMCPU,
2653                         mp_affinity, ARM64_AFFINITY_INVALID),
2654     DEFINE_PROP_INT32("node-id", ARMCPU, node_id, CPU_UNSET_NUMA_NODE_ID),
2655     DEFINE_PROP_INT32("core-count", ARMCPU, core_count, -1),
2656     /* True to default to the backward-compat old CNTFRQ rather than 1Ghz */
2657     DEFINE_PROP_BOOL("backcompat-cntfrq", ARMCPU, backcompat_cntfrq, false),
2658     DEFINE_PROP_BOOL("backcompat-pauth-default-use-qarma5", ARMCPU,
2659                       backcompat_pauth_default_use_qarma5, false),
2660 };
2661 
2662 static const gchar *arm_gdb_arch_name(CPUState *cs)
2663 {
2664     ARMCPU *cpu = ARM_CPU(cs);
2665     CPUARMState *env = &cpu->env;
2666 
2667     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
2668         return "iwmmxt";
2669     }
2670     return "arm";
2671 }
2672 
2673 #ifndef CONFIG_USER_ONLY
2674 #include "hw/core/sysemu-cpu-ops.h"
2675 
2676 static const struct SysemuCPUOps arm_sysemu_ops = {
2677     .get_phys_page_attrs_debug = arm_cpu_get_phys_page_attrs_debug,
2678     .asidx_from_attrs = arm_asidx_from_attrs,
2679     .write_elf32_note = arm_cpu_write_elf32_note,
2680     .write_elf64_note = arm_cpu_write_elf64_note,
2681     .virtio_is_big_endian = arm_cpu_virtio_is_big_endian,
2682     .legacy_vmsd = &vmstate_arm_cpu,
2683 };
2684 #endif
2685 
2686 #ifdef CONFIG_TCG
2687 static const TCGCPUOps arm_tcg_ops = {
2688     .initialize = arm_translate_init,
2689     .translate_code = arm_translate_code,
2690     .synchronize_from_tb = arm_cpu_synchronize_from_tb,
2691     .debug_excp_handler = arm_debug_excp_handler,
2692     .restore_state_to_opc = arm_restore_state_to_opc,
2693 
2694 #ifdef CONFIG_USER_ONLY
2695     .record_sigsegv = arm_cpu_record_sigsegv,
2696     .record_sigbus = arm_cpu_record_sigbus,
2697 #else
2698     .tlb_fill_align = arm_cpu_tlb_fill_align,
2699     .cpu_exec_interrupt = arm_cpu_exec_interrupt,
2700     .cpu_exec_halt = arm_cpu_exec_halt,
2701     .do_interrupt = arm_cpu_do_interrupt,
2702     .do_transaction_failed = arm_cpu_do_transaction_failed,
2703     .do_unaligned_access = arm_cpu_do_unaligned_access,
2704     .adjust_watchpoint_address = arm_adjust_watchpoint_address,
2705     .debug_check_watchpoint = arm_debug_check_watchpoint,
2706     .debug_check_breakpoint = arm_debug_check_breakpoint,
2707 #endif /* !CONFIG_USER_ONLY */
2708 };
2709 #endif /* CONFIG_TCG */
2710 
2711 static void arm_cpu_class_init(ObjectClass *oc, void *data)
2712 {
2713     ARMCPUClass *acc = ARM_CPU_CLASS(oc);
2714     CPUClass *cc = CPU_CLASS(acc);
2715     DeviceClass *dc = DEVICE_CLASS(oc);
2716     ResettableClass *rc = RESETTABLE_CLASS(oc);
2717 
2718     device_class_set_parent_realize(dc, arm_cpu_realizefn,
2719                                     &acc->parent_realize);
2720 
2721     device_class_set_props(dc, arm_cpu_properties);
2722 
2723     resettable_class_set_parent_phases(rc, NULL, arm_cpu_reset_hold, NULL,
2724                                        &acc->parent_phases);
2725 
2726     cc->class_by_name = arm_cpu_class_by_name;
2727     cc->has_work = arm_cpu_has_work;
2728     cc->mmu_index = arm_cpu_mmu_index;
2729     cc->dump_state = arm_cpu_dump_state;
2730     cc->set_pc = arm_cpu_set_pc;
2731     cc->get_pc = arm_cpu_get_pc;
2732     cc->gdb_read_register = arm_cpu_gdb_read_register;
2733     cc->gdb_write_register = arm_cpu_gdb_write_register;
2734 #ifndef CONFIG_USER_ONLY
2735     cc->sysemu_ops = &arm_sysemu_ops;
2736 #endif
2737     cc->gdb_arch_name = arm_gdb_arch_name;
2738     cc->gdb_stop_before_watchpoint = true;
2739     cc->disas_set_info = arm_disas_set_info;
2740 
2741 #ifdef CONFIG_TCG
2742     cc->tcg_ops = &arm_tcg_ops;
2743 #endif /* CONFIG_TCG */
2744 }
2745 
2746 static void arm_cpu_instance_init(Object *obj)
2747 {
2748     ARMCPUClass *acc = ARM_CPU_GET_CLASS(obj);
2749 
2750     acc->info->initfn(obj);
2751     arm_cpu_post_init(obj);
2752 }
2753 
2754 static void cpu_register_class_init(ObjectClass *oc, void *data)
2755 {
2756     ARMCPUClass *acc = ARM_CPU_CLASS(oc);
2757     CPUClass *cc = CPU_CLASS(acc);
2758 
2759     acc->info = data;
2760     cc->gdb_core_xml_file = "arm-core.xml";
2761 }
2762 
2763 void arm_cpu_register(const ARMCPUInfo *info)
2764 {
2765     TypeInfo type_info = {
2766         .parent = TYPE_ARM_CPU,
2767         .instance_init = arm_cpu_instance_init,
2768         .class_init = info->class_init ?: cpu_register_class_init,
2769         .class_data = (void *)info,
2770     };
2771 
2772     type_info.name = g_strdup_printf("%s-" TYPE_ARM_CPU, info->name);
2773     type_register_static(&type_info);
2774     g_free((void *)type_info.name);
2775 }
2776 
2777 static const TypeInfo arm_cpu_type_info = {
2778     .name = TYPE_ARM_CPU,
2779     .parent = TYPE_CPU,
2780     .instance_size = sizeof(ARMCPU),
2781     .instance_align = __alignof__(ARMCPU),
2782     .instance_init = arm_cpu_initfn,
2783     .instance_finalize = arm_cpu_finalizefn,
2784     .abstract = true,
2785     .class_size = sizeof(ARMCPUClass),
2786     .class_init = arm_cpu_class_init,
2787 };
2788 
2789 static void arm_cpu_register_types(void)
2790 {
2791     type_register_static(&arm_cpu_type_info);
2792 }
2793 
2794 type_init(arm_cpu_register_types)
2795