18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Xtensa KASAN shadow map initialization
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
58c2ecf20Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
68c2ecf20Sopenharmony_ci * for more details.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright (C) 2017 Cadence Design Systems Inc.
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/memblock.h>
128c2ecf20Sopenharmony_ci#include <linux/init_task.h>
138c2ecf20Sopenharmony_ci#include <linux/kasan.h>
148c2ecf20Sopenharmony_ci#include <linux/kernel.h>
158c2ecf20Sopenharmony_ci#include <asm/initialize_mmu.h>
168c2ecf20Sopenharmony_ci#include <asm/tlbflush.h>
178c2ecf20Sopenharmony_ci#include <asm/traps.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_civoid __init kasan_early_init(void)
208c2ecf20Sopenharmony_ci{
218c2ecf20Sopenharmony_ci	unsigned long vaddr = KASAN_SHADOW_START;
228c2ecf20Sopenharmony_ci	pmd_t *pmd = pmd_off_k(vaddr);
238c2ecf20Sopenharmony_ci	int i;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	for (i = 0; i < PTRS_PER_PTE; ++i)
268c2ecf20Sopenharmony_ci		set_pte(kasan_early_shadow_pte + i,
278c2ecf20Sopenharmony_ci			mk_pte(virt_to_page(kasan_early_shadow_page),
288c2ecf20Sopenharmony_ci				PAGE_KERNEL));
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	for (vaddr = 0; vaddr < KASAN_SHADOW_SIZE; vaddr += PMD_SIZE, ++pmd) {
318c2ecf20Sopenharmony_ci		BUG_ON(!pmd_none(*pmd));
328c2ecf20Sopenharmony_ci		set_pmd(pmd, __pmd((unsigned long)kasan_early_shadow_pte));
338c2ecf20Sopenharmony_ci	}
348c2ecf20Sopenharmony_ci	early_trap_init();
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic void __init populate(void *start, void *end)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	unsigned long n_pages = (end - start) / PAGE_SIZE;
408c2ecf20Sopenharmony_ci	unsigned long n_pmds = n_pages / PTRS_PER_PTE;
418c2ecf20Sopenharmony_ci	unsigned long i, j;
428c2ecf20Sopenharmony_ci	unsigned long vaddr = (unsigned long)start;
438c2ecf20Sopenharmony_ci	pmd_t *pmd = pmd_off_k(vaddr);
448c2ecf20Sopenharmony_ci	pte_t *pte = memblock_alloc(n_pages * sizeof(pte_t), PAGE_SIZE);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	if (!pte)
478c2ecf20Sopenharmony_ci		panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
488c2ecf20Sopenharmony_ci		      __func__, n_pages * sizeof(pte_t), PAGE_SIZE);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	pr_debug("%s: %p - %p\n", __func__, start, end);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	for (i = j = 0; i < n_pmds; ++i) {
538c2ecf20Sopenharmony_ci		int k;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci		for (k = 0; k < PTRS_PER_PTE; ++k, ++j) {
568c2ecf20Sopenharmony_ci			phys_addr_t phys =
578c2ecf20Sopenharmony_ci				memblock_phys_alloc_range(PAGE_SIZE, PAGE_SIZE,
588c2ecf20Sopenharmony_ci							  0,
598c2ecf20Sopenharmony_ci							  MEMBLOCK_ALLOC_ANYWHERE);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci			if (!phys)
628c2ecf20Sopenharmony_ci				panic("Failed to allocate page table page\n");
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci			set_pte(pte + j, pfn_pte(PHYS_PFN(phys), PAGE_KERNEL));
658c2ecf20Sopenharmony_ci		}
668c2ecf20Sopenharmony_ci	}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	for (i = 0; i < n_pmds ; ++i, pte += PTRS_PER_PTE)
698c2ecf20Sopenharmony_ci		set_pmd(pmd + i, __pmd((unsigned long)pte));
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	local_flush_tlb_all();
728c2ecf20Sopenharmony_ci	memset(start, 0, end - start);
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_civoid __init kasan_init(void)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	int i;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	BUILD_BUG_ON(KASAN_SHADOW_OFFSET != KASAN_SHADOW_START -
808c2ecf20Sopenharmony_ci		     (KASAN_START_VADDR >> KASAN_SHADOW_SCALE_SHIFT));
818c2ecf20Sopenharmony_ci	BUILD_BUG_ON(VMALLOC_START < KASAN_START_VADDR);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	/*
848c2ecf20Sopenharmony_ci	 * Replace shadow map pages that cover addresses from VMALLOC area
858c2ecf20Sopenharmony_ci	 * start to the end of KSEG with clean writable pages.
868c2ecf20Sopenharmony_ci	 */
878c2ecf20Sopenharmony_ci	populate(kasan_mem_to_shadow((void *)VMALLOC_START),
888c2ecf20Sopenharmony_ci		 kasan_mem_to_shadow((void *)XCHAL_KSEG_BYPASS_VADDR));
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	/*
918c2ecf20Sopenharmony_ci	 * Write protect kasan_early_shadow_page and zero-initialize it again.
928c2ecf20Sopenharmony_ci	 */
938c2ecf20Sopenharmony_ci	for (i = 0; i < PTRS_PER_PTE; ++i)
948c2ecf20Sopenharmony_ci		set_pte(kasan_early_shadow_pte + i,
958c2ecf20Sopenharmony_ci			mk_pte(virt_to_page(kasan_early_shadow_page),
968c2ecf20Sopenharmony_ci				PAGE_KERNEL_RO));
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	local_flush_tlb_all();
998c2ecf20Sopenharmony_ci	memset(kasan_early_shadow_page, 0, PAGE_SIZE);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	/* At this point kasan is fully initialized. Enable error messages. */
1028c2ecf20Sopenharmony_ci	current->kasan_depth = 0;
1038c2ecf20Sopenharmony_ci	pr_info("KernelAddressSanitizer initialized\n");
1048c2ecf20Sopenharmony_ci}
105