blob: 5031b7b22bbd8fec4df5e5f92f25314753c9fe57 [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"
Jiri Olsa84f5d362014-07-14 23:46:48 +020019#include "debug.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020020
Adrian Huntere477f3f2014-10-23 18:16:03 -030021/*
Florian Fainelli01153232018-12-20 19:43:36 -080022 * Include definition of find_map() also used in perf-read-vdso.c for
Adrian Huntere477f3f2014-10-23 18:16:03 -030023 * building perf-read-vdso32 and perf-read-vdsox32.
24 */
Florian Fainelli01153232018-12-20 19:43:36 -080025#include "find-map.c"
Adrian Huntere477f3f2014-10-23 18:16:03 -030026
Adrian Hunter30f4f812014-07-22 16:17:54 +030027#define VDSO__TEMP_FILE_NAME "/tmp/perf-vdso.so-XXXXXX"
28
29struct vdso_file {
30 bool found;
31 bool error;
32 char temp_file_name[sizeof(VDSO__TEMP_FILE_NAME)];
33 const char *dso_name;
Adrian Hunterf6832e172014-10-23 13:45:23 +030034 const char *read_prog;
Adrian Hunter30f4f812014-07-22 16:17:54 +030035};
36
37struct vdso_info {
38 struct vdso_file vdso;
Adrian Hunterf6832e172014-10-23 13:45:23 +030039#if BITS_PER_LONG == 64
40 struct vdso_file vdso32;
41 struct vdso_file vdsox32;
42#endif
Adrian Hunter30f4f812014-07-22 16:17:54 +030043};
44
Adrian Hunterd027b6402014-07-23 14:23:00 +030045static struct vdso_info *vdso_info__new(void)
46{
47 static const struct vdso_info vdso_info_init = {
48 .vdso = {
49 .temp_file_name = VDSO__TEMP_FILE_NAME,
Adrian Hunter51682dc2014-07-22 16:17:57 +030050 .dso_name = DSO__NAME_VDSO,
Adrian Hunterd027b6402014-07-23 14:23:00 +030051 },
Adrian Hunterf6832e172014-10-23 13:45:23 +030052#if BITS_PER_LONG == 64
53 .vdso32 = {
54 .temp_file_name = VDSO__TEMP_FILE_NAME,
55 .dso_name = DSO__NAME_VDSO32,
56 .read_prog = "perf-read-vdso32",
57 },
58 .vdsox32 = {
59 .temp_file_name = VDSO__TEMP_FILE_NAME,
60 .dso_name = DSO__NAME_VDSOX32,
61 .read_prog = "perf-read-vdsox32",
62 },
63#endif
Adrian Hunterd027b6402014-07-23 14:23:00 +030064 };
Adrian Hunter30f4f812014-07-22 16:17:54 +030065
Adrian Hunterd027b6402014-07-23 14:23:00 +030066 return memdup(&vdso_info_init, sizeof(vdso_info_init));
67}
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020068
Adrian Hunter30f4f812014-07-22 16:17:54 +030069static char *get_file(struct vdso_file *vdso_file)
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020070{
71 char *vdso = NULL;
72 char *buf = NULL;
73 void *start, *end;
74 size_t size;
75 int fd;
76
Adrian Hunter30f4f812014-07-22 16:17:54 +030077 if (vdso_file->found)
78 return vdso_file->temp_file_name;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020079
Florian Fainelli01153232018-12-20 19:43:36 -080080 if (vdso_file->error || find_map(&start, &end, VDSO__MAP_NAME))
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020081 return NULL;
82
83 size = end - start;
84
85 buf = memdup(start, size);
86 if (!buf)
87 return NULL;
88
Adrian Hunter30f4f812014-07-22 16:17:54 +030089 fd = mkstemp(vdso_file->temp_file_name);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020090 if (fd < 0)
91 goto out;
92
93 if (size == (size_t) write(fd, buf, size))
Adrian Hunter30f4f812014-07-22 16:17:54 +030094 vdso = vdso_file->temp_file_name;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020095
96 close(fd);
97
98 out:
99 free(buf);
100
Adrian Hunter30f4f812014-07-22 16:17:54 +0300101 vdso_file->found = (vdso != NULL);
102 vdso_file->error = !vdso_file->found;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200103 return vdso;
104}
105
Arnaldo Carvalho de Melo9a4388c2015-05-29 11:54:08 -0300106void machine__exit_vdso(struct machine *machine)
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200107{
Adrian Hunterd027b6402014-07-23 14:23:00 +0300108 struct vdso_info *vdso_info = machine->vdso_info;
109
110 if (!vdso_info)
111 return;
112
Adrian Hunter30f4f812014-07-22 16:17:54 +0300113 if (vdso_info->vdso.found)
114 unlink(vdso_info->vdso.temp_file_name);
Adrian Hunterf6832e172014-10-23 13:45:23 +0300115#if BITS_PER_LONG == 64
116 if (vdso_info->vdso32.found)
117 unlink(vdso_info->vdso32.temp_file_name);
118 if (vdso_info->vdsox32.found)
119 unlink(vdso_info->vdsox32.temp_file_name);
120#endif
Adrian Hunterd027b6402014-07-23 14:23:00 +0300121
122 zfree(&machine->vdso_info);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200123}
124
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300125static struct dso *__machine__addnew_vdso(struct machine *machine, const char *short_name,
126 const char *long_name)
Adrian Hunter4f71f2a2014-07-22 16:17:56 +0300127{
128 struct dso *dso;
129
130 dso = dso__new(short_name);
131 if (dso != NULL) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300132 __dsos__add(&machine->dsos, dso);
Adrian Hunter4f71f2a2014-07-22 16:17:56 +0300133 dso__set_long_name(dso, long_name, false);
134 }
135
136 return dso;
137}
138
Adrian Hunterf6832e172014-10-23 13:45:23 +0300139static enum dso_type machine__thread_dso_type(struct machine *machine,
140 struct thread *thread)
141{
142 enum dso_type dso_type = DSO__TYPE_UNKNOWN;
Arnaldo Carvalho de Melodce04782018-04-25 17:28:55 -0300143 struct map *map = map_groups__first(thread->mg);
Adrian Hunterf6832e172014-10-23 13:45:23 +0300144
Adrian Hunterf6832e172014-10-23 13:45:23 +0300145 for (; map ; map = map_groups__next(map)) {
Arnaldo Carvalho de Melodce04782018-04-25 17:28:55 -0300146 struct dso *dso = map->dso;
Adrian Hunterf6832e172014-10-23 13:45:23 +0300147 if (!dso || dso->long_name[0] != '/')
148 continue;
149 dso_type = dso__type(dso, machine);
150 if (dso_type != DSO__TYPE_UNKNOWN)
151 break;
152 }
153
154 return dso_type;
155}
156
He Kuang76c588f2016-05-17 09:04:54 +0000157#if BITS_PER_LONG == 64
158
Adrian Hunterf6832e172014-10-23 13:45:23 +0300159static int vdso__do_copy_compat(FILE *f, int fd)
160{
161 char buf[4096];
162 size_t count;
163
164 while (1) {
165 count = fread(buf, 1, sizeof(buf), f);
166 if (ferror(f))
167 return -errno;
168 if (feof(f))
169 break;
170 if (count && writen(fd, buf, count) != (ssize_t)count)
171 return -errno;
172 }
173
174 return 0;
175}
176
177static int vdso__copy_compat(const char *prog, int fd)
178{
179 FILE *f;
180 int err;
181
182 f = popen(prog, "r");
183 if (!f)
184 return -errno;
185
186 err = vdso__do_copy_compat(f, fd);
187
188 if (pclose(f) == -1)
189 return -errno;
190
191 return err;
192}
193
194static int vdso__create_compat_file(const char *prog, char *temp_name)
195{
196 int fd, err;
197
198 fd = mkstemp(temp_name);
199 if (fd < 0)
200 return -errno;
201
202 err = vdso__copy_compat(prog, fd);
203
204 if (close(fd) == -1)
205 return -errno;
206
207 return err;
208}
209
210static const char *vdso__get_compat_file(struct vdso_file *vdso_file)
211{
212 int err;
213
214 if (vdso_file->found)
215 return vdso_file->temp_file_name;
216
217 if (vdso_file->error)
218 return NULL;
219
220 err = vdso__create_compat_file(vdso_file->read_prog,
221 vdso_file->temp_file_name);
222 if (err) {
223 pr_err("%s failed, error %d\n", vdso_file->read_prog, err);
224 vdso_file->error = true;
225 return NULL;
226 }
227
228 vdso_file->found = true;
229
230 return vdso_file->temp_file_name;
231}
232
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300233static struct dso *__machine__findnew_compat(struct machine *machine,
234 struct vdso_file *vdso_file)
Adrian Hunterf6832e172014-10-23 13:45:23 +0300235{
236 const char *file_name;
237 struct dso *dso;
238
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300239 dso = __dsos__find(&machine->dsos, vdso_file->dso_name, true);
Adrian Hunterf6832e172014-10-23 13:45:23 +0300240 if (dso)
Adrian Hunter6d545a62015-07-07 14:13:38 +0300241 goto out;
Adrian Hunterf6832e172014-10-23 13:45:23 +0300242
243 file_name = vdso__get_compat_file(vdso_file);
244 if (!file_name)
Adrian Hunter6d545a62015-07-07 14:13:38 +0300245 goto out;
Adrian Hunterf6832e172014-10-23 13:45:23 +0300246
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300247 dso = __machine__addnew_vdso(machine, vdso_file->dso_name, file_name);
Adrian Hunter6d545a62015-07-07 14:13:38 +0300248out:
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300249 return dso;
Adrian Hunterf6832e172014-10-23 13:45:23 +0300250}
251
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300252static int __machine__findnew_vdso_compat(struct machine *machine,
253 struct thread *thread,
254 struct vdso_info *vdso_info,
255 struct dso **dso)
Adrian Hunterf6832e172014-10-23 13:45:23 +0300256{
257 enum dso_type dso_type;
258
259 dso_type = machine__thread_dso_type(machine, thread);
Adrian Hunter46b1fa82014-10-23 13:45:24 +0300260
261#ifndef HAVE_PERF_READ_VDSO32
262 if (dso_type == DSO__TYPE_32BIT)
263 return 0;
264#endif
265#ifndef HAVE_PERF_READ_VDSOX32
266 if (dso_type == DSO__TYPE_X32BIT)
267 return 0;
268#endif
269
Adrian Hunterf6832e172014-10-23 13:45:23 +0300270 switch (dso_type) {
271 case DSO__TYPE_32BIT:
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300272 *dso = __machine__findnew_compat(machine, &vdso_info->vdso32);
Adrian Hunterf6832e172014-10-23 13:45:23 +0300273 return 1;
274 case DSO__TYPE_X32BIT:
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300275 *dso = __machine__findnew_compat(machine, &vdso_info->vdsox32);
Adrian Hunterf6832e172014-10-23 13:45:23 +0300276 return 1;
277 case DSO__TYPE_UNKNOWN:
278 case DSO__TYPE_64BIT:
279 default:
280 return 0;
281 }
282}
283
284#endif
285
He Kuang76c588f2016-05-17 09:04:54 +0000286static struct dso *machine__find_vdso(struct machine *machine,
287 struct thread *thread)
288{
289 struct dso *dso = NULL;
290 enum dso_type dso_type;
291
292 dso_type = machine__thread_dso_type(machine, thread);
293 switch (dso_type) {
294 case DSO__TYPE_32BIT:
295 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO32, true);
296 if (!dso) {
297 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO,
298 true);
299 if (dso && dso_type != dso__type(dso, machine))
300 dso = NULL;
301 }
302 break;
303 case DSO__TYPE_X32BIT:
304 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSOX32, true);
305 break;
306 case DSO__TYPE_64BIT:
307 case DSO__TYPE_UNKNOWN:
308 default:
309 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true);
310 break;
311 }
312
313 return dso;
314}
315
Arnaldo Carvalho de Melo9a4388c2015-05-29 11:54:08 -0300316struct dso *machine__findnew_vdso(struct machine *machine,
He Kuang76c588f2016-05-17 09:04:54 +0000317 struct thread *thread)
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200318{
Adrian Hunterd027b6402014-07-23 14:23:00 +0300319 struct vdso_info *vdso_info;
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300320 struct dso *dso = NULL;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200321
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300322 down_write(&machine->dsos.lock);
Adrian Hunterd027b6402014-07-23 14:23:00 +0300323 if (!machine->vdso_info)
324 machine->vdso_info = vdso_info__new();
325
326 vdso_info = machine->vdso_info;
327 if (!vdso_info)
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300328 goto out_unlock;
Adrian Hunterd027b6402014-07-23 14:23:00 +0300329
He Kuang76c588f2016-05-17 09:04:54 +0000330 dso = machine__find_vdso(machine, thread);
331 if (dso)
332 goto out_unlock;
333
Adrian Hunterf6832e172014-10-23 13:45:23 +0300334#if BITS_PER_LONG == 64
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300335 if (__machine__findnew_vdso_compat(machine, thread, vdso_info, &dso))
336 goto out_unlock;
Adrian Hunterf6832e172014-10-23 13:45:23 +0300337#endif
338
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300339 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200340 if (!dso) {
341 char *file;
342
Adrian Hunter30f4f812014-07-22 16:17:54 +0300343 file = get_file(&vdso_info->vdso);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300344 if (file)
345 dso = __machine__addnew_vdso(machine, DSO__NAME_VDSO, file);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200346 }
347
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300348out_unlock:
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300349 dso__get(dso);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300350 up_write(&machine->dsos.lock);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200351 return dso;
352}
Adrian Hunter51682dc2014-07-22 16:17:57 +0300353
354bool dso__is_vdso(struct dso *dso)
355{
Adrian Hunterf6832e172014-10-23 13:45:23 +0300356 return !strcmp(dso->short_name, DSO__NAME_VDSO) ||
357 !strcmp(dso->short_name, DSO__NAME_VDSO32) ||
358 !strcmp(dso->short_name, DSO__NAME_VDSOX32);
Adrian Hunter51682dc2014-07-22 16:17:57 +0300359}