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
25dd7664d6SAlexandre Ghiti static pgd_t tmp_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
26dd7664d6SAlexandre Ghiti static p4d_t tmp_p4d[PTRS_PER_P4D] __page_aligned_bss;
27dd7664d6SAlexandre Ghiti static pud_t tmp_pud[PTRS_PER_PUD] __page_aligned_bss;
288ad8b727SNick Hu
kasan_populate_pte(pmd_t * pmd,unsigned long vaddr,unsigned long end)291987501bSJisheng Zhang static void __init kasan_populate_pte(pmd_t *pmd, unsigned long vaddr, unsigned long end)
308ad8b727SNick Hu {
31d127c19cSAlexandre Ghiti phys_addr_t phys_addr;
3296f9d4daSAlexandre Ghiti pte_t *ptep, *p;
33a0a31fd8SZong Li
34*e0316069SAlexandre Ghiti if (pmd_none(pmdp_get(pmd))) {
3596f9d4daSAlexandre Ghiti p = memblock_alloc(PTRS_PER_PTE * sizeof(pte_t), PAGE_SIZE);
3696f9d4daSAlexandre Ghiti set_pmd(pmd, pfn_pmd(PFN_DOWN(__pa(p)), PAGE_TABLE));
3796f9d4daSAlexandre Ghiti }
388ad8b727SNick Hu
3996f9d4daSAlexandre Ghiti ptep = pte_offset_kernel(pmd, vaddr);
40d127c19cSAlexandre Ghiti
41d127c19cSAlexandre Ghiti do {
42*e0316069SAlexandre Ghiti if (pte_none(ptep_get(ptep))) {
43d127c19cSAlexandre Ghiti phys_addr = memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
44d127c19cSAlexandre Ghiti set_pte(ptep, pfn_pte(PFN_DOWN(phys_addr), PAGE_KERNEL));
4596f9d4daSAlexandre Ghiti memset(__va(phys_addr), KASAN_SHADOW_INIT, PAGE_SIZE);
46d127c19cSAlexandre Ghiti }
47d127c19cSAlexandre Ghiti } while (ptep++, vaddr += PAGE_SIZE, vaddr != end);
488ad8b727SNick Hu }
498ad8b727SNick Hu
kasan_populate_pmd(pud_t * pud,unsigned long vaddr,unsigned long end)50e8a62cc2SAlexandre Ghiti static void __init kasan_populate_pmd(pud_t *pud, unsigned long vaddr, unsigned long end)
51d127c19cSAlexandre Ghiti {
52d127c19cSAlexandre Ghiti phys_addr_t phys_addr;
5396f9d4daSAlexandre Ghiti pmd_t *pmdp, *p;
54d127c19cSAlexandre Ghiti unsigned long next;
558ad8b727SNick Hu
56*e0316069SAlexandre Ghiti if (pud_none(pudp_get(pud))) {
5796f9d4daSAlexandre Ghiti p = memblock_alloc(PTRS_PER_PMD * sizeof(pmd_t), PAGE_SIZE);
5896f9d4daSAlexandre Ghiti set_pud(pud, pfn_pud(PFN_DOWN(__pa(p)), PAGE_TABLE));
59e8a62cc2SAlexandre Ghiti }
60d127c19cSAlexandre Ghiti
6196f9d4daSAlexandre Ghiti pmdp = pmd_offset(pud, vaddr);
62d127c19cSAlexandre Ghiti
63d127c19cSAlexandre Ghiti do {
64d127c19cSAlexandre Ghiti next = pmd_addr_end(vaddr, end);
65d7fbcf40SAlexandre Ghiti
66*e0316069SAlexandre Ghiti if (pmd_none(pmdp_get(pmdp)) && IS_ALIGNED(vaddr, PMD_SIZE) &&
67*e0316069SAlexandre Ghiti (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
kasan_populate_pud(p4d_t * p4d,unsigned long vaddr,unsigned long end)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
87*e0316069SAlexandre Ghiti if (p4d_none(p4dp_get(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
97*e0316069SAlexandre Ghiti if (pud_none(pudp_get(pudp)) && IS_ALIGNED(vaddr, PUD_SIZE) &&
98*e0316069SAlexandre Ghiti (next - vaddr) >= PUD_SIZE) {
99e8a62cc2SAlexandre Ghiti phys_addr = memblock_phys_alloc(PUD_SIZE, PUD_SIZE);
100e8a62cc2SAlexandre Ghiti if (phys_addr) {
101e8a62cc2SAlexandre Ghiti set_pud(pudp, pfn_pud(PFN_DOWN(phys_addr), PAGE_KERNEL));
10296f9d4daSAlexandre Ghiti memset(__va(phys_addr), KASAN_SHADOW_INIT, PUD_SIZE);
103e8a62cc2SAlexandre Ghiti continue;
104e8a62cc2SAlexandre Ghiti }
105e8a62cc2SAlexandre Ghiti }
106e8a62cc2SAlexandre Ghiti
107e8a62cc2SAlexandre Ghiti kasan_populate_pmd(pudp, vaddr, next);
108e8a62cc2SAlexandre Ghiti } while (pudp++, vaddr = next, vaddr != end);
109e8a62cc2SAlexandre Ghiti }
110e8a62cc2SAlexandre Ghiti
kasan_populate_p4d(pgd_t * pgd,unsigned long vaddr,unsigned long end)1118fbdccd2SQinglin Pan static void __init kasan_populate_p4d(pgd_t *pgd,
112cd0334e1SAlexandre Ghiti unsigned long vaddr, unsigned long end)
1138fbdccd2SQinglin Pan {
1148fbdccd2SQinglin Pan phys_addr_t phys_addr;
11596f9d4daSAlexandre Ghiti p4d_t *p4dp, *p;
1168fbdccd2SQinglin Pan unsigned long next;
1178fbdccd2SQinglin Pan
118*e0316069SAlexandre Ghiti if (pgd_none(pgdp_get(pgd))) {
11996f9d4daSAlexandre Ghiti p = memblock_alloc(PTRS_PER_P4D * sizeof(p4d_t), PAGE_SIZE);
12096f9d4daSAlexandre Ghiti set_pgd(pgd, pfn_pgd(PFN_DOWN(__pa(p)), PAGE_TABLE));
1219f2ac64dSQinglin Pan }
1228fbdccd2SQinglin Pan
12396f9d4daSAlexandre Ghiti p4dp = p4d_offset(pgd, vaddr);
1248fbdccd2SQinglin Pan
1258fbdccd2SQinglin Pan do {
1268fbdccd2SQinglin Pan next = p4d_addr_end(vaddr, end);
1278fbdccd2SQinglin Pan
128*e0316069SAlexandre Ghiti if (p4d_none(p4dp_get(p4dp)) && IS_ALIGNED(vaddr, P4D_SIZE) &&
129*e0316069SAlexandre Ghiti (next - vaddr) >= P4D_SIZE) {
1308fbdccd2SQinglin Pan phys_addr = memblock_phys_alloc(P4D_SIZE, P4D_SIZE);
1318fbdccd2SQinglin Pan if (phys_addr) {
1328fbdccd2SQinglin Pan set_p4d(p4dp, pfn_p4d(PFN_DOWN(phys_addr), PAGE_KERNEL));
13396f9d4daSAlexandre Ghiti memset(__va(phys_addr), KASAN_SHADOW_INIT, P4D_SIZE);
1348fbdccd2SQinglin Pan continue;
1358fbdccd2SQinglin Pan }
1368fbdccd2SQinglin Pan }
1378fbdccd2SQinglin Pan
13896f9d4daSAlexandre Ghiti kasan_populate_pud(p4dp, vaddr, next);
1398fbdccd2SQinglin Pan } while (p4dp++, vaddr = next, vaddr != end);
1408fbdccd2SQinglin Pan }
1418fbdccd2SQinglin Pan
kasan_populate_pgd(pgd_t * pgdp,unsigned long vaddr,unsigned long end)1422efad17eSAlexandre Ghiti static void __init kasan_populate_pgd(pgd_t *pgdp,
143cd0334e1SAlexandre Ghiti unsigned long vaddr, unsigned long end)
144d127c19cSAlexandre Ghiti {
145d127c19cSAlexandre Ghiti phys_addr_t phys_addr;
146d127c19cSAlexandre Ghiti unsigned long next;
147d127c19cSAlexandre Ghiti
148d127c19cSAlexandre Ghiti do {
149d127c19cSAlexandre Ghiti next = pgd_addr_end(vaddr, end);
150d7fbcf40SAlexandre Ghiti
151*e0316069SAlexandre Ghiti if (pgd_none(pgdp_get(pgdp)) && IS_ALIGNED(vaddr, PGDIR_SIZE) &&
15296f9d4daSAlexandre Ghiti (next - vaddr) >= PGDIR_SIZE) {
153d7fbcf40SAlexandre Ghiti phys_addr = memblock_phys_alloc(PGDIR_SIZE, PGDIR_SIZE);
154d7fbcf40SAlexandre Ghiti if (phys_addr) {
155d7fbcf40SAlexandre Ghiti set_pgd(pgdp, pfn_pgd(PFN_DOWN(phys_addr), PAGE_KERNEL));
15696f9d4daSAlexandre Ghiti memset(__va(phys_addr), KASAN_SHADOW_INIT, PGDIR_SIZE);
157d7fbcf40SAlexandre Ghiti continue;
158d7fbcf40SAlexandre Ghiti }
159d7fbcf40SAlexandre Ghiti }
16096f9d4daSAlexandre Ghiti
16196f9d4daSAlexandre Ghiti kasan_populate_p4d(pgdp, vaddr, next);
16296f9d4daSAlexandre Ghiti } while (pgdp++, vaddr = next, vaddr != end);
1632efad17eSAlexandre Ghiti }
164d7fbcf40SAlexandre Ghiti
kasan_early_clear_pud(p4d_t * p4dp,unsigned long vaddr,unsigned long end)16596f9d4daSAlexandre Ghiti static void __init kasan_early_clear_pud(p4d_t *p4dp,
16696f9d4daSAlexandre Ghiti unsigned long vaddr, unsigned long end)
16796f9d4daSAlexandre Ghiti {
16896f9d4daSAlexandre Ghiti pud_t *pudp, *base_pud;
16996f9d4daSAlexandre Ghiti unsigned long next;
17096f9d4daSAlexandre Ghiti
17196f9d4daSAlexandre Ghiti if (!pgtable_l4_enabled) {
17296f9d4daSAlexandre Ghiti pudp = (pud_t *)p4dp;
17396f9d4daSAlexandre Ghiti } else {
174*e0316069SAlexandre Ghiti base_pud = pt_ops.get_pud_virt(pfn_to_phys(_p4d_pfn(p4dp_get(p4dp))));
17596f9d4daSAlexandre Ghiti pudp = base_pud + pud_index(vaddr);
17696f9d4daSAlexandre Ghiti }
17796f9d4daSAlexandre Ghiti
17896f9d4daSAlexandre Ghiti do {
17996f9d4daSAlexandre Ghiti next = pud_addr_end(vaddr, end);
18096f9d4daSAlexandre Ghiti
18196f9d4daSAlexandre Ghiti if (IS_ALIGNED(vaddr, PUD_SIZE) && (next - vaddr) >= PUD_SIZE) {
18296f9d4daSAlexandre Ghiti pud_clear(pudp);
18396f9d4daSAlexandre Ghiti continue;
18496f9d4daSAlexandre Ghiti }
18596f9d4daSAlexandre Ghiti
18696f9d4daSAlexandre Ghiti BUG();
18796f9d4daSAlexandre Ghiti } while (pudp++, vaddr = next, vaddr != end);
18896f9d4daSAlexandre Ghiti }
18996f9d4daSAlexandre Ghiti
kasan_early_clear_p4d(pgd_t * pgdp,unsigned long vaddr,unsigned long end)19096f9d4daSAlexandre Ghiti static void __init kasan_early_clear_p4d(pgd_t *pgdp,
19196f9d4daSAlexandre Ghiti unsigned long vaddr, unsigned long end)
19296f9d4daSAlexandre Ghiti {
19396f9d4daSAlexandre Ghiti p4d_t *p4dp, *base_p4d;
19496f9d4daSAlexandre Ghiti unsigned long next;
19596f9d4daSAlexandre Ghiti
19696f9d4daSAlexandre Ghiti if (!pgtable_l5_enabled) {
19796f9d4daSAlexandre Ghiti p4dp = (p4d_t *)pgdp;
19896f9d4daSAlexandre Ghiti } else {
199*e0316069SAlexandre Ghiti base_p4d = pt_ops.get_p4d_virt(pfn_to_phys(_pgd_pfn(pgdp_get(pgdp))));
20096f9d4daSAlexandre Ghiti p4dp = base_p4d + p4d_index(vaddr);
20196f9d4daSAlexandre Ghiti }
20296f9d4daSAlexandre Ghiti
20396f9d4daSAlexandre Ghiti do {
20496f9d4daSAlexandre Ghiti next = p4d_addr_end(vaddr, end);
20596f9d4daSAlexandre Ghiti
20696f9d4daSAlexandre Ghiti if (pgtable_l4_enabled && IS_ALIGNED(vaddr, P4D_SIZE) &&
20796f9d4daSAlexandre Ghiti (next - vaddr) >= P4D_SIZE) {
20896f9d4daSAlexandre Ghiti p4d_clear(p4dp);
20996f9d4daSAlexandre Ghiti continue;
21096f9d4daSAlexandre Ghiti }
21196f9d4daSAlexandre Ghiti
21296f9d4daSAlexandre Ghiti kasan_early_clear_pud(p4dp, vaddr, next);
21396f9d4daSAlexandre Ghiti } while (p4dp++, vaddr = next, vaddr != end);
21496f9d4daSAlexandre Ghiti }
21596f9d4daSAlexandre Ghiti
kasan_early_clear_pgd(pgd_t * pgdp,unsigned long vaddr,unsigned long end)21696f9d4daSAlexandre Ghiti static void __init kasan_early_clear_pgd(pgd_t *pgdp,
21796f9d4daSAlexandre Ghiti unsigned long vaddr, unsigned long end)
21896f9d4daSAlexandre Ghiti {
21996f9d4daSAlexandre Ghiti unsigned long next;
22096f9d4daSAlexandre Ghiti
22196f9d4daSAlexandre Ghiti do {
22296f9d4daSAlexandre Ghiti next = pgd_addr_end(vaddr, end);
22396f9d4daSAlexandre Ghiti
22496f9d4daSAlexandre Ghiti if (pgtable_l5_enabled && IS_ALIGNED(vaddr, PGDIR_SIZE) &&
22596f9d4daSAlexandre Ghiti (next - vaddr) >= PGDIR_SIZE) {
22696f9d4daSAlexandre Ghiti pgd_clear(pgdp);
22796f9d4daSAlexandre Ghiti continue;
22896f9d4daSAlexandre Ghiti }
22996f9d4daSAlexandre Ghiti
23096f9d4daSAlexandre Ghiti kasan_early_clear_p4d(pgdp, vaddr, next);
231cd0334e1SAlexandre Ghiti } while (pgdp++, vaddr = next, vaddr != end);
232cd0334e1SAlexandre Ghiti }
233cd0334e1SAlexandre Ghiti
kasan_early_populate_pud(p4d_t * p4dp,unsigned long vaddr,unsigned long end)234cd0334e1SAlexandre Ghiti static void __init kasan_early_populate_pud(p4d_t *p4dp,
235cd0334e1SAlexandre Ghiti unsigned long vaddr,
236cd0334e1SAlexandre Ghiti unsigned long end)
237cd0334e1SAlexandre Ghiti {
238cd0334e1SAlexandre Ghiti pud_t *pudp, *base_pud;
239cd0334e1SAlexandre Ghiti phys_addr_t phys_addr;
240cd0334e1SAlexandre Ghiti unsigned long next;
241cd0334e1SAlexandre Ghiti
242cd0334e1SAlexandre Ghiti if (!pgtable_l4_enabled) {
243cd0334e1SAlexandre Ghiti pudp = (pud_t *)p4dp;
244cd0334e1SAlexandre Ghiti } else {
245*e0316069SAlexandre Ghiti base_pud = pt_ops.get_pud_virt(pfn_to_phys(_p4d_pfn(p4dp_get(p4dp))));
246cd0334e1SAlexandre Ghiti pudp = base_pud + pud_index(vaddr);
247cd0334e1SAlexandre Ghiti }
248cd0334e1SAlexandre Ghiti
249cd0334e1SAlexandre Ghiti do {
250cd0334e1SAlexandre Ghiti next = pud_addr_end(vaddr, end);
251cd0334e1SAlexandre Ghiti
252*e0316069SAlexandre Ghiti if (pud_none(pudp_get(pudp)) && IS_ALIGNED(vaddr, PUD_SIZE) &&
253cd0334e1SAlexandre Ghiti (next - vaddr) >= PUD_SIZE) {
254cd0334e1SAlexandre Ghiti phys_addr = __pa((uintptr_t)kasan_early_shadow_pmd);
255cd0334e1SAlexandre Ghiti set_pud(pudp, pfn_pud(PFN_DOWN(phys_addr), PAGE_TABLE));
256cd0334e1SAlexandre Ghiti continue;
257cd0334e1SAlexandre Ghiti }
258cd0334e1SAlexandre Ghiti
259cd0334e1SAlexandre Ghiti BUG();
260cd0334e1SAlexandre Ghiti } while (pudp++, vaddr = next, vaddr != end);
261cd0334e1SAlexandre Ghiti }
262cd0334e1SAlexandre Ghiti
kasan_early_populate_p4d(pgd_t * pgdp,unsigned long vaddr,unsigned long end)263cd0334e1SAlexandre Ghiti static void __init kasan_early_populate_p4d(pgd_t *pgdp,
264cd0334e1SAlexandre Ghiti unsigned long vaddr,
265cd0334e1SAlexandre Ghiti unsigned long end)
266cd0334e1SAlexandre Ghiti {
267cd0334e1SAlexandre Ghiti p4d_t *p4dp, *base_p4d;
268cd0334e1SAlexandre Ghiti phys_addr_t phys_addr;
269cd0334e1SAlexandre Ghiti unsigned long next;
270cd0334e1SAlexandre Ghiti
271cd0334e1SAlexandre Ghiti /*
272cd0334e1SAlexandre Ghiti * We can't use pgd_page_vaddr here as it would return a linear
273cd0334e1SAlexandre Ghiti * mapping address but it is not mapped yet, but when populating
274cd0334e1SAlexandre Ghiti * early_pg_dir, we need the physical address and when populating
275cd0334e1SAlexandre Ghiti * swapper_pg_dir, we need the kernel virtual address so use
276cd0334e1SAlexandre Ghiti * pt_ops facility.
277cd0334e1SAlexandre Ghiti * Note that this test is then completely equivalent to
278cd0334e1SAlexandre Ghiti * p4dp = p4d_offset(pgdp, vaddr)
279cd0334e1SAlexandre Ghiti */
280cd0334e1SAlexandre Ghiti if (!pgtable_l5_enabled) {
281cd0334e1SAlexandre Ghiti p4dp = (p4d_t *)pgdp;
282cd0334e1SAlexandre Ghiti } else {
283*e0316069SAlexandre Ghiti base_p4d = pt_ops.get_p4d_virt(pfn_to_phys(_pgd_pfn(pgdp_get(pgdp))));
284cd0334e1SAlexandre Ghiti p4dp = base_p4d + p4d_index(vaddr);
285cd0334e1SAlexandre Ghiti }
286cd0334e1SAlexandre Ghiti
287cd0334e1SAlexandre Ghiti do {
288cd0334e1SAlexandre Ghiti next = p4d_addr_end(vaddr, end);
289cd0334e1SAlexandre Ghiti
290*e0316069SAlexandre Ghiti if (p4d_none(p4dp_get(p4dp)) && IS_ALIGNED(vaddr, P4D_SIZE) &&
291cd0334e1SAlexandre Ghiti (next - vaddr) >= P4D_SIZE) {
292cd0334e1SAlexandre Ghiti phys_addr = __pa((uintptr_t)kasan_early_shadow_pud);
293cd0334e1SAlexandre Ghiti set_p4d(p4dp, pfn_p4d(PFN_DOWN(phys_addr), PAGE_TABLE));
294cd0334e1SAlexandre Ghiti continue;
295cd0334e1SAlexandre Ghiti }
296cd0334e1SAlexandre Ghiti
297cd0334e1SAlexandre Ghiti kasan_early_populate_pud(p4dp, vaddr, next);
298cd0334e1SAlexandre Ghiti } while (p4dp++, vaddr = next, vaddr != end);
299cd0334e1SAlexandre Ghiti }
300cd0334e1SAlexandre Ghiti
kasan_early_populate_pgd(pgd_t * pgdp,unsigned long vaddr,unsigned long end)301cd0334e1SAlexandre Ghiti static void __init kasan_early_populate_pgd(pgd_t *pgdp,
302cd0334e1SAlexandre Ghiti unsigned long vaddr,
303cd0334e1SAlexandre Ghiti unsigned long end)
304cd0334e1SAlexandre Ghiti {
305cd0334e1SAlexandre Ghiti phys_addr_t phys_addr;
306cd0334e1SAlexandre Ghiti unsigned long next;
307cd0334e1SAlexandre Ghiti
308cd0334e1SAlexandre Ghiti do {
309cd0334e1SAlexandre Ghiti next = pgd_addr_end(vaddr, end);
310cd0334e1SAlexandre Ghiti
311*e0316069SAlexandre Ghiti if (pgd_none(pgdp_get(pgdp)) && IS_ALIGNED(vaddr, PGDIR_SIZE) &&
312cd0334e1SAlexandre Ghiti (next - vaddr) >= PGDIR_SIZE) {
313cd0334e1SAlexandre Ghiti phys_addr = __pa((uintptr_t)kasan_early_shadow_p4d);
314cd0334e1SAlexandre Ghiti set_pgd(pgdp, pfn_pgd(PFN_DOWN(phys_addr), PAGE_TABLE));
315cd0334e1SAlexandre Ghiti continue;
316cd0334e1SAlexandre Ghiti }
317cd0334e1SAlexandre Ghiti
318cd0334e1SAlexandre Ghiti kasan_early_populate_p4d(pgdp, vaddr, next);
319d127c19cSAlexandre Ghiti } while (pgdp++, vaddr = next, vaddr != end);
320d127c19cSAlexandre Ghiti }
321d127c19cSAlexandre Ghiti
kasan_early_init(void)3222efad17eSAlexandre Ghiti asmlinkage void __init kasan_early_init(void)
3232efad17eSAlexandre Ghiti {
3242efad17eSAlexandre Ghiti uintptr_t i;
3252efad17eSAlexandre Ghiti
3262efad17eSAlexandre Ghiti BUILD_BUG_ON(KASAN_SHADOW_OFFSET !=
3272efad17eSAlexandre Ghiti KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT)));
3282efad17eSAlexandre Ghiti
3292efad17eSAlexandre Ghiti for (i = 0; i < PTRS_PER_PTE; ++i)
3302efad17eSAlexandre Ghiti set_pte(kasan_early_shadow_pte + i,
331a3d32803SAlexandre Ghiti pfn_pte(virt_to_pfn(kasan_early_shadow_page), PAGE_KERNEL));
3322efad17eSAlexandre Ghiti
3332efad17eSAlexandre Ghiti for (i = 0; i < PTRS_PER_PMD; ++i)
3342efad17eSAlexandre Ghiti set_pmd(kasan_early_shadow_pmd + i,
3352efad17eSAlexandre Ghiti pfn_pmd(PFN_DOWN
3362efad17eSAlexandre Ghiti (__pa((uintptr_t)kasan_early_shadow_pte)),
3372efad17eSAlexandre Ghiti PAGE_TABLE));
3382efad17eSAlexandre Ghiti
3392efad17eSAlexandre Ghiti if (pgtable_l4_enabled) {
3402efad17eSAlexandre Ghiti for (i = 0; i < PTRS_PER_PUD; ++i)
3412efad17eSAlexandre Ghiti set_pud(kasan_early_shadow_pud + i,
3422efad17eSAlexandre Ghiti pfn_pud(PFN_DOWN
3432efad17eSAlexandre Ghiti (__pa(((uintptr_t)kasan_early_shadow_pmd))),
3442efad17eSAlexandre Ghiti PAGE_TABLE));
3452efad17eSAlexandre Ghiti }
3462efad17eSAlexandre Ghiti
3478fbdccd2SQinglin Pan if (pgtable_l5_enabled) {
3488fbdccd2SQinglin Pan for (i = 0; i < PTRS_PER_P4D; ++i)
3498fbdccd2SQinglin Pan set_p4d(kasan_early_shadow_p4d + i,
3508fbdccd2SQinglin Pan pfn_p4d(PFN_DOWN
3518fbdccd2SQinglin Pan (__pa(((uintptr_t)kasan_early_shadow_pud))),
3528fbdccd2SQinglin Pan PAGE_TABLE));
3538fbdccd2SQinglin Pan }
3548fbdccd2SQinglin Pan
355cd0334e1SAlexandre Ghiti kasan_early_populate_pgd(early_pg_dir + pgd_index(KASAN_SHADOW_START),
356cd0334e1SAlexandre Ghiti KASAN_SHADOW_START, KASAN_SHADOW_END);
3572efad17eSAlexandre Ghiti
3582efad17eSAlexandre Ghiti local_flush_tlb_all();
3592efad17eSAlexandre Ghiti }
3602efad17eSAlexandre Ghiti
kasan_swapper_init(void)3612efad17eSAlexandre Ghiti void __init kasan_swapper_init(void)
3622efad17eSAlexandre Ghiti {
363cd0334e1SAlexandre Ghiti kasan_early_populate_pgd(pgd_offset_k(KASAN_SHADOW_START),
364cd0334e1SAlexandre Ghiti KASAN_SHADOW_START, KASAN_SHADOW_END);
3652efad17eSAlexandre Ghiti
3662efad17eSAlexandre Ghiti local_flush_tlb_all();
3672efad17eSAlexandre Ghiti }
3682efad17eSAlexandre Ghiti
kasan_populate(void * start,void * end)369d127c19cSAlexandre Ghiti static void __init kasan_populate(void *start, void *end)
370d127c19cSAlexandre Ghiti {
371d127c19cSAlexandre Ghiti unsigned long vaddr = (unsigned long)start & PAGE_MASK;
372d127c19cSAlexandre Ghiti unsigned long vend = PAGE_ALIGN((unsigned long)end);
373d127c19cSAlexandre Ghiti
374cd0334e1SAlexandre Ghiti kasan_populate_pgd(pgd_offset_k(vaddr), vaddr, vend);
3758ad8b727SNick Hu }
3768ad8b727SNick Hu
kasan_shallow_populate_pud(p4d_t * p4d,unsigned long vaddr,unsigned long end)37796f9d4daSAlexandre Ghiti static void __init kasan_shallow_populate_pud(p4d_t *p4d,
3788fbdccd2SQinglin Pan unsigned long vaddr, unsigned long end)
3798fbdccd2SQinglin Pan {
3808fbdccd2SQinglin Pan unsigned long next;
38196f9d4daSAlexandre Ghiti void *p;
38296f9d4daSAlexandre Ghiti pud_t *pud_k = pud_offset(p4d, vaddr);
383e8a62cc2SAlexandre Ghiti
384e8a62cc2SAlexandre Ghiti do {
385e8a62cc2SAlexandre Ghiti next = pud_addr_end(vaddr, end);
386e8a62cc2SAlexandre Ghiti
387*e0316069SAlexandre Ghiti if (pud_none(pudp_get(pud_k))) {
38896f9d4daSAlexandre Ghiti p = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
38996f9d4daSAlexandre Ghiti set_pud(pud_k, pfn_pud(PFN_DOWN(__pa(p)), PAGE_TABLE));
3908fbdccd2SQinglin Pan continue;
391e8a62cc2SAlexandre Ghiti }
392e8a62cc2SAlexandre Ghiti
39396f9d4daSAlexandre Ghiti BUG();
39496f9d4daSAlexandre Ghiti } while (pud_k++, vaddr = next, vaddr != end);
39596f9d4daSAlexandre Ghiti }
39696f9d4daSAlexandre Ghiti
kasan_shallow_populate_p4d(pgd_t * pgd,unsigned long vaddr,unsigned long end)39796f9d4daSAlexandre Ghiti static void __init kasan_shallow_populate_p4d(pgd_t *pgd,
3988fbdccd2SQinglin Pan unsigned long vaddr, unsigned long end)
3998fbdccd2SQinglin Pan {
4008fbdccd2SQinglin Pan unsigned long next;
40196f9d4daSAlexandre Ghiti void *p;
40296f9d4daSAlexandre Ghiti p4d_t *p4d_k = p4d_offset(pgd, vaddr);
4038fbdccd2SQinglin Pan
4048fbdccd2SQinglin Pan do {
4058fbdccd2SQinglin Pan next = p4d_addr_end(vaddr, end);
4068fbdccd2SQinglin Pan
407*e0316069SAlexandre Ghiti if (p4d_none(p4dp_get(p4d_k))) {
40896f9d4daSAlexandre Ghiti p = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
40996f9d4daSAlexandre Ghiti set_p4d(p4d_k, pfn_p4d(PFN_DOWN(__pa(p)), PAGE_TABLE));
4108fbdccd2SQinglin Pan continue;
4118fbdccd2SQinglin Pan }
4128fbdccd2SQinglin Pan
41396f9d4daSAlexandre Ghiti kasan_shallow_populate_pud(p4d_k, vaddr, end);
41496f9d4daSAlexandre Ghiti } while (p4d_k++, vaddr = next, vaddr != end);
41596f9d4daSAlexandre Ghiti }
4168fbdccd2SQinglin Pan
kasan_shallow_populate_pgd(unsigned long vaddr,unsigned long end)4172da073c1SAlexandre Ghiti static void __init kasan_shallow_populate_pgd(unsigned long vaddr, unsigned long end)
4182da073c1SAlexandre Ghiti {
4192da073c1SAlexandre Ghiti unsigned long next;
4202da073c1SAlexandre Ghiti void *p;
4212da073c1SAlexandre Ghiti pgd_t *pgd_k = pgd_offset_k(vaddr);
4222da073c1SAlexandre Ghiti
4232da073c1SAlexandre Ghiti do {
4242da073c1SAlexandre Ghiti next = pgd_addr_end(vaddr, end);
425e8a62cc2SAlexandre Ghiti
426*e0316069SAlexandre Ghiti if (pgd_none(pgdp_get(pgd_k))) {
4272da073c1SAlexandre Ghiti p = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
4282da073c1SAlexandre Ghiti set_pgd(pgd_k, pfn_pgd(PFN_DOWN(__pa(p)), PAGE_TABLE));
42996f9d4daSAlexandre Ghiti continue;
4302da073c1SAlexandre Ghiti }
431e8a62cc2SAlexandre Ghiti
43296f9d4daSAlexandre Ghiti kasan_shallow_populate_p4d(pgd_k, vaddr, next);
4332da073c1SAlexandre Ghiti } while (pgd_k++, vaddr = next, vaddr != end);
4342da073c1SAlexandre Ghiti }
4352da073c1SAlexandre Ghiti
kasan_shallow_populate(void * start,void * end)43678947bdfSPalmer Dabbelt static void __init kasan_shallow_populate(void *start, void *end)
437e178d670SNylon Chen {
438e178d670SNylon Chen unsigned long vaddr = (unsigned long)start & PAGE_MASK;
439e178d670SNylon Chen unsigned long vend = PAGE_ALIGN((unsigned long)end);
440e178d670SNylon Chen
4412da073c1SAlexandre Ghiti kasan_shallow_populate_pgd(vaddr, vend);
44296f9d4daSAlexandre Ghiti }
44396f9d4daSAlexandre Ghiti
create_tmp_mapping(void)4449bdd9248SAlexandre Ghiti static void __init create_tmp_mapping(void)
44596f9d4daSAlexandre Ghiti {
44696f9d4daSAlexandre Ghiti void *ptr;
44796f9d4daSAlexandre Ghiti p4d_t *base_p4d;
44896f9d4daSAlexandre Ghiti
44996f9d4daSAlexandre Ghiti /*
45096f9d4daSAlexandre Ghiti * We need to clean the early mapping: this is hard to achieve "in-place",
45196f9d4daSAlexandre Ghiti * so install a temporary mapping like arm64 and x86 do.
45296f9d4daSAlexandre Ghiti */
45396f9d4daSAlexandre Ghiti memcpy(tmp_pg_dir, swapper_pg_dir, sizeof(pgd_t) * PTRS_PER_PGD);
45496f9d4daSAlexandre Ghiti
45596f9d4daSAlexandre Ghiti /* Copy the last p4d since it is shared with the kernel mapping. */
45696f9d4daSAlexandre Ghiti if (pgtable_l5_enabled) {
457*e0316069SAlexandre Ghiti ptr = (p4d_t *)pgd_page_vaddr(pgdp_get(pgd_offset_k(KASAN_SHADOW_END)));
45896f9d4daSAlexandre Ghiti memcpy(tmp_p4d, ptr, sizeof(p4d_t) * PTRS_PER_P4D);
45996f9d4daSAlexandre Ghiti set_pgd(&tmp_pg_dir[pgd_index(KASAN_SHADOW_END)],
46096f9d4daSAlexandre Ghiti pfn_pgd(PFN_DOWN(__pa(tmp_p4d)), PAGE_TABLE));
46196f9d4daSAlexandre Ghiti base_p4d = tmp_p4d;
46296f9d4daSAlexandre Ghiti } else {
46396f9d4daSAlexandre Ghiti base_p4d = (p4d_t *)tmp_pg_dir;
46496f9d4daSAlexandre Ghiti }
46596f9d4daSAlexandre Ghiti
46696f9d4daSAlexandre Ghiti /* Copy the last pud since it is shared with the kernel mapping. */
46796f9d4daSAlexandre Ghiti if (pgtable_l4_enabled) {
468*e0316069SAlexandre Ghiti ptr = (pud_t *)p4d_page_vaddr(p4dp_get(base_p4d + p4d_index(KASAN_SHADOW_END)));
46996f9d4daSAlexandre Ghiti memcpy(tmp_pud, ptr, sizeof(pud_t) * PTRS_PER_PUD);
47096f9d4daSAlexandre Ghiti set_p4d(&base_p4d[p4d_index(KASAN_SHADOW_END)],
47196f9d4daSAlexandre Ghiti pfn_p4d(PFN_DOWN(__pa(tmp_pud)), PAGE_TABLE));
47296f9d4daSAlexandre Ghiti }
4738ad8b727SNick Hu }
4748ad8b727SNick Hu
kasan_init(void)4758ad8b727SNick Hu void __init kasan_init(void)
4768ad8b727SNick Hu {
477314b7817SJisheng Zhang phys_addr_t p_start, p_end;
478b10d6bcaSMike Rapoport u64 i;
4798ad8b727SNick Hu
48096f9d4daSAlexandre Ghiti create_tmp_mapping();
48196f9d4daSAlexandre Ghiti csr_write(CSR_SATP, PFN_DOWN(__pa(tmp_pg_dir)) | satp_mode);
48296f9d4daSAlexandre Ghiti
48396f9d4daSAlexandre Ghiti kasan_early_clear_pgd(pgd_offset_k(KASAN_SHADOW_START),
48496f9d4daSAlexandre Ghiti KASAN_SHADOW_START, KASAN_SHADOW_END);
48596f9d4daSAlexandre Ghiti
48696f9d4daSAlexandre Ghiti kasan_populate_early_shadow((void *)kasan_mem_to_shadow((void *)FIXADDR_START),
48796f9d4daSAlexandre Ghiti (void *)kasan_mem_to_shadow((void *)VMALLOC_START));
48896f9d4daSAlexandre Ghiti
48996f9d4daSAlexandre Ghiti if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) {
490e178d670SNylon Chen kasan_shallow_populate(
491e178d670SNylon Chen (void *)kasan_mem_to_shadow((void *)VMALLOC_START),
492e178d670SNylon Chen (void *)kasan_mem_to_shadow((void *)VMALLOC_END));
49396f9d4daSAlexandre Ghiti /* Shallow populate modules and BPF which are vmalloc-allocated */
49496f9d4daSAlexandre Ghiti kasan_shallow_populate(
49596f9d4daSAlexandre Ghiti (void *)kasan_mem_to_shadow((void *)MODULES_VADDR),
49696f9d4daSAlexandre Ghiti (void *)kasan_mem_to_shadow((void *)MODULES_END));
49796f9d4daSAlexandre Ghiti } else {
49896f9d4daSAlexandre Ghiti kasan_populate_early_shadow((void *)kasan_mem_to_shadow((void *)VMALLOC_START),
49996f9d4daSAlexandre Ghiti (void *)kasan_mem_to_shadow((void *)VMALLOC_END));
50096f9d4daSAlexandre Ghiti }
5018ad8b727SNick Hu
5022bfc6cd8SAlexandre Ghiti /* Populate the linear mapping */
503314b7817SJisheng Zhang for_each_mem_range(i, &p_start, &p_end) {
504314b7817SJisheng Zhang void *start = (void *)__va(p_start);
505314b7817SJisheng Zhang void *end = (void *)__va(p_end);
5068ad8b727SNick Hu
5078ad8b727SNick Hu if (start >= end)
5088ad8b727SNick Hu break;
5098ad8b727SNick Hu
510d127c19cSAlexandre Ghiti kasan_populate(kasan_mem_to_shadow(start), kasan_mem_to_shadow(end));
5119d8c7d92SYang Li }
5128ad8b727SNick Hu
51396f9d4daSAlexandre Ghiti /* Populate kernel */
51496f9d4daSAlexandre Ghiti kasan_populate(kasan_mem_to_shadow((const void *)MODULES_END),
5153a02764cSJisheng Zhang kasan_mem_to_shadow((const void *)MODULES_VADDR + SZ_2G));
5162bfc6cd8SAlexandre Ghiti
5178ad8b727SNick Hu for (i = 0; i < PTRS_PER_PTE; i++)
5188ad8b727SNick Hu set_pte(&kasan_early_shadow_pte[i],
5198ad8b727SNick Hu mk_pte(virt_to_page(kasan_early_shadow_page),
5208458ca14SZong Li __pgprot(_PAGE_PRESENT | _PAGE_READ |
5218458ca14SZong Li _PAGE_ACCESSED)));
5228ad8b727SNick Hu
5239484e2aeSAlexandre Ghiti memset(kasan_early_shadow_page, KASAN_SHADOW_INIT, PAGE_SIZE);
5248ad8b727SNick Hu init_task.kasan_depth = 0;
52596f9d4daSAlexandre Ghiti
52696f9d4daSAlexandre Ghiti csr_write(CSR_SATP, PFN_DOWN(__pa(swapper_pg_dir)) | satp_mode);
52796f9d4daSAlexandre Ghiti local_flush_tlb_all();
5288ad8b727SNick Hu }
529