1 #ifndef _ASMARM_PGTABLE_H_ 2 #define _ASMARM_PGTABLE_H_ 3 /* 4 * Adapted from arch/arm/include/asm/pgtable.h 5 * arch/arm/include/asm/pgtable-3level.h 6 * arch/arm/include/asm/pgalloc.h 7 * 8 * Note: some Linux function APIs have been modified. Nothing crazy, 9 * but if a function took, for example, an mm_struct, then 10 * that was either removed or replaced. 11 * 12 * Copyright (C) 2017, Red Hat Inc, Andrew Jones <drjones@redhat.com> 13 * 14 * This work is licensed under the terms of the GNU GPL, version 2. 15 */ 16 #include <alloc_page.h> 17 18 /* 19 * We can convert va <=> pa page table addresses with simple casts 20 * because we always allocate their pages with alloc_page(), and 21 * alloc_page() always returns identity mapped pages. 22 */ 23 #include <linux/compiler.h> 24 25 #define pgtable_va(x) ((void *)(unsigned long)(x)) 26 #define pgtable_pa(x) ((unsigned long)(x)) 27 28 #define pgd_none(pgd) (!pgd_val(pgd)) 29 #define pmd_none(pmd) (!pmd_val(pmd)) 30 #define pte_none(pte) (!pte_val(pte)) 31 32 #define pgd_index(addr) \ 33 (((addr) >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1)) 34 #define pgd_offset(pgtable, addr) ((pgtable) + pgd_index(addr)) 35 36 #define pgd_free(pgd) free(pgd) 37 static inline pgd_t *pgd_alloc(void) 38 { 39 pgd_t *pgd = memalign(L1_CACHE_BYTES, PTRS_PER_PGD * sizeof(pgd_t)); 40 memset(pgd, 0, PTRS_PER_PGD * sizeof(pgd_t)); 41 return pgd; 42 } 43 44 static inline pmd_t *pgd_page_vaddr(pgd_t pgd) 45 { 46 return pgtable_va(pgd_val(pgd) & PHYS_MASK & (s32)PAGE_MASK); 47 } 48 49 #define pmd_index(addr) \ 50 (((addr) >> PMD_SHIFT) & (PTRS_PER_PMD - 1)) 51 #define pmd_offset(pgd, addr) \ 52 (pgd_page_vaddr(*(pgd)) + pmd_index(addr)) 53 54 #define pmd_free(pmd) free_page(pmd) 55 static inline pmd_t *pmd_alloc_one(void) 56 { 57 assert(PTRS_PER_PMD * sizeof(pmd_t) == PAGE_SIZE); 58 pmd_t *pmd = alloc_page(); 59 return pmd; 60 } 61 static inline pmd_t *pmd_alloc(pgd_t *pgd, unsigned long addr) 62 { 63 if (pgd_none(*pgd)) { 64 pgd_t entry; 65 pgd_val(entry) = pgtable_pa(pmd_alloc_one()) | PMD_TYPE_TABLE; 66 WRITE_ONCE(*pgd, entry); 67 } 68 return pmd_offset(pgd, addr); 69 } 70 71 static inline pte_t *pmd_page_vaddr(pmd_t pmd) 72 { 73 return pgtable_va(pmd_val(pmd) & PHYS_MASK & (s32)PAGE_MASK); 74 } 75 76 #define pte_index(addr) \ 77 (((addr) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) 78 #define pte_offset(pmd, addr) \ 79 (pmd_page_vaddr(*(pmd)) + pte_index(addr)) 80 81 #define pte_free(pte) free_page(pte) 82 static inline pte_t *pte_alloc_one(void) 83 { 84 assert(PTRS_PER_PTE * sizeof(pte_t) == PAGE_SIZE); 85 pte_t *pte = alloc_page(); 86 return pte; 87 } 88 static inline pte_t *pte_alloc(pmd_t *pmd, unsigned long addr) 89 { 90 if (pmd_none(*pmd)) { 91 pmd_t entry; 92 pmd_val(entry) = pgtable_pa(pte_alloc_one()) | PMD_TYPE_TABLE; 93 WRITE_ONCE(*pmd, entry); 94 } 95 return pte_offset(pmd, addr); 96 } 97 98 #endif /* _ASMARM_PGTABLE_H_ */ 99