1 // SPDX-License-Identifier: GPL-2.0
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <assert.h>
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <poll.h>
10 #include <unistd.h>
11 #include <linux/perf_event.h>
12 #include <sys/mman.h>
13 #include "trace_helpers.h"
14 
15 #define DEBUGFS "/sys/kernel/debug/tracing/"
16 
17 #define MAX_SYMS 300000
18 static struct ksym syms[MAX_SYMS];
19 static int sym_cnt;
20 
21 static int ksym_cmp(const void *p1, const void *p2)
22 {
23 	return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr;
24 }
25 
26 int load_kallsyms_refresh(void)
27 {
28 	FILE *f;
29 	char func[256], buf[256];
30 	char symbol;
31 	void *addr;
32 	int i = 0;
33 
34 	sym_cnt = 0;
35 
36 	f = fopen("/proc/kallsyms", "r");
37 	if (!f)
38 		return -ENOENT;
39 
40 	while (fgets(buf, sizeof(buf), f)) {
41 		if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
42 			break;
43 		if (!addr)
44 			continue;
45 		syms[i].addr = (long) addr;
46 		syms[i].name = strdup(func);
47 		i++;
48 	}
49 	fclose(f);
50 	sym_cnt = i;
51 	qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp);
52 	return 0;
53 }
54 
55 int load_kallsyms(void)
56 {
57 	/*
58 	 * This is called/used from multiplace places,
59 	 * load symbols just once.
60 	 */
61 	if (sym_cnt)
62 		return 0;
63 	return load_kallsyms_refresh();
64 }
65 
66 struct ksym *ksym_search(long key)
67 {
68 	int start = 0, end = sym_cnt;
69 	int result;
70 
71 	/* kallsyms not loaded. return NULL */
72 	if (sym_cnt <= 0)
73 		return NULL;
74 
75 	while (start < end) {
76 		size_t mid = start + (end - start) / 2;
77 
78 		result = key - syms[mid].addr;
79 		if (result < 0)
80 			end = mid;
81 		else if (result > 0)
82 			start = mid + 1;
83 		else
84 			return &syms[mid];
85 	}
86 
87 	if (start >= 1 && syms[start - 1].addr < key &&
88 	    key < syms[start].addr)
89 		/* valid ksym */
90 		return &syms[start - 1];
91 
92 	/* out of range. return _stext */
93 	return &syms[0];
94 }
95 
96 long ksym_get_addr(const char *name)
97 {
98 	int i;
99 
100 	for (i = 0; i < sym_cnt; i++) {
101 		if (strcmp(syms[i].name, name) == 0)
102 			return syms[i].addr;
103 	}
104 
105 	return 0;
106 }
107 
108 /* open kallsyms and read symbol addresses on the fly. Without caching all symbols,
109  * this is faster than load + find.
110  */
111 int kallsyms_find(const char *sym, unsigned long long *addr)
112 {
113 	char type, name[500];
114 	unsigned long long value;
115 	int err = 0;
116 	FILE *f;
117 
118 	f = fopen("/proc/kallsyms", "r");
119 	if (!f)
120 		return -EINVAL;
121 
122 	while (fscanf(f, "%llx %c %499s%*[^\n]\n", &value, &type, name) > 0) {
123 		if (strcmp(name, sym) == 0) {
124 			*addr = value;
125 			goto out;
126 		}
127 	}
128 	err = -ENOENT;
129 
130 out:
131 	fclose(f);
132 	return err;
133 }
134 
135 void read_trace_pipe(void)
136 {
137 	int trace_fd;
138 
139 	trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
140 	if (trace_fd < 0)
141 		return;
142 
143 	while (1) {
144 		static char buf[4096];
145 		ssize_t sz;
146 
147 		sz = read(trace_fd, buf, sizeof(buf) - 1);
148 		if (sz > 0) {
149 			buf[sz] = 0;
150 			puts(buf);
151 		}
152 	}
153 }
154 
155 ssize_t get_uprobe_offset(const void *addr)
156 {
157 	size_t start, end, base;
158 	char buf[256];
159 	bool found = false;
160 	FILE *f;
161 
162 	f = fopen("/proc/self/maps", "r");
163 	if (!f)
164 		return -errno;
165 
166 	while (fscanf(f, "%zx-%zx %s %zx %*[^\n]\n", &start, &end, buf, &base) == 4) {
167 		if (buf[2] == 'x' && (uintptr_t)addr >= start && (uintptr_t)addr < end) {
168 			found = true;
169 			break;
170 		}
171 	}
172 
173 	fclose(f);
174 
175 	if (!found)
176 		return -ESRCH;
177 
178 #if defined(__powerpc64__) && defined(_CALL_ELF) && _CALL_ELF == 2
179 
180 #define OP_RT_RA_MASK   0xffff0000UL
181 #define LIS_R2          0x3c400000UL
182 #define ADDIS_R2_R12    0x3c4c0000UL
183 #define ADDI_R2_R2      0x38420000UL
184 
185 	/*
186 	 * A PPC64 ABIv2 function may have a local and a global entry
187 	 * point. We need to use the local entry point when patching
188 	 * functions, so identify and step over the global entry point
189 	 * sequence.
190 	 *
191 	 * The global entry point sequence is always of the form:
192 	 *
193 	 * addis r2,r12,XXXX
194 	 * addi  r2,r2,XXXX
195 	 *
196 	 * A linker optimisation may convert the addis to lis:
197 	 *
198 	 * lis   r2,XXXX
199 	 * addi  r2,r2,XXXX
200 	 */
201 	{
202 		const u32 *insn = (const u32 *)(uintptr_t)addr;
203 
204 		if ((((*insn & OP_RT_RA_MASK) == ADDIS_R2_R12) ||
205 		     ((*insn & OP_RT_RA_MASK) == LIS_R2)) &&
206 		    ((*(insn + 1) & OP_RT_RA_MASK) == ADDI_R2_R2))
207 			return (uintptr_t)(insn + 2) - start + base;
208 	}
209 #endif
210 	return (uintptr_t)addr - start + base;
211 }
212 
213 ssize_t get_rel_offset(uintptr_t addr)
214 {
215 	size_t start, end, offset;
216 	char buf[256];
217 	FILE *f;
218 
219 	f = fopen("/proc/self/maps", "r");
220 	if (!f)
221 		return -errno;
222 
223 	while (fscanf(f, "%zx-%zx %s %zx %*[^\n]\n", &start, &end, buf, &offset) == 4) {
224 		if (addr >= start && addr < end) {
225 			fclose(f);
226 			return (size_t)addr - start + offset;
227 		}
228 	}
229 
230 	fclose(f);
231 	return -EINVAL;
232 }
233