1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (C) 2012 ARM Ltd. 4 */ 5 #ifndef __ASM_PGTABLE_H 6 #define __ASM_PGTABLE_H 7 8 #include <asm/bug.h> 9 #include <asm/proc-fns.h> 10 11 #include <asm/memory.h> 12 #include <asm/mte.h> 13 #include <asm/pgtable-hwdef.h> 14 #include <asm/pgtable-prot.h> 15 #include <asm/tlbflush.h> 16 17 /* 18 * VMALLOC range. 19 * 20 * VMALLOC_START: beginning of the kernel vmalloc space 21 * VMALLOC_END: extends to the available space below vmemmap 22 */ 23 #define VMALLOC_START (MODULES_END) 24 #if VA_BITS == VA_BITS_MIN 25 #define VMALLOC_END (VMEMMAP_START - SZ_8M) 26 #else 27 #define VMEMMAP_UNUSED_NPAGES ((_PAGE_OFFSET(vabits_actual) - PAGE_OFFSET) >> PAGE_SHIFT) 28 #define VMALLOC_END (VMEMMAP_START + VMEMMAP_UNUSED_NPAGES * sizeof(struct page) - SZ_8M) 29 #endif 30 31 #define vmemmap ((struct page *)VMEMMAP_START - (memstart_addr >> PAGE_SHIFT)) 32 33 #ifndef __ASSEMBLY__ 34 35 #include <asm/cmpxchg.h> 36 #include <asm/fixmap.h> 37 #include <asm/por.h> 38 #include <linux/mmdebug.h> 39 #include <linux/mm_types.h> 40 #include <linux/sched.h> 41 #include <linux/page_table_check.h> 42 43 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 44 #define __HAVE_ARCH_FLUSH_PMD_TLB_RANGE 45 46 /* Set stride and tlb_level in flush_*_tlb_range */ 47 #define flush_pmd_tlb_range(vma, addr, end) \ 48 __flush_tlb_range(vma, addr, end, PMD_SIZE, false, 2) 49 #define flush_pud_tlb_range(vma, addr, end) \ 50 __flush_tlb_range(vma, addr, end, PUD_SIZE, false, 1) 51 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 52 53 /* 54 * Outside of a few very special situations (e.g. hibernation), we always 55 * use broadcast TLB invalidation instructions, therefore a spurious page 56 * fault on one CPU which has been handled concurrently by another CPU 57 * does not need to perform additional invalidation. 58 */ 59 #define flush_tlb_fix_spurious_fault(vma, address, ptep) do { } while (0) 60 61 /* 62 * ZERO_PAGE is a global shared page that is always zero: used 63 * for zero-mapped memory areas etc.. 64 */ 65 extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]; 66 #define ZERO_PAGE(vaddr) phys_to_page(__pa_symbol(empty_zero_page)) 67 68 #define pte_ERROR(e) \ 69 pr_err("%s:%d: bad pte %016llx.\n", __FILE__, __LINE__, pte_val(e)) 70 71 #ifdef CONFIG_ARM64_PA_BITS_52 72 static inline phys_addr_t __pte_to_phys(pte_t pte) 73 { 74 pte_val(pte) &= ~PTE_MAYBE_SHARED; 75 return (pte_val(pte) & PTE_ADDR_LOW) | 76 ((pte_val(pte) & PTE_ADDR_HIGH) << PTE_ADDR_HIGH_SHIFT); 77 } 78 static inline pteval_t __phys_to_pte_val(phys_addr_t phys) 79 { 80 return (phys | (phys >> PTE_ADDR_HIGH_SHIFT)) & PHYS_TO_PTE_ADDR_MASK; 81 } 82 #else 83 static inline phys_addr_t __pte_to_phys(pte_t pte) 84 { 85 return pte_val(pte) & PTE_ADDR_LOW; 86 } 87 88 static inline pteval_t __phys_to_pte_val(phys_addr_t phys) 89 { 90 return phys; 91 } 92 #endif 93 94 #define pte_pfn(pte) (__pte_to_phys(pte) >> PAGE_SHIFT) 95 #define pfn_pte(pfn,prot) \ 96 __pte(__phys_to_pte_val((phys_addr_t)(pfn) << PAGE_SHIFT) | pgprot_val(prot)) 97 98 #define pte_none(pte) (!pte_val(pte)) 99 #define __pte_clear(mm, addr, ptep) \ 100 __set_pte(ptep, __pte(0)) 101 #define pte_page(pte) (pfn_to_page(pte_pfn(pte))) 102 103 /* 104 * The following only work if pte_present(). Undefined behaviour otherwise. 105 */ 106 #define pte_present(pte) (pte_valid(pte) || pte_present_invalid(pte)) 107 #define pte_young(pte) (!!(pte_val(pte) & PTE_AF)) 108 #define pte_special(pte) (!!(pte_val(pte) & PTE_SPECIAL)) 109 #define pte_write(pte) (!!(pte_val(pte) & PTE_WRITE)) 110 #define pte_rdonly(pte) (!!(pte_val(pte) & PTE_RDONLY)) 111 #define pte_user(pte) (!!(pte_val(pte) & PTE_USER)) 112 #define pte_user_exec(pte) (!(pte_val(pte) & PTE_UXN)) 113 #define pte_cont(pte) (!!(pte_val(pte) & PTE_CONT)) 114 #define pte_devmap(pte) (!!(pte_val(pte) & PTE_DEVMAP)) 115 #define pte_tagged(pte) ((pte_val(pte) & PTE_ATTRINDX_MASK) == \ 116 PTE_ATTRINDX(MT_NORMAL_TAGGED)) 117 118 #define pte_cont_addr_end(addr, end) \ 119 ({ unsigned long __boundary = ((addr) + CONT_PTE_SIZE) & CONT_PTE_MASK; \ 120 (__boundary - 1 < (end) - 1) ? __boundary : (end); \ 121 }) 122 123 #define pmd_cont_addr_end(addr, end) \ 124 ({ unsigned long __boundary = ((addr) + CONT_PMD_SIZE) & CONT_PMD_MASK; \ 125 (__boundary - 1 < (end) - 1) ? __boundary : (end); \ 126 }) 127 128 #define pte_hw_dirty(pte) (pte_write(pte) && !pte_rdonly(pte)) 129 #define pte_sw_dirty(pte) (!!(pte_val(pte) & PTE_DIRTY)) 130 #define pte_dirty(pte) (pte_sw_dirty(pte) || pte_hw_dirty(pte)) 131 132 #define pte_valid(pte) (!!(pte_val(pte) & PTE_VALID)) 133 #define pte_present_invalid(pte) \ 134 ((pte_val(pte) & (PTE_VALID | PTE_PRESENT_INVALID)) == PTE_PRESENT_INVALID) 135 /* 136 * Execute-only user mappings do not have the PTE_USER bit set. All valid 137 * kernel mappings have the PTE_UXN bit set. 138 */ 139 #define pte_valid_not_user(pte) \ 140 ((pte_val(pte) & (PTE_VALID | PTE_USER | PTE_UXN)) == (PTE_VALID | PTE_UXN)) 141 /* 142 * Returns true if the pte is valid and has the contiguous bit set. 143 */ 144 #define pte_valid_cont(pte) (pte_valid(pte) && pte_cont(pte)) 145 /* 146 * Could the pte be present in the TLB? We must check mm_tlb_flush_pending 147 * so that we don't erroneously return false for pages that have been 148 * remapped as PROT_NONE but are yet to be flushed from the TLB. 149 * Note that we can't make any assumptions based on the state of the access 150 * flag, since __ptep_clear_flush_young() elides a DSB when invalidating the 151 * TLB. 152 */ 153 #define pte_accessible(mm, pte) \ 154 (mm_tlb_flush_pending(mm) ? pte_present(pte) : pte_valid(pte)) 155 156 static inline bool por_el0_allows_pkey(u8 pkey, bool write, bool execute) 157 { 158 u64 por; 159 160 if (!system_supports_poe()) 161 return true; 162 163 por = read_sysreg_s(SYS_POR_EL0); 164 165 if (write) 166 return por_elx_allows_write(por, pkey); 167 168 if (execute) 169 return por_elx_allows_exec(por, pkey); 170 171 return por_elx_allows_read(por, pkey); 172 } 173 174 /* 175 * p??_access_permitted() is true for valid user mappings (PTE_USER 176 * bit set, subject to the write permission check). For execute-only 177 * mappings, like PROT_EXEC with EPAN (both PTE_USER and PTE_UXN bits 178 * not set) must return false. PROT_NONE mappings do not have the 179 * PTE_VALID bit set. 180 */ 181 #define pte_access_permitted_no_overlay(pte, write) \ 182 (((pte_val(pte) & (PTE_VALID | PTE_USER)) == (PTE_VALID | PTE_USER)) && (!(write) || pte_write(pte))) 183 #define pte_access_permitted(pte, write) \ 184 (pte_access_permitted_no_overlay(pte, write) && \ 185 por_el0_allows_pkey(FIELD_GET(PTE_PO_IDX_MASK, pte_val(pte)), write, false)) 186 #define pmd_access_permitted(pmd, write) \ 187 (pte_access_permitted(pmd_pte(pmd), (write))) 188 #define pud_access_permitted(pud, write) \ 189 (pte_access_permitted(pud_pte(pud), (write))) 190 191 static inline pte_t clear_pte_bit(pte_t pte, pgprot_t prot) 192 { 193 pte_val(pte) &= ~pgprot_val(prot); 194 return pte; 195 } 196 197 static inline pte_t set_pte_bit(pte_t pte, pgprot_t prot) 198 { 199 pte_val(pte) |= pgprot_val(prot); 200 return pte; 201 } 202 203 static inline pmd_t clear_pmd_bit(pmd_t pmd, pgprot_t prot) 204 { 205 pmd_val(pmd) &= ~pgprot_val(prot); 206 return pmd; 207 } 208 209 static inline pmd_t set_pmd_bit(pmd_t pmd, pgprot_t prot) 210 { 211 pmd_val(pmd) |= pgprot_val(prot); 212 return pmd; 213 } 214 215 static inline pte_t pte_mkwrite_novma(pte_t pte) 216 { 217 pte = set_pte_bit(pte, __pgprot(PTE_WRITE)); 218 pte = clear_pte_bit(pte, __pgprot(PTE_RDONLY)); 219 return pte; 220 } 221 222 static inline pte_t pte_mkclean(pte_t pte) 223 { 224 pte = clear_pte_bit(pte, __pgprot(PTE_DIRTY)); 225 pte = set_pte_bit(pte, __pgprot(PTE_RDONLY)); 226 227 return pte; 228 } 229 230 static inline pte_t pte_mkdirty(pte_t pte) 231 { 232 pte = set_pte_bit(pte, __pgprot(PTE_DIRTY)); 233 234 if (pte_write(pte)) 235 pte = clear_pte_bit(pte, __pgprot(PTE_RDONLY)); 236 237 return pte; 238 } 239 240 static inline pte_t pte_wrprotect(pte_t pte) 241 { 242 /* 243 * If hardware-dirty (PTE_WRITE/DBM bit set and PTE_RDONLY 244 * clear), set the PTE_DIRTY bit. 245 */ 246 if (pte_hw_dirty(pte)) 247 pte = set_pte_bit(pte, __pgprot(PTE_DIRTY)); 248 249 pte = clear_pte_bit(pte, __pgprot(PTE_WRITE)); 250 pte = set_pte_bit(pte, __pgprot(PTE_RDONLY)); 251 return pte; 252 } 253 254 static inline pte_t pte_mkold(pte_t pte) 255 { 256 return clear_pte_bit(pte, __pgprot(PTE_AF)); 257 } 258 259 static inline pte_t pte_mkyoung(pte_t pte) 260 { 261 return set_pte_bit(pte, __pgprot(PTE_AF)); 262 } 263 264 static inline pte_t pte_mkspecial(pte_t pte) 265 { 266 return set_pte_bit(pte, __pgprot(PTE_SPECIAL)); 267 } 268 269 static inline pte_t pte_mkcont(pte_t pte) 270 { 271 return set_pte_bit(pte, __pgprot(PTE_CONT)); 272 } 273 274 static inline pte_t pte_mknoncont(pte_t pte) 275 { 276 return clear_pte_bit(pte, __pgprot(PTE_CONT)); 277 } 278 279 static inline pte_t pte_mkvalid(pte_t pte) 280 { 281 return set_pte_bit(pte, __pgprot(PTE_VALID)); 282 } 283 284 static inline pte_t pte_mkinvalid(pte_t pte) 285 { 286 pte = set_pte_bit(pte, __pgprot(PTE_PRESENT_INVALID)); 287 pte = clear_pte_bit(pte, __pgprot(PTE_VALID)); 288 return pte; 289 } 290 291 static inline pmd_t pmd_mkcont(pmd_t pmd) 292 { 293 return __pmd(pmd_val(pmd) | PMD_SECT_CONT); 294 } 295 296 static inline pte_t pte_mkdevmap(pte_t pte) 297 { 298 return set_pte_bit(pte, __pgprot(PTE_DEVMAP | PTE_SPECIAL)); 299 } 300 301 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP 302 static inline int pte_uffd_wp(pte_t pte) 303 { 304 return !!(pte_val(pte) & PTE_UFFD_WP); 305 } 306 307 static inline pte_t pte_mkuffd_wp(pte_t pte) 308 { 309 return pte_wrprotect(set_pte_bit(pte, __pgprot(PTE_UFFD_WP))); 310 } 311 312 static inline pte_t pte_clear_uffd_wp(pte_t pte) 313 { 314 return clear_pte_bit(pte, __pgprot(PTE_UFFD_WP)); 315 } 316 #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */ 317 318 static inline void __set_pte_nosync(pte_t *ptep, pte_t pte) 319 { 320 WRITE_ONCE(*ptep, pte); 321 } 322 323 static inline void __set_pte(pte_t *ptep, pte_t pte) 324 { 325 __set_pte_nosync(ptep, pte); 326 327 /* 328 * Only if the new pte is valid and kernel, otherwise TLB maintenance 329 * or update_mmu_cache() have the necessary barriers. 330 */ 331 if (pte_valid_not_user(pte)) { 332 dsb(ishst); 333 isb(); 334 } 335 } 336 337 static inline pte_t __ptep_get(pte_t *ptep) 338 { 339 return READ_ONCE(*ptep); 340 } 341 342 extern void __sync_icache_dcache(pte_t pteval); 343 bool pgattr_change_is_safe(pteval_t old, pteval_t new); 344 345 /* 346 * PTE bits configuration in the presence of hardware Dirty Bit Management 347 * (PTE_WRITE == PTE_DBM): 348 * 349 * Dirty Writable | PTE_RDONLY PTE_WRITE PTE_DIRTY (sw) 350 * 0 0 | 1 0 0 351 * 0 1 | 1 1 0 352 * 1 0 | 1 0 1 353 * 1 1 | 0 1 x 354 * 355 * When hardware DBM is not present, the sofware PTE_DIRTY bit is updated via 356 * the page fault mechanism. Checking the dirty status of a pte becomes: 357 * 358 * PTE_DIRTY || (PTE_WRITE && !PTE_RDONLY) 359 */ 360 361 static inline void __check_safe_pte_update(struct mm_struct *mm, pte_t *ptep, 362 pte_t pte) 363 { 364 pte_t old_pte; 365 366 if (!IS_ENABLED(CONFIG_DEBUG_VM)) 367 return; 368 369 old_pte = __ptep_get(ptep); 370 371 if (!pte_valid(old_pte) || !pte_valid(pte)) 372 return; 373 if (mm != current->active_mm && atomic_read(&mm->mm_users) <= 1) 374 return; 375 376 /* 377 * Check for potential race with hardware updates of the pte 378 * (__ptep_set_access_flags safely changes valid ptes without going 379 * through an invalid entry). 380 */ 381 VM_WARN_ONCE(!pte_young(pte), 382 "%s: racy access flag clearing: 0x%016llx -> 0x%016llx", 383 __func__, pte_val(old_pte), pte_val(pte)); 384 VM_WARN_ONCE(pte_write(old_pte) && !pte_dirty(pte), 385 "%s: racy dirty state clearing: 0x%016llx -> 0x%016llx", 386 __func__, pte_val(old_pte), pte_val(pte)); 387 VM_WARN_ONCE(!pgattr_change_is_safe(pte_val(old_pte), pte_val(pte)), 388 "%s: unsafe attribute change: 0x%016llx -> 0x%016llx", 389 __func__, pte_val(old_pte), pte_val(pte)); 390 } 391 392 static inline void __sync_cache_and_tags(pte_t pte, unsigned int nr_pages) 393 { 394 if (pte_present(pte) && pte_user_exec(pte) && !pte_special(pte)) 395 __sync_icache_dcache(pte); 396 397 /* 398 * If the PTE would provide user space access to the tags associated 399 * with it then ensure that the MTE tags are synchronised. Although 400 * pte_access_permitted_no_overlay() returns false for exec only 401 * mappings, they don't expose tags (instruction fetches don't check 402 * tags). 403 */ 404 if (system_supports_mte() && pte_access_permitted_no_overlay(pte, false) && 405 !pte_special(pte) && pte_tagged(pte)) 406 mte_sync_tags(pte, nr_pages); 407 } 408 409 /* 410 * Select all bits except the pfn 411 */ 412 #define pte_pgprot pte_pgprot 413 static inline pgprot_t pte_pgprot(pte_t pte) 414 { 415 unsigned long pfn = pte_pfn(pte); 416 417 return __pgprot(pte_val(pfn_pte(pfn, __pgprot(0))) ^ pte_val(pte)); 418 } 419 420 #define pte_advance_pfn pte_advance_pfn 421 static inline pte_t pte_advance_pfn(pte_t pte, unsigned long nr) 422 { 423 return pfn_pte(pte_pfn(pte) + nr, pte_pgprot(pte)); 424 } 425 426 static inline void __set_ptes(struct mm_struct *mm, 427 unsigned long __always_unused addr, 428 pte_t *ptep, pte_t pte, unsigned int nr) 429 { 430 page_table_check_ptes_set(mm, ptep, pte, nr); 431 __sync_cache_and_tags(pte, nr); 432 433 for (;;) { 434 __check_safe_pte_update(mm, ptep, pte); 435 __set_pte(ptep, pte); 436 if (--nr == 0) 437 break; 438 ptep++; 439 pte = pte_advance_pfn(pte, 1); 440 } 441 } 442 443 /* 444 * Hugetlb definitions. 445 */ 446 #define HUGE_MAX_HSTATE 4 447 #define HPAGE_SHIFT PMD_SHIFT 448 #define HPAGE_SIZE (_AC(1, UL) << HPAGE_SHIFT) 449 #define HPAGE_MASK (~(HPAGE_SIZE - 1)) 450 #define HUGETLB_PAGE_ORDER (HPAGE_SHIFT - PAGE_SHIFT) 451 452 static inline pte_t pgd_pte(pgd_t pgd) 453 { 454 return __pte(pgd_val(pgd)); 455 } 456 457 static inline pte_t p4d_pte(p4d_t p4d) 458 { 459 return __pte(p4d_val(p4d)); 460 } 461 462 static inline pte_t pud_pte(pud_t pud) 463 { 464 return __pte(pud_val(pud)); 465 } 466 467 static inline pud_t pte_pud(pte_t pte) 468 { 469 return __pud(pte_val(pte)); 470 } 471 472 static inline pmd_t pud_pmd(pud_t pud) 473 { 474 return __pmd(pud_val(pud)); 475 } 476 477 static inline pte_t pmd_pte(pmd_t pmd) 478 { 479 return __pte(pmd_val(pmd)); 480 } 481 482 static inline pmd_t pte_pmd(pte_t pte) 483 { 484 return __pmd(pte_val(pte)); 485 } 486 487 static inline pgprot_t mk_pud_sect_prot(pgprot_t prot) 488 { 489 return __pgprot((pgprot_val(prot) & ~PUD_TYPE_MASK) | PUD_TYPE_SECT); 490 } 491 492 static inline pgprot_t mk_pmd_sect_prot(pgprot_t prot) 493 { 494 return __pgprot((pgprot_val(prot) & ~PMD_TYPE_MASK) | PMD_TYPE_SECT); 495 } 496 497 static inline pte_t pte_swp_mkexclusive(pte_t pte) 498 { 499 return set_pte_bit(pte, __pgprot(PTE_SWP_EXCLUSIVE)); 500 } 501 502 static inline int pte_swp_exclusive(pte_t pte) 503 { 504 return pte_val(pte) & PTE_SWP_EXCLUSIVE; 505 } 506 507 static inline pte_t pte_swp_clear_exclusive(pte_t pte) 508 { 509 return clear_pte_bit(pte, __pgprot(PTE_SWP_EXCLUSIVE)); 510 } 511 512 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP 513 static inline pte_t pte_swp_mkuffd_wp(pte_t pte) 514 { 515 return set_pte_bit(pte, __pgprot(PTE_SWP_UFFD_WP)); 516 } 517 518 static inline int pte_swp_uffd_wp(pte_t pte) 519 { 520 return !!(pte_val(pte) & PTE_SWP_UFFD_WP); 521 } 522 523 static inline pte_t pte_swp_clear_uffd_wp(pte_t pte) 524 { 525 return clear_pte_bit(pte, __pgprot(PTE_SWP_UFFD_WP)); 526 } 527 #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */ 528 529 #ifdef CONFIG_NUMA_BALANCING 530 /* 531 * See the comment in include/linux/pgtable.h 532 */ 533 static inline int pte_protnone(pte_t pte) 534 { 535 /* 536 * pte_present_invalid() tells us that the pte is invalid from HW 537 * perspective but present from SW perspective, so the fields are to be 538 * interpretted as per the HW layout. The second 2 checks are the unique 539 * encoding that we use for PROT_NONE. It is insufficient to only use 540 * the first check because we share the same encoding scheme with pmds 541 * which support pmd_mkinvalid(), so can be present-invalid without 542 * being PROT_NONE. 543 */ 544 return pte_present_invalid(pte) && !pte_user(pte) && !pte_user_exec(pte); 545 } 546 547 static inline int pmd_protnone(pmd_t pmd) 548 { 549 return pte_protnone(pmd_pte(pmd)); 550 } 551 #endif 552 553 #define pmd_present(pmd) pte_present(pmd_pte(pmd)) 554 #define pmd_dirty(pmd) pte_dirty(pmd_pte(pmd)) 555 #define pmd_young(pmd) pte_young(pmd_pte(pmd)) 556 #define pmd_valid(pmd) pte_valid(pmd_pte(pmd)) 557 #define pmd_user(pmd) pte_user(pmd_pte(pmd)) 558 #define pmd_user_exec(pmd) pte_user_exec(pmd_pte(pmd)) 559 #define pmd_cont(pmd) pte_cont(pmd_pte(pmd)) 560 #define pmd_wrprotect(pmd) pte_pmd(pte_wrprotect(pmd_pte(pmd))) 561 #define pmd_mkold(pmd) pte_pmd(pte_mkold(pmd_pte(pmd))) 562 #define pmd_mkwrite_novma(pmd) pte_pmd(pte_mkwrite_novma(pmd_pte(pmd))) 563 #define pmd_mkclean(pmd) pte_pmd(pte_mkclean(pmd_pte(pmd))) 564 #define pmd_mkdirty(pmd) pte_pmd(pte_mkdirty(pmd_pte(pmd))) 565 #define pmd_mkyoung(pmd) pte_pmd(pte_mkyoung(pmd_pte(pmd))) 566 #define pmd_mkinvalid(pmd) pte_pmd(pte_mkinvalid(pmd_pte(pmd))) 567 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP 568 #define pmd_uffd_wp(pmd) pte_uffd_wp(pmd_pte(pmd)) 569 #define pmd_mkuffd_wp(pmd) pte_pmd(pte_mkuffd_wp(pmd_pte(pmd))) 570 #define pmd_clear_uffd_wp(pmd) pte_pmd(pte_clear_uffd_wp(pmd_pte(pmd))) 571 #define pmd_swp_uffd_wp(pmd) pte_swp_uffd_wp(pmd_pte(pmd)) 572 #define pmd_swp_mkuffd_wp(pmd) pte_pmd(pte_swp_mkuffd_wp(pmd_pte(pmd))) 573 #define pmd_swp_clear_uffd_wp(pmd) \ 574 pte_pmd(pte_swp_clear_uffd_wp(pmd_pte(pmd))) 575 #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */ 576 577 #define pmd_write(pmd) pte_write(pmd_pte(pmd)) 578 579 static inline pmd_t pmd_mkhuge(pmd_t pmd) 580 { 581 /* 582 * It's possible that the pmd is present-invalid on entry 583 * and in that case it needs to remain present-invalid on 584 * exit. So ensure the VALID bit does not get modified. 585 */ 586 pmdval_t mask = PMD_TYPE_MASK & ~PTE_VALID; 587 pmdval_t val = PMD_TYPE_SECT & ~PTE_VALID; 588 589 return __pmd((pmd_val(pmd) & ~mask) | val); 590 } 591 592 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 593 #define pmd_devmap(pmd) pte_devmap(pmd_pte(pmd)) 594 #endif 595 static inline pmd_t pmd_mkdevmap(pmd_t pmd) 596 { 597 return pte_pmd(set_pte_bit(pmd_pte(pmd), __pgprot(PTE_DEVMAP))); 598 } 599 600 #ifdef CONFIG_ARCH_SUPPORTS_PMD_PFNMAP 601 #define pmd_special(pte) (!!((pmd_val(pte) & PTE_SPECIAL))) 602 static inline pmd_t pmd_mkspecial(pmd_t pmd) 603 { 604 return set_pmd_bit(pmd, __pgprot(PTE_SPECIAL)); 605 } 606 #endif 607 608 #define __pmd_to_phys(pmd) __pte_to_phys(pmd_pte(pmd)) 609 #define __phys_to_pmd_val(phys) __phys_to_pte_val(phys) 610 #define pmd_pfn(pmd) ((__pmd_to_phys(pmd) & PMD_MASK) >> PAGE_SHIFT) 611 #define pfn_pmd(pfn,prot) __pmd(__phys_to_pmd_val((phys_addr_t)(pfn) << PAGE_SHIFT) | pgprot_val(prot)) 612 613 #define pud_young(pud) pte_young(pud_pte(pud)) 614 #define pud_mkyoung(pud) pte_pud(pte_mkyoung(pud_pte(pud))) 615 #define pud_write(pud) pte_write(pud_pte(pud)) 616 617 static inline pud_t pud_mkhuge(pud_t pud) 618 { 619 /* 620 * It's possible that the pud is present-invalid on entry 621 * and in that case it needs to remain present-invalid on 622 * exit. So ensure the VALID bit does not get modified. 623 */ 624 pudval_t mask = PUD_TYPE_MASK & ~PTE_VALID; 625 pudval_t val = PUD_TYPE_SECT & ~PTE_VALID; 626 627 return __pud((pud_val(pud) & ~mask) | val); 628 } 629 630 #define __pud_to_phys(pud) __pte_to_phys(pud_pte(pud)) 631 #define __phys_to_pud_val(phys) __phys_to_pte_val(phys) 632 #define pud_pfn(pud) ((__pud_to_phys(pud) & PUD_MASK) >> PAGE_SHIFT) 633 #define pfn_pud(pfn,prot) __pud(__phys_to_pud_val((phys_addr_t)(pfn) << PAGE_SHIFT) | pgprot_val(prot)) 634 635 #define pmd_pgprot pmd_pgprot 636 static inline pgprot_t pmd_pgprot(pmd_t pmd) 637 { 638 unsigned long pfn = pmd_pfn(pmd); 639 640 return __pgprot(pmd_val(pfn_pmd(pfn, __pgprot(0))) ^ pmd_val(pmd)); 641 } 642 643 #define pud_pgprot pud_pgprot 644 static inline pgprot_t pud_pgprot(pud_t pud) 645 { 646 unsigned long pfn = pud_pfn(pud); 647 648 return __pgprot(pud_val(pfn_pud(pfn, __pgprot(0))) ^ pud_val(pud)); 649 } 650 651 static inline void __set_pte_at(struct mm_struct *mm, 652 unsigned long __always_unused addr, 653 pte_t *ptep, pte_t pte, unsigned int nr) 654 { 655 __sync_cache_and_tags(pte, nr); 656 __check_safe_pte_update(mm, ptep, pte); 657 __set_pte(ptep, pte); 658 } 659 660 static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr, 661 pmd_t *pmdp, pmd_t pmd) 662 { 663 page_table_check_pmd_set(mm, pmdp, pmd); 664 return __set_pte_at(mm, addr, (pte_t *)pmdp, pmd_pte(pmd), 665 PMD_SIZE >> PAGE_SHIFT); 666 } 667 668 static inline void set_pud_at(struct mm_struct *mm, unsigned long addr, 669 pud_t *pudp, pud_t pud) 670 { 671 page_table_check_pud_set(mm, pudp, pud); 672 return __set_pte_at(mm, addr, (pte_t *)pudp, pud_pte(pud), 673 PUD_SIZE >> PAGE_SHIFT); 674 } 675 676 #define __p4d_to_phys(p4d) __pte_to_phys(p4d_pte(p4d)) 677 #define __phys_to_p4d_val(phys) __phys_to_pte_val(phys) 678 679 #define __pgd_to_phys(pgd) __pte_to_phys(pgd_pte(pgd)) 680 #define __phys_to_pgd_val(phys) __phys_to_pte_val(phys) 681 682 #define __pgprot_modify(prot,mask,bits) \ 683 __pgprot((pgprot_val(prot) & ~(mask)) | (bits)) 684 685 #define pgprot_nx(prot) \ 686 __pgprot_modify(prot, PTE_MAYBE_GP, PTE_PXN) 687 688 #define pgprot_decrypted(prot) \ 689 __pgprot_modify(prot, PROT_NS_SHARED, PROT_NS_SHARED) 690 #define pgprot_encrypted(prot) \ 691 __pgprot_modify(prot, PROT_NS_SHARED, 0) 692 693 /* 694 * Mark the prot value as uncacheable and unbufferable. 695 */ 696 #define pgprot_noncached(prot) \ 697 __pgprot_modify(prot, PTE_ATTRINDX_MASK, PTE_ATTRINDX(MT_DEVICE_nGnRnE) | PTE_PXN | PTE_UXN) 698 #define pgprot_writecombine(prot) \ 699 __pgprot_modify(prot, PTE_ATTRINDX_MASK, PTE_ATTRINDX(MT_NORMAL_NC) | PTE_PXN | PTE_UXN) 700 #define pgprot_device(prot) \ 701 __pgprot_modify(prot, PTE_ATTRINDX_MASK, PTE_ATTRINDX(MT_DEVICE_nGnRE) | PTE_PXN | PTE_UXN) 702 #define pgprot_tagged(prot) \ 703 __pgprot_modify(prot, PTE_ATTRINDX_MASK, PTE_ATTRINDX(MT_NORMAL_TAGGED)) 704 #define pgprot_mhp pgprot_tagged 705 /* 706 * DMA allocations for non-coherent devices use what the Arm architecture calls 707 * "Normal non-cacheable" memory, which permits speculation, unaligned accesses 708 * and merging of writes. This is different from "Device-nGnR[nE]" memory which 709 * is intended for MMIO and thus forbids speculation, preserves access size, 710 * requires strict alignment and can also force write responses to come from the 711 * endpoint. 712 */ 713 #define pgprot_dmacoherent(prot) \ 714 __pgprot_modify(prot, PTE_ATTRINDX_MASK, \ 715 PTE_ATTRINDX(MT_NORMAL_NC) | PTE_PXN | PTE_UXN) 716 717 #define __HAVE_PHYS_MEM_ACCESS_PROT 718 struct file; 719 extern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, 720 unsigned long size, pgprot_t vma_prot); 721 722 #define pmd_none(pmd) (!pmd_val(pmd)) 723 724 #define pmd_table(pmd) ((pmd_val(pmd) & PMD_TYPE_MASK) == \ 725 PMD_TYPE_TABLE) 726 #define pmd_sect(pmd) ((pmd_val(pmd) & PMD_TYPE_MASK) == \ 727 PMD_TYPE_SECT) 728 #define pmd_leaf(pmd) (pmd_present(pmd) && !pmd_table(pmd)) 729 #define pmd_bad(pmd) (!pmd_table(pmd)) 730 731 #define pmd_leaf_size(pmd) (pmd_cont(pmd) ? CONT_PMD_SIZE : PMD_SIZE) 732 #define pte_leaf_size(pte) (pte_cont(pte) ? CONT_PTE_SIZE : PAGE_SIZE) 733 734 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 735 static inline int pmd_trans_huge(pmd_t pmd) 736 { 737 /* 738 * If pmd is present-invalid, pmd_table() won't detect it 739 * as a table, so force the valid bit for the comparison. 740 */ 741 return pmd_val(pmd) && pmd_present(pmd) && 742 !pmd_table(__pmd(pmd_val(pmd) | PTE_VALID)); 743 } 744 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 745 746 #if defined(CONFIG_ARM64_64K_PAGES) || CONFIG_PGTABLE_LEVELS < 3 747 static inline bool pud_sect(pud_t pud) { return false; } 748 static inline bool pud_table(pud_t pud) { return true; } 749 #else 750 #define pud_sect(pud) ((pud_val(pud) & PUD_TYPE_MASK) == \ 751 PUD_TYPE_SECT) 752 #define pud_table(pud) ((pud_val(pud) & PUD_TYPE_MASK) == \ 753 PUD_TYPE_TABLE) 754 #endif 755 756 extern pgd_t init_pg_dir[]; 757 extern pgd_t init_pg_end[]; 758 extern pgd_t swapper_pg_dir[]; 759 extern pgd_t idmap_pg_dir[]; 760 extern pgd_t tramp_pg_dir[]; 761 extern pgd_t reserved_pg_dir[]; 762 763 extern void set_swapper_pgd(pgd_t *pgdp, pgd_t pgd); 764 765 static inline bool in_swapper_pgdir(void *addr) 766 { 767 return ((unsigned long)addr & PAGE_MASK) == 768 ((unsigned long)swapper_pg_dir & PAGE_MASK); 769 } 770 771 static inline void set_pmd(pmd_t *pmdp, pmd_t pmd) 772 { 773 #ifdef __PAGETABLE_PMD_FOLDED 774 if (in_swapper_pgdir(pmdp)) { 775 set_swapper_pgd((pgd_t *)pmdp, __pgd(pmd_val(pmd))); 776 return; 777 } 778 #endif /* __PAGETABLE_PMD_FOLDED */ 779 780 WRITE_ONCE(*pmdp, pmd); 781 782 if (pmd_valid(pmd)) { 783 dsb(ishst); 784 isb(); 785 } 786 } 787 788 static inline void pmd_clear(pmd_t *pmdp) 789 { 790 set_pmd(pmdp, __pmd(0)); 791 } 792 793 static inline phys_addr_t pmd_page_paddr(pmd_t pmd) 794 { 795 return __pmd_to_phys(pmd); 796 } 797 798 static inline unsigned long pmd_page_vaddr(pmd_t pmd) 799 { 800 return (unsigned long)__va(pmd_page_paddr(pmd)); 801 } 802 803 /* Find an entry in the third-level page table. */ 804 #define pte_offset_phys(dir,addr) (pmd_page_paddr(READ_ONCE(*(dir))) + pte_index(addr) * sizeof(pte_t)) 805 806 #define pte_set_fixmap(addr) ((pte_t *)set_fixmap_offset(FIX_PTE, addr)) 807 #define pte_set_fixmap_offset(pmd, addr) pte_set_fixmap(pte_offset_phys(pmd, addr)) 808 #define pte_clear_fixmap() clear_fixmap(FIX_PTE) 809 810 #define pmd_page(pmd) phys_to_page(__pmd_to_phys(pmd)) 811 812 /* use ONLY for statically allocated translation tables */ 813 #define pte_offset_kimg(dir,addr) ((pte_t *)__phys_to_kimg(pte_offset_phys((dir), (addr)))) 814 815 #if CONFIG_PGTABLE_LEVELS > 2 816 817 #define pmd_ERROR(e) \ 818 pr_err("%s:%d: bad pmd %016llx.\n", __FILE__, __LINE__, pmd_val(e)) 819 820 #define pud_none(pud) (!pud_val(pud)) 821 #define pud_bad(pud) ((pud_val(pud) & PUD_TYPE_MASK) != \ 822 PUD_TYPE_TABLE) 823 #define pud_present(pud) pte_present(pud_pte(pud)) 824 #ifndef __PAGETABLE_PMD_FOLDED 825 #define pud_leaf(pud) (pud_present(pud) && !pud_table(pud)) 826 #else 827 #define pud_leaf(pud) false 828 #endif 829 #define pud_valid(pud) pte_valid(pud_pte(pud)) 830 #define pud_user(pud) pte_user(pud_pte(pud)) 831 #define pud_user_exec(pud) pte_user_exec(pud_pte(pud)) 832 833 static inline bool pgtable_l4_enabled(void); 834 835 static inline void set_pud(pud_t *pudp, pud_t pud) 836 { 837 if (!pgtable_l4_enabled() && in_swapper_pgdir(pudp)) { 838 set_swapper_pgd((pgd_t *)pudp, __pgd(pud_val(pud))); 839 return; 840 } 841 842 WRITE_ONCE(*pudp, pud); 843 844 if (pud_valid(pud)) { 845 dsb(ishst); 846 isb(); 847 } 848 } 849 850 static inline void pud_clear(pud_t *pudp) 851 { 852 set_pud(pudp, __pud(0)); 853 } 854 855 static inline phys_addr_t pud_page_paddr(pud_t pud) 856 { 857 return __pud_to_phys(pud); 858 } 859 860 static inline pmd_t *pud_pgtable(pud_t pud) 861 { 862 return (pmd_t *)__va(pud_page_paddr(pud)); 863 } 864 865 /* Find an entry in the second-level page table. */ 866 #define pmd_offset_phys(dir, addr) (pud_page_paddr(READ_ONCE(*(dir))) + pmd_index(addr) * sizeof(pmd_t)) 867 868 #define pmd_set_fixmap(addr) ((pmd_t *)set_fixmap_offset(FIX_PMD, addr)) 869 #define pmd_set_fixmap_offset(pud, addr) pmd_set_fixmap(pmd_offset_phys(pud, addr)) 870 #define pmd_clear_fixmap() clear_fixmap(FIX_PMD) 871 872 #define pud_page(pud) phys_to_page(__pud_to_phys(pud)) 873 874 /* use ONLY for statically allocated translation tables */ 875 #define pmd_offset_kimg(dir,addr) ((pmd_t *)__phys_to_kimg(pmd_offset_phys((dir), (addr)))) 876 877 #else 878 879 #define pud_valid(pud) false 880 #define pud_page_paddr(pud) ({ BUILD_BUG(); 0; }) 881 #define pud_user_exec(pud) pud_user(pud) /* Always 0 with folding */ 882 883 /* Match pmd_offset folding in <asm/generic/pgtable-nopmd.h> */ 884 #define pmd_set_fixmap(addr) NULL 885 #define pmd_set_fixmap_offset(pudp, addr) ((pmd_t *)pudp) 886 #define pmd_clear_fixmap() 887 888 #define pmd_offset_kimg(dir,addr) ((pmd_t *)dir) 889 890 #endif /* CONFIG_PGTABLE_LEVELS > 2 */ 891 892 #if CONFIG_PGTABLE_LEVELS > 3 893 894 static __always_inline bool pgtable_l4_enabled(void) 895 { 896 if (CONFIG_PGTABLE_LEVELS > 4 || !IS_ENABLED(CONFIG_ARM64_LPA2)) 897 return true; 898 if (!alternative_has_cap_likely(ARM64_ALWAYS_BOOT)) 899 return vabits_actual == VA_BITS; 900 return alternative_has_cap_unlikely(ARM64_HAS_VA52); 901 } 902 903 static inline bool mm_pud_folded(const struct mm_struct *mm) 904 { 905 return !pgtable_l4_enabled(); 906 } 907 #define mm_pud_folded mm_pud_folded 908 909 #define pud_ERROR(e) \ 910 pr_err("%s:%d: bad pud %016llx.\n", __FILE__, __LINE__, pud_val(e)) 911 912 #define p4d_none(p4d) (pgtable_l4_enabled() && !p4d_val(p4d)) 913 #define p4d_bad(p4d) (pgtable_l4_enabled() && \ 914 ((p4d_val(p4d) & P4D_TYPE_MASK) != \ 915 P4D_TYPE_TABLE)) 916 #define p4d_present(p4d) (!p4d_none(p4d)) 917 918 static inline void set_p4d(p4d_t *p4dp, p4d_t p4d) 919 { 920 if (in_swapper_pgdir(p4dp)) { 921 set_swapper_pgd((pgd_t *)p4dp, __pgd(p4d_val(p4d))); 922 return; 923 } 924 925 WRITE_ONCE(*p4dp, p4d); 926 dsb(ishst); 927 isb(); 928 } 929 930 static inline void p4d_clear(p4d_t *p4dp) 931 { 932 if (pgtable_l4_enabled()) 933 set_p4d(p4dp, __p4d(0)); 934 } 935 936 static inline phys_addr_t p4d_page_paddr(p4d_t p4d) 937 { 938 return __p4d_to_phys(p4d); 939 } 940 941 #define pud_index(addr) (((addr) >> PUD_SHIFT) & (PTRS_PER_PUD - 1)) 942 943 static inline pud_t *p4d_to_folded_pud(p4d_t *p4dp, unsigned long addr) 944 { 945 /* Ensure that 'p4dp' indexes a page table according to 'addr' */ 946 VM_BUG_ON(((addr >> P4D_SHIFT) ^ ((u64)p4dp >> 3)) % PTRS_PER_P4D); 947 948 return (pud_t *)PTR_ALIGN_DOWN(p4dp, PAGE_SIZE) + pud_index(addr); 949 } 950 951 static inline pud_t *p4d_pgtable(p4d_t p4d) 952 { 953 return (pud_t *)__va(p4d_page_paddr(p4d)); 954 } 955 956 static inline phys_addr_t pud_offset_phys(p4d_t *p4dp, unsigned long addr) 957 { 958 BUG_ON(!pgtable_l4_enabled()); 959 960 return p4d_page_paddr(READ_ONCE(*p4dp)) + pud_index(addr) * sizeof(pud_t); 961 } 962 963 static inline 964 pud_t *pud_offset_lockless(p4d_t *p4dp, p4d_t p4d, unsigned long addr) 965 { 966 if (!pgtable_l4_enabled()) 967 return p4d_to_folded_pud(p4dp, addr); 968 return (pud_t *)__va(p4d_page_paddr(p4d)) + pud_index(addr); 969 } 970 #define pud_offset_lockless pud_offset_lockless 971 972 static inline pud_t *pud_offset(p4d_t *p4dp, unsigned long addr) 973 { 974 return pud_offset_lockless(p4dp, READ_ONCE(*p4dp), addr); 975 } 976 #define pud_offset pud_offset 977 978 static inline pud_t *pud_set_fixmap(unsigned long addr) 979 { 980 if (!pgtable_l4_enabled()) 981 return NULL; 982 return (pud_t *)set_fixmap_offset(FIX_PUD, addr); 983 } 984 985 static inline pud_t *pud_set_fixmap_offset(p4d_t *p4dp, unsigned long addr) 986 { 987 if (!pgtable_l4_enabled()) 988 return p4d_to_folded_pud(p4dp, addr); 989 return pud_set_fixmap(pud_offset_phys(p4dp, addr)); 990 } 991 992 static inline void pud_clear_fixmap(void) 993 { 994 if (pgtable_l4_enabled()) 995 clear_fixmap(FIX_PUD); 996 } 997 998 /* use ONLY for statically allocated translation tables */ 999 static inline pud_t *pud_offset_kimg(p4d_t *p4dp, u64 addr) 1000 { 1001 if (!pgtable_l4_enabled()) 1002 return p4d_to_folded_pud(p4dp, addr); 1003 return (pud_t *)__phys_to_kimg(pud_offset_phys(p4dp, addr)); 1004 } 1005 1006 #define p4d_page(p4d) pfn_to_page(__phys_to_pfn(__p4d_to_phys(p4d))) 1007 1008 #else 1009 1010 static inline bool pgtable_l4_enabled(void) { return false; } 1011 1012 #define p4d_page_paddr(p4d) ({ BUILD_BUG(); 0;}) 1013 1014 /* Match pud_offset folding in <asm/generic/pgtable-nopud.h> */ 1015 #define pud_set_fixmap(addr) NULL 1016 #define pud_set_fixmap_offset(pgdp, addr) ((pud_t *)pgdp) 1017 #define pud_clear_fixmap() 1018 1019 #define pud_offset_kimg(dir,addr) ((pud_t *)dir) 1020 1021 #endif /* CONFIG_PGTABLE_LEVELS > 3 */ 1022 1023 #if CONFIG_PGTABLE_LEVELS > 4 1024 1025 static __always_inline bool pgtable_l5_enabled(void) 1026 { 1027 if (!alternative_has_cap_likely(ARM64_ALWAYS_BOOT)) 1028 return vabits_actual == VA_BITS; 1029 return alternative_has_cap_unlikely(ARM64_HAS_VA52); 1030 } 1031 1032 static inline bool mm_p4d_folded(const struct mm_struct *mm) 1033 { 1034 return !pgtable_l5_enabled(); 1035 } 1036 #define mm_p4d_folded mm_p4d_folded 1037 1038 #define p4d_ERROR(e) \ 1039 pr_err("%s:%d: bad p4d %016llx.\n", __FILE__, __LINE__, p4d_val(e)) 1040 1041 #define pgd_none(pgd) (pgtable_l5_enabled() && !pgd_val(pgd)) 1042 #define pgd_bad(pgd) (pgtable_l5_enabled() && \ 1043 ((pgd_val(pgd) & PGD_TYPE_MASK) != \ 1044 PGD_TYPE_TABLE)) 1045 #define pgd_present(pgd) (!pgd_none(pgd)) 1046 1047 static inline void set_pgd(pgd_t *pgdp, pgd_t pgd) 1048 { 1049 if (in_swapper_pgdir(pgdp)) { 1050 set_swapper_pgd(pgdp, __pgd(pgd_val(pgd))); 1051 return; 1052 } 1053 1054 WRITE_ONCE(*pgdp, pgd); 1055 dsb(ishst); 1056 isb(); 1057 } 1058 1059 static inline void pgd_clear(pgd_t *pgdp) 1060 { 1061 if (pgtable_l5_enabled()) 1062 set_pgd(pgdp, __pgd(0)); 1063 } 1064 1065 static inline phys_addr_t pgd_page_paddr(pgd_t pgd) 1066 { 1067 return __pgd_to_phys(pgd); 1068 } 1069 1070 #define p4d_index(addr) (((addr) >> P4D_SHIFT) & (PTRS_PER_P4D - 1)) 1071 1072 static inline p4d_t *pgd_to_folded_p4d(pgd_t *pgdp, unsigned long addr) 1073 { 1074 /* Ensure that 'pgdp' indexes a page table according to 'addr' */ 1075 VM_BUG_ON(((addr >> PGDIR_SHIFT) ^ ((u64)pgdp >> 3)) % PTRS_PER_PGD); 1076 1077 return (p4d_t *)PTR_ALIGN_DOWN(pgdp, PAGE_SIZE) + p4d_index(addr); 1078 } 1079 1080 static inline phys_addr_t p4d_offset_phys(pgd_t *pgdp, unsigned long addr) 1081 { 1082 BUG_ON(!pgtable_l5_enabled()); 1083 1084 return pgd_page_paddr(READ_ONCE(*pgdp)) + p4d_index(addr) * sizeof(p4d_t); 1085 } 1086 1087 static inline 1088 p4d_t *p4d_offset_lockless(pgd_t *pgdp, pgd_t pgd, unsigned long addr) 1089 { 1090 if (!pgtable_l5_enabled()) 1091 return pgd_to_folded_p4d(pgdp, addr); 1092 return (p4d_t *)__va(pgd_page_paddr(pgd)) + p4d_index(addr); 1093 } 1094 #define p4d_offset_lockless p4d_offset_lockless 1095 1096 static inline p4d_t *p4d_offset(pgd_t *pgdp, unsigned long addr) 1097 { 1098 return p4d_offset_lockless(pgdp, READ_ONCE(*pgdp), addr); 1099 } 1100 1101 static inline p4d_t *p4d_set_fixmap(unsigned long addr) 1102 { 1103 if (!pgtable_l5_enabled()) 1104 return NULL; 1105 return (p4d_t *)set_fixmap_offset(FIX_P4D, addr); 1106 } 1107 1108 static inline p4d_t *p4d_set_fixmap_offset(pgd_t *pgdp, unsigned long addr) 1109 { 1110 if (!pgtable_l5_enabled()) 1111 return pgd_to_folded_p4d(pgdp, addr); 1112 return p4d_set_fixmap(p4d_offset_phys(pgdp, addr)); 1113 } 1114 1115 static inline void p4d_clear_fixmap(void) 1116 { 1117 if (pgtable_l5_enabled()) 1118 clear_fixmap(FIX_P4D); 1119 } 1120 1121 /* use ONLY for statically allocated translation tables */ 1122 static inline p4d_t *p4d_offset_kimg(pgd_t *pgdp, u64 addr) 1123 { 1124 if (!pgtable_l5_enabled()) 1125 return pgd_to_folded_p4d(pgdp, addr); 1126 return (p4d_t *)__phys_to_kimg(p4d_offset_phys(pgdp, addr)); 1127 } 1128 1129 #define pgd_page(pgd) pfn_to_page(__phys_to_pfn(__pgd_to_phys(pgd))) 1130 1131 #else 1132 1133 static inline bool pgtable_l5_enabled(void) { return false; } 1134 1135 #define p4d_index(addr) (((addr) >> P4D_SHIFT) & (PTRS_PER_P4D - 1)) 1136 1137 /* Match p4d_offset folding in <asm/generic/pgtable-nop4d.h> */ 1138 #define p4d_set_fixmap(addr) NULL 1139 #define p4d_set_fixmap_offset(p4dp, addr) ((p4d_t *)p4dp) 1140 #define p4d_clear_fixmap() 1141 1142 #define p4d_offset_kimg(dir,addr) ((p4d_t *)dir) 1143 1144 static inline 1145 p4d_t *p4d_offset_lockless_folded(pgd_t *pgdp, pgd_t pgd, unsigned long addr) 1146 { 1147 /* 1148 * With runtime folding of the pud, pud_offset_lockless() passes 1149 * the 'pgd_t *' we return here to p4d_to_folded_pud(), which 1150 * will offset the pointer assuming that it points into 1151 * a page-table page. However, the fast GUP path passes us a 1152 * pgd_t allocated on the stack and so we must use the original 1153 * pointer in 'pgdp' to construct the p4d pointer instead of 1154 * using the generic p4d_offset_lockless() implementation. 1155 * 1156 * Note: reusing the original pointer means that we may 1157 * dereference the same (live) page-table entry multiple times. 1158 * This is safe because it is still only loaded once in the 1159 * context of each level and the CPU guarantees same-address 1160 * read-after-read ordering. 1161 */ 1162 return p4d_offset(pgdp, addr); 1163 } 1164 #define p4d_offset_lockless p4d_offset_lockless_folded 1165 1166 #endif /* CONFIG_PGTABLE_LEVELS > 4 */ 1167 1168 #define pgd_ERROR(e) \ 1169 pr_err("%s:%d: bad pgd %016llx.\n", __FILE__, __LINE__, pgd_val(e)) 1170 1171 #define pgd_set_fixmap(addr) ((pgd_t *)set_fixmap_offset(FIX_PGD, addr)) 1172 #define pgd_clear_fixmap() clear_fixmap(FIX_PGD) 1173 1174 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot) 1175 { 1176 /* 1177 * Normal and Normal-Tagged are two different memory types and indices 1178 * in MAIR_EL1. The mask below has to include PTE_ATTRINDX_MASK. 1179 */ 1180 const pteval_t mask = PTE_USER | PTE_PXN | PTE_UXN | PTE_RDONLY | 1181 PTE_PRESENT_INVALID | PTE_VALID | PTE_WRITE | 1182 PTE_GP | PTE_ATTRINDX_MASK | PTE_PO_IDX_MASK; 1183 1184 /* preserve the hardware dirty information */ 1185 if (pte_hw_dirty(pte)) 1186 pte = set_pte_bit(pte, __pgprot(PTE_DIRTY)); 1187 1188 pte_val(pte) = (pte_val(pte) & ~mask) | (pgprot_val(newprot) & mask); 1189 /* 1190 * If we end up clearing hw dirtiness for a sw-dirty PTE, set hardware 1191 * dirtiness again. 1192 */ 1193 if (pte_sw_dirty(pte)) 1194 pte = pte_mkdirty(pte); 1195 return pte; 1196 } 1197 1198 static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot) 1199 { 1200 return pte_pmd(pte_modify(pmd_pte(pmd), newprot)); 1201 } 1202 1203 extern int __ptep_set_access_flags(struct vm_area_struct *vma, 1204 unsigned long address, pte_t *ptep, 1205 pte_t entry, int dirty); 1206 1207 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1208 #define __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS 1209 static inline int pmdp_set_access_flags(struct vm_area_struct *vma, 1210 unsigned long address, pmd_t *pmdp, 1211 pmd_t entry, int dirty) 1212 { 1213 return __ptep_set_access_flags(vma, address, (pte_t *)pmdp, 1214 pmd_pte(entry), dirty); 1215 } 1216 1217 static inline int pud_devmap(pud_t pud) 1218 { 1219 return 0; 1220 } 1221 1222 static inline int pgd_devmap(pgd_t pgd) 1223 { 1224 return 0; 1225 } 1226 #endif 1227 1228 #ifdef CONFIG_PAGE_TABLE_CHECK 1229 static inline bool pte_user_accessible_page(pte_t pte) 1230 { 1231 return pte_valid(pte) && (pte_user(pte) || pte_user_exec(pte)); 1232 } 1233 1234 static inline bool pmd_user_accessible_page(pmd_t pmd) 1235 { 1236 return pmd_valid(pmd) && !pmd_table(pmd) && (pmd_user(pmd) || pmd_user_exec(pmd)); 1237 } 1238 1239 static inline bool pud_user_accessible_page(pud_t pud) 1240 { 1241 return pud_valid(pud) && !pud_table(pud) && (pud_user(pud) || pud_user_exec(pud)); 1242 } 1243 #endif 1244 1245 /* 1246 * Atomic pte/pmd modifications. 1247 */ 1248 static inline int __ptep_test_and_clear_young(struct vm_area_struct *vma, 1249 unsigned long address, 1250 pte_t *ptep) 1251 { 1252 pte_t old_pte, pte; 1253 1254 pte = __ptep_get(ptep); 1255 do { 1256 old_pte = pte; 1257 pte = pte_mkold(pte); 1258 pte_val(pte) = cmpxchg_relaxed(&pte_val(*ptep), 1259 pte_val(old_pte), pte_val(pte)); 1260 } while (pte_val(pte) != pte_val(old_pte)); 1261 1262 return pte_young(pte); 1263 } 1264 1265 static inline int __ptep_clear_flush_young(struct vm_area_struct *vma, 1266 unsigned long address, pte_t *ptep) 1267 { 1268 int young = __ptep_test_and_clear_young(vma, address, ptep); 1269 1270 if (young) { 1271 /* 1272 * We can elide the trailing DSB here since the worst that can 1273 * happen is that a CPU continues to use the young entry in its 1274 * TLB and we mistakenly reclaim the associated page. The 1275 * window for such an event is bounded by the next 1276 * context-switch, which provides a DSB to complete the TLB 1277 * invalidation. 1278 */ 1279 flush_tlb_page_nosync(vma, address); 1280 } 1281 1282 return young; 1283 } 1284 1285 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG) 1286 #define __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG 1287 static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma, 1288 unsigned long address, 1289 pmd_t *pmdp) 1290 { 1291 /* Operation applies to PMD table entry only if FEAT_HAFT is enabled */ 1292 VM_WARN_ON(pmd_table(READ_ONCE(*pmdp)) && !system_supports_haft()); 1293 return __ptep_test_and_clear_young(vma, address, (pte_t *)pmdp); 1294 } 1295 #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG */ 1296 1297 static inline pte_t __ptep_get_and_clear(struct mm_struct *mm, 1298 unsigned long address, pte_t *ptep) 1299 { 1300 pte_t pte = __pte(xchg_relaxed(&pte_val(*ptep), 0)); 1301 1302 page_table_check_pte_clear(mm, pte); 1303 1304 return pte; 1305 } 1306 1307 static inline void __clear_full_ptes(struct mm_struct *mm, unsigned long addr, 1308 pte_t *ptep, unsigned int nr, int full) 1309 { 1310 for (;;) { 1311 __ptep_get_and_clear(mm, addr, ptep); 1312 if (--nr == 0) 1313 break; 1314 ptep++; 1315 addr += PAGE_SIZE; 1316 } 1317 } 1318 1319 static inline pte_t __get_and_clear_full_ptes(struct mm_struct *mm, 1320 unsigned long addr, pte_t *ptep, 1321 unsigned int nr, int full) 1322 { 1323 pte_t pte, tmp_pte; 1324 1325 pte = __ptep_get_and_clear(mm, addr, ptep); 1326 while (--nr) { 1327 ptep++; 1328 addr += PAGE_SIZE; 1329 tmp_pte = __ptep_get_and_clear(mm, addr, ptep); 1330 if (pte_dirty(tmp_pte)) 1331 pte = pte_mkdirty(pte); 1332 if (pte_young(tmp_pte)) 1333 pte = pte_mkyoung(pte); 1334 } 1335 return pte; 1336 } 1337 1338 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1339 #define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR 1340 static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, 1341 unsigned long address, pmd_t *pmdp) 1342 { 1343 pmd_t pmd = __pmd(xchg_relaxed(&pmd_val(*pmdp), 0)); 1344 1345 page_table_check_pmd_clear(mm, pmd); 1346 1347 return pmd; 1348 } 1349 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 1350 1351 static inline void ___ptep_set_wrprotect(struct mm_struct *mm, 1352 unsigned long address, pte_t *ptep, 1353 pte_t pte) 1354 { 1355 pte_t old_pte; 1356 1357 do { 1358 old_pte = pte; 1359 pte = pte_wrprotect(pte); 1360 pte_val(pte) = cmpxchg_relaxed(&pte_val(*ptep), 1361 pte_val(old_pte), pte_val(pte)); 1362 } while (pte_val(pte) != pte_val(old_pte)); 1363 } 1364 1365 /* 1366 * __ptep_set_wrprotect - mark read-only while transferring potential hardware 1367 * dirty status (PTE_DBM && !PTE_RDONLY) to the software PTE_DIRTY bit. 1368 */ 1369 static inline void __ptep_set_wrprotect(struct mm_struct *mm, 1370 unsigned long address, pte_t *ptep) 1371 { 1372 ___ptep_set_wrprotect(mm, address, ptep, __ptep_get(ptep)); 1373 } 1374 1375 static inline void __wrprotect_ptes(struct mm_struct *mm, unsigned long address, 1376 pte_t *ptep, unsigned int nr) 1377 { 1378 unsigned int i; 1379 1380 for (i = 0; i < nr; i++, address += PAGE_SIZE, ptep++) 1381 __ptep_set_wrprotect(mm, address, ptep); 1382 } 1383 1384 static inline void __clear_young_dirty_pte(struct vm_area_struct *vma, 1385 unsigned long addr, pte_t *ptep, 1386 pte_t pte, cydp_t flags) 1387 { 1388 pte_t old_pte; 1389 1390 do { 1391 old_pte = pte; 1392 1393 if (flags & CYDP_CLEAR_YOUNG) 1394 pte = pte_mkold(pte); 1395 if (flags & CYDP_CLEAR_DIRTY) 1396 pte = pte_mkclean(pte); 1397 1398 pte_val(pte) = cmpxchg_relaxed(&pte_val(*ptep), 1399 pte_val(old_pte), pte_val(pte)); 1400 } while (pte_val(pte) != pte_val(old_pte)); 1401 } 1402 1403 static inline void __clear_young_dirty_ptes(struct vm_area_struct *vma, 1404 unsigned long addr, pte_t *ptep, 1405 unsigned int nr, cydp_t flags) 1406 { 1407 pte_t pte; 1408 1409 for (;;) { 1410 pte = __ptep_get(ptep); 1411 1412 if (flags == (CYDP_CLEAR_YOUNG | CYDP_CLEAR_DIRTY)) 1413 __set_pte(ptep, pte_mkclean(pte_mkold(pte))); 1414 else 1415 __clear_young_dirty_pte(vma, addr, ptep, pte, flags); 1416 1417 if (--nr == 0) 1418 break; 1419 ptep++; 1420 addr += PAGE_SIZE; 1421 } 1422 } 1423 1424 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1425 #define __HAVE_ARCH_PMDP_SET_WRPROTECT 1426 static inline void pmdp_set_wrprotect(struct mm_struct *mm, 1427 unsigned long address, pmd_t *pmdp) 1428 { 1429 __ptep_set_wrprotect(mm, address, (pte_t *)pmdp); 1430 } 1431 1432 #define pmdp_establish pmdp_establish 1433 static inline pmd_t pmdp_establish(struct vm_area_struct *vma, 1434 unsigned long address, pmd_t *pmdp, pmd_t pmd) 1435 { 1436 page_table_check_pmd_set(vma->vm_mm, pmdp, pmd); 1437 return __pmd(xchg_relaxed(&pmd_val(*pmdp), pmd_val(pmd))); 1438 } 1439 #endif 1440 1441 /* 1442 * Encode and decode a swap entry: 1443 * bits 0-1: present (must be zero) 1444 * bits 2: remember PG_anon_exclusive 1445 * bit 3: remember uffd-wp state 1446 * bits 6-10: swap type 1447 * bit 11: PTE_PRESENT_INVALID (must be zero) 1448 * bits 12-61: swap offset 1449 */ 1450 #define __SWP_TYPE_SHIFT 6 1451 #define __SWP_TYPE_BITS 5 1452 #define __SWP_TYPE_MASK ((1 << __SWP_TYPE_BITS) - 1) 1453 #define __SWP_OFFSET_SHIFT 12 1454 #define __SWP_OFFSET_BITS 50 1455 #define __SWP_OFFSET_MASK ((1UL << __SWP_OFFSET_BITS) - 1) 1456 1457 #define __swp_type(x) (((x).val >> __SWP_TYPE_SHIFT) & __SWP_TYPE_MASK) 1458 #define __swp_offset(x) (((x).val >> __SWP_OFFSET_SHIFT) & __SWP_OFFSET_MASK) 1459 #define __swp_entry(type,offset) ((swp_entry_t) { ((type) << __SWP_TYPE_SHIFT) | ((offset) << __SWP_OFFSET_SHIFT) }) 1460 1461 #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) }) 1462 #define __swp_entry_to_pte(swp) ((pte_t) { (swp).val }) 1463 1464 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION 1465 #define __pmd_to_swp_entry(pmd) ((swp_entry_t) { pmd_val(pmd) }) 1466 #define __swp_entry_to_pmd(swp) __pmd((swp).val) 1467 #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ 1468 1469 /* 1470 * Ensure that there are not more swap files than can be encoded in the kernel 1471 * PTEs. 1472 */ 1473 #define MAX_SWAPFILES_CHECK() BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > __SWP_TYPE_BITS) 1474 1475 #ifdef CONFIG_ARM64_MTE 1476 1477 #define __HAVE_ARCH_PREPARE_TO_SWAP 1478 extern int arch_prepare_to_swap(struct folio *folio); 1479 1480 #define __HAVE_ARCH_SWAP_INVALIDATE 1481 static inline void arch_swap_invalidate_page(int type, pgoff_t offset) 1482 { 1483 if (system_supports_mte()) 1484 mte_invalidate_tags(type, offset); 1485 } 1486 1487 static inline void arch_swap_invalidate_area(int type) 1488 { 1489 if (system_supports_mte()) 1490 mte_invalidate_tags_area(type); 1491 } 1492 1493 #define __HAVE_ARCH_SWAP_RESTORE 1494 extern void arch_swap_restore(swp_entry_t entry, struct folio *folio); 1495 1496 #endif /* CONFIG_ARM64_MTE */ 1497 1498 /* 1499 * On AArch64, the cache coherency is handled via the __set_ptes() function. 1500 */ 1501 static inline void update_mmu_cache_range(struct vm_fault *vmf, 1502 struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, 1503 unsigned int nr) 1504 { 1505 /* 1506 * We don't do anything here, so there's a very small chance of 1507 * us retaking a user fault which we just fixed up. The alternative 1508 * is doing a dsb(ishst), but that penalises the fastpath. 1509 */ 1510 } 1511 1512 #define update_mmu_cache(vma, addr, ptep) \ 1513 update_mmu_cache_range(NULL, vma, addr, ptep, 1) 1514 #define update_mmu_cache_pmd(vma, address, pmd) do { } while (0) 1515 1516 #ifdef CONFIG_ARM64_PA_BITS_52 1517 #define phys_to_ttbr(addr) (((addr) | ((addr) >> 46)) & TTBR_BADDR_MASK_52) 1518 #else 1519 #define phys_to_ttbr(addr) (addr) 1520 #endif 1521 1522 /* 1523 * On arm64 without hardware Access Flag, copying from user will fail because 1524 * the pte is old and cannot be marked young. So we always end up with zeroed 1525 * page after fork() + CoW for pfn mappings. We don't always have a 1526 * hardware-managed access flag on arm64. 1527 */ 1528 #define arch_has_hw_pte_young cpu_has_hw_af 1529 1530 #ifdef CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG 1531 #define arch_has_hw_nonleaf_pmd_young system_supports_haft 1532 #endif 1533 1534 /* 1535 * Experimentally, it's cheap to set the access flag in hardware and we 1536 * benefit from prefaulting mappings as 'old' to start with. 1537 */ 1538 #define arch_wants_old_prefaulted_pte cpu_has_hw_af 1539 1540 static inline bool pud_sect_supported(void) 1541 { 1542 return PAGE_SIZE == SZ_4K; 1543 } 1544 1545 1546 #define __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION 1547 #define ptep_modify_prot_start ptep_modify_prot_start 1548 extern pte_t ptep_modify_prot_start(struct vm_area_struct *vma, 1549 unsigned long addr, pte_t *ptep); 1550 1551 #define ptep_modify_prot_commit ptep_modify_prot_commit 1552 extern void ptep_modify_prot_commit(struct vm_area_struct *vma, 1553 unsigned long addr, pte_t *ptep, 1554 pte_t old_pte, pte_t new_pte); 1555 1556 #ifdef CONFIG_ARM64_CONTPTE 1557 1558 /* 1559 * The contpte APIs are used to transparently manage the contiguous bit in ptes 1560 * where it is possible and makes sense to do so. The PTE_CONT bit is considered 1561 * a private implementation detail of the public ptep API (see below). 1562 */ 1563 extern void __contpte_try_fold(struct mm_struct *mm, unsigned long addr, 1564 pte_t *ptep, pte_t pte); 1565 extern void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr, 1566 pte_t *ptep, pte_t pte); 1567 extern pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte); 1568 extern pte_t contpte_ptep_get_lockless(pte_t *orig_ptep); 1569 extern void contpte_set_ptes(struct mm_struct *mm, unsigned long addr, 1570 pte_t *ptep, pte_t pte, unsigned int nr); 1571 extern void contpte_clear_full_ptes(struct mm_struct *mm, unsigned long addr, 1572 pte_t *ptep, unsigned int nr, int full); 1573 extern pte_t contpte_get_and_clear_full_ptes(struct mm_struct *mm, 1574 unsigned long addr, pte_t *ptep, 1575 unsigned int nr, int full); 1576 extern int contpte_ptep_test_and_clear_young(struct vm_area_struct *vma, 1577 unsigned long addr, pte_t *ptep); 1578 extern int contpte_ptep_clear_flush_young(struct vm_area_struct *vma, 1579 unsigned long addr, pte_t *ptep); 1580 extern void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr, 1581 pte_t *ptep, unsigned int nr); 1582 extern int contpte_ptep_set_access_flags(struct vm_area_struct *vma, 1583 unsigned long addr, pte_t *ptep, 1584 pte_t entry, int dirty); 1585 extern void contpte_clear_young_dirty_ptes(struct vm_area_struct *vma, 1586 unsigned long addr, pte_t *ptep, 1587 unsigned int nr, cydp_t flags); 1588 1589 static __always_inline void contpte_try_fold(struct mm_struct *mm, 1590 unsigned long addr, pte_t *ptep, pte_t pte) 1591 { 1592 /* 1593 * Only bother trying if both the virtual and physical addresses are 1594 * aligned and correspond to the last entry in a contig range. The core 1595 * code mostly modifies ranges from low to high, so this is the likely 1596 * the last modification in the contig range, so a good time to fold. 1597 * We can't fold special mappings, because there is no associated folio. 1598 */ 1599 1600 const unsigned long contmask = CONT_PTES - 1; 1601 bool valign = ((addr >> PAGE_SHIFT) & contmask) == contmask; 1602 1603 if (unlikely(valign)) { 1604 bool palign = (pte_pfn(pte) & contmask) == contmask; 1605 1606 if (unlikely(palign && 1607 pte_valid(pte) && !pte_cont(pte) && !pte_special(pte))) 1608 __contpte_try_fold(mm, addr, ptep, pte); 1609 } 1610 } 1611 1612 static __always_inline void contpte_try_unfold(struct mm_struct *mm, 1613 unsigned long addr, pte_t *ptep, pte_t pte) 1614 { 1615 if (unlikely(pte_valid_cont(pte))) 1616 __contpte_try_unfold(mm, addr, ptep, pte); 1617 } 1618 1619 #define pte_batch_hint pte_batch_hint 1620 static inline unsigned int pte_batch_hint(pte_t *ptep, pte_t pte) 1621 { 1622 if (!pte_valid_cont(pte)) 1623 return 1; 1624 1625 return CONT_PTES - (((unsigned long)ptep >> 3) & (CONT_PTES - 1)); 1626 } 1627 1628 /* 1629 * The below functions constitute the public API that arm64 presents to the 1630 * core-mm to manipulate PTE entries within their page tables (or at least this 1631 * is the subset of the API that arm64 needs to implement). These public 1632 * versions will automatically and transparently apply the contiguous bit where 1633 * it makes sense to do so. Therefore any users that are contig-aware (e.g. 1634 * hugetlb, kernel mapper) should NOT use these APIs, but instead use the 1635 * private versions, which are prefixed with double underscore. All of these 1636 * APIs except for ptep_get_lockless() are expected to be called with the PTL 1637 * held. Although the contiguous bit is considered private to the 1638 * implementation, it is deliberately allowed to leak through the getters (e.g. 1639 * ptep_get()), back to core code. This is required so that pte_leaf_size() can 1640 * provide an accurate size for perf_get_pgtable_size(). But this leakage means 1641 * its possible a pte will be passed to a setter with the contiguous bit set, so 1642 * we explicitly clear the contiguous bit in those cases to prevent accidentally 1643 * setting it in the pgtable. 1644 */ 1645 1646 #define ptep_get ptep_get 1647 static inline pte_t ptep_get(pte_t *ptep) 1648 { 1649 pte_t pte = __ptep_get(ptep); 1650 1651 if (likely(!pte_valid_cont(pte))) 1652 return pte; 1653 1654 return contpte_ptep_get(ptep, pte); 1655 } 1656 1657 #define ptep_get_lockless ptep_get_lockless 1658 static inline pte_t ptep_get_lockless(pte_t *ptep) 1659 { 1660 pte_t pte = __ptep_get(ptep); 1661 1662 if (likely(!pte_valid_cont(pte))) 1663 return pte; 1664 1665 return contpte_ptep_get_lockless(ptep); 1666 } 1667 1668 static inline void set_pte(pte_t *ptep, pte_t pte) 1669 { 1670 /* 1671 * We don't have the mm or vaddr so cannot unfold contig entries (since 1672 * it requires tlb maintenance). set_pte() is not used in core code, so 1673 * this should never even be called. Regardless do our best to service 1674 * any call and emit a warning if there is any attempt to set a pte on 1675 * top of an existing contig range. 1676 */ 1677 pte_t orig_pte = __ptep_get(ptep); 1678 1679 WARN_ON_ONCE(pte_valid_cont(orig_pte)); 1680 __set_pte(ptep, pte_mknoncont(pte)); 1681 } 1682 1683 #define set_ptes set_ptes 1684 static __always_inline void set_ptes(struct mm_struct *mm, unsigned long addr, 1685 pte_t *ptep, pte_t pte, unsigned int nr) 1686 { 1687 pte = pte_mknoncont(pte); 1688 1689 if (likely(nr == 1)) { 1690 contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); 1691 __set_ptes(mm, addr, ptep, pte, 1); 1692 contpte_try_fold(mm, addr, ptep, pte); 1693 } else { 1694 contpte_set_ptes(mm, addr, ptep, pte, nr); 1695 } 1696 } 1697 1698 static inline void pte_clear(struct mm_struct *mm, 1699 unsigned long addr, pte_t *ptep) 1700 { 1701 contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); 1702 __pte_clear(mm, addr, ptep); 1703 } 1704 1705 #define clear_full_ptes clear_full_ptes 1706 static inline void clear_full_ptes(struct mm_struct *mm, unsigned long addr, 1707 pte_t *ptep, unsigned int nr, int full) 1708 { 1709 if (likely(nr == 1)) { 1710 contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); 1711 __clear_full_ptes(mm, addr, ptep, nr, full); 1712 } else { 1713 contpte_clear_full_ptes(mm, addr, ptep, nr, full); 1714 } 1715 } 1716 1717 #define get_and_clear_full_ptes get_and_clear_full_ptes 1718 static inline pte_t get_and_clear_full_ptes(struct mm_struct *mm, 1719 unsigned long addr, pte_t *ptep, 1720 unsigned int nr, int full) 1721 { 1722 pte_t pte; 1723 1724 if (likely(nr == 1)) { 1725 contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); 1726 pte = __get_and_clear_full_ptes(mm, addr, ptep, nr, full); 1727 } else { 1728 pte = contpte_get_and_clear_full_ptes(mm, addr, ptep, nr, full); 1729 } 1730 1731 return pte; 1732 } 1733 1734 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR 1735 static inline pte_t ptep_get_and_clear(struct mm_struct *mm, 1736 unsigned long addr, pte_t *ptep) 1737 { 1738 contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); 1739 return __ptep_get_and_clear(mm, addr, ptep); 1740 } 1741 1742 #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG 1743 static inline int ptep_test_and_clear_young(struct vm_area_struct *vma, 1744 unsigned long addr, pte_t *ptep) 1745 { 1746 pte_t orig_pte = __ptep_get(ptep); 1747 1748 if (likely(!pte_valid_cont(orig_pte))) 1749 return __ptep_test_and_clear_young(vma, addr, ptep); 1750 1751 return contpte_ptep_test_and_clear_young(vma, addr, ptep); 1752 } 1753 1754 #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH 1755 static inline int ptep_clear_flush_young(struct vm_area_struct *vma, 1756 unsigned long addr, pte_t *ptep) 1757 { 1758 pte_t orig_pte = __ptep_get(ptep); 1759 1760 if (likely(!pte_valid_cont(orig_pte))) 1761 return __ptep_clear_flush_young(vma, addr, ptep); 1762 1763 return contpte_ptep_clear_flush_young(vma, addr, ptep); 1764 } 1765 1766 #define wrprotect_ptes wrprotect_ptes 1767 static __always_inline void wrprotect_ptes(struct mm_struct *mm, 1768 unsigned long addr, pte_t *ptep, unsigned int nr) 1769 { 1770 if (likely(nr == 1)) { 1771 /* 1772 * Optimization: wrprotect_ptes() can only be called for present 1773 * ptes so we only need to check contig bit as condition for 1774 * unfold, and we can remove the contig bit from the pte we read 1775 * to avoid re-reading. This speeds up fork() which is sensitive 1776 * for order-0 folios. Equivalent to contpte_try_unfold(). 1777 */ 1778 pte_t orig_pte = __ptep_get(ptep); 1779 1780 if (unlikely(pte_cont(orig_pte))) { 1781 __contpte_try_unfold(mm, addr, ptep, orig_pte); 1782 orig_pte = pte_mknoncont(orig_pte); 1783 } 1784 ___ptep_set_wrprotect(mm, addr, ptep, orig_pte); 1785 } else { 1786 contpte_wrprotect_ptes(mm, addr, ptep, nr); 1787 } 1788 } 1789 1790 #define __HAVE_ARCH_PTEP_SET_WRPROTECT 1791 static inline void ptep_set_wrprotect(struct mm_struct *mm, 1792 unsigned long addr, pte_t *ptep) 1793 { 1794 wrprotect_ptes(mm, addr, ptep, 1); 1795 } 1796 1797 #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS 1798 static inline int ptep_set_access_flags(struct vm_area_struct *vma, 1799 unsigned long addr, pte_t *ptep, 1800 pte_t entry, int dirty) 1801 { 1802 pte_t orig_pte = __ptep_get(ptep); 1803 1804 entry = pte_mknoncont(entry); 1805 1806 if (likely(!pte_valid_cont(orig_pte))) 1807 return __ptep_set_access_flags(vma, addr, ptep, entry, dirty); 1808 1809 return contpte_ptep_set_access_flags(vma, addr, ptep, entry, dirty); 1810 } 1811 1812 #define clear_young_dirty_ptes clear_young_dirty_ptes 1813 static inline void clear_young_dirty_ptes(struct vm_area_struct *vma, 1814 unsigned long addr, pte_t *ptep, 1815 unsigned int nr, cydp_t flags) 1816 { 1817 if (likely(nr == 1 && !pte_cont(__ptep_get(ptep)))) 1818 __clear_young_dirty_ptes(vma, addr, ptep, nr, flags); 1819 else 1820 contpte_clear_young_dirty_ptes(vma, addr, ptep, nr, flags); 1821 } 1822 1823 #else /* CONFIG_ARM64_CONTPTE */ 1824 1825 #define ptep_get __ptep_get 1826 #define set_pte __set_pte 1827 #define set_ptes __set_ptes 1828 #define pte_clear __pte_clear 1829 #define clear_full_ptes __clear_full_ptes 1830 #define get_and_clear_full_ptes __get_and_clear_full_ptes 1831 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR 1832 #define ptep_get_and_clear __ptep_get_and_clear 1833 #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG 1834 #define ptep_test_and_clear_young __ptep_test_and_clear_young 1835 #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH 1836 #define ptep_clear_flush_young __ptep_clear_flush_young 1837 #define __HAVE_ARCH_PTEP_SET_WRPROTECT 1838 #define ptep_set_wrprotect __ptep_set_wrprotect 1839 #define wrprotect_ptes __wrprotect_ptes 1840 #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS 1841 #define ptep_set_access_flags __ptep_set_access_flags 1842 #define clear_young_dirty_ptes __clear_young_dirty_ptes 1843 1844 #endif /* CONFIG_ARM64_CONTPTE */ 1845 1846 #endif /* !__ASSEMBLY__ */ 1847 1848 #endif /* __ASM_PGTABLE_H */ 1849