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