blob: 3188280e3b7bade170f881f59f781b8747770a0c [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"
Mingyang Sund44522a2020-01-16 16:48:37 +080015#include "tfm_utils.h"
16#include "tfm_internal_defines.h"
Edison Ai764d41f2018-09-21 15:56:36 +080017#include "tfm_message_queue.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080018#include "tfm_spm_hal.h"
19#include "tfm_irq_list.h"
20#include "tfm_api.h"
21#include "tfm_secure_api.h"
22#include "tfm_memory_utils.h"
23#include "spm_api.h"
24#include "tfm_peripherals_def.h"
25#include "spm_db.h"
26#include "tfm_core_utils.h"
27#include "spm_psa_client_call.h"
28#include "tfm_rpc.h"
29#include "tfm_internal.h"
30#include "tfm_core_trustzone.h"
31#include "tfm_core_mem_check.h"
Edison Ai764d41f2018-09-21 15:56:36 +080032#include "tfm_list.h"
33#include "tfm_pools.h"
Summer Qin2bfd2a02018-09-26 17:10:41 +080034#include "region_defs.h"
Summer Qin830c5542020-02-14 13:44:20 +080035#include "tfm_spm_services_api.h"
Edison Ai764d41f2018-09-21 15:56:36 +080036
Summer Qind99509f2019-08-02 17:36:58 +080037#include "secure_fw/services/tfm_service_list.inc"
38
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/* Extern SPM variable */
44extern struct spm_partition_db_t g_spm_partition_db;
45
46/* Pools */
47TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t),
48 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +080049
Mingyang Sund44522a2020-01-16 16:48:37 +080050void tfm_irq_handler(uint32_t partition_id, psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +010051 IRQn_Type irq_line);
Mingyang Sund44522a2020-01-16 16:48:37 +080052
53#include "tfm_secure_irq_handlers_ipc.inc"
Edison Ai764d41f2018-09-21 15:56:36 +080054
55/* Service handle management functions */
Summer Qin630c76b2020-05-20 10:32:58 +080056struct tfm_conn_handle_t *tfm_spm_create_conn_handle(
57 struct tfm_spm_service_t *service,
Summer Qin1ce712a2019-10-14 18:04:05 +080058 int32_t client_id)
Edison Ai764d41f2018-09-21 15:56:36 +080059{
Edison Ai9cc26242019-08-06 11:28:04 +080060 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +080061
Ken Liuf250b8b2019-12-27 16:31:24 +080062 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +080063
64 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +080065 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
66 if (!p_handle) {
Summer Qin630c76b2020-05-20 10:32:58 +080067 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +080068 }
69
Edison Ai9cc26242019-08-06 11:28:04 +080070 p_handle->service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +080071 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +080072 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +080073
74 /* Add handle node to list for next psa functions */
Edison Ai9cc26242019-08-06 11:28:04 +080075 tfm_list_add_tail(&service->handle_list, &p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +080076
Summer Qin630c76b2020-05-20 10:32:58 +080077 return p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +080078}
79
Summer Qin630c76b2020-05-20 10:32:58 +080080int32_t tfm_spm_validate_conn_handle(
81 const struct tfm_conn_handle_t *conn_handle,
82 int32_t client_id)
Summer Qin1ce712a2019-10-14 18:04:05 +080083{
84 /* Check the handle address is validated */
85 if (is_valid_chunk_data_in_pool(conn_handle_pool,
86 (uint8_t *)conn_handle) != true) {
87 return IPC_ERROR_GENERIC;
88 }
89
90 /* Check the handle caller is correct */
Summer Qin630c76b2020-05-20 10:32:58 +080091 if (conn_handle->client_id != client_id) {
Summer Qin1ce712a2019-10-14 18:04:05 +080092 return IPC_ERROR_GENERIC;
93 }
94
95 return IPC_SUCCESS;
96}
97
Mingyang Sund44522a2020-01-16 16:48:37 +080098/**
99 * \brief Free connection handle which not used anymore.
100 *
101 * \param[in] service Target service context pointer
102 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800103 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800104 *
105 * \retval IPC_SUCCESS Success
106 * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
107 * \retval "Does not return" Panic for not find service by handle
108 */
109static int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800110 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800111{
Ken Liuf250b8b2019-12-27 16:31:24 +0800112 TFM_CORE_ASSERT(service);
Summer Qin630c76b2020-05-20 10:32:58 +0800113 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800114
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200115 /* Clear magic as the handler is not used anymore */
Summer Qin630c76b2020-05-20 10:32:58 +0800116 conn_handle->internal_msg.magic = 0;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200117
Edison Ai764d41f2018-09-21 15:56:36 +0800118 /* Remove node from handle list */
Summer Qin630c76b2020-05-20 10:32:58 +0800119 tfm_list_del_node(&conn_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800120
121 /* Back handle buffer to pool */
Summer Qin630c76b2020-05-20 10:32:58 +0800122 tfm_pool_free(conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800123 return IPC_SUCCESS;
124}
125
Mingyang Sund44522a2020-01-16 16:48:37 +0800126/**
127 * \brief Set reverse handle value for connection.
128 *
129 * \param[in] service Target service context pointer
130 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800131 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800132 * \param[in] rhandle rhandle need to save
133 *
134 * \retval IPC_SUCCESS Success
135 * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
136 * \retval "Does not return" Panic for not find handle node
137 */
138static int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800139 struct tfm_conn_handle_t *conn_handle,
Mingyang Sund44522a2020-01-16 16:48:37 +0800140 void *rhandle)
Edison Ai764d41f2018-09-21 15:56:36 +0800141{
Ken Liuf250b8b2019-12-27 16:31:24 +0800142 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800143 /* Set reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800144 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800145
Summer Qin630c76b2020-05-20 10:32:58 +0800146 conn_handle->rhandle = rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800147 return IPC_SUCCESS;
148}
149
Mingyang Sund44522a2020-01-16 16:48:37 +0800150/**
151 * \brief Get reverse handle value from connection hanlde.
152 *
153 * \param[in] service Target service context pointer
154 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800155 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800156 *
157 * \retval void * Success
158 * \retval "Does not return" Panic for those:
159 * service pointer are NULL
160 * hanlde is \ref PSA_NULL_HANDLE
161 * handle node does not be found
162 */
163static void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800164 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800165{
Ken Liuf250b8b2019-12-27 16:31:24 +0800166 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800167 /* Get reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800168 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800169
Summer Qin630c76b2020-05-20 10:32:58 +0800170 return conn_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800171}
172
173/* Partition management functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800174
175/**
176 * \brief Get the service context by signal.
177 *
178 * \param[in] partition Partition context pointer
179 * \ref spm_partition_desc_t structures
180 * \param[in] signal Signal associated with inputs to the Secure
181 * Partition, \ref psa_signal_t
182 *
183 * \retval NULL Failed
184 * \retval "Not NULL" Target service context pointer,
185 * \ref tfm_spm_service_t structures
186 */
187static struct tfm_spm_service_t *
Mingyang Sunf3d29892019-07-10 17:50:23 +0800188 tfm_spm_get_service_by_signal(struct spm_partition_desc_t *partition,
189 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800190{
191 struct tfm_list_node_t *node, *head;
192 struct tfm_spm_service_t *service;
193
Ken Liuf250b8b2019-12-27 16:31:24 +0800194 TFM_CORE_ASSERT(partition);
Edison Ai764d41f2018-09-21 15:56:36 +0800195
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800196 if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
Edison Ai9059ea02019-11-28 13:46:14 +0800197 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800198 }
199
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800200 head = &partition->runtime_data.service_list;
Edison Ai764d41f2018-09-21 15:56:36 +0800201 TFM_LIST_FOR_EACH(node, head) {
202 service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t, list);
Summer Qine578c5b2019-08-16 16:42:16 +0800203 if (service->service_db->signal == signal) {
Edison Ai764d41f2018-09-21 15:56:36 +0800204 return service;
205 }
206 }
207 return NULL;
208}
209
210struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid)
211{
212 uint32_t i;
213 struct tfm_list_node_t *node, *head;
214 struct tfm_spm_service_t *service;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800215 struct spm_partition_desc_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800216
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200217 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800218 partition = &g_spm_partition_db.partitions[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800219 /* Skip partition without IPC flag */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800220 if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
Edison Ai764d41f2018-09-21 15:56:36 +0800221 continue;
222 }
223
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800224 if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
Edison Ai764d41f2018-09-21 15:56:36 +0800225 continue;
226 }
227
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800228 head = &partition->runtime_data.service_list;
Edison Ai764d41f2018-09-21 15:56:36 +0800229 TFM_LIST_FOR_EACH(node, head) {
230 service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t,
231 list);
Summer Qine578c5b2019-08-16 16:42:16 +0800232 if (service->service_db->sid == sid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800233 return service;
234 }
235 }
236 }
237 return NULL;
238}
239
Mingyang Sund44522a2020-01-16 16:48:37 +0800240/**
241 * \brief Get the partition context by partition ID.
242 *
243 * \param[in] partition_id Partition identity
244 *
245 * \retval NULL Failed
246 * \retval "Not NULL" Target partition context pointer,
247 * \ref spm_partition_desc_t structures
248 */
249static struct spm_partition_desc_t *
250 tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800251{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800252 uint32_t idx = get_partition_idx(partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800253
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800254 if (idx != SPM_INVALID_PARTITION_IDX) {
255 return &(g_spm_partition_db.partitions[idx]);
Edison Ai764d41f2018-09-21 15:56:36 +0800256 }
257 return NULL;
258}
259
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800260struct spm_partition_desc_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800261{
262 uint32_t spid;
263
Mingyang Sunf3d29892019-07-10 17:50:23 +0800264 spid = tfm_spm_partition_get_running_partition_id();
Edison Ai764d41f2018-09-21 15:56:36 +0800265
266 return tfm_spm_get_partition_by_id(spid);
267}
268
269int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530270 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800271{
Ken Liuf250b8b2019-12-27 16:31:24 +0800272 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800273
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530274 switch (service->service_db->version_policy) {
Edison Ai764d41f2018-09-21 15:56:36 +0800275 case TFM_VERSION_POLICY_RELAXED:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530276 if (version > service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800277 return IPC_ERROR_VERSION;
278 }
279 break;
280 case TFM_VERSION_POLICY_STRICT:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530281 if (version != service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800282 return IPC_ERROR_VERSION;
283 }
284 break;
285 default:
286 return IPC_ERROR_VERSION;
287 }
288 return IPC_SUCCESS;
289}
290
Edison Aie728fbf2019-11-13 09:37:12 +0800291int32_t tfm_spm_check_authorization(uint32_t sid,
292 struct tfm_spm_service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800293 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800294{
295 struct spm_partition_desc_t *partition = NULL;
296 int32_t i;
297
Ken Liuf250b8b2019-12-27 16:31:24 +0800298 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800299
300 if (ns_caller) {
301 if (!service->service_db->non_secure_client) {
302 return IPC_ERROR_GENERIC;
303 }
304 } else {
305 partition = tfm_spm_get_running_partition();
306 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800307 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800308 }
309
310 for (i = 0; i < partition->static_data->dependencies_num; i++) {
311 if (partition->static_data->p_dependencies[i] == sid) {
312 break;
313 }
314 }
315
316 if (i == partition->static_data->dependencies_num) {
317 return IPC_ERROR_GENERIC;
318 }
319 }
320 return IPC_SUCCESS;
321}
322
Edison Ai764d41f2018-09-21 15:56:36 +0800323/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800324
325/**
326 * \brief Get message context by message handle.
327 *
328 * \param[in] msg_handle Message handle which is a reference generated
329 * by the SPM to a specific message.
330 *
331 * \return The message body context pointer
332 * \ref tfm_msg_body_t structures
333 */
334static struct tfm_msg_body_t *
335 tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800336{
337 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200338 * The message handler passed by the caller is considered invalid in the
339 * following cases:
340 * 1. Not a valid message handle. (The address of a message is not the
341 * address of a possible handle from the pool
342 * 2. Handle not belongs to the caller partition (The handle is either
343 * unused, or owned by anither partition)
344 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800345 */
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200346 struct tfm_conn_handle_t *connection_handle_address;
Edison Ai764d41f2018-09-21 15:56:36 +0800347 struct tfm_msg_body_t *msg;
348 uint32_t partition_id;
349
350 msg = (struct tfm_msg_body_t *)msg_handle;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200351
352 connection_handle_address =
353 TFM_GET_CONTAINER_PTR(msg, struct tfm_conn_handle_t, internal_msg);
354
355 if (is_valid_chunk_data_in_pool(
356 conn_handle_pool, (uint8_t *)connection_handle_address) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800357 return NULL;
358 }
359
360 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200361 * Check that the magic number is correct. This proves that the message
362 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800363 */
364 if (msg->magic != TFM_MSG_MAGIC) {
365 return NULL;
366 }
367
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200368 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800369 partition_id = tfm_spm_partition_get_running_partition_id();
Summer Qin423dbef2019-08-22 15:59:35 +0800370 if (partition_id != msg->service->partition->static_data->partition_id) {
Edison Ai764d41f2018-09-21 15:56:36 +0800371 return NULL;
372 }
373
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200374 /*
375 * FixMe: For condition 1 it should be checked whether the message belongs
376 * to the service. Skipping this check isn't a security risk as even if the
377 * message belongs to another service, the handle belongs to the calling
378 * partition.
379 */
380
Edison Ai764d41f2018-09-21 15:56:36 +0800381 return msg;
382}
383
Edison Ai97115822019-08-01 14:22:19 +0800384struct tfm_msg_body_t *
Summer Qin630c76b2020-05-20 10:32:58 +0800385 tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800386{
Summer Qin630c76b2020-05-20 10:32:58 +0800387 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai97115822019-08-01 14:22:19 +0800388
Summer Qin630c76b2020-05-20 10:32:58 +0800389 return &(conn_handle->internal_msg);
Edison Ai97115822019-08-01 14:22:19 +0800390}
391
392void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
393 struct tfm_spm_service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800394 struct tfm_conn_handle_t *handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800395 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800396 psa_invec *invec, size_t in_len,
397 psa_outvec *outvec, size_t out_len,
398 psa_outvec *caller_outvec)
399{
Edison Ai764d41f2018-09-21 15:56:36 +0800400 uint32_t i;
401
Ken Liuf250b8b2019-12-27 16:31:24 +0800402 TFM_CORE_ASSERT(msg);
403 TFM_CORE_ASSERT(service);
404 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
405 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
406 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
407 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
408 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800409
Edison Ai764d41f2018-09-21 15:56:36 +0800410 /* Clear message buffer before using it */
Mingyang Sun94b1b412019-09-20 15:11:14 +0800411 tfm_core_util_memset(msg, 0, sizeof(struct tfm_msg_body_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800412
Ken Liu35f89392019-03-14 14:51:05 +0800413 tfm_event_init(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800414 msg->magic = TFM_MSG_MAGIC;
415 msg->service = service;
416 msg->handle = handle;
417 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800418 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800419
420 /* Copy contents */
421 msg->msg.type = type;
422
423 for (i = 0; i < in_len; i++) {
424 msg->msg.in_size[i] = invec[i].len;
425 msg->invec[i].base = invec[i].base;
426 }
427
428 for (i = 0; i < out_len; i++) {
429 msg->msg.out_size[i] = outvec[i].len;
430 msg->outvec[i].base = outvec[i].base;
431 /* Out len is used to record the writed number, set 0 here again */
432 msg->outvec[i].len = 0;
433 }
434
435 /* Use message address as handle */
436 msg->msg.handle = (psa_handle_t)msg;
437
438 /* For connected handle, set rhandle to every message */
Summer Qin630c76b2020-05-20 10:32:58 +0800439 if (handle) {
Edison Ai764d41f2018-09-21 15:56:36 +0800440 msg->msg.rhandle = tfm_spm_get_rhandle(service, handle);
441 }
David Hu46603dd2019-12-11 18:05:16 +0800442
443 /* Set the private data of NSPE client caller in multi-core topology */
444 if (TFM_CLIENT_ID_IS_NS(client_id)) {
445 tfm_rpc_set_caller_data(msg, client_id);
446 }
Edison Ai764d41f2018-09-21 15:56:36 +0800447}
448
449int32_t tfm_spm_send_event(struct tfm_spm_service_t *service,
450 struct tfm_msg_body_t *msg)
451{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800452 struct spm_partition_runtime_data_t *p_runtime_data =
453 &service->partition->runtime_data;
454
Ken Liuf250b8b2019-12-27 16:31:24 +0800455 TFM_CORE_ASSERT(service);
456 TFM_CORE_ASSERT(msg);
Edison Ai764d41f2018-09-21 15:56:36 +0800457
458 /* Enqueue message to service message queue */
459 if (tfm_msg_enqueue(&service->msg_queue, msg) != IPC_SUCCESS) {
460 return IPC_ERROR_GENERIC;
461 }
462
463 /* Messages put. Update signals */
Summer Qine578c5b2019-08-16 16:42:16 +0800464 p_runtime_data->signals |= service->service_db->signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800465
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800466 tfm_event_wake(&p_runtime_data->signal_evnt, (p_runtime_data->signals &
467 p_runtime_data->signal_mask));
Edison Ai764d41f2018-09-21 15:56:36 +0800468
David Hufb38d562019-09-23 15:58:34 +0800469 /*
470 * If it is a NS request via RPC, it is unnecessary to block current
471 * thread.
472 */
473 if (!is_tfm_rpc_msg(msg)) {
474 tfm_event_wait(&msg->ack_evnt);
475 }
Edison Ai764d41f2018-09-21 15:56:36 +0800476
477 return IPC_SUCCESS;
478}
479
Mingyang Sund44522a2020-01-16 16:48:37 +0800480/**
481 * \brief Get bottom of stack region for a partition
482 *
483 * \param[in] partition_idx Partition index
484 *
485 * \return Stack region bottom value
486 *
487 * \note This function doesn't check if partition_idx is valid.
488 */
489static uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
Edison Ai7aff9e82019-07-11 14:56:46 +0800490{
491 return g_spm_partition_db.partitions[partition_idx].
Summer Qin423dbef2019-08-22 15:59:35 +0800492 memory_data->stack_bottom;
Edison Ai7aff9e82019-07-11 14:56:46 +0800493}
494
Mingyang Sund44522a2020-01-16 16:48:37 +0800495/**
496 * \brief Get top of stack region for a partition
497 *
498 * \param[in] partition_idx Partition index
499 *
500 * \return Stack region top value
501 *
502 * \note This function doesn't check if partition_idx is valid.
503 */
504static uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
Edison Ai7aff9e82019-07-11 14:56:46 +0800505{
Summer Qin423dbef2019-08-22 15:59:35 +0800506 return g_spm_partition_db.partitions[partition_idx].memory_data->stack_top;
Edison Ai7aff9e82019-07-11 14:56:46 +0800507}
508
Mingyang Sunf3d29892019-07-10 17:50:23 +0800509uint32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800510{
Summer Qin66f1e032020-01-06 15:40:03 +0800511 struct tfm_core_thread_t *pth = tfm_core_thrd_get_curr_thread();
Edison Ai764d41f2018-09-21 15:56:36 +0800512 struct spm_partition_desc_t *partition;
Summer Qinb5da9cc2019-08-26 15:19:45 +0800513 struct spm_partition_runtime_data_t *r_data;
Edison Ai764d41f2018-09-21 15:56:36 +0800514
Summer Qinb5da9cc2019-08-26 15:19:45 +0800515 r_data = TFM_GET_CONTAINER_PTR(pth, struct spm_partition_runtime_data_t,
516 sp_thrd);
517 partition = TFM_GET_CONTAINER_PTR(r_data, struct spm_partition_desc_t,
518 runtime_data);
Summer Qin423dbef2019-08-22 15:59:35 +0800519 return partition->static_data->partition_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800520}
521
Summer Qin66f1e032020-01-06 15:40:03 +0800522static struct tfm_core_thread_t *
Mingyang Sunf3d29892019-07-10 17:50:23 +0800523 tfm_spm_partition_get_thread_info(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800524{
Summer Qinb5da9cc2019-08-26 15:19:45 +0800525 return &g_spm_partition_db.partitions[partition_idx].runtime_data.sp_thrd;
Edison Ai764d41f2018-09-21 15:56:36 +0800526}
527
Summer Qin66f1e032020-01-06 15:40:03 +0800528static tfm_core_thrd_entry_t
Mingyang Sunf3d29892019-07-10 17:50:23 +0800529 tfm_spm_partition_get_init_func(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800530{
Summer Qin66f1e032020-01-06 15:40:03 +0800531 return (tfm_core_thrd_entry_t)(g_spm_partition_db.partitions[partition_idx].
Summer Qin423dbef2019-08-22 15:59:35 +0800532 static_data->partition_init);
Edison Ai764d41f2018-09-21 15:56:36 +0800533}
534
Mingyang Sunf3d29892019-07-10 17:50:23 +0800535static uint32_t tfm_spm_partition_get_priority(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800536{
Summer Qin423dbef2019-08-22 15:59:35 +0800537 return g_spm_partition_db.partitions[partition_idx].static_data->
Edison Ai764d41f2018-09-21 15:56:36 +0800538 partition_priority;
539}
540
Summer Qin43c185d2019-10-10 15:44:42 +0800541int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800542 enum tfm_memory_access_e access,
543 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800544{
Hugues de Valon99578562019-06-18 16:08:51 +0100545 enum tfm_status_e err;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800546
547 /* If len is zero, this indicates an empty buffer and base is ignored */
548 if (len == 0) {
549 return IPC_SUCCESS;
550 }
551
552 if (!buffer) {
553 return IPC_ERROR_BAD_PARAMETERS;
554 }
555
556 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
557 return IPC_ERROR_MEMORY_CHECK;
558 }
559
Summer Qin424d4db2019-03-25 14:09:51 +0800560 if (access == TFM_MEMORY_ACCESS_RW) {
Summer Qineb537e52019-03-29 09:57:10 +0800561 err = tfm_core_has_write_access_to_region(buffer, len, ns_caller,
562 privileged);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800563 } else {
Summer Qineb537e52019-03-29 09:57:10 +0800564 err = tfm_core_has_read_access_to_region(buffer, len, ns_caller,
565 privileged);
Summer Qin424d4db2019-03-25 14:09:51 +0800566 }
Summer Qin0fc3f592019-04-11 16:00:10 +0800567 if (err == TFM_SUCCESS) {
Summer Qin424d4db2019-03-25 14:09:51 +0800568 return IPC_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800569 }
570
571 return IPC_ERROR_MEMORY_CHECK;
572}
573
Ken Liuce2692d2020-02-11 12:39:36 +0800574uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800575{
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800576 uint32_t i, j, num;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800577 struct spm_partition_desc_t *partition;
Summer Qin66f1e032020-01-06 15:40:03 +0800578 struct tfm_core_thread_t *pth, *p_ns_entry_thread = NULL;
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100579 const struct tfm_spm_partition_platform_data_t **platform_data_p;
Edison Ai764d41f2018-09-21 15:56:36 +0800580
581 tfm_pool_init(conn_handle_pool,
582 POOL_BUFFER_SIZE(conn_handle_pool),
583 sizeof(struct tfm_conn_handle_t),
584 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800585
586 /* Init partition first for it will be used when init service */
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200587 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800588 partition = &g_spm_partition_db.partitions[i];
Edison Aif0501702019-10-11 14:36:42 +0800589
590 /* Check if the PSA framework version matches. */
591 if (partition->static_data->psa_framework_version !=
592 PSA_FRAMEWORK_VERSION) {
593 ERROR_MSG("Warning: PSA Framework Verison is not matched!");
594 continue;
595 }
596
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100597 platform_data_p = partition->platform_data_list;
598 if (platform_data_p != NULL) {
599 while ((*platform_data_p) != NULL) {
Edison Ai6be3df12020-02-14 22:14:33 +0800600 if (tfm_spm_hal_configure_default_isolation(i,
601 *platform_data_p) != TFM_PLAT_ERR_SUCCESS) {
602 tfm_core_panic();
603 }
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100604 ++platform_data_p;
605 }
606 }
607
Edison Ai764d41f2018-09-21 15:56:36 +0800608 if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
609 continue;
610 }
Ken Liu35f89392019-03-14 14:51:05 +0800611
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800612 /* Add PSA_DOORBELL signal to assigned_signals */
613 partition->runtime_data.assigned_signals |= PSA_DOORBELL;
614
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800615 /* TODO: This can be optimized by generating the assigned signal
616 * in code generation time.
617 */
618 for (j = 0; j < tfm_core_irq_signals_count; ++j) {
619 if (tfm_core_irq_signals[j].partition_id ==
620 partition->static_data->partition_id) {
621 partition->runtime_data.assigned_signals |=
622 tfm_core_irq_signals[j].signal_value;
623 }
624 }
625
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800626 tfm_event_init(&partition->runtime_data.signal_evnt);
627 tfm_list_init(&partition->runtime_data.service_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800628
Mingyang Sunf3d29892019-07-10 17:50:23 +0800629 pth = tfm_spm_partition_get_thread_info(i);
Edison Ai764d41f2018-09-21 15:56:36 +0800630 if (!pth) {
Edison Ai9059ea02019-11-28 13:46:14 +0800631 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800632 }
633
Summer Qin66f1e032020-01-06 15:40:03 +0800634 tfm_core_thrd_init(pth,
635 tfm_spm_partition_get_init_func(i),
636 NULL,
637 (uintptr_t)tfm_spm_partition_get_stack_top(i),
638 (uintptr_t)tfm_spm_partition_get_stack_bottom(i));
Edison Ai788bae22019-02-18 17:38:59 +0800639
Mingyang Sunf3d29892019-07-10 17:50:23 +0800640 pth->prior = tfm_spm_partition_get_priority(i);
Edison Ai764d41f2018-09-21 15:56:36 +0800641
Ken Liu490281d2019-12-30 15:55:26 +0800642 if (partition->static_data->partition_id == TFM_SP_NON_SECURE_ID) {
643 p_ns_entry_thread = pth;
Ken Liu5248af22019-12-29 12:47:13 +0800644 pth->param = (void *)tfm_spm_hal_get_ns_entry_point();
Ken Liu490281d2019-12-30 15:55:26 +0800645 }
646
Edison Ai764d41f2018-09-21 15:56:36 +0800647 /* Kick off */
Summer Qin66f1e032020-01-06 15:40:03 +0800648 if (tfm_core_thrd_start(pth) != THRD_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800649 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800650 }
651 }
652
653 /* Init Service */
Summer Qind99509f2019-08-02 17:36:58 +0800654 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
Edison Ai764d41f2018-09-21 15:56:36 +0800655 for (i = 0; i < num; i++) {
Summer Qine578c5b2019-08-16 16:42:16 +0800656 service[i].service_db = &service_db[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800657 partition =
Summer Qine578c5b2019-08-16 16:42:16 +0800658 tfm_spm_get_partition_by_id(service[i].service_db->partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800659 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800660 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800661 }
Summer Qind99509f2019-08-02 17:36:58 +0800662 service[i].partition = partition;
Jaykumar Pitambarbhai Patel0c7a0382020-01-09 15:25:58 +0530663 partition->runtime_data.assigned_signals |= service[i].service_db->signal;
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800664
Summer Qind99509f2019-08-02 17:36:58 +0800665 tfm_list_init(&service[i].handle_list);
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800666 tfm_list_add_tail(&partition->runtime_data.service_list,
Summer Qind99509f2019-08-02 17:36:58 +0800667 &service[i].list);
Edison Ai764d41f2018-09-21 15:56:36 +0800668 }
669
Ken Liu483f5da2019-04-24 10:45:21 +0800670 /*
671 * All threads initialized, start the scheduler.
672 *
673 * NOTE:
Ken Liu490281d2019-12-30 15:55:26 +0800674 * It is worthy to give the thread object to scheduler if the background
675 * context belongs to one of the threads. Here the background thread is the
676 * initialization thread who calls SPM SVC, which re-uses the non-secure
677 * entry thread's stack. After SPM initialization is done, this stack is
678 * cleaned up and the background context is never going to return. Tell
679 * the scheduler that the current thread is non-secure entry thread.
Ken Liu483f5da2019-04-24 10:45:21 +0800680 */
Summer Qin66f1e032020-01-06 15:40:03 +0800681 tfm_core_thrd_start_scheduler(p_ns_entry_thread);
Ken Liuce2692d2020-02-11 12:39:36 +0800682
Summer Qind2ad7e72020-01-06 18:16:35 +0800683 return p_ns_entry_thread->arch_ctx.lr;
Edison Ai764d41f2018-09-21 15:56:36 +0800684}
Ken Liu2d175172019-03-21 17:08:41 +0800685
Summer Qind2ad7e72020-01-06 18:16:35 +0800686void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx)
Ken Liu2d175172019-03-21 17:08:41 +0800687{
688#if TFM_LVL == 2
689 struct spm_partition_desc_t *p_next_partition;
Summer Qinb5da9cc2019-08-26 15:19:45 +0800690 struct spm_partition_runtime_data_t *r_data;
Ken Liu2d175172019-03-21 17:08:41 +0800691 uint32_t is_privileged;
692#endif
Summer Qin66f1e032020-01-06 15:40:03 +0800693 struct tfm_core_thread_t *pth_next = tfm_core_thrd_get_next_thread();
694 struct tfm_core_thread_t *pth_curr = tfm_core_thrd_get_curr_thread();
Ken Liu2d175172019-03-21 17:08:41 +0800695
Mate Toth-Pal32b2ccd2019-04-26 10:00:16 +0200696 if (pth_next != NULL && pth_curr != pth_next) {
Ken Liu2d175172019-03-21 17:08:41 +0800697#if TFM_LVL == 2
Summer Qinb5da9cc2019-08-26 15:19:45 +0800698 r_data = TFM_GET_CONTAINER_PTR(pth_next,
699 struct spm_partition_runtime_data_t,
700 sp_thrd);
701 p_next_partition = TFM_GET_CONTAINER_PTR(r_data,
Ken Liu2d175172019-03-21 17:08:41 +0800702 struct spm_partition_desc_t,
Summer Qinb5da9cc2019-08-26 15:19:45 +0800703 runtime_data);
Ken Liu2d175172019-03-21 17:08:41 +0800704
Summer Qin423dbef2019-08-22 15:59:35 +0800705 if (p_next_partition->static_data->partition_flags &
Ken Liu2d175172019-03-21 17:08:41 +0800706 SPM_PART_FLAG_PSA_ROT) {
707 is_privileged = TFM_PARTITION_PRIVILEGED_MODE;
708 } else {
709 is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE;
710 }
711
712 tfm_spm_partition_change_privilege(is_privileged);
713#endif
Mate Toth-Palc430b992019-05-09 21:01:14 +0200714
Summer Qind2ad7e72020-01-06 18:16:35 +0800715 tfm_core_thrd_switch_context(p_actx, pth_curr, pth_next);
Ken Liu2d175172019-03-21 17:08:41 +0800716 }
David Hufb38d562019-09-23 15:58:34 +0800717
718 /*
719 * Handle pending mailbox message from NS in multi-core topology.
720 * Empty operation on single Armv8-M platform.
721 */
722 tfm_rpc_client_call_handler();
Ken Liu2d175172019-03-21 17:08:41 +0800723}
Mingyang Sund44522a2020-01-16 16:48:37 +0800724
725/*********************** SPM functions for PSA Client APIs *******************/
726
727uint32_t tfm_spm_psa_framework_version(void)
728{
729 return tfm_spm_client_psa_framework_version();
730}
731
732uint32_t tfm_spm_psa_version(uint32_t *args, bool ns_caller)
733{
734 uint32_t sid;
735
736 TFM_CORE_ASSERT(args != NULL);
737 sid = (uint32_t)args[0];
738
739 return tfm_spm_client_psa_version(sid, ns_caller);
740}
741
742psa_status_t tfm_spm_psa_connect(uint32_t *args, bool ns_caller)
743{
744 uint32_t sid;
745 uint32_t version;
746
747 TFM_CORE_ASSERT(args != NULL);
748 sid = (uint32_t)args[0];
749 version = (uint32_t)args[1];
750
751 return tfm_spm_client_psa_connect(sid, version, ns_caller);
752}
753
754psa_status_t tfm_spm_psa_call(uint32_t *args, bool ns_caller, uint32_t lr)
755{
756 psa_handle_t handle;
757 psa_invec *inptr;
758 psa_outvec *outptr;
759 size_t in_num, out_num;
760 struct spm_partition_desc_t *partition = NULL;
761 uint32_t privileged;
762 int32_t type;
763 struct tfm_control_parameter_t ctrl_param;
764
765 TFM_CORE_ASSERT(args != NULL);
766 handle = (psa_handle_t)args[0];
767
768 partition = tfm_spm_get_running_partition();
769 if (!partition) {
770 tfm_core_panic();
771 }
772 privileged = tfm_spm_partition_get_privileged_mode(
773 partition->static_data->partition_flags);
774
775 /*
776 * Read parameters from the arguments. It is a fatal error if the
777 * memory reference for buffer is invalid or not readable.
778 */
779 if (tfm_memory_check((const void *)args[1],
780 sizeof(struct tfm_control_parameter_t), ns_caller,
781 TFM_MEMORY_ACCESS_RW, privileged) != IPC_SUCCESS) {
782 tfm_core_panic();
783 }
784
785 tfm_core_util_memcpy(&ctrl_param,
786 (const void *)args[1],
787 sizeof(ctrl_param));
788
789 type = ctrl_param.type;
790 in_num = ctrl_param.in_len;
791 out_num = ctrl_param.out_len;
792 inptr = (psa_invec *)args[2];
793 outptr = (psa_outvec *)args[3];
794
795 /* The request type must be zero or positive. */
796 if (type < 0) {
797 tfm_core_panic();
798 }
799
800 return tfm_spm_client_psa_call(handle, type, inptr, in_num, outptr, out_num,
801 ns_caller, privileged);
802}
803
804void tfm_spm_psa_close(uint32_t *args, bool ns_caller)
805{
806 psa_handle_t handle;
807
808 TFM_CORE_ASSERT(args != NULL);
809 handle = args[0];
810
811 tfm_spm_client_psa_close(handle, ns_caller);
812}
813
814uint32_t tfm_spm_get_lifecycle_state(void)
815{
816 /*
817 * FixMe: return PSA_LIFECYCLE_UNKNOWN to the caller directly. It will be
818 * implemented in the future.
819 */
820 return PSA_LIFECYCLE_UNKNOWN;
821}
822
823/********************* SPM functions for PSA Service APIs ********************/
824
825psa_signal_t tfm_spm_psa_wait(uint32_t *args)
826{
827 psa_signal_t signal_mask;
828 uint32_t timeout;
829 struct spm_partition_desc_t *partition = NULL;
830
831 TFM_CORE_ASSERT(args != NULL);
832 signal_mask = (psa_signal_t)args[0];
833 timeout = args[1];
834
835 /*
836 * Timeout[30:0] are reserved for future use.
837 * SPM must ignore the value of RES.
838 */
839 timeout &= PSA_TIMEOUT_MASK;
840
841 partition = tfm_spm_get_running_partition();
842 if (!partition) {
843 tfm_core_panic();
844 }
845
846 /*
847 * It is a PROGRAMMER ERROR if the signal_mask does not include any assigned
848 * signals.
849 */
850 if ((partition->runtime_data.assigned_signals & signal_mask) == 0) {
851 tfm_core_panic();
852 }
853
854 /*
855 * Expected signals are included in signal wait mask, ignored signals
856 * should not be set and affect caller thread state. Save this mask for
857 * further checking while signals are ready to be set.
858 */
859 partition->runtime_data.signal_mask = signal_mask;
860
861 /*
862 * tfm_event_wait() blocks the caller thread if no signals are available.
863 * In this case, the return value of this function is temporary set into
864 * runtime context. After new signal(s) are available, the return value
865 * is updated with the available signal(s) and blocked thread gets to run.
866 */
867 if (timeout == PSA_BLOCK &&
868 (partition->runtime_data.signals & signal_mask) == 0) {
869 tfm_event_wait(&partition->runtime_data.signal_evnt);
870 }
871
872 return partition->runtime_data.signals & signal_mask;
873}
874
875psa_status_t tfm_spm_psa_get(uint32_t *args)
876{
877 psa_signal_t signal;
878 psa_msg_t *msg = NULL;
879 struct tfm_spm_service_t *service = NULL;
880 struct tfm_msg_body_t *tmp_msg = NULL;
881 struct spm_partition_desc_t *partition = NULL;
882 uint32_t privileged;
883
884 TFM_CORE_ASSERT(args != NULL);
885 signal = (psa_signal_t)args[0];
886 msg = (psa_msg_t *)args[1];
887
888 /*
889 * Only one message could be retrieved every time for psa_get(). It is a
890 * fatal error if the input signal has more than a signal bit set.
891 */
Ken Liu410ada52020-01-08 11:37:27 +0800892 if (!tfm_is_one_bit_set(signal)) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800893 tfm_core_panic();
894 }
895
896 partition = tfm_spm_get_running_partition();
897 if (!partition) {
898 tfm_core_panic();
899 }
900 privileged = tfm_spm_partition_get_privileged_mode(
901 partition->static_data->partition_flags);
902
903 /*
904 * Write the message to the service buffer. It is a fatal error if the
905 * input msg pointer is not a valid memory reference or not read-write.
906 */
907 if (tfm_memory_check(msg, sizeof(psa_msg_t), false, TFM_MEMORY_ACCESS_RW,
908 privileged) != IPC_SUCCESS) {
909 tfm_core_panic();
910 }
911
912 /*
913 * It is a fatal error if the caller call psa_get() when no message has
914 * been set. The caller must call this function after an RoT Service signal
915 * is returned by psa_wait().
916 */
917 if (partition->runtime_data.signals == 0) {
918 tfm_core_panic();
919 }
920
921 /*
922 * It is a fatal error if the RoT Service signal is not currently asserted.
923 */
924 if ((partition->runtime_data.signals & signal) == 0) {
925 tfm_core_panic();
926 }
927
928 /*
929 * Get RoT service by signal from partition. It is a fatal error if getting
930 * failed, which means the input signal is not correspond to an RoT service.
931 */
932 service = tfm_spm_get_service_by_signal(partition, signal);
933 if (!service) {
934 tfm_core_panic();
935 }
936
937 tmp_msg = tfm_msg_dequeue(&service->msg_queue);
938 if (!tmp_msg) {
939 return PSA_ERROR_DOES_NOT_EXIST;
940 }
941
942 ((struct tfm_conn_handle_t *)(tmp_msg->handle))->status =
943 TFM_HANDLE_STATUS_ACTIVE;
944
945 tfm_core_util_memcpy(msg, &tmp_msg->msg, sizeof(psa_msg_t));
946
947 /*
948 * There may be multiple messages for this RoT Service signal, do not clear
949 * its mask until no remaining message.
950 */
951 if (tfm_msg_queue_is_empty(&service->msg_queue)) {
952 partition->runtime_data.signals &= ~signal;
953 }
954
955 return PSA_SUCCESS;
956}
957
958void tfm_spm_psa_set_rhandle(uint32_t *args)
959{
960 psa_handle_t msg_handle;
961 void *rhandle = NULL;
962 struct tfm_msg_body_t *msg = NULL;
963
964 TFM_CORE_ASSERT(args != NULL);
965 msg_handle = (psa_handle_t)args[0];
966 rhandle = (void *)args[1];
967
968 /* It is a fatal error if message handle is invalid */
969 msg = tfm_spm_get_msg_from_handle(msg_handle);
970 if (!msg) {
971 tfm_core_panic();
972 }
973
974 msg->msg.rhandle = rhandle;
975
976 /* Store reverse handle for following client calls. */
977 tfm_spm_set_rhandle(msg->service, msg->handle, rhandle);
978}
979
980size_t tfm_spm_psa_read(uint32_t *args)
981{
982 psa_handle_t msg_handle;
983 uint32_t invec_idx;
984 void *buffer = NULL;
985 size_t num_bytes;
986 size_t bytes;
987 struct tfm_msg_body_t *msg = NULL;
988 uint32_t privileged;
989 struct spm_partition_desc_t *partition = NULL;
990
991 TFM_CORE_ASSERT(args != NULL);
992 msg_handle = (psa_handle_t)args[0];
993 invec_idx = args[1];
994 buffer = (void *)args[2];
995 num_bytes = (size_t)args[3];
996
997 /* It is a fatal error if message handle is invalid */
998 msg = tfm_spm_get_msg_from_handle(msg_handle);
999 if (!msg) {
1000 tfm_core_panic();
1001 }
1002
1003 partition = msg->service->partition;
1004 privileged = tfm_spm_partition_get_privileged_mode(
1005 partition->static_data->partition_flags);
1006
1007 /*
1008 * It is a fatal error if message handle does not refer to a request
1009 * message
1010 */
1011 if (msg->msg.type < PSA_IPC_CALL) {
1012 tfm_core_panic();
1013 }
1014
1015 /*
1016 * It is a fatal error if invec_idx is equal to or greater than
1017 * PSA_MAX_IOVEC
1018 */
1019 if (invec_idx >= PSA_MAX_IOVEC) {
1020 tfm_core_panic();
1021 }
1022
1023 /* There was no remaining data in this input vector */
1024 if (msg->msg.in_size[invec_idx] == 0) {
1025 return 0;
1026 }
1027
1028 /*
1029 * Copy the client data to the service buffer. It is a fatal error
1030 * if the memory reference for buffer is invalid or not read-write.
1031 */
1032 if (tfm_memory_check(buffer, num_bytes, false,
1033 TFM_MEMORY_ACCESS_RW, privileged) != IPC_SUCCESS) {
1034 tfm_core_panic();
1035 }
1036
1037 bytes = num_bytes > msg->msg.in_size[invec_idx] ?
1038 msg->msg.in_size[invec_idx] : num_bytes;
1039
1040 tfm_core_util_memcpy(buffer, msg->invec[invec_idx].base, bytes);
1041
1042 /* There maybe some remaining data */
1043 msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base + bytes;
1044 msg->msg.in_size[invec_idx] -= bytes;
1045
1046 return bytes;
1047}
1048
1049size_t tfm_spm_psa_skip(uint32_t *args)
1050{
1051 psa_handle_t msg_handle;
1052 uint32_t invec_idx;
1053 size_t num_bytes;
1054 struct tfm_msg_body_t *msg = NULL;
1055
1056 TFM_CORE_ASSERT(args != NULL);
1057 msg_handle = (psa_handle_t)args[0];
1058 invec_idx = args[1];
1059 num_bytes = (size_t)args[2];
1060
1061 /* It is a fatal error if message handle is invalid */
1062 msg = tfm_spm_get_msg_from_handle(msg_handle);
1063 if (!msg) {
1064 tfm_core_panic();
1065 }
1066
1067 /*
1068 * It is a fatal error if message handle does not refer to a request
1069 * message
1070 */
1071 if (msg->msg.type < PSA_IPC_CALL) {
1072 tfm_core_panic();
1073 }
1074
1075 /*
1076 * It is a fatal error if invec_idx is equal to or greater than
1077 * PSA_MAX_IOVEC
1078 */
1079 if (invec_idx >= PSA_MAX_IOVEC) {
1080 tfm_core_panic();
1081 }
1082
1083 /* There was no remaining data in this input vector */
1084 if (msg->msg.in_size[invec_idx] == 0) {
1085 return 0;
1086 }
1087
1088 /*
1089 * If num_bytes is greater than the remaining size of the input vector then
1090 * the remaining size of the input vector is used.
1091 */
1092 if (num_bytes > msg->msg.in_size[invec_idx]) {
1093 num_bytes = msg->msg.in_size[invec_idx];
1094 }
1095
1096 /* There maybe some remaining data */
1097 msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base +
1098 num_bytes;
1099 msg->msg.in_size[invec_idx] -= num_bytes;
1100
1101 return num_bytes;
1102}
1103
1104void tfm_spm_psa_write(uint32_t *args)
1105{
1106 psa_handle_t msg_handle;
1107 uint32_t outvec_idx;
1108 void *buffer = NULL;
1109 size_t num_bytes;
1110 struct tfm_msg_body_t *msg = NULL;
1111 uint32_t privileged;
1112 struct spm_partition_desc_t *partition = NULL;
1113
1114 TFM_CORE_ASSERT(args != NULL);
1115 msg_handle = (psa_handle_t)args[0];
1116 outvec_idx = args[1];
1117 buffer = (void *)args[2];
1118 num_bytes = (size_t)args[3];
1119
1120 /* It is a fatal error if message handle is invalid */
1121 msg = tfm_spm_get_msg_from_handle(msg_handle);
1122 if (!msg) {
1123 tfm_core_panic();
1124 }
1125
1126 partition = msg->service->partition;
1127 privileged = tfm_spm_partition_get_privileged_mode(
1128 partition->static_data->partition_flags);
1129
1130 /*
1131 * It is a fatal error if message handle does not refer to a request
1132 * message
1133 */
1134 if (msg->msg.type < PSA_IPC_CALL) {
1135 tfm_core_panic();
1136 }
1137
1138 /*
1139 * It is a fatal error if outvec_idx is equal to or greater than
1140 * PSA_MAX_IOVEC
1141 */
1142 if (outvec_idx >= PSA_MAX_IOVEC) {
1143 tfm_core_panic();
1144 }
1145
1146 /*
1147 * It is a fatal error if the call attempts to write data past the end of
1148 * the client output vector
1149 */
1150 if (num_bytes > msg->msg.out_size[outvec_idx] -
1151 msg->outvec[outvec_idx].len) {
1152 tfm_core_panic();
1153 }
1154
1155 /*
1156 * Copy the service buffer to client outvecs. It is a fatal error
1157 * if the memory reference for buffer is invalid or not readable.
1158 */
1159 if (tfm_memory_check(buffer, num_bytes, false,
1160 TFM_MEMORY_ACCESS_RO, privileged) != IPC_SUCCESS) {
1161 tfm_core_panic();
1162 }
1163
1164 tfm_core_util_memcpy((char *)msg->outvec[outvec_idx].base +
1165 msg->outvec[outvec_idx].len, buffer, num_bytes);
1166
1167 /* Update the write number */
1168 msg->outvec[outvec_idx].len += num_bytes;
1169}
1170
1171static void update_caller_outvec_len(struct tfm_msg_body_t *msg)
1172{
1173 uint32_t i;
1174
1175 /*
1176 * FixeMe: abstract these part into dedicated functions to avoid
1177 * accessing thread context in psa layer
1178 */
1179 /* If it is a NS request via RPC, the owner of this message is not set */
1180 if (!is_tfm_rpc_msg(msg)) {
1181 TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK);
1182 }
1183
1184 for (i = 0; i < PSA_MAX_IOVEC; i++) {
1185 if (msg->msg.out_size[i] == 0) {
1186 continue;
1187 }
1188
1189 TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
1190
1191 msg->caller_outvec[i].len = msg->outvec[i].len;
1192 }
1193}
1194
1195void tfm_spm_psa_reply(uint32_t *args)
1196{
1197 psa_handle_t msg_handle;
1198 psa_status_t status;
1199 struct tfm_spm_service_t *service = NULL;
1200 struct tfm_msg_body_t *msg = NULL;
1201 int32_t ret = PSA_SUCCESS;
1202
1203 TFM_CORE_ASSERT(args != NULL);
1204 msg_handle = (psa_handle_t)args[0];
1205 status = (psa_status_t)args[1];
1206
1207 /* It is a fatal error if message handle is invalid */
1208 msg = tfm_spm_get_msg_from_handle(msg_handle);
1209 if (!msg) {
1210 tfm_core_panic();
1211 }
1212
1213 /*
1214 * RoT Service information is needed in this function, stored it in message
1215 * body structure. Only two parameters are passed in this function: handle
1216 * and status, so it is useful and simply to do like this.
1217 */
1218 service = msg->service;
1219 if (!service) {
1220 tfm_core_panic();
1221 }
1222
1223 /*
1224 * Three type of message are passed in this function: CONNECTION, REQUEST,
1225 * DISCONNECTION. It needs to process differently for each type.
1226 */
1227 switch (msg->msg.type) {
1228 case PSA_IPC_CONNECT:
1229 /*
1230 * Reply to PSA_IPC_CONNECT message. Connect handle is returned if the
1231 * input status is PSA_SUCCESS. Others return values are based on the
1232 * input status.
1233 */
1234 if (status == PSA_SUCCESS) {
Summer Qin630c76b2020-05-20 10:32:58 +08001235 ret = (psa_handle_t)msg->handle;
Mingyang Sund44522a2020-01-16 16:48:37 +08001236 } else if (status == PSA_ERROR_CONNECTION_REFUSED) {
1237 /* Refuse the client connection, indicating a permanent error. */
1238 tfm_spm_free_conn_handle(service, msg->handle);
1239 ret = PSA_ERROR_CONNECTION_REFUSED;
1240 } else if (status == PSA_ERROR_CONNECTION_BUSY) {
1241 /* Fail the client connection, indicating a transient error. */
1242 ret = PSA_ERROR_CONNECTION_BUSY;
1243 } else {
1244 tfm_core_panic();
1245 }
1246 break;
1247 case PSA_IPC_DISCONNECT:
1248 /* Service handle is not used anymore */
1249 tfm_spm_free_conn_handle(service, msg->handle);
1250
1251 /*
1252 * If the message type is PSA_IPC_DISCONNECT, then the status code is
1253 * ignored
1254 */
1255 break;
1256 default:
1257 if (msg->msg.type >= PSA_IPC_CALL) {
1258 /* Reply to a request message. Return values are based on status */
1259 ret = status;
1260 /*
1261 * The total number of bytes written to a single parameter must be
1262 * reported to the client by updating the len member of the
1263 * psa_outvec structure for the parameter before returning from
1264 * psa_call().
1265 */
1266 update_caller_outvec_len(msg);
1267 } else {
1268 tfm_core_panic();
1269 }
1270 }
1271
1272 if (ret == PSA_ERROR_PROGRAMMER_ERROR) {
1273 /*
1274 * If the source of the programmer error is a Secure Partition, the SPM
1275 * must panic the Secure Partition in response to a PROGRAMMER ERROR.
1276 */
1277 if (TFM_CLIENT_ID_IS_NS(msg->msg.client_id)) {
1278 ((struct tfm_conn_handle_t *)(msg->handle))->status =
1279 TFM_HANDLE_STATUS_CONNECT_ERROR;
1280 } else {
1281 tfm_core_panic();
1282 }
1283 } else {
1284 ((struct tfm_conn_handle_t *)(msg->handle))->status =
1285 TFM_HANDLE_STATUS_IDLE;
1286 }
1287
1288 if (is_tfm_rpc_msg(msg)) {
1289 tfm_rpc_client_call_reply(msg, ret);
1290 } else {
1291 tfm_event_wake(&msg->ack_evnt, ret);
1292 }
1293}
1294
1295/**
1296 * \brief notify the partition with the signal.
1297 *
1298 * \param[in] partition_id The ID of the partition to be notified.
1299 * \param[in] signal The signal that the partition is to be notified
1300 * with.
1301 *
1302 * \retval void Success.
1303 * \retval "Does not return" If partition_id is invalid.
1304 */
1305static void notify_with_signal(int32_t partition_id, psa_signal_t signal)
1306{
1307 struct spm_partition_desc_t *partition = NULL;
1308
1309 /*
1310 * The value of partition_id must be greater than zero as the target of
1311 * notification must be a Secure Partition, providing a Non-secure
1312 * Partition ID is a fatal error.
1313 */
1314 if (!TFM_CLIENT_ID_IS_S(partition_id)) {
1315 tfm_core_panic();
1316 }
1317
1318 /*
1319 * It is a fatal error if partition_id does not correspond to a Secure
1320 * Partition.
1321 */
1322 partition = tfm_spm_get_partition_by_id(partition_id);
1323 if (!partition) {
1324 tfm_core_panic();
1325 }
1326
1327 partition->runtime_data.signals |= signal;
1328
1329 /*
1330 * The target partition may be blocked with waiting for signals after
1331 * called psa_wait(). Set the return value with the available signals
1332 * before wake it up with tfm_event_signal().
1333 */
1334 tfm_event_wake(&partition->runtime_data.signal_evnt,
1335 partition->runtime_data.signals &
1336 partition->runtime_data.signal_mask);
1337}
1338
1339void tfm_spm_psa_notify(uint32_t *args)
1340{
1341 int32_t partition_id;
1342
1343 TFM_CORE_ASSERT(args != NULL);
1344 partition_id = (int32_t)args[0];
1345
1346 notify_with_signal(partition_id, PSA_DOORBELL);
1347}
1348
1349/**
1350 * \brief assert signal for a given IRQ line.
1351 *
1352 * \param[in] partition_id The ID of the partition which handles this IRQ
1353 * \param[in] signal The signal associated with this IRQ
1354 * \param[in] irq_line The number of the IRQ line
1355 *
1356 * \retval void Success.
1357 * \retval "Does not return" Partition ID is invalid
1358 */
1359void tfm_irq_handler(uint32_t partition_id, psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +01001360 IRQn_Type irq_line)
Mingyang Sund44522a2020-01-16 16:48:37 +08001361{
1362 tfm_spm_hal_disable_irq(irq_line);
1363 notify_with_signal(partition_id, signal);
1364}
1365
1366void tfm_spm_psa_clear(void)
1367{
1368 struct spm_partition_desc_t *partition = NULL;
1369
1370 partition = tfm_spm_get_running_partition();
1371 if (!partition) {
1372 tfm_core_panic();
1373 }
1374
1375 /*
1376 * It is a fatal error if the Secure Partition's doorbell signal is not
1377 * currently asserted.
1378 */
1379 if ((partition->runtime_data.signals & PSA_DOORBELL) == 0) {
1380 tfm_core_panic();
1381 }
1382 partition->runtime_data.signals &= ~PSA_DOORBELL;
1383}
1384
1385void tfm_spm_psa_panic(void)
1386{
1387 /*
1388 * PSA FF recommends that the SPM causes the system to restart when a secure
1389 * partition panics.
1390 */
1391 tfm_spm_hal_system_reset();
1392}
1393
1394/**
1395 * \brief Return the IRQ line number associated with a signal
1396 *
1397 * \param[in] partition_id The ID of the partition in which we look for
1398 * the signal.
1399 * \param[in] signal The signal we do the query for.
1400 * \param[out] irq_line The irq line associated with signal
1401 *
1402 * \retval IPC_SUCCESS Execution successful, irq_line contains a valid
1403 * value.
1404 * \retval IPC_ERROR_GENERIC There was an error finding the IRQ line for the
1405 * signal. irq_line is unchanged.
1406 */
1407static int32_t get_irq_line_for_signal(int32_t partition_id,
1408 psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +01001409 IRQn_Type *irq_line)
Mingyang Sund44522a2020-01-16 16:48:37 +08001410{
1411 size_t i;
1412
1413 for (i = 0; i < tfm_core_irq_signals_count; ++i) {
1414 if (tfm_core_irq_signals[i].partition_id == partition_id &&
1415 tfm_core_irq_signals[i].signal_value == signal) {
1416 *irq_line = tfm_core_irq_signals[i].irq_line;
1417 return IPC_SUCCESS;
1418 }
1419 }
1420 return IPC_ERROR_GENERIC;
1421}
1422
1423void tfm_spm_psa_eoi(uint32_t *args)
1424{
1425 psa_signal_t irq_signal;
TTornblomfaf74f52020-03-04 17:56:27 +01001426 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001427 int32_t ret;
1428 struct spm_partition_desc_t *partition = NULL;
1429
1430 TFM_CORE_ASSERT(args != NULL);
1431 irq_signal = (psa_signal_t)args[0];
1432
1433 /* It is a fatal error if passed signal indicates more than one signals. */
1434 if (!tfm_is_one_bit_set(irq_signal)) {
1435 tfm_core_panic();
1436 }
1437
1438 partition = tfm_spm_get_running_partition();
1439 if (!partition) {
1440 tfm_core_panic();
1441 }
1442
1443 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1444 irq_signal, &irq_line);
1445 /* It is a fatal error if passed signal is not an interrupt signal. */
1446 if (ret != IPC_SUCCESS) {
1447 tfm_core_panic();
1448 }
1449
1450 /* It is a fatal error if passed signal is not currently asserted */
1451 if ((partition->runtime_data.signals & irq_signal) == 0) {
1452 tfm_core_panic();
1453 }
1454
1455 partition->runtime_data.signals &= ~irq_signal;
1456
1457 tfm_spm_hal_clear_pending_irq(irq_line);
1458 tfm_spm_hal_enable_irq(irq_line);
1459}
1460
1461void tfm_spm_enable_irq(uint32_t *args)
1462{
1463 struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args;
1464 psa_signal_t irq_signal = svc_ctx->r0;
TTornblomfaf74f52020-03-04 17:56:27 +01001465 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001466 int32_t ret;
1467 struct spm_partition_desc_t *partition = NULL;
1468
1469 /* It is a fatal error if passed signal indicates more than one signals. */
1470 if (!tfm_is_one_bit_set(irq_signal)) {
1471 tfm_core_panic();
1472 }
1473
1474 partition = tfm_spm_get_running_partition();
1475 if (!partition) {
1476 tfm_core_panic();
1477 }
1478
1479 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1480 irq_signal, &irq_line);
1481 /* It is a fatal error if passed signal is not an interrupt signal. */
1482 if (ret != IPC_SUCCESS) {
1483 tfm_core_panic();
1484 }
1485
1486 tfm_spm_hal_enable_irq(irq_line);
1487}
1488
1489void tfm_spm_disable_irq(uint32_t *args)
1490{
1491 struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args;
1492 psa_signal_t irq_signal = svc_ctx->r0;
TTornblomfaf74f52020-03-04 17:56:27 +01001493 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001494 int32_t ret;
1495 struct spm_partition_desc_t *partition = NULL;
1496
1497 /* It is a fatal error if passed signal indicates more than one signals. */
1498 if (!tfm_is_one_bit_set(irq_signal)) {
1499 tfm_core_panic();
1500 }
1501
1502 partition = tfm_spm_get_running_partition();
1503 if (!partition) {
1504 tfm_core_panic();
1505 }
1506
1507 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1508 irq_signal, &irq_line);
1509 /* It is a fatal error if passed signal is not an interrupt signal. */
1510 if (ret != IPC_SUCCESS) {
1511 tfm_core_panic();
1512 }
1513
1514 tfm_spm_hal_disable_irq(irq_line);
1515}
1516
1517void tfm_spm_validate_caller(struct spm_partition_desc_t *p_cur_sp,
1518 uint32_t *p_ctx, uint32_t exc_return,
1519 bool ns_caller)
1520{
1521 uintptr_t stacked_ctx_pos;
1522
1523 if (ns_caller) {
1524 /*
1525 * The background IRQ can't be supported, since if SP is executing,
1526 * the preempted context of SP can be different with the one who
1527 * preempts veneer.
1528 */
1529 if (p_cur_sp->static_data->partition_id != TFM_SP_NON_SECURE_ID) {
1530 tfm_core_panic();
1531 }
1532
1533 /*
1534 * It is non-secure caller, check if veneer stack contains
1535 * multiple contexts.
1536 */
1537 stacked_ctx_pos = (uintptr_t)p_ctx +
1538 sizeof(struct tfm_state_context_t) +
1539 TFM_VENEER_STACK_GUARD_SIZE;
1540
1541 if (is_stack_alloc_fp_space(exc_return)) {
1542#if defined (__FPU_USED) && (__FPU_USED == 1U)
1543 if (FPU->FPCCR & FPU_FPCCR_TS_Msk) {
1544 stacked_ctx_pos += TFM_ADDTIONAL_FP_CONTEXT_WORDS *
1545 sizeof(uint32_t);
1546 }
1547#endif
1548 stacked_ctx_pos += TFM_BASIC_FP_CONTEXT_WORDS * sizeof(uint32_t);
1549 }
1550
1551 if (stacked_ctx_pos != p_cur_sp->runtime_data.sp_thrd.stk_top) {
1552 tfm_core_panic();
1553 }
1554 } else if (p_cur_sp->static_data->partition_id <= 0) {
1555 tfm_core_panic();
1556 }
1557}
Summer Qin830c5542020-02-14 13:44:20 +08001558
1559void tfm_spm_request_handler(const struct tfm_state_context_t *svc_ctx)
1560{
1561 uint32_t *res_ptr = (uint32_t *)&svc_ctx->r0;
1562 uint32_t running_partition_flags = 0;
1563 const struct spm_partition_desc_t *partition = NULL;
1564
1565 /* Check permissions on request type basis */
1566
1567 switch (svc_ctx->r0) {
1568 case TFM_SPM_REQUEST_RESET_VOTE:
1569 partition = tfm_spm_get_running_partition();
1570 if (!partition) {
1571 tfm_core_panic();
1572 }
1573 running_partition_flags = partition->static_data->partition_flags;
1574
1575 /* Currently only PSA Root of Trust services are allowed to make Reset
1576 * vote request
1577 */
1578 if ((running_partition_flags & SPM_PART_FLAG_PSA_ROT) == 0) {
1579 *res_ptr = (uint32_t)TFM_ERROR_GENERIC;
1580 }
1581
1582 /* FixMe: this is a placeholder for checks to be performed before
1583 * allowing execution of reset
1584 */
1585 *res_ptr = (uint32_t)TFM_SUCCESS;
1586
1587 break;
1588 default:
1589 *res_ptr = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
1590 }
1591}