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