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 | |
Andrey Konovalov | afe6ef8 | 2020-12-22 12:00:53 -0800 | [diff] [blame] | 24 | #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) |
| 25 | |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 26 | static pgd_t tmp_pg_dir[PTRS_PER_PGD] __initdata __aligned(PGD_SIZE); |
| 27 | |
Laura Abbott | 2077be6 | 2017-01-10 13:35:49 -0800 | [diff] [blame] | 28 | /* |
| 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. |
| 33 | */ |
| 34 | |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 35 | static phys_addr_t __init kasan_alloc_zeroed_page(int node) |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 36 | { |
Mike Rapoport | eb31d55 | 2018-10-30 15:08:04 -0700 | [diff] [blame] | 37 | void *p = memblock_alloc_try_nid(PAGE_SIZE, PAGE_SIZE, |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 38 | __pa(MAX_DMA_ADDRESS), |
Qian Cai | c6975d7 | 2021-11-05 11:05:09 -0400 | [diff] [blame] | 39 | MEMBLOCK_ALLOC_NOLEAKTRACE, node); |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 40 | if (!p) |
| 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)); |
| 44 | |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 45 | return __pa(p); |
| 46 | } |
| 47 | |
Andrey Konovalov | 080eb83 | 2018-12-28 00:30:09 -0800 | [diff] [blame] | 48 | static phys_addr_t __init kasan_alloc_raw_page(int node) |
| 49 | { |
| 50 | void *p = memblock_alloc_try_nid_raw(PAGE_SIZE, PAGE_SIZE, |
| 51 | __pa(MAX_DMA_ADDRESS), |
Qian Cai | c6975d7 | 2021-11-05 11:05:09 -0400 | [diff] [blame] | 52 | MEMBLOCK_ALLOC_NOLEAKTRACE, |
| 53 | node); |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 54 | if (!p) |
| 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)); |
| 58 | |
Andrey Konovalov | 080eb83 | 2018-12-28 00:30:09 -0800 | [diff] [blame] | 59 | return __pa(p); |
| 60 | } |
| 61 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 62 | 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] | 63 | bool early) |
| 64 | { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 65 | if (pmd_none(READ_ONCE(*pmdp))) { |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 66 | phys_addr_t pte_phys = early ? |
| 67 | __pa_symbol(kasan_early_shadow_pte) |
| 68 | : kasan_alloc_zeroed_page(node); |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 69 | __pmd_populate(pmdp, pte_phys, PMD_TYPE_TABLE); |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 72 | return early ? pte_offset_kimg(pmdp, addr) |
| 73 | : pte_offset_kernel(pmdp, addr); |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 76 | 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] | 77 | bool early) |
| 78 | { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 79 | if (pud_none(READ_ONCE(*pudp))) { |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 80 | phys_addr_t pmd_phys = early ? |
| 81 | __pa_symbol(kasan_early_shadow_pmd) |
| 82 | : kasan_alloc_zeroed_page(node); |
Ard Biesheuvel | c1fd78a | 2021-03-10 11:49:40 +0100 | [diff] [blame] | 83 | __pud_populate(pudp, pmd_phys, PUD_TYPE_TABLE); |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 86 | return early ? pmd_offset_kimg(pudp, addr) : pmd_offset(pudp, addr); |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Mike Rapoport | e9f6376 | 2020-06-04 16:46:23 -0700 | [diff] [blame] | 89 | 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] | 90 | bool early) |
| 91 | { |
Mike Rapoport | e9f6376 | 2020-06-04 16:46:23 -0700 | [diff] [blame] | 92 | if (p4d_none(READ_ONCE(*p4dp))) { |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 93 | phys_addr_t pud_phys = early ? |
| 94 | __pa_symbol(kasan_early_shadow_pud) |
| 95 | : kasan_alloc_zeroed_page(node); |
Ard Biesheuvel | c1fd78a | 2021-03-10 11:49:40 +0100 | [diff] [blame] | 96 | __p4d_populate(p4dp, pud_phys, P4D_TYPE_TABLE); |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Mike Rapoport | e9f6376 | 2020-06-04 16:46:23 -0700 | [diff] [blame] | 99 | return early ? pud_offset_kimg(p4dp, addr) : pud_offset(p4dp, addr); |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 102 | static void __init kasan_pte_populate(pmd_t *pmdp, unsigned long addr, |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 103 | unsigned long end, int node, bool early) |
| 104 | { |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 105 | unsigned long next; |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 106 | pte_t *ptep = kasan_pte_offset(pmdp, addr, node, early); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 107 | |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 108 | do { |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 109 | phys_addr_t page_phys = early ? |
| 110 | __pa_symbol(kasan_early_shadow_page) |
Andrey Konovalov | 080eb83 | 2018-12-28 00:30:09 -0800 | [diff] [blame] | 111 | : kasan_alloc_raw_page(node); |
| 112 | if (!early) |
| 113 | memset(__va(page_phys), KASAN_SHADOW_INIT, PAGE_SIZE); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 114 | next = addr + PAGE_SIZE; |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 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))); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 117 | } |
| 118 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 119 | static void __init kasan_pmd_populate(pud_t *pudp, unsigned long addr, |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 120 | unsigned long end, int node, bool 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 | unsigned long next; |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 123 | pmd_t *pmdp = kasan_pmd_offset(pudp, addr, node, early); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 124 | |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 125 | do { |
| 126 | next = pmd_addr_end(addr, end); |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 127 | kasan_pte_populate(pmdp, addr, next, node, early); |
| 128 | } while (pmdp++, addr = next, addr != end && pmd_none(READ_ONCE(*pmdp))); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 129 | } |
| 130 | |
Mike Rapoport | e9f6376 | 2020-06-04 16:46:23 -0700 | [diff] [blame] | 131 | static void __init kasan_pud_populate(p4d_t *p4dp, unsigned long addr, |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 132 | unsigned long end, int node, bool 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 | unsigned long next; |
Mike Rapoport | e9f6376 | 2020-06-04 16:46:23 -0700 | [diff] [blame] | 135 | pud_t *pudp = kasan_pud_offset(p4dp, addr, node, early); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 136 | |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 137 | do { |
| 138 | next = pud_addr_end(addr, end); |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 139 | kasan_pmd_populate(pudp, addr, next, node, early); |
| 140 | } while (pudp++, addr = next, addr != end && pud_none(READ_ONCE(*pudp))); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 141 | } |
| 142 | |
Mike Rapoport | e9f6376 | 2020-06-04 16:46:23 -0700 | [diff] [blame] | 143 | static void __init kasan_p4d_populate(pgd_t *pgdp, unsigned long addr, |
| 144 | unsigned long end, int node, bool early) |
| 145 | { |
| 146 | unsigned long next; |
| 147 | p4d_t *p4dp = p4d_offset(pgdp, addr); |
| 148 | |
| 149 | do { |
| 150 | next = p4d_addr_end(addr, end); |
| 151 | kasan_pud_populate(p4dp, addr, next, node, early); |
| 152 | } while (p4dp++, addr = next, addr != end); |
| 153 | } |
| 154 | |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 155 | static void __init kasan_pgd_populate(unsigned long addr, unsigned long end, |
| 156 | int node, bool early) |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 157 | { |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 158 | unsigned long next; |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 159 | pgd_t *pgdp; |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 160 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 161 | pgdp = pgd_offset_k(addr); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 162 | do { |
| 163 | next = pgd_addr_end(addr, end); |
Mike Rapoport | e9f6376 | 2020-06-04 16:46:23 -0700 | [diff] [blame] | 164 | kasan_p4d_populate(pgdp, addr, next, node, early); |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 165 | } while (pgdp++, addr = next, addr != end); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 166 | } |
| 167 | |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 168 | /* The early shadow maps everything to a single page of zeroes */ |
Will Deacon | 8304012 | 2015-10-13 14:01:06 +0100 | [diff] [blame] | 169 | asmlinkage void __init kasan_early_init(void) |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 170 | { |
Andrey Konovalov | 917538e | 2018-02-06 15:36:44 -0800 | [diff] [blame] | 171 | BUILD_BUG_ON(KASAN_SHADOW_OFFSET != |
| 172 | KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT))); |
Steve Capper | 90ec95c | 2019-08-07 16:55:17 +0100 | [diff] [blame] | 173 | BUILD_BUG_ON(!IS_ALIGNED(_KASAN_SHADOW_START(VA_BITS), PGDIR_SIZE)); |
| 174 | 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] | 175 | BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, PGDIR_SIZE)); |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 176 | kasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE, |
| 177 | true); |
| 178 | } |
| 179 | |
| 180 | /* Set up full kasan mappings, ensuring that the mapped pages are zeroed */ |
| 181 | static void __init kasan_map_populate(unsigned long start, unsigned long end, |
| 182 | int node) |
| 183 | { |
| 184 | kasan_pgd_populate(start & PAGE_MASK, PAGE_ALIGN(end), node, false); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 185 | } |
| 186 | |
Mark Rutland | 068a17a | 2016-01-25 11:45:12 +0000 | [diff] [blame] | 187 | /* |
| 188 | * Copy the current shadow region into a new pgdir. |
| 189 | */ |
| 190 | void __init kasan_copy_shadow(pgd_t *pgdir) |
| 191 | { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 192 | pgd_t *pgdp, *pgdp_new, *pgdp_end; |
Mark Rutland | 068a17a | 2016-01-25 11:45:12 +0000 | [diff] [blame] | 193 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 194 | pgdp = pgd_offset_k(KASAN_SHADOW_START); |
| 195 | pgdp_end = pgd_offset_k(KASAN_SHADOW_END); |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 196 | pgdp_new = pgd_offset_pgd(pgdir, KASAN_SHADOW_START); |
Mark Rutland | 068a17a | 2016-01-25 11:45:12 +0000 | [diff] [blame] | 197 | do { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 198 | set_pgd(pgdp_new, READ_ONCE(*pgdp)); |
| 199 | } while (pgdp++, pgdp_new++, pgdp != pgdp_end); |
Mark Rutland | 068a17a | 2016-01-25 11:45:12 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 202 | static void __init clear_pgds(unsigned long start, |
| 203 | unsigned long end) |
| 204 | { |
| 205 | /* |
| 206 | * Remove references to kasan page tables from |
| 207 | * swapper_pg_dir. pgd_clear() can't be used |
| 208 | * here because it's nop on 2,3-level pagetable setups |
| 209 | */ |
| 210 | for (; start < end; start += PGDIR_SIZE) |
| 211 | set_pgd(pgd_offset_k(start), __pgd(0)); |
| 212 | } |
| 213 | |
Andrey Konovalov | afe6ef8 | 2020-12-22 12:00:53 -0800 | [diff] [blame] | 214 | static void __init kasan_init_shadow(void) |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 215 | { |
Ard Biesheuvel | f904077 | 2016-02-16 13:52:40 +0100 | [diff] [blame] | 216 | u64 kimg_shadow_start, kimg_shadow_end; |
Ard Biesheuvel | f80fb3a | 2016-01-26 14:12:01 +0100 | [diff] [blame] | 217 | u64 mod_shadow_start, mod_shadow_end; |
Lecopzer Chen | 9a0732e | 2021-03-24 12:05:18 +0800 | [diff] [blame] | 218 | u64 vmalloc_shadow_end; |
Mike Rapoport | b10d6bc | 2020-10-13 16:58:08 -0700 | [diff] [blame] | 219 | phys_addr_t pa_start, pa_end; |
| 220 | u64 i; |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 221 | |
Lecopzer Chen | 7d7b88f | 2021-03-24 12:05:19 +0800 | [diff] [blame] | 222 | kimg_shadow_start = (u64)kasan_mem_to_shadow(KERNEL_START) & PAGE_MASK; |
| 223 | kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(KERNEL_END)); |
Ard Biesheuvel | f904077 | 2016-02-16 13:52:40 +0100 | [diff] [blame] | 224 | |
Ard Biesheuvel | f80fb3a | 2016-01-26 14:12:01 +0100 | [diff] [blame] | 225 | mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR); |
| 226 | mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END); |
| 227 | |
Lecopzer Chen | 9a0732e | 2021-03-24 12:05:18 +0800 | [diff] [blame] | 228 | vmalloc_shadow_end = (u64)kasan_mem_to_shadow((void *)VMALLOC_END); |
| 229 | |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 230 | /* |
| 231 | * We are going to perform proper setup of shadow memory. |
Kyrylo Tkachov | 0293c8b | 2018-10-04 17:06:46 +0100 | [diff] [blame] | 232 | * At first we should unmap early shadow (clear_pgds() call below). |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 233 | * However, instrumented code couldn't execute without shadow memory. |
| 234 | * tmp_pg_dir used to keep early shadow mapped until full shadow |
| 235 | * setup will be finished. |
| 236 | */ |
| 237 | memcpy(tmp_pg_dir, swapper_pg_dir, sizeof(tmp_pg_dir)); |
Mark Rutland | c1a88e9 | 2016-01-25 11:45:02 +0000 | [diff] [blame] | 238 | dsb(ishst); |
Ard Biesheuvel | 1682c45 | 2022-06-24 17:06:40 +0200 | [diff] [blame] | 239 | cpu_replace_ttbr1(lm_alias(tmp_pg_dir), idmap_pg_dir); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 240 | |
| 241 | clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END); |
| 242 | |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 243 | kasan_map_populate(kimg_shadow_start, kimg_shadow_end, |
Lecopzer Chen | 7d7b88f | 2021-03-24 12:05:19 +0800 | [diff] [blame] | 244 | early_pfn_to_nid(virt_to_pfn(lm_alias(KERNEL_START)))); |
Ard Biesheuvel | f904077 | 2016-02-16 13:52:40 +0100 | [diff] [blame] | 245 | |
Mark Rutland | 77ad4ce | 2019-08-14 14:28:48 +0100 | [diff] [blame] | 246 | kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END), |
Steve Capper | 14c127c | 2019-08-07 16:55:14 +0100 | [diff] [blame] | 247 | (void *)mod_shadow_start); |
Ard Biesheuvel | f80fb3a | 2016-01-26 14:12:01 +0100 | [diff] [blame] | 248 | |
Lecopzer Chen | 9a0732e | 2021-03-24 12:05:18 +0800 | [diff] [blame] | 249 | if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) { |
| 250 | BUILD_BUG_ON(VMALLOC_START != MODULES_END); |
| 251 | kasan_populate_early_shadow((void *)vmalloc_shadow_end, |
| 252 | (void *)KASAN_SHADOW_END); |
| 253 | } else { |
| 254 | kasan_populate_early_shadow((void *)kimg_shadow_end, |
| 255 | (void *)KASAN_SHADOW_END); |
| 256 | if (kimg_shadow_start > mod_shadow_end) |
| 257 | kasan_populate_early_shadow((void *)mod_shadow_end, |
| 258 | (void *)kimg_shadow_start); |
| 259 | } |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 260 | |
Mike Rapoport | b10d6bc | 2020-10-13 16:58:08 -0700 | [diff] [blame] | 261 | for_each_mem_range(i, &pa_start, &pa_end) { |
| 262 | void *start = (void *)__phys_to_virt(pa_start); |
| 263 | void *end = (void *)__phys_to_virt(pa_end); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 264 | |
| 265 | if (start >= end) |
| 266 | break; |
| 267 | |
Will Deacon | e17d802 | 2017-11-15 17:36:40 -0800 | [diff] [blame] | 268 | kasan_map_populate((unsigned long)kasan_mem_to_shadow(start), |
| 269 | (unsigned long)kasan_mem_to_shadow(end), |
Mark Rutland | 800cb2e | 2018-04-16 14:44:41 +0100 | [diff] [blame] | 270 | early_pfn_to_nid(virt_to_pfn(start))); |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 271 | } |
| 272 | |
Ard Biesheuvel | 7b1af97 | 2016-01-11 14:50:21 +0100 | [diff] [blame] | 273 | /* |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 274 | * KAsan may reuse the contents of kasan_early_shadow_pte directly, |
| 275 | * 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] | 276 | */ |
| 277 | for (i = 0; i < PTRS_PER_PTE; i++) |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 278 | set_pte(&kasan_early_shadow_pte[i], |
| 279 | pfn_pte(sym_to_pfn(kasan_early_shadow_page), |
| 280 | PAGE_KERNEL_RO)); |
Ard Biesheuvel | 7b1af97 | 2016-01-11 14:50:21 +0100 | [diff] [blame] | 281 | |
Andrey Konovalov | 080eb83 | 2018-12-28 00:30:09 -0800 | [diff] [blame] | 282 | memset(kasan_early_shadow_page, KASAN_SHADOW_INIT, PAGE_SIZE); |
Ard Biesheuvel | 1682c45 | 2022-06-24 17:06:40 +0200 | [diff] [blame] | 283 | cpu_replace_ttbr1(lm_alias(swapper_pg_dir), idmap_pg_dir); |
Andrey Konovalov | afe6ef8 | 2020-12-22 12:00:53 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Andrey Konovalov | d73b493 | 2020-12-22 12:00:56 -0800 | [diff] [blame] | 286 | static void __init kasan_init_depth(void) |
| 287 | { |
| 288 | init_task.kasan_depth = 0; |
| 289 | } |
| 290 | |
Kefeng Wang | 3252b1d | 2021-11-05 13:39:47 -0700 | [diff] [blame] | 291 | #ifdef CONFIG_KASAN_VMALLOC |
| 292 | void __init kasan_populate_early_vm_area_shadow(void *start, unsigned long size) |
| 293 | { |
| 294 | unsigned long shadow_start, shadow_end; |
| 295 | |
| 296 | if (!is_vmalloc_or_module_addr(start)) |
| 297 | return; |
| 298 | |
| 299 | shadow_start = (unsigned long)kasan_mem_to_shadow(start); |
| 300 | shadow_start = ALIGN_DOWN(shadow_start, PAGE_SIZE); |
| 301 | shadow_end = (unsigned long)kasan_mem_to_shadow(start + size); |
| 302 | shadow_end = ALIGN(shadow_end, PAGE_SIZE); |
| 303 | kasan_map_populate(shadow_start, shadow_end, NUMA_NO_NODE); |
| 304 | } |
| 305 | #endif |
| 306 | |
Andrey Konovalov | afe6ef8 | 2020-12-22 12:00:53 -0800 | [diff] [blame] | 307 | void __init kasan_init(void) |
| 308 | { |
| 309 | kasan_init_shadow(); |
Andrey Konovalov | d73b493 | 2020-12-22 12:00:56 -0800 | [diff] [blame] | 310 | kasan_init_depth(); |
Andrey Konovalov | 28ab358 | 2020-12-22 12:01:00 -0800 | [diff] [blame] | 311 | #if defined(CONFIG_KASAN_GENERIC) |
Andrey Konovalov | 60a3a5f | 2020-12-22 12:01:03 -0800 | [diff] [blame] | 312 | /* CONFIG_KASAN_SW_TAGS also requires kasan_init_sw_tags(). */ |
Kuan-Ying Lee | b873e98 | 2021-11-10 20:32:49 -0800 | [diff] [blame] | 313 | pr_info("KernelAddressSanitizer initialized (generic)\n"); |
Andrey Konovalov | 28ab358 | 2020-12-22 12:01:00 -0800 | [diff] [blame] | 314 | #endif |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 315 | } |
Andrey Konovalov | 28ab358 | 2020-12-22 12:01:00 -0800 | [diff] [blame] | 316 | |
| 317 | #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */ |