blob: a5714541db63b230da762cfd1bb24e71b74c7485 [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
Summer Qin373feb12020-03-27 15:35:33 +080055/*********************** Connection handle conversion APIs *******************/
56
57/* Set a minimal value here for feature expansion. */
58#define CLIENT_HANDLE_VALUE_MIN 32
59
60#define CONVERSION_FACTOR_BITOFFSET 3
61#define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET)
62/* Set 32 as the maximum */
63#define CONVERSION_FACTOR_VALUE_MAX 0x20
64
65#if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX
66#error "CONVERSION FACTOR OUT OF RANGE"
67#endif
68
69static uint32_t loop_index;
70
71/*
72 * A handle instance psa_handle_t allocated inside SPM is actually a memory
73 * address among the handle pool. Return this handle to the client directly
74 * exposes information of secure memory address. In this case, converting the
75 * handle into another value does not represent the memory address to avoid
76 * exposing secure memory directly to clients.
77 *
78 * This function converts the handle instance into another value by scaling the
79 * handle in pool offset, the converted value is named as a user handle.
80 *
81 * The formula:
82 * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE +
83 * CLIENT_HANDLE_VALUE_MIN + loop_index
84 * where:
85 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
86 * exceed CONVERSION_FACTOR_VALUE_MAX.
87 *
88 * handle_instance in RANGE[POOL_START, POOL_END]
89 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
90 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
91 *
92 * note:
93 * loop_index is used to promise same handle instance is converted into
94 * different user handles in short time.
95 */
96static psa_handle_t tfm_spm_to_user_handle(
97 struct tfm_conn_handle_t *handle_instance)
98{
99 psa_handle_t user_handle;
100
101 loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE;
102 user_handle = (psa_handle_t)((((uintptr_t)handle_instance -
103 (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) +
104 CLIENT_HANDLE_VALUE_MIN + loop_index);
105
106 return user_handle;
107}
108
109/*
110 * This function converts a user handle into a corresponded handle instance.
111 * The converted value is validated before returning, an invalid handle instance
112 * is returned as NULL.
113 *
114 * The formula:
115 * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) /
116 * CONVERSION_FACTOR_VALUE) + POOL_START
117 * where:
118 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
119 * exceed CONVERSION_FACTOR_VALUE_MAX.
120 *
121 * handle_instance in RANGE[POOL_START, POOL_END]
122 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
123 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
124 */
125struct tfm_conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle)
126{
127 struct tfm_conn_handle_t *handle_instance;
128
129 if (user_handle == PSA_NULL_HANDLE) {
130 return NULL;
131 }
132
133 handle_instance = (struct tfm_conn_handle_t *)((((uintptr_t)user_handle -
134 CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) +
135 (uintptr_t)conn_handle_pool);
136
137 return handle_instance;
138}
139
Edison Ai764d41f2018-09-21 15:56:36 +0800140/* Service handle management functions */
Summer Qin630c76b2020-05-20 10:32:58 +0800141struct tfm_conn_handle_t *tfm_spm_create_conn_handle(
142 struct tfm_spm_service_t *service,
Summer Qin1ce712a2019-10-14 18:04:05 +0800143 int32_t client_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800144{
Edison Ai9cc26242019-08-06 11:28:04 +0800145 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800146
Ken Liuf250b8b2019-12-27 16:31:24 +0800147 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800148
149 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +0800150 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
151 if (!p_handle) {
Summer Qin630c76b2020-05-20 10:32:58 +0800152 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800153 }
154
Edison Ai9cc26242019-08-06 11:28:04 +0800155 p_handle->service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800156 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +0800157 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800158
159 /* Add handle node to list for next psa functions */
Edison Ai9cc26242019-08-06 11:28:04 +0800160 tfm_list_add_tail(&service->handle_list, &p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800161
Summer Qin630c76b2020-05-20 10:32:58 +0800162 return p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800163}
164
Summer Qin630c76b2020-05-20 10:32:58 +0800165int32_t tfm_spm_validate_conn_handle(
166 const struct tfm_conn_handle_t *conn_handle,
167 int32_t client_id)
Summer Qin1ce712a2019-10-14 18:04:05 +0800168{
169 /* Check the handle address is validated */
170 if (is_valid_chunk_data_in_pool(conn_handle_pool,
171 (uint8_t *)conn_handle) != true) {
172 return IPC_ERROR_GENERIC;
173 }
174
175 /* Check the handle caller is correct */
Summer Qin630c76b2020-05-20 10:32:58 +0800176 if (conn_handle->client_id != client_id) {
Summer Qin1ce712a2019-10-14 18:04:05 +0800177 return IPC_ERROR_GENERIC;
178 }
179
180 return IPC_SUCCESS;
181}
182
Mingyang Sund44522a2020-01-16 16:48:37 +0800183/**
184 * \brief Free connection handle which not used anymore.
185 *
186 * \param[in] service Target service context pointer
187 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800188 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800189 *
190 * \retval IPC_SUCCESS Success
191 * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
192 * \retval "Does not return" Panic for not find service by handle
193 */
194static int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800195 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800196{
Ken Liuf250b8b2019-12-27 16:31:24 +0800197 TFM_CORE_ASSERT(service);
Summer Qin630c76b2020-05-20 10:32:58 +0800198 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800199
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200200 /* Clear magic as the handler is not used anymore */
Summer Qin630c76b2020-05-20 10:32:58 +0800201 conn_handle->internal_msg.magic = 0;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200202
Edison Ai764d41f2018-09-21 15:56:36 +0800203 /* Remove node from handle list */
Summer Qin630c76b2020-05-20 10:32:58 +0800204 tfm_list_del_node(&conn_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800205
206 /* Back handle buffer to pool */
Summer Qin630c76b2020-05-20 10:32:58 +0800207 tfm_pool_free(conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800208 return IPC_SUCCESS;
209}
210
Mingyang Sund44522a2020-01-16 16:48:37 +0800211/**
212 * \brief Set reverse handle value for connection.
213 *
214 * \param[in] service Target service context pointer
215 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800216 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800217 * \param[in] rhandle rhandle need to save
218 *
219 * \retval IPC_SUCCESS Success
220 * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
221 * \retval "Does not return" Panic for not find handle node
222 */
223static int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800224 struct tfm_conn_handle_t *conn_handle,
Mingyang Sund44522a2020-01-16 16:48:37 +0800225 void *rhandle)
Edison Ai764d41f2018-09-21 15:56:36 +0800226{
Ken Liuf250b8b2019-12-27 16:31:24 +0800227 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800228 /* Set reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800229 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800230
Summer Qin630c76b2020-05-20 10:32:58 +0800231 conn_handle->rhandle = rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800232 return IPC_SUCCESS;
233}
234
Mingyang Sund44522a2020-01-16 16:48:37 +0800235/**
236 * \brief Get reverse handle value from connection hanlde.
237 *
238 * \param[in] service Target service context pointer
239 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800240 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800241 *
242 * \retval void * Success
243 * \retval "Does not return" Panic for those:
244 * service pointer are NULL
245 * hanlde is \ref PSA_NULL_HANDLE
246 * handle node does not be found
247 */
248static void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800249 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800250{
Ken Liuf250b8b2019-12-27 16:31:24 +0800251 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800252 /* Get reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800253 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800254
Summer Qin630c76b2020-05-20 10:32:58 +0800255 return conn_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800256}
257
258/* Partition management functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800259
260/**
261 * \brief Get the service context by signal.
262 *
263 * \param[in] partition Partition context pointer
264 * \ref spm_partition_desc_t structures
265 * \param[in] signal Signal associated with inputs to the Secure
266 * Partition, \ref psa_signal_t
267 *
268 * \retval NULL Failed
269 * \retval "Not NULL" Target service context pointer,
270 * \ref tfm_spm_service_t structures
271 */
272static struct tfm_spm_service_t *
Mingyang Sunf3d29892019-07-10 17:50:23 +0800273 tfm_spm_get_service_by_signal(struct spm_partition_desc_t *partition,
274 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800275{
276 struct tfm_list_node_t *node, *head;
277 struct tfm_spm_service_t *service;
278
Ken Liuf250b8b2019-12-27 16:31:24 +0800279 TFM_CORE_ASSERT(partition);
Edison Ai764d41f2018-09-21 15:56:36 +0800280
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800281 if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
Edison Ai9059ea02019-11-28 13:46:14 +0800282 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800283 }
284
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800285 head = &partition->runtime_data.service_list;
Edison Ai764d41f2018-09-21 15:56:36 +0800286 TFM_LIST_FOR_EACH(node, head) {
287 service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t, list);
Summer Qine578c5b2019-08-16 16:42:16 +0800288 if (service->service_db->signal == signal) {
Edison Ai764d41f2018-09-21 15:56:36 +0800289 return service;
290 }
291 }
292 return NULL;
293}
294
295struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid)
296{
297 uint32_t i;
298 struct tfm_list_node_t *node, *head;
299 struct tfm_spm_service_t *service;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800300 struct spm_partition_desc_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800301
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200302 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800303 partition = &g_spm_partition_db.partitions[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800304 /* Skip partition without IPC flag */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800305 if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
Edison Ai764d41f2018-09-21 15:56:36 +0800306 continue;
307 }
308
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800309 if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
Edison Ai764d41f2018-09-21 15:56:36 +0800310 continue;
311 }
312
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800313 head = &partition->runtime_data.service_list;
Edison Ai764d41f2018-09-21 15:56:36 +0800314 TFM_LIST_FOR_EACH(node, head) {
315 service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t,
316 list);
Summer Qine578c5b2019-08-16 16:42:16 +0800317 if (service->service_db->sid == sid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800318 return service;
319 }
320 }
321 }
322 return NULL;
323}
324
Mingyang Sund44522a2020-01-16 16:48:37 +0800325/**
326 * \brief Get the partition context by partition ID.
327 *
328 * \param[in] partition_id Partition identity
329 *
330 * \retval NULL Failed
331 * \retval "Not NULL" Target partition context pointer,
332 * \ref spm_partition_desc_t structures
333 */
334static struct spm_partition_desc_t *
335 tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800336{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800337 uint32_t idx = get_partition_idx(partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800338
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800339 if (idx != SPM_INVALID_PARTITION_IDX) {
340 return &(g_spm_partition_db.partitions[idx]);
Edison Ai764d41f2018-09-21 15:56:36 +0800341 }
342 return NULL;
343}
344
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800345struct spm_partition_desc_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800346{
347 uint32_t spid;
348
Mingyang Sunf3d29892019-07-10 17:50:23 +0800349 spid = tfm_spm_partition_get_running_partition_id();
Edison Ai764d41f2018-09-21 15:56:36 +0800350
351 return tfm_spm_get_partition_by_id(spid);
352}
353
354int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530355 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800356{
Ken Liuf250b8b2019-12-27 16:31:24 +0800357 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800358
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530359 switch (service->service_db->version_policy) {
Edison Ai764d41f2018-09-21 15:56:36 +0800360 case TFM_VERSION_POLICY_RELAXED:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530361 if (version > service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800362 return IPC_ERROR_VERSION;
363 }
364 break;
365 case TFM_VERSION_POLICY_STRICT:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530366 if (version != service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800367 return IPC_ERROR_VERSION;
368 }
369 break;
370 default:
371 return IPC_ERROR_VERSION;
372 }
373 return IPC_SUCCESS;
374}
375
Edison Aie728fbf2019-11-13 09:37:12 +0800376int32_t tfm_spm_check_authorization(uint32_t sid,
377 struct tfm_spm_service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800378 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800379{
380 struct spm_partition_desc_t *partition = NULL;
381 int32_t i;
382
Ken Liuf250b8b2019-12-27 16:31:24 +0800383 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800384
385 if (ns_caller) {
386 if (!service->service_db->non_secure_client) {
387 return IPC_ERROR_GENERIC;
388 }
389 } else {
390 partition = tfm_spm_get_running_partition();
391 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800392 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800393 }
394
395 for (i = 0; i < partition->static_data->dependencies_num; i++) {
396 if (partition->static_data->p_dependencies[i] == sid) {
397 break;
398 }
399 }
400
401 if (i == partition->static_data->dependencies_num) {
402 return IPC_ERROR_GENERIC;
403 }
404 }
405 return IPC_SUCCESS;
406}
407
Edison Ai764d41f2018-09-21 15:56:36 +0800408/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800409
410/**
411 * \brief Get message context by message handle.
412 *
413 * \param[in] msg_handle Message handle which is a reference generated
414 * by the SPM to a specific message.
415 *
416 * \return The message body context pointer
417 * \ref tfm_msg_body_t structures
418 */
419static struct tfm_msg_body_t *
420 tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800421{
422 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200423 * The message handler passed by the caller is considered invalid in the
424 * following cases:
425 * 1. Not a valid message handle. (The address of a message is not the
426 * address of a possible handle from the pool
427 * 2. Handle not belongs to the caller partition (The handle is either
428 * unused, or owned by anither partition)
429 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800430 */
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200431 struct tfm_conn_handle_t *connection_handle_address;
Edison Ai764d41f2018-09-21 15:56:36 +0800432 struct tfm_msg_body_t *msg;
433 uint32_t partition_id;
434
435 msg = (struct tfm_msg_body_t *)msg_handle;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200436
437 connection_handle_address =
438 TFM_GET_CONTAINER_PTR(msg, struct tfm_conn_handle_t, internal_msg);
439
440 if (is_valid_chunk_data_in_pool(
441 conn_handle_pool, (uint8_t *)connection_handle_address) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800442 return NULL;
443 }
444
445 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200446 * Check that the magic number is correct. This proves that the message
447 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800448 */
449 if (msg->magic != TFM_MSG_MAGIC) {
450 return NULL;
451 }
452
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200453 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800454 partition_id = tfm_spm_partition_get_running_partition_id();
Summer Qin423dbef2019-08-22 15:59:35 +0800455 if (partition_id != msg->service->partition->static_data->partition_id) {
Edison Ai764d41f2018-09-21 15:56:36 +0800456 return NULL;
457 }
458
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200459 /*
460 * FixMe: For condition 1 it should be checked whether the message belongs
461 * to the service. Skipping this check isn't a security risk as even if the
462 * message belongs to another service, the handle belongs to the calling
463 * partition.
464 */
465
Edison Ai764d41f2018-09-21 15:56:36 +0800466 return msg;
467}
468
Edison Ai97115822019-08-01 14:22:19 +0800469struct tfm_msg_body_t *
Summer Qin630c76b2020-05-20 10:32:58 +0800470 tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800471{
Summer Qin630c76b2020-05-20 10:32:58 +0800472 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai97115822019-08-01 14:22:19 +0800473
Summer Qin630c76b2020-05-20 10:32:58 +0800474 return &(conn_handle->internal_msg);
Edison Ai97115822019-08-01 14:22:19 +0800475}
476
477void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
478 struct tfm_spm_service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800479 struct tfm_conn_handle_t *handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800480 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800481 psa_invec *invec, size_t in_len,
482 psa_outvec *outvec, size_t out_len,
483 psa_outvec *caller_outvec)
484{
Edison Ai764d41f2018-09-21 15:56:36 +0800485 uint32_t i;
486
Ken Liuf250b8b2019-12-27 16:31:24 +0800487 TFM_CORE_ASSERT(msg);
488 TFM_CORE_ASSERT(service);
489 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
490 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
491 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
492 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
493 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800494
Edison Ai764d41f2018-09-21 15:56:36 +0800495 /* Clear message buffer before using it */
Mingyang Sun94b1b412019-09-20 15:11:14 +0800496 tfm_core_util_memset(msg, 0, sizeof(struct tfm_msg_body_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800497
Ken Liu35f89392019-03-14 14:51:05 +0800498 tfm_event_init(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800499 msg->magic = TFM_MSG_MAGIC;
500 msg->service = service;
501 msg->handle = handle;
502 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800503 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800504
505 /* Copy contents */
506 msg->msg.type = type;
507
508 for (i = 0; i < in_len; i++) {
509 msg->msg.in_size[i] = invec[i].len;
510 msg->invec[i].base = invec[i].base;
511 }
512
513 for (i = 0; i < out_len; i++) {
514 msg->msg.out_size[i] = outvec[i].len;
515 msg->outvec[i].base = outvec[i].base;
516 /* Out len is used to record the writed number, set 0 here again */
517 msg->outvec[i].len = 0;
518 }
519
520 /* Use message address as handle */
521 msg->msg.handle = (psa_handle_t)msg;
522
523 /* For connected handle, set rhandle to every message */
Summer Qin630c76b2020-05-20 10:32:58 +0800524 if (handle) {
Edison Ai764d41f2018-09-21 15:56:36 +0800525 msg->msg.rhandle = tfm_spm_get_rhandle(service, handle);
526 }
David Hu46603dd2019-12-11 18:05:16 +0800527
528 /* Set the private data of NSPE client caller in multi-core topology */
529 if (TFM_CLIENT_ID_IS_NS(client_id)) {
530 tfm_rpc_set_caller_data(msg, client_id);
531 }
Edison Ai764d41f2018-09-21 15:56:36 +0800532}
533
534int32_t tfm_spm_send_event(struct tfm_spm_service_t *service,
535 struct tfm_msg_body_t *msg)
536{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800537 struct spm_partition_runtime_data_t *p_runtime_data =
538 &service->partition->runtime_data;
539
Ken Liuf250b8b2019-12-27 16:31:24 +0800540 TFM_CORE_ASSERT(service);
541 TFM_CORE_ASSERT(msg);
Edison Ai764d41f2018-09-21 15:56:36 +0800542
543 /* Enqueue message to service message queue */
544 if (tfm_msg_enqueue(&service->msg_queue, msg) != IPC_SUCCESS) {
545 return IPC_ERROR_GENERIC;
546 }
547
548 /* Messages put. Update signals */
Summer Qine578c5b2019-08-16 16:42:16 +0800549 p_runtime_data->signals |= service->service_db->signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800550
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800551 tfm_event_wake(&p_runtime_data->signal_evnt, (p_runtime_data->signals &
552 p_runtime_data->signal_mask));
Edison Ai764d41f2018-09-21 15:56:36 +0800553
David Hufb38d562019-09-23 15:58:34 +0800554 /*
555 * If it is a NS request via RPC, it is unnecessary to block current
556 * thread.
557 */
558 if (!is_tfm_rpc_msg(msg)) {
559 tfm_event_wait(&msg->ack_evnt);
560 }
Edison Ai764d41f2018-09-21 15:56:36 +0800561
562 return IPC_SUCCESS;
563}
564
Mingyang Sund44522a2020-01-16 16:48:37 +0800565/**
566 * \brief Get bottom of stack region for a partition
567 *
568 * \param[in] partition_idx Partition index
569 *
570 * \return Stack region bottom value
571 *
572 * \note This function doesn't check if partition_idx is valid.
573 */
574static uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
Edison Ai7aff9e82019-07-11 14:56:46 +0800575{
576 return g_spm_partition_db.partitions[partition_idx].
Summer Qin423dbef2019-08-22 15:59:35 +0800577 memory_data->stack_bottom;
Edison Ai7aff9e82019-07-11 14:56:46 +0800578}
579
Mingyang Sund44522a2020-01-16 16:48:37 +0800580/**
581 * \brief Get top of stack region for a partition
582 *
583 * \param[in] partition_idx Partition index
584 *
585 * \return Stack region top value
586 *
587 * \note This function doesn't check if partition_idx is valid.
588 */
589static uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
Edison Ai7aff9e82019-07-11 14:56:46 +0800590{
Summer Qin423dbef2019-08-22 15:59:35 +0800591 return g_spm_partition_db.partitions[partition_idx].memory_data->stack_top;
Edison Ai7aff9e82019-07-11 14:56:46 +0800592}
593
Mingyang Sunf3d29892019-07-10 17:50:23 +0800594uint32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800595{
Summer Qin66f1e032020-01-06 15:40:03 +0800596 struct tfm_core_thread_t *pth = tfm_core_thrd_get_curr_thread();
Edison Ai764d41f2018-09-21 15:56:36 +0800597 struct spm_partition_desc_t *partition;
Summer Qinb5da9cc2019-08-26 15:19:45 +0800598 struct spm_partition_runtime_data_t *r_data;
Edison Ai764d41f2018-09-21 15:56:36 +0800599
Summer Qinb5da9cc2019-08-26 15:19:45 +0800600 r_data = TFM_GET_CONTAINER_PTR(pth, struct spm_partition_runtime_data_t,
601 sp_thrd);
602 partition = TFM_GET_CONTAINER_PTR(r_data, struct spm_partition_desc_t,
603 runtime_data);
Summer Qin423dbef2019-08-22 15:59:35 +0800604 return partition->static_data->partition_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800605}
606
Summer Qin66f1e032020-01-06 15:40:03 +0800607static struct tfm_core_thread_t *
Mingyang Sunf3d29892019-07-10 17:50:23 +0800608 tfm_spm_partition_get_thread_info(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800609{
Summer Qinb5da9cc2019-08-26 15:19:45 +0800610 return &g_spm_partition_db.partitions[partition_idx].runtime_data.sp_thrd;
Edison Ai764d41f2018-09-21 15:56:36 +0800611}
612
Summer Qin66f1e032020-01-06 15:40:03 +0800613static tfm_core_thrd_entry_t
Mingyang Sunf3d29892019-07-10 17:50:23 +0800614 tfm_spm_partition_get_init_func(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800615{
Summer Qin66f1e032020-01-06 15:40:03 +0800616 return (tfm_core_thrd_entry_t)(g_spm_partition_db.partitions[partition_idx].
Summer Qin423dbef2019-08-22 15:59:35 +0800617 static_data->partition_init);
Edison Ai764d41f2018-09-21 15:56:36 +0800618}
619
Mingyang Sunf3d29892019-07-10 17:50:23 +0800620static uint32_t tfm_spm_partition_get_priority(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800621{
Summer Qin423dbef2019-08-22 15:59:35 +0800622 return g_spm_partition_db.partitions[partition_idx].static_data->
Edison Ai764d41f2018-09-21 15:56:36 +0800623 partition_priority;
624}
625
Summer Qin43c185d2019-10-10 15:44:42 +0800626int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800627 enum tfm_memory_access_e access,
628 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800629{
Hugues de Valon99578562019-06-18 16:08:51 +0100630 enum tfm_status_e err;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800631
632 /* If len is zero, this indicates an empty buffer and base is ignored */
633 if (len == 0) {
634 return IPC_SUCCESS;
635 }
636
637 if (!buffer) {
638 return IPC_ERROR_BAD_PARAMETERS;
639 }
640
641 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
642 return IPC_ERROR_MEMORY_CHECK;
643 }
644
Summer Qin424d4db2019-03-25 14:09:51 +0800645 if (access == TFM_MEMORY_ACCESS_RW) {
Summer Qineb537e52019-03-29 09:57:10 +0800646 err = tfm_core_has_write_access_to_region(buffer, len, ns_caller,
647 privileged);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800648 } else {
Summer Qineb537e52019-03-29 09:57:10 +0800649 err = tfm_core_has_read_access_to_region(buffer, len, ns_caller,
650 privileged);
Summer Qin424d4db2019-03-25 14:09:51 +0800651 }
Summer Qin0fc3f592019-04-11 16:00:10 +0800652 if (err == TFM_SUCCESS) {
Summer Qin424d4db2019-03-25 14:09:51 +0800653 return IPC_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800654 }
655
656 return IPC_ERROR_MEMORY_CHECK;
657}
658
Ken Liuce2692d2020-02-11 12:39:36 +0800659uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800660{
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800661 uint32_t i, j, num;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800662 struct spm_partition_desc_t *partition;
Summer Qin66f1e032020-01-06 15:40:03 +0800663 struct tfm_core_thread_t *pth, *p_ns_entry_thread = NULL;
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100664 const struct tfm_spm_partition_platform_data_t **platform_data_p;
Edison Ai764d41f2018-09-21 15:56:36 +0800665
666 tfm_pool_init(conn_handle_pool,
667 POOL_BUFFER_SIZE(conn_handle_pool),
668 sizeof(struct tfm_conn_handle_t),
669 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800670
671 /* Init partition first for it will be used when init service */
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200672 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800673 partition = &g_spm_partition_db.partitions[i];
Edison Aif0501702019-10-11 14:36:42 +0800674
675 /* Check if the PSA framework version matches. */
676 if (partition->static_data->psa_framework_version !=
677 PSA_FRAMEWORK_VERSION) {
678 ERROR_MSG("Warning: PSA Framework Verison is not matched!");
679 continue;
680 }
681
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100682 platform_data_p = partition->platform_data_list;
683 if (platform_data_p != NULL) {
684 while ((*platform_data_p) != NULL) {
Edison Ai6be3df12020-02-14 22:14:33 +0800685 if (tfm_spm_hal_configure_default_isolation(i,
686 *platform_data_p) != TFM_PLAT_ERR_SUCCESS) {
687 tfm_core_panic();
688 }
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100689 ++platform_data_p;
690 }
691 }
692
Edison Ai764d41f2018-09-21 15:56:36 +0800693 if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
694 continue;
695 }
Ken Liu35f89392019-03-14 14:51:05 +0800696
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800697 /* Add PSA_DOORBELL signal to assigned_signals */
698 partition->runtime_data.assigned_signals |= PSA_DOORBELL;
699
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800700 /* TODO: This can be optimized by generating the assigned signal
701 * in code generation time.
702 */
703 for (j = 0; j < tfm_core_irq_signals_count; ++j) {
704 if (tfm_core_irq_signals[j].partition_id ==
705 partition->static_data->partition_id) {
706 partition->runtime_data.assigned_signals |=
707 tfm_core_irq_signals[j].signal_value;
708 }
709 }
710
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800711 tfm_event_init(&partition->runtime_data.signal_evnt);
712 tfm_list_init(&partition->runtime_data.service_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800713
Mingyang Sunf3d29892019-07-10 17:50:23 +0800714 pth = tfm_spm_partition_get_thread_info(i);
Edison Ai764d41f2018-09-21 15:56:36 +0800715 if (!pth) {
Edison Ai9059ea02019-11-28 13:46:14 +0800716 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800717 }
718
Summer Qin66f1e032020-01-06 15:40:03 +0800719 tfm_core_thrd_init(pth,
720 tfm_spm_partition_get_init_func(i),
721 NULL,
722 (uintptr_t)tfm_spm_partition_get_stack_top(i),
723 (uintptr_t)tfm_spm_partition_get_stack_bottom(i));
Edison Ai788bae22019-02-18 17:38:59 +0800724
Mingyang Sunf3d29892019-07-10 17:50:23 +0800725 pth->prior = tfm_spm_partition_get_priority(i);
Edison Ai764d41f2018-09-21 15:56:36 +0800726
Ken Liu490281d2019-12-30 15:55:26 +0800727 if (partition->static_data->partition_id == TFM_SP_NON_SECURE_ID) {
728 p_ns_entry_thread = pth;
Ken Liu5248af22019-12-29 12:47:13 +0800729 pth->param = (void *)tfm_spm_hal_get_ns_entry_point();
Ken Liu490281d2019-12-30 15:55:26 +0800730 }
731
Edison Ai764d41f2018-09-21 15:56:36 +0800732 /* Kick off */
Summer Qin66f1e032020-01-06 15:40:03 +0800733 if (tfm_core_thrd_start(pth) != THRD_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800734 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800735 }
736 }
737
738 /* Init Service */
Summer Qind99509f2019-08-02 17:36:58 +0800739 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
Edison Ai764d41f2018-09-21 15:56:36 +0800740 for (i = 0; i < num; i++) {
Summer Qine578c5b2019-08-16 16:42:16 +0800741 service[i].service_db = &service_db[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800742 partition =
Summer Qine578c5b2019-08-16 16:42:16 +0800743 tfm_spm_get_partition_by_id(service[i].service_db->partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800744 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800745 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800746 }
Summer Qind99509f2019-08-02 17:36:58 +0800747 service[i].partition = partition;
Jaykumar Pitambarbhai Patel0c7a0382020-01-09 15:25:58 +0530748 partition->runtime_data.assigned_signals |= service[i].service_db->signal;
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800749
Summer Qind99509f2019-08-02 17:36:58 +0800750 tfm_list_init(&service[i].handle_list);
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800751 tfm_list_add_tail(&partition->runtime_data.service_list,
Summer Qind99509f2019-08-02 17:36:58 +0800752 &service[i].list);
Edison Ai764d41f2018-09-21 15:56:36 +0800753 }
754
Ken Liu483f5da2019-04-24 10:45:21 +0800755 /*
756 * All threads initialized, start the scheduler.
757 *
758 * NOTE:
Ken Liu490281d2019-12-30 15:55:26 +0800759 * It is worthy to give the thread object to scheduler if the background
760 * context belongs to one of the threads. Here the background thread is the
761 * initialization thread who calls SPM SVC, which re-uses the non-secure
762 * entry thread's stack. After SPM initialization is done, this stack is
763 * cleaned up and the background context is never going to return. Tell
764 * the scheduler that the current thread is non-secure entry thread.
Ken Liu483f5da2019-04-24 10:45:21 +0800765 */
Summer Qin66f1e032020-01-06 15:40:03 +0800766 tfm_core_thrd_start_scheduler(p_ns_entry_thread);
Ken Liuce2692d2020-02-11 12:39:36 +0800767
Summer Qind2ad7e72020-01-06 18:16:35 +0800768 return p_ns_entry_thread->arch_ctx.lr;
Edison Ai764d41f2018-09-21 15:56:36 +0800769}
Ken Liu2d175172019-03-21 17:08:41 +0800770
Summer Qind2ad7e72020-01-06 18:16:35 +0800771void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx)
Ken Liu2d175172019-03-21 17:08:41 +0800772{
773#if TFM_LVL == 2
774 struct spm_partition_desc_t *p_next_partition;
Summer Qinb5da9cc2019-08-26 15:19:45 +0800775 struct spm_partition_runtime_data_t *r_data;
Ken Liu2d175172019-03-21 17:08:41 +0800776 uint32_t is_privileged;
777#endif
Summer Qin66f1e032020-01-06 15:40:03 +0800778 struct tfm_core_thread_t *pth_next = tfm_core_thrd_get_next_thread();
779 struct tfm_core_thread_t *pth_curr = tfm_core_thrd_get_curr_thread();
Ken Liu2d175172019-03-21 17:08:41 +0800780
Mate Toth-Pal32b2ccd2019-04-26 10:00:16 +0200781 if (pth_next != NULL && pth_curr != pth_next) {
Ken Liu2d175172019-03-21 17:08:41 +0800782#if TFM_LVL == 2
Summer Qinb5da9cc2019-08-26 15:19:45 +0800783 r_data = TFM_GET_CONTAINER_PTR(pth_next,
784 struct spm_partition_runtime_data_t,
785 sp_thrd);
786 p_next_partition = TFM_GET_CONTAINER_PTR(r_data,
Ken Liu2d175172019-03-21 17:08:41 +0800787 struct spm_partition_desc_t,
Summer Qinb5da9cc2019-08-26 15:19:45 +0800788 runtime_data);
Ken Liu2d175172019-03-21 17:08:41 +0800789
Summer Qin423dbef2019-08-22 15:59:35 +0800790 if (p_next_partition->static_data->partition_flags &
Ken Liu2d175172019-03-21 17:08:41 +0800791 SPM_PART_FLAG_PSA_ROT) {
792 is_privileged = TFM_PARTITION_PRIVILEGED_MODE;
793 } else {
794 is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE;
795 }
796
797 tfm_spm_partition_change_privilege(is_privileged);
798#endif
Mate Toth-Palc430b992019-05-09 21:01:14 +0200799
Summer Qind2ad7e72020-01-06 18:16:35 +0800800 tfm_core_thrd_switch_context(p_actx, pth_curr, pth_next);
Ken Liu2d175172019-03-21 17:08:41 +0800801 }
David Hufb38d562019-09-23 15:58:34 +0800802
803 /*
804 * Handle pending mailbox message from NS in multi-core topology.
805 * Empty operation on single Armv8-M platform.
806 */
807 tfm_rpc_client_call_handler();
Ken Liu2d175172019-03-21 17:08:41 +0800808}
Mingyang Sund44522a2020-01-16 16:48:37 +0800809
810/*********************** SPM functions for PSA Client APIs *******************/
811
812uint32_t tfm_spm_psa_framework_version(void)
813{
814 return tfm_spm_client_psa_framework_version();
815}
816
817uint32_t tfm_spm_psa_version(uint32_t *args, bool ns_caller)
818{
819 uint32_t sid;
820
821 TFM_CORE_ASSERT(args != NULL);
822 sid = (uint32_t)args[0];
823
824 return tfm_spm_client_psa_version(sid, ns_caller);
825}
826
827psa_status_t tfm_spm_psa_connect(uint32_t *args, bool ns_caller)
828{
829 uint32_t sid;
830 uint32_t version;
831
832 TFM_CORE_ASSERT(args != NULL);
833 sid = (uint32_t)args[0];
834 version = (uint32_t)args[1];
835
836 return tfm_spm_client_psa_connect(sid, version, ns_caller);
837}
838
839psa_status_t tfm_spm_psa_call(uint32_t *args, bool ns_caller, uint32_t lr)
840{
841 psa_handle_t handle;
842 psa_invec *inptr;
843 psa_outvec *outptr;
844 size_t in_num, out_num;
845 struct spm_partition_desc_t *partition = NULL;
846 uint32_t privileged;
847 int32_t type;
848 struct tfm_control_parameter_t ctrl_param;
849
850 TFM_CORE_ASSERT(args != NULL);
851 handle = (psa_handle_t)args[0];
852
853 partition = tfm_spm_get_running_partition();
854 if (!partition) {
855 tfm_core_panic();
856 }
857 privileged = tfm_spm_partition_get_privileged_mode(
858 partition->static_data->partition_flags);
859
860 /*
861 * Read parameters from the arguments. It is a fatal error if the
862 * memory reference for buffer is invalid or not readable.
863 */
864 if (tfm_memory_check((const void *)args[1],
865 sizeof(struct tfm_control_parameter_t), ns_caller,
866 TFM_MEMORY_ACCESS_RW, privileged) != IPC_SUCCESS) {
867 tfm_core_panic();
868 }
869
870 tfm_core_util_memcpy(&ctrl_param,
871 (const void *)args[1],
872 sizeof(ctrl_param));
873
874 type = ctrl_param.type;
875 in_num = ctrl_param.in_len;
876 out_num = ctrl_param.out_len;
877 inptr = (psa_invec *)args[2];
878 outptr = (psa_outvec *)args[3];
879
880 /* The request type must be zero or positive. */
881 if (type < 0) {
882 tfm_core_panic();
883 }
884
885 return tfm_spm_client_psa_call(handle, type, inptr, in_num, outptr, out_num,
886 ns_caller, privileged);
887}
888
889void tfm_spm_psa_close(uint32_t *args, bool ns_caller)
890{
891 psa_handle_t handle;
892
893 TFM_CORE_ASSERT(args != NULL);
894 handle = args[0];
895
896 tfm_spm_client_psa_close(handle, ns_caller);
897}
898
899uint32_t tfm_spm_get_lifecycle_state(void)
900{
901 /*
902 * FixMe: return PSA_LIFECYCLE_UNKNOWN to the caller directly. It will be
903 * implemented in the future.
904 */
905 return PSA_LIFECYCLE_UNKNOWN;
906}
907
908/********************* SPM functions for PSA Service APIs ********************/
909
910psa_signal_t tfm_spm_psa_wait(uint32_t *args)
911{
912 psa_signal_t signal_mask;
913 uint32_t timeout;
914 struct spm_partition_desc_t *partition = NULL;
915
916 TFM_CORE_ASSERT(args != NULL);
917 signal_mask = (psa_signal_t)args[0];
918 timeout = args[1];
919
920 /*
921 * Timeout[30:0] are reserved for future use.
922 * SPM must ignore the value of RES.
923 */
924 timeout &= PSA_TIMEOUT_MASK;
925
926 partition = tfm_spm_get_running_partition();
927 if (!partition) {
928 tfm_core_panic();
929 }
930
931 /*
932 * It is a PROGRAMMER ERROR if the signal_mask does not include any assigned
933 * signals.
934 */
935 if ((partition->runtime_data.assigned_signals & signal_mask) == 0) {
936 tfm_core_panic();
937 }
938
939 /*
940 * Expected signals are included in signal wait mask, ignored signals
941 * should not be set and affect caller thread state. Save this mask for
942 * further checking while signals are ready to be set.
943 */
944 partition->runtime_data.signal_mask = signal_mask;
945
946 /*
947 * tfm_event_wait() blocks the caller thread if no signals are available.
948 * In this case, the return value of this function is temporary set into
949 * runtime context. After new signal(s) are available, the return value
950 * is updated with the available signal(s) and blocked thread gets to run.
951 */
952 if (timeout == PSA_BLOCK &&
953 (partition->runtime_data.signals & signal_mask) == 0) {
954 tfm_event_wait(&partition->runtime_data.signal_evnt);
955 }
956
957 return partition->runtime_data.signals & signal_mask;
958}
959
960psa_status_t tfm_spm_psa_get(uint32_t *args)
961{
962 psa_signal_t signal;
963 psa_msg_t *msg = NULL;
964 struct tfm_spm_service_t *service = NULL;
965 struct tfm_msg_body_t *tmp_msg = NULL;
966 struct spm_partition_desc_t *partition = NULL;
967 uint32_t privileged;
968
969 TFM_CORE_ASSERT(args != NULL);
970 signal = (psa_signal_t)args[0];
971 msg = (psa_msg_t *)args[1];
972
973 /*
974 * Only one message could be retrieved every time for psa_get(). It is a
975 * fatal error if the input signal has more than a signal bit set.
976 */
Ken Liu410ada52020-01-08 11:37:27 +0800977 if (!tfm_is_one_bit_set(signal)) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800978 tfm_core_panic();
979 }
980
981 partition = tfm_spm_get_running_partition();
982 if (!partition) {
983 tfm_core_panic();
984 }
985 privileged = tfm_spm_partition_get_privileged_mode(
986 partition->static_data->partition_flags);
987
988 /*
989 * Write the message to the service buffer. It is a fatal error if the
990 * input msg pointer is not a valid memory reference or not read-write.
991 */
992 if (tfm_memory_check(msg, sizeof(psa_msg_t), false, TFM_MEMORY_ACCESS_RW,
993 privileged) != IPC_SUCCESS) {
994 tfm_core_panic();
995 }
996
997 /*
998 * It is a fatal error if the caller call psa_get() when no message has
999 * been set. The caller must call this function after an RoT Service signal
1000 * is returned by psa_wait().
1001 */
1002 if (partition->runtime_data.signals == 0) {
1003 tfm_core_panic();
1004 }
1005
1006 /*
1007 * It is a fatal error if the RoT Service signal is not currently asserted.
1008 */
1009 if ((partition->runtime_data.signals & signal) == 0) {
1010 tfm_core_panic();
1011 }
1012
1013 /*
1014 * Get RoT service by signal from partition. It is a fatal error if getting
1015 * failed, which means the input signal is not correspond to an RoT service.
1016 */
1017 service = tfm_spm_get_service_by_signal(partition, signal);
1018 if (!service) {
1019 tfm_core_panic();
1020 }
1021
1022 tmp_msg = tfm_msg_dequeue(&service->msg_queue);
1023 if (!tmp_msg) {
1024 return PSA_ERROR_DOES_NOT_EXIST;
1025 }
1026
1027 ((struct tfm_conn_handle_t *)(tmp_msg->handle))->status =
1028 TFM_HANDLE_STATUS_ACTIVE;
1029
1030 tfm_core_util_memcpy(msg, &tmp_msg->msg, sizeof(psa_msg_t));
1031
1032 /*
1033 * There may be multiple messages for this RoT Service signal, do not clear
1034 * its mask until no remaining message.
1035 */
1036 if (tfm_msg_queue_is_empty(&service->msg_queue)) {
1037 partition->runtime_data.signals &= ~signal;
1038 }
1039
1040 return PSA_SUCCESS;
1041}
1042
1043void tfm_spm_psa_set_rhandle(uint32_t *args)
1044{
1045 psa_handle_t msg_handle;
1046 void *rhandle = NULL;
1047 struct tfm_msg_body_t *msg = NULL;
1048
1049 TFM_CORE_ASSERT(args != NULL);
1050 msg_handle = (psa_handle_t)args[0];
1051 rhandle = (void *)args[1];
1052
1053 /* It is a fatal error if message handle is invalid */
1054 msg = tfm_spm_get_msg_from_handle(msg_handle);
1055 if (!msg) {
1056 tfm_core_panic();
1057 }
1058
1059 msg->msg.rhandle = rhandle;
1060
1061 /* Store reverse handle for following client calls. */
1062 tfm_spm_set_rhandle(msg->service, msg->handle, rhandle);
1063}
1064
1065size_t tfm_spm_psa_read(uint32_t *args)
1066{
1067 psa_handle_t msg_handle;
1068 uint32_t invec_idx;
1069 void *buffer = NULL;
1070 size_t num_bytes;
1071 size_t bytes;
1072 struct tfm_msg_body_t *msg = NULL;
1073 uint32_t privileged;
1074 struct spm_partition_desc_t *partition = NULL;
1075
1076 TFM_CORE_ASSERT(args != NULL);
1077 msg_handle = (psa_handle_t)args[0];
1078 invec_idx = args[1];
1079 buffer = (void *)args[2];
1080 num_bytes = (size_t)args[3];
1081
1082 /* It is a fatal error if message handle is invalid */
1083 msg = tfm_spm_get_msg_from_handle(msg_handle);
1084 if (!msg) {
1085 tfm_core_panic();
1086 }
1087
1088 partition = msg->service->partition;
1089 privileged = tfm_spm_partition_get_privileged_mode(
1090 partition->static_data->partition_flags);
1091
1092 /*
1093 * It is a fatal error if message handle does not refer to a request
1094 * message
1095 */
1096 if (msg->msg.type < PSA_IPC_CALL) {
1097 tfm_core_panic();
1098 }
1099
1100 /*
1101 * It is a fatal error if invec_idx is equal to or greater than
1102 * PSA_MAX_IOVEC
1103 */
1104 if (invec_idx >= PSA_MAX_IOVEC) {
1105 tfm_core_panic();
1106 }
1107
1108 /* There was no remaining data in this input vector */
1109 if (msg->msg.in_size[invec_idx] == 0) {
1110 return 0;
1111 }
1112
1113 /*
1114 * Copy the client data to the service buffer. It is a fatal error
1115 * if the memory reference for buffer is invalid or not read-write.
1116 */
1117 if (tfm_memory_check(buffer, num_bytes, false,
1118 TFM_MEMORY_ACCESS_RW, privileged) != IPC_SUCCESS) {
1119 tfm_core_panic();
1120 }
1121
1122 bytes = num_bytes > msg->msg.in_size[invec_idx] ?
1123 msg->msg.in_size[invec_idx] : num_bytes;
1124
1125 tfm_core_util_memcpy(buffer, msg->invec[invec_idx].base, bytes);
1126
1127 /* There maybe some remaining data */
1128 msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base + bytes;
1129 msg->msg.in_size[invec_idx] -= bytes;
1130
1131 return bytes;
1132}
1133
1134size_t tfm_spm_psa_skip(uint32_t *args)
1135{
1136 psa_handle_t msg_handle;
1137 uint32_t invec_idx;
1138 size_t num_bytes;
1139 struct tfm_msg_body_t *msg = NULL;
1140
1141 TFM_CORE_ASSERT(args != NULL);
1142 msg_handle = (psa_handle_t)args[0];
1143 invec_idx = args[1];
1144 num_bytes = (size_t)args[2];
1145
1146 /* It is a fatal error if message handle is invalid */
1147 msg = tfm_spm_get_msg_from_handle(msg_handle);
1148 if (!msg) {
1149 tfm_core_panic();
1150 }
1151
1152 /*
1153 * It is a fatal error if message handle does not refer to a request
1154 * message
1155 */
1156 if (msg->msg.type < PSA_IPC_CALL) {
1157 tfm_core_panic();
1158 }
1159
1160 /*
1161 * It is a fatal error if invec_idx is equal to or greater than
1162 * PSA_MAX_IOVEC
1163 */
1164 if (invec_idx >= PSA_MAX_IOVEC) {
1165 tfm_core_panic();
1166 }
1167
1168 /* There was no remaining data in this input vector */
1169 if (msg->msg.in_size[invec_idx] == 0) {
1170 return 0;
1171 }
1172
1173 /*
1174 * If num_bytes is greater than the remaining size of the input vector then
1175 * the remaining size of the input vector is used.
1176 */
1177 if (num_bytes > msg->msg.in_size[invec_idx]) {
1178 num_bytes = msg->msg.in_size[invec_idx];
1179 }
1180
1181 /* There maybe some remaining data */
1182 msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base +
1183 num_bytes;
1184 msg->msg.in_size[invec_idx] -= num_bytes;
1185
1186 return num_bytes;
1187}
1188
1189void tfm_spm_psa_write(uint32_t *args)
1190{
1191 psa_handle_t msg_handle;
1192 uint32_t outvec_idx;
1193 void *buffer = NULL;
1194 size_t num_bytes;
1195 struct tfm_msg_body_t *msg = NULL;
1196 uint32_t privileged;
1197 struct spm_partition_desc_t *partition = NULL;
1198
1199 TFM_CORE_ASSERT(args != NULL);
1200 msg_handle = (psa_handle_t)args[0];
1201 outvec_idx = args[1];
1202 buffer = (void *)args[2];
1203 num_bytes = (size_t)args[3];
1204
1205 /* It is a fatal error if message handle is invalid */
1206 msg = tfm_spm_get_msg_from_handle(msg_handle);
1207 if (!msg) {
1208 tfm_core_panic();
1209 }
1210
1211 partition = msg->service->partition;
1212 privileged = tfm_spm_partition_get_privileged_mode(
1213 partition->static_data->partition_flags);
1214
1215 /*
1216 * It is a fatal error if message handle does not refer to a request
1217 * message
1218 */
1219 if (msg->msg.type < PSA_IPC_CALL) {
1220 tfm_core_panic();
1221 }
1222
1223 /*
1224 * It is a fatal error if outvec_idx is equal to or greater than
1225 * PSA_MAX_IOVEC
1226 */
1227 if (outvec_idx >= PSA_MAX_IOVEC) {
1228 tfm_core_panic();
1229 }
1230
1231 /*
1232 * It is a fatal error if the call attempts to write data past the end of
1233 * the client output vector
1234 */
1235 if (num_bytes > msg->msg.out_size[outvec_idx] -
1236 msg->outvec[outvec_idx].len) {
1237 tfm_core_panic();
1238 }
1239
1240 /*
1241 * Copy the service buffer to client outvecs. It is a fatal error
1242 * if the memory reference for buffer is invalid or not readable.
1243 */
1244 if (tfm_memory_check(buffer, num_bytes, false,
1245 TFM_MEMORY_ACCESS_RO, privileged) != IPC_SUCCESS) {
1246 tfm_core_panic();
1247 }
1248
1249 tfm_core_util_memcpy((char *)msg->outvec[outvec_idx].base +
1250 msg->outvec[outvec_idx].len, buffer, num_bytes);
1251
1252 /* Update the write number */
1253 msg->outvec[outvec_idx].len += num_bytes;
1254}
1255
1256static void update_caller_outvec_len(struct tfm_msg_body_t *msg)
1257{
1258 uint32_t i;
1259
1260 /*
1261 * FixeMe: abstract these part into dedicated functions to avoid
1262 * accessing thread context in psa layer
1263 */
1264 /* If it is a NS request via RPC, the owner of this message is not set */
1265 if (!is_tfm_rpc_msg(msg)) {
1266 TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK);
1267 }
1268
1269 for (i = 0; i < PSA_MAX_IOVEC; i++) {
1270 if (msg->msg.out_size[i] == 0) {
1271 continue;
1272 }
1273
1274 TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
1275
1276 msg->caller_outvec[i].len = msg->outvec[i].len;
1277 }
1278}
1279
1280void tfm_spm_psa_reply(uint32_t *args)
1281{
1282 psa_handle_t msg_handle;
1283 psa_status_t status;
1284 struct tfm_spm_service_t *service = NULL;
1285 struct tfm_msg_body_t *msg = NULL;
1286 int32_t ret = PSA_SUCCESS;
1287
1288 TFM_CORE_ASSERT(args != NULL);
1289 msg_handle = (psa_handle_t)args[0];
1290 status = (psa_status_t)args[1];
1291
1292 /* It is a fatal error if message handle is invalid */
1293 msg = tfm_spm_get_msg_from_handle(msg_handle);
1294 if (!msg) {
1295 tfm_core_panic();
1296 }
1297
1298 /*
1299 * RoT Service information is needed in this function, stored it in message
1300 * body structure. Only two parameters are passed in this function: handle
1301 * and status, so it is useful and simply to do like this.
1302 */
1303 service = msg->service;
1304 if (!service) {
1305 tfm_core_panic();
1306 }
1307
1308 /*
1309 * Three type of message are passed in this function: CONNECTION, REQUEST,
1310 * DISCONNECTION. It needs to process differently for each type.
1311 */
1312 switch (msg->msg.type) {
1313 case PSA_IPC_CONNECT:
1314 /*
1315 * Reply to PSA_IPC_CONNECT message. Connect handle is returned if the
1316 * input status is PSA_SUCCESS. Others return values are based on the
1317 * input status.
1318 */
1319 if (status == PSA_SUCCESS) {
Summer Qin373feb12020-03-27 15:35:33 +08001320 ret = tfm_spm_to_user_handle(msg->handle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001321 } else if (status == PSA_ERROR_CONNECTION_REFUSED) {
1322 /* Refuse the client connection, indicating a permanent error. */
1323 tfm_spm_free_conn_handle(service, msg->handle);
1324 ret = PSA_ERROR_CONNECTION_REFUSED;
1325 } else if (status == PSA_ERROR_CONNECTION_BUSY) {
1326 /* Fail the client connection, indicating a transient error. */
1327 ret = PSA_ERROR_CONNECTION_BUSY;
1328 } else {
1329 tfm_core_panic();
1330 }
1331 break;
1332 case PSA_IPC_DISCONNECT:
1333 /* Service handle is not used anymore */
1334 tfm_spm_free_conn_handle(service, msg->handle);
1335
1336 /*
1337 * If the message type is PSA_IPC_DISCONNECT, then the status code is
1338 * ignored
1339 */
1340 break;
1341 default:
1342 if (msg->msg.type >= PSA_IPC_CALL) {
1343 /* Reply to a request message. Return values are based on status */
1344 ret = status;
1345 /*
1346 * The total number of bytes written to a single parameter must be
1347 * reported to the client by updating the len member of the
1348 * psa_outvec structure for the parameter before returning from
1349 * psa_call().
1350 */
1351 update_caller_outvec_len(msg);
1352 } else {
1353 tfm_core_panic();
1354 }
1355 }
1356
1357 if (ret == PSA_ERROR_PROGRAMMER_ERROR) {
1358 /*
1359 * If the source of the programmer error is a Secure Partition, the SPM
1360 * must panic the Secure Partition in response to a PROGRAMMER ERROR.
1361 */
1362 if (TFM_CLIENT_ID_IS_NS(msg->msg.client_id)) {
1363 ((struct tfm_conn_handle_t *)(msg->handle))->status =
1364 TFM_HANDLE_STATUS_CONNECT_ERROR;
1365 } else {
1366 tfm_core_panic();
1367 }
1368 } else {
1369 ((struct tfm_conn_handle_t *)(msg->handle))->status =
1370 TFM_HANDLE_STATUS_IDLE;
1371 }
1372
1373 if (is_tfm_rpc_msg(msg)) {
1374 tfm_rpc_client_call_reply(msg, ret);
1375 } else {
1376 tfm_event_wake(&msg->ack_evnt, ret);
1377 }
1378}
1379
1380/**
1381 * \brief notify the partition with the signal.
1382 *
1383 * \param[in] partition_id The ID of the partition to be notified.
1384 * \param[in] signal The signal that the partition is to be notified
1385 * with.
1386 *
1387 * \retval void Success.
1388 * \retval "Does not return" If partition_id is invalid.
1389 */
1390static void notify_with_signal(int32_t partition_id, psa_signal_t signal)
1391{
1392 struct spm_partition_desc_t *partition = NULL;
1393
1394 /*
1395 * The value of partition_id must be greater than zero as the target of
1396 * notification must be a Secure Partition, providing a Non-secure
1397 * Partition ID is a fatal error.
1398 */
1399 if (!TFM_CLIENT_ID_IS_S(partition_id)) {
1400 tfm_core_panic();
1401 }
1402
1403 /*
1404 * It is a fatal error if partition_id does not correspond to a Secure
1405 * Partition.
1406 */
1407 partition = tfm_spm_get_partition_by_id(partition_id);
1408 if (!partition) {
1409 tfm_core_panic();
1410 }
1411
1412 partition->runtime_data.signals |= signal;
1413
1414 /*
1415 * The target partition may be blocked with waiting for signals after
1416 * called psa_wait(). Set the return value with the available signals
1417 * before wake it up with tfm_event_signal().
1418 */
1419 tfm_event_wake(&partition->runtime_data.signal_evnt,
1420 partition->runtime_data.signals &
1421 partition->runtime_data.signal_mask);
1422}
1423
1424void tfm_spm_psa_notify(uint32_t *args)
1425{
1426 int32_t partition_id;
1427
1428 TFM_CORE_ASSERT(args != NULL);
1429 partition_id = (int32_t)args[0];
1430
1431 notify_with_signal(partition_id, PSA_DOORBELL);
1432}
1433
1434/**
1435 * \brief assert signal for a given IRQ line.
1436 *
1437 * \param[in] partition_id The ID of the partition which handles this IRQ
1438 * \param[in] signal The signal associated with this IRQ
1439 * \param[in] irq_line The number of the IRQ line
1440 *
1441 * \retval void Success.
1442 * \retval "Does not return" Partition ID is invalid
1443 */
1444void tfm_irq_handler(uint32_t partition_id, psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +01001445 IRQn_Type irq_line)
Mingyang Sund44522a2020-01-16 16:48:37 +08001446{
1447 tfm_spm_hal_disable_irq(irq_line);
1448 notify_with_signal(partition_id, signal);
1449}
1450
1451void tfm_spm_psa_clear(void)
1452{
1453 struct spm_partition_desc_t *partition = NULL;
1454
1455 partition = tfm_spm_get_running_partition();
1456 if (!partition) {
1457 tfm_core_panic();
1458 }
1459
1460 /*
1461 * It is a fatal error if the Secure Partition's doorbell signal is not
1462 * currently asserted.
1463 */
1464 if ((partition->runtime_data.signals & PSA_DOORBELL) == 0) {
1465 tfm_core_panic();
1466 }
1467 partition->runtime_data.signals &= ~PSA_DOORBELL;
1468}
1469
1470void tfm_spm_psa_panic(void)
1471{
1472 /*
1473 * PSA FF recommends that the SPM causes the system to restart when a secure
1474 * partition panics.
1475 */
1476 tfm_spm_hal_system_reset();
1477}
1478
1479/**
1480 * \brief Return the IRQ line number associated with a signal
1481 *
1482 * \param[in] partition_id The ID of the partition in which we look for
1483 * the signal.
1484 * \param[in] signal The signal we do the query for.
1485 * \param[out] irq_line The irq line associated with signal
1486 *
1487 * \retval IPC_SUCCESS Execution successful, irq_line contains a valid
1488 * value.
1489 * \retval IPC_ERROR_GENERIC There was an error finding the IRQ line for the
1490 * signal. irq_line is unchanged.
1491 */
1492static int32_t get_irq_line_for_signal(int32_t partition_id,
1493 psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +01001494 IRQn_Type *irq_line)
Mingyang Sund44522a2020-01-16 16:48:37 +08001495{
1496 size_t i;
1497
1498 for (i = 0; i < tfm_core_irq_signals_count; ++i) {
1499 if (tfm_core_irq_signals[i].partition_id == partition_id &&
1500 tfm_core_irq_signals[i].signal_value == signal) {
1501 *irq_line = tfm_core_irq_signals[i].irq_line;
1502 return IPC_SUCCESS;
1503 }
1504 }
1505 return IPC_ERROR_GENERIC;
1506}
1507
1508void tfm_spm_psa_eoi(uint32_t *args)
1509{
1510 psa_signal_t irq_signal;
TTornblomfaf74f52020-03-04 17:56:27 +01001511 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001512 int32_t ret;
1513 struct spm_partition_desc_t *partition = NULL;
1514
1515 TFM_CORE_ASSERT(args != NULL);
1516 irq_signal = (psa_signal_t)args[0];
1517
1518 /* It is a fatal error if passed signal indicates more than one signals. */
1519 if (!tfm_is_one_bit_set(irq_signal)) {
1520 tfm_core_panic();
1521 }
1522
1523 partition = tfm_spm_get_running_partition();
1524 if (!partition) {
1525 tfm_core_panic();
1526 }
1527
1528 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1529 irq_signal, &irq_line);
1530 /* It is a fatal error if passed signal is not an interrupt signal. */
1531 if (ret != IPC_SUCCESS) {
1532 tfm_core_panic();
1533 }
1534
1535 /* It is a fatal error if passed signal is not currently asserted */
1536 if ((partition->runtime_data.signals & irq_signal) == 0) {
1537 tfm_core_panic();
1538 }
1539
1540 partition->runtime_data.signals &= ~irq_signal;
1541
1542 tfm_spm_hal_clear_pending_irq(irq_line);
1543 tfm_spm_hal_enable_irq(irq_line);
1544}
1545
1546void tfm_spm_enable_irq(uint32_t *args)
1547{
1548 struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args;
1549 psa_signal_t irq_signal = svc_ctx->r0;
TTornblomfaf74f52020-03-04 17:56:27 +01001550 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001551 int32_t ret;
1552 struct spm_partition_desc_t *partition = NULL;
1553
1554 /* It is a fatal error if passed signal indicates more than one signals. */
1555 if (!tfm_is_one_bit_set(irq_signal)) {
1556 tfm_core_panic();
1557 }
1558
1559 partition = tfm_spm_get_running_partition();
1560 if (!partition) {
1561 tfm_core_panic();
1562 }
1563
1564 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1565 irq_signal, &irq_line);
1566 /* It is a fatal error if passed signal is not an interrupt signal. */
1567 if (ret != IPC_SUCCESS) {
1568 tfm_core_panic();
1569 }
1570
1571 tfm_spm_hal_enable_irq(irq_line);
1572}
1573
1574void tfm_spm_disable_irq(uint32_t *args)
1575{
1576 struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args;
1577 psa_signal_t irq_signal = svc_ctx->r0;
TTornblomfaf74f52020-03-04 17:56:27 +01001578 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001579 int32_t ret;
1580 struct spm_partition_desc_t *partition = NULL;
1581
1582 /* It is a fatal error if passed signal indicates more than one signals. */
1583 if (!tfm_is_one_bit_set(irq_signal)) {
1584 tfm_core_panic();
1585 }
1586
1587 partition = tfm_spm_get_running_partition();
1588 if (!partition) {
1589 tfm_core_panic();
1590 }
1591
1592 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1593 irq_signal, &irq_line);
1594 /* It is a fatal error if passed signal is not an interrupt signal. */
1595 if (ret != IPC_SUCCESS) {
1596 tfm_core_panic();
1597 }
1598
1599 tfm_spm_hal_disable_irq(irq_line);
1600}
1601
1602void tfm_spm_validate_caller(struct spm_partition_desc_t *p_cur_sp,
1603 uint32_t *p_ctx, uint32_t exc_return,
1604 bool ns_caller)
1605{
1606 uintptr_t stacked_ctx_pos;
1607
1608 if (ns_caller) {
1609 /*
1610 * The background IRQ can't be supported, since if SP is executing,
1611 * the preempted context of SP can be different with the one who
1612 * preempts veneer.
1613 */
1614 if (p_cur_sp->static_data->partition_id != TFM_SP_NON_SECURE_ID) {
1615 tfm_core_panic();
1616 }
1617
1618 /*
1619 * It is non-secure caller, check if veneer stack contains
1620 * multiple contexts.
1621 */
1622 stacked_ctx_pos = (uintptr_t)p_ctx +
1623 sizeof(struct tfm_state_context_t) +
1624 TFM_VENEER_STACK_GUARD_SIZE;
1625
1626 if (is_stack_alloc_fp_space(exc_return)) {
1627#if defined (__FPU_USED) && (__FPU_USED == 1U)
1628 if (FPU->FPCCR & FPU_FPCCR_TS_Msk) {
1629 stacked_ctx_pos += TFM_ADDTIONAL_FP_CONTEXT_WORDS *
1630 sizeof(uint32_t);
1631 }
1632#endif
1633 stacked_ctx_pos += TFM_BASIC_FP_CONTEXT_WORDS * sizeof(uint32_t);
1634 }
1635
1636 if (stacked_ctx_pos != p_cur_sp->runtime_data.sp_thrd.stk_top) {
1637 tfm_core_panic();
1638 }
1639 } else if (p_cur_sp->static_data->partition_id <= 0) {
1640 tfm_core_panic();
1641 }
1642}
Summer Qin830c5542020-02-14 13:44:20 +08001643
1644void tfm_spm_request_handler(const struct tfm_state_context_t *svc_ctx)
1645{
1646 uint32_t *res_ptr = (uint32_t *)&svc_ctx->r0;
1647 uint32_t running_partition_flags = 0;
1648 const struct spm_partition_desc_t *partition = NULL;
1649
1650 /* Check permissions on request type basis */
1651
1652 switch (svc_ctx->r0) {
1653 case TFM_SPM_REQUEST_RESET_VOTE:
1654 partition = tfm_spm_get_running_partition();
1655 if (!partition) {
1656 tfm_core_panic();
1657 }
1658 running_partition_flags = partition->static_data->partition_flags;
1659
1660 /* Currently only PSA Root of Trust services are allowed to make Reset
1661 * vote request
1662 */
1663 if ((running_partition_flags & SPM_PART_FLAG_PSA_ROT) == 0) {
1664 *res_ptr = (uint32_t)TFM_ERROR_GENERIC;
1665 }
1666
1667 /* FixMe: this is a placeholder for checks to be performed before
1668 * allowing execution of reset
1669 */
1670 *res_ptr = (uint32_t)TFM_SUCCESS;
1671
1672 break;
1673 default:
1674 *res_ptr = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
1675 }
1676}