blob: 4b55b15707a33e5a5c7c202977845f86e847bda5 [file] [log] [blame]
Andrey Ryabinin39d114d2015-10-12 18:52:58 +03001/*
2 * This file contains kasan initialization code for ARM64.
3 *
4 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
5 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 */
12
13#define pr_fmt(fmt) "kasan: " fmt
14#include <linux/kasan.h>
15#include <linux/kernel.h>
Ingo Molnar9164bb42017-02-04 01:20:53 +010016#include <linux/sched/task.h>
Andrey Ryabinin39d114d2015-10-12 18:52:58 +030017#include <linux/memblock.h>
18#include <linux/start_kernel.h>
Laura Abbott2077be62017-01-10 13:35:49 -080019#include <linux/mm.h>
Andrey Ryabinin39d114d2015-10-12 18:52:58 +030020
Mark Rutlandc1a88e92016-01-25 11:45:02 +000021#include <asm/mmu_context.h>
Ard Biesheuvelf9040772016-02-16 13:52:40 +010022#include <asm/kernel-pgtable.h>
Andrey Ryabinin39d114d2015-10-12 18:52:58 +030023#include <asm/page.h>
24#include <asm/pgalloc.h>
25#include <asm/pgtable.h>
Ard Biesheuvelf9040772016-02-16 13:52:40 +010026#include <asm/sections.h>
Andrey Ryabinin39d114d2015-10-12 18:52:58 +030027#include <asm/tlbflush.h>
28
29static pgd_t tmp_pg_dir[PTRS_PER_PGD] __initdata __aligned(PGD_SIZE);
30
Laura Abbott2077be62017-01-10 13:35:49 -080031/*
32 * The p*d_populate functions call virt_to_phys implicitly so they can't be used
33 * directly on kernel symbols (bm_p*d). All the early functions are called too
34 * early to use lm_alias so __p*d_populate functions must be used to populate
35 * with the physical address from __pa_symbol.
36 */
37
Will Deacone17d8022017-11-15 17:36:40 -080038static phys_addr_t __init kasan_alloc_zeroed_page(int node)
Andrey Ryabinin39d114d2015-10-12 18:52:58 +030039{
Mike Rapoporteb31d552018-10-30 15:08:04 -070040 void *p = memblock_alloc_try_nid(PAGE_SIZE, PAGE_SIZE,
Will Deacone17d8022017-11-15 17:36:40 -080041 __pa(MAX_DMA_ADDRESS),
Qian Caifed84c72018-12-28 00:36:29 -080042 MEMBLOCK_ALLOC_KASAN, node);
Will Deacone17d8022017-11-15 17:36:40 -080043 return __pa(p);
44}
45
Andrey Konovalov080eb832018-12-28 00:30:09 -080046static phys_addr_t __init kasan_alloc_raw_page(int node)
47{
48 void *p = memblock_alloc_try_nid_raw(PAGE_SIZE, PAGE_SIZE,
49 __pa(MAX_DMA_ADDRESS),
50 MEMBLOCK_ALLOC_KASAN, node);
51 return __pa(p);
52}
53
Will Deacon20a004e2018-02-15 11:14:56 +000054static pte_t *__init kasan_pte_offset(pmd_t *pmdp, unsigned long addr, int node,
Will Deacone17d8022017-11-15 17:36:40 -080055 bool early)
56{
Will Deacon20a004e2018-02-15 11:14:56 +000057 if (pmd_none(READ_ONCE(*pmdp))) {
Andrey Konovalov9577dd72018-12-28 00:30:01 -080058 phys_addr_t pte_phys = early ?
59 __pa_symbol(kasan_early_shadow_pte)
60 : kasan_alloc_zeroed_page(node);
Will Deacon20a004e2018-02-15 11:14:56 +000061 __pmd_populate(pmdp, pte_phys, PMD_TYPE_TABLE);
Will Deacone17d8022017-11-15 17:36:40 -080062 }
63
Will Deacon20a004e2018-02-15 11:14:56 +000064 return early ? pte_offset_kimg(pmdp, addr)
65 : pte_offset_kernel(pmdp, addr);
Will Deacone17d8022017-11-15 17:36:40 -080066}
67
Will Deacon20a004e2018-02-15 11:14:56 +000068static pmd_t *__init kasan_pmd_offset(pud_t *pudp, unsigned long addr, int node,
Will Deacone17d8022017-11-15 17:36:40 -080069 bool early)
70{
Will Deacon20a004e2018-02-15 11:14:56 +000071 if (pud_none(READ_ONCE(*pudp))) {
Andrey Konovalov9577dd72018-12-28 00:30:01 -080072 phys_addr_t pmd_phys = early ?
73 __pa_symbol(kasan_early_shadow_pmd)
74 : kasan_alloc_zeroed_page(node);
Will Deacon20a004e2018-02-15 11:14:56 +000075 __pud_populate(pudp, pmd_phys, PMD_TYPE_TABLE);
Will Deacone17d8022017-11-15 17:36:40 -080076 }
77
Will Deacon20a004e2018-02-15 11:14:56 +000078 return early ? pmd_offset_kimg(pudp, addr) : pmd_offset(pudp, addr);
Will Deacone17d8022017-11-15 17:36:40 -080079}
80
Will Deacon20a004e2018-02-15 11:14:56 +000081static pud_t *__init kasan_pud_offset(pgd_t *pgdp, unsigned long addr, int node,
Will Deacone17d8022017-11-15 17:36:40 -080082 bool early)
83{
Will Deacon20a004e2018-02-15 11:14:56 +000084 if (pgd_none(READ_ONCE(*pgdp))) {
Andrey Konovalov9577dd72018-12-28 00:30:01 -080085 phys_addr_t pud_phys = early ?
86 __pa_symbol(kasan_early_shadow_pud)
87 : kasan_alloc_zeroed_page(node);
Will Deacon20a004e2018-02-15 11:14:56 +000088 __pgd_populate(pgdp, pud_phys, PMD_TYPE_TABLE);
Will Deacone17d8022017-11-15 17:36:40 -080089 }
90
Will Deacon20a004e2018-02-15 11:14:56 +000091 return early ? pud_offset_kimg(pgdp, addr) : pud_offset(pgdp, addr);
Will Deacone17d8022017-11-15 17:36:40 -080092}
93
Will Deacon20a004e2018-02-15 11:14:56 +000094static void __init kasan_pte_populate(pmd_t *pmdp, unsigned long addr,
Will Deacone17d8022017-11-15 17:36:40 -080095 unsigned long end, int node, bool early)
96{
Andrey Ryabinin39d114d2015-10-12 18:52:58 +030097 unsigned long next;
Will Deacon20a004e2018-02-15 11:14:56 +000098 pte_t *ptep = kasan_pte_offset(pmdp, addr, node, early);
Andrey Ryabinin39d114d2015-10-12 18:52:58 +030099
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300100 do {
Andrey Konovalov9577dd72018-12-28 00:30:01 -0800101 phys_addr_t page_phys = early ?
102 __pa_symbol(kasan_early_shadow_page)
Andrey Konovalov080eb832018-12-28 00:30:09 -0800103 : kasan_alloc_raw_page(node);
104 if (!early)
105 memset(__va(page_phys), KASAN_SHADOW_INIT, PAGE_SIZE);
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300106 next = addr + PAGE_SIZE;
Will Deacon20a004e2018-02-15 11:14:56 +0000107 set_pte(ptep, pfn_pte(__phys_to_pfn(page_phys), PAGE_KERNEL));
108 } while (ptep++, addr = next, addr != end && pte_none(READ_ONCE(*ptep)));
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300109}
110
Will Deacon20a004e2018-02-15 11:14:56 +0000111static void __init kasan_pmd_populate(pud_t *pudp, unsigned long addr,
Will Deacone17d8022017-11-15 17:36:40 -0800112 unsigned long end, int node, bool early)
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300113{
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300114 unsigned long next;
Will Deacon20a004e2018-02-15 11:14:56 +0000115 pmd_t *pmdp = kasan_pmd_offset(pudp, addr, node, early);
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300116
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300117 do {
118 next = pmd_addr_end(addr, end);
Will Deacon20a004e2018-02-15 11:14:56 +0000119 kasan_pte_populate(pmdp, addr, next, node, early);
120 } while (pmdp++, addr = next, addr != end && pmd_none(READ_ONCE(*pmdp)));
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300121}
122
Will Deacon20a004e2018-02-15 11:14:56 +0000123static void __init kasan_pud_populate(pgd_t *pgdp, unsigned long addr,
Will Deacone17d8022017-11-15 17:36:40 -0800124 unsigned long end, int node, bool early)
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300125{
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300126 unsigned long next;
Will Deacon20a004e2018-02-15 11:14:56 +0000127 pud_t *pudp = kasan_pud_offset(pgdp, addr, node, early);
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300128
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300129 do {
130 next = pud_addr_end(addr, end);
Will Deacon20a004e2018-02-15 11:14:56 +0000131 kasan_pmd_populate(pudp, addr, next, node, early);
132 } while (pudp++, addr = next, addr != end && pud_none(READ_ONCE(*pudp)));
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300133}
134
Will Deacone17d8022017-11-15 17:36:40 -0800135static void __init kasan_pgd_populate(unsigned long addr, unsigned long end,
136 int node, bool early)
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300137{
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300138 unsigned long next;
Will Deacon20a004e2018-02-15 11:14:56 +0000139 pgd_t *pgdp;
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300140
Will Deacon20a004e2018-02-15 11:14:56 +0000141 pgdp = pgd_offset_k(addr);
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300142 do {
143 next = pgd_addr_end(addr, end);
Will Deacon20a004e2018-02-15 11:14:56 +0000144 kasan_pud_populate(pgdp, addr, next, node, early);
145 } while (pgdp++, addr = next, addr != end);
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300146}
147
Will Deacone17d8022017-11-15 17:36:40 -0800148/* The early shadow maps everything to a single page of zeroes */
Will Deacon83040122015-10-13 14:01:06 +0100149asmlinkage void __init kasan_early_init(void)
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300150{
Andrey Konovalov917538e2018-02-06 15:36:44 -0800151 BUILD_BUG_ON(KASAN_SHADOW_OFFSET !=
152 KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT)));
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300153 BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_START, PGDIR_SIZE));
154 BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, PGDIR_SIZE));
Will Deacone17d8022017-11-15 17:36:40 -0800155 kasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE,
156 true);
157}
158
159/* Set up full kasan mappings, ensuring that the mapped pages are zeroed */
160static void __init kasan_map_populate(unsigned long start, unsigned long end,
161 int node)
162{
163 kasan_pgd_populate(start & PAGE_MASK, PAGE_ALIGN(end), node, false);
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300164}
165
Mark Rutland068a17a2016-01-25 11:45:12 +0000166/*
167 * Copy the current shadow region into a new pgdir.
168 */
169void __init kasan_copy_shadow(pgd_t *pgdir)
170{
Will Deacon20a004e2018-02-15 11:14:56 +0000171 pgd_t *pgdp, *pgdp_new, *pgdp_end;
Mark Rutland068a17a2016-01-25 11:45:12 +0000172
Will Deacon20a004e2018-02-15 11:14:56 +0000173 pgdp = pgd_offset_k(KASAN_SHADOW_START);
174 pgdp_end = pgd_offset_k(KASAN_SHADOW_END);
175 pgdp_new = pgd_offset_raw(pgdir, KASAN_SHADOW_START);
Mark Rutland068a17a2016-01-25 11:45:12 +0000176 do {
Will Deacon20a004e2018-02-15 11:14:56 +0000177 set_pgd(pgdp_new, READ_ONCE(*pgdp));
178 } while (pgdp++, pgdp_new++, pgdp != pgdp_end);
Mark Rutland068a17a2016-01-25 11:45:12 +0000179}
180
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300181static void __init clear_pgds(unsigned long start,
182 unsigned long end)
183{
184 /*
185 * Remove references to kasan page tables from
186 * swapper_pg_dir. pgd_clear() can't be used
187 * here because it's nop on 2,3-level pagetable setups
188 */
189 for (; start < end; start += PGDIR_SIZE)
190 set_pgd(pgd_offset_k(start), __pgd(0));
191}
192
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300193void __init kasan_init(void)
194{
Ard Biesheuvelf9040772016-02-16 13:52:40 +0100195 u64 kimg_shadow_start, kimg_shadow_end;
Ard Biesheuvelf80fb3a2016-01-26 14:12:01 +0100196 u64 mod_shadow_start, mod_shadow_end;
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300197 struct memblock_region *reg;
Ard Biesheuvel7b1af972016-01-11 14:50:21 +0100198 int i;
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300199
Will Deacone17d8022017-11-15 17:36:40 -0800200 kimg_shadow_start = (u64)kasan_mem_to_shadow(_text) & PAGE_MASK;
201 kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(_end));
Ard Biesheuvelf9040772016-02-16 13:52:40 +0100202
Ard Biesheuvelf80fb3a2016-01-26 14:12:01 +0100203 mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
204 mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
205
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300206 /*
207 * We are going to perform proper setup of shadow memory.
Kyrylo Tkachov0293c8b2018-10-04 17:06:46 +0100208 * At first we should unmap early shadow (clear_pgds() call below).
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300209 * However, instrumented code couldn't execute without shadow memory.
210 * tmp_pg_dir used to keep early shadow mapped until full shadow
211 * setup will be finished.
212 */
213 memcpy(tmp_pg_dir, swapper_pg_dir, sizeof(tmp_pg_dir));
Mark Rutlandc1a88e92016-01-25 11:45:02 +0000214 dsb(ishst);
Laura Abbott2077be62017-01-10 13:35:49 -0800215 cpu_replace_ttbr1(lm_alias(tmp_pg_dir));
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300216
217 clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END);
218
Will Deacone17d8022017-11-15 17:36:40 -0800219 kasan_map_populate(kimg_shadow_start, kimg_shadow_end,
Mark Rutland800cb2e2018-04-16 14:44:41 +0100220 early_pfn_to_nid(virt_to_pfn(lm_alias(_text))));
Ard Biesheuvelf9040772016-02-16 13:52:40 +0100221
Andrey Konovalov9577dd72018-12-28 00:30:01 -0800222 kasan_populate_early_shadow((void *)KASAN_SHADOW_START,
223 (void *)mod_shadow_start);
224 kasan_populate_early_shadow((void *)kimg_shadow_end,
225 kasan_mem_to_shadow((void *)PAGE_OFFSET));
Ard Biesheuvelf80fb3a2016-01-26 14:12:01 +0100226
227 if (kimg_shadow_start > mod_shadow_end)
Andrey Konovalov9577dd72018-12-28 00:30:01 -0800228 kasan_populate_early_shadow((void *)mod_shadow_end,
229 (void *)kimg_shadow_start);
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300230
231 for_each_memblock(memory, reg) {
232 void *start = (void *)__phys_to_virt(reg->base);
233 void *end = (void *)__phys_to_virt(reg->base + reg->size);
234
235 if (start >= end)
236 break;
237
Will Deacone17d8022017-11-15 17:36:40 -0800238 kasan_map_populate((unsigned long)kasan_mem_to_shadow(start),
239 (unsigned long)kasan_mem_to_shadow(end),
Mark Rutland800cb2e2018-04-16 14:44:41 +0100240 early_pfn_to_nid(virt_to_pfn(start)));
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300241 }
242
Ard Biesheuvel7b1af972016-01-11 14:50:21 +0100243 /*
Andrey Konovalov9577dd72018-12-28 00:30:01 -0800244 * KAsan may reuse the contents of kasan_early_shadow_pte directly,
245 * so we should make sure that it maps the zero page read-only.
Ard Biesheuvel7b1af972016-01-11 14:50:21 +0100246 */
247 for (i = 0; i < PTRS_PER_PTE; i++)
Andrey Konovalov9577dd72018-12-28 00:30:01 -0800248 set_pte(&kasan_early_shadow_pte[i],
249 pfn_pte(sym_to_pfn(kasan_early_shadow_page),
250 PAGE_KERNEL_RO));
Ard Biesheuvel7b1af972016-01-11 14:50:21 +0100251
Andrey Konovalov080eb832018-12-28 00:30:09 -0800252 memset(kasan_early_shadow_page, KASAN_SHADOW_INIT, PAGE_SIZE);
Laura Abbott2077be62017-01-10 13:35:49 -0800253 cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300254
Andrey Konovalov3c9e3aa2018-12-28 00:30:16 -0800255 kasan_init_tags();
256
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300257 /* At this point kasan is fully initialized. Enable error messages */
258 init_task.kasan_depth = 0;
259 pr_info("KernelAddressSanitizer initialized\n");
260}