xref: /netbsd-src/sys/arch/amd64/include/csan.h (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: csan.h,v 1.4 2020/09/10 14:04:45 maxv Exp $	*/
2 
3 /*
4  * Copyright (c) 2019-2020 Maxime Villard, m00nbsd.net
5  * All rights reserved.
6  *
7  * This code is part of the KCSAN subsystem of the NetBSD kernel.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/ksyms.h>
32 #include <uvm/uvm.h>
33 #include <amd64/pmap.h>
34 #include <x86/cpufunc.h>
35 
36 static inline bool
37 kcsan_md_unsupported(vaddr_t addr)
38 {
39 	return (addr >= (vaddr_t)PTE_BASE &&
40 	    addr < ((vaddr_t)PTE_BASE + NBPD_L4));
41 }
42 
43 static inline bool
44 kcsan_md_is_avail(void)
45 {
46 	return true;
47 }
48 
49 static inline void
50 kcsan_md_disable_intrs(uint64_t *state)
51 {
52 	*state = x86_read_psl();
53 	x86_disable_intr();
54 }
55 
56 static inline void
57 kcsan_md_enable_intrs(uint64_t *state)
58 {
59 	x86_write_psl(*state);
60 }
61 
62 static inline void
63 kcsan_md_delay(uint64_t us)
64 {
65 	DELAY(us);
66 }
67 
68 static inline bool
69 __md_unwind_end(const char *name)
70 {
71 	if (!strcmp(name, "syscall") ||
72 	    !strcmp(name, "alltraps") ||
73 	    !strcmp(name, "handle_syscall") ||
74 	    !strncmp(name, "Xtrap", 5) ||
75 	    !strncmp(name, "Xintr", 5) ||
76 	    !strncmp(name, "Xhandle", 7) ||
77 	    !strncmp(name, "Xresume", 7) ||
78 	    !strncmp(name, "Xstray", 6) ||
79 	    !strncmp(name, "Xhold", 5) ||
80 	    !strncmp(name, "Xrecurse", 8) ||
81 	    !strcmp(name, "Xdoreti") ||
82 	    !strncmp(name, "Xsoft", 5)) {
83 		return true;
84 	}
85 
86 	return false;
87 }
88 
89 static void
90 kcsan_md_unwind(void)
91 {
92 	uint64_t *rbp, rip;
93 	const char *mod;
94 	const char *sym;
95 	size_t nsym;
96 	int error;
97 
98 	rbp = (uint64_t *)__builtin_frame_address(0);
99 	nsym = 0;
100 
101 	while (1) {
102 		/* 8(%rbp) contains the saved %rip. */
103 		rip = *(rbp + 1);
104 
105 		if (rip < KERNBASE) {
106 			break;
107 		}
108 		error = ksyms_getname(&mod, &sym, (vaddr_t)rip, KSYMS_PROC);
109 		if (error) {
110 			break;
111 		}
112 		printf("#%zu %p in %s <%s>\n", nsym, (void *)rip, sym, mod);
113 		if (__md_unwind_end(sym)) {
114 			break;
115 		}
116 
117 		rbp = (uint64_t *)*(rbp);
118 		if (rbp == 0) {
119 			break;
120 		}
121 		nsym++;
122 
123 		if (nsym >= 15) {
124 			break;
125 		}
126 	}
127 }
128