xref: /qemu/target/arm/ptw.c (revision 513823e7521a09ed7ad1e32e6454bac3b2cbf52d)
1 /*
2  * ARM page table walking.
3  *
4  * This code is licensed under the GNU GPL v2 or later.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "qemu/osdep.h"
10 #include "qemu/log.h"
11 #include "qemu/range.h"
12 #include "qemu/main-loop.h"
13 #include "exec/exec-all.h"
14 #include "exec/page-protection.h"
15 #include "cpu.h"
16 #include "internals.h"
17 #include "cpu-features.h"
18 #include "idau.h"
19 
20 typedef struct S1Translate {
21     /*
22      * in_mmu_idx : specifies which TTBR, TCR, etc to use for the walk.
23      * Together with in_space, specifies the architectural translation regime.
24      */
25     ARMMMUIdx in_mmu_idx;
26     /*
27      * in_ptw_idx: specifies which mmuidx to use for the actual
28      * page table descriptor load operations. This will be one of the
29      * ARMMMUIdx_Stage2* or one of the ARMMMUIdx_Phys_* indexes.
30      * If a Secure ptw is "downgraded" to NonSecure by an NSTable bit,
31      * this field is updated accordingly.
32      */
33     ARMMMUIdx in_ptw_idx;
34     /*
35      * in_space: the security space for this walk. This plus
36      * the in_mmu_idx specify the architectural translation regime.
37      * If a Secure ptw is "downgraded" to NonSecure by an NSTable bit,
38      * this field is updated accordingly.
39      *
40      * Note that the security space for the in_ptw_idx may be different
41      * from that for the in_mmu_idx. We do not need to explicitly track
42      * the in_ptw_idx security space because:
43      *  - if the in_ptw_idx is an ARMMMUIdx_Phys_* then the mmuidx
44      *    itself specifies the security space
45      *  - if the in_ptw_idx is an ARMMMUIdx_Stage2* then the security
46      *    space used for ptw reads is the same as that of the security
47      *    space of the stage 1 translation for all cases except where
48      *    stage 1 is Secure; in that case the only possibilities for
49      *    the ptw read are Secure and NonSecure, and the in_ptw_idx
50      *    value being Stage2 vs Stage2_S distinguishes those.
51      */
52     ARMSecuritySpace in_space;
53     /*
54      * in_debug: is this a QEMU debug access (gdbstub, etc)? Debug
55      * accesses will not update the guest page table access flags
56      * and will not change the state of the softmmu TLBs.
57      */
58     bool in_debug;
59     /*
60      * If this is stage 2 of a stage 1+2 page table walk, then this must
61      * be true if stage 1 is an EL0 access; otherwise this is ignored.
62      * Stage 2 is indicated by in_mmu_idx set to ARMMMUIdx_Stage2{,_S}.
63      */
64     bool in_s1_is_el0;
65     bool out_rw;
66     bool out_be;
67     ARMSecuritySpace out_space;
68     hwaddr out_virt;
69     hwaddr out_phys;
70     void *out_host;
71 } S1Translate;
72 
73 static bool get_phys_addr_nogpc(CPUARMState *env, S1Translate *ptw,
74                                 vaddr address,
75                                 MMUAccessType access_type, MemOp memop,
76                                 GetPhysAddrResult *result,
77                                 ARMMMUFaultInfo *fi);
78 
79 static bool get_phys_addr_gpc(CPUARMState *env, S1Translate *ptw,
80                               vaddr address,
81                               MMUAccessType access_type, MemOp memop,
82                               GetPhysAddrResult *result,
83                               ARMMMUFaultInfo *fi);
84 
85 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
86                       int user_rw, int prot_rw, int xn, int pxn,
87                       ARMSecuritySpace in_pa, ARMSecuritySpace out_pa);
88 
89 /* This mapping is common between ID_AA64MMFR0.PARANGE and TCR_ELx.{I}PS. */
90 static const uint8_t pamax_map[] = {
91     [0] = 32,
92     [1] = 36,
93     [2] = 40,
94     [3] = 42,
95     [4] = 44,
96     [5] = 48,
97     [6] = 52,
98 };
99 
100 uint8_t round_down_to_parange_index(uint8_t bit_size)
101 {
102     for (int i = ARRAY_SIZE(pamax_map) - 1; i >= 0; i--) {
103         if (pamax_map[i] <= bit_size) {
104             return i;
105         }
106     }
107     g_assert_not_reached();
108 }
109 
110 uint8_t round_down_to_parange_bit_size(uint8_t bit_size)
111 {
112     return pamax_map[round_down_to_parange_index(bit_size)];
113 }
114 
115 /*
116  * The cpu-specific constant value of PAMax; also used by hw/arm/virt.
117  * Note that machvirt_init calls this on a CPU that is inited but not realized!
118  */
119 unsigned int arm_pamax(ARMCPU *cpu)
120 {
121     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
122         unsigned int parange =
123             FIELD_EX64(cpu->isar.id_aa64mmfr0, ID_AA64MMFR0, PARANGE);
124 
125         /*
126          * id_aa64mmfr0 is a read-only register so values outside of the
127          * supported mappings can be considered an implementation error.
128          */
129         assert(parange < ARRAY_SIZE(pamax_map));
130         return pamax_map[parange];
131     }
132 
133     if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
134         /* v7 or v8 with LPAE */
135         return 40;
136     }
137     /* Anything else */
138     return 32;
139 }
140 
141 /*
142  * Convert a possible stage1+2 MMU index into the appropriate stage 1 MMU index
143  */
144 ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
145 {
146     switch (mmu_idx) {
147     case ARMMMUIdx_E10_0:
148         return ARMMMUIdx_Stage1_E0;
149     case ARMMMUIdx_E10_1:
150         return ARMMMUIdx_Stage1_E1;
151     case ARMMMUIdx_E10_1_PAN:
152         return ARMMMUIdx_Stage1_E1_PAN;
153     default:
154         return mmu_idx;
155     }
156 }
157 
158 ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
159 {
160     return stage_1_mmu_idx(arm_mmu_idx(env));
161 }
162 
163 /*
164  * Return where we should do ptw loads from for a stage 2 walk.
165  * This depends on whether the address we are looking up is a
166  * Secure IPA or a NonSecure IPA, which we know from whether this is
167  * Stage2 or Stage2_S.
168  * If this is the Secure EL1&0 regime we need to check the NSW and SW bits.
169  */
170 static ARMMMUIdx ptw_idx_for_stage_2(CPUARMState *env, ARMMMUIdx stage2idx)
171 {
172     bool s2walk_secure;
173 
174     /*
175      * We're OK to check the current state of the CPU here because
176      * (1) we always invalidate all TLBs when the SCR_EL3.NS or SCR_EL3.NSE bit
177      * changes.
178      * (2) there's no way to do a lookup that cares about Stage 2 for a
179      * different security state to the current one for AArch64, and AArch32
180      * never has a secure EL2. (AArch32 ATS12NSO[UP][RW] allow EL3 to do
181      * an NS stage 1+2 lookup while the NS bit is 0.)
182      */
183     if (!arm_el_is_aa64(env, 3)) {
184         return ARMMMUIdx_Phys_NS;
185     }
186 
187     switch (arm_security_space_below_el3(env)) {
188     case ARMSS_NonSecure:
189         return ARMMMUIdx_Phys_NS;
190     case ARMSS_Realm:
191         return ARMMMUIdx_Phys_Realm;
192     case ARMSS_Secure:
193         if (stage2idx == ARMMMUIdx_Stage2_S) {
194             s2walk_secure = !(env->cp15.vstcr_el2 & VSTCR_SW);
195         } else {
196             s2walk_secure = !(env->cp15.vtcr_el2 & VTCR_NSW);
197         }
198         return s2walk_secure ? ARMMMUIdx_Phys_S : ARMMMUIdx_Phys_NS;
199     default:
200         g_assert_not_reached();
201     }
202 }
203 
204 static bool regime_translation_big_endian(CPUARMState *env, ARMMMUIdx mmu_idx)
205 {
206     return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
207 }
208 
209 /* Return the TTBR associated with this translation regime */
210 static uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx, int ttbrn)
211 {
212     if (mmu_idx == ARMMMUIdx_Stage2) {
213         return env->cp15.vttbr_el2;
214     }
215     if (mmu_idx == ARMMMUIdx_Stage2_S) {
216         return env->cp15.vsttbr_el2;
217     }
218     if (ttbrn == 0) {
219         return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
220     } else {
221         return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
222     }
223 }
224 
225 /* Return true if the specified stage of address translation is disabled */
226 static bool regime_translation_disabled(CPUARMState *env, ARMMMUIdx mmu_idx,
227                                         ARMSecuritySpace space)
228 {
229     uint64_t hcr_el2;
230 
231     if (arm_feature(env, ARM_FEATURE_M)) {
232         bool is_secure = arm_space_is_secure(space);
233         switch (env->v7m.mpu_ctrl[is_secure] &
234                 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
235         case R_V7M_MPU_CTRL_ENABLE_MASK:
236             /* Enabled, but not for HardFault and NMI */
237             return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
238         case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
239             /* Enabled for all cases */
240             return false;
241         case 0:
242         default:
243             /*
244              * HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
245              * we warned about that in armv7m_nvic.c when the guest set it.
246              */
247             return true;
248         }
249     }
250 
251 
252     switch (mmu_idx) {
253     case ARMMMUIdx_Stage2:
254     case ARMMMUIdx_Stage2_S:
255         /* HCR.DC means HCR.VM behaves as 1 */
256         hcr_el2 = arm_hcr_el2_eff_secstate(env, space);
257         return (hcr_el2 & (HCR_DC | HCR_VM)) == 0;
258 
259     case ARMMMUIdx_E10_0:
260     case ARMMMUIdx_E10_1:
261     case ARMMMUIdx_E10_1_PAN:
262         /* TGE means that EL0/1 act as if SCTLR_EL1.M is zero */
263         hcr_el2 = arm_hcr_el2_eff_secstate(env, space);
264         if (hcr_el2 & HCR_TGE) {
265             return true;
266         }
267         break;
268 
269     case ARMMMUIdx_Stage1_E0:
270     case ARMMMUIdx_Stage1_E1:
271     case ARMMMUIdx_Stage1_E1_PAN:
272         /* HCR.DC means SCTLR_EL1.M behaves as 0 */
273         hcr_el2 = arm_hcr_el2_eff_secstate(env, space);
274         if (hcr_el2 & HCR_DC) {
275             return true;
276         }
277         break;
278 
279     case ARMMMUIdx_E20_0:
280     case ARMMMUIdx_E20_2:
281     case ARMMMUIdx_E20_2_PAN:
282     case ARMMMUIdx_E2:
283     case ARMMMUIdx_E3:
284     case ARMMMUIdx_E30_0:
285     case ARMMMUIdx_E30_3_PAN:
286         break;
287 
288     case ARMMMUIdx_Phys_S:
289     case ARMMMUIdx_Phys_NS:
290     case ARMMMUIdx_Phys_Root:
291     case ARMMMUIdx_Phys_Realm:
292         /* No translation for physical address spaces. */
293         return true;
294 
295     default:
296         g_assert_not_reached();
297     }
298 
299     return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
300 }
301 
302 static bool granule_protection_check(CPUARMState *env, uint64_t paddress,
303                                      ARMSecuritySpace pspace,
304                                      ARMMMUFaultInfo *fi)
305 {
306     MemTxAttrs attrs = {
307         .secure = true,
308         .space = ARMSS_Root,
309     };
310     ARMCPU *cpu = env_archcpu(env);
311     uint64_t gpccr = env->cp15.gpccr_el3;
312     unsigned pps, pgs, l0gptsz, level = 0;
313     uint64_t tableaddr, pps_mask, align, entry, index;
314     AddressSpace *as;
315     MemTxResult result;
316     int gpi;
317 
318     if (!FIELD_EX64(gpccr, GPCCR, GPC)) {
319         return true;
320     }
321 
322     /*
323      * GPC Priority 1 (R_GMGRR):
324      * R_JWCSM: If the configuration of GPCCR_EL3 is invalid,
325      * the access fails as GPT walk fault at level 0.
326      */
327 
328     /*
329      * Configuration of PPS to a value exceeding the implemented
330      * physical address size is invalid.
331      */
332     pps = FIELD_EX64(gpccr, GPCCR, PPS);
333     if (pps > FIELD_EX64(cpu->isar.id_aa64mmfr0, ID_AA64MMFR0, PARANGE)) {
334         goto fault_walk;
335     }
336     pps = pamax_map[pps];
337     pps_mask = MAKE_64BIT_MASK(0, pps);
338 
339     switch (FIELD_EX64(gpccr, GPCCR, SH)) {
340     case 0b10: /* outer shareable */
341         break;
342     case 0b00: /* non-shareable */
343     case 0b11: /* inner shareable */
344         /* Inner and Outer non-cacheable requires Outer shareable. */
345         if (FIELD_EX64(gpccr, GPCCR, ORGN) == 0 &&
346             FIELD_EX64(gpccr, GPCCR, IRGN) == 0) {
347             goto fault_walk;
348         }
349         break;
350     default:   /* reserved */
351         goto fault_walk;
352     }
353 
354     switch (FIELD_EX64(gpccr, GPCCR, PGS)) {
355     case 0b00: /* 4KB */
356         pgs = 12;
357         break;
358     case 0b01: /* 64KB */
359         pgs = 16;
360         break;
361     case 0b10: /* 16KB */
362         pgs = 14;
363         break;
364     default: /* reserved */
365         goto fault_walk;
366     }
367 
368     /* Note this field is read-only and fixed at reset. */
369     l0gptsz = 30 + FIELD_EX64(gpccr, GPCCR, L0GPTSZ);
370 
371     /*
372      * GPC Priority 2: Secure, Realm or Root address exceeds PPS.
373      * R_CPDSB: A NonSecure physical address input exceeding PPS
374      * does not experience any fault.
375      */
376     if (paddress & ~pps_mask) {
377         if (pspace == ARMSS_NonSecure) {
378             return true;
379         }
380         goto fault_size;
381     }
382 
383     /* GPC Priority 3: the base address of GPTBR_EL3 exceeds PPS. */
384     tableaddr = env->cp15.gptbr_el3 << 12;
385     if (tableaddr & ~pps_mask) {
386         goto fault_size;
387     }
388 
389     /*
390      * BADDR is aligned per a function of PPS and L0GPTSZ.
391      * These bits of GPTBR_EL3 are RES0, but are not a configuration error,
392      * unlike the RES0 bits of the GPT entries (R_XNKFZ).
393      */
394     align = MAX(pps - l0gptsz + 3, 12);
395     align = MAKE_64BIT_MASK(0, align);
396     tableaddr &= ~align;
397 
398     as = arm_addressspace(env_cpu(env), attrs);
399 
400     /* Level 0 lookup. */
401     index = extract64(paddress, l0gptsz, pps - l0gptsz);
402     tableaddr += index * 8;
403     entry = address_space_ldq_le(as, tableaddr, attrs, &result);
404     if (result != MEMTX_OK) {
405         goto fault_eabt;
406     }
407 
408     switch (extract32(entry, 0, 4)) {
409     case 1: /* block descriptor */
410         if (entry >> 8) {
411             goto fault_walk; /* RES0 bits not 0 */
412         }
413         gpi = extract32(entry, 4, 4);
414         goto found;
415     case 3: /* table descriptor */
416         tableaddr = entry & ~0xf;
417         align = MAX(l0gptsz - pgs - 1, 12);
418         align = MAKE_64BIT_MASK(0, align);
419         if (tableaddr & (~pps_mask | align)) {
420             goto fault_walk; /* RES0 bits not 0 */
421         }
422         break;
423     default: /* invalid */
424         goto fault_walk;
425     }
426 
427     /* Level 1 lookup */
428     level = 1;
429     index = extract64(paddress, pgs + 4, l0gptsz - pgs - 4);
430     tableaddr += index * 8;
431     entry = address_space_ldq_le(as, tableaddr, attrs, &result);
432     if (result != MEMTX_OK) {
433         goto fault_eabt;
434     }
435 
436     switch (extract32(entry, 0, 4)) {
437     case 1: /* contiguous descriptor */
438         if (entry >> 10) {
439             goto fault_walk; /* RES0 bits not 0 */
440         }
441         /*
442          * Because the softmmu tlb only works on units of TARGET_PAGE_SIZE,
443          * and because we cannot invalidate by pa, and thus will always
444          * flush entire tlbs, we don't actually care about the range here
445          * and can simply extract the GPI as the result.
446          */
447         if (extract32(entry, 8, 2) == 0) {
448             goto fault_walk; /* reserved contig */
449         }
450         gpi = extract32(entry, 4, 4);
451         break;
452     default:
453         index = extract64(paddress, pgs, 4);
454         gpi = extract64(entry, index * 4, 4);
455         break;
456     }
457 
458  found:
459     switch (gpi) {
460     case 0b0000: /* no access */
461         break;
462     case 0b1111: /* all access */
463         return true;
464     case 0b1000:
465     case 0b1001:
466     case 0b1010:
467     case 0b1011:
468         if (pspace == (gpi & 3)) {
469             return true;
470         }
471         break;
472     default:
473         goto fault_walk; /* reserved */
474     }
475 
476     fi->gpcf = GPCF_Fail;
477     goto fault_common;
478  fault_eabt:
479     fi->gpcf = GPCF_EABT;
480     goto fault_common;
481  fault_size:
482     fi->gpcf = GPCF_AddressSize;
483     goto fault_common;
484  fault_walk:
485     fi->gpcf = GPCF_Walk;
486  fault_common:
487     fi->level = level;
488     fi->paddr = paddress;
489     fi->paddr_space = pspace;
490     return false;
491 }
492 
493 static bool S1_attrs_are_device(uint8_t attrs)
494 {
495     /*
496      * This slightly under-decodes the MAIR_ELx field:
497      * 0b0000dd01 is Device with FEAT_XS, otherwise UNPREDICTABLE;
498      * 0b0000dd1x is UNPREDICTABLE.
499      */
500     return (attrs & 0xf0) == 0;
501 }
502 
503 static bool S2_attrs_are_device(uint64_t hcr, uint8_t attrs)
504 {
505     /*
506      * For an S1 page table walk, the stage 1 attributes are always
507      * some form of "this is Normal memory". The combined S1+S2
508      * attributes are therefore only Device if stage 2 specifies Device.
509      * With HCR_EL2.FWB == 0 this is when descriptor bits [5:4] are 0b00,
510      * ie when cacheattrs.attrs bits [3:2] are 0b00.
511      * With HCR_EL2.FWB == 1 this is when descriptor bit [4] is 0, ie
512      * when cacheattrs.attrs bit [2] is 0.
513      */
514     if (hcr & HCR_FWB) {
515         return (attrs & 0x4) == 0;
516     } else {
517         return (attrs & 0xc) == 0;
518     }
519 }
520 
521 static ARMSecuritySpace S2_security_space(ARMSecuritySpace s1_space,
522                                           ARMMMUIdx s2_mmu_idx)
523 {
524     /*
525      * Return the security space to use for stage 2 when doing
526      * the S1 page table descriptor load.
527      */
528     if (regime_is_stage2(s2_mmu_idx)) {
529         /*
530          * The security space for ptw reads is almost always the same
531          * as that of the security space of the stage 1 translation.
532          * The only exception is when stage 1 is Secure; in that case
533          * the ptw read might be to the Secure or the NonSecure space
534          * (but never Realm or Root), and the s2_mmu_idx tells us which.
535          * Root translations are always single-stage.
536          */
537         if (s1_space == ARMSS_Secure) {
538             return arm_secure_to_space(s2_mmu_idx == ARMMMUIdx_Stage2_S);
539         } else {
540             assert(s2_mmu_idx != ARMMMUIdx_Stage2_S);
541             assert(s1_space != ARMSS_Root);
542             return s1_space;
543         }
544     } else {
545         /* ptw loads are from phys: the mmu idx itself says which space */
546         return arm_phys_to_space(s2_mmu_idx);
547     }
548 }
549 
550 static bool fault_s1ns(ARMSecuritySpace space, ARMMMUIdx s2_mmu_idx)
551 {
552     /*
553      * For stage 2 faults in Secure EL22, S1NS indicates
554      * whether the faulting IPA is in the Secure or NonSecure
555      * IPA space. For all other kinds of fault, it is false.
556      */
557     return space == ARMSS_Secure && regime_is_stage2(s2_mmu_idx)
558         && s2_mmu_idx == ARMMMUIdx_Stage2_S;
559 }
560 
561 /* Translate a S1 pagetable walk through S2 if needed.  */
562 static bool S1_ptw_translate(CPUARMState *env, S1Translate *ptw,
563                              hwaddr addr, ARMMMUFaultInfo *fi)
564 {
565     ARMMMUIdx mmu_idx = ptw->in_mmu_idx;
566     ARMMMUIdx s2_mmu_idx = ptw->in_ptw_idx;
567     uint8_t pte_attrs;
568 
569     ptw->out_virt = addr;
570 
571     if (unlikely(ptw->in_debug)) {
572         /*
573          * From gdbstub, do not use softmmu so that we don't modify the
574          * state of the cpu at all, including softmmu tlb contents.
575          */
576         ARMSecuritySpace s2_space = S2_security_space(ptw->in_space, s2_mmu_idx);
577         S1Translate s2ptw = {
578             .in_mmu_idx = s2_mmu_idx,
579             .in_ptw_idx = ptw_idx_for_stage_2(env, s2_mmu_idx),
580             .in_space = s2_space,
581             .in_debug = true,
582         };
583         GetPhysAddrResult s2 = { };
584 
585         if (get_phys_addr_gpc(env, &s2ptw, addr, MMU_DATA_LOAD, 0, &s2, fi)) {
586             goto fail;
587         }
588 
589         ptw->out_phys = s2.f.phys_addr;
590         pte_attrs = s2.cacheattrs.attrs;
591         ptw->out_host = NULL;
592         ptw->out_rw = false;
593         ptw->out_space = s2.f.attrs.space;
594     } else {
595 #ifdef CONFIG_TCG
596         CPUTLBEntryFull *full;
597         int flags;
598 
599         env->tlb_fi = fi;
600         flags = probe_access_full_mmu(env, addr, 0, MMU_DATA_LOAD,
601                                       arm_to_core_mmu_idx(s2_mmu_idx),
602                                       &ptw->out_host, &full);
603         env->tlb_fi = NULL;
604 
605         if (unlikely(flags & TLB_INVALID_MASK)) {
606             goto fail;
607         }
608         ptw->out_phys = full->phys_addr | (addr & ~TARGET_PAGE_MASK);
609         ptw->out_rw = full->prot & PAGE_WRITE;
610         pte_attrs = full->extra.arm.pte_attrs;
611         ptw->out_space = full->attrs.space;
612 #else
613         g_assert_not_reached();
614 #endif
615     }
616 
617     if (regime_is_stage2(s2_mmu_idx)) {
618         uint64_t hcr = arm_hcr_el2_eff_secstate(env, ptw->in_space);
619 
620         if ((hcr & HCR_PTW) && S2_attrs_are_device(hcr, pte_attrs)) {
621             /*
622              * PTW set and S1 walk touched S2 Device memory:
623              * generate Permission fault.
624              */
625             fi->type = ARMFault_Permission;
626             fi->s2addr = addr;
627             fi->stage2 = true;
628             fi->s1ptw = true;
629             fi->s1ns = fault_s1ns(ptw->in_space, s2_mmu_idx);
630             return false;
631         }
632     }
633 
634     ptw->out_be = regime_translation_big_endian(env, mmu_idx);
635     return true;
636 
637  fail:
638     assert(fi->type != ARMFault_None);
639     if (fi->type == ARMFault_GPCFOnOutput) {
640         fi->type = ARMFault_GPCFOnWalk;
641     }
642     fi->s2addr = addr;
643     fi->stage2 = regime_is_stage2(s2_mmu_idx);
644     fi->s1ptw = fi->stage2;
645     fi->s1ns = fault_s1ns(ptw->in_space, s2_mmu_idx);
646     return false;
647 }
648 
649 /* All loads done in the course of a page table walk go through here. */
650 static uint32_t arm_ldl_ptw(CPUARMState *env, S1Translate *ptw,
651                             ARMMMUFaultInfo *fi)
652 {
653     CPUState *cs = env_cpu(env);
654     void *host = ptw->out_host;
655     uint32_t data;
656 
657     if (likely(host)) {
658         /* Page tables are in RAM, and we have the host address. */
659         data = qatomic_read((uint32_t *)host);
660         if (ptw->out_be) {
661             data = be32_to_cpu(data);
662         } else {
663             data = le32_to_cpu(data);
664         }
665     } else {
666         /* Page tables are in MMIO. */
667         MemTxAttrs attrs = {
668             .space = ptw->out_space,
669             .secure = arm_space_is_secure(ptw->out_space),
670         };
671         AddressSpace *as = arm_addressspace(cs, attrs);
672         MemTxResult result = MEMTX_OK;
673 
674         if (ptw->out_be) {
675             data = address_space_ldl_be(as, ptw->out_phys, attrs, &result);
676         } else {
677             data = address_space_ldl_le(as, ptw->out_phys, attrs, &result);
678         }
679         if (unlikely(result != MEMTX_OK)) {
680             fi->type = ARMFault_SyncExternalOnWalk;
681             fi->ea = arm_extabort_type(result);
682             return 0;
683         }
684     }
685     return data;
686 }
687 
688 static uint64_t arm_ldq_ptw(CPUARMState *env, S1Translate *ptw,
689                             ARMMMUFaultInfo *fi)
690 {
691     CPUState *cs = env_cpu(env);
692     void *host = ptw->out_host;
693     uint64_t data;
694 
695     if (likely(host)) {
696         /* Page tables are in RAM, and we have the host address. */
697 #ifdef CONFIG_ATOMIC64
698         data = qatomic_read__nocheck((uint64_t *)host);
699         if (ptw->out_be) {
700             data = be64_to_cpu(data);
701         } else {
702             data = le64_to_cpu(data);
703         }
704 #else
705         if (ptw->out_be) {
706             data = ldq_be_p(host);
707         } else {
708             data = ldq_le_p(host);
709         }
710 #endif
711     } else {
712         /* Page tables are in MMIO. */
713         MemTxAttrs attrs = {
714             .space = ptw->out_space,
715             .secure = arm_space_is_secure(ptw->out_space),
716         };
717         AddressSpace *as = arm_addressspace(cs, attrs);
718         MemTxResult result = MEMTX_OK;
719 
720         if (ptw->out_be) {
721             data = address_space_ldq_be(as, ptw->out_phys, attrs, &result);
722         } else {
723             data = address_space_ldq_le(as, ptw->out_phys, attrs, &result);
724         }
725         if (unlikely(result != MEMTX_OK)) {
726             fi->type = ARMFault_SyncExternalOnWalk;
727             fi->ea = arm_extabort_type(result);
728             return 0;
729         }
730     }
731     return data;
732 }
733 
734 static uint64_t arm_casq_ptw(CPUARMState *env, uint64_t old_val,
735                              uint64_t new_val, S1Translate *ptw,
736                              ARMMMUFaultInfo *fi)
737 {
738 #if defined(TARGET_AARCH64) && defined(CONFIG_TCG)
739     uint64_t cur_val;
740     void *host = ptw->out_host;
741 
742     if (unlikely(!host)) {
743         /* Page table in MMIO Memory Region */
744         CPUState *cs = env_cpu(env);
745         MemTxAttrs attrs = {
746             .space = ptw->out_space,
747             .secure = arm_space_is_secure(ptw->out_space),
748         };
749         AddressSpace *as = arm_addressspace(cs, attrs);
750         MemTxResult result = MEMTX_OK;
751         bool need_lock = !bql_locked();
752 
753         if (need_lock) {
754             bql_lock();
755         }
756         if (ptw->out_be) {
757             cur_val = address_space_ldq_be(as, ptw->out_phys, attrs, &result);
758             if (unlikely(result != MEMTX_OK)) {
759                 fi->type = ARMFault_SyncExternalOnWalk;
760                 fi->ea = arm_extabort_type(result);
761                 if (need_lock) {
762                     bql_unlock();
763                 }
764                 return old_val;
765             }
766             if (cur_val == old_val) {
767                 address_space_stq_be(as, ptw->out_phys, new_val, attrs, &result);
768                 if (unlikely(result != MEMTX_OK)) {
769                     fi->type = ARMFault_SyncExternalOnWalk;
770                     fi->ea = arm_extabort_type(result);
771                     if (need_lock) {
772                         bql_unlock();
773                     }
774                     return old_val;
775                 }
776                 cur_val = new_val;
777             }
778         } else {
779             cur_val = address_space_ldq_le(as, ptw->out_phys, attrs, &result);
780             if (unlikely(result != MEMTX_OK)) {
781                 fi->type = ARMFault_SyncExternalOnWalk;
782                 fi->ea = arm_extabort_type(result);
783                 if (need_lock) {
784                     bql_unlock();
785                 }
786                 return old_val;
787             }
788             if (cur_val == old_val) {
789                 address_space_stq_le(as, ptw->out_phys, new_val, attrs, &result);
790                 if (unlikely(result != MEMTX_OK)) {
791                     fi->type = ARMFault_SyncExternalOnWalk;
792                     fi->ea = arm_extabort_type(result);
793                     if (need_lock) {
794                         bql_unlock();
795                     }
796                     return old_val;
797                 }
798                 cur_val = new_val;
799             }
800         }
801         if (need_lock) {
802             bql_unlock();
803         }
804         return cur_val;
805     }
806 
807     /*
808      * Raising a stage2 Protection fault for an atomic update to a read-only
809      * page is delayed until it is certain that there is a change to make.
810      */
811     if (unlikely(!ptw->out_rw)) {
812         int flags;
813 
814         env->tlb_fi = fi;
815         flags = probe_access_full_mmu(env, ptw->out_virt, 0,
816                                       MMU_DATA_STORE,
817                                       arm_to_core_mmu_idx(ptw->in_ptw_idx),
818                                       NULL, NULL);
819         env->tlb_fi = NULL;
820 
821         if (unlikely(flags & TLB_INVALID_MASK)) {
822             /*
823              * We know this must be a stage 2 fault because the granule
824              * protection table does not separately track read and write
825              * permission, so all GPC faults are caught in S1_ptw_translate():
826              * we only get here for "readable but not writeable".
827              */
828             assert(fi->type != ARMFault_None);
829             fi->s2addr = ptw->out_virt;
830             fi->stage2 = true;
831             fi->s1ptw = true;
832             fi->s1ns = fault_s1ns(ptw->in_space, ptw->in_ptw_idx);
833             return 0;
834         }
835 
836         /* In case CAS mismatches and we loop, remember writability. */
837         ptw->out_rw = true;
838     }
839 
840     if (ptw->out_be) {
841         old_val = cpu_to_be64(old_val);
842         new_val = cpu_to_be64(new_val);
843         cur_val = qatomic_cmpxchg__nocheck((uint64_t *)host, old_val, new_val);
844         cur_val = be64_to_cpu(cur_val);
845     } else {
846         old_val = cpu_to_le64(old_val);
847         new_val = cpu_to_le64(new_val);
848         cur_val = qatomic_cmpxchg__nocheck((uint64_t *)host, old_val, new_val);
849         cur_val = le64_to_cpu(cur_val);
850     }
851     return cur_val;
852 #else
853     /* AArch32 does not have FEAT_HADFS; non-TCG guests only use debug-mode. */
854     g_assert_not_reached();
855 #endif
856 }
857 
858 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
859                                      uint32_t *table, uint32_t address)
860 {
861     /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
862     uint64_t tcr = regime_tcr(env, mmu_idx);
863     int maskshift = extract32(tcr, 0, 3);
864     uint32_t mask = ~(((uint32_t)0xffffffffu) >> maskshift);
865     uint32_t base_mask;
866 
867     if (address & mask) {
868         if (tcr & TTBCR_PD1) {
869             /* Translation table walk disabled for TTBR1 */
870             return false;
871         }
872         *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
873     } else {
874         if (tcr & TTBCR_PD0) {
875             /* Translation table walk disabled for TTBR0 */
876             return false;
877         }
878         base_mask = ~((uint32_t)0x3fffu >> maskshift);
879         *table = regime_ttbr(env, mmu_idx, 0) & base_mask;
880     }
881     *table |= (address >> 18) & 0x3ffc;
882     return true;
883 }
884 
885 /*
886  * Translate section/page access permissions to page R/W protection flags
887  * @env:         CPUARMState
888  * @mmu_idx:     MMU index indicating required translation regime
889  * @ap:          The 3-bit access permissions (AP[2:0])
890  * @domain_prot: The 2-bit domain access permissions
891  * @is_user: TRUE if accessing from PL0
892  */
893 static int ap_to_rw_prot_is_user(CPUARMState *env, ARMMMUIdx mmu_idx,
894                          int ap, int domain_prot, bool is_user)
895 {
896     if (domain_prot == 3) {
897         return PAGE_READ | PAGE_WRITE;
898     }
899 
900     switch (ap) {
901     case 0:
902         if (arm_feature(env, ARM_FEATURE_V7)) {
903             return 0;
904         }
905         switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
906         case SCTLR_S:
907             return is_user ? 0 : PAGE_READ;
908         case SCTLR_R:
909             return PAGE_READ;
910         default:
911             return 0;
912         }
913     case 1:
914         return is_user ? 0 : PAGE_READ | PAGE_WRITE;
915     case 2:
916         if (is_user) {
917             return PAGE_READ;
918         } else {
919             return PAGE_READ | PAGE_WRITE;
920         }
921     case 3:
922         return PAGE_READ | PAGE_WRITE;
923     case 4: /* Reserved.  */
924         return 0;
925     case 5:
926         return is_user ? 0 : PAGE_READ;
927     case 6:
928         return PAGE_READ;
929     case 7:
930         if (!arm_feature(env, ARM_FEATURE_V6K)) {
931             return 0;
932         }
933         return PAGE_READ;
934     default:
935         g_assert_not_reached();
936     }
937 }
938 
939 /*
940  * Translate section/page access permissions to page R/W protection flags
941  * @env:         CPUARMState
942  * @mmu_idx:     MMU index indicating required translation regime
943  * @ap:          The 3-bit access permissions (AP[2:0])
944  * @domain_prot: The 2-bit domain access permissions
945  */
946 static int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
947                          int ap, int domain_prot)
948 {
949    return ap_to_rw_prot_is_user(env, mmu_idx, ap, domain_prot,
950                                 regime_is_user(env, mmu_idx));
951 }
952 
953 /*
954  * Translate section/page access permissions to page R/W protection flags.
955  * @ap:      The 2-bit simple AP (AP[2:1])
956  * @is_user: TRUE if accessing from PL0
957  */
958 static int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
959 {
960     switch (ap) {
961     case 0:
962         return is_user ? 0 : PAGE_READ | PAGE_WRITE;
963     case 1:
964         return PAGE_READ | PAGE_WRITE;
965     case 2:
966         return is_user ? 0 : PAGE_READ;
967     case 3:
968         return PAGE_READ;
969     default:
970         g_assert_not_reached();
971     }
972 }
973 
974 static int simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
975 {
976     return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
977 }
978 
979 static bool get_phys_addr_v5(CPUARMState *env, S1Translate *ptw,
980                              uint32_t address, MMUAccessType access_type,
981                              GetPhysAddrResult *result, ARMMMUFaultInfo *fi)
982 {
983     int level = 1;
984     uint32_t table;
985     uint32_t desc;
986     int type;
987     int ap;
988     int domain = 0;
989     int domain_prot;
990     hwaddr phys_addr;
991     uint32_t dacr;
992 
993     /* Pagetable walk.  */
994     /* Lookup l1 descriptor.  */
995     if (!get_level1_table_address(env, ptw->in_mmu_idx, &table, address)) {
996         /* Section translation fault if page walk is disabled by PD0 or PD1 */
997         fi->type = ARMFault_Translation;
998         goto do_fault;
999     }
1000     if (!S1_ptw_translate(env, ptw, table, fi)) {
1001         goto do_fault;
1002     }
1003     desc = arm_ldl_ptw(env, ptw, fi);
1004     if (fi->type != ARMFault_None) {
1005         goto do_fault;
1006     }
1007     type = (desc & 3);
1008     domain = (desc >> 5) & 0x0f;
1009     if (regime_el(env, ptw->in_mmu_idx) == 1) {
1010         dacr = env->cp15.dacr_ns;
1011     } else {
1012         dacr = env->cp15.dacr_s;
1013     }
1014     domain_prot = (dacr >> (domain * 2)) & 3;
1015     if (type == 0) {
1016         /* Section translation fault.  */
1017         fi->type = ARMFault_Translation;
1018         goto do_fault;
1019     }
1020     if (type != 2) {
1021         level = 2;
1022     }
1023     if (domain_prot == 0 || domain_prot == 2) {
1024         fi->type = ARMFault_Domain;
1025         goto do_fault;
1026     }
1027     if (type == 2) {
1028         /* 1Mb section.  */
1029         phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1030         ap = (desc >> 10) & 3;
1031         result->f.lg_page_size = 20; /* 1MB */
1032     } else {
1033         /* Lookup l2 entry.  */
1034         if (type == 1) {
1035             /* Coarse pagetable.  */
1036             table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1037         } else {
1038             /* Fine pagetable.  */
1039             table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
1040         }
1041         if (!S1_ptw_translate(env, ptw, table, fi)) {
1042             goto do_fault;
1043         }
1044         desc = arm_ldl_ptw(env, ptw, fi);
1045         if (fi->type != ARMFault_None) {
1046             goto do_fault;
1047         }
1048         switch (desc & 3) {
1049         case 0: /* Page translation fault.  */
1050             fi->type = ARMFault_Translation;
1051             goto do_fault;
1052         case 1: /* 64k page.  */
1053             phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1054             ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
1055             result->f.lg_page_size = 16;
1056             break;
1057         case 2: /* 4k page.  */
1058             phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1059             ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
1060             result->f.lg_page_size = 12;
1061             break;
1062         case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
1063             if (type == 1) {
1064                 /* ARMv6/XScale extended small page format */
1065                 if (arm_feature(env, ARM_FEATURE_XSCALE)
1066                     || arm_feature(env, ARM_FEATURE_V6)) {
1067                     phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1068                     result->f.lg_page_size = 12;
1069                 } else {
1070                     /*
1071                      * UNPREDICTABLE in ARMv5; we choose to take a
1072                      * page translation fault.
1073                      */
1074                     fi->type = ARMFault_Translation;
1075                     goto do_fault;
1076                 }
1077             } else {
1078                 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
1079                 result->f.lg_page_size = 10;
1080             }
1081             ap = (desc >> 4) & 3;
1082             break;
1083         default:
1084             /* Never happens, but compiler isn't smart enough to tell.  */
1085             g_assert_not_reached();
1086         }
1087     }
1088     result->f.prot = ap_to_rw_prot(env, ptw->in_mmu_idx, ap, domain_prot);
1089     result->f.prot |= result->f.prot ? PAGE_EXEC : 0;
1090     if (!(result->f.prot & (1 << access_type))) {
1091         /* Access permission fault.  */
1092         fi->type = ARMFault_Permission;
1093         goto do_fault;
1094     }
1095     result->f.phys_addr = phys_addr;
1096     return false;
1097 do_fault:
1098     fi->domain = domain;
1099     fi->level = level;
1100     return true;
1101 }
1102 
1103 static bool get_phys_addr_v6(CPUARMState *env, S1Translate *ptw,
1104                              uint32_t address, MMUAccessType access_type,
1105                              GetPhysAddrResult *result, ARMMMUFaultInfo *fi)
1106 {
1107     ARMCPU *cpu = env_archcpu(env);
1108     ARMMMUIdx mmu_idx = ptw->in_mmu_idx;
1109     int level = 1;
1110     uint32_t table;
1111     uint32_t desc;
1112     uint32_t xn;
1113     uint32_t pxn = 0;
1114     int type;
1115     int ap;
1116     int domain = 0;
1117     int domain_prot;
1118     hwaddr phys_addr;
1119     uint32_t dacr;
1120     bool ns;
1121     ARMSecuritySpace out_space;
1122 
1123     /* Pagetable walk.  */
1124     /* Lookup l1 descriptor.  */
1125     if (!get_level1_table_address(env, mmu_idx, &table, address)) {
1126         /* Section translation fault if page walk is disabled by PD0 or PD1 */
1127         fi->type = ARMFault_Translation;
1128         goto do_fault;
1129     }
1130     if (!S1_ptw_translate(env, ptw, table, fi)) {
1131         goto do_fault;
1132     }
1133     desc = arm_ldl_ptw(env, ptw, fi);
1134     if (fi->type != ARMFault_None) {
1135         goto do_fault;
1136     }
1137     type = (desc & 3);
1138     if (type == 0 || (type == 3 && !cpu_isar_feature(aa32_pxn, cpu))) {
1139         /* Section translation fault, or attempt to use the encoding
1140          * which is Reserved on implementations without PXN.
1141          */
1142         fi->type = ARMFault_Translation;
1143         goto do_fault;
1144     }
1145     if ((type == 1) || !(desc & (1 << 18))) {
1146         /* Page or Section.  */
1147         domain = (desc >> 5) & 0x0f;
1148     }
1149     if (regime_el(env, mmu_idx) == 1) {
1150         dacr = env->cp15.dacr_ns;
1151     } else {
1152         dacr = env->cp15.dacr_s;
1153     }
1154     if (type == 1) {
1155         level = 2;
1156     }
1157     domain_prot = (dacr >> (domain * 2)) & 3;
1158     if (domain_prot == 0 || domain_prot == 2) {
1159         /* Section or Page domain fault */
1160         fi->type = ARMFault_Domain;
1161         goto do_fault;
1162     }
1163     if (type != 1) {
1164         if (desc & (1 << 18)) {
1165             /* Supersection.  */
1166             phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
1167             phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
1168             phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
1169             result->f.lg_page_size = 24;  /* 16MB */
1170         } else {
1171             /* Section.  */
1172             phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1173             result->f.lg_page_size = 20;  /* 1MB */
1174         }
1175         ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
1176         xn = desc & (1 << 4);
1177         pxn = desc & 1;
1178         ns = extract32(desc, 19, 1);
1179     } else {
1180         if (cpu_isar_feature(aa32_pxn, cpu)) {
1181             pxn = (desc >> 2) & 1;
1182         }
1183         ns = extract32(desc, 3, 1);
1184         /* Lookup l2 entry.  */
1185         table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1186         if (!S1_ptw_translate(env, ptw, table, fi)) {
1187             goto do_fault;
1188         }
1189         desc = arm_ldl_ptw(env, ptw, fi);
1190         if (fi->type != ARMFault_None) {
1191             goto do_fault;
1192         }
1193         ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1194         switch (desc & 3) {
1195         case 0: /* Page translation fault.  */
1196             fi->type = ARMFault_Translation;
1197             goto do_fault;
1198         case 1: /* 64k page.  */
1199             phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1200             xn = desc & (1 << 15);
1201             result->f.lg_page_size = 16;
1202             break;
1203         case 2: case 3: /* 4k page.  */
1204             phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1205             xn = desc & 1;
1206             result->f.lg_page_size = 12;
1207             break;
1208         default:
1209             /* Never happens, but compiler isn't smart enough to tell.  */
1210             g_assert_not_reached();
1211         }
1212     }
1213     out_space = ptw->in_space;
1214     if (ns) {
1215         /*
1216          * The NS bit will (as required by the architecture) have no effect if
1217          * the CPU doesn't support TZ or this is a non-secure translation
1218          * regime, because the output space will already be non-secure.
1219          */
1220         out_space = ARMSS_NonSecure;
1221     }
1222     if (domain_prot == 3) {
1223         result->f.prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1224     } else {
1225         int user_rw, prot_rw;
1226 
1227         if (arm_feature(env, ARM_FEATURE_V6K) &&
1228                 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
1229             /* The simplified model uses AP[0] as an access control bit.  */
1230             if ((ap & 1) == 0) {
1231                 /* Access flag fault.  */
1232                 fi->type = ARMFault_AccessFlag;
1233                 goto do_fault;
1234             }
1235             prot_rw = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
1236             user_rw = simple_ap_to_rw_prot_is_user(ap >> 1, 1);
1237         } else {
1238             prot_rw = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
1239             user_rw = ap_to_rw_prot_is_user(env, mmu_idx, ap, domain_prot, 1);
1240         }
1241 
1242         result->f.prot = get_S1prot(env, mmu_idx, false, user_rw, prot_rw,
1243                                     xn, pxn, result->f.attrs.space, out_space);
1244         if (!(result->f.prot & (1 << access_type))) {
1245             /* Access permission fault.  */
1246             fi->type = ARMFault_Permission;
1247             goto do_fault;
1248         }
1249     }
1250     result->f.attrs.space = out_space;
1251     result->f.attrs.secure = arm_space_is_secure(out_space);
1252     result->f.phys_addr = phys_addr;
1253     return false;
1254 do_fault:
1255     fi->domain = domain;
1256     fi->level = level;
1257     return true;
1258 }
1259 
1260 /*
1261  * Translate S2 section/page access permissions to protection flags
1262  * @env:     CPUARMState
1263  * @s2ap:    The 2-bit stage2 access permissions (S2AP)
1264  * @xn:      XN (execute-never) bits
1265  * @s1_is_el0: true if this is S2 of an S1+2 walk for EL0
1266  */
1267 static int get_S2prot_noexecute(int s2ap)
1268 {
1269     int prot = 0;
1270 
1271     if (s2ap & 1) {
1272         prot |= PAGE_READ;
1273     }
1274     if (s2ap & 2) {
1275         prot |= PAGE_WRITE;
1276     }
1277     return prot;
1278 }
1279 
1280 static int get_S2prot(CPUARMState *env, int s2ap, int xn, bool s1_is_el0)
1281 {
1282     int prot = get_S2prot_noexecute(s2ap);
1283 
1284     if (cpu_isar_feature(any_tts2uxn, env_archcpu(env))) {
1285         switch (xn) {
1286         case 0:
1287             prot |= PAGE_EXEC;
1288             break;
1289         case 1:
1290             if (s1_is_el0) {
1291                 prot |= PAGE_EXEC;
1292             }
1293             break;
1294         case 2:
1295             break;
1296         case 3:
1297             if (!s1_is_el0) {
1298                 prot |= PAGE_EXEC;
1299             }
1300             break;
1301         default:
1302             g_assert_not_reached();
1303         }
1304     } else {
1305         if (!extract32(xn, 1, 1)) {
1306             if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
1307                 prot |= PAGE_EXEC;
1308             }
1309         }
1310     }
1311     return prot;
1312 }
1313 
1314 /*
1315  * Translate section/page access permissions to protection flags
1316  * @env:     CPUARMState
1317  * @mmu_idx: MMU index indicating required translation regime
1318  * @is_aa64: TRUE if AArch64
1319  * @user_rw: Translated AP for user access
1320  * @prot_rw: Translated AP for privileged access
1321  * @xn:      XN (execute-never) bit
1322  * @pxn:     PXN (privileged execute-never) bit
1323  * @in_pa:   The original input pa space
1324  * @out_pa:  The output pa space, modified by NSTable, NS, and NSE
1325  */
1326 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
1327                       int user_rw, int prot_rw, int xn, int pxn,
1328                       ARMSecuritySpace in_pa, ARMSecuritySpace out_pa)
1329 {
1330     ARMCPU *cpu = env_archcpu(env);
1331     bool is_user = regime_is_user(env, mmu_idx);
1332     bool have_wxn;
1333     int wxn = 0;
1334 
1335     assert(!regime_is_stage2(mmu_idx));
1336 
1337     if (is_user) {
1338         prot_rw = user_rw;
1339     } else {
1340         /*
1341          * PAN controls can forbid data accesses but don't affect insn fetch.
1342          * Plain PAN forbids data accesses if EL0 has data permissions;
1343          * PAN3 forbids data accesses if EL0 has either data or exec perms.
1344          * Note that for AArch64 the 'user can exec' case is exactly !xn.
1345          * We make the IMPDEF choices that SCR_EL3.SIF and Realm EL2&0
1346          * do not affect EPAN.
1347          */
1348         if (user_rw && regime_is_pan(env, mmu_idx)) {
1349             prot_rw = 0;
1350         } else if (cpu_isar_feature(aa64_pan3, cpu) && is_aa64 &&
1351                    regime_is_pan(env, mmu_idx) &&
1352                    (regime_sctlr(env, mmu_idx) & SCTLR_EPAN) && !xn) {
1353             prot_rw = 0;
1354         }
1355     }
1356 
1357     if (in_pa != out_pa) {
1358         switch (in_pa) {
1359         case ARMSS_Root:
1360             /*
1361              * R_ZWRVD: permission fault for insn fetched from non-Root,
1362              * I_WWBFB: SIF has no effect in EL3.
1363              */
1364             return prot_rw;
1365         case ARMSS_Realm:
1366             /*
1367              * R_PKTDS: permission fault for insn fetched from non-Realm,
1368              * for Realm EL2 or EL2&0.  The corresponding fault for EL1&0
1369              * happens during any stage2 translation.
1370              */
1371             switch (mmu_idx) {
1372             case ARMMMUIdx_E2:
1373             case ARMMMUIdx_E20_0:
1374             case ARMMMUIdx_E20_2:
1375             case ARMMMUIdx_E20_2_PAN:
1376                 return prot_rw;
1377             default:
1378                 break;
1379             }
1380             break;
1381         case ARMSS_Secure:
1382             if (env->cp15.scr_el3 & SCR_SIF) {
1383                 return prot_rw;
1384             }
1385             break;
1386         default:
1387             /* Input NonSecure must have output NonSecure. */
1388             g_assert_not_reached();
1389         }
1390     }
1391 
1392     /* TODO have_wxn should be replaced with
1393      *   ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
1394      * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
1395      * compatible processors have EL2, which is required for [U]WXN.
1396      */
1397     have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
1398 
1399     if (have_wxn) {
1400         wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
1401     }
1402 
1403     if (is_aa64) {
1404         if (regime_has_2_ranges(mmu_idx) && !is_user) {
1405             xn = pxn || (user_rw & PAGE_WRITE);
1406         }
1407     } else if (arm_feature(env, ARM_FEATURE_V7)) {
1408         switch (regime_el(env, mmu_idx)) {
1409         case 1:
1410         case 3:
1411             if (is_user) {
1412                 xn = xn || !(user_rw & PAGE_READ);
1413             } else {
1414                 int uwxn = 0;
1415                 if (have_wxn) {
1416                     uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
1417                 }
1418                 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
1419                      (uwxn && (user_rw & PAGE_WRITE));
1420             }
1421             break;
1422         case 2:
1423             break;
1424         }
1425     } else {
1426         xn = wxn = 0;
1427     }
1428 
1429     if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
1430         return prot_rw;
1431     }
1432     return prot_rw | PAGE_EXEC;
1433 }
1434 
1435 static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
1436                                           ARMMMUIdx mmu_idx)
1437 {
1438     uint64_t tcr = regime_tcr(env, mmu_idx);
1439     uint32_t el = regime_el(env, mmu_idx);
1440     int select, tsz;
1441     bool epd, hpd;
1442 
1443     assert(mmu_idx != ARMMMUIdx_Stage2_S);
1444 
1445     if (mmu_idx == ARMMMUIdx_Stage2) {
1446         /* VTCR */
1447         bool sext = extract32(tcr, 4, 1);
1448         bool sign = extract32(tcr, 3, 1);
1449 
1450         /*
1451          * If the sign-extend bit is not the same as t0sz[3], the result
1452          * is unpredictable. Flag this as a guest error.
1453          */
1454         if (sign != sext) {
1455             qemu_log_mask(LOG_GUEST_ERROR,
1456                           "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
1457         }
1458         tsz = sextract32(tcr, 0, 4) + 8;
1459         select = 0;
1460         hpd = false;
1461         epd = false;
1462     } else if (el == 2) {
1463         /* HTCR */
1464         tsz = extract32(tcr, 0, 3);
1465         select = 0;
1466         hpd = extract64(tcr, 24, 1);
1467         epd = false;
1468     } else {
1469         int t0sz = extract32(tcr, 0, 3);
1470         int t1sz = extract32(tcr, 16, 3);
1471 
1472         if (t1sz == 0) {
1473             select = va > (0xffffffffu >> t0sz);
1474         } else {
1475             /* Note that we will detect errors later.  */
1476             select = va >= ~(0xffffffffu >> t1sz);
1477         }
1478         if (!select) {
1479             tsz = t0sz;
1480             epd = extract32(tcr, 7, 1);
1481             hpd = extract64(tcr, 41, 1);
1482         } else {
1483             tsz = t1sz;
1484             epd = extract32(tcr, 23, 1);
1485             hpd = extract64(tcr, 42, 1);
1486         }
1487         /* For aarch32, hpd0 is not enabled without t2e as well.  */
1488         hpd &= extract32(tcr, 6, 1);
1489     }
1490 
1491     return (ARMVAParameters) {
1492         .tsz = tsz,
1493         .select = select,
1494         .epd = epd,
1495         .hpd = hpd,
1496     };
1497 }
1498 
1499 /*
1500  * check_s2_mmu_setup
1501  * @cpu:        ARMCPU
1502  * @is_aa64:    True if the translation regime is in AArch64 state
1503  * @tcr:        VTCR_EL2 or VSTCR_EL2
1504  * @ds:         Effective value of TCR.DS.
1505  * @iasize:     Bitsize of IPAs
1506  * @stride:     Page-table stride (See the ARM ARM)
1507  *
1508  * Decode the starting level of the S2 lookup, returning INT_MIN if
1509  * the configuration is invalid.
1510  */
1511 static int check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, uint64_t tcr,
1512                               bool ds, int iasize, int stride)
1513 {
1514     int sl0, sl2, startlevel, granulebits, levels;
1515     int s1_min_iasize, s1_max_iasize;
1516 
1517     sl0 = extract32(tcr, 6, 2);
1518     if (is_aa64) {
1519         /*
1520          * AArch64.S2InvalidSL: Interpretation of SL depends on the page size,
1521          * so interleave AArch64.S2StartLevel.
1522          */
1523         switch (stride) {
1524         case 9: /* 4KB */
1525             /* SL2 is RES0 unless DS=1 & 4KB granule. */
1526             sl2 = extract64(tcr, 33, 1);
1527             if (ds && sl2) {
1528                 if (sl0 != 0) {
1529                     goto fail;
1530                 }
1531                 startlevel = -1;
1532             } else {
1533                 startlevel = 2 - sl0;
1534                 switch (sl0) {
1535                 case 2:
1536                     if (arm_pamax(cpu) < 44) {
1537                         goto fail;
1538                     }
1539                     break;
1540                 case 3:
1541                     if (!cpu_isar_feature(aa64_st, cpu)) {
1542                         goto fail;
1543                     }
1544                     startlevel = 3;
1545                     break;
1546                 }
1547             }
1548             break;
1549         case 11: /* 16KB */
1550             switch (sl0) {
1551             case 2:
1552                 if (arm_pamax(cpu) < 42) {
1553                     goto fail;
1554                 }
1555                 break;
1556             case 3:
1557                 if (!ds) {
1558                     goto fail;
1559                 }
1560                 break;
1561             }
1562             startlevel = 3 - sl0;
1563             break;
1564         case 13: /* 64KB */
1565             switch (sl0) {
1566             case 2:
1567                 if (arm_pamax(cpu) < 44) {
1568                     goto fail;
1569                 }
1570                 break;
1571             case 3:
1572                 goto fail;
1573             }
1574             startlevel = 3 - sl0;
1575             break;
1576         default:
1577             g_assert_not_reached();
1578         }
1579     } else {
1580         /*
1581          * Things are simpler for AArch32 EL2, with only 4k pages.
1582          * There is no separate S2InvalidSL function, but AArch32.S2Walk
1583          * begins with walkparms.sl0 in {'1x'}.
1584          */
1585         assert(stride == 9);
1586         if (sl0 >= 2) {
1587             goto fail;
1588         }
1589         startlevel = 2 - sl0;
1590     }
1591 
1592     /* AArch{64,32}.S2InconsistentSL are functionally equivalent.  */
1593     levels = 3 - startlevel;
1594     granulebits = stride + 3;
1595 
1596     s1_min_iasize = levels * stride + granulebits + 1;
1597     s1_max_iasize = s1_min_iasize + (stride - 1) + 4;
1598 
1599     if (iasize >= s1_min_iasize && iasize <= s1_max_iasize) {
1600         return startlevel;
1601     }
1602 
1603  fail:
1604     return INT_MIN;
1605 }
1606 
1607 static bool lpae_block_desc_valid(ARMCPU *cpu, bool ds,
1608                                   ARMGranuleSize gran, int level)
1609 {
1610     /*
1611      * See pseudocode AArch46.BlockDescSupported(): block descriptors
1612      * are not valid at all levels, depending on the page size.
1613      */
1614     switch (gran) {
1615     case Gran4K:
1616         return (level == 0 && ds) || level == 1 || level == 2;
1617     case Gran16K:
1618         return (level == 1 && ds) || level == 2;
1619     case Gran64K:
1620         return (level == 1 && arm_pamax(cpu) == 52) || level == 2;
1621     default:
1622         g_assert_not_reached();
1623     }
1624 }
1625 
1626 static bool nv_nv1_enabled(CPUARMState *env, S1Translate *ptw)
1627 {
1628     uint64_t hcr = arm_hcr_el2_eff_secstate(env, ptw->in_space);
1629     return (hcr & (HCR_NV | HCR_NV1)) == (HCR_NV | HCR_NV1);
1630 }
1631 
1632 /**
1633  * get_phys_addr_lpae: perform one stage of page table walk, LPAE format
1634  *
1635  * Returns false if the translation was successful. Otherwise, phys_ptr,
1636  * attrs, prot and page_size may not be filled in, and the populated fsr
1637  * value provides information on why the translation aborted, in the format
1638  * of a long-format DFSR/IFSR fault register, with the following caveat:
1639  * the WnR bit is never set (the caller must do this).
1640  *
1641  * @env: CPUARMState
1642  * @ptw: Current and next stage parameters for the walk.
1643  * @address: virtual address to get physical address for
1644  * @access_type: MMU_DATA_LOAD, MMU_DATA_STORE or MMU_INST_FETCH
1645  * @memop: memory operation feeding this access, or 0 for none
1646  * @result: set on translation success,
1647  * @fi: set to fault info if the translation fails
1648  */
1649 static bool get_phys_addr_lpae(CPUARMState *env, S1Translate *ptw,
1650                                uint64_t address,
1651                                MMUAccessType access_type, MemOp memop,
1652                                GetPhysAddrResult *result, ARMMMUFaultInfo *fi)
1653 {
1654     ARMCPU *cpu = env_archcpu(env);
1655     ARMMMUIdx mmu_idx = ptw->in_mmu_idx;
1656     int32_t level;
1657     ARMVAParameters param;
1658     uint64_t ttbr;
1659     hwaddr descaddr, indexmask, indexmask_grainsize;
1660     uint32_t tableattrs;
1661     target_ulong page_size;
1662     uint64_t attrs;
1663     int32_t stride;
1664     int addrsize, inputsize, outputsize;
1665     uint64_t tcr = regime_tcr(env, mmu_idx);
1666     int ap, xn, pxn;
1667     uint32_t el = regime_el(env, mmu_idx);
1668     uint64_t descaddrmask;
1669     bool aarch64 = arm_el_is_aa64(env, el);
1670     uint64_t descriptor, new_descriptor;
1671     ARMSecuritySpace out_space;
1672     bool device;
1673 
1674     /* TODO: This code does not support shareability levels. */
1675     if (aarch64) {
1676         int ps;
1677 
1678         param = aa64_va_parameters(env, address, mmu_idx,
1679                                    access_type != MMU_INST_FETCH,
1680                                    !arm_el_is_aa64(env, 1));
1681         level = 0;
1682 
1683         /*
1684          * If TxSZ is programmed to a value larger than the maximum,
1685          * or smaller than the effective minimum, it is IMPLEMENTATION
1686          * DEFINED whether we behave as if the field were programmed
1687          * within bounds, or if a level 0 Translation fault is generated.
1688          *
1689          * With FEAT_LVA, fault on less than minimum becomes required,
1690          * so our choice is to always raise the fault.
1691          */
1692         if (param.tsz_oob) {
1693             goto do_translation_fault;
1694         }
1695 
1696         addrsize = 64 - 8 * param.tbi;
1697         inputsize = 64 - param.tsz;
1698 
1699         /*
1700          * Bound PS by PARANGE to find the effective output address size.
1701          * ID_AA64MMFR0 is a read-only register so values outside of the
1702          * supported mappings can be considered an implementation error.
1703          */
1704         ps = FIELD_EX64(cpu->isar.id_aa64mmfr0, ID_AA64MMFR0, PARANGE);
1705         ps = MIN(ps, param.ps);
1706         assert(ps < ARRAY_SIZE(pamax_map));
1707         outputsize = pamax_map[ps];
1708 
1709         /*
1710          * With LPA2, the effective output address (OA) size is at most 48 bits
1711          * unless TCR.DS == 1
1712          */
1713         if (!param.ds && param.gran != Gran64K) {
1714             outputsize = MIN(outputsize, 48);
1715         }
1716     } else {
1717         param = aa32_va_parameters(env, address, mmu_idx);
1718         level = 1;
1719         addrsize = (mmu_idx == ARMMMUIdx_Stage2 ? 40 : 32);
1720         inputsize = addrsize - param.tsz;
1721         outputsize = 40;
1722     }
1723 
1724     /*
1725      * We determined the region when collecting the parameters, but we
1726      * have not yet validated that the address is valid for the region.
1727      * Extract the top bits and verify that they all match select.
1728      *
1729      * For aa32, if inputsize == addrsize, then we have selected the
1730      * region by exclusion in aa32_va_parameters and there is no more
1731      * validation to do here.
1732      */
1733     if (inputsize < addrsize) {
1734         target_ulong top_bits = sextract64(address, inputsize,
1735                                            addrsize - inputsize);
1736         if (-top_bits != param.select) {
1737             /* The gap between the two regions is a Translation fault */
1738             goto do_translation_fault;
1739         }
1740     }
1741 
1742     stride = arm_granule_bits(param.gran) - 3;
1743 
1744     /*
1745      * Note that QEMU ignores shareability and cacheability attributes,
1746      * so we don't need to do anything with the SH, ORGN, IRGN fields
1747      * in the TTBCR.  Similarly, TTBCR:A1 selects whether we get the
1748      * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
1749      * implement any ASID-like capability so we can ignore it (instead
1750      * we will always flush the TLB any time the ASID is changed).
1751      */
1752     ttbr = regime_ttbr(env, mmu_idx, param.select);
1753 
1754     /*
1755      * Here we should have set up all the parameters for the translation:
1756      * inputsize, ttbr, epd, stride, tbi
1757      */
1758 
1759     if (param.epd) {
1760         /*
1761          * Translation table walk disabled => Translation fault on TLB miss
1762          * Note: This is always 0 on 64-bit EL2 and EL3.
1763          */
1764         goto do_translation_fault;
1765     }
1766 
1767     if (!regime_is_stage2(mmu_idx)) {
1768         /*
1769          * The starting level depends on the virtual address size (which can
1770          * be up to 48 bits) and the translation granule size. It indicates
1771          * the number of strides (stride bits at a time) needed to
1772          * consume the bits of the input address. In the pseudocode this is:
1773          *  level = 4 - RoundUp((inputsize - grainsize) / stride)
1774          * where their 'inputsize' is our 'inputsize', 'grainsize' is
1775          * our 'stride + 3' and 'stride' is our 'stride'.
1776          * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
1777          * = 4 - (inputsize - stride - 3 + stride - 1) / stride
1778          * = 4 - (inputsize - 4) / stride;
1779          */
1780         level = 4 - (inputsize - 4) / stride;
1781     } else {
1782         int startlevel = check_s2_mmu_setup(cpu, aarch64, tcr, param.ds,
1783                                             inputsize, stride);
1784         if (startlevel == INT_MIN) {
1785             level = 0;
1786             goto do_translation_fault;
1787         }
1788         level = startlevel;
1789     }
1790 
1791     indexmask_grainsize = MAKE_64BIT_MASK(0, stride + 3);
1792     indexmask = MAKE_64BIT_MASK(0, inputsize - (stride * (4 - level)));
1793 
1794     /* Now we can extract the actual base address from the TTBR */
1795     descaddr = extract64(ttbr, 0, 48);
1796 
1797     /*
1798      * For FEAT_LPA and PS=6, bits [51:48] of descaddr are in [5:2] of TTBR.
1799      *
1800      * Otherwise, if the base address is out of range, raise AddressSizeFault.
1801      * In the pseudocode, this is !IsZero(baseregister<47:outputsize>),
1802      * but we've just cleared the bits above 47, so simplify the test.
1803      */
1804     if (outputsize > 48) {
1805         descaddr |= extract64(ttbr, 2, 4) << 48;
1806     } else if (descaddr >> outputsize) {
1807         level = 0;
1808         fi->type = ARMFault_AddressSize;
1809         goto do_fault;
1810     }
1811 
1812     /*
1813      * We rely on this masking to clear the RES0 bits at the bottom of the TTBR
1814      * and also to mask out CnP (bit 0) which could validly be non-zero.
1815      */
1816     descaddr &= ~indexmask;
1817 
1818     /*
1819      * For AArch32, the address field in the descriptor goes up to bit 39
1820      * for both v7 and v8.  However, for v8 the SBZ bits [47:40] must be 0
1821      * or an AddressSize fault is raised.  So for v8 we extract those SBZ
1822      * bits as part of the address, which will be checked via outputsize.
1823      * For AArch64, the address field goes up to bit 47, or 49 with FEAT_LPA2;
1824      * the highest bits of a 52-bit output are placed elsewhere.
1825      */
1826     if (param.ds) {
1827         descaddrmask = MAKE_64BIT_MASK(0, 50);
1828     } else if (arm_feature(env, ARM_FEATURE_V8)) {
1829         descaddrmask = MAKE_64BIT_MASK(0, 48);
1830     } else {
1831         descaddrmask = MAKE_64BIT_MASK(0, 40);
1832     }
1833     descaddrmask &= ~indexmask_grainsize;
1834     tableattrs = 0;
1835 
1836  next_level:
1837     descaddr |= (address >> (stride * (4 - level))) & indexmask;
1838     descaddr &= ~7ULL;
1839 
1840     /*
1841      * Process the NSTable bit from the previous level.  This changes
1842      * the table address space and the output space from Secure to
1843      * NonSecure.  With RME, the EL3 translation regime does not change
1844      * from Root to NonSecure.
1845      */
1846     if (ptw->in_space == ARMSS_Secure
1847         && !regime_is_stage2(mmu_idx)
1848         && extract32(tableattrs, 4, 1)) {
1849         /*
1850          * Stage2_S -> Stage2 or Phys_S -> Phys_NS
1851          * Assert the relative order of the secure/non-secure indexes.
1852          */
1853         QEMU_BUILD_BUG_ON(ARMMMUIdx_Phys_S + 1 != ARMMMUIdx_Phys_NS);
1854         QEMU_BUILD_BUG_ON(ARMMMUIdx_Stage2_S + 1 != ARMMMUIdx_Stage2);
1855         ptw->in_ptw_idx += 1;
1856         ptw->in_space = ARMSS_NonSecure;
1857     }
1858 
1859     if (!S1_ptw_translate(env, ptw, descaddr, fi)) {
1860         goto do_fault;
1861     }
1862     descriptor = arm_ldq_ptw(env, ptw, fi);
1863     if (fi->type != ARMFault_None) {
1864         goto do_fault;
1865     }
1866     new_descriptor = descriptor;
1867 
1868  restart_atomic_update:
1869     if (!(descriptor & 1) ||
1870         (!(descriptor & 2) &&
1871          !lpae_block_desc_valid(cpu, param.ds, param.gran, level))) {
1872         /* Invalid, or a block descriptor at an invalid level */
1873         goto do_translation_fault;
1874     }
1875 
1876     descaddr = descriptor & descaddrmask;
1877 
1878     /*
1879      * For FEAT_LPA and PS=6, bits [51:48] of descaddr are in [15:12]
1880      * of descriptor.  For FEAT_LPA2 and effective DS, bits [51:50] of
1881      * descaddr are in [9:8].  Otherwise, if descaddr is out of range,
1882      * raise AddressSizeFault.
1883      */
1884     if (outputsize > 48) {
1885         if (param.ds) {
1886             descaddr |= extract64(descriptor, 8, 2) << 50;
1887         } else {
1888             descaddr |= extract64(descriptor, 12, 4) << 48;
1889         }
1890     } else if (descaddr >> outputsize) {
1891         fi->type = ARMFault_AddressSize;
1892         goto do_fault;
1893     }
1894 
1895     if ((descriptor & 2) && (level < 3)) {
1896         /*
1897          * Table entry. The top five bits are attributes which may
1898          * propagate down through lower levels of the table (and
1899          * which are all arranged so that 0 means "no effect", so
1900          * we can gather them up by ORing in the bits at each level).
1901          */
1902         tableattrs |= extract64(descriptor, 59, 5);
1903         level++;
1904         indexmask = indexmask_grainsize;
1905         goto next_level;
1906     }
1907 
1908     /*
1909      * Block entry at level 1 or 2, or page entry at level 3.
1910      * These are basically the same thing, although the number
1911      * of bits we pull in from the vaddr varies. Note that although
1912      * descaddrmask masks enough of the low bits of the descriptor
1913      * to give a correct page or table address, the address field
1914      * in a block descriptor is smaller; so we need to explicitly
1915      * clear the lower bits here before ORing in the low vaddr bits.
1916      *
1917      * Afterward, descaddr is the final physical address.
1918      */
1919     page_size = (1ULL << ((stride * (4 - level)) + 3));
1920     descaddr &= ~(hwaddr)(page_size - 1);
1921     descaddr |= (address & (page_size - 1));
1922 
1923     if (likely(!ptw->in_debug)) {
1924         /*
1925          * Access flag.
1926          * If HA is enabled, prepare to update the descriptor below.
1927          * Otherwise, pass the access fault on to software.
1928          */
1929         if (!(descriptor & (1 << 10))) {
1930             if (param.ha) {
1931                 new_descriptor |= 1 << 10; /* AF */
1932             } else {
1933                 fi->type = ARMFault_AccessFlag;
1934                 goto do_fault;
1935             }
1936         }
1937 
1938         /*
1939          * Dirty Bit.
1940          * If HD is enabled, pre-emptively set/clear the appropriate AP/S2AP
1941          * bit for writeback. The actual write protection test may still be
1942          * overridden by tableattrs, to be merged below.
1943          */
1944         if (param.hd
1945             && extract64(descriptor, 51, 1)  /* DBM */
1946             && access_type == MMU_DATA_STORE) {
1947             if (regime_is_stage2(mmu_idx)) {
1948                 new_descriptor |= 1ull << 7;    /* set S2AP[1] */
1949             } else {
1950                 new_descriptor &= ~(1ull << 7); /* clear AP[2] */
1951             }
1952         }
1953     }
1954 
1955     /*
1956      * Extract attributes from the (modified) descriptor, and apply
1957      * table descriptors. Stage 2 table descriptors do not include
1958      * any attribute fields. HPD disables all the table attributes
1959      * except NSTable (which we have already handled).
1960      */
1961     attrs = new_descriptor & (MAKE_64BIT_MASK(2, 10) | MAKE_64BIT_MASK(50, 14));
1962     if (!regime_is_stage2(mmu_idx)) {
1963         if (!param.hpd) {
1964             attrs |= extract64(tableattrs, 0, 2) << 53;     /* XN, PXN */
1965             /*
1966              * The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
1967              * means "force PL1 access only", which means forcing AP[1] to 0.
1968              */
1969             attrs &= ~(extract64(tableattrs, 2, 1) << 6); /* !APT[0] => AP[1] */
1970             attrs |= extract32(tableattrs, 3, 1) << 7;    /* APT[1] => AP[2] */
1971         }
1972     }
1973 
1974     ap = extract32(attrs, 6, 2);
1975     out_space = ptw->in_space;
1976     if (regime_is_stage2(mmu_idx)) {
1977         /*
1978          * R_GYNXY: For stage2 in Realm security state, bit 55 is NS.
1979          * The bit remains ignored for other security states.
1980          * R_YMCSL: Executing an insn fetched from non-Realm causes
1981          * a stage2 permission fault.
1982          */
1983         if (out_space == ARMSS_Realm && extract64(attrs, 55, 1)) {
1984             out_space = ARMSS_NonSecure;
1985             result->f.prot = get_S2prot_noexecute(ap);
1986         } else {
1987             xn = extract64(attrs, 53, 2);
1988             result->f.prot = get_S2prot(env, ap, xn, ptw->in_s1_is_el0);
1989         }
1990 
1991         result->cacheattrs.is_s2_format = true;
1992         result->cacheattrs.attrs = extract32(attrs, 2, 4);
1993         /*
1994          * Security state does not really affect HCR_EL2.FWB;
1995          * we only need to filter FWB for aa32 or other FEAT.
1996          */
1997         device = S2_attrs_are_device(arm_hcr_el2_eff(env),
1998                                      result->cacheattrs.attrs);
1999     } else {
2000         int nse, ns = extract32(attrs, 5, 1);
2001         uint8_t attrindx;
2002         uint64_t mair;
2003         int user_rw, prot_rw;
2004 
2005         switch (out_space) {
2006         case ARMSS_Root:
2007             /*
2008              * R_GVZML: Bit 11 becomes the NSE field in the EL3 regime.
2009              * R_XTYPW: NSE and NS together select the output pa space.
2010              */
2011             nse = extract32(attrs, 11, 1);
2012             out_space = (nse << 1) | ns;
2013             if (out_space == ARMSS_Secure &&
2014                 !cpu_isar_feature(aa64_sel2, cpu)) {
2015                 out_space = ARMSS_NonSecure;
2016             }
2017             break;
2018         case ARMSS_Secure:
2019             if (ns) {
2020                 out_space = ARMSS_NonSecure;
2021             }
2022             break;
2023         case ARMSS_Realm:
2024             switch (mmu_idx) {
2025             case ARMMMUIdx_Stage1_E0:
2026             case ARMMMUIdx_Stage1_E1:
2027             case ARMMMUIdx_Stage1_E1_PAN:
2028                 /* I_CZPRF: For Realm EL1&0 stage1, NS bit is RES0. */
2029                 break;
2030             case ARMMMUIdx_E2:
2031             case ARMMMUIdx_E20_0:
2032             case ARMMMUIdx_E20_2:
2033             case ARMMMUIdx_E20_2_PAN:
2034                 /*
2035                  * R_LYKFZ, R_WGRZN: For Realm EL2 and EL2&1,
2036                  * NS changes the output to non-secure space.
2037                  */
2038                 if (ns) {
2039                     out_space = ARMSS_NonSecure;
2040                 }
2041                 break;
2042             default:
2043                 g_assert_not_reached();
2044             }
2045             break;
2046         case ARMSS_NonSecure:
2047             /* R_QRMFF: For NonSecure state, the NS bit is RES0. */
2048             break;
2049         default:
2050             g_assert_not_reached();
2051         }
2052         xn = extract64(attrs, 54, 1);
2053         pxn = extract64(attrs, 53, 1);
2054 
2055         if (el == 1 && nv_nv1_enabled(env, ptw)) {
2056             /*
2057              * With FEAT_NV, when HCR_EL2.{NV,NV1} == {1,1}, the block/page
2058              * descriptor bit 54 holds PXN, 53 is RES0, and the effective value
2059              * of UXN is 0. Similarly for bits 59 and 60 in table descriptors
2060              * (which we have already folded into bits 53 and 54 of attrs).
2061              * AP[1] (descriptor bit 6, our ap bit 0) is treated as 0.
2062              * Similarly, APTable[0] from the table descriptor is treated as 0;
2063              * we already folded this into AP[1] and squashing that to 0 does
2064              * the right thing.
2065              */
2066             pxn = xn;
2067             xn = 0;
2068             ap &= ~1;
2069         }
2070 
2071         user_rw = simple_ap_to_rw_prot_is_user(ap, true);
2072         prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
2073         /*
2074          * Note that we modified ptw->in_space earlier for NSTable, but
2075          * result->f.attrs retains a copy of the original security space.
2076          */
2077         result->f.prot = get_S1prot(env, mmu_idx, aarch64, user_rw, prot_rw,
2078                                     xn, pxn, result->f.attrs.space, out_space);
2079 
2080         /* Index into MAIR registers for cache attributes */
2081         attrindx = extract32(attrs, 2, 3);
2082         mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
2083         assert(attrindx <= 7);
2084         result->cacheattrs.is_s2_format = false;
2085         result->cacheattrs.attrs = extract64(mair, attrindx * 8, 8);
2086 
2087         /* When in aarch64 mode, and BTI is enabled, remember GP in the TLB. */
2088         if (aarch64 && cpu_isar_feature(aa64_bti, cpu)) {
2089             result->f.extra.arm.guarded = extract64(attrs, 50, 1); /* GP */
2090         }
2091         device = S1_attrs_are_device(result->cacheattrs.attrs);
2092     }
2093 
2094     /*
2095      * Enable alignment checks on Device memory.
2096      *
2097      * Per R_XCHFJ, the correct ordering for alignment, permission,
2098      * and stage 2 faults is:
2099      *    - Alignment fault caused by the memory type
2100      *    - Permission fault
2101      *    - A stage 2 fault on the memory access
2102      * Perform the alignment check now, so that we recognize it in
2103      * the correct order.  Set TLB_CHECK_ALIGNED so that any subsequent
2104      * softmmu tlb hit will also check the alignment; clear along the
2105      * non-device path so that tlb_fill_flags is consistent in the
2106      * event of restart_atomic_update.
2107      *
2108      * In v7, for a CPU without the Virtualization Extensions this
2109      * access is UNPREDICTABLE; we choose to make it take the alignment
2110      * fault as is required for a v7VE CPU. (QEMU doesn't emulate any
2111      * CPUs with ARM_FEATURE_LPAE but not ARM_FEATURE_V7VE anyway.)
2112      */
2113     if (device) {
2114         unsigned a_bits = memop_atomicity_bits(memop);
2115         if (address & ((1 << a_bits) - 1)) {
2116             fi->type = ARMFault_Alignment;
2117             goto do_fault;
2118         }
2119         result->f.tlb_fill_flags = TLB_CHECK_ALIGNED;
2120     } else {
2121         result->f.tlb_fill_flags = 0;
2122     }
2123 
2124     if (!(result->f.prot & (1 << access_type))) {
2125         fi->type = ARMFault_Permission;
2126         goto do_fault;
2127     }
2128 
2129     /* If FEAT_HAFDBS has made changes, update the PTE. */
2130     if (new_descriptor != descriptor) {
2131         new_descriptor = arm_casq_ptw(env, descriptor, new_descriptor, ptw, fi);
2132         if (fi->type != ARMFault_None) {
2133             goto do_fault;
2134         }
2135         /*
2136          * I_YZSVV says that if the in-memory descriptor has changed,
2137          * then we must use the information in that new value
2138          * (which might include a different output address, different
2139          * attributes, or generate a fault).
2140          * Restart the handling of the descriptor value from scratch.
2141          */
2142         if (new_descriptor != descriptor) {
2143             descriptor = new_descriptor;
2144             goto restart_atomic_update;
2145         }
2146     }
2147 
2148     result->f.attrs.space = out_space;
2149     result->f.attrs.secure = arm_space_is_secure(out_space);
2150 
2151     /*
2152      * For FEAT_LPA2 and effective DS, the SH field in the attributes
2153      * was re-purposed for output address bits.  The SH attribute in
2154      * that case comes from TCR_ELx, which we extracted earlier.
2155      */
2156     if (param.ds) {
2157         result->cacheattrs.shareability = param.sh;
2158     } else {
2159         result->cacheattrs.shareability = extract32(attrs, 8, 2);
2160     }
2161 
2162     result->f.phys_addr = descaddr;
2163     result->f.lg_page_size = ctz64(page_size);
2164     return false;
2165 
2166  do_translation_fault:
2167     fi->type = ARMFault_Translation;
2168  do_fault:
2169     if (fi->s1ptw) {
2170         /* Retain the existing stage 2 fi->level */
2171         assert(fi->stage2);
2172     } else {
2173         fi->level = level;
2174         fi->stage2 = regime_is_stage2(mmu_idx);
2175     }
2176     fi->s1ns = fault_s1ns(ptw->in_space, mmu_idx);
2177     return true;
2178 }
2179 
2180 static bool get_phys_addr_pmsav5(CPUARMState *env,
2181                                  S1Translate *ptw,
2182                                  uint32_t address,
2183                                  MMUAccessType access_type,
2184                                  GetPhysAddrResult *result,
2185                                  ARMMMUFaultInfo *fi)
2186 {
2187     int n;
2188     uint32_t mask;
2189     uint32_t base;
2190     ARMMMUIdx mmu_idx = ptw->in_mmu_idx;
2191     bool is_user = regime_is_user(env, mmu_idx);
2192 
2193     if (regime_translation_disabled(env, mmu_idx, ptw->in_space)) {
2194         /* MPU disabled.  */
2195         result->f.phys_addr = address;
2196         result->f.prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
2197         return false;
2198     }
2199 
2200     result->f.phys_addr = address;
2201     for (n = 7; n >= 0; n--) {
2202         base = env->cp15.c6_region[n];
2203         if ((base & 1) == 0) {
2204             continue;
2205         }
2206         mask = 1 << ((base >> 1) & 0x1f);
2207         /* Keep this shift separate from the above to avoid an
2208            (undefined) << 32.  */
2209         mask = (mask << 1) - 1;
2210         if (((base ^ address) & ~mask) == 0) {
2211             break;
2212         }
2213     }
2214     if (n < 0) {
2215         fi->type = ARMFault_Background;
2216         return true;
2217     }
2218 
2219     if (access_type == MMU_INST_FETCH) {
2220         mask = env->cp15.pmsav5_insn_ap;
2221     } else {
2222         mask = env->cp15.pmsav5_data_ap;
2223     }
2224     mask = (mask >> (n * 4)) & 0xf;
2225     switch (mask) {
2226     case 0:
2227         fi->type = ARMFault_Permission;
2228         fi->level = 1;
2229         return true;
2230     case 1:
2231         if (is_user) {
2232             fi->type = ARMFault_Permission;
2233             fi->level = 1;
2234             return true;
2235         }
2236         result->f.prot = PAGE_READ | PAGE_WRITE;
2237         break;
2238     case 2:
2239         result->f.prot = PAGE_READ;
2240         if (!is_user) {
2241             result->f.prot |= PAGE_WRITE;
2242         }
2243         break;
2244     case 3:
2245         result->f.prot = PAGE_READ | PAGE_WRITE;
2246         break;
2247     case 5:
2248         if (is_user) {
2249             fi->type = ARMFault_Permission;
2250             fi->level = 1;
2251             return true;
2252         }
2253         result->f.prot = PAGE_READ;
2254         break;
2255     case 6:
2256         result->f.prot = PAGE_READ;
2257         break;
2258     default:
2259         /* Bad permission.  */
2260         fi->type = ARMFault_Permission;
2261         fi->level = 1;
2262         return true;
2263     }
2264     result->f.prot |= PAGE_EXEC;
2265     return false;
2266 }
2267 
2268 static void get_phys_addr_pmsav7_default(CPUARMState *env, ARMMMUIdx mmu_idx,
2269                                          int32_t address, uint8_t *prot)
2270 {
2271     if (!arm_feature(env, ARM_FEATURE_M)) {
2272         *prot = PAGE_READ | PAGE_WRITE;
2273         switch (address) {
2274         case 0xF0000000 ... 0xFFFFFFFF:
2275             if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
2276                 /* hivecs execing is ok */
2277                 *prot |= PAGE_EXEC;
2278             }
2279             break;
2280         case 0x00000000 ... 0x7FFFFFFF:
2281             *prot |= PAGE_EXEC;
2282             break;
2283         }
2284     } else {
2285         /* Default system address map for M profile cores.
2286          * The architecture specifies which regions are execute-never;
2287          * at the MPU level no other checks are defined.
2288          */
2289         switch (address) {
2290         case 0x00000000 ... 0x1fffffff: /* ROM */
2291         case 0x20000000 ... 0x3fffffff: /* SRAM */
2292         case 0x60000000 ... 0x7fffffff: /* RAM */
2293         case 0x80000000 ... 0x9fffffff: /* RAM */
2294             *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
2295             break;
2296         case 0x40000000 ... 0x5fffffff: /* Peripheral */
2297         case 0xa0000000 ... 0xbfffffff: /* Device */
2298         case 0xc0000000 ... 0xdfffffff: /* Device */
2299         case 0xe0000000 ... 0xffffffff: /* System */
2300             *prot = PAGE_READ | PAGE_WRITE;
2301             break;
2302         default:
2303             g_assert_not_reached();
2304         }
2305     }
2306 }
2307 
2308 static bool m_is_ppb_region(CPUARMState *env, uint32_t address)
2309 {
2310     /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
2311     return arm_feature(env, ARM_FEATURE_M) &&
2312         extract32(address, 20, 12) == 0xe00;
2313 }
2314 
2315 static bool m_is_system_region(CPUARMState *env, uint32_t address)
2316 {
2317     /*
2318      * True if address is in the M profile system region
2319      * 0xe0000000 - 0xffffffff
2320      */
2321     return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
2322 }
2323 
2324 static bool pmsav7_use_background_region(ARMCPU *cpu, ARMMMUIdx mmu_idx,
2325                                          bool is_secure, bool is_user)
2326 {
2327     /*
2328      * Return true if we should use the default memory map as a
2329      * "background" region if there are no hits against any MPU regions.
2330      */
2331     CPUARMState *env = &cpu->env;
2332 
2333     if (is_user) {
2334         return false;
2335     }
2336 
2337     if (arm_feature(env, ARM_FEATURE_M)) {
2338         return env->v7m.mpu_ctrl[is_secure] & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
2339     }
2340 
2341     if (mmu_idx == ARMMMUIdx_Stage2) {
2342         return false;
2343     }
2344 
2345     return regime_sctlr(env, mmu_idx) & SCTLR_BR;
2346 }
2347 
2348 static bool get_phys_addr_pmsav7(CPUARMState *env,
2349                                  S1Translate *ptw,
2350                                  uint32_t address,
2351                                  MMUAccessType access_type,
2352                                  GetPhysAddrResult *result,
2353                                  ARMMMUFaultInfo *fi)
2354 {
2355     ARMCPU *cpu = env_archcpu(env);
2356     int n;
2357     ARMMMUIdx mmu_idx = ptw->in_mmu_idx;
2358     bool is_user = regime_is_user(env, mmu_idx);
2359     bool secure = arm_space_is_secure(ptw->in_space);
2360 
2361     result->f.phys_addr = address;
2362     result->f.lg_page_size = TARGET_PAGE_BITS;
2363     result->f.prot = 0;
2364 
2365     if (regime_translation_disabled(env, mmu_idx, ptw->in_space) ||
2366         m_is_ppb_region(env, address)) {
2367         /*
2368          * MPU disabled or M profile PPB access: use default memory map.
2369          * The other case which uses the default memory map in the
2370          * v7M ARM ARM pseudocode is exception vector reads from the vector
2371          * table. In QEMU those accesses are done in arm_v7m_load_vector(),
2372          * which always does a direct read using address_space_ldl(), rather
2373          * than going via this function, so we don't need to check that here.
2374          */
2375         get_phys_addr_pmsav7_default(env, mmu_idx, address, &result->f.prot);
2376     } else { /* MPU enabled */
2377         for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
2378             /* region search */
2379             uint32_t base = env->pmsav7.drbar[n];
2380             uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
2381             uint32_t rmask;
2382             bool srdis = false;
2383 
2384             if (!(env->pmsav7.drsr[n] & 0x1)) {
2385                 continue;
2386             }
2387 
2388             if (!rsize) {
2389                 qemu_log_mask(LOG_GUEST_ERROR,
2390                               "DRSR[%d]: Rsize field cannot be 0\n", n);
2391                 continue;
2392             }
2393             rsize++;
2394             rmask = (1ull << rsize) - 1;
2395 
2396             if (base & rmask) {
2397                 qemu_log_mask(LOG_GUEST_ERROR,
2398                               "DRBAR[%d]: 0x%" PRIx32 " misaligned "
2399                               "to DRSR region size, mask = 0x%" PRIx32 "\n",
2400                               n, base, rmask);
2401                 continue;
2402             }
2403 
2404             if (address < base || address > base + rmask) {
2405                 /*
2406                  * Address not in this region. We must check whether the
2407                  * region covers addresses in the same page as our address.
2408                  * In that case we must not report a size that covers the
2409                  * whole page for a subsequent hit against a different MPU
2410                  * region or the background region, because it would result in
2411                  * incorrect TLB hits for subsequent accesses to addresses that
2412                  * are in this MPU region.
2413                  */
2414                 if (ranges_overlap(base, rmask,
2415                                    address & TARGET_PAGE_MASK,
2416                                    TARGET_PAGE_SIZE)) {
2417                     result->f.lg_page_size = 0;
2418                 }
2419                 continue;
2420             }
2421 
2422             /* Region matched */
2423 
2424             if (rsize >= 8) { /* no subregions for regions < 256 bytes */
2425                 int i, snd;
2426                 uint32_t srdis_mask;
2427 
2428                 rsize -= 3; /* sub region size (power of 2) */
2429                 snd = ((address - base) >> rsize) & 0x7;
2430                 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
2431 
2432                 srdis_mask = srdis ? 0x3 : 0x0;
2433                 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
2434                     /*
2435                      * This will check in groups of 2, 4 and then 8, whether
2436                      * the subregion bits are consistent. rsize is incremented
2437                      * back up to give the region size, considering consistent
2438                      * adjacent subregions as one region. Stop testing if rsize
2439                      * is already big enough for an entire QEMU page.
2440                      */
2441                     int snd_rounded = snd & ~(i - 1);
2442                     uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
2443                                                      snd_rounded + 8, i);
2444                     if (srdis_mask ^ srdis_multi) {
2445                         break;
2446                     }
2447                     srdis_mask = (srdis_mask << i) | srdis_mask;
2448                     rsize++;
2449                 }
2450             }
2451             if (srdis) {
2452                 continue;
2453             }
2454             if (rsize < TARGET_PAGE_BITS) {
2455                 result->f.lg_page_size = rsize;
2456             }
2457             break;
2458         }
2459 
2460         if (n == -1) { /* no hits */
2461             if (!pmsav7_use_background_region(cpu, mmu_idx, secure, is_user)) {
2462                 /* background fault */
2463                 fi->type = ARMFault_Background;
2464                 return true;
2465             }
2466             get_phys_addr_pmsav7_default(env, mmu_idx, address,
2467                                          &result->f.prot);
2468         } else { /* a MPU hit! */
2469             uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
2470             uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
2471 
2472             if (m_is_system_region(env, address)) {
2473                 /* System space is always execute never */
2474                 xn = 1;
2475             }
2476 
2477             if (is_user) { /* User mode AP bit decoding */
2478                 switch (ap) {
2479                 case 0:
2480                 case 1:
2481                 case 5:
2482                     break; /* no access */
2483                 case 3:
2484                     result->f.prot |= PAGE_WRITE;
2485                     /* fall through */
2486                 case 2:
2487                 case 6:
2488                     result->f.prot |= PAGE_READ | PAGE_EXEC;
2489                     break;
2490                 case 7:
2491                     /* for v7M, same as 6; for R profile a reserved value */
2492                     if (arm_feature(env, ARM_FEATURE_M)) {
2493                         result->f.prot |= PAGE_READ | PAGE_EXEC;
2494                         break;
2495                     }
2496                     /* fall through */
2497                 default:
2498                     qemu_log_mask(LOG_GUEST_ERROR,
2499                                   "DRACR[%d]: Bad value for AP bits: 0x%"
2500                                   PRIx32 "\n", n, ap);
2501                 }
2502             } else { /* Priv. mode AP bits decoding */
2503                 switch (ap) {
2504                 case 0:
2505                     break; /* no access */
2506                 case 1:
2507                 case 2:
2508                 case 3:
2509                     result->f.prot |= PAGE_WRITE;
2510                     /* fall through */
2511                 case 5:
2512                 case 6:
2513                     result->f.prot |= PAGE_READ | PAGE_EXEC;
2514                     break;
2515                 case 7:
2516                     /* for v7M, same as 6; for R profile a reserved value */
2517                     if (arm_feature(env, ARM_FEATURE_M)) {
2518                         result->f.prot |= PAGE_READ | PAGE_EXEC;
2519                         break;
2520                     }
2521                     /* fall through */
2522                 default:
2523                     qemu_log_mask(LOG_GUEST_ERROR,
2524                                   "DRACR[%d]: Bad value for AP bits: 0x%"
2525                                   PRIx32 "\n", n, ap);
2526                 }
2527             }
2528 
2529             /* execute never */
2530             if (xn) {
2531                 result->f.prot &= ~PAGE_EXEC;
2532             }
2533         }
2534     }
2535 
2536     fi->type = ARMFault_Permission;
2537     fi->level = 1;
2538     return !(result->f.prot & (1 << access_type));
2539 }
2540 
2541 static uint32_t *regime_rbar(CPUARMState *env, ARMMMUIdx mmu_idx,
2542                              uint32_t secure)
2543 {
2544     if (regime_el(env, mmu_idx) == 2) {
2545         return env->pmsav8.hprbar;
2546     } else {
2547         return env->pmsav8.rbar[secure];
2548     }
2549 }
2550 
2551 static uint32_t *regime_rlar(CPUARMState *env, ARMMMUIdx mmu_idx,
2552                              uint32_t secure)
2553 {
2554     if (regime_el(env, mmu_idx) == 2) {
2555         return env->pmsav8.hprlar;
2556     } else {
2557         return env->pmsav8.rlar[secure];
2558     }
2559 }
2560 
2561 bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
2562                        MMUAccessType access_type, ARMMMUIdx mmu_idx,
2563                        bool secure, GetPhysAddrResult *result,
2564                        ARMMMUFaultInfo *fi, uint32_t *mregion)
2565 {
2566     /*
2567      * Perform a PMSAv8 MPU lookup (without also doing the SAU check
2568      * that a full phys-to-virt translation does).
2569      * mregion is (if not NULL) set to the region number which matched,
2570      * or -1 if no region number is returned (MPU off, address did not
2571      * hit a region, address hit in multiple regions).
2572      * If the region hit doesn't cover the entire TARGET_PAGE the address
2573      * is within, then we set the result page_size to 1 to force the
2574      * memory system to use a subpage.
2575      */
2576     ARMCPU *cpu = env_archcpu(env);
2577     bool is_user = regime_is_user(env, mmu_idx);
2578     int n;
2579     int matchregion = -1;
2580     bool hit = false;
2581     uint32_t addr_page_base = address & TARGET_PAGE_MASK;
2582     uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
2583     int region_counter;
2584 
2585     if (regime_el(env, mmu_idx) == 2) {
2586         region_counter = cpu->pmsav8r_hdregion;
2587     } else {
2588         region_counter = cpu->pmsav7_dregion;
2589     }
2590 
2591     result->f.lg_page_size = TARGET_PAGE_BITS;
2592     result->f.phys_addr = address;
2593     result->f.prot = 0;
2594     if (mregion) {
2595         *mregion = -1;
2596     }
2597 
2598     if (mmu_idx == ARMMMUIdx_Stage2) {
2599         fi->stage2 = true;
2600     }
2601 
2602     /*
2603      * Unlike the ARM ARM pseudocode, we don't need to check whether this
2604      * was an exception vector read from the vector table (which is always
2605      * done using the default system address map), because those accesses
2606      * are done in arm_v7m_load_vector(), which always does a direct
2607      * read using address_space_ldl(), rather than going via this function.
2608      */
2609     if (regime_translation_disabled(env, mmu_idx, arm_secure_to_space(secure))) {
2610         /* MPU disabled */
2611         hit = true;
2612     } else if (m_is_ppb_region(env, address)) {
2613         hit = true;
2614     } else {
2615         if (pmsav7_use_background_region(cpu, mmu_idx, secure, is_user)) {
2616             hit = true;
2617         }
2618 
2619         uint32_t bitmask;
2620         if (arm_feature(env, ARM_FEATURE_M)) {
2621             bitmask = 0x1f;
2622         } else {
2623             bitmask = 0x3f;
2624             fi->level = 0;
2625         }
2626 
2627         for (n = region_counter - 1; n >= 0; n--) {
2628             /* region search */
2629             /*
2630              * Note that the base address is bits [31:x] from the register
2631              * with bits [x-1:0] all zeroes, but the limit address is bits
2632              * [31:x] from the register with bits [x:0] all ones. Where x is
2633              * 5 for Cortex-M and 6 for Cortex-R
2634              */
2635             uint32_t base = regime_rbar(env, mmu_idx, secure)[n] & ~bitmask;
2636             uint32_t limit = regime_rlar(env, mmu_idx, secure)[n] | bitmask;
2637 
2638             if (!(regime_rlar(env, mmu_idx, secure)[n] & 0x1)) {
2639                 /* Region disabled */
2640                 continue;
2641             }
2642 
2643             if (address < base || address > limit) {
2644                 /*
2645                  * Address not in this region. We must check whether the
2646                  * region covers addresses in the same page as our address.
2647                  * In that case we must not report a size that covers the
2648                  * whole page for a subsequent hit against a different MPU
2649                  * region or the background region, because it would result in
2650                  * incorrect TLB hits for subsequent accesses to addresses that
2651                  * are in this MPU region.
2652                  */
2653                 if (limit >= base &&
2654                     ranges_overlap(base, limit - base + 1,
2655                                    addr_page_base,
2656                                    TARGET_PAGE_SIZE)) {
2657                     result->f.lg_page_size = 0;
2658                 }
2659                 continue;
2660             }
2661 
2662             if (base > addr_page_base || limit < addr_page_limit) {
2663                 result->f.lg_page_size = 0;
2664             }
2665 
2666             if (matchregion != -1) {
2667                 /*
2668                  * Multiple regions match -- always a failure (unlike
2669                  * PMSAv7 where highest-numbered-region wins)
2670                  */
2671                 fi->type = ARMFault_Permission;
2672                 if (arm_feature(env, ARM_FEATURE_M)) {
2673                     fi->level = 1;
2674                 }
2675                 return true;
2676             }
2677 
2678             matchregion = n;
2679             hit = true;
2680         }
2681     }
2682 
2683     if (!hit) {
2684         if (arm_feature(env, ARM_FEATURE_M)) {
2685             fi->type = ARMFault_Background;
2686         } else {
2687             fi->type = ARMFault_Permission;
2688         }
2689         return true;
2690     }
2691 
2692     if (matchregion == -1) {
2693         /* hit using the background region */
2694         get_phys_addr_pmsav7_default(env, mmu_idx, address, &result->f.prot);
2695     } else {
2696         uint32_t matched_rbar = regime_rbar(env, mmu_idx, secure)[matchregion];
2697         uint32_t matched_rlar = regime_rlar(env, mmu_idx, secure)[matchregion];
2698         uint32_t ap = extract32(matched_rbar, 1, 2);
2699         uint32_t xn = extract32(matched_rbar, 0, 1);
2700         bool pxn = false;
2701 
2702         if (arm_feature(env, ARM_FEATURE_V8_1M)) {
2703             pxn = extract32(matched_rlar, 4, 1);
2704         }
2705 
2706         if (m_is_system_region(env, address)) {
2707             /* System space is always execute never */
2708             xn = 1;
2709         }
2710 
2711         if (regime_el(env, mmu_idx) == 2) {
2712             result->f.prot = simple_ap_to_rw_prot_is_user(ap,
2713                                             mmu_idx != ARMMMUIdx_E2);
2714         } else {
2715             result->f.prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
2716         }
2717 
2718         if (!arm_feature(env, ARM_FEATURE_M)) {
2719             uint8_t attrindx = extract32(matched_rlar, 1, 3);
2720             uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
2721             uint8_t sh = extract32(matched_rlar, 3, 2);
2722 
2723             if (regime_sctlr(env, mmu_idx) & SCTLR_WXN &&
2724                 result->f.prot & PAGE_WRITE && mmu_idx != ARMMMUIdx_Stage2) {
2725                 xn = 0x1;
2726             }
2727 
2728             if ((regime_el(env, mmu_idx) == 1) &&
2729                 regime_sctlr(env, mmu_idx) & SCTLR_UWXN && ap == 0x1) {
2730                 pxn = 0x1;
2731             }
2732 
2733             result->cacheattrs.is_s2_format = false;
2734             result->cacheattrs.attrs = extract64(mair, attrindx * 8, 8);
2735             result->cacheattrs.shareability = sh;
2736         }
2737 
2738         if (result->f.prot && !xn && !(pxn && !is_user)) {
2739             result->f.prot |= PAGE_EXEC;
2740         }
2741 
2742         if (mregion) {
2743             *mregion = matchregion;
2744         }
2745     }
2746 
2747     fi->type = ARMFault_Permission;
2748     if (arm_feature(env, ARM_FEATURE_M)) {
2749         fi->level = 1;
2750     }
2751     return !(result->f.prot & (1 << access_type));
2752 }
2753 
2754 static bool v8m_is_sau_exempt(CPUARMState *env,
2755                               uint32_t address, MMUAccessType access_type)
2756 {
2757     /*
2758      * The architecture specifies that certain address ranges are
2759      * exempt from v8M SAU/IDAU checks.
2760      */
2761     return
2762         (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
2763         (address >= 0xe0000000 && address <= 0xe0002fff) ||
2764         (address >= 0xe000e000 && address <= 0xe000efff) ||
2765         (address >= 0xe002e000 && address <= 0xe002efff) ||
2766         (address >= 0xe0040000 && address <= 0xe0041fff) ||
2767         (address >= 0xe00ff000 && address <= 0xe00fffff);
2768 }
2769 
2770 void v8m_security_lookup(CPUARMState *env, uint32_t address,
2771                          MMUAccessType access_type, ARMMMUIdx mmu_idx,
2772                          bool is_secure, V8M_SAttributes *sattrs)
2773 {
2774     /*
2775      * Look up the security attributes for this address. Compare the
2776      * pseudocode SecurityCheck() function.
2777      * We assume the caller has zero-initialized *sattrs.
2778      */
2779     ARMCPU *cpu = env_archcpu(env);
2780     int r;
2781     bool idau_exempt = false, idau_ns = true, idau_nsc = true;
2782     int idau_region = IREGION_NOTVALID;
2783     uint32_t addr_page_base = address & TARGET_PAGE_MASK;
2784     uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
2785 
2786     if (cpu->idau) {
2787         IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
2788         IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
2789 
2790         iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
2791                    &idau_nsc);
2792     }
2793 
2794     if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
2795         /* 0xf0000000..0xffffffff is always S for insn fetches */
2796         return;
2797     }
2798 
2799     if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
2800         sattrs->ns = !is_secure;
2801         return;
2802     }
2803 
2804     if (idau_region != IREGION_NOTVALID) {
2805         sattrs->irvalid = true;
2806         sattrs->iregion = idau_region;
2807     }
2808 
2809     switch (env->sau.ctrl & 3) {
2810     case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
2811         break;
2812     case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
2813         sattrs->ns = true;
2814         break;
2815     default: /* SAU.ENABLE == 1 */
2816         for (r = 0; r < cpu->sau_sregion; r++) {
2817             if (env->sau.rlar[r] & 1) {
2818                 uint32_t base = env->sau.rbar[r] & ~0x1f;
2819                 uint32_t limit = env->sau.rlar[r] | 0x1f;
2820 
2821                 if (base <= address && limit >= address) {
2822                     if (base > addr_page_base || limit < addr_page_limit) {
2823                         sattrs->subpage = true;
2824                     }
2825                     if (sattrs->srvalid) {
2826                         /*
2827                          * If we hit in more than one region then we must report
2828                          * as Secure, not NS-Callable, with no valid region
2829                          * number info.
2830                          */
2831                         sattrs->ns = false;
2832                         sattrs->nsc = false;
2833                         sattrs->sregion = 0;
2834                         sattrs->srvalid = false;
2835                         break;
2836                     } else {
2837                         if (env->sau.rlar[r] & 2) {
2838                             sattrs->nsc = true;
2839                         } else {
2840                             sattrs->ns = true;
2841                         }
2842                         sattrs->srvalid = true;
2843                         sattrs->sregion = r;
2844                     }
2845                 } else {
2846                     /*
2847                      * Address not in this region. We must check whether the
2848                      * region covers addresses in the same page as our address.
2849                      * In that case we must not report a size that covers the
2850                      * whole page for a subsequent hit against a different MPU
2851                      * region or the background region, because it would result
2852                      * in incorrect TLB hits for subsequent accesses to
2853                      * addresses that are in this MPU region.
2854                      */
2855                     if (limit >= base &&
2856                         ranges_overlap(base, limit - base + 1,
2857                                        addr_page_base,
2858                                        TARGET_PAGE_SIZE)) {
2859                         sattrs->subpage = true;
2860                     }
2861                 }
2862             }
2863         }
2864         break;
2865     }
2866 
2867     /*
2868      * The IDAU will override the SAU lookup results if it specifies
2869      * higher security than the SAU does.
2870      */
2871     if (!idau_ns) {
2872         if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
2873             sattrs->ns = false;
2874             sattrs->nsc = idau_nsc;
2875         }
2876     }
2877 }
2878 
2879 static bool get_phys_addr_pmsav8(CPUARMState *env,
2880                                  S1Translate *ptw,
2881                                  uint32_t address,
2882                                  MMUAccessType access_type,
2883                                  GetPhysAddrResult *result,
2884                                  ARMMMUFaultInfo *fi)
2885 {
2886     V8M_SAttributes sattrs = {};
2887     ARMMMUIdx mmu_idx = ptw->in_mmu_idx;
2888     bool secure = arm_space_is_secure(ptw->in_space);
2889     bool ret;
2890 
2891     if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
2892         v8m_security_lookup(env, address, access_type, mmu_idx,
2893                             secure, &sattrs);
2894         if (access_type == MMU_INST_FETCH) {
2895             /*
2896              * Instruction fetches always use the MMU bank and the
2897              * transaction attribute determined by the fetch address,
2898              * regardless of CPU state. This is painful for QEMU
2899              * to handle, because it would mean we need to encode
2900              * into the mmu_idx not just the (user, negpri) information
2901              * for the current security state but also that for the
2902              * other security state, which would balloon the number
2903              * of mmu_idx values needed alarmingly.
2904              * Fortunately we can avoid this because it's not actually
2905              * possible to arbitrarily execute code from memory with
2906              * the wrong security attribute: it will always generate
2907              * an exception of some kind or another, apart from the
2908              * special case of an NS CPU executing an SG instruction
2909              * in S&NSC memory. So we always just fail the translation
2910              * here and sort things out in the exception handler
2911              * (including possibly emulating an SG instruction).
2912              */
2913             if (sattrs.ns != !secure) {
2914                 if (sattrs.nsc) {
2915                     fi->type = ARMFault_QEMU_NSCExec;
2916                 } else {
2917                     fi->type = ARMFault_QEMU_SFault;
2918                 }
2919                 result->f.lg_page_size = sattrs.subpage ? 0 : TARGET_PAGE_BITS;
2920                 result->f.phys_addr = address;
2921                 result->f.prot = 0;
2922                 return true;
2923             }
2924         } else {
2925             /*
2926              * For data accesses we always use the MMU bank indicated
2927              * by the current CPU state, but the security attributes
2928              * might downgrade a secure access to nonsecure.
2929              */
2930             if (sattrs.ns) {
2931                 result->f.attrs.secure = false;
2932                 result->f.attrs.space = ARMSS_NonSecure;
2933             } else if (!secure) {
2934                 /*
2935                  * NS access to S memory must fault.
2936                  * Architecturally we should first check whether the
2937                  * MPU information for this address indicates that we
2938                  * are doing an unaligned access to Device memory, which
2939                  * should generate a UsageFault instead. QEMU does not
2940                  * currently check for that kind of unaligned access though.
2941                  * If we added it we would need to do so as a special case
2942                  * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
2943                  */
2944                 fi->type = ARMFault_QEMU_SFault;
2945                 result->f.lg_page_size = sattrs.subpage ? 0 : TARGET_PAGE_BITS;
2946                 result->f.phys_addr = address;
2947                 result->f.prot = 0;
2948                 return true;
2949             }
2950         }
2951     }
2952 
2953     ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, secure,
2954                             result, fi, NULL);
2955     if (sattrs.subpage) {
2956         result->f.lg_page_size = 0;
2957     }
2958     return ret;
2959 }
2960 
2961 /*
2962  * Translate from the 4-bit stage 2 representation of
2963  * memory attributes (without cache-allocation hints) to
2964  * the 8-bit representation of the stage 1 MAIR registers
2965  * (which includes allocation hints).
2966  *
2967  * ref: shared/translation/attrs/S2AttrDecode()
2968  *      .../S2ConvertAttrsHints()
2969  */
2970 static uint8_t convert_stage2_attrs(uint64_t hcr, uint8_t s2attrs)
2971 {
2972     uint8_t hiattr = extract32(s2attrs, 2, 2);
2973     uint8_t loattr = extract32(s2attrs, 0, 2);
2974     uint8_t hihint = 0, lohint = 0;
2975 
2976     if (hiattr != 0) { /* normal memory */
2977         if (hcr & HCR_CD) { /* cache disabled */
2978             hiattr = loattr = 1; /* non-cacheable */
2979         } else {
2980             if (hiattr != 1) { /* Write-through or write-back */
2981                 hihint = 3; /* RW allocate */
2982             }
2983             if (loattr != 1) { /* Write-through or write-back */
2984                 lohint = 3; /* RW allocate */
2985             }
2986         }
2987     }
2988 
2989     return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
2990 }
2991 
2992 /*
2993  * Combine either inner or outer cacheability attributes for normal
2994  * memory, according to table D4-42 and pseudocode procedure
2995  * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
2996  *
2997  * NB: only stage 1 includes allocation hints (RW bits), leading to
2998  * some asymmetry.
2999  */
3000 static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
3001 {
3002     if (s1 == 4 || s2 == 4) {
3003         /* non-cacheable has precedence */
3004         return 4;
3005     } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
3006         /* stage 1 write-through takes precedence */
3007         return s1;
3008     } else if (extract32(s2, 2, 2) == 2) {
3009         /* stage 2 write-through takes precedence, but the allocation hint
3010          * is still taken from stage 1
3011          */
3012         return (2 << 2) | extract32(s1, 0, 2);
3013     } else { /* write-back */
3014         return s1;
3015     }
3016 }
3017 
3018 /*
3019  * Combine the memory type and cacheability attributes of
3020  * s1 and s2 for the HCR_EL2.FWB == 0 case, returning the
3021  * combined attributes in MAIR_EL1 format.
3022  */
3023 static uint8_t combined_attrs_nofwb(uint64_t hcr,
3024                                     ARMCacheAttrs s1, ARMCacheAttrs s2)
3025 {
3026     uint8_t s1lo, s2lo, s1hi, s2hi, s2_mair_attrs, ret_attrs;
3027 
3028     if (s2.is_s2_format) {
3029         s2_mair_attrs = convert_stage2_attrs(hcr, s2.attrs);
3030     } else {
3031         s2_mair_attrs = s2.attrs;
3032     }
3033 
3034     s1lo = extract32(s1.attrs, 0, 4);
3035     s2lo = extract32(s2_mair_attrs, 0, 4);
3036     s1hi = extract32(s1.attrs, 4, 4);
3037     s2hi = extract32(s2_mair_attrs, 4, 4);
3038 
3039     /* Combine memory type and cacheability attributes */
3040     if (s1hi == 0 || s2hi == 0) {
3041         /* Device has precedence over normal */
3042         if (s1lo == 0 || s2lo == 0) {
3043             /* nGnRnE has precedence over anything */
3044             ret_attrs = 0;
3045         } else if (s1lo == 4 || s2lo == 4) {
3046             /* non-Reordering has precedence over Reordering */
3047             ret_attrs = 4;  /* nGnRE */
3048         } else if (s1lo == 8 || s2lo == 8) {
3049             /* non-Gathering has precedence over Gathering */
3050             ret_attrs = 8;  /* nGRE */
3051         } else {
3052             ret_attrs = 0xc; /* GRE */
3053         }
3054     } else { /* Normal memory */
3055         /* Outer/inner cacheability combine independently */
3056         ret_attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
3057                   | combine_cacheattr_nibble(s1lo, s2lo);
3058     }
3059     return ret_attrs;
3060 }
3061 
3062 static uint8_t force_cacheattr_nibble_wb(uint8_t attr)
3063 {
3064     /*
3065      * Given the 4 bits specifying the outer or inner cacheability
3066      * in MAIR format, return a value specifying Normal Write-Back,
3067      * with the allocation and transient hints taken from the input
3068      * if the input specified some kind of cacheable attribute.
3069      */
3070     if (attr == 0 || attr == 4) {
3071         /*
3072          * 0 == an UNPREDICTABLE encoding
3073          * 4 == Non-cacheable
3074          * Either way, force Write-Back RW allocate non-transient
3075          */
3076         return 0xf;
3077     }
3078     /* Change WriteThrough to WriteBack, keep allocation and transient hints */
3079     return attr | 4;
3080 }
3081 
3082 /*
3083  * Combine the memory type and cacheability attributes of
3084  * s1 and s2 for the HCR_EL2.FWB == 1 case, returning the
3085  * combined attributes in MAIR_EL1 format.
3086  */
3087 static uint8_t combined_attrs_fwb(ARMCacheAttrs s1, ARMCacheAttrs s2)
3088 {
3089     assert(s2.is_s2_format && !s1.is_s2_format);
3090 
3091     switch (s2.attrs) {
3092     case 7:
3093         /* Use stage 1 attributes */
3094         return s1.attrs;
3095     case 6:
3096         /*
3097          * Force Normal Write-Back. Note that if S1 is Normal cacheable
3098          * then we take the allocation hints from it; otherwise it is
3099          * RW allocate, non-transient.
3100          */
3101         if ((s1.attrs & 0xf0) == 0) {
3102             /* S1 is Device */
3103             return 0xff;
3104         }
3105         /* Need to check the Inner and Outer nibbles separately */
3106         return force_cacheattr_nibble_wb(s1.attrs & 0xf) |
3107             force_cacheattr_nibble_wb(s1.attrs >> 4) << 4;
3108     case 5:
3109         /* If S1 attrs are Device, use them; otherwise Normal Non-cacheable */
3110         if ((s1.attrs & 0xf0) == 0) {
3111             return s1.attrs;
3112         }
3113         return 0x44;
3114     case 0 ... 3:
3115         /* Force Device, of subtype specified by S2 */
3116         return s2.attrs << 2;
3117     default:
3118         /*
3119          * RESERVED values (including RES0 descriptor bit [5] being nonzero);
3120          * arbitrarily force Device.
3121          */
3122         return 0;
3123     }
3124 }
3125 
3126 /*
3127  * Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
3128  * and CombineS1S2Desc()
3129  *
3130  * @env:     CPUARMState
3131  * @s1:      Attributes from stage 1 walk
3132  * @s2:      Attributes from stage 2 walk
3133  */
3134 static ARMCacheAttrs combine_cacheattrs(uint64_t hcr,
3135                                         ARMCacheAttrs s1, ARMCacheAttrs s2)
3136 {
3137     ARMCacheAttrs ret;
3138     bool tagged = false;
3139 
3140     assert(!s1.is_s2_format);
3141     ret.is_s2_format = false;
3142 
3143     if (s1.attrs == 0xf0) {
3144         tagged = true;
3145         s1.attrs = 0xff;
3146     }
3147 
3148     /* Combine shareability attributes (table D4-43) */
3149     if (s1.shareability == 2 || s2.shareability == 2) {
3150         /* if either are outer-shareable, the result is outer-shareable */
3151         ret.shareability = 2;
3152     } else if (s1.shareability == 3 || s2.shareability == 3) {
3153         /* if either are inner-shareable, the result is inner-shareable */
3154         ret.shareability = 3;
3155     } else {
3156         /* both non-shareable */
3157         ret.shareability = 0;
3158     }
3159 
3160     /* Combine memory type and cacheability attributes */
3161     if (hcr & HCR_FWB) {
3162         ret.attrs = combined_attrs_fwb(s1, s2);
3163     } else {
3164         ret.attrs = combined_attrs_nofwb(hcr, s1, s2);
3165     }
3166 
3167     /*
3168      * Any location for which the resultant memory type is any
3169      * type of Device memory is always treated as Outer Shareable.
3170      * Any location for which the resultant memory type is Normal
3171      * Inner Non-cacheable, Outer Non-cacheable is always treated
3172      * as Outer Shareable.
3173      * TODO: FEAT_XS adds another value (0x40) also meaning iNCoNC
3174      */
3175     if ((ret.attrs & 0xf0) == 0 || ret.attrs == 0x44) {
3176         ret.shareability = 2;
3177     }
3178 
3179     /* TODO: CombineS1S2Desc does not consider transient, only WB, RWA. */
3180     if (tagged && ret.attrs == 0xff) {
3181         ret.attrs = 0xf0;
3182     }
3183 
3184     return ret;
3185 }
3186 
3187 /*
3188  * MMU disabled.  S1 addresses within aa64 translation regimes are
3189  * still checked for bounds -- see AArch64.S1DisabledOutput().
3190  */
3191 static bool get_phys_addr_disabled(CPUARMState *env,
3192                                    S1Translate *ptw,
3193                                    vaddr address,
3194                                    MMUAccessType access_type,
3195                                    GetPhysAddrResult *result,
3196                                    ARMMMUFaultInfo *fi)
3197 {
3198     ARMMMUIdx mmu_idx = ptw->in_mmu_idx;
3199     uint8_t memattr = 0x00;    /* Device nGnRnE */
3200     uint8_t shareability = 0;  /* non-shareable */
3201     int r_el;
3202 
3203     switch (mmu_idx) {
3204     case ARMMMUIdx_Stage2:
3205     case ARMMMUIdx_Stage2_S:
3206     case ARMMMUIdx_Phys_S:
3207     case ARMMMUIdx_Phys_NS:
3208     case ARMMMUIdx_Phys_Root:
3209     case ARMMMUIdx_Phys_Realm:
3210         break;
3211 
3212     default:
3213         r_el = regime_el(env, mmu_idx);
3214         if (arm_el_is_aa64(env, r_el)) {
3215             int pamax = arm_pamax(env_archcpu(env));
3216             uint64_t tcr = env->cp15.tcr_el[r_el];
3217             int addrtop, tbi;
3218 
3219             tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
3220             if (access_type == MMU_INST_FETCH) {
3221                 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
3222             }
3223             tbi = (tbi >> extract64(address, 55, 1)) & 1;
3224             addrtop = (tbi ? 55 : 63);
3225 
3226             if (extract64(address, pamax, addrtop - pamax + 1) != 0) {
3227                 fi->type = ARMFault_AddressSize;
3228                 fi->level = 0;
3229                 fi->stage2 = false;
3230                 return 1;
3231             }
3232 
3233             /*
3234              * When TBI is disabled, we've just validated that all of the
3235              * bits above PAMax are zero, so logically we only need to
3236              * clear the top byte for TBI.  But it's clearer to follow
3237              * the pseudocode set of addrdesc.paddress.
3238              */
3239             address = extract64(address, 0, 52);
3240         }
3241 
3242         /* Fill in cacheattr a-la AArch64.TranslateAddressS1Off. */
3243         if (r_el == 1) {
3244             uint64_t hcr = arm_hcr_el2_eff_secstate(env, ptw->in_space);
3245             if (hcr & HCR_DC) {
3246                 if (hcr & HCR_DCT) {
3247                     memattr = 0xf0;  /* Tagged, Normal, WB, RWA */
3248                 } else {
3249                     memattr = 0xff;  /* Normal, WB, RWA */
3250                 }
3251             }
3252         }
3253         if (memattr == 0) {
3254             if (access_type == MMU_INST_FETCH) {
3255                 if (regime_sctlr(env, mmu_idx) & SCTLR_I) {
3256                     memattr = 0xee;  /* Normal, WT, RA, NT */
3257                 } else {
3258                     memattr = 0x44;  /* Normal, NC, No */
3259                 }
3260             }
3261             shareability = 2; /* outer shareable */
3262         }
3263         result->cacheattrs.is_s2_format = false;
3264         break;
3265     }
3266 
3267     result->f.phys_addr = address;
3268     result->f.prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
3269     result->f.lg_page_size = TARGET_PAGE_BITS;
3270     result->cacheattrs.shareability = shareability;
3271     result->cacheattrs.attrs = memattr;
3272     return false;
3273 }
3274 
3275 static bool get_phys_addr_twostage(CPUARMState *env, S1Translate *ptw,
3276                                    vaddr address,
3277                                    MMUAccessType access_type, MemOp memop,
3278                                    GetPhysAddrResult *result,
3279                                    ARMMMUFaultInfo *fi)
3280 {
3281     hwaddr ipa;
3282     int s1_prot, s1_lgpgsz;
3283     ARMSecuritySpace in_space = ptw->in_space;
3284     bool ret, ipa_secure, s1_guarded;
3285     ARMCacheAttrs cacheattrs1;
3286     ARMSecuritySpace ipa_space;
3287     uint64_t hcr;
3288 
3289     ret = get_phys_addr_nogpc(env, ptw, address, access_type,
3290                               memop, result, fi);
3291 
3292     /* If S1 fails, return early.  */
3293     if (ret) {
3294         return ret;
3295     }
3296 
3297     ipa = result->f.phys_addr;
3298     ipa_secure = result->f.attrs.secure;
3299     ipa_space = result->f.attrs.space;
3300 
3301     ptw->in_s1_is_el0 = ptw->in_mmu_idx == ARMMMUIdx_Stage1_E0;
3302     ptw->in_mmu_idx = ipa_secure ? ARMMMUIdx_Stage2_S : ARMMMUIdx_Stage2;
3303     ptw->in_space = ipa_space;
3304     ptw->in_ptw_idx = ptw_idx_for_stage_2(env, ptw->in_mmu_idx);
3305 
3306     /*
3307      * S1 is done, now do S2 translation.
3308      * Save the stage1 results so that we may merge prot and cacheattrs later.
3309      */
3310     s1_prot = result->f.prot;
3311     s1_lgpgsz = result->f.lg_page_size;
3312     s1_guarded = result->f.extra.arm.guarded;
3313     cacheattrs1 = result->cacheattrs;
3314     memset(result, 0, sizeof(*result));
3315 
3316     ret = get_phys_addr_nogpc(env, ptw, ipa, access_type,
3317                               memop, result, fi);
3318     fi->s2addr = ipa;
3319 
3320     /* Combine the S1 and S2 perms.  */
3321     result->f.prot &= s1_prot;
3322 
3323     /* If S2 fails, return early.  */
3324     if (ret) {
3325         return ret;
3326     }
3327 
3328     /*
3329      * If either S1 or S2 returned a result smaller than TARGET_PAGE_SIZE,
3330      * this means "don't put this in the TLB"; in this case, return a
3331      * result with lg_page_size == 0 to achieve that. Otherwise,
3332      * use the maximum of the S1 & S2 page size, so that invalidation
3333      * of pages > TARGET_PAGE_SIZE works correctly. (This works even though
3334      * we know the combined result permissions etc only cover the minimum
3335      * of the S1 and S2 page size, because we know that the common TLB code
3336      * never actually creates TLB entries bigger than TARGET_PAGE_SIZE,
3337      * and passing a larger page size value only affects invalidations.)
3338      */
3339     if (result->f.lg_page_size < TARGET_PAGE_BITS ||
3340         s1_lgpgsz < TARGET_PAGE_BITS) {
3341         result->f.lg_page_size = 0;
3342     } else if (result->f.lg_page_size < s1_lgpgsz) {
3343         result->f.lg_page_size = s1_lgpgsz;
3344     }
3345 
3346     /* Combine the S1 and S2 cache attributes. */
3347     hcr = arm_hcr_el2_eff_secstate(env, in_space);
3348     if (hcr & HCR_DC) {
3349         /*
3350          * HCR.DC forces the first stage attributes to
3351          *  Normal Non-Shareable,
3352          *  Inner Write-Back Read-Allocate Write-Allocate,
3353          *  Outer Write-Back Read-Allocate Write-Allocate.
3354          * Do not overwrite Tagged within attrs.
3355          */
3356         if (cacheattrs1.attrs != 0xf0) {
3357             cacheattrs1.attrs = 0xff;
3358         }
3359         cacheattrs1.shareability = 0;
3360     }
3361     result->cacheattrs = combine_cacheattrs(hcr, cacheattrs1,
3362                                             result->cacheattrs);
3363 
3364     /* No BTI GP information in stage 2, we just use the S1 value */
3365     result->f.extra.arm.guarded = s1_guarded;
3366 
3367     /*
3368      * Check if IPA translates to secure or non-secure PA space.
3369      * Note that VSTCR overrides VTCR and {N}SW overrides {N}SA.
3370      */
3371     if (in_space == ARMSS_Secure) {
3372         result->f.attrs.secure =
3373             !(env->cp15.vstcr_el2 & (VSTCR_SA | VSTCR_SW))
3374             && (ipa_secure
3375                 || !(env->cp15.vtcr_el2 & (VTCR_NSA | VTCR_NSW)));
3376         result->f.attrs.space = arm_secure_to_space(result->f.attrs.secure);
3377     }
3378 
3379     return false;
3380 }
3381 
3382 static bool get_phys_addr_nogpc(CPUARMState *env, S1Translate *ptw,
3383                                       vaddr address,
3384                                       MMUAccessType access_type, MemOp memop,
3385                                       GetPhysAddrResult *result,
3386                                       ARMMMUFaultInfo *fi)
3387 {
3388     ARMMMUIdx mmu_idx = ptw->in_mmu_idx;
3389     ARMMMUIdx s1_mmu_idx;
3390 
3391     /*
3392      * The page table entries may downgrade Secure to NonSecure, but
3393      * cannot upgrade a NonSecure translation regime's attributes
3394      * to Secure or Realm.
3395      */
3396     result->f.attrs.space = ptw->in_space;
3397     result->f.attrs.secure = arm_space_is_secure(ptw->in_space);
3398 
3399     switch (mmu_idx) {
3400     case ARMMMUIdx_Phys_S:
3401     case ARMMMUIdx_Phys_NS:
3402     case ARMMMUIdx_Phys_Root:
3403     case ARMMMUIdx_Phys_Realm:
3404         /* Checking Phys early avoids special casing later vs regime_el. */
3405         return get_phys_addr_disabled(env, ptw, address, access_type,
3406                                       result, fi);
3407 
3408     case ARMMMUIdx_Stage1_E0:
3409     case ARMMMUIdx_Stage1_E1:
3410     case ARMMMUIdx_Stage1_E1_PAN:
3411         /*
3412          * First stage lookup uses second stage for ptw; only
3413          * Secure has both S and NS IPA and starts with Stage2_S.
3414          */
3415         ptw->in_ptw_idx = (ptw->in_space == ARMSS_Secure) ?
3416             ARMMMUIdx_Stage2_S : ARMMMUIdx_Stage2;
3417         break;
3418 
3419     case ARMMMUIdx_Stage2:
3420     case ARMMMUIdx_Stage2_S:
3421         /*
3422          * Second stage lookup uses physical for ptw; whether this is S or
3423          * NS may depend on the SW/NSW bits if this is a stage 2 lookup for
3424          * the Secure EL2&0 regime.
3425          */
3426         ptw->in_ptw_idx = ptw_idx_for_stage_2(env, mmu_idx);
3427         break;
3428 
3429     case ARMMMUIdx_E10_0:
3430         s1_mmu_idx = ARMMMUIdx_Stage1_E0;
3431         goto do_twostage;
3432     case ARMMMUIdx_E10_1:
3433         s1_mmu_idx = ARMMMUIdx_Stage1_E1;
3434         goto do_twostage;
3435     case ARMMMUIdx_E10_1_PAN:
3436         s1_mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3437     do_twostage:
3438         /*
3439          * Call ourselves recursively to do the stage 1 and then stage 2
3440          * translations if mmu_idx is a two-stage regime, and EL2 present.
3441          * Otherwise, a stage1+stage2 translation is just stage 1.
3442          */
3443         ptw->in_mmu_idx = mmu_idx = s1_mmu_idx;
3444         if (arm_feature(env, ARM_FEATURE_EL2) &&
3445             !regime_translation_disabled(env, ARMMMUIdx_Stage2, ptw->in_space)) {
3446             return get_phys_addr_twostage(env, ptw, address, access_type,
3447                                           memop, result, fi);
3448         }
3449         /* fall through */
3450 
3451     default:
3452         /* Single stage uses physical for ptw. */
3453         ptw->in_ptw_idx = arm_space_to_phys(ptw->in_space);
3454         break;
3455     }
3456 
3457     result->f.attrs.user = regime_is_user(env, mmu_idx);
3458 
3459     /*
3460      * Fast Context Switch Extension. This doesn't exist at all in v8.
3461      * In v7 and earlier it affects all stage 1 translations.
3462      */
3463     if (address < 0x02000000 && mmu_idx != ARMMMUIdx_Stage2
3464         && !arm_feature(env, ARM_FEATURE_V8)) {
3465         if (regime_el(env, mmu_idx) == 3) {
3466             address += env->cp15.fcseidr_s;
3467         } else {
3468             address += env->cp15.fcseidr_ns;
3469         }
3470     }
3471 
3472     if (arm_feature(env, ARM_FEATURE_PMSA)) {
3473         bool ret;
3474         result->f.lg_page_size = TARGET_PAGE_BITS;
3475 
3476         if (arm_feature(env, ARM_FEATURE_V8)) {
3477             /* PMSAv8 */
3478             ret = get_phys_addr_pmsav8(env, ptw, address, access_type,
3479                                        result, fi);
3480         } else if (arm_feature(env, ARM_FEATURE_V7)) {
3481             /* PMSAv7 */
3482             ret = get_phys_addr_pmsav7(env, ptw, address, access_type,
3483                                        result, fi);
3484         } else {
3485             /* Pre-v7 MPU */
3486             ret = get_phys_addr_pmsav5(env, ptw, address, access_type,
3487                                        result, fi);
3488         }
3489         qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
3490                       " mmu_idx %u -> %s (prot %c%c%c)\n",
3491                       access_type == MMU_DATA_LOAD ? "reading" :
3492                       (access_type == MMU_DATA_STORE ? "writing" : "execute"),
3493                       (uint32_t)address, mmu_idx,
3494                       ret ? "Miss" : "Hit",
3495                       result->f.prot & PAGE_READ ? 'r' : '-',
3496                       result->f.prot & PAGE_WRITE ? 'w' : '-',
3497                       result->f.prot & PAGE_EXEC ? 'x' : '-');
3498 
3499         return ret;
3500     }
3501 
3502     /* Definitely a real MMU, not an MPU */
3503 
3504     if (regime_translation_disabled(env, mmu_idx, ptw->in_space)) {
3505         return get_phys_addr_disabled(env, ptw, address, access_type,
3506                                       result, fi);
3507     }
3508 
3509     if (regime_using_lpae_format(env, mmu_idx)) {
3510         return get_phys_addr_lpae(env, ptw, address, access_type,
3511                                   memop, result, fi);
3512     } else if (arm_feature(env, ARM_FEATURE_V7) ||
3513                regime_sctlr(env, mmu_idx) & SCTLR_XP) {
3514         return get_phys_addr_v6(env, ptw, address, access_type, result, fi);
3515     } else {
3516         return get_phys_addr_v5(env, ptw, address, access_type, result, fi);
3517     }
3518 }
3519 
3520 static bool get_phys_addr_gpc(CPUARMState *env, S1Translate *ptw,
3521                               vaddr address,
3522                               MMUAccessType access_type, MemOp memop,
3523                               GetPhysAddrResult *result,
3524                               ARMMMUFaultInfo *fi)
3525 {
3526     if (get_phys_addr_nogpc(env, ptw, address, access_type,
3527                             memop, result, fi)) {
3528         return true;
3529     }
3530     if (!granule_protection_check(env, result->f.phys_addr,
3531                                   result->f.attrs.space, fi)) {
3532         fi->type = ARMFault_GPCFOnOutput;
3533         return true;
3534     }
3535     return false;
3536 }
3537 
3538 bool get_phys_addr_with_space_nogpc(CPUARMState *env, vaddr address,
3539                                     MMUAccessType access_type, MemOp memop,
3540                                     ARMMMUIdx mmu_idx, ARMSecuritySpace space,
3541                                     GetPhysAddrResult *result,
3542                                     ARMMMUFaultInfo *fi)
3543 {
3544     S1Translate ptw = {
3545         .in_mmu_idx = mmu_idx,
3546         .in_space = space,
3547     };
3548     return get_phys_addr_nogpc(env, &ptw, address, access_type,
3549                                memop, result, fi);
3550 }
3551 
3552 bool get_phys_addr(CPUARMState *env, vaddr address,
3553                    MMUAccessType access_type, MemOp memop, ARMMMUIdx mmu_idx,
3554                    GetPhysAddrResult *result, ARMMMUFaultInfo *fi)
3555 {
3556     S1Translate ptw = {
3557         .in_mmu_idx = mmu_idx,
3558     };
3559     ARMSecuritySpace ss;
3560 
3561     switch (mmu_idx) {
3562     case ARMMMUIdx_E10_0:
3563     case ARMMMUIdx_E10_1:
3564     case ARMMMUIdx_E10_1_PAN:
3565     case ARMMMUIdx_E20_0:
3566     case ARMMMUIdx_E20_2:
3567     case ARMMMUIdx_E20_2_PAN:
3568     case ARMMMUIdx_Stage1_E0:
3569     case ARMMMUIdx_Stage1_E1:
3570     case ARMMMUIdx_Stage1_E1_PAN:
3571     case ARMMMUIdx_E2:
3572         ss = arm_security_space_below_el3(env);
3573         break;
3574     case ARMMMUIdx_Stage2:
3575         /*
3576          * For Secure EL2, we need this index to be NonSecure;
3577          * otherwise this will already be NonSecure or Realm.
3578          */
3579         ss = arm_security_space_below_el3(env);
3580         if (ss == ARMSS_Secure) {
3581             ss = ARMSS_NonSecure;
3582         }
3583         break;
3584     case ARMMMUIdx_Phys_NS:
3585     case ARMMMUIdx_MPrivNegPri:
3586     case ARMMMUIdx_MUserNegPri:
3587     case ARMMMUIdx_MPriv:
3588     case ARMMMUIdx_MUser:
3589         ss = ARMSS_NonSecure;
3590         break;
3591     case ARMMMUIdx_Stage2_S:
3592     case ARMMMUIdx_Phys_S:
3593     case ARMMMUIdx_MSPrivNegPri:
3594     case ARMMMUIdx_MSUserNegPri:
3595     case ARMMMUIdx_MSPriv:
3596     case ARMMMUIdx_MSUser:
3597         ss = ARMSS_Secure;
3598         break;
3599     case ARMMMUIdx_E3:
3600     case ARMMMUIdx_E30_0:
3601     case ARMMMUIdx_E30_3_PAN:
3602         if (arm_feature(env, ARM_FEATURE_AARCH64) &&
3603             cpu_isar_feature(aa64_rme, env_archcpu(env))) {
3604             ss = ARMSS_Root;
3605         } else {
3606             ss = ARMSS_Secure;
3607         }
3608         break;
3609     case ARMMMUIdx_Phys_Root:
3610         ss = ARMSS_Root;
3611         break;
3612     case ARMMMUIdx_Phys_Realm:
3613         ss = ARMSS_Realm;
3614         break;
3615     default:
3616         g_assert_not_reached();
3617     }
3618 
3619     ptw.in_space = ss;
3620     return get_phys_addr_gpc(env, &ptw, address, access_type,
3621                              memop, result, fi);
3622 }
3623 
3624 hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
3625                                          MemTxAttrs *attrs)
3626 {
3627     ARMCPU *cpu = ARM_CPU(cs);
3628     CPUARMState *env = &cpu->env;
3629     ARMMMUIdx mmu_idx = arm_mmu_idx(env);
3630     ARMSecuritySpace ss = arm_security_space(env);
3631     S1Translate ptw = {
3632         .in_mmu_idx = mmu_idx,
3633         .in_space = ss,
3634         .in_debug = true,
3635     };
3636     GetPhysAddrResult res = {};
3637     ARMMMUFaultInfo fi = {};
3638     bool ret;
3639 
3640     ret = get_phys_addr_gpc(env, &ptw, addr, MMU_DATA_LOAD, 0, &res, &fi);
3641     *attrs = res.f.attrs;
3642 
3643     if (ret) {
3644         return -1;
3645     }
3646     return res.f.phys_addr;
3647 }
3648