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