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