1d2912cb1SThomas 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; 33*84e6ffb2SMike Rapoport p4d_t *new_p4d, *init_p4d; 34516295e5SRussell King pud_t *new_pud, *init_pud; 350c668984SRussell King pmd_t *new_pmd, *init_pmd; 360c668984SRussell King pte_t *new_pte, *init_pte; 370c668984SRussell King 38da028779SCatalin Marinas new_pgd = __pgd_alloc(); 390c668984SRussell King if (!new_pgd) 400c668984SRussell King goto no_pgd; 410c668984SRussell King 42e926f449SRussell King memset(new_pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t)); 430c668984SRussell King 440c668984SRussell King /* 450c668984SRussell King * Copy over the kernel and IO PGD entries 460c668984SRussell King */ 470c668984SRussell King init_pgd = pgd_offset_k(0); 48e926f449SRussell King memcpy(new_pgd + USER_PTRS_PER_PGD, init_pgd + USER_PTRS_PER_PGD, 49e926f449SRussell King (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t)); 500c668984SRussell King 510c668984SRussell King clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t)); 520c668984SRussell King 53da028779SCatalin Marinas #ifdef CONFIG_ARM_LPAE 54da028779SCatalin Marinas /* 55da028779SCatalin Marinas * Allocate PMD table for modules and pkmap mappings. 56da028779SCatalin Marinas */ 57*84e6ffb2SMike Rapoport new_p4d = p4d_alloc(mm, new_pgd + pgd_index(MODULES_VADDR), 58da028779SCatalin Marinas MODULES_VADDR); 59*84e6ffb2SMike Rapoport if (!new_p4d) 60*84e6ffb2SMike Rapoport goto no_p4d; 61*84e6ffb2SMike Rapoport 62*84e6ffb2SMike Rapoport new_pud = pud_alloc(mm, new_p4d, MODULES_VADDR); 63da028779SCatalin Marinas if (!new_pud) 64da028779SCatalin Marinas goto no_pud; 65da028779SCatalin Marinas 66da028779SCatalin Marinas new_pmd = pmd_alloc(mm, new_pud, 0); 67da028779SCatalin Marinas if (!new_pmd) 68da028779SCatalin Marinas goto no_pmd; 69da028779SCatalin Marinas #endif 70da028779SCatalin Marinas 710c668984SRussell King if (!vectors_high()) { 720c668984SRussell King /* 730c668984SRussell King * On ARM, first page must always be allocated since it 74da028779SCatalin Marinas * contains the machine vectors. The vectors are always high 75da028779SCatalin Marinas * with LPAE. 760c668984SRussell King */ 77*84e6ffb2SMike Rapoport new_p4d = p4d_alloc(mm, new_pgd, 0); 78*84e6ffb2SMike Rapoport if (!new_p4d) 79*84e6ffb2SMike Rapoport goto no_p4d; 80*84e6ffb2SMike Rapoport 81*84e6ffb2SMike Rapoport new_pud = pud_alloc(mm, new_p4d, 0); 82516295e5SRussell King if (!new_pud) 83516295e5SRussell King goto no_pud; 84516295e5SRussell King 85516295e5SRussell King new_pmd = pmd_alloc(mm, new_pud, 0); 860c668984SRussell King if (!new_pmd) 870c668984SRussell King goto no_pmd; 880c668984SRussell King 893ed3a4f0SKirill A. Shutemov new_pte = pte_alloc_map(mm, new_pmd, 0); 900c668984SRussell King if (!new_pte) 910c668984SRussell King goto no_pte; 920c668984SRussell King 93a02d8dfdSRussell King #ifndef CONFIG_ARM_LPAE 94a02d8dfdSRussell King /* 95a02d8dfdSRussell King * Modify the PTE pointer to have the correct domain. This 96a02d8dfdSRussell King * needs to be the vectors domain to avoid the low vectors 97a02d8dfdSRussell King * being unmapped. 98a02d8dfdSRussell King */ 99a02d8dfdSRussell King pmd_val(*new_pmd) &= ~PMD_DOMAIN_MASK; 100a02d8dfdSRussell King pmd_val(*new_pmd) |= PMD_DOMAIN(DOMAIN_VECTORS); 101a02d8dfdSRussell King #endif 102a02d8dfdSRussell King 103*84e6ffb2SMike Rapoport init_p4d = p4d_offset(init_pgd, 0); 104*84e6ffb2SMike Rapoport init_pud = pud_offset(init_p4d, 0); 105516295e5SRussell King init_pmd = pmd_offset(init_pud, 0); 106ece0e2b6SPeter Zijlstra init_pte = pte_offset_map(init_pmd, 0); 107d8aa712cSRussell King set_pte_ext(new_pte + 0, init_pte[0], 0); 108d8aa712cSRussell King set_pte_ext(new_pte + 1, init_pte[1], 0); 109ece0e2b6SPeter Zijlstra pte_unmap(init_pte); 1100c668984SRussell King pte_unmap(new_pte); 1110c668984SRussell King } 1120c668984SRussell King 1130c668984SRussell King return new_pgd; 1140c668984SRussell King 1150c668984SRussell King no_pte: 1165e541973SBenjamin Herrenschmidt pmd_free(mm, new_pmd); 117b30fe6c7SKirill A. Shutemov mm_dec_nr_pmds(mm); 1180c668984SRussell King no_pmd: 119516295e5SRussell King pud_free(mm, new_pud); 120516295e5SRussell King no_pud: 121*84e6ffb2SMike Rapoport p4d_free(mm, new_p4d); 122*84e6ffb2SMike Rapoport no_p4d: 123da028779SCatalin Marinas __pgd_free(new_pgd); 1240c668984SRussell King no_pgd: 1250c668984SRussell King return NULL; 1260c668984SRussell King } 1270c668984SRussell King 1286e4beb5eSRussell King void pgd_free(struct mm_struct *mm, pgd_t *pgd_base) 1290c668984SRussell King { 1306e4beb5eSRussell King pgd_t *pgd; 131*84e6ffb2SMike Rapoport p4d_t *p4d; 132516295e5SRussell King pud_t *pud; 1330c668984SRussell King pmd_t *pmd; 1340c82d83cSUwe Kleine-König pgtable_t pte; 1350c668984SRussell King 1366e4beb5eSRussell King if (!pgd_base) 1370c668984SRussell King return; 1380c668984SRussell King 1396e4beb5eSRussell King pgd = pgd_base + pgd_index(0); 1406e4beb5eSRussell King if (pgd_none_or_clear_bad(pgd)) 1416e4beb5eSRussell King goto no_pgd; 1426e4beb5eSRussell King 143*84e6ffb2SMike Rapoport p4d = p4d_offset(pgd, 0); 144*84e6ffb2SMike Rapoport if (p4d_none_or_clear_bad(p4d)) 145*84e6ffb2SMike Rapoport goto no_p4d; 146*84e6ffb2SMike Rapoport 147*84e6ffb2SMike Rapoport pud = pud_offset(p4d, 0); 148516295e5SRussell King if (pud_none_or_clear_bad(pud)) 149516295e5SRussell King goto no_pud; 150516295e5SRussell King 151516295e5SRussell King pmd = pmd_offset(pud, 0); 1526e4beb5eSRussell King if (pmd_none_or_clear_bad(pmd)) 1536e4beb5eSRussell King goto no_pmd; 1540c668984SRussell King 1550c82d83cSUwe Kleine-König pte = pmd_pgtable(*pmd); 1560c668984SRussell King pmd_clear(pmd); 1575e541973SBenjamin Herrenschmidt pte_free(mm, pte); 158c4812909SKirill A. Shutemov mm_dec_nr_ptes(mm); 1596e4beb5eSRussell King no_pmd: 160516295e5SRussell King pud_clear(pud); 1615e541973SBenjamin Herrenschmidt pmd_free(mm, pmd); 162b30fe6c7SKirill A. Shutemov mm_dec_nr_pmds(mm); 163516295e5SRussell King no_pud: 164*84e6ffb2SMike Rapoport p4d_clear(p4d); 165516295e5SRussell King pud_free(mm, pud); 166*84e6ffb2SMike Rapoport no_p4d: 167*84e6ffb2SMike Rapoport pgd_clear(pgd); 168*84e6ffb2SMike Rapoport p4d_free(mm, p4d); 1696e4beb5eSRussell King no_pgd: 170da028779SCatalin Marinas #ifdef CONFIG_ARM_LPAE 171da028779SCatalin Marinas /* 172da028779SCatalin Marinas * Free modules/pkmap or identity pmd tables. 173da028779SCatalin Marinas */ 174da028779SCatalin Marinas for (pgd = pgd_base; pgd < pgd_base + PTRS_PER_PGD; pgd++) { 175da028779SCatalin Marinas if (pgd_none_or_clear_bad(pgd)) 176da028779SCatalin Marinas continue; 177da028779SCatalin Marinas if (pgd_val(*pgd) & L_PGD_SWAPPER) 178da028779SCatalin Marinas continue; 179*84e6ffb2SMike Rapoport p4d = p4d_offset(pgd, 0); 180*84e6ffb2SMike Rapoport if (p4d_none_or_clear_bad(p4d)) 181*84e6ffb2SMike Rapoport continue; 182*84e6ffb2SMike Rapoport pud = pud_offset(p4d, 0); 183da028779SCatalin Marinas if (pud_none_or_clear_bad(pud)) 184da028779SCatalin Marinas continue; 185da028779SCatalin Marinas pmd = pmd_offset(pud, 0); 186da028779SCatalin Marinas pud_clear(pud); 187da028779SCatalin Marinas pmd_free(mm, pmd); 188b30fe6c7SKirill A. Shutemov mm_dec_nr_pmds(mm); 189*84e6ffb2SMike Rapoport p4d_clear(p4d); 190da028779SCatalin Marinas pud_free(mm, pud); 191*84e6ffb2SMike Rapoport mm_dec_nr_puds(mm); 192*84e6ffb2SMike Rapoport pgd_clear(pgd); 193*84e6ffb2SMike Rapoport p4d_free(mm, p4d); 194da028779SCatalin Marinas } 195da028779SCatalin Marinas #endif 196da028779SCatalin Marinas __pgd_free(pgd_base); 1970c668984SRussell King } 198