162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *  arch/arm/include/asm/kasan_def.h
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *  Copyright (c) 2018 Huawei Technologies Co., Ltd.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci *  Author: Abbott Liu <liuwenliang@huawei.com>
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#ifndef __ASM_KASAN_DEF_H
1162306a36Sopenharmony_ci#define __ASM_KASAN_DEF_H
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#ifdef CONFIG_KASAN
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci/*
1662306a36Sopenharmony_ci * Define KASAN_SHADOW_OFFSET,KASAN_SHADOW_START and KASAN_SHADOW_END for
1762306a36Sopenharmony_ci * the Arm kernel address sanitizer. We are "stealing" lowmem (the 4GB
1862306a36Sopenharmony_ci * addressable by a 32bit architecture) out of the virtual address
1962306a36Sopenharmony_ci * space to use as shadow memory for KASan as follows:
2062306a36Sopenharmony_ci *
2162306a36Sopenharmony_ci * +----+ 0xffffffff
2262306a36Sopenharmony_ci * |    |							\
2362306a36Sopenharmony_ci * |    | |-> Static kernel image (vmlinux) BSS and page table
2462306a36Sopenharmony_ci * |    |/
2562306a36Sopenharmony_ci * +----+ PAGE_OFFSET
2662306a36Sopenharmony_ci * |    |							\
2762306a36Sopenharmony_ci * |    | |->  Loadable kernel modules virtual address space area
2862306a36Sopenharmony_ci * |    |/
2962306a36Sopenharmony_ci * +----+ MODULES_VADDR = KASAN_SHADOW_END
3062306a36Sopenharmony_ci * |    |						\
3162306a36Sopenharmony_ci * |    | |-> The shadow area of kernel virtual address.
3262306a36Sopenharmony_ci * |    |/
3362306a36Sopenharmony_ci * +----+->  TASK_SIZE (start of kernel space) = KASAN_SHADOW_START the
3462306a36Sopenharmony_ci * |    |\   shadow address of MODULES_VADDR
3562306a36Sopenharmony_ci * |    | |
3662306a36Sopenharmony_ci * |    | |
3762306a36Sopenharmony_ci * |    | |-> The user space area in lowmem. The kernel address
3862306a36Sopenharmony_ci * |    | |   sanitizer do not use this space, nor does it map it.
3962306a36Sopenharmony_ci * |    | |
4062306a36Sopenharmony_ci * |    | |
4162306a36Sopenharmony_ci * |    | |
4262306a36Sopenharmony_ci * |    | |
4362306a36Sopenharmony_ci * |    |/
4462306a36Sopenharmony_ci * ------ 0
4562306a36Sopenharmony_ci *
4662306a36Sopenharmony_ci * 1) KASAN_SHADOW_START
4762306a36Sopenharmony_ci *   This value begins with the MODULE_VADDR's shadow address. It is the
4862306a36Sopenharmony_ci *   start of kernel virtual space. Since we have modules to load, we need
4962306a36Sopenharmony_ci *   to cover also that area with shadow memory so we can find memory
5062306a36Sopenharmony_ci *   bugs in modules.
5162306a36Sopenharmony_ci *
5262306a36Sopenharmony_ci * 2) KASAN_SHADOW_END
5362306a36Sopenharmony_ci *   This value is the 0x100000000's shadow address: the mapping that would
5462306a36Sopenharmony_ci *   be after the end of the kernel memory at 0xffffffff. It is the end of
5562306a36Sopenharmony_ci *   kernel address sanitizer shadow area. It is also the start of the
5662306a36Sopenharmony_ci *   module area.
5762306a36Sopenharmony_ci *
5862306a36Sopenharmony_ci * 3) KASAN_SHADOW_OFFSET:
5962306a36Sopenharmony_ci *   This value is used to map an address to the corresponding shadow
6062306a36Sopenharmony_ci *   address by the following formula:
6162306a36Sopenharmony_ci *
6262306a36Sopenharmony_ci *	shadow_addr = (address >> 3) + KASAN_SHADOW_OFFSET;
6362306a36Sopenharmony_ci *
6462306a36Sopenharmony_ci *  As you would expect, >> 3 is equal to dividing by 8, meaning each
6562306a36Sopenharmony_ci *  byte in the shadow memory covers 8 bytes of kernel memory, so one
6662306a36Sopenharmony_ci *  bit shadow memory per byte of kernel memory is used.
6762306a36Sopenharmony_ci *
6862306a36Sopenharmony_ci *  The KASAN_SHADOW_OFFSET is provided in a Kconfig option depending
6962306a36Sopenharmony_ci *  on the VMSPLIT layout of the system: the kernel and userspace can
7062306a36Sopenharmony_ci *  split up lowmem in different ways according to needs, so we calculate
7162306a36Sopenharmony_ci *  the shadow offset depending on this.
7262306a36Sopenharmony_ci */
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci#define KASAN_SHADOW_SCALE_SHIFT	3
7562306a36Sopenharmony_ci#define KASAN_SHADOW_OFFSET	_AC(CONFIG_KASAN_SHADOW_OFFSET, UL)
7662306a36Sopenharmony_ci#define KASAN_SHADOW_END	((UL(1) << (32 - KASAN_SHADOW_SCALE_SHIFT)) \
7762306a36Sopenharmony_ci				 + KASAN_SHADOW_OFFSET)
7862306a36Sopenharmony_ci#define KASAN_SHADOW_START      ((KASAN_SHADOW_END >> 3) + KASAN_SHADOW_OFFSET)
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci#endif
8162306a36Sopenharmony_ci#endif
82