blob: f87a32484ea82c16eb45717b81b641024f915f17 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Andrey Ryabinin39d114d2015-10-12 18:52:58 +03002/*
3 * This file contains kasan initialization code for ARM64.
4 *
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Andrey Ryabinin39d114d2015-10-12 18:52:58 +03007 */
8
9#define pr_fmt(fmt) "kasan: " fmt
10#include <linux/kasan.h>
11#include <linux/kernel.h>
Ingo Molnar9164bb42017-02-04 01:20:53 +010012#include <linux/sched/task.h>
Andrey Ryabinin39d114d2015-10-12 18:52:58 +030013#include <linux/memblock.h>
14#include <linux/start_kernel.h>
Laura Abbott2077be62017-01-10 13:35:49 -080015#include <linux/mm.h>
Andrey Ryabinin39d114d2015-10-12 18:52:58 +030016
Mark Rutlandc1a88e92016-01-25 11:45:02 +000017#include <asm/mmu_context.h>
Ard Biesheuvelf9040772016-02-16 13:52:40 +010018#include <asm/kernel-pgtable.h>
Andrey Ryabinin39d114d2015-10-12 18:52:58 +030019#include <asm/page.h>
20#include <asm/pgalloc.h>
21#include <asm/pgtable.h>
Ard Biesheuvelf9040772016-02-16 13:52:40 +010022#include <asm/sections.h>
Andrey Ryabinin39d114d2015-10-12 18:52:58 +030023#include <asm/tlbflush.h>
24
25static pgd_t tmp_pg_dir[PTRS_PER_PGD] __initdata __aligned(PGD_SIZE);
26
Laura Abbott2077be62017-01-10 13:35:49 -080027/*
28 * The p*d_populate functions call virt_to_phys implicitly so they can't be used
29 * directly on kernel symbols (bm_p*d). All the early functions are called too
30 * early to use lm_alias so __p*d_populate functions must be used to populate
31 * with the physical address from __pa_symbol.
32 */
33
Will Deacone17d8022017-11-15 17:36:40 -080034static phys_addr_t __init kasan_alloc_zeroed_page(int node)
Andrey Ryabinin39d114d2015-10-12 18:52:58 +030035{
Mike Rapoporteb31d552018-10-30 15:08:04 -070036 void *p = memblock_alloc_try_nid(PAGE_SIZE, PAGE_SIZE,
Will Deacone17d8022017-11-15 17:36:40 -080037 __pa(MAX_DMA_ADDRESS),
Qian Caifed84c72018-12-28 00:36:29 -080038 MEMBLOCK_ALLOC_KASAN, node);
Mike Rapoport8a7f97b2019-03-11 23:30:31 -070039 if (!p)
40 panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%llx\n",
41 __func__, PAGE_SIZE, PAGE_SIZE, node,
42 __pa(MAX_DMA_ADDRESS));
43
Will Deacone17d8022017-11-15 17:36:40 -080044 return __pa(p);
45}
46
Andrey Konovalov080eb832018-12-28 00:30:09 -080047static phys_addr_t __init kasan_alloc_raw_page(int node)
48{
49 void *p = memblock_alloc_try_nid_raw(PAGE_SIZE, PAGE_SIZE,
50 __pa(MAX_DMA_ADDRESS),
51 MEMBLOCK_ALLOC_KASAN, node);
Mike Rapoport8a7f97b2019-03-11 23:30:31 -070052 if (!p)
53 panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%llx\n",
54 __func__, PAGE_SIZE, PAGE_SIZE, node,
55 __pa(MAX_DMA_ADDRESS));
56
Andrey Konovalov080eb832018-12-28 00:30:09 -080057 return __pa(p);
58}
59
Will Deacon20a004e2018-02-15 11:14:56 +000060static pte_t *__init kasan_pte_offset(pmd_t *pmdp, unsigned long addr, int node,
Will Deacone17d8022017-11-15 17:36:40 -080061 bool early)
62{
Will Deacon20a004e2018-02-15 11:14:56 +000063 if (pmd_none(READ_ONCE(*pmdp))) {
Andrey Konovalov9577dd72018-12-28 00:30:01 -080064 phys_addr_t pte_phys = early ?
65 __pa_symbol(kasan_early_shadow_pte)
66 : kasan_alloc_zeroed_page(node);
Will Deacon20a004e2018-02-15 11:14:56 +000067 __pmd_populate(pmdp, pte_phys, PMD_TYPE_TABLE);
Will Deacone17d8022017-11-15 17:36:40 -080068 }
69
Will Deacon20a004e2018-02-15 11:14:56 +000070 return early ? pte_offset_kimg(pmdp, addr)
71 : pte_offset_kernel(pmdp, addr);
Will Deacone17d8022017-11-15 17:36:40 -080072}
73
Will Deacon20a004e2018-02-15 11:14:56 +000074static pmd_t *__init kasan_pmd_offset(pud_t *pudp, unsigned long addr, int node,
Will Deacone17d8022017-11-15 17:36:40 -080075 bool early)
76{
Will Deacon20a004e2018-02-15 11:14:56 +000077 if (pud_none(READ_ONCE(*pudp))) {
Andrey Konovalov9577dd72018-12-28 00:30:01 -080078 phys_addr_t pmd_phys = early ?
79 __pa_symbol(kasan_early_shadow_pmd)
80 : kasan_alloc_zeroed_page(node);
Will Deacon20a004e2018-02-15 11:14:56 +000081 __pud_populate(pudp, pmd_phys, PMD_TYPE_TABLE);
Will Deacone17d8022017-11-15 17:36:40 -080082 }
83
Will Deacon20a004e2018-02-15 11:14:56 +000084 return early ? pmd_offset_kimg(pudp, addr) : pmd_offset(pudp, addr);
Will Deacone17d8022017-11-15 17:36:40 -080085}
86
Will Deacon20a004e2018-02-15 11:14:56 +000087static pud_t *__init kasan_pud_offset(pgd_t *pgdp, unsigned long addr, int node,
Will Deacone17d8022017-11-15 17:36:40 -080088 bool early)
89{
Will Deacon20a004e2018-02-15 11:14:56 +000090 if (pgd_none(READ_ONCE(*pgdp))) {
Andrey Konovalov9577dd72018-12-28 00:30:01 -080091 phys_addr_t pud_phys = early ?
92 __pa_symbol(kasan_early_shadow_pud)
93 : kasan_alloc_zeroed_page(node);
Will Deacon20a004e2018-02-15 11:14:56 +000094 __pgd_populate(pgdp, pud_phys, PMD_TYPE_TABLE);
Will Deacone17d8022017-11-15 17:36:40 -080095 }
96
Will Deacon20a004e2018-02-15 11:14:56 +000097 return early ? pud_offset_kimg(pgdp, addr) : pud_offset(pgdp, addr);
Will Deacone17d8022017-11-15 17:36:40 -080098}
99
Will Deacon20a004e2018-02-15 11:14:56 +0000100static void __init kasan_pte_populate(pmd_t *pmdp, unsigned long addr,
Will Deacone17d8022017-11-15 17:36:40 -0800101 unsigned long end, int node, bool early)
102{
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300103 unsigned long next;
Will Deacon20a004e2018-02-15 11:14:56 +0000104 pte_t *ptep = kasan_pte_offset(pmdp, addr, node, early);
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300105
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300106 do {
Andrey Konovalov9577dd72018-12-28 00:30:01 -0800107 phys_addr_t page_phys = early ?
108 __pa_symbol(kasan_early_shadow_page)
Andrey Konovalov080eb832018-12-28 00:30:09 -0800109 : kasan_alloc_raw_page(node);
110 if (!early)
111 memset(__va(page_phys), KASAN_SHADOW_INIT, PAGE_SIZE);
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300112 next = addr + PAGE_SIZE;
Will Deacon20a004e2018-02-15 11:14:56 +0000113 set_pte(ptep, pfn_pte(__phys_to_pfn(page_phys), PAGE_KERNEL));
114 } while (ptep++, addr = next, addr != end && pte_none(READ_ONCE(*ptep)));
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300115}
116
Will Deacon20a004e2018-02-15 11:14:56 +0000117static void __init kasan_pmd_populate(pud_t *pudp, unsigned long addr,
Will Deacone17d8022017-11-15 17:36:40 -0800118 unsigned long end, int node, bool early)
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300119{
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300120 unsigned long next;
Will Deacon20a004e2018-02-15 11:14:56 +0000121 pmd_t *pmdp = kasan_pmd_offset(pudp, addr, node, early);
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300122
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300123 do {
124 next = pmd_addr_end(addr, end);
Will Deacon20a004e2018-02-15 11:14:56 +0000125 kasan_pte_populate(pmdp, addr, next, node, early);
126 } while (pmdp++, addr = next, addr != end && pmd_none(READ_ONCE(*pmdp)));
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300127}
128
Will Deacon20a004e2018-02-15 11:14:56 +0000129static void __init kasan_pud_populate(pgd_t *pgdp, unsigned long addr,
Will Deacone17d8022017-11-15 17:36:40 -0800130 unsigned long end, int node, bool early)
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300131{
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300132 unsigned long next;
Will Deacon20a004e2018-02-15 11:14:56 +0000133 pud_t *pudp = kasan_pud_offset(pgdp, addr, node, early);
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300134
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300135 do {
136 next = pud_addr_end(addr, end);
Will Deacon20a004e2018-02-15 11:14:56 +0000137 kasan_pmd_populate(pudp, addr, next, node, early);
138 } while (pudp++, addr = next, addr != end && pud_none(READ_ONCE(*pudp)));
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300139}
140
Will Deacone17d8022017-11-15 17:36:40 -0800141static void __init kasan_pgd_populate(unsigned long addr, unsigned long end,
142 int node, bool early)
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300143{
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300144 unsigned long next;
Will Deacon20a004e2018-02-15 11:14:56 +0000145 pgd_t *pgdp;
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300146
Will Deacon20a004e2018-02-15 11:14:56 +0000147 pgdp = pgd_offset_k(addr);
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300148 do {
149 next = pgd_addr_end(addr, end);
Will Deacon20a004e2018-02-15 11:14:56 +0000150 kasan_pud_populate(pgdp, addr, next, node, early);
151 } while (pgdp++, addr = next, addr != end);
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300152}
153
Will Deacone17d8022017-11-15 17:36:40 -0800154/* The early shadow maps everything to a single page of zeroes */
Will Deacon83040122015-10-13 14:01:06 +0100155asmlinkage void __init kasan_early_init(void)
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300156{
Andrey Konovalov917538e2018-02-06 15:36:44 -0800157 BUILD_BUG_ON(KASAN_SHADOW_OFFSET !=
158 KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT)));
Steve Capper90ec95c2019-08-07 16:55:17 +0100159 BUILD_BUG_ON(!IS_ALIGNED(_KASAN_SHADOW_START(VA_BITS), PGDIR_SIZE));
160 BUILD_BUG_ON(!IS_ALIGNED(_KASAN_SHADOW_START(VA_BITS_MIN), PGDIR_SIZE));
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300161 BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, PGDIR_SIZE));
Will Deacone17d8022017-11-15 17:36:40 -0800162 kasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE,
163 true);
164}
165
166/* Set up full kasan mappings, ensuring that the mapped pages are zeroed */
167static void __init kasan_map_populate(unsigned long start, unsigned long end,
168 int node)
169{
170 kasan_pgd_populate(start & PAGE_MASK, PAGE_ALIGN(end), node, false);
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300171}
172
Mark Rutland068a17a2016-01-25 11:45:12 +0000173/*
174 * Copy the current shadow region into a new pgdir.
175 */
176void __init kasan_copy_shadow(pgd_t *pgdir)
177{
Will Deacon20a004e2018-02-15 11:14:56 +0000178 pgd_t *pgdp, *pgdp_new, *pgdp_end;
Mark Rutland068a17a2016-01-25 11:45:12 +0000179
Will Deacon20a004e2018-02-15 11:14:56 +0000180 pgdp = pgd_offset_k(KASAN_SHADOW_START);
181 pgdp_end = pgd_offset_k(KASAN_SHADOW_END);
182 pgdp_new = pgd_offset_raw(pgdir, KASAN_SHADOW_START);
Mark Rutland068a17a2016-01-25 11:45:12 +0000183 do {
Will Deacon20a004e2018-02-15 11:14:56 +0000184 set_pgd(pgdp_new, READ_ONCE(*pgdp));
185 } while (pgdp++, pgdp_new++, pgdp != pgdp_end);
Mark Rutland068a17a2016-01-25 11:45:12 +0000186}
187
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300188static void __init clear_pgds(unsigned long start,
189 unsigned long end)
190{
191 /*
192 * Remove references to kasan page tables from
193 * swapper_pg_dir. pgd_clear() can't be used
194 * here because it's nop on 2,3-level pagetable setups
195 */
196 for (; start < end; start += PGDIR_SIZE)
197 set_pgd(pgd_offset_k(start), __pgd(0));
198}
199
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300200void __init kasan_init(void)
201{
Ard Biesheuvelf9040772016-02-16 13:52:40 +0100202 u64 kimg_shadow_start, kimg_shadow_end;
Ard Biesheuvelf80fb3a2016-01-26 14:12:01 +0100203 u64 mod_shadow_start, mod_shadow_end;
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300204 struct memblock_region *reg;
Ard Biesheuvel7b1af972016-01-11 14:50:21 +0100205 int i;
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300206
Will Deacone17d8022017-11-15 17:36:40 -0800207 kimg_shadow_start = (u64)kasan_mem_to_shadow(_text) & PAGE_MASK;
208 kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(_end));
Ard Biesheuvelf9040772016-02-16 13:52:40 +0100209
Ard Biesheuvelf80fb3a2016-01-26 14:12:01 +0100210 mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
211 mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
212
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300213 /*
214 * We are going to perform proper setup of shadow memory.
Kyrylo Tkachov0293c8b2018-10-04 17:06:46 +0100215 * At first we should unmap early shadow (clear_pgds() call below).
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300216 * However, instrumented code couldn't execute without shadow memory.
217 * tmp_pg_dir used to keep early shadow mapped until full shadow
218 * setup will be finished.
219 */
220 memcpy(tmp_pg_dir, swapper_pg_dir, sizeof(tmp_pg_dir));
Mark Rutlandc1a88e92016-01-25 11:45:02 +0000221 dsb(ishst);
Laura Abbott2077be62017-01-10 13:35:49 -0800222 cpu_replace_ttbr1(lm_alias(tmp_pg_dir));
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300223
224 clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END);
225
Will Deacone17d8022017-11-15 17:36:40 -0800226 kasan_map_populate(kimg_shadow_start, kimg_shadow_end,
Mark Rutland800cb2e2018-04-16 14:44:41 +0100227 early_pfn_to_nid(virt_to_pfn(lm_alias(_text))));
Ard Biesheuvelf9040772016-02-16 13:52:40 +0100228
Mark Rutland77ad4ce2019-08-14 14:28:48 +0100229 kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END),
Steve Capper14c127c2019-08-07 16:55:14 +0100230 (void *)mod_shadow_start);
Andrey Konovalov9577dd72018-12-28 00:30:01 -0800231 kasan_populate_early_shadow((void *)kimg_shadow_end,
Steve Capper14c127c2019-08-07 16:55:14 +0100232 (void *)KASAN_SHADOW_END);
Ard Biesheuvelf80fb3a2016-01-26 14:12:01 +0100233
234 if (kimg_shadow_start > mod_shadow_end)
Andrey Konovalov9577dd72018-12-28 00:30:01 -0800235 kasan_populate_early_shadow((void *)mod_shadow_end,
236 (void *)kimg_shadow_start);
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300237
238 for_each_memblock(memory, reg) {
239 void *start = (void *)__phys_to_virt(reg->base);
240 void *end = (void *)__phys_to_virt(reg->base + reg->size);
241
242 if (start >= end)
243 break;
244
Will Deacone17d8022017-11-15 17:36:40 -0800245 kasan_map_populate((unsigned long)kasan_mem_to_shadow(start),
246 (unsigned long)kasan_mem_to_shadow(end),
Mark Rutland800cb2e2018-04-16 14:44:41 +0100247 early_pfn_to_nid(virt_to_pfn(start)));
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300248 }
249
Ard Biesheuvel7b1af972016-01-11 14:50:21 +0100250 /*
Andrey Konovalov9577dd72018-12-28 00:30:01 -0800251 * KAsan may reuse the contents of kasan_early_shadow_pte directly,
252 * so we should make sure that it maps the zero page read-only.
Ard Biesheuvel7b1af972016-01-11 14:50:21 +0100253 */
254 for (i = 0; i < PTRS_PER_PTE; i++)
Andrey Konovalov9577dd72018-12-28 00:30:01 -0800255 set_pte(&kasan_early_shadow_pte[i],
256 pfn_pte(sym_to_pfn(kasan_early_shadow_page),
257 PAGE_KERNEL_RO));
Ard Biesheuvel7b1af972016-01-11 14:50:21 +0100258
Andrey Konovalov080eb832018-12-28 00:30:09 -0800259 memset(kasan_early_shadow_page, KASAN_SHADOW_INIT, PAGE_SIZE);
Laura Abbott2077be62017-01-10 13:35:49 -0800260 cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300261
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300262 /* At this point kasan is fully initialized. Enable error messages */
263 init_task.kasan_depth = 0;
264 pr_info("KernelAddressSanitizer initialized\n");
265}