blob: dd4477b27a7ebc3ff4b9f4d4829fdda8cf641ef3 [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>
Jamie Foxcc31d402019-01-28 17:13:52 +000010#include "psa/client.h"
11#include "psa/service.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080012#include "tfm_thread.h"
Edison Ai764d41f2018-09-21 15:56:36 +080013#include "tfm_wait.h"
Ken Liubcae38b2021-01-20 15:47:44 +080014#include "internal_errors.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080015#include "tfm_spm_hal.h"
16#include "tfm_irq_list.h"
17#include "tfm_api.h"
18#include "tfm_secure_api.h"
19#include "tfm_memory_utils.h"
Mingyang Sund1ed6732020-08-26 15:52:21 +080020#include "tfm_hal_defs.h"
21#include "tfm_hal_isolation.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080022#include "spm_ipc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080023#include "tfm_peripherals_def.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080024#include "tfm_core_utils.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080025#include "tfm_rpc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080026#include "tfm_core_trustzone.h"
Edison Ai764d41f2018-09-21 15:56:36 +080027#include "tfm_list.h"
28#include "tfm_pools.h"
Mingyang Sunbd7ceb52020-06-11 16:53:03 +080029#include "region.h"
Summer Qin2bfd2a02018-09-26 17:10:41 +080030#include "region_defs.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080031#include "spm_partition_defs.h"
32#include "psa_manifest/pid.h"
Summer Qin5fdcf632020-06-22 16:49:24 +080033#include "tfm/tfm_spm_services.h"
Edison Ai764d41f2018-09-21 15:56:36 +080034
Ken Liu1f345b02020-05-30 21:11:05 +080035#include "secure_fw/partitions/tfm_service_list.inc"
Mingyang Sunbd7ceb52020-06-11 16:53:03 +080036#include "tfm_spm_db_ipc.inc"
Summer Qind99509f2019-08-02 17:36:58 +080037
38/* Extern service variable */
39extern struct tfm_spm_service_t service[];
Summer Qine578c5b2019-08-16 16:42:16 +080040extern const struct tfm_spm_service_db_t service_db[];
Summer Qind99509f2019-08-02 17:36:58 +080041
Edison Ai764d41f2018-09-21 15:56:36 +080042/* Pools */
43TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t),
44 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +080045
Mingyang Sund44522a2020-01-16 16:48:37 +080046void tfm_irq_handler(uint32_t partition_id, psa_signal_t signal,
Kevin Penga20b5af2021-01-11 11:20:52 +080047 uint32_t irq_line);
Mingyang Sund44522a2020-01-16 16:48:37 +080048
49#include "tfm_secure_irq_handlers_ipc.inc"
Edison Ai764d41f2018-09-21 15:56:36 +080050
Summer Qin373feb12020-03-27 15:35:33 +080051/*********************** Connection handle conversion APIs *******************/
52
53/* Set a minimal value here for feature expansion. */
54#define CLIENT_HANDLE_VALUE_MIN 32
55
56#define CONVERSION_FACTOR_BITOFFSET 3
57#define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET)
58/* Set 32 as the maximum */
59#define CONVERSION_FACTOR_VALUE_MAX 0x20
60
61#if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX
62#error "CONVERSION FACTOR OUT OF RANGE"
63#endif
64
65static uint32_t loop_index;
66
67/*
68 * A handle instance psa_handle_t allocated inside SPM is actually a memory
69 * address among the handle pool. Return this handle to the client directly
70 * exposes information of secure memory address. In this case, converting the
71 * handle into another value does not represent the memory address to avoid
72 * exposing secure memory directly to clients.
73 *
74 * This function converts the handle instance into another value by scaling the
75 * handle in pool offset, the converted value is named as a user handle.
76 *
77 * The formula:
78 * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE +
79 * CLIENT_HANDLE_VALUE_MIN + loop_index
80 * where:
81 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
82 * exceed CONVERSION_FACTOR_VALUE_MAX.
83 *
84 * handle_instance in RANGE[POOL_START, POOL_END]
85 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
86 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
87 *
88 * note:
89 * loop_index is used to promise same handle instance is converted into
90 * different user handles in short time.
91 */
Ken Liu505b1702020-05-29 13:19:58 +080092psa_handle_t tfm_spm_to_user_handle(struct tfm_conn_handle_t *handle_instance)
Summer Qin373feb12020-03-27 15:35:33 +080093{
94 psa_handle_t user_handle;
95
96 loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE;
97 user_handle = (psa_handle_t)((((uintptr_t)handle_instance -
98 (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) +
99 CLIENT_HANDLE_VALUE_MIN + loop_index);
100
101 return user_handle;
102}
103
104/*
105 * This function converts a user handle into a corresponded handle instance.
106 * The converted value is validated before returning, an invalid handle instance
107 * is returned as NULL.
108 *
109 * The formula:
110 * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) /
111 * CONVERSION_FACTOR_VALUE) + POOL_START
112 * where:
113 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
114 * exceed CONVERSION_FACTOR_VALUE_MAX.
115 *
116 * handle_instance in RANGE[POOL_START, POOL_END]
117 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
118 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
119 */
120struct tfm_conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle)
121{
122 struct tfm_conn_handle_t *handle_instance;
123
124 if (user_handle == PSA_NULL_HANDLE) {
125 return NULL;
126 }
127
128 handle_instance = (struct tfm_conn_handle_t *)((((uintptr_t)user_handle -
129 CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) +
130 (uintptr_t)conn_handle_pool);
131
132 return handle_instance;
133}
134
Edison Ai764d41f2018-09-21 15:56:36 +0800135/* Service handle management functions */
Summer Qin630c76b2020-05-20 10:32:58 +0800136struct tfm_conn_handle_t *tfm_spm_create_conn_handle(
137 struct tfm_spm_service_t *service,
Summer Qin1ce712a2019-10-14 18:04:05 +0800138 int32_t client_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800139{
Edison Ai9cc26242019-08-06 11:28:04 +0800140 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800141
Ken Liuf250b8b2019-12-27 16:31:24 +0800142 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800143
144 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +0800145 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
146 if (!p_handle) {
Summer Qin630c76b2020-05-20 10:32:58 +0800147 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800148 }
149
Edison Ai9cc26242019-08-06 11:28:04 +0800150 p_handle->service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800151 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +0800152 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800153
154 /* Add handle node to list for next psa functions */
Edison Ai9cc26242019-08-06 11:28:04 +0800155 tfm_list_add_tail(&service->handle_list, &p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800156
Summer Qin630c76b2020-05-20 10:32:58 +0800157 return p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800158}
159
Summer Qin630c76b2020-05-20 10:32:58 +0800160int32_t tfm_spm_validate_conn_handle(
161 const struct tfm_conn_handle_t *conn_handle,
162 int32_t client_id)
Summer Qin1ce712a2019-10-14 18:04:05 +0800163{
164 /* Check the handle address is validated */
165 if (is_valid_chunk_data_in_pool(conn_handle_pool,
166 (uint8_t *)conn_handle) != true) {
Ken Liubcae38b2021-01-20 15:47:44 +0800167 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800168 }
169
170 /* Check the handle caller is correct */
Summer Qin630c76b2020-05-20 10:32:58 +0800171 if (conn_handle->client_id != client_id) {
Ken Liubcae38b2021-01-20 15:47:44 +0800172 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800173 }
174
Ken Liubcae38b2021-01-20 15:47:44 +0800175 return SPM_SUCCESS;
Summer Qin1ce712a2019-10-14 18:04:05 +0800176}
177
Summer Qin02f7f072020-08-24 16:02:54 +0800178int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service,
179 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800180{
Ken Liuf250b8b2019-12-27 16:31:24 +0800181 TFM_CORE_ASSERT(service);
Summer Qin630c76b2020-05-20 10:32:58 +0800182 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800183
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200184 /* Clear magic as the handler is not used anymore */
Summer Qin630c76b2020-05-20 10:32:58 +0800185 conn_handle->internal_msg.magic = 0;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200186
Edison Ai764d41f2018-09-21 15:56:36 +0800187 /* Remove node from handle list */
Summer Qin630c76b2020-05-20 10:32:58 +0800188 tfm_list_del_node(&conn_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800189
190 /* Back handle buffer to pool */
Summer Qin630c76b2020-05-20 10:32:58 +0800191 tfm_pool_free(conn_handle);
Ken Liubcae38b2021-01-20 15:47:44 +0800192 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800193}
194
Summer Qin02f7f072020-08-24 16:02:54 +0800195int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service,
196 struct tfm_conn_handle_t *conn_handle,
197 void *rhandle)
Edison Ai764d41f2018-09-21 15:56:36 +0800198{
Ken Liuf250b8b2019-12-27 16:31:24 +0800199 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800200 /* Set reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800201 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800202
Summer Qin630c76b2020-05-20 10:32:58 +0800203 conn_handle->rhandle = rhandle;
Ken Liubcae38b2021-01-20 15:47:44 +0800204 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800205}
206
Mingyang Sund44522a2020-01-16 16:48:37 +0800207/**
208 * \brief Get reverse handle value from connection hanlde.
209 *
210 * \param[in] service Target service context pointer
211 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800212 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800213 *
214 * \retval void * Success
215 * \retval "Does not return" Panic for those:
216 * service pointer are NULL
217 * hanlde is \ref PSA_NULL_HANDLE
218 * handle node does not be found
219 */
220static void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800221 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800222{
Ken Liuf250b8b2019-12-27 16:31:24 +0800223 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800224 /* Get reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800225 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800226
Summer Qin630c76b2020-05-20 10:32:58 +0800227 return conn_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800228}
229
230/* Partition management functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800231
Summer Qin02f7f072020-08-24 16:02:54 +0800232struct tfm_msg_body_t *tfm_spm_get_msg_by_signal(struct partition_t *partition,
233 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800234{
235 struct tfm_list_node_t *node, *head;
Mingyang Sun73056b62020-07-03 15:18:46 +0800236 struct tfm_msg_body_t *tmp_msg, *msg = NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800237
Ken Liuf250b8b2019-12-27 16:31:24 +0800238 TFM_CORE_ASSERT(partition);
Edison Ai764d41f2018-09-21 15:56:36 +0800239
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800240 head = &partition->msg_list;
Mingyang Sun73056b62020-07-03 15:18:46 +0800241
242 if (tfm_list_is_empty(head)) {
243 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800244 }
245
Mingyang Sun73056b62020-07-03 15:18:46 +0800246 /*
247 * There may be multiple messages for this RoT Service signal, do not clear
248 * partition mask until no remaining message. Search may be optimized.
249 */
Edison Ai764d41f2018-09-21 15:56:36 +0800250 TFM_LIST_FOR_EACH(node, head) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800251 tmp_msg = TFM_GET_CONTAINER_PTR(node, struct tfm_msg_body_t, msg_node);
252 if (tmp_msg->service->service_db->signal == signal && msg) {
253 return msg;
254 } else if (tmp_msg->service->service_db->signal == signal) {
255 msg = tmp_msg;
256 tfm_list_del_node(node);
Edison Ai764d41f2018-09-21 15:56:36 +0800257 }
258 }
Mingyang Sun73056b62020-07-03 15:18:46 +0800259
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800260 partition->signals_asserted &= ~signal;
Mingyang Sun73056b62020-07-03 15:18:46 +0800261
262 return msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800263}
264
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800265/**
266 * \brief Returns the index of the partition with the given partition ID.
267 *
268 * \param[in] partition_id Partition id
269 *
270 * \return the partition idx if partition_id is valid,
271 * \ref SPM_INVALID_PARTITION_IDX othervise
272 */
273static uint32_t get_partition_idx(uint32_t partition_id)
274{
275 uint32_t i;
276
277 if (partition_id == INVALID_PARTITION_ID) {
278 return SPM_INVALID_PARTITION_IDX;
279 }
280
281 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
Mingyang Sun56c59692020-07-20 17:02:19 +0800282 if (g_spm_partition_db.partitions[i].p_static->pid == partition_id) {
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800283 return i;
284 }
285 }
286 return SPM_INVALID_PARTITION_IDX;
287}
288
289/**
290 * \brief Get the flags associated with a partition
291 *
292 * \param[in] partition_idx Partition index
293 *
294 * \return Flags associated with the partition
295 *
296 * \note This function doesn't check if partition_idx is valid.
297 */
298static uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
299{
Mingyang Sun56c59692020-07-20 17:02:19 +0800300 return g_spm_partition_db.partitions[partition_idx].p_static->flags;
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800301}
302
303#if TFM_LVL != 1
304/**
305 * \brief Change the privilege mode for partition thread mode.
306 *
307 * \param[in] privileged Privileged mode,
308 * \ref TFM_PARTITION_PRIVILEGED_MODE
309 * and \ref TFM_PARTITION_UNPRIVILEGED_MODE
310 *
311 * \note Barrier instructions are not called by this function, and if
312 * it is called in thread mode, it might be necessary to call
313 * them after this function returns.
314 */
315static void tfm_spm_partition_change_privilege(uint32_t privileged)
316{
317 CONTROL_Type ctrl;
318
319 ctrl.w = __get_CONTROL();
320
321 if (privileged == TFM_PARTITION_PRIVILEGED_MODE) {
322 ctrl.b.nPRIV = 0;
323 } else {
324 ctrl.b.nPRIV = 1;
325 }
326
327 __set_CONTROL(ctrl.w);
328}
329#endif /* if(TFM_LVL != 1) */
330
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800331uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
332{
333 if (partition_flags & SPM_PART_FLAG_PSA_ROT) {
334 return TFM_PARTITION_PRIVILEGED_MODE;
335 } else {
336 return TFM_PARTITION_UNPRIVILEGED_MODE;
337 }
338}
339
340bool tfm_is_partition_privileged(uint32_t partition_idx)
341{
342 uint32_t flags = tfm_spm_partition_get_flags(partition_idx);
343
344 return tfm_spm_partition_get_privileged_mode(flags) ==
345 TFM_PARTITION_PRIVILEGED_MODE;
346}
347
Edison Ai764d41f2018-09-21 15:56:36 +0800348struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid)
349{
Summer Qin2fca1c82020-03-20 14:37:55 +0800350 uint32_t i, num;
Edison Ai764d41f2018-09-21 15:56:36 +0800351
Summer Qin2fca1c82020-03-20 14:37:55 +0800352 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
353 for (i = 0; i < num; i++) {
354 if (service[i].service_db->sid == sid) {
355 return &service[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800356 }
357 }
Summer Qin2fca1c82020-03-20 14:37:55 +0800358
Edison Ai764d41f2018-09-21 15:56:36 +0800359 return NULL;
360}
361
Mingyang Sund44522a2020-01-16 16:48:37 +0800362/**
363 * \brief Get the partition context by partition ID.
364 *
365 * \param[in] partition_id Partition identity
366 *
367 * \retval NULL Failed
368 * \retval "Not NULL" Target partition context pointer,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800369 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800370 */
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800371static struct partition_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800372{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800373 uint32_t idx = get_partition_idx(partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800374
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800375 if (idx != SPM_INVALID_PARTITION_IDX) {
376 return &(g_spm_partition_db.partitions[idx]);
Edison Ai764d41f2018-09-21 15:56:36 +0800377 }
378 return NULL;
379}
380
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800381struct partition_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800382{
Kevin Peng79c2bda2020-07-24 16:31:12 +0800383 struct tfm_core_thread_t *pth = tfm_core_thrd_get_curr_thread();
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800384 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800385
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800386 partition = TFM_GET_CONTAINER_PTR(pth, struct partition_t, sp_thread);
387
Kevin Peng79c2bda2020-07-24 16:31:12 +0800388 return partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800389}
390
391int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530392 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800393{
Ken Liuf250b8b2019-12-27 16:31:24 +0800394 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800395
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530396 switch (service->service_db->version_policy) {
Edison Ai764d41f2018-09-21 15:56:36 +0800397 case TFM_VERSION_POLICY_RELAXED:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530398 if (version > service->service_db->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800399 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800400 }
401 break;
402 case TFM_VERSION_POLICY_STRICT:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530403 if (version != service->service_db->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800404 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800405 }
406 break;
407 default:
Ken Liubcae38b2021-01-20 15:47:44 +0800408 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800409 }
Ken Liubcae38b2021-01-20 15:47:44 +0800410 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800411}
412
Edison Aie728fbf2019-11-13 09:37:12 +0800413int32_t tfm_spm_check_authorization(uint32_t sid,
414 struct tfm_spm_service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800415 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800416{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800417 struct partition_t *partition = NULL;
Edison Aie728fbf2019-11-13 09:37:12 +0800418 int32_t i;
419
Ken Liuf250b8b2019-12-27 16:31:24 +0800420 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800421
422 if (ns_caller) {
423 if (!service->service_db->non_secure_client) {
Ken Liubcae38b2021-01-20 15:47:44 +0800424 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800425 }
426 } else {
427 partition = tfm_spm_get_running_partition();
428 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800429 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800430 }
431
Mingyang Sun56c59692020-07-20 17:02:19 +0800432 for (i = 0; i < partition->p_static->ndeps; i++) {
433 if (partition->p_static->deps[i] == sid) {
Edison Aie728fbf2019-11-13 09:37:12 +0800434 break;
435 }
436 }
437
Mingyang Sun56c59692020-07-20 17:02:19 +0800438 if (i == partition->p_static->ndeps) {
Ken Liubcae38b2021-01-20 15:47:44 +0800439 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800440 }
441 }
Ken Liubcae38b2021-01-20 15:47:44 +0800442 return SPM_SUCCESS;
Edison Aie728fbf2019-11-13 09:37:12 +0800443}
444
Edison Ai764d41f2018-09-21 15:56:36 +0800445/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800446
Summer Qin02f7f072020-08-24 16:02:54 +0800447struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800448{
449 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200450 * The message handler passed by the caller is considered invalid in the
451 * following cases:
452 * 1. Not a valid message handle. (The address of a message is not the
453 * address of a possible handle from the pool
454 * 2. Handle not belongs to the caller partition (The handle is either
455 * unused, or owned by anither partition)
456 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800457 */
Ken Liu505b1702020-05-29 13:19:58 +0800458 struct tfm_msg_body_t *p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800459 uint32_t partition_id;
Ken Liu505b1702020-05-29 13:19:58 +0800460 struct tfm_conn_handle_t *p_conn_handle =
461 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200462
463 if (is_valid_chunk_data_in_pool(
Ken Liu505b1702020-05-29 13:19:58 +0800464 conn_handle_pool, (uint8_t *)p_conn_handle) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800465 return NULL;
466 }
467
Ken Liu505b1702020-05-29 13:19:58 +0800468 p_msg = &p_conn_handle->internal_msg;
469
Edison Ai764d41f2018-09-21 15:56:36 +0800470 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200471 * Check that the magic number is correct. This proves that the message
472 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800473 */
Ken Liu505b1702020-05-29 13:19:58 +0800474 if (p_msg->magic != TFM_MSG_MAGIC) {
Edison Ai764d41f2018-09-21 15:56:36 +0800475 return NULL;
476 }
477
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200478 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800479 partition_id = tfm_spm_partition_get_running_partition_id();
Mingyang Sun56c59692020-07-20 17:02:19 +0800480 if (partition_id != p_msg->service->partition->p_static->pid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800481 return NULL;
482 }
483
Ken Liu505b1702020-05-29 13:19:58 +0800484 return p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800485}
486
Edison Ai97115822019-08-01 14:22:19 +0800487struct tfm_msg_body_t *
Summer Qin630c76b2020-05-20 10:32:58 +0800488 tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800489{
Summer Qin630c76b2020-05-20 10:32:58 +0800490 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai97115822019-08-01 14:22:19 +0800491
Summer Qin630c76b2020-05-20 10:32:58 +0800492 return &(conn_handle->internal_msg);
Edison Ai97115822019-08-01 14:22:19 +0800493}
494
495void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
496 struct tfm_spm_service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800497 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800498 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800499 psa_invec *invec, size_t in_len,
500 psa_outvec *outvec, size_t out_len,
501 psa_outvec *caller_outvec)
502{
Edison Ai764d41f2018-09-21 15:56:36 +0800503 uint32_t i;
Ken Liu505b1702020-05-29 13:19:58 +0800504 struct tfm_conn_handle_t *conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800505
Ken Liuf250b8b2019-12-27 16:31:24 +0800506 TFM_CORE_ASSERT(msg);
507 TFM_CORE_ASSERT(service);
508 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
509 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
510 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
511 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
512 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800513
Edison Ai764d41f2018-09-21 15:56:36 +0800514 /* Clear message buffer before using it */
Summer Qinf24dbb52020-07-23 14:53:54 +0800515 spm_memset(msg, 0, sizeof(struct tfm_msg_body_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800516
Ken Liu35f89392019-03-14 14:51:05 +0800517 tfm_event_init(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800518 msg->magic = TFM_MSG_MAGIC;
519 msg->service = service;
Edison Ai764d41f2018-09-21 15:56:36 +0800520 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800521 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800522
523 /* Copy contents */
524 msg->msg.type = type;
525
526 for (i = 0; i < in_len; i++) {
527 msg->msg.in_size[i] = invec[i].len;
528 msg->invec[i].base = invec[i].base;
529 }
530
531 for (i = 0; i < out_len; i++) {
532 msg->msg.out_size[i] = outvec[i].len;
533 msg->outvec[i].base = outvec[i].base;
534 /* Out len is used to record the writed number, set 0 here again */
535 msg->outvec[i].len = 0;
536 }
537
Ken Liu505b1702020-05-29 13:19:58 +0800538 /* Use the user connect handle as the message handle */
539 msg->msg.handle = handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800540
Ken Liu505b1702020-05-29 13:19:58 +0800541 conn_handle = tfm_spm_to_handle_instance(handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800542 /* For connected handle, set rhandle to every message */
Ken Liu505b1702020-05-29 13:19:58 +0800543 if (conn_handle) {
544 msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800545 }
David Hu46603dd2019-12-11 18:05:16 +0800546
547 /* Set the private data of NSPE client caller in multi-core topology */
548 if (TFM_CLIENT_ID_IS_NS(client_id)) {
549 tfm_rpc_set_caller_data(msg, client_id);
550 }
Edison Ai764d41f2018-09-21 15:56:36 +0800551}
552
553int32_t tfm_spm_send_event(struct tfm_spm_service_t *service,
554 struct tfm_msg_body_t *msg)
555{
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800556 struct partition_t *partition = service->partition;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800557
Ken Liuf250b8b2019-12-27 16:31:24 +0800558 TFM_CORE_ASSERT(service);
559 TFM_CORE_ASSERT(msg);
Edison Ai764d41f2018-09-21 15:56:36 +0800560
Mingyang Sun73056b62020-07-03 15:18:46 +0800561 /* Add message to partition message list tail */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800562 tfm_list_add_tail(&partition->msg_list, &msg->msg_node);
Edison Ai764d41f2018-09-21 15:56:36 +0800563
564 /* Messages put. Update signals */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800565 partition->signals_asserted |= service->service_db->signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800566
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800567 tfm_event_wake(&partition->event,
568 (partition->signals_asserted & partition->signals_waiting));
Edison Ai764d41f2018-09-21 15:56:36 +0800569
David Hufb38d562019-09-23 15:58:34 +0800570 /*
571 * If it is a NS request via RPC, it is unnecessary to block current
572 * thread.
573 */
574 if (!is_tfm_rpc_msg(msg)) {
575 tfm_event_wait(&msg->ack_evnt);
576 }
Edison Ai764d41f2018-09-21 15:56:36 +0800577
Ken Liubcae38b2021-01-20 15:47:44 +0800578 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800579}
580
Mingyang Sunf3d29892019-07-10 17:50:23 +0800581uint32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800582{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800583 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800584
Kevin Peng79c2bda2020-07-24 16:31:12 +0800585 partition = tfm_spm_get_running_partition();
Mingyang Sun56c59692020-07-20 17:02:19 +0800586 if (partition && partition->p_static) {
587 return partition->p_static->pid;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800588 } else {
589 return INVALID_PARTITION_ID;
590 }
Edison Ai764d41f2018-09-21 15:56:36 +0800591}
592
Summer Qin43c185d2019-10-10 15:44:42 +0800593int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800594 enum tfm_memory_access_e access,
595 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800596{
Mingyang Sund1ed6732020-08-26 15:52:21 +0800597 enum tfm_hal_status_t err;
598 uint32_t attr = 0;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800599
600 /* If len is zero, this indicates an empty buffer and base is ignored */
601 if (len == 0) {
Ken Liubcae38b2021-01-20 15:47:44 +0800602 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800603 }
604
605 if (!buffer) {
Ken Liubcae38b2021-01-20 15:47:44 +0800606 return SPM_ERROR_BAD_PARAMETERS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800607 }
608
609 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800610 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800611 }
612
Summer Qin424d4db2019-03-25 14:09:51 +0800613 if (access == TFM_MEMORY_ACCESS_RW) {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800614 attr |= (TFM_HAL_ACCESS_READABLE | TFM_HAL_ACCESS_WRITABLE);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800615 } else {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800616 attr |= TFM_HAL_ACCESS_READABLE;
Summer Qin424d4db2019-03-25 14:09:51 +0800617 }
Mingyang Sund1ed6732020-08-26 15:52:21 +0800618
619 if (privileged == TFM_PARTITION_UNPRIVILEGED_MODE) {
620 attr |= TFM_HAL_ACCESS_UNPRIVILEGED;
621 } else {
622 attr &= ~TFM_HAL_ACCESS_UNPRIVILEGED;
623 }
624
625 if (ns_caller) {
626 attr |= TFM_HAL_ACCESS_NS;
627 }
628
629 err = tfm_hal_memory_has_access((uintptr_t)buffer, len, attr);
630
631 if (err == TFM_HAL_SUCCESS) {
Ken Liubcae38b2021-01-20 15:47:44 +0800632 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800633 }
634
Ken Liubcae38b2021-01-20 15:47:44 +0800635 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800636}
637
Ken Liuce2692d2020-02-11 12:39:36 +0800638uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800639{
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800640 uint32_t i, j, num;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800641 struct partition_t *partition;
Summer Qin66f1e032020-01-06 15:40:03 +0800642 struct tfm_core_thread_t *pth, *p_ns_entry_thread = NULL;
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100643 const struct tfm_spm_partition_platform_data_t **platform_data_p;
Edison Ai764d41f2018-09-21 15:56:36 +0800644
645 tfm_pool_init(conn_handle_pool,
646 POOL_BUFFER_SIZE(conn_handle_pool),
647 sizeof(struct tfm_conn_handle_t),
648 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800649
650 /* Init partition first for it will be used when init service */
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200651 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800652 partition = &g_spm_partition_db.partitions[i];
Edison Aif0501702019-10-11 14:36:42 +0800653
Mingyang Sun56c59692020-07-20 17:02:19 +0800654 if (!partition || !partition->memory_data || !partition->p_static) {
Kevin Peng79c2bda2020-07-24 16:31:12 +0800655 tfm_core_panic();
656 }
657
Mingyang Sun56c59692020-07-20 17:02:19 +0800658 if (!(partition->p_static->flags & SPM_PART_FLAG_IPC)) {
Kevin Peng79c2bda2020-07-24 16:31:12 +0800659 tfm_core_panic();
660 }
661
Edison Aif0501702019-10-11 14:36:42 +0800662 /* Check if the PSA framework version matches. */
Kevin Penga876d432021-01-07 16:14:28 +0800663 if (partition->p_static->psa_ff_ver >
Edison Aif0501702019-10-11 14:36:42 +0800664 PSA_FRAMEWORK_VERSION) {
Kevin Penga876d432021-01-07 16:14:28 +0800665 ERROR_MSG("Warning: Partition requires higher framework version!");
Edison Aif0501702019-10-11 14:36:42 +0800666 continue;
667 }
668
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100669 platform_data_p = partition->platform_data_list;
670 if (platform_data_p != NULL) {
671 while ((*platform_data_p) != NULL) {
Edison Ai6be3df12020-02-14 22:14:33 +0800672 if (tfm_spm_hal_configure_default_isolation(i,
673 *platform_data_p) != TFM_PLAT_ERR_SUCCESS) {
674 tfm_core_panic();
675 }
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100676 ++platform_data_p;
677 }
678 }
679
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800680 /* Add PSA_DOORBELL signal to assigned_signals */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800681 partition->signals_allowed |= PSA_DOORBELL;
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800682
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800683 /* TODO: This can be optimized by generating the assigned signal
684 * in code generation time.
685 */
686 for (j = 0; j < tfm_core_irq_signals_count; ++j) {
687 if (tfm_core_irq_signals[j].partition_id ==
Mingyang Sun56c59692020-07-20 17:02:19 +0800688 partition->p_static->pid) {
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800689 partition->signals_allowed |=
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800690 tfm_core_irq_signals[j].signal_value;
691 }
692 }
693
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800694 tfm_event_init(&partition->event);
695 tfm_list_init(&partition->msg_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800696
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800697 pth = &partition->sp_thread;
Edison Ai764d41f2018-09-21 15:56:36 +0800698 if (!pth) {
Edison Ai9059ea02019-11-28 13:46:14 +0800699 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800700 }
701
Summer Qin66f1e032020-01-06 15:40:03 +0800702 tfm_core_thrd_init(pth,
Mingyang Sun56c59692020-07-20 17:02:19 +0800703 (tfm_core_thrd_entry_t)partition->p_static->entry,
Summer Qin66f1e032020-01-06 15:40:03 +0800704 NULL,
Kevin Peng79c2bda2020-07-24 16:31:12 +0800705 (uintptr_t)partition->memory_data->stack_top,
706 (uintptr_t)partition->memory_data->stack_bottom);
Edison Ai788bae22019-02-18 17:38:59 +0800707
Mingyang Sun56c59692020-07-20 17:02:19 +0800708 pth->prior = partition->p_static->priority;
Edison Ai764d41f2018-09-21 15:56:36 +0800709
Mingyang Sun56c59692020-07-20 17:02:19 +0800710 if (partition->p_static->pid == TFM_SP_NON_SECURE_ID) {
Ken Liu490281d2019-12-30 15:55:26 +0800711 p_ns_entry_thread = pth;
Ken Liu5248af22019-12-29 12:47:13 +0800712 pth->param = (void *)tfm_spm_hal_get_ns_entry_point();
Ken Liu490281d2019-12-30 15:55:26 +0800713 }
714
Edison Ai764d41f2018-09-21 15:56:36 +0800715 /* Kick off */
Summer Qin66f1e032020-01-06 15:40:03 +0800716 if (tfm_core_thrd_start(pth) != THRD_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800717 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800718 }
719 }
720
721 /* Init Service */
Summer Qind99509f2019-08-02 17:36:58 +0800722 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
Edison Ai764d41f2018-09-21 15:56:36 +0800723 for (i = 0; i < num; i++) {
Summer Qine578c5b2019-08-16 16:42:16 +0800724 service[i].service_db = &service_db[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800725 partition =
Summer Qine578c5b2019-08-16 16:42:16 +0800726 tfm_spm_get_partition_by_id(service[i].service_db->partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800727 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800728 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800729 }
Summer Qind99509f2019-08-02 17:36:58 +0800730 service[i].partition = partition;
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800731 partition->signals_allowed |= service[i].service_db->signal;
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800732
Summer Qind99509f2019-08-02 17:36:58 +0800733 tfm_list_init(&service[i].handle_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800734 }
735
Ken Liu483f5da2019-04-24 10:45:21 +0800736 /*
737 * All threads initialized, start the scheduler.
738 *
739 * NOTE:
Ken Liu490281d2019-12-30 15:55:26 +0800740 * It is worthy to give the thread object to scheduler if the background
741 * context belongs to one of the threads. Here the background thread is the
742 * initialization thread who calls SPM SVC, which re-uses the non-secure
743 * entry thread's stack. After SPM initialization is done, this stack is
744 * cleaned up and the background context is never going to return. Tell
745 * the scheduler that the current thread is non-secure entry thread.
Ken Liu483f5da2019-04-24 10:45:21 +0800746 */
Summer Qin66f1e032020-01-06 15:40:03 +0800747 tfm_core_thrd_start_scheduler(p_ns_entry_thread);
Ken Liuce2692d2020-02-11 12:39:36 +0800748
Summer Qind2ad7e72020-01-06 18:16:35 +0800749 return p_ns_entry_thread->arch_ctx.lr;
Edison Ai764d41f2018-09-21 15:56:36 +0800750}
Ken Liu2d175172019-03-21 17:08:41 +0800751
Summer Qind2ad7e72020-01-06 18:16:35 +0800752void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx)
Ken Liu2d175172019-03-21 17:08:41 +0800753{
Kevin Peng25b190b2020-10-30 17:10:45 +0800754#if TFM_LVL != 1
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800755 struct partition_t *p_next_partition;
Ken Liu2d175172019-03-21 17:08:41 +0800756 uint32_t is_privileged;
757#endif
Summer Qin66f1e032020-01-06 15:40:03 +0800758 struct tfm_core_thread_t *pth_next = tfm_core_thrd_get_next_thread();
759 struct tfm_core_thread_t *pth_curr = tfm_core_thrd_get_curr_thread();
Ken Liu2d175172019-03-21 17:08:41 +0800760
Mate Toth-Pal32b2ccd2019-04-26 10:00:16 +0200761 if (pth_next != NULL && pth_curr != pth_next) {
Kevin Peng25b190b2020-10-30 17:10:45 +0800762#if TFM_LVL != 1
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800763 p_next_partition = TFM_GET_CONTAINER_PTR(pth_next,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800764 struct partition_t,
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800765 sp_thread);
Ken Liu2d175172019-03-21 17:08:41 +0800766
Mingyang Sun56c59692020-07-20 17:02:19 +0800767 if (p_next_partition->p_static->flags & SPM_PART_FLAG_PSA_ROT) {
Ken Liu2d175172019-03-21 17:08:41 +0800768 is_privileged = TFM_PARTITION_PRIVILEGED_MODE;
769 } else {
770 is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE;
771 }
772
773 tfm_spm_partition_change_privilege(is_privileged);
Kevin Peng25b190b2020-10-30 17:10:45 +0800774#if TFM_LVL == 3
775 /*
776 * FIXME: To implement isolations among partitions in isolation level 3,
777 * each partition needs to run in unprivileged mode. Currently some
778 * PRoTs cannot work in unprivileged mode, make them privileged now.
779 */
780 if (is_privileged == TFM_PARTITION_UNPRIVILEGED_MODE) {
781 /* FIXME: only MPU-based implementations are supported currently */
782 if (tfm_hal_mpu_update_partition_boundary(
783 p_next_partition->memory_data->data_start,
784 p_next_partition->memory_data->data_limit)
785 != TFM_HAL_SUCCESS) {
786 tfm_core_panic();
787 }
788 }
789#endif /* TFM_LVL == 3 */
790#endif /* TFM_LVL != 1 */
Mate Toth-Palc430b992019-05-09 21:01:14 +0200791
Summer Qind2ad7e72020-01-06 18:16:35 +0800792 tfm_core_thrd_switch_context(p_actx, pth_curr, pth_next);
Ken Liu2d175172019-03-21 17:08:41 +0800793 }
David Hufb38d562019-09-23 15:58:34 +0800794
795 /*
796 * Handle pending mailbox message from NS in multi-core topology.
797 * Empty operation on single Armv8-M platform.
798 */
799 tfm_rpc_client_call_handler();
Ken Liu2d175172019-03-21 17:08:41 +0800800}
Mingyang Sund44522a2020-01-16 16:48:37 +0800801
Summer Qin02f7f072020-08-24 16:02:54 +0800802void update_caller_outvec_len(struct tfm_msg_body_t *msg)
Mingyang Sund44522a2020-01-16 16:48:37 +0800803{
804 uint32_t i;
805
806 /*
807 * FixeMe: abstract these part into dedicated functions to avoid
808 * accessing thread context in psa layer
809 */
810 /* If it is a NS request via RPC, the owner of this message is not set */
811 if (!is_tfm_rpc_msg(msg)) {
812 TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK);
813 }
814
815 for (i = 0; i < PSA_MAX_IOVEC; i++) {
816 if (msg->msg.out_size[i] == 0) {
817 continue;
818 }
819
820 TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
821
822 msg->caller_outvec[i].len = msg->outvec[i].len;
823 }
824}
825
Summer Qin02f7f072020-08-24 16:02:54 +0800826void notify_with_signal(int32_t partition_id, psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800827{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800828 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800829
830 /*
831 * The value of partition_id must be greater than zero as the target of
832 * notification must be a Secure Partition, providing a Non-secure
833 * Partition ID is a fatal error.
834 */
835 if (!TFM_CLIENT_ID_IS_S(partition_id)) {
836 tfm_core_panic();
837 }
838
839 /*
840 * It is a fatal error if partition_id does not correspond to a Secure
841 * Partition.
842 */
843 partition = tfm_spm_get_partition_by_id(partition_id);
844 if (!partition) {
845 tfm_core_panic();
846 }
847
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800848 partition->signals_asserted |= signal;
Mingyang Sund44522a2020-01-16 16:48:37 +0800849
850 /*
851 * The target partition may be blocked with waiting for signals after
852 * called psa_wait(). Set the return value with the available signals
853 * before wake it up with tfm_event_signal().
854 */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800855 tfm_event_wake(&partition->event,
856 partition->signals_asserted & partition->signals_waiting);
Mingyang Sund44522a2020-01-16 16:48:37 +0800857}
858
Mingyang Sund44522a2020-01-16 16:48:37 +0800859/**
860 * \brief assert signal for a given IRQ line.
861 *
862 * \param[in] partition_id The ID of the partition which handles this IRQ
863 * \param[in] signal The signal associated with this IRQ
864 * \param[in] irq_line The number of the IRQ line
865 *
866 * \retval void Success.
867 * \retval "Does not return" Partition ID is invalid
868 */
869void tfm_irq_handler(uint32_t partition_id, psa_signal_t signal,
Kevin Penga20b5af2021-01-11 11:20:52 +0800870 uint32_t irq_line)
Mingyang Sund44522a2020-01-16 16:48:37 +0800871{
872 tfm_spm_hal_disable_irq(irq_line);
873 notify_with_signal(partition_id, signal);
874}
875
Kevin Penga20b5af2021-01-11 11:20:52 +0800876int32_t get_irq_line_for_signal(int32_t partition_id, psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800877{
878 size_t i;
879
Kevin Peng410bee52021-01-13 16:27:17 +0800880 if (!tfm_is_one_bit_set(signal)) {
881 return -1;
882 }
883
Mingyang Sund44522a2020-01-16 16:48:37 +0800884 for (i = 0; i < tfm_core_irq_signals_count; ++i) {
885 if (tfm_core_irq_signals[i].partition_id == partition_id &&
886 tfm_core_irq_signals[i].signal_value == signal) {
Kevin Penga20b5af2021-01-11 11:20:52 +0800887 return tfm_core_irq_signals[i].irq_line;
Mingyang Sund44522a2020-01-16 16:48:37 +0800888 }
889 }
Kevin Penga20b5af2021-01-11 11:20:52 +0800890
Ken Liubcae38b2021-01-20 15:47:44 +0800891 return SPM_ERROR_GENERIC;
Mingyang Sund44522a2020-01-16 16:48:37 +0800892}
893
Mingyang Sund44522a2020-01-16 16:48:37 +0800894void tfm_spm_enable_irq(uint32_t *args)
895{
896 struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args;
897 psa_signal_t irq_signal = svc_ctx->r0;
Kevin Penga20b5af2021-01-11 11:20:52 +0800898 int32_t irq_line = 0;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800899 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800900
Mingyang Sund44522a2020-01-16 16:48:37 +0800901 partition = tfm_spm_get_running_partition();
902 if (!partition) {
903 tfm_core_panic();
904 }
905
Kevin Penga20b5af2021-01-11 11:20:52 +0800906 irq_line = get_irq_line_for_signal(partition->p_static->pid, irq_signal);
Mingyang Sund44522a2020-01-16 16:48:37 +0800907 /* It is a fatal error if passed signal is not an interrupt signal. */
Kevin Penga20b5af2021-01-11 11:20:52 +0800908 if (irq_line < 0) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800909 tfm_core_panic();
910 }
911
912 tfm_spm_hal_enable_irq(irq_line);
913}
914
915void tfm_spm_disable_irq(uint32_t *args)
916{
917 struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args;
918 psa_signal_t irq_signal = svc_ctx->r0;
Kevin Penga20b5af2021-01-11 11:20:52 +0800919 int32_t irq_line = 0;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800920 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800921
Mingyang Sund44522a2020-01-16 16:48:37 +0800922 partition = tfm_spm_get_running_partition();
923 if (!partition) {
924 tfm_core_panic();
925 }
926
Kevin Penga20b5af2021-01-11 11:20:52 +0800927 irq_line = get_irq_line_for_signal(partition->p_static->pid, irq_signal);
Mingyang Sund44522a2020-01-16 16:48:37 +0800928 /* It is a fatal error if passed signal is not an interrupt signal. */
Kevin Penga20b5af2021-01-11 11:20:52 +0800929 if (irq_line < 0) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800930 tfm_core_panic();
931 }
932
933 tfm_spm_hal_disable_irq(irq_line);
934}
935
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800936void tfm_spm_validate_caller(struct partition_t *p_cur_sp, uint32_t *p_ctx,
937 uint32_t exc_return, bool ns_caller)
Mingyang Sund44522a2020-01-16 16:48:37 +0800938{
939 uintptr_t stacked_ctx_pos;
940
941 if (ns_caller) {
942 /*
943 * The background IRQ can't be supported, since if SP is executing,
944 * the preempted context of SP can be different with the one who
945 * preempts veneer.
946 */
Mingyang Sun56c59692020-07-20 17:02:19 +0800947 if (p_cur_sp->p_static->pid != TFM_SP_NON_SECURE_ID) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800948 tfm_core_panic();
949 }
950
951 /*
952 * It is non-secure caller, check if veneer stack contains
953 * multiple contexts.
954 */
955 stacked_ctx_pos = (uintptr_t)p_ctx +
956 sizeof(struct tfm_state_context_t) +
Ken Liu05e13ba2020-07-25 10:31:33 +0800957 TFM_STACK_SEALED_SIZE;
Mingyang Sund44522a2020-01-16 16:48:37 +0800958
959 if (is_stack_alloc_fp_space(exc_return)) {
960#if defined (__FPU_USED) && (__FPU_USED == 1U)
961 if (FPU->FPCCR & FPU_FPCCR_TS_Msk) {
962 stacked_ctx_pos += TFM_ADDTIONAL_FP_CONTEXT_WORDS *
963 sizeof(uint32_t);
964 }
965#endif
966 stacked_ctx_pos += TFM_BASIC_FP_CONTEXT_WORDS * sizeof(uint32_t);
967 }
968
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800969 if (stacked_ctx_pos != p_cur_sp->sp_thread.stk_top) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800970 tfm_core_panic();
971 }
Mingyang Sun56c59692020-07-20 17:02:19 +0800972 } else if (p_cur_sp->p_static->pid <= 0) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800973 tfm_core_panic();
974 }
975}
Summer Qin830c5542020-02-14 13:44:20 +0800976
977void tfm_spm_request_handler(const struct tfm_state_context_t *svc_ctx)
978{
979 uint32_t *res_ptr = (uint32_t *)&svc_ctx->r0;
980 uint32_t running_partition_flags = 0;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800981 const struct partition_t *partition = NULL;
Summer Qin830c5542020-02-14 13:44:20 +0800982
983 /* Check permissions on request type basis */
984
985 switch (svc_ctx->r0) {
986 case TFM_SPM_REQUEST_RESET_VOTE:
987 partition = tfm_spm_get_running_partition();
988 if (!partition) {
989 tfm_core_panic();
990 }
Mingyang Sun56c59692020-07-20 17:02:19 +0800991 running_partition_flags = partition->p_static->flags;
Summer Qin830c5542020-02-14 13:44:20 +0800992
993 /* Currently only PSA Root of Trust services are allowed to make Reset
994 * vote request
995 */
996 if ((running_partition_flags & SPM_PART_FLAG_PSA_ROT) == 0) {
997 *res_ptr = (uint32_t)TFM_ERROR_GENERIC;
998 }
999
1000 /* FixMe: this is a placeholder for checks to be performed before
1001 * allowing execution of reset
1002 */
1003 *res_ptr = (uint32_t)TFM_SUCCESS;
1004
1005 break;
1006 default:
1007 *res_ptr = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
1008 }
1009}
Mingyang Sunbd7ceb52020-06-11 16:53:03 +08001010
1011enum spm_err_t tfm_spm_db_init(void)
1012{
1013 uint32_t i;
1014
1015 /* This function initialises partition db */
1016
1017 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun56c59692020-07-20 17:02:19 +08001018 g_spm_partition_db.partitions[i].p_static = &static_data_list[i];
Mingyang Sunbd7ceb52020-06-11 16:53:03 +08001019 g_spm_partition_db.partitions[i].platform_data_list =
1020 platform_data_list_list[i];
1021 g_spm_partition_db.partitions[i].memory_data = &memory_data_list[i];
1022 }
1023 g_spm_partition_db.is_init = 1;
1024
1025 return SPM_ERR_OK;
1026}