18ad8b727SNick Hu // SPDX-License-Identifier: GPL-2.0 28ad8b727SNick Hu // Copyright (C) 2019 Andes Technology Corporation 38ad8b727SNick Hu 48ad8b727SNick Hu #include <linux/pfn.h> 58ad8b727SNick Hu #include <linux/init_task.h> 68ad8b727SNick Hu #include <linux/kasan.h> 78ad8b727SNick Hu #include <linux/kernel.h> 88ad8b727SNick Hu #include <linux/memblock.h> 9ca5999fdSMike Rapoport #include <linux/pgtable.h> 1065fddcfcSMike Rapoport #include <asm/tlbflush.h> 118ad8b727SNick Hu #include <asm/fixmap.h> 12e178d670SNylon Chen #include <asm/pgalloc.h> 13e178d670SNylon Chen 14e8a62cc2SAlexandre Ghiti /* 15e8a62cc2SAlexandre Ghiti * Kasan shadow region must lie at a fixed address across sv39, sv48 and sv57 16e8a62cc2SAlexandre Ghiti * which is right before the kernel. 17e8a62cc2SAlexandre Ghiti * 18e8a62cc2SAlexandre Ghiti * For sv39, the region is aligned on PGDIR_SIZE so we only need to populate 19e8a62cc2SAlexandre Ghiti * the page global directory with kasan_early_shadow_pmd. 20e8a62cc2SAlexandre Ghiti * 2196f9d4daSAlexandre Ghiti * For sv48 and sv57, the region start is aligned on PGDIR_SIZE whereas the end 2296f9d4daSAlexandre Ghiti * region is not and then we have to go down to the PUD level. 23e8a62cc2SAlexandre Ghiti */ 24e8a62cc2SAlexandre Ghiti 258ad8b727SNick Hu extern pgd_t early_pg_dir[PTRS_PER_PGD]; 26*dd7664d6SAlexandre Ghiti static pgd_t tmp_pg_dir[PTRS_PER_PGD] __page_aligned_bss; 27*dd7664d6SAlexandre Ghiti static p4d_t tmp_p4d[PTRS_PER_P4D] __page_aligned_bss; 28*dd7664d6SAlexandre Ghiti static pud_t tmp_pud[PTRS_PER_PUD] __page_aligned_bss; 298ad8b727SNick Hu 301987501bSJisheng Zhang static void __init kasan_populate_pte(pmd_t *pmd, unsigned long vaddr, unsigned long end) 318ad8b727SNick Hu { 32d127c19cSAlexandre Ghiti phys_addr_t phys_addr; 3396f9d4daSAlexandre Ghiti pte_t *ptep, *p; 34a0a31fd8SZong Li 3596f9d4daSAlexandre Ghiti if (pmd_none(*pmd)) { 3696f9d4daSAlexandre Ghiti p = memblock_alloc(PTRS_PER_PTE * sizeof(pte_t), PAGE_SIZE); 3796f9d4daSAlexandre Ghiti set_pmd(pmd, pfn_pmd(PFN_DOWN(__pa(p)), PAGE_TABLE)); 3896f9d4daSAlexandre Ghiti } 398ad8b727SNick Hu 4096f9d4daSAlexandre Ghiti ptep = pte_offset_kernel(pmd, vaddr); 41d127c19cSAlexandre Ghiti 42d127c19cSAlexandre Ghiti do { 43d127c19cSAlexandre Ghiti if (pte_none(*ptep)) { 44d127c19cSAlexandre Ghiti phys_addr = memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE); 45d127c19cSAlexandre Ghiti set_pte(ptep, pfn_pte(PFN_DOWN(phys_addr), PAGE_KERNEL)); 4696f9d4daSAlexandre Ghiti memset(__va(phys_addr), KASAN_SHADOW_INIT, PAGE_SIZE); 47d127c19cSAlexandre Ghiti } 48d127c19cSAlexandre Ghiti } while (ptep++, vaddr += PAGE_SIZE, vaddr != end); 498ad8b727SNick Hu } 508ad8b727SNick Hu 51e8a62cc2SAlexandre Ghiti static void __init kasan_populate_pmd(pud_t *pud, unsigned long vaddr, unsigned long end) 52d127c19cSAlexandre Ghiti { 53d127c19cSAlexandre Ghiti phys_addr_t phys_addr; 5496f9d4daSAlexandre Ghiti pmd_t *pmdp, *p; 55d127c19cSAlexandre Ghiti unsigned long next; 568ad8b727SNick Hu 57e8a62cc2SAlexandre Ghiti if (pud_none(*pud)) { 5896f9d4daSAlexandre Ghiti p = memblock_alloc(PTRS_PER_PMD * sizeof(pmd_t), PAGE_SIZE); 5996f9d4daSAlexandre Ghiti set_pud(pud, pfn_pud(PFN_DOWN(__pa(p)), PAGE_TABLE)); 60e8a62cc2SAlexandre Ghiti } 61d127c19cSAlexandre Ghiti 6296f9d4daSAlexandre Ghiti pmdp = pmd_offset(pud, vaddr); 63d127c19cSAlexandre Ghiti 64d127c19cSAlexandre Ghiti do { 65d127c19cSAlexandre Ghiti next = pmd_addr_end(vaddr, end); 66d7fbcf40SAlexandre Ghiti 67d7fbcf40SAlexandre Ghiti if (pmd_none(*pmdp) && IS_ALIGNED(vaddr, PMD_SIZE) && (next - vaddr) >= PMD_SIZE) { 68d7fbcf40SAlexandre Ghiti phys_addr = memblock_phys_alloc(PMD_SIZE, PMD_SIZE); 69d7fbcf40SAlexandre Ghiti if (phys_addr) { 70d7fbcf40SAlexandre Ghiti set_pmd(pmdp, pfn_pmd(PFN_DOWN(phys_addr), PAGE_KERNEL)); 7196f9d4daSAlexandre Ghiti memset(__va(phys_addr), KASAN_SHADOW_INIT, PMD_SIZE); 72d7fbcf40SAlexandre Ghiti continue; 73d7fbcf40SAlexandre Ghiti } 74d7fbcf40SAlexandre Ghiti } 75d7fbcf40SAlexandre Ghiti 76d127c19cSAlexandre Ghiti kasan_populate_pte(pmdp, vaddr, next); 77d127c19cSAlexandre Ghiti } while (pmdp++, vaddr = next, vaddr != end); 78d127c19cSAlexandre Ghiti } 79d127c19cSAlexandre Ghiti 8096f9d4daSAlexandre Ghiti static void __init kasan_populate_pud(p4d_t *p4d, 81cd0334e1SAlexandre Ghiti unsigned long vaddr, unsigned long end) 82d127c19cSAlexandre Ghiti { 83d127c19cSAlexandre Ghiti phys_addr_t phys_addr; 8496f9d4daSAlexandre Ghiti pud_t *pudp, *p; 85e8a62cc2SAlexandre Ghiti unsigned long next; 86e8a62cc2SAlexandre Ghiti 8796f9d4daSAlexandre Ghiti if (p4d_none(*p4d)) { 8896f9d4daSAlexandre Ghiti p = memblock_alloc(PTRS_PER_PUD * sizeof(pud_t), PAGE_SIZE); 8996f9d4daSAlexandre Ghiti set_p4d(p4d, pfn_p4d(PFN_DOWN(__pa(p)), PAGE_TABLE)); 90e8a62cc2SAlexandre Ghiti } 91e8a62cc2SAlexandre Ghiti 9296f9d4daSAlexandre Ghiti pudp = pud_offset(p4d, vaddr); 93e8a62cc2SAlexandre Ghiti 94e8a62cc2SAlexandre Ghiti do { 95e8a62cc2SAlexandre Ghiti next = pud_addr_end(vaddr, end); 96e8a62cc2SAlexandre Ghiti 97e8a62cc2SAlexandre Ghiti if (pud_none(*pudp) && IS_ALIGNED(vaddr, PUD_SIZE) && (next - vaddr) >= PUD_SIZE) { 98e8a62cc2SAlexandre Ghiti phys_addr = memblock_phys_alloc(PUD_SIZE, PUD_SIZE); 99e8a62cc2SAlexandre Ghiti if (phys_addr) { 100e8a62cc2SAlexandre Ghiti set_pud(pudp, pfn_pud(PFN_DOWN(phys_addr), PAGE_KERNEL)); 10196f9d4daSAlexandre Ghiti memset(__va(phys_addr), KASAN_SHADOW_INIT, PUD_SIZE); 102e8a62cc2SAlexandre Ghiti continue; 103e8a62cc2SAlexandre Ghiti } 104e8a62cc2SAlexandre Ghiti } 105e8a62cc2SAlexandre Ghiti 106e8a62cc2SAlexandre Ghiti kasan_populate_pmd(pudp, vaddr, next); 107e8a62cc2SAlexandre Ghiti } while (pudp++, vaddr = next, vaddr != end); 108e8a62cc2SAlexandre Ghiti } 109e8a62cc2SAlexandre Ghiti 1108fbdccd2SQinglin Pan static void __init kasan_populate_p4d(pgd_t *pgd, 111cd0334e1SAlexandre Ghiti unsigned long vaddr, unsigned long end) 1128fbdccd2SQinglin Pan { 1138fbdccd2SQinglin Pan phys_addr_t phys_addr; 11496f9d4daSAlexandre Ghiti p4d_t *p4dp, *p; 1158fbdccd2SQinglin Pan unsigned long next; 1168fbdccd2SQinglin Pan 11796f9d4daSAlexandre Ghiti if (pgd_none(*pgd)) { 11896f9d4daSAlexandre Ghiti p = memblock_alloc(PTRS_PER_P4D * sizeof(p4d_t), PAGE_SIZE); 11996f9d4daSAlexandre Ghiti set_pgd(pgd, pfn_pgd(PFN_DOWN(__pa(p)), PAGE_TABLE)); 1209f2ac64dSQinglin Pan } 1218fbdccd2SQinglin Pan 12296f9d4daSAlexandre Ghiti p4dp = p4d_offset(pgd, vaddr); 1238fbdccd2SQinglin Pan 1248fbdccd2SQinglin Pan do { 1258fbdccd2SQinglin Pan next = p4d_addr_end(vaddr, end); 1268fbdccd2SQinglin Pan 1278fbdccd2SQinglin Pan if (p4d_none(*p4dp) && IS_ALIGNED(vaddr, P4D_SIZE) && (next - vaddr) >= P4D_SIZE) { 1288fbdccd2SQinglin Pan phys_addr = memblock_phys_alloc(P4D_SIZE, P4D_SIZE); 1298fbdccd2SQinglin Pan if (phys_addr) { 1308fbdccd2SQinglin Pan set_p4d(p4dp, pfn_p4d(PFN_DOWN(phys_addr), PAGE_KERNEL)); 13196f9d4daSAlexandre Ghiti memset(__va(phys_addr), KASAN_SHADOW_INIT, P4D_SIZE); 1328fbdccd2SQinglin Pan continue; 1338fbdccd2SQinglin Pan } 1348fbdccd2SQinglin Pan } 1358fbdccd2SQinglin Pan 13696f9d4daSAlexandre Ghiti kasan_populate_pud(p4dp, vaddr, next); 1378fbdccd2SQinglin Pan } while (p4dp++, vaddr = next, vaddr != end); 1388fbdccd2SQinglin Pan } 1398fbdccd2SQinglin Pan 1402efad17eSAlexandre Ghiti static void __init kasan_populate_pgd(pgd_t *pgdp, 141cd0334e1SAlexandre Ghiti unsigned long vaddr, unsigned long end) 142d127c19cSAlexandre Ghiti { 143d127c19cSAlexandre Ghiti phys_addr_t phys_addr; 144d127c19cSAlexandre Ghiti unsigned long next; 145d127c19cSAlexandre Ghiti 146d127c19cSAlexandre Ghiti do { 147d127c19cSAlexandre Ghiti next = pgd_addr_end(vaddr, end); 148d7fbcf40SAlexandre Ghiti 14996f9d4daSAlexandre Ghiti if (pgd_none(*pgdp) && IS_ALIGNED(vaddr, PGDIR_SIZE) && 15096f9d4daSAlexandre Ghiti (next - vaddr) >= PGDIR_SIZE) { 151d7fbcf40SAlexandre Ghiti phys_addr = memblock_phys_alloc(PGDIR_SIZE, PGDIR_SIZE); 152d7fbcf40SAlexandre Ghiti if (phys_addr) { 153d7fbcf40SAlexandre Ghiti set_pgd(pgdp, pfn_pgd(PFN_DOWN(phys_addr), PAGE_KERNEL)); 15496f9d4daSAlexandre Ghiti memset(__va(phys_addr), KASAN_SHADOW_INIT, PGDIR_SIZE); 155d7fbcf40SAlexandre Ghiti continue; 156d7fbcf40SAlexandre Ghiti } 157d7fbcf40SAlexandre Ghiti } 15896f9d4daSAlexandre Ghiti 15996f9d4daSAlexandre Ghiti kasan_populate_p4d(pgdp, vaddr, next); 16096f9d4daSAlexandre Ghiti } while (pgdp++, vaddr = next, vaddr != end); 1612efad17eSAlexandre Ghiti } 162d7fbcf40SAlexandre Ghiti 16396f9d4daSAlexandre Ghiti static void __init kasan_early_clear_pud(p4d_t *p4dp, 16496f9d4daSAlexandre Ghiti unsigned long vaddr, unsigned long end) 16596f9d4daSAlexandre Ghiti { 16696f9d4daSAlexandre Ghiti pud_t *pudp, *base_pud; 16796f9d4daSAlexandre Ghiti unsigned long next; 16896f9d4daSAlexandre Ghiti 16996f9d4daSAlexandre Ghiti if (!pgtable_l4_enabled) { 17096f9d4daSAlexandre Ghiti pudp = (pud_t *)p4dp; 17196f9d4daSAlexandre Ghiti } else { 17296f9d4daSAlexandre Ghiti base_pud = pt_ops.get_pud_virt(pfn_to_phys(_p4d_pfn(*p4dp))); 17396f9d4daSAlexandre Ghiti pudp = base_pud + pud_index(vaddr); 17496f9d4daSAlexandre Ghiti } 17596f9d4daSAlexandre Ghiti 17696f9d4daSAlexandre Ghiti do { 17796f9d4daSAlexandre Ghiti next = pud_addr_end(vaddr, end); 17896f9d4daSAlexandre Ghiti 17996f9d4daSAlexandre Ghiti if (IS_ALIGNED(vaddr, PUD_SIZE) && (next - vaddr) >= PUD_SIZE) { 18096f9d4daSAlexandre Ghiti pud_clear(pudp); 18196f9d4daSAlexandre Ghiti continue; 18296f9d4daSAlexandre Ghiti } 18396f9d4daSAlexandre Ghiti 18496f9d4daSAlexandre Ghiti BUG(); 18596f9d4daSAlexandre Ghiti } while (pudp++, vaddr = next, vaddr != end); 18696f9d4daSAlexandre Ghiti } 18796f9d4daSAlexandre Ghiti 18896f9d4daSAlexandre Ghiti static void __init kasan_early_clear_p4d(pgd_t *pgdp, 18996f9d4daSAlexandre Ghiti unsigned long vaddr, unsigned long end) 19096f9d4daSAlexandre Ghiti { 19196f9d4daSAlexandre Ghiti p4d_t *p4dp, *base_p4d; 19296f9d4daSAlexandre Ghiti unsigned long next; 19396f9d4daSAlexandre Ghiti 19496f9d4daSAlexandre Ghiti if (!pgtable_l5_enabled) { 19596f9d4daSAlexandre Ghiti p4dp = (p4d_t *)pgdp; 19696f9d4daSAlexandre Ghiti } else { 19796f9d4daSAlexandre Ghiti base_p4d = pt_ops.get_p4d_virt(pfn_to_phys(_pgd_pfn(*pgdp))); 19896f9d4daSAlexandre Ghiti p4dp = base_p4d + p4d_index(vaddr); 19996f9d4daSAlexandre Ghiti } 20096f9d4daSAlexandre Ghiti 20196f9d4daSAlexandre Ghiti do { 20296f9d4daSAlexandre Ghiti next = p4d_addr_end(vaddr, end); 20396f9d4daSAlexandre Ghiti 20496f9d4daSAlexandre Ghiti if (pgtable_l4_enabled && IS_ALIGNED(vaddr, P4D_SIZE) && 20596f9d4daSAlexandre Ghiti (next - vaddr) >= P4D_SIZE) { 20696f9d4daSAlexandre Ghiti p4d_clear(p4dp); 20796f9d4daSAlexandre Ghiti continue; 20896f9d4daSAlexandre Ghiti } 20996f9d4daSAlexandre Ghiti 21096f9d4daSAlexandre Ghiti kasan_early_clear_pud(p4dp, vaddr, next); 21196f9d4daSAlexandre Ghiti } while (p4dp++, vaddr = next, vaddr != end); 21296f9d4daSAlexandre Ghiti } 21396f9d4daSAlexandre Ghiti 21496f9d4daSAlexandre Ghiti static void __init kasan_early_clear_pgd(pgd_t *pgdp, 21596f9d4daSAlexandre Ghiti unsigned long vaddr, unsigned long end) 21696f9d4daSAlexandre Ghiti { 21796f9d4daSAlexandre Ghiti unsigned long next; 21896f9d4daSAlexandre Ghiti 21996f9d4daSAlexandre Ghiti do { 22096f9d4daSAlexandre Ghiti next = pgd_addr_end(vaddr, end); 22196f9d4daSAlexandre Ghiti 22296f9d4daSAlexandre Ghiti if (pgtable_l5_enabled && IS_ALIGNED(vaddr, PGDIR_SIZE) && 22396f9d4daSAlexandre Ghiti (next - vaddr) >= PGDIR_SIZE) { 22496f9d4daSAlexandre Ghiti pgd_clear(pgdp); 22596f9d4daSAlexandre Ghiti continue; 22696f9d4daSAlexandre Ghiti } 22796f9d4daSAlexandre Ghiti 22896f9d4daSAlexandre Ghiti kasan_early_clear_p4d(pgdp, vaddr, next); 229cd0334e1SAlexandre Ghiti } while (pgdp++, vaddr = next, vaddr != end); 230cd0334e1SAlexandre Ghiti } 231cd0334e1SAlexandre Ghiti 232cd0334e1SAlexandre Ghiti static void __init kasan_early_populate_pud(p4d_t *p4dp, 233cd0334e1SAlexandre Ghiti unsigned long vaddr, 234cd0334e1SAlexandre Ghiti unsigned long end) 235cd0334e1SAlexandre Ghiti { 236cd0334e1SAlexandre Ghiti pud_t *pudp, *base_pud; 237cd0334e1SAlexandre Ghiti phys_addr_t phys_addr; 238cd0334e1SAlexandre Ghiti unsigned long next; 239cd0334e1SAlexandre Ghiti 240cd0334e1SAlexandre Ghiti if (!pgtable_l4_enabled) { 241cd0334e1SAlexandre Ghiti pudp = (pud_t *)p4dp; 242cd0334e1SAlexandre Ghiti } else { 243cd0334e1SAlexandre Ghiti base_pud = pt_ops.get_pud_virt(pfn_to_phys(_p4d_pfn(*p4dp))); 244cd0334e1SAlexandre Ghiti pudp = base_pud + pud_index(vaddr); 245cd0334e1SAlexandre Ghiti } 246cd0334e1SAlexandre Ghiti 247cd0334e1SAlexandre Ghiti do { 248cd0334e1SAlexandre Ghiti next = pud_addr_end(vaddr, end); 249cd0334e1SAlexandre Ghiti 250cd0334e1SAlexandre Ghiti if (pud_none(*pudp) && IS_ALIGNED(vaddr, PUD_SIZE) && 251cd0334e1SAlexandre Ghiti (next - vaddr) >= PUD_SIZE) { 252cd0334e1SAlexandre Ghiti phys_addr = __pa((uintptr_t)kasan_early_shadow_pmd); 253cd0334e1SAlexandre Ghiti set_pud(pudp, pfn_pud(PFN_DOWN(phys_addr), PAGE_TABLE)); 254cd0334e1SAlexandre Ghiti continue; 255cd0334e1SAlexandre Ghiti } 256cd0334e1SAlexandre Ghiti 257cd0334e1SAlexandre Ghiti BUG(); 258cd0334e1SAlexandre Ghiti } while (pudp++, vaddr = next, vaddr != end); 259cd0334e1SAlexandre Ghiti } 260cd0334e1SAlexandre Ghiti 261cd0334e1SAlexandre Ghiti static void __init kasan_early_populate_p4d(pgd_t *pgdp, 262cd0334e1SAlexandre Ghiti unsigned long vaddr, 263cd0334e1SAlexandre Ghiti unsigned long end) 264cd0334e1SAlexandre Ghiti { 265cd0334e1SAlexandre Ghiti p4d_t *p4dp, *base_p4d; 266cd0334e1SAlexandre Ghiti phys_addr_t phys_addr; 267cd0334e1SAlexandre Ghiti unsigned long next; 268cd0334e1SAlexandre Ghiti 269cd0334e1SAlexandre Ghiti /* 270cd0334e1SAlexandre Ghiti * We can't use pgd_page_vaddr here as it would return a linear 271cd0334e1SAlexandre Ghiti * mapping address but it is not mapped yet, but when populating 272cd0334e1SAlexandre Ghiti * early_pg_dir, we need the physical address and when populating 273cd0334e1SAlexandre Ghiti * swapper_pg_dir, we need the kernel virtual address so use 274cd0334e1SAlexandre Ghiti * pt_ops facility. 275cd0334e1SAlexandre Ghiti * Note that this test is then completely equivalent to 276cd0334e1SAlexandre Ghiti * p4dp = p4d_offset(pgdp, vaddr) 277cd0334e1SAlexandre Ghiti */ 278cd0334e1SAlexandre Ghiti if (!pgtable_l5_enabled) { 279cd0334e1SAlexandre Ghiti p4dp = (p4d_t *)pgdp; 280cd0334e1SAlexandre Ghiti } else { 281cd0334e1SAlexandre Ghiti base_p4d = pt_ops.get_p4d_virt(pfn_to_phys(_pgd_pfn(*pgdp))); 282cd0334e1SAlexandre Ghiti p4dp = base_p4d + p4d_index(vaddr); 283cd0334e1SAlexandre Ghiti } 284cd0334e1SAlexandre Ghiti 285cd0334e1SAlexandre Ghiti do { 286cd0334e1SAlexandre Ghiti next = p4d_addr_end(vaddr, end); 287cd0334e1SAlexandre Ghiti 288cd0334e1SAlexandre Ghiti if (p4d_none(*p4dp) && IS_ALIGNED(vaddr, P4D_SIZE) && 289cd0334e1SAlexandre Ghiti (next - vaddr) >= P4D_SIZE) { 290cd0334e1SAlexandre Ghiti phys_addr = __pa((uintptr_t)kasan_early_shadow_pud); 291cd0334e1SAlexandre Ghiti set_p4d(p4dp, pfn_p4d(PFN_DOWN(phys_addr), PAGE_TABLE)); 292cd0334e1SAlexandre Ghiti continue; 293cd0334e1SAlexandre Ghiti } 294cd0334e1SAlexandre Ghiti 295cd0334e1SAlexandre Ghiti kasan_early_populate_pud(p4dp, vaddr, next); 296cd0334e1SAlexandre Ghiti } while (p4dp++, vaddr = next, vaddr != end); 297cd0334e1SAlexandre Ghiti } 298cd0334e1SAlexandre Ghiti 299cd0334e1SAlexandre Ghiti static void __init kasan_early_populate_pgd(pgd_t *pgdp, 300cd0334e1SAlexandre Ghiti unsigned long vaddr, 301cd0334e1SAlexandre Ghiti unsigned long end) 302cd0334e1SAlexandre Ghiti { 303cd0334e1SAlexandre Ghiti phys_addr_t phys_addr; 304cd0334e1SAlexandre Ghiti unsigned long next; 305cd0334e1SAlexandre Ghiti 306cd0334e1SAlexandre Ghiti do { 307cd0334e1SAlexandre Ghiti next = pgd_addr_end(vaddr, end); 308cd0334e1SAlexandre Ghiti 309cd0334e1SAlexandre Ghiti if (pgd_none(*pgdp) && IS_ALIGNED(vaddr, PGDIR_SIZE) && 310cd0334e1SAlexandre Ghiti (next - vaddr) >= PGDIR_SIZE) { 311cd0334e1SAlexandre Ghiti phys_addr = __pa((uintptr_t)kasan_early_shadow_p4d); 312cd0334e1SAlexandre Ghiti set_pgd(pgdp, pfn_pgd(PFN_DOWN(phys_addr), PAGE_TABLE)); 313cd0334e1SAlexandre Ghiti continue; 314cd0334e1SAlexandre Ghiti } 315cd0334e1SAlexandre Ghiti 316cd0334e1SAlexandre Ghiti kasan_early_populate_p4d(pgdp, vaddr, next); 317d127c19cSAlexandre Ghiti } while (pgdp++, vaddr = next, vaddr != end); 318d127c19cSAlexandre Ghiti } 319d127c19cSAlexandre Ghiti 3202efad17eSAlexandre Ghiti asmlinkage void __init kasan_early_init(void) 3212efad17eSAlexandre Ghiti { 3222efad17eSAlexandre Ghiti uintptr_t i; 3232efad17eSAlexandre Ghiti 3242efad17eSAlexandre Ghiti BUILD_BUG_ON(KASAN_SHADOW_OFFSET != 3252efad17eSAlexandre Ghiti KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT))); 3262efad17eSAlexandre Ghiti 3272efad17eSAlexandre Ghiti for (i = 0; i < PTRS_PER_PTE; ++i) 3282efad17eSAlexandre Ghiti set_pte(kasan_early_shadow_pte + i, 329a3d32803SAlexandre Ghiti pfn_pte(virt_to_pfn(kasan_early_shadow_page), PAGE_KERNEL)); 3302efad17eSAlexandre Ghiti 3312efad17eSAlexandre Ghiti for (i = 0; i < PTRS_PER_PMD; ++i) 3322efad17eSAlexandre Ghiti set_pmd(kasan_early_shadow_pmd + i, 3332efad17eSAlexandre Ghiti pfn_pmd(PFN_DOWN 3342efad17eSAlexandre Ghiti (__pa((uintptr_t)kasan_early_shadow_pte)), 3352efad17eSAlexandre Ghiti PAGE_TABLE)); 3362efad17eSAlexandre Ghiti 3372efad17eSAlexandre Ghiti if (pgtable_l4_enabled) { 3382efad17eSAlexandre Ghiti for (i = 0; i < PTRS_PER_PUD; ++i) 3392efad17eSAlexandre Ghiti set_pud(kasan_early_shadow_pud + i, 3402efad17eSAlexandre Ghiti pfn_pud(PFN_DOWN 3412efad17eSAlexandre Ghiti (__pa(((uintptr_t)kasan_early_shadow_pmd))), 3422efad17eSAlexandre Ghiti PAGE_TABLE)); 3432efad17eSAlexandre Ghiti } 3442efad17eSAlexandre Ghiti 3458fbdccd2SQinglin Pan if (pgtable_l5_enabled) { 3468fbdccd2SQinglin Pan for (i = 0; i < PTRS_PER_P4D; ++i) 3478fbdccd2SQinglin Pan set_p4d(kasan_early_shadow_p4d + i, 3488fbdccd2SQinglin Pan pfn_p4d(PFN_DOWN 3498fbdccd2SQinglin Pan (__pa(((uintptr_t)kasan_early_shadow_pud))), 3508fbdccd2SQinglin Pan PAGE_TABLE)); 3518fbdccd2SQinglin Pan } 3528fbdccd2SQinglin Pan 353cd0334e1SAlexandre Ghiti kasan_early_populate_pgd(early_pg_dir + pgd_index(KASAN_SHADOW_START), 354cd0334e1SAlexandre Ghiti KASAN_SHADOW_START, KASAN_SHADOW_END); 3552efad17eSAlexandre Ghiti 3562efad17eSAlexandre Ghiti local_flush_tlb_all(); 3572efad17eSAlexandre Ghiti } 3582efad17eSAlexandre Ghiti 3592efad17eSAlexandre Ghiti void __init kasan_swapper_init(void) 3602efad17eSAlexandre Ghiti { 361cd0334e1SAlexandre Ghiti kasan_early_populate_pgd(pgd_offset_k(KASAN_SHADOW_START), 362cd0334e1SAlexandre Ghiti KASAN_SHADOW_START, KASAN_SHADOW_END); 3632efad17eSAlexandre Ghiti 3642efad17eSAlexandre Ghiti local_flush_tlb_all(); 3652efad17eSAlexandre Ghiti } 3662efad17eSAlexandre Ghiti 367d127c19cSAlexandre Ghiti static void __init kasan_populate(void *start, void *end) 368d127c19cSAlexandre Ghiti { 369d127c19cSAlexandre Ghiti unsigned long vaddr = (unsigned long)start & PAGE_MASK; 370d127c19cSAlexandre Ghiti unsigned long vend = PAGE_ALIGN((unsigned long)end); 371d127c19cSAlexandre Ghiti 372cd0334e1SAlexandre Ghiti kasan_populate_pgd(pgd_offset_k(vaddr), vaddr, vend); 3738ad8b727SNick Hu } 3748ad8b727SNick Hu 37596f9d4daSAlexandre Ghiti static void __init kasan_shallow_populate_pud(p4d_t *p4d, 3768fbdccd2SQinglin Pan unsigned long vaddr, unsigned long end) 3778fbdccd2SQinglin Pan { 3788fbdccd2SQinglin Pan unsigned long next; 37996f9d4daSAlexandre Ghiti void *p; 38096f9d4daSAlexandre Ghiti pud_t *pud_k = pud_offset(p4d, vaddr); 381e8a62cc2SAlexandre Ghiti 382e8a62cc2SAlexandre Ghiti do { 383e8a62cc2SAlexandre Ghiti next = pud_addr_end(vaddr, end); 384e8a62cc2SAlexandre Ghiti 38596f9d4daSAlexandre Ghiti if (pud_none(*pud_k)) { 38696f9d4daSAlexandre Ghiti p = memblock_alloc(PAGE_SIZE, PAGE_SIZE); 38796f9d4daSAlexandre Ghiti set_pud(pud_k, pfn_pud(PFN_DOWN(__pa(p)), PAGE_TABLE)); 3888fbdccd2SQinglin Pan continue; 389e8a62cc2SAlexandre Ghiti } 390e8a62cc2SAlexandre Ghiti 39196f9d4daSAlexandre Ghiti BUG(); 39296f9d4daSAlexandre Ghiti } while (pud_k++, vaddr = next, vaddr != end); 39396f9d4daSAlexandre Ghiti } 39496f9d4daSAlexandre Ghiti 39596f9d4daSAlexandre Ghiti static void __init kasan_shallow_populate_p4d(pgd_t *pgd, 3968fbdccd2SQinglin Pan unsigned long vaddr, unsigned long end) 3978fbdccd2SQinglin Pan { 3988fbdccd2SQinglin Pan unsigned long next; 39996f9d4daSAlexandre Ghiti void *p; 40096f9d4daSAlexandre Ghiti p4d_t *p4d_k = p4d_offset(pgd, vaddr); 4018fbdccd2SQinglin Pan 4028fbdccd2SQinglin Pan do { 4038fbdccd2SQinglin Pan next = p4d_addr_end(vaddr, end); 4048fbdccd2SQinglin Pan 40596f9d4daSAlexandre Ghiti if (p4d_none(*p4d_k)) { 40696f9d4daSAlexandre Ghiti p = memblock_alloc(PAGE_SIZE, PAGE_SIZE); 40796f9d4daSAlexandre Ghiti set_p4d(p4d_k, pfn_p4d(PFN_DOWN(__pa(p)), PAGE_TABLE)); 4088fbdccd2SQinglin Pan continue; 4098fbdccd2SQinglin Pan } 4108fbdccd2SQinglin Pan 41196f9d4daSAlexandre Ghiti kasan_shallow_populate_pud(p4d_k, vaddr, end); 41296f9d4daSAlexandre Ghiti } while (p4d_k++, vaddr = next, vaddr != end); 41396f9d4daSAlexandre Ghiti } 4148fbdccd2SQinglin Pan 4152da073c1SAlexandre Ghiti static void __init kasan_shallow_populate_pgd(unsigned long vaddr, unsigned long end) 4162da073c1SAlexandre Ghiti { 4172da073c1SAlexandre Ghiti unsigned long next; 4182da073c1SAlexandre Ghiti void *p; 4192da073c1SAlexandre Ghiti pgd_t *pgd_k = pgd_offset_k(vaddr); 4202da073c1SAlexandre Ghiti 4212da073c1SAlexandre Ghiti do { 4222da073c1SAlexandre Ghiti next = pgd_addr_end(vaddr, end); 423e8a62cc2SAlexandre Ghiti 42496f9d4daSAlexandre Ghiti if (pgd_none(*pgd_k)) { 4252da073c1SAlexandre Ghiti p = memblock_alloc(PAGE_SIZE, PAGE_SIZE); 4262da073c1SAlexandre Ghiti set_pgd(pgd_k, pfn_pgd(PFN_DOWN(__pa(p)), PAGE_TABLE)); 42796f9d4daSAlexandre Ghiti continue; 4282da073c1SAlexandre Ghiti } 429e8a62cc2SAlexandre Ghiti 43096f9d4daSAlexandre Ghiti kasan_shallow_populate_p4d(pgd_k, vaddr, next); 4312da073c1SAlexandre Ghiti } while (pgd_k++, vaddr = next, vaddr != end); 4322da073c1SAlexandre Ghiti } 4332da073c1SAlexandre Ghiti 43478947bdfSPalmer Dabbelt static void __init kasan_shallow_populate(void *start, void *end) 435e178d670SNylon Chen { 436e178d670SNylon Chen unsigned long vaddr = (unsigned long)start & PAGE_MASK; 437e178d670SNylon Chen unsigned long vend = PAGE_ALIGN((unsigned long)end); 438e178d670SNylon Chen 4392da073c1SAlexandre Ghiti kasan_shallow_populate_pgd(vaddr, vend); 44096f9d4daSAlexandre Ghiti } 44196f9d4daSAlexandre Ghiti 44296f9d4daSAlexandre Ghiti static void create_tmp_mapping(void) 44396f9d4daSAlexandre Ghiti { 44496f9d4daSAlexandre Ghiti void *ptr; 44596f9d4daSAlexandre Ghiti p4d_t *base_p4d; 44696f9d4daSAlexandre Ghiti 44796f9d4daSAlexandre Ghiti /* 44896f9d4daSAlexandre Ghiti * We need to clean the early mapping: this is hard to achieve "in-place", 44996f9d4daSAlexandre Ghiti * so install a temporary mapping like arm64 and x86 do. 45096f9d4daSAlexandre Ghiti */ 45196f9d4daSAlexandre Ghiti memcpy(tmp_pg_dir, swapper_pg_dir, sizeof(pgd_t) * PTRS_PER_PGD); 45296f9d4daSAlexandre Ghiti 45396f9d4daSAlexandre Ghiti /* Copy the last p4d since it is shared with the kernel mapping. */ 45496f9d4daSAlexandre Ghiti if (pgtable_l5_enabled) { 45596f9d4daSAlexandre Ghiti ptr = (p4d_t *)pgd_page_vaddr(*pgd_offset_k(KASAN_SHADOW_END)); 45696f9d4daSAlexandre Ghiti memcpy(tmp_p4d, ptr, sizeof(p4d_t) * PTRS_PER_P4D); 45796f9d4daSAlexandre Ghiti set_pgd(&tmp_pg_dir[pgd_index(KASAN_SHADOW_END)], 45896f9d4daSAlexandre Ghiti pfn_pgd(PFN_DOWN(__pa(tmp_p4d)), PAGE_TABLE)); 45996f9d4daSAlexandre Ghiti base_p4d = tmp_p4d; 46096f9d4daSAlexandre Ghiti } else { 46196f9d4daSAlexandre Ghiti base_p4d = (p4d_t *)tmp_pg_dir; 46296f9d4daSAlexandre Ghiti } 46396f9d4daSAlexandre Ghiti 46496f9d4daSAlexandre Ghiti /* Copy the last pud since it is shared with the kernel mapping. */ 46596f9d4daSAlexandre Ghiti if (pgtable_l4_enabled) { 46696f9d4daSAlexandre Ghiti ptr = (pud_t *)p4d_page_vaddr(*(base_p4d + p4d_index(KASAN_SHADOW_END))); 46796f9d4daSAlexandre Ghiti memcpy(tmp_pud, ptr, sizeof(pud_t) * PTRS_PER_PUD); 46896f9d4daSAlexandre Ghiti set_p4d(&base_p4d[p4d_index(KASAN_SHADOW_END)], 46996f9d4daSAlexandre Ghiti pfn_p4d(PFN_DOWN(__pa(tmp_pud)), PAGE_TABLE)); 47096f9d4daSAlexandre Ghiti } 4718ad8b727SNick Hu } 4728ad8b727SNick Hu 4738ad8b727SNick Hu void __init kasan_init(void) 4748ad8b727SNick Hu { 475314b7817SJisheng Zhang phys_addr_t p_start, p_end; 476b10d6bcaSMike Rapoport u64 i; 4778ad8b727SNick Hu 47896f9d4daSAlexandre Ghiti create_tmp_mapping(); 47996f9d4daSAlexandre Ghiti csr_write(CSR_SATP, PFN_DOWN(__pa(tmp_pg_dir)) | satp_mode); 48096f9d4daSAlexandre Ghiti 48196f9d4daSAlexandre Ghiti kasan_early_clear_pgd(pgd_offset_k(KASAN_SHADOW_START), 48296f9d4daSAlexandre Ghiti KASAN_SHADOW_START, KASAN_SHADOW_END); 48396f9d4daSAlexandre Ghiti 48496f9d4daSAlexandre Ghiti kasan_populate_early_shadow((void *)kasan_mem_to_shadow((void *)FIXADDR_START), 48596f9d4daSAlexandre Ghiti (void *)kasan_mem_to_shadow((void *)VMALLOC_START)); 48696f9d4daSAlexandre Ghiti 48796f9d4daSAlexandre Ghiti if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) { 488e178d670SNylon Chen kasan_shallow_populate( 489e178d670SNylon Chen (void *)kasan_mem_to_shadow((void *)VMALLOC_START), 490e178d670SNylon Chen (void *)kasan_mem_to_shadow((void *)VMALLOC_END)); 49196f9d4daSAlexandre Ghiti /* Shallow populate modules and BPF which are vmalloc-allocated */ 49296f9d4daSAlexandre Ghiti kasan_shallow_populate( 49396f9d4daSAlexandre Ghiti (void *)kasan_mem_to_shadow((void *)MODULES_VADDR), 49496f9d4daSAlexandre Ghiti (void *)kasan_mem_to_shadow((void *)MODULES_END)); 49596f9d4daSAlexandre Ghiti } else { 49696f9d4daSAlexandre Ghiti kasan_populate_early_shadow((void *)kasan_mem_to_shadow((void *)VMALLOC_START), 49796f9d4daSAlexandre Ghiti (void *)kasan_mem_to_shadow((void *)VMALLOC_END)); 49896f9d4daSAlexandre Ghiti } 4998ad8b727SNick Hu 5002bfc6cd8SAlexandre Ghiti /* Populate the linear mapping */ 501314b7817SJisheng Zhang for_each_mem_range(i, &p_start, &p_end) { 502314b7817SJisheng Zhang void *start = (void *)__va(p_start); 503314b7817SJisheng Zhang void *end = (void *)__va(p_end); 5048ad8b727SNick Hu 5058ad8b727SNick Hu if (start >= end) 5068ad8b727SNick Hu break; 5078ad8b727SNick Hu 508d127c19cSAlexandre Ghiti kasan_populate(kasan_mem_to_shadow(start), kasan_mem_to_shadow(end)); 5099d8c7d92SYang Li } 5108ad8b727SNick Hu 51196f9d4daSAlexandre Ghiti /* Populate kernel */ 51296f9d4daSAlexandre Ghiti kasan_populate(kasan_mem_to_shadow((const void *)MODULES_END), 5133a02764cSJisheng Zhang kasan_mem_to_shadow((const void *)MODULES_VADDR + SZ_2G)); 5142bfc6cd8SAlexandre Ghiti 5158ad8b727SNick Hu for (i = 0; i < PTRS_PER_PTE; i++) 5168ad8b727SNick Hu set_pte(&kasan_early_shadow_pte[i], 5178ad8b727SNick Hu mk_pte(virt_to_page(kasan_early_shadow_page), 5188458ca14SZong Li __pgprot(_PAGE_PRESENT | _PAGE_READ | 5198458ca14SZong Li _PAGE_ACCESSED))); 5208ad8b727SNick Hu 5219484e2aeSAlexandre Ghiti memset(kasan_early_shadow_page, KASAN_SHADOW_INIT, PAGE_SIZE); 5228ad8b727SNick Hu init_task.kasan_depth = 0; 52396f9d4daSAlexandre Ghiti 52496f9d4daSAlexandre Ghiti csr_write(CSR_SATP, PFN_DOWN(__pa(swapper_pg_dir)) | satp_mode); 52596f9d4daSAlexandre Ghiti local_flush_tlb_all(); 5268ad8b727SNick Hu } 527