blob: 824cd0476a0682712ce588a0203d0caf8e6b8e8d [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 "psa/lifecycle.h"
13#include "tfm_thread.h"
Edison Ai764d41f2018-09-21 15:56:36 +080014#include "tfm_wait.h"
Summer Qin5fdcf632020-06-22 16:49:24 +080015#include "utilities.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080016#include "tfm_internal_defines.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080017#include "tfm_spm_hal.h"
18#include "tfm_irq_list.h"
19#include "tfm_api.h"
20#include "tfm_secure_api.h"
21#include "tfm_memory_utils.h"
Mingyang 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"
25#include "spm_psa_client_call.h"
26#include "tfm_rpc.h"
27#include "tfm_internal.h"
28#include "tfm_core_trustzone.h"
29#include "tfm_core_mem_check.h"
Edison Ai764d41f2018-09-21 15:56:36 +080030#include "tfm_list.h"
31#include "tfm_pools.h"
Mingyang Sunbd7ceb52020-06-11 16:53:03 +080032#include "region.h"
Summer Qin2bfd2a02018-09-26 17:10:41 +080033#include "region_defs.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080034#include "spm_partition_defs.h"
35#include "psa_manifest/pid.h"
Summer Qin5fdcf632020-06-22 16:49:24 +080036#include "tfm/tfm_spm_services.h"
Edison Ai764d41f2018-09-21 15:56:36 +080037
Ken Liu1f345b02020-05-30 21:11:05 +080038#include "secure_fw/partitions/tfm_service_list.inc"
Mingyang Sunbd7ceb52020-06-11 16:53:03 +080039#include "tfm_spm_db_ipc.inc"
Summer Qind99509f2019-08-02 17:36:58 +080040
41/* Extern service variable */
42extern struct tfm_spm_service_t service[];
Summer Qine578c5b2019-08-16 16:42:16 +080043extern const struct tfm_spm_service_db_t service_db[];
Summer Qind99509f2019-08-02 17:36:58 +080044
Edison Ai764d41f2018-09-21 15:56:36 +080045/* Pools */
46TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t),
47 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +080048
Mingyang Sund44522a2020-01-16 16:48:37 +080049void tfm_irq_handler(uint32_t partition_id, psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +010050 IRQn_Type irq_line);
Mingyang Sund44522a2020-01-16 16:48:37 +080051
52#include "tfm_secure_irq_handlers_ipc.inc"
Edison Ai764d41f2018-09-21 15:56:36 +080053
Summer Qin373feb12020-03-27 15:35:33 +080054/*********************** Connection handle conversion APIs *******************/
55
56/* Set a minimal value here for feature expansion. */
57#define CLIENT_HANDLE_VALUE_MIN 32
58
59#define CONVERSION_FACTOR_BITOFFSET 3
60#define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET)
61/* Set 32 as the maximum */
62#define CONVERSION_FACTOR_VALUE_MAX 0x20
63
64#if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX
65#error "CONVERSION FACTOR OUT OF RANGE"
66#endif
67
68static uint32_t loop_index;
69
70/*
71 * A handle instance psa_handle_t allocated inside SPM is actually a memory
72 * address among the handle pool. Return this handle to the client directly
73 * exposes information of secure memory address. In this case, converting the
74 * handle into another value does not represent the memory address to avoid
75 * exposing secure memory directly to clients.
76 *
77 * This function converts the handle instance into another value by scaling the
78 * handle in pool offset, the converted value is named as a user handle.
79 *
80 * The formula:
81 * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE +
82 * CLIENT_HANDLE_VALUE_MIN + loop_index
83 * where:
84 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
85 * exceed CONVERSION_FACTOR_VALUE_MAX.
86 *
87 * handle_instance in RANGE[POOL_START, POOL_END]
88 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
89 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
90 *
91 * note:
92 * loop_index is used to promise same handle instance is converted into
93 * different user handles in short time.
94 */
Ken Liu505b1702020-05-29 13:19:58 +080095psa_handle_t tfm_spm_to_user_handle(struct tfm_conn_handle_t *handle_instance)
Summer Qin373feb12020-03-27 15:35:33 +080096{
97 psa_handle_t user_handle;
98
99 loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE;
100 user_handle = (psa_handle_t)((((uintptr_t)handle_instance -
101 (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) +
102 CLIENT_HANDLE_VALUE_MIN + loop_index);
103
104 return user_handle;
105}
106
107/*
108 * This function converts a user handle into a corresponded handle instance.
109 * The converted value is validated before returning, an invalid handle instance
110 * is returned as NULL.
111 *
112 * The formula:
113 * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) /
114 * CONVERSION_FACTOR_VALUE) + POOL_START
115 * where:
116 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
117 * exceed CONVERSION_FACTOR_VALUE_MAX.
118 *
119 * handle_instance in RANGE[POOL_START, POOL_END]
120 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
121 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
122 */
123struct tfm_conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle)
124{
125 struct tfm_conn_handle_t *handle_instance;
126
127 if (user_handle == PSA_NULL_HANDLE) {
128 return NULL;
129 }
130
131 handle_instance = (struct tfm_conn_handle_t *)((((uintptr_t)user_handle -
132 CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) +
133 (uintptr_t)conn_handle_pool);
134
135 return handle_instance;
136}
137
Edison Ai764d41f2018-09-21 15:56:36 +0800138/* Service handle management functions */
Summer Qin630c76b2020-05-20 10:32:58 +0800139struct tfm_conn_handle_t *tfm_spm_create_conn_handle(
140 struct tfm_spm_service_t *service,
Summer Qin1ce712a2019-10-14 18:04:05 +0800141 int32_t client_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800142{
Edison Ai9cc26242019-08-06 11:28:04 +0800143 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800144
Ken Liuf250b8b2019-12-27 16:31:24 +0800145 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800146
147 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +0800148 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
149 if (!p_handle) {
Summer Qin630c76b2020-05-20 10:32:58 +0800150 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800151 }
152
Edison Ai9cc26242019-08-06 11:28:04 +0800153 p_handle->service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800154 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +0800155 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800156
157 /* Add handle node to list for next psa functions */
Edison Ai9cc26242019-08-06 11:28:04 +0800158 tfm_list_add_tail(&service->handle_list, &p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800159
Summer Qin630c76b2020-05-20 10:32:58 +0800160 return p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800161}
162
Summer Qin630c76b2020-05-20 10:32:58 +0800163int32_t tfm_spm_validate_conn_handle(
164 const struct tfm_conn_handle_t *conn_handle,
165 int32_t client_id)
Summer Qin1ce712a2019-10-14 18:04:05 +0800166{
167 /* Check the handle address is validated */
168 if (is_valid_chunk_data_in_pool(conn_handle_pool,
169 (uint8_t *)conn_handle) != true) {
170 return IPC_ERROR_GENERIC;
171 }
172
173 /* Check the handle caller is correct */
Summer Qin630c76b2020-05-20 10:32:58 +0800174 if (conn_handle->client_id != client_id) {
Summer Qin1ce712a2019-10-14 18:04:05 +0800175 return IPC_ERROR_GENERIC;
176 }
177
178 return IPC_SUCCESS;
179}
180
Mingyang Sund44522a2020-01-16 16:48:37 +0800181/**
182 * \brief Free connection handle which not used anymore.
183 *
184 * \param[in] service Target service context pointer
185 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800186 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800187 *
188 * \retval IPC_SUCCESS Success
189 * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
190 * \retval "Does not return" Panic for not find service by handle
191 */
192static int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800193 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800194{
Ken Liuf250b8b2019-12-27 16:31:24 +0800195 TFM_CORE_ASSERT(service);
Summer Qin630c76b2020-05-20 10:32:58 +0800196 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800197
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200198 /* Clear magic as the handler is not used anymore */
Summer Qin630c76b2020-05-20 10:32:58 +0800199 conn_handle->internal_msg.magic = 0;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200200
Edison Ai764d41f2018-09-21 15:56:36 +0800201 /* Remove node from handle list */
Summer Qin630c76b2020-05-20 10:32:58 +0800202 tfm_list_del_node(&conn_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800203
204 /* Back handle buffer to pool */
Summer Qin630c76b2020-05-20 10:32:58 +0800205 tfm_pool_free(conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800206 return IPC_SUCCESS;
207}
208
Mingyang Sund44522a2020-01-16 16:48:37 +0800209/**
210 * \brief Set reverse handle value for connection.
211 *
212 * \param[in] service Target service context pointer
213 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800214 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800215 * \param[in] rhandle rhandle need to save
216 *
217 * \retval IPC_SUCCESS Success
218 * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
219 * \retval "Does not return" Panic for not find handle node
220 */
221static int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800222 struct tfm_conn_handle_t *conn_handle,
Mingyang Sund44522a2020-01-16 16:48:37 +0800223 void *rhandle)
Edison Ai764d41f2018-09-21 15:56:36 +0800224{
Ken Liuf250b8b2019-12-27 16:31:24 +0800225 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800226 /* Set reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800227 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800228
Summer Qin630c76b2020-05-20 10:32:58 +0800229 conn_handle->rhandle = rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800230 return IPC_SUCCESS;
231}
232
Mingyang Sund44522a2020-01-16 16:48:37 +0800233/**
234 * \brief Get reverse handle value from connection hanlde.
235 *
236 * \param[in] service Target service context pointer
237 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800238 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800239 *
240 * \retval void * Success
241 * \retval "Does not return" Panic for those:
242 * service pointer are NULL
243 * hanlde is \ref PSA_NULL_HANDLE
244 * handle node does not be found
245 */
246static void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800247 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800248{
Ken Liuf250b8b2019-12-27 16:31:24 +0800249 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800250 /* Get reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800251 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800252
Summer Qin630c76b2020-05-20 10:32:58 +0800253 return conn_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800254}
255
256/* Partition management functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800257
258/**
Mingyang Sun73056b62020-07-03 15:18:46 +0800259 * \brief Get the msg context by signal.
Mingyang Sund44522a2020-01-16 16:48:37 +0800260 *
261 * \param[in] partition Partition context pointer
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800262 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800263 * \param[in] signal Signal associated with inputs to the Secure
264 * Partition, \ref psa_signal_t
265 *
266 * \retval NULL Failed
267 * \retval "Not NULL" Target service context pointer,
Mingyang Sun73056b62020-07-03 15:18:46 +0800268 * \ref tfm_msg_body_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800269 */
Mingyang Sun73056b62020-07-03 15:18:46 +0800270static struct tfm_msg_body_t *
271 tfm_spm_get_msg_by_signal(struct partition_t *partition,
272 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800273{
274 struct tfm_list_node_t *node, *head;
Mingyang Sun73056b62020-07-03 15:18:46 +0800275 struct tfm_msg_body_t *tmp_msg, *msg = NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800276
Ken Liuf250b8b2019-12-27 16:31:24 +0800277 TFM_CORE_ASSERT(partition);
Edison Ai764d41f2018-09-21 15:56:36 +0800278
Mingyang Sun73056b62020-07-03 15:18:46 +0800279 head = &partition->runtime_data.msg_list;
280
281 if (tfm_list_is_empty(head)) {
282 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800283 }
284
Mingyang Sun73056b62020-07-03 15:18:46 +0800285 /*
286 * There may be multiple messages for this RoT Service signal, do not clear
287 * partition mask until no remaining message. Search may be optimized.
288 */
Edison Ai764d41f2018-09-21 15:56:36 +0800289 TFM_LIST_FOR_EACH(node, head) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800290 tmp_msg = TFM_GET_CONTAINER_PTR(node, struct tfm_msg_body_t, msg_node);
291 if (tmp_msg->service->service_db->signal == signal && msg) {
292 return msg;
293 } else if (tmp_msg->service->service_db->signal == signal) {
294 msg = tmp_msg;
295 tfm_list_del_node(node);
Edison Ai764d41f2018-09-21 15:56:36 +0800296 }
297 }
Mingyang Sun73056b62020-07-03 15:18:46 +0800298
299 partition->runtime_data.signals &= ~signal;
300
301 return msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800302}
303
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800304/**
305 * \brief Returns the index of the partition with the given partition ID.
306 *
307 * \param[in] partition_id Partition id
308 *
309 * \return the partition idx if partition_id is valid,
310 * \ref SPM_INVALID_PARTITION_IDX othervise
311 */
312static uint32_t get_partition_idx(uint32_t partition_id)
313{
314 uint32_t i;
315
316 if (partition_id == INVALID_PARTITION_ID) {
317 return SPM_INVALID_PARTITION_IDX;
318 }
319
320 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
321 if (g_spm_partition_db.partitions[i].static_data->partition_id ==
322 partition_id) {
323 return i;
324 }
325 }
326 return SPM_INVALID_PARTITION_IDX;
327}
328
329/**
330 * \brief Get the flags associated with a partition
331 *
332 * \param[in] partition_idx Partition index
333 *
334 * \return Flags associated with the partition
335 *
336 * \note This function doesn't check if partition_idx is valid.
337 */
338static uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
339{
340 return g_spm_partition_db.partitions[partition_idx].static_data->
341 partition_flags;
342}
343
344#if TFM_LVL != 1
345/**
346 * \brief Change the privilege mode for partition thread mode.
347 *
348 * \param[in] privileged Privileged mode,
349 * \ref TFM_PARTITION_PRIVILEGED_MODE
350 * and \ref TFM_PARTITION_UNPRIVILEGED_MODE
351 *
352 * \note Barrier instructions are not called by this function, and if
353 * it is called in thread mode, it might be necessary to call
354 * them after this function returns.
355 */
356static void tfm_spm_partition_change_privilege(uint32_t privileged)
357{
358 CONTROL_Type ctrl;
359
360 ctrl.w = __get_CONTROL();
361
362 if (privileged == TFM_PARTITION_PRIVILEGED_MODE) {
363 ctrl.b.nPRIV = 0;
364 } else {
365 ctrl.b.nPRIV = 1;
366 }
367
368 __set_CONTROL(ctrl.w);
369}
370#endif /* if(TFM_LVL != 1) */
371
372uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
373{
374 return g_spm_partition_db.partitions[partition_idx].static_data->
375 partition_id;
376}
377
378uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
379{
380 if (partition_flags & SPM_PART_FLAG_PSA_ROT) {
381 return TFM_PARTITION_PRIVILEGED_MODE;
382 } else {
383 return TFM_PARTITION_UNPRIVILEGED_MODE;
384 }
385}
386
387bool tfm_is_partition_privileged(uint32_t partition_idx)
388{
389 uint32_t flags = tfm_spm_partition_get_flags(partition_idx);
390
391 return tfm_spm_partition_get_privileged_mode(flags) ==
392 TFM_PARTITION_PRIVILEGED_MODE;
393}
394
Edison Ai764d41f2018-09-21 15:56:36 +0800395struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid)
396{
Summer Qin2fca1c82020-03-20 14:37:55 +0800397 uint32_t i, num;
Edison Ai764d41f2018-09-21 15:56:36 +0800398
Summer Qin2fca1c82020-03-20 14:37:55 +0800399 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
400 for (i = 0; i < num; i++) {
401 if (service[i].service_db->sid == sid) {
402 return &service[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800403 }
404 }
Summer Qin2fca1c82020-03-20 14:37:55 +0800405
Edison Ai764d41f2018-09-21 15:56:36 +0800406 return NULL;
407}
408
Mingyang Sund44522a2020-01-16 16:48:37 +0800409/**
410 * \brief Get the partition context by partition ID.
411 *
412 * \param[in] partition_id Partition identity
413 *
414 * \retval NULL Failed
415 * \retval "Not NULL" Target partition context pointer,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800416 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800417 */
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800418static struct partition_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800419{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800420 uint32_t idx = get_partition_idx(partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800421
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800422 if (idx != SPM_INVALID_PARTITION_IDX) {
423 return &(g_spm_partition_db.partitions[idx]);
Edison Ai764d41f2018-09-21 15:56:36 +0800424 }
425 return NULL;
426}
427
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800428struct partition_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800429{
Kevin Peng79c2bda2020-07-24 16:31:12 +0800430 struct tfm_core_thread_t *pth = tfm_core_thrd_get_curr_thread();
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800431 struct partition_t *partition;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800432 struct spm_partition_runtime_data_t *rt_data;
Edison Ai764d41f2018-09-21 15:56:36 +0800433
Kevin Peng79c2bda2020-07-24 16:31:12 +0800434 rt_data = TFM_GET_CONTAINER_PTR(pth, struct spm_partition_runtime_data_t,
435 sp_thrd);
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800436 partition = TFM_GET_CONTAINER_PTR(rt_data, struct partition_t,
Kevin Peng79c2bda2020-07-24 16:31:12 +0800437 runtime_data);
438 return partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800439}
440
441int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530442 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800443{
Ken Liuf250b8b2019-12-27 16:31:24 +0800444 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800445
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530446 switch (service->service_db->version_policy) {
Edison Ai764d41f2018-09-21 15:56:36 +0800447 case TFM_VERSION_POLICY_RELAXED:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530448 if (version > service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800449 return IPC_ERROR_VERSION;
450 }
451 break;
452 case TFM_VERSION_POLICY_STRICT:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530453 if (version != service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800454 return IPC_ERROR_VERSION;
455 }
456 break;
457 default:
458 return IPC_ERROR_VERSION;
459 }
460 return IPC_SUCCESS;
461}
462
Edison Aie728fbf2019-11-13 09:37:12 +0800463int32_t tfm_spm_check_authorization(uint32_t sid,
464 struct tfm_spm_service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800465 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800466{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800467 struct partition_t *partition = NULL;
Edison Aie728fbf2019-11-13 09:37:12 +0800468 int32_t i;
469
Ken Liuf250b8b2019-12-27 16:31:24 +0800470 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800471
472 if (ns_caller) {
473 if (!service->service_db->non_secure_client) {
474 return IPC_ERROR_GENERIC;
475 }
476 } else {
477 partition = tfm_spm_get_running_partition();
478 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800479 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800480 }
481
482 for (i = 0; i < partition->static_data->dependencies_num; i++) {
483 if (partition->static_data->p_dependencies[i] == sid) {
484 break;
485 }
486 }
487
488 if (i == partition->static_data->dependencies_num) {
489 return IPC_ERROR_GENERIC;
490 }
491 }
492 return IPC_SUCCESS;
493}
494
Edison Ai764d41f2018-09-21 15:56:36 +0800495/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800496
497/**
498 * \brief Get message context by message handle.
499 *
500 * \param[in] msg_handle Message handle which is a reference generated
501 * by the SPM to a specific message.
502 *
503 * \return The message body context pointer
504 * \ref tfm_msg_body_t structures
505 */
506static struct tfm_msg_body_t *
507 tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800508{
509 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200510 * The message handler passed by the caller is considered invalid in the
511 * following cases:
512 * 1. Not a valid message handle. (The address of a message is not the
513 * address of a possible handle from the pool
514 * 2. Handle not belongs to the caller partition (The handle is either
515 * unused, or owned by anither partition)
516 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800517 */
Ken Liu505b1702020-05-29 13:19:58 +0800518 struct tfm_msg_body_t *p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800519 uint32_t partition_id;
Ken Liu505b1702020-05-29 13:19:58 +0800520 struct tfm_conn_handle_t *p_conn_handle =
521 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200522
523 if (is_valid_chunk_data_in_pool(
Ken Liu505b1702020-05-29 13:19:58 +0800524 conn_handle_pool, (uint8_t *)p_conn_handle) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800525 return NULL;
526 }
527
Ken Liu505b1702020-05-29 13:19:58 +0800528 p_msg = &p_conn_handle->internal_msg;
529
Edison Ai764d41f2018-09-21 15:56:36 +0800530 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200531 * Check that the magic number is correct. This proves that the message
532 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800533 */
Ken Liu505b1702020-05-29 13:19:58 +0800534 if (p_msg->magic != TFM_MSG_MAGIC) {
Edison Ai764d41f2018-09-21 15:56:36 +0800535 return NULL;
536 }
537
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200538 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800539 partition_id = tfm_spm_partition_get_running_partition_id();
Ken Liu505b1702020-05-29 13:19:58 +0800540 if (partition_id != p_msg->service->partition->static_data->partition_id) {
Edison Ai764d41f2018-09-21 15:56:36 +0800541 return NULL;
542 }
543
Ken Liu505b1702020-05-29 13:19:58 +0800544 return p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800545}
546
Edison Ai97115822019-08-01 14:22:19 +0800547struct tfm_msg_body_t *
Summer Qin630c76b2020-05-20 10:32:58 +0800548 tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800549{
Summer Qin630c76b2020-05-20 10:32:58 +0800550 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai97115822019-08-01 14:22:19 +0800551
Summer Qin630c76b2020-05-20 10:32:58 +0800552 return &(conn_handle->internal_msg);
Edison Ai97115822019-08-01 14:22:19 +0800553}
554
555void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
556 struct tfm_spm_service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800557 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800558 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800559 psa_invec *invec, size_t in_len,
560 psa_outvec *outvec, size_t out_len,
561 psa_outvec *caller_outvec)
562{
Edison Ai764d41f2018-09-21 15:56:36 +0800563 uint32_t i;
Ken Liu505b1702020-05-29 13:19:58 +0800564 struct tfm_conn_handle_t *conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800565
Ken Liuf250b8b2019-12-27 16:31:24 +0800566 TFM_CORE_ASSERT(msg);
567 TFM_CORE_ASSERT(service);
568 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
569 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
570 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
571 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
572 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800573
Edison Ai764d41f2018-09-21 15:56:36 +0800574 /* Clear message buffer before using it */
Summer Qinf24dbb52020-07-23 14:53:54 +0800575 spm_memset(msg, 0, sizeof(struct tfm_msg_body_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800576
Ken Liu35f89392019-03-14 14:51:05 +0800577 tfm_event_init(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800578 msg->magic = TFM_MSG_MAGIC;
579 msg->service = service;
Edison Ai764d41f2018-09-21 15:56:36 +0800580 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800581 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800582
583 /* Copy contents */
584 msg->msg.type = type;
585
586 for (i = 0; i < in_len; i++) {
587 msg->msg.in_size[i] = invec[i].len;
588 msg->invec[i].base = invec[i].base;
589 }
590
591 for (i = 0; i < out_len; i++) {
592 msg->msg.out_size[i] = outvec[i].len;
593 msg->outvec[i].base = outvec[i].base;
594 /* Out len is used to record the writed number, set 0 here again */
595 msg->outvec[i].len = 0;
596 }
597
Ken Liu505b1702020-05-29 13:19:58 +0800598 /* Use the user connect handle as the message handle */
599 msg->msg.handle = handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800600
Ken Liu505b1702020-05-29 13:19:58 +0800601 conn_handle = tfm_spm_to_handle_instance(handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800602 /* For connected handle, set rhandle to every message */
Ken Liu505b1702020-05-29 13:19:58 +0800603 if (conn_handle) {
604 msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800605 }
David Hu46603dd2019-12-11 18:05:16 +0800606
607 /* Set the private data of NSPE client caller in multi-core topology */
608 if (TFM_CLIENT_ID_IS_NS(client_id)) {
609 tfm_rpc_set_caller_data(msg, client_id);
610 }
Edison Ai764d41f2018-09-21 15:56:36 +0800611}
612
613int32_t tfm_spm_send_event(struct tfm_spm_service_t *service,
614 struct tfm_msg_body_t *msg)
615{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800616 struct spm_partition_runtime_data_t *p_runtime_data =
617 &service->partition->runtime_data;
618
Ken Liuf250b8b2019-12-27 16:31:24 +0800619 TFM_CORE_ASSERT(service);
620 TFM_CORE_ASSERT(msg);
Edison Ai764d41f2018-09-21 15:56:36 +0800621
Mingyang Sun73056b62020-07-03 15:18:46 +0800622 /* Add message to partition message list tail */
623 tfm_list_add_tail(&p_runtime_data->msg_list, &msg->msg_node);
Edison Ai764d41f2018-09-21 15:56:36 +0800624
625 /* Messages put. Update signals */
Summer Qine578c5b2019-08-16 16:42:16 +0800626 p_runtime_data->signals |= service->service_db->signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800627
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800628 tfm_event_wake(&p_runtime_data->signal_evnt, (p_runtime_data->signals &
629 p_runtime_data->signal_mask));
Edison Ai764d41f2018-09-21 15:56:36 +0800630
David Hufb38d562019-09-23 15:58:34 +0800631 /*
632 * If it is a NS request via RPC, it is unnecessary to block current
633 * thread.
634 */
635 if (!is_tfm_rpc_msg(msg)) {
636 tfm_event_wait(&msg->ack_evnt);
637 }
Edison Ai764d41f2018-09-21 15:56:36 +0800638
639 return IPC_SUCCESS;
640}
641
Mingyang Sunf3d29892019-07-10 17:50:23 +0800642uint32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800643{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800644 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800645
Kevin Peng79c2bda2020-07-24 16:31:12 +0800646 partition = tfm_spm_get_running_partition();
647 if (partition && partition->static_data) {
648 return partition->static_data->partition_id;
649 } else {
650 return INVALID_PARTITION_ID;
651 }
Edison Ai764d41f2018-09-21 15:56:36 +0800652}
653
Summer Qin43c185d2019-10-10 15:44:42 +0800654int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800655 enum tfm_memory_access_e access,
656 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800657{
Hugues de Valon99578562019-06-18 16:08:51 +0100658 enum tfm_status_e err;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800659
660 /* If len is zero, this indicates an empty buffer and base is ignored */
661 if (len == 0) {
662 return IPC_SUCCESS;
663 }
664
665 if (!buffer) {
666 return IPC_ERROR_BAD_PARAMETERS;
667 }
668
669 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
670 return IPC_ERROR_MEMORY_CHECK;
671 }
672
Summer Qin424d4db2019-03-25 14:09:51 +0800673 if (access == TFM_MEMORY_ACCESS_RW) {
Summer Qineb537e52019-03-29 09:57:10 +0800674 err = tfm_core_has_write_access_to_region(buffer, len, ns_caller,
675 privileged);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800676 } else {
Summer Qineb537e52019-03-29 09:57:10 +0800677 err = tfm_core_has_read_access_to_region(buffer, len, ns_caller,
678 privileged);
Summer Qin424d4db2019-03-25 14:09:51 +0800679 }
Summer Qin0fc3f592019-04-11 16:00:10 +0800680 if (err == TFM_SUCCESS) {
Summer Qin424d4db2019-03-25 14:09:51 +0800681 return IPC_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800682 }
683
684 return IPC_ERROR_MEMORY_CHECK;
685}
686
Ken Liuce2692d2020-02-11 12:39:36 +0800687uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800688{
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800689 uint32_t i, j, num;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800690 struct partition_t *partition;
Summer Qin66f1e032020-01-06 15:40:03 +0800691 struct tfm_core_thread_t *pth, *p_ns_entry_thread = NULL;
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100692 const struct tfm_spm_partition_platform_data_t **platform_data_p;
Edison Ai764d41f2018-09-21 15:56:36 +0800693
694 tfm_pool_init(conn_handle_pool,
695 POOL_BUFFER_SIZE(conn_handle_pool),
696 sizeof(struct tfm_conn_handle_t),
697 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800698
699 /* Init partition first for it will be used when init service */
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200700 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800701 partition = &g_spm_partition_db.partitions[i];
Edison Aif0501702019-10-11 14:36:42 +0800702
Kevin Peng79c2bda2020-07-24 16:31:12 +0800703 if (!partition || !partition->memory_data || !partition->static_data) {
704 tfm_core_panic();
705 }
706
707 if (!(partition->static_data->partition_flags & SPM_PART_FLAG_IPC)) {
708 tfm_core_panic();
709 }
710
Edison Aif0501702019-10-11 14:36:42 +0800711 /* Check if the PSA framework version matches. */
712 if (partition->static_data->psa_framework_version !=
713 PSA_FRAMEWORK_VERSION) {
Kevin Peng79c2bda2020-07-24 16:31:12 +0800714 ERROR_MSG("Warning: PSA Framework Verison does not match!");
Edison Aif0501702019-10-11 14:36:42 +0800715 continue;
716 }
717
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100718 platform_data_p = partition->platform_data_list;
719 if (platform_data_p != NULL) {
720 while ((*platform_data_p) != NULL) {
Edison Ai6be3df12020-02-14 22:14:33 +0800721 if (tfm_spm_hal_configure_default_isolation(i,
722 *platform_data_p) != TFM_PLAT_ERR_SUCCESS) {
723 tfm_core_panic();
724 }
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100725 ++platform_data_p;
726 }
727 }
728
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800729 /* Add PSA_DOORBELL signal to assigned_signals */
730 partition->runtime_data.assigned_signals |= PSA_DOORBELL;
731
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800732 /* TODO: This can be optimized by generating the assigned signal
733 * in code generation time.
734 */
735 for (j = 0; j < tfm_core_irq_signals_count; ++j) {
736 if (tfm_core_irq_signals[j].partition_id ==
737 partition->static_data->partition_id) {
738 partition->runtime_data.assigned_signals |=
739 tfm_core_irq_signals[j].signal_value;
740 }
741 }
742
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800743 tfm_event_init(&partition->runtime_data.signal_evnt);
Mingyang Sun73056b62020-07-03 15:18:46 +0800744 tfm_list_init(&partition->runtime_data.msg_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800745
Kevin Peng79c2bda2020-07-24 16:31:12 +0800746 pth = &partition->runtime_data.sp_thrd;
Edison Ai764d41f2018-09-21 15:56:36 +0800747 if (!pth) {
Edison Ai9059ea02019-11-28 13:46:14 +0800748 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800749 }
750
Summer Qin66f1e032020-01-06 15:40:03 +0800751 tfm_core_thrd_init(pth,
Kevin Peng79c2bda2020-07-24 16:31:12 +0800752 (tfm_core_thrd_entry_t)
753 partition->static_data->partition_init,
Summer Qin66f1e032020-01-06 15:40:03 +0800754 NULL,
Kevin Peng79c2bda2020-07-24 16:31:12 +0800755 (uintptr_t)partition->memory_data->stack_top,
756 (uintptr_t)partition->memory_data->stack_bottom);
Edison Ai788bae22019-02-18 17:38:59 +0800757
Kevin Peng79c2bda2020-07-24 16:31:12 +0800758 pth->prior = partition->static_data->partition_priority;
Edison Ai764d41f2018-09-21 15:56:36 +0800759
Ken Liu490281d2019-12-30 15:55:26 +0800760 if (partition->static_data->partition_id == TFM_SP_NON_SECURE_ID) {
761 p_ns_entry_thread = pth;
Ken Liu5248af22019-12-29 12:47:13 +0800762 pth->param = (void *)tfm_spm_hal_get_ns_entry_point();
Ken Liu490281d2019-12-30 15:55:26 +0800763 }
764
Edison Ai764d41f2018-09-21 15:56:36 +0800765 /* Kick off */
Summer Qin66f1e032020-01-06 15:40:03 +0800766 if (tfm_core_thrd_start(pth) != THRD_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800767 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800768 }
769 }
770
771 /* Init Service */
Summer Qind99509f2019-08-02 17:36:58 +0800772 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
Edison Ai764d41f2018-09-21 15:56:36 +0800773 for (i = 0; i < num; i++) {
Summer Qine578c5b2019-08-16 16:42:16 +0800774 service[i].service_db = &service_db[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800775 partition =
Summer Qine578c5b2019-08-16 16:42:16 +0800776 tfm_spm_get_partition_by_id(service[i].service_db->partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800777 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800778 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800779 }
Summer Qind99509f2019-08-02 17:36:58 +0800780 service[i].partition = partition;
Jaykumar Pitambarbhai Patel0c7a0382020-01-09 15:25:58 +0530781 partition->runtime_data.assigned_signals |= service[i].service_db->signal;
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800782
Summer Qind99509f2019-08-02 17:36:58 +0800783 tfm_list_init(&service[i].handle_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800784 }
785
Ken Liu483f5da2019-04-24 10:45:21 +0800786 /*
787 * All threads initialized, start the scheduler.
788 *
789 * NOTE:
Ken Liu490281d2019-12-30 15:55:26 +0800790 * It is worthy to give the thread object to scheduler if the background
791 * context belongs to one of the threads. Here the background thread is the
792 * initialization thread who calls SPM SVC, which re-uses the non-secure
793 * entry thread's stack. After SPM initialization is done, this stack is
794 * cleaned up and the background context is never going to return. Tell
795 * the scheduler that the current thread is non-secure entry thread.
Ken Liu483f5da2019-04-24 10:45:21 +0800796 */
Summer Qin66f1e032020-01-06 15:40:03 +0800797 tfm_core_thrd_start_scheduler(p_ns_entry_thread);
Ken Liuce2692d2020-02-11 12:39:36 +0800798
Summer Qind2ad7e72020-01-06 18:16:35 +0800799 return p_ns_entry_thread->arch_ctx.lr;
Edison Ai764d41f2018-09-21 15:56:36 +0800800}
Ken Liu2d175172019-03-21 17:08:41 +0800801
Summer Qind2ad7e72020-01-06 18:16:35 +0800802void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx)
Ken Liu2d175172019-03-21 17:08:41 +0800803{
804#if TFM_LVL == 2
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800805 struct partition_t *p_next_partition;
Summer Qinb5da9cc2019-08-26 15:19:45 +0800806 struct spm_partition_runtime_data_t *r_data;
Ken Liu2d175172019-03-21 17:08:41 +0800807 uint32_t is_privileged;
808#endif
Summer Qin66f1e032020-01-06 15:40:03 +0800809 struct tfm_core_thread_t *pth_next = tfm_core_thrd_get_next_thread();
810 struct tfm_core_thread_t *pth_curr = tfm_core_thrd_get_curr_thread();
Ken Liu2d175172019-03-21 17:08:41 +0800811
Mate Toth-Pal32b2ccd2019-04-26 10:00:16 +0200812 if (pth_next != NULL && pth_curr != pth_next) {
Ken Liu2d175172019-03-21 17:08:41 +0800813#if TFM_LVL == 2
Summer Qinb5da9cc2019-08-26 15:19:45 +0800814 r_data = TFM_GET_CONTAINER_PTR(pth_next,
815 struct spm_partition_runtime_data_t,
816 sp_thrd);
817 p_next_partition = TFM_GET_CONTAINER_PTR(r_data,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800818 struct partition_t,
Summer Qinb5da9cc2019-08-26 15:19:45 +0800819 runtime_data);
Ken Liu2d175172019-03-21 17:08:41 +0800820
Summer Qin423dbef2019-08-22 15:59:35 +0800821 if (p_next_partition->static_data->partition_flags &
Ken Liu2d175172019-03-21 17:08:41 +0800822 SPM_PART_FLAG_PSA_ROT) {
823 is_privileged = TFM_PARTITION_PRIVILEGED_MODE;
824 } else {
825 is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE;
826 }
827
828 tfm_spm_partition_change_privilege(is_privileged);
829#endif
Mate Toth-Palc430b992019-05-09 21:01:14 +0200830
Summer Qind2ad7e72020-01-06 18:16:35 +0800831 tfm_core_thrd_switch_context(p_actx, pth_curr, pth_next);
Ken Liu2d175172019-03-21 17:08:41 +0800832 }
David Hufb38d562019-09-23 15:58:34 +0800833
834 /*
835 * Handle pending mailbox message from NS in multi-core topology.
836 * Empty operation on single Armv8-M platform.
837 */
838 tfm_rpc_client_call_handler();
Ken Liu2d175172019-03-21 17:08:41 +0800839}
Mingyang Sund44522a2020-01-16 16:48:37 +0800840
841/*********************** SPM functions for PSA Client APIs *******************/
842
843uint32_t tfm_spm_psa_framework_version(void)
844{
845 return tfm_spm_client_psa_framework_version();
846}
847
848uint32_t tfm_spm_psa_version(uint32_t *args, bool ns_caller)
849{
850 uint32_t sid;
851
852 TFM_CORE_ASSERT(args != NULL);
853 sid = (uint32_t)args[0];
854
855 return tfm_spm_client_psa_version(sid, ns_caller);
856}
857
858psa_status_t tfm_spm_psa_connect(uint32_t *args, bool ns_caller)
859{
860 uint32_t sid;
861 uint32_t version;
862
863 TFM_CORE_ASSERT(args != NULL);
864 sid = (uint32_t)args[0];
865 version = (uint32_t)args[1];
866
867 return tfm_spm_client_psa_connect(sid, version, ns_caller);
868}
869
870psa_status_t tfm_spm_psa_call(uint32_t *args, bool ns_caller, uint32_t lr)
871{
872 psa_handle_t handle;
873 psa_invec *inptr;
874 psa_outvec *outptr;
875 size_t in_num, out_num;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800876 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800877 uint32_t privileged;
878 int32_t type;
879 struct tfm_control_parameter_t ctrl_param;
880
881 TFM_CORE_ASSERT(args != NULL);
882 handle = (psa_handle_t)args[0];
883
884 partition = tfm_spm_get_running_partition();
885 if (!partition) {
886 tfm_core_panic();
887 }
888 privileged = tfm_spm_partition_get_privileged_mode(
889 partition->static_data->partition_flags);
890
891 /*
892 * Read parameters from the arguments. It is a fatal error if the
893 * memory reference for buffer is invalid or not readable.
894 */
895 if (tfm_memory_check((const void *)args[1],
896 sizeof(struct tfm_control_parameter_t), ns_caller,
897 TFM_MEMORY_ACCESS_RW, privileged) != IPC_SUCCESS) {
898 tfm_core_panic();
899 }
900
Summer Qinf24dbb52020-07-23 14:53:54 +0800901 spm_memcpy(&ctrl_param, (const void *)args[1], sizeof(ctrl_param));
Mingyang Sund44522a2020-01-16 16:48:37 +0800902
903 type = ctrl_param.type;
904 in_num = ctrl_param.in_len;
905 out_num = ctrl_param.out_len;
906 inptr = (psa_invec *)args[2];
907 outptr = (psa_outvec *)args[3];
908
909 /* The request type must be zero or positive. */
910 if (type < 0) {
911 tfm_core_panic();
912 }
913
914 return tfm_spm_client_psa_call(handle, type, inptr, in_num, outptr, out_num,
915 ns_caller, privileged);
916}
917
918void tfm_spm_psa_close(uint32_t *args, bool ns_caller)
919{
920 psa_handle_t handle;
921
922 TFM_CORE_ASSERT(args != NULL);
923 handle = args[0];
924
925 tfm_spm_client_psa_close(handle, ns_caller);
926}
927
928uint32_t tfm_spm_get_lifecycle_state(void)
929{
930 /*
931 * FixMe: return PSA_LIFECYCLE_UNKNOWN to the caller directly. It will be
932 * implemented in the future.
933 */
934 return PSA_LIFECYCLE_UNKNOWN;
935}
936
937/********************* SPM functions for PSA Service APIs ********************/
938
939psa_signal_t tfm_spm_psa_wait(uint32_t *args)
940{
941 psa_signal_t signal_mask;
942 uint32_t timeout;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800943 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800944
945 TFM_CORE_ASSERT(args != NULL);
946 signal_mask = (psa_signal_t)args[0];
947 timeout = args[1];
948
949 /*
950 * Timeout[30:0] are reserved for future use.
951 * SPM must ignore the value of RES.
952 */
953 timeout &= PSA_TIMEOUT_MASK;
954
955 partition = tfm_spm_get_running_partition();
956 if (!partition) {
957 tfm_core_panic();
958 }
959
960 /*
961 * It is a PROGRAMMER ERROR if the signal_mask does not include any assigned
962 * signals.
963 */
964 if ((partition->runtime_data.assigned_signals & signal_mask) == 0) {
965 tfm_core_panic();
966 }
967
968 /*
969 * Expected signals are included in signal wait mask, ignored signals
970 * should not be set and affect caller thread state. Save this mask for
971 * further checking while signals are ready to be set.
972 */
973 partition->runtime_data.signal_mask = signal_mask;
974
975 /*
976 * tfm_event_wait() blocks the caller thread if no signals are available.
977 * In this case, the return value of this function is temporary set into
978 * runtime context. After new signal(s) are available, the return value
979 * is updated with the available signal(s) and blocked thread gets to run.
980 */
981 if (timeout == PSA_BLOCK &&
982 (partition->runtime_data.signals & signal_mask) == 0) {
983 tfm_event_wait(&partition->runtime_data.signal_evnt);
984 }
985
986 return partition->runtime_data.signals & signal_mask;
987}
988
989psa_status_t tfm_spm_psa_get(uint32_t *args)
990{
991 psa_signal_t signal;
992 psa_msg_t *msg = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800993 struct tfm_msg_body_t *tmp_msg = NULL;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800994 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800995 uint32_t privileged;
996
997 TFM_CORE_ASSERT(args != NULL);
998 signal = (psa_signal_t)args[0];
999 msg = (psa_msg_t *)args[1];
1000
1001 /*
1002 * Only one message could be retrieved every time for psa_get(). It is a
1003 * fatal error if the input signal has more than a signal bit set.
1004 */
Ken Liu410ada52020-01-08 11:37:27 +08001005 if (!tfm_is_one_bit_set(signal)) {
Mingyang Sund44522a2020-01-16 16:48:37 +08001006 tfm_core_panic();
1007 }
1008
1009 partition = tfm_spm_get_running_partition();
1010 if (!partition) {
1011 tfm_core_panic();
1012 }
1013 privileged = tfm_spm_partition_get_privileged_mode(
1014 partition->static_data->partition_flags);
1015
1016 /*
1017 * Write the message to the service buffer. It is a fatal error if the
1018 * input msg pointer is not a valid memory reference or not read-write.
1019 */
1020 if (tfm_memory_check(msg, sizeof(psa_msg_t), false, TFM_MEMORY_ACCESS_RW,
1021 privileged) != IPC_SUCCESS) {
1022 tfm_core_panic();
1023 }
1024
1025 /*
1026 * It is a fatal error if the caller call psa_get() when no message has
1027 * been set. The caller must call this function after an RoT Service signal
1028 * is returned by psa_wait().
1029 */
1030 if (partition->runtime_data.signals == 0) {
1031 tfm_core_panic();
1032 }
1033
1034 /*
1035 * It is a fatal error if the RoT Service signal is not currently asserted.
1036 */
1037 if ((partition->runtime_data.signals & signal) == 0) {
1038 tfm_core_panic();
1039 }
1040
1041 /*
Mingyang Sun73056b62020-07-03 15:18:46 +08001042 * Get message by signal from partition. It is a fatal error if getting
Mingyang Sund44522a2020-01-16 16:48:37 +08001043 * failed, which means the input signal is not correspond to an RoT service.
1044 */
Mingyang Sun73056b62020-07-03 15:18:46 +08001045 tmp_msg = tfm_spm_get_msg_by_signal(partition, signal);
Mingyang Sund44522a2020-01-16 16:48:37 +08001046 if (!tmp_msg) {
1047 return PSA_ERROR_DOES_NOT_EXIST;
1048 }
1049
Ken Liu505b1702020-05-29 13:19:58 +08001050 (TFM_GET_CONTAINER_PTR(tmp_msg,
1051 struct tfm_conn_handle_t,
1052 internal_msg))->status = TFM_HANDLE_STATUS_ACTIVE;
Mingyang Sund44522a2020-01-16 16:48:37 +08001053
Summer Qinf24dbb52020-07-23 14:53:54 +08001054 spm_memcpy(msg, &tmp_msg->msg, sizeof(psa_msg_t));
Mingyang Sund44522a2020-01-16 16:48:37 +08001055
Mingyang Sund44522a2020-01-16 16:48:37 +08001056 return PSA_SUCCESS;
1057}
1058
1059void tfm_spm_psa_set_rhandle(uint32_t *args)
1060{
1061 psa_handle_t msg_handle;
1062 void *rhandle = NULL;
1063 struct tfm_msg_body_t *msg = NULL;
Ken Liu505b1702020-05-29 13:19:58 +08001064 struct tfm_conn_handle_t *conn_handle;
Mingyang Sund44522a2020-01-16 16:48:37 +08001065
1066 TFM_CORE_ASSERT(args != NULL);
1067 msg_handle = (psa_handle_t)args[0];
1068 rhandle = (void *)args[1];
1069
1070 /* It is a fatal error if message handle is invalid */
1071 msg = tfm_spm_get_msg_from_handle(msg_handle);
1072 if (!msg) {
1073 tfm_core_panic();
1074 }
1075
1076 msg->msg.rhandle = rhandle;
Ken Liu505b1702020-05-29 13:19:58 +08001077 conn_handle = tfm_spm_to_handle_instance(msg_handle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001078
1079 /* Store reverse handle for following client calls. */
Ken Liu505b1702020-05-29 13:19:58 +08001080 tfm_spm_set_rhandle(msg->service, conn_handle, rhandle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001081}
1082
1083size_t tfm_spm_psa_read(uint32_t *args)
1084{
1085 psa_handle_t msg_handle;
1086 uint32_t invec_idx;
1087 void *buffer = NULL;
1088 size_t num_bytes;
1089 size_t bytes;
1090 struct tfm_msg_body_t *msg = NULL;
1091 uint32_t privileged;
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001092 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001093
1094 TFM_CORE_ASSERT(args != NULL);
1095 msg_handle = (psa_handle_t)args[0];
1096 invec_idx = args[1];
1097 buffer = (void *)args[2];
1098 num_bytes = (size_t)args[3];
1099
1100 /* It is a fatal error if message handle is invalid */
1101 msg = tfm_spm_get_msg_from_handle(msg_handle);
1102 if (!msg) {
1103 tfm_core_panic();
1104 }
1105
1106 partition = msg->service->partition;
1107 privileged = tfm_spm_partition_get_privileged_mode(
1108 partition->static_data->partition_flags);
1109
1110 /*
1111 * It is a fatal error if message handle does not refer to a request
1112 * message
1113 */
1114 if (msg->msg.type < PSA_IPC_CALL) {
1115 tfm_core_panic();
1116 }
1117
1118 /*
1119 * It is a fatal error if invec_idx is equal to or greater than
1120 * PSA_MAX_IOVEC
1121 */
1122 if (invec_idx >= PSA_MAX_IOVEC) {
1123 tfm_core_panic();
1124 }
1125
1126 /* There was no remaining data in this input vector */
1127 if (msg->msg.in_size[invec_idx] == 0) {
1128 return 0;
1129 }
1130
1131 /*
1132 * Copy the client data to the service buffer. It is a fatal error
1133 * if the memory reference for buffer is invalid or not read-write.
1134 */
1135 if (tfm_memory_check(buffer, num_bytes, false,
1136 TFM_MEMORY_ACCESS_RW, privileged) != IPC_SUCCESS) {
1137 tfm_core_panic();
1138 }
1139
1140 bytes = num_bytes > msg->msg.in_size[invec_idx] ?
1141 msg->msg.in_size[invec_idx] : num_bytes;
1142
Summer Qinf24dbb52020-07-23 14:53:54 +08001143 spm_memcpy(buffer, msg->invec[invec_idx].base, bytes);
Mingyang Sund44522a2020-01-16 16:48:37 +08001144
1145 /* There maybe some remaining data */
1146 msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base + bytes;
1147 msg->msg.in_size[invec_idx] -= bytes;
1148
1149 return bytes;
1150}
1151
1152size_t tfm_spm_psa_skip(uint32_t *args)
1153{
1154 psa_handle_t msg_handle;
1155 uint32_t invec_idx;
1156 size_t num_bytes;
1157 struct tfm_msg_body_t *msg = NULL;
1158
1159 TFM_CORE_ASSERT(args != NULL);
1160 msg_handle = (psa_handle_t)args[0];
1161 invec_idx = args[1];
1162 num_bytes = (size_t)args[2];
1163
1164 /* It is a fatal error if message handle is invalid */
1165 msg = tfm_spm_get_msg_from_handle(msg_handle);
1166 if (!msg) {
1167 tfm_core_panic();
1168 }
1169
1170 /*
1171 * It is a fatal error if message handle does not refer to a request
1172 * message
1173 */
1174 if (msg->msg.type < PSA_IPC_CALL) {
1175 tfm_core_panic();
1176 }
1177
1178 /*
1179 * It is a fatal error if invec_idx is equal to or greater than
1180 * PSA_MAX_IOVEC
1181 */
1182 if (invec_idx >= PSA_MAX_IOVEC) {
1183 tfm_core_panic();
1184 }
1185
1186 /* There was no remaining data in this input vector */
1187 if (msg->msg.in_size[invec_idx] == 0) {
1188 return 0;
1189 }
1190
1191 /*
1192 * If num_bytes is greater than the remaining size of the input vector then
1193 * the remaining size of the input vector is used.
1194 */
1195 if (num_bytes > msg->msg.in_size[invec_idx]) {
1196 num_bytes = msg->msg.in_size[invec_idx];
1197 }
1198
1199 /* There maybe some remaining data */
1200 msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base +
1201 num_bytes;
1202 msg->msg.in_size[invec_idx] -= num_bytes;
1203
1204 return num_bytes;
1205}
1206
1207void tfm_spm_psa_write(uint32_t *args)
1208{
1209 psa_handle_t msg_handle;
1210 uint32_t outvec_idx;
1211 void *buffer = NULL;
1212 size_t num_bytes;
1213 struct tfm_msg_body_t *msg = NULL;
1214 uint32_t privileged;
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001215 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001216
1217 TFM_CORE_ASSERT(args != NULL);
1218 msg_handle = (psa_handle_t)args[0];
1219 outvec_idx = args[1];
1220 buffer = (void *)args[2];
1221 num_bytes = (size_t)args[3];
1222
1223 /* It is a fatal error if message handle is invalid */
1224 msg = tfm_spm_get_msg_from_handle(msg_handle);
1225 if (!msg) {
1226 tfm_core_panic();
1227 }
1228
1229 partition = msg->service->partition;
1230 privileged = tfm_spm_partition_get_privileged_mode(
1231 partition->static_data->partition_flags);
1232
1233 /*
1234 * It is a fatal error if message handle does not refer to a request
1235 * message
1236 */
1237 if (msg->msg.type < PSA_IPC_CALL) {
1238 tfm_core_panic();
1239 }
1240
1241 /*
1242 * It is a fatal error if outvec_idx is equal to or greater than
1243 * PSA_MAX_IOVEC
1244 */
1245 if (outvec_idx >= PSA_MAX_IOVEC) {
1246 tfm_core_panic();
1247 }
1248
1249 /*
1250 * It is a fatal error if the call attempts to write data past the end of
1251 * the client output vector
1252 */
1253 if (num_bytes > msg->msg.out_size[outvec_idx] -
1254 msg->outvec[outvec_idx].len) {
1255 tfm_core_panic();
1256 }
1257
1258 /*
1259 * Copy the service buffer to client outvecs. It is a fatal error
1260 * if the memory reference for buffer is invalid or not readable.
1261 */
1262 if (tfm_memory_check(buffer, num_bytes, false,
1263 TFM_MEMORY_ACCESS_RO, privileged) != IPC_SUCCESS) {
1264 tfm_core_panic();
1265 }
1266
Summer Qinf24dbb52020-07-23 14:53:54 +08001267 spm_memcpy((char *)msg->outvec[outvec_idx].base +
1268 msg->outvec[outvec_idx].len, buffer, num_bytes);
Mingyang Sund44522a2020-01-16 16:48:37 +08001269
1270 /* Update the write number */
1271 msg->outvec[outvec_idx].len += num_bytes;
1272}
1273
1274static void update_caller_outvec_len(struct tfm_msg_body_t *msg)
1275{
1276 uint32_t i;
1277
1278 /*
1279 * FixeMe: abstract these part into dedicated functions to avoid
1280 * accessing thread context in psa layer
1281 */
1282 /* If it is a NS request via RPC, the owner of this message is not set */
1283 if (!is_tfm_rpc_msg(msg)) {
1284 TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK);
1285 }
1286
1287 for (i = 0; i < PSA_MAX_IOVEC; i++) {
1288 if (msg->msg.out_size[i] == 0) {
1289 continue;
1290 }
1291
1292 TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
1293
1294 msg->caller_outvec[i].len = msg->outvec[i].len;
1295 }
1296}
1297
1298void tfm_spm_psa_reply(uint32_t *args)
1299{
1300 psa_handle_t msg_handle;
1301 psa_status_t status;
1302 struct tfm_spm_service_t *service = NULL;
1303 struct tfm_msg_body_t *msg = NULL;
1304 int32_t ret = PSA_SUCCESS;
Ken Liu505b1702020-05-29 13:19:58 +08001305 struct tfm_conn_handle_t *conn_handle;
Mingyang Sund44522a2020-01-16 16:48:37 +08001306
1307 TFM_CORE_ASSERT(args != NULL);
1308 msg_handle = (psa_handle_t)args[0];
1309 status = (psa_status_t)args[1];
1310
1311 /* It is a fatal error if message handle is invalid */
1312 msg = tfm_spm_get_msg_from_handle(msg_handle);
1313 if (!msg) {
1314 tfm_core_panic();
1315 }
1316
1317 /*
1318 * RoT Service information is needed in this function, stored it in message
1319 * body structure. Only two parameters are passed in this function: handle
1320 * and status, so it is useful and simply to do like this.
1321 */
1322 service = msg->service;
1323 if (!service) {
1324 tfm_core_panic();
1325 }
1326
1327 /*
1328 * Three type of message are passed in this function: CONNECTION, REQUEST,
1329 * DISCONNECTION. It needs to process differently for each type.
1330 */
Ken Liu505b1702020-05-29 13:19:58 +08001331 conn_handle = tfm_spm_to_handle_instance(msg_handle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001332 switch (msg->msg.type) {
1333 case PSA_IPC_CONNECT:
1334 /*
1335 * Reply to PSA_IPC_CONNECT message. Connect handle is returned if the
1336 * input status is PSA_SUCCESS. Others return values are based on the
1337 * input status.
1338 */
1339 if (status == PSA_SUCCESS) {
Ken Liu505b1702020-05-29 13:19:58 +08001340 ret = msg_handle;
Mingyang Sund44522a2020-01-16 16:48:37 +08001341 } else if (status == PSA_ERROR_CONNECTION_REFUSED) {
1342 /* Refuse the client connection, indicating a permanent error. */
Ken Liu505b1702020-05-29 13:19:58 +08001343 tfm_spm_free_conn_handle(service, conn_handle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001344 ret = PSA_ERROR_CONNECTION_REFUSED;
1345 } else if (status == PSA_ERROR_CONNECTION_BUSY) {
1346 /* Fail the client connection, indicating a transient error. */
1347 ret = PSA_ERROR_CONNECTION_BUSY;
1348 } else {
1349 tfm_core_panic();
1350 }
1351 break;
1352 case PSA_IPC_DISCONNECT:
1353 /* Service handle is not used anymore */
Ken Liu505b1702020-05-29 13:19:58 +08001354 tfm_spm_free_conn_handle(service, conn_handle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001355
1356 /*
1357 * If the message type is PSA_IPC_DISCONNECT, then the status code is
1358 * ignored
1359 */
1360 break;
1361 default:
1362 if (msg->msg.type >= PSA_IPC_CALL) {
1363 /* Reply to a request message. Return values are based on status */
1364 ret = status;
1365 /*
1366 * The total number of bytes written to a single parameter must be
1367 * reported to the client by updating the len member of the
1368 * psa_outvec structure for the parameter before returning from
1369 * psa_call().
1370 */
1371 update_caller_outvec_len(msg);
1372 } else {
1373 tfm_core_panic();
1374 }
1375 }
1376
1377 if (ret == PSA_ERROR_PROGRAMMER_ERROR) {
1378 /*
1379 * If the source of the programmer error is a Secure Partition, the SPM
1380 * must panic the Secure Partition in response to a PROGRAMMER ERROR.
1381 */
1382 if (TFM_CLIENT_ID_IS_NS(msg->msg.client_id)) {
Ken Liu505b1702020-05-29 13:19:58 +08001383 conn_handle->status = TFM_HANDLE_STATUS_CONNECT_ERROR;
Mingyang Sund44522a2020-01-16 16:48:37 +08001384 } else {
1385 tfm_core_panic();
1386 }
1387 } else {
Ken Liu505b1702020-05-29 13:19:58 +08001388 conn_handle->status = TFM_HANDLE_STATUS_IDLE;
Mingyang Sund44522a2020-01-16 16:48:37 +08001389 }
1390
1391 if (is_tfm_rpc_msg(msg)) {
1392 tfm_rpc_client_call_reply(msg, ret);
1393 } else {
1394 tfm_event_wake(&msg->ack_evnt, ret);
1395 }
1396}
1397
1398/**
1399 * \brief notify the partition with the signal.
1400 *
1401 * \param[in] partition_id The ID of the partition to be notified.
1402 * \param[in] signal The signal that the partition is to be notified
1403 * with.
1404 *
1405 * \retval void Success.
1406 * \retval "Does not return" If partition_id is invalid.
1407 */
1408static void notify_with_signal(int32_t partition_id, psa_signal_t signal)
1409{
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001410 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001411
1412 /*
1413 * The value of partition_id must be greater than zero as the target of
1414 * notification must be a Secure Partition, providing a Non-secure
1415 * Partition ID is a fatal error.
1416 */
1417 if (!TFM_CLIENT_ID_IS_S(partition_id)) {
1418 tfm_core_panic();
1419 }
1420
1421 /*
1422 * It is a fatal error if partition_id does not correspond to a Secure
1423 * Partition.
1424 */
1425 partition = tfm_spm_get_partition_by_id(partition_id);
1426 if (!partition) {
1427 tfm_core_panic();
1428 }
1429
1430 partition->runtime_data.signals |= signal;
1431
1432 /*
1433 * The target partition may be blocked with waiting for signals after
1434 * called psa_wait(). Set the return value with the available signals
1435 * before wake it up with tfm_event_signal().
1436 */
1437 tfm_event_wake(&partition->runtime_data.signal_evnt,
1438 partition->runtime_data.signals &
1439 partition->runtime_data.signal_mask);
1440}
1441
1442void tfm_spm_psa_notify(uint32_t *args)
1443{
1444 int32_t partition_id;
1445
1446 TFM_CORE_ASSERT(args != NULL);
1447 partition_id = (int32_t)args[0];
1448
1449 notify_with_signal(partition_id, PSA_DOORBELL);
1450}
1451
1452/**
1453 * \brief assert signal for a given IRQ line.
1454 *
1455 * \param[in] partition_id The ID of the partition which handles this IRQ
1456 * \param[in] signal The signal associated with this IRQ
1457 * \param[in] irq_line The number of the IRQ line
1458 *
1459 * \retval void Success.
1460 * \retval "Does not return" Partition ID is invalid
1461 */
1462void tfm_irq_handler(uint32_t partition_id, psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +01001463 IRQn_Type irq_line)
Mingyang Sund44522a2020-01-16 16:48:37 +08001464{
1465 tfm_spm_hal_disable_irq(irq_line);
1466 notify_with_signal(partition_id, signal);
1467}
1468
1469void tfm_spm_psa_clear(void)
1470{
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001471 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001472
1473 partition = tfm_spm_get_running_partition();
1474 if (!partition) {
1475 tfm_core_panic();
1476 }
1477
1478 /*
1479 * It is a fatal error if the Secure Partition's doorbell signal is not
1480 * currently asserted.
1481 */
1482 if ((partition->runtime_data.signals & PSA_DOORBELL) == 0) {
1483 tfm_core_panic();
1484 }
1485 partition->runtime_data.signals &= ~PSA_DOORBELL;
1486}
1487
1488void tfm_spm_psa_panic(void)
1489{
1490 /*
1491 * PSA FF recommends that the SPM causes the system to restart when a secure
1492 * partition panics.
1493 */
1494 tfm_spm_hal_system_reset();
1495}
1496
1497/**
1498 * \brief Return the IRQ line number associated with a signal
1499 *
1500 * \param[in] partition_id The ID of the partition in which we look for
1501 * the signal.
1502 * \param[in] signal The signal we do the query for.
1503 * \param[out] irq_line The irq line associated with signal
1504 *
1505 * \retval IPC_SUCCESS Execution successful, irq_line contains a valid
1506 * value.
1507 * \retval IPC_ERROR_GENERIC There was an error finding the IRQ line for the
1508 * signal. irq_line is unchanged.
1509 */
1510static int32_t get_irq_line_for_signal(int32_t partition_id,
1511 psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +01001512 IRQn_Type *irq_line)
Mingyang Sund44522a2020-01-16 16:48:37 +08001513{
1514 size_t i;
1515
1516 for (i = 0; i < tfm_core_irq_signals_count; ++i) {
1517 if (tfm_core_irq_signals[i].partition_id == partition_id &&
1518 tfm_core_irq_signals[i].signal_value == signal) {
1519 *irq_line = tfm_core_irq_signals[i].irq_line;
1520 return IPC_SUCCESS;
1521 }
1522 }
1523 return IPC_ERROR_GENERIC;
1524}
1525
1526void tfm_spm_psa_eoi(uint32_t *args)
1527{
1528 psa_signal_t irq_signal;
TTornblomfaf74f52020-03-04 17:56:27 +01001529 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001530 int32_t ret;
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001531 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001532
1533 TFM_CORE_ASSERT(args != NULL);
1534 irq_signal = (psa_signal_t)args[0];
1535
1536 /* It is a fatal error if passed signal indicates more than one signals. */
1537 if (!tfm_is_one_bit_set(irq_signal)) {
1538 tfm_core_panic();
1539 }
1540
1541 partition = tfm_spm_get_running_partition();
1542 if (!partition) {
1543 tfm_core_panic();
1544 }
1545
1546 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1547 irq_signal, &irq_line);
1548 /* It is a fatal error if passed signal is not an interrupt signal. */
1549 if (ret != IPC_SUCCESS) {
1550 tfm_core_panic();
1551 }
1552
1553 /* It is a fatal error if passed signal is not currently asserted */
1554 if ((partition->runtime_data.signals & irq_signal) == 0) {
1555 tfm_core_panic();
1556 }
1557
1558 partition->runtime_data.signals &= ~irq_signal;
1559
1560 tfm_spm_hal_clear_pending_irq(irq_line);
1561 tfm_spm_hal_enable_irq(irq_line);
1562}
1563
1564void tfm_spm_enable_irq(uint32_t *args)
1565{
1566 struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args;
1567 psa_signal_t irq_signal = svc_ctx->r0;
TTornblomfaf74f52020-03-04 17:56:27 +01001568 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001569 int32_t ret;
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001570 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001571
1572 /* It is a fatal error if passed signal indicates more than one signals. */
1573 if (!tfm_is_one_bit_set(irq_signal)) {
1574 tfm_core_panic();
1575 }
1576
1577 partition = tfm_spm_get_running_partition();
1578 if (!partition) {
1579 tfm_core_panic();
1580 }
1581
1582 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1583 irq_signal, &irq_line);
1584 /* It is a fatal error if passed signal is not an interrupt signal. */
1585 if (ret != IPC_SUCCESS) {
1586 tfm_core_panic();
1587 }
1588
1589 tfm_spm_hal_enable_irq(irq_line);
1590}
1591
1592void tfm_spm_disable_irq(uint32_t *args)
1593{
1594 struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args;
1595 psa_signal_t irq_signal = svc_ctx->r0;
TTornblomfaf74f52020-03-04 17:56:27 +01001596 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001597 int32_t ret;
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001598 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001599
1600 /* It is a fatal error if passed signal indicates more than one signals. */
1601 if (!tfm_is_one_bit_set(irq_signal)) {
1602 tfm_core_panic();
1603 }
1604
1605 partition = tfm_spm_get_running_partition();
1606 if (!partition) {
1607 tfm_core_panic();
1608 }
1609
1610 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1611 irq_signal, &irq_line);
1612 /* It is a fatal error if passed signal is not an interrupt signal. */
1613 if (ret != IPC_SUCCESS) {
1614 tfm_core_panic();
1615 }
1616
1617 tfm_spm_hal_disable_irq(irq_line);
1618}
1619
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001620void tfm_spm_validate_caller(struct partition_t *p_cur_sp, uint32_t *p_ctx,
1621 uint32_t exc_return, bool ns_caller)
Mingyang Sund44522a2020-01-16 16:48:37 +08001622{
1623 uintptr_t stacked_ctx_pos;
1624
1625 if (ns_caller) {
1626 /*
1627 * The background IRQ can't be supported, since if SP is executing,
1628 * the preempted context of SP can be different with the one who
1629 * preempts veneer.
1630 */
1631 if (p_cur_sp->static_data->partition_id != TFM_SP_NON_SECURE_ID) {
1632 tfm_core_panic();
1633 }
1634
1635 /*
1636 * It is non-secure caller, check if veneer stack contains
1637 * multiple contexts.
1638 */
1639 stacked_ctx_pos = (uintptr_t)p_ctx +
1640 sizeof(struct tfm_state_context_t) +
1641 TFM_VENEER_STACK_GUARD_SIZE;
1642
1643 if (is_stack_alloc_fp_space(exc_return)) {
1644#if defined (__FPU_USED) && (__FPU_USED == 1U)
1645 if (FPU->FPCCR & FPU_FPCCR_TS_Msk) {
1646 stacked_ctx_pos += TFM_ADDTIONAL_FP_CONTEXT_WORDS *
1647 sizeof(uint32_t);
1648 }
1649#endif
1650 stacked_ctx_pos += TFM_BASIC_FP_CONTEXT_WORDS * sizeof(uint32_t);
1651 }
1652
1653 if (stacked_ctx_pos != p_cur_sp->runtime_data.sp_thrd.stk_top) {
1654 tfm_core_panic();
1655 }
1656 } else if (p_cur_sp->static_data->partition_id <= 0) {
1657 tfm_core_panic();
1658 }
1659}
Summer Qin830c5542020-02-14 13:44:20 +08001660
1661void tfm_spm_request_handler(const struct tfm_state_context_t *svc_ctx)
1662{
1663 uint32_t *res_ptr = (uint32_t *)&svc_ctx->r0;
1664 uint32_t running_partition_flags = 0;
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001665 const struct partition_t *partition = NULL;
Summer Qin830c5542020-02-14 13:44:20 +08001666
1667 /* Check permissions on request type basis */
1668
1669 switch (svc_ctx->r0) {
1670 case TFM_SPM_REQUEST_RESET_VOTE:
1671 partition = tfm_spm_get_running_partition();
1672 if (!partition) {
1673 tfm_core_panic();
1674 }
1675 running_partition_flags = partition->static_data->partition_flags;
1676
1677 /* Currently only PSA Root of Trust services are allowed to make Reset
1678 * vote request
1679 */
1680 if ((running_partition_flags & SPM_PART_FLAG_PSA_ROT) == 0) {
1681 *res_ptr = (uint32_t)TFM_ERROR_GENERIC;
1682 }
1683
1684 /* FixMe: this is a placeholder for checks to be performed before
1685 * allowing execution of reset
1686 */
1687 *res_ptr = (uint32_t)TFM_SUCCESS;
1688
1689 break;
1690 default:
1691 *res_ptr = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
1692 }
1693}
Mingyang Sunbd7ceb52020-06-11 16:53:03 +08001694
1695enum spm_err_t tfm_spm_db_init(void)
1696{
1697 uint32_t i;
1698
1699 /* This function initialises partition db */
1700
1701 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
1702 g_spm_partition_db.partitions[i].static_data = &static_data_list[i];
1703 g_spm_partition_db.partitions[i].platform_data_list =
1704 platform_data_list_list[i];
1705 g_spm_partition_db.partitions[i].memory_data = &memory_data_list[i];
1706 }
1707 g_spm_partition_db.is_init = 1;
1708
1709 return SPM_ERR_OK;
1710}