xref: /linux/arch/loongarch/include/asm/kasan.h (revision c5f404511890d75c90e4ec06c54f06bd397d96f5)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_KASAN_H
3 #define __ASM_KASAN_H
4 
5 #ifndef __ASSEMBLY__
6 
7 #include <linux/linkage.h>
8 #include <linux/mmzone.h>
9 #include <asm/addrspace.h>
10 #include <asm/io.h>
11 #include <asm/pgtable.h>
12 
13 #define KASAN_SHADOW_SCALE_SHIFT 3
14 #define KASAN_SHADOW_OFFSET	_AC(CONFIG_KASAN_SHADOW_OFFSET, UL)
15 
16 #define XRANGE_SHIFT (48)
17 
18 /* Valid address length */
19 #define XRANGE_SHADOW_SHIFT	min(cpu_vabits, VA_BITS)
20 /* Used for taking out the valid address */
21 #define XRANGE_SHADOW_MASK	GENMASK_ULL(XRANGE_SHADOW_SHIFT - 1, 0)
22 /* One segment whole address space size */
23 #define XRANGE_SIZE		(XRANGE_SHADOW_MASK + 1)
24 
25 /* 64-bit segment value. */
26 #define XKPRANGE_UC_SEG		(0x8000)
27 #define XKPRANGE_CC_SEG		(0x9000)
28 #define XKPRANGE_WC_SEG		(0xa000)
29 #define XKVRANGE_VC_SEG		(0xffff)
30 
31 /* Cached */
32 #define XKPRANGE_CC_START		CACHE_BASE
33 #define XKPRANGE_CC_SIZE		XRANGE_SIZE
34 #define XKPRANGE_CC_KASAN_OFFSET	(0)
35 #define XKPRANGE_CC_SHADOW_SIZE		(XKPRANGE_CC_SIZE >> KASAN_SHADOW_SCALE_SHIFT)
36 #define XKPRANGE_CC_SHADOW_END		(XKPRANGE_CC_KASAN_OFFSET + XKPRANGE_CC_SHADOW_SIZE)
37 
38 /* UnCached */
39 #define XKPRANGE_UC_START		UNCACHE_BASE
40 #define XKPRANGE_UC_SIZE		XRANGE_SIZE
41 #define XKPRANGE_UC_KASAN_OFFSET	XKPRANGE_CC_SHADOW_END
42 #define XKPRANGE_UC_SHADOW_SIZE		(XKPRANGE_UC_SIZE >> KASAN_SHADOW_SCALE_SHIFT)
43 #define XKPRANGE_UC_SHADOW_END		(XKPRANGE_UC_KASAN_OFFSET + XKPRANGE_UC_SHADOW_SIZE)
44 
45 /* WriteCombine */
46 #define XKPRANGE_WC_START		WRITECOMBINE_BASE
47 #define XKPRANGE_WC_SIZE		XRANGE_SIZE
48 #define XKPRANGE_WC_KASAN_OFFSET	XKPRANGE_UC_SHADOW_END
49 #define XKPRANGE_WC_SHADOW_SIZE		(XKPRANGE_WC_SIZE >> KASAN_SHADOW_SCALE_SHIFT)
50 #define XKPRANGE_WC_SHADOW_END		(XKPRANGE_WC_KASAN_OFFSET + XKPRANGE_WC_SHADOW_SIZE)
51 
52 /* VMALLOC (Cached or UnCached)  */
53 #define XKVRANGE_VC_START		MODULES_VADDR
54 #define XKVRANGE_VC_SIZE		round_up(KFENCE_AREA_END - MODULES_VADDR + 1, PGDIR_SIZE)
55 #define XKVRANGE_VC_KASAN_OFFSET	XKPRANGE_WC_SHADOW_END
56 #define XKVRANGE_VC_SHADOW_SIZE		(XKVRANGE_VC_SIZE >> KASAN_SHADOW_SCALE_SHIFT)
57 #define XKVRANGE_VC_SHADOW_END		(XKVRANGE_VC_KASAN_OFFSET + XKVRANGE_VC_SHADOW_SIZE)
58 
59 /* KAsan shadow memory start right after vmalloc. */
60 #define KASAN_SHADOW_START		round_up(KFENCE_AREA_END, PGDIR_SIZE)
61 #define KASAN_SHADOW_SIZE		(XKVRANGE_VC_SHADOW_END - XKPRANGE_CC_KASAN_OFFSET)
62 #define KASAN_SHADOW_END		(round_up(KASAN_SHADOW_START + KASAN_SHADOW_SIZE, PGDIR_SIZE) - 1)
63 
64 #define XKPRANGE_CC_SHADOW_OFFSET	(KASAN_SHADOW_START + XKPRANGE_CC_KASAN_OFFSET)
65 #define XKPRANGE_UC_SHADOW_OFFSET	(KASAN_SHADOW_START + XKPRANGE_UC_KASAN_OFFSET)
66 #define XKPRANGE_WC_SHADOW_OFFSET	(KASAN_SHADOW_START + XKPRANGE_WC_KASAN_OFFSET)
67 #define XKVRANGE_VC_SHADOW_OFFSET	(KASAN_SHADOW_START + XKVRANGE_VC_KASAN_OFFSET)
68 
69 extern bool kasan_early_stage;
70 extern unsigned char kasan_early_shadow_page[PAGE_SIZE];
71 
72 #define kasan_mem_to_shadow kasan_mem_to_shadow
73 void *kasan_mem_to_shadow(const void *addr);
74 
75 #define kasan_shadow_to_mem kasan_shadow_to_mem
76 const void *kasan_shadow_to_mem(const void *shadow_addr);
77 
78 #define kasan_arch_is_ready kasan_arch_is_ready
kasan_arch_is_ready(void)79 static __always_inline bool kasan_arch_is_ready(void)
80 {
81 	return !kasan_early_stage;
82 }
83 
84 #define addr_has_metadata addr_has_metadata
addr_has_metadata(const void * addr)85 static __always_inline bool addr_has_metadata(const void *addr)
86 {
87 	return (kasan_mem_to_shadow((void *)addr) != NULL);
88 }
89 
90 void kasan_init(void);
91 asmlinkage void kasan_early_init(void);
92 
93 #endif
94 #endif
95