Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
a43783ae | 2 | #include <errno.h> |
7dbf4dcf JO |
3 | #include <unistd.h> |
4 | #include <stdio.h> | |
5 | #include <string.h> | |
6 | #include <sys/types.h> | |
7 | #include <sys/stat.h> | |
8 | #include <fcntl.h> | |
9 | #include <stdlib.h> | |
10 | #include <linux/kernel.h> | |
11 | ||
12 | #include "vdso.h" | |
4a3cec84 | 13 | #include "dso.h" |
fb71c86c | 14 | #include <internal/lib.h> |
1101f69a | 15 | #include "map.h" |
7dbf4dcf | 16 | #include "symbol.h" |
2a03068c | 17 | #include "machine.h" |
f6832e17 | 18 | #include "thread.h" |
7dbf4dcf | 19 | #include "linux/string.h" |
7f7c536f | 20 | #include <linux/zalloc.h> |
84f5d36f | 21 | #include "debug.h" |
7dbf4dcf | 22 | |
e477f3f0 | 23 | /* |
01153237 | 24 | * Include definition of find_map() also used in perf-read-vdso.c for |
e477f3f0 AH |
25 | * building perf-read-vdso32 and perf-read-vdsox32. |
26 | */ | |
01153237 | 27 | #include "find-map.c" |
e477f3f0 | 28 | |
30f4f815 AH |
29 | #define VDSO__TEMP_FILE_NAME "/tmp/perf-vdso.so-XXXXXX" |
30 | ||
31 | struct vdso_file { | |
32 | bool found; | |
33 | bool error; | |
34 | char temp_file_name[sizeof(VDSO__TEMP_FILE_NAME)]; | |
35 | const char *dso_name; | |
f6832e17 | 36 | const char *read_prog; |
30f4f815 AH |
37 | }; |
38 | ||
39 | struct vdso_info { | |
40 | struct vdso_file vdso; | |
f6832e17 AH |
41 | #if BITS_PER_LONG == 64 |
42 | struct vdso_file vdso32; | |
43 | struct vdso_file vdsox32; | |
44 | #endif | |
30f4f815 AH |
45 | }; |
46 | ||
d027b640 AH |
47 | static struct vdso_info *vdso_info__new(void) |
48 | { | |
49 | static const struct vdso_info vdso_info_init = { | |
50 | .vdso = { | |
51 | .temp_file_name = VDSO__TEMP_FILE_NAME, | |
51682dc7 | 52 | .dso_name = DSO__NAME_VDSO, |
d027b640 | 53 | }, |
f6832e17 AH |
54 | #if BITS_PER_LONG == 64 |
55 | .vdso32 = { | |
56 | .temp_file_name = VDSO__TEMP_FILE_NAME, | |
57 | .dso_name = DSO__NAME_VDSO32, | |
58 | .read_prog = "perf-read-vdso32", | |
59 | }, | |
60 | .vdsox32 = { | |
61 | .temp_file_name = VDSO__TEMP_FILE_NAME, | |
62 | .dso_name = DSO__NAME_VDSOX32, | |
63 | .read_prog = "perf-read-vdsox32", | |
64 | }, | |
65 | #endif | |
d027b640 AH |
66 | }; |
67 | ||
68 | return memdup(&vdso_info_init, sizeof(vdso_info_init)); | |
69 | } | |
7dbf4dcf | 70 | |
30f4f815 | 71 | static char *get_file(struct vdso_file *vdso_file) |
7dbf4dcf JO |
72 | { |
73 | char *vdso = NULL; | |
74 | char *buf = NULL; | |
75 | void *start, *end; | |
76 | size_t size; | |
77 | int fd; | |
78 | ||
30f4f815 AH |
79 | if (vdso_file->found) |
80 | return vdso_file->temp_file_name; | |
7dbf4dcf | 81 | |
01153237 | 82 | if (vdso_file->error || find_map(&start, &end, VDSO__MAP_NAME)) |
7dbf4dcf JO |
83 | return NULL; |
84 | ||
85 | size = end - start; | |
86 | ||
87 | buf = memdup(start, size); | |
88 | if (!buf) | |
89 | return NULL; | |
90 | ||
30f4f815 | 91 | fd = mkstemp(vdso_file->temp_file_name); |
7dbf4dcf JO |
92 | if (fd < 0) |
93 | goto out; | |
94 | ||
95 | if (size == (size_t) write(fd, buf, size)) | |
30f4f815 | 96 | vdso = vdso_file->temp_file_name; |
7dbf4dcf JO |
97 | |
98 | close(fd); | |
99 | ||
100 | out: | |
101 | free(buf); | |
102 | ||
30f4f815 AH |
103 | vdso_file->found = (vdso != NULL); |
104 | vdso_file->error = !vdso_file->found; | |
7dbf4dcf JO |
105 | return vdso; |
106 | } | |
107 | ||
9a4388c7 | 108 | void machine__exit_vdso(struct machine *machine) |
7dbf4dcf | 109 | { |
d027b640 AH |
110 | struct vdso_info *vdso_info = machine->vdso_info; |
111 | ||
112 | if (!vdso_info) | |
113 | return; | |
114 | ||
30f4f815 AH |
115 | if (vdso_info->vdso.found) |
116 | unlink(vdso_info->vdso.temp_file_name); | |
f6832e17 AH |
117 | #if BITS_PER_LONG == 64 |
118 | if (vdso_info->vdso32.found) | |
119 | unlink(vdso_info->vdso32.temp_file_name); | |
120 | if (vdso_info->vdsox32.found) | |
121 | unlink(vdso_info->vdsox32.temp_file_name); | |
122 | #endif | |
d027b640 AH |
123 | |
124 | zfree(&machine->vdso_info); | |
7dbf4dcf JO |
125 | } |
126 | ||
e8807844 ACM |
127 | static struct dso *__machine__addnew_vdso(struct machine *machine, const char *short_name, |
128 | const char *long_name) | |
4f71f2a0 AH |
129 | { |
130 | struct dso *dso; | |
131 | ||
132 | dso = dso__new(short_name); | |
133 | if (dso != NULL) { | |
e8807844 | 134 | __dsos__add(&machine->dsos, dso); |
4f71f2a0 | 135 | dso__set_long_name(dso, long_name, false); |
41d58541 NK |
136 | /* Put dso here because __dsos_add already got it */ |
137 | dso__put(dso); | |
4f71f2a0 AH |
138 | } |
139 | ||
140 | return dso; | |
141 | } | |
142 | ||
f6832e17 AH |
143 | static enum dso_type machine__thread_dso_type(struct machine *machine, |
144 | struct thread *thread) | |
145 | { | |
146 | enum dso_type dso_type = DSO__TYPE_UNKNOWN; | |
50481461 | 147 | struct map *map; |
f6832e17 | 148 | |
fe87797d | 149 | maps__for_each_entry(thread->maps, map) { |
dce0478b | 150 | struct dso *dso = map->dso; |
f6832e17 AH |
151 | if (!dso || dso->long_name[0] != '/') |
152 | continue; | |
153 | dso_type = dso__type(dso, machine); | |
154 | if (dso_type != DSO__TYPE_UNKNOWN) | |
155 | break; | |
156 | } | |
157 | ||
158 | return dso_type; | |
159 | } | |
160 | ||
76c588f1 HK |
161 | #if BITS_PER_LONG == 64 |
162 | ||
f6832e17 AH |
163 | static int vdso__do_copy_compat(FILE *f, int fd) |
164 | { | |
165 | char buf[4096]; | |
166 | size_t count; | |
167 | ||
168 | while (1) { | |
169 | count = fread(buf, 1, sizeof(buf), f); | |
170 | if (ferror(f)) | |
171 | return -errno; | |
172 | if (feof(f)) | |
173 | break; | |
174 | if (count && writen(fd, buf, count) != (ssize_t)count) | |
175 | return -errno; | |
176 | } | |
177 | ||
178 | return 0; | |
179 | } | |
180 | ||
181 | static int vdso__copy_compat(const char *prog, int fd) | |
182 | { | |
183 | FILE *f; | |
184 | int err; | |
185 | ||
186 | f = popen(prog, "r"); | |
187 | if (!f) | |
188 | return -errno; | |
189 | ||
190 | err = vdso__do_copy_compat(f, fd); | |
191 | ||
192 | if (pclose(f) == -1) | |
193 | return -errno; | |
194 | ||
195 | return err; | |
196 | } | |
197 | ||
198 | static int vdso__create_compat_file(const char *prog, char *temp_name) | |
199 | { | |
200 | int fd, err; | |
201 | ||
202 | fd = mkstemp(temp_name); | |
203 | if (fd < 0) | |
204 | return -errno; | |
205 | ||
206 | err = vdso__copy_compat(prog, fd); | |
207 | ||
208 | if (close(fd) == -1) | |
209 | return -errno; | |
210 | ||
211 | return err; | |
212 | } | |
213 | ||
214 | static const char *vdso__get_compat_file(struct vdso_file *vdso_file) | |
215 | { | |
216 | int err; | |
217 | ||
218 | if (vdso_file->found) | |
219 | return vdso_file->temp_file_name; | |
220 | ||
221 | if (vdso_file->error) | |
222 | return NULL; | |
223 | ||
224 | err = vdso__create_compat_file(vdso_file->read_prog, | |
225 | vdso_file->temp_file_name); | |
226 | if (err) { | |
227 | pr_err("%s failed, error %d\n", vdso_file->read_prog, err); | |
228 | vdso_file->error = true; | |
229 | return NULL; | |
230 | } | |
231 | ||
232 | vdso_file->found = true; | |
233 | ||
234 | return vdso_file->temp_file_name; | |
235 | } | |
236 | ||
e8807844 ACM |
237 | static struct dso *__machine__findnew_compat(struct machine *machine, |
238 | struct vdso_file *vdso_file) | |
f6832e17 AH |
239 | { |
240 | const char *file_name; | |
241 | struct dso *dso; | |
242 | ||
e8807844 | 243 | dso = __dsos__find(&machine->dsos, vdso_file->dso_name, true); |
f6832e17 | 244 | if (dso) |
6d545a63 | 245 | goto out; |
f6832e17 AH |
246 | |
247 | file_name = vdso__get_compat_file(vdso_file); | |
248 | if (!file_name) | |
6d545a63 | 249 | goto out; |
f6832e17 | 250 | |
e8807844 | 251 | dso = __machine__addnew_vdso(machine, vdso_file->dso_name, file_name); |
6d545a63 | 252 | out: |
e8807844 | 253 | return dso; |
f6832e17 AH |
254 | } |
255 | ||
e8807844 ACM |
256 | static int __machine__findnew_vdso_compat(struct machine *machine, |
257 | struct thread *thread, | |
258 | struct vdso_info *vdso_info, | |
259 | struct dso **dso) | |
f6832e17 AH |
260 | { |
261 | enum dso_type dso_type; | |
262 | ||
263 | dso_type = machine__thread_dso_type(machine, thread); | |
46b1fa85 AH |
264 | |
265 | #ifndef HAVE_PERF_READ_VDSO32 | |
266 | if (dso_type == DSO__TYPE_32BIT) | |
267 | return 0; | |
268 | #endif | |
269 | #ifndef HAVE_PERF_READ_VDSOX32 | |
270 | if (dso_type == DSO__TYPE_X32BIT) | |
271 | return 0; | |
272 | #endif | |
273 | ||
f6832e17 AH |
274 | switch (dso_type) { |
275 | case DSO__TYPE_32BIT: | |
e8807844 | 276 | *dso = __machine__findnew_compat(machine, &vdso_info->vdso32); |
f6832e17 AH |
277 | return 1; |
278 | case DSO__TYPE_X32BIT: | |
e8807844 | 279 | *dso = __machine__findnew_compat(machine, &vdso_info->vdsox32); |
f6832e17 AH |
280 | return 1; |
281 | case DSO__TYPE_UNKNOWN: | |
282 | case DSO__TYPE_64BIT: | |
283 | default: | |
284 | return 0; | |
285 | } | |
286 | } | |
287 | ||
288 | #endif | |
289 | ||
76c588f1 HK |
290 | static struct dso *machine__find_vdso(struct machine *machine, |
291 | struct thread *thread) | |
292 | { | |
293 | struct dso *dso = NULL; | |
294 | enum dso_type dso_type; | |
295 | ||
296 | dso_type = machine__thread_dso_type(machine, thread); | |
297 | switch (dso_type) { | |
298 | case DSO__TYPE_32BIT: | |
299 | dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO32, true); | |
300 | if (!dso) { | |
301 | dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, | |
302 | true); | |
303 | if (dso && dso_type != dso__type(dso, machine)) | |
304 | dso = NULL; | |
305 | } | |
306 | break; | |
307 | case DSO__TYPE_X32BIT: | |
308 | dso = __dsos__find(&machine->dsos, DSO__NAME_VDSOX32, true); | |
309 | break; | |
310 | case DSO__TYPE_64BIT: | |
311 | case DSO__TYPE_UNKNOWN: | |
312 | default: | |
313 | dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true); | |
314 | break; | |
315 | } | |
316 | ||
317 | return dso; | |
318 | } | |
319 | ||
9a4388c7 | 320 | struct dso *machine__findnew_vdso(struct machine *machine, |
76c588f1 | 321 | struct thread *thread) |
7dbf4dcf | 322 | { |
d027b640 | 323 | struct vdso_info *vdso_info; |
e8807844 | 324 | struct dso *dso = NULL; |
d027b640 | 325 | |
0a7c74ea | 326 | down_write(&machine->dsos.lock); |
d027b640 AH |
327 | if (!machine->vdso_info) |
328 | machine->vdso_info = vdso_info__new(); | |
329 | ||
330 | vdso_info = machine->vdso_info; | |
331 | if (!vdso_info) | |
e8807844 | 332 | goto out_unlock; |
7dbf4dcf | 333 | |
76c588f1 HK |
334 | dso = machine__find_vdso(machine, thread); |
335 | if (dso) | |
336 | goto out_unlock; | |
337 | ||
f6832e17 | 338 | #if BITS_PER_LONG == 64 |
e8807844 ACM |
339 | if (__machine__findnew_vdso_compat(machine, thread, vdso_info, &dso)) |
340 | goto out_unlock; | |
f6832e17 AH |
341 | #endif |
342 | ||
e8807844 | 343 | dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true); |
7dbf4dcf JO |
344 | if (!dso) { |
345 | char *file; | |
346 | ||
30f4f815 | 347 | file = get_file(&vdso_info->vdso); |
e8807844 ACM |
348 | if (file) |
349 | dso = __machine__addnew_vdso(machine, DSO__NAME_VDSO, file); | |
7dbf4dcf JO |
350 | } |
351 | ||
e8807844 | 352 | out_unlock: |
d3a7c489 | 353 | dso__get(dso); |
0a7c74ea | 354 | up_write(&machine->dsos.lock); |
7dbf4dcf JO |
355 | return dso; |
356 | } | |
51682dc7 AH |
357 | |
358 | bool dso__is_vdso(struct dso *dso) | |
359 | { | |
f6832e17 AH |
360 | return !strcmp(dso->short_name, DSO__NAME_VDSO) || |
361 | !strcmp(dso->short_name, DSO__NAME_VDSO32) || | |
362 | !strcmp(dso->short_name, DSO__NAME_VDSOX32); | |
51682dc7 | 363 | } |