1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file contains kasan initialization code for ARM64.
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
9 #define pr_fmt(fmt) "kasan: " fmt
10 #include <linux/kasan.h>
11 #include <linux/kernel.h>
12 #include <linux/sched/task.h>
13 #include <linux/memblock.h>
14 #include <linux/start_kernel.h>
17 #include <asm/mmu_context.h>
18 #include <asm/kernel-pgtable.h>
20 #include <asm/pgalloc.h>
21 #include <asm/sections.h>
22 #include <asm/tlbflush.h>
24 static pgd_t tmp_pg_dir[PTRS_PER_PGD] __initdata __aligned(PGD_SIZE);
27 * The p*d_populate functions call virt_to_phys implicitly so they can't be used
28 * directly on kernel symbols (bm_p*d). All the early functions are called too
29 * early to use lm_alias so __p*d_populate functions must be used to populate
30 * with the physical address from __pa_symbol.
33 static phys_addr_t __init kasan_alloc_zeroed_page(int node)
35 void *p = memblock_alloc_try_nid(PAGE_SIZE, PAGE_SIZE,
36 __pa(MAX_DMA_ADDRESS),
37 MEMBLOCK_ALLOC_KASAN, node);
39 panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%llx\n",
40 __func__, PAGE_SIZE, PAGE_SIZE, node,
41 __pa(MAX_DMA_ADDRESS));
46 static phys_addr_t __init kasan_alloc_raw_page(int node)
48 void *p = memblock_alloc_try_nid_raw(PAGE_SIZE, PAGE_SIZE,
49 __pa(MAX_DMA_ADDRESS),
50 MEMBLOCK_ALLOC_KASAN, node);
52 panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%llx\n",
53 __func__, PAGE_SIZE, PAGE_SIZE, node,
54 __pa(MAX_DMA_ADDRESS));
59 static pte_t *__init kasan_pte_offset(pmd_t *pmdp, unsigned long addr, int node,
62 if (pmd_none(READ_ONCE(*pmdp))) {
63 phys_addr_t pte_phys = early ?
64 __pa_symbol(kasan_early_shadow_pte)
65 : kasan_alloc_zeroed_page(node);
66 __pmd_populate(pmdp, pte_phys, PMD_TYPE_TABLE);
69 return early ? pte_offset_kimg(pmdp, addr)
70 : pte_offset_kernel(pmdp, addr);
73 static pmd_t *__init kasan_pmd_offset(pud_t *pudp, unsigned long addr, int node,
76 if (pud_none(READ_ONCE(*pudp))) {
77 phys_addr_t pmd_phys = early ?
78 __pa_symbol(kasan_early_shadow_pmd)
79 : kasan_alloc_zeroed_page(node);
80 __pud_populate(pudp, pmd_phys, PMD_TYPE_TABLE);
83 return early ? pmd_offset_kimg(pudp, addr) : pmd_offset(pudp, addr);
86 static pud_t *__init kasan_pud_offset(p4d_t *p4dp, unsigned long addr, int node,
89 if (p4d_none(READ_ONCE(*p4dp))) {
90 phys_addr_t pud_phys = early ?
91 __pa_symbol(kasan_early_shadow_pud)
92 : kasan_alloc_zeroed_page(node);
93 __p4d_populate(p4dp, pud_phys, PMD_TYPE_TABLE);
96 return early ? pud_offset_kimg(p4dp, addr) : pud_offset(p4dp, addr);
99 static void __init kasan_pte_populate(pmd_t *pmdp, unsigned long addr,
100 unsigned long end, int node, bool early)
103 pte_t *ptep = kasan_pte_offset(pmdp, addr, node, early);
106 phys_addr_t page_phys = early ?
107 __pa_symbol(kasan_early_shadow_page)
108 : kasan_alloc_raw_page(node);
110 memset(__va(page_phys), KASAN_SHADOW_INIT, PAGE_SIZE);
111 next = addr + PAGE_SIZE;
112 set_pte(ptep, pfn_pte(__phys_to_pfn(page_phys), PAGE_KERNEL));
113 } while (ptep++, addr = next, addr != end && pte_none(READ_ONCE(*ptep)));
116 static void __init kasan_pmd_populate(pud_t *pudp, unsigned long addr,
117 unsigned long end, int node, bool early)
120 pmd_t *pmdp = kasan_pmd_offset(pudp, addr, node, early);
123 next = pmd_addr_end(addr, end);
124 kasan_pte_populate(pmdp, addr, next, node, early);
125 } while (pmdp++, addr = next, addr != end && pmd_none(READ_ONCE(*pmdp)));
128 static void __init kasan_pud_populate(p4d_t *p4dp, unsigned long addr,
129 unsigned long end, int node, bool early)
132 pud_t *pudp = kasan_pud_offset(p4dp, addr, node, early);
135 next = pud_addr_end(addr, end);
136 kasan_pmd_populate(pudp, addr, next, node, early);
137 } while (pudp++, addr = next, addr != end && pud_none(READ_ONCE(*pudp)));
140 static void __init kasan_p4d_populate(pgd_t *pgdp, unsigned long addr,
141 unsigned long end, int node, bool early)
144 p4d_t *p4dp = p4d_offset(pgdp, addr);
147 next = p4d_addr_end(addr, end);
148 kasan_pud_populate(p4dp, addr, next, node, early);
149 } while (p4dp++, addr = next, addr != end);
152 static void __init kasan_pgd_populate(unsigned long addr, unsigned long end,
153 int node, bool early)
158 pgdp = pgd_offset_k(addr);
160 next = pgd_addr_end(addr, end);
161 kasan_p4d_populate(pgdp, addr, next, node, early);
162 } while (pgdp++, addr = next, addr != end);
165 /* The early shadow maps everything to a single page of zeroes */
166 asmlinkage void __init kasan_early_init(void)
168 BUILD_BUG_ON(KASAN_SHADOW_OFFSET !=
169 KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT)));
170 BUILD_BUG_ON(!IS_ALIGNED(_KASAN_SHADOW_START(VA_BITS), PGDIR_SIZE));
171 BUILD_BUG_ON(!IS_ALIGNED(_KASAN_SHADOW_START(VA_BITS_MIN), PGDIR_SIZE));
172 BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, PGDIR_SIZE));
173 kasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE,
177 /* Set up full kasan mappings, ensuring that the mapped pages are zeroed */
178 static void __init kasan_map_populate(unsigned long start, unsigned long end,
181 kasan_pgd_populate(start & PAGE_MASK, PAGE_ALIGN(end), node, false);
185 * Copy the current shadow region into a new pgdir.
187 void __init kasan_copy_shadow(pgd_t *pgdir)
189 pgd_t *pgdp, *pgdp_new, *pgdp_end;
191 pgdp = pgd_offset_k(KASAN_SHADOW_START);
192 pgdp_end = pgd_offset_k(KASAN_SHADOW_END);
193 pgdp_new = pgd_offset_pgd(pgdir, KASAN_SHADOW_START);
195 set_pgd(pgdp_new, READ_ONCE(*pgdp));
196 } while (pgdp++, pgdp_new++, pgdp != pgdp_end);
199 static void __init clear_pgds(unsigned long start,
203 * Remove references to kasan page tables from
204 * swapper_pg_dir. pgd_clear() can't be used
205 * here because it's nop on 2,3-level pagetable setups
207 for (; start < end; start += PGDIR_SIZE)
208 set_pgd(pgd_offset_k(start), __pgd(0));
211 void __init kasan_init(void)
213 u64 kimg_shadow_start, kimg_shadow_end;
214 u64 mod_shadow_start, mod_shadow_end;
215 struct memblock_region *reg;
218 kimg_shadow_start = (u64)kasan_mem_to_shadow(_text) & PAGE_MASK;
219 kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(_end));
221 mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
222 mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
225 * We are going to perform proper setup of shadow memory.
226 * At first we should unmap early shadow (clear_pgds() call below).
227 * However, instrumented code couldn't execute without shadow memory.
228 * tmp_pg_dir used to keep early shadow mapped until full shadow
229 * setup will be finished.
231 memcpy(tmp_pg_dir, swapper_pg_dir, sizeof(tmp_pg_dir));
233 cpu_replace_ttbr1(lm_alias(tmp_pg_dir));
235 clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END);
237 kasan_map_populate(kimg_shadow_start, kimg_shadow_end,
238 early_pfn_to_nid(virt_to_pfn(lm_alias(_text))));
240 kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END),
241 (void *)mod_shadow_start);
242 kasan_populate_early_shadow((void *)kimg_shadow_end,
243 (void *)KASAN_SHADOW_END);
245 if (kimg_shadow_start > mod_shadow_end)
246 kasan_populate_early_shadow((void *)mod_shadow_end,
247 (void *)kimg_shadow_start);
249 for_each_memblock(memory, reg) {
250 void *start = (void *)__phys_to_virt(reg->base);
251 void *end = (void *)__phys_to_virt(reg->base + reg->size);
256 kasan_map_populate((unsigned long)kasan_mem_to_shadow(start),
257 (unsigned long)kasan_mem_to_shadow(end),
258 early_pfn_to_nid(virt_to_pfn(start)));
262 * KAsan may reuse the contents of kasan_early_shadow_pte directly,
263 * so we should make sure that it maps the zero page read-only.
265 for (i = 0; i < PTRS_PER_PTE; i++)
266 set_pte(&kasan_early_shadow_pte[i],
267 pfn_pte(sym_to_pfn(kasan_early_shadow_page),
270 memset(kasan_early_shadow_page, KASAN_SHADOW_INIT, PAGE_SIZE);
271 cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
273 /* At this point kasan is fully initialized. Enable error messages */
274 init_task.kasan_depth = 0;
275 pr_info("KernelAddressSanitizer initialized\n");