18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci#ifndef _ASM_POWERPC_BOOK3S_32_PGTABLE_H 38c2ecf20Sopenharmony_ci#define _ASM_POWERPC_BOOK3S_32_PGTABLE_H 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci#include <asm-generic/pgtable-nopmd.h> 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#include <asm/book3s/32/hash.h> 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci/* And here we include common definitions */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#define _PAGE_KERNEL_RO 0 128c2ecf20Sopenharmony_ci#define _PAGE_KERNEL_ROX (_PAGE_EXEC) 138c2ecf20Sopenharmony_ci#define _PAGE_KERNEL_RW (_PAGE_DIRTY | _PAGE_RW) 148c2ecf20Sopenharmony_ci#define _PAGE_KERNEL_RWX (_PAGE_DIRTY | _PAGE_RW | _PAGE_EXEC) 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#define _PAGE_HPTEFLAGS _PAGE_HASHPTE 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__ 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_cistatic inline bool pte_user(pte_t pte) 218c2ecf20Sopenharmony_ci{ 228c2ecf20Sopenharmony_ci return pte_val(pte) & _PAGE_USER; 238c2ecf20Sopenharmony_ci} 248c2ecf20Sopenharmony_ci#endif /* __ASSEMBLY__ */ 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci/* 278c2ecf20Sopenharmony_ci * Location of the PFN in the PTE. Most 32-bit platforms use the same 288c2ecf20Sopenharmony_ci * as _PAGE_SHIFT here (ie, naturally aligned). 298c2ecf20Sopenharmony_ci * Platform who don't just pre-define the value so we don't override it here. 308c2ecf20Sopenharmony_ci */ 318c2ecf20Sopenharmony_ci#define PTE_RPN_SHIFT (PAGE_SHIFT) 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci/* 348c2ecf20Sopenharmony_ci * The mask covered by the RPN must be a ULL on 32-bit platforms with 358c2ecf20Sopenharmony_ci * 64-bit PTEs. 368c2ecf20Sopenharmony_ci */ 378c2ecf20Sopenharmony_ci#ifdef CONFIG_PTE_64BIT 388c2ecf20Sopenharmony_ci#define PTE_RPN_MASK (~((1ULL << PTE_RPN_SHIFT) - 1)) 398c2ecf20Sopenharmony_ci#define MAX_POSSIBLE_PHYSMEM_BITS 36 408c2ecf20Sopenharmony_ci#else 418c2ecf20Sopenharmony_ci#define PTE_RPN_MASK (~((1UL << PTE_RPN_SHIFT) - 1)) 428c2ecf20Sopenharmony_ci#define MAX_POSSIBLE_PHYSMEM_BITS 32 438c2ecf20Sopenharmony_ci#endif 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci/* 468c2ecf20Sopenharmony_ci * _PAGE_CHG_MASK masks of bits that are to be preserved across 478c2ecf20Sopenharmony_ci * pgprot changes. 488c2ecf20Sopenharmony_ci */ 498c2ecf20Sopenharmony_ci#define _PAGE_CHG_MASK (PTE_RPN_MASK | _PAGE_HASHPTE | _PAGE_DIRTY | \ 508c2ecf20Sopenharmony_ci _PAGE_ACCESSED | _PAGE_SPECIAL) 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci/* 538c2ecf20Sopenharmony_ci * We define 2 sets of base prot bits, one for basic pages (ie, 548c2ecf20Sopenharmony_ci * cacheable kernel and user pages) and one for non cacheable 558c2ecf20Sopenharmony_ci * pages. We always set _PAGE_COHERENT when SMP is enabled or 568c2ecf20Sopenharmony_ci * the processor might need it for DMA coherency. 578c2ecf20Sopenharmony_ci */ 588c2ecf20Sopenharmony_ci#define _PAGE_BASE_NC (_PAGE_PRESENT | _PAGE_ACCESSED) 598c2ecf20Sopenharmony_ci#define _PAGE_BASE (_PAGE_BASE_NC | _PAGE_COHERENT) 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci/* 628c2ecf20Sopenharmony_ci * Permission masks used to generate the __P and __S table. 638c2ecf20Sopenharmony_ci * 648c2ecf20Sopenharmony_ci * Note:__pgprot is defined in arch/powerpc/include/asm/page.h 658c2ecf20Sopenharmony_ci * 668c2ecf20Sopenharmony_ci * Write permissions imply read permissions for now. 678c2ecf20Sopenharmony_ci */ 688c2ecf20Sopenharmony_ci#define PAGE_NONE __pgprot(_PAGE_BASE) 698c2ecf20Sopenharmony_ci#define PAGE_SHARED __pgprot(_PAGE_BASE | _PAGE_USER | _PAGE_RW) 708c2ecf20Sopenharmony_ci#define PAGE_SHARED_X __pgprot(_PAGE_BASE | _PAGE_USER | _PAGE_RW | _PAGE_EXEC) 718c2ecf20Sopenharmony_ci#define PAGE_COPY __pgprot(_PAGE_BASE | _PAGE_USER) 728c2ecf20Sopenharmony_ci#define PAGE_COPY_X __pgprot(_PAGE_BASE | _PAGE_USER | _PAGE_EXEC) 738c2ecf20Sopenharmony_ci#define PAGE_READONLY __pgprot(_PAGE_BASE | _PAGE_USER) 748c2ecf20Sopenharmony_ci#define PAGE_READONLY_X __pgprot(_PAGE_BASE | _PAGE_USER | _PAGE_EXEC) 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci/* Permission masks used for kernel mappings */ 778c2ecf20Sopenharmony_ci#define PAGE_KERNEL __pgprot(_PAGE_BASE | _PAGE_KERNEL_RW) 788c2ecf20Sopenharmony_ci#define PAGE_KERNEL_NC __pgprot(_PAGE_BASE_NC | _PAGE_KERNEL_RW | _PAGE_NO_CACHE) 798c2ecf20Sopenharmony_ci#define PAGE_KERNEL_NCG __pgprot(_PAGE_BASE_NC | _PAGE_KERNEL_RW | \ 808c2ecf20Sopenharmony_ci _PAGE_NO_CACHE | _PAGE_GUARDED) 818c2ecf20Sopenharmony_ci#define PAGE_KERNEL_X __pgprot(_PAGE_BASE | _PAGE_KERNEL_RWX) 828c2ecf20Sopenharmony_ci#define PAGE_KERNEL_RO __pgprot(_PAGE_BASE | _PAGE_KERNEL_RO) 838c2ecf20Sopenharmony_ci#define PAGE_KERNEL_ROX __pgprot(_PAGE_BASE | _PAGE_KERNEL_ROX) 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci/* 868c2ecf20Sopenharmony_ci * Protection used for kernel text. We want the debuggers to be able to 878c2ecf20Sopenharmony_ci * set breakpoints anywhere, so don't write protect the kernel text 888c2ecf20Sopenharmony_ci * on platforms where such control is possible. 898c2ecf20Sopenharmony_ci */ 908c2ecf20Sopenharmony_ci#if defined(CONFIG_KGDB) || defined(CONFIG_XMON) || defined(CONFIG_BDI_SWITCH) ||\ 918c2ecf20Sopenharmony_ci defined(CONFIG_KPROBES) || defined(CONFIG_DYNAMIC_FTRACE) 928c2ecf20Sopenharmony_ci#define PAGE_KERNEL_TEXT PAGE_KERNEL_X 938c2ecf20Sopenharmony_ci#else 948c2ecf20Sopenharmony_ci#define PAGE_KERNEL_TEXT PAGE_KERNEL_ROX 958c2ecf20Sopenharmony_ci#endif 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci/* Make modules code happy. We don't set RO yet */ 988c2ecf20Sopenharmony_ci#define PAGE_KERNEL_EXEC PAGE_KERNEL_X 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci/* Advertise special mapping type for AGP */ 1018c2ecf20Sopenharmony_ci#define PAGE_AGP (PAGE_KERNEL_NC) 1028c2ecf20Sopenharmony_ci#define HAVE_PAGE_AGP 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci#define PTE_INDEX_SIZE PTE_SHIFT 1058c2ecf20Sopenharmony_ci#define PMD_INDEX_SIZE 0 1068c2ecf20Sopenharmony_ci#define PUD_INDEX_SIZE 0 1078c2ecf20Sopenharmony_ci#define PGD_INDEX_SIZE (32 - PGDIR_SHIFT) 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci#define PMD_CACHE_INDEX PMD_INDEX_SIZE 1108c2ecf20Sopenharmony_ci#define PUD_CACHE_INDEX PUD_INDEX_SIZE 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__ 1138c2ecf20Sopenharmony_ci#define PTE_TABLE_SIZE (sizeof(pte_t) << PTE_INDEX_SIZE) 1148c2ecf20Sopenharmony_ci#define PMD_TABLE_SIZE 0 1158c2ecf20Sopenharmony_ci#define PUD_TABLE_SIZE 0 1168c2ecf20Sopenharmony_ci#define PGD_TABLE_SIZE (sizeof(pgd_t) << PGD_INDEX_SIZE) 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci/* Bits to mask out from a PMD to get to the PTE page */ 1198c2ecf20Sopenharmony_ci#define PMD_MASKED_BITS (PTE_TABLE_SIZE - 1) 1208c2ecf20Sopenharmony_ci#endif /* __ASSEMBLY__ */ 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci#define PTRS_PER_PTE (1 << PTE_INDEX_SIZE) 1238c2ecf20Sopenharmony_ci#define PTRS_PER_PGD (1 << PGD_INDEX_SIZE) 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci/* 1268c2ecf20Sopenharmony_ci * The normal case is that PTEs are 32-bits and we have a 1-page 1278c2ecf20Sopenharmony_ci * 1024-entry pgdir pointing to 1-page 1024-entry PTE pages. -- paulus 1288c2ecf20Sopenharmony_ci * 1298c2ecf20Sopenharmony_ci * For any >32-bit physical address platform, we can use the following 1308c2ecf20Sopenharmony_ci * two level page table layout where the pgdir is 8KB and the MS 13 bits 1318c2ecf20Sopenharmony_ci * are an index to the second level table. The combined pgdir/pmd first 1328c2ecf20Sopenharmony_ci * level has 2048 entries and the second level has 512 64-bit PTE entries. 1338c2ecf20Sopenharmony_ci * -Matt 1348c2ecf20Sopenharmony_ci */ 1358c2ecf20Sopenharmony_ci/* PGDIR_SHIFT determines what a top-level page table entry can map */ 1368c2ecf20Sopenharmony_ci#define PGDIR_SHIFT (PAGE_SHIFT + PTE_INDEX_SIZE) 1378c2ecf20Sopenharmony_ci#define PGDIR_SIZE (1UL << PGDIR_SHIFT) 1388c2ecf20Sopenharmony_ci#define PGDIR_MASK (~(PGDIR_SIZE-1)) 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci#define USER_PTRS_PER_PGD (TASK_SIZE / PGDIR_SIZE) 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__ 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ciint map_kernel_page(unsigned long va, phys_addr_t pa, pgprot_t prot); 1458c2ecf20Sopenharmony_civoid unmap_kernel_page(unsigned long va); 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci#endif /* !__ASSEMBLY__ */ 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci/* 1508c2ecf20Sopenharmony_ci * This is the bottom of the PKMAP area with HIGHMEM or an arbitrary 1518c2ecf20Sopenharmony_ci * value (for now) on others, from where we can start layout kernel 1528c2ecf20Sopenharmony_ci * virtual space that goes below PKMAP and FIXMAP 1538c2ecf20Sopenharmony_ci */ 1548c2ecf20Sopenharmony_ci#include <asm/fixmap.h> 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci/* 1578c2ecf20Sopenharmony_ci * ioremap_bot starts at that address. Early ioremaps move down from there, 1588c2ecf20Sopenharmony_ci * until mem_init() at which point this becomes the top of the vmalloc 1598c2ecf20Sopenharmony_ci * and ioremap space 1608c2ecf20Sopenharmony_ci */ 1618c2ecf20Sopenharmony_ci#ifdef CONFIG_HIGHMEM 1628c2ecf20Sopenharmony_ci#define IOREMAP_TOP PKMAP_BASE 1638c2ecf20Sopenharmony_ci#else 1648c2ecf20Sopenharmony_ci#define IOREMAP_TOP FIXADDR_START 1658c2ecf20Sopenharmony_ci#endif 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci/* PPC32 shares vmalloc area with ioremap */ 1688c2ecf20Sopenharmony_ci#define IOREMAP_START VMALLOC_START 1698c2ecf20Sopenharmony_ci#define IOREMAP_END VMALLOC_END 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci/* 1728c2ecf20Sopenharmony_ci * Just any arbitrary offset to the start of the vmalloc VM area: the 1738c2ecf20Sopenharmony_ci * current 16MB value just means that there will be a 64MB "hole" after the 1748c2ecf20Sopenharmony_ci * physical memory until the kernel virtual memory starts. That means that 1758c2ecf20Sopenharmony_ci * any out-of-bounds memory accesses will hopefully be caught. 1768c2ecf20Sopenharmony_ci * The vmalloc() routines leaves a hole of 4kB between each vmalloced 1778c2ecf20Sopenharmony_ci * area for the same reason. ;) 1788c2ecf20Sopenharmony_ci * 1798c2ecf20Sopenharmony_ci * We no longer map larger than phys RAM with the BATs so we don't have 1808c2ecf20Sopenharmony_ci * to worry about the VMALLOC_OFFSET causing problems. We do have to worry 1818c2ecf20Sopenharmony_ci * about clashes between our early calls to ioremap() that start growing down 1828c2ecf20Sopenharmony_ci * from ioremap_base being run into the VM area allocations (growing upwards 1838c2ecf20Sopenharmony_ci * from VMALLOC_START). For this reason we have ioremap_bot to check when 1848c2ecf20Sopenharmony_ci * we actually run into our mappings setup in the early boot with the VM 1858c2ecf20Sopenharmony_ci * system. This really does become a problem for machines with good amounts 1868c2ecf20Sopenharmony_ci * of RAM. -- Cort 1878c2ecf20Sopenharmony_ci */ 1888c2ecf20Sopenharmony_ci#define VMALLOC_OFFSET (0x1000000) /* 16M */ 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci#define VMALLOC_START ((((long)high_memory + VMALLOC_OFFSET) & ~(VMALLOC_OFFSET-1))) 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci#ifdef CONFIG_KASAN_VMALLOC 1938c2ecf20Sopenharmony_ci#define VMALLOC_END ALIGN_DOWN(ioremap_bot, PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT) 1948c2ecf20Sopenharmony_ci#else 1958c2ecf20Sopenharmony_ci#define VMALLOC_END ioremap_bot 1968c2ecf20Sopenharmony_ci#endif 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci#ifdef CONFIG_STRICT_KERNEL_RWX 1998c2ecf20Sopenharmony_ci#define MODULES_END ALIGN_DOWN(PAGE_OFFSET, SZ_256M) 2008c2ecf20Sopenharmony_ci#define MODULES_VADDR (MODULES_END - SZ_256M) 2018c2ecf20Sopenharmony_ci#endif 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__ 2048c2ecf20Sopenharmony_ci#include <linux/sched.h> 2058c2ecf20Sopenharmony_ci#include <linux/threads.h> 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci/* Bits to mask out from a PGD to get to the PUD page */ 2088c2ecf20Sopenharmony_ci#define PGD_MASKED_BITS 0 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci#define pte_ERROR(e) \ 2118c2ecf20Sopenharmony_ci pr_err("%s:%d: bad pte %llx.\n", __FILE__, __LINE__, \ 2128c2ecf20Sopenharmony_ci (unsigned long long)pte_val(e)) 2138c2ecf20Sopenharmony_ci#define pgd_ERROR(e) \ 2148c2ecf20Sopenharmony_ci pr_err("%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__, pgd_val(e)) 2158c2ecf20Sopenharmony_ci/* 2168c2ecf20Sopenharmony_ci * Bits in a linux-style PTE. These match the bits in the 2178c2ecf20Sopenharmony_ci * (hardware-defined) PowerPC PTE as closely as possible. 2188c2ecf20Sopenharmony_ci */ 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci#define pte_clear(mm, addr, ptep) \ 2218c2ecf20Sopenharmony_ci do { pte_update(mm, addr, ptep, ~_PAGE_HASHPTE, 0, 0); } while (0) 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci#define pmd_none(pmd) (!pmd_val(pmd)) 2248c2ecf20Sopenharmony_ci#define pmd_bad(pmd) (pmd_val(pmd) & _PMD_BAD) 2258c2ecf20Sopenharmony_ci#define pmd_present(pmd) (pmd_val(pmd) & _PMD_PRESENT_MASK) 2268c2ecf20Sopenharmony_cistatic inline void pmd_clear(pmd_t *pmdp) 2278c2ecf20Sopenharmony_ci{ 2288c2ecf20Sopenharmony_ci *pmdp = __pmd(0); 2298c2ecf20Sopenharmony_ci} 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci/* 2338c2ecf20Sopenharmony_ci * When flushing the tlb entry for a page, we also need to flush the hash 2348c2ecf20Sopenharmony_ci * table entry. flush_hash_pages is assembler (for speed) in hashtable.S. 2358c2ecf20Sopenharmony_ci */ 2368c2ecf20Sopenharmony_ciextern int flush_hash_pages(unsigned context, unsigned long va, 2378c2ecf20Sopenharmony_ci unsigned long pmdval, int count); 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci/* Add an HPTE to the hash table */ 2408c2ecf20Sopenharmony_ciextern void add_hash_page(unsigned context, unsigned long va, 2418c2ecf20Sopenharmony_ci unsigned long pmdval); 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci/* Flush an entry from the TLB/hash table */ 2448c2ecf20Sopenharmony_ciextern void flush_hash_entry(struct mm_struct *mm, pte_t *ptep, 2458c2ecf20Sopenharmony_ci unsigned long address); 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci/* 2488c2ecf20Sopenharmony_ci * PTE updates. This function is called whenever an existing 2498c2ecf20Sopenharmony_ci * valid PTE is updated. This does -not- include set_pte_at() 2508c2ecf20Sopenharmony_ci * which nowadays only sets a new PTE. 2518c2ecf20Sopenharmony_ci * 2528c2ecf20Sopenharmony_ci * Depending on the type of MMU, we may need to use atomic updates 2538c2ecf20Sopenharmony_ci * and the PTE may be either 32 or 64 bit wide. In the later case, 2548c2ecf20Sopenharmony_ci * when using atomic updates, only the low part of the PTE is 2558c2ecf20Sopenharmony_ci * accessed atomically. 2568c2ecf20Sopenharmony_ci */ 2578c2ecf20Sopenharmony_cistatic inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, pte_t *p, 2588c2ecf20Sopenharmony_ci unsigned long clr, unsigned long set, int huge) 2598c2ecf20Sopenharmony_ci{ 2608c2ecf20Sopenharmony_ci pte_basic_t old; 2618c2ecf20Sopenharmony_ci unsigned long tmp; 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci __asm__ __volatile__( 2648c2ecf20Sopenharmony_ci#ifndef CONFIG_PTE_64BIT 2658c2ecf20Sopenharmony_ci"1: lwarx %0, 0, %3\n" 2668c2ecf20Sopenharmony_ci" andc %1, %0, %4\n" 2678c2ecf20Sopenharmony_ci#else 2688c2ecf20Sopenharmony_ci"1: lwarx %L0, 0, %3\n" 2698c2ecf20Sopenharmony_ci" lwz %0, -4(%3)\n" 2708c2ecf20Sopenharmony_ci" andc %1, %L0, %4\n" 2718c2ecf20Sopenharmony_ci#endif 2728c2ecf20Sopenharmony_ci" or %1, %1, %5\n" 2738c2ecf20Sopenharmony_ci" stwcx. %1, 0, %3\n" 2748c2ecf20Sopenharmony_ci" bne- 1b" 2758c2ecf20Sopenharmony_ci : "=&r" (old), "=&r" (tmp), "=m" (*p) 2768c2ecf20Sopenharmony_ci#ifndef CONFIG_PTE_64BIT 2778c2ecf20Sopenharmony_ci : "r" (p), 2788c2ecf20Sopenharmony_ci#else 2798c2ecf20Sopenharmony_ci : "b" ((unsigned long)(p) + 4), 2808c2ecf20Sopenharmony_ci#endif 2818c2ecf20Sopenharmony_ci "r" (clr), "r" (set), "m" (*p) 2828c2ecf20Sopenharmony_ci : "cc" ); 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci return old; 2858c2ecf20Sopenharmony_ci} 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ci/* 2888c2ecf20Sopenharmony_ci * 2.6 calls this without flushing the TLB entry; this is wrong 2898c2ecf20Sopenharmony_ci * for our hash-based implementation, we fix that up here. 2908c2ecf20Sopenharmony_ci */ 2918c2ecf20Sopenharmony_ci#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG 2928c2ecf20Sopenharmony_cistatic inline int __ptep_test_and_clear_young(struct mm_struct *mm, 2938c2ecf20Sopenharmony_ci unsigned long addr, pte_t *ptep) 2948c2ecf20Sopenharmony_ci{ 2958c2ecf20Sopenharmony_ci unsigned long old; 2968c2ecf20Sopenharmony_ci old = pte_update(mm, addr, ptep, _PAGE_ACCESSED, 0, 0); 2978c2ecf20Sopenharmony_ci if (old & _PAGE_HASHPTE) { 2988c2ecf20Sopenharmony_ci unsigned long ptephys = __pa(ptep) & PAGE_MASK; 2998c2ecf20Sopenharmony_ci flush_hash_pages(mm->context.id, addr, ptephys, 1); 3008c2ecf20Sopenharmony_ci } 3018c2ecf20Sopenharmony_ci return (old & _PAGE_ACCESSED) != 0; 3028c2ecf20Sopenharmony_ci} 3038c2ecf20Sopenharmony_ci#define ptep_test_and_clear_young(__vma, __addr, __ptep) \ 3048c2ecf20Sopenharmony_ci __ptep_test_and_clear_young((__vma)->vm_mm, __addr, __ptep) 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci#define __HAVE_ARCH_PTEP_GET_AND_CLEAR 3078c2ecf20Sopenharmony_cistatic inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, 3088c2ecf20Sopenharmony_ci pte_t *ptep) 3098c2ecf20Sopenharmony_ci{ 3108c2ecf20Sopenharmony_ci return __pte(pte_update(mm, addr, ptep, ~_PAGE_HASHPTE, 0, 0)); 3118c2ecf20Sopenharmony_ci} 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ci#define __HAVE_ARCH_PTEP_SET_WRPROTECT 3148c2ecf20Sopenharmony_cistatic inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, 3158c2ecf20Sopenharmony_ci pte_t *ptep) 3168c2ecf20Sopenharmony_ci{ 3178c2ecf20Sopenharmony_ci pte_update(mm, addr, ptep, _PAGE_RW, 0, 0); 3188c2ecf20Sopenharmony_ci} 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_cistatic inline void __ptep_set_access_flags(struct vm_area_struct *vma, 3218c2ecf20Sopenharmony_ci pte_t *ptep, pte_t entry, 3228c2ecf20Sopenharmony_ci unsigned long address, 3238c2ecf20Sopenharmony_ci int psize) 3248c2ecf20Sopenharmony_ci{ 3258c2ecf20Sopenharmony_ci unsigned long set = pte_val(entry) & 3268c2ecf20Sopenharmony_ci (_PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_RW | _PAGE_EXEC); 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci pte_update(vma->vm_mm, address, ptep, 0, set, 0); 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci flush_tlb_page(vma, address); 3318c2ecf20Sopenharmony_ci} 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_ci#define __HAVE_ARCH_PTE_SAME 3348c2ecf20Sopenharmony_ci#define pte_same(A,B) (((pte_val(A) ^ pte_val(B)) & ~_PAGE_HASHPTE) == 0) 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_ci#define pmd_page(pmd) \ 3378c2ecf20Sopenharmony_ci pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT) 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ci/* 3408c2ecf20Sopenharmony_ci * Encode and decode a swap entry. 3418c2ecf20Sopenharmony_ci * Note that the bits we use in a PTE for representing a swap entry 3428c2ecf20Sopenharmony_ci * must not include the _PAGE_PRESENT bit or the _PAGE_HASHPTE bit (if used). 3438c2ecf20Sopenharmony_ci * -- paulus 3448c2ecf20Sopenharmony_ci */ 3458c2ecf20Sopenharmony_ci#define __swp_type(entry) ((entry).val & 0x1f) 3468c2ecf20Sopenharmony_ci#define __swp_offset(entry) ((entry).val >> 5) 3478c2ecf20Sopenharmony_ci#define __swp_entry(type, offset) ((swp_entry_t) { (type) | ((offset) << 5) }) 3488c2ecf20Sopenharmony_ci#define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) >> 3 }) 3498c2ecf20Sopenharmony_ci#define __swp_entry_to_pte(x) ((pte_t) { (x).val << 3 }) 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci/* Generic accessors to PTE bits */ 3528c2ecf20Sopenharmony_cistatic inline int pte_write(pte_t pte) { return !!(pte_val(pte) & _PAGE_RW);} 3538c2ecf20Sopenharmony_cistatic inline int pte_read(pte_t pte) { return 1; } 3548c2ecf20Sopenharmony_cistatic inline int pte_dirty(pte_t pte) { return !!(pte_val(pte) & _PAGE_DIRTY); } 3558c2ecf20Sopenharmony_cistatic inline int pte_young(pte_t pte) { return !!(pte_val(pte) & _PAGE_ACCESSED); } 3568c2ecf20Sopenharmony_cistatic inline int pte_special(pte_t pte) { return !!(pte_val(pte) & _PAGE_SPECIAL); } 3578c2ecf20Sopenharmony_cistatic inline int pte_none(pte_t pte) { return (pte_val(pte) & ~_PTE_NONE_MASK) == 0; } 3588c2ecf20Sopenharmony_cistatic inline bool pte_exec(pte_t pte) { return pte_val(pte) & _PAGE_EXEC; } 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_cistatic inline int pte_present(pte_t pte) 3618c2ecf20Sopenharmony_ci{ 3628c2ecf20Sopenharmony_ci return pte_val(pte) & _PAGE_PRESENT; 3638c2ecf20Sopenharmony_ci} 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_cistatic inline bool pte_hw_valid(pte_t pte) 3668c2ecf20Sopenharmony_ci{ 3678c2ecf20Sopenharmony_ci return pte_val(pte) & _PAGE_PRESENT; 3688c2ecf20Sopenharmony_ci} 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_cistatic inline bool pte_hashpte(pte_t pte) 3718c2ecf20Sopenharmony_ci{ 3728c2ecf20Sopenharmony_ci return !!(pte_val(pte) & _PAGE_HASHPTE); 3738c2ecf20Sopenharmony_ci} 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_cistatic inline bool pte_ci(pte_t pte) 3768c2ecf20Sopenharmony_ci{ 3778c2ecf20Sopenharmony_ci return !!(pte_val(pte) & _PAGE_NO_CACHE); 3788c2ecf20Sopenharmony_ci} 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_ci/* 3818c2ecf20Sopenharmony_ci * We only find page table entry in the last level 3828c2ecf20Sopenharmony_ci * Hence no need for other accessors 3838c2ecf20Sopenharmony_ci */ 3848c2ecf20Sopenharmony_ci#define pte_access_permitted pte_access_permitted 3858c2ecf20Sopenharmony_cistatic inline bool pte_access_permitted(pte_t pte, bool write) 3868c2ecf20Sopenharmony_ci{ 3878c2ecf20Sopenharmony_ci /* 3888c2ecf20Sopenharmony_ci * A read-only access is controlled by _PAGE_USER bit. 3898c2ecf20Sopenharmony_ci * We have _PAGE_READ set for WRITE and EXECUTE 3908c2ecf20Sopenharmony_ci */ 3918c2ecf20Sopenharmony_ci if (!pte_present(pte) || !pte_user(pte) || !pte_read(pte)) 3928c2ecf20Sopenharmony_ci return false; 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_ci if (write && !pte_write(pte)) 3958c2ecf20Sopenharmony_ci return false; 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci return true; 3988c2ecf20Sopenharmony_ci} 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_ci/* Conversion functions: convert a page and protection to a page entry, 4018c2ecf20Sopenharmony_ci * and a page entry and page directory to the page they refer to. 4028c2ecf20Sopenharmony_ci * 4038c2ecf20Sopenharmony_ci * Even if PTEs can be unsigned long long, a PFN is always an unsigned 4048c2ecf20Sopenharmony_ci * long for now. 4058c2ecf20Sopenharmony_ci */ 4068c2ecf20Sopenharmony_cistatic inline pte_t pfn_pte(unsigned long pfn, pgprot_t pgprot) 4078c2ecf20Sopenharmony_ci{ 4088c2ecf20Sopenharmony_ci return __pte(((pte_basic_t)(pfn) << PTE_RPN_SHIFT) | 4098c2ecf20Sopenharmony_ci pgprot_val(pgprot)); 4108c2ecf20Sopenharmony_ci} 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_cistatic inline unsigned long pte_pfn(pte_t pte) 4138c2ecf20Sopenharmony_ci{ 4148c2ecf20Sopenharmony_ci return pte_val(pte) >> PTE_RPN_SHIFT; 4158c2ecf20Sopenharmony_ci} 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci/* Generic modifiers for PTE bits */ 4188c2ecf20Sopenharmony_cistatic inline pte_t pte_wrprotect(pte_t pte) 4198c2ecf20Sopenharmony_ci{ 4208c2ecf20Sopenharmony_ci return __pte(pte_val(pte) & ~_PAGE_RW); 4218c2ecf20Sopenharmony_ci} 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_cistatic inline pte_t pte_exprotect(pte_t pte) 4248c2ecf20Sopenharmony_ci{ 4258c2ecf20Sopenharmony_ci return __pte(pte_val(pte) & ~_PAGE_EXEC); 4268c2ecf20Sopenharmony_ci} 4278c2ecf20Sopenharmony_ci 4288c2ecf20Sopenharmony_cistatic inline pte_t pte_mkclean(pte_t pte) 4298c2ecf20Sopenharmony_ci{ 4308c2ecf20Sopenharmony_ci return __pte(pte_val(pte) & ~_PAGE_DIRTY); 4318c2ecf20Sopenharmony_ci} 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_cistatic inline pte_t pte_mkold(pte_t pte) 4348c2ecf20Sopenharmony_ci{ 4358c2ecf20Sopenharmony_ci return __pte(pte_val(pte) & ~_PAGE_ACCESSED); 4368c2ecf20Sopenharmony_ci} 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_cistatic inline pte_t pte_mkexec(pte_t pte) 4398c2ecf20Sopenharmony_ci{ 4408c2ecf20Sopenharmony_ci return __pte(pte_val(pte) | _PAGE_EXEC); 4418c2ecf20Sopenharmony_ci} 4428c2ecf20Sopenharmony_ci 4438c2ecf20Sopenharmony_cistatic inline pte_t pte_mkpte(pte_t pte) 4448c2ecf20Sopenharmony_ci{ 4458c2ecf20Sopenharmony_ci return pte; 4468c2ecf20Sopenharmony_ci} 4478c2ecf20Sopenharmony_ci 4488c2ecf20Sopenharmony_cistatic inline pte_t pte_mkwrite(pte_t pte) 4498c2ecf20Sopenharmony_ci{ 4508c2ecf20Sopenharmony_ci return __pte(pte_val(pte) | _PAGE_RW); 4518c2ecf20Sopenharmony_ci} 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_cistatic inline pte_t pte_mkdirty(pte_t pte) 4548c2ecf20Sopenharmony_ci{ 4558c2ecf20Sopenharmony_ci return __pte(pte_val(pte) | _PAGE_DIRTY); 4568c2ecf20Sopenharmony_ci} 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_cistatic inline pte_t pte_mkyoung(pte_t pte) 4598c2ecf20Sopenharmony_ci{ 4608c2ecf20Sopenharmony_ci return __pte(pte_val(pte) | _PAGE_ACCESSED); 4618c2ecf20Sopenharmony_ci} 4628c2ecf20Sopenharmony_ci 4638c2ecf20Sopenharmony_cistatic inline pte_t pte_mkspecial(pte_t pte) 4648c2ecf20Sopenharmony_ci{ 4658c2ecf20Sopenharmony_ci return __pte(pte_val(pte) | _PAGE_SPECIAL); 4668c2ecf20Sopenharmony_ci} 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_cistatic inline pte_t pte_mkhuge(pte_t pte) 4698c2ecf20Sopenharmony_ci{ 4708c2ecf20Sopenharmony_ci return pte; 4718c2ecf20Sopenharmony_ci} 4728c2ecf20Sopenharmony_ci 4738c2ecf20Sopenharmony_cistatic inline pte_t pte_mkprivileged(pte_t pte) 4748c2ecf20Sopenharmony_ci{ 4758c2ecf20Sopenharmony_ci return __pte(pte_val(pte) & ~_PAGE_USER); 4768c2ecf20Sopenharmony_ci} 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_cistatic inline pte_t pte_mkuser(pte_t pte) 4798c2ecf20Sopenharmony_ci{ 4808c2ecf20Sopenharmony_ci return __pte(pte_val(pte) | _PAGE_USER); 4818c2ecf20Sopenharmony_ci} 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_cistatic inline pte_t pte_modify(pte_t pte, pgprot_t newprot) 4848c2ecf20Sopenharmony_ci{ 4858c2ecf20Sopenharmony_ci return __pte((pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot)); 4868c2ecf20Sopenharmony_ci} 4878c2ecf20Sopenharmony_ci 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ci/* This low level function performs the actual PTE insertion 4918c2ecf20Sopenharmony_ci * Setting the PTE depends on the MMU type and other factors. It's 4928c2ecf20Sopenharmony_ci * an horrible mess that I'm not going to try to clean up now but 4938c2ecf20Sopenharmony_ci * I'm keeping it in one place rather than spread around 4948c2ecf20Sopenharmony_ci */ 4958c2ecf20Sopenharmony_cistatic inline void __set_pte_at(struct mm_struct *mm, unsigned long addr, 4968c2ecf20Sopenharmony_ci pte_t *ptep, pte_t pte, int percpu) 4978c2ecf20Sopenharmony_ci{ 4988c2ecf20Sopenharmony_ci#if defined(CONFIG_SMP) && !defined(CONFIG_PTE_64BIT) 4998c2ecf20Sopenharmony_ci /* First case is 32-bit Hash MMU in SMP mode with 32-bit PTEs. We use the 5008c2ecf20Sopenharmony_ci * helper pte_update() which does an atomic update. We need to do that 5018c2ecf20Sopenharmony_ci * because a concurrent invalidation can clear _PAGE_HASHPTE. If it's a 5028c2ecf20Sopenharmony_ci * per-CPU PTE such as a kmap_atomic, we do a simple update preserving 5038c2ecf20Sopenharmony_ci * the hash bits instead (ie, same as the non-SMP case) 5048c2ecf20Sopenharmony_ci */ 5058c2ecf20Sopenharmony_ci if (percpu) 5068c2ecf20Sopenharmony_ci *ptep = __pte((pte_val(*ptep) & _PAGE_HASHPTE) 5078c2ecf20Sopenharmony_ci | (pte_val(pte) & ~_PAGE_HASHPTE)); 5088c2ecf20Sopenharmony_ci else 5098c2ecf20Sopenharmony_ci pte_update(mm, addr, ptep, ~_PAGE_HASHPTE, pte_val(pte), 0); 5108c2ecf20Sopenharmony_ci 5118c2ecf20Sopenharmony_ci#elif defined(CONFIG_PTE_64BIT) 5128c2ecf20Sopenharmony_ci /* Second case is 32-bit with 64-bit PTE. In this case, we 5138c2ecf20Sopenharmony_ci * can just store as long as we do the two halves in the right order 5148c2ecf20Sopenharmony_ci * with a barrier in between. This is possible because we take care, 5158c2ecf20Sopenharmony_ci * in the hash code, to pre-invalidate if the PTE was already hashed, 5168c2ecf20Sopenharmony_ci * which synchronizes us with any concurrent invalidation. 5178c2ecf20Sopenharmony_ci * In the percpu case, we also fallback to the simple update preserving 5188c2ecf20Sopenharmony_ci * the hash bits 5198c2ecf20Sopenharmony_ci */ 5208c2ecf20Sopenharmony_ci if (percpu) { 5218c2ecf20Sopenharmony_ci *ptep = __pte((pte_val(*ptep) & _PAGE_HASHPTE) 5228c2ecf20Sopenharmony_ci | (pte_val(pte) & ~_PAGE_HASHPTE)); 5238c2ecf20Sopenharmony_ci return; 5248c2ecf20Sopenharmony_ci } 5258c2ecf20Sopenharmony_ci if (pte_val(*ptep) & _PAGE_HASHPTE) 5268c2ecf20Sopenharmony_ci flush_hash_entry(mm, ptep, addr); 5278c2ecf20Sopenharmony_ci __asm__ __volatile__("\ 5288c2ecf20Sopenharmony_ci stw%X0 %2,%0\n\ 5298c2ecf20Sopenharmony_ci eieio\n\ 5308c2ecf20Sopenharmony_ci stw%X1 %L2,%1" 5318c2ecf20Sopenharmony_ci : "=m" (*ptep), "=m" (*((unsigned char *)ptep+4)) 5328c2ecf20Sopenharmony_ci : "r" (pte) : "memory"); 5338c2ecf20Sopenharmony_ci 5348c2ecf20Sopenharmony_ci#else 5358c2ecf20Sopenharmony_ci /* Third case is 32-bit hash table in UP mode, we need to preserve 5368c2ecf20Sopenharmony_ci * the _PAGE_HASHPTE bit since we may not have invalidated the previous 5378c2ecf20Sopenharmony_ci * translation in the hash yet (done in a subsequent flush_tlb_xxx()) 5388c2ecf20Sopenharmony_ci * and see we need to keep track that this PTE needs invalidating 5398c2ecf20Sopenharmony_ci */ 5408c2ecf20Sopenharmony_ci *ptep = __pte((pte_val(*ptep) & _PAGE_HASHPTE) 5418c2ecf20Sopenharmony_ci | (pte_val(pte) & ~_PAGE_HASHPTE)); 5428c2ecf20Sopenharmony_ci#endif 5438c2ecf20Sopenharmony_ci} 5448c2ecf20Sopenharmony_ci 5458c2ecf20Sopenharmony_ci/* 5468c2ecf20Sopenharmony_ci * Macro to mark a page protection value as "uncacheable". 5478c2ecf20Sopenharmony_ci */ 5488c2ecf20Sopenharmony_ci 5498c2ecf20Sopenharmony_ci#define _PAGE_CACHE_CTL (_PAGE_COHERENT | _PAGE_GUARDED | _PAGE_NO_CACHE | \ 5508c2ecf20Sopenharmony_ci _PAGE_WRITETHRU) 5518c2ecf20Sopenharmony_ci 5528c2ecf20Sopenharmony_ci#define pgprot_noncached pgprot_noncached 5538c2ecf20Sopenharmony_cistatic inline pgprot_t pgprot_noncached(pgprot_t prot) 5548c2ecf20Sopenharmony_ci{ 5558c2ecf20Sopenharmony_ci return __pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | 5568c2ecf20Sopenharmony_ci _PAGE_NO_CACHE | _PAGE_GUARDED); 5578c2ecf20Sopenharmony_ci} 5588c2ecf20Sopenharmony_ci 5598c2ecf20Sopenharmony_ci#define pgprot_noncached_wc pgprot_noncached_wc 5608c2ecf20Sopenharmony_cistatic inline pgprot_t pgprot_noncached_wc(pgprot_t prot) 5618c2ecf20Sopenharmony_ci{ 5628c2ecf20Sopenharmony_ci return __pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | 5638c2ecf20Sopenharmony_ci _PAGE_NO_CACHE); 5648c2ecf20Sopenharmony_ci} 5658c2ecf20Sopenharmony_ci 5668c2ecf20Sopenharmony_ci#define pgprot_cached pgprot_cached 5678c2ecf20Sopenharmony_cistatic inline pgprot_t pgprot_cached(pgprot_t prot) 5688c2ecf20Sopenharmony_ci{ 5698c2ecf20Sopenharmony_ci return __pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | 5708c2ecf20Sopenharmony_ci _PAGE_COHERENT); 5718c2ecf20Sopenharmony_ci} 5728c2ecf20Sopenharmony_ci 5738c2ecf20Sopenharmony_ci#define pgprot_cached_wthru pgprot_cached_wthru 5748c2ecf20Sopenharmony_cistatic inline pgprot_t pgprot_cached_wthru(pgprot_t prot) 5758c2ecf20Sopenharmony_ci{ 5768c2ecf20Sopenharmony_ci return __pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | 5778c2ecf20Sopenharmony_ci _PAGE_COHERENT | _PAGE_WRITETHRU); 5788c2ecf20Sopenharmony_ci} 5798c2ecf20Sopenharmony_ci 5808c2ecf20Sopenharmony_ci#define pgprot_cached_noncoherent pgprot_cached_noncoherent 5818c2ecf20Sopenharmony_cistatic inline pgprot_t pgprot_cached_noncoherent(pgprot_t prot) 5828c2ecf20Sopenharmony_ci{ 5838c2ecf20Sopenharmony_ci return __pgprot(pgprot_val(prot) & ~_PAGE_CACHE_CTL); 5848c2ecf20Sopenharmony_ci} 5858c2ecf20Sopenharmony_ci 5868c2ecf20Sopenharmony_ci#define pgprot_writecombine pgprot_writecombine 5878c2ecf20Sopenharmony_cistatic inline pgprot_t pgprot_writecombine(pgprot_t prot) 5888c2ecf20Sopenharmony_ci{ 5898c2ecf20Sopenharmony_ci return pgprot_noncached_wc(prot); 5908c2ecf20Sopenharmony_ci} 5918c2ecf20Sopenharmony_ci 5928c2ecf20Sopenharmony_ci#endif /* !__ASSEMBLY__ */ 5938c2ecf20Sopenharmony_ci 5948c2ecf20Sopenharmony_ci#endif /* _ASM_POWERPC_BOOK3S_32_PGTABLE_H */ 595