1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  */
5 #include <compiler.h>
6 #include <malloc.h>
7 #include <tee_ta_api.h>
8 #include <tee_internal_api_extensions.h>
9 #include <trace.h>
10 #include <user_ta_header.h>
11 #include <user_ta_header_defines.h>
12 #include <utee_syscalls.h>
13 
14 int trace_level = TRACE_LEVEL;
15 
16 const char trace_ext_prefix[]  = "TA";
17 
18 #ifndef TA_VERSION
19 #define TA_VERSION "Undefined version"
20 #endif
21 
22 #ifndef TA_DESCRIPTION
23 #define TA_DESCRIPTION "Undefined description"
24 #endif
25 
26 /* exprted to user_ta_header.c, built within TA */
27 struct utee_params;
28 
29 #ifdef ARM32
30 #define _C_FUNCTION(name) name##_c
31 #else
32 #define _C_FUNCTION(name) name
33 #endif /* ARM32 */
34 
35 /* From libutee */
36 TEE_Result __utee_entry(unsigned long func, unsigned long session_id,
37 			struct utee_params *up, unsigned long cmd_id);
38 
39 void __noreturn _C_FUNCTION(__ta_entry)(unsigned long func,
40 					unsigned long session_id,
41 					struct utee_params *up,
42 					unsigned long cmd_id);
43 
_C_FUNCTION(__ta_entry)44 void __noreturn _C_FUNCTION(__ta_entry)(unsigned long func,
45 					unsigned long session_id,
46 					struct utee_params *up,
47 					unsigned long cmd_id)
48 {
49 	TEE_Result res = __utee_entry(func, session_id, up, cmd_id);
50 
51 #if defined(CFG_FTRACE_SUPPORT)
52 	/*
53 	 * __ta_entry is the first TA API called from TEE core. As it being
54 	 * __noreturn API, we need to call ftrace_return in this API just
55 	 * before _utee_return syscall to get proper ftrace call graph.
56 	 */
57 	ftrace_return();
58 #endif
59 
60 	_utee_return(res);
61 }
62 
63 /*
64  * According to GP Internal API, TA_STACK_SIZE corresponds to the stack
65  * size used by the TA code itself and does not include stack space
66  * possibly used by the Trusted Core Framework.
67  * Hence, stack_size which is the size of the stack to use,
68  * must be enlarged
69  * It has been set to 2048 to include trace framework and invoke commands
70  */
71 #define TA_FRAMEWORK_STACK_SIZE 2048
72 
73 const struct ta_head ta_head __section(".ta_head") = {
74 	/* UUID, unique to each TA */
75 	.uuid = TA_UUID,
76 	/*
77 	 * According to GP Internal API, TA_FRAMEWORK_STACK_SIZE corresponds to
78 	 * the stack size used by the TA code itself and does not include stack
79 	 * space possibly used by the Trusted Core Framework.
80 	 * Hence, stack_size which is the size of the stack to use,
81 	 * must be enlarged
82 	 */
83 	.stack_size = TA_STACK_SIZE + TA_FRAMEWORK_STACK_SIZE,
84 	.flags = TA_FLAGS,
85 	/*
86 	 * The TA entry doesn't go via this field any longer, to be able to
87 	 * reliably check that an old TA isn't loaded set this field to a
88 	 * fixed value.
89 	 */
90 	.depr_entry = UINT64_MAX,
91 };
92 
93 /* Keeping the heap in bss */
94 #if TA_DATA_SIZE < MALLOC_INITIAL_POOL_MIN_SIZE
95 #error TA_DATA_SIZE too small
96 #endif
97 
98 uint8_t ta_heap[TA_DATA_SIZE];
99 const size_t ta_heap_size = sizeof(ta_heap);
100 
101 const struct user_ta_property ta_props[] = {
102 	{TA_PROP_STR_SINGLE_INSTANCE, USER_TA_PROP_TYPE_BOOL,
103 	 &(const bool){(TA_FLAGS & TA_FLAG_SINGLE_INSTANCE) != 0}},
104 
105 	{TA_PROP_STR_MULTI_SESSION, USER_TA_PROP_TYPE_BOOL,
106 	 &(const bool){(TA_FLAGS & TA_FLAG_MULTI_SESSION) != 0}},
107 
108 	{TA_PROP_STR_KEEP_ALIVE, USER_TA_PROP_TYPE_BOOL,
109 	 &(const bool){(TA_FLAGS & TA_FLAG_INSTANCE_KEEP_ALIVE) != 0}},
110 
111 	{TA_PROP_STR_DATA_SIZE, USER_TA_PROP_TYPE_U32,
112 	 &(const uint32_t){TA_DATA_SIZE}},
113 
114 	{TA_PROP_STR_STACK_SIZE, USER_TA_PROP_TYPE_U32,
115 	 &(const uint32_t){TA_STACK_SIZE}},
116 
117 	{TA_PROP_STR_VERSION, USER_TA_PROP_TYPE_STRING,
118 	 TA_VERSION},
119 
120 	{TA_PROP_STR_DESCRIPTION, USER_TA_PROP_TYPE_STRING,
121 	 TA_DESCRIPTION},
122 
123 /*
124  * Extended propietary properties, name of properties must not begin with
125  * "gpd."
126  */
127 #ifdef TA_CURRENT_TA_EXT_PROPERTIES
128 	TA_CURRENT_TA_EXT_PROPERTIES
129 #endif
130 };
131 
132 const size_t ta_num_props = sizeof(ta_props) / sizeof(ta_props[0]);
133 
134 #ifdef CFG_FTRACE_SUPPORT
135 struct __ftrace_info __ftrace_info = {
136 #ifdef __ILP32__
137 	.buf_start.ptr32 = { .lo = (uint32_t)&__ftrace_buf_start },
138 	.buf_end.ptr32 = { .lo = (uint32_t)__ftrace_buf_end },
139 	.ret_ptr.ptr32 = { .lo = (uint32_t)&__ftrace_return },
140 #else
141 	.buf_start.ptr64 = (uint64_t)&__ftrace_buf_start,
142 	.buf_end.ptr64 = (uint64_t)__ftrace_buf_end,
143 	.ret_ptr.ptr64 = (uint64_t)&__ftrace_return,
144 #endif
145 };
146 #endif
147 
tahead_get_trace_level(void)148 int tahead_get_trace_level(void)
149 {
150 	/*
151 	 * Store trace level in TA head structure, as ta_head.prop_tracelevel
152 	 */
153 	return TRACE_LEVEL;
154 }
155