blob: 5abbe24898f8ce5fd99e35a103a4b279a5f8889f [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"
Summer Qindf7a14c2020-08-24 11:42:27 +080025#include "common/spm_psa_client_call.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080026#include "tfm_rpc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080027#include "tfm_core_trustzone.h"
28#include "tfm_core_mem_check.h"
Edison Ai764d41f2018-09-21 15:56:36 +080029#include "tfm_list.h"
30#include "tfm_pools.h"
Mingyang Sunbd7ceb52020-06-11 16:53:03 +080031#include "region.h"
Summer Qin2bfd2a02018-09-26 17:10:41 +080032#include "region_defs.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080033#include "spm_partition_defs.h"
34#include "psa_manifest/pid.h"
Summer Qin5fdcf632020-06-22 16:49:24 +080035#include "tfm/tfm_spm_services.h"
Edison Ai764d41f2018-09-21 15:56:36 +080036
Ken Liu1f345b02020-05-30 21:11:05 +080037#include "secure_fw/partitions/tfm_service_list.inc"
Mingyang Sunbd7ceb52020-06-11 16:53:03 +080038#include "tfm_spm_db_ipc.inc"
Summer Qinbce21132020-08-19 14:28:10 +080039#include "tfm_hal_platform.h"
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/**
Mingyang Sun73056b62020-07-03 15:18:46 +0800259 * \brief Get the msg context by signal.
Mingyang Sund44522a2020-01-16 16:48:37 +0800260 *
261 * \param[in] partition Partition context pointer
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800262 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800263 * \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,
Mingyang Sun73056b62020-07-03 15:18:46 +0800268 * \ref tfm_msg_body_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800269 */
Mingyang Sun73056b62020-07-03 15:18:46 +0800270static struct tfm_msg_body_t *
271 tfm_spm_get_msg_by_signal(struct partition_t *partition,
272 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800273{
274 struct tfm_list_node_t *node, *head;
Mingyang Sun73056b62020-07-03 15:18:46 +0800275 struct tfm_msg_body_t *tmp_msg, *msg = NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800276
Ken Liuf250b8b2019-12-27 16:31:24 +0800277 TFM_CORE_ASSERT(partition);
Edison Ai764d41f2018-09-21 15:56:36 +0800278
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800279 head = &partition->msg_list;
Mingyang Sun73056b62020-07-03 15:18:46 +0800280
281 if (tfm_list_is_empty(head)) {
282 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800283 }
284
Mingyang Sun73056b62020-07-03 15:18:46 +0800285 /*
286 * There may be multiple messages for this RoT Service signal, do not clear
287 * partition mask until no remaining message. Search may be optimized.
288 */
Edison Ai764d41f2018-09-21 15:56:36 +0800289 TFM_LIST_FOR_EACH(node, head) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800290 tmp_msg = TFM_GET_CONTAINER_PTR(node, struct tfm_msg_body_t, msg_node);
291 if (tmp_msg->service->service_db->signal == signal && msg) {
292 return msg;
293 } else if (tmp_msg->service->service_db->signal == signal) {
294 msg = tmp_msg;
295 tfm_list_del_node(node);
Edison Ai764d41f2018-09-21 15:56:36 +0800296 }
297 }
Mingyang Sun73056b62020-07-03 15:18:46 +0800298
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800299 partition->signals_asserted &= ~signal;
Mingyang Sun73056b62020-07-03 15:18:46 +0800300
301 return msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800302}
303
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800304/**
305 * \brief Returns the index of the partition with the given partition ID.
306 *
307 * \param[in] partition_id Partition id
308 *
309 * \return the partition idx if partition_id is valid,
310 * \ref SPM_INVALID_PARTITION_IDX othervise
311 */
312static uint32_t get_partition_idx(uint32_t partition_id)
313{
314 uint32_t i;
315
316 if (partition_id == INVALID_PARTITION_ID) {
317 return SPM_INVALID_PARTITION_IDX;
318 }
319
320 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
321 if (g_spm_partition_db.partitions[i].static_data->partition_id ==
322 partition_id) {
323 return i;
324 }
325 }
326 return SPM_INVALID_PARTITION_IDX;
327}
328
329/**
330 * \brief Get the flags associated with a partition
331 *
332 * \param[in] partition_idx Partition index
333 *
334 * \return Flags associated with the partition
335 *
336 * \note This function doesn't check if partition_idx is valid.
337 */
338static uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
339{
340 return g_spm_partition_db.partitions[partition_idx].static_data->
341 partition_flags;
342}
343
344#if TFM_LVL != 1
345/**
346 * \brief Change the privilege mode for partition thread mode.
347 *
348 * \param[in] privileged Privileged mode,
349 * \ref TFM_PARTITION_PRIVILEGED_MODE
350 * and \ref TFM_PARTITION_UNPRIVILEGED_MODE
351 *
352 * \note Barrier instructions are not called by this function, and if
353 * it is called in thread mode, it might be necessary to call
354 * them after this function returns.
355 */
356static void tfm_spm_partition_change_privilege(uint32_t privileged)
357{
358 CONTROL_Type ctrl;
359
360 ctrl.w = __get_CONTROL();
361
362 if (privileged == TFM_PARTITION_PRIVILEGED_MODE) {
363 ctrl.b.nPRIV = 0;
364 } else {
365 ctrl.b.nPRIV = 1;
366 }
367
368 __set_CONTROL(ctrl.w);
369}
370#endif /* if(TFM_LVL != 1) */
371
372uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
373{
374 return g_spm_partition_db.partitions[partition_idx].static_data->
375 partition_id;
376}
377
378uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
379{
380 if (partition_flags & SPM_PART_FLAG_PSA_ROT) {
381 return TFM_PARTITION_PRIVILEGED_MODE;
382 } else {
383 return TFM_PARTITION_UNPRIVILEGED_MODE;
384 }
385}
386
387bool tfm_is_partition_privileged(uint32_t partition_idx)
388{
389 uint32_t flags = tfm_spm_partition_get_flags(partition_idx);
390
391 return tfm_spm_partition_get_privileged_mode(flags) ==
392 TFM_PARTITION_PRIVILEGED_MODE;
393}
394
Edison Ai764d41f2018-09-21 15:56:36 +0800395struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid)
396{
Summer Qin2fca1c82020-03-20 14:37:55 +0800397 uint32_t i, num;
Edison Ai764d41f2018-09-21 15:56:36 +0800398
Summer Qin2fca1c82020-03-20 14:37:55 +0800399 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
400 for (i = 0; i < num; i++) {
401 if (service[i].service_db->sid == sid) {
402 return &service[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800403 }
404 }
Summer Qin2fca1c82020-03-20 14:37:55 +0800405
Edison Ai764d41f2018-09-21 15:56:36 +0800406 return NULL;
407}
408
Mingyang Sund44522a2020-01-16 16:48:37 +0800409/**
410 * \brief Get the partition context by partition ID.
411 *
412 * \param[in] partition_id Partition identity
413 *
414 * \retval NULL Failed
415 * \retval "Not NULL" Target partition context pointer,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800416 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800417 */
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800418static struct partition_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800419{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800420 uint32_t idx = get_partition_idx(partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800421
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800422 if (idx != SPM_INVALID_PARTITION_IDX) {
423 return &(g_spm_partition_db.partitions[idx]);
Edison Ai764d41f2018-09-21 15:56:36 +0800424 }
425 return NULL;
426}
427
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800428struct partition_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800429{
Kevin Peng79c2bda2020-07-24 16:31:12 +0800430 struct tfm_core_thread_t *pth = tfm_core_thrd_get_curr_thread();
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800431 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800432
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800433 partition = TFM_GET_CONTAINER_PTR(pth, struct partition_t, sp_thread);
434
Kevin Peng79c2bda2020-07-24 16:31:12 +0800435 return partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800436}
437
438int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530439 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800440{
Ken Liuf250b8b2019-12-27 16:31:24 +0800441 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800442
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530443 switch (service->service_db->version_policy) {
Edison Ai764d41f2018-09-21 15:56:36 +0800444 case TFM_VERSION_POLICY_RELAXED:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530445 if (version > service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800446 return IPC_ERROR_VERSION;
447 }
448 break;
449 case TFM_VERSION_POLICY_STRICT:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530450 if (version != service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800451 return IPC_ERROR_VERSION;
452 }
453 break;
454 default:
455 return IPC_ERROR_VERSION;
456 }
457 return IPC_SUCCESS;
458}
459
Edison Aie728fbf2019-11-13 09:37:12 +0800460int32_t tfm_spm_check_authorization(uint32_t sid,
461 struct tfm_spm_service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800462 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800463{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800464 struct partition_t *partition = NULL;
Edison Aie728fbf2019-11-13 09:37:12 +0800465 int32_t i;
466
Ken Liuf250b8b2019-12-27 16:31:24 +0800467 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800468
469 if (ns_caller) {
470 if (!service->service_db->non_secure_client) {
471 return IPC_ERROR_GENERIC;
472 }
473 } else {
474 partition = tfm_spm_get_running_partition();
475 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800476 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800477 }
478
479 for (i = 0; i < partition->static_data->dependencies_num; i++) {
480 if (partition->static_data->p_dependencies[i] == sid) {
481 break;
482 }
483 }
484
485 if (i == partition->static_data->dependencies_num) {
486 return IPC_ERROR_GENERIC;
487 }
488 }
489 return IPC_SUCCESS;
490}
491
Edison Ai764d41f2018-09-21 15:56:36 +0800492/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800493
494/**
495 * \brief Get message context by message handle.
496 *
497 * \param[in] msg_handle Message handle which is a reference generated
498 * by the SPM to a specific message.
499 *
500 * \return The message body context pointer
501 * \ref tfm_msg_body_t structures
502 */
503static struct tfm_msg_body_t *
504 tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800505{
506 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200507 * The message handler passed by the caller is considered invalid in the
508 * following cases:
509 * 1. Not a valid message handle. (The address of a message is not the
510 * address of a possible handle from the pool
511 * 2. Handle not belongs to the caller partition (The handle is either
512 * unused, or owned by anither partition)
513 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800514 */
Ken Liu505b1702020-05-29 13:19:58 +0800515 struct tfm_msg_body_t *p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800516 uint32_t partition_id;
Ken Liu505b1702020-05-29 13:19:58 +0800517 struct tfm_conn_handle_t *p_conn_handle =
518 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200519
520 if (is_valid_chunk_data_in_pool(
Ken Liu505b1702020-05-29 13:19:58 +0800521 conn_handle_pool, (uint8_t *)p_conn_handle) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800522 return NULL;
523 }
524
Ken Liu505b1702020-05-29 13:19:58 +0800525 p_msg = &p_conn_handle->internal_msg;
526
Edison Ai764d41f2018-09-21 15:56:36 +0800527 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200528 * Check that the magic number is correct. This proves that the message
529 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800530 */
Ken Liu505b1702020-05-29 13:19:58 +0800531 if (p_msg->magic != TFM_MSG_MAGIC) {
Edison Ai764d41f2018-09-21 15:56:36 +0800532 return NULL;
533 }
534
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200535 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800536 partition_id = tfm_spm_partition_get_running_partition_id();
Ken Liu505b1702020-05-29 13:19:58 +0800537 if (partition_id != p_msg->service->partition->static_data->partition_id) {
Edison Ai764d41f2018-09-21 15:56:36 +0800538 return NULL;
539 }
540
Ken Liu505b1702020-05-29 13:19:58 +0800541 return p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800542}
543
Edison Ai97115822019-08-01 14:22:19 +0800544struct tfm_msg_body_t *
Summer Qin630c76b2020-05-20 10:32:58 +0800545 tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800546{
Summer Qin630c76b2020-05-20 10:32:58 +0800547 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai97115822019-08-01 14:22:19 +0800548
Summer Qin630c76b2020-05-20 10:32:58 +0800549 return &(conn_handle->internal_msg);
Edison Ai97115822019-08-01 14:22:19 +0800550}
551
552void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
553 struct tfm_spm_service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800554 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800555 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800556 psa_invec *invec, size_t in_len,
557 psa_outvec *outvec, size_t out_len,
558 psa_outvec *caller_outvec)
559{
Edison Ai764d41f2018-09-21 15:56:36 +0800560 uint32_t i;
Ken Liu505b1702020-05-29 13:19:58 +0800561 struct tfm_conn_handle_t *conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800562
Ken Liuf250b8b2019-12-27 16:31:24 +0800563 TFM_CORE_ASSERT(msg);
564 TFM_CORE_ASSERT(service);
565 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
566 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
567 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
568 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
569 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800570
Edison Ai764d41f2018-09-21 15:56:36 +0800571 /* Clear message buffer before using it */
Summer Qinf24dbb52020-07-23 14:53:54 +0800572 spm_memset(msg, 0, sizeof(struct tfm_msg_body_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800573
Ken Liu35f89392019-03-14 14:51:05 +0800574 tfm_event_init(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800575 msg->magic = TFM_MSG_MAGIC;
576 msg->service = service;
Edison Ai764d41f2018-09-21 15:56:36 +0800577 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800578 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800579
580 /* Copy contents */
581 msg->msg.type = type;
582
583 for (i = 0; i < in_len; i++) {
584 msg->msg.in_size[i] = invec[i].len;
585 msg->invec[i].base = invec[i].base;
586 }
587
588 for (i = 0; i < out_len; i++) {
589 msg->msg.out_size[i] = outvec[i].len;
590 msg->outvec[i].base = outvec[i].base;
591 /* Out len is used to record the writed number, set 0 here again */
592 msg->outvec[i].len = 0;
593 }
594
Ken Liu505b1702020-05-29 13:19:58 +0800595 /* Use the user connect handle as the message handle */
596 msg->msg.handle = handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800597
Ken Liu505b1702020-05-29 13:19:58 +0800598 conn_handle = tfm_spm_to_handle_instance(handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800599 /* For connected handle, set rhandle to every message */
Ken Liu505b1702020-05-29 13:19:58 +0800600 if (conn_handle) {
601 msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800602 }
David Hu46603dd2019-12-11 18:05:16 +0800603
604 /* Set the private data of NSPE client caller in multi-core topology */
605 if (TFM_CLIENT_ID_IS_NS(client_id)) {
606 tfm_rpc_set_caller_data(msg, client_id);
607 }
Edison Ai764d41f2018-09-21 15:56:36 +0800608}
609
610int32_t tfm_spm_send_event(struct tfm_spm_service_t *service,
611 struct tfm_msg_body_t *msg)
612{
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800613 struct partition_t *partition = service->partition;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800614
Ken Liuf250b8b2019-12-27 16:31:24 +0800615 TFM_CORE_ASSERT(service);
616 TFM_CORE_ASSERT(msg);
Edison Ai764d41f2018-09-21 15:56:36 +0800617
Mingyang Sun73056b62020-07-03 15:18:46 +0800618 /* Add message to partition message list tail */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800619 tfm_list_add_tail(&partition->msg_list, &msg->msg_node);
Edison Ai764d41f2018-09-21 15:56:36 +0800620
621 /* Messages put. Update signals */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800622 partition->signals_asserted |= service->service_db->signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800623
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800624 tfm_event_wake(&partition->event,
625 (partition->signals_asserted & partition->signals_waiting));
Edison Ai764d41f2018-09-21 15:56:36 +0800626
David Hufb38d562019-09-23 15:58:34 +0800627 /*
628 * If it is a NS request via RPC, it is unnecessary to block current
629 * thread.
630 */
631 if (!is_tfm_rpc_msg(msg)) {
632 tfm_event_wait(&msg->ack_evnt);
633 }
Edison Ai764d41f2018-09-21 15:56:36 +0800634
635 return IPC_SUCCESS;
636}
637
Mingyang Sunf3d29892019-07-10 17:50:23 +0800638uint32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800639{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800640 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800641
Kevin Peng79c2bda2020-07-24 16:31:12 +0800642 partition = tfm_spm_get_running_partition();
643 if (partition && partition->static_data) {
644 return partition->static_data->partition_id;
645 } else {
646 return INVALID_PARTITION_ID;
647 }
Edison Ai764d41f2018-09-21 15:56:36 +0800648}
649
Summer Qin43c185d2019-10-10 15:44:42 +0800650int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800651 enum tfm_memory_access_e access,
652 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800653{
Hugues de Valon99578562019-06-18 16:08:51 +0100654 enum tfm_status_e err;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800655
656 /* If len is zero, this indicates an empty buffer and base is ignored */
657 if (len == 0) {
658 return IPC_SUCCESS;
659 }
660
661 if (!buffer) {
662 return IPC_ERROR_BAD_PARAMETERS;
663 }
664
665 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
666 return IPC_ERROR_MEMORY_CHECK;
667 }
668
Summer Qin424d4db2019-03-25 14:09:51 +0800669 if (access == TFM_MEMORY_ACCESS_RW) {
Summer Qineb537e52019-03-29 09:57:10 +0800670 err = tfm_core_has_write_access_to_region(buffer, len, ns_caller,
671 privileged);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800672 } else {
Summer Qineb537e52019-03-29 09:57:10 +0800673 err = tfm_core_has_read_access_to_region(buffer, len, ns_caller,
674 privileged);
Summer Qin424d4db2019-03-25 14:09:51 +0800675 }
Summer Qin0fc3f592019-04-11 16:00:10 +0800676 if (err == TFM_SUCCESS) {
Summer Qin424d4db2019-03-25 14:09:51 +0800677 return IPC_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800678 }
679
680 return IPC_ERROR_MEMORY_CHECK;
681}
682
Ken Liuce2692d2020-02-11 12:39:36 +0800683uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800684{
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800685 uint32_t i, j, num;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800686 struct partition_t *partition;
Summer Qin66f1e032020-01-06 15:40:03 +0800687 struct tfm_core_thread_t *pth, *p_ns_entry_thread = NULL;
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100688 const struct tfm_spm_partition_platform_data_t **platform_data_p;
Edison Ai764d41f2018-09-21 15:56:36 +0800689
690 tfm_pool_init(conn_handle_pool,
691 POOL_BUFFER_SIZE(conn_handle_pool),
692 sizeof(struct tfm_conn_handle_t),
693 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800694
695 /* Init partition first for it will be used when init service */
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200696 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800697 partition = &g_spm_partition_db.partitions[i];
Edison Aif0501702019-10-11 14:36:42 +0800698
Kevin Peng79c2bda2020-07-24 16:31:12 +0800699 if (!partition || !partition->memory_data || !partition->static_data) {
700 tfm_core_panic();
701 }
702
703 if (!(partition->static_data->partition_flags & SPM_PART_FLAG_IPC)) {
704 tfm_core_panic();
705 }
706
Edison Aif0501702019-10-11 14:36:42 +0800707 /* Check if the PSA framework version matches. */
708 if (partition->static_data->psa_framework_version !=
709 PSA_FRAMEWORK_VERSION) {
Kevin Peng79c2bda2020-07-24 16:31:12 +0800710 ERROR_MSG("Warning: PSA Framework Verison does not match!");
Edison Aif0501702019-10-11 14:36:42 +0800711 continue;
712 }
713
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100714 platform_data_p = partition->platform_data_list;
715 if (platform_data_p != NULL) {
716 while ((*platform_data_p) != NULL) {
Edison Ai6be3df12020-02-14 22:14:33 +0800717 if (tfm_spm_hal_configure_default_isolation(i,
718 *platform_data_p) != TFM_PLAT_ERR_SUCCESS) {
719 tfm_core_panic();
720 }
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100721 ++platform_data_p;
722 }
723 }
724
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800725 /* Add PSA_DOORBELL signal to assigned_signals */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800726 partition->signals_allowed |= PSA_DOORBELL;
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800727
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800728 /* TODO: This can be optimized by generating the assigned signal
729 * in code generation time.
730 */
731 for (j = 0; j < tfm_core_irq_signals_count; ++j) {
732 if (tfm_core_irq_signals[j].partition_id ==
733 partition->static_data->partition_id) {
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800734 partition->signals_allowed |=
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800735 tfm_core_irq_signals[j].signal_value;
736 }
737 }
738
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800739 tfm_event_init(&partition->event);
740 tfm_list_init(&partition->msg_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800741
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800742 pth = &partition->sp_thread;
Edison Ai764d41f2018-09-21 15:56:36 +0800743 if (!pth) {
Edison Ai9059ea02019-11-28 13:46:14 +0800744 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800745 }
746
Summer Qin66f1e032020-01-06 15:40:03 +0800747 tfm_core_thrd_init(pth,
Kevin Peng79c2bda2020-07-24 16:31:12 +0800748 (tfm_core_thrd_entry_t)
749 partition->static_data->partition_init,
Summer Qin66f1e032020-01-06 15:40:03 +0800750 NULL,
Kevin Peng79c2bda2020-07-24 16:31:12 +0800751 (uintptr_t)partition->memory_data->stack_top,
752 (uintptr_t)partition->memory_data->stack_bottom);
Edison Ai788bae22019-02-18 17:38:59 +0800753
Kevin Peng79c2bda2020-07-24 16:31:12 +0800754 pth->prior = partition->static_data->partition_priority;
Edison Ai764d41f2018-09-21 15:56:36 +0800755
Ken Liu490281d2019-12-30 15:55:26 +0800756 if (partition->static_data->partition_id == TFM_SP_NON_SECURE_ID) {
757 p_ns_entry_thread = pth;
Ken Liu5248af22019-12-29 12:47:13 +0800758 pth->param = (void *)tfm_spm_hal_get_ns_entry_point();
Ken Liu490281d2019-12-30 15:55:26 +0800759 }
760
Edison Ai764d41f2018-09-21 15:56:36 +0800761 /* Kick off */
Summer Qin66f1e032020-01-06 15:40:03 +0800762 if (tfm_core_thrd_start(pth) != THRD_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800763 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800764 }
765 }
766
767 /* Init Service */
Summer Qind99509f2019-08-02 17:36:58 +0800768 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
Edison Ai764d41f2018-09-21 15:56:36 +0800769 for (i = 0; i < num; i++) {
Summer Qine578c5b2019-08-16 16:42:16 +0800770 service[i].service_db = &service_db[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800771 partition =
Summer Qine578c5b2019-08-16 16:42:16 +0800772 tfm_spm_get_partition_by_id(service[i].service_db->partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800773 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800774 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800775 }
Summer Qind99509f2019-08-02 17:36:58 +0800776 service[i].partition = partition;
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800777 partition->signals_allowed |= service[i].service_db->signal;
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800778
Summer Qind99509f2019-08-02 17:36:58 +0800779 tfm_list_init(&service[i].handle_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800780 }
781
Ken Liu483f5da2019-04-24 10:45:21 +0800782 /*
783 * All threads initialized, start the scheduler.
784 *
785 * NOTE:
Ken Liu490281d2019-12-30 15:55:26 +0800786 * It is worthy to give the thread object to scheduler if the background
787 * context belongs to one of the threads. Here the background thread is the
788 * initialization thread who calls SPM SVC, which re-uses the non-secure
789 * entry thread's stack. After SPM initialization is done, this stack is
790 * cleaned up and the background context is never going to return. Tell
791 * the scheduler that the current thread is non-secure entry thread.
Ken Liu483f5da2019-04-24 10:45:21 +0800792 */
Summer Qin66f1e032020-01-06 15:40:03 +0800793 tfm_core_thrd_start_scheduler(p_ns_entry_thread);
Ken Liuce2692d2020-02-11 12:39:36 +0800794
Summer Qind2ad7e72020-01-06 18:16:35 +0800795 return p_ns_entry_thread->arch_ctx.lr;
Edison Ai764d41f2018-09-21 15:56:36 +0800796}
Ken Liu2d175172019-03-21 17:08:41 +0800797
Summer Qind2ad7e72020-01-06 18:16:35 +0800798void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx)
Ken Liu2d175172019-03-21 17:08:41 +0800799{
800#if TFM_LVL == 2
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800801 struct partition_t *p_next_partition;
Ken Liu2d175172019-03-21 17:08:41 +0800802 uint32_t is_privileged;
803#endif
Summer Qin66f1e032020-01-06 15:40:03 +0800804 struct tfm_core_thread_t *pth_next = tfm_core_thrd_get_next_thread();
805 struct tfm_core_thread_t *pth_curr = tfm_core_thrd_get_curr_thread();
Ken Liu2d175172019-03-21 17:08:41 +0800806
Mate Toth-Pal32b2ccd2019-04-26 10:00:16 +0200807 if (pth_next != NULL && pth_curr != pth_next) {
Ken Liu2d175172019-03-21 17:08:41 +0800808#if TFM_LVL == 2
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800809 p_next_partition = TFM_GET_CONTAINER_PTR(pth_next,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800810 struct partition_t,
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800811 sp_thread);
Ken Liu2d175172019-03-21 17:08:41 +0800812
Summer Qin423dbef2019-08-22 15:59:35 +0800813 if (p_next_partition->static_data->partition_flags &
Ken Liu2d175172019-03-21 17:08:41 +0800814 SPM_PART_FLAG_PSA_ROT) {
815 is_privileged = TFM_PARTITION_PRIVILEGED_MODE;
816 } else {
817 is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE;
818 }
819
820 tfm_spm_partition_change_privilege(is_privileged);
821#endif
Mate Toth-Palc430b992019-05-09 21:01:14 +0200822
Summer Qind2ad7e72020-01-06 18:16:35 +0800823 tfm_core_thrd_switch_context(p_actx, pth_curr, pth_next);
Ken Liu2d175172019-03-21 17:08:41 +0800824 }
David Hufb38d562019-09-23 15:58:34 +0800825
826 /*
827 * Handle pending mailbox message from NS in multi-core topology.
828 * Empty operation on single Armv8-M platform.
829 */
830 tfm_rpc_client_call_handler();
Ken Liu2d175172019-03-21 17:08:41 +0800831}
Mingyang Sund44522a2020-01-16 16:48:37 +0800832
833/*********************** SPM functions for PSA Client APIs *******************/
834
835uint32_t tfm_spm_psa_framework_version(void)
836{
837 return tfm_spm_client_psa_framework_version();
838}
839
840uint32_t tfm_spm_psa_version(uint32_t *args, bool ns_caller)
841{
842 uint32_t sid;
843
844 TFM_CORE_ASSERT(args != NULL);
845 sid = (uint32_t)args[0];
846
847 return tfm_spm_client_psa_version(sid, ns_caller);
848}
849
850psa_status_t tfm_spm_psa_connect(uint32_t *args, bool ns_caller)
851{
852 uint32_t sid;
853 uint32_t version;
854
855 TFM_CORE_ASSERT(args != NULL);
856 sid = (uint32_t)args[0];
857 version = (uint32_t)args[1];
858
859 return tfm_spm_client_psa_connect(sid, version, ns_caller);
860}
861
862psa_status_t tfm_spm_psa_call(uint32_t *args, bool ns_caller, uint32_t lr)
863{
864 psa_handle_t handle;
865 psa_invec *inptr;
866 psa_outvec *outptr;
867 size_t in_num, out_num;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800868 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800869 uint32_t privileged;
870 int32_t type;
871 struct tfm_control_parameter_t ctrl_param;
872
873 TFM_CORE_ASSERT(args != NULL);
874 handle = (psa_handle_t)args[0];
875
876 partition = tfm_spm_get_running_partition();
877 if (!partition) {
878 tfm_core_panic();
879 }
880 privileged = tfm_spm_partition_get_privileged_mode(
881 partition->static_data->partition_flags);
882
883 /*
884 * Read parameters from the arguments. It is a fatal error if the
885 * memory reference for buffer is invalid or not readable.
886 */
887 if (tfm_memory_check((const void *)args[1],
888 sizeof(struct tfm_control_parameter_t), ns_caller,
889 TFM_MEMORY_ACCESS_RW, privileged) != IPC_SUCCESS) {
890 tfm_core_panic();
891 }
892
Summer Qinf24dbb52020-07-23 14:53:54 +0800893 spm_memcpy(&ctrl_param, (const void *)args[1], sizeof(ctrl_param));
Mingyang Sund44522a2020-01-16 16:48:37 +0800894
895 type = ctrl_param.type;
896 in_num = ctrl_param.in_len;
897 out_num = ctrl_param.out_len;
898 inptr = (psa_invec *)args[2];
899 outptr = (psa_outvec *)args[3];
900
901 /* The request type must be zero or positive. */
902 if (type < 0) {
903 tfm_core_panic();
904 }
905
906 return tfm_spm_client_psa_call(handle, type, inptr, in_num, outptr, out_num,
907 ns_caller, privileged);
908}
909
910void tfm_spm_psa_close(uint32_t *args, bool ns_caller)
911{
912 psa_handle_t handle;
913
914 TFM_CORE_ASSERT(args != NULL);
915 handle = args[0];
916
917 tfm_spm_client_psa_close(handle, ns_caller);
918}
919
920uint32_t tfm_spm_get_lifecycle_state(void)
921{
922 /*
923 * FixMe: return PSA_LIFECYCLE_UNKNOWN to the caller directly. It will be
924 * implemented in the future.
925 */
926 return PSA_LIFECYCLE_UNKNOWN;
927}
928
929/********************* SPM functions for PSA Service APIs ********************/
930
931psa_signal_t tfm_spm_psa_wait(uint32_t *args)
932{
933 psa_signal_t signal_mask;
934 uint32_t timeout;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800935 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800936
937 TFM_CORE_ASSERT(args != NULL);
938 signal_mask = (psa_signal_t)args[0];
939 timeout = args[1];
940
941 /*
942 * Timeout[30:0] are reserved for future use.
943 * SPM must ignore the value of RES.
944 */
945 timeout &= PSA_TIMEOUT_MASK;
946
947 partition = tfm_spm_get_running_partition();
948 if (!partition) {
949 tfm_core_panic();
950 }
951
952 /*
953 * It is a PROGRAMMER ERROR if the signal_mask does not include any assigned
954 * signals.
955 */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800956 if ((partition->signals_allowed & signal_mask) == 0) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800957 tfm_core_panic();
958 }
959
960 /*
961 * Expected signals are included in signal wait mask, ignored signals
962 * should not be set and affect caller thread state. Save this mask for
963 * further checking while signals are ready to be set.
964 */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800965 partition->signals_waiting = signal_mask;
Mingyang Sund44522a2020-01-16 16:48:37 +0800966
967 /*
968 * tfm_event_wait() blocks the caller thread if no signals are available.
969 * In this case, the return value of this function is temporary set into
970 * runtime context. After new signal(s) are available, the return value
971 * is updated with the available signal(s) and blocked thread gets to run.
972 */
973 if (timeout == PSA_BLOCK &&
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800974 (partition->signals_asserted & signal_mask) == 0) {
975 tfm_event_wait(&partition->event);
Mingyang Sund44522a2020-01-16 16:48:37 +0800976 }
977
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800978 return partition->signals_asserted & signal_mask;
Mingyang Sund44522a2020-01-16 16:48:37 +0800979}
980
981psa_status_t tfm_spm_psa_get(uint32_t *args)
982{
983 psa_signal_t signal;
984 psa_msg_t *msg = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800985 struct tfm_msg_body_t *tmp_msg = NULL;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800986 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800987 uint32_t privileged;
988
989 TFM_CORE_ASSERT(args != NULL);
990 signal = (psa_signal_t)args[0];
991 msg = (psa_msg_t *)args[1];
992
993 /*
994 * Only one message could be retrieved every time for psa_get(). It is a
995 * fatal error if the input signal has more than a signal bit set.
996 */
Ken Liu410ada52020-01-08 11:37:27 +0800997 if (!tfm_is_one_bit_set(signal)) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800998 tfm_core_panic();
999 }
1000
1001 partition = tfm_spm_get_running_partition();
1002 if (!partition) {
1003 tfm_core_panic();
1004 }
1005 privileged = tfm_spm_partition_get_privileged_mode(
1006 partition->static_data->partition_flags);
1007
1008 /*
1009 * Write the message to the service buffer. It is a fatal error if the
1010 * input msg pointer is not a valid memory reference or not read-write.
1011 */
1012 if (tfm_memory_check(msg, sizeof(psa_msg_t), false, TFM_MEMORY_ACCESS_RW,
1013 privileged) != IPC_SUCCESS) {
1014 tfm_core_panic();
1015 }
1016
1017 /*
1018 * It is a fatal error if the caller call psa_get() when no message has
1019 * been set. The caller must call this function after an RoT Service signal
1020 * is returned by psa_wait().
1021 */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +08001022 if (partition->signals_asserted == 0) {
Mingyang Sund44522a2020-01-16 16:48:37 +08001023 tfm_core_panic();
1024 }
1025
1026 /*
1027 * It is a fatal error if the RoT Service signal is not currently asserted.
1028 */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +08001029 if ((partition->signals_asserted & signal) == 0) {
Mingyang Sund44522a2020-01-16 16:48:37 +08001030 tfm_core_panic();
1031 }
1032
1033 /*
Mingyang Sun73056b62020-07-03 15:18:46 +08001034 * Get message by signal from partition. It is a fatal error if getting
Mingyang Sund44522a2020-01-16 16:48:37 +08001035 * failed, which means the input signal is not correspond to an RoT service.
1036 */
Mingyang Sun73056b62020-07-03 15:18:46 +08001037 tmp_msg = tfm_spm_get_msg_by_signal(partition, signal);
Mingyang Sund44522a2020-01-16 16:48:37 +08001038 if (!tmp_msg) {
1039 return PSA_ERROR_DOES_NOT_EXIST;
1040 }
1041
Ken Liu505b1702020-05-29 13:19:58 +08001042 (TFM_GET_CONTAINER_PTR(tmp_msg,
1043 struct tfm_conn_handle_t,
1044 internal_msg))->status = TFM_HANDLE_STATUS_ACTIVE;
Mingyang Sund44522a2020-01-16 16:48:37 +08001045
Summer Qinf24dbb52020-07-23 14:53:54 +08001046 spm_memcpy(msg, &tmp_msg->msg, sizeof(psa_msg_t));
Mingyang Sund44522a2020-01-16 16:48:37 +08001047
Mingyang Sund44522a2020-01-16 16:48:37 +08001048 return PSA_SUCCESS;
1049}
1050
1051void tfm_spm_psa_set_rhandle(uint32_t *args)
1052{
1053 psa_handle_t msg_handle;
1054 void *rhandle = NULL;
1055 struct tfm_msg_body_t *msg = NULL;
Ken Liu505b1702020-05-29 13:19:58 +08001056 struct tfm_conn_handle_t *conn_handle;
Mingyang Sund44522a2020-01-16 16:48:37 +08001057
1058 TFM_CORE_ASSERT(args != NULL);
1059 msg_handle = (psa_handle_t)args[0];
1060 rhandle = (void *)args[1];
1061
1062 /* It is a fatal error if message handle is invalid */
1063 msg = tfm_spm_get_msg_from_handle(msg_handle);
1064 if (!msg) {
1065 tfm_core_panic();
1066 }
1067
1068 msg->msg.rhandle = rhandle;
Ken Liu505b1702020-05-29 13:19:58 +08001069 conn_handle = tfm_spm_to_handle_instance(msg_handle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001070
1071 /* Store reverse handle for following client calls. */
Ken Liu505b1702020-05-29 13:19:58 +08001072 tfm_spm_set_rhandle(msg->service, conn_handle, rhandle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001073}
1074
1075size_t tfm_spm_psa_read(uint32_t *args)
1076{
1077 psa_handle_t msg_handle;
1078 uint32_t invec_idx;
1079 void *buffer = NULL;
1080 size_t num_bytes;
1081 size_t bytes;
1082 struct tfm_msg_body_t *msg = NULL;
1083 uint32_t privileged;
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001084 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001085
1086 TFM_CORE_ASSERT(args != NULL);
1087 msg_handle = (psa_handle_t)args[0];
1088 invec_idx = args[1];
1089 buffer = (void *)args[2];
1090 num_bytes = (size_t)args[3];
1091
1092 /* It is a fatal error if message handle is invalid */
1093 msg = tfm_spm_get_msg_from_handle(msg_handle);
1094 if (!msg) {
1095 tfm_core_panic();
1096 }
1097
1098 partition = msg->service->partition;
1099 privileged = tfm_spm_partition_get_privileged_mode(
1100 partition->static_data->partition_flags);
1101
1102 /*
1103 * It is a fatal error if message handle does not refer to a request
1104 * message
1105 */
1106 if (msg->msg.type < PSA_IPC_CALL) {
1107 tfm_core_panic();
1108 }
1109
1110 /*
1111 * It is a fatal error if invec_idx is equal to or greater than
1112 * PSA_MAX_IOVEC
1113 */
1114 if (invec_idx >= PSA_MAX_IOVEC) {
1115 tfm_core_panic();
1116 }
1117
1118 /* There was no remaining data in this input vector */
1119 if (msg->msg.in_size[invec_idx] == 0) {
1120 return 0;
1121 }
1122
1123 /*
1124 * Copy the client data to the service buffer. It is a fatal error
1125 * if the memory reference for buffer is invalid or not read-write.
1126 */
1127 if (tfm_memory_check(buffer, num_bytes, false,
1128 TFM_MEMORY_ACCESS_RW, privileged) != IPC_SUCCESS) {
1129 tfm_core_panic();
1130 }
1131
1132 bytes = num_bytes > msg->msg.in_size[invec_idx] ?
1133 msg->msg.in_size[invec_idx] : num_bytes;
1134
Summer Qinf24dbb52020-07-23 14:53:54 +08001135 spm_memcpy(buffer, msg->invec[invec_idx].base, bytes);
Mingyang Sund44522a2020-01-16 16:48:37 +08001136
1137 /* There maybe some remaining data */
1138 msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base + bytes;
1139 msg->msg.in_size[invec_idx] -= bytes;
1140
1141 return bytes;
1142}
1143
1144size_t tfm_spm_psa_skip(uint32_t *args)
1145{
1146 psa_handle_t msg_handle;
1147 uint32_t invec_idx;
1148 size_t num_bytes;
1149 struct tfm_msg_body_t *msg = NULL;
1150
1151 TFM_CORE_ASSERT(args != NULL);
1152 msg_handle = (psa_handle_t)args[0];
1153 invec_idx = args[1];
1154 num_bytes = (size_t)args[2];
1155
1156 /* It is a fatal error if message handle is invalid */
1157 msg = tfm_spm_get_msg_from_handle(msg_handle);
1158 if (!msg) {
1159 tfm_core_panic();
1160 }
1161
1162 /*
1163 * It is a fatal error if message handle does not refer to a request
1164 * message
1165 */
1166 if (msg->msg.type < PSA_IPC_CALL) {
1167 tfm_core_panic();
1168 }
1169
1170 /*
1171 * It is a fatal error if invec_idx is equal to or greater than
1172 * PSA_MAX_IOVEC
1173 */
1174 if (invec_idx >= PSA_MAX_IOVEC) {
1175 tfm_core_panic();
1176 }
1177
1178 /* There was no remaining data in this input vector */
1179 if (msg->msg.in_size[invec_idx] == 0) {
1180 return 0;
1181 }
1182
1183 /*
1184 * If num_bytes is greater than the remaining size of the input vector then
1185 * the remaining size of the input vector is used.
1186 */
1187 if (num_bytes > msg->msg.in_size[invec_idx]) {
1188 num_bytes = msg->msg.in_size[invec_idx];
1189 }
1190
1191 /* There maybe some remaining data */
1192 msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base +
1193 num_bytes;
1194 msg->msg.in_size[invec_idx] -= num_bytes;
1195
1196 return num_bytes;
1197}
1198
1199void tfm_spm_psa_write(uint32_t *args)
1200{
1201 psa_handle_t msg_handle;
1202 uint32_t outvec_idx;
1203 void *buffer = NULL;
1204 size_t num_bytes;
1205 struct tfm_msg_body_t *msg = NULL;
1206 uint32_t privileged;
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001207 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001208
1209 TFM_CORE_ASSERT(args != NULL);
1210 msg_handle = (psa_handle_t)args[0];
1211 outvec_idx = args[1];
1212 buffer = (void *)args[2];
1213 num_bytes = (size_t)args[3];
1214
1215 /* It is a fatal error if message handle is invalid */
1216 msg = tfm_spm_get_msg_from_handle(msg_handle);
1217 if (!msg) {
1218 tfm_core_panic();
1219 }
1220
1221 partition = msg->service->partition;
1222 privileged = tfm_spm_partition_get_privileged_mode(
1223 partition->static_data->partition_flags);
1224
1225 /*
1226 * It is a fatal error if message handle does not refer to a request
1227 * message
1228 */
1229 if (msg->msg.type < PSA_IPC_CALL) {
1230 tfm_core_panic();
1231 }
1232
1233 /*
1234 * It is a fatal error if outvec_idx is equal to or greater than
1235 * PSA_MAX_IOVEC
1236 */
1237 if (outvec_idx >= PSA_MAX_IOVEC) {
1238 tfm_core_panic();
1239 }
1240
1241 /*
1242 * It is a fatal error if the call attempts to write data past the end of
1243 * the client output vector
1244 */
1245 if (num_bytes > msg->msg.out_size[outvec_idx] -
1246 msg->outvec[outvec_idx].len) {
1247 tfm_core_panic();
1248 }
1249
1250 /*
1251 * Copy the service buffer to client outvecs. It is a fatal error
1252 * if the memory reference for buffer is invalid or not readable.
1253 */
1254 if (tfm_memory_check(buffer, num_bytes, false,
1255 TFM_MEMORY_ACCESS_RO, privileged) != IPC_SUCCESS) {
1256 tfm_core_panic();
1257 }
1258
Summer Qinf24dbb52020-07-23 14:53:54 +08001259 spm_memcpy((char *)msg->outvec[outvec_idx].base +
1260 msg->outvec[outvec_idx].len, buffer, num_bytes);
Mingyang Sund44522a2020-01-16 16:48:37 +08001261
1262 /* Update the write number */
1263 msg->outvec[outvec_idx].len += num_bytes;
1264}
1265
1266static void update_caller_outvec_len(struct tfm_msg_body_t *msg)
1267{
1268 uint32_t i;
1269
1270 /*
1271 * FixeMe: abstract these part into dedicated functions to avoid
1272 * accessing thread context in psa layer
1273 */
1274 /* If it is a NS request via RPC, the owner of this message is not set */
1275 if (!is_tfm_rpc_msg(msg)) {
1276 TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK);
1277 }
1278
1279 for (i = 0; i < PSA_MAX_IOVEC; i++) {
1280 if (msg->msg.out_size[i] == 0) {
1281 continue;
1282 }
1283
1284 TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
1285
1286 msg->caller_outvec[i].len = msg->outvec[i].len;
1287 }
1288}
1289
1290void tfm_spm_psa_reply(uint32_t *args)
1291{
1292 psa_handle_t msg_handle;
1293 psa_status_t status;
1294 struct tfm_spm_service_t *service = NULL;
1295 struct tfm_msg_body_t *msg = NULL;
1296 int32_t ret = PSA_SUCCESS;
Ken Liu505b1702020-05-29 13:19:58 +08001297 struct tfm_conn_handle_t *conn_handle;
Mingyang Sund44522a2020-01-16 16:48:37 +08001298
1299 TFM_CORE_ASSERT(args != NULL);
1300 msg_handle = (psa_handle_t)args[0];
1301 status = (psa_status_t)args[1];
1302
1303 /* It is a fatal error if message handle is invalid */
1304 msg = tfm_spm_get_msg_from_handle(msg_handle);
1305 if (!msg) {
1306 tfm_core_panic();
1307 }
1308
1309 /*
1310 * RoT Service information is needed in this function, stored it in message
1311 * body structure. Only two parameters are passed in this function: handle
1312 * and status, so it is useful and simply to do like this.
1313 */
1314 service = msg->service;
1315 if (!service) {
1316 tfm_core_panic();
1317 }
1318
1319 /*
1320 * Three type of message are passed in this function: CONNECTION, REQUEST,
1321 * DISCONNECTION. It needs to process differently for each type.
1322 */
Ken Liu505b1702020-05-29 13:19:58 +08001323 conn_handle = tfm_spm_to_handle_instance(msg_handle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001324 switch (msg->msg.type) {
1325 case PSA_IPC_CONNECT:
1326 /*
1327 * Reply to PSA_IPC_CONNECT message. Connect handle is returned if the
1328 * input status is PSA_SUCCESS. Others return values are based on the
1329 * input status.
1330 */
1331 if (status == PSA_SUCCESS) {
Ken Liu505b1702020-05-29 13:19:58 +08001332 ret = msg_handle;
Mingyang Sund44522a2020-01-16 16:48:37 +08001333 } else if (status == PSA_ERROR_CONNECTION_REFUSED) {
1334 /* Refuse the client connection, indicating a permanent error. */
Ken Liu505b1702020-05-29 13:19:58 +08001335 tfm_spm_free_conn_handle(service, conn_handle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001336 ret = PSA_ERROR_CONNECTION_REFUSED;
1337 } else if (status == PSA_ERROR_CONNECTION_BUSY) {
1338 /* Fail the client connection, indicating a transient error. */
1339 ret = PSA_ERROR_CONNECTION_BUSY;
1340 } else {
1341 tfm_core_panic();
1342 }
1343 break;
1344 case PSA_IPC_DISCONNECT:
1345 /* Service handle is not used anymore */
Ken Liu505b1702020-05-29 13:19:58 +08001346 tfm_spm_free_conn_handle(service, conn_handle);
Mingyang Sund44522a2020-01-16 16:48:37 +08001347
1348 /*
1349 * If the message type is PSA_IPC_DISCONNECT, then the status code is
1350 * ignored
1351 */
1352 break;
1353 default:
1354 if (msg->msg.type >= PSA_IPC_CALL) {
1355 /* Reply to a request message. Return values are based on status */
1356 ret = status;
1357 /*
1358 * The total number of bytes written to a single parameter must be
1359 * reported to the client by updating the len member of the
1360 * psa_outvec structure for the parameter before returning from
1361 * psa_call().
1362 */
1363 update_caller_outvec_len(msg);
1364 } else {
1365 tfm_core_panic();
1366 }
1367 }
1368
1369 if (ret == PSA_ERROR_PROGRAMMER_ERROR) {
1370 /*
1371 * If the source of the programmer error is a Secure Partition, the SPM
1372 * must panic the Secure Partition in response to a PROGRAMMER ERROR.
1373 */
1374 if (TFM_CLIENT_ID_IS_NS(msg->msg.client_id)) {
Ken Liu505b1702020-05-29 13:19:58 +08001375 conn_handle->status = TFM_HANDLE_STATUS_CONNECT_ERROR;
Mingyang Sund44522a2020-01-16 16:48:37 +08001376 } else {
1377 tfm_core_panic();
1378 }
1379 } else {
Ken Liu505b1702020-05-29 13:19:58 +08001380 conn_handle->status = TFM_HANDLE_STATUS_IDLE;
Mingyang Sund44522a2020-01-16 16:48:37 +08001381 }
1382
1383 if (is_tfm_rpc_msg(msg)) {
1384 tfm_rpc_client_call_reply(msg, ret);
1385 } else {
1386 tfm_event_wake(&msg->ack_evnt, ret);
1387 }
1388}
1389
1390/**
1391 * \brief notify the partition with the signal.
1392 *
1393 * \param[in] partition_id The ID of the partition to be notified.
1394 * \param[in] signal The signal that the partition is to be notified
1395 * with.
1396 *
1397 * \retval void Success.
1398 * \retval "Does not return" If partition_id is invalid.
1399 */
1400static void notify_with_signal(int32_t partition_id, psa_signal_t signal)
1401{
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001402 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001403
1404 /*
1405 * The value of partition_id must be greater than zero as the target of
1406 * notification must be a Secure Partition, providing a Non-secure
1407 * Partition ID is a fatal error.
1408 */
1409 if (!TFM_CLIENT_ID_IS_S(partition_id)) {
1410 tfm_core_panic();
1411 }
1412
1413 /*
1414 * It is a fatal error if partition_id does not correspond to a Secure
1415 * Partition.
1416 */
1417 partition = tfm_spm_get_partition_by_id(partition_id);
1418 if (!partition) {
1419 tfm_core_panic();
1420 }
1421
Mingyang Sunaf22ffa2020-07-09 17:48:37 +08001422 partition->signals_asserted |= signal;
Mingyang Sund44522a2020-01-16 16:48:37 +08001423
1424 /*
1425 * The target partition may be blocked with waiting for signals after
1426 * called psa_wait(). Set the return value with the available signals
1427 * before wake it up with tfm_event_signal().
1428 */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +08001429 tfm_event_wake(&partition->event,
1430 partition->signals_asserted & partition->signals_waiting);
Mingyang Sund44522a2020-01-16 16:48:37 +08001431}
1432
1433void tfm_spm_psa_notify(uint32_t *args)
1434{
1435 int32_t partition_id;
1436
1437 TFM_CORE_ASSERT(args != NULL);
1438 partition_id = (int32_t)args[0];
1439
1440 notify_with_signal(partition_id, PSA_DOORBELL);
1441}
1442
1443/**
1444 * \brief assert signal for a given IRQ line.
1445 *
1446 * \param[in] partition_id The ID of the partition which handles this IRQ
1447 * \param[in] signal The signal associated with this IRQ
1448 * \param[in] irq_line The number of the IRQ line
1449 *
1450 * \retval void Success.
1451 * \retval "Does not return" Partition ID is invalid
1452 */
1453void tfm_irq_handler(uint32_t partition_id, psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +01001454 IRQn_Type irq_line)
Mingyang Sund44522a2020-01-16 16:48:37 +08001455{
1456 tfm_spm_hal_disable_irq(irq_line);
1457 notify_with_signal(partition_id, signal);
1458}
1459
1460void tfm_spm_psa_clear(void)
1461{
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001462 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001463
1464 partition = tfm_spm_get_running_partition();
1465 if (!partition) {
1466 tfm_core_panic();
1467 }
1468
1469 /*
1470 * It is a fatal error if the Secure Partition's doorbell signal is not
1471 * currently asserted.
1472 */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +08001473 if ((partition->signals_asserted & PSA_DOORBELL) == 0) {
Mingyang Sund44522a2020-01-16 16:48:37 +08001474 tfm_core_panic();
1475 }
Mingyang Sunaf22ffa2020-07-09 17:48:37 +08001476 partition->signals_asserted &= ~PSA_DOORBELL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001477}
1478
1479void tfm_spm_psa_panic(void)
1480{
1481 /*
1482 * PSA FF recommends that the SPM causes the system to restart when a secure
1483 * partition panics.
1484 */
Summer Qinbce21132020-08-19 14:28:10 +08001485 tfm_hal_system_reset();
Mingyang Sund44522a2020-01-16 16:48:37 +08001486}
1487
1488/**
1489 * \brief Return the IRQ line number associated with a signal
1490 *
1491 * \param[in] partition_id The ID of the partition in which we look for
1492 * the signal.
1493 * \param[in] signal The signal we do the query for.
1494 * \param[out] irq_line The irq line associated with signal
1495 *
1496 * \retval IPC_SUCCESS Execution successful, irq_line contains a valid
1497 * value.
1498 * \retval IPC_ERROR_GENERIC There was an error finding the IRQ line for the
1499 * signal. irq_line is unchanged.
1500 */
1501static int32_t get_irq_line_for_signal(int32_t partition_id,
1502 psa_signal_t signal,
TTornblomfaf74f52020-03-04 17:56:27 +01001503 IRQn_Type *irq_line)
Mingyang Sund44522a2020-01-16 16:48:37 +08001504{
1505 size_t i;
1506
1507 for (i = 0; i < tfm_core_irq_signals_count; ++i) {
1508 if (tfm_core_irq_signals[i].partition_id == partition_id &&
1509 tfm_core_irq_signals[i].signal_value == signal) {
1510 *irq_line = tfm_core_irq_signals[i].irq_line;
1511 return IPC_SUCCESS;
1512 }
1513 }
1514 return IPC_ERROR_GENERIC;
1515}
1516
1517void tfm_spm_psa_eoi(uint32_t *args)
1518{
1519 psa_signal_t irq_signal;
TTornblomfaf74f52020-03-04 17:56:27 +01001520 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001521 int32_t ret;
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001522 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001523
1524 TFM_CORE_ASSERT(args != NULL);
1525 irq_signal = (psa_signal_t)args[0];
1526
1527 /* It is a fatal error if passed signal indicates more than one signals. */
1528 if (!tfm_is_one_bit_set(irq_signal)) {
1529 tfm_core_panic();
1530 }
1531
1532 partition = tfm_spm_get_running_partition();
1533 if (!partition) {
1534 tfm_core_panic();
1535 }
1536
1537 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1538 irq_signal, &irq_line);
1539 /* It is a fatal error if passed signal is not an interrupt signal. */
1540 if (ret != IPC_SUCCESS) {
1541 tfm_core_panic();
1542 }
1543
1544 /* It is a fatal error if passed signal is not currently asserted */
Mingyang Sunaf22ffa2020-07-09 17:48:37 +08001545 if ((partition->signals_asserted & irq_signal) == 0) {
Mingyang Sund44522a2020-01-16 16:48:37 +08001546 tfm_core_panic();
1547 }
1548
Mingyang Sunaf22ffa2020-07-09 17:48:37 +08001549 partition->signals_asserted &= ~irq_signal;
Mingyang Sund44522a2020-01-16 16:48:37 +08001550
1551 tfm_spm_hal_clear_pending_irq(irq_line);
1552 tfm_spm_hal_enable_irq(irq_line);
1553}
1554
1555void tfm_spm_enable_irq(uint32_t *args)
1556{
1557 struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args;
1558 psa_signal_t irq_signal = svc_ctx->r0;
TTornblomfaf74f52020-03-04 17:56:27 +01001559 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001560 int32_t ret;
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001561 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001562
1563 /* It is a fatal error if passed signal indicates more than one signals. */
1564 if (!tfm_is_one_bit_set(irq_signal)) {
1565 tfm_core_panic();
1566 }
1567
1568 partition = tfm_spm_get_running_partition();
1569 if (!partition) {
1570 tfm_core_panic();
1571 }
1572
1573 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1574 irq_signal, &irq_line);
1575 /* It is a fatal error if passed signal is not an interrupt signal. */
1576 if (ret != IPC_SUCCESS) {
1577 tfm_core_panic();
1578 }
1579
1580 tfm_spm_hal_enable_irq(irq_line);
1581}
1582
1583void tfm_spm_disable_irq(uint32_t *args)
1584{
1585 struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args;
1586 psa_signal_t irq_signal = svc_ctx->r0;
TTornblomfaf74f52020-03-04 17:56:27 +01001587 IRQn_Type irq_line = (IRQn_Type) 0;
Mingyang Sund44522a2020-01-16 16:48:37 +08001588 int32_t ret;
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001589 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +08001590
1591 /* It is a fatal error if passed signal indicates more than one signals. */
1592 if (!tfm_is_one_bit_set(irq_signal)) {
1593 tfm_core_panic();
1594 }
1595
1596 partition = tfm_spm_get_running_partition();
1597 if (!partition) {
1598 tfm_core_panic();
1599 }
1600
1601 ret = get_irq_line_for_signal(partition->static_data->partition_id,
1602 irq_signal, &irq_line);
1603 /* It is a fatal error if passed signal is not an interrupt signal. */
1604 if (ret != IPC_SUCCESS) {
1605 tfm_core_panic();
1606 }
1607
1608 tfm_spm_hal_disable_irq(irq_line);
1609}
1610
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001611void tfm_spm_validate_caller(struct partition_t *p_cur_sp, uint32_t *p_ctx,
1612 uint32_t exc_return, bool ns_caller)
Mingyang Sund44522a2020-01-16 16:48:37 +08001613{
1614 uintptr_t stacked_ctx_pos;
1615
1616 if (ns_caller) {
1617 /*
1618 * The background IRQ can't be supported, since if SP is executing,
1619 * the preempted context of SP can be different with the one who
1620 * preempts veneer.
1621 */
1622 if (p_cur_sp->static_data->partition_id != TFM_SP_NON_SECURE_ID) {
1623 tfm_core_panic();
1624 }
1625
1626 /*
1627 * It is non-secure caller, check if veneer stack contains
1628 * multiple contexts.
1629 */
1630 stacked_ctx_pos = (uintptr_t)p_ctx +
1631 sizeof(struct tfm_state_context_t) +
1632 TFM_VENEER_STACK_GUARD_SIZE;
1633
1634 if (is_stack_alloc_fp_space(exc_return)) {
1635#if defined (__FPU_USED) && (__FPU_USED == 1U)
1636 if (FPU->FPCCR & FPU_FPCCR_TS_Msk) {
1637 stacked_ctx_pos += TFM_ADDTIONAL_FP_CONTEXT_WORDS *
1638 sizeof(uint32_t);
1639 }
1640#endif
1641 stacked_ctx_pos += TFM_BASIC_FP_CONTEXT_WORDS * sizeof(uint32_t);
1642 }
1643
Mingyang Sunaf22ffa2020-07-09 17:48:37 +08001644 if (stacked_ctx_pos != p_cur_sp->sp_thread.stk_top) {
Mingyang Sund44522a2020-01-16 16:48:37 +08001645 tfm_core_panic();
1646 }
1647 } else if (p_cur_sp->static_data->partition_id <= 0) {
1648 tfm_core_panic();
1649 }
1650}
Summer Qin830c5542020-02-14 13:44:20 +08001651
1652void tfm_spm_request_handler(const struct tfm_state_context_t *svc_ctx)
1653{
1654 uint32_t *res_ptr = (uint32_t *)&svc_ctx->r0;
1655 uint32_t running_partition_flags = 0;
Mingyang Sunae70d8d2020-06-30 15:56:05 +08001656 const struct partition_t *partition = NULL;
Summer Qin830c5542020-02-14 13:44:20 +08001657
1658 /* Check permissions on request type basis */
1659
1660 switch (svc_ctx->r0) {
1661 case TFM_SPM_REQUEST_RESET_VOTE:
1662 partition = tfm_spm_get_running_partition();
1663 if (!partition) {
1664 tfm_core_panic();
1665 }
1666 running_partition_flags = partition->static_data->partition_flags;
1667
1668 /* Currently only PSA Root of Trust services are allowed to make Reset
1669 * vote request
1670 */
1671 if ((running_partition_flags & SPM_PART_FLAG_PSA_ROT) == 0) {
1672 *res_ptr = (uint32_t)TFM_ERROR_GENERIC;
1673 }
1674
1675 /* FixMe: this is a placeholder for checks to be performed before
1676 * allowing execution of reset
1677 */
1678 *res_ptr = (uint32_t)TFM_SUCCESS;
1679
1680 break;
1681 default:
1682 *res_ptr = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
1683 }
1684}
Mingyang Sunbd7ceb52020-06-11 16:53:03 +08001685
1686enum spm_err_t tfm_spm_db_init(void)
1687{
1688 uint32_t i;
1689
1690 /* This function initialises partition db */
1691
1692 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
1693 g_spm_partition_db.partitions[i].static_data = &static_data_list[i];
1694 g_spm_partition_db.partitions[i].platform_data_list =
1695 platform_data_list_list[i];
1696 g_spm_partition_db.partitions[i].memory_data = &memory_data_list[i];
1697 }
1698 g_spm_partition_db.is_init = 1;
1699
1700 return SPM_ERR_OK;
1701}