gitlab.arm.com will be in the maintainance mode on Wednesday June 29th 01:00 - 10:00 (UTC+1). Repositories is read only during the maintainance.

kasan_init.c 7.71 KB
Newer Older
Andrey Ryabinin's avatar
Andrey Ryabinin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
 * This file contains kasan initialization code for ARM64.
 *
 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#define pr_fmt(fmt) "kasan: " fmt
#include <linux/kasan.h>
#include <linux/kernel.h>
16
#include <linux/sched/task.h>
Andrey Ryabinin's avatar
Andrey Ryabinin committed
17
18
#include <linux/memblock.h>
#include <linux/start_kernel.h>
19
#include <linux/mm.h>
Andrey Ryabinin's avatar
Andrey Ryabinin committed
20

21
#include <asm/mmu_context.h>
22
#include <asm/kernel-pgtable.h>
Andrey Ryabinin's avatar
Andrey Ryabinin committed
23
24
25
#include <asm/page.h>
#include <asm/pgalloc.h>
#include <asm/pgtable.h>
26
#include <asm/sections.h>
Andrey Ryabinin's avatar
Andrey Ryabinin committed
27
28
29
30
#include <asm/tlbflush.h>

static pgd_t tmp_pg_dir[PTRS_PER_PGD] __initdata __aligned(PGD_SIZE);

31
32
33
34
35
36
37
/*
 * The p*d_populate functions call virt_to_phys implicitly so they can't be used
 * directly on kernel symbols (bm_p*d). All the early functions are called too
 * early to use lm_alias so __p*d_populate functions must be used to populate
 * with the physical address from __pa_symbol.
 */

38
static phys_addr_t __init kasan_alloc_zeroed_page(int node)
Andrey Ryabinin's avatar
Andrey Ryabinin committed
39
{
40
	void *p = memblock_alloc_try_nid(PAGE_SIZE, PAGE_SIZE,
41
42
43
44
45
					      __pa(MAX_DMA_ADDRESS),
					      MEMBLOCK_ALLOC_ACCESSIBLE, node);
	return __pa(p);
}

46
47
48
49
50
51
52
53
static phys_addr_t __init kasan_alloc_raw_page(int node)
{
	void *p = memblock_alloc_try_nid_raw(PAGE_SIZE, PAGE_SIZE,
						__pa(MAX_DMA_ADDRESS),
						MEMBLOCK_ALLOC_KASAN, node);
	return __pa(p);
}

54
static pte_t *__init kasan_pte_offset(pmd_t *pmdp, unsigned long addr, int node,
55
56
				      bool early)
{
57
	if (pmd_none(READ_ONCE(*pmdp))) {
58
59
60
		phys_addr_t pte_phys = early ?
				__pa_symbol(kasan_early_shadow_pte)
					: kasan_alloc_zeroed_page(node);
61
		__pmd_populate(pmdp, pte_phys, PMD_TYPE_TABLE);
62
63
	}

64
65
	return early ? pte_offset_kimg(pmdp, addr)
		     : pte_offset_kernel(pmdp, addr);
66
}
Andrey Ryabinin's avatar
Andrey Ryabinin committed
67

68
static pmd_t *__init kasan_pmd_offset(pud_t *pudp, unsigned long addr, int node,
69
70
				      bool early)
{
71
	if (pud_none(READ_ONCE(*pudp))) {
72
73
74
		phys_addr_t pmd_phys = early ?
				__pa_symbol(kasan_early_shadow_pmd)
					: kasan_alloc_zeroed_page(node);
75
		__pud_populate(pudp, pmd_phys, PMD_TYPE_TABLE);
76
77
	}

78
	return early ? pmd_offset_kimg(pudp, addr) : pmd_offset(pudp, addr);
79
80
}

81
static pud_t *__init kasan_pud_offset(pgd_t *pgdp, unsigned long addr, int node,
82
83
				      bool early)
{
84
	if (pgd_none(READ_ONCE(*pgdp))) {
85
86
87
		phys_addr_t pud_phys = early ?
				__pa_symbol(kasan_early_shadow_pud)
					: kasan_alloc_zeroed_page(node);
88
		__pgd_populate(pgdp, pud_phys, PMD_TYPE_TABLE);
89
90
	}

91
	return early ? pud_offset_kimg(pgdp, addr) : pud_offset(pgdp, addr);
92
93
}

