Path: blob/master/arch/powerpc/include/asm/book3s/32/pgtable.h
50831 views
/* SPDX-License-Identifier: GPL-2.0 */1#ifndef _ASM_POWERPC_BOOK3S_32_PGTABLE_H2#define _ASM_POWERPC_BOOK3S_32_PGTABLE_H34#include <asm-generic/pgtable-nopmd.h>56/*7* The "classic" 32-bit implementation of the PowerPC MMU uses a hash8* table containing PTEs, together with a set of 16 segment registers,9* to define the virtual to physical address mapping.10*11* We use the hash table as an extended TLB, i.e. a cache of currently12* active mappings. We maintain a two-level page table tree, much13* like that used by the i386, for the sake of the Linux memory14* management code. Low-level assembler code in hash_low_32.S15* (procedure hash_page) is responsible for extracting ptes from the16* tree and putting them into the hash table when necessary, and17* updating the accessed and modified bits in the page table tree.18*/1920#define _PAGE_PRESENT 0x001 /* software: pte contains a translation */21#define _PAGE_HASHPTE 0x002 /* hash_page has made an HPTE for this pte */22#define _PAGE_READ 0x004 /* software: read access allowed */23#define _PAGE_GUARDED 0x008 /* G: prohibit speculative access */24#define _PAGE_COHERENT 0x010 /* M: enforce memory coherence (SMP systems) */25#define _PAGE_NO_CACHE 0x020 /* I: cache inhibit */26#define _PAGE_WRITETHRU 0x040 /* W: cache write-through */27#define _PAGE_DIRTY 0x080 /* C: page changed */28#define _PAGE_ACCESSED 0x100 /* R: page referenced */29#define _PAGE_EXEC 0x200 /* software: exec allowed */30#define _PAGE_WRITE 0x400 /* software: user write access allowed */31#define _PAGE_SPECIAL 0x800 /* software: Special page */3233#ifdef CONFIG_PTE_64BIT34/* We never clear the high word of the pte */35#define _PTE_NONE_MASK (0xffffffff00000000ULL | _PAGE_HASHPTE)36#else37#define _PTE_NONE_MASK _PAGE_HASHPTE38#endif3940#define _PMD_PRESENT 041#define _PMD_PRESENT_MASK (PAGE_MASK)42#define _PMD_BAD (~PAGE_MASK)4344/* We borrow the _PAGE_READ bit to store the exclusive marker in swap PTEs. */45#define _PAGE_SWP_EXCLUSIVE _PAGE_READ4647/* And here we include common definitions */4849#define _PAGE_HPTEFLAGS _PAGE_HASHPTE5051/*52* Location of the PFN in the PTE. Most 32-bit platforms use the same53* as _PAGE_SHIFT here (ie, naturally aligned).54* Platform who don't just pre-define the value so we don't override it here.55*/56#define PTE_RPN_SHIFT (PAGE_SHIFT)5758/*59* The mask covered by the RPN must be a ULL on 32-bit platforms with60* 64-bit PTEs.61*/62#ifdef CONFIG_PTE_64BIT63#define PTE_RPN_MASK (~((1ULL << PTE_RPN_SHIFT) - 1))64#define MAX_POSSIBLE_PHYSMEM_BITS 3665#else66#define PTE_RPN_MASK (~((1UL << PTE_RPN_SHIFT) - 1))67#define MAX_POSSIBLE_PHYSMEM_BITS 3268#endif6970/*71* _PAGE_CHG_MASK masks of bits that are to be preserved across72* pgprot changes.73*/74#define _PAGE_CHG_MASK (PTE_RPN_MASK | _PAGE_HASHPTE | _PAGE_DIRTY | \75_PAGE_ACCESSED | _PAGE_SPECIAL)7677/*78* We define 2 sets of base prot bits, one for basic pages (ie,79* cacheable kernel and user pages) and one for non cacheable80* pages. We always set _PAGE_COHERENT when SMP is enabled or81* the processor might need it for DMA coherency.82*/83#define _PAGE_BASE_NC (_PAGE_PRESENT | _PAGE_ACCESSED)84#define _PAGE_BASE (_PAGE_BASE_NC | _PAGE_COHERENT)8586#include <asm/pgtable-masks.h>8788/* Permission masks used for kernel mappings */89#define PAGE_KERNEL __pgprot(_PAGE_BASE | _PAGE_KERNEL_RW)90#define PAGE_KERNEL_NC __pgprot(_PAGE_BASE_NC | _PAGE_KERNEL_RW | _PAGE_NO_CACHE)91#define PAGE_KERNEL_NCG __pgprot(_PAGE_BASE_NC | _PAGE_KERNEL_RW | _PAGE_NO_CACHE | _PAGE_GUARDED)92#define PAGE_KERNEL_X __pgprot(_PAGE_BASE | _PAGE_KERNEL_RWX)93#define PAGE_KERNEL_RO __pgprot(_PAGE_BASE | _PAGE_KERNEL_RO)94#define PAGE_KERNEL_ROX __pgprot(_PAGE_BASE | _PAGE_KERNEL_ROX)9596#define PTE_INDEX_SIZE PTE_SHIFT97#define PMD_INDEX_SIZE 098#define PUD_INDEX_SIZE 099#define PGD_INDEX_SIZE (32 - PGDIR_SHIFT)100101#define PMD_CACHE_INDEX PMD_INDEX_SIZE102#define PUD_CACHE_INDEX PUD_INDEX_SIZE103104#ifndef __ASSEMBLER__105#define PTE_TABLE_SIZE (sizeof(pte_t) << PTE_INDEX_SIZE)106#define PMD_TABLE_SIZE 0107#define PUD_TABLE_SIZE 0108#define PGD_TABLE_SIZE (sizeof(pgd_t) << PGD_INDEX_SIZE)109110/* Bits to mask out from a PMD to get to the PTE page */111#define PMD_MASKED_BITS (PTE_TABLE_SIZE - 1)112#endif /* __ASSEMBLER__ */113114#define PTRS_PER_PTE (1 << PTE_INDEX_SIZE)115#define PTRS_PER_PGD (1 << PGD_INDEX_SIZE)116117/*118* The normal case is that PTEs are 32-bits and we have a 1-page119* 1024-entry pgdir pointing to 1-page 1024-entry PTE pages. -- paulus120*121* For any >32-bit physical address platform, we can use the following122* two level page table layout where the pgdir is 8KB and the MS 13 bits123* are an index to the second level table. The combined pgdir/pmd first124* level has 2048 entries and the second level has 512 64-bit PTE entries.125* -Matt126*/127/* PGDIR_SHIFT determines what a top-level page table entry can map */128#define PGDIR_SHIFT (PAGE_SHIFT + PTE_INDEX_SIZE)129#define PGDIR_SIZE (1UL << PGDIR_SHIFT)130#define PGDIR_MASK (~(PGDIR_SIZE-1))131132#define USER_PTRS_PER_PGD (TASK_SIZE / PGDIR_SIZE)133134#ifndef __ASSEMBLER__135136int map_kernel_page(unsigned long va, phys_addr_t pa, pgprot_t prot);137void unmap_kernel_page(unsigned long va);138139#endif /* !__ASSEMBLER__ */140141/*142* This is the bottom of the PKMAP area with HIGHMEM or an arbitrary143* value (for now) on others, from where we can start layout kernel144* virtual space that goes below PKMAP and FIXMAP145*/146147#define FIXADDR_SIZE 0148#ifdef CONFIG_KASAN149#include <asm/kasan.h>150#define FIXADDR_TOP (KASAN_SHADOW_START - PAGE_SIZE)151#else152#define FIXADDR_TOP ((unsigned long)(-PAGE_SIZE))153#endif154155/*156* ioremap_bot starts at that address. Early ioremaps move down from there,157* until mem_init() at which point this becomes the top of the vmalloc158* and ioremap space159*/160#ifdef CONFIG_HIGHMEM161#define IOREMAP_TOP PKMAP_BASE162#else163#define IOREMAP_TOP FIXADDR_START164#endif165166/* PPC32 shares vmalloc area with ioremap */167#define IOREMAP_START VMALLOC_START168#define IOREMAP_END VMALLOC_END169170/*171* Just any arbitrary offset to the start of the vmalloc VM area: the172* current 16MB value just means that there will be a 64MB "hole" after the173* physical memory until the kernel virtual memory starts. That means that174* any out-of-bounds memory accesses will hopefully be caught.175* The vmalloc() routines leaves a hole of 4kB between each vmalloced176* area for the same reason. ;)177*178* We no longer map larger than phys RAM with the BATs so we don't have179* to worry about the VMALLOC_OFFSET causing problems. We do have to worry180* about clashes between our early calls to ioremap() that start growing down181* from ioremap_base being run into the VM area allocations (growing upwards182* from VMALLOC_START). For this reason we have ioremap_bot to check when183* we actually run into our mappings setup in the early boot with the VM184* system. This really does become a problem for machines with good amounts185* of RAM. -- Cort186*/187#define VMALLOC_OFFSET (0x1000000) /* 16M */188189#define VMALLOC_START ((((long)high_memory + VMALLOC_OFFSET) & ~(VMALLOC_OFFSET-1)))190191#ifdef CONFIG_KASAN_VMALLOC192#define VMALLOC_END ALIGN_DOWN(ioremap_bot, PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT)193#else194#define VMALLOC_END ioremap_bot195#endif196197#ifndef __ASSEMBLER__198#include <linux/sched.h>199#include <linux/threads.h>200201/* Bits to mask out from a PGD to get to the PUD page */202#define PGD_MASKED_BITS 0203204#define pgd_ERROR(e) \205pr_err("%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__, pgd_val(e))206/*207* Bits in a linux-style PTE. These match the bits in the208* (hardware-defined) PowerPC PTE as closely as possible.209*/210211#define pte_clear(mm, addr, ptep) \212do { pte_update(mm, addr, ptep, ~_PAGE_HASHPTE, 0, 0); } while (0)213214#define pmd_none(pmd) (!pmd_val(pmd))215#define pmd_bad(pmd) (pmd_val(pmd) & _PMD_BAD)216#define pmd_present(pmd) (pmd_val(pmd) & _PMD_PRESENT_MASK)217static inline void pmd_clear(pmd_t *pmdp)218{219*pmdp = __pmd(0);220}221222223/*224* When flushing the tlb entry for a page, we also need to flush the hash225* table entry. flush_hash_pages is assembler (for speed) in hashtable.S.226*/227extern int flush_hash_pages(unsigned context, unsigned long va,228unsigned long pmdval, int count);229230/* Add an HPTE to the hash table */231extern void add_hash_page(unsigned context, unsigned long va,232unsigned long pmdval);233234/* Flush an entry from the TLB/hash table */235static inline void flush_hash_entry(struct mm_struct *mm, pte_t *ptep, unsigned long addr)236{237if (mmu_has_feature(MMU_FTR_HPTE_TABLE)) {238unsigned long ptephys = __pa(ptep) & PAGE_MASK;239240flush_hash_pages(mm->context.id, addr, ptephys, 1);241}242}243244/*245* PTE updates. This function is called whenever an existing246* valid PTE is updated. This does -not- include set_pte_at()247* which nowadays only sets a new PTE.248*249* Depending on the type of MMU, we may need to use atomic updates250* and the PTE may be either 32 or 64 bit wide. In the later case,251* when using atomic updates, only the low part of the PTE is252* accessed atomically.253*/254static inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, pte_t *p,255unsigned long clr, unsigned long set, int huge)256{257pte_basic_t old;258259if (mmu_has_feature(MMU_FTR_HPTE_TABLE)) {260unsigned long tmp;261262asm volatile(263#ifndef CONFIG_PTE_64BIT264"1: lwarx %0, 0, %3\n"265" andc %1, %0, %4\n"266#else267"1: lwarx %L0, 0, %3\n"268" lwz %0, -4(%3)\n"269" andc %1, %L0, %4\n"270#endif271" or %1, %1, %5\n"272" stwcx. %1, 0, %3\n"273" bne- 1b"274: "=&r" (old), "=&r" (tmp), "=m" (*p)275#ifndef CONFIG_PTE_64BIT276: "r" (p),277#else278: "b" ((unsigned long)(p) + 4),279#endif280"r" (clr), "r" (set), "m" (*p)281: "cc" );282} else {283old = pte_val(*p);284285*p = __pte((old & ~(pte_basic_t)clr) | set);286}287288return old;289}290291/*292* 2.6 calls this without flushing the TLB entry; this is wrong293* for our hash-based implementation, we fix that up here.294*/295#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG296static inline int __ptep_test_and_clear_young(struct mm_struct *mm,297unsigned long addr, pte_t *ptep)298{299unsigned long old;300old = pte_update(mm, addr, ptep, _PAGE_ACCESSED, 0, 0);301if (old & _PAGE_HASHPTE)302flush_hash_entry(mm, ptep, addr);303304return (old & _PAGE_ACCESSED) != 0;305}306#define ptep_test_and_clear_young(__vma, __addr, __ptep) \307__ptep_test_and_clear_young((__vma)->vm_mm, __addr, __ptep)308309#define __HAVE_ARCH_PTEP_GET_AND_CLEAR310static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,311pte_t *ptep)312{313return __pte(pte_update(mm, addr, ptep, ~_PAGE_HASHPTE, 0, 0));314}315316#define __HAVE_ARCH_PTEP_SET_WRPROTECT317static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr,318pte_t *ptep)319{320pte_update(mm, addr, ptep, _PAGE_WRITE, 0, 0);321}322323static inline void __ptep_set_access_flags(struct vm_area_struct *vma,324pte_t *ptep, pte_t entry,325unsigned long address,326int psize)327{328unsigned long set = pte_val(entry) &329(_PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_RW | _PAGE_EXEC);330331pte_update(vma->vm_mm, address, ptep, 0, set, 0);332333flush_tlb_page(vma, address);334}335336#define __HAVE_ARCH_PTE_SAME337#define pte_same(A,B) (((pte_val(A) ^ pte_val(B)) & ~_PAGE_HASHPTE) == 0)338339#define pmd_pfn(pmd) (pmd_val(pmd) >> PAGE_SHIFT)340#define pmd_page(pmd) pfn_to_page(pmd_pfn(pmd))341342/*343* Encode/decode swap entries and swap PTEs. Swap PTEs are all PTEs that344* are !pte_none() && !pte_present().345*346* Format of swap PTEs (32bit PTEs):347*348* 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3349* 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 1350* <----------------- offset --------------------> < type -> E H P351*352* E is the exclusive marker that is not stored in swap entries.353* _PAGE_PRESENT (P) and __PAGE_HASHPTE (H) must be 0.354*355* For 64bit PTEs, the offset is extended by 32bit.356*/357#define __swp_type(entry) ((entry).val & 0x1f)358#define __swp_offset(entry) ((entry).val >> 5)359#define __swp_entry(type, offset) ((swp_entry_t) { ((type) & 0x1f) | ((offset) << 5) })360#define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) >> 3 })361#define __swp_entry_to_pte(x) ((pte_t) { (x).val << 3 })362363static inline bool pte_swp_exclusive(pte_t pte)364{365return pte_val(pte) & _PAGE_SWP_EXCLUSIVE;366}367368static inline pte_t pte_swp_mkexclusive(pte_t pte)369{370return __pte(pte_val(pte) | _PAGE_SWP_EXCLUSIVE);371}372373static inline pte_t pte_swp_clear_exclusive(pte_t pte)374{375return __pte(pte_val(pte) & ~_PAGE_SWP_EXCLUSIVE);376}377378/* Generic accessors to PTE bits */379static inline bool pte_read(pte_t pte)380{381return !!(pte_val(pte) & _PAGE_READ);382}383384static inline bool pte_write(pte_t pte)385{386return !!(pte_val(pte) & _PAGE_WRITE);387}388389static inline int pte_dirty(pte_t pte) { return !!(pte_val(pte) & _PAGE_DIRTY); }390static inline int pte_young(pte_t pte) { return !!(pte_val(pte) & _PAGE_ACCESSED); }391static inline int pte_special(pte_t pte) { return !!(pte_val(pte) & _PAGE_SPECIAL); }392static inline int pte_none(pte_t pte) { return (pte_val(pte) & ~_PTE_NONE_MASK) == 0; }393static inline bool pte_exec(pte_t pte) { return pte_val(pte) & _PAGE_EXEC; }394395static inline int pte_present(pte_t pte)396{397return pte_val(pte) & _PAGE_PRESENT;398}399400static inline bool pte_hw_valid(pte_t pte)401{402return pte_val(pte) & _PAGE_PRESENT;403}404405static inline bool pte_hashpte(pte_t pte)406{407return !!(pte_val(pte) & _PAGE_HASHPTE);408}409410static inline bool pte_ci(pte_t pte)411{412return !!(pte_val(pte) & _PAGE_NO_CACHE);413}414415/*416* We only find page table entry in the last level417* Hence no need for other accessors418*/419#define pte_access_permitted pte_access_permitted420static inline bool pte_access_permitted(pte_t pte, bool write)421{422/*423* A read-only access is controlled by _PAGE_READ bit.424* We have _PAGE_READ set for WRITE425*/426if (!pte_present(pte) || !pte_read(pte))427return false;428429if (write && !pte_write(pte))430return false;431432return true;433}434435/* Conversion functions: convert a page and protection to a page entry,436* and a page entry and page directory to the page they refer to.437*438* Even if PTEs can be unsigned long long, a PFN is always an unsigned439* long for now.440*/441static inline pte_t pfn_pte(unsigned long pfn, pgprot_t pgprot)442{443return __pte(((pte_basic_t)(pfn) << PTE_RPN_SHIFT) |444pgprot_val(pgprot));445}446447/* Generic modifiers for PTE bits */448static inline pte_t pte_wrprotect(pte_t pte)449{450return __pte(pte_val(pte) & ~_PAGE_WRITE);451}452453static inline pte_t pte_exprotect(pte_t pte)454{455return __pte(pte_val(pte) & ~_PAGE_EXEC);456}457458static inline pte_t pte_mkclean(pte_t pte)459{460return __pte(pte_val(pte) & ~_PAGE_DIRTY);461}462463static inline pte_t pte_mkold(pte_t pte)464{465return __pte(pte_val(pte) & ~_PAGE_ACCESSED);466}467468static inline pte_t pte_mkexec(pte_t pte)469{470return __pte(pte_val(pte) | _PAGE_EXEC);471}472473static inline pte_t pte_mkpte(pte_t pte)474{475return pte;476}477478static inline pte_t pte_mkwrite_novma(pte_t pte)479{480/*481* write implies read, hence set both482*/483return __pte(pte_val(pte) | _PAGE_RW);484}485486static inline pte_t pte_mkdirty(pte_t pte)487{488return __pte(pte_val(pte) | _PAGE_DIRTY);489}490491static inline pte_t pte_mkyoung(pte_t pte)492{493return __pte(pte_val(pte) | _PAGE_ACCESSED);494}495496static inline pte_t pte_mkspecial(pte_t pte)497{498return __pte(pte_val(pte) | _PAGE_SPECIAL);499}500501static inline pte_t pte_mkhuge(pte_t pte)502{503return pte;504}505506static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)507{508return __pte((pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot));509}510511512513/* This low level function performs the actual PTE insertion514* Setting the PTE depends on the MMU type and other factors.515*516* First case is 32-bit in UP mode with 32-bit PTEs, we need to preserve517* the _PAGE_HASHPTE bit since we may not have invalidated the previous518* translation in the hash yet (done in a subsequent flush_tlb_xxx())519* and see we need to keep track that this PTE needs invalidating.520*521* Second case is 32-bit with 64-bit PTE. In this case, we522* can just store as long as we do the two halves in the right order523* with a barrier in between. This is possible because we take care,524* in the hash code, to pre-invalidate if the PTE was already hashed,525* which synchronizes us with any concurrent invalidation.526* In the percpu case, we fallback to the simple update preserving527* the hash bits (ie, same as the non-SMP case).528*529* Third case is 32-bit in SMP mode with 32-bit PTEs. We use the530* helper pte_update() which does an atomic update. We need to do that531* because a concurrent invalidation can clear _PAGE_HASHPTE. If it's a532* per-CPU PTE such as a kmap_atomic, we also do a simple update preserving533* the hash bits instead.534*/535static inline void __set_pte_at(struct mm_struct *mm, unsigned long addr,536pte_t *ptep, pte_t pte, int percpu)537{538if ((!IS_ENABLED(CONFIG_SMP) && !IS_ENABLED(CONFIG_PTE_64BIT)) || percpu) {539*ptep = __pte((pte_val(*ptep) & _PAGE_HASHPTE) |540(pte_val(pte) & ~_PAGE_HASHPTE));541} else if (IS_ENABLED(CONFIG_PTE_64BIT)) {542if (pte_val(*ptep) & _PAGE_HASHPTE)543flush_hash_entry(mm, ptep, addr);544545asm volatile("stw%X0 %2,%0; eieio; stw%X1 %L2,%1" :546"=m" (*ptep), "=m" (*((unsigned char *)ptep+4)) :547"r" (pte) : "memory");548} else {549pte_update(mm, addr, ptep, ~_PAGE_HASHPTE, pte_val(pte), 0);550}551}552553/*554* Macro to mark a page protection value as "uncacheable".555*/556557#define _PAGE_CACHE_CTL (_PAGE_COHERENT | _PAGE_GUARDED | _PAGE_NO_CACHE | \558_PAGE_WRITETHRU)559560#define pgprot_noncached pgprot_noncached561static inline pgprot_t pgprot_noncached(pgprot_t prot)562{563return __pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) |564_PAGE_NO_CACHE | _PAGE_GUARDED);565}566567#define pgprot_noncached_wc pgprot_noncached_wc568static inline pgprot_t pgprot_noncached_wc(pgprot_t prot)569{570return __pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) |571_PAGE_NO_CACHE);572}573574#define pgprot_cached pgprot_cached575static inline pgprot_t pgprot_cached(pgprot_t prot)576{577return __pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) |578_PAGE_COHERENT);579}580581#define pgprot_cached_wthru pgprot_cached_wthru582static inline pgprot_t pgprot_cached_wthru(pgprot_t prot)583{584return __pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) |585_PAGE_COHERENT | _PAGE_WRITETHRU);586}587588#define pgprot_cached_noncoherent pgprot_cached_noncoherent589static inline pgprot_t pgprot_cached_noncoherent(pgprot_t prot)590{591return __pgprot(pgprot_val(prot) & ~_PAGE_CACHE_CTL);592}593594#define pgprot_writecombine pgprot_writecombine595static inline pgprot_t pgprot_writecombine(pgprot_t prot)596{597return pgprot_noncached_wc(prot);598}599600#endif /* !__ASSEMBLER__ */601602#endif /* _ASM_POWERPC_BOOK3S_32_PGTABLE_H */603604605