18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2020 Loongson Technology Corporation Limited
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "kasan: " fmt
68c2ecf20Sopenharmony_ci#include <linux/kasan.h>
78c2ecf20Sopenharmony_ci#include <linux/memblock.h>
88c2ecf20Sopenharmony_ci#include <linux/sched/task.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <asm/pgalloc.h>
118c2ecf20Sopenharmony_ci#include <asm/tlbflush.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistatic pgd_t kasan_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define __pgd_none(early, pgd) (early ? (pgd_val(pgd) == 0) : \
168c2ecf20Sopenharmony_ci(__pa(pgd_val(pgd)) == (unsigned long)__pa(kasan_early_shadow_pmd)))
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define __pmd_none(early, pmd) (early ? (pmd_val(pmd) == 0) : \
198c2ecf20Sopenharmony_ci(__pa(pmd_val(pmd)) == (unsigned long)__pa(kasan_early_shadow_pte)))
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define __pte_none(early, pte) (early ? pte_none(pte) : \
228c2ecf20Sopenharmony_ci((pte_val(pte) & _PFN_MASK) == (unsigned long)__pa(kasan_early_shadow_page)))
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cibool kasan_early_stage = true;
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_civoid *kasan_mem_to_shadow(const void *addr)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	if (kasan_early_stage) {
298c2ecf20Sopenharmony_ci		return (void *)(kasan_early_shadow_page);
308c2ecf20Sopenharmony_ci	} else {
318c2ecf20Sopenharmony_ci		unsigned long maddr = (unsigned long)addr;
328c2ecf20Sopenharmony_ci		unsigned long xrange = (maddr >> XRANGE_SHIFT) & 0xffff;
338c2ecf20Sopenharmony_ci		unsigned long offset = 0;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci		if (maddr >= FIXADDR_START)
368c2ecf20Sopenharmony_ci			return (void *)(kasan_early_shadow_page);
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci		maddr &= XRANGE_SHADOW_MASK;
398c2ecf20Sopenharmony_ci		switch (xrange) {
408c2ecf20Sopenharmony_ci		case XKPRANGE_CC_SEG:
418c2ecf20Sopenharmony_ci			offset = XKPRANGE_CC_SHADOW_OFFSET;
428c2ecf20Sopenharmony_ci			break;
438c2ecf20Sopenharmony_ci		case XKPRANGE_UC_SEG:
448c2ecf20Sopenharmony_ci			offset = XKPRANGE_UC_SHADOW_OFFSET;
458c2ecf20Sopenharmony_ci			break;
468c2ecf20Sopenharmony_ci		case XKVRANGE_VC_SEG:
478c2ecf20Sopenharmony_ci			offset = XKVRANGE_VC_SHADOW_OFFSET;
488c2ecf20Sopenharmony_ci			break;
498c2ecf20Sopenharmony_ci		default:
508c2ecf20Sopenharmony_ci			WARN_ON(1);
518c2ecf20Sopenharmony_ci			return NULL;
528c2ecf20Sopenharmony_ci		}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci		return (void *)((maddr >> KASAN_SHADOW_SCALE_SHIFT) + offset);
558c2ecf20Sopenharmony_ci	}
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ciconst void *kasan_shadow_to_mem(const void *shadow_addr)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	unsigned long addr = (unsigned long)shadow_addr;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	if (unlikely(addr > KASAN_SHADOW_END) ||
638c2ecf20Sopenharmony_ci		unlikely(addr < KASAN_SHADOW_START)) {
648c2ecf20Sopenharmony_ci		WARN_ON(1);
658c2ecf20Sopenharmony_ci		return NULL;
668c2ecf20Sopenharmony_ci	}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	if (addr >= XKVRANGE_VC_SHADOW_OFFSET)
698c2ecf20Sopenharmony_ci		return (void *)(((addr - XKVRANGE_VC_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT) + XKVRANGE_VC_START);
708c2ecf20Sopenharmony_ci	else if (addr >= XKPRANGE_UC_SHADOW_OFFSET)
718c2ecf20Sopenharmony_ci		return (void *)(((addr - XKPRANGE_UC_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT) + XKPRANGE_UC_START);
728c2ecf20Sopenharmony_ci	else if (addr >= XKPRANGE_CC_SHADOW_OFFSET)
738c2ecf20Sopenharmony_ci		return (void *)(((addr - XKPRANGE_CC_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT) + XKPRANGE_CC_START);
748c2ecf20Sopenharmony_ci	else {
758c2ecf20Sopenharmony_ci		WARN_ON(1);
768c2ecf20Sopenharmony_ci		return NULL;
778c2ecf20Sopenharmony_ci	}
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci/*
818c2ecf20Sopenharmony_ci * Alloc memory for shadow memory page table.
828c2ecf20Sopenharmony_ci */
838c2ecf20Sopenharmony_cistatic phys_addr_t __init kasan_alloc_zeroed_page(int node)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	void *p = memblock_alloc_try_nid(PAGE_SIZE, PAGE_SIZE,
868c2ecf20Sopenharmony_ci					__pa(MAX_DMA_ADDRESS),
878c2ecf20Sopenharmony_ci					MEMBLOCK_ALLOC_ACCESSIBLE, node);
888c2ecf20Sopenharmony_ci	return __pa(p);
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic pte_t *kasan_pte_offset(pmd_t *pmdp, unsigned long addr, int node,
928c2ecf20Sopenharmony_ci				      bool early)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	if (__pmd_none(early, READ_ONCE(*pmdp))) {
958c2ecf20Sopenharmony_ci		phys_addr_t pte_phys = early ?
968c2ecf20Sopenharmony_ci				__pa_symbol(kasan_early_shadow_pte)
978c2ecf20Sopenharmony_ci					: kasan_alloc_zeroed_page(node);
988c2ecf20Sopenharmony_ci		if (!early)
998c2ecf20Sopenharmony_ci			memcpy(__va(pte_phys), kasan_early_shadow_pte,
1008c2ecf20Sopenharmony_ci				sizeof(kasan_early_shadow_pte));
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci		pmd_populate_kernel(NULL, pmdp, (pte_t *)__va(pte_phys));
1038c2ecf20Sopenharmony_ci	}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	return pte_offset_kernel(pmdp, addr);
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic inline void kasan_set_pgd(pgd_t *pgdp, pgd_t pgdval)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	WRITE_ONCE(*pgdp, pgdval);
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic pmd_t *kasan_pmd_offset(pgd_t *pgdp, unsigned long addr, int node,
1148c2ecf20Sopenharmony_ci				      bool early)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	if (__pgd_none(early, READ_ONCE(*pgdp))) {
1178c2ecf20Sopenharmony_ci		phys_addr_t pmd_phys = early ?
1188c2ecf20Sopenharmony_ci				__pa_symbol(kasan_early_shadow_pmd)
1198c2ecf20Sopenharmony_ci					: kasan_alloc_zeroed_page(node);
1208c2ecf20Sopenharmony_ci		if (!early)
1218c2ecf20Sopenharmony_ci			memcpy(__va(pmd_phys), kasan_early_shadow_pmd,
1228c2ecf20Sopenharmony_ci				sizeof(kasan_early_shadow_pmd));
1238c2ecf20Sopenharmony_ci		kasan_set_pgd(pgdp, __pgd((unsigned long)__va(pmd_phys)));
1248c2ecf20Sopenharmony_ci	}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	return (pmd_t *)((pmd_t *)pgd_val(*pgdp) + pmd_index(addr));
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic void  kasan_pte_populate(pmd_t *pmdp, unsigned long addr,
1308c2ecf20Sopenharmony_ci				      unsigned long end, int node, bool early)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	unsigned long next;
1338c2ecf20Sopenharmony_ci	pte_t *ptep = kasan_pte_offset(pmdp, addr, node, early);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	do {
1368c2ecf20Sopenharmony_ci		phys_addr_t page_phys = early ?
1378c2ecf20Sopenharmony_ci					__pa_symbol(kasan_early_shadow_page)
1388c2ecf20Sopenharmony_ci					      : kasan_alloc_zeroed_page(node);
1398c2ecf20Sopenharmony_ci		next = addr + PAGE_SIZE;
1408c2ecf20Sopenharmony_ci		set_pte(ptep, pfn_pte(__phys_to_pfn(page_phys), PAGE_KERNEL));
1418c2ecf20Sopenharmony_ci	} while (ptep++, addr = next, addr != end && __pte_none(early, READ_ONCE(*ptep)));
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic void kasan_pmd_populate(pgd_t *pgdp, unsigned long addr,
1458c2ecf20Sopenharmony_ci				      unsigned long end, int node, bool early)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	unsigned long next;
1488c2ecf20Sopenharmony_ci	pmd_t *pmdp = kasan_pmd_offset(pgdp, addr, node, early);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	do {
1518c2ecf20Sopenharmony_ci		next = pmd_addr_end(addr, end);
1528c2ecf20Sopenharmony_ci		kasan_pte_populate(pmdp, addr, next, node, early);
1538c2ecf20Sopenharmony_ci	} while (pmdp++, addr = next, addr != end && __pmd_none(early, READ_ONCE(*pmdp)));
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic void __init kasan_pgd_populate(unsigned long addr, unsigned long end,
1578c2ecf20Sopenharmony_ci				      int node, bool early)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	unsigned long next;
1608c2ecf20Sopenharmony_ci	pgd_t *pgdp;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	pgdp = pgd_offset_k(addr);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	do {
1658c2ecf20Sopenharmony_ci		next = pgd_addr_end(addr, end);
1668c2ecf20Sopenharmony_ci		kasan_pmd_populate(pgdp, addr, next, node, early);
1678c2ecf20Sopenharmony_ci	} while (pgdp++, addr = next, addr != end);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ciasmlinkage void __init kasan_early_init(void)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_START, PGDIR_SIZE));
1748c2ecf20Sopenharmony_ci	BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, PGDIR_SIZE));
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci/* Set up full kasan mappings, ensuring that the mapped pages are zeroed */
1788c2ecf20Sopenharmony_cistatic void __init kasan_map_populate(unsigned long start, unsigned long end,
1798c2ecf20Sopenharmony_ci				      int node)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	kasan_pgd_populate(start & PAGE_MASK, PAGE_ALIGN(end), node, false);
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic void __init clear_pgds(unsigned long start,
1858c2ecf20Sopenharmony_ci			unsigned long end)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	for (; start < end; start += PGDIR_SIZE)
1888c2ecf20Sopenharmony_ci		kasan_set_pgd((pgd_t *)pgd_offset_k(start), __pgd(0));
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_civoid __init kasan_init(void)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	u64 i;
1948c2ecf20Sopenharmony_ci	phys_addr_t pa_start, pa_end;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	/*
1978c2ecf20Sopenharmony_ci	 * PGD was populated as invalid_pmd_table or invalid_pud_table
1988c2ecf20Sopenharmony_ci	 * in pagetable_init() which depends on how many levels of page
1998c2ecf20Sopenharmony_ci	 * table you are using, but we had to clean the gpd of kasan
2008c2ecf20Sopenharmony_ci	 * shadow memory, as the pgd value is none-zero.
2018c2ecf20Sopenharmony_ci	 * The assertion pgd_none is going to be false and the formal populate
2028c2ecf20Sopenharmony_ci	 * afterwards is not going to create any new pgd at all.
2038c2ecf20Sopenharmony_ci	 */
2048c2ecf20Sopenharmony_ci	memcpy(kasan_pg_dir, swapper_pg_dir, sizeof(kasan_pg_dir));
2058c2ecf20Sopenharmony_ci	csr_write64(__pa_symbol(kasan_pg_dir), LOONGARCH_CSR_PGDH);
2068c2ecf20Sopenharmony_ci	local_flush_tlb_all();
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	/* Maps everything to a single page of zeroes */
2118c2ecf20Sopenharmony_ci	kasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE, true);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	kasan_populate_early_shadow(kasan_mem_to_shadow((void *)VMALLOC_START),
2148c2ecf20Sopenharmony_ci					kasan_mem_to_shadow((void *)VMEMMAP_END));
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	kasan_early_stage = false;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	/* Populate the linear mapping */
2198c2ecf20Sopenharmony_ci	for_each_mem_range(i, &pa_start, &pa_end) {
2208c2ecf20Sopenharmony_ci		void *start = (void *)phys_to_virt(pa_start);
2218c2ecf20Sopenharmony_ci		void *end   = (void *)phys_to_virt(pa_end);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci		if (start >= end)
2248c2ecf20Sopenharmony_ci			break;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci		kasan_map_populate((unsigned long)kasan_mem_to_shadow(start),
2278c2ecf20Sopenharmony_ci			(unsigned long)kasan_mem_to_shadow(end), NUMA_NO_NODE);
2288c2ecf20Sopenharmony_ci	}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	/* Populate modules mapping */
2318c2ecf20Sopenharmony_ci	kasan_map_populate((unsigned long)kasan_mem_to_shadow((void *)MODULES_VADDR),
2328c2ecf20Sopenharmony_ci		(unsigned long)kasan_mem_to_shadow((void *)MODULES_END), NUMA_NO_NODE);
2338c2ecf20Sopenharmony_ci	/*
2348c2ecf20Sopenharmony_ci	 * KAsan may reuse the contents of kasan_zero_pte directly, so we
2358c2ecf20Sopenharmony_ci	 * should make sure that it maps the zero page read-only.
2368c2ecf20Sopenharmony_ci	 */
2378c2ecf20Sopenharmony_ci	for (i = 0; i < PTRS_PER_PTE; i++)
2388c2ecf20Sopenharmony_ci		set_pte(&kasan_early_shadow_pte[i],
2398c2ecf20Sopenharmony_ci			pfn_pte(__phys_to_pfn(__pa_symbol(kasan_early_shadow_page)),
2408c2ecf20Sopenharmony_ci				PAGE_KERNEL_RO));
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	memset(kasan_early_shadow_page, 0, PAGE_SIZE);
2438c2ecf20Sopenharmony_ci	csr_write64(__pa_symbol(swapper_pg_dir), LOONGARCH_CSR_PGDH);
2448c2ecf20Sopenharmony_ci	local_flush_tlb_all();
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	/* At this point kasan is fully initialized. Enable error messages */
2478c2ecf20Sopenharmony_ci	init_task.kasan_depth = 0;
2488c2ecf20Sopenharmony_ci	pr_info("KernelAddressSanitizer initialized.\n");
2498c2ecf20Sopenharmony_ci}
250