94
static void __init kasan_pte_populate(pmd_t *pmdp, unsigned long addr,
95
96
97
				      unsigned long end, int node, bool early)
{
	unsigned long next;
98
	pte_t *ptep = kasan_pte_offset(pmdp, addr, node, early);
Andrey Ryabinin's avatar
Andrey Ryabinin committed
99
100

	do {
101
102
		phys_addr_t page_phys = early ?
				__pa_symbol(kasan_early_shadow_page)
103
104
105
					: kasan_alloc_raw_page(node);
		if (!early)
			memset(__va(page_phys), KASAN_SHADOW_INIT, PAGE_SIZE);
Andrey Ryabinin's avatar
Andrey Ryabinin committed
106
		next = addr + PAGE_SIZE;
107
108
		set_pte(ptep, pfn_pte(__phys_to_pfn(page_phys), PAGE_KERNEL));
	} while (ptep++, addr = next, addr != end && pte_none(READ_ONCE(*ptep)));
Andrey Ryabinin's avatar
Andrey Ryabinin committed
109
110
}

111
static void __init kasan_pmd_populate(pud_t *pudp, unsigned long addr,
112
				      unsigned long end, int node, bool early)
Andrey Ryabinin's avatar
Andrey Ryabinin committed
113
114
{
	unsigned long next;
115
	pmd_t *pmdp = kasan_pmd_offset(pudp, addr, node, early);
Andrey Ryabinin's avatar
Andrey Ryabinin committed
116
117
118

	do {
		next = pmd_addr_end(addr, end);
119
120
		kasan_pte_populate(pmdp, addr, next, node, early);
	} while (pmdp++, addr = next, addr != end && pmd_none(READ_ONCE(*pmdp)));
Andrey Ryabinin's avatar
Andrey Ryabinin committed
121
122
}

123
static void __init kasan_pud_populate(pgd_t *pgdp, unsigned long addr,
124
				      unsigned long end, int node, bool early)
Andrey Ryabinin's avatar
Andrey Ryabinin committed
125
126
{
	unsigned long next;
127
	pud_t *pudp = kasan_pud_offset(pgdp, addr, node, early);
Andrey Ryabinin's avatar
Andrey Ryabinin committed
128
129
130

	do {
		next = pud_addr_end(addr, end);
131
132
		kasan_pmd_populate(pudp, addr, next, node, early);
	} while (pudp++, addr = next, addr != end && pud_none(READ_ONCE(*pudp)));
Andrey Ryabinin's avatar
Andrey Ryabinin committed
133
134
}

135
136
static void __init kasan_pgd_populate(unsigned long addr, unsigned long end,
				      int node, bool early)
Andrey Ryabinin's avatar
Andrey Ryabinin committed
137
138
{
	unsigned long next;
139
	pgd_t *pgdp;
Andrey Ryabinin's avatar
Andrey Ryabinin committed
140

141
	pgdp = pgd_offset_k(addr);
Andrey Ryabinin's avatar
Andrey Ryabinin committed
142
143
	do {
		next = pgd_addr_end(addr, end);
144
145
		kasan_pud_populate(pgdp, addr, next, node, early);
	} while (pgdp++, addr = next, addr != end);
Andrey Ryabinin's avatar
Andrey Ryabinin committed
146
147
}

148
/* The early shadow maps everything to a single page of zeroes */
149
asmlinkage void __init kasan_early_init(void)
Andrey Ryabinin's avatar
Andrey Ryabinin committed
150
{
151
152
	BUILD_BUG_ON(KASAN_SHADOW_OFFSET !=
		KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT)));
Andrey Ryabinin's avatar
Andrey Ryabinin committed
153
154
	BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_START, PGDIR_SIZE));
	BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, PGDIR_SIZE));
155
156
157
158
159
160
161
162
163
	kasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE,
			   true);
}

/* Set up full kasan mappings, ensuring that the mapped pages are zeroed */
static void __init kasan_map_populate(unsigned long start, unsigned long end,
				      int node)
{
	kasan_pgd_populate(start & PAGE_MASK, PAGE_ALIGN(end), node, false);
Andrey Ryabinin's avatar
Andrey Ryabinin committed
164
165
}

166
167
168
169
170
/*
 * Copy the current shadow region into a new pgdir.
 */
