xref: /linux/arch/powerpc/mm/kasan/book3s_32.c (revision e7d759f31ca295d589f7420719c311870bb3166f)
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 <mm/mmu_decl.h>
8 
9 int __init kasan_init_region(void *start, size_t size)
10 {
11 	unsigned long k_start = (unsigned long)kasan_mem_to_shadow(start);
12 	unsigned long k_end = (unsigned long)kasan_mem_to_shadow(start + size);
13 	unsigned long k_nobat = k_start;
14 	unsigned long k_cur;
15 	phys_addr_t phys;
16 	int ret;
17 
18 	while (k_nobat < k_end) {
19 		unsigned int k_size = bat_block_size(k_nobat, k_end);
20 		int idx = find_free_bat();
21 
22 		if (idx == -1)
23 			break;
24 		if (k_size < SZ_128K)
25 			break;
26 		phys = memblock_phys_alloc_range(k_size, k_size, 0,
27 						 MEMBLOCK_ALLOC_ANYWHERE);
28 		if (!phys)
29 			break;
30 
31 		setbat(idx, k_nobat, phys, k_size, PAGE_KERNEL);
32 		k_nobat += k_size;
33 	}
34 	if (k_nobat != k_start)
35 		update_bats();
36 
37 	if (k_nobat < k_end) {
38 		phys = memblock_phys_alloc_range(k_end - k_nobat, PAGE_SIZE, 0,
39 						 MEMBLOCK_ALLOC_ANYWHERE);
40 		if (!phys)
41 			return -ENOMEM;
42 	}
43 
44 	ret = kasan_init_shadow_page_tables(k_start, k_end);
45 	if (ret)
46 		return ret;
47 
48 	kasan_update_early_region(k_start, k_nobat, __pte(0));
49 
50 	for (k_cur = k_nobat; k_cur < k_end; k_cur += PAGE_SIZE) {
51 		pmd_t *pmd = pmd_off_k(k_cur);
52 		pte_t pte = pfn_pte(PHYS_PFN(phys + k_cur - k_nobat), PAGE_KERNEL);
53 
54 		__set_pte_at(&init_mm, k_cur, pte_offset_kernel(pmd, k_cur), pte, 0);
55 	}
56 	flush_tlb_kernel_range(k_start, k_end);
57 	memset(kasan_mem_to_shadow(start), 0, k_end - k_start);
58 
59 	return 0;
60 }
61