blob: 690e0e1ab012e9ecc8dead25594aee417812b00f [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
38 if (SUB_OVERFLOW(finfo->buf_end, finfo->buf_start, &fbuf_size))
39 return false;
40
41 if (fbuf_size < MIN_FTRACE_BUF_SIZE) {
42 DMSG("ftrace buffer too small");
43 return false;
44 }
45
46 fbuf = (struct ftrace_buf *)finfo->buf_start;
47 fbuf->head_off = sizeof(struct ftrace_buf);
48 count = snprintk((char *)fbuf + fbuf->head_off, MAX_HEADER_STRLEN,
49 "Function graph for TA: %pUl @ %lx\n",
50 (void *)&elf->uuid, elf->load_addr);
51 assert(count < MAX_HEADER_STRLEN);
52
53 fbuf->ret_func_ptr = finfo->ret_ptr;
54 fbuf->ret_idx = 0;
55 fbuf->lr_idx = 0;
56 fbuf->buf_off = fbuf->head_off + count;
57 fbuf->curr_size = 0;
58 fbuf->max_size = fbuf_size - sizeof(struct ftrace_buf) - count;
59
60 return true;
61}
62
63void ftrace_copy_buf(void *pctx, void (*copy_func)(void *pctx, void *b,
64 size_t bl))
65{
66 if (fbuf) {
67 struct ta_elf *elf = TAILQ_FIRST(&main_elf_queue);
68 size_t dump_size = fbuf->buf_off - fbuf->head_off +
69 fbuf->curr_size;
70
71 assert(elf && elf->is_main);
72 copy_func(pctx, (char *)fbuf + fbuf->head_off, dump_size);
73 }
74}
75
76void ftrace_map_lr(uint64_t *lr)
77{
78 if (fbuf) {
79 if (*lr == fbuf->ret_func_ptr &&
80 fbuf->lr_idx < fbuf->ret_idx) {
81 fbuf->lr_idx++;
82 *lr = fbuf->ret_stack[fbuf->ret_idx - fbuf->lr_idx];
83 }
84 }
85}