blob: 70980f26f31d3b4ff416b732d0d26c6f859ddd4d [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
93 /* Remove node from handle list */
Edison Ai9cc26242019-08-06 11:28:04 +080094 tfm_list_del_node(&p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +080095
96 /* Back handle buffer to pool */
Edison Ai9cc26242019-08-06 11:28:04 +080097 tfm_pool_free(p_handle);
Edison Ai764d41f2018-09-21 15:56:36 +080098 return IPC_SUCCESS;
99}
100
101int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service,
102 psa_handle_t conn_handle,
103 void *rhandle)
104{
Edison Ai9cc26242019-08-06 11:28:04 +0800105 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800106
107 TFM_ASSERT(service);
108 /* Set reverse handle value only be allowed for a connected handle */
109 TFM_ASSERT(conn_handle != PSA_NULL_HANDLE);
110
111 /* There are many handles for each RoT Service */
Edison Ai9cc26242019-08-06 11:28:04 +0800112 p_handle = tfm_spm_find_conn_handle_node(service, conn_handle);
113 if (!p_handle) {
Edison Ai764d41f2018-09-21 15:56:36 +0800114 tfm_panic();
115 }
116
Edison Ai9cc26242019-08-06 11:28:04 +0800117 p_handle->rhandle = rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800118 return IPC_SUCCESS;
119}
120
121void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service,
122 psa_handle_t conn_handle)
123{
Edison Ai9cc26242019-08-06 11:28:04 +0800124 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800125
126 TFM_ASSERT(service);
127 /* Get reverse handle value only be allowed for a connected handle */
128 TFM_ASSERT(conn_handle != PSA_NULL_HANDLE);
129
130 /* There are many handles for each RoT Service */
Edison Ai9cc26242019-08-06 11:28:04 +0800131 p_handle = tfm_spm_find_conn_handle_node(service, conn_handle);
132 if (!p_handle) {
Edison Ai764d41f2018-09-21 15:56:36 +0800133 tfm_panic();
134 }
135
Edison Ai9cc26242019-08-06 11:28:04 +0800136 return p_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800137}
138
139/* Partition management functions */
140struct tfm_spm_service_t *
Mingyang Sunf3d29892019-07-10 17:50:23 +0800141 tfm_spm_get_service_by_signal(struct spm_partition_desc_t *partition,
142 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800143{
144 struct tfm_list_node_t *node, *head;
145 struct tfm_spm_service_t *service;
146
147 TFM_ASSERT(partition);
148
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800149 if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
Edison Ai764d41f2018-09-21 15:56:36 +0800150 tfm_panic();
151 }
152
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800153 head = &partition->runtime_data.service_list;
Edison Ai764d41f2018-09-21 15:56:36 +0800154 TFM_LIST_FOR_EACH(node, head) {
155 service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t, list);
Summer Qine578c5b2019-08-16 16:42:16 +0800156 if (service->service_db->signal == signal) {
Edison Ai764d41f2018-09-21 15:56:36 +0800157 return service;
158 }
159 }
160 return NULL;
161}
162
163struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid)
164{
165 uint32_t i;
166 struct tfm_list_node_t *node, *head;
167 struct tfm_spm_service_t *service;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800168 struct spm_partition_desc_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800169
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200170 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800171 partition = &g_spm_partition_db.partitions[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800172 /* Skip partition without IPC flag */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800173 if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
Edison Ai764d41f2018-09-21 15:56:36 +0800174 continue;
175 }
176
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800177 if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
Edison Ai764d41f2018-09-21 15:56:36 +0800178 continue;
179 }
180
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800181 head = &partition->runtime_data.service_list;
Edison Ai764d41f2018-09-21 15:56:36 +0800182 TFM_LIST_FOR_EACH(node, head) {
183 service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t,
184 list);
Summer Qine578c5b2019-08-16 16:42:16 +0800185 if (service->service_db->sid == sid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800186 return service;
187 }
188 }
189 }
190 return NULL;
191}
192
193struct tfm_spm_service_t *
194 tfm_spm_get_service_by_handle(psa_handle_t conn_handle)
195{
Edison Ai9cc26242019-08-06 11:28:04 +0800196 return ((struct tfm_conn_handle_t *)conn_handle)->service;
Edison Ai764d41f2018-09-21 15:56:36 +0800197}
198
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800199struct spm_partition_desc_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800200{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800201 uint32_t idx = get_partition_idx(partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800202
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800203 if (idx != SPM_INVALID_PARTITION_IDX) {
204 return &(g_spm_partition_db.partitions[idx]);
Edison Ai764d41f2018-09-21 15:56:36 +0800205 }
206 return NULL;
207}
208
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800209struct spm_partition_desc_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800210{
211 uint32_t spid;
212
Mingyang Sunf3d29892019-07-10 17:50:23 +0800213 spid = tfm_spm_partition_get_running_partition_id();
Edison Ai764d41f2018-09-21 15:56:36 +0800214
215 return tfm_spm_get_partition_by_id(spid);
216}
217
218int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
219 uint32_t minor_version)
220{
221 TFM_ASSERT(service);
222
Summer Qine578c5b2019-08-16 16:42:16 +0800223 switch (service->service_db->minor_policy) {
Edison Ai764d41f2018-09-21 15:56:36 +0800224 case TFM_VERSION_POLICY_RELAXED:
Summer Qine578c5b2019-08-16 16:42:16 +0800225 if (minor_version > service->service_db->minor_version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800226 return IPC_ERROR_VERSION;
227 }
228 break;
229 case TFM_VERSION_POLICY_STRICT:
Summer Qine578c5b2019-08-16 16:42:16 +0800230 if (minor_version != service->service_db->minor_version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800231 return IPC_ERROR_VERSION;
232 }
233 break;
234 default:
235 return IPC_ERROR_VERSION;
236 }
237 return IPC_SUCCESS;
238}
239
240/* Message functions */
241struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
242{
243 /*
244 * There may be one error handle passed by the caller in two conditions:
245 * 1. Not a valid message handle.
246 * 2. Handle between different Partitions. Partition A passes one handle
247 * belong to other Partitions and tries to access other's data.
248 * So, need do necessary checking to prevent those conditions.
249 */
250 struct tfm_msg_body_t *msg;
251 uint32_t partition_id;
252
253 msg = (struct tfm_msg_body_t *)msg_handle;
254 if (!msg) {
255 return NULL;
256 }
257
258 /*
259 * FixMe: For condition 1: using a magic number to define it's a message.
260 * It needs to be an enhancement to check the handle belong to service.
261 */
262 if (msg->magic != TFM_MSG_MAGIC) {
263 return NULL;
264 }
265
266 /* For condition 2: check if the partition ID is same */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800267 partition_id = tfm_spm_partition_get_running_partition_id();
Summer Qin423dbef2019-08-22 15:59:35 +0800268 if (partition_id != msg->service->partition->static_data->partition_id) {
Edison Ai764d41f2018-09-21 15:56:36 +0800269 return NULL;
270 }
271
272 return msg;
273}
274
Edison Ai97115822019-08-01 14:22:19 +0800275struct tfm_msg_body_t *
276 tfm_spm_get_msg_buffer_from_conn_handle(psa_handle_t conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800277{
Edison Ai97115822019-08-01 14:22:19 +0800278 TFM_ASSERT(conn_handle != PSA_NULL_HANDLE);
279
280 return &(((struct tfm_conn_handle_t *)conn_handle)->internal_msg);
281}
282
283void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
284 struct tfm_spm_service_t *service,
285 psa_handle_t handle,
286 int32_t type, int32_t ns_caller,
287 psa_invec *invec, size_t in_len,
288 psa_outvec *outvec, size_t out_len,
289 psa_outvec *caller_outvec)
290{
Edison Ai764d41f2018-09-21 15:56:36 +0800291 uint32_t i;
292
Edison Ai97115822019-08-01 14:22:19 +0800293 TFM_ASSERT(msg);
Edison Ai764d41f2018-09-21 15:56:36 +0800294 TFM_ASSERT(service);
295 TFM_ASSERT(!(invec == NULL && in_len != 0));
296 TFM_ASSERT(!(outvec == NULL && out_len != 0));
297 TFM_ASSERT(in_len <= PSA_MAX_IOVEC);
298 TFM_ASSERT(out_len <= PSA_MAX_IOVEC);
299 TFM_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
300
Edison Ai764d41f2018-09-21 15:56:36 +0800301 /* Clear message buffer before using it */
Mingyang Sun94b1b412019-09-20 15:11:14 +0800302 tfm_core_util_memset(msg, 0, sizeof(struct tfm_msg_body_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800303
Ken Liu35f89392019-03-14 14:51:05 +0800304 tfm_event_init(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800305 msg->magic = TFM_MSG_MAGIC;
306 msg->service = service;
307 msg->handle = handle;
308 msg->caller_outvec = caller_outvec;
309 /* Get current partition id */
310 if (ns_caller) {
311 msg->msg.client_id = tfm_nspm_get_current_client_id();
312 } else {
Mingyang Sunf3d29892019-07-10 17:50:23 +0800313 msg->msg.client_id = tfm_spm_partition_get_running_partition_id();
Edison Ai764d41f2018-09-21 15:56:36 +0800314 }
315
316 /* Copy contents */
317 msg->msg.type = type;
318
319 for (i = 0; i < in_len; i++) {
320 msg->msg.in_size[i] = invec[i].len;
321 msg->invec[i].base = invec[i].base;
322 }
323
324 for (i = 0; i < out_len; i++) {
325 msg->msg.out_size[i] = outvec[i].len;
326 msg->outvec[i].base = outvec[i].base;
327 /* Out len is used to record the writed number, set 0 here again */
328 msg->outvec[i].len = 0;
329 }
330
331 /* Use message address as handle */
332 msg->msg.handle = (psa_handle_t)msg;
333
334 /* For connected handle, set rhandle to every message */
335 if (handle != PSA_NULL_HANDLE) {
336 msg->msg.rhandle = tfm_spm_get_rhandle(service, handle);
337 }
Edison Ai764d41f2018-09-21 15:56:36 +0800338}
339
340int32_t tfm_spm_send_event(struct tfm_spm_service_t *service,
341 struct tfm_msg_body_t *msg)
342{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800343 struct spm_partition_runtime_data_t *p_runtime_data =
344 &service->partition->runtime_data;
345
Edison Ai764d41f2018-09-21 15:56:36 +0800346 TFM_ASSERT(service);
347 TFM_ASSERT(msg);
348
349 /* Enqueue message to service message queue */
350 if (tfm_msg_enqueue(&service->msg_queue, msg) != IPC_SUCCESS) {
351 return IPC_ERROR_GENERIC;
352 }
353
354 /* Messages put. Update signals */
Summer Qine578c5b2019-08-16 16:42:16 +0800355 p_runtime_data->signals |= service->service_db->signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800356
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800357 tfm_event_wake(&p_runtime_data->signal_evnt, (p_runtime_data->signals &
358 p_runtime_data->signal_mask));
Edison Ai764d41f2018-09-21 15:56:36 +0800359
Ken Liu35f89392019-03-14 14:51:05 +0800360 tfm_event_wait(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800361
362 return IPC_SUCCESS;
363}
364
Edison Ai7aff9e82019-07-11 14:56:46 +0800365uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
366{
367 return g_spm_partition_db.partitions[partition_idx].
Summer Qin423dbef2019-08-22 15:59:35 +0800368 memory_data->stack_bottom;
Edison Ai7aff9e82019-07-11 14:56:46 +0800369}
370
371uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
372{
Summer Qin423dbef2019-08-22 15:59:35 +0800373 return g_spm_partition_db.partitions[partition_idx].memory_data->stack_top;
Edison Ai7aff9e82019-07-11 14:56:46 +0800374}
375
Mingyang Sunf3d29892019-07-10 17:50:23 +0800376uint32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800377{
378 struct tfm_thrd_ctx *pth = tfm_thrd_curr_thread();
379 struct spm_partition_desc_t *partition;
Summer Qinb5da9cc2019-08-26 15:19:45 +0800380 struct spm_partition_runtime_data_t *r_data;
Edison Ai764d41f2018-09-21 15:56:36 +0800381
Summer Qinb5da9cc2019-08-26 15:19:45 +0800382 r_data = TFM_GET_CONTAINER_PTR(pth, struct spm_partition_runtime_data_t,
383 sp_thrd);
384 partition = TFM_GET_CONTAINER_PTR(r_data, struct spm_partition_desc_t,
385 runtime_data);
Summer Qin423dbef2019-08-22 15:59:35 +0800386 return partition->static_data->partition_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800387}
388
389static struct tfm_thrd_ctx *
Mingyang Sunf3d29892019-07-10 17:50:23 +0800390 tfm_spm_partition_get_thread_info(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800391{
Summer Qinb5da9cc2019-08-26 15:19:45 +0800392 return &g_spm_partition_db.partitions[partition_idx].runtime_data.sp_thrd;
Edison Ai764d41f2018-09-21 15:56:36 +0800393}
394
Edison Ai764d41f2018-09-21 15:56:36 +0800395static tfm_thrd_func_t
Mingyang Sunf3d29892019-07-10 17:50:23 +0800396 tfm_spm_partition_get_init_func(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800397{
398 return (tfm_thrd_func_t)(g_spm_partition_db.partitions[partition_idx].
Summer Qin423dbef2019-08-22 15:59:35 +0800399 static_data->partition_init);
Edison Ai764d41f2018-09-21 15:56:36 +0800400}
401
Mingyang Sunf3d29892019-07-10 17:50:23 +0800402static uint32_t tfm_spm_partition_get_priority(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800403{
Summer Qin423dbef2019-08-22 15:59:35 +0800404 return g_spm_partition_db.partitions[partition_idx].static_data->
Edison Ai764d41f2018-09-21 15:56:36 +0800405 partition_priority;
406}
407
David Hucb05d972019-08-06 18:10:11 +0800408int32_t tfm_memory_check(const void *buffer, size_t len, int32_t ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800409 enum tfm_memory_access_e access,
410 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800411{
Hugues de Valon99578562019-06-18 16:08:51 +0100412 enum tfm_status_e err;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800413
414 /* If len is zero, this indicates an empty buffer and base is ignored */
415 if (len == 0) {
416 return IPC_SUCCESS;
417 }
418
419 if (!buffer) {
420 return IPC_ERROR_BAD_PARAMETERS;
421 }
422
423 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
424 return IPC_ERROR_MEMORY_CHECK;
425 }
426
Summer Qin424d4db2019-03-25 14:09:51 +0800427 if (access == TFM_MEMORY_ACCESS_RW) {
Summer Qineb537e52019-03-29 09:57:10 +0800428 err = tfm_core_has_write_access_to_region(buffer, len, ns_caller,
429 privileged);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800430 } else {
Summer Qineb537e52019-03-29 09:57:10 +0800431 err = tfm_core_has_read_access_to_region(buffer, len, ns_caller,
432 privileged);
Summer Qin424d4db2019-03-25 14:09:51 +0800433 }
Summer Qin0fc3f592019-04-11 16:00:10 +0800434 if (err == TFM_SUCCESS) {
Summer Qin424d4db2019-03-25 14:09:51 +0800435 return IPC_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800436 }
437
438 return IPC_ERROR_MEMORY_CHECK;
439}
440
Summer Qin75f0d752019-08-27 14:38:20 +0800441uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
Summer Qineb537e52019-03-29 09:57:10 +0800442{
Summer Qin75f0d752019-08-27 14:38:20 +0800443 if (partition_flags & SPM_PART_FLAG_PSA_ROT) {
Summer Qineb537e52019-03-29 09:57:10 +0800444 return TFM_PARTITION_PRIVILEGED_MODE;
445 } else {
446 return TFM_PARTITION_UNPRIVILEGED_MODE;
447 }
448}
449
Edison Ai764d41f2018-09-21 15:56:36 +0800450/********************** SPM functions for thread mode ************************/
451
452void tfm_spm_init(void)
453{
454 uint32_t i, num;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800455 struct spm_partition_desc_t *partition;
Ken Liu483f5da2019-04-24 10:45:21 +0800456 struct tfm_thrd_ctx *pth, this_thrd;
Edison Ai764d41f2018-09-21 15:56:36 +0800457
458 tfm_pool_init(conn_handle_pool,
459 POOL_BUFFER_SIZE(conn_handle_pool),
460 sizeof(struct tfm_conn_handle_t),
461 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800462
463 /* Init partition first for it will be used when init service */
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200464 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800465 partition = &g_spm_partition_db.partitions[i];
466 tfm_spm_hal_configure_default_isolation(partition->platform_data);
Edison Ai764d41f2018-09-21 15:56:36 +0800467 if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
468 continue;
469 }
Ken Liu35f89392019-03-14 14:51:05 +0800470
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800471 tfm_event_init(&partition->runtime_data.signal_evnt);
472 tfm_list_init(&partition->runtime_data.service_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800473
Mingyang Sunf3d29892019-07-10 17:50:23 +0800474 pth = tfm_spm_partition_get_thread_info(i);
Edison Ai764d41f2018-09-21 15:56:36 +0800475 if (!pth) {
476 tfm_panic();
477 }
478
479 tfm_thrd_init(pth,
Mingyang Sunf3d29892019-07-10 17:50:23 +0800480 tfm_spm_partition_get_init_func(i),
Edison Ai764d41f2018-09-21 15:56:36 +0800481 NULL,
Ken Liu5a2b9052019-08-15 19:03:29 +0800482 (uintptr_t)tfm_spm_partition_get_stack_top(i),
483 (uintptr_t)tfm_spm_partition_get_stack_bottom(i));
Edison Ai788bae22019-02-18 17:38:59 +0800484
Mingyang Sunf3d29892019-07-10 17:50:23 +0800485 pth->prior = tfm_spm_partition_get_priority(i);
Edison Ai764d41f2018-09-21 15:56:36 +0800486
487 /* Kick off */
488 if (tfm_thrd_start(pth) != THRD_SUCCESS) {
489 tfm_panic();
490 }
491 }
492
493 /* Init Service */
Summer Qind99509f2019-08-02 17:36:58 +0800494 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
Edison Ai764d41f2018-09-21 15:56:36 +0800495 for (i = 0; i < num; i++) {
Summer Qine578c5b2019-08-16 16:42:16 +0800496 service[i].service_db = &service_db[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800497 partition =
Summer Qine578c5b2019-08-16 16:42:16 +0800498 tfm_spm_get_partition_by_id(service[i].service_db->partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800499 if (!partition) {
500 tfm_panic();
501 }
Summer Qind99509f2019-08-02 17:36:58 +0800502 service[i].partition = partition;
503 tfm_list_init(&service[i].handle_list);
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800504 tfm_list_add_tail(&partition->runtime_data.service_list,
Summer Qind99509f2019-08-02 17:36:58 +0800505 &service[i].list);
Edison Ai764d41f2018-09-21 15:56:36 +0800506 }
507
Ken Liu483f5da2019-04-24 10:45:21 +0800508 /*
509 * All threads initialized, start the scheduler.
510 *
511 * NOTE:
512 * Here is the booting privileged thread mode, and will never
513 * return to this place after scheduler is started. The start
514 * function has to save current runtime context to act as a
515 * 'current thread' to avoid repeating NULL 'current thread'
516 * checking while context switching. This saved context is worthy
517 * of being saved somewhere if there are potential usage purpose.
518 * Let's save this context in a local variable 'this_thrd' at
519 * current since there is no usage for it.
Mate Toth-Palc430b992019-05-09 21:01:14 +0200520 * Also set tfm_nspm_thread_entry as pfn for this thread to
521 * use in detecting NS/S thread scheduling changes.
Ken Liu483f5da2019-04-24 10:45:21 +0800522 */
Mate Toth-Palc430b992019-05-09 21:01:14 +0200523 this_thrd.pfn = (tfm_thrd_func_t)tfm_nspm_thread_entry;
Ken Liu483f5da2019-04-24 10:45:21 +0800524 tfm_thrd_start_scheduler(&this_thrd);
Edison Ai764d41f2018-09-21 15:56:36 +0800525}
Ken Liu2d175172019-03-21 17:08:41 +0800526
527void tfm_pendsv_do_schedule(struct tfm_state_context_ext *ctxb)
528{
529#if TFM_LVL == 2
530 struct spm_partition_desc_t *p_next_partition;
Summer Qinb5da9cc2019-08-26 15:19:45 +0800531 struct spm_partition_runtime_data_t *r_data;
Ken Liu2d175172019-03-21 17:08:41 +0800532 uint32_t is_privileged;
533#endif
534 struct tfm_thrd_ctx *pth_next = tfm_thrd_next_thread();
535 struct tfm_thrd_ctx *pth_curr = tfm_thrd_curr_thread();
536
Mate Toth-Pal32b2ccd2019-04-26 10:00:16 +0200537 if (pth_next != NULL && pth_curr != pth_next) {
Ken Liu2d175172019-03-21 17:08:41 +0800538#if TFM_LVL == 2
Summer Qinb5da9cc2019-08-26 15:19:45 +0800539 r_data = TFM_GET_CONTAINER_PTR(pth_next,
540 struct spm_partition_runtime_data_t,
541 sp_thrd);
542 p_next_partition = TFM_GET_CONTAINER_PTR(r_data,
Ken Liu2d175172019-03-21 17:08:41 +0800543 struct spm_partition_desc_t,
Summer Qinb5da9cc2019-08-26 15:19:45 +0800544 runtime_data);
Ken Liu2d175172019-03-21 17:08:41 +0800545
Summer Qin423dbef2019-08-22 15:59:35 +0800546 if (p_next_partition->static_data->partition_flags &
Ken Liu2d175172019-03-21 17:08:41 +0800547 SPM_PART_FLAG_PSA_ROT) {
548 is_privileged = TFM_PARTITION_PRIVILEGED_MODE;
549 } else {
550 is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE;
551 }
552
553 tfm_spm_partition_change_privilege(is_privileged);
554#endif
Mate Toth-Palc430b992019-05-09 21:01:14 +0200555 /* Increase the secure lock, if we enter secure from non-secure */
556 if ((void *)pth_curr->pfn == (void *)tfm_nspm_thread_entry) {
557 ++tfm_secure_lock;
558 }
559 /* Decrease the secure lock, if we return from secure to non-secure */
560 if ((void *)pth_next->pfn == (void *)tfm_nspm_thread_entry) {
561 --tfm_secure_lock;
562 }
563
Ken Liu2d175172019-03-21 17:08:41 +0800564 tfm_thrd_context_switch(ctxb, pth_curr, pth_next);
565 }
566}