summaryrefslogtreecommitdiffstats
path: root/arch/powerpc/mm/kasan/init_book3s_64.c
blob: 9300d641cf9ae850865007bafeef382027e2dcb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// SPDX-License-Identifier: GPL-2.0
/*
 * KASAN for 64-bit Book3S powerpc
 *
 * Copyright 2019-2022, Daniel Axtens, IBM Corporation.
 */

/*
 * ppc64 turns on virtual memory late in boot, after calling into generic code
 * like the device-tree parser, so it uses this in conjunction with a hook in
 * outline mode to avoid invalid access early in boot.
 */

#define DISABLE_BRANCH_PROFILING

#include <linux/kasan.h>
#include <linux/printk.h>
#include <linux/sched/task.h>
#include <linux/memblock.h>
#include <asm/pgalloc.h>

DEFINE_STATIC_KEY_FALSE(powerpc_kasan_enabled_key);

static void __init kasan_init_phys_region(void *start, void *end)
{
	unsigned long k_start, k_end, k_cur;
	void *va;

	if (start >= end)
		return;

	k_start = ALIGN_DOWN((unsigned long)kasan_mem_to_shadow(start), PAGE_SIZE);
	k_end = ALIGN((unsigned long)kasan_mem_to_shadow(end), PAGE_SIZE);

	va = memblock_alloc(k_end - k_start, PAGE_SIZE);
	for (k_cur = k_start; k_cur < k_end; k_cur += PAGE_SIZE, va += PAGE_SIZE)
		map_kernel_page(k_cur, __pa(va), PAGE_KERNEL);
}

void __init kasan_init(void)
{
	/*
	 * We want to do the following things:
	 *  1) Map real memory into the shadow for all physical memblocks
	 *     This takes us from c000... to c008...
	 *  2) Leave a hole over the shadow of vmalloc space. KASAN_VMALLOC
	 *     will manage this for us.
	 *     This takes us from c008... to c00a...
	 *  3) Map the 'early shadow'/zero page over iomap and vmemmap space.
	 *     This takes us up to where we start at c00e...
	 */

	void *k_start = kasan_mem_to_shadow((void *)RADIX_VMALLOC_END);
	void *k_end = kasan_mem_to_shadow((void *)RADIX_VMEMMAP_END);
	phys_addr_t start, end;
	u64 i;
	pte_t zero_pte = pfn_pte(virt_to_pfn(kasan_early_shadow_page), PAGE_KERNEL);

	if (!early_radix_enabled()) {
		pr_warn("KASAN not enabled as it requires radix!");
		return;
	}

	for_each_mem_range(i, &start, &end)
		kasan_init_phys_region((void *)start, (void *)end);

	for (i = 0; i < PTRS_PER_PTE; i++)
		__set_pte_at(&init_mm, (unsigned long)kasan_early_shadow_page,
			     &kasan_early_shadow_pte[i], zero_pte, 0);

	for (i = 0; i < PTRS_PER_PMD; i++)
		pmd_populate_kernel(&init_mm, &kasan_early_shadow_pmd[i],
				    kasan_early_shadow_pte);

	for (i = 0; i < PTRS_PER_PUD; i++)
		pud_populate(&init_mm, &kasan_early_shadow_pud[i],
			     kasan_early_shadow_pmd);

	/* map the early shadow over the iomap and vmemmap space */
	kasan_populate_early_shadow(k_start, k_end);

	/* mark early shadow region as RO and wipe it */
	zero_pte = pfn_pte(virt_to_pfn(kasan_early_shadow_page), PAGE_KERNEL_RO);
	for (i = 0; i < PTRS_PER_PTE; i++)
		__set_pte_at(&init_mm, (unsigned long)kasan_early_shadow_page,
			     &kasan_early_shadow_pte[i], zero_pte, 0);

	/*
	 * clear_page relies on some cache info that hasn't been set up yet.
	 * It ends up looping ~forever and blows up other data.
	 * Use memset instead.
	 */
	memset(kasan_early_shadow_page, 0, PAGE_SIZE);

	static_branch_inc(&powerpc_kasan_enabled_key);

	/* Enable error messages */
	init_task.kasan_depth = 0;
	pr_info("KASAN init done\n");
}

void __init kasan_early_init(void) { }

void __init kasan_late_init(void) { }