1 #define pr_fmt(fmt) "kasan: " fmt
2 #include <linux/bootmem.h>
3 #include <linux/kasan.h>
4 #include <linux/kdebug.h>
6 #include <linux/sched.h>
7 #include <linux/vmalloc.h>
9 #include <asm/tlbflush.h>
10 #include <asm/sections.h>
12 extern pgd_t early_level4_pgt
[PTRS_PER_PGD
];
13 extern struct range pfn_mapped
[E820_X_MAX
];
15 static int __init
map_range(struct range
*range
)
20 start
= (unsigned long)kasan_mem_to_shadow(pfn_to_kaddr(range
->start
));
21 end
= (unsigned long)kasan_mem_to_shadow(pfn_to_kaddr(range
->end
));
24 * end + 1 here is intentional. We check several shadow bytes in advance
25 * to slightly speed up fastpath. In some rare cases we could cross
26 * boundary of mapped shadow, so we just map some more here.
28 return vmemmap_populate(start
, end
+ 1, NUMA_NO_NODE
);
31 static void __init
clear_pgds(unsigned long start
,
34 for (; start
< end
; start
+= PGDIR_SIZE
)
35 pgd_clear(pgd_offset_k(start
));
38 static void __init
kasan_map_early_shadow(pgd_t
*pgd
)
41 unsigned long start
= KASAN_SHADOW_START
;
42 unsigned long end
= KASAN_SHADOW_END
;
44 for (i
= pgd_index(start
); start
< end
; i
++) {
45 pgd
[i
] = __pgd(__pa_nodebug(kasan_zero_pud
)
51 #ifdef CONFIG_KASAN_INLINE
52 static int kasan_die_handler(struct notifier_block
*self
,
57 pr_emerg("CONFIG_KASAN_INLINE enabled");
58 pr_emerg("GPF could be caused by NULL-ptr deref or user memory access");
63 static struct notifier_block kasan_die_notifier
= {
64 .notifier_call
= kasan_die_handler
,
68 void __init
kasan_early_init(void)
71 pteval_t pte_val
= __pa_nodebug(kasan_zero_page
) | __PAGE_KERNEL
;
72 pmdval_t pmd_val
= __pa_nodebug(kasan_zero_pte
) | _KERNPG_TABLE
;
73 pudval_t pud_val
= __pa_nodebug(kasan_zero_pmd
) | _KERNPG_TABLE
;
75 for (i
= 0; i
< PTRS_PER_PTE
; i
++)
76 kasan_zero_pte
[i
] = __pte(pte_val
);
78 for (i
= 0; i
< PTRS_PER_PMD
; i
++)
79 kasan_zero_pmd
[i
] = __pmd(pmd_val
);
81 for (i
= 0; i
< PTRS_PER_PUD
; i
++)
82 kasan_zero_pud
[i
] = __pud(pud_val
);
84 kasan_map_early_shadow(early_level4_pgt
);
85 kasan_map_early_shadow(init_level4_pgt
);
88 void __init
kasan_init(void)
92 #ifdef CONFIG_KASAN_INLINE
93 register_die_notifier(&kasan_die_notifier
);
96 memcpy(early_level4_pgt
, init_level4_pgt
, sizeof(early_level4_pgt
));
97 load_cr3(early_level4_pgt
);
100 clear_pgds(KASAN_SHADOW_START
, KASAN_SHADOW_END
);
102 kasan_populate_zero_shadow((void *)KASAN_SHADOW_START
,
103 kasan_mem_to_shadow((void *)PAGE_OFFSET
));
105 for (i
= 0; i
< E820_X_MAX
; i
++) {
106 if (pfn_mapped
[i
].end
== 0)
109 if (map_range(&pfn_mapped
[i
]))
110 panic("kasan: unable to allocate shadow!");
112 kasan_populate_zero_shadow(
113 kasan_mem_to_shadow((void *)PAGE_OFFSET
+ MAXMEM
),
114 kasan_mem_to_shadow((void *)__START_KERNEL_map
));
116 vmemmap_populate((unsigned long)kasan_mem_to_shadow(_stext
),
117 (unsigned long)kasan_mem_to_shadow(_end
),
120 kasan_populate_zero_shadow(kasan_mem_to_shadow((void *)MODULES_END
),
121 (void *)KASAN_SHADOW_END
);
123 memset(kasan_zero_page
, 0, PAGE_SIZE
);
125 load_cr3(init_level4_pgt
);
127 init_task
.kasan_depth
= 0;
129 pr_info("KernelAddressSanitizer initialized\n");