1 // SPDX-License-Identifier: GPL-2.0
4 * Transitional page tables for kexec and hibernate
6 * This file derived from: arch/arm64/kernel/hibernate.c
8 * Copyright (c) 2020, Microsoft Corporation.
9 * Pavel Tatashin <pasha.tatashin@soleen.com>
14 * Transitional tables are used during system transferring from one world to
15 * another: such as during hibernate restore, and kexec reboots. During these
16 * phases one cannot rely on page table not being overwritten. This is because
17 * hibernate and kexec can overwrite the current page tables during transition.
20 #include <asm/trans_pgd.h>
21 #include <asm/pgalloc.h>
22 #include <asm/pgtable.h>
23 #include <linux/suspend.h>
24 #include <linux/bug.h>
26 #include <linux/mmzone.h>
28 static void *trans_alloc(struct trans_pgd_info *info)
30 return info->trans_alloc_page(info->trans_alloc_arg);
33 static void _copy_pte(pte_t *dst_ptep, pte_t *src_ptep, unsigned long addr)
35 pte_t pte = READ_ONCE(*src_ptep);
39 * Resume will overwrite areas that may be marked
40 * read only (code, rodata). Clear the RDONLY bit from
41 * the temporary mappings we use during restore.
43 set_pte(dst_ptep, pte_mkwrite(pte));
44 } else if (debug_pagealloc_enabled() && !pte_none(pte)) {
46 * debug_pagealloc will removed the PTE_VALID bit if
47 * the page isn't in use by the resume kernel. It may have
48 * been in use by the original kernel, in which case we need
49 * to put it back in our copy to do the restore.
51 * Before marking this entry valid, check the pfn should
54 BUG_ON(!pfn_valid(pte_pfn(pte)));
56 set_pte(dst_ptep, pte_mkpresent(pte_mkwrite(pte)));
60 static int copy_pte(struct trans_pgd_info *info, pmd_t *dst_pmdp,
61 pmd_t *src_pmdp, unsigned long start, unsigned long end)
65 unsigned long addr = start;
67 dst_ptep = trans_alloc(info);
70 pmd_populate_kernel(NULL, dst_pmdp, dst_ptep);
71 dst_ptep = pte_offset_kernel(dst_pmdp, start);
73 src_ptep = pte_offset_kernel(src_pmdp, start);
75 _copy_pte(dst_ptep, src_ptep, addr);
76 } while (dst_ptep++, src_ptep++, addr += PAGE_SIZE, addr != end);
81 static int copy_pmd(struct trans_pgd_info *info, pud_t *dst_pudp,
82 pud_t *src_pudp, unsigned long start, unsigned long end)
87 unsigned long addr = start;
89 if (pud_none(READ_ONCE(*dst_pudp))) {
90 dst_pmdp = trans_alloc(info);
93 pud_populate(NULL, dst_pudp, dst_pmdp);
95 dst_pmdp = pmd_offset(dst_pudp, start);
97 src_pmdp = pmd_offset(src_pudp, start);
99 pmd_t pmd = READ_ONCE(*src_pmdp);
101 next = pmd_addr_end(addr, end);
104 if (pmd_table(pmd)) {
105 if (copy_pte(info, dst_pmdp, src_pmdp, addr, next))
109 __pmd(pmd_val(pmd) & ~PMD_SECT_RDONLY));
111 } while (dst_pmdp++, src_pmdp++, addr = next, addr != end);
116 static int copy_pud(struct trans_pgd_info *info, p4d_t *dst_p4dp,
117 p4d_t *src_p4dp, unsigned long start,
123 unsigned long addr = start;
125 if (p4d_none(READ_ONCE(*dst_p4dp))) {
126 dst_pudp = trans_alloc(info);
129 p4d_populate(NULL, dst_p4dp, dst_pudp);
131 dst_pudp = pud_offset(dst_p4dp, start);
133 src_pudp = pud_offset(src_p4dp, start);
135 pud_t pud = READ_ONCE(*src_pudp);
137 next = pud_addr_end(addr, end);
140 if (pud_table(pud)) {
141 if (copy_pmd(info, dst_pudp, src_pudp, addr, next))
145 __pud(pud_val(pud) & ~PUD_SECT_RDONLY));
147 } while (dst_pudp++, src_pudp++, addr = next, addr != end);
152 static int copy_p4d(struct trans_pgd_info *info, pgd_t *dst_pgdp,
153 pgd_t *src_pgdp, unsigned long start,
159 unsigned long addr = start;
161 dst_p4dp = p4d_offset(dst_pgdp, start);
162 src_p4dp = p4d_offset(src_pgdp, start);
164 next = p4d_addr_end(addr, end);
165 if (p4d_none(READ_ONCE(*src_p4dp)))
167 if (copy_pud(info, dst_p4dp, src_p4dp, addr, next))
169 } while (dst_p4dp++, src_p4dp++, addr = next, addr != end);
174 static int copy_page_tables(struct trans_pgd_info *info, pgd_t *dst_pgdp,
175 unsigned long start, unsigned long end)
178 unsigned long addr = start;
179 pgd_t *src_pgdp = pgd_offset_k(start);
181 dst_pgdp = pgd_offset_pgd(dst_pgdp, start);
183 next = pgd_addr_end(addr, end);
184 if (pgd_none(READ_ONCE(*src_pgdp)))
186 if (copy_p4d(info, dst_pgdp, src_pgdp, addr, next))
188 } while (dst_pgdp++, src_pgdp++, addr = next, addr != end);
194 * Create trans_pgd and copy linear map.
195 * info: contains allocator and its argument
196 * dst_pgdp: new page table that is created, and to which map is copied.
197 * start: Start of the interval (inclusive).
198 * end: End of the interval (exclusive).
200 * Returns 0 on success, and -ENOMEM on failure.
202 int trans_pgd_create_copy(struct trans_pgd_info *info, pgd_t **dst_pgdp,
203 unsigned long start, unsigned long end)
206 pgd_t *trans_pgd = trans_alloc(info);
209 pr_err("Failed to allocate memory for temporary page tables.\n");
213 rc = copy_page_tables(info, trans_pgd, start, end);
215 *dst_pgdp = trans_pgd;
221 * Add map entry to trans_pgd for a base-size page at PTE level.
222 * info: contains allocator and its argument
223 * trans_pgd: page table in which new map is added.
224 * page: page to be mapped.
225 * dst_addr: new VA address for the page
226 * pgprot: protection for the page.
228 * Returns 0 on success, and -ENOMEM on failure.
230 int trans_pgd_map_page(struct trans_pgd_info *info, pgd_t *trans_pgd,
231 void *page, unsigned long dst_addr, pgprot_t pgprot)
239 pgdp = pgd_offset_pgd(trans_pgd, dst_addr);
240 if (pgd_none(READ_ONCE(*pgdp))) {
241 p4dp = trans_alloc(info);
244 pgd_populate(NULL, pgdp, p4dp);
247 p4dp = p4d_offset(pgdp, dst_addr);
248 if (p4d_none(READ_ONCE(*p4dp))) {
249 pudp = trans_alloc(info);
252 p4d_populate(NULL, p4dp, pudp);
255 pudp = pud_offset(p4dp, dst_addr);
256 if (pud_none(READ_ONCE(*pudp))) {
257 pmdp = trans_alloc(info);
260 pud_populate(NULL, pudp, pmdp);
263 pmdp = pmd_offset(pudp, dst_addr);
264 if (pmd_none(READ_ONCE(*pmdp))) {
265 ptep = trans_alloc(info);
268 pmd_populate_kernel(NULL, pmdp, ptep);
271 ptep = pte_offset_kernel(pmdp, dst_addr);
272 set_pte(ptep, pfn_pte(virt_to_pfn(page), pgprot));