2020-01-06 11:38:32 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
// Copyright (C) 2019 Andes Technology Corporation
|
|
|
|
|
|
|
|
#include <linux/pfn.h>
|
|
|
|
#include <linux/init_task.h>
|
|
|
|
#include <linux/kasan.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/memblock.h>
|
2020-06-08 21:32:38 -07:00
|
|
|
#include <linux/pgtable.h>
|
2020-06-08 21:32:42 -07:00
|
|
|
#include <asm/tlbflush.h>
|
2020-01-06 11:38:32 -07:00
|
|
|
#include <asm/fixmap.h>
|
2021-01-15 22:58:35 -07:00
|
|
|
#include <asm/pgalloc.h>
|
|
|
|
|
2021-12-06 03:46:51 -07:00
|
|
|
/*
|
|
|
|
* Kasan shadow region must lie at a fixed address across sv39, sv48 and sv57
|
|
|
|
* which is right before the kernel.
|
|
|
|
*
|
|
|
|
* For sv39, the region is aligned on PGDIR_SIZE so we only need to populate
|
|
|
|
* the page global directory with kasan_early_shadow_pmd.
|
|
|
|
*
|
2023-02-03 00:52:28 -07:00
|
|
|
* For sv48 and sv57, the region start is aligned on PGDIR_SIZE whereas the end
|
|
|
|
* region is not and then we have to go down to the PUD level.
|
2021-12-06 03:46:51 -07:00
|
|
|
*/
|
2020-01-06 11:38:32 -07:00
|
|
|
|
2023-07-04 00:43:56 -07:00
|
|
|
static pgd_t tmp_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
|
|
|
|
static p4d_t tmp_p4d[PTRS_PER_P4D] __page_aligned_bss;
|
|
|
|
static pud_t tmp_pud[PTRS_PER_PUD] __page_aligned_bss;
|
2020-01-06 11:38:32 -07:00
|
|
|
|
2021-03-29 11:22:21 -07:00
|
|
|
static void __init kasan_populate_pte(pmd_t *pmd, unsigned long vaddr, unsigned long end)
|
2021-02-08 12:30:16 -07:00
|
|
|
{
|
|
|
|
phys_addr_t phys_addr;
|
2023-02-03 00:52:28 -07:00
|
|
|
pte_t *ptep, *p;
|
2021-02-08 12:30:16 -07:00
|
|
|
|
2023-12-13 13:30:01 -07:00
|
|
|
if (pmd_none(pmdp_get(pmd))) {
|
2023-02-03 00:52:28 -07:00
|
|
|
p = memblock_alloc(PTRS_PER_PTE * sizeof(pte_t), PAGE_SIZE);
|
|
|
|
set_pmd(pmd, pfn_pmd(PFN_DOWN(__pa(p)), PAGE_TABLE));
|
|
|
|
}
|
2021-02-08 12:30:16 -07:00
|
|
|
|
2023-02-03 00:52:28 -07:00
|
|
|
ptep = pte_offset_kernel(pmd, vaddr);
|
2021-02-08 12:30:16 -07:00
|
|
|
|
|
|
|
do {
|
2023-12-13 13:30:01 -07:00
|
|
|
if (pte_none(ptep_get(ptep))) {
|
2021-02-08 12:30:16 -07:00
|
|
|
phys_addr = memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
|
|
|
|
set_pte(ptep, pfn_pte(PFN_DOWN(phys_addr), PAGE_KERNEL));
|
2023-02-03 00:52:28 -07:00
|
|
|
memset(__va(phys_addr), KASAN_SHADOW_INIT, PAGE_SIZE);
|
2021-02-08 12:30:16 -07:00
|
|
|
}
|
|
|
|
} while (ptep++, vaddr += PAGE_SIZE, vaddr != end);
|
|
|
|
}
|
|
|
|
|
2021-12-06 03:46:51 -07:00
|
|
|
static void __init kasan_populate_pmd(pud_t *pud, unsigned long vaddr, unsigned long end)
|
2021-02-08 12:30:16 -07:00
|
|
|
{
|
|
|
|
phys_addr_t phys_addr;
|
2023-02-03 00:52:28 -07:00
|
|
|
pmd_t *pmdp, *p;
|
2021-02-08 12:30:16 -07:00
|
|
|
unsigned long next;
|
|
|
|
|
2023-12-13 13:30:01 -07:00
|
|
|
if (pud_none(pudp_get(pud))) {
|
2023-02-03 00:52:28 -07:00
|
|
|
p = memblock_alloc(PTRS_PER_PMD * sizeof(pmd_t), PAGE_SIZE);
|
|
|
|
set_pud(pud, pfn_pud(PFN_DOWN(__pa(p)), PAGE_TABLE));
|
2021-12-06 03:46:51 -07:00
|
|
|
}
|
2021-02-08 12:30:16 -07:00
|
|
|
|
2023-02-03 00:52:28 -07:00
|
|
|
pmdp = pmd_offset(pud, vaddr);
|
2021-02-08 12:30:16 -07:00
|
|
|
|
|
|
|
do {
|
|
|
|
next = pmd_addr_end(vaddr, end);
|
2021-02-08 12:30:17 -07:00
|
|
|
|
2023-12-13 13:30:01 -07:00
|
|
|
if (pmd_none(pmdp_get(pmdp)) && IS_ALIGNED(vaddr, PMD_SIZE) &&
|
|
|
|
(next - vaddr) >= PMD_SIZE) {
|
2021-02-08 12:30:17 -07:00
|
|
|
phys_addr = memblock_phys_alloc(PMD_SIZE, PMD_SIZE);
|
|
|
|
if (phys_addr) {
|
|
|
|
set_pmd(pmdp, pfn_pmd(PFN_DOWN(phys_addr), PAGE_KERNEL));
|
2023-02-03 00:52:28 -07:00
|
|
|
memset(__va(phys_addr), KASAN_SHADOW_INIT, PMD_SIZE);
|
2021-02-08 12:30:17 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 12:30:16 -07:00
|
|
|
kasan_populate_pte(pmdp, vaddr, next);
|
|
|
|
} while (pmdp++, vaddr = next, vaddr != end);
|
2021-12-06 03:46:51 -07:00
|
|
|
}
|
|
|
|
|
2023-02-03 00:52:28 -07:00
|
|
|
static void __init kasan_populate_pud(p4d_t *p4d,
|
2023-02-03 00:52:27 -07:00
|
|
|
unsigned long vaddr, unsigned long end)
|
2021-12-06 03:46:51 -07:00
|
|
|
{
|
|
|
|
phys_addr_t phys_addr;
|
2023-02-03 00:52:28 -07:00
|
|
|
pud_t *pudp, *p;
|
2021-12-06 03:46:51 -07:00
|
|
|
unsigned long next;
|
|
|
|
|
2023-12-13 13:30:01 -07:00
|
|
|
if (p4d_none(p4dp_get(p4d))) {
|
2023-02-03 00:52:28 -07:00
|
|
|
p = memblock_alloc(PTRS_PER_PUD * sizeof(pud_t), PAGE_SIZE);
|
|
|
|
set_p4d(p4d, pfn_p4d(PFN_DOWN(__pa(p)), PAGE_TABLE));
|
2021-12-06 03:46:51 -07:00
|
|
|
}
|
|
|
|
|
2023-02-03 00:52:28 -07:00
|
|
|
pudp = pud_offset(p4d, vaddr);
|
2021-12-06 03:46:51 -07:00
|
|
|
|
|
|
|
do {
|
|
|
|
next = pud_addr_end(vaddr, end);
|
|
|
|
|
2023-12-13 13:30:01 -07:00
|
|
|
if (pud_none(pudp_get(pudp)) && IS_ALIGNED(vaddr, PUD_SIZE) &&
|
|
|
|
(next - vaddr) >= PUD_SIZE) {
|
2023-02-03 00:52:27 -07:00
|
|
|
phys_addr = memblock_phys_alloc(PUD_SIZE, PUD_SIZE);
|
|
|
|
if (phys_addr) {
|
|
|
|
set_pud(pudp, pfn_pud(PFN_DOWN(phys_addr), PAGE_KERNEL));
|
2023-02-03 00:52:28 -07:00
|
|
|
memset(__va(phys_addr), KASAN_SHADOW_INIT, PUD_SIZE);
|
2021-12-06 03:46:51 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
kasan_populate_pmd(pudp, vaddr, next);
|
|
|
|
} while (pudp++, vaddr = next, vaddr != end);
|
2021-02-08 12:30:16 -07:00
|
|
|
}
|
|
|
|
|
2022-01-26 19:48:44 -07:00
|
|
|
static void __init kasan_populate_p4d(pgd_t *pgd,
|
2023-02-03 00:52:27 -07:00
|
|
|
unsigned long vaddr, unsigned long end)
|
2022-01-26 19:48:44 -07:00
|
|
|
{
|
|
|
|
phys_addr_t phys_addr;
|
2023-02-03 00:52:28 -07:00
|
|
|
p4d_t *p4dp, *p;
|
2022-01-26 19:48:44 -07:00
|
|
|
unsigned long next;
|
|
|
|
|
2023-12-13 13:30:01 -07:00
|
|
|
if (pgd_none(pgdp_get(pgd))) {
|
2023-02-03 00:52:28 -07:00
|
|
|
p = memblock_alloc(PTRS_PER_P4D * sizeof(p4d_t), PAGE_SIZE);
|
|
|
|
set_pgd(pgd, pfn_pgd(PFN_DOWN(__pa(p)), PAGE_TABLE));
|
2022-01-26 19:48:44 -07:00
|
|
|
}
|
|
|
|
|
2023-02-03 00:52:28 -07:00
|
|
|
p4dp = p4d_offset(pgd, vaddr);
|
2022-01-26 19:48:44 -07:00
|
|
|
|
|
|
|
do {
|
|
|
|
next = p4d_addr_end(vaddr, end);
|
|
|
|
|
2023-12-13 13:30:01 -07:00
|
|
|
if (p4d_none(p4dp_get(p4dp)) && IS_ALIGNED(vaddr, P4D_SIZE) &&
|
|
|
|
(next - vaddr) >= P4D_SIZE) {
|
2023-02-03 00:52:27 -07:00
|
|
|
phys_addr = memblock_phys_alloc(P4D_SIZE, P4D_SIZE);
|
|
|
|
if (phys_addr) {
|
|
|
|
set_p4d(p4dp, pfn_p4d(PFN_DOWN(phys_addr), PAGE_KERNEL));
|
2023-02-03 00:52:28 -07:00
|
|
|
memset(__va(phys_addr), KASAN_SHADOW_INIT, P4D_SIZE);
|
2022-01-26 19:48:44 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 00:52:28 -07:00
|
|
|
kasan_populate_pud(p4dp, vaddr, next);
|
2022-01-26 19:48:44 -07:00
|
|
|
} while (p4dp++, vaddr = next, vaddr != end);
|
|
|
|
}
|
|
|
|
|
2021-12-06 03:46:46 -07:00
|
|
|
static void __init kasan_populate_pgd(pgd_t *pgdp,
|
2023-02-03 00:52:27 -07:00
|
|
|
unsigned long vaddr, unsigned long end)
|
2021-02-08 12:30:16 -07:00
|
|
|
{
|
|
|
|
phys_addr_t phys_addr;
|
|
|
|
unsigned long next;
|
|
|
|
|
|
|
|
do {
|
|
|
|
next = pgd_addr_end(vaddr, end);
|
2021-02-08 12:30:17 -07:00
|
|
|
|
2023-12-13 13:30:01 -07:00
|
|
|
if (pgd_none(pgdp_get(pgdp)) && IS_ALIGNED(vaddr, PGDIR_SIZE) &&
|
2023-02-03 00:52:28 -07:00
|
|
|
(next - vaddr) >= PGDIR_SIZE) {
|
|
|
|
phys_addr = memblock_phys_alloc(PGDIR_SIZE, PGDIR_SIZE);
|
|
|
|
if (phys_addr) {
|
|
|
|
set_pgd(pgdp, pfn_pgd(PFN_DOWN(phys_addr), PAGE_KERNEL));
|
|
|
|
memset(__va(phys_addr), KASAN_SHADOW_INIT, PGDIR_SIZE);
|
|
|
|
continue;
|
2021-02-08 12:30:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 00:52:28 -07:00
|
|
|
kasan_populate_p4d(pgdp, vaddr, next);
|
|
|
|
} while (pgdp++, vaddr = next, vaddr != end);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __init kasan_early_clear_pud(p4d_t *p4dp,
|
|
|
|
unsigned long vaddr, unsigned long end)
|
|
|
|
{
|
|
|
|
pud_t *pudp, *base_pud;
|
|
|
|
unsigned long next;
|
|
|
|
|
|
|
|
if (!pgtable_l4_enabled) {
|
|
|
|
pudp = (pud_t *)p4dp;
|
|
|
|
} else {
|
2023-12-13 13:30:01 -07:00
|
|
|
base_pud = pt_ops.get_pud_virt(pfn_to_phys(_p4d_pfn(p4dp_get(p4dp))));
|
2023-02-03 00:52:28 -07:00
|
|
|
pudp = base_pud + pud_index(vaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
next = pud_addr_end(vaddr, end);
|
|
|
|
|
|
|
|
if (IS_ALIGNED(vaddr, PUD_SIZE) && (next - vaddr) >= PUD_SIZE) {
|
|
|
|
pud_clear(pudp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
BUG();
|
|
|
|
} while (pudp++, vaddr = next, vaddr != end);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __init kasan_early_clear_p4d(pgd_t *pgdp,
|
|
|
|
unsigned long vaddr, unsigned long end)
|
|
|
|
{
|
|
|
|
p4d_t *p4dp, *base_p4d;
|
|
|
|
unsigned long next;
|
|
|
|
|
|
|
|
if (!pgtable_l5_enabled) {
|
|
|
|
p4dp = (p4d_t *)pgdp;
|
|
|
|
} else {
|
2023-12-13 13:30:01 -07:00
|
|
|
base_p4d = pt_ops.get_p4d_virt(pfn_to_phys(_pgd_pfn(pgdp_get(pgdp))));
|
2023-02-03 00:52:28 -07:00
|
|
|
p4dp = base_p4d + p4d_index(vaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
next = p4d_addr_end(vaddr, end);
|
|
|
|
|
|
|
|
if (pgtable_l4_enabled && IS_ALIGNED(vaddr, P4D_SIZE) &&
|
|
|
|
(next - vaddr) >= P4D_SIZE) {
|
|
|
|
p4d_clear(p4dp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
kasan_early_clear_pud(p4dp, vaddr, next);
|
|
|
|
} while (p4dp++, vaddr = next, vaddr != end);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __init kasan_early_clear_pgd(pgd_t *pgdp,
|
|
|
|
unsigned long vaddr, unsigned long end)
|
|
|
|
{
|
|
|
|
unsigned long next;
|
|
|
|
|
|
|
|
do {
|
|
|
|
next = pgd_addr_end(vaddr, end);
|
|
|
|
|
|
|
|
if (pgtable_l5_enabled && IS_ALIGNED(vaddr, PGDIR_SIZE) &&
|
|
|
|
(next - vaddr) >= PGDIR_SIZE) {
|
|
|
|
pgd_clear(pgdp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
kasan_early_clear_p4d(pgdp, vaddr, next);
|
2023-02-03 00:52:27 -07:00
|
|
|
} while (pgdp++, vaddr = next, vaddr != end);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __init kasan_early_populate_pud(p4d_t *p4dp,
|
|
|
|
unsigned long vaddr,
|
|
|
|
unsigned long end)
|
|
|
|
{
|
|
|
|
pud_t *pudp, *base_pud;
|
|
|
|
phys_addr_t phys_addr;
|
|
|
|
unsigned long next;
|
|
|
|
|
|
|
|
if (!pgtable_l4_enabled) {
|
|
|
|
pudp = (pud_t *)p4dp;
|
|
|
|
} else {
|
2023-12-13 13:30:01 -07:00
|
|
|
base_pud = pt_ops.get_pud_virt(pfn_to_phys(_p4d_pfn(p4dp_get(p4dp))));
|
2023-02-03 00:52:27 -07:00
|
|
|
pudp = base_pud + pud_index(vaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
next = pud_addr_end(vaddr, end);
|
|
|
|
|
2023-12-13 13:30:01 -07:00
|
|
|
if (pud_none(pudp_get(pudp)) && IS_ALIGNED(vaddr, PUD_SIZE) &&
|
2023-02-03 00:52:27 -07:00
|
|
|
(next - vaddr) >= PUD_SIZE) {
|
|
|
|
phys_addr = __pa((uintptr_t)kasan_early_shadow_pmd);
|
|
|
|
set_pud(pudp, pfn_pud(PFN_DOWN(phys_addr), PAGE_TABLE));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
BUG();
|
|
|
|
} while (pudp++, vaddr = next, vaddr != end);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __init kasan_early_populate_p4d(pgd_t *pgdp,
|
|
|
|
unsigned long vaddr,
|
|
|
|
unsigned long end)
|
|
|
|
{
|
|
|
|
p4d_t *p4dp, *base_p4d;
|
|
|
|
phys_addr_t phys_addr;
|
|
|
|
unsigned long next;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We can't use pgd_page_vaddr here as it would return a linear
|
|
|
|
* mapping address but it is not mapped yet, but when populating
|
|
|
|
* early_pg_dir, we need the physical address and when populating
|
|
|
|
* swapper_pg_dir, we need the kernel virtual address so use
|
|
|
|
* pt_ops facility.
|
|
|
|
* Note that this test is then completely equivalent to
|
|
|
|
* p4dp = p4d_offset(pgdp, vaddr)
|
|
|
|
*/
|
|
|
|
if (!pgtable_l5_enabled) {
|
|
|
|
p4dp = (p4d_t *)pgdp;
|
|
|
|
} else {
|
2023-12-13 13:30:01 -07:00
|
|
|
base_p4d = pt_ops.get_p4d_virt(pfn_to_phys(_pgd_pfn(pgdp_get(pgdp))));
|
2023-02-03 00:52:27 -07:00
|
|
|
p4dp = base_p4d + p4d_index(vaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
next = p4d_addr_end(vaddr, end);
|
|
|
|
|
2023-12-13 13:30:01 -07:00
|
|
|
if (p4d_none(p4dp_get(p4dp)) && IS_ALIGNED(vaddr, P4D_SIZE) &&
|
2023-02-03 00:52:27 -07:00
|
|
|
(next - vaddr) >= P4D_SIZE) {
|
|
|
|
phys_addr = __pa((uintptr_t)kasan_early_shadow_pud);
|
|
|
|
set_p4d(p4dp, pfn_p4d(PFN_DOWN(phys_addr), PAGE_TABLE));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
kasan_early_populate_pud(p4dp, vaddr, next);
|
|
|
|
} while (p4dp++, vaddr = next, vaddr != end);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __init kasan_early_populate_pgd(pgd_t *pgdp,
|
|
|
|
unsigned long vaddr,
|
|
|
|
unsigned long end)
|
|
|
|
{
|
|
|
|
phys_addr_t phys_addr;
|
|
|
|
unsigned long next;
|
|
|
|
|
|
|
|
do {
|
|
|
|
next = pgd_addr_end(vaddr, end);
|
|
|
|
|
2023-12-13 13:30:01 -07:00
|
|
|
if (pgd_none(pgdp_get(pgdp)) && IS_ALIGNED(vaddr, PGDIR_SIZE) &&
|
2023-02-03 00:52:27 -07:00
|
|
|
(next - vaddr) >= PGDIR_SIZE) {
|
|
|
|
phys_addr = __pa((uintptr_t)kasan_early_shadow_p4d);
|
|
|
|
set_pgd(pgdp, pfn_pgd(PFN_DOWN(phys_addr), PAGE_TABLE));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
kasan_early_populate_p4d(pgdp, vaddr, next);
|
2021-02-08 12:30:16 -07:00
|
|
|
} while (pgdp++, vaddr = next, vaddr != end);
|
|
|
|
}
|
|
|
|
|
2021-12-06 03:46:46 -07:00
|
|
|
asmlinkage void __init kasan_early_init(void)
|
|
|
|
{
|
|
|
|
uintptr_t i;
|
|
|
|
|
|
|
|
BUILD_BUG_ON(KASAN_SHADOW_OFFSET !=
|
|
|
|
KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT)));
|
|
|
|
|
|
|
|
for (i = 0; i < PTRS_PER_PTE; ++i)
|
|
|
|
set_pte(kasan_early_shadow_pte + i,
|
2022-02-25 05:39:49 -07:00
|
|
|
pfn_pte(virt_to_pfn(kasan_early_shadow_page), PAGE_KERNEL));
|
2021-12-06 03:46:46 -07:00
|
|
|
|
|
|
|
for (i = 0; i < PTRS_PER_PMD; ++i)
|
|
|
|
set_pmd(kasan_early_shadow_pmd + i,
|
|
|
|
pfn_pmd(PFN_DOWN
|
|
|
|
(__pa((uintptr_t)kasan_early_shadow_pte)),
|
|
|
|
PAGE_TABLE));
|
|
|
|
|
|
|
|
if (pgtable_l4_enabled) {
|
|
|
|
for (i = 0; i < PTRS_PER_PUD; ++i)
|
|
|
|
set_pud(kasan_early_shadow_pud + i,
|
|
|
|
pfn_pud(PFN_DOWN
|
|
|
|
(__pa(((uintptr_t)kasan_early_shadow_pmd))),
|
|
|
|
PAGE_TABLE));
|
|
|
|
}
|
|
|
|
|
2022-01-26 19:48:44 -07:00
|
|
|
if (pgtable_l5_enabled) {
|
|
|
|
for (i = 0; i < PTRS_PER_P4D; ++i)
|
|
|
|
set_p4d(kasan_early_shadow_p4d + i,
|
|
|
|
pfn_p4d(PFN_DOWN
|
|
|
|
(__pa(((uintptr_t)kasan_early_shadow_pud))),
|
|
|
|
PAGE_TABLE));
|
|
|
|
}
|
|
|
|
|
2023-02-03 00:52:27 -07:00
|
|
|
kasan_early_populate_pgd(early_pg_dir + pgd_index(KASAN_SHADOW_START),
|
|
|
|
KASAN_SHADOW_START, KASAN_SHADOW_END);
|
2021-12-06 03:46:46 -07:00
|
|
|
|
|
|
|
local_flush_tlb_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
void __init kasan_swapper_init(void)
|
|
|
|
{
|
2023-02-03 00:52:27 -07:00
|
|
|
kasan_early_populate_pgd(pgd_offset_k(KASAN_SHADOW_START),
|
|
|
|
KASAN_SHADOW_START, KASAN_SHADOW_END);
|
2021-12-06 03:46:46 -07:00
|
|
|
|
|
|
|
local_flush_tlb_all();
|
|
|
|
}
|
|
|
|
|
2021-02-08 12:30:16 -07:00
|
|
|
static void __init kasan_populate(void *start, void *end)
|
2020-01-06 11:38:32 -07:00
|
|
|
{
|
|
|
|
unsigned long vaddr = (unsigned long)start & PAGE_MASK;
|
|
|
|
unsigned long vend = PAGE_ALIGN((unsigned long)end);
|
|
|
|
|
2023-02-03 00:52:27 -07:00
|
|
|
kasan_populate_pgd(pgd_offset_k(vaddr), vaddr, vend);
|
2022-01-26 19:48:44 -07:00
|
|
|
}
|
|
|
|
|
2023-02-03 00:52:28 -07:00
|
|
|
static void __init kasan_shallow_populate_pud(p4d_t *p4d,
|
2022-01-26 19:48:44 -07:00
|
|
|
unsigned long vaddr, unsigned long end)
|
2021-12-06 03:46:51 -07:00
|
|
|
{
|
|
|
|
unsigned long next;
|
2023-02-03 00:52:28 -07:00
|
|
|
void *p;
|
|
|
|
pud_t *pud_k = pud_offset(p4d, vaddr);
|
2021-12-06 03:46:51 -07:00
|
|
|
|
|
|
|
do {
|
|
|
|
next = pud_addr_end(vaddr, end);
|
|
|
|
|
2023-12-13 13:30:01 -07:00
|
|
|
if (pud_none(pudp_get(pud_k))) {
|
2023-02-03 00:52:28 -07:00
|
|
|
p = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
|
|
|
|
set_pud(pud_k, pfn_pud(PFN_DOWN(__pa(p)), PAGE_TABLE));
|
2022-01-26 19:48:44 -07:00
|
|
|
continue;
|
2023-02-03 00:52:28 -07:00
|
|
|
}
|
2022-01-26 19:48:44 -07:00
|
|
|
|
2023-02-03 00:52:28 -07:00
|
|
|
BUG();
|
|
|
|
} while (pud_k++, vaddr = next, vaddr != end);
|
2021-12-06 03:46:51 -07:00
|
|
|
}
|
|
|
|
|
2023-02-03 00:52:28 -07:00
|
|
|
static void __init kasan_shallow_populate_p4d(pgd_t *pgd,
|
2022-01-26 19:48:44 -07:00
|
|
|
unsigned long vaddr, unsigned long end)
|
|
|
|
{
|
|
|
|
unsigned long next;
|
2023-02-03 00:52:28 -07:00
|
|
|
void *p;
|
|
|
|
p4d_t *p4d_k = p4d_offset(pgd, vaddr);
|
2022-01-26 19:48:44 -07:00
|
|
|
|
|
|
|
do {
|
|
|
|
next = p4d_addr_end(vaddr, end);
|
|
|
|
|
2023-12-13 13:30:01 -07:00
|
|
|
if (p4d_none(p4dp_get(p4d_k))) {
|
2023-02-03 00:52:28 -07:00
|
|
|
p = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
|
|
|
|
set_p4d(p4d_k, pfn_p4d(PFN_DOWN(__pa(p)), PAGE_TABLE));
|
2022-01-26 19:48:44 -07:00
|
|
|
continue;
|
2023-02-03 00:52:28 -07:00
|
|
|
}
|
2022-01-26 19:48:44 -07:00
|
|
|
|
2023-02-03 00:52:28 -07:00
|
|
|
kasan_shallow_populate_pud(p4d_k, vaddr, end);
|
|
|
|
} while (p4d_k++, vaddr = next, vaddr != end);
|
2022-01-26 19:48:44 -07:00
|
|
|
}
|
|
|
|
|
2021-03-13 01:45:05 -07:00
|
|
|
static void __init kasan_shallow_populate_pgd(unsigned long vaddr, unsigned long end)
|
2021-01-15 22:58:35 -07:00
|
|
|
{
|
2021-03-13 01:45:05 -07:00
|
|
|
unsigned long next;
|
2021-01-15 22:58:35 -07:00
|
|
|
void *p;
|
2021-03-13 01:45:05 -07:00
|
|
|
pgd_t *pgd_k = pgd_offset_k(vaddr);
|
|
|
|
|
|
|
|
do {
|
|
|
|
next = pgd_addr_end(vaddr, end);
|
2021-12-06 03:46:51 -07:00
|
|
|
|
2023-12-13 13:30:01 -07:00
|
|
|
if (pgd_none(pgdp_get(pgd_k))) {
|
2021-03-13 01:45:05 -07:00
|
|
|
p = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
|
|
|
|
set_pgd(pgd_k, pfn_pgd(PFN_DOWN(__pa(p)), PAGE_TABLE));
|
2021-12-06 03:46:51 -07:00
|
|
|
continue;
|
2023-02-03 00:52:28 -07:00
|
|
|
}
|
2021-12-06 03:46:51 -07:00
|
|
|
|
2023-02-03 00:52:28 -07:00
|
|
|
kasan_shallow_populate_p4d(pgd_k, vaddr, next);
|
2021-03-13 01:45:05 -07:00
|
|
|
} while (pgd_k++, vaddr = next, vaddr != end);
|
|
|
|
}
|
|
|
|
|
2021-03-16 22:01:04 -07:00
|
|
|
static void __init kasan_shallow_populate(void *start, void *end)
|
2021-01-15 22:58:35 -07:00
|
|
|
{
|
|
|
|
unsigned long vaddr = (unsigned long)start & PAGE_MASK;
|
|
|
|
unsigned long vend = PAGE_ALIGN((unsigned long)end);
|
2021-03-13 01:45:04 -07:00
|
|
|
|
2021-03-13 01:45:05 -07:00
|
|
|
kasan_shallow_populate_pgd(vaddr, vend);
|
2023-02-03 00:52:28 -07:00
|
|
|
}
|
|
|
|
|
2023-12-12 14:34:57 -07:00
|
|
|
#ifdef CONFIG_KASAN_VMALLOC
|
|
|
|
void __init kasan_populate_early_vm_area_shadow(void *start, unsigned long size)
|
|
|
|
{
|
|
|
|
kasan_populate(kasan_mem_to_shadow(start),
|
|
|
|
kasan_mem_to_shadow(start + size));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-07-04 00:43:57 -07:00
|
|
|
static void __init create_tmp_mapping(void)
|
2023-02-03 00:52:28 -07:00
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
p4d_t *base_p4d;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to clean the early mapping: this is hard to achieve "in-place",
|
|
|
|
* so install a temporary mapping like arm64 and x86 do.
|
|
|
|
*/
|
|
|
|
memcpy(tmp_pg_dir, swapper_pg_dir, sizeof(pgd_t) * PTRS_PER_PGD);
|
|
|
|
|
|
|
|
/* Copy the last p4d since it is shared with the kernel mapping. */
|
|
|
|
if (pgtable_l5_enabled) {
|
2023-12-13 13:30:01 -07:00
|
|
|
ptr = (p4d_t *)pgd_page_vaddr(pgdp_get(pgd_offset_k(KASAN_SHADOW_END)));
|
2023-02-03 00:52:28 -07:00
|
|
|
memcpy(tmp_p4d, ptr, sizeof(p4d_t) * PTRS_PER_P4D);
|
|
|
|
set_pgd(&tmp_pg_dir[pgd_index(KASAN_SHADOW_END)],
|
|
|
|
pfn_pgd(PFN_DOWN(__pa(tmp_p4d)), PAGE_TABLE));
|
|
|
|
base_p4d = tmp_p4d;
|
|
|
|
} else {
|
|
|
|
base_p4d = (p4d_t *)tmp_pg_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the last pud since it is shared with the kernel mapping. */
|
|
|
|
if (pgtable_l4_enabled) {
|
2023-12-13 13:30:01 -07:00
|
|
|
ptr = (pud_t *)p4d_page_vaddr(p4dp_get(base_p4d + p4d_index(KASAN_SHADOW_END)));
|
2023-02-03 00:52:28 -07:00
|
|
|
memcpy(tmp_pud, ptr, sizeof(pud_t) * PTRS_PER_PUD);
|
|
|
|
set_p4d(&base_p4d[p4d_index(KASAN_SHADOW_END)],
|
|
|
|
pfn_p4d(PFN_DOWN(__pa(tmp_pud)), PAGE_TABLE));
|
|
|
|
}
|
2020-01-06 11:38:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void __init kasan_init(void)
|
|
|
|
{
|
2021-06-18 07:01:36 -07:00
|
|
|
phys_addr_t p_start, p_end;
|
2020-10-13 16:58:08 -07:00
|
|
|
u64 i;
|
2020-01-06 11:38:32 -07:00
|
|
|
|
2023-02-03 00:52:28 -07:00
|
|
|
create_tmp_mapping();
|
|
|
|
csr_write(CSR_SATP, PFN_DOWN(__pa(tmp_pg_dir)) | satp_mode);
|
|
|
|
|
|
|
|
kasan_early_clear_pgd(pgd_offset_k(KASAN_SHADOW_START),
|
|
|
|
KASAN_SHADOW_START, KASAN_SHADOW_END);
|
|
|
|
|
|
|
|
kasan_populate_early_shadow((void *)kasan_mem_to_shadow((void *)FIXADDR_START),
|
|
|
|
(void *)kasan_mem_to_shadow((void *)VMALLOC_START));
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) {
|
2021-01-15 22:58:35 -07:00
|
|
|
kasan_shallow_populate(
|
|
|
|
(void *)kasan_mem_to_shadow((void *)VMALLOC_START),
|
|
|
|
(void *)kasan_mem_to_shadow((void *)VMALLOC_END));
|
2023-02-03 00:52:28 -07:00
|
|
|
/* Shallow populate modules and BPF which are vmalloc-allocated */
|
|
|
|
kasan_shallow_populate(
|
|
|
|
(void *)kasan_mem_to_shadow((void *)MODULES_VADDR),
|
|
|
|
(void *)kasan_mem_to_shadow((void *)MODULES_END));
|
|
|
|
} else {
|
|
|
|
kasan_populate_early_shadow((void *)kasan_mem_to_shadow((void *)VMALLOC_START),
|
|
|
|
(void *)kasan_mem_to_shadow((void *)VMALLOC_END));
|
|
|
|
}
|
2020-01-06 11:38:32 -07:00
|
|
|
|
2021-04-11 09:41:44 -07:00
|
|
|
/* Populate the linear mapping */
|
2021-06-18 07:01:36 -07:00
|
|
|
for_each_mem_range(i, &p_start, &p_end) {
|
|
|
|
void *start = (void *)__va(p_start);
|
|
|
|
void *end = (void *)__va(p_end);
|
2020-01-06 11:38:32 -07:00
|
|
|
|
|
|
|
if (start >= end)
|
|
|
|
break;
|
|
|
|
|
2021-02-08 12:30:16 -07:00
|
|
|
kasan_populate(kasan_mem_to_shadow(start), kasan_mem_to_shadow(end));
|
2021-03-22 01:38:36 -07:00
|
|
|
}
|
2020-01-06 11:38:32 -07:00
|
|
|
|
2023-02-03 00:52:28 -07:00
|
|
|
/* Populate kernel */
|
|
|
|
kasan_populate(kasan_mem_to_shadow((const void *)MODULES_END),
|
riscv: Ensure BPF_JIT_REGION_START aligned with PMD size
Andreas reported commit fc8504765ec5 ("riscv: bpf: Avoid breaking W^X")
breaks booting with one kind of defconfig, I reproduced a kernel panic
with the defconfig:
[ 0.138553] Unable to handle kernel paging request at virtual address ffffffff81201220
[ 0.139159] Oops [#1]
[ 0.139303] Modules linked in:
[ 0.139601] CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.13.0-rc5-default+ #1
[ 0.139934] Hardware name: riscv-virtio,qemu (DT)
[ 0.140193] epc : __memset+0xc4/0xfc
[ 0.140416] ra : skb_flow_dissector_init+0x1e/0x82
[ 0.140609] epc : ffffffff8029806c ra : ffffffff8033be78 sp : ffffffe001647da0
[ 0.140878] gp : ffffffff81134b08 tp : ffffffe001654380 t0 : ffffffff81201158
[ 0.141156] t1 : 0000000000000002 t2 : 0000000000000154 s0 : ffffffe001647dd0
[ 0.141424] s1 : ffffffff80a43250 a0 : ffffffff81201220 a1 : 0000000000000000
[ 0.141654] a2 : 000000000000003c a3 : ffffffff81201258 a4 : 0000000000000064
[ 0.141893] a5 : ffffffff8029806c a6 : 0000000000000040 a7 : ffffffffffffffff
[ 0.142126] s2 : ffffffff81201220 s3 : 0000000000000009 s4 : ffffffff81135088
[ 0.142353] s5 : ffffffff81135038 s6 : ffffffff8080ce80 s7 : ffffffff80800438
[ 0.142584] s8 : ffffffff80bc6578 s9 : 0000000000000008 s10: ffffffff806000ac
[ 0.142810] s11: 0000000000000000 t3 : fffffffffffffffc t4 : 0000000000000000
[ 0.143042] t5 : 0000000000000155 t6 : 00000000000003ff
[ 0.143220] status: 0000000000000120 badaddr: ffffffff81201220 cause: 000000000000000f
[ 0.143560] [<ffffffff8029806c>] __memset+0xc4/0xfc
[ 0.143859] [<ffffffff8061e984>] init_default_flow_dissectors+0x22/0x60
[ 0.144092] [<ffffffff800010fc>] do_one_initcall+0x3e/0x168
[ 0.144278] [<ffffffff80600df0>] kernel_init_freeable+0x1c8/0x224
[ 0.144479] [<ffffffff804868a8>] kernel_init+0x12/0x110
[ 0.144658] [<ffffffff800022de>] ret_from_exception+0x0/0xc
[ 0.145124] ---[ end trace f1e9643daa46d591 ]---
After some investigation, I think I found the root cause: commit
2bfc6cd81bd ("move kernel mapping outside of linear mapping") moves
BPF JIT region after the kernel:
| #define BPF_JIT_REGION_START PFN_ALIGN((unsigned long)&_end)
The &_end is unlikely aligned with PMD size, so the front bpf jit
region sits with part of kernel .data section in one PMD size mapping.
But kernel is mapped in PMD SIZE, when bpf_jit_binary_lock_ro() is
called to make the first bpf jit prog ROX, we will make part of kernel
.data section RO too, so when we write to, for example memset the
.data section, MMU will trigger a store page fault.
To fix the issue, we need to ensure the BPF JIT region is PMD size
aligned. This patch acchieve this goal by restoring the BPF JIT region
to original position, I.E the 128MB before kernel .text section. The
modification to kasan_init.c is inspired by Alexandre.
Fixes: fc8504765ec5 ("riscv: bpf: Avoid breaking W^X")
Reported-by: Andreas Schwab <schwab@linux-m68k.org>
Signed-off-by: Jisheng Zhang <jszhang@kernel.org>
Signed-off-by: Palmer Dabbelt <palmerdabbelt@google.com>
2021-06-18 07:09:13 -07:00
|
|
|
kasan_mem_to_shadow((const void *)MODULES_VADDR + SZ_2G));
|
2021-04-11 09:41:44 -07:00
|
|
|
|
2020-01-06 11:38:32 -07:00
|
|
|
for (i = 0; i < PTRS_PER_PTE; i++)
|
|
|
|
set_pte(&kasan_early_shadow_pte[i],
|
|
|
|
mk_pte(virt_to_page(kasan_early_shadow_page),
|
2020-02-07 02:52:45 -07:00
|
|
|
__pgprot(_PAGE_PRESENT | _PAGE_READ |
|
|
|
|
_PAGE_ACCESSED)));
|
2020-01-06 11:38:32 -07:00
|
|
|
|
2021-02-08 12:30:15 -07:00
|
|
|
memset(kasan_early_shadow_page, KASAN_SHADOW_INIT, PAGE_SIZE);
|
2020-01-06 11:38:32 -07:00
|
|
|
init_task.kasan_depth = 0;
|
2023-02-03 00:52:28 -07:00
|
|
|
|
|
|
|
csr_write(CSR_SATP, PFN_DOWN(__pa(swapper_pg_dir)) | satp_mode);
|
|
|
|
local_flush_tlb_all();
|
2020-01-06 11:38:32 -07:00
|
|
|
}
|