summaryrefslogtreecommitdiffstats
path: root/arch/powerpc/mm/kasan/book3s_32.c
blob: 450a67ef0bbe1eb4fc4f897e897f1ff24727f44c (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
// SPDX-License-Identifier: GPL-2.0

#define DISABLE_BRANCH_PROFILING

#include <linux/kasan.h>
#include <linux/memblock.h>
#include <mm/mmu_decl.h>

int __init kasan_init_region(void *start, size_t size)
{
	unsigned long k_start = (unsigned long)kasan_mem_to_shadow(start);
	unsigned long k_end = (unsigned long)kasan_mem_to_shadow(start + size);
	unsigned long k_nobat = k_start;
	unsigned long k_cur;
	phys_addr_t phys;
	int ret;

	while (k_nobat < k_end) {
		unsigned int k_size = bat_block_size(k_nobat, k_end);
		int idx = find_free_bat();

		if (idx == -1)
			break;
		if (k_size < SZ_128K)
			break;
		phys = memblock_phys_alloc_range(k_size, k_size, 0,
						 MEMBLOCK_ALLOC_ANYWHERE);
		if (!phys)
			break;

		setbat(idx, k_nobat, phys, k_size, PAGE_KERNEL);
		k_nobat += k_size;
	}
	if (k_nobat != k_start)
		update_bats();

	if (k_nobat < k_end) {
		phys = memblock_phys_alloc_range(k_end - k_nobat, PAGE_SIZE, 0,
						 MEMBLOCK_ALLOC_ANYWHERE);
		if (!phys)
			return -ENOMEM;
	}

	ret = kasan_init_shadow_page_tables(k_start, k_end);
	if (ret)
		return ret;

	kasan_update_early_region(k_start, k_nobat, __pte(0));

	for (k_cur = k_nobat; k_cur < k_end; k_cur += PAGE_SIZE) {
		pmd_t *pmd = pmd_off_k(k_cur);
		pte_t pte = pfn_pte(PHYS_PFN(phys + k_cur - k_nobat), PAGE_KERNEL);

		__set_pte_at(&init_mm, k_cur, pte_offset_kernel(pmd, k_cur), pte, 0);
	}
	flush_tlb_kernel_range(k_start, k_end);
	memset(kasan_mem_to_shadow(start), 0, k_end - k_start);

	return 0;
}