162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci#ifndef _ASM_POWERPC_BOOK3S_32_PGTABLE_H 362306a36Sopenharmony_ci#define _ASM_POWERPC_BOOK3S_32_PGTABLE_H 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci#include <asm-generic/pgtable-nopmd.h> 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci/* 862306a36Sopenharmony_ci * The "classic" 32-bit implementation of the PowerPC MMU uses a hash 962306a36Sopenharmony_ci * table containing PTEs, together with a set of 16 segment registers, 1062306a36Sopenharmony_ci * to define the virtual to physical address mapping. 1162306a36Sopenharmony_ci * 1262306a36Sopenharmony_ci * We use the hash table as an extended TLB, i.e. a cache of currently 1362306a36Sopenharmony_ci * active mappings. We maintain a two-level page table tree, much 1462306a36Sopenharmony_ci * like that used by the i386, for the sake of the Linux memory 1562306a36Sopenharmony_ci * management code. Low-level assembler code in hash_low_32.S 1662306a36Sopenharmony_ci * (procedure hash_page) is responsible for extracting ptes from the 1762306a36Sopenharmony_ci * tree and putting them into the hash table when necessary, and 1862306a36Sopenharmony_ci * updating the accessed and modified bits in the page table tree. 1962306a36Sopenharmony_ci */ 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ci#define _PAGE_PRESENT 0x001 /* software: pte contains a translation */ 2262306a36Sopenharmony_ci#define _PAGE_HASHPTE 0x002 /* hash_page has made an HPTE for this pte */ 2362306a36Sopenharmony_ci#define _PAGE_USER 0x004 /* usermode access allowed */ 2462306a36Sopenharmony_ci#define _PAGE_GUARDED 0x008 /* G: prohibit speculative access */ 2562306a36Sopenharmony_ci#define _PAGE_COHERENT 0x010 /* M: enforce memory coherence (SMP systems) */ 2662306a36Sopenharmony_ci#define _PAGE_NO_CACHE 0x020 /* I: cache inhibit */ 2762306a36Sopenharmony_ci#define _PAGE_WRITETHRU 0x040 /* W: cache write-through */ 2862306a36Sopenharmony_ci#define _PAGE_DIRTY 0x080 /* C: page changed */ 2962306a36Sopenharmony_ci#define _PAGE_ACCESSED 0x100 /* R: page referenced */ 3062306a36Sopenharmony_ci#define _PAGE_EXEC 0x200 /* software: exec allowed */ 3162306a36Sopenharmony_ci#define _PAGE_RW 0x400 /* software: user write access allowed */ 3262306a36Sopenharmony_ci#define _PAGE_SPECIAL 0x800 /* software: Special page */ 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci#ifdef CONFIG_PTE_64BIT 3562306a36Sopenharmony_ci/* We never clear the high word of the pte */ 3662306a36Sopenharmony_ci#define _PTE_NONE_MASK (0xffffffff00000000ULL | _PAGE_HASHPTE) 3762306a36Sopenharmony_ci#else 3862306a36Sopenharmony_ci#define _PTE_NONE_MASK _PAGE_HASHPTE 3962306a36Sopenharmony_ci#endif 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci#define _PMD_PRESENT 0 4262306a36Sopenharmony_ci#define _PMD_PRESENT_MASK (PAGE_MASK) 4362306a36Sopenharmony_ci#define _PMD_BAD (~PAGE_MASK) 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci/* We borrow the _PAGE_USER bit to store the exclusive marker in swap PTEs. */ 4662306a36Sopenharmony_ci#define _PAGE_SWP_EXCLUSIVE _PAGE_USER 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci/* And here we include common definitions */ 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ci#define _PAGE_KERNEL_RO 0 5162306a36Sopenharmony_ci#define _PAGE_KERNEL_ROX (_PAGE_EXEC) 5262306a36Sopenharmony_ci#define _PAGE_KERNEL_RW (_PAGE_DIRTY | _PAGE_RW) 5362306a36Sopenharmony_ci#define _PAGE_KERNEL_RWX (_PAGE_DIRTY | _PAGE_RW | _PAGE_EXEC) 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci#define _PAGE_HPTEFLAGS _PAGE_HASHPTE 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci#ifndef __ASSEMBLY__ 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_cistatic inline bool pte_user(pte_t pte) 6062306a36Sopenharmony_ci{ 6162306a36Sopenharmony_ci return pte_val(pte) & _PAGE_USER; 6262306a36Sopenharmony_ci} 6362306a36Sopenharmony_ci#endif /* __ASSEMBLY__ */ 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci/* 6662306a36Sopenharmony_ci * Location of the PFN in the PTE. Most 32-bit platforms use the same 6762306a36Sopenharmony_ci * as _PAGE_SHIFT here (ie, naturally aligned). 6862306a36Sopenharmony_ci * Platform who don't just pre-define the value so we don't override it here. 6962306a36Sopenharmony_ci */ 7062306a36Sopenharmony_ci#define PTE_RPN_SHIFT (PAGE_SHIFT) 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci/* 7362306a36Sopenharmony_ci * The mask covered by the RPN must be a ULL on 32-bit platforms with 7462306a36Sopenharmony_ci * 64-bit PTEs. 7562306a36Sopenharmony_ci */ 7662306a36Sopenharmony_ci#ifdef CONFIG_PTE_64BIT 7762306a36Sopenharmony_ci#define PTE_RPN_MASK (~((1ULL << PTE_RPN_SHIFT) - 1)) 7862306a36Sopenharmony_ci#define MAX_POSSIBLE_PHYSMEM_BITS 36 7962306a36Sopenharmony_ci#else 8062306a36Sopenharmony_ci#define PTE_RPN_MASK (~((1UL << PTE_RPN_SHIFT) - 1)) 8162306a36Sopenharmony_ci#define MAX_POSSIBLE_PHYSMEM_BITS 32 8262306a36Sopenharmony_ci#endif 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci/* 8562306a36Sopenharmony_ci * _PAGE_CHG_MASK masks of bits that are to be preserved across 8662306a36Sopenharmony_ci * pgprot changes. 8762306a36Sopenharmony_ci */ 8862306a36Sopenharmony_ci#define _PAGE_CHG_MASK (PTE_RPN_MASK | _PAGE_HASHPTE | _PAGE_DIRTY | \ 8962306a36Sopenharmony_ci _PAGE_ACCESSED | _PAGE_SPECIAL) 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci/* 9262306a36Sopenharmony_ci * We define 2 sets of base prot bits, one for basic pages (ie, 9362306a36Sopenharmony_ci * cacheable kernel and user pages) and one for non cacheable 9462306a36Sopenharmony_ci * pages. We always set _PAGE_COHERENT when SMP is enabled or 9562306a36Sopenharmony_ci * the processor might need it for DMA coherency. 9662306a36Sopenharmony_ci */ 9762306a36Sopenharmony_ci#define _PAGE_BASE_NC (_PAGE_PRESENT | _PAGE_ACCESSED) 9862306a36Sopenharmony_ci#define _PAGE_BASE (_PAGE_BASE_NC | _PAGE_COHERENT) 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci/* 10162306a36Sopenharmony_ci * Permission masks used to generate the __P and __S table. 10262306a36Sopenharmony_ci * 10362306a36Sopenharmony_ci * Note:__pgprot is defined in arch/powerpc/include/asm/page.h 10462306a36Sopenharmony_ci * 10562306a36Sopenharmony_ci * Write permissions imply read permissions for now. 10662306a36Sopenharmony_ci */ 10762306a36Sopenharmony_ci#define PAGE_NONE __pgprot(_PAGE_BASE) 10862306a36Sopenharmony_ci#define PAGE_SHARED __pgprot(_PAGE_BASE | _PAGE_USER | _PAGE_RW) 10962306a36Sopenharmony_ci#define PAGE_SHARED_X __pgprot(_PAGE_BASE | _PAGE_USER | _PAGE_RW | _PAGE_EXEC) 11062306a36Sopenharmony_ci#define PAGE_COPY __pgprot(_PAGE_BASE | _PAGE_USER) 11162306a36Sopenharmony_ci#define PAGE_COPY_X __pgprot(_PAGE_BASE | _PAGE_USER | _PAGE_EXEC) 11262306a36Sopenharmony_ci#define PAGE_READONLY __pgprot(_PAGE_BASE | _PAGE_USER) 11362306a36Sopenharmony_ci#define PAGE_READONLY_X __pgprot(_PAGE_BASE | _PAGE_USER | _PAGE_EXEC) 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci/* Permission masks used for kernel mappings */ 11662306a36Sopenharmony_ci#define PAGE_KERNEL __pgprot(_PAGE_BASE | _PAGE_KERNEL_RW) 11762306a36Sopenharmony_ci#define PAGE_KERNEL_NC __pgprot(_PAGE_BASE_NC | _PAGE_KERNEL_RW | _PAGE_NO_CACHE) 11862306a36Sopenharmony_ci#define PAGE_KERNEL_NCG __pgprot(_PAGE_BASE_NC | _PAGE_KERNEL_RW | _PAGE_NO_CACHE | _PAGE_GUARDED) 11962306a36Sopenharmony_ci#define PAGE_KERNEL_X __pgprot(_PAGE_BASE | _PAGE_KERNEL_RWX) 12062306a36Sopenharmony_ci#define PAGE_KERNEL_RO __pgprot(_PAGE_BASE | _PAGE_KERNEL_RO) 12162306a36Sopenharmony_ci#define PAGE_KERNEL_ROX __pgprot(_PAGE_BASE | _PAGE_KERNEL_ROX) 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci#define PTE_INDEX_SIZE PTE_SHIFT 12462306a36Sopenharmony_ci#define PMD_INDEX_SIZE 0 12562306a36Sopenharmony_ci#define PUD_INDEX_SIZE 0 12662306a36Sopenharmony_ci#define PGD_INDEX_SIZE (32 - PGDIR_SHIFT) 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci#define PMD_CACHE_INDEX PMD_INDEX_SIZE 12962306a36Sopenharmony_ci#define PUD_CACHE_INDEX PUD_INDEX_SIZE 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci#ifndef __ASSEMBLY__ 13262306a36Sopenharmony_ci#define PTE_TABLE_SIZE (sizeof(pte_t) << PTE_INDEX_SIZE) 13362306a36Sopenharmony_ci#define PMD_TABLE_SIZE 0 13462306a36Sopenharmony_ci#define PUD_TABLE_SIZE 0 13562306a36Sopenharmony_ci#define PGD_TABLE_SIZE (sizeof(pgd_t) << PGD_INDEX_SIZE) 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci/* Bits to mask out from a PMD to get to the PTE page */ 13862306a36Sopenharmony_ci#define PMD_MASKED_BITS (PTE_TABLE_SIZE - 1) 13962306a36Sopenharmony_ci#endif /* __ASSEMBLY__ */ 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ci#define PTRS_PER_PTE (1 << PTE_INDEX_SIZE) 14262306a36Sopenharmony_ci#define PTRS_PER_PGD (1 << PGD_INDEX_SIZE) 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci/* 14562306a36Sopenharmony_ci * The normal case is that PTEs are 32-bits and we have a 1-page 14662306a36Sopenharmony_ci * 1024-entry pgdir pointing to 1-page 1024-entry PTE pages. -- paulus 14762306a36Sopenharmony_ci * 14862306a36Sopenharmony_ci * For any >32-bit physical address platform, we can use the following 14962306a36Sopenharmony_ci * two level page table layout where the pgdir is 8KB and the MS 13 bits 15062306a36Sopenharmony_ci * are an index to the second level table. The combined pgdir/pmd first 15162306a36Sopenharmony_ci * level has 2048 entries and the second level has 512 64-bit PTE entries. 15262306a36Sopenharmony_ci * -Matt 15362306a36Sopenharmony_ci */ 15462306a36Sopenharmony_ci/* PGDIR_SHIFT determines what a top-level page table entry can map */ 15562306a36Sopenharmony_ci#define PGDIR_SHIFT (PAGE_SHIFT + PTE_INDEX_SIZE) 15662306a36Sopenharmony_ci#define PGDIR_SIZE (1UL << PGDIR_SHIFT) 15762306a36Sopenharmony_ci#define PGDIR_MASK (~(PGDIR_SIZE-1)) 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci#define USER_PTRS_PER_PGD (TASK_SIZE / PGDIR_SIZE) 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci#ifndef __ASSEMBLY__ 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ciint map_kernel_page(unsigned long va, phys_addr_t pa, pgprot_t prot); 16462306a36Sopenharmony_civoid unmap_kernel_page(unsigned long va); 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ci#endif /* !__ASSEMBLY__ */ 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci/* 16962306a36Sopenharmony_ci * This is the bottom of the PKMAP area with HIGHMEM or an arbitrary 17062306a36Sopenharmony_ci * value (for now) on others, from where we can start layout kernel 17162306a36Sopenharmony_ci * virtual space that goes below PKMAP and FIXMAP 17262306a36Sopenharmony_ci */ 17362306a36Sopenharmony_ci#include <asm/fixmap.h> 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ci/* 17662306a36Sopenharmony_ci * ioremap_bot starts at that address. Early ioremaps move down from there, 17762306a36Sopenharmony_ci * until mem_init() at which point this becomes the top of the vmalloc 17862306a36Sopenharmony_ci * and ioremap space 17962306a36Sopenharmony_ci */ 18062306a36Sopenharmony_ci#ifdef CONFIG_HIGHMEM 18162306a36Sopenharmony_ci#define IOREMAP_TOP PKMAP_BASE 18262306a36Sopenharmony_ci#else 18362306a36Sopenharmony_ci#define IOREMAP_TOP FIXADDR_START 18462306a36Sopenharmony_ci#endif 18562306a36Sopenharmony_ci 18662306a36Sopenharmony_ci/* PPC32 shares vmalloc area with ioremap */ 18762306a36Sopenharmony_ci#define IOREMAP_START VMALLOC_START 18862306a36Sopenharmony_ci#define IOREMAP_END VMALLOC_END 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci/* 19162306a36Sopenharmony_ci * Just any arbitrary offset to the start of the vmalloc VM area: the 19262306a36Sopenharmony_ci * current 16MB value just means that there will be a 64MB "hole" after the 19362306a36Sopenharmony_ci * physical memory until the kernel virtual memory starts. That means that 19462306a36Sopenharmony_ci * any out-of-bounds memory accesses will hopefully be caught. 19562306a36Sopenharmony_ci * The vmalloc() routines leaves a hole of 4kB between each vmalloced 19662306a36Sopenharmony_ci * area for the same reason. ;) 19762306a36Sopenharmony_ci * 19862306a36Sopenharmony_ci * We no longer map larger than phys RAM with the BATs so we don't have 19962306a36Sopenharmony_ci * to worry about the VMALLOC_OFFSET causing problems. We do have to worry 20062306a36Sopenharmony_ci * about clashes between our early calls to ioremap() that start growing down 20162306a36Sopenharmony_ci * from ioremap_base being run into the VM area allocations (growing upwards 20262306a36Sopenharmony_ci * from VMALLOC_START). For this reason we have ioremap_bot to check when 20362306a36Sopenharmony_ci * we actually run into our mappings setup in the early boot with the VM 20462306a36Sopenharmony_ci * system. This really does become a problem for machines with good amounts 20562306a36Sopenharmony_ci * of RAM. -- Cort 20662306a36Sopenharmony_ci */ 20762306a36Sopenharmony_ci#define VMALLOC_OFFSET (0x1000000) /* 16M */ 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_ci#define VMALLOC_START ((((long)high_memory + VMALLOC_OFFSET) & ~(VMALLOC_OFFSET-1))) 21062306a36Sopenharmony_ci 21162306a36Sopenharmony_ci#ifdef CONFIG_KASAN_VMALLOC 21262306a36Sopenharmony_ci#define VMALLOC_END ALIGN_DOWN(ioremap_bot, PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT) 21362306a36Sopenharmony_ci#else 21462306a36Sopenharmony_ci#define VMALLOC_END ioremap_bot 21562306a36Sopenharmony_ci#endif 21662306a36Sopenharmony_ci 21762306a36Sopenharmony_ci#define MODULES_END ALIGN_DOWN(PAGE_OFFSET, SZ_256M) 21862306a36Sopenharmony_ci#define MODULES_VADDR (MODULES_END - SZ_256M) 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_ci#ifndef __ASSEMBLY__ 22162306a36Sopenharmony_ci#include <linux/sched.h> 22262306a36Sopenharmony_ci#include <linux/threads.h> 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ci/* Bits to mask out from a PGD to get to the PUD page */ 22562306a36Sopenharmony_ci#define PGD_MASKED_BITS 0 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_ci#define pte_ERROR(e) \ 22862306a36Sopenharmony_ci pr_err("%s:%d: bad pte %llx.\n", __FILE__, __LINE__, \ 22962306a36Sopenharmony_ci (unsigned long long)pte_val(e)) 23062306a36Sopenharmony_ci#define pgd_ERROR(e) \ 23162306a36Sopenharmony_ci pr_err("%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__, pgd_val(e)) 23262306a36Sopenharmony_ci/* 23362306a36Sopenharmony_ci * Bits in a linux-style PTE. These match the bits in the 23462306a36Sopenharmony_ci * (hardware-defined) PowerPC PTE as closely as possible. 23562306a36Sopenharmony_ci */ 23662306a36Sopenharmony_ci 23762306a36Sopenharmony_ci#define pte_clear(mm, addr, ptep) \ 23862306a36Sopenharmony_ci do { pte_update(mm, addr, ptep, ~_PAGE_HASHPTE, 0, 0); } while (0) 23962306a36Sopenharmony_ci 24062306a36Sopenharmony_ci#define pmd_none(pmd) (!pmd_val(pmd)) 24162306a36Sopenharmony_ci#define pmd_bad(pmd) (pmd_val(pmd) & _PMD_BAD) 24262306a36Sopenharmony_ci#define pmd_present(pmd) (pmd_val(pmd) & _PMD_PRESENT_MASK) 24362306a36Sopenharmony_cistatic inline void pmd_clear(pmd_t *pmdp) 24462306a36Sopenharmony_ci{ 24562306a36Sopenharmony_ci *pmdp = __pmd(0); 24662306a36Sopenharmony_ci} 24762306a36Sopenharmony_ci 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ci/* 25062306a36Sopenharmony_ci * When flushing the tlb entry for a page, we also need to flush the hash 25162306a36Sopenharmony_ci * table entry. flush_hash_pages is assembler (for speed) in hashtable.S. 25262306a36Sopenharmony_ci */ 25362306a36Sopenharmony_ciextern int flush_hash_pages(unsigned context, unsigned long va, 25462306a36Sopenharmony_ci unsigned long pmdval, int count); 25562306a36Sopenharmony_ci 25662306a36Sopenharmony_ci/* Add an HPTE to the hash table */ 25762306a36Sopenharmony_ciextern void add_hash_page(unsigned context, unsigned long va, 25862306a36Sopenharmony_ci unsigned long pmdval); 25962306a36Sopenharmony_ci 26062306a36Sopenharmony_ci/* Flush an entry from the TLB/hash table */ 26162306a36Sopenharmony_cistatic inline void flush_hash_entry(struct mm_struct *mm, pte_t *ptep, unsigned long addr) 26262306a36Sopenharmony_ci{ 26362306a36Sopenharmony_ci if (mmu_has_feature(MMU_FTR_HPTE_TABLE)) { 26462306a36Sopenharmony_ci unsigned long ptephys = __pa(ptep) & PAGE_MASK; 26562306a36Sopenharmony_ci 26662306a36Sopenharmony_ci flush_hash_pages(mm->context.id, addr, ptephys, 1); 26762306a36Sopenharmony_ci } 26862306a36Sopenharmony_ci} 26962306a36Sopenharmony_ci 27062306a36Sopenharmony_ci/* 27162306a36Sopenharmony_ci * PTE updates. This function is called whenever an existing 27262306a36Sopenharmony_ci * valid PTE is updated. This does -not- include set_pte_at() 27362306a36Sopenharmony_ci * which nowadays only sets a new PTE. 27462306a36Sopenharmony_ci * 27562306a36Sopenharmony_ci * Depending on the type of MMU, we may need to use atomic updates 27662306a36Sopenharmony_ci * and the PTE may be either 32 or 64 bit wide. In the later case, 27762306a36Sopenharmony_ci * when using atomic updates, only the low part of the PTE is 27862306a36Sopenharmony_ci * accessed atomically. 27962306a36Sopenharmony_ci */ 28062306a36Sopenharmony_cistatic inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, pte_t *p, 28162306a36Sopenharmony_ci unsigned long clr, unsigned long set, int huge) 28262306a36Sopenharmony_ci{ 28362306a36Sopenharmony_ci pte_basic_t old; 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_ci if (mmu_has_feature(MMU_FTR_HPTE_TABLE)) { 28662306a36Sopenharmony_ci unsigned long tmp; 28762306a36Sopenharmony_ci 28862306a36Sopenharmony_ci asm volatile( 28962306a36Sopenharmony_ci#ifndef CONFIG_PTE_64BIT 29062306a36Sopenharmony_ci "1: lwarx %0, 0, %3\n" 29162306a36Sopenharmony_ci " andc %1, %0, %4\n" 29262306a36Sopenharmony_ci#else 29362306a36Sopenharmony_ci "1: lwarx %L0, 0, %3\n" 29462306a36Sopenharmony_ci " lwz %0, -4(%3)\n" 29562306a36Sopenharmony_ci " andc %1, %L0, %4\n" 29662306a36Sopenharmony_ci#endif 29762306a36Sopenharmony_ci " or %1, %1, %5\n" 29862306a36Sopenharmony_ci " stwcx. %1, 0, %3\n" 29962306a36Sopenharmony_ci " bne- 1b" 30062306a36Sopenharmony_ci : "=&r" (old), "=&r" (tmp), "=m" (*p) 30162306a36Sopenharmony_ci#ifndef CONFIG_PTE_64BIT 30262306a36Sopenharmony_ci : "r" (p), 30362306a36Sopenharmony_ci#else 30462306a36Sopenharmony_ci : "b" ((unsigned long)(p) + 4), 30562306a36Sopenharmony_ci#endif 30662306a36Sopenharmony_ci "r" (clr), "r" (set), "m" (*p) 30762306a36Sopenharmony_ci : "cc" ); 30862306a36Sopenharmony_ci } else { 30962306a36Sopenharmony_ci old = pte_val(*p); 31062306a36Sopenharmony_ci 31162306a36Sopenharmony_ci *p = __pte((old & ~(pte_basic_t)clr) | set); 31262306a36Sopenharmony_ci } 31362306a36Sopenharmony_ci 31462306a36Sopenharmony_ci return old; 31562306a36Sopenharmony_ci} 31662306a36Sopenharmony_ci 31762306a36Sopenharmony_ci/* 31862306a36Sopenharmony_ci * 2.6 calls this without flushing the TLB entry; this is wrong 31962306a36Sopenharmony_ci * for our hash-based implementation, we fix that up here. 32062306a36Sopenharmony_ci */ 32162306a36Sopenharmony_ci#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG 32262306a36Sopenharmony_cistatic inline int __ptep_test_and_clear_young(struct mm_struct *mm, 32362306a36Sopenharmony_ci unsigned long addr, pte_t *ptep) 32462306a36Sopenharmony_ci{ 32562306a36Sopenharmony_ci unsigned long old; 32662306a36Sopenharmony_ci old = pte_update(mm, addr, ptep, _PAGE_ACCESSED, 0, 0); 32762306a36Sopenharmony_ci if (old & _PAGE_HASHPTE) 32862306a36Sopenharmony_ci flush_hash_entry(mm, ptep, addr); 32962306a36Sopenharmony_ci 33062306a36Sopenharmony_ci return (old & _PAGE_ACCESSED) != 0; 33162306a36Sopenharmony_ci} 33262306a36Sopenharmony_ci#define ptep_test_and_clear_young(__vma, __addr, __ptep) \ 33362306a36Sopenharmony_ci __ptep_test_and_clear_young((__vma)->vm_mm, __addr, __ptep) 33462306a36Sopenharmony_ci 33562306a36Sopenharmony_ci#define __HAVE_ARCH_PTEP_GET_AND_CLEAR 33662306a36Sopenharmony_cistatic inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, 33762306a36Sopenharmony_ci pte_t *ptep) 33862306a36Sopenharmony_ci{ 33962306a36Sopenharmony_ci return __pte(pte_update(mm, addr, ptep, ~_PAGE_HASHPTE, 0, 0)); 34062306a36Sopenharmony_ci} 34162306a36Sopenharmony_ci 34262306a36Sopenharmony_ci#define __HAVE_ARCH_PTEP_SET_WRPROTECT 34362306a36Sopenharmony_cistatic inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, 34462306a36Sopenharmony_ci pte_t *ptep) 34562306a36Sopenharmony_ci{ 34662306a36Sopenharmony_ci pte_update(mm, addr, ptep, _PAGE_RW, 0, 0); 34762306a36Sopenharmony_ci} 34862306a36Sopenharmony_ci 34962306a36Sopenharmony_cistatic inline void __ptep_set_access_flags(struct vm_area_struct *vma, 35062306a36Sopenharmony_ci pte_t *ptep, pte_t entry, 35162306a36Sopenharmony_ci unsigned long address, 35262306a36Sopenharmony_ci int psize) 35362306a36Sopenharmony_ci{ 35462306a36Sopenharmony_ci unsigned long set = pte_val(entry) & 35562306a36Sopenharmony_ci (_PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_RW | _PAGE_EXEC); 35662306a36Sopenharmony_ci 35762306a36Sopenharmony_ci pte_update(vma->vm_mm, address, ptep, 0, set, 0); 35862306a36Sopenharmony_ci 35962306a36Sopenharmony_ci flush_tlb_page(vma, address); 36062306a36Sopenharmony_ci} 36162306a36Sopenharmony_ci 36262306a36Sopenharmony_ci#define __HAVE_ARCH_PTE_SAME 36362306a36Sopenharmony_ci#define pte_same(A,B) (((pte_val(A) ^ pte_val(B)) & ~_PAGE_HASHPTE) == 0) 36462306a36Sopenharmony_ci 36562306a36Sopenharmony_ci#define pmd_pfn(pmd) (pmd_val(pmd) >> PAGE_SHIFT) 36662306a36Sopenharmony_ci#define pmd_page(pmd) pfn_to_page(pmd_pfn(pmd)) 36762306a36Sopenharmony_ci 36862306a36Sopenharmony_ci/* 36962306a36Sopenharmony_ci * Encode/decode swap entries and swap PTEs. Swap PTEs are all PTEs that 37062306a36Sopenharmony_ci * are !pte_none() && !pte_present(). 37162306a36Sopenharmony_ci * 37262306a36Sopenharmony_ci * Format of swap PTEs (32bit PTEs): 37362306a36Sopenharmony_ci * 37462306a36Sopenharmony_ci * 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 37562306a36Sopenharmony_ci * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 37662306a36Sopenharmony_ci * <----------------- offset --------------------> < type -> E H P 37762306a36Sopenharmony_ci * 37862306a36Sopenharmony_ci * E is the exclusive marker that is not stored in swap entries. 37962306a36Sopenharmony_ci * _PAGE_PRESENT (P) and __PAGE_HASHPTE (H) must be 0. 38062306a36Sopenharmony_ci * 38162306a36Sopenharmony_ci * For 64bit PTEs, the offset is extended by 32bit. 38262306a36Sopenharmony_ci */ 38362306a36Sopenharmony_ci#define __swp_type(entry) ((entry).val & 0x1f) 38462306a36Sopenharmony_ci#define __swp_offset(entry) ((entry).val >> 5) 38562306a36Sopenharmony_ci#define __swp_entry(type, offset) ((swp_entry_t) { ((type) & 0x1f) | ((offset) << 5) }) 38662306a36Sopenharmony_ci#define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) >> 3 }) 38762306a36Sopenharmony_ci#define __swp_entry_to_pte(x) ((pte_t) { (x).val << 3 }) 38862306a36Sopenharmony_ci 38962306a36Sopenharmony_cistatic inline int pte_swp_exclusive(pte_t pte) 39062306a36Sopenharmony_ci{ 39162306a36Sopenharmony_ci return pte_val(pte) & _PAGE_SWP_EXCLUSIVE; 39262306a36Sopenharmony_ci} 39362306a36Sopenharmony_ci 39462306a36Sopenharmony_cistatic inline pte_t pte_swp_mkexclusive(pte_t pte) 39562306a36Sopenharmony_ci{ 39662306a36Sopenharmony_ci return __pte(pte_val(pte) | _PAGE_SWP_EXCLUSIVE); 39762306a36Sopenharmony_ci} 39862306a36Sopenharmony_ci 39962306a36Sopenharmony_cistatic inline pte_t pte_swp_clear_exclusive(pte_t pte) 40062306a36Sopenharmony_ci{ 40162306a36Sopenharmony_ci return __pte(pte_val(pte) & ~_PAGE_SWP_EXCLUSIVE); 40262306a36Sopenharmony_ci} 40362306a36Sopenharmony_ci 40462306a36Sopenharmony_ci/* Generic accessors to PTE bits */ 40562306a36Sopenharmony_cistatic inline int pte_write(pte_t pte) { return !!(pte_val(pte) & _PAGE_RW);} 40662306a36Sopenharmony_cistatic inline int pte_read(pte_t pte) { return 1; } 40762306a36Sopenharmony_cistatic inline int pte_dirty(pte_t pte) { return !!(pte_val(pte) & _PAGE_DIRTY); } 40862306a36Sopenharmony_cistatic inline int pte_young(pte_t pte) { return !!(pte_val(pte) & _PAGE_ACCESSED); } 40962306a36Sopenharmony_cistatic inline int pte_special(pte_t pte) { return !!(pte_val(pte) & _PAGE_SPECIAL); } 41062306a36Sopenharmony_cistatic inline int pte_none(pte_t pte) { return (pte_val(pte) & ~_PTE_NONE_MASK) == 0; } 41162306a36Sopenharmony_cistatic inline bool pte_exec(pte_t pte) { return pte_val(pte) & _PAGE_EXEC; } 41262306a36Sopenharmony_ci 41362306a36Sopenharmony_cistatic inline int pte_present(pte_t pte) 41462306a36Sopenharmony_ci{ 41562306a36Sopenharmony_ci return pte_val(pte) & _PAGE_PRESENT; 41662306a36Sopenharmony_ci} 41762306a36Sopenharmony_ci 41862306a36Sopenharmony_cistatic inline bool pte_hw_valid(pte_t pte) 41962306a36Sopenharmony_ci{ 42062306a36Sopenharmony_ci return pte_val(pte) & _PAGE_PRESENT; 42162306a36Sopenharmony_ci} 42262306a36Sopenharmony_ci 42362306a36Sopenharmony_cistatic inline bool pte_hashpte(pte_t pte) 42462306a36Sopenharmony_ci{ 42562306a36Sopenharmony_ci return !!(pte_val(pte) & _PAGE_HASHPTE); 42662306a36Sopenharmony_ci} 42762306a36Sopenharmony_ci 42862306a36Sopenharmony_cistatic inline bool pte_ci(pte_t pte) 42962306a36Sopenharmony_ci{ 43062306a36Sopenharmony_ci return !!(pte_val(pte) & _PAGE_NO_CACHE); 43162306a36Sopenharmony_ci} 43262306a36Sopenharmony_ci 43362306a36Sopenharmony_ci/* 43462306a36Sopenharmony_ci * We only find page table entry in the last level 43562306a36Sopenharmony_ci * Hence no need for other accessors 43662306a36Sopenharmony_ci */ 43762306a36Sopenharmony_ci#define pte_access_permitted pte_access_permitted 43862306a36Sopenharmony_cistatic inline bool pte_access_permitted(pte_t pte, bool write) 43962306a36Sopenharmony_ci{ 44062306a36Sopenharmony_ci /* 44162306a36Sopenharmony_ci * A read-only access is controlled by _PAGE_USER bit. 44262306a36Sopenharmony_ci * We have _PAGE_READ set for WRITE and EXECUTE 44362306a36Sopenharmony_ci */ 44462306a36Sopenharmony_ci if (!pte_present(pte) || !pte_user(pte) || !pte_read(pte)) 44562306a36Sopenharmony_ci return false; 44662306a36Sopenharmony_ci 44762306a36Sopenharmony_ci if (write && !pte_write(pte)) 44862306a36Sopenharmony_ci return false; 44962306a36Sopenharmony_ci 45062306a36Sopenharmony_ci return true; 45162306a36Sopenharmony_ci} 45262306a36Sopenharmony_ci 45362306a36Sopenharmony_ci/* Conversion functions: convert a page and protection to a page entry, 45462306a36Sopenharmony_ci * and a page entry and page directory to the page they refer to. 45562306a36Sopenharmony_ci * 45662306a36Sopenharmony_ci * Even if PTEs can be unsigned long long, a PFN is always an unsigned 45762306a36Sopenharmony_ci * long for now. 45862306a36Sopenharmony_ci */ 45962306a36Sopenharmony_cistatic inline pte_t pfn_pte(unsigned long pfn, pgprot_t pgprot) 46062306a36Sopenharmony_ci{ 46162306a36Sopenharmony_ci return __pte(((pte_basic_t)(pfn) << PTE_RPN_SHIFT) | 46262306a36Sopenharmony_ci pgprot_val(pgprot)); 46362306a36Sopenharmony_ci} 46462306a36Sopenharmony_ci 46562306a36Sopenharmony_ci/* Generic modifiers for PTE bits */ 46662306a36Sopenharmony_cistatic inline pte_t pte_wrprotect(pte_t pte) 46762306a36Sopenharmony_ci{ 46862306a36Sopenharmony_ci return __pte(pte_val(pte) & ~_PAGE_RW); 46962306a36Sopenharmony_ci} 47062306a36Sopenharmony_ci 47162306a36Sopenharmony_cistatic inline pte_t pte_exprotect(pte_t pte) 47262306a36Sopenharmony_ci{ 47362306a36Sopenharmony_ci return __pte(pte_val(pte) & ~_PAGE_EXEC); 47462306a36Sopenharmony_ci} 47562306a36Sopenharmony_ci 47662306a36Sopenharmony_cistatic inline pte_t pte_mkclean(pte_t pte) 47762306a36Sopenharmony_ci{ 47862306a36Sopenharmony_ci return __pte(pte_val(pte) & ~_PAGE_DIRTY); 47962306a36Sopenharmony_ci} 48062306a36Sopenharmony_ci 48162306a36Sopenharmony_cistatic inline pte_t pte_mkold(pte_t pte) 48262306a36Sopenharmony_ci{ 48362306a36Sopenharmony_ci return __pte(pte_val(pte) & ~_PAGE_ACCESSED); 48462306a36Sopenharmony_ci} 48562306a36Sopenharmony_ci 48662306a36Sopenharmony_cistatic inline pte_t pte_mkexec(pte_t pte) 48762306a36Sopenharmony_ci{ 48862306a36Sopenharmony_ci return __pte(pte_val(pte) | _PAGE_EXEC); 48962306a36Sopenharmony_ci} 49062306a36Sopenharmony_ci 49162306a36Sopenharmony_cistatic inline pte_t pte_mkpte(pte_t pte) 49262306a36Sopenharmony_ci{ 49362306a36Sopenharmony_ci return pte; 49462306a36Sopenharmony_ci} 49562306a36Sopenharmony_ci 49662306a36Sopenharmony_cistatic inline pte_t pte_mkwrite_novma(pte_t pte) 49762306a36Sopenharmony_ci{ 49862306a36Sopenharmony_ci return __pte(pte_val(pte) | _PAGE_RW); 49962306a36Sopenharmony_ci} 50062306a36Sopenharmony_ci 50162306a36Sopenharmony_cistatic inline pte_t pte_mkdirty(pte_t pte) 50262306a36Sopenharmony_ci{ 50362306a36Sopenharmony_ci return __pte(pte_val(pte) | _PAGE_DIRTY); 50462306a36Sopenharmony_ci} 50562306a36Sopenharmony_ci 50662306a36Sopenharmony_cistatic inline pte_t pte_mkyoung(pte_t pte) 50762306a36Sopenharmony_ci{ 50862306a36Sopenharmony_ci return __pte(pte_val(pte) | _PAGE_ACCESSED); 50962306a36Sopenharmony_ci} 51062306a36Sopenharmony_ci 51162306a36Sopenharmony_cistatic inline pte_t pte_mkspecial(pte_t pte) 51262306a36Sopenharmony_ci{ 51362306a36Sopenharmony_ci return __pte(pte_val(pte) | _PAGE_SPECIAL); 51462306a36Sopenharmony_ci} 51562306a36Sopenharmony_ci 51662306a36Sopenharmony_cistatic inline pte_t pte_mkhuge(pte_t pte) 51762306a36Sopenharmony_ci{ 51862306a36Sopenharmony_ci return pte; 51962306a36Sopenharmony_ci} 52062306a36Sopenharmony_ci 52162306a36Sopenharmony_cistatic inline pte_t pte_mkprivileged(pte_t pte) 52262306a36Sopenharmony_ci{ 52362306a36Sopenharmony_ci return __pte(pte_val(pte) & ~_PAGE_USER); 52462306a36Sopenharmony_ci} 52562306a36Sopenharmony_ci 52662306a36Sopenharmony_cistatic inline pte_t pte_mkuser(pte_t pte) 52762306a36Sopenharmony_ci{ 52862306a36Sopenharmony_ci return __pte(pte_val(pte) | _PAGE_USER); 52962306a36Sopenharmony_ci} 53062306a36Sopenharmony_ci 53162306a36Sopenharmony_cistatic inline pte_t pte_modify(pte_t pte, pgprot_t newprot) 53262306a36Sopenharmony_ci{ 53362306a36Sopenharmony_ci return __pte((pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot)); 53462306a36Sopenharmony_ci} 53562306a36Sopenharmony_ci 53662306a36Sopenharmony_ci 53762306a36Sopenharmony_ci 53862306a36Sopenharmony_ci/* This low level function performs the actual PTE insertion 53962306a36Sopenharmony_ci * Setting the PTE depends on the MMU type and other factors. 54062306a36Sopenharmony_ci * 54162306a36Sopenharmony_ci * First case is 32-bit in UP mode with 32-bit PTEs, we need to preserve 54262306a36Sopenharmony_ci * the _PAGE_HASHPTE bit since we may not have invalidated the previous 54362306a36Sopenharmony_ci * translation in the hash yet (done in a subsequent flush_tlb_xxx()) 54462306a36Sopenharmony_ci * and see we need to keep track that this PTE needs invalidating. 54562306a36Sopenharmony_ci * 54662306a36Sopenharmony_ci * Second case is 32-bit with 64-bit PTE. In this case, we 54762306a36Sopenharmony_ci * can just store as long as we do the two halves in the right order 54862306a36Sopenharmony_ci * with a barrier in between. This is possible because we take care, 54962306a36Sopenharmony_ci * in the hash code, to pre-invalidate if the PTE was already hashed, 55062306a36Sopenharmony_ci * which synchronizes us with any concurrent invalidation. 55162306a36Sopenharmony_ci * In the percpu case, we fallback to the simple update preserving 55262306a36Sopenharmony_ci * the hash bits (ie, same as the non-SMP case). 55362306a36Sopenharmony_ci * 55462306a36Sopenharmony_ci * Third case is 32-bit in SMP mode with 32-bit PTEs. We use the 55562306a36Sopenharmony_ci * helper pte_update() which does an atomic update. We need to do that 55662306a36Sopenharmony_ci * because a concurrent invalidation can clear _PAGE_HASHPTE. If it's a 55762306a36Sopenharmony_ci * per-CPU PTE such as a kmap_atomic, we also do a simple update preserving 55862306a36Sopenharmony_ci * the hash bits instead. 55962306a36Sopenharmony_ci */ 56062306a36Sopenharmony_cistatic inline void __set_pte_at(struct mm_struct *mm, unsigned long addr, 56162306a36Sopenharmony_ci pte_t *ptep, pte_t pte, int percpu) 56262306a36Sopenharmony_ci{ 56362306a36Sopenharmony_ci if ((!IS_ENABLED(CONFIG_SMP) && !IS_ENABLED(CONFIG_PTE_64BIT)) || percpu) { 56462306a36Sopenharmony_ci *ptep = __pte((pte_val(*ptep) & _PAGE_HASHPTE) | 56562306a36Sopenharmony_ci (pte_val(pte) & ~_PAGE_HASHPTE)); 56662306a36Sopenharmony_ci } else if (IS_ENABLED(CONFIG_PTE_64BIT)) { 56762306a36Sopenharmony_ci if (pte_val(*ptep) & _PAGE_HASHPTE) 56862306a36Sopenharmony_ci flush_hash_entry(mm, ptep, addr); 56962306a36Sopenharmony_ci 57062306a36Sopenharmony_ci asm volatile("stw%X0 %2,%0; eieio; stw%X1 %L2,%1" : 57162306a36Sopenharmony_ci "=m" (*ptep), "=m" (*((unsigned char *)ptep+4)) : 57262306a36Sopenharmony_ci "r" (pte) : "memory"); 57362306a36Sopenharmony_ci } else { 57462306a36Sopenharmony_ci pte_update(mm, addr, ptep, ~_PAGE_HASHPTE, pte_val(pte), 0); 57562306a36Sopenharmony_ci } 57662306a36Sopenharmony_ci} 57762306a36Sopenharmony_ci 57862306a36Sopenharmony_ci/* 57962306a36Sopenharmony_ci * Macro to mark a page protection value as "uncacheable". 58062306a36Sopenharmony_ci */ 58162306a36Sopenharmony_ci 58262306a36Sopenharmony_ci#define _PAGE_CACHE_CTL (_PAGE_COHERENT | _PAGE_GUARDED | _PAGE_NO_CACHE | \ 58362306a36Sopenharmony_ci _PAGE_WRITETHRU) 58462306a36Sopenharmony_ci 58562306a36Sopenharmony_ci#define pgprot_noncached pgprot_noncached 58662306a36Sopenharmony_cistatic inline pgprot_t pgprot_noncached(pgprot_t prot) 58762306a36Sopenharmony_ci{ 58862306a36Sopenharmony_ci return __pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | 58962306a36Sopenharmony_ci _PAGE_NO_CACHE | _PAGE_GUARDED); 59062306a36Sopenharmony_ci} 59162306a36Sopenharmony_ci 59262306a36Sopenharmony_ci#define pgprot_noncached_wc pgprot_noncached_wc 59362306a36Sopenharmony_cistatic inline pgprot_t pgprot_noncached_wc(pgprot_t prot) 59462306a36Sopenharmony_ci{ 59562306a36Sopenharmony_ci return __pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | 59662306a36Sopenharmony_ci _PAGE_NO_CACHE); 59762306a36Sopenharmony_ci} 59862306a36Sopenharmony_ci 59962306a36Sopenharmony_ci#define pgprot_cached pgprot_cached 60062306a36Sopenharmony_cistatic inline pgprot_t pgprot_cached(pgprot_t prot) 60162306a36Sopenharmony_ci{ 60262306a36Sopenharmony_ci return __pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | 60362306a36Sopenharmony_ci _PAGE_COHERENT); 60462306a36Sopenharmony_ci} 60562306a36Sopenharmony_ci 60662306a36Sopenharmony_ci#define pgprot_cached_wthru pgprot_cached_wthru 60762306a36Sopenharmony_cistatic inline pgprot_t pgprot_cached_wthru(pgprot_t prot) 60862306a36Sopenharmony_ci{ 60962306a36Sopenharmony_ci return __pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | 61062306a36Sopenharmony_ci _PAGE_COHERENT | _PAGE_WRITETHRU); 61162306a36Sopenharmony_ci} 61262306a36Sopenharmony_ci 61362306a36Sopenharmony_ci#define pgprot_cached_noncoherent pgprot_cached_noncoherent 61462306a36Sopenharmony_cistatic inline pgprot_t pgprot_cached_noncoherent(pgprot_t prot) 61562306a36Sopenharmony_ci{ 61662306a36Sopenharmony_ci return __pgprot(pgprot_val(prot) & ~_PAGE_CACHE_CTL); 61762306a36Sopenharmony_ci} 61862306a36Sopenharmony_ci 61962306a36Sopenharmony_ci#define pgprot_writecombine pgprot_writecombine 62062306a36Sopenharmony_cistatic inline pgprot_t pgprot_writecombine(pgprot_t prot) 62162306a36Sopenharmony_ci{ 62262306a36Sopenharmony_ci return pgprot_noncached_wc(prot); 62362306a36Sopenharmony_ci} 62462306a36Sopenharmony_ci 62562306a36Sopenharmony_ci#endif /* !__ASSEMBLY__ */ 62662306a36Sopenharmony_ci 62762306a36Sopenharmony_ci#endif /* _ASM_POWERPC_BOOK3S_32_PGTABLE_H */ 628