18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci 38c2ecf20Sopenharmony_ci#define DISABLE_BRANCH_PROFILING 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci#include <linux/kasan.h> 68c2ecf20Sopenharmony_ci#include <linux/memblock.h> 78c2ecf20Sopenharmony_ci#include <linux/hugetlb.h> 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_cistatic int __init 108c2ecf20Sopenharmony_cikasan_init_shadow_8M(unsigned long k_start, unsigned long k_end, void *block) 118c2ecf20Sopenharmony_ci{ 128c2ecf20Sopenharmony_ci pmd_t *pmd = pmd_off_k(k_start); 138c2ecf20Sopenharmony_ci unsigned long k_cur, k_next; 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci for (k_cur = k_start; k_cur != k_end; k_cur = k_next, pmd += 2, block += SZ_8M) { 168c2ecf20Sopenharmony_ci pte_basic_t *new; 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci k_next = pgd_addr_end(k_cur, k_end); 198c2ecf20Sopenharmony_ci k_next = pgd_addr_end(k_next, k_end); 208c2ecf20Sopenharmony_ci if ((void *)pmd_page_vaddr(*pmd) != kasan_early_shadow_pte) 218c2ecf20Sopenharmony_ci continue; 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci new = memblock_alloc(sizeof(pte_basic_t), SZ_4K); 248c2ecf20Sopenharmony_ci if (!new) 258c2ecf20Sopenharmony_ci return -ENOMEM; 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci *new = pte_val(pte_mkhuge(pfn_pte(PHYS_PFN(__pa(block)), PAGE_KERNEL))); 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci hugepd_populate_kernel((hugepd_t *)pmd, (pte_t *)new, PAGE_SHIFT_8M); 308c2ecf20Sopenharmony_ci hugepd_populate_kernel((hugepd_t *)pmd + 1, (pte_t *)new, PAGE_SHIFT_8M); 318c2ecf20Sopenharmony_ci } 328c2ecf20Sopenharmony_ci return 0; 338c2ecf20Sopenharmony_ci} 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ciint __init kasan_init_region(void *start, size_t size) 368c2ecf20Sopenharmony_ci{ 378c2ecf20Sopenharmony_ci unsigned long k_start = (unsigned long)kasan_mem_to_shadow(start); 388c2ecf20Sopenharmony_ci unsigned long k_end = (unsigned long)kasan_mem_to_shadow(start + size); 398c2ecf20Sopenharmony_ci unsigned long k_cur; 408c2ecf20Sopenharmony_ci int ret; 418c2ecf20Sopenharmony_ci void *block; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci block = memblock_alloc(k_end - k_start, SZ_8M); 448c2ecf20Sopenharmony_ci if (!block) 458c2ecf20Sopenharmony_ci return -ENOMEM; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci if (IS_ALIGNED(k_start, SZ_8M)) { 488c2ecf20Sopenharmony_ci kasan_init_shadow_8M(k_start, ALIGN_DOWN(k_end, SZ_8M), block); 498c2ecf20Sopenharmony_ci k_cur = ALIGN_DOWN(k_end, SZ_8M); 508c2ecf20Sopenharmony_ci if (k_cur == k_end) 518c2ecf20Sopenharmony_ci goto finish; 528c2ecf20Sopenharmony_ci } else { 538c2ecf20Sopenharmony_ci k_cur = k_start; 548c2ecf20Sopenharmony_ci } 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci ret = kasan_init_shadow_page_tables(k_start, k_end); 578c2ecf20Sopenharmony_ci if (ret) 588c2ecf20Sopenharmony_ci return ret; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci for (; k_cur < k_end; k_cur += PAGE_SIZE) { 618c2ecf20Sopenharmony_ci pmd_t *pmd = pmd_off_k(k_cur); 628c2ecf20Sopenharmony_ci void *va = block + k_cur - k_start; 638c2ecf20Sopenharmony_ci pte_t pte = pfn_pte(PHYS_PFN(__pa(va)), PAGE_KERNEL); 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci if (k_cur < ALIGN_DOWN(k_end, SZ_512K)) 668c2ecf20Sopenharmony_ci pte = pte_mkhuge(pte); 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci __set_pte_at(&init_mm, k_cur, pte_offset_kernel(pmd, k_cur), pte, 0); 698c2ecf20Sopenharmony_ci } 708c2ecf20Sopenharmony_cifinish: 718c2ecf20Sopenharmony_ci flush_tlb_kernel_range(k_start, k_end); 728c2ecf20Sopenharmony_ci return 0; 738c2ecf20Sopenharmony_ci} 74