1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_MACHINE_H
3 #define __PERF_MACHINE_H
4 
5 #include <sys/types.h>
6 #include <linux/rbtree.h>
7 #include "map.h"
8 #include "dso.h"
9 #include "event.h"
10 #include "rwsem.h"
11 
12 struct addr_location;
13 struct branch_stack;
14 struct perf_evsel;
15 struct perf_sample;
16 struct symbol;
17 struct thread;
18 union perf_event;
19 
20 /* Native host kernel uses -1 as pid index in machine */
21 #define	HOST_KERNEL_ID			(-1)
22 #define	DEFAULT_GUEST_KERNEL_ID		(0)
23 
24 extern const char *ref_reloc_sym_names[];
25 
26 struct vdso_info;
27 
28 #define THREADS__TABLE_BITS	8
29 #define THREADS__TABLE_SIZE	(1 << THREADS__TABLE_BITS)
30 
31 struct threads {
32 	struct rb_root	  entries;
33 	struct rw_semaphore lock;
34 	unsigned int	  nr;
35 	struct list_head  dead;
36 	struct thread	  *last_match;
37 };
38 
39 struct machine {
40 	struct rb_node	  rb_node;
41 	pid_t		  pid;
42 	u16		  id_hdr_size;
43 	bool		  comm_exec;
44 	bool		  kptr_restrict_warned;
45 	char		  *root_dir;
46 	char		  *mmap_name;
47 	struct threads    threads[THREADS__TABLE_SIZE];
48 	struct vdso_info  *vdso_info;
49 	struct perf_env   *env;
50 	struct dsos	  dsos;
51 	struct map_groups kmaps;
52 	struct map	  *vmlinux_map;
53 	u64		  kernel_start;
54 	pid_t		  *current_tid;
55 	union { /* Tool specific area */
56 		void	  *priv;
57 		u64	  db_id;
58 	};
59 	bool		  trampolines_mapped;
60 };
61 
machine__threads(struct machine * machine,pid_t tid)62 static inline struct threads *machine__threads(struct machine *machine, pid_t tid)
63 {
64 	/* Cast it to handle tid == -1 */
65 	return &machine->threads[(unsigned int)tid % THREADS__TABLE_SIZE];
66 }
67 
68 /*
69  * The main kernel (vmlinux) map
70  */
71 static inline
machine__kernel_map(struct machine * machine)72 struct map *machine__kernel_map(struct machine *machine)
73 {
74 	return machine->vmlinux_map;
75 }
76 
77 /*
78  * kernel (the one returned by machine__kernel_map()) plus kernel modules maps
79  */
80 static inline
machine__kernel_maps(struct machine * machine)81 struct maps *machine__kernel_maps(struct machine *machine)
82 {
83 	return &machine->kmaps.maps;
84 }
85 
86 int machine__get_kernel_start(struct machine *machine);
87 
machine__kernel_start(struct machine * machine)88 static inline u64 machine__kernel_start(struct machine *machine)
89 {
90 	if (!machine->kernel_start)
91 		machine__get_kernel_start(machine);
92 	return machine->kernel_start;
93 }
94 
machine__kernel_ip(struct machine * machine,u64 ip)95 static inline bool machine__kernel_ip(struct machine *machine, u64 ip)
96 {
97 	u64 kernel_start = machine__kernel_start(machine);
98 
99 	return ip >= kernel_start;
100 }
101 
102 struct thread *machine__find_thread(struct machine *machine, pid_t pid,
103 				    pid_t tid);
104 struct comm *machine__thread_exec_comm(struct machine *machine,
105 				       struct thread *thread);
106 
107 int machine__process_comm_event(struct machine *machine, union perf_event *event,
108 				struct perf_sample *sample);
109 int machine__process_exit_event(struct machine *machine, union perf_event *event,
110 				struct perf_sample *sample);
111 int machine__process_fork_event(struct machine *machine, union perf_event *event,
112 				struct perf_sample *sample);
113 int machine__process_lost_event(struct machine *machine, union perf_event *event,
114 				struct perf_sample *sample);
115 int machine__process_lost_samples_event(struct machine *machine, union perf_event *event,
116 					struct perf_sample *sample);
117 int machine__process_aux_event(struct machine *machine,
118 			       union perf_event *event);
119 int machine__process_itrace_start_event(struct machine *machine,
120 					union perf_event *event);
121 int machine__process_switch_event(struct machine *machine,
122 				  union perf_event *event);
123 int machine__process_namespaces_event(struct machine *machine,
124 				      union perf_event *event,
125 				      struct perf_sample *sample);
126 int machine__process_mmap_event(struct machine *machine, union perf_event *event,
127 				struct perf_sample *sample);
128 int machine__process_mmap2_event(struct machine *machine, union perf_event *event,
129 				 struct perf_sample *sample);
130 int machine__process_event(struct machine *machine, union perf_event *event,
131 				struct perf_sample *sample);
132 
133 typedef void (*machine__process_t)(struct machine *machine, void *data);
134 
135 struct machines {
136 	struct machine host;
137 	struct rb_root guests;
138 };
139 
140 void machines__init(struct machines *machines);
141 void machines__exit(struct machines *machines);
142 
143 void machines__process_guests(struct machines *machines,
144 			      machine__process_t process, void *data);
145 
146 struct machine *machines__add(struct machines *machines, pid_t pid,
147 			      const char *root_dir);
148 struct machine *machines__find_host(struct machines *machines);
149 struct machine *machines__find(struct machines *machines, pid_t pid);
150 struct machine *machines__findnew(struct machines *machines, pid_t pid);
151 
152 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size);
153 void machines__set_comm_exec(struct machines *machines, bool comm_exec);
154 
155 struct machine *machine__new_host(void);
156 struct machine *machine__new_kallsyms(void);
157 int machine__init(struct machine *machine, const char *root_dir, pid_t pid);
158 void machine__exit(struct machine *machine);
159 void machine__delete_threads(struct machine *machine);
160 void machine__delete(struct machine *machine);
161 void machine__remove_thread(struct machine *machine, struct thread *th);
162 
163 struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
164 					   struct addr_location *al);
165 struct mem_info *sample__resolve_mem(struct perf_sample *sample,
166 				     struct addr_location *al);
167 
168 struct callchain_cursor;
169 
170 int thread__resolve_callchain(struct thread *thread,
171 			      struct callchain_cursor *cursor,
172 			      struct perf_evsel *evsel,
173 			      struct perf_sample *sample,
174 			      struct symbol **parent,
175 			      struct addr_location *root_al,
176 			      int max_stack);
177 
178 /*
179  * Default guest kernel is defined by parameter --guestkallsyms
180  * and --guestmodules
181  */
machine__is_default_guest(struct machine * machine)182 static inline bool machine__is_default_guest(struct machine *machine)
183 {
184 	return machine ? machine->pid == DEFAULT_GUEST_KERNEL_ID : false;
185 }
186 
machine__is_host(struct machine * machine)187 static inline bool machine__is_host(struct machine *machine)
188 {
189 	return machine ? machine->pid == HOST_KERNEL_ID : false;
190 }
191 
192 bool machine__is(struct machine *machine, const char *arch);
193 int machine__nr_cpus_avail(struct machine *machine);
194 
195 struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid);
196 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid);
197 
198 struct dso *machine__findnew_dso(struct machine *machine, const char *filename);
199 
200 size_t machine__fprintf(struct machine *machine, FILE *fp);
201 
202 static inline
machine__find_kernel_symbol(struct machine * machine,u64 addr,struct map ** mapp)203 struct symbol *machine__find_kernel_symbol(struct machine *machine, u64 addr,
204 					   struct map **mapp)
205 {
206 	return map_groups__find_symbol(&machine->kmaps, addr, mapp);
207 }
208 
209 static inline
machine__find_kernel_symbol_by_name(struct machine * machine,const char * name,struct map ** mapp)210 struct symbol *machine__find_kernel_symbol_by_name(struct machine *machine,
211 						   const char *name,
212 						   struct map **mapp)
213 {
214 	return map_groups__find_symbol_by_name(&machine->kmaps, name, mapp);
215 }
216 
217 struct map *machine__findnew_module_map(struct machine *machine, u64 start,
218 					const char *filename);
219 int arch__fix_module_text_start(u64 *start, const char *name);
220 
221 int machine__load_kallsyms(struct machine *machine, const char *filename);
222 
223 int machine__load_vmlinux_path(struct machine *machine);
224 
225 size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
226 				     bool (skip)(struct dso *dso, int parm), int parm);
227 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp);
228 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
229 				     bool (skip)(struct dso *dso, int parm), int parm);
230 
231 void machine__destroy_kernel_maps(struct machine *machine);
232 int machine__create_kernel_maps(struct machine *machine);
233 
234 int machines__create_kernel_maps(struct machines *machines, pid_t pid);
235 int machines__create_guest_kernel_maps(struct machines *machines);
236 void machines__destroy_kernel_maps(struct machines *machines);
237 
238 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp);
239 
240 int machine__for_each_thread(struct machine *machine,
241 			     int (*fn)(struct thread *thread, void *p),
242 			     void *priv);
243 int machines__for_each_thread(struct machines *machines,
244 			      int (*fn)(struct thread *thread, void *p),
245 			      void *priv);
246 
247 int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
248 				  struct target *target, struct thread_map *threads,
249 				  perf_event__handler_t process, bool data_mmap,
250 				  unsigned int proc_map_timeout,
251 				  unsigned int nr_threads_synthesize);
252 static inline
machine__synthesize_threads(struct machine * machine,struct target * target,struct thread_map * threads,bool data_mmap,unsigned int proc_map_timeout,unsigned int nr_threads_synthesize)253 int machine__synthesize_threads(struct machine *machine, struct target *target,
254 				struct thread_map *threads, bool data_mmap,
255 				unsigned int proc_map_timeout,
256 				unsigned int nr_threads_synthesize)
257 {
258 	return __machine__synthesize_threads(machine, NULL, target, threads,
259 					     perf_event__process, data_mmap,
260 					     proc_map_timeout,
261 					     nr_threads_synthesize);
262 }
263 
264 pid_t machine__get_current_tid(struct machine *machine, int cpu);
265 int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
266 			     pid_t tid);
267 /*
268  * For use with libtraceevent's tep_set_function_resolver()
269  */
270 char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp);
271 
272 void machine__get_kallsyms_filename(struct machine *machine, char *buf,
273 				    size_t bufsz);
274 
275 int machine__create_extra_kernel_maps(struct machine *machine,
276 				      struct dso *kernel);
277 
278 /* Kernel-space maps for symbols that are outside the main kernel map and module maps */
279 struct extra_kernel_map {
280 	u64 start;
281 	u64 end;
282 	u64 pgoff;
283 	char name[KMAP_NAME_LEN];
284 };
285 
286 int machine__create_extra_kernel_map(struct machine *machine,
287 				     struct dso *kernel,
288 				     struct extra_kernel_map *xm);
289 
290 int machine__map_x86_64_entry_trampolines(struct machine *machine,
291 					  struct dso *kernel);
292 
293 #endif /* __PERF_MACHINE_H */
294