Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 2 | /* |
| 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 Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #define pr_fmt(fmt) "kasan: " fmt |
| 10 | #include <linux/kasan.h> |
| 11 | #include <linux/kernel.h> |
Ingo Molnar | 9164bb4 | 2017-02-04 01:20:53 +0100 | [diff] [blame] | 12 | #include <linux/sched/task.h> |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 13 | #include <linux/memblock.h> |
| 14 | #include <linux/start_kernel.h> |
Laura Abbott | 2077be6 | 2017-01-10 13:35:49 -0800 | [diff] [blame] | 15 | #include <linux/mm.h> |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 16 | |
Mark Rutland | c1a88e9 | 2016-01-25 11:45:02 +0000 | [diff] [blame] | 17 | #include <asm/mmu_context.h> |
Ard Biesheuvel | f904077 | 2016-02-16 13:52:40 +0100 | [diff] [blame] | 18 | #include <asm/kernel-pgtable.h> |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 19 | #include <asm/page.h> |
| 20 | #include <asm/pgalloc.h> |
Ard Biesheuvel | f904077 | 2016-02-16 13:52:40 +0100 | [diff] [blame] | 21 | #include <asm/sections.h> |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 22 | #include <asm/tlbflush.h> |
| 23 | |
| 24 | static pgd_t tmp_pg_dir[PTRS_PER_PGD] __initdata __aligned(PGD_SIZE); |
| 25 | |
Laura Abbott | 2077be6 | 2017-01-10 13:35:49 -0800 | [diff] [blame] | 26 | /* |
| 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. |
| 31 | */ |
| 32 | |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 33 | static phys_addr_t __init kasan_alloc_zeroed_page(int node) |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 34 | { |
Mike Rapoport | eb31d55 | 2018-10-30 15:08:04 -0700 | [diff] [blame] | 35 | void *p = memblock_alloc_try_nid(PAGE_SIZE, PAGE_SIZE, |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 36 | __pa(MAX_DMA_ADDRESS), |
Qian Cai | fed84c7 | 2018-12-28 00:36:29 -0800 | [diff] [blame] | 37 | MEMBLOCK_ALLOC_KASAN, node); |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 38 | if (!p) |
| 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)); |
| 42 | |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 43 | return __pa(p); |
| 44 | } |
| 45 | |
Andrey Konovalov | 080eb83 | 2018-12-28 00:30:09 -0800 | [diff] [blame] | 46 | static 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); |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 51 | if (!p) |
| 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)); |
| 55 | |
Andrey Konovalov | 080eb83 | 2018-12-28 00:30:09 -0800 | [diff] [blame] | 56 | return __pa(p); |
| 57 | } |
| 58 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 59 | static pte_t *__init kasan_pte_offset(pmd_t *pmdp, unsigned long addr, int node, |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 60 | bool early) |
| 61 | { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 62 | if (pmd_none(READ_ONCE(*pmdp))) { |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 63 | phys_addr_t pte_phys = early ? |
| 64 | __pa_symbol(kasan_early_shadow_pte) |
| 65 | : kasan_alloc_zeroed_page(node); |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 66 | __pmd_populate(pmdp, pte_phys, PMD_TYPE_TABLE); |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 69 | return early ? pte_offset_kimg(pmdp, addr) |
| 70 | : pte_offset_kernel(pmdp, addr); |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 73 | static pmd_t *__init kasan_pmd_offset(pud_t *pudp, unsigned long addr, int node, |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 74 | bool early) |
| 75 | { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 76 | if (pud_none(READ_ONCE(*pudp))) { |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 77 | phys_addr_t pmd_phys = early ? |
| 78 | __pa_symbol(kasan_early_shadow_pmd) |
| 79 | : kasan_alloc_zeroed_page(node); |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 80 | __pud_populate(pudp, pmd_phys, PMD_TYPE_TABLE); |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 83 | return early ? pmd_offset_kimg(pudp, addr) : pmd_offset(pudp, addr); |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Mike Rapoport | e9f6376 | 2020-06-04 16:46:23 -0700 | [diff] [blame] | 86 | static pud_t *__init kasan_pud_offset(p4d_t *p4dp, unsigned long addr, int node, |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 87 | bool early) |
| 88 | { |
Mike Rapoport | e9f6376 | 2020-06-04 16:46:23 -0700 | [diff] [blame] | 89 | if (p4d_none(READ_ONCE(*p4dp))) { |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 90 | phys_addr_t pud_phys = early ? |
| 91 | __pa_symbol(kasan_early_shadow_pud) |
| 92 | : kasan_alloc_zeroed_page(node); |
Mike Rapoport | e9f6376 | 2020-06-04 16:46:23 -0700 | [diff] [blame] | 93 | __p4d_populate(p4dp, pud_phys, PMD_TYPE_TABLE); |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Mike Rapoport | e9f6376 | 2020-06-04 16:46:23 -0700 | [diff] [blame] | 96 | return early ? pud_offset_kimg(p4dp, addr) : pud_offset(p4dp, addr); |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 99 | static void __init kasan_pte_populate(pmd_t *pmdp, unsigned long addr, |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 100 | unsigned long end, int node, bool early) |
| 101 | { |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 102 | unsigned long next; |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 103 | pte_t *ptep = kasan_pte_offset(pmdp, addr, node, early); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 104 | |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 105 | do { |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 106 | phys_addr_t page_phys = early ? |
| 107 | __pa_symbol(kasan_early_shadow_page) |
Andrey Konovalov | 080eb83 | 2018-12-28 00:30:09 -0800 | [diff] [blame] | 108 | : kasan_alloc_raw_page(node); |
| 109 | if (!early) |
| 110 | memset(__va(page_phys), KASAN_SHADOW_INIT, PAGE_SIZE); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 111 | next = addr + PAGE_SIZE; |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 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))); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 114 | } |
| 115 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 116 | static void __init kasan_pmd_populate(pud_t *pudp, unsigned long addr, |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 117 | unsigned long end, int node, bool early) |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 118 | { |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 119 | unsigned long next; |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 120 | pmd_t *pmdp = kasan_pmd_offset(pudp, addr, node, early); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 121 | |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 122 | do { |
| 123 | next = pmd_addr_end(addr, end); |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 124 | kasan_pte_populate(pmdp, addr, next, node, early); |
| 125 | } while (pmdp++, addr = next, addr != end && pmd_none(READ_ONCE(*pmdp))); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 126 | } |
| 127 | |
Mike Rapoport | e9f6376 | 2020-06-04 16:46:23 -0700 | [diff] [blame] | 128 | static void __init kasan_pud_populate(p4d_t *p4dp, unsigned long addr, |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 129 | unsigned long end, int node, bool early) |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 130 | { |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 131 | unsigned long next; |
Mike Rapoport | e9f6376 | 2020-06-04 16:46:23 -0700 | [diff] [blame] | 132 | pud_t *pudp = kasan_pud_offset(p4dp, addr, node, early); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 133 | |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 134 | do { |
| 135 | next = pud_addr_end(addr, end); |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 136 | kasan_pmd_populate(pudp, addr, next, node, early); |
| 137 | } while (pudp++, addr = next, addr != end && pud_none(READ_ONCE(*pudp))); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 138 | } |
| 139 | |
Mike Rapoport | e9f6376 | 2020-06-04 16:46:23 -0700 | [diff] [blame] | 140 | static void __init kasan_p4d_populate(pgd_t *pgdp, unsigned long addr, |
| 141 | unsigned long end, int node, bool early) |
| 142 | { |
| 143 | unsigned long next; |
| 144 | p4d_t *p4dp = p4d_offset(pgdp, addr); |
| 145 | |
| 146 | do { |
| 147 | next = p4d_addr_end(addr, end); |
| 148 | kasan_pud_populate(p4dp, addr, next, node, early); |
| 149 | } while (p4dp++, addr = next, addr != end); |
| 150 | } |
| 151 | |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 152 | static void __init kasan_pgd_populate(unsigned long addr, unsigned long end, |
| 153 | int node, bool early) |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 154 | { |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 155 | unsigned long next; |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 156 | pgd_t *pgdp; |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 157 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 158 | pgdp = pgd_offset_k(addr); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 159 | do { |
| 160 | next = pgd_addr_end(addr, end); |
Mike Rapoport | e9f6376 | 2020-06-04 16:46:23 -0700 | [diff] [blame] | 161 | kasan_p4d_populate(pgdp, addr, next, node, early); |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 162 | } while (pgdp++, addr = next, addr != end); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 163 | } |
| 164 | |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 165 | /* The early shadow maps everything to a single page of zeroes */ |
Will Deacon | 8304012 | 2015-10-13 14:01:06 +0100 | [diff] [blame] | 166 | asmlinkage void __init kasan_early_init(void) |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 167 | { |
Andrey Konovalov | 917538e | 2018-02-06 15:36:44 -0800 | [diff] [blame] | 168 | BUILD_BUG_ON(KASAN_SHADOW_OFFSET != |
| 169 | KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT))); |
Steve Capper | 90ec95c | 2019-08-07 16:55:17 +0100 | [diff] [blame] | 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)); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 172 | BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, PGDIR_SIZE)); |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 173 | kasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE, |
| 174 | true); |
| 175 | } |
| 176 | |
| 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, |
| 179 | int node) |
| 180 | { |
| 181 | kasan_pgd_populate(start & PAGE_MASK, PAGE_ALIGN(end), node, false); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 182 | } |
| 183 | |
Mark Rutland | 068a17a | 2016-01-25 11:45:12 +0000 | [diff] [blame] | 184 | /* |
| 185 | * Copy the current shadow region into a new pgdir. |
| 186 | */ |
| 187 | void __init kasan_copy_shadow(pgd_t *pgdir) |
| 188 | { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 189 | pgd_t *pgdp, *pgdp_new, *pgdp_end; |
Mark Rutland | 068a17a | 2016-01-25 11:45:12 +0000 | [diff] [blame] | 190 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 191 | pgdp = pgd_offset_k(KASAN_SHADOW_START); |
| 192 | pgdp_end = pgd_offset_k(KASAN_SHADOW_END); |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 193 | pgdp_new = pgd_offset_pgd(pgdir, KASAN_SHADOW_START); |
Mark Rutland | 068a17a | 2016-01-25 11:45:12 +0000 | [diff] [blame] | 194 | do { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 195 | set_pgd(pgdp_new, READ_ONCE(*pgdp)); |
| 196 | } while (pgdp++, pgdp_new++, pgdp != pgdp_end); |
Mark Rutland | 068a17a | 2016-01-25 11:45:12 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 199 | static void __init clear_pgds(unsigned long start, |
| 200 | unsigned long end) |
| 201 | { |
| 202 | /* |
| 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 |
| 206 | */ |
| 207 | for (; start < end; start += PGDIR_SIZE) |
| 208 | set_pgd(pgd_offset_k(start), __pgd(0)); |
| 209 | } |
| 210 | |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 211 | void __init kasan_init(void) |
| 212 | { |
Ard Biesheuvel | f904077 | 2016-02-16 13:52:40 +0100 | [diff] [blame] | 213 | u64 kimg_shadow_start, kimg_shadow_end; |
Ard Biesheuvel | f80fb3a | 2016-01-26 14:12:01 +0100 | [diff] [blame] | 214 | u64 mod_shadow_start, mod_shadow_end; |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 215 | struct memblock_region *reg; |
Ard Biesheuvel | 7b1af97 | 2016-01-11 14:50:21 +0100 | [diff] [blame] | 216 | int i; |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 217 | |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 218 | kimg_shadow_start = (u64)kasan_mem_to_shadow(_text) & PAGE_MASK; |
| 219 | kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(_end)); |
Ard Biesheuvel | f904077 | 2016-02-16 13:52:40 +0100 | [diff] [blame] | 220 | |
Ard Biesheuvel | f80fb3a | 2016-01-26 14:12:01 +0100 | [diff] [blame] | 221 | mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR); |
| 222 | mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END); |
| 223 | |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 224 | /* |
| 225 | * We are going to perform proper setup of shadow memory. |
Kyrylo Tkachov | 0293c8b | 2018-10-04 17:06:46 +0100 | [diff] [blame] | 226 | * At first we should unmap early shadow (clear_pgds() call below). |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 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. |
| 230 | */ |
| 231 | memcpy(tmp_pg_dir, swapper_pg_dir, sizeof(tmp_pg_dir)); |
Mark Rutland | c1a88e9 | 2016-01-25 11:45:02 +0000 | [diff] [blame] | 232 | dsb(ishst); |
Laura Abbott | 2077be6 | 2017-01-10 13:35:49 -0800 | [diff] [blame] | 233 | cpu_replace_ttbr1(lm_alias(tmp_pg_dir)); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 234 | |
| 235 | clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END); |
| 236 | |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 237 | kasan_map_populate(kimg_shadow_start, kimg_shadow_end, |
Mark Rutland | 800cb2e | 2018-04-16 14:44:41 +0100 | [diff] [blame] | 238 | early_pfn_to_nid(virt_to_pfn(lm_alias(_text)))); |
Ard Biesheuvel | f904077 | 2016-02-16 13:52:40 +0100 | [diff] [blame] | 239 | |
Mark Rutland | 77ad4ce | 2019-08-14 14:28:48 +0100 | [diff] [blame] | 240 | kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END), |
Steve Capper | 14c127c | 2019-08-07 16:55:14 +0100 | [diff] [blame] | 241 | (void *)mod_shadow_start); |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 242 | kasan_populate_early_shadow((void *)kimg_shadow_end, |
Steve Capper | 14c127c | 2019-08-07 16:55:14 +0100 | [diff] [blame] | 243 | (void *)KASAN_SHADOW_END); |
Ard Biesheuvel | f80fb3a | 2016-01-26 14:12:01 +0100 | [diff] [blame] | 244 | |
| 245 | if (kimg_shadow_start > mod_shadow_end) |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 246 | kasan_populate_early_shadow((void *)mod_shadow_end, |
| 247 | (void *)kimg_shadow_start); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 248 | |
| 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); |
| 252 | |
| 253 | if (start >= end) |
| 254 | break; |
| 255 | |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 256 | kasan_map_populate((unsigned long)kasan_mem_to_shadow(start), |
| 257 | (unsigned long)kasan_mem_to_shadow(end), |
Mark Rutland | 800cb2e | 2018-04-16 14:44:41 +0100 | [diff] [blame] | 258 | early_pfn_to_nid(virt_to_pfn(start))); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 259 | } |
| 260 | |
Ard Biesheuvel | 7b1af97 | 2016-01-11 14:50:21 +0100 | [diff] [blame] | 261 | /* |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 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. |
Ard Biesheuvel | 7b1af97 | 2016-01-11 14:50:21 +0100 | [diff] [blame] | 264 | */ |
| 265 | for (i = 0; i < PTRS_PER_PTE; i++) |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 266 | set_pte(&kasan_early_shadow_pte[i], |
| 267 | pfn_pte(sym_to_pfn(kasan_early_shadow_page), |
| 268 | PAGE_KERNEL_RO)); |
Ard Biesheuvel | 7b1af97 | 2016-01-11 14:50:21 +0100 | [diff] [blame] | 269 | |
Andrey Konovalov | 080eb83 | 2018-12-28 00:30:09 -0800 | [diff] [blame] | 270 | memset(kasan_early_shadow_page, KASAN_SHADOW_INIT, PAGE_SIZE); |
Laura Abbott | 2077be6 | 2017-01-10 13:35:49 -0800 | [diff] [blame] | 271 | cpu_replace_ttbr1(lm_alias(swapper_pg_dir)); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 272 | |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 273 | /* At this point kasan is fully initialized. Enable error messages */ |
| 274 | init_task.kasan_depth = 0; |
| 275 | pr_info("KernelAddressSanitizer initialized\n"); |
| 276 | } |