blob: ffec2f8fa6ed5414937a9ee574ca39766ca2ecf2 [file] [log] [blame]
Edison Ai764d41f2018-09-21 15:56:36 +08001/*
Ken Liu5248af22019-12-29 12:47:13 +08002 * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
Edison Ai764d41f2018-09-21 15:56:36 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
Mingyang Sunda01a972019-07-12 17:32:59 +08007
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"
Mingyang Sund44522a2020-01-16 16:48:37 +080014#include "tfm_internal_defines.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,
TTornblomfaf74f52020-03-04 17:56:27 +010047 IRQn_Type 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) {
167 return IPC_ERROR_GENERIC;
168 }
169
170 /* Check the handle caller is correct */
Summer Qin630c76b2020-05-20 10:32:58 +0800171 if (conn_handle->client_id != client_id) {
Summer Qin1ce712a2019-10-14 18:04:05 +0800172 return IPC_ERROR_GENERIC;
173 }
174
175 return IPC_SUCCESS;
176}
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);
Edison Ai764d41f2018-09-21 15:56:36 +0800192 return IPC_SUCCESS;
193}
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;
Edison Ai764d41f2018-09-21 15:56:36 +0800204 return IPC_SUCCESS;
205}
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) {
282 if (g_spm_partition_db.partitions[i].static_data->partition_id ==
283 partition_id) {
284 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{
301 return g_spm_partition_db.partitions[partition_idx].static_data->
302 partition_flags;
303}
304
305#if TFM_LVL != 1
306/**
307 * \brief Change the privilege mode for partition thread mode.
308 *
309 * \param[in] privileged Privileged mode,
310 * \ref TFM_PARTITION_PRIVILEGED_MODE
311 * and \ref TFM_PARTITION_UNPRIVILEGED_MODE
312 *
313 * \note Barrier instructions are not called by this function, and if
314 * it is called in thread mode, it might be necessary to call
315 * them after this function returns.
316 */
317static void tfm_spm_partition_change_privilege(uint32_t privileged)
318{
319 CONTROL_Type ctrl;
320
321 ctrl.w = __get_CONTROL();
322
323 if (privileged == TFM_PARTITION_PRIVILEGED_MODE) {
324 ctrl.b.nPRIV = 0;
325 } else {
326 ctrl.b.nPRIV = 1;
327 }
328
329 __set_CONTROL(ctrl.w);
330}
331#endif /* if(TFM_LVL != 1) */
332
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800333uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
334{
335 if (partition_flags & SPM_PART_FLAG_PSA_ROT) {
336 return TFM_PARTITION_PRIVILEGED_MODE;
337 } else {
338 return TFM_PARTITION_UNPRIVILEGED_MODE;
339 }
340}
341
342bool tfm_is_partition_privileged(uint32_t partition_idx)
343{
344 uint32_t flags = tfm_spm_partition_get_flags(partition_idx);
345
346 return tfm_spm_partition_get_privileged_mode(flags) ==
347 TFM_PARTITION_PRIVILEGED_MODE;
348}
349
Edison Ai764d41f2018-09-21 15:56:36 +0800350struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid)
351{
Summer Qin2fca1c82020-03-20 14:37:55 +0800352 uint32_t i, num;
Edison Ai764d41f2018-09-21 15:56:36 +0800353
Summer Qin2fca1c82020-03-20 14:37:55 +0800354 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
355 for (i = 0; i < num; i++) {
356 if (service[i].service_db->sid == sid) {
357 return &service[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800358 }
359 }
Summer Qin2fca1c82020-03-20 14:37:55 +0800360
Edison Ai764d41f2018-09-21 15:56:36 +0800361 return NULL;
362}
363
Mingyang Sund44522a2020-01-16 16:48:37 +0800364/**
365 * \brief Get the partition context by partition ID.
366 *
367 * \param[in] partition_id Partition identity
368 *
369 * \retval NULL Failed
370 * \retval "Not NULL" Target partition context pointer,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800371 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800372 */
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800373static struct partition_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800374{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800375 uint32_t idx = get_partition_idx(partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800376
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800377 if (idx != SPM_INVALID_PARTITION_IDX) {
378 return &(g_spm_partition_db.partitions[idx]);
Edison Ai764d41f2018-09-21 15:56:36 +0800379 }
380 return NULL;
381}
382
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800383struct partition_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800384{
Kevin Peng79c2bda2020-07-24 16:31:12 +0800385 struct tfm_core_thread_t *pth = tfm_core_thrd_get_curr_thread();
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800386 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800387
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800388 partition = TFM_GET_CONTAINER_PTR(pth, struct partition_t, sp_thread);
389
Kevin Peng79c2bda2020-07-24 16:31:12 +0800390 return partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800391}
392
393int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530394 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800395{
Ken Liuf250b8b2019-12-27 16:31:24 +0800396 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800397
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530398 switch (service->service_db->version_policy) {
Edison Ai764d41f2018-09-21 15:56:36 +0800399 case TFM_VERSION_POLICY_RELAXED:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530400 if (version > service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800401 return IPC_ERROR_VERSION;
402 }
403 break;
404 case TFM_VERSION_POLICY_STRICT:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530405 if (version != service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800406 return IPC_ERROR_VERSION;
407 }
408 break;
409 default:
410 return IPC_ERROR_VERSION;
411 }
412 return IPC_SUCCESS;
413}
414
Edison Aie728fbf2019-11-13 09:37:12 +0800415int32_t tfm_spm_check_authorization(uint32_t sid,
416 struct tfm_spm_service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800417 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800418{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800419 struct partition_t *partition = NULL;
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) {
425 if (!service->service_db->non_secure_client) {
426 return IPC_ERROR_GENERIC;
427 }
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
434 for (i = 0; i < partition->static_data->dependencies_num; i++) {
435 if (partition->static_data->p_dependencies[i] == sid) {
436 break;
437 }
438 }
439
440 if (i == partition->static_data->dependencies_num) {
441 return IPC_ERROR_GENERIC;
442 }
443 }
444 return IPC_SUCCESS;
445}
446
Edison Ai764d41f2018-09-21 15:56:36 +0800447/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800448
Summer Qin02f7f072020-08-24 16:02:54 +0800449struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800450{
451 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200452 * The message handler passed by the caller is considered invalid in the
453 * following cases:
454 * 1. Not a valid message handle. (The address of a message is not the
455 * address of a possible handle from the pool
456 * 2. Handle not belongs to the caller partition (The handle is either
457 * unused, or owned by anither partition)
458 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800459 */
Ken Liu505b1702020-05-29 13:19:58 +0800460 struct tfm_msg_body_t *p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800461 uint32_t partition_id;
Ken Liu505b1702020-05-29 13:19:58 +0800462 struct tfm_conn_handle_t *p_conn_handle =
463 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200464
465 if (is_valid_chunk_data_in_pool(
Ken Liu505b1702020-05-29 13:19:58 +0800466 conn_handle_pool, (uint8_t *)p_conn_handle) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800467 return NULL;
468 }
469
Ken Liu505b1702020-05-29 13:19:58 +0800470 p_msg = &p_conn_handle->internal_msg;
471
Edison Ai764d41f2018-09-21 15:56:36 +0800472 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200473 * Check that the magic number is correct. This proves that the message
474 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800475 */
Ken Liu505b1702020-05-29 13:19:58 +0800476 if (p_msg->magic != TFM_MSG_MAGIC) {
Edison Ai764d41f2018-09-21 15:56:36 +0800477 return NULL;
478 }
479
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200480 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800481 partition_id = tfm_spm_partition_get_running_partition_id();
Ken Liu505b1702020-05-29 13:19:58 +0800482 if (partition_id != p_msg->service->partition->static_data->partition_id) {
Edison Ai764d41f2018-09-21 15:56:36 +0800483 return NULL;
484 }
485
Ken Liu505b1702020-05-29 13:19:58 +0800486 return p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800487}
488
Edison Ai97115822019-08-01 14:22:19 +0800489struct tfm_msg_body_t *
Summer Qin630c76b2020-05-20 10:32:58 +0800490 tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800491{
Summer Qin630c76b2020-05-20 10:32:58 +0800492 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai97115822019-08-01 14:22:19 +0800493
Summer Qin630c76b2020-05-20 10:32:58 +0800494 return &(conn_handle->internal_msg);
Edison Ai97115822019-08-01 14:22:19 +0800495}
496
497void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
498 struct tfm_spm_service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800499 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800500 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800501 psa_invec *invec, size_t in_len,
502 psa_outvec *outvec, size_t out_len,
503 psa_outvec *caller_outvec)
504{
Edison Ai764d41f2018-09-21 15:56:36 +0800505 uint32_t i;
Ken Liu505b1702020-05-29 13:19:58 +0800506 struct tfm_conn_handle_t *conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800507
Ken Liuf250b8b2019-12-27 16:31:24 +0800508 TFM_CORE_ASSERT(msg);
509 TFM_CORE_ASSERT(service);
510 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
511 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
512 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
513 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
514 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800515
Edison Ai764d41f2018-09-21 15:56:36 +0800516 /* Clear message buffer before using it */
Summer Qinf24dbb52020-07-23 14:53:54 +0800517 spm_memset(msg, 0, sizeof(struct tfm_msg_body_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800518
Ken Liu35f89392019-03-14 14:51:05 +0800519 tfm_event_init(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800520 msg->magic = TFM_MSG_MAGIC;
521 msg->service = service;
Edison Ai764d41f2018-09-21 15:56:36 +0800522 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800523 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800524
525 /* Copy contents */
526 msg->msg.type = type;
527
528 for (i = 0; i < in_len; i++) {
529 msg->msg.in_size[i] = invec[i].len;
530 msg->invec[i].base = invec[i].base;
531 }
532
533 for (i = 0; i < out_len; i++) {
534 msg->msg.out_size[i] = outvec[i].len;
535 msg->outvec[i].base = outvec[i].base;
536 /* Out len is used to record the writed number, set 0 here again */
537 msg->outvec[i].len = 0;
538 }
539
Ken Liu505b1702020-05-29 13:19:58 +0800540 /* Use the user connect handle as the message handle */
541 msg->msg.handle = handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800542
Ken Liu505b1702020-05-29 13:19:58 +0800543 conn_handle = tfm_spm_to_handle_instance(handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800544 /* For connected handle, set rhandle to every message */
Ken Liu505b1702020-05-29 13:19:58 +0800545 if (conn_handle) {
546 msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800547 }
David Hu46603dd2019-12-11 18:05:16 +0800548
549 /* Set the private data of NSPE client caller in multi-core topology */
550 if (TFM_CLIENT_ID_IS_NS(client_id)) {
551 tfm_rpc_set_caller_data(msg, client_id);
552 }
Edison Ai764d41f2018-09-21 15:56:36 +0800553}
554
555int32_t tfm_spm_send_event(struct tfm_spm_service_t *service,
556 struct tfm_msg_body_t *msg)
557{
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800558 struct partition_t *partition = service->partition;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800559
Ken Liuf250b8b2019-12-27 16:31:24 +0800560 TFM_CORE_ASSERT(service);
561 TFM_CORE_ASSERT(msg);
Edison Ai764d41f2018-09-21 15:56:36 +0800562
Mingyang Sun73056b62020-07-03 15:18:46 +0800563 /* Add message to partition message list tail */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800564 tfm_list_add_tail(&partition->msg_list, &msg->msg_node);
Edison Ai764d41f2018-09-21 15:56:36 +0800565
566 /* Messages put. Update signals */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800567 partition->signals_asserted |= service->service_db->signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800568
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800569 tfm_event_wake(&partition->event,
570 (partition->signals_asserted & partition->signals_waiting));
Edison Ai764d41f2018-09-21 15:56:36 +0800571
David Hufb38d562019-09-23 15:58:34 +0800572 /*
573 * If it is a NS request via RPC, it is unnecessary to block current
574 * thread.
575 */
576 if (!is_tfm_rpc_msg(msg)) {
577 tfm_event_wait(&msg->ack_evnt);
578 }
Edison Ai764d41f2018-09-21 15:56:36 +0800579
580 return IPC_SUCCESS;
581}
582
Mingyang Sunf3d29892019-07-10 17:50:23 +0800583uint32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800584{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800585 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800586
Kevin Peng79c2bda2020-07-24 16:31:12 +0800587 partition = tfm_spm_get_running_partition();
588 if (partition && partition->static_data) {
589 return partition->static_data->partition_id;
590 } else {
591 return INVALID_PARTITION_ID;
592 }
Edison Ai764d41f2018-09-21 15:56:36 +0800593}
594
Summer Qin43c185d2019-10-10 15:44:42 +0800595int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800596 enum tfm_memory_access_e access,
597 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800598{
Mingyang Sund1ed6732020-08-26 15:52:21 +0800599 enum tfm_hal_status_t err;
600 uint32_t attr = 0;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800601
602 /* If len is zero, this indicates an empty buffer and base is ignored */
603 if (len == 0) {
604 return IPC_SUCCESS;
605 }
606
607 if (!buffer) {
608 return IPC_ERROR_BAD_PARAMETERS;
609 }
610
611 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
612 return IPC_ERROR_MEMORY_CHECK;
613 }
614
Summer Qin424d4db2019-03-25 14:09:51 +0800615 if (access == TFM_MEMORY_ACCESS_RW) {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800616 attr |= (TFM_HAL_ACCESS_READABLE | TFM_HAL_ACCESS_WRITABLE);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800617 } else {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800618 attr |= TFM_HAL_ACCESS_READABLE;
Summer Qin424d4db2019-03-25 14:09:51 +0800619 }
Mingyang Sund1ed6732020-08-26 15:52:21 +0800620
621 if (privileged == TFM_PARTITION_UNPRIVILEGED_MODE) {
622 attr |= TFM_HAL_ACCESS_UNPRIVILEGED;
623 } else {
624 attr &= ~TFM_HAL_ACCESS_UNPRIVILEGED;
625 }
626
627 if (ns_caller) {
628 attr |= TFM_HAL_ACCESS_NS;
629 }
630
631 err = tfm_hal_memory_has_access((uintptr_t)buffer, len, attr);
632
633 if (err == TFM_HAL_SUCCESS) {
Summer Qin424d4db2019-03-25 14:09:51 +0800634 return IPC_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800635 }
636
637 return IPC_ERROR_MEMORY_CHECK;
638}
639
Ken Liuce2692d2020-02-11 12:39:36 +0800640uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800641{
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800642 uint32_t i, j, num;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800643 struct partition_t *partition;
Summer Qin66f1e032020-01-06 15:40:03 +0800644 struct tfm_core_thread_t *pth, *p_ns_entry_thread = NULL;
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100645 const struct tfm_spm_partition_platform_data_t **platform_data_p;
Edison Ai764d41f2018-09-21 15:56:36 +0800646
647 tfm_pool_init(conn_handle_pool,
648 POOL_BUFFER_SIZE(conn_handle_pool),
649 sizeof(struct tfm_conn_handle_t),
650 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800651
652 /* Init partition first for it will be used when init service */
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200653 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800654 partition = &g_spm_partition_db.partitions[i];
Edison Aif0501702019-10-11 14:36:42 +0800655
Kevin Peng79c2bda2020-07-24 16:31:12 +0800656 if (!partition || !partition->memory_data || !partition->static_data) {
657 tfm_core_panic();
658 }
659
660 if (!(partition->static_data->partition_flags & SPM_PART_FLAG_IPC)) {
661 tfm_core_panic();
662 }
663
Edison Aif0501702019-10-11 14:36:42 +0800664 /* Check if the PSA framework version matches. */
665 if (partition->static_data->psa_framework_version !=
666 PSA_FRAMEWORK_VERSION) {
Kevin Peng79c2bda2020-07-24 16:31:12 +0800667 ERROR_MSG("Warning: PSA Framework Verison does not match!");
Edison Aif0501702019-10-11 14:36:42 +0800668 continue;
669 }
670
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100671 platform_data_p = partition->platform_data_list;
672 if (platform_data_p != NULL) {
673 while ((*platform_data_p) != NULL) {
Edison Ai6be3df12020-02-14 22:14:33 +0800674 if (tfm_spm_hal_configure_default_isolation(i,
675 *platform_data_p) != TFM_PLAT_ERR_SUCCESS) {
676 tfm_core_panic();
677 }
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100678 ++platform_data_p;
679 }
680 }
681
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800682 /* Add PSA_DOORBELL signal to assigned_signals */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800683 partition->signals_allowed |= PSA_DOORBELL;
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800684
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800685 /* TODO: This can be optimized by generating the assigned signal
686 * in code generation time.
687 */
688 for (j = 0; j < tfm_core_irq_signals_count; ++j) {
689 if (tfm_core_irq_signals[j].partition_id ==
690 partition->static_data->partition_id) {
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800691 partition->signals_allowed |=
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800692 tfm_core_irq_signals[j].signal_value;
693 }
694 }
695
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800696 tfm_event_init(&partition->event);
697 tfm_list_init(&partition->msg_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800698
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800699 pth = &partition->sp_thread;
Edison Ai764d41f2018-09-21 15:56:36 +0800700 if (!pth) {
Edison Ai9059ea02019-11-28 13:46:14 +0800701 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800702 }
703
Summer Qin66f1e032020-01-06 15:40:03 +0800704 tfm_core_thrd_init(pth,
Kevin Peng79c2bda2020-07-24 16:31:12 +0800705 (tfm_core_thrd_entry_t)
706 partition->static_data->partition_init,
Summer Qin66f1e032020-01-06 15:40:03 +0800707 NULL,
Kevin Peng79c2bda2020-07-24 16:31:12 +0800708 (uintptr_t)partition->memory_data->stack_top,
709 (uintptr_t)partition->memory_data->stack_bottom);
Edison Ai788bae22019-02-18 17:38:59 +0800710
Kevin Peng79c2bda2020-07-24 16:31:12 +0800711 pth->prior = partition->static_data->partition_priority;
Edison Ai764d41f2018-09-21 15:56:36 +0800712
Ken Liu490281d2019-12-30 15:55:26 +0800713 if (partition->static_data->partition_id == TFM_SP_NON_SECURE_ID) {
714 p_ns_entry_thread = pth;
Ken Liu5248af22019-12-29 12:47:13 +0800715 pth->param = (void *)tfm_spm_hal_get_ns_entry_point();
Ken Liu490281d2019-12-30 15:55:26 +0800716 }
717
Edison Ai764d41f2018-09-21 15:56:36 +0800718 /* Kick off */
Summer Qin66f1e032020-01-06 15:40:03 +0800719 if (tfm_core_thrd_start(pth) != THRD_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800720 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800721 }
722 }
723
724 /* Init Service */
Summer Qind99509f2019-08-02 17:36:58 +0800725 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
Edison Ai764d41f2018-09-21 15:56:36 +0800726 for (i = 0; i < num; i++) {
Summer Qine578c5b2019-08-16 16:42:16 +0800727 service[i].service_db = &service_db[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800728 partition =
Summer Qine578c5b2019-08-16 16:42:16 +0800729 tfm_spm_get_partition_by_id(service[i].service_db->partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800730 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800731 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800732 }
Summer Qind99509f2019-08-02 17:36:58 +0800733 service[i].partition = partition;
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800734 partition->signals_allowed |= service[i].service_db->signal;
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800735
Summer Qind99509f2019-08-02 17:36:58 +0800736 tfm_list_init(&service[i].handle_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800737 }
738
Ken Liu483f5da2019-04-24 10:45:21 +0800739 /*
740 * All threads initialized, start the scheduler.
741 *
742 * NOTE:
Ken Liu490281d2019-12-30 15:55:26 +0800743 * It is worthy to give the thread object to scheduler if the background
744 * context belongs to one of the threads. Here the background thread is the
745 * initialization thread who calls SPM SVC, which re-uses the non-secure
746 * entry thread's stack. After SPM initialization is done, this stack is
747 * cleaned up and the background context is never going to return. Tell
748 * the scheduler that the current thread is non-secure entry thread.
Ken Liu483f5da2019-04-24 10:45:21 +0800749 */
Summer Qin66f1e032020-01-06 15:40:03 +0800750 tfm_core_thrd_start_scheduler(p_ns_entry_thread);
Ken Liuce2692d2020-02-11 12:39:36 +0800751
Summer Qind2ad7e72020-01-06 18:16:35 +0800752 return p_ns_entry_thread->arch_ctx.lr;
Edison Ai764d41f2018-09-21 15:56:36 +0800753}
Ken Liu2d175172019-03-21 17:08:41 +0800754
Summer Qind2ad7e72020-01-06 18:16:35 +0800755void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx)
Ken Liu2d175172019-03-21 17:08:41 +0800756{
757#if TFM_LVL == 2
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800758 struct partition_t *p_next_partition;
Ken Liu2d175172019-03-21 17:08:41 +0800759 uint32_t is_privileged;
760#endif
Summer Qin66f1e032020-01-06 15:40:03 +0800761 struct tfm_core_thread_t *pth_next = tfm_core_thrd_get_next_thread();
762 struct tfm_core_thread_t *pth_curr = tfm_core_thrd_get_curr_thread();
Ken Liu2d175172019-03-21 17:08:41 +0800763
Mate Toth-Pal32b2ccd2019-04-26 10:00:16 +0200764 if (pth_next != NULL && pth_curr != pth_next) {
Ken Liu2d175172019-03-21 17:08:41 +0800765#if TFM_LVL == 2
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800766 p_next_partition = TFM_GET_CONTAINER_PTR(pth_next,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800767 struct partition_t,
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800768 sp_thread);
Ken Liu2d175172019-03-21 17:08:41 +0800769
Summer Qin423dbef2019-08-22 15:59:35 +0800770 if (p_next_partition->static_data->partition_flags &
Ken Liu2d175172019-03-21 17:08:41 +0800771 SPM_PART_FLAG_PSA_ROT) {
772 is_privileged = TFM_PARTITION_PRIVILEGED_MODE;
773 } else {
774 is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE;
775 }
776
777 tfm_spm_partition_change_privilege(is_privileged);
778#endif
Mate Toth-Palc430b992019-05-09 21:01:14 +0200779
Summer Qind2ad7e72020-01-06 18:16:35 +0800780 tfm_core_thrd_switch_context(p_actx, pth_curr, pth_next);
Ken Liu2d175172019-03-21 17:08:41 +0800781 }
David Hufb38d562019-09-23 15:58:34 +0800782
783 /*
784 * Handle pending mailbox message from NS in multi-core topology.
785 * Empty operation on single Armv8-M platform.
786 */
787 tfm_rpc_client_call_handler();
Ken Liu2d175172019-03-21 17:08:41 +0800788}
Mingyang Sund44522a2020-01-16 16:48:37 +0800789
Summer Qin02f7f072020-08-24 16:02:54 +0800790void update_caller_outvec_len(struct tfm_msg_body_t *msg)
Mingyang Sund44522a2020-01-16 16:48:37 +0800791{
792 uint32_t i;
793
794 /*
795 * FixeMe: abstract these part into dedicated functions to avoid
796 * accessing thread context in psa layer
797 */
798 /* If it is a NS request via RPC, the owner of this message is not set */
799 if (!is_tfm_rpc_msg(msg)) {
800 TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK);
801 }
802
803 for (i = 0; i < PSA_MAX_IOVEC; i++) {
804 if (msg->msg.out_size[i] == 0) {
805 continue;
806 }
807
808 TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
809
810 msg->caller_outvec[i].len = msg->outvec[i].len;
811 }
812}
813
Summer Qin02f7f072020-08-24 16:02:54 +0800814void notify_with_signal(int32_t partition_id, psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800815{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800816 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800817
818 /*
819 * The value of partition_id must be greater than zero as the target of
820 * notification must be a Secure Partition, providing a Non-secure
821 * Partition ID is a fatal error.
822 */
823 if (!TFM_CLIENT_ID_IS_S(partition_id)) {
824 tfm_core_panic();
825 }
826
827 /*
828 * It is a fatal error if partition_id does not correspond to a Secure
829 * Partition.
830 */
831 partition = tfm_spm_get_partition_by_id(partition_id);
832 if (!partition) {
833 tfm_core_panic();
834 }
835
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800836 partition->signals_asserted |= signal;
Mingyang Sund44522a2020-01-16 16:48:37 +0800837
838 /*
839 * The target partition may be blocked with waiting for signals after
840 * called psa_wait(). Set the return value with the available signals
841 * before wake it up with tfm_event_signal().
842 */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800843 tfm_event_wake(&partition->event,
844 partition->signals_asserted & partition->signals_waiting);
Mingyang Sund44522a2020-01-16 16:48:37 +0800845}
846
Mingyang Sund44522a2020-01-16 16:48:37 +0800847/**
848 * \brief assert signal for a given IRQ line.
849 *
850 * \param[in] partition_id The ID of the partition which handles this IRQ
851 * \param[in] signal The signal associated with this IRQ
852 * \param[in] irq_line The number of the IRQ line
853 *
854 * \retval void Success.
855 * \retval "Does not return" Partition ID is invalid
856 */
857void tfm_irq_handler(uint32_t partition_id, psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +0100858 IRQn_Type irq_line)
Mingyang Sund44522a2020-01-16 16:48:37 +0800859{
860 tfm_spm_hal_disable_irq(irq_line);
861 notify_with_signal(partition_id, signal);
862}
863
Summer Qin02f7f072020-08-24 16:02:54 +0800864int32_t get_irq_line_for_signal(int32_t partition_id,
865 psa_signal_t signal,
866 IRQn_Type *irq_line)
Mingyang Sund44522a2020-01-16 16:48:37 +0800867{
868 size_t i;
869
870 for (i = 0; i < tfm_core_irq_signals_count; ++i) {
871 if (tfm_core_irq_signals[i].partition_id == partition_id &&
872 tfm_core_irq_signals[i].signal_value == signal) {
873 *irq_line = tfm_core_irq_signals[i].irq_line;
874 return IPC_SUCCESS;
875 }
876 }
877 return IPC_ERROR_GENERIC;
878}
879
Mingyang Sund44522a2020-01-16 16:48:37 +0800880void tfm_spm_enable_irq(uint32_t *args)
881{
882 struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args;
883 psa_signal_t irq_signal = svc_ctx->r0;
TTornblomfaf74f52020-03-04 17:56:27 +0100884 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +0800885 int32_t ret;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800886 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800887
888 /* It is a fatal error if passed signal indicates more than one signals. */
889 if (!tfm_is_one_bit_set(irq_signal)) {
890 tfm_core_panic();
891 }
892
893 partition = tfm_spm_get_running_partition();
894 if (!partition) {
895 tfm_core_panic();
896 }
897
898 ret = get_irq_line_for_signal(partition->static_data->partition_id,
899 irq_signal, &irq_line);
900 /* It is a fatal error if passed signal is not an interrupt signal. */
901 if (ret != IPC_SUCCESS) {
902 tfm_core_panic();
903 }
904
905 tfm_spm_hal_enable_irq(irq_line);
906}
907
908void tfm_spm_disable_irq(uint32_t *args)
909{
910 struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args;
911 psa_signal_t irq_signal = svc_ctx->r0;
TTornblomfaf74f52020-03-04 17:56:27 +0100912 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +0800913 int32_t ret;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800914 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800915
916 /* It is a fatal error if passed signal indicates more than one signals. */
917 if (!tfm_is_one_bit_set(irq_signal)) {
918 tfm_core_panic();
919 }
920
921 partition = tfm_spm_get_running_partition();
922 if (!partition) {
923 tfm_core_panic();
924 }
925
926 ret = get_irq_line_for_signal(partition->static_data->partition_id,
927 irq_signal, &irq_line);
928 /* It is a fatal error if passed signal is not an interrupt signal. */
929 if (ret != IPC_SUCCESS) {
930 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 */
947 if (p_cur_sp->static_data->partition_id != TFM_SP_NON_SECURE_ID) {
948 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 }
972 } else if (p_cur_sp->static_data->partition_id <= 0) {
973 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 }
991 running_partition_flags = partition->static_data->partition_flags;
992
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++) {
1018 g_spm_partition_db.partitions[i].static_data = &static_data_list[i];
1019 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}