1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#ifndef TARGET_ARM_INTERNALS_H
26#define TARGET_ARM_INTERNALS_H
27
28#include "hw/registerfields.h"
29#include "tcg/tcg-gvec-desc.h"
30#include "syndrome.h"
31
32
33#define BANK_USRSYS 0
34#define BANK_SVC 1
35#define BANK_ABT 2
36#define BANK_UND 3
37#define BANK_IRQ 4
38#define BANK_FIQ 5
39#define BANK_HYP 6
40#define BANK_MON 7
41
42static inline bool excp_is_internal(int excp)
43{
44
45
46
47 return excp == EXCP_INTERRUPT
48 || excp == EXCP_HLT
49 || excp == EXCP_DEBUG
50 || excp == EXCP_HALTED
51 || excp == EXCP_EXCEPTION_EXIT
52 || excp == EXCP_KERNEL_TRAP
53 || excp == EXCP_SEMIHOST;
54}
55
56
57
58
59#define GTIMER_SCALE 16
60
61
62FIELD(V7M_CONTROL, NPRIV, 0, 1)
63FIELD(V7M_CONTROL, SPSEL, 1, 1)
64FIELD(V7M_CONTROL, FPCA, 2, 1)
65FIELD(V7M_CONTROL, SFPA, 3, 1)
66
67
68FIELD(V7M_EXCRET, ES, 0, 1)
69FIELD(V7M_EXCRET, RES0, 1, 1)
70FIELD(V7M_EXCRET, SPSEL, 2, 1)
71FIELD(V7M_EXCRET, MODE, 3, 1)
72FIELD(V7M_EXCRET, FTYPE, 4, 1)
73FIELD(V7M_EXCRET, DCRS, 5, 1)
74FIELD(V7M_EXCRET, S, 6, 1)
75FIELD(V7M_EXCRET, RES1, 7, 25)
76
77
78#define EXC_RETURN_MIN_MAGIC 0xff000000
79
80
81
82#define FNC_RETURN_MIN_MAGIC 0xfefffffe
83
84
85
86
87
88
89
90
91
92
93
94
95
96#define M_FAKE_FSR_NSC_EXEC 0xf
97#define M_FAKE_FSR_SFAULT 0xe
98
99
100
101
102
103
104
105void QEMU_NORETURN raise_exception(CPUARMState *env, uint32_t excp,
106 uint32_t syndrome, uint32_t target_el);
107
108
109
110
111void QEMU_NORETURN raise_exception_ra(CPUARMState *env, uint32_t excp,
112 uint32_t syndrome, uint32_t target_el,
113 uintptr_t ra);
114
115
116
117
118
119
120
121static inline unsigned int aarch64_banked_spsr_index(unsigned int el)
122{
123 static const unsigned int map[4] = {
124 [1] = BANK_SVC,
125 [2] = BANK_HYP,
126 [3] = BANK_MON,
127 };
128 assert(el >= 1 && el <= 3);
129 return map[el];
130}
131
132
133static inline int bank_number(int mode)
134{
135 switch (mode) {
136 case ARM_CPU_MODE_USR:
137 case ARM_CPU_MODE_SYS:
138 return BANK_USRSYS;
139 case ARM_CPU_MODE_SVC:
140 return BANK_SVC;
141 case ARM_CPU_MODE_ABT:
142 return BANK_ABT;
143 case ARM_CPU_MODE_UND:
144 return BANK_UND;
145 case ARM_CPU_MODE_IRQ:
146 return BANK_IRQ;
147 case ARM_CPU_MODE_FIQ:
148 return BANK_FIQ;
149 case ARM_CPU_MODE_HYP:
150 return BANK_HYP;
151 case ARM_CPU_MODE_MON:
152 return BANK_MON;
153 }
154 g_assert_not_reached();
155}
156
157
158
159
160
161
162
163
164
165
166
167
168static inline int r14_bank_number(int mode)
169{
170 return (mode == ARM_CPU_MODE_HYP) ? BANK_USRSYS : bank_number(mode);
171}
172
173void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu);
174void arm_translate_init(void);
175
176#ifdef CONFIG_TCG
177void arm_cpu_synchronize_from_tb(CPUState *cs, const TranslationBlock *tb);
178#endif
179
180
181
182
183
184
185
186
187
188
189uint32_t aarch64_sve_zcr_get_valid_len(ARMCPU *cpu, uint32_t start_len);
190
191enum arm_fprounding {
192 FPROUNDING_TIEEVEN,
193 FPROUNDING_POSINF,
194 FPROUNDING_NEGINF,
195 FPROUNDING_ZERO,
196 FPROUNDING_TIEAWAY,
197 FPROUNDING_ODD
198};
199
200int arm_rmode_to_sf(int rmode);
201
202static inline void aarch64_save_sp(CPUARMState *env, int el)
203{
204 if (env->pstate & PSTATE_SP) {
205 env->sp_el[el] = env->xregs[31];
206 } else {
207 env->sp_el[0] = env->xregs[31];
208 }
209}
210
211static inline void aarch64_restore_sp(CPUARMState *env, int el)
212{
213 if (env->pstate & PSTATE_SP) {
214 env->xregs[31] = env->sp_el[el];
215 } else {
216 env->xregs[31] = env->sp_el[0];
217 }
218}
219
220static inline void update_spsel(CPUARMState *env, uint32_t imm)
221{
222 unsigned int cur_el = arm_current_el(env);
223
224
225
226 if (!((imm ^ env->pstate) & PSTATE_SP)) {
227 return;
228 }
229 aarch64_save_sp(env, cur_el);
230 env->pstate = deposit32(env->pstate, 0, 1, imm);
231
232
233
234
235 assert(cur_el >= 1 && cur_el <= 3);
236 aarch64_restore_sp(env, cur_el);
237}
238
239
240
241
242
243
244
245
246static inline unsigned int arm_pamax(ARMCPU *cpu)
247{
248 static const unsigned int pamax_map[] = {
249 [0] = 32,
250 [1] = 36,
251 [2] = 40,
252 [3] = 42,
253 [4] = 44,
254 [5] = 48,
255 };
256 unsigned int parange =
257 FIELD_EX64(cpu->isar.id_aa64mmfr0, ID_AA64MMFR0, PARANGE);
258
259
260
261 assert(parange < ARRAY_SIZE(pamax_map));
262 return pamax_map[parange];
263}
264
265
266
267
268
269static inline bool extended_addresses_enabled(CPUARMState *env)
270{
271 TCR *tcr = &env->cp15.tcr_el[arm_is_secure(env) ? 3 : 1];
272 return arm_el_is_aa64(env, 1) ||
273 (arm_feature(env, ARM_FEATURE_LPAE) && (tcr->raw_tcr & TTBCR_EAE));
274}
275
276
277
278
279void hw_watchpoint_update(ARMCPU *cpu, int n);
280
281
282
283
284void hw_watchpoint_update_all(ARMCPU *cpu);
285
286
287
288void hw_breakpoint_update(ARMCPU *cpu, int n);
289
290
291
292
293void hw_breakpoint_update_all(ARMCPU *cpu);
294
295
296bool arm_debug_check_breakpoint(CPUState *cs);
297
298
299bool arm_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp);
300
301
302
303
304vaddr arm_adjust_watchpoint_address(CPUState *cs, vaddr addr, int len);
305
306
307void arm_debug_excp_handler(CPUState *cs);
308
309#if defined(CONFIG_USER_ONLY) || !defined(CONFIG_TCG)
310static inline bool arm_is_psci_call(ARMCPU *cpu, int excp_type)
311{
312 return false;
313}
314static inline void arm_handle_psci_call(ARMCPU *cpu)
315{
316 g_assert_not_reached();
317}
318#else
319
320bool arm_is_psci_call(ARMCPU *cpu, int excp_type);
321
322void arm_handle_psci_call(ARMCPU *cpu);
323#endif
324
325
326
327
328
329
330static inline void arm_clear_exclusive(CPUARMState *env)
331{
332 env->exclusive_addr = -1;
333}
334
335
336
337
338
339
340typedef enum ARMFaultType {
341 ARMFault_None,
342 ARMFault_AccessFlag,
343 ARMFault_Alignment,
344 ARMFault_Background,
345 ARMFault_Domain,
346 ARMFault_Permission,
347 ARMFault_Translation,
348 ARMFault_AddressSize,
349 ARMFault_SyncExternal,
350 ARMFault_SyncExternalOnWalk,
351 ARMFault_SyncParity,
352 ARMFault_SyncParityOnWalk,
353 ARMFault_AsyncParity,
354 ARMFault_AsyncExternal,
355 ARMFault_Debug,
356 ARMFault_TLBConflict,
357 ARMFault_Lockdown,
358 ARMFault_Exclusive,
359 ARMFault_ICacheMaint,
360 ARMFault_QEMU_NSCExec,
361 ARMFault_QEMU_SFault,
362} ARMFaultType;
363
364
365
366
367
368
369
370
371
372
373
374
375typedef struct ARMMMUFaultInfo ARMMMUFaultInfo;
376struct ARMMMUFaultInfo {
377 ARMFaultType type;
378 target_ulong s2addr;
379 int level;
380 int domain;
381 bool stage2;
382 bool s1ptw;
383 bool s1ns;
384 bool ea;
385};
386
387
388
389
390
391
392
393static inline uint32_t arm_fi_to_sfsc(ARMMMUFaultInfo *fi)
394{
395 uint32_t fsc;
396
397 switch (fi->type) {
398 case ARMFault_None:
399 return 0;
400 case ARMFault_AccessFlag:
401 fsc = fi->level == 1 ? 0x3 : 0x6;
402 break;
403 case ARMFault_Alignment:
404 fsc = 0x1;
405 break;
406 case ARMFault_Permission:
407 fsc = fi->level == 1 ? 0xd : 0xf;
408 break;
409 case ARMFault_Domain:
410 fsc = fi->level == 1 ? 0x9 : 0xb;
411 break;
412 case ARMFault_Translation:
413 fsc = fi->level == 1 ? 0x5 : 0x7;
414 break;
415 case ARMFault_SyncExternal:
416 fsc = 0x8 | (fi->ea << 12);
417 break;
418 case ARMFault_SyncExternalOnWalk:
419 fsc = fi->level == 1 ? 0xc : 0xe;
420 fsc |= (fi->ea << 12);
421 break;
422 case ARMFault_SyncParity:
423 fsc = 0x409;
424 break;
425 case ARMFault_SyncParityOnWalk:
426 fsc = fi->level == 1 ? 0x40c : 0x40e;
427 break;
428 case ARMFault_AsyncParity:
429 fsc = 0x408;
430 break;
431 case ARMFault_AsyncExternal:
432 fsc = 0x406 | (fi->ea << 12);
433 break;
434 case ARMFault_Debug:
435 fsc = 0x2;
436 break;
437 case ARMFault_TLBConflict:
438 fsc = 0x400;
439 break;
440 case ARMFault_Lockdown:
441 fsc = 0x404;
442 break;
443 case ARMFault_Exclusive:
444 fsc = 0x405;
445 break;
446 case ARMFault_ICacheMaint:
447 fsc = 0x4;
448 break;
449 case ARMFault_Background:
450 fsc = 0x0;
451 break;
452 case ARMFault_QEMU_NSCExec:
453 fsc = M_FAKE_FSR_NSC_EXEC;
454 break;
455 case ARMFault_QEMU_SFault:
456 fsc = M_FAKE_FSR_SFAULT;
457 break;
458 default:
459
460
461
462 g_assert_not_reached();
463 }
464
465 fsc |= (fi->domain << 4);
466 return fsc;
467}
468
469
470
471
472
473
474static inline uint32_t arm_fi_to_lfsc(ARMMMUFaultInfo *fi)
475{
476 uint32_t fsc;
477
478 switch (fi->type) {
479 case ARMFault_None:
480 return 0;
481 case ARMFault_AddressSize:
482 fsc = fi->level & 3;
483 break;
484 case ARMFault_AccessFlag:
485 fsc = (fi->level & 3) | (0x2 << 2);
486 break;
487 case ARMFault_Permission:
488 fsc = (fi->level & 3) | (0x3 << 2);
489 break;
490 case ARMFault_Translation:
491 fsc = (fi->level & 3) | (0x1 << 2);
492 break;
493 case ARMFault_SyncExternal:
494 fsc = 0x10 | (fi->ea << 12);
495 break;
496 case ARMFault_SyncExternalOnWalk:
497 fsc = (fi->level & 3) | (0x5 << 2) | (fi->ea << 12);
498 break;
499 case ARMFault_SyncParity:
500 fsc = 0x18;
501 break;
502 case ARMFault_SyncParityOnWalk:
503 fsc = (fi->level & 3) | (0x7 << 2);
504 break;
505 case ARMFault_AsyncParity:
506 fsc = 0x19;
507 break;
508 case ARMFault_AsyncExternal:
509 fsc = 0x11 | (fi->ea << 12);
510 break;
511 case ARMFault_Alignment:
512 fsc = 0x21;
513 break;
514 case ARMFault_Debug:
515 fsc = 0x22;
516 break;
517 case ARMFault_TLBConflict:
518 fsc = 0x30;
519 break;
520 case ARMFault_Lockdown:
521 fsc = 0x34;
522 break;
523 case ARMFault_Exclusive:
524 fsc = 0x35;
525 break;
526 default:
527
528
529
530 g_assert_not_reached();
531 }
532
533 fsc |= 1 << 9;
534 return fsc;
535}
536
537static inline bool arm_extabort_type(MemTxResult result)
538{
539
540
541
542
543
544 return result != MEMTX_DECODE_ERROR;
545}
546
547bool arm_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
548 MMUAccessType access_type, int mmu_idx,
549 bool probe, uintptr_t retaddr);
550
551static inline int arm_to_core_mmu_idx(ARMMMUIdx mmu_idx)
552{
553 return mmu_idx & ARM_MMU_IDX_COREIDX_MASK;
554}
555
556static inline ARMMMUIdx core_to_arm_mmu_idx(CPUARMState *env, int mmu_idx)
557{
558 if (arm_feature(env, ARM_FEATURE_M)) {
559 return mmu_idx | ARM_MMU_IDX_M;
560 } else {
561 return mmu_idx | ARM_MMU_IDX_A;
562 }
563}
564
565static inline ARMMMUIdx core_to_aa64_mmu_idx(int mmu_idx)
566{
567
568 return mmu_idx | ARM_MMU_IDX_A;
569}
570
571int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx);
572
573
574
575
576
577ARMMMUIdx arm_v7m_mmu_idx_all(CPUARMState *env,
578 bool secstate, bool priv, bool negpri);
579
580
581
582
583
584ARMMMUIdx arm_v7m_mmu_idx_for_secstate_and_priv(CPUARMState *env,
585 bool secstate, bool priv);
586
587
588ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate);
589
590
591
592bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx);
593
594
595void arm_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr,
596 MMUAccessType access_type,
597 int mmu_idx, uintptr_t retaddr);
598
599
600
601
602
603void arm_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
604 vaddr addr, unsigned size,
605 MMUAccessType access_type,
606 int mmu_idx, MemTxAttrs attrs,
607 MemTxResult response, uintptr_t retaddr);
608
609
610static inline void arm_call_pre_el_change_hook(ARMCPU *cpu)
611{
612 ARMELChangeHook *hook, *next;
613 QLIST_FOREACH_SAFE(hook, &cpu->pre_el_change_hooks, node, next) {
614 hook->hook(cpu, hook->opaque);
615 }
616}
617static inline void arm_call_el_change_hook(ARMCPU *cpu)
618{
619 ARMELChangeHook *hook, *next;
620 QLIST_FOREACH_SAFE(hook, &cpu->el_change_hooks, node, next) {
621 hook->hook(cpu, hook->opaque);
622 }
623}
624
625
626static inline bool regime_has_2_ranges(ARMMMUIdx mmu_idx)
627{
628 switch (mmu_idx) {
629 case ARMMMUIdx_Stage1_E0:
630 case ARMMMUIdx_Stage1_E1:
631 case ARMMMUIdx_Stage1_E1_PAN:
632 case ARMMMUIdx_Stage1_SE0:
633 case ARMMMUIdx_Stage1_SE1:
634 case ARMMMUIdx_Stage1_SE1_PAN:
635 case ARMMMUIdx_E10_0:
636 case ARMMMUIdx_E10_1:
637 case ARMMMUIdx_E10_1_PAN:
638 case ARMMMUIdx_E20_0:
639 case ARMMMUIdx_E20_2:
640 case ARMMMUIdx_E20_2_PAN:
641 case ARMMMUIdx_SE10_0:
642 case ARMMMUIdx_SE10_1:
643 case ARMMMUIdx_SE10_1_PAN:
644 case ARMMMUIdx_SE20_0:
645 case ARMMMUIdx_SE20_2:
646 case ARMMMUIdx_SE20_2_PAN:
647 return true;
648 default:
649 return false;
650 }
651}
652
653
654static inline bool regime_is_secure(CPUARMState *env, ARMMMUIdx mmu_idx)
655{
656 switch (mmu_idx) {
657 case ARMMMUIdx_E10_0:
658 case ARMMMUIdx_E10_1:
659 case ARMMMUIdx_E10_1_PAN:
660 case ARMMMUIdx_E20_0:
661 case ARMMMUIdx_E20_2:
662 case ARMMMUIdx_E20_2_PAN:
663 case ARMMMUIdx_Stage1_E0:
664 case ARMMMUIdx_Stage1_E1:
665 case ARMMMUIdx_Stage1_E1_PAN:
666 case ARMMMUIdx_E2:
667 case ARMMMUIdx_Stage2:
668 case ARMMMUIdx_MPrivNegPri:
669 case ARMMMUIdx_MUserNegPri:
670 case ARMMMUIdx_MPriv:
671 case ARMMMUIdx_MUser:
672 return false;
673 case ARMMMUIdx_SE3:
674 case ARMMMUIdx_SE10_0:
675 case ARMMMUIdx_SE10_1:
676 case ARMMMUIdx_SE10_1_PAN:
677 case ARMMMUIdx_SE20_0:
678 case ARMMMUIdx_SE20_2:
679 case ARMMMUIdx_SE20_2_PAN:
680 case ARMMMUIdx_Stage1_SE0:
681 case ARMMMUIdx_Stage1_SE1:
682 case ARMMMUIdx_Stage1_SE1_PAN:
683 case ARMMMUIdx_SE2:
684 case ARMMMUIdx_Stage2_S:
685 case ARMMMUIdx_MSPrivNegPri:
686 case ARMMMUIdx_MSUserNegPri:
687 case ARMMMUIdx_MSPriv:
688 case ARMMMUIdx_MSUser:
689 return true;
690 default:
691 g_assert_not_reached();
692 }
693}
694
695static inline bool regime_is_pan(CPUARMState *env, ARMMMUIdx mmu_idx)
696{
697 switch (mmu_idx) {
698 case ARMMMUIdx_Stage1_E1_PAN:
699 case ARMMMUIdx_Stage1_SE1_PAN:
700 case ARMMMUIdx_E10_1_PAN:
701 case ARMMMUIdx_E20_2_PAN:
702 case ARMMMUIdx_SE10_1_PAN:
703 case ARMMMUIdx_SE20_2_PAN:
704 return true;
705 default:
706 return false;
707 }
708}
709
710
711static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
712{
713 switch (mmu_idx) {
714 case ARMMMUIdx_SE20_0:
715 case ARMMMUIdx_SE20_2:
716 case ARMMMUIdx_SE20_2_PAN:
717 case ARMMMUIdx_E20_0:
718 case ARMMMUIdx_E20_2:
719 case ARMMMUIdx_E20_2_PAN:
720 case ARMMMUIdx_Stage2:
721 case ARMMMUIdx_Stage2_S:
722 case ARMMMUIdx_SE2:
723 case ARMMMUIdx_E2:
724 return 2;
725 case ARMMMUIdx_SE3:
726 return 3;
727 case ARMMMUIdx_SE10_0:
728 case ARMMMUIdx_Stage1_SE0:
729 return arm_el_is_aa64(env, 3) ? 1 : 3;
730 case ARMMMUIdx_SE10_1:
731 case ARMMMUIdx_SE10_1_PAN:
732 case ARMMMUIdx_Stage1_E0:
733 case ARMMMUIdx_Stage1_E1:
734 case ARMMMUIdx_Stage1_E1_PAN:
735 case ARMMMUIdx_Stage1_SE1:
736 case ARMMMUIdx_Stage1_SE1_PAN:
737 case ARMMMUIdx_E10_0:
738 case ARMMMUIdx_E10_1:
739 case ARMMMUIdx_E10_1_PAN:
740 case ARMMMUIdx_MPrivNegPri:
741 case ARMMMUIdx_MUserNegPri:
742 case ARMMMUIdx_MPriv:
743 case ARMMMUIdx_MUser:
744 case ARMMMUIdx_MSPrivNegPri:
745 case ARMMMUIdx_MSUserNegPri:
746 case ARMMMUIdx_MSPriv:
747 case ARMMMUIdx_MSUser:
748 return 1;
749 default:
750 g_assert_not_reached();
751 }
752}
753
754
755static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
756{
757 if (mmu_idx == ARMMMUIdx_Stage2) {
758 return &env->cp15.vtcr_el2;
759 }
760 if (mmu_idx == ARMMMUIdx_Stage2_S) {
761
762
763
764
765 return &env->cp15.vstcr_el2;
766 }
767 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
768}
769
770
771
772
773static inline uint32_t arm_debug_exception_fsr(CPUARMState *env)
774{
775 ARMMMUFaultInfo fi = { .type = ARMFault_Debug };
776 int target_el = arm_debug_target_el(env);
777 bool using_lpae = false;
778
779 if (target_el == 2 || arm_el_is_aa64(env, target_el)) {
780 using_lpae = true;
781 } else {
782 if (arm_feature(env, ARM_FEATURE_LPAE) &&
783 (env->cp15.tcr_el[target_el].raw_tcr & TTBCR_EAE)) {
784 using_lpae = true;
785 }
786 }
787
788 if (using_lpae) {
789 return arm_fi_to_lfsc(&fi);
790 } else {
791 return arm_fi_to_sfsc(&fi);
792 }
793}
794
795
796
797
798
799
800static inline int arm_num_brps(ARMCPU *cpu)
801{
802 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
803 return FIELD_EX64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, BRPS) + 1;
804 } else {
805 return FIELD_EX32(cpu->isar.dbgdidr, DBGDIDR, BRPS) + 1;
806 }
807}
808
809
810
811
812
813
814static inline int arm_num_wrps(ARMCPU *cpu)
815{
816 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
817 return FIELD_EX64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, WRPS) + 1;
818 } else {
819 return FIELD_EX32(cpu->isar.dbgdidr, DBGDIDR, WRPS) + 1;
820 }
821}
822
823
824
825
826
827
828static inline int arm_num_ctx_cmps(ARMCPU *cpu)
829{
830 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
831 return FIELD_EX64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, CTX_CMPS) + 1;
832 } else {
833 return FIELD_EX32(cpu->isar.dbgdidr, DBGDIDR, CTX_CMPS) + 1;
834 }
835}
836
837
838
839
840
841
842static inline bool v7m_using_psp(CPUARMState *env)
843{
844
845
846
847
848
849 return !arm_v7m_is_handler_mode(env) &&
850 env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK;
851}
852
853
854
855
856
857
858static inline uint32_t v7m_sp_limit(CPUARMState *env)
859{
860 if (v7m_using_psp(env)) {
861 return env->v7m.psplim[env->v7m.secure];
862 } else {
863 return env->v7m.msplim[env->v7m.secure];
864 }
865}
866
867
868
869
870
871
872static inline bool v7m_cpacr_pass(CPUARMState *env,
873 bool is_secure, bool is_priv)
874{
875 switch (extract32(env->v7m.cpacr[is_secure], 20, 2)) {
876 case 0:
877 case 2:
878 return false;
879 case 1:
880 return is_priv;
881 case 3:
882 return true;
883 default:
884 g_assert_not_reached();
885 }
886}
887
888
889
890
891
892
893
894
895
896static inline const char *aarch32_mode_name(uint32_t psr)
897{
898 static const char cpu_mode_names[16][4] = {
899 "usr", "fiq", "irq", "svc", "???", "???", "mon", "abt",
900 "???", "???", "hyp", "und", "???", "???", "???", "sys"
901 };
902
903 return cpu_mode_names[psr & 0xf];
904}
905
906
907
908
909
910
911
912
913void arm_cpu_update_virq(ARMCPU *cpu);
914
915
916
917
918
919
920
921
922void arm_cpu_update_vfiq(ARMCPU *cpu);
923
924
925
926
927
928
929
930
931ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el);
932
933
934
935
936
937
938
939ARMMMUIdx arm_mmu_idx(CPUARMState *env);
940
941
942
943
944
945
946
947#ifdef CONFIG_USER_ONLY
948static inline ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
949{
950 return ARMMMUIdx_Stage1_E0;
951}
952#else
953ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env);
954#endif
955
956
957
958
959
960
961
962
963static inline bool arm_mmu_idx_is_stage1_of_2(ARMMMUIdx mmu_idx)
964{
965 switch (mmu_idx) {
966 case ARMMMUIdx_Stage1_E0:
967 case ARMMMUIdx_Stage1_E1:
968 case ARMMMUIdx_Stage1_E1_PAN:
969 case ARMMMUIdx_Stage1_SE0:
970 case ARMMMUIdx_Stage1_SE1:
971 case ARMMMUIdx_Stage1_SE1_PAN:
972 return true;
973 default:
974 return false;
975 }
976}
977
978static inline uint32_t aarch32_cpsr_valid_mask(uint64_t features,
979 const ARMISARegisters *id)
980{
981 uint32_t valid = CPSR_M | CPSR_AIF | CPSR_IL | CPSR_NZCV;
982
983 if ((features >> ARM_FEATURE_V4T) & 1) {
984 valid |= CPSR_T;
985 }
986 if ((features >> ARM_FEATURE_V5) & 1) {
987 valid |= CPSR_Q;
988 }
989 if ((features >> ARM_FEATURE_V6) & 1) {
990 valid |= CPSR_E | CPSR_GE;
991 }
992 if ((features >> ARM_FEATURE_THUMB2) & 1) {
993 valid |= CPSR_IT;
994 }
995 if (isar_feature_aa32_jazelle(id)) {
996 valid |= CPSR_J;
997 }
998 if (isar_feature_aa32_pan(id)) {
999 valid |= CPSR_PAN;
1000 }
1001 if (isar_feature_aa32_dit(id)) {
1002 valid |= CPSR_DIT;
1003 }
1004 if (isar_feature_aa32_ssbs(id)) {
1005 valid |= CPSR_SSBS;
1006 }
1007
1008 return valid;
1009}
1010
1011static inline uint32_t aarch64_pstate_valid_mask(const ARMISARegisters *id)
1012{
1013 uint32_t valid;
1014
1015 valid = PSTATE_M | PSTATE_DAIF | PSTATE_IL | PSTATE_SS | PSTATE_NZCV;
1016 if (isar_feature_aa64_bti(id)) {
1017 valid |= PSTATE_BTYPE;
1018 }
1019 if (isar_feature_aa64_pan(id)) {
1020 valid |= PSTATE_PAN;
1021 }
1022 if (isar_feature_aa64_uao(id)) {
1023 valid |= PSTATE_UAO;
1024 }
1025 if (isar_feature_aa64_dit(id)) {
1026 valid |= PSTATE_DIT;
1027 }
1028 if (isar_feature_aa64_ssbs(id)) {
1029 valid |= PSTATE_SSBS;
1030 }
1031 if (isar_feature_aa64_mte(id)) {
1032 valid |= PSTATE_TCO;
1033 }
1034
1035 return valid;
1036}
1037
1038
1039
1040
1041
1042typedef struct ARMVAParameters {
1043 unsigned tsz : 8;
1044 unsigned select : 1;
1045 bool tbi : 1;
1046 bool epd : 1;
1047 bool hpd : 1;
1048 bool using16k : 1;
1049 bool using64k : 1;
1050} ARMVAParameters;
1051
1052ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
1053 ARMMMUIdx mmu_idx, bool data);
1054
1055static inline int exception_target_el(CPUARMState *env)
1056{
1057 int target_el = MAX(1, arm_current_el(env));
1058
1059
1060
1061
1062
1063 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3) && target_el == 1) {
1064 target_el = 3;
1065 }
1066
1067 return target_el;
1068}
1069
1070
1071static inline bool allocation_tag_access_enabled(CPUARMState *env, int el,
1072 uint64_t sctlr)
1073{
1074 if (el < 3
1075 && arm_feature(env, ARM_FEATURE_EL3)
1076 && !(env->cp15.scr_el3 & SCR_ATA)) {
1077 return false;
1078 }
1079 if (el < 2 && arm_feature(env, ARM_FEATURE_EL2)) {
1080 uint64_t hcr = arm_hcr_el2_eff(env);
1081 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
1082 return false;
1083 }
1084 }
1085 sctlr &= (el == 0 ? SCTLR_ATA0 : SCTLR_ATA);
1086 return sctlr != 0;
1087}
1088
1089#ifndef CONFIG_USER_ONLY
1090
1091
1092typedef struct V8M_SAttributes {
1093 bool subpage;
1094 bool ns;
1095 bool nsc;
1096 uint8_t sregion;
1097 bool srvalid;
1098 uint8_t iregion;
1099 bool irvalid;
1100} V8M_SAttributes;
1101
1102void v8m_security_lookup(CPUARMState *env, uint32_t address,
1103 MMUAccessType access_type, ARMMMUIdx mmu_idx,
1104 V8M_SAttributes *sattrs);
1105
1106bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
1107 MMUAccessType access_type, ARMMMUIdx mmu_idx,
1108 hwaddr *phys_ptr, MemTxAttrs *txattrs,
1109 int *prot, bool *is_subpage,
1110 ARMMMUFaultInfo *fi, uint32_t *mregion);
1111
1112
1113typedef struct ARMCacheAttrs {
1114 unsigned int attrs:8;
1115 unsigned int shareability:2;
1116} ARMCacheAttrs;
1117
1118bool get_phys_addr(CPUARMState *env, target_ulong address,
1119 MMUAccessType access_type, ARMMMUIdx mmu_idx,
1120 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
1121 target_ulong *page_size,
1122 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
1123 __attribute__((nonnull));
1124
1125void arm_log_exception(int idx);
1126
1127#endif
1128
1129
1130
1131
1132
1133#define GMID_EL1_BS 6
1134
1135
1136#define LOG2_TAG_GRANULE 4
1137#define TAG_GRANULE (1 << LOG2_TAG_GRANULE)
1138
1139
1140
1141
1142
1143
1144FIELD(PREDDESC, OPRSZ, 0, 6)
1145FIELD(PREDDESC, ESZ, 6, 2)
1146FIELD(PREDDESC, DATA, 8, 24)
1147
1148
1149
1150
1151
1152#define SVE_MTEDESC_SHIFT 5
1153
1154
1155FIELD(MTEDESC, MIDX, 0, 4)
1156FIELD(MTEDESC, TBI, 4, 2)
1157FIELD(MTEDESC, TCMA, 6, 2)
1158FIELD(MTEDESC, WRITE, 8, 1)
1159FIELD(MTEDESC, SIZEM1, 9, SIMD_DATA_BITS - 9)
1160
1161bool mte_probe(CPUARMState *env, uint32_t desc, uint64_t ptr);
1162uint64_t mte_check(CPUARMState *env, uint32_t desc, uint64_t ptr, uintptr_t ra);
1163
1164static inline int allocation_tag_from_addr(uint64_t ptr)
1165{
1166 return extract64(ptr, 56, 4);
1167}
1168
1169static inline uint64_t address_with_allocation_tag(uint64_t ptr, int rtag)
1170{
1171 return deposit64(ptr, 56, 4, rtag);
1172}
1173
1174
1175static inline bool tbi_check(uint32_t desc, int bit55)
1176{
1177 return (desc >> (R_MTEDESC_TBI_SHIFT + bit55)) & 1;
1178}
1179
1180
1181static inline bool tcma_check(uint32_t desc, int bit55, int ptr_tag)
1182{
1183
1184
1185
1186
1187 bool match = ((ptr_tag + bit55) & 0xf) == 0;
1188 bool tcma = (desc >> (R_MTEDESC_TCMA_SHIFT + bit55)) & 1;
1189 return tcma && match;
1190}
1191
1192
1193
1194
1195
1196
1197
1198static inline uint64_t useronly_clean_ptr(uint64_t ptr)
1199{
1200#ifdef CONFIG_USER_ONLY
1201
1202 ptr &= sextract64(ptr, 0, 56);
1203#endif
1204 return ptr;
1205}
1206
1207static inline uint64_t useronly_maybe_clean_ptr(uint32_t desc, uint64_t ptr)
1208{
1209#ifdef CONFIG_USER_ONLY
1210 int64_t clean_ptr = sextract64(ptr, 0, 56);
1211 if (tbi_check(desc, clean_ptr < 0)) {
1212 ptr = clean_ptr;
1213 }
1214#endif
1215 return ptr;
1216}
1217
1218
1219bool generate_cache_maintenance(const ARMCPRegInfo *ri);
1220void cpu_clean_inv_one(CPUState *cpu, run_on_cpu_data d);
1221
1222
1223enum MVEECIState {
1224 ECI_NONE = 0,
1225 ECI_A0 = 1,
1226 ECI_A0A1 = 2,
1227
1228 ECI_A0A1A2 = 4,
1229 ECI_A0A1A2B0 = 5,
1230
1231};
1232
1233#endif
1234