asan.h revision 1.6.4.2 1 1.6.4.2 christos /* $NetBSD: asan.h,v 1.6.4.2 2019/06/10 22:05:43 christos Exp $ */
2 1.6.4.2 christos
3 1.6.4.2 christos /*
4 1.6.4.2 christos * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.6.4.2 christos * All rights reserved.
6 1.6.4.2 christos *
7 1.6.4.2 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.6.4.2 christos * by Maxime Villard.
9 1.6.4.2 christos *
10 1.6.4.2 christos * Redistribution and use in source and binary forms, with or without
11 1.6.4.2 christos * modification, are permitted provided that the following conditions
12 1.6.4.2 christos * are met:
13 1.6.4.2 christos * 1. Redistributions of source code must retain the above copyright
14 1.6.4.2 christos * notice, this list of conditions and the following disclaimer.
15 1.6.4.2 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.6.4.2 christos * notice, this list of conditions and the following disclaimer in the
17 1.6.4.2 christos * documentation and/or other materials provided with the distribution.
18 1.6.4.2 christos *
19 1.6.4.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.6.4.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.6.4.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.6.4.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.6.4.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.6.4.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.6.4.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.6.4.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.6.4.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.6.4.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.6.4.2 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.6.4.2 christos */
31 1.6.4.2 christos
32 1.6.4.2 christos #include <sys/atomic.h>
33 1.6.4.2 christos #include <sys/ksyms.h>
34 1.6.4.2 christos
35 1.6.4.2 christos #include <aarch64/pmap.h>
36 1.6.4.2 christos #include <aarch64/vmparam.h>
37 1.6.4.2 christos #include <aarch64/cpufunc.h>
38 1.6.4.2 christos #include <aarch64/armreg.h>
39 1.6.4.2 christos #include <aarch64/machdep.h>
40 1.6.4.2 christos
41 1.6.4.2 christos #define __MD_VIRTUAL_SHIFT 48 /* 49bit address space, cut half */
42 1.6.4.2 christos #define __MD_CANONICAL_BASE 0xFFFF000000000000
43 1.6.4.2 christos
44 1.6.4.2 christos #define __MD_SHADOW_SIZE (1ULL << (__MD_VIRTUAL_SHIFT - KASAN_SHADOW_SCALE_SHIFT))
45 1.6.4.2 christos #define KASAN_MD_SHADOW_START (AARCH64_KSEG_END)
46 1.6.4.2 christos #define KASAN_MD_SHADOW_END (KASAN_MD_SHADOW_START + __MD_SHADOW_SIZE)
47 1.6.4.2 christos
48 1.6.4.2 christos static bool __md_early __read_mostly = true;
49 1.6.4.2 christos
50 1.6.4.2 christos static inline int8_t *
51 1.6.4.2 christos kasan_md_addr_to_shad(const void *addr)
52 1.6.4.2 christos {
53 1.6.4.2 christos vaddr_t va = (vaddr_t)addr;
54 1.6.4.2 christos return (int8_t *)(KASAN_MD_SHADOW_START +
55 1.6.4.2 christos ((va - __MD_CANONICAL_BASE) >> KASAN_SHADOW_SCALE_SHIFT));
56 1.6.4.2 christos }
57 1.6.4.2 christos
58 1.6.4.2 christos static inline bool
59 1.6.4.2 christos kasan_md_unsupported(vaddr_t addr)
60 1.6.4.2 christos {
61 1.6.4.2 christos return (addr < VM_MIN_KERNEL_ADDRESS) ||
62 1.6.4.2 christos (addr >= VM_KERNEL_IO_ADDRESS);
63 1.6.4.2 christos }
64 1.6.4.2 christos
65 1.6.4.2 christos static paddr_t
66 1.6.4.2 christos __md_palloc(void)
67 1.6.4.2 christos {
68 1.6.4.2 christos paddr_t pa;
69 1.6.4.2 christos
70 1.6.4.2 christos if (__predict_false(__md_early))
71 1.6.4.2 christos pa = (paddr_t)bootpage_alloc();
72 1.6.4.2 christos else
73 1.6.4.2 christos pa = pmap_alloc_pdp(pmap_kernel(), NULL, 0, false);
74 1.6.4.2 christos
75 1.6.4.2 christos return pa;
76 1.6.4.2 christos }
77 1.6.4.2 christos
78 1.6.4.2 christos static void
79 1.6.4.2 christos kasan_md_shadow_map_page(vaddr_t va)
80 1.6.4.2 christos {
81 1.6.4.2 christos pd_entry_t *l0, *l1, *l2, *l3;
82 1.6.4.2 christos paddr_t l0pa, pa;
83 1.6.4.2 christos pd_entry_t pde;
84 1.6.4.2 christos size_t idx;
85 1.6.4.2 christos
86 1.6.4.2 christos l0pa = reg_ttbr1_el1_read();
87 1.6.4.2 christos if (__predict_false(__md_early)) {
88 1.6.4.2 christos l0 = (void *)KERN_PHYSTOV(l0pa);
89 1.6.4.2 christos } else {
90 1.6.4.2 christos l0 = (void *)AARCH64_PA_TO_KVA(l0pa);
91 1.6.4.2 christos }
92 1.6.4.2 christos
93 1.6.4.2 christos idx = l0pde_index(va);
94 1.6.4.2 christos pde = l0[idx];
95 1.6.4.2 christos if (!l0pde_valid(pde)) {
96 1.6.4.2 christos pa = __md_palloc();
97 1.6.4.2 christos atomic_swap_64(&l0[idx], pa | L0_TABLE);
98 1.6.4.2 christos } else {
99 1.6.4.2 christos pa = l0pde_pa(pde);
100 1.6.4.2 christos }
101 1.6.4.2 christos if (__predict_false(__md_early)) {
102 1.6.4.2 christos l1 = (void *)KERN_PHYSTOV(pa);
103 1.6.4.2 christos } else {
104 1.6.4.2 christos l1 = (void *)AARCH64_PA_TO_KVA(pa);
105 1.6.4.2 christos }
106 1.6.4.2 christos
107 1.6.4.2 christos idx = l1pde_index(va);
108 1.6.4.2 christos pde = l1[idx];
109 1.6.4.2 christos if (!l1pde_valid(pde)) {
110 1.6.4.2 christos pa = __md_palloc();
111 1.6.4.2 christos atomic_swap_64(&l1[idx], pa | L1_TABLE);
112 1.6.4.2 christos } else {
113 1.6.4.2 christos pa = l1pde_pa(pde);
114 1.6.4.2 christos }
115 1.6.4.2 christos if (__predict_false(__md_early)) {
116 1.6.4.2 christos l2 = (void *)KERN_PHYSTOV(pa);
117 1.6.4.2 christos } else {
118 1.6.4.2 christos l2 = (void *)AARCH64_PA_TO_KVA(pa);
119 1.6.4.2 christos }
120 1.6.4.2 christos
121 1.6.4.2 christos idx = l2pde_index(va);
122 1.6.4.2 christos pde = l2[idx];
123 1.6.4.2 christos if (!l2pde_valid(pde)) {
124 1.6.4.2 christos pa = __md_palloc();
125 1.6.4.2 christos atomic_swap_64(&l2[idx], pa | L2_TABLE);
126 1.6.4.2 christos } else {
127 1.6.4.2 christos pa = l2pde_pa(pde);
128 1.6.4.2 christos }
129 1.6.4.2 christos if (__predict_false(__md_early)) {
130 1.6.4.2 christos l3 = (void *)KERN_PHYSTOV(pa);
131 1.6.4.2 christos } else {
132 1.6.4.2 christos l3 = (void *)AARCH64_PA_TO_KVA(pa);
133 1.6.4.2 christos }
134 1.6.4.2 christos
135 1.6.4.2 christos idx = l3pte_index(va);
136 1.6.4.2 christos pde = l3[idx];
137 1.6.4.2 christos if (!l3pte_valid(pde)) {
138 1.6.4.2 christos pa = __md_palloc();
139 1.6.4.2 christos atomic_swap_64(&l3[idx], pa | L3_PAGE | LX_BLKPAG_UXN |
140 1.6.4.2 christos LX_BLKPAG_PXN | LX_BLKPAG_AF | LX_BLKPAG_SH_IS |
141 1.6.4.2 christos LX_BLKPAG_AP_RW);
142 1.6.4.2 christos aarch64_tlbi_by_va(va);
143 1.6.4.2 christos }
144 1.6.4.2 christos }
145 1.6.4.2 christos
146 1.6.4.2 christos static void
147 1.6.4.2 christos kasan_md_early_init(void *stack)
148 1.6.4.2 christos {
149 1.6.4.2 christos kasan_shadow_map(stack, USPACE);
150 1.6.4.2 christos __md_early = false;
151 1.6.4.2 christos }
152 1.6.4.2 christos
153 1.6.4.2 christos static void
154 1.6.4.2 christos kasan_md_init(void)
155 1.6.4.2 christos {
156 1.6.4.2 christos vaddr_t eva, dummy;
157 1.6.4.2 christos
158 1.6.4.2 christos CTASSERT((__MD_SHADOW_SIZE / L0_SIZE) == 64);
159 1.6.4.2 christos
160 1.6.4.2 christos /* The VAs we've created until now. */
161 1.6.4.2 christos pmap_virtual_space(&eva, &dummy);
162 1.6.4.2 christos kasan_shadow_map((void *)VM_MIN_KERNEL_ADDRESS,
163 1.6.4.2 christos eva - VM_MIN_KERNEL_ADDRESS);
164 1.6.4.2 christos }
165 1.6.4.2 christos
166 1.6.4.2 christos static inline bool
167 1.6.4.2 christos __md_unwind_end(const char *name)
168 1.6.4.2 christos {
169 1.6.4.2 christos if (!strncmp(name, "el0_trap", 8) ||
170 1.6.4.2 christos !strncmp(name, "el1_trap", 8)) {
171 1.6.4.2 christos return true;
172 1.6.4.2 christos }
173 1.6.4.2 christos
174 1.6.4.2 christos return false;
175 1.6.4.2 christos }
176 1.6.4.2 christos
177 1.6.4.2 christos static void
178 1.6.4.2 christos kasan_md_unwind(void)
179 1.6.4.2 christos {
180 1.6.4.2 christos uint64_t lr, *fp;
181 1.6.4.2 christos const char *mod;
182 1.6.4.2 christos const char *sym;
183 1.6.4.2 christos size_t nsym;
184 1.6.4.2 christos int error;
185 1.6.4.2 christos
186 1.6.4.2 christos fp = (uint64_t *)__builtin_frame_address(0);
187 1.6.4.2 christos nsym = 0;
188 1.6.4.2 christos
189 1.6.4.2 christos while (1) {
190 1.6.4.2 christos /*
191 1.6.4.2 christos * normal stack frame
192 1.6.4.2 christos * fp[0] saved fp(x29) value
193 1.6.4.2 christos * fp[1] saved lr(x30) value
194 1.6.4.2 christos */
195 1.6.4.2 christos lr = fp[1];
196 1.6.4.2 christos
197 1.6.4.2 christos if (lr < VM_MIN_KERNEL_ADDRESS) {
198 1.6.4.2 christos break;
199 1.6.4.2 christos }
200 1.6.4.2 christos error = ksyms_getname(&mod, &sym, (vaddr_t)lr, KSYMS_PROC);
201 1.6.4.2 christos if (error) {
202 1.6.4.2 christos break;
203 1.6.4.2 christos }
204 1.6.4.2 christos printf("#%zu %p in %s <%s>\n", nsym, (void *)lr, sym, mod);
205 1.6.4.2 christos if (__md_unwind_end(sym)) {
206 1.6.4.2 christos break;
207 1.6.4.2 christos }
208 1.6.4.2 christos
209 1.6.4.2 christos fp = (uint64_t *)fp[0];
210 1.6.4.2 christos if (fp == NULL) {
211 1.6.4.2 christos break;
212 1.6.4.2 christos }
213 1.6.4.2 christos nsym++;
214 1.6.4.2 christos
215 1.6.4.2 christos if (nsym >= 15) {
216 1.6.4.2 christos break;
217 1.6.4.2 christos }
218 1.6.4.2 christos }
219 1.6.4.2 christos }
220