Path: blob/master/arch/powerpc/mm/kasan/init_book3s_64.c
29274 views
// SPDX-License-Identifier: GPL-2.01/*2* KASAN for 64-bit Book3S powerpc3*4* Copyright 2019-2022, Daniel Axtens, IBM Corporation.5*/67/*8* ppc64 turns on virtual memory late in boot, after calling into generic code9* like the device-tree parser, so it uses this in conjunction with a hook in10* outline mode to avoid invalid access early in boot.11*/1213#define DISABLE_BRANCH_PROFILING1415#include <linux/kasan.h>16#include <linux/printk.h>17#include <linux/sched/task.h>18#include <linux/memblock.h>19#include <asm/pgalloc.h>2021static void __init kasan_init_phys_region(void *start, void *end)22{23unsigned long k_start, k_end, k_cur;24void *va;2526if (start >= end)27return;2829k_start = ALIGN_DOWN((unsigned long)kasan_mem_to_shadow(start), PAGE_SIZE);30k_end = ALIGN((unsigned long)kasan_mem_to_shadow(end), PAGE_SIZE);3132va = memblock_alloc_or_panic(k_end - k_start, PAGE_SIZE);33for (k_cur = k_start; k_cur < k_end; k_cur += PAGE_SIZE, va += PAGE_SIZE)34map_kernel_page(k_cur, __pa(va), PAGE_KERNEL);35}3637void __init kasan_init(void)38{39/*40* We want to do the following things:41* 1) Map real memory into the shadow for all physical memblocks42* This takes us from c000... to c008...43* 2) Leave a hole over the shadow of vmalloc space. KASAN_VMALLOC44* will manage this for us.45* This takes us from c008... to c00a...46* 3) Map the 'early shadow'/zero page over iomap and vmemmap space.47* This takes us up to where we start at c00e...48*/4950void *k_start = kasan_mem_to_shadow((void *)RADIX_VMALLOC_END);51void *k_end = kasan_mem_to_shadow((void *)RADIX_VMEMMAP_END);52phys_addr_t start, end;53u64 i;54pte_t zero_pte = pfn_pte(virt_to_pfn(kasan_early_shadow_page), PAGE_KERNEL);5556if (!early_radix_enabled()) {57pr_warn("KASAN not enabled as it requires radix!");58return;59}6061for_each_mem_range(i, &start, &end)62kasan_init_phys_region(phys_to_virt(start), phys_to_virt(end));6364for (i = 0; i < PTRS_PER_PTE; i++)65__set_pte_at(&init_mm, (unsigned long)kasan_early_shadow_page,66&kasan_early_shadow_pte[i], zero_pte, 0);6768for (i = 0; i < PTRS_PER_PMD; i++)69pmd_populate_kernel(&init_mm, &kasan_early_shadow_pmd[i],70kasan_early_shadow_pte);7172for (i = 0; i < PTRS_PER_PUD; i++)73pud_populate(&init_mm, &kasan_early_shadow_pud[i],74kasan_early_shadow_pmd);7576/* map the early shadow over the iomap and vmemmap space */77kasan_populate_early_shadow(k_start, k_end);7879/* mark early shadow region as RO and wipe it */80zero_pte = pfn_pte(virt_to_pfn(kasan_early_shadow_page), PAGE_KERNEL_RO);81for (i = 0; i < PTRS_PER_PTE; i++)82__set_pte_at(&init_mm, (unsigned long)kasan_early_shadow_page,83&kasan_early_shadow_pte[i], zero_pte, 0);8485/*86* clear_page relies on some cache info that hasn't been set up yet.87* It ends up looping ~forever and blows up other data.88* Use memset instead.89*/90memset(kasan_early_shadow_page, 0, PAGE_SIZE);9192/* Enable error messages */93init_task.kasan_depth = 0;94kasan_init_generic();95}9697void __init kasan_early_init(void) { }9899void __init kasan_late_init(void) { }100101102