]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
b2e16a85 SG |
2 | /* |
3 | * Copyright (c) 2012 The Chromium OS Authors. | |
b2e16a85 SG |
4 | */ |
5 | ||
6 | #include <common.h> | |
0eb25b61 | 7 | #include <mapmem.h> |
b2e16a85 SG |
8 | #include <trace.h> |
9 | #include <asm/io.h> | |
10 | #include <asm/sections.h> | |
11 | ||
12 | DECLARE_GLOBAL_DATA_PTR; | |
13 | ||
14 | static char trace_enabled __attribute__((section(".data"))); | |
15 | static char trace_inited __attribute__((section(".data"))); | |
16 | ||
17 | /* The header block at the start of the trace memory area */ | |
18 | struct trace_hdr { | |
19 | int func_count; /* Total number of function call sites */ | |
20 | u64 call_count; /* Total number of tracked function calls */ | |
21 | u64 untracked_count; /* Total number of untracked function calls */ | |
22 | int funcs_used; /* Total number of functions used */ | |
23 | ||
24 | /* | |
25 | * Call count for each function. This is indexed by the word offset | |
26 | * of the function from gd->relocaddr | |
27 | */ | |
28 | uintptr_t *call_accum; | |
29 | ||
30 | /* Function trace list */ | |
31 | struct trace_call *ftrace; /* The function call records */ | |
32 | ulong ftrace_size; /* Num. of ftrace records we have space for */ | |
33 | ulong ftrace_count; /* Num. of ftrace records written */ | |
34 | ulong ftrace_too_deep_count; /* Functions that were too deep */ | |
35 | ||
36 | int depth; | |
37 | int depth_limit; | |
38 | int max_depth; | |
39 | }; | |
40 | ||
41 | static struct trace_hdr *hdr; /* Pointer to start of trace buffer */ | |
42 | ||
43 | static inline uintptr_t __attribute__((no_instrument_function)) | |
44 | func_ptr_to_num(void *func_ptr) | |
45 | { | |
46 | uintptr_t offset = (uintptr_t)func_ptr; | |
47 | ||
48 | #ifdef CONFIG_SANDBOX | |
49 | offset -= (uintptr_t)&_init; | |
50 | #else | |
51 | if (gd->flags & GD_FLG_RELOC) | |
52 | offset -= gd->relocaddr; | |
53 | else | |
54 | offset -= CONFIG_SYS_TEXT_BASE; | |
55 | #endif | |
56 | return offset / FUNC_SITE_SIZE; | |
57 | } | |
58 | ||
a2fa38da HS |
59 | #ifdef CONFIG_EFI_LOADER |
60 | ||
61 | /** | |
62 | * trace_gd - the value of the gd register | |
63 | */ | |
64 | static volatile void *trace_gd; | |
65 | ||
66 | /** | |
67 | * trace_save_gd() - save the value of the gd register | |
68 | */ | |
69 | static void __attribute__((no_instrument_function)) trace_save_gd(void) | |
70 | { | |
71 | trace_gd = gd; | |
72 | } | |
73 | ||
74 | /** | |
75 | * trace_swap_gd() - swap between U-Boot and application gd register value | |
76 | * | |
77 | * An UEFI application may change the value of the register that gd lives in. | |
78 | * But some of our functions like get_ticks() access this register. So we | |
79 | * have to set the gd register to the U-Boot value when entering a trace | |
80 | * point and set it back to the application value when exiting the trace point. | |
81 | */ | |
82 | static void __attribute__((no_instrument_function)) trace_swap_gd(void) | |
83 | { | |
84 | volatile void *temp_gd = trace_gd; | |
85 | ||
86 | trace_gd = gd; | |
87 | gd = temp_gd; | |
88 | } | |
89 | ||
90 | #else | |
91 | ||
92 | static void __attribute__((no_instrument_function)) trace_save_gd(void) | |
93 | { | |
94 | } | |
95 | ||
96 | static void __attribute__((no_instrument_function)) trace_swap_gd(void) | |
97 | { | |
98 | } | |
99 | ||
100 | #endif | |
101 | ||
b2e16a85 SG |
102 | static void __attribute__((no_instrument_function)) add_ftrace(void *func_ptr, |
103 | void *caller, ulong flags) | |
104 | { | |
105 | if (hdr->depth > hdr->depth_limit) { | |
106 | hdr->ftrace_too_deep_count++; | |
107 | return; | |
108 | } | |
109 | if (hdr->ftrace_count < hdr->ftrace_size) { | |
110 | struct trace_call *rec = &hdr->ftrace[hdr->ftrace_count]; | |
111 | ||
112 | rec->func = func_ptr_to_num(func_ptr); | |
113 | rec->caller = func_ptr_to_num(caller); | |
114 | rec->flags = flags | (timer_get_us() & FUNCF_TIMESTAMP_MASK); | |
115 | } | |
116 | hdr->ftrace_count++; | |
117 | } | |
118 | ||
119 | static void __attribute__((no_instrument_function)) add_textbase(void) | |
120 | { | |
121 | if (hdr->ftrace_count < hdr->ftrace_size) { | |
122 | struct trace_call *rec = &hdr->ftrace[hdr->ftrace_count]; | |
123 | ||
124 | rec->func = CONFIG_SYS_TEXT_BASE; | |
125 | rec->caller = 0; | |
126 | rec->flags = FUNCF_TEXTBASE; | |
127 | } | |
128 | hdr->ftrace_count++; | |
129 | } | |
130 | ||
131 | /** | |
132 | * This is called on every function entry | |
133 | * | |
134 | * We add to our tally for this function and add to the list of called | |
135 | * functions. | |
136 | * | |
137 | * @param func_ptr Pointer to function being entered | |
138 | * @param caller Pointer to function which called this function | |
139 | */ | |
140 | void __attribute__((no_instrument_function)) __cyg_profile_func_enter( | |
141 | void *func_ptr, void *caller) | |
142 | { | |
143 | if (trace_enabled) { | |
144 | int func; | |
145 | ||
a2fa38da | 146 | trace_swap_gd(); |
b2e16a85 SG |
147 | add_ftrace(func_ptr, caller, FUNCF_ENTRY); |
148 | func = func_ptr_to_num(func_ptr); | |
149 | if (func < hdr->func_count) { | |
150 | hdr->call_accum[func]++; | |
151 | hdr->call_count++; | |
152 | } else { | |
153 | hdr->untracked_count++; | |
154 | } | |
155 | hdr->depth++; | |
156 | if (hdr->depth > hdr->depth_limit) | |
157 | hdr->max_depth = hdr->depth; | |
a2fa38da | 158 | trace_swap_gd(); |
b2e16a85 SG |
159 | } |
160 | } | |
161 | ||
162 | /** | |
163 | * This is called on every function exit | |
164 | * | |
165 | * We do nothing here. | |
166 | * | |
167 | * @param func_ptr Pointer to function being entered | |
168 | * @param caller Pointer to function which called this function | |
169 | */ | |
170 | void __attribute__((no_instrument_function)) __cyg_profile_func_exit( | |
171 | void *func_ptr, void *caller) | |
172 | { | |
173 | if (trace_enabled) { | |
a2fa38da | 174 | trace_swap_gd(); |
b2e16a85 SG |
175 | add_ftrace(func_ptr, caller, FUNCF_EXIT); |
176 | hdr->depth--; | |
a2fa38da | 177 | trace_swap_gd(); |
b2e16a85 SG |
178 | } |
179 | } | |
180 | ||
181 | /** | |
182 | * Produce a list of called functions | |
183 | * | |
184 | * The information is written into the supplied buffer - a header followed | |
185 | * by a list of function records. | |
186 | * | |
187 | * @param buff Buffer to place list into | |
188 | * @param buff_size Size of buffer | |
189 | * @param needed Returns size of buffer needed, which may be | |
190 | * greater than buff_size if we ran out of space. | |
191 | * @return 0 if ok, -1 if space was exhausted | |
192 | */ | |
2b7a3882 | 193 | int trace_list_functions(void *buff, size_t buff_size, size_t *needed) |
b2e16a85 SG |
194 | { |
195 | struct trace_output_hdr *output_hdr = NULL; | |
196 | void *end, *ptr = buff; | |
2b7a3882 HS |
197 | size_t func; |
198 | size_t upto; | |
b2e16a85 SG |
199 | |
200 | end = buff ? buff + buff_size : NULL; | |
201 | ||
202 | /* Place some header information */ | |
203 | if (ptr + sizeof(struct trace_output_hdr) < end) | |
204 | output_hdr = ptr; | |
205 | ptr += sizeof(struct trace_output_hdr); | |
206 | ||
207 | /* Add information about each function */ | |
208 | for (func = upto = 0; func < hdr->func_count; func++) { | |
2b7a3882 | 209 | size_t calls = hdr->call_accum[func]; |
b2e16a85 SG |
210 | |
211 | if (!calls) | |
212 | continue; | |
213 | ||
214 | if (ptr + sizeof(struct trace_output_func) < end) { | |
215 | struct trace_output_func *stats = ptr; | |
216 | ||
217 | stats->offset = func * FUNC_SITE_SIZE; | |
218 | stats->call_count = calls; | |
219 | upto++; | |
220 | } | |
221 | ptr += sizeof(struct trace_output_func); | |
222 | } | |
223 | ||
224 | /* Update the header */ | |
225 | if (output_hdr) { | |
226 | output_hdr->rec_count = upto; | |
227 | output_hdr->type = TRACE_CHUNK_FUNCS; | |
228 | } | |
229 | ||
230 | /* Work out how must of the buffer we used */ | |
231 | *needed = ptr - buff; | |
232 | if (ptr > end) | |
f564d096 SG |
233 | return -ENOSPC; |
234 | ||
b2e16a85 SG |
235 | return 0; |
236 | } | |
237 | ||
2b7a3882 | 238 | int trace_list_calls(void *buff, size_t buff_size, size_t *needed) |
b2e16a85 SG |
239 | { |
240 | struct trace_output_hdr *output_hdr = NULL; | |
241 | void *end, *ptr = buff; | |
2b7a3882 HS |
242 | size_t rec, upto; |
243 | size_t count; | |
b2e16a85 SG |
244 | |
245 | end = buff ? buff + buff_size : NULL; | |
246 | ||
247 | /* Place some header information */ | |
248 | if (ptr + sizeof(struct trace_output_hdr) < end) | |
249 | output_hdr = ptr; | |
250 | ptr += sizeof(struct trace_output_hdr); | |
251 | ||
252 | /* Add information about each call */ | |
253 | count = hdr->ftrace_count; | |
254 | if (count > hdr->ftrace_size) | |
255 | count = hdr->ftrace_size; | |
256 | for (rec = upto = 0; rec < count; rec++) { | |
257 | if (ptr + sizeof(struct trace_call) < end) { | |
258 | struct trace_call *call = &hdr->ftrace[rec]; | |
259 | struct trace_call *out = ptr; | |
260 | ||
261 | out->func = call->func * FUNC_SITE_SIZE; | |
262 | out->caller = call->caller * FUNC_SITE_SIZE; | |
263 | out->flags = call->flags; | |
264 | upto++; | |
265 | } | |
266 | ptr += sizeof(struct trace_call); | |
267 | } | |
268 | ||
269 | /* Update the header */ | |
270 | if (output_hdr) { | |
271 | output_hdr->rec_count = upto; | |
272 | output_hdr->type = TRACE_CHUNK_CALLS; | |
273 | } | |
274 | ||
275 | /* Work out how must of the buffer we used */ | |
276 | *needed = ptr - buff; | |
277 | if (ptr > end) | |
f564d096 SG |
278 | return -ENOSPC; |
279 | ||
b2e16a85 SG |
280 | return 0; |
281 | } | |
282 | ||
283 | /* Print basic information about tracing */ | |
284 | void trace_print_stats(void) | |
285 | { | |
286 | ulong count; | |
287 | ||
288 | #ifndef FTRACE | |
289 | puts("Warning: make U-Boot with FTRACE to enable function instrumenting.\n"); | |
290 | puts("You will likely get zeroed data here\n"); | |
291 | #endif | |
292 | if (!trace_inited) { | |
293 | printf("Trace is disabled\n"); | |
294 | return; | |
295 | } | |
296 | print_grouped_ull(hdr->func_count, 10); | |
297 | puts(" function sites\n"); | |
298 | print_grouped_ull(hdr->call_count, 10); | |
299 | puts(" function calls\n"); | |
300 | print_grouped_ull(hdr->untracked_count, 10); | |
301 | puts(" untracked function calls\n"); | |
302 | count = min(hdr->ftrace_count, hdr->ftrace_size); | |
303 | print_grouped_ull(count, 10); | |
304 | puts(" traced function calls"); | |
305 | if (hdr->ftrace_count > hdr->ftrace_size) { | |
306 | printf(" (%lu dropped due to overflow)", | |
307 | hdr->ftrace_count - hdr->ftrace_size); | |
308 | } | |
309 | puts("\n"); | |
310 | printf("%15d maximum observed call depth\n", hdr->max_depth); | |
311 | printf("%15d call depth limit\n", hdr->depth_limit); | |
312 | print_grouped_ull(hdr->ftrace_too_deep_count, 10); | |
313 | puts(" calls not traced due to depth\n"); | |
314 | } | |
315 | ||
316 | void __attribute__((no_instrument_function)) trace_set_enabled(int enabled) | |
317 | { | |
318 | trace_enabled = enabled != 0; | |
319 | } | |
320 | ||
321 | /** | |
322 | * Init the tracing system ready for used, and enable it | |
323 | * | |
324 | * @param buff Pointer to trace buffer | |
325 | * @param buff_size Size of trace buffer | |
326 | */ | |
327 | int __attribute__((no_instrument_function)) trace_init(void *buff, | |
328 | size_t buff_size) | |
329 | { | |
330 | ulong func_count = gd->mon_len / FUNC_SITE_SIZE; | |
331 | size_t needed; | |
332 | int was_disabled = !trace_enabled; | |
333 | ||
a2fa38da HS |
334 | trace_save_gd(); |
335 | ||
b2e16a85 SG |
336 | if (!was_disabled) { |
337 | #ifdef CONFIG_TRACE_EARLY | |
338 | char *end; | |
339 | ulong used; | |
340 | ||
341 | /* | |
342 | * Copy over the early trace data if we have it. Disable | |
343 | * tracing while we are doing this. | |
344 | */ | |
345 | trace_enabled = 0; | |
346 | hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR, | |
347 | CONFIG_TRACE_EARLY_SIZE); | |
1c6eb075 SG |
348 | end = (char *)&hdr->ftrace[min(hdr->ftrace_count, |
349 | hdr->ftrace_size)]; | |
b2e16a85 SG |
350 | used = end - (char *)hdr; |
351 | printf("trace: copying %08lx bytes of early data from %x to %08lx\n", | |
352 | used, CONFIG_TRACE_EARLY_ADDR, | |
353 | (ulong)map_to_sysmem(buff)); | |
354 | memcpy(buff, hdr, used); | |
355 | #else | |
356 | puts("trace: already enabled\n"); | |
f564d096 | 357 | return -EALREADY; |
b2e16a85 SG |
358 | #endif |
359 | } | |
360 | hdr = (struct trace_hdr *)buff; | |
361 | needed = sizeof(*hdr) + func_count * sizeof(uintptr_t); | |
362 | if (needed > buff_size) { | |
363 | printf("trace: buffer size %zd bytes: at least %zd needed\n", | |
364 | buff_size, needed); | |
f564d096 | 365 | return -ENOSPC; |
b2e16a85 SG |
366 | } |
367 | ||
368 | if (was_disabled) | |
369 | memset(hdr, '\0', needed); | |
370 | hdr->func_count = func_count; | |
371 | hdr->call_accum = (uintptr_t *)(hdr + 1); | |
372 | ||
373 | /* Use any remaining space for the timed function trace */ | |
374 | hdr->ftrace = (struct trace_call *)(buff + needed); | |
375 | hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace); | |
376 | add_textbase(); | |
377 | ||
378 | puts("trace: enabled\n"); | |
da0fb5fd | 379 | hdr->depth_limit = CONFIG_TRACE_CALL_DEPTH_LIMIT; |
b2e16a85 SG |
380 | trace_enabled = 1; |
381 | trace_inited = 1; | |
f564d096 | 382 | |
b2e16a85 SG |
383 | return 0; |
384 | } | |
385 | ||
386 | #ifdef CONFIG_TRACE_EARLY | |
387 | int __attribute__((no_instrument_function)) trace_early_init(void) | |
388 | { | |
389 | ulong func_count = gd->mon_len / FUNC_SITE_SIZE; | |
390 | size_t buff_size = CONFIG_TRACE_EARLY_SIZE; | |
391 | size_t needed; | |
392 | ||
393 | /* We can ignore additional calls to this function */ | |
394 | if (trace_enabled) | |
395 | return 0; | |
396 | ||
397 | hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR, CONFIG_TRACE_EARLY_SIZE); | |
398 | needed = sizeof(*hdr) + func_count * sizeof(uintptr_t); | |
399 | if (needed > buff_size) { | |
400 | printf("trace: buffer size is %zd bytes, at least %zd needed\n", | |
401 | buff_size, needed); | |
f564d096 | 402 | return -ENOSPC; |
b2e16a85 SG |
403 | } |
404 | ||
405 | memset(hdr, '\0', needed); | |
406 | hdr->call_accum = (uintptr_t *)(hdr + 1); | |
407 | hdr->func_count = func_count; | |
408 | ||
409 | /* Use any remaining space for the timed function trace */ | |
410 | hdr->ftrace = (struct trace_call *)((char *)hdr + needed); | |
411 | hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace); | |
412 | add_textbase(); | |
da0fb5fd | 413 | hdr->depth_limit = CONFIG_TRACE_EARLY_CALL_DEPTH_LIMIT; |
b2e16a85 SG |
414 | printf("trace: early enable at %08x\n", CONFIG_TRACE_EARLY_ADDR); |
415 | ||
416 | trace_enabled = 1; | |
f564d096 | 417 | |
b2e16a85 SG |
418 | return 0; |
419 | } | |
420 | #endif |