^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) // Copyright (C) 2019 Andes Technology Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/pfn.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/init_task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/kasan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/pgtable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <asm/tlbflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <asm/fixmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) extern pgd_t early_pg_dir[PTRS_PER_PGD];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) asmlinkage void __init kasan_early_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) uintptr_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) pgd_t *pgd = early_pg_dir + pgd_index(KASAN_SHADOW_START);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) BUILD_BUG_ON(KASAN_SHADOW_OFFSET !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) for (i = 0; i < PTRS_PER_PTE; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) set_pte(kasan_early_shadow_pte + i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) pfn_pte(virt_to_pfn(kasan_early_shadow_page), PAGE_KERNEL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) for (i = 0; i < PTRS_PER_PMD; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) set_pmd(kasan_early_shadow_pmd + i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) pfn_pmd(PFN_DOWN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) (__pa((uintptr_t) kasan_early_shadow_pte)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) __pgprot(_PAGE_TABLE)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) for (i = KASAN_SHADOW_START; i < KASAN_SHADOW_END;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) i += PGDIR_SIZE, ++pgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) set_pgd(pgd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) pfn_pgd(PFN_DOWN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) (__pa(((uintptr_t) kasan_early_shadow_pmd))),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) __pgprot(_PAGE_TABLE)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /* init for swapper_pg_dir */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) pgd = pgd_offset_k(KASAN_SHADOW_START);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) for (i = KASAN_SHADOW_START; i < KASAN_SHADOW_END;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) i += PGDIR_SIZE, ++pgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) set_pgd(pgd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) pfn_pgd(PFN_DOWN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) (__pa(((uintptr_t) kasan_early_shadow_pmd))),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) __pgprot(_PAGE_TABLE)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) local_flush_tlb_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static void __init populate(void *start, void *end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) unsigned long i, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) unsigned long vaddr = (unsigned long)start & PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) unsigned long vend = PAGE_ALIGN((unsigned long)end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) unsigned long n_pages = (vend - vaddr) / PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) unsigned long n_ptes =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) ((n_pages + PTRS_PER_PTE) & -PTRS_PER_PTE) / PTRS_PER_PTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) unsigned long n_pmds =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) ((n_ptes + PTRS_PER_PMD) & -PTRS_PER_PMD) / PTRS_PER_PMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) pte_t *pte =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) memblock_alloc(n_ptes * PTRS_PER_PTE * sizeof(pte_t), PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) pmd_t *pmd =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) memblock_alloc(n_pmds * PTRS_PER_PMD * sizeof(pmd_t), PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) pgd_t *pgd = pgd_offset_k(vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) for (i = 0; i < n_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) phys_addr_t phys = memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) set_pte(&pte[i], pfn_pte(PHYS_PFN(phys), PAGE_KERNEL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) for (i = 0, offset = 0; i < n_ptes; i++, offset += PTRS_PER_PTE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) set_pmd(&pmd[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) pfn_pmd(PFN_DOWN(__pa(&pte[offset])),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) __pgprot(_PAGE_TABLE)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) for (i = 0, offset = 0; i < n_pmds; i++, offset += PTRS_PER_PMD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) set_pgd(&pgd[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) pfn_pgd(PFN_DOWN(__pa(&pmd[offset])),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) __pgprot(_PAGE_TABLE)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) local_flush_tlb_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) memset(start, 0, end - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) void __init kasan_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) phys_addr_t _start, _end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) u64 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) kasan_populate_early_shadow((void *)KASAN_SHADOW_START,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) (void *)kasan_mem_to_shadow((void *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) VMALLOC_END));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) for_each_mem_range(i, &_start, &_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) void *start = (void *)__va(_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) void *end = (void *)__va(_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (start >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) populate(kasan_mem_to_shadow(start), kasan_mem_to_shadow(end));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) for (i = 0; i < PTRS_PER_PTE; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) set_pte(&kasan_early_shadow_pte[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) mk_pte(virt_to_page(kasan_early_shadow_page),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) __pgprot(_PAGE_PRESENT | _PAGE_READ |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) _PAGE_ACCESSED)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) memset(kasan_early_shadow_page, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) init_task.kasan_depth = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }