1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 20c668984SRussell King /* 30c668984SRussell King * linux/arch/arm/mm/pgd.c 40c668984SRussell King * 50c668984SRussell King * Copyright (C) 1998-2005 Russell King 60c668984SRussell King */ 70c668984SRussell King #include <linux/mm.h> 85a0e3ad6STejun Heo #include <linux/gfp.h> 90c668984SRussell King #include <linux/highmem.h> 10da028779SCatalin Marinas #include <linux/slab.h> 110c668984SRussell King 1215d07dc9SRussell King #include <asm/cp15.h> 130c668984SRussell King #include <asm/pgalloc.h> 140c668984SRussell King #include <asm/page.h> 150c668984SRussell King #include <asm/tlbflush.h> 160c668984SRussell King 170c668984SRussell King #include "mm.h" 180c668984SRussell King 19da028779SCatalin Marinas #ifdef CONFIG_ARM_LPAE 206da2ec56SKees Cook #define __pgd_alloc() kmalloc_array(PTRS_PER_PGD, sizeof(pgd_t), GFP_KERNEL) 21da028779SCatalin Marinas #define __pgd_free(pgd) kfree(pgd) 22da028779SCatalin Marinas #else 23397b080bSMichal Hocko #define __pgd_alloc() (pgd_t *)__get_free_pages(GFP_KERNEL, 2) 24da028779SCatalin Marinas #define __pgd_free(pgd) free_pages((unsigned long)pgd, 2) 25da028779SCatalin Marinas #endif 26da028779SCatalin Marinas 270c668984SRussell King /* 280c668984SRussell King * need to get a 16k page for level 1 290c668984SRussell King */ 30b0d03745SRussell King pgd_t *pgd_alloc(struct mm_struct *mm) 310c668984SRussell King { 320c668984SRussell King pgd_t *new_pgd, *init_pgd; 33516295e5SRussell King pud_t *new_pud, *init_pud; 340c668984SRussell King pmd_t *new_pmd, *init_pmd; 350c668984SRussell King pte_t *new_pte, *init_pte; 360c668984SRussell King 37da028779SCatalin Marinas new_pgd = __pgd_alloc(); 380c668984SRussell King if (!new_pgd) 390c668984SRussell King goto no_pgd; 400c668984SRussell King 41e926f449SRussell King memset(new_pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t)); 420c668984SRussell King 430c668984SRussell King /* 440c668984SRussell King * Copy over the kernel and IO PGD entries 450c668984SRussell King */ 460c668984SRussell King init_pgd = pgd_offset_k(0); 47e926f449SRussell King memcpy(new_pgd + USER_PTRS_PER_PGD, init_pgd + USER_PTRS_PER_PGD, 48e926f449SRussell King (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t)); 490c668984SRussell King 500c668984SRussell King clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t)); 510c668984SRussell King 52da028779SCatalin Marinas #ifdef CONFIG_ARM_LPAE 53da028779SCatalin Marinas /* 54da028779SCatalin Marinas * Allocate PMD table for modules and pkmap mappings. 55da028779SCatalin Marinas */ 56da028779SCatalin Marinas new_pud = pud_alloc(mm, new_pgd + pgd_index(MODULES_VADDR), 57da028779SCatalin Marinas MODULES_VADDR); 58da028779SCatalin Marinas if (!new_pud) 59da028779SCatalin Marinas goto no_pud; 60da028779SCatalin Marinas 61da028779SCatalin Marinas new_pmd = pmd_alloc(mm, new_pud, 0); 62da028779SCatalin Marinas if (!new_pmd) 63da028779SCatalin Marinas goto no_pmd; 64da028779SCatalin Marinas #endif 65da028779SCatalin Marinas 660c668984SRussell King if (!vectors_high()) { 670c668984SRussell King /* 680c668984SRussell King * On ARM, first page must always be allocated since it 69da028779SCatalin Marinas * contains the machine vectors. The vectors are always high 70da028779SCatalin Marinas * with LPAE. 710c668984SRussell King */ 72516295e5SRussell King new_pud = pud_alloc(mm, new_pgd, 0); 73516295e5SRussell King if (!new_pud) 74516295e5SRussell King goto no_pud; 75516295e5SRussell King 76516295e5SRussell King new_pmd = pmd_alloc(mm, new_pud, 0); 770c668984SRussell King if (!new_pmd) 780c668984SRussell King goto no_pmd; 790c668984SRussell King 803ed3a4f0SKirill A. Shutemov new_pte = pte_alloc_map(mm, new_pmd, 0); 810c668984SRussell King if (!new_pte) 820c668984SRussell King goto no_pte; 830c668984SRussell King 84a02d8dfdSRussell King #ifndef CONFIG_ARM_LPAE 85a02d8dfdSRussell King /* 86a02d8dfdSRussell King * Modify the PTE pointer to have the correct domain. This 87a02d8dfdSRussell King * needs to be the vectors domain to avoid the low vectors 88a02d8dfdSRussell King * being unmapped. 89a02d8dfdSRussell King */ 90a02d8dfdSRussell King pmd_val(*new_pmd) &= ~PMD_DOMAIN_MASK; 91a02d8dfdSRussell King pmd_val(*new_pmd) |= PMD_DOMAIN(DOMAIN_VECTORS); 92a02d8dfdSRussell King #endif 93a02d8dfdSRussell King 94516295e5SRussell King init_pud = pud_offset(init_pgd, 0); 95516295e5SRussell King init_pmd = pmd_offset(init_pud, 0); 96ece0e2b6SPeter Zijlstra init_pte = pte_offset_map(init_pmd, 0); 97d8aa712cSRussell King set_pte_ext(new_pte + 0, init_pte[0], 0); 98d8aa712cSRussell King set_pte_ext(new_pte + 1, init_pte[1], 0); 99ece0e2b6SPeter Zijlstra pte_unmap(init_pte); 1000c668984SRussell King pte_unmap(new_pte); 1010c668984SRussell King } 1020c668984SRussell King 1030c668984SRussell King return new_pgd; 1040c668984SRussell King 1050c668984SRussell King no_pte: 1065e541973SBenjamin Herrenschmidt pmd_free(mm, new_pmd); 107b30fe6c7SKirill A. Shutemov mm_dec_nr_pmds(mm); 1080c668984SRussell King no_pmd: 109516295e5SRussell King pud_free(mm, new_pud); 110516295e5SRussell King no_pud: 111da028779SCatalin Marinas __pgd_free(new_pgd); 1120c668984SRussell King no_pgd: 1130c668984SRussell King return NULL; 1140c668984SRussell King } 1150c668984SRussell King 1166e4beb5eSRussell King void pgd_free(struct mm_struct *mm, pgd_t *pgd_base) 1170c668984SRussell King { 1186e4beb5eSRussell King pgd_t *pgd; 119516295e5SRussell King pud_t *pud; 1200c668984SRussell King pmd_t *pmd; 1210c82d83cSUwe Kleine-König pgtable_t pte; 1220c668984SRussell King 1236e4beb5eSRussell King if (!pgd_base) 1240c668984SRussell King return; 1250c668984SRussell King 1266e4beb5eSRussell King pgd = pgd_base + pgd_index(0); 1276e4beb5eSRussell King if (pgd_none_or_clear_bad(pgd)) 1286e4beb5eSRussell King goto no_pgd; 1296e4beb5eSRussell King 130516295e5SRussell King pud = pud_offset(pgd, 0); 131516295e5SRussell King if (pud_none_or_clear_bad(pud)) 132516295e5SRussell King goto no_pud; 133516295e5SRussell King 134516295e5SRussell King pmd = pmd_offset(pud, 0); 1356e4beb5eSRussell King if (pmd_none_or_clear_bad(pmd)) 1366e4beb5eSRussell King goto no_pmd; 1370c668984SRussell King 1380c82d83cSUwe Kleine-König pte = pmd_pgtable(*pmd); 1390c668984SRussell King pmd_clear(pmd); 1405e541973SBenjamin Herrenschmidt pte_free(mm, pte); 141c4812909SKirill A. Shutemov mm_dec_nr_ptes(mm); 1426e4beb5eSRussell King no_pmd: 143516295e5SRussell King pud_clear(pud); 1445e541973SBenjamin Herrenschmidt pmd_free(mm, pmd); 145b30fe6c7SKirill A. Shutemov mm_dec_nr_pmds(mm); 146516295e5SRussell King no_pud: 147516295e5SRussell King pgd_clear(pgd); 148516295e5SRussell King pud_free(mm, pud); 1496e4beb5eSRussell King no_pgd: 150da028779SCatalin Marinas #ifdef CONFIG_ARM_LPAE 151da028779SCatalin Marinas /* 152da028779SCatalin Marinas * Free modules/pkmap or identity pmd tables. 153da028779SCatalin Marinas */ 154da028779SCatalin Marinas for (pgd = pgd_base; pgd < pgd_base + PTRS_PER_PGD; pgd++) { 155da028779SCatalin Marinas if (pgd_none_or_clear_bad(pgd)) 156da028779SCatalin Marinas continue; 157da028779SCatalin Marinas if (pgd_val(*pgd) & L_PGD_SWAPPER) 158da028779SCatalin Marinas continue; 159da028779SCatalin Marinas pud = pud_offset(pgd, 0); 160da028779SCatalin Marinas if (pud_none_or_clear_bad(pud)) 161da028779SCatalin Marinas continue; 162da028779SCatalin Marinas pmd = pmd_offset(pud, 0); 163da028779SCatalin Marinas pud_clear(pud); 164da028779SCatalin Marinas pmd_free(mm, pmd); 165b30fe6c7SKirill A. Shutemov mm_dec_nr_pmds(mm); 166da028779SCatalin Marinas pgd_clear(pgd); 167da028779SCatalin Marinas pud_free(mm, pud); 168da028779SCatalin Marinas } 169da028779SCatalin Marinas #endif 170da028779SCatalin Marinas __pgd_free(pgd_base); 1710c668984SRussell King } 172