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 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
26 static pgd_t tmp_pg_dir[PTRS_PER_PGD] __initdata __aligned(PGD_SIZE);
29 * The p*d_populate functions call virt_to_phys implicitly so they can't be used
30 * directly on kernel symbols (bm_p*d). All the early functions are called too
31 * early to use lm_alias so __p*d_populate functions must be used to populate
32 * with the physical address from __pa_symbol.
35 static phys_addr_t __init kasan_alloc_zeroed_page(int node)
37 void *p = memblock_alloc_try_nid(PAGE_SIZE, PAGE_SIZE,
38 __pa(MAX_DMA_ADDRESS),
39 MEMBLOCK_ALLOC_NOLEAKTRACE, node);
41 panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%llx\n",
42 __func__, PAGE_SIZE, PAGE_SIZE, node,
43 __pa(MAX_DMA_ADDRESS));
48 static phys_addr_t __init kasan_alloc_raw_page(int node)
50 void *p = memblock_alloc_try_nid_raw(PAGE_SIZE, PAGE_SIZE,
51 __pa(MAX_DMA_ADDRESS),
52 MEMBLOCK_ALLOC_NOLEAKTRACE,
55 panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%llx\n",
56 __func__, PAGE_SIZE, PAGE_SIZE, node,
57 __pa(MAX_DMA_ADDRESS));
62 static pte_t *__init kasan_pte_offset(pmd_t *pmdp, unsigned long addr, int node,
65 if (pmd_none(READ_ONCE(*pmdp))) {
66 phys_addr_t pte_phys = early ?
67 __pa_symbol(kasan_early_shadow_pte)
68 : kasan_alloc_zeroed_page(node);
69 __pmd_populate(pmdp, pte_phys, PMD_TYPE_TABLE);
72 return early ? pte_offset_kimg(pmdp, addr)
73 : pte_offset_kernel(pmdp, addr);
76 static pmd_t *__init kasan_pmd_offset(pud_t *pudp, unsigned long addr, int node,
79 if (pud_none(READ_ONCE(*pudp))) {
80 phys_addr_t pmd_phys = early ?
81 __pa_symbol(kasan_early_shadow_pmd)
82 : kasan_alloc_zeroed_page(node);
83 __pud_populate(pudp, pmd_phys, PUD_TYPE_TABLE);
86 return early ? pmd_offset_kimg(pudp, addr) : pmd_offset(pudp, addr);
89 static pud_t *__init kasan_pud_offset(p4d_t *p4dp, unsigned long addr, int node,
92 if (p4d_none(READ_ONCE(*p4dp))) {
93 phys_addr_t pud_phys = early ?
94 __pa_symbol(kasan_early_shadow_pud)
95 : kasan_alloc_zeroed_page(node);
96 __p4d_populate(p4dp, pud_phys, P4D_TYPE_TABLE);
99 return early ? pud_offset_kimg(p4dp, addr) : pud_offset(p4dp, addr);
102 static void __init kasan_pte_populate(pmd_t *pmdp, unsigned long addr,
103 unsigned long end, int node, bool early)
106 pte_t *ptep = kasan_pte_offset(pmdp, addr, node, early);
109 phys_addr_t page_phys = early ?
110 __pa_symbol(kasan_early_shadow_page)
111 : kasan_alloc_raw_page(node);
113 memset(__va(page_phys), KASAN_SHADOW_INIT, PAGE_SIZE);
114 next = addr + PAGE_SIZE;
115 set_pte(ptep, pfn_pte(__phys_to_pfn(page_phys), PAGE_KERNEL));
116 } while (ptep++, addr = next, addr != end && pte_none(READ_ONCE(*ptep)));
119 static void __init kasan_pmd_populate(pud_t *pudp, unsigned long addr,
120 unsigned long end, int node, bool early)
123 pmd_t *pmdp = kasan_pmd_offset(pudp, addr, node, early);
126 next = pmd_addr_end(addr, end);
127 kasan_pte_populate(pmdp, addr, next, node, early);
128 } while (pmdp++, addr = next, addr != end && pmd_none(READ_ONCE(*pmdp)));
131 static void __init kasan_pud_populate(p4d_t *p4dp, unsigned long addr,
132 unsigned long end, int node, bool early)
135 pud_t *pudp = kasan_pud_offset(p4dp, addr, node, early);
138 next = pud_addr_end(addr, end);
139 kasan_pmd_populate(pudp, addr, next, node, early);
140 } while (pudp++, addr = next, addr != end && pud_none(READ_ONCE(*pudp)));
143 static void __init kasan_p4d_populate(pgd_t *pgdp, unsigned long addr,
144 unsigned long end, int node, bool early)
147 p4d_t *p4dp = p4d_offset(pgdp, addr);
150 next = p4d_addr_end(addr, end);
151 kasan_pud_populate(p4dp, addr, next, node, early);
152 } while (p4dp++, addr = next, addr != end);
155 static void __init kasan_pgd_populate(unsigned long addr, unsigned long end,
156 int node, bool early)
161 pgdp = pgd_offset_k(addr);
163 next = pgd_addr_end(addr, end);
164 kasan_p4d_populate(pgdp, addr, next, node, early);
165 } while (pgdp++, addr = next, addr != end);
168 /* The early shadow maps everything to a single page of zeroes */
169 asmlinkage void __init kasan_early_init(void)
171 BUILD_BUG_ON(KASAN_SHADOW_OFFSET !=
172 KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT)));
174 * We cannot check the actual value of KASAN_SHADOW_START during build,
175 * as it depends on vabits_actual. As a best-effort approach, check
176 * potential values calculated based on VA_BITS and VA_BITS_MIN.
178 BUILD_BUG_ON(!IS_ALIGNED(_KASAN_SHADOW_START(VA_BITS), PGDIR_SIZE));
179 BUILD_BUG_ON(!IS_ALIGNED(_KASAN_SHADOW_START(VA_BITS_MIN), PGDIR_SIZE));
180 BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, PGDIR_SIZE));
181 kasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE,
185 /* Set up full kasan mappings, ensuring that the mapped pages are zeroed */
186 static void __init kasan_map_populate(unsigned long start, unsigned long end,
189 kasan_pgd_populate(start & PAGE_MASK, PAGE_ALIGN(end), node, false);
192 static void __init clear_pgds(unsigned long start,
196 * Remove references to kasan page tables from
197 * swapper_pg_dir. pgd_clear() can't be used
198 * here because it's nop on 2,3-level pagetable setups
200 for (; start < end; start += PGDIR_SIZE)
201 set_pgd(pgd_offset_k(start), __pgd(0));
204 static void __init kasan_init_shadow(void)
206 u64 kimg_shadow_start, kimg_shadow_end;
207 u64 mod_shadow_start;
208 u64 vmalloc_shadow_end;
209 phys_addr_t pa_start, pa_end;
212 kimg_shadow_start = (u64)kasan_mem_to_shadow(KERNEL_START) & PAGE_MASK;
213 kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(KERNEL_END));
215 mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
217 vmalloc_shadow_end = (u64)kasan_mem_to_shadow((void *)VMALLOC_END);
220 * We are going to perform proper setup of shadow memory.
221 * At first we should unmap early shadow (clear_pgds() call below).
222 * However, instrumented code couldn't execute without shadow memory.
223 * tmp_pg_dir used to keep early shadow mapped until full shadow
224 * setup will be finished.
226 memcpy(tmp_pg_dir, swapper_pg_dir, sizeof(tmp_pg_dir));
228 cpu_replace_ttbr1(lm_alias(tmp_pg_dir));
230 clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END);
232 kasan_map_populate(kimg_shadow_start, kimg_shadow_end,
233 early_pfn_to_nid(virt_to_pfn(lm_alias(KERNEL_START))));
235 kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END),
236 (void *)mod_shadow_start);
238 BUILD_BUG_ON(VMALLOC_START != MODULES_END);
239 kasan_populate_early_shadow((void *)vmalloc_shadow_end,
240 (void *)KASAN_SHADOW_END);
242 for_each_mem_range(i, &pa_start, &pa_end) {
243 void *start = (void *)__phys_to_virt(pa_start);
244 void *end = (void *)__phys_to_virt(pa_end);
249 kasan_map_populate((unsigned long)kasan_mem_to_shadow(start),
250 (unsigned long)kasan_mem_to_shadow(end),
251 early_pfn_to_nid(virt_to_pfn(start)));
255 * KAsan may reuse the contents of kasan_early_shadow_pte directly,
256 * so we should make sure that it maps the zero page read-only.
258 for (i = 0; i < PTRS_PER_PTE; i++)
259 set_pte(&kasan_early_shadow_pte[i],
260 pfn_pte(sym_to_pfn(kasan_early_shadow_page),
263 memset(kasan_early_shadow_page, KASAN_SHADOW_INIT, PAGE_SIZE);
264 cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
267 static void __init kasan_init_depth(void)
269 init_task.kasan_depth = 0;
272 #ifdef CONFIG_KASAN_VMALLOC
273 void __init kasan_populate_early_vm_area_shadow(void *start, unsigned long size)
275 unsigned long shadow_start, shadow_end;
277 if (!is_vmalloc_or_module_addr(start))
280 shadow_start = (unsigned long)kasan_mem_to_shadow(start);
281 shadow_start = ALIGN_DOWN(shadow_start, PAGE_SIZE);
282 shadow_end = (unsigned long)kasan_mem_to_shadow(start + size);
283 shadow_end = ALIGN(shadow_end, PAGE_SIZE);
284 kasan_map_populate(shadow_start, shadow_end, NUMA_NO_NODE);
288 void __init kasan_init(void)
292 #if defined(CONFIG_KASAN_GENERIC)
294 * Generic KASAN is now fully initialized.
295 * Software and Hardware Tag-Based modes still require
296 * kasan_init_sw_tags() and kasan_init_hw_tags() correspondingly.
298 pr_info("KernelAddressSanitizer initialized (generic)\n");
302 #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */