2 * This file contains some kasan initialization code.
4 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
5 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
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.
13 #include <linux/bootmem.h>
14 #include <linux/init.h>
15 #include <linux/kasan.h>
16 #include <linux/kernel.h>
17 #include <linux/memblock.h>
19 #include <linux/pfn.h>
22 #include <asm/pgalloc.h>
25 * This page serves two purposes:
26 * - It used as early shadow memory. The entire shadow region populated
27 * with this page, before we will be able to setup normal shadow memory.
28 * - Latter it reused it as zero shadow to cover large ranges of memory
29 * that allowed to access, but not handled by kasan (vmalloc/vmemmap ...).
31 unsigned char kasan_zero_page
[PAGE_SIZE
] __page_aligned_bss
;
33 #if CONFIG_PGTABLE_LEVELS > 4
34 p4d_t kasan_zero_p4d
[PTRS_PER_P4D
] __page_aligned_bss
;
36 #if CONFIG_PGTABLE_LEVELS > 3
37 pud_t kasan_zero_pud
[PTRS_PER_PUD
] __page_aligned_bss
;
39 #if CONFIG_PGTABLE_LEVELS > 2
40 pmd_t kasan_zero_pmd
[PTRS_PER_PMD
] __page_aligned_bss
;
42 pte_t kasan_zero_pte
[PTRS_PER_PTE
] __page_aligned_bss
;
44 static __init
void *early_alloc(size_t size
, int node
)
46 return memblock_virt_alloc_try_nid(size
, size
, __pa(MAX_DMA_ADDRESS
),
47 BOOTMEM_ALLOC_ACCESSIBLE
, node
);
50 static void __init
zero_pte_populate(pmd_t
*pmd
, unsigned long addr
,
53 pte_t
*pte
= pte_offset_kernel(pmd
, addr
);
56 zero_pte
= pfn_pte(PFN_DOWN(__pa_symbol(kasan_zero_page
)), PAGE_KERNEL
);
57 zero_pte
= pte_wrprotect(zero_pte
);
59 while (addr
+ PAGE_SIZE
<= end
) {
60 set_pte_at(&init_mm
, addr
, pte
, zero_pte
);
62 pte
= pte_offset_kernel(pmd
, addr
);
66 static void __init
zero_pmd_populate(pud_t
*pud
, unsigned long addr
,
69 pmd_t
*pmd
= pmd_offset(pud
, addr
);
73 next
= pmd_addr_end(addr
, end
);
75 if (IS_ALIGNED(addr
, PMD_SIZE
) && end
- addr
>= PMD_SIZE
) {
76 pmd_populate_kernel(&init_mm
, pmd
, lm_alias(kasan_zero_pte
));
81 pmd_populate_kernel(&init_mm
, pmd
,
82 early_alloc(PAGE_SIZE
, NUMA_NO_NODE
));
84 zero_pte_populate(pmd
, addr
, next
);
85 } while (pmd
++, addr
= next
, addr
!= end
);
88 static void __init
zero_pud_populate(p4d_t
*p4d
, unsigned long addr
,
91 pud_t
*pud
= pud_offset(p4d
, addr
);
95 next
= pud_addr_end(addr
, end
);
96 if (IS_ALIGNED(addr
, PUD_SIZE
) && end
- addr
>= PUD_SIZE
) {
99 pud_populate(&init_mm
, pud
, lm_alias(kasan_zero_pmd
));
100 pmd
= pmd_offset(pud
, addr
);
101 pmd_populate_kernel(&init_mm
, pmd
, lm_alias(kasan_zero_pte
));
105 if (pud_none(*pud
)) {
106 pud_populate(&init_mm
, pud
,
107 early_alloc(PAGE_SIZE
, NUMA_NO_NODE
));
109 zero_pmd_populate(pud
, addr
, next
);
110 } while (pud
++, addr
= next
, addr
!= end
);
113 static void __init
zero_p4d_populate(pgd_t
*pgd
, unsigned long addr
,
116 p4d_t
*p4d
= p4d_offset(pgd
, addr
);
120 next
= p4d_addr_end(addr
, end
);
122 if (p4d_none(*p4d
)) {
123 p4d_populate(&init_mm
, p4d
,
124 early_alloc(PAGE_SIZE
, NUMA_NO_NODE
));
126 zero_pud_populate(p4d
, addr
, next
);
127 } while (p4d
++, addr
= next
, addr
!= end
);
131 * kasan_populate_zero_shadow - populate shadow memory region with
133 * @shadow_start - start of the memory range to populate
134 * @shadow_end - end of the memory range to populate
136 void __init
kasan_populate_zero_shadow(const void *shadow_start
,
137 const void *shadow_end
)
139 unsigned long addr
= (unsigned long)shadow_start
;
140 unsigned long end
= (unsigned long)shadow_end
;
141 pgd_t
*pgd
= pgd_offset_k(addr
);
145 next
= pgd_addr_end(addr
, end
);
147 if (IS_ALIGNED(addr
, PGDIR_SIZE
) && end
- addr
>= PGDIR_SIZE
) {
153 * kasan_zero_pud should be populated with pmds
155 * [pud,pmd]_populate*() below needed only for
156 * 3,2 - level page tables where we don't have
157 * puds,pmds, so pgd_populate(), pud_populate()
160 * The ifndef is required to avoid build breakage.
162 * With 5level-fixup.h, pgd_populate() is not nop and
163 * we reference kasan_zero_p4d. It's not defined
164 * unless 5-level paging enabled.
166 * The ifndef can be dropped once all KASAN-enabled
167 * architectures will switch to pgtable-nop4d.h.
169 #ifndef __ARCH_HAS_5LEVEL_HACK
170 pgd_populate(&init_mm
, pgd
, lm_alias(kasan_zero_p4d
));
172 p4d
= p4d_offset(pgd
, addr
);
173 p4d_populate(&init_mm
, p4d
, lm_alias(kasan_zero_pud
));
174 pud
= pud_offset(p4d
, addr
);
175 pud_populate(&init_mm
, pud
, lm_alias(kasan_zero_pmd
));
176 pmd
= pmd_offset(pud
, addr
);
177 pmd_populate_kernel(&init_mm
, pmd
, lm_alias(kasan_zero_pte
));
181 if (pgd_none(*pgd
)) {
182 pgd_populate(&init_mm
, pgd
,
183 early_alloc(PAGE_SIZE
, NUMA_NO_NODE
));
185 zero_p4d_populate(pgd
, addr
, next
);
186 } while (pgd
++, addr
= next
, addr
!= end
);