blob: 7cb3fc0e827f43942f765c8fa55c5e209c1e1894 [file] [log] [blame]
Edison Ai764d41f2018-09-21 15:56:36 +08001/*
Ken Liu5248af22019-12-29 12:47:13 +08002 * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
Edison Ai764d41f2018-09-21 15:56:36 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
Mingyang Sunda01a972019-07-12 17:32:59 +08007
Edison Ai764d41f2018-09-21 15:56:36 +08008#include <inttypes.h>
9#include <stdbool.h>
Jamie Foxcc31d402019-01-28 17:13:52 +000010#include "psa/client.h"
11#include "psa/service.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080012#include "psa/lifecycle.h"
13#include "tfm_thread.h"
Edison Ai764d41f2018-09-21 15:56:36 +080014#include "tfm_wait.h"
Summer Qin5fdcf632020-06-22 16:49:24 +080015#include "utilities.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080016#include "tfm_internal_defines.h"
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"
Mingyang Sunc3123ec2020-06-11 17:43:58 +080023#include "spm_api.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080024#include "tfm_peripherals_def.h"
Mingyang Sunc3123ec2020-06-11 17:43:58 +080025#include "spm_db.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080026#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"
Mingyang Sunbd7ceb52020-06-11 16:53:03 +080034#include "region.h"
Summer Qin2bfd2a02018-09-26 17:10:41 +080035#include "region_defs.h"
Summer Qin5fdcf632020-06-22 16:49:24 +080036#include "tfm/tfm_spm_services.h"
Edison Ai764d41f2018-09-21 15:56:36 +080037
Ken Liu1f345b02020-05-30 21:11:05 +080038#include "secure_fw/partitions/tfm_service_list.inc"
Mingyang Sunbd7ceb52020-06-11 16:53:03 +080039#include "tfm_spm_db_ipc.inc"
Summer Qind99509f2019-08-02 17:36:58 +080040
41/* Extern service variable */
42extern struct tfm_spm_service_t service[];
Summer Qine578c5b2019-08-16 16:42:16 +080043extern const struct tfm_spm_service_db_t service_db[];
Summer Qind99509f2019-08-02 17:36:58 +080044
Edison Ai764d41f2018-09-21 15:56:36 +080045/* Pools */
46TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t),
47 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +080048
Mingyang Sund44522a2020-01-16 16:48:37 +080049void tfm_irq_handler(uint32_t partition_id, psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +010050 IRQn_Type irq_line);
Mingyang Sund44522a2020-01-16 16:48:37 +080051
52#include "tfm_secure_irq_handlers_ipc.inc"
Edison Ai764d41f2018-09-21 15:56:36 +080053
Summer Qin373feb12020-03-27 15:35:33 +080054/*********************** Connection handle conversion APIs *******************/
55
56/* Set a minimal value here for feature expansion. */
57#define CLIENT_HANDLE_VALUE_MIN 32
58
59#define CONVERSION_FACTOR_BITOFFSET 3
60#define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET)
61/* Set 32 as the maximum */
62#define CONVERSION_FACTOR_VALUE_MAX 0x20
63
64#if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX
65#error "CONVERSION FACTOR OUT OF RANGE"
66#endif
67
68static uint32_t loop_index;
69
70/*
71 * A handle instance psa_handle_t allocated inside SPM is actually a memory
72 * address among the handle pool. Return this handle to the client directly
73 * exposes information of secure memory address. In this case, converting the
74 * handle into another value does not represent the memory address to avoid
75 * exposing secure memory directly to clients.
76 *
77 * This function converts the handle instance into another value by scaling the
78 * handle in pool offset, the converted value is named as a user handle.
79 *
80 * The formula:
81 * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE +
82 * CLIENT_HANDLE_VALUE_MIN + loop_index
83 * where:
84 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
85 * exceed CONVERSION_FACTOR_VALUE_MAX.
86 *
87 * handle_instance in RANGE[POOL_START, POOL_END]
88 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
89 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
90 *
91 * note:
92 * loop_index is used to promise same handle instance is converted into
93 * different user handles in short time.
94 */
Ken Liu505b1702020-05-29 13:19:58 +080095psa_handle_t tfm_spm_to_user_handle(struct tfm_conn_handle_t *handle_instance)
Summer Qin373feb12020-03-27 15:35:33 +080096{
97 psa_handle_t user_handle;
98
99 loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE;
100 user_handle = (psa_handle_t)((((uintptr_t)handle_instance -
101 (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) +
102 CLIENT_HANDLE_VALUE_MIN + loop_index);
103
104 return user_handle;
105}
106
107/*
108 * This function converts a user handle into a corresponded handle instance.
109 * The converted value is validated before returning, an invalid handle instance
110 * is returned as NULL.
111 *
112 * The formula:
113 * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) /
114 * CONVERSION_FACTOR_VALUE) + POOL_START
115 * where:
116 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
117 * exceed CONVERSION_FACTOR_VALUE_MAX.
118 *
119 * handle_instance in RANGE[POOL_START, POOL_END]
120 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
121 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
122 */
123struct tfm_conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle)
124{
125 struct tfm_conn_handle_t *handle_instance;
126
127 if (user_handle == PSA_NULL_HANDLE) {
128 return NULL;
129 }
130
131 handle_instance = (struct tfm_conn_handle_t *)((((uintptr_t)user_handle -
132 CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) +
133 (uintptr_t)conn_handle_pool);
134
135 return handle_instance;
136}
137
Edison Ai764d41f2018-09-21 15:56:36 +0800138/* Service handle management functions */
Summer Qin630c76b2020-05-20 10:32:58 +0800139struct tfm_conn_handle_t *tfm_spm_create_conn_handle(
140 struct tfm_spm_service_t *service,
Summer Qin1ce712a2019-10-14 18:04:05 +0800141 int32_t client_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800142{
Edison Ai9cc26242019-08-06 11:28:04 +0800143 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800144
Ken Liuf250b8b2019-12-27 16:31:24 +0800145 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800146
147 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +0800148 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
149 if (!p_handle) {
Summer Qin630c76b2020-05-20 10:32:58 +0800150 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800151 }
152
Edison Ai9cc26242019-08-06 11:28:04 +0800153 p_handle->service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800154 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +0800155 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800156
157 /* Add handle node to list for next psa functions */
Edison Ai9cc26242019-08-06 11:28:04 +0800158 tfm_list_add_tail(&service->handle_list, &p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800159
Summer Qin630c76b2020-05-20 10:32:58 +0800160 return p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800161}
162
Summer Qin630c76b2020-05-20 10:32:58 +0800163int32_t tfm_spm_validate_conn_handle(
164 const struct tfm_conn_handle_t *conn_handle,
165 int32_t client_id)
Summer Qin1ce712a2019-10-14 18:04:05 +0800166{
167 /* Check the handle address is validated */
168 if (is_valid_chunk_data_in_pool(conn_handle_pool,
169 (uint8_t *)conn_handle) != true) {
170 return IPC_ERROR_GENERIC;
171 }
172
173 /* Check the handle caller is correct */
Summer Qin630c76b2020-05-20 10:32:58 +0800174 if (conn_handle->client_id != client_id) {
Summer Qin1ce712a2019-10-14 18:04:05 +0800175 return IPC_ERROR_GENERIC;
176 }
177
178 return IPC_SUCCESS;
179}
180
Mingyang Sund44522a2020-01-16 16:48:37 +0800181/**
182 * \brief Free connection handle which not used anymore.
183 *
184 * \param[in] service Target service context pointer
185 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800186 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800187 *
188 * \retval IPC_SUCCESS Success
189 * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
190 * \retval "Does not return" Panic for not find service by handle
191 */
192static int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800193 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800194{
Ken Liuf250b8b2019-12-27 16:31:24 +0800195 TFM_CORE_ASSERT(service);
Summer Qin630c76b2020-05-20 10:32:58 +0800196 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800197
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200198 /* Clear magic as the handler is not used anymore */
Summer Qin630c76b2020-05-20 10:32:58 +0800199 conn_handle->internal_msg.magic = 0;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200200
Edison Ai764d41f2018-09-21 15:56:36 +0800201 /* Remove node from handle list */
Summer Qin630c76b2020-05-20 10:32:58 +0800202 tfm_list_del_node(&conn_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800203
204 /* Back handle buffer to pool */
Summer Qin630c76b2020-05-20 10:32:58 +0800205 tfm_pool_free(conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800206 return IPC_SUCCESS;
207}
208
Mingyang Sund44522a2020-01-16 16:48:37 +0800209/**
210 * \brief Set reverse handle value for connection.
211 *
212 * \param[in] service Target service context pointer
213 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800214 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800215 * \param[in] rhandle rhandle need to save
216 *
217 * \retval IPC_SUCCESS Success
218 * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
219 * \retval "Does not return" Panic for not find handle node
220 */
221static int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800222 struct tfm_conn_handle_t *conn_handle,
Mingyang Sund44522a2020-01-16 16:48:37 +0800223 void *rhandle)
Edison Ai764d41f2018-09-21 15:56:36 +0800224{
Ken Liuf250b8b2019-12-27 16:31:24 +0800225 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800226 /* Set reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800227 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800228
Summer Qin630c76b2020-05-20 10:32:58 +0800229 conn_handle->rhandle = rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800230 return IPC_SUCCESS;
231}
232
Mingyang Sund44522a2020-01-16 16:48:37 +0800233/**
234 * \brief Get reverse handle value from connection hanlde.
235 *
236 * \param[in] service Target service context pointer
237 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800238 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800239 *
240 * \retval void * Success
241 * \retval "Does not return" Panic for those:
242 * service pointer are NULL
243 * hanlde is \ref PSA_NULL_HANDLE
244 * handle node does not be found
245 */
246static void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800247 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800248{
Ken Liuf250b8b2019-12-27 16:31:24 +0800249 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800250 /* Get reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800251 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800252
Summer Qin630c76b2020-05-20 10:32:58 +0800253 return conn_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800254}
255
256/* Partition management functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800257
258/**
259 * \brief Get the service context by signal.
260 *
261 * \param[in] partition Partition context pointer
262 * \ref spm_partition_desc_t structures
263 * \param[in] signal Signal associated with inputs to the Secure
264 * Partition, \ref psa_signal_t
265 *
266 * \retval NULL Failed
267 * \retval "Not NULL" Target service context pointer,
268 * \ref tfm_spm_service_t structures
269 */
270static struct tfm_spm_service_t *
Mingyang Sunf3d29892019-07-10 17:50:23 +0800271 tfm_spm_get_service_by_signal(struct spm_partition_desc_t *partition,
272 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800273{
274 struct tfm_list_node_t *node, *head;
275 struct tfm_spm_service_t *service;
276
Ken Liuf250b8b2019-12-27 16:31:24 +0800277 TFM_CORE_ASSERT(partition);
Edison Ai764d41f2018-09-21 15:56:36 +0800278
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800279 if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
Edison Ai9059ea02019-11-28 13:46:14 +0800280 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800281 }
282
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800283 head = &partition->runtime_data.service_list;
Edison Ai764d41f2018-09-21 15:56:36 +0800284 TFM_LIST_FOR_EACH(node, head) {
285 service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t, list);
Summer Qine578c5b2019-08-16 16:42:16 +0800286 if (service->service_db->signal == signal) {
Edison Ai764d41f2018-09-21 15:56:36 +0800287 return service;
288 }
289 }
290 return NULL;
291}
292
293struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid)
294{
Summer Qin2fca1c82020-03-20 14:37:55 +0800295 uint32_t i, num;
Edison Ai764d41f2018-09-21 15:56:36 +0800296
Summer Qin2fca1c82020-03-20 14:37:55 +0800297 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
298 for (i = 0; i < num; i++) {
299 if (service[i].service_db->sid == sid) {
300 return &service[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800301 }
302 }
Summer Qin2fca1c82020-03-20 14:37:55 +0800303
Edison Ai764d41f2018-09-21 15:56:36 +0800304 return NULL;
305}
306
Mingyang Sund44522a2020-01-16 16:48:37 +0800307/**
308 * \brief Get the partition context by partition ID.
309 *
310 * \param[in] partition_id Partition identity
311 *
312 * \retval NULL Failed
313 * \retval "Not NULL" Target partition context pointer,
314 * \ref spm_partition_desc_t structures
315 */
316static struct spm_partition_desc_t *
317 tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800318{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800319 uint32_t idx = get_partition_idx(partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800320
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800321 if (idx != SPM_INVALID_PARTITION_IDX) {
322 return &(g_spm_partition_db.partitions[idx]);
Edison Ai764d41f2018-09-21 15:56:36 +0800323 }
324 return NULL;
325}
326
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800327struct spm_partition_desc_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800328{
Kevin Peng79c2bda2020-07-24 16:31:12 +0800329 struct tfm_core_thread_t *pth = tfm_core_thrd_get_curr_thread();
330 struct spm_partition_desc_t *partition;
331 struct spm_partition_runtime_data_t *rt_data;
Edison Ai764d41f2018-09-21 15:56:36 +0800332
Kevin Peng79c2bda2020-07-24 16:31:12 +0800333 rt_data = TFM_GET_CONTAINER_PTR(pth, struct spm_partition_runtime_data_t,
334 sp_thrd);
335 partition = TFM_GET_CONTAINER_PTR(rt_data, struct spm_partition_desc_t,
336 runtime_data);
337 return partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800338}
339
340int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530341 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800342{
Ken Liuf250b8b2019-12-27 16:31:24 +0800343 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800344
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530345 switch (service->service_db->version_policy) {
Edison Ai764d41f2018-09-21 15:56:36 +0800346 case TFM_VERSION_POLICY_RELAXED:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530347 if (version > service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800348 return IPC_ERROR_VERSION;
349 }
350 break;
351 case TFM_VERSION_POLICY_STRICT:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530352 if (version != service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800353 return IPC_ERROR_VERSION;
354 }
355 break;
356 default:
357 return IPC_ERROR_VERSION;
358 }
359 return IPC_SUCCESS;
360}
361
Edison Aie728fbf2019-11-13 09:37:12 +0800362int32_t tfm_spm_check_authorization(uint32_t sid,
363 struct tfm_spm_service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800364 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800365{
366 struct spm_partition_desc_t *partition = NULL;
367 int32_t i;
368
Ken Liuf250b8b2019-12-27 16:31:24 +0800369 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800370
371 if (ns_caller) {
372 if (!service->service_db->non_secure_client) {
373 return IPC_ERROR_GENERIC;
374 }
375 } else {
376 partition = tfm_spm_get_running_partition();
377 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800378 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800379 }
380
381 for (i = 0; i < partition->static_data->dependencies_num; i++) {
382 if (partition->static_data->p_dependencies[i] == sid) {
383 break;
384 }
385 }
386
387 if (i == partition->static_data->dependencies_num) {
388 return IPC_ERROR_GENERIC;
389 }
390 }
391 return IPC_SUCCESS;
392}
393
Edison Ai764d41f2018-09-21 15:56:36 +0800394/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800395
396/**
397 * \brief Get message context by message handle.
398 *
399 * \param[in] msg_handle Message handle which is a reference generated
400 * by the SPM to a specific message.
401 *
402 * \return The message body context pointer
403 * \ref tfm_msg_body_t structures
404 */
405static struct tfm_msg_body_t *
406 tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800407{
408 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200409 * The message handler passed by the caller is considered invalid in the
410 * following cases:
411 * 1. Not a valid message handle. (The address of a message is not the
412 * address of a possible handle from the pool
413 * 2. Handle not belongs to the caller partition (The handle is either
414 * unused, or owned by anither partition)
415 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800416 */
Ken Liu505b1702020-05-29 13:19:58 +0800417 struct tfm_msg_body_t *p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800418 uint32_t partition_id;
Ken Liu505b1702020-05-29 13:19:58 +0800419 struct tfm_conn_handle_t *p_conn_handle =
420 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200421
422 if (is_valid_chunk_data_in_pool(
Ken Liu505b1702020-05-29 13:19:58 +0800423 conn_handle_pool, (uint8_t *)p_conn_handle) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800424 return NULL;
425 }
426
Ken Liu505b1702020-05-29 13:19:58 +0800427 p_msg = &p_conn_handle->internal_msg;
428
Edison Ai764d41f2018-09-21 15:56:36 +0800429 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200430 * Check that the magic number is correct. This proves that the message
431 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800432 */
Ken Liu505b1702020-05-29 13:19:58 +0800433 if (p_msg->magic != TFM_MSG_MAGIC) {
Edison Ai764d41f2018-09-21 15:56:36 +0800434 return NULL;
435 }
436
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200437 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800438 partition_id = tfm_spm_partition_get_running_partition_id();
Ken Liu505b1702020-05-29 13:19:58 +0800439 if (partition_id != p_msg->service->partition->static_data->partition_id) {
Edison Ai764d41f2018-09-21 15:56:36 +0800440 return NULL;
441 }
442
Ken Liu505b1702020-05-29 13:19:58 +0800443 return p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800444}
445
Edison Ai97115822019-08-01 14:22:19 +0800446struct tfm_msg_body_t *
Summer Qin630c76b2020-05-20 10:32:58 +0800447 tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800448{
Summer Qin630c76b2020-05-20 10:32:58 +0800449 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai97115822019-08-01 14:22:19 +0800450
Summer Qin630c76b2020-05-20 10:32:58 +0800451 return &(conn_handle->internal_msg);
Edison Ai97115822019-08-01 14:22:19 +0800452}
453
454void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
455 struct tfm_spm_service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800456 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800457 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800458 psa_invec *invec, size_t in_len,
459 psa_outvec *outvec, size_t out_len,
460 psa_outvec *caller_outvec)
461{
Edison Ai764d41f2018-09-21 15:56:36 +0800462 uint32_t i;
Ken Liu505b1702020-05-29 13:19:58 +0800463 struct tfm_conn_handle_t *conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800464
Ken Liuf250b8b2019-12-27 16:31:24 +0800465 TFM_CORE_ASSERT(msg);
466 TFM_CORE_ASSERT(service);
467 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
468 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
469 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
470 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
471 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800472
Edison Ai764d41f2018-09-21 15:56:36 +0800473 /* Clear message buffer before using it */
Mingyang Sun94b1b412019-09-20 15:11:14 +0800474 tfm_core_util_memset(msg, 0, sizeof(struct tfm_msg_body_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800475
Ken Liu35f89392019-03-14 14:51:05 +0800476 tfm_event_init(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800477 msg->magic = TFM_MSG_MAGIC;
478 msg->service = service;
Edison Ai764d41f2018-09-21 15:56:36 +0800479 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800480 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800481
482 /* Copy contents */
483 msg->msg.type = type;
484
485 for (i = 0; i < in_len; i++) {
486 msg->msg.in_size[i] = invec[i].len;
487 msg->invec[i].base = invec[i].base;
488 }
489
490 for (i = 0; i < out_len; i++) {
491 msg->msg.out_size[i] = outvec[i].len;
492 msg->outvec[i].base = outvec[i].base;
493 /* Out len is used to record the writed number, set 0 here again */
494 msg->outvec[i].len = 0;
495 }
496
Ken Liu505b1702020-05-29 13:19:58 +0800497 /* Use the user connect handle as the message handle */
498 msg->msg.handle = handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800499
Ken Liu505b1702020-05-29 13:19:58 +0800500 conn_handle = tfm_spm_to_handle_instance(handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800501 /* For connected handle, set rhandle to every message */
Ken Liu505b1702020-05-29 13:19:58 +0800502 if (conn_handle) {
503 msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800504 }
David Hu46603dd2019-12-11 18:05:16 +0800505
506 /* Set the private data of NSPE client caller in multi-core topology */
507 if (TFM_CLIENT_ID_IS_NS(client_id)) {
508 tfm_rpc_set_caller_data(msg, client_id);
509 }
Edison Ai764d41f2018-09-21 15:56:36 +0800510}
511
512int32_t tfm_spm_send_event(struct tfm_spm_service_t *service,
513 struct tfm_msg_body_t *msg)
514{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800515 struct spm_partition_runtime_data_t *p_runtime_data =
516 &service->partition->runtime_data;
517
Ken Liuf250b8b2019-12-27 16:31:24 +0800518 TFM_CORE_ASSERT(service);
519 TFM_CORE_ASSERT(msg);
Edison Ai764d41f2018-09-21 15:56:36 +0800520
521 /* Enqueue message to service message queue */
522 if (tfm_msg_enqueue(&service->msg_queue, msg) != IPC_SUCCESS) {
523 return IPC_ERROR_GENERIC;
524 }
525
526 /* Messages put. Update signals */
Summer Qine578c5b2019-08-16 16:42:16 +0800527 p_runtime_data->signals |= service->service_db->signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800528
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800529 tfm_event_wake(&p_runtime_data->signal_evnt, (p_runtime_data->signals &
530 p_runtime_data->signal_mask));
Edison Ai764d41f2018-09-21 15:56:36 +0800531
David Hufb38d562019-09-23 15:58:34 +0800532 /*
533 * If it is a NS request via RPC, it is unnecessary to block current
534 * thread.
535 */
536 if (!is_tfm_rpc_msg(msg)) {
537 tfm_event_wait(&msg->ack_evnt);
538 }
Edison Ai764d41f2018-09-21 15:56:36 +0800539
540 return IPC_SUCCESS;
541}
542
Mingyang Sunf3d29892019-07-10 17:50:23 +0800543uint32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800544{
Edison Ai764d41f2018-09-21 15:56:36 +0800545 struct spm_partition_desc_t *partition;
546
Kevin Peng79c2bda2020-07-24 16:31:12 +0800547 partition = tfm_spm_get_running_partition();
548 if (partition && partition->static_data) {
549 return partition->static_data->partition_id;
550 } else {
551 return INVALID_PARTITION_ID;
552 }
Edison Ai764d41f2018-09-21 15:56:36 +0800553}
554
Summer Qin43c185d2019-10-10 15:44:42 +0800555int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800556 enum tfm_memory_access_e access,
557 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800558{
Hugues de Valon99578562019-06-18 16:08:51 +0100559 enum tfm_status_e err;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800560
561 /* If len is zero, this indicates an empty buffer and base is ignored */
562 if (len == 0) {
563 return IPC_SUCCESS;
564 }
565
566 if (!buffer) {
567 return IPC_ERROR_BAD_PARAMETERS;
568 }
569
570 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
571 return IPC_ERROR_MEMORY_CHECK;
572 }
573
Summer Qin424d4db2019-03-25 14:09:51 +0800574 if (access == TFM_MEMORY_ACCESS_RW) {
Summer Qineb537e52019-03-29 09:57:10 +0800575 err = tfm_core_has_write_access_to_region(buffer, len, ns_caller,
576 privileged);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800577 } else {
Summer Qineb537e52019-03-29 09:57:10 +0800578 err = tfm_core_has_read_access_to_region(buffer, len, ns_caller,
579 privileged);
Summer Qin424d4db2019-03-25 14:09:51 +0800580 }
Summer Qin0fc3f592019-04-11 16:00:10 +0800581 if (err == TFM_SUCCESS) {
Summer Qin424d4db2019-03-25 14:09:51 +0800582 return IPC_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800583 }
584
585 return IPC_ERROR_MEMORY_CHECK;
586}
587
Ken Liuce2692d2020-02-11 12:39:36 +0800588uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800589{
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800590 uint32_t i, j, num;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800591 struct spm_partition_desc_t *partition;
Summer Qin66f1e032020-01-06 15:40:03 +0800592 struct tfm_core_thread_t *pth, *p_ns_entry_thread = NULL;
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100593 const struct tfm_spm_partition_platform_data_t **platform_data_p;
Edison Ai764d41f2018-09-21 15:56:36 +0800594
595 tfm_pool_init(conn_handle_pool,
596 POOL_BUFFER_SIZE(conn_handle_pool),
597 sizeof(struct tfm_conn_handle_t),
598 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800599
600 /* Init partition first for it will be used when init service */
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200601 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800602 partition = &g_spm_partition_db.partitions[i];
Edison Aif0501702019-10-11 14:36:42 +0800603
Kevin Peng79c2bda2020-07-24 16:31:12 +0800604 if (!partition || !partition->memory_data || !partition->static_data) {
605 tfm_core_panic();
606 }
607
608 if (!(partition->static_data->partition_flags & SPM_PART_FLAG_IPC)) {
609 tfm_core_panic();
610 }
611
Edison Aif0501702019-10-11 14:36:42 +0800612 /* Check if the PSA framework version matches. */
613 if (partition->static_data->psa_framework_version !=
614 PSA_FRAMEWORK_VERSION) {
Kevin Peng79c2bda2020-07-24 16:31:12 +0800615 ERROR_MSG("Warning: PSA Framework Verison does not match!");
Edison Aif0501702019-10-11 14:36:42 +0800616 continue;
617 }
618
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100619 platform_data_p = partition->platform_data_list;
620 if (platform_data_p != NULL) {
621 while ((*platform_data_p) != NULL) {
Edison Ai6be3df12020-02-14 22:14:33 +0800622 if (tfm_spm_hal_configure_default_isolation(i,
623 *platform_data_p) != TFM_PLAT_ERR_SUCCESS) {
624 tfm_core_panic();
625 }
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100626 ++platform_data_p;
627 }
628 }
629
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800630 /* Add PSA_DOORBELL signal to assigned_signals */
631 partition->runtime_data.assigned_signals |= PSA_DOORBELL;
632
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800633 /* TODO: This can be optimized by generating the assigned signal
634 * in code generation time.
635 */
636 for (j = 0; j < tfm_core_irq_signals_count; ++j) {
637 if (tfm_core_irq_signals[j].partition_id ==
638 partition->static_data->partition_id) {
639 partition->runtime_data.assigned_signals |=
640 tfm_core_irq_signals[j].signal_value;
641 }
642 }
643
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800644 tfm_event_init(&partition->runtime_data.signal_evnt);
645 tfm_list_init(&partition->runtime_data.service_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800646
Kevin Peng79c2bda2020-07-24 16:31:12 +0800647 pth = &partition->runtime_data.sp_thrd;
Edison Ai764d41f2018-09-21 15:56:36 +0800648 if (!pth) {
Edison Ai9059ea02019-11-28 13:46:14 +0800649 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800650 }
651
Summer Qin66f1e032020-01-06 15:40:03 +0800652 tfm_core_thrd_init(pth,
Kevin Peng79c2bda2020-07-24 16:31:12 +0800653 (tfm_core_thrd_entry_t)
654 partition->static_data->partition_init,
Summer Qin66f1e032020-01-06 15:40:03 +0800655 NULL,
Kevin Peng79c2bda2020-07-24 16:31:12 +0800656 (uintptr_t)partition->memory_data->stack_top,
657 (uintptr_t)partition->memory_data->stack_bottom);
Edison Ai788bae22019-02-18 17:38:59 +0800658
Kevin Peng79c2bda2020-07-24 16:31:12 +0800659 pth->prior = partition->static_data->partition_priority;
Edison Ai764d41f2018-09-21 15:56:36 +0800660
Ken Liu490281d2019-12-30 15:55:26 +0800661 if (partition->static_data->partition_id == TFM_SP_NON_SECURE_ID) {
662 p_ns_entry_thread = pth;
Ken Liu5248af22019-12-29 12:47:13 +0800663 pth->param = (void *)tfm_spm_hal_get_ns_entry_point();
Ken Liu490281d2019-12-30 15:55:26 +0800664 }
665
Edison Ai764d41f2018-09-21 15:56:36 +0800666 /* Kick off */
Summer Qin66f1e032020-01-06 15:40:03 +0800667 if (tfm_core_thrd_start(pth) != THRD_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800668 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800669 }
670 }
671
672 /* Init Service */
Summer Qind99509f2019-08-02 17:36:58 +0800673 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
Edison Ai764d41f2018-09-21 15:56:36 +0800674 for (i = 0; i < num; i++) {
Summer Qine578c5b2019-08-16 16:42:16 +0800675 service[i].service_db = &service_db[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800676 partition =
Summer Qine578c5b2019-08-16 16:42:16 +0800677 tfm_spm_get_partition_by_id(service[i].service_db->partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800678 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800679 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800680 }
Summer Qind99509f2019-08-02 17:36:58 +0800681 service[i].partition = partition;
Jaykumar Pitambarbhai Patel0c7a0382020-01-09 15:25:58 +0530682 partition->runtime_data.assigned_signals |= service[i].service_db->signal;
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800683
Summer Qind99509f2019-08-02 17:36:58 +0800684 tfm_list_init(&service[i].handle_list);
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800685 tfm_list_add_tail(&partition->runtime_data.service_list,
Summer Qind99509f2019-08-02 17:36:58 +0800686 &service[i].list);
Edison Ai764d41f2018-09-21 15:56:36 +0800687 }
688
Ken Liu483f5da2019-04-24 10:45:21 +0800689 /*
690 * All threads initialized, start the scheduler.
691 *
692 * NOTE:
Ken Liu490281d2019-12-30 15:55:26 +0800693 * It is worthy to give the thread object to scheduler if the background
694 * context belongs to one of the threads. Here the background thread is the
695 * initialization thread who calls SPM SVC, which re-uses the non-secure
696 * entry thread's stack. After SPM initialization is done, this stack is
697 * cleaned up and the background context is never going to return. Tell
698 * the scheduler that the current thread is non-secure entry thread.
Ken Liu483f5da2019-04-24 10:45:21 +0800699 */
Summer Qin66f1e032020-01-06 15:40:03 +0800700 tfm_core_thrd_start_scheduler(p_ns_entry_thread);
Ken Liuce2692d2020-02-11 12:39:36 +0800701
Summer Qind2ad7e72020-01-06 18:16:35 +0800702 return p_ns_entry_thread->arch_ctx.lr;
Edison Ai764d41f2018-09-21 15:56:36 +0800703}
Ken Liu2d175172019-03-21 17:08:41 +0800704
Summer Qind2ad7e72020-01-06 18:16:35 +0800705void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx)
Ken Liu2d175172019-03-21 17:08:41 +0800706{
707#if TFM_LVL == 2
708 struct spm_partition_desc_t *p_next_partition;
Summer Qinb5da9cc2019-08-26 15:19:45 +0800709 struct spm_partition_runtime_data_t *r_data;
Ken Liu2d175172019-03-21 17:08:41 +0800710 uint32_t is_privileged;
711#endif
Summer Qin66f1e032020-01-06 15:40:03 +0800712 struct tfm_core_thread_t *pth_next = tfm_core_thrd_get_next_thread();
713 struct tfm_core_thread_t *pth_curr = tfm_core_thrd_get_curr_thread();
Ken Liu2d175172019-03-21 17:08:41 +0800714
Mate Toth-Pal32b2ccd2019-04-26 10:00:16 +0200715 if (pth_next != NULL && pth_curr != pth_next) {
Ken Liu2d175172019-03-21 17:08:41 +0800716#if TFM_LVL == 2
Summer Qinb5da9cc2019-08-26 15:19:45 +0800717 r_data = TFM_GET_CONTAINER_PTR(pth_next,
718 struct spm_partition_runtime_data_t,
719 sp_thrd);
720 p_next_partition = TFM_GET_CONTAINER_PTR(r_data,
Ken Liu2d175172019-03-21 17:08:41 +0800721 struct spm_partition_desc_t,
Summer Qinb5da9cc2019-08-26 15:19:45 +0800722 runtime_data);
Ken Liu2d175172019-03-21 17:08:41 +0800723
Summer Qin423dbef2019-08-22 15:59:35 +0800724 if (p_next_partition->static_data->partition_flags &
Ken Liu2d175172019-03-21 17:08:41 +0800725 SPM_PART_FLAG_PSA_ROT) {
726 is_privileged = TFM_PARTITION_PRIVILEGED_MODE;
727 } else {
728 is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE;
729 }
730
731 tfm_spm_partition_change_privilege(is_privileged);
732#endif
Mate Toth-Palc430b992019-05-09 21:01:14 +0200733
Summer Qind2ad7e72020-01-06 18:16:35 +0800734 tfm_core_thrd_switch_context(p_actx, pth_curr, pth_next);
Ken Liu2d175172019-03-21 17:08:41 +0800735 }
David Hufb38d562019-09-23 15:58:34 +0800736
737 /*
738 * Handle pending mailbox message from NS in multi-core topology.
739 * Empty operation on single Armv8-M platform.
740 */
741 tfm_rpc_client_call_handler();
Ken Liu2d175172019-03-21 17:08:41 +0800742}
Mingyang Sund44522a2020-01-16 16:48:37 +0800743
744/*********************** SPM functions for PSA Client APIs *******************/
745
746uint32_t tfm_spm_psa_framework_version(void)
747{
748 return tfm_spm_client_psa_framework_version();
749}
750
751uint32_t tfm_spm_psa_version(uint32_t *args, bool ns_caller)
752{
753 uint32_t sid;
754
755 TFM_CORE_ASSERT(args != NULL);
756 sid = (uint32_t)args[0];
757
758 return tfm_spm_client_psa_version(sid, ns_caller);
759}
760
761psa_status_t tfm_spm_psa_connect(uint32_t *args, bool ns_caller)
762{
763 uint32_t sid;
764 uint32_t version;
765
766 TFM_CORE_ASSERT(args != NULL);
767 sid = (uint32_t)args[0];
768 version = (uint32_t)args[1];
769
770 return tfm_spm_client_psa_connect(sid, version, ns_caller);
771}
772
773psa_status_t tfm_spm_psa_call(uint32_t *args, bool ns_caller, uint32_t lr)
774{
775 psa_handle_t handle;
776 psa_invec *inptr;
777 psa_outvec *outptr;
778 size_t in_num, out_num;
779 struct spm_partition_desc_t *partition = NULL;
780 uint32_t privileged;
781 int32_t type;
782 struct tfm_control_parameter_t ctrl_param;
783
784 TFM_CORE_ASSERT(args != NULL);
785 handle = (psa_handle_t)args[0];
786
787 partition = tfm_spm_get_running_partition();
788 if (!partition) {
789 tfm_core_panic();
790 }
791 privileged = tfm_spm_partition_get_privileged_mode(
792 partition->static_data->partition_flags);
793
794 /*
795 * Read parameters from the arguments. It is a fatal error if the
796 * memory reference for buffer is invalid or not readable.
797 */
798 if (tfm_memory_check((const void *)args[1],
799 sizeof(struct tfm_control_parameter_t), ns_caller,
800 TFM_MEMORY_ACCESS_RW, privileged) != IPC_SUCCESS) {
801 tfm_core_panic();
802 }
803
804 tfm_core_util_memcpy(&ctrl_param,
805 (const void *)args[1],
806 sizeof(ctrl_param));
807
808 type = ctrl_param.type;
809 in_num = ctrl_param.in_len;
810 out_num = ctrl_param.out_len;
811 inptr = (psa_invec *)args[2];
812 outptr = (psa_outvec *)args[3];
813
814 /* The request type must be zero or positive. */
815 if (type < 0) {
816 tfm_core_panic();
817 }
818
819 return tfm_spm_client_psa_call(handle, type, inptr, in_num, outptr, out_num,
820 ns_caller, privileged);
821}
822
823void tfm_spm_psa_close(uint32_t *args, bool ns_caller)
824{
825 psa_handle_t handle;
826
827 TFM_CORE_ASSERT(args != NULL);
828 handle = args[0];
829
830 tfm_spm_client_psa_close(handle, ns_caller);
831}
832
833uint32_t tfm_spm_get_lifecycle_state(void)
834{
835 /*
836 * FixMe: return PSA_LIFECYCLE_UNKNOWN to the caller directly. It will be
837 * implemented in the future.
838 */
839 return PSA_LIFECYCLE_UNKNOWN;
840}
841
842/********************* SPM functions for PSA Service APIs ********************/
843
844psa_signal_t tfm_spm_psa_wait(uint32_t *args)
845{
846 psa_signal_t signal_mask;
847 uint32_t timeout;
848 struct spm_partition_desc_t *partition = NULL;
849
850 TFM_CORE_ASSERT(args != NULL);
851 signal_mask = (psa_signal_t)args[0];
852 timeout = args[1];
853
854 /*
855 * Timeout[30:0] are reserved for future use.
856 * SPM must ignore the value of RES.
857 */
858 timeout &= PSA_TIMEOUT_MASK;
859
860 partition = tfm_spm_get_running_partition();
861 if (!partition) {
862 tfm_core_panic();
863 }
864
865 /*
866 * It is a PROGRAMMER ERROR if the signal_mask does not include any assigned
867 * signals.
868 */
869 if ((partition->runtime_data.assigned_signals & signal_mask) == 0) {
870 tfm_core_panic();
871 }
872
873 /*
874 * Expected signals are included in signal wait mask, ignored signals
875 * should not be set and affect caller thread state. Save this mask for
876 * further checking while signals are ready to be set.
877 */
878 partition->runtime_data.signal_mask = signal_mask;
879
880 /*
881 * tfm_event_wait() blocks the caller thread if no signals are available.
882 * In this case, the return value of this function is temporary set into
883 * runtime context. After new signal(s) are available, the return value
884 * is updated with the available signal(s) and blocked thread gets to run.
885 */
886 if (timeout == PSA_BLOCK &&
887 (partition->runtime_data.signals & signal_mask) == 0) {
888 tfm_event_wait(&partition->runtime_data.signal_evnt);
889 }
890
891 return partition->runtime_data.signals & signal_mask;
892}
893
894psa_status_t tfm_spm_psa_get(uint32_t *args)
895{
896 psa_signal_t signal;
897 psa_msg_t *msg = NULL;
898 struct tfm_spm_service_t *service = NULL;
899 struct tfm_msg_body_t *tmp_msg = NULL;
900 struct spm_partition_desc_t *partition = NULL;
901 uint32_t privileged;
902
903 TFM_CORE_ASSERT(args != NULL);
904 signal = (psa_signal_t)args[0];
905 msg = (psa_msg_t *)args[1];
906
907 /*
908 * Only one message could be retrieved every time for psa_get(). It is a
909 * fatal error if the input signal has more than a signal bit set.
910 */
Ken Liu410ada52020-01-08 11:37:27 +0800911 if (!tfm_is_one_bit_set(signal)) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800912 tfm_core_panic();
913 }
914
915 partition = tfm_spm_get_running_partition();
916 if (!partition) {
917 tfm_core_panic();
918 }
919 privileged = tfm_spm_partition_get_privileged_mode(
920 partition->static_data->partition_flags);
921
922 /*
923 * Write the message to the service buffer. It is a fatal error if the
924 * input msg pointer is not a valid memory reference or not read-write.
925 */
926 if (tfm_memory_check(msg, sizeof(psa_msg_t), false, TFM_MEMORY_ACCESS_RW,
927 privileged) != IPC_SUCCESS) {
928 tfm_core_panic();
929 }
930
931 /*
932 * It is a fatal error if the caller call psa_get() when no message has
933 * been set. The caller must call this function after an RoT Service signal
934 * is returned by psa_wait().
935 */
936 if (partition->runtime_data.signals == 0) {
937 tfm_core_panic();
938 }
939
940 /*
941 * It is a fatal error if the RoT Service signal is not currently asserted.
942 */
943 if ((partition->runtime_data.signals & signal) == 0) {
944 tfm_core_panic();
945 }
946
947 /*
948 * Get RoT service by signal from partition. It is a fatal error if getting
949 * failed, which means the input signal is not correspond to an RoT service.
950 */
951 service = tfm_spm_get_service_by_signal(partition, signal);
952 if (!service) {
953 tfm_core_panic();
954 }
955
956 tmp_msg = tfm_msg_dequeue(&service->msg_queue);
957 if (!tmp_msg) {
958 return PSA_ERROR_DOES_NOT_EXIST;
959 }
960
Ken Liu505b1702020-05-29 13:19:58 +0800961 (TFM_GET_CONTAINER_PTR(tmp_msg,
962 struct tfm_conn_handle_t,
963 internal_msg))->status = TFM_HANDLE_STATUS_ACTIVE;
Mingyang Sund44522a2020-01-16 16:48:37 +0800964
965 tfm_core_util_memcpy(msg, &tmp_msg->msg, sizeof(psa_msg_t));
966
967 /*
968 * There may be multiple messages for this RoT Service signal, do not clear
969 * its mask until no remaining message.
970 */
971 if (tfm_msg_queue_is_empty(&service->msg_queue)) {
972 partition->runtime_data.signals &= ~signal;
973 }
974
975 return PSA_SUCCESS;
976}
977
978void tfm_spm_psa_set_rhandle(uint32_t *args)
979{
980 psa_handle_t msg_handle;
981 void *rhandle = NULL;
982 struct tfm_msg_body_t *msg = NULL;
Ken Liu505b1702020-05-29 13:19:58 +0800983 struct tfm_conn_handle_t *conn_handle;
Mingyang Sund44522a2020-01-16 16:48:37 +0800984
985 TFM_CORE_ASSERT(args != NULL);
986 msg_handle = (psa_handle_t)args[0];
987 rhandle = (void *)args[1];
988
989 /* It is a fatal error if message handle is invalid */
990 msg = tfm_spm_get_msg_from_handle(msg_handle);
991 if (!msg) {
992 tfm_core_panic();
993 }
994
995 msg->msg.rhandle = rhandle;
Ken Liu505b1702020-05-29 13:19:58 +0800996 conn_handle = tfm_spm_to_handle_instance(msg_handle);
Mingyang Sund44522a2020-01-16 16:48:37 +0800997
998 /* Store reverse handle for following client calls. */
Ken Liu505b1702020-05-29 13:19:58 +0800999 tfm_spm_set_rhandle(msg->service, conn_handle, rhandle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001000}
1001
1002size_t tfm_spm_psa_read(uint32_t *args)
1003{
1004 psa_handle_t msg_handle;
1005 uint32_t invec_idx;
1006 void *buffer = NULL;
1007 size_t num_bytes;
1008 size_t bytes;
1009 struct tfm_msg_body_t *msg = NULL;
1010 uint32_t privileged;
1011 struct spm_partition_desc_t *partition = NULL;
1012
1013 TFM_CORE_ASSERT(args != NULL);
1014 msg_handle = (psa_handle_t)args[0];
1015 invec_idx = args[1];
1016 buffer = (void *)args[2];
1017 num_bytes = (size_t)args[3];
1018
1019 /* It is a fatal error if message handle is invalid */
1020 msg = tfm_spm_get_msg_from_handle(msg_handle);
1021 if (!msg) {
1022 tfm_core_panic();
1023 }
1024
1025 partition = msg->service->partition;
1026 privileged = tfm_spm_partition_get_privileged_mode(
1027 partition->static_data->partition_flags);
1028
1029 /*
1030 * It is a fatal error if message handle does not refer to a request
1031 * message
1032 */
1033 if (msg->msg.type < PSA_IPC_CALL) {
1034 tfm_core_panic();
1035 }
1036
1037 /*
1038 * It is a fatal error if invec_idx is equal to or greater than
1039 * PSA_MAX_IOVEC
1040 */
1041 if (invec_idx >= PSA_MAX_IOVEC) {
1042 tfm_core_panic();
1043 }
1044
1045 /* There was no remaining data in this input vector */
1046 if (msg->msg.in_size[invec_idx] == 0) {
1047 return 0;
1048 }
1049
1050 /*
1051 * Copy the client data to the service buffer. It is a fatal error
1052 * if the memory reference for buffer is invalid or not read-write.
1053 */
1054 if (tfm_memory_check(buffer, num_bytes, false,
1055 TFM_MEMORY_ACCESS_RW, privileged) != IPC_SUCCESS) {
1056 tfm_core_panic();
1057 }
1058
1059 bytes = num_bytes > msg->msg.in_size[invec_idx] ?
1060 msg->msg.in_size[invec_idx] : num_bytes;
1061
1062 tfm_core_util_memcpy(buffer, msg->invec[invec_idx].base, bytes);
1063
1064 /* There maybe some remaining data */
1065 msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base + bytes;
1066 msg->msg.in_size[invec_idx] -= bytes;
1067
1068 return bytes;
1069}
1070
1071size_t tfm_spm_psa_skip(uint32_t *args)
1072{
1073 psa_handle_t msg_handle;
1074 uint32_t invec_idx;
1075 size_t num_bytes;
1076 struct tfm_msg_body_t *msg = NULL;
1077
1078 TFM_CORE_ASSERT(args != NULL);
1079 msg_handle = (psa_handle_t)args[0];
1080 invec_idx = args[1];
1081 num_bytes = (size_t)args[2];
1082
1083 /* It is a fatal error if message handle is invalid */
1084 msg = tfm_spm_get_msg_from_handle(msg_handle);
1085 if (!msg) {
1086 tfm_core_panic();
1087 }
1088
1089 /*
1090 * It is a fatal error if message handle does not refer to a request
1091 * message
1092 */
1093 if (msg->msg.type < PSA_IPC_CALL) {
1094 tfm_core_panic();
1095 }
1096
1097 /*
1098 * It is a fatal error if invec_idx is equal to or greater than
1099 * PSA_MAX_IOVEC
1100 */
1101 if (invec_idx >= PSA_MAX_IOVEC) {
1102 tfm_core_panic();
1103 }
1104
1105 /* There was no remaining data in this input vector */
1106 if (msg->msg.in_size[invec_idx] == 0) {
1107 return 0;
1108 }
1109
1110 /*
1111 * If num_bytes is greater than the remaining size of the input vector then
1112 * the remaining size of the input vector is used.
1113 */
1114 if (num_bytes > msg->msg.in_size[invec_idx]) {
1115 num_bytes = msg->msg.in_size[invec_idx];
1116 }
1117
1118 /* There maybe some remaining data */
1119 msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base +
1120 num_bytes;
1121 msg->msg.in_size[invec_idx] -= num_bytes;
1122
1123 return num_bytes;
1124}
1125
1126void tfm_spm_psa_write(uint32_t *args)
1127{
1128 psa_handle_t msg_handle;
1129 uint32_t outvec_idx;
1130 void *buffer = NULL;
1131 size_t num_bytes;
1132 struct tfm_msg_body_t *msg = NULL;
1133 uint32_t privileged;
1134 struct spm_partition_desc_t *partition = NULL;
1135
1136 TFM_CORE_ASSERT(args != NULL);
1137 msg_handle = (psa_handle_t)args[0];
1138 outvec_idx = args[1];
1139 buffer = (void *)args[2];
1140 num_bytes = (size_t)args[3];
1141
1142 /* It is a fatal error if message handle is invalid */
1143 msg = tfm_spm_get_msg_from_handle(msg_handle);
1144 if (!msg) {
1145 tfm_core_panic();
1146 }
1147
1148 partition = msg->service->partition;
1149 privileged = tfm_spm_partition_get_privileged_mode(
1150 partition->static_data->partition_flags);
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 outvec_idx is equal to or greater than
1162 * PSA_MAX_IOVEC
1163 */
1164 if (outvec_idx >= PSA_MAX_IOVEC) {
1165 tfm_core_panic();
1166 }
1167
1168 /*
1169 * It is a fatal error if the call attempts to write data past the end of
1170 * the client output vector
1171 */
1172 if (num_bytes > msg->msg.out_size[outvec_idx] -
1173 msg->outvec[outvec_idx].len) {
1174 tfm_core_panic();
1175 }
1176
1177 /*
1178 * Copy the service buffer to client outvecs. It is a fatal error
1179 * if the memory reference for buffer is invalid or not readable.
1180 */
1181 if (tfm_memory_check(buffer, num_bytes, false,
1182 TFM_MEMORY_ACCESS_RO, privileged) != IPC_SUCCESS) {
1183 tfm_core_panic();
1184 }
1185
1186 tfm_core_util_memcpy((char *)msg->outvec[outvec_idx].base +
1187 msg->outvec[outvec_idx].len, buffer, num_bytes);
1188
1189 /* Update the write number */
1190 msg->outvec[outvec_idx].len += num_bytes;
1191}
1192
1193static void update_caller_outvec_len(struct tfm_msg_body_t *msg)
1194{
1195 uint32_t i;
1196
1197 /*
1198 * FixeMe: abstract these part into dedicated functions to avoid
1199 * accessing thread context in psa layer
1200 */
1201 /* If it is a NS request via RPC, the owner of this message is not set */
1202 if (!is_tfm_rpc_msg(msg)) {
1203 TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK);
1204 }
1205
1206 for (i = 0; i < PSA_MAX_IOVEC; i++) {
1207 if (msg->msg.out_size[i] == 0) {
1208 continue;
1209 }
1210
1211 TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
1212
1213 msg->caller_outvec[i].len = msg->outvec[i].len;
1214 }
1215}
1216
1217void tfm_spm_psa_reply(uint32_t *args)
1218{
1219 psa_handle_t msg_handle;
1220 psa_status_t status;
1221 struct tfm_spm_service_t *service = NULL;
1222 struct tfm_msg_body_t *msg = NULL;
1223 int32_t ret = PSA_SUCCESS;
Ken Liu505b1702020-05-29 13:19:58 +08001224 struct tfm_conn_handle_t *conn_handle;
Mingyang Sund44522a2020-01-16 16:48:37 +08001225
1226 TFM_CORE_ASSERT(args != NULL);
1227 msg_handle = (psa_handle_t)args[0];
1228 status = (psa_status_t)args[1];
1229
1230 /* It is a fatal error if message handle is invalid */
1231 msg = tfm_spm_get_msg_from_handle(msg_handle);
1232 if (!msg) {
1233 tfm_core_panic();
1234 }
1235
1236 /*
1237 * RoT Service information is needed in this function, stored it in message
1238 * body structure. Only two parameters are passed in this function: handle
1239 * and status, so it is useful and simply to do like this.
1240 */
1241 service = msg->service;
1242 if (!service) {
1243 tfm_core_panic();
1244 }
1245
1246 /*
1247 * Three type of message are passed in this function: CONNECTION, REQUEST,
1248 * DISCONNECTION. It needs to process differently for each type.
1249 */
Ken Liu505b1702020-05-29 13:19:58 +08001250 conn_handle = tfm_spm_to_handle_instance(msg_handle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001251 switch (msg->msg.type) {
1252 case PSA_IPC_CONNECT:
1253 /*
1254 * Reply to PSA_IPC_CONNECT message. Connect handle is returned if the
1255 * input status is PSA_SUCCESS. Others return values are based on the
1256 * input status.
1257 */
1258 if (status == PSA_SUCCESS) {
Ken Liu505b1702020-05-29 13:19:58 +08001259 ret = msg_handle;
Mingyang Sund44522a2020-01-16 16:48:37 +08001260 } else if (status == PSA_ERROR_CONNECTION_REFUSED) {
1261 /* Refuse the client connection, indicating a permanent error. */
Ken Liu505b1702020-05-29 13:19:58 +08001262 tfm_spm_free_conn_handle(service, conn_handle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001263 ret = PSA_ERROR_CONNECTION_REFUSED;
1264 } else if (status == PSA_ERROR_CONNECTION_BUSY) {
1265 /* Fail the client connection, indicating a transient error. */
1266 ret = PSA_ERROR_CONNECTION_BUSY;
1267 } else {
1268 tfm_core_panic();
1269 }
1270 break;
1271 case PSA_IPC_DISCONNECT:
1272 /* Service handle is not used anymore */
Ken Liu505b1702020-05-29 13:19:58 +08001273 tfm_spm_free_conn_handle(service, conn_handle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001274
1275 /*
1276 * If the message type is PSA_IPC_DISCONNECT, then the status code is
1277 * ignored
1278 */
1279 break;
1280 default:
1281 if (msg->msg.type >= PSA_IPC_CALL) {
1282 /* Reply to a request message. Return values are based on status */
1283 ret = status;
1284 /*
1285 * The total number of bytes written to a single parameter must be
1286 * reported to the client by updating the len member of the
1287 * psa_outvec structure for the parameter before returning from
1288 * psa_call().
1289 */
1290 update_caller_outvec_len(msg);
1291 } else {
1292 tfm_core_panic();
1293 }
1294 }
1295
1296 if (ret == PSA_ERROR_PROGRAMMER_ERROR) {
1297 /*
1298 * If the source of the programmer error is a Secure Partition, the SPM
1299 * must panic the Secure Partition in response to a PROGRAMMER ERROR.
1300 */
1301 if (TFM_CLIENT_ID_IS_NS(msg->msg.client_id)) {
Ken Liu505b1702020-05-29 13:19:58 +08001302 conn_handle->status = TFM_HANDLE_STATUS_CONNECT_ERROR;
Mingyang Sund44522a2020-01-16 16:48:37 +08001303 } else {
1304 tfm_core_panic();
1305 }
1306 } else {
Ken Liu505b1702020-05-29 13:19:58 +08001307 conn_handle->status = TFM_HANDLE_STATUS_IDLE;
Mingyang Sund44522a2020-01-16 16:48:37 +08001308 }
1309
1310 if (is_tfm_rpc_msg(msg)) {
1311 tfm_rpc_client_call_reply(msg, ret);
1312 } else {
1313 tfm_event_wake(&msg->ack_evnt, ret);
1314 }
1315}
1316
1317/**
1318 * \brief notify the partition with the signal.
1319 *
1320 * \param[in] partition_id The ID of the partition to be notified.
1321 * \param[in] signal The signal that the partition is to be notified
1322 * with.
1323 *
1324 * \retval void Success.
1325 * \retval "Does not return" If partition_id is invalid.
1326 */
1327static void notify_with_signal(int32_t partition_id, psa_signal_t signal)
1328{
1329 struct spm_partition_desc_t *partition = NULL;
1330
1331 /*
1332 * The value of partition_id must be greater than zero as the target of
1333 * notification must be a Secure Partition, providing a Non-secure
1334 * Partition ID is a fatal error.
1335 */
1336 if (!TFM_CLIENT_ID_IS_S(partition_id)) {
1337 tfm_core_panic();
1338 }
1339
1340 /*
1341 * It is a fatal error if partition_id does not correspond to a Secure
1342 * Partition.
1343 */
1344 partition = tfm_spm_get_partition_by_id(partition_id);
1345 if (!partition) {
1346 tfm_core_panic();
1347 }
1348
1349 partition->runtime_data.signals |= signal;
1350
1351 /*
1352 * The target partition may be blocked with waiting for signals after
1353 * called psa_wait(). Set the return value with the available signals
1354 * before wake it up with tfm_event_signal().
1355 */
1356 tfm_event_wake(&partition->runtime_data.signal_evnt,
1357 partition->runtime_data.signals &
1358 partition->runtime_data.signal_mask);
1359}
1360
1361void tfm_spm_psa_notify(uint32_t *args)
1362{
1363 int32_t partition_id;
1364
1365 TFM_CORE_ASSERT(args != NULL);
1366 partition_id = (int32_t)args[0];
1367
1368 notify_with_signal(partition_id, PSA_DOORBELL);
1369}
1370
1371/**
1372 * \brief assert signal for a given IRQ line.
1373 *
1374 * \param[in] partition_id The ID of the partition which handles this IRQ
1375 * \param[in] signal The signal associated with this IRQ
1376 * \param[in] irq_line The number of the IRQ line
1377 *
1378 * \retval void Success.
1379 * \retval "Does not return" Partition ID is invalid
1380 */
1381void tfm_irq_handler(uint32_t partition_id, psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +01001382 IRQn_Type irq_line)
Mingyang Sund44522a2020-01-16 16:48:37 +08001383{
1384 tfm_spm_hal_disable_irq(irq_line);
1385 notify_with_signal(partition_id, signal);
1386}
1387
1388void tfm_spm_psa_clear(void)
1389{
1390 struct spm_partition_desc_t *partition = NULL;
1391
1392 partition = tfm_spm_get_running_partition();
1393 if (!partition) {
1394 tfm_core_panic();
1395 }
1396
1397 /*
1398 * It is a fatal error if the Secure Partition's doorbell signal is not
1399 * currently asserted.
1400 */
1401 if ((partition->runtime_data.signals & PSA_DOORBELL) == 0) {
1402 tfm_core_panic();
1403 }
1404 partition->runtime_data.signals &= ~PSA_DOORBELL;
1405}
1406
1407void tfm_spm_psa_panic(void)
1408{
1409 /*
1410 * PSA FF recommends that the SPM causes the system to restart when a secure
1411 * partition panics.
1412 */
1413 tfm_spm_hal_system_reset();
1414}
1415
1416/**
1417 * \brief Return the IRQ line number associated with a signal
1418 *
1419 * \param[in] partition_id The ID of the partition in which we look for
1420 * the signal.
1421 * \param[in] signal The signal we do the query for.
1422 * \param[out] irq_line The irq line associated with signal
1423 *
1424 * \retval IPC_SUCCESS Execution successful, irq_line contains a valid
1425 * value.
1426 * \retval IPC_ERROR_GENERIC There was an error finding the IRQ line for the
1427 * signal. irq_line is unchanged.
1428 */
1429static int32_t get_irq_line_for_signal(int32_t partition_id,
1430 psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +01001431 IRQn_Type *irq_line)
Mingyang Sund44522a2020-01-16 16:48:37 +08001432{
1433 size_t i;
1434
1435 for (i = 0; i < tfm_core_irq_signals_count; ++i) {
1436 if (tfm_core_irq_signals[i].partition_id == partition_id &&
1437 tfm_core_irq_signals[i].signal_value == signal) {
1438 *irq_line = tfm_core_irq_signals[i].irq_line;
1439 return IPC_SUCCESS;
1440 }
1441 }
1442 return IPC_ERROR_GENERIC;
1443}
1444
1445void tfm_spm_psa_eoi(uint32_t *args)
1446{
1447 psa_signal_t irq_signal;
TTornblomfaf74f52020-03-04 17:56:27 +01001448 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001449 int32_t ret;
1450 struct spm_partition_desc_t *partition = NULL;
1451
1452 TFM_CORE_ASSERT(args != NULL);
1453 irq_signal = (psa_signal_t)args[0];
1454
1455 /* It is a fatal error if passed signal indicates more than one signals. */
1456 if (!tfm_is_one_bit_set(irq_signal)) {
1457 tfm_core_panic();
1458 }
1459
1460 partition = tfm_spm_get_running_partition();
1461 if (!partition) {
1462 tfm_core_panic();
1463 }
1464
1465 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1466 irq_signal, &irq_line);
1467 /* It is a fatal error if passed signal is not an interrupt signal. */
1468 if (ret != IPC_SUCCESS) {
1469 tfm_core_panic();
1470 }
1471
1472 /* It is a fatal error if passed signal is not currently asserted */
1473 if ((partition->runtime_data.signals & irq_signal) == 0) {
1474 tfm_core_panic();
1475 }
1476
1477 partition->runtime_data.signals &= ~irq_signal;
1478
1479 tfm_spm_hal_clear_pending_irq(irq_line);
1480 tfm_spm_hal_enable_irq(irq_line);
1481}
1482
1483void tfm_spm_enable_irq(uint32_t *args)
1484{
1485 struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args;
1486 psa_signal_t irq_signal = svc_ctx->r0;
TTornblomfaf74f52020-03-04 17:56:27 +01001487 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001488 int32_t ret;
1489 struct spm_partition_desc_t *partition = NULL;
1490
1491 /* It is a fatal error if passed signal indicates more than one signals. */
1492 if (!tfm_is_one_bit_set(irq_signal)) {
1493 tfm_core_panic();
1494 }
1495
1496 partition = tfm_spm_get_running_partition();
1497 if (!partition) {
1498 tfm_core_panic();
1499 }
1500
1501 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1502 irq_signal, &irq_line);
1503 /* It is a fatal error if passed signal is not an interrupt signal. */
1504 if (ret != IPC_SUCCESS) {
1505 tfm_core_panic();
1506 }
1507
1508 tfm_spm_hal_enable_irq(irq_line);
1509}
1510
1511void tfm_spm_disable_irq(uint32_t *args)
1512{
1513 struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args;
1514 psa_signal_t irq_signal = svc_ctx->r0;
TTornblomfaf74f52020-03-04 17:56:27 +01001515 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001516 int32_t ret;
1517 struct spm_partition_desc_t *partition = NULL;
1518
1519 /* It is a fatal error if passed signal indicates more than one signals. */
1520 if (!tfm_is_one_bit_set(irq_signal)) {
1521 tfm_core_panic();
1522 }
1523
1524 partition = tfm_spm_get_running_partition();
1525 if (!partition) {
1526 tfm_core_panic();
1527 }
1528
1529 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1530 irq_signal, &irq_line);
1531 /* It is a fatal error if passed signal is not an interrupt signal. */
1532 if (ret != IPC_SUCCESS) {
1533 tfm_core_panic();
1534 }
1535
1536 tfm_spm_hal_disable_irq(irq_line);
1537}
1538
1539void tfm_spm_validate_caller(struct spm_partition_desc_t *p_cur_sp,
1540 uint32_t *p_ctx, uint32_t exc_return,
1541 bool ns_caller)
1542{
1543 uintptr_t stacked_ctx_pos;
1544
1545 if (ns_caller) {
1546 /*
1547 * The background IRQ can't be supported, since if SP is executing,
1548 * the preempted context of SP can be different with the one who
1549 * preempts veneer.
1550 */
1551 if (p_cur_sp->static_data->partition_id != TFM_SP_NON_SECURE_ID) {
1552 tfm_core_panic();
1553 }
1554
1555 /*
1556 * It is non-secure caller, check if veneer stack contains
1557 * multiple contexts.
1558 */
1559 stacked_ctx_pos = (uintptr_t)p_ctx +
1560 sizeof(struct tfm_state_context_t) +
1561 TFM_VENEER_STACK_GUARD_SIZE;
1562
1563 if (is_stack_alloc_fp_space(exc_return)) {
1564#if defined (__FPU_USED) && (__FPU_USED == 1U)
1565 if (FPU->FPCCR & FPU_FPCCR_TS_Msk) {
1566 stacked_ctx_pos += TFM_ADDTIONAL_FP_CONTEXT_WORDS *
1567 sizeof(uint32_t);
1568 }
1569#endif
1570 stacked_ctx_pos += TFM_BASIC_FP_CONTEXT_WORDS * sizeof(uint32_t);
1571 }
1572
1573 if (stacked_ctx_pos != p_cur_sp->runtime_data.sp_thrd.stk_top) {
1574 tfm_core_panic();
1575 }
1576 } else if (p_cur_sp->static_data->partition_id <= 0) {
1577 tfm_core_panic();
1578 }
1579}
Summer Qin830c5542020-02-14 13:44:20 +08001580
1581void tfm_spm_request_handler(const struct tfm_state_context_t *svc_ctx)
1582{
1583 uint32_t *res_ptr = (uint32_t *)&svc_ctx->r0;
1584 uint32_t running_partition_flags = 0;
1585 const struct spm_partition_desc_t *partition = NULL;
1586
1587 /* Check permissions on request type basis */
1588
1589 switch (svc_ctx->r0) {
1590 case TFM_SPM_REQUEST_RESET_VOTE:
1591 partition = tfm_spm_get_running_partition();
1592 if (!partition) {
1593 tfm_core_panic();
1594 }
1595 running_partition_flags = partition->static_data->partition_flags;
1596
1597 /* Currently only PSA Root of Trust services are allowed to make Reset
1598 * vote request
1599 */
1600 if ((running_partition_flags & SPM_PART_FLAG_PSA_ROT) == 0) {
1601 *res_ptr = (uint32_t)TFM_ERROR_GENERIC;
1602 }
1603
1604 /* FixMe: this is a placeholder for checks to be performed before
1605 * allowing execution of reset
1606 */
1607 *res_ptr = (uint32_t)TFM_SUCCESS;
1608
1609 break;
1610 default:
1611 *res_ptr = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
1612 }
1613}
Mingyang Sunbd7ceb52020-06-11 16:53:03 +08001614
1615enum spm_err_t tfm_spm_db_init(void)
1616{
1617 uint32_t i;
1618
1619 /* This function initialises partition db */
1620
1621 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
1622 g_spm_partition_db.partitions[i].static_data = &static_data_list[i];
1623 g_spm_partition_db.partitions[i].platform_data_list =
1624 platform_data_list_list[i];
1625 g_spm_partition_db.partitions[i].memory_data = &memory_data_list[i];
1626 }
1627 g_spm_partition_db.is_init = 1;
1628
1629 return SPM_ERR_OK;
1630}