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