blob: 340c77855e2d11090e2355224b9332d6c7ba4b79 [file] [log] [blame]
Edison Ai764d41f2018-09-21 15:56:36 +08001/*
Ken Liu5248af22019-12-29 12:47:13 +08002 * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
Edison Ai764d41f2018-09-21 15:56:36 +08003 *
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"
Mingyang Sun94b1b412019-09-20 15:11:14 +080030#include "tfm_core_utils.h"
David Hufb38d562019-09-23 15:58:34 +080031#include "tfm_rpc.h"
Shawn Shan9b0e0c72019-10-22 13:43:07 +080032#include "tfm_irq_list.h"
Edison Ai764d41f2018-09-21 15:56:36 +080033
Summer Qind99509f2019-08-02 17:36:58 +080034#include "secure_fw/services/tfm_service_list.inc"
35
36/* Extern service variable */
37extern struct tfm_spm_service_t service[];
Summer Qine578c5b2019-08-16 16:42:16 +080038extern const struct tfm_spm_service_db_t service_db[];
Summer Qind99509f2019-08-02 17:36:58 +080039
Edison Ai764d41f2018-09-21 15:56:36 +080040/* Extern SPM variable */
41extern struct spm_partition_db_t g_spm_partition_db;
42
43/* Pools */
44TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t),
45 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +080046
Edison Ai764d41f2018-09-21 15:56:36 +080047/********************** SPM functions for handler mode ***********************/
48
49/* Service handle management functions */
Summer Qin1ce712a2019-10-14 18:04:05 +080050psa_handle_t tfm_spm_create_conn_handle(struct tfm_spm_service_t *service,
51 int32_t client_id)
Edison Ai764d41f2018-09-21 15:56:36 +080052{
Edison Ai9cc26242019-08-06 11:28:04 +080053 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +080054
Ken Liuf250b8b2019-12-27 16:31:24 +080055 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +080056
57 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +080058 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
59 if (!p_handle) {
Edison Ai764d41f2018-09-21 15:56:36 +080060 return PSA_NULL_HANDLE;
61 }
62
Edison Ai9cc26242019-08-06 11:28:04 +080063 p_handle->service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +080064 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +080065 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +080066
67 /* Add handle node to list for next psa functions */
Edison Ai9cc26242019-08-06 11:28:04 +080068 tfm_list_add_tail(&service->handle_list, &p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +080069
Edison Ai9cc26242019-08-06 11:28:04 +080070 return (psa_handle_t)p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +080071}
72
Summer Qin1ce712a2019-10-14 18:04:05 +080073int32_t tfm_spm_validate_conn_handle(psa_handle_t conn_handle,
74 int32_t client_id)
75{
76 /* Check the handle address is validated */
77 if (is_valid_chunk_data_in_pool(conn_handle_pool,
78 (uint8_t *)conn_handle) != true) {
79 return IPC_ERROR_GENERIC;
80 }
81
82 /* Check the handle caller is correct */
83 if (((struct tfm_conn_handle_t *)conn_handle)->client_id != client_id) {
84 return IPC_ERROR_GENERIC;
85 }
86
87 return IPC_SUCCESS;
88}
89
Edison Ai764d41f2018-09-21 15:56:36 +080090static struct tfm_conn_handle_t *
Mingyang Sun5e13aa72019-07-10 10:30:16 +080091 tfm_spm_find_conn_handle_node(struct tfm_spm_service_t *service,
92 psa_handle_t conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +080093{
Ken Liuf250b8b2019-12-27 16:31:24 +080094 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +080095
Edison Ai9cc26242019-08-06 11:28:04 +080096 return (struct tfm_conn_handle_t *)conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +080097}
98
99int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service,
100 psa_handle_t conn_handle)
101{
Edison Ai9cc26242019-08-06 11:28:04 +0800102 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800103
Ken Liuf250b8b2019-12-27 16:31:24 +0800104 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800105
106 /* There are many handles for each RoT Service */
Edison Ai9cc26242019-08-06 11:28:04 +0800107 p_handle = tfm_spm_find_conn_handle_node(service, conn_handle);
108 if (!p_handle) {
Edison Ai9059ea02019-11-28 13:46:14 +0800109 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800110 }
111
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200112 /* Clear magic as the handler is not used anymore */
113 p_handle->internal_msg.magic = 0;
114
Edison Ai764d41f2018-09-21 15:56:36 +0800115 /* Remove node from handle list */
Edison Ai9cc26242019-08-06 11:28:04 +0800116 tfm_list_del_node(&p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800117
118 /* Back handle buffer to pool */
Edison Ai9cc26242019-08-06 11:28:04 +0800119 tfm_pool_free(p_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800120 return IPC_SUCCESS;
121}
122
123int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service,
124 psa_handle_t conn_handle,
125 void *rhandle)
126{
Edison Ai9cc26242019-08-06 11:28:04 +0800127 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800128
Ken Liuf250b8b2019-12-27 16:31:24 +0800129 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800130 /* Set reverse handle value only be allowed for a connected handle */
Ken Liuf250b8b2019-12-27 16:31:24 +0800131 TFM_CORE_ASSERT(conn_handle != PSA_NULL_HANDLE);
Edison Ai764d41f2018-09-21 15:56:36 +0800132
133 /* There are many handles for each RoT Service */
Edison Ai9cc26242019-08-06 11:28:04 +0800134 p_handle = tfm_spm_find_conn_handle_node(service, conn_handle);
135 if (!p_handle) {
Edison Ai9059ea02019-11-28 13:46:14 +0800136 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800137 }
138
Edison Ai9cc26242019-08-06 11:28:04 +0800139 p_handle->rhandle = rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800140 return IPC_SUCCESS;
141}
142
143void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service,
144 psa_handle_t conn_handle)
145{
Edison Ai9cc26242019-08-06 11:28:04 +0800146 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800147
Ken Liuf250b8b2019-12-27 16:31:24 +0800148 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800149 /* Get reverse handle value only be allowed for a connected handle */
Ken Liuf250b8b2019-12-27 16:31:24 +0800150 TFM_CORE_ASSERT(conn_handle != PSA_NULL_HANDLE);
Edison Ai764d41f2018-09-21 15:56:36 +0800151
152 /* There are many handles for each RoT Service */
Edison Ai9cc26242019-08-06 11:28:04 +0800153 p_handle = tfm_spm_find_conn_handle_node(service, conn_handle);
154 if (!p_handle) {
Edison Ai9059ea02019-11-28 13:46:14 +0800155 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800156 }
157
Edison Ai9cc26242019-08-06 11:28:04 +0800158 return p_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800159}
160
161/* Partition management functions */
162struct tfm_spm_service_t *
Mingyang Sunf3d29892019-07-10 17:50:23 +0800163 tfm_spm_get_service_by_signal(struct spm_partition_desc_t *partition,
164 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800165{
166 struct tfm_list_node_t *node, *head;
167 struct tfm_spm_service_t *service;
168
Ken Liuf250b8b2019-12-27 16:31:24 +0800169 TFM_CORE_ASSERT(partition);
Edison Ai764d41f2018-09-21 15:56:36 +0800170
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800171 if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
Edison Ai9059ea02019-11-28 13:46:14 +0800172 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800173 }
174
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800175 head = &partition->runtime_data.service_list;
Edison Ai764d41f2018-09-21 15:56:36 +0800176 TFM_LIST_FOR_EACH(node, head) {
177 service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t, list);
Summer Qine578c5b2019-08-16 16:42:16 +0800178 if (service->service_db->signal == signal) {
Edison Ai764d41f2018-09-21 15:56:36 +0800179 return service;
180 }
181 }
182 return NULL;
183}
184
185struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid)
186{
187 uint32_t i;
188 struct tfm_list_node_t *node, *head;
189 struct tfm_spm_service_t *service;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800190 struct spm_partition_desc_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800191
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200192 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800193 partition = &g_spm_partition_db.partitions[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800194 /* Skip partition without IPC flag */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800195 if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
Edison Ai764d41f2018-09-21 15:56:36 +0800196 continue;
197 }
198
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800199 if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
Edison Ai764d41f2018-09-21 15:56:36 +0800200 continue;
201 }
202
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800203 head = &partition->runtime_data.service_list;
Edison Ai764d41f2018-09-21 15:56:36 +0800204 TFM_LIST_FOR_EACH(node, head) {
205 service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t,
206 list);
Summer Qine578c5b2019-08-16 16:42:16 +0800207 if (service->service_db->sid == sid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800208 return service;
209 }
210 }
211 }
212 return NULL;
213}
214
215struct tfm_spm_service_t *
216 tfm_spm_get_service_by_handle(psa_handle_t conn_handle)
217{
Edison Ai9cc26242019-08-06 11:28:04 +0800218 return ((struct tfm_conn_handle_t *)conn_handle)->service;
Edison Ai764d41f2018-09-21 15:56:36 +0800219}
220
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800221struct spm_partition_desc_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800222{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800223 uint32_t idx = get_partition_idx(partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800224
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800225 if (idx != SPM_INVALID_PARTITION_IDX) {
226 return &(g_spm_partition_db.partitions[idx]);
Edison Ai764d41f2018-09-21 15:56:36 +0800227 }
228 return NULL;
229}
230
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800231struct spm_partition_desc_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800232{
233 uint32_t spid;
234
Mingyang Sunf3d29892019-07-10 17:50:23 +0800235 spid = tfm_spm_partition_get_running_partition_id();
Edison Ai764d41f2018-09-21 15:56:36 +0800236
237 return tfm_spm_get_partition_by_id(spid);
238}
239
240int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530241 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800242{
Ken Liuf250b8b2019-12-27 16:31:24 +0800243 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800244
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530245 switch (service->service_db->version_policy) {
Edison Ai764d41f2018-09-21 15:56:36 +0800246 case TFM_VERSION_POLICY_RELAXED:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530247 if (version > service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800248 return IPC_ERROR_VERSION;
249 }
250 break;
251 case TFM_VERSION_POLICY_STRICT:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530252 if (version != service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800253 return IPC_ERROR_VERSION;
254 }
255 break;
256 default:
257 return IPC_ERROR_VERSION;
258 }
259 return IPC_SUCCESS;
260}
261
Edison Aie728fbf2019-11-13 09:37:12 +0800262int32_t tfm_spm_check_authorization(uint32_t sid,
263 struct tfm_spm_service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800264 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800265{
266 struct spm_partition_desc_t *partition = NULL;
267 int32_t i;
268
Ken Liuf250b8b2019-12-27 16:31:24 +0800269 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800270
271 if (ns_caller) {
272 if (!service->service_db->non_secure_client) {
273 return IPC_ERROR_GENERIC;
274 }
275 } else {
276 partition = tfm_spm_get_running_partition();
277 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800278 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800279 }
280
281 for (i = 0; i < partition->static_data->dependencies_num; i++) {
282 if (partition->static_data->p_dependencies[i] == sid) {
283 break;
284 }
285 }
286
287 if (i == partition->static_data->dependencies_num) {
288 return IPC_ERROR_GENERIC;
289 }
290 }
291 return IPC_SUCCESS;
292}
293
Edison Ai764d41f2018-09-21 15:56:36 +0800294/* Message functions */
295struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
296{
297 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200298 * The message handler passed by the caller is considered invalid in the
299 * following cases:
300 * 1. Not a valid message handle. (The address of a message is not the
301 * address of a possible handle from the pool
302 * 2. Handle not belongs to the caller partition (The handle is either
303 * unused, or owned by anither partition)
304 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800305 */
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200306 struct tfm_conn_handle_t *connection_handle_address;
Edison Ai764d41f2018-09-21 15:56:36 +0800307 struct tfm_msg_body_t *msg;
308 uint32_t partition_id;
309
310 msg = (struct tfm_msg_body_t *)msg_handle;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200311
312 connection_handle_address =
313 TFM_GET_CONTAINER_PTR(msg, struct tfm_conn_handle_t, internal_msg);
314
315 if (is_valid_chunk_data_in_pool(
316 conn_handle_pool, (uint8_t *)connection_handle_address) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800317 return NULL;
318 }
319
320 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200321 * Check that the magic number is correct. This proves that the message
322 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800323 */
324 if (msg->magic != TFM_MSG_MAGIC) {
325 return NULL;
326 }
327
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200328 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800329 partition_id = tfm_spm_partition_get_running_partition_id();
Summer Qin423dbef2019-08-22 15:59:35 +0800330 if (partition_id != msg->service->partition->static_data->partition_id) {
Edison Ai764d41f2018-09-21 15:56:36 +0800331 return NULL;
332 }
333
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200334 /*
335 * FixMe: For condition 1 it should be checked whether the message belongs
336 * to the service. Skipping this check isn't a security risk as even if the
337 * message belongs to another service, the handle belongs to the calling
338 * partition.
339 */
340
Edison Ai764d41f2018-09-21 15:56:36 +0800341 return msg;
342}
343
Edison Ai97115822019-08-01 14:22:19 +0800344struct tfm_msg_body_t *
345 tfm_spm_get_msg_buffer_from_conn_handle(psa_handle_t conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800346{
Ken Liuf250b8b2019-12-27 16:31:24 +0800347 TFM_CORE_ASSERT(conn_handle != PSA_NULL_HANDLE);
Edison Ai97115822019-08-01 14:22:19 +0800348
349 return &(((struct tfm_conn_handle_t *)conn_handle)->internal_msg);
350}
351
352void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
353 struct tfm_spm_service_t *service,
354 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800355 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800356 psa_invec *invec, size_t in_len,
357 psa_outvec *outvec, size_t out_len,
358 psa_outvec *caller_outvec)
359{
Edison Ai764d41f2018-09-21 15:56:36 +0800360 uint32_t i;
361
Ken Liuf250b8b2019-12-27 16:31:24 +0800362 TFM_CORE_ASSERT(msg);
363 TFM_CORE_ASSERT(service);
364 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
365 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
366 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
367 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
368 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800369
Edison Ai764d41f2018-09-21 15:56:36 +0800370 /* Clear message buffer before using it */
Mingyang Sun94b1b412019-09-20 15:11:14 +0800371 tfm_core_util_memset(msg, 0, sizeof(struct tfm_msg_body_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800372
Ken Liu35f89392019-03-14 14:51:05 +0800373 tfm_event_init(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800374 msg->magic = TFM_MSG_MAGIC;
375 msg->service = service;
376 msg->handle = handle;
377 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800378 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800379
380 /* Copy contents */
381 msg->msg.type = type;
382
383 for (i = 0; i < in_len; i++) {
384 msg->msg.in_size[i] = invec[i].len;
385 msg->invec[i].base = invec[i].base;
386 }
387
388 for (i = 0; i < out_len; i++) {
389 msg->msg.out_size[i] = outvec[i].len;
390 msg->outvec[i].base = outvec[i].base;
391 /* Out len is used to record the writed number, set 0 here again */
392 msg->outvec[i].len = 0;
393 }
394
395 /* Use message address as handle */
396 msg->msg.handle = (psa_handle_t)msg;
397
398 /* For connected handle, set rhandle to every message */
399 if (handle != PSA_NULL_HANDLE) {
400 msg->msg.rhandle = tfm_spm_get_rhandle(service, handle);
401 }
Edison Ai764d41f2018-09-21 15:56:36 +0800402}
403
404int32_t tfm_spm_send_event(struct tfm_spm_service_t *service,
405 struct tfm_msg_body_t *msg)
406{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800407 struct spm_partition_runtime_data_t *p_runtime_data =
408 &service->partition->runtime_data;
409
Ken Liuf250b8b2019-12-27 16:31:24 +0800410 TFM_CORE_ASSERT(service);
411 TFM_CORE_ASSERT(msg);
Edison Ai764d41f2018-09-21 15:56:36 +0800412
413 /* Enqueue message to service message queue */
414 if (tfm_msg_enqueue(&service->msg_queue, msg) != IPC_SUCCESS) {
415 return IPC_ERROR_GENERIC;
416 }
417
418 /* Messages put. Update signals */
Summer Qine578c5b2019-08-16 16:42:16 +0800419 p_runtime_data->signals |= service->service_db->signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800420
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800421 tfm_event_wake(&p_runtime_data->signal_evnt, (p_runtime_data->signals &
422 p_runtime_data->signal_mask));
Edison Ai764d41f2018-09-21 15:56:36 +0800423
David Hufb38d562019-09-23 15:58:34 +0800424 /*
425 * If it is a NS request via RPC, it is unnecessary to block current
426 * thread.
427 */
428 if (!is_tfm_rpc_msg(msg)) {
429 tfm_event_wait(&msg->ack_evnt);
430 }
Edison Ai764d41f2018-09-21 15:56:36 +0800431
432 return IPC_SUCCESS;
433}
434
Edison Ai7aff9e82019-07-11 14:56:46 +0800435uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
436{
437 return g_spm_partition_db.partitions[partition_idx].
Summer Qin423dbef2019-08-22 15:59:35 +0800438 memory_data->stack_bottom;
Edison Ai7aff9e82019-07-11 14:56:46 +0800439}
440
441uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
442{
Summer Qin423dbef2019-08-22 15:59:35 +0800443 return g_spm_partition_db.partitions[partition_idx].memory_data->stack_top;
Edison Ai7aff9e82019-07-11 14:56:46 +0800444}
445
Mingyang Sunf3d29892019-07-10 17:50:23 +0800446uint32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800447{
Summer Qin66f1e032020-01-06 15:40:03 +0800448 struct tfm_core_thread_t *pth = tfm_core_thrd_get_curr_thread();
Edison Ai764d41f2018-09-21 15:56:36 +0800449 struct spm_partition_desc_t *partition;
Summer Qinb5da9cc2019-08-26 15:19:45 +0800450 struct spm_partition_runtime_data_t *r_data;
Edison Ai764d41f2018-09-21 15:56:36 +0800451
Summer Qinb5da9cc2019-08-26 15:19:45 +0800452 r_data = TFM_GET_CONTAINER_PTR(pth, struct spm_partition_runtime_data_t,
453 sp_thrd);
454 partition = TFM_GET_CONTAINER_PTR(r_data, struct spm_partition_desc_t,
455 runtime_data);
Summer Qin423dbef2019-08-22 15:59:35 +0800456 return partition->static_data->partition_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800457}
458
Summer Qin66f1e032020-01-06 15:40:03 +0800459static struct tfm_core_thread_t *
Mingyang Sunf3d29892019-07-10 17:50:23 +0800460 tfm_spm_partition_get_thread_info(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800461{
Summer Qinb5da9cc2019-08-26 15:19:45 +0800462 return &g_spm_partition_db.partitions[partition_idx].runtime_data.sp_thrd;
Edison Ai764d41f2018-09-21 15:56:36 +0800463}
464
Summer Qin66f1e032020-01-06 15:40:03 +0800465static tfm_core_thrd_entry_t
Mingyang Sunf3d29892019-07-10 17:50:23 +0800466 tfm_spm_partition_get_init_func(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800467{
Summer Qin66f1e032020-01-06 15:40:03 +0800468 return (tfm_core_thrd_entry_t)(g_spm_partition_db.partitions[partition_idx].
Summer Qin423dbef2019-08-22 15:59:35 +0800469 static_data->partition_init);
Edison Ai764d41f2018-09-21 15:56:36 +0800470}
471
Mingyang Sunf3d29892019-07-10 17:50:23 +0800472static uint32_t tfm_spm_partition_get_priority(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800473{
Summer Qin423dbef2019-08-22 15:59:35 +0800474 return g_spm_partition_db.partitions[partition_idx].static_data->
Edison Ai764d41f2018-09-21 15:56:36 +0800475 partition_priority;
476}
477
Summer Qin43c185d2019-10-10 15:44:42 +0800478int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800479 enum tfm_memory_access_e access,
480 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800481{
Hugues de Valon99578562019-06-18 16:08:51 +0100482 enum tfm_status_e err;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800483
484 /* If len is zero, this indicates an empty buffer and base is ignored */
485 if (len == 0) {
486 return IPC_SUCCESS;
487 }
488
489 if (!buffer) {
490 return IPC_ERROR_BAD_PARAMETERS;
491 }
492
493 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
494 return IPC_ERROR_MEMORY_CHECK;
495 }
496
Summer Qin424d4db2019-03-25 14:09:51 +0800497 if (access == TFM_MEMORY_ACCESS_RW) {
Summer Qineb537e52019-03-29 09:57:10 +0800498 err = tfm_core_has_write_access_to_region(buffer, len, ns_caller,
499 privileged);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800500 } else {
Summer Qineb537e52019-03-29 09:57:10 +0800501 err = tfm_core_has_read_access_to_region(buffer, len, ns_caller,
502 privileged);
Summer Qin424d4db2019-03-25 14:09:51 +0800503 }
Summer Qin0fc3f592019-04-11 16:00:10 +0800504 if (err == TFM_SUCCESS) {
Summer Qin424d4db2019-03-25 14:09:51 +0800505 return IPC_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800506 }
507
508 return IPC_ERROR_MEMORY_CHECK;
509}
510
Edison Ai764d41f2018-09-21 15:56:36 +0800511/********************** SPM functions for thread mode ************************/
512
Ken Liuce2692d2020-02-11 12:39:36 +0800513uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800514{
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800515 uint32_t i, j, num;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800516 struct spm_partition_desc_t *partition;
Summer Qin66f1e032020-01-06 15:40:03 +0800517 struct tfm_core_thread_t *pth, *p_ns_entry_thread = NULL;
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100518 const struct tfm_spm_partition_platform_data_t **platform_data_p;
Edison Ai764d41f2018-09-21 15:56:36 +0800519
520 tfm_pool_init(conn_handle_pool,
521 POOL_BUFFER_SIZE(conn_handle_pool),
522 sizeof(struct tfm_conn_handle_t),
523 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800524
525 /* Init partition first for it will be used when init service */
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200526 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800527 partition = &g_spm_partition_db.partitions[i];
Edison Aif0501702019-10-11 14:36:42 +0800528
529 /* Check if the PSA framework version matches. */
530 if (partition->static_data->psa_framework_version !=
531 PSA_FRAMEWORK_VERSION) {
532 ERROR_MSG("Warning: PSA Framework Verison is not matched!");
533 continue;
534 }
535
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100536 platform_data_p = partition->platform_data_list;
537 if (platform_data_p != NULL) {
538 while ((*platform_data_p) != NULL) {
Mate Toth-Pal5e6d0342019-11-22 11:43:20 +0100539 tfm_spm_hal_configure_default_isolation(i, *platform_data_p);
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100540 ++platform_data_p;
541 }
542 }
543
Edison Ai764d41f2018-09-21 15:56:36 +0800544 if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
545 continue;
546 }
Ken Liu35f89392019-03-14 14:51:05 +0800547
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800548 /* Add PSA_DOORBELL signal to assigned_signals */
549 partition->runtime_data.assigned_signals |= PSA_DOORBELL;
550
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800551 /* TODO: This can be optimized by generating the assigned signal
552 * in code generation time.
553 */
554 for (j = 0; j < tfm_core_irq_signals_count; ++j) {
555 if (tfm_core_irq_signals[j].partition_id ==
556 partition->static_data->partition_id) {
557 partition->runtime_data.assigned_signals |=
558 tfm_core_irq_signals[j].signal_value;
559 }
560 }
561
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800562 tfm_event_init(&partition->runtime_data.signal_evnt);
563 tfm_list_init(&partition->runtime_data.service_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800564
Mingyang Sunf3d29892019-07-10 17:50:23 +0800565 pth = tfm_spm_partition_get_thread_info(i);
Edison Ai764d41f2018-09-21 15:56:36 +0800566 if (!pth) {
Edison Ai9059ea02019-11-28 13:46:14 +0800567 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800568 }
569
Summer Qin66f1e032020-01-06 15:40:03 +0800570 tfm_core_thrd_init(pth,
571 tfm_spm_partition_get_init_func(i),
572 NULL,
573 (uintptr_t)tfm_spm_partition_get_stack_top(i),
574 (uintptr_t)tfm_spm_partition_get_stack_bottom(i));
Edison Ai788bae22019-02-18 17:38:59 +0800575
Mingyang Sunf3d29892019-07-10 17:50:23 +0800576 pth->prior = tfm_spm_partition_get_priority(i);
Edison Ai764d41f2018-09-21 15:56:36 +0800577
Ken Liu490281d2019-12-30 15:55:26 +0800578 if (partition->static_data->partition_id == TFM_SP_NON_SECURE_ID) {
579 p_ns_entry_thread = pth;
Ken Liu5248af22019-12-29 12:47:13 +0800580 pth->param = (void *)tfm_spm_hal_get_ns_entry_point();
Ken Liu490281d2019-12-30 15:55:26 +0800581 }
582
Edison Ai764d41f2018-09-21 15:56:36 +0800583 /* Kick off */
Summer Qin66f1e032020-01-06 15:40:03 +0800584 if (tfm_core_thrd_start(pth) != THRD_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800585 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800586 }
587 }
588
589 /* Init Service */
Summer Qind99509f2019-08-02 17:36:58 +0800590 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
Edison Ai764d41f2018-09-21 15:56:36 +0800591 for (i = 0; i < num; i++) {
Summer Qine578c5b2019-08-16 16:42:16 +0800592 service[i].service_db = &service_db[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800593 partition =
Summer Qine578c5b2019-08-16 16:42:16 +0800594 tfm_spm_get_partition_by_id(service[i].service_db->partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800595 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800596 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800597 }
Summer Qind99509f2019-08-02 17:36:58 +0800598 service[i].partition = partition;
Jaykumar Pitambarbhai Patel0c7a0382020-01-09 15:25:58 +0530599 partition->runtime_data.assigned_signals |= service[i].service_db->signal;
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800600
Summer Qind99509f2019-08-02 17:36:58 +0800601 tfm_list_init(&service[i].handle_list);
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800602 tfm_list_add_tail(&partition->runtime_data.service_list,
Summer Qind99509f2019-08-02 17:36:58 +0800603 &service[i].list);
Edison Ai764d41f2018-09-21 15:56:36 +0800604 }
605
Ken Liu483f5da2019-04-24 10:45:21 +0800606 /*
607 * All threads initialized, start the scheduler.
608 *
609 * NOTE:
Ken Liu490281d2019-12-30 15:55:26 +0800610 * It is worthy to give the thread object to scheduler if the background
611 * context belongs to one of the threads. Here the background thread is the
612 * initialization thread who calls SPM SVC, which re-uses the non-secure
613 * entry thread's stack. After SPM initialization is done, this stack is
614 * cleaned up and the background context is never going to return. Tell
615 * the scheduler that the current thread is non-secure entry thread.
Ken Liu483f5da2019-04-24 10:45:21 +0800616 */
Summer Qin66f1e032020-01-06 15:40:03 +0800617 tfm_core_thrd_start_scheduler(p_ns_entry_thread);
Ken Liuce2692d2020-02-11 12:39:36 +0800618
Summer Qind2ad7e72020-01-06 18:16:35 +0800619 return p_ns_entry_thread->arch_ctx.lr;
Edison Ai764d41f2018-09-21 15:56:36 +0800620}
Ken Liu2d175172019-03-21 17:08:41 +0800621
Summer Qind2ad7e72020-01-06 18:16:35 +0800622void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx)
Ken Liu2d175172019-03-21 17:08:41 +0800623{
624#if TFM_LVL == 2
625 struct spm_partition_desc_t *p_next_partition;
Summer Qinb5da9cc2019-08-26 15:19:45 +0800626 struct spm_partition_runtime_data_t *r_data;
Ken Liu2d175172019-03-21 17:08:41 +0800627 uint32_t is_privileged;
628#endif
Summer Qin66f1e032020-01-06 15:40:03 +0800629 struct tfm_core_thread_t *pth_next = tfm_core_thrd_get_next_thread();
630 struct tfm_core_thread_t *pth_curr = tfm_core_thrd_get_curr_thread();
Ken Liu2d175172019-03-21 17:08:41 +0800631
Mate Toth-Pal32b2ccd2019-04-26 10:00:16 +0200632 if (pth_next != NULL && pth_curr != pth_next) {
Ken Liu2d175172019-03-21 17:08:41 +0800633#if TFM_LVL == 2
Summer Qinb5da9cc2019-08-26 15:19:45 +0800634 r_data = TFM_GET_CONTAINER_PTR(pth_next,
635 struct spm_partition_runtime_data_t,
636 sp_thrd);
637 p_next_partition = TFM_GET_CONTAINER_PTR(r_data,
Ken Liu2d175172019-03-21 17:08:41 +0800638 struct spm_partition_desc_t,
Summer Qinb5da9cc2019-08-26 15:19:45 +0800639 runtime_data);
Ken Liu2d175172019-03-21 17:08:41 +0800640
Summer Qin423dbef2019-08-22 15:59:35 +0800641 if (p_next_partition->static_data->partition_flags &
Ken Liu2d175172019-03-21 17:08:41 +0800642 SPM_PART_FLAG_PSA_ROT) {
643 is_privileged = TFM_PARTITION_PRIVILEGED_MODE;
644 } else {
645 is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE;
646 }
647
648 tfm_spm_partition_change_privilege(is_privileged);
649#endif
Mate Toth-Palc430b992019-05-09 21:01:14 +0200650
Summer Qind2ad7e72020-01-06 18:16:35 +0800651 tfm_core_thrd_switch_context(p_actx, pth_curr, pth_next);
Ken Liu2d175172019-03-21 17:08:41 +0800652 }
David Hufb38d562019-09-23 15:58:34 +0800653
654 /*
655 * Handle pending mailbox message from NS in multi-core topology.
656 * Empty operation on single Armv8-M platform.
657 */
658 tfm_rpc_client_call_handler();
Ken Liu2d175172019-03-21 17:08:41 +0800659}