blob: deb7d30705766fb1fad2373255ff016ac7f731f6 [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"
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"
Ken Liu1f345b02020-05-30 21:11:05 +080036#include "tfm/tfm_spm_services_api.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{
329 uint32_t spid;
330
Mingyang Sunf3d29892019-07-10 17:50:23 +0800331 spid = tfm_spm_partition_get_running_partition_id();
Edison Ai764d41f2018-09-21 15:56:36 +0800332
333 return tfm_spm_get_partition_by_id(spid);
334}
335
336int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530337 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800338{
Ken Liuf250b8b2019-12-27 16:31:24 +0800339 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800340
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530341 switch (service->service_db->version_policy) {
Edison Ai764d41f2018-09-21 15:56:36 +0800342 case TFM_VERSION_POLICY_RELAXED:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530343 if (version > service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800344 return IPC_ERROR_VERSION;
345 }
346 break;
347 case TFM_VERSION_POLICY_STRICT:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530348 if (version != service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800349 return IPC_ERROR_VERSION;
350 }
351 break;
352 default:
353 return IPC_ERROR_VERSION;
354 }
355 return IPC_SUCCESS;
356}
357
Edison Aie728fbf2019-11-13 09:37:12 +0800358int32_t tfm_spm_check_authorization(uint32_t sid,
359 struct tfm_spm_service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800360 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800361{
362 struct spm_partition_desc_t *partition = NULL;
363 int32_t i;
364
Ken Liuf250b8b2019-12-27 16:31:24 +0800365 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800366
367 if (ns_caller) {
368 if (!service->service_db->non_secure_client) {
369 return IPC_ERROR_GENERIC;
370 }
371 } else {
372 partition = tfm_spm_get_running_partition();
373 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800374 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800375 }
376
377 for (i = 0; i < partition->static_data->dependencies_num; i++) {
378 if (partition->static_data->p_dependencies[i] == sid) {
379 break;
380 }
381 }
382
383 if (i == partition->static_data->dependencies_num) {
384 return IPC_ERROR_GENERIC;
385 }
386 }
387 return IPC_SUCCESS;
388}
389
Edison Ai764d41f2018-09-21 15:56:36 +0800390/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800391
392/**
393 * \brief Get message context by message handle.
394 *
395 * \param[in] msg_handle Message handle which is a reference generated
396 * by the SPM to a specific message.
397 *
398 * \return The message body context pointer
399 * \ref tfm_msg_body_t structures
400 */
401static struct tfm_msg_body_t *
402 tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800403{
404 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200405 * The message handler passed by the caller is considered invalid in the
406 * following cases:
407 * 1. Not a valid message handle. (The address of a message is not the
408 * address of a possible handle from the pool
409 * 2. Handle not belongs to the caller partition (The handle is either
410 * unused, or owned by anither partition)
411 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800412 */
Ken Liu505b1702020-05-29 13:19:58 +0800413 struct tfm_msg_body_t *p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800414 uint32_t partition_id;
Ken Liu505b1702020-05-29 13:19:58 +0800415 struct tfm_conn_handle_t *p_conn_handle =
416 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200417
418 if (is_valid_chunk_data_in_pool(
Ken Liu505b1702020-05-29 13:19:58 +0800419 conn_handle_pool, (uint8_t *)p_conn_handle) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800420 return NULL;
421 }
422
Ken Liu505b1702020-05-29 13:19:58 +0800423 p_msg = &p_conn_handle->internal_msg;
424
Edison Ai764d41f2018-09-21 15:56:36 +0800425 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200426 * Check that the magic number is correct. This proves that the message
427 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800428 */
Ken Liu505b1702020-05-29 13:19:58 +0800429 if (p_msg->magic != TFM_MSG_MAGIC) {
Edison Ai764d41f2018-09-21 15:56:36 +0800430 return NULL;
431 }
432
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200433 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800434 partition_id = tfm_spm_partition_get_running_partition_id();
Ken Liu505b1702020-05-29 13:19:58 +0800435 if (partition_id != p_msg->service->partition->static_data->partition_id) {
Edison Ai764d41f2018-09-21 15:56:36 +0800436 return NULL;
437 }
438
Ken Liu505b1702020-05-29 13:19:58 +0800439 return p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800440}
441
Edison Ai97115822019-08-01 14:22:19 +0800442struct tfm_msg_body_t *
Summer Qin630c76b2020-05-20 10:32:58 +0800443 tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800444{
Summer Qin630c76b2020-05-20 10:32:58 +0800445 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai97115822019-08-01 14:22:19 +0800446
Summer Qin630c76b2020-05-20 10:32:58 +0800447 return &(conn_handle->internal_msg);
Edison Ai97115822019-08-01 14:22:19 +0800448}
449
450void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
451 struct tfm_spm_service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800452 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800453 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800454 psa_invec *invec, size_t in_len,
455 psa_outvec *outvec, size_t out_len,
456 psa_outvec *caller_outvec)
457{
Edison Ai764d41f2018-09-21 15:56:36 +0800458 uint32_t i;
Ken Liu505b1702020-05-29 13:19:58 +0800459 struct tfm_conn_handle_t *conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800460
Ken Liuf250b8b2019-12-27 16:31:24 +0800461 TFM_CORE_ASSERT(msg);
462 TFM_CORE_ASSERT(service);
463 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
464 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
465 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
466 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
467 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800468
Edison Ai764d41f2018-09-21 15:56:36 +0800469 /* Clear message buffer before using it */
Mingyang Sun94b1b412019-09-20 15:11:14 +0800470 tfm_core_util_memset(msg, 0, sizeof(struct tfm_msg_body_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800471
Ken Liu35f89392019-03-14 14:51:05 +0800472 tfm_event_init(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800473 msg->magic = TFM_MSG_MAGIC;
474 msg->service = service;
Edison Ai764d41f2018-09-21 15:56:36 +0800475 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800476 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800477
478 /* Copy contents */
479 msg->msg.type = type;
480
481 for (i = 0; i < in_len; i++) {
482 msg->msg.in_size[i] = invec[i].len;
483 msg->invec[i].base = invec[i].base;
484 }
485
486 for (i = 0; i < out_len; i++) {
487 msg->msg.out_size[i] = outvec[i].len;
488 msg->outvec[i].base = outvec[i].base;
489 /* Out len is used to record the writed number, set 0 here again */
490 msg->outvec[i].len = 0;
491 }
492
Ken Liu505b1702020-05-29 13:19:58 +0800493 /* Use the user connect handle as the message handle */
494 msg->msg.handle = handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800495
Ken Liu505b1702020-05-29 13:19:58 +0800496 conn_handle = tfm_spm_to_handle_instance(handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800497 /* For connected handle, set rhandle to every message */
Ken Liu505b1702020-05-29 13:19:58 +0800498 if (conn_handle) {
499 msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800500 }
David Hu46603dd2019-12-11 18:05:16 +0800501
502 /* Set the private data of NSPE client caller in multi-core topology */
503 if (TFM_CLIENT_ID_IS_NS(client_id)) {
504 tfm_rpc_set_caller_data(msg, client_id);
505 }
Edison Ai764d41f2018-09-21 15:56:36 +0800506}
507
508int32_t tfm_spm_send_event(struct tfm_spm_service_t *service,
509 struct tfm_msg_body_t *msg)
510{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800511 struct spm_partition_runtime_data_t *p_runtime_data =
512 &service->partition->runtime_data;
513
Ken Liuf250b8b2019-12-27 16:31:24 +0800514 TFM_CORE_ASSERT(service);
515 TFM_CORE_ASSERT(msg);
Edison Ai764d41f2018-09-21 15:56:36 +0800516
517 /* Enqueue message to service message queue */
518 if (tfm_msg_enqueue(&service->msg_queue, msg) != IPC_SUCCESS) {
519 return IPC_ERROR_GENERIC;
520 }
521
522 /* Messages put. Update signals */
Summer Qine578c5b2019-08-16 16:42:16 +0800523 p_runtime_data->signals |= service->service_db->signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800524
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800525 tfm_event_wake(&p_runtime_data->signal_evnt, (p_runtime_data->signals &
526 p_runtime_data->signal_mask));
Edison Ai764d41f2018-09-21 15:56:36 +0800527
David Hufb38d562019-09-23 15:58:34 +0800528 /*
529 * If it is a NS request via RPC, it is unnecessary to block current
530 * thread.
531 */
532 if (!is_tfm_rpc_msg(msg)) {
533 tfm_event_wait(&msg->ack_evnt);
534 }
Edison Ai764d41f2018-09-21 15:56:36 +0800535
536 return IPC_SUCCESS;
537}
538
Mingyang Sund44522a2020-01-16 16:48:37 +0800539/**
540 * \brief Get bottom of stack region for a partition
541 *
542 * \param[in] partition_idx Partition index
543 *
544 * \return Stack region bottom value
545 *
546 * \note This function doesn't check if partition_idx is valid.
547 */
548static uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
Edison Ai7aff9e82019-07-11 14:56:46 +0800549{
550 return g_spm_partition_db.partitions[partition_idx].
Summer Qin423dbef2019-08-22 15:59:35 +0800551 memory_data->stack_bottom;
Edison Ai7aff9e82019-07-11 14:56:46 +0800552}
553
Mingyang Sund44522a2020-01-16 16:48:37 +0800554/**
555 * \brief Get top of stack region for a partition
556 *
557 * \param[in] partition_idx Partition index
558 *
559 * \return Stack region top value
560 *
561 * \note This function doesn't check if partition_idx is valid.
562 */
563static uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
Edison Ai7aff9e82019-07-11 14:56:46 +0800564{
Summer Qin423dbef2019-08-22 15:59:35 +0800565 return g_spm_partition_db.partitions[partition_idx].memory_data->stack_top;
Edison Ai7aff9e82019-07-11 14:56:46 +0800566}
567
Mingyang Sunf3d29892019-07-10 17:50:23 +0800568uint32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800569{
Summer Qin66f1e032020-01-06 15:40:03 +0800570 struct tfm_core_thread_t *pth = tfm_core_thrd_get_curr_thread();
Edison Ai764d41f2018-09-21 15:56:36 +0800571 struct spm_partition_desc_t *partition;
Summer Qinb5da9cc2019-08-26 15:19:45 +0800572 struct spm_partition_runtime_data_t *r_data;
Edison Ai764d41f2018-09-21 15:56:36 +0800573
Summer Qinb5da9cc2019-08-26 15:19:45 +0800574 r_data = TFM_GET_CONTAINER_PTR(pth, struct spm_partition_runtime_data_t,
575 sp_thrd);
576 partition = TFM_GET_CONTAINER_PTR(r_data, struct spm_partition_desc_t,
577 runtime_data);
Summer Qin423dbef2019-08-22 15:59:35 +0800578 return partition->static_data->partition_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800579}
580
Summer Qin66f1e032020-01-06 15:40:03 +0800581static struct tfm_core_thread_t *
Mingyang Sunf3d29892019-07-10 17:50:23 +0800582 tfm_spm_partition_get_thread_info(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800583{
Summer Qinb5da9cc2019-08-26 15:19:45 +0800584 return &g_spm_partition_db.partitions[partition_idx].runtime_data.sp_thrd;
Edison Ai764d41f2018-09-21 15:56:36 +0800585}
586
Summer Qin66f1e032020-01-06 15:40:03 +0800587static tfm_core_thrd_entry_t
Mingyang Sunf3d29892019-07-10 17:50:23 +0800588 tfm_spm_partition_get_init_func(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800589{
Summer Qin66f1e032020-01-06 15:40:03 +0800590 return (tfm_core_thrd_entry_t)(g_spm_partition_db.partitions[partition_idx].
Summer Qin423dbef2019-08-22 15:59:35 +0800591 static_data->partition_init);
Edison Ai764d41f2018-09-21 15:56:36 +0800592}
593
Mingyang Sunf3d29892019-07-10 17:50:23 +0800594static uint32_t tfm_spm_partition_get_priority(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800595{
Summer Qin423dbef2019-08-22 15:59:35 +0800596 return g_spm_partition_db.partitions[partition_idx].static_data->
Edison Ai764d41f2018-09-21 15:56:36 +0800597 partition_priority;
598}
599
Summer Qin43c185d2019-10-10 15:44:42 +0800600int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800601 enum tfm_memory_access_e access,
602 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800603{
Hugues de Valon99578562019-06-18 16:08:51 +0100604 enum tfm_status_e err;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800605
606 /* If len is zero, this indicates an empty buffer and base is ignored */
607 if (len == 0) {
608 return IPC_SUCCESS;
609 }
610
611 if (!buffer) {
612 return IPC_ERROR_BAD_PARAMETERS;
613 }
614
615 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
616 return IPC_ERROR_MEMORY_CHECK;
617 }
618
Summer Qin424d4db2019-03-25 14:09:51 +0800619 if (access == TFM_MEMORY_ACCESS_RW) {
Summer Qineb537e52019-03-29 09:57:10 +0800620 err = tfm_core_has_write_access_to_region(buffer, len, ns_caller,
621 privileged);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800622 } else {
Summer Qineb537e52019-03-29 09:57:10 +0800623 err = tfm_core_has_read_access_to_region(buffer, len, ns_caller,
624 privileged);
Summer Qin424d4db2019-03-25 14:09:51 +0800625 }
Summer Qin0fc3f592019-04-11 16:00:10 +0800626 if (err == TFM_SUCCESS) {
Summer Qin424d4db2019-03-25 14:09:51 +0800627 return IPC_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800628 }
629
630 return IPC_ERROR_MEMORY_CHECK;
631}
632
Ken Liuce2692d2020-02-11 12:39:36 +0800633uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800634{
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800635 uint32_t i, j, num;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800636 struct spm_partition_desc_t *partition;
Summer Qin66f1e032020-01-06 15:40:03 +0800637 struct tfm_core_thread_t *pth, *p_ns_entry_thread = NULL;
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100638 const struct tfm_spm_partition_platform_data_t **platform_data_p;
Edison Ai764d41f2018-09-21 15:56:36 +0800639
640 tfm_pool_init(conn_handle_pool,
641 POOL_BUFFER_SIZE(conn_handle_pool),
642 sizeof(struct tfm_conn_handle_t),
643 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800644
645 /* Init partition first for it will be used when init service */
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200646 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800647 partition = &g_spm_partition_db.partitions[i];
Edison Aif0501702019-10-11 14:36:42 +0800648
649 /* Check if the PSA framework version matches. */
650 if (partition->static_data->psa_framework_version !=
651 PSA_FRAMEWORK_VERSION) {
652 ERROR_MSG("Warning: PSA Framework Verison is not matched!");
653 continue;
654 }
655
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100656 platform_data_p = partition->platform_data_list;
657 if (platform_data_p != NULL) {
658 while ((*platform_data_p) != NULL) {
Edison Ai6be3df12020-02-14 22:14:33 +0800659 if (tfm_spm_hal_configure_default_isolation(i,
660 *platform_data_p) != TFM_PLAT_ERR_SUCCESS) {
661 tfm_core_panic();
662 }
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100663 ++platform_data_p;
664 }
665 }
666
Edison Ai764d41f2018-09-21 15:56:36 +0800667 if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
668 continue;
669 }
Ken Liu35f89392019-03-14 14:51:05 +0800670
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800671 /* Add PSA_DOORBELL signal to assigned_signals */
672 partition->runtime_data.assigned_signals |= PSA_DOORBELL;
673
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800674 /* TODO: This can be optimized by generating the assigned signal
675 * in code generation time.
676 */
677 for (j = 0; j < tfm_core_irq_signals_count; ++j) {
678 if (tfm_core_irq_signals[j].partition_id ==
679 partition->static_data->partition_id) {
680 partition->runtime_data.assigned_signals |=
681 tfm_core_irq_signals[j].signal_value;
682 }
683 }
684
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800685 tfm_event_init(&partition->runtime_data.signal_evnt);
686 tfm_list_init(&partition->runtime_data.service_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800687
Mingyang Sunf3d29892019-07-10 17:50:23 +0800688 pth = tfm_spm_partition_get_thread_info(i);
Edison Ai764d41f2018-09-21 15:56:36 +0800689 if (!pth) {
Edison Ai9059ea02019-11-28 13:46:14 +0800690 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800691 }
692
Summer Qin66f1e032020-01-06 15:40:03 +0800693 tfm_core_thrd_init(pth,
694 tfm_spm_partition_get_init_func(i),
695 NULL,
696 (uintptr_t)tfm_spm_partition_get_stack_top(i),
697 (uintptr_t)tfm_spm_partition_get_stack_bottom(i));
Edison Ai788bae22019-02-18 17:38:59 +0800698
Mingyang Sunf3d29892019-07-10 17:50:23 +0800699 pth->prior = tfm_spm_partition_get_priority(i);
Edison Ai764d41f2018-09-21 15:56:36 +0800700
Ken Liu490281d2019-12-30 15:55:26 +0800701 if (partition->static_data->partition_id == TFM_SP_NON_SECURE_ID) {
702 p_ns_entry_thread = pth;
Ken Liu5248af22019-12-29 12:47:13 +0800703 pth->param = (void *)tfm_spm_hal_get_ns_entry_point();
Ken Liu490281d2019-12-30 15:55:26 +0800704 }
705
Edison Ai764d41f2018-09-21 15:56:36 +0800706 /* Kick off */
Summer Qin66f1e032020-01-06 15:40:03 +0800707 if (tfm_core_thrd_start(pth) != THRD_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800708 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800709 }
710 }
711
712 /* Init Service */
Summer Qind99509f2019-08-02 17:36:58 +0800713 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
Edison Ai764d41f2018-09-21 15:56:36 +0800714 for (i = 0; i < num; i++) {
Summer Qine578c5b2019-08-16 16:42:16 +0800715 service[i].service_db = &service_db[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800716 partition =
Summer Qine578c5b2019-08-16 16:42:16 +0800717 tfm_spm_get_partition_by_id(service[i].service_db->partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800718 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800719 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800720 }
Summer Qind99509f2019-08-02 17:36:58 +0800721 service[i].partition = partition;
Jaykumar Pitambarbhai Patel0c7a0382020-01-09 15:25:58 +0530722 partition->runtime_data.assigned_signals |= service[i].service_db->signal;
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800723
Summer Qind99509f2019-08-02 17:36:58 +0800724 tfm_list_init(&service[i].handle_list);
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800725 tfm_list_add_tail(&partition->runtime_data.service_list,
Summer Qind99509f2019-08-02 17:36:58 +0800726 &service[i].list);
Edison Ai764d41f2018-09-21 15:56:36 +0800727 }
728
Ken Liu483f5da2019-04-24 10:45:21 +0800729 /*
730 * All threads initialized, start the scheduler.
731 *
732 * NOTE:
Ken Liu490281d2019-12-30 15:55:26 +0800733 * It is worthy to give the thread object to scheduler if the background
734 * context belongs to one of the threads. Here the background thread is the
735 * initialization thread who calls SPM SVC, which re-uses the non-secure
736 * entry thread's stack. After SPM initialization is done, this stack is
737 * cleaned up and the background context is never going to return. Tell
738 * the scheduler that the current thread is non-secure entry thread.
Ken Liu483f5da2019-04-24 10:45:21 +0800739 */
Summer Qin66f1e032020-01-06 15:40:03 +0800740 tfm_core_thrd_start_scheduler(p_ns_entry_thread);
Ken Liuce2692d2020-02-11 12:39:36 +0800741
Summer Qind2ad7e72020-01-06 18:16:35 +0800742 return p_ns_entry_thread->arch_ctx.lr;
Edison Ai764d41f2018-09-21 15:56:36 +0800743}
Ken Liu2d175172019-03-21 17:08:41 +0800744
Summer Qind2ad7e72020-01-06 18:16:35 +0800745void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx)
Ken Liu2d175172019-03-21 17:08:41 +0800746{
747#if TFM_LVL == 2
748 struct spm_partition_desc_t *p_next_partition;
Summer Qinb5da9cc2019-08-26 15:19:45 +0800749 struct spm_partition_runtime_data_t *r_data;
Ken Liu2d175172019-03-21 17:08:41 +0800750 uint32_t is_privileged;
751#endif
Summer Qin66f1e032020-01-06 15:40:03 +0800752 struct tfm_core_thread_t *pth_next = tfm_core_thrd_get_next_thread();
753 struct tfm_core_thread_t *pth_curr = tfm_core_thrd_get_curr_thread();
Ken Liu2d175172019-03-21 17:08:41 +0800754
Mate Toth-Pal32b2ccd2019-04-26 10:00:16 +0200755 if (pth_next != NULL && pth_curr != pth_next) {
Ken Liu2d175172019-03-21 17:08:41 +0800756#if TFM_LVL == 2
Summer Qinb5da9cc2019-08-26 15:19:45 +0800757 r_data = TFM_GET_CONTAINER_PTR(pth_next,
758 struct spm_partition_runtime_data_t,
759 sp_thrd);
760 p_next_partition = TFM_GET_CONTAINER_PTR(r_data,
Ken Liu2d175172019-03-21 17:08:41 +0800761 struct spm_partition_desc_t,
Summer Qinb5da9cc2019-08-26 15:19:45 +0800762 runtime_data);
Ken Liu2d175172019-03-21 17:08:41 +0800763
Summer Qin423dbef2019-08-22 15:59:35 +0800764 if (p_next_partition->static_data->partition_flags &
Ken Liu2d175172019-03-21 17:08:41 +0800765 SPM_PART_FLAG_PSA_ROT) {
766 is_privileged = TFM_PARTITION_PRIVILEGED_MODE;
767 } else {
768 is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE;
769 }
770
771 tfm_spm_partition_change_privilege(is_privileged);
772#endif
Mate Toth-Palc430b992019-05-09 21:01:14 +0200773
Summer Qind2ad7e72020-01-06 18:16:35 +0800774 tfm_core_thrd_switch_context(p_actx, pth_curr, pth_next);
Ken Liu2d175172019-03-21 17:08:41 +0800775 }
David Hufb38d562019-09-23 15:58:34 +0800776
777 /*
778 * Handle pending mailbox message from NS in multi-core topology.
779 * Empty operation on single Armv8-M platform.
780 */
781 tfm_rpc_client_call_handler();
Ken Liu2d175172019-03-21 17:08:41 +0800782}
Mingyang Sund44522a2020-01-16 16:48:37 +0800783
784/*********************** SPM functions for PSA Client APIs *******************/
785
786uint32_t tfm_spm_psa_framework_version(void)
787{
788 return tfm_spm_client_psa_framework_version();
789}
790
791uint32_t tfm_spm_psa_version(uint32_t *args, bool ns_caller)
792{
793 uint32_t sid;
794
795 TFM_CORE_ASSERT(args != NULL);
796 sid = (uint32_t)args[0];
797
798 return tfm_spm_client_psa_version(sid, ns_caller);
799}
800
801psa_status_t tfm_spm_psa_connect(uint32_t *args, bool ns_caller)
802{
803 uint32_t sid;
804 uint32_t version;
805
806 TFM_CORE_ASSERT(args != NULL);
807 sid = (uint32_t)args[0];
808 version = (uint32_t)args[1];
809
810 return tfm_spm_client_psa_connect(sid, version, ns_caller);
811}
812
813psa_status_t tfm_spm_psa_call(uint32_t *args, bool ns_caller, uint32_t lr)
814{
815 psa_handle_t handle;
816 psa_invec *inptr;
817 psa_outvec *outptr;
818 size_t in_num, out_num;
819 struct spm_partition_desc_t *partition = NULL;
820 uint32_t privileged;
821 int32_t type;
822 struct tfm_control_parameter_t ctrl_param;
823
824 TFM_CORE_ASSERT(args != NULL);
825 handle = (psa_handle_t)args[0];
826
827 partition = tfm_spm_get_running_partition();
828 if (!partition) {
829 tfm_core_panic();
830 }
831 privileged = tfm_spm_partition_get_privileged_mode(
832 partition->static_data->partition_flags);
833
834 /*
835 * Read parameters from the arguments. It is a fatal error if the
836 * memory reference for buffer is invalid or not readable.
837 */
838 if (tfm_memory_check((const void *)args[1],
839 sizeof(struct tfm_control_parameter_t), ns_caller,
840 TFM_MEMORY_ACCESS_RW, privileged) != IPC_SUCCESS) {
841 tfm_core_panic();
842 }
843
844 tfm_core_util_memcpy(&ctrl_param,
845 (const void *)args[1],
846 sizeof(ctrl_param));
847
848 type = ctrl_param.type;
849 in_num = ctrl_param.in_len;
850 out_num = ctrl_param.out_len;
851 inptr = (psa_invec *)args[2];
852 outptr = (psa_outvec *)args[3];
853
854 /* The request type must be zero or positive. */
855 if (type < 0) {
856 tfm_core_panic();
857 }
858
859 return tfm_spm_client_psa_call(handle, type, inptr, in_num, outptr, out_num,
860 ns_caller, privileged);
861}
862
863void tfm_spm_psa_close(uint32_t *args, bool ns_caller)
864{
865 psa_handle_t handle;
866
867 TFM_CORE_ASSERT(args != NULL);
868 handle = args[0];
869
870 tfm_spm_client_psa_close(handle, ns_caller);
871}
872
873uint32_t tfm_spm_get_lifecycle_state(void)
874{
875 /*
876 * FixMe: return PSA_LIFECYCLE_UNKNOWN to the caller directly. It will be
877 * implemented in the future.
878 */
879 return PSA_LIFECYCLE_UNKNOWN;
880}
881
882/********************* SPM functions for PSA Service APIs ********************/
883
884psa_signal_t tfm_spm_psa_wait(uint32_t *args)
885{
886 psa_signal_t signal_mask;
887 uint32_t timeout;
888 struct spm_partition_desc_t *partition = NULL;
889
890 TFM_CORE_ASSERT(args != NULL);
891 signal_mask = (psa_signal_t)args[0];
892 timeout = args[1];
893
894 /*
895 * Timeout[30:0] are reserved for future use.
896 * SPM must ignore the value of RES.
897 */
898 timeout &= PSA_TIMEOUT_MASK;
899
900 partition = tfm_spm_get_running_partition();
901 if (!partition) {
902 tfm_core_panic();
903 }
904
905 /*
906 * It is a PROGRAMMER ERROR if the signal_mask does not include any assigned
907 * signals.
908 */
909 if ((partition->runtime_data.assigned_signals & signal_mask) == 0) {
910 tfm_core_panic();
911 }
912
913 /*
914 * Expected signals are included in signal wait mask, ignored signals
915 * should not be set and affect caller thread state. Save this mask for
916 * further checking while signals are ready to be set.
917 */
918 partition->runtime_data.signal_mask = signal_mask;
919
920 /*
921 * tfm_event_wait() blocks the caller thread if no signals are available.
922 * In this case, the return value of this function is temporary set into
923 * runtime context. After new signal(s) are available, the return value
924 * is updated with the available signal(s) and blocked thread gets to run.
925 */
926 if (timeout == PSA_BLOCK &&
927 (partition->runtime_data.signals & signal_mask) == 0) {
928 tfm_event_wait(&partition->runtime_data.signal_evnt);
929 }
930
931 return partition->runtime_data.signals & signal_mask;
932}
933
934psa_status_t tfm_spm_psa_get(uint32_t *args)
935{
936 psa_signal_t signal;
937 psa_msg_t *msg = NULL;
938 struct tfm_spm_service_t *service = NULL;
939 struct tfm_msg_body_t *tmp_msg = NULL;
940 struct spm_partition_desc_t *partition = NULL;
941 uint32_t privileged;
942
943 TFM_CORE_ASSERT(args != NULL);
944 signal = (psa_signal_t)args[0];
945 msg = (psa_msg_t *)args[1];
946
947 /*
948 * Only one message could be retrieved every time for psa_get(). It is a
949 * fatal error if the input signal has more than a signal bit set.
950 */
Ken Liu410ada52020-01-08 11:37:27 +0800951 if (!tfm_is_one_bit_set(signal)) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800952 tfm_core_panic();
953 }
954
955 partition = tfm_spm_get_running_partition();
956 if (!partition) {
957 tfm_core_panic();
958 }
959 privileged = tfm_spm_partition_get_privileged_mode(
960 partition->static_data->partition_flags);
961
962 /*
963 * Write the message to the service buffer. It is a fatal error if the
964 * input msg pointer is not a valid memory reference or not read-write.
965 */
966 if (tfm_memory_check(msg, sizeof(psa_msg_t), false, TFM_MEMORY_ACCESS_RW,
967 privileged) != IPC_SUCCESS) {
968 tfm_core_panic();
969 }
970
971 /*
972 * It is a fatal error if the caller call psa_get() when no message has
973 * been set. The caller must call this function after an RoT Service signal
974 * is returned by psa_wait().
975 */
976 if (partition->runtime_data.signals == 0) {
977 tfm_core_panic();
978 }
979
980 /*
981 * It is a fatal error if the RoT Service signal is not currently asserted.
982 */
983 if ((partition->runtime_data.signals & signal) == 0) {
984 tfm_core_panic();
985 }
986
987 /*
988 * Get RoT service by signal from partition. It is a fatal error if getting
989 * failed, which means the input signal is not correspond to an RoT service.
990 */
991 service = tfm_spm_get_service_by_signal(partition, signal);
992 if (!service) {
993 tfm_core_panic();
994 }
995
996 tmp_msg = tfm_msg_dequeue(&service->msg_queue);
997 if (!tmp_msg) {
998 return PSA_ERROR_DOES_NOT_EXIST;
999 }
1000
Ken Liu505b1702020-05-29 13:19:58 +08001001 (TFM_GET_CONTAINER_PTR(tmp_msg,
1002 struct tfm_conn_handle_t,
1003 internal_msg))->status = TFM_HANDLE_STATUS_ACTIVE;
Mingyang Sund44522a2020-01-16 16:48:37 +08001004
1005 tfm_core_util_memcpy(msg, &tmp_msg->msg, sizeof(psa_msg_t));
1006
1007 /*
1008 * There may be multiple messages for this RoT Service signal, do not clear
1009 * its mask until no remaining message.
1010 */
1011 if (tfm_msg_queue_is_empty(&service->msg_queue)) {
1012 partition->runtime_data.signals &= ~signal;
1013 }
1014
1015 return PSA_SUCCESS;
1016}
1017
1018void tfm_spm_psa_set_rhandle(uint32_t *args)
1019{
1020 psa_handle_t msg_handle;
1021 void *rhandle = NULL;
1022 struct tfm_msg_body_t *msg = NULL;
Ken Liu505b1702020-05-29 13:19:58 +08001023 struct tfm_conn_handle_t *conn_handle;
Mingyang Sund44522a2020-01-16 16:48:37 +08001024
1025 TFM_CORE_ASSERT(args != NULL);
1026 msg_handle = (psa_handle_t)args[0];
1027 rhandle = (void *)args[1];
1028
1029 /* It is a fatal error if message handle is invalid */
1030 msg = tfm_spm_get_msg_from_handle(msg_handle);
1031 if (!msg) {
1032 tfm_core_panic();
1033 }
1034
1035 msg->msg.rhandle = rhandle;
Ken Liu505b1702020-05-29 13:19:58 +08001036 conn_handle = tfm_spm_to_handle_instance(msg_handle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001037
1038 /* Store reverse handle for following client calls. */
Ken Liu505b1702020-05-29 13:19:58 +08001039 tfm_spm_set_rhandle(msg->service, conn_handle, rhandle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001040}
1041
1042size_t tfm_spm_psa_read(uint32_t *args)
1043{
1044 psa_handle_t msg_handle;
1045 uint32_t invec_idx;
1046 void *buffer = NULL;
1047 size_t num_bytes;
1048 size_t bytes;
1049 struct tfm_msg_body_t *msg = NULL;
1050 uint32_t privileged;
1051 struct spm_partition_desc_t *partition = NULL;
1052
1053 TFM_CORE_ASSERT(args != NULL);
1054 msg_handle = (psa_handle_t)args[0];
1055 invec_idx = args[1];
1056 buffer = (void *)args[2];
1057 num_bytes = (size_t)args[3];
1058
1059 /* It is a fatal error if message handle is invalid */
1060 msg = tfm_spm_get_msg_from_handle(msg_handle);
1061 if (!msg) {
1062 tfm_core_panic();
1063 }
1064
1065 partition = msg->service->partition;
1066 privileged = tfm_spm_partition_get_privileged_mode(
1067 partition->static_data->partition_flags);
1068
1069 /*
1070 * It is a fatal error if message handle does not refer to a request
1071 * message
1072 */
1073 if (msg->msg.type < PSA_IPC_CALL) {
1074 tfm_core_panic();
1075 }
1076
1077 /*
1078 * It is a fatal error if invec_idx is equal to or greater than
1079 * PSA_MAX_IOVEC
1080 */
1081 if (invec_idx >= PSA_MAX_IOVEC) {
1082 tfm_core_panic();
1083 }
1084
1085 /* There was no remaining data in this input vector */
1086 if (msg->msg.in_size[invec_idx] == 0) {
1087 return 0;
1088 }
1089
1090 /*
1091 * Copy the client data to the service buffer. It is a fatal error
1092 * if the memory reference for buffer is invalid or not read-write.
1093 */
1094 if (tfm_memory_check(buffer, num_bytes, false,
1095 TFM_MEMORY_ACCESS_RW, privileged) != IPC_SUCCESS) {
1096 tfm_core_panic();
1097 }
1098
1099 bytes = num_bytes > msg->msg.in_size[invec_idx] ?
1100 msg->msg.in_size[invec_idx] : num_bytes;
1101
1102 tfm_core_util_memcpy(buffer, msg->invec[invec_idx].base, bytes);
1103
1104 /* There maybe some remaining data */
1105 msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base + bytes;
1106 msg->msg.in_size[invec_idx] -= bytes;
1107
1108 return bytes;
1109}
1110
1111size_t tfm_spm_psa_skip(uint32_t *args)
1112{
1113 psa_handle_t msg_handle;
1114 uint32_t invec_idx;
1115 size_t num_bytes;
1116 struct tfm_msg_body_t *msg = NULL;
1117
1118 TFM_CORE_ASSERT(args != NULL);
1119 msg_handle = (psa_handle_t)args[0];
1120 invec_idx = args[1];
1121 num_bytes = (size_t)args[2];
1122
1123 /* It is a fatal error if message handle is invalid */
1124 msg = tfm_spm_get_msg_from_handle(msg_handle);
1125 if (!msg) {
1126 tfm_core_panic();
1127 }
1128
1129 /*
1130 * It is a fatal error if message handle does not refer to a request
1131 * message
1132 */
1133 if (msg->msg.type < PSA_IPC_CALL) {
1134 tfm_core_panic();
1135 }
1136
1137 /*
1138 * It is a fatal error if invec_idx is equal to or greater than
1139 * PSA_MAX_IOVEC
1140 */
1141 if (invec_idx >= PSA_MAX_IOVEC) {
1142 tfm_core_panic();
1143 }
1144
1145 /* There was no remaining data in this input vector */
1146 if (msg->msg.in_size[invec_idx] == 0) {
1147 return 0;
1148 }
1149
1150 /*
1151 * If num_bytes is greater than the remaining size of the input vector then
1152 * the remaining size of the input vector is used.
1153 */
1154 if (num_bytes > msg->msg.in_size[invec_idx]) {
1155 num_bytes = msg->msg.in_size[invec_idx];
1156 }
1157
1158 /* There maybe some remaining data */
1159 msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base +
1160 num_bytes;
1161 msg->msg.in_size[invec_idx] -= num_bytes;
1162
1163 return num_bytes;
1164}
1165
1166void tfm_spm_psa_write(uint32_t *args)
1167{
1168 psa_handle_t msg_handle;
1169 uint32_t outvec_idx;
1170 void *buffer = NULL;
1171 size_t num_bytes;
1172 struct tfm_msg_body_t *msg = NULL;
1173 uint32_t privileged;
1174 struct spm_partition_desc_t *partition = NULL;
1175
1176 TFM_CORE_ASSERT(args != NULL);
1177 msg_handle = (psa_handle_t)args[0];
1178 outvec_idx = args[1];
1179 buffer = (void *)args[2];
1180 num_bytes = (size_t)args[3];
1181
1182 /* It is a fatal error if message handle is invalid */
1183 msg = tfm_spm_get_msg_from_handle(msg_handle);
1184 if (!msg) {
1185 tfm_core_panic();
1186 }
1187
1188 partition = msg->service->partition;
1189 privileged = tfm_spm_partition_get_privileged_mode(
1190 partition->static_data->partition_flags);
1191
1192 /*
1193 * It is a fatal error if message handle does not refer to a request
1194 * message
1195 */
1196 if (msg->msg.type < PSA_IPC_CALL) {
1197 tfm_core_panic();
1198 }
1199
1200 /*
1201 * It is a fatal error if outvec_idx is equal to or greater than
1202 * PSA_MAX_IOVEC
1203 */
1204 if (outvec_idx >= PSA_MAX_IOVEC) {
1205 tfm_core_panic();
1206 }
1207
1208 /*
1209 * It is a fatal error if the call attempts to write data past the end of
1210 * the client output vector
1211 */
1212 if (num_bytes > msg->msg.out_size[outvec_idx] -
1213 msg->outvec[outvec_idx].len) {
1214 tfm_core_panic();
1215 }
1216
1217 /*
1218 * Copy the service buffer to client outvecs. It is a fatal error
1219 * if the memory reference for buffer is invalid or not readable.
1220 */
1221 if (tfm_memory_check(buffer, num_bytes, false,
1222 TFM_MEMORY_ACCESS_RO, privileged) != IPC_SUCCESS) {
1223 tfm_core_panic();
1224 }
1225
1226 tfm_core_util_memcpy((char *)msg->outvec[outvec_idx].base +
1227 msg->outvec[outvec_idx].len, buffer, num_bytes);
1228
1229 /* Update the write number */
1230 msg->outvec[outvec_idx].len += num_bytes;
1231}
1232
1233static void update_caller_outvec_len(struct tfm_msg_body_t *msg)
1234{
1235 uint32_t i;
1236
1237 /*
1238 * FixeMe: abstract these part into dedicated functions to avoid
1239 * accessing thread context in psa layer
1240 */
1241 /* If it is a NS request via RPC, the owner of this message is not set */
1242 if (!is_tfm_rpc_msg(msg)) {
1243 TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK);
1244 }
1245
1246 for (i = 0; i < PSA_MAX_IOVEC; i++) {
1247 if (msg->msg.out_size[i] == 0) {
1248 continue;
1249 }
1250
1251 TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
1252
1253 msg->caller_outvec[i].len = msg->outvec[i].len;
1254 }
1255}
1256
1257void tfm_spm_psa_reply(uint32_t *args)
1258{
1259 psa_handle_t msg_handle;
1260 psa_status_t status;
1261 struct tfm_spm_service_t *service = NULL;
1262 struct tfm_msg_body_t *msg = NULL;
1263 int32_t ret = PSA_SUCCESS;
Ken Liu505b1702020-05-29 13:19:58 +08001264 struct tfm_conn_handle_t *conn_handle;
Mingyang Sund44522a2020-01-16 16:48:37 +08001265
1266 TFM_CORE_ASSERT(args != NULL);
1267 msg_handle = (psa_handle_t)args[0];
1268 status = (psa_status_t)args[1];
1269
1270 /* It is a fatal error if message handle is invalid */
1271 msg = tfm_spm_get_msg_from_handle(msg_handle);
1272 if (!msg) {
1273 tfm_core_panic();
1274 }
1275
1276 /*
1277 * RoT Service information is needed in this function, stored it in message
1278 * body structure. Only two parameters are passed in this function: handle
1279 * and status, so it is useful and simply to do like this.
1280 */
1281 service = msg->service;
1282 if (!service) {
1283 tfm_core_panic();
1284 }
1285
1286 /*
1287 * Three type of message are passed in this function: CONNECTION, REQUEST,
1288 * DISCONNECTION. It needs to process differently for each type.
1289 */
Ken Liu505b1702020-05-29 13:19:58 +08001290 conn_handle = tfm_spm_to_handle_instance(msg_handle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001291 switch (msg->msg.type) {
1292 case PSA_IPC_CONNECT:
1293 /*
1294 * Reply to PSA_IPC_CONNECT message. Connect handle is returned if the
1295 * input status is PSA_SUCCESS. Others return values are based on the
1296 * input status.
1297 */
1298 if (status == PSA_SUCCESS) {
Ken Liu505b1702020-05-29 13:19:58 +08001299 ret = msg_handle;
Mingyang Sund44522a2020-01-16 16:48:37 +08001300 } else if (status == PSA_ERROR_CONNECTION_REFUSED) {
1301 /* Refuse the client connection, indicating a permanent error. */
Ken Liu505b1702020-05-29 13:19:58 +08001302 tfm_spm_free_conn_handle(service, conn_handle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001303 ret = PSA_ERROR_CONNECTION_REFUSED;
1304 } else if (status == PSA_ERROR_CONNECTION_BUSY) {
1305 /* Fail the client connection, indicating a transient error. */
1306 ret = PSA_ERROR_CONNECTION_BUSY;
1307 } else {
1308 tfm_core_panic();
1309 }
1310 break;
1311 case PSA_IPC_DISCONNECT:
1312 /* Service handle is not used anymore */
Ken Liu505b1702020-05-29 13:19:58 +08001313 tfm_spm_free_conn_handle(service, conn_handle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001314
1315 /*
1316 * If the message type is PSA_IPC_DISCONNECT, then the status code is
1317 * ignored
1318 */
1319 break;
1320 default:
1321 if (msg->msg.type >= PSA_IPC_CALL) {
1322 /* Reply to a request message. Return values are based on status */
1323 ret = status;
1324 /*
1325 * The total number of bytes written to a single parameter must be
1326 * reported to the client by updating the len member of the
1327 * psa_outvec structure for the parameter before returning from
1328 * psa_call().
1329 */
1330 update_caller_outvec_len(msg);
1331 } else {
1332 tfm_core_panic();
1333 }
1334 }
1335
1336 if (ret == PSA_ERROR_PROGRAMMER_ERROR) {
1337 /*
1338 * If the source of the programmer error is a Secure Partition, the SPM
1339 * must panic the Secure Partition in response to a PROGRAMMER ERROR.
1340 */
1341 if (TFM_CLIENT_ID_IS_NS(msg->msg.client_id)) {
Ken Liu505b1702020-05-29 13:19:58 +08001342 conn_handle->status = TFM_HANDLE_STATUS_CONNECT_ERROR;
Mingyang Sund44522a2020-01-16 16:48:37 +08001343 } else {
1344 tfm_core_panic();
1345 }
1346 } else {
Ken Liu505b1702020-05-29 13:19:58 +08001347 conn_handle->status = TFM_HANDLE_STATUS_IDLE;
Mingyang Sund44522a2020-01-16 16:48:37 +08001348 }
1349
1350 if (is_tfm_rpc_msg(msg)) {
1351 tfm_rpc_client_call_reply(msg, ret);
1352 } else {
1353 tfm_event_wake(&msg->ack_evnt, ret);
1354 }
1355}
1356
1357/**
1358 * \brief notify the partition with the signal.
1359 *
1360 * \param[in] partition_id The ID of the partition to be notified.
1361 * \param[in] signal The signal that the partition is to be notified
1362 * with.
1363 *
1364 * \retval void Success.
1365 * \retval "Does not return" If partition_id is invalid.
1366 */
1367static void notify_with_signal(int32_t partition_id, psa_signal_t signal)
1368{
1369 struct spm_partition_desc_t *partition = NULL;
1370
1371 /*
1372 * The value of partition_id must be greater than zero as the target of
1373 * notification must be a Secure Partition, providing a Non-secure
1374 * Partition ID is a fatal error.
1375 */
1376 if (!TFM_CLIENT_ID_IS_S(partition_id)) {
1377 tfm_core_panic();
1378 }
1379
1380 /*
1381 * It is a fatal error if partition_id does not correspond to a Secure
1382 * Partition.
1383 */
1384 partition = tfm_spm_get_partition_by_id(partition_id);
1385 if (!partition) {
1386 tfm_core_panic();
1387 }
1388
1389 partition->runtime_data.signals |= signal;
1390
1391 /*
1392 * The target partition may be blocked with waiting for signals after
1393 * called psa_wait(). Set the return value with the available signals
1394 * before wake it up with tfm_event_signal().
1395 */
1396 tfm_event_wake(&partition->runtime_data.signal_evnt,
1397 partition->runtime_data.signals &
1398 partition->runtime_data.signal_mask);
1399}
1400
1401void tfm_spm_psa_notify(uint32_t *args)
1402{
1403 int32_t partition_id;
1404
1405 TFM_CORE_ASSERT(args != NULL);
1406 partition_id = (int32_t)args[0];
1407
1408 notify_with_signal(partition_id, PSA_DOORBELL);
1409}
1410
1411/**
1412 * \brief assert signal for a given IRQ line.
1413 *
1414 * \param[in] partition_id The ID of the partition which handles this IRQ
1415 * \param[in] signal The signal associated with this IRQ
1416 * \param[in] irq_line The number of the IRQ line
1417 *
1418 * \retval void Success.
1419 * \retval "Does not return" Partition ID is invalid
1420 */
1421void tfm_irq_handler(uint32_t partition_id, psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +01001422 IRQn_Type irq_line)
Mingyang Sund44522a2020-01-16 16:48:37 +08001423{
1424 tfm_spm_hal_disable_irq(irq_line);
1425 notify_with_signal(partition_id, signal);
1426}
1427
1428void tfm_spm_psa_clear(void)
1429{
1430 struct spm_partition_desc_t *partition = NULL;
1431
1432 partition = tfm_spm_get_running_partition();
1433 if (!partition) {
1434 tfm_core_panic();
1435 }
1436
1437 /*
1438 * It is a fatal error if the Secure Partition's doorbell signal is not
1439 * currently asserted.
1440 */
1441 if ((partition->runtime_data.signals & PSA_DOORBELL) == 0) {
1442 tfm_core_panic();
1443 }
1444 partition->runtime_data.signals &= ~PSA_DOORBELL;
1445}
1446
1447void tfm_spm_psa_panic(void)
1448{
1449 /*
1450 * PSA FF recommends that the SPM causes the system to restart when a secure
1451 * partition panics.
1452 */
1453 tfm_spm_hal_system_reset();
1454}
1455
1456/**
1457 * \brief Return the IRQ line number associated with a signal
1458 *
1459 * \param[in] partition_id The ID of the partition in which we look for
1460 * the signal.
1461 * \param[in] signal The signal we do the query for.
1462 * \param[out] irq_line The irq line associated with signal
1463 *
1464 * \retval IPC_SUCCESS Execution successful, irq_line contains a valid
1465 * value.
1466 * \retval IPC_ERROR_GENERIC There was an error finding the IRQ line for the
1467 * signal. irq_line is unchanged.
1468 */
1469static int32_t get_irq_line_for_signal(int32_t partition_id,
1470 psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +01001471 IRQn_Type *irq_line)
Mingyang Sund44522a2020-01-16 16:48:37 +08001472{
1473 size_t i;
1474
1475 for (i = 0; i < tfm_core_irq_signals_count; ++i) {
1476 if (tfm_core_irq_signals[i].partition_id == partition_id &&
1477 tfm_core_irq_signals[i].signal_value == signal) {
1478 *irq_line = tfm_core_irq_signals[i].irq_line;
1479 return IPC_SUCCESS;
1480 }
1481 }
1482 return IPC_ERROR_GENERIC;
1483}
1484
1485void tfm_spm_psa_eoi(uint32_t *args)
1486{
1487 psa_signal_t irq_signal;
TTornblomfaf74f52020-03-04 17:56:27 +01001488 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001489 int32_t ret;
1490 struct spm_partition_desc_t *partition = NULL;
1491
1492 TFM_CORE_ASSERT(args != NULL);
1493 irq_signal = (psa_signal_t)args[0];
1494
1495 /* It is a fatal error if passed signal indicates more than one signals. */
1496 if (!tfm_is_one_bit_set(irq_signal)) {
1497 tfm_core_panic();
1498 }
1499
1500 partition = tfm_spm_get_running_partition();
1501 if (!partition) {
1502 tfm_core_panic();
1503 }
1504
1505 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1506 irq_signal, &irq_line);
1507 /* It is a fatal error if passed signal is not an interrupt signal. */
1508 if (ret != IPC_SUCCESS) {
1509 tfm_core_panic();
1510 }
1511
1512 /* It is a fatal error if passed signal is not currently asserted */
1513 if ((partition->runtime_data.signals & irq_signal) == 0) {
1514 tfm_core_panic();
1515 }
1516
1517 partition->runtime_data.signals &= ~irq_signal;
1518
1519 tfm_spm_hal_clear_pending_irq(irq_line);
1520 tfm_spm_hal_enable_irq(irq_line);
1521}
1522
1523void tfm_spm_enable_irq(uint32_t *args)
1524{
1525 struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args;
1526 psa_signal_t irq_signal = svc_ctx->r0;
TTornblomfaf74f52020-03-04 17:56:27 +01001527 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001528 int32_t ret;
1529 struct spm_partition_desc_t *partition = NULL;
1530
1531 /* It is a fatal error if passed signal indicates more than one signals. */
1532 if (!tfm_is_one_bit_set(irq_signal)) {
1533 tfm_core_panic();
1534 }
1535
1536 partition = tfm_spm_get_running_partition();
1537 if (!partition) {
1538 tfm_core_panic();
1539 }
1540
1541 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1542 irq_signal, &irq_line);
1543 /* It is a fatal error if passed signal is not an interrupt signal. */
1544 if (ret != IPC_SUCCESS) {
1545 tfm_core_panic();
1546 }
1547
1548 tfm_spm_hal_enable_irq(irq_line);
1549}
1550
1551void tfm_spm_disable_irq(uint32_t *args)
1552{
1553 struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args;
1554 psa_signal_t irq_signal = svc_ctx->r0;
TTornblomfaf74f52020-03-04 17:56:27 +01001555 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001556 int32_t ret;
1557 struct spm_partition_desc_t *partition = NULL;
1558
1559 /* It is a fatal error if passed signal indicates more than one signals. */
1560 if (!tfm_is_one_bit_set(irq_signal)) {
1561 tfm_core_panic();
1562 }
1563
1564 partition = tfm_spm_get_running_partition();
1565 if (!partition) {
1566 tfm_core_panic();
1567 }
1568
1569 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1570 irq_signal, &irq_line);
1571 /* It is a fatal error if passed signal is not an interrupt signal. */
1572 if (ret != IPC_SUCCESS) {
1573 tfm_core_panic();
1574 }
1575
1576 tfm_spm_hal_disable_irq(irq_line);
1577}
1578
1579void tfm_spm_validate_caller(struct spm_partition_desc_t *p_cur_sp,
1580 uint32_t *p_ctx, uint32_t exc_return,
1581 bool ns_caller)
1582{
1583 uintptr_t stacked_ctx_pos;
1584
1585 if (ns_caller) {
1586 /*
1587 * The background IRQ can't be supported, since if SP is executing,
1588 * the preempted context of SP can be different with the one who
1589 * preempts veneer.
1590 */
1591 if (p_cur_sp->static_data->partition_id != TFM_SP_NON_SECURE_ID) {
1592 tfm_core_panic();
1593 }
1594
1595 /*
1596 * It is non-secure caller, check if veneer stack contains
1597 * multiple contexts.
1598 */
1599 stacked_ctx_pos = (uintptr_t)p_ctx +
1600 sizeof(struct tfm_state_context_t) +
1601 TFM_VENEER_STACK_GUARD_SIZE;
1602
1603 if (is_stack_alloc_fp_space(exc_return)) {
1604#if defined (__FPU_USED) && (__FPU_USED == 1U)
1605 if (FPU->FPCCR & FPU_FPCCR_TS_Msk) {
1606 stacked_ctx_pos += TFM_ADDTIONAL_FP_CONTEXT_WORDS *
1607 sizeof(uint32_t);
1608 }
1609#endif
1610 stacked_ctx_pos += TFM_BASIC_FP_CONTEXT_WORDS * sizeof(uint32_t);
1611 }
1612
1613 if (stacked_ctx_pos != p_cur_sp->runtime_data.sp_thrd.stk_top) {
1614 tfm_core_panic();
1615 }
1616 } else if (p_cur_sp->static_data->partition_id <= 0) {
1617 tfm_core_panic();
1618 }
1619}
Summer Qin830c5542020-02-14 13:44:20 +08001620
1621void tfm_spm_request_handler(const struct tfm_state_context_t *svc_ctx)
1622{
1623 uint32_t *res_ptr = (uint32_t *)&svc_ctx->r0;
1624 uint32_t running_partition_flags = 0;
1625 const struct spm_partition_desc_t *partition = NULL;
1626
1627 /* Check permissions on request type basis */
1628
1629 switch (svc_ctx->r0) {
1630 case TFM_SPM_REQUEST_RESET_VOTE:
1631 partition = tfm_spm_get_running_partition();
1632 if (!partition) {
1633 tfm_core_panic();
1634 }
1635 running_partition_flags = partition->static_data->partition_flags;
1636
1637 /* Currently only PSA Root of Trust services are allowed to make Reset
1638 * vote request
1639 */
1640 if ((running_partition_flags & SPM_PART_FLAG_PSA_ROT) == 0) {
1641 *res_ptr = (uint32_t)TFM_ERROR_GENERIC;
1642 }
1643
1644 /* FixMe: this is a placeholder for checks to be performed before
1645 * allowing execution of reset
1646 */
1647 *res_ptr = (uint32_t)TFM_SUCCESS;
1648
1649 break;
1650 default:
1651 *res_ptr = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
1652 }
1653}
Mingyang Sunbd7ceb52020-06-11 16:53:03 +08001654
1655enum spm_err_t tfm_spm_db_init(void)
1656{
1657 uint32_t i;
1658
1659 /* This function initialises partition db */
1660
1661 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
1662 g_spm_partition_db.partitions[i].static_data = &static_data_list[i];
1663 g_spm_partition_db.partitions[i].platform_data_list =
1664 platform_data_list_list[i];
1665 g_spm_partition_db.partitions[i].memory_data = &memory_data_list[i];
1666 }
1667 g_spm_partition_db.is_init = 1;
1668
1669 return SPM_ERR_OK;
1670}