1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "qemu/osdep.h"
22#include "qemu/qemu-print.h"
23#include "qemu-common.h"
24#include "target/arm/idau.h"
25#include "qemu/module.h"
26#include "qapi/error.h"
27#include "qapi/visitor.h"
28#include "cpu.h"
29#ifdef CONFIG_TCG
30#include "hw/core/tcg-cpu-ops.h"
31#endif
32#include "internals.h"
33#include "exec/exec-all.h"
34#include "hw/qdev-properties.h"
35#if !defined(CONFIG_USER_ONLY)
36#include "hw/loader.h"
37#include "hw/boards.h"
38#endif
39#include "sysemu/runstate.h"
40#include "sysemu/tcg.h"
41#include "sysemu/hw_accel.h"
42#include "kvm_arm.h"
43#include "disas/capstone.h"
44#include "fpu/softfloat.h"
45
46#include "hw/core/cpu-exec-gpio.h"
47#include "hw/fdt_generic_util.h"
48
49#if !defined(CONFIG_USER_ONLY)
50static void arm_cpu_set_irq(void *opaque, int irq, int level);
51#endif
52
53static void arm_cpu_set_pc(CPUState *cs, vaddr value)
54{
55 ARMCPU *cpu = ARM_CPU(cs);
56 CPUARMState *env = &cpu->env;
57
58 if (is_a64(env)) {
59 env->pc = value;
60 env->thumb = 0;
61 } else {
62 env->regs[15] = value & ~1;
63 env->thumb = value & 1;
64 }
65}
66
67static vaddr arm_cpu_get_pc(CPUState *cs)
68{
69 ARMCPU *cpu = ARM_CPU(cs);
70 CPUARMState *env = &cpu->env;
71
72 return is_a64(env) ? env->pc : env->regs[15];
73}
74
75#ifdef CONFIG_TCG
76void arm_cpu_synchronize_from_tb(CPUState *cs,
77 const TranslationBlock *tb)
78{
79 ARMCPU *cpu = ARM_CPU(cs);
80 CPUARMState *env = &cpu->env;
81
82
83
84
85
86 if (is_a64(env)) {
87 env->pc = tb->pc;
88 } else {
89 env->regs[15] = tb->pc;
90 }
91}
92#endif
93
94enum {
95 ARM_DEBUG_CURRENT_EL,
96 ARM_DEBUG_PHYS
97};
98
99static const char *arm_debug_ctx[] = {
100 [ARM_DEBUG_CURRENT_EL] = "current-el",
101 [ARM_DEBUG_PHYS] = "phys",
102 NULL
103};
104
105static bool arm_cpu_has_work(CPUState *cs)
106{
107 ARMCPU *cpu = ARM_CPU(cs);
108
109 return (cpu->power_state != PSCI_OFF)
110 && cs->interrupt_request &
111 (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD
112 | CPU_INTERRUPT_VFIQ | CPU_INTERRUPT_VIRQ
113 | CPU_INTERRUPT_EXITTB);
114}
115
116void arm_register_pre_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook,
117 void *opaque)
118{
119 ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1);
120
121 entry->hook = hook;
122 entry->opaque = opaque;
123
124 QLIST_INSERT_HEAD(&cpu->pre_el_change_hooks, entry, node);
125}
126
127void arm_register_el_change_hook(ARMCPU *cpu, ARMELChangeHookFn *hook,
128 void *opaque)
129{
130 ARMELChangeHook *entry = g_new0(ARMELChangeHook, 1);
131
132 entry->hook = hook;
133 entry->opaque = opaque;
134
135 QLIST_INSERT_HEAD(&cpu->el_change_hooks, entry, node);
136}
137
138static void cp_reg_reset(gpointer key, gpointer value, gpointer opaque)
139{
140
141 ARMCPRegInfo *ri = value;
142 ARMCPU *cpu = opaque;
143
144 if (ri->type & (ARM_CP_SPECIAL | ARM_CP_ALIAS)) {
145 return;
146 }
147
148 if (ri->resetfn) {
149 ri->resetfn(&cpu->env, ri);
150 return;
151 }
152
153
154
155
156
157
158 if (!ri->fieldoffset) {
159 return;
160 }
161
162 if (cpreg_field_is_64bit(ri)) {
163 CPREG_FIELD64(&cpu->env, ri) = ri->resetvalue;
164 } else {
165 CPREG_FIELD32(&cpu->env, ri) = ri->resetvalue;
166 }
167}
168
169static void cp_reg_check_reset(gpointer key, gpointer value, gpointer opaque)
170{
171
172
173
174
175
176 ARMCPRegInfo *ri = value;
177 ARMCPU *cpu = opaque;
178 uint64_t oldvalue, newvalue;
179
180 if (ri->type & (ARM_CP_SPECIAL | ARM_CP_ALIAS | ARM_CP_NO_RAW)) {
181 return;
182 }
183
184 oldvalue = read_raw_cp_reg(&cpu->env, ri);
185 cp_reg_reset(key, value, opaque);
186 newvalue = read_raw_cp_reg(&cpu->env, ri);
187 assert(oldvalue == newvalue);
188}
189
190static void arm_cpu_reset(DeviceState *dev)
191{
192 CPUState *s = CPU(dev);
193 ARMCPU *cpu = ARM_CPU(s);
194 ARMCPUClass *acc = ARM_CPU_GET_CLASS(cpu);
195 CPUARMState *env = &cpu->env;
196#ifndef CONFIG_USER_ONLY
197 CPUClass *cc = CPU_GET_CLASS(s);
198 vaddr old_pc = is_a64(&cpu->env) ? cpu->env.pc : cpu->env.regs[15];
199 int i;
200#endif
201
202 acc->parent_reset(dev);
203
204 memset(env, 0, offsetof(CPUARMState, end_reset_fields));
205
206 g_hash_table_foreach(cpu->cp_regs, cp_reg_reset, cpu);
207 g_hash_table_foreach(cpu->cp_regs, cp_reg_check_reset, cpu);
208
209 env->vfp.xregs[ARM_VFP_FPSID] = cpu->reset_fpsid;
210 env->vfp.xregs[ARM_VFP_MVFR0] = cpu->isar.mvfr0;
211 env->vfp.xregs[ARM_VFP_MVFR1] = cpu->isar.mvfr1;
212 env->vfp.xregs[ARM_VFP_MVFR2] = cpu->isar.mvfr2;
213
214 cpu->power_state = s->start_powered_off || s->halt_pin || s->arch_halt_pin ?
215 PSCI_OFF : PSCI_ON;
216
217
218 env->cp15.sctlr_ns &= ~SCTLR_V;
219 env->cp15.sctlr_s &= ~SCTLR_V;
220 env->cp15.sctlr_ns |= env->vinithi ? SCTLR_V : 0;
221 env->cp15.sctlr_s |= env->vinithi ? SCTLR_V : 0;
222
223 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
224 env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
225 }
226
227 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
228
229 env->aarch64 = 1;
230#if defined(CONFIG_USER_ONLY)
231 env->pstate = PSTATE_MODE_EL0t;
232
233 env->cp15.sctlr_el[1] |= SCTLR_UCT | SCTLR_UCI | SCTLR_DZE;
234
235 env->cp15.sctlr_el[1] |= (SCTLR_EnIA | SCTLR_EnIB |
236 SCTLR_EnDA | SCTLR_EnDB);
237
238 env->cp15.cpacr_el1 = deposit64(env->cp15.cpacr_el1, 20, 2, 3);
239
240 env->cp15.cpacr_el1 = deposit64(env->cp15.cpacr_el1, 16, 2, 3);
241
242 if (cpu_isar_feature(aa64_sve, cpu)) {
243 env->vfp.zcr_el[1] =
244 aarch64_sve_zcr_get_valid_len(cpu, cpu->sve_default_vq - 1);
245 }
246
247
248
249
250 env->cp15.tcr_el[1].raw_tcr = (1ULL << 37);
251
252
253 if (cpu_isar_feature(aa64_mte, cpu)) {
254
255 env->cp15.sctlr_el[1] |= SCTLR_ATA0;
256
257
258
259
260
261
262
263
264 env->cp15.gcr_el1 = 0x1ffff;
265 }
266#else
267
268 if (arm_feature(env, ARM_FEATURE_EL3)) {
269 env->pstate = PSTATE_MODE_EL3h;
270 } else if (arm_feature(env, ARM_FEATURE_EL2)) {
271 env->pstate = PSTATE_MODE_EL2h;
272 } else {
273 env->pstate = PSTATE_MODE_EL1h;
274 }
275#endif
276 } else {
277#if defined(CONFIG_USER_ONLY)
278
279 env->cp15.cpacr_el1 = deposit64(env->cp15.cpacr_el1, 20, 4, 0xf);
280#endif
281 }
282
283#ifndef CONFIG_USER_ONLY
284 if (arm_feature(env, ARM_FEATURE_V8)) {
285 cc->set_pc(s, cpu->rvbar);
286 }
287#endif
288
289#if defined(CONFIG_USER_ONLY)
290 env->uncached_cpsr = ARM_CPU_MODE_USR;
291
292 env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
293 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
294 env->cp15.c15_cpar = 3;
295 } else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
296 env->cp15.c15_cpar = 1;
297 }
298#else
299
300
301
302
303
304
305 if (arm_feature(env, ARM_FEATURE_EL2) &&
306 !arm_feature(env, ARM_FEATURE_EL3)) {
307 env->uncached_cpsr = ARM_CPU_MODE_HYP;
308 } else {
309 env->uncached_cpsr = ARM_CPU_MODE_SVC;
310 }
311 env->daif = PSTATE_D | PSTATE_A | PSTATE_I | PSTATE_F;
312
313 if (arm_feature(env, ARM_FEATURE_M)) {
314 uint32_t initial_msp;
315 uint32_t initial_pc;
316 uint8_t *rom;
317 uint32_t vecbase;
318
319 if (cpu_isar_feature(aa32_lob, cpu)) {
320
321
322
323
324
325 env->v7m.ltpsize = 4;
326
327 env->v7m.fpdscr[M_REG_NS] = 4 << FPCR_LTPSIZE_SHIFT;
328 env->v7m.fpdscr[M_REG_S] = 4 << FPCR_LTPSIZE_SHIFT;
329 }
330
331 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
332 env->v7m.secure = true;
333 } else {
334
335
336
337
338
339 env->v7m.aircr = R_V7M_AIRCR_BFHFNMINS_MASK;
340
341
342
343
344
345
346
347 env->v7m.nsacr = 0xcff;
348 }
349
350
351
352
353
354 env->v7m.ccr[M_REG_NS] = R_V7M_CCR_STKALIGN_MASK;
355 env->v7m.ccr[M_REG_S] = R_V7M_CCR_STKALIGN_MASK;
356 if (arm_feature(env, ARM_FEATURE_V8)) {
357
358 env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_NONBASETHRDENA_MASK;
359 env->v7m.ccr[M_REG_S] |= R_V7M_CCR_NONBASETHRDENA_MASK;
360 }
361 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
362 env->v7m.ccr[M_REG_NS] |= R_V7M_CCR_UNALIGN_TRP_MASK;
363 env->v7m.ccr[M_REG_S] |= R_V7M_CCR_UNALIGN_TRP_MASK;
364 }
365
366 if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
367 env->v7m.fpccr[M_REG_NS] = R_V7M_FPCCR_ASPEN_MASK;
368 env->v7m.fpccr[M_REG_S] = R_V7M_FPCCR_ASPEN_MASK |
369 R_V7M_FPCCR_LSPEN_MASK | R_V7M_FPCCR_S_MASK;
370 }
371
372 env->regs[14] = 0xffffffff;
373
374 env->v7m.vecbase[M_REG_S] = cpu->init_svtor & 0xffffff80;
375 env->v7m.vecbase[M_REG_NS] = cpu->init_nsvtor & 0xffffff80;
376
377
378 vecbase = env->v7m.vecbase[env->v7m.secure];
379 rom = rom_ptr_for_as(s->as, vecbase, 8);
380 if (rom) {
381
382
383
384 initial_msp = ldl_p(rom);
385 initial_pc = ldl_p(rom + 4);
386 } else {
387
388
389
390
391
392 initial_msp = ldl_phys(s->as, vecbase);
393 initial_pc = ldl_phys(s->as, vecbase + 4);
394 }
395
396 env->regs[13] = initial_msp & 0xFFFFFFFC;
397 env->regs[15] = initial_pc & ~1;
398 env->thumb = initial_pc & 1;
399 }
400
401
402
403
404
405 if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
406 env->regs[15] = 0xFFFF0000;
407 }
408
409
410
411
412
413 arm_clear_exclusive(env);
414
415 env->vfp.xregs[ARM_VFP_FPEXC] = 0;
416#endif
417
418 if (arm_feature(env, ARM_FEATURE_PMSA)) {
419 if (cpu->pmsav7_dregion > 0) {
420 if (arm_feature(env, ARM_FEATURE_V8)) {
421 memset(env->pmsav8.rbar[M_REG_NS], 0,
422 sizeof(*env->pmsav8.rbar[M_REG_NS])
423 * cpu->pmsav7_dregion);
424 memset(env->pmsav8.rlar[M_REG_NS], 0,
425 sizeof(*env->pmsav8.rlar[M_REG_NS])
426 * cpu->pmsav7_dregion);
427 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
428 memset(env->pmsav8.rbar[M_REG_S], 0,
429 sizeof(*env->pmsav8.rbar[M_REG_S])
430 * cpu->pmsav7_dregion);
431 memset(env->pmsav8.rlar[M_REG_S], 0,
432 sizeof(*env->pmsav8.rlar[M_REG_S])
433 * cpu->pmsav7_dregion);
434 }
435 } else if (arm_feature(env, ARM_FEATURE_V7)) {
436 memset(env->pmsav7.drbar, 0,
437 sizeof(*env->pmsav7.drbar) * cpu->pmsav7_dregion);
438 memset(env->pmsav7.drsr, 0,
439 sizeof(*env->pmsav7.drsr) * cpu->pmsav7_dregion);
440 memset(env->pmsav7.dracr, 0,
441 sizeof(*env->pmsav7.dracr) * cpu->pmsav7_dregion);
442 }
443 }
444 env->pmsav7.rnr[M_REG_NS] = 0;
445 env->pmsav7.rnr[M_REG_S] = 0;
446 env->pmsav8.mair0[M_REG_NS] = 0;
447 env->pmsav8.mair0[M_REG_S] = 0;
448 env->pmsav8.mair1[M_REG_NS] = 0;
449 env->pmsav8.mair1[M_REG_S] = 0;
450 }
451
452 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
453 if (cpu->sau_sregion > 0) {
454 memset(env->sau.rbar, 0, sizeof(*env->sau.rbar) * cpu->sau_sregion);
455 memset(env->sau.rlar, 0, sizeof(*env->sau.rlar) * cpu->sau_sregion);
456 }
457 env->sau.rnr = 0;
458
459
460
461 env->sau.ctrl = 0;
462 }
463
464 set_flush_to_zero(1, &env->vfp.standard_fp_status);
465 set_flush_inputs_to_zero(1, &env->vfp.standard_fp_status);
466 set_default_nan_mode(1, &env->vfp.standard_fp_status);
467 set_default_nan_mode(1, &env->vfp.standard_fp_status_f16);
468 set_float_detect_tininess(float_tininess_before_rounding,
469 &env->vfp.fp_status);
470 set_float_detect_tininess(float_tininess_before_rounding,
471 &env->vfp.standard_fp_status);
472 set_float_detect_tininess(float_tininess_before_rounding,
473 &env->vfp.fp_status_f16);
474 set_float_detect_tininess(float_tininess_before_rounding,
475 &env->vfp.standard_fp_status_f16);
476#ifndef CONFIG_USER_ONLY
477 if (kvm_enabled()) {
478 kvm_arm_reset_vcpu(cpu);
479 }
480
481 if (!runstate_is_running() && !arm_feature(env, ARM_FEATURE_M)) {
482
483 cc->set_pc(s, old_pc);
484 }
485#endif
486
487 cpu->is_in_wfi = false;
488 qemu_set_irq(cpu->wfi, cpu->is_in_wfi);
489
490 hw_breakpoint_update_all(cpu);
491 hw_watchpoint_update_all(cpu);
492
493#ifndef CONFIG_USER_ONLY
494 if (cpu->env.memattr_ns) {
495 env_tlb(&cpu->env)->memattr[MEM_ATTR_NS].attrs = *cpu->env.memattr_ns;
496 }
497
498 if (cpu->env.memattr_s) {
499 env_tlb(&cpu->env)->memattr[MEM_ATTR_SEC].attrs = *cpu->env.memattr_s;
500 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
501
502 env_tlb(&cpu->env)->memattr[MEM_ATTR_SEC].attrs.secure = true;
503 }
504
505 for (i = 0; i < ARRAY_SIZE(cpu->env.irq_wires); i++) {
506 if (!arm_feature(env, ARM_FEATURE_EL2) && i >= ARM_CPU_VIRQ) {
507 break;
508 }
509 arm_cpu_set_irq(cpu, i, cpu->env.irq_wires[i]);
510 }
511#endif
512
513 arm_rebuild_hflags(env);
514}
515
516static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx,
517 unsigned int target_el,
518 unsigned int cur_el, bool secure,
519 uint64_t hcr_el2)
520{
521 CPUARMState *env = cs->env_ptr;
522 bool pstate_unmasked;
523 bool unmasked = false;
524
525
526
527
528
529
530 if (cur_el > target_el) {
531 return false;
532 }
533
534 switch (excp_idx) {
535 case EXCP_FIQ:
536 pstate_unmasked = !(env->daif & PSTATE_F);
537 break;
538
539 case EXCP_IRQ:
540 pstate_unmasked = !(env->daif & PSTATE_I);
541 break;
542
543 case EXCP_VFIQ:
544 if (!(hcr_el2 & HCR_FMO) || (hcr_el2 & HCR_TGE)) {
545
546 return false;
547 }
548 return !(env->daif & PSTATE_F);
549 case EXCP_VIRQ:
550 if (!(hcr_el2 & HCR_IMO) || (hcr_el2 & HCR_TGE)) {
551
552 return false;
553 }
554 return !(env->daif & PSTATE_I);
555 default:
556 g_assert_not_reached();
557 }
558
559
560
561
562
563
564 if ((target_el > cur_el) && (target_el != 1)) {
565
566 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
567
568
569
570
571
572
573 if (target_el == 3 || !secure || (env->cp15.scr_el3 & SCR_EEL2)) {
574 unmasked = true;
575 }
576 } else {
577
578
579
580
581
582 bool hcr, scr;
583
584 switch (excp_idx) {
585 case EXCP_FIQ:
586
587
588
589
590
591
592
593 hcr = hcr_el2 & HCR_FMO;
594 scr = (env->cp15.scr_el3 & SCR_FIQ);
595
596
597
598
599
600
601
602 scr = scr && !((env->cp15.scr_el3 & SCR_FW) && !hcr);
603 break;
604 case EXCP_IRQ:
605
606
607
608
609
610
611
612 hcr = hcr_el2 & HCR_IMO;
613 scr = false;
614 break;
615 default:
616 g_assert_not_reached();
617 }
618
619 if ((scr || hcr) && !secure) {
620 unmasked = true;
621 }
622 }
623 }
624
625
626
627
628
629 return unmasked || pstate_unmasked;
630}
631
632bool arm_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
633{
634 CPUClass *cc = CPU_GET_CLASS(cs);
635 CPUARMState *env = cs->env_ptr;
636 uint32_t cur_el = arm_current_el(env);
637 bool secure = arm_is_secure(env);
638 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
639 uint32_t target_el;
640 uint32_t excp_idx;
641
642
643
644 if (interrupt_request & CPU_INTERRUPT_FIQ) {
645 excp_idx = EXCP_FIQ;
646 target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
647 if (arm_excp_unmasked(cs, excp_idx, target_el,
648 cur_el, secure, hcr_el2)) {
649 goto found;
650 }
651 }
652 if (interrupt_request & CPU_INTERRUPT_HARD) {
653 excp_idx = EXCP_IRQ;
654 target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
655 if (arm_excp_unmasked(cs, excp_idx, target_el,
656 cur_el, secure, hcr_el2)) {
657 goto found;
658 }
659 }
660 if (interrupt_request & CPU_INTERRUPT_VIRQ) {
661 excp_idx = EXCP_VIRQ;
662 target_el = 1;
663 if (arm_excp_unmasked(cs, excp_idx, target_el,
664 cur_el, secure, hcr_el2)) {
665 goto found;
666 }
667 }
668 if (interrupt_request & CPU_INTERRUPT_VFIQ) {
669 excp_idx = EXCP_VFIQ;
670 target_el = 1;
671 if (arm_excp_unmasked(cs, excp_idx, target_el,
672 cur_el, secure, hcr_el2)) {
673 goto found;
674 }
675 }
676 return false;
677
678 found:
679 cs->exception_index = excp_idx;
680 env->exception.target_el = target_el;
681 cc->tcg_ops->do_interrupt(cs);
682 return true;
683}
684
685void arm_cpu_update_virq(ARMCPU *cpu)
686{
687
688
689
690
691 CPUARMState *env = &cpu->env;
692 CPUState *cs = CPU(cpu);
693
694 bool new_state = (env->cp15.hcr_el2 & HCR_VI) ||
695 (env->irq_line_state & CPU_INTERRUPT_VIRQ);
696
697 if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VIRQ) != 0)) {
698 if (new_state) {
699 cpu_interrupt(cs, CPU_INTERRUPT_VIRQ);
700 } else {
701 cpu_reset_interrupt(cs, CPU_INTERRUPT_VIRQ);
702 }
703 }
704}
705
706void arm_cpu_update_vfiq(ARMCPU *cpu)
707{
708
709
710
711
712 CPUARMState *env = &cpu->env;
713 CPUState *cs = CPU(cpu);
714
715 bool new_state = (env->cp15.hcr_el2 & HCR_VF) ||
716 (env->irq_line_state & CPU_INTERRUPT_VFIQ);
717
718 if (new_state != ((cs->interrupt_request & CPU_INTERRUPT_VFIQ) != 0)) {
719 if (new_state) {
720 cpu_interrupt(cs, CPU_INTERRUPT_VFIQ);
721 } else {
722 cpu_reset_interrupt(cs, CPU_INTERRUPT_VFIQ);
723 }
724 }
725}
726
727#ifndef CONFIG_USER_ONLY
728static void arm_cpu_set_irq(void *opaque, int irq, int level)
729{
730 ARMCPU *cpu = opaque;
731 CPUARMState *env = &cpu->env;
732 CPUState *cs = CPU(cpu);
733 static const int mask[] = {
734 [ARM_CPU_IRQ] = CPU_INTERRUPT_HARD,
735 [ARM_CPU_FIQ] = CPU_INTERRUPT_FIQ,
736 [ARM_CPU_VIRQ] = CPU_INTERRUPT_VIRQ,
737 [ARM_CPU_VFIQ] = CPU_INTERRUPT_VFIQ
738 };
739
740 env->irq_wires[irq] = level;
741
742 if (level) {
743 env->irq_line_state |= mask[irq];
744 } else {
745 env->irq_line_state &= ~mask[irq];
746 }
747
748 switch (irq) {
749 case ARM_CPU_VIRQ:
750 assert(arm_feature(env, ARM_FEATURE_EL2));
751 arm_cpu_update_virq(cpu);
752 break;
753 case ARM_CPU_VFIQ:
754 assert(arm_feature(env, ARM_FEATURE_EL2));
755 arm_cpu_update_vfiq(cpu);
756 break;
757 case ARM_CPU_IRQ:
758 case ARM_CPU_FIQ:
759 if (level) {
760 cpu_interrupt(cs, mask[irq]);
761 } else {
762 cpu_reset_interrupt(cs, mask[irq]);
763 }
764 break;
765 default:
766 g_assert_not_reached();
767 }
768}
769
770static void arm_cpu_kvm_set_irq(void *opaque, int irq, int level)
771{
772#ifdef CONFIG_KVM
773 ARMCPU *cpu = opaque;
774 CPUARMState *env = &cpu->env;
775 CPUState *cs = CPU(cpu);
776 uint32_t linestate_bit;
777 int irq_id;
778
779 switch (irq) {
780 case ARM_CPU_IRQ:
781 irq_id = KVM_ARM_IRQ_CPU_IRQ;
782 linestate_bit = CPU_INTERRUPT_HARD;
783 break;
784 case ARM_CPU_FIQ:
785 irq_id = KVM_ARM_IRQ_CPU_FIQ;
786 linestate_bit = CPU_INTERRUPT_FIQ;
787 break;
788 default:
789 g_assert_not_reached();
790 }
791
792 if (level) {
793 env->irq_line_state |= linestate_bit;
794 } else {
795 env->irq_line_state &= ~linestate_bit;
796 }
797 kvm_arm_set_irq(cs->cpu_index, KVM_ARM_IRQ_TYPE_CPU, irq_id, !!level);
798#endif
799}
800
801static bool arm_cpu_virtio_is_big_endian(CPUState *cs)
802{
803 ARMCPU *cpu = ARM_CPU(cs);
804 CPUARMState *env = &cpu->env;
805
806 cpu_synchronize_state(cs);
807 return arm_cpu_data_is_big_endian(env);
808}
809
810#endif
811
812#ifndef CONFIG_USER_ONLY
813static void arm_cpu_set_ncpuhalt(void *opaque, int irq, int level)
814{
815 CPUState *cs = opaque;
816 ARMCPU *cpu = ARM_CPU(cs);
817 int old_value = cs->arch_halt_pin;
818
819
820
821
822
823
824
825#if 0
826 if (!cs->reset_pin) {
827 return;
828 }
829#endif
830 cs->arch_halt_pin = level;
831
832
833
834 cpu->power_state = level ? PSCI_OFF : PSCI_ON;
835 cpu_halt_update(cs);
836
837 if (cs->arch_halt_pin != old_value && !cs->arch_halt_pin) {
838 cpu_interrupt(cs, CPU_INTERRUPT_EXITTB);
839 }
840}
841
842static void arm_cpu_set_vinithi(void *opaque, int irq, int level)
843{
844 CPUState *cs = opaque;
845 ARMCPU *cpu = ARM_CPU(cs);
846 CPUClass *cc = CPU_GET_CLASS(cs);
847
848 cpu->env.vinithi = level;
849 if (cs->arch_halt_pin) {
850 cc->set_pc(cs, level ? 0xFFFF0000: 0x0);
851 }
852}
853#endif
854
855static int
856print_insn_thumb1(bfd_vma pc, disassemble_info *info)
857{
858 return print_insn_arm(pc | 1, info);
859}
860
861static void arm_disas_set_info(CPUState *cpu, disassemble_info *info)
862{
863 ARMCPU *ac = ARM_CPU(cpu);
864 CPUARMState *env = &ac->env;
865 bool sctlr_b;
866
867 if (is_a64(env)) {
868
869
870
871
872#if defined(CONFIG_ARM_A64_DIS)
873 info->print_insn = print_insn_arm_a64;
874#endif
875 info->cap_arch = CS_ARCH_ARM64;
876 info->cap_insn_unit = 4;
877 info->cap_insn_split = 4;
878 } else {
879 int cap_mode;
880 if (env->thumb) {
881 info->print_insn = print_insn_thumb1;
882 info->cap_insn_unit = 2;
883 info->cap_insn_split = 4;
884 cap_mode = CS_MODE_THUMB;
885 } else {
886 info->print_insn = print_insn_arm;
887 info->cap_insn_unit = 4;
888 info->cap_insn_split = 4;
889 cap_mode = CS_MODE_ARM;
890 }
891 if (arm_feature(env, ARM_FEATURE_V8)) {
892 cap_mode |= CS_MODE_V8;
893 }
894 if (arm_feature(env, ARM_FEATURE_M)) {
895 cap_mode |= CS_MODE_MCLASS;
896 }
897 info->cap_arch = CS_ARCH_ARM;
898 info->cap_mode = cap_mode;
899 }
900
901 sctlr_b = arm_sctlr_b(env);
902 if (bswap_code(sctlr_b)) {
903#ifdef TARGET_WORDS_BIGENDIAN
904 info->endian = BFD_ENDIAN_LITTLE;
905#else
906 info->endian = BFD_ENDIAN_BIG;
907#endif
908 }
909 info->flags &= ~INSN_ARM_BE32;
910#ifndef CONFIG_USER_ONLY
911 if (sctlr_b) {
912 info->flags |= INSN_ARM_BE32;
913 }
914#endif
915}
916
917#ifdef TARGET_AARCH64
918
919static void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags)
920{
921 ARMCPU *cpu = ARM_CPU(cs);
922 CPUARMState *env = &cpu->env;
923 uint32_t psr = pstate_read(env);
924 int i;
925 int el = arm_current_el(env);
926 const char *ns_status;
927
928 qemu_fprintf(f, " PC=%016" PRIx64 " ", env->pc);
929 for (i = 0; i < 32; i++) {
930 if (i == 31) {
931 qemu_fprintf(f, " SP=%016" PRIx64 "\n", env->xregs[i]);
932 } else {
933 qemu_fprintf(f, "X%02d=%016" PRIx64 "%s", i, env->xregs[i],
934 (i + 2) % 3 ? " " : "\n");
935 }
936 }
937
938 if (arm_feature(env, ARM_FEATURE_EL3) && el != 3) {
939 ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
940 } else {
941 ns_status = "";
942 }
943 qemu_fprintf(f, "PSTATE=%08x %c%c%c%c %sEL%d%c",
944 psr,
945 psr & PSTATE_N ? 'N' : '-',
946 psr & PSTATE_Z ? 'Z' : '-',
947 psr & PSTATE_C ? 'C' : '-',
948 psr & PSTATE_V ? 'V' : '-',
949 ns_status,
950 el,
951 psr & PSTATE_SP ? 'h' : 't');
952
953 if (cpu_isar_feature(aa64_bti, cpu)) {
954 qemu_fprintf(f, " BTYPE=%d", (psr & PSTATE_BTYPE) >> 10);
955 }
956 if (!(flags & CPU_DUMP_FPU)) {
957 qemu_fprintf(f, "\n");
958 return;
959 }
960 if (fp_exception_el(env, el) != 0) {
961 qemu_fprintf(f, " FPU disabled\n");
962 return;
963 }
964 qemu_fprintf(f, " FPCR=%08x FPSR=%08x\n",
965 vfp_get_fpcr(env), vfp_get_fpsr(env));
966
967 if (cpu_isar_feature(aa64_sve, cpu) && sve_exception_el(env, el) == 0) {
968 int j, zcr_len = sve_zcr_len_for_el(env, el);
969
970 for (i = 0; i <= FFR_PRED_NUM; i++) {
971 bool eol;
972 if (i == FFR_PRED_NUM) {
973 qemu_fprintf(f, "FFR=");
974
975 eol = true;
976 } else {
977 qemu_fprintf(f, "P%02d=", i);
978 switch (zcr_len) {
979 case 0:
980 eol = i % 8 == 7;
981 break;
982 case 1:
983 eol = i % 6 == 5;
984 break;
985 case 2:
986 case 3:
987 eol = i % 3 == 2;
988 break;
989 default:
990
991 eol = true;
992 break;
993 }
994 }
995 for (j = zcr_len / 4; j >= 0; j--) {
996 int digits;
997 if (j * 4 + 4 <= zcr_len + 1) {
998 digits = 16;
999 } else {
1000 digits = (zcr_len % 4 + 1) * 4;
1001 }
1002 qemu_fprintf(f, "%0*" PRIx64 "%s", digits,
1003 env->vfp.pregs[i].p[j],
1004 j ? ":" : eol ? "\n" : " ");
1005 }
1006 }
1007
1008 for (i = 0; i < 32; i++) {
1009 if (zcr_len == 0) {
1010 qemu_fprintf(f, "Z%02d=%016" PRIx64 ":%016" PRIx64 "%s",
1011 i, env->vfp.zregs[i].d[1],
1012 env->vfp.zregs[i].d[0], i & 1 ? "\n" : " ");
1013 } else if (zcr_len == 1) {
1014 qemu_fprintf(f, "Z%02d=%016" PRIx64 ":%016" PRIx64
1015 ":%016" PRIx64 ":%016" PRIx64 "\n",
1016 i, env->vfp.zregs[i].d[3], env->vfp.zregs[i].d[2],
1017 env->vfp.zregs[i].d[1], env->vfp.zregs[i].d[0]);
1018 } else {
1019 for (j = zcr_len; j >= 0; j--) {
1020 bool odd = (zcr_len - j) % 2 != 0;
1021 if (j == zcr_len) {
1022 qemu_fprintf(f, "Z%02d[%x-%x]=", i, j, j - 1);
1023 } else if (!odd) {
1024 if (j > 0) {
1025 qemu_fprintf(f, " [%x-%x]=", j, j - 1);
1026 } else {
1027 qemu_fprintf(f, " [%x]=", j);
1028 }
1029 }
1030 qemu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%s",
1031 env->vfp.zregs[i].d[j * 2 + 1],
1032 env->vfp.zregs[i].d[j * 2],
1033 odd || j == 0 ? "\n" : ":");
1034 }
1035 }
1036 }
1037 } else {
1038 for (i = 0; i < 32; i++) {
1039 uint64_t *q = aa64_vfp_qreg(env, i);
1040 qemu_fprintf(f, "Q%02d=%016" PRIx64 ":%016" PRIx64 "%s",
1041 i, q[1], q[0], (i & 1 ? "\n" : " "));
1042 }
1043 }
1044}
1045
1046#else
1047
1048static inline void aarch64_cpu_dump_state(CPUState *cs, FILE *f, int flags)
1049{
1050 g_assert_not_reached();
1051}
1052
1053#endif
1054
1055static void arm_cpu_dump_state(CPUState *cs, FILE *f, int flags)
1056{
1057 ARMCPU *cpu = ARM_CPU(cs);
1058 CPUARMState *env = &cpu->env;
1059 int i;
1060
1061 if (is_a64(env)) {
1062 aarch64_cpu_dump_state(cs, f, flags);
1063 return;
1064 }
1065
1066 for (i = 0; i < 16; i++) {
1067 qemu_fprintf(f, "R%02d=%08x", i, env->regs[i]);
1068 if ((i % 4) == 3) {
1069 qemu_fprintf(f, "\n");
1070 } else {
1071 qemu_fprintf(f, " ");
1072 }
1073 }
1074
1075 if (arm_feature(env, ARM_FEATURE_M)) {
1076 uint32_t xpsr = xpsr_read(env);
1077 const char *mode;
1078 const char *ns_status = "";
1079
1080 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
1081 ns_status = env->v7m.secure ? "S " : "NS ";
1082 }
1083
1084 if (xpsr & XPSR_EXCP) {
1085 mode = "handler";
1086 } else {
1087 if (env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_NPRIV_MASK) {
1088 mode = "unpriv-thread";
1089 } else {
1090 mode = "priv-thread";
1091 }
1092 }
1093
1094 qemu_fprintf(f, "XPSR=%08x %c%c%c%c %c %s%s\n",
1095 xpsr,
1096 xpsr & XPSR_N ? 'N' : '-',
1097 xpsr & XPSR_Z ? 'Z' : '-',
1098 xpsr & XPSR_C ? 'C' : '-',
1099 xpsr & XPSR_V ? 'V' : '-',
1100 xpsr & XPSR_T ? 'T' : 'A',
1101 ns_status,
1102 mode);
1103 } else {
1104 uint32_t psr = cpsr_read(env);
1105 const char *ns_status = "";
1106
1107 if (arm_feature(env, ARM_FEATURE_EL3) &&
1108 (psr & CPSR_M) != ARM_CPU_MODE_MON) {
1109 ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
1110 }
1111
1112 qemu_fprintf(f, "PSR=%08x %c%c%c%c %c %s%s%d\n",
1113 psr,
1114 psr & CPSR_N ? 'N' : '-',
1115 psr & CPSR_Z ? 'Z' : '-',
1116 psr & CPSR_C ? 'C' : '-',
1117 psr & CPSR_V ? 'V' : '-',
1118 psr & CPSR_T ? 'T' : 'A',
1119 ns_status,
1120 aarch32_mode_name(psr), (psr & 0x10) ? 32 : 26);
1121 }
1122
1123 if (flags & CPU_DUMP_FPU) {
1124 int numvfpregs = 0;
1125 if (cpu_isar_feature(aa32_simd_r32, cpu)) {
1126 numvfpregs = 32;
1127 } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
1128 numvfpregs = 16;
1129 }
1130 for (i = 0; i < numvfpregs; i++) {
1131 uint64_t v = *aa32_vfp_dreg(env, i);
1132 qemu_fprintf(f, "s%02d=%08x s%02d=%08x d%02d=%016" PRIx64 "\n",
1133 i * 2, (uint32_t)v,
1134 i * 2 + 1, (uint32_t)(v >> 32),
1135 i, v);
1136 }
1137 qemu_fprintf(f, "FPSCR: %08x\n", vfp_get_fpscr(env));
1138 }
1139}
1140
1141uint64_t arm_cpu_mp_affinity(int idx, uint8_t clustersz)
1142{
1143 uint32_t Aff1 = idx / clustersz;
1144 uint32_t Aff0 = idx % clustersz;
1145 return (Aff1 << ARM_AFF1_SHIFT) | Aff0;
1146}
1147
1148static void cpreg_hashtable_data_destroy(gpointer data)
1149{
1150
1151
1152
1153
1154
1155
1156 ARMCPRegInfo *r = data;
1157
1158 g_free((void *)r->name);
1159 g_free(r);
1160}
1161
1162static void arm_cpu_initfn(Object *obj)
1163{
1164 ARMCPU *cpu = ARM_CPU(obj);
1165
1166 cpu_set_cpustate_pointers(cpu);
1167 cpu->cp_regs = g_hash_table_new_full(g_int_hash, g_int_equal,
1168 g_free, cpreg_hashtable_data_destroy);
1169
1170 QLIST_INIT(&cpu->pre_el_change_hooks);
1171 QLIST_INIT(&cpu->el_change_hooks);
1172
1173#ifdef CONFIG_USER_ONLY
1174# ifdef TARGET_AARCH64
1175
1176
1177
1178
1179
1180 cpu->sve_default_vq = 4;
1181# endif
1182#else
1183
1184 if (kvm_enabled()) {
1185
1186
1187
1188 qdev_init_gpio_in(DEVICE(cpu), arm_cpu_kvm_set_irq, 4);
1189 } else {
1190 qdev_init_gpio_in(DEVICE(cpu), arm_cpu_set_irq, 4);
1191 }
1192
1193 qdev_init_gpio_in_named(DEVICE(cpu), arm_cpu_set_ncpuhalt, "ncpuhalt", 1);
1194 qdev_init_gpio_in_named(DEVICE(cpu), arm_cpu_set_vinithi, "vinithi", 1);
1195
1196 qdev_init_gpio_out(DEVICE(cpu), cpu->gt_timer_outputs,
1197 ARRAY_SIZE(cpu->gt_timer_outputs));
1198
1199 qdev_init_gpio_out_named(DEVICE(cpu), &cpu->wfi, "wfi", 1);
1200
1201 qdev_init_gpio_out_named(DEVICE(cpu), &cpu->gicv3_maintenance_interrupt,
1202 "gicv3-maintenance-interrupt", 1);
1203 qdev_init_gpio_out_named(DEVICE(cpu), &cpu->pmu_interrupt,
1204 "pmu-interrupt", 1);
1205#endif
1206
1207
1208
1209
1210
1211 cpu->dtb_compatible = "qemu,unknown";
1212 cpu->psci_version = 1;
1213 cpu->kvm_target = QEMU_KVM_ARM_TARGET_NONE;
1214
1215 if (tcg_enabled()) {
1216 cpu->psci_version = 2;
1217 }
1218
1219#ifndef CONFIG_USER_ONLY
1220 object_property_add_link(obj, "memattr_ns", TYPE_MEMORY_TRANSACTION_ATTR,
1221 (Object **)&cpu->env.memattr_ns,
1222 qdev_prop_allow_set_link_before_realize,
1223 OBJ_PROP_LINK_STRONG);
1224
1225 object_property_add_link(obj, "memattr_s", TYPE_MEMORY_TRANSACTION_ATTR,
1226 (Object **)&cpu->env.memattr_s,
1227 qdev_prop_allow_set_link_before_realize,
1228 OBJ_PROP_LINK_STRONG);
1229#endif
1230}
1231
1232static Property arm_cpu_gt_cntfrq_property =
1233 DEFINE_PROP_UINT64("cntfrq", ARMCPU, gt_cntfrq_hz,
1234 NANOSECONDS_PER_SECOND / GTIMER_SCALE);
1235
1236static Property arm_cpu_gt_cntfrq_property_alias =
1237 DEFINE_PROP_UINT64("generic-timer-frequency", ARMCPU, gt_cntfrq_hz,
1238 NANOSECONDS_PER_SECOND / GTIMER_SCALE);
1239
1240static Property arm_cpu_reset_cbar_property =
1241 DEFINE_PROP_UINT64("reset-cbar", ARMCPU, reset_cbar, 0);
1242
1243static Property arm_cpu_reset_hivecs_property =
1244 DEFINE_PROP_BOOL("reset-hivecs", ARMCPU, reset_hivecs, false);
1245
1246static void arm_cpu_get_rvbar(Object *obj, Visitor *v,
1247 const char *name, void *opaque,
1248 Error **errp)
1249{
1250 ARMCPU *cpu = ARM_CPU(obj);
1251 Error *local_err = NULL;
1252
1253 visit_type_uint64(v, name, &cpu->rvbar, &local_err);
1254 if (local_err) {
1255 error_propagate(errp, local_err);
1256 }
1257}
1258
1259static void arm_cpu_set_rvbar(Object *obj, Visitor *v,
1260 const char *name, void *opaque,
1261 Error **errp)
1262{
1263 ARMCPU *cpu = ARM_CPU(obj);
1264 Error *local_err = NULL;
1265
1266 visit_type_uint64(v, name, &cpu->rvbar, &local_err);
1267 if (local_err) {
1268 error_propagate(errp, local_err);
1269 }
1270}
1271
1272#ifndef CONFIG_USER_ONLY
1273static void arm_cpu_set_memattr_secure(Object *obj, Visitor *v,
1274 const char *name, void *opaque,
1275 Error **errp)
1276{
1277 ARMCPU *cpu = ARM_CPU(obj);
1278 bool secure;
1279 visit_type_bool(v, name, &secure,
1280 errp);
1281 env_tlb(&cpu->env)->memattr[MEM_ATTR_NS].attrs.secure = secure;
1282}
1283#endif
1284
1285#ifndef CONFIG_USER_ONLY
1286static Property arm_cpu_has_el2_property =
1287 DEFINE_PROP_BOOL("has_el2", ARMCPU, has_el2, true);
1288
1289static Property arm_cpu_has_el3_property =
1290 DEFINE_PROP_BOOL("has_el3", ARMCPU, has_el3, true);
1291#endif
1292
1293static Property arm_cpu_cfgend_property =
1294 DEFINE_PROP_BOOL("cfgend", ARMCPU, cfgend, false);
1295
1296static Property arm_cpu_has_vfp_property =
1297 DEFINE_PROP_BOOL("vfp", ARMCPU, has_vfp, true);
1298
1299static Property arm_cpu_has_neon_property =
1300 DEFINE_PROP_BOOL("neon", ARMCPU, has_neon, true);
1301
1302static Property arm_cpu_has_dsp_property =
1303 DEFINE_PROP_BOOL("dsp", ARMCPU, has_dsp, true);
1304
1305static Property arm_cpu_has_mpu_property =
1306 DEFINE_PROP_BOOL("has-mpu", ARMCPU, has_mpu, true);
1307
1308
1309
1310
1311
1312
1313static Property arm_cpu_pmsav7_dregion_property =
1314 DEFINE_PROP_UNSIGNED_NODEFAULT("pmsav7-dregion", ARMCPU,
1315 pmsav7_dregion,
1316 qdev_prop_uint32, uint32_t);
1317
1318static bool arm_get_pmu(Object *obj, Error **errp)
1319{
1320 ARMCPU *cpu = ARM_CPU(obj);
1321
1322 return cpu->has_pmu;
1323}
1324
1325static void arm_set_pmu(Object *obj, bool value, Error **errp)
1326{
1327 ARMCPU *cpu = ARM_CPU(obj);
1328
1329 if (value) {
1330 if (kvm_enabled() && !kvm_arm_pmu_supported()) {
1331 error_setg(errp, "'pmu' feature not supported by KVM on this host");
1332 return;
1333 }
1334 set_feature(&cpu->env, ARM_FEATURE_PMU);
1335 } else {
1336 unset_feature(&cpu->env, ARM_FEATURE_PMU);
1337 }
1338 cpu->has_pmu = value;
1339}
1340
1341unsigned int gt_cntfrq_period_ns(ARMCPU *cpu)
1342{
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361 return NANOSECONDS_PER_SECOND > cpu->gt_cntfrq_hz ?
1362 NANOSECONDS_PER_SECOND / cpu->gt_cntfrq_hz : 1;
1363}
1364
1365void arm_cpu_post_init(Object *obj)
1366{
1367 ARMCPU *cpu = ARM_CPU(obj);
1368
1369
1370
1371
1372
1373 if (arm_feature(&cpu->env, ARM_FEATURE_M)) {
1374 set_feature(&cpu->env, ARM_FEATURE_PMSA);
1375 }
1376
1377 if (arm_feature(&cpu->env, ARM_FEATURE_CBAR) ||
1378 arm_feature(&cpu->env, ARM_FEATURE_CBAR_RO)) {
1379 qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_cbar_property);
1380 }
1381
1382 if (!arm_feature(&cpu->env, ARM_FEATURE_M)) {
1383 qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_hivecs_property);
1384 }
1385
1386 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1387 object_property_add(obj, "rvbar", "uint64",
1388 arm_cpu_get_rvbar,
1389 arm_cpu_set_rvbar,
1390 NULL, NULL);
1391 }
1392
1393#ifndef CONFIG_USER_ONLY
1394 object_property_add(obj, "memattr-secure", "bool",
1395 NULL, arm_cpu_set_memattr_secure,
1396 NULL, NULL);
1397#endif
1398
1399#ifndef CONFIG_USER_ONLY
1400 if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
1401
1402
1403
1404 qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el3_property);
1405
1406 object_property_add_link(obj, "secure-memory",
1407 TYPE_MEMORY_REGION,
1408 (Object **)&cpu->secure_memory,
1409 qdev_prop_allow_set_link_before_realize,
1410 OBJ_PROP_LINK_STRONG);
1411 }
1412
1413 if (arm_feature(&cpu->env, ARM_FEATURE_EL2)) {
1414 qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el2_property);
1415 }
1416#endif
1417
1418 if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) {
1419 cpu->has_pmu = true;
1420 object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu);
1421 }
1422
1423
1424
1425
1426
1427
1428 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)
1429 ? cpu_isar_feature(aa64_fp_simd, cpu)
1430 : cpu_isar_feature(aa32_vfp, cpu)) {
1431 cpu->has_vfp = true;
1432 if (!kvm_enabled()) {
1433 qdev_property_add_static(DEVICE(obj), &arm_cpu_has_vfp_property);
1434 }
1435 }
1436
1437 if (arm_feature(&cpu->env, ARM_FEATURE_NEON)) {
1438 cpu->has_neon = true;
1439 if (!kvm_enabled()) {
1440 qdev_property_add_static(DEVICE(obj), &arm_cpu_has_neon_property);
1441 }
1442 }
1443
1444 if (arm_feature(&cpu->env, ARM_FEATURE_M) &&
1445 arm_feature(&cpu->env, ARM_FEATURE_THUMB_DSP)) {
1446 qdev_property_add_static(DEVICE(obj), &arm_cpu_has_dsp_property);
1447 }
1448
1449 if (arm_feature(&cpu->env, ARM_FEATURE_PMSA)) {
1450 qdev_property_add_static(DEVICE(obj), &arm_cpu_has_mpu_property);
1451 if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
1452 qdev_property_add_static(DEVICE(obj),
1453 &arm_cpu_pmsav7_dregion_property);
1454 }
1455 }
1456
1457 if (arm_feature(&cpu->env, ARM_FEATURE_M_SECURITY)) {
1458 object_property_add_link(obj, "idau", TYPE_IDAU_INTERFACE, &cpu->idau,
1459 qdev_prop_allow_set_link_before_realize,
1460 OBJ_PROP_LINK_STRONG);
1461
1462
1463
1464
1465
1466 object_property_add_uint32_ptr(obj, "init-svtor",
1467 &cpu->init_svtor,
1468 OBJ_PROP_FLAG_READWRITE);
1469 }
1470 if (arm_feature(&cpu->env, ARM_FEATURE_M)) {
1471
1472
1473
1474
1475 object_property_add_uint32_ptr(obj, "init-nsvtor",
1476 &cpu->init_nsvtor,
1477 OBJ_PROP_FLAG_READWRITE);
1478 }
1479
1480 qdev_property_add_static(DEVICE(obj), &arm_cpu_cfgend_property);
1481
1482 if (arm_feature(&cpu->env, ARM_FEATURE_GENERIC_TIMER)) {
1483 qdev_property_add_static(DEVICE(cpu), &arm_cpu_gt_cntfrq_property);
1484 qdev_property_add_static(DEVICE(cpu), &arm_cpu_gt_cntfrq_property_alias);
1485 }
1486
1487 if (kvm_enabled()) {
1488 kvm_arm_add_vcpu_properties(obj);
1489 }
1490
1491#ifndef CONFIG_USER_ONLY
1492 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) &&
1493 cpu_isar_feature(aa64_mte, cpu)) {
1494 object_property_add_link(obj, "tag-memory",
1495 TYPE_MEMORY_REGION,
1496 (Object **)&cpu->tag_memory,
1497 qdev_prop_allow_set_link_before_realize,
1498 OBJ_PROP_LINK_STRONG);
1499
1500 if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
1501 object_property_add_link(obj, "secure-tag-memory",
1502 TYPE_MEMORY_REGION,
1503 (Object **)&cpu->secure_tag_memory,
1504 qdev_prop_allow_set_link_before_realize,
1505 OBJ_PROP_LINK_STRONG);
1506 }
1507 }
1508#endif
1509}
1510
1511static void arm_cpu_finalizefn(Object *obj)
1512{
1513 ARMCPU *cpu = ARM_CPU(obj);
1514 ARMELChangeHook *hook, *next;
1515
1516 g_hash_table_destroy(cpu->cp_regs);
1517
1518 QLIST_FOREACH_SAFE(hook, &cpu->pre_el_change_hooks, node, next) {
1519 QLIST_REMOVE(hook, node);
1520 g_free(hook);
1521 }
1522 QLIST_FOREACH_SAFE(hook, &cpu->el_change_hooks, node, next) {
1523 QLIST_REMOVE(hook, node);
1524 g_free(hook);
1525 }
1526#ifndef CONFIG_USER_ONLY
1527 if (cpu->pmu_timer) {
1528 timer_free(cpu->pmu_timer);
1529 }
1530#endif
1531}
1532
1533void arm_cpu_finalize_features(ARMCPU *cpu, Error **errp)
1534{
1535 Error *local_err = NULL;
1536
1537 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
1538 arm_cpu_sve_finalize(cpu, &local_err);
1539 if (local_err != NULL) {
1540 error_propagate(errp, local_err);
1541 return;
1542 }
1543
1544
1545
1546
1547
1548
1549 if (!kvm_enabled()) {
1550 arm_cpu_pauth_finalize(cpu, &local_err);
1551 if (local_err != NULL) {
1552 error_propagate(errp, local_err);
1553 return;
1554 }
1555 }
1556 }
1557
1558 if (kvm_enabled()) {
1559 kvm_arm_steal_time_finalize(cpu, &local_err);
1560 if (local_err != NULL) {
1561 error_propagate(errp, local_err);
1562 return;
1563 }
1564 }
1565}
1566
1567static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
1568{
1569 CPUState *cs = CPU(dev);
1570 ARMCPU *cpu = ARM_CPU(dev);
1571 ARMCPUClass *acc = ARM_CPU_GET_CLASS(dev);
1572 CPUARMState *env = &cpu->env;
1573 int pagebits;
1574 Error *local_err = NULL;
1575 bool no_aa32 = false;
1576
1577
1578
1579
1580
1581 if (cpu->host_cpu_probe_failed) {
1582 if (!kvm_enabled()) {
1583 error_setg(errp, "The 'host' CPU type can only be used with KVM");
1584 } else {
1585 error_setg(errp, "Failed to retrieve host CPU features");
1586 }
1587 return;
1588 }
1589
1590#ifndef CONFIG_USER_ONLY
1591
1592
1593
1594
1595 if (arm_feature(env, ARM_FEATURE_M)) {
1596 if (!env->nvic) {
1597 error_setg(errp, "This board cannot be used with Cortex-M CPUs");
1598 return;
1599 }
1600 } else {
1601 if (env->nvic) {
1602 error_setg(errp, "This board can only be used with Cortex-M CPUs");
1603 return;
1604 }
1605 }
1606
1607 {
1608 uint64_t scale;
1609
1610 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
1611 if (!cpu->gt_cntfrq_hz) {
1612 error_setg(errp, "Invalid CNTFRQ: %"PRId64"Hz",
1613 cpu->gt_cntfrq_hz);
1614 return;
1615 }
1616 scale = gt_cntfrq_period_ns(cpu);
1617 } else {
1618 scale = GTIMER_SCALE;
1619 }
1620
1621 cpu->gt_timer[GTIMER_PHYS] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
1622 arm_gt_ptimer_cb, cpu);
1623 cpu->gt_timer[GTIMER_VIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
1624 arm_gt_vtimer_cb, cpu);
1625 cpu->gt_timer[GTIMER_HYP] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
1626 arm_gt_htimer_cb, cpu);
1627 cpu->gt_timer[GTIMER_SEC] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
1628 arm_gt_stimer_cb, cpu);
1629 cpu->gt_timer[GTIMER_HYPVIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
1630 arm_gt_hvtimer_cb, cpu);
1631 }
1632#endif
1633
1634 cpu_exec_realizefn(cs, &local_err);
1635 if (local_err != NULL) {
1636 error_propagate(errp, local_err);
1637 return;
1638 }
1639
1640 arm_cpu_finalize_features(cpu, &local_err);
1641 if (local_err != NULL) {
1642 error_propagate(errp, local_err);
1643 return;
1644 }
1645
1646 if (arm_feature(env, ARM_FEATURE_AARCH64) &&
1647 cpu->has_vfp != cpu->has_neon) {
1648
1649
1650
1651
1652 error_setg(errp,
1653 "AArch64 CPUs must have both VFP and Neon or neither");
1654 return;
1655 }
1656
1657 if (!cpu->has_vfp) {
1658 uint64_t t;
1659 uint32_t u;
1660
1661 t = cpu->isar.id_aa64isar1;
1662 t = FIELD_DP64(t, ID_AA64ISAR1, JSCVT, 0);
1663 cpu->isar.id_aa64isar1 = t;
1664
1665 t = cpu->isar.id_aa64pfr0;
1666 t = FIELD_DP64(t, ID_AA64PFR0, FP, 0xf);
1667 cpu->isar.id_aa64pfr0 = t;
1668
1669 u = cpu->isar.id_isar6;
1670 u = FIELD_DP32(u, ID_ISAR6, JSCVT, 0);
1671 u = FIELD_DP32(u, ID_ISAR6, BF16, 0);
1672 cpu->isar.id_isar6 = u;
1673
1674 u = cpu->isar.mvfr0;
1675 u = FIELD_DP32(u, MVFR0, FPSP, 0);
1676 u = FIELD_DP32(u, MVFR0, FPDP, 0);
1677 u = FIELD_DP32(u, MVFR0, FPDIVIDE, 0);
1678 u = FIELD_DP32(u, MVFR0, FPSQRT, 0);
1679 u = FIELD_DP32(u, MVFR0, FPROUND, 0);
1680 if (!arm_feature(env, ARM_FEATURE_M)) {
1681 u = FIELD_DP32(u, MVFR0, FPTRAP, 0);
1682 u = FIELD_DP32(u, MVFR0, FPSHVEC, 0);
1683 }
1684 cpu->isar.mvfr0 = u;
1685
1686 u = cpu->isar.mvfr1;
1687 u = FIELD_DP32(u, MVFR1, FPFTZ, 0);
1688 u = FIELD_DP32(u, MVFR1, FPDNAN, 0);
1689 u = FIELD_DP32(u, MVFR1, FPHP, 0);
1690 if (arm_feature(env, ARM_FEATURE_M)) {
1691 u = FIELD_DP32(u, MVFR1, FP16, 0);
1692 }
1693 cpu->isar.mvfr1 = u;
1694
1695 u = cpu->isar.mvfr2;
1696 u = FIELD_DP32(u, MVFR2, FPMISC, 0);
1697 cpu->isar.mvfr2 = u;
1698 }
1699
1700 if (!cpu->has_neon) {
1701 uint64_t t;
1702 uint32_t u;
1703
1704 unset_feature(env, ARM_FEATURE_NEON);
1705
1706 t = cpu->isar.id_aa64isar0;
1707 t = FIELD_DP64(t, ID_AA64ISAR0, DP, 0);
1708 cpu->isar.id_aa64isar0 = t;
1709
1710 t = cpu->isar.id_aa64isar1;
1711 t = FIELD_DP64(t, ID_AA64ISAR1, FCMA, 0);
1712 t = FIELD_DP64(t, ID_AA64ISAR1, BF16, 0);
1713 t = FIELD_DP64(t, ID_AA64ISAR1, I8MM, 0);
1714 cpu->isar.id_aa64isar1 = t;
1715
1716 t = cpu->isar.id_aa64pfr0;
1717 t = FIELD_DP64(t, ID_AA64PFR0, ADVSIMD, 0xf);
1718 cpu->isar.id_aa64pfr0 = t;
1719
1720 u = cpu->isar.id_isar5;
1721 u = FIELD_DP32(u, ID_ISAR5, RDM, 0);
1722 u = FIELD_DP32(u, ID_ISAR5, VCMA, 0);
1723 cpu->isar.id_isar5 = u;
1724
1725 u = cpu->isar.id_isar6;
1726 u = FIELD_DP32(u, ID_ISAR6, DP, 0);
1727 u = FIELD_DP32(u, ID_ISAR6, FHM, 0);
1728 u = FIELD_DP32(u, ID_ISAR6, BF16, 0);
1729 u = FIELD_DP32(u, ID_ISAR6, I8MM, 0);
1730 cpu->isar.id_isar6 = u;
1731
1732 if (!arm_feature(env, ARM_FEATURE_M)) {
1733 u = cpu->isar.mvfr1;
1734 u = FIELD_DP32(u, MVFR1, SIMDLS, 0);
1735 u = FIELD_DP32(u, MVFR1, SIMDINT, 0);
1736 u = FIELD_DP32(u, MVFR1, SIMDSP, 0);
1737 u = FIELD_DP32(u, MVFR1, SIMDHP, 0);
1738 cpu->isar.mvfr1 = u;
1739
1740 u = cpu->isar.mvfr2;
1741 u = FIELD_DP32(u, MVFR2, SIMDMISC, 0);
1742 cpu->isar.mvfr2 = u;
1743 }
1744 }
1745
1746 if (!cpu->has_neon && !cpu->has_vfp) {
1747 uint64_t t;
1748 uint32_t u;
1749
1750 t = cpu->isar.id_aa64isar0;
1751 t = FIELD_DP64(t, ID_AA64ISAR0, FHM, 0);
1752 cpu->isar.id_aa64isar0 = t;
1753
1754 t = cpu->isar.id_aa64isar1;
1755 t = FIELD_DP64(t, ID_AA64ISAR1, FRINTTS, 0);
1756 cpu->isar.id_aa64isar1 = t;
1757
1758 u = cpu->isar.mvfr0;
1759 u = FIELD_DP32(u, MVFR0, SIMDREG, 0);
1760 cpu->isar.mvfr0 = u;
1761
1762
1763 u = cpu->isar.mvfr1;
1764 u = FIELD_DP32(u, MVFR1, SIMDFMAC, 0);
1765 cpu->isar.mvfr1 = u;
1766 }
1767
1768 if (arm_feature(env, ARM_FEATURE_M) && !cpu->has_dsp) {
1769 uint32_t u;
1770
1771 unset_feature(env, ARM_FEATURE_THUMB_DSP);
1772
1773 u = cpu->isar.id_isar1;
1774 u = FIELD_DP32(u, ID_ISAR1, EXTEND, 1);
1775 cpu->isar.id_isar1 = u;
1776
1777 u = cpu->isar.id_isar2;
1778 u = FIELD_DP32(u, ID_ISAR2, MULTU, 1);
1779 u = FIELD_DP32(u, ID_ISAR2, MULTS, 1);
1780 cpu->isar.id_isar2 = u;
1781
1782 u = cpu->isar.id_isar3;
1783 u = FIELD_DP32(u, ID_ISAR3, SIMD, 1);
1784 u = FIELD_DP32(u, ID_ISAR3, SATURATE, 0);
1785 cpu->isar.id_isar3 = u;
1786 }
1787
1788
1789 if (arm_feature(env, ARM_FEATURE_V8)) {
1790 if (arm_feature(env, ARM_FEATURE_M)) {
1791 set_feature(env, ARM_FEATURE_V7);
1792 } else {
1793 set_feature(env, ARM_FEATURE_V7VE);
1794 }
1795 }
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
1806 no_aa32 = !cpu_isar_feature(aa64_aa32, cpu);
1807 }
1808
1809 if (arm_feature(env, ARM_FEATURE_V7VE)) {
1810
1811
1812
1813
1814
1815
1816
1817
1818 assert(!tcg_enabled() || no_aa32 ||
1819 cpu_isar_feature(aa32_arm_div, cpu));
1820 set_feature(env, ARM_FEATURE_LPAE);
1821 set_feature(env, ARM_FEATURE_V7);
1822 }
1823 if (arm_feature(env, ARM_FEATURE_V7)) {
1824 set_feature(env, ARM_FEATURE_VAPA);
1825 set_feature(env, ARM_FEATURE_THUMB2);
1826 set_feature(env, ARM_FEATURE_MPIDR);
1827 if (!arm_feature(env, ARM_FEATURE_M)) {
1828 set_feature(env, ARM_FEATURE_V6K);
1829 } else {
1830 set_feature(env, ARM_FEATURE_V6);
1831 }
1832
1833
1834
1835
1836 set_feature(env, ARM_FEATURE_VBAR);
1837 }
1838 if (arm_feature(env, ARM_FEATURE_V6K)) {
1839 set_feature(env, ARM_FEATURE_V6);
1840 set_feature(env, ARM_FEATURE_MVFR);
1841 }
1842 if (arm_feature(env, ARM_FEATURE_V6)) {
1843 set_feature(env, ARM_FEATURE_V5);
1844 if (!arm_feature(env, ARM_FEATURE_M)) {
1845 assert(!tcg_enabled() || no_aa32 ||
1846 cpu_isar_feature(aa32_jazelle, cpu));
1847 set_feature(env, ARM_FEATURE_AUXCR);
1848 }
1849 }
1850 if (arm_feature(env, ARM_FEATURE_V5)) {
1851 set_feature(env, ARM_FEATURE_V4T);
1852 }
1853 if (arm_feature(env, ARM_FEATURE_LPAE)) {
1854 set_feature(env, ARM_FEATURE_V7MP);
1855 }
1856 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
1857 set_feature(env, ARM_FEATURE_CBAR);
1858 }
1859 if (arm_feature(env, ARM_FEATURE_THUMB2) &&
1860 !arm_feature(env, ARM_FEATURE_M)) {
1861 set_feature(env, ARM_FEATURE_THUMB_DSP);
1862 }
1863
1864
1865
1866
1867
1868 assert(arm_feature(&cpu->env, ARM_FEATURE_AARCH64) ||
1869 !cpu_isar_feature(aa32_vfp_simd, cpu) ||
1870 !arm_feature(env, ARM_FEATURE_XSCALE));
1871
1872 if (arm_feature(env, ARM_FEATURE_V7) &&
1873 !arm_feature(env, ARM_FEATURE_M) &&
1874 !arm_feature(env, ARM_FEATURE_PMSA)) {
1875
1876
1877
1878 pagebits = 12;
1879 } else {
1880
1881
1882
1883 pagebits = 10;
1884 }
1885 if (!set_preferred_target_page_bits(pagebits)) {
1886
1887
1888
1889
1890 error_setg(errp, "This CPU requires a smaller page size than the "
1891 "system is using");
1892 return;
1893 }
1894
1895
1896
1897
1898
1899
1900 if (cpu->mp_affinity == ARM64_AFFINITY_INVALID) {
1901 cpu->mp_affinity = arm_cpu_mp_affinity(cs->cpu_index,
1902 ARM_DEFAULT_CPUS_PER_CLUSTER);
1903 }
1904
1905 if (cpu->reset_hivecs) {
1906 cpu->reset_sctlr |= (1 << 13);
1907 }
1908
1909 if (cpu->cfgend) {
1910 if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
1911 cpu->reset_sctlr |= SCTLR_EE;
1912 } else {
1913 cpu->reset_sctlr |= SCTLR_B;
1914 }
1915 }
1916
1917 if (!arm_feature(env, ARM_FEATURE_M) && !cpu->has_el3) {
1918
1919
1920
1921 unset_feature(env, ARM_FEATURE_EL3);
1922
1923
1924
1925
1926 cpu->isar.id_pfr1 &= ~0xf0;
1927 cpu->isar.id_aa64pfr0 &= ~0xf000;
1928 }
1929
1930 if (!cpu->has_el2) {
1931 unset_feature(env, ARM_FEATURE_EL2);
1932 }
1933
1934 if (!cpu->has_pmu) {
1935 unset_feature(env, ARM_FEATURE_PMU);
1936 }
1937 if (arm_feature(env, ARM_FEATURE_PMU)) {
1938 pmu_init(cpu);
1939
1940 if (!kvm_enabled()) {
1941 arm_register_pre_el_change_hook(cpu, &pmu_pre_el_change, 0);
1942 arm_register_el_change_hook(cpu, &pmu_post_el_change, 0);
1943 }
1944
1945#ifndef CONFIG_USER_ONLY
1946 cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, arm_pmu_timer_cb,
1947 cpu);
1948#endif
1949 } else {
1950 cpu->isar.id_aa64dfr0 =
1951 FIELD_DP64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, PMUVER, 0);
1952 cpu->isar.id_dfr0 = FIELD_DP32(cpu->isar.id_dfr0, ID_DFR0, PERFMON, 0);
1953 cpu->pmceid0 = 0;
1954 cpu->pmceid1 = 0;
1955 }
1956
1957 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1958
1959
1960
1961
1962 cpu->isar.id_aa64pfr0 &= ~0xf00;
1963 cpu->isar.id_pfr1 &= ~0xf000;
1964 }
1965
1966#ifndef CONFIG_USER_ONLY
1967 if (cpu->tag_memory == NULL && cpu_isar_feature(aa64_mte, cpu)) {
1968
1969
1970
1971
1972 cpu->isar.id_aa64pfr1 =
1973 FIELD_DP64(cpu->isar.id_aa64pfr1, ID_AA64PFR1, MTE, 0);
1974 }
1975#endif
1976
1977
1978
1979
1980 if (!cpu->has_mpu) {
1981 cpu->pmsav7_dregion = 0;
1982 }
1983 if (cpu->pmsav7_dregion == 0) {
1984 cpu->has_mpu = false;
1985 }
1986
1987 if (arm_feature(env, ARM_FEATURE_PMSA) &&
1988 arm_feature(env, ARM_FEATURE_V7)) {
1989 uint32_t nr = cpu->pmsav7_dregion;
1990
1991 if (nr > 0xff) {
1992 error_setg(errp, "PMSAv7 MPU #regions invalid %" PRIu32, nr);
1993 return;
1994 }
1995
1996 if (nr) {
1997 if (arm_feature(env, ARM_FEATURE_V8)) {
1998
1999 env->pmsav8.rbar[M_REG_NS] = g_new0(uint32_t, nr);
2000 env->pmsav8.rlar[M_REG_NS] = g_new0(uint32_t, nr);
2001 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
2002 env->pmsav8.rbar[M_REG_S] = g_new0(uint32_t, nr);
2003 env->pmsav8.rlar[M_REG_S] = g_new0(uint32_t, nr);
2004 }
2005 } else {
2006 env->pmsav7.drbar = g_new0(uint32_t, nr);
2007 env->pmsav7.drsr = g_new0(uint32_t, nr);
2008 env->pmsav7.dracr = g_new0(uint32_t, nr);
2009 }
2010 }
2011 }
2012
2013 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
2014 uint32_t nr = cpu->sau_sregion;
2015
2016 if (nr > 0xff) {
2017 error_setg(errp, "v8M SAU #regions invalid %" PRIu32, nr);
2018 return;
2019 }
2020
2021 if (nr) {
2022 env->sau.rbar = g_new0(uint32_t, nr);
2023 env->sau.rlar = g_new0(uint32_t, nr);
2024 }
2025 }
2026
2027 if (arm_feature(env, ARM_FEATURE_EL3)) {
2028 set_feature(env, ARM_FEATURE_VBAR);
2029 }
2030
2031 register_cp_regs_for_features(cpu);
2032 arm_cpu_register_gdb_regs_for_features(cpu);
2033
2034 init_cpreg_list(cpu);
2035
2036#ifndef CONFIG_USER_ONLY
2037 MachineState *ms = MACHINE(qdev_get_machine());
2038 unsigned int smp_cpus = ms->smp.cpus;
2039 bool has_secure = cpu->has_el3 || arm_feature(env, ARM_FEATURE_M_SECURITY);
2040
2041
2042
2043
2044
2045 has_secure = true;
2046
2047
2048
2049
2050
2051 if (cpu->tag_memory != NULL) {
2052 cs->num_ases = 3 + has_secure;
2053 } else {
2054 cs->num_ases = 1 + has_secure;
2055 }
2056
2057 if (has_secure) {
2058 if (!cpu->secure_memory) {
2059 cpu->secure_memory = cs->memory;
2060 }
2061 cpu_address_space_init(cs, ARMASIdx_S, "cpu-secure-memory",
2062 cpu->secure_memory);
2063 }
2064
2065 if (cpu->tag_memory != NULL) {
2066 cpu_address_space_init(cs, ARMASIdx_TagNS, "cpu-tag-memory",
2067 cpu->tag_memory);
2068 if (has_secure) {
2069 cpu_address_space_init(cs, ARMASIdx_TagS, "cpu-tag-memory",
2070 cpu->secure_tag_memory);
2071 }
2072 }
2073
2074 cpu_address_space_init(cs, ARMASIdx_NS, "cpu-memory", cs->memory);
2075
2076
2077 if (cpu->core_count == -1) {
2078 cpu->core_count = smp_cpus;
2079 }
2080#endif
2081
2082 if (tcg_enabled()) {
2083 int dcz_blocklen = 4 << cpu->dcz_blocksize;
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094 assert(dcz_blocklen <= TARGET_PAGE_SIZE);
2095
2096
2097
2098
2099
2100
2101 if (cpu_isar_feature(aa64_mte, cpu)) {
2102 assert(dcz_blocklen >= 2 * TAG_GRANULE);
2103 }
2104 }
2105
2106 qemu_init_vcpu(cs);
2107 cpu_reset(cs);
2108
2109 acc->parent_realize(dev, errp);
2110}
2111
2112static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
2113{
2114 ObjectClass *oc;
2115 char *typename;
2116 char **cpuname;
2117 const char *cpunamestr;
2118
2119 cpuname = g_strsplit(cpu_model, ",", 1);
2120 cpunamestr = cpuname[0];
2121#ifdef CONFIG_USER_ONLY
2122
2123
2124
2125 if (!strcmp(cpunamestr, "any")) {
2126 cpunamestr = "max";
2127 }
2128#endif
2129 typename = g_strdup_printf(ARM_CPU_TYPE_NAME("%s"), cpunamestr);
2130 oc = object_class_by_name(typename);
2131 g_strfreev(cpuname);
2132 g_free(typename);
2133 if (!oc || !object_class_dynamic_cast(oc, TYPE_ARM_CPU) ||
2134 object_class_is_abstract(oc)) {
2135 return NULL;
2136 }
2137 return oc;
2138}
2139
2140static Property arm_cpu_properties[] = {
2141 DEFINE_PROP_UINT32("psci-conduit", ARMCPU, psci_conduit, 0),
2142 DEFINE_PROP_UINT64("ctr", ARMCPU, ctr, 0),
2143 DEFINE_PROP_UINT64("clidr", ARMCPU, clidr, 0),
2144 DEFINE_PROP_UINT32("id_pfr0", ARMCPU, isar.id_pfr0, 0),
2145 DEFINE_PROP_UINT32("id_pfr1", ARMCPU, isar.id_pfr1, 0),
2146 DEFINE_PROP_UINT64("ccsidr0", ARMCPU, ccsidr[0], 0),
2147 DEFINE_PROP_UINT64("ccsidr1", ARMCPU, ccsidr[1], 0),
2148 DEFINE_PROP_UINT64("midr", ARMCPU, midr, 0),
2149 DEFINE_PROP_UINT64("mp-affinity", ARMCPU,
2150 mp_affinity, ARM64_AFFINITY_INVALID),
2151 DEFINE_PROP_INT32("node-id", ARMCPU, node_id, CPU_UNSET_NUMA_NODE_ID),
2152 DEFINE_PROP_INT32("core-count", ARMCPU, core_count, -1),
2153 DEFINE_PROP_END_OF_LIST()
2154};
2155
2156static void set_debug_context(CPUState *cs, unsigned int ctx)
2157{
2158 ARMCPU *cpu = ARM_CPU(cs);
2159 switch (ctx) {
2160 case ARM_DEBUG_CURRENT_EL:
2161 cpu->env.debug_ctx = DEBUG_CURRENT_EL;
2162 break;
2163
2164 case ARM_DEBUG_PHYS:
2165 cpu->env.debug_ctx = DEBUG_PHYS;
2166 break;
2167 }
2168}
2169
2170static void arm_cpu_pwr_cntrl(void *opaque, int n, int level)
2171{
2172 DeviceClass *dc_parent = DEVICE_CLASS(ARM_CPU_PARENT_CLASS);
2173 ARMCPU *cpu = ARM_CPU(opaque);
2174
2175 cpu->power_state = level ? PSCI_ON : PSCI_OFF;
2176 dc_parent->pwr_cntrl(opaque, n, level);
2177}
2178
2179static gchar *arm_gdb_arch_name(CPUState *cs)
2180{
2181 ARMCPU *cpu = ARM_CPU(cs);
2182 CPUARMState *env = &cpu->env;
2183
2184 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
2185 return g_strdup("iwmmxt");
2186 }
2187 return g_strdup("arm");
2188}
2189
2190#ifndef CONFIG_USER_ONLY
2191#include "hw/core/sysemu-cpu-ops.h"
2192
2193static const struct SysemuCPUOps arm_sysemu_ops = {
2194 .get_phys_page_attrs_debug = arm_cpu_get_phys_page_attrs_debug,
2195 .asidx_from_attrs = arm_asidx_from_attrs,
2196 .write_elf32_note = arm_cpu_write_elf32_note,
2197 .write_elf64_note = arm_cpu_write_elf64_note,
2198 .virtio_is_big_endian = arm_cpu_virtio_is_big_endian,
2199 .legacy_vmsd = &vmstate_arm_cpu,
2200};
2201#endif
2202
2203#ifdef CONFIG_TCG
2204static const struct TCGCPUOps arm_tcg_ops = {
2205 .initialize = arm_translate_init,
2206 .synchronize_from_tb = arm_cpu_synchronize_from_tb,
2207 .cpu_exec_interrupt = arm_cpu_exec_interrupt,
2208 .tlb_fill = arm_cpu_tlb_fill,
2209 .debug_excp_handler = arm_debug_excp_handler,
2210
2211#if !defined(CONFIG_USER_ONLY)
2212 .do_interrupt = arm_cpu_do_interrupt,
2213 .do_transaction_failed = arm_cpu_do_transaction_failed,
2214 .do_unaligned_access = arm_cpu_do_unaligned_access,
2215 .adjust_watchpoint_address = arm_adjust_watchpoint_address,
2216 .debug_check_watchpoint = arm_debug_check_watchpoint,
2217 .debug_check_breakpoint = arm_debug_check_breakpoint,
2218#endif
2219};
2220#endif
2221
2222static void arm_cpu_class_init(ObjectClass *oc, void *data)
2223{
2224 ARMCPUClass *acc = ARM_CPU_CLASS(oc);
2225 CPUClass *cc = CPU_CLASS(acc);
2226 DeviceClass *dc = DEVICE_CLASS(oc);
2227
2228 device_class_set_parent_realize(dc, arm_cpu_realizefn,
2229 &acc->parent_realize);
2230
2231 dc->pwr_cntrl = arm_cpu_pwr_cntrl;
2232
2233 device_class_set_props(dc, arm_cpu_properties);
2234 device_class_set_parent_reset(dc, arm_cpu_reset, &acc->parent_reset);
2235
2236 cc->class_by_name = arm_cpu_class_by_name;
2237 cc->has_work = arm_cpu_has_work;
2238 cc->dump_state = arm_cpu_dump_state;
2239 cc->set_pc = arm_cpu_set_pc;
2240 cc->get_pc = arm_cpu_get_pc;
2241 cc->debug_contexts = arm_debug_ctx;
2242 cc->set_debug_context = set_debug_context;
2243 cc->gdb_read_register = arm_cpu_gdb_read_register;
2244 cc->gdb_write_register = arm_cpu_gdb_write_register;
2245#ifndef CONFIG_USER_ONLY
2246 dc->rst_cntrl = cpu_reset_gpio;
2247 cc->sysemu_ops = &arm_sysemu_ops;
2248#endif
2249 cc->gdb_num_core_regs = 32;
2250 cc->gdb_core_xml_file = "arm-core.xml";
2251 cc->gdb_arch_name = arm_gdb_arch_name;
2252 cc->gdb_get_dynamic_xml = arm_gdb_get_dynamic_xml;
2253 cc->gdb_stop_before_watchpoint = true;
2254 cc->disas_set_info = arm_disas_set_info;
2255
2256#ifdef CONFIG_TCG
2257 cc->tcg_ops = &arm_tcg_ops;
2258#endif
2259}
2260
2261#ifdef CONFIG_KVM
2262static void arm_host_initfn(Object *obj)
2263{
2264 ARMCPU *cpu = ARM_CPU(obj);
2265
2266 kvm_arm_set_cpu_features_from_host(cpu);
2267 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
2268 aarch64_add_sve_properties(obj);
2269 }
2270 arm_cpu_post_init(obj);
2271}
2272
2273static const TypeInfo host_arm_cpu_type_info = {
2274 .name = TYPE_ARM_HOST_CPU,
2275 .parent = TYPE_AARCH64_CPU,
2276 .instance_init = arm_host_initfn,
2277};
2278
2279#endif
2280
2281static void arm_cpu_instance_init(Object *obj)
2282{
2283 ARMCPUClass *acc = ARM_CPU_GET_CLASS(obj);
2284
2285 acc->info->initfn(obj);
2286 arm_cpu_post_init(obj);
2287}
2288
2289static void cpu_register_class_init(ObjectClass *oc, void *data)
2290{
2291 ARMCPUClass *acc = ARM_CPU_CLASS(oc);
2292
2293 acc->info = data;
2294}
2295
2296void arm_cpu_register(const ARMCPUInfo *info)
2297{
2298 TypeInfo type_info = {
2299 .parent = TYPE_ARM_CPU,
2300 .instance_size = sizeof(ARMCPU),
2301 .instance_align = __alignof__(ARMCPU),
2302 .instance_init = arm_cpu_instance_init,
2303 .class_size = sizeof(ARMCPUClass),
2304 .class_init = info->class_init ?: cpu_register_class_init,
2305 .class_data = (void *)info,
2306 };
2307
2308 type_info.name = g_strdup_printf("%s-" TYPE_ARM_CPU, info->name);
2309 type_register(&type_info);
2310 g_free((void *)type_info.name);
2311}
2312
2313static const TypeInfo arm_cpu_type_info = {
2314 .name = TYPE_ARM_CPU,
2315 .parent = TYPE_CPU,
2316 .instance_size = sizeof(ARMCPU),
2317 .instance_align = __alignof__(ARMCPU),
2318 .instance_init = arm_cpu_initfn,
2319 .instance_finalize = arm_cpu_finalizefn,
2320 .abstract = true,
2321 .class_size = sizeof(ARMCPUClass),
2322 .class_init = arm_cpu_class_init,
2323};
2324
2325static void arm_cpu_register_types(void)
2326{
2327 type_register_static(&arm_cpu_type_info);
2328
2329#ifdef CONFIG_KVM
2330 type_register_static(&host_arm_cpu_type_info);
2331#endif
2332}
2333
2334type_init(arm_cpu_register_types)
2335
2336#ifndef CONFIG_USER_ONLY
2337
2338static Object *fdt_armv8_timer_get_intc(FDTMachineInfo *fdti, char *node_path)
2339{
2340 char intc_node_path[DT_PATH_LENGTH];
2341 uint32_t intc_phandle;
2342 Error *errp = NULL;
2343 DeviceState *intc;
2344
2345 intc_phandle = qemu_fdt_getprop_cell(fdti->fdt, node_path,
2346 "interrupt-parent",
2347 0, true, &errp);
2348
2349
2350 if (errp ||
2351 qemu_devtree_get_node_by_phandle(fdti->fdt,
2352 intc_node_path, intc_phandle)) {
2353 g_assert_not_reached();
2354 }
2355
2356 while (!fdt_init_has_opaque(fdti, intc_node_path)) {
2357 fdt_init_yield(fdti);
2358 }
2359
2360 intc = DEVICE(fdt_init_get_opaque(fdti, intc_node_path));
2361
2362 while (!intc->realized) {
2363 fdt_init_yield(fdti);
2364 }
2365
2366 return OBJECT(intc);
2367}
2368
2369static int armv8_timer_fdt_init(char *node_path, FDTMachineInfo *fdti,
2370 void *priv)
2371{
2372 Object *intc = fdt_armv8_timer_get_intc(fdti, node_path);
2373 CPUState *cpu;
2374 bool map_mode = false;
2375 qemu_irq *sec_irqs = NULL;
2376 qemu_irq *ns_irqs;
2377 qemu_irq *v_irqs;
2378 qemu_irq *h_irqs;
2379 uint32_t first_cpu_idx;
2380 uint32_t num_cpu;
2381 bool has_sec_ext;
2382 Error *err = NULL;
2383
2384 first_cpu_idx = object_property_get_uint(intc, "first-cpu-idx", &err);
2385 if (!err) {
2386 num_cpu = object_property_get_uint(intc, "num-cpu", &err);
2387 assert(!err);
2388 has_sec_ext = object_property_get_bool(intc, "has-security-extensions",
2389 &err);
2390 assert(!err);
2391 } else {
2392
2393
2394
2395
2396
2397 num_cpu = 0;
2398 has_sec_ext = true;
2399 }
2400
2401 if (has_sec_ext) {
2402 sec_irqs = fdt_get_irq(fdti, node_path, 0, &map_mode);
2403 ns_irqs = fdt_get_irq(fdti, node_path, 1, &map_mode);
2404 v_irqs = fdt_get_irq(fdti, node_path, 2, &map_mode);
2405 h_irqs = fdt_get_irq(fdti, node_path, 3, &map_mode);
2406 } else {
2407 ns_irqs = fdt_get_irq(fdti, node_path, 0, &map_mode);
2408 v_irqs = fdt_get_irq(fdti, node_path, 1, &map_mode);
2409 h_irqs = fdt_get_irq(fdti, node_path, 2, &map_mode);
2410 }
2411
2412 assert(!map_mode);
2413
2414 for (cpu = first_cpu; cpu; cpu = CPU_NEXT(cpu)) {
2415 ARMCPU *acpu = ARM_CPU(cpu);
2416 bool is_gic_cpu;
2417
2418 if (!arm_feature(&acpu->env, ARM_FEATURE_GENERIC_TIMER)) {
2419 continue;
2420 }
2421
2422 is_gic_cpu = cpu->cpu_index >= first_cpu_idx &&
2423 cpu->cpu_index < (first_cpu_idx + num_cpu);
2424
2425 if (!num_cpu || is_gic_cpu) {
2426
2427 assert(*ns_irqs);
2428 assert(*v_irqs);
2429 assert(*h_irqs);
2430 qdev_connect_gpio_out(DEVICE(acpu), 0, *ns_irqs++);
2431 qdev_connect_gpio_out(DEVICE(acpu), 1, *v_irqs++);
2432 qdev_connect_gpio_out(DEVICE(acpu), 2, *h_irqs++);
2433
2434 if (has_sec_ext) {
2435 assert(*sec_irqs);
2436 qdev_connect_gpio_out(DEVICE(acpu), 3, *sec_irqs++);
2437 }
2438 }
2439 }
2440
2441 return 0;
2442}
2443
2444fdt_register_compatibility_n(armv8_timer_fdt_init,
2445 "compatible:arm,armv8-timer", 13);
2446
2447#endif
2448
2449static const TypeInfo fdt_qom_aliases [] = {
2450 { .name = "arm.cortex-a9", .parent = "cortex-a9-arm-cpu" },
2451};
2452
2453static void fdt_generic_register_types(void)
2454{
2455 int i;
2456
2457 for (i = 0; i < ARRAY_SIZE(fdt_qom_aliases); ++i) {
2458 type_register_static(&fdt_qom_aliases[i]);
2459 }
2460}
2461
2462type_init(fdt_generic_register_types)
2463