• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 
13 #ifndef __TA_FRAMWORK_H_
14 #define __TA_FRAMWORK_H_
15 
16 #include <mem_page_ops.h>
17 
18 #include "tee_defines.h"
19 #include "tee_common.h"
20 #include "tee_time_api.h"
21 #include "tee_core_api.h"
22 #include "ipclib.h"
23 
24 #define TA_SESSION_MAX 8 /* concurrent opened session count */
25 #define TA_STACK_MAX 5   /* we limit ta memory to heap_size + stack_size * 5 to compatible with the old version */
26 
27 #define TASK_INVALID_HANDLE ((uint32_t)0xffffffff)
28 
29 #define GLOBAL_SERVICE_NAME "TEEGlobalTask"
30 #define GLOBAL_HANDLE 0U
31 
32 #define SSA_SERVICE_NAME "task_ssa"
33 
34 #define PERM_SERVICE_NAME "task_permservice"
35 #define BIO_TASK_NAME "task_bioservice"
36 #define ROT_TASK_NAME "task_rotservice"
37 #define ART_TASK_NAME "task_artservice"
38 #define SE_TASK_NAME "task_seservice"
39 #define HSM_TASK_NAME "task_hsmservice"
40 #define SEM_TASK_NAME "sem_task"
41 #define VLTMMSRV_TASK_NAME "task_vltmm_service"
42 #define HUK_TASK_NAME "task_hukservice"
43 #define CRYPTO_AGENT_TASK_NAME "task_cryptoagent_service"
44 #define TEST_SERVICE_TASK_NAME "test_service"
45 #define TEST_SERVICE_TASK_NAME_A64 "test_service_a64"
46 
47 #define TEE_PARAM_NUM 4 /* TA input param nums: TEE_Param[4] */
48 
49 /* add for gtask send msg to service thread begin */
50 /* sizeof 'struct remove_thread_msg' should smaller than NOTIFY_MAX_LEN */
51 #define HANDLE_MAX ((NOTIFY_MAX_LEN - 3 * sizeof(uint32_t)) / sizeof(int))
52 struct remove_thread_msg {
53     uint32_t tid;
54     uint32_t session_id;
55 };
56 
57 #define compile_time_assert(cond, msg) typedef char assert_##msg[(cond) ? 1 : -1]
58 
59 compile_time_assert(sizeof(struct remove_thread_msg) <= NOTIFY_MAX_LEN, size_of_remove_thread_msg_too_large);
60 
61 struct create_thread_msg {
62     uint64_t stack_size;
63 };
64 
65 struct global_to_service_thread_msg {
66     union {
67         struct remove_thread_msg remove_msg;
68         struct create_thread_msg create_msg;
69     };
70 };
71 
72 enum smc_cmd_type {
73     CMD_TYPE_GLOBAL,
74     CMD_TYPE_TA,
75     CMD_TYPE_TA_AGENT,
76     CMD_TYPE_TA2TA_AGENT, /* compatible with TA2TA2TA->AGENT etc. */
77     CMD_TYPE_BUILDIN_AGENT,
78 };
79 
80 /* add for gtask send msg to service thread end */
81 typedef struct {
82     uint8_t uuid[sizeof(TEE_UUID)];
83     unsigned int cmd_type; /* refer to smc_cmd_type */
84     unsigned int cmd_id;
85     unsigned int dev_file_id;
86     unsigned int context; /* high_16bits is service_index, low16_bits is session_id */
87     unsigned int agent_id;
88     unsigned int operation_phys;
89     unsigned int operation_h_phys;
90     unsigned int login_method;
91     unsigned int login_data_phy;
92     unsigned int login_data_h_phy;
93     unsigned int login_data_len;
94     unsigned int err_origin;
95     unsigned int ret_val;
96     unsigned int event_nr;
97     unsigned int uid;
98     unsigned int ca_pid;
99     unsigned int pid;
100     unsigned int eventindex;
101     bool started;
102 } __attribute__((__packed__)) smc_cmd_t;
103 
104 #define SERVICE_NAME_MAX 100
105 
106 #define AGENT_BUFF_SIZE (4 * 1024)
107 // agent id
108 #define TEE_FS_AGENT_ID 0x46536673      /* FSfs */
109 #define TEE_MISC_AGENT_ID 0x4d495343    /* MISC */
110 #define TEE_SOCKET_AGENT_ID 0x69e85664  /* socket */
111 #define TEE_SECLOAD_AGENT_ID 0x4c4f4144 /* SECFILE-LOAD-AGENT */
112 #define TEE_VLTMM_AGENT_ID 0x564c544d   /* agent for vltmm service */
113 
114 struct ta_property {
115     TEE_UUID uuid;
116     uint32_t stack_size;
117     uint32_t heap_size;
118     bool single_instance;
119     bool multi_session;
120     bool keep_alive;
121     bool ssa_enum_enable;
122     char *other_buff;   // TA's non-std property
123     uint32_t other_len; // non-std propery buff len
124 };
125 
126 
127 // data for async call
128 struct notify_context_timer {
129     uint32_t dev_id;
130     TEE_UUID uuid;
131     uint32_t session_id;
132     TEE_timer_property property;
133     uint32_t expire_time;
134 };
135 
136 struct notify_context_wakeup {
137     uint32_t ca_thread_id;
138 };
139 
140 struct notify_context_shadow {
141     uint64_t target_tcb;
142 };
143 
144 struct notify_context_shadow_exit {
145     uint32_t ca_thread_exit;
146 };
147 
148 struct notify_context_meta {
149     uint32_t send_s;
150     uint32_t recv_s;
151     uint32_t send_w;
152     uint32_t recv_w;
153     uint32_t missed; /* type of missed notifications */
154 };
155 
156 union notify_context {
157     struct notify_context_timer timer;
158     struct notify_context_wakeup wakeup;
159     struct notify_context_shadow shadow;
160     struct notify_context_shadow_exit shadow_exit;
161     struct notify_context_meta meta;
162 };
163 
164 struct notify_data_entry {
165     uint32_t entry_type : 31;
166     uint32_t filled : 1;
167     union notify_context context;
168 };
169 
170 #define NOTIFY_DATA_ENTRY_COUNT ((PAGE_SIZE / sizeof(struct notify_data_entry)) - 1)
171 
172 struct notify_data_struct {
173     struct notify_data_entry entry[NOTIFY_DATA_ENTRY_COUNT];
174     struct notify_data_entry meta;
175 };
176 
177 enum notify_data_type {
178     NOTIFY_DATA_ENTRY_UNUSED,
179     NOTIFY_DATA_ENTRY_TIMER,
180     NOTIFY_DATA_ENTRY_RTC,
181     NOTIFY_DATA_ENTRY_WAKEUP,
182     NOTIFY_DATA_ENTRY_SHADOW,
183     NOTIFY_DATA_ENTRY_FIQSHD,
184     NOTIFY_DATA_ENTRY_SHADOW_EXIT,
185     NOTIFY_DATA_ENTRY_MAX,
186 };
187 
188 enum TA_VERSION {
189     TA_SIGN_VERSION = 1,    /* first version */
190     TA_RSA2048_VERSION = 2, /* use rsa 2048, and use right crypt mode */
191     CIPHER_LAYER_VERSION = 3,
192     TA_THIRD_VERSION = 9,
193     TA_SIGN_VERSION_MAX
194 };
195 
196 // global call TA entrypoit cmd
197 #define CALL_TA_DEFAULT_CMD 0x0
198 #define CALL_TA_OPEN_SESSION 0x11
199 #define CALL_TA_INVOKE_CMD 0x12
200 #define CALL_TA_CLOSE_SESSION 0x13
201 #define CALL_TA_OPEN_SESSION_INIT 0x17
202 #define CALL_TA_OPEN_SESSION_PROP 0x18
203 #define CALL_TA_CREATE_THREAD 0x19
204 #define CALL_TA_REMOVE_THREAD 0x22
205 #define CALL_TA_STHREAD_EXIT 0x23
206 
207 #define TA2TA_CALL 0x41
208 #define MSG_ABORT_VALUE 0xf0
209 #define TEE_PANIC_VALUE 0xf1
210 #define MSG_SRE_AUDIT_TRIGER 0xf2
211 #define TA_GET_AGENT_BUFFER 0x20
212 #define TA_CALL_AGENT 0x21
213 #define TA_LOCK_AGENT 0x22
214 #define TA_UNLOCK_AGENT 0x23
215 #define TA_GET_REEINFO 0x24
216 #define TA_GET_CALLERINFO 0x25
217 
218 #define TA_LOCK_ACK 0x29
219 #define BINDER_SERVICE 0x2d
220 #define UNBINDER_SERVICE 0x2e
221 #define TEE_SERVICE_ACK 0x2f
222 #define TEE_SEC_NEED_LOAD 0x30
223 #define TEE_UNLINK_LIB 0x31
224 #define TEE_UNLINK_DYNAMIC_DRV 0x32
225 #define TEE_UNREGISTER_SERVICE 0x2007
226 
227 #define REGISTER_ELF_REQ 0x3E
228 
229 #define TEE_MAX_API_LEVEL_CONFIG ((CIPHER_LAYER_VERSION << 16) | API_LEVEL1_2)
230 
231 struct global_to_ta_msg {
232     uint32_t session_id;
233     uint32_t session_type;
234     uint32_t cmd_id;
235     uint32_t param_type;
236     TEE_Param *params;
237     void *session_context;
238     uint32_t dev_id;
239     char first_session;
240     char last_session;
241     bool started;
242     uint32_t stack_size;
243     TEE_Result ret;
244 };
245 
246 struct ta_to_global_msg {
247     TEE_Result ret;
248     uint32_t agent_id;
249     void *session_context;
250     uint32_t ta2ta_from_taskid;
251 };
252 
253 struct global_to_ta_for_uid {
254     uint32_t userid;
255     uint32_t appid;
256     uint32_t cmd_id;
257     uint32_t reserved;
258 };
259 
260 struct ta_init_msg {
261     void *fs_mem;   /* fs agent share mem */
262     void *misc_mem; /* misc agent share mem */
263     struct ta_property prop;
264     uint32_t login_method;
265     void *time_data;
266     TEE_Time sys_time;
267     uint32_t rtc_time;
268 };
269 
270 struct ta2ta_ret_msg {
271     TEE_Result ret;
272     uint32_t origin;
273     TEE_TASessionHandle session;
274     smc_cmd_t cmd;
275 };
276 
277 struct ta2ta_msg {
278     TEE_Result ret;
279     TEE_TASessionHandle handle;
280     uint64_t session_context; /* no use */
281     uint64_t cmd;             /* this correspond to the pointer of smc_cmd_t cmd */
282     bool is_load_worked;      /* indicate if sec file agent has worked */
283 };
284 
285 #define MAX_NAME_LEN 31
286 struct tee_srvc_send_msg {
287     char srvc_name[MAX_NAME_LEN + 1];
288 };
289 #endif
290