blob: 7b96b116a1567085ceb9a49447ff5e40c55cbc67 [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"
Miklos Balint386b8b52017-11-29 13:12:32 +000017#include "secure_fw/core/tfm_core.h"
Mate Toth-Pale1475332018-04-09 17:28:49 +020018#include "platform_retarget.h"
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020019#include "tfm_peripherals_def.h"
Mate Toth-Pale1475332018-04-09 17:28:49 +020020#include "spm_partition_defs.h"
21
Miklos Balint386b8b52017-11-29 13:12:32 +000022
Mate Toth-Pal349714a2018-02-23 15:30:24 +010023struct spm_partition_db_t g_spm_partition_db = {0,};
Miklos Balint386b8b52017-11-29 13:12:32 +000024
Miklos Balint386b8b52017-11-29 13:12:32 +000025typedef enum {
26 TFM_INIT_FAILURE,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010027} sp_error_type_t;
Miklos Balint386b8b52017-11-29 13:12:32 +000028
29/*
Mate Toth-Pal349714a2018-02-23 15:30:24 +010030 * This function is called when a secure partition causes an error.
Mate Toth-Pal65291f32018-02-23 14:35:22 +010031 * In case of an error in the error handling, a non-zero value have to be
32 * returned.
Miklos Balint386b8b52017-11-29 13:12:32 +000033 */
Mate Toth-Pal349714a2018-02-23 15:30:24 +010034static void tfm_spm_partition_err_handler(
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020035 struct spm_partition_desc_t *partition,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010036 sp_error_type_t err_type,
Mate Toth-Pal65291f32018-02-23 14:35:22 +010037 int32_t err_code)
Miklos Balint386b8b52017-11-29 13:12:32 +000038{
Miklos Balint386b8b52017-11-29 13:12:32 +000039#ifdef TFM_CORE_DEBUG
40 if (err_type == TFM_INIT_FAILURE) {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010041 printf("Partition init failed for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010042 partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000043 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010044 printf("Unknown partition error %d for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010045 err_type, partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000046 }
47#endif
Mate Toth-Pal18b83922018-02-26 17:58:18 +010048 tfm_spm_partition_set_state(partition->static_data.partition_id,
Mate Toth-Pal349714a2018-02-23 15:30:24 +010049 SPM_PARTITION_STATE_CLOSED);
Miklos Balint386b8b52017-11-29 13:12:32 +000050}
51
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010052uint32_t get_partition_idx(uint32_t partition_id)
53{
54 int i;
55
56 if (partition_id == INVALID_PARTITION_ID) {
57 return SPM_INVALID_PARTITION_IDX;
58 }
59
60 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
61 if (g_spm_partition_db.partitions[i].static_data.partition_id ==
62 partition_id) {
63 return i;
64 }
65 }
66 return SPM_INVALID_PARTITION_IDX;
67}
68
Miklos Balint12735bc2018-08-01 15:45:18 +020069/* FixMe: this should be in a header */
70extern void tfm_nspm_configure_clients(void);
71
Miklos Balint386b8b52017-11-29 13:12:32 +000072enum spm_err_t tfm_spm_db_init(void)
73{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020074 struct spm_partition_desc_t *part_ptr;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010075
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +010076 memset (&g_spm_partition_db, 0, sizeof(g_spm_partition_db));
77
Mate Toth-Pal349714a2018-02-23 15:30:24 +010078 /* This function initialises partition db */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010079 g_spm_partition_db.running_partition_idx = SPM_INVALID_PARTITION_IDX;
80 g_spm_partition_db.partition_count = 0;
Miklos Balint386b8b52017-11-29 13:12:32 +000081
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010082 /* There are a few partitions that are used by TF-M internally.
83 * These are explicitly added to the partition db here.
84 */
85
86 /* For the non secure Execution environment */
Miklos Balint6a139ae2018-04-04 19:44:37 +020087#if TFM_LVL != 1
88 extern uint32_t Stack_Mem[];
89 extern uint32_t Stack_top[];
90#endif
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010091 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
92 return SPM_ERR_INVALID_CONFIG;
93 }
94 part_ptr = &(g_spm_partition_db.partitions[
95 g_spm_partition_db.partition_count]);
96 part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +010097 part_ptr->static_data.partition_flags = 0;
Miklos Balint6a139ae2018-04-04 19:44:37 +020098
99#if TFM_LVL != 1
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200100 part_ptr->memory_data.stack_bottom = (uint32_t)Stack_Mem;
101 part_ptr->memory_data.stack_top = (uint32_t)Stack_top;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200102 /* Since RW, ZI and stack are configured as one MPU region, configure
103 * RW start address to Stack_Mem to get RW access to stack
104 */
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200105 part_ptr->memory_data.rw_start = (uint32_t)Stack_Mem;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200106#endif
107
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100108 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
Miklos Balint12735bc2018-08-01 15:45:18 +0200109 tfm_nspm_configure_clients();
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100110 ++g_spm_partition_db.partition_count;
111
112 /* For the TF-M core environment itself */
113 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
114 return SPM_ERR_INVALID_CONFIG;
115 }
116 part_ptr = &(g_spm_partition_db.partitions[
117 g_spm_partition_db.partition_count]);
118 part_ptr->static_data.partition_id = TFM_SP_CORE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +0100119 part_ptr->static_data.partition_flags =
120 SPM_PART_FLAG_SECURE | SPM_PART_FLAG_TRUSTED;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100121 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
122 ++g_spm_partition_db.partition_count;
123
124 /* Add user-defined secure partitions */
Miklos Balintd306ab12018-05-18 16:58:18 +0200125 #include "secure_fw/services/tfm_partition_list.inc"
Miklos Balint386b8b52017-11-29 13:12:32 +0000126
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +0100127 g_spm_partition_db.is_init = 1;
128
Miklos Balint386b8b52017-11-29 13:12:32 +0000129 return SPM_ERR_OK;
130}
131
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100132enum spm_err_t tfm_spm_partition_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +0000133{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200134 struct spm_partition_desc_t *part;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200135 struct tfm_sfn_req_s desc;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200136 int32_t args[4] = {0};
Miklos Balint386b8b52017-11-29 13:12:32 +0000137 int32_t fail_cnt = 0;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100138 uint32_t idx;
Miklos Balint386b8b52017-11-29 13:12:32 +0000139
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100140 /* Call the init function for each partition */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100141 for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) {
142 part = &g_spm_partition_db.partitions[idx];
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200143 tfm_spm_hal_configure_default_isolation(part->platform_data);
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100144 if (part->static_data.partition_init == NULL) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100145 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Miklos Balint6a139ae2018-04-04 19:44:37 +0200146 tfm_spm_partition_set_caller_partition_idx(idx,
147 SPM_INVALID_PARTITION_IDX);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100148 } else {
Miklos Balintace4c3f2018-07-30 12:31:15 +0200149 int32_t res;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200150
151 desc.args = args;
152 desc.exc_num = EXC_NUM_THREAD_MODE;
153 desc.ns_caller = 0;
154 desc.sfn = (sfn_t)part->static_data.partition_init;
155 desc.sp_id = part->static_data.partition_id;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200156 res = tfm_core_sfn_request(&desc);
157 if (res == TFM_SUCCESS) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100158 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100159 } else {
Miklos Balintace4c3f2018-07-30 12:31:15 +0200160 tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, res);
Miklos Balint386b8b52017-11-29 13:12:32 +0000161 fail_cnt++;
162 }
163 }
164 }
165
Miklos Balint6a139ae2018-04-04 19:44:37 +0200166 tfm_secure_api_init_done();
167
Miklos Balint386b8b52017-11-29 13:12:32 +0000168 if (fail_cnt == 0) {
169 return SPM_ERR_OK;
170 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100171 return SPM_ERR_PARTITION_NOT_AVAILABLE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000172 }
173}
174
175#if TFM_LVL != 1
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100176enum spm_err_t tfm_spm_partition_sandbox_config(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000177{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200178 struct spm_partition_desc_t *part;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100179 if (!g_spm_partition_db.is_init) {
180 return SPM_ERR_PARTITION_DB_NOT_INIT;
Miklos Balint386b8b52017-11-29 13:12:32 +0000181 }
182
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100183 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000184
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200185 return tfm_spm_hal_partition_sandbox_config(&(part->memory_data),
186 part->platform_data);
Miklos Balint386b8b52017-11-29 13:12:32 +0000187
Miklos Balint386b8b52017-11-29 13:12:32 +0000188}
189
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100190enum spm_err_t tfm_spm_partition_sandbox_deconfig(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000191{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100192 /* This function takes a partition id and disables the
193 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000194 */
195
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200196 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000197
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100198 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000199
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200200 return tfm_spm_hal_partition_sandbox_deconfig(&(part->memory_data),
201 part->platform_data);
Miklos Balint386b8b52017-11-29 13:12:32 +0000202}
203
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100204uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000205{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100206 return g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200207 memory_data.stack_bottom;
Miklos Balint386b8b52017-11-29 13:12:32 +0000208}
209
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100210uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000211{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200212 return g_spm_partition_db.partitions[partition_idx].memory_data.stack_top;
Miklos Balint386b8b52017-11-29 13:12:32 +0000213}
214
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100215void tfm_spm_partition_set_stack(uint32_t partition_idx, uint32_t stack_ptr)
Miklos Balint386b8b52017-11-29 13:12:32 +0000216{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100217 g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100218 runtime_data.stack_ptr = stack_ptr;
Miklos Balint386b8b52017-11-29 13:12:32 +0000219}
220#endif
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100221
Miklos Balintace4c3f2018-07-30 12:31:15 +0200222void tfm_spm_partition_store_context(uint32_t partition_idx,
223 uint32_t stack_ptr, uint32_t lr)
224{
225 g_spm_partition_db.partitions[partition_idx].
226 runtime_data.stack_ptr = stack_ptr;
227 g_spm_partition_db.partitions[partition_idx].
228 runtime_data.lr = lr;
229}
230
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100231uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100232{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100233 return g_spm_partition_db.partitions[partition_idx].static_data.
234 partition_id;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100235}
236
Mate Toth-Pal59398712018-02-28 17:06:40 +0100237uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
238{
239 return g_spm_partition_db.partitions[partition_idx].static_data.
240 partition_flags;
241}
242
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100243const struct spm_partition_runtime_data_t *
Mate Toth-Pal59398712018-02-28 17:06:40 +0100244 tfm_spm_partition_get_runtime_data(uint32_t partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100245{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100246 return &(g_spm_partition_db.partitions[partition_idx].runtime_data);
247}
248
249void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state)
250{
251 g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state =
252 state;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100253 if (state == SPM_PARTITION_STATE_RUNNING) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100254 g_spm_partition_db.running_partition_idx = partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100255 }
256}
257
Miklos Balint6a139ae2018-04-04 19:44:37 +0200258void tfm_spm_partition_set_caller_partition_idx(uint32_t partition_idx,
259 uint32_t caller_partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100260{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100261 g_spm_partition_db.partitions[partition_idx].runtime_data.
262 caller_partition_idx = caller_partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100263}
264
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100265enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100266 uint32_t share)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100267{
268 enum spm_err_t ret = SPM_ERR_OK;
269
270#if TFM_LVL != 1
271 /* Only need to set configuration on levels higher than 1 */
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200272 ret = tfm_spm_hal_set_share_region(share);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100273#endif
274
275 if (ret == SPM_ERR_OK) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100276 g_spm_partition_db.partitions[partition_idx].runtime_data.share = share;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100277 }
278 return ret;
279}
280
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100281uint32_t tfm_spm_partition_get_running_partition_idx(void)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100282{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100283 return g_spm_partition_db.running_partition_idx;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100284}
285
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100286void tfm_spm_partition_cleanup_context(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100287{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200288 struct spm_partition_desc_t *partition =
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100289 &(g_spm_partition_db.partitions[partition_idx]);
290 partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100291 partition->runtime_data.share = 0;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100292}