xref: /openbmc/linux/tools/perf/arch/x86/util/event.c (revision b9df3997)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/types.h>
3 #include <linux/string.h>
4 #include <linux/zalloc.h>
5 
6 #include "../../util/event.h"
7 #include "../../util/synthetic-events.h"
8 #include "../../util/machine.h"
9 #include "../../util/tool.h"
10 #include "../../util/map.h"
11 #include "../../util/debug.h"
12 
13 #if defined(__x86_64__)
14 
15 int perf_event__synthesize_extra_kmaps(struct perf_tool *tool,
16 				       perf_event__handler_t process,
17 				       struct machine *machine)
18 {
19 	int rc = 0;
20 	struct map *pos;
21 	struct map_groups *kmaps = &machine->kmaps;
22 	struct maps *maps = &kmaps->maps;
23 	union perf_event *event = zalloc(sizeof(event->mmap) +
24 					 machine->id_hdr_size);
25 
26 	if (!event) {
27 		pr_debug("Not enough memory synthesizing mmap event "
28 			 "for extra kernel maps\n");
29 		return -1;
30 	}
31 
32 	for (pos = maps__first(maps); pos; pos = map__next(pos)) {
33 		struct kmap *kmap;
34 		size_t size;
35 
36 		if (!__map__is_extra_kernel_map(pos))
37 			continue;
38 
39 		kmap = map__kmap(pos);
40 
41 		size = sizeof(event->mmap) - sizeof(event->mmap.filename) +
42 		       PERF_ALIGN(strlen(kmap->name) + 1, sizeof(u64)) +
43 		       machine->id_hdr_size;
44 
45 		memset(event, 0, size);
46 
47 		event->mmap.header.type = PERF_RECORD_MMAP;
48 
49 		/*
50 		 * kernel uses 0 for user space maps, see kernel/perf_event.c
51 		 * __perf_event_mmap
52 		 */
53 		if (machine__is_host(machine))
54 			event->header.misc = PERF_RECORD_MISC_KERNEL;
55 		else
56 			event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
57 
58 		event->mmap.header.size = size;
59 
60 		event->mmap.start = pos->start;
61 		event->mmap.len   = pos->end - pos->start;
62 		event->mmap.pgoff = pos->pgoff;
63 		event->mmap.pid   = machine->pid;
64 
65 		strlcpy(event->mmap.filename, kmap->name, PATH_MAX);
66 
67 		if (perf_tool__process_synth_event(tool, event, machine,
68 						   process) != 0) {
69 			rc = -1;
70 			break;
71 		}
72 	}
73 
74 	free(event);
75 	return rc;
76 }
77 
78 #endif
79