1 #ifndef _ASMARM64_PGTABLE_H_ 2 #define _ASMARM64_PGTABLE_H_ 3 /* 4 * Adapted from arch/arm64/include/asm/pgtable.h 5 * include/asm-generic/pgtable-nopmd.h 6 * include/linux/mm.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.h> 17 #include <alloc_page.h> 18 #include <asm/setup.h> 19 #include <asm/page.h> 20 #include <asm/pgtable-hwdef.h> 21 22 #include <linux/compiler.h> 23 24 /* 25 * We can convert va <=> pa page table addresses with simple casts 26 * because we always allocate their pages with alloc_page(), and 27 * alloc_page() always returns identity mapped pages. 28 */ 29 #define pgtable_va(x) ((void *)(unsigned long)(x)) 30 #define pgtable_pa(x) ((unsigned long)(x)) 31 32 #define pgd_none(pgd) (!pgd_val(pgd)) 33 #define pmd_none(pmd) (!pmd_val(pmd)) 34 #define pte_none(pte) (!pte_val(pte)) 35 36 #define pgd_valid(pgd) (pgd_val(pgd) & PGD_VALID) 37 #define pmd_valid(pmd) (pmd_val(pmd) & PMD_SECT_VALID) 38 #define pte_valid(pte) (pte_val(pte) & PTE_VALID) 39 40 #define pmd_huge(pmd) \ 41 ((pmd_val(pmd) & PMD_TYPE_MASK) == PMD_TYPE_SECT) 42 43 #define pgd_index(addr) \ 44 (((addr) >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1)) 45 #define pgd_offset(pgtable, addr) ((pgtable) + pgd_index(addr)) 46 47 #define pgd_free(pgd) free(pgd) 48 static inline pgd_t *pgd_alloc(void) 49 { 50 pgd_t *pgd = memalign(PAGE_SIZE, PTRS_PER_PGD * sizeof(pgd_t)); 51 memset(pgd, 0, PTRS_PER_PGD * sizeof(pgd_t)); 52 return pgd; 53 } 54 55 #define pmd_offset(pgd, addr) ((pmd_t *)pgd) 56 #define pmd_free(pmd) 57 #define pmd_alloc(pgd, addr) pmd_offset(pgd, addr) 58 59 static inline pte_t *pmd_page_vaddr(pmd_t pmd) 60 { 61 return pgtable_va(pmd_val(pmd) & PHYS_MASK & (s32)PAGE_MASK); 62 } 63 64 #define pte_index(addr) \ 65 (((addr) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) 66 #define pte_offset(pmd, addr) \ 67 (pmd_page_vaddr(*(pmd)) + pte_index(addr)) 68 69 #define pte_free(pte) free_page(pte) 70 static inline pte_t *pte_alloc_one(void) 71 { 72 assert(PTRS_PER_PTE * sizeof(pte_t) == PAGE_SIZE); 73 pte_t *pte = alloc_page(); 74 return pte; 75 } 76 static inline pte_t *pte_alloc(pmd_t *pmd, unsigned long addr) 77 { 78 if (pmd_none(*pmd)) { 79 pmd_t entry; 80 pmd_val(entry) = pgtable_pa(pte_alloc_one()) | PMD_TYPE_TABLE; 81 WRITE_ONCE(*pmd, entry); 82 } 83 return pte_offset(pmd, addr); 84 } 85 86 #endif /* _ASMARM64_PGTABLE_H_ */ 87