18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef __ASM_KASAN_H
38c2ecf20Sopenharmony_ci#define __ASM_KASAN_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/linkage.h>
88c2ecf20Sopenharmony_ci#include <linux/mmzone.h>
98c2ecf20Sopenharmony_ci#include <asm/addrspace.h>
108c2ecf20Sopenharmony_ci#include <asm/io.h>
118c2ecf20Sopenharmony_ci#include <asm/pgtable.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#define KASAN_SHADOW_SCALE_SHIFT 3
148c2ecf20Sopenharmony_ci#define KASAN_SHADOW_OFFSET	_AC(CONFIG_KASAN_SHADOW_OFFSET, UL)
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define XRANGE_SHIFT (48)
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/* Valid address length */
198c2ecf20Sopenharmony_ci#define XRANGE_SHADOW_SHIFT	(PGDIR_SHIFT + PGD_ORDER + PAGE_SHIFT - 3)
208c2ecf20Sopenharmony_ci/* Used for taking out the valid address */
218c2ecf20Sopenharmony_ci#define XRANGE_SHADOW_MASK	GENMASK_ULL(XRANGE_SHADOW_SHIFT - 1, 0)
228c2ecf20Sopenharmony_ci/* One segment whole address space size */
238c2ecf20Sopenharmony_ci#define	XRANGE_SIZE		(XRANGE_SHADOW_MASK + 1)
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* 64-bit segment value. */
268c2ecf20Sopenharmony_ci#define XKPRANGE_UC_SEG		(0x8000)
278c2ecf20Sopenharmony_ci#define XKPRANGE_CC_SEG		(0x9000)
288c2ecf20Sopenharmony_ci#define XKVRANGE_VC_SEG		(0xffff)
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/* Cached */
318c2ecf20Sopenharmony_ci#define XKPRANGE_CC_START		CACHE_BASE
328c2ecf20Sopenharmony_ci#define XKPRANGE_CC_SIZE		XRANGE_SIZE
338c2ecf20Sopenharmony_ci#define XKPRANGE_CC_KASAN_OFFSET	(0)
348c2ecf20Sopenharmony_ci#define XKPRANGE_CC_SHADOW_SIZE		(XKPRANGE_CC_SIZE >> KASAN_SHADOW_SCALE_SHIFT)
358c2ecf20Sopenharmony_ci#define XKPRANGE_CC_SHADOW_END		(XKPRANGE_CC_KASAN_OFFSET + XKPRANGE_CC_SHADOW_SIZE)
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* UnCached */
388c2ecf20Sopenharmony_ci#define XKPRANGE_UC_START		UNCACHE_BASE
398c2ecf20Sopenharmony_ci#define XKPRANGE_UC_SIZE		XRANGE_SIZE
408c2ecf20Sopenharmony_ci#define XKPRANGE_UC_KASAN_OFFSET	XKPRANGE_CC_SHADOW_END
418c2ecf20Sopenharmony_ci#define XKPRANGE_UC_SHADOW_SIZE		(XKPRANGE_UC_SIZE >> KASAN_SHADOW_SCALE_SHIFT)
428c2ecf20Sopenharmony_ci#define XKPRANGE_UC_SHADOW_END		(XKPRANGE_UC_KASAN_OFFSET + XKPRANGE_UC_SHADOW_SIZE)
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/* VMALLOC (Cached or UnCached)  */
458c2ecf20Sopenharmony_ci#define XKVRANGE_VC_START		VMALLOC_START
468c2ecf20Sopenharmony_ci#define XKVRANGE_VC_SIZE		round_up(VMEMMAP_END - MODULES_VADDR + 1, PGDIR_SIZE)
478c2ecf20Sopenharmony_ci#define XKVRANGE_VC_KASAN_OFFSET	XKPRANGE_UC_SHADOW_END
488c2ecf20Sopenharmony_ci#define XKVRANGE_VC_SHADOW_SIZE		(XKVRANGE_VC_SIZE >> KASAN_SHADOW_SCALE_SHIFT)
498c2ecf20Sopenharmony_ci#define XKVRANGE_VC_SHADOW_END		(XKVRANGE_VC_KASAN_OFFSET + XKVRANGE_VC_SHADOW_SIZE)
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/* Kasan shadow memory start right after vmalloc. */
528c2ecf20Sopenharmony_ci#define KASAN_SHADOW_START		round_up(VMEMMAP_END, PGDIR_SIZE)
538c2ecf20Sopenharmony_ci#define KASAN_SHADOW_SIZE		(XKVRANGE_VC_SHADOW_END - XKPRANGE_CC_KASAN_OFFSET)
548c2ecf20Sopenharmony_ci#define KASAN_SHADOW_END		round_up(KASAN_SHADOW_START + KASAN_SHADOW_SIZE, PGDIR_SIZE)
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#define XKPRANGE_CC_SHADOW_OFFSET	(KASAN_SHADOW_START + XKPRANGE_CC_KASAN_OFFSET)
578c2ecf20Sopenharmony_ci#define XKPRANGE_UC_SHADOW_OFFSET	(KASAN_SHADOW_START + XKPRANGE_UC_KASAN_OFFSET)
588c2ecf20Sopenharmony_ci#define XKVRANGE_VC_SHADOW_OFFSET	(KASAN_SHADOW_START + XKVRANGE_VC_KASAN_OFFSET)
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ciextern bool kasan_early_stage;
618c2ecf20Sopenharmony_ciextern unsigned char kasan_early_shadow_page[PAGE_SIZE];
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#define kasan_mem_to_shadow kasan_mem_to_shadow
648c2ecf20Sopenharmony_civoid *kasan_mem_to_shadow(const void *addr);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define kasan_shadow_to_mem kasan_shadow_to_mem
678c2ecf20Sopenharmony_ciconst void *kasan_shadow_to_mem(const void *shadow_addr);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci#define addr_has_metadata addr_has_metadata
708c2ecf20Sopenharmony_cistatic inline bool addr_has_metadata(const void *addr)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	return (kasan_mem_to_shadow((void *)addr) != NULL);
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_civoid kasan_init(void);
768c2ecf20Sopenharmony_ciasmlinkage void kasan_early_init(void);
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci#endif
798c2ecf20Sopenharmony_ci#endif
80