blob: 98f7a0efb7c027d0ec90b486e901e9d090ad430e [file] [log] [blame]
Edison Ai764d41f2018-09-21 15:56:36 +08001/*
2 * Copyright (c) 2018-2019, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
Mingyang Sunda01a972019-07-12 17:32:59 +08007
8/* All the APIs defined in this file are used for IPC model. */
9
Edison Ai764d41f2018-09-21 15:56:36 +080010#include <inttypes.h>
Summer Qin2bfd2a02018-09-26 17:10:41 +080011#include <limits.h>
Edison Ai764d41f2018-09-21 15:56:36 +080012#include <stdbool.h>
Edison Ai764d41f2018-09-21 15:56:36 +080013#include <stdlib.h>
Jamie Foxcc31d402019-01-28 17:13:52 +000014#include "psa/client.h"
15#include "psa/service.h"
Edison Ai764d41f2018-09-21 15:56:36 +080016#include "tfm_utils.h"
Mingyang Sunf3d29892019-07-10 17:50:23 +080017#include "tfm_spm_hal.h"
Edison Ai764d41f2018-09-21 15:56:36 +080018#include "spm_api.h"
19#include "spm_db.h"
David Hu326f3972019-08-06 14:16:51 +080020#include "tfm_core_mem_check.h"
Edison Ai764d41f2018-09-21 15:56:36 +080021#include "tfm_internal_defines.h"
22#include "tfm_wait.h"
23#include "tfm_message_queue.h"
24#include "tfm_list.h"
25#include "tfm_pools.h"
Edison Ai764d41f2018-09-21 15:56:36 +080026#include "tfm_thread.h"
Summer Qin2bfd2a02018-09-26 17:10:41 +080027#include "region_defs.h"
Edison Ai764d41f2018-09-21 15:56:36 +080028#include "tfm_nspm.h"
Edison Ai807fedb2019-03-07 11:22:03 +080029#include "tfm_memory_utils.h"
Mingyang Sun94b1b412019-09-20 15:11:14 +080030#include "tfm_core_utils.h"
Edison Ai764d41f2018-09-21 15:56:36 +080031
Summer Qind99509f2019-08-02 17:36:58 +080032#include "secure_fw/services/tfm_service_list.inc"
33
34/* Extern service variable */
35extern struct tfm_spm_service_t service[];
Summer Qine578c5b2019-08-16 16:42:16 +080036extern const struct tfm_spm_service_db_t service_db[];
Summer Qind99509f2019-08-02 17:36:58 +080037
Edison Ai764d41f2018-09-21 15:56:36 +080038/* Extern SPM variable */
39extern struct spm_partition_db_t g_spm_partition_db;
40
Mate Toth-Palc430b992019-05-09 21:01:14 +020041/* Extern secure lock variable */
42extern int32_t tfm_secure_lock;
Mingyang Sunf3d29892019-07-10 17:50:23 +080043
Edison Ai764d41f2018-09-21 15:56:36 +080044/* Pools */
45TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t),
46 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +080047
Edison Ai764d41f2018-09-21 15:56:36 +080048/********************** SPM functions for handler mode ***********************/
49
50/* Service handle management functions */
51psa_handle_t tfm_spm_create_conn_handle(struct tfm_spm_service_t *service)
52{
Edison Ai9cc26242019-08-06 11:28:04 +080053 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +080054
55 TFM_ASSERT(service);
56
57 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +080058 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
59 if (!p_handle) {
Edison Ai764d41f2018-09-21 15:56:36 +080060 return PSA_NULL_HANDLE;
61 }
62
Edison Ai9cc26242019-08-06 11:28:04 +080063 p_handle->service = service;
Edison Ai764d41f2018-09-21 15:56:36 +080064
65 /* Add handle node to list for next psa functions */
Edison Ai9cc26242019-08-06 11:28:04 +080066 tfm_list_add_tail(&service->handle_list, &p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +080067
Edison Ai9cc26242019-08-06 11:28:04 +080068 return (psa_handle_t)p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +080069}
70
71static struct tfm_conn_handle_t *
Mingyang Sun5e13aa72019-07-10 10:30:16 +080072 tfm_spm_find_conn_handle_node(struct tfm_spm_service_t *service,
73 psa_handle_t conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +080074{
Edison Ai764d41f2018-09-21 15:56:36 +080075 TFM_ASSERT(service);
76
Edison Ai9cc26242019-08-06 11:28:04 +080077 return (struct tfm_conn_handle_t *)conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +080078}
79
80int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service,
81 psa_handle_t conn_handle)
82{
Edison Ai9cc26242019-08-06 11:28:04 +080083 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +080084
85 TFM_ASSERT(service);
86
87 /* There are many handles for each RoT Service */
Edison Ai9cc26242019-08-06 11:28:04 +080088 p_handle = tfm_spm_find_conn_handle_node(service, conn_handle);
89 if (!p_handle) {
Edison Ai764d41f2018-09-21 15:56:36 +080090 tfm_panic();
91 }
92
Mate Toth-Pala4b5d242019-09-23 09:14:47 +020093 /* Clear magic as the handler is not used anymore */
94 p_handle->internal_msg.magic = 0;
95
Edison Ai764d41f2018-09-21 15:56:36 +080096 /* Remove node from handle list */
Edison Ai9cc26242019-08-06 11:28:04 +080097 tfm_list_del_node(&p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +080098
99 /* Back handle buffer to pool */
Edison Ai9cc26242019-08-06 11:28:04 +0800100 tfm_pool_free(p_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800101 return IPC_SUCCESS;
102}
103
104int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service,
105 psa_handle_t conn_handle,
106 void *rhandle)
107{
Edison Ai9cc26242019-08-06 11:28:04 +0800108 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800109
110 TFM_ASSERT(service);
111 /* Set reverse handle value only be allowed for a connected handle */
112 TFM_ASSERT(conn_handle != PSA_NULL_HANDLE);
113
114 /* There are many handles for each RoT Service */
Edison Ai9cc26242019-08-06 11:28:04 +0800115 p_handle = tfm_spm_find_conn_handle_node(service, conn_handle);
116 if (!p_handle) {
Edison Ai764d41f2018-09-21 15:56:36 +0800117 tfm_panic();
118 }
119
Edison Ai9cc26242019-08-06 11:28:04 +0800120 p_handle->rhandle = rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800121 return IPC_SUCCESS;
122}
123
124void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service,
125 psa_handle_t conn_handle)
126{
Edison Ai9cc26242019-08-06 11:28:04 +0800127 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800128
129 TFM_ASSERT(service);
130 /* Get reverse handle value only be allowed for a connected handle */
131 TFM_ASSERT(conn_handle != PSA_NULL_HANDLE);
132
133 /* There are many handles for each RoT Service */
Edison Ai9cc26242019-08-06 11:28:04 +0800134 p_handle = tfm_spm_find_conn_handle_node(service, conn_handle);
135 if (!p_handle) {
Edison Ai764d41f2018-09-21 15:56:36 +0800136 tfm_panic();
137 }
138
Edison Ai9cc26242019-08-06 11:28:04 +0800139 return p_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800140}
141
142/* Partition management functions */
143struct tfm_spm_service_t *
Mingyang Sunf3d29892019-07-10 17:50:23 +0800144 tfm_spm_get_service_by_signal(struct spm_partition_desc_t *partition,
145 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800146{
147 struct tfm_list_node_t *node, *head;
148 struct tfm_spm_service_t *service;
149
150 TFM_ASSERT(partition);
151
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800152 if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
Edison Ai764d41f2018-09-21 15:56:36 +0800153 tfm_panic();
154 }
155
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800156 head = &partition->runtime_data.service_list;
Edison Ai764d41f2018-09-21 15:56:36 +0800157 TFM_LIST_FOR_EACH(node, head) {
158 service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t, list);
Summer Qine578c5b2019-08-16 16:42:16 +0800159 if (service->service_db->signal == signal) {
Edison Ai764d41f2018-09-21 15:56:36 +0800160 return service;
161 }
162 }
163 return NULL;
164}
165
166struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid)
167{
168 uint32_t i;
169 struct tfm_list_node_t *node, *head;
170 struct tfm_spm_service_t *service;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800171 struct spm_partition_desc_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800172
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200173 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800174 partition = &g_spm_partition_db.partitions[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800175 /* Skip partition without IPC flag */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800176 if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
Edison Ai764d41f2018-09-21 15:56:36 +0800177 continue;
178 }
179
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800180 if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
Edison Ai764d41f2018-09-21 15:56:36 +0800181 continue;
182 }
183
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800184 head = &partition->runtime_data.service_list;
Edison Ai764d41f2018-09-21 15:56:36 +0800185 TFM_LIST_FOR_EACH(node, head) {
186 service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t,
187 list);
Summer Qine578c5b2019-08-16 16:42:16 +0800188 if (service->service_db->sid == sid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800189 return service;
190 }
191 }
192 }
193 return NULL;
194}
195
196struct tfm_spm_service_t *
197 tfm_spm_get_service_by_handle(psa_handle_t conn_handle)
198{
Edison Ai9cc26242019-08-06 11:28:04 +0800199 return ((struct tfm_conn_handle_t *)conn_handle)->service;
Edison Ai764d41f2018-09-21 15:56:36 +0800200}
201
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800202struct spm_partition_desc_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800203{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800204 uint32_t idx = get_partition_idx(partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800205
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800206 if (idx != SPM_INVALID_PARTITION_IDX) {
207 return &(g_spm_partition_db.partitions[idx]);
Edison Ai764d41f2018-09-21 15:56:36 +0800208 }
209 return NULL;
210}
211
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800212struct spm_partition_desc_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800213{
214 uint32_t spid;
215
Mingyang Sunf3d29892019-07-10 17:50:23 +0800216 spid = tfm_spm_partition_get_running_partition_id();
Edison Ai764d41f2018-09-21 15:56:36 +0800217
218 return tfm_spm_get_partition_by_id(spid);
219}
220
221int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
222 uint32_t minor_version)
223{
224 TFM_ASSERT(service);
225
Summer Qine578c5b2019-08-16 16:42:16 +0800226 switch (service->service_db->minor_policy) {
Edison Ai764d41f2018-09-21 15:56:36 +0800227 case TFM_VERSION_POLICY_RELAXED:
Summer Qine578c5b2019-08-16 16:42:16 +0800228 if (minor_version > service->service_db->minor_version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800229 return IPC_ERROR_VERSION;
230 }
231 break;
232 case TFM_VERSION_POLICY_STRICT:
Summer Qine578c5b2019-08-16 16:42:16 +0800233 if (minor_version != service->service_db->minor_version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800234 return IPC_ERROR_VERSION;
235 }
236 break;
237 default:
238 return IPC_ERROR_VERSION;
239 }
240 return IPC_SUCCESS;
241}
242
243/* Message functions */
244struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
245{
246 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200247 * The message handler passed by the caller is considered invalid in the
248 * following cases:
249 * 1. Not a valid message handle. (The address of a message is not the
250 * address of a possible handle from the pool
251 * 2. Handle not belongs to the caller partition (The handle is either
252 * unused, or owned by anither partition)
253 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800254 */
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200255 struct tfm_conn_handle_t *connection_handle_address;
Edison Ai764d41f2018-09-21 15:56:36 +0800256 struct tfm_msg_body_t *msg;
257 uint32_t partition_id;
258
259 msg = (struct tfm_msg_body_t *)msg_handle;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200260
261 connection_handle_address =
262 TFM_GET_CONTAINER_PTR(msg, struct tfm_conn_handle_t, internal_msg);
263
264 if (is_valid_chunk_data_in_pool(
265 conn_handle_pool, (uint8_t *)connection_handle_address) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800266 return NULL;
267 }
268
269 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200270 * Check that the magic number is correct. This proves that the message
271 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800272 */
273 if (msg->magic != TFM_MSG_MAGIC) {
274 return NULL;
275 }
276
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200277 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800278 partition_id = tfm_spm_partition_get_running_partition_id();
Summer Qin423dbef2019-08-22 15:59:35 +0800279 if (partition_id != msg->service->partition->static_data->partition_id) {
Edison Ai764d41f2018-09-21 15:56:36 +0800280 return NULL;
281 }
282
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200283 /*
284 * FixMe: For condition 1 it should be checked whether the message belongs
285 * to the service. Skipping this check isn't a security risk as even if the
286 * message belongs to another service, the handle belongs to the calling
287 * partition.
288 */
289
Edison Ai764d41f2018-09-21 15:56:36 +0800290 return msg;
291}
292
Edison Ai97115822019-08-01 14:22:19 +0800293struct tfm_msg_body_t *
294 tfm_spm_get_msg_buffer_from_conn_handle(psa_handle_t conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800295{
Edison Ai97115822019-08-01 14:22:19 +0800296 TFM_ASSERT(conn_handle != PSA_NULL_HANDLE);
297
298 return &(((struct tfm_conn_handle_t *)conn_handle)->internal_msg);
299}
300
301void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
302 struct tfm_spm_service_t *service,
303 psa_handle_t handle,
304 int32_t type, int32_t ns_caller,
305 psa_invec *invec, size_t in_len,
306 psa_outvec *outvec, size_t out_len,
307 psa_outvec *caller_outvec)
308{
Edison Ai764d41f2018-09-21 15:56:36 +0800309 uint32_t i;
310
Edison Ai97115822019-08-01 14:22:19 +0800311 TFM_ASSERT(msg);
Edison Ai764d41f2018-09-21 15:56:36 +0800312 TFM_ASSERT(service);
313 TFM_ASSERT(!(invec == NULL && in_len != 0));
314 TFM_ASSERT(!(outvec == NULL && out_len != 0));
315 TFM_ASSERT(in_len <= PSA_MAX_IOVEC);
316 TFM_ASSERT(out_len <= PSA_MAX_IOVEC);
317 TFM_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
318
Edison Ai764d41f2018-09-21 15:56:36 +0800319 /* Clear message buffer before using it */
Mingyang Sun94b1b412019-09-20 15:11:14 +0800320 tfm_core_util_memset(msg, 0, sizeof(struct tfm_msg_body_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800321
Ken Liu35f89392019-03-14 14:51:05 +0800322 tfm_event_init(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800323 msg->magic = TFM_MSG_MAGIC;
324 msg->service = service;
325 msg->handle = handle;
326 msg->caller_outvec = caller_outvec;
327 /* Get current partition id */
328 if (ns_caller) {
329 msg->msg.client_id = tfm_nspm_get_current_client_id();
330 } else {
Mingyang Sunf3d29892019-07-10 17:50:23 +0800331 msg->msg.client_id = tfm_spm_partition_get_running_partition_id();
Edison Ai764d41f2018-09-21 15:56:36 +0800332 }
333
334 /* Copy contents */
335 msg->msg.type = type;
336
337 for (i = 0; i < in_len; i++) {
338 msg->msg.in_size[i] = invec[i].len;
339 msg->invec[i].base = invec[i].base;
340 }
341
342 for (i = 0; i < out_len; i++) {
343 msg->msg.out_size[i] = outvec[i].len;
344 msg->outvec[i].base = outvec[i].base;
345 /* Out len is used to record the writed number, set 0 here again */
346 msg->outvec[i].len = 0;
347 }
348
349 /* Use message address as handle */
350 msg->msg.handle = (psa_handle_t)msg;
351
352 /* For connected handle, set rhandle to every message */
353 if (handle != PSA_NULL_HANDLE) {
354 msg->msg.rhandle = tfm_spm_get_rhandle(service, handle);
355 }
Edison Ai764d41f2018-09-21 15:56:36 +0800356}
357
358int32_t tfm_spm_send_event(struct tfm_spm_service_t *service,
359 struct tfm_msg_body_t *msg)
360{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800361 struct spm_partition_runtime_data_t *p_runtime_data =
362 &service->partition->runtime_data;
363
Edison Ai764d41f2018-09-21 15:56:36 +0800364 TFM_ASSERT(service);
365 TFM_ASSERT(msg);
366
367 /* Enqueue message to service message queue */
368 if (tfm_msg_enqueue(&service->msg_queue, msg) != IPC_SUCCESS) {
369 return IPC_ERROR_GENERIC;
370 }
371
372 /* Messages put. Update signals */
Summer Qine578c5b2019-08-16 16:42:16 +0800373 p_runtime_data->signals |= service->service_db->signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800374
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800375 tfm_event_wake(&p_runtime_data->signal_evnt, (p_runtime_data->signals &
376 p_runtime_data->signal_mask));
Edison Ai764d41f2018-09-21 15:56:36 +0800377
Ken Liu35f89392019-03-14 14:51:05 +0800378 tfm_event_wait(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800379
380 return IPC_SUCCESS;
381}
382
Edison Ai7aff9e82019-07-11 14:56:46 +0800383uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
384{
385 return g_spm_partition_db.partitions[partition_idx].
Summer Qin423dbef2019-08-22 15:59:35 +0800386 memory_data->stack_bottom;
Edison Ai7aff9e82019-07-11 14:56:46 +0800387}
388
389uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
390{
Summer Qin423dbef2019-08-22 15:59:35 +0800391 return g_spm_partition_db.partitions[partition_idx].memory_data->stack_top;
Edison Ai7aff9e82019-07-11 14:56:46 +0800392}
393
Mingyang Sunf3d29892019-07-10 17:50:23 +0800394uint32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800395{
396 struct tfm_thrd_ctx *pth = tfm_thrd_curr_thread();
397 struct spm_partition_desc_t *partition;
Summer Qinb5da9cc2019-08-26 15:19:45 +0800398 struct spm_partition_runtime_data_t *r_data;
Edison Ai764d41f2018-09-21 15:56:36 +0800399
Summer Qinb5da9cc2019-08-26 15:19:45 +0800400 r_data = TFM_GET_CONTAINER_PTR(pth, struct spm_partition_runtime_data_t,
401 sp_thrd);
402 partition = TFM_GET_CONTAINER_PTR(r_data, struct spm_partition_desc_t,
403 runtime_data);
Summer Qin423dbef2019-08-22 15:59:35 +0800404 return partition->static_data->partition_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800405}
406
407static struct tfm_thrd_ctx *
Mingyang Sunf3d29892019-07-10 17:50:23 +0800408 tfm_spm_partition_get_thread_info(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800409{
Summer Qinb5da9cc2019-08-26 15:19:45 +0800410 return &g_spm_partition_db.partitions[partition_idx].runtime_data.sp_thrd;
Edison Ai764d41f2018-09-21 15:56:36 +0800411}
412
Edison Ai764d41f2018-09-21 15:56:36 +0800413static tfm_thrd_func_t
Mingyang Sunf3d29892019-07-10 17:50:23 +0800414 tfm_spm_partition_get_init_func(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800415{
416 return (tfm_thrd_func_t)(g_spm_partition_db.partitions[partition_idx].
Summer Qin423dbef2019-08-22 15:59:35 +0800417 static_data->partition_init);
Edison Ai764d41f2018-09-21 15:56:36 +0800418}
419
Mingyang Sunf3d29892019-07-10 17:50:23 +0800420static uint32_t tfm_spm_partition_get_priority(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800421{
Summer Qin423dbef2019-08-22 15:59:35 +0800422 return g_spm_partition_db.partitions[partition_idx].static_data->
Edison Ai764d41f2018-09-21 15:56:36 +0800423 partition_priority;
424}
425
David Hucb05d972019-08-06 18:10:11 +0800426int32_t tfm_memory_check(const void *buffer, size_t len, int32_t ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800427 enum tfm_memory_access_e access,
428 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800429{
Hugues de Valon99578562019-06-18 16:08:51 +0100430 enum tfm_status_e err;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800431
432 /* If len is zero, this indicates an empty buffer and base is ignored */
433 if (len == 0) {
434 return IPC_SUCCESS;
435 }
436
437 if (!buffer) {
438 return IPC_ERROR_BAD_PARAMETERS;
439 }
440
441 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
442 return IPC_ERROR_MEMORY_CHECK;
443 }
444
Summer Qin424d4db2019-03-25 14:09:51 +0800445 if (access == TFM_MEMORY_ACCESS_RW) {
Summer Qineb537e52019-03-29 09:57:10 +0800446 err = tfm_core_has_write_access_to_region(buffer, len, ns_caller,
447 privileged);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800448 } else {
Summer Qineb537e52019-03-29 09:57:10 +0800449 err = tfm_core_has_read_access_to_region(buffer, len, ns_caller,
450 privileged);
Summer Qin424d4db2019-03-25 14:09:51 +0800451 }
Summer Qin0fc3f592019-04-11 16:00:10 +0800452 if (err == TFM_SUCCESS) {
Summer Qin424d4db2019-03-25 14:09:51 +0800453 return IPC_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800454 }
455
456 return IPC_ERROR_MEMORY_CHECK;
457}
458
Summer Qin75f0d752019-08-27 14:38:20 +0800459uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
Summer Qineb537e52019-03-29 09:57:10 +0800460{
Summer Qin75f0d752019-08-27 14:38:20 +0800461 if (partition_flags & SPM_PART_FLAG_PSA_ROT) {
Summer Qineb537e52019-03-29 09:57:10 +0800462 return TFM_PARTITION_PRIVILEGED_MODE;
463 } else {
464 return TFM_PARTITION_UNPRIVILEGED_MODE;
465 }
466}
467
Edison Ai764d41f2018-09-21 15:56:36 +0800468/********************** SPM functions for thread mode ************************/
469
470void tfm_spm_init(void)
471{
472 uint32_t i, num;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800473 struct spm_partition_desc_t *partition;
Ken Liu483f5da2019-04-24 10:45:21 +0800474 struct tfm_thrd_ctx *pth, this_thrd;
Edison Ai764d41f2018-09-21 15:56:36 +0800475
476 tfm_pool_init(conn_handle_pool,
477 POOL_BUFFER_SIZE(conn_handle_pool),
478 sizeof(struct tfm_conn_handle_t),
479 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800480
481 /* Init partition first for it will be used when init service */
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200482 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800483 partition = &g_spm_partition_db.partitions[i];
484 tfm_spm_hal_configure_default_isolation(partition->platform_data);
Edison Ai764d41f2018-09-21 15:56:36 +0800485 if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
486 continue;
487 }
Ken Liu35f89392019-03-14 14:51:05 +0800488
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800489 tfm_event_init(&partition->runtime_data.signal_evnt);
490 tfm_list_init(&partition->runtime_data.service_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800491
Mingyang Sunf3d29892019-07-10 17:50:23 +0800492 pth = tfm_spm_partition_get_thread_info(i);
Edison Ai764d41f2018-09-21 15:56:36 +0800493 if (!pth) {
494 tfm_panic();
495 }
496
497 tfm_thrd_init(pth,
Mingyang Sunf3d29892019-07-10 17:50:23 +0800498 tfm_spm_partition_get_init_func(i),
Edison Ai764d41f2018-09-21 15:56:36 +0800499 NULL,
Ken Liu5a2b9052019-08-15 19:03:29 +0800500 (uintptr_t)tfm_spm_partition_get_stack_top(i),
501 (uintptr_t)tfm_spm_partition_get_stack_bottom(i));
Edison Ai788bae22019-02-18 17:38:59 +0800502
Mingyang Sunf3d29892019-07-10 17:50:23 +0800503 pth->prior = tfm_spm_partition_get_priority(i);
Edison Ai764d41f2018-09-21 15:56:36 +0800504
505 /* Kick off */
506 if (tfm_thrd_start(pth) != THRD_SUCCESS) {
507 tfm_panic();
508 }
509 }
510
511 /* Init Service */
Summer Qind99509f2019-08-02 17:36:58 +0800512 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
Edison Ai764d41f2018-09-21 15:56:36 +0800513 for (i = 0; i < num; i++) {
Summer Qine578c5b2019-08-16 16:42:16 +0800514 service[i].service_db = &service_db[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800515 partition =
Summer Qine578c5b2019-08-16 16:42:16 +0800516 tfm_spm_get_partition_by_id(service[i].service_db->partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800517 if (!partition) {
518 tfm_panic();
519 }
Summer Qind99509f2019-08-02 17:36:58 +0800520 service[i].partition = partition;
521 tfm_list_init(&service[i].handle_list);
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800522 tfm_list_add_tail(&partition->runtime_data.service_list,
Summer Qind99509f2019-08-02 17:36:58 +0800523 &service[i].list);
Edison Ai764d41f2018-09-21 15:56:36 +0800524 }
525
Ken Liu483f5da2019-04-24 10:45:21 +0800526 /*
527 * All threads initialized, start the scheduler.
528 *
529 * NOTE:
530 * Here is the booting privileged thread mode, and will never
531 * return to this place after scheduler is started. The start
532 * function has to save current runtime context to act as a
533 * 'current thread' to avoid repeating NULL 'current thread'
534 * checking while context switching. This saved context is worthy
535 * of being saved somewhere if there are potential usage purpose.
536 * Let's save this context in a local variable 'this_thrd' at
537 * current since there is no usage for it.
Mate Toth-Palc430b992019-05-09 21:01:14 +0200538 * Also set tfm_nspm_thread_entry as pfn for this thread to
539 * use in detecting NS/S thread scheduling changes.
Ken Liu483f5da2019-04-24 10:45:21 +0800540 */
Mate Toth-Palc430b992019-05-09 21:01:14 +0200541 this_thrd.pfn = (tfm_thrd_func_t)tfm_nspm_thread_entry;
Ken Liu483f5da2019-04-24 10:45:21 +0800542 tfm_thrd_start_scheduler(&this_thrd);
Edison Ai764d41f2018-09-21 15:56:36 +0800543}
Ken Liu2d175172019-03-21 17:08:41 +0800544
545void tfm_pendsv_do_schedule(struct tfm_state_context_ext *ctxb)
546{
547#if TFM_LVL == 2
548 struct spm_partition_desc_t *p_next_partition;
Summer Qinb5da9cc2019-08-26 15:19:45 +0800549 struct spm_partition_runtime_data_t *r_data;
Ken Liu2d175172019-03-21 17:08:41 +0800550 uint32_t is_privileged;
551#endif
552 struct tfm_thrd_ctx *pth_next = tfm_thrd_next_thread();
553 struct tfm_thrd_ctx *pth_curr = tfm_thrd_curr_thread();
554
Mate Toth-Pal32b2ccd2019-04-26 10:00:16 +0200555 if (pth_next != NULL && pth_curr != pth_next) {
Ken Liu2d175172019-03-21 17:08:41 +0800556#if TFM_LVL == 2
Summer Qinb5da9cc2019-08-26 15:19:45 +0800557 r_data = TFM_GET_CONTAINER_PTR(pth_next,
558 struct spm_partition_runtime_data_t,
559 sp_thrd);
560 p_next_partition = TFM_GET_CONTAINER_PTR(r_data,
Ken Liu2d175172019-03-21 17:08:41 +0800561 struct spm_partition_desc_t,
Summer Qinb5da9cc2019-08-26 15:19:45 +0800562 runtime_data);
Ken Liu2d175172019-03-21 17:08:41 +0800563
Summer Qin423dbef2019-08-22 15:59:35 +0800564 if (p_next_partition->static_data->partition_flags &
Ken Liu2d175172019-03-21 17:08:41 +0800565 SPM_PART_FLAG_PSA_ROT) {
566 is_privileged = TFM_PARTITION_PRIVILEGED_MODE;
567 } else {
568 is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE;
569 }
570
571 tfm_spm_partition_change_privilege(is_privileged);
572#endif
Mate Toth-Palc430b992019-05-09 21:01:14 +0200573 /* Increase the secure lock, if we enter secure from non-secure */
574 if ((void *)pth_curr->pfn == (void *)tfm_nspm_thread_entry) {
575 ++tfm_secure_lock;
576 }
577 /* Decrease the secure lock, if we return from secure to non-secure */
578 if ((void *)pth_next->pfn == (void *)tfm_nspm_thread_entry) {
579 --tfm_secure_lock;
580 }
581
Ken Liu2d175172019-03-21 17:08:41 +0800582 tfm_thrd_context_switch(ctxb, pth_curr, pth_next);
583 }
584}