Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1 | /* |
Mate Toth-Pal | 5e6d034 | 2019-11-22 11:43:20 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020, Arm Limited. All rights reserved. |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | /* All the APIs defined in this file are used for library model. */ |
| 9 | |
| 10 | #include <inttypes.h> |
| 11 | #include <limits.h> |
| 12 | #include <stdbool.h> |
| 13 | #include <stdlib.h> |
| 14 | #include "tfm_utils.h" |
| 15 | #include "tfm_spm_hal.h" |
| 16 | #include "spm_api.h" |
| 17 | #include "spm_db.h" |
| 18 | #include "region_defs.h" |
| 19 | #include "tfm_nspm.h" |
| 20 | #include "tfm_memory_utils.h" |
| 21 | #include "tfm_internal.h" |
| 22 | |
| 23 | extern struct spm_partition_db_t g_spm_partition_db; |
| 24 | |
| 25 | typedef enum { |
| 26 | TFM_INIT_FAILURE, |
| 27 | } sp_error_type_t; |
| 28 | |
| 29 | /* |
| 30 | * This function is called when a secure partition causes an error. |
| 31 | * In case of an error in the error handling, a non-zero value have to be |
| 32 | * returned. |
| 33 | */ |
| 34 | static void tfm_spm_partition_err_handler( |
| 35 | const struct spm_partition_desc_t *partition, |
| 36 | sp_error_type_t err_type, |
| 37 | int32_t err_code) |
| 38 | { |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 39 | (void)err_type; |
| 40 | (void)err_code; |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 41 | |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 42 | tfm_spm_partition_set_state(partition->static_data->partition_id, |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 43 | SPM_PARTITION_STATE_CLOSED); |
| 44 | } |
| 45 | |
| 46 | enum spm_err_t tfm_spm_partition_init(void) |
| 47 | { |
| 48 | struct spm_partition_desc_t *part; |
| 49 | struct tfm_sfn_req_s desc; |
| 50 | int32_t args[4] = {0}; |
| 51 | int32_t fail_cnt = 0; |
| 52 | uint32_t idx; |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 53 | const struct tfm_spm_partition_platform_data_t **platform_data_p; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 54 | |
| 55 | /* Call the init function for each partition */ |
| 56 | for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) { |
| 57 | part = &g_spm_partition_db.partitions[idx]; |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 58 | platform_data_p = part->platform_data_list; |
| 59 | if (platform_data_p != NULL) { |
| 60 | while ((*platform_data_p) != NULL) { |
Edison Ai | 6be3df1 | 2020-02-14 22:14:33 +0800 | [diff] [blame] | 61 | if (tfm_spm_hal_configure_default_isolation(idx, |
| 62 | *platform_data_p) != TFM_PLAT_ERR_SUCCESS) { |
| 63 | fail_cnt++; |
| 64 | } |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 65 | ++platform_data_p; |
| 66 | } |
| 67 | } |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 68 | if (part->static_data->partition_init == NULL) { |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 69 | tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE); |
| 70 | tfm_spm_partition_set_caller_partition_idx(idx, |
| 71 | SPM_INVALID_PARTITION_IDX); |
| 72 | } else { |
| 73 | int32_t res; |
| 74 | |
| 75 | desc.args = args; |
Summer Qin | 43c185d | 2019-10-10 15:44:42 +0800 | [diff] [blame] | 76 | desc.ns_caller = false; |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 77 | desc.sfn = (sfn_t)part->static_data->partition_init; |
| 78 | desc.sp_id = part->static_data->partition_id; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 79 | res = tfm_core_sfn_request(&desc); |
| 80 | if (res == TFM_SUCCESS) { |
| 81 | tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE); |
| 82 | } else { |
| 83 | tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, res); |
| 84 | fail_cnt++; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | tfm_secure_api_init_done(); |
| 90 | |
| 91 | if (fail_cnt == 0) { |
| 92 | return SPM_ERR_OK; |
| 93 | } else { |
| 94 | return SPM_ERR_PARTITION_NOT_AVAILABLE; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void tfm_spm_partition_push_interrupted_ctx(uint32_t partition_idx) |
| 99 | { |
| 100 | struct spm_partition_runtime_data_t *runtime_data = |
| 101 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 102 | struct interrupted_ctx_stack_frame_t *stack_frame = |
Edison Ai | 7aff9e8 | 2019-07-11 14:56:46 +0800 | [diff] [blame] | 103 | (struct interrupted_ctx_stack_frame_t *)runtime_data->ctx_stack_ptr; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 104 | |
| 105 | stack_frame->partition_state = runtime_data->partition_state; |
Matt | 463ed58 | 2019-12-20 12:31:25 +0800 | [diff] [blame] | 106 | |
| 107 | runtime_data->ctx_stack_ptr += |
| 108 | sizeof(struct interrupted_ctx_stack_frame_t) / sizeof(uint32_t); |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void tfm_spm_partition_pop_interrupted_ctx(uint32_t partition_idx) |
| 112 | { |
| 113 | struct spm_partition_runtime_data_t *runtime_data = |
| 114 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 115 | struct interrupted_ctx_stack_frame_t *stack_frame; |
| 116 | |
Matt | 463ed58 | 2019-12-20 12:31:25 +0800 | [diff] [blame] | 117 | runtime_data->ctx_stack_ptr -= |
| 118 | sizeof(struct interrupted_ctx_stack_frame_t) / sizeof(uint32_t); |
| 119 | |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 120 | stack_frame = (struct interrupted_ctx_stack_frame_t *) |
| 121 | runtime_data->ctx_stack_ptr; |
| 122 | tfm_spm_partition_set_state(partition_idx, stack_frame->partition_state); |
| 123 | stack_frame->partition_state = 0; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void tfm_spm_partition_push_handler_ctx(uint32_t partition_idx) |
| 127 | { |
| 128 | struct spm_partition_runtime_data_t *runtime_data = |
| 129 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 130 | struct handler_ctx_stack_frame_t *stack_frame = |
| 131 | (struct handler_ctx_stack_frame_t *) |
| 132 | runtime_data->ctx_stack_ptr; |
| 133 | |
| 134 | stack_frame->partition_state = runtime_data->partition_state; |
| 135 | stack_frame->caller_partition_idx = runtime_data->caller_partition_idx; |
| 136 | |
| 137 | runtime_data->ctx_stack_ptr += |
| 138 | sizeof(struct handler_ctx_stack_frame_t) / sizeof(uint32_t); |
| 139 | } |
| 140 | |
| 141 | void tfm_spm_partition_pop_handler_ctx(uint32_t partition_idx) |
| 142 | { |
| 143 | struct spm_partition_runtime_data_t *runtime_data = |
| 144 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 145 | struct handler_ctx_stack_frame_t *stack_frame; |
| 146 | |
| 147 | runtime_data->ctx_stack_ptr -= |
| 148 | sizeof(struct handler_ctx_stack_frame_t) / sizeof(uint32_t); |
| 149 | |
| 150 | stack_frame = (struct handler_ctx_stack_frame_t *) |
| 151 | runtime_data->ctx_stack_ptr; |
| 152 | |
| 153 | tfm_spm_partition_set_state(partition_idx, stack_frame->partition_state); |
| 154 | stack_frame->partition_state = 0; |
| 155 | tfm_spm_partition_set_caller_partition_idx( |
| 156 | partition_idx, stack_frame->caller_partition_idx); |
| 157 | stack_frame->caller_partition_idx = 0; |
| 158 | } |
| 159 | |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 160 | void tfm_spm_partition_store_context(uint32_t partition_idx, |
| 161 | uint32_t stack_ptr, uint32_t lr) |
| 162 | { |
| 163 | g_spm_partition_db.partitions[partition_idx]. |
| 164 | runtime_data.stack_ptr = stack_ptr; |
| 165 | g_spm_partition_db.partitions[partition_idx]. |
| 166 | runtime_data.lr = lr; |
| 167 | } |
| 168 | |
| 169 | const struct spm_partition_runtime_data_t * |
| 170 | tfm_spm_partition_get_runtime_data(uint32_t partition_idx) |
| 171 | { |
| 172 | return &(g_spm_partition_db.partitions[partition_idx].runtime_data); |
| 173 | } |
| 174 | |
| 175 | void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state) |
| 176 | { |
| 177 | g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state = |
| 178 | state; |
| 179 | if (state == SPM_PARTITION_STATE_RUNNING || |
| 180 | state == SPM_PARTITION_STATE_HANDLING_IRQ) { |
| 181 | g_spm_partition_db.running_partition_idx = partition_idx; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | void tfm_spm_partition_set_caller_partition_idx(uint32_t partition_idx, |
| 186 | uint32_t caller_partition_idx) |
| 187 | { |
| 188 | g_spm_partition_db.partitions[partition_idx].runtime_data. |
| 189 | caller_partition_idx = caller_partition_idx; |
| 190 | } |
| 191 | |
| 192 | void tfm_spm_partition_set_signal_mask(uint32_t partition_idx, |
| 193 | uint32_t signal_mask) |
| 194 | { |
| 195 | g_spm_partition_db.partitions[partition_idx].runtime_data. |
| 196 | signal_mask = signal_mask; |
| 197 | } |
| 198 | |
| 199 | void tfm_spm_partition_set_caller_client_id(uint32_t partition_idx, |
| 200 | int32_t caller_client_id) |
| 201 | { |
| 202 | g_spm_partition_db.partitions[partition_idx].runtime_data. |
| 203 | caller_client_id = caller_client_id; |
| 204 | } |
| 205 | |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 206 | enum spm_err_t tfm_spm_partition_set_iovec(uint32_t partition_idx, |
| 207 | const int32_t *args) |
| 208 | { |
| 209 | struct spm_partition_runtime_data_t *runtime_data = |
| 210 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 211 | size_t i; |
| 212 | |
| 213 | if ((args[1] < 0) || (args[3] < 0)) { |
| 214 | return SPM_ERR_INVALID_PARAMETER; |
| 215 | } |
| 216 | |
| 217 | runtime_data->iovec_args.in_len = (size_t)args[1]; |
| 218 | for (i = 0U; i < runtime_data->iovec_args.in_len; ++i) { |
| 219 | runtime_data->iovec_args.in_vec[i].base = |
| 220 | ((psa_invec *)args[0])[i].base; |
| 221 | runtime_data->iovec_args.in_vec[i].len = ((psa_invec *)args[0])[i].len; |
| 222 | } |
| 223 | runtime_data->iovec_args.out_len = (size_t)args[3]; |
| 224 | for (i = 0U; i < runtime_data->iovec_args.out_len; ++i) { |
| 225 | runtime_data->iovec_args.out_vec[i].base = |
| 226 | ((psa_outvec *)args[2])[i].base; |
| 227 | runtime_data->iovec_args.out_vec[i].len = |
| 228 | ((psa_outvec *)args[2])[i].len; |
| 229 | } |
| 230 | runtime_data->orig_outvec = (psa_outvec *)args[2]; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 231 | |
| 232 | return SPM_ERR_OK; |
| 233 | } |
| 234 | |
| 235 | uint32_t tfm_spm_partition_get_running_partition_idx(void) |
| 236 | { |
| 237 | return g_spm_partition_db.running_partition_idx; |
| 238 | } |
| 239 | |
| 240 | void tfm_spm_partition_cleanup_context(uint32_t partition_idx) |
| 241 | { |
| 242 | struct spm_partition_desc_t *partition = |
| 243 | &(g_spm_partition_db.partitions[partition_idx]); |
| 244 | int32_t i; |
| 245 | |
| 246 | partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 247 | partition->runtime_data.iovec_args.in_len = 0; |
| 248 | for (i = 0; i < PSA_MAX_IOVEC; ++i) { |
| 249 | partition->runtime_data.iovec_args.in_vec[i].base = 0; |
| 250 | partition->runtime_data.iovec_args.in_vec[i].len = 0; |
| 251 | } |
| 252 | partition->runtime_data.iovec_args.out_len = 0; |
| 253 | for (i = 0; i < PSA_MAX_IOVEC; ++i) { |
| 254 | partition->runtime_data.iovec_args.out_vec[i].base = 0; |
| 255 | partition->runtime_data.iovec_args.out_vec[i].len = 0; |
| 256 | } |
| 257 | partition->runtime_data.orig_outvec = 0; |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 258 | } |