blob: 4d3ace02f311ef6ba9b90f0b5c6f5afc0300da70 [file] [log] [blame]
Edison Ai764d41f2018-09-21 15:56:36 +08001/*
Ken Liu5248af22019-12-29 12:47:13 +08002 * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
Edison Ai764d41f2018-09-21 15:56:36 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
Mingyang Sunda01a972019-07-12 17:32:59 +08007
Edison Ai764d41f2018-09-21 15:56:36 +08008#include <inttypes.h>
9#include <stdbool.h>
Jamie Foxcc31d402019-01-28 17:13:52 +000010#include "psa/client.h"
11#include "psa/service.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080012#include "psa/lifecycle.h"
13#include "tfm_thread.h"
Edison Ai764d41f2018-09-21 15:56:36 +080014#include "tfm_wait.h"
Summer Qin5fdcf632020-06-22 16:49:24 +080015#include "utilities.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080016#include "tfm_internal_defines.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080017#include "tfm_spm_hal.h"
18#include "tfm_irq_list.h"
19#include "tfm_api.h"
20#include "tfm_secure_api.h"
21#include "tfm_memory_utils.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080022#include "spm_ipc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080023#include "tfm_peripherals_def.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080024#include "tfm_core_utils.h"
25#include "spm_psa_client_call.h"
26#include "tfm_rpc.h"
27#include "tfm_internal.h"
28#include "tfm_core_trustzone.h"
29#include "tfm_core_mem_check.h"
Edison Ai764d41f2018-09-21 15:56:36 +080030#include "tfm_list.h"
31#include "tfm_pools.h"
Mingyang Sunbd7ceb52020-06-11 16:53:03 +080032#include "region.h"
Summer Qin2bfd2a02018-09-26 17:10:41 +080033#include "region_defs.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080034#include "spm_partition_defs.h"
35#include "psa_manifest/pid.h"
Summer Qin5fdcf632020-06-22 16:49:24 +080036#include "tfm/tfm_spm_services.h"
Edison Ai764d41f2018-09-21 15:56:36 +080037
Ken Liu1f345b02020-05-30 21:11:05 +080038#include "secure_fw/partitions/tfm_service_list.inc"
Mingyang Sunbd7ceb52020-06-11 16:53:03 +080039#include "tfm_spm_db_ipc.inc"
Summer Qinbce21132020-08-19 14:28:10 +080040#include "tfm_hal_platform.h"
Summer Qind99509f2019-08-02 17:36:58 +080041
42/* Extern service variable */
43extern struct tfm_spm_service_t service[];
Summer Qine578c5b2019-08-16 16:42:16 +080044extern const struct tfm_spm_service_db_t service_db[];
Summer Qind99509f2019-08-02 17:36:58 +080045
Edison Ai764d41f2018-09-21 15:56:36 +080046/* Pools */
47TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t),
48 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +080049
Mingyang Sund44522a2020-01-16 16:48:37 +080050void tfm_irq_handler(uint32_t partition_id, psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +010051 IRQn_Type irq_line);
Mingyang Sund44522a2020-01-16 16:48:37 +080052
53#include "tfm_secure_irq_handlers_ipc.inc"
Edison Ai764d41f2018-09-21 15:56:36 +080054
Summer Qin373feb12020-03-27 15:35:33 +080055/*********************** Connection handle conversion APIs *******************/
56
57/* Set a minimal value here for feature expansion. */
58#define CLIENT_HANDLE_VALUE_MIN 32
59
60#define CONVERSION_FACTOR_BITOFFSET 3
61#define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET)
62/* Set 32 as the maximum */
63#define CONVERSION_FACTOR_VALUE_MAX 0x20
64
65#if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX
66#error "CONVERSION FACTOR OUT OF RANGE"
67#endif
68
69static uint32_t loop_index;
70
71/*
72 * A handle instance psa_handle_t allocated inside SPM is actually a memory
73 * address among the handle pool. Return this handle to the client directly
74 * exposes information of secure memory address. In this case, converting the
75 * handle into another value does not represent the memory address to avoid
76 * exposing secure memory directly to clients.
77 *
78 * This function converts the handle instance into another value by scaling the
79 * handle in pool offset, the converted value is named as a user handle.
80 *
81 * The formula:
82 * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE +
83 * CLIENT_HANDLE_VALUE_MIN + loop_index
84 * where:
85 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
86 * exceed CONVERSION_FACTOR_VALUE_MAX.
87 *
88 * handle_instance in RANGE[POOL_START, POOL_END]
89 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
90 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
91 *
92 * note:
93 * loop_index is used to promise same handle instance is converted into
94 * different user handles in short time.
95 */
Ken Liu505b1702020-05-29 13:19:58 +080096psa_handle_t tfm_spm_to_user_handle(struct tfm_conn_handle_t *handle_instance)
Summer Qin373feb12020-03-27 15:35:33 +080097{
98 psa_handle_t user_handle;
99
100 loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE;
101 user_handle = (psa_handle_t)((((uintptr_t)handle_instance -
102 (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) +
103 CLIENT_HANDLE_VALUE_MIN + loop_index);
104
105 return user_handle;
106}
107
108/*
109 * This function converts a user handle into a corresponded handle instance.
110 * The converted value is validated before returning, an invalid handle instance
111 * is returned as NULL.
112 *
113 * The formula:
114 * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) /
115 * CONVERSION_FACTOR_VALUE) + POOL_START
116 * where:
117 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
118 * exceed CONVERSION_FACTOR_VALUE_MAX.
119 *
120 * handle_instance in RANGE[POOL_START, POOL_END]
121 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
122 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
123 */
124struct tfm_conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle)
125{
126 struct tfm_conn_handle_t *handle_instance;
127
128 if (user_handle == PSA_NULL_HANDLE) {
129 return NULL;
130 }
131
132 handle_instance = (struct tfm_conn_handle_t *)((((uintptr_t)user_handle -
133 CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) +
134 (uintptr_t)conn_handle_pool);
135
136 return handle_instance;
137}
138
Edison Ai764d41f2018-09-21 15:56:36 +0800139/* Service handle management functions */
Summer Qin630c76b2020-05-20 10:32:58 +0800140struct tfm_conn_handle_t *tfm_spm_create_conn_handle(
141 struct tfm_spm_service_t *service,
Summer Qin1ce712a2019-10-14 18:04:05 +0800142 int32_t client_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800143{
Edison Ai9cc26242019-08-06 11:28:04 +0800144 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800145
Ken Liuf250b8b2019-12-27 16:31:24 +0800146 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800147
148 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +0800149 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
150 if (!p_handle) {
Summer Qin630c76b2020-05-20 10:32:58 +0800151 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800152 }
153
Edison Ai9cc26242019-08-06 11:28:04 +0800154 p_handle->service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800155 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +0800156 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800157
158 /* Add handle node to list for next psa functions */
Edison Ai9cc26242019-08-06 11:28:04 +0800159 tfm_list_add_tail(&service->handle_list, &p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800160
Summer Qin630c76b2020-05-20 10:32:58 +0800161 return p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800162}
163
Summer Qin630c76b2020-05-20 10:32:58 +0800164int32_t tfm_spm_validate_conn_handle(
165 const struct tfm_conn_handle_t *conn_handle,
166 int32_t client_id)
Summer Qin1ce712a2019-10-14 18:04:05 +0800167{
168 /* Check the handle address is validated */
169 if (is_valid_chunk_data_in_pool(conn_handle_pool,
170 (uint8_t *)conn_handle) != true) {
171 return IPC_ERROR_GENERIC;
172 }
173
174 /* Check the handle caller is correct */
Summer Qin630c76b2020-05-20 10:32:58 +0800175 if (conn_handle->client_id != client_id) {
Summer Qin1ce712a2019-10-14 18:04:05 +0800176 return IPC_ERROR_GENERIC;
177 }
178
179 return IPC_SUCCESS;
180}
181
Mingyang Sund44522a2020-01-16 16:48:37 +0800182/**
183 * \brief Free connection handle which not used anymore.
184 *
185 * \param[in] service Target service context pointer
186 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800187 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800188 *
189 * \retval IPC_SUCCESS Success
190 * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
191 * \retval "Does not return" Panic for not find service by handle
192 */
193static int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800194 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800195{
Ken Liuf250b8b2019-12-27 16:31:24 +0800196 TFM_CORE_ASSERT(service);
Summer Qin630c76b2020-05-20 10:32:58 +0800197 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800198
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200199 /* Clear magic as the handler is not used anymore */
Summer Qin630c76b2020-05-20 10:32:58 +0800200 conn_handle->internal_msg.magic = 0;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200201
Edison Ai764d41f2018-09-21 15:56:36 +0800202 /* Remove node from handle list */
Summer Qin630c76b2020-05-20 10:32:58 +0800203 tfm_list_del_node(&conn_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800204
205 /* Back handle buffer to pool */
Summer Qin630c76b2020-05-20 10:32:58 +0800206 tfm_pool_free(conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800207 return IPC_SUCCESS;
208}
209
Mingyang Sund44522a2020-01-16 16:48:37 +0800210/**
211 * \brief Set reverse handle value for connection.
212 *
213 * \param[in] service Target service context pointer
214 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800215 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800216 * \param[in] rhandle rhandle need to save
217 *
218 * \retval IPC_SUCCESS Success
219 * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
220 * \retval "Does not return" Panic for not find handle node
221 */
222static int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800223 struct tfm_conn_handle_t *conn_handle,
Mingyang Sund44522a2020-01-16 16:48:37 +0800224 void *rhandle)
Edison Ai764d41f2018-09-21 15:56:36 +0800225{
Ken Liuf250b8b2019-12-27 16:31:24 +0800226 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800227 /* Set reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800228 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800229
Summer Qin630c76b2020-05-20 10:32:58 +0800230 conn_handle->rhandle = rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800231 return IPC_SUCCESS;
232}
233
Mingyang Sund44522a2020-01-16 16:48:37 +0800234/**
235 * \brief Get reverse handle value from connection hanlde.
236 *
237 * \param[in] service Target service context pointer
238 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800239 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800240 *
241 * \retval void * Success
242 * \retval "Does not return" Panic for those:
243 * service pointer are NULL
244 * hanlde is \ref PSA_NULL_HANDLE
245 * handle node does not be found
246 */
247static void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800248 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800249{
Ken Liuf250b8b2019-12-27 16:31:24 +0800250 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800251 /* Get reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800252 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800253
Summer Qin630c76b2020-05-20 10:32:58 +0800254 return conn_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800255}
256
257/* Partition management functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800258
259/**
Mingyang Sun73056b62020-07-03 15:18:46 +0800260 * \brief Get the msg context by signal.
Mingyang Sund44522a2020-01-16 16:48:37 +0800261 *
262 * \param[in] partition Partition context pointer
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800263 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800264 * \param[in] signal Signal associated with inputs to the Secure
265 * Partition, \ref psa_signal_t
266 *
267 * \retval NULL Failed
268 * \retval "Not NULL" Target service context pointer,
Mingyang Sun73056b62020-07-03 15:18:46 +0800269 * \ref tfm_msg_body_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800270 */
Mingyang Sun73056b62020-07-03 15:18:46 +0800271static struct tfm_msg_body_t *
272 tfm_spm_get_msg_by_signal(struct partition_t *partition,
273 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800274{
275 struct tfm_list_node_t *node, *head;
Mingyang Sun73056b62020-07-03 15:18:46 +0800276 struct tfm_msg_body_t *tmp_msg, *msg = NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800277
Ken Liuf250b8b2019-12-27 16:31:24 +0800278 TFM_CORE_ASSERT(partition);
Edison Ai764d41f2018-09-21 15:56:36 +0800279
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800280 head = &partition->msg_list;
Mingyang Sun73056b62020-07-03 15:18:46 +0800281
282 if (tfm_list_is_empty(head)) {
283 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800284 }
285
Mingyang Sun73056b62020-07-03 15:18:46 +0800286 /*
287 * There may be multiple messages for this RoT Service signal, do not clear
288 * partition mask until no remaining message. Search may be optimized.
289 */
Edison Ai764d41f2018-09-21 15:56:36 +0800290 TFM_LIST_FOR_EACH(node, head) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800291 tmp_msg = TFM_GET_CONTAINER_PTR(node, struct tfm_msg_body_t, msg_node);
292 if (tmp_msg->service->service_db->signal == signal && msg) {
293 return msg;
294 } else if (tmp_msg->service->service_db->signal == signal) {
295 msg = tmp_msg;
296 tfm_list_del_node(node);
Edison Ai764d41f2018-09-21 15:56:36 +0800297 }
298 }
Mingyang Sun73056b62020-07-03 15:18:46 +0800299
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800300 partition->signals_asserted &= ~signal;
Mingyang Sun73056b62020-07-03 15:18:46 +0800301
302 return msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800303}
304
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800305/**
306 * \brief Returns the index of the partition with the given partition ID.
307 *
308 * \param[in] partition_id Partition id
309 *
310 * \return the partition idx if partition_id is valid,
311 * \ref SPM_INVALID_PARTITION_IDX othervise
312 */
313static uint32_t get_partition_idx(uint32_t partition_id)
314{
315 uint32_t i;
316
317 if (partition_id == INVALID_PARTITION_ID) {
318 return SPM_INVALID_PARTITION_IDX;
319 }
320
321 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
322 if (g_spm_partition_db.partitions[i].static_data->partition_id ==
323 partition_id) {
324 return i;
325 }
326 }
327 return SPM_INVALID_PARTITION_IDX;
328}
329
330/**
331 * \brief Get the flags associated with a partition
332 *
333 * \param[in] partition_idx Partition index
334 *
335 * \return Flags associated with the partition
336 *
337 * \note This function doesn't check if partition_idx is valid.
338 */
339static uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
340{
341 return g_spm_partition_db.partitions[partition_idx].static_data->
342 partition_flags;
343}
344
345#if TFM_LVL != 1
346/**
347 * \brief Change the privilege mode for partition thread mode.
348 *
349 * \param[in] privileged Privileged mode,
350 * \ref TFM_PARTITION_PRIVILEGED_MODE
351 * and \ref TFM_PARTITION_UNPRIVILEGED_MODE
352 *
353 * \note Barrier instructions are not called by this function, and if
354 * it is called in thread mode, it might be necessary to call
355 * them after this function returns.
356 */
357static void tfm_spm_partition_change_privilege(uint32_t privileged)
358{
359 CONTROL_Type ctrl;
360
361 ctrl.w = __get_CONTROL();
362
363 if (privileged == TFM_PARTITION_PRIVILEGED_MODE) {
364 ctrl.b.nPRIV = 0;
365 } else {
366 ctrl.b.nPRIV = 1;
367 }
368
369 __set_CONTROL(ctrl.w);
370}
371#endif /* if(TFM_LVL != 1) */
372
373uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
374{
375 return g_spm_partition_db.partitions[partition_idx].static_data->
376 partition_id;
377}
378
379uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
380{
381 if (partition_flags & SPM_PART_FLAG_PSA_ROT) {
382 return TFM_PARTITION_PRIVILEGED_MODE;
383 } else {
384 return TFM_PARTITION_UNPRIVILEGED_MODE;
385 }
386}
387
388bool tfm_is_partition_privileged(uint32_t partition_idx)
389{
390 uint32_t flags = tfm_spm_partition_get_flags(partition_idx);
391
392 return tfm_spm_partition_get_privileged_mode(flags) ==
393 TFM_PARTITION_PRIVILEGED_MODE;
394}
395
Edison Ai764d41f2018-09-21 15:56:36 +0800396struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid)
397{
Summer Qin2fca1c82020-03-20 14:37:55 +0800398 uint32_t i, num;
Edison Ai764d41f2018-09-21 15:56:36 +0800399
Summer Qin2fca1c82020-03-20 14:37:55 +0800400 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
401 for (i = 0; i < num; i++) {
402 if (service[i].service_db->sid == sid) {
403 return &service[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800404 }
405 }
Summer Qin2fca1c82020-03-20 14:37:55 +0800406
Edison Ai764d41f2018-09-21 15:56:36 +0800407 return NULL;
408}
409
Mingyang Sund44522a2020-01-16 16:48:37 +0800410/**
411 * \brief Get the partition context by partition ID.
412 *
413 * \param[in] partition_id Partition identity
414 *
415 * \retval NULL Failed
416 * \retval "Not NULL" Target partition context pointer,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800417 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800418 */
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800419static struct partition_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800420{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800421 uint32_t idx = get_partition_idx(partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800422
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800423 if (idx != SPM_INVALID_PARTITION_IDX) {
424 return &(g_spm_partition_db.partitions[idx]);
Edison Ai764d41f2018-09-21 15:56:36 +0800425 }
426 return NULL;
427}
428
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800429struct partition_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800430{
Kevin Peng79c2bda2020-07-24 16:31:12 +0800431 struct tfm_core_thread_t *pth = tfm_core_thrd_get_curr_thread();
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800432 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800433
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800434 partition = TFM_GET_CONTAINER_PTR(pth, struct partition_t, sp_thread);
435
Kevin Peng79c2bda2020-07-24 16:31:12 +0800436 return partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800437}
438
439int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530440 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800441{
Ken Liuf250b8b2019-12-27 16:31:24 +0800442 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800443
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530444 switch (service->service_db->version_policy) {
Edison Ai764d41f2018-09-21 15:56:36 +0800445 case TFM_VERSION_POLICY_RELAXED:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530446 if (version > service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800447 return IPC_ERROR_VERSION;
448 }
449 break;
450 case TFM_VERSION_POLICY_STRICT:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530451 if (version != service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800452 return IPC_ERROR_VERSION;
453 }
454 break;
455 default:
456 return IPC_ERROR_VERSION;
457 }
458 return IPC_SUCCESS;
459}
460
Edison Aie728fbf2019-11-13 09:37:12 +0800461int32_t tfm_spm_check_authorization(uint32_t sid,
462 struct tfm_spm_service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800463 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800464{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800465 struct partition_t *partition = NULL;
Edison Aie728fbf2019-11-13 09:37:12 +0800466 int32_t i;
467
Ken Liuf250b8b2019-12-27 16:31:24 +0800468 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800469
470 if (ns_caller) {
471 if (!service->service_db->non_secure_client) {
472 return IPC_ERROR_GENERIC;
473 }
474 } else {
475 partition = tfm_spm_get_running_partition();
476 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800477 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800478 }
479
480 for (i = 0; i < partition->static_data->dependencies_num; i++) {
481 if (partition->static_data->p_dependencies[i] == sid) {
482 break;
483 }
484 }
485
486 if (i == partition->static_data->dependencies_num) {
487 return IPC_ERROR_GENERIC;
488 }
489 }
490 return IPC_SUCCESS;
491}
492
Edison Ai764d41f2018-09-21 15:56:36 +0800493/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800494
495/**
496 * \brief Get message context by message handle.
497 *
498 * \param[in] msg_handle Message handle which is a reference generated
499 * by the SPM to a specific message.
500 *
501 * \return The message body context pointer
502 * \ref tfm_msg_body_t structures
503 */
504static struct tfm_msg_body_t *
505 tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800506{
507 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200508 * The message handler passed by the caller is considered invalid in the
509 * following cases:
510 * 1. Not a valid message handle. (The address of a message is not the
511 * address of a possible handle from the pool
512 * 2. Handle not belongs to the caller partition (The handle is either
513 * unused, or owned by anither partition)
514 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800515 */
Ken Liu505b1702020-05-29 13:19:58 +0800516 struct tfm_msg_body_t *p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800517 uint32_t partition_id;
Ken Liu505b1702020-05-29 13:19:58 +0800518 struct tfm_conn_handle_t *p_conn_handle =
519 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200520
521 if (is_valid_chunk_data_in_pool(
Ken Liu505b1702020-05-29 13:19:58 +0800522 conn_handle_pool, (uint8_t *)p_conn_handle) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800523 return NULL;
524 }
525
Ken Liu505b1702020-05-29 13:19:58 +0800526 p_msg = &p_conn_handle->internal_msg;
527
Edison Ai764d41f2018-09-21 15:56:36 +0800528 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200529 * Check that the magic number is correct. This proves that the message
530 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800531 */
Ken Liu505b1702020-05-29 13:19:58 +0800532 if (p_msg->magic != TFM_MSG_MAGIC) {
Edison Ai764d41f2018-09-21 15:56:36 +0800533 return NULL;
534 }
535
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200536 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800537 partition_id = tfm_spm_partition_get_running_partition_id();
Ken Liu505b1702020-05-29 13:19:58 +0800538 if (partition_id != p_msg->service->partition->static_data->partition_id) {
Edison Ai764d41f2018-09-21 15:56:36 +0800539 return NULL;
540 }
541
Ken Liu505b1702020-05-29 13:19:58 +0800542 return p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800543}
544
Edison Ai97115822019-08-01 14:22:19 +0800545struct tfm_msg_body_t *
Summer Qin630c76b2020-05-20 10:32:58 +0800546 tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800547{
Summer Qin630c76b2020-05-20 10:32:58 +0800548 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai97115822019-08-01 14:22:19 +0800549
Summer Qin630c76b2020-05-20 10:32:58 +0800550 return &(conn_handle->internal_msg);
Edison Ai97115822019-08-01 14:22:19 +0800551}
552
553void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
554 struct tfm_spm_service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800555 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800556 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800557 psa_invec *invec, size_t in_len,
558 psa_outvec *outvec, size_t out_len,
559 psa_outvec *caller_outvec)
560{
Edison Ai764d41f2018-09-21 15:56:36 +0800561 uint32_t i;
Ken Liu505b1702020-05-29 13:19:58 +0800562 struct tfm_conn_handle_t *conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800563
Ken Liuf250b8b2019-12-27 16:31:24 +0800564 TFM_CORE_ASSERT(msg);
565 TFM_CORE_ASSERT(service);
566 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
567 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
568 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
569 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
570 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800571
Edison Ai764d41f2018-09-21 15:56:36 +0800572 /* Clear message buffer before using it */
Summer Qinf24dbb52020-07-23 14:53:54 +0800573 spm_memset(msg, 0, sizeof(struct tfm_msg_body_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800574
Ken Liu35f89392019-03-14 14:51:05 +0800575 tfm_event_init(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800576 msg->magic = TFM_MSG_MAGIC;
577 msg->service = service;
Edison Ai764d41f2018-09-21 15:56:36 +0800578 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800579 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800580
581 /* Copy contents */
582 msg->msg.type = type;
583
584 for (i = 0; i < in_len; i++) {
585 msg->msg.in_size[i] = invec[i].len;
586 msg->invec[i].base = invec[i].base;
587 }
588
589 for (i = 0; i < out_len; i++) {
590 msg->msg.out_size[i] = outvec[i].len;
591 msg->outvec[i].base = outvec[i].base;
592 /* Out len is used to record the writed number, set 0 here again */
593 msg->outvec[i].len = 0;
594 }
595
Ken Liu505b1702020-05-29 13:19:58 +0800596 /* Use the user connect handle as the message handle */
597 msg->msg.handle = handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800598
Ken Liu505b1702020-05-29 13:19:58 +0800599 conn_handle = tfm_spm_to_handle_instance(handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800600 /* For connected handle, set rhandle to every message */
Ken Liu505b1702020-05-29 13:19:58 +0800601 if (conn_handle) {
602 msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800603 }
David Hu46603dd2019-12-11 18:05:16 +0800604
605 /* Set the private data of NSPE client caller in multi-core topology */
606 if (TFM_CLIENT_ID_IS_NS(client_id)) {
607 tfm_rpc_set_caller_data(msg, client_id);
608 }
Edison Ai764d41f2018-09-21 15:56:36 +0800609}
610
611int32_t tfm_spm_send_event(struct tfm_spm_service_t *service,
612 struct tfm_msg_body_t *msg)
613{
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800614 struct partition_t *partition = service->partition;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800615
Ken Liuf250b8b2019-12-27 16:31:24 +0800616 TFM_CORE_ASSERT(service);
617 TFM_CORE_ASSERT(msg);
Edison Ai764d41f2018-09-21 15:56:36 +0800618
Mingyang Sun73056b62020-07-03 15:18:46 +0800619 /* Add message to partition message list tail */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800620 tfm_list_add_tail(&partition->msg_list, &msg->msg_node);
Edison Ai764d41f2018-09-21 15:56:36 +0800621
622 /* Messages put. Update signals */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800623 partition->signals_asserted |= service->service_db->signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800624
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800625 tfm_event_wake(&partition->event,
626 (partition->signals_asserted & partition->signals_waiting));
Edison Ai764d41f2018-09-21 15:56:36 +0800627
David Hufb38d562019-09-23 15:58:34 +0800628 /*
629 * If it is a NS request via RPC, it is unnecessary to block current
630 * thread.
631 */
632 if (!is_tfm_rpc_msg(msg)) {
633 tfm_event_wait(&msg->ack_evnt);
634 }
Edison Ai764d41f2018-09-21 15:56:36 +0800635
636 return IPC_SUCCESS;
637}
638
Mingyang Sunf3d29892019-07-10 17:50:23 +0800639uint32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800640{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800641 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800642
Kevin Peng79c2bda2020-07-24 16:31:12 +0800643 partition = tfm_spm_get_running_partition();
644 if (partition && partition->static_data) {
645 return partition->static_data->partition_id;
646 } else {
647 return INVALID_PARTITION_ID;
648 }
Edison Ai764d41f2018-09-21 15:56:36 +0800649}
650
Summer Qin43c185d2019-10-10 15:44:42 +0800651int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800652 enum tfm_memory_access_e access,
653 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800654{
Hugues de Valon99578562019-06-18 16:08:51 +0100655 enum tfm_status_e err;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800656
657 /* If len is zero, this indicates an empty buffer and base is ignored */
658 if (len == 0) {
659 return IPC_SUCCESS;
660 }
661
662 if (!buffer) {
663 return IPC_ERROR_BAD_PARAMETERS;
664 }
665
666 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
667 return IPC_ERROR_MEMORY_CHECK;
668 }
669
Summer Qin424d4db2019-03-25 14:09:51 +0800670 if (access == TFM_MEMORY_ACCESS_RW) {
Summer Qineb537e52019-03-29 09:57:10 +0800671 err = tfm_core_has_write_access_to_region(buffer, len, ns_caller,
672 privileged);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800673 } else {
Summer Qineb537e52019-03-29 09:57:10 +0800674 err = tfm_core_has_read_access_to_region(buffer, len, ns_caller,
675 privileged);
Summer Qin424d4db2019-03-25 14:09:51 +0800676 }
Summer Qin0fc3f592019-04-11 16:00:10 +0800677 if (err == TFM_SUCCESS) {
Summer Qin424d4db2019-03-25 14:09:51 +0800678 return IPC_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800679 }
680
681 return IPC_ERROR_MEMORY_CHECK;
682}
683
Ken Liuce2692d2020-02-11 12:39:36 +0800684uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800685{
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800686 uint32_t i, j, num;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800687 struct partition_t *partition;
Summer Qin66f1e032020-01-06 15:40:03 +0800688 struct tfm_core_thread_t *pth, *p_ns_entry_thread = NULL;
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100689 const struct tfm_spm_partition_platform_data_t **platform_data_p;
Edison Ai764d41f2018-09-21 15:56:36 +0800690
691 tfm_pool_init(conn_handle_pool,
692 POOL_BUFFER_SIZE(conn_handle_pool),
693 sizeof(struct tfm_conn_handle_t),
694 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800695
696 /* Init partition first for it will be used when init service */
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200697 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800698 partition = &g_spm_partition_db.partitions[i];
Edison Aif0501702019-10-11 14:36:42 +0800699
Kevin Peng79c2bda2020-07-24 16:31:12 +0800700 if (!partition || !partition->memory_data || !partition->static_data) {
701 tfm_core_panic();
702 }
703
704 if (!(partition->static_data->partition_flags & SPM_PART_FLAG_IPC)) {
705 tfm_core_panic();
706 }
707
Edison Aif0501702019-10-11 14:36:42 +0800708 /* Check if the PSA framework version matches. */
709 if (partition->static_data->psa_framework_version !=
710 PSA_FRAMEWORK_VERSION) {
Kevin Peng79c2bda2020-07-24 16:31:12 +0800711 ERROR_MSG("Warning: PSA Framework Verison does not match!");
Edison Aif0501702019-10-11 14:36:42 +0800712 continue;
713 }
714
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100715 platform_data_p = partition->platform_data_list;
716 if (platform_data_p != NULL) {
717 while ((*platform_data_p) != NULL) {
Edison Ai6be3df12020-02-14 22:14:33 +0800718 if (tfm_spm_hal_configure_default_isolation(i,
719 *platform_data_p) != TFM_PLAT_ERR_SUCCESS) {
720 tfm_core_panic();
721 }
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100722 ++platform_data_p;
723 }
724 }
725
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800726 /* Add PSA_DOORBELL signal to assigned_signals */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800727 partition->signals_allowed |= PSA_DOORBELL;
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800728
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800729 /* TODO: This can be optimized by generating the assigned signal
730 * in code generation time.
731 */
732 for (j = 0; j < tfm_core_irq_signals_count; ++j) {
733 if (tfm_core_irq_signals[j].partition_id ==
734 partition->static_data->partition_id) {
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800735 partition->signals_allowed |=
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800736 tfm_core_irq_signals[j].signal_value;
737 }
738 }
739
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800740 tfm_event_init(&partition->event);
741 tfm_list_init(&partition->msg_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800742
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800743 pth = &partition->sp_thread;
Edison Ai764d41f2018-09-21 15:56:36 +0800744 if (!pth) {
Edison Ai9059ea02019-11-28 13:46:14 +0800745 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800746 }
747
Summer Qin66f1e032020-01-06 15:40:03 +0800748 tfm_core_thrd_init(pth,
Kevin Peng79c2bda2020-07-24 16:31:12 +0800749 (tfm_core_thrd_entry_t)
750 partition->static_data->partition_init,
Summer Qin66f1e032020-01-06 15:40:03 +0800751 NULL,
Kevin Peng79c2bda2020-07-24 16:31:12 +0800752 (uintptr_t)partition->memory_data->stack_top,
753 (uintptr_t)partition->memory_data->stack_bottom);
Edison Ai788bae22019-02-18 17:38:59 +0800754
Kevin Peng79c2bda2020-07-24 16:31:12 +0800755 pth->prior = partition->static_data->partition_priority;
Edison Ai764d41f2018-09-21 15:56:36 +0800756
Ken Liu490281d2019-12-30 15:55:26 +0800757 if (partition->static_data->partition_id == TFM_SP_NON_SECURE_ID) {
758 p_ns_entry_thread = pth;
Ken Liu5248af22019-12-29 12:47:13 +0800759 pth->param = (void *)tfm_spm_hal_get_ns_entry_point();
Ken Liu490281d2019-12-30 15:55:26 +0800760 }
761
Edison Ai764d41f2018-09-21 15:56:36 +0800762 /* Kick off */
Summer Qin66f1e032020-01-06 15:40:03 +0800763 if (tfm_core_thrd_start(pth) != THRD_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800764 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800765 }
766 }
767
768 /* Init Service */
Summer Qind99509f2019-08-02 17:36:58 +0800769 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
Edison Ai764d41f2018-09-21 15:56:36 +0800770 for (i = 0; i < num; i++) {
Summer Qine578c5b2019-08-16 16:42:16 +0800771 service[i].service_db = &service_db[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800772 partition =
Summer Qine578c5b2019-08-16 16:42:16 +0800773 tfm_spm_get_partition_by_id(service[i].service_db->partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800774 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800775 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800776 }
Summer Qind99509f2019-08-02 17:36:58 +0800777 service[i].partition = partition;
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800778 partition->signals_allowed |= service[i].service_db->signal;
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800779
Summer Qind99509f2019-08-02 17:36:58 +0800780 tfm_list_init(&service[i].handle_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800781 }
782
Ken Liu483f5da2019-04-24 10:45:21 +0800783 /*
784 * All threads initialized, start the scheduler.
785 *
786 * NOTE:
Ken Liu490281d2019-12-30 15:55:26 +0800787 * It is worthy to give the thread object to scheduler if the background
788 * context belongs to one of the threads. Here the background thread is the
789 * initialization thread who calls SPM SVC, which re-uses the non-secure
790 * entry thread's stack. After SPM initialization is done, this stack is
791 * cleaned up and the background context is never going to return. Tell
792 * the scheduler that the current thread is non-secure entry thread.
Ken Liu483f5da2019-04-24 10:45:21 +0800793 */
Summer Qin66f1e032020-01-06 15:40:03 +0800794 tfm_core_thrd_start_scheduler(p_ns_entry_thread);
Ken Liuce2692d2020-02-11 12:39:36 +0800795
Summer Qind2ad7e72020-01-06 18:16:35 +0800796 return p_ns_entry_thread->arch_ctx.lr;
Edison Ai764d41f2018-09-21 15:56:36 +0800797}
Ken Liu2d175172019-03-21 17:08:41 +0800798
Summer Qind2ad7e72020-01-06 18:16:35 +0800799void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx)
Ken Liu2d175172019-03-21 17:08:41 +0800800{
801#if TFM_LVL == 2
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800802 struct partition_t *p_next_partition;
Ken Liu2d175172019-03-21 17:08:41 +0800803 uint32_t is_privileged;
804#endif
Summer Qin66f1e032020-01-06 15:40:03 +0800805 struct tfm_core_thread_t *pth_next = tfm_core_thrd_get_next_thread();
806 struct tfm_core_thread_t *pth_curr = tfm_core_thrd_get_curr_thread();
Ken Liu2d175172019-03-21 17:08:41 +0800807
Mate Toth-Pal32b2ccd2019-04-26 10:00:16 +0200808 if (pth_next != NULL && pth_curr != pth_next) {
Ken Liu2d175172019-03-21 17:08:41 +0800809#if TFM_LVL == 2
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800810 p_next_partition = TFM_GET_CONTAINER_PTR(pth_next,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800811 struct partition_t,
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800812 sp_thread);
Ken Liu2d175172019-03-21 17:08:41 +0800813
Summer Qin423dbef2019-08-22 15:59:35 +0800814 if (p_next_partition->static_data->partition_flags &
Ken Liu2d175172019-03-21 17:08:41 +0800815 SPM_PART_FLAG_PSA_ROT) {
816 is_privileged = TFM_PARTITION_PRIVILEGED_MODE;
817 } else {
818 is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE;
819 }
820
821 tfm_spm_partition_change_privilege(is_privileged);
822#endif
Mate Toth-Palc430b992019-05-09 21:01:14 +0200823
Summer Qind2ad7e72020-01-06 18:16:35 +0800824 tfm_core_thrd_switch_context(p_actx, pth_curr, pth_next);
Ken Liu2d175172019-03-21 17:08:41 +0800825 }
David Hufb38d562019-09-23 15:58:34 +0800826
827 /*
828 * Handle pending mailbox message from NS in multi-core topology.
829 * Empty operation on single Armv8-M platform.
830 */
831 tfm_rpc_client_call_handler();
Ken Liu2d175172019-03-21 17:08:41 +0800832}
Mingyang Sund44522a2020-01-16 16:48:37 +0800833
834/*********************** SPM functions for PSA Client APIs *******************/
835
836uint32_t tfm_spm_psa_framework_version(void)
837{
838 return tfm_spm_client_psa_framework_version();
839}
840
841uint32_t tfm_spm_psa_version(uint32_t *args, bool ns_caller)
842{
843 uint32_t sid;
844
845 TFM_CORE_ASSERT(args != NULL);
846 sid = (uint32_t)args[0];
847
848 return tfm_spm_client_psa_version(sid, ns_caller);
849}
850
851psa_status_t tfm_spm_psa_connect(uint32_t *args, bool ns_caller)
852{
853 uint32_t sid;
854 uint32_t version;
855
856 TFM_CORE_ASSERT(args != NULL);
857 sid = (uint32_t)args[0];
858 version = (uint32_t)args[1];
859
860 return tfm_spm_client_psa_connect(sid, version, ns_caller);
861}
862
863psa_status_t tfm_spm_psa_call(uint32_t *args, bool ns_caller, uint32_t lr)
864{
865 psa_handle_t handle;
866 psa_invec *inptr;
867 psa_outvec *outptr;
868 size_t in_num, out_num;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800869 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800870 uint32_t privileged;
871 int32_t type;
872 struct tfm_control_parameter_t ctrl_param;
873
874 TFM_CORE_ASSERT(args != NULL);
875 handle = (psa_handle_t)args[0];
876
877 partition = tfm_spm_get_running_partition();
878 if (!partition) {
879 tfm_core_panic();
880 }
881 privileged = tfm_spm_partition_get_privileged_mode(
882 partition->static_data->partition_flags);
883
884 /*
885 * Read parameters from the arguments. It is a fatal error if the
886 * memory reference for buffer is invalid or not readable.
887 */
888 if (tfm_memory_check((const void *)args[1],
889 sizeof(struct tfm_control_parameter_t), ns_caller,
890 TFM_MEMORY_ACCESS_RW, privileged) != IPC_SUCCESS) {
891 tfm_core_panic();
892 }
893
Summer Qinf24dbb52020-07-23 14:53:54 +0800894 spm_memcpy(&ctrl_param, (const void *)args[1], sizeof(ctrl_param));
Mingyang Sund44522a2020-01-16 16:48:37 +0800895
896 type = ctrl_param.type;
897 in_num = ctrl_param.in_len;
898 out_num = ctrl_param.out_len;
899 inptr = (psa_invec *)args[2];
900 outptr = (psa_outvec *)args[3];
901
902 /* The request type must be zero or positive. */
903 if (type < 0) {
904 tfm_core_panic();
905 }
906
907 return tfm_spm_client_psa_call(handle, type, inptr, in_num, outptr, out_num,
908 ns_caller, privileged);
909}
910
911void tfm_spm_psa_close(uint32_t *args, bool ns_caller)
912{
913 psa_handle_t handle;
914
915 TFM_CORE_ASSERT(args != NULL);
916 handle = args[0];
917
918 tfm_spm_client_psa_close(handle, ns_caller);
919}
920
921uint32_t tfm_spm_get_lifecycle_state(void)
922{
923 /*
924 * FixMe: return PSA_LIFECYCLE_UNKNOWN to the caller directly. It will be
925 * implemented in the future.
926 */
927 return PSA_LIFECYCLE_UNKNOWN;
928}
929
930/********************* SPM functions for PSA Service APIs ********************/
931
932psa_signal_t tfm_spm_psa_wait(uint32_t *args)
933{
934 psa_signal_t signal_mask;
935 uint32_t timeout;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800936 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800937
938 TFM_CORE_ASSERT(args != NULL);
939 signal_mask = (psa_signal_t)args[0];
940 timeout = args[1];
941
942 /*
943 * Timeout[30:0] are reserved for future use.
944 * SPM must ignore the value of RES.
945 */
946 timeout &= PSA_TIMEOUT_MASK;
947
948 partition = tfm_spm_get_running_partition();
949 if (!partition) {
950 tfm_core_panic();
951 }
952
953 /*
954 * It is a PROGRAMMER ERROR if the signal_mask does not include any assigned
955 * signals.
956 */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800957 if ((partition->signals_allowed & signal_mask) == 0) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800958 tfm_core_panic();
959 }
960
961 /*
962 * Expected signals are included in signal wait mask, ignored signals
963 * should not be set and affect caller thread state. Save this mask for
964 * further checking while signals are ready to be set.
965 */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800966 partition->signals_waiting = signal_mask;
Mingyang Sund44522a2020-01-16 16:48:37 +0800967
968 /*
969 * tfm_event_wait() blocks the caller thread if no signals are available.
970 * In this case, the return value of this function is temporary set into
971 * runtime context. After new signal(s) are available, the return value
972 * is updated with the available signal(s) and blocked thread gets to run.
973 */
974 if (timeout == PSA_BLOCK &&
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800975 (partition->signals_asserted & signal_mask) == 0) {
976 tfm_event_wait(&partition->event);
Mingyang Sund44522a2020-01-16 16:48:37 +0800977 }
978
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800979 return partition->signals_asserted & signal_mask;
Mingyang Sund44522a2020-01-16 16:48:37 +0800980}
981
982psa_status_t tfm_spm_psa_get(uint32_t *args)
983{
984 psa_signal_t signal;
985 psa_msg_t *msg = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800986 struct tfm_msg_body_t *tmp_msg = NULL;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800987 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800988 uint32_t privileged;
989
990 TFM_CORE_ASSERT(args != NULL);
991 signal = (psa_signal_t)args[0];
992 msg = (psa_msg_t *)args[1];
993
994 /*
995 * Only one message could be retrieved every time for psa_get(). It is a
996 * fatal error if the input signal has more than a signal bit set.
997 */
Ken Liu410ada52020-01-08 11:37:27 +0800998 if (!tfm_is_one_bit_set(signal)) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800999 tfm_core_panic();
1000 }
1001
1002 partition = tfm_spm_get_running_partition();
1003 if (!partition) {
1004 tfm_core_panic();
1005 }
1006 privileged = tfm_spm_partition_get_privileged_mode(
1007 partition->static_data->partition_flags);
1008
1009 /*
1010 * Write the message to the service buffer. It is a fatal error if the
1011 * input msg pointer is not a valid memory reference or not read-write.
1012 */
1013 if (tfm_memory_check(msg, sizeof(psa_msg_t), false, TFM_MEMORY_ACCESS_RW,
1014 privileged) != IPC_SUCCESS) {
1015 tfm_core_panic();
1016 }
1017
1018 /*
1019 * It is a fatal error if the caller call psa_get() when no message has
1020 * been set. The caller must call this function after an RoT Service signal
1021 * is returned by psa_wait().
1022 */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +08001023 if (partition->signals_asserted == 0) {
Mingyang Sund44522a2020-01-16 16:48:37 +08001024 tfm_core_panic();
1025 }
1026
1027 /*
1028 * It is a fatal error if the RoT Service signal is not currently asserted.
1029 */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +08001030 if ((partition->signals_asserted & signal) == 0) {
Mingyang Sund44522a2020-01-16 16:48:37 +08001031 tfm_core_panic();
1032 }
1033
1034 /*
Mingyang Sun73056b62020-07-03 15:18:46 +08001035 * Get message by signal from partition. It is a fatal error if getting
Mingyang Sund44522a2020-01-16 16:48:37 +08001036 * failed, which means the input signal is not correspond to an RoT service.
1037 */
Mingyang Sun73056b62020-07-03 15:18:46 +08001038 tmp_msg = tfm_spm_get_msg_by_signal(partition, signal);
Mingyang Sund44522a2020-01-16 16:48:37 +08001039 if (!tmp_msg) {
1040 return PSA_ERROR_DOES_NOT_EXIST;
1041 }
1042
Ken Liu505b1702020-05-29 13:19:58 +08001043 (TFM_GET_CONTAINER_PTR(tmp_msg,
1044 struct tfm_conn_handle_t,
1045 internal_msg))->status = TFM_HANDLE_STATUS_ACTIVE;
Mingyang Sund44522a2020-01-16 16:48:37 +08001046
Summer Qinf24dbb52020-07-23 14:53:54 +08001047 spm_memcpy(msg, &tmp_msg->msg, sizeof(psa_msg_t));
Mingyang Sund44522a2020-01-16 16:48:37 +08001048
Mingyang Sund44522a2020-01-16 16:48:37 +08001049 return PSA_SUCCESS;
1050}
1051
1052void tfm_spm_psa_set_rhandle(uint32_t *args)
1053{
1054 psa_handle_t msg_handle;
1055 void *rhandle = NULL;
1056 struct tfm_msg_body_t *msg = NULL;
Ken Liu505b1702020-05-29 13:19:58 +08001057 struct tfm_conn_handle_t *conn_handle;
Mingyang Sund44522a2020-01-16 16:48:37 +08001058
1059 TFM_CORE_ASSERT(args != NULL);
1060 msg_handle = (psa_handle_t)args[0];
1061 rhandle = (void *)args[1];
1062
1063 /* It is a fatal error if message handle is invalid */
1064 msg = tfm_spm_get_msg_from_handle(msg_handle);
1065 if (!msg) {
1066 tfm_core_panic();
1067 }
1068
1069 msg->msg.rhandle = rhandle;
Ken Liu505b1702020-05-29 13:19:58 +08001070 conn_handle = tfm_spm_to_handle_instance(msg_handle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001071
1072 /* Store reverse handle for following client calls. */
Ken Liu505b1702020-05-29 13:19:58 +08001073 tfm_spm_set_rhandle(msg->service, conn_handle, rhandle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001074}
1075
1076size_t tfm_spm_psa_read(uint32_t *args)
1077{
1078 psa_handle_t msg_handle;
1079 uint32_t invec_idx;
1080 void *buffer = NULL;
1081 size_t num_bytes;
1082 size_t bytes;
1083 struct tfm_msg_body_t *msg = NULL;
1084 uint32_t privileged;
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001085 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001086
1087 TFM_CORE_ASSERT(args != NULL);
1088 msg_handle = (psa_handle_t)args[0];
1089 invec_idx = args[1];
1090 buffer = (void *)args[2];
1091 num_bytes = (size_t)args[3];
1092
1093 /* It is a fatal error if message handle is invalid */
1094 msg = tfm_spm_get_msg_from_handle(msg_handle);
1095 if (!msg) {
1096 tfm_core_panic();
1097 }
1098
1099 partition = msg->service->partition;
1100 privileged = tfm_spm_partition_get_privileged_mode(
1101 partition->static_data->partition_flags);
1102
1103 /*
1104 * It is a fatal error if message handle does not refer to a request
1105 * message
1106 */
1107 if (msg->msg.type < PSA_IPC_CALL) {
1108 tfm_core_panic();
1109 }
1110
1111 /*
1112 * It is a fatal error if invec_idx is equal to or greater than
1113 * PSA_MAX_IOVEC
1114 */
1115 if (invec_idx >= PSA_MAX_IOVEC) {
1116 tfm_core_panic();
1117 }
1118
1119 /* There was no remaining data in this input vector */
1120 if (msg->msg.in_size[invec_idx] == 0) {
1121 return 0;
1122 }
1123
1124 /*
1125 * Copy the client data to the service buffer. It is a fatal error
1126 * if the memory reference for buffer is invalid or not read-write.
1127 */
1128 if (tfm_memory_check(buffer, num_bytes, false,
1129 TFM_MEMORY_ACCESS_RW, privileged) != IPC_SUCCESS) {
1130 tfm_core_panic();
1131 }
1132
1133 bytes = num_bytes > msg->msg.in_size[invec_idx] ?
1134 msg->msg.in_size[invec_idx] : num_bytes;
1135
Summer Qinf24dbb52020-07-23 14:53:54 +08001136 spm_memcpy(buffer, msg->invec[invec_idx].base, bytes);
Mingyang Sund44522a2020-01-16 16:48:37 +08001137
1138 /* There maybe some remaining data */
1139 msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base + bytes;
1140 msg->msg.in_size[invec_idx] -= bytes;
1141
1142 return bytes;
1143}
1144
1145size_t tfm_spm_psa_skip(uint32_t *args)
1146{
1147 psa_handle_t msg_handle;
1148 uint32_t invec_idx;
1149 size_t num_bytes;
1150 struct tfm_msg_body_t *msg = NULL;
1151
1152 TFM_CORE_ASSERT(args != NULL);
1153 msg_handle = (psa_handle_t)args[0];
1154 invec_idx = args[1];
1155 num_bytes = (size_t)args[2];
1156
1157 /* It is a fatal error if message handle is invalid */
1158 msg = tfm_spm_get_msg_from_handle(msg_handle);
1159 if (!msg) {
1160 tfm_core_panic();
1161 }
1162
1163 /*
1164 * It is a fatal error if message handle does not refer to a request
1165 * message
1166 */
1167 if (msg->msg.type < PSA_IPC_CALL) {
1168 tfm_core_panic();
1169 }
1170
1171 /*
1172 * It is a fatal error if invec_idx is equal to or greater than
1173 * PSA_MAX_IOVEC
1174 */
1175 if (invec_idx >= PSA_MAX_IOVEC) {
1176 tfm_core_panic();
1177 }
1178
1179 /* There was no remaining data in this input vector */
1180 if (msg->msg.in_size[invec_idx] == 0) {
1181 return 0;
1182 }
1183
1184 /*
1185 * If num_bytes is greater than the remaining size of the input vector then
1186 * the remaining size of the input vector is used.
1187 */
1188 if (num_bytes > msg->msg.in_size[invec_idx]) {
1189 num_bytes = msg->msg.in_size[invec_idx];
1190 }
1191
1192 /* There maybe some remaining data */
1193 msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base +
1194 num_bytes;
1195 msg->msg.in_size[invec_idx] -= num_bytes;
1196
1197 return num_bytes;
1198}
1199
1200void tfm_spm_psa_write(uint32_t *args)
1201{
1202 psa_handle_t msg_handle;
1203 uint32_t outvec_idx;
1204 void *buffer = NULL;
1205 size_t num_bytes;
1206 struct tfm_msg_body_t *msg = NULL;
1207 uint32_t privileged;
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001208 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001209
1210 TFM_CORE_ASSERT(args != NULL);
1211 msg_handle = (psa_handle_t)args[0];
1212 outvec_idx = args[1];
1213 buffer = (void *)args[2];
1214 num_bytes = (size_t)args[3];
1215
1216 /* It is a fatal error if message handle is invalid */
1217 msg = tfm_spm_get_msg_from_handle(msg_handle);
1218 if (!msg) {
1219 tfm_core_panic();
1220 }
1221
1222 partition = msg->service->partition;
1223 privileged = tfm_spm_partition_get_privileged_mode(
1224 partition->static_data->partition_flags);
1225
1226 /*
1227 * It is a fatal error if message handle does not refer to a request
1228 * message
1229 */
1230 if (msg->msg.type < PSA_IPC_CALL) {
1231 tfm_core_panic();
1232 }
1233
1234 /*
1235 * It is a fatal error if outvec_idx is equal to or greater than
1236 * PSA_MAX_IOVEC
1237 */
1238 if (outvec_idx >= PSA_MAX_IOVEC) {
1239 tfm_core_panic();
1240 }
1241
1242 /*
1243 * It is a fatal error if the call attempts to write data past the end of
1244 * the client output vector
1245 */
1246 if (num_bytes > msg->msg.out_size[outvec_idx] -
1247 msg->outvec[outvec_idx].len) {
1248 tfm_core_panic();
1249 }
1250
1251 /*
1252 * Copy the service buffer to client outvecs. It is a fatal error
1253 * if the memory reference for buffer is invalid or not readable.
1254 */
1255 if (tfm_memory_check(buffer, num_bytes, false,
1256 TFM_MEMORY_ACCESS_RO, privileged) != IPC_SUCCESS) {
1257 tfm_core_panic();
1258 }
1259
Summer Qinf24dbb52020-07-23 14:53:54 +08001260 spm_memcpy((char *)msg->outvec[outvec_idx].base +
1261 msg->outvec[outvec_idx].len, buffer, num_bytes);
Mingyang Sund44522a2020-01-16 16:48:37 +08001262
1263 /* Update the write number */
1264 msg->outvec[outvec_idx].len += num_bytes;
1265}
1266
1267static void update_caller_outvec_len(struct tfm_msg_body_t *msg)
1268{
1269 uint32_t i;
1270
1271 /*
1272 * FixeMe: abstract these part into dedicated functions to avoid
1273 * accessing thread context in psa layer
1274 */
1275 /* If it is a NS request via RPC, the owner of this message is not set */
1276 if (!is_tfm_rpc_msg(msg)) {
1277 TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK);
1278 }
1279
1280 for (i = 0; i < PSA_MAX_IOVEC; i++) {
1281 if (msg->msg.out_size[i] == 0) {
1282 continue;
1283 }
1284
1285 TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
1286
1287 msg->caller_outvec[i].len = msg->outvec[i].len;
1288 }
1289}
1290
1291void tfm_spm_psa_reply(uint32_t *args)
1292{
1293 psa_handle_t msg_handle;
1294 psa_status_t status;
1295 struct tfm_spm_service_t *service = NULL;
1296 struct tfm_msg_body_t *msg = NULL;
1297 int32_t ret = PSA_SUCCESS;
Ken Liu505b1702020-05-29 13:19:58 +08001298 struct tfm_conn_handle_t *conn_handle;
Mingyang Sund44522a2020-01-16 16:48:37 +08001299
1300 TFM_CORE_ASSERT(args != NULL);
1301 msg_handle = (psa_handle_t)args[0];
1302 status = (psa_status_t)args[1];
1303
1304 /* It is a fatal error if message handle is invalid */
1305 msg = tfm_spm_get_msg_from_handle(msg_handle);
1306 if (!msg) {
1307 tfm_core_panic();
1308 }
1309
1310 /*
1311 * RoT Service information is needed in this function, stored it in message
1312 * body structure. Only two parameters are passed in this function: handle
1313 * and status, so it is useful and simply to do like this.
1314 */
1315 service = msg->service;
1316 if (!service) {
1317 tfm_core_panic();
1318 }
1319
1320 /*
1321 * Three type of message are passed in this function: CONNECTION, REQUEST,
1322 * DISCONNECTION. It needs to process differently for each type.
1323 */
Ken Liu505b1702020-05-29 13:19:58 +08001324 conn_handle = tfm_spm_to_handle_instance(msg_handle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001325 switch (msg->msg.type) {
1326 case PSA_IPC_CONNECT:
1327 /*
1328 * Reply to PSA_IPC_CONNECT message. Connect handle is returned if the
1329 * input status is PSA_SUCCESS. Others return values are based on the
1330 * input status.
1331 */
1332 if (status == PSA_SUCCESS) {
Ken Liu505b1702020-05-29 13:19:58 +08001333 ret = msg_handle;
Mingyang Sund44522a2020-01-16 16:48:37 +08001334 } else if (status == PSA_ERROR_CONNECTION_REFUSED) {
1335 /* Refuse the client connection, indicating a permanent error. */
Ken Liu505b1702020-05-29 13:19:58 +08001336 tfm_spm_free_conn_handle(service, conn_handle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001337 ret = PSA_ERROR_CONNECTION_REFUSED;
1338 } else if (status == PSA_ERROR_CONNECTION_BUSY) {
1339 /* Fail the client connection, indicating a transient error. */
1340 ret = PSA_ERROR_CONNECTION_BUSY;
1341 } else {
1342 tfm_core_panic();
1343 }
1344 break;
1345 case PSA_IPC_DISCONNECT:
1346 /* Service handle is not used anymore */
Ken Liu505b1702020-05-29 13:19:58 +08001347 tfm_spm_free_conn_handle(service, conn_handle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001348
1349 /*
1350 * If the message type is PSA_IPC_DISCONNECT, then the status code is
1351 * ignored
1352 */
1353 break;
1354 default:
1355 if (msg->msg.type >= PSA_IPC_CALL) {
1356 /* Reply to a request message. Return values are based on status */
1357 ret = status;
1358 /*
1359 * The total number of bytes written to a single parameter must be
1360 * reported to the client by updating the len member of the
1361 * psa_outvec structure for the parameter before returning from
1362 * psa_call().
1363 */
1364 update_caller_outvec_len(msg);
1365 } else {
1366 tfm_core_panic();
1367 }
1368 }
1369
1370 if (ret == PSA_ERROR_PROGRAMMER_ERROR) {
1371 /*
1372 * If the source of the programmer error is a Secure Partition, the SPM
1373 * must panic the Secure Partition in response to a PROGRAMMER ERROR.
1374 */
1375 if (TFM_CLIENT_ID_IS_NS(msg->msg.client_id)) {
Ken Liu505b1702020-05-29 13:19:58 +08001376 conn_handle->status = TFM_HANDLE_STATUS_CONNECT_ERROR;
Mingyang Sund44522a2020-01-16 16:48:37 +08001377 } else {
1378 tfm_core_panic();
1379 }
1380 } else {
Ken Liu505b1702020-05-29 13:19:58 +08001381 conn_handle->status = TFM_HANDLE_STATUS_IDLE;
Mingyang Sund44522a2020-01-16 16:48:37 +08001382 }
1383
1384 if (is_tfm_rpc_msg(msg)) {
1385 tfm_rpc_client_call_reply(msg, ret);
1386 } else {
1387 tfm_event_wake(&msg->ack_evnt, ret);
1388 }
1389}
1390
1391/**
1392 * \brief notify the partition with the signal.
1393 *
1394 * \param[in] partition_id The ID of the partition to be notified.
1395 * \param[in] signal The signal that the partition is to be notified
1396 * with.
1397 *
1398 * \retval void Success.
1399 * \retval "Does not return" If partition_id is invalid.
1400 */
1401static void notify_with_signal(int32_t partition_id, psa_signal_t signal)
1402{
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001403 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001404
1405 /*
1406 * The value of partition_id must be greater than zero as the target of
1407 * notification must be a Secure Partition, providing a Non-secure
1408 * Partition ID is a fatal error.
1409 */
1410 if (!TFM_CLIENT_ID_IS_S(partition_id)) {
1411 tfm_core_panic();
1412 }
1413
1414 /*
1415 * It is a fatal error if partition_id does not correspond to a Secure
1416 * Partition.
1417 */
1418 partition = tfm_spm_get_partition_by_id(partition_id);
1419 if (!partition) {
1420 tfm_core_panic();
1421 }
1422
Mingyang Sunaf22ffa2020-07-09 17:48:37 +08001423 partition->signals_asserted |= signal;
Mingyang Sund44522a2020-01-16 16:48:37 +08001424
1425 /*
1426 * The target partition may be blocked with waiting for signals after
1427 * called psa_wait(). Set the return value with the available signals
1428 * before wake it up with tfm_event_signal().
1429 */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +08001430 tfm_event_wake(&partition->event,
1431 partition->signals_asserted & partition->signals_waiting);
Mingyang Sund44522a2020-01-16 16:48:37 +08001432}
1433
1434void tfm_spm_psa_notify(uint32_t *args)
1435{
1436 int32_t partition_id;
1437
1438 TFM_CORE_ASSERT(args != NULL);
1439 partition_id = (int32_t)args[0];
1440
1441 notify_with_signal(partition_id, PSA_DOORBELL);
1442}
1443
1444/**
1445 * \brief assert signal for a given IRQ line.
1446 *
1447 * \param[in] partition_id The ID of the partition which handles this IRQ
1448 * \param[in] signal The signal associated with this IRQ
1449 * \param[in] irq_line The number of the IRQ line
1450 *
1451 * \retval void Success.
1452 * \retval "Does not return" Partition ID is invalid
1453 */
1454void tfm_irq_handler(uint32_t partition_id, psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +01001455 IRQn_Type irq_line)
Mingyang Sund44522a2020-01-16 16:48:37 +08001456{
1457 tfm_spm_hal_disable_irq(irq_line);
1458 notify_with_signal(partition_id, signal);
1459}
1460
1461void tfm_spm_psa_clear(void)
1462{
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001463 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001464
1465 partition = tfm_spm_get_running_partition();
1466 if (!partition) {
1467 tfm_core_panic();
1468 }
1469
1470 /*
1471 * It is a fatal error if the Secure Partition's doorbell signal is not
1472 * currently asserted.
1473 */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +08001474 if ((partition->signals_asserted & PSA_DOORBELL) == 0) {
Mingyang Sund44522a2020-01-16 16:48:37 +08001475 tfm_core_panic();
1476 }
Mingyang Sunaf22ffa2020-07-09 17:48:37 +08001477 partition->signals_asserted &= ~PSA_DOORBELL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001478}
1479
1480void tfm_spm_psa_panic(void)
1481{
1482 /*
1483 * PSA FF recommends that the SPM causes the system to restart when a secure
1484 * partition panics.
1485 */
Summer Qinbce21132020-08-19 14:28:10 +08001486 tfm_hal_system_reset();
Mingyang Sund44522a2020-01-16 16:48:37 +08001487}
1488
1489/**
1490 * \brief Return the IRQ line number associated with a signal
1491 *
1492 * \param[in] partition_id The ID of the partition in which we look for
1493 * the signal.
1494 * \param[in] signal The signal we do the query for.
1495 * \param[out] irq_line The irq line associated with signal
1496 *
1497 * \retval IPC_SUCCESS Execution successful, irq_line contains a valid
1498 * value.
1499 * \retval IPC_ERROR_GENERIC There was an error finding the IRQ line for the
1500 * signal. irq_line is unchanged.
1501 */
1502static int32_t get_irq_line_for_signal(int32_t partition_id,
1503 psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +01001504 IRQn_Type *irq_line)
Mingyang Sund44522a2020-01-16 16:48:37 +08001505{
1506 size_t i;
1507
1508 for (i = 0; i < tfm_core_irq_signals_count; ++i) {
1509 if (tfm_core_irq_signals[i].partition_id == partition_id &&
1510 tfm_core_irq_signals[i].signal_value == signal) {
1511 *irq_line = tfm_core_irq_signals[i].irq_line;
1512 return IPC_SUCCESS;
1513 }
1514 }
1515 return IPC_ERROR_GENERIC;
1516}
1517
1518void tfm_spm_psa_eoi(uint32_t *args)
1519{
1520 psa_signal_t irq_signal;
TTornblomfaf74f52020-03-04 17:56:27 +01001521 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001522 int32_t ret;
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001523 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001524
1525 TFM_CORE_ASSERT(args != NULL);
1526 irq_signal = (psa_signal_t)args[0];
1527
1528 /* It is a fatal error if passed signal indicates more than one signals. */
1529 if (!tfm_is_one_bit_set(irq_signal)) {
1530 tfm_core_panic();
1531 }
1532
1533 partition = tfm_spm_get_running_partition();
1534 if (!partition) {
1535 tfm_core_panic();
1536 }
1537
1538 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1539 irq_signal, &irq_line);
1540 /* It is a fatal error if passed signal is not an interrupt signal. */
1541 if (ret != IPC_SUCCESS) {
1542 tfm_core_panic();
1543 }
1544
1545 /* It is a fatal error if passed signal is not currently asserted */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +08001546 if ((partition->signals_asserted & irq_signal) == 0) {
Mingyang Sund44522a2020-01-16 16:48:37 +08001547 tfm_core_panic();
1548 }
1549
Mingyang Sunaf22ffa2020-07-09 17:48:37 +08001550 partition->signals_asserted &= ~irq_signal;
Mingyang Sund44522a2020-01-16 16:48:37 +08001551
1552 tfm_spm_hal_clear_pending_irq(irq_line);
1553 tfm_spm_hal_enable_irq(irq_line);
1554}
1555
1556void tfm_spm_enable_irq(uint32_t *args)
1557{
1558 struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args;
1559 psa_signal_t irq_signal = svc_ctx->r0;
TTornblomfaf74f52020-03-04 17:56:27 +01001560 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001561 int32_t ret;
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001562 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001563
1564 /* It is a fatal error if passed signal indicates more than one signals. */
1565 if (!tfm_is_one_bit_set(irq_signal)) {
1566 tfm_core_panic();
1567 }
1568
1569 partition = tfm_spm_get_running_partition();
1570 if (!partition) {
1571 tfm_core_panic();
1572 }
1573
1574 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1575 irq_signal, &irq_line);
1576 /* It is a fatal error if passed signal is not an interrupt signal. */
1577 if (ret != IPC_SUCCESS) {
1578 tfm_core_panic();
1579 }
1580
1581 tfm_spm_hal_enable_irq(irq_line);
1582}
1583
1584void tfm_spm_disable_irq(uint32_t *args)
1585{
1586 struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args;
1587 psa_signal_t irq_signal = svc_ctx->r0;
TTornblomfaf74f52020-03-04 17:56:27 +01001588 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001589 int32_t ret;
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001590 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001591
1592 /* It is a fatal error if passed signal indicates more than one signals. */
1593 if (!tfm_is_one_bit_set(irq_signal)) {
1594 tfm_core_panic();
1595 }
1596
1597 partition = tfm_spm_get_running_partition();
1598 if (!partition) {
1599 tfm_core_panic();
1600 }
1601
1602 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1603 irq_signal, &irq_line);
1604 /* It is a fatal error if passed signal is not an interrupt signal. */
1605 if (ret != IPC_SUCCESS) {
1606 tfm_core_panic();
1607 }
1608
1609 tfm_spm_hal_disable_irq(irq_line);
1610}
1611
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001612void tfm_spm_validate_caller(struct partition_t *p_cur_sp, uint32_t *p_ctx,
1613 uint32_t exc_return, bool ns_caller)
Mingyang Sund44522a2020-01-16 16:48:37 +08001614{
1615 uintptr_t stacked_ctx_pos;
1616
1617 if (ns_caller) {
1618 /*
1619 * The background IRQ can't be supported, since if SP is executing,
1620 * the preempted context of SP can be different with the one who
1621 * preempts veneer.
1622 */
1623 if (p_cur_sp->static_data->partition_id != TFM_SP_NON_SECURE_ID) {
1624 tfm_core_panic();
1625 }
1626
1627 /*
1628 * It is non-secure caller, check if veneer stack contains
1629 * multiple contexts.
1630 */
1631 stacked_ctx_pos = (uintptr_t)p_ctx +
1632 sizeof(struct tfm_state_context_t) +
1633 TFM_VENEER_STACK_GUARD_SIZE;
1634
1635 if (is_stack_alloc_fp_space(exc_return)) {
1636#if defined (__FPU_USED) && (__FPU_USED == 1U)
1637 if (FPU->FPCCR & FPU_FPCCR_TS_Msk) {
1638 stacked_ctx_pos += TFM_ADDTIONAL_FP_CONTEXT_WORDS *
1639 sizeof(uint32_t);
1640 }
1641#endif
1642 stacked_ctx_pos += TFM_BASIC_FP_CONTEXT_WORDS * sizeof(uint32_t);
1643 }
1644
Mingyang Sunaf22ffa2020-07-09 17:48:37 +08001645 if (stacked_ctx_pos != p_cur_sp->sp_thread.stk_top) {
Mingyang Sund44522a2020-01-16 16:48:37 +08001646 tfm_core_panic();
1647 }
1648 } else if (p_cur_sp->static_data->partition_id <= 0) {
1649 tfm_core_panic();
1650 }
1651}
Summer Qin830c5542020-02-14 13:44:20 +08001652
1653void tfm_spm_request_handler(const struct tfm_state_context_t *svc_ctx)
1654{
1655 uint32_t *res_ptr = (uint32_t *)&svc_ctx->r0;
1656 uint32_t running_partition_flags = 0;
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001657 const struct partition_t *partition = NULL;
Summer Qin830c5542020-02-14 13:44:20 +08001658
1659 /* Check permissions on request type basis */
1660
1661 switch (svc_ctx->r0) {
1662 case TFM_SPM_REQUEST_RESET_VOTE:
1663 partition = tfm_spm_get_running_partition();
1664 if (!partition) {
1665 tfm_core_panic();
1666 }
1667 running_partition_flags = partition->static_data->partition_flags;
1668
1669 /* Currently only PSA Root of Trust services are allowed to make Reset
1670 * vote request
1671 */
1672 if ((running_partition_flags & SPM_PART_FLAG_PSA_ROT) == 0) {
1673 *res_ptr = (uint32_t)TFM_ERROR_GENERIC;
1674 }
1675
1676 /* FixMe: this is a placeholder for checks to be performed before
1677 * allowing execution of reset
1678 */
1679 *res_ptr = (uint32_t)TFM_SUCCESS;
1680
1681 break;
1682 default:
1683 *res_ptr = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
1684 }
1685}
Mingyang Sunbd7ceb52020-06-11 16:53:03 +08001686
1687enum spm_err_t tfm_spm_db_init(void)
1688{
1689 uint32_t i;
1690
1691 /* This function initialises partition db */
1692
1693 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
1694 g_spm_partition_db.partitions[i].static_data = &static_data_list[i];
1695 g_spm_partition_db.partitions[i].platform_data_list =
1696 platform_data_list_list[i];
1697 g_spm_partition_db.partitions[i].memory_data = &memory_data_list[i];
1698 }
1699 g_spm_partition_db.is_init = 1;
1700
1701 return SPM_ERR_OK;
1702}