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