blob: f03cf96e4bac35615cd5f8a8b29d558f7b5e93f1 [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{
51 struct tfm_conn_handle_t *node;
52
53 TFM_ASSERT(service);
54
55 /* Get buffer for handle list structure from handle pool */
56 node = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
57 if (!node) {
58 return PSA_NULL_HANDLE;
59 }
60
61 /* Global unique handle, use handle buffer address directly */
62 node->handle = (psa_handle_t)node;
63
64 /* Add handle node to list for next psa functions */
65 tfm_list_add_tail(&service->handle_list, &node->list);
66
67 return node->handle;
68}
69
70static struct tfm_conn_handle_t *
Mingyang Sun5e13aa72019-07-10 10:30:16 +080071 tfm_spm_find_conn_handle_node(struct tfm_spm_service_t *service,
72 psa_handle_t conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +080073{
74 struct tfm_conn_handle_t *handle_node;
75 struct tfm_list_node_t *node, *head;
76
77 TFM_ASSERT(service);
78
79 head = &service->handle_list;
80 TFM_LIST_FOR_EACH(node, head) {
81 handle_node = TFM_GET_CONTAINER_PTR(node, struct tfm_conn_handle_t,
82 list);
83 if (handle_node->handle == conn_handle) {
84 return handle_node;
85 }
86 }
87 return NULL;
88}
89
90int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service,
91 psa_handle_t conn_handle)
92{
93 struct tfm_conn_handle_t *node;
94
95 TFM_ASSERT(service);
96
97 /* There are many handles for each RoT Service */
98 node = tfm_spm_find_conn_handle_node(service, conn_handle);
99 if (!node) {
100 tfm_panic();
101 }
102
103 /* Remove node from handle list */
104 tfm_list_del_node(&node->list);
105
106 /* Back handle buffer to pool */
107 tfm_pool_free(node);
108 return IPC_SUCCESS;
109}
110
111int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service,
112 psa_handle_t conn_handle,
113 void *rhandle)
114{
115 struct tfm_conn_handle_t *node;
116
117 TFM_ASSERT(service);
118 /* Set reverse handle value only be allowed for a connected handle */
119 TFM_ASSERT(conn_handle != PSA_NULL_HANDLE);
120
121 /* There are many handles for each RoT Service */
122 node = tfm_spm_find_conn_handle_node(service, conn_handle);
123 if (!node) {
124 tfm_panic();
125 }
126
127 node->rhandle = rhandle;
128 return IPC_SUCCESS;
129}
130
131void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service,
132 psa_handle_t conn_handle)
133{
134 struct tfm_conn_handle_t *node;
135
136 TFM_ASSERT(service);
137 /* Get reverse handle value only be allowed for a connected handle */
138 TFM_ASSERT(conn_handle != PSA_NULL_HANDLE);
139
140 /* There are many handles for each RoT Service */
141 node = tfm_spm_find_conn_handle_node(service, conn_handle);
142 if (!node) {
143 tfm_panic();
144 }
145
146 return node->rhandle;
147}
148
149/* Partition management functions */
150struct tfm_spm_service_t *
Mingyang Sunf3d29892019-07-10 17:50:23 +0800151 tfm_spm_get_service_by_signal(struct spm_partition_desc_t *partition,
152 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800153{
154 struct tfm_list_node_t *node, *head;
155 struct tfm_spm_service_t *service;
156
157 TFM_ASSERT(partition);
158
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800159 if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
Edison Ai764d41f2018-09-21 15:56:36 +0800160 tfm_panic();
161 }
162
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800163 head = &partition->runtime_data.service_list;
Edison Ai764d41f2018-09-21 15:56:36 +0800164 TFM_LIST_FOR_EACH(node, head) {
165 service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t, list);
Summer Qind99509f2019-08-02 17:36:58 +0800166 if (service->service_db.signal == signal) {
Edison Ai764d41f2018-09-21 15:56:36 +0800167 return service;
168 }
169 }
170 return NULL;
171}
172
173struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid)
174{
175 uint32_t i;
176 struct tfm_list_node_t *node, *head;
177 struct tfm_spm_service_t *service;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800178 struct spm_partition_desc_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800179
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200180 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800181 partition = &g_spm_partition_db.partitions[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800182 /* Skip partition without IPC flag */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800183 if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
Edison Ai764d41f2018-09-21 15:56:36 +0800184 continue;
185 }
186
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800187 if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
Edison Ai764d41f2018-09-21 15:56:36 +0800188 continue;
189 }
190
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800191 head = &partition->runtime_data.service_list;
Edison Ai764d41f2018-09-21 15:56:36 +0800192 TFM_LIST_FOR_EACH(node, head) {
193 service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t,
194 list);
Summer Qind99509f2019-08-02 17:36:58 +0800195 if (service->service_db.sid == sid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800196 return service;
197 }
198 }
199 }
200 return NULL;
201}
202
203struct tfm_spm_service_t *
204 tfm_spm_get_service_by_handle(psa_handle_t conn_handle)
205{
206 uint32_t i;
207 struct tfm_conn_handle_t *handle;
208 struct tfm_list_node_t *service_node, *service_head;
209 struct tfm_list_node_t *handle_node, *handle_head;
210 struct tfm_spm_service_t *service;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800211 struct spm_partition_desc_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800212
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200213 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800214 partition = &g_spm_partition_db.partitions[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800215 /* Skip partition without IPC flag */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800216 if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
Edison Ai764d41f2018-09-21 15:56:36 +0800217 continue;
218 }
219
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800220 if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
Edison Ai764d41f2018-09-21 15:56:36 +0800221 continue;
222 }
223
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800224 service_head = &partition->runtime_data.service_list;
Edison Ai764d41f2018-09-21 15:56:36 +0800225 TFM_LIST_FOR_EACH(service_node, service_head) {
226 service = TFM_GET_CONTAINER_PTR(service_node,
227 struct tfm_spm_service_t, list);
228 handle_head = &service->handle_list;
229 TFM_LIST_FOR_EACH(handle_node, handle_head) {
230 handle = TFM_GET_CONTAINER_PTR(handle_node,
231 struct tfm_conn_handle_t, list);
232 if (handle->handle == conn_handle) {
233 return service;
234 }
235 }
236 }
237 }
238 return NULL;
239}
240
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800241struct spm_partition_desc_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800242{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800243 uint32_t idx = get_partition_idx(partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800244
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800245 if (idx != SPM_INVALID_PARTITION_IDX) {
246 return &(g_spm_partition_db.partitions[idx]);
Edison Ai764d41f2018-09-21 15:56:36 +0800247 }
248 return NULL;
249}
250
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800251struct spm_partition_desc_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800252{
253 uint32_t spid;
254
Mingyang Sunf3d29892019-07-10 17:50:23 +0800255 spid = tfm_spm_partition_get_running_partition_id();
Edison Ai764d41f2018-09-21 15:56:36 +0800256
257 return tfm_spm_get_partition_by_id(spid);
258}
259
260int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
261 uint32_t minor_version)
262{
263 TFM_ASSERT(service);
264
Summer Qind99509f2019-08-02 17:36:58 +0800265 switch (service->service_db.minor_policy) {
Edison Ai764d41f2018-09-21 15:56:36 +0800266 case TFM_VERSION_POLICY_RELAXED:
Summer Qind99509f2019-08-02 17:36:58 +0800267 if (minor_version > service->service_db.minor_version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800268 return IPC_ERROR_VERSION;
269 }
270 break;
271 case TFM_VERSION_POLICY_STRICT:
Summer Qind99509f2019-08-02 17:36:58 +0800272 if (minor_version != service->service_db.minor_version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800273 return IPC_ERROR_VERSION;
274 }
275 break;
276 default:
277 return IPC_ERROR_VERSION;
278 }
279 return IPC_SUCCESS;
280}
281
282/* Message functions */
283struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
284{
285 /*
286 * There may be one error handle passed by the caller in two conditions:
287 * 1. Not a valid message handle.
288 * 2. Handle between different Partitions. Partition A passes one handle
289 * belong to other Partitions and tries to access other's data.
290 * So, need do necessary checking to prevent those conditions.
291 */
292 struct tfm_msg_body_t *msg;
293 uint32_t partition_id;
294
295 msg = (struct tfm_msg_body_t *)msg_handle;
296 if (!msg) {
297 return NULL;
298 }
299
300 /*
301 * FixMe: For condition 1: using a magic number to define it's a message.
302 * It needs to be an enhancement to check the handle belong to service.
303 */
304 if (msg->magic != TFM_MSG_MAGIC) {
305 return NULL;
306 }
307
308 /* For condition 2: check if the partition ID is same */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800309 partition_id = tfm_spm_partition_get_running_partition_id();
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800310 if (partition_id != msg->service->partition->static_data.partition_id) {
Edison Ai764d41f2018-09-21 15:56:36 +0800311 return NULL;
312 }
313
314 return msg;
315}
316
Edison Ai97115822019-08-01 14:22:19 +0800317struct tfm_msg_body_t *
318 tfm_spm_get_msg_buffer_from_conn_handle(psa_handle_t conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800319{
Edison Ai97115822019-08-01 14:22:19 +0800320 TFM_ASSERT(conn_handle != PSA_NULL_HANDLE);
321
322 return &(((struct tfm_conn_handle_t *)conn_handle)->internal_msg);
323}
324
325void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
326 struct tfm_spm_service_t *service,
327 psa_handle_t handle,
328 int32_t type, int32_t ns_caller,
329 psa_invec *invec, size_t in_len,
330 psa_outvec *outvec, size_t out_len,
331 psa_outvec *caller_outvec)
332{
Edison Ai764d41f2018-09-21 15:56:36 +0800333 uint32_t i;
334
Edison Ai97115822019-08-01 14:22:19 +0800335 TFM_ASSERT(msg);
Edison Ai764d41f2018-09-21 15:56:36 +0800336 TFM_ASSERT(service);
337 TFM_ASSERT(!(invec == NULL && in_len != 0));
338 TFM_ASSERT(!(outvec == NULL && out_len != 0));
339 TFM_ASSERT(in_len <= PSA_MAX_IOVEC);
340 TFM_ASSERT(out_len <= PSA_MAX_IOVEC);
341 TFM_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
342
Edison Ai764d41f2018-09-21 15:56:36 +0800343 /* Clear message buffer before using it */
344 tfm_memset(msg, 0, sizeof(struct tfm_msg_body_t));
345
Ken Liu35f89392019-03-14 14:51:05 +0800346 tfm_event_init(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800347 msg->magic = TFM_MSG_MAGIC;
348 msg->service = service;
349 msg->handle = handle;
350 msg->caller_outvec = caller_outvec;
351 /* Get current partition id */
352 if (ns_caller) {
353 msg->msg.client_id = tfm_nspm_get_current_client_id();
354 } else {
Mingyang Sunf3d29892019-07-10 17:50:23 +0800355 msg->msg.client_id = tfm_spm_partition_get_running_partition_id();
Edison Ai764d41f2018-09-21 15:56:36 +0800356 }
357
358 /* Copy contents */
359 msg->msg.type = type;
360
361 for (i = 0; i < in_len; i++) {
362 msg->msg.in_size[i] = invec[i].len;
363 msg->invec[i].base = invec[i].base;
364 }
365
366 for (i = 0; i < out_len; i++) {
367 msg->msg.out_size[i] = outvec[i].len;
368 msg->outvec[i].base = outvec[i].base;
369 /* Out len is used to record the writed number, set 0 here again */
370 msg->outvec[i].len = 0;
371 }
372
373 /* Use message address as handle */
374 msg->msg.handle = (psa_handle_t)msg;
375
376 /* For connected handle, set rhandle to every message */
377 if (handle != PSA_NULL_HANDLE) {
378 msg->msg.rhandle = tfm_spm_get_rhandle(service, handle);
379 }
Edison Ai764d41f2018-09-21 15:56:36 +0800380}
381
382int32_t tfm_spm_send_event(struct tfm_spm_service_t *service,
383 struct tfm_msg_body_t *msg)
384{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800385 struct spm_partition_runtime_data_t *p_runtime_data =
386 &service->partition->runtime_data;
387
Edison Ai764d41f2018-09-21 15:56:36 +0800388 TFM_ASSERT(service);
389 TFM_ASSERT(msg);
390
391 /* Enqueue message to service message queue */
392 if (tfm_msg_enqueue(&service->msg_queue, msg) != IPC_SUCCESS) {
393 return IPC_ERROR_GENERIC;
394 }
395
396 /* Messages put. Update signals */
Summer Qind99509f2019-08-02 17:36:58 +0800397 p_runtime_data->signals |= service->service_db.signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800398
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800399 tfm_event_wake(&p_runtime_data->signal_evnt, (p_runtime_data->signals &
400 p_runtime_data->signal_mask));
Edison Ai764d41f2018-09-21 15:56:36 +0800401
Ken Liu35f89392019-03-14 14:51:05 +0800402 tfm_event_wait(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800403
404 return IPC_SUCCESS;
405}
406
Edison Ai7aff9e82019-07-11 14:56:46 +0800407uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
408{
409 return g_spm_partition_db.partitions[partition_idx].
410 memory_data.stack_bottom;
411}
412
413uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
414{
415 return g_spm_partition_db.partitions[partition_idx].memory_data.stack_top;
416}
417
Mingyang Sunf3d29892019-07-10 17:50:23 +0800418uint32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800419{
420 struct tfm_thrd_ctx *pth = tfm_thrd_curr_thread();
421 struct spm_partition_desc_t *partition;
422
423 partition = TFM_GET_CONTAINER_PTR(pth, struct spm_partition_desc_t,
424 sp_thrd);
425 return partition->static_data.partition_id;
426}
427
428static struct tfm_thrd_ctx *
Mingyang Sunf3d29892019-07-10 17:50:23 +0800429 tfm_spm_partition_get_thread_info(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800430{
431 return &g_spm_partition_db.partitions[partition_idx].sp_thrd;
432}
433
Edison Ai764d41f2018-09-21 15:56:36 +0800434static tfm_thrd_func_t
Mingyang Sunf3d29892019-07-10 17:50:23 +0800435 tfm_spm_partition_get_init_func(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800436{
437 return (tfm_thrd_func_t)(g_spm_partition_db.partitions[partition_idx].
438 static_data.partition_init);
439}
440
Mingyang Sunf3d29892019-07-10 17:50:23 +0800441static uint32_t tfm_spm_partition_get_priority(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800442{
443 return g_spm_partition_db.partitions[partition_idx].static_data.
444 partition_priority;
445}
446
David Hucb05d972019-08-06 18:10:11 +0800447int32_t tfm_memory_check(const void *buffer, size_t len, int32_t ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800448 enum tfm_memory_access_e access,
449 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800450{
Hugues de Valon99578562019-06-18 16:08:51 +0100451 enum tfm_status_e err;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800452
453 /* If len is zero, this indicates an empty buffer and base is ignored */
454 if (len == 0) {
455 return IPC_SUCCESS;
456 }
457
458 if (!buffer) {
459 return IPC_ERROR_BAD_PARAMETERS;
460 }
461
462 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
463 return IPC_ERROR_MEMORY_CHECK;
464 }
465
Summer Qin424d4db2019-03-25 14:09:51 +0800466 if (access == TFM_MEMORY_ACCESS_RW) {
Summer Qineb537e52019-03-29 09:57:10 +0800467 err = tfm_core_has_write_access_to_region(buffer, len, ns_caller,
468 privileged);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800469 } else {
Summer Qineb537e52019-03-29 09:57:10 +0800470 err = tfm_core_has_read_access_to_region(buffer, len, ns_caller,
471 privileged);
Summer Qin424d4db2019-03-25 14:09:51 +0800472 }
Summer Qin0fc3f592019-04-11 16:00:10 +0800473 if (err == TFM_SUCCESS) {
Summer Qin424d4db2019-03-25 14:09:51 +0800474 return IPC_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800475 }
476
477 return IPC_ERROR_MEMORY_CHECK;
478}
479
Summer Qineb537e52019-03-29 09:57:10 +0800480uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_idx)
481{
482 if (tfm_spm_partition_get_flags(partition_idx) & SPM_PART_FLAG_PSA_ROT) {
483 return TFM_PARTITION_PRIVILEGED_MODE;
484 } else {
485 return TFM_PARTITION_UNPRIVILEGED_MODE;
486 }
487}
488
Edison Ai764d41f2018-09-21 15:56:36 +0800489/********************** SPM functions for thread mode ************************/
490
491void tfm_spm_init(void)
492{
493 uint32_t i, num;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800494 struct spm_partition_desc_t *partition;
Summer Qind99509f2019-08-02 17:36:58 +0800495 /*struct tfm_spm_service_t *service;*/
Ken Liu483f5da2019-04-24 10:45:21 +0800496 struct tfm_thrd_ctx *pth, this_thrd;
Edison Ai764d41f2018-09-21 15:56:36 +0800497
498 tfm_pool_init(conn_handle_pool,
499 POOL_BUFFER_SIZE(conn_handle_pool),
500 sizeof(struct tfm_conn_handle_t),
501 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800502
503 /* Init partition first for it will be used when init service */
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200504 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800505 partition = &g_spm_partition_db.partitions[i];
506 tfm_spm_hal_configure_default_isolation(partition->platform_data);
507 partition->static_data.index = i;
Edison Ai764d41f2018-09-21 15:56:36 +0800508 if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
509 continue;
510 }
Ken Liu35f89392019-03-14 14:51:05 +0800511
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800512 tfm_event_init(&partition->runtime_data.signal_evnt);
513 tfm_list_init(&partition->runtime_data.service_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800514
Mingyang Sunf3d29892019-07-10 17:50:23 +0800515 pth = tfm_spm_partition_get_thread_info(i);
Edison Ai764d41f2018-09-21 15:56:36 +0800516 if (!pth) {
517 tfm_panic();
518 }
519
520 tfm_thrd_init(pth,
Mingyang Sunf3d29892019-07-10 17:50:23 +0800521 tfm_spm_partition_get_init_func(i),
Edison Ai764d41f2018-09-21 15:56:36 +0800522 NULL,
Edison Ai788bae22019-02-18 17:38:59 +0800523 (uint8_t *)tfm_spm_partition_get_stack_top(i),
524 (uint8_t *)tfm_spm_partition_get_stack_bottom(i));
Edison Ai788bae22019-02-18 17:38:59 +0800525
Mingyang Sunf3d29892019-07-10 17:50:23 +0800526 pth->prior = tfm_spm_partition_get_priority(i);
Edison Ai764d41f2018-09-21 15:56:36 +0800527
528 /* Kick off */
529 if (tfm_thrd_start(pth) != THRD_SUCCESS) {
530 tfm_panic();
531 }
532 }
533
534 /* Init Service */
Summer Qind99509f2019-08-02 17:36:58 +0800535 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
Edison Ai764d41f2018-09-21 15:56:36 +0800536 for (i = 0; i < num; i++) {
537 partition =
Summer Qind99509f2019-08-02 17:36:58 +0800538 tfm_spm_get_partition_by_id(service[i].service_db.partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800539 if (!partition) {
540 tfm_panic();
541 }
Summer Qind99509f2019-08-02 17:36:58 +0800542 service[i].partition = partition;
543 tfm_list_init(&service[i].handle_list);
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800544 tfm_list_add_tail(&partition->runtime_data.service_list,
Summer Qind99509f2019-08-02 17:36:58 +0800545 &service[i].list);
Edison Ai764d41f2018-09-21 15:56:36 +0800546 }
547
Ken Liu483f5da2019-04-24 10:45:21 +0800548 /*
549 * All threads initialized, start the scheduler.
550 *
551 * NOTE:
552 * Here is the booting privileged thread mode, and will never
553 * return to this place after scheduler is started. The start
554 * function has to save current runtime context to act as a
555 * 'current thread' to avoid repeating NULL 'current thread'
556 * checking while context switching. This saved context is worthy
557 * of being saved somewhere if there are potential usage purpose.
558 * Let's save this context in a local variable 'this_thrd' at
559 * current since there is no usage for it.
Mate Toth-Palc430b992019-05-09 21:01:14 +0200560 * Also set tfm_nspm_thread_entry as pfn for this thread to
561 * use in detecting NS/S thread scheduling changes.
Ken Liu483f5da2019-04-24 10:45:21 +0800562 */
Mate Toth-Palc430b992019-05-09 21:01:14 +0200563 this_thrd.pfn = (tfm_thrd_func_t)tfm_nspm_thread_entry;
Ken Liu483f5da2019-04-24 10:45:21 +0800564 tfm_thrd_start_scheduler(&this_thrd);
Edison Ai764d41f2018-09-21 15:56:36 +0800565}
Ken Liu2d175172019-03-21 17:08:41 +0800566
567void tfm_pendsv_do_schedule(struct tfm_state_context_ext *ctxb)
568{
569#if TFM_LVL == 2
570 struct spm_partition_desc_t *p_next_partition;
571 uint32_t is_privileged;
572#endif
573 struct tfm_thrd_ctx *pth_next = tfm_thrd_next_thread();
574 struct tfm_thrd_ctx *pth_curr = tfm_thrd_curr_thread();
575
Mate Toth-Pal32b2ccd2019-04-26 10:00:16 +0200576 if (pth_next != NULL && pth_curr != pth_next) {
Ken Liu2d175172019-03-21 17:08:41 +0800577#if TFM_LVL == 2
578 p_next_partition = TFM_GET_CONTAINER_PTR(pth_next,
579 struct spm_partition_desc_t,
580 sp_thrd);
581
582 if (p_next_partition->static_data.partition_flags &
583 SPM_PART_FLAG_PSA_ROT) {
584 is_privileged = TFM_PARTITION_PRIVILEGED_MODE;
585 } else {
586 is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE;
587 }
588
589 tfm_spm_partition_change_privilege(is_privileged);
590#endif
Mate Toth-Palc430b992019-05-09 21:01:14 +0200591 /* Increase the secure lock, if we enter secure from non-secure */
592 if ((void *)pth_curr->pfn == (void *)tfm_nspm_thread_entry) {
593 ++tfm_secure_lock;
594 }
595 /* Decrease the secure lock, if we return from secure to non-secure */
596 if ((void *)pth_next->pfn == (void *)tfm_nspm_thread_entry) {
597 --tfm_secure_lock;
598 }
599
Ken Liu2d175172019-03-21 17:08:41 +0800600 tfm_thrd_context_switch(ctxb, pth_curr, pth_next);
601 }
602}