subr_csan.c revision 1.3 1 1.3 maxv /* $NetBSD: subr_csan.c,v 1.3 2019/11/08 12:36:10 maxv Exp $ */
2 1.1 maxv
3 1.1 maxv /*
4 1.1 maxv * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 1.1 maxv * All rights reserved.
6 1.1 maxv *
7 1.1 maxv * This code is derived from software contributed to The NetBSD Foundation
8 1.1 maxv * by Maxime Villard.
9 1.1 maxv *
10 1.1 maxv * Redistribution and use in source and binary forms, with or without
11 1.1 maxv * modification, are permitted provided that the following conditions
12 1.1 maxv * are met:
13 1.1 maxv * 1. Redistributions of source code must retain the above copyright
14 1.1 maxv * notice, this list of conditions and the following disclaimer.
15 1.1 maxv * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 maxv * notice, this list of conditions and the following disclaimer in the
17 1.1 maxv * documentation and/or other materials provided with the distribution.
18 1.1 maxv *
19 1.1 maxv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 maxv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 maxv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 maxv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 maxv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 maxv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 maxv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 maxv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 maxv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 maxv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 maxv * POSSIBILITY OF SUCH DAMAGE.
30 1.1 maxv */
31 1.1 maxv
32 1.1 maxv #include <sys/cdefs.h>
33 1.3 maxv __KERNEL_RCSID(0, "$NetBSD: subr_csan.c,v 1.3 2019/11/08 12:36:10 maxv Exp $");
34 1.1 maxv
35 1.1 maxv #include "opt_kcsan.h"
36 1.1 maxv
37 1.1 maxv #include <sys/param.h>
38 1.1 maxv #include <sys/device.h>
39 1.1 maxv #include <sys/kernel.h>
40 1.1 maxv #include <sys/param.h>
41 1.1 maxv #include <sys/conf.h>
42 1.1 maxv #include <sys/systm.h>
43 1.1 maxv #include <sys/types.h>
44 1.1 maxv #include <sys/csan.h>
45 1.1 maxv #include <sys/cpu.h>
46 1.1 maxv
47 1.1 maxv #ifdef KCSAN_PANIC
48 1.1 maxv #define REPORT panic
49 1.1 maxv #else
50 1.1 maxv #define REPORT printf
51 1.1 maxv #endif
52 1.1 maxv
53 1.1 maxv typedef struct {
54 1.1 maxv uintptr_t addr;
55 1.1 maxv uint32_t size;
56 1.1 maxv bool write:1;
57 1.1 maxv bool atomic:1;
58 1.1 maxv uintptr_t pc;
59 1.1 maxv } csan_cell_t;
60 1.1 maxv
61 1.1 maxv typedef struct {
62 1.1 maxv bool inited;
63 1.1 maxv uint32_t cnt;
64 1.1 maxv csan_cell_t cell;
65 1.1 maxv } csan_cpu_t;
66 1.1 maxv
67 1.1 maxv static csan_cpu_t kcsan_cpus[MAXCPUS];
68 1.1 maxv static bool kcsan_enabled __read_mostly;
69 1.1 maxv
70 1.1 maxv #define __RET_ADDR (uintptr_t)__builtin_return_address(0)
71 1.1 maxv
72 1.2 maxv #define KCSAN_NACCESSES 1024
73 1.2 maxv #define KCSAN_DELAY 10 /* 10 microseconds */
74 1.1 maxv
75 1.1 maxv /* -------------------------------------------------------------------------- */
76 1.1 maxv
77 1.1 maxv /* The MD code. */
78 1.1 maxv #include <machine/csan.h>
79 1.1 maxv
80 1.1 maxv /* -------------------------------------------------------------------------- */
81 1.1 maxv
82 1.1 maxv void
83 1.1 maxv kcsan_init(void)
84 1.1 maxv {
85 1.1 maxv kcsan_enabled = true;
86 1.1 maxv }
87 1.1 maxv
88 1.1 maxv void
89 1.1 maxv kcsan_cpu_init(struct cpu_info *ci)
90 1.1 maxv {
91 1.1 maxv kcsan_cpus[cpu_index(ci)].inited = true;
92 1.1 maxv }
93 1.1 maxv
94 1.1 maxv /* -------------------------------------------------------------------------- */
95 1.1 maxv
96 1.1 maxv static inline void
97 1.1 maxv kcsan_report(csan_cell_t *new, cpuid_t newcpu, csan_cell_t *old, cpuid_t oldcpu)
98 1.1 maxv {
99 1.1 maxv const char *newsym, *oldsym;
100 1.1 maxv
101 1.1 maxv if (ksyms_getname(NULL, &newsym, (vaddr_t)new->pc, KSYMS_PROC) != 0) {
102 1.1 maxv newsym = "Unknown";
103 1.1 maxv }
104 1.1 maxv if (ksyms_getname(NULL, &oldsym, (vaddr_t)old->pc, KSYMS_PROC) != 0) {
105 1.1 maxv oldsym = "Unknown";
106 1.1 maxv }
107 1.1 maxv REPORT("CSan: Racy Access "
108 1.1 maxv "[Cpu%lu %s%s Addr=%p Size=%u PC=%p<%s>] "
109 1.1 maxv "[Cpu%lu %s%s Addr=%p Size=%u PC=%p<%s>]\n",
110 1.1 maxv newcpu,
111 1.1 maxv (new->atomic ? "Atomic " : ""), (new->write ? "Write" : "Read"),
112 1.1 maxv (void *)new->addr, new->size, (void *)new->pc, newsym,
113 1.1 maxv oldcpu,
114 1.1 maxv (old->atomic ? "Atomic " : ""), (old->write ? "Write" : "Read"),
115 1.1 maxv (void *)old->addr, old->size, (void *)old->pc, oldsym);
116 1.1 maxv kcsan_md_unwind();
117 1.1 maxv }
118 1.1 maxv
119 1.1 maxv static inline bool
120 1.1 maxv kcsan_access_is_atomic(csan_cell_t *new, csan_cell_t *old)
121 1.1 maxv {
122 1.1 maxv if (new->write && !new->atomic)
123 1.1 maxv return false;
124 1.1 maxv if (old->write && !old->atomic)
125 1.1 maxv return false;
126 1.1 maxv return true;
127 1.1 maxv }
128 1.1 maxv
129 1.1 maxv static inline void
130 1.1 maxv kcsan_access(uintptr_t addr, size_t size, bool write, bool atomic, uintptr_t pc)
131 1.1 maxv {
132 1.1 maxv csan_cell_t old, new;
133 1.1 maxv csan_cpu_t *cpu;
134 1.1 maxv uint64_t intr;
135 1.1 maxv size_t i;
136 1.1 maxv
137 1.1 maxv if (__predict_false(!kcsan_enabled))
138 1.1 maxv return;
139 1.3 maxv if (__predict_false(kcsan_md_unsupported((vaddr_t)addr)))
140 1.3 maxv return;
141 1.1 maxv
142 1.1 maxv new.addr = addr;
143 1.1 maxv new.size = size;
144 1.1 maxv new.write = write;
145 1.1 maxv new.atomic = atomic;
146 1.1 maxv new.pc = pc;
147 1.1 maxv
148 1.1 maxv for (i = 0; i < ncpu; i++) {
149 1.1 maxv __builtin_memcpy(&old, &kcsan_cpus[i].cell, sizeof(old));
150 1.1 maxv
151 1.1 maxv if (old.addr + old.size <= new.addr)
152 1.1 maxv continue;
153 1.1 maxv if (new.addr + new.size <= old.addr)
154 1.1 maxv continue;
155 1.1 maxv if (__predict_true(!old.write && !new.write))
156 1.1 maxv continue;
157 1.1 maxv if (__predict_true(kcsan_access_is_atomic(&new, &old)))
158 1.1 maxv continue;
159 1.1 maxv
160 1.1 maxv kcsan_report(&new, cpu_number(), &old, i);
161 1.1 maxv break;
162 1.1 maxv }
163 1.1 maxv
164 1.1 maxv if (__predict_false(!kcsan_md_is_avail()))
165 1.1 maxv return;
166 1.1 maxv
167 1.1 maxv kcsan_md_disable_intrs(&intr);
168 1.1 maxv
169 1.1 maxv cpu = &kcsan_cpus[cpu_number()];
170 1.1 maxv if (__predict_false(!cpu->inited))
171 1.1 maxv goto out;
172 1.1 maxv cpu->cnt = (cpu->cnt + 1) % KCSAN_NACCESSES;
173 1.1 maxv if (__predict_true(cpu->cnt != 0))
174 1.1 maxv goto out;
175 1.1 maxv
176 1.1 maxv __builtin_memcpy(&cpu->cell, &new, sizeof(new));
177 1.1 maxv kcsan_md_delay(KCSAN_DELAY);
178 1.1 maxv __builtin_memset(&cpu->cell, 0, sizeof(new));
179 1.1 maxv
180 1.1 maxv out:
181 1.1 maxv kcsan_md_enable_intrs(&intr);
182 1.1 maxv }
183 1.1 maxv
184 1.1 maxv #define CSAN_READ(size) \
185 1.1 maxv void __tsan_read##size(uintptr_t); \
186 1.1 maxv void __tsan_read##size(uintptr_t addr) \
187 1.1 maxv { \
188 1.1 maxv kcsan_access(addr, size, false, false, __RET_ADDR); \
189 1.1 maxv }
190 1.1 maxv
191 1.1 maxv CSAN_READ(1)
192 1.1 maxv CSAN_READ(2)
193 1.1 maxv CSAN_READ(4)
194 1.1 maxv CSAN_READ(8)
195 1.1 maxv CSAN_READ(16)
196 1.1 maxv
197 1.1 maxv #define CSAN_WRITE(size) \
198 1.1 maxv void __tsan_write##size(uintptr_t); \
199 1.1 maxv void __tsan_write##size(uintptr_t addr) \
200 1.1 maxv { \
201 1.1 maxv kcsan_access(addr, size, true, false, __RET_ADDR); \
202 1.1 maxv }
203 1.1 maxv
204 1.1 maxv CSAN_WRITE(1)
205 1.1 maxv CSAN_WRITE(2)
206 1.1 maxv CSAN_WRITE(4)
207 1.1 maxv CSAN_WRITE(8)
208 1.1 maxv CSAN_WRITE(16)
209 1.1 maxv
210 1.1 maxv void __tsan_read_range(uintptr_t, size_t);
211 1.1 maxv void __tsan_write_range(uintptr_t, size_t);
212 1.1 maxv
213 1.1 maxv void
214 1.1 maxv __tsan_read_range(uintptr_t addr, size_t size)
215 1.1 maxv {
216 1.1 maxv kcsan_access(addr, size, false, false, __RET_ADDR);
217 1.1 maxv }
218 1.1 maxv
219 1.1 maxv void
220 1.1 maxv __tsan_write_range(uintptr_t addr, size_t size)
221 1.1 maxv {
222 1.1 maxv kcsan_access(addr, size, true, false, __RET_ADDR);
223 1.1 maxv }
224 1.1 maxv
225 1.1 maxv void __tsan_init(void);
226 1.1 maxv void __tsan_func_entry(void *);
227 1.1 maxv void __tsan_func_exit(void);
228 1.1 maxv
229 1.1 maxv void
230 1.1 maxv __tsan_init(void)
231 1.1 maxv {
232 1.1 maxv }
233 1.1 maxv
234 1.1 maxv void
235 1.1 maxv __tsan_func_entry(void *call_pc)
236 1.1 maxv {
237 1.1 maxv }
238 1.1 maxv
239 1.1 maxv void
240 1.1 maxv __tsan_func_exit(void)
241 1.1 maxv {
242 1.1 maxv }
243 1.1 maxv
244 1.1 maxv /* -------------------------------------------------------------------------- */
245 1.1 maxv
246 1.1 maxv void *
247 1.1 maxv kcsan_memcpy(void *dst, const void *src, size_t len)
248 1.1 maxv {
249 1.1 maxv kcsan_access((uintptr_t)src, len, false, false, __RET_ADDR);
250 1.1 maxv kcsan_access((uintptr_t)dst, len, true, false, __RET_ADDR);
251 1.1 maxv return __builtin_memcpy(dst, src, len);
252 1.1 maxv }
253 1.1 maxv
254 1.1 maxv int
255 1.1 maxv kcsan_memcmp(const void *b1, const void *b2, size_t len)
256 1.1 maxv {
257 1.1 maxv kcsan_access((uintptr_t)b1, len, false, false, __RET_ADDR);
258 1.1 maxv kcsan_access((uintptr_t)b2, len, false, false, __RET_ADDR);
259 1.1 maxv return __builtin_memcmp(b1, b2, len);
260 1.1 maxv }
261 1.1 maxv
262 1.1 maxv void *
263 1.1 maxv kcsan_memset(void *b, int c, size_t len)
264 1.1 maxv {
265 1.1 maxv kcsan_access((uintptr_t)b, len, true, false, __RET_ADDR);
266 1.1 maxv return __builtin_memset(b, c, len);
267 1.1 maxv }
268 1.1 maxv
269 1.1 maxv void *
270 1.1 maxv kcsan_memmove(void *dst, const void *src, size_t len)
271 1.1 maxv {
272 1.1 maxv kcsan_access((uintptr_t)src, len, false, false, __RET_ADDR);
273 1.1 maxv kcsan_access((uintptr_t)dst, len, true, false, __RET_ADDR);
274 1.1 maxv return __builtin_memmove(dst, src, len);
275 1.1 maxv }
276 1.1 maxv
277 1.1 maxv char *
278 1.1 maxv kcsan_strcpy(char *dst, const char *src)
279 1.1 maxv {
280 1.1 maxv char *save = dst;
281 1.1 maxv
282 1.1 maxv while (1) {
283 1.1 maxv kcsan_access((uintptr_t)src, 1, false, false, __RET_ADDR);
284 1.1 maxv kcsan_access((uintptr_t)dst, 1, true, false, __RET_ADDR);
285 1.1 maxv *dst = *src;
286 1.1 maxv if (*src == '\0')
287 1.1 maxv break;
288 1.1 maxv src++, dst++;
289 1.1 maxv }
290 1.1 maxv
291 1.1 maxv return save;
292 1.1 maxv }
293 1.1 maxv
294 1.1 maxv int
295 1.1 maxv kcsan_strcmp(const char *s1, const char *s2)
296 1.1 maxv {
297 1.1 maxv while (1) {
298 1.1 maxv kcsan_access((uintptr_t)s1, 1, false, false, __RET_ADDR);
299 1.1 maxv kcsan_access((uintptr_t)s2, 1, false, false, __RET_ADDR);
300 1.1 maxv if (*s1 != *s2)
301 1.1 maxv break;
302 1.1 maxv if (*s1 == '\0')
303 1.1 maxv return 0;
304 1.1 maxv s1++, s2++;
305 1.1 maxv }
306 1.1 maxv
307 1.1 maxv return (*(const unsigned char *)s1 - *(const unsigned char *)s2);
308 1.1 maxv }
309 1.1 maxv
310 1.1 maxv size_t
311 1.1 maxv kcsan_strlen(const char *str)
312 1.1 maxv {
313 1.1 maxv const char *s;
314 1.1 maxv
315 1.1 maxv s = str;
316 1.1 maxv while (1) {
317 1.1 maxv kcsan_access((uintptr_t)s, 1, false, false, __RET_ADDR);
318 1.1 maxv if (*s == '\0')
319 1.1 maxv break;
320 1.1 maxv s++;
321 1.1 maxv }
322 1.1 maxv
323 1.1 maxv return (s - str);
324 1.1 maxv }
325 1.1 maxv
326 1.1 maxv #undef kcopy
327 1.1 maxv #undef copystr
328 1.1 maxv #undef copyinstr
329 1.1 maxv #undef copyoutstr
330 1.1 maxv #undef copyin
331 1.1 maxv
332 1.1 maxv int kcsan_kcopy(const void *, void *, size_t);
333 1.1 maxv int kcsan_copystr(const void *, void *, size_t, size_t *);
334 1.1 maxv int kcsan_copyinstr(const void *, void *, size_t, size_t *);
335 1.1 maxv int kcsan_copyoutstr(const void *, void *, size_t, size_t *);
336 1.1 maxv int kcsan_copyin(const void *, void *, size_t);
337 1.1 maxv int kcopy(const void *, void *, size_t);
338 1.1 maxv int copystr(const void *, void *, size_t, size_t *);
339 1.1 maxv int copyinstr(const void *, void *, size_t, size_t *);
340 1.1 maxv int copyoutstr(const void *, void *, size_t, size_t *);
341 1.1 maxv int copyin(const void *, void *, size_t);
342 1.1 maxv
343 1.1 maxv int
344 1.1 maxv kcsan_kcopy(const void *src, void *dst, size_t len)
345 1.1 maxv {
346 1.1 maxv kcsan_access((uintptr_t)src, len, false, false, __RET_ADDR);
347 1.1 maxv kcsan_access((uintptr_t)dst, len, true, false, __RET_ADDR);
348 1.1 maxv return kcopy(src, dst, len);
349 1.1 maxv }
350 1.1 maxv
351 1.1 maxv int
352 1.1 maxv kcsan_copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
353 1.1 maxv {
354 1.1 maxv kcsan_access((uintptr_t)kdaddr, len, true, false, __RET_ADDR);
355 1.1 maxv return copystr(kfaddr, kdaddr, len, done);
356 1.1 maxv }
357 1.1 maxv
358 1.1 maxv int
359 1.1 maxv kcsan_copyin(const void *uaddr, void *kaddr, size_t len)
360 1.1 maxv {
361 1.1 maxv kcsan_access((uintptr_t)kaddr, len, true, false, __RET_ADDR);
362 1.1 maxv return copyin(uaddr, kaddr, len);
363 1.1 maxv }
364 1.1 maxv
365 1.1 maxv int
366 1.1 maxv kcsan_copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
367 1.1 maxv {
368 1.1 maxv kcsan_access((uintptr_t)kaddr, len, true, false, __RET_ADDR);
369 1.1 maxv return copyinstr(uaddr, kaddr, len, done);
370 1.1 maxv }
371 1.1 maxv
372 1.1 maxv int
373 1.1 maxv kcsan_copyoutstr(const void *kaddr, void *uaddr, size_t len, size_t *done)
374 1.1 maxv {
375 1.1 maxv kcsan_access((uintptr_t)kaddr, len, false, false, __RET_ADDR);
376 1.1 maxv return copyoutstr(kaddr, uaddr, len, done);
377 1.1 maxv }
378 1.1 maxv
379 1.1 maxv /* -------------------------------------------------------------------------- */
380 1.1 maxv
381 1.1 maxv #undef atomic_add_32
382 1.1 maxv #undef atomic_add_int
383 1.1 maxv #undef atomic_add_long
384 1.1 maxv #undef atomic_add_ptr
385 1.1 maxv #undef atomic_add_64
386 1.1 maxv #undef atomic_add_32_nv
387 1.1 maxv #undef atomic_add_int_nv
388 1.1 maxv #undef atomic_add_long_nv
389 1.1 maxv #undef atomic_add_ptr_nv
390 1.1 maxv #undef atomic_add_64_nv
391 1.1 maxv #undef atomic_and_32
392 1.1 maxv #undef atomic_and_uint
393 1.1 maxv #undef atomic_and_ulong
394 1.1 maxv #undef atomic_and_64
395 1.1 maxv #undef atomic_and_32_nv
396 1.1 maxv #undef atomic_and_uint_nv
397 1.1 maxv #undef atomic_and_ulong_nv
398 1.1 maxv #undef atomic_and_64_nv
399 1.1 maxv #undef atomic_or_32
400 1.1 maxv #undef atomic_or_uint
401 1.1 maxv #undef atomic_or_ulong
402 1.1 maxv #undef atomic_or_64
403 1.1 maxv #undef atomic_or_32_nv
404 1.1 maxv #undef atomic_or_uint_nv
405 1.1 maxv #undef atomic_or_ulong_nv
406 1.1 maxv #undef atomic_or_64_nv
407 1.1 maxv #undef atomic_cas_32
408 1.1 maxv #undef atomic_cas_uint
409 1.1 maxv #undef atomic_cas_ulong
410 1.1 maxv #undef atomic_cas_ptr
411 1.1 maxv #undef atomic_cas_64
412 1.1 maxv #undef atomic_cas_32_ni
413 1.1 maxv #undef atomic_cas_uint_ni
414 1.1 maxv #undef atomic_cas_ulong_ni
415 1.1 maxv #undef atomic_cas_ptr_ni
416 1.1 maxv #undef atomic_cas_64_ni
417 1.1 maxv #undef atomic_swap_32
418 1.1 maxv #undef atomic_swap_uint
419 1.1 maxv #undef atomic_swap_ulong
420 1.1 maxv #undef atomic_swap_ptr
421 1.1 maxv #undef atomic_swap_64
422 1.1 maxv #undef atomic_dec_32
423 1.1 maxv #undef atomic_dec_uint
424 1.1 maxv #undef atomic_dec_ulong
425 1.1 maxv #undef atomic_dec_ptr
426 1.1 maxv #undef atomic_dec_64
427 1.1 maxv #undef atomic_dec_32_nv
428 1.1 maxv #undef atomic_dec_uint_nv
429 1.1 maxv #undef atomic_dec_ulong_nv
430 1.1 maxv #undef atomic_dec_ptr_nv
431 1.1 maxv #undef atomic_dec_64_nv
432 1.1 maxv #undef atomic_inc_32
433 1.1 maxv #undef atomic_inc_uint
434 1.1 maxv #undef atomic_inc_ulong
435 1.1 maxv #undef atomic_inc_ptr
436 1.1 maxv #undef atomic_inc_64
437 1.1 maxv #undef atomic_inc_32_nv
438 1.1 maxv #undef atomic_inc_uint_nv
439 1.1 maxv #undef atomic_inc_ulong_nv
440 1.1 maxv #undef atomic_inc_ptr_nv
441 1.1 maxv #undef atomic_inc_64_nv
442 1.1 maxv
443 1.1 maxv #define CSAN_ATOMIC_FUNC_ADD(name, tret, targ1, targ2) \
444 1.1 maxv void atomic_add_##name(volatile targ1 *, targ2); \
445 1.1 maxv void kcsan_atomic_add_##name(volatile targ1 *, targ2); \
446 1.1 maxv void kcsan_atomic_add_##name(volatile targ1 *ptr, targ2 val) \
447 1.1 maxv { \
448 1.1 maxv kcsan_access((uintptr_t)ptr, sizeof(tret), true, true, \
449 1.1 maxv __RET_ADDR); \
450 1.1 maxv atomic_add_##name(ptr, val); \
451 1.1 maxv } \
452 1.1 maxv tret atomic_add_##name##_nv(volatile targ1 *, targ2); \
453 1.1 maxv tret kcsan_atomic_add_##name##_nv(volatile targ1 *, targ2); \
454 1.1 maxv tret kcsan_atomic_add_##name##_nv(volatile targ1 *ptr, targ2 val) \
455 1.1 maxv { \
456 1.1 maxv kcsan_access((uintptr_t)ptr, sizeof(tret), true, true, \
457 1.1 maxv __RET_ADDR); \
458 1.1 maxv return atomic_add_##name##_nv(ptr, val); \
459 1.1 maxv }
460 1.1 maxv
461 1.1 maxv #define CSAN_ATOMIC_FUNC_AND(name, tret, targ1, targ2) \
462 1.1 maxv void atomic_and_##name(volatile targ1 *, targ2); \
463 1.1 maxv void kcsan_atomic_and_##name(volatile targ1 *, targ2); \
464 1.1 maxv void kcsan_atomic_and_##name(volatile targ1 *ptr, targ2 val) \
465 1.1 maxv { \
466 1.1 maxv kcsan_access((uintptr_t)ptr, sizeof(tret), true, true, \
467 1.1 maxv __RET_ADDR); \
468 1.1 maxv atomic_and_##name(ptr, val); \
469 1.1 maxv } \
470 1.1 maxv tret atomic_and_##name##_nv(volatile targ1 *, targ2); \
471 1.1 maxv tret kcsan_atomic_and_##name##_nv(volatile targ1 *, targ2); \
472 1.1 maxv tret kcsan_atomic_and_##name##_nv(volatile targ1 *ptr, targ2 val) \
473 1.1 maxv { \
474 1.1 maxv kcsan_access((uintptr_t)ptr, sizeof(tret), true, true, \
475 1.1 maxv __RET_ADDR); \
476 1.1 maxv return atomic_and_##name##_nv(ptr, val); \
477 1.1 maxv }
478 1.1 maxv
479 1.1 maxv #define CSAN_ATOMIC_FUNC_OR(name, tret, targ1, targ2) \
480 1.1 maxv void atomic_or_##name(volatile targ1 *, targ2); \
481 1.1 maxv void kcsan_atomic_or_##name(volatile targ1 *, targ2); \
482 1.1 maxv void kcsan_atomic_or_##name(volatile targ1 *ptr, targ2 val) \
483 1.1 maxv { \
484 1.1 maxv kcsan_access((uintptr_t)ptr, sizeof(tret), true, true, \
485 1.1 maxv __RET_ADDR); \
486 1.1 maxv atomic_or_##name(ptr, val); \
487 1.1 maxv } \
488 1.1 maxv tret atomic_or_##name##_nv(volatile targ1 *, targ2); \
489 1.1 maxv tret kcsan_atomic_or_##name##_nv(volatile targ1 *, targ2); \
490 1.1 maxv tret kcsan_atomic_or_##name##_nv(volatile targ1 *ptr, targ2 val) \
491 1.1 maxv { \
492 1.1 maxv kcsan_access((uintptr_t)ptr, sizeof(tret), true, true, \
493 1.1 maxv __RET_ADDR); \
494 1.1 maxv return atomic_or_##name##_nv(ptr, val); \
495 1.1 maxv }
496 1.1 maxv
497 1.1 maxv #define CSAN_ATOMIC_FUNC_CAS(name, tret, targ1, targ2) \
498 1.1 maxv tret atomic_cas_##name(volatile targ1 *, targ2, targ2); \
499 1.1 maxv tret kcsan_atomic_cas_##name(volatile targ1 *, targ2, targ2); \
500 1.1 maxv tret kcsan_atomic_cas_##name(volatile targ1 *ptr, targ2 exp, targ2 new) \
501 1.1 maxv { \
502 1.1 maxv kcsan_access((uintptr_t)ptr, sizeof(tret), true, true, \
503 1.1 maxv __RET_ADDR); \
504 1.1 maxv return atomic_cas_##name(ptr, exp, new); \
505 1.1 maxv } \
506 1.1 maxv tret atomic_cas_##name##_ni(volatile targ1 *, targ2, targ2); \
507 1.1 maxv tret kcsan_atomic_cas_##name##_ni(volatile targ1 *, targ2, targ2); \
508 1.1 maxv tret kcsan_atomic_cas_##name##_ni(volatile targ1 *ptr, targ2 exp, targ2 new) \
509 1.1 maxv { \
510 1.1 maxv kcsan_access((uintptr_t)ptr, sizeof(tret), true, true, \
511 1.1 maxv __RET_ADDR); \
512 1.1 maxv return atomic_cas_##name##_ni(ptr, exp, new); \
513 1.1 maxv }
514 1.1 maxv
515 1.1 maxv #define CSAN_ATOMIC_FUNC_SWAP(name, tret, targ1, targ2) \
516 1.1 maxv tret atomic_swap_##name(volatile targ1 *, targ2); \
517 1.1 maxv tret kcsan_atomic_swap_##name(volatile targ1 *, targ2); \
518 1.1 maxv tret kcsan_atomic_swap_##name(volatile targ1 *ptr, targ2 val) \
519 1.1 maxv { \
520 1.1 maxv kcsan_access((uintptr_t)ptr, sizeof(tret), true, true, \
521 1.1 maxv __RET_ADDR); \
522 1.1 maxv return atomic_swap_##name(ptr, val); \
523 1.1 maxv }
524 1.1 maxv
525 1.1 maxv #define CSAN_ATOMIC_FUNC_DEC(name, tret, targ1) \
526 1.1 maxv void atomic_dec_##name(volatile targ1 *); \
527 1.1 maxv void kcsan_atomic_dec_##name(volatile targ1 *); \
528 1.1 maxv void kcsan_atomic_dec_##name(volatile targ1 *ptr) \
529 1.1 maxv { \
530 1.1 maxv kcsan_access((uintptr_t)ptr, sizeof(tret), true, true, \
531 1.1 maxv __RET_ADDR); \
532 1.1 maxv atomic_dec_##name(ptr); \
533 1.1 maxv } \
534 1.1 maxv tret atomic_dec_##name##_nv(volatile targ1 *); \
535 1.1 maxv tret kcsan_atomic_dec_##name##_nv(volatile targ1 *); \
536 1.1 maxv tret kcsan_atomic_dec_##name##_nv(volatile targ1 *ptr) \
537 1.1 maxv { \
538 1.1 maxv kcsan_access((uintptr_t)ptr, sizeof(tret), true, true, \
539 1.1 maxv __RET_ADDR); \
540 1.1 maxv return atomic_dec_##name##_nv(ptr); \
541 1.1 maxv }
542 1.1 maxv
543 1.1 maxv #define CSAN_ATOMIC_FUNC_INC(name, tret, targ1) \
544 1.1 maxv void atomic_inc_##name(volatile targ1 *); \
545 1.1 maxv void kcsan_atomic_inc_##name(volatile targ1 *); \
546 1.1 maxv void kcsan_atomic_inc_##name(volatile targ1 *ptr) \
547 1.1 maxv { \
548 1.1 maxv kcsan_access((uintptr_t)ptr, sizeof(tret), true, true, \
549 1.1 maxv __RET_ADDR); \
550 1.1 maxv atomic_inc_##name(ptr); \
551 1.1 maxv } \
552 1.1 maxv tret atomic_inc_##name##_nv(volatile targ1 *); \
553 1.1 maxv tret kcsan_atomic_inc_##name##_nv(volatile targ1 *); \
554 1.1 maxv tret kcsan_atomic_inc_##name##_nv(volatile targ1 *ptr) \
555 1.1 maxv { \
556 1.1 maxv kcsan_access((uintptr_t)ptr, sizeof(tret), true, true, \
557 1.1 maxv __RET_ADDR); \
558 1.1 maxv return atomic_inc_##name##_nv(ptr); \
559 1.1 maxv }
560 1.1 maxv
561 1.1 maxv CSAN_ATOMIC_FUNC_ADD(32, uint32_t, uint32_t, int32_t);
562 1.1 maxv CSAN_ATOMIC_FUNC_ADD(64, uint64_t, uint64_t, int64_t);
563 1.1 maxv CSAN_ATOMIC_FUNC_ADD(int, unsigned int, unsigned int, int);
564 1.1 maxv CSAN_ATOMIC_FUNC_ADD(long, unsigned long, unsigned long, long);
565 1.1 maxv CSAN_ATOMIC_FUNC_ADD(ptr, void *, void, ssize_t);
566 1.1 maxv
567 1.1 maxv CSAN_ATOMIC_FUNC_AND(32, uint32_t, uint32_t, uint32_t);
568 1.1 maxv CSAN_ATOMIC_FUNC_AND(64, uint64_t, uint64_t, uint64_t);
569 1.1 maxv CSAN_ATOMIC_FUNC_AND(uint, unsigned int, unsigned int, unsigned int);
570 1.1 maxv CSAN_ATOMIC_FUNC_AND(ulong, unsigned long, unsigned long, unsigned long);
571 1.1 maxv
572 1.1 maxv CSAN_ATOMIC_FUNC_OR(32, uint32_t, uint32_t, uint32_t);
573 1.1 maxv CSAN_ATOMIC_FUNC_OR(64, uint64_t, uint64_t, uint64_t);
574 1.1 maxv CSAN_ATOMIC_FUNC_OR(uint, unsigned int, unsigned int, unsigned int);
575 1.1 maxv CSAN_ATOMIC_FUNC_OR(ulong, unsigned long, unsigned long, unsigned long);
576 1.1 maxv
577 1.1 maxv CSAN_ATOMIC_FUNC_CAS(32, uint32_t, uint32_t, uint32_t);
578 1.1 maxv CSAN_ATOMIC_FUNC_CAS(64, uint64_t, uint64_t, uint64_t);
579 1.1 maxv CSAN_ATOMIC_FUNC_CAS(uint, unsigned int, unsigned int, unsigned int);
580 1.1 maxv CSAN_ATOMIC_FUNC_CAS(ulong, unsigned long, unsigned long, unsigned long);
581 1.1 maxv CSAN_ATOMIC_FUNC_CAS(ptr, void *, void, void *);
582 1.1 maxv
583 1.1 maxv CSAN_ATOMIC_FUNC_SWAP(32, uint32_t, uint32_t, uint32_t);
584 1.1 maxv CSAN_ATOMIC_FUNC_SWAP(64, uint64_t, uint64_t, uint64_t);
585 1.1 maxv CSAN_ATOMIC_FUNC_SWAP(uint, unsigned int, unsigned int, unsigned int);
586 1.1 maxv CSAN_ATOMIC_FUNC_SWAP(ulong, unsigned long, unsigned long, unsigned long);
587 1.1 maxv CSAN_ATOMIC_FUNC_SWAP(ptr, void *, void, void *);
588 1.1 maxv
589 1.1 maxv CSAN_ATOMIC_FUNC_DEC(32, uint32_t, uint32_t)
590 1.1 maxv CSAN_ATOMIC_FUNC_DEC(64, uint64_t, uint64_t)
591 1.1 maxv CSAN_ATOMIC_FUNC_DEC(uint, unsigned int, unsigned int);
592 1.1 maxv CSAN_ATOMIC_FUNC_DEC(ulong, unsigned long, unsigned long);
593 1.1 maxv CSAN_ATOMIC_FUNC_DEC(ptr, void *, void);
594 1.1 maxv
595 1.1 maxv CSAN_ATOMIC_FUNC_INC(32, uint32_t, uint32_t)
596 1.1 maxv CSAN_ATOMIC_FUNC_INC(64, uint64_t, uint64_t)
597 1.1 maxv CSAN_ATOMIC_FUNC_INC(uint, unsigned int, unsigned int);
598 1.1 maxv CSAN_ATOMIC_FUNC_INC(ulong, unsigned long, unsigned long);
599 1.1 maxv CSAN_ATOMIC_FUNC_INC(ptr, void *, void);
600 1.1 maxv
601 1.1 maxv /* -------------------------------------------------------------------------- */
602 1.1 maxv
603 1.1 maxv #include <sys/bus.h>
604 1.1 maxv
605 1.1 maxv #undef bus_space_read_multi_1
606 1.1 maxv #undef bus_space_read_multi_2
607 1.1 maxv #undef bus_space_read_multi_4
608 1.1 maxv #undef bus_space_read_multi_8
609 1.1 maxv #undef bus_space_read_multi_stream_1
610 1.1 maxv #undef bus_space_read_multi_stream_2
611 1.1 maxv #undef bus_space_read_multi_stream_4
612 1.1 maxv #undef bus_space_read_multi_stream_8
613 1.1 maxv #undef bus_space_read_region_1
614 1.1 maxv #undef bus_space_read_region_2
615 1.1 maxv #undef bus_space_read_region_4
616 1.1 maxv #undef bus_space_read_region_8
617 1.1 maxv #undef bus_space_read_region_stream_1
618 1.1 maxv #undef bus_space_read_region_stream_2
619 1.1 maxv #undef bus_space_read_region_stream_4
620 1.1 maxv #undef bus_space_read_region_stream_8
621 1.1 maxv #undef bus_space_write_multi_1
622 1.1 maxv #undef bus_space_write_multi_2
623 1.1 maxv #undef bus_space_write_multi_4
624 1.1 maxv #undef bus_space_write_multi_8
625 1.1 maxv #undef bus_space_write_multi_stream_1
626 1.1 maxv #undef bus_space_write_multi_stream_2
627 1.1 maxv #undef bus_space_write_multi_stream_4
628 1.1 maxv #undef bus_space_write_multi_stream_8
629 1.1 maxv #undef bus_space_write_region_1
630 1.1 maxv #undef bus_space_write_region_2
631 1.1 maxv #undef bus_space_write_region_4
632 1.1 maxv #undef bus_space_write_region_8
633 1.1 maxv #undef bus_space_write_region_stream_1
634 1.1 maxv #undef bus_space_write_region_stream_2
635 1.1 maxv #undef bus_space_write_region_stream_4
636 1.1 maxv #undef bus_space_write_region_stream_8
637 1.1 maxv
638 1.1 maxv #define CSAN_BUS_READ_FUNC(bytes, bits) \
639 1.1 maxv void bus_space_read_multi_##bytes(bus_space_tag_t, bus_space_handle_t, \
640 1.1 maxv bus_size_t, uint##bits##_t *, bus_size_t); \
641 1.1 maxv void kcsan_bus_space_read_multi_##bytes(bus_space_tag_t, \
642 1.1 maxv bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t); \
643 1.1 maxv void kcsan_bus_space_read_multi_##bytes(bus_space_tag_t tag, \
644 1.1 maxv bus_space_handle_t hnd, bus_size_t size, uint##bits##_t *buf, \
645 1.1 maxv bus_size_t count) \
646 1.1 maxv { \
647 1.1 maxv kcsan_access((uintptr_t)buf, sizeof(uint##bits##_t) * count, \
648 1.1 maxv false, false, __RET_ADDR); \
649 1.1 maxv bus_space_read_multi_##bytes(tag, hnd, size, buf, count); \
650 1.1 maxv } \
651 1.1 maxv void bus_space_read_multi_stream_##bytes(bus_space_tag_t, \
652 1.1 maxv bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t); \
653 1.1 maxv void kcsan_bus_space_read_multi_stream_##bytes(bus_space_tag_t, \
654 1.1 maxv bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t); \
655 1.1 maxv void kcsan_bus_space_read_multi_stream_##bytes(bus_space_tag_t tag, \
656 1.1 maxv bus_space_handle_t hnd, bus_size_t size, uint##bits##_t *buf, \
657 1.1 maxv bus_size_t count) \
658 1.1 maxv { \
659 1.1 maxv kcsan_access((uintptr_t)buf, sizeof(uint##bits##_t) * count, \
660 1.1 maxv false, false, __RET_ADDR); \
661 1.1 maxv bus_space_read_multi_stream_##bytes(tag, hnd, size, buf, count);\
662 1.1 maxv } \
663 1.1 maxv void bus_space_read_region_##bytes(bus_space_tag_t, bus_space_handle_t, \
664 1.1 maxv bus_size_t, uint##bits##_t *, bus_size_t); \
665 1.1 maxv void kcsan_bus_space_read_region_##bytes(bus_space_tag_t, \
666 1.1 maxv bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t); \
667 1.1 maxv void kcsan_bus_space_read_region_##bytes(bus_space_tag_t tag, \
668 1.1 maxv bus_space_handle_t hnd, bus_size_t size, uint##bits##_t *buf, \
669 1.1 maxv bus_size_t count) \
670 1.1 maxv { \
671 1.1 maxv kcsan_access((uintptr_t)buf, sizeof(uint##bits##_t) * count, \
672 1.1 maxv false, false, __RET_ADDR); \
673 1.1 maxv bus_space_read_region_##bytes(tag, hnd, size, buf, count); \
674 1.1 maxv } \
675 1.1 maxv void bus_space_read_region_stream_##bytes(bus_space_tag_t, \
676 1.1 maxv bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t); \
677 1.1 maxv void kcsan_bus_space_read_region_stream_##bytes(bus_space_tag_t, \
678 1.1 maxv bus_space_handle_t, bus_size_t, uint##bits##_t *, bus_size_t); \
679 1.1 maxv void kcsan_bus_space_read_region_stream_##bytes(bus_space_tag_t tag, \
680 1.1 maxv bus_space_handle_t hnd, bus_size_t size, uint##bits##_t *buf, \
681 1.1 maxv bus_size_t count) \
682 1.1 maxv { \
683 1.1 maxv kcsan_access((uintptr_t)buf, sizeof(uint##bits##_t) * count, \
684 1.1 maxv false, false, __RET_ADDR); \
685 1.1 maxv bus_space_read_region_stream_##bytes(tag, hnd, size, buf, count);\
686 1.1 maxv }
687 1.1 maxv
688 1.1 maxv #define CSAN_BUS_WRITE_FUNC(bytes, bits) \
689 1.1 maxv void bus_space_write_multi_##bytes(bus_space_tag_t, bus_space_handle_t, \
690 1.1 maxv bus_size_t, const uint##bits##_t *, bus_size_t); \
691 1.1 maxv void kcsan_bus_space_write_multi_##bytes(bus_space_tag_t, \
692 1.1 maxv bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\
693 1.1 maxv void kcsan_bus_space_write_multi_##bytes(bus_space_tag_t tag, \
694 1.1 maxv bus_space_handle_t hnd, bus_size_t size, const uint##bits##_t *buf, \
695 1.1 maxv bus_size_t count) \
696 1.1 maxv { \
697 1.1 maxv kcsan_access((uintptr_t)buf, sizeof(uint##bits##_t) * count, \
698 1.1 maxv true, false, __RET_ADDR); \
699 1.1 maxv bus_space_write_multi_##bytes(tag, hnd, size, buf, count); \
700 1.1 maxv } \
701 1.1 maxv void bus_space_write_multi_stream_##bytes(bus_space_tag_t, \
702 1.1 maxv bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\
703 1.1 maxv void kcsan_bus_space_write_multi_stream_##bytes(bus_space_tag_t, \
704 1.1 maxv bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\
705 1.1 maxv void kcsan_bus_space_write_multi_stream_##bytes(bus_space_tag_t tag, \
706 1.1 maxv bus_space_handle_t hnd, bus_size_t size, const uint##bits##_t *buf, \
707 1.1 maxv bus_size_t count) \
708 1.1 maxv { \
709 1.1 maxv kcsan_access((uintptr_t)buf, sizeof(uint##bits##_t) * count, \
710 1.1 maxv true, false, __RET_ADDR); \
711 1.1 maxv bus_space_write_multi_stream_##bytes(tag, hnd, size, buf, count);\
712 1.1 maxv } \
713 1.1 maxv void bus_space_write_region_##bytes(bus_space_tag_t, bus_space_handle_t,\
714 1.1 maxv bus_size_t, const uint##bits##_t *, bus_size_t); \
715 1.1 maxv void kcsan_bus_space_write_region_##bytes(bus_space_tag_t, \
716 1.1 maxv bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\
717 1.1 maxv void kcsan_bus_space_write_region_##bytes(bus_space_tag_t tag, \
718 1.1 maxv bus_space_handle_t hnd, bus_size_t size, const uint##bits##_t *buf, \
719 1.1 maxv bus_size_t count) \
720 1.1 maxv { \
721 1.1 maxv kcsan_access((uintptr_t)buf, sizeof(uint##bits##_t) * count, \
722 1.1 maxv true, false, __RET_ADDR); \
723 1.1 maxv bus_space_write_region_##bytes(tag, hnd, size, buf, count); \
724 1.1 maxv } \
725 1.1 maxv void bus_space_write_region_stream_##bytes(bus_space_tag_t, \
726 1.1 maxv bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\
727 1.1 maxv void kcsan_bus_space_write_region_stream_##bytes(bus_space_tag_t, \
728 1.1 maxv bus_space_handle_t, bus_size_t, const uint##bits##_t *, bus_size_t);\
729 1.1 maxv void kcsan_bus_space_write_region_stream_##bytes(bus_space_tag_t tag, \
730 1.1 maxv bus_space_handle_t hnd, bus_size_t size, const uint##bits##_t *buf, \
731 1.1 maxv bus_size_t count) \
732 1.1 maxv { \
733 1.1 maxv kcsan_access((uintptr_t)buf, sizeof(uint##bits##_t) * count, \
734 1.1 maxv true, false, __RET_ADDR); \
735 1.1 maxv bus_space_write_region_stream_##bytes(tag, hnd, size, buf, count);\
736 1.1 maxv }
737 1.1 maxv
738 1.1 maxv CSAN_BUS_READ_FUNC(1, 8)
739 1.1 maxv CSAN_BUS_READ_FUNC(2, 16)
740 1.1 maxv CSAN_BUS_READ_FUNC(4, 32)
741 1.1 maxv CSAN_BUS_READ_FUNC(8, 64)
742 1.1 maxv
743 1.1 maxv CSAN_BUS_WRITE_FUNC(1, 8)
744 1.1 maxv CSAN_BUS_WRITE_FUNC(2, 16)
745 1.1 maxv CSAN_BUS_WRITE_FUNC(4, 32)
746 1.1 maxv CSAN_BUS_WRITE_FUNC(8, 64)
747