1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2012 The Chromium OS Authors.
11 #include <asm/sections.h>
13 DECLARE_GLOBAL_DATA_PTR;
15 static char trace_enabled __attribute__((section(".data")));
16 static char trace_inited __attribute__((section(".data")));
18 /* The header block at the start of the trace memory area */
20 int func_count; /* Total number of function call sites */
21 u64 call_count; /* Total number of tracked function calls */
22 u64 untracked_count; /* Total number of untracked function calls */
23 int funcs_used; /* Total number of functions used */
26 * Call count for each function. This is indexed by the word offset
27 * of the function from gd->relocaddr
29 uintptr_t *call_accum;
31 /* Function trace list */
32 struct trace_call *ftrace; /* The function call records */
33 ulong ftrace_size; /* Num. of ftrace records we have space for */
34 ulong ftrace_count; /* Num. of ftrace records written */
35 ulong ftrace_too_deep_count; /* Functions that were too deep */
42 static struct trace_hdr *hdr; /* Pointer to start of trace buffer */
44 static inline uintptr_t __attribute__((no_instrument_function))
45 func_ptr_to_num(void *func_ptr)
47 uintptr_t offset = (uintptr_t)func_ptr;
50 offset -= (uintptr_t)&_init;
52 if (gd->flags & GD_FLG_RELOC)
53 offset -= gd->relocaddr;
55 offset -= CONFIG_SYS_TEXT_BASE;
57 return offset / FUNC_SITE_SIZE;
60 #ifdef CONFIG_EFI_LOADER
63 * trace_gd - the value of the gd register
65 static volatile void *trace_gd;
68 * trace_save_gd() - save the value of the gd register
70 static void __attribute__((no_instrument_function)) trace_save_gd(void)
76 * trace_swap_gd() - swap between U-Boot and application gd register value
78 * An UEFI application may change the value of the register that gd lives in.
79 * But some of our functions like get_ticks() access this register. So we
80 * have to set the gd register to the U-Boot value when entering a trace
81 * point and set it back to the application value when exiting the trace point.
83 static void __attribute__((no_instrument_function)) trace_swap_gd(void)
85 volatile void *temp_gd = trace_gd;
93 static void __attribute__((no_instrument_function)) trace_save_gd(void)
97 static void __attribute__((no_instrument_function)) trace_swap_gd(void)
103 static void __attribute__((no_instrument_function)) add_ftrace(void *func_ptr,
104 void *caller, ulong flags)
106 if (hdr->depth > hdr->depth_limit) {
107 hdr->ftrace_too_deep_count++;
110 if (hdr->ftrace_count < hdr->ftrace_size) {
111 struct trace_call *rec = &hdr->ftrace[hdr->ftrace_count];
113 rec->func = func_ptr_to_num(func_ptr);
114 rec->caller = func_ptr_to_num(caller);
115 rec->flags = flags | (timer_get_us() & FUNCF_TIMESTAMP_MASK);
120 static void __attribute__((no_instrument_function)) add_textbase(void)
122 if (hdr->ftrace_count < hdr->ftrace_size) {
123 struct trace_call *rec = &hdr->ftrace[hdr->ftrace_count];
125 rec->func = CONFIG_SYS_TEXT_BASE;
127 rec->flags = FUNCF_TEXTBASE;
133 * This is called on every function entry
135 * We add to our tally for this function and add to the list of called
138 * @param func_ptr Pointer to function being entered
139 * @param caller Pointer to function which called this function
141 void __attribute__((no_instrument_function)) __cyg_profile_func_enter(
142 void *func_ptr, void *caller)
148 add_ftrace(func_ptr, caller, FUNCF_ENTRY);
149 func = func_ptr_to_num(func_ptr);
150 if (func < hdr->func_count) {
151 hdr->call_accum[func]++;
154 hdr->untracked_count++;
157 if (hdr->depth > hdr->depth_limit)
158 hdr->max_depth = hdr->depth;
164 * This is called on every function exit
166 * We do nothing here.
168 * @param func_ptr Pointer to function being entered
169 * @param caller Pointer to function which called this function
171 void __attribute__((no_instrument_function)) __cyg_profile_func_exit(
172 void *func_ptr, void *caller)
176 add_ftrace(func_ptr, caller, FUNCF_EXIT);
183 * Produce a list of called functions
185 * The information is written into the supplied buffer - a header followed
186 * by a list of function records.
188 * @param buff Buffer to place list into
189 * @param buff_size Size of buffer
190 * @param needed Returns size of buffer needed, which may be
191 * greater than buff_size if we ran out of space.
192 * @return 0 if ok, -1 if space was exhausted
194 int trace_list_functions(void *buff, size_t buff_size, size_t *needed)
196 struct trace_output_hdr *output_hdr = NULL;
197 void *end, *ptr = buff;
201 end = buff ? buff + buff_size : NULL;
203 /* Place some header information */
204 if (ptr + sizeof(struct trace_output_hdr) < end)
206 ptr += sizeof(struct trace_output_hdr);
208 /* Add information about each function */
209 for (func = upto = 0; func < hdr->func_count; func++) {
210 size_t calls = hdr->call_accum[func];
215 if (ptr + sizeof(struct trace_output_func) < end) {
216 struct trace_output_func *stats = ptr;
218 stats->offset = func * FUNC_SITE_SIZE;
219 stats->call_count = calls;
222 ptr += sizeof(struct trace_output_func);
225 /* Update the header */
227 output_hdr->rec_count = upto;
228 output_hdr->type = TRACE_CHUNK_FUNCS;
231 /* Work out how must of the buffer we used */
232 *needed = ptr - buff;
239 int trace_list_calls(void *buff, size_t buff_size, size_t *needed)
241 struct trace_output_hdr *output_hdr = NULL;
242 void *end, *ptr = buff;
246 end = buff ? buff + buff_size : NULL;
248 /* Place some header information */
249 if (ptr + sizeof(struct trace_output_hdr) < end)
251 ptr += sizeof(struct trace_output_hdr);
253 /* Add information about each call */
254 count = hdr->ftrace_count;
255 if (count > hdr->ftrace_size)
256 count = hdr->ftrace_size;
257 for (rec = upto = 0; rec < count; rec++) {
258 if (ptr + sizeof(struct trace_call) < end) {
259 struct trace_call *call = &hdr->ftrace[rec];
260 struct trace_call *out = ptr;
262 out->func = call->func * FUNC_SITE_SIZE;
263 out->caller = call->caller * FUNC_SITE_SIZE;
264 out->flags = call->flags;
267 ptr += sizeof(struct trace_call);
270 /* Update the header */
272 output_hdr->rec_count = upto;
273 output_hdr->type = TRACE_CHUNK_CALLS;
276 /* Work out how must of the buffer we used */
277 *needed = ptr - buff;
284 /* Print basic information about tracing */
285 void trace_print_stats(void)
290 puts("Warning: make U-Boot with FTRACE to enable function instrumenting.\n");
291 puts("You will likely get zeroed data here\n");
294 printf("Trace is disabled\n");
297 print_grouped_ull(hdr->func_count, 10);
298 puts(" function sites\n");
299 print_grouped_ull(hdr->call_count, 10);
300 puts(" function calls\n");
301 print_grouped_ull(hdr->untracked_count, 10);
302 puts(" untracked function calls\n");
303 count = min(hdr->ftrace_count, hdr->ftrace_size);
304 print_grouped_ull(count, 10);
305 puts(" traced function calls");
306 if (hdr->ftrace_count > hdr->ftrace_size) {
307 printf(" (%lu dropped due to overflow)",
308 hdr->ftrace_count - hdr->ftrace_size);
311 printf("%15d maximum observed call depth\n", hdr->max_depth);
312 printf("%15d call depth limit\n", hdr->depth_limit);
313 print_grouped_ull(hdr->ftrace_too_deep_count, 10);
314 puts(" calls not traced due to depth\n");
317 void __attribute__((no_instrument_function)) trace_set_enabled(int enabled)
319 trace_enabled = enabled != 0;
323 * Init the tracing system ready for used, and enable it
325 * @param buff Pointer to trace buffer
326 * @param buff_size Size of trace buffer
328 int __attribute__((no_instrument_function)) trace_init(void *buff,
331 ulong func_count = gd->mon_len / FUNC_SITE_SIZE;
333 int was_disabled = !trace_enabled;
338 #ifdef CONFIG_TRACE_EARLY
343 * Copy over the early trace data if we have it. Disable
344 * tracing while we are doing this.
347 hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR,
348 CONFIG_TRACE_EARLY_SIZE);
349 end = (char *)&hdr->ftrace[min(hdr->ftrace_count,
351 used = end - (char *)hdr;
352 printf("trace: copying %08lx bytes of early data from %x to %08lx\n",
353 used, CONFIG_TRACE_EARLY_ADDR,
354 (ulong)map_to_sysmem(buff));
355 memcpy(buff, hdr, used);
357 puts("trace: already enabled\n");
361 hdr = (struct trace_hdr *)buff;
362 needed = sizeof(*hdr) + func_count * sizeof(uintptr_t);
363 if (needed > buff_size) {
364 printf("trace: buffer size %zd bytes: at least %zd needed\n",
370 memset(hdr, '\0', needed);
371 hdr->func_count = func_count;
372 hdr->call_accum = (uintptr_t *)(hdr + 1);
374 /* Use any remaining space for the timed function trace */
375 hdr->ftrace = (struct trace_call *)(buff + needed);
376 hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
379 puts("trace: enabled\n");
380 hdr->depth_limit = CONFIG_TRACE_CALL_DEPTH_LIMIT;
387 #ifdef CONFIG_TRACE_EARLY
388 int __attribute__((no_instrument_function)) trace_early_init(void)
390 ulong func_count = gd->mon_len / FUNC_SITE_SIZE;
391 size_t buff_size = CONFIG_TRACE_EARLY_SIZE;
394 /* We can ignore additional calls to this function */
398 hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR, CONFIG_TRACE_EARLY_SIZE);
399 needed = sizeof(*hdr) + func_count * sizeof(uintptr_t);
400 if (needed > buff_size) {
401 printf("trace: buffer size is %zd bytes, at least %zd needed\n",
406 memset(hdr, '\0', needed);
407 hdr->call_accum = (uintptr_t *)(hdr + 1);
408 hdr->func_count = func_count;
410 /* Use any remaining space for the timed function trace */
411 hdr->ftrace = (struct trace_call *)((char *)hdr + needed);
412 hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
414 hdr->depth_limit = CONFIG_TRACE_EARLY_CALL_DEPTH_LIMIT;
415 printf("trace: early enable at %08x\n", CONFIG_TRACE_EARLY_ADDR);