Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018-2019, Arm Limited. All rights reserved. |
| 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 | { |
| 39 | #ifdef TFM_CORE_DEBUG |
| 40 | if (err_type == TFM_INIT_FAILURE) { |
| 41 | printf("Partition init failed for partition id 0x%08X\r\n", |
| 42 | partition->static_data.partition_id); |
| 43 | } else { |
| 44 | printf( |
| 45 | "Unknown partition error %d (code: %d) for partition id 0x%08X\r\n", |
| 46 | err_type, err_code, partition->static_data.partition_id); |
| 47 | } |
| 48 | #else |
| 49 | (void)err_type; |
| 50 | (void)err_code; |
| 51 | #endif |
| 52 | tfm_spm_partition_set_state(partition->static_data.partition_id, |
| 53 | SPM_PARTITION_STATE_CLOSED); |
| 54 | } |
| 55 | |
| 56 | enum spm_err_t tfm_spm_partition_init(void) |
| 57 | { |
| 58 | struct spm_partition_desc_t *part; |
| 59 | struct tfm_sfn_req_s desc; |
| 60 | int32_t args[4] = {0}; |
| 61 | int32_t fail_cnt = 0; |
| 62 | uint32_t idx; |
| 63 | |
| 64 | /* Call the init function for each partition */ |
| 65 | for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) { |
| 66 | part = &g_spm_partition_db.partitions[idx]; |
| 67 | tfm_spm_hal_configure_default_isolation(part->platform_data); |
| 68 | if (part->static_data.partition_init == NULL) { |
| 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; |
| 76 | desc.ns_caller = 0U; |
| 77 | desc.iovec_api = TFM_SFN_API_IOVEC; |
| 78 | desc.sfn = (sfn_t)part->static_data.partition_init; |
| 79 | desc.sp_id = part->static_data.partition_id; |
| 80 | res = tfm_core_sfn_request(&desc); |
| 81 | if (res == TFM_SUCCESS) { |
| 82 | tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE); |
| 83 | } else { |
| 84 | tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, res); |
| 85 | fail_cnt++; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | tfm_secure_api_init_done(); |
| 91 | |
| 92 | if (fail_cnt == 0) { |
| 93 | return SPM_ERR_OK; |
| 94 | } else { |
| 95 | return SPM_ERR_PARTITION_NOT_AVAILABLE; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void tfm_spm_partition_push_interrupted_ctx(uint32_t partition_idx) |
| 100 | { |
| 101 | struct spm_partition_runtime_data_t *runtime_data = |
| 102 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 103 | struct interrupted_ctx_stack_frame_t *stack_frame = |
Edison Ai | 7aff9e8 | 2019-07-11 14:56:46 +0800 | [diff] [blame] | 104 | (struct interrupted_ctx_stack_frame_t *)runtime_data->ctx_stack_ptr; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 105 | |
| 106 | stack_frame->partition_state = runtime_data->partition_state; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void tfm_spm_partition_pop_interrupted_ctx(uint32_t partition_idx) |
| 110 | { |
| 111 | struct spm_partition_runtime_data_t *runtime_data = |
| 112 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 113 | struct interrupted_ctx_stack_frame_t *stack_frame; |
| 114 | |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 115 | stack_frame = (struct interrupted_ctx_stack_frame_t *) |
| 116 | runtime_data->ctx_stack_ptr; |
| 117 | tfm_spm_partition_set_state(partition_idx, stack_frame->partition_state); |
| 118 | stack_frame->partition_state = 0; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void tfm_spm_partition_push_handler_ctx(uint32_t partition_idx) |
| 122 | { |
| 123 | struct spm_partition_runtime_data_t *runtime_data = |
| 124 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 125 | struct handler_ctx_stack_frame_t *stack_frame = |
| 126 | (struct handler_ctx_stack_frame_t *) |
| 127 | runtime_data->ctx_stack_ptr; |
| 128 | |
| 129 | stack_frame->partition_state = runtime_data->partition_state; |
| 130 | stack_frame->caller_partition_idx = runtime_data->caller_partition_idx; |
| 131 | |
| 132 | runtime_data->ctx_stack_ptr += |
| 133 | sizeof(struct handler_ctx_stack_frame_t) / sizeof(uint32_t); |
| 134 | } |
| 135 | |
| 136 | void tfm_spm_partition_pop_handler_ctx(uint32_t partition_idx) |
| 137 | { |
| 138 | struct spm_partition_runtime_data_t *runtime_data = |
| 139 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 140 | struct handler_ctx_stack_frame_t *stack_frame; |
| 141 | |
| 142 | runtime_data->ctx_stack_ptr -= |
| 143 | sizeof(struct handler_ctx_stack_frame_t) / sizeof(uint32_t); |
| 144 | |
| 145 | stack_frame = (struct handler_ctx_stack_frame_t *) |
| 146 | runtime_data->ctx_stack_ptr; |
| 147 | |
| 148 | tfm_spm_partition_set_state(partition_idx, stack_frame->partition_state); |
| 149 | stack_frame->partition_state = 0; |
| 150 | tfm_spm_partition_set_caller_partition_idx( |
| 151 | partition_idx, stack_frame->caller_partition_idx); |
| 152 | stack_frame->caller_partition_idx = 0; |
| 153 | } |
| 154 | |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 155 | void tfm_spm_partition_store_context(uint32_t partition_idx, |
| 156 | uint32_t stack_ptr, uint32_t lr) |
| 157 | { |
| 158 | g_spm_partition_db.partitions[partition_idx]. |
| 159 | runtime_data.stack_ptr = stack_ptr; |
| 160 | g_spm_partition_db.partitions[partition_idx]. |
| 161 | runtime_data.lr = lr; |
| 162 | } |
| 163 | |
| 164 | const struct spm_partition_runtime_data_t * |
| 165 | tfm_spm_partition_get_runtime_data(uint32_t partition_idx) |
| 166 | { |
| 167 | return &(g_spm_partition_db.partitions[partition_idx].runtime_data); |
| 168 | } |
| 169 | |
| 170 | void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state) |
| 171 | { |
| 172 | g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state = |
| 173 | state; |
| 174 | if (state == SPM_PARTITION_STATE_RUNNING || |
| 175 | state == SPM_PARTITION_STATE_HANDLING_IRQ) { |
| 176 | g_spm_partition_db.running_partition_idx = partition_idx; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void tfm_spm_partition_set_caller_partition_idx(uint32_t partition_idx, |
| 181 | uint32_t caller_partition_idx) |
| 182 | { |
| 183 | g_spm_partition_db.partitions[partition_idx].runtime_data. |
| 184 | caller_partition_idx = caller_partition_idx; |
| 185 | } |
| 186 | |
| 187 | void tfm_spm_partition_set_signal_mask(uint32_t partition_idx, |
| 188 | uint32_t signal_mask) |
| 189 | { |
| 190 | g_spm_partition_db.partitions[partition_idx].runtime_data. |
| 191 | signal_mask = signal_mask; |
| 192 | } |
| 193 | |
| 194 | void tfm_spm_partition_set_caller_client_id(uint32_t partition_idx, |
| 195 | int32_t caller_client_id) |
| 196 | { |
| 197 | g_spm_partition_db.partitions[partition_idx].runtime_data. |
| 198 | caller_client_id = caller_client_id; |
| 199 | } |
| 200 | |
| 201 | enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_idx, |
| 202 | uint32_t share) |
| 203 | { |
Edison Ai | 7aff9e8 | 2019-07-11 14:56:46 +0800 | [diff] [blame] | 204 | g_spm_partition_db.partitions[partition_idx].runtime_data.share = share; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 205 | |
Edison Ai | 7aff9e8 | 2019-07-11 14:56:46 +0800 | [diff] [blame] | 206 | return SPM_ERR_OK; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | enum spm_err_t tfm_spm_partition_set_iovec(uint32_t partition_idx, |
| 210 | const int32_t *args) |
| 211 | { |
| 212 | struct spm_partition_runtime_data_t *runtime_data = |
| 213 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 214 | size_t i; |
| 215 | |
| 216 | if ((args[1] < 0) || (args[3] < 0)) { |
| 217 | return SPM_ERR_INVALID_PARAMETER; |
| 218 | } |
| 219 | |
| 220 | runtime_data->iovec_args.in_len = (size_t)args[1]; |
| 221 | for (i = 0U; i < runtime_data->iovec_args.in_len; ++i) { |
| 222 | runtime_data->iovec_args.in_vec[i].base = |
| 223 | ((psa_invec *)args[0])[i].base; |
| 224 | runtime_data->iovec_args.in_vec[i].len = ((psa_invec *)args[0])[i].len; |
| 225 | } |
| 226 | runtime_data->iovec_args.out_len = (size_t)args[3]; |
| 227 | for (i = 0U; i < runtime_data->iovec_args.out_len; ++i) { |
| 228 | runtime_data->iovec_args.out_vec[i].base = |
| 229 | ((psa_outvec *)args[2])[i].base; |
| 230 | runtime_data->iovec_args.out_vec[i].len = |
| 231 | ((psa_outvec *)args[2])[i].len; |
| 232 | } |
| 233 | runtime_data->orig_outvec = (psa_outvec *)args[2]; |
| 234 | runtime_data->iovec_api = 1; |
| 235 | |
| 236 | return SPM_ERR_OK; |
| 237 | } |
| 238 | |
| 239 | uint32_t tfm_spm_partition_get_running_partition_idx(void) |
| 240 | { |
| 241 | return g_spm_partition_db.running_partition_idx; |
| 242 | } |
| 243 | |
| 244 | void tfm_spm_partition_cleanup_context(uint32_t partition_idx) |
| 245 | { |
| 246 | struct spm_partition_desc_t *partition = |
| 247 | &(g_spm_partition_db.partitions[partition_idx]); |
| 248 | int32_t i; |
| 249 | |
| 250 | partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX; |
| 251 | partition->runtime_data.share = 0; |
| 252 | partition->runtime_data.iovec_args.in_len = 0; |
| 253 | for (i = 0; i < PSA_MAX_IOVEC; ++i) { |
| 254 | partition->runtime_data.iovec_args.in_vec[i].base = 0; |
| 255 | partition->runtime_data.iovec_args.in_vec[i].len = 0; |
| 256 | } |
| 257 | partition->runtime_data.iovec_args.out_len = 0; |
| 258 | for (i = 0; i < PSA_MAX_IOVEC; ++i) { |
| 259 | partition->runtime_data.iovec_args.out_vec[i].base = 0; |
| 260 | partition->runtime_data.iovec_args.out_vec[i].len = 0; |
| 261 | } |
| 262 | partition->runtime_data.orig_outvec = 0; |
| 263 | partition->runtime_data.iovec_api = 0; |
| 264 | } |