blob: 263b59020500f5e7e583cff2292e18c350104060 [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
100static void __init clear_pgds(unsigned long start,
101 unsigned long end)
102{
103 /*
104 * Remove references to kasan page tables from
105 * swapper_pg_dir. pgd_clear() can't be used
106 * here because it's nop on 2,3-level pagetable setups
107 */
108 for (; start < end; start += PGDIR_SIZE)
109 set_pgd(pgd_offset_k(start), __pgd(0));
110}
111
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300112void __init kasan_init(void)
113{
114 struct memblock_region *reg;
Ard Biesheuvel7b1af972016-01-11 14:50:21 +0100115 int i;
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300116
117 /*
118 * We are going to perform proper setup of shadow memory.
119 * At first we should unmap early shadow (clear_pgds() call bellow).
120 * However, instrumented code couldn't execute without shadow memory.
121 * tmp_pg_dir used to keep early shadow mapped until full shadow
122 * setup will be finished.
123 */
124 memcpy(tmp_pg_dir, swapper_pg_dir, sizeof(tmp_pg_dir));
Mark Rutlandc1a88e92016-01-25 11:45:02 +0000125 dsb(ishst);
126 cpu_replace_ttbr1(tmp_pg_dir);
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300127
128 clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END);
129
130 kasan_populate_zero_shadow((void *)KASAN_SHADOW_START,
131 kasan_mem_to_shadow((void *)MODULES_VADDR));
132
133 for_each_memblock(memory, reg) {
134 void *start = (void *)__phys_to_virt(reg->base);
135 void *end = (void *)__phys_to_virt(reg->base + reg->size);
136
137 if (start >= end)
138 break;
139
140 /*
141 * end + 1 here is intentional. We check several shadow bytes in
142 * advance to slightly speed up fastpath. In some rare cases
143 * we could cross boundary of mapped shadow, so we just map
144 * some more here.
145 */
146 vmemmap_populate((unsigned long)kasan_mem_to_shadow(start),
147 (unsigned long)kasan_mem_to_shadow(end) + 1,
148 pfn_to_nid(virt_to_pfn(start)));
149 }
150
Ard Biesheuvel7b1af972016-01-11 14:50:21 +0100151 /*
152 * KAsan may reuse the contents of kasan_zero_pte directly, so we
153 * should make sure that it maps the zero page read-only.
154 */
155 for (i = 0; i < PTRS_PER_PTE; i++)
156 set_pte(&kasan_zero_pte[i],
157 pfn_pte(virt_to_pfn(kasan_zero_page), PAGE_KERNEL_RO));
158
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300159 memset(kasan_zero_page, 0, PAGE_SIZE);
Mark Rutlandc1a88e92016-01-25 11:45:02 +0000160 cpu_replace_ttbr1(swapper_pg_dir);
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300161
162 /* At this point kasan is fully initialized. Enable error messages */
163 init_task.kasan_depth = 0;
164 pr_info("KernelAddressSanitizer initialized\n");
165}