^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/arch/arm/mm/pgd.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 1998-2005 Russell King
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <asm/cp15.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <asm/pgalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <asm/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <asm/tlbflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "mm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #ifdef CONFIG_ARM_LPAE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define __pgd_alloc() kmalloc_array(PTRS_PER_PGD, sizeof(pgd_t), GFP_KERNEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define __pgd_free(pgd) kfree(pgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define __pgd_alloc() (pgd_t *)__get_free_pages(GFP_KERNEL, 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define __pgd_free(pgd) free_pages((unsigned long)pgd, 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * need to get a 16k page for level 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) pgd_t *pgd_alloc(struct mm_struct *mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) pgd_t *new_pgd, *init_pgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) p4d_t *new_p4d, *init_p4d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) pud_t *new_pud, *init_pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) pmd_t *new_pmd, *init_pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) pte_t *new_pte, *init_pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) new_pgd = __pgd_alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (!new_pgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) goto no_pgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) memset(new_pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * Copy over the kernel and IO PGD entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) init_pgd = pgd_offset_k(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) memcpy(new_pgd + USER_PTRS_PER_PGD, init_pgd + USER_PTRS_PER_PGD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #ifdef CONFIG_ARM_LPAE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * Allocate PMD table for modules and pkmap mappings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) new_p4d = p4d_alloc(mm, new_pgd + pgd_index(MODULES_VADDR),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) MODULES_VADDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (!new_p4d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) goto no_p4d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) new_pud = pud_alloc(mm, new_p4d, MODULES_VADDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (!new_pud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) goto no_pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) new_pmd = pmd_alloc(mm, new_pud, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (!new_pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) goto no_pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (!vectors_high()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * On ARM, first page must always be allocated since it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * contains the machine vectors. The vectors are always high
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * with LPAE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) new_p4d = p4d_alloc(mm, new_pgd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (!new_p4d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) goto no_p4d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) new_pud = pud_alloc(mm, new_p4d, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (!new_pud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) goto no_pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) new_pmd = pmd_alloc(mm, new_pud, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (!new_pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) goto no_pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) new_pte = pte_alloc_map(mm, new_pmd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (!new_pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) goto no_pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #ifndef CONFIG_ARM_LPAE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * Modify the PTE pointer to have the correct domain. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * needs to be the vectors domain to avoid the low vectors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * being unmapped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) pmd_val(*new_pmd) &= ~PMD_DOMAIN_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) pmd_val(*new_pmd) |= PMD_DOMAIN(DOMAIN_VECTORS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) init_p4d = p4d_offset(init_pgd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) init_pud = pud_offset(init_p4d, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) init_pmd = pmd_offset(init_pud, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) init_pte = pte_offset_map(init_pmd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) set_pte_ext(new_pte + 0, init_pte[0], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) set_pte_ext(new_pte + 1, init_pte[1], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) pte_unmap(init_pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) pte_unmap(new_pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return new_pgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) no_pte:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) pmd_free(mm, new_pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) mm_dec_nr_pmds(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) no_pmd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) pud_free(mm, new_pud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) no_pud:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) p4d_free(mm, new_p4d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) no_p4d:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) __pgd_free(new_pgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) no_pgd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) void pgd_free(struct mm_struct *mm, pgd_t *pgd_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) pgd_t *pgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) p4d_t *p4d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) pud_t *pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) pmd_t *pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) pgtable_t pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (!pgd_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) pgd = pgd_base + pgd_index(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (pgd_none_or_clear_bad(pgd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) goto no_pgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) p4d = p4d_offset(pgd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (p4d_none_or_clear_bad(p4d))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) goto no_p4d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) pud = pud_offset(p4d, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (pud_none_or_clear_bad(pud))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) goto no_pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) pmd = pmd_offset(pud, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (pmd_none_or_clear_bad(pmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) goto no_pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) pte = pmd_pgtable(*pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) pmd_clear(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) pte_free(mm, pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) mm_dec_nr_ptes(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) no_pmd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) pud_clear(pud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) pmd_free(mm, pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) mm_dec_nr_pmds(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) no_pud:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) p4d_clear(p4d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) pud_free(mm, pud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) no_p4d:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) pgd_clear(pgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) p4d_free(mm, p4d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) no_pgd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) #ifdef CONFIG_ARM_LPAE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * Free modules/pkmap or identity pmd tables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) for (pgd = pgd_base; pgd < pgd_base + PTRS_PER_PGD; pgd++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (pgd_none_or_clear_bad(pgd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (pgd_val(*pgd) & L_PGD_SWAPPER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) p4d = p4d_offset(pgd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (p4d_none_or_clear_bad(p4d))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) pud = pud_offset(p4d, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (pud_none_or_clear_bad(pud))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) pmd = pmd_offset(pud, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) pud_clear(pud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) pmd_free(mm, pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) mm_dec_nr_pmds(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) p4d_clear(p4d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) pud_free(mm, pud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) mm_dec_nr_puds(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) pgd_clear(pgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) p4d_free(mm, p4d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) __pgd_free(pgd_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }