blob: cc569a38bc76fa40fa02fdded8438772a0cc1e0a [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>
16#include <linux/memblock.h>
17#include <linux/start_kernel.h>
18
Mark Rutlandc1a88e92016-01-25 11:45:02 +000019#include <asm/mmu_context.h>
Andrey Ryabinin39d114d2015-10-12 18:52:58 +030020#include <asm/page.h>
21#include <asm/pgalloc.h>
22#include <asm/pgtable.h>
23#include <asm/tlbflush.h>
24
25static pgd_t tmp_pg_dir[PTRS_PER_PGD] __initdata __aligned(PGD_SIZE);
26
27static void __init kasan_early_pte_populate(pmd_t *pmd, unsigned long addr,
28 unsigned long end)
29{
30 pte_t *pte;
31 unsigned long next;
32
33 if (pmd_none(*pmd))
34 pmd_populate_kernel(&init_mm, pmd, kasan_zero_pte);
35
36 pte = pte_offset_kernel(pmd, addr);
37 do {
38 next = addr + PAGE_SIZE;
39 set_pte(pte, pfn_pte(virt_to_pfn(kasan_zero_page),
40 PAGE_KERNEL));
41 } while (pte++, addr = next, addr != end && pte_none(*pte));
42}
43
44static void __init kasan_early_pmd_populate(pud_t *pud,
45 unsigned long addr,
46 unsigned long end)
47{
48 pmd_t *pmd;
49 unsigned long next;
50
51 if (pud_none(*pud))
52 pud_populate(&init_mm, pud, kasan_zero_pmd);
53
54 pmd = pmd_offset(pud, addr);
55 do {
56 next = pmd_addr_end(addr, end);
57 kasan_early_pte_populate(pmd, addr, next);
58 } while (pmd++, addr = next, addr != end && pmd_none(*pmd));
59}
60
61static void __init kasan_early_pud_populate(pgd_t *pgd,
62 unsigned long addr,
63 unsigned long end)
64{
65 pud_t *pud;
66 unsigned long next;
67
68 if (pgd_none(*pgd))
69 pgd_populate(&init_mm, pgd, kasan_zero_pud);
70
71 pud = pud_offset(pgd, addr);
72 do {
73 next = pud_addr_end(addr, end);
74 kasan_early_pmd_populate(pud, addr, next);
75 } while (pud++, addr = next, addr != end && pud_none(*pud));
76}
77
78static void __init kasan_map_early_shadow(void)
79{
80 unsigned long addr = KASAN_SHADOW_START;
81 unsigned long end = KASAN_SHADOW_END;
82 unsigned long next;
83 pgd_t *pgd;
84
85 pgd = pgd_offset_k(addr);
86 do {
87 next = pgd_addr_end(addr, end);
88 kasan_early_pud_populate(pgd, addr, next);
89 } while (pgd++, addr = next, addr != end);
90}
91
Will Deacon83040122015-10-13 14:01:06 +010092asmlinkage void __init kasan_early_init(void)
Andrey Ryabinin39d114d2015-10-12 18:52:58 +030093{
94 BUILD_BUG_ON(KASAN_SHADOW_OFFSET != KASAN_SHADOW_END - (1UL << 61));
95 BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_START, PGDIR_SIZE));
96 BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, PGDIR_SIZE));
97 kasan_map_early_shadow();
98}
99
Mark Rutland068a17a2016-01-25 11:45:12 +0000100/*
101 * Copy the current shadow region into a new pgdir.
102 */
103void __init kasan_copy_shadow(pgd_t *pgdir)
104{
105 pgd_t *pgd, *pgd_new, *pgd_end;
106
107 pgd = pgd_offset_k(KASAN_SHADOW_START);
108 pgd_end = pgd_offset_k(KASAN_SHADOW_END);
109 pgd_new = pgd_offset_raw(pgdir, KASAN_SHADOW_START);
110 do {
111 set_pgd(pgd_new, *pgd);
112 } while (pgd++, pgd_new++, pgd != pgd_end);
113}
114
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300115static void __init clear_pgds(unsigned long start,
116 unsigned long end)
117{
118 /*
119 * Remove references to kasan page tables from
120 * swapper_pg_dir. pgd_clear() can't be used
121 * here because it's nop on 2,3-level pagetable setups
122 */
123 for (; start < end; start += PGDIR_SIZE)
124 set_pgd(pgd_offset_k(start), __pgd(0));
125}
126
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300127void __init kasan_init(void)
128{
129 struct memblock_region *reg;
Ard Biesheuvel7b1af972016-01-11 14:50:21 +0100130 int i;
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300131
132 /*
133 * We are going to perform proper setup of shadow memory.
134 * At first we should unmap early shadow (clear_pgds() call bellow).
135 * However, instrumented code couldn't execute without shadow memory.
136 * tmp_pg_dir used to keep early shadow mapped until full shadow
137 * setup will be finished.
138 */
139 memcpy(tmp_pg_dir, swapper_pg_dir, sizeof(tmp_pg_dir));
Mark Rutlandc1a88e92016-01-25 11:45:02 +0000140 dsb(ishst);
141 cpu_replace_ttbr1(tmp_pg_dir);
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300142
143 clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END);
144
145 kasan_populate_zero_shadow((void *)KASAN_SHADOW_START,
146 kasan_mem_to_shadow((void *)MODULES_VADDR));
147
148 for_each_memblock(memory, reg) {
149 void *start = (void *)__phys_to_virt(reg->base);
150 void *end = (void *)__phys_to_virt(reg->base + reg->size);
151
152 if (start >= end)
153 break;
154
155 /*
156 * end + 1 here is intentional. We check several shadow bytes in
157 * advance to slightly speed up fastpath. In some rare cases
158 * we could cross boundary of mapped shadow, so we just map
159 * some more here.
160 */
161 vmemmap_populate((unsigned long)kasan_mem_to_shadow(start),
162 (unsigned long)kasan_mem_to_shadow(end) + 1,
163 pfn_to_nid(virt_to_pfn(start)));
164 }
165
Ard Biesheuvel7b1af972016-01-11 14:50:21 +0100166 /*
167 * KAsan may reuse the contents of kasan_zero_pte directly, so we
168 * should make sure that it maps the zero page read-only.
169 */
170 for (i = 0; i < PTRS_PER_PTE; i++)
171 set_pte(&kasan_zero_pte[i],
172 pfn_pte(virt_to_pfn(kasan_zero_page), PAGE_KERNEL_RO));
173
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300174 memset(kasan_zero_page, 0, PAGE_SIZE);
Mark Rutlandc1a88e92016-01-25 11:45:02 +0000175 cpu_replace_ttbr1(swapper_pg_dir);
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300176
177 /* At this point kasan is fully initialized. Enable error messages */
178 init_task.kasan_depth = 0;
179 pr_info("KernelAddressSanitizer initialized\n");
180}