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