blob: 7f427bab6c1296812fe4c84ccd8365789485f7f1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03002#include <errno.h>
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +02003#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"
13#include "util.h"
Arnaldo Carvalho de Melo1101f692019-01-27 13:42:37 +010014#include "map.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020015#include "symbol.h"
Adrian Hunter2a030682014-07-22 16:17:53 +030016#include "machine.h"
Adrian Hunterf6832e172014-10-23 13:45:23 +030017#include "thread.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020018#include "linux/string.h"
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030019#include <linux/zalloc.h>
Jiri Olsa84f5d362014-07-14 23:46:48 +020020#include "debug.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020021
Adrian Huntere477f3f2014-10-23 18:16:03 -030022/*
Florian Fainelli01153232018-12-20 19:43:36 -080023 * Include definition of find_map() also used in perf-read-vdso.c for
Adrian Huntere477f3f2014-10-23 18:16:03 -030024 * building perf-read-vdso32 and perf-read-vdsox32.
25 */
Florian Fainelli01153232018-12-20 19:43:36 -080026#include "find-map.c"
Adrian Huntere477f3f2014-10-23 18:16:03 -030027
Adrian Hunter30f4f812014-07-22 16:17:54 +030028#define VDSO__TEMP_FILE_NAME "/tmp/perf-vdso.so-XXXXXX"
29
30struct vdso_file {
31 bool found;
32 bool error;
33 char temp_file_name[sizeof(VDSO__TEMP_FILE_NAME)];
34 const char *dso_name;
Adrian Hunterf6832e172014-10-23 13:45:23 +030035 const char *read_prog;
Adrian Hunter30f4f812014-07-22 16:17:54 +030036};
37
38struct vdso_info {
39 struct vdso_file vdso;
Adrian Hunterf6832e172014-10-23 13:45:23 +030040#if BITS_PER_LONG == 64
41 struct vdso_file vdso32;
42 struct vdso_file vdsox32;
43#endif
Adrian Hunter30f4f812014-07-22 16:17:54 +030044};
45
Adrian Hunterd027b6402014-07-23 14:23:00 +030046static struct vdso_info *vdso_info__new(void)
47{
48 static const struct vdso_info vdso_info_init = {
49 .vdso = {
50 .temp_file_name = VDSO__TEMP_FILE_NAME,
Adrian Hunter51682dc2014-07-22 16:17:57 +030051 .dso_name = DSO__NAME_VDSO,
Adrian Hunterd027b6402014-07-23 14:23:00 +030052 },
Adrian Hunterf6832e172014-10-23 13:45:23 +030053#if BITS_PER_LONG == 64
54 .vdso32 = {
55 .temp_file_name = VDSO__TEMP_FILE_NAME,
56 .dso_name = DSO__NAME_VDSO32,
57 .read_prog = "perf-read-vdso32",
58 },
59 .vdsox32 = {
60 .temp_file_name = VDSO__TEMP_FILE_NAME,
61 .dso_name = DSO__NAME_VDSOX32,
62 .read_prog = "perf-read-vdsox32",
63 },
64#endif
Adrian Hunterd027b6402014-07-23 14:23:00 +030065 };
Adrian Hunter30f4f812014-07-22 16:17:54 +030066
Adrian Hunterd027b6402014-07-23 14:23:00 +030067 return memdup(&vdso_info_init, sizeof(vdso_info_init));
68}
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020069
Adrian Hunter30f4f812014-07-22 16:17:54 +030070static char *get_file(struct vdso_file *vdso_file)
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020071{
72 char *vdso = NULL;
73 char *buf = NULL;
74 void *start, *end;
75 size_t size;
76 int fd;
77
Adrian Hunter30f4f812014-07-22 16:17:54 +030078 if (vdso_file->found)
79 return vdso_file->temp_file_name;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020080
Florian Fainelli01153232018-12-20 19:43:36 -080081 if (vdso_file->error || find_map(&start, &end, VDSO__MAP_NAME))
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020082 return NULL;
83
84 size = end - start;
85
86 buf = memdup(start, size);
87 if (!buf)
88 return NULL;
89
Adrian Hunter30f4f812014-07-22 16:17:54 +030090 fd = mkstemp(vdso_file->temp_file_name);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020091 if (fd < 0)
92 goto out;
93
94 if (size == (size_t) write(fd, buf, size))
Adrian Hunter30f4f812014-07-22 16:17:54 +030095 vdso = vdso_file->temp_file_name;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020096
97 close(fd);
98
99 out:
100 free(buf);
101
Adrian Hunter30f4f812014-07-22 16:17:54 +0300102 vdso_file->found = (vdso != NULL);
103 vdso_file->error = !vdso_file->found;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200104 return vdso;
105}
106
Arnaldo Carvalho de Melo9a4388c2015-05-29 11:54:08 -0300107void machine__exit_vdso(struct machine *machine)
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200108{
Adrian Hunterd027b6402014-07-23 14:23:00 +0300109 struct vdso_info *vdso_info = machine->vdso_info;
110
111 if (!vdso_info)
112 return;
113
Adrian Hunter30f4f812014-07-22 16:17:54 +0300114 if (vdso_info->vdso.found)
115 unlink(vdso_info->vdso.temp_file_name);
Adrian Hunterf6832e172014-10-23 13:45:23 +0300116#if BITS_PER_LONG == 64
117 if (vdso_info->vdso32.found)
118 unlink(vdso_info->vdso32.temp_file_name);
119 if (vdso_info->vdsox32.found)
120 unlink(vdso_info->vdsox32.temp_file_name);
121#endif
Adrian Hunterd027b6402014-07-23 14:23:00 +0300122
123 zfree(&machine->vdso_info);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200124}
125
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300126static struct dso *__machine__addnew_vdso(struct machine *machine, const char *short_name,
127 const char *long_name)
Adrian Hunter4f71f2a2014-07-22 16:17:56 +0300128{
129 struct dso *dso;
130
131 dso = dso__new(short_name);
132 if (dso != NULL) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300133 __dsos__add(&machine->dsos, dso);
Adrian Hunter4f71f2a2014-07-22 16:17:56 +0300134 dso__set_long_name(dso, long_name, false);
135 }
136
137 return dso;
138}
139
Adrian Hunterf6832e172014-10-23 13:45:23 +0300140static enum dso_type machine__thread_dso_type(struct machine *machine,
141 struct thread *thread)
142{
143 enum dso_type dso_type = DSO__TYPE_UNKNOWN;
Arnaldo Carvalho de Melodce04782018-04-25 17:28:55 -0300144 struct map *map = map_groups__first(thread->mg);
Adrian Hunterf6832e172014-10-23 13:45:23 +0300145
Adrian Hunterf6832e172014-10-23 13:45:23 +0300146 for (; map ; map = map_groups__next(map)) {
Arnaldo Carvalho de Melodce04782018-04-25 17:28:55 -0300147 struct dso *dso = map->dso;
Adrian Hunterf6832e172014-10-23 13:45:23 +0300148 if (!dso || dso->long_name[0] != '/')
149 continue;
150 dso_type = dso__type(dso, machine);
151 if (dso_type != DSO__TYPE_UNKNOWN)
152 break;
153 }
154
155 return dso_type;
156}
157
He Kuang76c588f2016-05-17 09:04:54 +0000158#if BITS_PER_LONG == 64
159
Adrian Hunterf6832e172014-10-23 13:45:23 +0300160static int vdso__do_copy_compat(FILE *f, int fd)
161{
162 char buf[4096];
163 size_t count;
164
165 while (1) {
166 count = fread(buf, 1, sizeof(buf), f);
167 if (ferror(f))
168 return -errno;
169 if (feof(f))
170 break;
171 if (count && writen(fd, buf, count) != (ssize_t)count)
172 return -errno;
173 }
174
175 return 0;
176}
177
178static int vdso__copy_compat(const char *prog, int fd)
179{
180 FILE *f;
181 int err;
182
183 f = popen(prog, "r");
184 if (!f)
185 return -errno;
186
187 err = vdso__do_copy_compat(f, fd);
188
189 if (pclose(f) == -1)
190 return -errno;
191
192 return err;
193}
194
195static int vdso__create_compat_file(const char *prog, char *temp_name)
196{
197 int fd, err;
198
199 fd = mkstemp(temp_name);
200 if (fd < 0)
201 return -errno;
202
203 err = vdso__copy_compat(prog, fd);
204
205 if (close(fd) == -1)
206 return -errno;
207
208 return err;
209}
210
211static const char *vdso__get_compat_file(struct vdso_file *vdso_file)
212{
213 int err;
214
215 if (vdso_file->found)
216 return vdso_file->temp_file_name;
217
218 if (vdso_file->error)
219 return NULL;
220
221 err = vdso__create_compat_file(vdso_file->read_prog,
222 vdso_file->temp_file_name);
223 if (err) {
224 pr_err("%s failed, error %d\n", vdso_file->read_prog, err);
225 vdso_file->error = true;
226 return NULL;
227 }
228
229 vdso_file->found = true;
230
231 return vdso_file->temp_file_name;
232}
233
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300234static struct dso *__machine__findnew_compat(struct machine *machine,
235 struct vdso_file *vdso_file)
Adrian Hunterf6832e172014-10-23 13:45:23 +0300236{
237 const char *file_name;
238 struct dso *dso;
239
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300240 dso = __dsos__find(&machine->dsos, vdso_file->dso_name, true);
Adrian Hunterf6832e172014-10-23 13:45:23 +0300241 if (dso)
Adrian Hunter6d545a62015-07-07 14:13:38 +0300242 goto out;
Adrian Hunterf6832e172014-10-23 13:45:23 +0300243
244 file_name = vdso__get_compat_file(vdso_file);
245 if (!file_name)
Adrian Hunter6d545a62015-07-07 14:13:38 +0300246 goto out;
Adrian Hunterf6832e172014-10-23 13:45:23 +0300247
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300248 dso = __machine__addnew_vdso(machine, vdso_file->dso_name, file_name);
Adrian Hunter6d545a62015-07-07 14:13:38 +0300249out:
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300250 return dso;
Adrian Hunterf6832e172014-10-23 13:45:23 +0300251}
252
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300253static int __machine__findnew_vdso_compat(struct machine *machine,
254 struct thread *thread,
255 struct vdso_info *vdso_info,
256 struct dso **dso)
Adrian Hunterf6832e172014-10-23 13:45:23 +0300257{
258 enum dso_type dso_type;
259
260 dso_type = machine__thread_dso_type(machine, thread);
Adrian Hunter46b1fa82014-10-23 13:45:24 +0300261
262#ifndef HAVE_PERF_READ_VDSO32
263 if (dso_type == DSO__TYPE_32BIT)
264 return 0;
265#endif
266#ifndef HAVE_PERF_READ_VDSOX32
267 if (dso_type == DSO__TYPE_X32BIT)
268 return 0;
269#endif
270
Adrian Hunterf6832e172014-10-23 13:45:23 +0300271 switch (dso_type) {
272 case DSO__TYPE_32BIT:
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300273 *dso = __machine__findnew_compat(machine, &vdso_info->vdso32);
Adrian Hunterf6832e172014-10-23 13:45:23 +0300274 return 1;
275 case DSO__TYPE_X32BIT:
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300276 *dso = __machine__findnew_compat(machine, &vdso_info->vdsox32);
Adrian Hunterf6832e172014-10-23 13:45:23 +0300277 return 1;
278 case DSO__TYPE_UNKNOWN:
279 case DSO__TYPE_64BIT:
280 default:
281 return 0;
282 }
283}
284
285#endif
286
He Kuang76c588f2016-05-17 09:04:54 +0000287static struct dso *machine__find_vdso(struct machine *machine,
288 struct thread *thread)
289{
290 struct dso *dso = NULL;
291 enum dso_type dso_type;
292
293 dso_type = machine__thread_dso_type(machine, thread);
294 switch (dso_type) {
295 case DSO__TYPE_32BIT:
296 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO32, true);
297 if (!dso) {
298 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO,
299 true);
300 if (dso && dso_type != dso__type(dso, machine))
301 dso = NULL;
302 }
303 break;
304 case DSO__TYPE_X32BIT:
305 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSOX32, true);
306 break;
307 case DSO__TYPE_64BIT:
308 case DSO__TYPE_UNKNOWN:
309 default:
310 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true);
311 break;
312 }
313
314 return dso;
315}
316
Arnaldo Carvalho de Melo9a4388c2015-05-29 11:54:08 -0300317struct dso *machine__findnew_vdso(struct machine *machine,
He Kuang76c588f2016-05-17 09:04:54 +0000318 struct thread *thread)
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200319{
Adrian Hunterd027b6402014-07-23 14:23:00 +0300320 struct vdso_info *vdso_info;
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300321 struct dso *dso = NULL;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200322
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300323 down_write(&machine->dsos.lock);
Adrian Hunterd027b6402014-07-23 14:23:00 +0300324 if (!machine->vdso_info)
325 machine->vdso_info = vdso_info__new();
326
327 vdso_info = machine->vdso_info;
328 if (!vdso_info)
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300329 goto out_unlock;
Adrian Hunterd027b6402014-07-23 14:23:00 +0300330
He Kuang76c588f2016-05-17 09:04:54 +0000331 dso = machine__find_vdso(machine, thread);
332 if (dso)
333 goto out_unlock;
334
Adrian Hunterf6832e172014-10-23 13:45:23 +0300335#if BITS_PER_LONG == 64
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300336 if (__machine__findnew_vdso_compat(machine, thread, vdso_info, &dso))
337 goto out_unlock;
Adrian Hunterf6832e172014-10-23 13:45:23 +0300338#endif
339
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300340 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200341 if (!dso) {
342 char *file;
343
Adrian Hunter30f4f812014-07-22 16:17:54 +0300344 file = get_file(&vdso_info->vdso);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300345 if (file)
346 dso = __machine__addnew_vdso(machine, DSO__NAME_VDSO, file);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200347 }
348
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300349out_unlock:
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300350 dso__get(dso);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300351 up_write(&machine->dsos.lock);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200352 return dso;
353}
Adrian Hunter51682dc2014-07-22 16:17:57 +0300354
355bool dso__is_vdso(struct dso *dso)
356{
Adrian Hunterf6832e172014-10-23 13:45:23 +0300357 return !strcmp(dso->short_name, DSO__NAME_VDSO) ||
358 !strcmp(dso->short_name, DSO__NAME_VDSO32) ||
359 !strcmp(dso->short_name, DSO__NAME_VDSOX32);
Adrian Hunter51682dc2014-07-22 16:17:57 +0300360}