blob: 3a3b2f899058902b538bb766c7ac25b0d28a72af [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
Edison Ai66fbdf12019-07-08 16:05:07 +080026#ifndef TFM_PSA_API
Miklos Balint386b8b52017-11-29 13:12:32 +000027typedef enum {
28 TFM_INIT_FAILURE,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010029} sp_error_type_t;
Miklos Balint386b8b52017-11-29 13:12:32 +000030
Mate Toth-Pal4341de02018-10-02 12:55:47 +020031/* The size of this struct must be multiple of 4 bytes as it is stacked to an
32 * uint32_t[] array
33 */
34struct interrupted_ctx_stack_frame_t {
35#if TFM_LVL != 1
36 uint32_t stack_ptr;
37#endif
38 uint32_t partition_state;
39};
40
41/* The size of this struct must be multiple of 4 bytes as it is stacked to an
42 * uint32_t[] array
43 */
44struct handler_ctx_stack_frame_t {
45 uint32_t partition_state;
46 uint32_t caller_partition_idx;
47};
Edison Ai66fbdf12019-07-08 16:05:07 +080048#endif /* !defined(TFM_PSA_API) */
Mate Toth-Pal4341de02018-10-02 12:55:47 +020049
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +020050/* Define SPM DB structure */
51#include "secure_fw/services/tfm_spm_db.inc"
52
Miklos Balint386b8b52017-11-29 13:12:32 +000053/*
Mate Toth-Pal349714a2018-02-23 15:30:24 +010054 * This function is called when a secure partition causes an error.
Mate Toth-Pal65291f32018-02-23 14:35:22 +010055 * In case of an error in the error handling, a non-zero value have to be
56 * returned.
Miklos Balint386b8b52017-11-29 13:12:32 +000057 */
Summer Qinb4a854d2019-05-29 15:31:22 +080058#ifndef TFM_PSA_API
Mate Toth-Pal349714a2018-02-23 15:30:24 +010059static void tfm_spm_partition_err_handler(
Hugues de Valonf704c802019-02-19 14:51:41 +000060 const struct spm_partition_desc_t *partition,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010061 sp_error_type_t err_type,
Mate Toth-Pal65291f32018-02-23 14:35:22 +010062 int32_t err_code)
Miklos Balint386b8b52017-11-29 13:12:32 +000063{
Miklos Balint386b8b52017-11-29 13:12:32 +000064#ifdef TFM_CORE_DEBUG
65 if (err_type == TFM_INIT_FAILURE) {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010066 printf("Partition init failed for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010067 partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000068 } else {
Hugues de Valonf704c802019-02-19 14:51:41 +000069 printf(
70 "Unknown partition error %d (code: %d) for partition id 0x%08X\r\n",
71 err_type, err_code, partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000072 }
Hugues de Valonf704c802019-02-19 14:51:41 +000073#else
74 (void)err_type;
75 (void)err_code;
Miklos Balint386b8b52017-11-29 13:12:32 +000076#endif
Mate Toth-Pal18b83922018-02-26 17:58:18 +010077 tfm_spm_partition_set_state(partition->static_data.partition_id,
Mate Toth-Pal349714a2018-02-23 15:30:24 +010078 SPM_PARTITION_STATE_CLOSED);
Miklos Balint386b8b52017-11-29 13:12:32 +000079}
Summer Qinb4a854d2019-05-29 15:31:22 +080080#endif /* !defined(TFM_PSA_API) */
Miklos Balint386b8b52017-11-29 13:12:32 +000081
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010082uint32_t get_partition_idx(uint32_t partition_id)
83{
Hugues de Valonf704c802019-02-19 14:51:41 +000084 uint32_t i;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010085
86 if (partition_id == INVALID_PARTITION_ID) {
87 return SPM_INVALID_PARTITION_IDX;
88 }
89
90 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
91 if (g_spm_partition_db.partitions[i].static_data.partition_id ==
92 partition_id) {
93 return i;
94 }
95 }
96 return SPM_INVALID_PARTITION_IDX;
97}
98
Miklos Balint386b8b52017-11-29 13:12:32 +000099enum spm_err_t tfm_spm_db_init(void)
100{
Edison Ai66fbdf12019-07-08 16:05:07 +0800101 struct spm_partition_desc_t *part_ptr;
102#ifndef TFM_PSA_API
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200103 static uint32_t ns_interrupt_ctx_stack[
104 sizeof(struct interrupted_ctx_stack_frame_t)/sizeof(uint32_t)] = {0};
105 static uint32_t tfm_core_interrupt_ctx_stack[
106 sizeof(struct interrupted_ctx_stack_frame_t)/sizeof(uint32_t)] = {0};
Edison Ai66fbdf12019-07-08 16:05:07 +0800107#endif /* !defined(TFM_PSA_API) */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100108
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100109 /* This function initialises partition db */
Miklos Balint386b8b52017-11-29 13:12:32 +0000110
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100111 /* There are a few partitions that are used by TF-M internally.
112 * These are explicitly added to the partition db here.
113 */
114
115 /* For the non secure Execution environment */
Miklos Balintdd02bb32019-05-26 21:13:12 +0200116#if (TFM_LVL != 1) || defined(TFM_PSA_API)
Tamas Ban56ef3022018-09-13 23:49:16 +0100117 extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[];
118 extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit[];
119 uint32_t psp_stack_bottom = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Base;
120 uint32_t psp_stack_top = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Limit;
Miklos Balintdd02bb32019-05-26 21:13:12 +0200121#endif
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200122
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100123 part_ptr = &(g_spm_partition_db.partitions[
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200124 NON_SECURE_INTERNAL_PARTITION_DB_IDX]);
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100125 part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID;
Hugues de Valon99578562019-06-18 16:08:51 +0100126#ifdef TFM_PSA_API
Edison Ai4dcae6f2019-03-18 10:13:47 +0800127 part_ptr->static_data.partition_flags = SPM_PART_FLAG_APP_ROT |
128 SPM_PART_FLAG_IPC;
129 part_ptr->static_data.partition_priority = TFM_PRIORITY_LOW;
130 part_ptr->static_data.partition_init = tfm_nspm_thread_entry;
131#else
Mate Toth-Pal59398712018-02-28 17:06:40 +0100132 part_ptr->static_data.partition_flags = 0;
Edison Ai4dcae6f2019-03-18 10:13:47 +0800133#endif
Miklos Balint6a139ae2018-04-04 19:44:37 +0200134
Miklos Balintdd02bb32019-05-26 21:13:12 +0200135#if (TFM_LVL != 1) || defined(TFM_PSA_API)
Tamas Ban56ef3022018-09-13 23:49:16 +0100136 part_ptr->memory_data.stack_bottom = psp_stack_bottom;
137 part_ptr->memory_data.stack_top = psp_stack_top;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200138 /* Since RW, ZI and stack are configured as one MPU region, configure
Tamas Ban56ef3022018-09-13 23:49:16 +0100139 * RW start address to psp_stack_bottom to get RW access to stack
Miklos Balint6a139ae2018-04-04 19:44:37 +0200140 */
Tamas Ban56ef3022018-09-13 23:49:16 +0100141 part_ptr->memory_data.rw_start = psp_stack_bottom;
Miklos Balintdd02bb32019-05-26 21:13:12 +0200142#endif
Miklos Balint6a139ae2018-04-04 19:44:37 +0200143
Edison Ai66fbdf12019-07-08 16:05:07 +0800144#ifndef TFM_PSA_API
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100145 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200146 part_ptr->runtime_data.ctx_stack_ptr = ns_interrupt_ctx_stack;
Edison Ai66fbdf12019-07-08 16:05:07 +0800147#endif /* !defined(TFM_PSA_API) */
148
Miklos Balint12735bc2018-08-01 15:45:18 +0200149 tfm_nspm_configure_clients();
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100150
151 /* For the TF-M core environment itself */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100152 part_ptr = &(g_spm_partition_db.partitions[
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200153 TFM_CORE_INTERNAL_PARTITION_DB_IDX]);
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100154 part_ptr->static_data.partition_id = TFM_SP_CORE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +0100155 part_ptr->static_data.partition_flags =
Edison Aibb614aa2018-11-21 15:15:00 +0800156 SPM_PART_FLAG_APP_ROT | SPM_PART_FLAG_PSA_ROT;
Edison Ai66fbdf12019-07-08 16:05:07 +0800157#ifndef TFM_PSA_API
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100158 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200159 part_ptr->runtime_data.ctx_stack_ptr = tfm_core_interrupt_ctx_stack;
Edison Ai66fbdf12019-07-08 16:05:07 +0800160#endif /* !defined(TFM_PSA_API) */
Miklos Balint386b8b52017-11-29 13:12:32 +0000161
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +0100162 g_spm_partition_db.is_init = 1;
163
Miklos Balint386b8b52017-11-29 13:12:32 +0000164 return SPM_ERR_OK;
165}
166
Summer Qinb4a854d2019-05-29 15:31:22 +0800167#ifndef TFM_PSA_API
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100168enum spm_err_t tfm_spm_partition_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +0000169{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200170 struct spm_partition_desc_t *part;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200171 struct tfm_sfn_req_s desc;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200172 int32_t args[4] = {0};
Miklos Balint386b8b52017-11-29 13:12:32 +0000173 int32_t fail_cnt = 0;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100174 uint32_t idx;
Miklos Balint386b8b52017-11-29 13:12:32 +0000175
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100176 /* Call the init function for each partition */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100177 for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) {
178 part = &g_spm_partition_db.partitions[idx];
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200179 tfm_spm_hal_configure_default_isolation(part->platform_data);
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100180 if (part->static_data.partition_init == NULL) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100181 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Miklos Balint6a139ae2018-04-04 19:44:37 +0200182 tfm_spm_partition_set_caller_partition_idx(idx,
183 SPM_INVALID_PARTITION_IDX);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100184 } else {
Miklos Balintace4c3f2018-07-30 12:31:15 +0200185 int32_t res;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200186
187 desc.args = args;
Hugues de Valonf704c802019-02-19 14:51:41 +0000188 desc.ns_caller = 0U;
Mate Toth-Palb8ce0dd2018-07-25 10:18:34 +0200189 desc.iovec_api = TFM_SFN_API_IOVEC;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200190 desc.sfn = (sfn_t)part->static_data.partition_init;
191 desc.sp_id = part->static_data.partition_id;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200192 res = tfm_core_sfn_request(&desc);
Hugues de Valon99578562019-06-18 16:08:51 +0100193 if (res == (int32_t)TFM_SUCCESS) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100194 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100195 } else {
Miklos Balintace4c3f2018-07-30 12:31:15 +0200196 tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, res);
Miklos Balint386b8b52017-11-29 13:12:32 +0000197 fail_cnt++;
198 }
199 }
200 }
201
Miklos Balint6a139ae2018-04-04 19:44:37 +0200202 tfm_secure_api_init_done();
203
Miklos Balint386b8b52017-11-29 13:12:32 +0000204 if (fail_cnt == 0) {
205 return SPM_ERR_OK;
206 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100207 return SPM_ERR_PARTITION_NOT_AVAILABLE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000208 }
209}
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200210
211void tfm_spm_partition_push_interrupted_ctx(uint32_t partition_idx)
212{
213 struct spm_partition_runtime_data_t *runtime_data =
214 &g_spm_partition_db.partitions[partition_idx].runtime_data;
215 struct interrupted_ctx_stack_frame_t *stack_frame =
216 (struct interrupted_ctx_stack_frame_t *)
217 runtime_data->ctx_stack_ptr;
218
219 stack_frame->partition_state = runtime_data->partition_state;
220#if TFM_LVL != 1
221 stack_frame->stack_ptr = runtime_data->stack_ptr;
222#endif
223 runtime_data->ctx_stack_ptr +=
224 sizeof(struct interrupted_ctx_stack_frame_t) / sizeof(uint32_t);
225
226}
227
228void tfm_spm_partition_pop_interrupted_ctx(uint32_t partition_idx)
229{
230 struct spm_partition_runtime_data_t *runtime_data =
231 &g_spm_partition_db.partitions[partition_idx].runtime_data;
232 struct interrupted_ctx_stack_frame_t *stack_frame;
233
234 runtime_data->ctx_stack_ptr -=
235 sizeof(struct interrupted_ctx_stack_frame_t) / sizeof(uint32_t);
236 stack_frame = (struct interrupted_ctx_stack_frame_t *)
237 runtime_data->ctx_stack_ptr;
238 tfm_spm_partition_set_state(partition_idx, stack_frame->partition_state);
239 stack_frame->partition_state = 0;
240#if TFM_LVL != 1
241 tfm_spm_partition_set_stack(partition_idx, stack_frame->stack_ptr);
242 stack_frame->stack_ptr = 0;
243#endif
244}
245
246void tfm_spm_partition_push_handler_ctx(uint32_t partition_idx)
247{
248 struct spm_partition_runtime_data_t *runtime_data =
249 &g_spm_partition_db.partitions[partition_idx].runtime_data;
250 struct handler_ctx_stack_frame_t *stack_frame =
251 (struct handler_ctx_stack_frame_t *)
252 runtime_data->ctx_stack_ptr;
253
254 stack_frame->partition_state = runtime_data->partition_state;
255 stack_frame->caller_partition_idx = runtime_data->caller_partition_idx;
256
257 runtime_data->ctx_stack_ptr +=
258 sizeof(struct handler_ctx_stack_frame_t) / sizeof(uint32_t);
259}
260
261void tfm_spm_partition_pop_handler_ctx(uint32_t partition_idx)
262{
263 struct spm_partition_runtime_data_t *runtime_data =
264 &g_spm_partition_db.partitions[partition_idx].runtime_data;
265 struct handler_ctx_stack_frame_t *stack_frame;
266
267 runtime_data->ctx_stack_ptr -=
268 sizeof(struct handler_ctx_stack_frame_t) / sizeof(uint32_t);
269
270 stack_frame = (struct handler_ctx_stack_frame_t *)
271 runtime_data->ctx_stack_ptr;
272
273 tfm_spm_partition_set_state(partition_idx, stack_frame->partition_state);
274 stack_frame->partition_state = 0;
275 tfm_spm_partition_set_caller_partition_idx(
276 partition_idx, stack_frame->caller_partition_idx);
277 stack_frame->caller_partition_idx = 0;
278
279}
280
Summer Qinb4a854d2019-05-29 15:31:22 +0800281#endif /* !defined(TFM_PSA_API) */
Miklos Balint386b8b52017-11-29 13:12:32 +0000282
Miklos Balintdd02bb32019-05-26 21:13:12 +0200283#if (TFM_LVL != 1) || defined(TFM_PSA_API)
Summer Qind00e4db2019-05-09 18:03:52 +0800284uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
285{
286 return g_spm_partition_db.partitions[partition_idx].
287 memory_data.stack_bottom;
288}
289
290uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
291{
292 return g_spm_partition_db.partitions[partition_idx].memory_data.stack_top;
293}
Miklos Balintdd02bb32019-05-26 21:13:12 +0200294#endif
Summer Qind00e4db2019-05-09 18:03:52 +0800295
Miklos Balintdd02bb32019-05-26 21:13:12 +0200296#if (TFM_LVL != 1) && !defined(TFM_PSA_API)
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100297enum spm_err_t tfm_spm_partition_sandbox_config(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000298{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200299 struct spm_partition_desc_t *part;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100300 if (!g_spm_partition_db.is_init) {
301 return SPM_ERR_PARTITION_DB_NOT_INIT;
Miklos Balint386b8b52017-11-29 13:12:32 +0000302 }
303
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100304 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000305
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200306 return tfm_spm_hal_partition_sandbox_config(&(part->memory_data),
307 part->platform_data);
Miklos Balint386b8b52017-11-29 13:12:32 +0000308
Miklos Balint386b8b52017-11-29 13:12:32 +0000309}
310
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100311enum spm_err_t tfm_spm_partition_sandbox_deconfig(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000312{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100313 /* This function takes a partition id and disables the
314 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000315 */
316
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200317 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000318
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100319 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000320
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200321 return tfm_spm_hal_partition_sandbox_deconfig(&(part->memory_data),
322 part->platform_data);
Miklos Balint386b8b52017-11-29 13:12:32 +0000323}
324
Mate Toth-Pal21a74c92018-04-13 14:05:41 +0200325uint32_t tfm_spm_partition_get_zi_start(uint32_t partition_idx)
326{
327 return g_spm_partition_db.partitions[partition_idx].
328 memory_data.zi_start;
329}
330
331uint32_t tfm_spm_partition_get_zi_limit(uint32_t partition_idx)
332{
333 return g_spm_partition_db.partitions[partition_idx].
334 memory_data.zi_limit;
335}
336
337uint32_t tfm_spm_partition_get_rw_start(uint32_t partition_idx)
338{
339 return g_spm_partition_db.partitions[partition_idx].
340 memory_data.rw_start;
341}
342
343uint32_t tfm_spm_partition_get_rw_limit(uint32_t partition_idx)
344{
345 return g_spm_partition_db.partitions[partition_idx].
346 memory_data.rw_limit;
347}
348
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100349void tfm_spm_partition_set_stack(uint32_t partition_idx, uint32_t stack_ptr)
Miklos Balint386b8b52017-11-29 13:12:32 +0000350{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100351 g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100352 runtime_data.stack_ptr = stack_ptr;
Miklos Balint386b8b52017-11-29 13:12:32 +0000353}
354#endif
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100355
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100356uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100357{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100358 return g_spm_partition_db.partitions[partition_idx].static_data.
359 partition_id;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100360}
361
Mate Toth-Pal59398712018-02-28 17:06:40 +0100362uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
363{
364 return g_spm_partition_db.partitions[partition_idx].static_data.
365 partition_flags;
366}
367
Summer Qinb4a854d2019-05-29 15:31:22 +0800368#ifndef TFM_PSA_API
369void tfm_spm_partition_store_context(uint32_t partition_idx,
370 uint32_t stack_ptr, uint32_t lr)
371{
372 g_spm_partition_db.partitions[partition_idx].
373 runtime_data.stack_ptr = stack_ptr;
374 g_spm_partition_db.partitions[partition_idx].
375 runtime_data.lr = lr;
376}
377
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100378const struct spm_partition_runtime_data_t *
Mate Toth-Pal59398712018-02-28 17:06:40 +0100379 tfm_spm_partition_get_runtime_data(uint32_t partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100380{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100381 return &(g_spm_partition_db.partitions[partition_idx].runtime_data);
382}
383
384void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state)
385{
386 g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state =
387 state;
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200388 if (state == SPM_PARTITION_STATE_RUNNING ||
389 state == SPM_PARTITION_STATE_HANDLING_IRQ) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100390 g_spm_partition_db.running_partition_idx = partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100391 }
392}
393
Miklos Balint6a139ae2018-04-04 19:44:37 +0200394void tfm_spm_partition_set_caller_partition_idx(uint32_t partition_idx,
395 uint32_t caller_partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100396{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100397 g_spm_partition_db.partitions[partition_idx].runtime_data.
398 caller_partition_idx = caller_partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100399}
400
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200401void tfm_spm_partition_set_signal_mask(uint32_t partition_idx,
402 uint32_t signal_mask)
403{
404 g_spm_partition_db.partitions[partition_idx].runtime_data.
405 signal_mask = signal_mask;
406}
407
Mate Toth-Pal21a74c92018-04-13 14:05:41 +0200408void tfm_spm_partition_set_caller_client_id(uint32_t partition_idx,
409 int32_t caller_client_id)
410{
411 g_spm_partition_db.partitions[partition_idx].runtime_data.
412 caller_client_id = caller_client_id;
413}
414
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100415enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100416 uint32_t share)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100417{
418 enum spm_err_t ret = SPM_ERR_OK;
419
420#if TFM_LVL != 1
421 /* Only need to set configuration on levels higher than 1 */
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200422 ret = tfm_spm_hal_set_share_region(share);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100423#endif
424
425 if (ret == SPM_ERR_OK) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100426 g_spm_partition_db.partitions[partition_idx].runtime_data.share = share;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100427 }
428 return ret;
429}
430
Hugues de Valonf704c802019-02-19 14:51:41 +0000431enum spm_err_t tfm_spm_partition_set_iovec(uint32_t partition_idx,
432 const int32_t *args)
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200433{
434 struct spm_partition_runtime_data_t *runtime_data =
435 &g_spm_partition_db.partitions[partition_idx].runtime_data;
Hugues de Valonf704c802019-02-19 14:51:41 +0000436 size_t i;
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200437
Hugues de Valonf704c802019-02-19 14:51:41 +0000438 if ((args[1] < 0) || (args[3] < 0)) {
439 return SPM_ERR_INVALID_PARAMETER;
440 }
441
442 runtime_data->iovec_args.in_len = (size_t)args[1];
443 for (i = 0U; i < runtime_data->iovec_args.in_len; ++i) {
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200444 runtime_data->iovec_args.in_vec[i].base =
445 ((psa_invec *)args[0])[i].base;
446 runtime_data->iovec_args.in_vec[i].len = ((psa_invec *)args[0])[i].len;
447 }
Hugues de Valonf704c802019-02-19 14:51:41 +0000448 runtime_data->iovec_args.out_len = (size_t)args[3];
449 for (i = 0U; i < runtime_data->iovec_args.out_len; ++i) {
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200450 runtime_data->iovec_args.out_vec[i].base =
451 ((psa_outvec *)args[2])[i].base;
452 runtime_data->iovec_args.out_vec[i].len =
453 ((psa_outvec *)args[2])[i].len;
454 }
455 runtime_data->orig_outvec = (psa_outvec *)args[2];
Mate Toth-Pal2a6f8c22018-12-13 16:37:17 +0100456 runtime_data->iovec_api = 1;
Hugues de Valonf704c802019-02-19 14:51:41 +0000457
458 return SPM_ERR_OK;
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200459}
460
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100461uint32_t tfm_spm_partition_get_running_partition_idx(void)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100462{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100463 return g_spm_partition_db.running_partition_idx;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100464}
465
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100466void tfm_spm_partition_cleanup_context(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100467{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200468 struct spm_partition_desc_t *partition =
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100469 &(g_spm_partition_db.partitions[partition_idx]);
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200470 int32_t i;
471
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100472 partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100473 partition->runtime_data.share = 0;
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200474 partition->runtime_data.iovec_args.in_len = 0;
475 for (i = 0; i < PSA_MAX_IOVEC; ++i) {
476 partition->runtime_data.iovec_args.in_vec[i].base = 0;
477 partition->runtime_data.iovec_args.in_vec[i].len = 0;
478 }
479 partition->runtime_data.iovec_args.out_len = 0;
480 for (i = 0; i < PSA_MAX_IOVEC; ++i) {
481 partition->runtime_data.iovec_args.out_vec[i].base = 0;
482 partition->runtime_data.iovec_args.out_vec[i].len = 0;
483 }
484 partition->runtime_data.orig_outvec = 0;
Mate Toth-Pal2a6f8c22018-12-13 16:37:17 +0100485 partition->runtime_data.iovec_api = 0;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100486}
Summer Qinb4a854d2019-05-29 15:31:22 +0800487#endif /* !defined(TFM_PSA_API) */
Edison Aib5571352019-03-22 10:49:52 +0800488
489__attribute__((section("SFN")))
490void tfm_spm_partition_change_privilege(uint32_t privileged)
491{
492 CONTROL_Type ctrl;
493
494 ctrl.w = __get_CONTROL();
495
496 if (privileged == TFM_PARTITION_PRIVILEGED_MODE) {
497 ctrl.b.nPRIV = 0;
498 } else {
499 ctrl.b.nPRIV = 1;
500 }
501
502 __set_CONTROL(ctrl.w);
503}