blob: 83bc75d4372516b6074d77449ed53998d7df6124 [file] [log] [blame]
Jens Wiklanderc86f2182019-06-18 16:45:19 +02001// SPDX-License-Identifier: BSD-2-Clause
2/*
3 * Copyright (c) 2019, Linaro Limited
4 */
5
6#include <assert.h>
7#include <printk.h>
8#include <sys/queue.h>
9#include <types_ext.h>
10#include <util.h>
11#include <user_ta_header.h>
12
13#include "ftrace.h"
14#include "ta_elf.h"
15
16#define MIN_FTRACE_BUF_SIZE 1024
17#define MAX_HEADER_STRLEN 128
18
19static struct __ftrace_info *finfo;
20static struct ftrace_buf *fbuf;
21
22bool ftrace_init(void)
23{
24 struct ta_elf *elf = TAILQ_FIRST(&main_elf_queue);
25 TEE_Result res = TEE_SUCCESS;
26 vaddr_t val = 0;
27 int count = 0;
28 size_t fbuf_size = 0;
29
30 res = ta_elf_resolve_sym("__ftrace_info", &val);
31 if (res)
32 return false;
33
34 finfo = (struct __ftrace_info *)val;
35
36 assert(elf && elf->is_main);
37
Sumit Gargc96d7092019-07-01 17:51:28 +053038 if (SUB_OVERFLOW(finfo->buf_end.ptr64, finfo->buf_start.ptr64,
39 &fbuf_size))
Jens Wiklanderc86f2182019-06-18 16:45:19 +020040 return false;
41
42 if (fbuf_size < MIN_FTRACE_BUF_SIZE) {
43 DMSG("ftrace buffer too small");
44 return false;
45 }
46
Sumit Gargc96d7092019-07-01 17:51:28 +053047 fbuf = (struct ftrace_buf *)finfo->buf_start.ptr64;
Jens Wiklanderc86f2182019-06-18 16:45:19 +020048 fbuf->head_off = sizeof(struct ftrace_buf);
49 count = snprintk((char *)fbuf + fbuf->head_off, MAX_HEADER_STRLEN,
50 "Function graph for TA: %pUl @ %lx\n",
51 (void *)&elf->uuid, elf->load_addr);
52 assert(count < MAX_HEADER_STRLEN);
53
Sumit Gargc96d7092019-07-01 17:51:28 +053054 fbuf->ret_func_ptr = finfo->ret_ptr.ptr64;
Jens Wiklanderc86f2182019-06-18 16:45:19 +020055 fbuf->ret_idx = 0;
56 fbuf->lr_idx = 0;
57 fbuf->buf_off = fbuf->head_off + count;
58 fbuf->curr_size = 0;
59 fbuf->max_size = fbuf_size - sizeof(struct ftrace_buf) - count;
60
61 return true;
62}
63
64void ftrace_copy_buf(void *pctx, void (*copy_func)(void *pctx, void *b,
65 size_t bl))
66{
67 if (fbuf) {
68 struct ta_elf *elf = TAILQ_FIRST(&main_elf_queue);
69 size_t dump_size = fbuf->buf_off - fbuf->head_off +
70 fbuf->curr_size;
71
72 assert(elf && elf->is_main);
73 copy_func(pctx, (char *)fbuf + fbuf->head_off, dump_size);
74 }
75}
76
77void ftrace_map_lr(uint64_t *lr)
78{
79 if (fbuf) {
80 if (*lr == fbuf->ret_func_ptr &&
81 fbuf->lr_idx < fbuf->ret_idx) {
82 fbuf->lr_idx++;
83 *lr = fbuf->ret_stack[fbuf->ret_idx - fbuf->lr_idx];
84 }
85 }
86}