blob: 28ca30f412c0830084cef9150b6377baae8dfbca [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"
Kevin Peng25b190b2020-10-30 17:10:45 +080028#include "tfm_hal_isolation.h"
Edison Ai764d41f2018-09-21 15:56:36 +080029#include "tfm_pools.h"
Mingyang Sunbd7ceb52020-06-11 16:53:03 +080030#include "region.h"
Summer Qin2bfd2a02018-09-26 17:10:41 +080031#include "region_defs.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080032#include "spm_partition_defs.h"
33#include "psa_manifest/pid.h"
Summer Qin5fdcf632020-06-22 16:49:24 +080034#include "tfm/tfm_spm_services.h"
Edison Ai764d41f2018-09-21 15:56:36 +080035
Ken Liu1f345b02020-05-30 21:11:05 +080036#include "secure_fw/partitions/tfm_service_list.inc"
Mingyang Sunbd7ceb52020-06-11 16:53:03 +080037#include "tfm_spm_db_ipc.inc"
Summer Qind99509f2019-08-02 17:36:58 +080038
39/* Extern service variable */
40extern struct tfm_spm_service_t service[];
Summer Qine578c5b2019-08-16 16:42:16 +080041extern const struct tfm_spm_service_db_t service_db[];
Summer Qind99509f2019-08-02 17:36:58 +080042
Edison Ai764d41f2018-09-21 15:56:36 +080043/* Pools */
44TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t),
45 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +080046
Mingyang Sund44522a2020-01-16 16:48:37 +080047void tfm_irq_handler(uint32_t partition_id, psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +010048 IRQn_Type irq_line);
Mingyang Sund44522a2020-01-16 16:48:37 +080049
50#include "tfm_secure_irq_handlers_ipc.inc"
Edison Ai764d41f2018-09-21 15:56:36 +080051
Summer Qin373feb12020-03-27 15:35:33 +080052/*********************** Connection handle conversion APIs *******************/
53
54/* Set a minimal value here for feature expansion. */
55#define CLIENT_HANDLE_VALUE_MIN 32
56
57#define CONVERSION_FACTOR_BITOFFSET 3
58#define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET)
59/* Set 32 as the maximum */
60#define CONVERSION_FACTOR_VALUE_MAX 0x20
61
62#if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX
63#error "CONVERSION FACTOR OUT OF RANGE"
64#endif
65
66static uint32_t loop_index;
67
68/*
69 * A handle instance psa_handle_t allocated inside SPM is actually a memory
70 * address among the handle pool. Return this handle to the client directly
71 * exposes information of secure memory address. In this case, converting the
72 * handle into another value does not represent the memory address to avoid
73 * exposing secure memory directly to clients.
74 *
75 * This function converts the handle instance into another value by scaling the
76 * handle in pool offset, the converted value is named as a user handle.
77 *
78 * The formula:
79 * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE +
80 * CLIENT_HANDLE_VALUE_MIN + loop_index
81 * where:
82 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
83 * exceed CONVERSION_FACTOR_VALUE_MAX.
84 *
85 * handle_instance in RANGE[POOL_START, POOL_END]
86 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
87 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
88 *
89 * note:
90 * loop_index is used to promise same handle instance is converted into
91 * different user handles in short time.
92 */
Ken Liu505b1702020-05-29 13:19:58 +080093psa_handle_t tfm_spm_to_user_handle(struct tfm_conn_handle_t *handle_instance)
Summer Qin373feb12020-03-27 15:35:33 +080094{
95 psa_handle_t user_handle;
96
97 loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE;
98 user_handle = (psa_handle_t)((((uintptr_t)handle_instance -
99 (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) +
100 CLIENT_HANDLE_VALUE_MIN + loop_index);
101
102 return user_handle;
103}
104
105/*
106 * This function converts a user handle into a corresponded handle instance.
107 * The converted value is validated before returning, an invalid handle instance
108 * is returned as NULL.
109 *
110 * The formula:
111 * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) /
112 * CONVERSION_FACTOR_VALUE) + POOL_START
113 * where:
114 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
115 * exceed CONVERSION_FACTOR_VALUE_MAX.
116 *
117 * handle_instance in RANGE[POOL_START, POOL_END]
118 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
119 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
120 */
121struct tfm_conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle)
122{
123 struct tfm_conn_handle_t *handle_instance;
124
125 if (user_handle == PSA_NULL_HANDLE) {
126 return NULL;
127 }
128
129 handle_instance = (struct tfm_conn_handle_t *)((((uintptr_t)user_handle -
130 CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) +
131 (uintptr_t)conn_handle_pool);
132
133 return handle_instance;
134}
135
Edison Ai764d41f2018-09-21 15:56:36 +0800136/* Service handle management functions */
Summer Qin630c76b2020-05-20 10:32:58 +0800137struct tfm_conn_handle_t *tfm_spm_create_conn_handle(
138 struct tfm_spm_service_t *service,
Summer Qin1ce712a2019-10-14 18:04:05 +0800139 int32_t client_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800140{
Edison Ai9cc26242019-08-06 11:28:04 +0800141 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800142
Ken Liuf250b8b2019-12-27 16:31:24 +0800143 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800144
145 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +0800146 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
147 if (!p_handle) {
Summer Qin630c76b2020-05-20 10:32:58 +0800148 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800149 }
150
Edison Ai9cc26242019-08-06 11:28:04 +0800151 p_handle->service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800152 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +0800153 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800154
155 /* Add handle node to list for next psa functions */
Edison Ai9cc26242019-08-06 11:28:04 +0800156 tfm_list_add_tail(&service->handle_list, &p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800157
Summer Qin630c76b2020-05-20 10:32:58 +0800158 return p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800159}
160
Summer Qin630c76b2020-05-20 10:32:58 +0800161int32_t tfm_spm_validate_conn_handle(
162 const struct tfm_conn_handle_t *conn_handle,
163 int32_t client_id)
Summer Qin1ce712a2019-10-14 18:04:05 +0800164{
165 /* Check the handle address is validated */
166 if (is_valid_chunk_data_in_pool(conn_handle_pool,
167 (uint8_t *)conn_handle) != true) {
168 return IPC_ERROR_GENERIC;
169 }
170
171 /* Check the handle caller is correct */
Summer Qin630c76b2020-05-20 10:32:58 +0800172 if (conn_handle->client_id != client_id) {
Summer Qin1ce712a2019-10-14 18:04:05 +0800173 return IPC_ERROR_GENERIC;
174 }
175
176 return IPC_SUCCESS;
177}
178
Summer Qin02f7f072020-08-24 16:02:54 +0800179int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service,
180 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800181{
Ken Liuf250b8b2019-12-27 16:31:24 +0800182 TFM_CORE_ASSERT(service);
Summer Qin630c76b2020-05-20 10:32:58 +0800183 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800184
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200185 /* Clear magic as the handler is not used anymore */
Summer Qin630c76b2020-05-20 10:32:58 +0800186 conn_handle->internal_msg.magic = 0;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200187
Edison Ai764d41f2018-09-21 15:56:36 +0800188 /* Remove node from handle list */
Summer Qin630c76b2020-05-20 10:32:58 +0800189 tfm_list_del_node(&conn_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800190
191 /* Back handle buffer to pool */
Summer Qin630c76b2020-05-20 10:32:58 +0800192 tfm_pool_free(conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800193 return IPC_SUCCESS;
194}
195
Summer Qin02f7f072020-08-24 16:02:54 +0800196int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service,
197 struct tfm_conn_handle_t *conn_handle,
198 void *rhandle)
Edison Ai764d41f2018-09-21 15:56:36 +0800199{
Ken Liuf250b8b2019-12-27 16:31:24 +0800200 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800201 /* Set reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800202 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800203
Summer Qin630c76b2020-05-20 10:32:58 +0800204 conn_handle->rhandle = rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800205 return IPC_SUCCESS;
206}
207
Mingyang Sund44522a2020-01-16 16:48:37 +0800208/**
209 * \brief Get reverse handle value from connection hanlde.
210 *
211 * \param[in] service Target service context pointer
212 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800213 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800214 *
215 * \retval void * Success
216 * \retval "Does not return" Panic for those:
217 * service pointer are NULL
218 * hanlde is \ref PSA_NULL_HANDLE
219 * handle node does not be found
220 */
221static void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800222 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800223{
Ken Liuf250b8b2019-12-27 16:31:24 +0800224 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800225 /* Get reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800226 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800227
Summer Qin630c76b2020-05-20 10:32:58 +0800228 return conn_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800229}
230
231/* Partition management functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800232
Summer Qin02f7f072020-08-24 16:02:54 +0800233struct tfm_msg_body_t *tfm_spm_get_msg_by_signal(struct partition_t *partition,
234 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800235{
236 struct tfm_list_node_t *node, *head;
Mingyang Sun73056b62020-07-03 15:18:46 +0800237 struct tfm_msg_body_t *tmp_msg, *msg = NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800238
Ken Liuf250b8b2019-12-27 16:31:24 +0800239 TFM_CORE_ASSERT(partition);
Edison Ai764d41f2018-09-21 15:56:36 +0800240
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800241 head = &partition->msg_list;
Mingyang Sun73056b62020-07-03 15:18:46 +0800242
243 if (tfm_list_is_empty(head)) {
244 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800245 }
246
Mingyang Sun73056b62020-07-03 15:18:46 +0800247 /*
248 * There may be multiple messages for this RoT Service signal, do not clear
249 * partition mask until no remaining message. Search may be optimized.
250 */
Edison Ai764d41f2018-09-21 15:56:36 +0800251 TFM_LIST_FOR_EACH(node, head) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800252 tmp_msg = TFM_GET_CONTAINER_PTR(node, struct tfm_msg_body_t, msg_node);
253 if (tmp_msg->service->service_db->signal == signal && msg) {
254 return msg;
255 } else if (tmp_msg->service->service_db->signal == signal) {
256 msg = tmp_msg;
257 tfm_list_del_node(node);
Edison Ai764d41f2018-09-21 15:56:36 +0800258 }
259 }
Mingyang Sun73056b62020-07-03 15:18:46 +0800260
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800261 partition->signals_asserted &= ~signal;
Mingyang Sun73056b62020-07-03 15:18:46 +0800262
263 return msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800264}
265
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800266/**
267 * \brief Returns the index of the partition with the given partition ID.
268 *
269 * \param[in] partition_id Partition id
270 *
271 * \return the partition idx if partition_id is valid,
272 * \ref SPM_INVALID_PARTITION_IDX othervise
273 */
274static uint32_t get_partition_idx(uint32_t partition_id)
275{
276 uint32_t i;
277
278 if (partition_id == INVALID_PARTITION_ID) {
279 return SPM_INVALID_PARTITION_IDX;
280 }
281
282 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
283 if (g_spm_partition_db.partitions[i].static_data->partition_id ==
284 partition_id) {
285 return i;
286 }
287 }
288 return SPM_INVALID_PARTITION_IDX;
289}
290
291/**
292 * \brief Get the flags associated with a partition
293 *
294 * \param[in] partition_idx Partition index
295 *
296 * \return Flags associated with the partition
297 *
298 * \note This function doesn't check if partition_idx is valid.
299 */
300static uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
301{
302 return g_spm_partition_db.partitions[partition_idx].static_data->
303 partition_flags;
304}
305
306#if TFM_LVL != 1
307/**
308 * \brief Change the privilege mode for partition thread mode.
309 *
310 * \param[in] privileged Privileged mode,
311 * \ref TFM_PARTITION_PRIVILEGED_MODE
312 * and \ref TFM_PARTITION_UNPRIVILEGED_MODE
313 *
314 * \note Barrier instructions are not called by this function, and if
315 * it is called in thread mode, it might be necessary to call
316 * them after this function returns.
317 */
318static void tfm_spm_partition_change_privilege(uint32_t privileged)
319{
320 CONTROL_Type ctrl;
321
322 ctrl.w = __get_CONTROL();
323
324 if (privileged == TFM_PARTITION_PRIVILEGED_MODE) {
325 ctrl.b.nPRIV = 0;
326 } else {
327 ctrl.b.nPRIV = 1;
328 }
329
330 __set_CONTROL(ctrl.w);
331}
332#endif /* if(TFM_LVL != 1) */
333
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800334uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
335{
336 if (partition_flags & SPM_PART_FLAG_PSA_ROT) {
337 return TFM_PARTITION_PRIVILEGED_MODE;
338 } else {
339 return TFM_PARTITION_UNPRIVILEGED_MODE;
340 }
341}
342
343bool tfm_is_partition_privileged(uint32_t partition_idx)
344{
345 uint32_t flags = tfm_spm_partition_get_flags(partition_idx);
346
347 return tfm_spm_partition_get_privileged_mode(flags) ==
348 TFM_PARTITION_PRIVILEGED_MODE;
349}
350
Edison Ai764d41f2018-09-21 15:56:36 +0800351struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid)
352{
Summer Qin2fca1c82020-03-20 14:37:55 +0800353 uint32_t i, num;
Edison Ai764d41f2018-09-21 15:56:36 +0800354
Summer Qin2fca1c82020-03-20 14:37:55 +0800355 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
356 for (i = 0; i < num; i++) {
357 if (service[i].service_db->sid == sid) {
358 return &service[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800359 }
360 }
Summer Qin2fca1c82020-03-20 14:37:55 +0800361
Edison Ai764d41f2018-09-21 15:56:36 +0800362 return NULL;
363}
364
Mingyang Sund44522a2020-01-16 16:48:37 +0800365/**
366 * \brief Get the partition context by partition ID.
367 *
368 * \param[in] partition_id Partition identity
369 *
370 * \retval NULL Failed
371 * \retval "Not NULL" Target partition context pointer,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800372 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800373 */
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800374static struct partition_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800375{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800376 uint32_t idx = get_partition_idx(partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800377
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800378 if (idx != SPM_INVALID_PARTITION_IDX) {
379 return &(g_spm_partition_db.partitions[idx]);
Edison Ai764d41f2018-09-21 15:56:36 +0800380 }
381 return NULL;
382}
383
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800384struct partition_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800385{
Kevin Peng79c2bda2020-07-24 16:31:12 +0800386 struct tfm_core_thread_t *pth = tfm_core_thrd_get_curr_thread();
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800387 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800388
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800389 partition = TFM_GET_CONTAINER_PTR(pth, struct partition_t, sp_thread);
390
Kevin Peng79c2bda2020-07-24 16:31:12 +0800391 return partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800392}
393
394int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530395 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800396{
Ken Liuf250b8b2019-12-27 16:31:24 +0800397 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800398
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530399 switch (service->service_db->version_policy) {
Edison Ai764d41f2018-09-21 15:56:36 +0800400 case TFM_VERSION_POLICY_RELAXED:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530401 if (version > service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800402 return IPC_ERROR_VERSION;
403 }
404 break;
405 case TFM_VERSION_POLICY_STRICT:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530406 if (version != service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800407 return IPC_ERROR_VERSION;
408 }
409 break;
410 default:
411 return IPC_ERROR_VERSION;
412 }
413 return IPC_SUCCESS;
414}
415
Edison Aie728fbf2019-11-13 09:37:12 +0800416int32_t tfm_spm_check_authorization(uint32_t sid,
417 struct tfm_spm_service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800418 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800419{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800420 struct partition_t *partition = NULL;
Edison Aie728fbf2019-11-13 09:37:12 +0800421 int32_t i;
422
Ken Liuf250b8b2019-12-27 16:31:24 +0800423 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800424
425 if (ns_caller) {
426 if (!service->service_db->non_secure_client) {
427 return IPC_ERROR_GENERIC;
428 }
429 } else {
430 partition = tfm_spm_get_running_partition();
431 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800432 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800433 }
434
435 for (i = 0; i < partition->static_data->dependencies_num; i++) {
436 if (partition->static_data->p_dependencies[i] == sid) {
437 break;
438 }
439 }
440
441 if (i == partition->static_data->dependencies_num) {
442 return IPC_ERROR_GENERIC;
443 }
444 }
445 return IPC_SUCCESS;
446}
447
Edison Ai764d41f2018-09-21 15:56:36 +0800448/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800449
Summer Qin02f7f072020-08-24 16:02:54 +0800450struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800451{
452 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200453 * The message handler passed by the caller is considered invalid in the
454 * following cases:
455 * 1. Not a valid message handle. (The address of a message is not the
456 * address of a possible handle from the pool
457 * 2. Handle not belongs to the caller partition (The handle is either
458 * unused, or owned by anither partition)
459 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800460 */
Ken Liu505b1702020-05-29 13:19:58 +0800461 struct tfm_msg_body_t *p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800462 uint32_t partition_id;
Ken Liu505b1702020-05-29 13:19:58 +0800463 struct tfm_conn_handle_t *p_conn_handle =
464 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200465
466 if (is_valid_chunk_data_in_pool(
Ken Liu505b1702020-05-29 13:19:58 +0800467 conn_handle_pool, (uint8_t *)p_conn_handle) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800468 return NULL;
469 }
470
Ken Liu505b1702020-05-29 13:19:58 +0800471 p_msg = &p_conn_handle->internal_msg;
472
Edison Ai764d41f2018-09-21 15:56:36 +0800473 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200474 * Check that the magic number is correct. This proves that the message
475 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800476 */
Ken Liu505b1702020-05-29 13:19:58 +0800477 if (p_msg->magic != TFM_MSG_MAGIC) {
Edison Ai764d41f2018-09-21 15:56:36 +0800478 return NULL;
479 }
480
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200481 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800482 partition_id = tfm_spm_partition_get_running_partition_id();
Ken Liu505b1702020-05-29 13:19:58 +0800483 if (partition_id != p_msg->service->partition->static_data->partition_id) {
Edison Ai764d41f2018-09-21 15:56:36 +0800484 return NULL;
485 }
486
Ken Liu505b1702020-05-29 13:19:58 +0800487 return p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800488}
489
Edison Ai97115822019-08-01 14:22:19 +0800490struct tfm_msg_body_t *
Summer Qin630c76b2020-05-20 10:32:58 +0800491 tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800492{
Summer Qin630c76b2020-05-20 10:32:58 +0800493 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai97115822019-08-01 14:22:19 +0800494
Summer Qin630c76b2020-05-20 10:32:58 +0800495 return &(conn_handle->internal_msg);
Edison Ai97115822019-08-01 14:22:19 +0800496}
497
498void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
499 struct tfm_spm_service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800500 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800501 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800502 psa_invec *invec, size_t in_len,
503 psa_outvec *outvec, size_t out_len,
504 psa_outvec *caller_outvec)
505{
Edison Ai764d41f2018-09-21 15:56:36 +0800506 uint32_t i;
Ken Liu505b1702020-05-29 13:19:58 +0800507 struct tfm_conn_handle_t *conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800508
Ken Liuf250b8b2019-12-27 16:31:24 +0800509 TFM_CORE_ASSERT(msg);
510 TFM_CORE_ASSERT(service);
511 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
512 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
513 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
514 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
515 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800516
Edison Ai764d41f2018-09-21 15:56:36 +0800517 /* Clear message buffer before using it */
Summer Qinf24dbb52020-07-23 14:53:54 +0800518 spm_memset(msg, 0, sizeof(struct tfm_msg_body_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800519
Ken Liu35f89392019-03-14 14:51:05 +0800520 tfm_event_init(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800521 msg->magic = TFM_MSG_MAGIC;
522 msg->service = service;
Edison Ai764d41f2018-09-21 15:56:36 +0800523 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800524 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800525
526 /* Copy contents */
527 msg->msg.type = type;
528
529 for (i = 0; i < in_len; i++) {
530 msg->msg.in_size[i] = invec[i].len;
531 msg->invec[i].base = invec[i].base;
532 }
533
534 for (i = 0; i < out_len; i++) {
535 msg->msg.out_size[i] = outvec[i].len;
536 msg->outvec[i].base = outvec[i].base;
537 /* Out len is used to record the writed number, set 0 here again */
538 msg->outvec[i].len = 0;
539 }
540
Ken Liu505b1702020-05-29 13:19:58 +0800541 /* Use the user connect handle as the message handle */
542 msg->msg.handle = handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800543
Ken Liu505b1702020-05-29 13:19:58 +0800544 conn_handle = tfm_spm_to_handle_instance(handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800545 /* For connected handle, set rhandle to every message */
Ken Liu505b1702020-05-29 13:19:58 +0800546 if (conn_handle) {
547 msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800548 }
David Hu46603dd2019-12-11 18:05:16 +0800549
550 /* Set the private data of NSPE client caller in multi-core topology */
551 if (TFM_CLIENT_ID_IS_NS(client_id)) {
552 tfm_rpc_set_caller_data(msg, client_id);
553 }
Edison Ai764d41f2018-09-21 15:56:36 +0800554}
555
556int32_t tfm_spm_send_event(struct tfm_spm_service_t *service,
557 struct tfm_msg_body_t *msg)
558{
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800559 struct partition_t *partition = service->partition;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800560
Ken Liuf250b8b2019-12-27 16:31:24 +0800561 TFM_CORE_ASSERT(service);
562 TFM_CORE_ASSERT(msg);
Edison Ai764d41f2018-09-21 15:56:36 +0800563
Mingyang Sun73056b62020-07-03 15:18:46 +0800564 /* Add message to partition message list tail */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800565 tfm_list_add_tail(&partition->msg_list, &msg->msg_node);
Edison Ai764d41f2018-09-21 15:56:36 +0800566
567 /* Messages put. Update signals */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800568 partition->signals_asserted |= service->service_db->signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800569
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800570 tfm_event_wake(&partition->event,
571 (partition->signals_asserted & partition->signals_waiting));
Edison Ai764d41f2018-09-21 15:56:36 +0800572
David Hufb38d562019-09-23 15:58:34 +0800573 /*
574 * If it is a NS request via RPC, it is unnecessary to block current
575 * thread.
576 */
577 if (!is_tfm_rpc_msg(msg)) {
578 tfm_event_wait(&msg->ack_evnt);
579 }
Edison Ai764d41f2018-09-21 15:56:36 +0800580
581 return IPC_SUCCESS;
582}
583
Mingyang Sunf3d29892019-07-10 17:50:23 +0800584uint32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800585{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800586 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800587
Kevin Peng79c2bda2020-07-24 16:31:12 +0800588 partition = tfm_spm_get_running_partition();
589 if (partition && partition->static_data) {
590 return partition->static_data->partition_id;
591 } else {
592 return INVALID_PARTITION_ID;
593 }
Edison Ai764d41f2018-09-21 15:56:36 +0800594}
595
Summer Qin43c185d2019-10-10 15:44:42 +0800596int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800597 enum tfm_memory_access_e access,
598 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800599{
Mingyang Sund1ed6732020-08-26 15:52:21 +0800600 enum tfm_hal_status_t err;
601 uint32_t attr = 0;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800602
603 /* If len is zero, this indicates an empty buffer and base is ignored */
604 if (len == 0) {
605 return IPC_SUCCESS;
606 }
607
608 if (!buffer) {
609 return IPC_ERROR_BAD_PARAMETERS;
610 }
611
612 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
613 return IPC_ERROR_MEMORY_CHECK;
614 }
615
Summer Qin424d4db2019-03-25 14:09:51 +0800616 if (access == TFM_MEMORY_ACCESS_RW) {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800617 attr |= (TFM_HAL_ACCESS_READABLE | TFM_HAL_ACCESS_WRITABLE);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800618 } else {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800619 attr |= TFM_HAL_ACCESS_READABLE;
Summer Qin424d4db2019-03-25 14:09:51 +0800620 }
Mingyang Sund1ed6732020-08-26 15:52:21 +0800621
622 if (privileged == TFM_PARTITION_UNPRIVILEGED_MODE) {
623 attr |= TFM_HAL_ACCESS_UNPRIVILEGED;
624 } else {
625 attr &= ~TFM_HAL_ACCESS_UNPRIVILEGED;
626 }
627
628 if (ns_caller) {
629 attr |= TFM_HAL_ACCESS_NS;
630 }
631
632 err = tfm_hal_memory_has_access((uintptr_t)buffer, len, attr);
633
634 if (err == TFM_HAL_SUCCESS) {
Summer Qin424d4db2019-03-25 14:09:51 +0800635 return IPC_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800636 }
637
638 return IPC_ERROR_MEMORY_CHECK;
639}
640
Ken Liuce2692d2020-02-11 12:39:36 +0800641uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800642{
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800643 uint32_t i, j, num;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800644 struct partition_t *partition;
Summer Qin66f1e032020-01-06 15:40:03 +0800645 struct tfm_core_thread_t *pth, *p_ns_entry_thread = NULL;
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100646 const struct tfm_spm_partition_platform_data_t **platform_data_p;
Edison Ai764d41f2018-09-21 15:56:36 +0800647
648 tfm_pool_init(conn_handle_pool,
649 POOL_BUFFER_SIZE(conn_handle_pool),
650 sizeof(struct tfm_conn_handle_t),
651 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800652
653 /* Init partition first for it will be used when init service */
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200654 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800655 partition = &g_spm_partition_db.partitions[i];
Edison Aif0501702019-10-11 14:36:42 +0800656
Kevin Peng79c2bda2020-07-24 16:31:12 +0800657 if (!partition || !partition->memory_data || !partition->static_data) {
658 tfm_core_panic();
659 }
660
661 if (!(partition->static_data->partition_flags & SPM_PART_FLAG_IPC)) {
662 tfm_core_panic();
663 }
664
Edison Aif0501702019-10-11 14:36:42 +0800665 /* Check if the PSA framework version matches. */
666 if (partition->static_data->psa_framework_version !=
667 PSA_FRAMEWORK_VERSION) {
Kevin Peng79c2bda2020-07-24 16:31:12 +0800668 ERROR_MSG("Warning: PSA Framework Verison does not match!");
Edison Aif0501702019-10-11 14:36:42 +0800669 continue;
670 }
671
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100672 platform_data_p = partition->platform_data_list;
673 if (platform_data_p != NULL) {
674 while ((*platform_data_p) != NULL) {
Edison Ai6be3df12020-02-14 22:14:33 +0800675 if (tfm_spm_hal_configure_default_isolation(i,
676 *platform_data_p) != TFM_PLAT_ERR_SUCCESS) {
677 tfm_core_panic();
678 }
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100679 ++platform_data_p;
680 }
681 }
682
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800683 /* Add PSA_DOORBELL signal to assigned_signals */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800684 partition->signals_allowed |= PSA_DOORBELL;
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800685
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800686 /* TODO: This can be optimized by generating the assigned signal
687 * in code generation time.
688 */
689 for (j = 0; j < tfm_core_irq_signals_count; ++j) {
690 if (tfm_core_irq_signals[j].partition_id ==
691 partition->static_data->partition_id) {
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800692 partition->signals_allowed |=
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800693 tfm_core_irq_signals[j].signal_value;
694 }
695 }
696
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800697 tfm_event_init(&partition->event);
698 tfm_list_init(&partition->msg_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800699
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800700 pth = &partition->sp_thread;
Edison Ai764d41f2018-09-21 15:56:36 +0800701 if (!pth) {
Edison Ai9059ea02019-11-28 13:46:14 +0800702 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800703 }
704
Summer Qin66f1e032020-01-06 15:40:03 +0800705 tfm_core_thrd_init(pth,
Kevin Peng79c2bda2020-07-24 16:31:12 +0800706 (tfm_core_thrd_entry_t)
707 partition->static_data->partition_init,
Summer Qin66f1e032020-01-06 15:40:03 +0800708 NULL,
Kevin Peng79c2bda2020-07-24 16:31:12 +0800709 (uintptr_t)partition->memory_data->stack_top,
710 (uintptr_t)partition->memory_data->stack_bottom);
Edison Ai788bae22019-02-18 17:38:59 +0800711
Kevin Peng79c2bda2020-07-24 16:31:12 +0800712 pth->prior = partition->static_data->partition_priority;
Edison Ai764d41f2018-09-21 15:56:36 +0800713
Ken Liu490281d2019-12-30 15:55:26 +0800714 if (partition->static_data->partition_id == TFM_SP_NON_SECURE_ID) {
715 p_ns_entry_thread = pth;
Ken Liu5248af22019-12-29 12:47:13 +0800716 pth->param = (void *)tfm_spm_hal_get_ns_entry_point();
Ken Liu490281d2019-12-30 15:55:26 +0800717 }
718
Edison Ai764d41f2018-09-21 15:56:36 +0800719 /* Kick off */
Summer Qin66f1e032020-01-06 15:40:03 +0800720 if (tfm_core_thrd_start(pth) != THRD_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800721 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800722 }
723 }
724
725 /* Init Service */
Summer Qind99509f2019-08-02 17:36:58 +0800726 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
Edison Ai764d41f2018-09-21 15:56:36 +0800727 for (i = 0; i < num; i++) {
Summer Qine578c5b2019-08-16 16:42:16 +0800728 service[i].service_db = &service_db[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800729 partition =
Summer Qine578c5b2019-08-16 16:42:16 +0800730 tfm_spm_get_partition_by_id(service[i].service_db->partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800731 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800732 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800733 }
Summer Qind99509f2019-08-02 17:36:58 +0800734 service[i].partition = partition;
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800735 partition->signals_allowed |= service[i].service_db->signal;
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800736
Summer Qind99509f2019-08-02 17:36:58 +0800737 tfm_list_init(&service[i].handle_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800738 }
739
Ken Liu483f5da2019-04-24 10:45:21 +0800740 /*
741 * All threads initialized, start the scheduler.
742 *
743 * NOTE:
Ken Liu490281d2019-12-30 15:55:26 +0800744 * It is worthy to give the thread object to scheduler if the background
745 * context belongs to one of the threads. Here the background thread is the
746 * initialization thread who calls SPM SVC, which re-uses the non-secure
747 * entry thread's stack. After SPM initialization is done, this stack is
748 * cleaned up and the background context is never going to return. Tell
749 * the scheduler that the current thread is non-secure entry thread.
Ken Liu483f5da2019-04-24 10:45:21 +0800750 */
Summer Qin66f1e032020-01-06 15:40:03 +0800751 tfm_core_thrd_start_scheduler(p_ns_entry_thread);
Ken Liuce2692d2020-02-11 12:39:36 +0800752
Summer Qind2ad7e72020-01-06 18:16:35 +0800753 return p_ns_entry_thread->arch_ctx.lr;
Edison Ai764d41f2018-09-21 15:56:36 +0800754}
Ken Liu2d175172019-03-21 17:08:41 +0800755
Summer Qind2ad7e72020-01-06 18:16:35 +0800756void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx)
Ken Liu2d175172019-03-21 17:08:41 +0800757{
Kevin Peng25b190b2020-10-30 17:10:45 +0800758#if TFM_LVL != 1
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800759 struct partition_t *p_next_partition;
Ken Liu2d175172019-03-21 17:08:41 +0800760 uint32_t is_privileged;
761#endif
Summer Qin66f1e032020-01-06 15:40:03 +0800762 struct tfm_core_thread_t *pth_next = tfm_core_thrd_get_next_thread();
763 struct tfm_core_thread_t *pth_curr = tfm_core_thrd_get_curr_thread();
Ken Liu2d175172019-03-21 17:08:41 +0800764
Mate Toth-Pal32b2ccd2019-04-26 10:00:16 +0200765 if (pth_next != NULL && pth_curr != pth_next) {
Kevin Peng25b190b2020-10-30 17:10:45 +0800766#if TFM_LVL != 1
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800767 p_next_partition = TFM_GET_CONTAINER_PTR(pth_next,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800768 struct partition_t,
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800769 sp_thread);
Ken Liu2d175172019-03-21 17:08:41 +0800770
Summer Qin423dbef2019-08-22 15:59:35 +0800771 if (p_next_partition->static_data->partition_flags &
Ken Liu2d175172019-03-21 17:08:41 +0800772 SPM_PART_FLAG_PSA_ROT) {
773 is_privileged = TFM_PARTITION_PRIVILEGED_MODE;
774 } else {
775 is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE;
776 }
777
778 tfm_spm_partition_change_privilege(is_privileged);
Kevin Peng25b190b2020-10-30 17:10:45 +0800779#if TFM_LVL == 3
780 /*
781 * FIXME: To implement isolations among partitions in isolation level 3,
782 * each partition needs to run in unprivileged mode. Currently some
783 * PRoTs cannot work in unprivileged mode, make them privileged now.
784 */
785 if (is_privileged == TFM_PARTITION_UNPRIVILEGED_MODE) {
786 /* FIXME: only MPU-based implementations are supported currently */
787 if (tfm_hal_mpu_update_partition_boundary(
788 p_next_partition->memory_data->data_start,
789 p_next_partition->memory_data->data_limit)
790 != TFM_HAL_SUCCESS) {
791 tfm_core_panic();
792 }
793 }
794#endif /* TFM_LVL == 3 */
795#endif /* TFM_LVL != 1 */
Mate Toth-Palc430b992019-05-09 21:01:14 +0200796
Summer Qind2ad7e72020-01-06 18:16:35 +0800797 tfm_core_thrd_switch_context(p_actx, pth_curr, pth_next);
Ken Liu2d175172019-03-21 17:08:41 +0800798 }
David Hufb38d562019-09-23 15:58:34 +0800799
800 /*
801 * Handle pending mailbox message from NS in multi-core topology.
802 * Empty operation on single Armv8-M platform.
803 */
804 tfm_rpc_client_call_handler();
Ken Liu2d175172019-03-21 17:08:41 +0800805}
Mingyang Sund44522a2020-01-16 16:48:37 +0800806
Summer Qin02f7f072020-08-24 16:02:54 +0800807void update_caller_outvec_len(struct tfm_msg_body_t *msg)
Mingyang Sund44522a2020-01-16 16:48:37 +0800808{
809 uint32_t i;
810
811 /*
812 * FixeMe: abstract these part into dedicated functions to avoid
813 * accessing thread context in psa layer
814 */
815 /* If it is a NS request via RPC, the owner of this message is not set */
816 if (!is_tfm_rpc_msg(msg)) {
817 TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK);
818 }
819
820 for (i = 0; i < PSA_MAX_IOVEC; i++) {
821 if (msg->msg.out_size[i] == 0) {
822 continue;
823 }
824
825 TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
826
827 msg->caller_outvec[i].len = msg->outvec[i].len;
828 }
829}
830
Summer Qin02f7f072020-08-24 16:02:54 +0800831void notify_with_signal(int32_t partition_id, psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800832{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800833 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800834
835 /*
836 * The value of partition_id must be greater than zero as the target of
837 * notification must be a Secure Partition, providing a Non-secure
838 * Partition ID is a fatal error.
839 */
840 if (!TFM_CLIENT_ID_IS_S(partition_id)) {
841 tfm_core_panic();
842 }
843
844 /*
845 * It is a fatal error if partition_id does not correspond to a Secure
846 * Partition.
847 */
848 partition = tfm_spm_get_partition_by_id(partition_id);
849 if (!partition) {
850 tfm_core_panic();
851 }
852
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800853 partition->signals_asserted |= signal;
Mingyang Sund44522a2020-01-16 16:48:37 +0800854
855 /*
856 * The target partition may be blocked with waiting for signals after
857 * called psa_wait(). Set the return value with the available signals
858 * before wake it up with tfm_event_signal().
859 */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800860 tfm_event_wake(&partition->event,
861 partition->signals_asserted & partition->signals_waiting);
Mingyang Sund44522a2020-01-16 16:48:37 +0800862}
863
Mingyang Sund44522a2020-01-16 16:48:37 +0800864/**
865 * \brief assert signal for a given IRQ line.
866 *
867 * \param[in] partition_id The ID of the partition which handles this IRQ
868 * \param[in] signal The signal associated with this IRQ
869 * \param[in] irq_line The number of the IRQ line
870 *
871 * \retval void Success.
872 * \retval "Does not return" Partition ID is invalid
873 */
874void tfm_irq_handler(uint32_t partition_id, psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +0100875 IRQn_Type irq_line)
Mingyang Sund44522a2020-01-16 16:48:37 +0800876{
877 tfm_spm_hal_disable_irq(irq_line);
878 notify_with_signal(partition_id, signal);
879}
880
Summer Qin02f7f072020-08-24 16:02:54 +0800881int32_t get_irq_line_for_signal(int32_t partition_id,
882 psa_signal_t signal,
883 IRQn_Type *irq_line)
Mingyang Sund44522a2020-01-16 16:48:37 +0800884{
885 size_t i;
886
887 for (i = 0; i < tfm_core_irq_signals_count; ++i) {
888 if (tfm_core_irq_signals[i].partition_id == partition_id &&
889 tfm_core_irq_signals[i].signal_value == signal) {
890 *irq_line = tfm_core_irq_signals[i].irq_line;
891 return IPC_SUCCESS;
892 }
893 }
894 return IPC_ERROR_GENERIC;
895}
896
Mingyang Sund44522a2020-01-16 16:48:37 +0800897void tfm_spm_enable_irq(uint32_t *args)
898{
899 struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args;
900 psa_signal_t irq_signal = svc_ctx->r0;
TTornblomfaf74f52020-03-04 17:56:27 +0100901 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +0800902 int32_t ret;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800903 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800904
905 /* It is a fatal error if passed signal indicates more than one signals. */
906 if (!tfm_is_one_bit_set(irq_signal)) {
907 tfm_core_panic();
908 }
909
910 partition = tfm_spm_get_running_partition();
911 if (!partition) {
912 tfm_core_panic();
913 }
914
915 ret = get_irq_line_for_signal(partition->static_data->partition_id,
916 irq_signal, &irq_line);
917 /* It is a fatal error if passed signal is not an interrupt signal. */
918 if (ret != IPC_SUCCESS) {
919 tfm_core_panic();
920 }
921
922 tfm_spm_hal_enable_irq(irq_line);
923}
924
925void tfm_spm_disable_irq(uint32_t *args)
926{
927 struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args;
928 psa_signal_t irq_signal = svc_ctx->r0;
TTornblomfaf74f52020-03-04 17:56:27 +0100929 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +0800930 int32_t ret;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800931 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800932
933 /* It is a fatal error if passed signal indicates more than one signals. */
934 if (!tfm_is_one_bit_set(irq_signal)) {
935 tfm_core_panic();
936 }
937
938 partition = tfm_spm_get_running_partition();
939 if (!partition) {
940 tfm_core_panic();
941 }
942
943 ret = get_irq_line_for_signal(partition->static_data->partition_id,
944 irq_signal, &irq_line);
945 /* It is a fatal error if passed signal is not an interrupt signal. */
946 if (ret != IPC_SUCCESS) {
947 tfm_core_panic();
948 }
949
950 tfm_spm_hal_disable_irq(irq_line);
951}
952
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800953void tfm_spm_validate_caller(struct partition_t *p_cur_sp, uint32_t *p_ctx,
954 uint32_t exc_return, bool ns_caller)
Mingyang Sund44522a2020-01-16 16:48:37 +0800955{
956 uintptr_t stacked_ctx_pos;
957
958 if (ns_caller) {
959 /*
960 * The background IRQ can't be supported, since if SP is executing,
961 * the preempted context of SP can be different with the one who
962 * preempts veneer.
963 */
964 if (p_cur_sp->static_data->partition_id != TFM_SP_NON_SECURE_ID) {
965 tfm_core_panic();
966 }
967
968 /*
969 * It is non-secure caller, check if veneer stack contains
970 * multiple contexts.
971 */
972 stacked_ctx_pos = (uintptr_t)p_ctx +
973 sizeof(struct tfm_state_context_t) +
Ken Liu05e13ba2020-07-25 10:31:33 +0800974 TFM_STACK_SEALED_SIZE;
Mingyang Sund44522a2020-01-16 16:48:37 +0800975
976 if (is_stack_alloc_fp_space(exc_return)) {
977#if defined (__FPU_USED) && (__FPU_USED == 1U)
978 if (FPU->FPCCR & FPU_FPCCR_TS_Msk) {
979 stacked_ctx_pos += TFM_ADDTIONAL_FP_CONTEXT_WORDS *
980 sizeof(uint32_t);
981 }
982#endif
983 stacked_ctx_pos += TFM_BASIC_FP_CONTEXT_WORDS * sizeof(uint32_t);
984 }
985
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800986 if (stacked_ctx_pos != p_cur_sp->sp_thread.stk_top) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800987 tfm_core_panic();
988 }
989 } else if (p_cur_sp->static_data->partition_id <= 0) {
990 tfm_core_panic();
991 }
992}
Summer Qin830c5542020-02-14 13:44:20 +0800993
994void tfm_spm_request_handler(const struct tfm_state_context_t *svc_ctx)
995{
996 uint32_t *res_ptr = (uint32_t *)&svc_ctx->r0;
997 uint32_t running_partition_flags = 0;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800998 const struct partition_t *partition = NULL;
Summer Qin830c5542020-02-14 13:44:20 +0800999
1000 /* Check permissions on request type basis */
1001
1002 switch (svc_ctx->r0) {
1003 case TFM_SPM_REQUEST_RESET_VOTE:
1004 partition = tfm_spm_get_running_partition();
1005 if (!partition) {
1006 tfm_core_panic();
1007 }
1008 running_partition_flags = partition->static_data->partition_flags;
1009
1010 /* Currently only PSA Root of Trust services are allowed to make Reset
1011 * vote request
1012 */
1013 if ((running_partition_flags & SPM_PART_FLAG_PSA_ROT) == 0) {
1014 *res_ptr = (uint32_t)TFM_ERROR_GENERIC;
1015 }
1016
1017 /* FixMe: this is a placeholder for checks to be performed before
1018 * allowing execution of reset
1019 */
1020 *res_ptr = (uint32_t)TFM_SUCCESS;
1021
1022 break;
1023 default:
1024 *res_ptr = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
1025 }
1026}
Mingyang Sunbd7ceb52020-06-11 16:53:03 +08001027
1028enum spm_err_t tfm_spm_db_init(void)
1029{
1030 uint32_t i;
1031
1032 /* This function initialises partition db */
1033
1034 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
1035 g_spm_partition_db.partitions[i].static_data = &static_data_list[i];
1036 g_spm_partition_db.partitions[i].platform_data_list =
1037 platform_data_list_list[i];
1038 g_spm_partition_db.partitions[i].memory_data = &memory_data_list[i];
1039 }
1040 g_spm_partition_db.is_init = 1;
1041
1042 return SPM_ERR_OK;
1043}