xref: /openbmc/linux/arch/powerpc/mm/kasan/8xx.c (revision ca15ca40)
1a2feeb2cSChristophe Leroy // SPDX-License-Identifier: GPL-2.0
2a2feeb2cSChristophe Leroy 
3a2feeb2cSChristophe Leroy #define DISABLE_BRANCH_PROFILING
4a2feeb2cSChristophe Leroy 
5a2feeb2cSChristophe Leroy #include <linux/kasan.h>
6a2feeb2cSChristophe Leroy #include <linux/memblock.h>
7a2feeb2cSChristophe Leroy #include <linux/hugetlb.h>
8a2feeb2cSChristophe Leroy 
9a2feeb2cSChristophe Leroy static int __init
kasan_init_shadow_8M(unsigned long k_start,unsigned long k_end,void * block)10a2feeb2cSChristophe Leroy kasan_init_shadow_8M(unsigned long k_start, unsigned long k_end, void *block)
11a2feeb2cSChristophe Leroy {
12e05c7b1fSMike Rapoport 	pmd_t *pmd = pmd_off_k(k_start);
13a2feeb2cSChristophe Leroy 	unsigned long k_cur, k_next;
14a2feeb2cSChristophe Leroy 
15a2feeb2cSChristophe Leroy 	for (k_cur = k_start; k_cur != k_end; k_cur = k_next, pmd += 2, block += SZ_8M) {
16a2feeb2cSChristophe Leroy 		pte_basic_t *new;
17a2feeb2cSChristophe Leroy 
18a2feeb2cSChristophe Leroy 		k_next = pgd_addr_end(k_cur, k_end);
19a2feeb2cSChristophe Leroy 		k_next = pgd_addr_end(k_next, k_end);
20a2feeb2cSChristophe Leroy 		if ((void *)pmd_page_vaddr(*pmd) != kasan_early_shadow_pte)
21a2feeb2cSChristophe Leroy 			continue;
22a2feeb2cSChristophe Leroy 
23a2feeb2cSChristophe Leroy 		new = memblock_alloc(sizeof(pte_basic_t), SZ_4K);
24a2feeb2cSChristophe Leroy 		if (!new)
25a2feeb2cSChristophe Leroy 			return -ENOMEM;
26a2feeb2cSChristophe Leroy 
27a2feeb2cSChristophe Leroy 		*new = pte_val(pte_mkhuge(pfn_pte(PHYS_PFN(__pa(block)), PAGE_KERNEL)));
28a2feeb2cSChristophe Leroy 
29a2feeb2cSChristophe Leroy 		hugepd_populate_kernel((hugepd_t *)pmd, (pte_t *)new, PAGE_SHIFT_8M);
30a2feeb2cSChristophe Leroy 		hugepd_populate_kernel((hugepd_t *)pmd + 1, (pte_t *)new, PAGE_SHIFT_8M);
31a2feeb2cSChristophe Leroy 	}
32a2feeb2cSChristophe Leroy 	return 0;
33a2feeb2cSChristophe Leroy }
34a2feeb2cSChristophe Leroy 
kasan_init_region(void * start,size_t size)35a2feeb2cSChristophe Leroy int __init kasan_init_region(void *start, size_t size)
36a2feeb2cSChristophe Leroy {
37a2feeb2cSChristophe Leroy 	unsigned long k_start = (unsigned long)kasan_mem_to_shadow(start);
38a2feeb2cSChristophe Leroy 	unsigned long k_end = (unsigned long)kasan_mem_to_shadow(start + size);
39a2feeb2cSChristophe Leroy 	unsigned long k_cur;
40a2feeb2cSChristophe Leroy 	int ret;
41a2feeb2cSChristophe Leroy 	void *block;
42a2feeb2cSChristophe Leroy 
43a2feeb2cSChristophe Leroy 	block = memblock_alloc(k_end - k_start, SZ_8M);
44a2feeb2cSChristophe Leroy 	if (!block)
45a2feeb2cSChristophe Leroy 		return -ENOMEM;
46a2feeb2cSChristophe Leroy 
47a2feeb2cSChristophe Leroy 	if (IS_ALIGNED(k_start, SZ_8M)) {
48a2feeb2cSChristophe Leroy 		kasan_init_shadow_8M(k_start, ALIGN_DOWN(k_end, SZ_8M), block);
49a2feeb2cSChristophe Leroy 		k_cur = ALIGN_DOWN(k_end, SZ_8M);
50a2feeb2cSChristophe Leroy 		if (k_cur == k_end)
51a2feeb2cSChristophe Leroy 			goto finish;
52a2feeb2cSChristophe Leroy 	} else {
53a2feeb2cSChristophe Leroy 		k_cur = k_start;
54a2feeb2cSChristophe Leroy 	}
55a2feeb2cSChristophe Leroy 
56a2feeb2cSChristophe Leroy 	ret = kasan_init_shadow_page_tables(k_start, k_end);
57a2feeb2cSChristophe Leroy 	if (ret)
58a2feeb2cSChristophe Leroy 		return ret;
59a2feeb2cSChristophe Leroy 
60a2feeb2cSChristophe Leroy 	for (; k_cur < k_end; k_cur += PAGE_SIZE) {
61e05c7b1fSMike Rapoport 		pmd_t *pmd = pmd_off_k(k_cur);
62a2feeb2cSChristophe Leroy 		void *va = block + k_cur - k_start;
63a2feeb2cSChristophe Leroy 		pte_t pte = pfn_pte(PHYS_PFN(__pa(va)), PAGE_KERNEL);
64a2feeb2cSChristophe Leroy 
65a2feeb2cSChristophe Leroy 		if (k_cur < ALIGN_DOWN(k_end, SZ_512K))
66a2feeb2cSChristophe Leroy 			pte = pte_mkhuge(pte);
67a2feeb2cSChristophe Leroy 
68a2feeb2cSChristophe Leroy 		__set_pte_at(&init_mm, k_cur, pte_offset_kernel(pmd, k_cur), pte, 0);
69a2feeb2cSChristophe Leroy 	}
70a2feeb2cSChristophe Leroy finish:
71a2feeb2cSChristophe Leroy 	flush_tlb_kernel_range(k_start, k_end);
72a2feeb2cSChristophe Leroy 	return 0;
73a2feeb2cSChristophe Leroy }
74