18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  arch/arm/include/asm/kasan_def.h
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (c) 2018 Huawei Technologies Co., Ltd.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  Author: Abbott Liu <liuwenliang@huawei.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifndef __ASM_KASAN_DEF_H
118c2ecf20Sopenharmony_ci#define __ASM_KASAN_DEF_H
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#ifdef CONFIG_KASAN
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/*
168c2ecf20Sopenharmony_ci * Define KASAN_SHADOW_OFFSET,KASAN_SHADOW_START and KASAN_SHADOW_END for
178c2ecf20Sopenharmony_ci * the Arm kernel address sanitizer. We are "stealing" lowmem (the 4GB
188c2ecf20Sopenharmony_ci * addressable by a 32bit architecture) out of the virtual address
198c2ecf20Sopenharmony_ci * space to use as shadow memory for KASan as follows:
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * +----+ 0xffffffff
228c2ecf20Sopenharmony_ci * |    |							\
238c2ecf20Sopenharmony_ci * |    | |-> Static kernel image (vmlinux) BSS and page table
248c2ecf20Sopenharmony_ci * |    |/
258c2ecf20Sopenharmony_ci * +----+ PAGE_OFFSET
268c2ecf20Sopenharmony_ci * |    |							\
278c2ecf20Sopenharmony_ci * |    | |->  Loadable kernel modules virtual address space area
288c2ecf20Sopenharmony_ci * |    |/
298c2ecf20Sopenharmony_ci * +----+ MODULES_VADDR = KASAN_SHADOW_END
308c2ecf20Sopenharmony_ci * |    |						\
318c2ecf20Sopenharmony_ci * |    | |-> The shadow area of kernel virtual address.
328c2ecf20Sopenharmony_ci * |    |/
338c2ecf20Sopenharmony_ci * +----+->  TASK_SIZE (start of kernel space) = KASAN_SHADOW_START the
348c2ecf20Sopenharmony_ci * |    |\   shadow address of MODULES_VADDR
358c2ecf20Sopenharmony_ci * |    | |
368c2ecf20Sopenharmony_ci * |    | |
378c2ecf20Sopenharmony_ci * |    | |-> The user space area in lowmem. The kernel address
388c2ecf20Sopenharmony_ci * |    | |   sanitizer do not use this space, nor does it map it.
398c2ecf20Sopenharmony_ci * |    | |
408c2ecf20Sopenharmony_ci * |    | |
418c2ecf20Sopenharmony_ci * |    | |
428c2ecf20Sopenharmony_ci * |    | |
438c2ecf20Sopenharmony_ci * |    |/
448c2ecf20Sopenharmony_ci * ------ 0
458c2ecf20Sopenharmony_ci *
468c2ecf20Sopenharmony_ci * 1) KASAN_SHADOW_START
478c2ecf20Sopenharmony_ci *   This value begins with the MODULE_VADDR's shadow address. It is the
488c2ecf20Sopenharmony_ci *   start of kernel virtual space. Since we have modules to load, we need
498c2ecf20Sopenharmony_ci *   to cover also that area with shadow memory so we can find memory
508c2ecf20Sopenharmony_ci *   bugs in modules.
518c2ecf20Sopenharmony_ci *
528c2ecf20Sopenharmony_ci * 2) KASAN_SHADOW_END
538c2ecf20Sopenharmony_ci *   This value is the 0x100000000's shadow address: the mapping that would
548c2ecf20Sopenharmony_ci *   be after the end of the kernel memory at 0xffffffff. It is the end of
558c2ecf20Sopenharmony_ci *   kernel address sanitizer shadow area. It is also the start of the
568c2ecf20Sopenharmony_ci *   module area.
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci * 3) KASAN_SHADOW_OFFSET:
598c2ecf20Sopenharmony_ci *   This value is used to map an address to the corresponding shadow
608c2ecf20Sopenharmony_ci *   address by the following formula:
618c2ecf20Sopenharmony_ci *
628c2ecf20Sopenharmony_ci *	shadow_addr = (address >> 3) + KASAN_SHADOW_OFFSET;
638c2ecf20Sopenharmony_ci *
648c2ecf20Sopenharmony_ci *  As you would expect, >> 3 is equal to dividing by 8, meaning each
658c2ecf20Sopenharmony_ci *  byte in the shadow memory covers 8 bytes of kernel memory, so one
668c2ecf20Sopenharmony_ci *  bit shadow memory per byte of kernel memory is used.
678c2ecf20Sopenharmony_ci *
688c2ecf20Sopenharmony_ci *  The KASAN_SHADOW_OFFSET is provided in a Kconfig option depending
698c2ecf20Sopenharmony_ci *  on the VMSPLIT layout of the system: the kernel and userspace can
708c2ecf20Sopenharmony_ci *  split up lowmem in different ways according to needs, so we calculate
718c2ecf20Sopenharmony_ci *  the shadow offset depending on this.
728c2ecf20Sopenharmony_ci */
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#define KASAN_SHADOW_SCALE_SHIFT	3
758c2ecf20Sopenharmony_ci#define KASAN_SHADOW_OFFSET	_AC(CONFIG_KASAN_SHADOW_OFFSET, UL)
768c2ecf20Sopenharmony_ci#define KASAN_SHADOW_END	((UL(1) << (32 - KASAN_SHADOW_SCALE_SHIFT)) \
778c2ecf20Sopenharmony_ci				 + KASAN_SHADOW_OFFSET)
788c2ecf20Sopenharmony_ci#define KASAN_SHADOW_START      ((KASAN_SHADOW_END >> 3) + KASAN_SHADOW_OFFSET)
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci#endif
818c2ecf20Sopenharmony_ci#endif
82