trace.c 11 KB

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