1 // SPDX-License-Identifier: GPL-2.0
3 * Helper routines for building identity mapping page tables. This is
4 * included by both the compressed kernel and the regular kernel.
7 static void ident_pmd_init(struct x86_mapping_info *info, pmd_t *pmd_page,
8 unsigned long addr, unsigned long end)
11 for (; addr < end; addr += PMD_SIZE) {
12 pmd_t *pmd = pmd_page + pmd_index(addr);
14 if (pmd_present(*pmd))
17 set_pmd(pmd, __pmd((addr - info->offset) | info->page_flag));
21 static int ident_pud_init(struct x86_mapping_info *info, pud_t *pud_page,
22 unsigned long addr, unsigned long end)
26 for (; addr < end; addr = next) {
27 pud_t *pud = pud_page + pud_index(addr);
30 next = (addr & PUD_MASK) + PUD_SIZE;
34 if (info->direct_gbpages) {
37 if (pud_present(*pud))
41 pudval = __pud((addr - info->offset) | info->page_flag);
46 if (pud_present(*pud)) {
47 pmd = pmd_offset(pud, 0);
48 ident_pmd_init(info, pmd, addr, next);
51 pmd = (pmd_t *)info->alloc_pgt_page(info->context);
54 ident_pmd_init(info, pmd, addr, next);
55 set_pud(pud, __pud(__pa(pmd) | info->kernpg_flag));
61 static int ident_p4d_init(struct x86_mapping_info *info, p4d_t *p4d_page,
62 unsigned long addr, unsigned long end)
67 for (; addr < end; addr = next) {
68 p4d_t *p4d = p4d_page + p4d_index(addr);
71 next = (addr & P4D_MASK) + P4D_SIZE;
75 if (p4d_present(*p4d)) {
76 pud = pud_offset(p4d, 0);
77 result = ident_pud_init(info, pud, addr, next);
83 pud = (pud_t *)info->alloc_pgt_page(info->context);
87 result = ident_pud_init(info, pud, addr, next);
91 set_p4d(p4d, __p4d(__pa(pud) | info->kernpg_flag));
97 int kernel_ident_mapping_init(struct x86_mapping_info *info, pgd_t *pgd_page,
98 unsigned long pstart, unsigned long pend)
100 unsigned long addr = pstart + info->offset;
101 unsigned long end = pend + info->offset;
105 /* Set the default pagetable flags if not supplied */
106 if (!info->kernpg_flag)
107 info->kernpg_flag = _KERNPG_TABLE;
109 for (; addr < end; addr = next) {
110 pgd_t *pgd = pgd_page + pgd_index(addr);
113 next = (addr & PGDIR_MASK) + PGDIR_SIZE;
117 if (pgd_present(*pgd)) {
118 p4d = p4d_offset(pgd, 0);
119 result = ident_p4d_init(info, p4d, addr, next);
125 p4d = (p4d_t *)info->alloc_pgt_page(info->context);
128 result = ident_p4d_init(info, p4d, addr, next);
131 if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
132 set_pgd(pgd, __pgd(__pa(p4d) | info->kernpg_flag));
135 * With p4d folded, pgd is equal to p4d.
136 * The pgd entry has to point to the pud page table in this case.
138 pud_t *pud = pud_offset(p4d, 0);
139 set_pgd(pgd, __pgd(__pa(pud) | info->kernpg_flag));