void __init kasan_copy_shadow(pgd_t *pgdir)
{
171
	pgd_t *pgdp, *pgdp_new, *pgdp_end;
172

173
174
175
	pgdp = pgd_offset_k(KASAN_SHADOW_START);
	pgdp_end = pgd_offset_k(KASAN_SHADOW_END);
	pgdp_new = pgd_offset_raw(pgdir, KASAN_SHADOW_START);
176
	do {
177
178
		set_pgd(pgdp_new, READ_ONCE(*pgdp));
	} while (pgdp++, pgdp_new++, pgdp != pgdp_end);
179
180
}

Andrey Ryabinin's avatar
Andrey Ryabinin committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
static void __init clear_pgds(unsigned long start,
			unsigned long end)
{
	/*
	 * Remove references to kasan page tables from
	 * swapper_pg_dir. pgd_clear() can't be used
	 * here because it's nop on 2,3-level pagetable setups
	 */
	for (; start < end; start += PGDIR_SIZE)
		set_pgd(pgd_offset_k(start), __pgd(0));
}

void __init kasan_init(void)
{
195
	u64 kimg_shadow_start, kimg_shadow_end;
196
	u64 mod_shadow_start, mod_shadow_end;
Andrey Ryabinin's avatar
Andrey Ryabinin committed
197
	struct memblock_region *reg;
198
	int i;
Andrey Ryabinin's avatar
Andrey Ryabinin committed
199

200
201
	kimg_shadow_start = (u64)kasan_mem_to_shadow(_text) & PAGE_MASK;
	kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(_end));
202

203
204
205
	mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
	mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);

Andrey Ryabinin's avatar
Andrey Ryabinin committed
206
207
	/*
	 * We are going to perform proper setup of shadow memory.
208
	 * At first we should unmap early shadow (clear_pgds() call below).
Andrey Ryabinin's avatar
Andrey Ryabinin committed
209
210
211
212
213
	 * However, instrumented code couldn't execute without shadow memory.
	 * tmp_pg_dir used to keep early shadow mapped until full shadow
	 * setup will be finished.
	 */
	memcpy(tmp_pg_dir, swapper_pg_dir, sizeof(tmp_pg_dir));
214
	dsb(ishst);
215
	cpu_replace_ttbr1(lm_alias(tmp_pg_dir));
Andrey Ryabinin's avatar
Andrey Ryabinin committed
216
217
218

	clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END);

219
	kasan_map_populate(kimg_shadow_start, kimg_shadow_end,
220
			   early_pfn_to_nid(virt_to_pfn(lm_alias(_text))));
221

222
223
224
225
	kasan_populate_early_shadow((void *)KASAN_SHADOW_START,
				    (void *)mod_shadow_start);
	kasan_populate_early_shadow((void *)kimg_shadow_end,
				    kasan_mem_to_shadow((void *)PAGE_OFFSET));
226
227

	if (kimg_shadow_start > mod_shadow_end)
228
229
		kasan_populate_early_shadow((void *)mod_shadow_end,
					    (void *)kimg_shadow_start);
Andrey Ryabinin's avatar
Andrey Ryabinin committed
230
231
232
233
234
235
236
237

	for_each_memblock(memory, reg) {
		void *start = (void *)__phys_to_virt(reg->base);
		void *end = (void *)__phys_to_virt(reg->base + reg->size);

		if (start >= end)
			break;

238
239
		kasan_map_populate((unsigned long)kasan_mem_to_shadow(start),
				   (unsigned long)kasan_mem_to_shadow(end),
240
				   early_pfn_to_nid(virt_to_pfn(start)));
Andrey Ryabinin's avatar
Andrey Ryabinin committed
241
242
	}

243
	/*
244
245
	 * KAsan may reuse the contents of kasan_early_shadow_pte directly,
	 * so we should make sure that it maps the zero page read-only.
246
247
	 */
	for (i = 0; i < PTRS_PER_PTE; i++)
248
249
250
		set_pte(&kasan_early_shadow_pte[i],
			pfn_pte(sym_to_pfn(kasan_early_shadow_page),
				PAGE_KERNEL_RO));
251

252
	memset(kasan_early_shadow_page, KASAN_SHADOW_INIT, PAGE_SIZE);
253
	cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
Andrey Ryabinin's avatar
Andrey Ryabinin committed
254
255
256
257
258

	/* At this point kasan is fully initialized. Enable error messages */
	init_task.kasan_depth = 0;
	pr_info("KernelAddressSanitizer initialized\n");
}