blob: a8b6c14c2b0e3474aa7eadc4963b2d25dfe132ed [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"
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
Mate Toth-Palc430b992019-05-09 21:01:14 +020043/* Extern secure lock variable */
44extern int32_t tfm_secure_lock;
Mingyang Sunf3d29892019-07-10 17:50:23 +080045
Edison Ai764d41f2018-09-21 15:56:36 +080046/* Pools */
47TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t),
48 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +080049
Edison Ai764d41f2018-09-21 15:56:36 +080050/********************** SPM functions for handler mode ***********************/
51
52/* Service handle management functions */
Summer Qin1ce712a2019-10-14 18:04:05 +080053psa_handle_t tfm_spm_create_conn_handle(struct tfm_spm_service_t *service,
54 int32_t client_id)
Edison Ai764d41f2018-09-21 15:56:36 +080055{
Edison Ai9cc26242019-08-06 11:28:04 +080056 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +080057
58 TFM_ASSERT(service);
59
60 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +080061 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
62 if (!p_handle) {
Edison Ai764d41f2018-09-21 15:56:36 +080063 return PSA_NULL_HANDLE;
64 }
65
Edison Ai9cc26242019-08-06 11:28:04 +080066 p_handle->service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +080067 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +080068 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +080069
70 /* Add handle node to list for next psa functions */
Edison Ai9cc26242019-08-06 11:28:04 +080071 tfm_list_add_tail(&service->handle_list, &p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +080072
Edison Ai9cc26242019-08-06 11:28:04 +080073 return (psa_handle_t)p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +080074}
75
Summer Qin1ce712a2019-10-14 18:04:05 +080076int32_t tfm_spm_validate_conn_handle(psa_handle_t conn_handle,
77 int32_t client_id)
78{
79 /* Check the handle address is validated */
80 if (is_valid_chunk_data_in_pool(conn_handle_pool,
81 (uint8_t *)conn_handle) != true) {
82 return IPC_ERROR_GENERIC;
83 }
84
85 /* Check the handle caller is correct */
86 if (((struct tfm_conn_handle_t *)conn_handle)->client_id != client_id) {
87 return IPC_ERROR_GENERIC;
88 }
89
90 return IPC_SUCCESS;
91}
92
Edison Ai764d41f2018-09-21 15:56:36 +080093static struct tfm_conn_handle_t *
Mingyang Sun5e13aa72019-07-10 10:30:16 +080094 tfm_spm_find_conn_handle_node(struct tfm_spm_service_t *service,
95 psa_handle_t conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +080096{
Edison Ai764d41f2018-09-21 15:56:36 +080097 TFM_ASSERT(service);
98
Edison Ai9cc26242019-08-06 11:28:04 +080099 return (struct tfm_conn_handle_t *)conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800100}
101
102int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service,
103 psa_handle_t conn_handle)
104{
Edison Ai9cc26242019-08-06 11:28:04 +0800105 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800106
107 TFM_ASSERT(service);
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
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200115 /* Clear magic as the handler is not used anymore */
116 p_handle->internal_msg.magic = 0;
117
Edison Ai764d41f2018-09-21 15:56:36 +0800118 /* Remove node from handle list */
Edison Ai9cc26242019-08-06 11:28:04 +0800119 tfm_list_del_node(&p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800120
121 /* Back handle buffer to pool */
Edison Ai9cc26242019-08-06 11:28:04 +0800122 tfm_pool_free(p_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800123 return IPC_SUCCESS;
124}
125
126int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service,
127 psa_handle_t conn_handle,
128 void *rhandle)
129{
Edison Ai9cc26242019-08-06 11:28:04 +0800130 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800131
132 TFM_ASSERT(service);
133 /* Set reverse handle value only be allowed for a connected handle */
134 TFM_ASSERT(conn_handle != PSA_NULL_HANDLE);
135
136 /* There are many handles for each RoT Service */
Edison Ai9cc26242019-08-06 11:28:04 +0800137 p_handle = tfm_spm_find_conn_handle_node(service, conn_handle);
138 if (!p_handle) {
Edison Ai764d41f2018-09-21 15:56:36 +0800139 tfm_panic();
140 }
141
Edison Ai9cc26242019-08-06 11:28:04 +0800142 p_handle->rhandle = rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800143 return IPC_SUCCESS;
144}
145
146void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service,
147 psa_handle_t conn_handle)
148{
Edison Ai9cc26242019-08-06 11:28:04 +0800149 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800150
151 TFM_ASSERT(service);
152 /* Get reverse handle value only be allowed for a connected handle */
153 TFM_ASSERT(conn_handle != PSA_NULL_HANDLE);
154
155 /* There are many handles for each RoT Service */
Edison Ai9cc26242019-08-06 11:28:04 +0800156 p_handle = tfm_spm_find_conn_handle_node(service, conn_handle);
157 if (!p_handle) {
Edison Ai764d41f2018-09-21 15:56:36 +0800158 tfm_panic();
159 }
160
Edison Ai9cc26242019-08-06 11:28:04 +0800161 return p_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800162}
163
164/* Partition management functions */
165struct tfm_spm_service_t *
Mingyang Sunf3d29892019-07-10 17:50:23 +0800166 tfm_spm_get_service_by_signal(struct spm_partition_desc_t *partition,
167 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800168{
169 struct tfm_list_node_t *node, *head;
170 struct tfm_spm_service_t *service;
171
172 TFM_ASSERT(partition);
173
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800174 if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
Edison Ai764d41f2018-09-21 15:56:36 +0800175 tfm_panic();
176 }
177
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800178 head = &partition->runtime_data.service_list;
Edison Ai764d41f2018-09-21 15:56:36 +0800179 TFM_LIST_FOR_EACH(node, head) {
180 service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t, list);
Summer Qine578c5b2019-08-16 16:42:16 +0800181 if (service->service_db->signal == signal) {
Edison Ai764d41f2018-09-21 15:56:36 +0800182 return service;
183 }
184 }
185 return NULL;
186}
187
188struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid)
189{
190 uint32_t i;
191 struct tfm_list_node_t *node, *head;
192 struct tfm_spm_service_t *service;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800193 struct spm_partition_desc_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800194
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200195 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800196 partition = &g_spm_partition_db.partitions[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800197 /* Skip partition without IPC flag */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800198 if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
Edison Ai764d41f2018-09-21 15:56:36 +0800199 continue;
200 }
201
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800202 if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
Edison Ai764d41f2018-09-21 15:56:36 +0800203 continue;
204 }
205
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800206 head = &partition->runtime_data.service_list;
Edison Ai764d41f2018-09-21 15:56:36 +0800207 TFM_LIST_FOR_EACH(node, head) {
208 service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t,
209 list);
Summer Qine578c5b2019-08-16 16:42:16 +0800210 if (service->service_db->sid == sid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800211 return service;
212 }
213 }
214 }
215 return NULL;
216}
217
218struct tfm_spm_service_t *
219 tfm_spm_get_service_by_handle(psa_handle_t conn_handle)
220{
Edison Ai9cc26242019-08-06 11:28:04 +0800221 return ((struct tfm_conn_handle_t *)conn_handle)->service;
Edison Ai764d41f2018-09-21 15:56:36 +0800222}
223
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800224struct spm_partition_desc_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800225{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800226 uint32_t idx = get_partition_idx(partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800227
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800228 if (idx != SPM_INVALID_PARTITION_IDX) {
229 return &(g_spm_partition_db.partitions[idx]);
Edison Ai764d41f2018-09-21 15:56:36 +0800230 }
231 return NULL;
232}
233
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800234struct spm_partition_desc_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800235{
236 uint32_t spid;
237
Mingyang Sunf3d29892019-07-10 17:50:23 +0800238 spid = tfm_spm_partition_get_running_partition_id();
Edison Ai764d41f2018-09-21 15:56:36 +0800239
240 return tfm_spm_get_partition_by_id(spid);
241}
242
243int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530244 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800245{
246 TFM_ASSERT(service);
247
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530248 switch (service->service_db->version_policy) {
Edison Ai764d41f2018-09-21 15:56:36 +0800249 case TFM_VERSION_POLICY_RELAXED:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530250 if (version > service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800251 return IPC_ERROR_VERSION;
252 }
253 break;
254 case TFM_VERSION_POLICY_STRICT:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530255 if (version != service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800256 return IPC_ERROR_VERSION;
257 }
258 break;
259 default:
260 return IPC_ERROR_VERSION;
261 }
262 return IPC_SUCCESS;
263}
264
Edison Aie728fbf2019-11-13 09:37:12 +0800265int32_t tfm_spm_check_authorization(uint32_t sid,
266 struct tfm_spm_service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800267 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800268{
269 struct spm_partition_desc_t *partition = NULL;
270 int32_t i;
271
272 TFM_ASSERT(service);
273
274 if (ns_caller) {
275 if (!service->service_db->non_secure_client) {
276 return IPC_ERROR_GENERIC;
277 }
278 } else {
279 partition = tfm_spm_get_running_partition();
280 if (!partition) {
281 tfm_panic();
282 }
283
284 for (i = 0; i < partition->static_data->dependencies_num; i++) {
285 if (partition->static_data->p_dependencies[i] == sid) {
286 break;
287 }
288 }
289
290 if (i == partition->static_data->dependencies_num) {
291 return IPC_ERROR_GENERIC;
292 }
293 }
294 return IPC_SUCCESS;
295}
296
Edison Ai764d41f2018-09-21 15:56:36 +0800297/* Message functions */
298struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
299{
300 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200301 * The message handler passed by the caller is considered invalid in the
302 * following cases:
303 * 1. Not a valid message handle. (The address of a message is not the
304 * address of a possible handle from the pool
305 * 2. Handle not belongs to the caller partition (The handle is either
306 * unused, or owned by anither partition)
307 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800308 */
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200309 struct tfm_conn_handle_t *connection_handle_address;
Edison Ai764d41f2018-09-21 15:56:36 +0800310 struct tfm_msg_body_t *msg;
311 uint32_t partition_id;
312
313 msg = (struct tfm_msg_body_t *)msg_handle;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200314
315 connection_handle_address =
316 TFM_GET_CONTAINER_PTR(msg, struct tfm_conn_handle_t, internal_msg);
317
318 if (is_valid_chunk_data_in_pool(
319 conn_handle_pool, (uint8_t *)connection_handle_address) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800320 return NULL;
321 }
322
323 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200324 * Check that the magic number is correct. This proves that the message
325 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800326 */
327 if (msg->magic != TFM_MSG_MAGIC) {
328 return NULL;
329 }
330
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200331 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800332 partition_id = tfm_spm_partition_get_running_partition_id();
Summer Qin423dbef2019-08-22 15:59:35 +0800333 if (partition_id != msg->service->partition->static_data->partition_id) {
Edison Ai764d41f2018-09-21 15:56:36 +0800334 return NULL;
335 }
336
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200337 /*
338 * FixMe: For condition 1 it should be checked whether the message belongs
339 * to the service. Skipping this check isn't a security risk as even if the
340 * message belongs to another service, the handle belongs to the calling
341 * partition.
342 */
343
Edison Ai764d41f2018-09-21 15:56:36 +0800344 return msg;
345}
346
Edison Ai97115822019-08-01 14:22:19 +0800347struct tfm_msg_body_t *
348 tfm_spm_get_msg_buffer_from_conn_handle(psa_handle_t conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800349{
Edison Ai97115822019-08-01 14:22:19 +0800350 TFM_ASSERT(conn_handle != PSA_NULL_HANDLE);
351
352 return &(((struct tfm_conn_handle_t *)conn_handle)->internal_msg);
353}
354
355void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
356 struct tfm_spm_service_t *service,
357 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800358 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800359 psa_invec *invec, size_t in_len,
360 psa_outvec *outvec, size_t out_len,
361 psa_outvec *caller_outvec)
362{
Edison Ai764d41f2018-09-21 15:56:36 +0800363 uint32_t i;
364
Edison Ai97115822019-08-01 14:22:19 +0800365 TFM_ASSERT(msg);
Edison Ai764d41f2018-09-21 15:56:36 +0800366 TFM_ASSERT(service);
367 TFM_ASSERT(!(invec == NULL && in_len != 0));
368 TFM_ASSERT(!(outvec == NULL && out_len != 0));
369 TFM_ASSERT(in_len <= PSA_MAX_IOVEC);
370 TFM_ASSERT(out_len <= PSA_MAX_IOVEC);
371 TFM_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
372
Edison Ai764d41f2018-09-21 15:56:36 +0800373 /* Clear message buffer before using it */
Mingyang Sun94b1b412019-09-20 15:11:14 +0800374 tfm_core_util_memset(msg, 0, sizeof(struct tfm_msg_body_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800375
Ken Liu35f89392019-03-14 14:51:05 +0800376 tfm_event_init(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800377 msg->magic = TFM_MSG_MAGIC;
378 msg->service = service;
379 msg->handle = handle;
380 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800381 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800382
383 /* Copy contents */
384 msg->msg.type = type;
385
386 for (i = 0; i < in_len; i++) {
387 msg->msg.in_size[i] = invec[i].len;
388 msg->invec[i].base = invec[i].base;
389 }
390
391 for (i = 0; i < out_len; i++) {
392 msg->msg.out_size[i] = outvec[i].len;
393 msg->outvec[i].base = outvec[i].base;
394 /* Out len is used to record the writed number, set 0 here again */
395 msg->outvec[i].len = 0;
396 }
397
398 /* Use message address as handle */
399 msg->msg.handle = (psa_handle_t)msg;
400
401 /* For connected handle, set rhandle to every message */
402 if (handle != PSA_NULL_HANDLE) {
403 msg->msg.rhandle = tfm_spm_get_rhandle(service, handle);
404 }
Edison Ai764d41f2018-09-21 15:56:36 +0800405}
406
407int32_t tfm_spm_send_event(struct tfm_spm_service_t *service,
408 struct tfm_msg_body_t *msg)
409{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800410 struct spm_partition_runtime_data_t *p_runtime_data =
411 &service->partition->runtime_data;
412
Edison Ai764d41f2018-09-21 15:56:36 +0800413 TFM_ASSERT(service);
414 TFM_ASSERT(msg);
415
416 /* Enqueue message to service message queue */
417 if (tfm_msg_enqueue(&service->msg_queue, msg) != IPC_SUCCESS) {
418 return IPC_ERROR_GENERIC;
419 }
420
421 /* Messages put. Update signals */
Summer Qine578c5b2019-08-16 16:42:16 +0800422 p_runtime_data->signals |= service->service_db->signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800423
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800424 tfm_event_wake(&p_runtime_data->signal_evnt, (p_runtime_data->signals &
425 p_runtime_data->signal_mask));
Edison Ai764d41f2018-09-21 15:56:36 +0800426
David Hufb38d562019-09-23 15:58:34 +0800427 /*
428 * If it is a NS request via RPC, it is unnecessary to block current
429 * thread.
430 */
431 if (!is_tfm_rpc_msg(msg)) {
432 tfm_event_wait(&msg->ack_evnt);
433 }
Edison Ai764d41f2018-09-21 15:56:36 +0800434
435 return IPC_SUCCESS;
436}
437
Edison Ai7aff9e82019-07-11 14:56:46 +0800438uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
439{
440 return g_spm_partition_db.partitions[partition_idx].
Summer Qin423dbef2019-08-22 15:59:35 +0800441 memory_data->stack_bottom;
Edison Ai7aff9e82019-07-11 14:56:46 +0800442}
443
444uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
445{
Summer Qin423dbef2019-08-22 15:59:35 +0800446 return g_spm_partition_db.partitions[partition_idx].memory_data->stack_top;
Edison Ai7aff9e82019-07-11 14:56:46 +0800447}
448
Mingyang Sunf3d29892019-07-10 17:50:23 +0800449uint32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800450{
451 struct tfm_thrd_ctx *pth = tfm_thrd_curr_thread();
452 struct spm_partition_desc_t *partition;
Summer Qinb5da9cc2019-08-26 15:19:45 +0800453 struct spm_partition_runtime_data_t *r_data;
Edison Ai764d41f2018-09-21 15:56:36 +0800454
Summer Qinb5da9cc2019-08-26 15:19:45 +0800455 r_data = TFM_GET_CONTAINER_PTR(pth, struct spm_partition_runtime_data_t,
456 sp_thrd);
457 partition = TFM_GET_CONTAINER_PTR(r_data, struct spm_partition_desc_t,
458 runtime_data);
Summer Qin423dbef2019-08-22 15:59:35 +0800459 return partition->static_data->partition_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800460}
461
462static struct tfm_thrd_ctx *
Mingyang Sunf3d29892019-07-10 17:50:23 +0800463 tfm_spm_partition_get_thread_info(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800464{
Summer Qinb5da9cc2019-08-26 15:19:45 +0800465 return &g_spm_partition_db.partitions[partition_idx].runtime_data.sp_thrd;
Edison Ai764d41f2018-09-21 15:56:36 +0800466}
467
Edison Ai764d41f2018-09-21 15:56:36 +0800468static tfm_thrd_func_t
Mingyang Sunf3d29892019-07-10 17:50:23 +0800469 tfm_spm_partition_get_init_func(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800470{
471 return (tfm_thrd_func_t)(g_spm_partition_db.partitions[partition_idx].
Summer Qin423dbef2019-08-22 15:59:35 +0800472 static_data->partition_init);
Edison Ai764d41f2018-09-21 15:56:36 +0800473}
474
Mingyang Sunf3d29892019-07-10 17:50:23 +0800475static uint32_t tfm_spm_partition_get_priority(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800476{
Summer Qin423dbef2019-08-22 15:59:35 +0800477 return g_spm_partition_db.partitions[partition_idx].static_data->
Edison Ai764d41f2018-09-21 15:56:36 +0800478 partition_priority;
479}
480
Summer Qin43c185d2019-10-10 15:44:42 +0800481int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800482 enum tfm_memory_access_e access,
483 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800484{
Hugues de Valon99578562019-06-18 16:08:51 +0100485 enum tfm_status_e err;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800486
487 /* If len is zero, this indicates an empty buffer and base is ignored */
488 if (len == 0) {
489 return IPC_SUCCESS;
490 }
491
492 if (!buffer) {
493 return IPC_ERROR_BAD_PARAMETERS;
494 }
495
496 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
497 return IPC_ERROR_MEMORY_CHECK;
498 }
499
Summer Qin424d4db2019-03-25 14:09:51 +0800500 if (access == TFM_MEMORY_ACCESS_RW) {
Summer Qineb537e52019-03-29 09:57:10 +0800501 err = tfm_core_has_write_access_to_region(buffer, len, ns_caller,
502 privileged);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800503 } else {
Summer Qineb537e52019-03-29 09:57:10 +0800504 err = tfm_core_has_read_access_to_region(buffer, len, ns_caller,
505 privileged);
Summer Qin424d4db2019-03-25 14:09:51 +0800506 }
Summer Qin0fc3f592019-04-11 16:00:10 +0800507 if (err == TFM_SUCCESS) {
Summer Qin424d4db2019-03-25 14:09:51 +0800508 return IPC_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800509 }
510
511 return IPC_ERROR_MEMORY_CHECK;
512}
513
Summer Qin75f0d752019-08-27 14:38:20 +0800514uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
Summer Qineb537e52019-03-29 09:57:10 +0800515{
Summer Qin75f0d752019-08-27 14:38:20 +0800516 if (partition_flags & SPM_PART_FLAG_PSA_ROT) {
Summer Qineb537e52019-03-29 09:57:10 +0800517 return TFM_PARTITION_PRIVILEGED_MODE;
518 } else {
519 return TFM_PARTITION_UNPRIVILEGED_MODE;
520 }
521}
522
Edison Ai764d41f2018-09-21 15:56:36 +0800523/********************** SPM functions for thread mode ************************/
524
525void tfm_spm_init(void)
526{
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800527 uint32_t i, j, num;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800528 struct spm_partition_desc_t *partition;
Ken Liu483f5da2019-04-24 10:45:21 +0800529 struct tfm_thrd_ctx *pth, this_thrd;
Edison Ai764d41f2018-09-21 15:56:36 +0800530
531 tfm_pool_init(conn_handle_pool,
532 POOL_BUFFER_SIZE(conn_handle_pool),
533 sizeof(struct tfm_conn_handle_t),
534 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800535
536 /* Init partition first for it will be used when init service */
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200537 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800538 partition = &g_spm_partition_db.partitions[i];
Edison Aif0501702019-10-11 14:36:42 +0800539
540 /* Check if the PSA framework version matches. */
541 if (partition->static_data->psa_framework_version !=
542 PSA_FRAMEWORK_VERSION) {
543 ERROR_MSG("Warning: PSA Framework Verison is not matched!");
544 continue;
545 }
546
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800547 tfm_spm_hal_configure_default_isolation(partition->platform_data);
Edison Ai764d41f2018-09-21 15:56:36 +0800548 if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
549 continue;
550 }
Ken Liu35f89392019-03-14 14:51:05 +0800551
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800552 /* TODO: This can be optimized by generating the assigned signal
553 * in code generation time.
554 */
555 for (j = 0; j < tfm_core_irq_signals_count; ++j) {
556 if (tfm_core_irq_signals[j].partition_id ==
557 partition->static_data->partition_id) {
558 partition->runtime_data.assigned_signals |=
559 tfm_core_irq_signals[j].signal_value;
560 }
561 }
562
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800563 tfm_event_init(&partition->runtime_data.signal_evnt);
564 tfm_list_init(&partition->runtime_data.service_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800565
Mingyang Sunf3d29892019-07-10 17:50:23 +0800566 pth = tfm_spm_partition_get_thread_info(i);
Edison Ai764d41f2018-09-21 15:56:36 +0800567 if (!pth) {
568 tfm_panic();
569 }
570
571 tfm_thrd_init(pth,
Mingyang Sunf3d29892019-07-10 17:50:23 +0800572 tfm_spm_partition_get_init_func(i),
Edison Ai764d41f2018-09-21 15:56:36 +0800573 NULL,
Ken Liu5a2b9052019-08-15 19:03:29 +0800574 (uintptr_t)tfm_spm_partition_get_stack_top(i),
575 (uintptr_t)tfm_spm_partition_get_stack_bottom(i));
Edison Ai788bae22019-02-18 17:38:59 +0800576
Mingyang Sunf3d29892019-07-10 17:50:23 +0800577 pth->prior = tfm_spm_partition_get_priority(i);
Edison Ai764d41f2018-09-21 15:56:36 +0800578
579 /* Kick off */
580 if (tfm_thrd_start(pth) != THRD_SUCCESS) {
581 tfm_panic();
582 }
583 }
584
585 /* Init Service */
Summer Qind99509f2019-08-02 17:36:58 +0800586 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
Edison Ai764d41f2018-09-21 15:56:36 +0800587 for (i = 0; i < num; i++) {
Summer Qine578c5b2019-08-16 16:42:16 +0800588 service[i].service_db = &service_db[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800589 partition =
Summer Qine578c5b2019-08-16 16:42:16 +0800590 tfm_spm_get_partition_by_id(service[i].service_db->partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800591 if (!partition) {
592 tfm_panic();
593 }
Summer Qind99509f2019-08-02 17:36:58 +0800594 service[i].partition = partition;
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800595 partition->runtime_data.assigned_signals |= service->service_db->signal;
596
Summer Qind99509f2019-08-02 17:36:58 +0800597 tfm_list_init(&service[i].handle_list);
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800598 tfm_list_add_tail(&partition->runtime_data.service_list,
Summer Qind99509f2019-08-02 17:36:58 +0800599 &service[i].list);
Edison Ai764d41f2018-09-21 15:56:36 +0800600 }
601
Ken Liu483f5da2019-04-24 10:45:21 +0800602 /*
603 * All threads initialized, start the scheduler.
604 *
605 * NOTE:
606 * Here is the booting privileged thread mode, and will never
607 * return to this place after scheduler is started. The start
608 * function has to save current runtime context to act as a
609 * 'current thread' to avoid repeating NULL 'current thread'
610 * checking while context switching. This saved context is worthy
611 * of being saved somewhere if there are potential usage purpose.
612 * Let's save this context in a local variable 'this_thrd' at
613 * current since there is no usage for it.
Mate Toth-Palc430b992019-05-09 21:01:14 +0200614 * Also set tfm_nspm_thread_entry as pfn for this thread to
615 * use in detecting NS/S thread scheduling changes.
Ken Liu483f5da2019-04-24 10:45:21 +0800616 */
Mate Toth-Palc430b992019-05-09 21:01:14 +0200617 this_thrd.pfn = (tfm_thrd_func_t)tfm_nspm_thread_entry;
Ken Liu483f5da2019-04-24 10:45:21 +0800618 tfm_thrd_start_scheduler(&this_thrd);
Edison Ai764d41f2018-09-21 15:56:36 +0800619}
Ken Liu2d175172019-03-21 17:08:41 +0800620
621void tfm_pendsv_do_schedule(struct tfm_state_context_ext *ctxb)
622{
623#if TFM_LVL == 2
624 struct spm_partition_desc_t *p_next_partition;
Summer Qinb5da9cc2019-08-26 15:19:45 +0800625 struct spm_partition_runtime_data_t *r_data;
Ken Liu2d175172019-03-21 17:08:41 +0800626 uint32_t is_privileged;
627#endif
628 struct tfm_thrd_ctx *pth_next = tfm_thrd_next_thread();
629 struct tfm_thrd_ctx *pth_curr = tfm_thrd_curr_thread();
630
Mate Toth-Pal32b2ccd2019-04-26 10:00:16 +0200631 if (pth_next != NULL && pth_curr != pth_next) {
Ken Liu2d175172019-03-21 17:08:41 +0800632#if TFM_LVL == 2
Summer Qinb5da9cc2019-08-26 15:19:45 +0800633 r_data = TFM_GET_CONTAINER_PTR(pth_next,
634 struct spm_partition_runtime_data_t,
635 sp_thrd);
636 p_next_partition = TFM_GET_CONTAINER_PTR(r_data,
Ken Liu2d175172019-03-21 17:08:41 +0800637 struct spm_partition_desc_t,
Summer Qinb5da9cc2019-08-26 15:19:45 +0800638 runtime_data);
Ken Liu2d175172019-03-21 17:08:41 +0800639
Summer Qin423dbef2019-08-22 15:59:35 +0800640 if (p_next_partition->static_data->partition_flags &
Ken Liu2d175172019-03-21 17:08:41 +0800641 SPM_PART_FLAG_PSA_ROT) {
642 is_privileged = TFM_PARTITION_PRIVILEGED_MODE;
643 } else {
644 is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE;
645 }
646
647 tfm_spm_partition_change_privilege(is_privileged);
648#endif
Mate Toth-Palc430b992019-05-09 21:01:14 +0200649 /* Increase the secure lock, if we enter secure from non-secure */
650 if ((void *)pth_curr->pfn == (void *)tfm_nspm_thread_entry) {
651 ++tfm_secure_lock;
652 }
653 /* Decrease the secure lock, if we return from secure to non-secure */
654 if ((void *)pth_next->pfn == (void *)tfm_nspm_thread_entry) {
655 --tfm_secure_lock;
656 }
657
Ken Liu2d175172019-03-21 17:08:41 +0800658 tfm_thrd_context_switch(ctxb, pth_curr, pth_next);
659 }
David Hufb38d562019-09-23 15:58:34 +0800660
661 /*
662 * Handle pending mailbox message from NS in multi-core topology.
663 * Empty operation on single Armv8-M platform.
664 */
665 tfm_rpc_client_call_handler();
Ken Liu2d175172019-03-21 17:08:41 +0800666}