blob: ffca8494128425e2d00c90d4b4f159ffa7137d61 [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-Pal3ad2e3e2019-07-11 21:43:37 +020015#include "spm_db.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
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +020023#define NON_SECURE_INTERNAL_PARTITION_DB_IDX 0
24#define TFM_CORE_INTERNAL_PARTITION_DB_IDX 1
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
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +020048/* Define SPM DB structure */
49#include "secure_fw/services/tfm_spm_db.inc"
50
Miklos Balint386b8b52017-11-29 13:12:32 +000051/*
Mate Toth-Pal349714a2018-02-23 15:30:24 +010052 * This function is called when a secure partition causes an error.
Mate Toth-Pal65291f32018-02-23 14:35:22 +010053 * In case of an error in the error handling, a non-zero value have to be
54 * returned.
Miklos Balint386b8b52017-11-29 13:12:32 +000055 */
Summer Qinb4a854d2019-05-29 15:31:22 +080056#ifndef TFM_PSA_API
Mate Toth-Pal349714a2018-02-23 15:30:24 +010057static void tfm_spm_partition_err_handler(
Hugues de Valonf704c802019-02-19 14:51:41 +000058 const struct spm_partition_desc_t *partition,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010059 sp_error_type_t err_type,
Mate Toth-Pal65291f32018-02-23 14:35:22 +010060 int32_t err_code)
Miklos Balint386b8b52017-11-29 13:12:32 +000061{
Miklos Balint386b8b52017-11-29 13:12:32 +000062#ifdef TFM_CORE_DEBUG
63 if (err_type == TFM_INIT_FAILURE) {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010064 printf("Partition init failed for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010065 partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000066 } else {
Hugues de Valonf704c802019-02-19 14:51:41 +000067 printf(
68 "Unknown partition error %d (code: %d) for partition id 0x%08X\r\n",
69 err_type, err_code, partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000070 }
Hugues de Valonf704c802019-02-19 14:51:41 +000071#else
72 (void)err_type;
73 (void)err_code;
Miklos Balint386b8b52017-11-29 13:12:32 +000074#endif
Mate Toth-Pal18b83922018-02-26 17:58:18 +010075 tfm_spm_partition_set_state(partition->static_data.partition_id,
Mate Toth-Pal349714a2018-02-23 15:30:24 +010076 SPM_PARTITION_STATE_CLOSED);
Miklos Balint386b8b52017-11-29 13:12:32 +000077}
Summer Qinb4a854d2019-05-29 15:31:22 +080078#endif /* !defined(TFM_PSA_API) */
Miklos Balint386b8b52017-11-29 13:12:32 +000079
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010080uint32_t get_partition_idx(uint32_t partition_id)
81{
Hugues de Valonf704c802019-02-19 14:51:41 +000082 uint32_t i;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010083
84 if (partition_id == INVALID_PARTITION_ID) {
85 return SPM_INVALID_PARTITION_IDX;
86 }
87
88 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
89 if (g_spm_partition_db.partitions[i].static_data.partition_id ==
90 partition_id) {
91 return i;
92 }
93 }
94 return SPM_INVALID_PARTITION_IDX;
95}
96
Miklos Balint386b8b52017-11-29 13:12:32 +000097enum spm_err_t tfm_spm_db_init(void)
98{
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +020099 struct spm_partition_desc_t *part_ptr;
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200100 static uint32_t ns_interrupt_ctx_stack[
101 sizeof(struct interrupted_ctx_stack_frame_t)/sizeof(uint32_t)] = {0};
102 static uint32_t tfm_core_interrupt_ctx_stack[
103 sizeof(struct interrupted_ctx_stack_frame_t)/sizeof(uint32_t)] = {0};
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100104
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100105 /* This function initialises partition db */
Miklos Balint386b8b52017-11-29 13:12:32 +0000106
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100107 /* There are a few partitions that are used by TF-M internally.
108 * These are explicitly added to the partition db here.
109 */
110
111 /* For the non secure Execution environment */
Miklos Balintdd02bb32019-05-26 21:13:12 +0200112#if (TFM_LVL != 1) || defined(TFM_PSA_API)
Tamas Ban56ef3022018-09-13 23:49:16 +0100113 extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[];
114 extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit[];
115 uint32_t psp_stack_bottom = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Base;
116 uint32_t psp_stack_top = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Limit;
Miklos Balintdd02bb32019-05-26 21:13:12 +0200117#endif
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200118
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100119 part_ptr = &(g_spm_partition_db.partitions[
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200120 NON_SECURE_INTERNAL_PARTITION_DB_IDX]);
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100121 part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID;
Hugues de Valon99578562019-06-18 16:08:51 +0100122#ifdef TFM_PSA_API
Edison Ai4dcae6f2019-03-18 10:13:47 +0800123 part_ptr->static_data.partition_flags = SPM_PART_FLAG_APP_ROT |
124 SPM_PART_FLAG_IPC;
125 part_ptr->static_data.partition_priority = TFM_PRIORITY_LOW;
126 part_ptr->static_data.partition_init = tfm_nspm_thread_entry;
127#else
Mate Toth-Pal59398712018-02-28 17:06:40 +0100128 part_ptr->static_data.partition_flags = 0;
Edison Ai4dcae6f2019-03-18 10:13:47 +0800129#endif
Miklos Balint6a139ae2018-04-04 19:44:37 +0200130
Miklos Balintdd02bb32019-05-26 21:13:12 +0200131#if (TFM_LVL != 1) || defined(TFM_PSA_API)
Tamas Ban56ef3022018-09-13 23:49:16 +0100132 part_ptr->memory_data.stack_bottom = psp_stack_bottom;
133 part_ptr->memory_data.stack_top = psp_stack_top;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200134 /* Since RW, ZI and stack are configured as one MPU region, configure
Tamas Ban56ef3022018-09-13 23:49:16 +0100135 * RW start address to psp_stack_bottom to get RW access to stack
Miklos Balint6a139ae2018-04-04 19:44:37 +0200136 */
Tamas Ban56ef3022018-09-13 23:49:16 +0100137 part_ptr->memory_data.rw_start = psp_stack_bottom;
Miklos Balintdd02bb32019-05-26 21:13:12 +0200138#endif
Miklos Balint6a139ae2018-04-04 19:44:37 +0200139
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100140 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200141 part_ptr->runtime_data.ctx_stack_ptr = ns_interrupt_ctx_stack;
Miklos Balint12735bc2018-08-01 15:45:18 +0200142 tfm_nspm_configure_clients();
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100143
144 /* For the TF-M core environment itself */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100145 part_ptr = &(g_spm_partition_db.partitions[
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200146 TFM_CORE_INTERNAL_PARTITION_DB_IDX]);
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100147 part_ptr->static_data.partition_id = TFM_SP_CORE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +0100148 part_ptr->static_data.partition_flags =
Edison Aibb614aa2018-11-21 15:15:00 +0800149 SPM_PART_FLAG_APP_ROT | SPM_PART_FLAG_PSA_ROT;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100150 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200151 part_ptr->runtime_data.ctx_stack_ptr = tfm_core_interrupt_ctx_stack;
Miklos Balint386b8b52017-11-29 13:12:32 +0000152
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +0100153 g_spm_partition_db.is_init = 1;
154
Miklos Balint386b8b52017-11-29 13:12:32 +0000155 return SPM_ERR_OK;
156}
157
Summer Qinb4a854d2019-05-29 15:31:22 +0800158#ifndef TFM_PSA_API
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100159enum spm_err_t tfm_spm_partition_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +0000160{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200161 struct spm_partition_desc_t *part;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200162 struct tfm_sfn_req_s desc;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200163 int32_t args[4] = {0};
Miklos Balint386b8b52017-11-29 13:12:32 +0000164 int32_t fail_cnt = 0;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100165 uint32_t idx;
Miklos Balint386b8b52017-11-29 13:12:32 +0000166
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100167 /* Call the init function for each partition */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100168 for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) {
169 part = &g_spm_partition_db.partitions[idx];
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200170 tfm_spm_hal_configure_default_isolation(part->platform_data);
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100171 if (part->static_data.partition_init == NULL) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100172 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Miklos Balint6a139ae2018-04-04 19:44:37 +0200173 tfm_spm_partition_set_caller_partition_idx(idx,
174 SPM_INVALID_PARTITION_IDX);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100175 } else {
Miklos Balintace4c3f2018-07-30 12:31:15 +0200176 int32_t res;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200177
178 desc.args = args;
Hugues de Valonf704c802019-02-19 14:51:41 +0000179 desc.ns_caller = 0U;
Mate Toth-Palb8ce0dd2018-07-25 10:18:34 +0200180 desc.iovec_api = TFM_SFN_API_IOVEC;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200181 desc.sfn = (sfn_t)part->static_data.partition_init;
182 desc.sp_id = part->static_data.partition_id;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200183 res = tfm_core_sfn_request(&desc);
Hugues de Valon99578562019-06-18 16:08:51 +0100184 if (res == (int32_t)TFM_SUCCESS) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100185 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100186 } else {
Miklos Balintace4c3f2018-07-30 12:31:15 +0200187 tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, res);
Miklos Balint386b8b52017-11-29 13:12:32 +0000188 fail_cnt++;
189 }
190 }
191 }
192
Miklos Balint6a139ae2018-04-04 19:44:37 +0200193 tfm_secure_api_init_done();
194
Miklos Balint386b8b52017-11-29 13:12:32 +0000195 if (fail_cnt == 0) {
196 return SPM_ERR_OK;
197 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100198 return SPM_ERR_PARTITION_NOT_AVAILABLE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000199 }
200}
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200201
202void tfm_spm_partition_push_interrupted_ctx(uint32_t partition_idx)
203{
204 struct spm_partition_runtime_data_t *runtime_data =
205 &g_spm_partition_db.partitions[partition_idx].runtime_data;
206 struct interrupted_ctx_stack_frame_t *stack_frame =
207 (struct interrupted_ctx_stack_frame_t *)
208 runtime_data->ctx_stack_ptr;
209
210 stack_frame->partition_state = runtime_data->partition_state;
211#if TFM_LVL != 1
212 stack_frame->stack_ptr = runtime_data->stack_ptr;
213#endif
214 runtime_data->ctx_stack_ptr +=
215 sizeof(struct interrupted_ctx_stack_frame_t) / sizeof(uint32_t);
216
217}
218
219void tfm_spm_partition_pop_interrupted_ctx(uint32_t partition_idx)
220{
221 struct spm_partition_runtime_data_t *runtime_data =
222 &g_spm_partition_db.partitions[partition_idx].runtime_data;
223 struct interrupted_ctx_stack_frame_t *stack_frame;
224
225 runtime_data->ctx_stack_ptr -=
226 sizeof(struct interrupted_ctx_stack_frame_t) / sizeof(uint32_t);
227 stack_frame = (struct interrupted_ctx_stack_frame_t *)
228 runtime_data->ctx_stack_ptr;
229 tfm_spm_partition_set_state(partition_idx, stack_frame->partition_state);
230 stack_frame->partition_state = 0;
231#if TFM_LVL != 1
232 tfm_spm_partition_set_stack(partition_idx, stack_frame->stack_ptr);
233 stack_frame->stack_ptr = 0;
234#endif
235}
236
237void tfm_spm_partition_push_handler_ctx(uint32_t partition_idx)
238{
239 struct spm_partition_runtime_data_t *runtime_data =
240 &g_spm_partition_db.partitions[partition_idx].runtime_data;
241 struct handler_ctx_stack_frame_t *stack_frame =
242 (struct handler_ctx_stack_frame_t *)
243 runtime_data->ctx_stack_ptr;
244
245 stack_frame->partition_state = runtime_data->partition_state;
246 stack_frame->caller_partition_idx = runtime_data->caller_partition_idx;
247
248 runtime_data->ctx_stack_ptr +=
249 sizeof(struct handler_ctx_stack_frame_t) / sizeof(uint32_t);
250}
251
252void tfm_spm_partition_pop_handler_ctx(uint32_t partition_idx)
253{
254 struct spm_partition_runtime_data_t *runtime_data =
255 &g_spm_partition_db.partitions[partition_idx].runtime_data;
256 struct handler_ctx_stack_frame_t *stack_frame;
257
258 runtime_data->ctx_stack_ptr -=
259 sizeof(struct handler_ctx_stack_frame_t) / sizeof(uint32_t);
260
261 stack_frame = (struct handler_ctx_stack_frame_t *)
262 runtime_data->ctx_stack_ptr;
263
264 tfm_spm_partition_set_state(partition_idx, stack_frame->partition_state);
265 stack_frame->partition_state = 0;
266 tfm_spm_partition_set_caller_partition_idx(
267 partition_idx, stack_frame->caller_partition_idx);
268 stack_frame->caller_partition_idx = 0;
269
270}
271
Summer Qinb4a854d2019-05-29 15:31:22 +0800272#endif /* !defined(TFM_PSA_API) */
Miklos Balint386b8b52017-11-29 13:12:32 +0000273
Miklos Balintdd02bb32019-05-26 21:13:12 +0200274#if (TFM_LVL != 1) || defined(TFM_PSA_API)
Summer Qind00e4db2019-05-09 18:03:52 +0800275uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
276{
277 return g_spm_partition_db.partitions[partition_idx].
278 memory_data.stack_bottom;
279}
280
281uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
282{
283 return g_spm_partition_db.partitions[partition_idx].memory_data.stack_top;
284}
Miklos Balintdd02bb32019-05-26 21:13:12 +0200285#endif
Summer Qind00e4db2019-05-09 18:03:52 +0800286
Miklos Balintdd02bb32019-05-26 21:13:12 +0200287#if (TFM_LVL != 1) && !defined(TFM_PSA_API)
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100288enum spm_err_t tfm_spm_partition_sandbox_config(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000289{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200290 struct spm_partition_desc_t *part;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100291 if (!g_spm_partition_db.is_init) {
292 return SPM_ERR_PARTITION_DB_NOT_INIT;
Miklos Balint386b8b52017-11-29 13:12:32 +0000293 }
294
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100295 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000296
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200297 return tfm_spm_hal_partition_sandbox_config(&(part->memory_data),
298 part->platform_data);
Miklos Balint386b8b52017-11-29 13:12:32 +0000299
Miklos Balint386b8b52017-11-29 13:12:32 +0000300}
301
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100302enum spm_err_t tfm_spm_partition_sandbox_deconfig(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000303{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100304 /* This function takes a partition id and disables the
305 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000306 */
307
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200308 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000309
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100310 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000311
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200312 return tfm_spm_hal_partition_sandbox_deconfig(&(part->memory_data),
313 part->platform_data);
Miklos Balint386b8b52017-11-29 13:12:32 +0000314}
315
Mate Toth-Pal21a74c92018-04-13 14:05:41 +0200316uint32_t tfm_spm_partition_get_zi_start(uint32_t partition_idx)
317{
318 return g_spm_partition_db.partitions[partition_idx].
319 memory_data.zi_start;
320}
321
322uint32_t tfm_spm_partition_get_zi_limit(uint32_t partition_idx)
323{
324 return g_spm_partition_db.partitions[partition_idx].
325 memory_data.zi_limit;
326}
327
328uint32_t tfm_spm_partition_get_rw_start(uint32_t partition_idx)
329{
330 return g_spm_partition_db.partitions[partition_idx].
331 memory_data.rw_start;
332}
333
334uint32_t tfm_spm_partition_get_rw_limit(uint32_t partition_idx)
335{
336 return g_spm_partition_db.partitions[partition_idx].
337 memory_data.rw_limit;
338}
339
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100340void tfm_spm_partition_set_stack(uint32_t partition_idx, uint32_t stack_ptr)
Miklos Balint386b8b52017-11-29 13:12:32 +0000341{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100342 g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100343 runtime_data.stack_ptr = stack_ptr;
Miklos Balint386b8b52017-11-29 13:12:32 +0000344}
345#endif
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100346
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100347uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100348{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100349 return g_spm_partition_db.partitions[partition_idx].static_data.
350 partition_id;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100351}
352
Mate Toth-Pal59398712018-02-28 17:06:40 +0100353uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
354{
355 return g_spm_partition_db.partitions[partition_idx].static_data.
356 partition_flags;
357}
358
Summer Qinb4a854d2019-05-29 15:31:22 +0800359#ifndef TFM_PSA_API
360void tfm_spm_partition_store_context(uint32_t partition_idx,
361 uint32_t stack_ptr, uint32_t lr)
362{
363 g_spm_partition_db.partitions[partition_idx].
364 runtime_data.stack_ptr = stack_ptr;
365 g_spm_partition_db.partitions[partition_idx].
366 runtime_data.lr = lr;
367}
368
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100369const struct spm_partition_runtime_data_t *
Mate Toth-Pal59398712018-02-28 17:06:40 +0100370 tfm_spm_partition_get_runtime_data(uint32_t partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100371{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100372 return &(g_spm_partition_db.partitions[partition_idx].runtime_data);
373}
374
375void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state)
376{
377 g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state =
378 state;
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200379 if (state == SPM_PARTITION_STATE_RUNNING ||
380 state == SPM_PARTITION_STATE_HANDLING_IRQ) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100381 g_spm_partition_db.running_partition_idx = partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100382 }
383}
384
Miklos Balint6a139ae2018-04-04 19:44:37 +0200385void tfm_spm_partition_set_caller_partition_idx(uint32_t partition_idx,
386 uint32_t caller_partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100387{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100388 g_spm_partition_db.partitions[partition_idx].runtime_data.
389 caller_partition_idx = caller_partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100390}
391
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200392void tfm_spm_partition_set_signal_mask(uint32_t partition_idx,
393 uint32_t signal_mask)
394{
395 g_spm_partition_db.partitions[partition_idx].runtime_data.
396 signal_mask = signal_mask;
397}
398
Mate Toth-Pal21a74c92018-04-13 14:05:41 +0200399void tfm_spm_partition_set_caller_client_id(uint32_t partition_idx,
400 int32_t caller_client_id)
401{
402 g_spm_partition_db.partitions[partition_idx].runtime_data.
403 caller_client_id = caller_client_id;
404}
405
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100406enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100407 uint32_t share)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100408{
409 enum spm_err_t ret = SPM_ERR_OK;
410
411#if TFM_LVL != 1
412 /* Only need to set configuration on levels higher than 1 */
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200413 ret = tfm_spm_hal_set_share_region(share);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100414#endif
415
416 if (ret == SPM_ERR_OK) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100417 g_spm_partition_db.partitions[partition_idx].runtime_data.share = share;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100418 }
419 return ret;
420}
421
Hugues de Valonf704c802019-02-19 14:51:41 +0000422enum spm_err_t tfm_spm_partition_set_iovec(uint32_t partition_idx,
423 const int32_t *args)
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200424{
425 struct spm_partition_runtime_data_t *runtime_data =
426 &g_spm_partition_db.partitions[partition_idx].runtime_data;
Hugues de Valonf704c802019-02-19 14:51:41 +0000427 size_t i;
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200428
Hugues de Valonf704c802019-02-19 14:51:41 +0000429 if ((args[1] < 0) || (args[3] < 0)) {
430 return SPM_ERR_INVALID_PARAMETER;
431 }
432
433 runtime_data->iovec_args.in_len = (size_t)args[1];
434 for (i = 0U; i < runtime_data->iovec_args.in_len; ++i) {
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200435 runtime_data->iovec_args.in_vec[i].base =
436 ((psa_invec *)args[0])[i].base;
437 runtime_data->iovec_args.in_vec[i].len = ((psa_invec *)args[0])[i].len;
438 }
Hugues de Valonf704c802019-02-19 14:51:41 +0000439 runtime_data->iovec_args.out_len = (size_t)args[3];
440 for (i = 0U; i < runtime_data->iovec_args.out_len; ++i) {
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200441 runtime_data->iovec_args.out_vec[i].base =
442 ((psa_outvec *)args[2])[i].base;
443 runtime_data->iovec_args.out_vec[i].len =
444 ((psa_outvec *)args[2])[i].len;
445 }
446 runtime_data->orig_outvec = (psa_outvec *)args[2];
Mate Toth-Pal2a6f8c22018-12-13 16:37:17 +0100447 runtime_data->iovec_api = 1;
Hugues de Valonf704c802019-02-19 14:51:41 +0000448
449 return SPM_ERR_OK;
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200450}
451
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100452uint32_t tfm_spm_partition_get_running_partition_idx(void)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100453{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100454 return g_spm_partition_db.running_partition_idx;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100455}
456
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100457void tfm_spm_partition_cleanup_context(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100458{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200459 struct spm_partition_desc_t *partition =
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100460 &(g_spm_partition_db.partitions[partition_idx]);
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200461 int32_t i;
462
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100463 partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100464 partition->runtime_data.share = 0;
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200465 partition->runtime_data.iovec_args.in_len = 0;
466 for (i = 0; i < PSA_MAX_IOVEC; ++i) {
467 partition->runtime_data.iovec_args.in_vec[i].base = 0;
468 partition->runtime_data.iovec_args.in_vec[i].len = 0;
469 }
470 partition->runtime_data.iovec_args.out_len = 0;
471 for (i = 0; i < PSA_MAX_IOVEC; ++i) {
472 partition->runtime_data.iovec_args.out_vec[i].base = 0;
473 partition->runtime_data.iovec_args.out_vec[i].len = 0;
474 }
475 partition->runtime_data.orig_outvec = 0;
Mate Toth-Pal2a6f8c22018-12-13 16:37:17 +0100476 partition->runtime_data.iovec_api = 0;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100477}
Summer Qinb4a854d2019-05-29 15:31:22 +0800478#endif /* !defined(TFM_PSA_API) */
Edison Aib5571352019-03-22 10:49:52 +0800479
480__attribute__((section("SFN")))
481void tfm_spm_partition_change_privilege(uint32_t privileged)
482{
483 CONTROL_Type ctrl;
484
485 ctrl.w = __get_CONTROL();
486
487 if (privileged == TFM_PARTITION_PRIVILEGED_MODE) {
488 ctrl.b.nPRIV = 0;
489 } else {
490 ctrl.b.nPRIV = 1;
491 }
492
493 __set_CONTROL(ctrl.w);
494}