Path: blob/master/arch/powerpc/mm/kasan/init_book3s_64.c
26481 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>2021DEFINE_STATIC_KEY_FALSE(powerpc_kasan_enabled_key);2223static void __init kasan_init_phys_region(void *start, void *end)24{25unsigned long k_start, k_end, k_cur;26void *va;2728if (start >= end)29return;3031k_start = ALIGN_DOWN((unsigned long)kasan_mem_to_shadow(start), PAGE_SIZE);32k_end = ALIGN((unsigned long)kasan_mem_to_shadow(end), PAGE_SIZE);3334va = memblock_alloc_or_panic(k_end - k_start, PAGE_SIZE);35for (k_cur = k_start; k_cur < k_end; k_cur += PAGE_SIZE, va += PAGE_SIZE)36map_kernel_page(k_cur, __pa(va), PAGE_KERNEL);37}3839void __init kasan_init(void)40{41/*42* We want to do the following things:43* 1) Map real memory into the shadow for all physical memblocks44* This takes us from c000... to c008...45* 2) Leave a hole over the shadow of vmalloc space. KASAN_VMALLOC46* will manage this for us.47* This takes us from c008... to c00a...48* 3) Map the 'early shadow'/zero page over iomap and vmemmap space.49* This takes us up to where we start at c00e...50*/5152void *k_start = kasan_mem_to_shadow((void *)RADIX_VMALLOC_END);53void *k_end = kasan_mem_to_shadow((void *)RADIX_VMEMMAP_END);54phys_addr_t start, end;55u64 i;56pte_t zero_pte = pfn_pte(virt_to_pfn(kasan_early_shadow_page), PAGE_KERNEL);5758if (!early_radix_enabled()) {59pr_warn("KASAN not enabled as it requires radix!");60return;61}6263for_each_mem_range(i, &start, &end)64kasan_init_phys_region(phys_to_virt(start), phys_to_virt(end));6566for (i = 0; i < PTRS_PER_PTE; i++)67__set_pte_at(&init_mm, (unsigned long)kasan_early_shadow_page,68&kasan_early_shadow_pte[i], zero_pte, 0);6970for (i = 0; i < PTRS_PER_PMD; i++)71pmd_populate_kernel(&init_mm, &kasan_early_shadow_pmd[i],72kasan_early_shadow_pte);7374for (i = 0; i < PTRS_PER_PUD; i++)75pud_populate(&init_mm, &kasan_early_shadow_pud[i],76kasan_early_shadow_pmd);7778/* map the early shadow over the iomap and vmemmap space */79kasan_populate_early_shadow(k_start, k_end);8081/* mark early shadow region as RO and wipe it */82zero_pte = pfn_pte(virt_to_pfn(kasan_early_shadow_page), PAGE_KERNEL_RO);83for (i = 0; i < PTRS_PER_PTE; i++)84__set_pte_at(&init_mm, (unsigned long)kasan_early_shadow_page,85&kasan_early_shadow_pte[i], zero_pte, 0);8687/*88* clear_page relies on some cache info that hasn't been set up yet.89* It ends up looping ~forever and blows up other data.90* Use memset instead.91*/92memset(kasan_early_shadow_page, 0, PAGE_SIZE);9394static_branch_inc(&powerpc_kasan_enabled_key);9596/* Enable error messages */97init_task.kasan_depth = 0;98pr_info("KASAN init done\n");99}100101void __init kasan_early_init(void) { }102103void __init kasan_late_init(void) { }104105106