xref: /openbmc/linux/arch/powerpc/mm/kasan/book3s_32.c (revision 82df5b73)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #define DISABLE_BRANCH_PROFILING
4 
5 #include <linux/kasan.h>
6 #include <linux/memblock.h>
7 #include <asm/pgalloc.h>
8 #include <mm/mmu_decl.h>
9 
10 int __init kasan_init_region(void *start, size_t size)
11 {
12 	unsigned long k_start = (unsigned long)kasan_mem_to_shadow(start);
13 	unsigned long k_end = (unsigned long)kasan_mem_to_shadow(start + size);
14 	unsigned long k_cur = k_start;
15 	int k_size = k_end - k_start;
16 	int k_size_base = 1 << (ffs(k_size) - 1);
17 	int ret;
18 	void *block;
19 
20 	block = memblock_alloc(k_size, k_size_base);
21 
22 	if (block && k_size_base >= SZ_128K && k_start == ALIGN(k_start, k_size_base)) {
23 		int k_size_more = 1 << (ffs(k_size - k_size_base) - 1);
24 
25 		setbat(-1, k_start, __pa(block), k_size_base, PAGE_KERNEL);
26 		if (k_size_more >= SZ_128K)
27 			setbat(-1, k_start + k_size_base, __pa(block) + k_size_base,
28 			       k_size_more, PAGE_KERNEL);
29 		if (v_block_mapped(k_start))
30 			k_cur = k_start + k_size_base;
31 		if (v_block_mapped(k_start + k_size_base))
32 			k_cur = k_start + k_size_base + k_size_more;
33 
34 		update_bats();
35 	}
36 
37 	if (!block)
38 		block = memblock_alloc(k_size, PAGE_SIZE);
39 	if (!block)
40 		return -ENOMEM;
41 
42 	ret = kasan_init_shadow_page_tables(k_start, k_end);
43 	if (ret)
44 		return ret;
45 
46 	kasan_update_early_region(k_start, k_cur, __pte(0));
47 
48 	for (; k_cur < k_end; k_cur += PAGE_SIZE) {
49 		pmd_t *pmd = pmd_off_k(k_cur);
50 		void *va = block + k_cur - k_start;
51 		pte_t pte = pfn_pte(PHYS_PFN(__pa(va)), PAGE_KERNEL);
52 
53 		__set_pte_at(&init_mm, k_cur, pte_offset_kernel(pmd, k_cur), pte, 0);
54 	}
55 	flush_tlb_kernel_range(k_start, k_end);
56 	return 0;
57 }
58