blob: 4d56a911d27f89d557aefa3ed0ab0ba0389dbe42 [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
30/*
Mate Toth-Pal349714a2018-02-23 15:30:24 +010031 * This function is called when a secure partition causes an error.
Mate Toth-Pal65291f32018-02-23 14:35:22 +010032 * In case of an error in the error handling, a non-zero value have to be
33 * returned.
Miklos Balint386b8b52017-11-29 13:12:32 +000034 */
Mate Toth-Pal349714a2018-02-23 15:30:24 +010035static void tfm_spm_partition_err_handler(
Hugues de Valonf704c802019-02-19 14:51:41 +000036 const struct spm_partition_desc_t *partition,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010037 sp_error_type_t err_type,
Mate Toth-Pal65291f32018-02-23 14:35:22 +010038 int32_t err_code)
Miklos Balint386b8b52017-11-29 13:12:32 +000039{
Miklos Balint386b8b52017-11-29 13:12:32 +000040#ifdef TFM_CORE_DEBUG
41 if (err_type == TFM_INIT_FAILURE) {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010042 printf("Partition init failed for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010043 partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000044 } else {
Hugues de Valonf704c802019-02-19 14:51:41 +000045 printf(
46 "Unknown partition error %d (code: %d) for partition id 0x%08X\r\n",
47 err_type, err_code, partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000048 }
Hugues de Valonf704c802019-02-19 14:51:41 +000049#else
50 (void)err_type;
51 (void)err_code;
Miklos Balint386b8b52017-11-29 13:12:32 +000052#endif
Mate Toth-Pal18b83922018-02-26 17:58:18 +010053 tfm_spm_partition_set_state(partition->static_data.partition_id,
Mate Toth-Pal349714a2018-02-23 15:30:24 +010054 SPM_PARTITION_STATE_CLOSED);
Miklos Balint386b8b52017-11-29 13:12:32 +000055}
56
Hugues de Valonf704c802019-02-19 14:51:41 +000057/*
58 * This function prevents name clashes between the variable names accessibles in
59 * the scope of where tfm_partition_list.inc is included and the varaible names
60 * defined inside tfm_partition_list.inc file.
61 */
62static inline enum spm_err_t add_user_defined_partitions(void) {
63 #include "secure_fw/services/tfm_partition_list.inc"
64
65 return SPM_ERR_OK;
66}
67
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010068uint32_t get_partition_idx(uint32_t partition_id)
69{
Hugues de Valonf704c802019-02-19 14:51:41 +000070 uint32_t i;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010071
72 if (partition_id == INVALID_PARTITION_ID) {
73 return SPM_INVALID_PARTITION_IDX;
74 }
75
76 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
77 if (g_spm_partition_db.partitions[i].static_data.partition_id ==
78 partition_id) {
79 return i;
80 }
81 }
82 return SPM_INVALID_PARTITION_IDX;
83}
84
Miklos Balint386b8b52017-11-29 13:12:32 +000085enum spm_err_t tfm_spm_db_init(void)
86{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020087 struct spm_partition_desc_t *part_ptr;
Hugues de Valonf704c802019-02-19 14:51:41 +000088 enum spm_err_t err;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010089
Hugues de Valonf704c802019-02-19 14:51:41 +000090 (void)tfm_memset (&g_spm_partition_db, 0, sizeof(g_spm_partition_db));
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +010091
Mate Toth-Pal349714a2018-02-23 15:30:24 +010092 /* This function initialises partition db */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010093 g_spm_partition_db.running_partition_idx = SPM_INVALID_PARTITION_IDX;
94 g_spm_partition_db.partition_count = 0;
Miklos Balint386b8b52017-11-29 13:12:32 +000095
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010096 /* There are a few partitions that are used by TF-M internally.
97 * These are explicitly added to the partition db here.
98 */
99
100 /* For the non secure Execution environment */
Miklos Balintdd02bb32019-05-26 21:13:12 +0200101#if (TFM_LVL != 1) || defined(TFM_PSA_API)
Tamas Ban56ef3022018-09-13 23:49:16 +0100102 extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[];
103 extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit[];
104 uint32_t psp_stack_bottom = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Base;
105 uint32_t psp_stack_top = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Limit;
Miklos Balintdd02bb32019-05-26 21:13:12 +0200106#endif
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100107 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
108 return SPM_ERR_INVALID_CONFIG;
109 }
110 part_ptr = &(g_spm_partition_db.partitions[
111 g_spm_partition_db.partition_count]);
112 part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID;
Edison Ai4dcae6f2019-03-18 10:13:47 +0800113#if TFM_PSA_API
114 part_ptr->static_data.partition_flags = SPM_PART_FLAG_APP_ROT |
115 SPM_PART_FLAG_IPC;
116 part_ptr->static_data.partition_priority = TFM_PRIORITY_LOW;
117 part_ptr->static_data.partition_init = tfm_nspm_thread_entry;
118#else
Mate Toth-Pal59398712018-02-28 17:06:40 +0100119 part_ptr->static_data.partition_flags = 0;
Edison Ai4dcae6f2019-03-18 10:13:47 +0800120#endif
Miklos Balint6a139ae2018-04-04 19:44:37 +0200121
Miklos Balintdd02bb32019-05-26 21:13:12 +0200122#if (TFM_LVL != 1) || defined(TFM_PSA_API)
Tamas Ban56ef3022018-09-13 23:49:16 +0100123 part_ptr->memory_data.stack_bottom = psp_stack_bottom;
124 part_ptr->memory_data.stack_top = psp_stack_top;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200125 /* Since RW, ZI and stack are configured as one MPU region, configure
Tamas Ban56ef3022018-09-13 23:49:16 +0100126 * RW start address to psp_stack_bottom to get RW access to stack
Miklos Balint6a139ae2018-04-04 19:44:37 +0200127 */
Tamas Ban56ef3022018-09-13 23:49:16 +0100128 part_ptr->memory_data.rw_start = psp_stack_bottom;
Miklos Balintdd02bb32019-05-26 21:13:12 +0200129#endif
Miklos Balint6a139ae2018-04-04 19:44:37 +0200130
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100131 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
Miklos Balint12735bc2018-08-01 15:45:18 +0200132 tfm_nspm_configure_clients();
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100133 ++g_spm_partition_db.partition_count;
134
135 /* For the TF-M core environment itself */
136 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
137 return SPM_ERR_INVALID_CONFIG;
138 }
139 part_ptr = &(g_spm_partition_db.partitions[
140 g_spm_partition_db.partition_count]);
141 part_ptr->static_data.partition_id = TFM_SP_CORE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +0100142 part_ptr->static_data.partition_flags =
Edison Aibb614aa2018-11-21 15:15:00 +0800143 SPM_PART_FLAG_APP_ROT | SPM_PART_FLAG_PSA_ROT;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100144 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
145 ++g_spm_partition_db.partition_count;
146
Hugues de Valonf704c802019-02-19 14:51:41 +0000147 err = add_user_defined_partitions();
148 if (err != SPM_ERR_OK) {
149 return err;
150 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000151
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +0100152 g_spm_partition_db.is_init = 1;
153
Miklos Balint386b8b52017-11-29 13:12:32 +0000154 return SPM_ERR_OK;
155}
156
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100157enum spm_err_t tfm_spm_partition_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +0000158{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200159 struct spm_partition_desc_t *part;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200160 struct tfm_sfn_req_s desc;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200161 int32_t args[4] = {0};
Miklos Balint386b8b52017-11-29 13:12:32 +0000162 int32_t fail_cnt = 0;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100163 uint32_t idx;
Miklos Balint386b8b52017-11-29 13:12:32 +0000164
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100165 /* Call the init function for each partition */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100166 for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) {
167 part = &g_spm_partition_db.partitions[idx];
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200168 tfm_spm_hal_configure_default_isolation(part->platform_data);
Edison Aibb614aa2018-11-21 15:15:00 +0800169#ifdef TFM_PSA_API
170 if (part->static_data.partition_flags & SPM_PART_FLAG_IPC) {
171 continue;
172 }
173#endif
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100174 if (part->static_data.partition_init == NULL) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100175 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Miklos Balint6a139ae2018-04-04 19:44:37 +0200176 tfm_spm_partition_set_caller_partition_idx(idx,
177 SPM_INVALID_PARTITION_IDX);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100178 } else {
Miklos Balintace4c3f2018-07-30 12:31:15 +0200179 int32_t res;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200180
181 desc.args = args;
Hugues de Valonf704c802019-02-19 14:51:41 +0000182 desc.ns_caller = 0U;
Mate Toth-Palb8ce0dd2018-07-25 10:18:34 +0200183 desc.iovec_api = TFM_SFN_API_IOVEC;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200184 desc.sfn = (sfn_t)part->static_data.partition_init;
185 desc.sp_id = part->static_data.partition_id;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200186 res = tfm_core_sfn_request(&desc);
187 if (res == TFM_SUCCESS) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100188 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100189 } else {
Miklos Balintace4c3f2018-07-30 12:31:15 +0200190 tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, res);
Miklos Balint386b8b52017-11-29 13:12:32 +0000191 fail_cnt++;
192 }
193 }
194 }
195
Miklos Balintdd02bb32019-05-26 21:13:12 +0200196#ifndef TFM_PSA_API
197 /* Not applicable if IPC messaging is used */
Miklos Balint6a139ae2018-04-04 19:44:37 +0200198 tfm_secure_api_init_done();
Miklos Balintdd02bb32019-05-26 21:13:12 +0200199#endif
Miklos Balint6a139ae2018-04-04 19:44:37 +0200200
Miklos Balint386b8b52017-11-29 13:12:32 +0000201 if (fail_cnt == 0) {
202 return SPM_ERR_OK;
203 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100204 return SPM_ERR_PARTITION_NOT_AVAILABLE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000205 }
206}
207
Miklos Balintdd02bb32019-05-26 21:13:12 +0200208#if (TFM_LVL != 1) || defined(TFM_PSA_API)
Summer Qind00e4db2019-05-09 18:03:52 +0800209uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
210{
211 return g_spm_partition_db.partitions[partition_idx].
212 memory_data.stack_bottom;
213}
214
215uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
216{
217 return g_spm_partition_db.partitions[partition_idx].memory_data.stack_top;
218}
Miklos Balintdd02bb32019-05-26 21:13:12 +0200219#endif
Summer Qind00e4db2019-05-09 18:03:52 +0800220
Miklos Balintdd02bb32019-05-26 21:13:12 +0200221#if (TFM_LVL != 1) && !defined(TFM_PSA_API)
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100222enum spm_err_t tfm_spm_partition_sandbox_config(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000223{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200224 struct spm_partition_desc_t *part;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100225 if (!g_spm_partition_db.is_init) {
226 return SPM_ERR_PARTITION_DB_NOT_INIT;
Miklos Balint386b8b52017-11-29 13:12:32 +0000227 }
228
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100229 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000230
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200231 return tfm_spm_hal_partition_sandbox_config(&(part->memory_data),
232 part->platform_data);
Miklos Balint386b8b52017-11-29 13:12:32 +0000233
Miklos Balint386b8b52017-11-29 13:12:32 +0000234}
235
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100236enum spm_err_t tfm_spm_partition_sandbox_deconfig(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000237{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100238 /* This function takes a partition id and disables the
239 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000240 */
241
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200242 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000243
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100244 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000245
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200246 return tfm_spm_hal_partition_sandbox_deconfig(&(part->memory_data),
247 part->platform_data);
Miklos Balint386b8b52017-11-29 13:12:32 +0000248}
249
Mate Toth-Pal21a74c92018-04-13 14:05:41 +0200250uint32_t tfm_spm_partition_get_zi_start(uint32_t partition_idx)
251{
252 return g_spm_partition_db.partitions[partition_idx].
253 memory_data.zi_start;
254}
255
256uint32_t tfm_spm_partition_get_zi_limit(uint32_t partition_idx)
257{
258 return g_spm_partition_db.partitions[partition_idx].
259 memory_data.zi_limit;
260}
261
262uint32_t tfm_spm_partition_get_rw_start(uint32_t partition_idx)
263{
264 return g_spm_partition_db.partitions[partition_idx].
265 memory_data.rw_start;
266}
267
268uint32_t tfm_spm_partition_get_rw_limit(uint32_t partition_idx)
269{
270 return g_spm_partition_db.partitions[partition_idx].
271 memory_data.rw_limit;
272}
273
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100274void tfm_spm_partition_set_stack(uint32_t partition_idx, uint32_t stack_ptr)
Miklos Balint386b8b52017-11-29 13:12:32 +0000275{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100276 g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100277 runtime_data.stack_ptr = stack_ptr;
Miklos Balint386b8b52017-11-29 13:12:32 +0000278}
279#endif
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100280
Miklos Balintace4c3f2018-07-30 12:31:15 +0200281void tfm_spm_partition_store_context(uint32_t partition_idx,
282 uint32_t stack_ptr, uint32_t lr)
283{
284 g_spm_partition_db.partitions[partition_idx].
285 runtime_data.stack_ptr = stack_ptr;
286 g_spm_partition_db.partitions[partition_idx].
287 runtime_data.lr = lr;
288}
289
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100290uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100291{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100292 return g_spm_partition_db.partitions[partition_idx].static_data.
293 partition_id;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100294}
295
Mate Toth-Pal59398712018-02-28 17:06:40 +0100296uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
297{
298 return g_spm_partition_db.partitions[partition_idx].static_data.
299 partition_flags;
300}
301
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100302const struct spm_partition_runtime_data_t *
Mate Toth-Pal59398712018-02-28 17:06:40 +0100303 tfm_spm_partition_get_runtime_data(uint32_t partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100304{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100305 return &(g_spm_partition_db.partitions[partition_idx].runtime_data);
306}
307
308void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state)
309{
310 g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state =
311 state;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100312 if (state == SPM_PARTITION_STATE_RUNNING) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100313 g_spm_partition_db.running_partition_idx = partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100314 }
315}
316
Miklos Balint6a139ae2018-04-04 19:44:37 +0200317void tfm_spm_partition_set_caller_partition_idx(uint32_t partition_idx,
318 uint32_t caller_partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100319{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100320 g_spm_partition_db.partitions[partition_idx].runtime_data.
321 caller_partition_idx = caller_partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100322}
323
Mate Toth-Pal21a74c92018-04-13 14:05:41 +0200324void tfm_spm_partition_set_caller_client_id(uint32_t partition_idx,
325 int32_t caller_client_id)
326{
327 g_spm_partition_db.partitions[partition_idx].runtime_data.
328 caller_client_id = caller_client_id;
329}
330
Miklos Balintdd02bb32019-05-26 21:13:12 +0200331#ifndef TFM_PSA_API
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100332enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100333 uint32_t share)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100334{
335 enum spm_err_t ret = SPM_ERR_OK;
336
337#if TFM_LVL != 1
338 /* Only need to set configuration on levels higher than 1 */
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200339 ret = tfm_spm_hal_set_share_region(share);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100340#endif
341
342 if (ret == SPM_ERR_OK) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100343 g_spm_partition_db.partitions[partition_idx].runtime_data.share = share;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100344 }
345 return ret;
346}
Miklos Balintdd02bb32019-05-26 21:13:12 +0200347#endif
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100348
Hugues de Valonf704c802019-02-19 14:51:41 +0000349enum spm_err_t tfm_spm_partition_set_iovec(uint32_t partition_idx,
350 const int32_t *args)
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200351{
352 struct spm_partition_runtime_data_t *runtime_data =
353 &g_spm_partition_db.partitions[partition_idx].runtime_data;
Hugues de Valonf704c802019-02-19 14:51:41 +0000354 size_t i;
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200355
Hugues de Valonf704c802019-02-19 14:51:41 +0000356 if ((args[1] < 0) || (args[3] < 0)) {
357 return SPM_ERR_INVALID_PARAMETER;
358 }
359
360 runtime_data->iovec_args.in_len = (size_t)args[1];
361 for (i = 0U; i < runtime_data->iovec_args.in_len; ++i) {
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200362 runtime_data->iovec_args.in_vec[i].base =
363 ((psa_invec *)args[0])[i].base;
364 runtime_data->iovec_args.in_vec[i].len = ((psa_invec *)args[0])[i].len;
365 }
Hugues de Valonf704c802019-02-19 14:51:41 +0000366 runtime_data->iovec_args.out_len = (size_t)args[3];
367 for (i = 0U; i < runtime_data->iovec_args.out_len; ++i) {
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200368 runtime_data->iovec_args.out_vec[i].base =
369 ((psa_outvec *)args[2])[i].base;
370 runtime_data->iovec_args.out_vec[i].len =
371 ((psa_outvec *)args[2])[i].len;
372 }
373 runtime_data->orig_outvec = (psa_outvec *)args[2];
Mate Toth-Pal2a6f8c22018-12-13 16:37:17 +0100374 runtime_data->iovec_api = 1;
Hugues de Valonf704c802019-02-19 14:51:41 +0000375
376 return SPM_ERR_OK;
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200377}
378
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100379uint32_t tfm_spm_partition_get_running_partition_idx(void)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100380{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100381 return g_spm_partition_db.running_partition_idx;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100382}
383
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100384void tfm_spm_partition_cleanup_context(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100385{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200386 struct spm_partition_desc_t *partition =
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100387 &(g_spm_partition_db.partitions[partition_idx]);
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200388 int32_t i;
389
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100390 partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100391 partition->runtime_data.share = 0;
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200392 partition->runtime_data.iovec_args.in_len = 0;
393 for (i = 0; i < PSA_MAX_IOVEC; ++i) {
394 partition->runtime_data.iovec_args.in_vec[i].base = 0;
395 partition->runtime_data.iovec_args.in_vec[i].len = 0;
396 }
397 partition->runtime_data.iovec_args.out_len = 0;
398 for (i = 0; i < PSA_MAX_IOVEC; ++i) {
399 partition->runtime_data.iovec_args.out_vec[i].base = 0;
400 partition->runtime_data.iovec_args.out_vec[i].len = 0;
401 }
402 partition->runtime_data.orig_outvec = 0;
Mate Toth-Pal2a6f8c22018-12-13 16:37:17 +0100403 partition->runtime_data.iovec_api = 0;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100404}
Edison Aib5571352019-03-22 10:49:52 +0800405
406__attribute__((section("SFN")))
407void tfm_spm_partition_change_privilege(uint32_t privileged)
408{
409 CONTROL_Type ctrl;
410
411 ctrl.w = __get_CONTROL();
412
413 if (privileged == TFM_PARTITION_PRIVILEGED_MODE) {
414 ctrl.b.nPRIV = 0;
415 } else {
416 ctrl.b.nPRIV = 1;
417 }
418
419 __set_CONTROL(ctrl.w);
420}