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