blob: 9b3f3f21644260efebcfd89be6f6ee79d75c04df [file] [log] [blame]
Miklos Balint386b8b52017-11-29 13:12:32 +00001/*
Tamas Ban8bd24b72019-02-19 12:13:13 +00002 * Copyright (c) 2017-2019, Arm Limited. All rights reserved.
Miklos Balint386b8b52017-11-29 13:12:32 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Mate Toth-Pal349714a2018-02-23 15:30:24 +01008/* This file contains the APIs exported by the SPM to tfm core */
Miklos Balint386b8b52017-11-29 13:12:32 +00009
10#include <stdio.h>
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +010011#include <string.h>
Miklos Balint386b8b52017-11-29 13:12:32 +000012#include "spm_api.h"
Mate Toth-Pale1475332018-04-09 17:28:49 +020013#include "platform/include/tfm_spm_hal.h"
Tamas Ban8bd24b72019-02-19 12:13:13 +000014#include "tfm_memory_utils.h"
Mate Toth-Pale1475332018-04-09 17:28:49 +020015#include "spm_db_setup.h"
Miklos Balint6a139ae2018-04-04 19:44:37 +020016#include "tfm_internal.h"
Mate Toth-Pal65291f32018-02-23 14:35:22 +010017#include "tfm_api.h"
Mate Toth-Palce61afa2018-08-03 13:51:01 +020018#include "tfm_nspm.h"
Miklos Balint386b8b52017-11-29 13:12:32 +000019#include "secure_fw/core/tfm_core.h"
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020020#include "tfm_peripherals_def.h"
Mate Toth-Pale1475332018-04-09 17:28:49 +020021#include "spm_partition_defs.h"
22
Miklos Balint386b8b52017-11-29 13:12:32 +000023
Mate Toth-Pal349714a2018-02-23 15:30:24 +010024struct spm_partition_db_t g_spm_partition_db = {0,};
Miklos Balint386b8b52017-11-29 13:12:32 +000025
Miklos Balint386b8b52017-11-29 13:12:32 +000026typedef enum {
27 TFM_INIT_FAILURE,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010028} sp_error_type_t;
Miklos Balint386b8b52017-11-29 13:12:32 +000029
Mate Toth-Pal4341de02018-10-02 12:55:47 +020030/* The size of this struct must be multiple of 4 bytes as it is stacked to an
31 * uint32_t[] array
32 */
33struct interrupted_ctx_stack_frame_t {
34#if TFM_LVL != 1
35 uint32_t stack_ptr;
36#endif
37 uint32_t partition_state;
38};
39
40/* The size of this struct must be multiple of 4 bytes as it is stacked to an
41 * uint32_t[] array
42 */
43struct handler_ctx_stack_frame_t {
44 uint32_t partition_state;
45 uint32_t caller_partition_idx;
46};
47
Miklos Balint386b8b52017-11-29 13:12:32 +000048/*
Mate Toth-Pal349714a2018-02-23 15:30:24 +010049 * This function is called when a secure partition causes an error.
Mate Toth-Pal65291f32018-02-23 14:35:22 +010050 * In case of an error in the error handling, a non-zero value have to be
51 * returned.
Miklos Balint386b8b52017-11-29 13:12:32 +000052 */
Summer Qinb4a854d2019-05-29 15:31:22 +080053#ifndef TFM_PSA_API
Mate Toth-Pal349714a2018-02-23 15:30:24 +010054static void tfm_spm_partition_err_handler(
Hugues de Valonf704c802019-02-19 14:51:41 +000055 const struct spm_partition_desc_t *partition,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010056 sp_error_type_t err_type,
Mate Toth-Pal65291f32018-02-23 14:35:22 +010057 int32_t err_code)
Miklos Balint386b8b52017-11-29 13:12:32 +000058{
Miklos Balint386b8b52017-11-29 13:12:32 +000059#ifdef TFM_CORE_DEBUG
60 if (err_type == TFM_INIT_FAILURE) {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010061 printf("Partition init failed for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010062 partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000063 } else {
Hugues de Valonf704c802019-02-19 14:51:41 +000064 printf(
65 "Unknown partition error %d (code: %d) for partition id 0x%08X\r\n",
66 err_type, err_code, partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000067 }
Hugues de Valonf704c802019-02-19 14:51:41 +000068#else
69 (void)err_type;
70 (void)err_code;
Miklos Balint386b8b52017-11-29 13:12:32 +000071#endif
Mate Toth-Pal18b83922018-02-26 17:58:18 +010072 tfm_spm_partition_set_state(partition->static_data.partition_id,
Mate Toth-Pal349714a2018-02-23 15:30:24 +010073 SPM_PARTITION_STATE_CLOSED);
Miklos Balint386b8b52017-11-29 13:12:32 +000074}
Summer Qinb4a854d2019-05-29 15:31:22 +080075#endif /* !defined(TFM_PSA_API) */
Miklos Balint386b8b52017-11-29 13:12:32 +000076
Hugues de Valonf704c802019-02-19 14:51:41 +000077/*
78 * This function prevents name clashes between the variable names accessibles in
79 * the scope of where tfm_partition_list.inc is included and the varaible names
80 * defined inside tfm_partition_list.inc file.
81 */
82static inline enum spm_err_t add_user_defined_partitions(void) {
83 #include "secure_fw/services/tfm_partition_list.inc"
84
85 return SPM_ERR_OK;
86}
87
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010088uint32_t get_partition_idx(uint32_t partition_id)
89{
Hugues de Valonf704c802019-02-19 14:51:41 +000090 uint32_t i;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010091
92 if (partition_id == INVALID_PARTITION_ID) {
93 return SPM_INVALID_PARTITION_IDX;
94 }
95
96 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
97 if (g_spm_partition_db.partitions[i].static_data.partition_id ==
98 partition_id) {
99 return i;
100 }
101 }
102 return SPM_INVALID_PARTITION_IDX;
103}
104
Miklos Balint386b8b52017-11-29 13:12:32 +0000105enum spm_err_t tfm_spm_db_init(void)
106{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200107 struct spm_partition_desc_t *part_ptr;
Hugues de Valonf704c802019-02-19 14:51:41 +0000108 enum spm_err_t err;
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200109 static uint32_t ns_interrupt_ctx_stack[
110 sizeof(struct interrupted_ctx_stack_frame_t)/sizeof(uint32_t)] = {0};
111 static uint32_t tfm_core_interrupt_ctx_stack[
112 sizeof(struct interrupted_ctx_stack_frame_t)/sizeof(uint32_t)] = {0};
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100113
Hugues de Valonf704c802019-02-19 14:51:41 +0000114 (void)tfm_memset (&g_spm_partition_db, 0, sizeof(g_spm_partition_db));
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +0100115
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100116 /* This function initialises partition db */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100117 g_spm_partition_db.running_partition_idx = SPM_INVALID_PARTITION_IDX;
118 g_spm_partition_db.partition_count = 0;
Miklos Balint386b8b52017-11-29 13:12:32 +0000119
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100120 /* There are a few partitions that are used by TF-M internally.
121 * These are explicitly added to the partition db here.
122 */
123
124 /* For the non secure Execution environment */
Miklos Balintdd02bb32019-05-26 21:13:12 +0200125#if (TFM_LVL != 1) || defined(TFM_PSA_API)
Tamas Ban56ef3022018-09-13 23:49:16 +0100126 extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[];
127 extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit[];
128 uint32_t psp_stack_bottom = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Base;
129 uint32_t psp_stack_top = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Limit;
Miklos Balintdd02bb32019-05-26 21:13:12 +0200130#endif
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100131 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
132 return SPM_ERR_INVALID_CONFIG;
133 }
134 part_ptr = &(g_spm_partition_db.partitions[
135 g_spm_partition_db.partition_count]);
136 part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID;
Hugues de Valon99578562019-06-18 16:08:51 +0100137#ifdef TFM_PSA_API
Edison Ai4dcae6f2019-03-18 10:13:47 +0800138 part_ptr->static_data.partition_flags = SPM_PART_FLAG_APP_ROT |
139 SPM_PART_FLAG_IPC;
140 part_ptr->static_data.partition_priority = TFM_PRIORITY_LOW;
141 part_ptr->static_data.partition_init = tfm_nspm_thread_entry;
142#else
Mate Toth-Pal59398712018-02-28 17:06:40 +0100143 part_ptr->static_data.partition_flags = 0;
Edison Ai4dcae6f2019-03-18 10:13:47 +0800144#endif
Miklos Balint6a139ae2018-04-04 19:44:37 +0200145
Miklos Balintdd02bb32019-05-26 21:13:12 +0200146#if (TFM_LVL != 1) || defined(TFM_PSA_API)
Tamas Ban56ef3022018-09-13 23:49:16 +0100147 part_ptr->memory_data.stack_bottom = psp_stack_bottom;
148 part_ptr->memory_data.stack_top = psp_stack_top;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200149 /* Since RW, ZI and stack are configured as one MPU region, configure
Tamas Ban56ef3022018-09-13 23:49:16 +0100150 * RW start address to psp_stack_bottom to get RW access to stack
Miklos Balint6a139ae2018-04-04 19:44:37 +0200151 */
Tamas Ban56ef3022018-09-13 23:49:16 +0100152 part_ptr->memory_data.rw_start = psp_stack_bottom;
Miklos Balintdd02bb32019-05-26 21:13:12 +0200153#endif
Miklos Balint6a139ae2018-04-04 19:44:37 +0200154
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100155 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200156 part_ptr->runtime_data.ctx_stack_ptr = ns_interrupt_ctx_stack;
Miklos Balint12735bc2018-08-01 15:45:18 +0200157 tfm_nspm_configure_clients();
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100158 ++g_spm_partition_db.partition_count;
159
160 /* For the TF-M core environment itself */
161 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
162 return SPM_ERR_INVALID_CONFIG;
163 }
164 part_ptr = &(g_spm_partition_db.partitions[
165 g_spm_partition_db.partition_count]);
166 part_ptr->static_data.partition_id = TFM_SP_CORE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +0100167 part_ptr->static_data.partition_flags =
Edison Aibb614aa2018-11-21 15:15:00 +0800168 SPM_PART_FLAG_APP_ROT | SPM_PART_FLAG_PSA_ROT;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100169 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200170 part_ptr->runtime_data.ctx_stack_ptr = tfm_core_interrupt_ctx_stack;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100171 ++g_spm_partition_db.partition_count;
172
Hugues de Valonf704c802019-02-19 14:51:41 +0000173 err = add_user_defined_partitions();
174 if (err != SPM_ERR_OK) {
175 return err;
176 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000177
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +0100178 g_spm_partition_db.is_init = 1;
179
Miklos Balint386b8b52017-11-29 13:12:32 +0000180 return SPM_ERR_OK;
181}
182
Summer Qinb4a854d2019-05-29 15:31:22 +0800183#ifndef TFM_PSA_API
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100184enum spm_err_t tfm_spm_partition_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +0000185{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200186 struct spm_partition_desc_t *part;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200187 struct tfm_sfn_req_s desc;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200188 int32_t args[4] = {0};
Miklos Balint386b8b52017-11-29 13:12:32 +0000189 int32_t fail_cnt = 0;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100190 uint32_t idx;
Miklos Balint386b8b52017-11-29 13:12:32 +0000191
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100192 /* Call the init function for each partition */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100193 for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) {
194 part = &g_spm_partition_db.partitions[idx];
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200195 tfm_spm_hal_configure_default_isolation(part->platform_data);
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100196 if (part->static_data.partition_init == NULL) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100197 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Miklos Balint6a139ae2018-04-04 19:44:37 +0200198 tfm_spm_partition_set_caller_partition_idx(idx,
199 SPM_INVALID_PARTITION_IDX);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100200 } else {
Miklos Balintace4c3f2018-07-30 12:31:15 +0200201 int32_t res;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200202
203 desc.args = args;
Hugues de Valonf704c802019-02-19 14:51:41 +0000204 desc.ns_caller = 0U;
Mate Toth-Palb8ce0dd2018-07-25 10:18:34 +0200205 desc.iovec_api = TFM_SFN_API_IOVEC;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200206 desc.sfn = (sfn_t)part->static_data.partition_init;
207 desc.sp_id = part->static_data.partition_id;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200208 res = tfm_core_sfn_request(&desc);
Hugues de Valon99578562019-06-18 16:08:51 +0100209 if (res == (int32_t)TFM_SUCCESS) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100210 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100211 } else {
Miklos Balintace4c3f2018-07-30 12:31:15 +0200212 tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, res);
Miklos Balint386b8b52017-11-29 13:12:32 +0000213 fail_cnt++;
214 }
215 }
216 }
217
Miklos Balint6a139ae2018-04-04 19:44:37 +0200218 tfm_secure_api_init_done();
219
Miklos Balint386b8b52017-11-29 13:12:32 +0000220 if (fail_cnt == 0) {
221 return SPM_ERR_OK;
222 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100223 return SPM_ERR_PARTITION_NOT_AVAILABLE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000224 }
225}
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200226
227void tfm_spm_partition_push_interrupted_ctx(uint32_t partition_idx)
228{
229 struct spm_partition_runtime_data_t *runtime_data =
230 &g_spm_partition_db.partitions[partition_idx].runtime_data;
231 struct interrupted_ctx_stack_frame_t *stack_frame =
232 (struct interrupted_ctx_stack_frame_t *)
233 runtime_data->ctx_stack_ptr;
234
235 stack_frame->partition_state = runtime_data->partition_state;
236#if TFM_LVL != 1
237 stack_frame->stack_ptr = runtime_data->stack_ptr;
238#endif
239 runtime_data->ctx_stack_ptr +=
240 sizeof(struct interrupted_ctx_stack_frame_t) / sizeof(uint32_t);
241
242}
243
244void tfm_spm_partition_pop_interrupted_ctx(uint32_t partition_idx)
245{
246 struct spm_partition_runtime_data_t *runtime_data =
247 &g_spm_partition_db.partitions[partition_idx].runtime_data;
248 struct interrupted_ctx_stack_frame_t *stack_frame;
249
250 runtime_data->ctx_stack_ptr -=
251 sizeof(struct interrupted_ctx_stack_frame_t) / sizeof(uint32_t);
252 stack_frame = (struct interrupted_ctx_stack_frame_t *)
253 runtime_data->ctx_stack_ptr;
254 tfm_spm_partition_set_state(partition_idx, stack_frame->partition_state);
255 stack_frame->partition_state = 0;
256#if TFM_LVL != 1
257 tfm_spm_partition_set_stack(partition_idx, stack_frame->stack_ptr);
258 stack_frame->stack_ptr = 0;
259#endif
260}
261
262void tfm_spm_partition_push_handler_ctx(uint32_t partition_idx)
263{
264 struct spm_partition_runtime_data_t *runtime_data =
265 &g_spm_partition_db.partitions[partition_idx].runtime_data;
266 struct handler_ctx_stack_frame_t *stack_frame =
267 (struct handler_ctx_stack_frame_t *)
268 runtime_data->ctx_stack_ptr;
269
270 stack_frame->partition_state = runtime_data->partition_state;
271 stack_frame->caller_partition_idx = runtime_data->caller_partition_idx;
272
273 runtime_data->ctx_stack_ptr +=
274 sizeof(struct handler_ctx_stack_frame_t) / sizeof(uint32_t);
275}
276
277void tfm_spm_partition_pop_handler_ctx(uint32_t partition_idx)
278{
279 struct spm_partition_runtime_data_t *runtime_data =
280 &g_spm_partition_db.partitions[partition_idx].runtime_data;
281 struct handler_ctx_stack_frame_t *stack_frame;
282
283 runtime_data->ctx_stack_ptr -=
284 sizeof(struct handler_ctx_stack_frame_t) / sizeof(uint32_t);
285
286 stack_frame = (struct handler_ctx_stack_frame_t *)
287 runtime_data->ctx_stack_ptr;
288
289 tfm_spm_partition_set_state(partition_idx, stack_frame->partition_state);
290 stack_frame->partition_state = 0;
291 tfm_spm_partition_set_caller_partition_idx(
292 partition_idx, stack_frame->caller_partition_idx);
293 stack_frame->caller_partition_idx = 0;
294
295}
296
Summer Qinb4a854d2019-05-29 15:31:22 +0800297#endif /* !defined(TFM_PSA_API) */
Miklos Balint386b8b52017-11-29 13:12:32 +0000298
Miklos Balintdd02bb32019-05-26 21:13:12 +0200299#if (TFM_LVL != 1) || defined(TFM_PSA_API)
Summer Qind00e4db2019-05-09 18:03:52 +0800300uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
301{
302 return g_spm_partition_db.partitions[partition_idx].
303 memory_data.stack_bottom;
304}
305
306uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
307{
308 return g_spm_partition_db.partitions[partition_idx].memory_data.stack_top;
309}
Miklos Balintdd02bb32019-05-26 21:13:12 +0200310#endif
Summer Qind00e4db2019-05-09 18:03:52 +0800311
Miklos Balintdd02bb32019-05-26 21:13:12 +0200312#if (TFM_LVL != 1) && !defined(TFM_PSA_API)
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100313enum spm_err_t tfm_spm_partition_sandbox_config(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000314{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200315 struct spm_partition_desc_t *part;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100316 if (!g_spm_partition_db.is_init) {
317 return SPM_ERR_PARTITION_DB_NOT_INIT;
Miklos Balint386b8b52017-11-29 13:12:32 +0000318 }
319
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100320 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000321
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200322 return tfm_spm_hal_partition_sandbox_config(&(part->memory_data),
323 part->platform_data);
Miklos Balint386b8b52017-11-29 13:12:32 +0000324
Miklos Balint386b8b52017-11-29 13:12:32 +0000325}
326
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100327enum spm_err_t tfm_spm_partition_sandbox_deconfig(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000328{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100329 /* This function takes a partition id and disables the
330 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000331 */
332
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200333 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000334
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100335 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000336
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200337 return tfm_spm_hal_partition_sandbox_deconfig(&(part->memory_data),
338 part->platform_data);
Miklos Balint386b8b52017-11-29 13:12:32 +0000339}
340
Mate Toth-Pal21a74c92018-04-13 14:05:41 +0200341uint32_t tfm_spm_partition_get_zi_start(uint32_t partition_idx)
342{
343 return g_spm_partition_db.partitions[partition_idx].
344 memory_data.zi_start;
345}
346
347uint32_t tfm_spm_partition_get_zi_limit(uint32_t partition_idx)
348{
349 return g_spm_partition_db.partitions[partition_idx].
350 memory_data.zi_limit;
351}
352
353uint32_t tfm_spm_partition_get_rw_start(uint32_t partition_idx)
354{
355 return g_spm_partition_db.partitions[partition_idx].
356 memory_data.rw_start;
357}
358
359uint32_t tfm_spm_partition_get_rw_limit(uint32_t partition_idx)
360{
361 return g_spm_partition_db.partitions[partition_idx].
362 memory_data.rw_limit;
363}
364
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100365void tfm_spm_partition_set_stack(uint32_t partition_idx, uint32_t stack_ptr)
Miklos Balint386b8b52017-11-29 13:12:32 +0000366{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100367 g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100368 runtime_data.stack_ptr = stack_ptr;
Miklos Balint386b8b52017-11-29 13:12:32 +0000369}
370#endif
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100371
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100372uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100373{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100374 return g_spm_partition_db.partitions[partition_idx].static_data.
375 partition_id;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100376}
377
Mate Toth-Pal59398712018-02-28 17:06:40 +0100378uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
379{
380 return g_spm_partition_db.partitions[partition_idx].static_data.
381 partition_flags;
382}
383
Summer Qinb4a854d2019-05-29 15:31:22 +0800384#ifndef TFM_PSA_API
385void tfm_spm_partition_store_context(uint32_t partition_idx,
386 uint32_t stack_ptr, uint32_t lr)
387{
388 g_spm_partition_db.partitions[partition_idx].
389 runtime_data.stack_ptr = stack_ptr;
390 g_spm_partition_db.partitions[partition_idx].
391 runtime_data.lr = lr;
392}
393
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100394const struct spm_partition_runtime_data_t *
Mate Toth-Pal59398712018-02-28 17:06:40 +0100395 tfm_spm_partition_get_runtime_data(uint32_t partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100396{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100397 return &(g_spm_partition_db.partitions[partition_idx].runtime_data);
398}
399
400void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state)
401{
402 g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state =
403 state;
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200404 if (state == SPM_PARTITION_STATE_RUNNING ||
405 state == SPM_PARTITION_STATE_HANDLING_IRQ) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100406 g_spm_partition_db.running_partition_idx = partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100407 }
408}
409
Miklos Balint6a139ae2018-04-04 19:44:37 +0200410void tfm_spm_partition_set_caller_partition_idx(uint32_t partition_idx,
411 uint32_t caller_partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100412{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100413 g_spm_partition_db.partitions[partition_idx].runtime_data.
414 caller_partition_idx = caller_partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100415}
416
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200417void tfm_spm_partition_set_signal_mask(uint32_t partition_idx,
418 uint32_t signal_mask)
419{
420 g_spm_partition_db.partitions[partition_idx].runtime_data.
421 signal_mask = signal_mask;
422}
423
Mate Toth-Pal21a74c92018-04-13 14:05:41 +0200424void tfm_spm_partition_set_caller_client_id(uint32_t partition_idx,
425 int32_t caller_client_id)
426{
427 g_spm_partition_db.partitions[partition_idx].runtime_data.
428 caller_client_id = caller_client_id;
429}
430
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100431enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100432 uint32_t share)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100433{
434 enum spm_err_t ret = SPM_ERR_OK;
435
436#if TFM_LVL != 1
437 /* Only need to set configuration on levels higher than 1 */
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200438 ret = tfm_spm_hal_set_share_region(share);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100439#endif
440
441 if (ret == SPM_ERR_OK) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100442 g_spm_partition_db.partitions[partition_idx].runtime_data.share = share;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100443 }
444 return ret;
445}
446
Hugues de Valonf704c802019-02-19 14:51:41 +0000447enum spm_err_t tfm_spm_partition_set_iovec(uint32_t partition_idx,
448 const int32_t *args)
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200449{
450 struct spm_partition_runtime_data_t *runtime_data =
451 &g_spm_partition_db.partitions[partition_idx].runtime_data;
Hugues de Valonf704c802019-02-19 14:51:41 +0000452 size_t i;
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200453
Hugues de Valonf704c802019-02-19 14:51:41 +0000454 if ((args[1] < 0) || (args[3] < 0)) {
455 return SPM_ERR_INVALID_PARAMETER;
456 }
457
458 runtime_data->iovec_args.in_len = (size_t)args[1];
459 for (i = 0U; i < runtime_data->iovec_args.in_len; ++i) {
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200460 runtime_data->iovec_args.in_vec[i].base =
461 ((psa_invec *)args[0])[i].base;
462 runtime_data->iovec_args.in_vec[i].len = ((psa_invec *)args[0])[i].len;
463 }
Hugues de Valonf704c802019-02-19 14:51:41 +0000464 runtime_data->iovec_args.out_len = (size_t)args[3];
465 for (i = 0U; i < runtime_data->iovec_args.out_len; ++i) {
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200466 runtime_data->iovec_args.out_vec[i].base =
467 ((psa_outvec *)args[2])[i].base;
468 runtime_data->iovec_args.out_vec[i].len =
469 ((psa_outvec *)args[2])[i].len;
470 }
471 runtime_data->orig_outvec = (psa_outvec *)args[2];
Mate Toth-Pal2a6f8c22018-12-13 16:37:17 +0100472 runtime_data->iovec_api = 1;
Hugues de Valonf704c802019-02-19 14:51:41 +0000473
474 return SPM_ERR_OK;
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200475}
476
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100477uint32_t tfm_spm_partition_get_running_partition_idx(void)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100478{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100479 return g_spm_partition_db.running_partition_idx;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100480}
481
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100482void tfm_spm_partition_cleanup_context(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100483{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200484 struct spm_partition_desc_t *partition =
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100485 &(g_spm_partition_db.partitions[partition_idx]);
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200486 int32_t i;
487
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100488 partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100489 partition->runtime_data.share = 0;
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200490 partition->runtime_data.iovec_args.in_len = 0;
491 for (i = 0; i < PSA_MAX_IOVEC; ++i) {
492 partition->runtime_data.iovec_args.in_vec[i].base = 0;
493 partition->runtime_data.iovec_args.in_vec[i].len = 0;
494 }
495 partition->runtime_data.iovec_args.out_len = 0;
496 for (i = 0; i < PSA_MAX_IOVEC; ++i) {
497 partition->runtime_data.iovec_args.out_vec[i].base = 0;
498 partition->runtime_data.iovec_args.out_vec[i].len = 0;
499 }
500 partition->runtime_data.orig_outvec = 0;
Mate Toth-Pal2a6f8c22018-12-13 16:37:17 +0100501 partition->runtime_data.iovec_api = 0;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100502}
Summer Qinb4a854d2019-05-29 15:31:22 +0800503#endif /* !defined(TFM_PSA_API) */
Edison Aib5571352019-03-22 10:49:52 +0800504
505__attribute__((section("SFN")))
506void tfm_spm_partition_change_privilege(uint32_t privileged)
507{
508 CONTROL_Type ctrl;
509
510 ctrl.w = __get_CONTROL();
511
512 if (privileged == TFM_PARTITION_PRIVILEGED_MODE) {
513 ctrl.b.nPRIV = 0;
514 } else {
515 ctrl.b.nPRIV = 1;
516 }
517
518 __set_CONTROL(ctrl.w);
519}