blob: 1e547efec5142504e691c338aceb0a0274b2b1df [file] [log] [blame]
Edison Ai764d41f2018-09-21 15:56:36 +08001/*
Kevin Penga20b5af2021-01-11 11:20:52 +08002 * Copyright (c) 2018-2021, 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
Edison Ai764d41f2018-09-21 15:56:36 +08008#include <inttypes.h>
9#include <stdbool.h>
Ken Liu24dffb22021-02-10 11:03:58 +080010#include "bitops.h"
David Huf07e97d2021-02-15 22:05:40 +080011#include "fih.h"
Jamie Foxcc31d402019-01-28 17:13:52 +000012#include "psa/client.h"
13#include "psa/service.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080014#include "tfm_thread.h"
Edison Ai764d41f2018-09-21 15:56:36 +080015#include "tfm_wait.h"
Ken Liubcae38b2021-01-20 15:47:44 +080016#include "internal_errors.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080017#include "tfm_spm_hal.h"
18#include "tfm_irq_list.h"
19#include "tfm_api.h"
20#include "tfm_secure_api.h"
21#include "tfm_memory_utils.h"
Mingyang Sund1ed6732020-08-26 15:52:21 +080022#include "tfm_hal_defs.h"
23#include "tfm_hal_isolation.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080024#include "spm_ipc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080025#include "tfm_peripherals_def.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080026#include "tfm_core_utils.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080027#include "tfm_rpc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080028#include "tfm_core_trustzone.h"
Ken Liu24dffb22021-02-10 11:03:58 +080029#include "lists.h"
Edison Ai764d41f2018-09-21 15:56:36 +080030#include "tfm_pools.h"
Mingyang Sunbd7ceb52020-06-11 16:53:03 +080031#include "region.h"
Summer Qin2bfd2a02018-09-26 17:10:41 +080032#include "region_defs.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080033#include "spm_partition_defs.h"
34#include "psa_manifest/pid.h"
Summer Qin5fdcf632020-06-22 16:49:24 +080035#include "tfm/tfm_spm_services.h"
Mingyang Sun00df2352021-04-15 15:46:08 +080036#include "load/partition_defs.h"
37#include "load/service_defs.h"
Mingyang Sun2b352662021-04-21 11:35:43 +080038#include "load/partition_static_load.h"
Edison Ai764d41f2018-09-21 15:56:36 +080039
Ken Liu1f345b02020-05-30 21:11:05 +080040#include "secure_fw/partitions/tfm_service_list.inc"
Mingyang Sunbd7ceb52020-06-11 16:53:03 +080041#include "tfm_spm_db_ipc.inc"
Summer Qind99509f2019-08-02 17:36:58 +080042
Edison Ai764d41f2018-09-21 15:56:36 +080043/* 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
Kevin Pengdc791882021-03-12 10:57:12 +080047void tfm_set_irq_signal(uint32_t partition_id, psa_signal_t signal,
48 uint32_t irq_line);
Mingyang Sund44522a2020-01-16 16:48:37 +080049
50#include "tfm_secure_irq_handlers_ipc.inc"
Edison Ai764d41f2018-09-21 15:56:36 +080051
Mingyang Sun2b352662021-04-21 11:35:43 +080052/* Partition static data region */
53REGION_DECLARE(Image$$, TFM_SP_STATIC_LIST, $$RO$$Base);
54REGION_DECLARE(Image$$, TFM_SP_STATIC_LIST, $$RO$$Limit);
55
Summer Qin373feb12020-03-27 15:35:33 +080056/*********************** Connection handle conversion APIs *******************/
57
Summer Qin373feb12020-03-27 15:35:33 +080058#define CONVERSION_FACTOR_BITOFFSET 3
59#define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET)
60/* Set 32 as the maximum */
61#define CONVERSION_FACTOR_VALUE_MAX 0x20
62
63#if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX
64#error "CONVERSION FACTOR OUT OF RANGE"
65#endif
66
67static uint32_t loop_index;
68
69/*
70 * A handle instance psa_handle_t allocated inside SPM is actually a memory
71 * address among the handle pool. Return this handle to the client directly
72 * exposes information of secure memory address. In this case, converting the
73 * handle into another value does not represent the memory address to avoid
74 * exposing secure memory directly to clients.
75 *
76 * This function converts the handle instance into another value by scaling the
77 * handle in pool offset, the converted value is named as a user handle.
78 *
79 * The formula:
80 * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE +
81 * CLIENT_HANDLE_VALUE_MIN + loop_index
82 * where:
83 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
84 * exceed CONVERSION_FACTOR_VALUE_MAX.
85 *
86 * handle_instance in RANGE[POOL_START, POOL_END]
87 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
88 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
89 *
90 * note:
91 * loop_index is used to promise same handle instance is converted into
92 * different user handles in short time.
93 */
Ken Liu505b1702020-05-29 13:19:58 +080094psa_handle_t tfm_spm_to_user_handle(struct tfm_conn_handle_t *handle_instance)
Summer Qin373feb12020-03-27 15:35:33 +080095{
96 psa_handle_t user_handle;
97
98 loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE;
99 user_handle = (psa_handle_t)((((uintptr_t)handle_instance -
100 (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) +
101 CLIENT_HANDLE_VALUE_MIN + loop_index);
102
103 return user_handle;
104}
105
106/*
107 * This function converts a user handle into a corresponded handle instance.
108 * The converted value is validated before returning, an invalid handle instance
109 * is returned as NULL.
110 *
111 * The formula:
112 * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) /
113 * CONVERSION_FACTOR_VALUE) + POOL_START
114 * where:
115 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
116 * exceed CONVERSION_FACTOR_VALUE_MAX.
117 *
118 * handle_instance in RANGE[POOL_START, POOL_END]
119 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
120 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
121 */
122struct tfm_conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle)
123{
124 struct tfm_conn_handle_t *handle_instance;
125
126 if (user_handle == PSA_NULL_HANDLE) {
127 return NULL;
128 }
129
130 handle_instance = (struct tfm_conn_handle_t *)((((uintptr_t)user_handle -
131 CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) +
132 (uintptr_t)conn_handle_pool);
133
134 return handle_instance;
135}
136
Edison Ai764d41f2018-09-21 15:56:36 +0800137/* Service handle management functions */
Mingyang Sun783a59b2021-04-20 15:52:18 +0800138struct tfm_conn_handle_t *tfm_spm_create_conn_handle(struct service_t *service,
139 int32_t client_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800140{
Edison Ai9cc26242019-08-06 11:28:04 +0800141 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800142
Ken Liuf250b8b2019-12-27 16:31:24 +0800143 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800144
145 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +0800146 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
147 if (!p_handle) {
Summer Qin630c76b2020-05-20 10:32:58 +0800148 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800149 }
150
Edison Ai9cc26242019-08-06 11:28:04 +0800151 p_handle->service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800152 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +0800153 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800154
155 /* Add handle node to list for next psa functions */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800156 BI_LIST_INSERT_BEFORE(&service->handle_list, &p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800157
Summer Qin630c76b2020-05-20 10:32:58 +0800158 return p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800159}
160
Summer Qin630c76b2020-05-20 10:32:58 +0800161int32_t tfm_spm_validate_conn_handle(
162 const struct tfm_conn_handle_t *conn_handle,
163 int32_t client_id)
Summer Qin1ce712a2019-10-14 18:04:05 +0800164{
165 /* Check the handle address is validated */
166 if (is_valid_chunk_data_in_pool(conn_handle_pool,
167 (uint8_t *)conn_handle) != true) {
Ken Liubcae38b2021-01-20 15:47:44 +0800168 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800169 }
170
171 /* Check the handle caller is correct */
Summer Qin630c76b2020-05-20 10:32:58 +0800172 if (conn_handle->client_id != client_id) {
Ken Liubcae38b2021-01-20 15:47:44 +0800173 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800174 }
175
Ken Liubcae38b2021-01-20 15:47:44 +0800176 return SPM_SUCCESS;
Summer Qin1ce712a2019-10-14 18:04:05 +0800177}
178
Mingyang Sun783a59b2021-04-20 15:52:18 +0800179int32_t tfm_spm_free_conn_handle(struct service_t *service,
Summer Qin02f7f072020-08-24 16:02:54 +0800180 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800181{
Ken Liuf250b8b2019-12-27 16:31:24 +0800182 TFM_CORE_ASSERT(service);
Summer Qin630c76b2020-05-20 10:32:58 +0800183 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800184
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200185 /* Clear magic as the handler is not used anymore */
Summer Qin630c76b2020-05-20 10:32:58 +0800186 conn_handle->internal_msg.magic = 0;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200187
Edison Ai764d41f2018-09-21 15:56:36 +0800188 /* Remove node from handle list */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800189 BI_LIST_REMOVE_NODE(&conn_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800190
191 /* Back handle buffer to pool */
Ken Liu66ca6132021-02-24 08:49:51 +0800192 tfm_pool_free(conn_handle_pool, conn_handle);
Ken Liubcae38b2021-01-20 15:47:44 +0800193 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800194}
195
Mingyang Sun783a59b2021-04-20 15:52:18 +0800196int32_t tfm_spm_set_rhandle(struct service_t *service,
Summer Qin02f7f072020-08-24 16:02:54 +0800197 struct tfm_conn_handle_t *conn_handle,
198 void *rhandle)
Edison Ai764d41f2018-09-21 15:56:36 +0800199{
Ken Liuf250b8b2019-12-27 16:31:24 +0800200 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800201 /* Set reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800202 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800203
Summer Qin630c76b2020-05-20 10:32:58 +0800204 conn_handle->rhandle = rhandle;
Ken Liubcae38b2021-01-20 15:47:44 +0800205 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800206}
207
Mingyang Sund44522a2020-01-16 16:48:37 +0800208/**
209 * \brief Get reverse handle value from connection hanlde.
210 *
211 * \param[in] service Target service context pointer
212 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800213 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800214 *
215 * \retval void * Success
216 * \retval "Does not return" Panic for those:
217 * service pointer are NULL
218 * hanlde is \ref PSA_NULL_HANDLE
219 * handle node does not be found
220 */
Mingyang Sun783a59b2021-04-20 15:52:18 +0800221static void *tfm_spm_get_rhandle(struct service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800222 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800223{
Ken Liuf250b8b2019-12-27 16:31:24 +0800224 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800225 /* Get reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800226 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800227
Summer Qin630c76b2020-05-20 10:32:58 +0800228 return conn_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800229}
230
231/* Partition management functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800232
Summer Qin02f7f072020-08-24 16:02:54 +0800233struct tfm_msg_body_t *tfm_spm_get_msg_by_signal(struct partition_t *partition,
234 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800235{
Ken Liu2c47f7f2021-01-22 11:06:04 +0800236 struct bi_list_node_t *node, *head;
Mingyang Sun73056b62020-07-03 15:18:46 +0800237 struct tfm_msg_body_t *tmp_msg, *msg = NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800238
Ken Liuf250b8b2019-12-27 16:31:24 +0800239 TFM_CORE_ASSERT(partition);
Edison Ai764d41f2018-09-21 15:56:36 +0800240
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800241 head = &partition->msg_list;
Mingyang Sun73056b62020-07-03 15:18:46 +0800242
Ken Liu2c47f7f2021-01-22 11:06:04 +0800243 if (BI_LIST_IS_EMPTY(head)) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800244 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800245 }
246
Mingyang Sun73056b62020-07-03 15:18:46 +0800247 /*
248 * There may be multiple messages for this RoT Service signal, do not clear
249 * partition mask until no remaining message. Search may be optimized.
250 */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800251 BI_LIST_FOR_EACH(node, head) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800252 tmp_msg = TFM_GET_CONTAINER_PTR(node, struct tfm_msg_body_t, msg_node);
253 if (tmp_msg->service->service_db->signal == signal && msg) {
254 return msg;
255 } else if (tmp_msg->service->service_db->signal == signal) {
256 msg = tmp_msg;
Ken Liu2c47f7f2021-01-22 11:06:04 +0800257 BI_LIST_REMOVE_NODE(node);
Edison Ai764d41f2018-09-21 15:56:36 +0800258 }
259 }
Mingyang Sun73056b62020-07-03 15:18:46 +0800260
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800261 partition->signals_asserted &= ~signal;
Mingyang Sun73056b62020-07-03 15:18:46 +0800262
263 return msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800264}
265
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800266/**
267 * \brief Returns the index of the partition with the given partition ID.
268 *
269 * \param[in] partition_id Partition id
270 *
271 * \return the partition idx if partition_id is valid,
272 * \ref SPM_INVALID_PARTITION_IDX othervise
273 */
274static uint32_t get_partition_idx(uint32_t partition_id)
275{
276 uint32_t i;
277
278 if (partition_id == INVALID_PARTITION_ID) {
279 return SPM_INVALID_PARTITION_IDX;
280 }
281
282 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
Mingyang Sun56c59692020-07-20 17:02:19 +0800283 if (g_spm_partition_db.partitions[i].p_static->pid == partition_id) {
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800284 return i;
285 }
286 }
287 return SPM_INVALID_PARTITION_IDX;
288}
289
290/**
291 * \brief Get the flags associated with a partition
292 *
293 * \param[in] partition_idx Partition index
294 *
295 * \return Flags associated with the partition
296 *
297 * \note This function doesn't check if partition_idx is valid.
298 */
299static uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
300{
Mingyang Sun56c59692020-07-20 17:02:19 +0800301 return g_spm_partition_db.partitions[partition_idx].p_static->flags;
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800302}
303
304#if TFM_LVL != 1
305/**
306 * \brief Change the privilege mode for partition thread mode.
307 *
308 * \param[in] privileged Privileged mode,
309 * \ref TFM_PARTITION_PRIVILEGED_MODE
310 * and \ref TFM_PARTITION_UNPRIVILEGED_MODE
311 *
312 * \note Barrier instructions are not called by this function, and if
313 * it is called in thread mode, it might be necessary to call
314 * them after this function returns.
315 */
316static void tfm_spm_partition_change_privilege(uint32_t privileged)
317{
318 CONTROL_Type ctrl;
319
320 ctrl.w = __get_CONTROL();
321
322 if (privileged == TFM_PARTITION_PRIVILEGED_MODE) {
323 ctrl.b.nPRIV = 0;
324 } else {
325 ctrl.b.nPRIV = 1;
326 }
327
328 __set_CONTROL(ctrl.w);
329}
330#endif /* if(TFM_LVL != 1) */
331
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800332uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
333{
334 if (partition_flags & SPM_PART_FLAG_PSA_ROT) {
335 return TFM_PARTITION_PRIVILEGED_MODE;
336 } else {
337 return TFM_PARTITION_UNPRIVILEGED_MODE;
338 }
339}
340
341bool tfm_is_partition_privileged(uint32_t partition_idx)
342{
343 uint32_t flags = tfm_spm_partition_get_flags(partition_idx);
344
345 return tfm_spm_partition_get_privileged_mode(flags) ==
346 TFM_PARTITION_PRIVILEGED_MODE;
347}
348
Mingyang Sun783a59b2021-04-20 15:52:18 +0800349struct service_t *tfm_spm_get_service_by_sid(uint32_t sid)
Edison Ai764d41f2018-09-21 15:56:36 +0800350{
Summer Qin2fca1c82020-03-20 14:37:55 +0800351 uint32_t i, num;
Edison Ai764d41f2018-09-21 15:56:36 +0800352
Mingyang Sun783a59b2021-04-20 15:52:18 +0800353 num = sizeof(g_services) / sizeof(struct service_t);
Summer Qin2fca1c82020-03-20 14:37:55 +0800354 for (i = 0; i < num; i++) {
Mingyang Sun783a59b2021-04-20 15:52:18 +0800355 if (g_services[i].service_db->sid == sid) {
356 return &g_services[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800357 }
358 }
Summer Qin2fca1c82020-03-20 14:37:55 +0800359
Edison Ai764d41f2018-09-21 15:56:36 +0800360 return NULL;
361}
362
Mingyang Sund44522a2020-01-16 16:48:37 +0800363/**
364 * \brief Get the partition context by partition ID.
365 *
366 * \param[in] partition_id Partition identity
367 *
368 * \retval NULL Failed
369 * \retval "Not NULL" Target partition context pointer,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800370 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800371 */
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800372static struct partition_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800373{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800374 uint32_t idx = get_partition_idx(partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800375
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800376 if (idx != SPM_INVALID_PARTITION_IDX) {
377 return &(g_spm_partition_db.partitions[idx]);
Edison Ai764d41f2018-09-21 15:56:36 +0800378 }
379 return NULL;
380}
381
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800382struct partition_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800383{
Kevin Peng82fbca52021-03-09 13:48:48 +0800384 struct tfm_core_thread_t *pth = tfm_core_thrd_get_curr();
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800385 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800386
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800387 partition = TFM_GET_CONTAINER_PTR(pth, struct partition_t, sp_thread);
388
Kevin Peng79c2bda2020-07-24 16:31:12 +0800389 return partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800390}
391
Mingyang Sun783a59b2021-04-20 15:52:18 +0800392int32_t tfm_spm_check_client_version(struct service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530393 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800394{
Ken Liuf250b8b2019-12-27 16:31:24 +0800395 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800396
Mingyang Sun2b352662021-04-21 11:35:43 +0800397 switch (SERVICE_GET_VERSION_POLICY(service->service_db->flags)) {
Edison Ai764d41f2018-09-21 15:56:36 +0800398 case TFM_VERSION_POLICY_RELAXED:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530399 if (version > service->service_db->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800400 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800401 }
402 break;
403 case TFM_VERSION_POLICY_STRICT:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530404 if (version != service->service_db->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800405 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800406 }
407 break;
408 default:
Ken Liubcae38b2021-01-20 15:47:44 +0800409 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800410 }
Ken Liubcae38b2021-01-20 15:47:44 +0800411 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800412}
413
Edison Aie728fbf2019-11-13 09:37:12 +0800414int32_t tfm_spm_check_authorization(uint32_t sid,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800415 struct service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800416 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800417{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800418 struct partition_t *partition = NULL;
Mingyang Sun2b352662021-04-21 11:35:43 +0800419 uint32_t *dep;
Edison Aie728fbf2019-11-13 09:37:12 +0800420 int32_t i;
421
Ken Liuf250b8b2019-12-27 16:31:24 +0800422 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800423
424 if (ns_caller) {
Mingyang Sun2b352662021-04-21 11:35:43 +0800425 if (!SERVICE_IS_NS_ACCESSIBLE(service->service_db->flags)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800426 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800427 }
428 } else {
429 partition = tfm_spm_get_running_partition();
430 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800431 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800432 }
433
Mingyang Sun2b352662021-04-21 11:35:43 +0800434 dep = (uint32_t *)STATIC_INF_DEPS(partition->p_static);
Mingyang Sun56c59692020-07-20 17:02:19 +0800435 for (i = 0; i < partition->p_static->ndeps; i++) {
Mingyang Sun2b352662021-04-21 11:35:43 +0800436 if (dep[i] == sid) {
Edison Aie728fbf2019-11-13 09:37:12 +0800437 break;
438 }
439 }
440
Mingyang Sun56c59692020-07-20 17:02:19 +0800441 if (i == partition->p_static->ndeps) {
Ken Liubcae38b2021-01-20 15:47:44 +0800442 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800443 }
444 }
Ken Liubcae38b2021-01-20 15:47:44 +0800445 return SPM_SUCCESS;
Edison Aie728fbf2019-11-13 09:37:12 +0800446}
447
Edison Ai764d41f2018-09-21 15:56:36 +0800448/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800449
Summer Qin02f7f072020-08-24 16:02:54 +0800450struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800451{
452 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200453 * The message handler passed by the caller is considered invalid in the
454 * following cases:
455 * 1. Not a valid message handle. (The address of a message is not the
456 * address of a possible handle from the pool
457 * 2. Handle not belongs to the caller partition (The handle is either
458 * unused, or owned by anither partition)
459 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800460 */
Ken Liu505b1702020-05-29 13:19:58 +0800461 struct tfm_msg_body_t *p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800462 uint32_t partition_id;
Ken Liu505b1702020-05-29 13:19:58 +0800463 struct tfm_conn_handle_t *p_conn_handle =
464 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200465
466 if (is_valid_chunk_data_in_pool(
Ken Liu505b1702020-05-29 13:19:58 +0800467 conn_handle_pool, (uint8_t *)p_conn_handle) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800468 return NULL;
469 }
470
Ken Liu505b1702020-05-29 13:19:58 +0800471 p_msg = &p_conn_handle->internal_msg;
472
Edison Ai764d41f2018-09-21 15:56:36 +0800473 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200474 * Check that the magic number is correct. This proves that the message
475 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800476 */
Ken Liu505b1702020-05-29 13:19:58 +0800477 if (p_msg->magic != TFM_MSG_MAGIC) {
Edison Ai764d41f2018-09-21 15:56:36 +0800478 return NULL;
479 }
480
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200481 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800482 partition_id = tfm_spm_partition_get_running_partition_id();
Mingyang Sun56c59692020-07-20 17:02:19 +0800483 if (partition_id != p_msg->service->partition->p_static->pid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800484 return NULL;
485 }
486
Ken Liu505b1702020-05-29 13:19:58 +0800487 return p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800488}
489
Kevin Pengdf6aa292021-03-11 17:58:50 +0800490struct tfm_msg_body_t *
491 tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle)
492{
493 TFM_CORE_ASSERT(conn_handle != NULL);
494
495 return &(conn_handle->internal_msg);
496}
497
Edison Ai97115822019-08-01 14:22:19 +0800498void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800499 struct service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800500 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800501 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800502 psa_invec *invec, size_t in_len,
503 psa_outvec *outvec, size_t out_len,
504 psa_outvec *caller_outvec)
505{
Edison Ai764d41f2018-09-21 15:56:36 +0800506 uint32_t i;
Ken Liu505b1702020-05-29 13:19:58 +0800507 struct tfm_conn_handle_t *conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800508
Ken Liuf250b8b2019-12-27 16:31:24 +0800509 TFM_CORE_ASSERT(msg);
510 TFM_CORE_ASSERT(service);
511 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
512 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
513 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
514 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
515 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800516
Edison Ai764d41f2018-09-21 15:56:36 +0800517 /* Clear message buffer before using it */
Summer Qinf24dbb52020-07-23 14:53:54 +0800518 spm_memset(msg, 0, sizeof(struct tfm_msg_body_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800519
Ken Liu35f89392019-03-14 14:51:05 +0800520 tfm_event_init(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800521 msg->magic = TFM_MSG_MAGIC;
522 msg->service = service;
Edison Ai764d41f2018-09-21 15:56:36 +0800523 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800524 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800525
526 /* Copy contents */
527 msg->msg.type = type;
528
529 for (i = 0; i < in_len; i++) {
530 msg->msg.in_size[i] = invec[i].len;
531 msg->invec[i].base = invec[i].base;
532 }
533
534 for (i = 0; i < out_len; i++) {
535 msg->msg.out_size[i] = outvec[i].len;
536 msg->outvec[i].base = outvec[i].base;
537 /* Out len is used to record the writed number, set 0 here again */
538 msg->outvec[i].len = 0;
539 }
540
Ken Liu505b1702020-05-29 13:19:58 +0800541 /* Use the user connect handle as the message handle */
542 msg->msg.handle = handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800543
Ken Liu505b1702020-05-29 13:19:58 +0800544 conn_handle = tfm_spm_to_handle_instance(handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800545 /* For connected handle, set rhandle to every message */
Ken Liu505b1702020-05-29 13:19:58 +0800546 if (conn_handle) {
547 msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800548 }
David Hu46603dd2019-12-11 18:05:16 +0800549
550 /* Set the private data of NSPE client caller in multi-core topology */
551 if (TFM_CLIENT_ID_IS_NS(client_id)) {
552 tfm_rpc_set_caller_data(msg, client_id);
553 }
Edison Ai764d41f2018-09-21 15:56:36 +0800554}
555
Mingyang Sun783a59b2021-04-20 15:52:18 +0800556void tfm_spm_send_event(struct service_t *service,
Kevin Peng8dac6102021-03-09 16:44:00 +0800557 struct tfm_msg_body_t *msg)
Edison Ai764d41f2018-09-21 15:56:36 +0800558{
Kevin Peng8dac6102021-03-09 16:44:00 +0800559 struct partition_t *partition = NULL;
560 psa_signal_t signal = 0;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800561
Kevin Peng8dac6102021-03-09 16:44:00 +0800562 if (!msg || !service || !service->service_db || !service->partition) {
563 tfm_core_panic();
564 }
565
566 partition = service->partition;
567 signal = service->service_db->signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800568
Mingyang Sun73056b62020-07-03 15:18:46 +0800569 /* Add message to partition message list tail */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800570 BI_LIST_INSERT_BEFORE(&partition->msg_list, &msg->msg_node);
Edison Ai764d41f2018-09-21 15:56:36 +0800571
572 /* Messages put. Update signals */
Kevin Peng8dac6102021-03-09 16:44:00 +0800573 partition->signals_asserted |= signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800574
Kevin Peng8dac6102021-03-09 16:44:00 +0800575 if (partition->signals_waiting & signal) {
576 tfm_event_wake(
577 &partition->event,
578 (partition->signals_asserted & partition->signals_waiting));
579 partition->signals_waiting &= ~signal;
580 }
Edison Ai764d41f2018-09-21 15:56:36 +0800581
David Hufb38d562019-09-23 15:58:34 +0800582 /*
583 * If it is a NS request via RPC, it is unnecessary to block current
584 * thread.
585 */
586 if (!is_tfm_rpc_msg(msg)) {
587 tfm_event_wait(&msg->ack_evnt);
588 }
Edison Ai764d41f2018-09-21 15:56:36 +0800589}
590
Mingyang Sunf3d29892019-07-10 17:50:23 +0800591uint32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800592{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800593 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800594
Kevin Peng79c2bda2020-07-24 16:31:12 +0800595 partition = tfm_spm_get_running_partition();
Mingyang Sun56c59692020-07-20 17:02:19 +0800596 if (partition && partition->p_static) {
597 return partition->p_static->pid;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800598 } else {
599 return INVALID_PARTITION_ID;
600 }
Edison Ai764d41f2018-09-21 15:56:36 +0800601}
602
Summer Qin43c185d2019-10-10 15:44:42 +0800603int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800604 enum tfm_memory_access_e access,
605 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800606{
Mingyang Sund1ed6732020-08-26 15:52:21 +0800607 enum tfm_hal_status_t err;
608 uint32_t attr = 0;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800609
610 /* If len is zero, this indicates an empty buffer and base is ignored */
611 if (len == 0) {
Ken Liubcae38b2021-01-20 15:47:44 +0800612 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800613 }
614
615 if (!buffer) {
Ken Liubcae38b2021-01-20 15:47:44 +0800616 return SPM_ERROR_BAD_PARAMETERS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800617 }
618
619 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800620 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800621 }
622
Summer Qin424d4db2019-03-25 14:09:51 +0800623 if (access == TFM_MEMORY_ACCESS_RW) {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800624 attr |= (TFM_HAL_ACCESS_READABLE | TFM_HAL_ACCESS_WRITABLE);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800625 } else {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800626 attr |= TFM_HAL_ACCESS_READABLE;
Summer Qin424d4db2019-03-25 14:09:51 +0800627 }
Mingyang Sund1ed6732020-08-26 15:52:21 +0800628
629 if (privileged == TFM_PARTITION_UNPRIVILEGED_MODE) {
630 attr |= TFM_HAL_ACCESS_UNPRIVILEGED;
631 } else {
632 attr &= ~TFM_HAL_ACCESS_UNPRIVILEGED;
633 }
634
635 if (ns_caller) {
636 attr |= TFM_HAL_ACCESS_NS;
637 }
638
639 err = tfm_hal_memory_has_access((uintptr_t)buffer, len, attr);
640
641 if (err == TFM_HAL_SUCCESS) {
Ken Liubcae38b2021-01-20 15:47:44 +0800642 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800643 }
644
Ken Liubcae38b2021-01-20 15:47:44 +0800645 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800646}
647
Mingyang Sun2b352662021-04-21 11:35:43 +0800648/* Allocate runtime space for partition from the pool. Static allocation. */
649static struct partition_t *tfm_allocate_partition(void)
650{
651 static uint32_t partition_pool_pos = 0;
652 struct partition_t *p_partition_allocated = NULL;
653
654 if (partition_pool_pos >= g_spm_partition_db.partition_count) {
655 return NULL;
656 }
657
658 p_partition_allocated = &g_spm_partition_db.partitions[partition_pool_pos];
659 partition_pool_pos++;
660
661 return p_partition_allocated;
662}
663
664/* Allocate runtime space for service from the pool. Static allocation. */
665static struct service_t *tfm_allocate_service(uint32_t service_count)
666{
667 static uint32_t service_pool_pos = 0;
668 struct service_t *p_service_allocated = NULL;
669 uint32_t num_of_services = sizeof(g_services) / sizeof(struct service_t);
670
Mingyang Sun1881ef12021-05-13 12:12:05 +0800671 if ((service_count == 0) ||
672 (service_count > num_of_services) ||
Mingyang Sun2b352662021-04-21 11:35:43 +0800673 (service_pool_pos >= num_of_services) ||
674 (service_pool_pos + service_count > num_of_services)) {
675 return NULL;
676 }
677
678 p_service_allocated = &g_services[service_pool_pos];
679 service_pool_pos += service_count;
680
681 return p_service_allocated;
682}
683
684/* Check partition static data validation */
685bool tfm_validate_partition_static(struct partition_static_info_t *p_cmninf)
686{
687 return ((p_cmninf->psa_ff_ver & PARTITION_INFO_MAGIC_MASK)
688 == PARTITION_INFO_MAGIC);
689}
690
Ken Liuce2692d2020-02-11 12:39:36 +0800691uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800692{
Mingyang Sun2b352662021-04-21 11:35:43 +0800693 uint32_t i, j;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800694 struct partition_t *partition;
Mingyang Sun2b352662021-04-21 11:35:43 +0800695 struct service_t *service;
Summer Qin66f1e032020-01-06 15:40:03 +0800696 struct tfm_core_thread_t *pth, *p_ns_entry_thread = NULL;
Ken Liu172f1e32021-02-05 16:31:03 +0800697 const struct platform_data_t **platform_data_p;
Mingyang Sun2b352662021-04-21 11:35:43 +0800698 uintptr_t part_static_start, part_static_end;
699 struct partition_static_info_t *p_cmninf;
700 struct service_static_info_t *p_service_static;
David Huf07e97d2021-02-15 22:05:40 +0800701#ifdef TFM_FIH_PROFILE_ON
702 fih_int fih_rc = FIH_FAILURE;
703#endif
Edison Ai764d41f2018-09-21 15:56:36 +0800704
705 tfm_pool_init(conn_handle_pool,
706 POOL_BUFFER_SIZE(conn_handle_pool),
707 sizeof(struct tfm_conn_handle_t),
708 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800709
Mingyang Sun2b352662021-04-21 11:35:43 +0800710 /* Load partition and service data */
711 part_static_start = PART_REGION_ADDR(TFM_SP_STATIC_LIST, $$RO$$Base);
712 part_static_end = PART_REGION_ADDR(TFM_SP_STATIC_LIST, $$RO$$Limit);
713 while (part_static_start < part_static_end) {
714 p_cmninf = (struct partition_static_info_t *)part_static_start;
Ken Liu3669fd82021-02-08 16:46:22 +0800715
Mingyang Sun2b352662021-04-21 11:35:43 +0800716 /* Validate static info section range */
717 part_static_start += STATIC_INFSZ_BYTES(p_cmninf);
718 if (part_static_start > part_static_end) {
Kevin Peng79c2bda2020-07-24 16:31:12 +0800719 tfm_core_panic();
720 }
721
Mingyang Sun2b352662021-04-21 11:35:43 +0800722 /* Validate partition static info */
723 if (!tfm_validate_partition_static(p_cmninf)) {
724 tfm_core_panic();
725 }
726 if (!(p_cmninf->flags & SPM_PART_FLAG_IPC)) {
727 tfm_core_panic();
728 }
729 if ((p_cmninf->psa_ff_ver & PARTITION_INFO_VERSION_MASK)
730 > PSA_FRAMEWORK_VERSION) {
Kevin Penga876d432021-01-07 16:14:28 +0800731 ERROR_MSG("Warning: Partition requires higher framework version!");
Mingyang Sun2b352662021-04-21 11:35:43 +0800732 tfm_core_panic();
Edison Aif0501702019-10-11 14:36:42 +0800733 }
734
Mingyang Sun2b352662021-04-21 11:35:43 +0800735 /* Allocate runtime space */
736 partition = tfm_allocate_partition();
737 if (!partition) {
738 tfm_core_panic();
739 }
Mingyang Sun1881ef12021-05-13 12:12:05 +0800740 if (p_cmninf->nservices) {
741 service = tfm_allocate_service(p_cmninf->nservices);
742 if (!service) {
743 tfm_core_panic();
744 }
745 } else {
746 service = NULL;
Mingyang Sun2b352662021-04-21 11:35:43 +0800747 }
748
749 partition->p_static = p_cmninf;
750
751 platform_data_p = POSITION_TO_PTR(p_cmninf->plat_cookie,
752 const struct platform_data_t **);
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100753 if (platform_data_p != NULL) {
754 while ((*platform_data_p) != NULL) {
David Huf07e97d2021-02-15 22:05:40 +0800755#ifdef TFM_FIH_PROFILE_ON
756 FIH_CALL(tfm_spm_hal_configure_default_isolation, fih_rc, i,
757 *platform_data_p);
758 if (fih_not_eq(fih_rc, fih_int_encode(TFM_PLAT_ERR_SUCCESS))) {
759 tfm_core_panic();
760 }
761#else /* TFM_FIH_PROFILE_ON */
Edison Ai6be3df12020-02-14 22:14:33 +0800762 if (tfm_spm_hal_configure_default_isolation(i,
763 *platform_data_p) != TFM_PLAT_ERR_SUCCESS) {
764 tfm_core_panic();
765 }
David Huf07e97d2021-02-15 22:05:40 +0800766#endif /* TFM_FIH_PROFILE_ON */
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100767 ++platform_data_p;
768 }
769 }
770
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800771 partition->signals_allowed |= PSA_DOORBELL;
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800772
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800773 /* TODO: This can be optimized by generating the assigned signal
774 * in code generation time.
775 */
776 for (j = 0; j < tfm_core_irq_signals_count; ++j) {
Mingyang Sun2b352662021-04-21 11:35:43 +0800777 if (tfm_core_irq_signals[j].partition_id == p_cmninf->pid) {
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800778 partition->signals_allowed |=
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800779 tfm_core_irq_signals[j].signal_value;
Mingyang Sun2b352662021-04-21 11:35:43 +0800780 if ((p_cmninf->psa_ff_ver & PARTITION_INFO_VERSION_MASK)
781 == 0x0100) {
Kevin Pengc6976262021-01-11 15:52:55 +0800782 tfm_spm_hal_enable_irq(tfm_core_irq_signals[j].irq_line);
Mingyang Sun2b352662021-04-21 11:35:43 +0800783 } else if ((p_cmninf->psa_ff_ver & PARTITION_INFO_VERSION_MASK)
784 == 0x0101) {
Kevin Pengc6976262021-01-11 15:52:55 +0800785 tfm_spm_hal_disable_irq(tfm_core_irq_signals[j].irq_line);
786 }
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800787 }
788 }
789
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800790 tfm_event_init(&partition->event);
Ken Liu2c47f7f2021-01-22 11:06:04 +0800791 BI_LIST_INIT_NODE(&partition->msg_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800792
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800793 pth = &partition->sp_thread;
Edison Ai764d41f2018-09-21 15:56:36 +0800794 if (!pth) {
Edison Ai9059ea02019-11-28 13:46:14 +0800795 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800796 }
797
Mingyang Sun2b352662021-04-21 11:35:43 +0800798 tfm_core_thrd_init(
799 pth,
800 POSITION_TO_ENTRY(p_cmninf->entry, tfm_core_thrd_entry_t),
801 NULL,
802 (uintptr_t)(p_cmninf->vars[0] + p_cmninf->stack_size),
803 (uintptr_t)p_cmninf->vars[0]);
Edison Ai788bae22019-02-18 17:38:59 +0800804
Mingyang Sun2b352662021-04-21 11:35:43 +0800805 pth->prior = TO_THREAD_PRIORITY(PARTITION_GET_PRIOR(p_cmninf->flags));
Edison Ai764d41f2018-09-21 15:56:36 +0800806
Mingyang Sun2b352662021-04-21 11:35:43 +0800807 if (p_cmninf->pid == TFM_SP_NON_SECURE_ID) {
Ken Liu490281d2019-12-30 15:55:26 +0800808 p_ns_entry_thread = pth;
Ken Liu5248af22019-12-29 12:47:13 +0800809 pth->param = (void *)tfm_spm_hal_get_ns_entry_point();
Ken Liu490281d2019-12-30 15:55:26 +0800810 }
811
Edison Ai764d41f2018-09-21 15:56:36 +0800812 /* Kick off */
Summer Qin66f1e032020-01-06 15:40:03 +0800813 if (tfm_core_thrd_start(pth) != THRD_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800814 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800815 }
Edison Ai764d41f2018-09-21 15:56:36 +0800816
Mingyang Sun2b352662021-04-21 11:35:43 +0800817 /* Init Services in the partition */
818 p_service_static =
819 (struct service_static_info_t *)STATIC_INF_SERVICE(p_cmninf);
820 for (i = 0; i < p_cmninf->nservices; i++) {
821 /* Fill service runtime data */
822 partition->signals_allowed |= p_service_static[i].signal;
823 service[i].service_db = &p_service_static[i];
824 service[i].partition = partition;
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800825
Mingyang Sun2b352662021-04-21 11:35:43 +0800826 /* Populate the p_service of stateless_service_ref[] */
827 if (SERVICE_IS_STATELESS(p_service_static[i].flags)) {
828 for (j = 0; j < STATIC_HANDLE_NUM_LIMIT; j++) {
829 if (stateless_service_ref[j].sid ==
830 p_service_static[i].sid) {
831 stateless_service_ref[j].p_service = &service[i];
832 break;
833 }
834 }
835 /* Stateless service not found in tracking table */
836 if (j >= STATIC_HANDLE_NUM_LIMIT) {
837 tfm_core_panic();
Mingyang Suneceda862021-02-25 15:09:48 +0800838 }
839 }
Mingyang Suneceda862021-02-25 15:09:48 +0800840
Mingyang Sun2b352662021-04-21 11:35:43 +0800841 BI_LIST_INIT_NODE(&service[i].handle_list);
842 }
Edison Ai764d41f2018-09-21 15:56:36 +0800843 }
844
Ken Liu483f5da2019-04-24 10:45:21 +0800845 /*
846 * All threads initialized, start the scheduler.
847 *
848 * NOTE:
Ken Liu490281d2019-12-30 15:55:26 +0800849 * It is worthy to give the thread object to scheduler if the background
850 * context belongs to one of the threads. Here the background thread is the
851 * initialization thread who calls SPM SVC, which re-uses the non-secure
852 * entry thread's stack. After SPM initialization is done, this stack is
853 * cleaned up and the background context is never going to return. Tell
854 * the scheduler that the current thread is non-secure entry thread.
Ken Liu483f5da2019-04-24 10:45:21 +0800855 */
Summer Qin66f1e032020-01-06 15:40:03 +0800856 tfm_core_thrd_start_scheduler(p_ns_entry_thread);
Ken Liuce2692d2020-02-11 12:39:36 +0800857
Summer Qind2ad7e72020-01-06 18:16:35 +0800858 return p_ns_entry_thread->arch_ctx.lr;
Edison Ai764d41f2018-09-21 15:56:36 +0800859}
Ken Liu2d175172019-03-21 17:08:41 +0800860
Summer Qind2ad7e72020-01-06 18:16:35 +0800861void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx)
Ken Liu2d175172019-03-21 17:08:41 +0800862{
Kevin Peng25b190b2020-10-30 17:10:45 +0800863#if TFM_LVL != 1
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800864 struct partition_t *p_next_partition;
Ken Liu2d175172019-03-21 17:08:41 +0800865 uint32_t is_privileged;
866#endif
Kevin Peng82fbca52021-03-09 13:48:48 +0800867 struct tfm_core_thread_t *pth_next = tfm_core_thrd_get_next();
868 struct tfm_core_thread_t *pth_curr = tfm_core_thrd_get_curr();
David Huf07e97d2021-02-15 22:05:40 +0800869#if defined(TFM_FIH_PROFILE_ON) && (TFM_LVL == 3)
870 fih_int fih_rc = FIH_FAILURE;
871#endif
Ken Liu2d175172019-03-21 17:08:41 +0800872
Mate Toth-Pal32b2ccd2019-04-26 10:00:16 +0200873 if (pth_next != NULL && pth_curr != pth_next) {
Kevin Peng25b190b2020-10-30 17:10:45 +0800874#if TFM_LVL != 1
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800875 p_next_partition = TFM_GET_CONTAINER_PTR(pth_next,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800876 struct partition_t,
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800877 sp_thread);
Ken Liu2d175172019-03-21 17:08:41 +0800878
Mingyang Sun56c59692020-07-20 17:02:19 +0800879 if (p_next_partition->p_static->flags & SPM_PART_FLAG_PSA_ROT) {
Ken Liu2d175172019-03-21 17:08:41 +0800880 is_privileged = TFM_PARTITION_PRIVILEGED_MODE;
881 } else {
882 is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE;
883 }
884
885 tfm_spm_partition_change_privilege(is_privileged);
Kevin Peng25b190b2020-10-30 17:10:45 +0800886#if TFM_LVL == 3
887 /*
888 * FIXME: To implement isolations among partitions in isolation level 3,
889 * each partition needs to run in unprivileged mode. Currently some
890 * PRoTs cannot work in unprivileged mode, make them privileged now.
891 */
892 if (is_privileged == TFM_PARTITION_UNPRIVILEGED_MODE) {
893 /* FIXME: only MPU-based implementations are supported currently */
David Huf07e97d2021-02-15 22:05:40 +0800894#ifdef TFM_FIH_PROFILE_ON
895 FIH_CALL(tfm_hal_mpu_update_partition_boundary, fih_rc,
Ken Liucdaec9c2021-04-17 11:04:23 +0800896 p_next_partition->p_static->mems.start,
897 p_next_partition->p_static->mems.limit);
David Huf07e97d2021-02-15 22:05:40 +0800898 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
899 tfm_core_panic();
900 }
901#else /* TFM_FIH_PROFILE_ON */
Kevin Peng25b190b2020-10-30 17:10:45 +0800902 if (tfm_hal_mpu_update_partition_boundary(
Ken Liucdaec9c2021-04-17 11:04:23 +0800903 p_next_partition->p_static->mems.start,
904 p_next_partition->p_static->mems.limit)
Kevin Peng25b190b2020-10-30 17:10:45 +0800905 != TFM_HAL_SUCCESS) {
906 tfm_core_panic();
907 }
David Huf07e97d2021-02-15 22:05:40 +0800908#endif /* TFM_FIH_PROFILE_ON */
Kevin Peng25b190b2020-10-30 17:10:45 +0800909 }
910#endif /* TFM_LVL == 3 */
911#endif /* TFM_LVL != 1 */
Mate Toth-Palc430b992019-05-09 21:01:14 +0200912
Summer Qind2ad7e72020-01-06 18:16:35 +0800913 tfm_core_thrd_switch_context(p_actx, pth_curr, pth_next);
Ken Liu2d175172019-03-21 17:08:41 +0800914 }
David Hufb38d562019-09-23 15:58:34 +0800915
916 /*
917 * Handle pending mailbox message from NS in multi-core topology.
918 * Empty operation on single Armv8-M platform.
919 */
920 tfm_rpc_client_call_handler();
Ken Liu2d175172019-03-21 17:08:41 +0800921}
Mingyang Sund44522a2020-01-16 16:48:37 +0800922
Summer Qin02f7f072020-08-24 16:02:54 +0800923void update_caller_outvec_len(struct tfm_msg_body_t *msg)
Mingyang Sund44522a2020-01-16 16:48:37 +0800924{
925 uint32_t i;
926
927 /*
928 * FixeMe: abstract these part into dedicated functions to avoid
929 * accessing thread context in psa layer
930 */
931 /* If it is a NS request via RPC, the owner of this message is not set */
932 if (!is_tfm_rpc_msg(msg)) {
933 TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK);
934 }
935
936 for (i = 0; i < PSA_MAX_IOVEC; i++) {
937 if (msg->msg.out_size[i] == 0) {
938 continue;
939 }
940
941 TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
942
943 msg->caller_outvec[i].len = msg->outvec[i].len;
944 }
945}
946
Summer Qin02f7f072020-08-24 16:02:54 +0800947void notify_with_signal(int32_t partition_id, psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800948{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800949 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800950
951 /*
952 * The value of partition_id must be greater than zero as the target of
953 * notification must be a Secure Partition, providing a Non-secure
954 * Partition ID is a fatal error.
955 */
956 if (!TFM_CLIENT_ID_IS_S(partition_id)) {
957 tfm_core_panic();
958 }
959
960 /*
961 * It is a fatal error if partition_id does not correspond to a Secure
962 * Partition.
963 */
964 partition = tfm_spm_get_partition_by_id(partition_id);
965 if (!partition) {
966 tfm_core_panic();
967 }
968
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800969 partition->signals_asserted |= signal;
Mingyang Sund44522a2020-01-16 16:48:37 +0800970
Kevin Peng8dac6102021-03-09 16:44:00 +0800971 if (partition->signals_waiting & signal) {
972 tfm_event_wake(
973 &partition->event,
974 partition->signals_asserted & partition->signals_waiting);
975 partition->signals_waiting &= ~signal;
976 }
Mingyang Sund44522a2020-01-16 16:48:37 +0800977}
978
Mingyang Sund44522a2020-01-16 16:48:37 +0800979/**
Kevin Pengdc791882021-03-12 10:57:12 +0800980 * \brief Sets signal to partition for Second-Level Interrupt Handling mode IRQ
Mingyang Sund44522a2020-01-16 16:48:37 +0800981 *
982 * \param[in] partition_id The ID of the partition which handles this IRQ
983 * \param[in] signal The signal associated with this IRQ
984 * \param[in] irq_line The number of the IRQ line
985 *
986 * \retval void Success.
987 * \retval "Does not return" Partition ID is invalid
988 */
Kevin Pengdc791882021-03-12 10:57:12 +0800989void tfm_set_irq_signal(uint32_t partition_id, psa_signal_t signal,
990 uint32_t irq_line)
Mingyang Sund44522a2020-01-16 16:48:37 +0800991{
Kevin Pengdc791882021-03-12 10:57:12 +0800992 __disable_irq();
993
Mingyang Sund44522a2020-01-16 16:48:37 +0800994 tfm_spm_hal_disable_irq(irq_line);
995 notify_with_signal(partition_id, signal);
Kevin Pengdc791882021-03-12 10:57:12 +0800996
997 __enable_irq();
Mingyang Sund44522a2020-01-16 16:48:37 +0800998}
999
Kevin Penga20b5af2021-01-11 11:20:52 +08001000int32_t get_irq_line_for_signal(int32_t partition_id, psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +08001001{
1002 size_t i;
1003
Ken Liu24dffb22021-02-10 11:03:58 +08001004 if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) {
Kevin Peng410bee52021-01-13 16:27:17 +08001005 return -1;
1006 }
1007
Mingyang Sund44522a2020-01-16 16:48:37 +08001008 for (i = 0; i < tfm_core_irq_signals_count; ++i) {
1009 if (tfm_core_irq_signals[i].partition_id == partition_id &&
1010 tfm_core_irq_signals[i].signal_value == signal) {
Kevin Penga20b5af2021-01-11 11:20:52 +08001011 return tfm_core_irq_signals[i].irq_line;
Mingyang Sund44522a2020-01-16 16:48:37 +08001012 }
1013 }
Kevin Penga20b5af2021-01-11 11:20:52 +08001014
Ken Liubcae38b2021-01-20 15:47:44 +08001015 return SPM_ERROR_GENERIC;
Mingyang Sund44522a2020-01-16 16:48:37 +08001016}
1017
Summer Qindea1f2c2021-01-11 14:46:34 +08001018#if !defined(__ARM_ARCH_8_1M_MAIN__)
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001019void tfm_spm_validate_caller(struct partition_t *p_cur_sp, uint32_t *p_ctx,
1020 uint32_t exc_return, bool ns_caller)
Mingyang Sund44522a2020-01-16 16:48:37 +08001021{
1022 uintptr_t stacked_ctx_pos;
1023
1024 if (ns_caller) {
1025 /*
1026 * The background IRQ can't be supported, since if SP is executing,
1027 * the preempted context of SP can be different with the one who
1028 * preempts veneer.
1029 */
Mingyang Sun56c59692020-07-20 17:02:19 +08001030 if (p_cur_sp->p_static->pid != TFM_SP_NON_SECURE_ID) {
Mingyang Sund44522a2020-01-16 16:48:37 +08001031 tfm_core_panic();
1032 }
1033
1034 /*
1035 * It is non-secure caller, check if veneer stack contains
1036 * multiple contexts.
1037 */
1038 stacked_ctx_pos = (uintptr_t)p_ctx +
1039 sizeof(struct tfm_state_context_t) +
Ken Liu05e13ba2020-07-25 10:31:33 +08001040 TFM_STACK_SEALED_SIZE;
Mingyang Sund44522a2020-01-16 16:48:37 +08001041
1042 if (is_stack_alloc_fp_space(exc_return)) {
1043#if defined (__FPU_USED) && (__FPU_USED == 1U)
1044 if (FPU->FPCCR & FPU_FPCCR_TS_Msk) {
1045 stacked_ctx_pos += TFM_ADDTIONAL_FP_CONTEXT_WORDS *
1046 sizeof(uint32_t);
1047 }
1048#endif
1049 stacked_ctx_pos += TFM_BASIC_FP_CONTEXT_WORDS * sizeof(uint32_t);
1050 }
1051
Mingyang Sunaf22ffa2020-07-09 17:48:37 +08001052 if (stacked_ctx_pos != p_cur_sp->sp_thread.stk_top) {
Mingyang Sund44522a2020-01-16 16:48:37 +08001053 tfm_core_panic();
1054 }
Mingyang Sun56c59692020-07-20 17:02:19 +08001055 } else if (p_cur_sp->p_static->pid <= 0) {
Mingyang Sund44522a2020-01-16 16:48:37 +08001056 tfm_core_panic();
1057 }
1058}
Summer Qindea1f2c2021-01-11 14:46:34 +08001059#endif
Summer Qin830c5542020-02-14 13:44:20 +08001060
1061void tfm_spm_request_handler(const struct tfm_state_context_t *svc_ctx)
1062{
1063 uint32_t *res_ptr = (uint32_t *)&svc_ctx->r0;
1064 uint32_t running_partition_flags = 0;
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001065 const struct partition_t *partition = NULL;
Summer Qin830c5542020-02-14 13:44:20 +08001066
1067 /* Check permissions on request type basis */
1068
1069 switch (svc_ctx->r0) {
1070 case TFM_SPM_REQUEST_RESET_VOTE:
1071 partition = tfm_spm_get_running_partition();
1072 if (!partition) {
1073 tfm_core_panic();
1074 }
Mingyang Sun56c59692020-07-20 17:02:19 +08001075 running_partition_flags = partition->p_static->flags;
Summer Qin830c5542020-02-14 13:44:20 +08001076
1077 /* Currently only PSA Root of Trust services are allowed to make Reset
1078 * vote request
1079 */
1080 if ((running_partition_flags & SPM_PART_FLAG_PSA_ROT) == 0) {
1081 *res_ptr = (uint32_t)TFM_ERROR_GENERIC;
1082 }
1083
1084 /* FixMe: this is a placeholder for checks to be performed before
1085 * allowing execution of reset
1086 */
1087 *res_ptr = (uint32_t)TFM_SUCCESS;
1088
1089 break;
1090 default:
1091 *res_ptr = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
1092 }
1093}