xref: /openbmc/qemu/target/arm/cpu.c (revision d33abe82c7c9847284a23e575e1078cccab540b5)
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 "target/arm/idau.h"
23 #include "qapi/error.h"
24 #include "qapi/visitor.h"
25 #include "cpu.h"
26 #include "internals.h"
27 #include "qemu-common.h"
28 #include "exec/exec-all.h"
29 #include "hw/qdev-properties.h"
30 #if !defined(CONFIG_USER_ONLY)
31 #include "hw/loader.h"
32 #endif
33 #include "hw/arm/arm.h"
34 #include "sysemu/sysemu.h"
35 #include "sysemu/hw_accel.h"
36 #include "kvm_arm.h"
37 #include "disas/capstone.h"
38 #include "fpu/softfloat.h"
39 
40 static void arm_cpu_set_pc(CPUState *cs, vaddr value)
41 {
42     ARMCPU *cpu = ARM_CPU(cs);
43     CPUARMState *env = &cpu->env;
44 
45     if (is_a64(env)) {
46         env->pc = value;
47         env->thumb = 0;
48     } else {
49         env->regs[15] = value & ~1;
50         env->thumb = value & 1;
51     }
52 }
53 
54 static void arm_cpu_synchronize_from_tb(CPUState *cs, TranslationBlock *tb)
55 {
56     ARMCPU *cpu = ARM_CPU(cs);
57     CPUARMState *env = &cpu->env;
58 
59     /*
60      * It's OK to look at env for the current mode here, because it's
61      * never possible for an AArch64 TB to chain to an AArch32 TB.
62      */
63     if (is_a64(env)) {
64         env->pc = tb->pc;
65     } else {
66         env->regs[15] = tb->pc;
67     }
68 }
69 
70 static bool arm_cpu_has_work(CPUState *cs)
71 {
72     ARMCPU *cpu = ARM_CPU(cs);
73 
74     return (cpu->power_state != PSCI_OFF)
75         && cs->interrupt_request &
76         (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD
77          | CPU_INTERRUPT_VFIQ | CPU_INTERRUPT_VIRQ
78          | CPU_INTERRUPT_EXITTB);
79 }
80 
81 void arm_register_pre_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook,
82                                  void *opaque)
83 {
84     ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1);
85 
86     entry->hook = hook;
87     entry->opaque = opaque;
88 
89     QLIST_INSERT_HEAD(&cpu->pre_el_change_hooks, entry, node);
90 }
91 
92 void arm_register_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook,
93                                  void *opaque)
94 {
95     ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1);
96 
97     entry->hook = hook;
98     entry->opaque = opaque;
99 
100     QLIST_INSERT_HEAD(&cpu->el_change_hooks, entry, node);
101 }
102 
103 static void cp_reg_reset(gpointer key, gpointer value, gpointer opaque)
104 {
105     /* Reset a single ARMCPRegInfo register */
106     ARMCPRegInfo *ri = value;
107     ARMCPU *cpu = opaque;
108 
109     if (ri->type & (ARM_CP_SPECIAL | ARM_CP_ALIAS)) {
110         return;
111     }
112 
113     if (ri->resetfn) {
114         ri->resetfn(&cpu->env, ri);
115         return;
116     }
117 
118     /* A zero offset is never possible as it would be regs[0]
119      * so we use it to indicate that reset is being handled elsewhere.
120      * This is basically only used for fields in non-core coprocessors
121      * (like the pxa2xx ones).
122      */
123     if (!ri->fieldoffset) {
124         return;
125     }
126 
127     if (cpreg_field_is_64bit(ri)) {
128         CPREG_FIELD64(&cpu->env, ri) = ri->resetvalue;
129     } else {
130         CPREG_FIELD32(&cpu->env, ri) = ri->resetvalue;
131     }
132 }
133 
134 static void cp_reg_check_reset(gpointer key, gpointer value,  gpointer opaque)
135 {
136     /* Purely an assertion check: we've already done reset once,
137      * so now check that running the reset for the cpreg doesn't
138      * change its value. This traps bugs where two different cpregs
139      * both try to reset the same state field but to different values.
140      */
141     ARMCPRegInfo *ri = value;
142     ARMCPU *cpu = opaque;
143     uint64_t oldvalue, newvalue;
144 
145     if (ri->type & (ARM_CP_SPECIAL | ARM_CP_ALIAS | ARM_CP_NO_RAW)) {
146         return;
147     }
148 
149     oldvalue = read_raw_cp_reg(&cpu->env, ri);
150     cp_reg_reset(key, value, opaque);
151     newvalue = read_raw_cp_reg(&cpu->env, ri);
152     assert(oldvalue == newvalue);
153 }
154 
155 /* CPUClass::reset() */
156 static void arm_cpu_reset(CPUState *s)
157 {
158     ARMCPU *cpu = ARM_CPU(s);
159     ARMCPUClass *acc = ARM_CPU_GET_CLASS(cpu);
160     CPUARMState *env = &cpu->env;
161 
162     acc->parent_reset(s);
163 
164     memset(env, 0, offsetof(CPUARMState, end_reset_fields));
165 
166     g_hash_table_foreach(cpu->cp_regs, cp_reg_reset, cpu);
167     g_hash_table_foreach(cpu->cp_regs, cp_reg_check_reset, cpu);
168 
169     env->vfp.xregs[ARM_VFP_FPSID] = cpu->reset_fpsid;
170     env->vfp.xregs[ARM_VFP_MVFR0] = cpu->isar.mvfr0;
171     env->vfp.xregs[ARM_VFP_MVFR1] = cpu->isar.mvfr1;
172     env->vfp.xregs[ARM_VFP_MVFR2] = cpu->isar.mvfr2;
173 
174     cpu->power_state = cpu->start_powered_off ? PSCI_OFF : PSCI_ON;
175     s->halted = cpu->start_powered_off;
176 
177     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
178         env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
179     }
180 
181     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
182         /* 64 bit CPUs always start in 64 bit mode */
183         env->aarch64 = 1;
184 #if defined(CONFIG_USER_ONLY)
185         env->pstate = PSTATE_MODE_EL0t;
186         /* Userspace expects access to DC ZVA, CTL_EL0 and the cache ops */
187         env->cp15.sctlr_el[1] |= SCTLR_UCT | SCTLR_UCI | SCTLR_DZE;
188         /* Enable all PAC keys.  */
189         env->cp15.sctlr_el[1] |= (SCTLR_EnIA | SCTLR_EnIB |
190                                   SCTLR_EnDA | SCTLR_EnDB);
191         /* Enable all PAC instructions */
192         env->cp15.hcr_el2 |= HCR_API;
193         env->cp15.scr_el3 |= SCR_API;
194         /* and to the FP/Neon instructions */
195         env->cp15.cpacr_el1 = deposit64(env->cp15.cpacr_el1, 20, 2, 3);
196         /* and to the SVE instructions */
197         env->cp15.cpacr_el1 = deposit64(env->cp15.cpacr_el1, 16, 2, 3);
198         env->cp15.cptr_el[3] |= CPTR_EZ;
199         /* with maximum vector length */
200         env->vfp.zcr_el[1] = cpu->sve_max_vq - 1;
201         env->vfp.zcr_el[2] = env->vfp.zcr_el[1];
202         env->vfp.zcr_el[3] = env->vfp.zcr_el[1];
203         /*
204          * Enable TBI0 and TBI1.  While the real kernel only enables TBI0,
205          * turning on both here will produce smaller code and otherwise
206          * make no difference to the user-level emulation.
207          */
208         env->cp15.tcr_el[1].raw_tcr = (3ULL << 37);
209 #else
210         /* Reset into the highest available EL */
211         if (arm_feature(env, ARM_FEATURE_EL3)) {
212             env->pstate = PSTATE_MODE_EL3h;
213         } else if (arm_feature(env, ARM_FEATURE_EL2)) {
214             env->pstate = PSTATE_MODE_EL2h;
215         } else {
216             env->pstate = PSTATE_MODE_EL1h;
217         }
218         env->pc = cpu->rvbar;
219 #endif
220     } else {
221 #if defined(CONFIG_USER_ONLY)
222         /* Userspace expects access to cp10 and cp11 for FP/Neon */
223         env->cp15.cpacr_el1 = deposit64(env->cp15.cpacr_el1, 20, 4, 0xf);
224 #endif
225     }
226 
227 #if defined(CONFIG_USER_ONLY)
228     env->uncached_cpsr = ARM_CPU_MODE_USR;
229     /* For user mode we must enable access to coprocessors */
230     env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
231     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
232         env->cp15.c15_cpar = 3;
233     } else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
234         env->cp15.c15_cpar = 1;
235     }
236 #else
237 
238     /*
239      * If the highest available EL is EL2, AArch32 will start in Hyp
240      * mode; otherwise it starts in SVC. Note that if we start in
241      * AArch64 then these values in the uncached_cpsr will be ignored.
242      */
243     if (arm_feature(env, ARM_FEATURE_EL2) &&
244         !arm_feature(env, ARM_FEATURE_EL3)) {
245         env->uncached_cpsr = ARM_CPU_MODE_HYP;
246     } else {
247         env->uncached_cpsr = ARM_CPU_MODE_SVC;
248     }
249     env->daif = PSTATE_D | PSTATE_A | PSTATE_I | PSTATE_F;
250 
251     if (arm_feature(env, ARM_FEATURE_M)) {
252         uint32_t initial_msp; /* Loaded from 0x0 */
253         uint32_t initial_pc; /* Loaded from 0x4 */
254         uint8_t *rom;
255         uint32_t vecbase;
256 
257         if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
258             env->v7m.secure = true;
259         } else {
260             /* This bit resets to 0 if security is supported, but 1 if
261              * it is not. The bit is not present in v7M, but we set it
262              * here so we can avoid having to make checks on it conditional
263              * on ARM_FEATURE_V8 (we don't let the guest see the bit).
264              */
265             env->v7m.aircr = R_V7M_AIRCR_BFHFNMINS_MASK;
266         }
267 
268         /* In v7M the reset value of this bit is IMPDEF, but ARM recommends
269          * that it resets to 1, so QEMU always does that rather than making
270          * it dependent on CPU model. In v8M it is RES1.
271          */
272         env->v7m.ccr[M_REG_NS] = R_V7M_CCR_STKALIGN_MASK;
273         env->v7m.ccr[M_REG_S] = R_V7M_CCR_STKALIGN_MASK;
274         if (arm_feature(env, ARM_FEATURE_V8)) {
275             /* in v8M the NONBASETHRDENA bit [0] is RES1 */
276             env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_NONBASETHRDENA_MASK;
277             env->v7m.ccr[M_REG_S] |= R_V7M_CCR_NONBASETHRDENA_MASK;
278         }
279         if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
280             env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_UNALIGN_TRP_MASK;
281             env->v7m.ccr[M_REG_S] |= R_V7M_CCR_UNALIGN_TRP_MASK;
282         }
283 
284         if (arm_feature(env, ARM_FEATURE_VFP)) {
285             env->v7m.fpccr[M_REG_NS] = R_V7M_FPCCR_ASPEN_MASK;
286             env->v7m.fpccr[M_REG_S] = R_V7M_FPCCR_ASPEN_MASK |
287                 R_V7M_FPCCR_LSPEN_MASK | R_V7M_FPCCR_S_MASK;
288         }
289         /* Unlike A/R profile, M profile defines the reset LR value */
290         env->regs[14] = 0xffffffff;
291 
292         env->v7m.vecbase[M_REG_S] = cpu->init_svtor & 0xffffff80;
293 
294         /* Load the initial SP and PC from offset 0 and 4 in the vector table */
295         vecbase = env->v7m.vecbase[env->v7m.secure];
296         rom = rom_ptr(vecbase, 8);
297         if (rom) {
298             /* Address zero is covered by ROM which hasn't yet been
299              * copied into physical memory.
300              */
301             initial_msp = ldl_p(rom);
302             initial_pc = ldl_p(rom + 4);
303         } else {
304             /* Address zero not covered by a ROM blob, or the ROM blob
305              * is in non-modifiable memory and this is a second reset after
306              * it got copied into memory. In the latter case, rom_ptr
307              * will return a NULL pointer and we should use ldl_phys instead.
308              */
309             initial_msp = ldl_phys(s->as, vecbase);
310             initial_pc = ldl_phys(s->as, vecbase + 4);
311         }
312 
313         env->regs[13] = initial_msp & 0xFFFFFFFC;
314         env->regs[15] = initial_pc & ~1;
315         env->thumb = initial_pc & 1;
316     }
317 
318     /* AArch32 has a hard highvec setting of 0xFFFF0000.  If we are currently
319      * executing as AArch32 then check if highvecs are enabled and
320      * adjust the PC accordingly.
321      */
322     if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
323         env->regs[15] = 0xFFFF0000;
324     }
325 
326     /* M profile requires that reset clears the exclusive monitor;
327      * A profile does not, but clearing it makes more sense than having it
328      * set with an exclusive access on address zero.
329      */
330     arm_clear_exclusive(env);
331 
332     env->vfp.xregs[ARM_VFP_FPEXC] = 0;
333 #endif
334 
335     if (arm_feature(env, ARM_FEATURE_PMSA)) {
336         if (cpu->pmsav7_dregion > 0) {
337             if (arm_feature(env, ARM_FEATURE_V8)) {
338                 memset(env->pmsav8.rbar[M_REG_NS], 0,
339                        sizeof(*env->pmsav8.rbar[M_REG_NS])
340                        * cpu->pmsav7_dregion);
341                 memset(env->pmsav8.rlar[M_REG_NS], 0,
342                        sizeof(*env->pmsav8.rlar[M_REG_NS])
343                        * cpu->pmsav7_dregion);
344                 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
345                     memset(env->pmsav8.rbar[M_REG_S], 0,
346                            sizeof(*env->pmsav8.rbar[M_REG_S])
347                            * cpu->pmsav7_dregion);
348                     memset(env->pmsav8.rlar[M_REG_S], 0,
349                            sizeof(*env->pmsav8.rlar[M_REG_S])
350                            * cpu->pmsav7_dregion);
351                 }
352             } else if (arm_feature(env, ARM_FEATURE_V7)) {
353                 memset(env->pmsav7.drbar, 0,
354                        sizeof(*env->pmsav7.drbar) * cpu->pmsav7_dregion);
355                 memset(env->pmsav7.drsr, 0,
356                        sizeof(*env->pmsav7.drsr) * cpu->pmsav7_dregion);
357                 memset(env->pmsav7.dracr, 0,
358                        sizeof(*env->pmsav7.dracr) * cpu->pmsav7_dregion);
359             }
360         }
361         env->pmsav7.rnr[M_REG_NS] = 0;
362         env->pmsav7.rnr[M_REG_S] = 0;
363         env->pmsav8.mair0[M_REG_NS] = 0;
364         env->pmsav8.mair0[M_REG_S] = 0;
365         env->pmsav8.mair1[M_REG_NS] = 0;
366         env->pmsav8.mair1[M_REG_S] = 0;
367     }
368 
369     if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
370         if (cpu->sau_sregion > 0) {
371             memset(env->sau.rbar, 0, sizeof(*env->sau.rbar) * cpu->sau_sregion);
372             memset(env->sau.rlar, 0, sizeof(*env->sau.rlar) * cpu->sau_sregion);
373         }
374         env->sau.rnr = 0;
375         /* SAU_CTRL reset value is IMPDEF; we choose 0, which is what
376          * the Cortex-M33 does.
377          */
378         env->sau.ctrl = 0;
379     }
380 
381     set_flush_to_zero(1, &env->vfp.standard_fp_status);
382     set_flush_inputs_to_zero(1, &env->vfp.standard_fp_status);
383     set_default_nan_mode(1, &env->vfp.standard_fp_status);
384     set_float_detect_tininess(float_tininess_before_rounding,
385                               &env->vfp.fp_status);
386     set_float_detect_tininess(float_tininess_before_rounding,
387                               &env->vfp.standard_fp_status);
388     set_float_detect_tininess(float_tininess_before_rounding,
389                               &env->vfp.fp_status_f16);
390 #ifndef CONFIG_USER_ONLY
391     if (kvm_enabled()) {
392         kvm_arm_reset_vcpu(cpu);
393     }
394 #endif
395 
396     hw_breakpoint_update_all(cpu);
397     hw_watchpoint_update_all(cpu);
398 }
399 
400 bool arm_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
401 {
402     CPUClass *cc = CPU_GET_CLASS(cs);
403     CPUARMState *env = cs->env_ptr;
404     uint32_t cur_el = arm_current_el(env);
405     bool secure = arm_is_secure(env);
406     uint32_t target_el;
407     uint32_t excp_idx;
408     bool ret = false;
409 
410     if (interrupt_request & CPU_INTERRUPT_FIQ) {
411         excp_idx = EXCP_FIQ;
412         target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
413         if (arm_excp_unmasked(cs, excp_idx, target_el)) {
414             cs->exception_index = excp_idx;
415             env->exception.target_el = target_el;
416             cc->do_interrupt(cs);
417             ret = true;
418         }
419     }
420     if (interrupt_request & CPU_INTERRUPT_HARD) {
421         excp_idx = EXCP_IRQ;
422         target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
423         if (arm_excp_unmasked(cs, excp_idx, target_el)) {
424             cs->exception_index = excp_idx;
425             env->exception.target_el = target_el;
426             cc->do_interrupt(cs);
427             ret = true;
428         }
429     }
430     if (interrupt_request & CPU_INTERRUPT_VIRQ) {
431         excp_idx = EXCP_VIRQ;
432         target_el = 1;
433         if (arm_excp_unmasked(cs, excp_idx, target_el)) {
434             cs->exception_index = excp_idx;
435             env->exception.target_el = target_el;
436             cc->do_interrupt(cs);
437             ret = true;
438         }
439     }
440     if (interrupt_request & CPU_INTERRUPT_VFIQ) {
441         excp_idx = EXCP_VFIQ;
442         target_el = 1;
443         if (arm_excp_unmasked(cs, excp_idx, target_el)) {
444             cs->exception_index = excp_idx;
445             env->exception.target_el = target_el;
446             cc->do_interrupt(cs);
447             ret = true;
448         }
449     }
450 
451     return ret;
452 }
453 
454 #if !defined(CONFIG_USER_ONLY) || !defined(TARGET_AARCH64)
455 static bool arm_v7m_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
456 {
457     CPUClass *cc = CPU_GET_CLASS(cs);
458     ARMCPU *cpu = ARM_CPU(cs);
459     CPUARMState *env = &cpu->env;
460     bool ret = false;
461 
462     /* ARMv7-M interrupt masking works differently than -A or -R.
463      * There is no FIQ/IRQ distinction. Instead of I and F bits
464      * masking FIQ and IRQ interrupts, an exception is taken only
465      * if it is higher priority than the current execution priority
466      * (which depends on state like BASEPRI, FAULTMASK and the
467      * currently active exception).
468      */
469     if (interrupt_request & CPU_INTERRUPT_HARD
470         && (armv7m_nvic_can_take_pending_exception(env->nvic))) {
471         cs->exception_index = EXCP_IRQ;
472         cc->do_interrupt(cs);
473         ret = true;
474     }
475     return ret;
476 }
477 #endif
478 
479 void arm_cpu_update_virq(ARMCPU *cpu)
480 {
481     /*
482      * Update the interrupt level for VIRQ, which is the logical OR of
483      * the HCR_EL2.VI bit and the input line level from the GIC.
484      */
485     CPUARMState *env = &cpu->env;
486     CPUState *cs = CPU(cpu);
487 
488     bool new_state = (env->cp15.hcr_el2 & HCR_VI) ||
489         (env->irq_line_state & CPU_INTERRUPT_VIRQ);
490 
491     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VIRQ) != 0)) {
492         if (new_state) {
493             cpu_interrupt(cs, CPU_INTERRUPT_VIRQ);
494         } else {
495             cpu_reset_interrupt(cs, CPU_INTERRUPT_VIRQ);
496         }
497     }
498 }
499 
500 void arm_cpu_update_vfiq(ARMCPU *cpu)
501 {
502     /*
503      * Update the interrupt level for VFIQ, which is the logical OR of
504      * the HCR_EL2.VF bit and the input line level from the GIC.
505      */
506     CPUARMState *env = &cpu->env;
507     CPUState *cs = CPU(cpu);
508 
509     bool new_state = (env->cp15.hcr_el2 & HCR_VF) ||
510         (env->irq_line_state & CPU_INTERRUPT_VFIQ);
511 
512     if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VFIQ) != 0)) {
513         if (new_state) {
514             cpu_interrupt(cs, CPU_INTERRUPT_VFIQ);
515         } else {
516             cpu_reset_interrupt(cs, CPU_INTERRUPT_VFIQ);
517         }
518     }
519 }
520 
521 #ifndef CONFIG_USER_ONLY
522 static void arm_cpu_set_irq(void *opaque, int irq, int level)
523 {
524     ARMCPU *cpu = opaque;
525     CPUARMState *env = &cpu->env;
526     CPUState *cs = CPU(cpu);
527     static const int mask[] = {
528         [ARM_CPU_IRQ] = CPU_INTERRUPT_HARD,
529         [ARM_CPU_FIQ] = CPU_INTERRUPT_FIQ,
530         [ARM_CPU_VIRQ] = CPU_INTERRUPT_VIRQ,
531         [ARM_CPU_VFIQ] = CPU_INTERRUPT_VFIQ
532     };
533 
534     if (level) {
535         env->irq_line_state |= mask[irq];
536     } else {
537         env->irq_line_state &= ~mask[irq];
538     }
539 
540     switch (irq) {
541     case ARM_CPU_VIRQ:
542         assert(arm_feature(env, ARM_FEATURE_EL2));
543         arm_cpu_update_virq(cpu);
544         break;
545     case ARM_CPU_VFIQ:
546         assert(arm_feature(env, ARM_FEATURE_EL2));
547         arm_cpu_update_vfiq(cpu);
548         break;
549     case ARM_CPU_IRQ:
550     case ARM_CPU_FIQ:
551         if (level) {
552             cpu_interrupt(cs, mask[irq]);
553         } else {
554             cpu_reset_interrupt(cs, mask[irq]);
555         }
556         break;
557     default:
558         g_assert_not_reached();
559     }
560 }
561 
562 static void arm_cpu_kvm_set_irq(void *opaque, int irq, int level)
563 {
564 #ifdef CONFIG_KVM
565     ARMCPU *cpu = opaque;
566     CPUARMState *env = &cpu->env;
567     CPUState *cs = CPU(cpu);
568     int kvm_irq = KVM_ARM_IRQ_TYPE_CPU << KVM_ARM_IRQ_TYPE_SHIFT;
569     uint32_t linestate_bit;
570 
571     switch (irq) {
572     case ARM_CPU_IRQ:
573         kvm_irq |= KVM_ARM_IRQ_CPU_IRQ;
574         linestate_bit = CPU_INTERRUPT_HARD;
575         break;
576     case ARM_CPU_FIQ:
577         kvm_irq |= KVM_ARM_IRQ_CPU_FIQ;
578         linestate_bit = CPU_INTERRUPT_FIQ;
579         break;
580     default:
581         g_assert_not_reached();
582     }
583 
584     if (level) {
585         env->irq_line_state |= linestate_bit;
586     } else {
587         env->irq_line_state &= ~linestate_bit;
588     }
589 
590     kvm_irq |= cs->cpu_index << KVM_ARM_IRQ_VCPU_SHIFT;
591     kvm_set_irq(kvm_state, kvm_irq, level ? 1 : 0);
592 #endif
593 }
594 
595 static bool arm_cpu_virtio_is_big_endian(CPUState *cs)
596 {
597     ARMCPU *cpu = ARM_CPU(cs);
598     CPUARMState *env = &cpu->env;
599 
600     cpu_synchronize_state(cs);
601     return arm_cpu_data_is_big_endian(env);
602 }
603 
604 #endif
605 
606 static inline void set_feature(CPUARMState *env, int feature)
607 {
608     env->features |= 1ULL << feature;
609 }
610 
611 static inline void unset_feature(CPUARMState *env, int feature)
612 {
613     env->features &= ~(1ULL << feature);
614 }
615 
616 static int
617 print_insn_thumb1(bfd_vma pc, disassemble_info *info)
618 {
619   return print_insn_arm(pc | 1, info);
620 }
621 
622 static void arm_disas_set_info(CPUState *cpu, disassemble_info *info)
623 {
624     ARMCPU *ac = ARM_CPU(cpu);
625     CPUARMState *env = &ac->env;
626     bool sctlr_b;
627 
628     if (is_a64(env)) {
629         /* We might not be compiled with the A64 disassembler
630          * because it needs a C++ compiler. Leave print_insn
631          * unset in this case to use the caller default behaviour.
632          */
633 #if defined(CONFIG_ARM_A64_DIS)
634         info->print_insn = print_insn_arm_a64;
635 #endif
636         info->cap_arch = CS_ARCH_ARM64;
637         info->cap_insn_unit = 4;
638         info->cap_insn_split = 4;
639     } else {
640         int cap_mode;
641         if (env->thumb) {
642             info->print_insn = print_insn_thumb1;
643             info->cap_insn_unit = 2;
644             info->cap_insn_split = 4;
645             cap_mode = CS_MODE_THUMB;
646         } else {
647             info->print_insn = print_insn_arm;
648             info->cap_insn_unit = 4;
649             info->cap_insn_split = 4;
650             cap_mode = CS_MODE_ARM;
651         }
652         if (arm_feature(env, ARM_FEATURE_V8)) {
653             cap_mode |= CS_MODE_V8;
654         }
655         if (arm_feature(env, ARM_FEATURE_M)) {
656             cap_mode |= CS_MODE_MCLASS;
657         }
658         info->cap_arch = CS_ARCH_ARM;
659         info->cap_mode = cap_mode;
660     }
661 
662     sctlr_b = arm_sctlr_b(env);
663     if (bswap_code(sctlr_b)) {
664 #ifdef TARGET_WORDS_BIGENDIAN
665         info->endian = BFD_ENDIAN_LITTLE;
666 #else
667         info->endian = BFD_ENDIAN_BIG;
668 #endif
669     }
670     info->flags &= ~INSN_ARM_BE32;
671 #ifndef CONFIG_USER_ONLY
672     if (sctlr_b) {
673         info->flags |= INSN_ARM_BE32;
674     }
675 #endif
676 }
677 
678 uint64_t arm_cpu_mp_affinity(int idx, uint8_t clustersz)
679 {
680     uint32_t Aff1 = idx / clustersz;
681     uint32_t Aff0 = idx % clustersz;
682     return (Aff1 << ARM_AFF1_SHIFT) | Aff0;
683 }
684 
685 static void cpreg_hashtable_data_destroy(gpointer data)
686 {
687     /*
688      * Destroy function for cpu->cp_regs hashtable data entries.
689      * We must free the name string because it was g_strdup()ed in
690      * add_cpreg_to_hashtable(). It's OK to cast away the 'const'
691      * from r->name because we know we definitely allocated it.
692      */
693     ARMCPRegInfo *r = data;
694 
695     g_free((void *)r->name);
696     g_free(r);
697 }
698 
699 static void arm_cpu_initfn(Object *obj)
700 {
701     CPUState *cs = CPU(obj);
702     ARMCPU *cpu = ARM_CPU(obj);
703 
704     cs->env_ptr = &cpu->env;
705     cpu->cp_regs = g_hash_table_new_full(g_int_hash, g_int_equal,
706                                          g_free, cpreg_hashtable_data_destroy);
707 
708     QLIST_INIT(&cpu->pre_el_change_hooks);
709     QLIST_INIT(&cpu->el_change_hooks);
710 
711 #ifndef CONFIG_USER_ONLY
712     /* Our inbound IRQ and FIQ lines */
713     if (kvm_enabled()) {
714         /* VIRQ and VFIQ are unused with KVM but we add them to maintain
715          * the same interface as non-KVM CPUs.
716          */
717         qdev_init_gpio_in(DEVICE(cpu), arm_cpu_kvm_set_irq, 4);
718     } else {
719         qdev_init_gpio_in(DEVICE(cpu), arm_cpu_set_irq, 4);
720     }
721 
722     qdev_init_gpio_out(DEVICE(cpu), cpu->gt_timer_outputs,
723                        ARRAY_SIZE(cpu->gt_timer_outputs));
724 
725     qdev_init_gpio_out_named(DEVICE(cpu), &cpu->gicv3_maintenance_interrupt,
726                              "gicv3-maintenance-interrupt", 1);
727     qdev_init_gpio_out_named(DEVICE(cpu), &cpu->pmu_interrupt,
728                              "pmu-interrupt", 1);
729 #endif
730 
731     /* DTB consumers generally don't in fact care what the 'compatible'
732      * string is, so always provide some string and trust that a hypothetical
733      * picky DTB consumer will also provide a helpful error message.
734      */
735     cpu->dtb_compatible = "qemu,unknown";
736     cpu->psci_version = 1; /* By default assume PSCI v0.1 */
737     cpu->kvm_target = QEMU_KVM_ARM_TARGET_NONE;
738 
739     if (tcg_enabled()) {
740         cpu->psci_version = 2; /* TCG implements PSCI 0.2 */
741     }
742 }
743 
744 static Property arm_cpu_reset_cbar_property =
745             DEFINE_PROP_UINT64("reset-cbar", ARMCPU, reset_cbar, 0);
746 
747 static Property arm_cpu_reset_hivecs_property =
748             DEFINE_PROP_BOOL("reset-hivecs", ARMCPU, reset_hivecs, false);
749 
750 static Property arm_cpu_rvbar_property =
751             DEFINE_PROP_UINT64("rvbar", ARMCPU, rvbar, 0);
752 
753 static Property arm_cpu_has_el2_property =
754             DEFINE_PROP_BOOL("has_el2", ARMCPU, has_el2, true);
755 
756 static Property arm_cpu_has_el3_property =
757             DEFINE_PROP_BOOL("has_el3", ARMCPU, has_el3, true);
758 
759 static Property arm_cpu_cfgend_property =
760             DEFINE_PROP_BOOL("cfgend", ARMCPU, cfgend, false);
761 
762 /* use property name "pmu" to match other archs and virt tools */
763 static Property arm_cpu_has_pmu_property =
764             DEFINE_PROP_BOOL("pmu", ARMCPU, has_pmu, true);
765 
766 static Property arm_cpu_has_mpu_property =
767             DEFINE_PROP_BOOL("has-mpu", ARMCPU, has_mpu, true);
768 
769 /* This is like DEFINE_PROP_UINT32 but it doesn't set the default value,
770  * because the CPU initfn will have already set cpu->pmsav7_dregion to
771  * the right value for that particular CPU type, and we don't want
772  * to override that with an incorrect constant value.
773  */
774 static Property arm_cpu_pmsav7_dregion_property =
775             DEFINE_PROP_UNSIGNED_NODEFAULT("pmsav7-dregion", ARMCPU,
776                                            pmsav7_dregion,
777                                            qdev_prop_uint32, uint32_t);
778 
779 static void arm_get_init_svtor(Object *obj, Visitor *v, const char *name,
780                                void *opaque, Error **errp)
781 {
782     ARMCPU *cpu = ARM_CPU(obj);
783 
784     visit_type_uint32(v, name, &cpu->init_svtor, errp);
785 }
786 
787 static void arm_set_init_svtor(Object *obj, Visitor *v, const char *name,
788                                void *opaque, Error **errp)
789 {
790     ARMCPU *cpu = ARM_CPU(obj);
791 
792     visit_type_uint32(v, name, &cpu->init_svtor, errp);
793 }
794 
795 void arm_cpu_post_init(Object *obj)
796 {
797     ARMCPU *cpu = ARM_CPU(obj);
798 
799     /* M profile implies PMSA. We have to do this here rather than
800      * in realize with the other feature-implication checks because
801      * we look at the PMSA bit to see if we should add some properties.
802      */
803     if (arm_feature(&cpu->env, ARM_FEATURE_M)) {
804         set_feature(&cpu->env, ARM_FEATURE_PMSA);
805     }
806 
807     if (arm_feature(&cpu->env, ARM_FEATURE_CBAR) ||
808         arm_feature(&cpu->env, ARM_FEATURE_CBAR_RO)) {
809         qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_cbar_property,
810                                  &error_abort);
811     }
812 
813     if (!arm_feature(&cpu->env, ARM_FEATURE_M)) {
814         qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_hivecs_property,
815                                  &error_abort);
816     }
817 
818     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
819         qdev_property_add_static(DEVICE(obj), &arm_cpu_rvbar_property,
820                                  &error_abort);
821     }
822 
823     if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
824         /* Add the has_el3 state CPU property only if EL3 is allowed.  This will
825          * prevent "has_el3" from existing on CPUs which cannot support EL3.
826          */
827         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el3_property,
828                                  &error_abort);
829 
830 #ifndef CONFIG_USER_ONLY
831         object_property_add_link(obj, "secure-memory",
832                                  TYPE_MEMORY_REGION,
833                                  (Object **)&cpu->secure_memory,
834                                  qdev_prop_allow_set_link_before_realize,
835                                  OBJ_PROP_LINK_STRONG,
836                                  &error_abort);
837 #endif
838     }
839 
840     if (arm_feature(&cpu->env, ARM_FEATURE_EL2)) {
841         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el2_property,
842                                  &error_abort);
843     }
844 
845     if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) {
846         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_pmu_property,
847                                  &error_abort);
848     }
849 
850     if (arm_feature(&cpu->env, ARM_FEATURE_PMSA)) {
851         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_mpu_property,
852                                  &error_abort);
853         if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
854             qdev_property_add_static(DEVICE(obj),
855                                      &arm_cpu_pmsav7_dregion_property,
856                                      &error_abort);
857         }
858     }
859 
860     if (arm_feature(&cpu->env, ARM_FEATURE_M_SECURITY)) {
861         object_property_add_link(obj, "idau", TYPE_IDAU_INTERFACE, &cpu->idau,
862                                  qdev_prop_allow_set_link_before_realize,
863                                  OBJ_PROP_LINK_STRONG,
864                                  &error_abort);
865         /*
866          * M profile: initial value of the Secure VTOR. We can't just use
867          * a simple DEFINE_PROP_UINT32 for this because we want to permit
868          * the property to be set after realize.
869          */
870         object_property_add(obj, "init-svtor", "uint32",
871                             arm_get_init_svtor, arm_set_init_svtor,
872                             NULL, NULL, &error_abort);
873     }
874 
875     qdev_property_add_static(DEVICE(obj), &arm_cpu_cfgend_property,
876                              &error_abort);
877 }
878 
879 static void arm_cpu_finalizefn(Object *obj)
880 {
881     ARMCPU *cpu = ARM_CPU(obj);
882     ARMELChangeHook *hook, *next;
883 
884     g_hash_table_destroy(cpu->cp_regs);
885 
886     QLIST_FOREACH_SAFE(hook, &cpu->pre_el_change_hooks, node, next) {
887         QLIST_REMOVE(hook, node);
888         g_free(hook);
889     }
890     QLIST_FOREACH_SAFE(hook, &cpu->el_change_hooks, node, next) {
891         QLIST_REMOVE(hook, node);
892         g_free(hook);
893     }
894 #ifndef CONFIG_USER_ONLY
895     if (cpu->pmu_timer) {
896         timer_del(cpu->pmu_timer);
897         timer_deinit(cpu->pmu_timer);
898         timer_free(cpu->pmu_timer);
899     }
900 #endif
901 }
902 
903 static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
904 {
905     CPUState *cs = CPU(dev);
906     ARMCPU *cpu = ARM_CPU(dev);
907     ARMCPUClass *acc = ARM_CPU_GET_CLASS(dev);
908     CPUARMState *env = &cpu->env;
909     int pagebits;
910     Error *local_err = NULL;
911     bool no_aa32 = false;
912 
913     /* If we needed to query the host kernel for the CPU features
914      * then it's possible that might have failed in the initfn, but
915      * this is the first point where we can report it.
916      */
917     if (cpu->host_cpu_probe_failed) {
918         if (!kvm_enabled()) {
919             error_setg(errp, "The 'host' CPU type can only be used with KVM");
920         } else {
921             error_setg(errp, "Failed to retrieve host CPU features");
922         }
923         return;
924     }
925 
926 #ifndef CONFIG_USER_ONLY
927     /* The NVIC and M-profile CPU are two halves of a single piece of
928      * hardware; trying to use one without the other is a command line
929      * error and will result in segfaults if not caught here.
930      */
931     if (arm_feature(env, ARM_FEATURE_M)) {
932         if (!env->nvic) {
933             error_setg(errp, "This board cannot be used with Cortex-M CPUs");
934             return;
935         }
936     } else {
937         if (env->nvic) {
938             error_setg(errp, "This board can only be used with Cortex-M CPUs");
939             return;
940         }
941     }
942 
943     cpu->gt_timer[GTIMER_PHYS] = timer_new(QEMU_CLOCK_VIRTUAL, GTIMER_SCALE,
944                                            arm_gt_ptimer_cb, cpu);
945     cpu->gt_timer[GTIMER_VIRT] = timer_new(QEMU_CLOCK_VIRTUAL, GTIMER_SCALE,
946                                            arm_gt_vtimer_cb, cpu);
947     cpu->gt_timer[GTIMER_HYP] = timer_new(QEMU_CLOCK_VIRTUAL, GTIMER_SCALE,
948                                           arm_gt_htimer_cb, cpu);
949     cpu->gt_timer[GTIMER_SEC] = timer_new(QEMU_CLOCK_VIRTUAL, GTIMER_SCALE,
950                                           arm_gt_stimer_cb, cpu);
951 #endif
952 
953     cpu_exec_realizefn(cs, &local_err);
954     if (local_err != NULL) {
955         error_propagate(errp, local_err);
956         return;
957     }
958 
959     /* Some features automatically imply others: */
960     if (arm_feature(env, ARM_FEATURE_V8)) {
961         if (arm_feature(env, ARM_FEATURE_M)) {
962             set_feature(env, ARM_FEATURE_V7);
963         } else {
964             set_feature(env, ARM_FEATURE_V7VE);
965         }
966     }
967 
968     /*
969      * There exist AArch64 cpus without AArch32 support.  When KVM
970      * queries ID_ISAR0_EL1 on such a host, the value is UNKNOWN.
971      * Similarly, we cannot check ID_AA64PFR0 without AArch64 support.
972      */
973     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
974         no_aa32 = !cpu_isar_feature(aa64_aa32, cpu);
975     }
976 
977     if (arm_feature(env, ARM_FEATURE_V7VE)) {
978         /* v7 Virtualization Extensions. In real hardware this implies
979          * EL2 and also the presence of the Security Extensions.
980          * For QEMU, for backwards-compatibility we implement some
981          * CPUs or CPU configs which have no actual EL2 or EL3 but do
982          * include the various other features that V7VE implies.
983          * Presence of EL2 itself is ARM_FEATURE_EL2, and of the
984          * Security Extensions is ARM_FEATURE_EL3.
985          */
986         assert(no_aa32 || cpu_isar_feature(arm_div, cpu));
987         set_feature(env, ARM_FEATURE_LPAE);
988         set_feature(env, ARM_FEATURE_V7);
989     }
990     if (arm_feature(env, ARM_FEATURE_V7)) {
991         set_feature(env, ARM_FEATURE_VAPA);
992         set_feature(env, ARM_FEATURE_THUMB2);
993         set_feature(env, ARM_FEATURE_MPIDR);
994         if (!arm_feature(env, ARM_FEATURE_M)) {
995             set_feature(env, ARM_FEATURE_V6K);
996         } else {
997             set_feature(env, ARM_FEATURE_V6);
998         }
999 
1000         /* Always define VBAR for V7 CPUs even if it doesn't exist in
1001          * non-EL3 configs. This is needed by some legacy boards.
1002          */
1003         set_feature(env, ARM_FEATURE_VBAR);
1004     }
1005     if (arm_feature(env, ARM_FEATURE_V6K)) {
1006         set_feature(env, ARM_FEATURE_V6);
1007         set_feature(env, ARM_FEATURE_MVFR);
1008     }
1009     if (arm_feature(env, ARM_FEATURE_V6)) {
1010         set_feature(env, ARM_FEATURE_V5);
1011         if (!arm_feature(env, ARM_FEATURE_M)) {
1012             assert(no_aa32 || cpu_isar_feature(jazelle, cpu));
1013             set_feature(env, ARM_FEATURE_AUXCR);
1014         }
1015     }
1016     if (arm_feature(env, ARM_FEATURE_V5)) {
1017         set_feature(env, ARM_FEATURE_V4T);
1018     }
1019     if (arm_feature(env, ARM_FEATURE_VFP4)) {
1020         set_feature(env, ARM_FEATURE_VFP3);
1021     }
1022     if (arm_feature(env, ARM_FEATURE_VFP3)) {
1023         set_feature(env, ARM_FEATURE_VFP);
1024     }
1025     if (arm_feature(env, ARM_FEATURE_LPAE)) {
1026         set_feature(env, ARM_FEATURE_V7MP);
1027         set_feature(env, ARM_FEATURE_PXN);
1028     }
1029     if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
1030         set_feature(env, ARM_FEATURE_CBAR);
1031     }
1032     if (arm_feature(env, ARM_FEATURE_THUMB2) &&
1033         !arm_feature(env, ARM_FEATURE_M)) {
1034         set_feature(env, ARM_FEATURE_THUMB_DSP);
1035     }
1036 
1037     if (arm_feature(env, ARM_FEATURE_V7) &&
1038         !arm_feature(env, ARM_FEATURE_M) &&
1039         !arm_feature(env, ARM_FEATURE_PMSA)) {
1040         /* v7VMSA drops support for the old ARMv5 tiny pages, so we
1041          * can use 4K pages.
1042          */
1043         pagebits = 12;
1044     } else {
1045         /* For CPUs which might have tiny 1K pages, or which have an
1046          * MPU and might have small region sizes, stick with 1K pages.
1047          */
1048         pagebits = 10;
1049     }
1050     if (!set_preferred_target_page_bits(pagebits)) {
1051         /* This can only ever happen for hotplugging a CPU, or if
1052          * the board code incorrectly creates a CPU which it has
1053          * promised via minimum_page_size that it will not.
1054          */
1055         error_setg(errp, "This CPU requires a smaller page size than the "
1056                    "system is using");
1057         return;
1058     }
1059 
1060     /* This cpu-id-to-MPIDR affinity is used only for TCG; KVM will override it.
1061      * We don't support setting cluster ID ([16..23]) (known as Aff2
1062      * in later ARM ARM versions), or any of the higher affinity level fields,
1063      * so these bits always RAZ.
1064      */
1065     if (cpu->mp_affinity == ARM64_AFFINITY_INVALID) {
1066         cpu->mp_affinity = arm_cpu_mp_affinity(cs->cpu_index,
1067                                                ARM_DEFAULT_CPUS_PER_CLUSTER);
1068     }
1069 
1070     if (cpu->reset_hivecs) {
1071             cpu->reset_sctlr |= (1 << 13);
1072     }
1073 
1074     if (cpu->cfgend) {
1075         if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
1076             cpu->reset_sctlr |= SCTLR_EE;
1077         } else {
1078             cpu->reset_sctlr |= SCTLR_B;
1079         }
1080     }
1081 
1082     if (!cpu->has_el3) {
1083         /* If the has_el3 CPU property is disabled then we need to disable the
1084          * feature.
1085          */
1086         unset_feature(env, ARM_FEATURE_EL3);
1087 
1088         /* Disable the security extension feature bits in the processor feature
1089          * registers as well. These are id_pfr1[7:4] and id_aa64pfr0[15:12].
1090          */
1091         cpu->id_pfr1 &= ~0xf0;
1092         cpu->isar.id_aa64pfr0 &= ~0xf000;
1093     }
1094 
1095     if (!cpu->has_el2) {
1096         unset_feature(env, ARM_FEATURE_EL2);
1097     }
1098 
1099     if (!cpu->has_pmu) {
1100         unset_feature(env, ARM_FEATURE_PMU);
1101     }
1102     if (arm_feature(env, ARM_FEATURE_PMU)) {
1103         pmu_init(cpu);
1104 
1105         if (!kvm_enabled()) {
1106             arm_register_pre_el_change_hook(cpu, &pmu_pre_el_change, 0);
1107             arm_register_el_change_hook(cpu, &pmu_post_el_change, 0);
1108         }
1109 
1110 #ifndef CONFIG_USER_ONLY
1111         cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, arm_pmu_timer_cb,
1112                 cpu);
1113 #endif
1114     } else {
1115         cpu->id_aa64dfr0 &= ~0xf00;
1116         cpu->id_dfr0 &= ~(0xf << 24);
1117         cpu->pmceid0 = 0;
1118         cpu->pmceid1 = 0;
1119     }
1120 
1121     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1122         /* Disable the hypervisor feature bits in the processor feature
1123          * registers if we don't have EL2. These are id_pfr1[15:12] and
1124          * id_aa64pfr0_el1[11:8].
1125          */
1126         cpu->isar.id_aa64pfr0 &= ~0xf00;
1127         cpu->id_pfr1 &= ~0xf000;
1128     }
1129 
1130     /* MPU can be configured out of a PMSA CPU either by setting has-mpu
1131      * to false or by setting pmsav7-dregion to 0.
1132      */
1133     if (!cpu->has_mpu) {
1134         cpu->pmsav7_dregion = 0;
1135     }
1136     if (cpu->pmsav7_dregion == 0) {
1137         cpu->has_mpu = false;
1138     }
1139 
1140     if (arm_feature(env, ARM_FEATURE_PMSA) &&
1141         arm_feature(env, ARM_FEATURE_V7)) {
1142         uint32_t nr = cpu->pmsav7_dregion;
1143 
1144         if (nr > 0xff) {
1145             error_setg(errp, "PMSAv7 MPU #regions invalid %" PRIu32, nr);
1146             return;
1147         }
1148 
1149         if (nr) {
1150             if (arm_feature(env, ARM_FEATURE_V8)) {
1151                 /* PMSAv8 */
1152                 env->pmsav8.rbar[M_REG_NS] = g_new0(uint32_t, nr);
1153                 env->pmsav8.rlar[M_REG_NS] = g_new0(uint32_t, nr);
1154                 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
1155                     env->pmsav8.rbar[M_REG_S] = g_new0(uint32_t, nr);
1156                     env->pmsav8.rlar[M_REG_S] = g_new0(uint32_t, nr);
1157                 }
1158             } else {
1159                 env->pmsav7.drbar = g_new0(uint32_t, nr);
1160                 env->pmsav7.drsr = g_new0(uint32_t, nr);
1161                 env->pmsav7.dracr = g_new0(uint32_t, nr);
1162             }
1163         }
1164     }
1165 
1166     if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
1167         uint32_t nr = cpu->sau_sregion;
1168 
1169         if (nr > 0xff) {
1170             error_setg(errp, "v8M SAU #regions invalid %" PRIu32, nr);
1171             return;
1172         }
1173 
1174         if (nr) {
1175             env->sau.rbar = g_new0(uint32_t, nr);
1176             env->sau.rlar = g_new0(uint32_t, nr);
1177         }
1178     }
1179 
1180     if (arm_feature(env, ARM_FEATURE_EL3)) {
1181         set_feature(env, ARM_FEATURE_VBAR);
1182     }
1183 
1184     register_cp_regs_for_features(cpu);
1185     arm_cpu_register_gdb_regs_for_features(cpu);
1186 
1187     init_cpreg_list(cpu);
1188 
1189 #ifndef CONFIG_USER_ONLY
1190     if (cpu->has_el3 || arm_feature(env, ARM_FEATURE_M_SECURITY)) {
1191         cs->num_ases = 2;
1192 
1193         if (!cpu->secure_memory) {
1194             cpu->secure_memory = cs->memory;
1195         }
1196         cpu_address_space_init(cs, ARMASIdx_S, "cpu-secure-memory",
1197                                cpu->secure_memory);
1198     } else {
1199         cs->num_ases = 1;
1200     }
1201     cpu_address_space_init(cs, ARMASIdx_NS, "cpu-memory", cs->memory);
1202 
1203     /* No core_count specified, default to smp_cpus. */
1204     if (cpu->core_count == -1) {
1205         cpu->core_count = smp_cpus;
1206     }
1207 #endif
1208 
1209     qemu_init_vcpu(cs);
1210     cpu_reset(cs);
1211 
1212     acc->parent_realize(dev, errp);
1213 }
1214 
1215 static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
1216 {
1217     ObjectClass *oc;
1218     char *typename;
1219     char **cpuname;
1220     const char *cpunamestr;
1221 
1222     cpuname = g_strsplit(cpu_model, ",", 1);
1223     cpunamestr = cpuname[0];
1224 #ifdef CONFIG_USER_ONLY
1225     /* For backwards compatibility usermode emulation allows "-cpu any",
1226      * which has the same semantics as "-cpu max".
1227      */
1228     if (!strcmp(cpunamestr, "any")) {
1229         cpunamestr = "max";
1230     }
1231 #endif
1232     typename = g_strdup_printf(ARM_CPU_TYPE_NAME("%s"), cpunamestr);
1233     oc = object_class_by_name(typename);
1234     g_strfreev(cpuname);
1235     g_free(typename);
1236     if (!oc || !object_class_dynamic_cast(oc, TYPE_ARM_CPU) ||
1237         object_class_is_abstract(oc)) {
1238         return NULL;
1239     }
1240     return oc;
1241 }
1242 
1243 /* CPU models. These are not needed for the AArch64 linux-user build. */
1244 #if !defined(CONFIG_USER_ONLY) || !defined(TARGET_AARCH64)
1245 
1246 static void arm926_initfn(Object *obj)
1247 {
1248     ARMCPU *cpu = ARM_CPU(obj);
1249 
1250     cpu->dtb_compatible = "arm,arm926";
1251     set_feature(&cpu->env, ARM_FEATURE_V5);
1252     set_feature(&cpu->env, ARM_FEATURE_VFP);
1253     set_feature(&cpu->env, ARM_FEATURE_DUMMY_C15_REGS);
1254     set_feature(&cpu->env, ARM_FEATURE_CACHE_TEST_CLEAN);
1255     cpu->midr = 0x41069265;
1256     cpu->reset_fpsid = 0x41011090;
1257     cpu->ctr = 0x1dd20d2;
1258     cpu->reset_sctlr = 0x00090078;
1259 
1260     /*
1261      * ARMv5 does not have the ID_ISAR registers, but we can still
1262      * set the field to indicate Jazelle support within QEMU.
1263      */
1264     cpu->isar.id_isar1 = FIELD_DP32(cpu->isar.id_isar1, ID_ISAR1, JAZELLE, 1);
1265 }
1266 
1267 static void arm946_initfn(Object *obj)
1268 {
1269     ARMCPU *cpu = ARM_CPU(obj);
1270 
1271     cpu->dtb_compatible = "arm,arm946";
1272     set_feature(&cpu->env, ARM_FEATURE_V5);
1273     set_feature(&cpu->env, ARM_FEATURE_PMSA);
1274     set_feature(&cpu->env, ARM_FEATURE_DUMMY_C15_REGS);
1275     cpu->midr = 0x41059461;
1276     cpu->ctr = 0x0f004006;
1277     cpu->reset_sctlr = 0x00000078;
1278 }
1279 
1280 static void arm1026_initfn(Object *obj)
1281 {
1282     ARMCPU *cpu = ARM_CPU(obj);
1283 
1284     cpu->dtb_compatible = "arm,arm1026";
1285     set_feature(&cpu->env, ARM_FEATURE_V5);
1286     set_feature(&cpu->env, ARM_FEATURE_VFP);
1287     set_feature(&cpu->env, ARM_FEATURE_AUXCR);
1288     set_feature(&cpu->env, ARM_FEATURE_DUMMY_C15_REGS);
1289     set_feature(&cpu->env, ARM_FEATURE_CACHE_TEST_CLEAN);
1290     cpu->midr = 0x4106a262;
1291     cpu->reset_fpsid = 0x410110a0;
1292     cpu->ctr = 0x1dd20d2;
1293     cpu->reset_sctlr = 0x00090078;
1294     cpu->reset_auxcr = 1;
1295 
1296     /*
1297      * ARMv5 does not have the ID_ISAR registers, but we can still
1298      * set the field to indicate Jazelle support within QEMU.
1299      */
1300     cpu->isar.id_isar1 = FIELD_DP32(cpu->isar.id_isar1, ID_ISAR1, JAZELLE, 1);
1301 
1302     {
1303         /* The 1026 had an IFAR at c6,c0,0,1 rather than the ARMv6 c6,c0,0,2 */
1304         ARMCPRegInfo ifar = {
1305             .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
1306             .access = PL1_RW,
1307             .fieldoffset = offsetof(CPUARMState, cp15.ifar_ns),
1308             .resetvalue = 0
1309         };
1310         define_one_arm_cp_reg(cpu, &ifar);
1311     }
1312 }
1313 
1314 static void arm1136_r2_initfn(Object *obj)
1315 {
1316     ARMCPU *cpu = ARM_CPU(obj);
1317     /* What qemu calls "arm1136_r2" is actually the 1136 r0p2, ie an
1318      * older core than plain "arm1136". In particular this does not
1319      * have the v6K features.
1320      * These ID register values are correct for 1136 but may be wrong
1321      * for 1136_r2 (in particular r0p2 does not actually implement most
1322      * of the ID registers).
1323      */
1324 
1325     cpu->dtb_compatible = "arm,arm1136";
1326     set_feature(&cpu->env, ARM_FEATURE_V6);
1327     set_feature(&cpu->env, ARM_FEATURE_VFP);
1328     set_feature(&cpu->env, ARM_FEATURE_DUMMY_C15_REGS);
1329     set_feature(&cpu->env, ARM_FEATURE_CACHE_DIRTY_REG);
1330     set_feature(&cpu->env, ARM_FEATURE_CACHE_BLOCK_OPS);
1331     cpu->midr = 0x4107b362;
1332     cpu->reset_fpsid = 0x410120b4;
1333     cpu->isar.mvfr0 = 0x11111111;
1334     cpu->isar.mvfr1 = 0x00000000;
1335     cpu->ctr = 0x1dd20d2;
1336     cpu->reset_sctlr = 0x00050078;
1337     cpu->id_pfr0 = 0x111;
1338     cpu->id_pfr1 = 0x1;
1339     cpu->id_dfr0 = 0x2;
1340     cpu->id_afr0 = 0x3;
1341     cpu->id_mmfr0 = 0x01130003;
1342     cpu->id_mmfr1 = 0x10030302;
1343     cpu->id_mmfr2 = 0x01222110;
1344     cpu->isar.id_isar0 = 0x00140011;
1345     cpu->isar.id_isar1 = 0x12002111;
1346     cpu->isar.id_isar2 = 0x11231111;
1347     cpu->isar.id_isar3 = 0x01102131;
1348     cpu->isar.id_isar4 = 0x141;
1349     cpu->reset_auxcr = 7;
1350 }
1351 
1352 static void arm1136_initfn(Object *obj)
1353 {
1354     ARMCPU *cpu = ARM_CPU(obj);
1355 
1356     cpu->dtb_compatible = "arm,arm1136";
1357     set_feature(&cpu->env, ARM_FEATURE_V6K);
1358     set_feature(&cpu->env, ARM_FEATURE_V6);
1359     set_feature(&cpu->env, ARM_FEATURE_VFP);
1360     set_feature(&cpu->env, ARM_FEATURE_DUMMY_C15_REGS);
1361     set_feature(&cpu->env, ARM_FEATURE_CACHE_DIRTY_REG);
1362     set_feature(&cpu->env, ARM_FEATURE_CACHE_BLOCK_OPS);
1363     cpu->midr = 0x4117b363;
1364     cpu->reset_fpsid = 0x410120b4;
1365     cpu->isar.mvfr0 = 0x11111111;
1366     cpu->isar.mvfr1 = 0x00000000;
1367     cpu->ctr = 0x1dd20d2;
1368     cpu->reset_sctlr = 0x00050078;
1369     cpu->id_pfr0 = 0x111;
1370     cpu->id_pfr1 = 0x1;
1371     cpu->id_dfr0 = 0x2;
1372     cpu->id_afr0 = 0x3;
1373     cpu->id_mmfr0 = 0x01130003;
1374     cpu->id_mmfr1 = 0x10030302;
1375     cpu->id_mmfr2 = 0x01222110;
1376     cpu->isar.id_isar0 = 0x00140011;
1377     cpu->isar.id_isar1 = 0x12002111;
1378     cpu->isar.id_isar2 = 0x11231111;
1379     cpu->isar.id_isar3 = 0x01102131;
1380     cpu->isar.id_isar4 = 0x141;
1381     cpu->reset_auxcr = 7;
1382 }
1383 
1384 static void arm1176_initfn(Object *obj)
1385 {
1386     ARMCPU *cpu = ARM_CPU(obj);
1387 
1388     cpu->dtb_compatible = "arm,arm1176";
1389     set_feature(&cpu->env, ARM_FEATURE_V6K);
1390     set_feature(&cpu->env, ARM_FEATURE_VFP);
1391     set_feature(&cpu->env, ARM_FEATURE_VAPA);
1392     set_feature(&cpu->env, ARM_FEATURE_DUMMY_C15_REGS);
1393     set_feature(&cpu->env, ARM_FEATURE_CACHE_DIRTY_REG);
1394     set_feature(&cpu->env, ARM_FEATURE_CACHE_BLOCK_OPS);
1395     set_feature(&cpu->env, ARM_FEATURE_EL3);
1396     cpu->midr = 0x410fb767;
1397     cpu->reset_fpsid = 0x410120b5;
1398     cpu->isar.mvfr0 = 0x11111111;
1399     cpu->isar.mvfr1 = 0x00000000;
1400     cpu->ctr = 0x1dd20d2;
1401     cpu->reset_sctlr = 0x00050078;
1402     cpu->id_pfr0 = 0x111;
1403     cpu->id_pfr1 = 0x11;
1404     cpu->id_dfr0 = 0x33;
1405     cpu->id_afr0 = 0;
1406     cpu->id_mmfr0 = 0x01130003;
1407     cpu->id_mmfr1 = 0x10030302;
1408     cpu->id_mmfr2 = 0x01222100;
1409     cpu->isar.id_isar0 = 0x0140011;
1410     cpu->isar.id_isar1 = 0x12002111;
1411     cpu->isar.id_isar2 = 0x11231121;
1412     cpu->isar.id_isar3 = 0x01102131;
1413     cpu->isar.id_isar4 = 0x01141;
1414     cpu->reset_auxcr = 7;
1415 }
1416 
1417 static void arm11mpcore_initfn(Object *obj)
1418 {
1419     ARMCPU *cpu = ARM_CPU(obj);
1420 
1421     cpu->dtb_compatible = "arm,arm11mpcore";
1422     set_feature(&cpu->env, ARM_FEATURE_V6K);
1423     set_feature(&cpu->env, ARM_FEATURE_VFP);
1424     set_feature(&cpu->env, ARM_FEATURE_VAPA);
1425     set_feature(&cpu->env, ARM_FEATURE_MPIDR);
1426     set_feature(&cpu->env, ARM_FEATURE_DUMMY_C15_REGS);
1427     cpu->midr = 0x410fb022;
1428     cpu->reset_fpsid = 0x410120b4;
1429     cpu->isar.mvfr0 = 0x11111111;
1430     cpu->isar.mvfr1 = 0x00000000;
1431     cpu->ctr = 0x1d192992; /* 32K icache 32K dcache */
1432     cpu->id_pfr0 = 0x111;
1433     cpu->id_pfr1 = 0x1;
1434     cpu->id_dfr0 = 0;
1435     cpu->id_afr0 = 0x2;
1436     cpu->id_mmfr0 = 0x01100103;
1437     cpu->id_mmfr1 = 0x10020302;
1438     cpu->id_mmfr2 = 0x01222000;
1439     cpu->isar.id_isar0 = 0x00100011;
1440     cpu->isar.id_isar1 = 0x12002111;
1441     cpu->isar.id_isar2 = 0x11221011;
1442     cpu->isar.id_isar3 = 0x01102131;
1443     cpu->isar.id_isar4 = 0x141;
1444     cpu->reset_auxcr = 1;
1445 }
1446 
1447 static void cortex_m0_initfn(Object *obj)
1448 {
1449     ARMCPU *cpu = ARM_CPU(obj);
1450     set_feature(&cpu->env, ARM_FEATURE_V6);
1451     set_feature(&cpu->env, ARM_FEATURE_M);
1452 
1453     cpu->midr = 0x410cc200;
1454 }
1455 
1456 static void cortex_m3_initfn(Object *obj)
1457 {
1458     ARMCPU *cpu = ARM_CPU(obj);
1459     set_feature(&cpu->env, ARM_FEATURE_V7);
1460     set_feature(&cpu->env, ARM_FEATURE_M);
1461     set_feature(&cpu->env, ARM_FEATURE_M_MAIN);
1462     cpu->midr = 0x410fc231;
1463     cpu->pmsav7_dregion = 8;
1464     cpu->id_pfr0 = 0x00000030;
1465     cpu->id_pfr1 = 0x00000200;
1466     cpu->id_dfr0 = 0x00100000;
1467     cpu->id_afr0 = 0x00000000;
1468     cpu->id_mmfr0 = 0x00000030;
1469     cpu->id_mmfr1 = 0x00000000;
1470     cpu->id_mmfr2 = 0x00000000;
1471     cpu->id_mmfr3 = 0x00000000;
1472     cpu->isar.id_isar0 = 0x01141110;
1473     cpu->isar.id_isar1 = 0x02111000;
1474     cpu->isar.id_isar2 = 0x21112231;
1475     cpu->isar.id_isar3 = 0x01111110;
1476     cpu->isar.id_isar4 = 0x01310102;
1477     cpu->isar.id_isar5 = 0x00000000;
1478     cpu->isar.id_isar6 = 0x00000000;
1479 }
1480 
1481 static void cortex_m4_initfn(Object *obj)
1482 {
1483     ARMCPU *cpu = ARM_CPU(obj);
1484 
1485     set_feature(&cpu->env, ARM_FEATURE_V7);
1486     set_feature(&cpu->env, ARM_FEATURE_M);
1487     set_feature(&cpu->env, ARM_FEATURE_M_MAIN);
1488     set_feature(&cpu->env, ARM_FEATURE_THUMB_DSP);
1489     cpu->midr = 0x410fc240; /* r0p0 */
1490     cpu->pmsav7_dregion = 8;
1491     cpu->id_pfr0 = 0x00000030;
1492     cpu->id_pfr1 = 0x00000200;
1493     cpu->id_dfr0 = 0x00100000;
1494     cpu->id_afr0 = 0x00000000;
1495     cpu->id_mmfr0 = 0x00000030;
1496     cpu->id_mmfr1 = 0x00000000;
1497     cpu->id_mmfr2 = 0x00000000;
1498     cpu->id_mmfr3 = 0x00000000;
1499     cpu->isar.id_isar0 = 0x01141110;
1500     cpu->isar.id_isar1 = 0x02111000;
1501     cpu->isar.id_isar2 = 0x21112231;
1502     cpu->isar.id_isar3 = 0x01111110;
1503     cpu->isar.id_isar4 = 0x01310102;
1504     cpu->isar.id_isar5 = 0x00000000;
1505     cpu->isar.id_isar6 = 0x00000000;
1506 }
1507 
1508 static void cortex_m33_initfn(Object *obj)
1509 {
1510     ARMCPU *cpu = ARM_CPU(obj);
1511 
1512     set_feature(&cpu->env, ARM_FEATURE_V8);
1513     set_feature(&cpu->env, ARM_FEATURE_M);
1514     set_feature(&cpu->env, ARM_FEATURE_M_MAIN);
1515     set_feature(&cpu->env, ARM_FEATURE_M_SECURITY);
1516     set_feature(&cpu->env, ARM_FEATURE_THUMB_DSP);
1517     cpu->midr = 0x410fd213; /* r0p3 */
1518     cpu->pmsav7_dregion = 16;
1519     cpu->sau_sregion = 8;
1520     cpu->id_pfr0 = 0x00000030;
1521     cpu->id_pfr1 = 0x00000210;
1522     cpu->id_dfr0 = 0x00200000;
1523     cpu->id_afr0 = 0x00000000;
1524     cpu->id_mmfr0 = 0x00101F40;
1525     cpu->id_mmfr1 = 0x00000000;
1526     cpu->id_mmfr2 = 0x01000000;
1527     cpu->id_mmfr3 = 0x00000000;
1528     cpu->isar.id_isar0 = 0x01101110;
1529     cpu->isar.id_isar1 = 0x02212000;
1530     cpu->isar.id_isar2 = 0x20232232;
1531     cpu->isar.id_isar3 = 0x01111131;
1532     cpu->isar.id_isar4 = 0x01310132;
1533     cpu->isar.id_isar5 = 0x00000000;
1534     cpu->isar.id_isar6 = 0x00000000;
1535     cpu->clidr = 0x00000000;
1536     cpu->ctr = 0x8000c000;
1537 }
1538 
1539 static void arm_v7m_class_init(ObjectClass *oc, void *data)
1540 {
1541     ARMCPUClass *acc = ARM_CPU_CLASS(oc);
1542     CPUClass *cc = CPU_CLASS(oc);
1543 
1544     acc->info = data;
1545 #ifndef CONFIG_USER_ONLY
1546     cc->do_interrupt = arm_v7m_cpu_do_interrupt;
1547 #endif
1548 
1549     cc->cpu_exec_interrupt = arm_v7m_cpu_exec_interrupt;
1550 }
1551 
1552 static const ARMCPRegInfo cortexr5_cp_reginfo[] = {
1553     /* Dummy the TCM region regs for the moment */
1554     { .name = "ATCM", .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
1555       .access = PL1_RW, .type = ARM_CP_CONST },
1556     { .name = "BTCM", .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
1557       .access = PL1_RW, .type = ARM_CP_CONST },
1558     { .name = "DCACHE_INVAL", .cp = 15, .opc1 = 0, .crn = 15, .crm = 5,
1559       .opc2 = 0, .access = PL1_W, .type = ARM_CP_NOP },
1560     REGINFO_SENTINEL
1561 };
1562 
1563 static void cortex_r5_initfn(Object *obj)
1564 {
1565     ARMCPU *cpu = ARM_CPU(obj);
1566 
1567     set_feature(&cpu->env, ARM_FEATURE_V7);
1568     set_feature(&cpu->env, ARM_FEATURE_V7MP);
1569     set_feature(&cpu->env, ARM_FEATURE_PMSA);
1570     cpu->midr = 0x411fc153; /* r1p3 */
1571     cpu->id_pfr0 = 0x0131;
1572     cpu->id_pfr1 = 0x001;
1573     cpu->id_dfr0 = 0x010400;
1574     cpu->id_afr0 = 0x0;
1575     cpu->id_mmfr0 = 0x0210030;
1576     cpu->id_mmfr1 = 0x00000000;
1577     cpu->id_mmfr2 = 0x01200000;
1578     cpu->id_mmfr3 = 0x0211;
1579     cpu->isar.id_isar0 = 0x02101111;
1580     cpu->isar.id_isar1 = 0x13112111;
1581     cpu->isar.id_isar2 = 0x21232141;
1582     cpu->isar.id_isar3 = 0x01112131;
1583     cpu->isar.id_isar4 = 0x0010142;
1584     cpu->isar.id_isar5 = 0x0;
1585     cpu->isar.id_isar6 = 0x0;
1586     cpu->mp_is_up = true;
1587     cpu->pmsav7_dregion = 16;
1588     define_arm_cp_regs(cpu, cortexr5_cp_reginfo);
1589 }
1590 
1591 static void cortex_r5f_initfn(Object *obj)
1592 {
1593     ARMCPU *cpu = ARM_CPU(obj);
1594 
1595     cortex_r5_initfn(obj);
1596     set_feature(&cpu->env, ARM_FEATURE_VFP3);
1597 }
1598 
1599 static const ARMCPRegInfo cortexa8_cp_reginfo[] = {
1600     { .name = "L2LOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 1, .opc2 = 0,
1601       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1602     { .name = "L2AUXCR", .cp = 15, .crn = 9, .crm = 0, .opc1 = 1, .opc2 = 2,
1603       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1604     REGINFO_SENTINEL
1605 };
1606 
1607 static void cortex_a8_initfn(Object *obj)
1608 {
1609     ARMCPU *cpu = ARM_CPU(obj);
1610 
1611     cpu->dtb_compatible = "arm,cortex-a8";
1612     set_feature(&cpu->env, ARM_FEATURE_V7);
1613     set_feature(&cpu->env, ARM_FEATURE_VFP3);
1614     set_feature(&cpu->env, ARM_FEATURE_NEON);
1615     set_feature(&cpu->env, ARM_FEATURE_THUMB2EE);
1616     set_feature(&cpu->env, ARM_FEATURE_DUMMY_C15_REGS);
1617     set_feature(&cpu->env, ARM_FEATURE_EL3);
1618     cpu->midr = 0x410fc080;
1619     cpu->reset_fpsid = 0x410330c0;
1620     cpu->isar.mvfr0 = 0x11110222;
1621     cpu->isar.mvfr1 = 0x00011111;
1622     cpu->ctr = 0x82048004;
1623     cpu->reset_sctlr = 0x00c50078;
1624     cpu->id_pfr0 = 0x1031;
1625     cpu->id_pfr1 = 0x11;
1626     cpu->id_dfr0 = 0x400;
1627     cpu->id_afr0 = 0;
1628     cpu->id_mmfr0 = 0x31100003;
1629     cpu->id_mmfr1 = 0x20000000;
1630     cpu->id_mmfr2 = 0x01202000;
1631     cpu->id_mmfr3 = 0x11;
1632     cpu->isar.id_isar0 = 0x00101111;
1633     cpu->isar.id_isar1 = 0x12112111;
1634     cpu->isar.id_isar2 = 0x21232031;
1635     cpu->isar.id_isar3 = 0x11112131;
1636     cpu->isar.id_isar4 = 0x00111142;
1637     cpu->dbgdidr = 0x15141000;
1638     cpu->clidr = (1 << 27) | (2 << 24) | 3;
1639     cpu->ccsidr[0] = 0xe007e01a; /* 16k L1 dcache. */
1640     cpu->ccsidr[1] = 0x2007e01a; /* 16k L1 icache. */
1641     cpu->ccsidr[2] = 0xf0000000; /* No L2 icache. */
1642     cpu->reset_auxcr = 2;
1643     define_arm_cp_regs(cpu, cortexa8_cp_reginfo);
1644 }
1645 
1646 static const ARMCPRegInfo cortexa9_cp_reginfo[] = {
1647     /* power_control should be set to maximum latency. Again,
1648      * default to 0 and set by private hook
1649      */
1650     { .name = "A9_PWRCTL", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
1651       .access = PL1_RW, .resetvalue = 0,
1652       .fieldoffset = offsetof(CPUARMState, cp15.c15_power_control) },
1653     { .name = "A9_DIAG", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 1,
1654       .access = PL1_RW, .resetvalue = 0,
1655       .fieldoffset = offsetof(CPUARMState, cp15.c15_diagnostic) },
1656     { .name = "A9_PWRDIAG", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 2,
1657       .access = PL1_RW, .resetvalue = 0,
1658       .fieldoffset = offsetof(CPUARMState, cp15.c15_power_diagnostic) },
1659     { .name = "NEONBUSY", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
1660       .access = PL1_RW, .resetvalue = 0, .type = ARM_CP_CONST },
1661     /* TLB lockdown control */
1662     { .name = "TLB_LOCKR", .cp = 15, .crn = 15, .crm = 4, .opc1 = 5, .opc2 = 2,
1663       .access = PL1_W, .resetvalue = 0, .type = ARM_CP_NOP },
1664     { .name = "TLB_LOCKW", .cp = 15, .crn = 15, .crm = 4, .opc1 = 5, .opc2 = 4,
1665       .access = PL1_W, .resetvalue = 0, .type = ARM_CP_NOP },
1666     { .name = "TLB_VA", .cp = 15, .crn = 15, .crm = 5, .opc1 = 5, .opc2 = 2,
1667       .access = PL1_RW, .resetvalue = 0, .type = ARM_CP_CONST },
1668     { .name = "TLB_PA", .cp = 15, .crn = 15, .crm = 6, .opc1 = 5, .opc2 = 2,
1669       .access = PL1_RW, .resetvalue = 0, .type = ARM_CP_CONST },
1670     { .name = "TLB_ATTR", .cp = 15, .crn = 15, .crm = 7, .opc1 = 5, .opc2 = 2,
1671       .access = PL1_RW, .resetvalue = 0, .type = ARM_CP_CONST },
1672     REGINFO_SENTINEL
1673 };
1674 
1675 static void cortex_a9_initfn(Object *obj)
1676 {
1677     ARMCPU *cpu = ARM_CPU(obj);
1678 
1679     cpu->dtb_compatible = "arm,cortex-a9";
1680     set_feature(&cpu->env, ARM_FEATURE_V7);
1681     set_feature(&cpu->env, ARM_FEATURE_VFP3);
1682     set_feature(&cpu->env, ARM_FEATURE_NEON);
1683     set_feature(&cpu->env, ARM_FEATURE_THUMB2EE);
1684     set_feature(&cpu->env, ARM_FEATURE_EL3);
1685     /* Note that A9 supports the MP extensions even for
1686      * A9UP and single-core A9MP (which are both different
1687      * and valid configurations; we don't model A9UP).
1688      */
1689     set_feature(&cpu->env, ARM_FEATURE_V7MP);
1690     set_feature(&cpu->env, ARM_FEATURE_CBAR);
1691     cpu->midr = 0x410fc090;
1692     cpu->reset_fpsid = 0x41033090;
1693     cpu->isar.mvfr0 = 0x11110222;
1694     cpu->isar.mvfr1 = 0x01111111;
1695     cpu->ctr = 0x80038003;
1696     cpu->reset_sctlr = 0x00c50078;
1697     cpu->id_pfr0 = 0x1031;
1698     cpu->id_pfr1 = 0x11;
1699     cpu->id_dfr0 = 0x000;
1700     cpu->id_afr0 = 0;
1701     cpu->id_mmfr0 = 0x00100103;
1702     cpu->id_mmfr1 = 0x20000000;
1703     cpu->id_mmfr2 = 0x01230000;
1704     cpu->id_mmfr3 = 0x00002111;
1705     cpu->isar.id_isar0 = 0x00101111;
1706     cpu->isar.id_isar1 = 0x13112111;
1707     cpu->isar.id_isar2 = 0x21232041;
1708     cpu->isar.id_isar3 = 0x11112131;
1709     cpu->isar.id_isar4 = 0x00111142;
1710     cpu->dbgdidr = 0x35141000;
1711     cpu->clidr = (1 << 27) | (1 << 24) | 3;
1712     cpu->ccsidr[0] = 0xe00fe019; /* 16k L1 dcache. */
1713     cpu->ccsidr[1] = 0x200fe019; /* 16k L1 icache. */
1714     define_arm_cp_regs(cpu, cortexa9_cp_reginfo);
1715 }
1716 
1717 #ifndef CONFIG_USER_ONLY
1718 static uint64_t a15_l2ctlr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1719 {
1720     /* Linux wants the number of processors from here.
1721      * Might as well set the interrupt-controller bit too.
1722      */
1723     return ((smp_cpus - 1) << 24) | (1 << 23);
1724 }
1725 #endif
1726 
1727 static const ARMCPRegInfo cortexa15_cp_reginfo[] = {
1728 #ifndef CONFIG_USER_ONLY
1729     { .name = "L2CTLR", .cp = 15, .crn = 9, .crm = 0, .opc1 = 1, .opc2 = 2,
1730       .access = PL1_RW, .resetvalue = 0, .readfn = a15_l2ctlr_read,
1731       .writefn = arm_cp_write_ignore, },
1732 #endif
1733     { .name = "L2ECTLR", .cp = 15, .crn = 9, .crm = 0, .opc1 = 1, .opc2 = 3,
1734       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1735     REGINFO_SENTINEL
1736 };
1737 
1738 static void cortex_a7_initfn(Object *obj)
1739 {
1740     ARMCPU *cpu = ARM_CPU(obj);
1741 
1742     cpu->dtb_compatible = "arm,cortex-a7";
1743     set_feature(&cpu->env, ARM_FEATURE_V7VE);
1744     set_feature(&cpu->env, ARM_FEATURE_VFP4);
1745     set_feature(&cpu->env, ARM_FEATURE_NEON);
1746     set_feature(&cpu->env, ARM_FEATURE_THUMB2EE);
1747     set_feature(&cpu->env, ARM_FEATURE_GENERIC_TIMER);
1748     set_feature(&cpu->env, ARM_FEATURE_DUMMY_C15_REGS);
1749     set_feature(&cpu->env, ARM_FEATURE_CBAR_RO);
1750     set_feature(&cpu->env, ARM_FEATURE_EL2);
1751     set_feature(&cpu->env, ARM_FEATURE_EL3);
1752     set_feature(&cpu->env, ARM_FEATURE_PMU);
1753     cpu->kvm_target = QEMU_KVM_ARM_TARGET_CORTEX_A7;
1754     cpu->midr = 0x410fc075;
1755     cpu->reset_fpsid = 0x41023075;
1756     cpu->isar.mvfr0 = 0x10110222;
1757     cpu->isar.mvfr1 = 0x11111111;
1758     cpu->ctr = 0x84448003;
1759     cpu->reset_sctlr = 0x00c50078;
1760     cpu->id_pfr0 = 0x00001131;
1761     cpu->id_pfr1 = 0x00011011;
1762     cpu->id_dfr0 = 0x02010555;
1763     cpu->id_afr0 = 0x00000000;
1764     cpu->id_mmfr0 = 0x10101105;
1765     cpu->id_mmfr1 = 0x40000000;
1766     cpu->id_mmfr2 = 0x01240000;
1767     cpu->id_mmfr3 = 0x02102211;
1768     /* a7_mpcore_r0p5_trm, page 4-4 gives 0x01101110; but
1769      * table 4-41 gives 0x02101110, which includes the arm div insns.
1770      */
1771     cpu->isar.id_isar0 = 0x02101110;
1772     cpu->isar.id_isar1 = 0x13112111;
1773     cpu->isar.id_isar2 = 0x21232041;
1774     cpu->isar.id_isar3 = 0x11112131;
1775     cpu->isar.id_isar4 = 0x10011142;
1776     cpu->dbgdidr = 0x3515f005;
1777     cpu->clidr = 0x0a200023;
1778     cpu->ccsidr[0] = 0x701fe00a; /* 32K L1 dcache */
1779     cpu->ccsidr[1] = 0x201fe00a; /* 32K L1 icache */
1780     cpu->ccsidr[2] = 0x711fe07a; /* 4096K L2 unified cache */
1781     define_arm_cp_regs(cpu, cortexa15_cp_reginfo); /* Same as A15 */
1782 }
1783 
1784 static void cortex_a15_initfn(Object *obj)
1785 {
1786     ARMCPU *cpu = ARM_CPU(obj);
1787 
1788     cpu->dtb_compatible = "arm,cortex-a15";
1789     set_feature(&cpu->env, ARM_FEATURE_V7VE);
1790     set_feature(&cpu->env, ARM_FEATURE_VFP4);
1791     set_feature(&cpu->env, ARM_FEATURE_NEON);
1792     set_feature(&cpu->env, ARM_FEATURE_THUMB2EE);
1793     set_feature(&cpu->env, ARM_FEATURE_GENERIC_TIMER);
1794     set_feature(&cpu->env, ARM_FEATURE_DUMMY_C15_REGS);
1795     set_feature(&cpu->env, ARM_FEATURE_CBAR_RO);
1796     set_feature(&cpu->env, ARM_FEATURE_EL2);
1797     set_feature(&cpu->env, ARM_FEATURE_EL3);
1798     set_feature(&cpu->env, ARM_FEATURE_PMU);
1799     cpu->kvm_target = QEMU_KVM_ARM_TARGET_CORTEX_A15;
1800     cpu->midr = 0x412fc0f1;
1801     cpu->reset_fpsid = 0x410430f0;
1802     cpu->isar.mvfr0 = 0x10110222;
1803     cpu->isar.mvfr1 = 0x11111111;
1804     cpu->ctr = 0x8444c004;
1805     cpu->reset_sctlr = 0x00c50078;
1806     cpu->id_pfr0 = 0x00001131;
1807     cpu->id_pfr1 = 0x00011011;
1808     cpu->id_dfr0 = 0x02010555;
1809     cpu->id_afr0 = 0x00000000;
1810     cpu->id_mmfr0 = 0x10201105;
1811     cpu->id_mmfr1 = 0x20000000;
1812     cpu->id_mmfr2 = 0x01240000;
1813     cpu->id_mmfr3 = 0x02102211;
1814     cpu->isar.id_isar0 = 0x02101110;
1815     cpu->isar.id_isar1 = 0x13112111;
1816     cpu->isar.id_isar2 = 0x21232041;
1817     cpu->isar.id_isar3 = 0x11112131;
1818     cpu->isar.id_isar4 = 0x10011142;
1819     cpu->dbgdidr = 0x3515f021;
1820     cpu->clidr = 0x0a200023;
1821     cpu->ccsidr[0] = 0x701fe00a; /* 32K L1 dcache */
1822     cpu->ccsidr[1] = 0x201fe00a; /* 32K L1 icache */
1823     cpu->ccsidr[2] = 0x711fe07a; /* 4096K L2 unified cache */
1824     define_arm_cp_regs(cpu, cortexa15_cp_reginfo);
1825 }
1826 
1827 static void ti925t_initfn(Object *obj)
1828 {
1829     ARMCPU *cpu = ARM_CPU(obj);
1830     set_feature(&cpu->env, ARM_FEATURE_V4T);
1831     set_feature(&cpu->env, ARM_FEATURE_OMAPCP);
1832     cpu->midr = ARM_CPUID_TI925T;
1833     cpu->ctr = 0x5109149;
1834     cpu->reset_sctlr = 0x00000070;
1835 }
1836 
1837 static void sa1100_initfn(Object *obj)
1838 {
1839     ARMCPU *cpu = ARM_CPU(obj);
1840 
1841     cpu->dtb_compatible = "intel,sa1100";
1842     set_feature(&cpu->env, ARM_FEATURE_STRONGARM);
1843     set_feature(&cpu->env, ARM_FEATURE_DUMMY_C15_REGS);
1844     cpu->midr = 0x4401A11B;
1845     cpu->reset_sctlr = 0x00000070;
1846 }
1847 
1848 static void sa1110_initfn(Object *obj)
1849 {
1850     ARMCPU *cpu = ARM_CPU(obj);
1851     set_feature(&cpu->env, ARM_FEATURE_STRONGARM);
1852     set_feature(&cpu->env, ARM_FEATURE_DUMMY_C15_REGS);
1853     cpu->midr = 0x6901B119;
1854     cpu->reset_sctlr = 0x00000070;
1855 }
1856 
1857 static void pxa250_initfn(Object *obj)
1858 {
1859     ARMCPU *cpu = ARM_CPU(obj);
1860 
1861     cpu->dtb_compatible = "marvell,xscale";
1862     set_feature(&cpu->env, ARM_FEATURE_V5);
1863     set_feature(&cpu->env, ARM_FEATURE_XSCALE);
1864     cpu->midr = 0x69052100;
1865     cpu->ctr = 0xd172172;
1866     cpu->reset_sctlr = 0x00000078;
1867 }
1868 
1869 static void pxa255_initfn(Object *obj)
1870 {
1871     ARMCPU *cpu = ARM_CPU(obj);
1872 
1873     cpu->dtb_compatible = "marvell,xscale";
1874     set_feature(&cpu->env, ARM_FEATURE_V5);
1875     set_feature(&cpu->env, ARM_FEATURE_XSCALE);
1876     cpu->midr = 0x69052d00;
1877     cpu->ctr = 0xd172172;
1878     cpu->reset_sctlr = 0x00000078;
1879 }
1880 
1881 static void pxa260_initfn(Object *obj)
1882 {
1883     ARMCPU *cpu = ARM_CPU(obj);
1884 
1885     cpu->dtb_compatible = "marvell,xscale";
1886     set_feature(&cpu->env, ARM_FEATURE_V5);
1887     set_feature(&cpu->env, ARM_FEATURE_XSCALE);
1888     cpu->midr = 0x69052903;
1889     cpu->ctr = 0xd172172;
1890     cpu->reset_sctlr = 0x00000078;
1891 }
1892 
1893 static void pxa261_initfn(Object *obj)
1894 {
1895     ARMCPU *cpu = ARM_CPU(obj);
1896 
1897     cpu->dtb_compatible = "marvell,xscale";
1898     set_feature(&cpu->env, ARM_FEATURE_V5);
1899     set_feature(&cpu->env, ARM_FEATURE_XSCALE);
1900     cpu->midr = 0x69052d05;
1901     cpu->ctr = 0xd172172;
1902     cpu->reset_sctlr = 0x00000078;
1903 }
1904 
1905 static void pxa262_initfn(Object *obj)
1906 {
1907     ARMCPU *cpu = ARM_CPU(obj);
1908 
1909     cpu->dtb_compatible = "marvell,xscale";
1910     set_feature(&cpu->env, ARM_FEATURE_V5);
1911     set_feature(&cpu->env, ARM_FEATURE_XSCALE);
1912     cpu->midr = 0x69052d06;
1913     cpu->ctr = 0xd172172;
1914     cpu->reset_sctlr = 0x00000078;
1915 }
1916 
1917 static void pxa270a0_initfn(Object *obj)
1918 {
1919     ARMCPU *cpu = ARM_CPU(obj);
1920 
1921     cpu->dtb_compatible = "marvell,xscale";
1922     set_feature(&cpu->env, ARM_FEATURE_V5);
1923     set_feature(&cpu->env, ARM_FEATURE_XSCALE);
1924     set_feature(&cpu->env, ARM_FEATURE_IWMMXT);
1925     cpu->midr = 0x69054110;
1926     cpu->ctr = 0xd172172;
1927     cpu->reset_sctlr = 0x00000078;
1928 }
1929 
1930 static void pxa270a1_initfn(Object *obj)
1931 {
1932     ARMCPU *cpu = ARM_CPU(obj);
1933 
1934     cpu->dtb_compatible = "marvell,xscale";
1935     set_feature(&cpu->env, ARM_FEATURE_V5);
1936     set_feature(&cpu->env, ARM_FEATURE_XSCALE);
1937     set_feature(&cpu->env, ARM_FEATURE_IWMMXT);
1938     cpu->midr = 0x69054111;
1939     cpu->ctr = 0xd172172;
1940     cpu->reset_sctlr = 0x00000078;
1941 }
1942 
1943 static void pxa270b0_initfn(Object *obj)
1944 {
1945     ARMCPU *cpu = ARM_CPU(obj);
1946 
1947     cpu->dtb_compatible = "marvell,xscale";
1948     set_feature(&cpu->env, ARM_FEATURE_V5);
1949     set_feature(&cpu->env, ARM_FEATURE_XSCALE);
1950     set_feature(&cpu->env, ARM_FEATURE_IWMMXT);
1951     cpu->midr = 0x69054112;
1952     cpu->ctr = 0xd172172;
1953     cpu->reset_sctlr = 0x00000078;
1954 }
1955 
1956 static void pxa270b1_initfn(Object *obj)
1957 {
1958     ARMCPU *cpu = ARM_CPU(obj);
1959 
1960     cpu->dtb_compatible = "marvell,xscale";
1961     set_feature(&cpu->env, ARM_FEATURE_V5);
1962     set_feature(&cpu->env, ARM_FEATURE_XSCALE);
1963     set_feature(&cpu->env, ARM_FEATURE_IWMMXT);
1964     cpu->midr = 0x69054113;
1965     cpu->ctr = 0xd172172;
1966     cpu->reset_sctlr = 0x00000078;
1967 }
1968 
1969 static void pxa270c0_initfn(Object *obj)
1970 {
1971     ARMCPU *cpu = ARM_CPU(obj);
1972 
1973     cpu->dtb_compatible = "marvell,xscale";
1974     set_feature(&cpu->env, ARM_FEATURE_V5);
1975     set_feature(&cpu->env, ARM_FEATURE_XSCALE);
1976     set_feature(&cpu->env, ARM_FEATURE_IWMMXT);
1977     cpu->midr = 0x69054114;
1978     cpu->ctr = 0xd172172;
1979     cpu->reset_sctlr = 0x00000078;
1980 }
1981 
1982 static void pxa270c5_initfn(Object *obj)
1983 {
1984     ARMCPU *cpu = ARM_CPU(obj);
1985 
1986     cpu->dtb_compatible = "marvell,xscale";
1987     set_feature(&cpu->env, ARM_FEATURE_V5);
1988     set_feature(&cpu->env, ARM_FEATURE_XSCALE);
1989     set_feature(&cpu->env, ARM_FEATURE_IWMMXT);
1990     cpu->midr = 0x69054117;
1991     cpu->ctr = 0xd172172;
1992     cpu->reset_sctlr = 0x00000078;
1993 }
1994 
1995 #ifndef TARGET_AARCH64
1996 /* -cpu max: if KVM is enabled, like -cpu host (best possible with this host);
1997  * otherwise, a CPU with as many features enabled as our emulation supports.
1998  * The version of '-cpu max' for qemu-system-aarch64 is defined in cpu64.c;
1999  * this only needs to handle 32 bits.
2000  */
2001 static void arm_max_initfn(Object *obj)
2002 {
2003     ARMCPU *cpu = ARM_CPU(obj);
2004 
2005     if (kvm_enabled()) {
2006         kvm_arm_set_cpu_features_from_host(cpu);
2007     } else {
2008         cortex_a15_initfn(obj);
2009 #ifdef CONFIG_USER_ONLY
2010         /* We don't set these in system emulation mode for the moment,
2011          * since we don't correctly set (all of) the ID registers to
2012          * advertise them.
2013          */
2014         set_feature(&cpu->env, ARM_FEATURE_V8);
2015         {
2016             uint32_t t;
2017 
2018             t = cpu->isar.id_isar5;
2019             t = FIELD_DP32(t, ID_ISAR5, AES, 2);
2020             t = FIELD_DP32(t, ID_ISAR5, SHA1, 1);
2021             t = FIELD_DP32(t, ID_ISAR5, SHA2, 1);
2022             t = FIELD_DP32(t, ID_ISAR5, CRC32, 1);
2023             t = FIELD_DP32(t, ID_ISAR5, RDM, 1);
2024             t = FIELD_DP32(t, ID_ISAR5, VCMA, 1);
2025             cpu->isar.id_isar5 = t;
2026 
2027             t = cpu->isar.id_isar6;
2028             t = FIELD_DP32(t, ID_ISAR6, JSCVT, 1);
2029             t = FIELD_DP32(t, ID_ISAR6, DP, 1);
2030             t = FIELD_DP32(t, ID_ISAR6, FHM, 1);
2031             t = FIELD_DP32(t, ID_ISAR6, SB, 1);
2032             t = FIELD_DP32(t, ID_ISAR6, SPECRES, 1);
2033             cpu->isar.id_isar6 = t;
2034 
2035             t = cpu->isar.mvfr2;
2036             t = FIELD_DP32(t, MVFR2, SIMDMISC, 3); /* SIMD MaxNum */
2037             t = FIELD_DP32(t, MVFR2, FPMISC, 4);   /* FP MaxNum */
2038             cpu->isar.mvfr2 = t;
2039 
2040             t = cpu->id_mmfr4;
2041             t = FIELD_DP32(t, ID_MMFR4, HPDS, 1); /* AA32HPD */
2042             cpu->id_mmfr4 = t;
2043         }
2044 #endif
2045     }
2046 }
2047 #endif
2048 
2049 #endif /* !defined(CONFIG_USER_ONLY) || !defined(TARGET_AARCH64) */
2050 
2051 struct ARMCPUInfo {
2052     const char *name;
2053     void (*initfn)(Object *obj);
2054     void (*class_init)(ObjectClass *oc, void *data);
2055 };
2056 
2057 static const ARMCPUInfo arm_cpus[] = {
2058 #if !defined(CONFIG_USER_ONLY) || !defined(TARGET_AARCH64)
2059     { .name = "arm926",      .initfn = arm926_initfn },
2060     { .name = "arm946",      .initfn = arm946_initfn },
2061     { .name = "arm1026",     .initfn = arm1026_initfn },
2062     /* What QEMU calls "arm1136-r2" is actually the 1136 r0p2, i.e. an
2063      * older core than plain "arm1136". In particular this does not
2064      * have the v6K features.
2065      */
2066     { .name = "arm1136-r2",  .initfn = arm1136_r2_initfn },
2067     { .name = "arm1136",     .initfn = arm1136_initfn },
2068     { .name = "arm1176",     .initfn = arm1176_initfn },
2069     { .name = "arm11mpcore", .initfn = arm11mpcore_initfn },
2070     { .name = "cortex-m0",   .initfn = cortex_m0_initfn,
2071                              .class_init = arm_v7m_class_init },
2072     { .name = "cortex-m3",   .initfn = cortex_m3_initfn,
2073                              .class_init = arm_v7m_class_init },
2074     { .name = "cortex-m4",   .initfn = cortex_m4_initfn,
2075                              .class_init = arm_v7m_class_init },
2076     { .name = "cortex-m33",  .initfn = cortex_m33_initfn,
2077                              .class_init = arm_v7m_class_init },
2078     { .name = "cortex-r5",   .initfn = cortex_r5_initfn },
2079     { .name = "cortex-r5f",  .initfn = cortex_r5f_initfn },
2080     { .name = "cortex-a7",   .initfn = cortex_a7_initfn },
2081     { .name = "cortex-a8",   .initfn = cortex_a8_initfn },
2082     { .name = "cortex-a9",   .initfn = cortex_a9_initfn },
2083     { .name = "cortex-a15",  .initfn = cortex_a15_initfn },
2084     { .name = "ti925t",      .initfn = ti925t_initfn },
2085     { .name = "sa1100",      .initfn = sa1100_initfn },
2086     { .name = "sa1110",      .initfn = sa1110_initfn },
2087     { .name = "pxa250",      .initfn = pxa250_initfn },
2088     { .name = "pxa255",      .initfn = pxa255_initfn },
2089     { .name = "pxa260",      .initfn = pxa260_initfn },
2090     { .name = "pxa261",      .initfn = pxa261_initfn },
2091     { .name = "pxa262",      .initfn = pxa262_initfn },
2092     /* "pxa270" is an alias for "pxa270-a0" */
2093     { .name = "pxa270",      .initfn = pxa270a0_initfn },
2094     { .name = "pxa270-a0",   .initfn = pxa270a0_initfn },
2095     { .name = "pxa270-a1",   .initfn = pxa270a1_initfn },
2096     { .name = "pxa270-b0",   .initfn = pxa270b0_initfn },
2097     { .name = "pxa270-b1",   .initfn = pxa270b1_initfn },
2098     { .name = "pxa270-c0",   .initfn = pxa270c0_initfn },
2099     { .name = "pxa270-c5",   .initfn = pxa270c5_initfn },
2100 #ifndef TARGET_AARCH64
2101     { .name = "max",         .initfn = arm_max_initfn },
2102 #endif
2103 #ifdef CONFIG_USER_ONLY
2104     { .name = "any",         .initfn = arm_max_initfn },
2105 #endif
2106 #endif
2107     { .name = NULL }
2108 };
2109 
2110 static Property arm_cpu_properties[] = {
2111     DEFINE_PROP_BOOL("start-powered-off", ARMCPU, start_powered_off, false),
2112     DEFINE_PROP_UINT32("psci-conduit", ARMCPU, psci_conduit, 0),
2113     DEFINE_PROP_UINT32("midr", ARMCPU, midr, 0),
2114     DEFINE_PROP_UINT64("mp-affinity", ARMCPU,
2115                         mp_affinity, ARM64_AFFINITY_INVALID),
2116     DEFINE_PROP_INT32("node-id", ARMCPU, node_id, CPU_UNSET_NUMA_NODE_ID),
2117     DEFINE_PROP_INT32("core-count", ARMCPU, core_count, -1),
2118     DEFINE_PROP_END_OF_LIST()
2119 };
2120 
2121 #ifdef CONFIG_USER_ONLY
2122 static int arm_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int size,
2123                                     int rw, int mmu_idx)
2124 {
2125     ARMCPU *cpu = ARM_CPU(cs);
2126     CPUARMState *env = &cpu->env;
2127 
2128     env->exception.vaddress = address;
2129     if (rw == 2) {
2130         cs->exception_index = EXCP_PREFETCH_ABORT;
2131     } else {
2132         cs->exception_index = EXCP_DATA_ABORT;
2133     }
2134     return 1;
2135 }
2136 #endif
2137 
2138 static gchar *arm_gdb_arch_name(CPUState *cs)
2139 {
2140     ARMCPU *cpu = ARM_CPU(cs);
2141     CPUARMState *env = &cpu->env;
2142 
2143     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
2144         return g_strdup("iwmmxt");
2145     }
2146     return g_strdup("arm");
2147 }
2148 
2149 static void arm_cpu_class_init(ObjectClass *oc, void *data)
2150 {
2151     ARMCPUClass *acc = ARM_CPU_CLASS(oc);
2152     CPUClass *cc = CPU_CLASS(acc);
2153     DeviceClass *dc = DEVICE_CLASS(oc);
2154 
2155     device_class_set_parent_realize(dc, arm_cpu_realizefn,
2156                                     &acc->parent_realize);
2157     dc->props = arm_cpu_properties;
2158 
2159     acc->parent_reset = cc->reset;
2160     cc->reset = arm_cpu_reset;
2161 
2162     cc->class_by_name = arm_cpu_class_by_name;
2163     cc->has_work = arm_cpu_has_work;
2164     cc->cpu_exec_interrupt = arm_cpu_exec_interrupt;
2165     cc->dump_state = arm_cpu_dump_state;
2166     cc->set_pc = arm_cpu_set_pc;
2167     cc->synchronize_from_tb = arm_cpu_synchronize_from_tb;
2168     cc->gdb_read_register = arm_cpu_gdb_read_register;
2169     cc->gdb_write_register = arm_cpu_gdb_write_register;
2170 #ifdef CONFIG_USER_ONLY
2171     cc->handle_mmu_fault = arm_cpu_handle_mmu_fault;
2172 #else
2173     cc->do_interrupt = arm_cpu_do_interrupt;
2174     cc->do_unaligned_access = arm_cpu_do_unaligned_access;
2175     cc->do_transaction_failed = arm_cpu_do_transaction_failed;
2176     cc->get_phys_page_attrs_debug = arm_cpu_get_phys_page_attrs_debug;
2177     cc->asidx_from_attrs = arm_asidx_from_attrs;
2178     cc->vmsd = &vmstate_arm_cpu;
2179     cc->virtio_is_big_endian = arm_cpu_virtio_is_big_endian;
2180     cc->write_elf64_note = arm_cpu_write_elf64_note;
2181     cc->write_elf32_note = arm_cpu_write_elf32_note;
2182 #endif
2183     cc->gdb_num_core_regs = 26;
2184     cc->gdb_core_xml_file = "arm-core.xml";
2185     cc->gdb_arch_name = arm_gdb_arch_name;
2186     cc->gdb_get_dynamic_xml = arm_gdb_get_dynamic_xml;
2187     cc->gdb_stop_before_watchpoint = true;
2188     cc->debug_excp_handler = arm_debug_excp_handler;
2189     cc->debug_check_watchpoint = arm_debug_check_watchpoint;
2190 #if !defined(CONFIG_USER_ONLY)
2191     cc->adjust_watchpoint_address = arm_adjust_watchpoint_address;
2192 #endif
2193 
2194     cc->disas_set_info = arm_disas_set_info;
2195 #ifdef CONFIG_TCG
2196     cc->tcg_initialize = arm_translate_init;
2197 #endif
2198 }
2199 
2200 #ifdef CONFIG_KVM
2201 static void arm_host_initfn(Object *obj)
2202 {
2203     ARMCPU *cpu = ARM_CPU(obj);
2204 
2205     kvm_arm_set_cpu_features_from_host(cpu);
2206     arm_cpu_post_init(obj);
2207 }
2208 
2209 static const TypeInfo host_arm_cpu_type_info = {
2210     .name = TYPE_ARM_HOST_CPU,
2211 #ifdef TARGET_AARCH64
2212     .parent = TYPE_AARCH64_CPU,
2213 #else
2214     .parent = TYPE_ARM_CPU,
2215 #endif
2216     .instance_init = arm_host_initfn,
2217 };
2218 
2219 #endif
2220 
2221 static void arm_cpu_instance_init(Object *obj)
2222 {
2223     ARMCPUClass *acc = ARM_CPU_GET_CLASS(obj);
2224 
2225     acc->info->initfn(obj);
2226     arm_cpu_post_init(obj);
2227 }
2228 
2229 static void cpu_register_class_init(ObjectClass *oc, void *data)
2230 {
2231     ARMCPUClass *acc = ARM_CPU_CLASS(oc);
2232 
2233     acc->info = data;
2234 }
2235 
2236 static void cpu_register(const ARMCPUInfo *info)
2237 {
2238     TypeInfo type_info = {
2239         .parent = TYPE_ARM_CPU,
2240         .instance_size = sizeof(ARMCPU),
2241         .instance_init = arm_cpu_instance_init,
2242         .class_size = sizeof(ARMCPUClass),
2243         .class_init = info->class_init ?: cpu_register_class_init,
2244         .class_data = (void *)info,
2245     };
2246 
2247     type_info.name = g_strdup_printf("%s-" TYPE_ARM_CPU, info->name);
2248     type_register(&type_info);
2249     g_free((void *)type_info.name);
2250 }
2251 
2252 static const TypeInfo arm_cpu_type_info = {
2253     .name = TYPE_ARM_CPU,
2254     .parent = TYPE_CPU,
2255     .instance_size = sizeof(ARMCPU),
2256     .instance_init = arm_cpu_initfn,
2257     .instance_finalize = arm_cpu_finalizefn,
2258     .abstract = true,
2259     .class_size = sizeof(ARMCPUClass),
2260     .class_init = arm_cpu_class_init,
2261 };
2262 
2263 static const TypeInfo idau_interface_type_info = {
2264     .name = TYPE_IDAU_INTERFACE,
2265     .parent = TYPE_INTERFACE,
2266     .class_size = sizeof(IDAUInterfaceClass),
2267 };
2268 
2269 static void arm_cpu_register_types(void)
2270 {
2271     const ARMCPUInfo *info = arm_cpus;
2272 
2273     type_register_static(&arm_cpu_type_info);
2274     type_register_static(&idau_interface_type_info);
2275 
2276     while (info->name) {
2277         cpu_register(info);
2278         info++;
2279     }
2280 
2281 #ifdef CONFIG_KVM
2282     type_register_static(&host_arm_cpu_type_info);
2283 #endif
2284 }
2285 
2286 type_init(arm_cpu_register_types)
2287