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