blob: 77bf5be2bd649b4b81c6f9e002e44fe45a3d273d [file] [log] [blame]
Miklos Balint386b8b52017-11-29 13:12:32 +00001/*
Mate Toth-Pal65291f32018-02-23 14:35:22 +01002 * Copyright (c) 2017-2018, 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"
14#include "spm_db_setup.h"
Miklos Balint6a139ae2018-04-04 19:44:37 +020015#include "tfm_internal.h"
Mate Toth-Pal65291f32018-02-23 14:35:22 +010016#include "tfm_api.h"
Mate Toth-Palce61afa2018-08-03 13:51:01 +020017#include "tfm_nspm.h"
Miklos Balint386b8b52017-11-29 13:12:32 +000018#include "secure_fw/core/tfm_core.h"
Mate Toth-Pale1475332018-04-09 17:28:49 +020019#include "platform_retarget.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(
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020036 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 {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010045 printf("Unknown partition error %d for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010046 err_type, partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000047 }
48#endif
Mate Toth-Pal18b83922018-02-26 17:58:18 +010049 tfm_spm_partition_set_state(partition->static_data.partition_id,
Mate Toth-Pal349714a2018-02-23 15:30:24 +010050 SPM_PARTITION_STATE_CLOSED);
Miklos Balint386b8b52017-11-29 13:12:32 +000051}
52
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010053uint32_t get_partition_idx(uint32_t partition_id)
54{
55 int i;
56
57 if (partition_id == INVALID_PARTITION_ID) {
58 return SPM_INVALID_PARTITION_IDX;
59 }
60
61 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
62 if (g_spm_partition_db.partitions[i].static_data.partition_id ==
63 partition_id) {
64 return i;
65 }
66 }
67 return SPM_INVALID_PARTITION_IDX;
68}
69
Miklos Balint386b8b52017-11-29 13:12:32 +000070enum spm_err_t tfm_spm_db_init(void)
71{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020072 struct spm_partition_desc_t *part_ptr;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010073
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +010074 memset (&g_spm_partition_db, 0, sizeof(g_spm_partition_db));
75
Mate Toth-Pal349714a2018-02-23 15:30:24 +010076 /* This function initialises partition db */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010077 g_spm_partition_db.running_partition_idx = SPM_INVALID_PARTITION_IDX;
78 g_spm_partition_db.partition_count = 0;
Miklos Balint386b8b52017-11-29 13:12:32 +000079
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010080 /* There are a few partitions that are used by TF-M internally.
81 * These are explicitly added to the partition db here.
82 */
83
84 /* For the non secure Execution environment */
Miklos Balint6a139ae2018-04-04 19:44:37 +020085#if TFM_LVL != 1
86 extern uint32_t Stack_Mem[];
87 extern uint32_t Stack_top[];
88#endif
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010089 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
90 return SPM_ERR_INVALID_CONFIG;
91 }
92 part_ptr = &(g_spm_partition_db.partitions[
93 g_spm_partition_db.partition_count]);
94 part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +010095 part_ptr->static_data.partition_flags = 0;
Miklos Balint6a139ae2018-04-04 19:44:37 +020096
97#if TFM_LVL != 1
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020098 part_ptr->memory_data.stack_bottom = (uint32_t)Stack_Mem;
99 part_ptr->memory_data.stack_top = (uint32_t)Stack_top;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200100 /* Since RW, ZI and stack are configured as one MPU region, configure
101 * RW start address to Stack_Mem to get RW access to stack
102 */
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200103 part_ptr->memory_data.rw_start = (uint32_t)Stack_Mem;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200104#endif
105
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100106 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
Miklos Balint12735bc2018-08-01 15:45:18 +0200107 tfm_nspm_configure_clients();
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100108 ++g_spm_partition_db.partition_count;
109
110 /* For the TF-M core environment itself */
111 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
112 return SPM_ERR_INVALID_CONFIG;
113 }
114 part_ptr = &(g_spm_partition_db.partitions[
115 g_spm_partition_db.partition_count]);
116 part_ptr->static_data.partition_id = TFM_SP_CORE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +0100117 part_ptr->static_data.partition_flags =
118 SPM_PART_FLAG_SECURE | SPM_PART_FLAG_TRUSTED;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100119 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
120 ++g_spm_partition_db.partition_count;
121
122 /* Add user-defined secure partitions */
Miklos Balintd306ab12018-05-18 16:58:18 +0200123 #include "secure_fw/services/tfm_partition_list.inc"
Miklos Balint386b8b52017-11-29 13:12:32 +0000124
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +0100125 g_spm_partition_db.is_init = 1;
126
Miklos Balint386b8b52017-11-29 13:12:32 +0000127 return SPM_ERR_OK;
128}
129
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100130enum spm_err_t tfm_spm_partition_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +0000131{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200132 struct spm_partition_desc_t *part;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200133 struct tfm_sfn_req_s desc;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200134 int32_t args[4] = {0};
Miklos Balint386b8b52017-11-29 13:12:32 +0000135 int32_t fail_cnt = 0;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100136 uint32_t idx;
Miklos Balint386b8b52017-11-29 13:12:32 +0000137
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100138 /* Call the init function for each partition */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100139 for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) {
140 part = &g_spm_partition_db.partitions[idx];
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200141 tfm_spm_hal_configure_default_isolation(part->platform_data);
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100142 if (part->static_data.partition_init == NULL) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100143 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Miklos Balint6a139ae2018-04-04 19:44:37 +0200144 tfm_spm_partition_set_caller_partition_idx(idx,
145 SPM_INVALID_PARTITION_IDX);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100146 } else {
Miklos Balintace4c3f2018-07-30 12:31:15 +0200147 int32_t res;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200148
149 desc.args = args;
150 desc.exc_num = EXC_NUM_THREAD_MODE;
151 desc.ns_caller = 0;
152 desc.sfn = (sfn_t)part->static_data.partition_init;
153 desc.sp_id = part->static_data.partition_id;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200154 res = tfm_core_sfn_request(&desc);
155 if (res == TFM_SUCCESS) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100156 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100157 } else {
Miklos Balintace4c3f2018-07-30 12:31:15 +0200158 tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, res);
Miklos Balint386b8b52017-11-29 13:12:32 +0000159 fail_cnt++;
160 }
161 }
162 }
163
Miklos Balint6a139ae2018-04-04 19:44:37 +0200164 tfm_secure_api_init_done();
165
Miklos Balint386b8b52017-11-29 13:12:32 +0000166 if (fail_cnt == 0) {
167 return SPM_ERR_OK;
168 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100169 return SPM_ERR_PARTITION_NOT_AVAILABLE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000170 }
171}
172
173#if TFM_LVL != 1
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100174enum spm_err_t tfm_spm_partition_sandbox_config(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000175{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200176 struct spm_partition_desc_t *part;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100177 if (!g_spm_partition_db.is_init) {
178 return SPM_ERR_PARTITION_DB_NOT_INIT;
Miklos Balint386b8b52017-11-29 13:12:32 +0000179 }
180
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100181 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000182
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200183 return tfm_spm_hal_partition_sandbox_config(&(part->memory_data),
184 part->platform_data);
Miklos Balint386b8b52017-11-29 13:12:32 +0000185
Miklos Balint386b8b52017-11-29 13:12:32 +0000186}
187
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100188enum spm_err_t tfm_spm_partition_sandbox_deconfig(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000189{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100190 /* This function takes a partition id and disables the
191 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000192 */
193
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200194 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000195
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100196 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000197
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200198 return tfm_spm_hal_partition_sandbox_deconfig(&(part->memory_data),
199 part->platform_data);
Miklos Balint386b8b52017-11-29 13:12:32 +0000200}
201
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100202uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000203{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100204 return g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200205 memory_data.stack_bottom;
Miklos Balint386b8b52017-11-29 13:12:32 +0000206}
207
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100208uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000209{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200210 return g_spm_partition_db.partitions[partition_idx].memory_data.stack_top;
Miklos Balint386b8b52017-11-29 13:12:32 +0000211}
212
Mate Toth-Pal21a74c92018-04-13 14:05:41 +0200213uint32_t tfm_spm_partition_get_zi_start(uint32_t partition_idx)
214{
215 return g_spm_partition_db.partitions[partition_idx].
216 memory_data.zi_start;
217}
218
219uint32_t tfm_spm_partition_get_zi_limit(uint32_t partition_idx)
220{
221 return g_spm_partition_db.partitions[partition_idx].
222 memory_data.zi_limit;
223}
224
225uint32_t tfm_spm_partition_get_rw_start(uint32_t partition_idx)
226{
227 return g_spm_partition_db.partitions[partition_idx].
228 memory_data.rw_start;
229}
230
231uint32_t tfm_spm_partition_get_rw_limit(uint32_t partition_idx)
232{
233 return g_spm_partition_db.partitions[partition_idx].
234 memory_data.rw_limit;
235}
236
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100237void tfm_spm_partition_set_stack(uint32_t partition_idx, uint32_t stack_ptr)
Miklos Balint386b8b52017-11-29 13:12:32 +0000238{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100239 g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100240 runtime_data.stack_ptr = stack_ptr;
Miklos Balint386b8b52017-11-29 13:12:32 +0000241}
242#endif
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100243
Miklos Balintace4c3f2018-07-30 12:31:15 +0200244void tfm_spm_partition_store_context(uint32_t partition_idx,
245 uint32_t stack_ptr, uint32_t lr)
246{
247 g_spm_partition_db.partitions[partition_idx].
248 runtime_data.stack_ptr = stack_ptr;
249 g_spm_partition_db.partitions[partition_idx].
250 runtime_data.lr = lr;
251}
252
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100253uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100254{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100255 return g_spm_partition_db.partitions[partition_idx].static_data.
256 partition_id;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100257}
258
Mate Toth-Pal59398712018-02-28 17:06:40 +0100259uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
260{
261 return g_spm_partition_db.partitions[partition_idx].static_data.
262 partition_flags;
263}
264
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100265const struct spm_partition_runtime_data_t *
Mate Toth-Pal59398712018-02-28 17:06:40 +0100266 tfm_spm_partition_get_runtime_data(uint32_t partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100267{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100268 return &(g_spm_partition_db.partitions[partition_idx].runtime_data);
269}
270
271void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state)
272{
273 g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state =
274 state;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100275 if (state == SPM_PARTITION_STATE_RUNNING) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100276 g_spm_partition_db.running_partition_idx = partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100277 }
278}
279
Miklos Balint6a139ae2018-04-04 19:44:37 +0200280void tfm_spm_partition_set_caller_partition_idx(uint32_t partition_idx,
281 uint32_t caller_partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100282{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100283 g_spm_partition_db.partitions[partition_idx].runtime_data.
284 caller_partition_idx = caller_partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100285}
286
Mate Toth-Pal21a74c92018-04-13 14:05:41 +0200287void tfm_spm_partition_set_caller_client_id(uint32_t partition_idx,
288 int32_t caller_client_id)
289{
290 g_spm_partition_db.partitions[partition_idx].runtime_data.
291 caller_client_id = caller_client_id;
292}
293
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100294enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100295 uint32_t share)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100296{
297 enum spm_err_t ret = SPM_ERR_OK;
298
299#if TFM_LVL != 1
300 /* Only need to set configuration on levels higher than 1 */
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200301 ret = tfm_spm_hal_set_share_region(share);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100302#endif
303
304 if (ret == SPM_ERR_OK) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100305 g_spm_partition_db.partitions[partition_idx].runtime_data.share = share;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100306 }
307 return ret;
308}
309
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100310uint32_t tfm_spm_partition_get_running_partition_idx(void)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100311{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100312 return g_spm_partition_db.running_partition_idx;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100313}
314
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100315void tfm_spm_partition_cleanup_context(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100316{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200317 struct spm_partition_desc_t *partition =
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100318 &(g_spm_partition_db.partitions[partition_idx]);
319 partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100320 partition->runtime_data.share = 0;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100321}