blob: 1a1e8a6207a2491d526f8f7383e28947ad9d7d04 [file] [log] [blame]
Edison Ai764d41f2018-09-21 15:56:36 +08001/*
2 * Copyright (c) 2018-2019, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
Mingyang Sunda01a972019-07-12 17:32:59 +08007
8/* All the APIs defined in this file are used for IPC model. */
9
Edison Ai764d41f2018-09-21 15:56:36 +080010#include <inttypes.h>
Summer Qin2bfd2a02018-09-26 17:10:41 +080011#include <limits.h>
Edison Ai764d41f2018-09-21 15:56:36 +080012#include <stdbool.h>
Edison Ai764d41f2018-09-21 15:56:36 +080013#include <stdlib.h>
Jamie Foxcc31d402019-01-28 17:13:52 +000014#include "psa/client.h"
15#include "psa/service.h"
Edison Ai764d41f2018-09-21 15:56:36 +080016#include "tfm_utils.h"
Mingyang Sunf3d29892019-07-10 17:50:23 +080017#include "tfm_spm_hal.h"
Edison Ai764d41f2018-09-21 15:56:36 +080018#include "spm_api.h"
19#include "spm_db.h"
David Hu326f3972019-08-06 14:16:51 +080020#include "tfm_core_mem_check.h"
Edison Ai764d41f2018-09-21 15:56:36 +080021#include "tfm_internal_defines.h"
22#include "tfm_wait.h"
23#include "tfm_message_queue.h"
24#include "tfm_list.h"
25#include "tfm_pools.h"
Edison Ai764d41f2018-09-21 15:56:36 +080026#include "tfm_thread.h"
Summer Qin2bfd2a02018-09-26 17:10:41 +080027#include "region_defs.h"
Edison Ai764d41f2018-09-21 15:56:36 +080028#include "tfm_nspm.h"
Edison Ai807fedb2019-03-07 11:22:03 +080029#include "tfm_memory_utils.h"
Edison Ai764d41f2018-09-21 15:56:36 +080030
Summer Qind99509f2019-08-02 17:36:58 +080031#include "secure_fw/services/tfm_service_list.inc"
32
33/* Extern service variable */
34extern struct tfm_spm_service_t service[];
35
Edison Ai764d41f2018-09-21 15:56:36 +080036/* Extern SPM variable */
37extern struct spm_partition_db_t g_spm_partition_db;
38
Mate Toth-Palc430b992019-05-09 21:01:14 +020039/* Extern secure lock variable */
40extern int32_t tfm_secure_lock;
Mingyang Sunf3d29892019-07-10 17:50:23 +080041
Edison Ai764d41f2018-09-21 15:56:36 +080042/* Pools */
43TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t),
44 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +080045
Edison Ai764d41f2018-09-21 15:56:36 +080046/********************** SPM functions for handler mode ***********************/
47
48/* Service handle management functions */
49psa_handle_t tfm_spm_create_conn_handle(struct tfm_spm_service_t *service)
50{
Edison Ai9cc26242019-08-06 11:28:04 +080051 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +080052
53 TFM_ASSERT(service);
54
55 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +080056 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
57 if (!p_handle) {
Edison Ai764d41f2018-09-21 15:56:36 +080058 return PSA_NULL_HANDLE;
59 }
60
Edison Ai9cc26242019-08-06 11:28:04 +080061 p_handle->service = service;
Edison Ai764d41f2018-09-21 15:56:36 +080062
63 /* Add handle node to list for next psa functions */
Edison Ai9cc26242019-08-06 11:28:04 +080064 tfm_list_add_tail(&service->handle_list, &p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +080065
Edison Ai9cc26242019-08-06 11:28:04 +080066 return (psa_handle_t)p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +080067}
68
69static struct tfm_conn_handle_t *
Mingyang Sun5e13aa72019-07-10 10:30:16 +080070 tfm_spm_find_conn_handle_node(struct tfm_spm_service_t *service,
71 psa_handle_t conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +080072{
Edison Ai764d41f2018-09-21 15:56:36 +080073 TFM_ASSERT(service);
74
Edison Ai9cc26242019-08-06 11:28:04 +080075 return (struct tfm_conn_handle_t *)conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +080076}
77
78int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service,
79 psa_handle_t conn_handle)
80{
Edison Ai9cc26242019-08-06 11:28:04 +080081 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +080082
83 TFM_ASSERT(service);
84
85 /* There are many handles for each RoT Service */
Edison Ai9cc26242019-08-06 11:28:04 +080086 p_handle = tfm_spm_find_conn_handle_node(service, conn_handle);
87 if (!p_handle) {
Edison Ai764d41f2018-09-21 15:56:36 +080088 tfm_panic();
89 }
90
91 /* Remove node from handle list */
Edison Ai9cc26242019-08-06 11:28:04 +080092 tfm_list_del_node(&p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +080093
94 /* Back handle buffer to pool */
Edison Ai9cc26242019-08-06 11:28:04 +080095 tfm_pool_free(p_handle);
Edison Ai764d41f2018-09-21 15:56:36 +080096 return IPC_SUCCESS;
97}
98
99int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service,
100 psa_handle_t conn_handle,
101 void *rhandle)
102{
Edison Ai9cc26242019-08-06 11:28:04 +0800103 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800104
105 TFM_ASSERT(service);
106 /* Set reverse handle value only be allowed for a connected handle */
107 TFM_ASSERT(conn_handle != PSA_NULL_HANDLE);
108
109 /* There are many handles for each RoT Service */
Edison Ai9cc26242019-08-06 11:28:04 +0800110 p_handle = tfm_spm_find_conn_handle_node(service, conn_handle);
111 if (!p_handle) {
Edison Ai764d41f2018-09-21 15:56:36 +0800112 tfm_panic();
113 }
114
Edison Ai9cc26242019-08-06 11:28:04 +0800115 p_handle->rhandle = rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800116 return IPC_SUCCESS;
117}
118
119void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service,
120 psa_handle_t conn_handle)
121{
Edison Ai9cc26242019-08-06 11:28:04 +0800122 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800123
124 TFM_ASSERT(service);
125 /* Get reverse handle value only be allowed for a connected handle */
126 TFM_ASSERT(conn_handle != PSA_NULL_HANDLE);
127
128 /* There are many handles for each RoT Service */
Edison Ai9cc26242019-08-06 11:28:04 +0800129 p_handle = tfm_spm_find_conn_handle_node(service, conn_handle);
130 if (!p_handle) {
Edison Ai764d41f2018-09-21 15:56:36 +0800131 tfm_panic();
132 }
133
Edison Ai9cc26242019-08-06 11:28:04 +0800134 return p_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800135}
136
137/* Partition management functions */
138struct tfm_spm_service_t *
Mingyang Sunf3d29892019-07-10 17:50:23 +0800139 tfm_spm_get_service_by_signal(struct spm_partition_desc_t *partition,
140 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800141{
142 struct tfm_list_node_t *node, *head;
143 struct tfm_spm_service_t *service;
144
145 TFM_ASSERT(partition);
146
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800147 if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
Edison Ai764d41f2018-09-21 15:56:36 +0800148 tfm_panic();
149 }
150
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800151 head = &partition->runtime_data.service_list;
Edison Ai764d41f2018-09-21 15:56:36 +0800152 TFM_LIST_FOR_EACH(node, head) {
153 service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t, list);
Summer Qind99509f2019-08-02 17:36:58 +0800154 if (service->service_db.signal == signal) {
Edison Ai764d41f2018-09-21 15:56:36 +0800155 return service;
156 }
157 }
158 return NULL;
159}
160
161struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid)
162{
163 uint32_t i;
164 struct tfm_list_node_t *node, *head;
165 struct tfm_spm_service_t *service;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800166 struct spm_partition_desc_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800167
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200168 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800169 partition = &g_spm_partition_db.partitions[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800170 /* Skip partition without IPC flag */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800171 if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
Edison Ai764d41f2018-09-21 15:56:36 +0800172 continue;
173 }
174
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800175 if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
Edison Ai764d41f2018-09-21 15:56:36 +0800176 continue;
177 }
178
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800179 head = &partition->runtime_data.service_list;
Edison Ai764d41f2018-09-21 15:56:36 +0800180 TFM_LIST_FOR_EACH(node, head) {
181 service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t,
182 list);
Summer Qind99509f2019-08-02 17:36:58 +0800183 if (service->service_db.sid == sid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800184 return service;
185 }
186 }
187 }
188 return NULL;
189}
190
191struct tfm_spm_service_t *
192 tfm_spm_get_service_by_handle(psa_handle_t conn_handle)
193{
Edison Ai9cc26242019-08-06 11:28:04 +0800194 return ((struct tfm_conn_handle_t *)conn_handle)->service;
Edison Ai764d41f2018-09-21 15:56:36 +0800195}
196
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800197struct spm_partition_desc_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800198{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800199 uint32_t idx = get_partition_idx(partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800200
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800201 if (idx != SPM_INVALID_PARTITION_IDX) {
202 return &(g_spm_partition_db.partitions[idx]);
Edison Ai764d41f2018-09-21 15:56:36 +0800203 }
204 return NULL;
205}
206
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800207struct spm_partition_desc_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800208{
209 uint32_t spid;
210
Mingyang Sunf3d29892019-07-10 17:50:23 +0800211 spid = tfm_spm_partition_get_running_partition_id();
Edison Ai764d41f2018-09-21 15:56:36 +0800212
213 return tfm_spm_get_partition_by_id(spid);
214}
215
216int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
217 uint32_t minor_version)
218{
219 TFM_ASSERT(service);
220
Summer Qind99509f2019-08-02 17:36:58 +0800221 switch (service->service_db.minor_policy) {
Edison Ai764d41f2018-09-21 15:56:36 +0800222 case TFM_VERSION_POLICY_RELAXED:
Summer Qind99509f2019-08-02 17:36:58 +0800223 if (minor_version > service->service_db.minor_version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800224 return IPC_ERROR_VERSION;
225 }
226 break;
227 case TFM_VERSION_POLICY_STRICT:
Summer Qind99509f2019-08-02 17:36:58 +0800228 if (minor_version != service->service_db.minor_version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800229 return IPC_ERROR_VERSION;
230 }
231 break;
232 default:
233 return IPC_ERROR_VERSION;
234 }
235 return IPC_SUCCESS;
236}
237
238/* Message functions */
239struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
240{
241 /*
242 * There may be one error handle passed by the caller in two conditions:
243 * 1. Not a valid message handle.
244 * 2. Handle between different Partitions. Partition A passes one handle
245 * belong to other Partitions and tries to access other's data.
246 * So, need do necessary checking to prevent those conditions.
247 */
248 struct tfm_msg_body_t *msg;
249 uint32_t partition_id;
250
251 msg = (struct tfm_msg_body_t *)msg_handle;
252 if (!msg) {
253 return NULL;
254 }
255
256 /*
257 * FixMe: For condition 1: using a magic number to define it's a message.
258 * It needs to be an enhancement to check the handle belong to service.
259 */
260 if (msg->magic != TFM_MSG_MAGIC) {
261 return NULL;
262 }
263
264 /* For condition 2: check if the partition ID is same */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800265 partition_id = tfm_spm_partition_get_running_partition_id();
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800266 if (partition_id != msg->service->partition->static_data.partition_id) {
Edison Ai764d41f2018-09-21 15:56:36 +0800267 return NULL;
268 }
269
270 return msg;
271}
272
Edison Ai97115822019-08-01 14:22:19 +0800273struct tfm_msg_body_t *
274 tfm_spm_get_msg_buffer_from_conn_handle(psa_handle_t conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800275{
Edison Ai97115822019-08-01 14:22:19 +0800276 TFM_ASSERT(conn_handle != PSA_NULL_HANDLE);
277
278 return &(((struct tfm_conn_handle_t *)conn_handle)->internal_msg);
279}
280
281void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
282 struct tfm_spm_service_t *service,
283 psa_handle_t handle,
284 int32_t type, int32_t ns_caller,
285 psa_invec *invec, size_t in_len,
286 psa_outvec *outvec, size_t out_len,
287 psa_outvec *caller_outvec)
288{
Edison Ai764d41f2018-09-21 15:56:36 +0800289 uint32_t i;
290
Edison Ai97115822019-08-01 14:22:19 +0800291 TFM_ASSERT(msg);
Edison Ai764d41f2018-09-21 15:56:36 +0800292 TFM_ASSERT(service);
293 TFM_ASSERT(!(invec == NULL && in_len != 0));
294 TFM_ASSERT(!(outvec == NULL && out_len != 0));
295 TFM_ASSERT(in_len <= PSA_MAX_IOVEC);
296 TFM_ASSERT(out_len <= PSA_MAX_IOVEC);
297 TFM_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
298
Edison Ai764d41f2018-09-21 15:56:36 +0800299 /* Clear message buffer before using it */
300 tfm_memset(msg, 0, sizeof(struct tfm_msg_body_t));
301
Ken Liu35f89392019-03-14 14:51:05 +0800302 tfm_event_init(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800303 msg->magic = TFM_MSG_MAGIC;
304 msg->service = service;
305 msg->handle = handle;
306 msg->caller_outvec = caller_outvec;
307 /* Get current partition id */
308 if (ns_caller) {
309 msg->msg.client_id = tfm_nspm_get_current_client_id();
310 } else {
Mingyang Sunf3d29892019-07-10 17:50:23 +0800311 msg->msg.client_id = tfm_spm_partition_get_running_partition_id();
Edison Ai764d41f2018-09-21 15:56:36 +0800312 }
313
314 /* Copy contents */
315 msg->msg.type = type;
316
317 for (i = 0; i < in_len; i++) {
318 msg->msg.in_size[i] = invec[i].len;
319 msg->invec[i].base = invec[i].base;
320 }
321
322 for (i = 0; i < out_len; i++) {
323 msg->msg.out_size[i] = outvec[i].len;
324 msg->outvec[i].base = outvec[i].base;
325 /* Out len is used to record the writed number, set 0 here again */
326 msg->outvec[i].len = 0;
327 }
328
329 /* Use message address as handle */
330 msg->msg.handle = (psa_handle_t)msg;
331
332 /* For connected handle, set rhandle to every message */
333 if (handle != PSA_NULL_HANDLE) {
334 msg->msg.rhandle = tfm_spm_get_rhandle(service, handle);
335 }
Edison Ai764d41f2018-09-21 15:56:36 +0800336}
337
338int32_t tfm_spm_send_event(struct tfm_spm_service_t *service,
339 struct tfm_msg_body_t *msg)
340{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800341 struct spm_partition_runtime_data_t *p_runtime_data =
342 &service->partition->runtime_data;
343
Edison Ai764d41f2018-09-21 15:56:36 +0800344 TFM_ASSERT(service);
345 TFM_ASSERT(msg);
346
347 /* Enqueue message to service message queue */
348 if (tfm_msg_enqueue(&service->msg_queue, msg) != IPC_SUCCESS) {
349 return IPC_ERROR_GENERIC;
350 }
351
352 /* Messages put. Update signals */
Summer Qind99509f2019-08-02 17:36:58 +0800353 p_runtime_data->signals |= service->service_db.signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800354
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800355 tfm_event_wake(&p_runtime_data->signal_evnt, (p_runtime_data->signals &
356 p_runtime_data->signal_mask));
Edison Ai764d41f2018-09-21 15:56:36 +0800357
Ken Liu35f89392019-03-14 14:51:05 +0800358 tfm_event_wait(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800359
360 return IPC_SUCCESS;
361}
362
Edison Ai7aff9e82019-07-11 14:56:46 +0800363uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
364{
365 return g_spm_partition_db.partitions[partition_idx].
366 memory_data.stack_bottom;
367}
368
369uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
370{
371 return g_spm_partition_db.partitions[partition_idx].memory_data.stack_top;
372}
373
Mingyang Sunf3d29892019-07-10 17:50:23 +0800374uint32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800375{
376 struct tfm_thrd_ctx *pth = tfm_thrd_curr_thread();
377 struct spm_partition_desc_t *partition;
378
379 partition = TFM_GET_CONTAINER_PTR(pth, struct spm_partition_desc_t,
380 sp_thrd);
381 return partition->static_data.partition_id;
382}
383
384static struct tfm_thrd_ctx *
Mingyang Sunf3d29892019-07-10 17:50:23 +0800385 tfm_spm_partition_get_thread_info(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800386{
387 return &g_spm_partition_db.partitions[partition_idx].sp_thrd;
388}
389
Edison Ai764d41f2018-09-21 15:56:36 +0800390static tfm_thrd_func_t
Mingyang Sunf3d29892019-07-10 17:50:23 +0800391 tfm_spm_partition_get_init_func(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800392{
393 return (tfm_thrd_func_t)(g_spm_partition_db.partitions[partition_idx].
394 static_data.partition_init);
395}
396
Mingyang Sunf3d29892019-07-10 17:50:23 +0800397static uint32_t tfm_spm_partition_get_priority(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800398{
399 return g_spm_partition_db.partitions[partition_idx].static_data.
400 partition_priority;
401}
402
David Hucb05d972019-08-06 18:10:11 +0800403int32_t tfm_memory_check(const void *buffer, size_t len, int32_t ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800404 enum tfm_memory_access_e access,
405 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800406{
Hugues de Valon99578562019-06-18 16:08:51 +0100407 enum tfm_status_e err;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800408
409 /* If len is zero, this indicates an empty buffer and base is ignored */
410 if (len == 0) {
411 return IPC_SUCCESS;
412 }
413
414 if (!buffer) {
415 return IPC_ERROR_BAD_PARAMETERS;
416 }
417
418 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
419 return IPC_ERROR_MEMORY_CHECK;
420 }
421
Summer Qin424d4db2019-03-25 14:09:51 +0800422 if (access == TFM_MEMORY_ACCESS_RW) {
Summer Qineb537e52019-03-29 09:57:10 +0800423 err = tfm_core_has_write_access_to_region(buffer, len, ns_caller,
424 privileged);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800425 } else {
Summer Qineb537e52019-03-29 09:57:10 +0800426 err = tfm_core_has_read_access_to_region(buffer, len, ns_caller,
427 privileged);
Summer Qin424d4db2019-03-25 14:09:51 +0800428 }
Summer Qin0fc3f592019-04-11 16:00:10 +0800429 if (err == TFM_SUCCESS) {
Summer Qin424d4db2019-03-25 14:09:51 +0800430 return IPC_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800431 }
432
433 return IPC_ERROR_MEMORY_CHECK;
434}
435
Summer Qineb537e52019-03-29 09:57:10 +0800436uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_idx)
437{
438 if (tfm_spm_partition_get_flags(partition_idx) & SPM_PART_FLAG_PSA_ROT) {
439 return TFM_PARTITION_PRIVILEGED_MODE;
440 } else {
441 return TFM_PARTITION_UNPRIVILEGED_MODE;
442 }
443}
444
Edison Ai764d41f2018-09-21 15:56:36 +0800445/********************** SPM functions for thread mode ************************/
446
447void tfm_spm_init(void)
448{
449 uint32_t i, num;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800450 struct spm_partition_desc_t *partition;
Summer Qind99509f2019-08-02 17:36:58 +0800451 /*struct tfm_spm_service_t *service;*/
Ken Liu483f5da2019-04-24 10:45:21 +0800452 struct tfm_thrd_ctx *pth, this_thrd;
Edison Ai764d41f2018-09-21 15:56:36 +0800453
454 tfm_pool_init(conn_handle_pool,
455 POOL_BUFFER_SIZE(conn_handle_pool),
456 sizeof(struct tfm_conn_handle_t),
457 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800458
459 /* Init partition first for it will be used when init service */
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200460 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800461 partition = &g_spm_partition_db.partitions[i];
462 tfm_spm_hal_configure_default_isolation(partition->platform_data);
463 partition->static_data.index = i;
Edison Ai764d41f2018-09-21 15:56:36 +0800464 if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
465 continue;
466 }
Ken Liu35f89392019-03-14 14:51:05 +0800467
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800468 tfm_event_init(&partition->runtime_data.signal_evnt);
469 tfm_list_init(&partition->runtime_data.service_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800470
Mingyang Sunf3d29892019-07-10 17:50:23 +0800471 pth = tfm_spm_partition_get_thread_info(i);
Edison Ai764d41f2018-09-21 15:56:36 +0800472 if (!pth) {
473 tfm_panic();
474 }
475
476 tfm_thrd_init(pth,
Mingyang Sunf3d29892019-07-10 17:50:23 +0800477 tfm_spm_partition_get_init_func(i),
Edison Ai764d41f2018-09-21 15:56:36 +0800478 NULL,
Edison Ai788bae22019-02-18 17:38:59 +0800479 (uint8_t *)tfm_spm_partition_get_stack_top(i),
480 (uint8_t *)tfm_spm_partition_get_stack_bottom(i));
Edison Ai788bae22019-02-18 17:38:59 +0800481
Mingyang Sunf3d29892019-07-10 17:50:23 +0800482 pth->prior = tfm_spm_partition_get_priority(i);
Edison Ai764d41f2018-09-21 15:56:36 +0800483
484 /* Kick off */
485 if (tfm_thrd_start(pth) != THRD_SUCCESS) {
486 tfm_panic();
487 }
488 }
489
490 /* Init Service */
Summer Qind99509f2019-08-02 17:36:58 +0800491 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
Edison Ai764d41f2018-09-21 15:56:36 +0800492 for (i = 0; i < num; i++) {
493 partition =
Summer Qind99509f2019-08-02 17:36:58 +0800494 tfm_spm_get_partition_by_id(service[i].service_db.partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800495 if (!partition) {
496 tfm_panic();
497 }
Summer Qind99509f2019-08-02 17:36:58 +0800498 service[i].partition = partition;
499 tfm_list_init(&service[i].handle_list);
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800500 tfm_list_add_tail(&partition->runtime_data.service_list,
Summer Qind99509f2019-08-02 17:36:58 +0800501 &service[i].list);
Edison Ai764d41f2018-09-21 15:56:36 +0800502 }
503
Ken Liu483f5da2019-04-24 10:45:21 +0800504 /*
505 * All threads initialized, start the scheduler.
506 *
507 * NOTE:
508 * Here is the booting privileged thread mode, and will never
509 * return to this place after scheduler is started. The start
510 * function has to save current runtime context to act as a
511 * 'current thread' to avoid repeating NULL 'current thread'
512 * checking while context switching. This saved context is worthy
513 * of being saved somewhere if there are potential usage purpose.
514 * Let's save this context in a local variable 'this_thrd' at
515 * current since there is no usage for it.
Mate Toth-Palc430b992019-05-09 21:01:14 +0200516 * Also set tfm_nspm_thread_entry as pfn for this thread to
517 * use in detecting NS/S thread scheduling changes.
Ken Liu483f5da2019-04-24 10:45:21 +0800518 */
Mate Toth-Palc430b992019-05-09 21:01:14 +0200519 this_thrd.pfn = (tfm_thrd_func_t)tfm_nspm_thread_entry;
Ken Liu483f5da2019-04-24 10:45:21 +0800520 tfm_thrd_start_scheduler(&this_thrd);
Edison Ai764d41f2018-09-21 15:56:36 +0800521}
Ken Liu2d175172019-03-21 17:08:41 +0800522
523void tfm_pendsv_do_schedule(struct tfm_state_context_ext *ctxb)
524{
525#if TFM_LVL == 2
526 struct spm_partition_desc_t *p_next_partition;
527 uint32_t is_privileged;
528#endif
529 struct tfm_thrd_ctx *pth_next = tfm_thrd_next_thread();
530 struct tfm_thrd_ctx *pth_curr = tfm_thrd_curr_thread();
531
Mate Toth-Pal32b2ccd2019-04-26 10:00:16 +0200532 if (pth_next != NULL && pth_curr != pth_next) {
Ken Liu2d175172019-03-21 17:08:41 +0800533#if TFM_LVL == 2
534 p_next_partition = TFM_GET_CONTAINER_PTR(pth_next,
535 struct spm_partition_desc_t,
536 sp_thrd);
537
538 if (p_next_partition->static_data.partition_flags &
539 SPM_PART_FLAG_PSA_ROT) {
540 is_privileged = TFM_PARTITION_PRIVILEGED_MODE;
541 } else {
542 is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE;
543 }
544
545 tfm_spm_partition_change_privilege(is_privileged);
546#endif
Mate Toth-Palc430b992019-05-09 21:01:14 +0200547 /* Increase the secure lock, if we enter secure from non-secure */
548 if ((void *)pth_curr->pfn == (void *)tfm_nspm_thread_entry) {
549 ++tfm_secure_lock;
550 }
551 /* Decrease the secure lock, if we return from secure to non-secure */
552 if ((void *)pth_next->pfn == (void *)tfm_nspm_thread_entry) {
553 --tfm_secure_lock;
554 }
555
Ken Liu2d175172019-03-21 17:08:41 +0800556 tfm_thrd_context_switch(ctxb, pth_curr, pth_next);
557 }
558}