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) 2021, Microsoft Corporation.
9 * Pasha 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 * The page we want to idmap may be outside the range covered by VA_BITS that
222 * can be built using the kernel's p?d_populate() helpers. As a one off, for a
223 * single page, we build these page tables bottom up and just assume that will
224 * need the maximum T0SZ.
226 * Returns 0 on success, and -ENOMEM on failure.
227 * On success trans_ttbr0 contains page table with idmapped page, t0sz is set to
228 * maximum T0SZ for this page.
230 int trans_pgd_idmap_page(struct trans_pgd_info *info, phys_addr_t *trans_ttbr0,
231 unsigned long *t0sz, void *page)
233 phys_addr_t dst_addr = virt_to_phys(page);
234 unsigned long pfn = __phys_to_pfn(dst_addr);
235 int max_msb = (dst_addr & GENMASK(52, 48)) ? 51 : 47;
236 int bits_mapped = PAGE_SHIFT - 4;
237 unsigned long level_mask, prev_level_entry, *levels[4];
238 int this_level, index, level_lsb, level_msb;
240 dst_addr &= PAGE_MASK;
241 prev_level_entry = pte_val(pfn_pte(pfn, PAGE_KERNEL_ROX));
243 for (this_level = 3; this_level >= 0; this_level--) {
244 levels[this_level] = trans_alloc(info);
245 if (!levels[this_level])
248 level_lsb = ARM64_HW_PGTABLE_LEVEL_SHIFT(this_level);
249 level_msb = min(level_lsb + bits_mapped, max_msb);
250 level_mask = GENMASK_ULL(level_msb, level_lsb);
252 index = (dst_addr & level_mask) >> level_lsb;
253 *(levels[this_level] + index) = prev_level_entry;
255 pfn = virt_to_pfn(levels[this_level]);
256 prev_level_entry = pte_val(pfn_pte(pfn,
257 __pgprot(PMD_TYPE_TABLE)));
259 if (level_msb == max_msb)
263 *trans_ttbr0 = phys_to_ttbr(__pfn_to_phys(pfn));
264 *t0sz = TCR_T0SZ(max_msb + 1);
270 * Create a copy of the vector table so we can call HVC_SET_VECTORS or
271 * HVC_SOFT_RESTART from contexts where the table may be overwritten.
273 int trans_pgd_copy_el2_vectors(struct trans_pgd_info *info,
274 phys_addr_t *el2_vectors)
276 void *hyp_stub = trans_alloc(info);
280 *el2_vectors = virt_to_phys(hyp_stub);
281 memcpy(hyp_stub, &trans_pgd_stub_vectors, ARM64_VECTOR_TABLE_LEN);
282 caches_clean_inval_pou((unsigned long)hyp_stub,
283 (unsigned long)hyp_stub +
284 ARM64_VECTOR_TABLE_LEN);
285 dcache_clean_inval_poc((unsigned long)hyp_stub,
286 (unsigned long)hyp_stub +
287 ARM64_VECTOR_TABLE_LEN);